Hex Artifact Content Вы: nobody
Вход

Artifact 9aa84aa63dda2ce25862f4058123ce9434d22d46:


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 0d  ***************.
0050: 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73  .** This file is
0060: 20 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e   an amalgamation
0070: 20 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74   of many separat
0080: 65 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73  e C source files
0090: 20 66 72 6f 6d 20 53 51 4c 69 74 65 0d 0a 2a 2a   from SQLite..**
00a0: 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 31 30 2e   version 3.7.10.
00b0: 20 20 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61    By combining a
00c0: 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61  ll the individua
00d0: 6c 20 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69  l C code files i
00e0: 6e 74 6f 20 74 68 69 73 20 0d 0a 2a 2a 20 73 69  nto this ..** si
00f0: 6e 67 6c 65 20 6c 61 72 67 65 20 66 69 6c 65 2c  ngle large file,
0100: 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65   the entire code
0110: 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64   can be compiled
0120: 20 61 73 20 61 20 73 69 6e 67 6c 65 20 74 72 61   as a single tra
0130: 6e 73 6c 61 74 69 6f 6e 0d 0a 2a 2a 20 75 6e 69  nslation..** uni
0140: 74 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  t.  This allows 
0150: 6d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 20 74  many compilers t
0160: 6f 20 64 6f 20 6f 70 74 69 6d 69 7a 61 74 69 6f  o do optimizatio
0170: 6e 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f  ns that would no
0180: 74 20 62 65 0d 0a 2a 2a 20 70 6f 73 73 69 62 6c  t be..** possibl
0190: 65 20 69 66 20 74 68 65 20 66 69 6c 65 73 20 77  e if the files w
01a0: 65 72 65 20 63 6f 6d 70 69 6c 65 64 20 73 65 70  ere compiled sep
01b0: 61 72 61 74 65 6c 79 2e 20 20 50 65 72 66 6f 72  arately.  Perfor
01c0: 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e  mance improvemen
01d0: 74 73 0d 0a 2a 2a 20 6f 66 20 35 25 20 6f 72 20  ts..** of 5% or 
01e0: 6d 6f 72 65 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c  more are commonl
01f0: 79 20 73 65 65 6e 20 77 68 65 6e 20 53 51 4c 69  y seen when SQLi
0200: 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61  te is compiled a
0210: 73 20 61 20 73 69 6e 67 6c 65 0d 0a 2a 2a 20 74  s a single..** t
0220: 72 61 6e 73 6c 61 74 69 6f 6e 20 75 6e 69 74 2e  ranslation unit.
0230: 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69  ..**..** This fi
0240: 6c 65 20 69 73 20 61 6c 6c 20 79 6f 75 20 6e 65  le is all you ne
0250: 65 64 20 74 6f 20 63 6f 6d 70 69 6c 65 20 53 51  ed to compile SQ
0260: 4c 69 74 65 2e 20 20 54 6f 20 75 73 65 20 53 51  Lite.  To use SQ
0270: 4c 69 74 65 20 69 6e 20 6f 74 68 65 72 0d 0a 2a  Lite in other..*
0280: 2a 20 70 72 6f 67 72 61 6d 73 2c 20 79 6f 75 20  * programs, you 
0290: 6e 65 65 64 20 74 68 69 73 20 66 69 6c 65 20 61  need this file a
02a0: 6e 64 20 74 68 65 20 22 73 71 6c 69 74 65 33 2e  nd the "sqlite3.
02b0: 68 22 20 68 65 61 64 65 72 20 66 69 6c 65 20 74  h" header file t
02c0: 68 61 74 20 64 65 66 69 6e 65 73 0d 0a 2a 2a 20  hat defines..** 
02d0: 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20  the programming 
02e0: 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
02f0: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
0300: 20 20 28 49 66 20 79 6f 75 20 64 6f 20 6e 6f 74    (If you do not
0310: 20 68 61 76 65 20 0d 0a 2a 2a 20 74 68 65 20 22   have ..** the "
0320: 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 64 65  sqlite3.h" heade
0330: 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 2c 20  r file at hand, 
0340: 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 61 20  you will find a 
0350: 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20 77 69  copy embedded wi
0360: 74 68 69 6e 0d 0a 2a 2a 20 74 68 65 20 74 65 78  thin..** the tex
0370: 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e 20  t of this file. 
0380: 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65 67   Search for "Beg
0390: 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 2e  in file sqlite3.
03a0: 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73  h" to find the s
03b0: 74 61 72 74 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  tart..** of the 
03c0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03d0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03e0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03f0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
0400: 65 64 65 64 0d 0a 2a 2a 20 69 66 20 79 6f 75 20  eded..** if you 
0410: 77 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74  want a wrapper t
0420: 6f 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69  o interface SQLi
0430: 74 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f  te with your cho
0440: 69 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69  ice of programmi
0450: 6e 67 0d 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e  ng..** language.
0460: 20 54 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68   The code for th
0470: 65 20 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d  e "sqlite3" comm
0480: 61 6e 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69  and-line shell i
0490: 73 20 61 6c 73 6f 20 69 6e 20 61 0d 0a 2a 2a 20  s also in a..** 
04a0: 73 65 70 61 72 61 74 65 20 66 69 6c 65 2e 20 54  separate file. T
04b0: 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
04c0: 73 20 6f 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20  s only code for 
04d0: 74 68 65 20 63 6f 72 65 20 53 51 4c 69 74 65 20  the core SQLite 
04e0: 6c 69 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 23 64  library...*/..#d
04f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52  efine SQLITE_COR
0500: 45 20 31 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  E 1..#define SQL
0510: 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
0520: 20 31 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1..#ifndef SQLI
0530: 54 45 5f 50 52 49 56 41 54 45 0d 0a 23 20 64 65  TE_PRIVATE..# de
0540: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 56  fine SQLITE_PRIV
0550: 41 54 45 20 73 74 61 74 69 63 0d 0a 23 65 6e 64  ATE static..#end
0560: 69 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
0570: 54 45 5f 41 50 49 0d 0a 23 20 64 65 66 69 6e 65  TE_API..# define
0580: 20 53 51 4c 49 54 45 5f 41 50 49 0d 0a 23 65 6e   SQLITE_API..#en
0590: 64 69 66 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
05a0: 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
05b0: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05e0: 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
05f0: 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a  1 September 15..
0600: 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
0610: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
0620: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
0630: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
0640: 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
0650: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0660: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
0670: 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
0680: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0690: 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
06a0: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
06b0: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
06c0: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
06d0: 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
06e0: 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
06f0: 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
0700: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
0710: 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
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: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0760: 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 49 6e 74 65 72  ******..** Inter
0770: 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 20 64 65  nal interface de
0780: 66 69 6e 69 74 69 6f 6e 73 20 66 6f 72 20 53 51  finitions for SQ
0790: 4c 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 23  Lite...**..*/..#
07a0: 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e  ifndef _SQLITEIN
07b0: 54 5f 48 5f 0d 0a 23 64 65 66 69 6e 65 20 5f 53  T_H_..#define _S
07c0: 51 4c 49 54 45 49 4e 54 5f 48 5f 0d 0a 0d 0a 2f  QLITEINT_H_..../
07d0: 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66  *..** These #def
07e0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62  ines should enab
07f0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70  le >2GB file sup
0800: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66  port on POSIX if
0810: 20 74 68 65 0d 0a 2a 2a 20 75 6e 64 65 72 6c 79   the..** underly
0820: 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ing operating sy
0830: 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74  stem supports it
0840: 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63  .  If the OS lac
0850: 6b 73 0d 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  ks..** large fil
0860: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
0870: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
0880: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
0890: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0d 0a 2a 2a 0d   be no-ops...**.
08a0: 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39  .** Ticket #2739
08b0: 3a 20 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c  :  The _LARGEFIL
08c0: 45 5f 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d  E_SOURCE macro m
08d0: 75 73 74 20 61 70 70 65 61 72 20 62 65 66 6f 72  ust appear befor
08e0: 65 20 61 6e 79 0d 0a 2a 2a 20 73 79 73 74 65 6d  e any..** system
08f0: 20 23 69 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e   #includes.  Hen
0900: 63 65 2c 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f  ce, this block o
0910: 66 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 74  f code must be t
0920: 68 65 20 76 65 72 79 20 66 69 72 73 74 0d 0a 2a  he very first..*
0930: 2a 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20 73 6f  * code in all so
0940: 75 72 63 65 20 66 69 6c 65 73 2e 0d 0a 2a 2a 0d  urce files...**.
0950: 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 73  .** Large file s
0960: 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 69  upport can be di
0970: 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65  sabled using the
0980: 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 4c   -DSQLITE_DISABL
0990: 45 5f 4c 46 53 20 73 77 69 74 63 68 0d 0a 2a 2a  E_LFS switch..**
09a0: 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   on the compiler
09b0: 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20   command line.  
09c0: 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
09d0: 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d  y if you are com
09e0: 70 69 6c 69 6e 67 0d 0a 2a 2a 20 6f 6e 20 61 20  piling..** on a 
09f0: 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28  recent machine (
0a00: 65 78 3a 20 52 65 64 20 48 61 74 20 37 2e 32 29  ex: Red Hat 7.2)
0a10: 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f   but you want yo
0a20: 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0d  ur code to work.
0a30: 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20  .** on an older 
0a40: 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64  machine (ex: Red
0a50: 20 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79   Hat 6.0).  If y
0a60: 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65  ou compile on Re
0a70: 64 20 48 61 74 20 37 2e 32 0d 0a 2a 2a 20 77 69  d Hat 7.2..** wi
0a80: 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f  thout this optio
0a90: 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65  n, LFS is enable
0aa0: 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20  .  But LFS does 
0ab0: 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 65  not exist in the
0ac0: 20 6b 65 72 6e 65 6c 0d 0a 2a 2a 20 69 6e 20 52   kernel..** in R
0ad0: 65 64 20 48 61 74 20 36 2e 30 2c 20 73 6f 20 74  ed Hat 6.0, so t
0ae0: 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f  he code won't wo
0af0: 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20  rk.  Hence, for 
0b00: 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0d 0a  maximum binary..
0b10: 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79  ** portability y
0b20: 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c  ou should omit L
0b30: 46 53 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 69 6d 69  FS...**..** Simi
0b40: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20  lar is true for 
0b50: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69  Mac OS X.  LFS i
0b60: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
0b70: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61   on Mac OS X 9 a
0b80: 6e 64 20 6c 61 74 65 72 2e 0d 0a 2a 2f 0d 0a 23  nd later...*/..#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0d 0a 23 20 64 65 66  SABLE_LFS..# def
0bb0: 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20  ine _LARGE_FILE 
0bc0: 20 20 20 20 20 20 31 0d 0a 23 20 69 66 6e 64 65        1..# ifnde
0bd0: 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42  f _FILE_OFFSET_B
0be0: 49 54 53 0d 0a 23 20 20 20 64 65 66 69 6e 65 20  ITS..#   define 
0bf0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
0c00: 53 20 36 34 0d 0a 23 20 65 6e 64 69 66 0d 0a 23  S 64..# endif..#
0c10: 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49   define _LARGEFI
0c20: 4c 45 5f 53 4f 55 52 43 45 20 31 0d 0a 23 65 6e  LE_SOURCE 1..#en
0c30: 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  dif..../*..** In
0c40: 63 6c 75 64 65 20 74 68 65 20 63 6f 6e 66 69 67  clude the config
0c50: 75 72 61 74 69 6f 6e 20 68 65 61 64 65 72 20 6f  uration header o
0c60: 75 74 70 75 74 20 62 79 20 27 63 6f 6e 66 69 67  utput by 'config
0c70: 75 72 65 27 20 69 66 20 77 65 27 72 65 20 75 73  ure' if we're us
0c80: 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 61 75 74 6f  ing the..** auto
0c90: 63 6f 6e 66 2d 62 61 73 65 64 20 62 75 69 6c 64  conf-based build
0ca0: 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 5f 48 41  ..*/..#ifdef _HA
0cb0: 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  VE_SQLITE_CONFIG
0cc0: 5f 48 0d 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f  _H..#include "co
0cd0: 6e 66 69 67 2e 68 22 0d 0a 23 65 6e 64 69 66 0d  nfig.h"..#endif.
0ce0: 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
0cf0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
0d00: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20  eLimit.h in the 
0d10: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
0d20: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
0d30: 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
0d40: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
0d50: 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a  qliteLimit.h ***
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d80: 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 37  **/../*..** 2007
0d90: 20 4d 61 79 20 37 0d 0a 2a 2a 0d 0a 2a 2a 20 54   May 7..**..** T
0da0: 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
0db0: 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
0dc0: 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
0dd0: 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
0de0: 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
0df0: 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
0e00: 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
0e10: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
0e20: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
0e30: 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
0e40: 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
0e50: 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
0e60: 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
0e70: 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
0e80: 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
0e90: 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
0ea0: 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
0eb0: 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ee0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ef0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
0f00: 2a 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ** ..** This fil
0f10: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75  e defines variou
0f20: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74  s limits of what
0f30: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63   SQLite can proc
0f40: 65 73 73 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  ess...*/..../*..
0f50: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c  ** The maximum l
0f60: 65 6e 67 74 68 20 6f 66 20 61 20 54 45 58 54 20  ength of a TEXT 
0f70: 6f 72 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73  or BLOB in bytes
0f80: 2e 20 20 20 54 68 69 73 20 61 6c 73 6f 0d 0a 2a  .   This also..*
0f90: 2a 20 6c 69 6d 69 74 73 20 74 68 65 20 73 69 7a  * limits the siz
0fa0: 65 20 6f 66 20 61 20 72 6f 77 20 69 6e 20 61 20  e of a row in a 
0fb0: 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0d  table or index..
0fc0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 68 61 72 64  .**..** The hard
0fd0: 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20 61 62   limit is the ab
0fe0: 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d 62 69  ility of a 32-bi
0ff0: 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
1000: 0d 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74 68  ..** to count th
1010: 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20 6f  e size: 2^31-1 o
1020: 72 20 32 31 34 37 34 38 33 36 34 37 2e 0d 0a 2a  r 2147483647...*
1030: 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1040: 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0d 0a 23 20  E_MAX_LENGTH..# 
1050: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1060: 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30  X_LENGTH 1000000
1070: 30 30 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  000..#endif..../
1080: 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  *..** This is th
1090: 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
10a0: 20 6f 66 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 2a   of..**..**    *
10b0: 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61   Columns in a ta
10c0: 62 6c 65 0d 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c  ble..**    * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78  umns in an index
10e0: 0d 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e  ..**    * Column
10f0: 73 20 69 6e 20 61 20 76 69 65 77 0d 0a 2a 2a 20  s in a view..** 
1100: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
1110: 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20  e SET clause of 
1120: 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
1130: 65 6e 74 0d 0a 2a 2a 20 20 20 20 2a 20 54 65 72  ent..**    * Ter
1140: 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ms in the result
1150: 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54   set of a SELECT
1160: 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 20   statement..**  
1170: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65    * Terms in the
1180: 20 47 52 4f 55 50 20 42 59 20 6f 72 20 4f 52 44   GROUP BY or ORD
1190: 45 52 20 42 59 20 63 6c 61 75 73 65 73 20 6f 66  ER BY clauses of
11a0: 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
11b0: 65 6e 74 2e 0d 0a 2a 2a 20 20 20 20 2a 20 54 65  ent...**    * Te
11c0: 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c 55 45  rms in the VALUE
11d0: 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 49  S clause of an I
11e0: 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 0d  NSERT statement.
11f0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 68 61 72 64  .**..** The hard
1200: 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72   upper limit her
1210: 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73  e is 32676.  Mos
1220: 74 20 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c  t database peopl
1230: 65 20 77 69 6c 6c 0d 0a 2a 2a 20 74 65 6c 6c 20  e will..** tell 
1240: 79 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 65  you that in a we
1250: 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61  ll-normalized da
1260: 74 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 61  tabase, you usua
1270: 6c 6c 79 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 6e  lly should..** n
1280: 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61  ot have more tha
1290: 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20  n a dozen or so 
12a0: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74  columns in any t
12b0: 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0d 0a 2a  able.  And if..*
12c0: 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  * that is the ca
12d0: 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  se, there is no 
12e0: 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 20  point in having 
12f0: 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 0d  more than a few.
1300: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73  .** dozen values
1310: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
1320: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20  ther situations 
1330: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e  described above.
1340: 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
1350: 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0d  LITE_MAX_COLUMN.
1360: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1370: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30  _MAX_COLUMN 2000
1380: 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
1390: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c  ** The maximum l
13a0: 65 6e 67 74 68 20 6f 66 20 61 20 73 69 6e 67 6c  ength of a singl
13b0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
13c0: 69 6e 20 62 79 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a  in bytes...**..*
13d0: 2a 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 20  * It used to be 
13e0: 74 68 65 20 63 61 73 65 20 74 68 61 74 20 73 65  the case that se
13f0: 74 74 69 6e 67 20 74 68 69 73 20 76 61 6c 75 65  tting this value
1400: 20 74 6f 20 7a 65 72 6f 20 77 6f 75 6c 64 0d 0a   to zero would..
1410: 2a 2a 20 74 75 72 6e 20 74 68 65 20 6c 69 6d 69  ** turn the limi
1420: 74 20 6f 66 66 2e 20 20 54 68 61 74 20 69 73 20  t off.  That is 
1430: 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20  no longer true. 
1440: 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
1450: 62 6c 65 0d 0a 2a 2a 20 74 6f 20 74 75 72 6e 20  ble..** to turn 
1460: 74 68 69 73 20 6c 69 6d 69 74 20 6f 66 66 2e 0d  this limit off..
1470: 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
1480: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1490: 54 48 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  TH..# define SQL
14a0: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
14b0: 54 48 20 31 30 30 30 30 30 30 30 30 30 0d 0a 23  TH 1000000000..#
14c0: 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
14d0: 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74  The maximum dept
14e0: 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  h of an expressi
14f0: 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20 69 73  on tree. This is
1500: 20 6c 69 6d 69 74 65 64 20 74 6f 20 0d 0a 2a 2a   limited to ..**
1510: 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79 20   some extent by 
1520: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
1530: 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65 74  ENGTH. But somet
1540: 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0d 0a  ime you might ..
1550: 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65  ** want to place
1560: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d   more severe lim
1570: 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c  its on the compl
1580: 65 78 69 74 79 20 6f 66 20 61 6e 20 0d 0a 2a 2a  exity of an ..**
1590: 20 65 78 70 72 65 73 73 69 6f 6e 2e 0d 0a 2a 2a   expression...**
15a0: 0d 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20  ..** A value of 
15b0: 30 20 75 73 65 64 20 74 6f 20 6d 65 61 6e 20 74  0 used to mean t
15c0: 68 61 74 20 74 68 65 20 6c 69 6d 69 74 20 77 61  hat the limit wa
15d0: 73 20 6e 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0d  s not enforced..
15e0: 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 69 73 20  .** But that is 
15f0: 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20  no longer true. 
1600: 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20 6e 6f   The limit is no
1610: 77 20 73 74 72 69 63 74 6c 79 20 65 6e 66 6f 72  w strictly enfor
1620: 63 65 64 0d 0a 2a 2a 20 61 74 20 61 6c 6c 20 74  ced..** at all t
1630: 69 6d 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  imes...*/..#ifnd
1640: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  ef SQLITE_MAX_EX
1650: 50 52 5f 44 45 50 54 48 0d 0a 23 20 64 65 66 69  PR_DEPTH..# defi
1660: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  ne SQLITE_MAX_EX
1670: 50 52 5f 44 45 50 54 48 20 31 30 30 30 0d 0a 23  PR_DEPTH 1000..#
1680: 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
1690: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
16a0: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61  er of terms in a
16b0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
16c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 20   statement...** 
16d0: 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
16e0: 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20  or for compound 
16f0: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1700: 73 20 64 6f 65 73 20 6f 6e 65 0d 0a 2a 2a 20 6c  s does one..** l
1710: 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f  evel of recursio
1720: 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e  n for each term.
1730: 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c    A stack overfl
1740: 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0d 0a 2a  ow can result..*
1750: 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  * if the number 
1760: 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f 20  of terms is too 
1770: 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 74  large.  In pract
1780: 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0d 0a 2a  ice, most SQL..*
1790: 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65  * never has more
17a0: 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72   than 3 or 4 ter
17b0: 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65  ms.  Use a value
17c0: 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65   of 0 to disable
17d0: 0d 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f  ..** any limit o
17e0: 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
17f0: 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f  terms in a compo
1800: 75 6e 74 20 53 45 4c 45 43 54 2e 0d 0a 2a 2f 0d  unt SELECT...*/.
1810: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1820: 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
1830: 45 43 54 0d 0a 23 20 64 65 66 69 6e 65 20 53 51  ECT..# define SQ
1840: 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e  LITE_MAX_COMPOUN
1850: 44 5f 53 45 4c 45 43 54 20 35 30 30 0d 0a 23 65  D_SELECT 500..#e
1860: 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ndif..../*..** T
1870: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1880: 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69 6e 20  r of opcodes in 
1890: 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0d  a VDBE program..
18a0: 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c  .** Not currentl
18b0: 79 20 65 6e 66 6f 72 63 65 64 2e 0d 0a 2a 2f 0d  y enforced...*/.
18c0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18d0: 4d 41 58 5f 56 44 42 45 5f 4f 50 0d 0a 23 20 64  MAX_VDBE_OP..# d
18e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
18f0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0d 0a  _VDBE_OP 25000..
1900: 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
1910: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1920: 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
1930: 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74   to an SQL funct
1940: 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  ion...*/..#ifnde
1950: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  f SQLITE_MAX_FUN
1960: 43 54 49 4f 4e 5f 41 52 47 0d 0a 23 20 64 65 66  CTION_ARG..# def
1970: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ine SQLITE_MAX_F
1980: 55 4e 43 54 49 4f 4e 5f 41 52 47 20 31 32 37 0d  UNCTION_ARG 127.
1990: 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
19a0: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  * The maximum nu
19b0: 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  mber of in-memor
19c0: 79 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 66  y pages to use f
19d0: 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
19e0: 62 61 73 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 61  base..** table a
19f0: 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  nd for temporary
1a00: 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 53 51   tables.  The SQ
1a10: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
1a20: 48 45 5f 53 49 5a 45 0d 0a 2a 2f 0d 0a 23 69 66  HE_SIZE..*/..#if
1a30: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
1a40: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0d 0a  ULT_CACHE_SIZE..
1a50: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
1a70: 5a 45 20 20 32 30 30 30 0d 0a 23 65 6e 64 69 66  ZE  2000..#endif
1a80: 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1a90: 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41  _DEFAULT_TEMP_CA
1aa0: 43 48 45 5f 53 49 5a 45 0d 0a 23 20 64 65 66 69  CHE_SIZE..# defi
1ab0: 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
1ac0: 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a  T_TEMP_CACHE_SIZ
1ad0: 45 20 20 35 30 30 0d 0a 23 65 6e 64 69 66 0d 0a  E  500..#endif..
1ae0: 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 64 65 66  ../*..** The def
1af0: 61 75 6c 74 20 6e 75 6d 62 65 72 20 6f 66 20 66  ault number of f
1b00: 72 61 6d 65 73 20 74 6f 20 61 63 63 75 6d 75 6c  rames to accumul
1b10: 61 74 65 20 69 6e 20 74 68 65 20 6c 6f 67 20 66  ate in the log f
1b20: 69 6c 65 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 63  ile before..** c
1b30: 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20 74 68 65  heckpointing the
1b40: 20 64 61 74 61 62 61 73 65 20 69 6e 20 57 41 4c   database in WAL
1b50: 20 6d 6f 64 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e   mode...*/..#ifn
1b60: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
1b70: 4c 54 5f 57 41 4c 5f 41 55 54 4f 43 48 45 43 4b  LT_WAL_AUTOCHECK
1b80: 50 4f 49 4e 54 0d 0a 23 20 64 65 66 69 6e 65 20  POINT..# define 
1b90: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 57  SQLITE_DEFAULT_W
1ba0: 41 4c 5f 41 55 54 4f 43 48 45 43 4b 50 4f 49 4e  AL_AUTOCHECKPOIN
1bb0: 54 20 20 31 30 30 30 0d 0a 23 65 6e 64 69 66 0d  T  1000..#endif.
1bc0: 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6d 61  .../*..** The ma
1bd0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1be0: 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1bf0: 65 73 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62  es.  This must b
1c00: 65 20 62 65 74 77 65 65 6e 20 30 0d 0a 2a 2a 20  e between 0..** 
1c10: 61 6e 64 20 36 32 2e 20 20 54 68 65 20 75 70 70  and 62.  The upp
1c20: 65 72 20 62 6f 75 6e 64 20 6f 6e 20 36 32 20 69  er bound on 62 i
1c30: 73 20 62 65 63 61 75 73 65 20 61 20 36 34 2d 62  s because a 64-b
1c40: 69 74 20 69 6e 74 65 67 65 72 20 62 69 74 6d 61  it integer bitma
1c50: 70 0d 0a 2a 2a 20 69 73 20 75 73 65 64 20 69 6e  p..** is used in
1c60: 74 65 72 6e 61 6c 6c 79 20 74 6f 20 74 72 61 63  ternally to trac
1c70: 6b 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  k attached datab
1c80: 61 73 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  ases...*/..#ifnd
1c90: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  ef SQLITE_MAX_AT
1ca0: 54 41 43 48 45 44 0d 0a 23 20 64 65 66 69 6e 65  TACHED..# define
1cb0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41   SQLITE_MAX_ATTA
1cc0: 43 48 45 44 20 31 30 0d 0a 23 65 6e 64 69 66 0d  CHED 10..#endif.
1cd0: 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  ...../*..** The 
1ce0: 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  maximum value of
1cf0: 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64   a ?nnn wildcard
1d00: 20 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72   that the parser
1d10: 20 77 69 6c 6c 20 61 63 63 65 70 74 2e 0d 0a 2a   will accept...*
1d20: 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1d30: 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  E_MAX_VARIABLE_N
1d40: 55 4d 42 45 52 0d 0a 23 20 64 65 66 69 6e 65 20  UMBER..# define 
1d50: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
1d60: 42 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0d 0a  BLE_NUMBER 999..
1d70: 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 4d 61 78  #endif..../* Max
1d80: 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 20  imum page size. 
1d90: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64   The upper bound
1da0: 20 6f 6e 20 74 68 69 73 20 76 61 6c 75 65 20 69   on this value i
1db0: 73 20 36 35 35 33 36 2e 20 20 54 68 69 73 20 61  s 65536.  This a
1dc0: 20 6c 69 6d 69 74 0d 0a 2a 2a 20 69 6d 70 6f 73   limit..** impos
1dd0: 65 64 20 62 79 20 74 68 65 20 75 73 65 20 6f 66  ed by the use of
1de0: 20 31 36 2d 62 69 74 20 6f 66 66 73 65 74 73 20   16-bit offsets 
1df0: 77 69 74 68 69 6e 20 65 61 63 68 20 70 61 67 65  within each page
1e00: 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61 72 6c 69 65  ...**..** Earlie
1e10: 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
1e20: 4c 69 74 65 20 61 6c 6c 6f 77 65 64 20 74 68 65  Lite allowed the
1e30: 20 75 73 65 72 20 74 6f 20 63 68 61 6e 67 65 20   user to change 
1e40: 74 68 69 73 20 76 61 6c 75 65 20 61 74 0d 0a 2a  this value at..*
1e50: 2a 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20  * compile time. 
1e60: 54 68 69 73 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  This is no longe
1e70: 72 20 70 65 72 6d 69 74 74 65 64 2c 20 6f 6e 20  r permitted, on 
1e80: 74 68 65 20 67 72 6f 75 6e 64 73 20 74 68 61 74  the grounds that
1e90: 20 69 74 20 63 72 65 61 74 65 73 0d 0a 2a 2a 20   it creates..** 
1ea0: 61 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  a library that i
1eb0: 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 20 69 6e  s technically in
1ec0: 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20  compatible with 
1ed0: 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  an SQLite librar
1ee0: 79 20 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20  y ..** compiled 
1ef0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74  with a different
1f00: 20 6c 69 6d 69 74 2e 20 49 66 20 61 20 70 72 6f   limit. If a pro
1f10: 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 6f  cess operating o
1f20: 6e 20 61 20 64 61 74 61 62 61 73 65 20 0d 0a 2a  n a database ..*
1f30: 2a 20 77 69 74 68 20 61 20 70 61 67 65 2d 73 69  * with a page-si
1f40: 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74 65  ze of 65536 byte
1f50: 73 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20  s crashes, then 
1f60: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53  an instance of S
1f70: 51 4c 69 74 65 20 0d 0a 2a 2a 20 63 6f 6d 70 69  QLite ..** compi
1f80: 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66  led with the def
1f90: 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 6c  ault page-size l
1fa0: 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  imit will not be
1fb0: 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63   able to rollbac
1fc0: 6b 20 0d 0a 2a 2a 20 74 68 65 20 61 62 6f 72 74  k ..** the abort
1fd0: 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ed transaction. 
1fe0: 54 68 69 73 20 63 6f 75 6c 64 20 6c 65 61 64 20  This could lead 
1ff0: 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  to database corr
2000: 75 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66  uption...*/..#if
2010: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  def SQLITE_MAX_P
2020: 41 47 45 5f 53 49 5a 45 0d 0a 23 20 75 6e 64 65  AGE_SIZE..# unde
2030: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  f SQLITE_MAX_PAG
2040: 45 5f 53 49 5a 45 0d 0a 23 65 6e 64 69 66 0d 0a  E_SIZE..#endif..
2050: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
2060: 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 36 35 35  AX_PAGE_SIZE 655
2070: 33 36 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  36....../*..** T
2080: 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20  he default size 
2090: 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61  of a database pa
20a0: 67 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  ge...*/..#ifndef
20b0: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
20c0: 50 41 47 45 5f 53 49 5a 45 0d 0a 23 20 64 65 66  PAGE_SIZE..# def
20d0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
20e0: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 31 30 32  LT_PAGE_SIZE 102
20f0: 34 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 53  4..#endif..#if S
2100: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
2110: 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d  GE_SIZE>SQLITE_M
2120: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0d 0a 23 20  AX_PAGE_SIZE..# 
2130: 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  undef SQLITE_DEF
2140: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0d 0a  AULT_PAGE_SIZE..
2150: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2160: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
2170: 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  E SQLITE_MAX_PAG
2180: 45 5f 53 49 5a 45 0d 0a 23 65 6e 64 69 66 0d 0a  E_SIZE..#endif..
2190: 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 72 64 69 6e 61 72  ../*..** Ordinar
21a0: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65  ily, if no value
21b0: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70   is explicitly p
21c0: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20  rovided, SQLite 
21d0: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65  creates database
21e0: 73 0d 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20  s..** with page 
21f0: 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41  size SQLITE_DEFA
2200: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48  ULT_PAGE_SIZE. H
2210: 6f 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e  owever, based on
2220: 20 63 65 72 74 61 69 6e 0d 0a 2a 2a 20 64 65 76   certain..** dev
2230: 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ice characterist
2240: 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65  ics (sector-size
2250: 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74   and atomic writ
2260: 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0d 0a 2a  e() support),..*
2270: 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 6f  * SQLite may cho
2280: 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 6c  ose a larger val
2290: 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 6e  ue. This constan
22a0: 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  t is the maximum
22b0: 20 76 61 6c 75 65 0d 0a 2a 2a 20 53 51 4c 69 74   value..** SQLit
22c0: 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e  e will choose on
22d0: 20 69 74 73 20 6f 77 6e 2e 0d 0a 2a 2f 0d 0a 23   its own...*/..#
22e0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
22f0: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
2300: 49 5a 45 0d 0a 23 20 64 65 66 69 6e 65 20 53 51  IZE..# define SQ
2310: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
2320: 5f 50 41 47 45 5f 53 49 5a 45 20 38 31 39 32 0d  _PAGE_SIZE 8192.
2330: 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 53 51 4c  .#endif..#if SQL
2340: 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f  ITE_MAX_DEFAULT_
2350: 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45  PAGE_SIZE>SQLITE
2360: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0d 0a  _MAX_PAGE_SIZE..
2370: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  # undef SQLITE_M
2380: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
2390: 53 49 5a 45 0d 0a 23 20 64 65 66 69 6e 65 20 53  SIZE..# define S
23a0: 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
23b0: 54 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49  T_PAGE_SIZE SQLI
23c0: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
23d0: 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a  ..#endif....../*
23e0: 0d 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d  ..** Maximum num
23f0: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
2400: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
2410: 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  e...**..** This 
2420: 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74  is really just t
2430: 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2440: 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67   for the max_pag
2450: 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0d  e_count pragma..
2460: 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 63  .** This value c
2470: 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 6f  an be lowered (o
2480: 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 6e  r raised) at run
2490: 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 74  -time using that
24a0: 20 74 68 65 0d 0a 2a 2a 20 6d 61 78 5f 70 61 67   the..** max_pag
24b0: 65 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0d 0a  e_count macro...
24c0: 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
24d0: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e  TE_MAX_PAGE_COUN
24e0: 54 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  T..# define SQLI
24f0: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e  TE_MAX_PAGE_COUN
2500: 54 20 31 30 37 33 37 34 31 38 32 33 0d 0a 23 65  T 1073741823..#e
2510: 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  ndif..../*..** M
2520: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69  aximum length (i
2530: 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20  n bytes) of the 
2540: 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b  pattern in a LIK
2550: 45 20 6f 72 20 47 4c 4f 42 0d 0a 2a 2a 20 6f 70  E or GLOB..** op
2560: 65 72 61 74 6f 72 2e 0d 0a 2a 2f 0d 0a 23 69 66  erator...*/..#if
2570: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
2580: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
2590: 47 54 48 0d 0a 23 20 64 65 66 69 6e 65 20 53 51  GTH..# define SQ
25a0: 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
25b0: 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30  TTERN_LENGTH 500
25c0: 30 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  00..#endif..../*
25d0: 0d 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  ..** Maximum dep
25e0: 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  th of recursion 
25f0: 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0d 0a 2a  for triggers...*
2600: 2a 0d 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66  *..** A value of
2610: 20 31 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20   1 means that a 
2620: 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
2630: 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65  will not be able
2640: 20 74 6f 20 69 74 73 65 6c 66 0d 0a 2a 2a 20 66   to itself..** f
2650: 69 72 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ire any triggers
2660: 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 6d  . A value of 0 m
2670: 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 69  eans that no tri
2680: 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 61 74  gger programs at
2690: 20 61 6c 6c 20 0d 0a 2a 2a 20 6d 61 79 20 62 65   all ..** may be
26a0: 20 65 78 65 63 75 74 65 64 2e 0d 0a 2a 2f 0d 0a   executed...*/..
26b0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
26c0: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  AX_TRIGGER_DEPTH
26d0: 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ..# define SQLIT
26e0: 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
26f0: 50 54 48 20 31 30 30 30 0d 0a 23 65 6e 64 69 66  PTH 1000..#endif
2700: 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
2710: 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
2720: 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  eLimit.h *******
2730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2750: 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
2760: 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
2770: 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
2780: 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
2790: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
27a0: 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 20 44 69 73 61 62  ***/..../* Disab
27b0: 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 6e  le nuisance warn
27c0: 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 20  ings on Borland 
27d0: 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0d 0a 23 69  compilers */..#i
27e0: 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c  f defined(__BORL
27f0: 41 4e 44 43 5f 5f 29 0d 0a 23 70 72 61 67 6d 61  ANDC__)..#pragma
2800: 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e   warn -rch /* un
2810: 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a  reachable code *
2820: 2f 0d 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  /..#pragma warn 
2830: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f  -ccc /* Conditio
2840: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65  n is always true
2850: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0d 0a 23 70   or false */..#p
2860: 72 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20  ragma warn -aus 
2870: 2f 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75  /* Assigned valu
2880: 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20  e is never used 
2890: 2a 2f 0d 0a 23 70 72 61 67 6d 61 20 77 61 72 6e  */..#pragma warn
28a0: 20 2d 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69   -csu /* Compari
28b0: 6e 67 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e  ng signed and un
28c0: 73 69 67 6e 65 64 20 2a 2f 0d 0a 23 70 72 61 67  signed */..#prag
28d0: 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20  ma warn -spa /* 
28e0: 53 75 73 70 69 63 69 6f 75 73 20 70 6f 69 6e 74  Suspicious point
28f0: 65 72 20 61 72 69 74 68 6d 65 74 69 63 20 2a 2f  er arithmetic */
2900: 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 4e  ..#endif..../* N
2910: 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75  eeded for variou
2920: 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e  s definitions...
2930: 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 47 4e   */..#ifndef _GN
2940: 55 5f 53 4f 55 52 43 45 0d 0a 23 20 64 65 66 69  U_SOURCE..# defi
2950: 6e 65 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0d 0a  ne _GNU_SOURCE..
2960: 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
2970: 20 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 61 72   Include standar
2980: 64 20 68 65 61 64 65 72 20 66 69 6c 65 73 20 61  d header files a
2990: 73 20 6e 65 63 65 73 73 61 72 79 0d 0a 2a 2f 0d  s necessary..*/.
29a0: 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 44  .#ifdef HAVE_STD
29b0: 49 4e 54 5f 48 0d 0a 23 69 6e 63 6c 75 64 65 20  INT_H..#include 
29c0: 3c 73 74 64 69 6e 74 2e 68 3e 0d 0a 23 65 6e 64  <stdint.h>..#end
29d0: 69 66 0d 0a 23 69 66 64 65 66 20 48 41 56 45 5f  if..#ifdef HAVE_
29e0: 49 4e 54 54 59 50 45 53 5f 48 0d 0a 23 69 6e 63  INTTYPES_H..#inc
29f0: 6c 75 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68  lude <inttypes.h
2a00: 3e 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  >..#endif..../*.
2a10: 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2a20: 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65  g macros are use
2a30: 64 20 74 6f 20 63 61 73 74 20 70 6f 69 6e 74 65  d to cast pointe
2a40: 72 73 20 74 6f 20 69 6e 74 65 67 65 72 73 20 61  rs to integers a
2a50: 6e 64 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 73 20  nd..** integers 
2a60: 74 6f 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68  to pointers.  Th
2a70: 65 20 77 61 79 20 79 6f 75 20 64 6f 20 74 68 69  e way you do thi
2a80: 73 20 76 61 72 69 65 73 20 66 72 6f 6d 20 6f 6e  s varies from on
2a90: 65 20 63 6f 6d 70 69 6c 65 72 0d 0a 2a 2a 20 74  e compiler..** t
2aa0: 6f 20 74 68 65 20 6e 65 78 74 2c 20 73 6f 20 77  o the next, so w
2ab0: 65 20 68 61 76 65 20 64 65 76 65 6c 6f 70 65 64  e have developed
2ac0: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2ad0: 65 74 20 6f 66 20 23 69 66 20 73 74 61 74 65 6d  et of #if statem
2ae0: 65 6e 74 73 0d 0a 2a 2a 20 74 6f 20 67 65 6e 65  ents..** to gene
2af0: 72 61 74 65 20 61 70 70 72 6f 70 72 69 61 74 65  rate appropriate
2b00: 20 6d 61 63 72 6f 73 20 66 6f 72 20 61 20 77 69   macros for a wi
2b10: 64 65 20 72 61 6e 67 65 20 6f 66 20 63 6f 6d 70  de range of comp
2b20: 69 6c 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ilers...**..** T
2b30: 68 65 20 63 6f 72 72 65 63 74 20 22 41 4e 53 49  he correct "ANSI
2b40: 22 20 77 61 79 20 74 6f 20 64 6f 20 74 68 69 73  " way to do this
2b50: 20 69 73 20 74 6f 20 75 73 65 20 74 68 65 20 69   is to use the i
2b60: 6e 74 70 74 72 5f 74 20 74 79 70 65 2e 20 0d 0a  ntptr_t type. ..
2b70: 2a 2a 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79  ** Unfortunately
2b80: 2c 20 74 68 61 74 20 74 79 70 65 64 65 66 20 69  , that typedef i
2b90: 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  s not available 
2ba0: 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73  on all compilers
2bb0: 2c 20 6f 72 0d 0a 2a 2a 20 69 66 20 69 74 20 69  , or..** if it i
2bc0: 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20  s available, it 
2bd0: 72 65 71 75 69 72 65 73 20 61 6e 20 23 69 6e 63  requires an #inc
2be0: 6c 75 64 65 20 6f 66 20 73 70 65 63 69 66 69 63  lude of specific
2bf0: 20 68 65 61 64 65 72 73 0d 0a 2a 2a 20 74 68 61   headers..** tha
2c00: 74 20 76 61 72 79 20 66 72 6f 6d 20 6f 6e 65 20  t vary from one 
2c10: 6d 61 63 68 69 6e 65 20 74 6f 20 74 68 65 20 6e  machine to the n
2c20: 65 78 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 69 63  ext...**..** Tic
2c30: 6b 65 74 20 23 33 38 36 30 3a 20 20 54 68 65 20  ket #3860:  The 
2c40: 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20 63 6f 6d  llvm-gcc-4.2 com
2c50: 70 69 6c 65 72 20 66 72 6f 6d 20 41 70 70 6c 65  piler from Apple
2c60: 20 63 68 6f 6b 65 73 20 6f 6e 0d 0a 2a 2a 20 74   chokes on..** t
2c70: 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28 63 68  he ((void*)&((ch
2c80: 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e 73 74  ar*)0)[X]) const
2c90: 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56 43 20  ruct.  But MSVC 
2ca0: 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f 69 64  chokes on ((void
2cb0: 2a 29 28 58 29 29 2e 0d 0a 2a 2a 20 53 6f 20 77  *)(X))...** So w
2cc0: 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e 65  e have to define
2cd0: 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20 64   the macros in d
2ce0: 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 64 65  ifferent ways de
2cf0: 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0d 0a  pending on the..
2d00: 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0d 0a 2a 2f  ** compiler...*/
2d10: 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
2d20: 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f 29 20  PTRDIFF_TYPE__) 
2d30: 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 73 68   /* This case sh
2d40: 6f 75 6c 64 20 77 6f 72 6b 20 66 6f 72 20 47 43  ould work for GC
2d50: 43 20 2a 2f 0d 0a 23 20 64 65 66 69 6e 65 20 53  C */..# define S
2d60: 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2d70: 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 5f 5f  (X)  ((void*)(__
2d80: 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f 29 28  PTRDIFF_TYPE__)(
2d90: 58 29 29 0d 0a 23 20 64 65 66 69 6e 65 20 53 51  X))..# define SQ
2da0: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
2db0: 58 29 20 20 28 28 69 6e 74 29 28 5f 5f 50 54 52  X)  ((int)(__PTR
2dc0: 44 49 46 46 5f 54 59 50 45 5f 5f 29 28 58 29 29  DIFF_TYPE__)(X))
2dd0: 0d 0a 23 65 6c 69 66 20 21 64 65 66 69 6e 65 64  ..#elif !defined
2de0: 28 5f 5f 47 4e 55 43 5f 5f 29 20 20 20 20 20 20  (__GNUC__)      
2df0: 20 2f 2a 20 57 6f 72 6b 73 20 66 6f 72 20 63 6f   /* Works for co
2e00: 6d 70 69 6c 65 72 73 20 6f 74 68 65 72 20 74 68  mpilers other th
2e10: 61 6e 20 4c 4c 56 4d 20 2a 2f 0d 0a 23 20 64 65  an LLVM */..# de
2e20: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f  fine SQLITE_INT_
2e30: 54 4f 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69  TO_PTR(X)  ((voi
2e40: 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58  d*)&((char*)0)[X
2e50: 5d 29 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ])..# define SQL
2e60: 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58  ITE_PTR_TO_INT(X
2e70: 29 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 72  )  ((int)(((char
2e80: 2a 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 0d  *)X)-(char*)0)).
2e90: 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48  .#elif defined(H
2ea0: 41 56 45 5f 53 54 44 49 4e 54 5f 48 29 20 20 20  AVE_STDINT_H)   
2eb0: 2f 2a 20 55 73 65 20 74 68 69 73 20 63 61 73 65  /* Use this case
2ec0: 20 69 66 20 77 65 20 68 61 76 65 20 41 4e 53 49   if we have ANSI
2ed0: 20 68 65 61 64 65 72 73 20 2a 2f 0d 0a 23 20 64   headers */..# d
2ee0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
2ef0: 5f 54 4f 5f 50 54 52 28 58 29 20 20 28 28 76 6f  _TO_PTR(X)  ((vo
2f00: 69 64 2a 29 28 69 6e 74 70 74 72 5f 74 29 28 58  id*)(intptr_t)(X
2f10: 29 29 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ))..# define SQL
2f20: 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58  ITE_PTR_TO_INT(X
2f30: 29 20 20 28 28 69 6e 74 29 28 69 6e 74 70 74 72  )  ((int)(intptr
2f40: 5f 74 29 28 58 29 29 0d 0a 23 65 6c 73 65 20 20  _t)(X))..#else  
2f50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f60: 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
2f70: 61 74 65 73 20 61 20 77 61 72 6e 69 6e 67 20 2d  ates a warning -
2f80: 20 62 75 74 20 69 74 20 61 6c 77 61 79 73 20 77   but it always w
2f90: 6f 72 6b 73 20 2a 2f 0d 0a 23 20 64 65 66 69 6e  orks */..# defin
2fa0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  e SQLITE_INT_TO_
2fb0: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29  PTR(X)  ((void*)
2fc0: 28 58 29 29 0d 0a 23 20 64 65 66 69 6e 65 20 53  (X))..# define S
2fd0: 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
2fe0: 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 29 0d  (X)  ((int)(X)).
2ff0: 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
3000: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48 52  * The SQLITE_THR
3010: 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d 75  EADSAFE macro mu
3020: 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 73  st be defined as
3030: 20 30 2c 20 31 2c 20 6f 72 20 32 2e 0d 0a 2a 2a   0, 1, or 2...**
3040: 20 30 20 6d 65 61 6e 73 20 6d 75 74 65 78 65 73   0 means mutexes
3050: 20 61 72 65 20 70 65 72 6d 61 6e 65 6e 74 6c 79   are permanently
3060: 20 64 69 73 61 62 6c 65 20 61 6e 64 20 74 68 65   disable and the
3070: 20 6c 69 62 72 61 72 79 20 69 73 20 6e 65 76 65   library is neve
3080: 72 0d 0a 2a 2a 20 74 68 72 65 61 64 73 61 66 65  r..** threadsafe
3090: 2e 20 20 31 20 6d 65 61 6e 73 20 74 68 65 20 6c  .  1 means the l
30a0: 69 62 72 61 72 79 20 69 73 20 73 65 72 69 61 6c  ibrary is serial
30b0: 69 7a 65 64 20 77 68 69 63 68 20 69 73 20 74 68  ized which is th
30c0: 65 20 68 69 67 68 65 73 74 0d 0a 2a 2a 20 6c 65  e highest..** le
30d0: 76 65 6c 20 6f 66 20 74 68 72 65 61 64 73 61 66  vel of threadsaf
30e0: 65 74 79 2e 20 20 32 20 6d 65 61 6e 73 20 74 68  ety.  2 means th
30f0: 65 20 6c 69 62 61 72 79 20 69 73 20 6d 75 6c 74  e libary is mult
3100: 69 74 68 72 65 61 64 65 64 20 2d 20 6d 75 6c 74  ithreaded - mult
3110: 69 70 6c 65 0d 0a 2a 2a 20 74 68 72 65 61 64 73  iple..** threads
3120: 20 63 61 6e 20 75 73 65 20 53 51 4c 69 74 65 20   can use SQLite 
3130: 61 73 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77  as long as no tw
3140: 6f 20 74 68 72 65 61 64 73 20 74 72 79 20 74 6f  o threads try to
3150: 20 75 73 65 20 74 68 65 20 73 61 6d 65 0d 0a 2a   use the same..*
3160: 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
3170: 63 74 69 6f 6e 20 61 74 20 74 68 65 20 73 61 6d  ction at the sam
3180: 65 20 74 69 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  e time...**..** 
3190: 4f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  Older versions o
31a0: 66 20 53 51 4c 69 74 65 20 75 73 65 64 20 61 6e  f SQLite used an
31b0: 20 6f 70 74 69 6f 6e 61 6c 20 54 48 52 45 41 44   optional THREAD
31c0: 53 41 46 45 20 6d 61 63 72 6f 2e 0d 0a 2a 2a 20  SAFE macro...** 
31d0: 57 65 20 73 75 70 70 6f 72 74 20 74 68 61 74 20  We support that 
31e0: 66 6f 72 20 6c 65 67 61 63 79 2e 0d 0a 2a 2f 0d  for legacy...*/.
31f0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
3200: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29  LITE_THREADSAFE)
3210: 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  ..#if defined(TH
3220: 52 45 41 44 53 41 46 45 29 0d 0a 23 20 64 65 66  READSAFE)..# def
3230: 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41  ine SQLITE_THREA
3240: 44 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45  DSAFE THREADSAFE
3250: 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
3260: 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  e SQLITE_THREADS
3270: 41 46 45 20 31 20 2f 2a 20 49 4d 50 3a 20 52 2d  AFE 1 /* IMP: R-
3280: 30 37 32 37 32 2d 32 32 33 30 39 20 2a 2f 0d 0a  07272-22309 */..
3290: 23 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66 0d 0a  #endif..#endif..
32a0: 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f 77 65 72 73 61  ../*..** Powersa
32b0: 66 65 20 6f 76 65 72 77 72 69 74 65 20 69 73 20  fe overwrite is 
32c0: 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20  on by default.  
32d0: 42 75 74 20 63 61 6e 20 62 65 20 74 75 72 6e 65  But can be turne
32e0: 64 20 6f 66 66 20 75 73 69 6e 67 0d 0a 2a 2a 20  d off using..** 
32f0: 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 50 4f 57  the -DSQLITE_POW
3300: 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45  ERSAFE_OVERWRITE
3310: 3d 30 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20  =0 command-line 
3320: 6f 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66  option...*/..#if
3330: 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 4f 57 45  ndef SQLITE_POWE
3340: 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 0d  RSAFE_OVERWRITE.
3350: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
3360: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
3370: 52 49 54 45 20 31 0d 0a 23 65 6e 64 69 66 0d 0a  RITE 1..#endif..
3380: 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 53 51 4c  ../*..** The SQL
3390: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
33a0: 54 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74  TATUS macro must
33b0: 20 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65   be defined as e
33c0: 69 74 68 65 72 20 30 20 6f 72 20 31 2e 0d 0a 2a  ither 0 or 1...*
33d0: 2a 20 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20  * It determines 
33e0: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
33f0: 68 65 20 66 65 61 74 75 72 65 73 20 72 65 6c 61  he features rela
3400: 74 65 64 20 74 6f 20 0d 0a 2a 2a 20 53 51 4c 49  ted to ..** SQLI
3410: 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
3420: 54 55 53 20 61 72 65 20 61 76 61 69 6c 61 62 6c  TUS are availabl
3430: 65 20 62 79 20 64 65 66 61 75 6c 74 20 6f 72 20  e by default or 
3440: 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20  not. This value 
3450: 63 61 6e 0d 0a 2a 2a 20 62 65 20 6f 76 65 72 72  can..** be overr
3460: 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d 65  idden at runtime
3470: 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
3480: 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 2e  e3_config() API.
3490: 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65 66 69 6e  ..*/..#if !defin
34a0: 65 64 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ed(SQLITE_DEFAUL
34b0: 54 5f 4d 45 4d 53 54 41 54 55 53 29 0d 0a 23 20  T_MEMSTATUS)..# 
34c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
34d0: 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20  FAULT_MEMSTATUS 
34e0: 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  1..#endif..../*.
34f0: 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20  .** Exactly one 
3500: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
3510: 20 6d 61 63 72 6f 73 20 6d 75 73 74 20 62 65 20   macros must be 
3520: 64 65 66 69 6e 65 64 20 69 6e 20 6f 72 64 65 72  defined in order
3530: 20 74 6f 0d 0a 2a 2a 20 73 70 65 63 69 66 79 20   to..** specify 
3540: 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c  which memory all
3550: 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
3560: 6d 20 74 6f 20 75 73 65 2e 0d 0a 2a 2a 0d 0a 2a  m to use...**..*
3570: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 53  *     SQLITE_SYS
3580: 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20  TEM_MALLOC      
3590: 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d 61      // Use norma
35a0: 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28  l system malloc(
35b0: 29 0d 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  )..**     SQLITE
35c0: 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 20 20 20  _WIN32_MALLOC   
35d0: 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65 20 57          // Use W
35e0: 69 6e 33 32 20 6e 61 74 69 76 65 20 68 65 61 70  in32 native heap
35f0: 20 41 50 49 0d 0a 2a 2a 20 20 20 20 20 53 51 4c   API..**     SQL
3600: 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20  ITE_MEMDEBUG    
3610: 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65             // De
3620: 62 75 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20  bugging version 
3630: 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  of system malloc
3640: 28 29 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 20 57 69  ()..**..** On Wi
3650: 6e 64 6f 77 73 2c 20 69 66 20 74 68 65 20 53 51  ndows, if the SQ
3660: 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f  LITE_WIN32_MALLO
3670: 43 5f 56 41 4c 49 44 41 54 45 20 6d 61 63 72 6f  C_VALIDATE macro
3680: 20 69 73 20 64 65 66 69 6e 65 64 20 61 6e 64 20   is defined and 
3690: 74 68 65 0d 0a 2a 2a 20 61 73 73 65 72 74 28 29  the..** assert()
36a0: 20 6d 61 63 72 6f 20 69 73 20 65 6e 61 62 6c 65   macro is enable
36b0: 64 2c 20 65 61 63 68 20 63 61 6c 6c 20 69 6e 74  d, each call int
36c0: 6f 20 74 68 65 20 57 69 6e 33 32 20 6e 61 74 69  o the Win32 nati
36d0: 76 65 20 68 65 61 70 20 73 75 62 73 79 73 74 65  ve heap subsyste
36e0: 6d 0d 0a 2a 2a 20 77 69 6c 6c 20 63 61 75 73 65  m..** will cause
36f0: 20 48 65 61 70 56 61 6c 69 64 61 74 65 20 74 6f   HeapValidate to
3700: 20 62 65 20 63 61 6c 6c 65 64 2e 20 20 49 66 20   be called.  If 
3710: 68 65 61 70 20 76 61 6c 69 64 61 74 69 6f 6e 20  heap validation 
3720: 73 68 6f 75 6c 64 20 66 61 69 6c 2c 20 61 6e 0d  should fail, an.
3730: 0a 2a 2a 20 61 73 73 65 72 74 69 6f 6e 20 77 69  .** assertion wi
3740: 6c 6c 20 62 65 20 74 72 69 67 67 65 72 65 64 2e  ll be triggered.
3750: 0d 0a 2a 2a 0d 0a 2a 2a 20 28 48 69 73 74 6f 72  ..**..** (Histor
3760: 69 63 61 6c 20 6e 6f 74 65 3a 20 20 54 68 65 72  ical note:  Ther
3770: 65 20 75 73 65 64 20 74 6f 20 62 65 20 73 65 76  e used to be sev
3780: 65 72 61 6c 20 6f 74 68 65 72 20 6f 70 74 69 6f  eral other optio
3790: 6e 73 2c 20 62 75 74 20 77 65 27 76 65 0d 0a 2a  ns, but we've..*
37a0: 2a 20 70 61 72 65 64 20 69 74 20 64 6f 77 6e 20  * pared it down 
37b0: 74 6f 20 6a 75 73 74 20 74 68 65 73 65 20 74 68  to just these th
37c0: 72 65 65 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  ree.)..**..** If
37d0: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f   none of the abo
37e0: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  ve are defined, 
37f0: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f  then set SQLITE_
3800: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73  SYSTEM_MALLOC as
3810: 0d 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  ..** the default
3820: 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66 69 6e  ...*/..#if defin
3830: 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ed(SQLITE_SYSTEM
3840: 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64  _MALLOC)+defined
3850: 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41  (SQLITE_WIN32_MA
3860: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3870: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 3e 31  LITE_MEMDEBUG)>1
3880: 0d 0a 23 20 65 72 72 6f 72 20 22 41 74 20 6d 6f  ..# error "At mo
3890: 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  st one of the fo
38a0: 6c 6c 6f 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d  llowing compile-
38b0: 74 69 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69  time configurati
38c0: 6f 6e 20 6f 70 74 69 6f 6e 73 5c 0d 0a 20 69 73  on options\.. is
38d0: 20 61 6c 6c 6f 77 73 3a 20 53 51 4c 49 54 45 5f   allows: SQLITE_
38e0: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53  SYSTEM_MALLOC, S
38f0: 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c  QLITE_WIN32_MALL
3900: 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  OC, SQLITE_MEMDE
3910: 42 55 47 22 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  BUG"..#endif..#i
3920: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
3930: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b  _SYSTEM_MALLOC)+
3940: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
3950: 49 4e 33 32 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66  IN32_MALLOC)+def
3960: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44  ined(SQLITE_MEMD
3970: 45 42 55 47 29 3d 3d 30 0d 0a 23 20 64 65 66 69  EBUG)==0..# defi
3980: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ne SQLITE_SYSTEM
3990: 5f 4d 41 4c 4c 4f 43 20 31 0d 0a 23 65 6e 64 69  _MALLOC 1..#endi
39a0: 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 53  f..../*..** If S
39b0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
39c0: 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f 74 20 7a  T_LIMIT is not z
39d0: 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  ero, then try to
39e0: 20 6b 65 65 70 20 74 68 65 0d 0a 2a 2a 20 73 69   keep the..** si
39f0: 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c  zes of memory al
3a00: 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20  locations below 
3a10: 74 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 65  this value where
3a20: 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 2a 2f 0d 0a   possible...*/..
3a30: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3a40: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
3a50: 4c 49 4d 49 54 29 0d 0a 23 20 64 65 66 69 6e 65  LIMIT)..# define
3a60: 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53   SQLITE_MALLOC_S
3a70: 4f 46 54 5f 4c 49 4d 49 54 20 31 30 32 34 0d 0a  OFT_LIMIT 1024..
3a80: 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
3a90: 20 57 65 20 6e 65 65 64 20 74 6f 20 64 65 66 69   We need to defi
3aa0: 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  ne _XOPEN_SOURCE
3ab0: 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69 6e 20 6f   as follows in o
3ac0: 72 64 65 72 20 74 6f 20 65 6e 61 62 6c 65 0d 0a  rder to enable..
3ad0: 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ** recursive mut
3ae0: 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55 6e 69  exes on most Uni
3af0: 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75 74 20  x systems.  But 
3b00: 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69 66 66  Mac OS X is diff
3b10: 65 72 65 6e 74 2e 0d 0a 2a 2a 20 54 68 65 20 5f  erent...** The _
3b20: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65 66  XOPEN_SOURCE def
3b30: 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62 6c  ine causes probl
3b40: 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20 58  ems for Mac OS X
3b50: 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0d 0a 2a   we are told,..*
3b60: 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74  * so it is omitt
3b70: 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 74  ed there.  See t
3b80: 69 63 6b 65 74 20 23 32 36 37 33 2e 0d 0a 2a 2a  icket #2673...**
3b90: 0d 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65  ..** Later we le
3ba0: 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f  arn that _XOPEN_
3bb0: 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79  SOURCE is poorly
3bc0: 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0d   or incorrectly.
3bd0: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  .** implemented 
3be0: 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e  on some systems.
3bf0: 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65    So we avoid de
3c00: 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c  fining it at all
3c10: 0d 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c  ..** if it is al
3c20: 72 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72  ready defined or
3c30: 20 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64   if it is unneed
3c40: 65 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72  ed because we ar
3c50: 65 0d 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20  e..** not doing 
3c60: 61 20 74 68 72 65 61 64 73 61 66 65 20 62 75 69  a threadsafe bui
3c70: 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38  ld.  Ticket #268
3c80: 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61  1...**..** See a
3c90: 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 34 31  lso ticket #2741
3ca0: 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65 66 69  ...*/..#if !defi
3cb0: 6e 65 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ned(_XOPEN_SOURC
3cc0: 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  E) && !defined(_
3cd0: 5f 44 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64  _DARWIN__) && !d
3ce0: 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
3cf0: 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
3d00: 41 44 53 41 46 45 0d 0a 23 20 20 64 65 66 69 6e  ADSAFE..#  defin
3d10: 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20  e _XOPEN_SOURCE 
3d20: 35 30 30 20 20 2f 2a 20 4e 65 65 64 65 64 20 74  500  /* Needed t
3d30: 6f 20 65 6e 61 62 6c 65 20 70 74 68 72 65 61 64  o enable pthread
3d40: 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
3d50: 65 73 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d  es */..#endif...
3d60: 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 54 43 4c 20  ./*..** The TCL 
3d70: 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e 6c 79  headers are only
3d80: 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63 6f 6d   needed when com
3d90: 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c 20 62  piling the TCL b
3da0: 69 6e 64 69 6e 67 73 2e 0d 0a 2a 2f 0d 0a 23 69  indings...*/..#i
3db0: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
3dc0: 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  _TCL) || defined
3dd0: 28 54 43 4c 53 48 29 0d 0a 23 20 69 6e 63 6c 75  (TCLSH)..# inclu
3de0: 64 65 20 3c 74 63 6c 2e 68 3e 0d 0a 23 65 6e 64  de <tcl.h>..#end
3df0: 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6e  if..../*..** Man
3e00: 79 20 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69  y people are fai
3e10: 6c 69 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44  ling to set -DND
3e20: 45 42 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70  EBUG=1 when comp
3e30: 69 6c 69 6e 67 20 53 51 4c 69 74 65 2e 0d 0a 2a  iling SQLite...*
3e40: 2a 20 53 65 74 74 69 6e 67 20 4e 44 45 42 55 47  * Setting NDEBUG
3e50: 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20   makes the code 
3e60: 73 6d 61 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20  smaller and run 
3e70: 66 61 73 74 65 72 2e 20 20 53 6f 20 74 68 65 20  faster.  So the 
3e80: 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 6c 69  following..** li
3e90: 6e 65 73 20 61 72 65 20 61 64 64 65 64 20 74 6f  nes are added to
3ea0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73   automatically s
3eb0: 65 74 20 4e 44 45 42 55 47 20 75 6e 6c 65 73 73  et NDEBUG unless
3ec0: 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 45   the -DSQLITE_DE
3ed0: 42 55 47 3d 31 0d 0a 2a 2a 20 6f 70 74 69 6f 6e  BUG=1..** option
3ee0: 20 69 73 20 73 65 74 2e 20 20 54 68 75 73 20 4e   is set.  Thus N
3ef0: 44 45 42 55 47 20 62 65 63 6f 6d 65 73 20 61 6e  DEBUG becomes an
3f00: 20 6f 70 74 2d 69 6e 20 72 61 74 68 65 72 20 74   opt-in rather t
3f10: 68 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74 0d 0a  han an opt-out..
3f20: 2a 2a 20 66 65 61 74 75 72 65 2e 0d 0a 2a 2f 0d  ** feature...*/.
3f30: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
3f40: 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65  EBUG) && !define
3f50: 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
3f60: 0d 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55  ..# define NDEBU
3f70: 47 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  G 1..#endif..../
3f80: 2a 0d 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61  *..** The testca
3f90: 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73  se() macro is us
3fa0: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76  ed to aid in cov
3fb0: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20  erage testing.  
3fc0: 57 68 65 6e 20 0d 0a 2a 2a 20 64 6f 69 6e 67 20  When ..** doing 
3fd0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
3fe0: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
3ff0: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d  inside the argum
4000: 65 6e 74 20 74 6f 0d 0a 2a 2a 20 74 65 73 74 63  ent to..** testc
4010: 61 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76  ase() must be ev
4020: 61 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75  aluated both tru
4030: 65 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f  e and false in o
4040: 72 64 65 72 20 74 6f 0d 0a 2a 2a 20 67 65 74 20  rder to..** get 
4050: 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65  full branch cove
4060: 72 61 67 65 2e 20 20 54 68 65 20 74 65 73 74 63  rage.  The testc
4070: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69  ase() macro is i
4080: 6e 73 65 72 74 65 64 0d 0a 2a 2a 20 74 6f 20 68  nserted..** to h
4090: 65 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71 75  elp ensure adequ
40a0: 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 67  ate test coverag
40b0: 65 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72  e in places wher
40c0: 65 20 73 69 6d 70 6c 65 0d 0a 2a 2a 20 63 6f 6e  e simple..** con
40d0: 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20  dition/decision 
40e0: 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 64  coverage is inad
40f0: 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61  equate.  For exa
4100: 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29  mple, testcase()
4110: 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  ..** can be used
4120: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 6f   to make sure bo
4130: 75 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 72  undary values ar
4140: 65 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0d 0a  e tested.  For..
4150: 2a 2a 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73  ** bitmask tests
4160: 2c 20 74 65 73 74 63 61 73 65 28 29 20 63 61 6e  , testcase() can
4170: 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   be used to make
4180: 20 73 75 72 65 20 65 61 63 68 20 62 69 74 0d 0a   sure each bit..
4190: 2a 2a 20 69 73 20 73 69 67 6e 69 66 69 63 61 6e  ** is significan
41a0: 74 20 61 6e 64 20 75 73 65 64 20 61 74 20 6c 65  t and used at le
41b0: 61 73 74 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77  ast once.  On sw
41c0: 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 73 0d  itch statements.
41d0: 0a 2a 2a 20 77 68 65 72 65 20 6d 75 6c 74 69 70  .** where multip
41e0: 6c 65 20 63 61 73 65 73 20 67 6f 20 74 6f 20 74  le cases go to t
41f0: 68 65 20 73 61 6d 65 20 62 6c 6f 63 6b 20 6f 66  he same block of
4200: 20 63 6f 64 65 2c 20 74 65 73 74 63 61 73 65 28   code, testcase(
4210: 29 0d 0a 2a 2a 20 63 61 6e 20 69 6e 73 75 72 65  )..** can insure
4220: 20 74 68 61 74 20 61 6c 6c 20 63 61 73 65 73 20   that all cases 
4230: 61 72 65 20 65 76 61 6c 75 61 74 65 64 2e 0d 0a  are evaluated...
4240: 2a 2a 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53  **..*/..#ifdef S
4250: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
4260: 45 53 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST..SQLITE_PRIV
4270: 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4280: 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 29 3b  e3Coverage(int);
4290: 0d 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 63  ..# define testc
42a0: 61 73 65 28 58 29 20 20 69 66 28 20 58 20 29 7b  ase(X)  if( X ){
42b0: 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65   sqlite3Coverage
42c0: 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0d 0a 23  (__LINE__); }..#
42d0: 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 74  else..# define t
42e0: 65 73 74 63 61 73 65 28 58 29 0d 0a 23 65 6e 64  estcase(X)..#end
42f0: 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  if..../*..** The
4300: 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20   TESTONLY macro 
4310: 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f  is used to enclo
4320: 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c  se variable decl
4330: 61 72 61 74 69 6f 6e 73 20 6f 72 0d 0a 2a 2a 20  arations or..** 
4340: 6f 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 6f  other bits of co
4350: 64 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 64  de that are need
4360: 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  ed to support th
4370: 65 20 61 72 67 75 6d 65 6e 74 73 0d 0a 2a 2a 20  e arguments..** 
4380: 77 69 74 68 69 6e 20 74 65 73 74 63 61 73 65 28  within testcase(
4390: 29 20 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d  ) and assert() m
43a0: 61 63 72 6f 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20  acros...*/..#if 
43b0: 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
43c0: 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
43d0: 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54  TE_COVERAGE_TEST
43e0: 29 0d 0a 23 20 64 65 66 69 6e 65 20 54 45 53 54  )..# define TEST
43f0: 4f 4e 4c 59 28 58 29 20 20 58 0d 0a 23 65 6c 73  ONLY(X)  X..#els
4400: 65 0d 0a 23 20 64 65 66 69 6e 65 20 54 45 53 54  e..# define TEST
4410: 4f 4e 4c 59 28 58 29 0d 0a 23 65 6e 64 69 66 0d  ONLY(X)..#endif.
4420: 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 6f 6d 65 74 69  .../*..** Someti
4430: 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d  mes we need a sm
4440: 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f  all amount of co
4450: 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72  de such as a var
4460: 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61  iable initializa
4470: 74 69 6f 6e 0d 0a 2a 2a 20 74 6f 20 73 65 74 75  tion..** to setu
4480: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73  p for a later as
4490: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
44a0: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e  .  We do not wan
44b0: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0d 0a  t this code to..
44c0: 2a 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61  ** appear when a
44d0: 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62  ssert() is disab
44e0: 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  led.  The follow
44f0: 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65  ing macro is the
4500: 72 65 66 6f 72 65 0d 0a 2a 2a 20 75 73 65 64 20  refore..** used 
4510: 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20  to contain that 
4520: 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65  setup code.  The
4530: 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73   "VVA" acronym s
4540: 74 61 6e 64 73 20 66 6f 72 0d 0a 2a 2a 20 22 56  tands for..** "V
4550: 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c  erification, Val
4560: 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63  idation, and Acc
4570: 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e  reditation".  In
4580: 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
4590: 65 0d 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69  e..** 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 0d 0a 2a 2f 0d 0a 23  rocesses...*/..#
45e0: 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 23  ifndef NDEBUG..#
45f0: 20 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59   define VVA_ONLY
4600: 28 58 29 20 20 58 0d 0a 23 65 6c 73 65 0d 0a 23  (X)  X..#else..#
4610: 20 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59   define VVA_ONLY
4620: 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  (X)..#endif..../
4630: 2a 0d 0a 2a 2a 20 54 68 65 20 41 4c 57 41 59 53  *..** The ALWAYS
4640: 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 72 6f   and NEVER macro
4650: 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65  s surround boole
4660: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77  an expressions w
4670: 68 69 63 68 20 0d 0a 2a 2a 20 61 72 65 20 69 6e  hich ..** are in
4680: 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 73  tended to always
4690: 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c 73   be true or fals
46a0: 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  e, respectively.
46b0: 20 20 53 75 63 68 0d 0a 2a 2a 20 65 78 70 72 65    Such..** expre
46c0: 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20  ssions could be 
46d0: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  omitted from the
46e0: 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79   code completely
46f0: 2e 20 20 42 75 74 20 74 68 65 79 0d 0a 2a 2a 20  .  But they..** 
4700: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  are included in 
4710: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f  a few cases in o
4720: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20  rder to enhance 
4730: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0d 0a  the resilience..
4740: 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20  ** of SQLite to 
4750: 75 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76  unexpected behav
4760: 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68  ior - to make th
4770: 65 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61  e code "self-hea
4780: 6c 69 6e 67 22 0d 0a 2a 2a 20 6f 72 20 22 64 75  ling"..** or "du
4790: 63 74 69 6c 65 22 20 72 61 74 68 65 72 20 74 68  ctile" rather th
47a0: 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c  an being "brittl
47b0: 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20  e" and crashing 
47c0: 61 74 20 74 68 65 20 66 69 72 73 74 0d 0a 2a 2a  at the first..**
47d0: 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e   hint of unplann
47e0: 65 64 20 62 65 68 61 76 69 6f 72 2e 0d 0a 2a 2a  ed behavior...**
47f0: 0d 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f  ..** In other wo
4800: 72 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 20  rds, ALWAYS and 
4810: 4e 45 56 45 52 20 61 72 65 20 61 64 64 65 64 20  NEVER are added 
4820: 66 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 6f  for defensive co
4830: 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e  de...**..** When
4840: 20 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20   doing coverage 
4850: 74 65 73 74 69 6e 67 20 41 4c 57 41 59 53 20 61  testing ALWAYS a
4860: 6e 64 20 4e 45 56 45 52 20 61 72 65 20 68 61 72  nd NEVER are har
4870: 64 2d 63 6f 64 65 64 20 74 6f 0d 0a 2a 2a 20 62  d-coded to..** b
4880: 65 20 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65  e true and false
4890: 20 73 6f 20 74 68 61 74 20 74 68 65 20 75 6e 72   so that the unr
48a0: 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 74 68  eachable code th
48b0: 65 6e 20 73 70 65 63 69 66 79 20 77 69 6c 6c 0d  en specify will.
48c0: 0a 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74  .** not be count
48d0: 65 64 20 61 73 20 75 6e 74 65 73 74 65 64 20 63  ed as untested c
48e0: 6f 64 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65  ode...*/..#if de
48f0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56  fined(SQLITE_COV
4900: 45 52 41 47 45 5f 54 45 53 54 29 0d 0a 23 20 64  ERAGE_TEST)..# d
4910: 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20  efine ALWAYS(X) 
4920: 20 20 20 20 20 28 31 29 0d 0a 23 20 64 65 66 69       (1)..# defi
4930: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20  ne NEVER(X)     
4940: 20 20 28 30 29 0d 0a 23 65 6c 69 66 20 21 64 65    (0)..#elif !de
4950: 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0d 0a 23  fined(NDEBUG)..#
4960: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58   define ALWAYS(X
4970: 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61  )      ((X)?1:(a
4980: 73 73 65 72 74 28 30 29 2c 30 29 29 0d 0a 23 20  ssert(0),0))..# 
4990: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
49a0: 20 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65        ((X)?(asse
49b0: 72 74 28 30 29 2c 31 29 3a 30 29 0d 0a 23 65 6c  rt(0),1):0)..#el
49c0: 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 41 4c 57  se..# define ALW
49d0: 41 59 53 28 58 29 20 20 20 20 20 20 28 58 29 0d  AYS(X)      (X).
49e0: 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28  .# define NEVER(
49f0: 58 29 20 20 20 20 20 20 20 28 58 29 0d 0a 23 65  X)       (X)..#e
4a00: 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ndif..../*..** R
4a10: 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d  eturn true (non-
4a20: 7a 65 72 6f 29 20 69 66 20 74 68 65 20 69 6e 70  zero) if the inp
4a30: 75 74 20 69 73 20 61 20 69 6e 74 65 67 65 72 20  ut is a integer 
4a40: 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67  that is too larg
4a50: 65 0d 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20  e..** to fit in 
4a60: 33 32 2d 62 69 74 73 2e 20 20 54 68 69 73 20 6d  32-bits.  This m
4a70: 61 63 72 6f 20 69 73 20 75 73 65 64 20 69 6e 73  acro is used ins
4a80: 69 64 65 20 6f 66 20 76 61 72 69 6f 75 73 20 74  ide of various t
4a90: 65 73 74 63 61 73 65 28 29 0d 0a 2a 2a 20 6d 61  estcase()..** ma
4aa0: 63 72 6f 73 20 74 6f 20 76 65 72 69 66 79 20 74  cros to verify t
4ab0: 68 61 74 20 77 65 20 68 61 76 65 20 74 65 73 74  hat we have test
4ac0: 65 64 20 53 51 4c 69 74 65 20 66 6f 72 20 6c 61  ed SQLite for la
4ad0: 72 67 65 2d 66 69 6c 65 20 73 75 70 70 6f 72 74  rge-file support
4ae0: 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 49  ...*/..#define I
4af0: 53 5f 42 49 47 5f 49 4e 54 28 58 29 20 20 28 28  S_BIG_INT(X)  ((
4b00: 28 58 29 26 7e 28 69 36 34 29 30 78 66 66 66 66  (X)&~(i64)0xffff
4b10: 66 66 66 66 29 21 3d 30 29 0d 0a 0d 0a 2f 2a 0d  ffff)!=0)..../*.
4b20: 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 6e  .** The macro un
4b30: 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 69  likely() is a hi
4b40: 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e 64  nt that surround
4b50: 73 20 61 20 62 6f 6f 6c 65 61 6e 0d 0a 2a 2a 20  s a boolean..** 
4b60: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
4b70: 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65  is usually false
4b80: 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28  .  Macro likely(
4b90: 29 20 73 75 72 72 6f 75 6e 64 73 0d 0a 2a 2a 20  ) surrounds..** 
4ba0: 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  a boolean expres
4bb0: 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75  sion that is usu
4bc0: 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20  ally true.  GCC 
4bd0: 69 73 20 61 62 6c 65 20 74 6f 0d 0a 2a 2a 20 75  is able to..** u
4be0: 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74  se these hints t
4bf0: 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65  o generate bette
4c00: 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65  r code, sometime
4c10: 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66 69  s...*/..#if defi
4c20: 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
4c30: 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b   0..# define lik
4c40: 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c  ely(X)    __buil
4c50: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31  tin_expect((X),1
4c60: 29 0d 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69  )..# define unli
4c70: 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c 74  kely(X)  __built
4c80: 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 29  in_expect((X),0)
4c90: 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
4ca0: 65 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 21  e likely(X)    !
4cb0: 21 28 58 29 0d 0a 23 20 64 65 66 69 6e 65 20 75  !(X)..# define u
4cc0: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58  nlikely(X)  !!(X
4cd0: 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a  )..#endif..../**
4ce0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
4cf0: 6c 75 64 65 20 73 71 6c 69 74 65 33 2e 68 20 69  lude sqlite3.h i
4d00: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4d10: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
4d30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
4d40: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
4d50: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
4d80: 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  *..** 2001 Septe
4d90: 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20  mber 15..**..** 
4da0: 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4db0: 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
4dc0: 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
4dd0: 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
4de0: 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  ..** a legal not
4df0: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
4e00: 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  lessing:..**..**
4e10: 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
4e20: 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
4e30: 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
4e40: 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
4e50: 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
4e60: 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
4e70: 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  s...**    May yo
4e80: 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
4e90: 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
4ea0: 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
4eb0: 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
4ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ee0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ef0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
4f00: 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
4f10: 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
4f20: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
4f30: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
4f40: 72 79 0d 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20  ry..** presents 
4f50: 74 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61  to client progra
4f60: 6d 73 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63  ms.  If a C-func
4f70: 74 69 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c  tion, structure,
4f80: 20 64 61 74 61 74 79 70 65 2c 0d 0a 2a 2a 20 6f   datatype,..** o
4f90: 72 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e  r constant defin
4fa0: 69 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61  ition does not a
4fb0: 70 70 65 61 72 20 69 6e 20 74 68 69 73 20 66 69  ppear in this fi
4fc0: 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0d 0a  le, then it is..
4fd0: 2a 2a 20 6e 6f 74 20 61 20 70 75 62 6c 69 73 68  ** not a publish
4fe0: 65 64 20 41 50 49 20 6f 66 20 53 51 4c 69 74 65  ed API of SQLite
4ff0: 2c 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20  , is subject to 
5000: 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 0d 0a  change without..
5010: 2a 2a 20 6e 6f 74 69 63 65 2c 20 61 6e 64 20 73  ** notice, and s
5020: 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 66  hould not be ref
5030: 65 72 65 6e 63 65 64 20 62 79 20 70 72 6f 67 72  erenced by progr
5040: 61 6d 73 20 74 68 61 74 20 75 73 65 20 53 51 4c  ams that use SQL
5050: 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d  ite...**..** Som
5060: 65 20 6f 66 20 74 68 65 20 64 65 66 69 6e 69 74  e of the definit
5070: 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 69 6e  ions that are in
5080: 20 74 68 69 73 20 66 69 6c 65 20 61 72 65 20 6d   this file are m
5090: 61 72 6b 65 64 20 61 73 0d 0a 2a 2a 20 22 65 78  arked as..** "ex
50a0: 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 45 78  perimental".  Ex
50b0: 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72  perimental inter
50c0: 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d 61 6c  faces are normal
50d0: 6c 79 20 6e 65 77 0d 0a 2a 2a 20 66 65 61 74 75  ly new..** featu
50e0: 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 64  res recently add
50f0: 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 57  ed to SQLite.  W
5100: 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 70  e do not anticip
5110: 61 74 65 20 63 68 61 6e 67 65 73 0d 0a 2a 2a 20  ate changes..** 
5120: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  to experimental 
5130: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72  interfaces but r
5140: 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 74  eserve the right
5150: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63   to make minor c
5160: 68 61 6e 67 65 73 0d 0a 2a 2a 20 69 66 20 65 78  hanges..** if ex
5170: 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73  perience from us
5180: 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20  e "in the wild" 
5190: 73 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61  suggest such cha
51a0: 6e 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74  nges are prudent
51b0: 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f 66  ...**..** The of
51c0: 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67  ficial C-languag
51d0: 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74  e API documentat
51e0: 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69  ion for SQLite i
51f0: 73 20 64 65 72 69 76 65 64 0d 0a 2a 2a 20 66 72  s derived..** fr
5200: 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74  om comments in t
5210: 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73 20  his file.  This 
5220: 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 74 68  file is the auth
5230: 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 63 65  oritative source
5240: 0d 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69  ..** on how SQLi
5250: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  te interfaces ar
5260: 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65  e suppose to ope
5270: 72 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  rate...**..** Th
5280: 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66  e name of this f
5290: 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67  ile under config
52a0: 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65  uration manageme
52b0: 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e  nt is "sqlite.h.
52c0: 69 6e 22 2e 0d 0a 2a 2a 20 54 68 65 20 6d 61 6b  in"...** The mak
52d0: 65 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65  efile makes some
52e0: 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74   minor changes t
52f0: 6f 20 74 68 69 73 20 66 69 6c 65 20 28 73 75 63  o this file (suc
5300: 68 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0d 0a  h as inserting..
5310: 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e  ** the version n
5320: 75 6d 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67  umber) and chang
5330: 65 73 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22  es its name to "
5340: 73 71 6c 69 74 65 33 2e 68 22 20 61 73 0d 0a 2a  sqlite3.h" as..*
5350: 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 75  * part of the bu
5360: 69 6c 64 20 70 72 6f 63 65 73 73 2e 0d 0a 2a 2f  ild process...*/
5370: 0d 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  ..#ifndef _SQLIT
5380: 45 33 5f 48 5f 0d 0a 23 64 65 66 69 6e 65 20 5f  E3_H_..#define _
5390: 53 51 4c 49 54 45 33 5f 48 5f 0d 0a 23 69 6e 63  SQLITE3_H_..#inc
53a0: 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20  lude <stdarg.h> 
53b0: 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f      /* Needed fo
53c0: 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  r the definition
53d0: 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f 0d 0a   of va_list */..
53e0: 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65 20 73 75  ../*..** Make su
53f0: 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74  re we can call t
5400: 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43  his stuff from C
5410: 2b 2b 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 30 0d 0a  ++...*/..#if 0..
5420: 65 78 74 65 72 6e 20 22 43 22 20 7b 0d 0a 23 65  extern "C" {..#e
5430: 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ndif....../*..**
5440: 20 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 79   Add the ability
5450: 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78   to override 'ex
5460: 74 65 72 6e 27 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  tern'..*/..#ifnd
5470: 65 66 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e  ef SQLITE_EXTERN
5480: 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ..# define SQLIT
5490: 45 5f 45 58 54 45 52 4e 20 65 78 74 65 72 6e 0d  E_EXTERN extern.
54a0: 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64  .#endif....#ifnd
54b0: 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0d 0a 23  ef SQLITE_API..#
54c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
54d0: 50 49 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a  PI..#endif......
54e0: 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d  /*..** These no-
54f0: 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  op macros are us
5500: 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69  ed in front of i
5510: 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d 61 72  nterfaces to mar
5520: 6b 20 74 68 6f 73 65 0d 0a 2a 2a 20 69 6e 74 65  k those..** inte
5530: 72 66 61 63 65 73 20 61 73 20 65 69 74 68 65 72  rfaces as either
5540: 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 20 65   deprecated or e
5550: 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e 65  xperimental.  Ne
5560: 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0d 0a  w applications..
5570: 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73  ** should not us
5580: 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  e deprecated int
5590: 65 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61  erfaces - they a
55a0: 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62  re support for b
55b0: 61 63 6b 77 61 72 64 73 0d 0a 2a 2a 20 63 6f 6d  ackwards..** com
55c0: 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e  patibility only.
55d0: 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72    Application wr
55e0: 69 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20  iters should be 
55f0: 61 77 61 72 65 20 74 68 61 74 0d 0a 2a 2a 20 65  aware that..** e
5600: 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65  xperimental inte
5610: 72 66 61 63 65 73 20 61 72 65 20 73 75 62 6a 65  rfaces are subje
5620: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20  ct to change in 
5630: 70 6f 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0d  point releases..
5640: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 6d 61  .**..** These ma
5650: 63 72 6f 73 20 75 73 65 64 20 74 6f 20 72 65 73  cros used to res
5660: 6f 6c 76 65 20 74 6f 20 76 61 72 69 6f 75 73 20  olve to various 
5670: 6b 69 6e 64 73 20 6f 66 20 63 6f 6d 70 69 6c 65  kinds of compile
5680: 72 20 6d 61 67 69 63 20 74 68 61 74 0d 0a 2a 2a  r magic that..**
5690: 20 77 6f 75 6c 64 20 67 65 6e 65 72 61 74 65 20   would generate 
56a0: 77 61 72 6e 69 6e 67 20 6d 65 73 73 61 67 65 73  warning messages
56b0: 20 77 68 65 6e 20 74 68 65 79 20 77 65 72 65 20   when they were 
56c0: 75 73 65 64 2e 20 20 42 75 74 20 74 68 61 74 0d  used.  But that.
56d0: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67  .** compiler mag
56e0: 69 63 20 65 6e 64 65 64 20 75 70 20 67 65 6e 65  ic ended up gene
56f0: 72 61 74 69 6e 67 20 73 75 63 68 20 61 20 66 6c  rating such a fl
5700: 75 72 72 79 20 6f 66 20 62 75 67 20 72 65 70 6f  urry of bug repo
5710: 72 74 73 0d 0a 2a 2a 20 74 68 61 74 20 77 65 20  rts..** that we 
5720: 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20 61 6c  have taken it al
5730: 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 20 62  l out and gone b
5740: 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 69 6d  ack to using sim
5750: 70 6c 65 0d 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 63  ple..** noop mac
5760: 72 6f 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  ros...*/..#defin
5770: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  e SQLITE_DEPRECA
5780: 54 45 44 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  TED..#define SQL
5790: 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
57a0: 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 6e 73 75 72  ..../*..** Ensur
57b0: 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 20  e these symbols 
57c0: 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 64  were not defined
57d0: 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 6f 75   by some previou
57e0: 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0d 0a  s header file...
57f0: 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
5800: 45 5f 56 45 52 53 49 4f 4e 0d 0a 23 20 75 6e 64  E_VERSION..# und
5810: 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ef SQLITE_VERSIO
5820: 4e 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  N..#endif..#ifde
5830: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  f SQLITE_VERSION
5840: 5f 4e 55 4d 42 45 52 0d 0a 23 20 75 6e 64 65 66  _NUMBER..# undef
5850: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
5860: 4e 55 4d 42 45 52 0d 0a 23 65 6e 64 69 66 0d 0a  NUMBER..#endif..
5870: 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
5880: 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20  F: Compile-Time 
5890: 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20  Library Version 
58a0: 4e 75 6d 62 65 72 73 0d 0a 2a 2a 0d 0a 2a 2a 20  Numbers..**..** 
58b0: 5e 28 54 68 65 20 5b 53 51 4c 49 54 45 5f 56 45  ^(The [SQLITE_VE
58c0: 52 53 49 4f 4e 5d 20 43 20 70 72 65 70 72 6f 63  RSION] C preproc
58d0: 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 6e 20 74  essor macro in t
58e0: 68 65 20 73 71 6c 69 74 65 33 2e 68 20 68 65 61  he sqlite3.h hea
58f0: 64 65 72 0d 0a 2a 2a 20 65 76 61 6c 75 61 74 65  der..** evaluate
5900: 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69  s to a string li
5910: 74 65 72 61 6c 20 74 68 61 74 20 69 73 20 74 68  teral that is th
5920: 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  e SQLite version
5930: 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 66 6f 72 6d   in the..** form
5940: 61 74 20 22 58 2e 59 2e 5a 22 20 77 68 65 72 65  at "X.Y.Z" where
5950: 20 58 20 69 73 20 74 68 65 20 6d 61 6a 6f 72 20   X is the major 
5960: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 28  version number (
5970: 61 6c 77 61 79 73 20 33 20 66 6f 72 0d 0a 2a 2a  always 3 for..**
5980: 20 53 51 4c 69 74 65 33 29 20 61 6e 64 20 59 20   SQLite3) and Y 
5990: 69 73 20 74 68 65 20 6d 69 6e 6f 72 20 76 65 72  is the minor ver
59a0: 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20  sion number and 
59b0: 5a 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65  Z is the release
59c0: 20 6e 75 6d 62 65 72 2e 29 5e 0d 0a 2a 2a 20 5e   number.)^..** ^
59d0: 28 54 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52  (The [SQLITE_VER
59e0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 20 43 20 70  SION_NUMBER] C p
59f0: 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
5a00: 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 6e  o resolves to an
5a10: 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 77 69 74   integer..** wit
5a20: 68 20 74 68 65 20 76 61 6c 75 65 20 28 58 2a 31  h the value (X*1
5a30: 30 30 30 30 30 30 20 2b 20 59 2a 31 30 30 30 20  000000 + Y*1000 
5a40: 2b 20 5a 29 20 77 68 65 72 65 20 58 2c 20 59 2c  + Z) where X, Y,
5a50: 20 61 6e 64 20 5a 20 61 72 65 20 74 68 65 20 73   and Z are the s
5a60: 61 6d 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 73 20  ame..** numbers 
5a70: 75 73 65 64 20 69 6e 20 5b 53 51 4c 49 54 45 5f  used in [SQLITE_
5a80: 56 45 52 53 49 4f 4e 5d 2e 29 5e 0d 0a 2a 2a 20  VERSION].)^..** 
5a90: 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49  The SQLITE_VERSI
5aa0: 4f 4e 5f 4e 55 4d 42 45 52 20 66 6f 72 20 61 6e  ON_NUMBER for an
5ab0: 79 20 67 69 76 65 6e 20 72 65 6c 65 61 73 65 20  y given release 
5ac0: 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61  of SQLite will a
5ad0: 6c 73 6f 0d 0a 2a 2a 20 62 65 20 6c 61 72 67 65  lso..** be large
5ae0: 72 20 74 68 61 6e 20 74 68 65 20 72 65 6c 65 61  r than the relea
5af0: 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 69 74  se from which it
5b00: 20 69 73 20 64 65 72 69 76 65 64 2e 20 20 45 69   is derived.  Ei
5b10: 74 68 65 72 20 59 20 77 69 6c 6c 0d 0a 2a 2a 20  ther Y will..** 
5b20: 62 65 20 68 65 6c 64 20 63 6f 6e 73 74 61 6e 74  be held constant
5b30: 20 61 6e 64 20 5a 20 77 69 6c 6c 20 62 65 20 69   and Z will be i
5b40: 6e 63 72 65 6d 65 6e 74 65 64 20 6f 72 20 65 6c  ncremented or el
5b50: 73 65 20 59 20 77 69 6c 6c 20 62 65 20 69 6e 63  se Y will be inc
5b60: 72 65 6d 65 6e 74 65 64 0d 0a 2a 2a 20 61 6e 64  remented..** and
5b70: 20 5a 20 77 69 6c 6c 20 62 65 20 72 65 73 65 74   Z will be reset
5b80: 20 74 6f 20 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a   to zero...**..*
5b90: 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20  * Since version 
5ba0: 33 2e 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73  3.6.18, SQLite s
5bb0: 6f 75 72 63 65 20 63 6f 64 65 20 68 61 73 20 62  ource code has b
5bc0: 65 65 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68  een stored in th
5bd0: 65 0d 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68  e..** <a href="h
5be0: 74 74 70 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c  ttp://www.fossil
5bf0: 2d 73 63 6d 2e 6f 72 67 2f 22 3e 46 6f 73 73 69  -scm.org/">Fossi
5c00: 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
5c10: 6d 61 6e 61 67 65 6d 65 6e 74 0d 0a 2a 2a 20 73  management..** s
5c20: 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 5e 54 68 65  ystem</a>.  ^The
5c30: 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49   SQLITE_SOURCE_I
5c40: 44 20 6d 61 63 72 6f 20 65 76 61 6c 75 61 74 65  D macro evaluate
5c50: 73 20 74 6f 0d 0a 2a 2a 20 61 20 73 74 72 69 6e  s to..** a strin
5c60: 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 69  g which identifi
5c70: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
5c80: 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69  check-in of SQLi
5c90: 74 65 0d 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74  te..** within it
5ca0: 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  s configuration 
5cb0: 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65  management syste
5cc0: 6d 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45 5f  m.  ^The SQLITE_
5cd0: 53 4f 55 52 43 45 5f 49 44 0d 0a 2a 2a 20 73 74  SOURCE_ID..** st
5ce0: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 68  ring contains th
5cf0: 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20  e date and time 
5d00: 6f 66 20 74 68 65 20 63 68 65 63 6b 2d 69 6e 20  of the check-in 
5d10: 28 55 54 43 29 20 61 6e 64 20 61 6e 20 53 48 41  (UTC) and an SHA
5d20: 31 0d 0a 2a 2a 20 68 61 73 68 20 6f 66 20 74 68  1..** hash of th
5d30: 65 20 65 6e 74 69 72 65 20 73 6f 75 72 63 65 20  e entire source 
5d40: 74 72 65 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65  tree...**..** Se
5d50: 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
5d60: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0d  _libversion()],.
5d70: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62  .** [sqlite3_lib
5d80: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29  version_number()
5d90: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72  ], [sqlite3_sour
5da0: 63 65 69 64 28 29 5d 2c 0d 0a 2a 2a 20 5b 73 71  ceid()],..** [sq
5db0: 6c 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20  lite_version()] 
5dc0: 61 6e 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72  and [sqlite_sour
5dd0: 63 65 5f 69 64 28 29 5d 2e 0d 0a 2a 2f 0d 0a 23  ce_id()]...*/..#
5de0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45  define SQLITE_VE
5df0: 52 53 49 4f 4e 20 20 20 20 20 20 20 20 22 33 2e  RSION        "3.
5e00: 37 2e 31 30 22 0d 0a 23 64 65 66 69 6e 65 20 53  7.10"..#define S
5e10: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
5e20: 4d 42 45 52 20 33 30 30 37 30 31 30 0d 0a 23 64  MBER 3007010..#d
5e30: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 55  efine SQLITE_SOU
5e40: 52 43 45 5f 49 44 20 20 20 20 20 20 22 32 30 31  RCE_ID      "201
5e50: 32 2d 30 31 2d 31 36 20 31 33 3a 32 38 3a 34 30  2-01-16 13:28:40
5e60: 20 65 62 64 30 31 61 38 64 65 66 66 62 35 30 32   ebd01a8deffb502
5e70: 34 61 35 64 37 34 39 34 65 65 66 38 30 30 64 32  4a5d7494eef800d2
5e80: 33 36 36 64 39 37 32 30 34 22 0d 0a 0d 0a 2f 2a  366d97204"..../*
5e90: 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  ..** CAPI3REF: R
5ea0: 75 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20  un-Time Library 
5eb0: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 0d  Version Numbers.
5ec0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
5ed0: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 2c 20 73  lite3_version, s
5ee0: 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 0d  qlite3_sourceid.
5ef0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 69 6e  .**..** These in
5f00: 74 65 72 66 61 63 65 73 20 70 72 6f 76 69 64 65  terfaces provide
5f10: 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d   the same inform
5f20: 61 74 69 6f 6e 20 61 73 20 74 68 65 20 5b 53 51  ation as the [SQ
5f30: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 2c 0d 0a  LITE_VERSION],..
5f40: 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  ** [SQLITE_VERSI
5f50: 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e 64 20  ON_NUMBER], and 
5f60: 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49  [SQLITE_SOURCE_I
5f70: 44 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  D] C preprocesso
5f80: 72 20 6d 61 63 72 6f 73 0d 0a 2a 2a 20 62 75 74  r macros..** but
5f90: 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20   are associated 
5fa0: 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79  with the library
5fb0: 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
5fc0: 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 5e 28  header file.  ^(
5fd0: 43 61 75 74 69 6f 75 73 0d 0a 2a 2a 20 70 72 6f  Cautious..** pro
5fe0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69  grammers might i
5ff0: 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20  nclude assert() 
6000: 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
6010: 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  eir application 
6020: 74 6f 0d 0a 2a 2a 20 76 65 72 69 66 79 20 74 68  to..** verify th
6030: 61 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  at values return
6040: 65 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65  ed by these inte
6050: 72 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65  rfaces match the
6060: 20 6d 61 63 72 6f 73 20 69 6e 0d 0a 2a 2a 20 74   macros in..** t
6070: 68 65 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74  he header, and t
6080: 68 75 73 20 69 6e 73 75 72 65 20 74 68 61 74 20  hus insure that 
6090: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
60a0: 69 73 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20  is..** compiled 
60b0: 77 69 74 68 20 6d 61 74 63 68 69 6e 67 20 6c 69  with matching li
60c0: 62 72 61 72 79 20 61 6e 64 20 68 65 61 64 65 72  brary and header
60d0: 20 66 69 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   files...**..** 
60e0: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
60f0: 3e 0d 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 71  >..** assert( sq
6100: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
6110: 5f 6e 75 6d 62 65 72 28 29 3d 3d 53 51 4c 49 54  _number()==SQLIT
6120: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
6130: 20 29 3b 0d 0a 2a 2a 20 61 73 73 65 72 74 28 20   );..** assert( 
6140: 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 73  strcmp(sqlite3_s
6150: 6f 75 72 63 65 69 64 28 29 2c 53 51 4c 49 54 45  ourceid(),SQLITE
6160: 5f 53 4f 55 52 43 45 5f 49 44 29 3d 3d 30 20 29  _SOURCE_ID)==0 )
6170: 3b 0d 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 74  ;..** assert( st
6180: 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 6c 69 62  rcmp(sqlite3_lib
6190: 76 65 72 73 69 6f 6e 28 29 2c 53 51 4c 49 54 45  version(),SQLITE
61a0: 5f 56 45 52 53 49 4f 4e 29 3d 3d 30 20 29 3b 0d  _VERSION)==0 );.
61b0: 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
61c0: 6b 71 75 6f 74 65 3e 29 5e 0d 0a 2a 2a 0d 0a 2a  kquote>)^..**..*
61d0: 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 76  * ^The sqlite3_v
61e0: 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20  ersion[] string 
61f0: 63 6f 6e 73 74 61 6e 74 20 63 6f 6e 74 61 69 6e  constant contain
6200: 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 5b 53  s the text of [S
6210: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 0d 0a  QLITE_VERSION]..
6220: 2a 2a 20 6d 61 63 72 6f 2e 20 20 5e 54 68 65 20  ** macro.  ^The 
6230: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
6240: 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  on() function re
6250: 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
6260: 74 6f 20 74 68 65 0d 0a 2a 2a 20 74 6f 20 74 68  to the..** to th
6270: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  e sqlite3_versio
6280: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  n[] string const
6290: 61 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ant.  The sqlite
62a0: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 0d 0a  3_libversion()..
62b0: 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70  ** function is p
62c0: 72 6f 76 69 64 65 64 20 66 6f 72 20 75 73 65 20  rovided for use 
62d0: 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44 4c  in DLLs since DL
62e0: 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79 20  L users usually 
62f0: 64 6f 20 6e 6f 74 20 68 61 76 65 0d 0a 2a 2a 20  do not have..** 
6300: 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74 6f  direct access to
6310: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74   string constant
6320: 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c  s within the DLL
6330: 2e 20 20 5e 54 68 65 0d 0a 2a 2a 20 73 71 6c 69  .  ^The..** sqli
6340: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
6350: 75 6d 62 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  umber() function
6360: 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65   returns an inte
6370: 67 65 72 20 65 71 75 61 6c 20 74 6f 0d 0a 2a 2a  ger equal to..**
6380: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   [SQLITE_VERSION
6390: 5f 4e 55 4d 42 45 52 5d 2e 20 20 5e 54 68 65 20  _NUMBER].  ^The 
63a0: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64  sqlite3_sourceid
63b0: 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  () function retu
63c0: 72 6e 73 20 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74  rns ..** a point
63d0: 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63  er to a string c
63e0: 6f 6e 73 74 61 6e 74 20 77 68 6f 73 65 20 76 61  onstant whose va
63f0: 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  lue is the same 
6400: 61 73 20 74 68 65 20 0d 0a 2a 2a 20 5b 53 51 4c  as the ..** [SQL
6410: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 5d 20 43  ITE_SOURCE_ID] C
6420: 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   preprocessor ma
6430: 63 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  cro...**..** See
6440: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76   also: [sqlite_v
6450: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  ersion()] and [s
6460: 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28  qlite_source_id(
6470: 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  )]...*/..SQLITE_
6480: 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73  API const char s
6490: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d  qlite3_version[]
64a0: 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f   = SQLITE_VERSIO
64b0: 4e 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  N;..SQLITE_API c
64c0: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
64d0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f  e3_libversion(vo
64e0: 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  id);..SQLITE_API
64f0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
6500: 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76 6f  ite3_sourceid(vo
6510: 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  id);..SQLITE_API
6520: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62   int sqlite3_lib
6530: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76  version_number(v
6540: 6f 69 64 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  oid);..../*..** 
6550: 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69  CAPI3REF: Run-Ti
6560: 6d 65 20 4c 69 62 72 61 72 79 20 43 6f 6d 70 69  me Library Compi
6570: 6c 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 44  lation Options D
6580: 69 61 67 6e 6f 73 74 69 63 73 0d 0a 2a 2a 0d 0a  iagnostics..**..
6590: 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
65a0: 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
65b0: 65 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  ed() function re
65c0: 74 75 72 6e 73 20 30 20 6f 72 20 31 20 0d 0a 2a  turns 0 or 1 ..*
65d0: 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65  * indicating whe
65e0: 74 68 65 72 20 74 68 65 20 73 70 65 63 69 66 69  ther the specifi
65f0: 65 64 20 6f 70 74 69 6f 6e 20 77 61 73 20 64 65  ed option was de
6600: 66 69 6e 65 64 20 61 74 20 0d 0a 2a 2a 20 63 6f  fined at ..** co
6610: 6d 70 69 6c 65 20 74 69 6d 65 2e 20 20 5e 54 68  mpile time.  ^Th
6620: 65 20 53 51 4c 49 54 45 5f 20 70 72 65 66 69 78  e SQLITE_ prefix
6630: 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
6640: 66 72 6f 6d 20 74 68 65 20 0d 0a 2a 2a 20 6f 70  from the ..** op
6650: 74 69 6f 6e 20 6e 61 6d 65 20 70 61 73 73 65 64  tion name passed
6660: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70   to sqlite3_comp
6670: 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29  ileoption_used()
6680: 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  .  ..**..** ^The
6690: 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
66a0: 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 66 75 6e  option_get() fun
66b0: 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 69 74 65  ction allows ite
66c0: 72 61 74 69 6e 67 0d 0a 2a 2a 20 6f 76 65 72 20  rating..** over 
66d0: 74 68 65 20 6c 69 73 74 20 6f 66 20 6f 70 74 69  the list of opti
66e0: 6f 6e 73 20 74 68 61 74 20 77 65 72 65 20 64 65  ons that were de
66f0: 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  fined at compile
6700: 20 74 69 6d 65 20 62 79 0d 0a 2a 2a 20 72 65 74   time by..** ret
6710: 75 72 6e 69 6e 67 20 74 68 65 20 4e 2d 74 68 20  urning the N-th 
6720: 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 6f 70 74  compile time opt
6730: 69 6f 6e 20 73 74 72 69 6e 67 2e 20 20 5e 49 66  ion string.  ^If
6740: 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   N is out of ran
6750: 67 65 2c 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ge,..** sqlite3_
6760: 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65  compileoption_ge
6770: 74 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e 55  t() returns a NU
6780: 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 5e 54 68  LL pointer.  ^Th
6790: 65 20 53 51 4c 49 54 45 5f 20 0d 0a 2a 2a 20 70  e SQLITE_ ..** p
67a0: 72 65 66 69 78 20 69 73 20 6f 6d 69 74 74 65 64  refix is omitted
67b0: 20 66 72 6f 6d 20 61 6e 79 20 73 74 72 69 6e 67   from any string
67c0: 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 0d 0a  s returned by ..
67d0: 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  ** sqlite3_compi
67e0: 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 2e 0d  leoption_get()..
67f0: 0a 2a 2a 0d 0a 2a 2a 20 5e 53 75 70 70 6f 72 74  .**..** ^Support
6800: 20 66 6f 72 20 74 68 65 20 64 69 61 67 6e 6f 73   for the diagnos
6810: 74 69 63 20 66 75 6e 63 74 69 6f 6e 73 20 73 71  tic functions sq
6820: 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74  lite3_compileopt
6830: 69 6f 6e 5f 75 73 65 64 28 29 0d 0a 2a 2a 20 61  ion_used()..** a
6840: 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  nd sqlite3_compi
6850: 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 6d  leoption_get() m
6860: 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79  ay be omitted by
6870: 20 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 20   specifying the 
6880: 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d 49  ..** [SQLITE_OMI
6890: 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f  T_COMPILEOPTION_
68a0: 44 49 41 47 53 5d 20 6f 70 74 69 6f 6e 20 61 74  DIAGS] option at
68b0: 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 0d 0a   compile time...
68c0: 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  **..** See also:
68d0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 5b   SQL functions [
68e0: 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70  sqlite_compileop
68f0: 74 69 6f 6e 5f 75 73 65 64 28 29 5d 20 61 6e 64  tion_used()] and
6900: 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 63 6f 6d  ..** [sqlite_com
6910: 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29  pileoption_get()
6920: 5d 20 61 6e 64 20 74 68 65 20 5b 63 6f 6d 70 69  ] and the [compi
6930: 6c 65 5f 6f 70 74 69 6f 6e 73 20 70 72 61 67 6d  le_options pragm
6940: 61 5d 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  a]...*/..#ifndef
6950: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
6960: 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53  PILEOPTION_DIAGS
6970: 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
6980: 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
6990: 6f 70 74 69 6f 6e 5f 75 73 65 64 28 63 6f 6e 73  option_used(cons
69a0: 74 20 63 68 61 72 20 2a 7a 4f 70 74 4e 61 6d 65  t char *zOptName
69b0: 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  );..SQLITE_API c
69c0: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
69d0: 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e  e3_compileoption
69e0: 5f 67 65 74 28 69 6e 74 20 4e 29 3b 0d 0a 23 65  _get(int N);..#e
69f0: 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ndif..../*..** C
6a00: 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 54 6f  API3REF: Test To
6a10: 20 53 65 65 20 49 66 20 54 68 65 20 4c 69 62 72   See If The Libr
6a20: 61 72 79 20 49 73 20 54 68 72 65 61 64 73 61 66  ary Is Threadsaf
6a30: 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73  e..**..** ^The s
6a40: 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
6a50: 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  e() function ret
6a60: 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 61 6e 64  urns zero if and
6a70: 20 6f 6e 6c 79 20 69 66 0d 0a 2a 2a 20 53 51 4c   only if..** SQL
6a80: 69 74 65 20 77 61 73 20 63 6f 6d 70 69 6c 65 64  ite was compiled
6a90: 20 77 69 74 68 20 6d 75 74 65 78 69 6e 67 20 63   with mutexing c
6aa0: 6f 64 65 20 6f 6d 69 74 74 65 64 20 64 75 65 20  ode omitted due 
6ab0: 74 6f 20 74 68 65 0d 0a 2a 2a 20 5b 53 51 4c 49  to the..** [SQLI
6ac0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 63  TE_THREADSAFE] c
6ad0: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
6ae0: 6f 6e 20 62 65 69 6e 67 20 73 65 74 20 74 6f 20  on being set to 
6af0: 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74  0...**..** SQLit
6b00: 65 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65  e can be compile
6b10: 64 20 77 69 74 68 20 6f 72 20 77 69 74 68 6f 75  d with or withou
6b20: 74 20 6d 75 74 65 78 65 73 2e 20 20 57 68 65 6e  t mutexes.  When
6b30: 0d 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45  ..** the [SQLITE
6b40: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 43 20 70  _THREADSAFE] C p
6b50: 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
6b60: 6f 20 69 73 20 31 20 6f 72 20 32 2c 20 6d 75 74  o is 1 or 2, mut
6b70: 65 78 65 73 0d 0a 2a 2a 20 61 72 65 20 65 6e 61  exes..** are ena
6b80: 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20  bled and SQLite 
6b90: 69 73 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  is threadsafe.  
6ba0: 57 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 5b 53 51  When the..** [SQ
6bb0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d  LITE_THREADSAFE]
6bc0: 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0d 0a 2a   macro is 0, ..*
6bd0: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72  * the mutexes ar
6be0: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68  e omitted.  With
6bf0: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c  out the mutexes,
6c00: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0d   it is not safe.
6c10: 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74  .** to use SQLit
6c20: 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66  e concurrently f
6c30: 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  rom more than on
6c40: 65 20 74 68 72 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a  e thread...**..*
6c50: 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78  * Enabling mutex
6c60: 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73  es incurs a meas
6c70: 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e  urable performan
6c80: 63 65 20 70 65 6e 61 6c 74 79 2e 0d 0a 2a 2a 20  ce penalty...** 
6c90: 53 6f 20 69 66 20 73 70 65 65 64 20 69 73 20 6f  So if speed is o
6ca0: 66 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61  f utmost importa
6cb0: 6e 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65  nce, it makes se
6cc0: 6e 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0d 0a  nse to disable..
6cd0: 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20  ** the mutexes. 
6ce0: 20 42 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d   But for maximum
6cf0: 20 73 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73   safety, mutexes
6d00: 20 73 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c   should be enabl
6d10: 65 64 2e 0d 0a 2a 2a 20 5e 54 68 65 20 64 65 66  ed...** ^The def
6d20: 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 73  ault behavior is
6d30: 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74 6f 20   for mutexes to 
6d40: 62 65 20 65 6e 61 62 6c 65 64 2e 0d 0a 2a 2a 0d  be enabled...**.
6d50: 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
6d60: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 62  ce can be used b
6d70: 79 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  y an application
6d80: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
6d90: 61 74 20 74 68 65 0d 0a 2a 2a 20 76 65 72 73 69  at the..** versi
6da0: 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 61  on of SQLite tha
6db0: 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20  t it is linking 
6dc0: 61 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d 70  against was comp
6dd0: 69 6c 65 64 20 77 69 74 68 0d 0a 2a 2a 20 74 68  iled with..** th
6de0: 65 20 64 65 73 69 72 65 64 20 73 65 74 74 69 6e  e desired settin
6df0: 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  g of the [SQLITE
6e00: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63  _THREADSAFE] mac
6e10: 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ro...**..** This
6e20: 20 69 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20   interface only 
6e30: 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63  reports on the c
6e40: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65  ompile-time mute
6e50: 78 20 73 65 74 74 69 6e 67 0d 0a 2a 2a 20 6f 66  x setting..** of
6e60: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52   the [SQLITE_THR
6e70: 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e 20 20  EADSAFE] flag.  
6e80: 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
6e90: 70 69 6c 65 64 20 77 69 74 68 0d 0a 2a 2a 20 53  piled with..** S
6ea0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
6eb0: 3d 31 20 6f 72 20 3d 32 20 74 68 65 6e 20 6d 75  =1 or =2 then mu
6ec0: 74 65 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65  texes are enable
6ed0: 64 20 62 79 20 64 65 66 61 75 6c 74 20 62 75 74  d by default but
6ee0: 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c  ..** can be full
6ef0: 79 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 64  y or partially d
6f00: 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 61 20  isabled using a 
6f10: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
6f20: 5f 63 6f 6e 66 69 67 28 29 5d 0d 0a 2a 2a 20 77  _config()]..** w
6f30: 69 74 68 20 74 68 65 20 76 65 72 62 73 20 5b 53  ith the verbs [S
6f40: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
6f50: 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c  GLETHREAD], [SQL
6f60: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49  ITE_CONFIG_MULTI
6f70: 54 48 52 45 41 44 5d 2c 0d 0a 2a 2a 20 6f 72 20  THREAD],..** or 
6f80: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
6f90: 55 54 45 58 5d 2e 20 20 5e 28 54 68 65 20 72 65  UTEX].  ^(The re
6fa0: 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
6fb0: 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 68  e..** sqlite3_th
6fc0: 72 65 61 64 73 61 66 65 28 29 20 66 75 6e 63 74  readsafe() funct
6fd0: 69 6f 6e 20 73 68 6f 77 73 20 6f 6e 6c 79 20 74  ion shows only t
6fe0: 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  he compile-time 
6ff0: 73 65 74 74 69 6e 67 20 6f 66 0d 0a 2a 2a 20 74  setting of..** t
7000: 68 72 65 61 64 20 73 61 66 65 74 79 2c 20 6e 6f  hread safety, no
7010: 74 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63  t any run-time c
7020: 68 61 6e 67 65 73 20 74 6f 20 74 68 61 74 20 73  hanges to that s
7030: 65 74 74 69 6e 67 20 6d 61 64 65 20 62 79 0d 0a  etting made by..
7040: 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  ** sqlite3_confi
7050: 67 28 29 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f  g(). In other wo
7060: 72 64 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20  rds, the return 
7070: 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 74  value from sqlit
7080: 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 0d  e3_threadsafe().
7090: 0a 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64  .** is unchanged
70a0: 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c   by calls to sql
70b0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 29 5e  ite3_config().)^
70c0: 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 74 68 65  ..**..** See the
70d0: 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
70e0: 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  ] documentation 
70f0: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
7100: 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d  nformation...*/.
7110: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
7120: 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61  sqlite3_threadsa
7130: 66 65 28 76 6f 69 64 29 3b 0d 0a 0d 0a 2f 2a 0d  fe(void);..../*.
7140: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
7150: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
7160: 6e 20 48 61 6e 64 6c 65 0d 0a 2a 2a 20 4b 45 59  n Handle..** KEY
7170: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65  WORDS: {database
7180: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61   connection} {da
7190: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
71a0: 6e 73 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61 63 68  ns}..**..** Each
71b0: 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74   open SQLite dat
71c0: 61 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65  abase is represe
71d0: 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65  nted by a pointe
71e0: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
71f0: 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 6f 70 61 71   of..** the opaq
7200: 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d  ue structure nam
7210: 65 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49  ed "sqlite3".  I
7220: 74 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74  t is useful to t
7230: 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74  hink of an sqlit
7240: 65 33 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61  e3..** pointer a
7250: 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68  s an object.  Th
7260: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  e [sqlite3_open(
7270: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
7280: 6e 31 36 28 29 5d 2c 20 61 6e 64 0d 0a 2a 2a 20  n16()], and..** 
7290: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
72a0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 61  ()] interfaces a
72b0: 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 74  re its construct
72c0: 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ors, and [sqlite
72d0: 33 5f 63 6c 6f 73 65 28 29 5d 0d 0a 2a 2a 20 69  3_close()]..** i
72e0: 73 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72  s its destructor
72f0: 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e  .  There are man
7300: 79 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63  y other interfac
7310: 65 73 20 28 73 75 63 68 20 61 73 0d 0a 2a 2a 20  es (such as..** 
7320: 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
7330: 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  _v2()], [sqlite3
7340: 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
7350: 28 29 5d 2c 20 61 6e 64 0d 0a 2a 2a 20 5b 73 71  ()], and..** [sq
7360: 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
7370: 75 74 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75  ut()] to name bu
7380: 74 20 74 68 72 65 65 29 20 74 68 61 74 20 61 72  t three) that ar
7390: 65 20 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0d  e methods on an.
73a0: 0a 2a 2a 20 73 71 6c 69 74 65 33 20 6f 62 6a 65  .** sqlite3 obje
73b0: 63 74 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  ct...*/..typedef
73c0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20   struct sqlite3 
73d0: 73 71 6c 69 74 65 33 3b 0d 0a 0d 0a 2f 2a 0d 0a  sqlite3;..../*..
73e0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d  ** CAPI3REF: 64-
73f0: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65  Bit Integer Type
7400: 73 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  s..** KEYWORDS: 
7410: 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
7420: 69 74 65 5f 75 69 6e 74 36 34 0d 0a 2a 2a 0d 0a  ite_uint64..**..
7430: 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 65  ** Because there
7440: 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61   is no cross-pla
7450: 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 65  tform way to spe
7460: 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 65  cify 64-bit inte
7470: 67 65 72 20 74 79 70 65 73 0d 0a 2a 2a 20 53 51  ger types..** SQ
7480: 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79  Lite includes ty
7490: 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69  pedefs for 64-bi
74a0: 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73  t signed and uns
74b0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0d  igned integers..
74c0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  .**..** The sqli
74d0: 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71  te3_int64 and sq
74e0: 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65  lite3_uint64 are
74f0: 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
7500: 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e  ype definitions.
7510: 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f  ..** The sqlite_
7520: 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65  int64 and sqlite
7530: 5f 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 72  _uint64 types ar
7540: 65 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  e supported for 
7550: 62 61 63 6b 77 61 72 64 73 0d 0a 2a 2a 20 63 6f  backwards..** co
7560: 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79  mpatibility only
7570: 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73  ...**..** ^The s
7580: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e 64  qlite3_int64 and
7590: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 74 79   sqlite_int64 ty
75a0: 70 65 73 20 63 61 6e 20 73 74 6f 72 65 20 69 6e  pes can store in
75b0: 74 65 67 65 72 20 76 61 6c 75 65 73 0d 0a 2a 2a  teger values..**
75c0: 20 62 65 74 77 65 65 6e 20 2d 39 32 32 33 33 37   between -922337
75d0: 32 30 33 36 38 35 34 37 37 35 38 30 38 20 61 6e  2036854775808 an
75e0: 64 20 2b 39 32 32 33 33 37 32 30 33 36 38 35 34  d +9223372036854
75f0: 37 37 35 38 30 37 20 69 6e 63 6c 75 73 69 76 65  775807 inclusive
7600: 2e 20 20 5e 54 68 65 0d 0a 2a 2a 20 73 71 6c 69  .  ^The..** sqli
7610: 74 65 33 5f 75 69 6e 74 36 34 20 61 6e 64 20 73  te3_uint64 and s
7620: 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70  qlite_uint64 typ
7630: 65 73 20 63 61 6e 20 73 74 6f 72 65 20 69 6e 74  es can store int
7640: 65 67 65 72 20 76 61 6c 75 65 73 20 0d 0a 2a 2a  eger values ..**
7650: 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 2b   between 0 and +
7660: 31 38 34 34 36 37 34 34 30 37 33 37 30 39 35 35  1844674407370955
7670: 31 36 31 35 20 69 6e 63 6c 75 73 69 76 65 2e 0d  1615 inclusive..
7680: 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
7690: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0d 0a 20  TE_INT64_TYPE.. 
76a0: 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f   typedef SQLITE_
76b0: 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74  INT64_TYPE sqlit
76c0: 65 5f 69 6e 74 36 34 3b 0d 0a 20 20 74 79 70 65  e_int64;..  type
76d0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c  def unsigned SQL
76e0: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73  ITE_INT64_TYPE s
76f0: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0d 0a 23  qlite_uint64;..#
7700: 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
7710: 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65  C_VER) || define
7720: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0d  d(__BORLANDC__).
7730: 0a 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74  .  typedef __int
7740: 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b  64 sqlite_int64;
7750: 0d 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69  ..  typedef unsi
7760: 67 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c  gned __int64 sql
7770: 69 74 65 5f 75 69 6e 74 36 34 3b 0d 0a 23 65 6c  ite_uint64;..#el
7780: 73 65 0d 0a 20 20 74 79 70 65 64 65 66 20 6c 6f  se..  typedef lo
7790: 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69  ng long int sqli
77a0: 74 65 5f 69 6e 74 36 34 3b 0d 0a 20 20 74 79 70  te_int64;..  typ
77b0: 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 6c 6f  edef unsigned lo
77c0: 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69  ng long int sqli
77d0: 74 65 5f 75 69 6e 74 36 34 3b 0d 0a 23 65 6e 64  te_uint64;..#end
77e0: 69 66 0d 0a 74 79 70 65 64 65 66 20 73 71 6c 69  if..typedef sqli
77f0: 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
7800: 5f 69 6e 74 36 34 3b 0d 0a 74 79 70 65 64 65 66  _int64;..typedef
7810: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
7820: 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 3b 0d 0a  qlite3_uint64;..
7830: 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 63 6f 6d 70  ../*..** If comp
7840: 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63  iling for a proc
7850: 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73  essor that lacks
7860: 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
7870: 73 75 70 70 6f 72 74 2c 0d 0a 2a 2a 20 73 75 62  support,..** sub
7880: 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 20  stitute integer 
7890: 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69  for floating-poi
78a0: 6e 74 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  nt...*/..#ifdef 
78b0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
78c0: 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 23 20 64 65  TING_POINT..# de
78d0: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69  fine double sqli
78e0: 74 65 33 5f 69 6e 74 36 34 0d 0a 23 65 6e 64 69  te3_int64..#endi
78f0: 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  f..../*..** CAPI
7900: 33 52 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20  3REF: Closing A 
7910: 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74  Database Connect
7920: 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  ion..**..** ^The
7930: 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29   sqlite3_close()
7940: 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
7950: 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74  destructor for t
7960: 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a  he [sqlite3] obj
7970: 65 63 74 2e 0d 0a 2a 2a 20 5e 43 61 6c 6c 73 20  ect...** ^Calls 
7980: 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  to sqlite3_close
7990: 28 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  () return SQLITE
79a0: 5f 4f 4b 20 69 66 20 74 68 65 20 5b 73 71 6c 69  _OK if the [sqli
79b0: 74 65 33 5d 20 6f 62 6a 65 63 74 20 69 73 0d 0a  te3] object is..
79c0: 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ** successfully 
79d0: 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 61 6c  destroyed and al
79e0: 6c 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73  l associated res
79f0: 6f 75 72 63 65 73 20 61 72 65 20 64 65 61 6c 6c  ources are deall
7a00: 6f 63 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ocated...**..** 
7a10: 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73  Applications mus
7a20: 74 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  t [sqlite3_final
7a30: 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20  ize | finalize] 
7a40: 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74  all [prepared st
7a50: 61 74 65 6d 65 6e 74 73 5d 0d 0a 2a 2a 20 61 6e  atements]..** an
7a60: 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
7a70: 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61  close | close] a
7a80: 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73  ll [BLOB handles
7a90: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  ] associated wit
7aa0: 68 0d 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  h..** the [sqlit
7ab0: 65 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72  e3] object prior
7ac0: 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74   to attempting t
7ad0: 6f 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65  o close the obje
7ae0: 63 74 2e 20 20 5e 49 66 0d 0a 2a 2a 20 73 71 6c  ct.  ^If..** sql
7af0: 69 74 65 33 5f 63 6c 6f 73 65 28 29 20 69 73 20  ite3_close() is 
7b00: 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b 64 61 74  called on a [dat
7b10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
7b20: 5d 20 74 68 61 74 20 73 74 69 6c 6c 20 68 61 73  ] that still has
7b30: 0d 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67  ..** outstanding
7b40: 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
7b50: 6d 65 6e 74 73 5d 20 6f 72 20 5b 42 4c 4f 42 20  ments] or [BLOB 
7b60: 68 61 6e 64 6c 65 73 5d 2c 20 74 68 65 6e 20 69  handles], then i
7b70: 74 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 53 51  t returns..** SQ
7b80: 4c 49 54 45 5f 42 55 53 59 2e 0d 0a 2a 2a 0d 0a  LITE_BUSY...**..
7b90: 2a 2a 20 5e 49 66 20 5b 73 71 6c 69 74 65 33 5f  ** ^If [sqlite3_
7ba0: 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 6f  close()] is invo
7bb0: 6b 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e  ked while a tran
7bc0: 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c  saction is open,
7bd0: 0d 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63  ..** the transac
7be0: 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69  tion is automati
7bf0: 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
7c00: 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 43  k...**..** The C
7c10: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
7c20: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d  qlite3_close(C)]
7c30: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
7c40: 61 20 4e 55 4c 4c 0d 0a 2a 2a 20 70 6f 69 6e 74  a NULL..** point
7c50: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65  er or an [sqlite
7c60: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65  3] object pointe
7c70: 72 20 6f 62 74 61 69 6e 65 64 0d 0a 2a 2a 20 66  r obtained..** f
7c80: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  rom [sqlite3_ope
7c90: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f  n()], [sqlite3_o
7ca0: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 0d 0a 2a 2a  pen16()], or..**
7cb0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
7cc0: 32 28 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72  2()], and not pr
7cd0: 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e  eviously closed.
7ce0: 0d 0a 2a 2a 20 5e 43 61 6c 6c 69 6e 67 20 73 71  ..** ^Calling sq
7cf0: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 20 77 69  lite3_close() wi
7d00: 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  th a NULL pointe
7d10: 72 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  r argument is a 
7d20: 0d 0a 2a 2a 20 68 61 72 6d 6c 65 73 73 20 6e 6f  ..** harmless no
7d30: 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  -op...*/..SQLITE
7d40: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
7d50: 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a  _close(sqlite3 *
7d60: 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  );..../*..** The
7d70: 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c   type for a call
7d80: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  back function...
7d90: 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 63  ** This is legac
7da0: 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 64  y and deprecated
7db0: 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65  .  It is include
7dc0: 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  d for historical
7dd0: 0d 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  ..** compatibili
7de0: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f  ty and is not do
7df0: 63 75 6d 65 6e 74 65 64 2e 0d 0a 2a 2f 0d 0a 74  cumented...*/..t
7e00: 79 70 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c  ypedef int (*sql
7e10: 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76  ite3_callback)(v
7e20: 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c  oid*,int,char**,
7e30: 20 63 68 61 72 2a 2a 29 3b 0d 0a 0d 0a 2f 2a 0d   char**);..../*.
7e40: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e  .** CAPI3REF: On
7e50: 65 2d 53 74 65 70 20 51 75 65 72 79 20 45 78 65  e-Step Query Exe
7e60: 63 75 74 69 6f 6e 20 49 6e 74 65 72 66 61 63 65  cution Interface
7e70: 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c  ..**..** The sql
7e80: 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65  ite3_exec() inte
7e90: 72 66 61 63 65 20 69 73 20 61 20 63 6f 6e 76 65  rface is a conve
7ea0: 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61  nience wrapper a
7eb0: 72 6f 75 6e 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74  round..** [sqlit
7ec0: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
7ed0: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
7ee0: 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
7ef0: 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2c 0d 0a 2a  _finalize()],..*
7f00: 2a 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 61 6e  * that allows an
7f10: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20   application to 
7f20: 72 75 6e 20 6d 75 6c 74 69 70 6c 65 20 73 74 61  run multiple sta
7f30: 74 65 6d 65 6e 74 73 20 6f 66 20 53 51 4c 0d 0a  tements of SQL..
7f40: 2a 2a 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  ** without havin
7f50: 67 20 74 6f 20 75 73 65 20 61 20 6c 6f 74 20 6f  g to use a lot o
7f60: 66 20 43 20 63 6f 64 65 2e 20 0d 0a 2a 2a 0d 0a  f C code. ..**..
7f70: 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
7f80: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65  exec() interface
7f90: 20 72 75 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f   runs zero or mo
7fa0: 72 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  re UTF-8 encoded
7fb0: 2c 0d 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d  ,..** semicolon-
7fc0: 73 65 70 61 72 61 74 65 20 53 51 4c 20 73 74 61  separate SQL sta
7fd0: 74 65 6d 65 6e 74 73 20 70 61 73 73 65 64 20 69  tements passed i
7fe0: 6e 74 6f 20 69 74 73 20 32 6e 64 20 61 72 67 75  nto its 2nd argu
7ff0: 6d 65 6e 74 2c 0d 0a 2a 2a 20 69 6e 20 74 68 65  ment,..** in the
8000: 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
8010: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
8020: 74 69 6f 6e 5d 20 70 61 73 73 65 64 20 69 6e 20  tion] passed in 
8030: 61 73 20 69 74 73 20 31 73 74 0d 0a 2a 2a 20 61  as its 1st..** a
8040: 72 67 75 6d 65 6e 74 2e 20 20 5e 49 66 20 74 68  rgument.  ^If th
8050: 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
8060: 69 6f 6e 20 6f 66 20 74 68 65 20 33 72 64 20 61  ion of the 3rd a
8070: 72 67 75 6d 65 6e 74 20 74 6f 0d 0a 2a 2a 20 73  rgument to..** s
8080: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 73  qlite3_exec() is
8090: 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
80a0: 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f  it is invoked fo
80b0: 72 20 65 61 63 68 20 72 65 73 75 6c 74 20 72 6f  r each result ro
80c0: 77 0d 0a 2a 2a 20 63 6f 6d 69 6e 67 20 6f 75 74  w..** coming out
80d0: 20 6f 66 20 74 68 65 20 65 76 61 6c 75 61 74 65   of the evaluate
80e0: 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  d SQL statements
80f0: 2e 20 20 5e 54 68 65 20 34 74 68 20 61 72 67 75  .  ^The 4th argu
8100: 6d 65 6e 74 20 74 6f 0d 0a 2a 2a 20 73 71 6c 69  ment to..** sqli
8110: 74 65 33 5f 65 78 65 63 28 29 20 69 73 20 72 65  te3_exec() is re
8120: 6c 61 79 65 64 20 74 68 72 6f 75 67 68 20 74 6f  layed through to
8130: 20 74 68 65 20 31 73 74 20 61 72 67 75 6d 65 6e   the 1st argumen
8140: 74 20 6f 66 20 65 61 63 68 0d 0a 2a 2a 20 63 61  t of each..** ca
8150: 6c 6c 62 61 63 6b 20 69 6e 76 6f 63 61 74 69 6f  llback invocatio
8160: 6e 2e 20 20 5e 49 66 20 74 68 65 20 63 61 6c 6c  n.  ^If the call
8170: 62 61 63 6b 20 70 6f 69 6e 74 65 72 20 74 6f 20  back pointer to 
8180: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0d 0a  sqlite3_exec()..
8190: 2a 2a 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  ** is NULL, then
81a0: 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 20 69 73 20   no callback is 
81b0: 65 76 65 72 20 69 6e 76 6f 6b 65 64 20 61 6e 64  ever invoked and
81c0: 20 72 65 73 75 6c 74 20 72 6f 77 73 20 61 72 65   result rows are
81d0: 0d 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 0d 0a 2a  ..** ignored...*
81e0: 2a 0d 0a 2a 2a 20 5e 49 66 20 61 6e 20 65 72 72  *..** ^If an err
81f0: 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
8200: 65 76 61 6c 75 61 74 69 6e 67 20 74 68 65 20 53  evaluating the S
8210: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 70 61  QL statements pa
8220: 73 73 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 73 71  ssed into..** sq
8230: 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 74 68  lite3_exec(), th
8240: 65 6e 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  en execution of 
8250: 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
8260: 65 6d 65 6e 74 20 73 74 6f 70 73 20 61 6e 64 0d  ement stops and.
8270: 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 73  .** subsequent s
8280: 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 73 6b  tatements are sk
8290: 69 70 70 65 64 2e 20 20 5e 49 66 20 74 68 65 20  ipped.  ^If the 
82a0: 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  5th parameter to
82b0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0d   sqlite3_exec().
82c0: 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  .** is not NULL 
82d0: 74 68 65 6e 20 61 6e 79 20 65 72 72 6f 72 20 6d  then any error m
82e0: 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65  essage is writte
82f0: 6e 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  n into memory ob
8300: 74 61 69 6e 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20  tained..** from 
8310: 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
8320: 29 5d 20 61 6e 64 20 70 61 73 73 65 64 20 62 61  )] and passed ba
8330: 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 35  ck through the 5
8340: 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a 2a  th parameter...*
8350: 2a 20 54 6f 20 61 76 6f 69 64 20 6d 65 6d 6f 72  * To avoid memor
8360: 79 20 6c 65 61 6b 73 2c 20 74 68 65 20 61 70 70  y leaks, the app
8370: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  lication should 
8380: 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f  invoke [sqlite3_
8390: 66 72 65 65 28 29 5d 0d 0a 2a 2a 20 6f 6e 20 65  free()]..** on e
83a0: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
83b0: 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 74 68  ings returned th
83c0: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61  rough the 5th pa
83d0: 72 61 6d 65 74 65 72 20 6f 66 0d 0a 2a 2a 20 6f  rameter of..** o
83e0: 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  f sqlite3_exec()
83f0: 20 61 66 74 65 72 20 74 68 65 20 65 72 72 6f 72   after the error
8400: 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20   message string 
8410: 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65  is no longer nee
8420: 64 65 64 2e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65  ded...** ^If the
8430: 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   5th parameter t
8440: 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  o sqlite3_exec()
8450: 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64   is not NULL and
8460: 20 6e 6f 20 65 72 72 6f 72 73 0d 0a 2a 2a 20 6f   no errors..** o
8470: 63 63 75 72 2c 20 74 68 65 6e 20 73 71 6c 69 74  ccur, then sqlit
8480: 65 33 5f 65 78 65 63 28 29 20 73 65 74 73 20 74  e3_exec() sets t
8490: 68 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74  he pointer in it
84a0: 73 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  s 5th parameter 
84b0: 74 6f 0d 0a 2a 2a 20 4e 55 4c 4c 20 62 65 66 6f  to..** NULL befo
84c0: 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a  re returning...*
84d0: 2a 0d 0a 2a 2a 20 5e 49 66 20 61 6e 20 73 71 6c  *..** ^If an sql
84e0: 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c  ite3_exec() call
84f0: 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e  back returns non
8500: 2d 7a 65 72 6f 2c 20 74 68 65 20 73 71 6c 69 74  -zero, the sqlit
8510: 65 33 5f 65 78 65 63 28 29 0d 0a 2a 2a 20 72 6f  e3_exec()..** ro
8520: 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51  utine returns SQ
8530: 4c 49 54 45 5f 41 42 4f 52 54 20 77 69 74 68 6f  LITE_ABORT witho
8540: 75 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  ut invoking the 
8550: 63 61 6c 6c 62 61 63 6b 20 61 67 61 69 6e 20 61  callback again a
8560: 6e 64 0d 0a 2a 2a 20 77 69 74 68 6f 75 74 20 72  nd..** without r
8570: 75 6e 6e 69 6e 67 20 61 6e 79 20 73 75 62 73 65  unning any subse
8580: 71 75 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d  quent SQL statem
8590: 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  ents...**..** ^T
85a0: 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 20  he 2nd argument 
85b0: 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65  to the sqlite3_e
85c0: 78 65 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 66  xec() callback f
85d0: 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0d 0a  unction is the..
85e0: 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  ** number of col
85f0: 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
8600: 6c 74 2e 20 20 5e 54 68 65 20 33 72 64 20 61 72  lt.  ^The 3rd ar
8610: 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71  gument to the sq
8620: 6c 69 74 65 33 5f 65 78 65 63 28 29 0d 0a 2a 2a  lite3_exec()..**
8630: 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
8640: 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
8650: 73 20 74 6f 20 73 74 72 69 6e 67 73 20 6f 62 74  s to strings obt
8660: 61 69 6e 65 64 20 61 73 20 69 66 20 66 72 6f 6d  ained as if from
8670: 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  ..** [sqlite3_co
8680: 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 6f 6e  lumn_text()], on
8690: 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
86a0: 6e 2e 20 20 5e 49 66 20 61 6e 20 65 6c 65 6d 65  n.  ^If an eleme
86b0: 6e 74 20 6f 66 20 61 0d 0a 2a 2a 20 72 65 73 75  nt of a..** resu
86c0: 6c 74 20 72 6f 77 20 69 73 20 4e 55 4c 4c 20 74  lt row is NULL t
86d0: 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f  hen the correspo
86e0: 6e 64 69 6e 67 20 73 74 72 69 6e 67 20 70 6f 69  nding string poi
86f0: 6e 74 65 72 20 66 6f 72 20 74 68 65 0d 0a 2a 2a  nter for the..**
8700: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
8710: 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 4e 55  callback is a NU
8720: 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 5e 54 68  LL pointer.  ^Th
8730: 65 20 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74  e 4th argument t
8740: 6f 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74 65  o the..** sqlite
8750: 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 62 61 63  3_exec() callbac
8760: 6b 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  k is an array of
8770: 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72   pointers to str
8780: 69 6e 67 73 20 77 68 65 72 65 20 65 61 63 68 0d  ings where each.
8790: 0a 2a 2a 20 65 6e 74 72 79 20 72 65 70 72 65 73  .** entry repres
87a0: 65 6e 74 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ents the name of
87b0: 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72   corresponding r
87c0: 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 61 73 20  esult column as 
87d0: 6f 62 74 61 69 6e 65 64 0d 0a 2a 2a 20 66 72 6f  obtained..** fro
87e0: 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  m [sqlite3_colum
87f0: 6e 5f 6e 61 6d 65 28 29 5d 2e 0d 0a 2a 2a 0d 0a  n_name()]...**..
8800: 2a 2a 20 5e 49 66 20 74 68 65 20 32 6e 64 20 70  ** ^If the 2nd p
8810: 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
8820: 74 65 33 5f 65 78 65 63 28 29 20 69 73 20 61 20  te3_exec() is a 
8830: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 61 20  NULL pointer, a 
8840: 70 6f 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 61  pointer..** to a
8850: 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20  n empty string, 
8860: 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 68 61  or a pointer tha
8870: 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20  t contains only 
8880: 77 68 69 74 65 73 70 61 63 65 20 61 6e 64 2f 6f  whitespace and/o
8890: 72 20 0d 0a 2a 2a 20 53 51 4c 20 63 6f 6d 6d 65  r ..** SQL comme
88a0: 6e 74 73 2c 20 74 68 65 6e 20 6e 6f 20 53 51 4c  nts, then no SQL
88b0: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
88c0: 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68  evaluated and th
88d0: 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 69  e database..** i
88e0: 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e 0d 0a  s not changed...
88f0: 2a 2a 0d 0a 2a 2a 20 52 65 73 74 72 69 63 74 69  **..** Restricti
8900: 6f 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c  ons:..**..** <ul
8910: 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 61  >..** <li> The a
8920: 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
8930: 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  insure that the 
8940: 31 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  1st parameter to
8950: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0d   sqlite3_exec().
8960: 0a 2a 2a 20 20 20 20 20 20 69 73 20 61 20 76 61  .**      is a va
8970: 6c 69 64 20 61 6e 64 20 6f 70 65 6e 20 5b 64 61  lid and open [da
8980: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
8990: 6e 5d 2e 0d 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65  n]...** <li> The
89a0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
89b0: 74 20 6e 6f 74 20 63 6c 6f 73 65 20 5b 64 61 74  t not close [dat
89c0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
89d0: 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 0d 0a  ] specified by..
89e0: 2a 2a 20 20 20 20 20 20 74 68 65 20 31 73 74 20  **      the 1st 
89f0: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
8a00: 69 74 65 33 5f 65 78 65 63 28 29 20 77 68 69 6c  ite3_exec() whil
8a10: 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  e sqlite3_exec()
8a20: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0d 0a 2a 2a   is running...**
8a30: 20 3c 6c 69 3e 20 54 68 65 20 61 70 70 6c 69 63   <li> The applic
8a40: 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 6d  ation must not m
8a50: 6f 64 69 66 79 20 74 68 65 20 53 51 4c 20 73 74  odify the SQL st
8a60: 61 74 65 6d 65 6e 74 20 74 65 78 74 20 70 61 73  atement text pas
8a70: 73 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 20 20 20  sed into..**    
8a80: 20 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65    the 2nd parame
8a90: 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 65  ter of sqlite3_e
8aa0: 78 65 63 28 29 20 77 68 69 6c 65 20 73 71 6c 69  xec() while sqli
8ab0: 74 65 33 5f 65 78 65 63 28 29 20 69 73 20 72 75  te3_exec() is ru
8ac0: 6e 6e 69 6e 67 2e 0d 0a 2a 2a 20 3c 2f 75 6c 3e  nning...** </ul>
8ad0: 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
8ae0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
8af0: 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  c(..  sqlite3*, 
8b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b20: 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61   /* An open data
8b30: 62 61 73 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  base */..  const
8b40: 20 63 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 20   char *sql,     
8b50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b60: 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20        /* SQL to 
8b70: 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0d  be evaluated */.
8b80: 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63  .  int (*callbac
8b90: 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61  k)(void*,int,cha
8ba0: 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a  r**,char**),  /*
8bb0: 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   Callback functi
8bc0: 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 2c  on */..  void *,
8bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8bf0: 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d      /* 1st argum
8c00: 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20  ent to callback 
8c10: 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 65 72 72  */..  char **err
8c20: 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  msg             
8c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c40: 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
8c50: 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0d 0a 29  itten here */..)
8c60: 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
8c70: 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64  3REF: Result Cod
8c80: 65 73 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  es..** KEYWORDS:
8c90: 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f   SQLITE_OK {erro
8ca0: 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63  r code} {error c
8cb0: 6f 64 65 73 7d 0d 0a 2a 2a 20 4b 45 59 57 4f 52  odes}..** KEYWOR
8cc0: 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65  DS: {result code
8cd0: 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d  } {result codes}
8ce0: 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 61 6e 79 20 53 51  ..**..** Many SQ
8cf0: 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 72  Lite functions r
8d00: 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
8d10: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f   result code fro
8d20: 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 6e 0d  m the set shown.
8d30: 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 65  .** here in orde
8d40: 72 20 74 6f 20 69 6e 64 69 63 61 74 65 20 73 75  r to indicate su
8d50: 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65  ccess or failure
8d60: 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 65 77 20 65 72  ...**..** New er
8d70: 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65  ror codes may be
8d80: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
8d90: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
8da0: 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  ite...**..** See
8db0: 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49   also: [SQLITE_I
8dc0: 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65  OERR_READ | exte
8dd0: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
8de0: 73 5d 2c 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  s],..** [sqlite3
8df0: 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63  _vtab_on_conflic
8e00: 74 28 29 5d 20 5b 53 51 4c 49 54 45 5f 52 4f 4c  t()] [SQLITE_ROL
8e10: 4c 42 41 43 4b 20 7c 20 72 65 73 75 6c 74 20 63  LBACK | result c
8e20: 6f 64 65 73 5d 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  odes]...*/..#def
8e30: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20  ine SQLITE_OK   
8e40: 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53          0   /* S
8e50: 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74  uccessful result
8e60: 20 2a 2f 0d 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e   */../* beginnin
8e70: 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73  g-of-error-codes
8e80: 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
8e90: 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20  ITE_ERROR       
8ea0: 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f   1   /* SQL erro
8eb0: 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74  r or missing dat
8ec0: 61 62 61 73 65 20 2a 2f 0d 0a 23 64 65 66 69 6e  abase */..#defin
8ed0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41  e SQLITE_INTERNA
8ee0: 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 49 6e 74  L     2   /* Int
8ef0: 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f  ernal logic erro
8f00: 72 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0d 0a  r in SQLite */..
8f10: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50  #define SQLITE_P
8f20: 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 20 20  ERM         3   
8f30: 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d 69 73  /* Access permis
8f40: 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0d 0a  sion denied */..
8f50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
8f60: 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 20  BORT        4   
8f70: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74  /* Callback rout
8f80: 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 6e  ine requested an
8f90: 20 61 62 6f 72 74 20 2a 2f 0d 0a 23 64 65 66 69   abort */..#defi
8fa0: 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20  ne SQLITE_BUSY  
8fb0: 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68         5   /* Th
8fc0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
8fd0: 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 23 64  is locked */..#d
8fe0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
8ff0: 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a  KED       6   /*
9000: 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20   A table in the 
9010: 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
9020: 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ed */..#define S
9030: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20  QLITE_NOMEM     
9040: 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c     7   /* A mall
9050: 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0d 0a  oc() failed */..
9060: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
9070: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20  EADONLY     8   
9080: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72  /* Attempt to wr
9090: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64  ite a readonly d
90a0: 61 74 61 62 61 73 65 20 2a 2f 0d 0a 23 64 65 66  atabase */..#def
90b0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52  ine SQLITE_INTER
90c0: 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f  RUPT    9   /* O
90d0: 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61  peration termina
90e0: 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69  ted by sqlite3_i
90f0: 6e 74 65 72 72 75 70 74 28 29 2a 2f 0d 0a 23 64  nterrupt()*/..#d
9100: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9110: 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a  RR       10   /*
9120: 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69   Some kind of di
9130: 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  sk I/O error occ
9140: 75 72 72 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e  urred */..#defin
9150: 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  e SQLITE_CORRUPT
9160: 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65       11   /* The
9170: 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69   database disk i
9180: 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65  mage is malforme
9190: 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  d */..#define SQ
91a0: 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20  LITE_NOTFOUND   
91b0: 20 31 32 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e   12   /* Unknown
91c0: 20 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74   opcode in sqlit
91d0: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
91e0: 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  ) */..#define SQ
91f0: 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20  LITE_FULL       
9200: 20 31 33 20 20 20 2f 2a 20 49 6e 73 65 72 74 69   13   /* Inserti
9210: 6f 6e 20 66 61 69 6c 65 64 20 62 65 63 61 75 73  on failed becaus
9220: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 66 75  e database is fu
9230: 6c 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ll */..#define S
9240: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20  QLITE_CANTOPEN  
9250: 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65    14   /* Unable
9260: 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74   to open the dat
9270: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 23  abase file */..#
9280: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52  define SQLITE_PR
9290: 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 20 2f  OTOCOL    15   /
92a0: 2a 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20  * Database lock 
92b0: 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a  protocol error *
92c0: 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
92d0: 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36  E_EMPTY       16
92e0: 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
92f0: 73 20 65 6d 70 74 79 20 2a 2f 0d 0a 23 64 65 66  s empty */..#def
9300: 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  ine SQLITE_SCHEM
9310: 41 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54  A      17   /* T
9320: 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
9330: 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f 0d 0a 23  ma changed */..#
9340: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f  define SQLITE_TO
9350: 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 20 2f  OBIG      18   /
9360: 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
9370: 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 6c 69   exceeds size li
9380: 6d 69 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  mit */..#define 
9390: 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
93a0: 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 74  T  19   /* Abort
93b0: 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 69   due to constrai
93c0: 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0d  nt violation */.
93d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
93e0: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20  MISMATCH    20  
93f0: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69   /* Data type mi
9400: 73 6d 61 74 63 68 20 2a 2f 0d 0a 23 64 65 66 69  smatch */..#defi
9410: 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  ne SQLITE_MISUSE
9420: 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69        21   /* Li
9430: 62 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72  brary used incor
9440: 72 65 63 74 6c 79 20 2a 2f 0d 0a 23 64 65 66 69  rectly */..#defi
9450: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20  ne SQLITE_NOLFS 
9460: 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73        22   /* Us
9470: 65 73 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e  es OS features n
9480: 6f 74 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20  ot supported on 
9490: 68 6f 73 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  host */..#define
94a0: 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20   SQLITE_AUTH    
94b0: 20 20 20 20 32 33 20 20 20 2f 2a 20 41 75 74 68      23   /* Auth
94c0: 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64  orization denied
94d0: 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
94e0: 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20  ITE_FORMAT      
94f0: 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72  24   /* Auxiliar
9500: 79 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61  y database forma
9510: 74 20 65 72 72 6f 72 20 2a 2f 0d 0a 23 64 65 66  t error */..#def
9520: 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e 47 45  ine SQLITE_RANGE
9530: 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 32         25   /* 2
9540: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
9550: 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f 75 74  sqlite3_bind out
9560: 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0d 0a 23 64   of range */..#d
9570: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54  efine SQLITE_NOT
9580: 41 44 42 20 20 20 20 20 20 32 36 20 20 20 2f 2a  ADB      26   /*
9590: 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 61   File opened tha
95a0: 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62  t is not a datab
95b0: 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 23 64 65  ase file */..#de
95c0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 20  fine SQLITE_ROW 
95d0: 20 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 20          100  /* 
95e0: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68  sqlite3_step() h
95f0: 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 72  as another row r
9600: 65 61 64 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  eady */..#define
9610: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20   SQLITE_DONE    
9620: 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69      101  /* sqli
9630: 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 66  te3_step() has f
9640: 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e  inished executin
9650: 67 20 2a 2f 0d 0a 2f 2a 20 65 6e 64 2d 6f 66 2d  g */../* end-of-
9660: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0d 0a  error-codes */..
9670: 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
9680: 46 3a 20 45 78 74 65 6e 64 65 64 20 52 65 73 75  F: Extended Resu
9690: 6c 74 20 43 6f 64 65 73 0d 0a 2a 2a 20 4b 45 59  lt Codes..** KEY
96a0: 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64  WORDS: {extended
96b0: 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78   error code} {ex
96c0: 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
96d0: 65 73 7d 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  es}..** KEYWORDS
96e0: 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75  : {extended resu
96f0: 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64  lt code} {extend
9700: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 7d  ed result codes}
9710: 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 69 74 73 20  ..**..** In its 
9720: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72  default configur
9730: 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50  ation, SQLite AP
9740: 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  I routines retur
9750: 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65  n one of 26 inte
9760: 67 65 72 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ger..** [SQLITE_
9770: 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65  OK | result code
9780: 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78  s].  However, ex
9790: 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f  perience has sho
97a0: 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 0d  wn that many of.
97b0: 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74  .** these result
97c0: 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63   codes are too c
97d0: 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20  oarse-grained.  
97e0: 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76  They do not prov
97f0: 69 64 65 20 61 73 0d 0a 2a 2a 20 6d 75 63 68 20  ide as..** much 
9800: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
9810: 74 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72  t problems as pr
9820: 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20  ogrammers might 
9830: 6c 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66  like.  In an eff
9840: 6f 72 74 20 74 6f 0d 0a 2a 2a 20 61 64 64 72 65  ort to..** addre
9850: 73 73 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76  ss this, newer v
9860: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
9870: 65 20 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38  e (version 3.3.8
9880: 20 61 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c   and later) incl
9890: 75 64 65 0d 0a 2a 2a 20 73 75 70 70 6f 72 74 20  ude..** support 
98a0: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 72  for additional r
98b0: 65 73 75 6c 74 20 63 6f 64 65 73 20 74 68 61 74  esult codes that
98c0: 20 70 72 6f 76 69 64 65 20 6d 6f 72 65 20 64 65   provide more de
98d0: 74 61 69 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69  tailed informati
98e0: 6f 6e 0d 0a 2a 2a 20 61 62 6f 75 74 20 65 72 72  on..** about err
98f0: 6f 72 73 2e 20 54 68 65 20 65 78 74 65 6e 64 65  ors. The extende
9900: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
9910: 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69  re enabled or di
9920: 73 61 62 6c 65 64 0d 0a 2a 2a 20 6f 6e 20 61 20  sabled..** on a 
9930: 70 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  per database con
9940: 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 20 75 73  nection basis us
9950: 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 5b 73 71 6c  ing the..** [sql
9960: 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
9970: 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50  sult_codes()] AP
9980: 49 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d 65 20  I...**..** Some 
9990: 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  of the available
99a0: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
99b0: 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65   codes are liste
99c0: 64 20 68 65 72 65 2e 0d 0a 2a 2a 20 4f 6e 65 20  d here...** One 
99d0: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e  may expect the n
99e0: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65  umber of extende
99f0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  d result codes w
9a00: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0d 0a 2a  ill be expand..*
9a10: 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f  * over time.  So
9a20: 66 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73  ftware that uses
9a30: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
9a40: 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78   codes should ex
9a50: 70 65 63 74 0d 0a 2a 2a 20 74 6f 20 73 65 65 20  pect..** to see 
9a60: 6e 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73  new result codes
9a70: 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
9a80: 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0d 0a  ses of SQLite...
9a90: 2a 2a 0d 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  **..** The SQLIT
9aa0: 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 65  E_OK result code
9ab0: 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 65   will never be e
9ac0: 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69 6c  xtended.  It wil
9ad0: 6c 20 61 6c 77 61 79 73 0d 0a 2a 2a 20 62 65 20  l always..** be 
9ae0: 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0d 0a 2a  exactly zero...*
9af0: 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
9b00: 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20  E_IOERR_READ    
9b10: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9b20: 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29  E_IOERR | (1<<8)
9b30: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9b40: 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
9b50: 41 44 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  AD        (SQLIT
9b60: 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29  E_IOERR | (2<<8)
9b70: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9b80: 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20  E_IOERR_WRITE   
9b90: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9ba0: 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29  E_IOERR | (3<<8)
9bb0: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9bc0: 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20  E_IOERR_FSYNC   
9bd0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9be0: 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29  E_IOERR | (4<<8)
9bf0: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9c00: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
9c10: 43 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  C         (SQLIT
9c20: 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29  E_IOERR | (5<<8)
9c30: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9c40: 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
9c50: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9c60: 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29  E_IOERR | (6<<8)
9c70: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9c80: 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20  E_IOERR_FSTAT   
9c90: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9ca0: 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29  E_IOERR | (7<<8)
9cb0: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9cc0: 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20  E_IOERR_UNLOCK  
9cd0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9ce0: 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29  E_IOERR | (8<<8)
9cf0: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9d00: 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20  E_IOERR_RDLOCK  
9d10: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9d20: 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29  E_IOERR | (9<<8)
9d30: 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )..#define SQLIT
9d40: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20  E_IOERR_DELETE  
9d50: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9d60: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38  E_IOERR | (10<<8
9d70: 29 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  ))..#define SQLI
9d80: 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
9d90: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
9da0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c  TE_IOERR | (11<<
9db0: 38 29 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  8))..#define SQL
9dc0: 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
9dd0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
9de0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c  ITE_IOERR | (12<
9df0: 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65 20 53 51  <8))..#define SQ
9e00: 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
9e10: 53 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51  S            (SQ
9e20: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 33  LITE_IOERR | (13
9e30: 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65 20 53  <<8))..#define S
9e40: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
9e50: 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 20 28 53  KRESERVEDLOCK (S
9e60: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31  QLITE_IOERR | (1
9e70: 34 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65 20  4<<8))..#define 
9e80: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
9e90: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  K              (
9ea0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
9eb0: 31 35 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65  15<<8))..#define
9ec0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c   SQLITE_IOERR_CL
9ed0: 4f 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  OSE             
9ee0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
9ef0: 28 31 36 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e  (16<<8))..#defin
9f00: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  e SQLITE_IOERR_D
9f10: 49 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20  IR_CLOSE        
9f20: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
9f30: 20 28 31 37 3c 3c 38 29 29 0d 0a 23 64 65 66 69   (17<<8))..#defi
9f40: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
9f50: 53 48 4d 4f 50 45 4e 20 20 20 20 20 20 20 20 20  SHMOPEN         
9f60: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
9f70: 7c 20 28 31 38 3c 3c 38 29 29 0d 0a 23 64 65 66  | (18<<8))..#def
9f80: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9f90: 5f 53 48 4d 53 49 5a 45 20 20 20 20 20 20 20 20  _SHMSIZE        
9fa0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
9fb0: 20 7c 20 28 31 39 3c 3c 38 29 29 0d 0a 23 64 65   | (19<<8))..#de
9fc0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
9fd0: 52 5f 53 48 4d 4c 4f 43 4b 20 20 20 20 20 20 20  R_SHMLOCK       
9fe0: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
9ff0: 52 20 7c 20 28 32 30 3c 3c 38 29 29 0d 0a 23 64  R | (20<<8))..#d
a000: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
a010: 52 52 5f 53 48 4d 4d 41 50 20 20 20 20 20 20 20  RR_SHMMAP       
a020: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
a030: 52 52 20 7c 20 28 32 31 3c 3c 38 29 29 0d 0a 23  RR | (21<<8))..#
a040: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
a050: 45 52 52 5f 53 45 45 4b 20 20 20 20 20 20 20 20  ERR_SEEK        
a060: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
a070: 45 52 52 20 7c 20 28 32 32 3c 3c 38 29 29 0d 0a  ERR | (22<<8))..
a080: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
a090: 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
a0a0: 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4c  E      (SQLITE_L
a0b0: 4f 43 4b 45 44 20 7c 20 20 28 31 3c 3c 38 29 29  OCKED |  (1<<8))
a0c0: 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
a0d0: 5f 42 55 53 59 5f 52 45 43 4f 56 45 52 59 20 20  _BUSY_RECOVERY  
a0e0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
a0f0: 5f 42 55 53 59 20 20 20 7c 20 20 28 31 3c 3c 38  _BUSY   |  (1<<8
a100: 29 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  ))..#define SQLI
a110: 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 4e 4f 54 45  TE_CANTOPEN_NOTE
a120: 4d 50 44 49 52 20 20 20 20 20 20 28 53 51 4c 49  MPDIR      (SQLI
a130: 54 45 5f 43 41 4e 54 4f 50 45 4e 20 7c 20 28 31  TE_CANTOPEN | (1
a140: 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65 20 53  <<8))..#define S
a150: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 56 54  QLITE_CORRUPT_VT
a160: 41 42 20 20 20 20 20 20 20 20 20 20 20 20 28 53  AB            (S
a170: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 7c 20  QLITE_CORRUPT | 
a180: 28 31 3c 3c 38 29 29 0d 0a 23 64 65 66 69 6e 65  (1<<8))..#define
a190: 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
a1a0: 5f 52 45 43 4f 56 45 52 59 20 20 20 20 20 20 20  _RECOVERY       
a1b0: 28 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  (SQLITE_READONLY
a1c0: 20 7c 20 28 31 3c 3c 38 29 29 0d 0a 23 64 65 66   | (1<<8))..#def
a1d0: 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  ine SQLITE_READO
a1e0: 4e 4c 59 5f 43 41 4e 54 4c 4f 43 4b 20 20 20 20  NLY_CANTLOCK    
a1f0: 20 20 20 28 53 51 4c 49 54 45 5f 52 45 41 44 4f     (SQLITE_READO
a200: 4e 4c 59 20 7c 20 28 32 3c 3c 38 29 29 0d 0a 0d  NLY | (2<<8))...
a210: 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
a220: 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65  : Flags For File
a230: 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73   Open Operations
a240: 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 62  ..**..** These b
a250: 69 74 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e  it values are in
a260: 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
a270: 6e 20 74 68 65 0d 0a 2a 2a 20 33 72 64 20 70 61  n the..** 3rd pa
a280: 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b  rameter to the [
a290: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
a2a0: 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64  )] interface and
a2b0: 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68 20  ..** in the 4th 
a2c0: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
a2d0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f   [sqlite3_vfs.xO
a2e0: 70 65 6e 5d 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f  pen] method...*/
a2f0: 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
a300: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20  _OPEN_READONLY  
a310: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30         0x0000000
a320: 31 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c  1  /* Ok for sql
a330: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a  ite3_open_v2() *
a340: 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
a350: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
a360: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30          0x000000
a370: 30 32 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71  02  /* Ok for sq
a380: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
a390: 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
a3a0: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20  TE_OPEN_CREATE  
a3b0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
a3c0: 30 30 34 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  004  /* Ok for s
a3d0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
a3e0: 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
a3f0: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
a400: 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30  NCLOSE    0x0000
a410: 30 30 30 38 20 20 2f 2a 20 56 46 53 20 6f 6e 6c  0008  /* VFS onl
a420: 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  y */..#define SQ
a430: 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
a440: 49 56 45 20 20 20 20 20 20 20 20 30 78 30 30 30  IVE        0x000
a450: 30 30 30 31 30 20 20 2f 2a 20 56 46 53 20 6f 6e  00010  /* VFS on
a460: 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ly */..#define S
a470: 51 4c 49 54 45 5f 4f 50 45 4e 5f 41 55 54 4f 50  QLITE_OPEN_AUTOP
a480: 52 4f 58 59 20 20 20 20 20 20 20 20 30 78 30 30  ROXY        0x00
a490: 30 30 30 30 32 30 20 20 2f 2a 20 56 46 53 20 6f  000020  /* VFS o
a4a0: 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  nly */..#define 
a4b0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 20  SQLITE_OPEN_URI 
a4c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
a4d0: 30 30 30 30 30 34 30 20 20 2f 2a 20 4f 6b 20 66  0000040  /* Ok f
a4e0: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
a4f0: 76 32 28 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  v2() */..#define
a500: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
a510: 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78  N_DB          0x
a520: 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 53  00000100  /* VFS
a530: 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e   only */..#defin
a540: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  e SQLITE_OPEN_TE
a550: 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 30  MP_DB          0
a560: 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46  x00000200  /* VF
a570: 53 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69  S only */..#defi
a580: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
a590: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20  RANSIENT_DB     
a5a0: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56  0x00000400  /* V
a5b0: 46 53 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66  FS only */..#def
a5c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ine SQLITE_OPEN_
a5d0: 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20  MAIN_JOURNAL    
a5e0: 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20   0x00000800  /* 
a5f0: 56 46 53 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65  VFS only */..#de
a600: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e  fine SQLITE_OPEN
a610: 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20  _TEMP_JOURNAL   
a620: 20 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a    0x00001000  /*
a630: 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64   VFS only */..#d
a640: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
a650: 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20  N_SUBJOURNAL    
a660: 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f     0x00002000  /
a670: 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0d 0a 23  * VFS only */..#
a680: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a690: 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
a6a0: 4c 20 20 20 30 78 30 30 30 30 34 30 30 30 20 20  L   0x00004000  
a6b0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0d 0a  /* VFS only */..
a6c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
a6d0: 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 20 20  PEN_NOMUTEX     
a6e0: 20 20 20 20 20 30 78 30 30 30 30 38 30 30 30 20       0x00008000 
a6f0: 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74   /* Ok for sqlit
a700: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0d  e3_open_v2() */.
a710: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
a720: 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 20  OPEN_FULLMUTEX  
a730: 20 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30        0x00010000
a740: 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69    /* Ok for sqli
a750: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f  te3_open_v2() */
a760: 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
a770: 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
a780: 45 20 20 20 20 20 20 30 78 30 30 30 32 30 30 30  E      0x0002000
a790: 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c  0  /* Ok for sql
a7a0: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a  ite3_open_v2() *
a7b0: 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
a7c0: 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41  E_OPEN_PRIVATECA
a7d0: 43 48 45 20 20 20 20 20 30 78 30 30 30 34 30 30  CHE     0x000400
a7e0: 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 71  00  /* Ok for sq
a7f0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
a800: 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
a810: 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 20 20 20 20  TE_OPEN_WAL     
a820: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 38 30           0x00080
a830: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  000  /* VFS only
a840: 20 2a 2f 0d 0a 0d 0a 2f 2a 20 52 65 73 65 72 76   */..../* Reserv
a850: 65 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ed:             
a860: 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
a870: 46 30 30 30 30 30 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  F00000 */..../*.
a880: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
a890: 76 69 63 65 20 43 68 61 72 61 63 74 65 72 69 73  vice Characteris
a8a0: 74 69 63 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  tics..**..** The
a8b0: 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65   xDeviceCharacte
a8c0: 72 69 73 74 69 63 73 20 6d 65 74 68 6f 64 20 6f  ristics method o
a8d0: 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69  f the [sqlite3_i
a8e0: 6f 5f 6d 65 74 68 6f 64 73 5d 0d 0a 2a 2a 20 6f  o_methods]..** o
a8f0: 62 6a 65 63 74 20 72 65 74 75 72 6e 73 20 61 6e  bject returns an
a900: 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
a910: 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 74 68  s a vector of th
a920: 65 20 74 68 65 73 65 0d 0a 2a 2a 20 62 69 74 20  e these..** bit 
a930: 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e  values expressin
a940: 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69  g I/O characteri
a950: 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73  stics of the mas
a960: 73 20 73 74 6f 72 61 67 65 0d 0a 2a 2a 20 64 65  s storage..** de
a970: 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20  vice that holds 
a980: 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68  the file that th
a990: 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  e [sqlite3_io_me
a9a0: 74 68 6f 64 73 5d 0d 0a 2a 2a 20 72 65 66 65 72  thods]..** refer
a9b0: 73 20 74 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  s to...**..** Th
a9c0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
a9d0: 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d  TOMIC property m
a9e0: 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72  eans that all wr
a9f0: 69 74 65 73 20 6f 66 0d 0a 2a 2a 20 61 6e 79 20  ites of..** any 
aa00: 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e  size are atomic.
aa10: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
aa20: 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c  AP_ATOMICnnn val
aa30: 75 65 73 0d 0a 2a 2a 20 6d 65 61 6e 20 74 68 61  ues..** mean tha
aa40: 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63  t writes of bloc
aa50: 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20  ks that are nnn 
aa60: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e  bytes in size an
aa70: 64 0d 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65  d..** are aligne
aa80: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20  d to an address 
aa90: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65  which is an inte
aaa0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0d  ger multiple of.
aab0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d  .** nnn are atom
aac0: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
aad0: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
aae0: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0d 0a 2a  D value means..*
aaf0: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
ab00: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
ab10: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
ab20: 20 69 73 20 61 70 70 65 6e 64 65 64 0d 0a 2a 2a   is appended..**
ab30: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20   first then the 
ab40: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
ab50: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65   is extended, ne
ab60: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0d 0a 2a  ver the other..*
ab70: 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54  * way around.  T
ab80: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
ab90: 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65  SEQUENTIAL prope
aba0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0d 0a  rty means that..
abb0: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
abc0: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73  s written to dis
abd0: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72  k in the same or
abe0: 64 65 72 20 61 73 20 63 61 6c 6c 73 0d 0a 2a 2a  der as calls..**
abf0: 20 74 6f 20 78 57 72 69 74 65 28 29 2e 20 20 54   to xWrite().  T
ac00: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
ac10: 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52  POWERSAFE_OVERWR
ac20: 49 54 45 20 70 72 6f 70 65 72 74 79 20 6d 65 61  ITE property mea
ac30: 6e 73 20 74 68 61 74 0d 0a 2a 2a 20 61 66 74 65  ns that..** afte
ac40: 72 20 72 65 62 6f 6f 74 20 66 6f 6c 6c 6f 77 69  r reboot followi
ac50: 6e 67 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f  ng a crash or po
ac60: 77 65 72 20 6c 6f 73 73 2c 20 74 68 65 20 6f 6e  wer loss, the on
ac70: 6c 79 20 62 79 74 65 73 20 69 6e 20 61 0d 0a 2a  ly bytes in a..*
ac80: 2a 20 66 69 6c 65 20 74 68 61 74 20 77 65 72 65  * file that were
ac90: 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20   written at the 
aca0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 6c 65 76 65  application leve
acb0: 6c 20 6d 69 67 68 74 20 68 61 76 65 20 63 68 61  l might have cha
acc0: 6e 67 65 64 0d 0a 2a 2a 20 61 6e 64 20 74 68 61  nged..** and tha
acd0: 74 20 61 64 6a 61 63 65 6e 74 20 62 79 74 65 73  t adjacent bytes
ace0: 2c 20 65 76 65 6e 20 62 79 74 65 73 20 77 69 74  , even bytes wit
acf0: 68 69 6e 20 74 68 65 20 73 61 6d 65 20 73 65 63  hin the same sec
ad00: 74 6f 72 20 61 72 65 0d 0a 2a 2a 20 67 75 61 72  tor are..** guar
ad10: 61 6e 74 65 65 64 20 74 6f 20 62 65 20 75 6e 63  anteed to be unc
ad20: 68 61 6e 67 65 64 2e 0d 0a 2a 2f 0d 0a 23 64 65  hanged...*/..#de
ad30: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
ad40: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20  P_ATOMIC        
ad50: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
ad60: 30 30 31 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  001..#define SQL
ad70: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ad80: 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20  512             
ad90: 20 30 78 30 30 30 30 30 30 30 32 0d 0a 23 64 65   0x00000002..#de
ada0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
adb0: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20  P_ATOMIC1K      
adc0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
add0: 30 30 34 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  004..#define SQL
ade0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
adf0: 32 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2K              
ae00: 20 30 78 30 30 30 30 30 30 30 38 0d 0a 23 64 65   0x00000008..#de
ae10: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
ae20: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20  P_ATOMIC4K      
ae30: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
ae40: 30 31 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  010..#define SQL
ae50: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ae60: 38 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  8K              
ae70: 20 30 78 30 30 30 30 30 30 32 30 0d 0a 23 64 65   0x00000020..#de
ae80: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
ae90: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20  P_ATOMIC16K     
aea0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
aeb0: 30 34 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  040..#define SQL
aec0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
aed0: 33 32 4b 20 20 20 20 20 20 20 20 20 20 20 20 20  32K             
aee0: 20 30 78 30 30 30 30 30 30 38 30 0d 0a 23 64 65   0x00000080..#de
aef0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
af00: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20  P_ATOMIC64K     
af10: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
af20: 31 30 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  100..#define SQL
af30: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
af40: 50 50 45 4e 44 20 20 20 20 20 20 20 20 20 20 20  PPEND           
af50: 20 30 78 30 30 30 30 30 32 30 30 0d 0a 23 64 65   0x00000200..#de
af60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
af70: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20  P_SEQUENTIAL    
af80: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
af90: 34 30 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  400..#define SQL
afa0: 49 54 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45  ITE_IOCAP_UNDELE
afb0: 54 41 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20  TABLE_WHEN_OPEN 
afc0: 20 30 78 30 30 30 30 30 38 30 30 0d 0a 23 64 65   0x00000800..#de
afd0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
afe0: 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52  P_POWERSAFE_OVER
aff0: 57 52 49 54 45 20 20 20 20 30 78 30 30 30 30 31  WRITE    0x00001
b000: 30 30 30 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  000..../*..** CA
b010: 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63  PI3REF: File Loc
b020: 6b 69 6e 67 20 4c 65 76 65 6c 73 0d 0a 2a 2a 0d  king Levels..**.
b030: 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20  .** SQLite uses 
b040: 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74  one of these int
b050: 65 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74  eger values as t
b060: 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a 20 61 72  he second..** ar
b070: 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20  gument to calls 
b080: 69 74 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20  it makes to the 
b090: 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c  xLock() and xUnl
b0a0: 6f 63 6b 28 29 20 6d 65 74 68 6f 64 73 0d 0a 2a  ock() methods..*
b0b0: 2a 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33  * of an [sqlite3
b0c0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
b0d0: 65 63 74 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  ect...*/..#defin
b0e0: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f  e SQLITE_LOCK_NO
b0f0: 4e 45 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23  NE          0..#
b100: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
b110: 43 4b 5f 53 48 41 52 45 44 20 20 20 20 20 20 20  CK_SHARED       
b120: 20 31 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   1..#define SQLI
b130: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44  TE_LOCK_RESERVED
b140: 20 20 20 20 20 20 32 0d 0a 23 64 65 66 69 6e 65        2..#define
b150: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e   SQLITE_LOCK_PEN
b160: 44 49 4e 47 20 20 20 20 20 20 20 33 0d 0a 23 64  DING       3..#d
b170: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
b180: 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  K_EXCLUSIVE     
b190: 34 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  4..../*..** CAPI
b1a0: 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a  3REF: Synchroniz
b1b0: 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73  ation Type Flags
b1c0: 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 53 51  ..**..** When SQ
b1d0: 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65  Lite invokes the
b1e0: 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20   xSync() method 
b1f0: 6f 66 20 61 6e 0d 0a 2a 2a 20 5b 73 71 6c 69 74  of an..** [sqlit
b200: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
b210: 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 20  bject it uses a 
b220: 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0d 0a  combination of..
b230: 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  ** these integer
b240: 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73   values as the s
b250: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0d  econd argument..
b260: 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 65  .**..** When the
b270: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
b280: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73  AONLY flag is us
b290: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
b2a0: 74 20 74 68 65 0d 0a 2a 2a 20 73 79 6e 63 20 6f  t the..** sync o
b2b0: 70 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65  peration only ne
b2c0: 65 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74  eds to flush dat
b2d0: 61 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67  a to mass storag
b2e0: 65 2e 20 20 49 6e 6f 64 65 0d 0a 2a 2a 20 69 6e  e.  Inode..** in
b2f0: 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e  formation need n
b300: 6f 74 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49  ot be flushed. I
b310: 66 20 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72  f the lower four
b320: 20 62 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61   bits of the fla
b330: 67 0d 0a 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49  g..** equal SQLI
b340: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20  TE_SYNC_NORMAL, 
b350: 74 68 61 74 20 6d 65 61 6e 73 20 74 6f 20 75 73  that means to us
b360: 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29  e normal fsync()
b370: 20 73 65 6d 61 6e 74 69 63 73 2e 0d 0a 2a 2a 20   semantics...** 
b380: 49 66 20 74 68 65 20 6c 6f 77 65 72 20 66 6f 75  If the lower fou
b390: 72 20 62 69 74 73 20 65 71 75 61 6c 20 53 51 4c  r bits equal SQL
b3a0: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 2c 20 74  ITE_SYNC_FULL, t
b3b0: 68 61 74 20 6d 65 61 6e 73 0d 0a 2a 2a 20 74 6f  hat means..** to
b3c0: 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20 73 74   use Mac OS X st
b3d0: 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 6e 73  yle fullsync ins
b3e0: 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 29 2e  tead of fsync().
b3f0: 0d 0a 2a 2a 0d 0a 2a 2a 20 44 6f 20 6e 6f 74 20  ..**..** Do not 
b400: 63 6f 6e 66 75 73 65 20 74 68 65 20 53 51 4c 49  confuse the SQLI
b410: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 61  TE_SYNC_NORMAL a
b420: 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  nd SQLITE_SYNC_F
b430: 55 4c 4c 20 66 6c 61 67 73 0d 0a 2a 2a 20 77 69  ULL flags..** wi
b440: 74 68 20 74 68 65 20 5b 50 52 41 47 4d 41 20 73  th the [PRAGMA s
b450: 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 4e 4f 52 4d  ynchronous]=NORM
b460: 41 4c 20 61 6e 64 20 5b 50 52 41 47 4d 41 20 73  AL and [PRAGMA s
b470: 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 46 55 4c 4c  ynchronous]=FULL
b480: 0d 0a 2a 2a 20 73 65 74 74 69 6e 67 73 2e 20 20  ..** settings.  
b490: 54 68 65 20 5b 73 79 6e 63 68 72 6f 6e 6f 75 73  The [synchronous
b4a0: 20 70 72 61 67 6d 61 5d 20 64 65 74 65 72 6d 69   pragma] determi
b4b0: 6e 65 73 20 77 68 65 6e 20 63 61 6c 6c 73 20 74  nes when calls t
b4c0: 6f 20 74 68 65 0d 0a 2a 2a 20 78 53 79 6e 63 20  o the..** xSync 
b4d0: 56 46 53 20 6d 65 74 68 6f 64 20 6f 63 63 75 72  VFS method occur
b4e0: 20 61 6e 64 20 61 70 70 6c 69 65 73 20 75 6e 69   and applies uni
b4f0: 66 6f 72 6d 6c 79 20 61 63 72 6f 73 73 20 61 6c  formly across al
b500: 6c 20 70 6c 61 74 66 6f 72 6d 73 2e 0d 0a 2a 2a  l platforms...**
b510: 20 54 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43   The SQLITE_SYNC
b520: 5f 4e 4f 52 4d 41 4c 20 61 6e 64 20 53 51 4c 49  _NORMAL and SQLI
b530: 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61  TE_SYNC_FULL fla
b540: 67 73 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77  gs determine how
b550: 0d 0a 2a 2a 20 65 6e 65 72 67 65 74 69 63 20 6f  ..** energetic o
b560: 72 20 72 69 67 6f 72 6f 75 73 20 6f 72 20 66 6f  r rigorous or fo
b570: 72 63 65 66 75 6c 20 74 68 65 20 73 79 6e 63 20  rceful the sync 
b580: 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61  operations are a
b590: 6e 64 0d 0a 2a 2a 20 6f 6e 6c 79 20 6d 61 6b 65  nd..** only make
b5a0: 20 61 20 64 69 66 66 65 72 65 6e 63 65 20 6f 6e   a difference on
b5b0: 20 4d 61 63 20 4f 53 58 20 66 6f 72 20 74 68 65   Mac OSX for the
b5c0: 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20   default SQLite 
b5d0: 63 6f 64 65 2e 0d 0a 2a 2a 20 28 54 68 69 72 64  code...** (Third
b5e0: 2d 70 61 72 74 79 20 56 46 53 20 69 6d 70 6c 65  -party VFS imple
b5f0: 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 69 67 68 74  mentations might
b600: 20 61 6c 73 6f 20 6d 61 6b 65 20 74 68 65 20 64   also make the d
b610: 69 73 74 69 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 62  istinction..** b
b620: 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 53 59  etween SQLITE_SY
b630: 4e 43 5f 4e 4f 52 4d 41 4c 20 61 6e 64 20 53 51  NC_NORMAL and SQ
b640: 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 2c 20  LITE_SYNC_FULL, 
b650: 62 75 74 20 61 6d 6f 6e 67 20 74 68 65 0d 0a 2a  but among the..*
b660: 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
b670: 65 6d 73 20 6e 61 74 69 76 65 6c 79 20 73 75 70  ems natively sup
b680: 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65  ported by SQLite
b690: 2c 20 6f 6e 6c 79 20 4d 61 63 20 4f 53 58 0d 0a  , only Mac OSX..
b6a0: 2a 2a 20 63 61 72 65 73 20 61 62 6f 75 74 20 74  ** cares about t
b6b0: 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e 29 0d  he difference.).
b6c0: 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  .*/..#define SQL
b6d0: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20  ITE_SYNC_NORMAL 
b6e0: 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0d 0a         0x00002..
b6f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
b700: 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20  YNC_FULL        
b710: 20 20 30 78 30 30 30 30 33 0d 0a 23 64 65 66 69    0x00003..#defi
b720: 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44  ne SQLITE_SYNC_D
b730: 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30  ATAONLY      0x0
b740: 30 30 31 30 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  0010..../*..** C
b750: 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65  API3REF: OS Inte
b760: 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20  rface Open File 
b770: 48 61 6e 64 6c 65 0d 0a 2a 2a 0d 0a 2a 2a 20 41  Handle..**..** A
b780: 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  n [sqlite3_file]
b790: 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
b7a0: 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  ts an open file 
b7b0: 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 5b 73 71 6c  in the ..** [sql
b7c0: 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 20 69 6e  ite3_vfs | OS in
b7d0: 74 65 72 66 61 63 65 20 6c 61 79 65 72 5d 2e 20  terface layer]. 
b7e0: 20 49 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 69   Individual OS i
b7f0: 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20 69 6d 70  nterface..** imp
b800: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
b810: 6c 0d 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75  l..** want to su
b820: 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65  bclass this obje
b830: 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20  ct by appending 
b840: 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
b850: 73 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20  s..** for their 
b860: 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d  own use.  The pM
b870: 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20  ethods entry is 
b880: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0d  a pointer to an.
b890: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  .** [sqlite3_io_
b8a0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
b8b0: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74  that defines met
b8c0: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  hods for perform
b8d0: 69 6e 67 0d 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72  ing..** I/O oper
b8e0: 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70  ations on the op
b8f0: 65 6e 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 74 79  en file...*/..ty
b900: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
b910: 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65  ite3_file sqlite
b920: 33 5f 66 69 6c 65 3b 0d 0a 73 74 72 75 63 74 20  3_file;..struct 
b930: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0d 0a  sqlite3_file {..
b940: 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73    const struct s
b950: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
b960: 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a  s *pMethods;  /*
b970: 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20   Methods for an 
b980: 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0d 0a 7d 3b  open file */..};
b990: 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
b9a0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
b9b0: 65 20 46 69 6c 65 20 56 69 72 74 75 61 6c 20 4d  e File Virtual M
b9c0: 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 0d 0a 2a  ethods Object..*
b9d0: 2a 0d 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65  *..** Every file
b9e0: 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b   opened by the [
b9f0: 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
ba00: 6e 5d 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61  n] method popula
ba10: 74 65 73 20 61 6e 0d 0a 2a 2a 20 5b 73 71 6c 69  tes an..** [sqli
ba20: 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74  te3_file] object
ba30: 20 28 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f   (or, more commo
ba40: 6e 6c 79 2c 20 61 20 73 75 62 63 6c 61 73 73 20  nly, a subclass 
ba50: 6f 66 20 74 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69  of the..** [sqli
ba60: 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74  te3_file] object
ba70: 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  ) with a pointer
ba80: 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
ba90: 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 0d  of this object..
baa0: 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20  .** This object 
bab0: 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 74 68  defines the meth
bac0: 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72 66  ods used to perf
bad0: 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65 72  orm various oper
bae0: 61 74 69 6f 6e 73 0d 0a 2a 2a 20 61 67 61 69 6e  ations..** again
baf0: 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  st the open file
bb00: 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
bb10: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  the [sqlite3_fil
bb20: 65 5d 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a  e] object...**..
bb30: 2a 2a 20 49 66 20 74 68 65 20 5b 73 71 6c 69 74  ** If the [sqlit
bb40: 65 33 5f 76 66 73 2e 78 4f 70 65 6e 5d 20 6d 65  e3_vfs.xOpen] me
bb50: 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 73 71  thod sets the sq
bb60: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
bb70: 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0d 0a 2a 2a  ods element ..**
bb80: 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70   to a non-NULL p
bb90: 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 65  ointer, then the
bba0: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
bbb0: 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 6f  ods.xClose metho
bbc0: 64 0d 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76  d..** may be inv
bbd0: 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65  oked even if the
bbe0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f   [sqlite3_vfs.xO
bbf0: 70 65 6e 5d 20 72 65 70 6f 72 74 65 64 20 74 68  pen] reported th
bc00: 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 20 54  at it failed.  T
bc10: 68 65 0d 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 20  he..** only way 
bc20: 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 6c  to prevent a cal
bc30: 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c 6c  l to xClose foll
bc40: 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 5b  owing a failed [
bc50: 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
bc60: 6e 5d 0d 0a 2a 2a 20 69 73 20 66 6f 72 20 74 68  n]..** is for th
bc70: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78  e [sqlite3_vfs.x
bc80: 4f 70 65 6e 5d 20 74 6f 20 73 65 74 20 74 68 65  Open] to set the
bc90: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
bca0: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 0d 0a  ethods element..
bcb0: 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d  ** to NULL...**.
bcc0: 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72  .** The flags ar
bcd0: 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20  gument to xSync 
bce0: 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53  may be one of [S
bcf0: 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41  QLITE_SYNC_NORMA
bd00: 4c 5d 20 6f 72 0d 0a 2a 2a 20 5b 53 51 4c 49 54  L] or..** [SQLIT
bd10: 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54  E_SYNC_FULL].  T
bd20: 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65 20  he first choice 
bd30: 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73  is the normal fs
bd40: 79 6e 63 28 29 2e 0d 0a 2a 2a 20 54 68 65 20 73  ync()...** The s
bd50: 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20  econd choice is 
bd60: 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65  a Mac OS X style
bd70: 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20   fullsync.  The 
bd80: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  [SQLITE_SYNC_DAT
bd90: 41 4f 4e 4c 59 5d 0d 0a 2a 2a 20 66 6c 61 67 20  AONLY]..** flag 
bda0: 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74  may be ORed in t
bdb0: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
bdc0: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66  only the data of
bdd0: 20 74 68 65 20 66 69 6c 65 0d 0a 2a 2a 20 61 6e   the file..** an
bde0: 64 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20  d not its inode 
bdf0: 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63  needs to be sync
be00: 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
be10: 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74  integer values t
be20: 6f 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55  o xLock() and xU
be30: 6e 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65 20  nlock() are one 
be40: 6f 66 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20  of..** <ul>..** 
be50: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
be60: 4b 5f 4e 4f 4e 45 5d 2c 0d 0a 2a 2a 20 3c 6c 69  K_NONE],..** <li
be70: 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53  > [SQLITE_LOCK_S
be80: 48 41 52 45 44 5d 2c 0d 0a 2a 2a 20 3c 6c 69 3e  HARED],..** <li>
be90: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45   [SQLITE_LOCK_RE
bea0: 53 45 52 56 45 44 5d 2c 0d 0a 2a 2a 20 3c 6c 69  SERVED],..** <li
beb0: 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50  > [SQLITE_LOCK_P
bec0: 45 4e 44 49 4e 47 5d 2c 20 6f 72 0d 0a 2a 2a 20  ENDING], or..** 
bed0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
bee0: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0d 0a 2a  K_EXCLUSIVE]...*
bef0: 2a 20 3c 2f 75 6c 3e 0d 0a 2a 2a 20 78 4c 6f 63  * </ul>..** xLoc
bf00: 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 68  k() increases th
bf10: 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28  e lock. xUnlock(
bf20: 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65 20  ) decreases the 
bf30: 6c 6f 63 6b 2e 0d 0a 2a 2a 20 54 68 65 20 78 43  lock...** The xC
bf40: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
bf50: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73  () method checks
bf60: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74   whether any dat
bf70: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
bf80: 2c 0d 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20  ,..** either in 
bf90: 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20  this process or 
bfa0: 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72  in some other pr
bfb0: 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e  ocess, is holdin
bfc0: 67 20 61 20 52 45 53 45 52 56 45 44 2c 0d 0a 2a  g a RESERVED,..*
bfd0: 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58  * PENDING, or EX
bfe0: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
bff0: 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65  the file.  It re
c000: 74 75 72 6e 73 20 74 72 75 65 0d 0a 2a 2a 20 69  turns true..** i
c010: 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 78  f such a lock ex
c020: 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 6f  ists and false o
c030: 74 68 65 72 77 69 73 65 2e 0d 0a 2a 2a 0d 0a 2a  therwise...**..*
c040: 2a 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  * The xFileContr
c050: 6f 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61  ol() method is a
c060: 20 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61   generic interfa
c070: 63 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63  ce that allows c
c080: 75 73 74 6f 6d 0d 0a 2a 2a 20 56 46 53 20 69 6d  ustom..** VFS im
c090: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f  plementations to
c0a0: 20 64 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f   directly contro
c0b0: 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75  l an open file u
c0c0: 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 5b 73 71  sing the..** [sq
c0d0: 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
c0e0: 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  ol()] interface.
c0f0: 20 20 54 68 65 20 73 65 63 6f 6e 64 20 22 6f 70    The second "op
c100: 22 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  " argument is an
c110: 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 63  ..** integer opc
c120: 6f 64 65 2e 20 20 54 68 65 20 74 68 69 72 64 20  ode.  The third 
c130: 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65  argument is a ge
c140: 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 69 6e  neric pointer in
c150: 74 65 6e 64 65 64 20 74 6f 0d 0a 2a 2a 20 70 6f  tended to..** po
c160: 69 6e 74 20 74 6f 20 61 20 73 74 72 75 63 74 75  int to a structu
c170: 72 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74  re that may cont
c180: 61 69 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  ain arguments or
c190: 20 73 70 61 63 65 20 69 6e 20 77 68 69 63 68 20   space in which 
c1a0: 74 6f 0d 0a 2a 2a 20 77 72 69 74 65 20 72 65 74  to..** write ret
c1b0: 75 72 6e 20 76 61 6c 75 65 73 2e 20 20 50 6f 74  urn values.  Pot
c1c0: 65 6e 74 69 61 6c 20 75 73 65 73 20 66 6f 72 20  ential uses for 
c1d0: 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d  xFileControl() m
c1e0: 69 67 68 74 20 62 65 0d 0a 2a 2a 20 66 75 6e 63  ight be..** func
c1f0: 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 6c 65 20  tions to enable 
c200: 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73 20 77  blocking locks w
c210: 69 74 68 20 74 69 6d 65 6f 75 74 73 2c 20 74 6f  ith timeouts, to
c220: 20 63 68 61 6e 67 65 20 74 68 65 0d 0a 2a 2a 20   change the..** 
c230: 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
c240: 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 6f   (for example to
c250: 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f   use dot-file lo
c260: 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 72 65  cks), to inquire
c270: 0d 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 73  ..** about the s
c280: 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b 2c  tatus of a lock,
c290: 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 61   or to break sta
c2a0: 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 53  le locks.  The S
c2b0: 51 4c 69 74 65 0d 0a 2a 2a 20 63 6f 72 65 20 72  QLite..** core r
c2c0: 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f  eserves all opco
c2d0: 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30  des less than 10
c2e0: 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73  0 for its own us
c2f0: 65 2e 0d 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45  e...** A [SQLITE
c300: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
c310: 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64   | list of opcod
c320: 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30  es] less than 10
c330: 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0d  0 is available..
c340: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
c350: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63   that define a c
c360: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72  ustom xFileContr
c370: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64  ol method should
c380: 20 75 73 65 20 6f 70 63 6f 64 65 73 0d 0a 2a 2a   use opcodes..**
c390: 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30   greater than 10
c3a0: 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c  0 to avoid confl
c3b0: 69 63 74 73 2e 20 20 56 46 53 20 69 6d 70 6c 65  icts.  VFS imple
c3c0: 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c  mentations shoul
c3d0: 64 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51  d..** return [SQ
c3e0: 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 5d 20 66  LITE_NOTFOUND] f
c3f0: 6f 72 20 66 69 6c 65 20 63 6f 6e 74 72 6f 6c 20  or file control 
c400: 6f 70 63 6f 64 65 73 20 74 68 61 74 20 74 68 65  opcodes that the
c410: 79 20 64 6f 20 6e 6f 74 0d 0a 2a 2a 20 72 65 63  y do not..** rec
c420: 6f 67 6e 69 7a 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ognize...**..** 
c430: 54 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28  The xSectorSize(
c440: 29 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  ) method returns
c450: 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
c460: 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 64 65 76 69   of the..** devi
c470: 63 65 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65  ce that underlie
c480: 73 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  s the file.  The
c490: 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20   sector size is 
c4a0: 74 68 65 0d 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20  the..** minimum 
c4b0: 77 72 69 74 65 20 74 68 61 74 20 63 61 6e 20 62  write that can b
c4c0: 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68  e performed with
c4d0: 6f 75 74 20 64 69 73 74 75 72 62 69 6e 67 0d 0a  out disturbing..
c4e0: 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 69  ** other bytes i
c4f0: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  n the file.  The
c500: 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65   xDeviceCharacte
c510: 72 69 73 74 69 63 73 28 29 0d 0a 2a 2a 20 6d 65  ristics()..** me
c520: 74 68 6f 64 20 72 65 74 75 72 6e 73 20 61 20 62  thod returns a b
c530: 69 74 20 76 65 63 74 6f 72 20 64 65 73 63 72 69  it vector descri
c540: 62 69 6e 67 20 62 65 68 61 76 69 6f 72 73 20 6f  bing behaviors o
c550: 66 20 74 68 65 0d 0a 2a 2a 20 75 6e 64 65 72 6c  f the..** underl
c560: 79 69 6e 67 20 64 65 76 69 63 65 3a 0d 0a 2a 2a  ying device:..**
c570: 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c  ..** <ul>..** <l
c580: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
c590: 5f 41 54 4f 4d 49 43 5d 0d 0a 2a 2a 20 3c 6c 69  _ATOMIC]..** <li
c5a0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
c5b0: 41 54 4f 4d 49 43 35 31 32 5d 0d 0a 2a 2a 20 3c  ATOMIC512]..** <
c5c0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
c5d0: 50 5f 41 54 4f 4d 49 43 31 4b 5d 0d 0a 2a 2a 20  P_ATOMIC1K]..** 
c5e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
c5f0: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0d 0a 2a 2a  AP_ATOMIC2K]..**
c600: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
c610: 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0d 0a 2a  CAP_ATOMIC4K]..*
c620: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
c630: 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0d 0a  OCAP_ATOMIC8K]..
c640: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c650: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d  IOCAP_ATOMIC16K]
c660: 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ..** <li> [SQLIT
c670: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32  E_IOCAP_ATOMIC32
c680: 4b 5d 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  K]..** <li> [SQL
c690: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
c6a0: 36 34 4b 5d 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  64K]..** <li> [S
c6b0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
c6c0: 5f 41 50 50 45 4e 44 5d 0d 0a 2a 2a 20 3c 6c 69  _APPEND]..** <li
c6d0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
c6e0: 53 45 51 55 45 4e 54 49 41 4c 5d 0d 0a 2a 2a 20  SEQUENTIAL]..** 
c6f0: 3c 2f 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  </ul>..**..** Th
c700: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
c710: 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d  TOMIC property m
c720: 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72  eans that all wr
c730: 69 74 65 73 20 6f 66 0d 0a 2a 2a 20 61 6e 79 20  ites of..** any 
c740: 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e  size are atomic.
c750: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
c760: 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c  AP_ATOMICnnn val
c770: 75 65 73 0d 0a 2a 2a 20 6d 65 61 6e 20 74 68 61  ues..** mean tha
c780: 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63  t writes of bloc
c790: 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20  ks that are nnn 
c7a0: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e  bytes in size an
c7b0: 64 0d 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65  d..** are aligne
c7c0: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20  d to an address 
c7d0: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65  which is an inte
c7e0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0d  ger multiple of.
c7f0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d  .** nnn are atom
c800: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
c810: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
c820: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0d 0a 2a  D value means..*
c830: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
c840: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
c850: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
c860: 20 69 73 20 61 70 70 65 6e 64 65 64 0d 0a 2a 2a   is appended..**
c870: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20   first then the 
c880: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
c890: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65   is extended, ne
c8a0: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0d 0a 2a  ver the other..*
c8b0: 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54  * way around.  T
c8c0: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
c8d0: 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65  SEQUENTIAL prope
c8e0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0d 0a  rty means that..
c8f0: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
c900: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73  s written to dis
c910: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72  k in the same or
c920: 64 65 72 20 61 73 20 63 61 6c 6c 73 0d 0a 2a 2a  der as calls..**
c930: 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0d 0a 2a   to xWrite()...*
c940: 2a 0d 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29  *..** If xRead()
c950: 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
c960: 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
c970: 20 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69   it must also fi
c980: 6c 6c 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e  ll..** in the un
c990: 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66  read portions of
c9a0: 20 74 68 65 20 62 75 66 66 65 72 20 77 69 74 68   the buffer with
c9b0: 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74   zeros.  A VFS t
c9c0: 68 61 74 0d 0a 2a 2a 20 66 61 69 6c 73 20 74 6f  hat..** fails to
c9d0: 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74   zero-fill short
c9e0: 20 72 65 61 64 73 20 6d 69 67 68 74 20 73 65 65   reads might see
c9f0: 6d 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65  m to work.  Howe
ca00: 76 65 72 2c 0d 0a 2a 2a 20 66 61 69 6c 75 72 65  ver,..** failure
ca10: 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68   to zero-fill sh
ca20: 6f 72 74 20 72 65 61 64 73 20 77 69 6c 6c 20 65  ort reads will e
ca30: 76 65 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 74  ventually lead t
ca40: 6f 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  o..** database c
ca50: 6f 72 72 75 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  orruption...*/..
ca60: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
ca70: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
ca80: 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  s sqlite3_io_met
ca90: 68 6f 64 73 3b 0d 0a 73 74 72 75 63 74 20 73 71  hods;..struct sq
caa0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
cab0: 20 7b 0d 0a 20 20 69 6e 74 20 69 56 65 72 73 69   {..  int iVersi
cac0: 6f 6e 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 43 6c  on;..  int (*xCl
cad0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose)(sqlite3_fil
cae0: 65 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 52  e*);..  int (*xR
caf0: 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ead)(sqlite3_fil
cb00: 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69  e*, void*, int i
cb10: 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  Amt, sqlite3_int
cb20: 36 34 20 69 4f 66 73 74 29 3b 0d 0a 20 20 69 6e  64 iOfst);..  in
cb30: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69  t (*xWrite)(sqli
cb40: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74  te3_file*, const
cb50: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74   void*, int iAmt
cb60: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
cb70: 69 4f 66 73 74 29 3b 0d 0a 20 20 69 6e 74 20 28  iOfst);..  int (
cb80: 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69  *xTruncate)(sqli
cb90: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74  te3_file*, sqlit
cba0: 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0d  e3_int64 size);.
cbb0: 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28  .  int (*xSync)(
cbc0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
cbd0: 6e 74 20 66 6c 61 67 73 29 3b 0d 0a 20 20 69 6e  nt flags);..  in
cbe0: 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 73  t (*xFileSize)(s
cbf0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71  qlite3_file*, sq
cc00: 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
cc10: 7a 65 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 4c  ze);..  int (*xL
cc20: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
cc30: 65 2a 2c 20 69 6e 74 29 3b 0d 0a 20 20 69 6e 74  e*, int);..  int
cc40: 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69   (*xUnlock)(sqli
cc50: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
cc60: 0d 0a 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b  ..  int (*xCheck
cc70: 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71  ReservedLock)(sq
cc80: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
cc90: 20 2a 70 52 65 73 4f 75 74 29 3b 0d 0a 20 20 69   *pResOut);..  i
cca0: 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f  nt (*xFileContro
ccb0: 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  l)(sqlite3_file*
ccc0: 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
ccd0: 70 41 72 67 29 3b 0d 0a 20 20 69 6e 74 20 28 2a  pArg);..  int (*
cce0: 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c  xSectorSize)(sql
ccf0: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0d 0a 20 20  ite3_file*);..  
cd00: 69 6e 74 20 28 2a 78 44 65 76 69 63 65 43 68 61  int (*xDeviceCha
cd10: 72 61 63 74 65 72 69 73 74 69 63 73 29 28 73 71  racteristics)(sq
cd20: 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0d 0a 20  lite3_file*);.. 
cd30: 20 2f 2a 20 4d 65 74 68 6f 64 73 20 61 62 6f 76   /* Methods abov
cd40: 65 20 61 72 65 20 76 61 6c 69 64 20 66 6f 72 20  e are valid for 
cd50: 76 65 72 73 69 6f 6e 20 31 20 2a 2f 0d 0a 20 20  version 1 */..  
cd60: 69 6e 74 20 28 2a 78 53 68 6d 4d 61 70 29 28 73  int (*xShmMap)(s
cd70: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
cd80: 74 20 69 50 67 2c 20 69 6e 74 20 70 67 73 7a 2c  t iPg, int pgsz,
cd90: 20 69 6e 74 2c 20 76 6f 69 64 20 76 6f 6c 61 74   int, void volat
cda0: 69 6c 65 2a 2a 29 3b 0d 0a 20 20 69 6e 74 20 28  ile**);..  int (
cdb0: 2a 78 53 68 6d 4c 6f 63 6b 29 28 73 71 6c 69 74  *xShmLock)(sqlit
cdc0: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 66  e3_file*, int of
cdd0: 66 73 65 74 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  fset, int n, int
cde0: 20 66 6c 61 67 73 29 3b 0d 0a 20 20 76 6f 69 64   flags);..  void
cdf0: 20 28 2a 78 53 68 6d 42 61 72 72 69 65 72 29 28   (*xShmBarrier)(
ce00: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0d  sqlite3_file*);.
ce10: 0a 20 20 69 6e 74 20 28 2a 78 53 68 6d 55 6e 6d  .  int (*xShmUnm
ce20: 61 70 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ap)(sqlite3_file
ce30: 2a 2c 20 69 6e 74 20 64 65 6c 65 74 65 46 6c 61  *, int deleteFla
ce40: 67 29 3b 0d 0a 20 20 2f 2a 20 4d 65 74 68 6f 64  g);..  /* Method
ce50: 73 20 61 62 6f 76 65 20 61 72 65 20 76 61 6c 69  s above are vali
ce60: 64 20 66 6f 72 20 76 65 72 73 69 6f 6e 20 32 20  d for version 2 
ce70: 2a 2f 0d 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f  */..  /* Additio
ce80: 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20  nal methods may 
ce90: 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
cea0: 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0d 0a  re releases */..
ceb0: 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  };..../*..** CAP
cec0: 49 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 20  I3REF: Standard 
ced0: 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63  File Control Opc
cee0: 6f 64 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  odes..**..** The
cef0: 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74  se integer const
cf00: 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65 73  ants are opcodes
cf10: 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43 6f   for the xFileCo
cf20: 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0d 0a 2a 2a  ntrol method..**
cf30: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
cf40: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
cf50: 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20  ect and for the 
cf60: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
cf70: 6e 74 72 6f 6c 28 29 5d 0d 0a 2a 2a 20 69 6e 74  ntrol()]..** int
cf80: 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  erface...**..** 
cf90: 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54  The [SQLITE_FCNT
cfa0: 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63  L_LOCKSTATE] opc
cfb0: 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ode is used for 
cfc0: 64 65 62 75 67 67 69 6e 67 2e 20 20 54 68 69 73  debugging.  This
cfd0: 0d 0a 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 73  ..** opcode caus
cfe0: 65 73 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  es the xFileCont
cff0: 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 72  rol method to wr
d000: 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
d010: 73 74 61 74 65 20 6f 66 0d 0a 2a 2a 20 74 68 65  state of..** the
d020: 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53   lock (one of [S
d030: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d  QLITE_LOCK_NONE]
d040: 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53  , [SQLITE_LOCK_S
d050: 48 41 52 45 44 5d 2c 0d 0a 2a 2a 20 5b 53 51 4c  HARED],..** [SQL
d060: 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45  ITE_LOCK_RESERVE
d070: 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  D], [SQLITE_LOCK
d080: 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 20 5b 53  _PENDING], or [S
d090: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55  QLITE_LOCK_EXCLU
d0a0: 53 49 56 45 5d 29 0d 0a 2a 2a 20 69 6e 74 6f 20  SIVE])..** into 
d0b0: 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  an integer that 
d0c0: 74 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e  the pArg argumen
d0d0: 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 54 68 69  t points to. Thi
d0e0: 73 20 63 61 70 61 62 69 6c 69 74 79 0d 0a 2a 2a  s capability..**
d0f0: 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
d100: 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e 6c 79  testing and only
d110: 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 75 70   needs to be sup
d120: 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 4c 49  ported when SQLI
d130: 54 45 5f 54 45 53 54 0d 0a 2a 2a 20 69 73 20 64  TE_TEST..** is d
d140: 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  efined...**..** 
d150: 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54  The [SQLITE_FCNT
d160: 4c 5f 53 49 5a 45 5f 48 49 4e 54 5d 20 6f 70 63  L_SIZE_HINT] opc
d170: 6f 64 65 20 69 73 20 75 73 65 64 20 62 79 20 53  ode is used by S
d180: 51 4c 69 74 65 20 74 6f 20 67 69 76 65 20 74 68  QLite to give th
d190: 65 20 56 46 53 0d 0a 2a 2a 20 6c 61 79 65 72 20  e VFS..** layer 
d1a0: 61 20 68 69 6e 74 20 6f 66 20 68 6f 77 20 6c 61  a hint of how la
d1b0: 72 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65  rge the database
d1c0: 20 66 69 6c 65 20 77 69 6c 6c 20 67 72 6f 77 20   file will grow 
d1d0: 74 6f 20 62 65 20 64 75 72 69 6e 67 20 74 68 65  to be during the
d1e0: 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 72 61  ..** current tra
d1f0: 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  nsaction.  This 
d200: 68 69 6e 74 20 69 73 20 6e 6f 74 20 67 75 61 72  hint is not guar
d210: 61 6e 74 65 65 64 20 74 6f 20 62 65 20 61 63 63  anteed to be acc
d220: 75 72 61 74 65 20 62 75 74 20 69 74 0d 0a 2a 2a  urate but it..**
d230: 20 69 73 20 6f 66 74 65 6e 20 63 6c 6f 73 65 2e   is often close.
d240: 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67    The underlying
d250: 20 56 46 53 20 6d 69 67 68 74 20 63 68 6f 6f 73   VFS might choos
d260: 65 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65  e to preallocate
d270: 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 66 69   database..** fi
d280: 6c 65 20 73 70 61 63 65 20 62 61 73 65 64 20 6f  le space based o
d290: 6e 20 74 68 69 73 20 68 69 6e 74 20 69 6e 20 6f  n this hint in o
d2a0: 72 64 65 72 20 74 6f 20 68 65 6c 70 20 77 72 69  rder to help wri
d2b0: 74 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  tes to the datab
d2c0: 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 20 72 75 6e  ase..** file run
d2d0: 20 66 61 73 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a   faster...**..**
d2e0: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e   The [SQLITE_FCN
d2f0: 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 5d 20 6f  TL_CHUNK_SIZE] o
d300: 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f  pcode is used to
d310: 20 72 65 71 75 65 73 74 20 74 68 61 74 20 74 68   request that th
d320: 65 20 56 46 53 0d 0a 2a 2a 20 65 78 74 65 6e 64  e VFS..** extend
d330: 73 20 61 6e 64 20 74 72 75 6e 63 61 74 65 73 20  s and truncates 
d340: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
d350: 65 20 69 6e 20 63 68 75 6e 6b 73 20 6f 66 20 61  e in chunks of a
d360: 20 73 69 7a 65 20 73 70 65 63 69 66 69 65 64 0d   size specified.
d370: 0a 2a 2a 20 62 79 20 74 68 65 20 75 73 65 72 2e  .** by the user.
d380: 20 54 68 65 20 66 6f 75 72 74 68 20 61 72 67 75   The fourth argu
d390: 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  ment to [sqlite3
d3a0: 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _file_control()]
d3b0: 20 73 68 6f 75 6c 64 20 0d 0a 2a 2a 20 70 6f 69   should ..** poi
d3c0: 6e 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  nt to an integer
d3d0: 20 28 74 79 70 65 20 69 6e 74 29 20 63 6f 6e 74   (type int) cont
d3e0: 61 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 63  aining the new c
d3f0: 68 75 6e 6b 2d 73 69 7a 65 20 74 6f 20 75 73 65  hunk-size to use
d400: 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6e 6f 6d  ..** for the nom
d410: 69 6e 61 74 65 64 20 64 61 74 61 62 61 73 65 2e  inated database.
d420: 20 41 6c 6c 6f 63 61 74 69 6e 67 20 64 61 74 61   Allocating data
d430: 62 61 73 65 20 66 69 6c 65 20 73 70 61 63 65 20  base file space 
d440: 69 6e 20 6c 61 72 67 65 0d 0a 2a 2a 20 63 68 75  in large..** chu
d450: 6e 6b 73 20 28 73 61 79 20 31 4d 42 20 61 74 20  nks (say 1MB at 
d460: 61 20 74 69 6d 65 29 2c 20 6d 61 79 20 72 65 64  a time), may red
d470: 75 63 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  uce file-system 
d480: 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 6e  fragmentation an
d490: 64 0d 0a 2a 2a 20 69 6d 70 72 6f 76 65 20 70 65  d..** improve pe
d4a0: 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 73 6f 6d  rformance on som
d4b0: 65 20 73 79 73 74 65 6d 73 2e 0d 0a 2a 2a 0d 0a  e systems...**..
d4c0: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46  ** The [SQLITE_F
d4d0: 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  CNTL_FILE_POINTE
d4e0: 52 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  R] opcode is use
d4f0: 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 6f  d to obtain a po
d500: 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68 65  inter..** to the
d510: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
d520: 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65  object associate
d530: 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
d540: 6c 61 72 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a  lar database..**
d550: 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 53 65   connection.  Se
d560: 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66  e the [sqlite3_f
d570: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 64  ile_control()] d
d580: 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
d590: 0d 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20  ..** additional 
d5a0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2a  information...**
d5b0: 0d 0a 2a 2a 20 5e 28 54 68 65 20 5b 53 51 4c 49  ..** ^(The [SQLI
d5c0: 54 45 5f 46 43 4e 54 4c 5f 53 59 4e 43 5f 4f 4d  TE_FCNTL_SYNC_OM
d5d0: 49 54 54 45 44 5d 20 6f 70 63 6f 64 65 20 69 73  ITTED] opcode is
d5e0: 20 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 72   generated inter
d5f0: 6e 61 6c 6c 79 20 62 79 0d 0a 2a 2a 20 53 51 4c  nally by..** SQL
d600: 69 74 65 20 61 6e 64 20 73 65 6e 74 20 74 6f 20  ite and sent to 
d610: 61 6c 6c 20 56 46 53 65 73 20 69 6e 20 70 6c 61  all VFSes in pla
d620: 63 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20  ce of a call to 
d630: 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64  the xSync method
d640: 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 64 61  ..** when the da
d650: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
d660: 6e 20 68 61 73 20 5b 50 52 41 47 4d 41 20 73 79  n has [PRAGMA sy
d670: 6e 63 68 72 6f 6e 6f 75 73 5d 20 73 65 74 20 74  nchronous] set t
d680: 6f 20 4f 46 46 2e 29 5e 0d 0a 2a 2a 20 53 6f 6d  o OFF.)^..** Som
d690: 65 20 73 70 65 63 69 61 6c 69 7a 65 64 20 56 46  e specialized VF
d6a0: 53 65 73 20 6e 65 65 64 20 74 68 69 73 20 73 69  Ses need this si
d6b0: 67 6e 61 6c 20 69 6e 20 6f 72 64 65 72 20 74 6f  gnal in order to
d6c0: 20 6f 70 65 72 61 74 65 20 63 6f 72 72 65 63 74   operate correct
d6d0: 6c 79 0d 0a 2a 2a 20 77 68 65 6e 20 5b 50 52 41  ly..** when [PRA
d6e0: 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  GMA synchronous 
d6f0: 7c 20 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f  | PRAGMA synchro
d700: 6e 6f 75 73 3d 4f 46 46 5d 20 69 73 20 73 65 74  nous=OFF] is set
d710: 2c 20 62 75 74 20 6d 6f 73 74 20 0d 0a 2a 2a 20  , but most ..** 
d720: 56 46 53 65 73 20 64 6f 20 6e 6f 74 20 6e 65 65  VFSes do not nee
d730: 64 20 74 68 69 73 20 73 69 67 6e 61 6c 20 61 6e  d this signal an
d740: 64 20 73 68 6f 75 6c 64 20 73 69 6c 65 6e 74 6c  d should silentl
d750: 79 20 69 67 6e 6f 72 65 20 74 68 69 73 20 6f 70  y ignore this op
d760: 63 6f 64 65 2e 0d 0a 2a 2a 20 41 70 70 6c 69 63  code...** Applic
d770: 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f  ations should no
d780: 74 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f  t call [sqlite3_
d790: 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20  file_control()] 
d7a0: 77 69 74 68 20 74 68 69 73 0d 0a 2a 2a 20 6f 70  with this..** op
d7b0: 63 6f 64 65 20 61 73 20 64 6f 69 6e 67 20 73 6f  code as doing so
d7c0: 20 6d 61 79 20 64 69 73 72 75 70 74 20 74 68 65   may disrupt the
d7d0: 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
d7e0: 65 20 73 70 65 63 69 61 6c 69 7a 65 64 20 56 46  e specialized VF
d7f0: 53 65 73 0d 0a 2a 2a 20 74 68 61 74 20 64 6f 20  Ses..** that do 
d800: 72 65 71 75 69 72 65 20 69 74 2e 20 20 0d 0a 2a  require it.  ..*
d810: 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49  *..** ^The [SQLI
d820: 54 45 5f 46 43 4e 54 4c 5f 57 49 4e 33 32 5f 41  TE_FCNTL_WIN32_A
d830: 56 5f 52 45 54 52 59 5d 20 6f 70 63 6f 64 65 20  V_RETRY] opcode 
d840: 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69  is used to confi
d850: 67 75 72 65 20 61 75 74 6f 6d 61 74 69 63 0d 0a  gure automatic..
d860: 2a 2a 20 72 65 74 72 79 20 63 6f 75 6e 74 73 20  ** retry counts 
d870: 61 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 66 6f  and intervals fo
d880: 72 20 63 65 72 74 61 69 6e 20 64 69 73 6b 20 49  r certain disk I
d890: 2f 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f  /O operations fo
d8a0: 72 20 74 68 65 0d 0a 2a 2a 20 77 69 6e 64 6f 77  r the..** window
d8b0: 73 20 5b 56 46 53 5d 20 69 6e 20 6f 72 64 65 72  s [VFS] in order
d8c0: 20 74 6f 20 70 72 6f 76 69 64 65 20 72 6f 62 75   to provide robu
d8d0: 73 74 6e 65 73 73 20 69 6e 20 74 68 65 20 70 72  stness in the pr
d8e0: 65 73 65 6e 63 65 20 6f 66 0d 0a 2a 2a 20 61 6e  esence of..** an
d8f0: 74 69 2d 76 69 72 75 73 20 70 72 6f 67 72 61 6d  ti-virus program
d900: 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 20  s.  By default, 
d910: 74 68 65 20 77 69 6e 64 6f 77 73 20 56 46 53 20  the windows VFS 
d920: 77 69 6c 6c 20 72 65 74 72 79 20 66 69 6c 65 20  will retry file 
d930: 72 65 61 64 2c 0d 0a 2a 2a 20 66 69 6c 65 20 77  read,..** file w
d940: 72 69 74 65 2c 20 61 6e 64 20 66 69 6c 65 20 64  rite, and file d
d950: 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 73  elete operations
d960: 20 75 70 20 74 6f 20 31 30 20 74 69 6d 65 73 2c   up to 10 times,
d970: 20 77 69 74 68 20 61 20 64 65 6c 61 79 0d 0a 2a   with a delay..*
d980: 2a 20 6f 66 20 32 35 20 6d 69 6c 6c 69 73 65 63  * of 25 millisec
d990: 6f 6e 64 73 20 62 65 66 6f 72 65 20 74 68 65 20  onds before the 
d9a0: 66 69 72 73 74 20 72 65 74 72 79 20 61 6e 64 20  first retry and 
d9b0: 77 69 74 68 20 74 68 65 20 64 65 6c 61 79 20 69  with the delay i
d9c0: 6e 63 72 65 61 73 69 6e 67 0d 0a 2a 2a 20 62 79  ncreasing..** by
d9d0: 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 32   an additional 2
d9e0: 35 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 77  5 milliseconds w
d9f0: 69 74 68 20 65 61 63 68 20 73 75 62 73 65 71 75  ith each subsequ
da00: 65 6e 74 20 72 65 74 72 79 2e 20 20 54 68 69 73  ent retry.  This
da10: 0d 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6c 6c 6f  ..** opcode allo
da20: 77 73 20 74 68 65 73 65 20 74 77 6f 20 76 61 6c  ws these two val
da30: 75 65 73 20 28 31 30 20 72 65 74 72 69 65 73 20  ues (10 retries 
da40: 61 6e 64 20 32 35 20 6d 69 6c 6c 69 73 65 63 6f  and 25 milliseco
da50: 6e 64 73 20 6f 66 20 64 65 6c 61 79 29 0d 0a 2a  nds of delay)..*
da60: 2a 20 74 6f 20 62 65 20 61 64 6a 75 73 74 65 64  * to be adjusted
da70: 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 61 72  .  The values ar
da80: 65 20 63 68 61 6e 67 65 64 20 66 6f 72 20 61 6c  e changed for al
da90: 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  l database conne
daa0: 63 74 69 6f 6e 73 0d 0a 2a 2a 20 77 69 74 68 69  ctions..** withi
dab0: 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  n the same proce
dac0: 73 73 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  ss.  The argumen
dad0: 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
dae0: 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 77  o an array of tw
daf0: 6f 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 73 20 77  o..** integers w
db00: 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20 69  here the first i
db10: 6e 74 65 67 65 72 20 69 20 74 68 65 20 6e 65 77  nteger i the new
db20: 20 72 65 74 72 79 20 63 6f 75 6e 74 20 61 6e 64   retry count and
db30: 20 74 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a 20   the second..** 
db40: 69 6e 74 65 67 65 72 20 69 73 20 74 68 65 20 64  integer is the d
db50: 65 6c 61 79 2e 20 20 49 66 20 65 69 74 68 65 72  elay.  If either
db60: 20 69 6e 74 65 67 65 72 20 69 73 20 6e 65 67 61   integer is nega
db70: 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20 73  tive, then the s
db80: 65 74 74 69 6e 67 0d 0a 2a 2a 20 69 73 20 6e 6f  etting..** is no
db90: 74 20 63 68 61 6e 67 65 64 20 62 75 74 20 69 6e  t changed but in
dba0: 73 74 65 61 64 20 74 68 65 20 70 72 69 6f 72 20  stead the prior 
dbb0: 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20 73 65  value of that se
dbc0: 74 74 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  tting is written
dbd0: 0d 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 72  ..** into the ar
dbe0: 72 61 79 20 65 6e 74 72 79 2c 20 61 6c 6c 6f 77  ray entry, allow
dbf0: 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
dc00: 72 65 74 72 79 20 73 65 74 74 69 6e 67 73 20 74  retry settings t
dc10: 6f 20 62 65 0d 0a 2a 2a 20 69 6e 74 65 72 72 6f  o be..** interro
dc20: 67 61 74 65 64 2e 20 20 54 68 65 20 7a 44 62 4e  gated.  The zDbN
dc30: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
dc40: 20 69 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a   ignored...**..*
dc50: 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 46  * ^The [SQLITE_F
dc60: 43 4e 54 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c  CNTL_PERSIST_WAL
dc70: 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  ] opcode is used
dc80: 20 74 6f 20 73 65 74 20 6f 72 20 71 75 65 72 79   to set or query
dc90: 20 74 68 65 0d 0a 2a 2a 20 70 65 72 73 69 73 74   the..** persist
dca0: 65 6e 74 20 5b 57 41 4c 20 7c 20 57 72 69 74 65  ent [WAL | Write
dcb0: 20 41 48 65 61 64 20 4c 6f 67 5d 20 73 65 74 74   AHead Log] sett
dcc0: 69 6e 67 2e 20 20 42 79 20 64 65 66 61 75 6c 74  ing.  By default
dcd0: 2c 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0d  , the auxiliary.
dce0: 0a 2a 2a 20 77 72 69 74 65 20 61 68 65 61 64 20  .** write ahead 
dcf0: 6c 6f 67 20 61 6e 64 20 73 68 61 72 65 64 20 6d  log and shared m
dd00: 65 6d 6f 72 79 20 66 69 6c 65 73 20 75 73 65 64  emory files used
dd10: 20 66 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e   for transaction
dd20: 20 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a 20 61 72 65   control..** are
dd30: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
dd40: 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
dd50: 6c 61 74 65 73 74 20 63 6f 6e 6e 65 63 74 69 6f  latest connectio
dd60: 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
dd70: 65 0d 0a 2a 2a 20 63 6c 6f 73 65 73 2e 20 20 53  e..** closes.  S
dd80: 65 74 74 69 6e 67 20 70 65 72 73 69 73 74 65 6e  etting persisten
dd90: 74 20 57 41 4c 20 6d 6f 64 65 20 63 61 75 73 65  t WAL mode cause
dda0: 73 20 74 68 6f 73 65 20 66 69 6c 65 73 20 74 6f  s those files to
ddb0: 20 70 65 72 73 69 73 74 20 61 66 74 65 72 0d 0a   persist after..
ddc0: 2a 2a 20 63 6c 6f 73 65 2e 20 20 50 65 72 73 69  ** close.  Persi
ddd0: 73 74 69 6e 67 20 74 68 65 20 66 69 6c 65 73 20  sting the files 
dde0: 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 6f  is useful when o
ddf0: 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74  ther processes t
de00: 68 61 74 20 64 6f 20 6e 6f 74 0d 0a 2a 2a 20 68  hat do not..** h
de10: 61 76 65 20 77 72 69 74 65 20 70 65 72 6d 69 73  ave write permis
de20: 73 69 6f 6e 20 6f 6e 20 74 68 65 20 64 69 72 65  sion on the dire
de30: 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67  ctory containing
de40: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
de50: 6c 65 20 77 61 6e 74 0d 0a 2a 2a 20 74 6f 20 72  le want..** to r
de60: 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
de70: 20 66 69 6c 65 2c 20 61 73 20 74 68 65 20 57 41   file, as the WA
de80: 4c 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d  L and shared mem
de90: 6f 72 79 20 66 69 6c 65 73 20 6d 75 73 74 20 65  ory files must e
dea0: 78 69 73 74 0d 0a 2a 2a 20 69 6e 20 6f 72 64 65  xist..** in orde
deb0: 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  r for the databa
dec0: 73 65 20 74 6f 20 62 65 20 72 65 61 64 61 62 6c  se to be readabl
ded0: 65 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70  e.  The fourth p
dee0: 61 72 61 6d 65 74 65 72 20 74 6f 0d 0a 2a 2a 20  arameter to..** 
def0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
df00: 6e 74 72 6f 6c 28 29 5d 20 66 6f 72 20 74 68 69  ntrol()] for thi
df10: 73 20 6f 70 63 6f 64 65 20 73 68 6f 75 6c 64 20  s opcode should 
df20: 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
df30: 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a 2a 2a 20  an integer...** 
df40: 54 68 61 74 20 69 6e 74 65 67 65 72 20 69 73 20  That integer is 
df50: 30 20 74 6f 20 64 69 73 61 62 6c 65 20 70 65 72  0 to disable per
df60: 73 69 73 74 65 6e 74 20 57 41 4c 20 6d 6f 64 65  sistent WAL mode
df70: 20 6f 72 20 31 20 74 6f 20 65 6e 61 62 6c 65 20   or 1 to enable 
df80: 70 65 72 73 69 73 74 65 6e 74 0d 0a 2a 2a 20 57  persistent..** W
df90: 41 4c 20 6d 6f 64 65 2e 20 20 49 66 20 74 68 65  AL mode.  If the
dfa0: 20 69 6e 74 65 67 65 72 20 69 73 20 2d 31 2c 20   integer is -1, 
dfb0: 74 68 65 6e 20 69 74 20 69 73 20 6f 76 65 72 77  then it is overw
dfc0: 72 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20  ritten with the 
dfd0: 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 57 41 4c 20  current..** WAL 
dfe0: 70 65 72 73 69 73 74 65 6e 63 65 20 73 65 74 74  persistence sett
dff0: 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ing...**..** ^Th
e000: 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  e [SQLITE_FCNTL_
e010: 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52  POWERSAFE_OVERWR
e020: 49 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75  ITE] opcode is u
e030: 73 65 64 20 74 6f 20 73 65 74 20 6f 72 20 71 75  sed to set or qu
e040: 65 72 79 20 74 68 65 0d 0a 2a 2a 20 70 65 72 73  ery the..** pers
e050: 69 73 74 65 6e 74 20 22 70 6f 77 65 72 73 61 66  istent "powersaf
e060: 65 2d 6f 76 65 72 77 72 69 74 65 22 20 6f 72 20  e-overwrite" or 
e070: 22 50 53 4f 57 22 20 73 65 74 74 69 6e 67 2e 20  "PSOW" setting. 
e080: 20 54 68 65 20 50 53 4f 57 20 73 65 74 74 69 6e   The PSOW settin
e090: 67 0d 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73  g..** determines
e0a0: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 49 4f 43   the [SQLITE_IOC
e0b0: 41 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45  AP_POWERSAFE_OVE
e0c0: 52 57 52 49 54 45 5d 20 62 69 74 20 6f 66 20 74  RWRITE] bit of t
e0d0: 68 65 0d 0a 2a 2a 20 78 44 65 76 69 63 65 43 68  he..** xDeviceCh
e0e0: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6d 65  aracteristics me
e0f0: 74 68 6f 64 73 2e 20 54 68 65 20 66 6f 75 72 74  thods. The fourt
e100: 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0d 0a  h parameter to..
e110: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  ** [sqlite3_file
e120: 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 66 6f 72 20  _control()] for 
e130: 74 68 69 73 20 6f 70 63 6f 64 65 20 73 68 6f 75  this opcode shou
e140: 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20  ld be a pointer 
e150: 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a  to an integer...
e160: 2a 2a 20 54 68 61 74 20 69 6e 74 65 67 65 72 20  ** That integer 
e170: 69 73 20 30 20 74 6f 20 64 69 73 61 62 6c 65 20  is 0 to disable 
e180: 7a 65 72 6f 2d 64 61 6d 61 67 65 20 6d 6f 64 65  zero-damage mode
e190: 20 6f 72 20 31 20 74 6f 20 65 6e 61 62 6c 65 20   or 1 to enable 
e1a0: 7a 65 72 6f 2d 64 61 6d 61 67 65 0d 0a 2a 2a 20  zero-damage..** 
e1b0: 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 69 6e  mode.  If the in
e1c0: 74 65 67 65 72 20 69 73 20 2d 31 2c 20 74 68 65  teger is -1, the
e1d0: 6e 20 69 74 20 69 73 20 6f 76 65 72 77 72 69 74  n it is overwrit
e1e0: 74 65 6e 20 77 69 74 68 20 74 68 65 20 63 75 72  ten with the cur
e1f0: 72 65 6e 74 0d 0a 2a 2a 20 7a 65 72 6f 2d 64 61  rent..** zero-da
e200: 6d 61 67 65 20 6d 6f 64 65 20 73 65 74 74 69 6e  mage mode settin
e210: 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  g...**..** ^The 
e220: 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4f 56  [SQLITE_FCNTL_OV
e230: 45 52 57 52 49 54 45 5d 20 6f 70 63 6f 64 65 20  ERWRITE] opcode 
e240: 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 53 51  is invoked by SQ
e250: 4c 69 74 65 20 61 66 74 65 72 20 6f 70 65 6e 69  Lite after openi
e260: 6e 67 0d 0a 2a 2a 20 61 20 77 72 69 74 65 20 74  ng..** a write t
e270: 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 69 6e  ransaction to in
e280: 64 69 63 61 74 65 20 74 68 61 74 2c 20 75 6e 6c  dicate that, unl
e290: 65 73 73 20 69 74 20 69 73 20 72 6f 6c 6c 65 64  ess it is rolled
e2a0: 20 62 61 63 6b 20 66 6f 72 20 73 6f 6d 65 0d 0a   back for some..
e2b0: 2a 2a 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 65  ** reason, the e
e2c0: 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 20 66  ntire database f
e2d0: 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 76 65 72  ile will be over
e2e0: 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 63  written by the c
e2f0: 75 72 72 65 6e 74 20 0d 0a 2a 2a 20 74 72 61 6e  urrent ..** tran
e300: 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 69 73  saction. This is
e310: 20 75 73 65 64 20 62 79 20 56 41 43 55 55 4d 20   used by VACUUM 
e320: 6f 70 65 72 61 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d  operations...**.
e330: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45  .** ^The [SQLITE
e340: 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 4d 45 5d 20  _FCNTL_VFSNAME] 
e350: 6f 70 63 6f 64 65 20 63 61 6e 20 62 65 20 75 73  opcode can be us
e360: 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  ed to obtain the
e370: 20 6e 61 6d 65 73 20 6f 66 0d 0a 2a 2a 20 61 6c   names of..** al
e380: 6c 20 5b 56 46 53 65 73 5d 20 69 6e 20 74 68 65  l [VFSes] in the
e390: 20 56 46 53 20 73 74 61 63 6b 2e 20 20 54 68 65   VFS stack.  The
e3a0: 20 6e 61 6d 65 73 20 61 72 65 20 6f 66 20 61 6c   names are of al
e3b0: 6c 20 56 46 53 20 73 68 69 6d 73 20 61 6e 64 20  l VFS shims and 
e3c0: 74 68 65 0d 0a 2a 2a 20 66 69 6e 61 6c 20 62 6f  the..** final bo
e3d0: 74 74 6f 6d 2d 6c 65 76 65 6c 20 56 46 53 20 61  ttom-level VFS a
e3e0: 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  re written into 
e3f0: 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
e400: 66 72 6f 6d 20 0d 0a 2a 2a 20 5b 73 71 6c 69 74  from ..** [sqlit
e410: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 61 6e 64  e3_malloc()] and
e420: 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 73   the result is s
e430: 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 68 61  tored in the cha
e440: 72 2a 20 76 61 72 69 61 62 6c 65 0d 0a 2a 2a 20  r* variable..** 
e450: 74 68 61 74 20 74 68 65 20 66 6f 75 72 74 68 20  that the fourth 
e460: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71  parameter of [sq
e470: 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
e480: 6f 6c 28 29 5d 20 70 6f 69 6e 74 73 20 74 6f 2e  ol()] points to.
e490: 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  ..** The caller 
e4a0: 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
e4b0: 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d  or freeing the m
e4c0: 65 6d 6f 72 79 20 77 68 65 6e 20 64 6f 6e 65 2e  emory when done.
e4d0: 20 20 41 73 20 77 69 74 68 0d 0a 2a 2a 20 61 6c    As with..** al
e4e0: 6c 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 61  l file-control a
e4f0: 63 74 69 6f 6e 73 2c 20 74 68 65 72 65 20 69 73  ctions, there is
e500: 20 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68   no guarantee th
e510: 61 74 20 74 68 69 73 20 77 69 6c 6c 20 61 63 74  at this will act
e520: 75 61 6c 6c 79 0d 0a 2a 2a 20 64 6f 20 61 6e 79  ually..** do any
e530: 74 68 69 6e 67 2e 20 20 43 61 6c 6c 65 72 73 20  thing.  Callers 
e540: 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a  should initializ
e550: 65 20 74 68 65 20 63 68 61 72 2a 20 76 61 72 69  e the char* vari
e560: 61 62 6c 65 20 74 6f 20 61 20 4e 55 4c 4c 0d 0a  able to a NULL..
e570: 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 20 63 61  ** pointer in ca
e580: 73 65 20 74 68 69 73 20 66 69 6c 65 2d 63 6f 6e  se this file-con
e590: 74 72 6f 6c 20 69 73 20 6e 6f 74 20 69 6d 70 6c  trol is not impl
e5a0: 65 6d 65 6e 74 65 64 2e 20 20 54 68 69 73 20 66  emented.  This f
e5b0: 69 6c 65 2d 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a 20  ile-control..** 
e5c0: 69 73 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  is intended for 
e5d0: 64 69 61 67 6e 6f 73 74 69 63 20 75 73 65 20 6f  diagnostic use o
e5e0: 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  nly...*/..#defin
e5f0: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
e600: 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 20 20  OCKSTATE        
e610: 20 20 20 20 20 20 20 31 0d 0a 23 64 65 66 69 6e         1..#defin
e620: 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43  e SQLITE_GET_LOC
e630: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20  KPROXYFILE      
e640: 20 20 20 20 20 20 20 32 0d 0a 23 64 65 66 69 6e         2..#defin
e650: 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  e SQLITE_SET_LOC
e660: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20  KPROXYFILE      
e670: 20 20 20 20 20 20 20 33 0d 0a 23 64 65 66 69 6e         3..#defin
e680: 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52  e SQLITE_LAST_ER
e690: 52 4e 4f 20 20 20 20 20 20 20 20 20 20 20 20 20  RNO             
e6a0: 20 20 20 20 20 20 20 34 0d 0a 23 64 65 66 69 6e         4..#defin
e6b0: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 53  e SQLITE_FCNTL_S
e6c0: 49 5a 45 5f 48 49 4e 54 20 20 20 20 20 20 20 20  IZE_HINT        
e6d0: 20 20 20 20 20 20 20 35 0d 0a 23 64 65 66 69 6e         5..#defin
e6e0: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 43  e SQLITE_FCNTL_C
e6f0: 48 55 4e 4b 5f 53 49 5a 45 20 20 20 20 20 20 20  HUNK_SIZE       
e700: 20 20 20 20 20 20 20 36 0d 0a 23 64 65 66 69 6e         6..#defin
e710: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 46  e SQLITE_FCNTL_F
e720: 49 4c 45 5f 50 4f 49 4e 54 45 52 20 20 20 20 20  ILE_POINTER     
e730: 20 20 20 20 20 20 20 37 0d 0a 23 64 65 66 69 6e         7..#defin
e740: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 53  e SQLITE_FCNTL_S
e750: 59 4e 43 5f 4f 4d 49 54 54 45 44 20 20 20 20 20  YNC_OMITTED     
e760: 20 20 20 20 20 20 20 38 0d 0a 23 64 65 66 69 6e         8..#defin
e770: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 57  e SQLITE_FCNTL_W
e780: 49 4e 33 32 5f 41 56 5f 52 45 54 52 59 20 20 20  IN32_AV_RETRY   
e790: 20 20 20 20 20 20 20 39 0d 0a 23 64 65 66 69 6e         9..#defin
e7a0: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50  e SQLITE_FCNTL_P
e7b0: 45 52 53 49 53 54 5f 57 41 4c 20 20 20 20 20 20  ERSIST_WAL      
e7c0: 20 20 20 20 20 20 31 30 0d 0a 23 64 65 66 69 6e        10..#defin
e7d0: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4f  e SQLITE_FCNTL_O
e7e0: 56 45 52 57 52 49 54 45 20 20 20 20 20 20 20 20  VERWRITE        
e7f0: 20 20 20 20 20 20 31 31 0d 0a 23 64 65 66 69 6e        11..#defin
e800: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 56  e SQLITE_FCNTL_V
e810: 46 53 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20  FSNAME          
e820: 20 20 20 20 20 20 31 32 0d 0a 23 64 65 66 69 6e        12..#defin
e830: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50  e SQLITE_FCNTL_P
e840: 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49  OWERSAFE_OVERWRI
e850: 54 45 20 20 20 20 31 33 0d 0a 0d 0a 2f 2a 0d 0a  TE    13..../*..
e860: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
e870: 65 78 20 48 61 6e 64 6c 65 0d 0a 2a 2a 0d 0a 2a  ex Handle..**..*
e880: 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75  * The mutex modu
e890: 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65  le within SQLite
e8a0: 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65   defines [sqlite
e8b0: 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61  3_mutex] to be a
e8c0: 6e 0d 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74  n..** abstract t
e8d0: 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20  ype for a mutex 
e8e0: 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c  object.  The SQL
e8f0: 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c  ite core never l
e900: 6f 6f 6b 73 0d 0a 2a 2a 20 61 74 20 74 68 65 20  ooks..** at the 
e910: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
e920: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73  ntation of an [s
e930: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20  qlite3_mutex].  
e940: 49 74 20 6f 6e 6c 79 0d 0a 2a 2a 20 64 65 61 6c  It only..** deal
e950: 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  s with pointers 
e960: 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  to the [sqlite3_
e970: 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0d 0a  mutex] object...
e980: 2a 2a 0d 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61  **..** Mutexes a
e990: 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  re created using
e9a0: 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
e9b0: 61 6c 6c 6f 63 28 29 5d 2e 0d 0a 2a 2f 0d 0a 74  alloc()]...*/..t
e9c0: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
e9d0: 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69  lite3_mutex sqli
e9e0: 74 65 33 5f 6d 75 74 65 78 3b 0d 0a 0d 0a 2f 2a  te3_mutex;..../*
e9f0: 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  ..** CAPI3REF: O
ea00: 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 6a 65  S Interface Obje
ea10: 63 74 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 69 6e  ct..**..** An in
ea20: 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71  stance of the sq
ea30: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
ea40: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
ea50: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 0d 0a  erface between..
ea60: 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  ** the SQLite co
ea70: 72 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  re and the under
ea80: 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
ea90: 73 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 66  system.  The "vf
eaa0: 73 22 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61  s"..** in the na
eab0: 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74  me of the object
eac0: 20 73 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72   stands for "vir
ead0: 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  tual file system
eae0: 22 2e 20 20 53 65 65 0d 0a 2a 2a 20 74 68 65 20  ".  See..** the 
eaf0: 5b 56 46 53 20 7c 20 56 46 53 20 64 6f 63 75 6d  [VFS | VFS docum
eb00: 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 66 75  entation] for fu
eb10: 72 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f  rther informatio
eb20: 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 76  n...**..** The v
eb30: 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 65 72  alue of the iVer
eb40: 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 69 6e  sion field is in
eb50: 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 6d 61  itially 1 but ma
eb60: 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e 0d 0a  y be larger in..
eb70: 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 6f  ** future versio
eb80: 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  ns of SQLite.  A
eb90: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
eba0: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64   may be appended
ebb0: 20 74 6f 20 74 68 69 73 0d 0a 2a 2a 20 6f 62 6a   to this..** obj
ebc0: 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65  ect when the iVe
ebd0: 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69  rsion value is i
ebe0: 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20  ncreased.  Note 
ebf0: 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75  that the structu
ec00: 72 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71  re..** of the sq
ec10: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
ec20: 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20   changes in the 
ec30: 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77  transaction betw
ec40: 65 65 6e 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 76  een..** SQLite v
ec50: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64  ersion 3.5.9 and
ec60: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74   3.6.0 and yet t
ec70: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c  he iVersion fiel
ec80: 64 20 77 61 73 20 6e 6f 74 0d 0a 2a 2a 20 6d 6f  d was not..** mo
ec90: 64 69 66 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  dified...**..** 
eca0: 54 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69 65  The szOsFile fie
ecb0: 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ld is the size o
ecc0: 66 20 74 68 65 20 73 75 62 63 6c 61 73 73 65 64  f the subclassed
ecd0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0d   [sqlite3_file].
ece0: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 75 73  .** structure us
ecf0: 65 64 20 62 79 20 74 68 69 73 20 56 46 53 2e 20  ed by this VFS. 
ed00: 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74   mxPathname is t
ed10: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
ed20: 68 20 6f 66 0d 0a 2a 2a 20 61 20 70 61 74 68 6e  h of..** a pathn
ed30: 61 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53 2e  ame in this VFS.
ed40: 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 67 69 73 74 65  ..**..** Registe
ed50: 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66 73 20  red sqlite3_vfs 
ed60: 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65 70 74  objects are kept
ed70: 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   on a linked lis
ed80: 74 20 66 6f 72 6d 65 64 20 62 79 0d 0a 2a 2a 20  t formed by..** 
ed90: 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65  the pNext pointe
eda0: 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  r.  The [sqlite3
edb0: 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 5d  _vfs_register()]
edc0: 0d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65  ..** and [sqlite
edd0: 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
ede0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d  ()] interfaces m
edf0: 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0d  anage this list.
ee00: 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d  .** in a thread-
ee10: 73 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b  safe way.  The [
ee20: 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
ee30: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a  ()] interface..*
ee40: 2a 20 73 65 61 72 63 68 65 73 20 74 68 65 20 6c  * searches the l
ee50: 69 73 74 2e 20 20 4e 65 69 74 68 65 72 20 74 68  ist.  Neither th
ee60: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f  e application co
ee70: 64 65 20 6e 6f 72 20 74 68 65 20 56 46 53 0d 0a  de nor the VFS..
ee80: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
ee90: 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65  n should use the
eea0: 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 0d   pNext pointer..
eeb0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70 4e 65 78  .**..** The pNex
eec0: 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f  t field is the o
eed0: 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65  nly field in the
eee0: 20 73 71 6c 69 74 65 33 5f 76 66 73 0d 0a 2a 2a   sqlite3_vfs..**
eef0: 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
ef00: 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72  SQLite will ever
ef10: 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65   modify.  SQLite
ef20: 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73   will only acces
ef30: 73 0d 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20  s..** or modify 
ef40: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65  this field while
ef50: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69   holding a parti
ef60: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74  cular static mut
ef70: 65 78 2e 0d 0a 2a 2a 20 54 68 65 20 61 70 70 6c  ex...** The appl
ef80: 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  ication should n
ef90: 65 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74  ever modify anyt
efa0: 68 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20  hing within the 
efb0: 73 71 6c 69 74 65 33 5f 76 66 73 0d 0a 2a 2a 20  sqlite3_vfs..** 
efc0: 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20  object once the 
efd0: 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20  object has been 
efe0: 72 65 67 69 73 74 65 72 65 64 2e 0d 0a 2a 2a 0d  registered...**.
eff0: 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69  .** The zName fi
f000: 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  eld holds the na
f010: 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f  me of the VFS mo
f020: 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20  dule.  The name 
f030: 6d 75 73 74 0d 0a 2a 2a 20 62 65 20 75 6e 69 71  must..** be uniq
f040: 75 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56 46  ue across all VF
f050: 53 20 6d 6f 64 75 6c 65 73 2e 0d 0a 2a 2a 0d 0a  S modules...**..
f060: 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 5f 76 66 73  ** [[sqlite3_vfs
f070: 2e 78 4f 70 65 6e 5d 5d 0d 0a 2a 2a 20 5e 53 51  .xOpen]]..** ^SQ
f080: 4c 69 74 65 20 67 75 61 72 61 6e 74 65 65 73 20  Lite guarantees 
f090: 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61  that the zFilena
f0a0: 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  me parameter to 
f0b0: 78 4f 70 65 6e 0d 0a 2a 2a 20 69 73 20 65 69 74  xOpen..** is eit
f0c0: 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  her a NULL point
f0d0: 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74  er or string obt
f0e0: 61 69 6e 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20 78  ained..** from x
f0f0: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 77  FullPathname() w
f100: 69 74 68 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20  ith an optional 
f110: 73 75 66 66 69 78 20 61 64 64 65 64 2e 0d 0a 2a  suffix added...*
f120: 2a 20 5e 49 66 20 61 20 73 75 66 66 69 78 20 69  * ^If a suffix i
f130: 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 7a  s added to the z
f140: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74  Filename paramet
f150: 65 72 2c 20 69 74 20 77 69 6c 6c 0d 0a 2a 2a 20  er, it will..** 
f160: 63 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e  consist of a sin
f170: 67 6c 65 20 22 2d 22 20 63 68 61 72 61 63 74 65  gle "-" characte
f180: 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 6f  r followed by no
f190: 20 6d 6f 72 65 20 74 68 61 6e 0d 0a 2a 2a 20 31   more than..** 1
f1a0: 31 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 61  1 alphanumeric a
f1b0: 6e 64 2f 6f 72 20 22 2d 22 20 63 68 61 72 61 63  nd/or "-" charac
f1c0: 74 65 72 73 2e 0d 0a 2a 2a 20 5e 53 51 4c 69 74  ters...** ^SQLit
f1d0: 65 20 66 75 72 74 68 65 72 20 67 75 61 72 61 6e  e further guaran
f1e0: 74 65 65 73 20 74 68 61 74 0d 0a 2a 2a 20 74 68  tees that..** th
f1f0: 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65  e string will be
f200: 20 76 61 6c 69 64 20 61 6e 64 20 75 6e 63 68 61   valid and uncha
f210: 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 6c 6f 73  nged until xClos
f220: 65 28 29 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65  e() is..** calle
f230: 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68  d. Because of th
f240: 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65  e previous sente
f250: 6e 63 65 2c 0d 0a 2a 2a 20 74 68 65 20 5b 73 71  nce,..** the [sq
f260: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20  lite3_file] can 
f270: 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 70  safely store a p
f280: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0d 0a 2a  ointer to the..*
f290: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74  * filename if it
f2a0: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62   needs to rememb
f2b0: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  er the filename 
f2c0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e  for some reason.
f2d0: 0d 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c  ..** If the zFil
f2e0: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20  ename parameter 
f2f0: 74 6f 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55  to xOpen is a NU
f300: 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
f310: 78 4f 70 65 6e 0d 0a 2a 2a 20 6d 75 73 74 20 69  xOpen..** must i
f320: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65  nvent its own te
f330: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72  mporary name for
f340: 20 74 68 65 20 66 69 6c 65 2e 20 20 5e 57 68 65   the file.  ^Whe
f350: 6e 65 76 65 72 20 74 68 65 20 0d 0a 2a 2a 20 78  never the ..** x
f360: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74  Filename paramet
f370: 65 72 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69  er is NULL it wi
f380: 6c 6c 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63  ll also be the c
f390: 61 73 65 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a  ase that the..**
f3a0: 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
f3b0: 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53   will include [S
f3c0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
f3d0: 45 4f 4e 43 4c 4f 53 45 5d 2e 0d 0a 2a 2a 0d 0a  EONCLOSE]...**..
f3e0: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
f3f0: 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29  ument to xOpen()
f400: 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69   includes all bi
f410: 74 73 20 73 65 74 20 69 6e 0d 0a 2a 2a 20 74 68  ts set in..** th
f420: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  e flags argument
f430: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65   to [sqlite3_ope
f440: 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 20  n_v2()].  Or if 
f450: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
f460: 0d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ..** or [sqlite3
f470: 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75 73  _open16()] is us
f480: 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20 69  ed, then flags i
f490: 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73 74  ncludes at least
f4a0: 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  ..** [SQLITE_OPE
f4b0: 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
f4c0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
f4d0: 54 45 5d 2e 20 0d 0a 2a 2a 20 49 66 20 78 4f 70  TE]. ..** If xOp
f4e0: 65 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c  en() opens a fil
f4f0: 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e  e read-only then
f500: 20 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c   it sets *pOutFl
f510: 61 67 73 20 74 6f 0d 0a 2a 2a 20 69 6e 63 6c 75  ags to..** inclu
f520: 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  de [SQLITE_OPEN_
f530: 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65  READONLY].  Othe
f540: 72 20 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46  r bits in *pOutF
f550: 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e  lags may be set.
f560: 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 53 51 4c 69 74  ..**..** ^(SQLit
f570: 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20  e will also add 
f580: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
f590: 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68  wing flags to th
f5a0: 65 20 78 4f 70 65 6e 28 29 0d 0a 2a 2a 20 63 61  e xOpen()..** ca
f5b0: 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ll, depending on
f5c0: 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e   the object bein
f5d0: 67 20 6f 70 65 6e 65 64 3a 0d 0a 2a 2a 0d 0a 2a  g opened:..**..*
f5e0: 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20  * <ul>..** <li> 
f5f0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41   [SQLITE_OPEN_MA
f600: 49 4e 5f 44 42 5d 0d 0a 2a 2a 20 3c 6c 69 3e 20  IN_DB]..** <li> 
f610: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41   [SQLITE_OPEN_MA
f620: 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0d 0a 2a 2a 20  IN_JOURNAL]..** 
f630: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
f640: 45 4e 5f 54 45 4d 50 5f 44 42 5d 0d 0a 2a 2a 20  EN_TEMP_DB]..** 
f650: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
f660: 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d  EN_TEMP_JOURNAL]
f670: 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ..** <li>  [SQLI
f680: 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
f690: 54 5f 44 42 5d 0d 0a 2a 2a 20 3c 6c 69 3e 20 20  T_DB]..** <li>  
f6a0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42  [SQLITE_OPEN_SUB
f6b0: 4a 4f 55 52 4e 41 4c 5d 0d 0a 2a 2a 20 3c 6c 69  JOURNAL]..** <li
f6c0: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
f6d0: 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0d  MASTER_JOURNAL].
f6e0: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
f6f0: 45 5f 4f 50 45 4e 5f 57 41 4c 5d 0d 0a 2a 2a 20  E_OPEN_WAL]..** 
f700: 3c 2f 75 6c 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  </ul>)^..**..** 
f710: 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70  The file I/O imp
f720: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20  lementation can 
f730: 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74  use the object t
f740: 79 70 65 20 66 6c 61 67 73 20 74 6f 0d 0a 2a 2a  ype flags to..**
f750: 20 63 68 61 6e 67 65 20 74 68 65 20 77 61 79 20   change the way 
f760: 69 74 20 64 65 61 6c 73 20 77 69 74 68 20 66 69  it deals with fi
f770: 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  les.  For exampl
f780: 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  e, an applicatio
f790: 6e 0d 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20  n..** that does 
f7a0: 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 63  not care about c
f7b0: 72 61 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72  rash recovery or
f7c0: 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20   rollback might 
f7d0: 6d 61 6b 65 0d 0a 2a 2a 20 74 68 65 20 6f 70 65  make..** the ope
f7e0: 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66  n of a journal f
f7f0: 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72  ile a no-op.  Wr
f800: 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f 75  ites to this jou
f810: 72 6e 61 6c 20 77 6f 75 6c 64 0d 0a 2a 2a 20 61  rnal would..** a
f820: 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61  lso be no-ops, a
f830: 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74  nd any attempt t
f840: 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e  o read the journ
f850: 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0d  al would return.
f860: 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  .** SQLITE_IOERR
f870: 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d  .  Or the implem
f880: 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72  entation might r
f890: 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20  ecognize that a 
f8a0: 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c  database..** fil
f8b0: 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20  e will be doing 
f8c0: 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63  page-aligned sec
f8d0: 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72  tor reads and wr
f8e0: 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d  ites in a random
f8f0: 0d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73  ..** order and s
f900: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75  et up its I/O su
f910: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e  bsystem accordin
f920: 67 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c  gly...**..** SQL
f930: 69 74 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61  ite might also a
f940: 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  dd one of the fo
f950: 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f  llowing flags to
f960: 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f   the xOpen metho
f970: 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d  d:..**..** <ul>.
f980: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
f990: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
f9a0: 4f 53 45 5d 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  OSE]..** <li> [S
f9b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
f9c0: 53 49 56 45 5d 0d 0a 2a 2a 20 3c 2f 75 6c 3e 0d  SIVE]..** </ul>.
f9d0: 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  .**..** The [SQL
f9e0: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
f9f0: 4e 43 4c 4f 53 45 5d 20 66 6c 61 67 20 6d 65 61  NCLOSE] flag mea
fa00: 6e 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75  ns the file shou
fa10: 6c 64 20 62 65 0d 0a 2a 2a 20 64 65 6c 65 74 65  ld be..** delete
fa20: 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f  d when it is clo
fa30: 73 65 64 2e 20 20 5e 54 68 65 20 5b 53 51 4c 49  sed.  ^The [SQLI
fa40: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
fa50: 43 4c 4f 53 45 5d 0d 0a 2a 2a 20 77 69 6c 6c 20  CLOSE]..** will 
fa60: 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50 20  be set for TEMP 
fa70: 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 74 68  databases and th
fa80: 65 69 72 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 72  eir journals, tr
fa90: 61 6e 73 69 65 6e 74 0d 0a 2a 2a 20 64 61 74 61  ansient..** data
faa0: 62 61 73 65 73 2c 20 61 6e 64 20 73 75 62 6a 6f  bases, and subjo
fab0: 75 72 6e 61 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  urnals...**..** 
fac0: 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45  ^The [SQLITE_OPE
fad0: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61  N_EXCLUSIVE] fla
fae0: 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64  g is always used
faf0: 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0d   in conjunction.
fb00: 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51  .** with the [SQ
fb10: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
fb20: 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72  ] flag, which ar
fb30: 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0d  e both directly.
fb40: 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f  .** analogous to
fb50: 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20   the O_EXCL and 
fb60: 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66  O_CREAT flags of
fb70: 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28   the POSIX open(
fb80: 29 0d 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20  )..** API.  The 
fb90: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
fba0: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e  USIVE flag, when
fbb0: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65   paired with the
fbc0: 20 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45   ..** SQLITE_OPE
fbd0: 4e 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65  N_CREATE, is use
fbe0: 64 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  d to indicate th
fbf0: 61 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61  at file should a
fc00: 6c 77 61 79 73 0d 0a 2a 2a 20 62 65 20 63 72 65  lways..** be cre
fc10: 61 74 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69  ated, and that i
fc20: 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66  t is an error if
fc30: 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73   it already exis
fc40: 74 73 2e 0d 0a 2a 2a 20 49 74 20 69 73 20 3c 69  ts...** It is <i
fc50: 3e 6e 6f 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f  >not</i> used to
fc60: 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 66 69   indicate the fi
fc70: 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 6f 70 65  le should be ope
fc80: 6e 65 64 20 0d 0a 2a 2a 20 66 6f 72 20 65 78 63  ned ..** for exc
fc90: 6c 75 73 69 76 65 20 61 63 63 65 73 73 2e 0d 0a  lusive access...
fca0: 2a 2a 0d 0a 2a 2a 20 5e 41 74 20 6c 65 61 73 74  **..** ^At least
fcb0: 20 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 20   szOsFile bytes 
fcc0: 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 61 6c  of memory are al
fcd0: 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 74  located by SQLit
fce0: 65 0d 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 68  e..** to hold th
fcf0: 65 20 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  e  [sqlite3_file
fd00: 5d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  ] structure pass
fd10: 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 0d  ed as the third.
fd20: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  .** argument to 
fd30: 78 4f 70 65 6e 2e 20 20 54 68 65 20 78 4f 70 65  xOpen.  The xOpe
fd40: 6e 20 6d 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f  n method does no
fd50: 74 20 68 61 76 65 20 74 6f 0d 0a 2a 2a 20 61 6c  t have to..** al
fd60: 6c 6f 63 61 74 65 20 74 68 65 20 73 74 72 75 63  locate the struc
fd70: 74 75 72 65 3b 20 69 74 20 73 68 6f 75 6c 64 20  ture; it should 
fd80: 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 69 6e 2e  just fill it in.
fd90: 20 20 4e 6f 74 65 20 74 68 61 74 0d 0a 2a 2a 20    Note that..** 
fda0: 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  the xOpen method
fdb0: 20 6d 75 73 74 20 73 65 74 20 74 68 65 20 73 71   must set the sq
fdc0: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
fdd0: 6f 64 73 20 74 6f 20 65 69 74 68 65 72 0d 0a 2a  ods to either..*
fde0: 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c 69 74  * a valid [sqlit
fdf0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
fe00: 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55 4c 4c  bject or to NULL
fe10: 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20 64 6f  .  xOpen must do
fe20: 0d 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20 69  ..** this even i
fe30: 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c 73  f the open fails
fe40: 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63 74  .  SQLite expect
fe50: 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74  s that the sqlit
fe60: 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 73  e3_file.pMethods
fe70: 0d 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c  ..** element wil
fe80: 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72  l be valid after
fe90: 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72   xOpen returns r
fea0: 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
feb0: 20 73 75 63 63 65 73 73 0d 0a 2a 2a 20 6f 72 20   success..** or 
fec0: 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78  failure of the x
fed0: 4f 70 65 6e 20 63 61 6c 6c 2e 0d 0a 2a 2a 0d 0a  Open call...**..
fee0: 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 5f 76 66 73  ** [[sqlite3_vfs
fef0: 2e 78 41 63 63 65 73 73 5d 5d 0d 0a 2a 2a 20 5e  .xAccess]]..** ^
ff00: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  The flags argume
ff10: 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20  nt to xAccess() 
ff20: 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  may be [SQLITE_A
ff30: 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0d 0a 2a  CCESS_EXISTS]..*
ff40: 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68  * to test for th
ff50: 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61  e existence of a
ff60: 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54   file, or [SQLIT
ff70: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
ff80: 54 45 5d 20 74 6f 0d 0a 2a 2a 20 74 65 73 74 20  TE] to..** test 
ff90: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69  whether a file i
ffa0: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  s readable and w
ffb0: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c  ritable, or [SQL
ffc0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d  ITE_ACCESS_READ]
ffd0: 0d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65  ..** to test whe
ffe0: 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61  ther a file is a
fff0: 74 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65  t least readable
10000 2e 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e  .   The file can
10010 20 62 65 20 61 0d 0a 2a 2a 20 64 69 72 65 63 74   be a..** direct
10020 6f 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51  ory...**..** ^SQ
10030 4c 69 74 65 20 77 69 6c 6c 20 61 6c 77 61 79 73  Lite will always
10040 20 61 6c 6c 6f 63 61 74 65 20 61 74 20 6c 65 61   allocate at lea
10050 73 74 20 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20  st mxPathname+1 
10060 62 79 74 65 73 20 66 6f 72 20 74 68 65 0d 0a 2a  bytes for the..*
10070 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  * output buffer 
10080 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2e 20 20  xFullPathname.  
10090 54 68 65 20 65 78 61 63 74 20 73 69 7a 65 20 6f  The exact size o
100a0 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  f the output buf
100b0 66 65 72 0d 0a 2a 2a 20 69 73 20 61 6c 73 6f 20  fer..** is also 
100c0 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72 61  passed as a para
100d0 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20 6d  meter to both  m
100e0 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 20 6f  ethods. If the o
100f0 75 74 70 75 74 20 62 75 66 66 65 72 0d 0a 2a 2a  utput buffer..**
10100 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e   is not large en
10110 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 41  ough, [SQLITE_CA
10120 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 62  NTOPEN] should b
10130 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e 63  e returned. Sinc
10140 65 20 74 68 69 73 20 69 73 0d 0a 2a 2a 20 68 61  e this is..** ha
10150 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c  ndled as a fatal
10160 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65   error by SQLite
10170 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61  , vfs implementa
10180 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64  tions should end
10190 65 61 76 6f 72 0d 0a 2a 2a 20 74 6f 20 70 72 65  eavor..** to pre
101a0 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 65 74  vent this by set
101b0 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20  ting mxPathname 
101c0 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c  to a sufficientl
101d0 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0d 0a  y large value...
101e0 2a 2a 0d 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  **..** The xRand
101f0 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70  omness(), xSleep
10200 28 29 2c 20 78 43 75 72 72 65 6e 74 54 69 6d 65  (), xCurrentTime
10210 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74  (), and xCurrent
10220 54 69 6d 65 49 6e 74 36 34 28 29 0d 0a 2a 2a 20  TimeInt64()..** 
10230 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 6e  interfaces are n
10240 6f 74 20 73 74 72 69 63 74 6c 79 20 61 20 70 61  ot strictly a pa
10250 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79  rt of the filesy
10260 73 74 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61  stem, but they a
10270 72 65 0d 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20  re..** included 
10280 69 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 63  in the VFS struc
10290 74 75 72 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74  ture for complet
102a0 65 6e 65 73 73 2e 0d 0a 2a 2a 20 54 68 65 20 78  eness...** The x
102b0 52 61 6e 64 6f 6d 6e 65 73 73 28 29 20 66 75 6e  Randomness() fun
102c0 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ction attempts t
102d0 6f 20 72 65 74 75 72 6e 20 6e 42 79 74 65 73 20  o return nBytes 
102e0 62 79 74 65 73 0d 0a 2a 2a 20 6f 66 20 67 6f 6f  bytes..** of goo
102f0 64 2d 71 75 61 6c 69 74 79 20 72 61 6e 64 6f 6d  d-quality random
10300 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75 74 2e 20  ness into zOut. 
10310 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
10320 65 20 69 73 0d 0a 2a 2a 20 74 68 65 20 61 63 74  e is..** the act
10330 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  ual number of by
10340 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73  tes of randomnes
10350 73 20 6f 62 74 61 69 6e 65 64 2e 0d 0a 2a 2a 20  s obtained...** 
10360 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65 74  The xSleep() met
10370 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20 63  hod causes the c
10380 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74 6f  alling thread to
10390 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0d 0a 2a   sleep for at..*
103a0 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62  * least the numb
103b0 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
103c0 64 73 20 67 69 76 65 6e 2e 20 20 5e 54 68 65 20  ds given.  ^The 
103d0 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0d 0a  xCurrentTime()..
103e0 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e  ** method return
103f0 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e  s a Julian Day N
10400 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 63 75  umber for the cu
10410 72 72 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74  rrent date and t
10420 69 6d 65 20 61 73 0d 0a 2a 2a 20 61 20 66 6c 6f  ime as..** a flo
10430 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
10440 65 2e 0d 0a 2a 2a 20 5e 54 68 65 20 78 43 75 72  e...** ^The xCur
10450 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 29 20  rentTimeInt64() 
10460 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 2c 20  method returns, 
10470 61 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74  as an integer, t
10480 68 65 20 4a 75 6c 69 61 6e 0d 0a 2a 2a 20 44 61  he Julian..** Da
10490 79 20 4e 75 6d 62 65 72 20 6d 75 6c 74 69 70 6c  y Number multipl
104a0 69 65 64 20 62 79 20 38 36 34 30 30 30 30 30 20  ied by 86400000 
104b0 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d  (the number of m
104c0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 69 6e 20 0d  illiseconds in .
104d0 0a 2a 2a 20 61 20 32 34 2d 68 6f 75 72 20 64 61  .** a 24-hour da
104e0 79 29 2e 20 20 0d 0a 2a 2a 20 5e 53 51 4c 69 74  y).  ..** ^SQLit
104f0 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 78  e will use the x
10500 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34  CurrentTimeInt64
10510 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 67 65 74  () method to get
10520 20 74 68 65 20 63 75 72 72 65 6e 74 0d 0a 2a 2a   the current..**
10530 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 69   date and time i
10540 66 20 74 68 61 74 20 6d 65 74 68 6f 64 20 69 73  f that method is
10550 20 61 76 61 69 6c 61 62 6c 65 20 28 69 66 20 69   available (if i
10560 56 65 72 73 69 6f 6e 20 69 73 20 32 20 6f 72 20  Version is 2 or 
10570 0d 0a 2a 2a 20 67 72 65 61 74 65 72 20 61 6e 64  ..** greater and
10580 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f   the function po
10590 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
105a0 4c 29 20 61 6e 64 20 77 69 6c 6c 20 66 61 6c 6c  L) and will fall
105b0 20 62 61 63 6b 0d 0a 2a 2a 20 74 6f 20 78 43 75   back..** to xCu
105c0 72 72 65 6e 74 54 69 6d 65 28 29 20 69 66 20 78  rrentTime() if x
105d0 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34  CurrentTimeInt64
105e0 28 29 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c  () is unavailabl
105f0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  e...**..** ^The 
10600 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c 28 29  xSetSystemCall()
10610 2c 20 78 47 65 74 53 79 73 74 65 6d 43 61 6c 6c  , xGetSystemCall
10620 28 29 2c 20 61 6e 64 20 78 4e 65 73 74 53 79 73  (), and xNestSys
10630 74 65 6d 43 61 6c 6c 28 29 20 69 6e 74 65 72 66  temCall() interf
10640 61 63 65 73 0d 0a 2a 2a 20 61 72 65 20 6e 6f 74  aces..** are not
10650 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c   used by the SQL
10660 69 74 65 20 63 6f 72 65 2e 20 20 54 68 65 73 65  ite core.  These
10670 20 6f 70 74 69 6f 6e 61 6c 20 69 6e 74 65 72 66   optional interf
10680 61 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65  aces are provide
10690 64 0d 0a 2a 2a 20 62 79 20 73 6f 6d 65 20 56 46  d..** by some VF
106a0 53 65 73 20 74 6f 20 66 61 63 69 6c 69 74 61 74  Ses to facilitat
106b0 65 20 74 65 73 74 69 6e 67 20 6f 66 20 74 68 65  e testing of the
106c0 20 56 46 53 20 63 6f 64 65 2e 20 42 79 20 6f 76   VFS code. By ov
106d0 65 72 72 69 64 69 6e 67 20 0d 0a 2a 2a 20 73 79  erriding ..** sy
106e0 73 74 65 6d 20 63 61 6c 6c 73 20 77 69 74 68 20  stem calls with 
106f0 66 75 6e 63 74 69 6f 6e 73 20 75 6e 64 65 72 20  functions under 
10700 69 74 73 20 63 6f 6e 74 72 6f 6c 2c 20 61 20 74  its control, a t
10710 65 73 74 20 70 72 6f 67 72 61 6d 20 63 61 6e 0d  est program can.
10720 0a 2a 2a 20 73 69 6d 75 6c 61 74 65 20 66 61 75  .** simulate fau
10730 6c 74 73 20 61 6e 64 20 65 72 72 6f 72 20 63 6f  lts and error co
10740 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 77 6f  nditions that wo
10750 75 6c 64 20 6f 74 68 65 72 77 69 73 65 20 62 65  uld otherwise be
10760 20 64 69 66 66 69 63 75 6c 74 0d 0a 2a 2a 20 6f   difficult..** o
10770 72 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20  r impossible to 
10780 69 6e 64 75 63 65 2e 20 20 54 68 65 20 73 65 74  induce.  The set
10790 20 6f 66 20 73 79 73 74 65 6d 20 63 61 6c 6c 73   of system calls
107a0 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65   that can be ove
107b0 72 72 69 64 64 65 6e 0d 0a 2a 2a 20 76 61 72 69  rridden..** vari
107c0 65 73 20 66 72 6f 6d 20 6f 6e 65 20 56 46 53 20  es from one VFS 
107d0 74 6f 20 61 6e 6f 74 68 65 72 2c 20 61 6e 64 20  to another, and 
107e0 66 72 6f 6d 20 6f 6e 65 20 76 65 72 73 69 6f 6e  from one version
107f0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 56 46 53   of the same VFS
10800 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 6e 65 78 74   to the..** next
10810 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
10820 74 68 61 74 20 75 73 65 20 74 68 65 73 65 20 69  that use these i
10830 6e 74 65 72 66 61 63 65 73 20 6d 75 73 74 20 62  nterfaces must b
10840 65 20 70 72 65 70 61 72 65 64 20 66 6f 72 20 61  e prepared for a
10850 6e 79 0d 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66  ny..** or all of
10860 20 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65   these interface
10870 73 20 74 6f 20 62 65 20 4e 55 4c 4c 20 6f 72 20  s to be NULL or 
10880 66 6f 72 20 74 68 65 69 72 20 62 65 68 61 76 69  for their behavi
10890 6f 72 20 74 6f 20 63 68 61 6e 67 65 0d 0a 2a 2a  or to change..**
108a0 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73   from one releas
108b0 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 20 20  e to the next.  
108c0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73  Applications mus
108d0 74 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  t not attempt to
108e0 20 61 63 63 65 73 73 0d 0a 2a 2a 20 61 6e 79 20   access..** any 
108f0 6f 66 20 74 68 65 73 65 20 6d 65 74 68 6f 64 73  of these methods
10900 20 69 66 20 74 68 65 20 69 56 65 72 73 69 6f 6e   if the iVersion
10910 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 6c   of the VFS is l
10920 65 73 73 20 74 68 61 6e 20 33 2e 0d 0a 2a 2f 0d  ess than 3...*/.
10930 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
10940 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69  sqlite3_vfs sqli
10950 74 65 33 5f 76 66 73 3b 0d 0a 74 79 70 65 64 65  te3_vfs;..typede
10960 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  f void (*sqlite3
10970 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 28 76 6f  _syscall_ptr)(vo
10980 69 64 29 3b 0d 0a 73 74 72 75 63 74 20 73 71 6c  id);..struct sql
10990 69 74 65 33 5f 76 66 73 20 7b 0d 0a 20 20 69 6e  ite3_vfs {..  in
109a0 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20  t iVersion;     
109b0 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74         /* Struct
109c0 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  ure version numb
109d0 65 72 20 28 63 75 72 72 65 6e 74 6c 79 20 33 29  er (currently 3)
109e0 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 4f 73 46   */..  int szOsF
109f0 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ile;            
10a00 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c  /* Size of subcl
10a10 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69  assed sqlite3_fi
10a20 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 50  le */..  int mxP
10a30 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20  athname;        
10a40 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c    /* Maximum fil
10a50 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74  e pathname lengt
10a60 68 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  h */..  sqlite3_
10a70 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  vfs *pNext;     
10a80 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65   /* Next registe
10a90 72 65 64 20 56 46 53 20 2a 2f 0d 0a 20 20 63 6f  red VFS */..  co
10aa0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
10ab0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
10ac0 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66  f this virtual f
10ad0 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0d 0a 20  ile system */.. 
10ae0 20 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b   void *pAppData;
10af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
10b00 6e 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74  nter to applicat
10b10 69 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74  ion-specific dat
10b20 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 4f  a */..  int (*xO
10b30 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73  pen)(sqlite3_vfs
10b40 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
10b50 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69  Name, sqlite3_fi
10b60 6c 65 2a 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  le*,..          
10b70 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 2c 20       int flags, 
10b80 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b  int *pOutFlags);
10b90 0d 0a 20 20 69 6e 74 20 28 2a 78 44 65 6c 65 74  ..  int (*xDelet
10ba0 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  e)(sqlite3_vfs*,
10bb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
10bc0 6d 65 2c 20 69 6e 74 20 73 79 6e 63 44 69 72 29  me, int syncDir)
10bd0 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 41 63 63 65  ;..  int (*xAcce
10be0 73 73 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ss)(sqlite3_vfs*
10bf0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
10c00 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20  ame, int flags, 
10c10 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0d 0a  int *pResOut);..
10c20 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50 61 74    int (*xFullPat
10c30 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 76  hname)(sqlite3_v
10c40 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
10c50 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f 75 74  *zName, int nOut
10c60 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0d 0a  , char *zOut);..
10c70 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70 65    void *(*xDlOpe
10c80 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  n)(sqlite3_vfs*,
10c90 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
10ca0 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 76 6f 69 64  lename);..  void
10cb0 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c   (*xDlError)(sql
10cc0 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
10cd0 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72  Byte, char *zErr
10ce0 4d 73 67 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a  Msg);..  void (*
10cf0 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65  (*xDlSym)(sqlite
10d00 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f  3_vfs*,void*, co
10d10 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f  nst char *zSymbo
10d20 6c 29 29 28 76 6f 69 64 29 3b 0d 0a 20 20 76 6f  l))(void);..  vo
10d30 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73  id (*xDlClose)(s
10d40 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69  qlite3_vfs*, voi
10d50 64 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 52  d*);..  int (*xR
10d60 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74  andomness)(sqlit
10d70 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79  e3_vfs*, int nBy
10d80 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b  te, char *zOut);
10d90 0d 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70  ..  int (*xSleep
10da0 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
10db0 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73  int microseconds
10dc0 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 43 75 72  );..  int (*xCur
10dd0 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65  rentTime)(sqlite
10de0 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29  3_vfs*, double*)
10df0 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c  ;..  int (*xGetL
10e00 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65  astError)(sqlite
10e10 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61  3_vfs*, int, cha
10e20 72 20 2a 29 3b 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  r *);..  /*..  *
10e30 2a 20 54 68 65 20 6d 65 74 68 6f 64 73 20 61 62  * The methods ab
10e40 6f 76 65 20 61 72 65 20 69 6e 20 76 65 72 73 69  ove are in versi
10e50 6f 6e 20 31 20 6f 66 20 74 68 65 20 73 71 6c 69  on 1 of the sqli
10e60 74 65 5f 76 66 73 20 6f 62 6a 65 63 74 0d 0a 20  te_vfs object.. 
10e70 20 2a 2a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20   ** definition. 
10e80 20 54 68 6f 73 65 20 74 68 61 74 20 66 6f 6c 6c   Those that foll
10e90 6f 77 20 61 72 65 20 61 64 64 65 64 20 69 6e 20  ow are added in 
10ea0 76 65 72 73 69 6f 6e 20 32 20 6f 72 20 6c 61 74  version 2 or lat
10eb0 65 72 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20  er..  */..  int 
10ec0 28 2a 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e  (*xCurrentTimeIn
10ed0 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 66 73  t64)(sqlite3_vfs
10ee0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
10ef0 2a 29 3b 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  *);..  /*..  ** 
10f00 54 68 65 20 6d 65 74 68 6f 64 73 20 61 62 6f 76  The methods abov
10f10 65 20 61 72 65 20 69 6e 20 76 65 72 73 69 6f 6e  e are in version
10f20 73 20 31 20 61 6e 64 20 32 20 6f 66 20 74 68 65  s 1 and 2 of the
10f30 20 73 71 6c 69 74 65 5f 76 66 73 20 6f 62 6a 65   sqlite_vfs obje
10f40 63 74 2e 0d 0a 20 20 2a 2a 20 54 68 6f 73 65 20  ct...  ** Those 
10f50 62 65 6c 6f 77 20 61 72 65 20 66 6f 72 20 76 65  below are for ve
10f60 72 73 69 6f 6e 20 33 20 61 6e 64 20 67 72 65 61  rsion 3 and grea
10f70 74 65 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e  ter...  */..  in
10f80 74 20 28 2a 78 53 65 74 53 79 73 74 65 6d 43 61  t (*xSetSystemCa
10f90 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ll)(sqlite3_vfs*
10fa0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
10fb0 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 73 79 73  ame, sqlite3_sys
10fc0 63 61 6c 6c 5f 70 74 72 29 3b 0d 0a 20 20 73 71  call_ptr);..  sq
10fd0 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
10fe0 72 20 28 2a 78 47 65 74 53 79 73 74 65 6d 43 61  r (*xGetSystemCa
10ff0 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ll)(sqlite3_vfs*
11000 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
11010 61 6d 65 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  ame);..  const c
11020 68 61 72 20 2a 28 2a 78 4e 65 78 74 53 79 73 74  har *(*xNextSyst
11030 65 6d 43 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f  emCall)(sqlite3_
11040 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
11050 20 2a 7a 4e 61 6d 65 29 3b 0d 0a 20 20 2f 2a 0d   *zName);..  /*.
11060 0a 20 20 2a 2a 20 54 68 65 20 6d 65 74 68 6f 64  .  ** The method
11070 73 20 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76  s above are in v
11080 65 72 73 69 6f 6e 73 20 31 20 74 68 72 6f 75 67  ersions 1 throug
11090 68 20 33 20 6f 66 20 74 68 65 20 73 71 6c 69 74  h 3 of the sqlit
110a0 65 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0d 0a 20  e_vfs object... 
110b0 20 2a 2a 20 4e 65 77 20 66 69 65 6c 64 73 20 6d   ** New fields m
110c0 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 69  ay be appended i
110d0 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 6f 6e  n figure version
110e0 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f 6e  s.  The iVersion
110f0 0d 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c  ..  ** value wil
11100 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e  l increment when
11110 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 6e  ever this happen
11120 73 2e 20 0d 0a 20 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  s. ..  */..};...
11130 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
11140 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20  : Flags for the 
11150 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68  xAccess VFS meth
11160 6f 64 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65  od..**..** These
11170 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
11180 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  ts can be used a
11190 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  s the third para
111a0 6d 65 74 65 72 20 74 6f 0d 0a 2a 2a 20 74 68 65  meter to..** the
111b0 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
111c0 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76  of an [sqlite3_v
111d0 66 73 5d 20 6f 62 6a 65 63 74 2e 20 20 54 68 65  fs] object.  The
111e0 79 20 64 65 74 65 72 6d 69 6e 65 0d 0a 2a 2a 20  y determine..** 
111f0 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 72  what kind of per
11200 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41 63  missions the xAc
11210 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20 6c  cess method is l
11220 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0d 0a 2a 2a 20  ooking for...** 
11230 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45  With SQLITE_ACCE
11240 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78  SS_EXISTS, the x
11250 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0d 0a 2a  Access method..*
11260 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20  * simply checks 
11270 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65  whether the file
11280 20 65 78 69 73 74 73 2e 0d 0a 2a 2a 20 57 69 74   exists...** Wit
11290 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  h SQLITE_ACCESS_
112a0 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78  READWRITE, the x
112b0 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0d 0a 2a  Access method..*
112c0 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72  * checks whether
112d0 20 74 68 65 20 6e 61 6d 65 64 20 64 69 72 65 63   the named direc
112e0 74 6f 72 79 20 69 73 20 62 6f 74 68 20 72 65 61  tory is both rea
112f0 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62  dable and writab
11300 6c 65 0d 0a 2a 2a 20 28 69 6e 20 6f 74 68 65 72  le..** (in other
11310 20 77 6f 72 64 73 2c 20 69 66 20 66 69 6c 65 73   words, if files
11320 20 63 61 6e 20 62 65 20 61 64 64 65 64 2c 20 72   can be added, r
11330 65 6d 6f 76 65 64 2c 20 61 6e 64 20 72 65 6e 61  emoved, and rena
11340 6d 65 64 20 77 69 74 68 69 6e 0d 0a 2a 2a 20 74  med within..** t
11350 68 65 20 64 69 72 65 63 74 6f 72 79 29 2e 0d 0a  he directory)...
11360 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 43  ** The SQLITE_AC
11370 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 63  CESS_READWRITE c
11380 6f 6e 73 74 61 6e 74 20 69 73 20 63 75 72 72 65  onstant is curre
11390 6e 74 6c 79 20 75 73 65 64 20 6f 6e 6c 79 20 62  ntly used only b
113a0 79 20 74 68 65 0d 0a 2a 2a 20 5b 74 65 6d 70 5f  y the..** [temp_
113b0 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
113c0 70 72 61 67 6d 61 5d 2c 20 74 68 6f 75 67 68 20  pragma], though 
113d0 74 68 69 73 20 63 6f 75 6c 64 20 63 68 61 6e 67  this could chang
113e0 65 20 69 6e 20 61 20 66 75 74 75 72 65 0d 0a 2a  e in a future..*
113f0 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c  * release of SQL
11400 69 74 65 2e 0d 0a 2a 2a 20 57 69 74 68 20 53 51  ite...** With SQ
11410 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
11420 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65  , the xAccess me
11430 74 68 6f 64 0d 0a 2a 2a 20 63 68 65 63 6b 73 20  thod..** checks 
11440 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65  whether the file
11450 20 69 73 20 72 65 61 64 61 62 6c 65 2e 20 20 54   is readable.  T
11460 68 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  he SQLITE_ACCESS
11470 5f 52 45 41 44 20 63 6f 6e 73 74 61 6e 74 20 69  _READ constant i
11480 73 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  s..** currently 
11490 75 6e 75 73 65 64 2c 20 74 68 6f 75 67 68 20 69  unused, though i
114a0 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 20  t might be used 
114b0 69 6e 20 61 20 66 75 74 75 72 65 20 72 65 6c 65  in a future rele
114c0 61 73 65 20 6f 66 0d 0a 2a 2a 20 53 51 4c 69 74  ase of..** SQLit
114d0 65 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  e...*/..#define 
114e0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
114f0 49 53 54 53 20 20 20 20 30 0d 0a 23 64 65 66 69  ISTS    0..#defi
11500 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  ne SQLITE_ACCESS
11510 5f 52 45 41 44 57 52 49 54 45 20 31 20 20 20 2f  _READWRITE 1   /
11520 2a 20 55 73 65 64 20 62 79 20 50 52 41 47 4d 41  * Used by PRAGMA
11530 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
11540 63 74 6f 72 79 20 2a 2f 0d 0a 23 64 65 66 69 6e  ctory */..#defin
11550 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
11560 52 45 41 44 20 20 20 20 20 20 32 20 20 20 2f 2a  READ      2   /*
11570 20 55 6e 75 73 65 64 20 2a 2f 0d 0a 0d 0a 2f 2a   Unused */..../*
11580 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  ..** CAPI3REF: F
11590 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 53 68  lags for the xSh
115a0 6d 4c 6f 63 6b 20 56 46 53 20 6d 65 74 68 6f 64  mLock VFS method
115b0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 69  ..**..** These i
115c0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
115d0 20 64 65 66 69 6e 65 20 74 68 65 20 76 61 72 69   define the vari
115e0 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  ous locking oper
115f0 61 74 69 6f 6e 73 0d 0a 2a 2a 20 61 6c 6c 6f 77  ations..** allow
11600 65 64 20 62 79 20 74 68 65 20 78 53 68 6d 4c 6f  ed by the xShmLo
11610 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 5b 73 71  ck method of [sq
11620 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
11630 5d 2e 20 20 54 68 65 0d 0a 2a 2a 20 66 6f 6c 6c  ].  The..** foll
11640 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 6f 6e  owing are the on
11650 6c 79 20 6c 65 67 61 6c 20 63 6f 6d 62 69 6e 61  ly legal combina
11660 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 20 74  tions of flags t
11670 6f 20 74 68 65 0d 0a 2a 2a 20 78 53 68 6d 4c 6f  o the..** xShmLo
11680 63 6b 20 6d 65 74 68 6f 64 3a 0d 0a 2a 2a 0d 0a  ck method:..**..
11690 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e  ** <ul>..** <li>
116a0 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43    SQLITE_SHM_LOC
116b0 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53  K | SQLITE_SHM_S
116c0 48 41 52 45 44 0d 0a 2a 2a 20 3c 6c 69 3e 20 20  HARED..** <li>  
116d0 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20  SQLITE_SHM_LOCK 
116e0 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43  | SQLITE_SHM_EXC
116f0 4c 55 53 49 56 45 0d 0a 2a 2a 20 3c 6c 69 3e 20  LUSIVE..** <li> 
11700 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f   SQLITE_SHM_UNLO
11710 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f  CK | SQLITE_SHM_
11720 53 48 41 52 45 44 0d 0a 2a 2a 20 3c 6c 69 3e 20  SHARED..** <li> 
11730 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f   SQLITE_SHM_UNLO
11740 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f  CK | SQLITE_SHM_
11750 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 3c 2f  EXCLUSIVE..** </
11760 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e  ul>..**..** When
11770 20 75 6e 6c 6f 63 6b 69 6e 67 2c 20 74 68 65 20   unlocking, the 
11780 73 61 6d 65 20 53 48 41 52 45 44 20 6f 72 20 45  same SHARED or E
11790 58 43 4c 55 53 49 56 45 20 66 6c 61 67 20 6d 75  XCLUSIVE flag mu
117a0 73 74 20 62 65 20 73 75 70 70 6c 69 65 64 20 61  st be supplied a
117b0 73 0d 0a 2a 2a 20 77 61 73 20 67 69 76 65 6e 20  s..** was given 
117c0 6e 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  no the correspon
117d0 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 0d 0a 2a 2a  ding lock.  ..**
117e0 0d 0a 2a 2a 20 54 68 65 20 78 53 68 6d 4c 6f 63  ..** The xShmLoc
117f0 6b 20 6d 65 74 68 6f 64 20 63 61 6e 20 74 72 61  k method can tra
11800 6e 73 69 74 69 6f 6e 20 62 65 74 77 65 65 6e 20  nsition between 
11810 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20 53 48 41  unlocked and SHA
11820 52 45 44 20 6f 72 0d 0a 2a 2a 20 62 65 74 77 65  RED or..** betwe
11830 65 6e 20 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20  en unlocked and 
11840 45 58 43 4c 55 53 49 56 45 2e 20 20 49 74 20 63  EXCLUSIVE.  It c
11850 61 6e 6e 6f 74 20 74 72 61 6e 73 69 74 69 6f 6e  annot transition
11860 20 62 65 74 77 65 65 6e 20 53 48 41 52 45 44 0d   between SHARED.
11870 0a 2a 2a 20 61 6e 64 20 45 58 43 4c 55 53 49 56  .** and EXCLUSIV
11880 45 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  E...*/..#define 
11890 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43  SQLITE_SHM_UNLOC
118a0 4b 20 20 20 20 20 20 20 31 0d 0a 23 64 65 66 69  K       1..#defi
118b0 6e 65 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f  ne SQLITE_SHM_LO
118c0 43 4b 20 20 20 20 20 20 20 20 20 32 0d 0a 23 64  CK         2..#d
118d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 48 4d  efine SQLITE_SHM
118e0 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 34 0d  _SHARED       4.
118f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
11900 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 20 20 20  SHM_EXCLUSIVE   
11910 20 38 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50   8..../*..** CAP
11920 49 33 52 45 46 3a 20 4d 61 78 69 6d 75 6d 20 78  I3REF: Maximum x
11930 53 68 6d 4c 6f 63 6b 20 69 6e 64 65 78 0d 0a 2a  ShmLock index..*
11940 2a 0d 0a 2a 2a 20 54 68 65 20 78 53 68 6d 4c 6f  *..** The xShmLo
11950 63 6b 20 6d 65 74 68 6f 64 20 6f 6e 20 5b 73 71  ck method on [sq
11960 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
11970 5d 20 6d 61 79 20 75 73 65 20 76 61 6c 75 65 73  ] may use values
11980 0d 0a 2a 2a 20 62 65 74 77 65 65 6e 20 30 20 61  ..** between 0 a
11990 6e 64 20 74 68 69 73 20 75 70 70 65 72 20 62 6f  nd this upper bo
119a0 75 6e 64 20 61 73 20 69 74 73 20 22 6f 66 66 73  und as its "offs
119b0 65 74 22 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a  et" argument...*
119c0 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  * The SQLite cor
119d0 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 74 74  e will never att
119e0 65 6d 70 74 20 74 6f 20 61 63 71 75 69 72 65 20  empt to acquire 
119f0 6f 72 20 72 65 6c 65 61 73 65 20 61 0d 0a 2a 2a  or release a..**
11a00 20 6c 6f 63 6b 20 6f 75 74 73 69 64 65 20 6f 66   lock outside of
11a10 20 74 68 69 73 20 72 61 6e 67 65 0d 0a 2a 2f 0d   this range..*/.
11a20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
11a30 53 48 4d 5f 4e 4c 4f 43 4b 20 20 20 20 20 20 20  SHM_NLOCK       
11a40 20 38 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43   8....../*..** C
11a50 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c  API3REF: Initial
11a60 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c  ize The SQLite L
11a70 69 62 72 61 72 79 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ibrary..**..** ^
11a80 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
11a90 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
11aa0 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
11ab0 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72  ..** SQLite libr
11ac0 61 72 79 2e 20 20 5e 54 68 65 20 73 71 6c 69 74  ary.  ^The sqlit
11ad0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f  e3_shutdown() ro
11ae0 75 74 69 6e 65 0d 0a 2a 2a 20 64 65 61 6c 6c 6f  utine..** deallo
11af0 63 61 74 65 73 20 61 6e 79 20 72 65 73 6f 75 72  cates any resour
11b00 63 65 73 20 74 68 61 74 20 77 65 72 65 20 61 6c  ces that were al
11b10 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
11b20 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e  e3_initialize().
11b30 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  ..** These routi
11b40 6e 65 73 20 61 72 65 20 64 65 73 69 67 6e 65 64  nes are designed
11b50 20 74 6f 20 61 69 64 20 69 6e 20 70 72 6f 63 65   to aid in proce
11b60 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ss initializatio
11b70 6e 20 61 6e 64 0d 0a 2a 2a 20 73 68 75 74 64 6f  n and..** shutdo
11b80 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  wn on embedded s
11b90 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b 73 74 61  ystems.  Worksta
11ba0 74 69 6f 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  tion application
11bb0 73 20 75 73 69 6e 67 0d 0a 2a 2a 20 53 51 4c 69  s using..** SQLi
11bc0 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f 20 6e  te normally do n
11bd0 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b  ot need to invok
11be0 65 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73  e either of thes
11bf0 65 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a 2a 0d  e routines...**.
11c00 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
11c10 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
11c20 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
11c30 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
11c40 69 73 0d 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  is..** the first
11c50 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e   time sqlite3_in
11c60 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e  itialize() is in
11c70 76 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65  voked during the
11c80 20 6c 69 66 65 74 69 6d 65 20 6f 66 0d 0a 2a 2a   lifetime of..**
11c90 20 74 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72   the process, or
11ca0 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69   if it is the fi
11cb0 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33  rst time sqlite3
11cc0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73  _initialize() is
11cd0 20 69 6e 76 6f 6b 65 64 0d 0a 2a 2a 20 66 6f 6c   invoked..** fol
11ce0 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f  lowing a call to
11cf0 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
11d00 6e 28 29 2e 20 20 5e 28 4f 6e 6c 79 20 61 6e 20  n().  ^(Only an 
11d10 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 0d 0a  effective call..
11d20 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  ** of sqlite3_in
11d30 69 74 69 61 6c 69 7a 65 28 29 20 64 6f 65 73 20  itialize() does 
11d40 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  any initializati
11d50 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 63  on.  All other c
11d60 61 6c 6c 73 0d 0a 2a 2a 20 61 72 65 20 68 61 72  alls..** are har
11d70 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 29 5e 0d  mless no-ops.)^.
11d80 0a 2a 2a 0d 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  .**..** A call t
11d90 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  o sqlite3_shutdo
11da0 77 6e 28 29 20 69 73 20 61 6e 20 22 65 66 66 65  wn() is an "effe
11db0 63 74 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69  ctive" call if i
11dc0 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0d 0a  t is the first..
11dd0 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
11de0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 73 69  e3_shutdown() si
11df0 6e 63 65 20 74 68 65 20 6c 61 73 74 20 73 71 6c  nce the last sql
11e00 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
11e10 29 2e 20 20 5e 28 4f 6e 6c 79 0d 0a 2a 2a 20 61  ).  ^(Only..** a
11e20 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c  n effective call
11e30 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74   to sqlite3_shut
11e40 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79 20  down() does any 
11e50 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  deinitialization
11e60 2e 0d 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  ...** All other 
11e70 76 61 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73  valid calls to s
11e80 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
11e90 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  ) are harmless n
11ea0 6f 2d 6f 70 73 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  o-ops.)^..**..**
11eb0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69   The sqlite3_ini
11ec0 74 69 61 6c 69 7a 65 28 29 20 69 6e 74 65 72 66  tialize() interf
11ed0 61 63 65 20 69 73 20 74 68 72 65 61 64 73 61 66  ace is threadsaf
11ee0 65 2c 20 62 75 74 20 73 71 6c 69 74 65 33 5f 73  e, but sqlite3_s
11ef0 68 75 74 64 6f 77 6e 28 29 0d 0a 2a 2a 20 69 73  hutdown()..** is
11f00 20 6e 6f 74 2e 20 20 54 68 65 20 73 71 6c 69 74   not.  The sqlit
11f10 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 69 6e  e3_shutdown() in
11f20 74 65 72 66 61 63 65 20 6d 75 73 74 20 6f 6e 6c  terface must onl
11f30 79 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  y be called from
11f40 20 61 0d 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68   a..** single th
11f50 72 65 61 64 2e 20 20 41 6c 6c 20 6f 70 65 6e 20  read.  All open 
11f60 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
11f70 74 69 6f 6e 73 5d 20 6d 75 73 74 20 62 65 20 63  tions] must be c
11f80 6c 6f 73 65 64 20 61 6e 64 20 61 6c 6c 0d 0a 2a  losed and all..*
11f90 2a 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 72  * other SQLite r
11fa0 65 73 6f 75 72 63 65 73 20 6d 75 73 74 20 62 65  esources must be
11fb0 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 70 72 69   deallocated pri
11fc0 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 0d 0a  or to invoking..
11fd0 2a 2a 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  ** sqlite3_shutd
11fe0 6f 77 6e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41  own()...**..** A
11ff0 6d 6f 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67  mong other thing
12000 73 2c 20 5e 73 71 6c 69 74 65 33 5f 69 6e 69 74  s, ^sqlite3_init
12010 69 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 69 6e  ialize() will in
12020 76 6f 6b 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  voke..** sqlite3
12030 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d  _os_init().  Sim
12040 69 6c 61 72 6c 79 2c 20 5e 73 71 6c 69 74 65 33  ilarly, ^sqlite3
12050 5f 73 68 75 74 64 6f 77 6e 28 29 0d 0a 2a 2a 20  _shutdown()..** 
12060 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69  will invoke sqli
12070 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0d 0a 2a  te3_os_end()...*
12080 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  *..** ^The sqlit
12090 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
120a0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
120b0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73  [SQLITE_OK] on s
120c0 75 63 63 65 73 73 2e 0d 0a 2a 2a 20 5e 49 66 20  uccess...** ^If 
120d0 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c  for some reason,
120e0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
120f0 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c 65 20  ize() is unable 
12100 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0d 0a 2a  to initialize..*
12110 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 70  * the library (p
12120 65 72 68 61 70 73 20 69 74 20 69 73 20 75 6e 61  erhaps it is una
12130 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
12140 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 63  a needed resourc
12150 65 20 73 75 63 68 0d 0a 2a 2a 20 61 73 20 61 20  e such..** as a 
12160 6d 75 74 65 78 29 20 69 74 20 72 65 74 75 72 6e  mutex) it return
12170 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  s an [error code
12180 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51  ] other than [SQ
12190 4c 49 54 45 5f 4f 4b 5d 2e 0d 0a 2a 2a 0d 0a 2a  LITE_OK]...**..*
121a0 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 69  * ^The sqlite3_i
121b0 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74  nitialize() rout
121c0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e  ine is called in
121d0 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79  ternally by many
121e0 20 6f 74 68 65 72 0d 0a 2a 2a 20 53 51 4c 69 74   other..** SQLit
121f0 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f 20  e interfaces so 
12200 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74  that an applicat
12210 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 73  ion usually does
12220 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0d 0a 2a 2a   not need to..**
12230 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
12240 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72  initialize() dir
12250 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d  ectly.  For exam
12260 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ple, [sqlite3_op
12270 65 6e 28 29 5d 0d 0a 2a 2a 20 63 61 6c 6c 73 20  en()]..** calls 
12280 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
12290 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69  ze() so the SQLi
122a0 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20  te library will 
122b0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
122c0 0d 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64  ..** initialized
122d0 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f   when [sqlite3_o
122e0 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  pen()] is called
122f0 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62   if it has not b
12300 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0d 0a 2a  e initialized..*
12310 2a 20 61 6c 72 65 61 64 79 2e 20 20 5e 48 6f 77  * already.  ^How
12320 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20  ever, if SQLite 
12330 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
12340 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49   the [SQLITE_OMI
12350 54 5f 41 55 54 4f 49 4e 49 54 5d 0d 0a 2a 2a 20  T_AUTOINIT]..** 
12360 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
12370 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 75  ion, then the au
12380 74 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 6f  tomatic calls to
12390 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
123a0 69 7a 65 28 29 0d 0a 2a 2a 20 61 72 65 20 6f 6d  ize()..** are om
123b0 69 74 74 65 64 20 61 6e 64 20 74 68 65 20 61 70  itted and the ap
123c0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63  plication must c
123d0 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  all sqlite3_init
123e0 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c  ialize() directl
123f0 79 0d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75  y..** prior to u
12400 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53  sing any other S
12410 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e  QLite interface.
12420 20 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f    For maximum po
12430 72 74 61 62 69 6c 69 74 79 2c 0d 0a 2a 2a 20 69  rtability,..** i
12440 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  t is recommended
12450 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f   that applicatio
12460 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b 65  ns always invoke
12470 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
12480 69 7a 65 28 29 0d 0a 2a 2a 20 64 69 72 65 63 74  ize()..** direct
12490 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e  ly prior to usin
124a0 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69  g any other SQLi
124b0 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46  te interface.  F
124c0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 0d 0a  uture releases..
124d0 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79  ** of SQLite may
124e0 20 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 20   require this.  
124f0 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
12500 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 68  the behavior exh
12510 69 62 69 74 65 64 0d 0a 2a 2a 20 77 68 65 6e 20  ibited..** when 
12520 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
12530 65 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f  ed with [SQLITE_
12540 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d  OMIT_AUTOINIT] m
12550 69 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0d  ight become the.
12560 0a 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61  .** default beha
12570 76 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74  vior in some fut
12580 75 72 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53  ure release of S
12590 51 4c 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  QLite...**..** T
125a0 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  he sqlite3_os_in
125b0 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  it() routine doe
125c0 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74  s operating-syst
125d0 65 6d 20 73 70 65 63 69 66 69 63 0d 0a 2a 2a 20  em specific..** 
125e0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f  initialization o
125f0 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  f the SQLite lib
12600 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74  rary.  The sqlit
12610 65 33 5f 6f 73 5f 65 6e 64 28 29 0d 0a 2a 2a 20  e3_os_end()..** 
12620 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74  routine undoes t
12630 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c  he effect of sql
12640 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
12650 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0d 0a   Typical tasks..
12660 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20  ** performed by 
12670 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69  these routines i
12680 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f  nclude allocatio
12690 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f  n or deallocatio
126a0 6e 0d 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20  n..** of static 
126b0 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69  resources, initi
126c0 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f  alization of glo
126d0 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c 0d 0a  bal variables,..
126e0 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 20  ** setting up a 
126f0 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
12700 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72  _vfs] module, or
12710 20 73 65 74 74 69 6e 67 20 75 70 0d 0a 2a 2a 20   setting up..** 
12720 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  a default config
12730 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73  uration using [s
12740 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
12750 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 70  ...**..** The ap
12760 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64  plication should
12770 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69   never invoke ei
12780 74 68 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ther sqlite3_os_
12790 69 6e 69 74 28 29 0d 0a 2a 2a 20 6f 72 20 73 71  init()..** or sq
127a0 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64  lite3_os_end() d
127b0 69 72 65 63 74 6c 79 2e 20 20 54 68 65 20 61 70  irectly.  The ap
127c0 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64  plication should
127d0 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0d 0a 2a 2a   only invoke..**
127e0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
127f0 69 7a 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ize() and sqlite
12800 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 54  3_shutdown().  T
12810 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  he sqlite3_os_in
12820 69 74 28 29 0d 0a 2a 2a 20 69 6e 74 65 72 66 61  it()..** interfa
12830 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 75 74  ce is called aut
12840 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 71  omatically by sq
12850 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
12860 28 29 20 61 6e 64 0d 0a 2a 2a 20 73 71 6c 69 74  () and..** sqlit
12870 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 63  e3_os_end() is c
12880 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33  alled by sqlite3
12890 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 70  _shutdown().  Ap
128a0 70 72 6f 70 72 69 61 74 65 0d 0a 2a 2a 20 69 6d  propriate..** im
128b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
128c0 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  r sqlite3_os_ini
128d0 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
128e0 6f 73 5f 65 6e 64 28 29 0d 0a 2a 2a 20 61 72 65  os_end()..** are
128f0 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69   built into SQLi
12900 74 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f  te when it is co
12910 6d 70 69 6c 65 64 20 66 6f 72 20 55 6e 69 78 2c  mpiled for Unix,
12920 20 57 69 6e 64 6f 77 73 2c 20 6f 72 20 4f 53 2f   Windows, or OS/
12930 32 2e 0d 0a 2a 2a 20 57 68 65 6e 20 5b 63 75 73  2...** When [cus
12940 74 6f 6d 20 62 75 69 6c 64 73 20 7c 20 62 75 69  tom builds | bui
12950 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61  lt for other pla
12960 74 66 6f 72 6d 73 5d 0d 0a 2a 2a 20 28 75 73 69  tforms]..** (usi
12970 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  ng the [SQLITE_O
12980 53 5f 4f 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69  S_OTHER=1] compi
12990 6c 65 2d 74 69 6d 65 0d 0a 2a 2a 20 6f 70 74 69  le-time..** opti
129a0 6f 6e 29 20 74 68 65 20 61 70 70 6c 69 63 61 74  on) the applicat
129b0 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 20  ion must supply 
129c0 61 20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c 65  a suitable imple
129d0 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 0d 0a 2a  mentation for..*
129e0 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  * sqlite3_os_ini
129f0 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
12a00 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20 61 70  os_end().  An ap
12a10 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69  plication-suppli
12a20 65 64 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ed..** implement
12a30 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
12a40 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73 71  _os_init() or sq
12a50 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0d 0a  lite3_os_end()..
12a60 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b  ** must return [
12a70 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
12a80 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f  ccess and some o
12a90 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65  ther [error code
12aa0 5d 20 75 70 6f 6e 0d 0a 2a 2a 20 66 61 69 6c 75  ] upon..** failu
12ab0 72 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  re...*/..SQLITE_
12ac0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
12ad0 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  initialize(void)
12ae0 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ;..SQLITE_API in
12af0 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  t sqlite3_shutdo
12b00 77 6e 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54  wn(void);..SQLIT
12b10 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12b20 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b  3_os_init(void);
12b30 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
12b40 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
12b50 76 6f 69 64 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  void);..../*..**
12b60 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
12b70 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74  guring The SQLit
12b80 65 20 4c 69 62 72 61 72 79 0d 0a 2a 2a 0d 0a 2a  e Library..**..*
12b90 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
12ba0 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65  nfig() interface
12bb0 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   is used to make
12bc0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
12bd0 61 74 69 6f 6e 0d 0a 2a 2a 20 63 68 61 6e 67 65  ation..** change
12be0 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f  s to SQLite in o
12bf0 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c  rder to tune SQL
12c00 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 69  ite to the speci
12c10 66 69 63 20 6e 65 65 64 73 20 6f 66 0d 0a 2a 2a  fic needs of..**
12c20 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
12c30 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63  .  The default c
12c40 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20  onfiguration is 
12c50 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
12c60 6d 6f 73 74 0d 0a 2a 2a 20 61 70 70 6c 69 63 61  most..** applica
12c70 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69  tions and so thi
12c80 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
12c90 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61  ally not necessa
12ca0 72 79 2e 20 20 49 74 20 69 73 0d 0a 2a 2a 20 70  ry.  It is..** p
12cb0 72 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f  rovided to suppo
12cc0 72 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74  rt rare applicat
12cd0 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61  ions with unusua
12ce0 6c 20 6e 65 65 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  l needs...**..**
12cf0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e   The sqlite3_con
12d00 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
12d10 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
12d20 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74  e.  The applicat
12d30 69 6f 6e 0d 0a 2a 2a 20 6d 75 73 74 20 69 6e 73  ion..** must ins
12d40 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  ure that no othe
12d50 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  r SQLite interfa
12d60 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20  ces are invoked 
12d70 62 79 20 6f 74 68 65 72 0d 0a 2a 2a 20 74 68 72  by other..** thr
12d80 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74  eads while sqlit
12d90 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72  e3_config() is r
12da0 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72  unning.  Further
12db0 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f  more, sqlite3_co
12dc0 6e 66 69 67 28 29 0d 0a 2a 2a 20 6d 61 79 20 6f  nfig()..** may o
12dd0 6e 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70  nly be invoked p
12de0 72 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20  rior to library 
12df0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75  initialization u
12e00 73 69 6e 67 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65  sing..** [sqlite
12e10 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20  3_initialize()] 
12e20 6f 72 20 61 66 74 65 72 20 73 68 75 74 64 6f 77  or after shutdow
12e30 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68  n by [sqlite3_sh
12e40 75 74 64 6f 77 6e 28 29 5d 2e 0d 0a 2a 2a 20 5e  utdown()]...** ^
12e50 49 66 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  If sqlite3_confi
12e60 67 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 66  g() is called af
12e70 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69  ter [sqlite3_ini
12e80 74 69 61 6c 69 7a 65 28 29 5d 20 61 6e 64 20 62  tialize()] and b
12e90 65 66 6f 72 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74  efore..** [sqlit
12ea0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 74  e3_shutdown()] t
12eb0 68 65 6e 20 69 74 20 77 69 6c 6c 20 72 65 74 75  hen it will retu
12ec0 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
12ed0 2e 0d 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65  ...** Note, howe
12ee0 76 65 72 2c 20 74 68 61 74 20 5e 73 71 6c 69 74  ver, that ^sqlit
12ef0 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20  e3_config() can 
12f00 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72  be called as par
12f10 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 69 6d 70  t of the..** imp
12f20 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
12f30 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
12f40 66 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f  fined [sqlite3_o
12f50 73 5f 69 6e 69 74 28 29 5d 2e 0d 0a 2a 2a 0d 0a  s_init()]...**..
12f60 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
12f70 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
12f80 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 6e 20  _config() is an 
12f90 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 5b 63 6f 6e  integer..** [con
12fa0 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
12fb0 6e 5d 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  n] that determin
12fc0 65 73 0d 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70  es..** what prop
12fd0 65 72 74 79 20 6f 66 20 53 51 4c 69 74 65 20 69  erty of SQLite i
12fe0 73 20 74 6f 20 62 65 20 63 6f 6e 66 69 67 75 72  s to be configur
12ff0 65 64 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20  ed.  Subsequent 
13000 61 72 67 75 6d 65 6e 74 73 0d 0a 2a 2a 20 76 61  arguments..** va
13010 72 79 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ry depending on 
13020 74 68 65 20 5b 63 6f 6e 66 69 67 75 72 61 74 69  the [configurati
13030 6f 6e 20 6f 70 74 69 6f 6e 5d 0d 0a 2a 2a 20 69  on option]..** i
13040 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  n the first argu
13050 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 57  ment...**..** ^W
13060 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74  hen a configurat
13070 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65  ion option is se
13080 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  t, sqlite3_confi
13090 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  g() returns [SQL
130a0 49 54 45 5f 4f 4b 5d 2e 0d 0a 2a 2a 20 5e 49 66  ITE_OK]...** ^If
130b0 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75   the option is u
130c0 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65  nknown or SQLite
130d0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65   is unable to se
130e0 74 20 74 68 65 20 6f 70 74 69 6f 6e 0d 0a 2a 2a  t the option..**
130f0 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
13100 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e  ne returns a non
13110 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
13120 65 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  e]...*/..SQLITE_
13130 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
13140 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29  config(int, ...)
13150 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
13160 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20  3REF: Configure 
13170 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
13180 69 6f 6e 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ions..**..** The
13190 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
131a0 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  ig() interface i
131b0 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 63  s used to make c
131c0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0d 0a 2a 2a  onfiguration..**
131d0 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64   changes to a [d
131e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
131f0 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66  on].  The interf
13200 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ace is similar t
13210 6f 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  o..** [sqlite3_c
13220 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 20  onfig()] except 
13230 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73  that the changes
13240 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e 67   apply to a sing
13250 6c 65 0d 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  le..** [database
13260 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70   connection] (sp
13270 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 66  ecified in the f
13280 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 0d  irst argument)..
13290 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 65 63 6f  .**..** The seco
132a0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
132b0 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
132c0 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68  (D,V,...)  is th
132d0 65 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 42  e..** [SQLITE_DB
132e0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
132f0 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e   | configuration
13300 20 76 65 72 62 5d 20 2d 20 61 6e 20 69 6e 74 65   verb] - an inte
13310 67 65 72 20 63 6f 64 65 20 0d 0a 2a 2a 20 74 68  ger code ..** th
13320 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 61  at indicates wha
13330 74 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 20  t aspect of the 
13340 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
13350 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 63  tion] is being c
13360 6f 6e 66 69 67 75 72 65 64 2e 0d 0a 2a 2a 20 53  onfigured...** S
13370 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65  ubsequent argume
13380 6e 74 73 20 76 61 72 79 20 64 65 70 65 6e 64 69  nts vary dependi
13390 6e 67 20 6f 6e 20 74 68 65 20 63 6f 6e 66 69 67  ng on the config
133a0 75 72 61 74 69 6f 6e 20 76 65 72 62 2e 0d 0a 2a  uration verb...*
133b0 2a 0d 0a 2a 2a 20 5e 43 61 6c 6c 73 20 74 6f 20  *..** ^Calls to 
133c0 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
133d0 67 28 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  g() return SQLIT
133e0 45 5f 4f 4b 20 69 66 20 61 6e 64 20 6f 6e 6c 79  E_OK if and only
133f0 20 69 66 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c   if..** the call
13400 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 73   is considered s
13410 75 63 63 65 73 73 66 75 6c 2e 0d 0a 2a 2f 0d 0a  uccessful...*/..
13420 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
13430 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
13440 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f  (sqlite3*, int o
13450 70 2c 20 2e 2e 2e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  p, ...);..../*..
13460 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d  ** CAPI3REF: Mem
13470 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 52  ory Allocation R
13480 6f 75 74 69 6e 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20  outines..**..** 
13490 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
134a0 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e  his object defin
134b0 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
134c0 20 62 65 74 77 65 65 6e 20 53 51 4c 69 74 65 0d   between SQLite.
134d0 0a 2a 2a 20 61 6e 64 20 6c 6f 77 2d 6c 65 76 65  .** and low-leve
134e0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
134f0 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a  ion routines...*
13500 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63  *..** This objec
13510 74 20 69 73 20 75 73 65 64 20 69 6e 20 6f 6e 6c  t is used in onl
13520 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 6e 20 74  y one place in t
13530 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  he SQLite interf
13540 61 63 65 2e 0d 0a 2a 2a 20 41 20 70 6f 69 6e 74  ace...** A point
13550 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
13560 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
13570 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74   is the argument
13580 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   to..** [sqlite3
13590 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 20  _config()] when 
135a0 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  the configuratio
135b0 6e 20 6f 70 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20  n option is..** 
135c0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
135d0 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54  ALLOC] or [SQLIT
135e0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
135f0 4f 43 5d 2e 20 20 0d 0a 2a 2a 20 42 79 20 63 72  OC].  ..** By cr
13600 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e  eating an instan
13610 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
13620 74 0d 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e  t..** and passin
13630 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
13640 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
13650 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29  _CONFIG_MALLOC])
13660 0d 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66  ..** during conf
13670 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70  iguration, an ap
13680 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73 70  plication can sp
13690 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
136a0 74 69 76 65 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20  tive..** memory 
136b0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
136c0 73 74 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20  stem for SQLite 
136d0 74 6f 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f  to use for all o
136e0 66 20 69 74 73 0d 0a 2a 2a 20 64 79 6e 61 6d 69  f its..** dynami
136f0 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0d  c memory needs..
13700 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  .**..** Note tha
13710 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 77  t SQLite comes w
13720 69 74 68 20 73 65 76 65 72 61 6c 20 5b 62 75 69  ith several [bui
13730 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c  lt-in memory all
13740 6f 63 61 74 6f 72 73 5d 0d 0a 2a 2a 20 74 68 61  ocators]..** tha
13750 74 20 61 72 65 20 70 65 72 66 65 63 74 6c 79 20  t are perfectly 
13760 61 64 65 71 75 61 74 65 20 66 6f 72 20 74 68 65  adequate for the
13770 20 6f 76 65 72 77 68 65 6c 6d 69 6e 67 20 6d 61   overwhelming ma
13780 6a 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63  jority of applic
13790 61 74 69 6f 6e 73 0d 0a 2a 2a 20 61 6e 64 20 74  ations..** and t
137a0 68 61 74 20 74 68 69 73 20 6f 62 6a 65 63 74 20  hat this object 
137b0 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20 74  is only useful t
137c0 6f 20 61 20 74 69 6e 79 20 6d 69 6e 6f 72 69 74  o a tiny minorit
137d0 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e  y of application
137e0 73 0d 0a 2a 2a 20 77 69 74 68 20 73 70 65 63 69  s..** with speci
137f0 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 20 61 6c  alized memory al
13800 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65  location require
13810 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6f 62 6a  ments.  This obj
13820 65 63 74 20 69 73 0d 0a 2a 2a 20 61 6c 73 6f 20  ect is..** also 
13830 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
13840 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 20 69 6e  ing of SQLite in
13850 20 6f 72 64 65 72 20 74 6f 20 73 70 65 63 69 66   order to specif
13860 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  y an alternative
13870 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ..** memory allo
13880 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d 75 6c  cator that simul
13890 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 74 2d  ates memory out-
138a0 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74  of-memory condit
138b0 69 6f 6e 73 20 69 6e 0d 0a 2a 2a 20 6f 72 64 65  ions in..** orde
138c0 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  r to verify that
138d0 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72 73   SQLite recovers
138e0 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f 6d   gracefully from
138f0 20 73 75 63 68 0d 0a 2a 2a 20 63 6f 6e 64 69 74   such..** condit
13900 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ions...**..** Th
13910 65 20 78 4d 61 6c 6c 6f 63 2c 20 78 52 65 61 6c  e xMalloc, xReal
13920 6c 6f 63 2c 20 61 6e 64 20 78 46 72 65 65 20 6d  loc, and xFree m
13930 65 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b  ethods must work
13940 20 6c 69 6b 65 20 74 68 65 0d 0a 2a 2a 20 6d 61   like the..** ma
13950 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28  lloc(), realloc(
13960 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 75 6e  ) and free() fun
13970 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  ctions from the 
13980 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61  standard C libra
13990 72 79 2e 0d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20  ry...** ^SQLite 
139a0 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
139b0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
139c0 65 6e 74 20 74 6f 0d 0a 2a 2a 20 78 52 65 61 6c  ent to..** xReal
139d0 6c 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61 20  loc is always a 
139e0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
139f0 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
13a00 6f 20 78 52 6f 75 6e 64 75 70 2e 0d 0a 2a 2a 0d  o xRoundup...**.
13a10 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c 64  .** xSize should
13a20 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c 6f   return the allo
13a30 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20  cated size of a 
13a40 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
13a50 6e 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  n..** previously
13a60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78   obtained from x
13a70 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c  Malloc or xReall
13a80 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74  oc.  The allocat
13a90 65 64 20 73 69 7a 65 0d 0a 2a 2a 20 69 73 20 61  ed size..** is a
13aa0 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61  lways at least a
13ab0 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71  s big as the req
13ac0 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20  uested size but 
13ad0 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0d 0a  may be larger...
13ae0 2a 2a 0d 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e  **..** The xRoun
13af0 64 75 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72  dup method retur
13b00 6e 73 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65  ns what would be
13b10 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73   the allocated s
13b20 69 7a 65 20 6f 66 0d 0a 2a 2a 20 61 20 6d 65 6d  ize of..** a mem
13b30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67  ory allocation g
13b40 69 76 65 6e 20 61 20 70 61 72 74 69 63 75 6c 61  iven a particula
13b50 72 20 72 65 71 75 65 73 74 65 64 20 73 69 7a 65  r requested size
13b60 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0d 0a  .  Most memory..
13b70 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f  ** allocators ro
13b80 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c  und up memory al
13b90 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c 65 61  locations at lea
13ba0 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6d  st to the next m
13bb0 75 6c 74 69 70 6c 65 0d 0a 2a 2a 20 6f 66 20 38  ultiple..** of 8
13bc0 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f  .  Some allocato
13bd0 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 61  rs round up to a
13be0 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65   larger multiple
13bf0 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 6f   or to a power o
13c00 66 20 32 2e 0d 0a 2a 2a 20 45 76 65 72 79 20 6d  f 2...** Every m
13c10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
13c20 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20   request coming 
13c30 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69  in through [sqli
13c40 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0d 0a 2a  te3_malloc()]..*
13c50 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  * or [sqlite3_re
13c60 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 63  alloc()] first c
13c70 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 20  alls xRoundup.  
13c80 49 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 75  If xRoundup retu
13c90 72 6e 73 20 30 2c 20 0d 0a 2a 2a 20 74 68 61 74  rns 0, ..** that
13ca0 20 63 61 75 73 65 73 20 74 68 65 20 63 6f 72 72   causes the corr
13cb0 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79  esponding memory
13cc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66   allocation to f
13cd0 61 69 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ail...**..** The
13ce0 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e   xInit method in
13cf0 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65  itializes the me
13d00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20  mory allocator. 
13d10 20 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0d 0a   (For example,..
13d20 2a 2a 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f  ** it might allo
13d30 63 61 74 65 20 61 6e 79 20 72 65 71 75 69 72 65  cate any require
13d40 20 6d 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74   mutexes or init
13d50 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20  ialize internal 
13d60 64 61 74 61 0d 0a 2a 2a 20 73 74 72 75 63 74 75  data..** structu
13d70 72 65 73 2e 20 20 54 68 65 20 78 53 68 75 74 64  res.  The xShutd
13d80 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 20 69 6e  own method is in
13d90 76 6f 6b 65 64 20 28 69 6e 64 69 72 65 63 74 6c  voked (indirectl
13da0 79 29 20 62 79 0d 0a 2a 2a 20 5b 73 71 6c 69 74  y) by..** [sqlit
13db0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 61  e3_shutdown()] a
13dc0 6e 64 20 73 68 6f 75 6c 64 20 64 65 61 6c 6c 6f  nd should deallo
13dd0 63 61 74 65 20 61 6e 79 20 72 65 73 6f 75 72 63  cate any resourc
13de0 65 73 20 61 63 71 75 69 72 65 64 0d 0a 2a 2a 20  es acquired..** 
13df0 62 79 20 78 49 6e 69 74 2e 20 20 54 68 65 20 70  by xInit.  The p
13e00 41 70 70 44 61 74 61 20 70 6f 69 6e 74 65 72 20  AppData pointer 
13e10 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 6f  is used as the o
13e20 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 74 6f  nly parameter to
13e30 0d 0a 2a 2a 20 78 49 6e 69 74 20 61 6e 64 20 78  ..** xInit and x
13e40 53 68 75 74 64 6f 77 6e 2e 0d 0a 2a 2a 0d 0a 2a  Shutdown...**..*
13e50 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64 73 20 74  * SQLite holds t
13e60 68 65 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58  he [SQLITE_MUTEX
13e70 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 5d 20  _STATIC_MASTER] 
13e80 6d 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e  mutex when it in
13e90 76 6f 6b 65 73 0d 0a 2a 2a 20 74 68 65 20 78 49  vokes..** the xI
13ea0 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74  nit method, so t
13eb0 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20  he xInit method 
13ec0 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65  need not be thre
13ed0 61 64 73 61 66 65 2e 20 20 54 68 65 0d 0a 2a 2a  adsafe.  The..**
13ee0 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f   xShutdown metho
13ef0 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  d is only called
13f00 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73   from [sqlite3_s
13f10 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74  hutdown()] so it
13f20 20 64 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20 6e 65   does..** not ne
13f30 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73  ed to be threads
13f40 61 66 65 20 65 69 74 68 65 72 2e 20 20 46 6f 72  afe either.  For
13f50 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f   all other metho
13f60 64 73 2c 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 68  ds, SQLite..** h
13f70 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45  olds the [SQLITE
13f80 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
13f90 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e 67  M] mutex as long
13fa0 20 61 73 20 74 68 65 0d 0a 2a 2a 20 5b 53 51 4c   as the..** [SQL
13fb0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54  ITE_CONFIG_MEMST
13fc0 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74  ATUS] configurat
13fd0 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75  ion option is tu
13fe0 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 0d 0a  rned on (which..
13ff0 2a 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 61  ** it is by defa
14000 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 20  ult) and so the 
14010 6d 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 6f  methods are auto
14020 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 6c  matically serial
14030 69 7a 65 64 2e 0d 0a 2a 2a 20 48 6f 77 65 76 65  ized...** Howeve
14040 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f  r, if [SQLITE_CO
14050 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20  NFIG_MEMSTATUS] 
14060 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65  is disabled, the
14070 6e 20 74 68 65 20 6f 74 68 65 72 0d 0a 2a 2a 20  n the other..** 
14080 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20  methods must be 
14090 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 6c  threadsafe or el
140a0 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f 77  se make their ow
140b0 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 66  n arrangements f
140c0 6f 72 0d 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61  or..** serializa
140d0 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51  tion...**..** SQ
140e0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
140f0 69 6e 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d  invoke xInit() m
14100 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69  ore than once wi
14110 74 68 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65  thout an interve
14120 6e 69 6e 67 0d 0a 2a 2a 20 63 61 6c 6c 20 74 6f  ning..** call to
14130 20 78 53 68 75 74 64 6f 77 6e 28 29 2e 0d 0a 2a   xShutdown()...*
14140 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
14150 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
14160 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65  thods sqlite3_me
14170 6d 5f 6d 65 74 68 6f 64 73 3b 0d 0a 73 74 72 75  m_methods;..stru
14180 63 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ct sqlite3_mem_m
14190 65 74 68 6f 64 73 20 7b 0d 0a 20 20 76 6f 69 64  ethods {..  void
141a0 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74   *(*xMalloc)(int
141b0 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65  );         /* Me
141c0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
141d0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 76  function */..  v
141e0 6f 69 64 20 28 2a 78 46 72 65 65 29 28 76 6f 69  oid (*xFree)(voi
141f0 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  d*);          /*
14200 20 46 72 65 65 20 61 20 70 72 69 6f 72 20 61 6c   Free a prior al
14210 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 76  location */..  v
14220 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f 63 29  oid *(*xRealloc)
14230 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a  (void*,int);  /*
14240 20 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63   Resize an alloc
14250 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ation */..  int 
14260 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a 29 3b  (*xSize)(void*);
14270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
14280 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
14290 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a   an allocation *
142a0 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75 6e  /..  int (*xRoun
142b0 64 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20 20  dup)(int);      
142c0 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70 20      /* Round up 
142d0 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
142e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
142f0 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69  */..  int (*xIni
14300 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  t)(void*);      
14310 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69       /* Initiali
14320 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
14330 6c 6f 63 61 74 6f 72 20 2a 2f 0d 0a 20 20 76 6f  locator */..  vo
14340 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28  id (*xShutdown)(
14350 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20  void*);      /* 
14360 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  Deinitialize the
14370 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
14380 72 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 41  r */..  void *pA
14390 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
143a0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
143b0 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e  nt to xInit() an
143c0 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f  d xShutdown() */
143d0 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ..};..../*..** C
143e0 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75  API3REF: Configu
143f0 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 0d 0a  ration Options..
14400 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f  ** KEYWORDS: {co
14410 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
14420 6f 6e 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73  on}..**..** Thes
14430 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  e constants are 
14440 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  the available in
14450 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74  teger configurat
14460 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74  ion options that
14470 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73  ..** can be pass
14480 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
14490 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
144a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
144b0 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a  )] interface...*
144c0 2a 0d 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67  *..** New config
144d0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
144e0 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20  may be added in 
144f0 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
14500 6f 66 20 53 51 4c 69 74 65 2e 0d 0a 2a 2a 20 45  of SQLite...** E
14510 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72  xisting configur
14520 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69  ation options mi
14530 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e  ght be discontin
14540 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  ued.  Applicatio
14550 6e 73 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68  ns..** should ch
14560 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63  eck the return c
14570 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ode from [sqlite
14580 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d  3_config()] to m
14590 61 6b 65 20 73 75 72 65 20 74 68 61 74 0d 0a 2a  ake sure that..*
145a0 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65  * the call worke
145b0 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  d.  The [sqlite3
145c0 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72  _config()] inter
145d0 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  face will return
145e0 20 61 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20   a..** non-zero 
145f0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20  [error code] if 
14600 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f  a discontinued o
14610 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f  r unsupported co
14620 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
14630 6f 6e 0d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  on..** is invoke
14640 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 64 6c 3e 0d  d...**..** <dl>.
14650 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e  .** [[SQLITE_CON
14660 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
14670 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ]] <dt>SQLITE_CO
14680 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41  NFIG_SINGLETHREA
14690 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54  D</dt>..** <dd>T
146a0 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75  here are no argu
146b0 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70  ments to this op
146c0 74 69 6f 6e 2e 20 20 5e 54 68 69 73 20 6f 70 74  tion.  ^This opt
146d0 69 6f 6e 20 73 65 74 73 20 74 68 65 0d 0a 2a 2a  ion sets the..**
146e0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
146f0 5d 20 74 6f 20 53 69 6e 67 6c 65 2d 74 68 72 65  ] to Single-thre
14700 61 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ad.  In other wo
14710 72 64 73 2c 20 69 74 20 64 69 73 61 62 6c 65 73  rds, it disables
14720 0d 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 69 6e  ..** all mutexin
14730 67 20 61 6e 64 20 70 75 74 73 20 53 51 4c 69 74  g and puts SQLit
14740 65 20 69 6e 74 6f 20 61 20 6d 6f 64 65 20 77 68  e into a mode wh
14750 65 72 65 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20  ere it can only 
14760 62 65 20 75 73 65 64 0d 0a 2a 2a 20 62 79 20 61  be used..** by a
14770 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20   single thread. 
14780 20 20 5e 49 66 20 53 51 4c 69 74 65 20 69 73 20    ^If SQLite is 
14790 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0d 0a 2a  compiled with..*
147a0 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48  * the [SQLITE_TH
147b0 52 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49 54  READSAFE | SQLIT
147c0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20  E_THREADSAFE=0] 
147d0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
147e0 69 6f 6e 20 74 68 65 6e 0d 0a 2a 2a 20 69 74 20  ion then..** it 
147f0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
14800 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 5b 74  to change the [t
14810 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 66  hreading mode] f
14820 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c 74 0d  rom its default.
14830 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 53 69 6e  .** value of Sin
14840 67 6c 65 2d 74 68 72 65 61 64 20 61 6e 64 20 73  gle-thread and s
14850 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
14860 67 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e  g()] will return
14870 20 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52   ..** [SQLITE_ER
14880 52 4f 52 5d 20 69 66 20 63 61 6c 6c 65 64 20 77  ROR] if called w
14890 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 43  ith the SQLITE_C
148a0 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
148b0 41 44 0d 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61  AD..** configura
148c0 74 69 6f 6e 20 6f 70 74 69 6f 6e 2e 3c 2f 64 64  tion option.</dd
148d0 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49  >..**..** [[SQLI
148e0 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54  TE_CONFIG_MULTIT
148f0 48 52 45 41 44 5d 5d 20 3c 64 74 3e 53 51 4c 49  HREAD]] <dt>SQLI
14900 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54  TE_CONFIG_MULTIT
14910 48 52 45 41 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  HREAD</dt>..** <
14920 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20  dd>There are no 
14930 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69  arguments to thi
14940 73 20 6f 70 74 69 6f 6e 2e 20 20 5e 54 68 69 73  s option.  ^This
14950 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 65   option sets the
14960 0d 0a 2a 2a 20 5b 74 68 72 65 61 64 69 6e 67 20  ..** [threading 
14970 6d 6f 64 65 5d 20 74 6f 20 4d 75 6c 74 69 2d 74  mode] to Multi-t
14980 68 72 65 61 64 2e 20 20 49 6e 20 6f 74 68 65 72  hread.  In other
14990 20 77 6f 72 64 73 2c 20 69 74 20 64 69 73 61 62   words, it disab
149a0 6c 65 73 0d 0a 2a 2a 20 6d 75 74 65 78 69 6e 67  les..** mutexing
149b0 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
149c0 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70  nnection] and [p
149d0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
149e0 74 5d 20 6f 62 6a 65 63 74 73 2e 0d 0a 2a 2a 20  t] objects...** 
149f0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
14a00 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
14a10 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61  or serializing a
14a20 63 63 65 73 73 20 74 6f 0d 0a 2a 2a 20 5b 64 61  ccess to..** [da
14a30 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14a40 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  ns] and [prepare
14a50 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20  d statements].  
14a60 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65  But other mutexe
14a70 73 0d 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65  s..** are enable
14a80 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65  d so that SQLite
14a90 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f   will be safe to
14aa0 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d   use in a multi-
14ab0 74 68 72 65 61 64 65 64 0d 0a 2a 2a 20 65 6e 76  threaded..** env
14ac0 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67  ironment as long
14ad0 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61   as no two threa
14ae0 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ds attempt to us
14af0 65 20 74 68 65 20 73 61 6d 65 0d 0a 2a 2a 20 5b  e the same..** [
14b00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14b10 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65  ion] at the same
14b20 20 74 69 6d 65 2e 20 20 5e 49 66 20 53 51 4c 69   time.  ^If SQLi
14b30 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
14b40 69 74 68 0d 0a 2a 2a 20 74 68 65 20 5b 53 51 4c  ith..** the [SQL
14b50 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 7c  ITE_THREADSAFE |
14b60 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
14b70 46 45 3d 30 5d 20 63 6f 6d 70 69 6c 65 2d 74 69  FE=0] compile-ti
14b80 6d 65 20 6f 70 74 69 6f 6e 20 74 68 65 6e 0d 0a  me option then..
14b90 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** it is not pos
14ba0 73 69 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65  sible to set the
14bb0 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b 74   Multi-thread [t
14bc0 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 61  hreading mode] a
14bd0 6e 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  nd..** [sqlite3_
14be0 63 6f 6e 66 69 67 28 29 5d 20 77 69 6c 6c 20 72  config()] will r
14bf0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52  eturn [SQLITE_ER
14c00 52 4f 52 5d 20 69 66 20 63 61 6c 6c 65 64 20 77  ROR] if called w
14c10 69 74 68 20 74 68 65 0d 0a 2a 2a 20 53 51 4c 49  ith the..** SQLI
14c20 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54  TE_CONFIG_MULTIT
14c30 48 52 45 41 44 20 63 6f 6e 66 69 67 75 72 61 74  HREAD configurat
14c40 69 6f 6e 20 6f 70 74 69 6f 6e 2e 3c 2f 64 64 3e  ion option.</dd>
14c50 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54  ..**..** [[SQLIT
14c60 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49  E_CONFIG_SERIALI
14c70 5a 45 44 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45  ZED]] <dt>SQLITE
14c80 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
14c90 45 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e  ED</dt>..** <dd>
14ca0 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67  There are no arg
14cb0 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f  uments to this o
14cc0 70 74 69 6f 6e 2e 20 20 5e 54 68 69 73 20 6f 70  ption.  ^This op
14cd0 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0d 0a 2a  tion sets the..*
14ce0 2a 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  * [threading mod
14cf0 65 5d 20 74 6f 20 53 65 72 69 61 6c 69 7a 65 64  e] to Serialized
14d00 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  . In other words
14d10 2c 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 65 6e  , this option en
14d20 61 62 6c 65 73 0d 0a 2a 2a 20 61 6c 6c 20 6d 75  ables..** all mu
14d30 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e 67 20  texes including 
14d40 74 68 65 20 72 65 63 75 72 73 69 76 65 0d 0a 2a  the recursive..*
14d50 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64 61  * mutexes on [da
14d60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14d70 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64  n] and [prepared
14d80 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65   statement] obje
14d90 63 74 73 2e 0d 0a 2a 2a 20 49 6e 20 74 68 69 73  cts...** In this
14da0 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 20   mode (which is 
14db0 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e  the default when
14dc0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
14dd0 6c 65 64 20 77 69 74 68 0d 0a 2a 2a 20 5b 53 51  led with..** [SQ
14de0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
14df0 31 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c  1]) the SQLite l
14e00 69 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65  ibrary will itse
14e10 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63  lf serialize acc
14e20 65 73 73 0d 0a 2a 2a 20 74 6f 20 5b 64 61 74 61  ess..** to [data
14e30 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
14e40 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
14e50 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74  statements] so t
14e60 68 61 74 20 74 68 65 0d 0a 2a 2a 20 61 70 70 6c  hat the..** appl
14e70 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 20  ication is free 
14e80 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 20  to use the same 
14e90 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
14ea0 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0d 0a 2a 2a  tion] or the..**
14eb0 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20   same [prepared 
14ec0 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69  statement] in di
14ed0 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20  fferent threads 
14ee0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
14ef0 2e 0d 0a 2a 2a 20 5e 49 66 20 53 51 4c 69 74 65  ...** ^If SQLite
14f00 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
14f10 68 0d 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54  h..** the [SQLIT
14f20 45 5f 54 48 52 45 41 44 53 41 46 45 20 7c 20 53  E_THREADSAFE | S
14f30 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
14f40 3d 30 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  =0] compile-time
14f50 20 6f 70 74 69 6f 6e 20 74 68 65 6e 0d 0a 2a 2a   option then..**
14f60 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
14f70 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20 53  ble to set the S
14f80 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61  erialized [threa
14f90 64 69 6e 67 20 6d 6f 64 65 5d 20 61 6e 64 0d 0a  ding mode] and..
14fa0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ** [sqlite3_conf
14fb0 69 67 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72  ig()] will retur
14fc0 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  n [SQLITE_ERROR]
14fd0 20 69 66 20 63 61 6c 6c 65 64 20 77 69 74 68 20   if called with 
14fe0 74 68 65 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 43  the..** SQLITE_C
14ff0 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
15000 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
15010 70 74 69 6f 6e 2e 3c 2f 64 64 3e 0d 0a 2a 2a 0d  ption.</dd>..**.
15020 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e  .** [[SQLITE_CON
15030 46 49 47 5f 4d 41 4c 4c 4f 43 5d 5d 20 3c 64 74  FIG_MALLOC]] <dt
15040 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  >SQLITE_CONFIG_M
15050 41 4c 4c 4f 43 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  ALLOC</dt>..** <
15060 64 64 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f  dd> ^(This optio
15070 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
15080 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
15090 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
150a0 61 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  an..** instance 
150b0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
150c0 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72  mem_methods] str
150d0 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67  ucture.  The arg
150e0 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0d  ument specifies.
150f0 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20  .** alternative 
15100 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
15110 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
15120 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20  ines to be used 
15130 69 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  in place of..** 
15140 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
15150 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62  ation routines b
15160 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65  uilt into SQLite
15170 2e 29 5e 20 5e 53 51 4c 69 74 65 20 6d 61 6b 65  .)^ ^SQLite make
15180 73 0d 0a 2a 2a 20 69 74 73 20 6f 77 6e 20 70 72  s..** its own pr
15190 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 68  ivate copy of th
151a0 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
151b0 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65   [sqlite3_mem_me
151c0 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65  thods] structure
151d0 0d 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20  ..** before the 
151e0 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
151f0 29 5d 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  )] call returns.
15200 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b  </dd>..**..** [[
15210 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
15220 54 4d 41 4c 4c 4f 43 5d 5d 20 3c 64 74 3e 53 51  TMALLOC]] <dt>SQ
15230 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
15240 41 4c 4c 4f 43 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  ALLOC</dt>..** <
15250 64 64 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f  dd> ^(This optio
15260 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
15270 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
15280 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
15290 61 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  an..** instance 
152a0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
152b0 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72  mem_methods] str
152c0 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 71  ucture.  The [sq
152d0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
152e0 73 5d 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  s]..** structure
152f0 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
15300 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65  the currently de
15310 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c  fined memory all
15320 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
15330 2e 29 5e 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 74  .)^..** This opt
15340 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20  ion can be used 
15350 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20  to overload the 
15360 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61  default memory a
15370 6c 6c 6f 63 61 74 69 6f 6e 0d 0a 2a 2a 20 72 6f  llocation..** ro
15380 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77 72  utines with a wr
15390 61 70 70 65 72 20 74 68 61 74 20 73 69 6d 75 6c  apper that simul
153a0 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 79 20 61 6c  ations memory al
153b0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  location failure
153c0 20 6f 72 0d 0a 2a 2a 20 74 72 61 63 6b 73 20 6d   or..** tracks m
153d0 65 6d 6f 72 79 20 75 73 61 67 65 2c 20 66 6f 72  emory usage, for
153e0 20 65 78 61 6d 70 6c 65 2e 20 3c 2f 64 64 3e 0d   example. </dd>.
153f0 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45  .**..** [[SQLITE
15400 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
15410 53 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  S]] <dt>SQLITE_C
15420 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 3c  ONFIG_MEMSTATUS<
15430 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 54  /dt>..** <dd> ^T
15440 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
15450 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74   single argument
15460 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 6e   of type int, in
15470 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 0d  terpreted as a .
15480 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69  .** boolean, whi
15490 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69  ch enables or di
154a0 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65  sables the colle
154b0 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20  ction of memory 
154c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 0d 0a 2a 2a 20  allocation ..** 
154d0 73 74 61 74 69 73 74 69 63 73 2e 20 5e 28 57 68  statistics. ^(Wh
154e0 65 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  en memory alloca
154f0 74 69 6f 6e 20 73 74 61 74 69 73 74 69 63 73 20  tion statistics 
15500 61 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68  are disabled, th
15510 65 20 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  e ..** following
15520 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
15530 65 73 20 62 65 63 6f 6d 65 20 6e 6f 6e 2d 6f 70  es become non-op
15540 65 72 61 74 69 6f 6e 61 6c 3a 0d 0a 2a 2a 20 20  erational:..**  
15550 20 3c 75 6c 3e 0d 0a 2a 2a 20 20 20 3c 6c 69 3e   <ul>..**   <li>
15560 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
15570 5f 75 73 65 64 28 29 5d 0d 0a 2a 2a 20 20 20 3c  _used()]..**   <
15580 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  li> [sqlite3_mem
15590 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d  ory_highwater()]
155a0 0d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c  ..**   <li> [sql
155b0 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
155c0 69 6d 69 74 36 34 28 29 5d 0d 0a 2a 2a 20 20 20  imit64()]..**   
155d0 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74  <li> [sqlite3_st
155e0 61 74 75 73 28 29 5d 0d 0a 2a 2a 20 20 20 3c 2f  atus()]..**   </
155f0 75 6c 3e 29 5e 0d 0a 2a 2a 20 5e 4d 65 6d 6f 72  ul>)^..** ^Memor
15600 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61  y allocation sta
15610 74 69 73 74 69 63 73 20 61 72 65 20 65 6e 61 62  tistics are enab
15620 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 75  led by default u
15630 6e 6c 65 73 73 20 53 51 4c 69 74 65 20 69 73 0d  nless SQLite is.
15640 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  .** compiled wit
15650 68 20 5b 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  h [SQLITE_DEFAUL
15660 54 5f 4d 45 4d 53 54 41 54 55 53 5d 3d 30 20 69  T_MEMSTATUS]=0 i
15670 6e 20 77 68 69 63 68 20 63 61 73 65 20 6d 65 6d  n which case mem
15680 6f 72 79 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  ory..** allocati
15690 6f 6e 20 73 74 61 74 69 73 74 69 63 73 20 61 72  on statistics ar
156a0 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65  e disabled by de
156b0 66 61 75 6c 74 2e 0d 0a 2a 2a 20 3c 2f 64 64 3e  fault...** </dd>
156c0 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54  ..**..** [[SQLIT
156d0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
156e0 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ]] <dt>SQLITE_CO
156f0 4e 46 49 47 5f 53 43 52 41 54 43 48 3c 2f 64 74  NFIG_SCRATCH</dt
15700 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73  >..** <dd> ^This
15710 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65   option specifie
15720 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72  s a static memor
15730 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51  y buffer that SQ
15740 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72  Lite can use for
15750 0d 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 6d  ..** scratch mem
15760 6f 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 20  ory.  There are 
15770 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a  three arguments:
15780 20 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 38    A pointer an 8
15790 2d 62 79 74 65 0d 0a 2a 2a 20 61 6c 69 67 6e 65  -byte..** aligne
157a0 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  d memory buffer 
157b0 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73  from which the s
157c0 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f  cratch allocatio
157d0 6e 73 20 77 69 6c 6c 20 62 65 0d 0a 2a 2a 20 64  ns will be..** d
157e0 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f  rawn, the size o
157f0 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 61  f each scratch a
15800 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0d  llocation (sz),.
15810 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78 69  .** and the maxi
15820 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 63  mum number of sc
15830 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
15840 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0d 0a  s (N).  The sz..
15850 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74  ** argument must
15860 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f   be a multiple o
15870 66 20 31 36 2e 0d 0a 2a 2a 20 54 68 65 20 66 69  f 16...** The fi
15880 72 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73  rst argument mus
15890 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  t be a pointer t
158a0 6f 20 61 6e 20 38 2d 62 79 74 65 20 61 6c 69 67  o an 8-byte alig
158b0 6e 65 64 20 62 75 66 66 65 72 0d 0a 2a 2a 20 6f  ned buffer..** o
158c0 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20  f at least sz*N 
158d0 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
158e0 0d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c  ..** ^SQLite wil
158f0 6c 20 75 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68  l use no more th
15900 61 6e 20 74 77 6f 20 73 63 72 61 74 63 68 20 62  an two scratch b
15910 75 66 66 65 72 73 20 70 65 72 20 74 68 72 65 61  uffers per threa
15920 64 2e 20 20 53 6f 0d 0a 2a 2a 20 4e 20 73 68 6f  d.  So..** N sho
15930 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74 77  uld be set to tw
15940 69 63 65 20 74 68 65 20 65 78 70 65 63 74 65 64  ice the expected
15950 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
15960 6f 66 20 74 68 72 65 61 64 73 2e 0d 0a 2a 2a 20  of threads...** 
15970 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76  ^SQLite will nev
15980 65 72 20 72 65 71 75 69 72 65 20 61 20 73 63 72  er require a scr
15990 61 74 63 68 20 62 75 66 66 65 72 20 74 68 61 74  atch buffer that
159a0 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 36 0d   is more than 6.
159b0 0a 2a 2a 20 74 69 6d 65 73 20 74 68 65 20 64 61  .** times the da
159c0 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
159d0 2e 20 5e 49 66 20 53 51 4c 69 74 65 20 6e 65 65  . ^If SQLite nee
159e0 64 73 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f  ds needs additio
159f0 6e 61 6c 0d 0a 2a 2a 20 73 63 72 61 74 63 68 20  nal..** scratch 
15a00 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20 77 68  memory beyond wh
15a10 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 62  at is provided b
15a20 79 20 74 68 69 73 20 63 6f 6e 66 69 67 75 72 61  y this configura
15a30 74 69 6f 6e 20 6f 70 74 69 6f 6e 2c 20 74 68 65  tion option, the
15a40 6e 20 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  n ..** [sqlite3_
15a50 6d 61 6c 6c 6f 63 28 29 5d 20 77 69 6c 6c 20 62  malloc()] will b
15a60 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  e used to obtain
15a70 20 74 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64   the memory need
15a80 65 64 2e 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a  ed.</dd>..**..**
15a90 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47   [[SQLITE_CONFIG
15aa0 5f 50 41 47 45 43 41 43 48 45 5d 5d 20 3c 64 74  _PAGECACHE]] <dt
15ab0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  >SQLITE_CONFIG_P
15ac0 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0d 0a 2a  AGECACHE</dt>..*
15ad0 2a 20 3c 64 64 3e 20 5e 54 68 69 73 20 6f 70 74  * <dd> ^This opt
15ae0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20  ion specifies a 
15af0 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75  static memory bu
15b00 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65  ffer that SQLite
15b10 20 63 61 6e 20 75 73 65 20 66 6f 72 0d 0a 2a 2a   can use for..**
15b20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
15b30 67 65 20 63 61 63 68 65 20 77 69 74 68 20 74 68  ge cache with th
15b40 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63  e default page c
15b50 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ache implementat
15b60 69 6f 6e 2e 20 20 0d 0a 2a 2a 20 54 68 69 73 20  ion.  ..** This 
15b70 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 68  configuration sh
15b80 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
15b90 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69   if an applicati
15ba0 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 65 0d 0a  on-define page..
15bb0 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ** cache impleme
15bc0 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 64 65  ntation is loade
15bd0 64 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49  d using the SQLI
15be0 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
15bf0 32 20 6f 70 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68  2 option...** Th
15c00 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72  ere are three ar
15c10 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
15c20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 65  option: A pointe
15c30 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67  r to 8-byte alig
15c40 6e 65 64 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20  ned..** memory, 
15c50 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
15c60 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a   page buffer (sz
15c70 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ), and the numbe
15c80 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0d  r of pages (N)..
15c90 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d  .** The sz argum
15ca0 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68  ent should be th
15cb0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61  e size of the la
15cc0 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70  rgest database p
15cd0 61 67 65 0d 0a 2a 2a 20 28 61 20 70 6f 77 65 72  age..** (a power
15ce0 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20   of two between 
15cf0 35 31 32 20 61 6e 64 20 33 32 37 36 38 29 20 70  512 and 32768) p
15d00 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74  lus a little ext
15d10 72 61 20 66 6f 72 20 65 61 63 68 0d 0a 2a 2a 20  ra for each..** 
15d20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 5e 54  page header.  ^T
15d30 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 73  he page header s
15d40 69 7a 65 20 69 73 20 32 30 20 74 6f 20 34 30 20  ize is 20 to 40 
15d50 62 79 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20  bytes depending 
15d60 6f 6e 0d 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20  on..** the host 
15d70 61 72 63 68 69 74 65 63 74 75 72 65 2e 20 20 5e  architecture.  ^
15d80 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 2c 20  It is harmless, 
15d90 61 70 61 72 74 20 66 72 6f 6d 20 74 68 65 20 77  apart from the w
15da0 61 73 74 65 64 20 6d 65 6d 6f 72 79 2c 0d 0a 2a  asted memory,..*
15db0 2a 20 74 6f 20 6d 61 6b 65 20 73 7a 20 61 20 6c  * to make sz a l
15dc0 69 74 74 6c 65 20 74 6f 6f 20 6c 61 72 67 65 2e  ittle too large.
15dd0 20 20 54 68 65 20 66 69 72 73 74 0d 0a 2a 2a 20    The first..** 
15de0 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20  argument should 
15df0 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f  point to an allo
15e00 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61  cation of at lea
15e10 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66  st sz*N bytes of
15e20 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 20 5e 53 51   memory...** ^SQ
15e30 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68  Lite will use th
15e40 65 20 6d 65 6d 6f 72 79 20 70 72 6f 76 69 64 65  e memory provide
15e50 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 61  d by the first a
15e60 72 67 75 6d 65 6e 74 20 74 6f 20 73 61 74 69 73  rgument to satis
15e70 66 79 20 69 74 73 0d 0a 2a 2a 20 6d 65 6d 6f 72  fy its..** memor
15e80 79 20 6e 65 65 64 73 20 66 6f 72 20 74 68 65 20  y needs for the 
15e90 66 69 72 73 74 20 4e 20 70 61 67 65 73 20 74 68  first N pages th
15ea0 61 74 20 69 74 20 61 64 64 73 20 74 6f 20 63 61  at it adds to ca
15eb0 63 68 65 2e 20 20 5e 49 66 20 61 64 64 69 74 69  che.  ^If additi
15ec0 6f 6e 61 6c 0d 0a 2a 2a 20 70 61 67 65 20 63 61  onal..** page ca
15ed0 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 6e 65  che memory is ne
15ee0 65 64 65 64 20 62 65 79 6f 6e 64 20 77 68 61 74  eded beyond what
15ef0 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 20   is provided by 
15f00 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 74 68 65  this option, the
15f10 6e 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 67 6f 65  n..** SQLite goe
15f20 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  s to [sqlite3_ma
15f30 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 65 20  lloc()] for the 
15f40 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f 72 61  additional stora
15f50 67 65 20 73 70 61 63 65 2e 0d 0a 2a 2a 20 54 68  ge space...** Th
15f60 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 68 65  e pointer in the
15f70 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
15f80 6d 75 73 74 0d 0a 2a 2a 20 62 65 20 61 6c 69 67  must..** be alig
15f90 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65  ned to an 8-byte
15fa0 20 62 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62   boundary or sub
15fb0 73 65 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72  sequent behavior
15fc0 20 6f 66 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 77   of SQLite..** w
15fd0 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e 65 64  ill be undefined
15fe0 2e 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b  .</dd>..**..** [
15ff0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48  [SQLITE_CONFIG_H
16000 45 41 50 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45  EAP]] <dt>SQLITE
16010 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74  _CONFIG_HEAP</dt
16020 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73  >..** <dd> ^This
16030 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65   option specifie
16040 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72  s a static memor
16050 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51  y buffer that SQ
16060 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0d 0a 2a  Lite will use..*
16070 2a 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73  * for all of its
16080 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20   dynamic memory 
16090 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
160a0 20 62 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72   beyond those pr
160b0 6f 76 69 64 65 64 0d 0a 2a 2a 20 66 6f 72 20 62  ovided..** for b
160c0 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  y [SQLITE_CONFIG
160d0 5f 53 43 52 41 54 43 48 5d 20 61 6e 64 20 5b 53  _SCRATCH] and [S
160e0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
160f0 45 43 41 43 48 45 5d 2e 0d 0a 2a 2a 20 54 68 65  ECACHE]...** The
16100 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67  re are three arg
16110 75 6d 65 6e 74 73 3a 20 41 6e 20 38 2d 62 79 74  uments: An 8-byt
16120 65 20 61 6c 69 67 6e 65 64 20 70 6f 69 6e 74 65  e aligned pointe
16130 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c  r to the memory,
16140 0d 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  ..** the number 
16150 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
16160 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 2c 20 61  memory buffer, a
16170 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61  nd the minimum a
16180 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0d  llocation size..
16190 0a 2a 2a 20 5e 49 66 20 74 68 65 20 66 69 72 73  .** ^If the firs
161a0 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 6d  t pointer (the m
161b0 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 69  emory pointer) i
161c0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c  s NULL, then SQL
161d0 69 74 65 20 72 65 76 65 72 74 73 0d 0a 2a 2a 20  ite reverts..** 
161e0 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 66  to using its def
161f0 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ault memory allo
16200 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 65  cator (the syste
16210 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65  m malloc() imple
16220 6d 65 6e 74 61 74 69 6f 6e 29 2c 0d 0a 2a 2a 20  mentation),..** 
16230 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f  undoing any prio
16240 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  r invocation of 
16250 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
16260 41 4c 4c 4f 43 5d 2e 20 20 5e 49 66 20 74 68 65  ALLOC].  ^If the
16270 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  ..** memory poin
16280 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
16290 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49  and either [SQLI
162a0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
162b0 33 5d 20 6f 72 0d 0a 2a 2a 20 5b 53 51 4c 49 54  3] or..** [SQLIT
162c0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
162d0 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74  ] are defined, t
162e0 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74  hen the alternat
162f0 69 76 65 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 61  ive memory..** a
16300 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61  llocator is enga
16310 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c  ged to handle al
16320 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d  l of SQLites mem
16330 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
16340 65 65 64 73 2e 0d 0a 2a 2a 20 54 68 65 20 66 69  eeds...** The fi
16350 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65  rst pointer (the
16360 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29   memory pointer)
16370 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64   must be aligned
16380 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 0d 0a 2a   to an 8-byte..*
16390 2a 20 62 6f 75 6e 64 61 72 79 20 6f 72 20 73 75  * boundary or su
163a0 62 73 65 71 75 65 6e 74 20 62 65 68 61 76 69 6f  bsequent behavio
163b0 72 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c  r of SQLite will
163c0 20 62 65 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a   be undefined...
163d0 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 61  ** The minimum a
163e0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69  llocation size i
163f0 73 20 63 61 70 70 65 64 20 61 74 20 32 2a 2a 31  s capped at 2**1
16400 32 2e 20 52 65 61 73 6f 6e 61 62 6c 65 20 76 61  2. Reasonable va
16410 6c 75 65 73 0d 0a 2a 2a 20 66 6f 72 20 74 68 65  lues..** for the
16420 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74   minimum allocat
16430 69 6f 6e 20 73 69 7a 65 20 61 72 65 20 32 2a 2a  ion size are 2**
16440 35 20 74 68 72 6f 75 67 68 20 32 2a 2a 38 2e 3c  5 through 2**8.<
16450 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53  /dd>..**..** [[S
16460 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
16470 45 58 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f  EX]] <dt>SQLITE_
16480 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74  CONFIG_MUTEX</dt
16490 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69  >..** <dd> ^(Thi
164a0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61  s option takes a
164b0 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74   single argument
164c0 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e   which is a poin
164d0 74 65 72 20 74 6f 20 61 6e 0d 0a 2a 2a 20 69 6e  ter to an..** in
164e0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73  stance of the [s
164f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
16500 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
16510 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73    The argument s
16520 70 65 63 69 66 69 65 73 0d 0a 2a 2a 20 61 6c 74  pecifies..** alt
16530 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76  ernative low-lev
16540 65 6c 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  el mutex routine
16550 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20  s to be used in 
16560 70 6c 61 63 65 0d 0a 2a 2a 20 74 68 65 20 6d 75  place..** the mu
16570 74 65 78 20 72 6f 75 74 69 6e 65 73 20 62 75 69  tex routines bui
16580 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e 29  lt into SQLite.)
16590 5e 20 20 5e 53 51 4c 69 74 65 20 6d 61 6b 65 73  ^  ^SQLite makes
165a0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0d 0a   a copy of the..
165b0 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  ** content of th
165c0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e [sqlite3_mutex
165d0 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
165e0 75 72 65 20 62 65 66 6f 72 65 20 74 68 65 20 63  ure before the c
165f0 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69  all to..** [sqli
16600 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 72 65  te3_config()] re
16610 74 75 72 6e 73 2e 20 5e 49 66 20 53 51 4c 69 74  turns. ^If SQLit
16620 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
16630 74 68 0d 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49  th..** the [SQLI
16640 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 7c 20  TE_THREADSAFE | 
16650 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
16660 45 3d 30 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  E=0] compile-tim
16670 65 20 6f 70 74 69 6f 6e 20 74 68 65 6e 0d 0a 2a  e option then..*
16680 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 75 74  * the entire mut
16690 65 78 69 6e 67 20 73 75 62 73 79 73 74 65 6d 20  exing subsystem 
166a0 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20  is omitted from 
166b0 74 68 65 20 62 75 69 6c 64 20 61 6e 64 20 68 65  the build and he
166c0 6e 63 65 20 63 61 6c 6c 73 20 74 6f 0d 0a 2a 2a  nce calls to..**
166d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
166e0 28 29 5d 20 77 69 74 68 20 74 68 65 20 53 51 4c  ()] with the SQL
166f0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
16700 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
16710 70 74 69 6f 6e 20 77 69 6c 6c 0d 0a 2a 2a 20 72  ption will..** r
16720 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52  eturn [SQLITE_ER
16730 52 4f 52 5d 2e 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a  ROR].</dd>..**..
16740 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ** [[SQLITE_CONF
16750 49 47 5f 47 45 54 4d 55 54 45 58 5d 5d 20 3c 64  IG_GETMUTEX]] <d
16760 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
16770 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0d 0a 2a  GETMUTEX</dt>..*
16780 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73 20 6f 70  * <dd> ^(This op
16790 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e  tion takes a sin
167a0 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69  gle argument whi
167b0 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ch is a pointer 
167c0 74 6f 20 61 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e  to an..** instan
167d0 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
167e0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
167f0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
16800 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  e..** [sqlite3_m
16810 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0d 0a 2a  utex_methods]..*
16820 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66  * structure is f
16830 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63  illed with the c
16840 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64  urrently defined
16850 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e   mutex routines.
16860 29 5e 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69  )^..** This opti
16870 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  on can be used t
16880 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64  o overload the d
16890 65 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c  efault mutex all
168a0 6f 63 61 74 69 6f 6e 0d 0a 2a 2a 20 72 6f 75 74  ocation..** rout
168b0 69 6e 65 73 20 77 69 74 68 20 61 20 77 72 61 70  ines with a wrap
168c0 70 65 72 20 75 73 65 64 20 74 6f 20 74 72 61 63  per used to trac
168d0 6b 20 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f  k mutex usage fo
168e0 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 0d 0a 2a  r performance..*
168f0 2a 20 70 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74  * profiling or t
16900 65 73 74 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d  esting, for exam
16910 70 6c 65 2e 20 20 20 5e 49 66 20 53 51 4c 69 74  ple.   ^If SQLit
16920 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
16930 74 68 0d 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49  th..** the [SQLI
16940 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 7c 20  TE_THREADSAFE | 
16950 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
16960 45 3d 30 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  E=0] compile-tim
16970 65 20 6f 70 74 69 6f 6e 20 74 68 65 6e 0d 0a 2a  e option then..*
16980 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 75 74  * the entire mut
16990 65 78 69 6e 67 20 73 75 62 73 79 73 74 65 6d 20  exing subsystem 
169a0 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20  is omitted from 
169b0 74 68 65 20 62 75 69 6c 64 20 61 6e 64 20 68 65  the build and he
169c0 6e 63 65 20 63 61 6c 6c 73 20 74 6f 0d 0a 2a 2a  nce calls to..**
169d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
169e0 28 29 5d 20 77 69 74 68 20 74 68 65 20 53 51 4c  ()] with the SQL
169f0 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55  ITE_CONFIG_GETMU
16a00 54 45 58 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  TEX configuratio
16a10 6e 20 6f 70 74 69 6f 6e 20 77 69 6c 6c 0d 0a 2a  n option will..*
16a20 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  * return [SQLITE
16a30 5f 45 52 52 4f 52 5d 2e 3c 2f 64 64 3e 0d 0a 2a  _ERROR].</dd>..*
16a40 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43  *..** [[SQLITE_C
16a50 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d  ONFIG_LOOKASIDE]
16a60 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  ] <dt>SQLITE_CON
16a70 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64  FIG_LOOKASIDE</d
16a80 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68  t>..** <dd> ^(Th
16a90 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
16aa0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68  two arguments th
16ab0 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  at determine the
16ac0 20 64 65 66 61 75 6c 74 0d 0a 2a 2a 20 6d 65 6d   default..** mem
16ad0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
16ae0 6f 72 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  or the lookaside
16af0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
16b00 72 20 6f 6e 20 65 61 63 68 0d 0a 2a 2a 20 5b 64  r on each..** [d
16b10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
16b20 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20  on].  The first 
16b30 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0d  argument is the.
16b40 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68  .** size of each
16b50 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
16b60 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 73  r slot and the s
16b70 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d  econd is the num
16b80 62 65 72 20 6f 66 0d 0a 2a 2a 20 73 6c 6f 74 73  ber of..** slots
16b90 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61   allocated to ea
16ba0 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ch database conn
16bb0 65 63 74 69 6f 6e 2e 29 5e 20 20 5e 28 54 68 69  ection.)^  ^(Thi
16bc0 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68  s option sets th
16bd0 65 0d 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74  e..** <i>default
16be0 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73  </i> lookaside s
16bf0 69 7a 65 2e 20 54 68 65 20 5b 53 51 4c 49 54 45  ize. The [SQLITE
16c00 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  _DBCONFIG_LOOKAS
16c10 49 44 45 5d 0d 0a 2a 2a 20 76 65 72 62 20 74 6f  IDE]..** verb to
16c20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
16c30 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 73  fig()] can be us
16c40 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
16c50 20 6c 6f 6f 6b 61 73 69 64 65 0d 0a 2a 2a 20 63   lookaside..** c
16c60 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20  onfiguration on 
16c70 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65  individual conne
16c80 63 74 69 6f 6e 73 2e 29 5e 20 3c 2f 64 64 3e 0d  ctions.)^ </dd>.
16c90 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45  .**..** [[SQLITE
16ca0 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 32 5d  _CONFIG_PCACHE2]
16cb0 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  ] <dt>SQLITE_CON
16cc0 46 49 47 5f 50 43 41 43 48 45 32 3c 2f 64 74 3e  FIG_PCACHE2</dt>
16cd0 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73  ..** <dd> ^(This
16ce0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
16cf0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
16d00 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
16d10 65 72 20 74 6f 0d 0a 2a 2a 20 61 6e 20 5b 73 71  er to..** an [sq
16d20 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
16d30 68 6f 64 73 32 5d 20 6f 62 6a 65 63 74 2e 20 20  hods2] object.  
16d40 54 68 69 73 20 6f 62 6a 65 63 74 20 73 70 65 63  This object spec
16d50 69 66 69 65 73 20 74 68 65 20 69 6e 74 65 72 66  ifies the interf
16d60 61 63 65 0d 0a 2a 2a 20 74 6f 20 61 20 63 75 73  ace..** to a cus
16d70 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 69  tom page cache i
16d80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 29 5e  mplementation.)^
16d90 20 20 5e 53 51 4c 69 74 65 20 6d 61 6b 65 73 20    ^SQLite makes 
16da0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0d 0a 2a  a copy of the..*
16db0 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 65  * object and use
16dc0 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 61  s it for page ca
16dd0 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  che memory alloc
16de0 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0d 0a 2a 2a  ations.</dd>..**
16df0 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f  ..** [[SQLITE_CO
16e00 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 32 5d  NFIG_GETPCACHE2]
16e10 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  ] <dt>SQLITE_CON
16e20 46 49 47 5f 47 45 54 50 43 41 43 48 45 32 3c 2f  FIG_GETPCACHE2</
16e30 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54  dt>..** <dd> ^(T
16e40 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
16e50 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65   a single argume
16e60 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f  nt which is a po
16e70 69 6e 74 65 72 20 74 6f 20 61 6e 0d 0a 2a 2a 20  inter to an..** 
16e80 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f  [sqlite3_pcache_
16e90 6d 65 74 68 6f 64 73 32 5d 20 6f 62 6a 65 63 74  methods2] object
16ea0 2e 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65 73  .  SQLite copies
16eb0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0d   of the current.
16ec0 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 69  .** page cache i
16ed0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
16ee0 74 6f 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 29  to that object.)
16ef0 5e 20 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a 20  ^ </dd>..**..** 
16f00 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  [[SQLITE_CONFIG_
16f10 4c 4f 47 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45  LOG]] <dt>SQLITE
16f20 5f 43 4f 4e 46 49 47 5f 4c 4f 47 3c 2f 64 74 3e  _CONFIG_LOG</dt>
16f30 0d 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 65 20 53  ..** <dd> ^The S
16f40 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47  QLITE_CONFIG_LOG
16f50 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77   option takes tw
16f60 6f 20 61 72 67 75 6d 65 6e 74 73 3a 20 61 20 70  o arguments: a p
16f70 6f 69 6e 74 65 72 20 74 6f 20 61 0d 0a 2a 2a 20  ointer to a..** 
16f80 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20  function with a 
16f90 63 61 6c 6c 20 73 69 67 6e 61 74 75 72 65 20 6f  call signature o
16fa0 66 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  f void(*)(void*,
16fb0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
16fc0 2c 20 0d 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69  , ..** and a poi
16fd0 6e 74 65 72 20 74 6f 20 76 6f 69 64 2e 20 5e 49  nter to void. ^I
16fe0 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70  f the function p
16ff0 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55  ointer is not NU
17000 4c 4c 2c 20 69 74 20 69 73 0d 0a 2a 2a 20 69 6e  LL, it is..** in
17010 76 6f 6b 65 64 20 62 79 20 5b 73 71 6c 69 74 65  voked by [sqlite
17020 33 5f 6c 6f 67 28 29 5d 20 74 6f 20 70 72 6f 63  3_log()] to proc
17030 65 73 73 20 65 61 63 68 20 6c 6f 67 67 69 6e 67  ess each logging
17040 20 65 76 65 6e 74 2e 20 20 5e 49 66 20 74 68 65   event.  ^If the
17050 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 6f  ..** function po
17060 69 6e 74 65 72 20 69 73 20 4e 55 4c 4c 2c 20 74  inter is NULL, t
17070 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 67 28  he [sqlite3_log(
17080 29 5d 20 69 6e 74 65 72 66 61 63 65 20 62 65 63  )] interface bec
17090 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  omes a no-op...*
170a0 2a 20 5e 54 68 65 20 76 6f 69 64 20 70 6f 69 6e  * ^The void poin
170b0 74 65 72 20 74 68 61 74 20 69 73 20 74 68 65 20  ter that is the 
170c0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
170d0 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  to SQLITE_CONFIG
170e0 5f 4c 4f 47 20 69 73 0d 0a 2a 2a 20 70 61 73 73  _LOG is..** pass
170f0 65 64 20 74 68 72 6f 75 67 68 20 61 73 20 74 68  ed through as th
17100 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
17110 72 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61  r to the applica
17120 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 6c 6f 67  tion-defined log
17130 67 65 72 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ger..** function
17140 20 77 68 65 6e 65 76 65 72 20 74 68 61 74 20 66   whenever that f
17150 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
17160 65 64 2e 20 20 5e 54 68 65 20 73 65 63 6f 6e 64  ed.  ^The second
17170 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0d 0a 2a   parameter to..*
17180 2a 20 74 68 65 20 6c 6f 67 67 65 72 20 66 75 6e  * the logger fun
17190 63 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20  ction is a copy 
171a0 6f 66 20 74 68 65 20 66 69 72 73 74 20 70 61 72  of the first par
171b0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 6f  ameter to the co
171c0 72 72 65 73 70 6f 6e 64 69 6e 67 0d 0a 2a 2a 20  rresponding..** 
171d0 5b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 5d 20  [sqlite3_log()] 
171e0 63 61 6c 6c 20 61 6e 64 20 69 73 20 69 6e 74 65  call and is inte
171f0 6e 64 65 64 20 74 6f 20 62 65 20 61 20 5b 72 65  nded to be a [re
17200 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 20 61 6e  sult code] or an
17210 0d 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72  ..** [extended r
17220 65 73 75 6c 74 20 63 6f 64 65 5d 2e 20 20 5e 54  esult code].  ^T
17230 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
17240 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  er passed to the
17250 20 6c 6f 67 67 65 72 20 69 73 0d 0a 2a 2a 20 6c   logger is..** l
17260 6f 67 20 6d 65 73 73 61 67 65 20 61 66 74 65 72  og message after
17270 20 66 6f 72 6d 61 74 74 69 6e 67 20 76 69 61 20   formatting via 
17280 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  [sqlite3_snprint
17290 66 28 29 5d 2e 0d 0a 2a 2a 20 54 68 65 20 53 51  f()]...** The SQ
172a0 4c 69 74 65 20 6c 6f 67 67 69 6e 67 20 69 6e 74  Lite logging int
172b0 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 72 65  erface is not re
172c0 65 6e 74 72 61 6e 74 3b 20 74 68 65 20 6c 6f 67  entrant; the log
172d0 67 65 72 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a  ger function..**
172e0 20 73 75 70 70 6c 69 65 64 20 62 79 20 74 68 65   supplied by the
172f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
17300 74 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 61 6e 79  t not invoke any
17310 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
17320 65 2e 0d 0a 2a 2a 20 49 6e 20 61 20 6d 75 6c 74  e...** In a mult
17330 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69  i-threaded appli
17340 63 61 74 69 6f 6e 2c 20 74 68 65 20 61 70 70 6c  cation, the appl
17350 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
17360 6c 6f 67 67 65 72 0d 0a 2a 2a 20 66 75 6e 63 74  logger..** funct
17370 69 6f 6e 20 6d 75 73 74 20 62 65 20 74 68 72 65  ion must be thre
17380 61 64 73 61 66 65 2e 20 3c 2f 64 64 3e 0d 0a 2a  adsafe. </dd>..*
17390 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43  *..** [[SQLITE_C
173a0 4f 4e 46 49 47 5f 55 52 49 5d 5d 20 3c 64 74 3e  ONFIG_URI]] <dt>
173b0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 55 52  SQLITE_CONFIG_UR
173c0 49 0d 0a 2a 2a 20 3c 64 64 3e 20 54 68 69 73 20  I..** <dd> This 
173d0 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  option takes a s
173e0 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 6f  ingle argument o
173f0 66 20 74 79 70 65 20 69 6e 74 2e 20 49 66 20 6e  f type int. If n
17400 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0d 0a 2a  on-zero, then..*
17410 2a 20 55 52 49 20 68 61 6e 64 6c 69 6e 67 20 69  * URI handling i
17420 73 20 67 6c 6f 62 61 6c 6c 79 20 65 6e 61 62 6c  s globally enabl
17430 65 64 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d  ed. If the param
17440 65 74 65 72 20 69 73 20 7a 65 72 6f 2c 20 74 68  eter is zero, th
17450 65 6e 20 55 52 49 20 68 61 6e 64 6c 69 6e 67 0d  en URI handling.
17460 0a 2a 2a 20 69 73 20 67 6c 6f 62 61 6c 6c 79 20  .** is globally 
17470 64 69 73 61 62 6c 65 64 2e 20 49 66 20 55 52 49  disabled. If URI
17480 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 67 6c 6f   handling is glo
17490 62 61 6c 6c 79 20 65 6e 61 62 6c 65 64 2c 20 61  bally enabled, a
174a0 6c 6c 20 66 69 6c 65 6e 61 6d 65 73 0d 0a 2a 2a  ll filenames..**
174b0 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69   passed to [sqli
174c0 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71  te3_open()], [sq
174d0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
174e0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
174f0 36 28 29 5d 20 6f 72 0d 0a 2a 2a 20 73 70 65 63  6()] or..** spec
17500 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66  ified as part of
17510 20 5b 41 54 54 41 43 48 5d 20 63 6f 6d 6d 61 6e   [ATTACH] comman
17520 64 73 20 61 72 65 20 69 6e 74 65 72 70 72 65 74  ds are interpret
17530 65 64 20 61 73 20 55 52 49 73 2c 20 72 65 67 61  ed as URIs, rega
17540 72 64 6c 65 73 73 0d 0a 2a 2a 20 6f 66 20 77 68  rdless..** of wh
17550 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
17560 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52   [SQLITE_OPEN_UR
17570 49 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 77  I] flag is set w
17580 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
17590 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ..** connection 
175a0 69 73 20 6f 70 65 6e 65 64 2e 20 49 66 20 69 74  is opened. If it
175b0 20 69 73 20 67 6c 6f 62 61 6c 6c 79 20 64 69 73   is globally dis
175c0 61 62 6c 65 64 2c 20 66 69 6c 65 6e 61 6d 65 73  abled, filenames
175d0 20 61 72 65 0d 0a 2a 2a 20 6f 6e 6c 79 20 69 6e   are..** only in
175e0 74 65 72 70 72 65 74 65 64 20 61 73 20 55 52 49  terpreted as URI
175f0 73 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f  s if the SQLITE_
17600 4f 50 45 4e 5f 55 52 49 20 66 6c 61 67 20 69 73  OPEN_URI flag is
17610 20 73 65 74 20 77 68 65 6e 20 74 68 65 0d 0a 2a   set when the..*
17620 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
17630 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2e  ction is opened.
17640 20 42 79 20 64 65 66 61 75 6c 74 2c 20 55 52 49   By default, URI
17650 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 67 6c 6f   handling is glo
17660 62 61 6c 6c 79 0d 0a 2a 2a 20 64 69 73 61 62 6c  bally..** disabl
17670 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20  ed. The default 
17680 76 61 6c 75 65 20 6d 61 79 20 62 65 20 63 68 61  value may be cha
17690 6e 67 65 64 20 62 79 20 63 6f 6d 70 69 6c 69 6e  nged by compilin
176a0 67 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20 5b  g with the..** [
176b0 53 51 4c 49 54 45 5f 55 53 45 5f 55 52 49 5d 20  SQLITE_USE_URI] 
176c0 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0d  symbol defined..
176d0 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45  .**..** [[SQLITE
176e0 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 5d  _CONFIG_PCACHE]]
176f0 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47   [[SQLITE_CONFIG
17700 5f 47 45 54 50 43 41 43 48 45 5d 5d 0d 0a 2a 2a  _GETPCACHE]]..**
17710 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
17720 49 47 5f 50 43 41 43 48 45 20 61 6e 64 20 53 51  IG_PCACHE and SQ
17730 4c 49 54 45 5f 43 4f 4e 46 4e 49 47 5f 47 45 54  LITE_CONFNIG_GET
17740 50 43 41 43 48 45 0d 0a 2a 2a 20 3c 64 64 3e 20  PCACHE..** <dd> 
17750 54 68 65 73 65 20 6f 70 74 69 6f 6e 73 20 61 72  These options ar
17760 65 20 6f 62 73 6f 6c 65 74 65 20 61 6e 64 20 73  e obsolete and s
17770 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65  hould not be use
17780 64 20 62 79 20 6e 65 77 20 63 6f 64 65 2e 0d 0a  d by new code...
17790 2a 2a 20 54 68 65 79 20 61 72 65 20 72 65 74 61  ** They are reta
177a0 69 6e 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72  ined for backwar
177b0 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
177c0 20 62 75 74 20 61 72 65 20 6e 6f 77 20 6e 6f 2d   but are now no-
177d0 6f 70 73 2e 0d 0a 2a 2a 20 3c 2f 64 6c 3e 0d 0a  ops...** </dl>..
177e0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
177f0 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
17800 54 48 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69  THREAD  1  /* ni
17810 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  l */..#define SQ
17820 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
17830 49 54 48 52 45 41 44 20 20 20 32 20 20 2f 2a 20  ITHREAD   2  /* 
17840 6e 69 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  nil */..#define 
17850 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
17860 52 49 41 4c 49 5a 45 44 20 20 20 20 33 20 20 2f  RIALIZED    3  /
17870 2a 20 6e 69 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e  * nil */..#defin
17880 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
17890 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 34 20  MALLOC        4 
178a0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f   /* sqlite3_mem_
178b0 6d 65 74 68 6f 64 73 2a 20 2a 2f 0d 0a 23 64 65  methods* */..#de
178c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
178d0 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 20 20 20 20  IG_GETMALLOC    
178e0 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d   5  /* sqlite3_m
178f0 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0d 0a  em_methods* */..
17900 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
17910 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 20 20 20  ONFIG_SCRATCH   
17920 20 20 20 20 36 20 20 2f 2a 20 76 6f 69 64 2a 2c      6  /* void*,
17930 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a   int sz, int N *
17940 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
17950 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
17960 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76 6f 69  HE     7  /* voi
17970 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20  d*, int sz, int 
17980 4e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  N */..#define SQ
17990 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50  LITE_CONFIG_HEAP
179a0 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a 20            8  /* 
179b0 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74 65  void*, int nByte
179c0 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0d 0a 23 64  , int min */..#d
179d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
179e0 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20  FIG_MEMSTATUS   
179f0 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20    9  /* boolean 
17a00 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
17a10 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20  TE_CONFIG_MUTEX 
17a20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71         10  /* sq
17a30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
17a40 6f 64 73 2a 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ods* */..#define
17a50 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
17a60 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20  ETMUTEX     11  
17a70 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
17a80 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0d 0a 2f 2a  _methods* */../*
17a90 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49   previously SQLI
17aa0 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41  TE_CONFIG_CHUNKA
17ab0 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73  LLOC 12 which is
17ac0 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20   now unused. */ 
17ad0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
17ae0 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  _CONFIG_LOOKASID
17af0 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20  E    13  /* int 
17b00 69 6e 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  int */..#define 
17b10 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43  SQLITE_CONFIG_PC
17b20 41 43 48 45 20 20 20 20 20 20 20 31 34 20 20 2f  ACHE       14  /
17b30 2a 20 6e 6f 2d 6f 70 20 2a 2f 0d 0a 23 64 65 66  * no-op */..#def
17b40 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
17b50 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31  G_GETPCACHE    1
17b60 35 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0d 0a  5  /* no-op */..
17b70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
17b80 4f 4e 46 49 47 5f 4c 4f 47 20 20 20 20 20 20 20  ONFIG_LOG       
17b90 20 20 20 31 36 20 20 2f 2a 20 78 46 75 6e 63 2c     16  /* xFunc,
17ba0 20 76 6f 69 64 2a 20 2a 2f 0d 0a 23 64 65 66 69   void* */..#defi
17bb0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
17bc0 5f 55 52 49 20 20 20 20 20 20 20 20 20 20 31 37  _URI          17
17bd0 20 20 2f 2a 20 69 6e 74 20 2a 2f 0d 0a 23 64 65    /* int */..#de
17be0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
17bf0 49 47 5f 50 43 41 43 48 45 32 20 20 20 20 20 20  IG_PCACHE2      
17c00 31 38 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70  18  /* sqlite3_p
17c10 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 2a 20  cache_methods2* 
17c20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
17c30 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41  TE_CONFIG_GETPCA
17c40 43 48 45 32 20 20 20 31 39 20 20 2f 2a 20 73 71  CHE2   19  /* sq
17c50 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
17c60 68 6f 64 73 32 2a 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  hods2* */..../*.
17c70 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
17c80 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
17c90 6e 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  n Configuration 
17ca0 4f 70 74 69 6f 6e 73 0d 0a 2a 2a 0d 0a 2a 2a 20  Options..**..** 
17cb0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
17cc0 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c  are the availabl
17cd0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67  e integer config
17ce0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
17cf0 74 68 61 74 0d 0a 2a 2a 20 63 61 6e 20 62 65 20  that..** can be 
17d00 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
17d10 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
17d20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62   the [sqlite3_db
17d30 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72  _config()] inter
17d40 66 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 65  face...**..** Ne
17d50 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  w configuration 
17d60 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61  options may be a
17d70 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72  dded in future r
17d80 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
17d90 65 2e 0d 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  e...** Existing 
17da0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
17db0 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64  tions might be d
17dc0 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70  iscontinued.  Ap
17dd0 70 6c 69 63 61 74 69 6f 6e 73 0d 0a 2a 2a 20 73  plications..** s
17de0 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20  hould check the 
17df0 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
17e00 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
17e10 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73  fig()] to make s
17e20 75 72 65 20 74 68 61 74 0d 0a 2a 2a 20 74 68 65  ure that..** the
17e30 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 5e   call worked.  ^
17e40 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  The [sqlite3_db_
17e50 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
17e60 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ace will return 
17e70 61 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b  a..** non-zero [
17e80 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61  error code] if a
17e90 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72   discontinued or
17ea0 20 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e   unsupported con
17eb0 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
17ec0 6e 0d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64  n..** is invoked
17ed0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 64 6c 3e 0d 0a  ...**..** <dl>..
17ee0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42  ** <dt>SQLITE_DB
17ef0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
17f00 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 20 5e  </dt>..** <dd> ^
17f10 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
17f20 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e  s three addition
17f30 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  al arguments tha
17f40 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  t determine the 
17f50 0d 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20  ..** [lookaside 
17f60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
17f70 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  ] configuration 
17f80 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73  for the [databas
17f90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0d 0a  e connection]...
17fa0 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20 61 72  ** ^The first ar
17fb0 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72  gument (the thir
17fc0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  d parameter to [
17fd0 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
17fe0 67 28 29 5d 20 69 73 20 61 0d 0a 2a 2a 20 70 6f  g()] is a..** po
17ff0 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72  inter to a memor
18000 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20  y buffer to use 
18010 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  for lookaside me
18020 6d 6f 72 79 2e 0d 0a 2a 2a 20 5e 54 68 65 20 66  mory...** ^The f
18030 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 61 66  irst argument af
18040 74 65 72 20 74 68 65 20 53 51 4c 49 54 45 5f 44  ter the SQLITE_D
18050 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  BCONFIG_LOOKASID
18060 45 20 76 65 72 62 0d 0a 2a 2a 20 6d 61 79 20 62  E verb..** may b
18070 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20  e NULL in which 
18080 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c  case SQLite will
18090 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 0d 0a 2a   allocate the..*
180a0 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  * lookaside buff
180b0 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20  er itself using 
180c0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
180d0 29 5d 2e 20 5e 54 68 65 20 73 65 63 6f 6e 64 20  )]. ^The second 
180e0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0d  argument is the.
180f0 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68  .** size of each
18100 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
18110 72 20 73 6c 6f 74 2e 20 20 5e 54 68 65 20 74 68  r slot.  ^The th
18120 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
18130 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a 2a  the number of..*
18140 2a 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 69  * slots.  The si
18150 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  ze of the buffer
18160 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72   in the first ar
18170 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 67  gument must be g
18180 72 65 61 74 65 72 20 74 68 61 6e 0d 0a 2a 2a 20  reater than..** 
18190 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
181a0 70 72 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73  product of the s
181b0 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
181c0 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20  arguments.  The 
181d0 62 75 66 66 65 72 0d 0a 2a 2a 20 6d 75 73 74 20  buffer..** must 
181e0 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  be aligned to an
181f0 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
18200 2e 20 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e  .  ^If the secon
18210 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0d 0a 2a  d argument to..*
18220 2a 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49  * SQLITE_DBCONFI
18230 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 69 73 20 6e  G_LOOKASIDE is n
18240 6f 74 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66  ot a multiple of
18250 20 38 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e   8, it is intern
18260 61 6c 6c 79 0d 0a 2a 2a 20 72 6f 75 6e 64 65 64  ally..** rounded
18270 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 78   down to the nex
18280 74 20 73 6d 61 6c 6c 65 72 20 6d 75 6c 74 69 70  t smaller multip
18290 6c 65 20 6f 66 20 38 2e 20 20 5e 28 54 68 65 20  le of 8.  ^(The 
182a0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
182b0 0d 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69  ..** configurati
182c0 6f 6e 20 66 6f 72 20 61 20 64 61 74 61 62 61 73  on for a databas
182d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e  e connection can
182e0 20 6f 6e 6c 79 20 62 65 20 63 68 61 6e 67 65 64   only be changed
182f0 20 77 68 65 6e 20 74 68 61 74 0d 0a 2a 2a 20 63   when that..** c
18300 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6e 6f 74  onnection is not
18310 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67   currently using
18320 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
18330 79 2c 20 6f 72 20 69 6e 20 6f 74 68 65 72 20 77  y, or in other w
18340 6f 72 64 73 0d 0a 2a 2a 20 77 68 65 6e 20 74 68  ords..** when th
18350 65 20 22 63 75 72 72 65 6e 74 20 76 61 6c 75 65  e "current value
18360 22 20 72 65 74 75 72 6e 65 64 20 62 79 0d 0a 2a  " returned by..*
18370 2a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74  * [sqlite3_db_st
18380 61 74 75 73 5d 28 44 2c 5b 53 51 4c 49 54 45 5f  atus](D,[SQLITE_
18390 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
183a0 5d 2c 2e 2e 2e 29 20 69 73 20 7a 65 72 6f 2e 0d  ],...) is zero..
183b0 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20  .** Any attempt 
183c0 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f  to change the lo
183d0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 63  okaside memory c
183e0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 77 68 65  onfiguration whe
183f0 6e 20 6c 6f 6f 6b 61 73 69 64 65 0d 0a 2a 2a 20  n lookaside..** 
18400 6d 65 6d 6f 72 79 20 69 73 20 69 6e 20 75 73 65  memory is in use
18410 20 6c 65 61 76 65 73 20 74 68 65 20 63 6f 6e 66   leaves the conf
18420 69 67 75 72 61 74 69 6f 6e 20 75 6e 63 68 61 6e  iguration unchan
18430 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ged and returns 
18440 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53  ..** [SQLITE_BUS
18450 59 5d 2e 29 5e 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a  Y].)^</dd>..**..
18460 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42  ** <dt>SQLITE_DB
18470 43 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f 46 4b  CONFIG_ENABLE_FK
18480 45 59 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e  EY</dt>..** <dd>
18490 20 5e 54 68 69 73 20 6f 70 74 69 6f 6e 20 69 73   ^This option is
184a0 20 75 73 65 64 20 74 6f 20 65 6e 61 62 6c 65 20   used to enable 
184b0 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 65  or disable the e
184c0 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 0d 0a 2a  nforcement of..*
184d0 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  * [foreign key c
184e0 6f 6e 73 74 72 61 69 6e 74 73 5d 2e 20 20 54 68  onstraints].  Th
184f0 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 74 77  ere should be tw
18500 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67  o additional arg
18510 75 6d 65 6e 74 73 2e 0d 0a 2a 2a 20 54 68 65 20  uments...** The 
18520 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
18530 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  s an integer whi
18540 63 68 20 69 73 20 30 20 74 6f 20 64 69 73 61 62  ch is 0 to disab
18550 6c 65 20 46 4b 20 65 6e 66 6f 72 63 65 6d 65 6e  le FK enforcemen
18560 74 2c 0d 0a 2a 2a 20 70 6f 73 69 74 69 76 65 20  t,..** positive 
18570 74 6f 20 65 6e 61 62 6c 65 20 46 4b 20 65 6e 66  to enable FK enf
18580 6f 72 63 65 6d 65 6e 74 20 6f 72 20 6e 65 67 61  orcement or nega
18590 74 69 76 65 20 74 6f 20 6c 65 61 76 65 20 46 4b  tive to leave FK
185a0 20 65 6e 66 6f 72 63 65 6d 65 6e 74 0d 0a 2a 2a   enforcement..**
185b0 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65   unchanged.  The
185c0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
185d0 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
185e0 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 74  o an integer int
185f0 6f 20 77 68 69 63 68 0d 0a 2a 2a 20 69 73 20 77  o which..** is w
18600 72 69 74 74 65 6e 20 30 20 6f 72 20 31 20 74 6f  ritten 0 or 1 to
18610 20 69 6e 64 69 63 61 74 65 20 77 68 65 74 68 65   indicate whethe
18620 72 20 46 4b 20 65 6e 66 6f 72 63 65 6d 65 6e 74  r FK enforcement
18630 20 69 73 20 6f 66 66 20 6f 72 20 6f 6e 0d 0a 2a   is off or on..*
18640 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73  * following this
18650 20 63 61 6c 6c 2e 20 20 54 68 65 20 73 65 63 6f   call.  The seco
18660 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79  nd parameter may
18670 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   be a NULL point
18680 65 72 2c 20 69 6e 0d 0a 2a 2a 20 77 68 69 63 68  er, in..** which
18690 20 63 61 73 65 20 74 68 65 20 46 4b 20 65 6e 66   case the FK enf
186a0 6f 72 63 65 6d 65 6e 74 20 73 65 74 74 69 6e 67  orcement setting
186b0 20 69 73 20 6e 6f 74 20 72 65 70 6f 72 74 65 64   is not reported
186c0 20 62 61 63 6b 2e 20 3c 2f 64 64 3e 0d 0a 2a 2a   back. </dd>..**
186d0 0d 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  ..** <dt>SQLITE_
186e0 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f  DBCONFIG_ENABLE_
186f0 54 52 49 47 47 45 52 3c 2f 64 74 3e 0d 0a 2a 2a  TRIGGER</dt>..**
18700 20 3c 64 64 3e 20 5e 54 68 69 73 20 6f 70 74 69   <dd> ^This opti
18710 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e  on is used to en
18720 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
18730 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  [CREATE TRIGGER 
18740 7c 20 74 72 69 67 67 65 72 73 5d 2e 0d 0a 2a 2a  | triggers]...**
18750 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   There should be
18760 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20   two additional 
18770 61 72 67 75 6d 65 6e 74 73 2e 0d 0a 2a 2a 20 54  arguments...** T
18780 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
18790 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  t is an integer 
187a0 77 68 69 63 68 20 69 73 20 30 20 74 6f 20 64 69  which is 0 to di
187b0 73 61 62 6c 65 20 74 72 69 67 67 65 72 73 2c 0d  sable triggers,.
187c0 0a 2a 2a 20 70 6f 73 69 74 69 76 65 20 74 6f 20  .** positive to 
187d0 65 6e 61 62 6c 65 20 74 72 69 67 67 65 72 73 20  enable triggers 
187e0 6f 72 20 6e 65 67 61 74 69 76 65 20 74 6f 20 6c  or negative to l
187f0 65 61 76 65 20 74 68 65 20 73 65 74 74 69 6e 67  eave the setting
18800 20 75 6e 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a 20   unchanged...** 
18810 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
18820 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65  eter is a pointe
18830 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  r to an integer 
18840 69 6e 74 6f 20 77 68 69 63 68 0d 0a 2a 2a 20 69  into which..** i
18850 73 20 77 72 69 74 74 65 6e 20 30 20 6f 72 20 31  s written 0 or 1
18860 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65   to indicate whe
18870 74 68 65 72 20 74 72 69 67 67 65 72 73 20 61 72  ther triggers ar
18880 65 20 64 69 73 61 62 6c 65 64 20 6f 72 20 65 6e  e disabled or en
18890 61 62 6c 65 64 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77  abled..** follow
188a0 69 6e 67 20 74 68 69 73 20 63 61 6c 6c 2e 20 20  ing this call.  
188b0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
188c0 65 74 65 72 20 6d 61 79 20 62 65 20 61 20 4e 55  eter may be a NU
188d0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69 6e 0d 0a  LL pointer, in..
188e0 2a 2a 20 77 68 69 63 68 20 63 61 73 65 20 74 68  ** which case th
188f0 65 20 74 72 69 67 67 65 72 20 73 65 74 74 69 6e  e trigger settin
18900 67 20 69 73 20 6e 6f 74 20 72 65 70 6f 72 74 65  g is not reporte
18910 64 20 62 61 63 6b 2e 20 3c 2f 64 64 3e 0d 0a 2a  d back. </dd>..*
18920 2a 0d 0a 2a 2a 20 3c 2f 64 6c 3e 0d 0a 2a 2f 0d  *..** </dl>..*/.
18930 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
18940 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
18950 44 45 20 20 20 20 20 20 20 31 30 30 31 20 20 2f  DE       1001  /
18960 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 6e 74 20  * void* int int 
18970 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
18980 54 45 5f 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42  TE_DBCONFIG_ENAB
18990 4c 45 5f 46 4b 45 59 20 20 20 20 20 31 30 30 32  LE_FKEY     1002
189a0 20 20 2f 2a 20 69 6e 74 20 69 6e 74 2a 20 2a 2f    /* int int* */
189b0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
189c0 5f 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c 45  _DBCONFIG_ENABLE
189d0 5f 54 52 49 47 47 45 52 20 20 31 30 30 33 20 20  _TRIGGER  1003  
189e0 2f 2a 20 69 6e 74 20 69 6e 74 2a 20 2a 2f 0d 0a  /* int int* */..
189f0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
18a00 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44  REF: Enable Or D
18a10 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65 64 20  isable Extended 
18a20 52 65 73 75 6c 74 20 43 6f 64 65 73 0d 0a 2a 2a  Result Codes..**
18a30 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
18a40 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  3_extended_resul
18a50 74 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 69 6e  t_codes() routin
18a60 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73  e enables or dis
18a70 61 62 6c 65 73 20 74 68 65 0d 0a 2a 2a 20 5b 65  ables the..** [e
18a80 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
18a90 6f 64 65 73 5d 20 66 65 61 74 75 72 65 20 6f 66  odes] feature of
18aa0 20 53 51 4c 69 74 65 2e 20 5e 54 68 65 20 65 78   SQLite. ^The ex
18ab0 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0d 0a 2a  tended result..*
18ac0 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 73 61  * codes are disa
18ad0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20  bled by default 
18ae0 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63  for historical c
18af0 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0d 0a 2a  ompatibility...*
18b00 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
18b10 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64  t sqlite3_extend
18b20 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
18b30 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e  sqlite3*, int on
18b40 6f 66 66 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  off);..../*..** 
18b50 43 41 50 49 33 52 45 46 3a 20 4c 61 73 74 20 49  CAPI3REF: Last I
18b60 6e 73 65 72 74 20 52 6f 77 69 64 0d 0a 2a 2a 0d  nsert Rowid..**.
18b70 0a 2a 2a 20 5e 45 61 63 68 20 65 6e 74 72 79 20  .** ^Each entry 
18b80 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 74 61 62  in an SQLite tab
18b90 6c 65 20 68 61 73 20 61 20 75 6e 69 71 75 65 20  le has a unique 
18ba0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 0d 0a 2a  64-bit signed..*
18bb0 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63 61  * integer key ca
18bc0 6c 6c 65 64 20 74 68 65 20 5b 52 4f 57 49 44 20  lled the [ROWID 
18bd0 7c 20 22 72 6f 77 69 64 22 5d 2e 20 5e 54 68 65  | "rowid"]. ^The
18be0 20 72 6f 77 69 64 20 69 73 20 61 6c 77 61 79 73   rowid is always
18bf0 20 61 76 61 69 6c 61 62 6c 65 0d 0a 2a 2a 20 61   available..** a
18c00 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 64 20  s an undeclared 
18c10 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 4f 57  column named ROW
18c20 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 4f 57  ID, OID, or _ROW
18c30 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ID_ as long as t
18c40 68 6f 73 65 0d 0a 2a 2a 20 6e 61 6d 65 73 20 61  hose..** names a
18c50 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 64  re not also used
18c60 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 64   by explicitly d
18c70 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 2e  eclared columns.
18c80 20 5e 49 66 0d 0a 2a 2a 20 74 68 65 20 74 61 62   ^If..** the tab
18c90 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20  le has a column 
18ca0 6f 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52  of type [INTEGER
18cb0 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68   PRIMARY KEY] th
18cc0 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0d 0a  en that column..
18cd0 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c  ** is another al
18ce0 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69  ias for the rowi
18cf0 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73  d...**..** ^This
18d00 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
18d10 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
18d20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0d  the most recent.
18d30 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b  .** successful [
18d40 49 4e 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65  INSERT] into the
18d50 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74   database from t
18d60 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
18d70 6e 65 63 74 69 6f 6e 5d 0d 0a 2a 2a 20 69 6e 20  nection]..** in 
18d80 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
18d90 6e 74 2e 20 20 5e 41 73 20 6f 66 20 53 51 4c 69  nt.  ^As of SQLi
18da0 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 37  te version 3.7.7
18db0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 0d  , this routines.
18dc0 0a 2a 2a 20 72 65 63 6f 72 64 73 20 74 68 65 20  .** records the 
18dd0 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
18de0 64 20 6f 66 20 62 6f 74 68 20 6f 72 64 69 6e 61  d of both ordina
18df0 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 5b 76  ry tables and [v
18e00 69 72 74 75 61 6c 20 74 61 62 6c 65 73 5d 2e 0d  irtual tables]..
18e10 0a 2a 2a 20 5e 49 66 20 6e 6f 20 73 75 63 63 65  .** ^If no succe
18e20 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0d  ssful [INSERT]s.
18e30 0a 2a 2a 20 68 61 76 65 20 65 76 65 72 20 6f 63  .** have ever oc
18e40 63 75 72 72 65 64 20 6f 6e 20 74 68 61 74 20 64  curred on that d
18e50 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
18e60 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75  on, zero is retu
18e70 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28  rned...**..** ^(
18e80 49 66 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 6f  If an [INSERT] o
18e90 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 20 74  ccurs within a t
18ea0 72 69 67 67 65 72 20 6f 72 20 77 69 74 68 69 6e  rigger or within
18eb0 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c   a [virtual tabl
18ec0 65 5d 0d 0a 2a 2a 20 6d 65 74 68 6f 64 2c 20 74  e]..** method, t
18ed0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
18ee0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 68 65   will return the
18ef0 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20   [rowid] of the 
18f00 69 6e 73 65 72 74 65 64 0d 0a 2a 2a 20 72 6f 77  inserted..** row
18f10 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20   as long as the 
18f20 74 72 69 67 67 65 72 20 6f 72 20 76 69 72 74 75  trigger or virtu
18f30 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20  al table method 
18f40 69 73 20 72 75 6e 6e 69 6e 67 2e 0d 0a 2a 2a 20  is running...** 
18f50 42 75 74 20 6f 6e 63 65 20 74 68 65 20 74 72 69  But once the tri
18f60 67 67 65 72 20 6f 72 20 76 69 72 74 75 61 6c 20  gger or virtual 
18f70 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 65 6e 64  table method end
18f80 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  s, the value ret
18f90 75 72 6e 65 64 20 0d 0a 2a 2a 20 62 79 20 74 68  urned ..** by th
18fa0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 76 65 72  is routine rever
18fb0 74 73 20 74 6f 20 77 68 61 74 20 69 74 20 77 61  ts to what it wa
18fc0 73 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69  s before the tri
18fd0 67 67 65 72 20 6f 72 20 76 69 72 74 75 61 6c 0d  gger or virtual.
18fe0 0a 2a 2a 20 74 61 62 6c 65 20 6d 65 74 68 6f 64  .** table method
18ff0 20 62 65 67 61 6e 2e 29 5e 0d 0a 2a 2a 0d 0a 2a   began.)^..**..*
19000 2a 20 5e 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74  * ^An [INSERT] t
19010 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 6f  hat fails due to
19020 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69   a constraint vi
19030 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61  olation is not a
19040 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  ..** successful 
19050 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65  [INSERT] and doe
19060 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
19070 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
19080 62 79 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74  by this..** rout
19090 69 6e 65 2e 20 20 5e 54 68 75 73 20 49 4e 53 45  ine.  ^Thus INSE
190a0 52 54 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45  RT OR FAIL, INSE
190b0 52 54 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e  RT OR IGNORE, IN
190c0 53 45 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b  SERT OR ROLLBACK
190d0 2c 0d 0a 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54  ,..** and INSERT
190e0 20 4f 52 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e   OR ABORT make n
190f0 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  o changes to the
19100 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
19110 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e   this..** routin
19120 65 20 77 68 65 6e 20 74 68 65 69 72 20 69 6e 73  e when their ins
19130 65 72 74 69 6f 6e 20 66 61 69 6c 73 2e 20 20 5e  ertion fails.  ^
19140 28 57 68 65 6e 20 49 4e 53 45 52 54 20 4f 52 20  (When INSERT OR 
19150 52 45 50 4c 41 43 45 0d 0a 2a 2a 20 65 6e 63 6f  REPLACE..** enco
19160 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 74 72 61  unters a constra
19170 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 69  int violation, i
19180 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 69 6c 2e  t does not fail.
19190 20 20 54 68 65 0d 0a 2a 2a 20 49 4e 53 45 52 54    The..** INSERT
191a0 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f   continues to co
191b0 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 20 64  mpletion after d
191c0 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 68 61  eleting rows tha
191d0 74 20 63 61 75 73 65 64 0d 0a 2a 2a 20 74 68 65  t caused..** the
191e0 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f 62   constraint prob
191f0 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f 52  lem so INSERT OR
19200 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 6c   REPLACE will al
19210 77 61 79 73 20 63 68 61 6e 67 65 0d 0a 2a 2a 20  ways change..** 
19220 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
19230 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61   of this interfa
19240 63 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 46  ce.)^..**..** ^F
19250 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
19260 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  of this routine,
19270 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 20   an [INSERT] is 
19280 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0d 0a 2a  considered to..*
19290 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20  * be successful 
192a0 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75  even if it is su
192b0 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65  bsequently rolle
192c0 64 20 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  d back...**..** 
192d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
192e0 20 61 63 63 65 73 73 69 62 6c 65 20 74 6f 20 53   accessible to S
192f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 76 69  QL statements vi
19300 61 20 74 68 65 0d 0a 2a 2a 20 5b 6c 61 73 74 5f  a the..** [last_
19310 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53  insert_rowid() S
19320 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0d 0a 2a  QL function]...*
19330 2a 0d 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72  *..** If a separ
19340 61 74 65 20 74 68 72 65 61 64 20 70 65 72 66 6f  ate thread perfo
19350 72 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45 52  rms a new [INSER
19360 54 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0d 0a  T] on the same..
19370 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
19380 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65  ection while the
19390 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69   [sqlite3_last_i
193a0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0d 0a  nsert_rowid()]..
193b0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  ** function is r
193c0 75 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 73 20  unning and thus 
193d0 63 68 61 6e 67 65 73 20 74 68 65 20 6c 61 73 74  changes the last
193e0 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c   insert [rowid],
193f0 0d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61  ..** then the va
19400 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
19410 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e  [sqlite3_last_in
19420 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73  sert_rowid()] is
19430 0d 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74 61 62  ..** unpredictab
19440 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74  le and might not
19450 20 65 71 75 61 6c 20 65 69 74 68 65 72 20 74 68   equal either th
19460 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e 65 77  e old or the new
19470 0d 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72 74  ..** last insert
19480 20 5b 72 6f 77 69 64 5d 2e 0d 0a 2a 2f 0d 0a 53   [rowid]...*/..S
19490 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
194a0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
194b0 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
194c0 64 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 0d 0a  d(sqlite3*);....
194d0 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
194e0 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62 65   Count The Numbe
194f0 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69  r Of Rows Modifi
19500 65 64 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73  ed..**..** ^This
19510 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
19520 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
19530 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68  database rows th
19540 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 0d  at were changed.
19550 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 20  .** or inserted 
19560 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 68  or deleted by th
19570 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
19580 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 74  completed SQL st
19590 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 6f 6e 20 74  atement..** on t
195a0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
195b0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69  nection] specifi
195c0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
195d0 70 61 72 61 6d 65 74 65 72 2e 0d 0a 2a 2a 20 5e  parameter...** ^
195e0 28 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74 68  (Only changes th
195f0 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79 20  at are directly 
19600 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
19610 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41   [INSERT], [UPDA
19620 54 45 5d 2c 0d 0a 2a 2a 20 6f 72 20 5b 44 45 4c  TE],..** or [DEL
19630 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61  ETE] statement a
19640 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78  re counted.  Aux
19650 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 63  iliary changes c
19660 61 75 73 65 64 20 62 79 0d 0a 2a 2a 20 74 72 69  aused by..** tri
19670 67 67 65 72 73 20 6f 72 20 5b 66 6f 72 65 69 67  ggers or [foreig
19680 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d 20 61  n key actions] a
19690 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 29  re not counted.)
196a0 5e 20 55 73 65 20 74 68 65 0d 0a 2a 2a 20 5b 73  ^ Use the..** [s
196b0 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
196c0 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e  nges()] function
196d0 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74   to find the tot
196e0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  al number of cha
196f0 6e 67 65 73 0d 0a 2a 2a 20 69 6e 63 6c 75 64 69  nges..** includi
19700 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ng changes cause
19710 64 20 62 79 20 74 72 69 67 67 65 72 73 20 61 6e  d by triggers an
19720 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63  d foreign key ac
19730 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  tions...**..** ^
19740 43 68 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65  Changes to a vie
19750 77 20 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c  w that are simul
19760 61 74 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54  ated by an [INST
19770 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0d  EAD OF trigger].
19780 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e  .** are not coun
19790 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20  ted.  Only real 
197a0 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20 61 72  table changes ar
197b0 65 20 63 6f 75 6e 74 65 64 2e 0d 0a 2a 2a 0d 0a  e counted...**..
197c0 2a 2a 20 5e 28 41 20 22 72 6f 77 20 63 68 61 6e  ** ^(A "row chan
197d0 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20  ge" is a change 
197e0 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  to a single row 
197f0 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
19800 65 0d 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20  e..** caused by 
19810 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54  an INSERT, DELET
19820 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61  E, or UPDATE sta
19830 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68  tement.  Rows th
19840 61 74 0d 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67  at..** are chang
19850 65 64 20 61 73 20 73 69 64 65 20 65 66 66 65 63  ed as side effec
19860 74 73 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20  ts of [REPLACE] 
19870 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c  constraint resol
19880 75 74 69 6f 6e 2c 0d 0a 2a 2a 20 72 6f 6c 6c 62  ution,..** rollb
19890 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65  ack, ABORT proce
198a0 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42  ssing, [DROP TAB
198b0 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f  LE], or by any o
198c0 74 68 65 72 0d 0a 2a 2a 20 6d 65 63 68 61 6e 69  ther..** mechani
198d0 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74  sms do not count
198e0 20 61 73 20 64 69 72 65 63 74 20 72 6f 77 20 63   as direct row c
198f0 68 61 6e 67 65 73 2e 29 5e 0d 0a 2a 2a 0d 0a 2a  hanges.)^..**..*
19900 2a 20 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e  * A "trigger con
19910 74 65 78 74 22 20 69 73 20 61 20 73 63 6f 70 65  text" is a scope
19920 20 6f 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68   of execution th
19930 61 74 20 62 65 67 69 6e 73 20 61 6e 64 0d 0a 2a  at begins and..*
19940 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68 65 20  * ends with the 
19950 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43 52 45  script of a [CRE
19960 41 54 45 20 54 52 49 47 47 45 52 20 7c 20 74 72  ATE TRIGGER | tr
19970 69 67 67 65 72 5d 2e 20 0d 0a 2a 2a 20 4d 6f 73  igger]. ..** Mos
19980 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  t SQL statements
19990 20 61 72 65 0d 0a 2a 2a 20 65 76 61 6c 75 61 74   are..** evaluat
199a0 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e  ed outside of an
199b0 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73  y trigger.  This
199c0 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76   is the "top lev
199d0 65 6c 22 0d 0a 2a 2a 20 74 72 69 67 67 65 72 20  el"..** trigger 
199e0 63 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74  context.  If a t
199f0 72 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f  rigger fires fro
19a00 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c  m the top level,
19a10 20 61 0d 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67   a..** new trigg
19a20 65 72 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e  er context is en
19a30 74 65 72 65 64 20 66 6f 72 20 74 68 65 20 64 75  tered for the du
19a40 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f  ration of that o
19a50 6e 65 0d 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20  ne..** trigger. 
19a60 20 53 75 62 74 72 69 67 67 65 72 73 20 63 72 65   Subtriggers cre
19a70 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 73 20  ate subcontexts 
19a80 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 74 69  for their durati
19a90 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 43 61 6c  on...**..** ^Cal
19aa0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78  ling [sqlite3_ex
19ab0 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  ec()] or [sqlite
19ac0 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75 72 73  3_step()] recurs
19ad0 69 76 65 6c 79 20 64 6f 65 73 0d 0a 2a 2a 20 6e  ively does..** n
19ae0 6f 74 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  ot create a new 
19af0 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e  trigger context.
19b00 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73 20 66  ..**..** ^This f
19b10 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
19b20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69  the number of di
19b30 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73  rect row changes
19b40 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 6d 6f 73 74   in the..** most
19b50 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20   recent INSERT, 
19b60 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
19b70 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  E statement with
19b80 69 6e 20 74 68 65 20 73 61 6d 65 0d 0a 2a 2a 20  in the same..** 
19b90 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e  trigger context.
19ba0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 75 73 2c 20  ..**..** ^Thus, 
19bb0 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d  when called from
19bc0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20   the top level, 
19bd0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
19be0 74 75 72 6e 73 20 74 68 65 0d 0a 2a 2a 20 6e 75  turns the..** nu
19bf0 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20  mber of changes 
19c00 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
19c10 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  nt INSERT, UPDAT
19c20 45 2c 20 6f 72 20 44 45 4c 45 54 45 0d 0a 2a 2a  E, or DELETE..**
19c30 20 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72   that also occur
19c40 72 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c  red at the top l
19c50 65 76 65 6c 2e 20 20 5e 28 57 69 74 68 69 6e 20  evel.  ^(Within 
19c60 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 74 72  the body of a tr
19c70 69 67 67 65 72 2c 0d 0a 2a 2a 20 74 68 65 20 73  igger,..** the s
19c80 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
19c90 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
19ca0 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e 64  e called to find
19cb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a   the number of..
19cc0 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  ** changes in th
19cd0 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
19ce0 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54  completed INSERT
19cf0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c  , UPDATE, or DEL
19d00 45 54 45 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ETE..** statemen
19d10 74 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64  t within the bod
19d20 79 20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72  y of the same tr
19d30 69 67 67 65 72 2e 0d 0a 2a 2a 20 48 6f 77 65 76  igger...** Howev
19d40 65 72 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 72  er, the number r
19d50 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74  eturned does not
19d60 20 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 73   include changes
19d70 0d 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 73  ..** caused by s
19d80 75 62 74 72 69 67 67 65 72 73 20 73 69 6e 63 65  ubtriggers since
19d90 20 74 68 6f 73 65 20 68 61 76 65 20 74 68 65 69   those have thei
19da0 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2e 29 5e  r own context.)^
19db0 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
19dc0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74  o the [sqlite3_t
19dd0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20  otal_changes()] 
19de0 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 0d 0a  interface, the..
19df0 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65  ** [count_change
19e00 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 74  s pragma], and t
19e10 68 65 20 5b 63 68 61 6e 67 65 73 28 29 20 53 51  he [changes() SQ
19e20 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0d 0a 2a 2a  L function]...**
19e30 0d 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61  ..** If a separa
19e40 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20  te thread makes 
19e50 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73  changes on the s
19e60 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
19e70 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 77 68 69 6c  nection..** whil
19e80 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  e [sqlite3_chang
19e90 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67  es()] is running
19ea0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
19eb0 72 65 74 75 72 6e 65 64 0d 0a 2a 2a 20 69 73 20  returned..** is 
19ec0 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e  unpredictable an
19ed0 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c  d not meaningful
19ee0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
19ef0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68  I int sqlite3_ch
19f00 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b  anges(sqlite3*);
19f10 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
19f20 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 65  REF: Total Numbe
19f30 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69  r Of Rows Modifi
19f40 65 64 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73  ed..**..** ^This
19f50 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
19f60 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
19f70 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73  row changes caus
19f80 65 64 20 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0d  ed by [INSERT],.
19f90 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f 72 20  .** [UPDATE] or 
19fa0 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65  [DELETE] stateme
19fb0 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b 64  nts since the [d
19fc0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
19fd0 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e 0d  on] was opened..
19fe0 0a 2a 2a 20 5e 28 54 68 65 20 63 6f 75 6e 74 20  .** ^(The count 
19ff0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
1a000 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
1a010 73 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c  s() includes all
1a020 20 63 68 61 6e 67 65 73 0d 0a 2a 2a 20 66 72 6f   changes..** fro
1a030 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54 52  m all [CREATE TR
1a040 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d  IGGER | trigger]
1a050 20 63 6f 6e 74 65 78 74 73 20 61 6e 64 20 63 68   contexts and ch
1a060 61 6e 67 65 73 20 6d 61 64 65 20 62 79 0d 0a 2a  anges made by..*
1a070 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 61  * [foreign key a
1a080 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 72  ctions]. However
1a090 2c 0d 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20  ,..** the count 
1a0a0 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
1a0b0 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f   changes used to
1a0c0 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c   implement [REPL
1a0d0 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73  ACE] constraints
1a0e0 2c 0d 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63  ,..** do rollbac
1a0f0 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63  ks or ABORT proc
1a100 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50  essing, or [DROP
1a110 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69   TABLE] processi
1a120 6e 67 2e 20 20 54 68 65 0d 0a 2a 2a 20 63 6f 75  ng.  The..** cou
1a130 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  nt does not incl
1a140 75 64 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77  ude rows of view
1a150 73 20 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b  s that fire an [
1a160 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
1a170 65 72 5d 2c 0d 0a 2a 2a 20 74 68 6f 75 67 68 20  er],..** though 
1a180 69 66 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f  if the INSTEAD O
1a190 46 20 74 72 69 67 67 65 72 20 6d 61 6b 65 73 20  F trigger makes 
1a1a0 63 68 61 6e 67 65 73 20 6f 66 20 69 74 73 20 6f  changes of its o
1a1b0 77 6e 2c 20 74 68 6f 73 65 20 63 68 61 6e 67 65  wn, those change
1a1c0 73 20 0d 0a 2a 2a 20 61 72 65 20 63 6f 75 6e 74  s ..** are count
1a1d0 65 64 2e 29 5e 0d 0a 2a 2a 20 5e 54 68 65 20 73  ed.)^..** ^The s
1a1e0 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
1a1f0 6e 67 65 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  nges() function 
1a200 63 6f 75 6e 74 73 20 74 68 65 20 63 68 61 6e 67  counts the chang
1a210 65 73 20 61 73 20 73 6f 6f 6e 20 61 73 0d 0a 2a  es as soon as..*
1a220 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
1a230 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20  that makes them 
1a240 69 73 20 63 6f 6d 70 6c 65 74 65 64 20 28 77 68  is completed (wh
1a250 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
1a260 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 69 73 20 70   handle..** is p
1a270 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65  assed to [sqlite
1a280 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73  3_reset()] or [s
1a290 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1a2a0 29 5d 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  )])...**..** See
1a2b0 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
1a2c0 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e  e3_changes()] in
1a2d0 74 65 72 66 61 63 65 2c 20 74 68 65 0d 0a 2a 2a  terface, the..**
1a2e0 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20   [count_changes 
1a2f0 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 74 68 65  pragma], and the
1a300 20 5b 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28   [total_changes(
1a310 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e  ) SQL function].
1a320 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20 73 65  ..**..** If a se
1a330 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61  parate thread ma
1a340 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74  kes changes on t
1a350 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
1a360 20 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20   connection..** 
1a370 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74  while [sqlite3_t
1a380 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20  otal_changes()] 
1a390 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20  is running then 
1a3a0 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20 72 65  the value..** re
1a3b0 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64  turned is unpred
1a3c0 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20  ictable and not 
1a3d0 6d 65 61 6e 69 6e 67 66 75 6c 2e 0d 0a 2a 2f 0d  meaningful...*/.
1a3e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a3f0 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
1a400 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b  anges(sqlite3*);
1a410 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
1a420 52 45 46 3a 20 49 6e 74 65 72 72 75 70 74 20 41  REF: Interrupt A
1a430 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 75   Long-Running Qu
1a440 65 72 79 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69  ery..**..** ^Thi
1a450 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  s function cause
1a460 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 61  s any pending da
1a470 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e  tabase operation
1a480 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0d 0a 2a   to abort and..*
1a490 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20  * return at its 
1a4a0 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75  earliest opportu
1a4b0 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69  nity. This routi
1a4c0 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0d  ne is typically.
1a4d0 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65  .** called in re
1a4e0 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72  sponse to a user
1a4f0 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20   action such as 
1a500 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c  pressing "Cancel
1a510 22 0d 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20  "..** or Ctrl-C 
1a520 77 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77  where the user w
1a530 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72  ants a long quer
1a540 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68  y operation to h
1a550 61 6c 74 0d 0a 2a 2a 20 69 6d 6d 65 64 69 61 74  alt..** immediat
1a560 65 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 74  ely...**..** ^It
1a570 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c   is safe to call
1a580 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   this routine fr
1a590 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66 66  om a thread diff
1a5a0 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0d 0a  erent from the..
1a5b0 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69  ** thread that i
1a5c0 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e  s currently runn
1a5d0 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
1a5e0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
1a5f0 20 69 74 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 73   it..** is not s
1a600 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  afe to call this
1a610 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20   routine with a 
1a620 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1a630 74 69 6f 6e 5d 20 74 68 61 74 0d 0a 2a 2a 20 69  tion] that..** i
1a640 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68  s closed or migh
1a650 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73  t close before s
1a660 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1a670 28 29 20 72 65 74 75 72 6e 73 2e 0d 0a 2a 2a 0d  () returns...**.
1a680 0a 2a 2a 20 5e 49 66 20 61 6e 20 53 51 4c 20 6f  .** ^If an SQL o
1a690 70 65 72 61 74 69 6f 6e 20 69 73 20 76 65 72 79  peration is very
1a6a0 20 6e 65 61 72 6c 79 20 66 69 6e 69 73 68 65 64   nearly finished
1a6b0 20 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 65   at the time whe
1a6c0 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e  n..** sqlite3_in
1a6d0 74 65 72 72 75 70 74 28 29 20 69 73 20 63 61 6c  terrupt() is cal
1a6e0 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  led, then it mig
1a6f0 68 74 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f  ht not have an o
1a700 70 70 6f 72 74 75 6e 69 74 79 0d 0a 2a 2a 20 74  pportunity..** t
1a710 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74 65 64  o be interrupted
1a720 20 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e 74 69   and might conti
1a730 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f  nue to completio
1a740 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 41 6e 20 53  n...**..** ^An S
1a750 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  QL operation tha
1a760 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
1a770 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
1a780 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e  LITE_INTERRUPT].
1a790 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 69 6e 74  ..** ^If the int
1a7a0 65 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65  errupted SQL ope
1a7b0 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53  ration is an INS
1a7c0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
1a7d0 44 45 4c 45 54 45 0d 0a 2a 2a 20 74 68 61 74 20  DELETE..** that 
1a7e0 69 73 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70  is inside an exp
1a7f0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f  licit transactio
1a800 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69  n, then the enti
1a810 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0d 0a  re transaction..
1a820 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65  ** will be rolle
1a830 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63  d back automatic
1a840 61 6c 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  ally...**..** ^T
1a850 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
1a860 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20  rupt(D) call is 
1a870 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20  in effect until 
1a880 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75  all currently ru
1a890 6e 6e 69 6e 67 0d 0a 2a 2a 20 53 51 4c 20 73 74  nning..** SQL st
1a8a0 61 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74  atements on [dat
1a8b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1a8c0 5d 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 5e  ] D complete.  ^
1a8d0 41 6e 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74  Any new SQL stat
1a8e0 65 6d 65 6e 74 73 0d 0a 2a 2a 20 74 68 61 74 20  ements..** that 
1a8f0 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65  are started afte
1a900 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  r the sqlite3_in
1a910 74 65 72 72 75 70 74 28 29 20 63 61 6c 6c 20 61  terrupt() call a
1a920 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20 0d 0a  nd before the ..
1a930 2a 2a 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65  ** running state
1a940 6d 65 6e 74 73 20 72 65 61 63 68 65 73 20 7a 65  ments reaches ze
1a950 72 6f 20 61 72 65 20 69 6e 74 65 72 72 75 70 74  ro are interrupt
1a960 65 64 20 61 73 20 69 66 20 74 68 65 79 20 68 61  ed as if they ha
1a970 64 20 62 65 65 6e 0d 0a 2a 2a 20 72 75 6e 6e 69  d been..** runni
1a980 6e 67 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ng prior to the 
1a990 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1a9a0 74 28 29 20 63 61 6c 6c 2e 20 20 5e 4e 65 77 20  t() call.  ^New 
1a9b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0d 0a  SQL statements..
1a9c0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72  ** that are star
1a9d0 74 65 64 20 61 66 74 65 72 20 74 68 65 20 72 75  ted after the ru
1a9e0 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  nning statement 
1a9f0 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
1aa00 72 6f 20 61 72 65 0d 0a 2a 2a 20 6e 6f 74 20 65  ro are..** not e
1aa10 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20 73  ffected by the s
1aa20 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1aa30 28 29 2e 0d 0a 2a 2a 20 5e 41 20 63 61 6c 6c 20  ()...** ^A call 
1aa40 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  to sqlite3_inter
1aa50 72 75 70 74 28 44 29 20 74 68 61 74 20 6f 63 63  rupt(D) that occ
1aa60 75 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 61  urs when there a
1aa70 72 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0d 0a 2a  re no running..*
1aa80 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
1aa90 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20   is a no-op and 
1aaa0 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
1aab0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0d   SQL statements.
1aac0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61  .** that are sta
1aad0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73  rted after the s
1aae0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1aaf0 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  () call returns.
1ab00 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
1ab10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1ab20 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68 69 6c 65  ion closes while
1ab30 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72   [sqlite3_interr
1ab40 75 70 74 28 29 5d 0d 0a 2a 2a 20 69 73 20 72 75  upt()]..** is ru
1ab50 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 64 20 74  nning then bad t
1ab60 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  hings will likel
1ab70 79 20 68 61 70 70 65 6e 2e 0d 0a 2a 2f 0d 0a 53  y happen...*/..S
1ab80 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1ab90 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1aba0 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 0d 0a 2f  (sqlite3*);..../
1abb0 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  *..** CAPI3REF: 
1abc0 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 6e 20  Determine If An 
1abd0 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 73  SQL Statement Is
1abe0 20 43 6f 6d 70 6c 65 74 65 0d 0a 2a 2a 0d 0a 2a   Complete..**..*
1abf0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
1ac00 20 61 72 65 20 75 73 65 66 75 6c 20 64 75 72 69   are useful duri
1ac10 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20  ng command-line 
1ac20 69 6e 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69  input to determi
1ac30 6e 65 20 69 66 20 74 68 65 0d 0a 2a 2a 20 63 75  ne if the..** cu
1ac40 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
1ac50 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20 66 6f  text seems to fo
1ac60 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51  rm a complete SQ
1ac70 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0d 0a  L statement or..
1ac80 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61 6c  ** if additional
1ac90 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65 64   input is needed
1aca0 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 20   before sending 
1acb0 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0d 0a 2a  the text into..*
1acc0 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72  * SQLite for par
1acd0 73 69 6e 67 2e 20 20 5e 54 68 65 73 65 20 72 6f  sing.  ^These ro
1ace0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 20  utines return 1 
1acf0 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  if the input str
1ad00 69 6e 67 0d 0a 2a 2a 20 61 70 70 65 61 72 73 20  ing..** appears 
1ad10 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65  to be a complete
1ad20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
1ad30 20 5e 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73   ^A statement is
1ad40 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0d 0a 2a   judged to be..*
1ad50 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74  * complete if it
1ad60 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d   ends with a sem
1ad70 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64  icolon token and
1ad80 20 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78   is not a prefix
1ad90 20 6f 66 20 61 0d 0a 2a 2a 20 77 65 6c 6c 2d 66   of a..** well-f
1ada0 6f 72 6d 65 64 20 43 52 45 41 54 45 20 54 52 49  ormed CREATE TRI
1adb0 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20  GGER statement. 
1adc0 20 5e 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61   ^Semicolons tha
1add0 74 20 61 72 65 20 65 6d 62 65 64 64 65 64 20 77  t are embedded w
1ade0 69 74 68 69 6e 0d 0a 2a 2a 20 73 74 72 69 6e 67  ithin..** string
1adf0 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 71 75 6f   literals or quo
1ae00 74 65 64 20 69 64 65 6e 74 69 66 69 65 72 20 6e  ted identifier n
1ae10 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73  ames or comments
1ae20 20 61 72 65 20 6e 6f 74 0d 0a 2a 2a 20 69 6e 64   are not..** ind
1ae30 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20  ependent tokens 
1ae40 28 74 68 65 79 20 61 72 65 20 70 61 72 74 20 6f  (they are part o
1ae50 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20 77  f the token in w
1ae60 68 69 63 68 20 74 68 65 79 20 61 72 65 0d 0a 2a  hich they are..*
1ae70 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 20  * embedded) and 
1ae80 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e  thus do not coun
1ae90 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74  t as a statement
1aea0 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 5e 57   terminator.  ^W
1aeb0 68 69 74 65 73 70 61 63 65 0d 0a 2a 2a 20 61 6e  hitespace..** an
1aec0 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20  d comments that 
1aed0 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c  follow the final
1aee0 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69   semicolon are i
1aef0 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  gnored...**..** 
1af00 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20  ^These routines 
1af10 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20  return 0 if the 
1af20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69 6e 63  statement is inc
1af30 6f 6d 70 6c 65 74 65 2e 20 20 5e 49 66 20 61 0d  omplete.  ^If a.
1af40 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  .** memory alloc
1af50 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65  ation fails, the
1af60 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  n SQLITE_NOMEM i
1af70 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d  s returned...**.
1af80 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 69  .** ^These routi
1af90 6e 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65  nes do not parse
1afa0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1afb0 6e 74 73 20 74 68 75 73 0d 0a 2a 2a 20 77 69 6c  nts thus..** wil
1afc0 6c 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e  l not detect syn
1afd0 74 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72  tactically incor
1afe0 72 65 63 74 20 53 51 4c 2e 0d 0a 2a 2a 0d 0a 2a  rect SQL...**..*
1aff0 2a 20 5e 28 49 66 20 53 51 4c 69 74 65 20 68 61  * ^(If SQLite ha
1b000 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74 69  s not been initi
1b010 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 5b 73 71  alized using [sq
1b020 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
1b030 28 29 5d 20 70 72 69 6f 72 20 0d 0a 2a 2a 20 74  ()] prior ..** t
1b040 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  o invoking sqlit
1b050 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 20  e3_complete16() 
1b060 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69  then sqlite3_ini
1b070 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
1b080 6f 6b 65 64 0d 0a 2a 2a 20 61 75 74 6f 6d 61 74  oked..** automat
1b090 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65  ically by sqlite
1b0a0 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e 20  3_complete16(). 
1b0b0 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 6c   If that initial
1b0c0 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0d 0a  ization fails,..
1b0d0 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  ** then the retu
1b0e0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  rn value from sq
1b0f0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
1b100 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a  () will be non-z
1b110 65 72 6f 0d 0a 2a 2a 20 72 65 67 61 72 64 6c 65  ero..** regardle
1b120 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
1b130 20 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53   not the input S
1b140 51 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 29  QL is complete.)
1b150 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e  ^..**..** The in
1b160 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
1b170 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74  complete()] must
1b180 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   be a zero-termi
1b190 6e 61 74 65 64 0d 0a 2a 2a 20 55 54 46 2d 38 20  nated..** UTF-8 
1b1a0 73 74 72 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  string...**..** 
1b1b0 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71  The input to [sq
1b1c0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
1b1d0 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65  ()] must be a ze
1b1e0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0d 0a 2a  ro-terminated..*
1b1f0 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20  * UTF-16 string 
1b200 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  in native byte o
1b210 72 64 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  rder...*/..SQLIT
1b220 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b230 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74  3_complete(const
1b240 20 63 68 61 72 20 2a 73 71 6c 29 3b 0d 0a 53 51   char *sql);..SQ
1b250 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b260 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
1b270 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29  const void *sql)
1b280 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
1b290 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41  3REF: Register A
1b2a0 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e   Callback To Han
1b2b0 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20  dle SQLITE_BUSY 
1b2c0 45 72 72 6f 72 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  Errors..**..** ^
1b2d0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
1b2e0 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  s a callback fun
1b2f0 63 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74  ction that might
1b300 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
1b310 65 76 65 72 0d 0a 2a 2a 20 61 6e 20 61 74 74 65  ever..** an atte
1b320 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  mpt is made to o
1b330 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 74  pen a database t
1b340 61 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65  able that anothe
1b350 72 20 74 68 72 65 61 64 0d 0a 2a 2a 20 6f 72 20  r thread..** or 
1b360 70 72 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b  process has lock
1b370 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20  ed...**..** ^If 
1b380 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
1b390 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  k is NULL, then 
1b3a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72  [SQLITE_BUSY] or
1b3b0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
1b3c0 4c 4f 43 4b 45 44 5d 0d 0a 2a 2a 20 69 73 20 72  LOCKED]..** is r
1b3d0 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
1b3e0 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74  ely upon encount
1b3f0 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20  ering the lock. 
1b400 20 5e 49 66 20 74 68 65 20 62 75 73 79 20 63 61   ^If the busy ca
1b410 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 69 73 20 6e 6f  llback..** is no
1b420 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  t NULL, then the
1b430 20 63 61 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20   callback might 
1b440 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20  be invoked with 
1b450 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0d 0a  two arguments...
1b460 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73  **..** ^The firs
1b470 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
1b480 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69  e busy handler i
1b490 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
1b4a0 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 77 68  void* pointer wh
1b4b0 69 63 68 0d 0a 2a 2a 20 69 73 20 74 68 65 20 74  ich..** is the t
1b4c0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  hird argument to
1b4d0 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61   sqlite3_busy_ha
1b4e0 6e 64 6c 65 72 28 29 2e 20 20 5e 54 68 65 20 73  ndler().  ^The s
1b4f0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
1b500 6f 0d 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 68  o..** the busy h
1b510 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20  andler callback 
1b520 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1b530 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20   times that the 
1b540 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61 73  busy handler has
1b550 0d 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65  ..** been invoke
1b560 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69  d for this locki
1b570 6e 67 20 65 76 65 6e 74 2e 20 20 5e 49 66 20 74  ng event.  ^If t
1b580 68 65 0d 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c  he..** busy call
1b590 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20  back returns 0, 
1b5a0 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e  then no addition
1b5b0 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20  al attempts are 
1b5c0 6d 61 64 65 20 74 6f 0d 0a 2a 2a 20 61 63 63 65  made to..** acce
1b5d0 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ss the database 
1b5e0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  and [SQLITE_BUSY
1b5f0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ] or [SQLITE_IOE
1b600 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72  RR_BLOCKED] is r
1b610 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 5e 49 66  eturned...** ^If
1b620 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   the callback re
1b630 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
1b640 74 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74  then another att
1b650 65 6d 70 74 0d 0a 2a 2a 20 69 73 20 6d 61 64 65  empt..** is made
1b660 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74   to open the dat
1b670 61 62 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e  abase for readin
1b680 67 20 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20  g and the cycle 
1b690 72 65 70 65 61 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  repeats...**..**
1b6a0 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   The presence of
1b6b0 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   a busy handler 
1b6c0 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e 74  does not guarant
1b6d0 65 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  ee that it will 
1b6e0 62 65 20 69 6e 76 6f 6b 65 64 0d 0a 2a 2a 20 77  be invoked..** w
1b6f0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63  hen there is loc
1b700 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 5e 49  k contention. ^I
1b710 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69  f SQLite determi
1b720 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e  nes that invokin
1b730 67 20 74 68 65 20 62 75 73 79 0d 0a 2a 2a 20 68  g the busy..** h
1b740 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73  andler could res
1b750 75 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63  ult in a deadloc
1b760 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68  k, it will go ah
1b770 65 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b  ead and return [
1b780 53 51 4c 49 54 45 5f 42 55 53 59 5d 0d 0a 2a 2a  SQLITE_BUSY]..**
1b790 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   or [SQLITE_IOER
1b7a0 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65  R_BLOCKED] inste
1b7b0 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74  ad of invoking t
1b7c0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e  he busy handler.
1b7d0 0d 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20  ..** Consider a 
1b7e0 73 63 65 6e 61 72 69 6f 20 77 68 65 72 65 20 6f  scenario where o
1b7f0 6e 65 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f  ne process is ho
1b800 6c 64 69 6e 67 20 61 20 72 65 61 64 20 6c 6f 63  lding a read loc
1b810 6b 20 74 68 61 74 0d 0a 2a 2a 20 69 74 20 69 73  k that..** it is
1b820 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f   trying to promo
1b830 74 65 20 74 6f 20 61 20 72 65 73 65 72 76 65 64  te to a reserved
1b840 20 6c 6f 63 6b 20 61 6e 64 0d 0a 2a 2a 20 61 20   lock and..** a 
1b850 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 69  second process i
1b860 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73 65  s holding a rese
1b870 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20 69  rved lock that i
1b880 74 20 69 73 20 74 72 79 69 6e 67 0d 0a 2a 2a 20  t is trying..** 
1b890 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e  to promote to an
1b8a0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
1b8b0 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f 63    The first proc
1b8c0 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65  ess cannot proce
1b8d0 65 64 0d 0a 2a 2a 20 62 65 63 61 75 73 65 20 69  ed..** because i
1b8e0 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
1b8f0 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74  the second and t
1b900 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73  he second proces
1b910 73 20 63 61 6e 6e 6f 74 0d 0a 2a 2a 20 70 72 6f  s cannot..** pro
1b920 63 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20  ceed because it 
1b930 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68  is blocked by th
1b940 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74  e first.  If bot
1b950 68 20 70 72 6f 63 65 73 73 65 73 0d 0a 2a 2a 20  h processes..** 
1b960 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
1b970 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65  handlers, neithe
1b980 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20  r will make any 
1b990 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65  progress.  There
1b9a0 66 6f 72 65 2c 0d 0a 2a 2a 20 53 51 4c 69 74 65  fore,..** SQLite
1b9b0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
1b9c0 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66  _BUSY] for the f
1b9d0 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f  irst process, ho
1b9e0 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0d 0a  ping that this..
1b9f0 2a 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74  ** will induce t
1ba00 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73  he first process
1ba10 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20   to release its 
1ba20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c  read lock and al
1ba30 6c 6f 77 0d 0a 2a 2a 20 74 68 65 20 73 65 63 6f  low..** the seco
1ba40 6e 64 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72  nd process to pr
1ba50 6f 63 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  oceed...**..** ^
1ba60 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73 79  The default busy
1ba70 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c   callback is NUL
1ba80 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  L...**..** ^The 
1ba90 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72  [SQLITE_BUSY] er
1baa0 72 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64  ror is converted
1bab0 20 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   to [SQLITE_IOER
1bac0 52 5f 42 4c 4f 43 4b 45 44 5d 0d 0a 2a 2a 20 77  R_BLOCKED]..** w
1bad0 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e  hen SQLite is in
1bae0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61   the middle of a
1baf0 20 6c 61 72 67 65 20 74 72 61 6e 73 61 63 74 69   large transacti
1bb00 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65  on where all the
1bb10 0d 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77 69 6c  ..** changes wil
1bb20 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f 20 74  l not fit into t
1bb30 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63  he in-memory cac
1bb40 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  he.  SQLite will
1bb50 0d 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f 6c  ..** already hol
1bb60 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  d a RESERVED loc
1bb70 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1bb80 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20 6e  e file, but it n
1bb90 65 65 64 73 0d 0a 2a 2a 20 74 6f 20 70 72 6f 6d  eeds..** to prom
1bba0 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f  ote this lock to
1bbb0 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68   EXCLUSIVE so th
1bbc0 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20  at it can spill 
1bbd0 63 61 63 68 65 0d 0a 2a 2a 20 70 61 67 65 73 20  cache..** pages 
1bbe0 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
1bbf0 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68  e file without h
1bc00 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e  arm to concurren
1bc10 74 0d 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20  t..** readers.  
1bc20 5e 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65  ^If it is unable
1bc30 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20   to promote the 
1bc40 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69  lock, then the i
1bc50 6e 2d 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 63 61 63  n-memory..** cac
1bc60 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  he will be left 
1bc70 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
1bc80 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20  nt state and so 
1bc90 74 68 65 20 65 72 72 6f 72 0d 0a 2a 2a 20 63 6f  the error..** co
1bca0 64 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66  de is promoted f
1bcb0 72 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65  rom the relative
1bcc0 6c 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54  ly benign [SQLIT
1bcd0 45 5f 42 55 53 59 5d 20 74 6f 0d 0a 2a 2a 20 74  E_BUSY] to..** t
1bce0 68 65 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b  he more severe [
1bcf0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
1bd00 43 4b 45 44 5d 2e 20 20 5e 54 68 69 73 20 65 72  CKED].  ^This er
1bd10 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69  ror code promoti
1bd20 6f 6e 0d 0a 2a 2a 20 66 6f 72 63 65 73 20 61 6e  on..** forces an
1bd30 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62   automatic rollb
1bd40 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61 6e 67  ack of the chang
1bd50 65 73 2e 20 20 53 65 65 20 74 68 65 0d 0a 2a 2a  es.  See the..**
1bd60 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 74 72   <a href="/cvstr
1bd70 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 70  ac/wiki?p=Corrup
1bd80 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73  tionFollowingBus
1bd90 79 45 72 72 6f 72 22 3e 0d 0a 2a 2a 20 43 6f 72  yError">..** Cor
1bda0 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67  ruptionFollowing
1bdb0 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69  BusyError</a> wi
1bdc0 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 69  ki page for a di
1bdd0 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0d  scussion of why.
1bde0 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f  .** this is impo
1bdf0 72 74 61 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  rtant...**..** ^
1be00 28 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20  (There can only 
1be10 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79  be a single busy
1be20 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64   handler defined
1be30 20 66 6f 72 20 65 61 63 68 0d 0a 2a 2a 20 5b 64   for each..** [d
1be40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1be50 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20  on].  Setting a 
1be60 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72  new busy handler
1be70 20 63 6c 65 61 72 73 20 61 6e 79 0d 0a 2a 2a 20   clears any..** 
1be80 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68  previously set h
1be90 61 6e 64 6c 65 72 2e 29 5e 20 20 5e 4e 6f 74 65  andler.)^  ^Note
1bea0 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73   that calling [s
1beb0 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
1bec0 6f 75 74 28 29 5d 0d 0a 2a 2a 20 77 69 6c 6c 20  out()]..** will 
1bed0 61 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61  also set or clea
1bee0 72 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  r the busy handl
1bef0 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  er...**..** The 
1bf00 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 73 68  busy callback sh
1bf10 6f 75 6c 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e  ould not take an
1bf20 79 20 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20  y actions which 
1bf30 6d 6f 64 69 66 79 20 74 68 65 0d 0a 2a 2a 20 64  modify the..** d
1bf40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1bf50 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  on that invoked 
1bf60 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
1bf70 2e 20 20 41 6e 79 20 73 75 63 68 20 61 63 74 69  .  Any such acti
1bf80 6f 6e 73 0d 0a 2a 2a 20 72 65 73 75 6c 74 20 69  ons..** result i
1bf90 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61  n undefined beha
1bfa0 76 69 6f 72 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 41  vior...** ..** A
1bfb0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 6d 75   busy handler mu
1bfc0 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65  st not close the
1bfd0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1bfe0 74 69 6f 6e 0d 0a 2a 2a 20 6f 72 20 5b 70 72 65  tion..** or [pre
1bff0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
1c000 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
1c010 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0d  e busy handler..
1c020 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
1c030 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
1c040 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
1c050 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  *, int(*)(void*,
1c060 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0d 0a 0d  int), void*);...
1c070 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
1c080 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 6d  : Set A Busy Tim
1c090 65 6f 75 74 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  eout..**..** ^Th
1c0a0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
1c0b0 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  a [sqlite3_busy_
1c0c0 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68  handler | busy h
1c0d0 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65  andler] that sle
1c0e0 65 70 73 0d 0a 2a 2a 20 66 6f 72 20 61 20 73 70  eps..** for a sp
1c0f0 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f  ecified amount o
1c100 66 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61  f time when a ta
1c110 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20  ble is locked.  
1c120 5e 54 68 65 20 68 61 6e 64 6c 65 72 0d 0a 2a 2a  ^The handler..**
1c130 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74   will sleep mult
1c140 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 6c  iple times until
1c150 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d   at least "ms" m
1c160 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
1c170 6c 65 65 70 69 6e 67 0d 0a 2a 2a 20 68 61 76 65  leeping..** have
1c180 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 20 5e   accumulated.  ^
1c190 41 66 74 65 72 20 61 74 20 6c 65 61 73 74 20 22  After at least "
1c1a0 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  ms" milliseconds
1c1b0 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0d 0a 2a   of sleeping,..*
1c1c0 2a 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65  * the handler re
1c1d0 74 75 72 6e 73 20 30 20 77 68 69 63 68 20 63 61  turns 0 which ca
1c1e0 75 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74  uses [sqlite3_st
1c1f0 65 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0d  ep()] to return.
1c200 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  .** [SQLITE_BUSY
1c210 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ] or [SQLITE_IOE
1c220 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 0d 0a 2a 2a  RR_BLOCKED]...**
1c230 0d 0a 2a 2a 20 5e 43 61 6c 6c 69 6e 67 20 74 68  ..** ^Calling th
1c240 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
1c250 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 73 73  an argument less
1c260 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1c270 6f 20 7a 65 72 6f 0d 0a 2a 2a 20 74 75 72 6e 73  o zero..** turns
1c280 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 61   off all busy ha
1c290 6e 64 6c 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ndlers...**..** 
1c2a0 5e 28 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  ^(There can only
1c2b0 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
1c2c0 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20  y handler for a 
1c2d0 70 61 72 74 69 63 75 6c 61 72 0d 0a 2a 2a 20 5b  particular..** [
1c2e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c2f0 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76  ion] any any giv
1c300 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61  en moment.  If a
1c310 6e 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64  nother busy hand
1c320 6c 65 72 0d 0a 2a 2a 20 77 61 73 20 64 65 66 69  ler..** was defi
1c330 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c  ned  (using [sql
1c340 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
1c350 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63  r()]) prior to c
1c360 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 74 68 69 73 20  alling..** this 
1c370 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74  routine, that ot
1c380 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72  her busy handler
1c390 20 69 73 20 63 6c 65 61 72 65 64 2e 29 5e 0d 0a   is cleared.)^..
1c3a0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
1c3b0 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
1c3c0 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a  timeout(sqlite3*
1c3d0 2c 20 69 6e 74 20 6d 73 29 3b 0d 0a 0d 0a 2f 2a  , int ms);..../*
1c3e0 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  ..** CAPI3REF: C
1c3f0 6f 6e 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69  onvenience Routi
1c400 6e 65 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20  nes For Running 
1c410 51 75 65 72 69 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20  Queries..**..** 
1c420 54 68 69 73 20 69 73 20 61 20 6c 65 67 61 63 79  This is a legacy
1c430 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
1c440 69 73 20 70 72 65 73 65 72 76 65 64 20 66 6f 72  is preserved for
1c450 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
1c460 74 69 62 69 6c 69 74 79 2e 0d 0a 2a 2a 20 55 73  tibility...** Us
1c470 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66  e of this interf
1c480 61 63 65 20 69 73 20 6e 6f 74 20 72 65 63 6f 6d  ace is not recom
1c490 6d 65 6e 64 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  mended...**..** 
1c4a0 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62  Definition: A <b
1c4b0 3e 72 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62  >result table</b
1c4c0 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61  > is memory data
1c4d0 20 73 74 72 75 63 74 75 72 65 20 63 72 65 61 74   structure creat
1c4e0 65 64 20 62 79 20 74 68 65 0d 0a 2a 2a 20 5b 73  ed by the..** [s
1c4f0 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
1c500 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20  ()] interface.  
1c510 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 72  A result table r
1c520 65 63 6f 72 64 73 20 74 68 65 0d 0a 2a 2a 20 63  ecords the..** c
1c530 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72 65  omplete query re
1c540 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20 6f  sults from one o
1c550 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e 0d  r more queries..
1c560 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 61 62 6c  .**..** The tabl
1c570 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68  e conceptually h
1c580 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72  as a number of r
1c590 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e  ows and columns.
1c5a0 20 20 42 75 74 0d 0a 2a 2a 20 74 68 65 73 65 20    But..** these 
1c5b0 6e 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20  numbers are not 
1c5c0 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75  part of the resu
1c5d0 6c 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e  lt table itself.
1c5e0 20 20 54 68 65 73 65 0d 0a 2a 2a 20 6e 75 6d 62    These..** numb
1c5f0 65 72 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64  ers are obtained
1c600 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65   separately.  Le
1c610 74 20 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65  t N be the numbe
1c620 72 20 6f 66 20 72 6f 77 73 0d 0a 2a 2a 20 61 6e  r of rows..** an
1c630 64 20 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 65  d M be the numbe
1c640 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0d 0a 2a  r of columns...*
1c650 2a 0d 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  *..** A result t
1c660 61 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 79  able is an array
1c670 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
1c680 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
1c690 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0d 0a  UTF-8 strings...
1c6a0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b  ** There are (N+
1c6b0 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e  1)*M elements in
1c6c0 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 65   the array.  The
1c6d0 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72   first M pointer
1c6e0 73 20 70 6f 69 6e 74 0d 0a 2a 2a 20 74 6f 20 7a  s point..** to z
1c6f0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ero-terminated s
1c700 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f 6e  trings that  con
1c710 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f  tain the names o
1c720 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0d 0a  f the columns...
1c730 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67  ** The remaining
1c740 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69   entries all poi
1c750 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 75  nt to query resu
1c760 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65  lts.  NULL value
1c770 73 20 72 65 73 75 6c 74 0d 0a 2a 2a 20 69 6e 20  s result..** in 
1c780 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20  NULL pointers.  
1c790 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73  All other values
1c7a0 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54   are in their UT
1c7b0 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  F-8 zero-termina
1c7c0 74 65 64 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 72  ted..** string r
1c7d0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73  epresentation as
1c7e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
1c7f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1c800 74 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20  t()]...**..** A 
1c810 72 65 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67  result table mig
1c820 68 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e  ht consist of on
1c830 65 20 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79  e or more memory
1c840 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0d 0a 2a   allocations...*
1c850 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
1c860 20 74 6f 20 70 61 73 73 20 61 20 72 65 73 75 6c   to pass a resul
1c870 74 20 74 61 62 6c 65 20 64 69 72 65 63 74 6c 79  t table directly
1c880 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65   to [sqlite3_fre
1c890 65 28 29 5d 2e 0d 0a 2a 2a 20 41 20 72 65 73 75  e()]...** A resu
1c8a0 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20  lt table should 
1c8b0 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 75  be deallocated u
1c8c0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72  sing [sqlite3_fr
1c8d0 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0d 0a 2a 2a  ee_table()]...**
1c8e0 0d 0a 2a 2a 20 5e 28 41 73 20 61 6e 20 65 78 61  ..** ^(As an exa
1c8f0 6d 70 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75  mple of the resu
1c900 6c 74 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c  lt table format,
1c910 20 73 75 70 70 6f 73 65 20 61 20 71 75 65 72 79   suppose a query
1c920 20 72 65 73 75 6c 74 0d 0a 2a 2a 20 69 73 20 61   result..** is a
1c930 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a  s follows:..**..
1c940 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
1c950 70 72 65 3e 0d 0a 2a 2a 20 20 20 20 20 20 20 20  pre>..**        
1c960 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67  Name        | Ag
1c970 65 0d 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d  e..**        ---
1c980 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c990 2d 2d 2d 2d 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ----..**        
1c9a0 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33  Alice       | 43
1c9b0 0d 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20  ..**        Bob 
1c9c0 20 20 20 20 20 20 20 20 7c 20 32 38 0d 0a 2a 2a          | 28..**
1c9d0 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20          Cindy   
1c9e0 20 20 20 20 7c 20 32 31 0d 0a 2a 2a 20 3c 2f 70      | 21..** </p
1c9f0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
1ca00 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 61  ..**..** There a
1ca10 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d  re two column (M
1ca20 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 65 20 72  ==2) and three r
1ca30 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75  ows (N==3).  Thu
1ca40 73 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75 6c 74  s the..** result
1ca50 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 6e 74   table has 8 ent
1ca60 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 20 74  ries.  Suppose t
1ca70 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
1ca80 69 73 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 69 6e  is stored..** in
1ca90 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 20   an array names 
1caa0 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e 20  azResult.  Then 
1cab0 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 74  azResult holds t
1cac0 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0d 0a 2a 2a  his content:..**
1cad0 0d 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  ..** <blockquote
1cae0 3e 3c 70 72 65 3e 0d 0a 2a 2a 20 20 20 20 20 20  ><pre>..**      
1caf0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30    azResult&#91;0
1cb00 5d 20 3d 20 22 4e 61 6d 65 22 3b 0d 0a 2a 2a 20  ] = "Name";..** 
1cb10 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
1cb20 23 39 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0d  #91;1] = "Age";.
1cb30 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
1cb40 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c  ult&#91;2] = "Al
1cb50 69 63 65 22 3b 0d 0a 2a 2a 20 20 20 20 20 20 20  ice";..**       
1cb60 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33 5d   azResult&#91;3]
1cb70 20 3d 20 22 34 33 22 3b 0d 0a 2a 2a 20 20 20 20   = "43";..**    
1cb80 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
1cb90 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0d 0a 2a 2a  ;4] = "Bob";..**
1cba0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
1cbb0 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0d  &#91;5] = "28";.
1cbc0 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
1cbd0 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69  ult&#91;6] = "Ci
1cbe0 6e 64 79 22 3b 0d 0a 2a 2a 20 20 20 20 20 20 20  ndy";..**       
1cbf0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d   azResult&#91;7]
1cc00 20 3d 20 22 32 31 22 3b 0d 0a 2a 2a 20 3c 2f 70   = "21";..** </p
1cc10 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
1cc20 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  )^..**..** ^The 
1cc30 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
1cc40 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 61  e() function eva
1cc50 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d 6f  luates one or mo
1cc60 72 65 0d 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e  re..** semicolon
1cc70 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73  -separated SQL s
1cc80 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
1cc90 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
1cca0 20 55 54 46 2d 38 0d 0a 2a 2a 20 73 74 72 69 6e   UTF-8..** strin
1ccb0 67 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72  g of its 2nd par
1ccc0 61 6d 65 74 65 72 20 61 6e 64 20 72 65 74 75 72  ameter and retur
1ccd0 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c  ns a result tabl
1cce0 65 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 6f 69  e to the..** poi
1ccf0 6e 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74  nter given in it
1cd00 73 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e  s 3rd parameter.
1cd10 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 74  ..**..** After t
1cd20 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 68  he application h
1cd30 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  as finished with
1cd40 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f 6d   the result from
1cd50 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
1cd60 6c 65 28 29 2c 0d 0a 2a 2a 20 69 74 20 6d 75 73  le(),..** it mus
1cd70 74 20 70 61 73 73 20 74 68 65 20 72 65 73 75 6c  t pass the resul
1cd80 74 20 74 61 62 6c 65 20 70 6f 69 6e 74 65 72 20  t table pointer 
1cd90 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  to sqlite3_free_
1cda0 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65 72  table() in order
1cdb0 20 74 6f 0d 0a 2a 2a 20 72 65 6c 65 61 73 65 20   to..** release 
1cdc0 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  the memory that 
1cdd0 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 42  was malloced.  B
1cde0 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61  ecause of the wa
1cdf0 79 20 74 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74  y the..** [sqlit
1ce00 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70  e3_malloc()] hap
1ce10 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69  pens within sqli
1ce20 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c  te3_get_table(),
1ce30 20 74 68 65 20 63 61 6c 6c 69 6e 67 0d 0a 2a 2a   the calling..**
1ce40 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e   function must n
1ce50 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b  ot try to call [
1ce60 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
1ce70 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0d  directly.  Only.
1ce80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  .** [sqlite3_fre
1ce90 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62  e_table()] is ab
1cea0 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68  le to release th
1ceb0 65 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c  e memory properl
1cec0 79 20 61 6e 64 20 73 61 66 65 6c 79 2e 0d 0a 2a  y and safely...*
1ced0 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  *..** The sqlite
1cee0 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 69 6e  3_get_table() in
1cef0 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65  terface is imple
1cf00 6d 65 6e 74 65 64 20 61 73 20 61 20 77 72 61 70  mented as a wrap
1cf10 70 65 72 20 61 72 6f 75 6e 64 0d 0a 2a 2a 20 5b  per around..** [
1cf20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2e  sqlite3_exec()].
1cf30 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65    The sqlite3_ge
1cf40 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 69 6e  t_table() routin
1cf50 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  e does not have 
1cf60 61 63 63 65 73 73 0d 0a 2a 2a 20 74 6f 20 61 6e  access..** to an
1cf70 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20  y internal data 
1cf80 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 53 51  structures of SQ
1cf90 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 20 6f  Lite.  It uses o
1cfa0 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 0d 0a  nly the public..
1cfb0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 66  ** interface def
1cfc0 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 61  ined here.  As a
1cfd0 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 72   consequence, er
1cfe0 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20  rors that occur 
1cff0 69 6e 20 74 68 65 0d 0a 2a 2a 20 77 72 61 70 70  in the..** wrapp
1d000 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65  er layer outside
1d010 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   of the internal
1d020 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
1d030 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0d 0a  ] call are not..
1d040 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20  ** reflected in 
1d050 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1d060 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
1d070 63 6f 64 65 28 29 5d 20 6f 72 0d 0a 2a 2a 20 5b  code()] or..** [
1d080 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
1d090 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  ]...*/..SQLITE_A
1d0a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
1d0b0 65 74 5f 74 61 62 6c 65 28 0d 0a 20 20 73 71 6c  et_table(..  sql
1d0c0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
1d0d0 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61     /* An open da
1d0e0 74 61 62 61 73 65 20 2a 2f 0d 0a 20 20 63 6f 6e  tabase */..  con
1d0f0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
1d100 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20     /* SQL to be 
1d110 65 76 61 6c 75 61 74 65 64 20 2a 2f 0d 0a 20 20  evaluated */..  
1d120 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c  char ***pazResul
1d130 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73  t,    /* Results
1d140 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f   of the query */
1d150 0d 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20  ..  int *pnRow, 
1d160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1d170 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f  ber of result ro
1d180 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  ws written here 
1d190 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c  */..  int *pnCol
1d1a0 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  umn,        /* N
1d1b0 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
1d1c0 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20  columns written 
1d1d0 68 65 72 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20  here */..  char 
1d1e0 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20  **pzErrmsg      
1d1f0 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
1d200 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0d 0a 29  itten here */..)
1d210 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  ;..SQLITE_API vo
1d220 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  id sqlite3_free_
1d230 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73  table(char **res
1d240 75 6c 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ult);..../*..** 
1d250 43 41 50 49 33 52 45 46 3a 20 46 6f 72 6d 61 74  CAPI3REF: Format
1d260 74 65 64 20 53 74 72 69 6e 67 20 50 72 69 6e 74  ted String Print
1d270 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 0d 0a 2a  ing Functions..*
1d280 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  *..** These rout
1d290 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 2d 61 6c  ines are work-al
1d2a0 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70 72 69  ikes of the "pri
1d2b0 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66  ntf()" family of
1d2c0 20 66 75 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 66   functions..** f
1d2d0 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
1d2e0 20 43 20 6c 69 62 72 61 72 79 2e 0d 0a 2a 2a 0d   C library...**.
1d2f0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
1d300 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73  _mprintf() and s
1d310 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
1d320 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65  ) routines write
1d330 20 74 68 65 69 72 0d 0a 2a 2a 20 72 65 73 75 6c   their..** resul
1d340 74 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  ts into memory o
1d350 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
1d360 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
1d370 0d 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73  ..** The strings
1d380 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
1d390 73 65 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20  se two routines 
1d3a0 73 68 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20 72 65  should be..** re
1d3b0 6c 65 61 73 65 64 20 62 79 20 5b 73 71 6c 69 74  leased by [sqlit
1d3c0 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 5e 42 6f  e3_free()].  ^Bo
1d3d0 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  th routines retu
1d3e0 72 6e 20 61 0d 0a 2a 2a 20 4e 55 4c 4c 20 70 6f  rn a..** NULL po
1d3f0 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 65  inter if [sqlite
1d400 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 75  3_malloc()] is u
1d410 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
1d420 65 20 65 6e 6f 75 67 68 0d 0a 2a 2a 20 6d 65 6d  e enough..** mem
1d430 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
1d440 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
1d450 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65 20  ...**..** ^(The 
1d460 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1d470 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69  () routine is si
1d480 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e  milar to "snprin
1d490 74 66 28 29 22 20 66 72 6f 6d 0d 0a 2a 2a 20 74  tf()" from..** t
1d4a0 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
1d4b0 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 73 75  brary.  The resu
1d4c0 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  lt is written in
1d4d0 74 6f 20 74 68 65 0d 0a 2a 2a 20 62 75 66 66 65  to the..** buffe
1d4e0 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68  r supplied as th
1d4f0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
1d500 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 73  er whose size is
1d510 20 67 69 76 65 6e 20 62 79 0d 0a 2a 2a 20 74 68   given by..** th
1d520 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
1d530 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  r. Note that the
1d540 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0d 0a 2a   order of the..*
1d550 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 61  * first two para
1d560 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73  meters is revers
1d570 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66  ed from snprintf
1d580 28 29 2e 29 5e 20 20 54 68 69 73 20 69 73 20 61  ().)^  This is a
1d590 6e 0d 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c  n..** historical
1d5a0 20 61 63 63 69 64 65 6e 74 20 74 68 61 74 20 63   accident that c
1d5b0 61 6e 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77  annot be fixed w
1d5c0 69 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0d  ithout breaking.
1d5d0 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f  .** backwards co
1d5e0 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 20 5e 28  mpatibility.  ^(
1d5f0 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 73  Note also that s
1d600 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1d610 29 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  )..** returns a 
1d620 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62  pointer to its b
1d630 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66  uffer instead of
1d640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a   the number of..
1d650 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63  ** characters ac
1d660 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69  tually written i
1d670 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 29  nto the buffer.)
1d680 5e 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 74  ^  We admit that
1d690 0d 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  ..** the number 
1d6a0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 77 72  of characters wr
1d6b0 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61  itten would be a
1d6c0 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74   more useful ret
1d6d0 75 72 6e 0d 0a 2a 2a 20 76 61 6c 75 65 20 62 75  urn..** value bu
1d6e0 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e  t we cannot chan
1d6f0 67 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ge the implement
1d700 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
1d710 5f 73 6e 70 72 69 6e 74 66 28 29 0d 0a 2a 2a 20  _snprintf()..** 
1d720 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 65 61  now without brea
1d730 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69  king compatibili
1d740 74 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 41 73 20  ty...**..** ^As 
1d750 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 66  long as the buff
1d760 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 74  er size is great
1d770 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71  er than zero, sq
1d780 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
1d790 0d 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20  ..** guarantees 
1d7a0 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20  that the buffer 
1d7b0 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74  is always zero-t
1d7c0 65 72 6d 69 6e 61 74 65 64 2e 20 20 5e 54 68 65  erminated.  ^The
1d7d0 20 66 69 72 73 74 0d 0a 2a 2a 20 70 61 72 61 6d   first..** param
1d7e0 65 74 65 72 20 22 6e 22 20 69 73 20 74 68 65 20  eter "n" is the 
1d7f0 74 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68  total size of th
1d800 65 20 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64  e buffer, includ
1d810 69 6e 67 20 73 70 61 63 65 20 66 6f 72 0d 0a 2a  ing space for..*
1d820 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69  * the zero termi
1d830 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c  nator.  So the l
1d840 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 20 74 68  ongest string th
1d850 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65  at can be comple
1d860 74 65 6c 79 0d 0a 2a 2a 20 77 72 69 74 74 65 6e  tely..** written
1d870 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 68 61   will be n-1 cha
1d880 72 61 63 74 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  racters...**..**
1d890 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 76 73   ^The sqlite3_vs
1d8a0 6e 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e  nprintf() routin
1d8b0 65 20 69 73 20 61 20 76 61 72 61 72 67 73 20 76  e is a varargs v
1d8c0 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
1d8d0 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 0d 0a 2a  3_snprintf()...*
1d8e0 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  *..** These rout
1d8f0 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65  ines all impleme
1d900 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e  nt some addition
1d910 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0d 0a 2a  al formatting..*
1d920 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61  * options that a
1d930 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f  re useful for co
1d940 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73  nstructing SQL s
1d950 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 20 41  tatements...** A
1d960 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20  ll of the usual 
1d970 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 74 74  printf() formatt
1d980 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c  ing options appl
1d990 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c  y.  In addition,
1d9a0 20 74 68 65 72 65 0d 0a 2a 2a 20 69 73 20 61 72   there..** is ar
1d9b0 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 6e  e "%q", "%Q", an
1d9c0 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0d  d "%z" options..
1d9d0 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65 20 25 71  .**..** ^(The %q
1d9e0 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69   option works li
1d9f0 6b 65 20 25 73 20 69 6e 20 74 68 61 74 20 69 74  ke %s in that it
1da00 20 73 75 62 73 74 69 74 75 74 65 73 20 61 20 6e   substitutes a n
1da10 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0d 0a 2a  ul-terminated..*
1da20 2a 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68  * string from th
1da30 65 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e  e argument list.
1da40 20 20 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f    But %q also do
1da50 75 62 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27  ubles every '\''
1da60 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 20   character...** 
1da70 25 71 20 69 73 20 64 65 73 69 67 6e 65 64 20 66  %q is designed f
1da80 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 20  or use inside a 
1da90 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 29  string literal.)
1daa0 5e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 65  ^  By doubling e
1dab0 61 63 68 20 27 5c 27 27 0d 0a 2a 2a 20 63 68 61  ach '\''..** cha
1dac0 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65  racter it escape
1dad0 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72  s that character
1dae0 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74   and allows it t
1daf0 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
1db00 74 6f 0d 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e  to..** the strin
1db10 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 65  g...**..** For e
1db20 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74  xample, assume t
1db30 68 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62  he string variab
1db40 6c 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e  le zText contain
1db50 73 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77  s text as follow
1db60 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 62 6c 6f 63  s:..**..** <bloc
1db70 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0d 0a 2a 2a  kquote><pre>..**
1db80 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20    char *zText = 
1db90 22 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61  "It's a happy da
1dba0 79 21 22 3b 0d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  y!";..** </pre><
1dbb0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0d 0a 2a 2a  /blockquote>..**
1dbc0 0d 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65  ..** One can use
1dbd0 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e   this text in an
1dbe0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61   SQL statement a
1dbf0 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a  s follows:..**..
1dc00 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
1dc10 70 72 65 3e 0d 0a 2a 2a 20 20 63 68 61 72 20 2a  pre>..**  char *
1dc20 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSQL = sqlite3_m
1dc30 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49  printf("INSERT I
1dc40 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53  NTO table VALUES
1dc50 28 27 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b  ('%q')", zText);
1dc60 0d 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78  ..**  sqlite3_ex
1dc70 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20  ec(db, zSQL, 0, 
1dc80 30 2c 20 30 29 3b 0d 0a 2a 2a 20 20 73 71 6c 69  0, 0);..**  sqli
1dc90 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0d  te3_free(zSQL);.
1dca0 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
1dcb0 6b 71 75 6f 74 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20  kquote>..**..** 
1dcc0 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66  Because the %q f
1dcd0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20  ormat string is 
1dce0 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63  used, the '\'' c
1dcf0 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78  haracter in zTex
1dd00 74 0d 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64  t..** is escaped
1dd10 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e   and the SQL gen
1dd20 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c  erated is as fol
1dd30 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 62  lows:..**..** <b
1dd40 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0d  lockquote><pre>.
1dd50 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .**  INSERT INTO
1dd60 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27   table1 VALUES('
1dd70 49 74 27 27 73 20 61 20 68 61 70 70 79 20 64 61  It''s a happy da
1dd80 79 21 27 29 0d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  y!')..** </pre><
1dd90 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0d 0a 2a 2a  /blockquote>..**
1dda0 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 72  ..** This is cor
1ddb0 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 73  rect.  Had we us
1ddc0 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f 66  ed %s instead of
1ddd0 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 74   %q, the generat
1dde0 65 64 20 53 51 4c 0d 0a 2a 2a 20 77 6f 75 6c 64  ed SQL..** would
1ddf0 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b   have looked lik
1de00 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  e this:..**..** 
1de10 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
1de20 3e 0d 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e  >..**  INSERT IN
1de30 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53  TO table1 VALUES
1de40 28 27 49 74 27 73 20 61 20 68 61 70 70 79 20 64  ('It's a happy d
1de50 61 79 21 27 29 3b 0d 0a 2a 2a 20 3c 2f 70 72 65  ay!');..** </pre
1de60 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0d 0a  ></blockquote>..
1de70 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 73 65 63 6f  **..** This seco
1de80 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 6e  nd example is an
1de90 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 6f   SQL syntax erro
1dea0 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 6c  r.  As a general
1deb0 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c 64   rule you should
1dec0 0d 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20  ..** always use 
1ded0 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73  %q instead of %s
1dee0 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20   when inserting 
1def0 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69  text into a stri
1df00 6e 67 20 6c 69 74 65 72 61 6c 2e 0d 0a 2a 2a 0d  ng literal...**.
1df10 0a 2a 2a 20 5e 28 54 68 65 20 25 51 20 6f 70 74  .** ^(The %Q opt
1df20 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
1df30 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f  q except it also
1df40 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f   adds single quo
1df50 74 65 73 20 61 72 6f 75 6e 64 0d 0a 2a 2a 20 74  tes around..** t
1df60 68 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  he outside of th
1df70 65 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20  e total string. 
1df80 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69   Additionally, i
1df90 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
1dfa0 69 6e 20 74 68 65 0d 0a 2a 2a 20 61 72 67 75 6d  in the..** argum
1dfb0 65 6e 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55  ent list is a NU
1dfc0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73  LL pointer, %Q s
1dfd0 75 62 73 74 69 74 75 74 65 73 20 74 68 65 20 74  ubstitutes the t
1dfe0 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68  ext "NULL" (with
1dff0 6f 75 74 0d 0a 2a 2a 20 73 69 6e 67 6c 65 20 71  out..** single q
1e000 75 6f 74 65 73 29 2e 29 5e 20 20 53 6f 2c 20 66  uotes).)^  So, f
1e010 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 20  or example, one 
1e020 63 6f 75 6c 64 20 73 61 79 3a 0d 0a 2a 2a 0d 0a  could say:..**..
1e030 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
1e040 70 72 65 3e 0d 0a 2a 2a 20 20 63 68 61 72 20 2a  pre>..**  char *
1e050 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSQL = sqlite3_m
1e060 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49  printf("INSERT I
1e070 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53  NTO table VALUES
1e080 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b 0d 0a  (%Q)", zText);..
1e090 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  **  sqlite3_exec
1e0a0 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c  (db, zSQL, 0, 0,
1e0b0 20 30 29 3b 0d 0a 2a 2a 20 20 73 71 6c 69 74 65   0);..**  sqlite
1e0c0 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0d 0a 2a  3_free(zSQL);..*
1e0d0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
1e0e0 75 6f 74 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  uote>..**..** Th
1e0f0 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69 6c  e code above wil
1e100 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72 65  l render a corre
1e110 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ct SQL statement
1e120 20 69 6e 20 74 68 65 20 7a 53 51 4c 0d 0a 2a 2a   in the zSQL..**
1e130 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69   variable even i
1e140 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69  f the zText vari
1e150 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70  able is a NULL p
1e160 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ointer...**..** 
1e170 5e 28 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61  ^(The "%z" forma
1e180 74 74 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72  tting option wor
1e190 6b 73 20 6c 69 6b 65 20 22 25 73 22 20 62 75 74  ks like "%s" but
1e1a0 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20 61 64   with the..** ad
1e1b0 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65  dition that afte
1e1c0 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 73  r the string has
1e1d0 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63   been read and c
1e1e0 6f 70 69 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 74  opied into..** t
1e1f0 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69  he result, [sqli
1e200 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63  te3_free()] is c
1e210 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70  alled on the inp
1e220 75 74 20 73 74 72 69 6e 67 2e 29 5e 0d 0a 2a 2f  ut string.)^..*/
1e230 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  ..SQLITE_API cha
1e240 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  r *sqlite3_mprin
1e250 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  tf(const char*,.
1e260 2e 2e 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..);..SQLITE_API
1e270 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
1e280 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
1e290 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0d 0a  ar*, va_list);..
1e2a0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
1e2b0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  *sqlite3_snprint
1e2c0 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73  f(int,char*,cons
1e2d0 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a  t char*, ...);..
1e2e0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
1e2f0 2a 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e  *sqlite3_vsnprin
1e300 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e  tf(int,char*,con
1e310 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73  st char*, va_lis
1e320 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  t);..../*..** CA
1e330 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
1e340 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73  llocation Subsys
1e350 74 65 6d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  tem..**..** The 
1e360 53 51 4c 69 74 65 20 63 6f 72 65 20 75 73 65 73  SQLite core uses
1e370 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f 75   these three rou
1e380 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66  tines for all of
1e390 20 69 74 73 20 6f 77 6e 0d 0a 2a 2a 20 69 6e 74   its own..** int
1e3a0 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ernal memory all
1e3b0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22  ocation needs. "
1e3c0 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65  Core" in the pre
1e3d0 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0d 0a  vious sentence..
1e3e0 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  ** does not incl
1e3f0 75 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  ude operating-sy
1e400 73 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 46  stem specific VF
1e410 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
1e420 2e 20 20 54 68 65 0d 0a 2a 2a 20 57 69 6e 64 6f  .  The..** Windo
1e430 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 74 69  ws VFS uses nati
1e440 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  ve malloc() and 
1e450 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20  free() for some 
1e460 6f 70 65 72 61 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d  operations...**.
1e470 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
1e480 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  _malloc() routin
1e490 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
1e4a0 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 0d 0a  ter to a block..
1e4b0 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 20  ** of memory at 
1e4c0 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e  least N bytes in
1e4d0 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 20 4e   length, where N
1e4e0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65   is the paramete
1e4f0 72 2e 0d 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74  r...** ^If sqlit
1e500 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75  e3_malloc() is u
1e510 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20  nable to obtain 
1e520 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0d  sufficient free.
1e530 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72  .** memory, it r
1e540 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f  eturns a NULL po
1e550 69 6e 74 65 72 2e 20 20 5e 49 66 20 74 68 65 20  inter.  ^If the 
1e560 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0d 0a  parameter N to..
1e570 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
1e580 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e  c() is zero or n
1e590 65 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c  egative then sql
1e5a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65  ite3_malloc() re
1e5b0 74 75 72 6e 73 0d 0a 2a 2a 20 61 20 4e 55 4c 4c  turns..** a NULL
1e5c0 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a   pointer...**..*
1e5d0 2a 20 5e 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74  * ^Calling sqlit
1e5e0 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 61  e3_free() with a
1e5f0 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f 75   pointer previou
1e600 73 6c 79 20 72 65 74 75 72 6e 65 64 0d 0a 2a 2a  sly returned..**
1e610 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
1e620 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  oc() or sqlite3_
1e630 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73  realloc() releas
1e640 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73  es that memory s
1e650 6f 0d 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69  o..** that it mi
1e660 67 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 20  ght be reused.  
1e670 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65  ^The sqlite3_fre
1e680 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 0d 0a  e() routine is..
1e690 2a 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73  ** a no-op if is
1e6a0 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e   called with a N
1e6b0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50 61  ULL pointer.  Pa
1e6c0 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69  ssing a NULL poi
1e6d0 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 73 71 6c 69  nter..** to sqli
1e6e0 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 68 61  te3_free() is ha
1e6f0 72 6d 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62  rmless.  After b
1e700 65 69 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f  eing freed, memo
1e710 72 79 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65  ry..** should ne
1e720 69 74 68 65 72 20 62 65 20 72 65 61 64 20 6e 6f  ither be read no
1e730 72 20 77 72 69 74 74 65 6e 2e 20 20 45 76 65 6e  r written.  Even
1e740 20 72 65 61 64 69 6e 67 20 70 72 65 76 69 6f 75   reading previou
1e750 73 6c 79 20 66 72 65 65 64 0d 0a 2a 2a 20 6d 65  sly freed..** me
1e760 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 73 75 6c  mory might resul
1e770 74 20 69 6e 20 61 20 73 65 67 6d 65 6e 74 61 74  t in a segmentat
1e780 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 6f 74 68  ion fault or oth
1e790 65 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 2e  er severe error.
1e7a0 0d 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 72  ..** Memory corr
1e7b0 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d 65 6e  uption, a segmen
1e7c0 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 6f 72  tation fault, or
1e7d0 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
1e7e0 72 6f 72 0d 0a 2a 2a 20 6d 69 67 68 74 20 72 65  ror..** might re
1e7f0 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 5f  sult if sqlite3_
1e800 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 64  free() is called
1e810 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c   with a non-NULL
1e820 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0d 0a 2a   pointer that..*
1e830 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e  * was not obtain
1e840 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
1e850 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69  malloc() or sqli
1e860 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0d 0a  te3_realloc()...
1e870 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65 20 73 71 6c  **..** ^(The sql
1e880 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69  ite3_realloc() i
1e890 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74  nterface attempt
1e8a0 73 20 74 6f 20 72 65 73 69 7a 65 20 61 0d 0a 2a  s to resize a..*
1e8b0 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61  * prior memory a
1e8c0 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20  llocation to be 
1e8d0 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73  at least N bytes
1e8e0 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
1e8f0 0d 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  ..** second para
1e900 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f  meter.  The memo
1e910 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
1e920 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20 74   be resized is t
1e930 68 65 20 66 69 72 73 74 0d 0a 2a 2a 20 70 61 72  he first..** par
1e940 61 6d 65 74 65 72 2e 29 5e 20 5e 20 49 66 20 74  ameter.)^ ^ If t
1e950 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
1e960 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
1e970 61 6c 6c 6f 63 28 29 0d 0a 2a 2a 20 69 73 20 61  alloc()..** is a
1e980 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68   NULL pointer th
1e990 65 6e 20 69 74 73 20 62 65 68 61 76 69 6f 72 20  en its behavior 
1e9a0 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  is identical to 
1e9b0 63 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 73 71 6c 69  calling..** sqli
1e9c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68  te3_malloc(N) wh
1e9d0 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63  ere N is the sec
1e9e0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ond parameter to
1e9f0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
1ea00 28 29 2e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20  ()...** ^If the 
1ea10 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
1ea20 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c   to sqlite3_real
1ea30 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72  loc() is zero or
1ea40 0d 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 68  ..** negative th
1ea50 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
1ea60 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  is exactly the s
1ea70 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0d 0a  ame as calling..
1ea80 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ** sqlite3_free(
1ea90 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 68  P) where P is th
1eaa0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
1eab0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
1eac0 6c 6c 6f 63 28 29 2e 0d 0a 2a 2a 20 5e 73 71 6c  lloc()...** ^sql
1ead0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72  ite3_realloc() r
1eae0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1eaf0 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   to a memory all
1eb00 6f 63 61 74 69 6f 6e 0d 0a 2a 2a 20 6f 66 20 61  ocation..** of a
1eb10 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20  t least N bytes 
1eb20 69 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20  in size or NULL 
1eb30 69 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65  if sufficient me
1eb40 6d 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61  mory is unavaila
1eb50 62 6c 65 2e 0d 0a 2a 2a 20 5e 49 66 20 4d 20 69  ble...** ^If M i
1eb60 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
1eb70 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  e prior allocati
1eb80 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d  on, then min(N,M
1eb90 29 20 62 79 74 65 73 0d 0a 2a 2a 20 6f 66 20 74  ) bytes..** of t
1eba0 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74  he prior allocat
1ebb0 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20 69  ion are copied i
1ebc0 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  nto the beginnin
1ebd0 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74 75  g of buffer retu
1ebe0 72 6e 65 64 0d 0a 2a 2a 20 62 79 20 73 71 6c 69  rned..** by sqli
1ebf0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e  te3_realloc() an
1ec00 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  d the prior allo
1ec10 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
1ec20 0d 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74 65 33  ..** ^If sqlite3
1ec30 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  _realloc() retur
1ec40 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  ns NULL, then th
1ec50 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  e prior allocati
1ec60 6f 6e 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72  on..** is not fr
1ec70 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  eed...**..** ^Th
1ec80 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65  e memory returne
1ec90 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c  d by sqlite3_mal
1eca0 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c 69 74 65  loc() and sqlite
1ecb0 33 5f 72 65 61 6c 6c 6f 63 28 29 0d 0a 2a 2a 20  3_realloc()..** 
1ecc0 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 6e 65  is always aligne
1ecd0 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 61 6e  d to at least an
1ece0 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8 byte boundary
1ecf0 2c 20 6f 72 20 74 6f 20 61 0d 0a 2a 2a 20 34 20  , or to a..** 4 
1ed00 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 69 66  byte boundary if
1ed10 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 34 5f 42   the [SQLITE_4_B
1ed20 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d 41 4c 4c  YTE_ALIGNED_MALL
1ed30 4f 43 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  OC] compile-time
1ed40 0d 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 75  ..** option is u
1ed50 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20  sed...**..** In 
1ed60 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
1ed70 2e 35 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20  .5.0 and 3.5.1, 
1ed80 69 74 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20  it was possible 
1ed90 74 6f 20 64 65 66 69 6e 65 0d 0a 2a 2a 20 74 68  to define..** th
1eda0 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  e SQLITE_OMIT_ME
1edb0 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20  MORY_ALLOCATION 
1edc0 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73  which would caus
1edd0 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 0d 0a  e the built-in..
1ede0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
1edf0 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  n of these routi
1ee00 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74 74 65  nes to be omitte
1ee10 64 2e 20 20 54 68 61 74 20 63 61 70 61 62 69 6c  d.  That capabil
1ee20 69 74 79 0d 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f  ity..** is no lo
1ee30 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20 20  nger provided.  
1ee40 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d 65  Only built-in me
1ee50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 20  mory allocators 
1ee60 63 61 6e 20 62 65 20 75 73 65 64 2e 0d 0a 2a 2a  can be used...**
1ee70 0d 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73  ..** The Windows
1ee80 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61   OS interface la
1ee90 79 65 72 20 63 61 6c 6c 73 0d 0a 2a 2a 20 74 68  yer calls..** th
1eea0 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28  e system malloc(
1eeb0 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72  ) and free() dir
1eec0 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65  ectly when conve
1eed0 72 74 69 6e 67 0d 0a 2a 2a 20 66 69 6c 65 6e 61  rting..** filena
1eee0 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20  mes between the 
1eef0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75  UTF-8 encoding u
1ef00 73 65 64 20 62 79 20 53 51 4c 69 74 65 0d 0a 2a  sed by SQLite..*
1ef10 2a 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66  * and whatever f
1ef20 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67  ilename encoding
1ef30 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
1ef40 70 61 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f  particular Windo
1ef50 77 73 0d 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74  ws..** installat
1ef60 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c  ion.  Memory all
1ef70 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61  ocation errors a
1ef80 72 65 20 64 65 74 65 63 74 65 64 2c 20 62 75 74  re detected, but
1ef90 0d 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 72 65  ..** they are re
1efa0 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 20 5b  ported back as [
1efb0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d  SQLITE_CANTOPEN]
1efc0 20 6f 72 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   or..** [SQLITE_
1efd0 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 68  IOERR] rather th
1efe0 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  an [SQLITE_NOMEM
1eff0 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70  ]...**..** The p
1f000 6f 69 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73  ointer arguments
1f010 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65   to [sqlite3_fre
1f020 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
1f030 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0d 0a 2a 2a  3_realloc()]..**
1f040 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
1f050 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69  NULL or else poi
1f060 6e 74 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66  nters obtained f
1f070 72 6f 6d 20 61 20 70 72 69 6f 72 0d 0a 2a 2a 20  rom a prior..** 
1f080 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73  invocation of [s
1f090 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
1f0a0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   or [sqlite3_rea
1f0b0 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 68 61 76  lloc()] that hav
1f0c0 65 0d 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65  e..** not yet be
1f0d0 65 6e 20 72 65 6c 65 61 73 65 64 2e 0d 0a 2a 2a  en released...**
1f0e0 0d 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  ..** The applica
1f0f0 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65  tion must not re
1f100 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20  ad or write any 
1f110 70 61 72 74 20 6f 66 0d 0a 2a 2a 20 61 20 62 6c  part of..** a bl
1f120 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66  ock of memory af
1f130 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ter it has been 
1f140 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0d 0a  released using..
1f150 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ** [sqlite3_free
1f160 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
1f170 72 65 61 6c 6c 6f 63 28 29 5d 2e 0d 0a 2a 2f 0d  realloc()]...*/.
1f180 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1f190 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   *sqlite3_malloc
1f1a0 28 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  (int);..SQLITE_A
1f1b0 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
1f1c0 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20  _realloc(void*, 
1f1d0 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50  int);..SQLITE_AP
1f1e0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66  I void sqlite3_f
1f1f0 72 65 65 28 76 6f 69 64 2a 29 3b 0d 0a 0d 0a 2f  ree(void*);..../
1f200 2a 0d 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 6f 72  Memory Allocator
1f220 20 53 74 61 74 69 73 74 69 63 73 0d 0a 2a 2a 0d   Statistics..**.
1f230 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69  .** SQLite provi
1f240 64 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e  des these two in
1f250 74 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70  terfaces for rep
1f260 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74  orting on the st
1f270 61 74 75 73 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  atus..** of the 
1f280 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
1f290 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  )], [sqlite3_fre
1f2a0 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74  e()], and [sqlit
1f2b0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0d 0a 2a  e3_realloc()]..*
1f2c0 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63  * routines, whic
1f2d0 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74  h form the built
1f2e0 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  -in memory alloc
1f2f0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
1f300 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b 73  ..**..** ^The [s
1f310 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
1f320 65 64 28 29 5d 20 72 6f 75 74 69 6e 65 20 72 65  ed()] routine re
1f330 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
1f340 20 6f 66 20 62 79 74 65 73 0d 0a 2a 2a 20 6f 66   of bytes..** of
1f350 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c   memory currentl
1f360 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 28 6d  y outstanding (m
1f370 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f 74 20  alloced but not 
1f380 66 72 65 65 64 29 2e 0d 0a 2a 2a 20 5e 54 68 65  freed)...** ^The
1f390 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
1f3a0 5f 68 69 67 68 77 61 74 65 72 28 29 5d 20 72 6f  _highwater()] ro
1f3b0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
1f3c0 65 20 6d 61 78 69 6d 75 6d 0d 0a 2a 2a 20 76 61  e maximum..** va
1f3d0 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  lue of [sqlite3_
1f3e0 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 73  memory_used()] s
1f3f0 69 6e 63 65 20 74 68 65 20 68 69 67 68 2d 77 61  ince the high-wa
1f400 74 65 72 20 6d 61 72 6b 0d 0a 2a 2a 20 77 61 73  ter mark..** was
1f410 20 6c 61 73 74 20 72 65 73 65 74 2e 20 20 5e 54   last reset.  ^T
1f420 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
1f430 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d  ed by [sqlite3_m
1f440 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 61 6e  emory_used()] an
1f450 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  d..** [sqlite3_m
1f460 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28  emory_highwater(
1f470 29 5d 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 6f  )] include any o
1f480 76 65 72 68 65 61 64 0d 0a 2a 2a 20 61 64 64 65  verhead..** adde
1f490 64 20 62 79 20 53 51 4c 69 74 65 20 69 6e 20 69  d by SQLite in i
1f4a0 74 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ts implementatio
1f4b0 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61  n of [sqlite3_ma
1f4c0 6c 6c 6f 63 28 29 5d 2c 0d 0a 2a 2a 20 62 75 74  lloc()],..** but
1f4d0 20 6e 6f 74 20 6f 76 65 72 68 65 61 64 20 61 64   not overhead ad
1f4e0 64 65 64 20 62 79 20 74 68 65 20 61 6e 79 20 75  ded by the any u
1f4f0 6e 64 65 72 6c 79 69 6e 67 20 73 79 73 74 65 6d  nderlying system
1f500 20 6c 69 62 72 61 72 79 0d 0a 2a 2a 20 72 6f 75   library..** rou
1f510 74 69 6e 65 73 20 74 68 61 74 20 5b 73 71 6c 69  tines that [sqli
1f520 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6d 61  te3_malloc()] ma
1f530 79 20 63 61 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  y call...**..** 
1f540 5e 54 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68  ^The memory high
1f550 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 73 20 72  -water mark is r
1f560 65 73 65 74 20 74 6f 20 74 68 65 20 63 75 72 72  eset to the curr
1f570 65 6e 74 20 76 61 6c 75 65 20 6f 66 0d 0a 2a 2a  ent value of..**
1f580 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
1f590 5f 75 73 65 64 28 29 5d 20 69 66 20 61 6e 64 20  _used()] if and 
1f5a0 6f 6e 6c 79 20 69 66 20 74 68 65 20 70 61 72 61  only if the para
1f5b0 6d 65 74 65 72 20 74 6f 0d 0a 2a 2a 20 5b 73 71  meter to..** [sq
1f5c0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
1f5d0 68 77 61 74 65 72 28 29 5d 20 69 73 20 74 72 75  hwater()] is tru
1f5e0 65 2e 20 20 5e 54 68 65 20 76 61 6c 75 65 20 72  e.  ^The value r
1f5f0 65 74 75 72 6e 65 64 0d 0a 2a 2a 20 62 79 20 5b  eturned..** by [
1f600 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68  sqlite3_memory_h
1f610 69 67 68 77 61 74 65 72 28 31 29 5d 20 69 73 20  ighwater(1)] is 
1f620 74 68 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d  the high-water m
1f630 61 72 6b 0d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f  ark..** prior to
1f640 20 74 68 65 20 72 65 73 65 74 2e 0d 0a 2a 2f 0d   the reset...*/.
1f650 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1f660 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
1f670 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f  3_memory_used(vo
1f680 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  id);..SQLITE_API
1f690 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
1f6a0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
1f6b0 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
1f6c0 74 46 6c 61 67 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  tFlag);..../*..*
1f6d0 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75  * CAPI3REF: Pseu
1f6e0 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72  do-Random Number
1f6f0 20 47 65 6e 65 72 61 74 6f 72 0d 0a 2a 2a 0d 0a   Generator..**..
1f700 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69  ** SQLite contai
1f710 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74  ns a high-qualit
1f720 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  y pseudo-random 
1f730 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
1f740 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0d   (PRNG) used to.
1f750 0a 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f  .** select rando
1f760 6d 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44  m [ROWID | ROWID
1f770 73 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  s] when insertin
1f780 67 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e  g new records in
1f790 74 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0d  to a table that.
1f7a0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73  .** already uses
1f7b0 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73   the largest pos
1f7c0 73 69 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20  sible [ROWID].  
1f7d0 54 68 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f  The PRNG is also
1f7e0 20 75 73 65 64 20 66 6f 72 0d 0a 2a 2a 20 74 68   used for..** th
1f7f0 65 20 62 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f  e build-in rando
1f800 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c  m() and randombl
1f810 6f 62 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  ob() SQL functio
1f820 6e 73 2e 20 20 54 68 69 73 20 69 6e 74 65 72 66  ns.  This interf
1f830 61 63 65 20 61 6c 6c 6f 77 73 0d 0a 2a 2a 20 61  ace allows..** a
1f840 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20 61  pplications to a
1f850 63 63 65 73 73 20 74 68 65 20 73 61 6d 65 20 50  ccess the same P
1f860 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20 70 75  RNG for other pu
1f870 72 70 6f 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  rposes...**..** 
1f880 5e 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20  ^A call to this 
1f890 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e  routine stores N
1f8a0 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
1f8b0 6e 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72  ness into buffer
1f8c0 20 50 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65   P...**..** ^The
1f8d0 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
1f8e0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f   routine is invo
1f8f0 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e 74 65  ked (either inte
1f900 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0d 0a 2a 2a  rnally or by..**
1f910 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
1f920 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 65  ) the PRNG is se
1f930 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 6f  eded using rando
1f940 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0d 0a  mness obtained..
1f950 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e  ** from the xRan
1f960 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f  domness method o
1f970 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  f the default [s
1f980 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1f990 63 74 2e 0d 0a 2a 2a 20 5e 4f 6e 20 61 6c 6c 20  ct...** ^On all 
1f9a0 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63  subsequent invoc
1f9b0 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75  ations, the pseu
1f9c0 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73  do-randomness is
1f9d0 20 67 65 6e 65 72 61 74 65 64 0d 0a 2a 2a 20 69   generated..** i
1f9e0 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69  nternally and wi
1f9f0 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74  thout recourse t
1fa00 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  o the [sqlite3_v
1fa10 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0d  fs] xRandomness.
1fa20 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d  .** method...*/.
1fa30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1fa40 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
1fa50 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
1fa60 2a 50 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  *P);..../*..** C
1fa70 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65  API3REF: Compile
1fa80 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 7a 61 74  -Time Authorizat
1fa90 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 0d 0a 2a  ion Callbacks..*
1faa0 2a 0d 0a 2a 2a 20 5e 54 68 69 73 20 72 6f 75 74  *..** ^This rout
1fab0 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 6e  ine registers an
1fac0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1fad0 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 74  back with a part
1fae0 69 63 75 6c 61 72 0d 0a 2a 2a 20 5b 64 61 74 61  icular..** [data
1faf0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
1fb00 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68  , supplied in th
1fb10 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1fb20 2e 0d 0a 2a 2a 20 5e 54 68 65 20 61 75 74 68 6f  ...** ^The autho
1fb30 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
1fb40 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c  s invoked as SQL
1fb50 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
1fb60 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0d 0a  being compiled..
1fb70 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70  ** by [sqlite3_p
1fb80 72 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73  repare()] or its
1fb90 20 76 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74   variants [sqlit
1fba0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1fbb0 2c 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  ,..** [sqlite3_p
1fbc0 72 65 70 61 72 65 31 36 28 29 5d 20 61 6e 64 20  repare16()] and 
1fbd0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1fbe0 31 36 5f 76 32 28 29 5d 2e 20 20 5e 41 74 20 76  16_v2()].  ^At v
1fbf0 61 72 69 6f 75 73 0d 0a 2a 2a 20 70 6f 69 6e 74  arious..** point
1fc00 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d  s during the com
1fc10 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  pilation process
1fc20 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 65  , as logic is be
1fc30 69 6e 67 20 63 72 65 61 74 65 64 0d 0a 2a 2a 20  ing created..** 
1fc40 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f  to perform vario
1fc50 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20  us actions, the 
1fc60 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1fc70 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74  ack is invoked t
1fc80 6f 0d 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f  o..** see if tho
1fc90 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61  se actions are a
1fca0 6c 6c 6f 77 65 64 2e 20 20 5e 54 68 65 20 61 75  llowed.  ^The au
1fcb0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1fcc0 6b 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 72 65 74  k should..** ret
1fcd0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  urn [SQLITE_OK] 
1fce0 74 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74  to allow the act
1fcf0 69 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e  ion, [SQLITE_IGN
1fd00 4f 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77  ORE] to disallow
1fd10 20 74 68 65 0d 0a 2a 2a 20 73 70 65 63 69 66 69   the..** specifi
1fd20 63 20 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c  c action but all
1fd30 6f 77 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  ow the SQL state
1fd40 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65  ment to continue
1fd50 20 74 6f 20 62 65 0d 0a 2a 2a 20 63 6f 6d 70 69   to be..** compi
1fd60 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  led, or [SQLITE_
1fd70 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65 20 74  DENY] to cause t
1fd80 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20 73 74  he entire SQL st
1fd90 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0d 0a 2a  atement to be..*
1fda0 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68 20  * rejected with 
1fdb0 61 6e 20 65 72 72 6f 72 2e 20 20 5e 49 66 20 74  an error.  ^If t
1fdc0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
1fdd0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0d 0a  llback returns..
1fde0 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68  ** any value oth
1fdf0 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
1fe00 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45  IGNORE], [SQLITE
1fe10 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45  _OK], or [SQLITE
1fe20 5f 44 45 4e 59 5d 0d 0a 2a 2a 20 74 68 65 6e 20  _DENY]..** then 
1fe30 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  the [sqlite3_pre
1fe40 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
1fe50 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
1fe60 61 74 20 74 72 69 67 67 65 72 65 64 0d 0a 2a 2a  at triggered..**
1fe70 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
1fe80 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61  will fail with a
1fe90 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  n error message.
1fea0 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68  ..**..** When th
1feb0 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
1fec0 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20  ns [SQLITE_OK], 
1fed0 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 6f  that means the o
1fee0 70 65 72 61 74 69 6f 6e 0d 0a 2a 2a 20 72 65 71  peration..** req
1fef0 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 5e  uested is ok.  ^
1ff00 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  When the callbac
1ff10 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
1ff20 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0d 0a 2a 2a  E_DENY], the..**
1ff30 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1ff40 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76  e_v2()] or equiv
1ff50 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20  alent call that 
1ff60 74 72 69 67 67 65 72 65 64 20 74 68 65 0d 0a 2a  triggered the..*
1ff70 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c  * authorizer wil
1ff80 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65  l fail with an e
1ff90 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70  rror message exp
1ffa0 6c 61 69 6e 69 6e 67 20 74 68 61 74 0d 0a 2a 2a  laining that..**
1ffb0 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65   access is denie
1ffc0 64 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  d. ..**..** ^The
1ffd0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
1ffe0 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a   to the authoriz
1fff0 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61  er callback is a
20000 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69   copy of the thi
20010 72 64 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rd..** parameter
20020 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f   to the sqlite3_
20030 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29  set_authorizer()
20040 20 69 6e 74 65 72 66 61 63 65 2e 20 5e 54 68 65   interface. ^The
20050 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
20060 72 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c  r..** to the cal
20070 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65  lback is an inte
20080 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59  ger [SQLITE_COPY
20090 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20   | action code] 
200a0 74 68 61 74 20 73 70 65 63 69 66 69 65 73 0d 0a  that specifies..
200b0 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c 61  ** the particula
200c0 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61  r action to be a
200d0 75 74 68 6f 72 69 7a 65 64 2e 20 5e 54 68 65 20  uthorized. ^The 
200e0 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 69  third through si
200f0 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0d 0a  xth parameters..
20100 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ** to the callba
20110 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d  ck are zero-term
20120 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74  inated strings t
20130 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69  hat contain addi
20140 74 69 6f 6e 61 6c 0d 0a 2a 2a 20 64 65 74 61 69  tional..** detai
20150 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
20160 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
20170 69 7a 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49  ized...**..** ^I
20180 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f 64  f the action cod
20190 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45 41  e is [SQLITE_REA
201a0 44 5d 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63  D]..** and the c
201b0 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
201c0 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20  [SQLITE_IGNORE] 
201d0 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 5b 70 72  then the..** [pr
201e0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
201f0 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63  ] statement is c
20200 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 75  onstructed to su
20210 62 73 74 69 74 75 74 65 0d 0a 2a 2a 20 61 20 4e  bstitute..** a N
20220 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61  ULL value in pla
20230 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ce of the table 
20240 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c  column that woul
20250 64 20 68 61 76 65 0d 0a 2a 2a 20 62 65 65 6e 20  d have..** been 
20260 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f  read if [SQLITE_
20270 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74  OK] had been ret
20280 75 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c  urned.  The [SQL
20290 49 54 45 5f 49 47 4e 4f 52 45 5d 0d 0a 2a 2a 20  ITE_IGNORE]..** 
202a0 72 65 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73  return can be us
202b0 65 64 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e  ed to deny an un
202c0 74 72 75 73 74 65 64 20 75 73 65 72 20 61 63 63  trusted user acc
202d0 65 73 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61  ess to individua
202e0 6c 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66  l..** columns of
202f0 20 61 20 74 61 62 6c 65 2e 0d 0a 2a 2a 20 5e 49   a table...** ^I
20300 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f 64  f the action cod
20310 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 45 4c  e is [SQLITE_DEL
20320 45 54 45 5d 20 61 6e 64 20 74 68 65 20 63 61 6c  ETE] and the cal
20330 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0d 0a 2a  lback returns..*
20340 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  * [SQLITE_IGNORE
20350 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 4c 45  ] then the [DELE
20360 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 70 72  TE] operation pr
20370 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 0d 0a  oceeds but the..
20380 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74  ** [truncate opt
20390 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 69  imization] is di
203a0 73 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 72  sabled and all r
203b0 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20  ows are deleted 
203c0 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0d 0a 2a  individually...*
203d0 2a 0d 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69  *..** An authori
203e0 7a 65 72 20 69 73 20 75 73 65 64 20 77 68 65 6e  zer is used when
203f0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
20400 65 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0d 0a  e | preparing]..
20410 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
20420 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73  s from an untrus
20430 74 65 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65  ted source, to e
20440 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 53  nsure that the S
20450 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0d 0a 2a  QL statements..*
20460 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20  * do not try to 
20470 61 63 63 65 73 73 20 64 61 74 61 20 74 68 65 79  access data they
20480 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64   are not allowed
20490 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 68 61 74   to see, or that
204a0 20 74 68 65 79 20 64 6f 20 6e 6f 74 0d 0a 2a 2a   they do not..**
204b0 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 65 20   try to execute 
204c0 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 65 6d  malicious statem
204d0 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 67 65  ents that damage
204e0 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
204f0 46 6f 72 0d 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c  For..** example,
20500 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
20510 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 72  may allow a user
20520 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 72   to enter arbitr
20530 61 72 79 0d 0a 2a 2a 20 53 51 4c 20 71 75 65 72  ary..** SQL quer
20540 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69  ies for evaluati
20550 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65  on by a database
20560 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69  .  But the appli
20570 63 61 74 69 6f 6e 20 64 6f 65 73 0d 0a 2a 2a 20  cation does..** 
20580 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65  not want the use
20590 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  r to be able to 
205a0 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63  make arbitrary c
205b0 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0d 0a 2a  hanges to the..*
205c0 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20  * database.  An 
205d0 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64  authorizer could
205e0 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20   then be put in 
205f0 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0d  place while the.
20600 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64  .** user-entered
20610 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73   SQL is being [s
20620 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c  qlite3_prepare |
20630 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 0d   prepared] that.
20640 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76  .** disallows ev
20650 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20  erything except 
20660 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
20670 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 70 70  nts...**..** App
20680 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e  lications that n
20690 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20 53  eed to process S
206a0 51 4c 20 66 72 6f 6d 20 75 6e 74 72 75 73 74 65  QL from untruste
206b0 64 20 73 6f 75 72 63 65 73 0d 0a 2a 2a 20 6d 69  d sources..** mi
206c0 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65  ght also conside
206d0 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 73 6f 75  r lowering resou
206e0 72 63 65 20 6c 69 6d 69 74 73 20 75 73 69 6e 67  rce limits using
206f0 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28   [sqlite3_limit(
20700 29 5d 0d 0a 2a 2a 20 61 6e 64 20 6c 69 6d 69 74  )]..** and limit
20710 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 69 7a  ing database siz
20720 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d 61 78  e using the [max
20730 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52  _page_count] [PR
20740 41 47 4d 41 5d 0d 0a 2a 2a 20 69 6e 20 61 64 64  AGMA]..** in add
20750 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 61  ition to using a
20760 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e 0d 0a 2a  n authorizer...*
20770 2a 0d 0a 2a 2a 20 5e 28 4f 6e 6c 79 20 61 20 73  *..** ^(Only a s
20780 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a 65 72  ingle authorizer
20790 20 63 61 6e 20 62 65 20 69 6e 20 70 6c 61 63 65   can be in place
207a0 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 63   on a database c
207b0 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 61 74  onnection..** at
207c0 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 20 63   a time.  Each c
207d0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
207e0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f 76  et_authorizer ov
207f0 65 72 72 69 64 65 73 20 74 68 65 0d 0a 2a 2a 20  errides the..** 
20800 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 29 5e  previous call.)^
20810 20 20 5e 44 69 73 61 62 6c 65 20 74 68 65 20 61    ^Disable the a
20820 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73  uthorizer by ins
20830 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63  talling a NULL c
20840 61 6c 6c 62 61 63 6b 2e 0d 0a 2a 2a 20 54 68 65  allback...** The
20850 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64   authorizer is d
20860 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  isabled by defau
20870 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  lt...**..** The 
20880 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
20890 61 63 6b 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20  ack must not do 
208a0 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69  anything that wi
208b0 6c 6c 20 6d 6f 64 69 66 79 0d 0a 2a 2a 20 74 68  ll modify..** th
208c0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
208d0 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
208e0 65 64 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  ed the authorize
208f0 72 20 63 61 6c 6c 62 61 63 6b 2e 0d 0a 2a 2a 20  r callback...** 
20900 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
20910 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
20920 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
20930 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
20940 79 20 74 68 65 69 72 0d 0a 2a 2a 20 64 61 74 61  y their..** data
20950 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
20960 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67   for the meaning
20970 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20   of "modify" in 
20980 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0d  this paragraph..
20990 0a 2a 2a 0d 0a 2a 2a 20 5e 57 68 65 6e 20 5b 73  .**..** ^When [s
209a0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
209b0 32 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20  2()] is used to 
209c0 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d  prepare a statem
209d0 65 6e 74 2c 20 74 68 65 0d 0a 2a 2a 20 73 74 61  ent, the..** sta
209e0 74 65 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20  tement might be 
209f0 72 65 2d 70 72 65 70 61 72 65 64 20 64 75 72 69  re-prepared duri
20a00 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ng [sqlite3_step
20a10 28 29 5d 20 64 75 65 20 74 6f 20 61 20 0d 0a 2a  ()] due to a ..*
20a20 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  * schema change.
20a30 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61 70 70    Hence, the app
20a40 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  lication should 
20a50 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 0d  ensure that the.
20a60 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74 68  .** correct auth
20a70 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
20a80 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63 65  remains in place
20a90 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71 6c   during the [sql
20aa0 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0d 0a 2a  ite3_step()]...*
20ab0 2a 0d 0a 2a 2a 20 5e 4e 6f 74 65 20 74 68 61 74  *..** ^Note that
20ac0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
20ad0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
20ae0 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0d  ked only during.
20af0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
20b00 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76  pare()] or its v
20b10 61 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72  ariants.  Author
20b20 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0d 0a  ization is not..
20b30 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72  ** performed dur
20b40 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76  ing statement ev
20b50 61 6c 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c  aluation in [sql
20b60 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e  ite3_step()], un
20b70 6c 65 73 73 0d 0a 2a 2a 20 61 73 20 73 74 61 74  less..** as stat
20b80 65 64 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f  ed in the previo
20b90 75 73 20 70 61 72 61 67 72 61 70 68 2c 20 73 71  us paragraph, sq
20ba0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e 76  lite3_step() inv
20bb0 6f 6b 65 73 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  okes..** sqlite3
20bc0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 74 6f  _prepare_v2() to
20bd0 20 72 65 70 72 65 70 61 72 65 20 61 20 73 74 61   reprepare a sta
20be0 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73  tement after a s
20bf0 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0d 0a 2a  chema change...*
20c00 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
20c10 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  t sqlite3_set_au
20c20 74 68 6f 72 69 7a 65 72 28 0d 0a 20 20 73 71 6c  thorizer(..  sql
20c30 69 74 65 33 2a 2c 0d 0a 20 20 69 6e 74 20 28 2a  ite3*,..  int (*
20c40 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
20c50 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
20c60 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
20c70 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
20c80 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 55 73 65  ),..  void *pUse
20c90 72 44 61 74 61 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d  rData..);..../*.
20ca0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75  .** CAPI3REF: Au
20cb0 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 6e 20  thorizer Return 
20cc0 43 6f 64 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  Codes..**..** Th
20cd0 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  e [sqlite3_set_a
20ce0 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68  uthorizer | auth
20cf0 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
20d00 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0d 0a  function] must..
20d10 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72  ** return either
20d20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20   [SQLITE_OK] or 
20d30 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f  one of these two
20d40 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72   constants in or
20d50 64 65 72 0d 0a 2a 2a 20 74 6f 20 73 69 67 6e 61  der..** to signa
20d60 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72  l SQLite whether
20d70 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69   or not the acti
20d80 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  on is permitted.
20d90 20 20 53 65 65 20 74 68 65 0d 0a 2a 2a 20 5b 73    See the..** [s
20da0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
20db0 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
20dc0 65 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  er documentation
20dd0 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ] for additional
20de0 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ..** information
20df0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74  ...**..** Note t
20e00 68 61 74 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  hat SQLITE_IGNOR
20e10 45 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 61  E is also used a
20e20 73 20 61 20 5b 53 51 4c 49 54 45 5f 52 4f 4c 4c  s a [SQLITE_ROLL
20e30 42 41 43 4b 20 7c 20 72 65 74 75 72 6e 20 63 6f  BACK | return co
20e40 64 65 5d 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  de]..** from the
20e50 20 5b 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f   [sqlite3_vtab_o
20e60 6e 5f 63 6f 6e 66 6c 69 63 74 28 29 5d 20 69 6e  n_conflict()] in
20e70 74 65 72 66 61 63 65 2e 0d 0a 2a 2f 0d 0a 23 64  terface...*/..#d
20e80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e  efine SQLITE_DEN
20e90 59 20 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74  Y   1   /* Abort
20ea0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
20eb0 6e 74 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  nt with an error
20ec0 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
20ed0 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f  ITE_IGNORE 2   /
20ee0 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63  * Don't allow ac
20ef0 63 65 73 73 2c 20 62 75 74 20 64 6f 6e 27 74 20  cess, but don't 
20f00 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
20f10 72 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  r */..../*..** C
20f20 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
20f30 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73  zer Action Codes
20f40 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 5b 73 71  ..**..** The [sq
20f50 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
20f60 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63  izer()] interfac
20f70 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61  e registers a ca
20f80 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0d  llback function.
20f90 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f  .** that is invo
20fa0 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65  ked to authorize
20fb0 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61   certain SQL sta
20fc0 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20  tement actions. 
20fd0 20 54 68 65 0d 0a 2a 2a 20 73 65 63 6f 6e 64 20   The..** second 
20fe0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
20ff0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
21000 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61  integer code tha
21010 74 20 73 70 65 63 69 66 69 65 73 0d 0a 2a 2a 20  t specifies..** 
21020 77 68 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62  what action is b
21030 65 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e  eing authorized.
21040 20 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20    These are the 
21050 69 6e 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63  integer action c
21060 6f 64 65 73 20 74 68 61 74 0d 0a 2a 2a 20 74 68  odes that..** th
21070 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
21080 6c 62 61 63 6b 20 6d 61 79 20 62 65 20 70 61 73  lback may be pas
21090 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  sed...**..** The
210a0 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 76  se action code v
210b0 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 77 68  alues signify wh
210c0 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 72 61  at kind of opera
210d0 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0d 0a 2a  tion is to be..*
210e0 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54  * authorized.  T
210f0 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 70  he 3rd and 4th p
21100 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65  arameters to the
21110 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0d 0a   authorization..
21120 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  ** callback func
21130 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72  tion will be par
21140 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20  ameters or NULL 
21150 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69  depending on whi
21160 63 68 20 6f 66 20 74 68 65 73 65 0d 0a 2a 2a 20  ch of these..** 
21170 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73  codes is used as
21180 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
21190 6d 65 74 65 72 2e 20 20 5e 28 54 68 65 20 35 74  meter.  ^(The 5t
211a0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
211b0 68 65 0d 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65  he..** authorize
211c0 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68  r callback is th
211d0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
211e0 74 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20  tabase ("main", 
211f0 22 74 65 6d 70 22 2c 0d 0a 2a 2a 20 65 74 63 2e  "temp",..** etc.
21200 29 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2e  ) if applicable.
21210 29 5e 20 20 5e 54 68 65 20 36 74 68 20 70 61 72  )^  ^The 6th par
21220 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
21230 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
21240 6b 0d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d  k..** is the nam
21250 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d  e of the inner-m
21260 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76  ost trigger or v
21270 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70  iew that is resp
21280 6f 6e 73 69 62 6c 65 20 66 6f 72 0d 0a 2a 2a 20  onsible for..** 
21290 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d  the access attem
212a0 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68  pt or NULL if th
212b0 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  is access attemp
212c0 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72  t is directly fr
212d0 6f 6d 0d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c  om..** top-level
212e0 20 53 51 4c 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a   SQL code...*/..
212f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
21300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64  ************ 3rd
21320 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74   ************ 4t
21330 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  h ***********/..
21340 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21350 52 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20  REATE_INDEX     
21360 20 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65       1   /* Inde
21370 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
21380 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21390 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
213a0 52 45 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20  REATE_TABLE     
213b0 20 20 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c       2   /* Tabl
213c0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
213d0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
213e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
213f0 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58  REATE_TEMP_INDEX
21400 20 20 20 20 20 33 20 20 20 2f 2a 20 49 6e 64 65       3   /* Inde
21410 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
21420 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21430 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21440 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45  REATE_TEMP_TABLE
21450 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61 62 6c       4   /* Tabl
21460 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
21470 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21480 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21490 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47  REATE_TEMP_TRIGG
214a0 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69 67  ER   5   /* Trig
214b0 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
214c0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
214d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
214e0 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20  REATE_TEMP_VIEW 
214f0 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77       6   /* View
21500 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
21510 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21520 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21530 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20  REATE_TRIGGER   
21540 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67       7   /* Trig
21550 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
21560 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21570 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21580 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20  REATE_VIEW      
21590 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77       8   /* View
215a0 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
215b0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
215c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
215d0 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20  ELETE           
215e0 20 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c       9   /* Tabl
215f0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
21600 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21610 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21620 52 4f 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20  ROP_INDEX       
21630 20 20 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65      10   /* Inde
21640 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
21650 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21660 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21670 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20 20 20  ROP_TABLE       
21680 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61 62 6c      11   /* Tabl
21690 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
216a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
216b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
216c0 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20  ROP_TEMP_INDEX  
216d0 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64 65      12   /* Inde
216e0 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
216f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21700 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21710 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20  ROP_TEMP_TABLE  
21720 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c      13   /* Tabl
21730 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
21740 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21750 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21760 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
21770 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67      14   /* Trig
21780 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
21790 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
217a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
217b0 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20  ROP_TEMP_VIEW   
217c0 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77      15   /* View
217d0 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
217e0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
217f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21800 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20 20  ROP_TRIGGER     
21810 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67      16   /* Trig
21820 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
21830 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21840 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21850 52 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20  ROP_VIEW        
21860 20 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77      17   /* View
21870 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
21880 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21890 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
218a0 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
218b0 20 20 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c      18   /* Tabl
218c0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
218d0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
218e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50  #define SQLITE_P
218f0 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20  RAGMA           
21900 20 20 20 20 31 39 20 20 20 2f 2a 20 50 72 61 67      19   /* Prag
21910 6d 61 20 4e 61 6d 65 20 20 20 20 20 31 73 74 20  ma Name     1st 
21920 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a  arg or NULL */..
21930 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
21940 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20  EAD             
21950 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61 62 6c      20   /* Tabl
21960 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75  e Name      Colu
21970 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0d 0a  mn Name     */..
21980 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
21990 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20  ELECT           
219a0 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c 4c      21   /* NULL
219b0 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
219c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
219d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
219e0 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20  RANSACTION      
219f0 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 72      22   /* Oper
21a00 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c 4c  ation       NULL
21a10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21a20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
21a30 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20  PDATE           
21a40 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c      23   /* Tabl
21a50 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75  e Name      Colu
21a60 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0d 0a  mn Name     */..
21a70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
21a80 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  TTACH           
21a90 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65      24   /* File
21aa0 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c  name        NULL
21ab0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21ac0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21ad0 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  ETACH           
21ae0 20 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61      25   /* Data
21af0 62 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c  base Name   NULL
21b00 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21b10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
21b20 4c 54 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20  LTER_TABLE      
21b30 20 20 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61      26   /* Data
21b40 62 61 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c  base Name   Tabl
21b50 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0d 0a  e Name      */..
21b60 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
21b70 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  EINDEX          
21b80 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e 64 65      27   /* Inde
21b90 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  x Name      NULL
21ba0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21bb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
21bc0 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20  NALYZE          
21bd0 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62 6c      28   /* Tabl
21be0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
21bf0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
21c00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21c10 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20  REATE_VTABLE    
21c20 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c      29   /* Tabl
21c30 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75  e Name      Modu
21c40 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0d 0a  le Name     */..
21c50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21c60 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20  ROP_VTABLE      
21c70 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c      30   /* Tabl
21c80 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75  e Name      Modu
21c90 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0d 0a  le Name     */..
21ca0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
21cb0 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20  UNCTION         
21cc0 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c      31   /* NULL
21cd0 20 20 20 20 20 20 20 20 20 20 20 20 46 75 6e 63              Func
21ce0 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0d 0a  tion Name   */..
21cf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
21d00 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20  AVEPOINT        
21d10 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72      32   /* Oper
21d20 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65  ation       Save
21d30 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0d 0a  point Name  */..
21d40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
21d50 4f 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20  OPY             
21d60 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c       0   /* No l
21d70 6f 6e 67 65 72 20 75 73 65 64 20 2a 2f 0d 0a 0d  onger used */...
21d80 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
21d90 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20 50 72  : Tracing And Pr
21da0 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69 6f 6e  ofiling Function
21db0 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20  s..**..** These 
21dc0 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73 74 65  routines registe
21dd0 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
21de0 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 62 65  ions that can be
21df0 20 75 73 65 64 20 66 6f 72 0d 0a 2a 2a 20 74 72   used for..** tr
21e00 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69 6c  acing and profil
21e10 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f  ing the executio
21e20 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65  n of SQL stateme
21e30 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  nts...**..** ^Th
21e40 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
21e50 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
21e60 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28  y sqlite3_trace(
21e70 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0d  ) is invoked at.
21e80 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65  .** various time
21e90 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74  s when an SQL st
21ea0 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67  atement is being
21eb0 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33   run by [sqlite3
21ec0 5f 73 74 65 70 28 29 5d 2e 0d 0a 2a 2a 20 5e 54  _step()]...** ^T
21ed0 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  he sqlite3_trace
21ee0 28 29 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  () callback is i
21ef0 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 20 55 54  nvoked with a UT
21f00 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20 6f 66  F-8 rendering of
21f10 20 74 68 65 0d 0a 2a 2a 20 53 51 4c 20 73 74 61   the..** SQL sta
21f20 74 65 6d 65 6e 74 20 74 65 78 74 20 61 73 20 74  tement text as t
21f30 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 72  he statement fir
21f40 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 75 74  st begins execut
21f50 69 6e 67 2e 0d 0a 2a 2a 20 5e 28 41 64 64 69 74  ing...** ^(Addit
21f60 69 6f 6e 61 6c 20 73 71 6c 69 74 65 33 5f 74 72  ional sqlite3_tr
21f70 61 63 65 28 29 20 63 61 6c 6c 62 61 63 6b 73 20  ace() callbacks 
21f80 6d 69 67 68 74 20 6f 63 63 75 72 0d 0a 2a 2a 20  might occur..** 
21f90 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72 65  as each triggere
21fa0 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20  d subprogram is 
21fb0 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63 61  entered.  The ca
21fc0 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67  llbacks for trig
21fd0 67 65 72 73 0d 0a 2a 2a 20 63 6f 6e 74 61 69 6e  gers..** contain
21fe0 20 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d   a UTF-8 SQL com
21ff0 6d 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69  ment that identi
22000 66 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72  fies the trigger
22010 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  .)^..**..** ^The
22020 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
22030 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79  on registered by
22040 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65   sqlite3_profile
22050 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0d 0a 2a  () is invoked..*
22060 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74  * as each SQL st
22070 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73  atement finishes
22080 2e 20 20 5e 54 68 65 20 70 72 6f 66 69 6c 65 20  .  ^The profile 
22090 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
220a0 73 0d 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  s..** the origin
220b0 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  al statement tex
220c0 74 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74  t and an estimat
220d0 65 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20  e of wall-clock 
220e0 74 69 6d 65 0d 0a 2a 2a 20 6f 66 20 68 6f 77 20  time..** of how 
220f0 6c 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d  long that statem
22100 65 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e  ent took to run.
22110 20 20 5e 54 68 65 20 70 72 6f 66 69 6c 65 20 63    ^The profile c
22120 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 74 69 6d 65  allback..** time
22130 20 69 73 20 69 6e 20 75 6e 69 74 73 20 6f 66 20   is in units of 
22140 6e 61 6e 6f 73 65 63 6f 6e 64 73 2c 20 68 6f 77  nanoseconds, how
22150 65 76 65 72 20 74 68 65 20 63 75 72 72 65 6e 74  ever the current
22160 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0d   implementation.
22170 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 63 61 70 61  .** is only capa
22180 62 6c 65 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ble of milliseco
22190 6e 64 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 6f  nd resolution so
221a0 20 74 68 65 20 73 69 78 20 6c 65 61 73 74 20 73   the six least s
221b0 69 67 6e 69 66 69 63 61 6e 74 0d 0a 2a 2a 20 64  ignificant..** d
221c0 69 67 69 74 73 20 69 6e 20 74 68 65 20 74 69 6d  igits in the tim
221d0 65 20 61 72 65 20 6d 65 61 6e 69 6e 67 6c 65 73  e are meaningles
221e0 73 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69  s.  Future versi
221f0 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0d 0a 2a  ons of SQLite..*
22200 2a 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20  * might provide 
22210 67 72 65 61 74 65 72 20 72 65 73 6f 6c 75 74 69  greater resoluti
22220 6f 6e 20 6f 6e 20 74 68 65 20 70 72 6f 66 69 6c  on on the profil
22230 65 72 20 63 61 6c 6c 62 61 63 6b 2e 20 20 54 68  er callback.  Th
22240 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72  e..** sqlite3_pr
22250 6f 66 69 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e  ofile() function
22260 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 65   is considered e
22270 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20  xperimental and 
22280 69 73 0d 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74  is..** subject t
22290 6f 20 63 68 61 6e 67 65 20 69 6e 20 66 75 74 75  o change in futu
222a0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  re versions of S
222b0 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  QLite...*/..SQLI
222c0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
222d0 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74  ite3_trace(sqlit
222e0 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 61 63  e3*, void(*xTrac
222f0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
22300 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b 0d 0a  har*), void*);..
22310 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
22320 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 76  E_EXPERIMENTAL v
22330 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f  oid *sqlite3_pro
22340 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0d 0a  file(sqlite3*,..
22350 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c     void(*xProfil
22360 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
22370 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e  har*,sqlite3_uin
22380 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0d 0a 0d  t64), void*);...
22390 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
223a0 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73  : Query Progress
223b0 20 43 61 6c 6c 62 61 63 6b 73 0d 0a 2a 2a 0d 0a   Callbacks..**..
223c0 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
223d0 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
223e0 28 44 2c 4e 2c 58 2c 50 29 20 69 6e 74 65 72 66  (D,N,X,P) interf
223f0 61 63 65 20 63 61 75 73 65 73 20 74 68 65 20 63  ace causes the c
22400 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 66 75 6e 63  allback..** func
22410 74 69 6f 6e 20 58 20 74 6f 20 62 65 20 69 6e 76  tion X to be inv
22420 6f 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c  oked periodicall
22430 79 20 64 75 72 69 6e 67 20 6c 6f 6e 67 20 72 75  y during long ru
22440 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 0d 0a  nning calls to..
22450 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ** [sqlite3_exec
22460 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
22470 65 70 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ep()] and [sqlit
22480 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20  e3_get_table()] 
22490 66 6f 72 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65  for..** database
224a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 20 20   connection D.  
224b0 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 65 20 66  An example use f
224c0 6f 72 20 74 68 69 73 0d 0a 2a 2a 20 69 6e 74 65  or this..** inte
224d0 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70  rface is to keep
224e0 20 61 20 47 55 49 20 75 70 64 61 74 65 64 20 64   a GUI updated d
224f0 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75  uring a large qu
22500 65 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ery...**..** ^Th
22510 65 20 70 61 72 61 6d 65 74 65 72 20 50 20 69 73  e parameter P is
22520 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20   passed through 
22530 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61  as the only para
22540 6d 65 74 65 72 20 74 6f 20 74 68 65 20 0d 0a 2a  meter to the ..*
22550 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  * callback funct
22560 69 6f 6e 20 58 2e 20 20 5e 54 68 65 20 70 61 72  ion X.  ^The par
22570 61 6d 65 74 65 72 20 4e 20 69 73 20 74 68 65 20  ameter N is the 
22580 6e 75 6d 62 65 72 20 6f 66 20 0d 0a 2a 2a 20 5b  number of ..** [
22590 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
225a0 69 6e 73 74 72 75 63 74 69 6f 6e 73 5d 20 74 68  instructions] th
225b0 61 74 20 61 72 65 20 65 76 61 6c 75 61 74 65 64  at are evaluated
225c0 20 62 65 74 77 65 65 6e 20 73 75 63 63 65 73 73   between success
225d0 69 76 65 0d 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  ive..** invocati
225e0 6f 6e 73 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ons of the callb
225f0 61 63 6b 20 58 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ack X...**..** ^
22600 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72  Only a single pr
22610 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d  ogress handler m
22620 61 79 20 62 65 20 64 65 66 69 6e 65 64 20 61 74  ay be defined at
22630 20 6f 6e 65 20 74 69 6d 65 20 70 65 72 0d 0a 2a   one time per..*
22640 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
22650 65 63 74 69 6f 6e 5d 3b 20 73 65 74 74 69 6e 67  ection]; setting
22660 20 61 20 6e 65 77 20 70 72 6f 67 72 65 73 73 20   a new progress 
22670 68 61 6e 64 6c 65 72 20 63 61 6e 63 65 6c 73 20  handler cancels 
22680 74 68 65 0d 0a 2a 2a 20 6f 6c 64 20 6f 6e 65 2e  the..** old one.
22690 20 20 5e 53 65 74 74 69 6e 67 20 70 61 72 61 6d    ^Setting param
226a0 65 74 65 72 20 58 20 74 6f 20 4e 55 4c 4c 20 64  eter X to NULL d
226b0 69 73 61 62 6c 65 73 20 74 68 65 20 70 72 6f 67  isables the prog
226c0 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0d 0a 2a  ress handler...*
226d0 2a 20 5e 54 68 65 20 70 72 6f 67 72 65 73 73 20  * ^The progress 
226e0 68 61 6e 64 6c 65 72 20 69 73 20 61 6c 73 6f 20  handler is also 
226f0 64 69 73 61 62 6c 65 64 20 62 79 20 73 65 74 74  disabled by sett
22700 69 6e 67 20 4e 20 74 6f 20 61 20 76 61 6c 75 65  ing N to a value
22710 20 6c 65 73 73 0d 0a 2a 2a 20 74 68 61 6e 20 31   less..** than 1
22720 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68  ...**..** ^If th
22730 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
22740 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
22750 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 61 74  zero, the operat
22760 69 6f 6e 20 69 73 0d 0a 2a 2a 20 69 6e 74 65 72  ion is..** inter
22770 72 75 70 74 65 64 2e 20 20 54 68 69 73 20 66 65  rupted.  This fe
22780 61 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65  ature can be use
22790 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
227a0 0d 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75  ..** "Cancel" bu
227b0 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72  tton on a GUI pr
227c0 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f  ogress dialog bo
227d0 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70  x...**..** The p
227e0 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20  rogress handler 
227f0 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f  callback must no
22800 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68  t do anything th
22810 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0d 0a  at will modify..
22820 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
22830 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
22840 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 72 6f 67  invoked the prog
22850 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0d 0a 2a  ress handler...*
22860 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c  * Note that [sql
22870 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
22880 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
22890 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64  step()] both mod
228a0 69 66 79 20 74 68 65 69 72 0d 0a 2a 2a 20 64 61  ify their..** da
228b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
228c0 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
228d0 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
228e0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
228f0 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ...**..*/..SQLIT
22900 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
22910 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  e3_progress_hand
22920 6c 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ler(sqlite3*, in
22930 74 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29  t, int(*)(void*)
22940 2c 20 76 6f 69 64 2a 29 3b 0d 0a 0d 0a 2f 2a 0d  , void*);..../*.
22950 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 70  .** CAPI3REF: Op
22960 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 74 61  ening A New Data
22970 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0d  base Connection.
22980 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 73 65 20 72  .**..** ^These r
22990 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e 20  outines open an 
229a0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
229b0 66 69 6c 65 20 61 73 20 73 70 65 63 69 66 69 65  file as specifie
229c0 64 20 62 79 20 74 68 65 20 0d 0a 2a 2a 20 66 69  d by the ..** fi
229d0 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e  lename argument.
229e0 20 5e 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61   ^The filename a
229f0 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72  rgument is inter
22a00 70 72 65 74 65 64 20 61 73 20 55 54 46 2d 38 20  preted as UTF-8 
22a10 66 6f 72 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  for..** sqlite3_
22a20 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  open() and sqlit
22a30 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64  e3_open_v2() and
22a40 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 74 68   as UTF-16 in th
22a50 65 20 6e 61 74 69 76 65 20 62 79 74 65 0d 0a 2a  e native byte..*
22a60 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 71 6c 69  * order for sqli
22a70 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 5e 28  te3_open16(). ^(
22a80 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  A [database conn
22a90 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 69  ection] handle i
22aa0 73 20 75 73 75 61 6c 6c 79 0d 0a 2a 2a 20 72 65  s usually..** re
22ab0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c  turned in *ppDb,
22ac0 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f   even if an erro
22ad0 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f  r occurs.  The o
22ae0 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73  nly exception is
22af0 20 74 68 61 74 0d 0a 2a 2a 20 69 66 20 53 51 4c   that..** if SQL
22b00 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
22b10 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
22b20 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71   to hold the [sq
22b30 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0d 0a  lite3] object,..
22b40 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62  ** a NULL will b
22b50 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a  e written into *
22b60 70 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20  ppDb instead of 
22b70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
22b80 20 5b 73 71 6c 69 74 65 33 5d 0d 0a 2a 2a 20 6f   [sqlite3]..** o
22b90 62 6a 65 63 74 2e 29 5e 20 5e 28 49 66 20 74 68  bject.)^ ^(If th
22ba0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  e database is op
22bb0 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65  ened (and/or cre
22bc0 61 74 65 64 29 20 73 75 63 63 65 73 73 66 75 6c  ated) successful
22bd0 6c 79 2c 20 74 68 65 6e 0d 0a 2a 2a 20 5b 53 51  ly, then..** [SQ
22be0 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75  LITE_OK] is retu
22bf0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65  rned.  Otherwise
22c00 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
22c10 20 69 73 20 72 65 74 75 72 6e 65 64 2e 29 5e 20   is returned.)^ 
22c20 5e 54 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65  ^The..** [sqlite
22c30 33 5f 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b  3_errmsg()] or [
22c40 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
22c50 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e  ()] routines can
22c60 20 62 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61   be used to obta
22c70 69 6e 0d 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73  in..** an Englis
22c80 68 20 6c 61 6e 67 75 61 67 65 20 64 65 73 63 72  h language descr
22c90 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72  iption of the er
22ca0 72 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ror following a 
22cb0 66 61 69 6c 75 72 65 20 6f 66 20 61 6e 79 0d 0a  failure of any..
22cc0 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ** of the sqlite
22cd0 33 5f 6f 70 65 6e 28 29 20 72 6f 75 74 69 6e 65  3_open() routine
22ce0 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  s...**..** ^The 
22cf0 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 69 6e 67  default encoding
22d00 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
22d10 65 20 77 69 6c 6c 20 62 65 20 55 54 46 2d 38 20  e will be UTF-8 
22d20 69 66 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  if..** sqlite3_o
22d30 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33  pen() or sqlite3
22d40 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 63 61  _open_v2() is ca
22d50 6c 6c 65 64 20 61 6e 64 0d 0a 2a 2a 20 55 54 46  lled and..** UTF
22d60 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76  -16 in the nativ
22d70 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20  e byte order if 
22d80 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
22d90 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a   is used...**..*
22da0 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
22db0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
22dc0 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e   when it is open
22dd0 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0d 0a 2a  ed, resources..*
22de0 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
22df0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  h the [database 
22e00 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64  connection] hand
22e10 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c  le should be rel
22e20 65 61 73 65 64 20 62 79 0d 0a 2a 2a 20 70 61 73  eased by..** pas
22e30 73 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69  sing it to [sqli
22e40 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65  te3_close()] whe
22e50 6e 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  n it is no longe
22e60 72 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a 2a 0d  r required...**.
22e70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
22e80 6f 70 65 6e 5f 76 32 28 29 20 69 6e 74 65 72 66  open_v2() interf
22e90 61 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73  ace works like s
22ea0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 0d 0a 2a  qlite3_open()..*
22eb0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
22ec0 20 61 63 63 65 70 74 73 20 74 77 6f 20 61 64 64   accepts two add
22ed0 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65  itional paramete
22ee0 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  rs for additiona
22ef0 6c 20 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a 20 6f 76  l control..** ov
22f00 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62  er the new datab
22f10 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
22f20 20 5e 28 54 68 65 20 66 6c 61 67 73 20 70 61 72   ^(The flags par
22f30 61 6d 65 74 65 72 20 74 6f 0d 0a 2a 2a 20 73 71  ameter to..** sq
22f40 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
22f50 63 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0d  can take one of.
22f60 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
22f70 67 20 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20  g three values, 
22f80 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69  optionally combi
22f90 6e 65 64 20 77 69 74 68 20 74 68 65 20 0d 0a 2a  ned with the ..*
22fa0 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  * [SQLITE_OPEN_N
22fb0 4f 4d 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45  OMUTEX], [SQLITE
22fc0 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d  _OPEN_FULLMUTEX]
22fd0 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  , [SQLITE_OPEN_S
22fe0 48 41 52 45 44 43 41 43 48 45 5d 2c 0d 0a 2a 2a  HAREDCACHE],..**
22ff0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52   [SQLITE_OPEN_PR
23000 49 56 41 54 45 43 41 43 48 45 5d 2c 20 61 6e 64  IVATECACHE], and
23010 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  /or [SQLITE_OPEN
23020 5f 55 52 49 5d 20 66 6c 61 67 73 3a 29 5e 0d 0a  _URI] flags:)^..
23030 2a 2a 0d 0a 2a 2a 20 3c 64 6c 3e 0d 0a 2a 2a 20  **..** <dl>..** 
23040 5e 28 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  ^(<dt>[SQLITE_OP
23050 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74  EN_READONLY]</dt
23060 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  >..** <dd>The da
23070 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
23080 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f   in read-only mo
23090 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 61  de.  If the data
230a0 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a  base does not..*
230b0 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c  * already exist,
230c0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
230d0 75 72 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a  urned.</dd>)^..*
230e0 2a 0d 0a 2a 2a 20 5e 28 3c 64 74 3e 5b 53 51 4c  *..** ^(<dt>[SQL
230f0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
23100 54 45 5d 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64  TE]</dt>..** <dd
23110 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73  >The database is
23120 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
23130 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20  ing and writing 
23140 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 20  if possible, or 
23150 72 65 61 64 69 6e 67 0d 0a 2a 2a 20 6f 6e 6c 79  reading..** only
23160 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
23170 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20  write protected 
23180 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  by the operating
23190 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74   system.  In eit
231a0 68 65 72 0d 0a 2a 2a 20 63 61 73 65 20 74 68 65  her..** case the
231b0 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 61   database must a
231c0 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f 74  lready exist, ot
231d0 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72  herwise an error
231e0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64   is returned.</d
231f0 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 3c  d>)^..**..** ^(<
23200 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  dt>[SQLITE_OPEN_
23210 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51  READWRITE] | [SQ
23220 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
23230 5d 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54  ]</dt>..** <dd>T
23240 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
23250 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e  pened for readin
23260 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61  g and writing, a
23270 6e 64 20 69 73 20 63 72 65 61 74 65 64 20 69 66  nd is created if
23280 0d 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74  ..** it does not
23290 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20   already exist. 
232a0 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61  This is the beha
232b0 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77  vior that is alw
232c0 61 79 73 20 75 73 65 64 20 66 6f 72 0d 0a 2a 2a  ays used for..**
232d0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
232e0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
232f0 31 36 28 29 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a  16().</dd>)^..**
23300 20 3c 2f 64 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   </dl>..**..** I
23310 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
23320 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f  ter to sqlite3_o
23330 70 65 6e 5f 76 32 28 29 20 69 73 20 6e 6f 74 20  pen_v2() is not 
23340 6f 6e 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 63  one of the..** c
23350 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77  ombinations show
23360 6e 20 61 62 6f 76 65 20 6f 70 74 69 6f 6e 61 6c  n above optional
23370 6c 79 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  ly combined with
23380 20 6f 74 68 65 72 0d 0a 2a 2a 20 5b 53 51 4c 49   other..** [SQLI
23390 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
233a0 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 2a   | SQLITE_OPEN_*
233b0 20 62 69 74 73 5d 0d 0a 2a 2a 20 74 68 65 6e 20   bits]..** then 
233c0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
233d0 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a  undefined...**..
233e0 2a 2a 20 5e 49 66 20 74 68 65 20 5b 53 51 4c 49  ** ^If the [SQLI
233f0 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d  TE_OPEN_NOMUTEX]
23400 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
23410 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
23420 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 6f  connection..** o
23430 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74  pens in the mult
23440 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64  i-thread [thread
23450 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e  ing mode] as lon
23460 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d  g as the single-
23470 74 68 72 65 61 64 0d 0a 2a 2a 20 6d 6f 64 65 20  thread..** mode 
23480 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74  has not been set
23490 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
234a0 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20   or start-time. 
234b0 20 5e 49 66 20 74 68 65 0d 0a 2a 2a 20 5b 53 51   ^If the..** [SQ
234c0 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
234d0 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73 65 74  TEX] flag is set
234e0 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   then the databa
234f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70  se connection op
23500 65 6e 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ens..** in the s
23510 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61  erialized [threa
23520 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c 65 73  ding mode] unles
23530 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 20  s single-thread 
23540 77 61 73 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73  was..** previous
23550 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 63  ly selected at c
23560 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73  ompile-time or s
23570 74 61 72 74 2d 74 69 6d 65 2e 0d 0a 2a 2a 20 5e  tart-time...** ^
23580 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  The [SQLITE_OPEN
23590 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c  _SHAREDCACHE] fl
235a0 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64 61  ag causes the da
235b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
235c0 6e 20 74 6f 20 62 65 0d 0a 2a 2a 20 65 6c 69 67  n to be..** elig
235d0 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61  ible to use [sha
235e0 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c  red cache mode],
235f0 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
23600 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68  hether or not sh
23610 61 72 65 64 0d 0a 2a 2a 20 63 61 63 68 65 20 69  ared..** cache i
23620 73 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20  s enabled using 
23630 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f  [sqlite3_enable_
23640 73 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e  shared_cache()].
23650 20 20 5e 54 68 65 0d 0a 2a 2a 20 5b 53 51 4c 49    ^The..** [SQLI
23660 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43  TE_OPEN_PRIVATEC
23670 41 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65  ACHE] flag cause
23680 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  s the database c
23690 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
236a0 0d 0a 2a 2a 20 70 61 72 74 69 63 69 70 61 74 65  ..** participate
236b0 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63 68   in [shared cach
236c0 65 20 6d 6f 64 65 5d 20 65 76 65 6e 20 69 66 20  e mode] even if 
236d0 69 74 20 69 73 20 65 6e 61 62 6c 65 64 2e 0d 0a  it is enabled...
236e0 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 6f 75 72  **..** ^The four
236f0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
23700 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
23710 29 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ) is the name of
23720 20 74 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65   the..** [sqlite
23730 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 74 68  3_vfs] object th
23740 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f  at defines the o
23750 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
23760 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 0d 0a  interface that..
23770 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 62  ** the new datab
23780 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73  ase connection s
23790 68 6f 75 6c 64 20 75 73 65 2e 20 20 5e 49 66 20  hould use.  ^If 
237a0 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
237b0 65 74 65 72 20 69 73 0d 0a 2a 2a 20 61 20 4e 55  eter is..** a NU
237c0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
237d0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
237e0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
237f0 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a   is used...**..*
23800 2a 20 5e 49 66 20 74 68 65 20 66 69 6c 65 6e 61  * ^If the filena
23810 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22  me is ":memory:"
23820 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65  , then a private
23830 2c 20 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d  , temporary in-m
23840 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0d 0a  emory database..
23850 2a 2a 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  ** is created fo
23860 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  r the connection
23870 2e 20 20 5e 54 68 69 73 20 69 6e 2d 6d 65 6d 6f  .  ^This in-memo
23880 72 79 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  ry database will
23890 20 76 61 6e 69 73 68 20 77 68 65 6e 0d 0a 2a 2a   vanish when..**
238a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
238b0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73  nnection is clos
238c0 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  ed.  Future vers
238d0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
238e0 69 67 68 74 0d 0a 2a 2a 20 6d 61 6b 65 20 75 73  ight..** make us
238f0 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  e of additional 
23900 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65  special filename
23910 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
23920 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63  h the ":" charac
23930 74 65 72 2e 0d 0a 2a 2a 20 49 74 20 69 73 20 72  ter...** It is r
23940 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20  ecommended that 
23950 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20  when a database 
23960 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c  filename actuall
23970 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74  y does begin wit
23980 68 0d 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72  h..** a ":" char
23990 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64  acter you should
239a0 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65   prefix the file
239b0 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68  name with a path
239c0 6e 61 6d 65 20 73 75 63 68 20 61 73 0d 0a 2a 2a  name such as..**
239d0 20 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61   "./" to avoid a
239e0 6d 62 69 67 75 69 74 79 2e 0d 0a 2a 2a 0d 0a 2a  mbiguity...**..*
239f0 2a 20 5e 49 66 20 74 68 65 20 66 69 6c 65 6e 61  * ^If the filena
23a00 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73  me is an empty s
23a10 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72  tring, then a pr
23a20 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79  ivate, temporary
23a30 0d 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74  ..** on-disk dat
23a40 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72  abase will be cr
23a50 65 61 74 65 64 2e 20 20 5e 54 68 69 73 20 70 72  eated.  ^This pr
23a60 69 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77  ivate database w
23a70 69 6c 6c 20 62 65 0d 0a 2a 2a 20 61 75 74 6f 6d  ill be..** autom
23a80 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64  atically deleted
23a90 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
23aa0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
23ab0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0d 0a  ion is closed...
23ac0 2a 2a 0d 0a 2a 2a 20 5b 5b 55 52 49 20 66 69 6c  **..** [[URI fil
23ad0 65 6e 61 6d 65 73 20 69 6e 20 73 71 6c 69 74 65  enames in sqlite
23ae0 33 5f 6f 70 65 6e 28 29 5d 5d 20 3c 68 33 3e 55  3_open()]] <h3>U
23af0 52 49 20 46 69 6c 65 6e 61 6d 65 73 3c 2f 68 33  RI Filenames</h3
23b00 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 5b 55  >..**..** ^If [U
23b10 52 49 20 66 69 6c 65 6e 61 6d 65 5d 20 69 6e 74  RI filename] int
23b20 65 72 70 72 65 74 61 74 69 6f 6e 20 69 73 20 65  erpretation is e
23b30 6e 61 62 6c 65 64 2c 20 61 6e 64 20 74 68 65 20  nabled, and the 
23b40 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e  filename argumen
23b50 74 0d 0a 2a 2a 20 62 65 67 69 6e 73 20 77 69 74  t..** begins wit
23b60 68 20 22 66 69 6c 65 3a 22 2c 20 74 68 65 6e 20  h "file:", then 
23b70 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
23b80 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61  interpreted as a
23b90 20 55 52 49 2e 20 5e 55 52 49 0d 0a 2a 2a 20 66   URI. ^URI..** f
23ba0 69 6c 65 6e 61 6d 65 20 69 6e 74 65 72 70 72 65  ilename interpre
23bb0 74 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  tation is enable
23bc0 64 20 69 66 20 74 68 65 20 5b 53 51 4c 49 54 45  d if the [SQLITE
23bd0 5f 4f 50 45 4e 5f 55 52 49 5d 20 66 6c 61 67 20  _OPEN_URI] flag 
23be0 69 73 0d 0a 2a 2a 20 73 65 74 20 69 6e 20 74 68  is..** set in th
23bf0 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
23c00 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65  t to sqlite3_ope
23c10 6e 5f 76 32 28 29 2c 20 6f 72 20 69 66 20 69 74  n_v2(), or if it
23c20 20 68 61 73 0d 0a 2a 2a 20 62 65 65 6e 20 65 6e   has..** been en
23c30 61 62 6c 65 64 20 67 6c 6f 62 61 6c 6c 79 20 75  abled globally u
23c40 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45  sing the [SQLITE
23c50 5f 43 4f 4e 46 49 47 5f 55 52 49 5d 20 6f 70 74  _CONFIG_URI] opt
23c60 69 6f 6e 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a  ion with the..**
23c70 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
23c80 28 29 5d 20 6d 65 74 68 6f 64 20 6f 72 20 62 79  ()] method or by
23c90 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 55 53 45   the [SQLITE_USE
23ca0 5f 55 52 49 5d 20 63 6f 6d 70 69 6c 65 2d 74 69  _URI] compile-ti
23cb0 6d 65 20 6f 70 74 69 6f 6e 2e 0d 0a 2a 2a 20 41  me option...** A
23cc0 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73  s of SQLite vers
23cd0 69 6f 6e 20 33 2e 37 2e 37 2c 20 55 52 49 20 66  ion 3.7.7, URI f
23ce0 69 6c 65 6e 61 6d 65 20 69 6e 74 65 72 70 72 65  ilename interpre
23cf0 74 61 74 69 6f 6e 20 69 73 20 74 75 72 6e 65 64  tation is turned
23d00 20 6f 66 66 0d 0a 2a 2a 20 62 79 20 64 65 66 61   off..** by defa
23d10 75 6c 74 2c 20 62 75 74 20 66 75 74 75 72 65 20  ult, but future 
23d20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
23d30 74 65 20 6d 69 67 68 74 20 65 6e 61 62 6c 65 20  te might enable 
23d40 55 52 49 20 66 69 6c 65 6e 61 6d 65 0d 0a 2a 2a  URI filename..**
23d50 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
23d60 62 79 20 64 65 66 61 75 6c 74 2e 20 20 53 65 65  by default.  See
23d70 20 22 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65 73   "[URI filenames
23d80 5d 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  ]" for additiona
23d90 6c 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  l..** informatio
23da0 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 55 52 49 20 66  n...**..** URI f
23db0 69 6c 65 6e 61 6d 65 73 20 61 72 65 20 70 61 72  ilenames are par
23dc0 73 65 64 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  sed according to
23dd0 20 52 46 43 20 33 39 38 36 2e 20 5e 49 66 20 74   RFC 3986. ^If t
23de0 68 65 20 55 52 49 20 63 6f 6e 74 61 69 6e 73 20  he URI contains 
23df0 61 6e 0d 0a 2a 2a 20 61 75 74 68 6f 72 69 74 79  an..** authority
23e00 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20 62  , then it must b
23e10 65 20 65 69 74 68 65 72 20 61 6e 20 65 6d 70 74  e either an empt
23e20 79 20 73 74 72 69 6e 67 20 6f 72 20 74 68 65 20  y string or the 
23e30 73 74 72 69 6e 67 20 0d 0a 2a 2a 20 22 6c 6f 63  string ..** "loc
23e40 61 6c 68 6f 73 74 22 2e 20 5e 49 66 20 74 68 65  alhost". ^If the
23e50 20 61 75 74 68 6f 72 69 74 79 20 69 73 20 6e 6f   authority is no
23e60 74 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  t an empty strin
23e70 67 20 6f 72 20 22 6c 6f 63 61 6c 68 6f 73 74 22  g or "localhost"
23e80 2c 20 61 6e 20 0d 0a 2a 2a 20 65 72 72 6f 72 20  , an ..** error 
23e90 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
23ea0 68 65 20 63 61 6c 6c 65 72 2e 20 5e 54 68 65 20  he caller. ^The 
23eb0 66 72 61 67 6d 65 6e 74 20 63 6f 6d 70 6f 6e 65  fragment compone
23ec0 6e 74 20 6f 66 20 61 20 55 52 49 2c 20 69 66 20  nt of a URI, if 
23ed0 0d 0a 2a 2a 20 70 72 65 73 65 6e 74 2c 20 69 73  ..** present, is
23ee0 20 69 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a   ignored...**..*
23ef0 2a 20 5e 53 51 4c 69 74 65 20 75 73 65 73 20 74  * ^SQLite uses t
23f00 68 65 20 70 61 74 68 20 63 6f 6d 70 6f 6e 65 6e  he path componen
23f10 74 20 6f 66 20 74 68 65 20 55 52 49 20 61 73 20  t of the URI as 
23f20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
23f30 64 69 73 6b 20 66 69 6c 65 0d 0a 2a 2a 20 77 68  disk file..** wh
23f40 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ich contains the
23f50 20 64 61 74 61 62 61 73 65 2e 20 5e 49 66 20 74   database. ^If t
23f60 68 65 20 70 61 74 68 20 62 65 67 69 6e 73 20 77  he path begins w
23f70 69 74 68 20 61 20 27 2f 27 20 63 68 61 72 61 63  ith a '/' charac
23f80 74 65 72 2c 20 0d 0a 2a 2a 20 74 68 65 6e 20 69  ter, ..** then i
23f90 74 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64  t is interpreted
23fa0 20 61 73 20 61 6e 20 61 62 73 6f 6c 75 74 65 20   as an absolute 
23fb0 70 61 74 68 2e 20 5e 49 66 20 74 68 65 20 70 61  path. ^If the pa
23fc0 74 68 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69  th does not begi
23fd0 6e 20 0d 0a 2a 2a 20 77 69 74 68 20 61 20 27 2f  n ..** with a '/
23fe0 27 20 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  ' (meaning that 
23ff0 74 68 65 20 61 75 74 68 6f 72 69 74 79 20 73 65  the authority se
24000 63 74 69 6f 6e 20 69 73 20 6f 6d 69 74 74 65 64  ction is omitted
24010 20 66 72 6f 6d 20 74 68 65 20 55 52 49 29 0d 0a   from the URI)..
24020 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 74 68  ** then the path
24030 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20   is interpreted 
24040 61 73 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  as a relative pa
24050 74 68 2e 20 0d 0a 2a 2a 20 5e 4f 6e 20 77 69 6e  th. ..** ^On win
24060 64 6f 77 73 2c 20 74 68 65 20 66 69 72 73 74 20  dows, the first 
24070 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 61 6e 20  component of an 
24080 61 62 73 6f 6c 75 74 65 20 70 61 74 68 20 0d 0a  absolute path ..
24090 2a 2a 20 69 73 20 61 20 64 72 69 76 65 20 73 70  ** is a drive sp
240a0 65 63 69 66 69 63 61 74 69 6f 6e 20 28 65 2e 67  ecification (e.g
240b0 2e 20 22 43 3a 22 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  . "C:")...**..**
240c0 20 5b 5b 63 6f 72 65 20 55 52 49 20 71 75 65 72   [[core URI quer
240d0 79 20 70 61 72 61 6d 65 74 65 72 73 5d 5d 0d 0a  y parameters]]..
240e0 2a 2a 20 54 68 65 20 71 75 65 72 79 20 63 6f 6d  ** The query com
240f0 70 6f 6e 65 6e 74 20 6f 66 20 61 20 55 52 49 20  ponent of a URI 
24100 6d 61 79 20 63 6f 6e 74 61 69 6e 20 70 61 72 61  may contain para
24110 6d 65 74 65 72 73 20 74 68 61 74 20 61 72 65 20  meters that are 
24120 69 6e 74 65 72 70 72 65 74 65 64 0d 0a 2a 2a 20  interpreted..** 
24130 65 69 74 68 65 72 20 62 79 20 53 51 4c 69 74 65  either by SQLite
24140 20 69 74 73 65 6c 66 2c 20 6f 72 20 62 79 20 61   itself, or by a
24150 20 5b 56 46 53 20 7c 20 63 75 73 74 6f 6d 20 56   [VFS | custom V
24160 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
24170 6e 5d 2e 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 69  n]...** SQLite i
24180 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 66 6f  nterprets the fo
24190 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 71 75  llowing three qu
241a0 65 72 79 20 70 61 72 61 6d 65 74 65 72 73 3a 0d  ery parameters:.
241b0 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a  .**..** <ul>..**
241c0 20 20 20 3c 6c 69 3e 20 3c 62 3e 76 66 73 3c 2f     <li> <b>vfs</
241d0 62 3e 3a 20 5e 54 68 65 20 22 76 66 73 22 20 70  b>: ^The "vfs" p
241e0 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 20  arameter may be 
241f0 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20  used to specify 
24200 74 68 65 20 6e 61 6d 65 20 6f 66 0d 0a 2a 2a 20  the name of..** 
24210 20 20 20 20 61 20 56 46 53 20 6f 62 6a 65 63 74      a VFS object
24220 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 74   that provides t
24230 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
24240 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68  tem interface th
24250 61 74 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 20 20  at should..**   
24260 20 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 63    be used to acc
24270 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
24280 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 5e   file on disk. ^
24290 49 66 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 69  If this option i
242a0 73 20 73 65 74 20 74 6f 0d 0a 2a 2a 20 20 20 20  s set to..**    
242b0 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
242c0 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 53   the default VFS
242d0 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 2e   object is used.
242e0 20 5e 53 70 65 63 69 66 79 69 6e 67 20 61 6e 20   ^Specifying an 
242f0 75 6e 6b 6e 6f 77 6e 0d 0a 2a 2a 20 20 20 20 20  unknown..**     
24300 56 46 53 20 69 73 20 61 6e 20 65 72 72 6f 72 2e  VFS is an error.
24310 20 5e 49 66 20 73 71 6c 69 74 65 33 5f 6f 70 65   ^If sqlite3_ope
24320 6e 5f 76 32 28 29 20 69 73 20 75 73 65 64 20 61  n_v2() is used a
24330 6e 64 20 74 68 65 20 76 66 73 20 6f 70 74 69 6f  nd the vfs optio
24340 6e 20 69 73 0d 0a 2a 2a 20 20 20 20 20 70 72 65  n is..**     pre
24350 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 56  sent, then the V
24360 46 53 20 73 70 65 63 69 66 69 65 64 20 62 79 20  FS specified by 
24370 74 68 65 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  the option takes
24380 20 70 72 65 63 65 64 65 6e 63 65 20 6f 76 65 72   precedence over
24390 0d 0a 2a 2a 20 20 20 20 20 74 68 65 20 76 61 6c  ..**     the val
243a0 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
243b0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
243c0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65  r to sqlite3_ope
243d0 6e 5f 76 32 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  n_v2()...**..** 
243e0 20 20 3c 6c 69 3e 20 3c 62 3e 6d 6f 64 65 3c 2f    <li> <b>mode</
243f0 62 3e 3a 20 5e 28 54 68 65 20 6d 6f 64 65 20 70  b>: ^(The mode p
24400 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 20  arameter may be 
24410 73 65 74 20 74 6f 20 65 69 74 68 65 72 20 22 72  set to either "r
24420 6f 22 2c 20 22 72 77 22 20 6f 72 0d 0a 2a 2a 20  o", "rw" or..** 
24430 20 20 20 20 22 72 77 63 22 2e 20 41 74 74 65 6d      "rwc". Attem
24440 70 74 69 6e 67 20 74 6f 20 73 65 74 20 69 74 20  pting to set it 
24450 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 76 61 6c  to any other val
24460 75 65 20 69 73 20 61 6e 20 65 72 72 6f 72 29 5e  ue is an error)^
24470 2e 20 0d 0a 2a 2a 20 20 20 20 20 5e 49 66 20 22  . ..**     ^If "
24480 72 6f 22 20 69 73 20 73 70 65 63 69 66 69 65 64  ro" is specified
24490 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62  , then the datab
244a0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
244b0 72 20 72 65 61 64 2d 6f 6e 6c 79 20 0d 0a 2a 2a  r read-only ..**
244c0 20 20 20 20 20 61 63 63 65 73 73 2c 20 6a 75 73       access, jus
244d0 74 20 61 73 20 69 66 20 74 68 65 20 5b 53 51 4c  t as if the [SQL
244e0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
244f0 59 5d 20 66 6c 61 67 20 68 61 64 20 62 65 65 6e  Y] flag had been
24500 20 73 65 74 20 69 6e 20 74 68 65 20 0d 0a 2a 2a   set in the ..**
24510 20 20 20 20 20 74 68 69 72 64 20 61 72 67 75 6d       third argum
24520 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 70  ent to sqlite3_p
24530 72 65 70 61 72 65 5f 76 32 28 29 2e 20 5e 49 66  repare_v2(). ^If
24540 20 74 68 65 20 6d 6f 64 65 20 6f 70 74 69 6f 6e   the mode option
24550 20 69 73 20 73 65 74 20 74 6f 20 0d 0a 2a 2a 20   is set to ..** 
24560 20 20 20 20 22 72 77 22 2c 20 74 68 65 6e 20 74      "rw", then t
24570 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
24580 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2d 77  pened for read-w
24590 72 69 74 65 20 28 62 75 74 20 6e 6f 74 20 63 72  rite (but not cr
245a0 65 61 74 65 29 20 0d 0a 2a 2a 20 20 20 20 20 61  eate) ..**     a
245b0 63 63 65 73 73 2c 20 61 73 20 69 66 20 53 51 4c  ccess, as if SQL
245c0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
245d0 54 45 20 28 62 75 74 20 6e 6f 74 20 53 51 4c 49  TE (but not SQLI
245e0 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 20  TE_OPEN_CREATE) 
245f0 68 61 64 20 0d 0a 2a 2a 20 20 20 20 20 62 65 65  had ..**     bee
24600 6e 20 73 65 74 2e 20 5e 56 61 6c 75 65 20 22 72  n set. ^Value "r
24610 77 63 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e  wc" is equivalen
24620 74 20 74 6f 20 73 65 74 74 69 6e 67 20 62 6f 74  t to setting bot
24630 68 20 0d 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  h ..**     SQLIT
24640 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
24650 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 50 45 4e   and SQLITE_OPEN
24660 5f 43 52 45 41 54 45 2e 20 5e 49 66 20 73 71 6c  _CREATE. ^If sql
24670 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69  ite3_open_v2() i
24680 73 20 0d 0a 2a 2a 20 20 20 20 20 75 73 65 64 2c  s ..**     used,
24690 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   it is an error 
246a0 74 6f 20 73 70 65 63 69 66 79 20 61 20 76 61 6c  to specify a val
246b0 75 65 20 66 6f 72 20 74 68 65 20 6d 6f 64 65 20  ue for the mode 
246c0 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 20 69  parameter that i
246d0 73 20 0d 0a 2a 2a 20 20 20 20 20 6c 65 73 73 20  s ..**     less 
246e0 72 65 73 74 72 69 63 74 69 76 65 20 74 68 61 6e  restrictive than
246f0 20 74 68 61 74 20 73 70 65 63 69 66 69 65 64 20   that specified 
24700 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 73  by the flags pas
24710 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64  sed as the third
24720 20 0d 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65   ..**     parame
24730 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 3c  ter...**..**   <
24740 6c 69 3e 20 3c 62 3e 63 61 63 68 65 3c 2f 62 3e  li> <b>cache</b>
24750 3a 20 5e 54 68 65 20 63 61 63 68 65 20 70 61 72  : ^The cache par
24760 61 6d 65 74 65 72 20 6d 61 79 20 62 65 20 73 65  ameter may be se
24770 74 20 74 6f 20 65 69 74 68 65 72 20 22 73 68 61  t to either "sha
24780 72 65 64 22 20 6f 72 0d 0a 2a 2a 20 20 20 20 20  red" or..**     
24790 22 70 72 69 76 61 74 65 22 2e 20 5e 53 65 74 74  "private". ^Sett
247a0 69 6e 67 20 69 74 20 74 6f 20 22 73 68 61 72 65  ing it to "share
247b0 64 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  d" is equivalent
247c0 20 74 6f 20 73 65 74 74 69 6e 67 20 74 68 65 0d   to setting the.
247d0 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  .**     SQLITE_O
247e0 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 20  PEN_SHAREDCACHE 
247f0 62 69 74 20 69 6e 20 74 68 65 20 66 6c 61 67 73  bit in the flags
24800 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
24810 20 74 6f 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69   to..**     sqli
24820 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 20 5e  te3_open_v2(). ^
24830 53 65 74 74 69 6e 67 20 74 68 65 20 63 61 63 68  Setting the cach
24840 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 22  e parameter to "
24850 70 72 69 76 61 74 65 22 20 69 73 20 0d 0a 2a 2a  private" is ..**
24860 20 20 20 20 20 65 71 75 69 76 61 6c 65 6e 74 20       equivalent 
24870 74 6f 20 73 65 74 74 69 6e 67 20 74 68 65 20 53  to setting the S
24880 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41  QLITE_OPEN_PRIVA
24890 54 45 43 41 43 48 45 20 62 69 74 2e 0d 0a 2a 2a  TECACHE bit...**
248a0 20 20 20 20 20 5e 49 66 20 73 71 6c 69 74 65 33       ^If sqlite3
248b0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 75 73  _open_v2() is us
248c0 65 64 20 61 6e 64 20 74 68 65 20 22 63 61 63 68  ed and the "cach
248d0 65 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  e" parameter is 
248e0 70 72 65 73 65 6e 74 20 69 6e 0d 0a 2a 2a 20 20  present in..**  
248f0 20 20 20 61 20 55 52 49 20 66 69 6c 65 6e 61 6d     a URI filenam
24900 65 2c 20 69 74 73 20 76 61 6c 75 65 20 6f 76 65  e, its value ove
24910 72 72 69 64 65 73 20 61 6e 79 20 62 65 68 61 76  rrides any behav
24920 69 6f 75 72 20 72 65 71 75 65 73 74 65 64 20 62  iour requested b
24930 79 20 73 65 74 74 69 6e 67 0d 0a 2a 2a 20 20 20  y setting..**   
24940 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52    SQLITE_OPEN_PR
24950 49 56 41 54 45 43 41 43 48 45 20 6f 72 20 53 51  IVATECACHE or SQ
24960 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44  LITE_OPEN_SHARED
24970 43 41 43 48 45 20 66 6c 61 67 2e 0d 0a 2a 2a 20  CACHE flag...** 
24980 3c 2f 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53  </ul>..**..** ^S
24990 70 65 63 69 66 79 69 6e 67 20 61 6e 20 75 6e 6b  pecifying an unk
249a0 6e 6f 77 6e 20 70 61 72 61 6d 65 74 65 72 20 69  nown parameter i
249b0 6e 20 74 68 65 20 71 75 65 72 79 20 63 6f 6d 70  n the query comp
249c0 6f 6e 65 6e 74 20 6f 66 20 61 20 55 52 49 20 69  onent of a URI i
249d0 73 20 6e 6f 74 20 61 6e 0d 0a 2a 2a 20 65 72 72  s not an..** err
249e0 6f 72 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  or.  Future vers
249f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
24a00 69 67 68 74 20 75 6e 64 65 72 73 74 61 6e 64 20  ight understand 
24a10 61 64 64 69 74 69 6f 6e 61 6c 20 71 75 65 72 79  additional query
24a20 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e  ..** parameters.
24a30 20 20 53 65 65 20 22 5b 71 75 65 72 79 20 70 61    See "[query pa
24a40 72 61 6d 65 74 65 72 73 20 77 69 74 68 20 73 70  rameters with sp
24a50 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 20 74 6f  ecial meaning to
24a60 20 53 51 4c 69 74 65 5d 22 20 66 6f 72 0d 0a 2a   SQLite]" for..*
24a70 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  * additional inf
24a80 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  ormation...**..*
24a90 2a 20 5b 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65  * [[URI filename
24aa0 20 65 78 61 6d 70 6c 65 73 5d 5d 20 3c 68 33 3e   examples]] <h3>
24ab0 55 52 49 20 66 69 6c 65 6e 61 6d 65 20 65 78 61  URI filename exa
24ac0 6d 70 6c 65 73 3c 2f 68 33 3e 0d 0a 2a 2a 0d 0a  mples</h3>..**..
24ad0 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72  ** <table border
24ae0 3d 22 31 22 20 61 6c 69 67 6e 3d 63 65 6e 74 65  ="1" align=cente
24af0 72 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 35 3e  r cellpadding=5>
24b00 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 55 52  ..** <tr><th> UR
24b10 49 20 66 69 6c 65 6e 61 6d 65 73 20 3c 74 68 3e  I filenames <th>
24b20 20 52 65 73 75 6c 74 73 0d 0a 2a 2a 20 3c 74 72   Results..** <tr
24b30 3e 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e  ><td> file:data.
24b40 64 62 20 3c 74 64 3e 20 0d 0a 2a 2a 20 20 20 20  db <td> ..**    
24b50 20 20 20 20 20 20 4f 70 65 6e 20 74 68 65 20 66        Open the f
24b60 69 6c 65 20 22 64 61 74 61 2e 64 62 22 20 69 6e  ile "data.db" in
24b70 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 69 72   the current dir
24b80 65 63 74 6f 72 79 2e 0d 0a 2a 2a 20 3c 74 72 3e  ectory...** <tr>
24b90 3c 74 64 3e 20 66 69 6c 65 3a 2f 68 6f 6d 65 2f  <td> file:/home/
24ba0 66 72 65 64 2f 64 61 74 61 2e 64 62 3c 62 72 3e  fred/data.db<br>
24bb0 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69  ..**          fi
24bc0 6c 65 3a 2f 2f 2f 68 6f 6d 65 2f 66 72 65 64 2f  le:///home/fred/
24bd0 64 61 74 61 2e 64 62 20 3c 62 72 3e 20 0d 0a 2a  data.db <br> ..*
24be0 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 3a  *          file:
24bf0 2f 2f 6c 6f 63 61 6c 68 6f 73 74 2f 68 6f 6d 65  //localhost/home
24c00 2f 66 72 65 64 2f 64 61 74 61 2e 64 62 20 3c 62  /fred/data.db <b
24c10 72 3e 20 3c 74 64 3e 20 0d 0a 2a 2a 20 20 20 20  r> <td> ..**    
24c20 20 20 20 20 20 20 4f 70 65 6e 20 74 68 65 20 64        Open the d
24c30 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 2f 68  atabase file "/h
24c40 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61 2e 64 62  ome/fred/data.db
24c50 22 2e 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  "...** <tr><td> 
24c60 66 69 6c 65 3a 2f 2f 64 61 72 6b 73 74 61 72 2f  file://darkstar/
24c70 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61 2e 64  home/fred/data.d
24c80 62 20 3c 74 64 3e 20 0d 0a 2a 2a 20 20 20 20 20  b <td> ..**     
24c90 20 20 20 20 20 41 6e 20 65 72 72 6f 72 2e 20 22       An error. "
24ca0 64 61 72 6b 73 74 61 72 22 20 69 73 20 6e 6f 74  darkstar" is not
24cb0 20 61 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 75   a recognized au
24cc0 74 68 6f 72 69 74 79 2e 0d 0a 2a 2a 20 3c 74 72  thority...** <tr
24cd0 3e 3c 74 64 20 73 74 79 6c 65 3d 22 77 68 69 74  ><td style="whit
24ce0 65 2d 73 70 61 63 65 3a 6e 6f 77 72 61 70 22 3e  e-space:nowrap">
24cf0 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66   ..**          f
24d00 69 6c 65 3a 2f 2f 2f 43 3a 2f 44 6f 63 75 6d 65  ile:///C:/Docume
24d10 6e 74 73 25 32 30 61 6e 64 25 32 30 53 65 74 74  nts%20and%20Sett
24d20 69 6e 67 73 2f 66 72 65 64 2f 44 65 73 6b 74 6f  ings/fred/Deskto
24d30 70 2f 64 61 74 61 2e 64 62 0d 0a 2a 2a 20 20 20  p/data.db..**   
24d40 20 20 3c 74 64 3e 20 57 69 6e 64 6f 77 73 20 6f    <td> Windows o
24d50 6e 6c 79 3a 20 4f 70 65 6e 20 74 68 65 20 66 69  nly: Open the fi
24d60 6c 65 20 22 64 61 74 61 2e 64 62 22 20 6f 6e 20  le "data.db" on 
24d70 66 72 65 64 27 73 20 64 65 73 6b 74 6f 70 20 6f  fred's desktop o
24d80 6e 20 64 72 69 76 65 0d 0a 2a 2a 20 20 20 20 20  n drive..**     
24d90 20 20 20 20 20 43 3a 2e 20 4e 6f 74 65 20 74 68       C:. Note th
24da0 61 74 20 74 68 65 20 25 32 30 20 65 73 63 61 70  at the %20 escap
24db0 69 6e 67 20 69 6e 20 74 68 69 73 20 65 78 61 6d  ing in this exam
24dc0 70 6c 65 20 69 73 20 6e 6f 74 20 73 74 72 69 63  ple is not stric
24dd0 74 6c 79 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20  tly ..**        
24de0 20 20 6e 65 63 65 73 73 61 72 79 20 2d 20 73 70    necessary - sp
24df0 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 63  ace characters c
24e00 61 6e 20 62 65 20 75 73 65 64 20 6c 69 74 65 72  an be used liter
24e10 61 6c 6c 79 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ally..**        
24e20 20 20 69 6e 20 55 52 49 20 66 69 6c 65 6e 61 6d    in URI filenam
24e30 65 73 2e 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  es...** <tr><td>
24e40 20 66 69 6c 65 3a 64 61 74 61 2e 64 62 3f 6d 6f   file:data.db?mo
24e50 64 65 3d 72 6f 26 63 61 63 68 65 3d 70 72 69 76  de=ro&cache=priv
24e60 61 74 65 20 3c 74 64 3e 20 0d 0a 2a 2a 20 20 20  ate <td> ..**   
24e70 20 20 20 20 20 20 20 4f 70 65 6e 20 66 69 6c 65         Open file
24e80 20 22 64 61 74 61 2e 64 62 22 20 69 6e 20 74 68   "data.db" in th
24e90 65 20 63 75 72 72 65 6e 74 20 64 69 72 65 63 74  e current direct
24ea0 6f 72 79 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c  ory for read-onl
24eb0 79 20 61 63 63 65 73 73 2e 0d 0a 2a 2a 20 20 20  y access...**   
24ec0 20 20 20 20 20 20 20 52 65 67 61 72 64 6c 65 73         Regardles
24ed0 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
24ee0 6e 6f 74 20 73 68 61 72 65 64 2d 63 61 63 68 65  not shared-cache
24ef0 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64   mode is enabled
24f00 20 62 79 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20   by..**         
24f10 20 64 65 66 61 75 6c 74 2c 20 75 73 65 20 61 20   default, use a 
24f20 70 72 69 76 61 74 65 20 63 61 63 68 65 2e 0d 0a  private cache...
24f30 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 66 69 6c 65  ** <tr><td> file
24f40 3a 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61  :/home/fred/data
24f50 2e 64 62 3f 76 66 73 3d 75 6e 69 78 2d 6e 6f 6c  .db?vfs=unix-nol
24f60 6f 63 6b 20 3c 74 64 3e 0d 0a 2a 2a 20 20 20 20  ock <td>..**    
24f70 20 20 20 20 20 20 4f 70 65 6e 20 66 69 6c 65 20        Open file 
24f80 22 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61  "/home/fred/data
24f90 2e 64 62 22 2e 20 55 73 65 20 74 68 65 20 73 70  .db". Use the sp
24fa0 65 63 69 61 6c 20 56 46 53 20 22 75 6e 69 78 2d  ecial VFS "unix-
24fb0 6e 6f 6c 6f 63 6b 22 2e 0d 0a 2a 2a 20 3c 74 72  nolock"...** <tr
24fc0 3e 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e  ><td> file:data.
24fd0 64 62 3f 6d 6f 64 65 3d 72 65 61 64 6f 6e 6c 79  db?mode=readonly
24fe0 20 3c 74 64 3e 20 0d 0a 2a 2a 20 20 20 20 20 20   <td> ..**      
24ff0 20 20 20 20 41 6e 20 65 72 72 6f 72 2e 20 22 72      An error. "r
25000 65 61 64 6f 6e 6c 79 22 20 69 73 20 6e 6f 74 20  eadonly" is not 
25010 61 20 76 61 6c 69 64 20 6f 70 74 69 6f 6e 20 66  a valid option f
25020 6f 72 20 74 68 65 20 22 6d 6f 64 65 22 20 70 61  or the "mode" pa
25030 72 61 6d 65 74 65 72 2e 0d 0a 2a 2a 20 3c 2f 74  rameter...** </t
25040 61 62 6c 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 55  able>..**..** ^U
25050 52 49 20 68 65 78 61 64 65 63 69 6d 61 6c 20 65  RI hexadecimal e
25060 73 63 61 70 65 20 73 65 71 75 65 6e 63 65 73 20  scape sequences 
25070 28 25 48 48 29 20 61 72 65 20 73 75 70 70 6f 72  (%HH) are suppor
25080 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 70  ted within the p
25090 61 74 68 20 61 6e 64 0d 0a 2a 2a 20 71 75 65 72  ath and..** quer
250a0 79 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20  y components of 
250b0 61 20 55 52 49 2e 20 41 20 68 65 78 61 64 65 63  a URI. A hexadec
250c0 69 6d 61 6c 20 65 73 63 61 70 65 20 73 65 71 75  imal escape sequ
250d0 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 6f 66  ence consists of
250e0 20 61 0d 0a 2a 2a 20 70 65 72 63 65 6e 74 20 73   a..** percent s
250f0 69 67 6e 20 2d 20 22 25 22 20 2d 20 66 6f 6c 6c  ign - "%" - foll
25100 6f 77 65 64 20 62 79 20 65 78 61 63 74 6c 79 20  owed by exactly 
25110 74 77 6f 20 68 65 78 61 64 65 63 69 6d 61 6c 20  two hexadecimal 
25120 64 69 67 69 74 73 20 0d 0a 2a 2a 20 73 70 65 63  digits ..** spec
25130 69 66 79 69 6e 67 20 61 6e 20 6f 63 74 65 74 20  ifying an octet 
25140 76 61 6c 75 65 2e 20 5e 42 65 66 6f 72 65 20 74  value. ^Before t
25150 68 65 20 70 61 74 68 20 6f 72 20 71 75 65 72 79  he path or query
25160 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20 61   components of a
25170 0d 0a 2a 2a 20 55 52 49 20 66 69 6c 65 6e 61 6d  ..** URI filenam
25180 65 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65  e are interprete
25190 64 2c 20 74 68 65 79 20 61 72 65 20 65 6e 63 6f  d, they are enco
251a0 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 20  ded using UTF-8 
251b0 61 6e 64 20 61 6c 6c 20 0d 0a 2a 2a 20 68 65 78  and all ..** hex
251c0 61 64 65 63 69 6d 61 6c 20 65 73 63 61 70 65 20  adecimal escape 
251d0 73 65 71 75 65 6e 63 65 73 20 72 65 70 6c 61 63  sequences replac
251e0 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 62  ed by a single b
251f0 79 74 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  yte containing t
25200 68 65 0d 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  he..** correspon
25210 64 69 6e 67 20 6f 63 74 65 74 2e 20 49 66 20 74  ding octet. If t
25220 68 69 73 20 70 72 6f 63 65 73 73 20 67 65 6e 65  his process gene
25230 72 61 74 65 73 20 61 6e 20 69 6e 76 61 6c 69 64  rates an invalid
25240 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 2c   UTF-8 encoding,
25250 0d 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 73  ..** the results
25260 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0d   are undefined..
25270 0a 2a 2a 0d 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20  .**..** <b>Note 
25280 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73  to Windows users
25290 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64  :</b>  The encod
252a0 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 65  ing used for the
252b0 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65   filename argume
252c0 6e 74 0d 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65  nt..** of sqlite
252d0 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
252e0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d  ite3_open_v2() m
252f0 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f  ust be UTF-8, no
25300 74 20 77 68 61 74 65 76 65 72 0d 0a 2a 2a 20 63  t whatever..** c
25310 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65  odepage is curre
25320 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46  ntly defined.  F
25330 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e  ilenames contain
25340 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61  ing internationa
25350 6c 0d 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  l..** characters
25360 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74   must be convert
25370 65 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f  ed to UTF-8 prio
25380 72 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65  r to passing the
25390 6d 20 69 6e 74 6f 0d 0a 2a 2a 20 73 71 6c 69 74  m into..** sqlit
253a0 65 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c  e3_open() or sql
253b0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0d  ite3_open_v2()..
253c0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
253d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
253e0 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  (..  const char 
253f0 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20  *filename,   /* 
25400 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  Database filenam
25410 65 20 28 55 54 46 2d 38 29 20 2a 2f 0d 0a 20 20  e (UTF-8) */..  
25420 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20  sqlite3 **ppDb  
25430 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
25440 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65  SQLite db handle
25450 20 2a 2f 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f   */..);..SQLITE_
25460 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
25470 6f 70 65 6e 31 36 28 0d 0a 20 20 63 6f 6e 73 74  open16(..  const
25480 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
25490 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
254a0 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
254b0 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a   */..  sqlite3 *
254c0 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f  *ppDb          /
254d0 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62  * OUT: SQLite db
254e0 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 29 3b 0d 0a   handle */..);..
254f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
25500 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0d  qlite3_open_v2(.
25510 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
25520 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61  ilename,   /* Da
25530 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
25540 28 55 54 46 2d 38 29 20 2a 2f 0d 0a 20 20 73 71  (UTF-8) */..  sq
25550 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20  lite3 **ppDb,   
25560 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
25570 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
25580 2f 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  /..  int flags, 
25590 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
255a0 46 6c 61 67 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73  Flags */..  cons
255b0 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20  t char *zVfs    
255c0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56      /* Name of V
255d0 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65  FS module to use
255e0 20 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..);..../*..*
255f0 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61  * CAPI3REF: Obta
25600 69 6e 20 56 61 6c 75 65 73 20 46 6f 72 20 55 52  in Values For UR
25610 49 20 50 61 72 61 6d 65 74 65 72 73 0d 0a 2a 2a  I Parameters..**
25620 0d 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 75  ..** These are u
25630 74 69 6c 69 74 79 20 72 6f 75 74 69 6e 65 73 2c  tility routines,
25640 20 75 73 65 66 75 6c 20 74 6f 20 56 46 53 20 69   useful to VFS i
25650 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20  mplementations, 
25660 74 68 61 74 20 63 68 65 63 6b 0d 0a 2a 2a 20 74  that check..** t
25670 6f 20 73 65 65 20 69 66 20 61 20 64 61 74 61 62  o see if a datab
25680 61 73 65 20 66 69 6c 65 20 77 61 73 20 61 20 55  ase file was a U
25690 52 49 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 65  RI that containe
256a0 64 20 61 20 73 70 65 63 69 66 69 63 20 71 75 65  d a specific que
256b0 72 79 20 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65  ry ..** paramete
256c0 72 2c 20 61 6e 64 20 69 66 20 73 6f 20 6f 62 74  r, and if so obt
256d0 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 6f  ains the value o
256e0 66 20 74 68 61 74 20 71 75 65 72 79 20 70 61 72  f that query par
256f0 61 6d 65 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ameter...**..** 
25700 49 66 20 46 20 69 73 20 74 68 65 20 64 61 74 61  If F is the data
25710 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 70 6f  base filename po
25720 69 6e 74 65 72 20 70 61 73 73 65 64 20 69 6e 74  inter passed int
25730 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 20 6d 65  o the xOpen() me
25740 74 68 6f 64 20 6f 66 20 0d 0a 2a 2a 20 61 20 56  thod of ..** a V
25750 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
25760 6e 20 77 68 65 6e 20 74 68 65 20 66 6c 61 67 73  n when the flags
25770 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 4f   parameter to xO
25780 70 65 6e 28 29 20 68 61 73 20 6f 6e 65 20 6f 72  pen() has one or
25790 20 0d 0a 2a 2a 20 6d 6f 72 65 20 6f 66 20 74 68   ..** more of th
257a0 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55  e [SQLITE_OPEN_U
257b0 52 49 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f  RI] or [SQLITE_O
257c0 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 20 62 69 74  PEN_MAIN_DB] bit
257d0 73 20 73 65 74 20 61 6e 64 0d 0a 2a 2a 20 50 20  s set and..** P 
257e0 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
257f0 68 65 20 71 75 65 72 79 20 70 61 72 61 6d 65 74  he query paramet
25800 65 72 2c 20 74 68 65 6e 0d 0a 2a 2a 20 73 71 6c  er, then..** sql
25810 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74  ite3_uri_paramet
25820 65 72 28 46 2c 50 29 20 72 65 74 75 72 6e 73 20  er(F,P) returns 
25830 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
25840 20 50 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72   P..** parameter
25850 20 69 66 20 69 74 20 65 78 69 73 74 73 20 6f 72   if it exists or
25860 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
25870 69 66 20 50 20 64 6f 65 73 20 6e 6f 74 20 61 70  if P does not ap
25880 70 65 61 72 20 61 73 20 61 20 0d 0a 2a 2a 20 71  pear as a ..** q
25890 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 20 6f  uery parameter o
258a0 6e 20 46 2e 20 20 49 66 20 50 20 69 73 20 61 20  n F.  If P is a 
258b0 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 20  query parameter 
258c0 6f 66 20 46 0d 0a 2a 2a 20 68 61 73 20 6e 6f 20  of F..** has no 
258d0 65 78 70 6c 69 63 69 74 20 76 61 6c 75 65 2c 20  explicit value, 
258e0 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 75 72 69  then sqlite3_uri
258f0 5f 70 61 72 61 6d 65 74 65 72 28 46 2c 50 29 20  _parameter(F,P) 
25900 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 61 20 70 6f  returns..** a po
25910 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 6d 70 74  inter to an empt
25920 79 20 73 74 72 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  y string...**..*
25930 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 72  * The sqlite3_ur
25940 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 50 2c 42 29  i_boolean(F,P,B)
25950 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73   routine assumes
25960 20 74 68 61 74 20 50 20 69 73 20 61 20 62 6f 6f   that P is a boo
25970 6c 65 61 6e 0d 0a 2a 2a 20 70 61 72 61 6d 65 74  lean..** paramet
25980 65 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74  er and returns t
25990 72 75 65 20 28 31 29 20 6f 72 20 66 61 6c 73 65  rue (1) or false
259a0 20 28 30 29 20 61 63 63 6f 72 64 69 6e 67 20 74   (0) according t
259b0 6f 20 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20  o the value..** 
259c0 6f 66 20 50 2e 20 20 54 68 65 20 76 61 6c 75 65  of P.  The value
259d0 20 6f 66 20 50 20 69 73 20 74 72 75 65 20 69 66   of P is true if
259e0 20 69 74 20 69 73 20 22 79 65 73 22 20 6f 72 20   it is "yes" or 
259f0 22 74 72 75 65 22 20 6f 72 20 22 6f 6e 22 20 6f  "true" or "on" o
25a00 72 20 0d 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a 65 72  r ..** a non-zer
25a10 6f 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 20  o number and is 
25a20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
25a30 20 20 49 66 20 50 20 69 73 20 6e 6f 74 20 61 20    If P is not a 
25a40 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 0d  query parameter.
25a50 0a 2a 2a 20 6f 6e 20 46 20 74 68 65 6e 20 73 71  .** on F then sq
25a60 6c 69 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61  lite3_uri_boolea
25a70 6e 28 46 2c 50 2c 42 29 20 72 65 74 75 72 6e 73  n(F,P,B) returns
25a80 20 28 42 21 3d 30 29 2e 0d 0a 2a 2a 0d 0a 2a 2a   (B!=0)...**..**
25a90 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 72 69   The sqlite3_uri
25aa0 5f 69 6e 74 36 34 28 46 2c 50 2c 44 29 20 72 6f  _int64(F,P,D) ro
25ab0 75 74 69 6e 65 20 63 6f 6e 76 65 72 74 73 20 74  utine converts t
25ac0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 20 69 6e  he value of P in
25ad0 74 6f 20 61 0d 0a 2a 2a 20 36 34 2d 62 69 74 20  to a..** 64-bit 
25ae0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61  signed integer a
25af0 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20  nd returns that 
25b00 69 6e 74 65 67 65 72 2c 20 6f 72 20 44 20 69 66  integer, or D if
25b10 20 50 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a 20   P does not..** 
25b20 65 78 69 73 74 2e 20 20 49 66 20 74 68 65 20 76  exist.  If the v
25b30 61 6c 75 65 20 6f 66 20 50 20 69 73 20 73 6f 6d  alue of P is som
25b40 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
25b50 6e 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68  n an integer, th
25b60 65 6e 0d 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72  en..** zero is r
25b70 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 0d 0a 2a  eturned...** ..*
25b80 2a 20 49 66 20 46 20 69 73 20 61 20 4e 55 4c 4c  * If F is a NULL
25b90 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 73   pointer, then s
25ba0 71 6c 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d  qlite3_uri_param
25bb0 65 74 65 72 28 46 2c 50 29 20 72 65 74 75 72 6e  eter(F,P) return
25bc0 73 20 4e 55 4c 4c 20 61 6e 64 0d 0a 2a 2a 20 73  s NULL and..** s
25bd0 71 6c 69 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65  qlite3_uri_boole
25be0 61 6e 28 46 2c 50 2c 42 29 20 72 65 74 75 72 6e  an(F,P,B) return
25bf0 73 20 42 2e 20 20 49 66 20 46 20 69 73 20 6e 6f  s B.  If F is no
25c00 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  t a NULL pointer
25c10 20 61 6e 64 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20   and..** is not 
25c20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
25c30 70 61 74 68 6e 61 6d 65 20 70 6f 69 6e 74 65 72  pathname pointer
25c40 20 74 68 61 74 20 53 51 4c 69 74 65 20 70 61 73   that SQLite pas
25c50 73 65 64 20 69 6e 74 6f 20 74 68 65 20 78 4f 70  sed into the xOp
25c60 65 6e 0d 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f  en..** VFS metho
25c70 64 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61  d, then the beha
25c80 76 69 6f 72 20 6f 66 20 74 68 69 73 20 72 6f 75  vior of this rou
25c90 74 69 6e 65 20 69 73 20 75 6e 64 65 66 69 6e 65  tine is undefine
25ca0 64 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 0d 0a  d and probably..
25cb0 2a 2a 20 75 6e 64 65 73 69 72 61 62 6c 65 2e 0d  ** undesirable..
25cc0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
25cd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
25ce0 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65  te3_uri_paramete
25cf0 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  r(const char *zF
25d00 69 6c 65 6e 61 6d 65 2c 20 63 6f 6e 73 74 20 63  ilename, const c
25d10 68 61 72 20 2a 7a 50 61 72 61 6d 29 3b 0d 0a 53  har *zParam);..S
25d20 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
25d30 6c 69 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61  lite3_uri_boolea
25d40 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  n(const char *zF
25d50 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ile, const char 
25d60 2a 7a 50 61 72 61 6d 2c 20 69 6e 74 20 62 44 65  *zParam, int bDe
25d70 66 61 75 6c 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  fault);..SQLITE_
25d80 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
25d90 34 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e  4 sqlite3_uri_in
25da0 74 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  t64(const char*,
25db0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 73 71   const char*, sq
25dc0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0d 0a 0d  lite3_int64);...
25dd0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52  .../*..** CAPI3R
25de0 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 65 73 20  EF: Error Codes 
25df0 41 6e 64 20 4d 65 73 73 61 67 65 73 0d 0a 2a 2a  And Messages..**
25e00 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
25e10 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e 74 65  3_errcode() inte
25e20 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
25e30 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 75 6c  e numeric [resul
25e40 74 20 63 6f 64 65 5d 20 6f 72 0d 0a 2a 2a 20 5b  t code] or..** [
25e50 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
25e60 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f  code] for the mo
25e70 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64  st recent failed
25e80 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63   sqlite3_* API c
25e90 61 6c 6c 0d 0a 2a 2a 20 61 73 73 6f 63 69 61 74  all..** associat
25ea0 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 62  ed with a [datab
25eb0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
25ec0 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 20   If a prior API 
25ed0 63 61 6c 6c 20 66 61 69 6c 65 64 0d 0a 2a 2a 20  call failed..** 
25ee0 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63  but the most rec
25ef0 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63  ent API call suc
25f00 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 75  ceeded, the retu
25f10 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0d 0a 2a  rn value from..*
25f20 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
25f30 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64  e() is undefined
25f40 2e 20 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  .  ^The sqlite3_
25f50 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65  extended_errcode
25f60 28 29 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ()..** interface
25f70 20 69 73 20 74 68 65 20 73 61 6d 65 20 65 78 63   is the same exc
25f80 65 70 74 20 74 68 61 74 20 69 74 20 61 6c 77 61  ept that it alwa
25f90 79 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 0d  ys returns the .
25fa0 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65  .** [extended re
25fb0 73 75 6c 74 20 63 6f 64 65 5d 20 65 76 65 6e 20  sult code] even 
25fc0 77 68 65 6e 20 65 78 74 65 6e 64 65 64 20 72 65  when extended re
25fd0 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 0d 0a  sult codes are..
25fe0 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0d 0a 2a 2a  ** disabled...**
25ff0 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
26000 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73  3_errmsg() and s
26010 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
26020 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68  ) return English
26030 2d 6c 61 6e 67 75 61 67 65 0d 0a 2a 2a 20 74 65  -language..** te
26040 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65  xt that describe
26050 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20  s the error, as 
26060 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20  either UTF-8 or 
26070 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76  UTF-16 respectiv
26080 65 6c 79 2e 0d 0a 2a 2a 20 5e 28 4d 65 6d 6f 72  ely...** ^(Memor
26090 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 72  y to hold the er
260a0 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69  ror message stri
260b0 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20 69 6e  ng is managed in
260c0 74 65 72 6e 61 6c 6c 79 2e 0d 0a 2a 2a 20 54 68  ternally...** Th
260d0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f  e application do
260e0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 77  es not need to w
260f0 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 65 69  orry about freei
26100 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e 0d 0a  ng the result...
26110 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20  ** However, the 
26120 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 67  error string mig
26130 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ht be overwritte
26140 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64  n or deallocated
26150 20 62 79 0d 0a 2a 2a 20 73 75 62 73 65 71 75 65   by..** subseque
26160 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65  nt calls to othe
26170 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  r SQLite interfa
26180 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 29 5e 0d  ce functions.)^.
26190 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 65  .**..** When the
261a0 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72   serialized [thr
261b0 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20  eading mode] is 
261c0 69 6e 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74  in use, it might
261d0 20 62 65 20 74 68 65 0d 0a 2a 2a 20 63 61 73 65   be the..** case
261e0 20 74 68 61 74 20 61 20 73 65 63 6f 6e 64 20 65   that a second e
261f0 72 72 6f 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  rror occurs on a
26200 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64   separate thread
26210 20 69 6e 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20   in between..** 
26220 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68 65 20  the time of the 
26230 66 69 72 73 74 20 65 72 72 6f 72 20 61 6e 64 20  first error and 
26240 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 65 73  the call to thes
26250 65 20 69 6e 74 65 72 66 61 63 65 73 2e 0d 0a 2a  e interfaces...*
26260 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70  * When that happ
26270 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  ens, the second 
26280 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 65  error will be re
26290 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 68 65  ported since the
262a0 73 65 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  se..** interface
262b0 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 20  s always report 
262c0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
262d0 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f 69  result.  To avoi
262e0 64 0d 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68  d..** this, each
262f0 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61   thread can obta
26300 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65  in exclusive use
26310 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73   of the [databas
26320 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0d  e connection] D.
26330 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20  .** by invoking 
26340 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
26350 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64  nter]([sqlite3_d
26360 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66  b_mutex](D)) bef
26370 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0d 0a 2a  ore beginning..*
26380 2a 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69  * to use D and i
26390 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33  nvoking [sqlite3
263a0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73  _mutex_leave]([s
263b0 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d  qlite3_db_mutex]
263c0 28 44 29 29 20 61 66 74 65 72 0d 0a 2a 2a 20 61  (D)) after..** a
263d0 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20  ll calls to the 
263e0 69 6e 74 65 72 66 61 63 65 73 20 6c 69 73 74 65  interfaces liste
263f0 64 20 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c  d here are compl
26400 65 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  eted...**..** If
26410 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 66 61   an interface fa
26420 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ils with SQLITE_
26430 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d 65 61  MISUSE, that mea
26440 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  ns the interface
26450 0d 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 64  ..** was invoked
26460 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 20   incorrectly by 
26470 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
26480 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
26490 74 68 65 0d 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  the..** error co
264a0 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d  de and message m
264b0 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65  ay or may not be
264c0 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54   set...*/..SQLIT
264d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
264e0 33 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  3_errcode(sqlite
264f0 33 20 2a 64 62 29 3b 0d 0a 53 51 4c 49 54 45 5f  3 *db);..SQLITE_
26500 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
26510 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65  extended_errcode
26520 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0d 0a  (sqlite3 *db);..
26530 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
26540 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65   char *sqlite3_e
26550 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29 3b  rrmsg(sqlite3*);
26560 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
26570 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
26580 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65  _errmsg16(sqlite
26590 33 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  3*);..../*..** C
265a0 41 50 49 33 52 45 46 3a 20 53 51 4c 20 53 74 61  API3REF: SQL Sta
265b0 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 0d 0a 2a  tement Object..*
265c0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65  * KEYWORDS: {pre
265d0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d  pared statement}
265e0 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   {prepared state
265f0 6d 65 6e 74 73 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 41  ments}..**..** A
26600 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
26610 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  is object repres
26620 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 53 51  ents a single SQ
26630 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2a  L statement...**
26640 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20   This object is 
26650 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e 20  variously known 
26660 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20 73  as a "prepared s
26670 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0d 0a  tatement" or a..
26680 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c  ** "compiled SQL
26690 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73   statement" or s
266a0 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74  imply as a "stat
266b0 65 6d 65 6e 74 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ement"...**..** 
266c0 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74  The life of a st
266d0 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67  atement object g
266e0 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69  oes something li
266f0 6b 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  ke this:..**..**
26700 20 3c 6f 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 43   <ol>..** <li> C
26710 72 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74  reate the object
26720 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
26730 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72  prepare_v2()] or
26740 20 61 20 72 65 6c 61 74 65 64 0d 0a 2a 2a 20 20   a related..**  
26750 20 20 20 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a      function...*
26760 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75  * <li> Bind valu
26770 65 73 20 74 6f 20 5b 68 6f 73 74 20 70 61 72 61  es to [host para
26780 6d 65 74 65 72 73 5d 20 75 73 69 6e 67 20 74 68  meters] using th
26790 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a  e sqlite3_bind_*
267a0 28 29 0d 0a 2a 2a 20 20 20 20 20 20 69 6e 74 65  ()..**      inte
267b0 72 66 61 63 65 73 2e 0d 0a 2a 2a 20 3c 6c 69 3e  rfaces...** <li>
267c0 20 52 75 6e 20 74 68 65 20 53 51 4c 20 62 79 20   Run the SQL by 
267d0 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
267e0 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72 20  _step()] one or 
267f0 6d 6f 72 65 20 74 69 6d 65 73 2e 0d 0a 2a 2a 20  more times...** 
26800 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 20 73  <li> Reset the s
26810 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b  tatement using [
26820 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
26830 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0d 0a 2a   then go back..*
26840 2a 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 32  *      to step 2
26850 2e 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 20  .  Do this zero 
26860 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0d 0a  or more times...
26870 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20  ** <li> Destroy 
26880 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67  the object using
26890 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
268a0 7a 65 28 29 5d 2e 0d 0a 2a 2a 20 3c 2f 6f 6c 3e  ze()]...** </ol>
268b0 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 66 65 72 20 74  ..**..** Refer t
268c0 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  o documentation 
268d0 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d 65  on individual me
268e0 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72 20  thods above for 
268f0 61 64 64 69 74 69 6f 6e 61 6c 0d 0a 2a 2a 20 69  additional..** i
26900 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d  nformation...*/.
26910 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
26920 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c  sqlite3_stmt sql
26930 69 74 65 33 5f 73 74 6d 74 3b 0d 0a 0d 0a 2f 2a  ite3_stmt;..../*
26940 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  ..** CAPI3REF: R
26950 75 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 0d 0a  un-time Limits..
26960 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 69 73 20 69 6e  **..** ^(This in
26970 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 20 74  terface allows t
26980 68 65 20 73 69 7a 65 20 6f 66 20 76 61 72 69 6f  he size of vario
26990 75 73 20 63 6f 6e 73 74 72 75 63 74 73 20 74 6f  us constructs to
269a0 20 62 65 20 6c 69 6d 69 74 65 64 0d 0a 2a 2a 20   be limited..** 
269b0 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  on a connection 
269c0 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61  by connection ba
269d0 73 69 73 2e 20 20 54 68 65 20 66 69 72 73 74 20  sis.  The first 
269e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
269f0 0d 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  ..** [database c
26a00 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 6f 73 65  onnection] whose
26a10 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 62 65 20   limit is to be 
26a20 73 65 74 20 6f 72 20 71 75 65 72 69 65 64 2e 20  set or queried. 
26a30 20 54 68 65 0d 0a 2a 2a 20 73 65 63 6f 6e 64 20   The..** second 
26a40 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65  parameter is one
26a50 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 20 63   of the [limit c
26a60 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 74 20  ategories] that 
26a70 64 65 66 69 6e 65 20 61 0d 0a 2a 2a 20 63 6c 61  define a..** cla
26a80 73 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 73  ss of constructs
26a90 20 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d 69   to be size limi
26aa0 74 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 20  ted.  The third 
26ab0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
26ac0 0d 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66  ..** new limit f
26ad0 6f 72 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63  or that construc
26ae0 74 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66  t.)^..**..** ^If
26af0 20 74 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69   the new limit i
26b00 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  s a negative num
26b10 62 65 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69  ber, the limit i
26b20 73 20 75 6e 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a  s unchanged...**
26b30 20 5e 28 46 6f 72 20 65 61 63 68 20 6c 69 6d 69   ^(For each limi
26b40 74 20 63 61 74 65 67 6f 72 79 20 53 51 4c 49 54  t category SQLIT
26b50 45 5f 4c 49 4d 49 54 5f 3c 69 3e 4e 41 4d 45 3c  E_LIMIT_<i>NAME<
26b60 2f 69 3e 20 74 68 65 72 65 20 69 73 20 61 20 0d  /i> there is a .
26b70 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61  .** [limits | ha
26b80 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0d  rd upper bound].
26b90 0a 2a 2a 20 73 65 74 20 61 74 20 63 6f 6d 70 69  .** set at compi
26ba0 6c 65 2d 74 69 6d 65 20 62 79 20 61 20 43 20 70  le-time by a C p
26bb0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
26bc0 6f 20 63 61 6c 6c 65 64 0d 0a 2a 2a 20 5b 6c 69  o called..** [li
26bd0 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d 41  mits | SQLITE_MA
26be0 58 5f 3c 69 3e 4e 41 4d 45 3c 2f 69 3e 5d 2e 0d  X_<i>NAME</i>]..
26bf0 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54  .** (The "_LIMIT
26c00 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69  _" in the name i
26c10 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d  s changed to "_M
26c20 41 58 5f 22 2e 29 29 5e 0d 0a 2a 2a 20 5e 41 74  AX_".))^..** ^At
26c30 74 65 6d 70 74 73 20 74 6f 20 69 6e 63 72 65 61  tempts to increa
26c40 73 65 20 61 20 6c 69 6d 69 74 20 61 62 6f 76 65  se a limit above
26c50 20 69 74 73 20 68 61 72 64 20 75 70 70 65 72 20   its hard upper 
26c60 62 6f 75 6e 64 20 61 72 65 0d 0a 2a 2a 20 73 69  bound are..** si
26c70 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 74 65 64  lently truncated
26c80 20 74 6f 20 74 68 65 20 68 61 72 64 20 75 70 70   to the hard upp
26c90 65 72 20 62 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a  er bound...**..*
26ca0 2a 20 5e 52 65 67 61 72 64 6c 65 73 73 20 6f 66  * ^Regardless of
26cb0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
26cc0 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 63 68  the limit was ch
26cd0 61 6e 67 65 64 2c 20 74 68 65 20 0d 0a 2a 2a 20  anged, the ..** 
26ce0 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29  [sqlite3_limit()
26cf0 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
26d00 72 6e 73 20 74 68 65 20 70 72 69 6f 72 20 76 61  rns the prior va
26d10 6c 75 65 20 6f 66 20 74 68 65 20 6c 69 6d 69 74  lue of the limit
26d20 2e 0d 0a 2a 2a 20 5e 48 65 6e 63 65 2c 20 74 6f  ...** ^Hence, to
26d30 20 66 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e   find the curren
26d40 74 20 76 61 6c 75 65 20 6f 66 20 61 20 6c 69 6d  t value of a lim
26d50 69 74 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67  it without chang
26d60 69 6e 67 20 69 74 2c 0d 0a 2a 2a 20 73 69 6d 70  ing it,..** simp
26d70 6c 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 69  ly invoke this i
26d80 6e 74 65 72 66 61 63 65 20 77 69 74 68 20 74 68  nterface with th
26d90 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
26da0 72 20 73 65 74 20 74 6f 20 2d 31 2e 0d 0a 2a 2a  r set to -1...**
26db0 0d 0a 2a 2a 20 52 75 6e 2d 74 69 6d 65 20 6c 69  ..** Run-time li
26dc0 6d 69 74 73 20 61 72 65 20 69 6e 74 65 6e 64 65  mits are intende
26dd0 64 20 66 6f 72 20 75 73 65 20 69 6e 20 61 70 70  d for use in app
26de0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d  lications that m
26df0 61 6e 61 67 65 0d 0a 2a 2a 20 62 6f 74 68 20 74  anage..** both t
26e00 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61  heir own interna
26e10 6c 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 61  l database and a
26e20 6c 73 6f 20 64 61 74 61 62 61 73 65 73 20 74 68  lso databases th
26e30 61 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c 6c 65  at are controlle
26e40 64 0d 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73 74  d..** by untrust
26e50 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72  ed external sour
26e60 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65  ces.  An example
26e70 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69 67   application mig
26e80 68 74 20 62 65 20 61 0d 0a 2a 2a 20 77 65 62 20  ht be a..** web 
26e90 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73  browser that has
26ea0 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73   its own databas
26eb0 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68  es for storing h
26ec0 69 73 74 6f 72 79 20 61 6e 64 0d 0a 2a 2a 20 73  istory and..** s
26ed0 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65  eparate database
26ee0 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  s controlled by 
26ef0 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69  JavaScript appli
26f00 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64  cations download
26f10 65 64 0d 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49  ed..** off the I
26f20 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e  nternet.  The in
26f30 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73  ternal databases
26f40 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68   can be given th
26f50 65 0d 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66  e..** large, def
26f60 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61  ault limits.  Da
26f70 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20  tabases managed 
26f80 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72  by external sour
26f90 63 65 73 20 63 61 6e 0d 0a 2a 2a 20 62 65 20 67  ces can..** be g
26fa0 69 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65  iven much smalle
26fb0 72 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65  r limits designe
26fc0 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64  d to prevent a d
26fd0 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65  enial of service
26fe0 0d 0a 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65  ..** attack.  De
26ff0 76 65 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61  velopers might a
27000 6c 73 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20  lso want to use 
27010 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  the [sqlite3_set
27020 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 0d 0a  _authorizer()]..
27030 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ** interface to 
27040 66 75 72 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20  further control 
27050 75 6e 74 72 75 73 74 65 64 20 53 51 4c 2e 20 20  untrusted SQL.  
27060 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
27070 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63 72 65  database..** cre
27080 61 74 65 64 20 62 79 20 61 6e 20 75 6e 74 72 75  ated by an untru
27090 73 74 65 64 20 73 63 72 69 70 74 20 63 61 6e 20  sted script can 
270a0 62 65 20 63 6f 6e 74 61 69 6e 65 64 20 75 73 69  be contained usi
270b0 6e 67 20 74 68 65 0d 0a 2a 2a 20 5b 6d 61 78 5f  ng the..** [max_
270c0 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 41  page_count] [PRA
270d0 47 4d 41 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 65  GMA]...**..** Ne
270e0 77 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 74  w run-time limit
270f0 20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 20   categories may 
27100 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
27110 72 65 20 72 65 6c 65 61 73 65 73 2e 0d 0a 2a 2f  re releases...*/
27120 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
27130 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73   sqlite3_limit(s
27140 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c  qlite3*, int id,
27150 20 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0d 0a 0d   int newVal);...
27160 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
27170 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 6d 69 74  : Run-Time Limit
27180 20 43 61 74 65 67 6f 72 69 65 73 0d 0a 2a 2a 20   Categories..** 
27190 4b 45 59 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74  KEYWORDS: {limit
271a0 20 63 61 74 65 67 6f 72 79 7d 20 7b 2a 6c 69 6d   category} {*lim
271b0 69 74 20 63 61 74 65 67 6f 72 69 65 73 7d 0d 0a  it categories}..
271c0 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  **..** These con
271d0 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 76 61  stants define va
271e0 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 6e 63  rious performanc
271f0 65 20 6c 69 6d 69 74 73 0d 0a 2a 2a 20 74 68 61  e limits..** tha
27200 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64  t can be lowered
27210 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69   at run-time usi
27220 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69  ng [sqlite3_limi
27230 74 28 29 5d 2e 0d 0a 2a 2a 20 54 68 65 20 73 79  t()]...** The sy
27240 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65  nopsis of the me
27250 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
27260 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20  rious limits is 
27270 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0d 0a 2a 2a  shown below...**
27280 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   Additional info
27290 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  rmation is avail
272a0 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20  able at [limits 
272b0 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69  | Limits in SQLi
272c0 74 65 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 64 6c  te]...**..** <dl
272d0 3e 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c  >..** [[SQLITE_L
272e0 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 5d 20 5e 28  IMIT_LENGTH]] ^(
272f0 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
27300 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0d 0a 2a 2a  _LENGTH</dt>..**
27310 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
27320 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74 72   size of any str
27330 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20 74  ing or BLOB or t
27340 61 62 6c 65 20 72 6f 77 2c 20 69 6e 20 62 79 74  able row, in byt
27350 65 73 2e 3c 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a  es.<dd>)^..**..*
27360 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  * [[SQLITE_LIMIT
27370 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 5d 20 5e 28  _SQL_LENGTH]] ^(
27380 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
27390 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  _SQL_LENGTH</dt>
273a0 0d 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  ..** <dd>The max
273b0 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61  imum length of a
273c0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c  n SQL statement,
273d0 20 69 6e 20 62 79 74 65 73 2e 3c 2f 64 64 3e 29   in bytes.</dd>)
273e0 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49  ^..**..** [[SQLI
273f0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
27400 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  ] ^(<dt>SQLITE_L
27410 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e  IMIT_COLUMN</dt>
27420 0d 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  ..** <dd>The max
27430 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
27440 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c  olumns in a tabl
27450 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20  e definition or 
27460 69 6e 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75 6c  in the..** resul
27470 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45  t set of a [SELE
27480 43 54 5d 20 6f 72 20 74 68 65 20 6d 61 78 69 6d  CT] or the maxim
27490 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  um number of col
274a0 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78  umns in an index
274b0 0d 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e 20 4f 52  ..** or in an OR
274c0 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
274d0 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 64 3e 29  BY clause.</dd>)
274e0 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49  ^..**..** [[SQLI
274f0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45  TE_LIMIT_EXPR_DE
27500 50 54 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49  PTH]] ^(<dt>SQLI
27510 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45  TE_LIMIT_EXPR_DE
27520 50 54 48 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64  PTH</dt>..** <dd
27530 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70  >The maximum dep
27540 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 20  th of the parse 
27550 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72  tree on any expr
27560 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 29 5e 0d 0a  ession.</dd>)^..
27570 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  **..** [[SQLITE_
27580 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  LIMIT_COMPOUND_S
27590 45 4c 45 43 54 5d 5d 20 5e 28 3c 64 74 3e 53 51  ELECT]] ^(<dt>SQ
275a0 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
275b0 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0d  UND_SELECT</dt>.
275c0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
275d0 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  mum number of te
275e0 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  rms in a compoun
275f0 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
27600 6e 74 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a  nt.</dd>)^..**..
27610 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  ** [[SQLITE_LIMI
27620 54 5f 56 44 42 45 5f 4f 50 5d 5d 20 5e 28 3c 64  T_VDBE_OP]] ^(<d
27630 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t>SQLITE_LIMIT_V
27640 44 42 45 5f 4f 50 3c 2f 64 74 3e 0d 0a 2a 2a 20  DBE_OP</dt>..** 
27650 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
27660 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  number of instru
27670 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74  ctions in a virt
27680 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67  ual machine prog
27690 72 61 6d 0d 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ram..** used to 
276a0 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c  implement an SQL
276b0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69   statement.  Thi
276c0 73 20 6c 69 6d 69 74 20 69 73 20 6e 6f 74 20 63  s limit is not c
276d0 75 72 72 65 6e 74 6c 79 0d 0a 2a 2a 20 65 6e 66  urrently..** enf
276e0 6f 72 63 65 64 2c 20 74 68 6f 75 67 68 20 74 68  orced, though th
276f0 61 74 20 6d 69 67 68 74 20 62 65 20 61 64 64 65  at might be adde
27700 64 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 72 65  d in some future
27710 20 72 65 6c 65 61 73 65 20 6f 66 0d 0a 2a 2a 20   release of..** 
27720 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 29 5e 0d 0a  SQLite.</dd>)^..
27730 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  **..** [[SQLITE_
27740 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
27750 52 47 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54  RG]] ^(<dt>SQLIT
27760 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e  E_LIMIT_FUNCTION
27770 5f 41 52 47 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64  _ARG</dt>..** <d
27780 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
27790 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
277a0 73 20 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e  s on a function.
277b0 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  </dd>)^..**..** 
277c0 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41  [[SQLITE_LIMIT_A
277d0 54 54 41 43 48 45 44 5d 5d 20 5e 28 3c 64 74 3e  TTACHED]] ^(<dt>
277e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
277f0 41 43 48 45 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  ACHED</dt>..** <
27800 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  dd>The maximum n
27810 75 6d 62 65 72 20 6f 66 20 5b 41 54 54 41 43 48  umber of [ATTACH
27820 20 7c 20 61 74 74 61 63 68 65 64 20 64 61 74 61   | attached data
27830 62 61 73 65 73 5d 2e 29 5e 3c 2f 64 64 3e 0d 0a  bases].)^</dd>..
27840 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  **..** [[SQLITE_
27850 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45  LIMIT_LIKE_PATTE
27860 52 4e 5f 4c 45 4e 47 54 48 5d 5d 0d 0a 2a 2a 20  RN_LENGTH]]..** 
27870 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  ^(<dt>SQLITE_LIM
27880 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
27890 4c 45 4e 47 54 48 3c 2f 64 74 3e 0d 0a 2a 2a 20  LENGTH</dt>..** 
278a0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
278b0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61  length of the pa
278c0 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74  ttern argument t
278d0 6f 20 74 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0d  o the [LIKE] or.
278e0 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65 72 61  .** [GLOB] opera
278f0 74 6f 72 73 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a  tors.</dd>)^..**
27900 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49  ..** [[SQLITE_LI
27910 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
27920 42 45 52 5d 5d 0d 0a 2a 2a 20 5e 28 3c 64 74 3e  BER]]..** ^(<dt>
27930 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
27940 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74  IABLE_NUMBER</dt
27950 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  >..** <dd>The ma
27960 78 69 6d 75 6d 20 69 6e 64 65 78 20 6e 75 6d 62  ximum index numb
27970 65 72 20 6f 66 20 61 6e 79 20 5b 70 61 72 61 6d  er of any [param
27980 65 74 65 72 5d 20 69 6e 20 61 6e 20 53 51 4c 20  eter] in an SQL 
27990 73 74 61 74 65 6d 65 6e 74 2e 29 5e 0d 0a 2a 2a  statement.)^..**
279a0 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49  ..** [[SQLITE_LI
279b0 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
279c0 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45  H]] ^(<dt>SQLITE
279d0 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44  _LIMIT_TRIGGER_D
279e0 45 50 54 48 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64  EPTH</dt>..** <d
279f0 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  d>The maximum de
27a00 70 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e  pth of recursion
27a10 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 3c 2f   for triggers.</
27a20 64 64 3e 29 5e 0d 0a 2a 2a 20 3c 2f 64 6c 3e 0d  dd>)^..** </dl>.
27a30 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  .*/..#define SQL
27a40 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
27a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a60 20 20 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 53      0..#define S
27a70 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f  QLITE_LIMIT_SQL_
27a80 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20  LENGTH          
27a90 20 20 20 20 20 20 31 0d 0a 23 64 65 66 69 6e 65        1..#define
27aa0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f   SQLITE_LIMIT_CO
27ab0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20  LUMN            
27ac0 20 20 20 20 20 20 20 20 32 0d 0a 23 64 65 66 69          2..#defi
27ad0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
27ae0 45 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20  EXPR_DEPTH      
27af0 20 20 20 20 20 20 20 20 20 20 33 0d 0a 23 64 65            3..#de
27b00 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
27b10 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
27b20 54 20 20 20 20 20 20 20 20 20 20 20 34 0d 0a 23  T           4..#
27b30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
27b40 4d 49 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 20  MIT_VDBE_OP     
27b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0d                5.
27b60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
27b70 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
27b80 52 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RG              
27b90 36 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  6..#define SQLIT
27ba0 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44  E_LIMIT_ATTACHED
27bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27bc0 20 20 37 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c    7..#define SQL
27bd0 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
27be0 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 20 20  ATTERN_LENGTH   
27bf0 20 20 20 20 38 0d 0a 23 64 65 66 69 6e 65 20 53      8..#define S
27c00 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
27c10 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 20 20 20  ABLE_NUMBER     
27c20 20 20 20 20 20 20 39 0d 0a 23 64 65 66 69 6e 65        9..#define
27c30 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52   SQLITE_LIMIT_TR
27c40 49 47 47 45 52 5f 44 45 50 54 48 20 20 20 20 20  IGGER_DEPTH     
27c50 20 20 20 20 20 20 20 31 30 0d 0a 0d 0a 2f 2a 0d         10..../*.
27c60 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
27c70 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53  mpiling An SQL S
27c80 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 4b 45 59  tatement..** KEY
27c90 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74  WORDS: {SQL stat
27ca0 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0d  ement compiler}.
27cb0 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 65 78 65 63 75  .**..** To execu
27cc0 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c  te an SQL query,
27cd0 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 62   it must first b
27ce0 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20  e compiled into 
27cf0 61 20 62 79 74 65 2d 63 6f 64 65 0d 0a 2a 2a 20  a byte-code..** 
27d00 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e  program using on
27d10 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  e of these routi
27d20 6e 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  nes...**..** The
27d30 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
27d40 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74   "db", is a [dat
27d50 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
27d60 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  ] obtained from 
27d70 61 0d 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63  a..** prior succ
27d80 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
27d90 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
27da0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
27db0 32 28 29 5d 20 6f 72 0d 0a 2a 2a 20 5b 73 71 6c  2()] or..** [sql
27dc0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20  ite3_open16()]. 
27dd0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
27de0 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f  nnection must no
27df0 74 20 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73  t have been clos
27e00 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
27e10 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
27e20 20 22 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20   "zSql", is the 
27e30 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20  statement to be 
27e40 63 6f 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65  compiled, encode
27e50 64 0d 0a 2a 2a 20 61 73 20 65 69 74 68 65 72 20  d..** as either 
27e60 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e  UTF-8 or UTF-16.
27e70 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72    The sqlite3_pr
27e80 65 70 61 72 65 28 29 20 61 6e 64 20 73 71 6c 69  epare() and sqli
27e90 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
27ea0 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20  ..** interfaces 
27eb0 75 73 65 20 55 54 46 2d 38 2c 20 61 6e 64 20 73  use UTF-8, and s
27ec0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
27ed0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
27ee0 72 65 70 61 72 65 31 36 5f 76 32 28 29 0d 0a 2a  repare16_v2()..*
27ef0 2a 20 75 73 65 20 55 54 46 2d 31 36 2e 0d 0a 2a  * use UTF-16...*
27f00 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 6e 42  *..** ^If the nB
27f10 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  yte argument is 
27f20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
27f30 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 61  then zSql is rea
27f40 64 20 75 70 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  d up to the..** 
27f50 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
27f60 6e 61 74 6f 72 2e 20 5e 49 66 20 6e 42 79 74 65  nator. ^If nByte
27f70 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
27f80 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65  , then it is the
27f90 20 6d 61 78 69 6d 75 6d 0d 0a 2a 2a 20 6e 75 6d   maximum..** num
27fa0 62 65 72 20 6f 66 20 20 62 79 74 65 73 20 72 65  ber of  bytes re
27fb0 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 5e  ad from zSql.  ^
27fc0 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f  When nByte is no
27fd0 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0d  n-negative, the.
27fe0 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67 20  .** zSql string 
27ff0 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 20 74  ends at either t
28000 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 27 20  he first '\000' 
28010 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 61 72  or '\u0000' char
28020 61 63 74 65 72 20 6f 72 0d 0a 2a 2a 20 74 68 65  acter or..** the
28030 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20   nByte-th byte, 
28040 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
28050 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 61  first. If the ca
28060 6c 6c 65 72 20 6b 6e 6f 77 73 0d 0a 2a 2a 20 74  ller knows..** t
28070 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64  hat the supplied
28080 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74   string is nul-t
28090 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20  erminated, then 
280a0 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c  there is a small
280b0 0d 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  ..** performance
280c0 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65   advantage to be
280d0 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69   gained by passi
280e0 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61  ng an nByte para
280f0 6d 65 74 65 72 20 74 68 61 74 0d 0a 2a 2a 20 69  meter that..** i
28100 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e  s equal to the n
28110 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
28120 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  n the input stri
28130 6e 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c  ng <i>including<
28140 2f 69 3e 0d 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d  /i>..** the nul-
28150 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73  terminator bytes
28160 20 61 73 20 74 68 69 73 20 73 61 76 65 73 20 53   as this saves S
28170 51 4c 69 74 65 20 66 72 6f 6d 20 68 61 76 69 6e  QLite from havin
28180 67 20 74 6f 0d 0a 2a 2a 20 6d 61 6b 65 20 61 20  g to..** make a 
28190 63 6f 70 79 20 6f 66 20 74 68 65 20 69 6e 70 75  copy of the inpu
281a0 74 20 73 74 72 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  t string...**..*
281b0 2a 20 5e 49 66 20 70 7a 54 61 69 6c 20 69 73 20  * ^If pzTail is 
281c0 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70  not NULL then *p
281d0 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f  zTail is made to
281e0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69   point to the fi
281f0 72 73 74 20 62 79 74 65 0d 0a 2a 2a 20 70 61 73  rst byte..** pas
28200 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
28210 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65   first SQL state
28220 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54  ment in zSql.  T
28230 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
28240 6c 79 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74  ly..** compile t
28250 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65  he first stateme
28260 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a  nt in zSql, so *
28270 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70  pzTail is left p
28280 6f 69 6e 74 69 6e 67 20 74 6f 0d 0a 2a 2a 20 77  ointing to..** w
28290 68 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f  hat remains unco
282a0 6d 70 69 6c 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  mpiled...**..** 
282b0 5e 2a 70 70 53 74 6d 74 20 69 73 20 6c 65 66 74  ^*ppStmt is left
282c0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 63   pointing to a c
282d0 6f 6d 70 69 6c 65 64 20 5b 70 72 65 70 61 72 65  ompiled [prepare
282e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61  d statement] tha
282f0 74 20 63 61 6e 20 62 65 0d 0a 2a 2a 20 65 78 65  t can be..** exe
28300 63 75 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  cuted using [sql
28310 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 20 20 5e  ite3_step()].  ^
28320 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  If there is an e
28330 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 20 69 73  rror, *ppStmt is
28340 20 73 65 74 0d 0a 2a 2a 20 74 6f 20 4e 55 4c 4c   set..** to NULL
28350 2e 20 20 5e 49 66 20 74 68 65 20 69 6e 70 75 74  .  ^If the input
28360 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e   text contains n
28370 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e  o SQL (if the in
28380 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 79 0d  put is an empty.
28390 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 20  .** string or a 
283a0 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70  comment) then *p
283b0 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20  pStmt is set to 
283c0 4e 55 4c 4c 2e 0d 0a 2a 2a 20 54 68 65 20 63 61  NULL...** The ca
283d0 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
283e0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
283f0 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  or deleting the 
28400 63 6f 6d 70 69 6c 65 64 0d 0a 2a 2a 20 53 51 4c  compiled..** SQL
28410 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67   statement using
28420 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
28430 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 68  ze()] after it h
28440 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  as finished with
28450 20 69 74 2e 0d 0a 2a 2a 20 70 70 53 74 6d 74 20   it...** ppStmt 
28460 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e  may not be NULL.
28470 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 4f 6e 20 73 75 63  ..**..** ^On suc
28480 63 65 73 73 2c 20 74 68 65 20 73 71 6c 69 74 65  cess, the sqlite
28490 33 5f 70 72 65 70 61 72 65 28 29 20 66 61 6d 69  3_prepare() fami
284a0 6c 79 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 72  ly of routines r
284b0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b  eturn [SQLITE_OK
284c0 5d 3b 0d 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  ];..** otherwise
284d0 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
284e0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   is returned...*
284f0 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  *..** The sqlite
28500 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 61  3_prepare_v2() a
28510 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
28520 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72 66  re16_v2() interf
28530 61 63 65 73 20 61 72 65 0d 0a 2a 2a 20 72 65 63  aces are..** rec
28540 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c  ommended for all
28550 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54   new programs. T
28560 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74  he two older int
28570 65 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61  erfaces are reta
28580 69 6e 65 64 0d 0a 2a 2a 20 66 6f 72 20 62 61 63  ined..** for bac
28590 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
285a0 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69 72 20  lity, but their 
285b0 75 73 65 20 69 73 20 64 69 73 63 6f 75 72 61 67  use is discourag
285c0 65 64 2e 0d 0a 2a 2a 20 5e 49 6e 20 74 68 65 20  ed...** ^In the 
285d0 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c  "v2" interfaces,
285e0 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
285f0 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 74 68 61 74  atement..** that
28600 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68   is returned (th
28610 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d  e [sqlite3_stmt]
28620 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e   object) contain
28630 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0d  s a copy of the.
28640 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  .** original SQL
28650 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73   text. This caus
28660 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  es the [sqlite3_
28670 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63  step()] interfac
28680 65 20 74 6f 0d 0a 2a 2a 20 62 65 68 61 76 65 20  e to..** behave 
28690 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74  differently in t
286a0 68 72 65 65 20 77 61 79 73 3a 0d 0a 2a 2a 0d 0a  hree ways:..**..
286b0 2a 2a 20 3c 6f 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e  ** <ol>..** <li>
286c0 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 64 61 74  ..** ^If the dat
286d0 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61  abase schema cha
286e0 6e 67 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66  nges, instead of
286f0 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49   returning [SQLI
28700 54 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74  TE_SCHEMA] as it
28710 0d 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 64  ..** always used
28720 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33   to do, [sqlite3
28730 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 75  _step()] will au
28740 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
28750 6d 70 69 6c 65 20 74 68 65 20 53 51 4c 0d 0a 2a  mpile the SQL..*
28760 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  * statement and 
28770 74 72 79 20 74 6f 20 72 75 6e 20 69 74 20 61 67  try to run it ag
28780 61 69 6e 2e 0d 0a 2a 2a 20 3c 2f 6c 69 3e 0d 0a  ain...** </li>..
28790 2a 2a 0d 0a 2a 2a 20 3c 6c 69 3e 0d 0a 2a 2a 20  **..** <li>..** 
287a0 5e 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f  ^When an error o
287b0 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f  ccurs, [sqlite3_
287c0 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74  step()] will ret
287d0 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64  urn one of the d
287e0 65 74 61 69 6c 65 64 0d 0a 2a 2a 20 5b 65 72 72  etailed..** [err
287f0 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78  or codes] or [ex
28800 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
28810 65 73 5d 2e 20 20 5e 54 68 65 20 6c 65 67 61 63  es].  ^The legac
28820 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 20 74  y behavior was t
28830 68 61 74 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  hat..** [sqlite3
28840 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f  _step()] would o
28850 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e  nly return a gen
28860 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52  eric [SQLITE_ERR
28870 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0d  OR] result code.
28880 0a 2a 2a 20 61 6e 64 20 74 68 65 20 61 70 70 6c  .** and the appl
28890 69 63 61 74 69 6f 6e 20 77 6f 75 6c 64 20 68 61  ication would ha
288a0 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63  ve to make a sec
288b0 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ond call to [sql
288c0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 0d 0a 2a  ite3_reset()]..*
288d0 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69  * in order to fi
288e0 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  nd the underlyin
288f0 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 20 70  g cause of the p
28900 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65  roblem. With the
28910 20 22 76 32 22 20 70 72 65 70 61 72 65 0d 0a 2a   "v2" prepare..*
28920 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68  * interfaces, th
28930 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61  e underlying rea
28940 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f  son for the erro
28950 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d  r is returned im
28960 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 2a 2a 20 3c  mediately...** <
28970 2f 6c 69 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 6c 69  /li>..**..** <li
28980 3e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 73 70  >..** ^If the sp
28990 65 63 69 66 69 63 20 76 61 6c 75 65 20 62 6f 75  ecific value bou
289a0 6e 64 20 74 6f 20 5b 70 61 72 61 6d 65 74 65 72  nd to [parameter
289b0 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65   | host paramete
289c0 72 5d 20 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 57  r] in the ..** W
289d0 48 45 52 45 20 63 6c 61 75 73 65 20 6d 69 67 68  HERE clause migh
289e0 74 20 69 6e 66 6c 75 65 6e 63 65 20 74 68 65 20  t influence the 
289f0 63 68 6f 69 63 65 20 6f 66 20 71 75 65 72 79 20  choice of query 
28a00 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 74 65  plan for a state
28a10 6d 65 6e 74 2c 0d 0a 2a 2a 20 74 68 65 6e 20 74  ment,..** then t
28a20 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c  he statement wil
28a30 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
28a40 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 20 61  ly recompiled, a
28a50 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62  s if there had b
28a60 65 65 6e 20 0d 0a 2a 2a 20 61 20 73 63 68 65 6d  een ..** a schem
28a70 61 20 63 68 61 6e 67 65 2c 20 6f 6e 20 74 68 65  a change, on the
28a80 20 66 69 72 73 74 20 20 5b 73 71 6c 69 74 65 33   first  [sqlite3
28a90 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f  _step()] call fo
28aa0 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e  llowing any chan
28ab0 67 65 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73  ge..** to the [s
28ac0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
28ad0 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20   | bindings] of 
28ae0 74 68 61 74 20 5b 70 61 72 61 6d 65 74 65 72 5d  that [parameter]
28af0 2e 20 0d 0a 2a 2a 20 5e 54 68 65 20 73 70 65 63  . ..** ^The spec
28b00 69 66 69 63 20 76 61 6c 75 65 20 6f 66 20 57 48  ific value of WH
28b10 45 52 45 2d 63 6c 61 75 73 65 20 5b 70 61 72 61  ERE-clause [para
28b20 6d 65 74 65 72 5d 20 6d 69 67 68 74 20 69 6e 66  meter] might inf
28b30 6c 75 65 6e 63 65 20 74 68 65 20 0d 0a 2a 2a 20  luence the ..** 
28b40 63 68 6f 69 63 65 20 6f 66 20 71 75 65 72 79 20  choice of query 
28b50 70 6c 61 6e 20 69 66 20 74 68 65 20 70 61 72 61  plan if the para
28b60 6d 65 74 65 72 20 69 73 20 74 68 65 20 6c 65 66  meter is the lef
28b70 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61  t-hand side of a
28b80 20 5b 4c 49 4b 45 5d 0d 0a 2a 2a 20 6f 72 20 5b   [LIKE]..** or [
28b90 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 20 6f  GLOB] operator o
28ba0 72 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74  r if the paramet
28bb0 65 72 20 69 73 20 63 6f 6d 70 61 72 65 64 20 74  er is compared t
28bc0 6f 20 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c  o an indexed col
28bd0 75 6d 6e 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20  umn..** and the 
28be0 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53  [SQLITE_ENABLE_S
28bf0 54 41 54 33 5d 20 63 6f 6d 70 69 6c 65 2d 74 69  TAT3] compile-ti
28c00 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61  me option is ena
28c10 62 6c 65 64 2e 0d 0a 2a 2a 20 74 68 65 20 0d 0a  bled...** the ..
28c20 2a 2a 20 3c 2f 6c 69 3e 0d 0a 2a 2a 20 3c 2f 6f  ** </li>..** </o
28c30 6c 3e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  l>..*/..SQLITE_A
28c40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
28c50 72 65 70 61 72 65 28 0d 0a 20 20 73 71 6c 69 74  repare(..  sqlit
28c60 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
28c70 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
28c80 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73  andle */..  cons
28c90 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
28ca0 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
28cb0 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f  ment, UTF-8 enco
28cc0 64 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42  ded */..  int nB
28cd0 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
28ce0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
28cf0 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
28d00 79 74 65 73 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69  ytes. */..  sqli
28d10 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
28d20 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
28d30 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0d  ement handle */.
28d40 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
28d50 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55  pzTail     /* OU
28d60 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e  T: Pointer to un
28d70 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  used portion of 
28d80 7a 53 71 6c 20 2a 2f 0d 0a 29 3b 0d 0a 53 51 4c  zSql */..);..SQL
28d90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
28da0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0d  te3_prepare_v2(.
28db0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
28dc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
28dd0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
28de0 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
28df0 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
28e00 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
28e10 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0d 0a  F-8 encoded */..
28e20 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
28e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
28e40 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
28e50 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
28e60 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
28e70 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
28e80 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
28e90 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ndle */..  const
28ea0 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20   char **pzTail  
28eb0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
28ec0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
28ed0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0d  tion of zSql */.
28ee0 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .);..SQLITE_API 
28ef0 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
28f00 61 72 65 31 36 28 0d 0a 20 20 73 71 6c 69 74 65  are16(..  sqlite
28f10 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
28f20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
28f30 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ndle */..  const
28f40 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
28f50 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
28f60 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f  ent, UTF-16 enco
28f70 64 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42  ded */..  int nB
28f80 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
28f90 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
28fa0 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
28fb0 79 74 65 73 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69  ytes. */..  sqli
28fc0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
28fd0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
28fe0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0d  ement handle */.
28ff0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
29000 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55  pzTail     /* OU
29010 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e  T: Pointer to un
29020 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  used portion of 
29030 7a 53 71 6c 20 2a 2f 0d 0a 29 3b 0d 0a 53 51 4c  zSql */..);..SQL
29040 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29050 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
29060 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  (..  sqlite3 *db
29070 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
29080 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
29090 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  */..  const void
290a0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
290b0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
290c0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
290d0 2f 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  /..  int nByte, 
290e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
290f0 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  Maximum length o
29100 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
29110 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73   */..  sqlite3_s
29120 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f  tmt **ppStmt,  /
29130 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
29140 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f   handle */..  co
29150 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
29160 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
29170 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
29180 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
29190 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..);..../*..**
291a0 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
291b0 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 20  eving Statement 
291c0 53 51 4c 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69  SQL..**..** ^Thi
291d0 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  s interface can 
291e0 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  be used to retri
291f0 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79  eve a saved copy
29200 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
29210 0d 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73  ..** SQL text us
29220 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b  ed to create a [
29230 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
29240 6e 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 74  nt] if that stat
29250 65 6d 65 6e 74 20 77 61 73 0d 0a 2a 2a 20 63 6f  ement was..** co
29260 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74  mpiled using eit
29270 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  her [sqlite3_pre
29280 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73  pare_v2()] or [s
29290 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
292a0 5f 76 32 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c  _v2()]...*/..SQL
292b0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
292c0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
292d0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
292e0 74 6d 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  tmt);..../*..** 
292f0 43 41 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d  CAPI3REF: Determ
29300 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20 53 74  ine If An SQL St
29310 61 74 65 6d 65 6e 74 20 57 72 69 74 65 73 20 54  atement Writes T
29320 68 65 20 44 61 74 61 62 61 73 65 0d 0a 2a 2a 0d  he Database..**.
29330 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
29340 5f 73 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28 58  _stmt_readonly(X
29350 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
29360 72 6e 73 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65  rns true (non-ze
29370 72 6f 29 20 69 66 0d 0a 2a 2a 20 61 6e 64 20 6f  ro) if..** and o
29380 6e 6c 79 20 69 66 20 74 68 65 20 5b 70 72 65 70  nly if the [prep
29390 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
293a0 58 20 6d 61 6b 65 73 20 6e 6f 20 64 69 72 65 63  X makes no direc
293b0 74 20 63 68 61 6e 67 65 73 20 74 6f 0d 0a 2a 2a  t changes to..**
293c0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
293d0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
293e0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20  e...**..** Note 
293f0 74 68 61 74 20 5b 61 70 70 6c 69 63 61 74 69 6f  that [applicatio
29400 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
29410 6e 63 74 69 6f 6e 73 5d 20 6f 72 0d 0a 2a 2a 20  nctions] or..** 
29420 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 5d  [virtual tables]
29430 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68   might change th
29440 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 69 72  e database indir
29450 65 63 74 6c 79 20 61 73 20 61 20 73 69 64 65 20  ectly as a side 
29460 65 66 66 65 63 74 2e 20 20 0d 0a 2a 2a 20 5e 28  effect.  ..** ^(
29470 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
29480 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  an application d
29490 65 66 69 6e 65 73 20 61 20 66 75 6e 63 74 69 6f  efines a functio
294a0 6e 20 22 65 76 61 6c 28 29 22 20 74 68 61 74 20  n "eval()" that 
294b0 0d 0a 2a 2a 20 63 61 6c 6c 73 20 5b 73 71 6c 69  ..** calls [sqli
294c0 74 65 33 5f 65 78 65 63 28 29 5d 2c 20 74 68 65  te3_exec()], the
294d0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
294e0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 6f  SQL statement wo
294f0 75 6c 64 0d 0a 2a 2a 20 63 68 61 6e 67 65 20 74  uld..** change t
29500 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
29510 20 74 68 72 6f 75 67 68 20 73 69 64 65 2d 65 66   through side-ef
29520 66 65 63 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c  fects:..**..** <
29530 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
29540 0d 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 65  ..**    SELECT e
29550 76 61 6c 28 27 44 45 4c 45 54 45 20 46 52 4f 4d  val('DELETE FROM
29560 20 74 31 27 29 20 46 52 4f 4d 20 74 32 3b 0d 0a   t1') FROM t2;..
29570 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
29580 71 75 6f 74 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 42  quote>..**..** B
29590 75 74 20 62 65 63 61 75 73 65 20 74 68 65 20 5b  ut because the [
295a0 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e  SELECT] statemen
295b0 74 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67  t does not chang
295c0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
295d0 69 6c 65 0d 0a 2a 2a 20 64 69 72 65 63 74 6c 79  ile..** directly
295e0 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 72  , sqlite3_stmt_r
295f0 65 61 64 6f 6e 6c 79 28 29 20 77 6f 75 6c 64 20  eadonly() would 
29600 73 74 69 6c 6c 20 72 65 74 75 72 6e 20 74 72 75  still return tru
29610 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 72  e.)^..**..** ^Tr
29620 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 72 6f  ansaction contro
29630 6c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 75 63  l statements suc
29640 68 20 61 73 20 5b 42 45 47 49 4e 5d 2c 20 5b 43  h as [BEGIN], [C
29650 4f 4d 4d 49 54 5d 2c 20 5b 52 4f 4c 4c 42 41 43  OMMIT], [ROLLBAC
29660 4b 5d 2c 0d 0a 2a 2a 20 5b 53 41 56 45 50 4f 49  K],..** [SAVEPOI
29670 4e 54 5d 2c 20 61 6e 64 20 5b 52 45 4c 45 41 53  NT], and [RELEAS
29680 45 5d 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  E] cause sqlite3
29690 5f 73 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28 29  _stmt_readonly()
296a0 20 74 6f 20 72 65 74 75 72 6e 20 74 72 75 65 2c   to return true,
296b0 0d 0a 2a 2a 20 73 69 6e 63 65 20 74 68 65 20 73  ..** since the s
296c0 74 61 74 65 6d 65 6e 74 73 20 74 68 65 6d 73 65  tatements themse
296d0 6c 76 65 73 20 64 6f 20 6e 6f 74 20 61 63 74 75  lves do not actu
296e0 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20  ally modify the 
296f0 64 61 74 61 62 61 73 65 20 62 75 74 0d 0a 2a 2a  database but..**
29700 20 72 61 74 68 65 72 20 74 68 65 79 20 63 6f 6e   rather they con
29710 74 72 6f 6c 20 74 68 65 20 74 69 6d 69 6e 67 20  trol the timing 
29720 6f 66 20 77 68 65 6e 20 6f 74 68 65 72 20 73 74  of when other st
29730 61 74 65 6d 65 6e 74 73 20 6d 6f 64 69 66 79 20  atements modify 
29740 74 68 65 20 0d 0a 2a 2a 20 64 61 74 61 62 61 73  the ..** databas
29750 65 2e 20 20 5e 54 68 65 20 5b 41 54 54 41 43 48  e.  ^The [ATTACH
29760 5d 20 61 6e 64 20 5b 44 45 54 41 43 48 5d 20 73  ] and [DETACH] s
29770 74 61 74 65 6d 65 6e 74 73 20 61 6c 73 6f 20 63  tatements also c
29780 61 75 73 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  ause..** sqlite3
29790 5f 73 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28 29  _stmt_readonly()
297a0 20 74 6f 20 72 65 74 75 72 6e 20 74 72 75 65 20   to return true 
297b0 73 69 6e 63 65 2c 20 77 68 69 6c 65 20 74 68 6f  since, while tho
297c0 73 65 20 73 74 61 74 65 6d 65 6e 74 73 0d 0a 2a  se statements..*
297d0 2a 20 63 68 61 6e 67 65 20 74 68 65 20 63 6f 6e  * change the con
297e0 66 69 67 75 72 61 74 69 6f 6e 20 6f 66 20 61 20  figuration of a 
297f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
29800 69 6f 6e 2c 20 74 68 65 79 20 64 6f 20 6e 6f 74  ion, they do not
29810 20 6d 61 6b 65 20 0d 0a 2a 2a 20 63 68 61 6e 67   make ..** chang
29820 65 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  es to the conten
29830 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  t of the databas
29840 65 20 66 69 6c 65 73 20 6f 6e 20 64 69 73 6b 2e  e files on disk.
29850 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
29860 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d   int sqlite3_stm
29870 74 5f 72 65 61 64 6f 6e 6c 79 28 73 71 6c 69 74  t_readonly(sqlit
29880 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
29890 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
298a0 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49  REF: Determine I
298b0 66 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  f A Prepared Sta
298c0 74 65 6d 65 6e 74 20 48 61 73 20 42 65 65 6e 20  tement Has Been 
298d0 52 65 73 65 74 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  Reset..**..** ^T
298e0 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  he sqlite3_stmt_
298f0 62 75 73 79 28 53 29 20 69 6e 74 65 72 66 61 63  busy(S) interfac
29900 65 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 28  e returns true (
29910 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65  non-zero) if the
29920 0d 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  ..** [prepared s
29930 74 61 74 65 6d 65 6e 74 5d 20 53 20 68 61 73 20  tatement] S has 
29940 62 65 65 6e 20 73 74 65 70 70 65 64 20 61 74 20  been stepped at 
29950 6c 65 61 73 74 20 6f 6e 63 65 20 75 73 69 6e 67  least once using
29960 20 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73   ..** [sqlite3_s
29970 74 65 70 28 53 29 5d 20 62 75 74 20 68 61 73 20  tep(S)] but has 
29980 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c  not run to compl
29990 65 74 69 6f 6e 20 61 6e 64 2f 6f 72 20 68 61 73  etion and/or has
299a0 20 6e 6f 74 20 0d 0a 2a 2a 20 62 65 65 6e 20 72   not ..** been r
299b0 65 73 65 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  eset using [sqli
299c0 74 65 33 5f 72 65 73 65 74 28 53 29 5d 2e 20 20  te3_reset(S)].  
299d0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d  ^The sqlite3_stm
299e0 74 5f 62 75 73 79 28 53 29 0d 0a 2a 2a 20 69 6e  t_busy(S)..** in
299f0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
29a00 66 61 6c 73 65 20 69 66 20 53 20 69 73 20 61 20  false if S is a 
29a10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49  NULL pointer.  I
29a20 66 20 53 20 69 73 20 6e 6f 74 20 61 20 0d 0a 2a  f S is not a ..*
29a30 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61  * NULL pointer a
29a40 6e 64 20 69 73 20 6e 6f 74 20 61 20 70 6f 69 6e  nd is not a poin
29a50 74 65 72 20 74 6f 20 61 20 76 61 6c 69 64 20 5b  ter to a valid [
29a60 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
29a70 6e 74 5d 0d 0a 2a 2a 20 6f 62 6a 65 63 74 2c 20  nt]..** object, 
29a80 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
29a90 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61  r is undefined a
29aa0 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65  nd probably unde
29ab0 73 69 72 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  sirable...**..**
29ac0 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
29ad0 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 63  can be used in c
29ae0 6f 6d 62 69 6e 61 74 69 6f 6e 20 5b 73 71 6c 69  ombination [sqli
29af0 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d  te3_next_stmt()]
29b00 0d 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61  ..** to locate a
29b10 6c 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ll prepared stat
29b20 65 6d 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65  ements associate
29b30 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
29b40 65 20 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  e ..** connectio
29b50 6e 20 74 68 61 74 20 61 72 65 20 69 6e 20 6e 65  n that are in ne
29b60 65 64 20 6f 66 20 62 65 69 6e 67 20 72 65 73 65  ed of being rese
29b70 74 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  t.  This can be 
29b80 75 73 65 64 2c 0d 0a 2a 2a 20 66 6f 72 20 65 78  used,..** for ex
29b90 61 6d 70 6c 65 2c 20 69 6e 20 64 69 61 67 6e 6f  ample, in diagno
29ba0 73 74 69 63 20 72 6f 75 74 69 6e 65 73 20 74 6f  stic routines to
29bb0 20 73 65 61 72 63 68 20 66 6f 72 20 70 72 65 70   search for prep
29bc0 61 72 65 64 20 0d 0a 2a 2a 20 73 74 61 74 65 6d  ared ..** statem
29bd0 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 68 6f  ents that are ho
29be0 6c 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  lding a transact
29bf0 69 6f 6e 20 6f 70 65 6e 2e 0d 0a 2a 2f 0d 0a 53  ion open...*/..S
29c00 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
29c10 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 79 28  lite3_stmt_busy(
29c20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d  sqlite3_stmt*);.
29c30 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52  .../*..** CAPI3R
29c40 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20  EF: Dynamically 
29c50 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a 65  Typed Value Obje
29c60 63 74 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  ct..** KEYWORDS:
29c70 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   {protected sqli
29c80 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 72  te3_value} {unpr
29c90 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
29ca0 76 61 6c 75 65 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 53  value}..**..** S
29cb0 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73  QLite uses the s
29cc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
29cd0 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74  ect to represent
29ce0 20 61 6c 6c 20 76 61 6c 75 65 73 0d 0a 2a 2a 20   all values..** 
29cf0 74 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72  that can be stor
29d00 65 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  ed in a database
29d10 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75   table. SQLite u
29d20 73 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69  ses dynamic typi
29d30 6e 67 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76  ng..** for the v
29d40 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e  alues it stores.
29d50 20 20 5e 56 61 6c 75 65 73 20 73 74 6f 72 65 64    ^Values stored
29d60 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   in sqlite3_valu
29d70 65 20 6f 62 6a 65 63 74 73 0d 0a 2a 2a 20 63 61  e objects..** ca
29d80 6e 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66  n be integers, f
29d90 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
29da0 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42  lues, strings, B
29db0 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0d 0a  LOBs, or NULL...
29dc0 2a 2a 0d 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65  **..** An sqlite
29dd0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d  3_value object m
29de0 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70 72  ay be either "pr
29df0 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e 70  otected" or "unp
29e00 72 6f 74 65 63 74 65 64 22 2e 0d 0a 2a 2a 20 53  rotected"...** S
29e10 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72  ome interfaces r
29e20 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74  equire a protect
29e30 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
29e40 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61  .  Other interfa
29e50 63 65 73 0d 0a 2a 2a 20 77 69 6c 6c 20 61 63 63  ces..** will acc
29e60 65 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f  ept either a pro
29e70 74 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70  tected or an unp
29e80 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
29e90 5f 76 61 6c 75 65 2e 0d 0a 2a 2a 20 45 76 65 72  _value...** Ever
29ea0 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  y interface that
29eb0 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 65 33   accepts sqlite3
29ec0 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73  _value arguments
29ed0 20 73 70 65 63 69 66 69 65 73 0d 0a 2a 2a 20 77   specifies..** w
29ee0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74  hether or not it
29ef0 20 72 65 71 75 69 72 65 73 20 61 20 70 72 6f 74   requires a prot
29f00 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
29f10 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  lue...**..** The
29f20 20 74 65 72 6d 73 20 22 70 72 6f 74 65 63 74 65   terms "protecte
29f30 64 22 20 61 6e 64 20 22 75 6e 70 72 6f 74 65 63  d" and "unprotec
29f40 74 65 64 22 20 72 65 66 65 72 20 74 6f 20 77 68  ted" refer to wh
29f50 65 74 68 65 72 20 6f 72 20 6e 6f 74 0d 0a 2a 2a  ether or not..**
29f60 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64   a mutex is held
29f70 2e 20 20 41 6e 20 69 6e 74 65 72 6e 61 6c 20 6d  .  An internal m
29f80 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72  utex is held for
29f90 20 61 20 70 72 6f 74 65 63 74 65 64 0d 0a 2a 2a   a protected..**
29fa0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
29fb0 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74  bject but no mut
29fc0 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61  ex is held for a
29fd0 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0d 0a 2a  n unprotected..*
29fe0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
29ff0 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69  object.  If SQLi
2a000 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74  te is compiled t
2a010 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65  o be single-thre
2a020 61 64 65 64 0d 0a 2a 2a 20 28 77 69 74 68 20 5b  aded..** (with [
2a030 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2a040 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73  E=0] and with [s
2a050 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
2a060 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30  e()] returning 0
2a070 29 0d 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69  )..** or if SQLi
2a080 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65  te is run in one
2a090 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 65   of reduced mute
2a0a0 78 20 6d 6f 64 65 73 20 0d 0a 2a 2a 20 5b 53 51  x modes ..** [SQ
2a0b0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
2a0c0 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51  LETHREAD] or [SQ
2a0d0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
2a0e0 49 54 48 52 45 41 44 5d 0d 0a 2a 2a 20 74 68 65  ITHREAD]..** the
2a0f0 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69  n there is no di
2a100 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
2a110 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20  n protected and 
2a120 75 6e 70 72 6f 74 65 63 74 65 64 0d 0a 2a 2a 20  unprotected..** 
2a130 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
2a140 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79 20 63  jects and they c
2a150 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74 65 72  an be used inter
2a160 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77  changeably.  How
2a170 65 76 65 72 2c 0d 0a 2a 2a 20 66 6f 72 20 6d 61  ever,..** for ma
2a180 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74 61  ximum code porta
2a190 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65 63  bility it is rec
2a1a0 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70  ommended that ap
2a1b0 70 6c 69 63 61 74 69 6f 6e 73 0d 0a 2a 2a 20 73  plications..** s
2a1c0 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69  till make the di
2a1d0 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
2a1e0 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20  n protected and 
2a1f0 75 6e 70 72 6f 74 65 63 74 65 64 0d 0a 2a 2a 20  unprotected..** 
2a200 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
2a210 6a 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20  jects even when 
2a220 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 71  not strictly req
2a230 75 69 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  uired...**..** ^
2a240 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
2a250 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61  e objects that a
2a260 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72  re passed as par
2a270 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65  ameters into the
2a280 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ..** implementat
2a290 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74  ion of [applicat
2a2a0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
2a2b0 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70  functions] are p
2a2c0 72 6f 74 65 63 74 65 64 2e 0d 0a 2a 2a 20 5e 54  rotected...** ^T
2a2d0 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
2a2e0 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64   object returned
2a2f0 20 62 79 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   by..** [sqlite3
2a300 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
2a310 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e   is unprotected.
2a320 0d 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64  ..** Unprotected
2a330 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
2a340 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20  bjects may only 
2a350 62 65 20 75 73 65 64 20 77 69 74 68 0d 0a 2a 2a  be used with..**
2a360 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
2a370 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73  _value()] and [s
2a380 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
2a390 65 28 29 5d 2e 0d 0a 2a 2a 20 54 68 65 20 5b 73  e()]...** The [s
2a3a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2a3b0 62 20 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b | sqlite3_valu
2a3c0 65 5f 74 79 70 65 28 29 5d 20 66 61 6d 69 6c 79  e_type()] family
2a3d0 20 6f 66 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63   of..** interfac
2a3e0 65 73 20 72 65 71 75 69 72 65 20 70 72 6f 74 65  es require prote
2a3f0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2a400 75 65 20 6f 62 6a 65 63 74 73 2e 0d 0a 2a 2f 0d  ue objects...*/.
2a410 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2a420 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  Mem sqlite3_valu
2a430 65 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  e;..../*..** CAP
2a440 49 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74  I3REF: SQL Funct
2a450 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65  ion Context Obje
2a460 63 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63  ct..**..** The c
2a470 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20  ontext in which 
2a480 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  an SQL function 
2a490 65 78 65 63 75 74 65 73 20 69 73 20 73 74 6f 72  executes is stor
2a4a0 65 64 20 69 6e 20 61 6e 0d 0a 2a 2a 20 73 71 6c  ed in an..** sql
2a4b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a  ite3_context obj
2a4c0 65 63 74 2e 20 20 5e 41 20 70 6f 69 6e 74 65 72  ect.  ^A pointer
2a4d0 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 63   to an sqlite3_c
2a4e0 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0d 0a 2a  ontext object..*
2a4f0 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73  * is always firs
2a500 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  t parameter to [
2a510 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2a520 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ned SQL function
2a530 73 5d 2e 0d 0a 2a 2a 20 54 68 65 20 61 70 70 6c  s]...** The appl
2a540 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2a550 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
2a560 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c  lementation will
2a570 20 70 61 73 73 20 74 68 69 73 0d 0a 2a 2a 20 70   pass this..** p
2a580 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69  ointer through i
2a590 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  nto calls to [sq
2a5a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2a5b0 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c   | sqlite3_resul
2a5c0 74 28 29 5d 2c 0d 0a 2a 2a 20 5b 73 71 6c 69 74  t()],..** [sqlit
2a5d0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2a5e0 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65  text()], [sqlite
2a5f0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c 0d  3_user_data()],.
2a600 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
2a610 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29  text_db_handle()
2a620 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  ], [sqlite3_get_
2a630 61 75 78 64 61 74 61 28 29 5d 2c 0d 0a 2a 2a 20  auxdata()],..** 
2a640 61 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f  and/or [sqlite3_
2a650 73 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0d  set_auxdata()]..
2a660 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72  .*/..typedef str
2a670 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  uct sqlite3_cont
2a680 65 78 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ext sqlite3_cont
2a690 65 78 74 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ext;..../*..** C
2a6a0 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 6e 67  API3REF: Binding
2a6b0 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70 61   Values To Prepa
2a6c0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 0d 0a  red Statements..
2a6d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 68 6f  ** KEYWORDS: {ho
2a6e0 73 74 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 68  st parameter} {h
2a6f0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 7d 20  ost parameters} 
2a700 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20  {host parameter 
2a710 6e 61 6d 65 7d 0d 0a 2a 2a 20 4b 45 59 57 4f 52  name}..** KEYWOR
2a720 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65 74  DS: {SQL paramet
2a730 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65 74  er} {SQL paramet
2a740 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 72 20  ers} {parameter 
2a750 62 69 6e 64 69 6e 67 7d 0d 0a 2a 2a 0d 0a 2a 2a  binding}..**..**
2a760 20 5e 28 49 6e 20 74 68 65 20 53 51 4c 20 73 74   ^(In the SQL st
2a770 61 74 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 70  atement text inp
2a780 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70  ut to [sqlite3_p
2a790 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
2a7a0 20 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0d 0a   its variants,..
2a7b0 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20  ** literals may 
2a7c0 62 65 20 72 65 70 6c 61 63 65 64 20 62 79 20 61  be replaced by a
2a7d0 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61   [parameter] tha
2a7e0 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66  t matches one of
2a7f0 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 74   following..** t
2a800 65 6d 70 6c 61 74 65 73 3a 0d 0a 2a 2a 0d 0a 2a  emplates:..**..*
2a810 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20  * <ul>..** <li> 
2a820 20 3f 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e   ?..** <li>  ?NN
2a830 4e 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56  N..** <li>  :VVV
2a840 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0d  ..** <li>  @VVV.
2a850 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0d 0a  .** <li>  $VVV..
2a860 2a 2a 20 3c 2f 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a  ** </ul>..**..**
2a870 20 49 6e 20 74 68 65 20 74 65 6d 70 6c 61 74 65   In the template
2a880 73 20 61 62 6f 76 65 2c 20 4e 4e 4e 20 72 65 70  s above, NNN rep
2a890 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67  resents an integ
2a8a0 65 72 20 6c 69 74 65 72 61 6c 2c 0d 0a 2a 2a 20  er literal,..** 
2a8b0 61 6e 64 20 56 56 56 20 72 65 70 72 65 73 65 6e  and VVV represen
2a8c0 74 73 20 61 6e 20 61 6c 70 68 61 6e 75 6d 65 72  ts an alphanumer
2a8d0 69 63 20 69 64 65 6e 74 69 66 69 65 72 2e 29 5e  ic identifier.)^
2a8e0 20 20 5e 54 68 65 20 76 61 6c 75 65 73 20 6f 66    ^The values of
2a8f0 20 74 68 65 73 65 0d 0a 2a 2a 20 70 61 72 61 6d   these..** param
2a900 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c  eters (also call
2a910 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74  ed "host paramet
2a920 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 51  er names" or "SQ
2a930 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29 0d 0a  L parameters")..
2a940 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75 73  ** can be set us
2a950 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
2a960 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
2a970 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0d  s defined here..
2a980 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 72  .**..** ^The fir
2a990 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
2a9a0 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
2a9b0 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20  *() routines is 
2a9c0 61 6c 77 61 79 73 0d 0a 2a 2a 20 61 20 70 6f 69  always..** a poi
2a9d0 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c  nter to the [sql
2a9e0 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63  ite3_stmt] objec
2a9f0 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0d  t returned from.
2aa00 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
2aa10 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74  pare_v2()] or it
2aa20 73 20 76 61 72 69 61 6e 74 73 2e 0d 0a 2a 2a 0d  s variants...**.
2aa30 0a 2a 2a 20 5e 54 68 65 20 73 65 63 6f 6e 64 20  .** ^The second 
2aa40 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2aa50 69 6e 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c  index of the SQL
2aa60 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65   parameter to be
2aa70 20 73 65 74 2e 0d 0a 2a 2a 20 5e 54 68 65 20 6c   set...** ^The l
2aa80 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 72 61  eftmost SQL para
2aa90 6d 65 74 65 72 20 68 61 73 20 61 6e 20 69 6e 64  meter has an ind
2aaa0 65 78 20 6f 66 20 31 2e 20 20 5e 57 68 65 6e 20  ex of 1.  ^When 
2aab0 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 64 0d 0a  the same named..
2aac0 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  ** SQL parameter
2aad0 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 74 68   is used more th
2aae0 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e 64 20  an once, second 
2aaf0 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 0d 0a  and subsequent..
2ab00 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 68  ** occurrences h
2ab10 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e 64  ave the same ind
2ab20 65 78 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ex as the first 
2ab30 6f 63 63 75 72 72 65 6e 63 65 2e 0d 0a 2a 2a 20  occurrence...** 
2ab40 5e 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 6e  ^The index for n
2ab50 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 20  amed parameters 
2ab60 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70  can be looked up
2ab70 20 75 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 5b   using the..** [
2ab80 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
2ab90 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20  ameter_index()] 
2aba0 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e 20  API if desired. 
2abb0 20 5e 54 68 65 20 69 6e 64 65 78 0d 0a 2a 2a 20   ^The index..** 
2abc0 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d  for "?NNN" param
2abd0 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c  eters is the val
2abe0 75 65 20 6f 66 20 4e 4e 4e 2e 0d 0a 2a 2a 20 5e  ue of NNN...** ^
2abf0 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75  The NNN value mu
2ac00 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20  st be between 1 
2ac10 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33  and the [sqlite3
2ac20 5f 6c 69 6d 69 74 28 29 5d 0d 0a 2a 2a 20 70 61  _limit()]..** pa
2ac30 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f  rameter [SQLITE_
2ac40 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
2ac50 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20  UMBER] (default 
2ac60 76 61 6c 75 65 3a 20 39 39 39 29 2e 0d 0a 2a 2a  value: 999)...**
2ac70 0d 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20  ..** ^The third 
2ac80 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2ac90 76 61 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f  value to bind to
2aca0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0d   the parameter..
2acb0 0a 2a 2a 0d 0a 2a 2a 20 5e 28 49 6e 20 74 68 6f  .**..** ^(In tho
2acc0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  se routines that
2acd0 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 20 61   have a fourth a
2ace0 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 61 6c  rgument, its val
2acf0 75 65 20 69 73 20 74 68 65 0d 0a 2a 2a 20 6e 75  ue is the..** nu
2ad00 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
2ad10 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 20   the parameter. 
2ad20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 68   To be clear: th
2ad30 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0d 0a  e value is the..
2ad40 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e  ** number of <u>
2ad50 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65  bytes</u> in the
2ad60 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20   value, not the 
2ad70 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
2ad80 74 65 72 73 2e 29 5e 0d 0a 2a 2a 20 5e 49 66 20  ters.)^..** ^If 
2ad90 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
2ada0 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65  eter is negative
2adb0 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  , the length of 
2adc0 74 68 65 20 73 74 72 69 6e 67 20 69 73 0d 0a 2a  the string is..*
2add0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
2ade0 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20  bytes up to the 
2adf0 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
2ae00 6e 61 74 6f 72 2e 0d 0a 2a 2a 20 49 66 20 61 20  nator...** If a 
2ae10 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 66 6f 75  non-negative fou
2ae20 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  rth parameter is
2ae30 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 71 6c   provided to sql
2ae40 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29  ite3_bind_text()
2ae50 0d 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ..** or sqlite3_
2ae60 62 69 6e 64 5f 74 65 78 74 31 36 28 29 20 74 68  bind_text16() th
2ae70 65 6e 20 74 68 61 74 20 70 61 72 61 6d 65 74 65  en that paramete
2ae80 72 20 6d 75 73 74 20 62 65 20 74 68 65 20 62 79  r must be the by
2ae90 74 65 20 6f 66 66 73 65 74 0d 0a 2a 2a 20 77 68  te offset..** wh
2aea0 65 72 65 20 74 68 65 20 4e 55 4c 20 74 65 72 6d  ere the NUL term
2aeb0 69 6e 61 74 6f 72 20 77 6f 75 6c 64 20 6f 63 63  inator would occ
2aec0 75 72 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20  ur assuming the 
2aed0 73 74 72 69 6e 67 20 77 65 72 65 20 4e 55 4c 0d  string were NUL.
2aee0 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 65 64 2e 20  .** terminated. 
2aef0 20 49 66 20 61 6e 79 20 4e 55 4c 20 63 68 61 72   If any NUL char
2af00 61 63 74 65 72 73 20 6f 63 63 75 72 20 61 74 20  acters occur at 
2af10 62 79 74 65 20 6f 66 66 73 65 74 73 20 6c 65 73  byte offsets les
2af20 73 20 74 68 61 6e 20 0d 0a 2a 2a 20 74 68 65 20  s than ..** the 
2af30 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 75  value of the fou
2af40 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 68  rth parameter th
2af50 65 6e 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  en the resulting
2af60 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 77 69   string value wi
2af70 6c 6c 0d 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 65  ll..** contain e
2af80 6d 62 65 64 64 65 64 20 4e 55 4c 73 2e 20 20 54  mbedded NULs.  T
2af90 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 70  he result of exp
2afa0 72 65 73 73 69 6f 6e 73 20 69 6e 76 6f 6c 76 69  ressions involvi
2afb0 6e 67 20 73 74 72 69 6e 67 73 0d 0a 2a 2a 20 77  ng strings..** w
2afc0 69 74 68 20 65 6d 62 65 64 64 65 64 20 4e 55 4c  ith embedded NUL
2afd0 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d  s is undefined..
2afe0 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 66  .**..** ^The fif
2aff0 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  th argument to s
2b000 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
2b010 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  (), sqlite3_bind
2b020 5f 74 65 78 74 28 29 2c 20 61 6e 64 0d 0a 2a 2a  _text(), and..**
2b030 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
2b040 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73 74  xt16() is a dest
2b050 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64  ructor used to d
2b060 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c  ispose of the BL
2b070 4f 42 20 6f 72 0d 0a 2a 2a 20 73 74 72 69 6e 67  OB or..** string
2b080 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61   after SQLite ha
2b090 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
2b0a0 69 74 2e 20 20 5e 54 68 65 20 64 65 73 74 72 75  it.  ^The destru
2b0b0 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64 0d 0a  ctor is called..
2b0c0 2a 2a 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ** to dispose of
2b0d0 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72   the BLOB or str
2b0e0 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20  ing even if the 
2b0f0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
2b100 62 69 6e 64 5f 62 6c 6f 62 28 29 2c 0d 0a 2a 2a  bind_blob(),..**
2b110 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
2b120 78 74 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33  xt(), or sqlite3
2b130 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 29 20 66  _bind_text16() f
2b140 61 69 6c 73 2e 20 20 0d 0a 2a 2a 20 5e 49 66 20  ails.  ..** ^If 
2b150 74 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65  the fifth argume
2b160 6e 74 20 69 73 0d 0a 2a 2a 20 74 68 65 20 73 70  nt is..** the sp
2b170 65 63 69 61 6c 20 76 61 6c 75 65 20 5b 53 51 4c  ecial value [SQL
2b180 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 74 68 65  ITE_STATIC], the
2b190 6e 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73  n SQLite assumes
2b1a0 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a 20 69 6e   that the..** in
2b1b0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 6e 20  formation is in 
2b1c0 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 67 65  static, unmanage
2b1d0 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73  d space and does
2b1e0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
2b1f0 66 72 65 65 64 2e 0d 0a 2a 2a 20 5e 49 66 20 74  freed...** ^If t
2b200 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e  he fifth argumen
2b210 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 65 20  t has the value 
2b220 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  [SQLITE_TRANSIEN
2b230 54 5d 2c 20 74 68 65 6e 0d 0a 2a 2a 20 53 51 4c  T], then..** SQL
2b240 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77  ite makes its ow
2b250 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  n private copy o
2b260 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 64  f the data immed
2b270 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0d 0a  iately, before..
2b280 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ** the sqlite3_b
2b290 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20  ind_*() routine 
2b2a0 72 65 74 75 72 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  returns...**..**
2b2b0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69   ^The sqlite3_bi
2b2c0 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f  nd_zeroblob() ro
2b2d0 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c  utine binds a BL
2b2e0 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74  OB of length N t
2b2f0 68 61 74 0d 0a 2a 2a 20 69 73 20 66 69 6c 6c 65  hat..** is fille
2b300 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20  d with zeroes.  
2b310 5e 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73  ^A zeroblob uses
2b320 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20   a fixed amount 
2b330 6f 66 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 28 6a  of memory..** (j
2b340 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74  ust an integer t
2b350 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29  o hold its size)
2b360 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69   while it is bei
2b370 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0d 0a 2a  ng processed...*
2b380 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20  * Zeroblobs are 
2b390 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76  intended to serv
2b3a0 65 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72  e as placeholder
2b3b0 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73  s for BLOBs whos
2b3c0 65 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73  e..** content is
2b3d0 20 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75   later written u
2b3e0 73 69 6e 67 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65  sing..** [sqlite
2b3f0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e  3_blob_open | in
2b400 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49  cremental BLOB I
2b410 2f 4f 5d 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a  /O] routines...*
2b420 2a 20 5e 41 20 6e 65 67 61 74 69 76 65 20 76 61  * ^A negative va
2b430 6c 75 65 20 66 6f 72 20 74 68 65 20 7a 65 72 6f  lue for the zero
2b440 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 69 6e 20  blob results in 
2b450 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c  a zero-length BL
2b460 4f 42 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20  OB...**..** ^If 
2b470 61 6e 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74  any of the sqlit
2b480 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74  e3_bind_*() rout
2b490 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20  ines are called 
2b4a0 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  with a NULL poin
2b4b0 74 65 72 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ter..** for the 
2b4c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
2b4d0 65 6e 74 5d 20 6f 72 20 77 69 74 68 20 61 20 70  ent] or with a p
2b4e0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2b4f0 74 20 66 6f 72 20 77 68 69 63 68 0d 0a 2a 2a 20  t for which..** 
2b500 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
2b510 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
2b520 20 6d 6f 72 65 20 72 65 63 65 6e 74 6c 79 20 74   more recently t
2b530 68 61 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  han [sqlite3_res
2b540 65 74 28 29 5d 2c 0d 0a 2a 2a 20 74 68 65 6e 20  et()],..** then 
2b550 74 68 65 20 63 61 6c 6c 20 77 69 6c 6c 20 72 65  the call will re
2b560 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4d 49 53  turn [SQLITE_MIS
2b570 55 53 45 5d 2e 20 20 49 66 20 61 6e 79 20 73 71  USE].  If any sq
2b580 6c 69 74 65 33 5f 62 69 6e 64 5f 28 29 0d 0a 2a  lite3_bind_()..*
2b590 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 70 61 73  * routine is pas
2b5a0 73 65 64 20 61 20 5b 70 72 65 70 61 72 65 64 20  sed a [prepared 
2b5b0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20  statement] that 
2b5c0 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  has been finaliz
2b5d0 65 64 2c 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75  ed, the..** resu
2b5e0 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  lt is undefined 
2b5f0 61 6e 64 20 70 72 6f 62 61 62 6c 79 20 68 61 72  and probably har
2b600 6d 66 75 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 42  mful...**..** ^B
2b610 69 6e 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20  indings are not 
2b620 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 20 5b  cleared by the [
2b630 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
2b640 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 20 5e 55   routine...** ^U
2b650 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  nbound parameter
2b660 73 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65  s are interprete
2b670 64 20 61 73 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a  d as NULL...**..
2b680 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
2b690 62 69 6e 64 5f 2a 20 72 6f 75 74 69 6e 65 73 20  bind_* routines 
2b6a0 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f  return [SQLITE_O
2b6b0 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  K] on success or
2b6c0 20 61 6e 0d 0a 2a 2a 20 5b 65 72 72 6f 72 20 63   an..** [error c
2b6d0 6f 64 65 5d 20 69 66 20 61 6e 79 74 68 69 6e 67  ode] if anything
2b6e0 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0d 0a 2a 2a   goes wrong...**
2b6f0 20 5e 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d   ^[SQLITE_RANGE]
2b700 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
2b710 74 68 65 20 70 61 72 61 6d 65 74 65 72 0d 0a 2a  the parameter..*
2b720 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f  * index is out o
2b730 66 20 72 61 6e 67 65 2e 20 20 5e 5b 53 51 4c 49  f range.  ^[SQLI
2b740 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74  TE_NOMEM] is ret
2b750 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28  urned if malloc(
2b760 29 20 66 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  ) fails...**..**
2b770 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
2b780 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
2b790 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0d 0a 2a 2a  er_count()],..**
2b7a0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
2b7b0 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d  arameter_name()]
2b7c0 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  , and [sqlite3_b
2b7d0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
2b7e0 64 65 78 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c  dex()]...*/..SQL
2b7f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2b800 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71  te3_bind_blob(sq
2b810 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
2b820 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
2b830 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f  nt n, void(*)(vo
2b840 69 64 2a 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  id*));..SQLITE_A
2b850 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
2b860 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  ind_double(sqlit
2b870 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64  e3_stmt*, int, d
2b880 6f 75 62 6c 65 29 3b 0d 0a 53 51 4c 49 54 45 5f  ouble);..SQLITE_
2b890 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2b8a0 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33  bind_int(sqlite3
2b8b0 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  _stmt*, int, int
2b8c0 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  );..SQLITE_API i
2b8d0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
2b8e0 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
2b8f0 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65  mt*, int, sqlite
2b900 33 5f 69 6e 74 36 34 29 3b 0d 0a 53 51 4c 49 54  3_int64);..SQLIT
2b910 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2b920 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69  3_bind_null(sqli
2b930 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b  te3_stmt*, int);
2b940 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
2b950 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
2b960 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  xt(sqlite3_stmt*
2b970 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
2b980 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28  r*, int n, void(
2b990 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 53 51 4c  *)(void*));..SQL
2b9a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2b9b0 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28  te3_bind_text16(
2b9c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
2b9d0 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  nt, const void*,
2b9e0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
2b9f0 69 64 2a 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  id*));..SQLITE_A
2ba00 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
2ba10 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  ind_value(sqlite
2ba20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
2ba30 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nst sqlite3_valu
2ba40 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  e*);..SQLITE_API
2ba50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
2ba60 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74  d_zeroblob(sqlit
2ba70 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69  e3_stmt*, int, i
2ba80 6e 74 20 6e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  nt n);..../*..**
2ba90 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65   CAPI3REF: Numbe
2baa0 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 74  r Of SQL Paramet
2bab0 65 72 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69  ers..**..** ^Thi
2bac0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
2bad0 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68   used to find th
2bae0 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c  e number of [SQL
2baf0 20 70 61 72 61 6d 65 74 65 72 73 5d 0d 0a 2a 2a   parameters]..**
2bb00 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   in a [prepared 
2bb10 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c  statement].  SQL
2bb20 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
2bb30 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0d 0a 2a  tokens of the..*
2bb40 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e  * form "?", "?NN
2bb50 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41  N", ":AAA", "$AA
2bb60 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68  A", or "@AAA" th
2bb70 61 74 20 73 65 72 76 65 20 61 73 0d 0a 2a 2a 20  at serve as..** 
2bb80 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72  placeholders for
2bb90 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65   values that are
2bba0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
2bbb0 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0d 0a 2a 2a  lob | bound]..**
2bbc0 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65   to the paramete
2bbd0 72 73 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  rs at a later ti
2bbe0 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68  me...**..** ^(Th
2bbf0 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 75 61  is routine actua
2bc00 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 68 65 20  lly returns the 
2bc10 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6c 61 72  index of the lar
2bc20 67 65 73 74 20 28 72 69 67 68 74 6d 6f 73 74 29  gest (rightmost)
2bc30 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20  ..** parameter. 
2bc40 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 65 78  For all forms ex
2bc50 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 73 20  cept ?NNN, this 
2bc60 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20  will correspond 
2bc70 74 6f 20 74 68 65 0d 0a 2a 2a 20 6e 75 6d 62 65  to the..** numbe
2bc80 72 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 61  r of unique para
2bc90 6d 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 61  meters.  If para
2bca0 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f 4e  meters of the ?N
2bcb0 4e 4e 20 66 6f 72 6d 20 61 72 65 20 75 73 65 64  NN form are used
2bcc0 2c 0d 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20  ,..** there may 
2bcd0 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c  be gaps in the l
2bce0 69 73 74 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 53  ist.)^..**..** S
2bcf0 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
2bd00 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69  3_bind_blob|sqli
2bd10 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0d 0a 2a 2a  te3_bind()],..**
2bd20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
2bd30 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d  arameter_name()]
2bd40 2c 20 61 6e 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74  , and..** [sqlit
2bd50 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
2bd60 72 5f 69 6e 64 65 78 28 29 5d 2e 0d 0a 2a 2f 0d  r_index()]...*/.
2bd70 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2bd80 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
2bd90 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c  ameter_count(sql
2bda0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a 0d 0a  ite3_stmt*);....
2bdb0 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
2bdc0 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20   Name Of A Host 
2bdd0 50 61 72 61 6d 65 74 65 72 0d 0a 2a 2a 0d 0a 2a  Parameter..**..*
2bde0 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 62  * ^The sqlite3_b
2bdf0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
2be00 6d 65 28 50 2c 4e 29 20 69 6e 74 65 72 66 61 63  me(P,N) interfac
2be10 65 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 74 68  e returns..** th
2be20 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 2d  e name of the N-
2be30 74 68 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65  th [SQL paramete
2be40 72 5d 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61  r] in the [prepa
2be50 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 50  red statement] P
2be60 2e 0d 0a 2a 2a 20 5e 28 53 51 4c 20 70 61 72 61  ...** ^(SQL para
2be70 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f  meters of the fo
2be80 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41  rm "?NNN" or ":A
2be90 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72  AA" or "@AAA" or
2bea0 20 22 24 41 41 41 22 0d 0a 2a 2a 20 68 61 76 65   "$AAA"..** have
2beb0 20 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 73   a name which is
2bec0 20 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e 4e   the string "?NN
2bed0 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20  N" or ":AAA" or 
2bee0 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22  "@AAA" or "$AAA"
2bef0 0d 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c  ..** respectivel
2bf00 79 2e 0d 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  y...** In other 
2bf10 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69  words, the initi
2bf20 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72  al ":" or "$" or
2bf30 20 22 40 22 20 6f 72 20 22 3f 22 0d 0a 2a 2a 20   "@" or "?"..** 
2bf40 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70  is included as p
2bf50 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e  art of the name.
2bf60 29 5e 0d 0a 2a 2a 20 5e 50 61 72 61 6d 65 74 65  )^..** ^Paramete
2bf70 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rs of the form "
2bf80 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c  ?" without a fol
2bf90 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68  lowing integer h
2bfa0 61 76 65 20 6e 6f 20 6e 61 6d 65 0d 0a 2a 2a 20  ave no name..** 
2bfb0 61 6e 64 20 61 72 65 20 72 65 66 65 72 72 65 64  and are referred
2bfc0 20 74 6f 20 61 73 20 22 6e 61 6d 65 6c 65 73 73   to as "nameless
2bfd0 22 20 6f 72 20 22 61 6e 6f 6e 79 6d 6f 75 73 20  " or "anonymous 
2bfe0 70 61 72 61 6d 65 74 65 72 73 22 2e 0d 0a 2a 2a  parameters"...**
2bff0 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20  ..** ^The first 
2c000 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68  host parameter h
2c010 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
2c020 2c 20 6e 6f 74 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a  , not 0...**..**
2c030 20 5e 49 66 20 74 68 65 20 76 61 6c 75 65 20 4e   ^If the value N
2c040 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
2c050 20 6f 72 20 69 66 20 74 68 65 20 4e 2d 74 68 20   or if the N-th 
2c060 70 61 72 61 6d 65 74 65 72 20 69 73 0d 0a 2a 2a  parameter is..**
2c070 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20   nameless, then 
2c080 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
2c090 2e 20 20 5e 54 68 65 20 72 65 74 75 72 6e 65 64  .  ^The returned
2c0a0 20 73 74 72 69 6e 67 20 69 73 0d 0a 2a 2a 20 61   string is..** a
2c0b0 6c 77 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65  lways in UTF-8 e
2c0c0 6e 63 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20  ncoding even if 
2c0d0 74 68 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65  the named parame
2c0e0 74 65 72 20 77 61 73 0d 0a 2a 2a 20 6f 72 69 67  ter was..** orig
2c0f0 69 6e 61 6c 6c 79 20 73 70 65 63 69 66 69 65 64  inally specified
2c100 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 5b 73   as UTF-16 in [s
2c110 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2c120 28 29 5d 20 6f 72 0d 0a 2a 2a 20 5b 73 71 6c 69  ()] or..** [sqli
2c130 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
2c140 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  ()]...**..** See
2c150 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
2c160 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
2c170 33 5f 62 69 6e 64 28 29 5d 2c 0d 0a 2a 2a 20 5b  3_bind()],..** [
2c180 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
2c190 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c  ameter_count()],
2c1a0 20 61 6e 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65   and..** [sqlite
2c1b0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
2c1c0 5f 69 6e 64 65 78 28 29 5d 2e 0d 0a 2a 2f 0d 0a  _index()]...*/..
2c1d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2c1e0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62   char *sqlite3_b
2c1f0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
2c200 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  me(sqlite3_stmt*
2c210 2c 20 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  , int);..../*..*
2c220 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65  * CAPI3REF: Inde
2c230 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72  x Of A Parameter
2c240 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61   With A Given Na
2c250 6d 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 52 65 74 75  me..**..** ^Retu
2c260 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
2c270 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  an SQL parameter
2c280 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e   given its name.
2c290 20 20 5e 54 68 65 0d 0a 2a 2a 20 69 6e 64 65 78    ^The..** index
2c2a0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
2c2b0 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20  is suitable for 
2c2c0 75 73 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e  use as the secon
2c2d0 64 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  d..** parameter 
2c2e0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
2c2f0 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69  _blob|sqlite3_bi
2c300 6e 64 28 29 5d 2e 20 20 5e 41 20 7a 65 72 6f 0d  nd()].  ^A zero.
2c310 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
2c320 69 66 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70  if no matching p
2c330 61 72 61 6d 65 74 65 72 20 69 73 20 66 6f 75 6e  arameter is foun
2c340 64 2e 20 20 5e 54 68 65 20 70 61 72 61 6d 65 74  d.  ^The paramet
2c350 65 72 0d 0a 2a 2a 20 6e 61 6d 65 20 6d 75 73 74  er..** name must
2c360 20 62 65 20 67 69 76 65 6e 20 69 6e 20 55 54 46   be given in UTF
2c370 2d 38 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f  -8 even if the o
2c380 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e  riginal statemen
2c390 74 0d 0a 2a 2a 20 77 61 73 20 70 72 65 70 61 72  t..** was prepar
2c3a0 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 36 20 74  ed from UTF-16 t
2c3b0 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ext using [sqlit
2c3c0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
2c3d0 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20  )]...**..** See 
2c3e0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
2c3f0 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33  ind_blob|sqlite3
2c400 5f 62 69 6e 64 28 29 5d 2c 0d 0a 2a 2a 20 5b 73  _bind()],..** [s
2c410 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
2c420 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20  meter_count()], 
2c430 61 6e 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  and..** [sqlite3
2c440 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
2c450 69 6e 64 65 78 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53  index()]...*/..S
2c460 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2c470 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2c480 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74  eter_index(sqlit
2c490 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20  e3_stmt*, const 
2c4a0 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0d 0a 0d  char *zName);...
2c4b0 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
2c4c0 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64  : Reset All Bind
2c4d0 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72  ings On A Prepar
2c4e0 65 64 20 53 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a  ed Statement..**
2c4f0 0d 0a 2a 2a 20 5e 43 6f 6e 74 72 61 72 79 20 74  ..** ^Contrary t
2c500 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20  o the intuition 
2c510 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65  of many, [sqlite
2c520 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20  3_reset()] does 
2c530 6e 6f 74 20 72 65 73 65 74 0d 0a 2a 2a 20 74 68  not reset..** th
2c540 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
2c550 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d  blob | bindings]
2c560 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   on a [prepared 
2c570 73 74 61 74 65 6d 65 6e 74 5d 2e 0d 0a 2a 2a 20  statement]...** 
2c580 5e 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e  ^Use this routin
2c590 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68  e to reset all h
2c5a0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74  ost parameters t
2c5b0 6f 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 53 51 4c  o NULL...*/..SQL
2c5c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2c5d0 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e  te3_clear_bindin
2c5e0 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  gs(sqlite3_stmt*
2c5f0 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  );..../*..** CAP
2c600 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66  I3REF: Number Of
2c610 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65   Columns In A Re
2c620 73 75 6c 74 20 53 65 74 0d 0a 2a 2a 0d 0a 2a 2a  sult Set..**..**
2c630 20 5e 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d   ^Return the num
2c640 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2c650 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2c660 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
2c670 0d 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  ..** [prepared s
2c680 74 61 74 65 6d 65 6e 74 5d 2e 20 5e 54 68 69 73  tatement]. ^This
2c690 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2c6a0 20 30 20 69 66 20 70 53 74 6d 74 20 69 73 20 61   0 if pStmt is a
2c6b0 6e 20 53 51 4c 0d 0a 2a 2a 20 73 74 61 74 65 6d  n SQL..** statem
2c6c0 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ent that does no
2c6d0 74 20 72 65 74 75 72 6e 20 64 61 74 61 20 28 66  t return data (f
2c6e0 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55  or example an [U
2c6f0 50 44 41 54 45 5d 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  PDATE])...**..**
2c700 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
2c710 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 29  te3_data_count()
2c720 5d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ]..*/..SQLITE_AP
2c730 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
2c740 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74  lumn_count(sqlit
2c750 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
2c760 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
2c770 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65  REF: Column Name
2c780 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65  s In A Result Se
2c790 74 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 73 65  t..**..** ^These
2c7a0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
2c7b0 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e   the name assign
2c7c0 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c  ed to a particul
2c7d0 61 72 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 69 6e  ar column..** in
2c7e0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2c7f0 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74  of a [SELECT] st
2c800 61 74 65 6d 65 6e 74 2e 20 20 5e 54 68 65 20 73  atement.  ^The s
2c810 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2c820 6d 65 28 29 0d 0a 2a 2a 20 69 6e 74 65 72 66 61  me()..** interfa
2c830 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
2c840 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74  nter to a zero-t
2c850 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
2c860 73 74 72 69 6e 67 0d 0a 2a 2a 20 61 6e 64 20 73  string..** and s
2c870 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2c880 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61  me16() returns a
2c890 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65   pointer to a ze
2c8a0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0d 0a 2a  ro-terminated..*
2c8b0 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e  * UTF-16 string.
2c8c0 20 20 5e 54 68 65 20 66 69 72 73 74 20 70 61 72    ^The first par
2c8d0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 70  ameter is the [p
2c8e0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2c8f0 74 5d 0d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c  t]..** that impl
2c900 65 6d 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45  ements the [SELE
2c910 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 5e  CT] statement. ^
2c920 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
2c930 65 74 65 72 20 69 73 20 74 68 65 0d 0a 2a 2a 20  eter is the..** 
2c940 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20  column number.  
2c950 5e 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f  ^The leftmost co
2c960 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65 72 20 30  lumn is number 0
2c970 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 72  ...**..** ^The r
2c980 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 70  eturned string p
2c990 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20  ointer is valid 
2c9a0 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 65  until either the
2c9b0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2c9c0 6d 65 6e 74 5d 0d 0a 2a 2a 20 69 73 20 64 65 73  ment]..** is des
2c9d0 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74  troyed by [sqlit
2c9e0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
2c9f0 72 20 75 6e 74 69 6c 20 74 68 65 20 73 74 61 74  r until the stat
2ca00 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74  ement is automat
2ca10 69 63 61 6c 6c 79 0d 0a 2a 2a 20 72 65 70 72 65  ically..** repre
2ca20 70 61 72 65 64 20 62 79 20 74 68 65 20 66 69 72  pared by the fir
2ca30 73 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  st call to [sqli
2ca40 74 65 33 5f 73 74 65 70 28 29 5d 20 66 6f 72 20  te3_step()] for 
2ca50 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 75 6e  a particular run
2ca60 0d 0a 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 74 68  ..** or until th
2ca70 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0d 0a  e next call to..
2ca80 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
2ca90 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69  n_name() or sqli
2caa0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2cab0 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  6() on the same 
2cac0 63 6f 6c 75 6d 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  column...**..** 
2cad0 5e 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ^If sqlite3_mall
2cae0 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e  oc() fails durin
2caf0 67 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  g the processing
2cb00 20 6f 66 20 65 69 74 68 65 72 20 72 6f 75 74 69   of either routi
2cb10 6e 65 0d 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d  ne..** (for exam
2cb20 70 6c 65 20 64 75 72 69 6e 67 20 61 20 63 6f 6e  ple during a con
2cb30 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46  version from UTF
2cb40 2d 38 20 74 6f 20 55 54 46 2d 31 36 29 20 74 68  -8 to UTF-16) th
2cb50 65 6e 20 61 0d 0a 2a 2a 20 4e 55 4c 4c 20 70 6f  en a..** NULL po
2cb60 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
2cb70 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  d...**..** ^The 
2cb80 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c 74  name of a result
2cb90 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 76   column is the v
2cba0 61 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 22  alue of the "AS"
2cbb0 20 63 6c 61 75 73 65 20 66 6f 72 0d 0a 2a 2a 20   clause for..** 
2cbc0 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20  that column, if 
2cbd0 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 20 63  there is an AS c
2cbe0 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 72 65  lause.  If there
2cbf0 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65   is no AS clause
2cc00 0d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 61  ..** then the na
2cc10 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
2cc20 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 20   is unspecified 
2cc30 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 66  and may change f
2cc40 72 6f 6d 0d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65  rom..** one rele
2cc50 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f  ase of SQLite to
2cc60 20 74 68 65 20 6e 65 78 74 2e 0d 0a 2a 2f 0d 0a   the next...*/..
2cc70 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2cc80 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
2cc90 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  olumn_name(sqlit
2cca0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29  e3_stmt*, int N)
2ccb0 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  ;..SQLITE_API co
2ccc0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2ccd0 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28  3_column_name16(
2cce0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
2ccf0 6e 74 20 4e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  nt N);..../*..**
2cd00 20 43 41 50 49 33 52 45 46 3a 20 53 6f 75 72 63   CAPI3REF: Sourc
2cd10 65 20 4f 66 20 44 61 74 61 20 49 6e 20 41 20 51  e Of Data In A Q
2cd20 75 65 72 79 20 52 65 73 75 6c 74 0d 0a 2a 2a 0d  uery Result..**.
2cd30 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 69  .** ^These routi
2cd40 6e 65 73 20 70 72 6f 76 69 64 65 20 61 20 6d 65  nes provide a me
2cd50 61 6e 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ans to determine
2cd60 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
2cd70 61 62 6c 65 2c 20 61 6e 64 0d 0a 2a 2a 20 74 61  able, and..** ta
2cd80 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ble column that 
2cd90 69 73 20 74 68 65 20 6f 72 69 67 69 6e 20 6f 66  is the origin of
2cda0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65   a particular re
2cdb0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 6e 0d 0a  sult column in..
2cdc0 2a 2a 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74  ** [SELECT] stat
2cdd0 65 6d 65 6e 74 2e 0d 0a 2a 2a 20 5e 54 68 65 20  ement...** ^The 
2cde0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
2cdf0 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72  base or table or
2ce00 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72   column can be r
2ce10 65 74 75 72 6e 65 64 20 61 73 0d 0a 2a 2a 20 65  eturned as..** e
2ce20 69 74 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72  ither a UTF-8 or
2ce30 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20   UTF-16 string. 
2ce40 20 5e 54 68 65 20 5f 64 61 74 61 62 61 73 65 5f   ^The _database_
2ce50 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
2ce60 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  ..** the databas
2ce70 65 20 6e 61 6d 65 2c 20 74 68 65 20 5f 74 61 62  e name, the _tab
2ce80 6c 65 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74  le_ routines ret
2ce90 75 72 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61  urn the table na
2cea0 6d 65 2c 20 61 6e 64 0d 0a 2a 2a 20 74 68 65 20  me, and..** the 
2ceb0 6f 72 69 67 69 6e 5f 20 72 6f 75 74 69 6e 65 73  origin_ routines
2cec0 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75   return the colu
2ced0 6d 6e 20 6e 61 6d 65 2e 0d 0a 2a 2a 20 5e 54 68  mn name...** ^Th
2cee0 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
2cef0 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c  g is valid until
2cf00 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
2cf10 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73  tatement] is des
2cf20 74 72 6f 79 65 64 0d 0a 2a 2a 20 75 73 69 6e 67  troyed..** using
2cf30 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
2cf40 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74  ze()] or until t
2cf50 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
2cf60 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0d 0a 2a  automatically..*
2cf70 2a 20 72 65 70 72 65 70 61 72 65 64 20 62 79 20  * reprepared by 
2cf80 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74  the first call t
2cf90 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
2cfa0 29 5d 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  )] for a particu
2cfb0 6c 61 72 20 72 75 6e 0d 0a 2a 2a 20 6f 72 20 75  lar run..** or u
2cfc0 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e  ntil the same in
2cfd0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71  formation is req
2cfe0 75 65 73 74 65 64 0d 0a 2a 2a 20 61 67 61 69 6e  uested..** again
2cff0 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
2d000 65 6e 63 6f 64 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  encoding...**..*
2d010 2a 20 5e 54 68 65 20 6e 61 6d 65 73 20 72 65 74  * ^The names ret
2d020 75 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72  urned are the or
2d030 69 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65  iginal un-aliase
2d040 64 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0d 0a  d names of the..
2d050 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 61 62  ** database, tab
2d060 6c 65 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0d  le, and column..
2d070 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 72  .**..** ^The fir
2d080 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
2d090 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 20  hese interfaces 
2d0a0 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  is a [prepared s
2d0b0 74 61 74 65 6d 65 6e 74 5d 2e 0d 0a 2a 2a 20 5e  tatement]...** ^
2d0c0 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  These functions 
2d0d0 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69  return informati
2d0e0 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4e 74 68  on about the Nth
2d0f0 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 72   result column r
2d100 65 74 75 72 6e 65 64 20 62 79 0d 0a 2a 2a 20 74  eturned by..** t
2d110 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68  he statement, wh
2d120 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63  ere N is the sec
2d130 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  ond function arg
2d140 75 6d 65 6e 74 2e 0d 0a 2a 2a 20 5e 54 68 65 20  ument...** ^The 
2d150 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2d160 20 69 73 20 63 6f 6c 75 6d 6e 20 30 20 66 6f 72   is column 0 for
2d170 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e   these routines.
2d180 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68 65  ..**..** ^If the
2d190 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75   Nth column retu
2d1a0 72 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 74  rned by the stat
2d1b0 65 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 72  ement is an expr
2d1c0 65 73 73 69 6f 6e 20 6f 72 0d 0a 2a 2a 20 73 75  ession or..** su
2d1d0 62 71 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f  bquery and is no
2d1e0 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  t a column value
2d1f0 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68  , then all of th
2d200 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65  ese functions re
2d210 74 75 72 6e 0d 0a 2a 2a 20 4e 55 4c 4c 2e 20 20  turn..** NULL.  
2d220 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 6d  ^These routine m
2d230 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72 6e  ight also return
2d240 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72   NULL if a memor
2d250 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
2d260 6f 72 0d 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20  or..** occurs.  
2d270 5e 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79  ^Otherwise, they
2d280 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   return the name
2d290 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64   of the attached
2d2a0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
2d2b0 2c 0d 0a 2a 2a 20 6f 72 20 63 6f 6c 75 6d 6e 20  ,..** or column 
2d2c0 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c  that query resul
2d2d0 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74  t column was ext
2d2e0 72 61 63 74 65 64 20 66 72 6f 6d 2e 0d 0a 2a 2a  racted from...**
2d2f0 0d 0a 2a 2a 20 5e 41 73 20 77 69 74 68 20 61 6c  ..** ^As with al
2d300 6c 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41  l other SQLite A
2d310 50 49 73 2c 20 74 68 6f 73 65 20 77 68 6f 73 65  PIs, those whose
2d320 20 6e 61 6d 65 73 20 65 6e 64 20 77 69 74 68 20   names end with 
2d330 22 31 36 22 20 72 65 74 75 72 6e 0d 0a 2a 2a 20  "16" return..** 
2d340 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73  UTF-16 encoded s
2d350 74 72 69 6e 67 73 20 61 6e 64 20 74 68 65 20 6f  trings and the o
2d360 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72  ther functions r
2d370 65 74 75 72 6e 20 55 54 46 2d 38 2e 0d 0a 2a 2a  eturn UTF-8...**
2d380 0d 0a 2a 2a 20 5e 54 68 65 73 65 20 41 50 49 73  ..** ^These APIs
2d390 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61   are only availa
2d3a0 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61  ble if the libra
2d3b0 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  ry was compiled 
2d3c0 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20 5b 53 51  with the..** [SQ
2d3d0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
2d3e0 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70  MN_METADATA] C-p
2d3f0 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62  reprocessor symb
2d400 6f 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  ol...**..** If t
2d410 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61  wo or more threa
2d420 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d  ds call one or m
2d430 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ore of these rou
2d440 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68  tines against th
2d450 65 20 73 61 6d 65 0d 0a 2a 2a 20 70 72 65 70 61  e same..** prepa
2d460 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  red statement an
2d470 64 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20  d column at the 
2d480 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74  same time then t
2d490 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 0d 0a  he results are..
2d4a0 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a  ** undefined...*
2d4b0 2a 0d 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20  *..** If two or 
2d4c0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c  more threads cal
2d4d0 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0d 0a 2a  l one or more..*
2d4e0 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
2d4f0 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
2d500 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 74  | column metadat
2d510 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0d 0a 2a  a interfaces]..*
2d520 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b  * for the same [
2d530 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2d540 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63  nt] and result c
2d550 6f 6c 75 6d 6e 0d 0a 2a 2a 20 61 74 20 74 68 65  olumn..** at the
2d560 20 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20   same time then 
2d570 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  the results are 
2d580 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a  undefined...*/..
2d590 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2d5a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
2d5b0 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
2d5c0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
2d5d0 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  *,int);..SQLITE_
2d5e0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2d5f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
2d600 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73  atabase_name16(s
2d610 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2d620 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  );..SQLITE_API c
2d630 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
2d640 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
2d650 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
2d660 74 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  t*,int);..SQLITE
2d670 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2d680 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
2d690 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c  table_name16(sql
2d6a0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2d6b0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
2d6c0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2d6d0 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
2d6e0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
2d6f0 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  *,int);..SQLITE_
2d700 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2d710 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
2d720 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c  rigin_name16(sql
2d730 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2d740 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
2d750 52 45 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61  REF: Declared Da
2d760 74 61 74 79 70 65 20 4f 66 20 41 20 51 75 65 72  tatype Of A Quer
2d770 79 20 52 65 73 75 6c 74 0d 0a 2a 2a 0d 0a 2a 2a  y Result..**..**
2d780 20 5e 28 54 68 65 20 66 69 72 73 74 20 70 61 72   ^(The first par
2d790 61 6d 65 74 65 72 20 69 73 20 61 20 5b 70 72 65  ameter is a [pre
2d7a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2d7b0 2e 0d 0a 2a 2a 20 49 66 20 74 68 69 73 20 73 74  ...** If this st
2d7c0 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 53 45  atement is a [SE
2d7d0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20  LECT] statement 
2d7e0 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  and the Nth colu
2d7f0 6d 6e 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 72 65  mn of the..** re
2d800 74 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 65  turned result se
2d810 74 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 43  t of that [SELEC
2d820 54 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 6f  T] is a table co
2d830 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0d 0a 2a 2a  lumn (not an..**
2d840 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73   expression or s
2d850 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 74 68  ubquery) then th
2d860 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 20  e declared type 
2d870 6f 66 20 74 68 65 20 74 61 62 6c 65 0d 0a 2a 2a  of the table..**
2d880 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72   column is retur
2d890 6e 65 64 2e 29 5e 20 20 5e 49 66 20 74 68 65 20  ned.)^  ^If the 
2d8a0 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  Nth column of th
2d8b0 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20  e result set is 
2d8c0 61 6e 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  an..** expressio
2d8d0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74  n or subquery, t
2d8e0 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  hen a NULL point
2d8f0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d  er is returned..
2d900 0a 2a 2a 20 5e 54 68 65 20 72 65 74 75 72 6e 65  .** ^The returne
2d910 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61  d string is alwa
2d920 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  ys UTF-8 encoded
2d930 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 46 6f 72 20  ...**..** ^(For 
2d940 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
2d950 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2d960 6d 61 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 43 52 45 41  ma:..**..** CREA
2d970 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 56  TE TABLE t1(c1 V
2d980 41 52 49 41 4e 54 29 3b 0d 0a 2a 2a 0d 0a 2a 2a  ARIANT);..**..**
2d990 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69   and the followi
2d9a0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  ng statement to 
2d9b0 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0d 0a 2a 2a  be compiled:..**
2d9c0 0d 0a 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b  ..** SELECT c1 +
2d9d0 20 31 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0d   1, c1 FROM t1;.
2d9e0 0a 2a 2a 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75  .**..** this rou
2d9f0 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 74 75 72  tine would retur
2da00 6e 20 74 68 65 20 73 74 72 69 6e 67 20 22 56 41  n the string "VA
2da10 52 49 41 4e 54 22 20 66 6f 72 20 74 68 65 20 73  RIANT" for the s
2da20 65 63 6f 6e 64 20 72 65 73 75 6c 74 0d 0a 2a 2a  econd result..**
2da30 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 2c 20   column (i==1), 
2da40 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  and a NULL point
2da50 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  er for the first
2da60 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 28   result column (
2da70 69 3d 3d 30 29 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  i==0).)^..**..**
2da80 20 5e 53 51 4c 69 74 65 20 75 73 65 73 20 64 79   ^SQLite uses dy
2da90 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65 20 74  namic run-time t
2daa0 79 70 69 6e 67 2e 20 20 5e 53 6f 20 6a 75 73 74  yping.  ^So just
2dab0 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c 75 6d   because a colum
2dac0 6e 0d 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 65  n..** is declare
2dad0 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70  d to contain a p
2dae0 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 64  articular type d
2daf0 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61  oes not mean tha
2db00 74 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 20 73  t the..** data s
2db10 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f  tored in that co
2db20 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64  lumn is of the d
2db30 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53  eclared type.  S
2db40 51 4c 69 74 65 20 69 73 0d 0a 2a 2a 20 73 74 72  QLite is..** str
2db50 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74  ongly typed, but
2db60 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64   the typing is d
2db70 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69  ynamic not stati
2db80 63 2e 20 20 5e 54 79 70 65 0d 0a 2a 2a 20 69 73  c.  ^Type..** is
2db90 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2dba0 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75   individual valu
2dbb0 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  es, not with the
2dbc0 20 63 6f 6e 74 61 69 6e 65 72 73 0d 0a 2a 2a 20   containers..** 
2dbd0 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f  used to hold tho
2dbe0 73 65 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f 0d 0a  se values...*/..
2dbf0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2dc00 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
2dc10 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73  olumn_decltype(s
2dc20 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2dc30 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  );..SQLITE_API c
2dc40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
2dc50 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
2dc60 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  pe16(sqlite3_stm
2dc70 74 2a 2c 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  t*,int);..../*..
2dc80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61  ** CAPI3REF: Eva
2dc90 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61  luate An SQL Sta
2dca0 74 65 6d 65 6e 74 0d 0a 2a 2a 0d 0a 2a 2a 20 41  tement..**..** A
2dcb0 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64  fter a [prepared
2dcc0 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20   statement] has 
2dcd0 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73  been prepared us
2dce0 69 6e 67 20 65 69 74 68 65 72 0d 0a 2a 2a 20 5b  ing either..** [
2dcf0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
2dd00 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  v2()] or [sqlite
2dd10 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29  3_prepare16_v2()
2dd20 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  ] or one of the 
2dd30 6c 65 67 61 63 79 0d 0a 2a 2a 20 69 6e 74 65 72  legacy..** inter
2dd40 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70  faces [sqlite3_p
2dd50 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71  repare()] or [sq
2dd60 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
2dd70 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  )], this functio
2dd80 6e 0d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61  n..** must be ca
2dd90 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  lled one or more
2dda0 20 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61   times to evalua
2ddb0 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  te the statement
2ddc0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 64 65  ...**..** The de
2ddd0 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62 65 68  tails of the beh
2dde0 61 76 69 6f 72 20 6f 66 20 74 68 65 20 73 71 6c  avior of the sql
2ddf0 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e 74 65  ite3_step() inte
2de00 72 66 61 63 65 20 64 65 70 65 6e 64 0d 0a 2a 2a  rface depend..**
2de10 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65 20   on whether the 
2de20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 72  statement was pr
2de30 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68 65  epared using the
2de40 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74 65   newer "v2" inte
2de50 72 66 61 63 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74  rface..** [sqlit
2de60 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
2de70 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
2de80 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72  epare16_v2()] or
2de90 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63   the older legac
2dea0 79 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  y..** interface 
2deb0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2dec0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2ded0 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20  _prepare16()].  
2dee0 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0d 0a  The use of the..
2def0 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65  ** new "v2" inte
2df00 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65  rface is recomme
2df10 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70  nded for new app
2df20 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68  lications but th
2df30 65 20 6c 65 67 61 63 79 0d 0a 2a 2a 20 69 6e 74  e legacy..** int
2df40 65 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74  erface will cont
2df50 69 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f  inue to be suppo
2df60 72 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49  rted...**..** ^I
2df70 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74  n the legacy int
2df80 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 74 75  erface, the retu
2df90 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65  rn value will be
2dfa0 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f   either [SQLITE_
2dfb0 42 55 53 59 5d 2c 0d 0a 2a 2a 20 5b 53 51 4c 49  BUSY],..** [SQLI
2dfc0 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54  TE_DONE], [SQLIT
2dfd0 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f  E_ROW], [SQLITE_
2dfe0 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49  ERROR], or [SQLI
2dff0 54 45 5f 4d 49 53 55 53 45 5d 2e 0d 0a 2a 2a 20  TE_MISUSE]...** 
2e000 5e 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69  ^With the "v2" i
2e010 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66  nterface, any of
2e020 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75   the other [resu
2e030 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0d 0a 2a 2a  lt codes] or..**
2e040 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
2e050 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62  t codes] might b
2e060 65 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 65  e returned as we
2e070 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 5b 53 51  ll...**..** ^[SQ
2e080 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73  LITE_BUSY] means
2e090 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
2e0a0 73 65 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e  se engine was un
2e0b0 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20  able to acquire 
2e0c0 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65  the..** database
2e0d0 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64 73 20   locks it needs 
2e0e0 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20  to do its job.  
2e0f0 5e 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ^If the statemen
2e100 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d 0d  t is a [COMMIT].
2e110 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75  .** or occurs ou
2e120 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c  tside of an expl
2e130 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  icit transaction
2e140 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72  , then you can r
2e150 65 74 72 79 20 74 68 65 0d 0a 2a 2a 20 73 74 61  etry the..** sta
2e160 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20  tement.  If the 
2e170 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74  statement is not
2e180 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20   a [COMMIT] and 
2e190 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 6e  occurs within an
2e1a0 0d 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72  ..** explicit tr
2e1b0 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79  ansaction then y
2e1c0 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61  ou should rollba
2e1d0 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
2e1e0 6f 6e 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 63 6f  on before..** co
2e1f0 6e 74 69 6e 75 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  ntinuing...**..*
2e200 2a 20 5e 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  * ^[SQLITE_DONE]
2e210 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2e220 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69  statement has fi
2e230 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67  nished executing
2e240 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c  ..** successfull
2e250 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  y.  sqlite3_step
2e260 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  () should not be
2e270 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e   called again on
2e280 20 74 68 69 73 20 76 69 72 74 75 61 6c 0d 0a 2a   this virtual..*
2e290 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75  * machine withou
2e2a0 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20  t first calling 
2e2b0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
2e2c0 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76  ] to reset the v
2e2d0 69 72 74 75 61 6c 0d 0a 2a 2a 20 6d 61 63 68 69  irtual..** machi
2e2e0 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69  ne back to its i
2e2f0 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 0d 0a 2a  nitial state...*
2e300 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 53 51  *..** ^If the SQ
2e310 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  L statement bein
2e320 67 20 65 78 65 63 75 74 65 64 20 72 65 74 75 72  g executed retur
2e330 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65  ns any data, the
2e340 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0d 0a  n [SQLITE_ROW]..
2e350 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 65  ** is returned e
2e360 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77 20 72  ach time a new r
2e370 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65  ow of data is re
2e380 61 64 79 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ady for processi
2e390 6e 67 20 62 79 20 74 68 65 0d 0a 2a 2a 20 63 61  ng by the..** ca
2e3a0 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73  ller. The values
2e3b0 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
2e3c0 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c 75   using the [colu
2e3d0 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69  mn access functi
2e3e0 6f 6e 73 5d 2e 0d 0a 2a 2a 20 73 71 6c 69 74 65  ons]...** sqlite
2e3f0 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c  3_step() is call
2e400 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 72  ed again to retr
2e410 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 6f  ieve the next ro
2e420 77 20 6f 66 20 64 61 74 61 2e 0d 0a 2a 2a 0d 0a  w of data...**..
2e430 2a 2a 20 5e 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ** ^[SQLITE_ERRO
2e440 52 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  R] means that a 
2e450 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28  run-time error (
2e460 73 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72  such as a constr
2e470 61 69 6e 74 0d 0a 2a 2a 20 76 69 6f 6c 61 74 69  aint..** violati
2e480 6f 6e 29 20 68 61 73 20 6f 63 63 75 72 72 65 64  on) has occurred
2e490 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
2e4a0 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ) should not be 
2e4b0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0d  called again on.
2e4c0 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65  .** the VM. More
2e4d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79   information may
2e4e0 20 62 65 20 66 6f 75 6e 64 20 62 79 20 63 61 6c   be found by cal
2e4f0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 72  ling [sqlite3_er
2e500 72 6d 73 67 28 29 5d 2e 0d 0a 2a 2a 20 5e 57 69  rmsg()]...** ^Wi
2e510 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  th the legacy in
2e520 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 20  terface, a more 
2e530 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63  specific error c
2e540 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ode (for example
2e550 2c 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e  ,..** [SQLITE_IN
2e560 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54  TERRUPT], [SQLIT
2e570 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49  E_SCHEMA], [SQLI
2e580 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64  TE_CORRUPT], and
2e590 20 73 6f 20 66 6f 72 74 68 29 0d 0a 2a 2a 20 63   so forth)..** c
2e5a0 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62  an be obtained b
2e5b0 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  y calling [sqlit
2e5c0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74  e3_reset()] on t
2e5d0 68 65 0d 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64  he..** [prepared
2e5e0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 5e 49   statement].  ^I
2e5f0 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  n the "v2" inter
2e600 66 61 63 65 2c 0d 0a 2a 2a 20 74 68 65 20 6d 6f  face,..** the mo
2e610 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f  re specific erro
2e620 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
2e630 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73  ed directly by s
2e640 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0d 0a  qlite3_step()...
2e650 2a 2a 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d  **..** [SQLITE_M
2e660 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 68 61  ISUSE] means tha
2e670 74 20 74 68 65 20 74 68 69 73 20 72 6f 75 74 69  t the this routi
2e680 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 69 6e  ne was called in
2e690 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0d 0a  appropriately...
2e6a0 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 77 61  ** Perhaps it wa
2e6b0 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b 70  s called on a [p
2e6c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2e6d0 74 5d 20 74 68 61 74 20 68 61 73 0d 0a 2a 2a 20  t] that has..** 
2e6e0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 71  already been [sq
2e6f0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c  lite3_finalize |
2e700 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f   finalized] or o
2e710 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0d 0a  n one that had..
2e720 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ** previously re
2e730 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45  turned [SQLITE_E
2e740 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45  RROR] or [SQLITE
2e750 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63  _DONE].  Or it c
2e760 6f 75 6c 64 0d 0a 2a 2a 20 62 65 20 74 68 65 20  ould..** be the 
2e770 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 61  case that the sa
2e780 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
2e790 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  ection is being 
2e7a0 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0d 0a  used by two or..
2e7b0 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20  ** more threads 
2e7c0 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65  at the same mome
2e7d0 6e 74 20 69 6e 20 74 69 6d 65 2e 0d 0a 2a 2a 0d  nt in time...**.
2e7e0 0a 2a 2a 20 46 6f 72 20 61 6c 6c 20 76 65 72 73  .** For all vers
2e7f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75  ions of SQLite u
2e800 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69  p to and includi
2e810 6e 67 20 33 2e 36 2e 32 33 2e 31 2c 20 61 20 63  ng 3.6.23.1, a c
2e820 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69  all to..** [sqli
2e830 74 65 33 5f 72 65 73 65 74 28 29 5d 20 77 61 73  te3_reset()] was
2e840 20 72 65 71 75 69 72 65 64 20 61 66 74 65 72 20   required after 
2e850 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 72  sqlite3_step() r
2e860 65 74 75 72 6e 65 64 20 61 6e 79 74 68 69 6e 67  eturned anything
2e870 0d 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20  ..** other than 
2e880 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 62 65 66  [SQLITE_ROW] bef
2e890 6f 72 65 20 61 6e 79 20 73 75 62 73 65 71 75 65  ore any subseque
2e8a0 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  nt invocation of
2e8b0 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65  ..** sqlite3_ste
2e8c0 70 28 29 2e 20 20 46 61 69 6c 75 72 65 20 74 6f  p().  Failure to
2e8d0 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61   reset the prepa
2e8e0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 75 73  red statement us
2e8f0 69 6e 67 20 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65  ing ..** [sqlite
2e900 33 5f 72 65 73 65 74 28 29 5d 20 77 6f 75 6c 64  3_reset()] would
2e910 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 5b 53   result in an [S
2e920 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 72 65  QLITE_MISUSE] re
2e930 74 75 72 6e 20 66 72 6f 6d 0d 0a 2a 2a 20 73 71  turn from..** sq
2e940 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 42  lite3_step().  B
2e950 75 74 20 61 66 74 65 72 20 76 65 72 73 69 6f 6e  ut after version
2e960 20 33 2e 36 2e 32 33 2e 31 2c 20 73 71 6c 69 74   3.6.23.1, sqlit
2e970 65 33 5f 73 74 65 70 28 29 20 62 65 67 61 6e 0d  e3_step() began.
2e980 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c  .** calling [sql
2e990 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 61 75  ite3_reset()] au
2e9a0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 74  tomatically in t
2e9b0 68 69 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65  his circumstance
2e9c0 20 72 61 74 68 65 72 0d 0a 2a 2a 20 74 68 61 6e   rather..** than
2e9d0 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49   returning [SQLI
2e9e0 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 54 68 69  TE_MISUSE].  Thi
2e9f0 73 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  s is not conside
2ea00 72 65 64 20 61 20 63 6f 6d 70 61 74 69 62 69 6c  red a compatibil
2ea10 69 74 79 0d 0a 2a 2a 20 62 72 65 61 6b 20 62 65  ity..** break be
2ea20 63 61 75 73 65 20 61 6e 79 20 61 70 70 6c 69 63  cause any applic
2ea30 61 74 69 6f 6e 20 74 68 61 74 20 65 76 65 72 20  ation that ever 
2ea40 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49  receives an SQLI
2ea50 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 0d  TE_MISUSE error.
2ea60 0a 2a 2a 20 69 73 20 62 72 6f 6b 65 6e 20 62 79  .** is broken by
2ea70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68   definition.  Th
2ea80 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  e [SQLITE_OMIT_A
2ea90 55 54 4f 52 45 53 45 54 5d 20 63 6f 6d 70 69 6c  UTORESET] compil
2eaa0 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 0d 0a 2a  e-time option..*
2eab0 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  * can be used to
2eac0 20 72 65 73 74 6f 72 65 20 74 68 65 20 6c 65 67   restore the leg
2ead0 61 63 79 20 62 65 68 61 76 69 6f 72 2e 0d 0a 2a  acy behavior...*
2eae0 2a 0d 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49  *..** <b>Goofy I
2eaf0 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c  nterface Alert:<
2eb00 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 63  /b> In the legac
2eb10 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  y interface, the
2eb20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0d   sqlite3_step().
2eb30 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72  .** API always r
2eb40 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63  eturns a generic
2eb50 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51   error code, [SQ
2eb60 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c  LITE_ERROR], fol
2eb70 6c 6f 77 69 6e 67 20 61 6e 79 0d 0a 2a 2a 20 65  lowing any..** e
2eb80 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20  rror other than 
2eb90 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e  [SQLITE_BUSY] an
2eba0 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  d [SQLITE_MISUSE
2ebb0 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c  ].  You must cal
2ebc0 6c 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  l..** [sqlite3_r
2ebd0 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69  eset()] or [sqli
2ebe0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
2ebf0 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64  in order to find
2ec00 20 6f 6e 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20   one of the..** 
2ec10 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
2ec20 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74  codes] that bett
2ec30 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65  er describes the
2ec40 20 65 72 72 6f 72 2e 0d 0a 2a 2a 20 57 65 20 61   error...** We a
2ec50 64 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69  dmit that this i
2ec60 73 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e  s a goofy design
2ec70 2e 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68  .  The problem h
2ec80 61 73 20 62 65 65 6e 20 66 69 78 65 64 0d 0a 2a  as been fixed..*
2ec90 2a 20 77 69 74 68 20 74 68 65 20 22 76 32 22 20  * with the "v2" 
2eca0 69 6e 74 65 72 66 61 63 65 2e 20 20 49 66 20 79  interface.  If y
2ecb0 6f 75 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f  ou prepare all o
2ecc0 66 20 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65  f your SQL state
2ecd0 6d 65 6e 74 73 0d 0a 2a 2a 20 75 73 69 6e 67 20  ments..** using 
2ece0 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f  either [sqlite3_
2ecf0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72  prepare_v2()] or
2ed00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
2ed10 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 74 65 61  e16_v2()] instea
2ed20 64 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65 67  d..** of the leg
2ed30 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  acy [sqlite3_pre
2ed40 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  pare()] and [sql
2ed50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29  ite3_prepare16()
2ed60 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0d 0a 2a  ] interfaces,..*
2ed70 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 20  * then the more 
2ed80 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
2ed90 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72  codes] are retur
2eda0 6e 65 64 20 64 69 72 65 63 74 6c 79 0d 0a 2a 2a  ned directly..**
2edb0 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70   by sqlite3_step
2edc0 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  ().  The use of 
2edd0 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
2ede0 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  ce is recommende
2edf0 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  d...*/..SQLITE_A
2ee00 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
2ee10 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  tep(sqlite3_stmt
2ee20 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  *);..../*..** CA
2ee30 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f  PI3REF: Number o
2ee40 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72  f columns in a r
2ee50 65 73 75 6c 74 20 73 65 74 0d 0a 2a 2a 0d 0a 2a  esult set..**..*
2ee60 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64  * ^The sqlite3_d
2ee70 61 74 61 5f 63 6f 75 6e 74 28 50 29 20 69 6e 74  ata_count(P) int
2ee80 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74  erface returns t
2ee90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
2eea0 75 6d 6e 73 20 69 6e 20 74 68 65 0d 0a 2a 2a 20  umns in the..** 
2eeb0 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
2eec0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2eed0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2eee0 6d 65 6e 74 5d 20 50 2e 0d 0a 2a 2a 20 5e 49 66  ment] P...** ^If
2eef0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2ef00 65 6e 74 20 50 20 64 6f 65 73 20 6e 6f 74 20 68  ent P does not h
2ef10 61 76 65 20 72 65 73 75 6c 74 73 20 72 65 61 64  ave results read
2ef20 79 20 74 6f 20 72 65 74 75 72 6e 0d 0a 2a 2a 20  y to return..** 
2ef30 28 76 69 61 20 63 61 6c 6c 73 20 74 6f 20 74 68  (via calls to th
2ef40 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e [sqlite3_colum
2ef50 6e 5f 69 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f  n_int | sqlite3_
2ef60 63 6f 6c 75 6d 6e 5f 2a 28 29 5d 20 6f 66 0d 0a  column_*()] of..
2ef70 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 29 20 74  ** interfaces) t
2ef80 68 65 6e 20 73 71 6c 69 74 65 33 5f 64 61 74 61  hen sqlite3_data
2ef90 5f 63 6f 75 6e 74 28 50 29 20 72 65 74 75 72 6e  _count(P) return
2efa0 73 20 30 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73 71  s 0...** ^The sq
2efb0 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
2efc0 28 50 29 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f  (P) routine also
2efd0 20 72 65 74 75 72 6e 73 20 30 20 69 66 20 50 20   returns 0 if P 
2efe0 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
2eff0 72 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  r...** ^The sqli
2f000 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 50  te3_data_count(P
2f010 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  ) routine return
2f020 73 20 30 20 69 66 20 74 68 65 20 70 72 65 76 69  s 0 if the previ
2f030 6f 75 73 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20  ous call to..** 
2f040 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 28 50  [sqlite3_step](P
2f050 29 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  ) returned [SQLI
2f060 54 45 5f 44 4f 4e 45 5d 2e 20 20 5e 54 68 65 20  TE_DONE].  ^The 
2f070 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
2f080 6e 74 28 50 29 0d 0a 2a 2a 20 77 69 6c 6c 20 72  nt(P)..** will r
2f090 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
2f0a0 66 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  f previous call 
2f0b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  to [sqlite3_step
2f0c0 5d 28 50 29 20 72 65 74 75 72 6e 65 64 0d 0a 2a  ](P) returned..*
2f0d0 2a 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20  * [SQLITE_ROW], 
2f0e0 65 78 63 65 70 74 20 69 6e 20 74 68 65 20 63 61  except in the ca
2f0f0 73 65 20 6f 66 20 74 68 65 20 5b 50 52 41 47 4d  se of the [PRAGM
2f100 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61  A incremental_va
2f110 63 75 75 6d 5d 0d 0a 2a 2a 20 77 68 65 72 65 20  cuum]..** where 
2f120 69 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  it always return
2f130 73 20 7a 65 72 6f 20 73 69 6e 63 65 20 65 61 63  s zero since eac
2f140 68 20 73 74 65 70 20 6f 66 20 74 68 61 74 20 6d  h step of that m
2f150 75 6c 74 69 2d 73 74 65 70 0d 0a 2a 2a 20 70 72  ulti-step..** pr
2f160 61 67 6d 61 20 72 65 74 75 72 6e 73 20 30 20 63  agma returns 0 c
2f170 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 2e 0d  olumns of data..
2f180 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  .**..** See also
2f190 3a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  : [sqlite3_colum
2f1a0 6e 5f 63 6f 75 6e 74 28 29 5d 0d 0a 2a 2f 0d 0a  n_count()]..*/..
2f1b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2f1c0 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
2f1d0 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
2f1e0 70 53 74 6d 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  pStmt);..../*..*
2f1f0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64  * CAPI3REF: Fund
2f200 61 6d 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65  amental Datatype
2f210 73 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  s..** KEYWORDS: 
2f220 53 51 4c 49 54 45 5f 54 45 58 54 0d 0a 2a 2a 0d  SQLITE_TEXT..**.
2f230 0a 2a 2a 20 5e 28 45 76 65 72 79 20 76 61 6c 75  .** ^(Every valu
2f240 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61 73 20  e in SQLite has 
2f250 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75 6e 64  one of five fund
2f260 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79 70 65  amental datatype
2f270 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d  s:..**..** <ul>.
2f280 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
2f290 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0d 0a  signed integer..
2f2a0 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49  ** <li> 64-bit I
2f2b0 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  EEE floating poi
2f2c0 6e 74 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 3c 6c  nt number..** <l
2f2d0 69 3e 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 3c 6c  i> string..** <l
2f2e0 69 3e 20 42 4c 4f 42 0d 0a 2a 2a 20 3c 6c 69 3e  i> BLOB..** <li>
2f2f0 20 4e 55 4c 4c 0d 0a 2a 2a 20 3c 2f 75 6c 3e 29   NULL..** </ul>)
2f300 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20  ^..**..** These 
2f310 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 6f  constants are co
2f320 64 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20  des for each of 
2f330 74 68 6f 73 65 20 74 79 70 65 73 2e 0d 0a 2a 2a  those types...**
2f340 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  ..** Note that t
2f350 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 63  he SQLITE_TEXT c
2f360 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c 73 6f  onstant was also
2f370 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 20   used in SQLite 
2f380 76 65 72 73 69 6f 6e 20 32 0d 0a 2a 2a 20 66 6f  version 2..** fo
2f390 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64  r a completely d
2f3a0 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e 67  ifferent meaning
2f3b0 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 74  .  Software that
2f3c0 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 62   links against b
2f3d0 6f 74 68 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 76  oth..** SQLite v
2f3e0 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c  ersion 2 and SQL
2f3f0 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68  ite version 3 sh
2f400 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33  ould use SQLITE3
2f410 5f 54 45 58 54 2c 20 6e 6f 74 0d 0a 2a 2a 20 53  _TEXT, not..** S
2f420 51 4c 49 54 45 5f 54 45 58 54 2e 0d 0a 2a 2f 0d  QLITE_TEXT...*/.
2f430 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
2f440 49 4e 54 45 47 45 52 20 20 31 0d 0a 23 64 65 66  INTEGER  1..#def
2f450 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ine SQLITE_FLOAT
2f460 20 20 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 53      2..#define S
2f470 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34  QLITE_BLOB     4
2f480 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
2f490 5f 4e 55 4c 4c 20 20 20 20 20 35 0d 0a 23 69 66  _NULL     5..#if
2f4a0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0d  def SQLITE_TEXT.
2f4b0 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
2f4c0 54 45 58 54 0d 0a 23 65 6c 73 65 0d 0a 23 20 64  TEXT..#else..# d
2f4d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58  efine SQLITE_TEX
2f4e0 54 20 20 20 20 20 33 0d 0a 23 65 6e 64 69 66 0d  T     3..#endif.
2f4f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33  .#define SQLITE3
2f500 5f 54 45 58 54 20 20 20 20 20 33 0d 0a 0d 0a 2f  _TEXT     3..../
2f510 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  *..** CAPI3REF: 
2f520 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 46 72  Result Values Fr
2f530 6f 6d 20 41 20 51 75 65 72 79 0d 0a 2a 2a 20 4b  om A Query..** K
2f540 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e  EYWORDS: {column
2f550 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e   access function
2f560 73 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65  s}..**..** These
2f570 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 74   routines form t
2f580 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 22 20  he "result set" 
2f590 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a  interface...**..
2f5a0 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 69 6e  ** ^These routin
2f5b0 65 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d  es return inform
2f5c0 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69  ation about a si
2f5d0 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  ngle column of t
2f5e0 68 65 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 72  he current..** r
2f5f0 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20 71  esult row of a q
2f600 75 65 72 79 2e 20 20 5e 49 6e 20 65 76 65 72 79  uery.  ^In every
2f610 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74 20   case the first 
2f620 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f  argument is a po
2f630 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68 65  inter..** to the
2f640 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2f650 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65  ment] that is be
2f660 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74  ing evaluated (t
2f670 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
2f680 2a 5d 0d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  *]..** that was 
2f690 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73  returned from [s
2f6a0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
2f6b0 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69  2()] or one of i
2f6c0 74 73 20 76 61 72 69 61 6e 74 73 29 0d 0a 2a 2a  ts variants)..**
2f6d0 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
2f6e0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2f6f0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c  index of the col
2f700 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e  umn for which in
2f710 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 73 68  formation..** sh
2f720 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64  ould be returned
2f730 2e 20 5e 54 68 65 20 6c 65 66 74 6d 6f 73 74 20  . ^The leftmost 
2f740 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
2f750 73 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 65  sult set has the
2f760 20 69 6e 64 65 78 20 30 2e 0d 0a 2a 2a 20 5e 54   index 0...** ^T
2f770 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
2f780 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
2f790 6c 74 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d  lt can be determ
2f7a0 69 6e 65 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 5b  ined using..** [
2f7b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
2f7c0 6f 75 6e 74 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a  ount()]...**..**
2f7d0 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
2f7e0 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 63  ement does not c
2f7f0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74  urrently point t
2f800 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f  o a valid row, o
2f810 72 20 69 66 20 74 68 65 0d 0a 2a 2a 20 63 6f 6c  r if the..** col
2f820 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74  umn index is out
2f830 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72   of range, the r
2f840 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e  esult is undefin
2f850 65 64 2e 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ed...** These ro
2f860 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20  utines may only 
2f870 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  be called when t
2f880 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
2f890 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69  all to..** [sqli
2f8a0 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20  te3_step()] has 
2f8b0 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
2f8c0 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 65  _ROW] and neithe
2f8d0 72 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  r..** [sqlite3_r
2f8e0 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c  eset()] nor [sql
2f8f0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
2f900 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
2f910 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e 0d  d subsequently..
2f920 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68  .** If any of th
2f930 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2f940 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73   called after [s
2f950 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
2f960 6f 72 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  or..** [sqlite3_
2f970 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61  finalize()] or a
2f980 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74  fter [sqlite3_st
2f990 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e  ep()] has return
2f9a0 65 64 0d 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67  ed..** something
2f9b0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c   other than [SQL
2f9c0 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65  ITE_ROW], the re
2f9d0 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69  sults are undefi
2f9e0 6e 65 64 2e 0d 0a 2a 2a 20 49 66 20 5b 73 71 6c  ned...** If [sql
2f9f0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20  ite3_step()] or 
2fa00 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
2fa10 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69  ] or [sqlite3_fi
2fa20 6e 61 6c 69 7a 65 28 29 5d 0d 0a 2a 2a 20 61 72  nalize()]..** ar
2fa30 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20  e called from a 
2fa40 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
2fa50 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68   while any of th
2fa60 65 73 65 20 72 6f 75 74 69 6e 65 73 0d 0a 2a 2a  ese routines..**
2fa70 20 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68   are pending, th
2fa80 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
2fa90 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a  re undefined...*
2faa0 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  *..** ^The sqlit
2fab0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
2fac0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2fad0 20 74 68 65 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45   the..** [SQLITE
2fae0 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74  _INTEGER | datat
2faf0 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20 74 68  ype code] for th
2fb00 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 20 74  e initial data t
2fb10 79 70 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 72  ype..** of the r
2fb20 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 5e  esult column.  ^
2fb30 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
2fb40 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51  ue is one of [SQ
2fb50 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0d 0a  LITE_INTEGER],..
2fb60 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ** [SQLITE_FLOAT
2fb70 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d  ], [SQLITE_TEXT]
2fb80 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c  , [SQLITE_BLOB],
2fb90 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c   or [SQLITE_NULL
2fba0 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 0d 0a 2a  ].  The value..*
2fbb0 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  * returned by sq
2fbc0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
2fbd0 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e  e() is only mean
2fbe0 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70  ingful if no typ
2fbf0 65 0d 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e  e..** conversion
2fc00 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20  s have occurred 
2fc10 61 73 20 64 65 73 63 72 69 62 65 64 20 62 65 6c  as described bel
2fc20 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 79 70  ow.  After a typ
2fc30 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0d 0a 2a  e conversion,..*
2fc40 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  * the value retu
2fc50 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
2fc60 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73  column_type() is
2fc70 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 74   undefined.  Fut
2fc80 75 72 65 0d 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  ure..** versions
2fc90 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63   of SQLite may c
2fca0 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69  hange the behavi
2fcb0 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f  or of sqlite3_co
2fcc0 6c 75 6d 6e 5f 74 79 70 65 28 29 0d 0a 2a 2a 20  lumn_type()..** 
2fcd0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65  following a type
2fce0 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0d 0a 2a 2a   conversion...**
2fcf0 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73  ..** ^If the res
2fd00 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72  ult is a BLOB or
2fd10 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68   UTF-8 string th
2fd20 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  en the sqlite3_c
2fd30 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0d 0a 2a  olumn_bytes()..*
2fd40 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  * routine return
2fd50 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2fd60 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c  bytes in that BL
2fd70 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0d 0a 2a  OB or string...*
2fd80 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 6c 74  * ^If the result
2fd90 20 69 73 20 61 20 55 54 46 2d 31 36 20 73 74 72   is a UTF-16 str
2fda0 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65  ing, then sqlite
2fdb0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
2fdc0 20 63 6f 6e 76 65 72 74 73 0d 0a 2a 2a 20 74 68   converts..** th
2fdd0 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d  e string to UTF-
2fde0 38 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72  8 and then retur
2fdf0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
2fe00 20 62 79 74 65 73 2e 0d 0a 2a 2a 20 5e 49 66 20   bytes...** ^If 
2fe10 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  the result is a 
2fe20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 68  numeric value th
2fe30 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  en sqlite3_colum
2fe40 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0d 0a  n_bytes() uses..
2fe50 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72  ** [sqlite3_snpr
2fe60 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65  intf()] to conve
2fe70 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f  rt that value to
2fe80 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20   a UTF-8 string 
2fe90 61 6e 64 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20  and returns..** 
2fea0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
2feb0 74 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69  tes in that stri
2fec0 6e 67 2e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20  ng...** ^If the 
2fed0 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2c 20  result is NULL, 
2fee0 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
2fef0 75 6d 6e 5f 62 79 74 65 73 28 29 20 72 65 74 75  umn_bytes() retu
2ff00 72 6e 73 20 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a  rns zero...**..*
2ff10 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 6c 74  * ^If the result
2ff20 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54   is a BLOB or UT
2ff30 46 2d 31 36 20 73 74 72 69 6e 67 20 74 68 65 6e  F-16 string then
2ff40 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   the sqlite3_col
2ff50 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0d 0a 2a  umn_bytes16()..*
2ff60 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  * routine return
2ff70 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2ff80 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c  bytes in that BL
2ff90 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0d 0a 2a  OB or string...*
2ffa0 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 6c 74  * ^If the result
2ffb0 20 69 73 20 61 20 55 54 46 2d 38 20 73 74 72 69   is a UTF-8 stri
2ffc0 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ng, then sqlite3
2ffd0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
2ffe0 29 20 63 6f 6e 76 65 72 74 73 0d 0a 2a 2a 20 74  ) converts..** t
2fff0 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46  he string to UTF
30000 2d 31 36 20 61 6e 64 20 74 68 65 6e 20 72 65 74  -16 and then ret
30010 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
30020 6f 66 20 62 79 74 65 73 2e 0d 0a 2a 2a 20 5e 49  of bytes...** ^I
30030 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
30040 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
30050 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
30060 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 75 73  umn_bytes16() us
30070 65 73 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  es..** [sqlite3_
30080 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 63  snprintf()] to c
30090 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c 75  onvert that valu
300a0 65 20 74 6f 20 61 20 55 54 46 2d 31 36 20 73 74  e to a UTF-16 st
300b0 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
300c0 0d 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  ..** the number 
300d0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74  of bytes in that
300e0 20 73 74 72 69 6e 67 2e 0d 0a 2a 2a 20 5e 49 66   string...** ^If
300f0 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
30100 55 4c 4c 2c 20 74 68 65 6e 20 73 71 6c 69 74 65  ULL, then sqlite
30110 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
30120 28 29 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e  () returns zero.
30130 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 76 61  ..**..** ^The va
30140 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  lues returned by
30150 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
30160 5f 62 79 74 65 73 28 29 5d 20 61 6e 64 20 0d 0a  _bytes()] and ..
30170 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
30180 6d 6e 5f 62 79 74 65 73 31 36 28 29 5d 20 64 6f  mn_bytes16()] do
30190 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65   not include the
301a0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
301b0 73 20 61 74 20 74 68 65 20 65 6e 64 0d 0a 2a 2a  s at the end..**
301c0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20   of the string. 
301d0 20 5e 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74   ^For clarity: t
301e0 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
301f0 65 64 20 62 79 0d 0a 2a 2a 20 5b 73 71 6c 69 74  ed by..** [sqlit
30200 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
30210 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
30220 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
30230 5d 20 61 72 65 20 74 68 65 20 6e 75 6d 62 65 72  ] are the number
30240 20 6f 66 0d 0a 2a 2a 20 62 79 74 65 73 20 69 6e   of..** bytes in
30250 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74   the string, not
30260 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
30270 68 61 72 61 63 74 65 72 73 2e 0d 0a 2a 2a 0d 0a  haracters...**..
30280 2a 2a 20 5e 53 74 72 69 6e 67 73 20 72 65 74 75  ** ^Strings retu
30290 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
302a0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e  column_text() an
302b0 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  d sqlite3_column
302c0 5f 74 65 78 74 31 36 28 29 2c 0d 0a 2a 2a 20 65  _text16(),..** e
302d0 76 65 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67  ven empty string
302e0 73 2c 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65  s, are always ze
302f0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro-terminated.  
30300 5e 54 68 65 20 72 65 74 75 72 6e 0d 0a 2a 2a 20  ^The return..** 
30310 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 74  value from sqlit
30320 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
30330 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e 67   for a zero-leng
30340 74 68 20 42 4c 4f 42 20 69 73 20 61 20 4e 55 4c  th BLOB is a NUL
30350 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a  L pointer...**..
30360 2a 2a 20 5e 54 68 65 20 6f 62 6a 65 63 74 20 72  ** ^The object r
30370 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69  eturned by [sqli
30380 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
30390 28 29 5d 20 69 73 20 61 6e 0d 0a 2a 2a 20 5b 75  ()] is an..** [u
303a0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
303b0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
303c0 2e 20 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65  .  An unprotecte
303d0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  d sqlite3_value 
303e0 6f 62 6a 65 63 74 0d 0a 2a 2a 20 6d 61 79 20 6f  object..** may o
303f0 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68  nly be used with
30400 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76   [sqlite3_bind_v
30410 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  alue()] and [sql
30420 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
30430 65 28 29 5d 2e 0d 0a 2a 2a 20 49 66 20 74 68 65  e()]...** If the
30440 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   [unprotected sq
30450 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
30460 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 0d  ect returned by.
30470 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
30480 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20  umn_value()] is 
30490 75 73 65 64 20 69 6e 20 61 6e 79 20 6f 74 68 65  used in any othe
304a0 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 69 6e 67  r way, including
304b0 20 63 61 6c 6c 73 0d 0a 2a 2a 20 74 6f 20 72 6f   calls..** to ro
304c0 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c  utines like [sql
304d0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 29  ite3_value_int()
304e0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
304f0 65 5f 74 65 78 74 28 29 5d 2c 0d 0a 2a 2a 20 6f  e_text()],..** o
30500 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r [sqlite3_value
30510 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e 20  _bytes()], then 
30520 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
30530 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a  undefined...**..
30540 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
30550 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e  s attempt to con
30560 76 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77  vert the value w
30570 68 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65  here appropriate
30580 2e 20 20 5e 46 6f 72 0d 0a 2a 2a 20 65 78 61 6d  .  ^For..** exam
30590 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65  ple, if the inte
305a0 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
305b0 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64  ion is FLOAT and
305c0 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0d 0a   a text result..
305d0 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ** is requested,
305e0 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e   [sqlite3_snprin
305f0 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e  tf()] is used in
30600 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66  ternally to perf
30610 6f 72 6d 20 74 68 65 0d 0a 2a 2a 20 63 6f 6e 76  orm the..** conv
30620 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63  ersion automatic
30630 61 6c 6c 79 2e 20 20 5e 28 54 68 65 20 66 6f 6c  ally.  ^(The fol
30640 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74  lowing table det
30650 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73  ails the convers
30660 69 6f 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 61 72  ions..** that ar
30670 65 20 61 70 70 6c 69 65 64 3a 0d 0a 2a 2a 0d 0a  e applied:..**..
30680 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0d  ** <blockquote>.
30690 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65  .** <table borde
306a0 72 3d 22 31 22 3e 0d 0a 2a 2a 20 3c 74 72 3e 3c  r="1">..** <tr><
306b0 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e  th> Internal<br>
306c0 54 79 70 65 20 3c 74 68 3e 20 52 65 71 75 65 73  Type <th> Reques
306d0 74 65 64 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  ted<br>Type <th>
306e0 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0d 0a 2a 2a    Conversion..**
306f0 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e  ..** <tr><td>  N
30700 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54 45  ULL    <td> INTE
30710 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 75 6c  GER   <td> Resul
30720 74 20 69 73 20 30 0d 0a 2a 2a 20 3c 74 72 3e 3c  t is 0..** <tr><
30730 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
30740 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e  >  FLOAT    <td>
30750 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0d 0a   Result is 0.0..
30760 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
30770 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  L    <td>   TEXT
30780 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
30790 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0d  is NULL pointer.
307a0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55  .** <tr><td>  NU
307b0 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f  LL    <td>   BLO
307c0 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74  B    <td> Result
307d0 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72   is NULL pointer
307e0 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  ..** <tr><td> IN
307f0 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f  TEGER  <td>  FLO
30800 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  AT    <td> Conve
30810 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 20  rt from integer 
30820 74 6f 20 66 6c 6f 61 74 0d 0a 2a 2a 20 3c 74 72  to float..** <tr
30830 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
30840 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74  td>   TEXT    <t
30850 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69  d> ASCII renderi
30860 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ng of the intege
30870 72 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  r..** <tr><td> I
30880 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42  NTEGER  <td>   B
30890 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65  LOB    <td> Same
308a0 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58   as INTEGER->TEX
308b0 54 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  T..** <tr><td>  
308c0 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54  FLOAT   <td> INT
308d0 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76  EGER   <td> Conv
308e0 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74  ert from float t
308f0 6f 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 3c 74  o integer..** <t
30900 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
30910 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
30920 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72  td> ASCII render
30930 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74  ing of the float
30940 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46  ..** <tr><td>  F
30950 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c  LOAT   <td>   BL
30960 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
30970 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0d 0a  as FLOAT->TEXT..
30980 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
30990 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45  T    <td> INTEGE
309a0 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f  R   <td> Use ato
309b0 69 28 29 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  i()..** <tr><td>
309c0 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20    TEXT    <td>  
309d0 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73  FLOAT    <td> Us
309e0 65 20 61 74 6f 66 28 29 0d 0a 2a 2a 20 3c 74 72  e atof()..** <tr
309f0 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c  ><td>  TEXT    <
30a00 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74  td>   BLOB    <t
30a10 64 3e 20 4e 6f 20 63 68 61 6e 67 65 0d 0a 2a 2a  d> No change..**
30a20 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20   <tr><td>  BLOB 
30a30 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
30a40 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74    <td> Convert t
30a50 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 20  o TEXT then use 
30a60 61 74 6f 69 28 29 0d 0a 2a 2a 20 3c 74 72 3e 3c  atoi()..** <tr><
30a70 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  td>  BLOB    <td
30a80 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e  >  FLOAT    <td>
30a90 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54   Convert to TEXT
30aa0 20 74 68 65 6e 20 75 73 65 20 61 74 6f 66 28 29   then use atof()
30ab0 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42  ..** <tr><td>  B
30ac0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 20 54 45  LOB    <td>   TE
30ad0 58 54 20 20 20 20 3c 74 64 3e 20 41 64 64 20 61  XT    <td> Add a
30ae0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
30af0 20 69 66 20 6e 65 65 64 65 64 0d 0a 2a 2a 20 3c   if needed..** <
30b00 2f 74 61 62 6c 65 3e 0d 0a 2a 2a 20 3c 2f 62 6c  /table>..** </bl
30b10 6f 63 6b 71 75 6f 74 65 3e 29 5e 0d 0a 2a 2a 0d  ockquote>)^..**.
30b20 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 62  .** The table ab
30b30 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 65  ove makes refere
30b40 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 20  nce to standard 
30b50 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  C library functi
30b60 6f 6e 73 20 61 74 6f 69 28 29 0d 0a 2a 2a 20 61  ons atoi()..** a
30b70 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69  nd atof().  SQLi
30b80 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c  te does not real
30b90 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e  ly use these fun
30ba0 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20  ctions.  It has 
30bb0 69 74 73 0d 0a 2a 2a 20 6f 77 6e 20 65 71 75 69  its..** own equi
30bc0 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20  valent internal 
30bd0 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61  routines.  The a
30be0 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29  toi() and atof()
30bf0 20 6e 61 6d 65 73 20 61 72 65 0d 0a 2a 2a 20 75   names are..** u
30c00 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  sed in the table
30c10 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64   for brevity and
30c20 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
30c30 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f  e familiar to mo
30c40 73 74 0d 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d  st..** C program
30c50 6d 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f  mers...**..** No
30c60 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70  te that when typ
30c70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63  e conversions oc
30c80 63 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65  cur, pointers re
30c90 74 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0d  turned by prior.
30ca0 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  .** calls to sql
30cb0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
30cc0 28 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  (), sqlite3_colu
30cd0 6d 6e 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f  mn_text(), and/o
30ce0 72 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  r..** sqlite3_co
30cf0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 6d 61  lumn_text16() ma
30d00 79 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64  y be invalidated
30d10 2e 0d 0a 2a 2a 20 54 79 70 65 20 63 6f 6e 76 65  ...** Type conve
30d20 72 73 69 6f 6e 73 20 61 6e 64 20 70 6f 69 6e 74  rsions and point
30d30 65 72 20 69 6e 76 61 6c 69 64 61 74 69 6f 6e 73  er invalidations
30d40 20 6d 69 67 68 74 20 6f 63 63 75 72 0d 0a 2a 2a   might occur..**
30d50 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
30d60 67 20 63 61 73 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  g cases:..**..**
30d70 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 54   <ul>..** <li> T
30d80 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
30d90 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64  nt is a BLOB and
30da0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30db0 74 65 78 74 28 29 20 6f 72 0d 0a 2a 2a 20 20 20  text() or..**   
30dc0 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
30dd0 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61  n_text16() is ca
30de0 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65  lled.  A zero-te
30df0 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 0d 0a  rminator might..
30e00 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 6f 20  **      need to 
30e10 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  be added to the 
30e20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0d 0a 2a 2a  string.</li>..**
30e30 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61   <li> The initia
30e40 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46  l content is UTF
30e50 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  -8 text and sqli
30e60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
30e70 31 36 28 29 20 6f 72 0d 0a 2a 2a 20 20 20 20 20  16() or..**     
30e80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30e90 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c  text16() is call
30ea0 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ed.  The content
30eb0 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74   must be convert
30ec0 65 64 0d 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55  ed..**      to U
30ed0 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0d 0a 2a 2a 20  TF-16.</li>..** 
30ee0 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c  <li> The initial
30ef0 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d   content is UTF-
30f00 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  16 text and sqli
30f10 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
30f20 28 29 20 6f 72 0d 0a 2a 2a 20 20 20 20 20 20 73  () or..**      s
30f30 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
30f40 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  xt() is called. 
30f50 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73   The content mus
30f60 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0d 0a  t be converted..
30f70 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38  **      to UTF-8
30f80 2e 3c 2f 6c 69 3e 0d 0a 2a 2a 20 3c 2f 75 6c 3e  .</li>..** </ul>
30f90 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 43 6f 6e 76 65 72  ..**..** ^Conver
30fa0 73 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 55 54  sions between UT
30fb0 46 2d 31 36 62 65 20 61 6e 64 20 55 54 46 2d 31  F-16be and UTF-1
30fc0 36 6c 65 20 61 72 65 20 61 6c 77 61 79 73 20 64  6le are always d
30fd0 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 61 6e 64  one in place and
30fe0 20 64 6f 0d 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61   do..** not inva
30ff0 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72 20 70  lidate a prior p
31000 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68 20 6f  ointer, though o
31010 66 20 63 6f 75 72 73 65 20 74 68 65 20 63 6f 6e  f course the con
31020 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75 66 66  tent of the buff
31030 65 72 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  er..** that the 
31040 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 72 65  prior pointer re
31050 66 65 72 65 6e 63 65 73 20 77 69 6c 6c 20 68 61  ferences will ha
31060 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
31070 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0d 0a  .  Other kinds..
31080 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e  ** of conversion
31090 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61   are done in pla
310a0 63 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f  ce when it is po
310b0 73 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65  ssible, but some
310c0 74 69 6d 65 73 20 74 68 65 79 0d 0a 2a 2a 20 61  times they..** a
310d0 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  re not possible 
310e0 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73  and in those cas
310f0 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72  es prior pointer
31100 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65  s are invalidate
31110 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73  d...**..** The s
31120 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 73  afest and easies
31130 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f  t to remember po
31140 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b  licy is to invok
31150 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
31160 0d 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74  ..** in one of t
31170 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79  he following way
31180 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d  s:..**..** <ul>.
31190 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33  .**  <li>sqlite3
311a0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66  _column_text() f
311b0 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74  ollowed by sqlit
311c0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
311d0 29 3c 2f 6c 69 3e 0d 0a 2a 2a 20 20 3c 6c 69 3e  )</li>..**  <li>
311e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
311f0 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  lob() followed b
31200 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
31210 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0d 0a 2a  _bytes()</li>..*
31220 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63  *  <li>sqlite3_c
31230 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
31240 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74  ollowed by sqlit
31250 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
31260 36 28 29 3c 2f 6c 69 3e 0d 0a 2a 2a 20 3c 2f 75  6()</li>..** </u
31270 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 6f 74  l>..**..** In ot
31280 68 65 72 20 77 6f 72 64 73 2c 20 79 6f 75 20 73  her words, you s
31290 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74  hould call sqlit
312a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
312b0 2c 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ,..** sqlite3_co
312c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f 72 20  lumn_blob(), or 
312d0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
312e0 65 78 74 31 36 28 29 20 66 69 72 73 74 20 74 6f  ext16() first to
312f0 20 66 6f 72 63 65 20 74 68 65 20 72 65 73 75 6c   force the resul
31300 74 0d 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64  t..** into the d
31310 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c 20 74  esired format, t
31320 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  hen invoke sqlit
31330 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
31340 29 20 6f 72 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  ) or..** sqlite3
31350 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
31360 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 69  ) to find the si
31370 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ze of the result
31380 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61  .  Do not mix ca
31390 6c 6c 73 0d 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  lls..** to sqlit
313a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
313b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   or sqlite3_colu
313c0 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63  mn_blob() with c
313d0 61 6c 6c 73 20 74 6f 0d 0a 2a 2a 20 73 71 6c 69  alls to..** sqli
313e0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
313f0 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74  16(), and do not
31400 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71   mix calls to sq
31410 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
31420 74 31 36 28 29 0d 0a 2a 2a 20 77 69 74 68 20 63  t16()..** with c
31430 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
31440 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0d  column_bytes()..
31450 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 70 6f 69  .**..** ^The poi
31460 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 61  nters returned a
31470 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61  re valid until a
31480 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   type conversion
31490 20 6f 63 63 75 72 73 20 61 73 0d 0a 2a 2a 20 64   occurs as..** d
314a0 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20  escribed above, 
314b0 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65  or until [sqlite
314c0 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71  3_step()] or [sq
314d0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
314e0 72 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  r..** [sqlite3_f
314f0 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61  inalize()] is ca
31500 6c 6c 65 64 2e 20 20 5e 54 68 65 20 6d 65 6d 6f  lled.  ^The memo
31510 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f  ry space used to
31520 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0d 0a 2a   hold strings..*
31530 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66  * and BLOBs is f
31540 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  reed automatical
31550 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f  ly.  Do <b>not</
31560 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e  b> pass the poin
31570 74 65 72 73 20 72 65 74 75 72 6e 65 64 0d 0a 2a  ters returned..*
31580 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
31590 6e 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69  n_blob()], [sqli
315a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
315b0 29 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0d 0a 2a  )], etc. into..*
315c0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  * [sqlite3_free(
315d0 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 49 66  )]...**..** ^(If
315e0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
315f0 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
31600 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 61  s during the eva
31610 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0d 0a  luation of any..
31620 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ** of these rout
31630 69 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20  ines, a default 
31640 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
31650 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  d.  The default 
31660 76 61 6c 75 65 0d 0a 2a 2a 20 69 73 20 65 69 74  value..** is eit
31670 68 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 20  her the integer 
31680 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  0, the floating 
31690 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30  point number 0.0
316a0 2c 20 6f 72 20 61 20 4e 55 4c 4c 0d 0a 2a 2a 20  , or a NULL..** 
316b0 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 65 71  pointer.  Subseq
316c0 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73  uent calls to [s
316d0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
316e0 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0d 0a 2a  ] will return..*
316f0 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
31700 2e 29 5e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  .)^..*/..SQLITE_
31710 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
31720 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
31730 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lob(sqlite3_stmt
31740 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53  *, int iCol);..S
31750 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
31760 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
31770 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  es(sqlite3_stmt*
31780 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53 51  , int iCol);..SQ
31790 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
317a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
317b0 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  s16(sqlite3_stmt
317c0 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53  *, int iCol);..S
317d0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
317e0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
317f0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
31800 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
31810 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
31820 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
31830 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
31840 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53  *, int iCol);..S
31850 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
31860 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
31870 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
31880 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
31890 69 43 6f 6c 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  iCol);..SQLITE_A
318a0 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
318b0 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
318c0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69  column_text(sqli
318d0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
318e0 43 6f 6c 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50  Col);..SQLITE_AP
318f0 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
31900 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
31910 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  t16(sqlite3_stmt
31920 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53  *, int iCol);..S
31930 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
31940 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
31950 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
31960 20 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 53 51 4c   int iCol);..SQL
31970 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
31980 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63  value *sqlite3_c
31990 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69  olumn_value(sqli
319a0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
319b0 43 6f 6c 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  Col);..../*..** 
319c0 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f  CAPI3REF: Destro
319d0 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  y A Prepared Sta
319e0 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 0d 0a 2a  tement Object..*
319f0 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  *..** ^The sqlit
31a00 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75  e3_finalize() fu
31a10 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
31a20 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72   to delete a [pr
31a30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31a40 5d 2e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 20 6d  ]...** ^If the m
31a50 6f 73 74 20 72 65 63 65 6e 74 20 65 76 61 6c 75  ost recent evalu
31a60 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61  ation of the sta
31a70 74 65 6d 65 6e 74 20 65 6e 63 6f 75 6e 74 65 72  tement encounter
31a80 65 64 20 6e 6f 20 65 72 72 6f 72 73 0d 0a 2a 2a  ed no errors..**
31a90 20 6f 72 20 69 66 20 74 68 65 20 73 74 61 74 65   or if the state
31aa0 6d 65 6e 74 20 69 73 20 6e 65 76 65 72 20 62 65  ment is never be
31ab0 65 6e 20 65 76 61 6c 75 61 74 65 64 2c 20 74 68  en evaluated, th
31ac0 65 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  en sqlite3_final
31ad0 69 7a 65 28 29 20 72 65 74 75 72 6e 73 0d 0a 2a  ize() returns..*
31ae0 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 5e 49  * SQLITE_OK.  ^I
31af0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
31b00 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20  t evaluation of 
31b10 73 74 61 74 65 6d 65 6e 74 20 53 20 66 61 69 6c  statement S fail
31b20 65 64 2c 20 74 68 65 6e 0d 0a 2a 2a 20 73 71 6c  ed, then..** sql
31b30 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29  ite3_finalize(S)
31b40 20 72 65 74 75 72 6e 73 20 74 68 65 20 61 70 70   returns the app
31b50 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20  ropriate [error 
31b60 63 6f 64 65 5d 20 6f 72 0d 0a 2a 2a 20 5b 65 78  code] or..** [ex
31b70 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
31b80 65 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  e]...**..** ^The
31b90 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
31ba0 65 28 53 29 20 72 6f 75 74 69 6e 65 20 63 61 6e  e(S) routine can
31bb0 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e   be called at an
31bc0 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 0d 0a  y point during..
31bd0 2a 2a 20 74 68 65 20 6c 69 66 65 20 63 79 63 6c  ** the life cycl
31be0 65 20 6f 66 20 5b 70 72 65 70 61 72 65 64 20 73  e of [prepared s
31bf0 74 61 74 65 6d 65 6e 74 5d 20 53 3a 0d 0a 2a 2a  tatement] S:..**
31c00 20 62 65 66 6f 72 65 20 73 74 61 74 65 6d 65 6e   before statemen
31c10 74 20 53 20 69 73 20 65 76 65 72 20 65 76 61 6c  t S is ever eval
31c20 75 61 74 65 64 2c 20 61 66 74 65 72 0d 0a 2a 2a  uated, after..**
31c30 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61 6c   one or more cal
31c40 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ls to [sqlite3_r
31c50 65 73 65 74 28 29 5d 2c 20 6f 72 20 61 66 74 65  eset()], or afte
31c60 72 20 61 6e 79 20 63 61 6c 6c 0d 0a 2a 2a 20 74  r any call..** t
31c70 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
31c80 29 5d 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  )] regardless of
31c90 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
31ca0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61  the statement ha
31cb0 73 0d 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20  s..** completed 
31cc0 65 78 65 63 75 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  execution...**..
31cd0 2a 2a 20 5e 49 6e 76 6f 6b 69 6e 67 20 73 71 6c  ** ^Invoking sql
31ce0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20  ite3_finalize() 
31cf0 6f 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  on a NULL pointe
31d00 72 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20  r is a harmless 
31d10 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  no-op...**..** T
31d20 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
31d30 75 73 74 20 66 69 6e 61 6c 69 7a 65 20 65 76 65  ust finalize eve
31d40 72 79 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ry [prepared sta
31d50 74 65 6d 65 6e 74 5d 20 69 6e 20 6f 72 64 65 72  tement] in order
31d60 20 74 6f 20 61 76 6f 69 64 0d 0a 2a 2a 20 72 65   to avoid..** re
31d70 73 6f 75 72 63 65 20 6c 65 61 6b 73 2e 20 20 49  source leaks.  I
31d80 74 20 69 73 20 61 20 67 72 69 65 76 6f 75 73 20  t is a grievous 
31d90 65 72 72 6f 72 20 66 6f 72 20 74 68 65 20 61 70  error for the ap
31da0 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 74 72 79  plication to try
31db0 20 74 6f 20 75 73 65 0d 0a 2a 2a 20 61 20 70 72   to use..** a pr
31dc0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31dd0 20 61 66 74 65 72 20 69 74 20 68 61 73 20 62 65   after it has be
31de0 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 41  en finalized.  A
31df0 6e 79 20 75 73 65 20 6f 66 20 61 20 70 72 65 70  ny use of a prep
31e00 61 72 65 64 0d 0a 2a 2a 20 73 74 61 74 65 6d 65  ared..** stateme
31e10 6e 74 20 61 66 74 65 72 20 69 74 20 68 61 73 20  nt after it has 
31e20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 63  been finalized c
31e30 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64  an result in und
31e40 65 66 69 6e 65 64 20 61 6e 64 0d 0a 2a 2a 20 75  efined and..** u
31e50 6e 64 65 73 69 72 61 62 6c 65 20 62 65 68 61 76  ndesirable behav
31e60 69 6f 72 20 73 75 63 68 20 61 73 20 73 65 67 66  ior such as segf
31e70 61 75 6c 74 73 20 61 6e 64 20 68 65 61 70 20 63  aults and heap c
31e80 6f 72 72 75 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  orruption...*/..
31e90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31ea0 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
31eb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
31ec0 74 6d 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  tmt);..../*..** 
31ed0 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
31ee0 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
31ef0 6d 65 6e 74 20 4f 62 6a 65 63 74 0d 0a 2a 2a 0d  ment Object..**.
31f00 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
31f10 72 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e  reset() function
31f20 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
31f30 73 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20  set a [prepared 
31f40 73 74 61 74 65 6d 65 6e 74 5d 0d 0a 2a 2a 20 6f  statement]..** o
31f50 62 6a 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74  bject back to it
31f60 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c  s initial state,
31f70 20 72 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d   ready to be re-
31f80 65 78 65 63 75 74 65 64 2e 0d 0a 2a 2a 20 5e 41  executed...** ^A
31f90 6e 79 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ny SQL statement
31fa0 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74 20   variables that 
31fb0 68 61 64 20 76 61 6c 75 65 73 20 62 6f 75 6e 64  had values bound
31fc0 20 74 6f 20 74 68 65 6d 20 75 73 69 6e 67 0d 0a   to them using..
31fd0 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** the [sqlite3_
31fe0 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69  bind_blob | sqli
31ff0 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41 50 49  te3_bind_*() API
32000 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72 20 76  ] retain their v
32010 61 6c 75 65 73 2e 0d 0a 2a 2a 20 55 73 65 20 5b  alues...** Use [
32020 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
32030 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65 73  ndings()] to res
32040 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73 2e  et the bindings.
32050 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b 73  ..**..** ^The [s
32060 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
32070 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 74   interface reset
32080 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  s the [prepared 
32090 73 74 61 74 65 6d 65 6e 74 5d 20 53 0d 0a 2a 2a  statement] S..**
320a0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67   back to the beg
320b0 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72  inning of its pr
320c0 6f 67 72 61 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ogram...**..** ^
320d0 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  If the most rece
320e0 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nt call to [sqli
320f0 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72  te3_step(S)] for
32100 20 74 68 65 0d 0a 2a 2a 20 5b 70 72 65 70 61 72   the..** [prepar
32110 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
32120 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
32130 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45  _ROW] or [SQLITE
32140 5f 44 4f 4e 45 5d 2c 0d 0a 2a 2a 20 6f 72 20 69  _DONE],..** or i
32150 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  f [sqlite3_step(
32160 53 29 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65  S)] has never be
32170 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64  fore been called
32180 20 6f 6e 20 53 2c 0d 0a 2a 2a 20 74 68 65 6e 20   on S,..** then 
32190 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
321a0 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  )] returns [SQLI
321b0 54 45 5f 4f 4b 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  TE_OK]...**..** 
321c0 5e 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63  ^If the most rec
321d0 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ent call to [sql
321e0 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f  ite3_step(S)] fo
321f0 72 20 74 68 65 0d 0a 2a 2a 20 5b 70 72 65 70 61  r the..** [prepa
32200 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
32210 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72   indicated an er
32220 72 6f 72 2c 20 74 68 65 6e 0d 0a 2a 2a 20 5b 73  ror, then..** [s
32230 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
32240 20 72 65 74 75 72 6e 73 20 61 6e 20 61 70 70 72   returns an appr
32250 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 63  opriate [error c
32260 6f 64 65 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  ode]...**..** ^T
32270 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
32280 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20  t(S)] interface 
32290 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  does not change 
322a0 74 68 65 20 76 61 6c 75 65 73 0d 0a 2a 2a 20 6f  the values..** o
322b0 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 5f 62  f any [sqlite3_b
322c0 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 6e 67  ind_blob|binding
322d0 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 70 61  s] on the [prepa
322e0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
322f0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
32300 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
32310 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  set(sqlite3_stmt
32320 20 2a 70 53 74 6d 74 29 3b 0d 0a 0d 0a 2f 2a 0d   *pStmt);..../*.
32330 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72  .** CAPI3REF: Cr
32340 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65  eate Or Redefine
32350 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 0d 0a   SQL Functions..
32360 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75  ** KEYWORDS: {fu
32370 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
32380 72 6f 75 74 69 6e 65 73 7d 0d 0a 2a 2a 20 4b 45  routines}..** KE
32390 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61  YWORDS: {applica
323a0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
323b0 20 66 75 6e 63 74 69 6f 6e 7d 0d 0a 2a 2a 20 4b   function}..** K
323c0 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63  EYWORDS: {applic
323d0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51  ation-defined SQ
323e0 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0d 0a 2a 2a  L functions}..**
323f0 0d 0a 2a 2a 20 5e 54 68 65 73 65 20 66 75 6e 63  ..** ^These func
32400 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76  tions (collectiv
32410 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75  ely known as "fu
32420 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
32430 72 6f 75 74 69 6e 65 73 22 29 0d 0a 2a 2a 20 61  routines")..** a
32440 72 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53  re used to add S
32450 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20  QL functions or 
32460 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f  aggregates or to
32470 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65   redefine the be
32480 68 61 76 69 6f 72 0d 0a 2a 2a 20 6f 66 20 65 78  havior..** of ex
32490 69 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74  isting SQL funct
324a0 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74  ions or aggregat
324b0 65 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69  es.  The only di
324c0 66 66 65 72 65 6e 63 65 73 20 62 65 74 77 65 65  fferences betwee
324d0 6e 0d 0a 2a 2a 20 74 68 65 73 65 20 72 6f 75 74  n..** these rout
324e0 69 6e 65 73 20 61 72 65 20 74 68 65 20 74 65 78  ines are the tex
324f0 74 20 65 6e 63 6f 64 69 6e 67 20 65 78 70 65 63  t encoding expec
32500 74 65 64 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20  ted for..** the 
32510 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
32520 20 28 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68   (the name of th
32530 65 20 66 75 6e 63 74 69 6f 6e 20 62 65 69 6e 67  e function being
32540 20 63 72 65 61 74 65 64 29 0d 0a 2a 2a 20 61 6e   created)..** an
32550 64 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f  d the presence o
32560 72 20 61 62 73 65 6e 63 65 20 6f 66 20 61 20 64  r absence of a d
32570 65 73 74 72 75 63 74 6f 72 20 63 61 6c 6c 62 61  estructor callba
32580 63 6b 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 61  ck for..** the a
32590 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74 61 20  pplication data 
325a0 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  pointer...**..**
325b0 20 5e 54 68 65 20 66 69 72 73 74 20 70 61 72 61   ^The first para
325c0 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61  meter is the [da
325d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
325e0 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  n] to which the 
325f0 53 51 4c 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  SQL..** function
32600 20 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e   is to be added.
32610 20 20 5e 49 66 20 61 6e 20 61 70 70 6c 69 63 61    ^If an applica
32620 74 69 6f 6e 20 75 73 65 73 20 6d 6f 72 65 20 74  tion uses more t
32630 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
32640 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ..** connection 
32650 74 68 65 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  then application
32660 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
32670 63 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 61  ctions must be a
32680 64 64 65 64 0d 0a 2a 2a 20 74 6f 20 65 61 63 68  dded..** to each
32690 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
326a0 74 69 6f 6e 20 73 65 70 61 72 61 74 65 6c 79 2e  tion separately.
326b0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 65  ..**..** ^The se
326c0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  cond parameter i
326d0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
326e0 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  e SQL function t
326f0 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f 72 0d  o be created or.
32700 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20  .** redefined.  
32710 5e 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74  ^The length of t
32720 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74  he name is limit
32730 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 20  ed to 255 bytes 
32740 69 6e 20 61 20 55 54 46 2d 38 0d 0a 2a 2a 20 72  in a UTF-8..** r
32750 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 65  epresentation, e
32760 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20  xclusive of the 
32770 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e  zero-terminator.
32780 20 20 5e 4e 6f 74 65 20 74 68 61 74 20 74 68 65    ^Note that the
32790 20 6e 61 6d 65 0d 0a 2a 2a 20 6c 65 6e 67 74 68   name..** length
327a0 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 55 54 46   limit is in UTF
327b0 2d 38 20 62 79 74 65 73 2c 20 6e 6f 74 20 63 68  -8 bytes, not ch
327c0 61 72 61 63 74 65 72 73 20 6e 6f 72 20 55 54 46  aracters nor UTF
327d0 2d 31 36 20 62 79 74 65 73 2e 20 20 0d 0a 2a 2a  -16 bytes.  ..**
327e0 20 5e 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f   ^Any attempt to
327f0 20 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69   create a functi
32800 6f 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72  on with a longer
32810 20 6e 61 6d 65 0d 0a 2a 2a 20 77 69 6c 6c 20 72   name..** will r
32820 65 73 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45  esult in [SQLITE
32830 5f 4d 49 53 55 53 45 5d 20 62 65 69 6e 67 20 72  _MISUSE] being r
32840 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  eturned...**..**
32850 20 5e 54 68 65 20 74 68 69 72 64 20 70 61 72 61   ^The third para
32860 6d 65 74 65 72 20 28 6e 41 72 67 29 0d 0a 2a 2a  meter (nArg)..**
32870 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
32880 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  f arguments that
32890 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
328a0 6e 20 6f 72 0d 0a 2a 2a 20 61 67 67 72 65 67 61  n or..** aggrega
328b0 74 65 20 74 61 6b 65 73 2e 20 5e 49 66 20 74 68  te takes. ^If th
328c0 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  is parameter is 
328d0 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c  -1, then the SQL
328e0 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0d 0a 2a 2a   function or..**
328f0 20 61 67 67 72 65 67 61 74 65 20 6d 61 79 20 74   aggregate may t
32900 61 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f  ake any number o
32910 66 20 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77  f arguments betw
32920 65 65 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69  een 0 and the li
32930 6d 69 74 0d 0a 2a 2a 20 73 65 74 20 62 79 20 5b  mit..** set by [
32940 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b  sqlite3_limit]([
32950 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e  SQLITE_LIMIT_FUN
32960 43 54 49 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66  CTION_ARG]).  If
32970 20 74 68 65 20 74 68 69 72 64 0d 0a 2a 2a 20 70   the third..** p
32980 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 73  arameter is less
32990 20 74 68 61 6e 20 2d 31 20 6f 72 20 67 72 65 61   than -1 or grea
329a0 74 65 72 20 74 68 61 6e 20 31 32 37 20 74 68 65  ter than 127 the
329b0 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
329c0 73 0d 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  s..** undefined.
329d0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 6f  ..**..** ^The fo
329e0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20  urth parameter, 
329f0 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66  eTextRep, specif
32a00 69 65 73 20 77 68 61 74 0d 0a 2a 2a 20 5b 53 51  ies what..** [SQ
32a10 4c 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74  LITE_UTF8 | text
32a20 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20   encoding] this 
32a30 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  SQL function pre
32a40 66 65 72 73 20 66 6f 72 0d 0a 2a 2a 20 69 74 73  fers for..** its
32a50 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 45 76   parameters.  Ev
32a60 65 72 79 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ery SQL function
32a70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
32a80 6d 75 73 74 20 62 65 20 61 62 6c 65 20 74 6f 20  must be able to 
32a90 77 6f 72 6b 0d 0a 2a 2a 20 77 69 74 68 20 55 54  work..** with UT
32aa0 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f  F-8, UTF-16le, o
32ab0 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 74  r UTF-16be.  But
32ac0 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
32ad0 74 69 6f 6e 73 20 6d 61 79 20 62 65 0d 0a 2a 2a  tions may be..**
32ae0 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20   more efficient 
32af0 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e  with one encodin
32b00 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20  g than another. 
32b10 20 5e 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e   ^An application
32b20 20 6d 61 79 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 20   may..** invoke 
32b30 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
32b40 75 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c  unction() or sql
32b50 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
32b60 74 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c  tion16() multipl
32b70 65 0d 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68  e..** times with
32b80 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
32b90 6f 6e 20 62 75 74 20 77 69 74 68 20 64 69 66 66  on but with diff
32ba0 65 72 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20  erent values of 
32bb0 65 54 65 78 74 52 65 70 2e 0d 0a 2a 2a 20 5e 57  eTextRep...** ^W
32bc0 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  hen multiple imp
32bd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
32be0 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  the same functio
32bf0 6e 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c  n are available,
32c00 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 77 69 6c 6c   SQLite..** will
32c10 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 20 74 68   pick the one th
32c20 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 68 65 20  at involves the 
32c30 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20  least amount of 
32c40 64 61 74 61 20 63 6f 6e 76 65 72 73 69 6f 6e 2e  data conversion.
32c50 0d 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
32c60 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 69   only a single i
32c70 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68  mplementation wh
32c80 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 61 72  ich does not car
32c90 65 20 77 68 61 74 20 74 65 78 74 0d 0a 2a 2a 20  e what text..** 
32ca0 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64  encoding is used
32cb0 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 74  , then the fourt
32cc0 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c  h argument shoul
32cd0 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e 59  d be [SQLITE_ANY
32ce0 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65  ]...**..** ^(The
32cf0 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 65 72   fifth parameter
32d00 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
32d10 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 69   pointer.  The i
32d20 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
32d30 20 74 68 65 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f   the..** functio
32d40 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65 73  n can gain acces
32d50 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65  s to this pointe
32d60 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  r using [sqlite3
32d70 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e 29 5e  _user_data()].)^
32d80 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 69  ..**..** ^The si
32d90 78 74 68 2c 20 73 65 76 65 6e 74 68 20 61 6e 64  xth, seventh and
32da0 20 65 69 67 68 74 68 20 70 61 72 61 6d 65 74 65   eighth paramete
32db0 72 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70  rs, xFunc, xStep
32dc0 20 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 65   and xFinal, are
32dd0 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f  ..** pointers to
32de0 20 43 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63   C-language func
32df0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
32e00 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e  ment the SQL fun
32e10 63 74 69 6f 6e 20 6f 72 0d 0a 2a 2a 20 61 67 67  ction or..** agg
32e20 72 65 67 61 74 65 2e 20 5e 41 20 73 63 61 6c 61  regate. ^A scala
32e30 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72  r SQL function r
32e40 65 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65  equires an imple
32e50 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
32e60 20 78 46 75 6e 63 0d 0a 2a 2a 20 63 61 6c 6c 62   xFunc..** callb
32e70 61 63 6b 20 6f 6e 6c 79 3b 20 4e 55 4c 4c 20 70  ack only; NULL p
32e80 6f 69 6e 74 65 72 73 20 6d 75 73 74 20 62 65 20  ointers must be 
32e90 70 61 73 73 65 64 20 61 73 20 74 68 65 20 78 53  passed as the xS
32ea0 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 0d 0a  tep and xFinal..
32eb0 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e 20 5e  ** parameters. ^
32ec0 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  An aggregate SQL
32ed0 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72   function requir
32ee0 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  es an implementa
32ef0 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0d 0a 2a  tion of xStep..*
32f00 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64  * and xFinal and
32f10 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6d 75   NULL pointer mu
32f20 73 74 20 62 65 20 70 61 73 73 65 64 20 66 6f 72  st be passed for
32f30 20 78 46 75 6e 63 2e 20 5e 54 6f 20 64 65 6c 65   xFunc. ^To dele
32f40 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0d 0a  te an existing..
32f50 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
32f60 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61  or aggregate, pa
32f70 73 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  ss NULL pointers
32f80 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66   for all three f
32f90 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 63 61 6c 6c  unction..** call
32fa0 62 61 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  backs...**..** ^
32fb0 28 49 66 20 74 68 65 20 6e 69 6e 74 68 20 70 61  (If the ninth pa
32fc0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
32fd0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
32fe0 6f 6e 5f 76 32 28 29 20 69 73 20 6e 6f 74 20 4e  on_v2() is not N
32ff0 55 4c 4c 2c 0d 0a 2a 2a 20 74 68 65 6e 20 69 74  ULL,..** then it
33000 20 69 73 20 64 65 73 74 72 75 63 74 6f 72 20 66   is destructor f
33010 6f 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  or the applicati
33020 6f 6e 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2e  on data pointer.
33030 20 0d 0a 2a 2a 20 54 68 65 20 64 65 73 74 72 75   ..** The destru
33040 63 74 6f 72 20 69 73 20 69 6e 76 6f 6b 65 64 20  ctor is invoked 
33050 77 68 65 6e 20 74 68 65 20 66 75 6e 63 74 69 6f  when the functio
33060 6e 20 69 73 20 64 65 6c 65 74 65 64 2c 20 65 69  n is deleted, ei
33070 74 68 65 72 20 62 79 20 62 65 69 6e 67 0d 0a 2a  ther by being..*
33080 2a 20 6f 76 65 72 6c 6f 61 64 65 64 20 6f 72 20  * overloaded or 
33090 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  when the databas
330a0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f  e connection clo
330b0 73 65 73 2e 29 5e 0d 0a 2a 2a 20 5e 54 68 65 20  ses.)^..** ^The 
330c0 64 65 73 74 72 75 63 74 6f 72 20 69 73 20 61 6c  destructor is al
330d0 73 6f 20 69 6e 76 6f 6b 65 64 20 69 66 20 74 68  so invoked if th
330e0 65 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 73 71  e call to..** sq
330f0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
33100 63 74 69 6f 6e 5f 76 32 28 29 20 66 61 69 6c 73  ction_v2() fails
33110 2e 0d 0a 2a 2a 20 5e 57 68 65 6e 20 74 68 65 20  ...** ^When the 
33120 64 65 73 74 72 75 63 74 6f 72 20 63 61 6c 6c 62  destructor callb
33130 61 63 6b 20 6f 66 20 74 68 65 20 74 65 6e 74 68  ack of the tenth
33140 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 6e   parameter is in
33150 76 6f 6b 65 64 2c 20 69 74 0d 0a 2a 2a 20 69 73  voked, it..** is
33160 20 70 61 73 73 65 64 20 61 20 73 69 6e 67 6c 65   passed a single
33170 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
33180 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  is a copy of the
33190 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74   application dat
331a0 61 20 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 77  a ..** pointer w
331b0 68 69 63 68 20 77 61 73 20 74 68 65 20 66 69 66  hich was the fif
331c0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
331d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
331e0 75 6e 63 74 69 6f 6e 5f 76 32 28 29 2e 0d 0a 2a  unction_v2()...*
331f0 2a 0d 0a 2a 2a 20 5e 49 74 20 69 73 20 70 65 72  *..** ^It is per
33200 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69 73 74  mitted to regist
33210 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c  er multiple impl
33220 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
33230 68 65 20 73 61 6d 65 0d 0a 2a 2a 20 66 75 6e 63  he same..** func
33240 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20 73  tions with the s
33250 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74  ame name but wit
33260 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72 69  h either differi
33270 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0d 0a 2a  ng numbers of..*
33280 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64  * arguments or d
33290 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72  iffering preferr
332a0 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
332b0 73 2e 20 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c  s.  ^SQLite will
332c0 20 75 73 65 0d 0a 2a 2a 20 74 68 65 20 69 6d 70   use..** the imp
332d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
332e0 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61   most closely ma
332f0 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e  tches the way in
33300 20 77 68 69 63 68 20 74 68 65 0d 0a 2a 2a 20 53   which the..** S
33310 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  QL function is u
33320 73 65 64 2e 20 20 5e 41 20 66 75 6e 63 74 69 6f  sed.  ^A functio
33330 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
33340 20 77 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61   with a non-nega
33350 74 69 76 65 0d 0a 2a 2a 20 6e 41 72 67 20 70 61  tive..** nArg pa
33360 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 65 74  rameter is a bet
33370 74 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20 61  ter match than a
33380 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
33390 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 0d 0a 2a  entation with..*
333a0 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 41 72  * a negative nAr
333b0 67 2e 20 20 5e 41 20 66 75 6e 63 74 69 6f 6e 20  g.  ^A function 
333c0 77 68 65 72 65 20 74 68 65 20 70 72 65 66 65 72  where the prefer
333d0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
333e0 67 0d 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68  g..** matches th
333f0 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64  e database encod
33400 69 6e 67 20 69 73 20 61 20 62 65 74 74 65 72 0d  ing is a better.
33410 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61  .** match than a
33420 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
33430 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  the encoding is 
33440 64 69 66 66 65 72 65 6e 74 2e 20 20 0d 0a 2a 2a  different.  ..**
33450 20 5e 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65   ^A function whe
33460 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  re the encoding 
33470 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 62 65  difference is be
33480 74 77 65 65 6e 20 55 54 46 31 36 6c 65 20 61 6e  tween UTF16le an
33490 64 20 55 54 46 31 36 62 65 0d 0a 2a 2a 20 69 73  d UTF16be..** is
334a0 20 61 20 63 6c 6f 73 65 72 20 6d 61 74 63 68 20   a closer match 
334b0 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20  than a function 
334c0 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69  where the encodi
334d0 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  ng difference is
334e0 0d 0a 2a 2a 20 62 65 74 77 65 65 6e 20 55 54 46  ..** between UTF
334f0 38 20 61 6e 64 20 55 54 46 31 36 2e 0d 0a 2a 2a  8 and UTF16...**
33500 0d 0a 2a 2a 20 5e 42 75 69 6c 74 2d 69 6e 20 66  ..** ^Built-in f
33510 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20  unctions may be 
33520 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e 65  overloaded by ne
33530 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  w application-de
33540 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e  fined functions.
33550 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 41 6e 20 61 70 70  ..**..** ^An app
33560 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
33570 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 65 72   function is per
33580 6d 69 74 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f  mitted to call o
33590 74 68 65 72 0d 0a 2a 2a 20 53 51 4c 69 74 65 20  ther..** SQLite 
335a0 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77  interfaces.  How
335b0 65 76 65 72 2c 20 73 75 63 68 20 63 61 6c 6c 73  ever, such calls
335c0 20 6d 75 73 74 20 6e 6f 74 0d 0a 2a 2a 20 63 6c   must not..** cl
335d0 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ose the database
335e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 20   connection nor 
335f0 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 65 73 65  finalize or rese
33600 74 20 74 68 65 20 70 72 65 70 61 72 65 64 0d 0a  t the prepared..
33610 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  ** statement in 
33620 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 74 69  which the functi
33630 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0d 0a  on is running...
33640 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
33650 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
33660 65 5f 66 75 6e 63 74 69 6f 6e 28 0d 0a 20 20 73  e_function(..  s
33670 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20 20 63  qlite3 *db,..  c
33680 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
33690 74 69 6f 6e 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74  tionName,..  int
336a0 20 6e 41 72 67 2c 0d 0a 20 20 69 6e 74 20 65 54   nArg,..  int eT
336b0 65 78 74 52 65 70 2c 0d 0a 20 20 76 6f 69 64 20  extRep,..  void 
336c0 2a 70 41 70 70 2c 0d 0a 20 20 76 6f 69 64 20 28  *pApp,..  void (
336d0 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
336e0 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
336f0 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a  ite3_value**),..
33700 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
33710 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
33720 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
33730 75 65 2a 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28  ue**),..  void (
33740 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
33750 5f 63 6f 6e 74 65 78 74 2a 29 0d 0a 29 3b 0d 0a  _context*)..);..
33760 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33770 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33780 6e 63 74 69 6f 6e 31 36 28 0d 0a 20 20 73 71 6c  nction16(..  sql
33790 69 74 65 33 20 2a 64 62 2c 0d 0a 20 20 63 6f 6e  ite3 *db,..  con
337a0 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69  st void *zFuncti
337b0 6f 6e 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20 6e  onName,..  int n
337c0 41 72 67 2c 0d 0a 20 20 69 6e 74 20 65 54 65 78  Arg,..  int eTex
337d0 74 52 65 70 2c 0d 0a 20 20 76 6f 69 64 20 2a 70  tRep,..  void *p
337e0 41 70 70 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78  App,..  void (*x
337f0 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
33800 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
33810 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a 20 20  e3_value**),..  
33820 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
33830 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33840 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33850 2a 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78  **),..  void (*x
33860 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
33870 6f 6e 74 65 78 74 2a 29 0d 0a 29 3b 0d 0a 53 51  ontext*)..);..SQ
33880 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
33890 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
338a0 74 69 6f 6e 5f 76 32 28 0d 0a 20 20 73 71 6c 69  tion_v2(..  sqli
338b0 74 65 33 20 2a 64 62 2c 0d 0a 20 20 63 6f 6e 73  te3 *db,..  cons
338c0 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f  t char *zFunctio
338d0 6e 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20 6e 41  nName,..  int nA
338e0 72 67 2c 0d 0a 20 20 69 6e 74 20 65 54 65 78 74  rg,..  int eText
338f0 52 65 70 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41  Rep,..  void *pA
33900 70 70 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 46  pp,..  void (*xF
33910 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
33920 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
33930 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a 20 20 76  3_value**),..  v
33940 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
33950 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
33960 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
33970 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 46  *),..  void (*xF
33980 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
33990 6e 74 65 78 74 2a 29 2c 0d 0a 20 20 76 6f 69 64  ntext*),..  void
339a0 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
339b0 2a 29 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  *)..);..../*..**
339c0 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 20   CAPI3REF: Text 
339d0 45 6e 63 6f 64 69 6e 67 73 0d 0a 2a 2a 0d 0a 2a  Encodings..**..*
339e0 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
339f0 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20   define integer 
33a00 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65  codes that repre
33a10 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73  sent the various
33a20 0d 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69  ..** text encodi
33a30 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79  ngs supported by
33a40 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 23 64   SQLite...*/..#d
33a50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
33a60 38 20 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23  8           1..#
33a70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54  define SQLITE_UT
33a80 46 31 36 4c 45 20 20 20 20 20 20 20 20 32 0d 0a  F16LE        2..
33a90 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
33aa0 54 46 31 36 42 45 20 20 20 20 20 20 20 20 33 0d  TF16BE        3.
33ab0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
33ac0 55 54 46 31 36 20 20 20 20 20 20 20 20 20 20 34  UTF16          4
33ad0 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69 76      /* Use nativ
33ae0 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f 0d  e byte order */.
33af0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
33b00 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 35  ANY            5
33b10 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63      /* sqlite3_c
33b20 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f  reate_function o
33b30 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  nly */..#define 
33b40 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
33b50 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71  GNED  8    /* sq
33b60 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
33b70 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0d 0a  lation only */..
33b80 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
33b90 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75  F: Deprecated Fu
33ba0 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 44 45 50 52  nctions..** DEPR
33bb0 45 43 41 54 45 44 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ECATED..**..** T
33bc0 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hese functions a
33bd0 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 5d 2e  re [deprecated].
33be0 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61    In order to ma
33bf0 69 6e 74 61 69 6e 0d 0a 2a 2a 20 62 61 63 6b 77  intain..** backw
33c00 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
33c10 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 6f  ty with older co
33c20 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 69  de, these functi
33c30 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0d 0a 2a  ons continue ..*
33c40 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65  * to be supporte
33c50 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77  d.  However, new
33c60 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   applications sh
33c70 6f 75 6c 64 20 61 76 6f 69 64 0d 0a 2a 2a 20 74  ould avoid..** t
33c80 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20  he use of these 
33c90 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68  functions.  To h
33ca0 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65  elp encourage pe
33cb0 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0d 0a 2a  ople to avoid..*
33cc0 2a 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75  * using these fu
33cd0 6e 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20  nctions, we are 
33ce0 6e 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c  not going to tel
33cf0 6c 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20  l you what they 
33d00 64 6f 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  do...*/..#ifndef
33d10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
33d20 52 45 43 41 54 45 44 0d 0a 53 51 4c 49 54 45 5f  RECATED..SQLITE_
33d30 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
33d40 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65  CATED int sqlite
33d50 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  3_aggregate_coun
33d60 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
33d70 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  t*);..SQLITE_API
33d80 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
33d90 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  ED int sqlite3_e
33da0 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73  xpired(sqlite3_s
33db0 74 6d 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  tmt*);..SQLITE_A
33dc0 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43  PI SQLITE_DEPREC
33dd0 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33  ATED int sqlite3
33de0 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
33df0 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  gs(sqlite3_stmt*
33e00 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  , sqlite3_stmt*)
33e10 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  ;..SQLITE_API SQ
33e20 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
33e30 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  int sqlite3_glob
33e40 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29  al_recover(void)
33e50 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  ;..SQLITE_API SQ
33e60 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
33e70 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72  void sqlite3_thr
33e80 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64  ead_cleanup(void
33e90 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  );..SQLITE_API S
33ea0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
33eb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d   int sqlite3_mem
33ec0 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a  ory_alarm(void(*
33ed0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  )(void*,sqlite3_
33ee0 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a  int64,int),void*
33ef0 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b  ,sqlite3_int64);
33f00 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
33f10 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74  ** CAPI3REF: Obt
33f20 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e 63 74  aining SQL Funct
33f30 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 56 61  ion Parameter Va
33f40 6c 75 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  lues..**..** The
33f50 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c   C-language impl
33f60 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51  ementation of SQ
33f70 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  L functions and 
33f80 61 67 67 72 65 67 61 74 65 73 20 75 73 65 73 0d  aggregates uses.
33f90 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 20  .** this set of 
33fa0 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  interface routin
33fb0 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  es to access the
33fc0 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65   parameter value
33fd0 73 20 6f 6e 0d 0a 2a 2a 20 74 68 65 20 66 75 6e  s on..** the fun
33fe0 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61  ction or aggrega
33ff0 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  te...**..** The 
34000 78 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61  xFunc (for scala
34010 72 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20  r functions) or 
34020 78 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65  xStep (for aggre
34030 67 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72  gates) parameter
34040 73 0d 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65  s..** to [sqlite
34050 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
34060 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  n()] and [sqlite
34070 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
34080 6e 31 36 28 29 5d 0d 0a 2a 2a 20 64 65 66 69 6e  n16()]..** defin
34090 65 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74  e callbacks that
340a0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53   implement the S
340b0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64  QL functions and
340c0 20 61 67 67 72 65 67 61 74 65 73 2e 0d 0a 2a 2a   aggregates...**
340d0 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   The 3rd paramet
340e0 65 72 20 74 6f 20 74 68 65 73 65 20 63 61 6c 6c  er to these call
340f0 62 61 63 6b 73 20 69 73 20 61 6e 20 61 72 72 61  backs is an arra
34100 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
34110 0d 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 65 64 20  ..** [protected 
34120 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
34130 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20 69  bjects.  There i
34140 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f 76  s one [sqlite3_v
34150 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72  alue] object for
34160 0d 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65  ..** each parame
34170 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66  ter to the SQL f
34180 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20  unction.  These 
34190 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
341a0 64 20 74 6f 0d 0a 2a 2a 20 65 78 74 72 61 63 74  d to..** extract
341b0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
341c0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
341d0 20 6f 62 6a 65 63 74 73 2e 0d 0a 2a 2a 0d 0a 2a   objects...**..*
341e0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
341f0 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20   work only with 
34200 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
34210 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
34220 73 2e 0d 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d  s...** Any attem
34230 70 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20  pt to use these 
34240 72 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b  routines on an [
34250 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
34260 74 65 33 5f 76 61 6c 75 65 5d 0d 0a 2a 2a 20 6f  te3_value]..** o
34270 62 6a 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e  bject results in
34280 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76   undefined behav
34290 69 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ior...**..** ^Th
342a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  ese routines wor
342b0 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  k just like the 
342c0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 63  corresponding [c
342d0 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e  olumn access fun
342e0 63 74 69 6f 6e 73 5d 0d 0a 2a 2a 20 65 78 63 65  ctions]..** exce
342f0 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72  pt that  these r
34300 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73  outines take a s
34310 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64  ingle [protected
34320 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
34330 6f 62 6a 65 63 74 0d 0a 2a 2a 20 70 6f 69 6e 74  object..** point
34340 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  er instead of a 
34350 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20  [sqlite3_stmt*] 
34360 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69  pointer and an i
34370 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75  nteger column nu
34380 6d 62 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  mber...**..** ^T
34390 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
343a0 5f 74 65 78 74 31 36 28 29 20 69 6e 74 65 72 66  _text16() interf
343b0 61 63 65 20 65 78 74 72 61 63 74 73 20 61 20 55  ace extracts a U
343c0 54 46 2d 31 36 20 73 74 72 69 6e 67 0d 0a 2a 2a  TF-16 string..**
343d0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
343e0 79 74 65 2d 6f 72 64 65 72 20 6f 66 20 74 68 65  yte-order of the
343f0 20 68 6f 73 74 20 6d 61 63 68 69 6e 65 2e 20 20   host machine.  
34400 5e 54 68 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  ^The..** sqlite3
34410 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
34420 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 61  ) and sqlite3_va
34430 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29 20 69  lue_text16le() i
34440 6e 74 65 72 66 61 63 65 73 0d 0a 2a 2a 20 65 78  nterfaces..** ex
34450 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 74 72  tract UTF-16 str
34460 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64 69  ings as big-endi
34470 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 6e  an and little-en
34480 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 65 6c  dian respectivel
34490 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65  y...**..** ^(The
344a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
344b0 75 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e  umeric_type() in
344c0 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
344d0 20 74 6f 20 61 70 70 6c 79 0d 0a 2a 2a 20 6e 75   to apply..** nu
344e0 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 74  meric affinity t
344f0 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68  o the value.  Th
34500 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e  is means that an
34510 20 61 74 74 65 6d 70 74 20 69 73 0d 0a 2a 2a 20   attempt is..** 
34520 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 20  made to convert 
34530 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e 20  the value to an 
34540 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74  integer or float
34550 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0d 0a  ing point.  If..
34560 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72  ** such a conver
34570 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65  sion is possible
34580 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
34590 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e   information (in
345a0 20 6f 74 68 65 72 0d 0a 2a 2a 20 77 6f 72 64 73   other..** words
345b0 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  , if the value i
345c0 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  s a string that 
345d0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d  looks like a num
345e0 62 65 72 29 0d 0a 2a 2a 20 74 68 65 6e 20 74 68  ber)..** then th
345f0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
34600 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65  performed.  Othe
34610 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73  rwise no convers
34620 69 6f 6e 20 6f 63 63 75 72 73 2e 0d 0a 2a 2a 20  ion occurs...** 
34630 54 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  The [SQLITE_INTE
34640 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20  GER | datatype] 
34650 61 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e  after conversion
34660 20 69 73 20 72 65 74 75 72 6e 65 64 2e 29 5e 0d   is returned.)^.
34670 0a 2a 2a 0d 0a 2a 2a 20 50 6c 65 61 73 65 20 70  .**..** Please p
34680 61 79 20 70 61 72 74 69 63 75 6c 61 72 20 61 74  ay particular at
34690 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 66  tention to the f
346a0 61 63 74 20 74 68 61 74 20 74 68 65 20 70 6f 69  act that the poi
346b0 6e 74 65 72 20 72 65 74 75 72 6e 65 64 0d 0a 2a  nter returned..*
346c0 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  * from [sqlite3_
346d0 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 20 5b  value_blob()], [
346e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
346f0 78 74 28 29 5d 2c 20 6f 72 0d 0a 2a 2a 20 5b 73  xt()], or..** [s
34700 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
34710 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69 6e  t16()] can be in
34720 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 20 73  validated by a s
34730 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74  ubsequent call t
34740 6f 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  o..** [sqlite3_v
34750 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b  alue_bytes()], [
34760 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
34770 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74  tes16()], [sqlit
34780 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d  e3_value_text()]
34790 2c 0d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  ,..** or [sqlite
347a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
347b0 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65  ]...**..** These
347c0 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62   routines must b
347d0 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  e called from th
347e0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 61 73  e same thread as
347f0 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e  ..** the SQL fun
34800 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70 70 6c  ction that suppl
34810 69 65 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ied the [sqlite3
34820 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74  _value*] paramet
34830 65 72 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ers...*/..SQLITE
34840 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
34850 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  *sqlite3_value_b
34860 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  lob(sqlite3_valu
34870 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  e*);..SQLITE_API
34880 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
34890 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ue_bytes(sqlite3
348a0 5f 76 61 6c 75 65 2a 29 3b 0d 0a 53 51 4c 49 54  _value*);..SQLIT
348b0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
348c0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28  3_value_bytes16(
348d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
348e0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  ..SQLITE_API dou
348f0 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble sqlite3_valu
34900 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  e_double(sqlite3
34910 5f 76 61 6c 75 65 2a 29 3b 0d 0a 53 51 4c 49 54  _value*);..SQLIT
34920 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
34930 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69  3_value_int(sqli
34940 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a 53 51  te3_value*);..SQ
34950 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
34960 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76  _int64 sqlite3_v
34970 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74  alue_int64(sqlit
34980 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a 53 51 4c  e3_value*);..SQL
34990 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
349a0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
349b0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
349c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
349d0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
349e0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
349f0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71  _value_text16(sq
34a00 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a  lite3_value*);..
34a10 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
34a20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
34a30 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71  alue_text16le(sq
34a40 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a  lite3_value*);..
34a50 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
34a60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
34a70 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71  alue_text16be(sq
34a80 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a  lite3_value*);..
34a90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
34aa0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
34ab0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
34ac0 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  );..SQLITE_API i
34ad0 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
34ae0 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71  _numeric_type(sq
34af0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a  lite3_value*);..
34b00 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
34b10 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67  F: Obtain Aggreg
34b20 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e  ate Function Con
34b30 74 65 78 74 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6d 70  text..**..** Imp
34b40 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
34b50 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
34b60 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 69 73  nctions use this
34b70 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ..** routine to 
34b80 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
34b90 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69  for storing thei
34ba0 72 20 73 74 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  r state...**..**
34bb0 20 5e 54 68 65 20 66 69 72 73 74 20 74 69 6d 65   ^The first time
34bc0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67   the sqlite3_agg
34bd0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43  regate_context(C
34be0 2c 4e 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  ,N) routine is c
34bf0 61 6c 6c 65 64 20 0d 0a 2a 2a 20 66 6f 72 20 61  alled ..** for a
34c00 20 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 72   particular aggr
34c10 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20  egate function, 
34c20 53 51 4c 69 74 65 0d 0a 2a 2a 20 61 6c 6c 6f 63  SQLite..** alloc
34c30 61 74 65 73 20 4e 20 6f 66 20 6d 65 6d 6f 72 79  ates N of memory
34c40 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61  , zeroes out tha
34c50 74 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 72 65  t memory, and re
34c60 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 0d  turns a pointer.
34c70 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 6d  .** to the new m
34c80 65 6d 6f 72 79 2e 20 5e 4f 6e 20 73 65 63 6f 6e  emory. ^On secon
34c90 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
34ca0 20 63 61 6c 6c 73 20 74 6f 0d 0a 2a 2a 20 73 71   calls to..** sq
34cb0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
34cc0 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 68  context() for th
34cd0 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 65  e same aggregate
34ce0 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 74 61 6e   function instan
34cf0 63 65 2c 0d 0a 2a 2a 20 74 68 65 20 73 61 6d 65  ce,..** the same
34d00 20 62 75 66 66 65 72 20 69 73 20 72 65 74 75 72   buffer is retur
34d10 6e 65 64 2e 20 20 53 71 6c 69 74 65 33 5f 61 67  ned.  Sqlite3_ag
34d20 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
34d30 29 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0d 0a 2a  ) is normally..*
34d40 2a 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f  * called once fo
34d50 72 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f  r each invocatio
34d60 6e 20 6f 66 20 74 68 65 20 78 53 74 65 70 20 63  n of the xStep c
34d70 61 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 65 6e  allback and then
34d80 20 6f 6e 65 0d 0a 2a 2a 20 6c 61 73 74 20 74 69   one..** last ti
34d90 6d 65 20 77 68 65 6e 20 74 68 65 20 78 46 69 6e  me when the xFin
34da0 61 6c 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  al callback is i
34db0 6e 76 6f 6b 65 64 2e 20 20 5e 28 57 68 65 6e 20  nvoked.  ^(When 
34dc0 6e 6f 20 72 6f 77 73 20 6d 61 74 63 68 0d 0a 2a  no rows match..*
34dd0 2a 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 71  * an aggregate q
34de0 75 65 72 79 2c 20 74 68 65 20 78 53 74 65 70 28  uery, the xStep(
34df0 29 20 63 61 6c 6c 62 61 63 6b 20 6f 66 20 74 68  ) callback of th
34e00 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  e aggregate func
34e10 74 69 6f 6e 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65  tion..** impleme
34e20 6e 74 61 74 69 6f 6e 20 69 73 20 6e 65 76 65 72  ntation is never
34e30 20 63 61 6c 6c 65 64 20 61 6e 64 20 78 46 69 6e   called and xFin
34e40 61 6c 28 29 20 69 73 20 63 61 6c 6c 65 64 20 65  al() is called e
34e50 78 61 63 74 6c 79 20 6f 6e 63 65 2e 0d 0a 2a 2a  xactly once...**
34e60 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c   In those cases,
34e70 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
34e80 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 6d 69 67  te_context() mig
34e90 68 74 20 62 65 20 63 61 6c 6c 65 64 20 66 6f 72  ht be called for
34ea0 20 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 74   the..** first t
34eb0 69 6d 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ime from within 
34ec0 78 46 69 6e 61 6c 28 29 2e 29 5e 0d 0a 2a 2a 0d  xFinal().)^..**.
34ed0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
34ee0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
34ef0 78 74 28 43 2c 4e 29 20 72 6f 75 74 69 6e 65 20  xt(C,N) routine 
34f00 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
34f10 6f 69 6e 74 65 72 20 69 66 20 4e 20 69 73 0d 0a  ointer if N is..
34f20 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  ** less than or 
34f30 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 20 6f 72  equal to zero or
34f40 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
34f50 6f 63 61 74 65 20 65 72 72 6f 72 20 6f 63 63 75  ocate error occu
34f60 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68  rs...**..** ^(Th
34f70 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63  e amount of spac
34f80 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  e allocated by s
34f90 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
34fa0 5f 63 6f 6e 74 65 78 74 28 43 2c 4e 29 20 69 73  _context(C,N) is
34fb0 0d 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20  ..** determined 
34fc0 62 79 20 74 68 65 20 4e 20 70 61 72 61 6d 65 74  by the N paramet
34fd0 65 72 20 6f 6e 20 66 69 72 73 74 20 73 75 63 63  er on first succ
34fe0 65 73 73 66 75 6c 20 63 61 6c 6c 2e 20 20 43 68  essful call.  Ch
34ff0 61 6e 67 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 76  anging the..** v
35000 61 6c 75 65 20 6f 66 20 4e 20 69 6e 20 73 75 62  alue of N in sub
35010 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20  sequent call to 
35020 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
35030 65 5f 63 6f 6e 74 65 78 74 28 29 20 77 69 74 68  e_context() with
35040 69 6e 0d 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  in..** the same 
35050 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
35060 6f 6e 20 69 6e 73 74 61 6e 63 65 20 77 69 6c 6c  on instance will
35070 20 6e 6f 74 20 72 65 73 69 7a 65 20 74 68 65 20   not resize the 
35080 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 61 6c 6c 6f 63  memory..** alloc
35090 61 74 69 6f 6e 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  ation.)^..**..**
350a0 20 5e 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74   ^SQLite automat
350b0 69 63 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65  ically frees the
350c0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 65   memory allocate
350d0 64 20 62 79 20 0d 0a 2a 2a 20 73 71 6c 69 74 65  d by ..** sqlite
350e0 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
350f0 65 78 74 28 29 20 77 68 65 6e 20 74 68 65 20 61  ext() when the a
35100 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20 63  ggregate query c
35110 6f 6e 63 6c 75 64 65 73 2e 0d 0a 2a 2a 0d 0a 2a  oncludes...**..*
35120 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
35130 6d 65 74 65 72 20 6d 75 73 74 20 62 65 20 61 20  meter must be a 
35140 63 6f 70 79 20 6f 66 20 74 68 65 0d 0a 2a 2a 20  copy of the..** 
35150 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
35160 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20   | SQL function 
35170 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69 73  context] that is
35180 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   the first param
35190 65 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68 65 20  eter..** to the 
351a0 78 53 74 65 70 20 6f 72 20 78 46 69 6e 61 6c 20  xStep or xFinal 
351b0 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
351c0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
351d0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 0d 0a   the aggregate..
351e0 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a  ** function...**
351f0 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
35200 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
35210 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
35220 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0d 0a  hread in which..
35230 2a 2a 20 74 68 65 20 61 67 67 72 65 67 61 74 65  ** the aggregate
35240 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73   SQL function is
35250 20 72 75 6e 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53   running...*/..S
35260 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
35270 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
35280 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65  e_context(sqlite
35290 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20  3_context*, int 
352a0 6e 42 79 74 65 73 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  nBytes);..../*..
352b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65  ** CAPI3REF: Use
352c0 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74  r Data For Funct
352d0 69 6f 6e 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ions..**..** ^Th
352e0 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  e sqlite3_user_d
352f0 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
35300 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f  returns a copy o
35310 66 0d 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  f..** the pointe
35320 72 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70  r that was the p
35330 55 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74  UserData paramet
35340 65 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61  er (the 5th para
35350 6d 65 74 65 72 29 0d 0a 2a 2a 20 6f 66 20 74 68  meter)..** of th
35360 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  e [sqlite3_creat
35370 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0d 0a 2a  e_function()]..*
35380 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  * and [sqlite3_c
35390 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
353a0 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ()] routines tha
353b0 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0d 0a 2a 2a  t originally..**
353c0 20 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20   registered the 
353d0 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69  application defi
353e0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a  ned function...*
353f0 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
35400 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
35410 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  d from the same 
35420 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0d  thread in which.
35430 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
35440 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
35450 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e  tion is running.
35460 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
35470 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75   void *sqlite3_u
35480 73 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33  ser_data(sqlite3
35490 5f 63 6f 6e 74 65 78 74 2a 29 3b 0d 0a 0d 0a 2f  _context*);..../
354a0 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  *..** CAPI3REF: 
354b0 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74  Database Connect
354c0 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e  ion For Function
354d0 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73  s..**..** ^The s
354e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
354f0 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 65 72  b_handle() inter
35500 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63  face returns a c
35510 6f 70 79 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 70  opy of..** the p
35520 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 64  ointer to the [d
35530 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
35540 6f 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61 72  on] (the 1st par
35550 61 6d 65 74 65 72 29 0d 0a 2a 2a 20 6f 66 20 74  ameter)..** of t
35560 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
35570 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0d 0a  te_function()]..
35580 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ** and [sqlite3_
35590 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
355a0 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68  6()] routines th
355b0 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0d 0a 2a  at originally..*
355c0 2a 20 72 65 67 69 73 74 65 72 65 64 20 74 68 65  * registered the
355d0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66   application def
355e0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  ined function...
355f0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  */..SQLITE_API s
35600 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f  qlite3 *sqlite3_
35610 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
35620 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
35630 74 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  t*);..../*..** C
35640 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f  API3REF: Functio
35650 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61  n Auxiliary Data
35660 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  ..**..** The fol
35670 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74  lowing two funct
35680 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64  ions may be used
35690 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66   by scalar SQL f
356a0 75 6e 63 74 69 6f 6e 73 20 74 6f 0d 0a 2a 2a 20  unctions to..** 
356b0 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61  associate metada
356c0 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74  ta with argument
356d0 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20   values. If the 
356e0 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61  same value is pa
356f0 73 73 65 64 20 74 6f 0d 0a 2a 2a 20 6d 75 6c 74  ssed to..** mult
35700 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73  iple invocations
35710 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c   of the same SQL
35720 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67   function during
35730 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e   query execution
35740 2c 20 75 6e 64 65 72 0d 0a 2a 2a 20 73 6f 6d 65  , under..** some
35750 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74   circumstances t
35760 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65  he associated me
35770 74 61 64 61 74 61 20 6d 61 79 20 62 65 20 70 72  tadata may be pr
35780 65 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61  eserved. This ma
35790 79 0d 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66  y..** be used, f
357a0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61  or example, to a
357b0 64 64 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70  dd a regular-exp
357c0 72 65 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67  ression matching
357d0 20 73 63 61 6c 61 72 0d 0a 2a 2a 20 66 75 6e 63   scalar..** func
357e0 74 69 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c  tion. The compil
357f0 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  ed version of th
35800 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
35810 73 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 61  sion is stored a
35820 73 0d 0a 2a 2a 20 6d 65 74 61 64 61 74 61 20 61  s..** metadata a
35830 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
35840 68 65 20 53 51 4c 20 76 61 6c 75 65 20 70 61 73  he SQL value pas
35850 73 65 64 20 61 73 20 74 68 65 20 72 65 67 75 6c  sed as the regul
35860 61 72 20 65 78 70 72 65 73 73 69 6f 6e 0d 0a 2a  ar expression..*
35870 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68 65 20  * pattern.  The 
35880 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c 61 72  compiled regular
35890 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20   expression can 
358a0 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c  be reused on mul
358b0 74 69 70 6c 65 0d 0a 2a 2a 20 69 6e 76 6f 63 61  tiple..** invoca
358c0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d  tions of the sam
358d0 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 68  e function so th
358e0 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  at the original 
358f0 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0d 0a  pattern string..
35900 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ** does not need
35910 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65   to be recompile
35920 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  d on each invoca
35930 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  tion...**..** ^T
35940 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  he sqlite3_get_a
35950 75 78 64 61 74 61 28 29 20 69 6e 74 65 72 66 61  uxdata() interfa
35960 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
35970 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 74 61  nter to the meta
35980 64 61 74 61 0d 0a 2a 2a 20 61 73 73 6f 63 69 61  data..** associa
35990 74 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74  ted by the sqlit
359a0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
359b0 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
359c0 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 0d  he Nth argument.
359d0 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 68 65  .** value to the
359e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
359f0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 5e  ined function. ^
35a00 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 68  If no metadata h
35a10 61 73 20 62 65 65 6e 20 65 76 65 72 0d 0a 2a 2a  as been ever..**
35a20 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68   been set for th
35a30 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f  e Nth argument o
35a40 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20  f the function, 
35a50 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73  or if the corres
35a60 70 6f 6e 64 69 6e 67 0d 0a 2a 2a 20 66 75 6e 63  ponding..** func
35a70 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
35a80 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
35a90 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
35aa0 61 73 20 73 65 74 2c 0d 0a 2a 2a 20 74 68 65 6e  as set,..** then
35ab0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
35ac0 64 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61  data() returns a
35ad0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a   NULL pointer...
35ae0 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  **..** ^The sqli
35af0 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
35b00 29 20 69 6e 74 65 72 66 61 63 65 20 73 61 76 65  ) interface save
35b10 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 0d 0a  s the metadata..
35b20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ** pointed to by
35b30 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 74   its 3rd paramet
35b40 65 72 20 61 73 20 74 68 65 20 6d 65 74 61 64 61  er as the metada
35b50 74 61 20 66 6f 72 20 74 68 65 20 4e 2d 74 68 0d  ta for the N-th.
35b60 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66 20  .** argument of 
35b70 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
35b80 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
35b90 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0d 0a 2a  .  Subsequent..*
35ba0 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
35bb0 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
35bc0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74 68   might return th
35bd0 69 73 20 64 61 74 61 2c 20 69 66 20 69 74 20 68  is data, if it h
35be0 61 73 0d 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20  as..** not been 
35bf0 64 65 73 74 72 6f 79 65 64 2e 0d 0a 2a 2a 20 5e  destroyed...** ^
35c00 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
35c10 4c 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69  L, SQLite will i
35c20 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75  nvoke the destru
35c30 63 74 6f 72 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f  ctor..** functio
35c40 6e 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 34  n given by the 4
35c50 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
35c60 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
35c70 61 74 61 28 29 20 6f 6e 0d 0a 2a 2a 20 74 68 65  ata() on..** the
35c80 20 6d 65 74 61 64 61 74 61 20 77 68 65 6e 20 74   metadata when t
35c90 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
35ca0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
35cb0 74 65 72 20 63 68 61 6e 67 65 73 0d 0a 2a 2a 20  ter changes..** 
35cc0 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 4c 20  or when the SQL 
35cd0 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 6c 65  statement comple
35ce0 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20 63  tes, whichever c
35cf0 6f 6d 65 73 20 66 69 72 73 74 2e 0d 0a 2a 2a 0d  omes first...**.
35d00 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72  .** SQLite is fr
35d10 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64  ee to call the d
35d20 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72  estructor and dr
35d30 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61  op metadata on a
35d40 6e 79 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ny..** parameter
35d50 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e   of any function
35d60 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 5e   at any time.  ^
35d70 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74  The only guarant
35d80 65 65 20 69 73 20 74 68 61 74 0d 0a 2a 2a 20 74  ee is that..** t
35d90 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 69  he destructor wi
35da0 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66  ll be called bef
35db0 6f 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 61  ore the metadata
35dc0 20 69 73 20 64 72 6f 70 70 65 64 2e 0d 0a 2a 2a   is dropped...**
35dd0 0d 0a 2a 2a 20 5e 28 49 6e 20 70 72 61 63 74 69  ..** ^(In practi
35de0 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20  ce, metadata is 
35df0 70 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65  preserved betwee
35e00 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  n function calls
35e10 20 66 6f 72 0d 0a 2a 2a 20 65 78 70 72 65 73 73   for..** express
35e20 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f  ions that are co
35e30 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c  nstant at compil
35e40 65 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63  e time. This inc
35e50 6c 75 64 65 73 20 6c 69 74 65 72 61 6c 0d 0a 2a  ludes literal..*
35e60 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 5b 70 61  * values and [pa
35e70 72 61 6d 65 74 65 72 73 5d 2e 29 5e 0d 0a 2a 2a  rameters].)^..**
35e80 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  ..** These routi
35e90 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  nes must be call
35ea0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ed from the same
35eb0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
35ec0 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e  ..** the SQL fun
35ed0 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
35ee0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
35ef0 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
35f00 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69  get_auxdata(sqli
35f10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
35f20 74 20 4e 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50  t N);..SQLITE_AP
35f30 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73  I void sqlite3_s
35f40 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74  et_auxdata(sqlit
35f50 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
35f60 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20   N, void*, void 
35f70 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 0d 0a  (*)(void*));....
35f80 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
35f90 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66  F: Constants Def
35fa0 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65  ining Special De
35fb0 73 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f  structor Behavio
35fc0 72 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20  r..**..** These 
35fd0 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75  are special valu
35fe0 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72  es for the destr
35ff0 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61  uctor that is pa
36000 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 0d 0a  ssed in as the..
36010 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e  ** final argumen
36020 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69  t to routines li
36030 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  ke [sqlite3_resu
36040 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 5e 49 66  lt_blob()].  ^If
36050 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0d   the destructor.
36060 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
36070 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69  SQLITE_STATIC, i
36080 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
36090 20 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72   content pointer
360a0 20 69 73 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a 2a   is constant..**
360b0 20 61 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20   and will never 
360c0 63 68 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73  change.  It does
360d0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
360e0 64 65 73 74 72 6f 79 65 64 2e 20 20 5e 54 68 65  destroyed.  ^The
360f0 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e  ..** SQLITE_TRAN
36100 53 49 45 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e  SIENT value mean
36110 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65  s that the conte
36120 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63  nt will likely c
36130 68 61 6e 67 65 20 69 6e 0d 0a 2a 2a 20 74 68 65  hange in..** the
36140 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 6e 64   near future and
36150 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 68 6f   that SQLite sho
36160 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e  uld make its own
36170 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
36180 0d 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  ..** the content
36190 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
361a0 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74  g...**..** The t
361b0 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73  ypedef is necess
361c0 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  ary to work arou
361d0 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63  nd problems in c
361e0 65 72 74 61 69 6e 0d 0a 2a 2a 20 43 2b 2b 20 63  ertain..** C++ c
361f0 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74  ompilers.  See t
36200 69 63 6b 65 74 20 23 32 31 39 31 2e 0d 0a 2a 2f  icket #2191...*/
36210 0d 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 28  ..typedef void (
36220 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63  *sqlite3_destruc
36230 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a 29  tor_type)(void*)
36240 3b 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  ;..#define SQLIT
36250 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28  E_STATIC      ((
36260 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74  sqlite3_destruct
36270 6f 72 5f 74 79 70 65 29 30 29 0d 0a 23 64 65 66  or_type)0)..#def
36280 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ine SQLITE_TRANS
36290 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33  IENT   ((sqlite3
362a0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
362b0 29 2d 31 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  )-1)..../*..** C
362c0 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67  API3REF: Setting
362d0 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41   The Result Of A
362e0 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 0d 0a  n SQL Function..
362f0 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  **..** These rou
36300 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62  tines are used b
36310 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78  y the xFunc or x
36320 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20  Final callbacks 
36330 74 68 61 74 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65  that..** impleme
36340 6e 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  nt SQL functions
36350 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e   and aggregates.
36360 20 20 53 65 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74    See..** [sqlit
36370 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
36380 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  on()] and [sqlit
36390 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
363a0 6f 6e 31 36 28 29 5d 0d 0a 2a 2a 20 66 6f 72 20  on16()]..** for 
363b0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
363c0 6d 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  mation...**..** 
363d0 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  These functions 
363e0 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 20 6c  work very much l
363f0 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d 65 74  ike the [paramet
36400 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 6d 69  er binding] fami
36410 6c 79 20 6f 66 0d 0a 2a 2a 20 66 75 6e 63 74 69  ly of..** functi
36420 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64  ons used to bind
36430 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20   values to host 
36440 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72  parameters in pr
36450 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
36460 73 2e 0d 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20  s...** Refer to 
36470 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74  the [SQL paramet
36480 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  er] documentatio
36490 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  n for additional
364a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a   information...*
364b0 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  *..** ^The sqlit
364c0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29  e3_result_blob()
364d0 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20   interface sets 
364e0 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0d  the result from.
364f0 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 69  .** an applicati
36500 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
36510 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 42 4c  ion to be the BL
36520 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74  OB whose content
36530 20 69 73 20 70 6f 69 6e 74 65 64 0d 0a 2a 2a 20   is pointed..** 
36540 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  to by the second
36550 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 77   parameter and w
36560 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 20  hich is N bytes 
36570 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 20  long where N is 
36580 74 68 65 0d 0a 2a 2a 20 74 68 69 72 64 20 70 61  the..** third pa
36590 72 61 6d 65 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  rameter...**..**
365a0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65   ^The sqlite3_re
365b0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20  sult_zeroblob() 
365c0 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20 74  interfaces set t
365d0 68 65 20 72 65 73 75 6c 74 20 6f 66 0d 0a 2a 2a  he result of..**
365e0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
365f0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
36600 6e 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63  n to be a BLOB c
36610 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65  ontaining all ze
36620 72 6f 0d 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64  ro..** bytes and
36630 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65   N bytes in size
36640 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
36650 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e   value of the 2n
36660 64 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a 2a 2a  d parameter...**
36670 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
36680 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
36690 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73  ) interface sets
366a0 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f 6d   the result from
366b0 0d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74  ..** an applicat
366c0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
366d0 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c 6f  tion to be a flo
366e0 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
366f0 65 20 73 70 65 63 69 66 69 65 64 0d 0a 2a 2a 20  e specified..** 
36700 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d  by its 2nd argum
36710 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ent...**..** ^Th
36720 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
36730 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c  _error() and sql
36740 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
36750 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0d  r16() functions.
36760 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69 6d  .** cause the im
36770 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 75  plemented SQL fu
36780 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20  nction to throw 
36790 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0d 0a 2a  an exception...*
367a0 2a 20 5e 53 51 4c 69 74 65 20 75 73 65 73 20 74  * ^SQLite uses t
367b0 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65  he string pointe
367c0 64 20 74 6f 20 62 79 20 74 68 65 0d 0a 2a 2a 20  d to by the..** 
367d0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66  2nd parameter of
367e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
367f0 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74  error() or sqlit
36800 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
36810 36 28 29 0d 0a 2a 2a 20 61 73 20 74 68 65 20 74  6()..** as the t
36820 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20  ext of an error 
36830 6d 65 73 73 61 67 65 2e 20 20 5e 53 51 4c 69 74  message.  ^SQLit
36840 65 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65  e interprets the
36850 20 65 72 72 6f 72 0d 0a 2a 2a 20 6d 65 73 73 61   error..** messa
36860 67 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73  ge string from s
36870 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
36880 72 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e 20  ror() as UTF-8. 
36890 5e 53 51 4c 69 74 65 0d 0a 2a 2a 20 69 6e 74 65  ^SQLite..** inte
368a0 72 70 72 65 74 73 20 74 68 65 20 73 74 72 69 6e  rprets the strin
368b0 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72  g from sqlite3_r
368c0 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20  esult_error16() 
368d0 61 73 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74  as UTF-16 in nat
368e0 69 76 65 0d 0a 2a 2a 20 62 79 74 65 20 6f 72 64  ive..** byte ord
368f0 65 72 2e 20 20 5e 49 66 20 74 68 65 20 74 68 69  er.  ^If the thi
36900 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
36910 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
36920 72 72 6f 72 28 29 0d 0a 2a 2a 20 6f 72 20 73 71  rror()..** or sq
36930 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36940 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 69  or16() is negati
36950 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74  ve then SQLite t
36960 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 6f  akes as the erro
36970 72 0d 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c  r..** message al
36980 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67  l text up throug
36990 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  h the first zero
369a0 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 20   character...** 
369b0 5e 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61  ^If the third pa
369c0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
369d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
369e0 29 20 6f 72 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  ) or..** sqlite3
369f0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
36a00 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  ) is non-negativ
36a10 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61  e then SQLite ta
36a20 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 0d 0a 2a  kes that many..*
36a30 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61  * bytes (not cha
36a40 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 74 68  racters) from th
36a50 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
36a60 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  as the error mes
36a70 73 61 67 65 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73  sage...** ^The s
36a80 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
36a90 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ror() and sqlite
36aa0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
36ab0 28 29 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ()..** routines 
36ac0 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63  make a private c
36ad0 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72  opy of the error
36ae0 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65   message text be
36af0 66 6f 72 65 0d 0a 2a 2a 20 74 68 65 79 20 72 65  fore..** they re
36b00 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68  turn.  Hence, th
36b10 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
36b20 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74  on can deallocat
36b30 65 20 6f 72 0d 0a 2a 2a 20 6d 6f 64 69 66 79 20  e or..** modify 
36b40 74 68 65 20 74 65 78 74 20 61 66 74 65 72 20 74  the text after t
36b50 68 65 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f  hey return witho
36b60 75 74 20 68 61 72 6d 2e 0d 0a 2a 2a 20 5e 54 68  ut harm...** ^Th
36b70 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
36b80 5f 65 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75  _error_code() fu
36b90 6e 63 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74  nction changes t
36ba0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0d 0a 2a  he error code..*
36bb0 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  * returned by SQ
36bc0 4c 69 74 65 20 61 73 20 61 20 72 65 73 75 6c 74  Lite as a result
36bd0 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 69 6e 20   of an error in 
36be0 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 5e 42 79  a function.  ^By
36bf0 20 64 65 66 61 75 6c 74 2c 0d 0a 2a 2a 20 74 68   default,..** th
36c00 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
36c10 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 5e  SQLITE_ERROR.  ^
36c20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  A subsequent cal
36c30 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73  l to sqlite3_res
36c40 75 6c 74 5f 65 72 72 6f 72 28 29 0d 0a 2a 2a 20  ult_error()..** 
36c50 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
36c60 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65  t_error16() rese
36c70 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
36c80 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f  e to SQLITE_ERRO
36c90 52 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  R...**..** ^The 
36ca0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
36cb0 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 66 61 63  oobig() interfac
36cc0 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 65 20  e causes SQLite 
36cd0 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f  to throw an erro
36ce0 72 0d 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67  r..** indicating
36cf0 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 20 6f   that a string o
36d00 72 20 42 4c 4f 42 20 69 73 20 74 6f 6f 20 6c 6f  r BLOB is too lo
36d10 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e  ng to represent.
36d20 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71  ..**..** ^The sq
36d30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d  lite3_result_nom
36d40 65 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20 63  em() interface c
36d50 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20  auses SQLite to 
36d60 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0d 0a  throw an error..
36d70 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ** indicating th
36d80 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  at a memory allo
36d90 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0d 0a  cation failed...
36da0 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  **..** ^The sqli
36db0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 29  te3_result_int()
36dc0 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20   interface sets 
36dd0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
36de0 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c  ..** of the appl
36df0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
36e00 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74  function to be t
36e10 68 65 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64  he 32-bit signed
36e20 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 76 61 6c   integer..** val
36e30 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  ue given in the 
36e40 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a  2nd argument...*
36e50 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72  * ^The sqlite3_r
36e60 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e  esult_int64() in
36e70 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
36e80 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a   return value..*
36e90 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
36ea0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
36eb0 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
36ec0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
36ed0 74 65 67 65 72 0d 0a 2a 2a 20 76 61 6c 75 65 20  teger..** value 
36ee0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64  given in the 2nd
36ef0 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a   argument...**..
36f00 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
36f10 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e  result_null() in
36f20 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
36f30 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a   return value..*
36f40 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
36f50 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
36f60 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c  ction to be NULL
36f70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73  ...**..** ^The s
36f80 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
36f90 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65  xt(), sqlite3_re
36fa0 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0d 0a  sult_text16(),..
36fb0 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ** sqlite3_resul
36fc0 74 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e  t_text16le(), an
36fd0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
36fe0 5f 74 65 78 74 31 36 62 65 28 29 20 69 6e 74 65  _text16be() inte
36ff0 72 66 61 63 65 73 0d 0a 2a 2a 20 73 65 74 20 74  rfaces..** set t
37000 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
37010 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  of the applicati
37020 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
37030 69 6f 6e 20 74 6f 20 62 65 0d 0a 2a 2a 20 61 20  ion to be..** a 
37040 74 65 78 74 20 73 74 72 69 6e 67 20 77 68 69 63  text string whic
37050 68 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  h is represented
37060 20 61 73 20 55 54 46 2d 38 2c 20 55 54 46 2d 31   as UTF-8, UTF-1
37070 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  6 native byte or
37080 64 65 72 2c 0d 0a 2a 2a 20 55 54 46 2d 31 36 20  der,..** UTF-16 
37090 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 20 6f  little endian, o
370a0 72 20 55 54 46 2d 31 36 20 62 69 67 20 65 6e 64  r UTF-16 big end
370b0 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ian, respectivel
370c0 79 2e 0d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 74  y...** ^SQLite t
370d0 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 65  akes the text re
370e0 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 70  sult from the ap
370f0 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0d 0a  plication from..
37100 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  ** the 2nd param
37110 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69  eter of the sqli
37120 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
37130 20 69 6e 74 65 72 66 61 63 65 73 2e 0d 0a 2a 2a   interfaces...**
37140 20 5e 49 66 20 74 68 65 20 33 72 64 20 70 61 72   ^If the 3rd par
37150 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
37160 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
37170 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0d 0a 2a  t* interfaces..*
37180 2a 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  * is negative, t
37190 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73  hen SQLite takes
371a0 20 72 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f   result text fro
371b0 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65  m the 2nd parame
371c0 74 65 72 0d 0a 2a 2a 20 74 68 72 6f 75 67 68 20  ter..** through 
371d0 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63  the first zero c
371e0 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 20 5e 49  haracter...** ^I
371f0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
37200 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
37210 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
37220 69 6e 74 65 72 66 61 63 65 73 0d 0a 2a 2a 20 69  interfaces..** i
37230 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
37240 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74  then as many byt
37250 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65  es (not characte
37260 72 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0d  rs) of the text.
37270 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  .** pointed to b
37280 79 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65  y the 2nd parame
37290 74 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73  ter are taken as
372a0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
372b0 2d 64 65 66 69 6e 65 64 0d 0a 2a 2a 20 66 75 6e  -defined..** fun
372c0 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 20 20 49  ction result.  I
372d0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
372e0 74 65 72 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  ter is non-negat
372f0 69 76 65 2c 20 74 68 65 6e 20 69 74 0d 0a 2a 2a  ive, then it..**
37300 20 6d 75 73 74 20 62 65 20 74 68 65 20 62 79 74   must be the byt
37310 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  e offset into th
37320 65 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 74  e string where t
37330 68 65 20 4e 55 4c 20 74 65 72 6d 69 6e 61 74 6f  he NUL terminato
37340 72 20 77 6f 75 6c 64 0d 0a 2a 2a 20 61 70 70 65  r would..** appe
37350 61 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  ar if the string
37360 20 77 68 65 72 65 20 4e 55 4c 20 74 65 72 6d 69   where NUL termi
37370 6e 61 74 65 64 2e 20 20 49 66 20 61 6e 79 20 4e  nated.  If any N
37380 55 4c 20 63 68 61 72 61 63 74 65 72 73 20 6f 63  UL characters oc
37390 63 75 72 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 73  cur..** in the s
373a0 74 72 69 6e 67 20 61 74 20 61 20 62 79 74 65 20  tring at a byte 
373b0 6f 66 66 73 65 74 20 74 68 61 74 20 69 73 20 6c  offset that is l
373c0 65 73 73 20 74 68 61 6e 20 74 68 65 20 76 61 6c  ess than the val
373d0 75 65 20 6f 66 20 74 68 65 20 33 72 64 0d 0a 2a  ue of the 3rd..*
373e0 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65  * parameter, the
373f0 6e 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  n the resulting 
37400 73 74 72 69 6e 67 20 77 69 6c 6c 20 63 6f 6e 74  string will cont
37410 61 69 6e 20 65 6d 62 65 64 64 65 64 20 4e 55 4c  ain embedded NUL
37420 73 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 72 65  s and the..** re
37430 73 75 6c 74 20 6f 66 20 65 78 70 72 65 73 73 69  sult of expressi
37440 6f 6e 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e  ons operating on
37450 20 73 74 72 69 6e 67 73 20 77 69 74 68 20 65 6d   strings with em
37460 62 65 64 64 65 64 20 4e 55 4c 73 20 69 73 20 75  bedded NULs is u
37470 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 20 5e 49  ndefined...** ^I
37480 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  f the 4th parame
37490 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
374a0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
374b0 69 6e 74 65 72 66 61 63 65 73 0d 0a 2a 2a 20 6f  interfaces..** o
374c0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  r sqlite3_result
374d0 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e  _blob is a non-N
374e0 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65  ULL pointer, the
374f0 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  n SQLite calls t
37500 68 61 74 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  hat..** function
37510 20 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74   as the destruct
37520 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f  or on the text o
37530 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68  r BLOB result wh
37540 65 6e 20 69 74 20 68 61 73 0d 0a 2a 2a 20 66 69  en it has..** fi
37550 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 61  nished using tha
37560 74 20 72 65 73 75 6c 74 2e 0d 0a 2a 2a 20 5e 49  t result...** ^I
37570 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  f the 4th parame
37580 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
37590 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
375a0 69 6e 74 65 72 66 61 63 65 73 20 6f 72 20 74 6f  interfaces or to
375b0 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  ..** sqlite3_res
375c0 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20  ult_blob is the 
375d0 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74  special constant
375e0 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20   SQLITE_STATIC, 
375f0 74 68 65 6e 20 53 51 4c 69 74 65 0d 0a 2a 2a 20  then SQLite..** 
37600 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
37610 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65   text or BLOB re
37620 73 75 6c 74 20 69 73 20 69 6e 20 63 6f 6e 73 74  sult is in const
37630 61 6e 74 20 73 70 61 63 65 20 61 6e 64 20 64 6f  ant space and do
37640 65 73 20 6e 6f 74 0d 0a 2a 2a 20 63 6f 70 79 20  es not..** copy 
37650 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
37660 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e 6f 72  he parameter nor
37670 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 63 74   call a destruct
37680 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  or on the conten
37690 74 0d 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61  t..** when it ha
376a0 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
376b0 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0d 0a 2a   that result...*
376c0 2a 20 5e 49 66 20 74 68 65 20 34 74 68 20 70 61  * ^If the 4th pa
376d0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73  rameter to the s
376e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
376f0 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0d 0a  xt* interfaces..
37700 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65  ** or sqlite3_re
37710 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65  sult_blob is the
37720 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e   special constan
37730 74 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  t SQLITE_TRANSIE
37740 4e 54 0d 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69  NT..** then SQLi
37750 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20  te makes a copy 
37760 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  of the result in
37770 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
37780 64 20 66 72 6f 6d 0d 0a 2a 2a 20 66 72 6f 6d 20  d from..** from 
37790 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
377a0 29 5d 20 62 65 66 6f 72 65 20 69 74 20 72 65 74  )] before it ret
377b0 75 72 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  urns...**..** ^T
377c0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
377d0 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66  t_value() interf
377e0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73  ace sets the res
377f0 75 6c 74 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 61  ult of..** the a
37800 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
37810 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
37820 65 20 61 20 63 6f 70 79 20 74 68 65 0d 0a 2a 2a  e a copy the..**
37830 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   [unprotected sq
37840 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
37850 65 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79  ect specified by
37860 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
37870 65 72 2e 20 20 5e 54 68 65 0d 0a 2a 2a 20 73 71  er.  ^The..** sq
37880 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
37890 75 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d  ue() interface m
378a0 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74  akes a copy of t
378b0 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
378c0 65 5d 0d 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74  e]..** so that t
378d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
378e0 65 5d 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  e] specified in 
378f0 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6d 61  the parameter ma
37900 79 20 63 68 61 6e 67 65 20 6f 72 0d 0a 2a 2a 20  y change or..** 
37910 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61  be deallocated a
37920 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 65 73  fter sqlite3_res
37930 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 74 75  ult_value() retu
37940 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 72 6d  rns without harm
37950 2e 0d 0a 2a 2a 20 5e 41 20 5b 70 72 6f 74 65 63  ...** ^A [protec
37960 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
37970 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c  e] object may al
37980 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65  ways be used whe
37990 72 65 20 61 6e 0d 0a 2a 2a 20 5b 75 6e 70 72 6f  re an..** [unpro
379a0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
379b0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20  alue] object is 
379c0 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74  required, so eit
379d0 68 65 72 0d 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20  her..** kind of 
379e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
379f0 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73  object can be us
37a00 65 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74  ed with this int
37a10 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  erface...**..** 
37a20 49 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  If these routine
37a30 73 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f  s are called fro
37a40 6d 20 77 69 74 68 69 6e 20 74 68 65 20 64 69 66  m within the dif
37a50 66 65 72 65 6e 74 20 74 68 72 65 61 64 0d 0a 2a  ferent thread..*
37a60 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 63  * than the one c
37a70 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 61 70  ontaining the ap
37a80 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
37a90 64 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  d function that 
37aa0 72 65 63 65 69 76 65 64 0d 0a 2a 2a 20 74 68 65  received..** the
37ab0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
37ac0 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 20  t] pointer, the 
37ad0 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65  results are unde
37ae0 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  fined...*/..SQLI
37af0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
37b00 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
37b10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
37b20 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
37b30 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  nt, void(*)(void
37b40 2a 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  *));..SQLITE_API
37b50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
37b60 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
37b70 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
37b80 75 62 6c 65 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  uble);..SQLITE_A
37b90 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
37ba0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c  result_error(sql
37bb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
37bc0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
37bd0 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  ;..SQLITE_API vo
37be0 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
37bf0 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65  t_error16(sqlite
37c00 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73  3_context*, cons
37c10 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0d 0a  t void*, int);..
37c20 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
37c30 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37c40 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69  rror_toobig(sqli
37c50 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0d 0a  te3_context*);..
37c60 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
37c70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37c80 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
37c90 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0d 0a 53  e3_context*);..S
37ca0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
37cb0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37cc0 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33  ror_code(sqlite3
37cd0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b  _context*, int);
37ce0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ..SQLITE_API voi
37cf0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
37d00 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  _int(sqlite3_con
37d10 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51  text*, int);..SQ
37d20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
37d30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
37d40 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  64(sqlite3_conte
37d50 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  xt*, sqlite3_int
37d60 36 34 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  64);..SQLITE_API
37d70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
37d80 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65  sult_null(sqlite
37d90 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0d 0a 53 51  3_context*);..SQ
37da0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
37db0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
37dc0 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
37dd0 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  t*, const char*,
37de0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
37df0 69 64 2a 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  id*));..SQLITE_A
37e00 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
37e10 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71  result_text16(sq
37e20 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
37e30 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
37e40 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
37e50 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  );..SQLITE_API v
37e60 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
37e70 6c 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  lt_text16le(sqli
37e80 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
37e90 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76  nst void*, int,v
37ea0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d  oid(*)(void*));.
37eb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
37ec0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37ed0 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33  text16be(sqlite3
37ee0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
37ef0 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64   void*, int,void
37f00 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 53 51  (*)(void*));..SQ
37f10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
37f20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
37f30 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ue(sqlite3_conte
37f40 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  xt*, sqlite3_val
37f50 75 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50  ue*);..SQLITE_AP
37f60 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
37f70 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73  esult_zeroblob(s
37f80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
37f90 20 69 6e 74 20 6e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a   int n);..../*..
37fa0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66  ** CAPI3REF: Def
37fb0 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e  ine New Collatin
37fc0 67 20 53 65 71 75 65 6e 63 65 73 0d 0a 2a 2a 0d  g Sequences..**.
37fd0 0a 2a 2a 20 5e 54 68 65 73 65 20 66 75 6e 63 74  .** ^These funct
37fe0 69 6f 6e 73 20 61 64 64 2c 20 72 65 6d 6f 76 65  ions add, remove
37ff0 2c 20 6f 72 20 6d 6f 64 69 66 79 20 61 20 5b 63  , or modify a [c
38000 6f 6c 6c 61 74 69 6f 6e 5d 20 61 73 73 6f 63 69  ollation] associ
38010 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68 20 74 68  ated..** with th
38020 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
38030 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65  ection] specifie
38040 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
38050 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a  rgument...**..**
38060 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68   ^The name of th
38070 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 20 61  e collation is a
38080 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0d 0a 2a   UTF-8 string..*
38090 2a 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72  * for sqlite3_cr
380a0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
380b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 72 65   and sqlite3_cre
380c0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
380d0 28 29 0d 0a 2a 2a 20 61 6e 64 20 61 20 55 54 46  ()..** and a UTF
380e0 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61  -16 string in na
380f0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
38100 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61  for sqlite3_crea
38110 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29  te_collation16()
38120 2e 0d 0a 2a 2a 20 5e 43 6f 6c 6c 61 74 69 6f 6e  ...** ^Collation
38130 20 6e 61 6d 65 73 20 74 68 61 74 20 63 6f 6d 70   names that comp
38140 61 72 65 20 65 71 75 61 6c 20 61 63 63 6f 72 64  are equal accord
38150 69 6e 67 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ing to [sqlite3_
38160 73 74 72 6e 69 63 6d 70 28 29 5d 20 61 72 65 0d  strnicmp()] are.
38170 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 74  .** considered t
38180 6f 20 62 65 20 74 68 65 20 73 61 6d 65 20 6e 61  o be the same na
38190 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68  me...**..** ^(Th
381a0 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
381b0 20 28 65 54 65 78 74 52 65 70 29 20 6d 75 73 74   (eTextRep) must
381c0 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   be one of the c
381d0 6f 6e 73 74 61 6e 74 73 3a 0d 0a 2a 2a 20 3c 75  onstants:..** <u
381e0 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  l>..** <li> [SQL
381f0 49 54 45 5f 55 54 46 38 5d 2c 0d 0a 2a 2a 20 3c  ITE_UTF8],..** <
38200 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54 46 31  li> [SQLITE_UTF1
38210 36 4c 45 5d 2c 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b  6LE],..** <li> [
38220 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
38230 0d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ..** <li> [SQLIT
38240 45 5f 55 54 46 31 36 5d 2c 20 6f 72 0d 0a 2a 2a  E_UTF16], or..**
38250 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54   <li> [SQLITE_UT
38260 46 31 36 5f 41 4c 49 47 4e 45 44 5d 2e 0d 0a 2a  F16_ALIGNED]...*
38270 2a 20 3c 2f 75 6c 3e 29 5e 0d 0a 2a 2a 20 5e 54  * </ul>)^..** ^T
38280 68 65 20 65 54 65 78 74 52 65 70 20 61 72 67 75  he eTextRep argu
38290 6d 65 6e 74 20 64 65 74 65 72 6d 69 6e 65 73 20  ment determines 
382a0 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
382b0 73 74 72 69 6e 67 73 20 70 61 73 73 65 64 0d 0a  strings passed..
382c0 2a 2a 20 74 6f 20 74 68 65 20 63 6f 6c 6c 61 74  ** to the collat
382d0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  ing function cal
382e0 6c 62 61 63 6b 2c 20 78 43 61 6c 6c 62 61 63 6b  lback, xCallback
382f0 2e 0d 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49  ...** ^The [SQLI
38300 54 45 5f 55 54 46 31 36 5d 20 61 6e 64 20 5b 53  TE_UTF16] and [S
38310 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
38320 4e 45 44 5d 20 76 61 6c 75 65 73 20 66 6f 72 20  NED] values for 
38330 65 54 65 78 74 52 65 70 0d 0a 2a 2a 20 66 6f 72  eTextRep..** for
38340 63 65 20 73 74 72 69 6e 67 73 20 74 6f 20 62 65  ce strings to be
38350 20 55 54 46 31 36 20 77 69 74 68 20 6e 61 74 69   UTF16 with nati
38360 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0d 0a  ve byte order...
38370 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f  ** ^The [SQLITE_
38380 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 76  UTF16_ALIGNED] v
38390 61 6c 75 65 20 66 6f 72 20 65 54 65 78 74 52 65  alue for eTextRe
383a0 70 20 66 6f 72 63 65 73 20 73 74 72 69 6e 67 73  p forces strings
383b0 20 74 6f 20 62 65 67 69 6e 0d 0a 2a 2a 20 6f 6e   to begin..** on
383c0 20 61 6e 20 65 76 65 6e 20 62 79 74 65 20 61 64   an even byte ad
383d0 64 72 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  dress...**..** ^
383e0 54 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  The fourth argum
383f0 65 6e 74 2c 20 70 41 72 67 2c 20 69 73 20 61 6e  ent, pArg, is an
38400 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74   application dat
38410 61 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69  a pointer that i
38420 73 20 70 61 73 73 65 64 0d 0a 2a 2a 20 74 68 72  s passed..** thr
38430 6f 75 67 68 20 61 73 20 74 68 65 20 66 69 72 73  ough as the firs
38440 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
38450 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  e collating func
38460 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 0d 0a  tion callback...
38470 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 66 74  **..** ^The fift
38480 68 20 61 72 67 75 6d 65 6e 74 2c 20 78 43 61 6c  h argument, xCal
38490 6c 62 61 63 6b 2c 20 69 73 20 61 20 70 6f 69 6e  lback, is a poin
384a0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 6c 61  ter to the colla
384b0 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  ting function...
384c0 2a 2a 20 5e 4d 75 6c 74 69 70 6c 65 20 63 6f 6c  ** ^Multiple col
384d0 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lating functions
384e0 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72   can be register
384f0 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ed using the sam
38500 65 20 6e 61 6d 65 20 62 75 74 0d 0a 2a 2a 20 77  e name but..** w
38510 69 74 68 20 64 69 66 66 65 72 65 6e 74 20 65 54  ith different eT
38520 65 78 74 52 65 70 20 70 61 72 61 6d 65 74 65 72  extRep parameter
38530 73 20 61 6e 64 20 53 51 4c 69 74 65 20 77 69 6c  s and SQLite wil
38540 6c 20 75 73 65 20 77 68 69 63 68 65 76 65 72 0d  l use whichever.
38550 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 71  .** function req
38560 75 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20  uires the least 
38570 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 74  amount of data t
38580 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a  ransformation...
38590 2a 2a 20 5e 49 66 20 74 68 65 20 78 43 61 6c 6c  ** ^If the xCall
385a0 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73  back argument is
385b0 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 63   NULL then the c
385c0 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
385d0 6e 20 69 73 0d 0a 2a 2a 20 64 65 6c 65 74 65 64  n is..** deleted
385e0 2e 20 20 5e 57 68 65 6e 20 61 6c 6c 20 63 6f 6c  .  ^When all col
385f0 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lating functions
38600 20 68 61 76 69 6e 67 20 74 68 65 20 73 61 6d 65   having the same
38610 20 6e 61 6d 65 20 61 72 65 20 64 65 6c 65 74 65   name are delete
38620 64 2c 0d 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 6c  d,..** that coll
38630 61 74 69 6f 6e 20 69 73 20 6e 6f 20 6c 6f 6e 67  ation is no long
38640 65 72 20 75 73 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a  er usable...**..
38650 2a 2a 20 5e 54 68 65 20 63 6f 6c 6c 61 74 69 6e  ** ^The collatin
38660 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62  g function callb
38670 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 77  ack is invoked w
38680 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
38690 65 20 70 41 72 67 20 0d 0a 2a 2a 20 61 70 70 6c  e pArg ..** appl
386a0 69 63 61 74 69 6f 6e 20 64 61 74 61 20 70 6f 69  ication data poi
386b0 6e 74 65 72 20 61 6e 64 20 77 69 74 68 20 74 77  nter and with tw
386c0 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  o strings in the
386d0 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66   encoding specif
386e0 69 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20 65  ied..** by the e
386f0 54 65 78 74 52 65 70 20 61 72 67 75 6d 65 6e 74  TextRep argument
38700 2e 20 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67  .  The collating
38710 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 72   function must r
38720 65 74 75 72 6e 20 61 6e 0d 0a 2a 2a 20 69 6e 74  eturn an..** int
38730 65 67 65 72 20 74 68 61 74 20 69 73 20 6e 65 67  eger that is neg
38740 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20  ative, zero, or 
38750 70 6f 73 69 74 69 76 65 0d 0a 2a 2a 20 69 66 20  positive..** if 
38760 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
38770 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65   is less than, e
38780 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61  qual to, or grea
38790 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 65 63  ter than the sec
387a0 6f 6e 64 2c 0d 0a 2a 2a 20 72 65 73 70 65 63 74  ond,..** respect
387b0 69 76 65 6c 79 2e 20 20 41 20 63 6f 6c 6c 61 74  ively.  A collat
387c0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  ing function mus
387d0 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  t always return 
387e0 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65 72 0d  the same answer.
387f0 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 73 61  .** given the sa
38800 6d 65 20 69 6e 70 75 74 73 2e 20 20 49 66 20 74  me inputs.  If t
38810 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6c 6c 61  wo or more colla
38820 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 61  ting functions a
38830 72 65 20 72 65 67 69 73 74 65 72 65 64 0d 0a 2a  re registered..*
38840 2a 20 74 6f 20 74 68 65 20 73 61 6d 65 20 63 6f  * to the same co
38850 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 20 28 75 73  llation name (us
38860 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20 65 54  ing different eT
38870 65 78 74 52 65 70 20 76 61 6c 75 65 73 29 20 74  extRep values) t
38880 68 65 6e 20 61 6c 6c 0d 0a 2a 2a 20 6d 75 73 74  hen all..** must
38890 20 67 69 76 65 20 61 6e 20 65 71 75 69 76 61 6c   give an equival
388a0 65 6e 74 20 61 6e 73 77 65 72 20 77 68 65 6e 20  ent answer when 
388b0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 65 71 75  invoked with equ
388c0 69 76 61 6c 65 6e 74 20 73 74 72 69 6e 67 73 2e  ivalent strings.
388d0 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69  ..** The collati
388e0 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ng function must
388f0 20 6f 62 65 79 20 74 68 65 20 66 6f 6c 6c 6f 77   obey the follow
38900 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 20 66  ing properties f
38910 6f 72 20 61 6c 6c 0d 0a 2a 2a 20 73 74 72 69 6e  or all..** strin
38920 67 73 20 41 2c 20 42 2c 20 61 6e 64 20 43 3a 0d  gs A, B, and C:.
38930 0a 2a 2a 0d 0a 2a 2a 20 3c 6f 6c 3e 0d 0a 2a 2a  .**..** <ol>..**
38940 20 3c 6c 69 3e 20 49 66 20 41 3d 3d 42 20 74 68   <li> If A==B th
38950 65 6e 20 42 3d 3d 41 2e 0d 0a 2a 2a 20 3c 6c 69  en B==A...** <li
38960 3e 20 49 66 20 41 3d 3d 42 20 61 6e 64 20 42 3d  > If A==B and B=
38970 3d 43 20 74 68 65 6e 20 41 3d 3d 43 2e 0d 0a 2a  =C then A==C...*
38980 2a 20 3c 6c 69 3e 20 49 66 20 41 26 6c 74 3b 42  * <li> If A&lt;B
38990 20 54 48 45 4e 20 42 26 67 74 3b 41 2e 0d 0a 2a   THEN B&gt;A...*
389a0 2a 20 3c 6c 69 3e 20 49 66 20 41 26 6c 74 3b 42  * <li> If A&lt;B
389b0 20 61 6e 64 20 42 26 6c 74 3b 43 20 74 68 65 6e   and B&lt;C then
389c0 20 41 26 6c 74 3b 43 2e 0d 0a 2a 2a 20 3c 2f 6f   A&lt;C...** </o
389d0 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20  l>..**..** If a 
389e0 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
389f0 6f 6e 20 66 61 69 6c 73 20 61 6e 79 20 6f 66 20  on fails any of 
38a00 74 68 65 20 61 62 6f 76 65 20 63 6f 6e 73 74 72  the above constr
38a10 61 69 6e 74 73 20 61 6e 64 20 74 68 61 74 0d 0a  aints and that..
38a20 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  ** collating fun
38a30 63 74 69 6f 6e 20 69 73 20 20 72 65 67 69 73 74  ction is  regist
38a40 65 72 65 64 20 61 6e 64 20 75 73 65 64 2c 20 74  ered and used, t
38a50 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
38a60 20 6f 66 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 69   of SQLite..** i
38a70 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a  s undefined...**
38a80 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
38a90 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
38aa0 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69  on_v2() works li
38ab0 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ke sqlite3_creat
38ac0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0d 0a 2a  e_collation()..*
38ad0 2a 20 77 69 74 68 20 74 68 65 20 61 64 64 69 74  * with the addit
38ae0 69 6f 6e 20 74 68 61 74 20 74 68 65 20 78 44 65  ion that the xDe
38af0 73 74 72 6f 79 20 63 61 6c 6c 62 61 63 6b 20 69  stroy callback i
38b00 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 70 41 72  s invoked on pAr
38b10 67 20 77 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 63  g when..** the c
38b20 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
38b30 6e 20 69 73 20 64 65 6c 65 74 65 64 2e 0d 0a 2a  n is deleted...*
38b40 2a 20 5e 43 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  * ^Collating fun
38b50 63 74 69 6f 6e 73 20 61 72 65 20 64 65 6c 65 74  ctions are delet
38b60 65 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  ed when they are
38b70 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6c   overridden by l
38b80 61 74 65 72 0d 0a 2a 2a 20 63 61 6c 6c 73 20 74  ater..** calls t
38b90 6f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  o the collation 
38ba0 63 72 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  creation functio
38bb0 6e 73 20 6f 72 20 77 68 65 6e 20 74 68 65 0d 0a  ns or when the..
38bc0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
38bd0 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c 6f 73  nection] is clos
38be0 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ed using [sqlite
38bf0 33 5f 63 6c 6f 73 65 28 29 5d 2e 0d 0a 2a 2a 0d  3_close()]...**.
38c00 0a 2a 2a 20 5e 54 68 65 20 78 44 65 73 74 72 6f  .** ^The xDestro
38c10 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 3c 75  y callback is <u
38c20 3e 6e 6f 74 3c 2f 75 3e 20 63 61 6c 6c 65 64 20  >not</u> called 
38c30 69 66 20 74 68 65 20 0d 0a 2a 2a 20 73 71 6c 69  if the ..** sqli
38c40 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
38c50 74 69 6f 6e 5f 76 32 28 29 20 66 75 6e 63 74 69  tion_v2() functi
38c60 6f 6e 20 66 61 69 6c 73 2e 20 20 41 70 70 6c 69  on fails.  Appli
38c70 63 61 74 69 6f 6e 73 20 74 68 61 74 20 69 6e 76  cations that inv
38c80 6f 6b 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  oke..** sqlite3_
38c90 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
38ca0 5f 76 32 28 29 20 77 69 74 68 20 61 20 6e 6f 6e  _v2() with a non
38cb0 2d 4e 55 4c 4c 20 78 44 65 73 74 72 6f 79 20 61  -NULL xDestroy a
38cc0 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 0d  rgument should .
38cd0 0a 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 72 65  .** check the re
38ce0 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 64 69  turn code and di
38cf0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 61 70 70  spose of the app
38d00 6c 69 63 61 74 69 6f 6e 20 64 61 74 61 20 70 6f  lication data po
38d10 69 6e 74 65 72 0d 0a 2a 2a 20 74 68 65 6d 73 65  inter..** themse
38d20 6c 76 65 73 20 72 61 74 68 65 72 20 74 68 61 6e  lves rather than
38d30 20 65 78 70 65 63 74 69 6e 67 20 53 51 4c 69 74   expecting SQLit
38d40 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 69  e to deal with i
38d50 74 20 66 6f 72 20 74 68 65 6d 2e 0d 0a 2a 2a 20  t for them...** 
38d60 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e  This is differen
38d70 74 20 66 72 6f 6d 20 65 76 65 72 79 20 6f 74 68  t from every oth
38d80 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
38d90 61 63 65 2e 20 20 54 68 65 20 69 6e 63 6f 6e 73  ace.  The incons
38da0 69 73 74 65 6e 63 79 20 0d 0a 2a 2a 20 69 73 20  istency ..** is 
38db0 75 6e 66 6f 72 74 75 6e 61 74 65 20 62 75 74 20  unfortunate but 
38dc0 63 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  cannot be change
38dd0 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69  d without breaki
38de0 6e 67 20 62 61 63 6b 77 61 72 64 73 20 0d 0a 2a  ng backwards ..*
38df0 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  * compatibility.
38e00 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
38e10 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  o:  [sqlite3_col
38e20 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d  lation_needed()]
38e30 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f   and [sqlite3_co
38e40 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
38e50 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ()]...*/..SQLITE
38e60 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
38e70 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
38e80 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  n(..  sqlite3*, 
38e90 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
38ea0 7a 4e 61 6d 65 2c 20 0d 0a 20 20 69 6e 74 20 65  zName, ..  int e
38eb0 54 65 78 74 52 65 70 2c 20 0d 0a 20 20 76 6f 69  TextRep, ..  voi
38ec0 64 20 2a 70 41 72 67 2c 0d 0a 20 20 69 6e 74 28  d *pArg,..  int(
38ed0 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
38ee0 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
38ef0 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
38f00 29 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50  )..);..SQLITE_AP
38f10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
38f20 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
38f30 32 28 0d 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  2(..  sqlite3*, 
38f40 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
38f50 7a 4e 61 6d 65 2c 20 0d 0a 20 20 69 6e 74 20 65  zName, ..  int e
38f60 54 65 78 74 52 65 70 2c 20 0d 0a 20 20 76 6f 69  TextRep, ..  voi
38f70 64 20 2a 70 41 72 67 2c 0d 0a 20 20 69 6e 74 28  d *pArg,..  int(
38f80 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
38f90 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
38fa0 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
38fb0 29 2c 0d 0a 20 20 76 6f 69 64 28 2a 78 44 65 73  ),..  void(*xDes
38fc0 74 72 6f 79 29 28 76 6f 69 64 2a 29 0d 0a 29 3b  troy)(void*)..);
38fd0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
38fe0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
38ff0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0d 0a 20 20  collation16(..  
39000 73 71 6c 69 74 65 33 2a 2c 20 0d 0a 20 20 63 6f  sqlite3*, ..  co
39010 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c  nst void *zName,
39020 0d 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70  ..  int eTextRep
39030 2c 20 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  , ..  void *pArg
39040 2c 0d 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  ,..  int(*xCompa
39050 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
39060 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
39070 6e 73 74 20 76 6f 69 64 2a 29 0d 0a 29 3b 0d 0a  nst void*)..);..
39080 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
39090 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65  F: Collation Nee
390a0 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 0d 0a 2a  ded Callbacks..*
390b0 2a 0d 0a 2a 2a 20 5e 54 6f 20 61 76 6f 69 64 20  *..** ^To avoid 
390c0 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 74  having to regist
390d0 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f 6e  er all collation
390e0 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f 72   sequences befor
390f0 65 20 61 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a  e a database..**
39100 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20   can be used, a 
39110 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20  single callback 
39120 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
39130 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
39140 74 68 65 0d 0a 2a 2a 20 5b 64 61 74 61 62 61 73  the..** [databas
39150 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f  e connection] to
39160 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
39170 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65  ever an undefine
39180 64 20 63 6f 6c 6c 61 74 69 6f 6e 0d 0a 2a 2a 20  d collation..** 
39190 73 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75  sequence is requ
391a0 69 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49  ired...**..** ^I
391b0 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  f the function i
391c0 73 20 72 65 67 69 73 74 65 72 65 64 20 75 73 69  s registered usi
391d0 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ng the sqlite3_c
391e0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
391f0 29 20 41 50 49 2c 0d 0a 2a 2a 20 74 68 65 6e 20  ) API,..** then 
39200 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68 65  it is passed the
39210 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66 69   names of undefi
39220 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
39230 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69 6e  quences as strin
39240 67 73 0d 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69  gs..** encoded i
39250 6e 20 55 54 46 2d 38 2e 20 5e 49 66 20 73 71 6c  n UTF-8. ^If sql
39260 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
39270 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65  eeded16() is use
39280 64 2c 0d 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73  d,..** the names
39290 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55   are passed as U
392a0 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65  TF-16 in machine
392b0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
392c0 65 72 2e 0d 0a 2a 2a 20 5e 41 20 63 61 6c 6c 20  er...** ^A call 
392d0 74 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69  to either functi
392e0 6f 6e 20 72 65 70 6c 61 63 65 73 20 74 68 65 20  on replaces the 
392f0 65 78 69 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69  existing collati
39300 6f 6e 2d 6e 65 65 64 65 64 20 63 61 6c 6c 62 61  on-needed callba
39310 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 57 68  ck...**..** ^(Wh
39320 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
39330 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20  is invoked, the 
39340 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70  first argument p
39350 61 73 73 65 64 20 69 73 20 61 20 63 6f 70 79 0d  assed is a copy.
39360 0a 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  .** of the secon
39370 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  d argument to sq
39380 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
39390 6e 65 65 64 65 64 28 29 20 6f 72 0d 0a 2a 2a 20  needed() or..** 
393a0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
393b0 6e 5f 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54  n_needed16().  T
393c0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
393d0 6e 74 20 69 73 20 74 68 65 20 64 61 74 61 62 61  nt is the databa
393e0 73 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  se..** connectio
393f0 6e 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72  n.  The third ar
39400 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66  gument is one of
39410 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20   [SQLITE_UTF8], 
39420 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d  [SQLITE_UTF16BE]
39430 2c 0d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45  ,..** or [SQLITE
39440 5f 55 54 46 31 36 4c 45 5d 2c 20 69 6e 64 69 63  _UTF16LE], indic
39450 61 74 69 6e 67 20 74 68 65 20 6d 6f 73 74 20 64  ating the most d
39460 65 73 69 72 61 62 6c 65 20 66 6f 72 6d 20 6f 66  esirable form of
39470 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 0d 0a   the collation..
39480 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 75 6e 63  ** sequence func
39490 74 69 6f 6e 20 72 65 71 75 69 72 65 64 2e 20 20  tion required.  
394a0 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  The fourth param
394b0 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65  eter is the name
394c0 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 72 65 71 75   of the..** requ
394d0 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ired collation s
394e0 65 71 75 65 6e 63 65 2e 29 5e 0d 0a 2a 2a 0d 0a  equence.)^..**..
394f0 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20  ** The callback 
39500 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
39510 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73  register the des
39520 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75  ired collation u
39530 73 69 6e 67 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65  sing..** [sqlite
39540 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
39550 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  on()], [sqlite3_
39560 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
39570 31 36 28 29 5d 2c 20 6f 72 0d 0a 2a 2a 20 5b 73  16()], or..** [s
39580 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
39590 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0d 0a  llation_v2()]...
395a0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
395b0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
395c0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0d 0a 20 20  tion_needed(..  
395d0 73 71 6c 69 74 65 33 2a 2c 20 0d 0a 20 20 76 6f  sqlite3*, ..  vo
395e0 69 64 2a 2c 20 0d 0a 20 20 76 6f 69 64 28 2a 29  id*, ..  void(*)
395f0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
39600 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
39610 73 74 20 63 68 61 72 2a 29 0d 0a 29 3b 0d 0a 53  st char*)..);..S
39620 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
39630 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
39640 6e 65 65 64 65 64 31 36 28 0d 0a 20 20 73 71 6c  needed16(..  sql
39650 69 74 65 33 2a 2c 20 0d 0a 20 20 76 6f 69 64 2a  ite3*, ..  void*
39660 2c 0d 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69  ,..  void(*)(voi
39670 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
39680 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76  eTextRep,const v
39690 6f 69 64 2a 29 0d 0a 29 3b 0d 0a 0d 0a 23 69 66  oid*)..);....#if
396a0 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  def SQLITE_HAS_C
396b0 4f 44 45 43 0d 0a 2f 2a 0d 0a 2a 2a 20 53 70 65  ODEC../*..** Spe
396c0 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f 72  cify the key for
396d0 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61   an encrypted da
396e0 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f  tabase.  This ro
396f0 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 0d  utine should be.
39700 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74  .** called right
39710 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f   after sqlite3_o
39720 70 65 6e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  pen()...**..** T
39730 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  he code to imple
39740 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20 69 73  ment this API is
39750 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69   not available i
39760 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c  n the public rel
39770 65 61 73 65 0d 0a 2a 2a 20 6f 66 20 53 51 4c 69  ease..** of SQLi
39780 74 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  te...*/..SQLITE_
39790 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
397a0 6b 65 79 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  key(..  sqlite3 
397b0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
397c0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
397d0 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64  se to be rekeyed
397e0 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69   */..  const voi
397f0 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
39800 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y     /* The key
39810 20 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..);..../*..*
39820 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6b 65 79  * Change the key
39830 20 6f 6e 20 61 6e 20 6f 70 65 6e 20 64 61 74 61   on an open data
39840 62 61 73 65 2e 20 20 49 66 20 74 68 65 20 63 75  base.  If the cu
39850 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 69  rrent database i
39860 73 20 6e 6f 74 0d 0a 2a 2a 20 65 6e 63 72 79 70  s not..** encryp
39870 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ted, this routin
39880 65 20 77 69 6c 6c 20 65 6e 63 72 79 70 74 20 69  e will encrypt i
39890 74 2e 20 20 49 66 20 70 4e 65 77 3d 3d 30 20 6f  t.  If pNew==0 o
398a0 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 68 65 0d 0a  r nNew==0, the..
398b0 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 64  ** database is d
398c0 65 63 72 79 70 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a  ecrypted...**..*
398d0 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d  * The code to im
398e0 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49  plement this API
398f0 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   is not availabl
39900 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20  e in the public 
39910 72 65 6c 65 61 73 65 0d 0a 2a 2a 20 6f 66 20 53  release..** of S
39920 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  QLite...*/..SQLI
39930 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
39940 65 33 5f 72 65 6b 65 79 28 0d 0a 20 20 73 71 6c  e3_rekey(..  sql
39950 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
39960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
39970 61 74 61 62 61 73 65 20 74 6f 20 62 65 20 72 65  atabase to be re
39980 6b 65 79 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73  keyed */..  cons
39990 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
399a0 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68  t nKey     /* Th
399b0 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0d 0a 29 3b  e new key */..);
399c0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 70 65 63 69  ..../*..** Speci
399d0 66 79 20 74 68 65 20 61 63 74 69 76 61 74 69 6f  fy the activatio
399e0 6e 20 6b 65 79 20 66 6f 72 20 61 20 53 45 45 20  n key for a SEE 
399f0 64 61 74 61 62 61 73 65 2e 20 20 55 6e 6c 65 73  database.  Unles
39a00 73 20 0d 0a 2a 2a 20 61 63 74 69 76 61 74 65 64  s ..** activated
39a10 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 53 45  , none of the SE
39a20 45 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20  E routines will 
39a30 77 6f 72 6b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  work...*/..SQLIT
39a40 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
39a50 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28  e3_activate_see(
39a60 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
39a70 7a 50 61 73 73 50 68 72 61 73 65 20 20 20 20 20  zPassPhrase     
39a80 20 20 20 2f 2a 20 41 63 74 69 76 61 74 69 6f 6e     /* Activation
39a90 20 70 68 72 61 73 65 20 2a 2f 0d 0a 29 3b 0d 0a   phrase */..);..
39aa0 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66  #endif....#ifdef
39ab0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
39ac0 45 52 4f 44 0d 0a 2f 2a 0d 0a 2a 2a 20 53 70 65  EROD../*..** Spe
39ad0 63 69 66 79 20 74 68 65 20 61 63 74 69 76 61 74  cify the activat
39ae0 69 6f 6e 20 6b 65 79 20 66 6f 72 20 61 20 43 45  ion key for a CE
39af0 52 4f 44 20 64 61 74 61 62 61 73 65 2e 20 20 55  ROD database.  U
39b00 6e 6c 65 73 73 20 0d 0a 2a 2a 20 61 63 74 69 76  nless ..** activ
39b10 61 74 65 64 2c 20 6e 6f 6e 65 20 6f 66 20 74 68  ated, none of th
39b20 65 20 43 45 52 4f 44 20 72 6f 75 74 69 6e 65 73  e CEROD routines
39b30 20 77 69 6c 6c 20 77 6f 72 6b 2e 0d 0a 2a 2f 0d   will work...*/.
39b40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
39b50 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74   sqlite3_activat
39b60 65 5f 63 65 72 6f 64 28 0d 0a 20 20 63 6f 6e 73  e_cerod(..  cons
39b70 74 20 63 68 61 72 20 2a 7a 50 61 73 73 50 68 72  t char *zPassPhr
39b80 61 73 65 20 20 20 20 20 20 20 20 2f 2a 20 41 63  ase        /* Ac
39b90 74 69 76 61 74 69 6f 6e 20 70 68 72 61 73 65 20  tivation phrase 
39ba0 2a 2f 0d 0a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  */..);..#endif..
39bb0 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
39bc0 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 75  F: Suspend Execu
39bd0 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74  tion For A Short
39be0 20 54 69 6d 65 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68   Time..**..** Th
39bf0 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  e sqlite3_sleep(
39c00 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  ) function cause
39c10 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  s the current th
39c20 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20  read to suspend 
39c30 65 78 65 63 75 74 69 6f 6e 0d 0a 2a 2a 20 66 6f  execution..** fo
39c40 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d  r at least a num
39c50 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ber of milliseco
39c60 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e  nds specified in
39c70 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0d   its parameter..
39c80 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6f  .**..** If the o
39c90 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
39ca0 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  does not support
39cb0 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20   sleep requests 
39cc0 77 69 74 68 0d 0a 2a 2a 20 6d 69 6c 6c 69 73 65  with..** millise
39cd0 63 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75  cond time resolu
39ce0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74  tion, then the t
39cf0 69 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e  ime will be roun
39d00 64 65 64 20 75 70 20 74 6f 0d 0a 2a 2a 20 74 68  ded up to..** th
39d10 65 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64  e nearest second
39d20 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  . The number of 
39d30 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
39d40 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0d 0a  sleep actually..
39d50 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f  ** requested fro
39d60 6d 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  m the operating 
39d70 73 79 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e  system is return
39d80 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51 4c  ed...**..** ^SQL
39d90 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ite implements t
39da0 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 79  his interface by
39db0 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 6c   calling the xSl
39dc0 65 65 70 28 29 0d 0a 2a 2a 20 6d 65 74 68 6f 64  eep()..** method
39dd0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
39de0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
39df0 6a 65 63 74 2e 20 20 49 66 20 74 68 65 20 78 53  ject.  If the xS
39e00 6c 65 65 70 28 29 20 6d 65 74 68 6f 64 0d 0a 2a  leep() method..*
39e10 2a 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74  * of the default
39e20 20 56 46 53 20 69 73 20 6e 6f 74 20 69 6d 70 6c   VFS is not impl
39e30 65 6d 65 6e 74 65 64 20 63 6f 72 72 65 63 74 6c  emented correctl
39e40 79 2c 20 6f 72 20 6e 6f 74 20 69 6d 70 6c 65 6d  y, or not implem
39e50 65 6e 74 65 64 20 61 74 0d 0a 2a 2a 20 61 6c 6c  ented at..** all
39e60 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  , then the behav
39e70 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 73  ior of sqlite3_s
39e80 6c 65 65 70 28 29 20 6d 61 79 20 64 65 76 69 61  leep() may devia
39e90 74 65 20 66 72 6f 6d 20 74 68 65 20 64 65 73 63  te from the desc
39ea0 72 69 70 74 69 6f 6e 0d 0a 2a 2a 20 69 6e 20 74  ription..** in t
39eb0 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 61  he previous para
39ec0 67 72 61 70 68 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  graphs...*/..SQL
39ed0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
39ee0 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29 3b 0d  te3_sleep(int);.
39ef0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52  .../*..** CAPI3R
39f00 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54 68 65 20  EF: Name Of The 
39f10 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e 67 20 54  Folder Holding T
39f20 65 6d 70 6f 72 61 72 79 20 46 69 6c 65 73 0d 0a  emporary Files..
39f30 2a 2a 0d 0a 2a 2a 20 5e 28 49 66 20 74 68 69 73  **..** ^(If this
39f40 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
39f50 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   is made to poin
39f60 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68  t to a string wh
39f70 69 63 68 20 69 73 0d 0a 2a 2a 20 74 68 65 20 6e  ich is..** the n
39f80 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20  ame of a folder 
39f90 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72  (a.k.a. director
39fa0 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d  y), then all tem
39fb0 70 6f 72 61 72 79 20 66 69 6c 65 73 0d 0a 2a 2a  porary files..**
39fc0 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69   created by SQLi
39fd0 74 65 20 77 68 65 6e 20 75 73 69 6e 67 20 61 20  te when using a 
39fe0 62 75 69 6c 74 2d 69 6e 20 5b 73 71 6c 69 74 65  built-in [sqlite
39ff0 33 5f 76 66 73 20 7c 20 56 46 53 5d 0d 0a 2a 2a  3_vfs | VFS]..**
3a000 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 65 64 20   will be placed 
3a010 69 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f 72  in that director
3a020 79 2e 29 5e 20 20 5e 49 66 20 74 68 69 73 20 76  y.)^  ^If this v
3a030 61 72 69 61 62 6c 65 0d 0a 2a 2a 20 69 73 20 61  ariable..** is a
3a040 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
3a050 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f  hen SQLite perfo
3a060 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72  rms a search for
3a070 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0d   an appropriate.
3a080 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
3a090 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0d 0a 2a  le directory...*
3a0a0 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  *..** It is not 
3a0b0 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20  safe to read or 
3a0c0 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69  modify this vari
3a0d0 61 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61  able in more tha
3a0e0 6e 20 6f 6e 65 0d 0a 2a 2a 20 74 68 72 65 61 64  n one..** thread
3a0f0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20   at a time.  It 
3a100 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72  is not safe to r
3a110 65 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  ead or modify th
3a120 69 73 20 76 61 72 69 61 62 6c 65 0d 0a 2a 2a 20  is variable..** 
3a130 69 66 20 61 20 5b 64 61 74 61 62 61 73 65 20 63  if a [database c
3a140 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65  onnection] is be
3a150 69 6e 67 20 75 73 65 64 20 61 74 20 74 68 65 20  ing used at the 
3a160 73 61 6d 65 20 74 69 6d 65 20 69 6e 20 61 20 73  same time in a s
3a170 65 70 61 72 61 74 65 0d 0a 2a 2a 20 74 68 72 65  eparate..** thre
3a180 61 64 2e 0d 0a 2a 2a 20 49 74 20 69 73 20 69 6e  ad...** It is in
3a190 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 69 73  tended that this
3a1a0 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 65 74   variable be set
3a1b0 20 6f 6e 63 65 0d 0a 2a 2a 20 61 73 20 70 61 72   once..** as par
3a1c0 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69  t of process ini
3a1d0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20  tialization and 
3a1e0 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74  before any SQLit
3a1f0 65 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20  e interface..** 
3a200 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65  routines have be
3a210 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68  en called and th
3a220 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  at this variable
3a230 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65   remain unchange
3a240 64 0d 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72  d..** thereafter
3a250 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b  ...**..** ^The [
3a260 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
3a270 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d 61 79  tory pragma] may
3a280 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72   modify this var
3a290 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73 65 0d  iable and cause.
3a2a0 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74 20  .** it to point 
3a2b0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
3a2c0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
3a2d0 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 5e 46 75 72 74  _malloc].  ^Furt
3a2e0 68 65 72 6d 6f 72 65 2c 0d 0a 2a 2a 20 74 68 65  hermore,..** the
3a2f0 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   [temp_store_dir
3a300 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61  ectory pragma] a
3a310 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68  lways assumes th
3a320 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0d 0a 2a  at any string..*
3a330 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69  * that this vari
3a340 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  able points to i
3a350 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
3a360 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0d   obtained from .
3a370 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  .** [sqlite3_mal
3a380 6c 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61  loc] and the pra
3a390 67 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20  gma may attempt 
3a3a0 74 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d  to free that mem
3a3b0 6f 72 79 0d 0a 2a 2a 20 75 73 69 6e 67 20 5b 73  ory..** using [s
3a3c0 71 6c 69 74 65 33 5f 66 72 65 65 5d 2e 0d 0a 2a  qlite3_free]...*
3a3d0 2a 20 48 65 6e 63 65 2c 20 69 66 20 74 68 69 73  * Hence, if this
3a3e0 20 76 61 72 69 61 62 6c 65 20 69 73 20 6d 6f 64   variable is mod
3a3f0 69 66 69 65 64 20 64 69 72 65 63 74 6c 79 2c 20  ified directly, 
3a400 65 69 74 68 65 72 20 69 74 20 73 68 6f 75 6c 64  either it should
3a410 20 62 65 0d 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c   be..** made NUL
3a420 4c 20 6f 72 20 6d 61 64 65 20 74 6f 20 70 6f 69  L or made to poi
3a430 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74  nt to memory obt
3a440 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
3a450 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0d 0a 2a 2a 20  te3_malloc]..** 
3a460 6f 72 20 65 6c 73 65 20 74 68 65 20 75 73 65 20  or else the use 
3a470 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f 73 74 6f  of the [temp_sto
3a480 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61  re_directory pra
3a490 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 65 20 61  gma] should be a
3a4a0 76 6f 69 64 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  voided...*/..SQL
3a4b0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
3a4c0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
3a4d0 74 6f 72 79 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  tory;..../*..** 
3a4e0 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46  CAPI3REF: Test F
3a4f0 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d  or Auto-Commit M
3a500 6f 64 65 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  ode..** KEYWORDS
3a510 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  : {autocommit mo
3a520 64 65 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  de}..**..** ^The
3a530 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74   sqlite3_get_aut
3a540 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 65 72 66  ocommit() interf
3a550 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ace returns non-
3a560 7a 65 72 6f 20 6f 72 0d 0a 2a 2a 20 7a 65 72 6f  zero or..** zero
3a570 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 64 61   if the given da
3a580 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3a590 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 20 69  n is or is not i
3a5a0 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64  n autocommit mod
3a5b0 65 2c 0d 0a 2a 2a 20 72 65 73 70 65 63 74 69 76  e,..** respectiv
3a5c0 65 6c 79 2e 20 20 5e 41 75 74 6f 63 6f 6d 6d 69  ely.  ^Autocommi
3a5d0 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20  t mode is on by 
3a5e0 64 65 66 61 75 6c 74 2e 0d 0a 2a 2a 20 5e 41 75  default...** ^Au
3a5f0 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
3a600 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b   disabled by a [
3a610 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74  BEGIN] statement
3a620 2e 0d 0a 2a 2a 20 5e 41 75 74 6f 63 6f 6d 6d 69  ...** ^Autocommi
3a630 74 20 6d 6f 64 65 20 69 73 20 72 65 2d 65 6e 61  t mode is re-ena
3a640 62 6c 65 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49  bled by a [COMMI
3a650 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d  T] or [ROLLBACK]
3a660 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 63 65 72  ...**..** If cer
3a670 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 65 72  tain kinds of er
3a680 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 61 20  rors occur on a 
3a690 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e  statement within
3a6a0 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 6d 65   a multi-stateme
3a6b0 6e 74 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  nt..** transacti
3a6c0 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c 75  on (errors inclu
3a6d0 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 4c  ding [SQLITE_FUL
3a6e0 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  L], [SQLITE_IOER
3a6f0 52 5d 2c 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  R],..** [SQLITE_
3a700 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f  NOMEM], [SQLITE_
3a710 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49  BUSY], and [SQLI
3a720 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74  TE_INTERRUPT]) t
3a730 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 74 72 61 6e  hen the..** tran
3a740 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65  saction might be
3a750 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74   rolled back aut
3a760 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65  omatically.  The
3a770 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0d 0a 2a 2a   only way to..**
3a780 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65   find out whethe
3a790 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74  r SQLite automat
3a7a0 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61  ically rolled ba
3a7b0 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
3a7c0 6f 6e 20 61 66 74 65 72 0d 0a 2a 2a 20 61 6e 20  on after..** an 
3a7d0 65 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20  error is to use 
3a7e0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  this function...
3a7f0 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65  **..** If anothe
3a800 72 20 74 68 72 65 61 64 20 63 68 61 6e 67 65 73  r thread changes
3a810 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69 74 20   the autocommit 
3a820 73 74 61 74 75 73 20 6f 66 20 74 68 65 20 64 61  status of the da
3a830 74 61 62 61 73 65 0d 0a 2a 2a 20 63 6f 6e 6e 65  tabase..** conne
3a840 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 73  ction while this
3a850 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 6e   routine is runn
3a860 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ing, then the re
3a870 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20 69  turn value..** i
3a880 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f  s undefined...*/
3a890 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
3a8a0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74   sqlite3_get_aut
3a8b0 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a  ocommit(sqlite3*
3a8c0 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  );..../*..** CAP
3a8d0 49 33 52 45 46 3a 20 46 69 6e 64 20 54 68 65 20  I3REF: Find The 
3a8e0 44 61 74 61 62 61 73 65 20 48 61 6e 64 6c 65 20  Database Handle 
3a8f0 4f 66 20 41 20 50 72 65 70 61 72 65 64 20 53 74  Of A Prepared St
3a900 61 74 65 6d 65 6e 74 0d 0a 2a 2a 0d 0a 2a 2a 20  atement..**..** 
3a910 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f  ^The sqlite3_db_
3a920 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61 63 65  handle interface
3a930 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b 64 61   returns the [da
3a940 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3a950 6e 5d 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 74 6f  n] handle..** to
3a960 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61 72   which a [prepar
3a970 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62 65  ed statement] be
3a980 6c 6f 6e 67 73 2e 20 20 5e 54 68 65 20 5b 64 61  longs.  ^The [da
3a990 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3a9a0 6e 5d 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  n]..** returned 
3a9b0 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  by sqlite3_db_ha
3a9c0 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65  ndle is the same
3a9d0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3a9e0 63 74 69 6f 6e 5d 0d 0a 2a 2a 20 74 68 61 74 20  ction]..** that 
3a9f0 77 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72  was the first ar
3aa00 67 75 6d 65 6e 74 0d 0a 2a 2a 20 74 6f 20 74 68  gument..** to th
3aa10 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
3aa20 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f  re_v2()] call (o
3aa30 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20  r its variants) 
3aa40 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f  that was used to
3aa50 0d 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20  ..** create the 
3aa60 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
3aa70 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0d 0a 2a   first place...*
3aa80 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  /..SQLITE_API sq
3aa90 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64  lite3 *sqlite3_d
3aaa0 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33  b_handle(sqlite3
3aab0 5f 73 74 6d 74 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  _stmt*);..../*..
3aac0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74  ** CAPI3REF: Ret
3aad0 75 72 6e 20 54 68 65 20 46 69 6c 65 6e 61 6d 65  urn The Filename
3aae0 20 46 6f 72 20 41 20 44 61 74 61 62 61 73 65 20   For A Database 
3aaf0 43 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 0d 0a  Connection..**..
3ab00 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
3ab10 64 62 5f 66 69 6c 65 6e 61 6d 65 28 44 2c 4e 29  db_filename(D,N)
3ab20 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3ab30 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
3ab40 61 20 66 69 6c 65 6e 61 6d 65 0d 0a 2a 2a 20 61  a filename..** a
3ab50 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 64  ssociated with d
3ab60 61 74 61 62 61 73 65 20 4e 20 6f 66 20 63 6f 6e  atabase N of con
3ab70 6e 65 63 74 69 6f 6e 20 44 2e 20 20 5e 54 68 65  nection D.  ^The
3ab80 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
3ab90 69 6c 65 0d 0a 2a 2a 20 68 61 73 20 74 68 65 20  ile..** has the 
3aba0 6e 61 6d 65 20 22 6d 61 69 6e 22 2e 20 20 49 66  name "main".  If
3abb0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 74 74   there is no att
3abc0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 4e  ached database N
3abd0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
3abe0 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ..** connection 
3abf0 44 2c 20 6f 72 20 69 66 20 64 61 74 61 62 61 73  D, or if databas
3ac00 65 20 4e 20 69 73 20 61 20 74 65 6d 70 6f 72 61  e N is a tempora
3ac10 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  ry or in-memory 
3ac20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0d 0a  database, then..
3ac30 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
3ac40 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  r is returned...
3ac50 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 66 69 6c 65  **..** ^The file
3ac60 6e 61 6d 65 20 72 65 74 75 72 6e 65 64 20 62 79  name returned by
3ac70 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
3ac80 73 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20  s the output of 
3ac90 74 68 65 0d 0a 2a 2a 20 78 46 75 6c 6c 50 61 74  the..** xFullPat
3aca0 68 6e 61 6d 65 20 6d 65 74 68 6f 64 20 6f 66 20  hname method of 
3acb0 74 68 65 20 5b 56 46 53 5d 2e 20 20 5e 49 6e 20  the [VFS].  ^In 
3acc0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
3acd0 20 66 69 6c 65 6e 61 6d 65 0d 0a 2a 2a 20 77 69   filename..** wi
3ace0 6c 6c 20 62 65 20 61 6e 20 61 62 73 6f 6c 75 74  ll be an absolut
3acf0 65 20 70 61 74 68 6e 61 6d 65 2c 20 65 76 65 6e  e pathname, even
3ad00 20 69 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   if the filename
3ad10 20 75 73 65 64 0d 0a 2a 2a 20 74 6f 20 6f 70 65   used..** to ope
3ad20 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  n the database o
3ad30 72 69 67 69 6e 61 6c 6c 79 20 77 61 73 20 61 20  riginally was a 
3ad40 55 52 49 20 6f 72 20 72 65 6c 61 74 69 76 65 20  URI or relative 
3ad50 70 61 74 68 6e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 53  pathname...*/..S
3ad60 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
3ad70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 64 62  char *sqlite3_db
3ad80 5f 66 69 6c 65 6e 61 6d 65 28 73 71 6c 69 74 65  _filename(sqlite
3ad90 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
3ada0 72 20 2a 7a 44 62 4e 61 6d 65 29 3b 0d 0a 0d 0a  r *zDbName);....
3adb0 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
3adc0 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70   Find the next p
3add0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3ade0 74 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73 20  t..**..** ^This 
3adf0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
3ae00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
3ae10 68 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65  he next [prepare
3ae20 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74  d statement] aft
3ae30 65 72 0d 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73  er..** pStmt ass
3ae40 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3ae50 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3ae60 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20 5e 49 66  ction] pDb.  ^If
3ae70 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 0d 0a   pStmt is NULL..
3ae80 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 6e 74  ** then this int
3ae90 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
3aea0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3aeb0 66 69 72 73 74 20 70 72 65 70 61 72 65 64 20 73  first prepared s
3aec0 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 61 73 73  tatement..** ass
3aed0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3aee0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3aef0 74 69 6f 6e 20 70 44 62 2e 20 20 5e 49 66 20 6e  tion pDb.  ^If n
3af00 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  o prepared state
3af10 6d 65 6e 74 0d 0a 2a 2a 20 73 61 74 69 73 66 69  ment..** satisfi
3af20 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  es the condition
3af30 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  s of this routin
3af40 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  e, it returns NU
3af50 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  LL...**..** The 
3af60 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3af70 74 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20  tion] pointer D 
3af80 69 6e 20 61 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a  in a call to..**
3af90 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73   [sqlite3_next_s
3afa0 74 6d 74 28 44 2c 53 29 5d 20 6d 75 73 74 20 72  tmt(D,S)] must r
3afb0 65 66 65 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20  efer to an open 
3afc0 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63 6f 6e  database..** con
3afd0 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70  nection and in p
3afe0 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e  articular must n
3aff0 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69  ot be a NULL poi
3b000 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  nter...*/..SQLIT
3b010 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74  E_API sqlite3_st
3b020 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74  mt *sqlite3_next
3b030 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70  _stmt(sqlite3 *p
3b040 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  Db, sqlite3_stmt
3b050 20 2a 70 53 74 6d 74 29 3b 0d 0a 0d 0a 2f 2a 0d   *pStmt);..../*.
3b060 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
3b070 6d 6d 69 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63  mmit And Rollbac
3b080 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43  k Notification C
3b090 61 6c 6c 62 61 63 6b 73 0d 0a 2a 2a 0d 0a 2a 2a  allbacks..**..**
3b0a0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f   ^The sqlite3_co
3b0b0 6d 6d 69 74 5f 68 6f 6f 6b 28 29 20 69 6e 74 65  mmit_hook() inte
3b0c0 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20  rface registers 
3b0d0 61 20 63 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 66  a callback..** f
3b0e0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  unction to be in
3b0f0 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61  voked whenever a
3b100 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
3b110 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f 6d 6d 69 74  [COMMIT | commit
3b120 74 65 64 5d 2e 0d 0a 2a 2a 20 5e 41 6e 79 20 63  ted]...** ^Any c
3b130 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61  allback set by a
3b140 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
3b150 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74  o sqlite3_commit
3b160 5f 68 6f 6f 6b 28 29 0d 0a 2a 2a 20 66 6f 72 20  _hook()..** for 
3b170 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
3b180 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
3b190 6f 76 65 72 72 69 64 64 65 6e 2e 0d 0a 2a 2a 20  overridden...** 
3b1a0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c  ^The sqlite3_rol
3b1b0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74  lback_hook() int
3b1c0 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73  erface registers
3b1d0 20 61 20 63 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20   a callback..** 
3b1e0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  function to be i
3b1f0 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20  nvoked whenever 
3b200 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
3b210 20 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c   [ROLLBACK | rol
3b220 6c 65 64 20 62 61 63 6b 5d 2e 0d 0a 2a 2a 20 5e  led back]...** ^
3b230 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74  Any callback set
3b240 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63   by a previous c
3b250 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  all to sqlite3_r
3b260 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 0d 0a  ollback_hook()..
3b270 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  ** for the same 
3b280 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3b290 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65  ion is overridde
3b2a0 6e 2e 0d 0a 2a 2a 20 5e 54 68 65 20 70 41 72 67  n...** ^The pArg
3b2b0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73   argument is pas
3b2c0 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74  sed through to t
3b2d0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0d 0a 2a 2a  he callback...**
3b2e0 20 5e 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63   ^If the callbac
3b2f0 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f  k on a commit ho
3b300 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  ok function retu
3b310 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0d 0a 2a  rns non-zero,..*
3b320 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69  * then the commi
3b330 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69  t is converted i
3b340 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0d  nto a rollback..
3b350 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c  .**..** ^The sql
3b360 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3b370 28 44 2c 43 2c 50 29 20 61 6e 64 20 73 71 6c 69  (D,C,P) and sqli
3b380 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
3b390 6b 28 44 2c 43 2c 50 29 20 66 75 6e 63 74 69 6f  k(D,C,P) functio
3b3a0 6e 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68  ns..** return th
3b3b0 65 20 50 20 61 72 67 75 6d 65 6e 74 20 66 72 6f  e P argument fro
3b3c0 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  m the previous c
3b3d0 61 6c 6c 20 6f 66 20 74 68 65 20 73 61 6d 65 20  all of the same 
3b3e0 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6f 6e 20  function..** on 
3b3f0 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61  the same [databa
3b400 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
3b410 2c 20 6f 72 20 4e 55 4c 4c 20 66 6f 72 0d 0a 2a  , or NULL for..*
3b420 2a 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  * the first call
3b430 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 69   for each functi
3b440 6f 6e 20 6f 6e 20 44 2e 0d 0a 2a 2a 0d 0a 2a 2a  on on D...**..**
3b450 20 54 68 65 20 63 6f 6d 6d 69 74 20 61 6e 64 20   The commit and 
3b460 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 63 61  rollback hook ca
3b470 6c 6c 62 61 63 6b 73 20 61 72 65 20 6e 6f 74 20  llbacks are not 
3b480 72 65 65 6e 74 72 61 6e 74 2e 0d 0a 2a 2a 20 54  reentrant...** T
3b490 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c  he callback impl
3b4a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20  ementation must 
3b4b0 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20  not do anything 
3b4c0 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79  that will modify
3b4d0 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  ..** the databas
3b4e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
3b4f0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 63 61  t invoked the ca
3b500 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61 63 74  llback.  Any act
3b510 69 6f 6e 73 0d 0a 2a 2a 20 74 6f 20 6d 6f 64 69  ions..** to modi
3b520 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  fy the database 
3b530 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20  connection must 
3b540 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  be deferred unti
3b550 6c 20 61 66 74 65 72 20 74 68 65 0d 0a 2a 2a 20  l after the..** 
3b560 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68  completion of th
3b570 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
3b580 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69  )] call that tri
3b590 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69  ggered the commi
3b5a0 74 0d 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63  t..** or rollbac
3b5b0 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69  k hook in the fi
3b5c0 72 73 74 20 70 6c 61 63 65 2e 0d 0a 2a 2a 20 4e  rst place...** N
3b5d0 6f 74 65 20 74 68 61 74 20 72 75 6e 6e 69 6e 67  ote that running
3b5e0 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 73   any other SQL s
3b5f0 74 61 74 65 6d 65 6e 74 73 2c 20 69 6e 63 6c 75  tatements, inclu
3b600 64 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  ding SELECT stat
3b610 65 6d 65 6e 74 73 2c 0d 0a 2a 2a 20 6f 72 20 6d  ements,..** or m
3b620 65 72 65 6c 79 20 63 61 6c 6c 69 6e 67 20 5b 73  erely calling [s
3b630 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
3b640 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  2()] and [sqlite
3b650 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 6d  3_step()] will m
3b660 6f 64 69 66 79 0d 0a 2a 2a 20 74 68 65 20 64 61  odify..** the da
3b670 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3b680 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
3b690 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
3b6a0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
3b6b0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 52 65 67 69 73  ...**..** ^Regis
3b6c0 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75  tering a NULL fu
3b6d0 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
3b6e0 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0d 0a 2a  the callback...*
3b6f0 2a 0d 0a 2a 2a 20 5e 57 68 65 6e 20 74 68 65 20  *..** ^When the 
3b700 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c  commit hook call
3b710 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74  back routine ret
3b720 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b  urns zero, the [
3b730 43 4f 4d 4d 49 54 5d 0d 0a 2a 2a 20 6f 70 65 72  COMMIT]..** oper
3b740 61 74 69 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64  ation is allowed
3b750 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72   to continue nor
3b760 6d 61 6c 6c 79 2e 20 20 5e 49 66 20 74 68 65 20  mally.  ^If the 
3b770 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0d 0a 2a 2a 20  commit hook..** 
3b780 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
3b790 2c 20 74 68 65 6e 20 74 68 65 20 5b 43 4f 4d 4d  , then the [COMM
3b7a0 49 54 5d 20 69 73 20 63 6f 6e 76 65 72 74 65 64  IT] is converted
3b7b0 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c 42 41 43   into a [ROLLBAC
3b7c0 4b 5d 2e 0d 0a 2a 2a 20 5e 54 68 65 20 72 6f 6c  K]...** ^The rol
3b7d0 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69 6e  lback hook is in
3b7e0 76 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c 62  voked on a rollb
3b7f0 61 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74 73  ack that results
3b800 20 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0d 0a   from a commit..
3b810 2a 2a 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e  ** hook returnin
3b820 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74  g non-zero, just
3b830 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
3b840 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72  with any other r
3b850 6f 6c 6c 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  ollback...**..**
3b860 20 5e 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73   ^For the purpos
3b870 65 73 20 6f 66 20 74 68 69 73 20 41 50 49 2c 20  es of this API, 
3b880 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
3b890 20 73 61 69 64 20 74 6f 20 68 61 76 65 20 62 65   said to have be
3b8a0 65 6e 0d 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61  en..** rolled ba
3b8b0 63 6b 20 69 66 20 61 6e 20 65 78 70 6c 69 63 69  ck if an explici
3b8c0 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73 74 61  t "ROLLBACK" sta
3b8d0 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74  tement is execut
3b8e0 65 64 2c 20 6f 72 0d 0a 2a 2a 20 61 6e 20 65 72  ed, or..** an er
3b8f0 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 69 6e  ror or constrain
3b900 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d 70 6c  t causes an impl
3b910 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74 6f  icit rollback to
3b920 20 6f 63 63 75 72 2e 0d 0a 2a 2a 20 5e 54 68 65   occur...** ^The
3b930 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61   rollback callba
3b940 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65  ck is not invoke
3b950 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69  d if a transacti
3b960 6f 6e 20 69 73 0d 0a 2a 2a 20 61 75 74 6f 6d 61  on is..** automa
3b970 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62  tically rolled b
3b980 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20  ack because the 
3b990 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3b9a0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0d 0a  ion is closed...
3b9b0 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  **..** See also 
3b9c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64  the [sqlite3_upd
3b9d0 61 74 65 5f 68 6f 6f 6b 28 29 5d 20 69 6e 74 65  ate_hook()] inte
3b9e0 72 66 61 63 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  rface...*/..SQLI
3b9f0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
3ba00 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3ba10 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a  (sqlite3*, int(*
3ba20 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29  )(void*), void*)
3ba30 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  ;..SQLITE_API vo
3ba40 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  id *sqlite3_roll
3ba50 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65  back_hook(sqlite
3ba60 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  3*, void(*)(void
3ba70 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0d 0a 0d 0a   *), void*);....
3ba80 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
3ba90 20 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74   Data Change Not
3baa0 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61  ification Callba
3bab0 63 6b 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  cks..**..** ^The
3bac0 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f   sqlite3_update_
3bad0 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65  hook() interface
3bae0 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
3baf0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0d 0a  lback function..
3bb00 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61 74  ** with the [dat
3bb10 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3bb20 5d 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  ] identified by 
3bb30 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
3bb40 6e 74 0d 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76  nt..** to be inv
3bb50 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20  oked whenever a 
3bb60 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20  row is updated, 
3bb70 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65  inserted or dele
3bb80 74 65 64 2e 0d 0a 2a 2a 20 5e 41 6e 79 20 63 61  ted...** ^Any ca
3bb90 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61 20  llback set by a 
3bba0 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f  previous call to
3bbb0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0d 0a   this function..
3bbc0 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  ** for the same 
3bbd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3bbe0 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65  ion is overridde
3bbf0 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  n...**..** ^The 
3bc00 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
3bc10 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3bc20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  the function to 
3bc30 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0d 0a 2a  invoke when a..*
3bc40 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  * row is updated
3bc50 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  , inserted or de
3bc60 6c 65 74 65 64 2e 0d 0a 2a 2a 20 5e 54 68 65 20  leted...** ^The 
3bc70 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
3bc80 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
3bc90 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
3bca0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0d 0a  third argument..
3bcb0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70  ** to sqlite3_up
3bcc0 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0d 0a 2a 2a  date_hook()...**
3bcd0 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c   ^The second cal
3bce0 6c 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69  lback argument i
3bcf0 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45  s one of [SQLITE
3bd00 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54  _INSERT], [SQLIT
3bd10 45 5f 44 45 4c 45 54 45 5d 2c 0d 0a 2a 2a 20 6f  E_DELETE],..** o
3bd20 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45  r [SQLITE_UPDATE
3bd30 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ], depending on 
3bd40 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68  the operation th
3bd50 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61  at caused the ca
3bd60 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 74 6f 20 62 65  llback..** to be
3bd70 20 69 6e 76 6f 6b 65 64 2e 0d 0a 2a 2a 20 5e 54   invoked...** ^T
3bd80 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  he third and fou
3bd90 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  rth arguments to
3bda0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f   the callback co
3bdb0 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74  ntain pointers t
3bdc0 6f 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61  o the..** databa
3bdd0 73 65 20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d  se and table nam
3bde0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
3bdf0 20 61 66 66 65 63 74 65 64 20 72 6f 77 2e 0d 0a   affected row...
3be00 2a 2a 20 5e 54 68 65 20 66 69 6e 61 6c 20 63 61  ** ^The final ca
3be10 6c 6c 62 61 63 6b 20 70 61 72 61 6d 65 74 65 72  llback parameter
3be20 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20   is the [rowid] 
3be30 6f 66 20 74 68 65 20 72 6f 77 2e 0d 0a 2a 2a 20  of the row...** 
3be40 5e 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20  ^In the case of 
3be50 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73 20  an update, this 
3be60 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 61  is the [rowid] a
3be70 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 20  fter the update 
3be80 74 61 6b 65 73 20 70 6c 61 63 65 2e 0d 0a 2a 2a  takes place...**
3be90 0d 0a 2a 2a 20 5e 28 54 68 65 20 75 70 64 61 74  ..** ^(The updat
3bea0 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e  e hook is not in
3beb0 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72  voked when inter
3bec0 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65  nal system table
3bed0 73 20 61 72 65 0d 0a 2a 2a 20 6d 6f 64 69 66 69  s are..** modifi
3bee0 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f  ed (i.e. sqlite_
3bef0 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74  master and sqlit
3bf00 65 5f 73 65 71 75 65 6e 63 65 29 2e 29 5e 0d 0a  e_sequence).)^..
3bf10 2a 2a 0d 0a 2a 2a 20 5e 49 6e 20 74 68 65 20 63  **..** ^In the c
3bf20 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74  urrent implement
3bf30 61 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 74  ation, the updat
3bf40 65 20 68 6f 6f 6b 0d 0a 2a 2a 20 69 73 20 6e 6f  e hook..** is no
3bf50 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64  t invoked when d
3bf60 75 70 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20  uplication rows 
3bf70 61 72 65 20 64 65 6c 65 74 65 64 20 62 65 63 61  are deleted beca
3bf80 75 73 65 20 6f 66 20 61 6e 0d 0a 2a 2a 20 5b 4f  use of an..** [O
3bf90 4e 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20  N CONFLICT | ON 
3bfa0 43 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45  CONFLICT REPLACE
3bfb0 5d 20 63 6c 61 75 73 65 2e 20 20 5e 4e 6f 72 20  ] clause.  ^Nor 
3bfc0 69 73 20 74 68 65 20 75 70 64 61 74 65 20 68 6f  is the update ho
3bfd0 6f 6b 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 77  ok..** invoked w
3bfe0 68 65 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c  hen rows are del
3bff0 65 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b  eted using the [
3c000 74 72 75 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a  truncate optimiz
3c010 61 74 69 6f 6e 5d 2e 0d 0a 2a 2a 20 54 68 65 20  ation]...** The 
3c020 65 78 63 65 70 74 69 6f 6e 73 20 64 65 66 69 6e  exceptions defin
3c030 65 64 20 69 6e 20 74 68 69 73 20 70 61 72 61 67  ed in this parag
3c040 72 61 70 68 20 6d 69 67 68 74 20 63 68 61 6e 67  raph might chang
3c050 65 20 69 6e 20 61 20 66 75 74 75 72 65 0d 0a 2a  e in a future..*
3c060 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c  * release of SQL
3c070 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ite...**..** The
3c080 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70   update hook imp
3c090 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74  lementation must
3c0a0 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67   not do anything
3c0b0 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66   that will modif
3c0c0 79 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  y..** the databa
3c0d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
3c0e0 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75  at invoked the u
3c0f0 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79  pdate hook.  Any
3c100 20 61 63 74 69 6f 6e 73 0d 0a 2a 2a 20 74 6f 20   actions..** to 
3c110 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
3c120 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
3c130 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20  ust be deferred 
3c140 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0d  until after the.
3c150 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f  .** completion o
3c160 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  f the [sqlite3_s
3c170 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74  tep()] call that
3c180 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 75   triggered the u
3c190 70 64 61 74 65 20 68 6f 6f 6b 2e 0d 0a 2a 2a 20  pdate hook...** 
3c1a0 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
3c1b0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
3c1c0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
3c1d0 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
3c1e0 79 20 74 68 65 69 72 0d 0a 2a 2a 20 64 61 74 61  y their..** data
3c1f0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
3c200 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67   for the meaning
3c210 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20   of "modify" in 
3c220 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0d  this paragraph..
3c230 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c  .**..** ^The sql
3c240 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
3c250 28 44 2c 43 2c 50 29 20 66 75 6e 63 74 69 6f 6e  (D,C,P) function
3c260 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65  ..** returns the
3c270 20 50 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d   P argument from
3c280 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 61   the previous ca
3c290 6c 6c 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61  ll..** on the sa
3c2a0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3c2b0 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 6f 72 20 4e  nection] D, or N
3c2c0 55 4c 4c 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20  ULL for..** the 
3c2d0 66 69 72 73 74 20 63 61 6c 6c 20 6f 6e 20 44 2e  first call on D.
3c2e0 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
3c2f0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  o the [sqlite3_c
3c300 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 61 6e  ommit_hook()] an
3c310 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  d [sqlite3_rollb
3c320 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0d 0a 2a 2a 20  ack_hook()]..** 
3c330 69 6e 74 65 72 66 61 63 65 73 2e 0d 0a 2a 2f 0d  interfaces...*/.
3c340 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3c350 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65   *sqlite3_update
3c360 5f 68 6f 6f 6b 28 0d 0a 20 20 73 71 6c 69 74 65  _hook(..  sqlite
3c370 33 2a 2c 20 0d 0a 20 20 76 6f 69 64 28 2a 29 28  3*, ..  void(*)(
3c380 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68 61 72  void *,int ,char
3c390 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f   const *,char co
3c3a0 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  nst *,sqlite3_in
3c3b0 74 36 34 29 2c 0d 0a 20 20 76 6f 69 64 2a 0d 0a  t64),..  void*..
3c3c0 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  );..../*..** CAP
3c3d0 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72  I3REF: Enable Or
3c3e0 20 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20   Disable Shared 
3c3f0 50 61 67 65 72 20 43 61 63 68 65 0d 0a 2a 2a 20  Pager Cache..** 
3c400 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65  KEYWORDS: {share
3c410 64 20 63 61 63 68 65 7d 0d 0a 2a 2a 0d 0a 2a 2a  d cache}..**..**
3c420 20 5e 28 54 68 69 73 20 72 6f 75 74 69 6e 65 20   ^(This routine 
3c430 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61 62  enables or disab
3c440 6c 65 73 20 74 68 65 20 73 68 61 72 69 6e 67 20  les the sharing 
3c450 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
3c460 63 61 63 68 65 0d 0a 2a 2a 20 61 6e 64 20 73 63  cache..** and sc
3c470 68 65 6d 61 20 64 61 74 61 20 73 74 72 75 63 74  hema data struct
3c480 75 72 65 73 20 62 65 74 77 65 65 6e 20 5b 64 61  ures between [da
3c490 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3c4a0 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d  n | connections]
3c4b0 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d 65  ..** to the same
3c4c0 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 72 69   database. Shari
3c4d0 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 69 66  ng is enabled if
3c4e0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
3c4f0 20 74 72 75 65 0d 0a 2a 2a 20 61 6e 64 20 64 69   true..** and di
3c500 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72  sabled if the ar
3c510 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e  gument is false.
3c520 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 43 61 63 68  )^..**..** ^Cach
3c530 65 20 73 68 61 72 69 6e 67 20 69 73 20 65 6e 61  e sharing is ena
3c540 62 6c 65 64 20 61 6e 64 20 64 69 73 61 62 6c 65  bled and disable
3c550 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20  d for an entire 
3c560 70 72 6f 63 65 73 73 2e 0d 0a 2a 2a 20 54 68 69  process...** Thi
3c570 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 61 73  s is a change as
3c580 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73 69   of SQLite versi
3c590 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72 69  on 3.5.0. In pri
3c5a0 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  or versions of S
3c5b0 51 4c 69 74 65 2c 0d 0a 2a 2a 20 73 68 61 72 69  QLite,..** shari
3c5c0 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  ng was enabled o
3c5d0 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 65  r disabled for e
3c5e0 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 72  ach thread separ
3c5f0 61 74 65 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ately...**..** ^
3c600 28 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69  (The cache shari
3c610 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74  ng mode set by t
3c620 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66  his interface ef
3c630 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71  fects all subseq
3c640 75 65 6e 74 0d 0a 2a 2a 20 63 61 6c 6c 73 20 74  uent..** calls t
3c650 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  o [sqlite3_open(
3c660 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
3c670 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71  n_v2()], and [sq
3c680 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e  lite3_open16()].
3c690 0d 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61  ..** Existing da
3c6a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3c6b0 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20  ns continue use 
3c6c0 74 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65  the sharing mode
3c6d0 0d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 69 6e  ..** that was in
3c6e0 20 65 66 66 65 63 74 20 61 74 20 74 68 65 20 74   effect at the t
3c6f0 69 6d 65 20 74 68 65 79 20 77 65 72 65 20 6f 70  ime they were op
3c700 65 6e 65 64 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  ened.)^..**..** 
3c710 5e 28 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  ^(This routine r
3c720 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f  eturns [SQLITE_O
3c730 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 63  K] if shared cac
3c740 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  he was enabled o
3c750 72 20 64 69 73 61 62 6c 65 64 0d 0a 2a 2a 20 73  r disabled..** s
3c760 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e  uccessfully.  An
3c770 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
3c780 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77   returned otherw
3c790 69 73 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ise.)^..**..** ^
3c7a0 53 68 61 72 65 64 20 63 61 63 68 65 20 69 73 20  Shared cache is 
3c7b0 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61  disabled by defa
3c7c0 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20 6d 69  ult. But this mi
3c7d0 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0d 0a 2a  ght change in..*
3c7e0 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  * future release
3c7f0 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 70  s of SQLite.  Ap
3c800 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
3c810 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72 65  care about share
3c820 64 0d 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74  d..** cache sett
3c830 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69  ing should set i
3c840 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0d 0a 2a  t explicitly...*
3c850 2a 0d 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20  *..** See Also: 
3c860 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d   [SQLite Shared-
3c870 43 61 63 68 65 20 4d 6f 64 65 5d 0d 0a 2a 2f 0d  Cache Mode]..*/.
3c880 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3c890 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
3c8a0 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29  hared_cache(int)
3c8b0 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
3c8c0 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 54 6f  3REF: Attempt To
3c8d0 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d 6f 72   Free Heap Memor
3c8e0 79 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73  y..**..** ^The s
3c8f0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
3c900 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63  emory() interfac
3c910 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72  e attempts to fr
3c920 65 65 20 4e 20 62 79 74 65 73 0d 0a 2a 2a 20 6f  ee N bytes..** o
3c930 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79  f heap memory by
3c940 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f   deallocating no
3c950 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f  n-essential memo
3c960 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0d 0a  ry allocations..
3c970 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64  ** held by the d
3c980 61 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e  atabase library.
3c990 20 20 20 4d 65 6d 6f 72 79 20 75 73 65 64 20 74     Memory used t
3c9a0 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65  o cache database
3c9b0 0d 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20 69 6d  ..** pages to im
3c9c0 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61 6e 63  prove performanc
3c9d0 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c 65 20  e is an example 
3c9e0 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  of non-essential
3c9f0 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 20 5e 73 71   memory...** ^sq
3ca00 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
3ca10 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 74  mory() returns t
3ca20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
3ca30 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65  es actually free
3ca40 64 2c 0d 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67  d,..** which mig
3ca50 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65  ht be more or le
3ca60 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75  ss than the amou
3ca70 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0d 0a 2a  nt requested...*
3ca80 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72  * ^The sqlite3_r
3ca90 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
3caa0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
3cab0 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 7a 65 72  op returning zer
3cac0 6f 0d 0a 2a 2a 20 69 66 20 53 51 4c 69 74 65 20  o..** if SQLite 
3cad0 69 73 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 64 20  is not compiled 
3cae0 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 45 4e 41  with [SQLITE_ENA
3caf0 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
3cb00 45 4d 45 4e 54 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  EMENT]...**..** 
3cb10 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
3cb20 65 33 5f 64 62 5f 72 65 6c 65 61 73 65 5f 6d 65  e3_db_release_me
3cb30 6d 6f 72 79 28 29 5d 0d 0a 2a 2f 0d 0a 53 51 4c  mory()]..*/..SQL
3cb40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3cb50 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
3cb60 72 79 28 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  ry(int);..../*..
3cb70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 72 65  ** CAPI3REF: Fre
3cb80 65 20 4d 65 6d 6f 72 79 20 55 73 65 64 20 42 79  e Memory Used By
3cb90 20 41 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e   A Database Conn
3cba0 65 63 74 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ection..**..** ^
3cbb0 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 72  The sqlite3_db_r
3cbc0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 44 29  elease_memory(D)
3cbd0 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
3cbe0 70 74 73 20 74 6f 20 66 72 65 65 20 61 73 20 6d  pts to free as m
3cbf0 75 63 68 20 68 65 61 70 0d 0a 2a 2a 20 6d 65 6d  uch heap..** mem
3cc00 6f 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65 20  ory as possible 
3cc10 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 63 6f  from database co
3cc20 6e 6e 65 63 74 69 6f 6e 20 44 2e 20 55 6e 6c 69  nnection D. Unli
3cc30 6b 65 20 74 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69  ke the..** [sqli
3cc40 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
3cc50 72 79 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2c  ry()] interface,
3cc60 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
3cc70 69 73 20 65 66 66 65 63 74 20 65 76 65 6e 0d 0a  is effect even..
3cc80 2a 2a 20 77 68 65 6e 20 74 68 65 6e 20 5b 53 51  ** when then [SQ
3cc90 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
3cca0 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d 20 63  RY_MANAGEMENT] c
3ccb0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
3ccc0 6f 6e 20 69 73 0d 0a 2a 2a 20 6f 6d 69 74 74 65  on is..** omitte
3ccd0 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61  d...**..** See a
3cce0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 72 65  lso: [sqlite3_re
3ccf0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0d  lease_memory()].
3cd00 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3cd10 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 72  int sqlite3_db_r
3cd20 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 73 71  elease_memory(sq
3cd30 6c 69 74 65 33 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  lite3*);..../*..
3cd40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70  ** CAPI3REF: Imp
3cd50 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48  ose A Limit On H
3cd60 65 61 70 20 53 69 7a 65 0d 0a 2a 2a 0d 0a 2a 2a  eap Size..**..**
3cd70 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f   ^The sqlite3_so
3cd80 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28  ft_heap_limit64(
3cd90 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73  ) interface sets
3cda0 20 61 6e 64 2f 6f 72 20 71 75 65 72 69 65 73 20   and/or queries 
3cdb0 74 68 65 0d 0a 2a 2a 20 73 6f 66 74 20 6c 69 6d  the..** soft lim
3cdc0 69 74 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74  it on the amount
3cdd0 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20   of heap memory 
3cde0 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f  that may be allo
3cdf0 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  cated by SQLite.
3ce00 0d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 73 74 72  ..** ^SQLite str
3ce10 69 76 65 73 20 74 6f 20 6b 65 65 70 20 68 65 61  ives to keep hea
3ce20 70 20 6d 65 6d 6f 72 79 20 75 74 69 6c 69 7a 61  p memory utiliza
3ce30 74 69 6f 6e 20 62 65 6c 6f 77 20 74 68 65 20 73  tion below the s
3ce40 6f 66 74 20 68 65 61 70 0d 0a 2a 2a 20 6c 69 6d  oft heap..** lim
3ce50 69 74 20 62 79 20 72 65 64 75 63 69 6e 67 20 74  it by reducing t
3ce60 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
3ce70 65 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 70  es held in the p
3ce80 61 67 65 20 63 61 63 68 65 0d 0a 2a 2a 20 61 73  age cache..** as
3ce90 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 61   heap memory usa
3cea0 67 65 73 20 61 70 70 72 6f 61 63 68 65 73 20 74  ges approaches t
3ceb0 68 65 20 6c 69 6d 69 74 2e 0d 0a 2a 2a 20 5e 54  he limit...** ^T
3cec0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
3ced0 69 74 20 69 73 20 22 73 6f 66 74 22 20 62 65 63  it is "soft" bec
3cee0 61 75 73 65 20 65 76 65 6e 20 74 68 6f 75 67 68  ause even though
3cef0 20 53 51 4c 69 74 65 20 73 74 72 69 76 65 73 20   SQLite strives 
3cf00 74 6f 20 73 74 61 79 0d 0a 2a 2a 20 62 65 6c 6f  to stay..** belo
3cf10 77 20 74 68 65 20 6c 69 6d 69 74 2c 20 69 74 20  w the limit, it 
3cf20 77 69 6c 6c 20 65 78 63 65 65 64 20 74 68 65 20  will exceed the 
3cf30 6c 69 6d 69 74 20 72 61 74 68 65 72 20 74 68 61  limit rather tha
3cf40 6e 20 67 65 6e 65 72 61 74 65 0d 0a 2a 2a 20 61  n generate..** a
3cf50 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  n [SQLITE_NOMEM]
3cf60 20 65 72 72 6f 72 2e 20 20 49 6e 20 6f 74 68 65   error.  In othe
3cf70 72 20 77 6f 72 64 73 2c 20 74 68 65 20 73 6f 66  r words, the sof
3cf80 74 20 68 65 61 70 20 6c 69 6d 69 74 20 0d 0a 2a  t heap limit ..*
3cf90 2a 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e  * is advisory on
3cfa0 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  ly...**..** ^The
3cfb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
3cfc0 6f 6d 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f  om sqlite3_soft_
3cfd0 68 65 61 70 5f 6c 69 6d 69 74 36 34 28 29 20 69  heap_limit64() i
3cfe0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0d 0a 2a  s the size of..*
3cff0 2a 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  * the soft heap 
3d000 6c 69 6d 69 74 20 70 72 69 6f 72 20 74 6f 20 74  limit prior to t
3d010 68 65 20 63 61 6c 6c 2c 20 6f 72 20 6e 65 67 61  he call, or nega
3d020 74 69 76 65 20 69 6e 20 74 68 65 20 63 61 73 65  tive in the case
3d030 20 6f 66 20 61 6e 0d 0a 2a 2a 20 65 72 72 6f 72   of an..** error
3d040 2e 20 20 5e 49 66 20 74 68 65 20 61 72 67 75 6d  .  ^If the argum
3d050 65 6e 74 20 4e 20 69 73 20 6e 65 67 61 74 69 76  ent N is negativ
3d060 65 0d 0a 2a 2a 20 74 68 65 6e 20 6e 6f 20 63 68  e..** then no ch
3d070 61 6e 67 65 20 69 73 20 6d 61 64 65 20 74 6f 20  ange is made to 
3d080 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69  the soft heap li
3d090 6d 69 74 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  mit.  Hence, the
3d0a0 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 73 69 7a   current..** siz
3d0b0 65 20 6f 66 20 74 68 65 20 73 6f 66 74 20 68 65  e of the soft he
3d0c0 61 70 20 6c 69 6d 69 74 20 63 61 6e 20 62 65 20  ap limit can be 
3d0d0 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 69 6e  determined by in
3d0e0 76 6f 6b 69 6e 67 0d 0a 2a 2a 20 73 71 6c 69 74  voking..** sqlit
3d0f0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
3d100 69 74 36 34 28 29 20 77 69 74 68 20 61 20 6e 65  it64() with a ne
3d110 67 61 74 69 76 65 20 61 72 67 75 6d 65 6e 74 2e  gative argument.
3d120 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68 65  ..**..** ^If the
3d130 20 61 72 67 75 6d 65 6e 74 20 4e 20 69 73 20 7a   argument N is z
3d140 65 72 6f 20 74 68 65 6e 20 74 68 65 20 73 6f 66  ero then the sof
3d150 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20  t heap limit is 
3d160 64 69 73 61 62 6c 65 64 2e 0d 0a 2a 2a 0d 0a 2a  disabled...**..*
3d170 2a 20 5e 28 54 68 65 20 73 6f 66 74 20 68 65 61  * ^(The soft hea
3d180 70 20 6c 69 6d 69 74 20 69 73 20 6e 6f 74 20 65  p limit is not e
3d190 6e 66 6f 72 63 65 64 20 69 6e 20 74 68 65 20 63  nforced in the c
3d1a0 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74  urrent implement
3d1b0 61 74 69 6f 6e 0d 0a 2a 2a 20 69 66 20 6f 6e 65  ation..** if one
3d1c0 20 6f 72 20 6d 6f 72 65 20 6f 66 20 66 6f 6c 6c   or more of foll
3d1d0 6f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 73  owing conditions
3d1e0 20 61 72 65 20 74 72 75 65 3a 0d 0a 2a 2a 0d 0a   are true:..**..
3d1f0 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e  ** <ul>..** <li>
3d200 20 54 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   The soft heap l
3d210 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 7a  imit is set to z
3d220 65 72 6f 2e 0d 0a 2a 2a 20 3c 6c 69 3e 20 4d 65  ero...** <li> Me
3d230 6d 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20  mory accounting 
3d240 69 73 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  is disabled usin
3d250 67 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  g a combination 
3d260 6f 66 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20 20  of the..**      
3d270 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d  [sqlite3_config]
3d280 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  ([SQLITE_CONFIG_
3d290 4d 45 4d 53 54 41 54 55 53 5d 2c 2e 2e 2e 29 20  MEMSTATUS],...) 
3d2a0 73 74 61 72 74 2d 74 69 6d 65 20 6f 70 74 69 6f  start-time optio
3d2b0 6e 20 61 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 74  n and..**      t
3d2c0 68 65 20 5b 53 51 4c 49 54 45 5f 44 45 46 41 55  he [SQLITE_DEFAU
3d2d0 4c 54 5f 4d 45 4d 53 54 41 54 55 53 5d 20 63 6f  LT_MEMSTATUS] co
3d2e0 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
3d2f0 6e 2e 0d 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 61  n...** <li> An a
3d300 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65 20  lternative page 
3d310 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
3d320 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 69 65  tion is specifie
3d330 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 20 20 20 20  d using..**     
3d340 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
3d350 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ]([SQLITE_CONFIG
3d360 5f 50 43 41 43 48 45 32 5d 2c 2e 2e 2e 29 2e 0d  _PCACHE2],...)..
3d370 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 70 61 67  .** <li> The pag
3d380 65 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65  e cache allocate
3d390 73 20 66 72 6f 6d 20 69 74 73 20 6f 77 6e 20 6d  s from its own m
3d3a0 65 6d 6f 72 79 20 70 6f 6f 6c 20 73 75 70 70 6c  emory pool suppl
3d3b0 69 65 64 0d 0a 2a 2a 20 20 20 20 20 20 62 79 20  ied..**      by 
3d3c0 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d  [sqlite3_config]
3d3d0 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  ([SQLITE_CONFIG_
3d3e0 50 41 47 45 43 41 43 48 45 5d 2c 2e 2e 2e 29 20  PAGECACHE],...) 
3d3f0 72 61 74 68 65 72 20 74 68 61 6e 0d 0a 2a 2a 20  rather than..** 
3d400 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 68 65       from the he
3d410 61 70 2e 0d 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0d  ap...** </ul>)^.
3d420 0a 2a 2a 0d 0a 2a 2a 20 42 65 67 69 6e 6e 69 6e  .**..** Beginnin
3d430 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65  g with SQLite ve
3d440 72 73 69 6f 6e 20 33 2e 37 2e 33 2c 20 74 68 65  rsion 3.7.3, the
3d450 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
3d460 20 69 73 20 65 6e 66 6f 72 63 65 64 0d 0a 2a 2a   is enforced..**
3d470 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
3d480 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
3d490 65 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  e [SQLITE_ENABLE
3d4a0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
3d4b0 4e 54 5d 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d  NT]..** compile-
3d4c0 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 69  time option is i
3d4d0 6e 76 6f 6b 65 64 2e 20 20 57 69 74 68 20 5b 53  nvoked.  With [S
3d4e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
3d4f0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d 2c  ORY_MANAGEMENT],
3d500 0d 0a 2a 2a 20 74 68 65 20 73 6f 66 74 20 68 65  ..** the soft he
3d510 61 70 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f  ap limit is enfo
3d520 72 63 65 64 20 6f 6e 20 65 76 65 72 79 20 6d 65  rced on every me
3d530 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
3d540 20 20 57 69 74 68 6f 75 74 0d 0a 2a 2a 20 5b 53    Without..** [S
3d550 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
3d560 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d 2c  ORY_MANAGEMENT],
3d570 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
3d580 69 6d 69 74 20 69 73 20 6f 6e 6c 79 20 65 6e 66  imit is only enf
3d590 6f 72 63 65 64 0d 0a 2a 2a 20 77 68 65 6e 20 6d  orced..** when m
3d5a0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74  emory is allocat
3d5b0 65 64 20 62 79 20 74 68 65 20 70 61 67 65 20 63  ed by the page c
3d5c0 61 63 68 65 2e 20 20 54 65 73 74 69 6e 67 20 73  ache.  Testing s
3d5d0 75 67 67 65 73 74 73 20 74 68 61 74 20 62 65 63  uggests that bec
3d5e0 61 75 73 65 0d 0a 2a 2a 20 74 68 65 20 70 61 67  ause..** the pag
3d5f0 65 20 63 61 63 68 65 20 69 73 20 74 68 65 20 70  e cache is the p
3d600 72 65 64 6f 6d 69 6e 61 74 65 20 6d 65 6d 6f 72  redominate memor
3d610 79 20 75 73 65 72 20 69 6e 20 53 51 4c 69 74 65  y user in SQLite
3d620 2c 20 6d 6f 73 74 0d 0a 2a 2a 20 61 70 70 6c 69  , most..** appli
3d630 63 61 74 69 6f 6e 73 20 77 69 6c 6c 20 61 63 68  cations will ach
3d640 69 65 76 65 20 61 64 65 71 75 61 74 65 20 73 6f  ieve adequate so
3d650 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 65 6e  ft heap limit en
3d660 66 6f 72 63 65 6d 65 6e 74 20 77 69 74 68 6f 75  forcement withou
3d670 74 0d 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66  t..** the use of
3d680 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
3d690 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
3d6a0 54 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  T]...**..** The 
3d6b0 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 75 6e  circumstances un
3d6c0 64 65 72 20 77 68 69 63 68 20 53 51 4c 69 74 65  der which SQLite
3d6d0 20 77 69 6c 6c 20 65 6e 66 6f 72 63 65 20 74 68   will enforce th
3d6e0 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
3d6f0 74 20 6d 61 79 0d 0a 2a 2a 20 63 68 61 6e 67 65  t may..** change
3d700 73 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  s in future rele
3d710 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0d  ases of SQLite..
3d720 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3d730 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
3d740 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f  lite3_soft_heap_
3d750 6c 69 6d 69 74 36 34 28 73 71 6c 69 74 65 33 5f  limit64(sqlite3_
3d760 69 6e 74 36 34 20 4e 29 3b 0d 0a 0d 0a 2f 2a 0d  int64 N);..../*.
3d770 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
3d780 70 72 65 63 61 74 65 64 20 53 6f 66 74 20 48 65  precated Soft He
3d790 61 70 20 4c 69 6d 69 74 20 49 6e 74 65 72 66 61  ap Limit Interfa
3d7a0 63 65 0d 0a 2a 2a 20 44 45 50 52 45 43 41 54 45  ce..** DEPRECATE
3d7b0 44 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  D..**..** This i
3d7c0 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20 76  s a deprecated v
3d7d0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73  ersion of the [s
3d7e0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
3d7f0 5f 6c 69 6d 69 74 36 34 28 29 5d 0d 0a 2a 2a 20  _limit64()]..** 
3d800 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 69 73  interface.  This
3d810 20 72 6f 75 74 69 6e 65 20 69 73 20 70 72 6f 76   routine is prov
3d820 69 64 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69  ided for histori
3d830 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  cal compatibilit
3d840 79 0d 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 41 6c 6c  y..** only.  All
3d850 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e   new application
3d860 73 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65  s should use the
3d870 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6f  ..** [sqlite3_so
3d880 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28  ft_heap_limit64(
3d890 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 61 74  )] interface rat
3d8a0 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e  her than this on
3d8b0 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  e...*/..SQLITE_A
3d8c0 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43  PI SQLITE_DEPREC
3d8d0 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 74 65  ATED void sqlite
3d8e0 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
3d8f0 74 28 69 6e 74 20 4e 29 3b 0d 0a 0d 0a 0d 0a 2f  t(int N);....../
3d900 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  *..** CAPI3REF: 
3d910 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61  Extract Metadata
3d920 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20   About A Column 
3d930 4f 66 20 41 20 54 61 62 6c 65 0d 0a 2a 2a 0d 0a  Of A Table..**..
3d940 2a 2a 20 5e 54 68 69 73 20 72 6f 75 74 69 6e 65  ** ^This routine
3d950 20 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74   returns metadat
3d960 61 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66  a about a specif
3d970 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73  ic column of a s
3d980 70 65 63 69 66 69 63 0d 0a 2a 2a 20 64 61 74 61  pecific..** data
3d990 62 61 73 65 20 74 61 62 6c 65 20 61 63 63 65 73  base table acces
3d9a0 73 69 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20  sible using the 
3d9b0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3d9c0 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0d 0a 2a 2a  tion] handle..**
3d9d0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
3d9e0 69 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 72  irst function ar
3d9f0 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  gument...**..** 
3da00 5e 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69  ^The column is i
3da10 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
3da20 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64 20 61   second, third a
3da30 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  nd fourth parame
3da40 74 65 72 73 20 74 6f 0d 0a 2a 2a 20 74 68 69 73  ters to..** this
3da50 20 66 75 6e 63 74 69 6f 6e 2e 20 5e 54 68 65 20   function. ^The 
3da60 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
3da70 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 6e   is either the n
3da80 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
3da90 61 73 65 0d 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d  ase..** (i.e. "m
3daa0 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 20 6f 72  ain", "temp", or
3dab0 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74   an attached dat
3dac0 61 62 61 73 65 29 20 63 6f 6e 74 61 69 6e 69 6e  abase) containin
3dad0 67 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0d  g the specified.
3dae0 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c  .** table or NUL
3daf0 4c 2e 20 5e 49 66 20 69 74 20 69 73 20 4e 55 4c  L. ^If it is NUL
3db00 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 74 61  L, then all atta
3db10 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61  ched databases a
3db20 72 65 20 73 65 61 72 63 68 65 64 0d 0a 2a 2a 20  re searched..** 
3db30 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75 73  for the table us
3db40 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c 67  ing the same alg
3db50 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20 74  orithm used by t
3db60 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  he database engi
3db70 6e 65 20 74 6f 0d 0a 2a 2a 20 72 65 73 6f 6c 76  ne to..** resolv
3db80 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61  e unqualified ta
3db90 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0d  ble references..
3dba0 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 74 68 69  .**..** ^The thi
3dbb0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
3dbc0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
3dbd0 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
3dbe0 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  e table and colu
3dbf0 6d 6e 0d 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74  mn..** name of t
3dc00 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d  he desired colum
3dc10 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
3dc20 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73   Neither of thes
3dc30 65 20 70 61 72 61 6d 65 74 65 72 73 0d 0a 2a 2a  e parameters..**
3dc40 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0d 0a 2a   may be NULL...*
3dc50 2a 0d 0a 2a 2a 20 5e 4d 65 74 61 64 61 74 61 20  *..** ^Metadata 
3dc60 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 77  is returned by w
3dc70 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6d 65  riting to the me
3dc80 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 70  mory locations p
3dc90 61 73 73 65 64 20 61 73 20 74 68 65 20 35 74 68  assed as the 5th
3dca0 0d 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71 75  ..** and subsequ
3dcb0 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20 74  ent parameters t
3dcc0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
3dcd0 20 5e 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61   ^Any of these a
3dce0 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0d  rguments may be.
3dcf0 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69  .** NULL, in whi
3dd00 63 68 20 63 61 73 65 20 74 68 65 20 63 6f 72 72  ch case the corr
3dd10 65 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e  esponding elemen
3dd20 74 20 6f 66 20 6d 65 74 61 64 61 74 61 20 69 73  t of metadata is
3dd30 20 6f 6d 69 74 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a   omitted...**..*
3dd40 2a 20 5e 28 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  * ^(<blockquote>
3dd50 0d 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64  ..** <table bord
3dd60 65 72 3d 22 31 22 3e 0d 0a 2a 2a 20 3c 74 72 3e  er="1">..** <tr>
3dd70 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c  <th> Parameter <
3dd80 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79  th> Output<br>Ty
3dd90 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70  pe <th>  Descrip
3dda0 74 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 74 72  tion..**..** <tr
3ddb0 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63  ><td> 5th <td> c
3ddc0 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20  onst char* <td> 
3ddd0 44 61 74 61 20 74 79 70 65 0d 0a 2a 2a 20 3c 74  Data type..** <t
3dde0 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20  r><td> 6th <td> 
3ddf0 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e  const char* <td>
3de00 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74   Name of default
3de10 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3de20 6e 63 65 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  nce..** <tr><td>
3de30 20 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20   7th <td> int   
3de40 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20        <td> True 
3de50 69 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20  if column has a 
3de60 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
3de70 69 6e 74 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  int..** <tr><td>
3de80 20 38 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20   8th <td> int   
3de90 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20        <td> True 
3dea0 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  if column is par
3deb0 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
3dec0 20 4b 45 59 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64   KEY..** <tr><td
3ded0 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e 74 20 20  > 9th <td> int  
3dee0 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65         <td> True
3def0 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41   if column is [A
3df00 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d 0d 0a 2a  UTOINCREMENT]..*
3df10 2a 20 3c 2f 74 61 62 6c 65 3e 0d 0a 2a 2a 20 3c  * </table>..** <
3df20 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0d 0a  /blockquote>)^..
3df30 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 6d 65 6d 6f  **..** ^The memo
3df40 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ry pointed to by
3df50 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 70   the character p
3df60 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
3df70 20 66 6f 72 20 74 68 65 0d 0a 2a 2a 20 64 65 63   for the..** dec
3df80 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e  laration type an
3df90 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
3dfa0 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e  ence is valid on
3dfb0 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  ly until the nex
3dfc0 74 0d 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e  t..** call to an
3dfd0 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e  y SQLite API fun
3dfe0 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ction...**..** ^
3dff0 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64  If the specified
3e000 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c   table is actual
3e010 6c 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65  ly a view, an [e
3e020 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
3e030 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  turned...**..** 
3e040 5e 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65  ^If the specifie
3e050 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77  d column is "row
3e060 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f  id", "oid" or "_
3e070 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 6e 0d 0a  rowid_" and an..
3e080 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d  ** [INTEGER PRIM
3e090 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20  ARY KEY] column 
3e0a0 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69  has been explici
3e0b0 74 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 74 68  tly declared, th
3e0c0 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0d 0a 2a  en the output..*
3e0d0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
3e0e0 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 78 70   set for the exp
3e0f0 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
3e100 20 63 6f 6c 75 6d 6e 2e 20 5e 28 49 66 20 74 68   column. ^(If th
3e110 65 72 65 20 69 73 20 6e 6f 0d 0a 2a 2a 20 65 78  ere is no..** ex
3e120 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
3e130 64 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41  d [INTEGER PRIMA
3e140 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20  RY KEY] column, 
3e150 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0d  then the output.
3e160 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61  .** parameters a
3e170 72 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77  re set as follow
3e180 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 70 72 65 3e  s:..**..** <pre>
3e190 0d 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 74 79  ..**     data ty
3e1a0 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0d 0a 2a  pe: "INTEGER"..*
3e1b0 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20  *     collation 
3e1c0 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 52  sequence: "BINAR
3e1d0 59 22 0d 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e  Y"..**     not n
3e1e0 75 6c 6c 3a 20 30 0d 0a 2a 2a 20 20 20 20 20 70  ull: 0..**     p
3e1f0 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0d 0a 2a  rimary key: 1..*
3e200 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65  *     auto incre
3e210 6d 65 6e 74 3a 20 30 0d 0a 2a 2a 20 3c 2f 70 72  ment: 0..** </pr
3e220 65 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54  e>)^..**..** ^(T
3e230 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
3e240 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   load one or mor
3e250 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64  e schemas from d
3e260 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49  atabase files. I
3e270 66 20 61 6e 0d 0a 2a 2a 20 65 72 72 6f 72 20 6f  f an..** error o
3e280 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69  ccurs during thi
3e290 73 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66  s process, or if
3e2a0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74   the requested t
3e2b0 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0d 0a  able or column..
3e2c0 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  ** cannot be fou
3e2d0 6e 64 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f  nd, an [error co
3e2e0 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  de] is returned 
3e2f0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
3e300 73 61 67 65 20 6c 65 66 74 0d 0a 2a 2a 20 69 6e  sage left..** in
3e310 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3e320 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62  onnection] (to b
3e330 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e  e retrieved usin
3e340 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  g sqlite3_errmsg
3e350 28 29 29 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ()).)^..**..** ^
3e360 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79  This API is only
3e370 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
3e380 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
3e390 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0d  mpiled with the.
3e3a0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42  .** [SQLITE_ENAB
3e3b0 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
3e3c0 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73  TA] C-preprocess
3e3d0 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65  or symbol define
3e3e0 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  d...*/..SQLITE_A
3e3f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
3e400 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
3e410 64 61 74 61 28 0d 0a 20 20 73 71 6c 69 74 65 33  data(..  sqlite3
3e420 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
3e430 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
3e440 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  on handle */..  
3e450 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e  const char *zDbN
3e460 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44  ame,        /* D
3e470 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20  atabase name or 
3e480 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  NULL */..  const
3e490 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d   char *zTableNam
3e4a0 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  e,     /* Table 
3e4b0 6e 61 6d 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  name */..  const
3e4c0 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61   char *zColumnNa
3e4d0 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  me,    /* Column
3e4e0 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 63 68 61 72   name */..  char
3e4f0 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54   const **pzDataT
3e500 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55  ype,    /* OUTPU
3e510 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61  T: Declared data
3e520 20 74 79 70 65 20 2a 2f 0d 0a 20 20 63 68 61 72   type */..  char
3e530 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53   const **pzCollS
3e540 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55  eq,     /* OUTPU
3e550 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  T: Collation seq
3e560 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20  uence name */.. 
3e570 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20   int *pNotNull, 
3e580 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3e590 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
3e5a0 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
3e5b0 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0d 0a 20  int exists */.. 
3e5c0 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65   int *pPrimaryKe
3e5d0 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
3e5e0 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
3e5f0 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50  column part of P
3e600 4b 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 41 75  K */..  int *pAu
3e610 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20 20  toinc           
3e620 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
3e630 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
3e640 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20   auto-increment 
3e650 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..);..../*..**
3e660 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20   CAPI3REF: Load 
3e670 41 6e 20 45 78 74 65 6e 73 69 6f 6e 0d 0a 2a 2a  An Extension..**
3e680 0d 0a 2a 2a 20 5e 54 68 69 73 20 69 6e 74 65 72  ..** ^This inter
3e690 66 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51  face loads an SQ
3e6a0 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c  Lite extension l
3e6b0 69 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20  ibrary from the 
3e6c0 6e 61 6d 65 64 20 66 69 6c 65 2e 0d 0a 2a 2a 0d  named file...**.
3e6d0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
3e6e0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
3e6f0 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
3e700 6d 70 74 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0d  mpts to load an.
3e710 0a 2a 2a 20 53 51 4c 69 74 65 20 65 78 74 65 6e  .** SQLite exten
3e720 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e  sion library con
3e730 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69  tained in the fi
3e740 6c 65 20 7a 46 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a  le zFile...**..*
3e750 2a 20 5e 54 68 65 20 65 6e 74 72 79 20 70 6f 69  * ^The entry poi
3e760 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0d 0a 2a 2a  nt is zProc...**
3e770 20 5e 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30   ^zProc may be 0
3e780 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
3e790 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
3e7a0 65 6e 74 72 79 20 70 6f 69 6e 74 0d 0a 2a 2a 20  entry point..** 
3e7b0 64 65 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c  defaults to "sql
3e7c0 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
3e7d0 6e 69 74 22 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73  nit"...** ^The s
3e7e0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
3e7f0 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63  nsion() interfac
3e800 65 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 5b 53  e returns..** [S
3e810 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63  QLITE_OK] on suc
3e820 63 65 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45  cess and [SQLITE
3e830 5f 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74  _ERROR] if somet
3e840 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
3e850 0d 0a 2a 2a 20 5e 49 66 20 61 6e 20 65 72 72 6f  ..** ^If an erro
3e860 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45  r occurs and pzE
3e870 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20  rrMsg is not 0, 
3e880 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 5b 73 71  then the..** [sq
3e890 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
3e8a0 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63  sion()] interfac
3e8b0 65 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20  e shall attempt 
3e8c0 74 6f 0d 0a 2a 2a 20 66 69 6c 6c 20 2a 70 7a 45  to..** fill *pzE
3e8d0 72 72 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72  rrMsg with error
3e8e0 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 73 74   message text st
3e8f0 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0d 0a  ored in memory..
3e900 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ** obtained from
3e910 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3e920 28 29 5d 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67  ()]. The calling
3e930 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 73 68   function..** sh
3e940 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d  ould free this m
3e950 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67  emory by calling
3e960 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
3e970 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 45 78 74 65  ]...**..** ^Exte
3e980 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75  nsion loading mu
3e990 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73  st be enabled us
3e9a0 69 6e 67 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ing..** [sqlite3
3e9b0 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
3e9c0 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20  ension()] prior 
3e9d0 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  to calling this 
3e9e0 41 50 49 2c 0d 0a 2a 2a 20 6f 74 68 65 72 77 69  API,..** otherwi
3e9f0 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69 6c 6c  se an error will
3ea00 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   be returned...*
3ea10 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74  *..** See also t
3ea20 68 65 20 5b 6c 6f 61 64 5f 65 78 74 65 6e 73 69  he [load_extensi
3ea30 6f 6e 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  on() SQL functio
3ea40 6e 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  n]...*/..SQLITE_
3ea50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3ea60 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0d  load_extension(.
3ea70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3ea80 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64           /* Load
3ea90 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   the extension i
3eaa0 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73  nto this databas
3eab0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
3eac0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3ead0 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
3eae0 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
3eaf0 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
3eb00 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0d 0a  g extension */..
3eb10 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
3eb20 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79  roc,    /* Entry
3eb30 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64   point.  Derived
3eb40 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30   from zFile if 0
3eb50 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a   */..  char **pz
3eb60 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
3eb70 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
3eb80 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
3eb90 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..);..../*..**
3eba0 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c   CAPI3REF: Enabl
3ebb0 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74  e Or Disable Ext
3ebc0 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 0d 0a  ension Loading..
3ebd0 2a 2a 0d 0a 2a 2a 20 5e 53 6f 20 61 73 20 6e 6f  **..** ^So as no
3ebe0 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69  t to open securi
3ebf0 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65  ty holes in olde
3ec00 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  r applications t
3ec10 68 61 74 20 61 72 65 0d 0a 2a 2a 20 75 6e 70 72  hat are..** unpr
3ec20 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77  epared to deal w
3ec30 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ith extension lo
3ec40 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20  ading, and as a 
3ec50 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69  means of disabli
3ec60 6e 67 0d 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e  ng..** extension
3ec70 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65   loading while e
3ec80 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65  valuating user-e
3ec90 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20  ntered SQL, the 
3eca0 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0d 0a 2a  following API..*
3ecb0 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f  * is provided to
3ecc0 20 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74   turn the [sqlit
3ecd0 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
3ece0 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f  n()] mechanism o
3ecf0 6e 20 61 6e 64 20 6f 66 66 2e 0d 0a 2a 2a 0d 0a  n and off...**..
3ed00 2a 2a 20 5e 45 78 74 65 6e 73 69 6f 6e 20 6c 6f  ** ^Extension lo
3ed10 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20  ading is off by 
3ed20 64 65 66 61 75 6c 74 2e 20 53 65 65 20 74 69 63  default. See tic
3ed30 6b 65 74 20 23 31 38 36 33 2e 0d 0a 2a 2a 20 5e  ket #1863...** ^
3ed40 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 33  Call the sqlite3
3ed50 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
3ed60 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e 65  ension() routine
3ed70 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0d 0a   with onoff==1..
3ed80 2a 2a 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e  ** to turn exten
3ed90 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20  sion loading on 
3eda0 61 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68  and call it with
3edb0 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72   onoff==0 to tur
3edc0 6e 0d 0a 2a 2a 20 69 74 20 62 61 63 6b 20 6f 66  n..** it back of
3edd0 66 20 61 67 61 69 6e 2e 0d 0a 2a 2f 0d 0a 53 51  f again...*/..SQ
3ede0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3edf0 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64  ite3_enable_load
3ee00 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74  _extension(sqlit
3ee10 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66  e3 *db, int onof
3ee20 66 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  f);..../*..** CA
3ee30 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 69  PI3REF: Automati
3ee40 63 61 6c 6c 79 20 4c 6f 61 64 20 53 74 61 74 69  cally Load Stati
3ee50 63 61 6c 6c 79 20 4c 69 6e 6b 65 64 20 45 78 74  cally Linked Ext
3ee60 65 6e 73 69 6f 6e 73 0d 0a 2a 2a 0d 0a 2a 2a 20  ensions..**..** 
3ee70 5e 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20  ^This interface 
3ee80 63 61 75 73 65 73 20 74 68 65 20 78 45 6e 74 72  causes the xEntr
3ee90 79 50 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f  yPoint() functio
3eea0 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  n to be invoked 
3eeb0 66 6f 72 0d 0a 2a 2a 20 65 61 63 68 20 6e 65 77  for..** each new
3eec0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3eed0 63 74 69 6f 6e 5d 20 74 68 61 74 20 69 73 20 63  ction] that is c
3eee0 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 64 65  reated.  The ide
3eef0 61 20 68 65 72 65 20 69 73 20 74 68 61 74 0d 0a  a here is that..
3ef00 2a 2a 20 78 45 6e 74 72 79 50 6f 69 6e 74 28 29  ** xEntryPoint()
3ef10 20 69 73 20 74 68 65 20 65 6e 74 72 79 20 70 6f   is the entry po
3ef20 69 6e 74 20 66 6f 72 20 61 20 73 74 61 74 69 63  int for a static
3ef30 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 53 51 4c 69  ally linked SQLi
3ef40 74 65 20 65 78 74 65 6e 73 69 6f 6e 0d 0a 2a 2a  te extension..**
3ef50 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 61   that is to be a
3ef60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f 61  utomatically loa
3ef70 64 65 64 20 69 6e 74 6f 20 61 6c 6c 20 6e 65 77  ded into all new
3ef80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3ef90 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  tions...**..** ^
3efa0 28 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65  (Even though the
3efb0 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74   function protot
3efc0 79 70 65 20 73 68 6f 77 73 20 74 68 61 74 20 78  ype shows that x
3efd0 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 74 61 6b  EntryPoint() tak
3efe0 65 73 0d 0a 2a 2a 20 6e 6f 20 61 72 67 75 6d 65  es..** no argume
3eff0 6e 74 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20  nts and returns 
3f000 76 6f 69 64 2c 20 53 51 4c 69 74 65 20 69 6e 76  void, SQLite inv
3f010 6f 6b 65 73 20 78 45 6e 74 72 79 50 6f 69 6e 74  okes xEntryPoint
3f020 28 29 20 77 69 74 68 20 74 68 72 65 65 0d 0a 2a  () with three..*
3f030 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20  * arguments and 
3f040 65 78 70 65 63 74 73 20 61 6e 64 20 69 6e 74 65  expects and inte
3f050 67 65 72 20 72 65 73 75 6c 74 20 61 73 20 69 66  ger result as if
3f060 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f   the signature o
3f070 66 20 74 68 65 0d 0a 2a 2a 20 65 6e 74 72 79 20  f the..** entry 
3f080 70 6f 69 6e 74 20 77 68 65 72 65 20 61 73 20 66  point where as f
3f090 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ollows:..**..** 
3f0a0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
3f0b0 3e 0d 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 69 6e  >..** &nbsp;  in
3f0c0 74 20 78 45 6e 74 72 79 50 6f 69 6e 74 28 0d 0a  t xEntryPoint(..
3f0d0 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 73 71 6c  ** &nbsp;    sql
3f0e0 69 74 65 33 20 2a 64 62 2c 0d 0a 2a 2a 20 26 6e  ite3 *db,..** &n
3f0f0 62 73 70 3b 20 20 20 20 63 6f 6e 73 74 20 63 68  bsp;    const ch
3f100 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0d 0a  ar **pzErrMsg,..
3f110 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 63 6f 6e  ** &nbsp;    con
3f120 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
3f130 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a  3_api_routines *
3f140 70 54 68 75 6e 6b 0d 0a 2a 2a 20 26 6e 62 73 70  pThunk..** &nbsp
3f150 3b 20 20 29 3b 0d 0a 2a 2a 20 3c 2f 70 72 65 3e  ;  );..** </pre>
3f160 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0d  </blockquote>)^.
3f170 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 78  .**..** If the x
3f180 45 6e 74 72 79 50 6f 69 6e 74 20 72 6f 75 74 69  EntryPoint routi
3f190 6e 65 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e  ne encounters an
3f1a0 20 65 72 72 6f 72 2c 20 69 74 20 73 68 6f 75 6c   error, it shoul
3f1b0 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67  d make *pzErrMsg
3f1c0 0d 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e  ..** point to an
3f1d0 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   appropriate err
3f1e0 6f 72 20 6d 65 73 73 61 67 65 20 28 6f 62 74 61  or message (obta
3f1f0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
3f200 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 29 0d 0a  e3_mprintf()])..
3f210 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e  ** and return an
3f220 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72   appropriate [er
3f230 72 6f 72 20 63 6f 64 65 5d 2e 20 20 5e 53 51 4c  ror code].  ^SQL
3f240 69 74 65 20 65 6e 73 75 72 65 73 20 74 68 61 74  ite ensures that
3f250 20 2a 70 7a 45 72 72 4d 73 67 0d 0a 2a 2a 20 69   *pzErrMsg..** i
3f260 73 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61  s NULL before ca
3f270 6c 6c 69 6e 67 20 74 68 65 20 78 45 6e 74 72 79  lling the xEntry
3f280 50 6f 69 6e 74 28 29 2e 20 20 5e 53 51 4c 69 74  Point().  ^SQLit
3f290 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 0d 0a 2a  e will invoke..*
3f2a0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  * [sqlite3_free(
3f2b0 29 5d 20 6f 6e 20 2a 70 7a 45 72 72 4d 73 67 20  )] on *pzErrMsg 
3f2c0 61 66 74 65 72 20 78 45 6e 74 72 79 50 6f 69 6e  after xEntryPoin
3f2d0 74 28 29 20 72 65 74 75 72 6e 73 2e 20 20 5e 49  t() returns.  ^I
3f2e0 66 20 61 6e 79 0d 0a 2a 2a 20 78 45 6e 74 72 79  f any..** xEntry
3f2f0 50 6f 69 6e 74 28 29 20 72 65 74 75 72 6e 73 20  Point() returns 
3f300 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 20 5b 73  an error, the [s
3f310 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
3f320 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
3f330 29 5d 2c 0d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69  )],..** or [sqli
3f340 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 63  te3_open_v2()] c
3f350 61 6c 6c 20 74 68 61 74 20 70 72 6f 76 6f 6b 65  all that provoke
3f360 64 20 74 68 65 20 78 45 6e 74 72 79 50 6f 69 6e  d the xEntryPoin
3f370 74 28 29 20 77 69 6c 6c 20 66 61 69 6c 2e 0d 0a  t() will fail...
3f380 2a 2a 0d 0a 2a 2a 20 5e 43 61 6c 6c 69 6e 67 20  **..** ^Calling 
3f390 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
3f3a0 65 6e 73 69 6f 6e 28 58 29 20 77 69 74 68 20 61  ension(X) with a
3f3b0 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 58 20  n entry point X 
3f3c0 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 0d  that is already.
3f3d0 0a 2a 2a 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  .** on the list 
3f3e0 6f 66 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74  of automatic ext
3f3f0 65 6e 73 69 6f 6e 73 20 69 73 20 61 20 68 61 72  ensions is a har
3f400 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 20 5e 4e 6f  mless no-op. ^No
3f410 20 65 6e 74 72 79 20 70 6f 69 6e 74 0d 0a 2a 2a   entry point..**
3f420 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
3f430 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 66  more than once f
3f440 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
3f450 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
3f460 20 69 73 20 6f 70 65 6e 65 64 2e 0d 0a 2a 2a 0d   is opened...**.
3f470 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
3f480 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
3f490 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 2e 0d  o_extension()]..
3f4a0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3f4b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f  int sqlite3_auto
3f4c0 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20  _extension(void 
3f4d0 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76  (*xEntryPoint)(v
3f4e0 6f 69 64 29 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  oid));..../*..**
3f4f0 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74   CAPI3REF: Reset
3f500 20 41 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e   Automatic Exten
3f510 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 0d 0a 2a 2a  sion Loading..**
3f520 0d 0a 2a 2a 20 5e 54 68 69 73 20 69 6e 74 65 72  ..** ^This inter
3f530 66 61 63 65 20 64 69 73 61 62 6c 65 73 20 61 6c  face disables al
3f540 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  l automatic exte
3f550 6e 73 69 6f 6e 73 20 70 72 65 76 69 6f 75 73 6c  nsions previousl
3f560 79 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  y..** registered
3f570 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
3f580 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
3f590 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  ]...*/..SQLITE_A
3f5a0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3f5b0 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
3f5c0 73 69 6f 6e 28 76 6f 69 64 29 3b 0d 0a 0d 0a 2f  sion(void);..../
3f5d0 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66  *..** The interf
3f5e0 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75  ace to the virtu
3f5f0 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69  al-table mechani
3f600 73 6d 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  sm is currently 
3f610 63 6f 6e 73 69 64 65 72 65 64 0d 0a 2a 2a 20 74  considered..** t
3f620 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61  o be experimenta
3f630 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  l.  The interfac
3f640 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
3f650 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77  n incompatible w
3f660 61 79 73 2e 0d 0a 2a 2a 20 49 66 20 74 68 69 73  ays...** If this
3f670 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f   is a problem fo
3f680 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73  r you, do not us
3f690 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  e the interface 
3f6a0 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0d 0a 2a  at this time...*
3f6b0 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76  *..** When the v
3f6c0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
3f6d0 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65  hanism stabilize
3f6e0 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61  s, we will decla
3f6f0 72 65 20 74 68 65 0d 0a 2a 2a 20 69 6e 74 65 72  re the..** inter
3f700 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
3f710 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
3f720 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
3f730 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0d 0a 2a  this comment...*
3f740 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 74 72 75  /..../*..** Stru
3f750 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74  ctures used by t
3f760 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
3f770 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2f 0d 0a   interface..*/..
3f780 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
3f790 71 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69  qlite3_vtab sqli
3f7a0 74 65 33 5f 76 74 61 62 3b 0d 0a 74 79 70 65 64  te3_vtab;..typed
3f7b0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3f7c0 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c  3_index_info sql
3f7d0 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b  ite3_index_info;
3f7e0 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
3f7f0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
3f800 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 61  rsor sqlite3_vta
3f810 62 5f 63 75 72 73 6f 72 3b 0d 0a 74 79 70 65 64  b_cursor;..typed
3f820 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3f830 33 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33  3_module sqlite3
3f840 5f 6d 6f 64 75 6c 65 3b 0d 0a 0d 0a 2f 2a 0d 0a  _module;..../*..
3f850 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
3f860 74 75 61 6c 20 54 61 62 6c 65 20 4f 62 6a 65 63  tual Table Objec
3f870 74 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  t..** KEYWORDS: 
3f880 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b  sqlite3_module {
3f890 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
3f8a0 64 75 6c 65 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  dule}..**..** Th
3f8b0 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 73 6f  is structure, so
3f8c0 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 61  metimes called a
3f8d0 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   "virtual table 
3f8e0 6d 6f 64 75 6c 65 22 2c 20 0d 0a 2a 2a 20 64 65  module", ..** de
3f8f0 66 69 6e 65 73 20 74 68 65 20 69 6d 70 6c 65 6d  fines the implem
3f900 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 5b 76  entation of a [v
3f910 69 72 74 75 61 6c 20 74 61 62 6c 65 73 5d 2e 20  irtual tables]. 
3f920 20 0d 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63   ..** This struc
3f930 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20 6d 6f  ture consists mo
3f940 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64 73 20  stly of methods 
3f950 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0d  for the module..
3f960 0a 2a 2a 0d 0a 2a 2a 20 5e 41 20 76 69 72 74 75  .**..** ^A virtu
3f970 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
3f980 69 73 20 63 72 65 61 74 65 64 20 62 79 20 66 69  is created by fi
3f990 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69  lling in a persi
3f9a0 73 74 65 6e 74 0d 0a 2a 2a 20 69 6e 73 74 61 6e  stent..** instan
3f9b0 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
3f9c0 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67  ture and passing
3f9d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
3f9e0 61 74 20 69 6e 73 74 61 6e 63 65 0d 0a 2a 2a 20  at instance..** 
3f9f0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  to [sqlite3_crea
3fa00 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20  te_module()] or 
3fa10 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
3fa20 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0d 0a 2a  module_v2()]...*
3fa30 2a 20 5e 54 68 65 20 72 65 67 69 73 74 72 61 74  * ^The registrat
3fa40 69 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69  ion remains vali
3fa50 64 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65  d until it is re
3fa60 70 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66  placed by a diff
3fa70 65 72 65 6e 74 0d 0a 2a 2a 20 6d 6f 64 75 6c 65  erent..** module
3fa80 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64   or until the [d
3fa90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3faa0 6f 6e 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65  on] closes.  The
3fab0 20 63 6f 6e 74 65 6e 74 0d 0a 2a 2a 20 6f 66 20   content..** of 
3fac0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6d  this structure m
3fad0 75 73 74 20 6e 6f 74 20 63 68 61 6e 67 65 20 77  ust not change w
3fae0 68 69 6c 65 20 69 74 20 69 73 20 72 65 67 69 73  hile it is regis
3faf0 74 65 72 65 64 20 77 69 74 68 0d 0a 2a 2a 20 61  tered with..** a
3fb00 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ny database conn
3fb10 65 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 72  ection...*/..str
3fb20 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  uct sqlite3_modu
3fb30 6c 65 20 7b 0d 0a 20 20 69 6e 74 20 69 56 65 72  le {..  int iVer
3fb40 73 69 6f 6e 3b 0d 0a 20 20 69 6e 74 20 28 2a 78  sion;..  int (*x
3fb50 43 72 65 61 74 65 29 28 73 71 6c 69 74 65 33 2a  Create)(sqlite3*
3fb60 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0d 0a 20  , void *pAux,.. 
3fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
3fb80 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68  t argc, const ch
3fb90 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0d  ar *const*argv,.
3fba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3fbb0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
3fbc0 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0d  pVTab, char**);.
3fbd0 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 63  .  int (*xConnec
3fbe0 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  t)(sqlite3*, voi
3fbf0 64 20 2a 70 41 75 78 2c 0d 0a 20 20 20 20 20 20  d *pAux,..      
3fc00 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67           int arg
3fc10 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
3fc20 6f 6e 73 74 2a 61 72 67 76 2c 0d 0a 20 20 20 20  onst*argv,..    
3fc30 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3fc40 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
3fc50 2c 20 63 68 61 72 2a 2a 29 3b 0d 0a 20 20 69 6e  , char**);..  in
3fc60 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28  t (*xBestIndex)(
3fc70 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3fc80 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
3fc90 65 78 5f 69 6e 66 6f 2a 29 3b 0d 0a 20 20 69 6e  ex_info*);..  in
3fca0 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29  t (*xDisconnect)
3fcb0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
3fcc0 56 54 61 62 29 3b 0d 0a 20 20 69 6e 74 20 28 2a  VTab);..  int (*
3fcd0 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
3fce0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0d  3_vtab *pVTab);.
3fcf0 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28  .  int (*xOpen)(
3fd00 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3fd10 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  Tab, sqlite3_vta
3fd20 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  b_cursor **ppCur
3fd30 73 6f 72 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78  sor);..  int (*x
3fd40 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76  Close)(sqlite3_v
3fd50 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0d 0a 20  tab_cursor*);.. 
3fd60 20 69 6e 74 20 28 2a 78 46 69 6c 74 65 72 29 28   int (*xFilter)(
3fd70 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
3fd80 73 6f 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d  sor*, int idxNum
3fd90 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64  , const char *id
3fda0 78 53 74 72 2c 0d 0a 20 20 20 20 20 20 20 20 20  xStr,..         
3fdb0 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c         int argc,
3fdc0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
3fdd0 2a 61 72 67 76 29 3b 0d 0a 20 20 69 6e 74 20 28  *argv);..  int (
3fde0 2a 78 4e 65 78 74 29 28 73 71 6c 69 74 65 33 5f  *xNext)(sqlite3_
3fdf0 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0d 0a  vtab_cursor*);..
3fe00 20 20 69 6e 74 20 28 2a 78 45 6f 66 29 28 73 71    int (*xEof)(sq
3fe10 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
3fe20 72 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 43  r*);..  int (*xC
3fe30 6f 6c 75 6d 6e 29 28 73 71 6c 69 74 65 33 5f 76  olumn)(sqlite3_v
3fe40 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c  tab_cursor*, sql
3fe50 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
3fe60 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 52  nt);..  int (*xR
3fe70 6f 77 69 64 29 28 73 71 6c 69 74 65 33 5f 76 74  owid)(sqlite3_vt
3fe80 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69  ab_cursor*, sqli
3fe90 74 65 33 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69  te3_int64 *pRowi
3fea0 64 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 55 70  d);..  int (*xUp
3feb0 64 61 74 65 29 28 73 71 6c 69 74 65 33 5f 76 74  date)(sqlite3_vt
3fec0 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 74  ab *, int, sqlit
3fed0 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 73 71 6c  e3_value **, sql
3fee0 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 3b 0d 0a  ite3_int64 *);..
3fef0 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e 29 28    int (*xBegin)(
3ff00 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3ff10 54 61 62 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78  Tab);..  int (*x
3ff20 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 74  Sync)(sqlite3_vt
3ff30 61 62 20 2a 70 56 54 61 62 29 3b 0d 0a 20 20 69  ab *pVTab);..  i
3ff40 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71  nt (*xCommit)(sq
3ff50 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
3ff60 62 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 52 6f  b);..  int (*xRo
3ff70 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f  llback)(sqlite3_
3ff80 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0d 0a 20  vtab *pVTab);.. 
3ff90 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63   int (*xFindFunc
3ffa0 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  tion)(sqlite3_vt
3ffb0 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
3ffc0 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Arg, const char 
3ffd0 2a 7a 4e 61 6d 65 2c 0d 0a 20 20 20 20 20 20 20  *zName,..       
3ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fff0 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
40000 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
40010 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
40020 75 65 2a 2a 29 2c 0d 0a 20 20 20 20 20 20 20 20  ue**),..        
40030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
40040 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0d 0a 20  oid **ppArg);.. 
40050 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28   int (*xRename)(
40060 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
40070 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tab, const char 
40080 2a 7a 4e 65 77 29 3b 0d 0a 20 20 2f 2a 20 54 68  *zNew);..  /* Th
40090 65 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20  e methods above 
400a0 61 72 65 20 69 6e 20 76 65 72 73 69 6f 6e 20 31  are in version 1
400b0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 6d   of the sqlite_m
400c0 6f 64 75 6c 65 20 6f 62 6a 65 63 74 2e 20 54 68  odule object. Th
400d0 6f 73 65 20 0d 0a 20 20 2a 2a 20 62 65 6c 6f 77  ose ..  ** below
400e0 20 61 72 65 20 66 6f 72 20 76 65 72 73 69 6f 6e   are for version
400f0 20 32 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20   2 and greater. 
40100 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 53 61 76  */..  int (*xSav
40110 65 70 6f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f  epoint)(sqlite3_
40120 76 74 61 62 20 2a 70 56 54 61 62 2c 20 69 6e 74  vtab *pVTab, int
40130 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 52 65 6c  );..  int (*xRel
40140 65 61 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74  ease)(sqlite3_vt
40150 61 62 20 2a 70 56 54 61 62 2c 20 69 6e 74 29 3b  ab *pVTab, int);
40160 0d 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c 6c 62  ..  int (*xRollb
40170 61 63 6b 54 6f 29 28 73 71 6c 69 74 65 33 5f 76  ackTo)(sqlite3_v
40180 74 61 62 20 2a 70 56 54 61 62 2c 20 69 6e 74 29  tab *pVTab, int)
40190 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ;..};..../*..** 
401a0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
401b0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
401c0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a   Information..**
401d0 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74   KEYWORDS: sqlit
401e0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0d 0a 2a  e3_index_info..*
401f0 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  *..** The sqlite
40200 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
40210 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73  ucture and its s
40220 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20  ubstructures is 
40230 75 73 65 64 20 61 73 20 70 61 72 74 0d 0a 2a 2a  used as part..**
40240 20 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c   of the [virtual
40250 20 74 61 62 6c 65 5d 20 69 6e 74 65 72 66 61 63   table] interfac
40260 65 20 74 6f 0d 0a 2a 2a 20 70 61 73 73 20 69 6e  e to..** pass in
40270 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61  formation into a
40280 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72  nd receive the r
40290 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78  eply from the [x
402a0 42 65 73 74 49 6e 64 65 78 5d 0d 0a 2a 2a 20 6d  BestIndex]..** m
402b0 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74  ethod of a [virt
402c0 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
402d0 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75  ].  The fields u
402e0 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20  nder **Inputs** 
402f0 61 72 65 20 74 68 65 0d 0a 2a 2a 20 69 6e 70 75  are the..** inpu
40300 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78  ts to xBestIndex
40310 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e   and are read-on
40320 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20  ly.  xBestIndex 
40330 69 6e 73 65 72 74 73 20 69 74 73 0d 0a 2a 2a 20  inserts its..** 
40340 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65  results into the
40350 20 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65   **Outputs** fie
40360 6c 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54  lds...**..** ^(T
40370 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
40380 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 57   array records W
40390 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73  HERE clause cons
403a0 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 66  traints of the f
403b0 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 62 6c  orm:..**..** <bl
403c0 6f 63 6b 71 75 6f 74 65 3e 63 6f 6c 75 6d 6e 20  ockquote>column 
403d0 4f 50 20 65 78 70 72 3c 2f 62 6c 6f 63 6b 71 75  OP expr</blockqu
403e0 6f 74 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 77 68 65  ote>..**..** whe
403f0 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b  re OP is =, &lt;
40400 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f  , &lt;=, &gt;, o
40410 72 20 26 67 74 3b 3d 2e 29 5e 20 20 5e 28 54 68  r &gt;=.)^  ^(Th
40420 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65  e particular ope
40430 72 61 74 6f 72 20 69 73 0d 0a 2a 2a 20 73 74 6f  rator is..** sto
40440 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69  red in aConstrai
40450 6e 74 5b 5d 2e 6f 70 20 75 73 69 6e 67 20 6f 6e  nt[].op using on
40460 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 5b 53 51  e of the..** [SQ
40470 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
40480 52 41 49 4e 54 5f 45 51 20 7c 20 53 51 4c 49 54  RAINT_EQ | SQLIT
40490 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
404a0 4e 54 5f 20 76 61 6c 75 65 73 5d 2e 29 5e 0d 0a  NT_ values].)^..
404b0 2a 2a 20 5e 28 54 68 65 20 69 6e 64 65 78 20 6f  ** ^(The index o
404c0 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  f the column is 
404d0 73 74 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 61 43  stored in..** aC
404e0 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c  onstraint[].iCol
404f0 75 6d 6e 2e 29 5e 20 20 5e 28 61 43 6f 6e 73 74  umn.)^  ^(aConst
40500 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69  raint[].usable i
40510 73 20 54 52 55 45 20 69 66 20 74 68 65 0d 0a 2a  s TRUE if the..*
40520 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69  * expr on the ri
40530 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61  ght-hand side ca
40540 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 28  n be evaluated (
40550 61 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e  and thus the con
40560 73 74 72 61 69 6e 74 0d 0a 2a 2a 20 69 73 20 75  straint..** is u
40570 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65  sable) and false
40580 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 29 5e   if it cannot.)^
40590 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 6f 70  ..**..** ^The op
405a0 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d 61 74 69  timizer automati
405b0 63 61 6c 6c 79 20 69 6e 76 65 72 74 73 20 74 65  cally inverts te
405c0 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  rms of the form 
405d0 22 65 78 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22  "expr OP column"
405e0 0d 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 20 6f  ..** and makes o
405f0 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63 61 74  ther simplificat
40600 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48 45 52  ions to the WHER
40610 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e 20 61  E clause in an a
40620 74 74 65 6d 70 74 20 74 6f 0d 0a 2a 2a 20 67 65  ttempt to..** ge
40630 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45 20  t as many WHERE 
40640 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e 74  clause terms int
40650 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77 6e  o the form shown
40660 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69 62   above as possib
40670 6c 65 2e 0d 0a 2a 2a 20 5e 54 68 65 20 61 43 6f  le...** ^The aCo
40680 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
40690 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48   only reports WH
406a0 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
406b0 20 74 68 61 74 20 61 72 65 0d 0a 2a 2a 20 72 65   that are..** re
406c0 6c 65 76 61 6e 74 20 74 6f 20 74 68 65 20 70 61  levant to the pa
406d0 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c  rticular virtual
406e0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65   table being que
406f0 72 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49  ried...**..** ^I
40700 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
40710 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
40720 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20 69  ause is stored i
40730 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0d 0a 2a  n aOrderBy[]...*
40740 2a 20 5e 45 61 63 68 20 74 65 72 6d 20 6f 66 20  * ^Each term of 
40750 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73  aOrderBy records
40760 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
40770 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
40780 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 5b 78  ...**..** The [x
40790 42 65 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f  BestIndex] metho
407a0 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e  d must fill aCon
407b0 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77  straintUsage[] w
407c0 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0d  ith information.
407d0 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70  .** about what p
407e0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73  arameters to pas
407f0 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 5e  s to xFilter.  ^
40800 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74  If argvIndex>0 t
40810 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 72 69 67 68  hen..** the righ
40820 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
40830 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
40840 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69   aConstraint[] i
40850 73 20 65 76 61 6c 75 61 74 65 64 0d 0a 2a 2a 20  s evaluated..** 
40860 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20  and becomes the 
40870 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74  argvIndex-th ent
40880 72 79 20 69 6e 20 61 72 67 76 2e 20 20 5e 28 49  ry in argv.  ^(I
40890 66 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  f aConstraintUsa
408a0 67 65 5b 5d 2e 6f 6d 69 74 0d 0a 2a 2a 20 69 73  ge[].omit..** is
408b0 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
408c0 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 73  constraint is as
408d0 73 75 6d 65 64 20 74 6f 20 62 65 20 66 75 6c 6c  sumed to be full
408e0 79 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65  y handled by the
408f0 0d 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62  ..** virtual tab
40900 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 68  le and is not ch
40910 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79 20 53  ecked again by S
40920 51 4c 69 74 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  QLite.)^..**..**
40930 20 5e 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64   ^The idxNum and
40940 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61   idxPtr values a
40950 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20  re recorded and 
40960 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0d  passed into the.
40970 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65  .** [xFilter] me
40980 74 68 6f 64 2e 0d 0a 2a 2a 20 5e 5b 73 71 6c 69  thod...** ^[sqli
40990 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75  te3_free()] is u
409a0 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50  sed to free idxP
409b0 74 72 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69  tr if and only i
409c0 66 0d 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65  f..** needToFree
409d0 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0d  IdxPtr is true..
409e0 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 6f 72 64  .**..** ^The ord
409f0 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61  erByConsumed mea
40a00 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20 66  ns that output f
40a10 72 6f 6d 20 5b 78 46 69 6c 74 65 72 5d 2f 5b 78  rom [xFilter]/[x
40a20 4e 65 78 74 5d 20 77 69 6c 6c 20 6f 63 63 75 72  Next] will occur
40a30 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 63 6f 72 72   in..** the corr
40a40 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 61 74  ect order to sat
40a50 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42  isfy the ORDER B
40a60 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 61 74  Y clause so that
40a70 20 6e 6f 20 73 65 70 61 72 61 74 65 0d 0a 2a 2a   no separate..**
40a80 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 73   sorting step is
40a90 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a 2a 0d 0a   required...**..
40aa0 2a 2a 20 5e 54 68 65 20 65 73 74 69 6d 61 74 65  ** ^The estimate
40ab0 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61  dCost value is a
40ac0 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  n estimate of th
40ad0 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20  e cost of doing 
40ae0 74 68 65 0d 0a 2a 2a 20 70 61 72 74 69 63 75 6c  the..** particul
40af0 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75  ar lookup.  A fu
40b00 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62  ll scan of a tab
40b10 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65  le with N entrie
40b20 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0d 0a 2a  s should have..*
40b30 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20  * a cost of N.  
40b40 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20  A binary search 
40b50 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20  of a table of N 
40b60 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68  entries should h
40b70 61 76 65 20 61 0d 0a 2a 2a 20 63 6f 73 74 20 6f  ave a..** cost o
40b80 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20  f approximately 
40b90 6c 6f 67 28 4e 29 2e 0d 0a 2a 2f 0d 0a 73 74 72  log(N)...*/..str
40ba0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
40bb0 78 5f 69 6e 66 6f 20 7b 0d 0a 20 20 2f 2a 20 49  x_info {..  /* I
40bc0 6e 70 75 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  nputs */..  int 
40bd0 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  nConstraint;    
40be0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
40bf0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
40c00 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a 20  Constraint */.. 
40c10 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
40c20 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
40c30 20 7b 0d 0a 20 20 20 20 20 69 6e 74 20 69 43 6f   {..     int iCo
40c40 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lumn;           
40c50 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20     /* Column on 
40c60 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
40c70 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d  f constraint */.
40c80 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
40c90 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20  har op;         
40ca0 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70  /* Constraint op
40cb0 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 20  erator */..     
40cc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 73  unsigned char us
40cd0 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75  able;     /* Tru
40ce0 65 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72  e if this constr
40cf0 61 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a  aint is usable *
40d00 2f 0d 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72  /..     int iTer
40d10 6d 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  mOffset;        
40d20 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e    /* Used intern
40d30 61 6c 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65  ally - xBestInde
40d40 78 20 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20  x should ignore 
40d50 2a 2f 0d 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72  */..  } *aConstr
40d60 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  aint;           
40d70 20 2f 2a 20 54 61 62 6c 65 20 6f 66 20 57 48 45   /* Table of WHE
40d80 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72  RE clause constr
40d90 61 69 6e 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  aints */..  int 
40da0 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20 20  nOrderBy;       
40db0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
40dc0 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65   of terms in the
40dd0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
40de0 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 73 71   */..  struct sq
40df0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65  lite3_index_orde
40e00 72 62 79 20 7b 0d 0a 20 20 20 20 20 69 6e 74 20  rby {..     int 
40e10 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
40e20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
40e30 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20 20 20  number */..     
40e40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 65  unsigned char de
40e50 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  sc;       /* Tru
40e60 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 6c  e for DESC.  Fal
40e70 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0d 0a  se for ASC. */..
40e80 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20    } *aOrderBy;  
40e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40ea0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
40eb0 75 73 65 20 2a 2f 0d 0a 20 20 2f 2a 20 4f 75 74  use */..  /* Out
40ec0 70 75 74 73 20 2a 2f 0d 0a 20 20 73 74 72 75 63  puts */..  struc
40ed0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
40ee0 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
40ef0 20 7b 0d 0a 20 20 20 20 69 6e 74 20 61 72 67 76   {..    int argv
40f00 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20  Index;          
40f10 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74   /* if >0, const
40f20 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66  raint is part of
40f30 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72   argv to xFilter
40f40 20 2a 2f 0d 0a 20 20 20 20 75 6e 73 69 67 6e 65   */..    unsigne
40f50 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20  d char omit;    
40f60 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65    /* Do not code
40f70 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73   a test for this
40f80 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a   constraint */..
40f90 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74    } *aConstraint
40fa0 55 73 61 67 65 3b 0d 0a 20 20 69 6e 74 20 69 64  Usage;..  int id
40fb0 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  xNum;           
40fc0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75       /* Number u
40fd0 73 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20  sed to identify 
40fe0 74 68 65 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  the index */..  
40ff0 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20  char *idxStr;   
41000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
41010 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f  ring, possibly o
41020 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
41030 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0d 0a  ite3_malloc */..
41040 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 65    int needToFree
41050 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a 20  IdxStr;      /* 
41060 46 72 65 65 20 69 64 78 53 74 72 20 75 73 69 6e  Free idxStr usin
41070 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  g sqlite3_free()
41080 20 69 66 20 74 72 75 65 20 2a 2f 0d 0a 20 20 69   if true */..  i
41090 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  nt orderByConsum
410a0 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ed;       /* Tru
410b0 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20 61  e if output is a
410c0 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a  lready ordered *
410d0 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69  /..  double esti
410e0 6d 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20  matedCost;      
410f0 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73  /* Estimated cos
41100 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20  t of using this 
41110 69 6e 64 65 78 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  index */..};....
41120 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
41130 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43   Virtual Table C
41140 6f 6e 73 74 72 61 69 6e 74 20 4f 70 65 72 61 74  onstraint Operat
41150 6f 72 20 43 6f 64 65 73 0d 0a 2a 2a 0d 0a 2a 2a  or Codes..**..**
41160 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65   These macros de
41170 66 69 6e 65 64 20 74 68 65 20 61 6c 6c 6f 77 65  fined the allowe
41180 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65  d values for the
41190 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e  ..** [sqlite3_in
411a0 64 65 78 5f 69 6e 66 6f 5d 2e 61 43 6f 6e 73 74  dex_info].aConst
411b0 72 61 69 6e 74 5b 5d 2e 6f 70 20 66 69 65 6c 64  raint[].op field
411c0 2e 20 20 45 61 63 68 20 76 61 6c 75 65 20 72 65  .  Each value re
411d0 70 72 65 73 65 6e 74 73 0d 0a 2a 2a 20 61 6e 20  presents..** an 
411e0 6f 70 65 72 61 74 6f 72 20 74 68 61 74 20 69 73  operator that is
411f0 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6e 73 74   part of a const
41200 72 61 69 6e 74 20 74 65 72 6d 20 69 6e 20 74 68  raint term in th
41210 65 20 77 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e wHERE clause o
41220 66 0d 0a 2a 2a 20 61 20 71 75 65 72 79 20 74 68  f..** a query th
41230 61 74 20 75 73 65 73 20 61 20 5b 76 69 72 74 75  at uses a [virtu
41240 61 6c 20 74 61 62 6c 65 5d 2e 0d 0a 2a 2f 0d 0a  al table]...*/..
41250 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
41260 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
41270 45 51 20 20 20 20 32 0d 0a 23 64 65 66 69 6e 65  EQ    2..#define
41280 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
41290 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20 34  NSTRAINT_GT    4
412a0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
412b0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
412c0 54 5f 4c 45 20 20 20 20 38 0d 0a 23 64 65 66 69  T_LE    8..#defi
412d0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
412e0 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20  CONSTRAINT_LT   
412f0 20 31 36 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   16..#define SQL
41300 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
41310 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0d 0a 23  AINT_GE    32..#
41320 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
41330 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
41340 41 54 43 48 20 36 34 0d 0a 0d 0a 2f 2a 0d 0a 2a  ATCH 64..../*..*
41350 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
41360 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54  ster A Virtual T
41370 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74  able Implementat
41380 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  ion..**..** ^The
41390 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
413a0 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  used to register
413b0 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20   a new [virtual 
413c0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 6e 61  table module] na
413d0 6d 65 2e 0d 0a 2a 2a 20 5e 4d 6f 64 75 6c 65 20  me...** ^Module 
413e0 6e 61 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65  names must be re
413f0 67 69 73 74 65 72 65 64 20 62 65 66 6f 72 65 0d  gistered before.
41400 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 61 20 6e  .** creating a n
41410 65 77 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ew [virtual tabl
41420 65 5d 20 75 73 69 6e 67 20 74 68 65 20 6d 6f 64  e] using the mod
41430 75 6c 65 20 61 6e 64 20 62 65 66 6f 72 65 20 75  ule and before u
41440 73 69 6e 67 20 61 0d 0a 2a 2a 20 70 72 65 65 78  sing a..** preex
41450 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61 6c 20  isting [virtual 
41460 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65 20 6d  table] for the m
41470 6f 64 75 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  odule...**..** ^
41480 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
41490 69 73 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e  is registered on
414a0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
414b0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
414c0 66 69 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20  fied..** by the 
414d0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
414e0 20 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66 20 74    ^The name of t
414f0 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76  he module is giv
41500 65 6e 20 62 79 20 74 68 65 20 0d 0a 2a 2a 20 73  en by the ..** s
41510 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  econd parameter.
41520 20 20 5e 54 68 65 20 74 68 69 72 64 20 70 61 72    ^The third par
41530 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e  ameter is a poin
41540 74 65 72 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 69  ter to..** the i
41550 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
41560 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61   the [virtual ta
41570 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 5e  ble module].   ^
41580 54 68 65 20 66 6f 75 72 74 68 0d 0a 2a 2a 20 70  The fourth..** p
41590 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61  arameter is an a
415a0 72 62 69 74 72 61 72 79 20 63 6c 69 65 6e 74 20  rbitrary client 
415b0 64 61 74 61 20 70 6f 69 6e 74 65 72 20 74 68 61  data pointer tha
415c0 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f  t is passed thro
415d0 75 67 68 0d 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  ugh..** into the
415e0 20 5b 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b   [xCreate] and [
415f0 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64  xConnect] method
41600 73 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  s of the virtual
41610 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 0d 0a 2a   table module..*
41620 2a 20 77 68 65 6e 20 61 20 6e 65 77 20 76 69 72  * when a new vir
41630 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 62 65  tual table is be
41640 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 6f   being created o
41650 72 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 2e  r reinitialized.
41660 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71  ..**..** ^The sq
41670 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
41680 75 6c 65 5f 76 32 28 29 20 69 6e 74 65 72 66 61  ule_v2() interfa
41690 63 65 20 68 61 73 20 61 20 66 69 66 74 68 20 70  ce has a fifth p
416a0 61 72 61 6d 65 74 65 72 20 77 68 69 63 68 0d 0a  arameter which..
416b0 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ** is a pointer 
416c0 74 6f 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  to a destructor 
416d0 66 6f 72 20 74 68 65 20 70 43 6c 69 65 6e 74 44  for the pClientD
416e0 61 74 61 2e 20 20 5e 53 51 4c 69 74 65 20 77 69  ata.  ^SQLite wi
416f0 6c 6c 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  ll..** invoke th
41700 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
41710 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20  ction (if it is 
41720 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53  not NULL) when S
41730 51 4c 69 74 65 0d 0a 2a 2a 20 6e 6f 20 6c 6f 6e  QLite..** no lon
41740 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43  ger needs the pC
41750 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65  lientData pointe
41760 72 2e 20 20 5e 54 68 65 20 64 65 73 74 72 75 63  r.  ^The destruc
41770 74 6f 72 20 77 69 6c 6c 20 61 6c 73 6f 0d 0a 2a  tor will also..*
41780 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 69 66 20  * be invoked if 
41790 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
417a0 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
417b0 65 5f 76 32 28 29 20 66 61 69 6c 73 2e 0d 0a 2a  e_v2() fails...*
417c0 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63  * ^The sqlite3_c
417d0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 0d 0a  reate_module()..
417e0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
417f0 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 73 71  equivalent to sq
41800 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
41810 75 6c 65 5f 76 32 28 29 20 77 69 74 68 20 61 20  ule_v2() with a 
41820 4e 55 4c 4c 0d 0a 2a 2a 20 64 65 73 74 72 75 63  NULL..** destruc
41830 74 6f 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  tor...*/..SQLITE
41840 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
41850 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0d  _create_module(.
41860 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
41870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41880 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69   SQLite connecti
41890 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d  on to register m
418a0 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0d 0a 20  odule with */.. 
418b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
418c0 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
418d0 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  ame of the modul
418e0 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 73 71  e */..  const sq
418f0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 2c  lite3_module *p,
41900 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f     /* Methods fo
41910 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0d  r the module */.
41920 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74  .  void *pClient
41930 44 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a  Data          /*
41940 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
41950 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
41960 74 20 2a 2f 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45  t */..);..SQLITE
41970 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
41980 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
41990 32 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  2(..  sqlite3 *d
419a0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
419b0 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
419c0 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
419d0 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
419e0 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
419f0 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f  zName,         /
41a00 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f  * Name of the mo
41a10 64 75 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  dule */..  const
41a20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
41a30 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73  *p,   /* Methods
41a40 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20   for the module 
41a50 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69  */..  void *pCli
41a60 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20  entData,        
41a70 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
41a80 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
41a90 6e 65 63 74 20 2a 2f 0d 0a 20 20 76 6f 69 64 28  nect */..  void(
41aa0 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a  *xDestroy)(void*
41ab0 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20  )     /* Module 
41ac0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
41ad0 69 6f 6e 20 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a  ion */..);..../*
41ae0 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56  ..** CAPI3REF: V
41af0 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 73  irtual Table Ins
41b00 74 61 6e 63 65 20 4f 62 6a 65 63 74 0d 0a 2a 2a  tance Object..**
41b10 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74   KEYWORDS: sqlit
41b20 65 33 5f 76 74 61 62 0d 0a 2a 2a 0d 0a 2a 2a 20  e3_vtab..**..** 
41b30 45 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74  Every [virtual t
41b40 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70  able module] imp
41b50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73  lementation uses
41b60 20 61 20 73 75 62 63 6c 61 73 73 0d 0a 2a 2a 20   a subclass..** 
41b70 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 74  of this object t
41b80 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
41b90 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65  ticular instance
41ba0 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69 72  ..** of the [vir
41bb0 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45 61  tual table].  Ea
41bc0 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c 6c  ch subclass will
41bd0 0d 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64  ..** be tailored
41be0 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63   to the specific
41bf0 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f   needs of the mo
41c00 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
41c10 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 65 20 70 75 72  ion...** The pur
41c20 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70  pose of this sup
41c30 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65  erclass is to de
41c40 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65  fine certain fie
41c50 6c 64 73 20 74 68 61 74 20 61 72 65 0d 0a 2a 2a  lds that are..**
41c60 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d   common to all m
41c70 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
41c80 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  tions...**..** ^
41c90 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
41ca0 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61  ethods can set a
41cb0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
41cc0 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0d 0a  by assigning a..
41cd0 2a 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e  ** string obtain
41ce0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
41cf0 5f 6d 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a  _mprintf()] to z
41d00 45 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74  ErrMsg.  The met
41d10 68 6f 64 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 74  hod should..** t
41d20 61 6b 65 20 63 61 72 65 20 74 68 61 74 20 61 6e  ake care that an
41d30 79 20 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69  y prior string i
41d40 73 20 66 72 65 65 64 20 62 79 20 61 20 63 61 6c  s freed by a cal
41d50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72  l to [sqlite3_fr
41d60 65 65 28 29 5d 0d 0a 2a 2a 20 70 72 69 6f 72 20  ee()]..** prior 
41d70 74 6f 20 61 73 73 69 67 6e 69 6e 67 20 61 20 6e  to assigning a n
41d80 65 77 20 73 74 72 69 6e 67 20 74 6f 20 7a 45 72  ew string to zEr
41d90 72 4d 73 67 2e 20 20 5e 41 66 74 65 72 20 74 68  rMsg.  ^After th
41da0 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0d  e error message.
41db0 0a 2a 2a 20 69 73 20 64 65 6c 69 76 65 72 65 64  .** is delivered
41dc0 20 75 70 20 74 6f 20 74 68 65 20 63 6c 69 65 6e   up to the clien
41dd0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 20 74  t application, t
41de0 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62  he string will b
41df0 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0d  e automatically.
41e00 0a 2a 2a 20 66 72 65 65 64 20 62 79 20 73 71 6c  .** freed by sql
41e10 69 74 65 33 5f 66 72 65 65 28 29 20 61 6e 64 20  ite3_free() and 
41e20 74 68 65 20 7a 45 72 72 4d 73 67 20 66 69 65 6c  the zErrMsg fiel
41e30 64 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64  d will be zeroed
41e40 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 73 71  ...*/..struct sq
41e50 6c 69 74 65 33 5f 76 74 61 62 20 7b 0d 0a 20 20  lite3_vtab {..  
41e60 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
41e70 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
41e80 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f  /* The module fo
41e90 72 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74  r this virtual t
41ea0 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  able */..  int n
41eb0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
41ec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 4f             /* NO
41ed0 20 4c 4f 4e 47 45 52 20 55 53 45 44 20 2a 2f 0d   LONGER USED */.
41ee0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
41ef0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
41f00 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
41f10 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  age from sqlite3
41f20 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0d 0a 20  _mprintf() */.. 
41f30 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
41f40 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
41f50 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  s will typically
41f60 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
41f70 66 69 65 6c 64 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  fields */..};...
41f80 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
41f90 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  : Virtual Table 
41fa0 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 0d 0a 2a  Cursor Object..*
41fb0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
41fc0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
41fd0 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63  {virtual table c
41fe0 75 72 73 6f 72 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 45  ursor}..**..** E
41ff0 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61  very [virtual ta
42000 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c  ble module] impl
42010 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
42020 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68  a subclass of th
42030 65 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  e..** following 
42040 73 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 73  structure to des
42050 63 72 69 62 65 20 63 75 72 73 6f 72 73 20 74 68  cribe cursors th
42060 61 74 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68  at point into th
42070 65 0d 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74  e..** [virtual t
42080 61 62 6c 65 5d 20 61 6e 64 20 61 72 65 20 75 73  able] and are us
42090 65 64 0d 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74  ed..** to loop t
420a0 68 72 6f 75 67 68 20 74 68 65 20 76 69 72 74 75  hrough the virtu
420b0 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 72 73 6f  al table.  Curso
420c0 72 73 20 61 72 65 20 63 72 65 61 74 65 64 20 75  rs are created u
420d0 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 5b 73 71  sing the..** [sq
420e0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 4f 70  lite3_module.xOp
420f0 65 6e 20 7c 20 78 4f 70 65 6e 5d 20 6d 65 74 68  en | xOpen] meth
42100 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  od of the module
42110 20 61 6e 64 20 61 72 65 20 64 65 73 74 72 6f 79   and are destroy
42120 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20 5b 73  ed..** by the [s
42130 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 43  qlite3_module.xC
42140 6c 6f 73 65 20 7c 20 78 43 6c 6f 73 65 5d 20 6d  lose | xClose] m
42150 65 74 68 6f 64 2e 20 20 43 75 72 73 6f 72 73 20  ethod.  Cursors 
42160 61 72 65 20 75 73 65 64 0d 0a 2a 2a 20 62 79 20  are used..** by 
42170 74 68 65 20 5b 78 46 69 6c 74 65 72 5d 2c 20 5b  the [xFilter], [
42180 78 4e 65 78 74 5d 2c 20 5b 78 45 6f 66 5d 2c 20  xNext], [xEof], 
42190 5b 78 43 6f 6c 75 6d 6e 5d 2c 20 61 6e 64 20 5b  [xColumn], and [
421a0 78 52 6f 77 69 64 5d 20 6d 65 74 68 6f 64 73 0d  xRowid] methods.
421b0 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  .** of the modul
421c0 65 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c 65 20  e.  Each module 
421d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
421e0 69 6c 6c 20 64 65 66 69 6e 65 0d 0a 2a 2a 20 74  ill define..** t
421f0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
42200 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  cursor structure
42210 20 74 6f 20 73 75 69 74 20 69 74 73 20 6f 77 6e   to suit its own
42220 20 6e 65 65 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   needs...**..** 
42230 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73 20  This superclass 
42240 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72 20  exists in order 
42250 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64 73  to define fields
42260 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 74   of the cursor t
42270 68 61 74 0d 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d  hat..** are comm
42280 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d  on to all implem
42290 65 6e 74 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a  entations...*/..
422a0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76  struct sqlite3_v
422b0 74 61 62 5f 63 75 72 73 6f 72 20 7b 0d 0a 20 20  tab_cursor {..  
422c0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
422d0 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72  tab;      /* Vir
422e0 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68  tual table of th
422f0 69 73 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 20 20  is cursor */..  
42300 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
42310 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
42320 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
42330 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
42340 69 65 6c 64 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  ields */..};....
42350 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
42360 20 44 65 63 6c 61 72 65 20 54 68 65 20 53 63 68   Declare The Sch
42370 65 6d 61 20 4f 66 20 41 20 56 69 72 74 75 61 6c  ema Of A Virtual
42380 20 54 61 62 6c 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e   Table..**..** ^
42390 54 68 65 20 5b 78 43 72 65 61 74 65 5d 20 61 6e  The [xCreate] an
423a0 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 74  d [xConnect] met
423b0 68 6f 64 73 20 6f 66 20 61 0d 0a 2a 2a 20 5b 76  hods of a..** [v
423c0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
423d0 75 6c 65 5d 20 63 61 6c 6c 20 74 68 69 73 20 69  ule] call this i
423e0 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20 74 6f 20  nterface..** to 
423f0 64 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d  declare the form
42400 61 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e  at (the names an
42410 64 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74  d datatypes of t
42420 68 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0d 0a  he columns) of..
42430 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ** the virtual t
42440 61 62 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65  ables they imple
42450 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ment...*/..SQLIT
42460 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
42470 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73  3_declare_vtab(s
42480 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
42490 68 61 72 20 2a 7a 53 51 4c 29 3b 0d 0a 0d 0a 2f  har *zSQL);..../
424a0 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  *..** CAPI3REF: 
424b0 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 74  Overload A Funct
424c0 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 75 61  ion For A Virtua
424d0 6c 20 54 61 62 6c 65 0d 0a 2a 2a 0d 0a 2a 2a 20  l Table..**..** 
424e0 5e 28 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73  ^(Virtual tables
424f0 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74   can provide alt
42500 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65  ernative impleme
42510 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63  ntations of func
42520 74 69 6f 6e 73 0d 0a 2a 2a 20 75 73 69 6e 67 20  tions..** using 
42530 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69  the [xFindFuncti
42540 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  on] method of th
42550 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
42560 20 6d 6f 64 75 6c 65 5d 2e 20 20 0d 0a 2a 2a 20   module].  ..** 
42570 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69  But global versi
42580 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e  ons of those fun
42590 63 74 69 6f 6e 73 0d 0a 2a 2a 20 6d 75 73 74 20  ctions..** must 
425a0 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74  exist in order t
425b0 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e  o be overloaded.
425c0 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 69  )^..**..** ^(Thi
425d0 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65  s API makes sure
425e0 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f   a global versio
425f0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
42600 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
42610 72 0d 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e  r..** name and n
42620 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74  umber of paramet
42630 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 20  ers exists.  If 
42640 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e  no such function
42650 20 65 78 69 73 74 73 0d 0a 2a 2a 20 62 65 66 6f   exists..** befo
42660 72 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63  re this API is c
42670 61 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e  alled, a new fun
42680 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64  ction is created
42690 2e 29 5e 20 20 5e 54 68 65 20 69 6d 70 6c 65 6d  .)^  ^The implem
426a0 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a 20 6f 66 20  entation..** of 
426b0 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e  the new function
426c0 20 61 6c 77 61 79 73 20 63 61 75 73 65 73 20 61   always causes a
426d0 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 62  n exception to b
426e0 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0d 0a 2a  e thrown.  So..*
426f0 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69  * the new functi
42700 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20 66  on is not good f
42710 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20 69  or anything by i
42720 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c 79  tself.  Its only
42730 0d 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20  ..** purpose is 
42740 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c  to be a placehol
42750 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  der function tha
42760 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61  t can be overloa
42770 64 65 64 0d 0a 2a 2a 20 62 79 20 61 20 5b 76 69  ded..** by a [vi
42780 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0d 0a 2a  rtual table]...*
42790 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
427a0 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  t sqlite3_overlo
427b0 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ad_function(sqli
427c0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
427d0 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74   *zFuncName, int
427e0 20 6e 41 72 67 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   nArg);..../*..*
427f0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
42800 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
42810 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64  able mechanism d
42820 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61  efined above (ba
42830 63 6b 20 75 70 0d 0a 2a 2a 20 74 6f 20 61 20 63  ck up..** to a c
42840 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c  omment remarkabl
42850 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69  y similar to thi
42860 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e  s one) is curren
42870 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0d 0a  tly considered..
42880 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d  ** to be experim
42890 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65  ental.  The inte
428a0 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e  rface might chan
428b0 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62  ge in incompatib
428c0 6c 65 20 77 61 79 73 2e 0d 0a 2a 2a 20 49 66 20  le ways...** If 
428d0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65  this is a proble
428e0 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f  m for you, do no
428f0 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66  t use the interf
42900 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  ace at this time
42910 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74  ...**..** When t
42920 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
42930 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69   mechanism stabi
42940 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64  lizes, we will d
42950 65 63 6c 61 72 65 20 74 68 65 0d 0a 2a 2a 20 69  eclare the..** i
42960 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20  nterface fixed, 
42970 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66  support it indef
42980 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d  initely, and rem
42990 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74  ove this comment
429a0 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
429b0 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e 64  CAPI3REF: A Hand
429c0 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c  le To An Open BL
429d0 4f 42 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  OB..** KEYWORDS:
429e0 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b   {BLOB handle} {
429f0 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0d 0a 2a  BLOB handles}..*
42a00 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  *..** An instanc
42a10 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
42a20 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f   represents an o
42a30 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63  pen BLOB on whic
42a40 68 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  h..** [sqlite3_b
42a50 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65  lob_open | incre
42a60 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d  mental BLOB I/O]
42a70 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65   can be performe
42a80 64 2e 0d 0a 2a 2a 20 5e 4f 62 6a 65 63 74 73 20  d...** ^Objects 
42a90 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 65  of this type are
42aa0 20 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c   created by [sql
42ab0 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29  ite3_blob_open()
42ac0 5d 0d 0a 2a 2a 20 61 6e 64 20 64 65 73 74 72 6f  ]..** and destro
42ad0 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  yed by [sqlite3_
42ae0 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0d 0a  blob_close()]...
42af0 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33  ** ^The [sqlite3
42b00 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e  _blob_read()] an
42b10 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
42b20 77 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61  write()] interfa
42b30 63 65 73 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75  ces..** can be u
42b40 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77  sed to read or w
42b50 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65  rite small subse
42b60 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 4c  ctions of the BL
42b70 4f 42 2e 0d 0a 2a 2a 20 5e 54 68 65 20 5b 73 71  OB...** ^The [sq
42b80 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73  lite3_blob_bytes
42b90 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  ()] interface re
42ba0 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 6f  turns the size o
42bb0 66 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 79  f the BLOB in by
42bc0 74 65 73 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65  tes...*/..typede
42bd0 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
42be0 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c  _blob sqlite3_bl
42bf0 6f 62 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  ob;..../*..** CA
42c00 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42  PI3REF: Open A B
42c10 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e  LOB For Incremen
42c20 74 61 6c 20 49 2f 4f 0d 0a 2a 2a 0d 0a 2a 2a 20  tal I/O..**..** 
42c30 5e 28 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  ^(This interface
42c40 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20  s opens a [BLOB 
42c50 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d  handle | handle]
42c60 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63   to the BLOB loc
42c70 61 74 65 64 0d 0a 2a 2a 20 69 6e 20 72 6f 77 20  ated..** in row 
42c80 69 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f  iRow, column zCo
42c90 6c 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62  lumn, table zTab
42ca0 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a  le in database z
42cb0 44 62 3b 0d 0a 2a 2a 20 69 6e 20 6f 74 68 65 72  Db;..** in other
42cc0 20 77 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65   words, the same
42cd0 20 42 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64   BLOB that would
42ce0 20 62 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a   be selected by:
42cf0 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 70 72 65 3e 0d 0a  ..**..** <pre>..
42d00 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a 43  **     SELECT zC
42d10 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e 7a  olumn FROM zDb.z
42d20 54 61 62 6c 65 20 57 48 45 52 45 20 5b 72 6f 77  Table WHERE [row
42d30 69 64 5d 20 3d 20 69 52 6f 77 3b 0d 0a 2a 2a 20  id] = iRow;..** 
42d40 3c 2f 70 72 65 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  </pre>)^..**..**
42d50 20 5e 49 66 20 74 68 65 20 66 6c 61 67 73 20 70   ^If the flags p
42d60 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d  arameter is non-
42d70 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 42  zero, then the B
42d80 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  LOB is opened fo
42d90 72 20 72 65 61 64 0d 0a 2a 2a 20 61 6e 64 20 77  r read..** and w
42da0 72 69 74 65 20 61 63 63 65 73 73 2e 20 5e 49 66  rite access. ^If
42db0 20 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65   it is zero, the
42dc0 20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20   BLOB is opened 
42dd0 66 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 2e  for read access.
42de0 0d 0a 2a 2a 20 5e 49 74 20 69 73 20 6e 6f 74 20  ..** ^It is not 
42df0 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e  possible to open
42e00 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69   a column that i
42e10 73 20 70 61 72 74 20 6f 66 20 61 6e 20 69 6e 64  s part of an ind
42e20 65 78 20 6f 72 20 70 72 69 6d 61 72 79 20 0d 0a  ex or primary ..
42e30 2a 2a 20 6b 65 79 20 66 6f 72 20 77 72 69 74 69  ** key for writi
42e40 6e 67 2e 20 5e 49 66 20 5b 66 6f 72 65 69 67 6e  ng. ^If [foreign
42e50 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
42e60 5d 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20 69  ] are enabled, i
42e70 74 20 69 73 20 0d 0a 2a 2a 20 6e 6f 74 20 70 6f  t is ..** not po
42e80 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61  ssible to open a
42e90 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
42ea0 70 61 72 74 20 6f 66 20 61 20 5b 63 68 69 6c 64  part of a [child
42eb0 20 6b 65 79 5d 20 66 6f 72 20 77 72 69 74 69 6e   key] for writin
42ec0 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 4e 6f 74 65  g...**..** ^Note
42ed0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
42ee0 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74  se name is not t
42ef0 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74  he filename that
42f00 20 63 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 74 68   contains..** th
42f10 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 72  e database but r
42f20 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c  ather the symbol
42f30 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ic name of the d
42f40 61 74 61 62 61 73 65 20 74 68 61 74 0d 0a 2a 2a  atabase that..**
42f50 20 61 70 70 65 61 72 73 20 61 66 74 65 72 20 74   appears after t
42f60 68 65 20 41 53 20 6b 65 79 77 6f 72 64 20 77 68  he AS keyword wh
42f70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
42f80 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69  is connected usi
42f90 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0d 0a 2a 2a  ng [ATTACH]...**
42fa0 20 5e 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64   ^For the main d
42fb0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
42fc0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
42fd0 69 73 20 22 6d 61 69 6e 22 2e 0d 0a 2a 2a 20 5e  is "main"...** ^
42fe0 46 6f 72 20 54 45 4d 50 20 74 61 62 6c 65 73 2c  For TEMP tables,
42ff0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
43000 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0d 0a 2a  me is "temp"...*
43010 2a 0d 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 63 65  *..** ^(On succe
43020 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  ss, [SQLITE_OK] 
43030 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
43040 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61  the new [BLOB ha
43050 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e  ndle] is written
43060 0d 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 2e  ..** to *ppBlob.
43070 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65   Otherwise an [e
43080 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
43090 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 42 6c  turned and *ppBl
430a0 6f 62 20 69 73 20 73 65 74 0d 0a 2a 2a 20 74 6f  ob is set..** to
430b0 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74   be a null point
430c0 65 72 2e 29 5e 0d 0a 2a 2a 20 5e 54 68 69 73 20  er.)^..** ^This 
430d0 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68  function sets th
430e0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
430f0 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f  ection] error co
43100 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0d 0a  de and message..
43110 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  ** accessible vi
43120 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  a [sqlite3_errco
43130 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  de()] and [sqlit
43140 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64  e3_errmsg()] and
43150 20 72 65 6c 61 74 65 64 0d 0a 2a 2a 20 66 75 6e   related..** fun
43160 63 74 69 6f 6e 73 2e 20 5e 4e 6f 74 65 20 74 68  ctions. ^Note th
43170 61 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76  at the *ppBlob v
43180 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79  ariable is alway
43190 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e  s initialized in
431a0 20 61 0d 0a 2a 2a 20 77 61 79 20 74 68 61 74 20   a..** way that 
431b0 6d 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f  makes it safe to
431c0 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33   invoke [sqlite3
431d0 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f  _blob_close()] o
431e0 6e 20 2a 70 70 42 6c 6f 62 0d 0a 2a 2a 20 72 65  n *ppBlob..** re
431f0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
43200 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75  success or failu
43210 72 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  re of this routi
43220 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 49 66  ne...**..** ^(If
43230 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 61 20   the row that a 
43240 42 4c 4f 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e  BLOB handle poin
43250 74 73 20 74 6f 20 69 73 20 6d 6f 64 69 66 69 65  ts to is modifie
43260 64 20 62 79 20 61 6e 0d 0a 2a 2a 20 5b 55 50 44  d by an..** [UPD
43270 41 54 45 5d 2c 20 5b 44 45 4c 45 54 45 5d 2c 20  ATE], [DELETE], 
43280 6f 72 20 62 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49  or by [ON CONFLI
43290 43 54 5d 20 73 69 64 65 2d 65 66 66 65 63 74 73  CT] side-effects
432a0 0d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 42 4c  ..** then the BL
432b0 4f 42 20 68 61 6e 64 6c 65 20 69 73 20 6d 61 72  OB handle is mar
432c0 6b 65 64 20 61 73 20 22 65 78 70 69 72 65 64 22  ked as "expired"
432d0 2e 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 72  ...** This is tr
432e0 75 65 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  ue if any column
432f0 20 6f 66 20 74 68 65 20 72 6f 77 20 69 73 20 63   of the row is c
43300 68 61 6e 67 65 64 2c 20 65 76 65 6e 20 61 20 63  hanged, even a c
43310 6f 6c 75 6d 6e 0d 0a 2a 2a 20 6f 74 68 65 72 20  olumn..** other 
43320 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 74 68 65  than the one the
43330 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73 20   BLOB handle is 
43340 6f 70 65 6e 20 6f 6e 2e 29 5e 0d 0a 2a 2a 20 5e  open on.)^..** ^
43350 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  Calls to [sqlite
43360 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61  3_blob_read()] a
43370 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  nd [sqlite3_blob
43380 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0d 0a 2a  _write()] for..*
43390 2a 20 61 6e 20 65 78 70 69 72 65 64 20 42 4c 4f  * an expired BLO
433a0 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69  B handle fail wi
433b0 74 68 20 61 20 72 65 74 75 72 6e 20 63 6f 64 65  th a return code
433c0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
433d0 54 5d 2e 0d 0a 2a 2a 20 5e 28 43 68 61 6e 67 65  T]...** ^(Change
433e0 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61  s written into a
433f0 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74   BLOB prior to t
43400 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67  he BLOB expiring
43410 20 61 72 65 20 6e 6f 74 0d 0a 2a 2a 20 72 6f 6c   are not..** rol
43420 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 20  led back by the 
43430 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68  expiration of th
43440 65 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63 68  e BLOB.  Such ch
43450 61 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e 74  anges will event
43460 75 61 6c 6c 79 0d 0a 2a 2a 20 63 6f 6d 6d 69 74  ually..** commit
43470 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   if the transact
43480 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f  ion continues to
43490 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 29 5e 0d 0a   completion.)^..
434a0 2a 2a 0d 0a 2a 2a 20 5e 55 73 65 20 74 68 65 20  **..** ^Use the 
434b0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
434c0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
434d0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
434e0 65 20 73 69 7a 65 20 6f 66 0d 0a 2a 2a 20 74 68  e size of..** th
434f0 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20  e opened blob.  
43500 5e 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62  ^The size of a b
43510 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  lob may not be c
43520 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0d 0a  hanged by this..
43530 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55  ** interface.  U
43540 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20  se the [UPDATE] 
43550 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63  SQL command to c
43560 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f  hange the size o
43570 66 20 61 0d 0a 2a 2a 20 62 6c 6f 62 2e 0d 0a 2a  f a..** blob...*
43580 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69  *..** ^The [sqli
43590 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
435a0 62 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  b()] and [sqlite
435b0 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
435c0 62 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0d  b()] interfaces.
435d0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62 75 69 6c  .** and the buil
435e0 74 2d 69 6e 20 5b 7a 65 72 6f 62 6c 6f 62 5d 20  t-in [zeroblob] 
435f0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  SQL function can
43600 20 62 65 20 75 73 65 64 2c 20 69 66 20 64 65 73   be used, if des
43610 69 72 65 64 2c 0d 0a 2a 2a 20 74 6f 20 63 72 65  ired,..** to cre
43620 61 74 65 20 61 6e 20 65 6d 70 74 79 2c 20 7a 65  ate an empty, ze
43630 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 69  ro-filled blob i
43640 6e 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20  n which to read 
43650 6f 72 20 77 72 69 74 65 20 75 73 69 6e 67 0d 0a  or write using..
43660 2a 2a 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  ** this interfac
43670 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 61 76  e...**..** To av
43680 6f 69 64 20 61 20 72 65 73 6f 75 72 63 65 20 6c  oid a resource l
43690 65 61 6b 2c 20 65 76 65 72 79 20 6f 70 65 6e 20  eak, every open 
436a0 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 73 68  [BLOB handle] sh
436b0 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 0d  ould eventually.
436c0 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 20  .** be released 
436d0 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  by a call to [sq
436e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
436f0 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ()]...*/..SQLITE
43700 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
43710 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0d 0a 20 20 73  _blob_open(..  s
43720 71 6c 69 74 65 33 2a 2c 0d 0a 20 20 63 6f 6e 73  qlite3*,..  cons
43730 74 20 63 68 61 72 20 2a 7a 44 62 2c 0d 0a 20 20  t char *zDb,..  
43740 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
43750 6c 65 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  le,..  const cha
43760 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 0d 0a 20 20 73  r *zColumn,..  s
43770 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f  qlite3_int64 iRo
43780 77 2c 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c  w,..  int flags,
43790 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ..  sqlite3_blob
437a0 20 2a 2a 70 70 42 6c 6f 62 0d 0a 29 3b 0d 0a 0d   **ppBlob..);...
437b0 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
437c0 3a 20 4d 6f 76 65 20 61 20 42 4c 4f 42 20 48 61  : Move a BLOB Ha
437d0 6e 64 6c 65 20 74 6f 20 61 20 4e 65 77 20 52 6f  ndle to a New Ro
437e0 77 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73 20  w..**..** ^This 
437f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
43800 20 74 6f 20 6d 6f 76 65 20 61 6e 20 65 78 69 73   to move an exis
43810 74 69 6e 67 20 62 6c 6f 62 20 68 61 6e 64 6c 65  ting blob handle
43820 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
43830 74 73 0d 0a 2a 2a 20 74 6f 20 61 20 64 69 66 66  ts..** to a diff
43840 65 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  erent row of the
43850 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 74   same database t
43860 61 62 6c 65 2e 20 5e 54 68 65 20 6e 65 77 20 72  able. ^The new r
43870 6f 77 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ow is identified
43880 0d 0a 2a 2a 20 62 79 20 74 68 65 20 72 6f 77 69  ..** by the rowi
43890 64 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  d value passed a
438a0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
438b0 75 6d 65 6e 74 2e 20 4f 6e 6c 79 20 74 68 65 20  ument. Only the 
438c0 72 6f 77 20 63 61 6e 20 62 65 0d 0a 2a 2a 20 63  row can be..** c
438d0 68 61 6e 67 65 64 2e 20 5e 54 68 65 20 64 61 74  hanged. ^The dat
438e0 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64  abase, table and
438f0 20 63 6f 6c 75 6d 6e 20 6f 6e 20 77 68 69 63 68   column on which
43900 20 74 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c 65   the blob handle
43910 20 69 73 20 6f 70 65 6e 0d 0a 2a 2a 20 72 65 6d   is open..** rem
43920 61 69 6e 20 74 68 65 20 73 61 6d 65 2e 20 4d 6f  ain the same. Mo
43930 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  ving an existing
43940 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 6f 20   blob handle to 
43950 61 20 6e 65 77 20 72 6f 77 20 63 61 6e 20 62 65  a new row can be
43960 0d 0a 2a 2a 20 66 61 73 74 65 72 20 74 68 61 6e  ..** faster than
43970 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 65 78 69   closing the exi
43980 73 74 69 6e 67 20 68 61 6e 64 6c 65 20 61 6e 64  sting handle and
43990 20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20 6f   opening a new o
439a0 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68  ne...**..** ^(Th
439b0 65 20 6e 65 77 20 72 6f 77 20 6d 75 73 74 20 6d  e new row must m
439c0 65 65 74 20 74 68 65 20 73 61 6d 65 20 63 72 69  eet the same cri
439d0 74 65 72 69 61 20 61 73 20 66 6f 72 20 5b 73 71  teria as for [sq
439e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
439f0 29 5d 20 2d 0d 0a 2a 2a 20 69 74 20 6d 75 73 74  )] -..** it must
43a00 20 65 78 69 73 74 20 61 6e 64 20 74 68 65 72 65   exist and there
43a10 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
43a20 61 20 62 6c 6f 62 20 6f 72 20 74 65 78 74 20 76  a blob or text v
43a30 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0d 0a  alue stored in..
43a40 2a 2a 20 74 68 65 20 6e 6f 6d 69 6e 61 74 65 64  ** the nominated
43a50 20 63 6f 6c 75 6d 6e 2e 29 5e 20 5e 49 66 20 74   column.)^ ^If t
43a60 68 65 20 6e 65 77 20 72 6f 77 20 69 73 20 6e 6f  he new row is no
43a70 74 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  t present in the
43a80 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0d 0a 2a   table, or if..*
43a90 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 63 6f  * it does not co
43aa0 6e 74 61 69 6e 20 61 20 62 6c 6f 62 20 6f 72 20  ntain a blob or 
43ab0 74 65 78 74 20 76 61 6c 75 65 2c 20 6f 72 20 69  text value, or i
43ac0 66 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72 20  f another error 
43ad0 6f 63 63 75 72 73 2c 20 61 6e 0d 0a 2a 2a 20 53  occurs, an..** S
43ae0 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
43af0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
43b00 20 74 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c 65   the blob handle
43b10 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61   is considered a
43b20 62 6f 72 74 65 64 2e 0d 0a 2a 2a 20 5e 41 6c 6c  borted...** ^All
43b30 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
43b40 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  s to [sqlite3_bl
43b50 6f 62 5f 72 65 61 64 28 29 5d 2c 20 5b 73 71 6c  ob_read()], [sql
43b60 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
43b70 29 5d 20 6f 72 0d 0a 2a 2a 20 5b 73 71 6c 69 74  )] or..** [sqlit
43b80 65 33 5f 62 6c 6f 62 5f 72 65 6f 70 65 6e 28 29  e3_blob_reopen()
43b90 5d 20 6f 6e 20 61 6e 20 61 62 6f 72 74 65 64 20  ] on an aborted 
43ba0 62 6c 6f 62 20 68 61 6e 64 6c 65 20 69 6d 6d 65  blob handle imme
43bb0 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 0d 0a  diately return..
43bc0 2a 2a 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e  ** SQLITE_ABORT.
43bd0 20 5e 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74   ^Calling [sqlit
43be0 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d  e3_blob_bytes()]
43bf0 20 6f 6e 20 61 6e 20 61 62 6f 72 74 65 64 20 62   on an aborted b
43c00 6c 6f 62 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 61  lob handle..** a
43c10 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 7a 65  lways returns ze
43c20 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69  ro...**..** ^Thi
43c30 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20  s function sets 
43c40 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
43c50 64 6c 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  dle error code a
43c60 6e 64 20 6d 65 73 73 61 67 65 2e 0d 0a 2a 2f 0d  nd message...*/.
43c70 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
43c80 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
43c90 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
43ca0 5f 72 65 6f 70 65 6e 28 73 71 6c 69 74 65 33 5f  _reopen(sqlite3_
43cb0 62 6c 6f 62 20 2a 2c 20 73 71 6c 69 74 65 33 5f  blob *, sqlite3_
43cc0 69 6e 74 36 34 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  int64);..../*..*
43cd0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73  * CAPI3REF: Clos
43ce0 65 20 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 0d  e A BLOB Handle.
43cf0 0a 2a 2a 0d 0a 2a 2a 20 5e 43 6c 6f 73 65 73 20  .**..** ^Closes 
43d00 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61  an open [BLOB ha
43d10 6e 64 6c 65 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ndle]...**..** ^
43d20 43 6c 6f 73 69 6e 67 20 61 20 42 4c 4f 42 20 73  Closing a BLOB s
43d30 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 20 63  hall cause the c
43d40 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
43d50 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 0d 0a 2a 2a  on to commit..**
43d60 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
43d70 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c 20 6e 6f   other BLOBs, no
43d80 20 70 65 6e 64 69 6e 67 20 70 72 65 70 61 72 65   pending prepare
43d90 64 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 61 6e  d statements, an
43da0 64 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61  d the..** databa
43db0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
43dc0 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 74 20   in [autocommit 
43dd0 6d 6f 64 65 5d 2e 0d 0a 2a 2a 20 5e 49 66 20 61  mode]...** ^If a
43de0 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d  ny writes were m
43df0 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c  ade to the BLOB,
43e00 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68   they might be h
43e10 65 6c 64 20 69 6e 20 63 61 63 68 65 0d 0a 2a 2a  eld in cache..**
43e20 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65   until the close
43e30 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68   operation if th
43e40 65 79 20 77 69 6c 6c 20 66 69 74 2e 0d 0a 2a 2a  ey will fit...**
43e50 0d 0a 2a 2a 20 5e 28 43 6c 6f 73 69 6e 67 20 74  ..** ^(Closing t
43e60 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f  he BLOB often fo
43e70 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73  rces the changes
43e80 0d 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b  ..** out to disk
43e90 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49   and so if any I
43ea0 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  /O errors occur,
43eb0 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c   they will likel
43ec0 79 20 6f 63 63 75 72 0d 0a 2a 2a 20 61 74 20 74  y occur..** at t
43ed0 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65  he time when the
43ee0 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e   BLOB is closed.
43ef0 20 20 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61    Any errors tha
43f00 74 20 6f 63 63 75 72 20 64 75 72 69 6e 67 0d 0a  t occur during..
43f10 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 72 65 20 72  ** closing are r
43f20 65 70 6f 72 74 65 64 20 61 73 20 61 20 6e 6f 6e  eported as a non
43f30 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 76 61 6c  -zero return val
43f40 75 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28  ue.)^..**..** ^(
43f50 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73  The BLOB is clos
43f60 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  ed unconditional
43f70 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69  ly.  Even if thi
43f80 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
43f90 73 0d 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63  s..** an error c
43fa0 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73  ode, the BLOB is
43fb0 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 29 5e   still closed.)^
43fc0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 43 61 6c 6c 69 6e  ..**..** ^Callin
43fd0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  g this routine w
43fe0 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74  ith a null point
43ff0 65 72 20 28 73 75 63 68 20 61 73 20 77 6f 75 6c  er (such as woul
44000 64 20 62 65 20 72 65 74 75 72 6e 65 64 0d 0a 2a  d be returned..*
44010 2a 20 62 79 20 61 20 66 61 69 6c 65 64 20 63 61  * by a failed ca
44020 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ll to [sqlite3_b
44030 6c 6f 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20  lob_open()]) is 
44040 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
44050 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
44060 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
44070 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
44080 5f 62 6c 6f 62 20 2a 29 3b 0d 0a 0d 0a 2f 2a 0d  _blob *);..../*.
44090 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
440a0 74 75 72 6e 20 54 68 65 20 53 69 7a 65 20 4f 66  turn The Size Of
440b0 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 0d 0a 2a   An Open BLOB..*
440c0 2a 0d 0a 2a 2a 20 5e 52 65 74 75 72 6e 73 20 74  *..** ^Returns t
440d0 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  he size in bytes
440e0 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 61 63 63   of the BLOB acc
440f0 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20  essible via the 
44100 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c  ..** successfull
44110 79 20 6f 70 65 6e 65 64 20 5b 42 4c 4f 42 20 68  y opened [BLOB h
44120 61 6e 64 6c 65 5d 20 69 6e 20 69 74 73 20 6f 6e  andle] in its on
44130 6c 79 20 61 72 67 75 6d 65 6e 74 2e 20 20 5e 54  ly argument.  ^T
44140 68 65 0d 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74  he..** increment
44150 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72 6f 75 74  al blob I/O rout
44160 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79 20 72 65  ines can only re
44170 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 69 6e  ad or overwritin
44180 67 20 65 78 69 73 74 69 6e 67 0d 0a 2a 2a 20 62  g existing..** b
44190 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74 68 65  lob content; the
441a0 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20  y cannot change 
441b0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c  the size of a bl
441c0 6f 62 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ob...**..** This
441d0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f   routine only wo
441e0 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68  rks on a [BLOB h
441f0 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73  andle] which has
44200 20 62 65 65 6e 20 63 72 65 61 74 65 64 0d 0a 2a   been created..*
44210 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63  * by a prior suc
44220 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
44230 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
44240 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20  en()] and which 
44250 68 61 73 20 6e 6f 74 0d 0a 2a 2a 20 62 65 65 6e  has not..** been
44260 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69   closed by [sqli
44270 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
44280 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20  ].  Passing any 
44290 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e  other pointer in
442a0 0d 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75  ..** to this rou
442b0 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20  tine results in 
442c0 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72  undefined and pr
442d0 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62  obably undesirab
442e0 6c 65 20 62 65 68 61 76 69 6f 72 2e 0d 0a 2a 2f  le behavior...*/
442f0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
44300 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
44310 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
44320 20 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43   *);..../*..** C
44330 41 50 49 33 52 45 46 3a 20 52 65 61 64 20 44 61  API3REF: Read Da
44340 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42 20 49  ta From A BLOB I
44350 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 0d 0a 2a 2a  ncrementally..**
44360 0d 0a 2a 2a 20 5e 28 54 68 69 73 20 66 75 6e 63  ..** ^(This func
44370 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
44380 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  read data from a
44390 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  n open [BLOB han
443a0 64 6c 65 5d 20 69 6e 74 6f 20 61 0d 0a 2a 2a 20  dle] into a..** 
443b0 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20  caller-supplied 
443c0 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20  buffer. N bytes 
443d0 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69  of data are copi
443e0 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a  ed into buffer Z
443f0 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70  ..** from the op
44400 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e  en BLOB, startin
44410 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66  g at offset iOff
44420 73 65 74 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  set.)^..**..** ^
44430 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65  If offset iOffse
44440 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e  t is less than N
44450 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20   bytes from the 
44460 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c  end of the BLOB,
44470 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52  ..** [SQLITE_ERR
44480 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OR] is returned 
44490 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72  and no data is r
444a0 65 61 64 2e 20 20 5e 49 66 20 4e 20 6f 72 20 69  ead.  ^If N or i
444b0 4f 66 66 73 65 74 20 69 73 0d 0a 2a 2a 20 6c 65  Offset is..** le
444c0 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53  ss than zero, [S
444d0 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
444e0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
444f0 64 61 74 61 20 69 73 20 72 65 61 64 2e 0d 0a 2a  data is read...*
44500 2a 20 5e 54 68 65 20 73 69 7a 65 20 6f 66 20 74  * ^The size of t
44510 68 65 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e  he blob (and hen
44520 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76  ce the maximum v
44530 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65  alue of N+iOffse
44540 74 29 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65  t)..** can be de
44550 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74  termined using t
44560 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
44570 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
44580 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 41 6e  ace...**..** ^An
44590 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   attempt to read
445a0 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64   from an expired
445b0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
445c0 61 69 6c 73 20 77 69 74 68 20 61 6e 0d 0a 2a 2a  ails with an..**
445d0 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b   error code of [
445e0 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0d 0a  SQLITE_ABORT]...
445f0 2a 2a 0d 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 63  **..** ^(On succ
44600 65 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 6c 6f  ess, sqlite3_blo
44610 62 5f 72 65 61 64 28 29 20 72 65 74 75 72 6e 73  b_read() returns
44620 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 20   SQLITE_OK...** 
44630 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65  Otherwise, an [e
44640 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e  rror code] or an
44650 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72   [extended error
44660 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
44670 65 64 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ed.)^..**..** Th
44680 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
44690 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42  works on a [BLOB
446a0 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68   handle] which h
446b0 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0d  as been created.
446c0 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73  .** by a prior s
446d0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
446e0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  o [sqlite3_blob_
446f0 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63  open()] and whic
44700 68 20 68 61 73 20 6e 6f 74 0d 0a 2a 2a 20 62 65  h has not..** be
44710 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71  en closed by [sq
44720 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
44730 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e  ()].  Passing an
44740 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20  y other pointer 
44750 69 6e 0d 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72  in..** to this r
44760 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69  outine results i
44770 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20  n undefined and 
44780 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72  probably undesir
44790 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0d 0a  able behavior...
447a0 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  **..** See also:
447b0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
447c0 72 69 74 65 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51  rite()]...*/..SQ
447d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
447e0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73  ite3_blob_read(s
447f0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76  qlite3_blob *, v
44800 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69  oid *Z, int N, i
44810 6e 74 20 69 4f 66 66 73 65 74 29 3b 0d 0a 0d 0a  nt iOffset);....
44820 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
44830 20 57 72 69 74 65 20 44 61 74 61 20 49 6e 74 6f   Write Data Into
44840 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e   A BLOB Incremen
44850 74 61 6c 6c 79 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  tally..**..** ^T
44860 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
44870 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 61  used to write da
44880 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e 20  ta into an open 
44890 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 72  [BLOB handle] fr
448a0 6f 6d 20 61 0d 0a 2a 2a 20 63 61 6c 6c 65 72 2d  om a..** caller-
448b0 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e  supplied buffer.
448c0 20 5e 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74   ^N bytes of dat
448d0 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f  a are copied fro
448e0 6d 20 74 68 65 20 62 75 66 66 65 72 20 5a 0d 0a  m the buffer Z..
448f0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e  ** into the open
44900 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20   BLOB, starting 
44910 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65  at offset iOffse
44920 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 74  t...**..** ^If t
44930 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  he [BLOB handle]
44940 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
44950 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 61  irst argument wa
44960 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72  s not opened for
44970 0d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68  ..** writing (th
44980 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
44990 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  r to [sqlite3_bl
449a0 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a  ob_open()] was z
449b0 65 72 6f 29 2c 0d 0a 2a 2a 20 74 68 69 73 20 66  ero),..** this f
449c0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
449d0 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  [SQLITE_READONLY
449e0 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69 73  ]...**..** ^This
449f0 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e   function may on
44a00 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f  ly modify the co
44a10 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c  ntents of the BL
44a20 4f 42 3b 20 69 74 20 69 73 0d 0a 2a 2a 20 6e 6f  OB; it is..** no
44a30 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e  t possible to in
44a40 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
44a50 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20  of a BLOB using 
44a60 74 68 69 73 20 41 50 49 2e 0d 0a 2a 2a 20 5e 49  this API...** ^I
44a70 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74  f offset iOffset
44a80 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20   is less than N 
44a90 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65  bytes from the e
44aa0 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0d  nd of the BLOB,.
44ab0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  .** [SQLITE_ERRO
44ac0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
44ad0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72  nd no data is wr
44ae0 69 74 74 65 6e 2e 20 20 5e 49 66 20 4e 20 69 73  itten.  ^If N is
44af0 0d 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a  ..** less than z
44b00 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ero [SQLITE_ERRO
44b10 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
44b20 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72  nd no data is wr
44b30 69 74 74 65 6e 2e 0d 0a 2a 2a 20 54 68 65 20 73  itten...** The s
44b40 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20  ize of the BLOB 
44b50 28 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 6d  (and hence the m
44b60 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
44b70 4e 2b 69 4f 66 66 73 65 74 29 0d 0a 2a 2a 20 63  N+iOffset)..** c
44b80 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64  an be determined
44b90 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c 69   using the [sqli
44ba0 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29  te3_blob_bytes()
44bb0 5d 20 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2a  ] interface...**
44bc0 0d 0a 2a 2a 20 5e 41 6e 20 61 74 74 65 6d 70 74  ..** ^An attempt
44bd0 20 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e 20   to write to an 
44be0 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61  expired [BLOB ha
44bf0 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68  ndle] fails with
44c00 20 61 6e 0d 0a 2a 2a 20 65 72 72 6f 72 20 63 6f   an..** error co
44c10 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42  de of [SQLITE_AB
44c20 4f 52 54 5d 2e 20 20 5e 57 72 69 74 65 73 20 74  ORT].  ^Writes t
44c30 6f 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 20  o the BLOB that 
44c40 6f 63 63 75 72 72 65 64 0d 0a 2a 2a 20 62 65 66  occurred..** bef
44c50 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20 68 61  ore the [BLOB ha
44c60 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20 61 72  ndle] expired ar
44c70 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  e not rolled bac
44c80 6b 20 62 79 20 74 68 65 0d 0a 2a 2a 20 65 78 70  k by the..** exp
44c90 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 68  iration of the h
44ca0 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66  andle, though of
44cb0 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63 68   course those ch
44cc0 61 6e 67 65 73 20 6d 69 67 68 74 0d 0a 2a 2a 20  anges might..** 
44cd0 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72  have been overwr
44ce0 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61  itten by the sta
44cf0 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69  tement that expi
44d00 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e  red the BLOB han
44d10 64 6c 65 0d 0a 2a 2a 20 6f 72 20 62 79 20 6f 74  dle..** or by ot
44d20 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20  her independent 
44d30 73 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 0d  statements...**.
44d40 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 63 65 73 73  .** ^(On success
44d50 2c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77  , sqlite3_blob_w
44d60 72 69 74 65 28 29 20 72 65 74 75 72 6e 73 20 53  rite() returns S
44d70 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 20 4f 74  QLITE_OK...** Ot
44d80 68 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72  herwise, an  [er
44d90 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20  ror code] or an 
44da0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
44db0 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
44dc0 64 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  d.)^..**..** Thi
44dd0 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  s routine only w
44de0 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20  orks on a [BLOB 
44df0 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61  handle] which ha
44e00 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0d 0a  s been created..
44e10 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75  ** by a prior su
44e20 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
44e30 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
44e40 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68  pen()] and which
44e50 20 68 61 73 20 6e 6f 74 0d 0a 2a 2a 20 62 65 65   has not..** bee
44e60 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c  n closed by [sql
44e70 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
44e80 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79  )].  Passing any
44e90 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69   other pointer i
44ea0 6e 0d 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f  n..** to this ro
44eb0 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e  utine results in
44ec0 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70   undefined and p
44ed0 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61  robably undesira
44ee0 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0d 0a 2a  ble behavior...*
44ef0 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  *..** See also: 
44f00 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
44f10 61 64 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  ad()]...*/..SQLI
44f20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
44f30 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71  e3_blob_write(sq
44f40 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 63 6f  lite3_blob *, co
44f50 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74  nst void *z, int
44f60 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   n, int iOffset)
44f70 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
44f80 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46 69  3REF: Virtual Fi
44f90 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63 74  le System Object
44fa0 73 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 76 69 72 74  s..**..** A virt
44fb0 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 28  ual filesystem (
44fc0 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c 69  VFS) is an [sqli
44fd0 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 0d  te3_vfs] object.
44fe0 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20  .** that SQLite 
44ff0 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74  uses to interact
45000 0d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e  ..** with the un
45010 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
45020 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74  ng system.  Most
45030 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63   SQLite builds c
45040 6f 6d 65 20 77 69 74 68 20 61 0d 0a 2a 2a 20 73  ome with a..** s
45050 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46  ingle default VF
45060 53 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70  S that is approp
45070 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f  riate for the ho
45080 73 74 20 63 6f 6d 70 75 74 65 72 2e 0d 0a 2a 2a  st computer...**
45090 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 62   New VFSes can b
450a0 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e 64  e registered and
450b0 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 20   existing VFSes 
450c0 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65  can be unregiste
450d0 72 65 64 2e 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  red...** The fol
450e0 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65  lowing interface
450f0 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0d  s are provided..
45100 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c  .**..** ^The sql
45110 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20  ite3_vfs_find() 
45120 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
45130 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
45140 20 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e   VFS given its n
45150 61 6d 65 2e 0d 0a 2a 2a 20 5e 4e 61 6d 65 73 20  ame...** ^Names 
45160 61 72 65 20 63 61 73 65 20 73 65 6e 73 69 74 69  are case sensiti
45170 76 65 2e 0d 0a 2a 2a 20 5e 4e 61 6d 65 73 20 61  ve...** ^Names a
45180 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  re zero-terminat
45190 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73  ed UTF-8 strings
451a0 2e 0d 0a 2a 2a 20 5e 49 66 20 74 68 65 72 65 20  ...** ^If there 
451b0 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 61 20 4e  is no match, a N
451c0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72  ULL pointer is r
451d0 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 5e 49 66  eturned...** ^If
451e0 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55 4c   zVfsName is NUL
451f0 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75  L then the defau
45200 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 72 6e  lt VFS is return
45210 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 4e 65 77  ed...**..** ^New
45220 20 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73   VFSes are regis
45230 74 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74  tered with sqlit
45240 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
45250 29 2e 0d 0a 2a 2a 20 5e 45 61 63 68 20 6e 65 77  )...** ^Each new
45260 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65   VFS becomes the
45270 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 20   default VFS if 
45280 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61  the makeDflt fla
45290 67 20 69 73 20 73 65 74 2e 0d 0a 2a 2a 20 5e 54  g is set...** ^T
452a0 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20  he same VFS can 
452b0 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75  be registered mu
452c0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74  ltiple times wit
452d0 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0d 0a 2a 2a  hout injury...**
452e0 20 5e 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69   ^To make an exi
452f0 73 74 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74  sting VFS into t
45300 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20  he default VFS, 
45310 72 65 67 69 73 74 65 72 20 69 74 20 61 67 61 69  register it agai
45320 6e 0d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6d  n..** with the m
45330 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 73 65 74  akeDflt flag set
45340 2e 20 20 49 66 20 74 77 6f 20 64 69 66 66 65 72  .  If two differ
45350 65 6e 74 20 56 46 53 65 73 20 77 69 74 68 20 74  ent VFSes with t
45360 68 65 0d 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d 65  he..** same name
45370 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 2c   are registered,
45380 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
45390 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 20   undefined.  If 
453a0 61 0d 0a 2a 2a 20 56 46 53 20 69 73 20 72 65 67  a..** VFS is reg
453b0 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20 6e  istered with a n
453c0 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c 4c  ame that is NULL
453d0 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72   or an empty str
453e0 69 6e 67 2c 0d 0a 2a 2a 20 74 68 65 6e 20 74 68  ing,..** then th
453f0 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
45400 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  defined...**..**
45410 20 5e 55 6e 72 65 67 69 73 74 65 72 20 61 20 56   ^Unregister a V
45420 46 53 20 77 69 74 68 20 74 68 65 20 73 71 6c 69  FS with the sqli
45430 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
45440 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0d  er() interface..
45450 0a 2a 2a 20 5e 28 49 66 20 74 68 65 20 64 65 66  .** ^(If the def
45460 61 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65  ault VFS is unre
45470 67 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65  gistered, anothe
45480 72 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20  r VFS is chosen 
45490 61 73 0d 0a 2a 2a 20 74 68 65 20 64 65 66 61 75  as..** the defau
454a0 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20  lt.  The choice 
454b0 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 53 20  for the new VFS 
454c0 69 73 20 61 72 62 69 74 72 61 72 79 2e 29 5e 0d  is arbitrary.)^.
454d0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
454e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c  sqlite3_vfs *sql
454f0 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f  ite3_vfs_find(co
45500 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 4e 61  nst char *zVfsNa
45510 6d 65 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  me);..SQLITE_API
45520 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
45530 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  _register(sqlite
45540 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d 61 6b 65  3_vfs*, int make
45550 44 66 6c 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 41  Dflt);..SQLITE_A
45560 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
45570 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71  fs_unregister(sq
45580 6c 69 74 65 33 5f 76 66 73 2a 29 3b 0d 0a 0d 0a  lite3_vfs*);....
45590 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  /*..** CAPI3REF:
455a0 20 4d 75 74 65 78 65 73 0d 0a 2a 2a 0d 0a 2a 2a   Mutexes..**..**
455b0 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65   The SQLite core
455c0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74   uses these rout
455d0 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64 0d  ines for thread.
455e0 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74  .** synchronizat
455f0 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 79  ion. Though they
45600 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
45610 72 20 69 6e 74 65 72 6e 61 6c 0d 0a 2a 2a 20 75  r internal..** u
45620 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f  se by SQLite, co
45630 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  de that links ag
45640 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0d  ainst SQLite is.
45650 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f  .** permitted to
45660 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73   use any of thes
45670 65 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a 2a 0d  e routines...**.
45680 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73  .** The SQLite s
45690 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61  ource code conta
456a0 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ins multiple imp
456b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0d 0a 2a 2a  lementations..**
456c0 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20   of these mutex 
456d0 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70  routines.  An ap
456e0 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d  propriate implem
456f0 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20  entation..** is 
45700 73 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74  selected automat
45710 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c  ically at compil
45720 65 2d 74 69 6d 65 2e 20 20 5e 28 54 68 65 20 66  e-time.  ^(The f
45730 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 69 6d 70  ollowing..** imp
45740 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65  lementations are
45750 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
45760 65 20 53 51 4c 69 74 65 20 63 6f 72 65 3a 0d 0a  e SQLite core:..
45770 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20  **..** <ul>..** 
45780 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55  <li>   SQLITE_MU
45790 54 45 58 5f 4f 53 32 0d 0a 2a 2a 20 3c 6c 69 3e  TEX_OS2..** <li>
457a0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
457b0 50 54 48 52 45 41 44 53 0d 0a 2a 2a 20 3c 6c 69  PTHREADS..** <li
457c0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
457d0 5f 57 33 32 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 20  _W32..** <li>   
457e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
457f0 50 0d 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0d 0a 2a  P..** </ul>)^..*
45800 2a 0d 0a 2a 2a 20 5e 54 68 65 20 53 51 4c 49 54  *..** ^The SQLIT
45810 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70  E_MUTEX_NOOP imp
45820 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
45830 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
45840 0d 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e  ..** that does n
45850 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61  o real locking a
45860 6e 64 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  nd is appropriat
45870 65 20 66 6f 72 20 75 73 65 20 69 6e 0d 0a 2a 2a  e for use in..**
45880 20 61 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64   a single-thread
45890 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20  ed application. 
458a0 20 5e 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54   ^The SQLITE_MUT
458b0 45 58 5f 4f 53 32 2c 0d 0a 2a 2a 20 53 51 4c 49  EX_OS2,..** SQLI
458c0 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
458d0 53 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55  S, and SQLITE_MU
458e0 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e  TEX_W32 implemen
458f0 74 61 74 69 6f 6e 73 0d 0a 2a 2a 20 61 72 65 20  tations..** are 
45900 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
45910 75 73 65 20 6f 6e 20 4f 53 2f 32 2c 20 55 6e 69  use on OS/2, Uni
45920 78 2c 20 61 6e 64 20 57 69 6e 64 6f 77 73 2e 0d  x, and Windows..
45930 0a 2a 2a 0d 0a 2a 2a 20 5e 28 49 66 20 53 51 4c  .**..** ^(If SQL
45940 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ite is compiled 
45950 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
45960 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72 65  MUTEX_APPDEF pre
45970 70 72 6f 63 65 73 73 6f 72 0d 0a 2a 2a 20 6d 61  processor..** ma
45980 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74  cro defined (wit
45990 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45  h "-DSQLITE_MUTE
459a0 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68  X_APPDEF=1"), th
459b0 65 6e 20 6e 6f 20 6d 75 74 65 78 0d 0a 2a 2a 20  en no mutex..** 
459c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
459d0 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20  s included with 
459e0 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20  the library. In 
459f0 74 68 69 73 20 63 61 73 65 20 74 68 65 0d 0a 2a  this case the..*
45a00 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  * application mu
45a10 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 74  st supply a cust
45a20 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  om mutex impleme
45a30 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68  ntation using th
45a40 65 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  e..** [SQLITE_CO
45a50 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69  NFIG_MUTEX] opti
45a60 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
45a70 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74  3_config() funct
45a80 69 6f 6e 0d 0a 2a 2a 20 62 65 66 6f 72 65 20 63  ion..** before c
45a90 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
45aa0 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61  nitialize() or a
45ab0 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20  ny other public 
45ac0 73 71 6c 69 74 65 33 5f 0d 0a 2a 2a 20 66 75 6e  sqlite3_..** fun
45ad0 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73  ction that calls
45ae0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
45af0 69 7a 65 28 29 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  ize().)^..**..**
45b00 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75   ^The sqlite3_mu
45b10 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74  tex_alloc() rout
45b20 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
45b30 6e 65 77 0d 0a 2a 2a 20 6d 75 74 65 78 20 61 6e  new..** mutex an
45b40 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
45b50 74 65 72 20 74 6f 20 69 74 2e 20 5e 49 66 20 69  ter to it. ^If i
45b60 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0d 0a  t returns NULL..
45b70 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
45b80 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
45b90 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
45ba0 64 2e 20 20 5e 53 51 4c 69 74 65 0d 0a 2a 2a 20  d.  ^SQLite..** 
45bb0 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
45bc0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
45bd0 20 61 6e 20 65 72 72 6f 72 2e 20 20 5e 28 54 68   an error.  ^(Th
45be0 65 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 74  e argument..** t
45bf0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
45c00 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f  alloc() is one o
45c10 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
45c20 63 6f 6e 73 74 61 6e 74 73 3a 0d 0a 2a 2a 0d 0a  constants:..**..
45c30 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e  ** <ul>..** <li>
45c40 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
45c50 41 53 54 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  AST..** <li>  SQ
45c60 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
45c70 53 49 56 45 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53  SIVE..** <li>  S
45c80 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
45c90 49 43 5f 4d 41 53 54 45 52 0d 0a 2a 2a 20 3c 6c  IC_MASTER..** <l
45ca0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
45cb0 5f 53 54 41 54 49 43 5f 4d 45 4d 0d 0a 2a 2a 20  _STATIC_MEM..** 
45cc0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
45cd0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0d 0a  EX_STATIC_MEM2..
45ce0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
45cf0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e  MUTEX_STATIC_PRN
45d00 47 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  G..** <li>  SQLI
45d10 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
45d20 4c 52 55 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  LRU..** <li>  SQ
45d30 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
45d40 43 5f 4c 52 55 32 0d 0a 2a 2a 20 3c 2f 75 6c 3e  C_LRU2..** </ul>
45d50 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  )^..**..** ^The 
45d60 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
45d70 6e 74 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45  nts (SQLITE_MUTE
45d80 58 5f 46 41 53 54 20 61 6e 64 20 53 51 4c 49 54  X_FAST and SQLIT
45d90 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
45da0 45 29 0d 0a 2a 2a 20 63 61 75 73 65 20 73 71 6c  E)..** cause sql
45db0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
45dc0 28 29 20 74 6f 20 63 72 65 61 74 65 0d 0a 2a 2a  () to create..**
45dd0 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 5e   a new mutex.  ^
45de0 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73  The new mutex is
45df0 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20   recursive when 
45e00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
45e10 55 52 53 49 56 45 0d 0a 2a 2a 20 69 73 20 75 73  URSIVE..** is us
45e20 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73  ed but not neces
45e30 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53  sarily so when S
45e40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
45e50 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 20 54 68   is used...** Th
45e60 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
45e70 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
45e80 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
45e90 69 73 74 69 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 62  istinction..** b
45ea0 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55  etween SQLITE_MU
45eb0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e  TEX_RECURSIVE an
45ec0 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  d SQLITE_MUTEX_F
45ed0 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0d 0a  AST if it does..
45ee0 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
45ef0 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e   ^SQLite will on
45f00 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63  ly request a rec
45f10 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0d  ursive mutex in.
45f20 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
45f30 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
45f40 6f 6e 65 2e 20 20 5e 49 66 20 61 20 66 61 73 74  one.  ^If a fast
45f50 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65  er non-recursive
45f60 20 6d 75 74 65 78 0d 0a 2a 2a 20 69 6d 70 6c 65   mutex..** imple
45f70 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61  mentation is ava
45f80 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f  ilable on the ho
45f90 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65  st platform, the
45fa0 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
45fb0 0d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72  ..** might retur
45fc0 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69  n such a mutex i
45fd0 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51  n response to SQ
45fe0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e  LITE_MUTEX_FAST.
45ff0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 6f 74  ..**..** ^The ot
46000 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61  her allowed para
46010 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65  meters to sqlite
46020 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
46030 28 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 0d  (anything other.
46040 0a 2a 2a 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  .** than SQLITE_
46050 4d 55 54 45 58 5f 46 41 53 54 20 61 6e 64 20 53  MUTEX_FAST and S
46060 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
46070 52 53 49 56 45 29 20 65 61 63 68 20 72 65 74 75  RSIVE) each retu
46080 72 6e 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  rn..** a pointer
46090 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65   to a static pre
460a0 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20  existing mutex. 
460b0 20 5e 53 69 78 20 73 74 61 74 69 63 20 6d 75 74   ^Six static mut
460c0 65 78 65 73 20 61 72 65 0d 0a 2a 2a 20 75 73 65  exes are..** use
460d0 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  d by the current
460e0 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
460f0 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  te.  Future vers
46100 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0d 0a  ions of SQLite..
46110 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74  ** may add addit
46120 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74  ional static mut
46130 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75  exes.  Static mu
46140 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e  texes are for in
46150 74 65 72 6e 61 6c 0d 0a 2a 2a 20 75 73 65 20 62  ternal..** use b
46160 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
46170 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
46180 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
46190 65 78 65 73 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20  exes should..** 
461a0 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e  use only the dyn
461b0 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74  amic mutexes ret
461c0 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f  urned by SQLITE_
461d0 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0d 0a 2a  MUTEX_FAST or..*
461e0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
461f0 45 43 55 52 53 49 56 45 2e 0d 0a 2a 2a 0d 0a 2a  ECURSIVE...**..*
46200 2a 20 5e 4e 6f 74 65 20 74 68 61 74 20 69 66 20  * ^Note that if 
46210 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d  one of the dynam
46220 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74  ic mutex paramet
46230 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45  ers (SQLITE_MUTE
46240 58 5f 46 41 53 54 0d 0a 2a 2a 20 6f 72 20 53 51  X_FAST..** or SQ
46250 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
46260 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68  SIVE) is used th
46270 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  en sqlite3_mutex
46280 5f 61 6c 6c 6f 63 28 29 0d 0a 2a 2a 20 72 65 74  _alloc()..** ret
46290 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74  urns a different
462a0 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20   mutex on every 
462b0 63 61 6c 6c 2e 20 20 5e 42 75 74 20 66 6f 72 20  call.  ^But for 
462c0 74 68 65 20 73 74 61 74 69 63 0d 0a 2a 2a 20 6d  the static..** m
462d0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
462e0 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
462f0 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
46300 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0d 0a 2a  call that has..*
46310 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
46320 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  number...**..** 
46330 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ^The sqlite3_mut
46340 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e  ex_free() routin
46350 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
46360 70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 61  previously..** a
46370 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 63  llocated dynamic
46380 20 6d 75 74 65 78 2e 20 20 5e 53 51 4c 69 74 65   mutex.  ^SQLite
46390 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
463a0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0d  eallocate every.
463b0 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  .** dynamic mute
463c0 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61  x that it alloca
463d0 74 65 73 2e 20 20 54 68 65 20 64 79 6e 61 6d 69  tes.  The dynami
463e0 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e  c mutexes must n
463f0 6f 74 20 62 65 20 69 6e 0d 0a 2a 2a 20 75 73 65  ot be in..** use
46400 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 64   when they are d
46410 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 74 74  eallocated.  Att
46420 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c  empting to deall
46430 6f 63 61 74 65 20 61 20 73 74 61 74 69 63 0d 0a  ocate a static..
46440 2a 2a 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73  ** mutex results
46450 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65   in undefined be
46460 68 61 76 69 6f 72 2e 20 20 5e 53 51 4c 69 74 65  havior.  ^SQLite
46470 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
46480 65 73 0d 0a 2a 2a 20 61 20 73 74 61 74 69 63 20  es..** a static 
46490 6d 75 74 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  mutex...**..** ^
464a0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
464b0 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
464c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
464d0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
464e0 70 74 0d 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  pt..** to enter 
464f0 61 20 6d 75 74 65 78 2e 20 20 5e 49 66 20 61 6e  a mutex.  ^If an
46500 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
46510 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74  already within t
46520 68 65 20 6d 75 74 65 78 2c 0d 0a 2a 2a 20 73 71  he mutex,..** sq
46530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
46540 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61  r() will block a
46550 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
46560 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75  _try() will retu
46570 72 6e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  rn..** SQLITE_BU
46580 53 59 2e 20 20 5e 54 68 65 20 73 71 6c 69 74 65  SY.  ^The sqlite
46590 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e  3_mutex_try() in
465a0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
465b0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0d 0a 2a 2a 20  [SQLITE_OK]..** 
465c0 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20  upon successful 
465d0 65 6e 74 72 79 2e 20 20 5e 28 4d 75 74 65 78 65  entry.  ^(Mutexe
465e0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0d  s created using.
465f0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
46600 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62  _RECURSIVE can b
46610 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70  e entered multip
46620 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20  le times by the 
46630 73 61 6d 65 20 74 68 72 65 61 64 2e 0d 0a 2a 2a  same thread...**
46640 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
46650 68 65 2c 0d 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  he,..** mutex mu
46660 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
46670 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
46680 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
46690 74 68 65 72 20 74 68 72 65 61 64 0d 0a 2a 2a 20  ther thread..** 
466a0 63 61 6e 20 65 6e 74 65 72 2e 29 5e 20 20 5e 28  can enter.)^  ^(
466b0 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
466c0 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
466d0 72 20 61 6e 79 20 6f 74 68 65 72 0d 0a 2a 2a 20  r any other..** 
466e0 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f  kind of mutex mo
466f0 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68  re than once, th
46700 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
46710 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 20 53 51 4c  defined...** SQL
46720 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 65  ite will never e
46730 78 68 69 62 69 74 0d 0a 2a 2a 20 73 75 63 68 20  xhibit..** such 
46740 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20  behavior in its 
46750 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78  own use of mutex
46760 65 73 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28  es.)^..**..** ^(
46770 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f  Some systems (fo
46780 72 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f  r example, Windo
46790 77 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75  ws 95) do not su
467a0 70 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74  pport the operat
467b0 69 6f 6e 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  ion..** implemen
467c0 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d  ted by sqlite3_m
467d0 75 74 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20  utex_try().  On 
467e0 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73  those systems, s
467f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
46800 28 29 0d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61  ()..** will alwa
46810 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ys return SQLITE
46820 5f 42 55 53 59 2e 20 20 54 68 65 20 53 51 4c 69  _BUSY.  The SQLi
46830 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76 65  te core only eve
46840 72 20 75 73 65 73 0d 0a 2a 2a 20 73 71 6c 69 74  r uses..** sqlit
46850 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61  e3_mutex_try() a
46860 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
46870 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63  n so this is acc
46880 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72  eptable behavior
46890 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  .)^..**..** ^The
468a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
468b0 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
468c0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
468d0 74 20 77 61 73 0d 0a 2a 2a 20 70 72 65 76 69 6f  t was..** previo
468e0 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20  usly entered by 
468f0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
46900 20 20 20 5e 28 54 68 65 20 62 65 68 61 76 69 6f     ^(The behavio
46910 72 0d 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  r..** is undefin
46920 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
46930 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
46940 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 0d   entered by the.
46950 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 72 65  .** calling thre
46960 61 64 20 6f 72 20 69 73 20 6e 6f 74 20 63 75 72  ad or is not cur
46970 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
46980 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0d 0a  .  SQLite will..
46990 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68  ** never do eith
469a0 65 72 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49  er.)^..**..** ^I
469b0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  f the argument t
469c0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
469d0 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33  enter(), sqlite3
469e0 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72  _mutex_try(), or
469f0 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ..** sqlite3_mut
46a00 65 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20  ex_leave() is a 
46a10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68  NULL pointer, th
46a20 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75  en all three rou
46a30 74 69 6e 65 73 0d 0a 2a 2a 20 62 65 68 61 76 65  tines..** behave
46a40 20 61 73 20 6e 6f 2d 6f 70 73 2e 0d 0a 2a 2a 0d   as no-ops...**.
46a50 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
46a60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
46a70 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
46a80 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
46a90 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  )]...*/..SQLITE_
46aa0 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  API sqlite3_mute
46ab0 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  x *sqlite3_mutex
46ac0 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b 0d 0a 53 51  _alloc(int);..SQ
46ad0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
46ae0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
46af0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
46b00 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  ;..SQLITE_API vo
46b10 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  id sqlite3_mutex
46b20 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d  _enter(sqlite3_m
46b30 75 74 65 78 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  utex*);..SQLITE_
46b40 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
46b50 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
46b60 33 5f 6d 75 74 65 78 2a 29 3b 0d 0a 53 51 4c 49  3_mutex*);..SQLI
46b70 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
46b80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
46b90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
46ba0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33  ..../*..** CAPI3
46bb0 52 45 46 3a 20 4d 75 74 65 78 20 4d 65 74 68 6f  REF: Mutex Metho
46bc0 64 73 20 4f 62 6a 65 63 74 0d 0a 2a 2a 0d 0a 2a  ds Object..**..*
46bd0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
46be0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
46bf0 64 65 66 69 6e 65 73 20 74 68 65 20 6c 6f 77 2d  defines the low-
46c00 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 0d 0a  level routines..
46c10 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  ** used to alloc
46c20 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75 74 65  ate and use mute
46c30 78 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 55 73 75  xes...**..** Usu
46c40 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c  ally, the defaul
46c50 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  t mutex implemen
46c60 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64  tations provided
46c70 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0d 0a   by SQLite are..
46c80 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68  ** sufficient, h
46c90 6f 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20  owever the user 
46ca0 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f  has the option o
46cb0 66 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61  f substituting a
46cc0 20 63 75 73 74 6f 6d 0d 0a 2a 2a 20 69 6d 70 6c   custom..** impl
46cd0 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73  ementation for s
46ce0 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f  pecialized deplo
46cf0 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d  yments or system
46d00 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69  s for which SQLi
46d10 74 65 0d 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  te..** does not 
46d20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62  provide a suitab
46d30 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
46d40 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  n. In this case,
46d50 20 74 68 65 20 75 73 65 72 0d 0a 2a 2a 20 63 72   the user..** cr
46d60 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61  eates and popula
46d70 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
46d80 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
46d90 65 20 74 6f 20 70 61 73 73 0d 0a 2a 2a 20 74 6f  e to pass..** to
46da0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
46db0 29 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  ) along with the
46dc0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
46dd0 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0d 0a  MUTEX] option...
46de0 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  ** Additionally,
46df0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
46e00 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
46e10 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 6e  an be used as an
46e20 0d 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69  ..** output vari
46e30 61 62 6c 65 20 77 68 65 6e 20 71 75 65 72 79 69  able when queryi
46e40 6e 67 20 74 68 65 20 73 79 73 74 65 6d 20 66 6f  ng the system fo
46e50 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75  r the current mu
46e60 74 65 78 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  tex..** implemen
46e70 74 61 74 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68  tation, using th
46e80 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  e [SQLITE_CONFIG
46e90 5f 47 45 54 4d 55 54 45 58 5d 20 6f 70 74 69 6f  _GETMUTEX] optio
46ea0 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  n...**..** ^The 
46eb0 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f  xMutexInit metho
46ec0 64 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69  d defined by thi
46ed0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69  s structure is i
46ee0 6e 76 6f 6b 65 64 20 61 73 0d 0a 2a 2a 20 70 61  nvoked as..** pa
46ef0 72 74 20 6f 66 20 73 79 73 74 65 6d 20 69 6e 69  rt of system ini
46f00 74 69 61 6c 69 7a 61 74 69 6f 6e 20 62 79 20 74  tialization by t
46f10 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  he sqlite3_initi
46f20 61 6c 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  alize() function
46f30 2e 0d 0a 2a 2a 20 5e 54 68 65 20 78 4d 75 74 65  ...** ^The xMute
46f40 78 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 69 73  xInit routine is
46f50 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74   called by SQLit
46f60 65 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66  e exactly once f
46f70 6f 72 20 65 61 63 68 0d 0a 2a 2a 20 65 66 66 65  or each..** effe
46f80 63 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 5b 73  ctive call to [s
46f90 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
46fa0 65 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  e()]...**..** ^T
46fb0 68 65 20 78 4d 75 74 65 78 45 6e 64 20 6d 65 74  he xMutexEnd met
46fc0 68 6f 64 20 64 65 66 69 6e 65 64 20 62 79 20 74  hod defined by t
46fd0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
46fe0 20 69 6e 76 6f 6b 65 64 20 61 73 0d 0a 2a 2a 20   invoked as..** 
46ff0 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d 20 73  part of system s
47000 68 75 74 64 6f 77 6e 20 62 79 20 74 68 65 20 73  hutdown by the s
47010 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
47020 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 0d  ) function. The.
47030 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
47040 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 68 6f  on of this metho
47050 64 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f  d is expected to
47060 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6f 75 74   release all out
47070 73 74 61 6e 64 69 6e 67 0d 0a 2a 2a 20 72 65 73  standing..** res
47080 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 64 20  ources obtained 
47090 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74  by the mutex met
470a0 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  hods implementat
470b0 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 0d  ion, especially.
470c0 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 6e  .** those obtain
470d0 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 78  ed by the xMutex
470e0 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 20 5e 54  Init method.  ^T
470f0 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 0d 0a  he xMutexEnd()..
47100 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
47110 69 6e 76 6f 6b 65 64 20 65 78 61 63 74 6c 79 20  invoked exactly 
47120 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 61  once for each ca
47130 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  ll to [sqlite3_s
47140 68 75 74 64 6f 77 6e 28 29 5d 2e 0d 0a 2a 2a 0d  hutdown()]...**.
47150 0a 2a 2a 20 5e 28 54 68 65 20 72 65 6d 61 69 6e  .** ^(The remain
47160 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f 64  ing seven method
47170 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69  s defined by thi
47180 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d 75  s structure (xMu
47190 74 65 78 41 6c 6c 6f 63 2c 0d 0a 2a 2a 20 78 4d  texAlloc,..** xM
471a0 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78  utexFree, xMutex
471b0 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79  Enter, xMutexTry
471c0 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78  , xMutexLeave, x
471d0 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0d 0a 2a  MutexHeld and..*
471e0 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29  * xMutexNotheld)
471f0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66   implement the f
47200 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61  ollowing interfa
47210 63 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c  ces (respectivel
47220 79 29 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e  y):..**..** <ul>
47230 0d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  ..**   <li>  [sq
47240 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
47250 63 28 29 5d 20 3c 2f 6c 69 3e 0d 0a 2a 2a 20 20  c()] </li>..**  
47260 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
47270 6d 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f  mutex_free()] </
47280 6c 69 3e 0d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20  li>..**   <li>  
47290 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
472a0 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 3e 0d 0a 2a  nter()] </li>..*
472b0 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
472c0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 5d 20  e3_mutex_try()] 
472d0 3c 2f 6c 69 3e 0d 0a 2a 2a 20 20 20 3c 6c 69 3e  </li>..**   <li>
472e0 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78    [sqlite3_mutex
472f0 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c 69 3e 0d  _leave()] </li>.
47300 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
47310 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
47320 29 5d 20 3c 2f 6c 69 3e 0d 0a 2a 2a 20 20 20 3c  )] </li>..**   <
47330 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
47340 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 3c  tex_notheld()] <
47350 2f 6c 69 3e 0d 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e  /li>..** </ul>)^
47360 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f 6e 6c  ..**..** The onl
47370 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20  y difference is 
47380 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63 20  that the public 
47390 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e 63  sqlite3_XXX func
473a0 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 64  tions enumerated
473b0 0d 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e  ..** above silen
473c0 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69  tly ignore any i
473d0 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20  nvocations that 
473e0 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  pass a NULL poin
473f0 74 65 72 20 69 6e 73 74 65 61 64 0d 0a 2a 2a 20  ter instead..** 
47400 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78  of a valid mutex
47410 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70   handle. The imp
47420 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
47430 74 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69  the methods defi
47440 6e 65 64 0d 0a 2a 2a 20 62 79 20 74 68 69 73 20  ned..** by this 
47450 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f  structure are no
47460 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 68 61  t required to ha
47470 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20  ndle this case, 
47480 74 68 65 20 72 65 73 75 6c 74 73 0d 0a 2a 2a 20  the results..** 
47490 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 4c  of passing a NUL
474a0 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61  L pointer instea
474b0 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74  d of a valid mut
474c0 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 6e  ex handle are un
474d0 64 65 66 69 6e 65 64 0d 0a 2a 2a 20 28 69 2e 65  defined..** (i.e
474e0 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62  . it is acceptab
474f0 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  le to provide an
47500 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
47510 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69  that segfaults i
47520 66 0d 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73  f..** it is pass
47530 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ed a NULL pointe
47540 72 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  r)...**..** The 
47550 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 65 74  xMutexInit() met
47560 68 6f 64 20 6d 75 73 74 20 62 65 20 74 68 72 65  hod must be thre
47570 61 64 73 61 66 65 2e 20 20 5e 49 74 20 6d 75 73  adsafe.  ^It mus
47580 74 20 62 65 20 68 61 72 6d 6c 65 73 73 20 74 6f  t be harmless to
47590 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 20 78 4d 75 74  ..** invoke xMut
475a0 65 78 49 6e 69 74 28 29 20 6d 75 6c 74 69 70 6c  exInit() multipl
475b0 65 20 74 69 6d 65 73 20 77 69 74 68 69 6e 20 74  e times within t
475c0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  he same process 
475d0 61 6e 64 20 77 69 74 68 6f 75 74 0d 0a 2a 2a 20  and without..** 
475e0 69 6e 74 65 72 76 65 6e 69 6e 67 20 63 61 6c 6c  intervening call
475f0 73 20 74 6f 20 78 4d 75 74 65 78 45 6e 64 28 29  s to xMutexEnd()
47600 2e 20 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75  .  Second and su
47610 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
47620 6f 0d 0a 2a 2a 20 78 4d 75 74 65 78 49 6e 69 74  o..** xMutexInit
47630 28 29 20 6d 75 73 74 20 62 65 20 6e 6f 2d 6f 70  () must be no-op
47640 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 78 4d 75 74  s...**..** ^xMut
47650 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e 6f  exInit() must no
47660 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d  t use SQLite mem
47670 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28  ory allocation (
47680 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
47690 29 5d 0d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61  )]..** and its a
476a0 73 73 6f 63 69 61 74 65 73 29 2e 20 20 5e 53 69  ssociates).  ^Si
476b0 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41  milarly, xMutexA
476c0 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20  lloc() must not 
476d0 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72  use SQLite memor
476e0 79 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  y..** allocation
476f0 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75   for a static mu
47700 74 65 78 2e 20 20 5e 48 6f 77 65 76 65 72 20 78  tex.  ^However x
47710 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79  MutexAlloc() may
47720 20 75 73 65 20 53 51 4c 69 74 65 0d 0a 2a 2a 20   use SQLite..** 
47730 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
47740 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20  n for a fast or 
47750 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e  recursive mutex.
47760 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51 4c 69 74 65  ..**..** ^SQLite
47770 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65   will invoke the
47780 20 78 4d 75 74 65 78 45 6e 64 28 29 20 6d 65 74   xMutexEnd() met
47790 68 6f 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65  hod when [sqlite
477a0 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 69 73  3_shutdown()] is
477b0 0d 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 62 75 74  ..** called, but
477c0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 70 72 69   only if the pri
477d0 6f 72 20 63 61 6c 6c 20 74 6f 20 78 4d 75 74 65  or call to xMute
477e0 78 49 6e 69 74 20 72 65 74 75 72 6e 65 64 20 53  xInit returned S
477f0 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 20 49 66  QLITE_OK...** If
47800 20 78 4d 75 74 65 78 49 6e 69 74 20 66 61 69 6c   xMutexInit fail
47810 73 20 69 6e 20 61 6e 79 20 77 61 79 2c 20 69 74  s in any way, it
47820 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f 20   is expected to 
47830 63 6c 65 61 6e 20 75 70 20 61 66 74 65 72 20 69  clean up after i
47840 74 73 65 6c 66 0d 0a 2a 2a 20 70 72 69 6f 72 20  tself..** prior 
47850 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a  to returning...*
47860 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
47870 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
47880 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f  methods sqlite3_
47890 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 3b 0d 0a  mutex_methods;..
478a0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
478b0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b 0d 0a  utex_methods {..
478c0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49 6e    int (*xMutexIn
478d0 69 74 29 28 76 6f 69 64 29 3b 0d 0a 20 20 69 6e  it)(void);..  in
478e0 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76  t (*xMutexEnd)(v
478f0 6f 69 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  oid);..  sqlite3
47900 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78  _mutex *(*xMutex
47910 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0d 0a 20 20  Alloc)(int);..  
47920 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65  void (*xMutexFre
47930 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e)(sqlite3_mutex
47940 20 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78   *);..  void (*x
47950 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 6c 69  MutexEnter)(sqli
47960 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0d 0a 20  te3_mutex *);.. 
47970 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 79   int (*xMutexTry
47980 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
47990 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78 4d  *);..  void (*xM
479a0 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74  utexLeave)(sqlit
479b0 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0d 0a 20 20  e3_mutex *);..  
479c0 69 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64  int (*xMutexHeld
479d0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
479e0 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 4d 75  *);..  int (*xMu
479f0 74 65 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69  texNotheld)(sqli
47a00 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0d 0a 7d  te3_mutex *);..}
47a10 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
47a20 33 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 69  3REF: Mutex Veri
47a30 66 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65  fication Routine
47a40 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71  s..**..** The sq
47a50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
47a60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
47a70 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
47a80 6f 75 74 69 6e 65 73 0d 0a 2a 2a 20 61 72 65 20  outines..** are 
47a90 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
47aa0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
47ab0 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 5e 54   statements.  ^T
47ac0 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0d 0a  he SQLite core..
47ad0 2a 2a 20 6e 65 76 65 72 20 75 73 65 73 20 74 68  ** never uses th
47ae0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63  ese routines exc
47af0 65 70 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73  ept inside an as
47b00 73 65 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69  sert() and appli
47b10 63 61 74 69 6f 6e 73 0d 0a 2a 2a 20 61 72 65 20  cations..** are 
47b20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f  advised to follo
47b30 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20 74 68  w the lead of th
47b40 65 20 63 6f 72 65 2e 20 20 5e 54 68 65 20 53 51  e core.  ^The SQ
47b50 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 0d 0a  Lite core only..
47b60 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d 70 6c  ** provides impl
47b70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20  ementations for 
47b80 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77  these routines w
47b90 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c  hen it is compil
47ba0 65 64 0d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ed..** with the 
47bb0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c 61  SQLITE_DEBUG fla
47bc0 67 2e 20 20 5e 45 78 74 65 72 6e 61 6c 20 6d 75  g.  ^External mu
47bd0 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
47be0 6f 6e 73 0d 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79  ons..** are only
47bf0 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f   required to pro
47c00 76 69 64 65 20 74 68 65 73 65 20 72 6f 75 74 69  vide these routi
47c10 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44 45  nes if SQLITE_DE
47c20 42 55 47 20 69 73 0d 0a 2a 2a 20 64 65 66 69 6e  BUG is..** defin
47c30 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47  ed and if NDEBUG
47c40 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
47c50 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 73 65 20  ..**..** ^These 
47c60 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20  routines should 
47c70 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  return true if t
47c80 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69  he mutex in thei
47c90 72 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 69  r argument..** i
47ca0 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65  s held or not he
47cb0 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  ld, respectively
47cc0 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  , by the calling
47cd0 20 74 68 72 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a 2a   thread...**..**
47ce0 20 5e 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61   ^The implementa
47cf0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75  tion is not requ
47d00 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20  ired to provide 
47d10 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73  versions of thes
47d20 65 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74  e..** routines t
47d30 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77 6f 72  hat actually wor
47d40 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c 65 6d  k. If the implem
47d50 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
47d60 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e  t provide workin
47d70 67 0d 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f  g..** versions o
47d80 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
47d90 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 6c  , it should at l
47da0 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 75  east provide stu
47db0 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0d 0a  bs that always..
47dc0 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73  ** return true s
47dd0 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20  o that one does 
47de0 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73  not get spurious
47df0 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75   assertion failu
47e00 72 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66  res...**..** ^If
47e10 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
47e20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
47e30 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c 4c 20  eld() is a NULL 
47e40 70 6f 69 6e 74 65 72 20 74 68 65 6e 0d 0a 2a 2a  pointer then..**
47e50 20 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 6f   the routine sho
47e60 75 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 20  uld return 1.   
47e70 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74  This seems count
47e80 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e  er-intuitive sin
47e90 63 65 0d 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74  ce..** clearly t
47ea0 68 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20  he mutex cannot 
47eb0 62 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f  be held if it do
47ec0 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42  es not exist.  B
47ed0 75 74 0d 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f  ut..** the reaso
47ee0 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 73  n the mutex does
47ef0 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 65   not exist is be
47f00 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 20  cause the build 
47f10 69 73 20 6e 6f 74 0d 0a 2a 2a 20 75 73 69 6e 67  is not..** using
47f20 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77   mutexes.  And w
47f30 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68  e do not want th
47f40 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61  e assert() conta
47f50 69 6e 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 63 61  ining the..** ca
47f60 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ll to sqlite3_mu
47f70 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61  tex_held() to fa
47f80 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72  il, so a non-zer
47f90 6f 20 72 65 74 75 72 6e 20 69 73 0d 0a 2a 2a 20  o return is..** 
47fa0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
47fb0 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 5e 54  thing to do.  ^T
47fc0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
47fd0 5f 6e 6f 74 68 65 6c 64 28 29 0d 0a 2a 2a 20 69  _notheld()..** i
47fe0 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20  nterface should 
47ff0 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68  also return 1 wh
48000 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20  en given a NULL 
48010 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a 23 69  pointer...*/..#i
48020 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 53 51  fndef NDEBUG..SQ
48030 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
48040 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
48050 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
48060 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
48070 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
48080 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
48090 75 74 65 78 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d  utex*);..#endif.
480a0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52  .../*..** CAPI3R
480b0 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65 73 0d  EF: Mutex Types.
480c0 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  .**..** The [sql
480d0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
480e0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 61  ()] interface ta
480f0 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
48100 75 6d 65 6e 74 0d 0a 2a 2a 20 77 68 69 63 68 20  ument..** which 
48110 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  is one of these 
48120 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
48130 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73  s...**..** The s
48140 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74  et of static mut
48150 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20  exes may change 
48160 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20  from one SQLite 
48170 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0d 0a  release to the..
48180 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63  ** next.  Applic
48190 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72  ations that over
481a0 72 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69  ride the built-i
481b0 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75  n mutex logic mu
481c0 73 74 20 62 65 0d 0a 2a 2a 20 70 72 65 70 61 72  st be..** prepar
481d0 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74  ed to accommodat
481e0 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61  e additional sta
481f0 74 69 63 20 6d 75 74 65 78 65 73 2e 0d 0a 2a 2f  tic mutexes...*/
48200 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
48210 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
48220 20 20 20 20 20 20 20 20 30 0d 0a 23 64 65 66 69          0..#defi
48230 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
48240 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20  RECURSIVE       
48250 20 31 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   1..#define SQLI
48260 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
48270 4d 41 53 54 45 52 20 20 20 20 32 0d 0a 23 64 65  MASTER    2..#de
48280 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
48290 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20  X_STATIC_MEM    
482a0 20 20 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33     3  /* sqlite3
482b0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0d 0a 23 64  _malloc() */..#d
482c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
482d0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 20  EX_STATIC_MEM2  
482e0 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 20 55 53      4  /* NOT US
482f0 45 44 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ED */..#define S
48300 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
48310 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 20 20  IC_OPEN      4  
48320 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f  /* sqlite3BtreeO
48330 70 65 6e 28 29 20 2a 2f 0d 0a 23 64 65 66 69 6e  pen() */..#defin
48340 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
48350 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
48360 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 61  5  /* sqlite3_ra
48370 6e 64 6f 6d 28 29 20 2a 2f 0d 0a 23 64 65 66 69  ndom() */..#defi
48380 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
48390 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20  STATIC_LRU      
483a0 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20   6  /* lru page 
483b0 6c 69 73 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  list */..#define
483c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
483d0 41 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37  ATIC_LRU2      7
483e0 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f    /* NOT USED */
483f0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
48400 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 4d  _MUTEX_STATIC_PM
48410 45 4d 20 20 20 20 20 20 37 20 20 2f 2a 20 73 71  EM      7  /* sq
48420 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
48430 29 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ) */..../*..** C
48440 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76  API3REF: Retriev
48450 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f 72 20  e the mutex for 
48460 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
48470 63 74 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  ction..**..** ^T
48480 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65  his interface re
48490 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
484a0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
484b0 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  ex] object that 
484c0 0d 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20  ..** serializes 
484d0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64  access to the [d
484e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
484f0 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65  on] given in the
48500 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 77 68   argument..** wh
48510 65 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e  en the [threadin
48520 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61  g mode] is Seria
48530 6c 69 7a 65 64 2e 0d 0a 2a 2a 20 5e 49 66 20 74  lized...** ^If t
48540 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  he [threading mo
48550 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 68  de] is Single-th
48560 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68  read or Multi-th
48570 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0d 0a  read then this..
48580 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ** routine retur
48590 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ns a NULL pointe
485a0 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  r...*/..SQLITE_A
485b0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
485c0 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74   *sqlite3_db_mut
485d0 65 78 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 0d  ex(sqlite3*);...
485e0 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
485f0 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74  : Low-Level Cont
48600 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 65 20  rol Of Database 
48610 46 69 6c 65 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54  Files..**..** ^T
48620 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  he [sqlite3_file
48630 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65  _control()] inte
48640 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69  rface makes a di
48650 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65  rect call to the
48660 0d 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f  ..** xFileContro
48670 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65  l method for the
48680 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
48690 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73  hods] object ass
486a0 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68  ociated..** with
486b0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
486c0 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 65  tabase identifie
486d0 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20  d by the second 
486e0 61 72 67 75 6d 65 6e 74 2e 20 5e 54 68 65 0d 0a  argument. ^The..
486f0 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ** name of the d
48700 61 74 61 62 61 73 65 20 69 73 20 22 6d 61 69 6e  atabase is "main
48710 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  " for the main d
48720 61 74 61 62 61 73 65 20 6f 72 20 22 74 65 6d 70  atabase or "temp
48730 22 20 66 6f 72 20 74 68 65 0d 0a 2a 2a 20 54 45  " for the..** TE
48740 4d 50 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20  MP database, or 
48750 74 68 65 20 6e 61 6d 65 20 74 68 61 74 20 61 70  the name that ap
48760 70 65 61 72 73 20 61 66 74 65 72 20 74 68 65 20  pears after the 
48770 41 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 0d 0a  AS keyword for..
48780 2a 2a 20 64 61 74 61 62 61 73 65 73 20 74 68 61  ** databases tha
48790 74 20 61 72 65 20 61 64 64 65 64 20 75 73 69 6e  t are added usin
487a0 67 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 53  g the [ATTACH] S
487b0 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0d 0a 2a 2a 20  QL command...** 
487c0 5e 41 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20  ^A NULL pointer 
487d0 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 70  can be used in p
487e0 6c 61 63 65 20 6f 66 20 22 6d 61 69 6e 22 20 74  lace of "main" t
487f0 6f 20 72 65 66 65 72 20 74 6f 20 74 68 65 0d 0a  o refer to the..
48800 2a 2a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  ** main database
48810 20 66 69 6c 65 2e 0d 0a 2a 2a 20 5e 54 68 65 20   file...** ^The 
48820 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68  third and fourth
48830 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
48840 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20  his routine..** 
48850 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63  are passed direc
48860 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74  tly through to t
48870 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68  he second and th
48880 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f  ird parameters o
48890 66 0d 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43  f..** the xFileC
488a0 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20  ontrol method.  
488b0 5e 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75  ^The return valu
488c0 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f  e of the xFileCo
488d0 6e 74 72 6f 6c 0d 0a 2a 2a 20 6d 65 74 68 6f 64  ntrol..** method
488e0 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74   becomes the ret
488f0 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69  urn value of thi
48900 73 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 0d 0a  s routine...**..
48910 2a 2a 20 5e 54 68 65 20 53 51 4c 49 54 45 5f 46  ** ^The SQLITE_F
48920 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  CNTL_FILE_POINTE
48930 52 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  R value for the 
48940 6f 70 20 70 61 72 61 6d 65 74 65 72 20 63 61 75  op parameter cau
48950 73 65 73 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ses..** a pointe
48960 72 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  r to the underly
48970 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  ing [sqlite3_fil
48980 65 5d 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  e] object to be 
48990 77 72 69 74 74 65 6e 20 69 6e 74 6f 0d 0a 2a 2a  written into..**
489a0 20 74 68 65 20 73 70 61 63 65 20 70 6f 69 6e 74   the space point
489b0 65 64 20 74 6f 20 62 79 20 74 68 65 20 34 74 68  ed to by the 4th
489c0 20 70 61 72 61 6d 65 74 65 72 2e 20 20 5e 54 68   parameter.  ^Th
489d0 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 46  e SQLITE_FCNTL_F
489e0 49 4c 45 5f 50 4f 49 4e 54 45 52 0d 0a 2a 2a 20  ILE_POINTER..** 
489f0 63 61 73 65 20 69 73 20 61 20 73 68 6f 72 74 2d  case is a short-
48a00 63 69 72 63 75 69 74 20 70 61 74 68 20 77 68 69  circuit path whi
48a10 63 68 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75  ch does not actu
48a20 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 74 68 65 0d  ally invoke the.
48a30 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 73  .** underlying s
48a40 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
48a50 73 2e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  s.xFileControl m
48a60 65 74 68 6f 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ethod...**..** ^
48a70 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  If the second pa
48a80 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65  rameter (zDbName
48a90 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  ) does not match
48aa0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79   the name of any
48ab0 0d 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61  ..** open databa
48ac0 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
48ad0 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65  LITE_ERROR is re
48ae0 74 75 72 6e 65 64 2e 20 20 5e 54 68 69 73 20 65  turned.  ^This e
48af0 72 72 6f 72 0d 0a 2a 2a 20 63 6f 64 65 20 69 73  rror..** code is
48b00 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20   not remembered 
48b10 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  and will not be 
48b20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c  recalled by [sql
48b30 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0d  ite3_errcode()].
48b40 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  .** or [sqlite3_
48b50 65 72 72 6d 73 67 28 29 5d 2e 20 20 54 68 65 20  errmsg()].  The 
48b60 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 6c 65  underlying xFile
48b70 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d  Control method m
48b80 69 67 68 74 0d 0a 2a 2a 20 61 6c 73 6f 20 72 65  ight..** also re
48b90 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
48ba0 52 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20  R.  There is no 
48bb0 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69  way to distingui
48bc0 73 68 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 61  sh between..** a
48bd0 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e  n incorrect zDbN
48be0 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54  ame and an SQLIT
48bf0 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66  E_ERROR return f
48c00 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69  rom the underlyi
48c10 6e 67 0d 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74  ng..** xFileCont
48c20 72 6f 6c 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2a 0d  rol method...**.
48c30 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53  .** See also: [S
48c40 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
48c50 53 54 41 54 45 5d 0d 0a 2a 2f 0d 0a 53 51 4c 49  STATE]..*/..SQLI
48c60 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
48c70 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
48c80 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
48c90 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69  char *zDbName, i
48ca0 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0d 0a  nt op, void*);..
48cb0 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
48cc0 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72  F: Testing Inter
48cd0 66 61 63 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  face..**..** ^Th
48ce0 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
48cf0 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61  ontrol() interfa
48d00 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
48d10 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0d  ad out internal.
48d20 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c  .** state of SQL
48d30 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63  ite and to injec
48d40 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51  t faults into SQ
48d50 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67  Lite for testing
48d60 0d 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20  ..** purposes.  
48d70 5e 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d  ^The first param
48d80 65 74 65 72 20 69 73 20 61 6e 20 6f 70 65 72 61  eter is an opera
48d90 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 64  tion code that d
48da0 65 74 65 72 6d 69 6e 65 73 0d 0a 2a 2a 20 74 68  etermines..** th
48db0 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e  e number, meanin
48dc0 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e  g, and operation
48dd0 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 75 65   of all subseque
48de0 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0d 0a  nt parameters...
48df0 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  **..** This inte
48e00 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72  rface is not for
48e10 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 74   use by applicat
48e20 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73  ions.  It exists
48e30 20 73 6f 6c 65 6c 79 0d 0a 2a 2a 20 66 6f 72 20   solely..** for 
48e40 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f  verifying the co
48e50 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
48e60 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  of the SQLite li
48e70 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e  brary.  Dependin
48e80 67 0d 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65  g..** on how the
48e90 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
48ea0 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69  is compiled, thi
48eb0 73 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68  s interface migh
48ec0 74 20 6e 6f 74 20 65 78 69 73 74 2e 0d 0a 2a 2a  t not exist...**
48ed0 0d 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73  ..** The details
48ee0 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f   of the operatio
48ef0 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72 20 6d  n codes, their m
48f00 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 72  eanings, the par
48f10 61 6d 65 74 65 72 73 0d 0a 2a 2a 20 74 68 65 79  ameters..** they
48f20 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 20   take, and what 
48f30 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c 20  they do are all 
48f40 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
48f50 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65  e without notice
48f60 2e 0d 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73  ...** Unlike mos
48f70 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  t of the SQLite 
48f80 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69  API, this functi
48f90 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e  on is not guaran
48fa0 74 65 65 64 20 74 6f 0d 0a 2a 2a 20 6f 70 65 72  teed to..** oper
48fb0 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79  ate consistently
48fc0 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73   from one releas
48fd0 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0d 0a  e to the next...
48fe0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
48ff0 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  nt sqlite3_test_
49000 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20  control(int op, 
49010 2e 2e 2e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...);..../*..** 
49020 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e  CAPI3REF: Testin
49030 67 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72  g Interface Oper
49040 61 74 69 6f 6e 20 43 6f 64 65 73 0d 0a 2a 2a 0d  ation Codes..**.
49050 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
49060 6e 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69  nts are the vali
49070 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65  d operation code
49080 20 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64   parameters used
49090 0d 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73  ..** as the firs
490a0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73  t argument to [s
490b0 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
490c0 72 6f 6c 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  rol()]...**..** 
490d0 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73  These parameters
490e0 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e 69   and their meani
490f0 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 74 20  ngs are subject 
49100 74 6f 20 63 68 61 6e 67 65 0d 0a 2a 2a 20 77 69  to change..** wi
49110 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54  thout notice.  T
49120 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 20  hese values are 
49130 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  for testing purp
49140 6f 73 65 73 20 6f 6e 6c 79 2e 0d 0a 2a 2a 20 41  oses only...** A
49150 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75  pplications shou
49160 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f  ld not use any o
49170 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65  f these paramete
49180 72 73 20 6f 72 20 74 68 65 0d 0a 2a 2a 20 5b 73  rs or the..** [s
49190 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
491a0 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65  rol()] interface
491b0 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ...*/..#define S
491c0 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46  QLITE_TESTCTRL_F
491d0 49 52 53 54 20 20 20 20 20 20 20 20 20 20 20 20  IRST            
491e0 20 20 20 20 20 20 20 20 35 0d 0a 23 64 65 66 69          5..#defi
491f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
49200 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20  RL_PRNG_SAVE    
49210 20 20 20 20 20 20 20 20 20 20 20 20 35 0d 0a 23              5..#
49220 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
49230 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54  STCTRL_PRNG_REST
49240 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
49250 36 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  6..#define SQLIT
49260 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
49270 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20  RESET           
49280 20 20 20 20 37 0d 0a 23 64 65 66 69 6e 65 20 53      7..#define S
49290 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42  QLITE_TESTCTRL_B
492a0 49 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20  ITVEC_TEST      
492b0 20 20 20 20 20 20 20 20 38 0d 0a 23 64 65 66 69          8..#defi
492c0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
492d0 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c  RL_FAULT_INSTALL
492e0 20 20 20 20 20 20 20 20 20 20 20 20 39 0d 0a 23              9..#
492f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
49300 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41  STCTRL_BENIGN_MA
49310 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31  LLOC_HOOKS     1
49320 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  0..#define SQLIT
49330 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49  E_TESTCTRL_PENDI
49340 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 20  NG_BYTE         
49350 20 20 20 31 31 0d 0a 23 64 65 66 69 6e 65 20 53     11..#define S
49360 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
49370 53 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  SSERT           
49380 20 20 20 20 20 20 20 31 32 0d 0a 23 64 65 66 69         12..#defi
49390 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
493a0 52 4c 5f 41 4c 57 41 59 53 20 20 20 20 20 20 20  RL_ALWAYS       
493b0 20 20 20 20 20 20 20 20 20 20 20 31 33 0d 0a 23             13..#
493c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
493d0 53 54 43 54 52 4c 5f 52 45 53 45 52 56 45 20 20  STCTRL_RESERVE  
493e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
493f0 34 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  4..#define SQLIT
49400 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49 4d  E_TESTCTRL_OPTIM
49410 49 5a 41 54 49 4f 4e 53 20 20 20 20 20 20 20 20  IZATIONS        
49420 20 20 20 31 35 0d 0a 23 64 65 66 69 6e 65 20 53     15..#define S
49430 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 49  QLITE_TESTCTRL_I
49440 53 4b 45 59 57 4f 52 44 20 20 20 20 20 20 20 20  SKEYWORD        
49450 20 20 20 20 20 20 20 31 36 0d 0a 23 64 65 66 69         16..#defi
49460 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
49470 52 4c 5f 53 43 52 41 54 43 48 4d 41 4c 4c 4f 43  RL_SCRATCHMALLOC
49480 20 20 20 20 20 20 20 20 20 20 20 31 37 0d 0a 23             17..#
49490 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
494a0 53 54 43 54 52 4c 5f 4c 4f 43 41 4c 54 49 4d 45  STCTRL_LOCALTIME
494b0 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 31  _FAULT         1
494c0 38 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  8..#define SQLIT
494d0 45 5f 54 45 53 54 43 54 52 4c 5f 45 58 50 4c 41  E_TESTCTRL_EXPLA
494e0 49 4e 5f 53 54 4d 54 20 20 20 20 20 20 20 20 20  IN_STMT         
494f0 20 20 20 31 39 0d 0a 23 64 65 66 69 6e 65 20 53     19..#define S
49500 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 4c  QLITE_TESTCTRL_L
49510 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20  AST             
49520 20 20 20 20 20 20 20 31 39 0d 0a 0d 0a 2f 2a 0d         19..../*.
49530 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
49540 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61  Lite Runtime Sta
49550 74 75 73 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 69  tus..**..** ^Thi
49560 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75  s interface is u
49570 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20  sed to retrieve 
49580 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69  runtime status i
49590 6e 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 61  nformation..** a
495a0 62 6f 75 74 20 74 68 65 20 70 65 72 66 6f 72 6d  bout the perform
495b0 61 6e 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20  ance of SQLite, 
495c0 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74  and optionally t
495d0 6f 20 72 65 73 65 74 20 76 61 72 69 6f 75 73 0d  o reset various.
495e0 0a 2a 2a 20 68 69 67 68 77 61 74 65 72 20 6d 61  .** highwater ma
495f0 72 6b 73 2e 20 20 5e 54 68 65 20 66 69 72 73 74  rks.  ^The first
49600 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
49610 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72  integer code for
49620 0d 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69  ..** the specifi
49630 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 6d  c parameter to m
49640 65 61 73 75 72 65 2e 20 20 5e 28 52 65 63 6f 67  easure.  ^(Recog
49650 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 20 63 6f  nized integer co
49660 64 65 73 0d 0a 2a 2a 20 61 72 65 20 6f 66 20 74  des..** are of t
49670 68 65 20 66 6f 72 6d 20 5b 73 74 61 74 75 73 20  he form [status 
49680 70 61 72 61 6d 65 74 65 72 73 20 7c 20 53 51 4c  parameters | SQL
49690 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e  ITE_STATUS_...].
496a0 29 5e 0d 0a 2a 2a 20 5e 54 68 65 20 63 75 72 72  )^..** ^The curr
496b0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
496c0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65   parameter is re
496d0 74 75 72 6e 65 64 20 69 6e 74 6f 20 2a 70 43 75  turned into *pCu
496e0 72 72 65 6e 74 2e 0d 0a 2a 2a 20 5e 54 68 65 20  rrent...** ^The 
496f0 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 65 64  highest recorded
49700 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e   value is return
49710 65 64 20 69 6e 20 2a 70 48 69 67 68 77 61 74 65  ed in *pHighwate
49720 72 2e 20 20 5e 49 66 20 74 68 65 0d 0a 2a 2a 20  r.  ^If the..** 
49730 72 65 73 65 74 46 6c 61 67 20 69 73 20 74 72 75  resetFlag is tru
49740 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68  e, then the high
49750 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c 75 65  est record value
49760 20 69 73 20 72 65 73 65 74 20 61 66 74 65 72 0d   is reset after.
49770 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20  .** *pHighwater 
49780 69 73 20 77 72 69 74 74 65 6e 2e 20 20 5e 28 53  is written.  ^(S
49790 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 64  ome parameters d
497a0 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65  o not record the
497b0 20 68 69 67 68 65 73 74 0d 0a 2a 2a 20 76 61 6c   highest..** val
497c0 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70  ue.  For those p
497d0 61 72 61 6d 65 74 65 72 73 0d 0a 2a 2a 20 6e 6f  arameters..** no
497e0 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
497f0 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65   into *pHighwate
49800 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46  r and the resetF
49810 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 29  lag is ignored.)
49820 5e 0d 0a 2a 2a 20 5e 28 4f 74 68 65 72 20 70 61  ^..** ^(Other pa
49830 72 61 6d 65 74 65 72 73 20 72 65 63 6f 72 64 20  rameters record 
49840 6f 6e 6c 79 20 74 68 65 20 68 69 67 68 77 61 74  only the highwat
49850 65 72 20 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20  er mark and not 
49860 74 68 65 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20  the current..** 
49870 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 65 73  value.  For thes
49880 65 20 6c 61 74 74 65 72 20 70 61 72 61 6d 65 74  e latter paramet
49890 65 72 73 20 6e 6f 74 68 69 6e 67 20 69 73 20 77  ers nothing is w
498a0 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75  ritten into *pCu
498b0 72 72 65 6e 74 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a  rrent.)^..**..**
498c0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74   ^The sqlite3_st
498d0 61 74 75 73 28 29 20 72 6f 75 74 69 6e 65 20 72  atus() routine r
498e0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
498f0 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
49900 61 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b  a..** non-zero [
49910 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
49920 61 69 6c 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ailure...**..** 
49930 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
49940 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 69  threadsafe but i
49950 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54  s not atomic.  T
49960 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
49970 62 65 0d 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68  be..** called wh
49980 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ile other thread
49990 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68  s are running th
499a0 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72  e same or differ
499b0 65 6e 74 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 69  ent SQLite..** i
499c0 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65  nterfaces.  Howe
499d0 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72  ver the values r
499e0 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72  eturned in *pCur
499f0 72 65 6e 74 20 61 6e 64 0d 0a 2a 2a 20 2a 70 48  rent and..** *pH
49a00 69 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74  ighwater reflect
49a10 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53   the status of S
49a20 51 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65  QLite at differe
49a30 6e 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d  nt points in tim
49a40 65 0d 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20  e..** and it is 
49a50 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e  possible that an
49a60 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 69 67  other thread mig
49a70 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61  ht change the pa
49a80 72 61 6d 65 74 65 72 0d 0a 2a 2a 20 69 6e 20 62  rameter..** in b
49a90 65 74 77 65 65 6e 20 74 68 65 20 74 69 6d 65 73  etween the times
49aa0 20 77 68 65 6e 20 2a 70 43 75 72 72 65 6e 74 20   when *pCurrent 
49ab0 61 6e 64 20 2a 70 48 69 67 68 77 61 74 65 72 20  and *pHighwater 
49ac0 61 72 65 20 77 72 69 74 74 65 6e 2e 0d 0a 2a 2a  are written...**
49ad0 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  ..** See also: [
49ae0 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75  sqlite3_db_statu
49af0 73 28 29 5d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  s()]..*/..SQLITE
49b00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
49b10 5f 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20  _status(int op, 
49b20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69  int *pCurrent, i
49b30 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20  nt *pHighwater, 
49b40 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0d  int resetFlag);.
49b50 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ...../*..** CAPI
49b60 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
49b70 61 6d 65 74 65 72 73 0d 0a 2a 2a 20 4b 45 59 57  ameters..** KEYW
49b80 4f 52 44 53 3a 20 7b 73 74 61 74 75 73 20 70 61  ORDS: {status pa
49b90 72 61 6d 65 74 65 72 73 7d 0d 0a 2a 2a 0d 0a 2a  rameters}..**..*
49ba0 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20  * These integer 
49bb0 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 6e  constants design
49bc0 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e 2d  ate various run-
49bd0 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 61  time status para
49be0 6d 65 74 65 72 73 0d 0a 2a 2a 20 74 68 61 74 20  meters..** that 
49bf0 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20  can be returned 
49c00 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  by [sqlite3_stat
49c10 75 73 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c  us()]...**..** <
49c20 64 6c 3e 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45  dl>..** [[SQLITE
49c30 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
49c40 53 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49  SED]] ^(<dt>SQLI
49c50 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
49c60 5f 55 53 45 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  _USED</dt>..** <
49c70 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
49c80 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e 74  r is the current
49c90 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
49ca0 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0d 0a 2a  y checked out..*
49cb0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
49cc0 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68  _malloc()], eith
49cd0 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69  er directly or i
49ce0 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0d  ndirectly.  The.
49cf0 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75  .** figure inclu
49d00 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74  des calls made t
49d10 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
49d20 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c  c()] by the appl
49d30 69 63 61 74 69 6f 6e 0d 0a 2a 2a 20 61 6e 64 20  ication..** and 
49d40 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20  internal memory 
49d50 75 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c  usage by the SQL
49d60 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63  ite library.  Sc
49d70 72 61 74 63 68 20 6d 65 6d 6f 72 79 0d 0a 2a 2a  ratch memory..**
49d80 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b   controlled by [
49d90 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
49da0 52 41 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c  RATCH] and auxil
49db0 69 61 72 79 20 70 61 67 65 2d 63 61 63 68 65 0d  iary page-cache.
49dc0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72  .** memory contr
49dd0 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45  olled by [SQLITE
49de0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
49df0 45 5d 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64  E] is not includ
49e00 65 64 20 69 6e 0d 0a 2a 2a 20 74 68 69 73 20 70  ed in..** this p
49e10 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 61  arameter.  The a
49e20 6d 6f 75 6e 74 20 72 65 74 75 72 6e 65 64 20 69  mount returned i
49e30 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65  s the sum of the
49e40 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d 0a 2a 2a 20   allocation..** 
49e50 73 69 7a 65 73 20 61 73 20 72 65 70 6f 72 74 65  sizes as reporte
49e60 64 20 62 79 20 74 68 65 20 78 53 69 7a 65 20 6d  d by the xSize m
49e70 65 74 68 6f 64 20 69 6e 20 5b 73 71 6c 69 74 65  ethod in [sqlite
49e80 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 2e 3c  3_mem_methods].<
49e90 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b  /dd>)^..**..** [
49ea0 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  [SQLITE_STATUS_M
49eb0 41 4c 4c 4f 43 5f 53 49 5a 45 5d 5d 20 5e 28 3c  ALLOC_SIZE]] ^(<
49ec0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
49ed0 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74  _MALLOC_SIZE</dt
49ee0 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  >..** <dd>This p
49ef0 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73  arameter records
49f00 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d   the largest mem
49f10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
49f20 65 71 75 65 73 74 0d 0a 2a 2a 20 68 61 6e 64 65  equest..** hande
49f30 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  d to [sqlite3_ma
49f40 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  lloc()] or [sqli
49f50 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28  te3_realloc()] (
49f60 6f 72 20 74 68 65 69 72 0d 0a 2a 2a 20 69 6e 74  or their..** int
49f70 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74  ernal equivalent
49f80 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61  s).  Only the va
49f90 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20  lue returned in 
49fa0 74 68 65 0d 0a 2a 2a 20 2a 70 48 69 67 68 77 61  the..** *pHighwa
49fb0 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ter parameter to
49fc0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
49fd0 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65  ()] is of intere
49fe0 73 74 2e 20 20 0d 0a 2a 2a 20 54 68 65 20 76 61  st.  ..** The va
49ff0 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  lue written into
4a000 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70   the *pCurrent p
4a010 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65  arameter is unde
4a020 66 69 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a  fined.</dd>)^..*
4a030 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53  *..** [[SQLITE_S
4a040 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55  TATUS_MALLOC_COU
4a050 4e 54 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54  NT]] ^(<dt>SQLIT
4a060 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
4a070 43 4f 55 4e 54 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c  COUNT</dt>..** <
4a080 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
4a090 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6e 75  r records the nu
4a0a0 6d 62 65 72 20 6f 66 20 73 65 70 61 72 61 74 65  mber of separate
4a0b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
4a0c0 6f 6e 73 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  ons..** currentl
4a0d0 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f  y checked out.</
4a0e0 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b  dd>)^..**..** [[
4a0f0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
4a100 47 45 43 41 43 48 45 5f 55 53 45 44 5d 5d 20 5e  GECACHE_USED]] ^
4a110 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54  (<dt>SQLITE_STAT
4a120 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
4a130 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54  D</dt>..** <dd>T
4a140 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4a150 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
4a160 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20 6f   of pages used o
4a170 75 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 5b 70  ut of the..** [p
4a180 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  agecache memory 
4a190 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20  allocator] that 
4a1a0 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75  was configured u
4a1b0 73 69 6e 67 20 0d 0a 2a 2a 20 5b 53 51 4c 49 54  sing ..** [SQLIT
4a1c0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
4a1d0 48 45 5d 2e 20 20 54 68 65 0d 0a 2a 2a 20 76 61  HE].  The..** va
4a1e0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
4a1f0 69 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e  in pages, not in
4a200 20 62 79 74 65 73 2e 3c 2f 64 64 3e 29 5e 0d 0a   bytes.</dd>)^..
4a210 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  **..** [[SQLITE_
4a220 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
4a230 5f 4f 56 45 52 46 4c 4f 57 5d 5d 20 0d 0a 2a 2a  _OVERFLOW]] ..**
4a240 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54   ^(<dt>SQLITE_ST
4a250 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
4a260 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0d 0a 2a 2a  VERFLOW</dt>..**
4a270 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
4a280 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20  ter returns the 
4a290 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
4a2a0 6f 66 20 70 61 67 65 20 63 61 63 68 65 0d 0a 2a  of page cache..*
4a2b0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69  * allocation whi
4a2c0 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ch could not be 
4a2d0 73 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65  satisfied by the
4a2e0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
4a2f0 50 41 47 45 43 41 43 48 45 5d 0d 0a 2a 2a 20 62  PAGECACHE]..** b
4a300 75 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20  uffer and where 
4a310 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c  forced to overfl
4a320 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ow to [sqlite3_m
4a330 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0d 0a  alloc()].  The..
4a340 2a 2a 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  ** returned valu
4a350 65 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63  e includes alloc
4a360 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72  ations that over
4a370 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 20 74  flowed because t
4a380 68 65 79 0d 0a 2a 2a 20 77 68 65 72 65 20 74 6f  hey..** where to
4a390 6f 20 6c 61 72 67 65 20 28 74 68 65 79 20 77 65  o large (they we
4a3a0 72 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  re larger than t
4a3b0 68 65 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65  he "sz" paramete
4a3c0 72 20 74 6f 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45  r to..** [SQLITE
4a3d0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
4a3e0 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63 61 74 69  E]) and allocati
4a3f0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f  ons that overflo
4a400 77 65 64 20 62 65 63 61 75 73 65 0d 0a 2a 2a 20  wed because..** 
4a410 6e 6f 20 73 70 61 63 65 20 77 61 73 20 6c 65 66  no space was lef
4a420 74 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  t in the page ca
4a430 63 68 65 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d  che.</dd>)^..**.
4a440 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41  .** [[SQLITE_STA
4a450 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49  TUS_PAGECACHE_SI
4a460 5a 45 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54  ZE]] ^(<dt>SQLIT
4a470 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
4a480 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0d 0a 2a 2a  HE_SIZE</dt>..**
4a490 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
4a4a0 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20  ter records the 
4a4b0 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61  largest memory a
4a4c0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
4a4d0 74 0d 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20  t..** handed to 
4a4e0 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72  [pagecache memor
4a4f0 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f  y allocator].  O
4a500 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65  nly the value re
4a510 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0d 0a 2a  turned in the..*
4a520 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61  * *pHighwater pa
4a530 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
4a540 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73  te3_status()] is
4a550 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0d   of interest.  .
4a560 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72  .** The value wr
4a570 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a  itten into the *
4a580 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74  pCurrent paramet
4a590 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  er is undefined.
4a5a0 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  </dd>)^..**..** 
4a5b0 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  [[SQLITE_STATUS_
4a5c0 53 43 52 41 54 43 48 5f 55 53 45 44 5d 5d 20 5e  SCRATCH_USED]] ^
4a5d0 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54  (<dt>SQLITE_STAT
4a5e0 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 3c  US_SCRATCH_USED<
4a5f0 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69  /dt>..** <dd>Thi
4a600 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
4a610 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
4a620 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75 73  f allocations us
4a630 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0d 0a 2a  ed out of the..*
4a640 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  * [scratch memor
4a650 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e  y allocator] con
4a660 66 69 67 75 72 65 64 20 75 73 69 6e 67 0d 0a 2a  figured using..*
4a670 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
4a680 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20  _SCRATCH].  The 
4a690 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
4a6a0 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  s in allocations
4a6b0 2c 20 6e 6f 74 0d 0a 2a 2a 20 69 6e 20 62 79 74  , not..** in byt
4a6c0 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e  es.  Since a sin
4a6d0 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f  gle thread may o
4a6e0 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72  nly have one scr
4a6f0 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d  atch allocation.
4a700 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  .** outstanding 
4a710 61 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61  at time, this pa
4a720 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70  rameter also rep
4a730 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20  orts the number 
4a740 6f 66 20 74 68 72 65 61 64 73 0d 0a 2a 2a 20 75  of threads..** u
4a750 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d  sing scratch mem
4a760 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d 65 20  ory at the same 
4a770 74 69 6d 65 2e 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a  time.</dd>)^..**
4a780 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54  ..** [[SQLITE_ST
4a790 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
4a7a0 52 46 4c 4f 57 5d 5d 20 5e 28 3c 64 74 3e 53 51  RFLOW]] ^(<dt>SQ
4a7b0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
4a7c0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74  TCH_OVERFLOW</dt
4a7d0 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  >..** <dd>This p
4a7e0 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73  arameter returns
4a7f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
4a800 79 74 65 73 20 6f 66 20 73 63 72 61 74 63 68 20  ytes of scratch 
4a810 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 61 6c 6c 6f 63  memory..** alloc
4a820 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c  ation which coul
4a830 64 20 6e 6f 74 20 62 65 20 73 61 74 69 73 66 69  d not be satisfi
4a840 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54  ed by the [SQLIT
4a850 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
4a860 5d 0d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64  ]..** buffer and
4a870 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f   where forced to
4a880 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71   overflow to [sq
4a890 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
4a8a0 20 20 54 68 65 20 76 61 6c 75 65 73 0d 0a 2a 2a    The values..**
4a8b0 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
4a8c0 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 61  e overflows beca
4a8d0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
4a8e0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  d allocation was
4a8f0 20 74 6f 6f 0d 0a 2a 2a 20 6c 61 72 67 65 72 20   too..** larger 
4a900 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73  (that is, becaus
4a910 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  e the requested 
4a920 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c  allocation was l
4a930 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0d 0a  arger than the..
4a940 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65  ** "sz" paramete
4a950 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  r to [SQLITE_CON
4a960 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e  FIG_SCRATCH]) an
4a970 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72  d because no scr
4a980 61 74 63 68 20 62 75 66 66 65 72 0d 0a 2a 2a 20  atch buffer..** 
4a990 73 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c  slots were avail
4a9a0 61 62 6c 65 2e 0d 0a 2a 2a 20 3c 2f 64 64 3e 29  able...** </dd>)
4a9b0 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49  ^..**..** [[SQLI
4a9c0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
4a9d0 48 5f 53 49 5a 45 5d 5d 20 5e 28 3c 64 74 3e 53  H_SIZE]] ^(<dt>S
4a9e0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
4a9f0 41 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0d 0a  ATCH_SIZE</dt>..
4aa00 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4aa10 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68  meter records th
4aa20 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79  e largest memory
4aa30 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
4aa40 65 73 74 0d 0a 2a 2a 20 68 61 6e 64 65 64 20 74  est..** handed t
4aa50 6f 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  o [scratch memor
4aa60 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f  y allocator].  O
4aa70 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65  nly the value re
4aa80 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0d 0a 2a  turned in the..*
4aa90 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61  * *pHighwater pa
4aaa0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
4aab0 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73  te3_status()] is
4aac0 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0d   of interest.  .
4aad0 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72  .** The value wr
4aae0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a  itten into the *
4aaf0 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74  pCurrent paramet
4ab00 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  er is undefined.
4ab10 3c 2f 64 64 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  </dd>)^..**..** 
4ab20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  [[SQLITE_STATUS_
4ab30 50 41 52 53 45 52 5f 53 54 41 43 4b 5d 5d 20 5e  PARSER_STACK]] ^
4ab40 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54  (<dt>SQLITE_STAT
4ab50 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c  US_PARSER_STACK<
4ab60 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69  /dt>..** <dd>Thi
4ab70 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
4ab80 72 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20  rds the deepest 
4ab90 70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49  parser stack.  I
4aba0 74 20 69 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 6d 65  t is only..** me
4abb0 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69  aningful if SQLi
4abc0 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
4abd0 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53  ith [YYTRACKMAXS
4abe0 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e  TACKDEPTH].</dd>
4abf0 29 5e 0d 0a 2a 2a 20 3c 2f 64 6c 3e 0d 0a 2a 2a  )^..** </dl>..**
4ac00 0d 0a 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20  ..** New status 
4ac10 70 61 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62  parameters may b
4ac20 65 20 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d  e added from tim
4ac30 65 20 74 6f 20 74 69 6d 65 2e 0d 0a 2a 2f 0d 0a  e to time...*/..
4ac40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
4ac50 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
4ac60 44 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23 64  D          0..#d
4ac70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
4ac80 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53  TUS_PAGECACHE_US
4ac90 45 44 20 20 20 20 20 20 20 31 0d 0a 23 64 65 66  ED       1..#def
4aca0 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
4acb0 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52  S_PAGECACHE_OVER
4acc0 46 4c 4f 57 20 20 20 32 0d 0a 23 64 65 66 69 6e  FLOW   2..#defin
4acd0 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
4ace0 53 43 52 41 54 43 48 5f 55 53 45 44 20 20 20 20  SCRATCH_USED    
4acf0 20 20 20 20 20 33 0d 0a 23 64 65 66 69 6e 65 20       3..#define 
4ad00 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
4ad10 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 20  RATCH_OVERFLOW  
4ad20 20 20 20 34 0d 0a 23 64 65 66 69 6e 65 20 53 51     4..#define SQ
4ad30 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
4ad40 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20  OC_SIZE         
4ad50 20 35 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   5..#define SQLI
4ad60 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52  TE_STATUS_PARSER
4ad70 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36  _STACK         6
4ad80 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
4ad90 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
4ada0 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0d 0a  E_SIZE       7..
4adb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
4adc0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
4add0 5a 45 20 20 20 20 20 20 20 20 20 38 0d 0a 23 64  ZE         8..#d
4ade0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
4adf0 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55 4e 54  TUS_MALLOC_COUNT
4ae00 20 20 20 20 20 20 20 20 20 39 0d 0a 0d 0a 2f 2a           9..../*
4ae10 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  ..** CAPI3REF: D
4ae20 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
4ae30 6f 6e 20 53 74 61 74 75 73 0d 0a 2a 2a 0d 0a 2a  on Status..**..*
4ae40 2a 20 5e 54 68 69 73 20 69 6e 74 65 72 66 61 63  * ^This interfac
4ae50 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 74  e is used to ret
4ae60 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73 74  rieve runtime st
4ae70 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  atus information
4ae80 20 0d 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69   ..** about a si
4ae90 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63  ngle [database c
4aea0 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 5e 54 68  onnection].  ^Th
4aeb0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
4aec0 20 69 73 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61   is the..** data
4aed0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
4aee0 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74  object to be int
4aef0 65 72 72 6f 67 61 74 65 64 2e 20 20 5e 54 68 65  errogated.  ^The
4af00 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
4af10 0d 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67  ..** is an integ
4af20 65 72 20 63 6f 6e 73 74 61 6e 74 2c 20 74 61 6b  er constant, tak
4af30 65 6e 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20  en from the set 
4af40 6f 66 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  of..** [SQLITE_D
4af50 42 53 54 41 54 55 53 20 6f 70 74 69 6f 6e 73 5d  BSTATUS options]
4af60 2c 20 74 68 61 74 0d 0a 2a 2a 20 64 65 74 65 72  , that..** deter
4af70 6d 69 6e 65 73 20 74 68 65 20 70 61 72 61 6d 65  mines the parame
4af80 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61  ter to interroga
4af90 74 65 2e 20 20 54 68 65 20 73 65 74 20 6f 66 20  te.  The set of 
4afa0 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 42 53  ..** [SQLITE_DBS
4afb0 54 41 54 55 53 20 6f 70 74 69 6f 6e 73 5d 20 69  TATUS options] i
4afc0 73 20 6c 69 6b 65 6c 79 0d 0a 2a 2a 20 74 6f 20  s likely..** to 
4afd0 67 72 6f 77 20 69 6e 20 66 75 74 75 72 65 20 72  grow in future r
4afe0 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
4aff0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  e...**..** ^The 
4b000 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
4b010 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
4b020 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 69 74  arameter is writ
4b030 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0d 0a  ten into *pCur..
4b040 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 65  ** and the highe
4b050 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  st instantaneous
4b060 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 65   value is writte
4b070 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20  n into *pHiwtr. 
4b080 20 5e 49 66 0d 0a 2a 2a 20 74 68 65 20 72 65 73   ^If..** the res
4b090 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74  etFlg is true, t
4b0a0 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20  hen the highest 
4b0b0 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61  instantaneous va
4b0c0 6c 75 65 20 69 73 0d 0a 2a 2a 20 72 65 73 65 74  lue is..** reset
4b0d0 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68   back down to th
4b0e0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e  e current value.
4b0f0 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 73 71  ..**..** ^The sq
4b100 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
4b110 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  ) routine return
4b120 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  s SQLITE_OK on s
4b130 75 63 63 65 73 73 20 61 6e 64 20 61 0d 0a 2a 2a  uccess and a..**
4b140 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72   non-zero [error
4b150 20 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72   code] on failur
4b160 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61  e...**..** See a
4b170 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74  lso: [sqlite3_st
4b180 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c  atus()] and [sql
4b190 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
4b1a0 28 29 5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ()]...*/..SQLITE
4b1b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
4b1c0 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69 74  _db_status(sqlit
4b1d0 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74  e3*, int op, int
4b1e0 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 69   *pCur, int *pHi
4b1f0 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c  wtr, int resetFl
4b200 67 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  g);..../*..** CA
4b210 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50  PI3REF: Status P
4b220 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61  arameters for da
4b230 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
4b240 6e 73 0d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  ns..** KEYWORDS:
4b250 20 7b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55   {SQLITE_DBSTATU
4b260 53 20 6f 70 74 69 6f 6e 73 7d 0d 0a 2a 2a 0d 0a  S options}..**..
4b270 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
4b280 74 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c  ts are the avail
4b290 61 62 6c 65 20 69 6e 74 65 67 65 72 20 22 76 65  able integer "ve
4b2a0 72 62 73 22 20 74 68 61 74 20 63 61 6e 20 62 65  rbs" that can be
4b2b0 20 70 61 73 73 65 64 20 61 73 0d 0a 2a 2a 20 74   passed as..** t
4b2c0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4b2d0 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  nt to the [sqlit
4b2e0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20  e3_db_status()] 
4b2f0 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a  interface...**..
4b300 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 6d 61 79  ** New verbs may
4b310 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
4b320 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
4b330 53 51 4c 69 74 65 2e 20 45 78 69 73 74 69 6e 67  SQLite. Existing
4b340 20 76 65 72 62 73 0d 0a 2a 2a 20 6d 69 67 68 74   verbs..** might
4b350 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64   be discontinued
4b360 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73  . Applications s
4b370 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20  hould check the 
4b380 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
4b390 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64 62  ..** [sqlite3_db
4b3a0 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20 6d 61  _status()] to ma
4b3b0 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
4b3c0 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0d 0a 2a   call worked...*
4b3d0 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  * The [sqlite3_d
4b3e0 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  b_status()] inte
4b3f0 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72  rface will retur
4b400 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72  n a non-zero err
4b410 6f 72 20 63 6f 64 65 0d 0a 2a 2a 20 69 66 20 61  or code..** if a
4b420 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72   discontinued or
4b430 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72   unsupported ver
4b440 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0d 0a 2a  b is invoked...*
4b450 2a 0d 0a 2a 2a 20 3c 64 6c 3e 0d 0a 2a 2a 20 5b  *..** <dl>..** [
4b460 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  [SQLITE_DBSTATUS
4b470 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d  _LOOKASIDE_USED]
4b480 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44  ] ^(<dt>SQLITE_D
4b490 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
4b4a0 45 5f 55 53 45 44 3c 2f 64 74 3e 0d 0a 2a 2a 20  E_USED</dt>..** 
4b4b0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
4b4c0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
4b4d0 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b 61 73 69  umber of lookasi
4b4e0 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f 74 73 20  de memory slots 
4b4f0 63 75 72 72 65 6e 74 6c 79 0d 0a 2a 2a 20 63 68  currently..** ch
4b500 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 3e 29  ecked out.</dd>)
4b510 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49  ^..**..** [[SQLI
4b520 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
4b530 41 53 49 44 45 5f 48 49 54 5d 5d 20 5e 28 3c 64  ASIDE_HIT]] ^(<d
4b540 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  t>SQLITE_DBSTATU
4b550 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 3c  S_LOOKASIDE_HIT<
4b560 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69  /dt>..** <dd>Thi
4b570 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
4b580 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6d  rns the number m
4b590 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 73 20 74  alloc attempts t
4b5a0 68 61 74 20 77 65 72 65 20 0d 0a 2a 2a 20 73 61  hat were ..** sa
4b5b0 74 69 73 66 69 65 64 20 75 73 69 6e 67 20 6c 6f  tisfied using lo
4b5c0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20  okaside memory. 
4b5d0 4f 6e 6c 79 20 74 68 65 20 68 69 67 68 2d 77 61  Only the high-wa
4b5e0 74 65 72 20 76 61 6c 75 65 20 69 73 20 6d 65 61  ter value is mea
4b5f0 6e 69 6e 67 66 75 6c 3b 0d 0a 2a 2a 20 74 68 65  ningful;..** the
4b600 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 69   current value i
4b610 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2e 29 5e  s always zero.)^
4b620 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54  ..**..** [[SQLIT
4b630 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
4b640 53 49 44 45 5f 4d 49 53 53 5f 53 49 5a 45 5d 5d  SIDE_MISS_SIZE]]
4b650 0d 0a 2a 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54  ..** ^(<dt>SQLIT
4b660 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
4b670 53 49 44 45 5f 4d 49 53 53 5f 53 49 5a 45 3c 2f  SIDE_MISS_SIZE</
4b680 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  dt>..** <dd>This
4b690 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
4b6a0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6d 61  ns the number ma
4b6b0 6c 6c 6f 63 20 61 74 74 65 6d 70 74 73 20 74 68  lloc attempts th
4b6c0 61 74 20 6d 69 67 68 74 20 68 61 76 65 0d 0a 2a  at might have..*
4b6d0 2a 20 62 65 65 6e 20 73 61 74 69 73 66 69 65 64  * been satisfied
4b6e0 20 75 73 69 6e 67 20 6c 6f 6f 6b 61 73 69 64 65   using lookaside
4b6f0 20 6d 65 6d 6f 72 79 20 62 75 74 20 66 61 69 6c   memory but fail
4b700 65 64 20 64 75 65 20 74 6f 20 74 68 65 20 61 6d  ed due to the am
4b710 6f 75 6e 74 20 6f 66 0d 0a 2a 2a 20 6d 65 6d 6f  ount of..** memo
4b720 72 79 20 72 65 71 75 65 73 74 65 64 20 62 65 69  ry requested bei
4b730 6e 67 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ng larger than t
4b740 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f  he lookaside slo
4b750 74 20 73 69 7a 65 2e 0d 0a 2a 2a 20 4f 6e 6c 79  t size...** Only
4b760 20 74 68 65 20 68 69 67 68 2d 77 61 74 65 72 20   the high-water 
4b770 76 61 6c 75 65 20 69 73 20 6d 65 61 6e 69 6e 67  value is meaning
4b780 66 75 6c 3b 0d 0a 2a 2a 20 74 68 65 20 63 75 72  ful;..** the cur
4b790 72 65 6e 74 20 76 61 6c 75 65 20 69 73 20 61 6c  rent value is al
4b7a0 77 61 79 73 20 7a 65 72 6f 2e 29 5e 0d 0a 2a 2a  ways zero.)^..**
4b7b0 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42  ..** [[SQLITE_DB
4b7c0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
4b7d0 5f 4d 49 53 53 5f 46 55 4c 4c 5d 5d 0d 0a 2a 2a  _MISS_FULL]]..**
4b7e0 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42   ^(<dt>SQLITE_DB
4b7f0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
4b800 5f 4d 49 53 53 5f 46 55 4c 4c 3c 2f 64 74 3e 0d  _MISS_FULL</dt>.
4b810 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
4b820 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74  ameter returns t
4b830 68 65 20 6e 75 6d 62 65 72 20 6d 61 6c 6c 6f 63  he number malloc
4b840 20 61 74 74 65 6d 70 74 73 20 74 68 61 74 20 6d   attempts that m
4b850 69 67 68 74 20 68 61 76 65 0d 0a 2a 2a 20 62 65  ight have..** be
4b860 65 6e 20 73 61 74 69 73 66 69 65 64 20 75 73 69  en satisfied usi
4b870 6e 67 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d  ng lookaside mem
4b880 6f 72 79 20 62 75 74 20 66 61 69 6c 65 64 20 64  ory but failed d
4b890 75 65 20 74 6f 20 61 6c 6c 20 6c 6f 6f 6b 61 73  ue to all lookas
4b8a0 69 64 65 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  ide..** memory a
4b8b0 6c 72 65 61 64 79 20 62 65 69 6e 67 20 69 6e 20  lready being in 
4b8c0 75 73 65 2e 0d 0a 2a 2a 20 4f 6e 6c 79 20 74 68  use...** Only th
4b8d0 65 20 68 69 67 68 2d 77 61 74 65 72 20 76 61 6c  e high-water val
4b8e0 75 65 20 69 73 20 6d 65 61 6e 69 6e 67 66 75 6c  ue is meaningful
4b8f0 3b 0d 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  ;..** the curren
4b900 74 20 76 61 6c 75 65 20 69 73 20 61 6c 77 61 79  t value is alway
4b910 73 20 7a 65 72 6f 2e 29 5e 0d 0a 2a 2a 0d 0a 2a  s zero.)^..**..*
4b920 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 41  * [[SQLITE_DBSTA
4b930 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 5d 5d  TUS_CACHE_USED]]
4b940 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42   ^(<dt>SQLITE_DB
4b950 53 54 41 54 55 53 5f 43 41 43 48 45 5f 55 53 45  STATUS_CACHE_USE
4b960 44 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54  D</dt>..** <dd>T
4b970 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4b980 74 75 72 6e 73 20 74 68 65 20 61 70 70 72 6f 78  turns the approx
4b990 69 6d 61 74 65 20 6e 75 6d 62 65 72 20 6f 66 20  imate number of 
4b9a0 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 70  of bytes of heap
4b9b0 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 75 73 65 64  ..** memory used
4b9c0 20 62 79 20 61 6c 6c 20 70 61 67 65 72 20 63 61   by all pager ca
4b9d0 63 68 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ches associated 
4b9e0 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
4b9f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 29 5e 0d  e connection.)^.
4ba00 0a 2a 2a 20 5e 54 68 65 20 68 69 67 68 77 61 74  .** ^The highwat
4ba10 65 72 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 74  er mark associat
4ba20 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44  ed with SQLITE_D
4ba30 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 55 53  BSTATUS_CACHE_US
4ba40 45 44 20 69 73 20 61 6c 77 61 79 73 20 30 2e 0d  ED is always 0..
4ba50 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45  .**..** [[SQLITE
4ba60 5f 44 42 53 54 41 54 55 53 5f 53 43 48 45 4d 41  _DBSTATUS_SCHEMA
4ba70 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51  _USED]] ^(<dt>SQ
4ba80 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 43  LITE_DBSTATUS_SC
4ba90 48 45 4d 41 5f 55 53 45 44 3c 2f 64 74 3e 0d 0a  HEMA_USED</dt>..
4baa0 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4bab0 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
4bac0 65 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e 75  e approximate nu
4bad0 6d 62 65 72 20 6f 66 20 6f 66 20 62 79 74 65 73  mber of of bytes
4bae0 20 6f 66 20 68 65 61 70 0d 0a 2a 2a 20 6d 65 6d   of heap..** mem
4baf0 6f 72 79 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ory used to stor
4bb00 65 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72  e the schema for
4bb10 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61   all databases a
4bb20 73 73 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69  ssociated..** wi
4bb30 74 68 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  th the connectio
4bb40 6e 20 2d 20 6d 61 69 6e 2c 20 74 65 6d 70 2c 20  n - main, temp, 
4bb50 61 6e 64 20 61 6e 79 20 5b 41 54 54 41 43 48 5d  and any [ATTACH]
4bb60 2d 65 64 20 64 61 74 61 62 61 73 65 73 2e 29 5e  -ed databases.)^
4bb70 20 0d 0a 2a 2a 20 5e 54 68 65 20 66 75 6c 6c 20   ..** ^The full 
4bb80 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
4bb90 20 75 73 65 64 20 62 79 20 74 68 65 20 73 63 68   used by the sch
4bba0 65 6d 61 73 20 69 73 20 72 65 70 6f 72 74 65 64  emas is reported
4bbb0 2c 20 65 76 65 6e 20 69 66 20 74 68 65 0d 0a 2a  , even if the..*
4bbc0 2a 20 73 63 68 65 6d 61 20 6d 65 6d 6f 72 79 20  * schema memory 
4bbd0 69 73 20 73 68 61 72 65 64 20 77 69 74 68 20 6f  is shared with o
4bbe0 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
4bbf0 6e 6e 65 63 74 69 6f 6e 73 20 64 75 65 20 74 6f  nnections due to
4bc00 0d 0a 2a 2a 20 5b 73 68 61 72 65 64 20 63 61 63  ..** [shared cac
4bc10 68 65 20 6d 6f 64 65 5d 20 62 65 69 6e 67 20 65  he mode] being e
4bc20 6e 61 62 6c 65 64 2e 0d 0a 2a 2a 20 5e 54 68 65  nabled...** ^The
4bc30 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   highwater mark 
4bc40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
4bc50 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
4bc60 53 43 48 45 4d 41 5f 55 53 45 44 20 69 73 20 61  SCHEMA_USED is a
4bc70 6c 77 61 79 73 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a  lways 0...**..**
4bc80 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54   [[SQLITE_DBSTAT
4bc90 55 53 5f 53 54 4d 54 5f 55 53 45 44 5d 5d 20 5e  US_STMT_USED]] ^
4bca0 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54  (<dt>SQLITE_DBST
4bcb0 41 54 55 53 5f 53 54 4d 54 5f 55 53 45 44 3c 2f  ATUS_STMT_USED</
4bcc0 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  dt>..** <dd>This
4bcd0 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
4bce0 6e 73 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61  ns the approxima
4bcf0 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 66 20  te number of of 
4bd00 62 79 74 65 73 20 6f 66 20 68 65 61 70 0d 0a 2a  bytes of heap..*
4bd10 2a 20 61 6e 64 20 6c 6f 6f 6b 61 73 69 64 65 20  * and lookaside 
4bd20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61  memory used by a
4bd30 6c 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ll prepared stat
4bd40 65 6d 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65  ements associate
4bd50 64 20 77 69 74 68 0d 0a 2a 2a 20 74 68 65 20 64  d with..** the d
4bd60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
4bd70 6f 6e 2e 29 5e 0d 0a 2a 2a 20 5e 54 68 65 20 68  on.)^..** ^The h
4bd80 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 73  ighwater mark as
4bd90 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 51  sociated with SQ
4bda0 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 54  LITE_DBSTATUS_ST
4bdb0 4d 54 5f 55 53 45 44 20 69 73 20 61 6c 77 61 79  MT_USED is alway
4bdc0 73 20 30 2e 0d 0a 2a 2a 20 3c 2f 64 64 3e 0d 0a  s 0...** </dd>..
4bdd0 2a 2a 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  **..** [[SQLITE_
4bde0 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48  DBSTATUS_CACHE_H
4bdf0 49 54 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54  IT]] ^(<dt>SQLIT
4be00 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45  E_DBSTATUS_CACHE
4be10 5f 48 49 54 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64  _HIT</dt>..** <d
4be20 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
4be30 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
4be40 62 65 72 20 6f 66 20 70 61 67 65 72 20 63 61 63  ber of pager cac
4be50 68 65 20 68 69 74 73 20 74 68 61 74 20 68 61 76  he hits that hav
4be60 65 0d 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 29  e..** occurred.)
4be70 5e 20 5e 54 68 65 20 68 69 67 68 77 61 74 65 72  ^ ^The highwater
4be80 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 74 65 64   mark associated
4be90 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 42 53   with SQLITE_DBS
4bea0 54 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 20  TATUS_CACHE_HIT 
4beb0 0d 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 30  ..** is always 0
4bec0 2e 0d 0a 2a 2a 20 3c 2f 64 64 3e 0d 0a 2a 2a 0d  ...** </dd>..**.
4bed0 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53  .** [[SQLITE_DBS
4bee0 54 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53  TATUS_CACHE_MISS
4bef0 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f  ]] ^(<dt>SQLITE_
4bf00 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 4d  DBSTATUS_CACHE_M
4bf10 49 53 53 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64  ISS</dt>..** <dd
4bf20 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
4bf30 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
4bf40 65 72 20 6f 66 20 70 61 67 65 72 20 63 61 63 68  er of pager cach
4bf50 65 20 6d 69 73 73 65 73 20 74 68 61 74 20 68 61  e misses that ha
4bf60 76 65 0d 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e  ve..** occurred.
4bf70 29 5e 20 5e 54 68 65 20 68 69 67 68 77 61 74 65  )^ ^The highwate
4bf80 72 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 74 65  r mark associate
4bf90 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 42  d with SQLITE_DB
4bfa0 53 54 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 53  STATUS_CACHE_MIS
4bfb0 53 20 0d 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73  S ..** is always
4bfc0 20 30 2e 0d 0a 2a 2a 20 3c 2f 64 64 3e 0d 0a 2a   0...** </dd>..*
4bfd0 2a 20 3c 2f 64 6c 3e 0d 0a 2a 2f 0d 0a 23 64 65  * </dl>..*/..#de
4bfe0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4bff0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
4c000 53 45 44 20 20 20 20 20 20 20 30 0d 0a 23 64 65  SED       0..#de
4c010 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c020 41 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 20  ATUS_CACHE_USED 
4c030 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23 64 65            1..#de
4c040 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c050 41 54 55 53 5f 53 43 48 45 4d 41 5f 55 53 45 44  ATUS_SCHEMA_USED
4c060 20 20 20 20 20 20 20 20 20 20 32 0d 0a 23 64 65            2..#de
4c070 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c080 41 54 55 53 5f 53 54 4d 54 5f 55 53 45 44 20 20  ATUS_STMT_USED  
4c090 20 20 20 20 20 20 20 20 20 20 33 0d 0a 23 64 65            3..#de
4c0a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c0b0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48  ATUS_LOOKASIDE_H
4c0c0 49 54 20 20 20 20 20 20 20 20 34 0d 0a 23 64 65  IT        4..#de
4c0d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c0e0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d  ATUS_LOOKASIDE_M
4c0f0 49 53 53 5f 53 49 5a 45 20 20 35 0d 0a 23 64 65  ISS_SIZE  5..#de
4c100 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c110 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d  ATUS_LOOKASIDE_M
4c120 49 53 53 5f 46 55 4c 4c 20 20 36 0d 0a 23 64 65  ISS_FULL  6..#de
4c130 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c140 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 20 20  ATUS_CACHE_HIT  
4c150 20 20 20 20 20 20 20 20 20 20 37 0d 0a 23 64 65            7..#de
4c160 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c170 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 20  ATUS_CACHE_MISS 
4c180 20 20 20 20 20 20 20 20 20 20 38 0d 0a 23 64 65            8..#de
4c190 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4c1a0 41 54 55 53 5f 4d 41 58 20 20 20 20 20 20 20 20  ATUS_MAX        
4c1b0 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a            8   /*
4c1c0 20 4c 61 72 67 65 73 74 20 64 65 66 69 6e 65 64   Largest defined
4c1d0 20 44 42 53 54 41 54 55 53 20 2a 2f 0d 0a 0d 0a   DBSTATUS */....
4c1e0 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
4c1f0 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 74  F: Prepared Stat
4c200 65 6d 65 6e 74 20 53 74 61 74 75 73 0d 0a 2a 2a  ement Status..**
4c210 0d 0a 2a 2a 20 5e 28 45 61 63 68 20 70 72 65 70  ..** ^(Each prep
4c220 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d  ared statement m
4c230 61 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73  aintains various
4c240 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d  ..** [SQLITE_STM
4c250 54 53 54 41 54 55 53 20 63 6f 75 6e 74 65 72 73  TSTATUS counters
4c260 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74  ] that measure t
4c270 68 65 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 6f 66  he number..** of
4c280 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65   times it has pe
4c290 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63  rformed specific
4c2a0 20 6f 70 65 72 61 74 69 6f 6e 73 2e 29 5e 20 20   operations.)^  
4c2b0 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63  These counters c
4c2c0 61 6e 0d 0a 2a 2a 20 62 65 20 75 73 65 64 20 74  an..** be used t
4c2d0 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65  o monitor the pe
4c2e0 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63  rformance charac
4c2f0 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65  teristics of the
4c300 20 70 72 65 70 61 72 65 64 0d 0a 2a 2a 20 73 74   prepared..** st
4c310 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65  atements.  For e
4c320 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e  xample, if the n
4c330 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73  umber of table s
4c340 74 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63  teps greatly exc
4c350 65 65 64 73 0d 0a 2a 2a 20 74 68 65 20 6e 75 6d  eeds..** the num
4c360 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61  ber of table sea
4c370 72 63 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20  rches or result 
4c380 72 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64  rows, that would
4c390 20 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74   tend to indicat
4c3a0 65 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70  e..** that the p
4c3b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
4c3c0 74 20 69 73 20 75 73 69 6e 67 20 61 20 66 75 6c  t is using a ful
4c3d0 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 72 61 74  l table scan rat
4c3e0 68 65 72 20 74 68 61 6e 0d 0a 2a 2a 20 61 6e 20  her than..** an 
4c3f0 69 6e 64 65 78 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a  index.  ..**..**
4c400 20 5e 28 54 68 69 73 20 69 6e 74 65 72 66 61 63   ^(This interfac
4c410 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 74  e is used to ret
4c420 72 69 65 76 65 20 61 6e 64 20 72 65 73 65 74 20  rieve and reset 
4c430 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20 66  counter values f
4c440 72 6f 6d 0d 0a 2a 2a 20 61 20 5b 70 72 65 70 61  rom..** a [prepa
4c450 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
4c460 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
4c470 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 70 61  ent is the prepa
4c480 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a  red statement..*
4c490 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69  * object to be i
4c4a0 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68  nterrogated.  Th
4c4b0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
4c4c0 74 0d 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65  t..** is an inte
4c4d0 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ger code for a s
4c4e0 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f  pecific [SQLITE_
4c4f0 53 54 4d 54 53 54 41 54 55 53 20 63 6f 75 6e 74  STMTSTATUS count
4c500 65 72 5d 0d 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  er]..** to be in
4c510 74 65 72 72 6f 67 61 74 65 64 2e 29 5e 0d 0a 2a  terrogated.)^..*
4c520 2a 20 5e 54 68 65 20 63 75 72 72 65 6e 74 20 76  * ^The current v
4c530 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75  alue of the requ
4c540 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20 69 73  ested counter is
4c550 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 5e   returned...** ^
4c560 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 20  If the resetFlg 
4c570 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
4c580 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 73  e counter is res
4c590 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72  et to zero after
4c5a0 20 74 68 69 73 0d 0a 2a 2a 20 69 6e 74 65 72 66   this..** interf
4c5b0 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  ace call returns
4c5c0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c  ...**..** See al
4c5d0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
4c5e0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
4c5f0 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d  te3_db_status()]
4c600 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
4c610 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
4c620 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65  mt_status(sqlite
4c630 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c  3_stmt*, int op,
4c640 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0d 0a  int resetFlg);..
4c650 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
4c660 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65  F: Status Parame
4c670 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65  ters for prepare
4c680 64 20 73 74 61 74 65 6d 65 6e 74 73 0d 0a 2a 2a  d statements..**
4c690 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 49   KEYWORDS: {SQLI
4c6a0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 20 63 6f  TE_STMTSTATUS co
4c6b0 75 6e 74 65 72 7d 20 7b 53 51 4c 49 54 45 5f 53  unter} {SQLITE_S
4c6c0 54 4d 54 53 54 41 54 55 53 20 63 6f 75 6e 74 65  TMTSTATUS counte
4c6d0 72 73 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73  rs}..**..** Thes
4c6e0 65 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d  e preprocessor m
4c6f0 61 63 72 6f 73 20 64 65 66 69 6e 65 20 69 6e 74  acros define int
4c700 65 67 65 72 20 63 6f 64 65 73 20 74 68 61 74 20  eger codes that 
4c710 6e 61 6d 65 20 63 6f 75 6e 74 65 72 0d 0a 2a 2a  name counter..**
4c720 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74   values associat
4c730 65 64 20 77 69 74 68 20 74 68 65 20 5b 73 71 6c  ed with the [sql
4c740 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
4c750 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0d 0a  ()] interface...
4c760 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  ** The meanings 
4c770 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 63  of the various c
4c780 6f 75 6e 74 65 72 73 20 61 72 65 20 61 73 20 66  ounters are as f
4c790 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ollows:..**..** 
4c7a0 3c 64 6c 3e 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54  <dl>..** [[SQLIT
4c7b0 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c  E_STMTSTATUS_FUL
4c7c0 4c 53 43 41 4e 5f 53 54 45 50 5d 5d 20 3c 64 74  LSCAN_STEP]] <dt
4c7d0 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  >SQLITE_STMTSTAT
4c7e0 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50  US_FULLSCAN_STEP
4c7f0 3c 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 5e 54  </dt>..** <dd>^T
4c800 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65  his is the numbe
4c810 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
4c820 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70  SQLite has stepp
4c830 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0d 0a 2a  ed forward in..*
4c840 2a 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72  * a table as par
4c850 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c  t of a full tabl
4c860 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e  e scan.  Large n
4c870 75 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20  umbers for this 
4c880 63 6f 75 6e 74 65 72 0d 0a 2a 2a 20 6d 61 79 20  counter..** may 
4c890 69 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75  indicate opportu
4c8a0 6e 69 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f  nities for perfo
4c8b0 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65  rmance improveme
4c8c0 6e 74 20 74 68 72 6f 75 67 68 20 0d 0a 2a 2a 20  nt through ..** 
4c8d0 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20 69  careful use of i
4c8e0 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0d 0a 2a 2a  ndices.</dd>..**
4c8f0 0d 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54  ..** [[SQLITE_ST
4c900 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 5d 5d 20  MTSTATUS_SORT]] 
4c910 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53  <dt>SQLITE_STMTS
4c920 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0d  TATUS_SORT</dt>.
4c930 0a 2a 2a 20 3c 64 64 3e 5e 54 68 69 73 20 69 73  .** <dd>^This is
4c940 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
4c950 6f 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74  ort operations t
4c960 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65  hat have occurre
4c970 64 2e 0d 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72  d...** A non-zer
4c980 6f 20 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20  o value in this 
4c990 63 6f 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69  counter may indi
4c9a0 63 61 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e  cate an opportun
4c9b0 69 74 79 20 74 6f 0d 0a 2a 2a 20 69 6d 70 72 6f  ity to..** impro
4c9c0 76 65 6d 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e  vement performan
4c9d0 63 65 20 74 68 72 6f 75 67 68 20 63 61 72 65 66  ce through caref
4c9e0 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65  ul use of indice
4c9f0 73 2e 3c 2f 64 64 3e 0d 0a 2a 2a 0d 0a 2a 2a 20  s.</dd>..**..** 
4ca00 5b 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  [[SQLITE_STMTSTA
4ca10 54 55 53 5f 41 55 54 4f 49 4e 44 45 58 5d 5d 20  TUS_AUTOINDEX]] 
4ca20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53  <dt>SQLITE_STMTS
4ca30 54 41 54 55 53 5f 41 55 54 4f 49 4e 44 45 58 3c  TATUS_AUTOINDEX<
4ca40 2f 64 74 3e 0d 0a 2a 2a 20 3c 64 64 3e 5e 54 68  /dt>..** <dd>^Th
4ca50 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  is is the number
4ca60 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65   of rows inserte
4ca70 64 20 69 6e 74 6f 20 74 72 61 6e 73 69 65 6e 74  d into transient
4ca80 20 69 6e 64 69 63 65 73 20 74 68 61 74 0d 0a 2a   indices that..*
4ca90 2a 20 77 65 72 65 20 63 72 65 61 74 65 64 20 61  * were created a
4caa0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20  utomatically in 
4cab0 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 20 6a 6f  order to help jo
4cac0 69 6e 73 20 72 75 6e 20 66 61 73 74 65 72 2e 0d  ins run faster..
4cad0 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76  .** A non-zero v
4cae0 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75  alue in this cou
4caf0 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74  nter may indicat
4cb00 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  e an opportunity
4cb10 20 74 6f 0d 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d   to..** improvem
4cb20 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ent performance 
4cb30 62 79 20 61 64 64 69 6e 67 20 70 65 72 6d 61 6e  by adding perman
4cb40 65 6e 74 20 69 6e 64 69 63 65 73 20 74 68 61 74  ent indices that
4cb50 20 64 6f 20 6e 6f 74 0d 0a 2a 2a 20 6e 65 65 64   do not..** need
4cb60 20 74 6f 20 62 65 20 72 65 69 6e 69 74 69 61 6c   to be reinitial
4cb70 69 7a 65 64 20 65 61 63 68 20 74 69 6d 65 20 74  ized each time t
4cb80 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
4cb90 72 75 6e 2e 3c 2f 64 64 3e 0d 0a 2a 2a 20 3c 2f  run.</dd>..** </
4cba0 64 6c 3e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  dl>..*/..#define
4cbb0 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54   SQLITE_STMTSTAT
4cbc0 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50  US_FULLSCAN_STEP
4cbd0 20 20 20 20 20 31 0d 0a 23 64 65 66 69 6e 65 20       1..#define 
4cbe0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
4cbf0 53 5f 53 4f 52 54 20 20 20 20 20 20 20 20 20 20  S_SORT          
4cc00 20 20 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 53      2..#define S
4cc10 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4cc20 5f 41 55 54 4f 49 4e 44 45 58 20 20 20 20 20 20  _AUTOINDEX      
4cc30 20 20 20 33 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43     3..../*..** C
4cc40 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f 6d 20  API3REF: Custom 
4cc50 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a 65 63  Page Cache Objec
4cc60 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71  t..**..** The sq
4cc70 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70  lite3_pcache typ
4cc80 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74  e is opaque.  It
4cc90 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
4cca0 62 79 0d 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67  by..** the plugg
4ccb0 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68  able module.  Th
4ccc0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61  e SQLite core ha
4ccd0 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f  s no knowledge o
4cce0 66 0d 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f  f..** its size o
4ccf0 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63  r internal struc
4cd00 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64  ture and never d
4cd10 65 61 6c 73 20 77 69 74 68 20 74 68 65 0d 0a 2a  eals with the..*
4cd20 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
4cd30 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62   object except b
4cd40 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61  y holding and pa
4cd50 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0d 0a  ssing pointers..
4cd60 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74  ** to the object
4cd70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 5b 73  ...**..** See [s
4cd80 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
4cd90 74 68 6f 64 73 32 5d 20 66 6f 72 20 61 64 64 69  thods2] for addi
4cda0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
4cdb0 6f 6e 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  on...*/..typedef
4cdc0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
4cdd0 70 63 61 63 68 65 20 73 71 6c 69 74 65 33 5f 70  pcache sqlite3_p
4cde0 63 61 63 68 65 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  cache;..../*..**
4cdf0 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f   CAPI3REF: Custo
4ce00 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a  m Page Cache Obj
4ce10 65 63 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ect..**..** The 
4ce20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70  sqlite3_pcache_p
4ce30 61 67 65 20 6f 62 6a 65 63 74 20 72 65 70 72 65  age object repre
4ce40 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 70  sents a single p
4ce50 61 67 65 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 70  age in the..** p
4ce60 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65 20  age cache.  The 
4ce70 70 61 67 65 20 63 61 63 68 65 20 77 69 6c 6c 20  page cache will 
4ce80 61 6c 6c 6f 63 61 74 65 20 69 6e 73 74 61 6e 63  allocate instanc
4ce90 65 73 20 6f 66 20 74 68 69 73 0d 0a 2a 2a 20 6f  es of this..** o
4cea0 62 6a 65 63 74 2e 20 20 56 61 72 69 6f 75 73 20  bject.  Various 
4ceb0 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 70  methods of the p
4cec0 61 67 65 20 63 61 63 68 65 20 75 73 65 20 70 6f  age cache use po
4ced0 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 61 6e  inters to instan
4cee0 63 65 73 0d 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ces..** of this 
4cef0 6f 62 6a 65 63 74 20 61 73 20 70 61 72 61 6d 65  object as parame
4cf00 74 65 72 73 20 6f 72 20 61 73 20 74 68 65 69 72  ters or as their
4cf10 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0d 0a   return value...
4cf20 2a 2a 0d 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69  **..** See [sqli
4cf30 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
4cf40 64 73 32 5d 20 66 6f 72 20 61 64 64 69 74 69 6f  ds2] for additio
4cf50 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
4cf60 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74  ..*/..typedef st
4cf70 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61  ruct sqlite3_pca
4cf80 63 68 65 5f 70 61 67 65 20 73 71 6c 69 74 65 33  che_page sqlite3
4cf90 5f 70 63 61 63 68 65 5f 70 61 67 65 3b 0d 0a 73  _pcache_page;..s
4cfa0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63  truct sqlite3_pc
4cfb0 61 63 68 65 5f 70 61 67 65 20 7b 0d 0a 20 20 76  ache_page {..  v
4cfc0 6f 69 64 20 2a 70 42 75 66 3b 20 20 20 20 20 20  oid *pBuf;      
4cfd0 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74    /* The content
4cfe0 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0d   of the page */.
4cff0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b  .  void *pExtra;
4d000 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 69        /* Extra i
4d010 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63  nformation assoc
4d020 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 70  iated with the p
4d030 61 67 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  age */..};..../*
4d040 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41  ..** CAPI3REF: A
4d050 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e  pplication Defin
4d060 65 64 20 50 61 67 65 20 43 61 63 68 65 2e 0d 0a  ed Page Cache...
4d070 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 61  ** KEYWORDS: {pa
4d080 67 65 20 63 61 63 68 65 7d 0d 0a 2a 2a 0d 0a 2a  ge cache}..**..*
4d090 2a 20 5e 28 54 68 65 20 5b 73 71 6c 69 74 65 33  * ^(The [sqlite3
4d0a0 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
4d0b0 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 32 5d  _CONFIG_PCACHE2]
4d0c0 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65  , ...) interface
4d0d0 20 63 61 6e 0d 0a 2a 2a 20 72 65 67 69 73 74 65   can..** registe
4d0e0 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  r an alternative
4d0f0 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c   page cache impl
4d100 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61  ementation by pa
4d110 73 73 69 6e 67 20 69 6e 20 61 6e 20 0d 0a 2a 2a  ssing in an ..**
4d120 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
4d130 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
4d140 6d 65 74 68 6f 64 73 32 20 73 74 72 75 63 74 75  methods2 structu
4d150 72 65 2e 29 5e 0d 0a 2a 2a 20 49 6e 20 6d 61 6e  re.)^..** In man
4d160 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2c 20  y applications, 
4d170 6d 6f 73 74 20 6f 66 20 74 68 65 20 68 65 61 70  most of the heap
4d180 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 65   memory allocate
4d190 64 20 62 79 20 0d 0a 2a 2a 20 53 51 4c 69 74 65  d by ..** SQLite
4d1a0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 68 65   is used for the
4d1b0 20 70 61 67 65 20 63 61 63 68 65 2e 0d 0a 2a 2a   page cache...**
4d1c0 20 42 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67   By implementing
4d1d0 20 61 20 0d 0a 2a 2a 20 63 75 73 74 6f 6d 20 70   a ..** custom p
4d1e0 61 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 20  age cache using 
4d1f0 74 68 69 73 20 41 50 49 2c 20 61 6e 20 61 70 70  this API, an app
4d200 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 62 65 74  lication can bet
4d210 74 65 72 20 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a 20  ter control..** 
4d220 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
4d230 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79  mory consumed by
4d240 20 53 51 4c 69 74 65 2c 20 74 68 65 20 77 61 79   SQLite, the way
4d250 20 69 6e 20 77 68 69 63 68 20 0d 0a 2a 2a 20 74   in which ..** t
4d260 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c  hat memory is al
4d270 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65  located and rele
4d280 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f  ased, and the po
4d290 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 0d  licies used to .
4d2a0 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78  .** determine ex
4d2b0 61 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74  actly which part
4d2c0 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  s of a database 
4d2d0 66 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20  file are cached 
4d2e0 61 6e 64 20 66 6f 72 20 0d 0a 2a 2a 20 68 6f 77  and for ..** how
4d2f0 20 6c 6f 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54   long...**..** T
4d300 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70  he alternative p
4d310 61 67 65 20 63 61 63 68 65 20 6d 65 63 68 61 6e  age cache mechan
4d320 69 73 6d 20 69 73 20 61 6e 0d 0a 2a 2a 20 65 78  ism is an..** ex
4d330 74 72 65 6d 65 20 6d 65 61 73 75 72 65 20 74 68  treme measure th
4d340 61 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 65  at is only neede
4d350 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 64 65  d by the most de
4d360 6d 61 6e 64 69 6e 67 20 61 70 70 6c 69 63 61 74  manding applicat
4d370 69 6f 6e 73 2e 0d 0a 2a 2a 20 54 68 65 20 62 75  ions...** The bu
4d380 69 6c 74 2d 69 6e 20 70 61 67 65 20 63 61 63 68  ilt-in page cach
4d390 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
4d3a0 20 66 6f 72 20 6d 6f 73 74 20 75 73 65 73 2e 0d   for most uses..
4d3b0 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65 20 63 6f  .**..** ^(The co
4d3c0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71  ntents of the sq
4d3d0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
4d3e0 68 6f 64 73 32 20 73 74 72 75 63 74 75 72 65 20  hods2 structure 
4d3f0 61 72 65 20 63 6f 70 69 65 64 20 74 6f 20 61 6e  are copied to an
4d400 0d 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 62 75  ..** internal bu
4d410 66 66 65 72 20 62 79 20 53 51 4c 69 74 65 20 77  ffer by SQLite w
4d420 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74  ithin the call t
4d430 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
4d440 67 5d 2e 20 20 48 65 6e 63 65 0d 0a 2a 2a 20 74  g].  Hence..** t
4d450 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
4d460 61 79 20 64 69 73 63 61 72 64 20 74 68 65 20 70  ay discard the p
4d470 61 72 61 6d 65 74 65 72 20 61 66 74 65 72 20 74  arameter after t
4d480 68 65 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 5b  he call to..** [
4d490 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
4d4a0 5d 20 72 65 74 75 72 6e 73 2e 29 5e 0d 0a 2a 2a  ] returns.)^..**
4d4b0 0d 0a 2a 2a 20 5b 5b 74 68 65 20 78 49 6e 69 74  ..** [[the xInit
4d4c0 28 29 20 70 61 67 65 20 63 61 63 68 65 20 6d 65  () page cache me
4d4d0 74 68 6f 64 5d 5d 0d 0a 2a 2a 20 5e 28 54 68 65  thod]]..** ^(The
4d4e0 20 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20   xInit() method 
4d4f0 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66  is called once f
4d500 6f 72 20 65 61 63 68 20 65 66 66 65 63 74 69 76  or each effectiv
4d510 65 20 0d 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 5b  e ..** call to [
4d520 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
4d530 7a 65 28 29 5d 29 5e 0d 0a 2a 2a 20 28 75 73 75  ze()])^..** (usu
4d540 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20 64  ally only once d
4d550 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69  uring the lifeti
4d560 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73  me of the proces
4d570 73 29 2e 20 5e 28 54 68 65 20 78 49 6e 69 74 28  s). ^(The xInit(
4d580 29 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20  )..** method is 
4d590 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f 66  passed a copy of
4d5a0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
4d5b0 63 68 65 5f 6d 65 74 68 6f 64 73 32 2e 70 41 72  che_methods2.pAr
4d5c0 67 20 76 61 6c 75 65 2e 29 5e 0d 0a 2a 2a 20 54  g value.)^..** T
4d5d0 68 65 20 69 6e 74 65 6e 74 20 6f 66 20 74 68 65  he intent of the
4d5e0 20 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20   xInit() method 
4d5f0 69 73 20 74 6f 20 73 65 74 20 75 70 20 67 6c 6f  is to set up glo
4d600 62 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75  bal data structu
4d610 72 65 73 20 0d 0a 2a 2a 20 72 65 71 75 69 72 65  res ..** require
4d620 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20  d by the custom 
4d630 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
4d640 6d 65 6e 74 61 74 69 6f 6e 2e 20 0d 0a 2a 2a 20  mentation. ..** 
4d650 5e 28 49 66 20 74 68 65 20 78 49 6e 69 74 28 29  ^(If the xInit()
4d660 20 6d 65 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c   method is NULL,
4d670 20 74 68 65 6e 20 74 68 65 20 0d 0a 2a 2a 20 62   then the ..** b
4d680 75 69 6c 74 2d 69 6e 20 64 65 66 61 75 6c 74 20  uilt-in default 
4d690 70 61 67 65 20 63 61 63 68 65 20 69 73 20 75 73  page cache is us
4d6a0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
4d6b0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65  e application de
4d6c0 66 69 6e 65 64 0d 0a 2a 2a 20 70 61 67 65 20 63  fined..** page c
4d6d0 61 63 68 65 2e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a 20  ache.)^..**..** 
4d6e0 5b 5b 74 68 65 20 78 53 68 75 74 64 6f 77 6e 28  [[the xShutdown(
4d6f0 29 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 74  ) page cache met
4d700 68 6f 64 5d 5d 0d 0a 2a 2a 20 5e 54 68 65 20 78  hod]]..** ^The x
4d710 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f  Shutdown() metho
4d720 64 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 5b  d is called by [
4d730 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
4d740 28 29 5d 2e 0d 0a 2a 2a 20 49 74 20 63 61 6e 20  ()]...** It can 
4d750 62 65 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e  be used to clean
4d760 20 75 70 20 0d 0a 2a 2a 20 61 6e 79 20 6f 75 74   up ..** any out
4d770 73 74 61 6e 64 69 6e 67 20 72 65 73 6f 75 72 63  standing resourc
4d780 65 73 20 62 65 66 6f 72 65 20 70 72 6f 63 65 73  es before proces
4d790 73 20 73 68 75 74 64 6f 77 6e 2c 20 69 66 20 72  s shutdown, if r
4d7a0 65 71 75 69 72 65 64 2e 0d 0a 2a 2a 20 5e 54 68  equired...** ^Th
4d7b0 65 20 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65  e xShutdown() me
4d7c0 74 68 6f 64 20 6d 61 79 20 62 65 20 4e 55 4c 4c  thod may be NULL
4d7d0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51 4c 69 74  ...**..** ^SQLit
4d7e0 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
4d7f0 73 65 72 69 61 6c 69 7a 65 73 20 63 61 6c 6c 73  serializes calls
4d800 20 74 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65   to the xInit me
4d810 74 68 6f 64 2c 0d 0a 2a 2a 20 73 6f 20 74 68 65  thod,..** so the
4d820 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65   xInit method ne
4d830 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64  ed not be thread
4d840 73 61 66 65 2e 20 20 5e 54 68 65 0d 0a 2a 2a 20  safe.  ^The..** 
4d850 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64  xShutdown method
4d860 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
4d870 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68  from [sqlite3_sh
4d880 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20  utdown()] so it 
4d890 64 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20 6e 65 65  does..** not nee
4d8a0 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61  d to be threadsa
4d8b0 66 65 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20  fe either.  All 
4d8c0 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75  other methods mu
4d8d0 73 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65  st be threadsafe
4d8e0 0d 0a 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72  ..** in multithr
4d8f0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
4d900 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51 4c  ns...**..** ^SQL
4d910 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69  ite will never i
4d920 6e 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f  nvoke xInit() mo
4d930 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74  re than once wit
4d940 68 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e  hout an interven
4d950 69 6e 67 0d 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  ing..** call to 
4d960 78 53 68 75 74 64 6f 77 6e 28 29 2e 0d 0a 2a 2a  xShutdown()...**
4d970 0d 0a 2a 2a 20 5b 5b 74 68 65 20 78 43 72 65 61  ..** [[the xCrea
4d980 74 65 28 29 20 70 61 67 65 20 63 61 63 68 65 20  te() page cache 
4d990 6d 65 74 68 6f 64 73 5d 5d 0d 0a 2a 2a 20 5e 53  methods]]..** ^S
4d9a0 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68  QLite invokes th
4d9b0 65 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68  e xCreate() meth
4d9c0 6f 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  od to construct 
4d9d0 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73 74  a new cache inst
4d9e0 61 6e 63 65 2e 0d 0a 2a 2a 20 53 51 4c 69 74 65  ance...** SQLite
4d9f0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
4da00 63 72 65 61 74 65 20 6f 6e 65 20 63 61 63 68 65  create one cache
4da10 20 69 6e 73 74 61 6e 63 65 20 66 6f 72 20 65 61   instance for ea
4da20 63 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  ch open database
4da30 20 66 69 6c 65 2c 0d 0a 2a 2a 20 74 68 6f 75 67   file,..** thoug
4da40 68 20 74 68 69 73 20 69 73 20 6e 6f 74 20 67 75  h this is not gu
4da50 61 72 61 6e 74 65 65 64 2e 20 5e 54 68 65 0d 0a  aranteed. ^The..
4da60 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  ** first paramet
4da70 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74  er, szPage, is t
4da80 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  he size in bytes
4da90 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 68   of the pages th
4daa0 61 74 20 6d 75 73 74 0d 0a 2a 2a 20 62 65 20 61  at must..** be a
4dab0 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
4dac0 63 61 63 68 65 2e 20 20 5e 73 7a 50 61 67 65 20  cache.  ^szPage 
4dad0 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 20 70 6f  will always a po
4dae0 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 5e 54 68  wer of two.  ^Th
4daf0 65 0d 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72  e..** second par
4db00 61 6d 65 74 65 72 20 73 7a 45 78 74 72 61 20 69  ameter szExtra i
4db10 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  s a number of by
4db20 74 65 73 20 6f 66 20 65 78 74 72 61 20 73 74 6f  tes of extra sto
4db30 72 61 67 65 20 0d 0a 2a 2a 20 61 73 73 6f 63 69  rage ..** associ
4db40 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20 70  ated with each p
4db50 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 2e  age cache entry.
4db60 20 20 5e 54 68 65 20 73 7a 45 78 74 72 61 20 70    ^The szExtra p
4db70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c 0d 0a 2a  arameter will..*
4db80 2a 20 61 20 6e 75 6d 62 65 72 20 6c 65 73 73 20  * a number less 
4db90 74 68 61 6e 20 32 35 30 2e 20 20 53 51 4c 69 74  than 250.  SQLit
4dba0 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 0d 0a  e will use the..
4dbb0 2a 2a 20 65 78 74 72 61 20 73 7a 45 78 74 72 61  ** extra szExtra
4dbc0 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70   bytes on each p
4dbd0 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74  age to store met
4dbe0 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20  adata about the 
4dbf0 75 6e 64 65 72 6c 79 69 6e 67 0d 0a 2a 2a 20 64  underlying..** d
4dc00 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20  atabase page on 
4dc10 64 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65  disk.  The value
4dc20 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 7a 45   passed into szE
4dc30 78 74 72 61 20 64 65 70 65 6e 64 73 0d 0a 2a 2a  xtra depends..**
4dc40 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76   on the SQLite v
4dc50 65 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67  ersion, the targ
4dc60 65 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64  et platform, and
4dc70 20 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20   how SQLite was 
4dc80 63 6f 6d 70 69 6c 65 64 2e 0d 0a 2a 2a 20 5e 54  compiled...** ^T
4dc90 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
4dca0 74 20 74 6f 20 78 43 72 65 61 74 65 28 29 2c 20  t to xCreate(), 
4dcb0 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 74  bPurgeable, is t
4dcc0 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 65  rue if the cache
4dcd0 20 62 65 69 6e 67 0d 0a 2a 2a 20 63 72 65 61 74   being..** creat
4dce0 65 64 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  ed will be used 
4dcf0 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73  to cache databas
4dd00 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c  e pages of a fil
4dd10 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b  e stored on disk
4dd20 2c 20 6f 72 0d 0a 2a 2a 20 66 61 6c 73 65 20 69  , or..** false i
4dd30 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72  f it is used for
4dd40 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
4dd50 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68  tabase. The cach
4dd60 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
4dd70 0d 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61  ..** does not ha
4dd80 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ve to do anythin
4dd90 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20  g special based 
4dda0 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f  with the value o
4ddb0 66 20 62 50 75 72 67 65 61 62 6c 65 3b 0d 0a 2a  f bPurgeable;..*
4ddc0 2a 20 69 74 20 69 73 20 70 75 72 65 6c 79 20 61  * it is purely a
4ddd0 64 76 69 73 6f 72 79 2e 20 20 5e 4f 6e 20 61 20  dvisory.  ^On a 
4dde0 63 61 63 68 65 20 77 68 65 72 65 20 62 50 75 72  cache where bPur
4ddf0 67 65 61 62 6c 65 20 69 73 20 66 61 6c 73 65 2c  geable is false,
4de00 20 53 51 4c 69 74 65 20 77 69 6c 6c 0d 0a 2a 2a   SQLite will..**
4de10 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 55   never invoke xU
4de20 6e 70 69 6e 28 29 20 65 78 63 65 70 74 20 74 6f  npin() except to
4de30 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 64 65   deliberately de
4de40 6c 65 74 65 20 61 20 70 61 67 65 2e 0d 0a 2a 2a  lete a page...**
4de50 20 5e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73   ^In other words
4de60 2c 20 63 61 6c 6c 73 20 74 6f 20 78 55 6e 70 69  , calls to xUnpi
4de70 6e 28 29 20 6f 6e 20 61 20 63 61 63 68 65 20 77  n() on a cache w
4de80 69 74 68 20 62 50 75 72 67 65 61 62 6c 65 20 73  ith bPurgeable s
4de90 65 74 20 74 6f 0d 0a 2a 2a 20 66 61 6c 73 65 20  et to..** false 
4dea0 77 69 6c 6c 20 61 6c 77 61 79 73 20 68 61 76 65  will always have
4deb0 20 74 68 65 20 22 64 69 73 63 61 72 64 22 20 66   the "discard" f
4dec0 6c 61 67 20 73 65 74 20 74 6f 20 74 72 75 65 2e  lag set to true.
4ded0 20 20 0d 0a 2a 2a 20 5e 48 65 6e 63 65 2c 20 61    ..** ^Hence, a
4dee0 20 63 61 63 68 65 20 63 72 65 61 74 65 64 20 77   cache created w
4def0 69 74 68 20 62 50 75 72 67 65 61 62 6c 65 20 66  ith bPurgeable f
4df00 61 6c 73 65 20 77 69 6c 6c 0d 0a 2a 2a 20 6e 65  alse will..** ne
4df10 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79 20  ver contain any 
4df20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 2e 0d  unpinned pages..
4df30 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 74 68 65 20 78 43  .**..** [[the xC
4df40 61 63 68 65 73 69 7a 65 28 29 20 70 61 67 65 20  achesize() page 
4df50 63 61 63 68 65 20 6d 65 74 68 6f 64 5d 5d 0d 0a  cache method]]..
4df60 2a 2a 20 5e 28 54 68 65 20 78 43 61 63 68 65 73  ** ^(The xCaches
4df70 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d 61 79  ize() method may
4df80 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e   be called at an
4df90 79 20 74 69 6d 65 20 62 79 20 53 51 4c 69 74 65  y time by SQLite
4dfa0 20 74 6f 20 73 65 74 20 74 68 65 0d 0a 2a 2a 20   to set the..** 
4dfb0 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 6d 75  suggested maximu
4dfc0 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 6e 75  m cache-size (nu
4dfd0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74  mber of pages st
4dfe0 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 61 63  ored by) the cac
4dff0 68 65 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  he..** instance 
4e000 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
4e010 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
4e020 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  is is the value 
4e030 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
4e040 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  ..** the SQLite 
4e050 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73  "[PRAGMA cache_s
4e060 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 29 5e  ize]" command.)^
4e070 20 20 41 73 20 77 69 74 68 20 74 68 65 20 62 50    As with the bP
4e080 75 72 67 65 61 62 6c 65 0d 0a 2a 2a 20 70 61 72  urgeable..** par
4e090 61 6d 65 74 65 72 2c 20 74 68 65 20 69 6d 70 6c  ameter, the impl
4e0a0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f  ementation is no
4e0b0 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f  t required to do
4e0c0 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 20 74   anything with t
4e0d0 68 69 73 0d 0a 2a 2a 20 76 61 6c 75 65 3b 20 69  his..** value; i
4e0e0 74 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e  t is advisory on
4e0f0 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5b 5b 74 68  ly...**..** [[th
4e100 65 20 78 50 61 67 65 63 6f 75 6e 74 28 29 20 70  e xPagecount() p
4e110 61 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64  age cache method
4e120 73 5d 5d 0d 0a 2a 2a 20 54 68 65 20 78 50 61 67  s]]..** The xPag
4e130 65 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20  ecount() method 
4e140 6d 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  must return the 
4e150 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
4e160 63 75 72 72 65 6e 74 6c 79 0d 0a 2a 2a 20 73 74  currently..** st
4e170 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
4e180 65 2c 20 62 6f 74 68 20 70 69 6e 6e 65 64 20 61  e, both pinned a
4e190 6e 64 20 75 6e 70 69 6e 6e 65 64 2e 0d 0a 2a 2a  nd unpinned...**
4e1a0 20 0d 0a 2a 2a 20 5b 5b 74 68 65 20 78 46 65 74   ..** [[the xFet
4e1b0 63 68 28 29 20 70 61 67 65 20 63 61 63 68 65 20  ch() page cache 
4e1c0 6d 65 74 68 6f 64 73 5d 5d 0d 0a 2a 2a 20 54 68  methods]]..** Th
4e1d0 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f  e xFetch() metho
4e1e0 64 20 6c 6f 63 61 74 65 73 20 61 20 70 61 67 65  d locates a page
4e1f0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 61 6e   in the cache an
4e200 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
4e210 74 65 72 20 74 6f 20 0d 0a 2a 2a 20 61 6e 20 73  ter to ..** an s
4e220 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61  qlite3_pcache_pa
4e230 67 65 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69  ge object associ
4e240 61 74 65 64 20 77 69 74 68 20 74 68 61 74 20 70  ated with that p
4e250 61 67 65 2c 20 6f 72 20 61 20 4e 55 4c 4c 20 70  age, or a NULL p
4e260 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 20 54 68 65 20  ointer...** The 
4e270 70 42 75 66 20 65 6c 65 6d 65 6e 74 20 6f 66 20  pBuf element of 
4e280 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 71 6c  the returned sql
4e290 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65  ite3_pcache_page
4e2a0 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 20   object will be 
4e2b0 61 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f  a..** pointer to
4e2c0 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a 50   a buffer of szP
4e2d0 61 67 65 20 62 79 74 65 73 20 75 73 65 64 20 74  age bytes used t
4e2e0 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74  o store the cont
4e2f0 65 6e 74 20 6f 66 20 61 20 0d 0a 2a 2a 20 73 69  ent of a ..** si
4e300 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 70 61  ngle database pa
4e310 67 65 2e 20 20 54 68 65 20 70 45 78 74 72 61 20  ge.  The pExtra 
4e320 65 6c 65 6d 65 6e 74 20 6f 66 20 73 71 6c 69 74  element of sqlit
4e330 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 20 77  e3_pcache_page w
4e340 69 6c 6c 20 62 65 0d 0a 2a 2a 20 61 20 70 6f 69  ill be..** a poi
4e350 6e 74 65 72 20 74 6f 20 74 68 65 20 73 7a 45 78  nter to the szEx
4e360 74 72 61 20 62 79 74 65 73 20 6f 66 20 65 78 74  tra bytes of ext
4e370 72 61 20 73 74 6f 72 61 67 65 20 74 68 61 74 20  ra storage that 
4e380 53 51 4c 69 74 65 20 68 61 73 20 72 65 71 75 65  SQLite has reque
4e390 73 74 65 64 0d 0a 2a 2a 20 66 6f 72 20 65 61 63  sted..** for eac
4e3a0 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  h entry in the p
4e3b0 61 67 65 20 63 61 63 68 65 2e 0d 0a 2a 2a 0d 0a  age cache...**..
4e3c0 2a 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62  ** The page to b
4e3d0 65 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74  e fetched is det
4e3e0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b  ermined by the k
4e3f0 65 79 2e 20 5e 54 68 65 20 6d 69 6e 69 6d 75 6d  ey. ^The minimum
4e400 20 6b 65 79 20 76 61 6c 75 65 0d 0a 2a 2a 20 69   key value..** i
4e410 73 20 31 2e 20 20 41 66 74 65 72 20 69 74 20 68  s 1.  After it h
4e420 61 73 20 62 65 65 6e 20 72 65 74 72 69 65 76 65  as been retrieve
4e430 64 20 75 73 69 6e 67 20 78 46 65 74 63 68 2c 20  d using xFetch, 
4e440 74 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73  the page is cons
4e450 69 64 65 72 65 64 0d 0a 2a 2a 20 74 6f 20 62 65  idered..** to be
4e460 20 22 70 69 6e 6e 65 64 22 2e 0d 0a 2a 2a 0d 0a   "pinned"...**..
4e470 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73  ** If the reques
4e480 74 65 64 20 70 61 67 65 20 69 73 20 61 6c 72 65  ted page is alre
4e490 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65 20  ady in the page 
4e4a0 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 20  cache, then the 
4e4b0 70 61 67 65 20 63 61 63 68 65 0d 0a 2a 2a 20 69  page cache..** i
4e4c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
4e4d0 73 74 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  st return a poin
4e4e0 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
4e4f0 62 75 66 66 65 72 20 77 69 74 68 20 69 74 73 20  buffer with its 
4e500 63 6f 6e 74 65 6e 74 0d 0a 2a 2a 20 69 6e 74 61  content..** inta
4e510 63 74 2e 20 20 49 66 20 74 68 65 20 72 65 71 75  ct.  If the requ
4e520 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f  ested page is no
4e530 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
4e540 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65   cache, then the
4e550 0d 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65  ..** cache imple
4e560 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  mentation should
4e570 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f   use the value o
4e580 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67  f the createFlag
4e590 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74  ..** parameter t
4e5a0 6f 20 68 65 6c 70 20 69 74 20 64 65 74 65 72 6d  o help it determ
4e5b0 69 6e 65 64 20 77 68 61 74 20 61 63 74 69 6f 6e  ined what action
4e5c0 20 74 6f 20 74 61 6b 65 3a 0d 0a 2a 2a 0d 0a 2a   to take:..**..*
4e5d0 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d  * <table border=
4e5e0 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67  1 width=85% alig
4e5f0 6e 3d 63 65 6e 74 65 72 3e 0d 0a 2a 2a 20 3c 74  n=center>..** <t
4e600 72 3e 3c 74 68 3e 20 63 72 65 61 74 65 46 6c 61  r><th> createFla
4e610 67 20 3c 74 68 3e 20 42 65 68 61 76 69 6f 75 72  g <th> Behaviour
4e620 20 77 68 65 6e 20 70 61 67 65 20 69 73 20 6e 6f   when page is no
4e630 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63  t already in cac
4e640 68 65 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  he..** <tr><td> 
4e650 30 20 3c 74 64 3e 20 44 6f 20 6e 6f 74 20 61 6c  0 <td> Do not al
4e660 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67  locate a new pag
4e670 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e  e.  Return NULL.
4e680 0d 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 31 20  ..** <tr><td> 1 
4e690 3c 74 64 3e 20 41 6c 6c 6f 63 61 74 65 20 61 20  <td> Allocate a 
4e6a0 6e 65 77 20 70 61 67 65 20 69 66 20 69 74 20 65  new page if it e
4e6b0 61 73 79 20 61 6e 64 20 63 6f 6e 76 65 6e 69 65  asy and convenie
4e6c0 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 0d 0a 2a 2a  nt to do so...**
4e6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4e6e0 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
4e6f0 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2a 20 3c 74 72 3e  n NULL...** <tr>
4e700 3c 74 64 3e 20 32 20 3c 74 64 3e 20 4d 61 6b 65  <td> 2 <td> Make
4e710 20 65 76 65 72 79 20 65 66 66 6f 72 74 20 74 6f   every effort to
4e720 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
4e730 70 61 67 65 2e 20 20 4f 6e 6c 79 20 72 65 74 75  page.  Only retu
4e740 72 6e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rn..**          
4e750 20 20 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 61         NULL if a
4e760 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20  llocating a new 
4e770 70 61 67 65 20 69 73 20 65 66 66 65 63 74 69 76  page is effectiv
4e780 65 6c 79 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0d  ely impossible..
4e790 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0d 0a 2a 2a  .** </table>..**
4e7a0 0d 0a 2a 2a 20 5e 28 53 51 4c 69 74 65 20 77 69  ..** ^(SQLite wi
4e7b0 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 76 6f  ll normally invo
4e7c0 6b 65 20 78 46 65 74 63 68 28 29 20 77 69 74 68  ke xFetch() with
4e7d0 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66   a createFlag of
4e7e0 20 30 20 6f 72 20 31 2e 20 20 53 51 4c 69 74 65   0 or 1.  SQLite
4e7f0 0d 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 75  ..** will only u
4e800 73 65 20 61 20 63 72 65 61 74 65 46 6c 61 67 20  se a createFlag 
4e810 6f 66 20 32 20 61 66 74 65 72 20 61 20 70 72 69  of 2 after a pri
4e820 6f 72 20 63 61 6c 6c 20 77 69 74 68 20 61 20 63  or call with a c
4e830 72 65 61 74 65 46 6c 61 67 20 6f 66 20 31 0d 0a  reateFlag of 1..
4e840 2a 2a 20 66 61 69 6c 65 64 2e 29 5e 20 20 49 6e  ** failed.)^  In
4e850 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 6f 20   between the to 
4e860 78 46 65 74 63 68 28 29 20 63 61 6c 6c 73 2c 20  xFetch() calls, 
4e870 53 51 4c 69 74 65 20 6d 61 79 0d 0a 2a 2a 20 61  SQLite may..** a
4e880 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20  ttempt to unpin 
4e890 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61 63 68  one or more cach
4e8a0 65 20 70 61 67 65 73 20 62 79 20 73 70 69 6c 6c  e pages by spill
4e8b0 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ing the content 
4e8c0 6f 66 0d 0a 2a 2a 20 70 69 6e 6e 65 64 20 70 61  of..** pinned pa
4e8d0 67 65 73 20 74 6f 20 64 69 73 6b 20 61 6e 64 20  ges to disk and 
4e8e0 73 79 6e 63 68 69 6e 67 20 74 68 65 20 6f 70 65  synching the ope
4e8f0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 69  rating system di
4e900 73 6b 20 63 61 63 68 65 2e 0d 0a 2a 2a 0d 0a 2a  sk cache...**..*
4e910 2a 20 5b 5b 74 68 65 20 78 55 6e 70 69 6e 28 29  * [[the xUnpin()
4e920 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 74 68   page cache meth
4e930 6f 64 5d 5d 0d 0a 2a 2a 20 5e 78 55 6e 70 69 6e  od]]..** ^xUnpin
4e940 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  () is called by 
4e950 53 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f  SQLite with a po
4e960 69 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65  inter to a curre
4e970 6e 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65  ntly pinned page
4e980 0d 0a 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f  ..** as its seco
4e990 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  nd argument.  If
4e9a0 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
4e9b0 65 74 65 72 2c 20 64 69 73 63 61 72 64 2c 20 69  eter, discard, i
4e9c0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0d 0a 2a 2a 20  s non-zero,..** 
4e9d0 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 75  then the page mu
4e9e0 73 74 20 62 65 20 65 76 69 63 74 65 64 20 66 72  st be evicted fr
4e9f0 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0d 0a 2a  om the cache...*
4ea00 2a 20 5e 49 66 20 74 68 65 20 64 69 73 63 61 72  * ^If the discar
4ea10 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 0d 0a  d parameter is..
4ea20 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  ** zero, then th
4ea30 65 20 70 61 67 65 20 6d 61 79 20 62 65 20 64 69  e page may be di
4ea40 73 63 61 72 64 65 64 20 6f 72 20 72 65 74 61 69  scarded or retai
4ea50 6e 65 64 20 61 74 20 74 68 65 20 64 69 73 63 72  ned at the discr
4ea60 65 74 69 6f 6e 20 6f 66 0d 0a 2a 2a 20 70 61 67  etion of..** pag
4ea70 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
4ea80 74 61 74 69 6f 6e 2e 20 5e 54 68 65 20 70 61 67  tation. ^The pag
4ea90 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
4eaa0 74 61 74 69 6f 6e 0d 0a 2a 2a 20 6d 61 79 20 63  tation..** may c
4eab0 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75  hoose to evict u
4eac0 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74  npinned pages at
4ead0 20 61 6e 79 20 74 69 6d 65 2e 0d 0a 2a 2a 0d 0a   any time...**..
4eae0 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75 73  ** The cache mus
4eaf0 74 20 6e 6f 74 20 70 65 72 66 6f 72 6d 20 61 6e  t not perform an
4eb00 79 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  y reference coun
4eb10 74 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0d  ting. A single .
4eb20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70  .** call to xUnp
4eb30 69 6e 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20  in() unpins the 
4eb40 70 61 67 65 20 72 65 67 61 72 64 6c 65 73 73 20  page regardless 
4eb50 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
4eb60 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 0d 0a 2a   prior calls ..*
4eb70 2a 20 74 6f 20 78 46 65 74 63 68 28 29 2e 0d 0a  * to xFetch()...
4eb80 2a 2a 0d 0a 2a 2a 20 5b 5b 74 68 65 20 78 52 65  **..** [[the xRe
4eb90 6b 65 79 28 29 20 70 61 67 65 20 63 61 63 68 65  key() page cache
4eba0 20 6d 65 74 68 6f 64 73 5d 5d 0d 0a 2a 2a 20 54   methods]]..** T
4ebb0 68 65 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68  he xRekey() meth
4ebc0 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 68  od is used to ch
4ebd0 61 6e 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c  ange the key val
4ebe0 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
4ebf0 74 68 20 74 68 65 0d 0a 2a 2a 20 70 61 67 65 20  th the..** page 
4ec00 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
4ec10 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 49  cond argument. I
4ec20 66 20 74 68 65 20 63 61 63 68 65 0d 0a 2a 2a 20  f the cache..** 
4ec30 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61  previously conta
4ec40 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61 73 73  ins an entry ass
4ec50 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77  ociated with new
4ec60 4b 65 79 2c 20 69 74 20 6d 75 73 74 20 62 65 0d  Key, it must be.
4ec70 0a 2a 2a 20 64 69 73 63 61 72 64 65 64 2e 20 5e  .** discarded. ^
4ec80 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68 65 20  Any prior cache 
4ec90 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64  entry associated
4eca0 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69 73 20   with newKey is 
4ecb0 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0d 0a  guaranteed not..
4ecc0 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e  ** to be pinned.
4ecd0 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 53 51  ..**..** When SQ
4ece0 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78  Lite calls the x
4ecf0 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f  Truncate() metho
4ed00 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73  d, the cache mus
4ed10 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0d 0a 2a  t discard all..*
4ed20 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65  * existing cache
4ed30 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61   entries with pa
4ed40 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73  ge numbers (keys
4ed50 29 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  ) greater than o
4ed60 72 20 65 71 75 61 6c 0d 0a 2a 2a 20 74 6f 20 74  r equal..** to t
4ed70 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
4ed80 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72  iLimit parameter
4ed90 20 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e   passed to xTrun
4eda0 63 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0d 0a  cate(). If any..
4edb0 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 67 65  ** of these page
4edc0 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68  s are pinned, th
4edd0 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c  ey are implicitl
4ede0 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e  y unpinned, mean
4edf0 69 6e 67 20 74 68 61 74 0d 0a 2a 2a 20 74 68 65  ing that..** the
4ee00 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20  y can be safely 
4ee10 64 69 73 63 61 72 64 65 64 2e 0d 0a 2a 2a 0d 0a  discarded...**..
4ee20 2a 2a 20 5b 5b 74 68 65 20 78 44 65 73 74 72 6f  ** [[the xDestro
4ee30 79 28 29 20 70 61 67 65 20 63 61 63 68 65 20 6d  y() page cache m
4ee40 65 74 68 6f 64 5d 5d 0d 0a 2a 2a 20 5e 54 68 65  ethod]]..** ^The
4ee50 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68   xDestroy() meth
4ee60 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  od is used to de
4ee70 6c 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c  lete a cache all
4ee80 6f 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74  ocated by xCreat
4ee90 65 28 29 2e 0d 0a 2a 2a 20 41 6c 6c 20 72 65 73  e()...** All res
4eea0 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65  ources associate
4eeb0 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69  d with the speci
4eec0 66 69 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c  fied cache shoul
4eed0 64 20 62 65 20 66 72 65 65 64 2e 20 5e 41 66 74  d be freed. ^Aft
4eee0 65 72 0d 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  er..** calling t
4eef0 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65  he xDestroy() me
4ef00 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e  thod, SQLite con
4ef10 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 6c 69  siders the [sqli
4ef20 74 65 33 5f 70 63 61 63 68 65 2a 5d 0d 0a 2a 2a  te3_pcache*]..**
4ef30 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c   handle invalid,
4ef40 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73   and will not us
4ef50 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f 74  e it with any ot
4ef60 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 63  her sqlite3_pcac
4ef70 68 65 5f 6d 65 74 68 6f 64 73 32 0d 0a 2a 2a 20  he_methods2..** 
4ef80 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a  functions...**..
4ef90 2a 2a 20 5b 5b 74 68 65 20 78 53 68 72 69 6e 6b  ** [[the xShrink
4efa0 28 29 20 70 61 67 65 20 63 61 63 68 65 20 6d 65  () page cache me
4efb0 74 68 6f 64 5d 5d 0d 0a 2a 2a 20 5e 53 51 4c 69  thod]]..** ^SQLi
4efc0 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78  te invokes the x
4efd0 53 68 72 69 6e 6b 28 29 20 6d 65 74 68 6f 64 20  Shrink() method 
4efe0 77 68 65 6e 20 69 74 20 77 61 6e 74 73 20 74 68  when it wants th
4eff0 65 20 70 61 67 65 20 63 61 63 68 65 20 74 6f 0d  e page cache to.
4f000 0a 2a 2a 20 66 72 65 65 20 75 70 20 61 73 20 6d  .** free up as m
4f010 75 63 68 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f  uch of heap memo
4f020 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 20  ry as possible. 
4f030 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
4f040 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a  implementation..
4f050 2a 2a 20 69 73 20 6e 6f 74 20 6f 62 6c 69 67 61  ** is not obliga
4f060 74 65 64 20 74 6f 20 66 72 65 65 20 61 6e 79 20  ted to free any 
4f070 6d 65 6d 6f 72 79 2c 20 62 75 74 20 77 65 6c 6c  memory, but well
4f080 2d 62 65 68 61 76 65 64 20 69 6d 70 6c 65 6d 65  -behaved impleme
4f090 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 0d  ntations should.
4f0a0 0a 2a 2a 20 64 6f 20 74 68 65 69 72 20 62 65 73  .** do their bes
4f0b0 74 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20  t...*/..typedef 
4f0c0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70  struct sqlite3_p
4f0d0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 73  cache_methods2 s
4f0e0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
4f0f0 74 68 6f 64 73 32 3b 0d 0a 73 74 72 75 63 74 20  thods2;..struct 
4f100 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
4f110 65 74 68 6f 64 73 32 20 7b 0d 0a 20 20 69 6e 74  ethods2 {..  int
4f120 20 69 56 65 72 73 69 6f 6e 3b 0d 0a 20 20 76 6f   iVersion;..  vo
4f130 69 64 20 2a 70 41 72 67 3b 0d 0a 20 20 69 6e 74  id *pArg;..  int
4f140 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29   (*xInit)(void*)
4f150 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75  ;..  void (*xShu
4f160 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0d 0a  tdown)(void*);..
4f170 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
4f180 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74   *(*xCreate)(int
4f190 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 73 7a 45   szPage, int szE
4f1a0 78 74 72 61 2c 20 69 6e 74 20 62 50 75 72 67 65  xtra, int bPurge
4f1b0 61 62 6c 65 29 3b 0d 0a 20 20 76 6f 69 64 20 28  able);..  void (
4f1c0 2a 78 43 61 63 68 65 73 69 7a 65 29 28 73 71 6c  *xCachesize)(sql
4f1d0 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e  ite3_pcache*, in
4f1e0 74 20 6e 43 61 63 68 65 73 69 7a 65 29 3b 0d 0a  t nCachesize);..
4f1f0 20 20 69 6e 74 20 28 2a 78 50 61 67 65 63 6f 75    int (*xPagecou
4f200 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  nt)(sqlite3_pcac
4f210 68 65 2a 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  he*);..  sqlite3
4f220 5f 70 63 61 63 68 65 5f 70 61 67 65 20 2a 28 2a  _pcache_page *(*
4f230 78 46 65 74 63 68 29 28 73 71 6c 69 74 65 33 5f  xFetch)(sqlite3_
4f240 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65  pcache*, unsigne
4f250 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 65 61 74  d key, int creat
4f260 65 46 6c 61 67 29 3b 0d 0a 20 20 76 6f 69 64 20  eFlag);..  void 
4f270 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 74 65  (*xUnpin)(sqlite
4f280 33 5f 70 63 61 63 68 65 2a 2c 20 73 71 6c 69 74  3_pcache*, sqlit
4f290 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 2a 2c  e3_pcache_page*,
4f2a0 20 69 6e 74 20 64 69 73 63 61 72 64 29 3b 0d 0a   int discard);..
4f2b0 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 79 29    void (*xRekey)
4f2c0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
4f2d0 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  , sqlite3_pcache
4f2e0 5f 70 61 67 65 2a 2c 20 0d 0a 20 20 20 20 20 20  _page*, ..      
4f2f0 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c  unsigned oldKey,
4f300 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79   unsigned newKey
4f310 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78 54 72  );..  void (*xTr
4f320 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f  uncate)(sqlite3_
4f330 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65  pcache*, unsigne
4f340 64 20 69 4c 69 6d 69 74 29 3b 0d 0a 20 20 76 6f  d iLimit);..  vo
4f350 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73  id (*xDestroy)(s
4f360 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b  qlite3_pcache*);
4f370 0d 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 72 69  ..  void (*xShri
4f380 6e 6b 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  nk)(sqlite3_pcac
4f390 68 65 2a 29 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  he*);..};..../*.
4f3a0 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
4f3b0 6f 62 73 6f 6c 65 74 65 20 70 63 61 63 68 65 5f  obsolete pcache_
4f3c0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74  methods object t
4f3d0 68 61 74 20 68 61 73 20 6e 6f 77 20 62 65 65 6e  hat has now been
4f3e0 20 72 65 70 6c 61 63 65 64 0d 0a 2a 2a 20 62 79   replaced..** by
4f3f0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
4f400 6d 65 74 68 6f 64 73 32 2e 20 20 54 68 69 73 20  methods2.  This 
4f410 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 75 73  object is not us
4f420 65 64 20 62 79 20 53 51 4c 69 74 65 2e 20 20 49  ed by SQLite.  I
4f430 74 20 69 73 0d 0a 2a 2a 20 72 65 74 61 69 6e 65  t is..** retaine
4f440 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  d in the header 
4f450 66 69 6c 65 20 66 6f 72 20 62 61 63 6b 77 61 72  file for backwar
4f460 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
4f470 20 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 74 79 70 65   only...*/..type
4f480 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
4f490 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
4f4a0 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  s sqlite3_pcache
4f4b0 5f 6d 65 74 68 6f 64 73 3b 0d 0a 73 74 72 75 63  _methods;..struc
4f4c0 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
4f4d0 5f 6d 65 74 68 6f 64 73 20 7b 0d 0a 20 20 76 6f  _methods {..  vo
4f4e0 69 64 20 2a 70 41 72 67 3b 0d 0a 20 20 69 6e 74  id *pArg;..  int
4f4f0 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29   (*xInit)(void*)
4f500 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75  ;..  void (*xShu
4f510 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0d 0a  tdown)(void*);..
4f520 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
4f530 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74   *(*xCreate)(int
4f540 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75   szPage, int bPu
4f550 72 67 65 61 62 6c 65 29 3b 0d 0a 20 20 76 6f 69  rgeable);..  voi
4f560 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 28  d (*xCachesize)(
4f570 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
4f580 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 29   int nCachesize)
4f590 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65  ;..  int (*xPage
4f5a0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70  count)(sqlite3_p
4f5b0 63 61 63 68 65 2a 29 3b 0d 0a 20 20 76 6f 69 64  cache*);..  void
4f5c0 20 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69   *(*xFetch)(sqli
4f5d0 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73  te3_pcache*, uns
4f5e0 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63  igned key, int c
4f5f0 72 65 61 74 65 46 6c 61 67 29 3b 0d 0a 20 20 76  reateFlag);..  v
4f600 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71  oid (*xUnpin)(sq
4f610 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76  lite3_pcache*, v
4f620 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72  oid*, int discar
4f630 64 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 78 52  d);..  void (*xR
4f640 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70 63  ekey)(sqlite3_pc
4f650 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e  ache*, void*, un
4f660 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75  signed oldKey, u
4f670 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b  nsigned newKey);
4f680 0d 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e  ..  void (*xTrun
4f690 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63  cate)(sqlite3_pc
4f6a0 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20  ache*, unsigned 
4f6b0 69 4c 69 6d 69 74 29 3b 0d 0a 20 20 76 6f 69 64  iLimit);..  void
4f6c0 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c   (*xDestroy)(sql
4f6d0 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0d 0a  ite3_pcache*);..
4f6e0 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  };....../*..** C
4f6f0 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20  API3REF: Online 
4f700 42 61 63 6b 75 70 20 4f 62 6a 65 63 74 0d 0a 2a  Backup Object..*
4f710 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  *..** The sqlite
4f720 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20  3_backup object 
4f730 72 65 63 6f 72 64 73 20 73 74 61 74 65 20 69 6e  records state in
4f740 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
4f750 61 6e 20 6f 6e 67 6f 69 6e 67 0d 0a 2a 2a 20 6f  an ongoing..** o
4f760 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f 70 65  nline backup ope
4f770 72 61 74 69 6f 6e 2e 20 20 5e 54 68 65 20 73 71  ration.  ^The sq
4f780 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
4f790 65 63 74 20 69 73 20 63 72 65 61 74 65 64 20 62  ect is created b
4f7a0 79 0d 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20  y..** a call to 
4f7b0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  [sqlite3_backup_
4f7c0 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64  init()] and is d
4f7d0 65 73 74 72 6f 79 65 64 20 62 79 20 61 20 63 61  estroyed by a ca
4f7e0 6c 6c 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69 74  ll to..** [sqlit
4f7f0 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
4f800 28 29 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  ()]...**..** See
4f810 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68   Also: [Using th
4f820 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20  e SQLite Online 
4f830 42 61 63 6b 75 70 20 41 50 49 5d 0d 0a 2a 2f 0d  Backup API]..*/.
4f840 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4f850 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73  sqlite3_backup s
4f860 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0d 0a  qlite3_backup;..
4f870 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
4f880 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70  F: Online Backup
4f890 20 41 50 49 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68   API...**..** Th
4f8a0 65 20 62 61 63 6b 75 70 20 41 50 49 20 63 6f 70  e backup API cop
4f8b0 69 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ies the content 
4f8c0 6f 66 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  of one database 
4f8d0 69 6e 74 6f 20 61 6e 6f 74 68 65 72 2e 0d 0a 2a  into another...*
4f8e0 2a 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 65  * It is useful e
4f8f0 69 74 68 65 72 20 66 6f 72 20 63 72 65 61 74 69  ither for creati
4f900 6e 67 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61  ng backups of da
4f910 74 61 62 61 73 65 73 20 6f 72 0d 0a 2a 2a 20 66  tabases or..** f
4f920 6f 72 20 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65  or copying in-me
4f930 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 74  mory databases t
4f940 6f 20 6f 72 20 66 72 6f 6d 20 70 65 72 73 69 73  o or from persis
4f950 74 65 6e 74 20 66 69 6c 65 73 2e 20 0d 0a 2a 2a  tent files. ..**
4f960 0d 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b  ..** See Also: [
4f970 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65  Using the SQLite
4f980 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41   Online Backup A
4f990 50 49 5d 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 53 51 4c  PI]..**..** ^SQL
4f9a0 69 74 65 20 68 6f 6c 64 73 20 61 20 77 72 69 74  ite holds a writ
4f9b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  e transaction op
4f9c0 65 6e 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e  en on the destin
4f9d0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 66  ation database f
4f9e0 69 6c 65 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ile..** for the 
4f9f0 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  duration of the 
4fa00 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
4fa10 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73 6f 75 72 63  ...** ^The sourc
4fa20 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  e database is re
4fa30 61 64 2d 6c 6f 63 6b 65 64 20 6f 6e 6c 79 20 77  ad-locked only w
4fa40 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67  hile it is being
4fa50 20 72 65 61 64 3b 0d 0a 2a 2a 20 69 74 20 69 73   read;..** it is
4fa60 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 63 6f 6e 74   not locked cont
4fa70 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65  inuously for the
4fa80 20 65 6e 74 69 72 65 20 62 61 63 6b 75 70 20 6f   entire backup o
4fa90 70 65 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 20 5e 54  peration...** ^T
4faa0 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70 20  hus, the backup 
4fab0 6d 61 79 20 62 65 20 70 65 72 66 6f 72 6d 65 64  may be performed
4fac0 20 6f 6e 20 61 20 6c 69 76 65 20 73 6f 75 72 63   on a live sourc
4fad0 65 20 64 61 74 61 62 61 73 65 20 77 69 74 68 6f  e database witho
4fae0 75 74 0d 0a 2a 2a 20 70 72 65 76 65 6e 74 69 6e  ut..** preventin
4faf0 67 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  g other database
4fb00 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f   connections fro
4fb10 6d 0d 0a 2a 2a 20 72 65 61 64 69 6e 67 20 6f 72  m..** reading or
4fb20 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
4fb30 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
4fb40 77 68 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70  while the backup
4fb50 20 69 73 20 75 6e 64 65 72 77 61 79 2e 0d 0a 2a   is underway...*
4fb60 2a 20 0d 0a 2a 2a 20 5e 28 54 6f 20 70 65 72 66  * ..** ^(To perf
4fb70 6f 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70 65  orm a backup ope
4fb80 72 61 74 69 6f 6e 3a 20 0d 0a 2a 2a 20 20 20 3c  ration: ..**   <
4fb90 6f 6c 3e 0d 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e  ol>..**     <li>
4fba0 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
4fbb0 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20  p_init()</b> is 
4fbc0 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69  called once to i
4fbd0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 0d 0a 2a  nitialize the..*
4fbe0 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b 75 70  *         backup
4fbf0 2c 20 0d 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c  , ..**     <li><
4fc00 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
4fc10 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20 63  _step()</b> is c
4fc20 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  alled one or mor
4fc30 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e 73  e times to trans
4fc40 66 65 72 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20  fer ..**        
4fc50 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65   the data betwee
4fc60 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61  n the two databa
4fc70 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79  ses, and finally
4fc80 0d 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e  ..**     <li><b>
4fc90 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
4fca0 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63  inish()</b> is c
4fcb0 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65  alled to release
4fcc0 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0d   all resources .
4fcd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f  .**         asso
4fce0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
4fcf0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
4fd00 2e 20 0d 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 29 5e  . ..**   </ol>)^
4fd10 0d 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c  ..** There shoul
4fd20 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65  d be exactly one
4fd30 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
4fd40 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
4fd50 20 66 6f 72 20 65 61 63 68 0d 0a 2a 2a 20 73 75   for each..** su
4fd60 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
4fd70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
4fd80 69 6e 69 74 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  init()...**..** 
4fd90 5b 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  [[sqlite3_backup
4fda0 5f 69 6e 69 74 28 29 5d 5d 20 3c 62 3e 73 71 6c  _init()]] <b>sql
4fdb0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
4fdc0 28 29 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  ()</b>..**..** ^
4fdd0 54 68 65 20 44 20 61 6e 64 20 4e 20 61 72 67 75  The D and N argu
4fde0 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65 33  ments to sqlite3
4fdf0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 44 2c 4e  _backup_init(D,N
4fe00 2c 53 2c 4d 29 20 61 72 65 20 74 68 65 20 0d 0a  ,S,M) are the ..
4fe10 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
4fe20 6e 65 63 74 69 6f 6e 5d 20 61 73 73 6f 63 69 61  nection] associa
4fe30 74 65 64 20 77 69 74 68 20 74 68 65 20 64 65 73  ted with the des
4fe40 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
4fe50 65 20 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  e ..** and the d
4fe60 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 72 65  atabase name, re
4fe70 73 70 65 63 74 69 76 65 6c 79 2e 0d 0a 2a 2a 20  spectively...** 
4fe80 5e 54 68 65 20 64 61 74 61 62 61 73 65 20 6e 61  ^The database na
4fe90 6d 65 20 69 73 20 22 6d 61 69 6e 22 20 66 6f 72  me is "main" for
4fea0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
4feb0 73 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20 74  se, "temp" for t
4fec0 68 65 0d 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79  he..** temporary
4fed0 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 74 68   database, or th
4fee0 65 20 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64  e name specified
4fef0 20 61 66 74 65 72 20 74 68 65 20 41 53 20 6b 65   after the AS ke
4ff00 79 77 6f 72 64 20 69 6e 0d 0a 2a 2a 20 61 6e 20  yword in..** an 
4ff10 5b 41 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65  [ATTACH] stateme
4ff20 6e 74 20 66 6f 72 20 61 6e 20 61 74 74 61 63 68  nt for an attach
4ff30 65 64 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2a  ed database...**
4ff40 20 5e 54 68 65 20 53 20 61 6e 64 20 4d 20 61 72   ^The S and M ar
4ff50 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74  guments passed t
4ff60 6f 20 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  o ..** sqlite3_b
4ff70 61 63 6b 75 70 5f 69 6e 69 74 28 44 2c 4e 2c 53  ackup_init(D,N,S
4ff80 2c 4d 29 20 69 64 65 6e 74 69 66 79 20 74 68 65  ,M) identify the
4ff90 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
4ffa0 63 74 69 6f 6e 5d 0d 0a 2a 2a 20 61 6e 64 20 64  ction]..** and d
4ffb0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 66 20  atabase name of 
4ffc0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
4ffd0 61 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ase, respectivel
4ffe0 79 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73 6f 75 72  y...** ^The sour
4fff0 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
50000 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
50010 6e 65 63 74 69 6f 6e 73 5d 20 28 70 61 72 61 6d  nections] (param
50020 65 74 65 72 73 20 53 20 61 6e 64 20 44 29 0d 0a  eters S and D)..
50030 2a 2a 20 6d 75 73 74 20 62 65 20 64 69 66 66 65  ** must be diffe
50040 72 65 6e 74 20 6f 72 20 65 6c 73 65 20 73 71 6c  rent or else sql
50050 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
50060 28 44 2c 4e 2c 53 2c 4d 29 20 77 69 6c 6c 20 66  (D,N,S,M) will f
50070 61 69 6c 20 77 69 74 68 0d 0a 2a 2a 20 61 6e 20  ail with..** an 
50080 65 72 72 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  error...**..** ^
50090 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
500a0 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  rs within sqlite
500b0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 44 2c  3_backup_init(D,
500c0 4e 2c 53 2c 4d 29 2c 20 74 68 65 6e 20 4e 55 4c  N,S,M), then NUL
500d0 4c 20 69 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 65  L is..** returne
500e0 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63  d and an error c
500f0 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65  ode and error me
50100 73 73 61 67 65 20 61 72 65 20 73 74 6f 72 65 64  ssage are stored
50110 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 64 65 73 74   in the..** dest
50120 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73  ination [databas
50130 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e  e connection] D.
50140 0d 0a 2a 2a 20 5e 54 68 65 20 65 72 72 6f 72 20  ..** ^The error 
50150 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65  code and message
50160 20 66 6f 72 20 74 68 65 20 66 61 69 6c 65 64 20   for the failed 
50170 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
50180 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 0d 0a 2a  backup_init()..*
50190 2a 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 76  * can be retriev
501a0 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71  ed using the [sq
501b0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
501c0 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  , [sqlite3_errms
501d0 67 28 29 5d 2c 20 61 6e 64 2f 6f 72 0d 0a 2a 2a  g()], and/or..**
501e0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
501f0 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e 73 2e  16()] functions.
50200 0d 0a 2a 2a 20 5e 41 20 73 75 63 63 65 73 73 66  ..** ^A successf
50210 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ul call to sqlit
50220 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
50230 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
50240 65 72 20 74 6f 20 61 6e 0d 0a 2a 2a 20 5b 73 71  er to an..** [sq
50250 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62  lite3_backup] ob
50260 6a 65 63 74 2e 0d 0a 2a 2a 20 5e 54 68 65 20 5b  ject...** ^The [
50270 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20  sqlite3_backup] 
50280 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 75 73  object may be us
50290 65 64 20 77 69 74 68 20 74 68 65 20 73 71 6c 69  ed with the sqli
502a0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
502b0 29 20 61 6e 64 0d 0a 2a 2a 20 73 71 6c 69 74 65  ) and..** sqlite
502c0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
502d0 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70  ) functions to p
502e0 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63 69  erform the speci
502f0 66 69 65 64 20 62 61 63 6b 75 70 20 0d 0a 2a 2a  fied backup ..**
50300 20 6f 70 65 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d   operation...**.
50310 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 5f 62 61  .** [[sqlite3_ba
50320 63 6b 75 70 5f 73 74 65 70 28 29 5d 5d 20 3c 62  ckup_step()]] <b
50330 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  >sqlite3_backup_
50340 73 74 65 70 28 29 3c 2f 62 3e 0d 0a 2a 2a 0d 0a  step()</b>..**..
50350 2a 2a 20 5e 46 75 6e 63 74 69 6f 6e 20 73 71 6c  ** ^Function sql
50360 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
50370 28 42 2c 4e 29 20 77 69 6c 6c 20 63 6f 70 79 20  (B,N) will copy 
50380 75 70 20 74 6f 20 4e 20 70 61 67 65 73 20 62 65  up to N pages be
50390 74 77 65 65 6e 20 0d 0a 2a 2a 20 74 68 65 20 73  tween ..** the s
503a0 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
503b0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20  ation databases 
503c0 73 70 65 63 69 66 69 65 64 20 62 79 20 5b 73 71  specified by [sq
503d0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62  lite3_backup] ob
503e0 6a 65 63 74 20 42 2e 0d 0a 2a 2a 20 5e 49 66 20  ject B...** ^If 
503f0 4e 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 61  N is negative, a
50400 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75  ll remaining sou
50410 72 63 65 20 70 61 67 65 73 20 61 72 65 20 63 6f  rce pages are co
50420 70 69 65 64 2e 20 0d 0a 2a 2a 20 5e 49 66 20 73  pied. ..** ^If s
50430 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
50440 65 70 28 42 2c 4e 29 20 73 75 63 63 65 73 73 66  ep(B,N) successf
50450 75 6c 6c 79 20 63 6f 70 69 65 73 20 4e 20 70 61  ully copies N pa
50460 67 65 73 20 61 6e 64 20 74 68 65 72 65 0d 0a 2a  ges and there..*
50470 2a 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65  * are still more
50480 20 70 61 67 65 73 20 74 6f 20 62 65 20 63 6f 70   pages to be cop
50490 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 75  ied, then the fu
504a0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b  nction returns [
504b0 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0d 0a 2a 2a 20  SQLITE_OK]...** 
504c0 5e 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ^If sqlite3_back
504d0 75 70 5f 73 74 65 70 28 42 2c 4e 29 20 73 75 63  up_step(B,N) suc
504e0 63 65 73 73 66 75 6c 6c 79 20 66 69 6e 69 73 68  cessfully finish
504f0 65 73 20 63 6f 70 79 69 6e 67 20 61 6c 6c 20 70  es copying all p
50500 61 67 65 73 0d 0a 2a 2a 20 66 72 6f 6d 20 73 6f  ages..** from so
50510 75 72 63 65 20 74 6f 20 64 65 73 74 69 6e 61 74  urce to destinat
50520 69 6f 6e 2c 20 74 68 65 6e 20 69 74 20 72 65 74  ion, then it ret
50530 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  urns [SQLITE_DON
50540 45 5d 2e 0d 0a 2a 2a 20 5e 49 66 20 61 6e 20 65  E]...** ^If an e
50550 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
50560 65 20 72 75 6e 6e 69 6e 67 20 73 71 6c 69 74 65  e running sqlite
50570 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 42 2c  3_backup_step(B,
50580 4e 29 2c 0d 0a 2a 2a 20 74 68 65 6e 20 61 6e 20  N),..** then an 
50590 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
505a0 72 65 74 75 72 6e 65 64 2e 20 5e 41 73 20 77 65  returned. ^As we
505b0 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b  ll as [SQLITE_OK
505c0 5d 20 61 6e 64 0d 0a 2a 2a 20 5b 53 51 4c 49 54  ] and..** [SQLIT
505d0 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20  E_DONE], a call 
505e0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
505f0 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 65 74  p_step() may ret
50600 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44  urn [SQLITE_READ
50610 4f 4e 4c 59 5d 2c 0d 0a 2a 2a 20 5b 53 51 4c 49  ONLY],..** [SQLI
50620 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49  TE_NOMEM], [SQLI
50630 54 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49 54  TE_BUSY], [SQLIT
50640 45 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 6e  E_LOCKED], or an
50650 0d 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ..** [SQLITE_IOE
50660 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49  RR_ACCESS | SQLI
50670 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78  TE_IOERR_XXX] ex
50680 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
50690 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65  e...**..** ^(The
506a0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
506b0 73 74 65 70 28 29 20 6d 69 67 68 74 20 72 65 74  step() might ret
506c0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44  urn [SQLITE_READ
506d0 4f 4e 4c 59 5d 20 69 66 0d 0a 2a 2a 20 3c 6f 6c  ONLY] if..** <ol
506e0 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 74 68 65 20 64  >..** <li> the d
506f0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
50700 61 73 65 20 77 61 73 20 6f 70 65 6e 65 64 20 72  ase was opened r
50710 65 61 64 2d 6f 6e 6c 79 2c 20 6f 72 0d 0a 2a 2a  ead-only, or..**
50720 20 3c 6c 69 3e 20 74 68 65 20 64 65 73 74 69 6e   <li> the destin
50730 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69  ation database i
50740 73 20 75 73 69 6e 67 20 77 72 69 74 65 2d 61 68  s using write-ah
50750 65 61 64 2d 6c 6f 67 20 6a 6f 75 72 6e 61 6c 69  ead-log journali
50760 6e 67 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  ng..** and the d
50770 65 73 74 69 6e 61 74 69 6f 6e 20 61 6e 64 20 73  estination and s
50780 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 73  ource page sizes
50790 20 64 69 66 66 65 72 2c 20 6f 72 0d 0a 2a 2a 20   differ, or..** 
507a0 3c 6c 69 3e 20 74 68 65 20 64 65 73 74 69 6e 61  <li> the destina
507b0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73  tion database is
507c0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
507d0 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0d 0a  tabase and the..
507e0 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61  ** destination a
507f0 6e 64 20 73 6f 75 72 63 65 20 70 61 67 65 20 73  nd source page s
50800 69 7a 65 73 20 64 69 66 66 65 72 2e 0d 0a 2a 2a  izes differ...**
50810 20 3c 2f 6f 6c 3e 29 5e 0d 0a 2a 2a 0d 0a 2a 2a   </ol>)^..**..**
50820 20 5e 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63   ^If sqlite3_bac
50830 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f  kup_step() canno
50840 74 20 6f 62 74 61 69 6e 20 61 20 72 65 71 75 69  t obtain a requi
50850 72 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  red file-system 
50860 6c 6f 63 6b 2c 20 74 68 65 6e 0d 0a 2a 2a 20 74  lock, then..** t
50870 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  he [sqlite3_busy
50880 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d  _handler | busy-
50890 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e  handler function
508a0 5d 0d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64  ]..** is invoked
508b0 20 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63   (if one is spec
508c0 69 66 69 65 64 29 2e 20 5e 49 66 20 74 68 65 20  ified). ^If the 
508d0 0d 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65  ..** busy-handle
508e0 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  r returns non-ze
508f0 72 6f 20 62 65 66 6f 72 65 20 74 68 65 20 6c 6f  ro before the lo
50900 63 6b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c  ck is available,
50910 20 74 68 65 6e 20 0d 0a 2a 2a 20 5b 53 51 4c 49   then ..** [SQLI
50920 54 45 5f 42 55 53 59 5d 20 69 73 20 72 65 74 75  TE_BUSY] is retu
50930 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
50940 65 72 2e 20 5e 49 6e 20 74 68 69 73 20 63 61 73  er. ^In this cas
50950 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 0d 0a 2a  e the call to..*
50960 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
50970 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72  _step() can be r
50980 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20 5e 49  etried later. ^I
50990 66 20 74 68 65 20 73 6f 75 72 63 65 0d 0a 2a 2a  f the source..**
509a0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
509b0 63 74 69 6f 6e 5d 0d 0a 2a 2a 20 69 73 20 62 65  ction]..** is be
509c0 69 6e 67 20 75 73 65 64 20 74 6f 20 77 72 69 74  ing used to writ
509d0 65 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20  e to the source 
509e0 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 73 71  database when sq
509f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
50a00 70 28 29 0d 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  p()..** is calle
50a10 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  d, then [SQLITE_
50a20 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72  LOCKED] is retur
50a30 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  ned immediately.
50a40 20 5e 41 67 61 69 6e 2c 20 69 6e 20 74 68 69 73   ^Again, in this
50a50 0d 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61  ..** case the ca
50a60 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
50a70 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20  ckup_step() can 
50a80 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72  be retried later
50a90 20 6f 6e 2e 20 5e 28 49 66 0d 0a 2a 2a 20 5b 53   on. ^(If..** [S
50aa0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45  QLITE_IOERR_ACCE
50ab0 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52  SS | SQLITE_IOER
50ac0 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f  R_XXX], [SQLITE_
50ad0 4e 4f 4d 45 4d 5d 2c 20 6f 72 0d 0a 2a 2a 20 5b  NOMEM], or..** [
50ae0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
50af0 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
50b00 65 6e 20 0d 0a 2a 2a 20 74 68 65 72 65 20 69 73  en ..** there is
50b10 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 74   no point in ret
50b20 72 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74  rying the call t
50b30 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
50b40 5f 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 0d  _step(). These .
50b50 0a 2a 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63  .** errors are c
50b60 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e  onsidered fatal.
50b70 29 5e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74  )^  The applicat
50b80 69 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74 20  ion must accept 
50b90 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61  ..** that the ba
50ba0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68  ckup operation h
50bb0 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61  as failed and pa
50bc0 73 73 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  ss the backup op
50bd0 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0d  eration handle .
50be0 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74  .** to the sqlit
50bf0 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
50c00 28 29 20 74 6f 20 72 65 6c 65 61 73 65 20 61 73  () to release as
50c10 73 6f 63 69 61 74 65 64 20 72 65 73 6f 75 72 63  sociated resourc
50c20 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  es...**..** ^The
50c30 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73   first call to s
50c40 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
50c50 65 70 28 29 20 6f 62 74 61 69 6e 73 20 61 6e 20  ep() obtains an 
50c60 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0d 0a  exclusive lock..
50c70 2a 2a 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e  ** on the destin
50c80 61 74 69 6f 6e 20 66 69 6c 65 2e 20 5e 54 68 65  ation file. ^The
50c90 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
50ca0 69 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20  is not released 
50cb0 75 6e 74 69 6c 20 65 69 74 68 65 72 20 0d 0a 2a  until either ..*
50cc0 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
50cd0 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63 61 6c  _finish() is cal
50ce0 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63 6b 75  led or the backu
50cf0 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 63  p operation is c
50d00 6f 6d 70 6c 65 74 65 20 0d 0a 2a 2a 20 61 6e 64  omplete ..** and
50d10 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
50d20 73 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 5b  step() returns [
50d30 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 5e  SQLITE_DONE].  ^
50d40 45 76 65 72 79 20 63 61 6c 6c 20 74 6f 0d 0a 2a  Every call to..*
50d50 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
50d60 5f 73 74 65 70 28 29 20 6f 62 74 61 69 6e 73 20  _step() obtains 
50d70 61 20 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20  a [shared lock] 
50d80 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  on the source da
50d90 74 61 62 61 73 65 20 74 68 61 74 0d 0a 2a 2a 20  tabase that..** 
50da0 6c 61 73 74 73 20 66 6f 72 20 74 68 65 20 64 75  lasts for the du
50db0 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  ration of the sq
50dc0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
50dd0 70 28 29 20 63 61 6c 6c 2e 0d 0a 2a 2a 20 5e 42  p() call...** ^B
50de0 65 63 61 75 73 65 20 74 68 65 20 73 6f 75 72 63  ecause the sourc
50df0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f  e database is no
50e00 74 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65 6e  t locked between
50e10 20 63 61 6c 6c 73 20 74 6f 0d 0a 2a 2a 20 73 71   calls to..** sq
50e20 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
50e30 70 28 29 2c 20 74 68 65 20 73 6f 75 72 63 65 20  p(), the source 
50e40 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 20  database may be 
50e50 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79  modified mid-way
50e60 0d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  ..** through the
50e70 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 2e   backup process.
50e80 20 20 5e 49 66 20 74 68 65 20 73 6f 75 72 63 65    ^If the source
50e90 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64   database is mod
50ea0 69 66 69 65 64 20 62 79 20 61 6e 0d 0a 2a 2a 20  ified by an..** 
50eb0 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73  external process
50ec0 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 61   or via a databa
50ed0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74  se connection ot
50ee0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65  her than the one
50ef0 20 62 65 69 6e 67 0d 0a 2a 2a 20 75 73 65 64 20   being..** used 
50f00 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  by the backup op
50f10 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  eration, then th
50f20 65 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65  e backup will be
50f30 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0d 0a   automatically..
50f40 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62 79 20  ** restarted by 
50f50 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
50f60 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
50f70 73 74 65 70 28 29 2e 20 5e 49 66 20 74 68 65 20  step(). ^If the 
50f80 73 6f 75 72 63 65 20 0d 0a 2a 2a 20 64 61 74 61  source ..** data
50f90 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64  base is modified
50fa0 20 62 79 20 74 68 65 20 75 73 69 6e 67 20 74 68   by the using th
50fb0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
50fc0 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73  connection as is
50fd0 20 75 73 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65   used..** by the
50fe0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
50ff0 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b  n, then the back
51000 75 70 20 64 61 74 61 62 61 73 65 20 69 73 20 61  up database is a
51010 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0d 0a 2a 2a  utomatically..**
51020 20 75 70 64 61 74 65 64 20 61 74 20 74 68 65 20   updated at the 
51030 73 61 6d 65 20 74 69 6d 65 2e 0d 0a 2a 2a 0d 0a  same time...**..
51040 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 5f 62 61 63  ** [[sqlite3_bac
51050 6b 75 70 5f 66 69 6e 69 73 68 28 29 5d 5d 20 3c  kup_finish()]] <
51060 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
51070 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0d 0a 2a  _finish()</b>..*
51080 2a 0d 0a 2a 2a 20 57 68 65 6e 20 73 71 6c 69 74  *..** When sqlit
51090 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
510a0 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53   has returned [S
510b0 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20  QLITE_DONE], or 
510c0 77 68 65 6e 20 74 68 65 20 0d 0a 2a 2a 20 61 70  when the ..** ap
510d0 70 6c 69 63 61 74 69 6f 6e 20 77 69 73 68 65 73  plication wishes
510e0 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20   to abandon the 
510f0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
51100 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  , the applicatio
51110 6e 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 64 65 73  n..** should des
51120 74 72 6f 79 20 74 68 65 20 5b 73 71 6c 69 74 65  troy the [sqlite
51130 33 5f 62 61 63 6b 75 70 5d 20 62 79 20 70 61 73  3_backup] by pas
51140 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69 74  sing it to sqlit
51150 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
51160 28 29 2e 0d 0a 2a 2a 20 5e 54 68 65 20 73 71 6c  ()...** ^The sql
51170 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
51180 73 68 28 29 20 69 6e 74 65 72 66 61 63 65 73 20  sh() interfaces 
51190 72 65 6c 65 61 73 65 73 20 61 6c 6c 0d 0a 2a 2a  releases all..**
511a0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63   resources assoc
511b0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b  iated with the [
511c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20  sqlite3_backup] 
511d0 6f 62 6a 65 63 74 2e 20 0d 0a 2a 2a 20 5e 49 66  object. ..** ^If
511e0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
511f0 73 74 65 70 28 29 20 68 61 73 20 6e 6f 74 20 79  step() has not y
51200 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c  et returned [SQL
51210 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20  ITE_DONE], then 
51220 61 6e 79 0d 0a 2a 2a 20 61 63 74 69 76 65 20 77  any..** active w
51230 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
51240 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74   on the destinat
51250 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20  ion database is 
51260 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0d 0a 2a 2a  rolled back...**
51270 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61   The [sqlite3_ba
51280 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 20  ckup] object is 
51290 69 6e 76 61 6c 69 64 0d 0a 2a 2a 20 61 6e 64 20  invalid..** and 
512a0 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20  may not be used 
512b0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
512c0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
512d0 75 70 5f 66 69 6e 69 73 68 28 29 2e 0d 0a 2a 2a  up_finish()...**
512e0 0d 0a 2a 2a 20 5e 54 68 65 20 76 61 6c 75 65 20  ..** ^The value 
512f0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
51300 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
51310 68 20 69 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  h is [SQLITE_OK]
51320 20 69 66 20 6e 6f 0d 0a 2a 2a 20 73 71 6c 69 74   if no..** sqlit
51330 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
51340 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72 65 64   errors occurred
51350 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20  , regardless or 
51360 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0d 0a  whether or not..
51370 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
51380 70 5f 73 74 65 70 28 29 20 63 6f 6d 70 6c 65 74  p_step() complet
51390 65 64 2e 0d 0a 2a 2a 20 5e 49 66 20 61 6e 20 6f  ed...** ^If an o
513a0 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e  ut-of-memory con
513b0 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72  dition or IO err
513c0 6f 72 20 6f 63 63 75 72 72 65 64 20 64 75 72 69  or occurred duri
513d0 6e 67 20 61 6e 79 20 70 72 69 6f 72 0d 0a 2a 2a  ng any prior..**
513e0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
513f0 73 74 65 70 28 29 20 63 61 6c 6c 20 6f 6e 20 74  step() call on t
51400 68 65 20 73 61 6d 65 20 5b 73 71 6c 69 74 65 33  he same [sqlite3
51410 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2c  _backup] object,
51420 20 74 68 65 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65   then..** sqlite
51430 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
51440 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f  ) returns the co
51450 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 65 72 72  rresponding [err
51460 6f 72 20 63 6f 64 65 5d 2e 0d 0a 2a 2a 0d 0a 2a  or code]...**..*
51470 2a 20 5e 41 20 72 65 74 75 72 6e 20 6f 66 20 5b  * ^A return of [
51480 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20  SQLITE_BUSY] or 
51490 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20  [SQLITE_LOCKED] 
514a0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61 63  from sqlite3_bac
514b0 6b 75 70 5f 73 74 65 70 28 29 0d 0a 2a 2a 20 69  kup_step()..** i
514c0 73 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e  s not a permanen
514d0 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65 73  t error and does
514e0 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20   not affect the 
514f0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0d  return value of.
51500 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
51510 75 70 5f 66 69 6e 69 73 68 28 29 2e 0d 0a 2a 2a  up_finish()...**
51520 0d 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 5f 62  ..** [[sqlite3_b
51530 61 63 6b 75 70 5f 5f 72 65 6d 61 69 6e 69 6e 67  ackup__remaining
51540 28 29 5d 5d 20 5b 5b 73 71 6c 69 74 65 33 5f 62  ()]] [[sqlite3_b
51550 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28  ackup_pagecount(
51560 29 5d 5d 0d 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74  )]]..** <b>sqlit
51570 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
51580 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ing() and sqlite
51590 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
515a0 6e 74 28 29 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a  nt()</b>..**..**
515b0 20 5e 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73   ^Each call to s
515c0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
515d0 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 61  ep() sets two va
515e0 6c 75 65 73 20 69 6e 73 69 64 65 0d 0a 2a 2a 20  lues inside..** 
515f0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  the [sqlite3_bac
51600 6b 75 70 5d 20 6f 62 6a 65 63 74 3a 20 74 68 65  kup] object: the
51610 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
51620 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63   still to be bac
51630 6b 65 64 0d 0a 2a 2a 20 75 70 20 61 6e 64 20 74  ked..** up and t
51640 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
51650 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
51660 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
51670 66 69 6c 65 2e 0d 0a 2a 2a 20 54 68 65 20 73 71  file...** The sq
51680 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d  lite3_backup_rem
51690 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 6c  aining() and sql
516a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65  ite3_backup_page
516b0 63 6f 75 6e 74 28 29 20 69 6e 74 65 72 66 61 63  count() interfac
516c0 65 73 0d 0a 2a 2a 20 72 65 74 72 69 65 76 65 20  es..** retrieve 
516d0 74 68 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73  these two values
516e0 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0d  , respectively..
516f0 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 76 61 6c  .**..** ^The val
51700 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ues returned by 
51710 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
51720 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74 65 64  are only updated
51730 20 62 79 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   by..** sqlite3_
51740 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 5e  backup_step(). ^
51750 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  If the source da
51760 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69  tabase is modifi
51770 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  ed during a back
51780 75 70 0d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  up..** operation
51790 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
517a0 73 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65  s are not update
517b0 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  d to account for
517c0 20 61 6e 79 20 65 78 74 72 61 0d 0a 2a 2a 20 70   any extra..** p
517d0 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  ages that need t
517e0 6f 20 62 65 20 75 70 64 61 74 65 64 20 6f 72 20  o be updated or 
517f0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
51800 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
51810 66 69 6c 65 0d 0a 2a 2a 20 63 68 61 6e 67 69 6e  file..** changin
51820 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 62 3e 43 6f  g...**..** <b>Co
51830 6e 63 75 72 72 65 6e 74 20 55 73 61 67 65 20 6f  ncurrent Usage o
51840 66 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 6c  f Database Handl
51850 65 73 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  es</b>..**..** ^
51860 54 68 65 20 73 6f 75 72 63 65 20 5b 64 61 74 61  The source [data
51870 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
51880 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 20   may be used by 
51890 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
518a0 66 6f 72 20 6f 74 68 65 72 0d 0a 2a 2a 20 70 75  for other..** pu
518b0 72 70 6f 73 65 73 20 77 68 69 6c 65 20 61 20 62  rposes while a b
518c0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20  ackup operation 
518d0 69 73 20 75 6e 64 65 72 77 61 79 20 6f 72 20 62  is underway or b
518e0 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64  eing initialized
518f0 2e 0d 0a 2a 2a 20 5e 49 66 20 53 51 4c 69 74 65  ...** ^If SQLite
51900 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64   is compiled and
51910 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 73   configured to s
51920 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 66  upport threadsaf
51930 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63  e database..** c
51940 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e  onnections, then
51950 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
51960 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
51970 6d 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63  may be used conc
51980 75 72 72 65 6e 74 6c 79 0d 0a 2a 2a 20 66 72 6f  urrently..** fro
51990 6d 20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74  m within other t
519a0 68 72 65 61 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  hreads...**..** 
519b0 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70 70  However, the app
519c0 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75  lication must gu
519d0 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
519e0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 0d 0a 2a   destination ..*
519f0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
51a00 65 63 74 69 6f 6e 5d 20 69 73 20 6e 6f 74 20 70  ection] is not p
51a10 61 73 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68  assed to any oth
51a20 65 72 20 41 50 49 20 28 62 79 20 61 6e 79 20 74  er API (by any t
51a30 68 72 65 61 64 29 20 61 66 74 65 72 20 0d 0a 2a  hread) after ..*
51a40 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
51a50 5f 69 6e 69 74 28 29 20 69 73 20 63 61 6c 6c 65  _init() is calle
51a60 64 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68 65  d and before the
51a70 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 63   corresponding c
51a80 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 73 71 6c 69 74  all to..** sqlit
51a90 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
51aa0 28 29 2e 20 20 53 51 4c 69 74 65 20 64 6f 65 73  ().  SQLite does
51ab0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63   not currently c
51ac0 68 65 63 6b 20 74 6f 20 73 65 65 0d 0a 2a 2a 20  heck to see..** 
51ad0 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  if the applicati
51ae0 6f 6e 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 61  on incorrectly a
51af0 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74  ccesses the dest
51b00 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73  ination [databas
51b10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0d 0a 2a  e connection]..*
51b20 2a 20 61 6e 64 20 73 6f 20 6e 6f 20 65 72 72 6f  * and so no erro
51b30 72 20 63 6f 64 65 20 69 73 20 72 65 70 6f 72 74  r code is report
51b40 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 65 72  ed, but the oper
51b50 61 74 69 6f 6e 73 20 6d 61 79 20 6d 61 6c 66 75  ations may malfu
51b60 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6e 65 76 65 72  nction..** never
51b70 74 68 65 6c 65 73 73 2e 20 20 55 73 65 20 6f 66  theless.  Use of
51b80 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
51b90 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
51ba0 74 69 6f 6e 20 77 68 69 6c 65 20 61 0d 0a 2a 2a  tion while a..**
51bb0 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 72   backup is in pr
51bc0 6f 67 72 65 73 73 20 6d 69 67 68 74 20 61 6c 73  ogress might als
51bd0 6f 20 61 6c 73 6f 20 63 61 75 73 65 20 61 20 6d  o also cause a m
51be0 75 74 65 78 20 64 65 61 64 6c 6f 63 6b 2e 0d 0a  utex deadlock...
51bf0 2a 2a 0d 0a 2a 2a 20 49 66 20 72 75 6e 6e 69 6e  **..** If runnin
51c00 67 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63  g in [shared cac
51c10 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70  he mode], the ap
51c20 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 0d 0a  plication must..
51c30 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  ** guarantee tha
51c40 74 20 74 68 65 20 73 68 61 72 65 64 20 63 61 63  t the shared cac
51c50 68 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64  he used by the d
51c60 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
51c70 61 73 65 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 61  ase..** is not a
51c80 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 68  ccessed while th
51c90 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 6e 6e  e backup is runn
51ca0 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 63 65  ing. In practice
51cb0 20 74 68 69 73 20 6d 65 61 6e 73 0d 0a 2a 2a 20   this means..** 
51cc0 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61  that the applica
51cd0 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e  tion must guaran
51ce0 74 65 65 20 74 68 61 74 20 74 68 65 20 64 69 73  tee that the dis
51cf0 6b 20 66 69 6c 65 20 62 65 69 6e 67 20 0d 0a 2a  k file being ..*
51d00 2a 20 62 61 63 6b 65 64 20 75 70 20 74 6f 20 69  * backed up to i
51d10 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 62  s not accessed b
51d20 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  y any connection
51d30 20 77 69 74 68 69 6e 20 74 68 65 20 70 72 6f 63   within the proc
51d40 65 73 73 2c 0d 0a 2a 2a 20 6e 6f 74 20 6a 75 73  ess,..** not jus
51d50 74 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63  t the specific c
51d60 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 77  onnection that w
51d70 61 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  as passed to sql
51d80 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
51d90 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ()...**..** The 
51da0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
51db0 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69   object itself i
51dc0 73 20 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65  s partially thre
51dd0 61 64 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65  adsafe. Multiple
51de0 20 0d 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 61   ..** threads ma
51df0 79 20 73 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75  y safely make mu
51e00 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e  ltiple concurren
51e10 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  t calls to sqlit
51e20 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
51e30 2e 0d 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74  ...** However, t
51e40 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
51e50 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e  p_remaining() an
51e60 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  d sqlite3_backup
51e70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0d 0a 2a 2a  _pagecount()..**
51e80 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73 74   APIs are not st
51e90 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67 20  rictly speaking 
51ea0 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20 74  threadsafe. If t
51eb0 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64 20  hey are invoked 
51ec0 61 74 20 74 68 65 0d 0a 2a 2a 20 73 61 6d 65 20  at the..** same 
51ed0 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20  time as another 
51ee0 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69  thread is invoki
51ef0 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ng sqlite3_backu
51f00 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0d 0a  p_step() it is..
51f10 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74  ** possible that
51f20 20 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76   they return inv
51f30 61 6c 69 64 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f  alid values...*/
51f40 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ..SQLITE_API sql
51f50 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c  ite3_backup *sql
51f60 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
51f70 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 44  (..  sqlite3 *pD
51f80 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  est,            
51f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
51fa0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
51fb0 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ase handle */.. 
51fc0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65   const char *zDe
51fd0 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  stName,         
51fe0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
51ff0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
52000 6e 61 6d 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  name */..  sqlit
52010 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20  e3 *pSource,    
52020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52030 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61    /* Source data
52040 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a  base handle */..
52050 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
52060 6f 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 20  ourceName       
52070 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
52080 63 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  ce database name
52090 20 2a 2f 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f   */..);..SQLITE_
520a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
520b0 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69  backup_step(sqli
520c0 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69  te3_backup *p, i
520d0 6e 74 20 6e 50 61 67 65 29 3b 0d 0a 53 51 4c 49  nt nPage);..SQLI
520e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
520f0 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
52100 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
52110 2a 70 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49  *p);..SQLITE_API
52120 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
52130 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71  kup_remaining(sq
52140 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29  lite3_backup *p)
52150 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ;..SQLITE_API in
52160 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
52170 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74  _pagecount(sqlit
52180 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0d 0a  e3_backup *p);..
52190 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
521a0 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69  F: Unlock Notifi
521b0 63 61 74 69 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  cation..**..** ^
521c0 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20  When running in 
521d0 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
521e0 65 2c 20 61 20 64 61 74 61 62 61 73 65 20 6f 70  e, a database op
521f0 65 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c  eration may fail
52200 20 77 69 74 68 0d 0a 2a 2a 20 61 6e 20 5b 53 51   with..** an [SQ
52210 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72  LITE_LOCKED] err
52220 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 69 72  or if the requir
52230 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ed locks on the 
52240 73 68 61 72 65 64 2d 63 61 63 68 65 20 6f 72 0d  shared-cache or.
52250 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74  .** individual t
52260 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74 68 65  ables within the
52270 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 61   shared-cache ca
52280 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
52290 2e 20 53 65 65 0d 0a 2a 2a 20 5b 53 51 4c 69 74  . See..** [SQLit
522a0 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d  e Shared-Cache M
522b0 6f 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63 72  ode] for a descr
522c0 69 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65 64  iption of shared
522d0 2d 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e 20  -cache locking. 
522e0 0d 0a 2a 2a 20 5e 54 68 69 73 20 41 50 49 20 6d  ..** ^This API m
522f0 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65  ay be used to re
52300 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63  gister a callbac
52310 6b 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69  k that SQLite wi
52320 6c 6c 20 69 6e 76 6f 6b 65 20 0d 0a 2a 2a 20 77  ll invoke ..** w
52330 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  hen the connecti
52340 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c  on currently hol
52350 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72 65  ding the require
52360 64 20 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 69 73  d lock relinquis
52370 68 65 73 20 69 74 2e 0d 0a 2a 2a 20 5e 54 68 69  hes it...** ^Thi
52380 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76  s API is only av
52390 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c  ailable if the l
523a0 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
523b0 6c 65 64 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a  led with the..**
523c0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
523d0 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 43  UNLOCK_NOTIFY] C
523e0 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79  -preprocessor sy
523f0 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0d 0a 2a  mbol defined...*
52400 2a 0d 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20  *..** See Also: 
52410 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74  [Using the SQLit
52420 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63  e Unlock Notific
52430 61 74 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0d  ation Feature]..
52440 0a 2a 2a 0d 0a 2a 2a 20 5e 53 68 61 72 65 64 2d  .**..** ^Shared-
52450 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20  cache locks are 
52460 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20  released when a 
52470 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
52480 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0d 0a 2a  ion concludes..*
52490 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72  * its current tr
524a0 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65  ansaction, eithe
524b0 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20  r by committing 
524c0 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74  it or rolling it
524d0 20 62 61 63 6b 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20   back. ..**..** 
524e0 5e 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69  ^When a connecti
524f0 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65  on (known as the
52500 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
52510 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20 6f 62  ion) fails to ob
52520 74 61 69 6e 20 61 0d 0a 2a 2a 20 73 68 61 72 65  tain a..** share
52530 64 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e 64  d-cache lock and
52540 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
52550 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  s returned to th
52560 65 20 63 61 6c 6c 65 72 2c 20 74 68 65 0d 0a 2a  e caller, the..*
52570 2a 20 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68  * identity of th
52580 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
52590 63 74 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b  ction (the block
525a0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20  ing connection) 
525b0 74 68 61 74 0d 0a 2a 2a 20 68 61 73 20 6c 6f 63  that..** has loc
525c0 6b 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64  ked the required
525d0 20 72 65 73 6f 75 72 63 65 20 69 73 20 73 74 6f   resource is sto
525e0 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20  red internally. 
525f0 5e 41 66 74 65 72 20 61 6e 20 0d 0a 2a 2a 20 61  ^After an ..** a
52600 70 70 6c 69 63 61 74 69 6f 6e 20 72 65 63 65 69  pplication recei
52610 76 65 73 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f  ves an SQLITE_LO
52620 43 4b 45 44 20 65 72 72 6f 72 2c 20 69 74 20 6d  CKED error, it m
52630 61 79 20 63 61 6c 6c 20 74 68 65 0d 0a 2a 2a 20  ay call the..** 
52640 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
52650 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64 20 77  otify() method w
52660 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  ith the blocked 
52670 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
52680 65 20 61 73 20 0d 0a 2a 2a 20 74 68 65 20 66 69  e as ..** the fi
52690 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
526a0 72 65 67 69 73 74 65 72 20 66 6f 72 20 61 20 63  register for a c
526b0 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c  allback that wil
526c0 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0d 0a 2a 2a  l be invoked..**
526d0 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69   when the blocki
526e0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  ng connections c
526f0 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
52700 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e  on is concluded.
52710 20 5e 54 68 65 0d 0a 2a 2a 20 63 61 6c 6c 62 61   ^The..** callba
52720 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 72  ck is invoked fr
52730 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 5b 73  om within the [s
52740 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 20  qlite3_step] or 
52750 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d 0d  [sqlite3_close].
52760 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 6f  .** call that co
52770 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f 63  ncludes the bloc
52780 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  king connections
52790 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a   transaction...*
527a0 2a 0d 0a 2a 2a 20 5e 28 49 66 20 73 71 6c 69 74  *..** ^(If sqlit
527b0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
527c0 28 29 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20  () is called in 
527d0 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  a multi-threaded
527e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 0d 0a 2a   application,..*
527f0 2a 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61  * there is a cha
52800 6e 63 65 20 74 68 61 74 20 74 68 65 20 62 6c 6f  nce that the blo
52810 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
52820 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61   will have alrea
52830 64 79 0d 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 64  dy..** concluded
52840 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f 6e   its transaction
52850 20 62 79 20 74 68 65 20 74 69 6d 65 20 73 71 6c   by the time sql
52860 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
52870 66 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 2e  fy() is invoked.
52880 0d 0a 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70  ..** If this hap
52890 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73  pens, then the s
528a0 70 65 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63  pecified callbac
528b0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d  k is invoked imm
528c0 65 64 69 61 74 65 6c 79 2c 0d 0a 2a 2a 20 66 72  ediately,..** fr
528d0 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61  om within the ca
528e0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e  ll to sqlite3_un
528f0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 29 5e  lock_notify().)^
52900 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 49 66 20 74 68 65  ..**..** ^If the
52910 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
52920 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e  ion is attemptin
52930 67 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72  g to obtain a wr
52940 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0d 0a 2a  ite-lock on a..*
52950 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74  * shared-cache t
52960 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74  able, and more t
52970 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f  han one other co
52980 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74  nnection current
52990 6c 79 20 68 6f 6c 64 73 0d 0a 2a 2a 20 61 20 72  ly holds..** a r
529a0 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead-lock on the 
529b0 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68 65 6e  same table, then
529c0 20 53 51 4c 69 74 65 20 61 72 62 69 74 72 61 72   SQLite arbitrar
529d0 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e 65 20  ily selects one 
529e0 6f 66 20 0d 0a 2a 2a 20 74 68 65 20 6f 74 68 65  of ..** the othe
529f0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f  r connections to
52a00 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f 63   use as the bloc
52a10 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  king connection.
52a20 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 28 54 68 65 72 65  ..**..** ^(There
52a30 20 6d 61 79 20 62 65 20 61 74 20 6d 6f 73 74 20   may be at most 
52a40 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  one unlock-notif
52a50 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73  y callback regis
52a60 74 65 72 65 64 20 62 79 20 61 20 0d 0a 2a 2a 20  tered by a ..** 
52a70 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
52a80 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75  on. If sqlite3_u
52a90 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69  nlock_notify() i
52aa0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
52ab0 65 0d 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f  e..** blocked co
52ac0 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79  nnection already
52ad0 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72 65   has a registere
52ae0 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  d unlock-notify 
52af0 63 61 6c 6c 62 61 63 6b 2c 0d 0a 2a 2a 20 74 68  callback,..** th
52b00 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62  en the new callb
52b10 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65  ack replaces the
52b20 20 6f 6c 64 2e 29 5e 20 5e 49 66 20 73 71 6c 69   old.)^ ^If sqli
52b30 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
52b40 79 28 29 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65  y() is..** calle
52b50 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f  d with a NULL po
52b60 69 6e 74 65 72 20 61 73 20 69 74 73 20 73 65 63  inter as its sec
52b70 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68  ond argument, th
52b80 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e 67 0d  en any existing.
52b90 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  .** unlock-notif
52ba0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61  y callback is ca
52bb0 6e 63 65 6c 65 64 2e 20 5e 54 68 65 20 62 6c 6f  nceled. ^The blo
52bc0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
52bd0 20 0d 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74   ..** unlock-not
52be0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  ify callback may
52bf0 20 61 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65   also be cancele
52c00 64 20 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65  d by closing the
52c10 20 62 6c 6f 63 6b 65 64 0d 0a 2a 2a 20 63 6f 6e   blocked..** con
52c20 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73  nection using [s
52c30 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e  qlite3_close()].
52c40 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 75 6e 6c  ..**..** The unl
52c50 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
52c60 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e 74  ack is not reent
52c70 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70 6c  rant. If an appl
52c80 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0d  ication invokes.
52c90 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f  .** any sqlite3_
52ca0 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  xxx API function
52cb0 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e  s from within an
52cc0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
52cd0 61 6c 6c 62 61 63 6b 2c 20 61 0d 0a 2a 2a 20 63  allback, a..** c
52ce0 72 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b  rash or deadlock
52cf0 20 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75   may be the resu
52d00 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 55 6e 6c  lt...**..** ^Unl
52d10 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69 73 20  ess deadlock is 
52d20 64 65 74 65 63 74 65 64 20 28 73 65 65 20 62 65  detected (see be
52d30 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e  low), sqlite3_un
52d40 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c  lock_notify() al
52d50 77 61 79 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 73  ways..** returns
52d60 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 0d   SQLITE_OK...**.
52d70 0a 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20  .** <b>Callback 
52d80 49 6e 76 6f 63 61 74 69 6f 6e 20 44 65 74 61 69  Invocation Detai
52d90 6c 73 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 57  ls</b>..**..** W
52da0 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  hen an unlock-no
52db0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
52dc0 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65   registered, the
52dd0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 72 6f   application pro
52de0 76 69 64 65 73 20 61 20 0d 0a 2a 2a 20 73 69 6e  vides a ..** sin
52df0 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65  gle void* pointe
52e00 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64  r that is passed
52e10 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
52e20 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 76 6f   when it is invo
52e30 6b 65 64 2e 0d 0a 2a 2a 20 48 6f 77 65 76 65 72  ked...** However
52e40 2c 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20  , the signature 
52e50 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  of the callback 
52e60 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20  function allows 
52e70 53 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0d 0a  SQLite to pass..
52e80 2a 2a 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f  ** it an array o
52e90 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20  f void* context 
52ea0 70 6f 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69  pointers. The fi
52eb0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73  rst argument pas
52ec0 73 65 64 20 74 6f 0d 0a 2a 2a 20 61 6e 20 75 6e  sed to..** an un
52ed0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
52ee0 62 61 63 6b 20 69 73 20 61 20 70 6f 69 6e 74 65  back is a pointe
52ef0 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  r to an array of
52f00 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c   void* pointers,
52f10 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63  ..** and the sec
52f20 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ond is the numbe
52f30 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
52f40 74 68 65 20 61 72 72 61 79 2e 0d 0a 2a 2a 0d 0a  the array...**..
52f50 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69  ** When a blocki
52f60 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  ng connections t
52f70 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
52f80 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d  ncluded, there m
52f90 61 79 20 62 65 0d 0a 2a 2a 20 6d 6f 72 65 20 74  ay be..** more t
52fa0 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20  han one blocked 
52fb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
52fc0 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66  has registered f
52fd0 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  or an unlock-not
52fe0 69 66 79 0d 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  ify..** callback
52ff0 2e 20 5e 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72  . ^If two or mor
53000 65 20 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 63  e such blocked c
53010 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 20  onnections have 
53020 73 70 65 63 69 66 69 65 64 20 74 68 65 0d 0a 2a  specified the..*
53030 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20  * same callback 
53040 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69  function, then i
53050 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69  nstead of invoki
53060 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ng the callback 
53070 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75 6c  function..** mul
53080 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74 20  tiple times, it 
53090 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20  is invoked once 
530a0 77 69 74 68 20 74 68 65 20 73 65 74 20 6f 66 20  with the set of 
530b0 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f  void* context po
530c0 69 6e 74 65 72 73 0d 0a 2a 2a 20 73 70 65 63 69  inters..** speci
530d0 66 69 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63  fied by the bloc
530e0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
530f0 62 75 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72  bundled together
53100 20 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0d   into an array..
53110 0a 2a 2a 20 54 68 69 73 20 67 69 76 65 73 20 74  .** This gives t
53120 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61  he application a
53130 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  n opportunity to
53140 20 70 72 69 6f 72 69 74 69 7a 65 20 61 6e 79 20   prioritize any 
53150 61 63 74 69 6f 6e 73 20 0d 0a 2a 2a 20 72 65 6c  actions ..** rel
53160 61 74 65 64 20 74 6f 20 74 68 65 20 73 65 74 20  ated to the set 
53170 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 20 64 61 74  of unblocked dat
53180 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
53190 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 62 3e 44 65  s...**..** <b>De
531a0 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 6f 6e  adlock Detection
531b0 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 73 73  </b>..**..** Ass
531c0 75 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72  uming that after
531d0 20 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72   registering for
531e0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
531f0 79 20 63 61 6c 6c 62 61 63 6b 20 61 20 0d 0a 2a  y callback a ..*
53200 2a 20 64 61 74 61 62 61 73 65 20 77 61 69 74 73  * database waits
53210 20 66 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63   for the callbac
53220 6b 20 74 6f 20 62 65 20 69 73 73 75 65 64 20 62  k to be issued b
53230 65 66 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79  efore taking any
53240 20 66 75 72 74 68 65 72 0d 0a 2a 2a 20 61 63 74   further..** act
53250 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61 62 6c  ion (a reasonabl
53260 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74  e assumption), t
53270 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 41  hen using this A
53280 50 49 20 6d 61 79 20 63 61 75 73 65 20 74 68 65  PI may cause the
53290 0d 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  ..** application
532a0 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f   to deadlock. Fo
532b0 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f  r example, if co
532c0 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77 61  nnection X is wa
532d0 69 74 69 6e 67 20 66 6f 72 0d 0a 2a 2a 20 63 6f  iting for..** co
532e0 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61  nnection Y's tra
532f0 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  nsaction to be c
53300 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69  oncluded, and si
53310 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69  milarly connecti
53320 6f 6e 0d 0a 2a 2a 20 59 20 69 73 20 77 61 69 74  on..** Y is wait
53330 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f  ing on connectio
53340 6e 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f  n X's transactio
53350 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20  n, then neither 
53360 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 77  connection..** w
53370 69 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20  ill proceed and 
53380 74 68 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72  the system may r
53390 65 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64  emain deadlocked
533a0 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0d 0a   indefinitely...
533b0 2a 2a 0d 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  **..** To avoid 
533c0 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c 20 74  this scenario, t
533d0 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  he sqlite3_unloc
533e0 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72 66 6f  k_notify() perfo
533f0 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0d 0a 2a 2a  rms deadlock..**
53400 20 64 65 74 65 63 74 69 6f 6e 2e 20 5e 49 66 20   detection. ^If 
53410 61 20 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20  a given call to 
53420 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
53430 6f 74 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75  otify() would pu
53440 74 20 74 68 65 0d 0a 2a 2a 20 73 79 73 74 65 6d  t the..** system
53450 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 65 64   in a deadlocked
53460 20 73 74 61 74 65 2c 20 74 68 65 6e 20 53 51 4c   state, then SQL
53470 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65  ITE_LOCKED is re
53480 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 0d 0a 2a  turned and no..*
53490 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  * unlock-notify 
534a0 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69  callback is regi
534b0 73 74 65 72 65 64 2e 20 54 68 65 20 73 79 73 74  stered. The syst
534c0 65 6d 20 69 73 20 73 61 69 64 20 74 6f 20 62 65  em is said to be
534d0 20 69 6e 0d 0a 2a 2a 20 61 20 64 65 61 64 6c 6f   in..** a deadlo
534e0 63 6b 65 64 20 73 74 61 74 65 20 69 66 20 63 6f  cked state if co
534f0 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61 73 20 72  nnection A has r
53500 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e  egistered for an
53510 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0d 0a   unlock-notify..
53520 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74  ** callback on t
53530 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66  he conclusion of
53540 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27 73 20   connection B's 
53550 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
53560 20 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20   connection..** 
53570 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65 67  B has itself reg
53580 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75  istered for an u
53590 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
535a0 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e 65  lback when conne
535b0 63 74 69 6f 6e 0d 0a 2a 2a 20 41 27 73 20 74 72  ction..** A's tr
535c0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e  ansaction is con
535d0 63 6c 75 64 65 64 2e 20 5e 49 6e 64 69 72 65 63  cluded. ^Indirec
535e0 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c  t deadlock is al
535f0 73 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0d  so detected, so.
53600 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69  .** the system i
53610 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65  s also considere
53620 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b  d to be deadlock
53630 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  ed if connection
53640 20 42 20 68 61 73 0d 0a 2a 2a 20 72 65 67 69 73   B has..** regis
53650 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c  tered for an unl
53660 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
53670 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c  ack on the concl
53680 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74  usion of connect
53690 69 6f 6e 0d 0a 2a 2a 20 43 27 73 20 74 72 61 6e  ion..** C's tran
536a0 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63  saction, where c
536b0 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77  onnection C is w
536c0 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63  aiting on connec
536d0 74 69 6f 6e 20 41 2e 20 5e 41 6e 79 0d 0a 2a 2a  tion A. ^Any..**
536e0 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c   number of level
536f0 73 20 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e  s of indirection
53700 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0d 0a 2a   are allowed...*
53710 2a 0d 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52  *..** <b>The "DR
53720 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65 70 74  OP TABLE" Except
53730 69 6f 6e 3c 2f 62 3e 0d 0a 2a 2a 0d 0a 2a 2a 20  ion</b>..**..** 
53740 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b  When a call to [
53750 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
53760 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c  returns SQLITE_L
53770 4f 43 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d  OCKED, it is alm
53780 6f 73 74 20 0d 0a 2a 2a 20 61 6c 77 61 79 73 20  ost ..** always 
53790 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 63  appropriate to c
537a0 61 6c 6c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  all sqlite3_unlo
537b0 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 54 68 65  ck_notify(). The
537c0 72 65 20 69 73 20 68 6f 77 65 76 65 72 2c 0d 0a  re is however,..
537d0 2a 2a 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e  ** one exception
537e0 2e 20 57 68 65 6e 20 65 78 65 63 75 74 69 6e 67  . When executing
537f0 20 61 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20   a "DROP TABLE" 
53800 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20  or "DROP INDEX" 
53810 73 74 61 74 65 6d 65 6e 74 2c 0d 0a 2a 2a 20 53  statement,..** S
53820 51 4c 69 74 65 20 63 68 65 63 6b 73 20 69 66 20  QLite checks if 
53830 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 75  there are any cu
53840 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
53850 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  g SELECT stateme
53860 6e 74 73 0d 0a 2a 2a 20 74 68 61 74 20 62 65 6c  nts..** that bel
53870 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ong to the same 
53880 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 74  connection. If t
53890 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49 54 45  here are, SQLITE
538a0 5f 4c 4f 43 4b 45 44 20 69 73 0d 0a 2a 2a 20 72  _LOCKED is..** r
538b0 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
538c0 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e   case there is n
538d0 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  o "blocking conn
538e0 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76 6f  ection", so invo
538f0 6b 69 6e 67 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  king..** sqlite3
53900 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
53910 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
53920 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
53930 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0d 0a 2a 2a  llback being..**
53940 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61   invoked immedia
53950 74 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70 70  tely. If the app
53960 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72 65  lication then re
53970 2d 61 74 74 65 6d 70 74 73 20 74 68 65 20 22 44  -attempts the "D
53980 52 4f 50 20 54 41 42 4c 45 22 0d 0a 2a 2a 20 6f  ROP TABLE"..** o
53990 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71  r "DROP INDEX" q
539a0 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74  uery, an infinit
539b0 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20  e loop might be 
539c0 74 68 65 20 72 65 73 75 6c 74 2e 0d 0a 2a 2a 0d  the result...**.
539d0 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75  .** One way arou
539e0 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  nd this problem 
539f0 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  is to check the 
53a00 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
53a10 6f 64 65 20 72 65 74 75 72 6e 65 64 0d 0a 2a 2a  ode returned..**
53a20 20 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73   by an sqlite3_s
53a30 74 65 70 28 29 20 63 61 6c 6c 2e 20 5e 28 49 66  tep() call. ^(If
53a40 20 74 68 65 72 65 20 69 73 20 61 20 62 6c 6f 63   there is a bloc
53a50 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c  king connection,
53a60 20 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 65 78   then the..** ex
53a70 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
53a80 65 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  e is set to SQLI
53a90 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
53aa0 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69 73 65  CACHE. Otherwise
53ab0 2c 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 73 70 65  , in..** the spe
53ac0 63 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c 45  cial "DROP TABLE
53ad0 2f 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74 68  /INDEX" case, th
53ae0 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72  e extended error
53af0 20 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0d 0a   code is just ..
53b00 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ** SQLITE_LOCKED
53b10 2e 29 5e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  .)^..*/..SQLITE_
53b20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
53b30 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0d 0a  unlock_notify(..
53b40 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63    sqlite3 *pBloc
53b50 6b 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  ked,            
53b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
53b70 20 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63 74   Waiting connect
53b80 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28  ion */..  void (
53b90 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  *xNotify)(void *
53ba0 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67  *apArg, int nArg
53bb0 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63  ),    /* Callbac
53bc0 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  k function to in
53bd0 76 6f 6b 65 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  voke */..  void 
53be0 2a 70 4e 6f 74 69 66 79 41 72 67 20 20 20 20 20  *pNotifyArg     
53bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53c00 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
53c10 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 4e  nt to pass to xN
53c20 6f 74 69 66 79 20 2a 2f 0d 0a 29 3b 0d 0a 0d 0a  otify */..);....
53c30 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45  ../*..** CAPI3RE
53c40 46 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72  F: String Compar
53c50 69 73 6f 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68  ison..**..** ^Th
53c60 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  e [sqlite3_strni
53c70 63 6d 70 28 29 5d 20 41 50 49 20 61 6c 6c 6f 77  cmp()] API allow
53c80 73 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61  s applications a
53c90 6e 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f  nd extensions to
53ca0 0d 0a 2a 2a 20 63 6f 6d 70 61 72 65 20 74 68 65  ..** compare the
53cb0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f   contents of two
53cc0 20 62 75 66 66 65 72 73 20 63 6f 6e 74 61 69 6e   buffers contain
53cd0 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ing UTF-8 string
53ce0 73 20 69 6e 20 61 0d 0a 2a 2a 20 63 61 73 65 2d  s in a..** case-
53cf0 69 6e 64 65 70 65 6e 64 65 6e 74 20 66 61 73 68  independent fash
53d00 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 73  ion, using the s
53d10 61 6d 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  ame definition o
53d20 66 20 63 61 73 65 20 69 6e 64 65 70 65 6e 64 65  f case independe
53d30 6e 63 65 20 0d 0a 2a 2a 20 74 68 61 74 20 53 51  nce ..** that SQ
53d40 4c 69 74 65 20 75 73 65 73 20 69 6e 74 65 72 6e  Lite uses intern
53d50 61 6c 6c 79 20 77 68 65 6e 20 63 6f 6d 70 61 72  ally when compar
53d60 69 6e 67 20 69 64 65 6e 74 69 66 69 65 72 73 2e  ing identifiers.
53d70 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
53d80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72   int sqlite3_str
53d90 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  nicmp(const char
53da0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
53db0 2c 20 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  , int);..../*..*
53dc0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 72 72 6f  * CAPI3REF: Erro
53dd0 72 20 4c 6f 67 67 69 6e 67 20 49 6e 74 65 72 66  r Logging Interf
53de0 61 63 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65  ace..**..** ^The
53df0 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 5d   [sqlite3_log()]
53e00 20 69 6e 74 65 72 66 61 63 65 20 77 72 69 74 65   interface write
53e10 73 20 61 20 6d 65 73 73 61 67 65 20 69 6e 74 6f  s a message into
53e20 20 74 68 65 20 65 72 72 6f 72 20 6c 6f 67 0d 0a   the error log..
53e30 2a 2a 20 65 73 74 61 62 6c 69 73 68 65 64 20 62  ** established b
53e40 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f  y the [SQLITE_CO
53e50 4e 46 49 47 5f 4c 4f 47 5d 20 6f 70 74 69 6f 6e  NFIG_LOG] option
53e60 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
53e70 66 69 67 28 29 5d 2e 0d 0a 2a 2a 20 5e 49 66 20  fig()]...** ^If 
53e80 6c 6f 67 67 69 6e 67 20 69 73 20 65 6e 61 62 6c  logging is enabl
53e90 65 64 2c 20 74 68 65 20 7a 46 6f 72 6d 61 74 20  ed, the zFormat 
53ea0 73 74 72 69 6e 67 20 61 6e 64 20 73 75 62 73 65  string and subse
53eb0 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 20  quent arguments 
53ec0 61 72 65 0d 0a 2a 2a 20 75 73 65 64 20 77 69 74  are..** used wit
53ed0 68 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  h [sqlite3_snpri
53ee0 6e 74 66 28 29 5d 20 74 6f 20 67 65 6e 65 72 61  ntf()] to genera
53ef0 74 65 20 74 68 65 20 66 69 6e 61 6c 20 6f 75 74  te the final out
53f00 70 75 74 20 73 74 72 69 6e 67 2e 0d 0a 2a 2a 0d  put string...**.
53f10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
53f20 6c 6f 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  log() interface 
53f30 69 73 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  is intended for 
53f40 75 73 65 20 62 79 20 65 78 74 65 6e 73 69 6f 6e  use by extension
53f50 73 20 73 75 63 68 20 61 73 0d 0a 2a 2a 20 76 69  s such as..** vi
53f60 72 74 75 61 6c 20 74 61 62 6c 65 73 2c 20 63 6f  rtual tables, co
53f70 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
53f80 73 2c 20 61 6e 64 20 53 51 4c 20 66 75 6e 63 74  s, and SQL funct
53f90 69 6f 6e 73 2e 20 20 57 68 69 6c 65 20 74 68 65  ions.  While the
53fa0 72 65 20 69 73 0d 0a 2a 2a 20 6e 6f 74 68 69 6e  re is..** nothin
53fb0 67 20 74 6f 20 70 72 65 76 65 6e 74 20 61 6e 20  g to prevent an 
53fc0 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d  application from
53fd0 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
53fe0 5f 6c 6f 67 28 29 2c 20 64 6f 69 6e 67 20 73 6f  _log(), doing so
53ff0 0d 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72  ..** is consider
54000 65 64 20 62 61 64 20 66 6f 72 6d 2e 0d 0a 2a 2a  ed bad form...**
54010 0d 0a 2a 2a 20 54 68 65 20 7a 46 6f 72 6d 61 74  ..** The zFormat
54020 20 73 74 72 69 6e 67 20 6d 75 73 74 20 6e 6f 74   string must not
54030 20 62 65 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a   be NULL...**..*
54040 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  * To avoid deadl
54050 6f 63 6b 73 20 61 6e 64 20 6f 74 68 65 72 20 74  ocks and other t
54060 68 72 65 61 64 69 6e 67 20 70 72 6f 62 6c 65 6d  hreading problem
54070 73 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c  s, the sqlite3_l
54080 6f 67 28 29 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a  og() routine..**
54090 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 64 79   will not use dy
540a0 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
540b0 74 65 64 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65  ted memory.  The
540c0 20 6c 6f 67 20 6d 65 73 73 61 67 65 20 69 73 20   log message is 
540d0 73 74 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 61 20  stored in..** a 
540e0 66 69 78 65 64 2d 6c 65 6e 67 74 68 20 62 75 66  fixed-length buf
540f0 66 65 72 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  fer on the stack
54100 2e 20 20 49 66 20 74 68 65 20 6c 6f 67 20 6d 65  .  If the log me
54110 73 73 61 67 65 20 69 73 20 6c 6f 6e 67 65 72 20  ssage is longer 
54120 74 68 61 6e 0d 0a 2a 2a 20 61 20 66 65 77 20 68  than..** a few h
54130 75 6e 64 72 65 64 20 63 68 61 72 61 63 74 65 72  undred character
54140 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 74 72  s, it will be tr
54150 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 20 6c  uncated to the l
54160 65 6e 67 74 68 20 6f 66 20 74 68 65 0d 0a 2a 2a  ength of the..**
54170 20 62 75 66 66 65 72 2e 0d 0a 2a 2f 0d 0a 53 51   buffer...*/..SQ
54180 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
54190 6c 69 74 65 33 5f 6c 6f 67 28 69 6e 74 20 69 45  lite3_log(int iE
541a0 72 72 43 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68  rrCode, const ch
541b0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
541c0 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50  );..../*..** CAP
541d0 49 33 52 45 46 3a 20 57 72 69 74 65 2d 41 68 65  I3REF: Write-Ahe
541e0 61 64 20 4c 6f 67 20 43 6f 6d 6d 69 74 20 48 6f  ad Log Commit Ho
541f0 6f 6b 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20  ok..**..** ^The 
54200 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f  [sqlite3_wal_hoo
54210 6b 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 69 73  k()] function is
54220 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65   used to registe
54230 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  r a callback tha
54240 74 0d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69 6e  t..** will be in
54250 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20  voked each time 
54260 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
54270 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 64 61  ction commits da
54280 74 61 20 74 6f 20 61 0d 0a 2a 2a 20 5b 77 72 69  ta to a..** [wri
54290 74 65 2d 61 68 65 61 64 20 6c 6f 67 5d 20 28 69  te-ahead log] (i
542a0 2e 65 2e 20 77 68 65 6e 65 76 65 72 20 61 20 74  .e. whenever a t
542b0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
542c0 6d 6d 69 74 74 65 64 20 69 6e 0d 0a 2a 2a 20 5b  mmitted in..** [
542d0 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 7c 20 6a  journal_mode | j
542e0 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 20  ournal_mode=WAL 
542f0 6d 6f 64 65 5d 29 2e 20 0d 0a 2a 2a 0d 0a 2a 2a  mode]). ..**..**
54300 20 5e 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69   ^The callback i
54310 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 53 51 4c  s invoked by SQL
54320 69 74 65 20 61 66 74 65 72 20 74 68 65 20 63 6f  ite after the co
54330 6d 6d 69 74 20 68 61 73 20 74 61 6b 65 6e 20 70  mmit has taken p
54340 6c 61 63 65 20 61 6e 64 20 0d 0a 2a 2a 20 74 68  lace and ..** th
54350 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 72 69  e associated wri
54360 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  te-lock on the d
54370 61 74 61 62 61 73 65 20 72 65 6c 65 61 73 65 64  atabase released
54380 2c 20 73 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65  , so the impleme
54390 6e 74 61 74 69 6f 6e 20 0d 0a 2a 2a 20 6d 61 79  ntation ..** may
543a0 20 72 65 61 64 2c 20 77 72 69 74 65 20 6f 72 20   read, write or 
543b0 5b 63 68 65 63 6b 70 6f 69 6e 74 5d 20 74 68 65  [checkpoint] the
543c0 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65 71   database as req
543d0 75 69 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e  uired...**..** ^
543e0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
543f0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68  ter passed to th
54400 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
54410 69 6f 6e 20 77 68 65 6e 20 69 74 20 69 73 20 69  ion when it is i
54420 6e 76 6f 6b 65 64 0d 0a 2a 2a 20 69 73 20 61 20  nvoked..** is a 
54430 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72  copy of the thir
54440 64 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73  d parameter pass
54450 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 77 61  ed to sqlite3_wa
54460 6c 5f 68 6f 6f 6b 28 29 20 77 68 65 6e 0d 0a 2a  l_hook() when..*
54470 2a 20 72 65 67 69 73 74 65 72 69 6e 67 20 74 68  * registering th
54480 65 20 63 61 6c 6c 62 61 63 6b 2e 20 5e 54 68 65  e callback. ^The
54490 20 73 65 63 6f 6e 64 20 69 73 20 61 20 63 6f 70   second is a cop
544a0 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
544b0 65 20 68 61 6e 64 6c 65 2e 0d 0a 2a 2a 20 5e 54  e handle...** ^T
544c0 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
544d0 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  er is the name o
544e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
544f0 68 61 74 20 77 61 73 20 77 72 69 74 74 65 6e 20  hat was written 
54500 74 6f 20 2d 0d 0a 2a 2a 20 65 69 74 68 65 72 20  to -..** either 
54510 22 6d 61 69 6e 22 20 6f 72 20 74 68 65 20 6e 61  "main" or the na
54520 6d 65 20 6f 66 20 61 6e 20 5b 41 54 54 41 43 48  me of an [ATTACH
54530 5d 2d 65 64 20 64 61 74 61 62 61 73 65 2e 20 5e  ]-ed database. ^
54540 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  The fourth param
54550 65 74 65 72 0d 0a 2a 2a 20 69 73 20 74 68 65 20  eter..** is the 
54560 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
54570 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
54580 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67   write-ahead log
54590 20 66 69 6c 65 2c 0d 0a 2a 2a 20 69 6e 63 6c 75   file,..** inclu
545a0 64 69 6e 67 20 74 68 6f 73 65 20 74 68 61 74 20  ding those that 
545b0 77 65 72 65 20 6a 75 73 74 20 63 6f 6d 6d 69 74  were just commit
545c0 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ted...**..** The
545d0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
545e0 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c  on should normal
545f0 6c 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ly return [SQLIT
54600 45 5f 4f 4b 5d 2e 20 20 5e 49 66 20 61 6e 20 65  E_OK].  ^If an e
54610 72 72 6f 72 0d 0a 2a 2a 20 63 6f 64 65 20 69 73  rror..** code is
54620 20 72 65 74 75 72 6e 65 64 2c 20 74 68 61 74 20   returned, that 
54630 65 72 72 6f 72 20 77 69 6c 6c 20 70 72 6f 70 61  error will propa
54640 67 61 74 65 20 62 61 63 6b 20 75 70 20 74 68 72  gate back up thr
54650 6f 75 67 68 20 74 68 65 0d 0a 2a 2a 20 53 51 4c  ough the..** SQL
54660 69 74 65 20 63 6f 64 65 20 62 61 73 65 20 74 6f  ite code base to
54670 20 63 61 75 73 65 20 74 68 65 20 73 74 61 74 65   cause the state
54680 6d 65 6e 74 20 74 68 61 74 20 70 72 6f 76 6f 6b  ment that provok
54690 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0d  ed the callback.
546a0 0a 2a 2a 20 74 6f 20 72 65 70 6f 72 74 20 61 6e  .** to report an
546b0 20 65 72 72 6f 72 2c 20 74 68 6f 75 67 68 20 74   error, though t
546c0 68 65 20 63 6f 6d 6d 69 74 20 77 69 6c 6c 20 68  he commit will h
546d0 61 76 65 20 73 74 69 6c 6c 20 6f 63 63 75 72 72  ave still occurr
546e0 65 64 2e 20 49 66 20 74 68 65 0d 0a 2a 2a 20 63  ed. If the..** c
546f0 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
54700 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20  [SQLITE_ROW] or 
54710 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f  [SQLITE_DONE], o
54720 72 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 20  r if it returns 
54730 61 20 76 61 6c 75 65 0d 0a 2a 2a 20 74 68 61 74  a value..** that
54740 20 64 6f 65 73 20 6e 6f 74 20 63 6f 72 72 65 73   does not corres
54750 70 6f 6e 64 20 74 6f 20 61 6e 79 20 76 61 6c 69  pond to any vali
54760 64 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  d SQLite error c
54770 6f 64 65 2c 20 74 68 65 20 72 65 73 75 6c 74 73  ode, the results
54780 0d 0a 2a 2a 20 61 72 65 20 75 6e 64 65 66 69 6e  ..** are undefin
54790 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 73 69  ed...**..** A si
547a0 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 68 61  ngle database ha
547b0 6e 64 6c 65 20 6d 61 79 20 68 61 76 65 20 61 74  ndle may have at
547c0 20 6d 6f 73 74 20 61 20 73 69 6e 67 6c 65 20 77   most a single w
547d0 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 63  rite-ahead log c
547e0 61 6c 6c 62 61 63 6b 20 0d 0a 2a 2a 20 72 65 67  allback ..** reg
547f0 69 73 74 65 72 65 64 20 61 74 20 6f 6e 65 20 74  istered at one t
54800 69 6d 65 2e 20 5e 43 61 6c 6c 69 6e 67 20 5b 73  ime. ^Calling [s
54810 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28  qlite3_wal_hook(
54820 29 5d 20 72 65 70 6c 61 63 65 73 20 61 6e 79 0d  )] replaces any.
54830 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72  .** previously r
54840 65 67 69 73 74 65 72 65 64 20 77 72 69 74 65 2d  egistered write-
54850 61 68 65 61 64 20 6c 6f 67 20 63 61 6c 6c 62 61  ahead log callba
54860 63 6b 2e 20 5e 4e 6f 74 65 20 74 68 61 74 20 74  ck. ^Note that t
54870 68 65 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  he..** [sqlite3_
54880 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69  wal_autocheckpoi
54890 6e 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  nt()] interface 
548a0 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 5b 77 61 6c  and the..** [wal
548b0 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 20  _autocheckpoint 
548c0 70 72 61 67 6d 61 5d 20 62 6f 74 68 20 69 6e 76  pragma] both inv
548d0 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c  oke [sqlite3_wal
548e0 5f 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 77 69 6c  _hook()] and wil
548f0 6c 0d 0a 2a 2a 20 74 68 6f 73 65 20 6f 76 65 72  l..** those over
54900 77 72 69 74 65 20 61 6e 79 20 70 72 69 6f 72 20  write any prior 
54910 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f  [sqlite3_wal_hoo
54920 6b 28 29 5d 20 73 65 74 74 69 6e 67 73 2e 0d 0a  k()] settings...
54930 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  */..SQLITE_API v
54940 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 61 6c  oid *sqlite3_wal
54950 5f 68 6f 6f 6b 28 0d 0a 20 20 73 71 6c 69 74 65  _hook(..  sqlite
54960 33 2a 2c 20 0d 0a 20 20 69 6e 74 28 2a 29 28 76  3*, ..  int(*)(v
54970 6f 69 64 20 2a 2c 73 71 6c 69 74 65 33 2a 2c 63  oid *,sqlite3*,c
54980 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 2c  onst char*,int),
54990 0d 0a 20 20 76 6f 69 64 2a 0d 0a 29 3b 0d 0a 0d  ..  void*..);...
549a0 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46  ./*..** CAPI3REF
549b0 3a 20 43 6f 6e 66 69 67 75 72 65 20 61 6e 20 61  : Configure an a
549c0 75 74 6f 2d 63 68 65 63 6b 70 6f 69 6e 74 0d 0a  uto-checkpoint..
549d0 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c  **..** ^The [sql
549e0 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65  ite3_wal_autoche
549f0 63 6b 70 6f 69 6e 74 28 44 2c 4e 29 5d 20 69 73  ckpoint(D,N)] is
54a00 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e   a wrapper aroun
54a10 64 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 77  d..** [sqlite3_w
54a20 61 6c 5f 68 6f 6f 6b 28 29 5d 20 74 68 61 74 20  al_hook()] that 
54a30 63 61 75 73 65 73 20 61 6e 79 20 64 61 74 61 62  causes any datab
54a40 61 73 65 20 6f 6e 20 5b 64 61 74 61 62 61 73 65  ase on [database
54a50 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0d 0a   connection] D..
54a60 2a 2a 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61  ** to automatica
54a70 6c 6c 79 20 5b 63 68 65 63 6b 70 6f 69 6e 74 5d  lly [checkpoint]
54a80 0d 0a 2a 2a 20 61 66 74 65 72 20 63 6f 6d 6d 69  ..** after commi
54a90 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  tting a transact
54aa0 69 6f 6e 20 69 66 20 74 68 65 72 65 20 61 72 65  ion if there are
54ab0 20 4e 20 6f 72 0d 0a 2a 2a 20 6d 6f 72 65 20 66   N or..** more f
54ac0 72 61 6d 65 73 20 69 6e 20 74 68 65 20 5b 77 72  rames in the [wr
54ad0 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 5d 20 66  ite-ahead log] f
54ae0 69 6c 65 2e 20 20 5e 50 61 73 73 69 6e 67 20 7a  ile.  ^Passing z
54af0 65 72 6f 20 6f 72 20 0d 0a 2a 2a 20 61 20 6e 65  ero or ..** a ne
54b00 67 61 74 69 76 65 20 76 61 6c 75 65 20 61 73 20  gative value as 
54b10 74 68 65 20 6e 46 72 61 6d 65 20 70 61 72 61 6d  the nFrame param
54b20 65 74 65 72 20 64 69 73 61 62 6c 65 73 20 61 75  eter disables au
54b30 74 6f 6d 61 74 69 63 0d 0a 2a 2a 20 63 68 65 63  tomatic..** chec
54b40 6b 70 6f 69 6e 74 73 20 65 6e 74 69 72 65 6c 79  kpoints entirely
54b50 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 63  ...**..** ^The c
54b60 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72  allback register
54b70 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
54b80 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79  ion replaces any
54b90 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61   existing callba
54ba0 63 6b 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  ck..** registere
54bb0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
54bc0 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 2e 20 20 5e  _wal_hook()].  ^
54bd0 4c 69 6b 65 77 69 73 65 2c 20 72 65 67 69 73 74  Likewise, regist
54be0 65 72 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b  ering a callback
54bf0 0d 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69  ..** using [sqli
54c00 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 20  te3_wal_hook()] 
54c10 64 69 73 61 62 6c 65 73 20 74 68 65 20 61 75 74  disables the aut
54c20 6f 6d 61 74 69 63 20 63 68 65 63 6b 70 6f 69 6e  omatic checkpoin
54c30 74 20 6d 65 63 68 61 6e 69 73 6d 0d 0a 2a 2a 20  t mechanism..** 
54c40 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 74 68  configured by th
54c50 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a  is function...**
54c60 0d 0a 2a 2a 20 5e 54 68 65 20 5b 77 61 6c 5f 61  ..** ^The [wal_a
54c70 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 20 70 72  utocheckpoint pr
54c80 61 67 6d 61 5d 20 63 61 6e 20 62 65 20 75 73 65  agma] can be use
54c90 64 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69 73  d to invoke this
54ca0 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20 66   interface..** f
54cb0 72 6f 6d 20 53 51 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a  rom SQL...**..**
54cc0 20 5e 45 76 65 72 79 20 6e 65 77 20 5b 64 61 74   ^Every new [dat
54cd0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
54ce0 5d 20 64 65 66 61 75 6c 74 73 20 74 6f 20 68 61  ] defaults to ha
54cf0 76 69 6e 67 20 74 68 65 20 61 75 74 6f 2d 63 68  ving the auto-ch
54d00 65 63 6b 70 6f 69 6e 74 0d 0a 2a 2a 20 65 6e 61  eckpoint..** ena
54d10 62 6c 65 64 20 77 69 74 68 20 61 20 74 68 72 65  bled with a thre
54d20 73 68 6f 6c 64 20 6f 66 20 31 30 30 30 20 6f 72  shold of 1000 or
54d30 20 5b 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   [SQLITE_DEFAULT
54d40 5f 57 41 4c 5f 41 55 54 4f 43 48 45 43 4b 50 4f  _WAL_AUTOCHECKPO
54d50 49 4e 54 5d 0d 0a 2a 2a 20 70 61 67 65 73 2e 20  INT]..** pages. 
54d60 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 69 73   The use of this
54d70 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20 69   interface..** i
54d80 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79  s only necessary
54d90 20 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   if the default 
54da0 73 65 74 74 69 6e 67 20 69 73 20 66 6f 75 6e 64  setting is found
54db0 20 74 6f 20 62 65 20 73 75 62 6f 70 74 69 6d 61   to be suboptima
54dc0 6c 0d 0a 2a 2a 20 66 6f 72 20 61 20 70 61 72 74  l..** for a part
54dd0 69 63 75 6c 61 72 20 61 70 70 6c 69 63 61 74 69  icular applicati
54de0 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  on...*/..SQLITE_
54df0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
54e00 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69  wal_autocheckpoi
54e10 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nt(sqlite3 *db, 
54e20 69 6e 74 20 4e 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  int N);..../*..*
54e30 2a 20 43 41 50 49 33 52 45 46 3a 20 43 68 65 63  * CAPI3REF: Chec
54e40 6b 70 6f 69 6e 74 20 61 20 64 61 74 61 62 61 73  kpoint a databas
54e50 65 0d 0a 2a 2a 0d 0a 2a 2a 20 5e 54 68 65 20 5b  e..**..** ^The [
54e60 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63  sqlite3_wal_chec
54e70 6b 70 6f 69 6e 74 28 44 2c 58 29 5d 20 69 6e 74  kpoint(D,X)] int
54e80 65 72 66 61 63 65 20 63 61 75 73 65 73 20 64 61  erface causes da
54e90 74 61 62 61 73 65 20 6e 61 6d 65 64 20 58 0d 0a  tabase named X..
54ea0 2a 2a 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20  ** on [database 
54eb0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 74 6f  connection] D to
54ec0 20 62 65 20 5b 63 68 65 63 6b 70 6f 69 6e 74 65   be [checkpointe
54ed0 64 5d 2e 20 20 5e 49 66 20 58 20 69 73 20 4e 55  d].  ^If X is NU
54ee0 4c 4c 20 6f 72 20 61 6e 0d 0a 2a 2a 20 65 6d 70  LL or an..** emp
54ef0 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20  ty string, then 
54f00 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 69 73 20  a checkpoint is 
54f10 72 75 6e 20 6f 6e 20 61 6c 6c 20 64 61 74 61 62  run on all datab
54f20 61 73 65 73 20 6f 66 0d 0a 2a 2a 20 63 6f 6e 6e  ases of..** conn
54f30 65 63 74 69 6f 6e 20 44 2e 20 20 5e 49 66 20 74  ection D.  ^If t
54f40 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
54f50 65 63 74 69 6f 6e 20 44 20 69 73 20 6e 6f 74 20  ection D is not 
54f60 69 6e 0d 0a 2a 2a 20 5b 57 41 4c 20 7c 20 77 72  in..** [WAL | wr
54f70 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 6d 6f  ite-ahead log mo
54f80 64 65 5d 20 74 68 65 6e 20 74 68 69 73 20 69 6e  de] then this in
54f90 74 65 72 66 61 63 65 20 69 73 20 61 20 68 61 72  terface is a har
54fa0 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a  mless no-op...**
54fb0 0d 0a 2a 2a 20 5e 54 68 65 20 5b 77 61 6c 5f 63  ..** ^The [wal_c
54fc0 68 65 63 6b 70 6f 69 6e 74 20 70 72 61 67 6d 61  heckpoint pragma
54fd0 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  ] can be used to
54fe0 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 69 6e 74   invoke this int
54ff0 65 72 66 61 63 65 0d 0a 2a 2a 20 66 72 6f 6d 20  erface..** from 
55000 53 51 4c 2e 20 20 5e 54 68 65 20 5b 73 71 6c 69  SQL.  ^The [sqli
55010 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63  te3_wal_autochec
55020 6b 70 6f 69 6e 74 28 29 5d 20 69 6e 74 65 72 66  kpoint()] interf
55030 61 63 65 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20  ace and the..** 
55040 5b 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f  [wal_autocheckpo
55050 69 6e 74 20 70 72 61 67 6d 61 5d 20 63 61 6e 20  int pragma] can 
55060 62 65 20 75 73 65 64 20 74 6f 20 63 61 75 73 65  be used to cause
55070 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
55080 74 6f 20 62 65 0d 0a 2a 2a 20 72 75 6e 20 77 68  to be..** run wh
55090 65 6e 65 76 65 72 20 74 68 65 20 57 41 4c 20 72  enever the WAL r
550a0 65 61 63 68 65 73 20 61 20 63 65 72 74 61 69 6e  eaches a certain
550b0 20 73 69 7a 65 20 74 68 72 65 73 68 6f 6c 64 2e   size threshold.
550c0 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
550d0 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f  o: [sqlite3_wal_
550e0 63 68 65 63 6b 70 6f 69 6e 74 5f 76 32 28 29 5d  checkpoint_v2()]
550f0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
55100 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 61 6c   int sqlite3_wal
55110 5f 63 68 65 63 6b 70 6f 69 6e 74 28 73 71 6c 69  _checkpoint(sqli
55120 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
55130 68 61 72 20 2a 7a 44 62 29 3b 0d 0a 0d 0a 2f 2a  har *zDb);..../*
55140 0d 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  ..** CAPI3REF: C
55150 68 65 63 6b 70 6f 69 6e 74 20 61 20 64 61 74 61  heckpoint a data
55160 62 61 73 65 0d 0a 2a 2a 0d 0a 2a 2a 20 52 75 6e  base..**..** Run
55170 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 6f 70   a checkpoint op
55180 65 72 61 74 69 6f 6e 20 6f 6e 20 57 41 4c 20 64  eration on WAL d
55190 61 74 61 62 61 73 65 20 7a 44 62 20 61 74 74 61  atabase zDb atta
551a0 63 68 65 64 20 74 6f 20 64 61 74 61 62 61 73 65  ched to database
551b0 20 0d 0a 2a 2a 20 68 61 6e 64 6c 65 20 64 62 2e   ..** handle db.
551c0 20 54 68 65 20 73 70 65 63 69 66 69 63 20 6f 70   The specific op
551d0 65 72 61 74 69 6f 6e 20 69 73 20 64 65 74 65 72  eration is deter
551e0 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76 61 6c  mined by the val
551f0 75 65 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 65  ue of the ..** e
55200 4d 6f 64 65 20 70 61 72 61 6d 65 74 65 72 3a 0d  Mode parameter:.
55210 0a 2a 2a 0d 0a 2a 2a 20 3c 64 6c 3e 0d 0a 2a 2a  .**..** <dl>..**
55220 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 48 45 43   <dt>SQLITE_CHEC
55230 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 3c 64  KPOINT_PASSIVE<d
55240 64 3e 0d 0a 2a 2a 20 20 20 43 68 65 63 6b 70 6f  d>..**   Checkpo
55250 69 6e 74 20 61 73 20 6d 61 6e 79 20 66 72 61 6d  int as many fram
55260 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20 77  es as possible w
55270 69 74 68 6f 75 74 20 77 61 69 74 69 6e 67 20 66  ithout waiting f
55280 6f 72 20 61 6e 79 20 64 61 74 61 62 61 73 65 20  or any database 
55290 0d 0a 2a 2a 20 20 20 72 65 61 64 65 72 73 20 6f  ..**   readers o
552a0 72 20 77 72 69 74 65 72 73 20 74 6f 20 66 69 6e  r writers to fin
552b0 69 73 68 2e 20 53 79 6e 63 20 74 68 65 20 64 62  ish. Sync the db
552c0 20 66 69 6c 65 20 69 66 20 61 6c 6c 20 66 72 61   file if all fra
552d0 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f 67 0d 0a  mes in the log..
552e0 2a 2a 20 20 20 61 72 65 20 63 68 65 63 6b 70 6f  **   are checkpo
552f0 69 6e 74 65 64 2e 20 54 68 69 73 20 6d 6f 64 65  inted. This mode
55300 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
55310 63 61 6c 6c 69 6e 67 20 0d 0a 2a 2a 20 20 20 73  calling ..**   s
55320 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b  qlite3_wal_check
55330 70 6f 69 6e 74 28 29 2e 20 54 68 65 20 62 75 73  point(). The bus
55340 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61  y-handler callba
55350 63 6b 20 69 73 20 6e 65 76 65 72 20 69 6e 76 6f  ck is never invo
55360 6b 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 64 74  ked...**..** <dt
55370 3e 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49  >SQLITE_CHECKPOI
55380 4e 54 5f 46 55 4c 4c 3c 64 64 3e 0d 0a 2a 2a 20  NT_FULL<dd>..** 
55390 20 20 54 68 69 73 20 6d 6f 64 65 20 62 6c 6f 63    This mode bloc
553a0 6b 73 20 28 63 61 6c 6c 73 20 74 68 65 20 62 75  ks (calls the bu
553b0 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  sy-handler callb
553c0 61 63 6b 29 20 75 6e 74 69 6c 20 74 68 65 72 65  ack) until there
553d0 20 69 73 20 6e 6f 0d 0a 2a 2a 20 20 20 64 61 74   is no..**   dat
553e0 61 62 61 73 65 20 77 72 69 74 65 72 20 61 6e 64  abase writer and
553f0 20 61 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65   all readers are
55400 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68   reading from th
55410 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 64 61  e most recent da
55420 74 61 62 61 73 65 0d 0a 2a 2a 20 20 20 73 6e 61  tabase..**   sna
55430 70 73 68 6f 74 2e 20 49 74 20 74 68 65 6e 20 63  pshot. It then c
55440 68 65 63 6b 70 6f 69 6e 74 73 20 61 6c 6c 20 66  heckpoints all f
55450 72 61 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f 67  rames in the log
55460 20 66 69 6c 65 20 61 6e 64 20 73 79 6e 63 73 20   file and syncs 
55470 74 68 65 0d 0a 2a 2a 20 20 20 64 61 74 61 62 61  the..**   databa
55480 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 63 61  se file. This ca
55490 6c 6c 20 62 6c 6f 63 6b 73 20 64 61 74 61 62 61  ll blocks databa
554a0 73 65 20 77 72 69 74 65 72 73 20 77 68 69 6c 65  se writers while
554b0 20 69 74 20 69 73 20 72 75 6e 6e 69 6e 67 2c 0d   it is running,.
554c0 0a 2a 2a 20 20 20 62 75 74 20 6e 6f 74 20 64 61  .**   but not da
554d0 74 61 62 61 73 65 20 72 65 61 64 65 72 73 2e 0d  tabase readers..
554e0 0a 2a 2a 0d 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  .**..** <dt>SQLI
554f0 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45  TE_CHECKPOINT_RE
55500 53 54 41 52 54 3c 64 64 3e 0d 0a 2a 2a 20 20 20  START<dd>..**   
55510 54 68 69 73 20 6d 6f 64 65 20 77 6f 72 6b 73 20  This mode works 
55520 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 20  the same way as 
55530 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
55540 54 5f 46 55 4c 4c 2c 20 65 78 63 65 70 74 20 61  T_FULL, except a
55550 66 74 65 72 20 0d 0a 2a 2a 20 20 20 63 68 65 63  fter ..**   chec
55560 6b 70 6f 69 6e 74 69 6e 67 20 74 68 65 20 6c 6f  kpointing the lo
55570 67 20 66 69 6c 65 20 69 74 20 62 6c 6f 63 6b 73  g file it blocks
55580 20 28 63 61 6c 6c 73 20 74 68 65 20 62 75 73 79   (calls the busy
55590 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63  -handler callbac
555a0 6b 29 0d 0a 2a 2a 20 20 20 75 6e 74 69 6c 20 61  k)..**   until a
555b0 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65 20 72  ll readers are r
555c0 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  eading from the 
555d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e  database file on
555e0 6c 79 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73  ly. This ensures
555f0 20 0d 0a 2a 2a 20 20 20 74 68 61 74 20 74 68 65   ..**   that the
55600 20 6e 65 78 74 20 63 6c 69 65 6e 74 20 74 6f 20   next client to 
55610 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 61 74  write to the dat
55620 61 62 61 73 65 20 66 69 6c 65 20 72 65 73 74 61  abase file resta
55630 72 74 73 20 74 68 65 20 6c 6f 67 20 66 69 6c 65  rts the log file
55640 20 0d 0a 2a 2a 20 20 20 66 72 6f 6d 20 74 68 65   ..**   from the
55650 20 62 65 67 69 6e 6e 69 6e 67 2e 20 54 68 69 73   beginning. This
55660 20 63 61 6c 6c 20 62 6c 6f 63 6b 73 20 64 61 74   call blocks dat
55670 61 62 61 73 65 20 77 72 69 74 65 72 73 20 77 68  abase writers wh
55680 69 6c 65 20 69 74 20 69 73 20 72 75 6e 6e 69 6e  ile it is runnin
55690 67 2c 0d 0a 2a 2a 20 20 20 62 75 74 20 6e 6f 74  g,..**   but not
556a0 20 64 61 74 61 62 61 73 65 20 72 65 61 64 65 72   database reader
556b0 73 2e 0d 0a 2a 2a 20 3c 2f 64 6c 3e 0d 0a 2a 2a  s...** </dl>..**
556c0 0d 0a 2a 2a 20 49 66 20 70 6e 4c 6f 67 20 69 73  ..** If pnLog is
556d0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
556e0 2a 70 6e 4c 6f 67 20 69 73 20 73 65 74 20 74 6f  *pnLog is set to
556f0 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
55700 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 0d 0a  r of frames in..
55710 2a 2a 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20  ** the log file 
55720 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
55730 2e 20 49 66 20 70 6e 43 6b 70 74 20 69 73 20 6e  . If pnCkpt is n
55740 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70  ot NULL, then *p
55750 6e 43 6b 70 74 20 69 73 20 73 65 74 20 74 6f 0d  nCkpt is set to.
55760 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  .** the total nu
55770 6d 62 65 72 20 6f 66 20 63 68 65 63 6b 70 6f 69  mber of checkpoi
55780 6e 74 65 64 20 66 72 61 6d 65 73 20 28 69 6e 63  nted frames (inc
55790 6c 75 64 69 6e 67 20 61 6e 79 20 74 68 61 74 20  luding any that 
557a0 77 65 72 65 20 61 6c 72 65 61 64 79 0d 0a 2a 2a  were already..**
557b0 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 20 77 68   checkpointed wh
557c0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
557d0 20 69 73 20 63 61 6c 6c 65 64 29 2e 20 2a 70 6e   is called). *pn
557e0 4c 6f 67 20 61 6e 64 20 2a 70 6e 43 6b 70 74 20  Log and *pnCkpt 
557f0 6d 61 79 20 62 65 0d 0a 2a 2a 20 70 6f 70 75 6c  may be..** popul
55800 61 74 65 64 20 65 76 65 6e 20 69 66 20 73 71 6c  ated even if sql
55810 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f  ite3_wal_checkpo
55820 69 6e 74 5f 76 32 28 29 20 72 65 74 75 72 6e 73  int_v2() returns
55830 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
55840 54 45 5f 4f 4b 2e 0d 0a 2a 2a 20 49 66 20 6e 6f  TE_OK...** If no
55850 20 76 61 6c 75 65 73 20 61 72 65 20 61 76 61 69   values are avai
55860 6c 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 66  lable because of
55870 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 79 20   an error, they 
55880 61 72 65 20 62 6f 74 68 20 73 65 74 20 74 6f 20  are both set to 
55890 2d 31 0d 0a 2a 2a 20 62 65 66 6f 72 65 20 72 65  -1..** before re
558a0 74 75 72 6e 69 6e 67 20 74 6f 20 63 6f 6d 6d 75  turning to commu
558b0 6e 69 63 61 74 65 20 74 68 69 73 20 74 6f 20 74  nicate this to t
558c0 68 65 20 63 61 6c 6c 65 72 2e 0d 0a 2a 2a 0d 0a  he caller...**..
558d0 2a 2a 20 41 6c 6c 20 63 61 6c 6c 73 20 6f 62 74  ** All calls obt
558e0 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ain an exclusive
558f0 20 22 63 68 65 63 6b 70 6f 69 6e 74 22 20 6c 6f   "checkpoint" lo
55900 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
55910 73 65 20 66 69 6c 65 2e 20 49 66 0d 0a 2a 2a 20  se file. If..** 
55920 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
55930 73 20 69 73 20 72 75 6e 6e 69 6e 67 20 61 20 63  s is running a c
55940 68 65 63 6b 70 6f 69 6e 74 20 6f 70 65 72 61 74  heckpoint operat
55950 69 6f 6e 20 61 74 20 74 68 65 20 73 61 6d 65 20  ion at the same 
55960 74 69 6d 65 2c 20 74 68 65 20 0d 0a 2a 2a 20 6c  time, the ..** l
55970 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62  ock cannot be ob
55980 74 61 69 6e 65 64 20 61 6e 64 20 53 51 4c 49 54  tained and SQLIT
55990 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e  E_BUSY is return
559a0 65 64 2e 20 45 76 65 6e 20 69 66 20 74 68 65 72  ed. Even if ther
559b0 65 20 69 73 20 61 20 0d 0a 2a 2a 20 62 75 73 79  e is a ..** busy
559c0 2d 68 61 6e 64 6c 65 72 20 63 6f 6e 66 69 67 75  -handler configu
559d0 72 65 64 2c 20 69 74 20 77 69 6c 6c 20 6e 6f 74  red, it will not
559e0 20 62 65 20 69 6e 76 6f 6b 65 64 20 69 6e 20 74   be invoked in t
559f0 68 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a  his case...**..*
55a00 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 48 45  * The SQLITE_CHE
55a10 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 20 61 6e 64  CKPOINT_FULL and
55a20 20 52 45 53 54 41 52 54 20 6d 6f 64 65 73 20 61   RESTART modes a
55a30 6c 73 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 65  lso obtain the e
55a40 78 63 6c 75 73 69 76 65 20 0d 0a 2a 2a 20 22 77  xclusive ..** "w
55a50 72 69 74 65 72 22 20 6c 6f 63 6b 20 6f 6e 20 74  riter" lock on t
55a60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
55a70 2e 20 49 66 20 74 68 65 20 77 72 69 74 65 72 20  . If the writer 
55a80 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f  lock cannot be o
55a90 62 74 61 69 6e 65 64 0d 0a 2a 2a 20 69 6d 6d 65  btained..** imme
55aa0 64 69 61 74 65 6c 79 2c 20 61 6e 64 20 61 20 62  diately, and a b
55ab0 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 63  usy-handler is c
55ac0 6f 6e 66 69 67 75 72 65 64 2c 20 69 74 20 69 73  onfigured, it is
55ad0 20 69 6e 76 6f 6b 65 64 20 61 6e 64 20 74 68 65   invoked and the
55ae0 20 77 72 69 74 65 72 0d 0a 2a 2a 20 6c 6f 63 6b   writer..** lock
55af0 20 72 65 74 72 69 65 64 20 75 6e 74 69 6c 20 65   retried until e
55b00 69 74 68 65 72 20 74 68 65 20 62 75 73 79 2d 68  ither the busy-h
55b10 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30  andler returns 0
55b20 20 6f 72 20 74 68 65 20 6c 6f 63 6b 20 69 73 0d   or the lock is.
55b30 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
55b40 20 6f 62 74 61 69 6e 65 64 2e 20 54 68 65 20 62   obtained. The b
55b50 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 61  usy-handler is a
55b60 6c 73 6f 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c  lso invoked whil
55b70 65 20 77 61 69 74 69 6e 67 20 66 6f 72 0d 0a 2a  e waiting for..*
55b80 2a 20 64 61 74 61 62 61 73 65 20 72 65 61 64 65  * database reade
55b90 72 73 20 61 73 20 64 65 73 63 72 69 62 65 64 20  rs as described 
55ba0 61 62 6f 76 65 2e 20 49 66 20 74 68 65 20 62 75  above. If the bu
55bb0 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72  sy-handler retur
55bc0 6e 73 20 30 20 62 65 66 6f 72 65 0d 0a 2a 2a 20  ns 0 before..** 
55bd0 74 68 65 20 77 72 69 74 65 72 20 6c 6f 63 6b 20  the writer lock 
55be0 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 72 20 77  is obtained or w
55bf0 68 69 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72  hile waiting for
55c00 20 64 61 74 61 62 61 73 65 20 72 65 61 64 65 72   database reader
55c10 73 2c 20 74 68 65 0d 0a 2a 2a 20 63 68 65 63 6b  s, the..** check
55c20 70 6f 69 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  point operation 
55c30 70 72 6f 63 65 65 64 73 20 66 72 6f 6d 20 74 68  proceeds from th
55c40 61 74 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  at point in the 
55c50 73 61 6d 65 20 77 61 79 20 61 73 20 0d 0a 2a 2a  same way as ..**
55c60 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49   SQLITE_CHECKPOI
55c70 4e 54 5f 50 41 53 53 49 56 45 20 2d 20 63 68 65  NT_PASSIVE - che
55c80 63 6b 70 6f 69 6e 74 69 6e 67 20 61 73 20 6d 61  ckpointing as ma
55c90 6e 79 20 66 72 61 6d 65 73 20 61 73 20 70 6f 73  ny frames as pos
55ca0 73 69 62 6c 65 20 0d 0a 2a 2a 20 77 69 74 68 6f  sible ..** witho
55cb0 75 74 20 62 6c 6f 63 6b 69 6e 67 20 61 6e 79 20  ut blocking any 
55cc0 66 75 72 74 68 65 72 2e 20 53 51 4c 49 54 45 5f  further. SQLITE_
55cd0 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64  BUSY is returned
55ce0 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0d 0a   in this case...
55cf0 2a 2a 0d 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65  **..** If parame
55d00 74 65 72 20 7a 44 62 20 69 73 20 4e 55 4c 4c 20  ter zDb is NULL 
55d10 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 7a  or points to a z
55d20 65 72 6f 20 6c 65 6e 67 74 68 20 73 74 72 69 6e  ero length strin
55d30 67 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20  g, then the..** 
55d40 73 70 65 63 69 66 69 65 64 20 6f 70 65 72 61 74  specified operat
55d50 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 65 64  ion is attempted
55d60 20 6f 6e 20 61 6c 6c 20 57 41 4c 20 64 61 74 61   on all WAL data
55d70 62 61 73 65 73 2e 20 49 6e 20 74 68 69 73 20 63  bases. In this c
55d80 61 73 65 20 74 68 65 0d 0a 2a 2a 20 76 61 6c 75  ase the..** valu
55d90 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6f 75  es written to ou
55da0 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 20  tput parameters 
55db0 2a 70 6e 4c 6f 67 20 61 6e 64 20 2a 70 6e 43 6b  *pnLog and *pnCk
55dc0 70 74 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  pt are undefined
55dd0 2e 20 49 66 20 0d 0a 2a 2a 20 61 6e 20 53 51 4c  . If ..** an SQL
55de0 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69  ITE_BUSY error i
55df0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68  s encountered wh
55e00 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e  en processing on
55e10 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
55e20 20 0d 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 57   ..** attached W
55e30 41 4c 20 64 61 74 61 62 61 73 65 73 2c 20 74 68  AL databases, th
55e40 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 73  e operation is s
55e50 74 69 6c 6c 20 61 74 74 65 6d 70 74 65 64 20 6f  till attempted o
55e60 6e 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20  n any remaining 
55e70 0d 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61  ..** attached da
55e80 74 61 62 61 73 65 73 20 61 6e 64 20 53 51 4c 49  tabases and SQLI
55e90 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72  TE_BUSY is retur
55ea0 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ned to the calle
55eb0 72 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72 20  r. If any other 
55ec0 0d 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72  ..** error occur
55ed0 73 20 77 68 69 6c 65 20 70 72 6f 63 65 73 73 69  s while processi
55ee0 6e 67 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  ng an attached d
55ef0 61 74 61 62 61 73 65 2c 20 70 72 6f 63 65 73 73  atabase, process
55f00 69 6e 67 20 69 73 20 61 62 61 6e 64 6f 6e 65 64  ing is abandoned
55f10 20 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72   ..** and the er
55f20 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65  ror code returne
55f30 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  d to the caller 
55f40 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 66 20  immediately. If 
55f50 6e 6f 20 65 72 72 6f 72 20 0d 0a 2a 2a 20 28 53  no error ..** (S
55f60 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 6f 74  QLITE_BUSY or ot
55f70 68 65 72 77 69 73 65 29 20 69 73 20 65 6e 63 6f  herwise) is enco
55f80 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 70 72  untered while pr
55f90 6f 63 65 73 73 69 6e 67 20 74 68 65 20 61 74 74  ocessing the att
55fa0 61 63 68 65 64 20 0d 0a 2a 2a 20 64 61 74 61 62  ached ..** datab
55fb0 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ases, SQLITE_OK 
55fc0 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
55fd0 0d 0a 2a 2a 20 49 66 20 64 61 74 61 62 61 73 65  ..** If database
55fe0 20 7a 44 62 20 69 73 20 74 68 65 20 6e 61 6d 65   zDb is the name
55ff0 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20   of an attached 
56000 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73  database that is
56010 20 6e 6f 74 20 69 6e 20 57 41 4c 0d 0a 2a 2a 20   not in WAL..** 
56020 6d 6f 64 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  mode, SQLITE_OK 
56030 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
56040 62 6f 74 68 20 2a 70 6e 4c 6f 67 20 61 6e 64 20  both *pnLog and 
56050 2a 70 6e 43 6b 70 74 20 73 65 74 20 74 6f 20 2d  *pnCkpt set to -
56060 31 2e 20 49 66 0d 0a 2a 2a 20 7a 44 62 20 69 73  1. If..** zDb is
56070 20 6e 6f 74 20 4e 55 4c 4c 20 28 6f 72 20 61 20   not NULL (or a 
56080 7a 65 72 6f 20 6c 65 6e 67 74 68 20 73 74 72 69  zero length stri
56090 6e 67 29 20 61 6e 64 20 69 73 20 6e 6f 74 20 74  ng) and is not t
560a0 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0d 0a  he name of any..
560b0 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ** attached data
560c0 62 61 73 65 2c 20 53 51 4c 49 54 45 5f 45 52 52  base, SQLITE_ERR
560d0 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  OR is returned t
560e0 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a 2a  o the caller...*
560f0 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
56100 74 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68  t sqlite3_wal_ch
56110 65 63 6b 70 6f 69 6e 74 5f 76 32 28 0d 0a 20 20  eckpoint_v2(..  
56120 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
56130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56140 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
56150 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  le */..  const c
56160 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20  har *zDb,       
56170 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
56180 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74   of attached dat
56190 61 62 61 73 65 20 28 6f 72 20 4e 55 4c 4c 29 20  abase (or NULL) 
561a0 2a 2f 0d 0a 20 20 69 6e 74 20 65 4d 6f 64 65 2c  */..  int eMode,
561b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
561c0 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
561d0 43 48 45 43 4b 50 4f 49 4e 54 5f 2a 20 76 61 6c  CHECKPOINT_* val
561e0 75 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e  ue */..  int *pn
561f0 4c 6f 67 2c 20 20 20 20 20 20 20 20 20 20 20 20  Log,            
56200 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
56210 20 53 69 7a 65 20 6f 66 20 57 41 4c 20 6c 6f 67   Size of WAL log
56220 20 69 6e 20 66 72 61 6d 65 73 20 2a 2f 0d 0a 20   in frames */.. 
56230 20 69 6e 74 20 2a 70 6e 43 6b 70 74 20 20 20 20   int *pnCkpt    
56240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56250 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e   /* OUT: Total n
56260 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20  umber of frames 
56270 63 68 65 63 6b 70 6f 69 6e 74 65 64 20 2a 2f 0d  checkpointed */.
56280 0a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41  .);..../*..** CA
56290 50 49 33 52 45 46 3a 20 43 68 65 63 6b 70 6f 69  PI3REF: Checkpoi
562a0 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 61 72  nt operation par
562b0 61 6d 65 74 65 72 73 0d 0a 2a 2a 0d 0a 2a 2a 20  ameters..**..** 
562c0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
562d0 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74  can be used as t
562e0 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
562f0 20 74 6f 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   to..** [sqlite3
56300 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f  _wal_checkpoint_
56310 76 32 28 29 5d 2e 20 20 53 65 65 20 74 68 65 20  v2()].  See the 
56320 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65  [sqlite3_wal_che
56330 63 6b 70 6f 69 6e 74 5f 76 32 28 29 5d 0d 0a 2a  ckpoint_v2()]..*
56340 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  * documentation 
56350 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
56360 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
56370 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 61 6e 64   the meaning and
56380 20 75 73 65 20 6f 66 0d 0a 2a 2a 20 65 61 63 68   use of..** each
56390 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73   of these values
563a0 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ...*/..#define S
563b0 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54  QLITE_CHECKPOINT
563c0 5f 50 41 53 53 49 56 45 20 30 0d 0a 23 64 65 66  _PASSIVE 0..#def
563d0 69 6e 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  ine SQLITE_CHECK
563e0 50 4f 49 4e 54 5f 46 55 4c 4c 20 20 20 20 31 0d  POINT_FULL    1.
563f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
56400 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41  CHECKPOINT_RESTA
56410 52 54 20 32 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  RT 2..../*..** C
56420 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c  API3REF: Virtual
56430 20 54 61 62 6c 65 20 49 6e 74 65 72 66 61 63 65   Table Interface
56440 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0d 0a   Configuration..
56450 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  **..** This func
56460 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
56470 65 64 20 62 79 20 65 69 74 68 65 72 20 74 68 65  ed by either the
56480 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6f 72 20 5b   [xConnect] or [
56490 78 43 72 65 61 74 65 5d 20 6d 65 74 68 6f 64 0d  xCreate] method.
564a0 0a 2a 2a 20 6f 66 20 61 20 5b 76 69 72 74 75 61  .** of a [virtua
564b0 6c 20 74 61 62 6c 65 5d 20 69 6d 70 6c 65 6d 65  l table] impleme
564c0 6e 74 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 66 69  ntation to confi
564d0 67 75 72 65 0d 0a 2a 2a 20 76 61 72 69 6f 75 73  gure..** various
564e0 20 66 61 63 65 74 73 20 6f 66 20 74 68 65 20 76   facets of the v
564f0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74  irtual table int
56500 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  erface...**..** 
56510 49 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  If this interfac
56520 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 75 74  e is invoked out
56530 73 69 64 65 20 74 68 65 20 63 6f 6e 74 65 78 74  side the context
56540 20 6f 66 20 61 6e 20 78 43 6f 6e 6e 65 63 74 20   of an xConnect 
56550 6f 72 0d 0a 2a 2a 20 78 43 72 65 61 74 65 20 76  or..** xCreate v
56560 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74  irtual table met
56570 68 6f 64 20 74 68 65 6e 20 74 68 65 20 62 65 68  hod then the beh
56580 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
56590 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 74 20 70  ed...**..** At p
565a0 72 65 73 65 6e 74 2c 20 74 68 65 72 65 20 69 73  resent, there is
565b0 20 6f 6e 6c 79 20 6f 6e 65 20 6f 70 74 69 6f 6e   only one option
565c0 20 74 68 61 74 20 6d 61 79 20 62 65 20 63 6f 6e   that may be con
565d0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0d 0a 2a  figured using..*
565e0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  * this function.
565f0 20 28 53 65 65 20 5b 53 51 4c 49 54 45 5f 56 54   (See [SQLITE_VT
56600 41 42 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 53 55  AB_CONSTRAINT_SU
56610 50 50 4f 52 54 5d 2e 29 20 20 46 75 72 74 68 65  PPORT].)  Furthe
56620 72 20 6f 70 74 69 6f 6e 73 0d 0a 2a 2a 20 6d 61  r options..** ma
56630 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 74 68  y be added in th
56640 65 20 66 75 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 53  e future...*/..S
56650 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
56660 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69  lite3_vtab_confi
56670 67 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20  g(sqlite3*, int 
56680 6f 70 2c 20 2e 2e 2e 29 3b 0d 0a 0d 0a 2f 2a 0d  op, ...);..../*.
56690 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69  .** CAPI3REF: Vi
566a0 72 74 75 61 6c 20 54 61 62 6c 65 20 43 6f 6e 66  rtual Table Conf
566b0 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e  iguration Option
566c0 73 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20  s..**..** These 
566d0 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68  macros define th
566e0 65 20 76 61 72 69 6f 75 73 20 6f 70 74 69 6f 6e  e various option
566f0 73 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 5b 73 71  s to the..** [sq
56700 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69  lite3_vtab_confi
56710 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74  g()] interface t
56720 68 61 74 20 5b 76 69 72 74 75 61 6c 20 74 61 62  hat [virtual tab
56730 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  le] implementati
56740 6f 6e 73 0d 0a 2a 2a 20 63 61 6e 20 75 73 65 20  ons..** can use 
56750 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 61 6e 64  to customize and
56760 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 69 72 20   optimize their 
56770 62 65 68 61 76 69 6f 72 2e 0d 0a 2a 2a 0d 0a 2a  behavior...**..*
56780 2a 20 3c 64 6c 3e 0d 0a 2a 2a 20 3c 64 74 3e 53  * <dl>..** <dt>S
56790 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53 54  QLITE_VTAB_CONST
567a0 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 0d 0a 2a  RAINT_SUPPORT..*
567b0 2a 20 3c 64 64 3e 43 61 6c 6c 73 20 6f 66 20 74  * <dd>Calls of t
567c0 68 65 20 66 6f 72 6d 0d 0a 2a 2a 20 5b 73 71 6c  he form..** [sql
567d0 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67  ite3_vtab_config
567e0 5d 28 64 62 2c 53 51 4c 49 54 45 5f 56 54 41 42  ](db,SQLITE_VTAB
567f0 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 53 55 50 50  _CONSTRAINT_SUPP
56800 4f 52 54 2c 58 29 20 61 72 65 20 73 75 70 70 6f  ORT,X) are suppo
56810 72 74 65 64 2c 0d 0a 2a 2a 20 77 68 65 72 65 20  rted,..** where 
56820 58 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  X is an integer.
56830 20 20 49 66 20 58 20 69 73 20 7a 65 72 6f 2c 20    If X is zero, 
56840 74 68 65 6e 20 74 68 65 20 5b 76 69 72 74 75 61  then the [virtua
56850 6c 20 74 61 62 6c 65 5d 20 77 68 6f 73 65 0d 0a  l table] whose..
56860 2a 2a 20 5b 78 43 72 65 61 74 65 5d 20 6f 72 20  ** [xCreate] or 
56870 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f  [xConnect] metho
56880 64 20 69 6e 76 6f 6b 65 64 20 5b 73 71 6c 69 74  d invoked [sqlit
56890 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67 28 29  e3_vtab_config()
568a0 5d 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a 20 73  ] does not..** s
568b0 75 70 70 6f 72 74 20 63 6f 6e 73 74 72 61 69 6e  upport constrain
568c0 74 73 2e 20 20 49 6e 20 74 68 69 73 20 63 6f 6e  ts.  In this con
568d0 66 69 67 75 72 61 74 69 6f 6e 20 28 77 68 69 63  figuration (whic
568e0 68 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  h is the default
568f0 29 20 69 66 0d 0a 2a 2a 20 61 20 63 61 6c 6c 20  ) if..** a call 
56900 74 6f 20 74 68 65 20 5b 78 55 70 64 61 74 65 5d  to the [xUpdate]
56910 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
56920 5b 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  [SQLITE_CONSTRAI
56930 4e 54 5d 2c 20 74 68 65 6e 20 74 68 65 20 65 6e  NT], then the en
56940 74 69 72 65 0d 0a 2a 2a 20 73 74 61 74 65 6d 65  tire..** stateme
56950 6e 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  nt is rolled bac
56960 6b 20 61 73 20 69 66 20 5b 4f 4e 20 43 4f 4e 46  k as if [ON CONF
56970 4c 49 43 54 20 7c 20 4f 52 20 41 42 4f 52 54 5d  LICT | OR ABORT]
56980 20 68 61 64 20 62 65 65 6e 0d 0a 2a 2a 20 73 70   had been..** sp
56990 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20  ecified as part 
569a0 6f 66 20 74 68 65 20 75 73 65 72 73 20 53 51 4c  of the users SQL
569b0 20 73 74 61 74 65 6d 65 6e 74 2c 20 72 65 67 61   statement, rega
569c0 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 61 63  rdless of the ac
569d0 74 75 61 6c 0d 0a 2a 2a 20 4f 4e 20 43 4f 4e 46  tual..** ON CONF
569e0 4c 49 43 54 20 6d 6f 64 65 20 73 70 65 63 69 66  LICT mode specif
569f0 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ied...**..** If 
56a00 58 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  X is non-zero, t
56a10 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20  hen the virtual 
56a20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
56a30 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 73 0d  tion guarantees.
56a40 0a 2a 2a 20 74 68 61 74 20 69 66 20 5b 78 55 70  .** that if [xUp
56a50 64 61 74 65 5d 20 72 65 74 75 72 6e 73 20 5b 53  date] returns [S
56a60 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
56a70 5d 2c 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f  ], it will do so
56a80 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 61 6e 79 20   before..** any 
56a90 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f  modifications to
56aa0 20 69 6e 74 65 72 6e 61 6c 20 6f 72 20 70 65 72   internal or per
56ab0 73 69 73 74 65 6e 74 20 64 61 74 61 20 73 74 72  sistent data str
56ac0 75 63 74 75 72 65 73 20 68 61 76 65 20 62 65 65  uctures have bee
56ad0 6e 20 6d 61 64 65 2e 0d 0a 2a 2a 20 49 66 20 74  n made...** If t
56ae0 68 65 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d  he [ON CONFLICT]
56af0 20 6d 6f 64 65 20 69 73 20 41 42 4f 52 54 2c 20   mode is ABORT, 
56b00 46 41 49 4c 2c 20 49 47 4e 4f 52 45 20 6f 72 20  FAIL, IGNORE or 
56b10 52 4f 4c 4c 42 41 43 4b 2c 20 53 51 4c 69 74 65  ROLLBACK, SQLite
56b20 20 0d 0a 2a 2a 20 69 73 20 61 62 6c 65 20 74 6f   ..** is able to
56b30 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 73 74 61   roll back a sta
56b40 74 65 6d 65 6e 74 20 6f 72 20 64 61 74 61 62 61  tement or databa
56b50 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  se transaction, 
56b60 61 6e 64 20 61 62 61 6e 64 6f 6e 0d 0a 2a 2a 20  and abandon..** 
56b70 6f 72 20 63 6f 6e 74 69 6e 75 65 20 70 72 6f 63  or continue proc
56b80 65 73 73 69 6e 67 20 74 68 65 20 63 75 72 72 65  essing the curre
56b90 6e 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  nt SQL statement
56ba0 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e   as appropriate.
56bb0 20 0d 0a 2a 2a 20 49 66 20 74 68 65 20 4f 4e 20   ..** If the ON 
56bc0 43 4f 4e 46 4c 49 43 54 20 6d 6f 64 65 20 69 73  CONFLICT mode is
56bd0 20 52 45 50 4c 41 43 45 20 61 6e 64 20 74 68 65   REPLACE and the
56be0 20 5b 78 55 70 64 61 74 65 5d 20 6d 65 74 68 6f   [xUpdate] metho
56bf0 64 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 5b 53  d returns..** [S
56c00 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
56c10 5d 2c 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  ], SQLite handle
56c20 73 20 74 68 69 73 20 61 73 20 69 66 20 74 68 65  s this as if the
56c30 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 6d 6f 64   ON CONFLICT mod
56c40 65 0d 0a 2a 2a 20 68 61 64 20 62 65 65 6e 20 41  e..** had been A
56c50 42 4f 52 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 56 69  BORT...**..** Vi
56c60 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c  rtual table impl
56c70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 68 61 74  ementations that
56c80 20 61 72 65 20 72 65 71 75 69 72 65 64 20 74 6f   are required to
56c90 20 68 61 6e 64 6c 65 20 4f 52 20 52 45 50 4c 41   handle OR REPLA
56ca0 43 45 0d 0a 2a 2a 20 6d 75 73 74 20 64 6f 20 73  CE..** must do s
56cb0 6f 20 77 69 74 68 69 6e 20 74 68 65 20 5b 78 55  o within the [xU
56cc0 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 2e 20 49  pdate] method. I
56cd0 66 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20  f a call to the 
56ce0 0d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 74  ..** [sqlite3_vt
56cf0 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 29  ab_on_conflict()
56d00 5d 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 69 63  ] function indic
56d10 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 75  ates that the cu
56d20 72 72 65 6e 74 20 4f 4e 20 0d 0a 2a 2a 20 43 4f  rrent ON ..** CO
56d30 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 69 73  NFLICT policy is
56d40 20 52 45 50 4c 41 43 45 2c 20 74 68 65 20 76 69   REPLACE, the vi
56d50 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c  rtual table impl
56d60 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c  ementation shoul
56d70 64 20 0d 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20  d ..** silently 
56d80 72 65 70 6c 61 63 65 20 74 68 65 20 61 70 70 72  replace the appr
56d90 6f 70 72 69 61 74 65 20 72 6f 77 73 20 77 69 74  opriate rows wit
56da0 68 69 6e 20 74 68 65 20 78 55 70 64 61 74 65 20  hin the xUpdate 
56db0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 0d 0a 2a 2a  callback and..**
56dc0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
56dd0 4b 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69  K. Or, if this i
56de0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c 20  s not possible, 
56df0 69 74 20 6d 61 79 20 72 65 74 75 72 6e 0d 0a 2a  it may return..*
56e00 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  * SQLITE_CONSTRA
56e10 49 4e 54 2c 20 69 6e 20 77 68 69 63 68 20 63 61  INT, in which ca
56e20 73 65 20 53 51 4c 69 74 65 20 66 61 6c 6c 73 20  se SQLite falls 
56e30 62 61 63 6b 20 74 6f 20 4f 52 20 41 42 4f 52 54  back to OR ABORT
56e40 20 0d 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74   ..** constraint
56e50 20 68 61 6e 64 6c 69 6e 67 2e 0d 0a 2a 2a 20 3c   handling...** <
56e60 2f 64 6c 3e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  /dl>..*/..#defin
56e70 65 20 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f  e SQLITE_VTAB_CO
56e80 4e 53 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54  NSTRAINT_SUPPORT
56e90 20 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50   1..../*..** CAP
56ea0 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65  I3REF: Determine
56eb0 20 54 68 65 20 56 69 72 74 75 61 6c 20 54 61 62   The Virtual Tab
56ec0 6c 65 20 43 6f 6e 66 6c 69 63 74 20 50 6f 6c 69  le Conflict Poli
56ed0 63 79 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  cy..**..** This 
56ee0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c  function may onl
56ef0 79 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  y be called from
56f00 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c 20 74   within a call t
56f10 6f 20 74 68 65 20 5b 78 55 70 64 61 74 65 5d 20  o the [xUpdate] 
56f20 6d 65 74 68 6f 64 0d 0a 2a 2a 20 6f 66 20 61 20  method..** of a 
56f30 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20  [virtual table] 
56f40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
56f50 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20  or an INSERT or 
56f60 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
56f70 2e 20 5e 54 68 65 0d 0a 2a 2a 20 76 61 6c 75 65  . ^The..** value
56f80 20 72 65 74 75 72 6e 65 64 20 69 73 20 6f 6e 65   returned is one
56f90 20 6f 66 20 5b 53 51 4c 49 54 45 5f 52 4f 4c 4c   of [SQLITE_ROLL
56fa0 42 41 43 4b 5d 2c 20 5b 53 51 4c 49 54 45 5f 49  BACK], [SQLITE_I
56fb0 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f  GNORE], [SQLITE_
56fc0 46 41 49 4c 5d 2c 0d 0a 2a 2a 20 5b 53 51 4c 49  FAIL],..** [SQLI
56fd0 54 45 5f 41 42 4f 52 54 5d 2c 20 6f 72 20 5b 53  TE_ABORT], or [S
56fe0 51 4c 49 54 45 5f 52 45 50 4c 41 43 45 5d 2c 20  QLITE_REPLACE], 
56ff0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
57000 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 6d   [ON CONFLICT] m
57010 6f 64 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 53  ode..** of the S
57020 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
57030 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20  t triggered the 
57040 63 61 6c 6c 20 74 6f 20 74 68 65 20 5b 78 55 70  call to the [xUp
57050 64 61 74 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20  date] method of 
57060 74 68 65 0d 0a 2a 2a 20 5b 76 69 72 74 75 61 6c  the..** [virtual
57070 20 74 61 62 6c 65 5d 2e 0d 0a 2a 2f 0d 0a 53 51   table]...*/..SQ
57080 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
57090 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e  ite3_vtab_on_con
570a0 66 6c 69 63 74 28 73 71 6c 69 74 65 33 20 2a 29  flict(sqlite3 *)
570b0 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 41 50 49  ;..../*..** CAPI
570c0 33 52 45 46 3a 20 43 6f 6e 66 6c 69 63 74 20 72  3REF: Conflict r
570d0 65 73 6f 6c 75 74 69 6f 6e 20 6d 6f 64 65 73 0d  esolution modes.
570e0 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 63 6f  .**..** These co
570f0 6e 73 74 61 6e 74 73 20 61 72 65 20 72 65 74 75  nstants are retu
57100 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
57110 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63  _vtab_on_conflic
57120 74 28 29 5d 20 74 6f 0d 0a 2a 2a 20 69 6e 66 6f  t()] to..** info
57130 72 6d 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61  rm a [virtual ta
57140 62 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ble] implementat
57150 69 6f 6e 20 77 68 61 74 20 74 68 65 20 5b 4f 4e  ion what the [ON
57160 20 43 4f 4e 46 4c 49 43 54 5d 20 6d 6f 64 65 0d   CONFLICT] mode.
57170 0a 2a 2a 20 69 73 20 66 6f 72 20 74 68 65 20 53  .** is for the S
57180 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  QL statement bei
57190 6e 67 20 65 76 61 6c 75 61 74 65 64 2e 0d 0a 2a  ng evaluated...*
571a0 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  *..** Note that 
571b0 74 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  the [SQLITE_IGNO
571c0 52 45 5d 20 63 6f 6e 73 74 61 6e 74 20 69 73 20  RE] constant is 
571d0 61 6c 73 6f 20 75 73 65 64 20 61 73 20 61 20 70  also used as a p
571e0 6f 74 65 6e 74 69 61 6c 0d 0a 2a 2a 20 72 65 74  otential..** ret
571f0 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  urn value from t
57200 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
57210 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 63 61  authorizer()] ca
57220 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 61 74 0d  llback and that.
57230 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52  .** [SQLITE_ABOR
57240 54 5d 20 69 73 20 61 6c 73 6f 20 61 20 5b 72 65  T] is also a [re
57250 73 75 6c 74 20 63 6f 64 65 5d 2e 0d 0a 2a 2f 0d  sult code]...*/.
57260 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
57270 52 4f 4c 4c 42 41 43 4b 20 31 0d 0a 2f 2a 20 23  ROLLBACK 1../* #
57280 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 47  define SQLITE_IG
57290 4e 4f 52 45 20 32 20 2f 2f 20 41 6c 73 6f 20 75  NORE 2 // Also u
572a0 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 61  sed by sqlite3_a
572b0 75 74 68 6f 72 69 7a 65 72 28 29 20 63 61 6c 6c  uthorizer() call
572c0 62 61 63 6b 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  back */..#define
572d0 20 53 51 4c 49 54 45 5f 46 41 49 4c 20 20 20 20   SQLITE_FAIL    
572e0 20 33 0d 0a 2f 2a 20 23 64 65 66 69 6e 65 20 53   3../* #define S
572f0 51 4c 49 54 45 5f 41 42 4f 52 54 20 34 20 20 2f  QLITE_ABORT 4  /
57300 2f 20 41 6c 73 6f 20 61 6e 20 65 72 72 6f 72 20  / Also an error 
57310 63 6f 64 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  code */..#define
57320 20 53 51 4c 49 54 45 5f 52 45 50 4c 41 43 45 20   SQLITE_REPLACE 
57330 20 35 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   5......../*..**
57340 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74   Undo the hack t
57350 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f  hat converts flo
57360 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65  ating point type
57370 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72  s to integer for
57380 0d 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70  ..** builds on p
57390 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75  rocessors withou
573a0 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
573b0 20 73 75 70 70 6f 72 74 2e 0d 0a 2a 2f 0d 0a 23   support...*/..#
573c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
573d0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
573e0 0d 0a 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65  ..# undef double
573f0 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20  ..#endif....#if 
57400 30 0d 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66 20  0..}  /* End of 
57410 74 68 65 20 27 65 78 74 65 72 6e 20 22 43 22 27  the 'extern "C"'
57420 20 62 6c 6f 63 6b 20 2a 2f 0d 0a 23 65 6e 64 69   block */..#endi
57430 66 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  f..#endif..../*.
57440 0a 2a 2a 20 32 30 31 30 20 41 75 67 75 73 74 20  .** 2010 August 
57450 33 30 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  30..**..** The a
57460 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
57470 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
57480 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
57490 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
574a0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
574b0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
574c0 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
574d0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
574e0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
574f0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
57500 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
57510 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
57520 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
57530 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
57540 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
57550 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
57560 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
57570 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
57580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
575a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
575b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2f 0d 0a  **********..*/..
575c0 0d 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  ..#ifndef _SQLIT
575d0 45 33 52 54 52 45 45 5f 48 5f 0d 0a 23 64 65 66  E3RTREE_H_..#def
575e0 69 6e 65 20 5f 53 51 4c 49 54 45 33 52 54 52 45  ine _SQLITE3RTRE
575f0 45 5f 48 5f 0d 0a 0d 0a 0d 0a 23 69 66 20 30 0d  E_H_......#if 0.
57600 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0d 0a 23  .extern "C" {..#
57610 65 6e 64 69 66 0d 0a 0d 0a 74 79 70 65 64 65 66  endif....typedef
57620 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
57630 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 20 73  rtree_geometry s
57640 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f  qlite3_rtree_geo
57650 6d 65 74 72 79 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  metry;..../*..**
57660 20 52 65 67 69 73 74 65 72 20 61 20 67 65 6f 6d   Register a geom
57670 65 74 72 79 20 63 61 6c 6c 62 61 63 6b 20 6e 61  etry callback na
57680 6d 65 64 20 7a 47 65 6f 6d 20 74 68 61 74 20 63  med zGeom that c
57690 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 70 61  an be used as pa
576a0 72 74 20 6f 66 20 61 6e 0d 0a 2a 2a 20 52 2d 54  rt of an..** R-T
576b0 72 65 65 20 67 65 6f 6d 65 74 72 79 20 71 75 65  ree geometry que
576c0 72 79 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a  ry as follows:..
576d0 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  **..**   SELECT 
576e0 2e 2e 2e 20 46 52 4f 4d 20 3c 72 74 72 65 65 3e  ... FROM <rtree>
576f0 20 57 48 45 52 45 20 3c 72 74 72 65 65 20 63 6f   WHERE <rtree co
57700 6c 3e 20 4d 41 54 43 48 20 24 7a 47 65 6f 6d 28  l> MATCH $zGeom(
57710 2e 2e 2e 20 70 61 72 61 6d 73 20 2e 2e 2e 29 0d  ... params ...).
57720 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
57730 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 74 72 65  int sqlite3_rtre
57740 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c 6c 62  e_geometry_callb
57750 61 63 6b 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  ack(..  sqlite3 
57760 2a 64 62 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68  *db,..  const ch
57770 61 72 20 2a 7a 47 65 6f 6d 2c 0d 0a 20 20 69 6e  ar *zGeom,..  in
57780 74 20 28 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74  t (*xGeom)(sqlit
57790 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72  e3_rtree_geometr
577a0 79 20 2a 2c 20 69 6e 74 20 6e 43 6f 6f 72 64 2c  y *, int nCoord,
577b0 20 64 6f 75 62 6c 65 20 2a 61 43 6f 6f 72 64 2c   double *aCoord,
577c0 20 69 6e 74 20 2a 70 52 65 73 29 2c 0d 0a 20 20   int *pRes),..  
577d0 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78 74 0d 0a  void *pContext..
577e0 29 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  );....../*..** A
577f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
57800 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 66  ructure of the f
57810 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 69 73  ollowing type is
57820 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
57830 69 72 73 74 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e  irst..** argumen
57840 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b 73 20 72  t to callbacks r
57850 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20  egistered using 
57860 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 5f 63  rtree_geometry_c
57870 61 6c 6c 62 61 63 6b 28 29 2e 0d 0a 2a 2f 0d 0a  allback()...*/..
57880 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 72  struct sqlite3_r
57890 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 20 7b 0d  tree_geometry {.
578a0 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78  .  void *pContex
578b0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
578c0 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 43     /* Copy of pC
578d0 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20 74 6f  ontext passed to
578e0 20 73 5f 72 5f 67 5f 63 28 29 20 2a 2f 0d 0a 20   s_r_g_c() */.. 
578f0 20 69 6e 74 20 6e 50 61 72 61 6d 3b 20 20 20 20   int nParam;    
57900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57910 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61   /* Size of arra
57920 79 20 61 50 61 72 61 6d 5b 5d 20 2a 2f 0d 0a 20  y aParam[] */.. 
57930 20 64 6f 75 62 6c 65 20 2a 61 50 61 72 61 6d 3b   double *aParam;
57940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57950 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 70   /* Parameters p
57960 61 73 73 65 64 20 74 6f 20 53 51 4c 20 67 65 6f  assed to SQL geo
57970 6d 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20  m function */.. 
57980 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20   void *pUser;   
57990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
579a0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 69 6d 70   /* Callback imp
579b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 72  lementation user
579c0 20 64 61 74 61 20 2a 2f 0d 0a 20 20 76 6f 69 64   data */..  void
579d0 20 28 2a 78 44 65 6c 55 73 65 72 29 28 76 6f 69   (*xDelUser)(voi
579e0 64 20 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43  d *);       /* C
579f0 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20  alled by SQLite 
57a00 74 6f 20 63 6c 65 61 6e 20 75 70 20 70 55 73 65  to clean up pUse
57a10 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 23 69  r */..};......#i
57a20 66 20 30 0d 0a 7d 20 20 2f 2a 20 65 6e 64 20 6f  f 0..}  /* end o
57a30 66 20 74 68 65 20 27 65 78 74 65 72 6e 20 22 43  f the 'extern "C
57a40 22 27 20 62 6c 6f 63 6b 20 2a 2f 0d 0a 23 65 6e  "' block */..#en
57a50 64 69 66 0d 0a 0d 0a 23 65 6e 64 69 66 20 20 2f  dif....#endif  /
57a60 2a 20 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  * ifndef _SQLITE
57a70 33 52 54 52 45 45 5f 48 5f 20 2a 2f 0d 0a 0d 0a  3RTREE_H_ */....
57a80 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
57a90 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33  * End of sqlite3
57aa0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
57ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ad0 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
57ae0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
57af0 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
57b00 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
57b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57b20 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
57b30 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68  *** Include hash
57b40 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
57b50 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
57b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57b70 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
57b80 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
57b90 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
57ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57bc0 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
57bd0 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0d 0a  1 September 22..
57be0 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
57bf0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
57c00 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
57c10 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
57c20 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
57c30 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
57c40 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
57c50 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
57c60 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
57c70 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
57c80 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
57c90 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
57ca0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
57cb0 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
57cc0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
57cd0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
57ce0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
57cf0 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
57d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d40 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  ******..** This 
57d50 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69  is the header fi
57d60 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72  le for the gener
57d70 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d  ic hash-table im
57d80 70 6c 65 6d 65 6e 61 74 69 6f 6e 0d 0a 2a 2a 20  plemenation..** 
57d90 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0d  used in SQLite..
57da0 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 53 51  .*/..#ifndef _SQ
57db0 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0d 0a 23 64  LITE_HASH_H_..#d
57dc0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41  efine _SQLITE_HA
57dd0 53 48 5f 48 5f 0d 0a 0d 0a 2f 2a 20 46 6f 72 77  SH_H_..../* Forw
57de0 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
57df0 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e 20   of structures. 
57e00 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75  */..typedef stru
57e10 63 74 20 48 61 73 68 20 48 61 73 68 3b 0d 0a 74  ct Hash Hash;..t
57e20 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61  ypedef struct Ha
57e30 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b  shElem HashElem;
57e40 0d 0a 0d 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74  ..../* A complet
57e50 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
57e60 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
57e70 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
57e80 75 63 74 75 72 65 2e 0d 0a 2a 2a 20 54 68 65 20  ucture...** The 
57e90 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69  internals of thi
57ea0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
57eb0 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
57ec0 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0d  paque -- client.
57ed0 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  .** code should 
57ee0 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61  not attempt to a
57ef0 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20  ccess or modify 
57f00 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  the fields of th
57f10 69 73 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a  is structure..**
57f20 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e   directly.  Chan
57f30 67 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ge this structur
57f40 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20  e only by using 
57f50 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  the routines bel
57f60 6f 77 2e 0d 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ow...** However,
57f70 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22 70 72   some of the "pr
57f80 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 66  ocedures" and "f
57f90 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f  unctions" for mo
57fa0 64 69 66 79 69 6e 67 20 61 6e 64 0d 0a 2a 2a 20  difying and..** 
57fb0 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
57fc0 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
57fd0 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
57fe0 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
57ff0 61 6b 65 0d 0a 2a 2a 20 74 68 69 73 20 73 74 72  ake..** this str
58000 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0d 0a  ucture opaque...
58010 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65  **..** All eleme
58020 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20  nts of the hash 
58030 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 73  table are on a s
58040 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
58050 6b 65 64 20 6c 69 73 74 2e 0d 0a 2a 2a 20 48 61  ked list...** Ha
58060 73 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73 20  sh.first points 
58070 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
58080 68 69 73 20 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a  his list...**..*
58090 2a 20 54 68 65 72 65 20 61 72 65 20 48 61 73 68  * There are Hash
580a0 2e 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 2e  .htsize buckets.
580b0 20 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 6f    Each bucket po
580c0 69 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 69  ints to a spot i
580d0 6e 0d 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c  n..** the global
580e0 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c   doubly-linked l
580f0 69 73 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e  ist.  The conten
58100 74 73 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74  ts of the bucket
58110 20 61 72 65 20 74 68 65 0d 0a 2a 2a 20 65 6c 65   are the..** ele
58120 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f 20  ment pointed to 
58130 70 6c 75 73 20 74 68 65 20 6e 65 78 74 20 5f 68  plus the next _h
58140 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e  t.count-1 elemen
58150 74 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0d  ts in the list..
58160 0a 2a 2a 0d 0a 2a 2a 20 48 61 73 68 2e 68 74 73  .**..** Hash.hts
58170 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74 20  ize and Hash.ht 
58180 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49 6e  may be zero.  In
58190 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b 75   that case looku
581a0 70 20 69 73 20 64 6f 6e 65 0d 0a 2a 2a 20 62 79  p is done..** by
581b0 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
581c0 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c   of the global l
581d0 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20  ist.  For small 
581e0 74 61 62 6c 65 73 2c 20 74 68 65 20 0d 0a 2a 2a  tables, the ..**
581f0 20 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69   Hash.ht table i
58200 73 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65  s never allocate
58210 64 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65  d because if the
58220 72 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65  re are few eleme
58230 6e 74 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 74  nts..** in the t
58240 61 62 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74  able, it is fast
58250 65 72 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61  er to do a linea
58260 72 20 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f  r search than to
58270 20 6d 61 6e 61 67 65 0d 0a 2a 2a 20 74 68 65 20   manage..** the 
58280 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  hash table...*/.
58290 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0d 0a  .struct Hash {..
582a0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
582b0 74 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e  tsize;      /* N
582c0 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73  umber of buckets
582d0 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
582e0 6c 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65  le */..  unsigne
582f0 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20  d int count;    
58300 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
58310 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20  entries in this 
58320 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 48 61 73 68  table */..  Hash
58330 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20  Elem *first;    
58340 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72        /* The fir
58350 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  st element of th
58360 65 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 73 74  e array */..  st
58370 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20 20  ruct _ht {      
58380 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68          /* the h
58390 61 73 68 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  ash table */..  
583a0 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
583b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
583c0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
583d0 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  s with this hash
583e0 20 2a 2f 0d 0a 20 20 20 20 48 61 73 68 45 6c 65   */..    HashEle
583f0 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20  m *chain;       
58400 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
58410 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69  o first entry wi
58420 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0d  th this hash */.
58430 0a 20 20 7d 20 2a 68 74 3b 0d 0a 7d 3b 0d 0a 0d  .  } *ht;..};...
58440 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74  ./* Each element
58450 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
58460 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  le is an instanc
58470 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
58480 6e 67 20 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72  ng ..** structur
58490 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  e.  All elements
584a0 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61   are stored on a
584b0 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c   single doubly-l
584c0 69 6e 6b 65 64 20 6c 69 73 74 2e 0d 0a 2a 2a 0d  inked list...**.
584d0 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20  .** Again, this 
584e0 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74  structure is int
584f0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
58500 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74  ue, but it can't
58510 20 72 65 61 6c 6c 79 0d 0a 2a 2a 20 62 65 20 6f   really..** be o
58520 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74  paque because it
58530 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72   is used by macr
58540 6f 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  os...*/..struct 
58550 48 61 73 68 45 6c 65 6d 20 7b 0d 0a 20 20 48 61  HashElem {..  Ha
58560 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70  shElem *next, *p
58570 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65  rev;       /* Ne
58580 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20  xt and previous 
58590 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
585a0 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 76 6f 69 64  table */..  void
585b0 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20   *data;         
585c0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
585d0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
585e0 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f   this element */
585f0 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
58600 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20  pKey; int nKey; 
58610 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74   /* Key associat
58620 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
58630 6d 65 6e 74 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f  ment */..};..../
58640 2a 0d 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75  *..** Access rou
58650 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74  tines.  To delet
58660 65 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c  e, insert a NULL
58670 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a 53   pointer...*/..S
58680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
58690 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e  id sqlite3HashIn
586a0 69 74 28 48 61 73 68 2a 29 3b 0d 0a 53 51 4c 49  it(Hash*);..SQLI
586b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
586c0 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65  *sqlite3HashInse
586d0 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  rt(Hash*, const 
586e0 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20  char *pKey, int 
586f0 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74  nKey, void *pDat
58700 61 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  a);..SQLITE_PRIV
58710 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
58720 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20  3HashFind(const 
58730 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Hash*, const cha
58740 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  r *pKey, int nKe
58750 79 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  y);..SQLITE_PRIV
58760 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
58770 48 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29  HashClear(Hash*)
58780 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 63 72  ;..../*..** Macr
58790 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  os for looping o
587a0 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73  ver all elements
587b0 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c 65   of a hash table
587c0 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0d  .  The idiom is.
587d0 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a  .** like this:..
587e0 2a 2a 0d 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b  **..**   Hash h;
587f0 0d 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20  ..**   HashElem 
58800 2a 70 3b 0d 0a 2a 2a 20 20 20 2e 2e 2e 0d 0a 2a  *p;..**   .....*
58810 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  *   for(p=sqlite
58820 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70  HashFirst(&h); p
58830 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
58840 78 74 28 70 29 29 7b 0d 0a 2a 2a 20 20 20 20 20  xt(p)){..**     
58850 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70  SomeStructure *p
58860 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73  Data = sqliteHas
58870 68 44 61 74 61 28 70 29 3b 0d 0a 2a 2a 20 20 20  hData(p);..**   
58880 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e    // do somethin
58890 67 20 77 69 74 68 20 70 44 61 74 61 0d 0a 2a 2a  g with pData..**
588a0 20 20 20 7d 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e     }..*/..#defin
588b0 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  e sqliteHashFirs
588c0 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73  t(H)  ((H)->firs
588d0 74 29 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t)..#define sqli
588e0 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20  teHashNext(E)   
588f0 28 28 45 29 2d 3e 6e 65 78 74 29 0d 0a 23 64 65  ((E)->next)..#de
58900 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44  fine sqliteHashD
58910 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64  ata(E)   ((E)->d
58920 61 74 61 29 0d 0a 2f 2a 20 23 64 65 66 69 6e 65  ata)../* #define
58930 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45   sqliteHashKey(E
58940 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29  )    ((E)->pKey)
58950 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0d   // NOT USED */.
58960 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ./* #define sqli
58970 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29  teHashKeysize(E)
58980 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f   ((E)->nKey)  //
58990 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0d 0a 0d 0a   NOT USED */....
589a0 2f 2a 0d 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  /*..** Number of
589b0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
589c0 73 68 20 74 61 62 6c 65 0d 0a 2a 2f 0d 0a 2f 2a  sh table..*/../*
589d0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
589e0 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48  ashCount(H)  ((H
589f0 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54  )->count) // NOT
58a00 20 55 53 45 44 20 2a 2f 0d 0a 0d 0a 23 65 6e 64   USED */....#end
58a10 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41  if /* _SQLITE_HA
58a20 53 48 5f 48 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a  SH_H_ */..../***
58a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
58a40 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  of hash.h ******
58a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
58a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
58a90 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
58aa0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
58ab0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
58ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
58ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
58ae0 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 69 6e  clude parse.h in
58af0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
58b00 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
58b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
58b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
58b30 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
58b40 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
58b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
58b70 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20  #define TK_SEMI 
58b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58b90 20 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23 64             1..#d
58ba0 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e  efine TK_EXPLAIN
58bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58bc0 20 20 20 20 20 20 20 20 20 32 0d 0a 23 64 65 66           2..#def
58bd0 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20  ine TK_QUERY    
58be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58bf0 20 20 20 20 20 20 20 33 0d 0a 23 64 65 66 69 6e         3..#defin
58c00 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20  e TK_PLAN       
58c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c20 20 20 20 20 20 34 0d 0a 23 64 65 66 69 6e 65 20       4..#define 
58c30 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20  TK_BEGIN        
58c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c50 20 20 20 35 0d 0a 23 64 65 66 69 6e 65 20 54 4b     5..#define TK
58c60 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
58c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c80 20 36 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   6..#define TK_D
58c90 45 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20  EFERRED         
58ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
58cb0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d  ..#define TK_IMM
58cc0 45 44 49 41 54 45 20 20 20 20 20 20 20 20 20 20  EDIATE          
58cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0d 0a               8..
58ce0 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55  #define TK_EXCLU
58cf0 53 49 56 45 20 20 20 20 20 20 20 20 20 20 20 20  SIVE            
58d00 20 20 20 20 20 20 20 20 20 20 20 39 0d 0a 23 64             9..#d
58d10 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20  efine TK_COMMIT 
58d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d30 20 20 20 20 20 20 20 20 31 30 0d 0a 23 64 65 66          10..#def
58d40 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20  ine TK_END      
58d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d60 20 20 20 20 20 20 31 31 0d 0a 23 64 65 66 69 6e        11..#defin
58d70 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20  e TK_ROLLBACK   
58d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d90 20 20 20 20 31 32 0d 0a 23 64 65 66 69 6e 65 20      12..#define 
58da0 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20  TK_SAVEPOINT    
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58dc0 20 20 31 33 0d 0a 23 64 65 66 69 6e 65 20 54 4b    13..#define TK
58dd0 5f 52 45 4c 45 41 53 45 20 20 20 20 20 20 20 20  _RELEASE        
58de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58df0 31 34 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  14..#define TK_T
58e00 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
58e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
58e20 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 42  ..#define TK_TAB
58e30 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
58e40 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0d 0a              16..
58e50 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54  #define TK_CREAT
58e60 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
58e70 20 20 20 20 20 20 20 20 20 20 31 37 0d 0a 23 64            17..#d
58e80 65 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20  efine TK_IF     
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ea0 20 20 20 20 20 20 20 20 31 38 0d 0a 23 64 65 66          18..#def
58eb0 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20  ine TK_NOT      
58ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ed0 20 20 20 20 20 20 31 39 0d 0a 23 64 65 66 69 6e        19..#defin
58ee0 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20 20 20  e TK_EXISTS     
58ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f00 20 20 20 20 32 30 0d 0a 23 64 65 66 69 6e 65 20      20..#define 
58f10 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20 20  TK_TEMP         
58f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f30 20 20 32 31 0d 0a 23 64 65 66 69 6e 65 20 54 4b    21..#define TK
58f40 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20  _LP             
58f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f60 32 32 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52  22..#define TK_R
58f70 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
58f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33                23
58f90 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20  ..#define TK_AS 
58fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fb0 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0d 0a              24..
58fc0 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41  #define TK_COMMA
58fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fe0 20 20 20 20 20 20 20 20 20 20 32 35 0d 0a 23 64            25..#d
58ff0 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 20  efine TK_ID     
59000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59010 20 20 20 20 20 20 20 20 32 36 0d 0a 23 64 65 66          26..#def
59020 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20  ine TK_INDEXED  
59030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59040 20 20 20 20 20 20 32 37 0d 0a 23 64 65 66 69 6e        27..#defin
59050 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20  e TK_ABORT      
59060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59070 20 20 20 20 32 38 0d 0a 23 64 65 66 69 6e 65 20      28..#define 
59080 54 4b 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 20  TK_ACTION       
59090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
590a0 20 20 32 39 0d 0a 23 64 65 66 69 6e 65 20 54 4b    29..#define TK
590b0 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 20 20  _AFTER          
590c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
590d0 33 30 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  30..#define TK_A
590e0 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20  NALYZE          
590f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31                31
59100 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43  ..#define TK_ASC
59110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59120 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0d 0a              32..
59130 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43  #define TK_ATTAC
59140 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
59150 20 20 20 20 20 20 20 20 20 20 33 33 0d 0a 23 64            33..#d
59160 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20  efine TK_BEFORE 
59170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59180 20 20 20 20 20 20 20 20 33 34 0d 0a 23 64 65 66          34..#def
59190 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20  ine TK_BY       
591a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591b0 20 20 20 20 20 20 33 35 0d 0a 23 64 65 66 69 6e        35..#defin
591c0 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 20 20  e TK_CASCADE    
591d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591e0 20 20 20 20 33 36 0d 0a 23 64 65 66 69 6e 65 20      36..#define 
591f0 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 20  TK_CAST         
59200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59210 20 20 33 37 0d 0a 23 64 65 66 69 6e 65 20 54 4b    37..#define TK
59220 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20  _COLUMNKW       
59230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59240 33 38 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  38..#define TK_C
59250 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20  ONFLICT         
59260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39                39
59270 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54  ..#define TK_DAT
59280 41 42 41 53 45 20 20 20 20 20 20 20 20 20 20 20  ABASE           
59290 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0d 0a              40..
592a0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20  #define TK_DESC 
592b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
592c0 20 20 20 20 20 20 20 20 20 20 34 31 0d 0a 23 64            41..#d
592d0 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 20  efine TK_DETACH 
592e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
592f0 20 20 20 20 20 20 20 20 34 32 0d 0a 23 64 65 66          42..#def
59300 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20  ine TK_EACH     
59310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59320 20 20 20 20 20 20 34 33 0d 0a 23 64 65 66 69 6e        43..#defin
59330 65 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20  e TK_FAIL       
59340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59350 20 20 20 20 34 34 0d 0a 23 64 65 66 69 6e 65 20      44..#define 
59360 54 4b 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20  TK_FOR          
59370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59380 20 20 34 35 0d 0a 23 64 65 66 69 6e 65 20 54 4b    45..#define TK
59390 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20 20 20  _IGNORE         
593a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
593b0 34 36 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  46..#define TK_I
593c0 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 20  NITIALLY        
593d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37                47
593e0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53  ..#define TK_INS
593f0 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  TEAD            
59400 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0d 0a              48..
59410 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f  #define TK_LIKE_
59420 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
59430 20 20 20 20 20 20 20 20 20 20 34 39 0d 0a 23 64            49..#d
59440 65 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20  efine TK_MATCH  
59450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59460 20 20 20 20 20 20 20 20 35 30 0d 0a 23 64 65 66          50..#def
59470 69 6e 65 20 54 4b 5f 4e 4f 20 20 20 20 20 20 20  ine TK_NO       
59480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59490 20 20 20 20 20 20 35 31 0d 0a 23 64 65 66 69 6e        51..#defin
594a0 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20  e TK_KEY        
594b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
594c0 20 20 20 20 35 32 0d 0a 23 64 65 66 69 6e 65 20      52..#define 
594d0 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20  TK_OF           
594e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
594f0 20 20 35 33 0d 0a 23 64 65 66 69 6e 65 20 54 4b    53..#define TK
59500 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 20  _OFFSET         
59510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59520 35 34 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50  54..#define TK_P
59530 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20  RAGMA           
59540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35                55
59550 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49  ..#define TK_RAI
59560 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
59570 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0d 0a              56..
59580 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41  #define TK_REPLA
59590 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CE              
595a0 20 20 20 20 20 20 20 20 20 20 35 37 0d 0a 23 64            57..#d
595b0 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 43  efine TK_RESTRIC
595c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
595d0 20 20 20 20 20 20 20 20 35 38 0d 0a 23 64 65 66          58..#def
595e0 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20  ine TK_ROW      
595f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59600 20 20 20 20 20 20 35 39 0d 0a 23 64 65 66 69 6e        59..#defin
59610 65 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20  e TK_TRIGGER    
59620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59630 20 20 20 20 36 30 0d 0a 23 64 65 66 69 6e 65 20      60..#define 
59640 54 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20  TK_VACUUM       
59650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59660 20 20 36 31 0d 0a 23 64 65 66 69 6e 65 20 54 4b    61..#define TK
59670 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20  _VIEW           
59680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59690 36 32 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56  62..#define TK_V
596a0 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20 20  IRTUAL          
596b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33                63
596c0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49  ..#define TK_REI
596d0 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20  NDEX            
596e0 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0d 0a              64..
596f0 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d  #define TK_RENAM
59700 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
59710 20 20 20 20 20 20 20 20 20 20 36 35 0d 0a 23 64            65..#d
59720 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b  efine TK_CTIME_K
59730 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
59740 20 20 20 20 20 20 20 20 36 36 0d 0a 23 64 65 66          66..#def
59750 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20  ine TK_ANY      
59760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59770 20 20 20 20 20 20 36 37 0d 0a 23 64 65 66 69 6e        67..#defin
59780 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20  e TK_OR         
59790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
597a0 20 20 20 20 36 38 0d 0a 23 64 65 66 69 6e 65 20      68..#define 
597b0 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20  TK_AND          
597c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
597d0 20 20 36 39 0d 0a 23 64 65 66 69 6e 65 20 54 4b    69..#define TK
597e0 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 20  _IS             
597f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59800 37 30 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42  70..#define TK_B
59810 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20  ETWEEN          
59820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31                71
59830 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20  ..#define TK_IN 
59840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59850 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0d 0a              72..
59860 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c  #define TK_ISNUL
59870 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
59880 20 20 20 20 20 20 20 20 20 20 37 33 0d 0a 23 64            73..#d
59890 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  efine TK_NOTNULL
598a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
598b0 20 20 20 20 20 20 20 20 37 34 0d 0a 23 64 65 66          74..#def
598c0 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20  ine TK_NE       
598d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
598e0 20 20 20 20 20 20 37 35 0d 0a 23 64 65 66 69 6e        75..#defin
598f0 65 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20  e TK_EQ         
59900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59910 20 20 20 20 37 36 0d 0a 23 64 65 66 69 6e 65 20      76..#define 
59920 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20  TK_GT           
59930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59940 20 20 37 37 0d 0a 23 64 65 66 69 6e 65 20 54 4b    77..#define TK
59950 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _LE             
59960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59970 37 38 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c  78..#define TK_L
59980 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
59990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39                79
599a0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20  ..#define TK_GE 
599b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
599c0 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0d 0a              80..
599d0 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50  #define TK_ESCAP
599e0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
599f0 20 20 20 20 20 20 20 20 20 20 38 31 0d 0a 23 64            81..#d
59a00 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20  efine TK_BITAND 
59a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a20 20 20 20 20 20 20 20 20 38 32 0d 0a 23 64 65 66          82..#def
59a30 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20  ine TK_BITOR    
59a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a50 20 20 20 20 20 20 38 33 0d 0a 23 64 65 66 69 6e        83..#defin
59a60 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20  e TK_LSHIFT     
59a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a80 20 20 20 20 38 34 0d 0a 23 64 65 66 69 6e 65 20      84..#define 
59a90 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20 20  TK_RSHIFT       
59aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ab0 20 20 38 35 0d 0a 23 64 65 66 69 6e 65 20 54 4b    85..#define TK
59ac0 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20  _PLUS           
59ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ae0 38 36 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d  86..#define TK_M
59af0 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20  INUS            
59b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37                87
59b10 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41  ..#define TK_STA
59b20 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
59b30 20 20 20 20 20 20 20 20 20 20 20 20 38 38 0d 0a              88..
59b40 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48  #define TK_SLASH
59b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b60 20 20 20 20 20 20 20 20 20 20 38 39 0d 0a 23 64            89..#d
59b70 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20  efine TK_REM    
59b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b90 20 20 20 20 20 20 20 20 39 30 0d 0a 23 64 65 66          90..#def
59ba0 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20  ine TK_CONCAT   
59bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59bc0 20 20 20 20 20 20 39 31 0d 0a 23 64 65 66 69 6e        91..#defin
59bd0 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20  e TK_COLLATE    
59be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59bf0 20 20 20 20 39 32 0d 0a 23 64 65 66 69 6e 65 20      92..#define 
59c00 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20  TK_BITNOT       
59c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c20 20 20 39 33 0d 0a 23 64 65 66 69 6e 65 20 54 4b    93..#define TK
59c30 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20  _STRING         
59c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c50 39 34 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a  94..#define TK_J
59c60 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20  OIN_KW          
59c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35                95
59c80 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  ..#define TK_CON
59c90 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20 20  STRAINT         
59ca0 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0d 0a              96..
59cb0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55  #define TK_DEFAU
59cc0 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LT              
59cd0 20 20 20 20 20 20 20 20 20 20 39 37 0d 0a 23 64            97..#d
59ce0 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20  efine TK_NULL   
59cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d00 20 20 20 20 20 20 20 20 39 38 0d 0a 23 64 65 66          98..#def
59d10 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20  ine TK_PRIMARY  
59d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d30 20 20 20 20 20 20 39 39 0d 0a 23 64 65 66 69 6e        99..#defin
59d40 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20 20 20  e TK_UNIQUE     
59d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d60 20 20 20 20 31 30 30 0d 0a 23 64 65 66 69 6e 65      100..#define
59d70 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20   TK_CHECK       
59d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d90 20 20 20 31 30 31 0d 0a 23 64 65 66 69 6e 65 20     101..#define 
59da0 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20  TK_REFERENCES   
59db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59dc0 20 20 31 30 32 0d 0a 23 64 65 66 69 6e 65 20 54    102..#define T
59dd0 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20  K_AUTOINCR      
59de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59df0 20 31 30 33 0d 0a 23 64 65 66 69 6e 65 20 54 4b   103..#define TK
59e00 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  _ON             
59e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59e20 31 30 34 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f  104..#define TK_
59e30 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20  INSERT          
59e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
59e50 30 35 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  05..#define TK_D
59e60 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20  ELETE           
59e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
59e80 36 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50  6..#define TK_UP
59e90 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  DATE            
59ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
59eb0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54  ..#define TK_SET
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 20 20 20 31 30 38 0d              108.
59ee0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
59ef0 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  RRABLE          
59f00 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0d 0a             109..
59f10 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49  #define TK_FOREI
59f20 47 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  GN              
59f30 20 20 20 20 20 20 20 20 20 20 31 31 30 0d 0a 23            110..#
59f40 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20  define TK_DROP  
59f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f60 20 20 20 20 20 20 20 20 20 31 31 31 0d 0a 23 64           111..#d
59f70 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20  efine TK_UNION  
59f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f90 20 20 20 20 20 20 20 20 31 31 32 0d 0a 23 64 65          112..#de
59fa0 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20  fine TK_ALL     
59fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59fc0 20 20 20 20 20 20 20 31 31 33 0d 0a 23 64 65 66         113..#def
59fd0 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20  ine TK_EXCEPT   
59fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ff0 20 20 20 20 20 20 31 31 34 0d 0a 23 64 65 66 69        114..#defi
5a000 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  ne TK_INTERSECT 
5a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a020 20 20 20 20 20 31 31 35 0d 0a 23 64 65 66 69 6e       115..#defin
5a030 65 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20  e TK_SELECT     
5a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a050 20 20 20 20 31 31 36 0d 0a 23 64 65 66 69 6e 65      116..#define
5a060 20 54 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20   TK_DISTINCT    
5a070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a080 20 20 20 31 31 37 0d 0a 23 64 65 66 69 6e 65 20     117..#define 
5a090 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 20 20 20  TK_DOT          
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0b0 20 20 31 31 38 0d 0a 23 64 65 66 69 6e 65 20 54    118..#define T
5a0c0 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 20 20  K_FROM          
5a0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0e0 20 31 31 39 0d 0a 23 64 65 66 69 6e 65 20 54 4b   119..#define TK
5a0f0 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 20  _JOIN           
5a100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a110 31 32 30 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f  120..#define TK_
5a120 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20  USING           
5a130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5a140 32 31 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f  21..#define TK_O
5a150 52 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20  RDER            
5a160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
5a170 32 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52  2..#define TK_GR
5a180 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 20 20  OUP             
5a190 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33               123
5a1a0 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 56  ..#define TK_HAV
5a1b0 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
5a1c0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0d              124.
5a1d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49  .#define TK_LIMI
5a1e0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
5a1f0 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0d 0a             125..
5a200 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45  #define TK_WHERE
5a210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a220 20 20 20 20 20 20 20 20 20 20 31 32 36 0d 0a 23            126..#
5a230 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20  define TK_INTO  
5a240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a250 20 20 20 20 20 20 20 20 20 31 32 37 0d 0a 23 64           127..#d
5a260 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20  efine TK_VALUES 
5a270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a280 20 20 20 20 20 20 20 20 31 32 38 0d 0a 23 64 65          128..#de
5a290 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20  fine TK_INTEGER 
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 31 32 39 0d 0a 23 64 65 66         129..#def
5a2c0 69 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20  ine TK_FLOAT    
5a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2e0 20 20 20 20 20 20 31 33 30 0d 0a 23 64 65 66 69        130..#defi
5a2f0 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20  ne TK_BLOB      
5a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a310 20 20 20 20 20 31 33 31 0d 0a 23 64 65 66 69 6e       131..#defin
5a320 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 20 20  e TK_REGISTER   
5a330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a340 20 20 20 20 31 33 32 0d 0a 23 64 65 66 69 6e 65      132..#define
5a350 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 20 20   TK_VARIABLE    
5a360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a370 20 20 20 31 33 33 0d 0a 23 64 65 66 69 6e 65 20     133..#define 
5a380 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 20  TK_CASE         
5a390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3a0 20 20 31 33 34 0d 0a 23 64 65 66 69 6e 65 20 54    134..#define T
5a3b0 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20  K_WHEN          
5a3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3d0 20 31 33 35 0d 0a 23 64 65 66 69 6e 65 20 54 4b   135..#define TK
5a3e0 5f 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20  _THEN           
5a3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a400 31 33 36 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f  136..#define TK_
5a410 45 4c 53 45 20 20 20 20 20 20 20 20 20 20 20 20  ELSE            
5a420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5a430 33 37 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  37..#define TK_I
5a440 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20  NDEX            
5a450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
5a460 38 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c  8..#define TK_AL
5a470 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
5a480 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39               139
5a490 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44  ..#define TK_ADD
5a4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4b0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0d              140.
5a4c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54  .#define TK_TO_T
5a4d0 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20 20  EXT             
5a4e0 20 20 20 20 20 20 20 20 20 20 20 31 34 31 0d 0a             141..
5a4f0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c  #define TK_TO_BL
5a500 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OB              
5a510 20 20 20 20 20 20 20 20 20 20 31 34 32 0d 0a 23            142..#
5a520 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d  define TK_TO_NUM
5a530 45 52 49 43 20 20 20 20 20 20 20 20 20 20 20 20  ERIC            
5a540 20 20 20 20 20 20 20 20 20 31 34 33 0d 0a 23 64           143..#d
5a550 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20  efine TK_TO_INT 
5a560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a570 20 20 20 20 20 20 20 20 31 34 34 0d 0a 23 64 65          144..#de
5a580 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20  fine TK_TO_REAL 
5a590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5a0 20 20 20 20 20 20 20 31 34 35 0d 0a 23 64 65 66         145..#def
5a5b0 69 6e 65 20 54 4b 5f 49 53 4e 4f 54 20 20 20 20  ine TK_ISNOT    
5a5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5d0 20 20 20 20 20 20 31 34 36 0d 0a 23 64 65 66 69        146..#defi
5a5e0 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c  ne TK_END_OF_FIL
5a5f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
5a600 20 20 20 20 20 31 34 37 0d 0a 23 64 65 66 69 6e       147..#defin
5a610 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 20  e TK_ILLEGAL    
5a620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a630 20 20 20 20 31 34 38 0d 0a 23 64 65 66 69 6e 65      148..#define
5a640 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20 20   TK_SPACE       
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a660 20 20 20 31 34 39 0d 0a 23 64 65 66 69 6e 65 20     149..#define 
5a670 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49  TK_UNCLOSED_STRI
5a680 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
5a690 20 20 31 35 30 0d 0a 23 64 65 66 69 6e 65 20 54    150..#define T
5a6a0 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  K_FUNCTION      
5a6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6c0 20 31 35 31 0d 0a 23 64 65 66 69 6e 65 20 54 4b   151..#define TK
5a6d0 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20  _COLUMN         
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6f0 31 35 32 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f  152..#define TK_
5a700 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20  AGG_FUNCTION    
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5a720 35 33 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  53..#define TK_A
5a730 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20  GG_COLUMN       
5a740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
5a750 34 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  4..#define TK_CO
5a760 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20  NST_FUNC        
5a770 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 35               155
5a780 0d 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49  ..#define TK_UMI
5a790 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20  NUS             
5a7a0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 36 0d              156.
5a7b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55  .#define TK_UPLU
5a7c0 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
5a7d0 20 20 20 20 20 20 20 20 20 20 20 31 35 37 0d 0a             157..
5a7e0 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
5a7f0 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68  * End of parse.h
5a800 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
5a810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a830 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
5a840 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
5a850 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
5a860 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
5a870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a880 2a 2f 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  */..#include <st
5a890 64 69 6f 2e 68 3e 0d 0a 23 69 6e 63 6c 75 64 65  dio.h>..#include
5a8a0 20 3c 73 74 64 6c 69 62 2e 68 3e 0d 0a 23 69 6e   <stdlib.h>..#in
5a8b0 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e  clude <string.h>
5a8c0 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65  ..#include <asse
5a8d0 72 74 2e 68 3e 0d 0a 23 69 6e 63 6c 75 64 65 20  rt.h>..#include 
5a8e0 3c 73 74 64 64 65 66 2e 68 3e 0d 0a 0d 0a 2f 2a  <stddef.h>..../*
5a8f0 0d 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e  ..** If compilin
5a900 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f  g for a processo
5a910 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f  r that lacks flo
5a920 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70  ating point supp
5a930 6f 72 74 2c 0d 0a 2a 2a 20 73 75 62 73 74 69 74  ort,..** substit
5a940 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20  ute integer for 
5a950 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0d 0a  floating-point..
5a960 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
5a970 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
5a980 50 4f 49 4e 54 0d 0a 23 20 64 65 66 69 6e 65 20  POINT..# define 
5a990 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e  double sqlite_in
5a9a0 74 36 34 0d 0a 23 20 64 65 66 69 6e 65 20 66 6c  t64..# define fl
5a9b0 6f 61 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  oat sqlite_int64
5a9c0 0d 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44  ..# define LONGD
5a9d0 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 74  OUBLE_TYPE sqlit
5a9e0 65 5f 69 6e 74 36 34 0d 0a 23 20 69 66 6e 64 65  e_int64..# ifnde
5a9f0 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  f SQLITE_BIG_DBL
5aa00 0d 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c  ..#   define SQL
5aa10 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 28 28 73  ITE_BIG_DBL (((s
5aa20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 31 29 3c  qlite3_int64)1)<
5aa30 3c 35 30 29 0d 0a 23 20 65 6e 64 69 66 0d 0a 23  <50)..# endif..#
5aa40 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
5aa50 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
5aa60 43 53 20 31 0d 0a 23 20 64 65 66 69 6e 65 20 53  CS 1..# define S
5aa70 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
5aa80 20 31 0d 0a 23 20 75 6e 64 65 66 20 53 51 4c 49   1..# undef SQLI
5aa90 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
5aaa0 36 34 42 49 54 5f 46 4c 4f 41 54 0d 0a 23 20 75  64BIT_FLOAT..# u
5aab0 6e 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45  ndef SQLITE_HAVE
5aac0 5f 49 53 4e 41 4e 0d 0a 23 65 6e 64 69 66 0d 0a  _ISNAN..#endif..
5aad0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42  #ifndef SQLITE_B
5aae0 49 47 5f 44 42 4c 0d 0a 23 20 64 65 66 69 6e 65  IG_DBL..# define
5aaf0 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20   SQLITE_BIG_DBL 
5ab00 28 31 65 39 39 29 0d 0a 23 65 6e 64 69 66 0d 0a  (1e99)..#endif..
5ab10 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 4d 49 54 5f 54 45  ../*..** OMIT_TE
5ab20 4d 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31  MPDB is set to 1
5ab30 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
5ab40 54 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65  TEMPDB is define
5ab50 64 2c 20 6f 72 20 30 0d 0a 2a 2a 20 61 66 74 65  d, or 0..** afte
5ab60 72 77 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68  rward. Having th
5ab70 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20  is macro allows 
5ab80 75 73 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  us to cause the 
5ab90 43 20 63 6f 6d 70 69 6c 65 72 20 0d 0a 2a 2a 20  C compiler ..** 
5aba0 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 73 65  to omit code use
5abb0 64 20 62 79 20 54 45 4d 50 20 74 61 62 6c 65 73  d by TEMP tables
5abc0 20 77 69 74 68 6f 75 74 20 6d 65 73 73 79 20 23   without messy #
5abd0 69 66 6e 64 65 66 20 73 74 61 74 65 6d 65 6e 74  ifndef statement
5abe0 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53  s...*/..#ifdef S
5abf0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
5ac00 42 0d 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f  B..#define OMIT_
5ac10 54 45 4d 50 44 42 20 31 0d 0a 23 65 6c 73 65 0d  TEMPDB 1..#else.
5ac20 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45  .#define OMIT_TE
5ac30 4d 50 44 42 20 30 0d 0a 23 65 6e 64 69 66 0d 0a  MPDB 0..#endif..
5ac40 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 22 66 69  ../*..** The "fi
5ac50 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65  le format" numbe
5ac60 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  r is an integer 
5ac70 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e  that is incremen
5ac80 74 65 64 20 77 68 65 6e 65 76 65 72 0d 0a 2a 2a  ted whenever..**
5ac90 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c 20   the VDBE-level 
5aca0 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61 6e  file format chan
5acb0 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  ges.  The follow
5acc0 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  ing macros defin
5acd0 65 20 74 68 65 0d 0a 2a 2a 20 74 68 65 20 64 65  e the..** the de
5ace0 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61  fault file forma
5acf0 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61  t for new databa
5ad00 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69  ses and the maxi
5ad10 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0d  mum file format.
5ad20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62  .** that the lib
5ad30 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0d 0a  rary can read...
5ad40 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
5ad50 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d  TE_MAX_FILE_FORM
5ad60 41 54 20 34 0d 0a 23 69 66 6e 64 65 66 20 53 51  AT 4..#ifndef SQ
5ad70 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
5ad80 45 5f 46 4f 52 4d 41 54 0d 0a 23 20 64 65 66 69  E_FORMAT..# defi
5ad90 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
5ada0 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0d  T_FILE_FORMAT 4.
5adb0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
5adc0 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74  * Determine whet
5add0 68 65 72 20 74 72 69 67 67 65 72 73 20 61 72 65  her triggers are
5ade0 20 72 65 63 75 72 73 69 76 65 20 62 79 20 64 65   recursive by de
5adf0 66 61 75 6c 74 2e 20 20 54 68 69 73 20 63 61 6e  fault.  This can
5ae00 20 62 65 0d 0a 2a 2a 20 63 68 61 6e 67 65 64 20   be..** changed 
5ae10 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e  at run-time usin
5ae20 67 20 61 20 70 72 61 67 6d 61 2e 0d 0a 2a 2f 0d  g a pragma...*/.
5ae30 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5ae40 44 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56  DEFAULT_RECURSIV
5ae50 45 5f 54 52 49 47 47 45 52 53 0d 0a 23 20 64 65  E_TRIGGERS..# de
5ae60 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
5ae70 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54 52  ULT_RECURSIVE_TR
5ae80 49 47 47 45 52 53 20 30 0d 0a 23 65 6e 64 69 66  IGGERS 0..#endif
5ae90 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 6f 76 69  ..../*..** Provi
5aea0 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c  de a default val
5aeb0 75 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45  ue for SQLITE_TE
5aec0 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65  MP_STORE in case
5aed0 20 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69   it is not speci
5aee0 66 69 65 64 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20  fied..** on the 
5aef0 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0d 0a 2a 2f  command-line..*/
5af00 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
5af10 5f 54 45 4d 50 5f 53 54 4f 52 45 0d 0a 23 20 64  _TEMP_STORE..# d
5af20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
5af30 50 5f 53 54 4f 52 45 20 31 0d 0a 23 65 6e 64 69  P_STORE 1..#endi
5af40 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 43 43 20  f..../*..** GCC 
5af50 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20  does not define 
5af60 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d  the offsetof() m
5af70 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61  acro so we'll ha
5af80 76 65 20 74 6f 20 64 6f 20 69 74 0d 0a 2a 2a 20  ve to do it..** 
5af90 6f 75 72 73 65 6c 76 65 73 2e 0d 0a 2a 2f 0d 0a  ourselves...*/..
5afa0 23 69 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66  #ifndef offsetof
5afb0 0d 0a 23 64 65 66 69 6e 65 20 6f 66 66 73 65 74  ..#define offset
5afc0 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46 49 45  of(STRUCTURE,FIE
5afd0 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68 61 72  LD) ((int)((char
5afe0 2a 29 26 28 28 53 54 52 55 43 54 55 52 45 2a 29  *)&((STRUCTURE*)
5aff0 30 29 2d 3e 46 49 45 4c 44 29 29 0d 0a 23 65 6e  0)->FIELD))..#en
5b000 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68  dif..../*..** Ch
5b010 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
5b020 69 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20  is machine uses 
5b030 45 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62  EBCDIC.  (Yes, b
5b040 65 6c 69 65 76 65 20 69 74 20 6f 72 0d 0a 2a 2a  elieve it or..**
5b050 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20   not, there are 
5b060 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f  still machines o
5b070 75 74 20 74 68 65 72 65 20 74 68 61 74 20 75 73  ut there that us
5b080 65 20 45 42 43 44 49 43 2e 29 0d 0a 2a 2f 0d 0a  e EBCDIC.)..*/..
5b090 23 69 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31  #if 'A' == '\301
5b0a0 27 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  '..# define SQLI
5b0b0 54 45 5f 45 42 43 44 49 43 20 31 0d 0a 23 65 6c  TE_EBCDIC 1..#el
5b0c0 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  se..# define SQL
5b0d0 49 54 45 5f 41 53 43 49 49 20 31 0d 0a 23 65 6e  ITE_ASCII 1..#en
5b0e0 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  dif..../*..** In
5b0f0 74 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20  tegers of known 
5b100 73 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79  sizes.  These ty
5b110 70 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61  pedefs might cha
5b120 6e 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63  nge for architec
5b130 74 75 72 65 73 0d 0a 2a 2a 20 77 68 65 72 65 20  tures..** where 
5b140 74 68 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20  the sizes very. 
5b150 20 50 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   Preprocessor ma
5b160 63 72 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62  cros are availab
5b170 6c 65 20 73 6f 20 74 68 61 74 20 74 68 65 0d 0a  le so that the..
5b180 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62 65 20  ** types can be 
5b190 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72 65 64  conveniently red
5b1a0 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c  efined at compil
5b1b0 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20 74 68  e-type.  Like th
5b1c0 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  is:..**..**     
5b1d0 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54      cc '-DUINTPT
5b1e0 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67  R_TYPE=long long
5b1f0 20 69 6e 74 27 20 2e 2e 2e 0d 0a 2a 2f 0d 0a 23   int' .....*/..#
5b200 69 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59  ifndef UINT32_TY
5b210 50 45 0d 0a 23 20 69 66 64 65 66 20 48 41 56 45  PE..# ifdef HAVE
5b220 5f 55 49 4e 54 33 32 5f 54 0d 0a 23 20 20 64 65  _UINT32_T..#  de
5b230 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45  fine UINT32_TYPE
5b240 20 75 69 6e 74 33 32 5f 74 0d 0a 23 20 65 6c 73   uint32_t..# els
5b250 65 0d 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  e..#  define UIN
5b260 54 33 32 5f 54 59 50 45 20 75 6e 73 69 67 6e 65  T32_TYPE unsigne
5b270 64 20 69 6e 74 0d 0a 23 20 65 6e 64 69 66 0d 0a  d int..# endif..
5b280 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
5b290 55 49 4e 54 31 36 5f 54 59 50 45 0d 0a 23 20 69  UINT16_TYPE..# i
5b2a0 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36  fdef HAVE_UINT16
5b2b0 5f 54 0d 0a 23 20 20 64 65 66 69 6e 65 20 55 49  _T..#  define UI
5b2c0 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36  NT16_TYPE uint16
5b2d0 5f 74 0d 0a 23 20 65 6c 73 65 0d 0a 23 20 20 64  _t..# else..#  d
5b2e0 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50  efine UINT16_TYP
5b2f0 45 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74  E unsigned short
5b300 20 69 6e 74 0d 0a 23 20 65 6e 64 69 66 0d 0a 23   int..# endif..#
5b310 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66 20 49  endif..#ifndef I
5b320 4e 54 31 36 5f 54 59 50 45 0d 0a 23 20 69 66 64  NT16_TYPE..# ifd
5b330 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0d  ef HAVE_INT16_T.
5b340 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36  .#  define INT16
5b350 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 0d 0a 23  _TYPE int16_t..#
5b360 20 65 6c 73 65 0d 0a 23 20 20 64 65 66 69 6e 65   else..#  define
5b370 20 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f 72   INT16_TYPE shor
5b380 74 20 69 6e 74 0d 0a 23 20 65 6e 64 69 66 0d 0a  t int..# endif..
5b390 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
5b3a0 55 49 4e 54 38 5f 54 59 50 45 0d 0a 23 20 69 66  UINT8_TYPE..# if
5b3b0 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38 5f 54  def HAVE_UINT8_T
5b3c0 0d 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  ..#  define UINT
5b3d0 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0d 0a  8_TYPE uint8_t..
5b3e0 23 20 65 6c 73 65 0d 0a 23 20 20 64 65 66 69 6e  # else..#  defin
5b3f0 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73  e UINT8_TYPE uns
5b400 69 67 6e 65 64 20 63 68 61 72 0d 0a 23 20 65 6e  igned char..# en
5b410 64 69 66 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  dif..#endif..#if
5b420 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 0d 0a  ndef INT8_TYPE..
5b430 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54  # ifdef HAVE_INT
5b440 38 5f 54 0d 0a 23 20 20 64 65 66 69 6e 65 20 49  8_T..#  define I
5b450 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0d  NT8_TYPE int8_t.
5b460 0a 23 20 65 6c 73 65 0d 0a 23 20 20 64 65 66 69  .# else..#  defi
5b470 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67  ne INT8_TYPE sig
5b480 6e 65 64 20 63 68 61 72 0d 0a 23 20 65 6e 64 69  ned char..# endi
5b490 66 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64  f..#endif..#ifnd
5b4a0 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  ef LONGDOUBLE_TY
5b4b0 50 45 0d 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e  PE..# define LON
5b4c0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e  GDOUBLE_TYPE lon
5b4d0 67 20 64 6f 75 62 6c 65 0d 0a 23 65 6e 64 69 66  g double..#endif
5b4e0 0d 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  ..typedef sqlite
5b4f0 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20  _int64 i64;     
5b500 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73       /* 8-byte s
5b510 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
5b520 0d 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  ..typedef sqlite
5b530 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20  _uint64 u64;    
5b540 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 75       /* 8-byte u
5b550 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20  nsigned integer 
5b560 2a 2f 0d 0a 74 79 70 65 64 65 66 20 55 49 4e 54  */..typedef UINT
5b570 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20  32_TYPE u32;    
5b580 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65         /* 4-byte
5b590 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
5b5a0 72 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20 55 49  r */..typedef UI
5b5b0 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20  NT16_TYPE u16;  
5b5c0 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79           /* 2-by
5b5d0 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
5b5e0 67 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20  ger */..typedef 
5b5f0 49 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20  INT16_TYPE i16; 
5b600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d             /* 2-
5b610 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
5b620 67 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20  ger */..typedef 
5b630 55 49 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20  UINT8_TYPE u8;  
5b640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d             /* 1-
5b650 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e  byte unsigned in
5b660 74 65 67 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65  teger */..typede
5b670 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38 3b 20  f INT8_TYPE i8; 
5b680 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b690 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  1-byte signed in
5b6a0 74 65 67 65 72 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  teger */..../*..
5b6b0 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33  ** SQLITE_MAX_U3
5b6c0 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74  2 is a u64 const
5b6d0 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20  ant that is the 
5b6e0 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75  maximum u64 valu
5b6f0 65 0d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62  e..** that can b
5b700 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33  e stored in a u3
5b710 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f  2 without loss o
5b720 66 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c  f data.  The val
5b730 75 65 0d 0a 2a 2a 20 69 73 20 30 78 30 30 30 30  ue..** is 0x0000
5b740 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 20 42  0000ffffffff.  B
5b750 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 71 75  ut because of qu
5b760 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d  irks of some com
5b770 70 69 6c 65 72 73 2c 20 77 65 0d 0a 2a 2a 20 68  pilers, we..** h
5b780 61 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 74  ave to specify t
5b790 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  he value in the 
5b7a0 6c 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 6d  less intuitive m
5b7b0 61 6e 6e 65 72 20 73 68 6f 77 6e 3a 0d 0a 2a 2f  anner shown:..*/
5b7c0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
5b7d0 5f 4d 41 58 5f 55 33 32 20 20 28 28 28 28 75 36  _MAX_U32  ((((u6
5b7e0 34 29 31 29 3c 3c 33 32 29 2d 31 29 0d 0a 0d 0a  4)1)<<32)-1)....
5b7f0 2f 2a 0d 0a 2a 2a 20 54 68 65 20 64 61 74 61 74  /*..** The datat
5b800 79 70 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ype used to stor
5b810 65 20 65 73 74 69 6d 61 74 65 73 20 6f 66 20 74  e estimates of t
5b820 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
5b830 73 20 69 6e 20 61 0d 0a 2a 2a 20 74 61 62 6c 65  s in a..** table
5b840 20 6f 72 20 69 6e 64 65 78 2e 20 20 54 68 69 73   or index.  This
5b850 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
5b860 69 6e 74 65 67 65 72 20 74 79 70 65 2e 20 20 46  integer type.  F
5b870 6f 72 20 39 39 2e 39 25 20 6f 66 0d 0a 2a 2a 20  or 99.9% of..** 
5b880 74 68 65 20 77 6f 72 6c 64 2c 20 61 20 33 32 2d  the world, a 32-
5b890 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 73  bit integer is s
5b8a0 75 66 66 69 63 69 65 6e 74 2e 20 20 42 75 74 20  ufficient.  But 
5b8b0 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  a 64-bit integer
5b8c0 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  ..** can be used
5b8d0 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
5b8e0 20 69 66 20 64 65 73 69 72 65 64 2e 0d 0a 2a 2f   if desired...*/
5b8f0 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
5b900 36 34 42 49 54 5f 53 54 41 54 53 0d 0a 20 74 79  64BIT_STATS.. ty
5b910 70 65 64 65 66 20 75 36 34 20 74 52 6f 77 63 6e  pedef u64 tRowcn
5b920 74 3b 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20  t;    /* 64-bit 
5b930 6f 6e 6c 79 20 69 66 20 72 65 71 75 65 73 74 65  only if requeste
5b940 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
5b950 65 20 2a 2f 0d 0a 23 65 6c 73 65 0d 0a 20 74 79  e */..#else.. ty
5b960 70 65 64 65 66 20 75 33 32 20 74 52 6f 77 63 6e  pedef u32 tRowcn
5b970 74 3b 20 20 20 20 2f 2a 20 33 32 2d 62 69 74 20  t;    /* 32-bit 
5b980 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a  is the default *
5b990 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  /..#endif..../*.
5b9a0 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65  .** Macros to de
5b9b0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
5b9c0 74 68 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62  the machine is b
5b9d0 69 67 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64  ig or little end
5b9e0 69 61 6e 2c 0d 0a 2a 2a 20 65 76 61 6c 75 61 74  ian,..** evaluat
5b9f0 65 64 20 61 74 20 72 75 6e 74 69 6d 65 2e 0d 0a  ed at runtime...
5ba00 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
5ba10 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0d 0a  E_AMALGAMATION..
5ba20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
5ba30 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
5ba40 6f 6e 65 20 3d 20 31 3b 0d 0a 23 65 6c 73 65 0d  one = 1;..#else.
5ba50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5ba60 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65  const int sqlite
5ba70 33 6f 6e 65 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23  3one;..#endif..#
5ba80 69 66 20 64 65 66 69 6e 65 64 28 69 33 38 36 29  if defined(i386)
5ba90 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33   || defined(__i3
5baa0 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  86__) || defined
5bab0 28 5f 4d 5f 49 58 38 36 29 5c 0d 0a 20 20 20 20  (_M_IX86)\..    
5bac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bad0 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66 69           || defi
5bae0 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c 7c  ned(__x86_64) ||
5baf0 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
5bb00 34 5f 5f 29 0d 0a 23 20 64 65 66 69 6e 65 20 53  4__)..# define S
5bb10 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
5bb20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 53     0..# define S
5bb30 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
5bb40 41 4e 20 31 0d 0a 23 20 64 65 66 69 6e 65 20 53  AN 1..# define S
5bb50 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
5bb60 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E  SQLITE_UTF16L
5bb70 45 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  E..#else..# defi
5bb80 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
5bb90 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a  IAN    (*(char *
5bba0 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
5bbb0 30 29 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  0)..# define SQL
5bbc0 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e  ITE_LITTLEENDIAN
5bbd0 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c   (*(char *)(&sql
5bbe0 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0d 0a 23 20  ite3one)==1)..# 
5bbf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54  define SQLITE_UT
5bc00 46 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54  F16NATIVE (SQLIT
5bc10 45 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49  E_BIGENDIAN?SQLI
5bc20 54 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54  TE_UTF16BE:SQLIT
5bc30 45 5f 55 54 46 31 36 4c 45 29 0d 0a 23 65 6e 64  E_UTF16LE)..#end
5bc40 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e  if..../*..** Con
5bc50 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20 6c  stants for the l
5bc60 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c 6c  argest and small
5bc70 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34 2d  est possible 64-
5bc80 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
5bc90 65 72 73 2e 0d 0a 2a 2a 20 54 68 65 73 65 20 6d  ers...** These m
5bca0 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e  acros are design
5bcb0 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65  ed to work corre
5bcc0 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d  ctly on both 32-
5bcd0 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0d 0a  bit and 64-bit..
5bce0 2a 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0d 0a 2a  ** compilers...*
5bcf0 2f 0d 0a 23 64 65 66 69 6e 65 20 4c 41 52 47 45  /..#define LARGE
5bd00 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66 66 66  ST_INT64  (0xfff
5bd10 66 66 66 66 66 7c 28 28 28 69 36 34 29 30 78 37  fffff|(((i64)0x7
5bd20 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29 0d 0a  fffffff)<<32))..
5bd30 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53 54  #define SMALLEST
5bd40 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d 31  _INT64 (((i64)-1
5bd50 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36  ) - LARGEST_INT6
5bd60 34 29 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 52 6f  4)..../* ..** Ro
5bd70 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20  und up a number 
5bd80 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67  to the next larg
5bd90 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38  er multiple of 8
5bda0 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 0d  .  This is used.
5bdb0 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62  .** to force 8-b
5bdc0 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e  yte alignment on
5bdd0 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 63   64-bit architec
5bde0 74 75 72 65 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  tures...*/..#def
5bdf0 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20  ine ROUND8(x)   
5be00 20 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0d 0a    (((x)+7)&~7)..
5be10 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 75 6e 64 20 64  ../*..** Round d
5be20 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65  own to the neare
5be30 73 74 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38  st multiple of 8
5be40 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 52 4f  ..*/..#define RO
5be50 55 4e 44 44 4f 57 4e 38 28 78 29 20 28 28 78 29  UNDDOWN8(x) ((x)
5be60 26 7e 37 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  &~7)..../*..** A
5be70 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 70  ssert that the p
5be80 6f 69 6e 74 65 72 20 58 20 69 73 20 61 6c 69 67  ointer X is alig
5be90 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65  ned to an 8-byte
5bea0 20 62 6f 75 6e 64 61 72 79 2e 20 20 54 68 69 73   boundary.  This
5beb0 0d 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73  ..** macro is us
5bec0 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61  ed only within a
5bed0 73 73 65 72 74 28 29 20 74 6f 20 76 65 72 69 66  ssert() to verif
5bee0 79 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20  y that the code 
5bef0 67 65 74 73 0d 0a 2a 2a 20 61 6c 6c 20 61 6c 69  gets..** all ali
5bf00 67 6e 6d 65 6e 74 20 72 65 73 74 72 69 63 74 69  gnment restricti
5bf10 6f 6e 73 20 63 6f 72 72 65 63 74 2e 0d 0a 2a 2a  ons correct...**
5bf20 0d 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20  ..** Except, if 
5bf30 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c  SQLITE_4_BYTE_AL
5bf40 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 20 69 73 20  IGNED_MALLOC is 
5bf50 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
5bf60 65 0d 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67  e..** underlying
5bf70 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d   malloc() implem
5bf80 65 6e 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 74  ention might ret
5bf90 75 72 6e 20 75 73 20 34 2d 62 79 74 65 20 61 6c  urn us 4-byte al
5bfa0 69 67 6e 65 64 0d 0a 2a 2a 20 70 6f 69 6e 74 65  igned..** pointe
5bfb0 72 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  rs.  In that cas
5bfc0 65 2c 20 6f 6e 6c 79 20 76 65 72 69 66 79 20 34  e, only verify 4
5bfd0 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 2e  -byte alignment.
5bfe0 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c  ..*/..#ifdef SQL
5bff0 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49 47 4e  ITE_4_BYTE_ALIGN
5c000 45 44 5f 4d 41 4c 4c 4f 43 0d 0a 23 20 64 65 66  ED_MALLOC..# def
5c010 69 6e 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41  ine EIGHT_BYTE_A
5c020 4c 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28  LIGNMENT(X)   ((
5c030 28 28 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63  ((char*)(X) - (c
5c040 68 61 72 2a 29 30 29 26 33 29 3d 3d 30 29 0d 0a  har*)0)&3)==0)..
5c050 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
5c060 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
5c070 4d 45 4e 54 28 58 29 20 20 20 28 28 28 28 63 68  MENT(X)   ((((ch
5c080 61 72 2a 29 28 58 29 20 2d 20 28 63 68 61 72 2a  ar*)(X) - (char*
5c090 29 30 29 26 37 29 3d 3d 30 29 0d 0a 23 65 6e 64  )0)&7)==0)..#end
5c0a0 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  if....../*..** A
5c0b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
5c0c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
5c0d0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
5c0e0 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d   store the busy-
5c0f0 68 61 6e 64 6c 65 72 0d 0a 2a 2a 20 63 61 6c 6c  handler..** call
5c100 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 6e  back for a given
5c110 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20   sqlite handle. 
5c120 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c  ..**..** The sql
5c130 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20  ite.busyHandler 
5c140 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71  member of the sq
5c150 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74  lite struct cont
5c160 61 69 6e 73 20 74 68 65 20 62 75 73 79 0d 0a 2a  ains the busy..*
5c170 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74  * callback for t
5c180 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
5c190 6c 65 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f  le. Each pager o
5c1a0 70 65 6e 65 64 20 76 69 61 20 74 68 65 20 73 71  pened via the sq
5c1b0 6c 69 74 65 0d 0a 2a 2a 20 68 61 6e 64 6c 65 20  lite..** handle 
5c1c0 69 73 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e  is passed a poin
5c1d0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75  ter to sqlite.bu
5c1e0 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62  syHandler. The b
5c1f0 75 73 79 2d 68 61 6e 64 6c 65 72 0d 0a 2a 2a 20  usy-handler..** 
5c200 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 72  callback is curr
5c210 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e  ently invoked on
5c220 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70  ly from within p
5c230 61 67 65 72 2e 63 2e 0d 0a 2a 2f 0d 0a 74 79 70  ager.c...*/..typ
5c240 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79  edef struct Busy
5c250 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64  Handler BusyHand
5c260 6c 65 72 3b 0d 0a 73 74 72 75 63 74 20 42 75 73  ler;..struct Bus
5c270 79 48 61 6e 64 6c 65 72 20 7b 0d 0a 20 20 69 6e  yHandler {..  in
5c280 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20  t (*xFunc)(void 
5c290 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20  *,int);  /* The 
5c2a0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  busy callback */
5c2b0 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 20  ..  void *pArg; 
5c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5c2d0 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 62  * First arg to b
5c2e0 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d  usy callback */.
5c2f0 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 20  .  int nBusy;   
5c300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5c310 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   Incremented wit
5c320 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c 6c  h each busy call
5c330 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
5c340 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61  * Name of the ma
5c350 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61  ster database ta
5c360 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72  ble.  The master
5c370 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0d   database table.
5c380 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 6c  .** is a special
5c390 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c 64   table that hold
5c3a0 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20  s the names and 
5c3b0 61 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 6c  attributes of al
5c3c0 6c 0d 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65  l..** user table
5c3d0 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0d 0a  s and indices...
5c3e0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 41 53 54  */..#define MAST
5c3f0 45 52 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73  ER_NAME       "s
5c400 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 0d 0a 23  qlite_master"..#
5c410 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54  define TEMP_MAST
5c420 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69 74 65  ER_NAME  "sqlite
5c430 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0d 0a 0d  _temp_master"...
5c440 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ./*..** The root
5c450 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73  -page of the mas
5c460 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
5c470 6c 65 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  le...*/..#define
5c480 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20   MASTER_ROOT    
5c490 20 20 20 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54     1..../*..** T
5c4a0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73  he name of the s
5c4b0 63 68 65 6d 61 20 74 61 62 6c 65 2e 0d 0a 2a 2f  chema table...*/
5c4c0 0d 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41  ..#define SCHEMA
5c4d0 5f 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d  _TABLE(x)  ((!OM
5c4e0 49 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d  IT_TEMPDB)&&(x==
5c4f0 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e  1)?TEMP_MASTER_N
5c500 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29  AME:MASTER_NAME)
5c510 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 63 6f 6e  ..../*..** A con
5c520 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74  venience macro t
5c530 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65 20  hat returns the 
5c540 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
5c550 74 73 20 69 6e 0d 0a 2a 2a 20 61 6e 20 61 72 72  ts in..** an arr
5c560 61 79 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  ay...*/..#define
5c570 20 41 72 72 61 79 53 69 7a 65 28 58 29 20 20 20   ArraySize(X)   
5c580 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58   ((int)(sizeof(X
5c590 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29  )/sizeof(X[0])))
5c5a0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66  ..../*..** The f
5c5b0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61  ollowing value a
5c5c0 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d  s a destructor m
5c5d0 65 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69  eans to use sqli
5c5e0 74 65 33 44 62 46 72 65 65 28 29 2e 0d 0a 2a 2a  te3DbFree()...**
5c5f0 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65   This is an inte
5c600 72 6e 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74  rnal extension t
5c610 6f 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20  o SQLITE_STATIC 
5c620 61 6e 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  and SQLITE_TRANS
5c630 49 45 4e 54 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  IENT...*/..#defi
5c640 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  ne SQLITE_DYNAMI
5c650 43 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65  C   ((sqlite3_de
5c660 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 73 71  structor_type)sq
5c670 6c 69 74 65 33 44 62 46 72 65 65 29 0d 0a 0d 0a  lite3DbFree)....
5c680 2f 2a 0d 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49  /*..** When SQLI
5c690 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64  TE_OMIT_WSD is d
5c6a0 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73  efined, it means
5c6b0 20 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74   that the target
5c6c0 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0d 0a   platform does..
5c6d0 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57  ** not support W
5c6e0 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 44  ritable Static D
5c6f0 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20 61  ata (WSD) such a
5c700 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61  s global and sta
5c710 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0d 0a  tic variables...
5c720 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73  ** All variables
5c730 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20   must either be 
5c740 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20  on the stack or 
5c750 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
5c760 63 61 74 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 74  cated from..** t
5c770 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57  he heap.  When W
5c780 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  SD is unsupporte
5c790 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20  d, the variable 
5c7a0 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61  declarations sca
5c7b0 74 74 65 72 65 64 0d 0a 2a 2a 20 74 68 72 6f 75  ttered..** throu
5c7c0 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65  ghout the SQLite
5c7d0 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d   code must becom
5c7e0 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74  e constants inst
5c7f0 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45  ead.  The SQLITE
5c800 5f 57 53 44 0d 0a 2a 2a 20 6d 61 63 72 6f 20 69  _WSD..** macro i
5c810 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  s used for this 
5c820 70 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e  purpose.  And in
5c830 73 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e  stead of referen
5c840 63 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c  cing the variabl
5c850 65 0d 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20  e..** directly, 
5c860 77 65 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74  we use its const
5c870 61 6e 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20  ant as a key to 
5c880 6c 6f 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74  lookup the run-t
5c890 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 2a  ime allocated..*
5c8a0 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 68 6f  * buffer that ho
5c8b0 6c 64 73 20 72 65 61 6c 20 76 61 72 69 61 62 6c  lds real variabl
5c8c0 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74  e.  The constant
5c8d0 20 69 73 20 61 6c 73 6f 20 74 68 65 20 69 6e 69   is also the ini
5c8e0 74 69 61 6c 69 7a 65 72 0d 0a 2a 2a 20 66 6f 72  tializer..** for
5c8f0 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c   the run-time al
5c900 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e 0d  located buffer..
5c910 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74 68 65 20 75  .**..** In the u
5c920 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20  sual case where 
5c930 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64  WSD is supported
5c940 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44  , the SQLITE_WSD
5c950 20 61 6e 64 20 47 4c 4f 42 41 4c 0d 0a 2a 2a 20   and GLOBAL..** 
5c960 6d 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f  macros become no
5c970 2d 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65  -ops and have ze
5c980 72 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69  ro performance i
5c990 6d 70 61 63 74 2e 0d 0a 2a 2f 0d 0a 23 69 66 64  mpact...*/..#ifd
5c9a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
5c9b0 53 44 0d 0a 20 20 23 64 65 66 69 6e 65 20 53 51  SD..  #define SQ
5c9c0 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 0d 0a  LITE_WSD const..
5c9d0 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c    #define GLOBAL
5c9e0 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c 69  (t,v) (*(t*)sqli
5c9f0 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f  te3_wsd_find((vo
5ca00 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f 66  id*)&(v), sizeof
5ca10 28 76 29 29 29 0d 0a 20 20 23 64 65 66 69 6e 65  (v)))..  #define
5ca20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
5ca30 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75  nfig GLOBAL(stru
5ca40 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67  ct Sqlite3Config
5ca50 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29  , sqlite3Config)
5ca60 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69  ..SQLITE_API   i
5ca70 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69  nt sqlite3_wsd_i
5ca80 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a  nit(int N, int J
5ca90 29 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 20  );..SQLITE_API  
5caa0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77   void *sqlite3_w
5cab0 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c  sd_find(void *K,
5cac0 20 69 6e 74 20 4c 29 3b 0d 0a 23 65 6c 73 65 0d   int L);..#else.
5cad0 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54  .  #define SQLIT
5cae0 45 5f 57 53 44 20 0d 0a 20 20 23 64 65 66 69 6e  E_WSD ..  #defin
5caf0 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0d  e GLOBAL(t,v) v.
5cb00 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
5cb10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73  e3GlobalConfig s
5cb20 71 6c 69 74 65 33 43 6f 6e 66 69 67 0d 0a 23 65  qlite3Config..#e
5cb30 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ndif..../*..** T
5cb40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
5cb50 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ros are used to 
5cb60 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65  suppress compile
5cb70 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74  r warnings and t
5cb80 6f 0d 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c  o..** make it cl
5cb90 65 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61  ear to human rea
5cba0 64 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63  ders when a func
5cbb0 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69  tion parameter i
5cbc0 73 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0d  s deliberately .
5cbd0 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20  .** left unused 
5cbe0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
5cbf0 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54  of a function. T
5cc00 68 69 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70  his usually happ
5cc10 65 6e 73 20 77 68 65 6e 0d 0a 2a 2a 20 61 20 66  ens when..** a f
5cc20 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
5cc30 64 20 76 69 61 20 61 20 66 75 6e 63 74 69 6f 6e  d via a function
5cc40 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 65 78   pointer. For ex
5cc50 61 6d 70 6c 65 20 74 68 65 20 0d 0a 2a 2a 20 69  ample the ..** i
5cc60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
5cc70 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 61 74   an SQL aggregat
5cc80 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 6b 20  e step callback 
5cc90 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 65 0d  may not use the.
5cca0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e  .** parameter in
5ccb0 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 6d  dicating the num
5ccc0 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
5ccd0 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 61   passed to the a
5cce0 67 67 72 65 67 61 74 65 2c 0d 0a 2a 2a 20 69 66  ggregate,..** if
5ccf0 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74   it knows that t
5cd00 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20  his is enforced 
5cd10 65 6c 73 65 77 68 65 72 65 2e 0d 0a 2a 2a 0d 0a  elsewhere...**..
5cd20 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69  ** When a functi
5cd30 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  on parameter is 
5cd40 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20  not used at all 
5cd50 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
5cd60 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0d 0a  of a function,..
5cd70 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c  ** it is general
5cd80 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65  ly named "NotUse
5cd90 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 22  d" or "NotUsed2"
5cda0 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 20   to make things 
5cdb0 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0d 0a 2a  even clearer...*
5cdc0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65  * However, these
5cdd0 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f   macros may also
5cde0 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70 70   be used to supp
5cdf0 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65  ress warnings re
5ce00 6c 61 74 65 64 20 74 6f 0d 0a 2a 2a 20 70 61 72  lated to..** par
5ce10 61 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79  ameters that may
5ce20 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75   or may not be u
5ce30 73 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  sed depending on
5ce40 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74   compilation opt
5ce50 69 6f 6e 73 2e 0d 0a 2a 2a 20 46 6f 72 20 65 78  ions...** For ex
5ce60 61 6d 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61  ample those para
5ce70 6d 65 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64  meters only used
5ce80 20 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61   in assert() sta
5ce90 74 65 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73  tements. In thes
5cea0 65 0d 0a 2a 2a 20 63 61 73 65 73 20 74 68 65 20  e..** cases the 
5ceb0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e  parameters are n
5cec0 61 6d 65 64 20 61 73 20 70 65 72 20 74 68 65 20  amed as per the 
5ced0 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e  usual convention
5cee0 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  s...*/..#define 
5cef0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
5cf00 28 78 29 20 28 76 6f 69 64 29 28 78 29 0d 0a 23  (x) (void)(x)..#
5cf10 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41  define UNUSED_PA
5cf20 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e  RAMETER2(x,y) UN
5cf30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78  USED_PARAMETER(x
5cf40 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  ),UNUSED_PARAMET
5cf50 45 52 28 79 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ER(y)..../*..** 
5cf60 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
5cf70 65 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73  es to structures
5cf80 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74  ..*/..typedef st
5cf90 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 67  ruct AggInfo Agg
5cfa0 49 6e 66 6f 3b 0d 0a 74 79 70 65 64 65 66 20 73  Info;..typedef s
5cfb0 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78  truct AuthContex
5cfc0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0d 0a  t AuthContext;..
5cfd0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41  typedef struct A
5cfe0 75 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69  utoincInfo Autoi
5cff0 6e 63 49 6e 66 6f 3b 0d 0a 74 79 70 65 64 65 66  ncInfo;..typedef
5d000 20 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42   struct Bitvec B
5d010 69 74 76 65 63 3b 0d 0a 74 79 70 65 64 65 66 20  itvec;..typedef 
5d020 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 43  struct CollSeq C
5d030 6f 6c 6c 53 65 71 3b 0d 0a 74 79 70 65 64 65 66  ollSeq;..typedef
5d040 20 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43   struct Column C
5d050 6f 6c 75 6d 6e 3b 0d 0a 74 79 70 65 64 65 66 20  olumn;..typedef 
5d060 73 74 72 75 63 74 20 44 62 20 44 62 3b 0d 0a 74  struct Db Db;..t
5d070 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 63  ypedef struct Sc
5d080 68 65 6d 61 20 53 63 68 65 6d 61 3b 0d 0a 74 79  hema Schema;..ty
5d090 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70  pedef struct Exp
5d0a0 72 20 45 78 70 72 3b 0d 0a 74 79 70 65 64 65 66  r Expr;..typedef
5d0b0 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
5d0c0 20 45 78 70 72 4c 69 73 74 3b 0d 0a 74 79 70 65   ExprList;..type
5d0d0 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72 53  def struct ExprS
5d0e0 70 61 6e 20 45 78 70 72 53 70 61 6e 3b 0d 0a 74  pan ExprSpan;..t
5d0f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 4b  ypedef struct FK
5d100 65 79 20 46 4b 65 79 3b 0d 0a 74 79 70 65 64 65  ey FKey;..typede
5d110 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 73  f struct FuncDes
5d120 74 72 75 63 74 6f 72 20 46 75 6e 63 44 65 73 74  tructor FuncDest
5d130 72 75 63 74 6f 72 3b 0d 0a 74 79 70 65 64 65 66  ructor;..typedef
5d140 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20   struct FuncDef 
5d150 46 75 6e 63 44 65 66 3b 0d 0a 74 79 70 65 64 65  FuncDef;..typede
5d160 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66  f struct FuncDef
5d170 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 68  Hash FuncDefHash
5d180 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
5d190 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b  t IdList IdList;
5d1a0 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
5d1b0 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0d 0a 74   Index Index;..t
5d1c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
5d1d0 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53  dexSample IndexS
5d1e0 61 6d 70 6c 65 3b 0d 0a 74 79 70 65 64 65 66 20  ample;..typedef 
5d1f0 73 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20  struct KeyClass 
5d200 4b 65 79 43 6c 61 73 73 3b 0d 0a 74 79 70 65 64  KeyClass;..typed
5d210 65 66 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  ef struct KeyInf
5d220 6f 20 4b 65 79 49 6e 66 6f 3b 0d 0a 74 79 70 65  o KeyInfo;..type
5d230 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61  def struct Looka
5d240 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0d  side Lookaside;.
5d250 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5d260 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c 6f  LookasideSlot Lo
5d270 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0d 0a 74 79  okasideSlot;..ty
5d280 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64  pedef struct Mod
5d290 75 6c 65 20 4d 6f 64 75 6c 65 3b 0d 0a 74 79 70  ule Module;..typ
5d2a0 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65  edef struct Name
5d2b0 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74  Context NameCont
5d2c0 65 78 74 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  ext;..typedef st
5d2d0 72 75 63 74 20 50 61 72 73 65 20 50 61 72 73 65  ruct Parse Parse
5d2e0 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
5d2f0 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65 74 3b  t RowSet RowSet;
5d300 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
5d310 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 65 70   Savepoint Savep
5d320 6f 69 6e 74 3b 0d 0a 74 79 70 65 64 65 66 20 73  oint;..typedef s
5d330 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c  truct Select Sel
5d340 65 63 74 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  ect;..typedef st
5d350 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63  ruct SrcList Src
5d360 4c 69 73 74 3b 0d 0a 74 79 70 65 64 65 66 20 73  List;..typedef s
5d370 74 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53  truct StrAccum S
5d380 74 72 41 63 63 75 6d 3b 0d 0a 74 79 70 65 64 65  trAccum;..typede
5d390 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 20 54  f struct Table T
5d3a0 61 62 6c 65 3b 0d 0a 74 79 70 65 64 65 66 20 73  able;..typedef s
5d3b0 74 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20  truct TableLock 
5d3c0 54 61 62 6c 65 4c 6f 63 6b 3b 0d 0a 74 79 70 65  TableLock;..type
5d3d0 64 65 66 20 73 74 72 75 63 74 20 54 6f 6b 65 6e  def struct Token
5d3e0 20 54 6f 6b 65 6e 3b 0d 0a 74 79 70 65 64 65 66   Token;..typedef
5d3f0 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
5d400 54 72 69 67 67 65 72 3b 0d 0a 74 79 70 65 64 65  Trigger;..typede
5d410 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
5d420 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b 0d  Prg TriggerPrg;.
5d430 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5d440 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67  TriggerStep Trig
5d450 67 65 72 53 74 65 70 3b 0d 0a 74 79 70 65 64 65  gerStep;..typede
5d460 66 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65  f struct Unpacke
5d470 64 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64  dRecord Unpacked
5d480 52 65 63 6f 72 64 3b 0d 0a 74 79 70 65 64 65 66  Record;..typedef
5d490 20 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 56   struct VTable V
5d4a0 54 61 62 6c 65 3b 0d 0a 74 79 70 65 64 65 66 20  Table;..typedef 
5d4b0 73 74 72 75 63 74 20 56 74 61 62 43 74 78 20 56  struct VtabCtx V
5d4c0 74 61 62 43 74 78 3b 0d 0a 74 79 70 65 64 65 66  tabCtx;..typedef
5d4d0 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57   struct Walker W
5d4e0 61 6c 6b 65 72 3b 0d 0a 74 79 70 65 64 65 66 20  alker;..typedef 
5d4f0 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e  struct WherePlan
5d500 20 57 68 65 72 65 50 6c 61 6e 3b 0d 0a 74 79 70   WherePlan;..typ
5d510 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
5d520 65 49 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b  eInfo WhereInfo;
5d530 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
5d540 20 57 68 65 72 65 4c 65 76 65 6c 20 57 68 65 72   WhereLevel Wher
5d550 65 4c 65 76 65 6c 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  eLevel;..../*..*
5d560 2a 20 44 65 66 65 72 20 73 6f 75 72 63 69 6e 67  * Defer sourcing
5d570 20 76 64 62 65 2e 68 20 61 6e 64 20 62 74 72 65   vdbe.h and btre
5d580 65 2e 68 20 75 6e 74 69 6c 20 61 66 74 65 72 20  e.h until after 
5d590 74 68 65 20 22 75 38 22 20 61 6e 64 20 0d 0a 2a  the "u8" and ..*
5d5a0 2a 20 22 42 75 73 79 48 61 6e 64 6c 65 72 22 20  * "BusyHandler" 
5d5b0 74 79 70 65 64 65 66 73 2e 20 76 64 62 65 2e 68  typedefs. vdbe.h
5d5c0 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 20 61   also requires a
5d5d0 20 66 65 77 20 6f 66 20 74 68 65 20 6f 70 61 71   few of the opaq
5d5e0 75 65 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  ue..** pointer t
5d5f0 79 70 65 73 20 28 69 2e 65 2e 20 46 75 6e 63 44  ypes (i.e. FuncD
5d600 65 66 29 20 64 65 66 69 6e 65 64 20 61 62 6f 76  ef) defined abov
5d610 65 2e 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  e...*/../*******
5d620 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
5d630 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d  btree.h in the m
5d640 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
5d650 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
5d660 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
5d670 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
5d680 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a  ile btree.h ****
5d690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d6b0 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  *******/../*..**
5d6c0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
5d6d0 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  15..**..** The a
5d6e0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
5d6f0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
5d700 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
5d710 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
5d720 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
5d730 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
5d740 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
5d750 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
5d760 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
5d770 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
5d780 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
5d790 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
5d7a0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
5d7b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
5d7c0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
5d7d0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
5d7e0 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
5d7f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
5d800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54  **********..** T
5d840 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
5d850 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
5d860 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73  rface that the s
5d870 71 6c 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c  qlite B-Tree fil
5d880 65 0d 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e  e..** subsystem.
5d890 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69    See comments i
5d8a0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64  n the source cod
5d8b0 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c 65 64  e for a detailed
5d8c0 20 64 65 73 63 72 69 70 74 69 6f 6e 0d 0a 2a 2a   description..**
5d8d0 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69 6e   of what each in
5d8e0 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 20  terface routine 
5d8f0 64 6f 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  does...*/..#ifnd
5d900 65 66 20 5f 42 54 52 45 45 5f 48 5f 0d 0a 23 64  ef _BTREE_H_..#d
5d910 65 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0d  efine _BTREE_H_.
5d920 0a 0d 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73  .../* TODO: This
5d930 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a   definition is j
5d940 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73 6f 20  ust included so 
5d950 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f  other modules co
5d960 6d 70 69 6c 65 2e 20 49 74 0d 0a 2a 2a 20 6e 65  mpile. It..** ne
5d970 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73 69  eds to be revisi
5d980 74 65 64 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  ted...*/..#defin
5d990 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  e SQLITE_N_BTREE
5d9a0 5f 4d 45 54 41 20 31 30 0d 0a 0d 0a 2f 2a 0d 0a  _META 10..../*..
5d9b0 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73  ** If defined as
5d9c0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d   non-zero, auto-
5d9d0 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
5d9e0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74  d by default. Ot
5d9f0 68 65 72 77 69 73 65 0d 0a 2a 2a 20 69 74 20 6d  herwise..** it m
5da00 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e  ust be turned on
5da10 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
5da20 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41  se using "PRAGMA
5da30 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31   auto_vacuum = 1
5da40 22 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20  "...*/..#ifndef 
5da50 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
5da60 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 23 64 65  UTOVACUUM..  #de
5da70 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
5da80 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30  ULT_AUTOVACUUM 0
5da90 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66  ..#endif....#def
5daa0 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41  ine BTREE_AUTOVA
5dab0 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20  CUUM_NONE 0     
5dac0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20     /* Do not do 
5dad0 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0d 0a  auto-vacuum */..
5dae0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55  #define BTREE_AU
5daf0 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20  TOVACUUM_FULL 1 
5db00 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c         /* Do ful
5db10 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f  l auto-vacuum */
5db20 0d 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  ..#define BTREE_
5db30 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20  AUTOVACUUM_INCR 
5db40 32 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72  2        /* Incr
5db50 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a  emental vacuum *
5db60 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 6f 72 77  /..../*..** Forw
5db70 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
5db80 20 6f 66 20 73 74 72 75 63 74 75 72 65 0d 0a 2a   of structure..*
5db90 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
5dba0 74 20 42 74 72 65 65 20 42 74 72 65 65 3b 0d 0a  t Btree Btree;..
5dbb0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
5dbc0 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72  tCursor BtCursor
5dbd0 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
5dbe0 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61  t BtShared BtSha
5dbf0 72 65 64 3b 0d 0a 0d 0a 0d 0a 53 51 4c 49 54 45  red;......SQLITE
5dc00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5dc10 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0d 0a  ite3BtreeOpen(..
5dc20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
5dc30 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 56 46  Vfs,       /* VF
5dc40 53 20 74 6f 20 75 73 65 20 77 69 74 68 20 74 68  S to use with th
5dc50 69 73 20 62 2d 74 72 65 65 20 2a 2f 0d 0a 20 20  is b-tree */..  
5dc60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
5dc70 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  ename,   /* Name
5dc80 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
5dc90 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20  e to open */..  
5dca0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
5dcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f           /* Asso
5dcc0 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20  ciated database 
5dcd0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20  connection */.. 
5dce0 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65   Btree **ppBtree
5dcf0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74  ,         /* Ret
5dd00 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20  urn open Btree* 
5dd10 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 66  here */..  int f
5dd20 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
5dd30 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0d      /* Flags */.
5dd40 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  .  int vfsFlags 
5dd50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
5dd60 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
5dd70 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20  ugh to VFS open 
5dd80 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a 20 54 68 65  */..);..../* The
5dd90 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
5dda0 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
5ddb0 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20  Open can be the 
5ddc0 62 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68  bitwise or of th
5ddd0 65 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  e..** following 
5dde0 76 61 6c 75 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  values...**..** 
5ddf0 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c  NOTE:  These val
5de00 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74  ues must match t
5de10 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
5de20 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69   PAGER_ values i
5de30 6e 0d 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0d 0a  n..** pager.h...
5de40 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 54 52 45  */..#define BTRE
5de50 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20  E_OMIT_JOURNAL  
5de60 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65  1  /* Do not cre
5de70 61 74 65 20 6f 72 20 75 73 65 20 61 20 72 6f 6c  ate or use a rol
5de80 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f  lback journal */
5de90 0d 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  ..#define BTREE_
5dea0 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20  NO_READLOCK   2 
5deb0 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63   /* Omit readloc
5dec0 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66  ks on readonly f
5ded0 69 6c 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  iles */..#define
5dee0 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20   BTREE_MEMORY   
5def0 20 20 20 20 20 34 20 20 2f 2a 20 54 68 69 73 20       4  /* This 
5df00 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
5df10 44 42 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42  DB */..#define B
5df20 54 52 45 45 5f 53 49 4e 47 4c 45 20 20 20 20 20  TREE_SINGLE     
5df30 20 20 20 38 20 20 2f 2a 20 54 68 65 20 66 69 6c     8  /* The fil
5df40 65 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6d 6f  e contains at mo
5df50 73 74 20 31 20 62 2d 74 72 65 65 20 2a 2f 0d 0a  st 1 b-tree */..
5df60 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 55 4e  #define BTREE_UN
5df70 4f 52 44 45 52 45 44 20 20 20 20 31 36 20 20 2f  ORDERED    16  /
5df80 2a 20 55 73 65 20 6f 66 20 61 20 68 61 73 68 20  * Use of a hash 
5df90 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
5dfa0 73 20 4f 4b 20 2a 2f 0d 0a 0d 0a 53 51 4c 49 54  s OK */....SQLIT
5dfb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5dfc0 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
5dfd0 42 74 72 65 65 2a 29 3b 0d 0a 53 51 4c 49 54 45  Btree*);..SQLITE
5dfe0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5dff0 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
5e000 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74  eSize(Btree*,int
5e010 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
5e020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
5e030 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65  reeSetSafetyLeve
5e040 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74  l(Btree*,int,int
5e050 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ,int);..SQLITE_P
5e060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5e070 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62  e3BtreeSyncDisab
5e080 6c 65 64 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51  led(Btree*);..SQ
5e090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5e0a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
5e0b0 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a  PageSize(Btree *
5e0c0 70 2c 20 69 6e 74 20 6e 50 61 67 65 73 69 7a 65  p, int nPagesize
5e0d0 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20  , int nReserve, 
5e0e0 69 6e 74 20 65 46 69 78 29 3b 0d 0a 53 51 4c 49  int eFix);..SQLI
5e0f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5e100 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
5e110 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0d  geSize(Btree*);.
5e120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5e130 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5e140 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72  MaxPageCount(Btr
5e150 65 65 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54  ee*,int);..SQLIT
5e160 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
5e170 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 50 61  lite3BtreeLastPa
5e180 67 65 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51 4c  ge(Btree*);..SQL
5e190 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5e1a0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 63 75  sqlite3BtreeSecu
5e1b0 72 65 44 65 6c 65 74 65 28 42 74 72 65 65 2a 2c  reDelete(Btree*,
5e1c0 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
5e1d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5e1e0 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65  3BtreeGetReserve
5e1f0 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51 4c 49 54  (Btree*);..SQLIT
5e200 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5e210 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
5e220 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c  oVacuum(Btree *,
5e230 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
5e240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5e250 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
5e260 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0d 0a  cuum(Btree *);..
5e270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5e280 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42  nt sqlite3BtreeB
5e290 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a  eginTrans(Btree*
5e2a0 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ,int);..SQLITE_P
5e2b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5e2c0 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
5e2d0 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f  seOne(Btree*, co
5e2e0 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
5e2f0 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  r);..SQLITE_PRIV
5e300 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5e310 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
5e320 77 6f 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b  wo(Btree*, int);
5e330 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5e340 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5e350 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29 3b  eCommit(Btree*);
5e360 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5e370 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5e380 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a  eRollback(Btree*
5e390 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
5e3a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
5e3b0 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72  reeBeginStmt(Btr
5e3c0 65 65 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54  ee*,int);..SQLIT
5e3d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5e3e0 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
5e3f0 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e  Table(Btree*, in
5e400 74 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0d  t*, int flags);.
5e410 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5e420 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5e430 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a  IsInTrans(Btree*
5e440 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
5e450 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
5e460 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
5e470 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51 4c 49 54  (Btree*);..SQLIT
5e480 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5e490 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61  lite3BtreeIsInBa
5e4a0 63 6b 75 70 28 42 74 72 65 65 2a 29 3b 0d 0a 53  ckup(Btree*);..S
5e4b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5e4c0 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
5e4d0 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c 20  Schema(Btree *, 
5e4e0 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
5e4f0 64 20 2a 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  d *));..SQLITE_P
5e500 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5e510 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
5e520 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 65  ked(Btree *pBtre
5e530 65 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  e);..SQLITE_PRIV
5e540 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5e550 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74  treeLockTable(Bt
5e560 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74  ree *pBtree, int
5e570 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74   iTab, u8 isWrit
5e580 65 4c 6f 63 6b 29 3b 0d 0a 53 51 4c 49 54 45 5f  eLock);..SQLITE_
5e590 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5e5a0 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
5e5b0 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20  t(Btree *, int, 
5e5c0 69 6e 74 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f  int);....SQLITE_
5e5d0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
5e5e0 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
5e5f0 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65  GetFilename(Btre
5e600 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
5e610 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
5e620 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65   *sqlite3BtreeGe
5e630 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72  tJournalname(Btr
5e640 65 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ee *);..SQLITE_P
5e650 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5e660 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
5e670 42 74 72 65 65 20 2a 2c 20 42 74 72 65 65 20 2a  Btree *, Btree *
5e680 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  );....SQLITE_PRI
5e690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5e6a0 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28  BtreeIncrVacuum(
5e6b0 42 74 72 65 65 20 2a 29 3b 0d 0a 0d 0a 2f 2a 20  Btree *);..../* 
5e6c0 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  The flags parame
5e6d0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74  ter to sqlite3Bt
5e6e0 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63  reeCreateTable c
5e6f0 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73  an be the bitwis
5e700 65 20 4f 52 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  e OR..** of the 
5e710 66 6c 61 67 73 20 73 68 6f 77 6e 20 62 65 6c 6f  flags shown belo
5e720 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 76 65 72 79  w...**..** Every
5e730 20 53 51 4c 69 74 65 20 74 61 62 6c 65 20 6d 75   SQLite table mu
5e740 73 74 20 68 61 76 65 20 65 69 74 68 65 72 20 42  st have either B
5e750 54 52 45 45 5f 49 4e 54 4b 45 59 20 6f 72 20 42  TREE_INTKEY or B
5e760 54 52 45 45 5f 42 4c 4f 42 4b 45 59 20 73 65 74  TREE_BLOBKEY set
5e770 2e 0d 0a 2a 2a 20 57 69 74 68 20 42 54 52 45 45  ...** With BTREE
5e780 5f 49 4e 54 4b 45 59 2c 20 74 68 65 20 74 61 62  _INTKEY, the tab
5e790 6c 65 20 6b 65 79 20 69 73 20 61 20 36 34 2d 62  le key is a 64-b
5e7a0 69 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 61  it integer and a
5e7b0 72 62 69 74 72 61 72 79 20 64 61 74 61 0d 0a 2a  rbitrary data..*
5e7c0 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  * is stored in t
5e7d0 68 65 20 6c 65 61 76 65 73 2e 20 20 28 42 54 52  he leaves.  (BTR
5e7e0 45 45 5f 49 4e 54 4b 45 59 20 69 73 20 75 73 65  EE_INTKEY is use
5e7f0 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73  d for SQL tables
5e800 2e 29 20 20 57 69 74 68 0d 0a 2a 2a 20 42 54 52  .)  With..** BTR
5e810 45 45 5f 42 4c 4f 42 4b 45 59 2c 20 74 68 65 20  EE_BLOBKEY, the 
5e820 6b 65 79 20 69 73 20 61 6e 20 61 72 62 69 74 72  key is an arbitr
5e830 61 72 79 20 42 4c 4f 42 20 61 6e 64 20 6e 6f 20  ary BLOB and no 
5e840 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65  content is store
5e850 64 0d 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 2d  d..** anywhere -
5e860 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65 20   the key is the 
5e870 63 6f 6e 74 65 6e 74 2e 20 20 28 42 54 52 45 45  content.  (BTREE
5e880 5f 42 4c 4f 42 4b 45 59 20 69 73 20 75 73 65 64  _BLOBKEY is used
5e890 20 66 6f 72 20 53 51 4c 0d 0a 2a 2a 20 69 6e 64   for SQL..** ind
5e8a0 69 63 65 73 2e 29 0d 0a 2a 2f 0d 0a 23 64 65 66  ices.)..*/..#def
5e8b0 69 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  ine BTREE_INTKEY
5e8c0 20 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62       1    /* Tab
5e8d0 6c 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62  le has only 64-b
5e8e0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
5e8f0 72 20 6b 65 79 73 20 2a 2f 0d 0a 23 64 65 66 69  r keys */..#defi
5e900 6e 65 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59  ne BTREE_BLOBKEY
5e910 20 20 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c      2    /* Tabl
5e920 65 20 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20  e has keys only 
5e930 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0d 0a 0d 0a  - no data */....
5e940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5e950 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
5e960 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 2a 2c  ropTable(Btree*,
5e970 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51   int, int*);..SQ
5e980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5e990 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
5e9a0 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20  arTable(Btree*, 
5e9b0 69 6e 74 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51 4c  int, int*);..SQL
5e9c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5e9d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69   sqlite3BtreeTri
5e9e0 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65  pAllCursors(Btre
5e9f0 65 2a 2c 20 69 6e 74 29 3b 0d 0a 0d 0a 53 51 4c  e*, int);....SQL
5ea00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5ea10 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
5ea20 4d 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72  Meta(Btree *pBtr
5ea30 65 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  ee, int idx, u32
5ea40 20 2a 70 56 61 6c 75 65 29 3b 0d 0a 53 51 4c 49   *pValue);..SQLI
5ea50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5ea60 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
5ea70 65 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e  eMeta(Btree*, in
5ea80 74 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65  t idx, u32 value
5ea90 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  );..../*..** The
5eaa0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
5eab0 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  r to sqlite3Btre
5eac0 65 47 65 74 4d 65 74 61 20 6f 72 20 73 71 6c 69  eGetMeta or sqli
5ead0 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
5eae0 74 61 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65  ta..** should be
5eaf0 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
5eb00 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 54 68  owing values. Th
5eb10 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
5eb20 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 0d 0a   are assigned ..
5eb30 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 20  ** to constants 
5eb40 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 73  so that the offs
5eb50 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  et of the corres
5eb60 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 6e  ponding field in
5eb70 20 61 6e 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 64   an..** SQLite d
5eb80 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20 6d  atabase header m
5eb90 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e  ay be found usin
5eba0 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  g the following 
5ebb0 66 6f 72 6d 75 6c 61 3a 0d 0a 2a 2a 0d 0a 2a 2a  formula:..**..**
5ebc0 20 20 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b     offset = 36 +
5ebd0 20 28 69 64 78 20 2a 20 34 29 0d 0a 2a 2a 0d 0a   (idx * 4)..**..
5ebe0 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
5ebf0 74 68 65 20 66 72 65 65 2d 70 61 67 65 2d 63 6f  the free-page-co
5ec00 75 6e 74 20 66 69 65 6c 64 20 69 73 20 6c 6f 63  unt field is loc
5ec10 61 74 65 64 20 61 74 20 62 79 74 65 20 6f 66 66  ated at byte off
5ec20 73 65 74 20 33 36 20 6f 66 0d 0a 2a 2a 20 74 68  set 36 of..** th
5ec30 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
5ec40 68 65 61 64 65 72 2e 20 54 68 65 20 69 6e 63 72  header. The incr
5ec50 2d 76 61 63 75 75 6d 2d 66 6c 61 67 20 66 69 65  -vacuum-flag fie
5ec60 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 20 61 74  ld is located at
5ec70 0d 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74  ..** byte offset
5ec80 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a 37 29 2e   64 (== 36+4*7).
5ec90 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 54  ..*/..#define BT
5eca0 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f  REE_FREE_PAGE_CO
5ecb0 55 4e 54 20 20 20 20 20 30 0d 0a 23 64 65 66 69  UNT     0..#defi
5ecc0 6e 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  ne BTREE_SCHEMA_
5ecd0 56 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0d 0a  VERSION      1..
5ece0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49  #define BTREE_FI
5ecf0 4c 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20  LE_FORMAT       
5ed00 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 42 54 52    2..#define BTR
5ed10 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  EE_DEFAULT_CACHE
5ed20 5f 53 49 5a 45 20 20 33 0d 0a 23 64 65 66 69 6e  _SIZE  3..#defin
5ed30 65 20 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f  e BTREE_LARGEST_
5ed40 52 4f 4f 54 5f 50 41 47 45 20 20 20 34 0d 0a 23  ROOT_PAGE   4..#
5ed50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 54 45 58  define BTREE_TEX
5ed60 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 20 20 20  T_ENCODING      
5ed70 20 35 0d 0a 23 64 65 66 69 6e 65 20 42 54 52 45   5..#define BTRE
5ed80 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 20 20  E_USER_VERSION  
5ed90 20 20 20 20 20 20 36 0d 0a 23 64 65 66 69 6e 65        6..#define
5eda0 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55   BTREE_INCR_VACU
5edb0 55 4d 20 20 20 20 20 20 20 20 20 37 0d 0a 0d 0a  UM         7....
5edc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5edd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
5ede0 75 72 73 6f 72 28 0d 0a 20 20 42 74 72 65 65 2a  ursor(..  Btree*
5edf0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
5ee00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5ee10 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69  * BTree containi
5ee20 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  ng table to open
5ee30 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 61 62 6c   */..  int iTabl
5ee40 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
5ee50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
5ee60 6e 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67  ndex of root pag
5ee70 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 77 72 46 6c  e */..  int wrFl
5ee80 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ag,             
5ee90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5eea0 31 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20  1 for writing.  
5eeb0 30 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20  0 for read-only 
5eec0 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 4b 65 79  */..  struct Key
5eed0 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20 20 20 20  Info*,          
5eee0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
5eef0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
5ef00 63 6f 6d 70 61 72 65 20 66 75 6e 63 74 69 6f 6e  compare function
5ef10 20 2a 2f 0d 0a 20 20 42 74 43 75 72 73 6f 72 20   */..  BtCursor 
5ef20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20 20 20  *pCursor        
5ef30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
5ef40 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 63 75  pace to write cu
5ef50 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 20 2a  rsor structure *
5ef60 2f 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  /..);..SQLITE_PR
5ef70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5ef80 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
5ef90 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f  (void);..SQLITE_
5efa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5efb0 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a  ite3BtreeCursorZ
5efc0 65 72 6f 28 42 74 43 75 72 73 6f 72 2a 29 3b 0d  ero(BtCursor*);.
5efd0 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
5efe0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
5eff0 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74  eeCloseCursor(Bt
5f000 43 75 72 73 6f 72 2a 29 3b 0d 0a 53 51 4c 49 54  Cursor*);..SQLIT
5f010 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5f020 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
5f030 55 6e 70 61 63 6b 65 64 28 0d 0a 20 20 42 74 43  Unpacked(..  BtC
5f040 75 72 73 6f 72 2a 2c 0d 0a 20 20 55 6e 70 61 63  ursor*,..  Unpac
5f050 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 4b 65  kedRecord *pUnKe
5f060 79 2c 0d 0a 20 20 69 36 34 20 69 6e 74 4b 65 79  y,..  i64 intKey
5f070 2c 0d 0a 20 20 69 6e 74 20 62 69 61 73 2c 0d 0a  ,..  int bias,..
5f080 20 20 69 6e 74 20 2a 70 52 65 73 0d 0a 29 3b 0d    int *pRes..);.
5f090 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f0a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5f0b0 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42  CursorHasMoved(B
5f0c0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b  tCursor*, int*);
5f0d0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f0e0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f0f0 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72  eDelete(BtCursor
5f100 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
5f110 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5f120 74 72 65 65 49 6e 73 65 72 74 28 42 74 43 75 72  treeInsert(BtCur
5f130 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  sor*, const void
5f140 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79   *pKey, i64 nKey
5f150 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
5f160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f170 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
5f180 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
5f190 61 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  a,..            
5f1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f1b0 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 2c        int nZero,
5f1c0 20 69 6e 74 20 62 69 61 73 2c 20 69 6e 74 20 73   int bias, int s
5f1d0 65 65 6b 52 65 73 75 6c 74 29 3b 0d 0a 53 51 4c  eekResult);..SQL
5f1e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5f1f0 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
5f200 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  t(BtCursor*, int
5f210 20 2a 70 52 65 73 29 3b 0d 0a 53 51 4c 49 54 45   *pRes);..SQLITE
5f220 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5f230 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74  ite3BtreeLast(Bt
5f240 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52  Cursor*, int *pR
5f250 65 73 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  es);..SQLITE_PRI
5f260 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5f270 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73  BtreeNext(BtCurs
5f280 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
5f290 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f2a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f2b0 65 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b  eEof(BtCursor*);
5f2c0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f2d0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f2e0 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73  ePrevious(BtCurs
5f2f0 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
5f300 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f310 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f320 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f  eKeySize(BtCurso
5f330 72 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b  r*, i64 *pSize);
5f340 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f350 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f360 65 4b 65 79 28 42 74 43 75 72 73 6f 72 2a 2c 20  eKey(BtCursor*, 
5f370 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
5f380 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0d 0a 53 51  amt, void*);..SQ
5f390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
5f3a0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
5f3b0 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74  BtreeKeyFetch(Bt
5f3c0 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41  Cursor*, int *pA
5f3d0 6d 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  mt);..SQLITE_PRI
5f3e0 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
5f3f0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74  *sqlite3BtreeDat
5f400 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a  aFetch(BtCursor*
5f410 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0d 0a 53  , int *pAmt);..S
5f420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5f430 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
5f440 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  taSize(BtCursor*
5f450 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0d 0a  , u32 *pSize);..
5f460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5f470 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
5f480 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75  ata(BtCursor*, u
5f490 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
5f4a0 6d 74 2c 20 76 6f 69 64 2a 29 3b 0d 0a 53 51 4c  mt, void*);..SQL
5f4b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5f4c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
5f4d0 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
5f4e0 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69  rsor*, sqlite3_i
5f4f0 6e 74 36 34 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  nt64);..SQLITE_P
5f500 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69  RIVATE sqlite3_i
5f510 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 65  nt64 sqlite3Btre
5f520 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28  eGetCachedRowid(
5f530 42 74 43 75 72 73 6f 72 2a 29 3b 0d 0a 0d 0a 53  BtCursor*);....S
5f540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
5f550 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
5f560 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 42  IntegrityCheck(B
5f570 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f  tree*, int *aRoo
5f580 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e  t, int nRoot, in
5f590 74 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51 4c 49 54  t, int*);..SQLIT
5f5a0 45 5f 50 52 49 56 41 54 45 20 73 74 72 75 63 74  E_PRIVATE struct
5f5b0 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42   Pager *sqlite3B
5f5c0 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65 2a  treePager(Btree*
5f5d0 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  );....SQLITE_PRI
5f5e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5f5f0 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43  BtreePutData(BtC
5f600 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
5f610 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
5f620 64 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  d*);..SQLITE_PRI
5f630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5f640 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
5f650 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b  low(BtCursor *);
5f660 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f670 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
5f680 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74  eeClearCursor(Bt
5f690 43 75 72 73 6f 72 20 2a 29 3b 0d 0a 0d 0a 53 51  Cursor *);....SQ
5f6a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5f6b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
5f6c0 56 65 72 73 69 6f 6e 28 42 74 72 65 65 20 2a 70  Version(Btree *p
5f6d0 42 74 2c 20 69 6e 74 20 69 56 65 72 73 69 6f 6e  Bt, int iVersion
5f6e0 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44  );....#ifndef ND
5f6f0 45 42 55 47 0d 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG..SQLITE_PRI
5f700 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5f710 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
5f720 69 64 28 42 74 43 75 72 73 6f 72 2a 29 3b 0d 0a  id(BtCursor*);..
5f730 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65  #endif....#ifnde
5f740 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  f SQLITE_OMIT_BT
5f750 52 45 45 43 4f 55 4e 54 0d 0a 53 51 4c 49 54 45  REECOUNT..SQLITE
5f760 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5f770 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42  ite3BtreeCount(B
5f780 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a  tCursor *, i64 *
5f790 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  );..#endif....#i
5f7a0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
5f7b0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
5f7c0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5f7d0 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43 75  eCursorInfo(BtCu
5f7e0 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74  rsor*, int*, int
5f7f0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
5f800 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
5f810 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42  treeCursorList(B
5f820 74 72 65 65 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d  tree*);..#endif.
5f830 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
5f840 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 53 51 4c 49  E_OMIT_WAL..SQLI
5f850 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
5f860 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 68 65   sqlite3BtreeChe
5f870 63 6b 70 6f 69 6e 74 28 42 74 72 65 65 2a 2c 20  ckpoint(Btree*, 
5f880 69 6e 74 2c 20 69 6e 74 20 2a 2c 20 69 6e 74 20  int, int *, int 
5f890 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  *);..#endif..../
5f8a0 2a 0d 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  *..** If we are 
5f8b0 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72 65 64  not using shared
5f8c0 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65   cache, then the
5f8d0 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
5f8e0 0d 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 73  ..** use mutexes
5f8f0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 42   to access the B
5f900 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72  tShared structur
5f910 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 65  es.  So make the
5f920 0d 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c  ..** Enter and L
5f930 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 20  eave procedures 
5f940 6e 6f 2d 6f 70 73 2e 0d 0a 2a 2f 0d 0a 23 69 66  no-ops...*/..#if
5f950 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
5f960 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 53  _SHARED_CACHE..S
5f970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
5f980 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
5f990 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0d  eEnter(Btree*);.
5f9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f9b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
5f9c0 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69  reeEnterAll(sqli
5f9d0 74 65 33 2a 29 3b 0d 0a 23 65 6c 73 65 0d 0a 23  te3*);..#else..#
5f9e0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
5f9f0 74 72 65 65 45 6e 74 65 72 28 58 29 20 0d 0a 23  treeEnter(X) ..#
5fa00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
5fa10 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0d  treeEnterAll(X).
5fa20 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20 21  .#endif....#if !
5fa30 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
5fa40 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
5fa50 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
5fa60 41 44 53 41 46 45 0d 0a 53 51 4c 49 54 45 5f 50  ADSAFE..SQLITE_P
5fa70 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
5fa80 69 74 65 33 42 74 72 65 65 53 68 61 72 61 62 6c  ite3BtreeSharabl
5fa90 65 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51 4c 49  e(Btree*);..SQLI
5faa0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
5fab0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
5fac0 61 76 65 28 42 74 72 65 65 2a 29 3b 0d 0a 53 51  ave(Btree*);..SQ
5fad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
5fae0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
5faf0 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75  EnterCursor(BtCu
5fb00 72 73 6f 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  rsor*);..SQLITE_
5fb10 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
5fb20 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
5fb30 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a  Cursor(BtCursor*
5fb40 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
5fb50 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
5fb60 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73  3BtreeLeaveAll(s
5fb70 71 6c 69 74 65 33 2a 29 3b 0d 0a 23 69 66 6e 64  qlite3*);..#ifnd
5fb80 65 66 20 4e 44 45 42 55 47 0d 0a 20 20 2f 2a 20  ef NDEBUG..  /* 
5fb90 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
5fba0 72 65 20 75 73 65 64 20 69 6e 73 69 64 65 20 61  re used inside a
5fbb0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
5fbc0 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0d 0a 53 51 4c  ts only. */..SQL
5fbd0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
5fbe0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
5fbf0 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a 29  ldsMutex(Btree*)
5fc00 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
5fc10 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42  E   int sqlite3B
5fc20 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
5fc30 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a  xes(sqlite3*);..
5fc40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
5fc50 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65   int sqlite3Sche
5fc60 6d 61 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  maMutexHeld(sqli
5fc70 74 65 33 2a 2c 69 6e 74 2c 53 63 68 65 6d 61 2a  te3*,int,Schema*
5fc80 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 65 6c 73  );..#endif..#els
5fc90 65 0d 0a 0d 0a 23 20 64 65 66 69 6e 65 20 73 71  e....# define sq
5fca0 6c 69 74 65 33 42 74 72 65 65 53 68 61 72 61 62  lite3BtreeSharab
5fcb0 6c 65 28 58 29 20 30 0d 0a 23 20 64 65 66 69 6e  le(X) 0..# defin
5fcc0 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  e sqlite3BtreeLe
5fcd0 61 76 65 28 58 29 0d 0a 23 20 64 65 66 69 6e 65  ave(X)..# define
5fce0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
5fcf0 65 72 43 75 72 73 6f 72 28 58 29 0d 0a 23 20 64  erCursor(X)..# d
5fd00 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
5fd10 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29  eeLeaveCursor(X)
5fd20 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
5fd30 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
5fd40 58 29 0d 0a 0d 0a 23 20 64 65 66 69 6e 65 20 73  X)....# define s
5fd50 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
5fd60 4d 75 74 65 78 28 58 29 20 31 0d 0a 23 20 64 65  Mutex(X) 1..# de
5fd70 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
5fd80 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
5fd90 28 58 29 20 31 0d 0a 23 20 64 65 66 69 6e 65 20  (X) 1..# define 
5fda0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74  sqlite3SchemaMut
5fdb0 65 78 48 65 6c 64 28 58 2c 59 2c 5a 29 20 31 0d  exHeld(X,Y,Z) 1.
5fdc0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 23 65 6e  .#endif......#en
5fdd0 64 69 66 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f  dif /* _BTREE_H_
5fde0 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   */..../********
5fdf0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74  ****** End of bt
5fe00 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.h **********
5fe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe30 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
5fe40 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
5fe50 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
5fe60 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
5fe70 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
5fe80 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
5fe90 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
5fea0 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 20 6d   vdbe.h in the m
5feb0 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
5fec0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
5fed0 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
5fee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
5fef0 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  file vdbe.h ****
5ff00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ff10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ff20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
5ff30 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
5ff40 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   15..**..** The 
5ff50 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
5ff60 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
5ff70 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
5ff80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
5ff90 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
5ffa0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
5ffb0 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
5ffc0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
5ffd0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
5ffe0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
5fff0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
60000 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
60010 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
60020 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
60030 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
60040 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
60050 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
60060 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
60070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20  ***********..** 
600b0 48 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  Header file for 
600c0 74 68 65 20 56 69 72 74 75 61 6c 20 44 61 74 61  the Virtual Data
600d0 42 61 73 65 20 45 6e 67 69 6e 65 20 28 56 44 42  Base Engine (VDB
600e0 45 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  E)..**..** This 
600f0 68 65 61 64 65 72 20 64 65 66 69 6e 65 73 20 74  header defines t
60100 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  he interface to 
60110 74 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61  the virtual data
60120 62 61 73 65 20 65 6e 67 69 6e 65 0d 0a 2a 2a 20  base engine..** 
60130 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 56 44  or VDBE.  The VD
60140 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  BE implements an
60150 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 69 6e   abstract machin
60160 65 20 74 68 61 74 20 72 75 6e 73 20 61 0d 0a 2a  e that runs a..*
60170 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d  * simple program
60180 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 6d   to access and m
60190 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 6c  odify the underl
601a0 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0d 0a  ying database...
601b0 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c  */..#ifndef _SQL
601c0 49 54 45 5f 56 44 42 45 5f 48 5f 0d 0a 23 64 65  ITE_VDBE_H_..#de
601d0 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42  fine _SQLITE_VDB
601e0 45 5f 48 5f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  E_H_../* #includ
601f0 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0d 0a  e <stdio.h> */..
60200 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 73 69 6e 67 6c  ../*..** A singl
60210 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61  e VDBE is an opa
60220 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  que structure na
60230 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c  med "Vdbe".  Onl
60240 79 20 72 6f 75 74 69 6e 65 73 0d 0a 2a 2a 20 69  y routines..** i
60250 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c  n the source fil
60260 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61  e sqliteVdbe.c a
60270 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65  re allowed to se
60280 65 20 74 68 65 20 69 6e 73 69 64 65 73 0d 0a 2a  e the insides..*
60290 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  * of this struct
602a0 75 72 65 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65  ure...*/..typede
602b0 66 20 73 74 72 75 63 74 20 56 64 62 65 20 56 64  f struct Vdbe Vd
602c0 62 65 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  be;..../*..** Th
602d0 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66  e names of the f
602e0 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64  ollowing types d
602f0 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49  eclared in vdbeI
60300 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 65  nt.h are require
60310 64 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64  d..** for the Vd
60320 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e  beOp definition.
60330 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74  ..*/..typedef st
60340 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64  ruct VdbeFunc Vd
60350 62 65 46 75 6e 63 3b 0d 0a 74 79 70 65 64 65 66  beFunc;..typedef
60360 20 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b   struct Mem Mem;
60370 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
60380 20 53 75 62 50 72 6f 67 72 61 6d 20 53 75 62 50   SubProgram SubP
60390 72 6f 67 72 61 6d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  rogram;..../*..*
603a0 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 74 72  * A single instr
603b0 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69  uction of the vi
603c0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
603d0 73 20 61 6e 20 6f 70 63 6f 64 65 0d 0a 2a 2a 20  s an opcode..** 
603e0 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 74  and as many as t
603f0 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 20  hree operands.  
60400 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  The instruction 
60410 69 73 20 72 65 63 6f 72 64 65 64 0d 0a 2a 2a 20  is recorded..** 
60420 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  as an instance o
60430 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
60440 73 74 72 75 63 74 75 72 65 3a 0d 0a 2a 2f 0d 0a  structure:..*/..
60450 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0d  struct VdbeOp {.
60460 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20  .  u8 opcode;   
60470 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f         /* What o
60480 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66  peration to perf
60490 6f 72 6d 20 2a 2f 0d 0a 20 20 73 69 67 6e 65 64  orm */..  signed
604a0 20 63 68 61 72 20 70 34 74 79 70 65 3b 20 2f 2a   char p4type; /*
604b0 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 34 5f 78   One of the P4_x
604c0 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72  xx constants for
604d0 20 70 34 20 2a 2f 0d 0a 20 20 75 38 20 6f 70 66   p4 */..  u8 opf
604e0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a  lags;         /*
604f0 20 4d 61 73 6b 20 6f 66 20 74 68 65 20 4f 50 46   Mask of the OPF
60500 4c 47 5f 2a 20 66 6c 61 67 73 20 69 6e 20 6f 70  LG_* flags in op
60510 63 6f 64 65 73 2e 68 20 2a 2f 0d 0a 20 20 75 38  codes.h */..  u8
60520 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20   p5;            
60530 20 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d    /* Fifth param
60540 65 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67  eter is an unsig
60550 6e 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ned character */
60560 0d 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20  ..  int p1;     
60570 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
60580 20 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 69   operand */..  i
60590 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20  nt p2;          
605a0 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
605b0 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68  ameter (often th
605c0 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69  e jump destinati
605d0 6f 6e 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 33  on) */..  int p3
605e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
605f0 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   The third param
60600 65 74 65 72 20 2a 2f 0d 0a 20 20 75 6e 69 6f 6e  eter */..  union
60610 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
60620 2a 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  * fourth paramet
60630 65 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69  er */..    int i
60640 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
60650 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c    /* Integer val
60660 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34  ue if p4type==P4
60670 5f 49 4e 54 33 32 20 2a 2f 0d 0a 20 20 20 20 76  _INT32 */..    v
60680 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20  oid *p;         
60690 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63        /* Generic
606a0 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20 20 20   pointer */..   
606b0 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
606c0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
606d0 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73  er to data for s
606e0 74 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61  tring (char arra
606f0 79 29 20 74 79 70 65 73 20 2a 2f 0d 0a 20 20 20  y) types */..   
60700 20 69 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20   i64 *pI64;     
60710 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
60720 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
60730 34 5f 49 4e 54 36 34 20 2a 2f 0d 0a 20 20 20 20  4_INT64 */..    
60740 64 6f 75 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20  double *pReal;  
60750 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
60760 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
60770 5f 52 45 41 4c 20 2a 2f 0d 0a 20 20 20 20 46 75  _REAL */..    Fu
60780 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20  ncDef *pFunc;   
60790 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
607a0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 46  n p4type is P4_F
607b0 55 4e 43 44 45 46 20 2a 2f 0d 0a 20 20 20 20 56  UNCDEF */..    V
607c0 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
607d0 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 68  nc;   /* Used wh
607e0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
607f0 56 44 42 45 46 55 4e 43 20 2a 2f 0d 0a 20 20 20  VDBEFUNC */..   
60800 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
60810 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
60820 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
60830 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0d 0a 20 20  4_COLLSEQ */..  
60840 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20    Mem *pMem;    
60850 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
60860 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
60870 50 34 5f 4d 45 4d 20 2a 2f 0d 0a 20 20 20 20 56  P4_MEM */..    V
60880 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20  Table *pVtab;   
60890 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
608a0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
608b0 56 54 41 42 20 2a 2f 0d 0a 20 20 20 20 4b 65 79  VTAB */..    Key
608c0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
608d0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
608e0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45   p4type is P4_KE
608f0 59 49 4e 46 4f 20 2a 2f 0d 0a 20 20 20 20 69 6e  YINFO */..    in
60900 74 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20  t *ai;          
60910 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
60920 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49  n p4type is P4_I
60930 4e 54 41 52 52 41 59 20 2a 2f 0d 0a 20 20 20 20  NTARRAY */..    
60940 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f  SubProgram *pPro
60950 67 72 61 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77  gram;  /* Used w
60960 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
60970 5f 53 55 42 50 52 4f 47 52 41 4d 20 2a 2f 0d 0a  _SUBPROGRAM */..
60980 20 20 20 20 69 6e 74 20 28 2a 78 41 64 76 61 6e      int (*xAdvan
60990 63 65 29 28 42 74 43 75 72 73 6f 72 20 2a 2c 20  ce)(BtCursor *, 
609a0 69 6e 74 20 2a 29 3b 0d 0a 20 20 7d 20 70 34 3b  int *);..  } p4;
609b0 0d 0a 2f 2f 23 69 66 64 65 66 20 53 51 4c 49 54  ..//#ifdef SQLIT
609c0 45 5f 44 45 42 55 47 0d 0a 20 20 63 68 61 72 20  E_DEBUG..  char 
609d0 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20  *zComment;      
609e0 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74      /* Comment t
609f0 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62  o improve readab
60a00 69 6c 69 74 79 20 2a 2f 0d 0a 2f 2f 23 65 6e 64  ility */..//#end
60a10 69 66 0d 0a 23 69 66 64 65 66 20 56 44 42 45 5f  if..#ifdef VDBE_
60a20 50 52 4f 46 49 4c 45 0d 0a 20 20 69 6e 74 20 63  PROFILE..  int c
60a30 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
60a40 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
60a50 20 74 69 6d 65 73 20 74 68 69 73 20 69 6e 73 74   times this inst
60a60 72 75 63 74 69 6f 6e 20 77 61 73 20 65 78 65 63  ruction was exec
60a70 75 74 65 64 20 2a 2f 0d 0a 20 20 75 36 34 20 63  uted */..  u64 c
60a80 79 63 6c 65 73 3b 20 20 20 20 20 20 20 20 20 20  ycles;          
60a90 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d      /* Total tim
60aa0 65 20 73 70 65 6e 74 20 65 78 65 63 75 74 69 6e  e spent executin
60ab0 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  g this instructi
60ac0 6f 6e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d  on */..#endif..}
60ad0 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
60ae0 74 20 56 64 62 65 4f 70 20 56 64 62 65 4f 70 3b  t VdbeOp VdbeOp;
60af0 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 73  ....../*..** A s
60b00 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64 20  ub-routine used 
60b10 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 74  to implement a t
60b20 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0d  rigger program..
60b30 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 53 75 62 50  .*/..struct SubP
60b40 72 6f 67 72 61 6d 20 7b 0d 0a 20 20 56 64 62 65  rogram {..  Vdbe
60b50 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20  Op *aOp;        
60b60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
60b70 61 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f  ay of opcodes fo
60b80 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
60b90 0d 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20  ..  int nOp;    
60ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60bb0 20 20 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e    /* Elements in
60bc0 20 61 4f 70 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74   aOp[] */..  int
60bd0 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   nMem;          
60be0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
60bf0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63  mber of memory c
60c00 65 6c 6c 73 20 72 65 71 75 69 72 65 64 20 2a 2f  ells required */
60c10 0d 0a 20 20 69 6e 74 20 6e 43 73 72 3b 20 20 20  ..  int nCsr;   
60c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60c30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
60c40 75 72 73 6f 72 73 20 72 65 71 75 69 72 65 64 20  ursors required 
60c50 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 6e 63 65 3b  */..  int nOnce;
60c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60c70 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
60c80 20 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 75 63   OP_Once instruc
60c90 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 76 6f 69 64  tions */..  void
60ca0 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   *token;        
60cb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20            /* id 
60cc0 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64  that may be used
60cd0 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72   to recursive tr
60ce0 69 67 67 65 72 73 20 2a 2f 0d 0a 20 20 53 75 62  iggers */..  Sub
60cf0 50 72 6f 67 72 61 6d 20 2a 70 4e 65 78 74 3b 20  Program *pNext; 
60d00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
60d10 78 74 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 61  xt sub-program a
60d20 6c 72 65 61 64 79 20 76 69 73 69 74 65 64 20 2a  lready visited *
60d30 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  /..};..../*..** 
60d40 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f  A smaller versio
60d50 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64  n of VdbeOp used
60d60 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64   for the VdbeAdd
60d70 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f  OpList() functio
60d80 6e 20 62 65 63 61 75 73 65 0d 0a 2a 2a 20 69 74  n because..** it
60d90 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73   takes up less s
60da0 70 61 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  pace...*/..struc
60db0 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0d 0a  t VdbeOpList {..
60dc0 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20    u8 opcode;    
60dd0 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70        /* What op
60de0 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f  eration to perfo
60df0 72 6d 20 2a 2f 0d 0a 20 20 73 69 67 6e 65 64 20  rm */..  signed 
60e00 63 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20  char p1;     /* 
60e10 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f  First operand */
60e20 0d 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  ..  signed char 
60e30 70 32 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  p2;     /* Secon
60e40 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74  d parameter (oft
60e50 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74  en the jump dest
60e60 69 6e 61 74 69 6f 6e 29 20 2a 2f 0d 0a 20 20 73  ination) */..  s
60e70 69 67 6e 65 64 20 63 68 61 72 20 70 33 3b 20 20  igned char p3;  
60e80 20 20 20 2f 2a 20 54 68 69 72 64 20 70 61 72 61     /* Third para
60e90 6d 65 74 65 72 20 2a 2f 0d 0a 7d 3b 0d 0a 74 79  meter */..};..ty
60ea0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
60eb0 65 4f 70 4c 69 73 74 20 56 64 62 65 4f 70 4c 69  eOpList VdbeOpLi
60ec0 73 74 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c  st;..../*..** Al
60ed0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
60ee0 56 64 62 65 4f 70 2e 70 34 74 79 70 65 0d 0a 2a  VdbeOp.p4type..*
60ef0 2f 0d 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f  /..#define P4_NO
60f00 54 55 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20  TUSED    0   /* 
60f10 54 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72  The P4 parameter
60f20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0d   is not used */.
60f30 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 59 4e 41  .#define P4_DYNA
60f40 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f  MIC  (-1)  /* Po
60f50 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e  inter to a strin
60f60 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  g obtained from 
60f70 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a  sqliteMalloc() *
60f80 2f 0d 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 54  /..#define P4_ST
60f90 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20  ATIC   (-2)  /* 
60fa0 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  Pointer to a sta
60fb0 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 23  tic string */..#
60fc0 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 45  define P4_COLLSE
60fd0 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 69  Q  (-4)  /* P4 i
60fe0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
60ff0 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75   CollSeq structu
61000 72 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50  re */..#define P
61010 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20  4_FUNCDEF  (-5) 
61020 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
61030 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66  ter to a FuncDef
61040 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a 23   structure */..#
61050 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
61060 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69  O  (-6)  /* P4 i
61070 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
61080 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
61090 72 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50  re */..#define P
610a0 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20  4_VDBEFUNC (-7) 
610b0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
610c0 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e  ter to a VdbeFun
610d0 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a  c structure */..
610e0 23 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20  #define P4_MEM  
610f0 20 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20      (-8)  /* P4 
61100 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
61110 61 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74  a Mem*    struct
61120 75 72 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ure */..#define 
61130 50 34 5f 54 52 41 4e 53 49 45 4e 54 20 20 30 20  P4_TRANSIENT  0 
61140 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
61150 6e 74 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69  nter to a transi
61160 65 6e 74 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 23  ent string */..#
61170 64 65 66 69 6e 65 20 50 34 5f 56 54 41 42 20 20  define P4_VTAB  
61180 20 20 20 28 2d 31 30 29 20 2f 2a 20 50 34 20 69     (-10) /* P4 i
61190 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
611a0 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
611b0 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a 23 64 65  tructure */..#de
611c0 66 69 6e 65 20 50 34 5f 4d 50 52 49 4e 54 46 20  fine P4_MPRINTF 
611d0 20 28 2d 31 31 29 20 2f 2a 20 50 34 20 69 73 20   (-11) /* P4 is 
611e0 61 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65  a string obtaine
611f0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
61200 70 72 69 6e 74 66 28 29 20 2a 2f 0d 0a 23 64 65  printf() */..#de
61210 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 20 20 20  fine P4_REAL    
61220 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 69 73 20   (-12) /* P4 is 
61230 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e  a 64-bit floatin
61240 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 2a 2f  g point value */
61250 0d 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54  ..#define P4_INT
61260 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a 20 50  64    (-13) /* P
61270 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 69  4 is a 64-bit si
61280 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0d  gned integer */.
61290 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 33  .#define P4_INT3
612a0 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 34  2    (-14) /* P4
612b0 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 67   is a 32-bit sig
612c0 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0d 0a  ned integer */..
612d0 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52  #define P4_INTAR
612e0 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20  RAY (-15) /* P4 
612f0 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33  is a vector of 3
61300 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a  2-bit integers *
61310 2f 0d 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 55  /..#define P4_SU
61320 42 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 29 20  BPROGRAM  (-18) 
61330 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
61340 65 72 20 74 6f 20 61 20 53 75 62 50 72 6f 67 72  er to a SubProgr
61350 61 6d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d  am structure */.
61360 0a 23 64 65 66 69 6e 65 20 50 34 5f 41 44 56 41  .#define P4_ADVA
61370 4e 43 45 20 20 28 2d 31 39 29 20 2f 2a 20 50 34  NCE  (-19) /* P4
61380 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
61390 20 42 74 72 65 65 4e 65 78 74 28 29 20 6f 72 20   BtreeNext() or 
613a0 42 74 72 65 65 50 72 65 76 28 29 20 2a 2f 0d 0a  BtreePrev() */..
613b0 0d 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67  ../* When adding
613c0 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75   a P4 argument u
613d0 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c  sing P4_KEYINFO,
613e0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b   a copy of the K
613f0 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
61400 0d 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54  ..** is made.  T
61410 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65  hat copy is free
61420 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
61430 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42  is finalized.  B
61440 75 74 20 69 66 20 74 68 65 0d 0a 2a 2a 20 61 72  ut if the..** ar
61450 67 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59  gument is P4_KEY
61460 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68  INFO_HANDOFF, th
61470 65 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e  e passed in poin
61480 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74  ter is used.  It
61490 20 73 74 69 6c 6c 0d 0a 2a 2a 20 67 65 74 73 20   still..** gets 
614a0 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
614b0 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
614c0 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f   so it still sho
614d0 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0d  uld be obtained.
614e0 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  .** from a singl
614f0 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  e sqliteMalloc()
61500 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 69  .  But no copy i
61510 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 63  s made and the c
61520 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 66 75 6e 63 74  alling..** funct
61530 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a  ion should *not*
61540 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65   try to free the
61550 20 4b 65 79 49 6e 66 6f 2e 0d 0a 2a 2f 0d 0a 23   KeyInfo...*/..#
61560 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
61570 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0d  O_HANDOFF (-16).
61580 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49  .#define P4_KEYI
61590 4e 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37  NFO_STATIC  (-17
615a0 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  )..../*..** The 
615b0 56 64 62 65 2e 61 43 6f 6c 4e 61 6d 65 20 61 72  Vdbe.aColName ar
615c0 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 35 6e 20  ray contains 5n 
615d0 4d 65 6d 20 73 74 72 75 63 74 75 72 65 73 2c 20  Mem structures, 
615e0 77 68 65 72 65 20 6e 20 69 73 20 74 68 65 20 0d  where n is the .
615f0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  .** number of co
61600 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 72 65  lumns of data re
61610 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 73 74  turned by the st
61620 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 23 64  atement...*/..#d
61630 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 41  efine COLNAME_NA
61640 4d 45 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e  ME     0..#defin
61650 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  e COLNAME_DECLTY
61660 50 45 20 31 0d 0a 23 64 65 66 69 6e 65 20 43 4f  PE 1..#define CO
61670 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32  LNAME_DATABASE 2
61680 0d 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  ..#define COLNAM
61690 45 5f 54 41 42 4c 45 20 20 20 20 33 0d 0a 23 64  E_TABLE    3..#d
616a0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f  efine COLNAME_CO
616b0 4c 55 4d 4e 20 20 20 34 0d 0a 23 69 66 64 65 66  LUMN   4..#ifdef
616c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
616d0 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0d 0a  OLUMN_METADATA..
616e0 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  # define COLNAME
616f0 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20 20  _N        5     
61700 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f   /* Number of CO
61710 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c  LNAME_xxx symbol
61720 73 20 2a 2f 0d 0a 23 65 6c 73 65 0d 0a 23 20 69  s */..#else..# i
61730 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
61740 5f 44 45 43 4c 54 59 50 45 0d 0a 23 20 20 20 64  _DECLTYPE..#   d
61750 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
61760 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53       1      /* S
61770 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61  tore only the na
61780 6d 65 20 2a 2f 0d 0a 23 20 65 6c 73 65 0d 0a 23  me */..# else..#
61790 20 20 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d     define COLNAM
617a0 45 5f 4e 20 20 20 20 20 20 32 20 20 20 20 20 20  E_N      2      
617b0 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d  /* Store the nam
617c0 65 20 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a  e and decltype *
617d0 2f 0d 0a 23 20 65 6e 64 69 66 0d 0a 23 65 6e 64  /..# endif..#end
617e0 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  if..../*..** The
617f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
61800 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c 61   converts a rela
61810 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e 20  tive address in 
61820 74 68 65 20 70 32 20 66 69 65 6c 64 0d 0a 2a 2a  the p2 field..**
61830 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72   of a VdbeOp str
61840 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65  ucture into a ne
61850 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f  gative number so
61860 20 74 68 61 74 20 0d 0a 2a 2a 20 73 71 6c 69 74   that ..** sqlit
61870 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
61880 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65  ) knows that the
61890 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61   address is rela
618a0 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0d 0a  tive.  Calling..
618b0 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61  ** the macro aga
618c0 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20  in restores the 
618d0 61 64 64 72 65 73 73 2e 0d 0a 2a 2f 0d 0a 23 64  address...*/..#d
618e0 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 20 28  efine ADDR(X)  (
618f0 2d 31 2d 28 58 29 29 0d 0a 0d 0a 2f 2a 0d 0a 2a  -1-(X))..../*..*
61900 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73  * The makefile s
61910 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20  cans the vdbe.c 
61920 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20  source file and 
61930 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63  creates the "opc
61940 6f 64 65 73 2e 68 22 0d 0a 2a 2a 20 68 65 61 64  odes.h"..** head
61950 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66  er file that def
61960 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f  ines a number fo
61970 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73  r each opcode us
61980 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0d  ed by the VDBE..
61990 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/../**********
619a0 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63  **** Include opc
619b0 6f 64 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69  odes.h in the mi
619c0 64 64 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a  ddle of vdbe.h *
619d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
619e0 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
619f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
61a00 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a   opcodes.h *****
61a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61a30 2a 2a 2a 2a 2f 0d 0a 2f 2a 20 41 75 74 6f 6d 61  ****/../* Automa
61a40 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
61a50 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20  d.  Do not edit 
61a60 2a 2f 0d 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d  */../* See the m
61a70 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72  kopcodeh.awk scr
61a80 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 20  ipt for details 
61a90 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47  */..#define OP_G
61aa0 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20 20  oto             
61ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61ac0 20 20 20 20 31 0d 0a 23 64 65 66 69 6e 65 20 4f      1..#define O
61ad0 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20 20  P_Gosub         
61ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61af0 20 20 20 20 20 20 20 32 0d 0a 23 64 65 66 69 6e         2..#defin
61b00 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20  e OP_Return     
61b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61b20 20 20 20 20 20 20 20 20 20 20 33 0d 0a 23 64 65            3..#de
61b30 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20  fine OP_Yield   
61b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61b50 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0d 0a               4..
61b60 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49  #define OP_HaltI
61b70 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  fNull           
61b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61b90 35 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61  5..#define OP_Ha
61ba0 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  lt              
61bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61bc0 20 20 20 36 0d 0a 23 64 65 66 69 6e 65 20 4f 50     6..#define OP
61bd0 5f 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20  _Integer        
61be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61bf0 20 20 20 20 20 20 37 0d 0a 23 64 65 66 69 6e 65        7..#define
61c00 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20   OP_Int64       
61c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61c20 20 20 20 20 20 20 20 20 20 38 0d 0a 23 64 65 66           8..#def
61c30 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20 20 20  ine OP_Real     
61c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61c50 20 20 20 20 20 20 20 20 20 20 31 33 30 20 20 20            130   
61c60 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c  /* same as TK_FL
61c70 4f 41 54 20 20 20 20 2a 2f 0d 0a 23 64 65 66 69  OAT    */..#defi
61c80 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20  ne OP_String8   
61c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61ca0 20 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f            94   /
61cb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52  * same as TK_STR
61cc0 49 4e 47 20 20 20 2a 2f 0d 0a 23 64 65 66 69 6e  ING   */..#defin
61cd0 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20  e OP_String     
61ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61cf0 20 20 20 20 20 20 20 20 20 20 39 0d 0a 23 64 65            9..#de
61d00 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20  fine OP_Null    
61d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61d20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 0d 0a              10..
61d30 23 64 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20  #define OP_Blob 
61d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
61d60 31 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61  1..#define OP_Va
61d70 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  riable          
61d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61d90 20 20 31 32 0d 0a 23 64 65 66 69 6e 65 20 4f 50    12..#define OP
61da0 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20  _Move           
61db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61dc0 20 20 20 20 20 31 33 0d 0a 23 64 65 66 69 6e 65       13..#define
61dd0 20 4f 50 5f 43 6f 70 79 20 20 20 20 20 20 20 20   OP_Copy        
61de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61df0 20 20 20 20 20 20 20 20 31 34 0d 0a 23 64 65 66          14..#def
61e00 69 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20  ine OP_SCopy    
61e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61e20 20 20 20 20 20 20 20 20 20 20 20 31 35 0d 0a 23             15..#
61e30 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74  define OP_Result
61e40 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20  Row             
61e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36                16
61e60 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e  ..#define OP_Con
61e70 63 61 74 20 20 20 20 20 20 20 20 20 20 20 20 20  cat             
61e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61e90 20 39 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   91   /* same as
61ea0 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0d   TK_CONCAT   */.
61eb0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20  .#define OP_Add 
61ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61ee0 38 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  86   /* same as 
61ef0 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0d 0a  TK_PLUS     */..
61f00 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74 72  #define OP_Subtr
61f10 61 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20  act             
61f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
61f30 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
61f40 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0d 0a 23  K_MINUS    */..#
61f50 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70  define OP_Multip
61f60 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ly              
61f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38                88
61f80 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
61f90 5f 53 54 41 52 20 20 20 20 20 2a 2f 0d 0a 23 64  _STAR     */..#d
61fa0 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20  efine OP_Divide 
61fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39 20               89 
61fd0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
61fe0 53 4c 41 53 48 20 20 20 20 2a 2f 0d 0a 23 64 65  SLASH    */..#de
61ff0 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65  fine OP_Remainde
62000 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
62010 20 20 20 20 20 20 20 20 20 20 20 20 39 30 20 20              90  
62020 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
62030 45 4d 20 20 20 20 20 20 2a 2f 0d 0a 23 64 65 66  EM      */..#def
62040 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20  ine OP_CollSeq  
62050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62060 20 20 20 20 20 20 20 20 20 20 20 31 37 0d 0a 23             17..#
62070 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74 69  define OP_Functi
62080 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
62090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38                18
620a0 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  ..#define OP_Bit
620b0 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  And             
620c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
620d0 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   82   /* same as
620e0 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0d   TK_BITAND   */.
620f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f  .#define OP_BitO
62100 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
62110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62120 38 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  83   /* same as 
62130 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0d 0a  TK_BITOR    */..
62140 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74  #define OP_Shift
62150 4c 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20  Left            
62160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
62170 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  4   /* same as T
62180 4b 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0d 0a 23  K_LSHIFT   */..#
62190 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52  define OP_ShiftR
621a0 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20 20  ight            
621b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35                85
621c0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
621d0 5f 52 53 48 49 46 54 20 20 20 2a 2f 0d 0a 23 64  _RSHIFT   */..#d
621e0 65 66 69 6e 65 20 4f 50 5f 41 64 64 49 6d 6d 20  efine OP_AddImm 
621f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62200 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0d               20.
62210 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 74  .#define OP_Must
62220 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 20  BeInt           
62230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62240 32 31 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52  21..#define OP_R
62250 65 61 6c 41 66 66 69 6e 69 74 79 20 20 20 20 20  ealAffinity     
62260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62270 20 20 20 32 32 0d 0a 23 64 65 66 69 6e 65 20 4f     22..#define O
62280 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20 20  P_ToText        
62290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
622a0 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 73 61       141   /* sa
622b0 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54  me as TK_TO_TEXT
622c0 20 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50    */..#define OP
622d0 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20  _ToBlob         
622e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
622f0 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d      142   /* sam
62300 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20  e as TK_TO_BLOB 
62310 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   */..#define OP_
62320 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20  ToNumeric       
62330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62340 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65     143   /* same
62350 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49   as TK_TO_NUMERI
62360 43 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f  C*/..#define OP_
62370 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20  ToInt           
62380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62390 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d 65     144   /* same
623a0 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20   as TK_TO_INT   
623b0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54  */..#define OP_T
623c0 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  oReal           
623d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
623e0 20 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 20    145   /* same 
623f0 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a  as TK_TO_REAL  *
62400 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71  /..#define OP_Eq
62410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62430 20 20 37 36 20 20 20 2f 2a 20 73 61 6d 65 20 61    76   /* same a
62440 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f  s TK_EQ       */
62450 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20  ..#define OP_Ne 
62460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62480 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   75   /* same as
62490 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0d   TK_NE       */.
624a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20  .#define OP_Lt  
624b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
624c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
624d0 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  79   /* same as 
624e0 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0d 0a  TK_LT       */..
624f0 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20  #define OP_Le   
62500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
62520 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
62530 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0d 0a 23  K_LE       */..#
62540 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20  define OP_Gt    
62550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37                77
62570 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
62580 5f 47 54 20 20 20 20 20 20 20 2a 2f 0d 0a 23 64  _GT       */..#d
62590 65 66 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 20  efine OP_Ge     
625a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
625b0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 20               80 
625c0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
625d0 47 45 20 20 20 20 20 20 20 2a 2f 0d 0a 23 64 65  GE       */..#de
625e0 66 69 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74  fine OP_Permutat
625f0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
62600 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0d 0a              23..
62610 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 61  #define OP_Compa
62620 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  re              
62630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
62640 34 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4a 75  4..#define OP_Ju
62650 6d 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mp              
62660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62670 20 20 32 35 0d 0a 23 64 65 66 69 6e 65 20 4f 50    25..#define OP
62680 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  _And            
62690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
626a0 20 20 20 20 20 36 39 20 20 20 2f 2a 20 73 61 6d       69   /* sam
626b0 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20  e as TK_AND     
626c0 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   */..#define OP_
626d0 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Or              
626e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
626f0 20 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65      68   /* same
62700 20 61 73 20 54 4b 5f 4f 52 20 20 20 20 20 20 20   as TK_OR       
62710 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  */..#define OP_N
62720 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
62730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62740 20 20 20 31 39 20 20 20 2f 2a 20 73 61 6d 65 20     19   /* same 
62750 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 2a  as TK_NOT      *
62760 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  /..#define OP_Bi
62770 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20  tNot            
62780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62790 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61    93   /* same a
627a0 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f  s TK_BITNOT   */
627b0 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 6e 63  ..#define OP_Onc
627c0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
627d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
627e0 20 32 36 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   26..#define OP_
627f0 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  If              
62800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62810 20 20 20 20 32 37 0d 0a 23 64 65 66 69 6e 65 20      27..#define 
62820 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20  OP_IfNot        
62830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62840 20 20 20 20 20 20 20 32 38 0d 0a 23 64 65 66 69         28..#defi
62850 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20  ne OP_IsNull    
62860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62870 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f            73   /
62880 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e  * same as TK_ISN
62890 55 4c 4c 20 20 20 2a 2f 0d 0a 23 64 65 66 69 6e  ULL   */..#defin
628a0 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20  e OP_NotNull    
628b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
628c0 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
628d0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e   same as TK_NOTN
628e0 55 4c 4c 20 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ULL  */..#define
628f0 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20   OP_Column      
62900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62910 20 20 20 20 20 20 20 20 32 39 0d 0a 23 64 65 66          29..#def
62920 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20  ine OP_Affinity 
62930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62940 20 20 20 20 20 20 20 20 20 20 20 33 30 0d 0a 23             30..#
62950 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65  define OP_MakeRe
62960 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 20  cord            
62970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31                31
62980 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 75  ..#define OP_Cou
62990 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
629a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
629b0 20 33 32 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   32..#define OP_
629c0 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20 20  Savepoint       
629d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
629e0 20 20 20 20 33 33 0d 0a 23 64 65 66 69 6e 65 20      33..#define 
629f0 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20  OP_AutoCommit   
62a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62a10 20 20 20 20 20 20 20 33 34 0d 0a 23 64 65 66 69         34..#defi
62a20 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  ne OP_Transactio
62a30 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
62a40 20 20 20 20 20 20 20 20 20 20 33 35 0d 0a 23 64            35..#d
62a50 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f  efine OP_ReadCoo
62a60 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kie             
62a70 20 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0d               36.
62a80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 43  .#define OP_SetC
62a90 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20  ookie           
62aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ab0 33 37 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56  37..#define OP_V
62ac0 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20  erifyCookie     
62ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ae0 20 20 20 33 38 0d 0a 23 64 65 66 69 6e 65 20 4f     38..#define O
62af0 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20  P_OpenRead      
62b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62b10 20 20 20 20 20 20 33 39 0d 0a 23 64 65 66 69 6e        39..#defin
62b20 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20  e OP_OpenWrite  
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 34 30 0d 0a 23 64 65           40..#de
62b50 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 41 75 74 6f  fine OP_OpenAuto
62b60 69 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  index           
62b70 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0d 0a              41..
62b80 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45  #define OP_OpenE
62b90 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20  phemeral        
62ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
62bb0 32 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f  2..#define OP_So
62bc0 72 74 65 72 4f 70 65 6e 20 20 20 20 20 20 20 20  rterOpen        
62bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62be0 20 20 34 33 0d 0a 23 64 65 66 69 6e 65 20 4f 50    43..#define OP
62bf0 5f 4f 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20  _OpenPseudo     
62c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c10 20 20 20 20 20 34 34 0d 0a 23 64 65 66 69 6e 65       44..#define
62c20 20 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20   OP_Close       
62c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c40 20 20 20 20 20 20 20 20 34 35 0d 0a 23 64 65 66          45..#def
62c50 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20 20  ine OP_SeekLt   
62c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c70 20 20 20 20 20 20 20 20 20 20 20 34 36 0d 0a 23             46..#
62c80 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 65  define OP_SeekLe
62c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37                47
62cb0 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65  ..#define OP_See
62cc0 6b 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kGe             
62cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ce0 20 34 38 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   48..#define OP_
62cf0 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 20 20  SeekGt          
62d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62d10 20 20 20 20 34 39 0d 0a 23 64 65 66 69 6e 65 20      49..#define 
62d20 4f 50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20  OP_Seek         
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 35 30 0d 0a 23 64 65 66 69         50..#defi
62d50 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20  ne OP_NotFound  
62d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62d70 20 20 20 20 20 20 20 20 20 20 35 31 0d 0a 23 64            51..#d
62d80 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20 20  efine OP_Found  
62d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62da0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0d               52.
62db0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 73 55 6e  .#define OP_IsUn
62dc0 69 71 75 65 20 20 20 20 20 20 20 20 20 20 20 20  ique            
62dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62de0 35 33 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  53..#define OP_N
62df0 6f 74 45 78 69 73 74 73 20 20 20 20 20 20 20 20  otExists        
62e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e10 20 20 20 35 34 0d 0a 23 64 65 66 69 6e 65 20 4f     54..#define O
62e20 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20  P_Sequence      
62e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e40 20 20 20 20 20 20 35 35 0d 0a 23 64 65 66 69 6e        55..#defin
62e50 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20  e OP_NewRowid   
62e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e70 20 20 20 20 20 20 20 20 20 35 36 0d 0a 23 64 65           56..#de
62e80 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20  fine OP_Insert  
62e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ea0 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0d 0a              57..
62eb0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72  #define OP_Inser
62ec0 74 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  tInt            
62ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
62ee0 38 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65  8..#define OP_De
62ef0 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20  lete            
62f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62f10 20 20 35 39 0d 0a 23 64 65 66 69 6e 65 20 4f 50    59..#define OP
62f20 5f 52 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20  _ResetCount     
62f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62f40 20 20 20 20 20 36 30 0d 0a 23 64 65 66 69 6e 65       60..#define
62f50 20 4f 50 5f 53 6f 72 74 65 72 43 6f 6d 70 61 72   OP_SorterCompar
62f60 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
62f70 20 20 20 20 20 20 20 20 36 31 0d 0a 23 64 65 66          61..#def
62f80 69 6e 65 20 4f 50 5f 53 6f 72 74 65 72 44 61 74  ine OP_SorterDat
62f90 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a               
62fa0 20 20 20 20 20 20 20 20 20 20 20 36 32 0d 0a 23             62..#
62fb0 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79  define OP_RowKey
62fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33                63
62fe0 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77  ..#define OP_Row
62ff0 44 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20  Data            
63000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63010 20 36 34 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f   64..#define OP_
63020 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20  Rowid           
63030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63040 20 20 20 20 36 35 0d 0a 23 64 65 66 69 6e 65 20      65..#define 
63050 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20  OP_NullRow      
63060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63070 20 20 20 20 20 20 20 36 36 0d 0a 23 64 65 66 69         66..#defi
63080 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20  ne OP_Last      
63090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
630a0 20 20 20 20 20 20 20 20 20 20 36 37 0d 0a 23 64            67..#d
630b0 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 65 72 53  efine OP_SorterS
630c0 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ort             
630d0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0d               70.
630e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74  .#define OP_Sort
630f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63110 37 31 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52  71..#define OP_R
63120 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20 20  ewind           
63130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63140 20 20 20 37 32 0d 0a 23 64 65 66 69 6e 65 20 4f     72..#define O
63150 50 5f 53 6f 72 74 65 72 4e 65 78 74 20 20 20 20  P_SorterNext    
63160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63170 20 20 20 20 20 20 38 31 0d 0a 23 64 65 66 69 6e        81..#defin
63180 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20  e OP_Prev       
63190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631a0 20 20 20 20 20 20 20 20 20 39 32 0d 0a 23 64 65           92..#de
631b0 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20  fine OP_Next    
631c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631d0 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0d 0a              95..
631e0 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 65  #define OP_Sorte
631f0 72 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20  rInsert         
63200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
63210 36 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  6..#define OP_Id
63220 78 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20  xInsert         
63230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63240 20 20 39 37 0d 0a 23 64 65 66 69 6e 65 20 4f 50    97..#define OP
63250 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 20  _IdxDelete      
63260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63270 20 20 20 20 20 39 38 0d 0a 23 64 65 66 69 6e 65       98..#define
63280 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 20   OP_IdxRowid    
63290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632a0 20 20 20 20 20 20 20 20 39 39 0d 0a 23 64 65 66          99..#def
632b0 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20 20  ine OP_IdxLT    
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 20 31 30 30 0d 0a 23            100..#
632e0 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20  define OP_IdxGE 
632f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63300 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31               101
63310 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73  ..#define OP_Des
63320 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20  troy            
63330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63340 31 30 32 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f  102..#define OP_
63350 43 6c 65 61 72 20 20 20 20 20 20 20 20 20 20 20  Clear           
63360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63370 20 20 20 31 30 33 0d 0a 23 64 65 66 69 6e 65 20     103..#define 
63380 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 20 20  OP_CreateIndex  
63390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633a0 20 20 20 20 20 20 31 30 34 0d 0a 23 64 65 66 69        104..#defi
633b0 6e 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  ne OP_CreateTabl
633c0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
633d0 20 20 20 20 20 20 20 20 20 31 30 35 0d 0a 23 64           105..#d
633e0 65 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63  efine OP_ParseSc
633f0 68 65 6d 61 20 20 20 20 20 20 20 20 20 20 20 20  hema            
63400 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0d              106.
63410 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61 64  .#define OP_Load
63420 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20 20  Analysis        
63430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
63440 30 37 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44  07..#define OP_D
63450 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20  ropTable        
63460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63470 20 20 31 30 38 0d 0a 23 64 65 66 69 6e 65 20 4f    108..#define O
63480 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20  P_DropIndex     
63490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
634a0 20 20 20 20 20 31 30 39 0d 0a 23 64 65 66 69 6e       109..#defin
634b0 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72  e OP_DropTrigger
634c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
634d0 20 20 20 20 20 20 20 20 31 31 30 0d 0a 23 64 65          110..#de
634e0 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74  fine OP_Integrit
634f0 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  yCk             
63500 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0d 0a             111..
63510 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65  #define OP_RowSe
63520 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20  tAdd            
63530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
63540 32 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  2..#define OP_Ro
63550 77 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20  wSetRead        
63560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63570 20 31 31 33 0d 0a 23 64 65 66 69 6e 65 20 4f 50   113..#define OP
63580 5f 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20  _RowSetTest     
63590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
635a0 20 20 20 20 31 31 34 0d 0a 23 64 65 66 69 6e 65      114..#define
635b0 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 20   OP_Program     
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 31 31 35 0d 0a 23 64 65 66         115..#def
635e0 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20 20  ine OP_Param    
635f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63600 20 20 20 20 20 20 20 20 20 20 31 31 36 0d 0a 23            116..#
63610 64 65 66 69 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e  define OP_FkCoun
63620 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ter             
63630 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37               117
63640 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49  ..#define OP_FkI
63650 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20  fZero           
63660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63670 31 31 38 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f  118..#define OP_
63680 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20 20 20  MemMax          
63690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
636a0 20 20 20 31 31 39 0d 0a 23 64 65 66 69 6e 65 20     119..#define 
636b0 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20 20  OP_IfPos        
636c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
636d0 20 20 20 20 20 20 31 32 30 0d 0a 23 64 65 66 69        120..#defi
636e0 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 20 20  ne OP_IfNeg     
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 31 32 31 0d 0a 23 64           121..#d
63710 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20  efine OP_IfZero 
63720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63730 20 20 20 20 20 20 20 20 20 20 20 20 31 32 32 0d              122.
63740 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 53  .#define OP_AggS
63750 74 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20  tep             
63760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
63770 32 33 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41  23..#define OP_A
63780 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20  ggFinal         
63790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
637a0 20 20 31 32 34 0d 0a 23 64 65 66 69 6e 65 20 4f    124..#define O
637b0 50 5f 43 68 65 63 6b 70 6f 69 6e 74 20 20 20 20  P_Checkpoint    
637c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
637d0 20 20 20 20 20 31 32 35 0d 0a 23 64 65 66 69 6e       125..#defin
637e0 65 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f 64 65  e OP_JournalMode
637f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63800 20 20 20 20 20 20 20 20 31 32 36 0d 0a 23 64 65          126..#de
63810 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 20  fine OP_Vacuum  
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 20 20 20 20 31 32 37 0d 0a             127..
63840 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56  #define OP_IncrV
63850 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20  acuum           
63860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
63870 38 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78  8..#define OP_Ex
63880 70 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20  pire            
63890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
638a0 20 31 32 39 0d 0a 23 64 65 66 69 6e 65 20 4f 50   129..#define OP
638b0 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20  _TableLock      
638c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
638d0 20 20 20 20 31 33 31 0d 0a 23 64 65 66 69 6e 65      131..#define
638e0 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20   OP_VBegin      
638f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63900 20 20 20 20 20 20 20 31 33 32 0d 0a 23 64 65 66         132..#def
63910 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20 20  ine OP_VCreate  
63920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63930 20 20 20 20 20 20 20 20 20 20 31 33 33 0d 0a 23            133..#
63940 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72  define OP_VDestr
63950 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oy              
63960 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
63970 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4f 70  ..#define OP_VOp
63980 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  en              
63990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
639a0 31 33 35 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f  135..#define OP_
639b0 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 20 20  VFilter         
639c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
639d0 20 20 20 31 33 36 0d 0a 23 64 65 66 69 6e 65 20     136..#define 
639e0 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20  OP_VColumn      
639f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a00 20 20 20 20 20 20 31 33 37 0d 0a 23 64 65 66 69        137..#defi
63a10 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 20  ne OP_VNext     
63a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a30 20 20 20 20 20 20 20 20 20 31 33 38 0d 0a 23 64           138..#d
63a40 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65  efine OP_VRename
63a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a60 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39 0d              139.
63a70 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64  .#define OP_VUpd
63a80 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
63a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
63aa0 34 30 0d 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50  40..#define OP_P
63ab0 61 67 65 63 6f 75 6e 74 20 20 20 20 20 20 20 20  agecount        
63ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63ad0 20 20 31 34 36 0d 0a 23 64 65 66 69 6e 65 20 4f    146..#define O
63ae0 50 5f 4d 61 78 50 67 63 6e 74 20 20 20 20 20 20  P_MaxPgcnt      
63af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b00 20 20 20 20 20 31 34 37 0d 0a 23 64 65 66 69 6e       147..#defin
63b10 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20  e OP_Trace      
63b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b30 20 20 20 20 20 20 20 20 31 34 38 0d 0a 23 64 65          148..#de
63b40 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20  fine OP_Noop    
63b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b60 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0d 0a             149..
63b70 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61  #define OP_Expla
63b80 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  in              
63b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
63ba0 30 0d 0a 0d 0a 0d 0a 2f 2a 20 50 72 6f 70 65 72  0....../* Proper
63bb0 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f 75  ties such as "ou
63bc0 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 68  t2" or "jump" th
63bd0 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65 64  at are specified
63be0 20 69 6e 0d 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73   in..** comments
63bf0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
63c00 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f  case" for each o
63c10 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62  pcode in the vdb
63c20 65 2e 63 0d 0a 2a 2a 20 61 72 65 20 65 6e 63 6f  e.c..** are enco
63c30 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74  ded into bitvect
63c40 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d  ors as follows:.
63c50 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 46  .*/..#define OPF
63c60 4c 47 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20  LG_JUMP         
63c70 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75     0x0001  /* ju
63c80 6d 70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d  mp:  P2 holds jm
63c90 70 20 74 61 72 67 65 74 20 2a 2f 0d 0a 23 64 65  p target */..#de
63ca0 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f  fine OPFLG_OUT2_
63cb0 50 52 45 52 45 4c 45 41 53 45 20 30 78 30 30 30  PRERELEASE 0x000
63cc0 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65  2  /* out2-prere
63cd0 6c 65 61 73 65 3a 20 2a 2f 0d 0a 23 64 65 66 69  lease: */..#defi
63ce0 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20 20 20  ne OPFLG_IN1    
63cf0 20 20 20 20 20 20 20 20 20 30 78 30 30 30 34 20           0x0004 
63d00 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20 69 73   /* in1:   P1 is
63d10 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0d 0a 23 64   an input */..#d
63d20 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 20  efine OPFLG_IN2 
63d30 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
63d40 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50 32  08  /* in2:   P2
63d50 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0d   is an input */.
63d60 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
63d70 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N3             0
63d80 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20  x0010  /* in3:  
63d90 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P3 is an input 
63da0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c  */..#define OPFL
63db0 47 5f 4f 55 54 32 20 20 20 20 20 20 20 20 20 20  G_OUT2          
63dc0 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74    0x0020  /* out
63dd0 32 3a 20 20 50 32 20 69 73 20 61 6e 20 6f 75 74  2:  P2 is an out
63de0 70 75 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  put */..#define 
63df0 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20  OPFLG_OUT3      
63e00 20 20 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a        0x0040  /*
63e10 20 6f 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e   out3:  P3 is an
63e20 20 6f 75 74 70 75 74 20 2a 2f 0d 0a 23 64 65 66   output */..#def
63e30 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41  ine OPFLG_INITIA
63e40 4c 49 5a 45 52 20 7b 5c 0d 0a 2f 2a 20 20 20 30  LIZER {\../*   0
63e50 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20   */ 0x00, 0x01, 
63e60 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34  0x01, 0x04, 0x04
63e70 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78  , 0x10, 0x00, 0x
63e80 30 32 2c 5c 0d 0a 2f 2a 20 20 20 38 20 2a 2f 20  02,\../*   8 */ 
63e90 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
63ea0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
63eb0 30 30 2c 20 30 78 32 34 2c 20 30 78 32 34 2c 5c  00, 0x24, 0x24,\
63ec0 0d 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 30  ../*  16 */ 0x00
63ed0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
63ee0 32 34 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20  24, 0x04, 0x05, 
63ef0 30 78 30 34 2c 20 30 78 30 30 2c 5c 0d 0a 2f 2a  0x04, 0x00,\../*
63f00 20 20 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78    24 */ 0x00, 0x
63f10 30 31 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20  01, 0x01, 0x05, 
63f20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30  0x05, 0x00, 0x00
63f30 2c 20 30 78 30 30 2c 5c 0d 0a 2f 2a 20 20 33 32  , 0x00,\../*  32
63f40 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20   */ 0x02, 0x00, 
63f50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32  0x00, 0x00, 0x02
63f60 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78  , 0x10, 0x00, 0x
63f70 30 30 2c 5c 0d 0a 2f 2a 20 20 34 30 20 2a 2f 20  00,\../*  40 */ 
63f80 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
63f90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
63fa0 30 30 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 5c  00, 0x11, 0x11,\
63fb0 0d 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 78 31 31  ../*  48 */ 0x11
63fc0 2c 20 30 78 31 31 2c 20 30 78 30 38 2c 20 30 78  , 0x11, 0x08, 0x
63fd0 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20  11, 0x11, 0x11, 
63fe0 30 78 31 31 2c 20 30 78 30 32 2c 5c 0d 0a 2f 2a  0x11, 0x02,\../*
63ff0 20 20 35 36 20 2a 2f 20 30 78 30 32 2c 20 30 78    56 */ 0x02, 0x
64000 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
64010 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
64020 2c 20 30 78 30 30 2c 5c 0d 0a 2f 2a 20 20 36 34  , 0x00,\../*  64
64030 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20   */ 0x00, 0x02, 
64040 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 34 63  0x00, 0x01, 0x4c
64050 2c 20 30 78 34 63 2c 20 30 78 30 31 2c 20 30 78  , 0x4c, 0x01, 0x
64060 30 31 2c 5c 0d 0a 2f 2a 20 20 37 32 20 2a 2f 20  01,\../*  72 */ 
64070 30 78 30 31 2c 20 30 78 30 35 2c 20 30 78 30 35  0x01, 0x05, 0x05
64080 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
64090 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c  15, 0x15, 0x15,\
640a0 0d 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 31 35  ../*  80 */ 0x15
640b0 2c 20 30 78 30 31 2c 20 30 78 34 63 2c 20 30 78  , 0x01, 0x4c, 0x
640c0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20  4c, 0x4c, 0x4c, 
640d0 30 78 34 63 2c 20 30 78 34 63 2c 5c 0d 0a 2f 2a  0x4c, 0x4c,\../*
640e0 20 20 38 38 20 2a 2f 20 30 78 34 63 2c 20 30 78    88 */ 0x4c, 0x
640f0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20  4c, 0x4c, 0x4c, 
64100 30 78 30 31 2c 20 30 78 32 34 2c 20 30 78 30 32  0x01, 0x24, 0x02
64110 2c 20 30 78 30 31 2c 5c 0d 0a 2f 2a 20 20 39 36  , 0x01,\../*  96
64120 20 2a 2f 20 30 78 30 38 2c 20 30 78 30 38 2c 20   */ 0x08, 0x08, 
64130 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31  0x00, 0x02, 0x01
64140 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
64150 30 30 2c 5c 0d 0a 2f 2a 20 31 30 34 20 2a 2f 20  00,\../* 104 */ 
64160 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30  0x02, 0x02, 0x00
64170 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64180 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c  00, 0x00, 0x00,\
64190 0d 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 63  ../* 112 */ 0x0c
641a0 2c 20 30 78 34 35 2c 20 30 78 31 35 2c 20 30 78  , 0x45, 0x15, 0x
641b0 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20  01, 0x02, 0x00, 
641c0 30 78 30 31 2c 20 30 78 30 38 2c 5c 0d 0a 2f 2a  0x01, 0x08,\../*
641d0 20 31 32 30 20 2a 2f 20 30 78 30 35 2c 20 30 78   120 */ 0x05, 0x
641e0 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 20  05, 0x05, 0x00, 
641f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32  0x00, 0x00, 0x02
64200 2c 20 30 78 30 30 2c 5c 0d 0a 2f 2a 20 31 32 38  , 0x00,\../* 128
64210 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 20   */ 0x01, 0x00, 
64220 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30  0x02, 0x00, 0x00
64230 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64240 30 30 2c 5c 0d 0a 2f 2a 20 31 33 36 20 2a 2f 20  00,\../* 136 */ 
64250 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 31  0x01, 0x00, 0x01
64260 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64270 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c  04, 0x04, 0x04,\
64280 0d 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 34  ../* 144 */ 0x04
64290 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78  , 0x04, 0x02, 0x
642a0 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  02, 0x00, 0x00, 
642b0 30 78 30 30 2c 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  0x00,}..../*****
642c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
642d0 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a   opcodes.h *****
642e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
642f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64300 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
64310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
64320 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
64330 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e  eft off in vdbe.
64340 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
64350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a  *********/..../*
64360 0d 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73 20  ..** Prototypes 
64370 66 6f 72 20 74 68 65 20 56 44 42 45 20 69 6e 74  for the VDBE int
64380 65 72 66 61 63 65 2e 20 20 53 65 65 20 63 6f 6d  erface.  See com
64390 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 6d 70  ments on the imp
643a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a 20  lementation..** 
643b0 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f  for a descriptio
643c0 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 6f  n of what each o
643d0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
643e0 20 64 6f 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   does...*/..SQLI
643f0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
64400 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61  *sqlite3VdbeCrea
64410 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 53  te(sqlite3*);..S
64420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
64430 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
64440 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0d  Op0(Vdbe*,int);.
64450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64460 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
64470 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp1(Vdbe*,int,
64480 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
64490 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
644a0 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
644b0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0d  *,int,int,int);.
644c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
644d0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
644e0 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp3(Vdbe*,int,
644f0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0d 0a 53  int,int,int);..S
64500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
64510 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
64520 4f 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op4(Vdbe*,int,in
64530 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20  t,int,int,const 
64540 63 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0d  char *zP4,int);.
64550 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64560 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
64570 64 64 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69  ddOp4Int(Vdbe*,i
64580 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  nt,int,int,int,i
64590 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
645a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
645b0 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64  VdbeAddOpList(Vd
645c0 62 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64  be*, int nOp, Vd
645d0 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a  beOpList const *
645e0 61 4f 70 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  aOp);..SQLITE_PR
645f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
64600 65 33 56 64 62 65 41 64 64 50 61 72 73 65 53 63  e3VdbeAddParseSc
64610 68 65 6d 61 4f 70 28 56 64 62 65 2a 2c 69 6e 74  hemaOp(Vdbe*,int
64620 2c 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45  ,char*);..SQLITE
64630 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
64640 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
64650 31 28 56 64 62 65 2a 2c 20 75 33 32 20 61 64 64  1(Vdbe*, u32 add
64660 72 2c 20 69 6e 74 20 50 31 29 3b 0d 0a 53 51 4c  r, int P1);..SQL
64670 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
64680 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
64690 67 65 50 32 28 56 64 62 65 2a 2c 20 75 33 32 20  geP2(Vdbe*, u32 
646a0 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0d 0a  addr, int P2);..
646b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
646c0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
646d0 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 75  hangeP3(Vdbe*, u
646e0 33 32 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29  32 addr, int P3)
646f0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
64700 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
64710 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a  beChangeP5(Vdbe*
64720 2c 20 75 38 20 50 35 29 3b 0d 0a 53 51 4c 49 54  , u8 P5);..SQLIT
64730 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
64740 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
64750 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  re(Vdbe*, int ad
64760 64 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  dr);..SQLITE_PRI
64770 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
64780 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
64790 70 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  p(Vdbe*, int add
647a0 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  r);..SQLITE_PRIV
647b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
647c0 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 64 62  VdbeChangeP4(Vdb
647d0 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f  e*, int addr, co
647e0 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69  nst char *zP4, i
647f0 6e 74 20 4e 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  nt N);..SQLITE_P
64800 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
64810 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
64820 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0d 0a 53  (Vdbe*, int);..S
64830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
64840 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
64850 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e  eGetOp(Vdbe*, in
64860 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
64870 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
64880 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62  dbeMakeLabel(Vdb
64890 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  e*);..SQLITE_PRI
648a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
648b0 33 56 64 62 65 52 75 6e 4f 6e 6c 79 4f 6e 63 65  3VdbeRunOnlyOnce
648c0 28 56 64 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45  (Vdbe*);..SQLITE
648d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
648e0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28  lite3VdbeDelete(
648f0 56 64 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  Vdbe*);..SQLITE_
64900 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
64910 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 4f 62  ite3VdbeDeleteOb
64920 6a 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 56 64  ject(sqlite3*,Vd
64930 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  be*);..SQLITE_PR
64940 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
64950 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
64960 56 64 62 65 2a 2c 50 61 72 73 65 2a 29 3b 0d 0a  Vdbe*,Parse*);..
64970 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
64980 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  nt sqlite3VdbeFi
64990 6e 61 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0d 0a  nalize(Vdbe*);..
649a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
649b0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52  oid sqlite3VdbeR
649c0 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65  esolveLabel(Vdbe
649d0 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
649e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
649f0 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
64a00 64 64 72 28 56 64 62 65 2a 29 3b 0d 0a 23 69 66  ddr(Vdbe*);..#if
64a10 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
64a20 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
64a30 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64     int sqlite3Vd
64a40 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74  beAssertMayAbort
64a50 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0d 0a  (Vdbe *, int);..
64a60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
64a70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
64a80 65 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c  eTrace(Vdbe*,FIL
64a90 45 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 53 51  E*);..#endif..SQ
64aa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
64ab0 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  d sqlite3VdbeRes
64ac0 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 62  etStepResult(Vdb
64ad0 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  e*);..SQLITE_PRI
64ae0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
64af0 33 56 64 62 65 52 65 77 69 6e 64 28 56 64 62 65  3VdbeRewind(Vdbe
64b00 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
64b10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
64b20 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 29 3b  dbeReset(Vdbe*);
64b30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
64b40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
64b50 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65  eSetNumCols(Vdbe
64b60 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  *,int);..SQLITE_
64b70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
64b80 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
64b90 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e  e(Vdbe*, int, in
64ba0 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
64bb0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
64bc0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
64bd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
64be0 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56  beCountChanges(V
64bf0 64 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  dbe*);..SQLITE_P
64c00 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a  RIVATE sqlite3 *
64c10 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 64  sqlite3VdbeDb(Vd
64c20 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  be*);..SQLITE_PR
64c30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
64c40 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62  e3VdbeSetSql(Vdb
64c50 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  e*, const char *
64c60 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b 0d  z, int n, int);.
64c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64c80 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
64c90 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a  Swap(Vdbe*,Vdbe*
64ca0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
64cb0 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
64cc0 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61  e3VdbeTakeOpArra
64cd0 79 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69  y(Vdbe*, int*, i
64ce0 6e 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  nt*);..SQLITE_PR
64cf0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61  IVATE sqlite3_va
64d00 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65  lue *sqlite3Vdbe
64d10 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c 20  GetValue(Vdbe*, 
64d20 69 6e 74 2c 20 75 38 29 3b 0d 0a 53 51 4c 49 54  int, u8);..SQLIT
64d30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
64d40 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72  qlite3VdbeSetVar
64d50 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29  mask(Vdbe*, int)
64d60 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
64d70 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 53 51  E_OMIT_TRACE..SQ
64d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 63  LITE_PRIVATE   c
64d90 68 61 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65  har *sqlite3Vdbe
64da0 45 78 70 61 6e 64 53 71 6c 28 56 64 62 65 2a 2c  ExpandSql(Vdbe*,
64db0 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a   const char*);..
64dc0 23 65 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45  #endif....SQLITE
64dd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
64de0 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
64df0 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69  npack(KeyInfo*,i
64e00 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55  nt,const void*,U
64e10 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b  npackedRecord*);
64e20 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
64e30 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
64e40 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e  RecordCompare(in
64e50 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e  t,const void*,Un
64e60 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0d  packedRecord*);.
64e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64e80 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
64e90 73 71 6c 69 74 65 33 56 64 62 65 41 6c 6c 6f 63  sqlite3VdbeAlloc
64ea0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 4b  UnpackedRecord(K
64eb0 65 79 49 6e 66 6f 20 2a 2c 20 63 68 61 72 20 2a  eyInfo *, char *
64ec0 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 2a 29 3b  , int, char **);
64ed0 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
64ee0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d  TE_OMIT_TRIGGER.
64ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64f00 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
64f10 4c 69 6e 6b 53 75 62 50 72 6f 67 72 61 6d 28 56  LinkSubProgram(V
64f20 64 62 65 20 2a 2c 20 53 75 62 50 72 6f 67 72 61  dbe *, SubProgra
64f30 6d 20 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  m *);..#endif...
64f40 0a 0d 0a 2f 2f 23 69 66 6e 64 65 66 20 4e 44 45  ...//#ifndef NDE
64f50 42 55 47 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG..SQLITE_PRIV
64f60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
64f70 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64  e3VdbeComment(Vd
64f80 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
64f90 2c 20 2e 2e 2e 29 3b 0d 0a 23 20 64 65 66 69 6e  , ...);..# defin
64fa0 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29  e VdbeComment(X)
64fb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d    sqlite3VdbeCom
64fc0 6d 65 6e 74 20 58 0d 0a 53 51 4c 49 54 45 5f 50  ment X..SQLITE_P
64fd0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
64fe0 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d  lite3VdbeNoopCom
64ff0 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73  ment(Vdbe*, cons
65000 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a  t char*, ...);..
65010 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f  # define VdbeNoo
65020 70 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c  pComment(X)  sql
65030 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ite3VdbeNoopComm
65040 65 6e 74 20 58 0d 0a 2f 2f 23 65 6c 73 65 0d 0a  ent X..//#else..
65050 2f 2f 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  //# define VdbeC
65060 6f 6d 6d 65 6e 74 28 58 29 0d 0a 2f 2f 23 20 64  omment(X)..//# d
65070 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f  efine VdbeNoopCo
65080 6d 6d 65 6e 74 28 58 29 0d 0a 2f 2f 23 65 6e 64  mment(X)..//#end
65090 69 66 0d 0a 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  if....#endif....
650a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
650b0 45 6e 64 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a  End of vdbe.h **
650c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
650d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
650e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
650f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
65100 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
65110 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
65120 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
65130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
65140 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
65150 2a 20 49 6e 63 6c 75 64 65 20 70 61 67 65 72 2e  * Include pager.
65160 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
65170 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
65180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65190 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
651a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61  ** Begin file pa
651b0 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.h **********
651c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
651d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
651e0 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20  */../*..** 2001 
651f0 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a  September 15..**
65200 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
65210 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
65220 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
65230 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
65240 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61  ce of..** a lega
65250 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
65260 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a  s a blessing:..*
65270 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  *..**    May you
65280 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
65290 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61   evil...**    Ma
652a0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
652b0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
652c0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
652d0 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d  others...**    M
652e0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
652f0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
65300 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
65310 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a  give...**..*****
65320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65360 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 68 65  ****..** This he
65370 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65  ader file define
65380 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
65390 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20  that the sqlite 
653a0 70 61 67 65 20 63 61 63 68 65 0d 0a 2a 2a 20 73  page cache..** s
653b0 75 62 73 79 73 74 65 6d 2e 20 20 54 68 65 20 70  ubsystem.  The p
653c0 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73  age cache subsys
653d0 74 65 6d 20 72 65 61 64 73 20 61 6e 64 20 77 72  tem reads and wr
653e0 69 74 65 73 20 61 20 66 69 6c 65 20 61 20 70 61  ites a file a pa
653f0 67 65 0d 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65  ge..** at a time
65400 20 61 6e 64 20 70 72 6f 76 69 64 65 73 20 61 20   and provides a 
65410 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c  journal for roll
65420 62 61 63 6b 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66  back...*/....#if
65430 6e 64 65 66 20 5f 50 41 47 45 52 5f 48 5f 0d 0a  ndef _PAGER_H_..
65440 23 64 65 66 69 6e 65 20 5f 50 41 47 45 52 5f 48  #define _PAGER_H
65450 5f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 66 61  _..../*..** Defa
65460 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  ult maximum size
65470 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20   for persistent 
65480 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41  journal files. A
65490 20 6e 65 67 61 74 69 76 65 20 0d 0a 2a 2a 20 76   negative ..** v
654a0 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69  alue means no li
654b0 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20  mit. This value 
654c0 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65  may be overridde
654d0 6e 20 75 73 69 6e 67 20 74 68 65 20 0d 0a 2a 2a  n using the ..**
654e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
654f0 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20  rnalSizeLimit() 
65500 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50  API. See also "P
65510 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69  RAGMA journal_si
65520 7a 65 5f 6c 69 6d 69 74 22 2e 0d 0a 2a 2f 0d 0a  ze_limit"...*/..
65530 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
65540 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53  EFAULT_JOURNAL_S
65550 49 5a 45 5f 4c 49 4d 49 54 0d 0a 20 20 23 64 65  IZE_LIMIT..  #de
65560 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
65570 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45  ULT_JOURNAL_SIZE
65580 5f 4c 49 4d 49 54 20 2d 31 0d 0a 23 65 6e 64 69  _LIMIT -1..#endi
65590 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  f..../*..** The 
655a0 74 79 70 65 20 75 73 65 64 20 74 6f 20 72 65 70  type used to rep
655b0 72 65 73 65 6e 74 20 61 20 70 61 67 65 20 6e 75  resent a page nu
655c0 6d 62 65 72 2e 20 20 54 68 65 20 66 69 72 73 74  mber.  The first
655d0 20 70 61 67 65 20 69 6e 20 61 20 66 69 6c 65 0d   page in a file.
655e0 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 70 61  .** is called pa
655f0 67 65 20 31 2e 20 20 30 20 69 73 20 75 73 65 64  ge 1.  0 is used
65600 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 22 6e   to represent "n
65610 6f 74 20 61 20 70 61 67 65 22 2e 0d 0a 2a 2f 0d  ot a page"...*/.
65620 0a 74 79 70 65 64 65 66 20 75 33 32 20 50 67 6e  .typedef u32 Pgn
65630 6f 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63  o;..../*..** Eac
65640 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 6d  h open file is m
65650 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 61  anaged by a sepa
65660 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f 66  rate instance of
65670 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 72   the "Pager" str
65680 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 74 79 70  ucture...*/..typ
65690 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65  edef struct Page
656a0 72 20 50 61 67 65 72 3b 0d 0a 0d 0a 2f 2a 0d 0a  r Pager;..../*..
656b0 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66  ** Handle type f
656c0 6f 72 20 70 61 67 65 73 2e 0d 0a 2a 2f 0d 0a 74  or pages...*/..t
656d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
656e0 48 64 72 20 44 62 50 61 67 65 3b 0d 0a 0d 0a 2f  Hdr DbPage;..../
656f0 2a 0d 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65  *..** Page numbe
65700 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20  r PAGER_MJ_PGNO 
65710 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 69 6e  is never used in
65720 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62   an SQLite datab
65730 61 73 65 20 28 69 74 20 69 73 0d 0a 2a 2a 20 72  ase (it is..** r
65740 65 73 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b  eserved for work
65750 69 6e 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e  ing around a win
65760 64 6f 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d  dows/posix incom
65770 70 61 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20  patibility). It 
65780 69 73 0d 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74  is..** used in t
65790 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69  he journal to si
657a0 67 6e 69 66 79 20 74 68 61 74 20 74 68 65 20 72  gnify that the r
657b0 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20  emainder of the 
657c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0d 0a 2a  journal file ..*
657d0 2a 20 69 73 20 64 65 76 6f 74 65 64 20 74 6f 20  * is devoted to 
657e0 73 74 6f 72 69 6e 67 20 61 20 6d 61 73 74 65 72  storing a master
657f0 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2d 20   journal name - 
65800 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72  there are no mor
65810 65 20 70 61 67 65 73 20 74 6f 0d 0a 2a 2a 20 72  e pages to..** r
65820 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20 63 6f  oll back. See co
65830 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e 63 74  mments for funct
65840 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 65 72 4a  ion writeMasterJ
65850 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 61 67 65  ournal() in page
65860 72 2e 63 20 0d 0a 2a 2a 20 66 6f 72 20 64 65 74  r.c ..** for det
65870 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ails...*/..#defi
65880 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
65890 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e  (x) ((Pgno)((PEN
658a0 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e  DING_BYTE/((x)->
658b0 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0d 0a  pageSize))+1))..
658c0 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ../*..** Allowed
658d0 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
658e0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
658f0 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  to sqlite3PagerO
65900 70 65 6e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e  pen()...**..** N
65910 4f 54 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65  OTE: These value
65920 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65  s must match the
65930 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42   corresponding B
65940 54 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20  TREE_ values in 
65950 62 74 72 65 65 2e 68 2e 0d 0a 2a 2f 0d 0a 23 64  btree.h...*/..#d
65960 65 66 69 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54  efine PAGER_OMIT
65970 5f 4a 4f 55 52 4e 41 4c 20 20 30 78 30 30 30 31  _JOURNAL  0x0001
65980 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73      /* Do not us
65990 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  e a rollback jou
659a0 72 6e 61 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  rnal */..#define
659b0 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f   PAGER_NO_READLO
659c0 43 4b 20 20 20 30 78 30 30 30 32 20 20 20 20 2f  CK   0x0002    /
659d0 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73  * Omit readlocks
659e0 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c   on readonly fil
659f0 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50  es */..#define P
65a00 41 47 45 52 5f 4d 45 4d 4f 52 59 20 20 20 20 20  AGER_MEMORY     
65a10 20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20     0x0004    /* 
65a20 49 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  In-memory databa
65a30 73 65 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  se */..../*..** 
65a40 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
65a50 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
65a60 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50  ment to sqlite3P
65a70 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
65a80 29 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  )...*/..#define 
65a90 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
65aa0 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 0d  E_QUERY      -1.
65ab0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c  .#define PAGER_L
65ac0 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
65ad0 4c 20 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e  L      0..#defin
65ae0 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  e PAGER_LOCKINGM
65af0 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 20  ODE_EXCLUSIVE   
65b00 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4e 75 6d 65  1..../*..** Nume
65b10 72 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 74 68  ric constants th
65b20 61 74 20 65 6e 63 6f 64 65 20 74 68 65 20 6a 6f  at encode the jo
65b30 75 72 6e 61 6c 6d 6f 64 65 2e 20 20 0d 0a 2a 2f  urnalmode.  ..*/
65b40 0d 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  ..#define PAGER_
65b50 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
65b60 59 20 20 20 20 20 28 2d 31 29 20 20 2f 2a 20 51  Y     (-1)  /* Q
65b70 75 65 72 79 20 74 68 65 20 76 61 6c 75 65 20 6f  uery the value o
65b80 66 20 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 2a 2f  f journalmode */
65b90 0d 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  ..#define PAGER_
65ba0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
65bb0 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20 43  TE      0   /* C
65bc0 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69 6e  ommit by deletin
65bd0 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  g journal file *
65be0 2f 0d 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  /..#define PAGER
65bf0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
65c00 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20  SIST     1   /* 
65c10 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e  Commit by zeroin
65c20 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  g journal header
65c30 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50 41 47   */..#define PAG
65c40 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
65c50 46 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  FF         2   /
65c60 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65  * Journal omitte
65c70 64 2e 20 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  d.  */..#define 
65c80 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
65c90 45 5f 54 52 55 4e 43 41 54 45 20 20 20 20 33 20  E_TRUNCATE    3 
65ca0 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74    /* Commit by t
65cb0 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61  runcating journa
65cc0 6c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50 41  l */..#define PA
65cd0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
65ce0 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20 20 20  MEMORY      4   
65cf0 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  /* In-memory jou
65d00 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0d 0a 23 64  rnal file */..#d
65d10 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
65d20 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 20 20 20 20  NALMODE_WAL     
65d30 20 20 20 20 35 20 20 20 2f 2a 20 55 73 65 20 77      5   /* Use w
65d40 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 67 69  rite-ahead loggi
65d50 6e 67 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ng */..../*..** 
65d60 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66  The remainder of
65d70 20 74 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   this file conta
65d80 69 6e 73 20 74 68 65 20 64 65 63 6c 61 72 61 74  ins the declarat
65d90 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63  ions of the func
65da0 74 69 6f 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 6d  tions..** that m
65db0 61 6b 65 20 75 70 20 74 68 65 20 50 61 67 65 72  ake up the Pager
65dc0 20 73 75 62 2d 73 79 73 74 65 6d 20 41 50 49 2e   sub-system API.
65dd0 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65   See source code
65de0 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 0d 0a   comments for ..
65df0 2a 2a 20 61 20 64 65 74 61 69 6c 65 64 20 64 65  ** a detailed de
65e00 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 65 61 63  scription of eac
65e10 68 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d 0a  h routine...*/..
65e20 0d 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c  ../* Open and cl
65e30 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e 6e  ose a Pager conn
65e40 65 63 74 69 6f 6e 2e 20 2a 2f 20 0d 0a 53 51 4c  ection. */ ..SQL
65e50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
65e60 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
65e70 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  (..  sqlite3_vfs
65e80 2a 2c 0d 0a 20 20 50 61 67 65 72 20 2a 2a 70 70  *,..  Pager **pp
65e90 50 61 67 65 72 2c 0d 0a 20 20 63 6f 6e 73 74 20  Pager,..  const 
65ea0 63 68 61 72 2a 2c 0d 0a 20 20 69 6e 74 2c 0d 0a  char*,..  int,..
65eb0 20 20 69 6e 74 2c 0d 0a 20 20 69 6e 74 2c 0d 0a    int,..  int,..
65ec0 20 20 76 6f 69 64 28 2a 29 28 44 62 50 61 67 65    void(*)(DbPage
65ed0 2a 29 0d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  *)..);..SQLITE_P
65ee0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
65ef0 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67  e3PagerClose(Pag
65f00 65 72 20 2a 70 50 61 67 65 72 29 3b 0d 0a 53 51  er *pPager);..SQ
65f10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
65f20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61   sqlite3PagerRea
65f30 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65  dFileheader(Page
65f40 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 67 6e 65  r*, int, unsigne
65f50 64 20 63 68 61 72 2a 29 3b 0d 0a 0d 0a 2f 2a 20  d char*);..../* 
65f60 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
65f70 6f 20 63 6f 6e 66 69 67 75 72 65 20 61 20 50 61  o configure a Pa
65f80 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a 2f 0d 0a  ger object. */..
65f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
65fa0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
65fb0 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 50  SetBusyhandler(P
65fc0 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28 76 6f  ager*, int(*)(vo
65fd0 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29 3b 0d  id *), void *);.
65fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
65ff0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
66000 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65  SetPagesize(Page
66010 72 2a 2c 20 75 33 32 2a 2c 20 69 6e 74 29 3b 0d  r*, u32*, int);.
66020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66030 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
66040 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67  MaxPageCount(Pag
66050 65 72 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49  er*, int);..SQLI
66060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
66070 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
66080 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c  achesize(Pager*,
66090 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
660a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
660b0 74 65 33 50 61 67 65 72 53 68 72 69 6e 6b 28 50  te3PagerShrink(P
660c0 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  ager*);..SQLITE_
660d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
660e0 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
660f0 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69  tyLevel(Pager*,i
66100 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0d 0a 53 51  nt,int,int);..SQ
66110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
66120 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
66130 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 2a  kingMode(Pager *
66140 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
66150 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
66160 74 65 33 50 61 67 65 72 53 65 74 4a 6f 75 72 6e  te3PagerSetJourn
66170 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20  alMode(Pager *, 
66180 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
66190 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
661a0 33 50 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c  3PagerGetJournal
661b0 4d 6f 64 65 28 50 61 67 65 72 2a 29 3b 0d 0a 53  Mode(Pager*);..S
661c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
661d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 6b  t sqlite3PagerOk
661e0 54 6f 43 68 61 6e 67 65 4a 6f 75 72 6e 61 6c 4d  ToChangeJournalM
661f0 6f 64 65 28 50 61 67 65 72 2a 29 3b 0d 0a 53 51  ode(Pager*);..SQ
66200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
66210 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
66220 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
66230 67 65 72 20 2a 2c 20 69 36 34 29 3b 0d 0a 53 51  ger *, i64);..SQ
66240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
66250 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71  ite3_backup **sq
66260 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70  lite3PagerBackup
66270 50 74 72 28 50 61 67 65 72 2a 29 3b 0d 0a 0d 0a  Ptr(Pager*);....
66280 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65  /* Functions use
66290 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20  d to obtain and 
662a0 72 65 6c 65 61 73 65 20 70 61 67 65 20 72 65 66  release page ref
662b0 65 72 65 6e 63 65 73 2e 20 2a 2f 20 0d 0a 53 51  erences. */ ..SQ
662c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
662d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
662e0 75 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67  uire(Pager *pPag
662f0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44  er, Pgno pgno, D
66300 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  bPage **ppPage, 
66310 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0d 0a 23  int clrFlag);..#
66320 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
66330 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71  gerGet(A,B,C) sq
66340 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
66350 65 28 41 2c 42 2c 43 2c 30 29 0d 0a 53 51 4c 49  e(A,B,C,0)..SQLI
66360 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67  TE_PRIVATE DbPag
66370 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  e *sqlite3PagerL
66380 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61  ookup(Pager *pPa
66390 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b  ger, Pgno pgno);
663a0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
663b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
663c0 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0d  erRef(DbPage*);.
663d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
663e0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
663f0 72 55 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b  rUnref(DbPage*);
66400 0d 0a 0d 0a 2f 2a 20 4f 70 65 72 61 74 69 6f 6e  ..../* Operation
66410 73 20 6f 6e 20 70 61 67 65 20 72 65 66 65 72 65  s on page refere
66420 6e 63 65 73 2e 20 2a 2f 0d 0a 53 51 4c 49 54 45  nces. */..SQLITE
66430 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
66440 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44  ite3PagerWrite(D
66450 62 50 61 67 65 2a 29 3b 0d 0a 53 51 4c 49 54 45  bPage*);..SQLITE
66460 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
66470 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
66480 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0d 0a 53  ite(DbPage*);..S
66490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
664a0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  t sqlite3PagerMo
664b0 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62  vepage(Pager*,Db
664c0 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b  Page*,Pgno,int);
664d0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
664e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
664f0 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62  rPageRefcount(Db
66500 50 61 67 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  Page*);..SQLITE_
66510 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
66520 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
66530 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0d 0a 53  a(DbPage *); ..S
66540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
66550 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
66560 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
66570 2a 29 3b 20 0d 0a 0d 0a 2f 2a 20 46 75 6e 63 74  *); ..../* Funct
66580 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e  ions used to man
66590 61 67 65 20 70 61 67 65 72 20 74 72 61 6e 73 61  age pager transa
665a0 63 74 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70  ctions and savep
665b0 6f 69 6e 74 73 2e 20 2a 2f 0d 0a 53 51 4c 49 54  oints. */..SQLIT
665c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
665d0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
665e0 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74  ount(Pager*, int
665f0 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
66600 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
66610 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 2a  agerBegin(Pager*
66620 2c 20 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e  , int exFlag, in
66630 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
66640 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
66650 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
66660 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e 73 74 20  ne(Pager*,const 
66670 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69  char *zMaster, i
66680 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
66690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
666a0 50 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f  PagerExclusiveLo
666b0 63 6b 28 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c  ck(Pager*);..SQL
666c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
666d0 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
666e0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
666f0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
66700 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
66710 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
66720 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45  Pager*);..SQLITE
66730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
66740 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
66750 6b 28 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49  k(Pager*);..SQLI
66760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
66770 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53  qlite3PagerOpenS
66780 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a  avepoint(Pager *
66790 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0d  pPager, int n);.
667a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
667b0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
667c0 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
667d0 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c  *pPager, int op,
667e0 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29   int iSavepoint)
667f0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
66800 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
66810 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67  erSharedLock(Pag
66820 65 72 20 2a 70 50 61 67 65 72 29 3b 0d 0a 0d 0a  er *pPager);....
66830 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
66840 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
66850 68 65 63 6b 70 6f 69 6e 74 28 50 61 67 65 72 20  heckpoint(Pager 
66860 2a 70 50 61 67 65 72 2c 20 69 6e 74 2c 20 69 6e  *pPager, int, in
66870 74 2a 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51 4c 49  t*, int*);..SQLI
66880 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
66890 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 53 75  qlite3PagerWalSu
668a0 70 70 6f 72 74 65 64 28 50 61 67 65 72 20 2a 70  pported(Pager *p
668b0 50 61 67 65 72 29 3b 0d 0a 53 51 4c 49 54 45 5f  Pager);..SQLITE_
668c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
668d0 74 65 33 50 61 67 65 72 57 61 6c 43 61 6c 6c 62  te3PagerWalCallb
668e0 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ack(Pager *pPage
668f0 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  r);..SQLITE_PRIV
66900 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
66910 61 67 65 72 4f 70 65 6e 57 61 6c 28 50 61 67 65  agerOpenWal(Page
66920 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a  r *pPager, int *
66930 70 69 73 4f 70 65 6e 29 3b 0d 0a 53 51 4c 49 54  pisOpen);..SQLIT
66940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
66950 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 57  lite3PagerCloseW
66960 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
66970 29 3b 0d 0a 0d 0a 2f 2a 20 46 75 6e 63 74 69 6f  );..../* Functio
66980 6e 73 20 75 73 65 64 20 74 6f 20 71 75 65 72 79  ns used to query
66990 20 70 61 67 65 72 20 73 74 61 74 65 20 61 6e 64   pager state and
669a0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 20   configuration. 
669b0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
669c0 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67  TE u8 sqlite3Pag
669d0 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67  erIsreadonly(Pag
669e0 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  er*);..SQLITE_PR
669f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
66a00 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50  3PagerRefcount(P
66a10 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  ager*);..SQLITE_
66a20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
66a30 74 65 33 50 61 67 65 72 4d 65 6d 55 73 65 64 28  te3PagerMemUsed(
66a40 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45  Pager*);..SQLITE
66a50 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
66a60 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
66a70 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a  rFilename(Pager*
66a80 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
66a90 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
66aa0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
66ab0 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0d 0a  erVfs(Pager*);..
66ac0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
66ad0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c  qlite3_file *sql
66ae0 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61  ite3PagerFile(Pa
66af0 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ger*);..SQLITE_P
66b00 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
66b10 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a  r *sqlite3PagerJ
66b20 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72  ournalname(Pager
66b30 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
66b40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
66b50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72  agerNosync(Pager
66b60 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
66b70 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
66b80 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28  3PagerTempSpace(
66b90 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45  Pager*);..SQLITE
66ba0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
66bb0 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62  ite3PagerIsMemdb
66bc0 28 50 61 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54  (Pager*);..SQLIT
66bd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
66be0 71 6c 69 74 65 33 50 61 67 65 72 43 61 63 68 65  qlite3PagerCache
66bf0 53 74 61 74 28 50 61 67 65 72 20 2a 2c 20 69 6e  Stat(Pager *, in
66c00 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0d  t, int, int *);.
66c10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66c20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
66c30 72 43 6c 65 61 72 43 61 63 68 65 28 50 61 67 65  rClearCache(Page
66c40 72 20 2a 29 3b 0d 0a 0d 0a 2f 2a 20 46 75 6e 63  r *);..../* Func
66c50 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 74 72  tions used to tr
66c60 75 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62  uncate the datab
66c70 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0d 0a 53 51  ase file. */..SQ
66c80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
66c90 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  d sqlite3PagerTr
66ca0 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65  uncateImage(Page
66cb0 72 2a 2c 50 67 6e 6f 29 3b 0d 0a 0d 0a 23 69 66  r*,Pgno);....#if
66cc0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
66cd0 48 41 53 5f 43 4f 44 45 43 29 20 26 26 20 21 64  HAS_CODEC) && !d
66ce0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
66cf0 49 54 5f 57 41 4c 29 0d 0a 53 51 4c 49 54 45 5f  IT_WAL)..SQLITE_
66d00 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
66d10 6c 69 74 65 33 50 61 67 65 72 43 6f 64 65 63 28  lite3PagerCodec(
66d20 44 62 50 61 67 65 20 2a 29 3b 0d 0a 23 65 6e 64  DbPage *);..#end
66d30 69 66 0d 0a 0d 0a 2f 2a 20 46 75 6e 63 74 69 6f  if..../* Functio
66d40 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65  ns to support te
66d50 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
66d60 69 6e 67 2e 20 2a 2f 0d 0a 23 69 66 20 21 64 65  ing. */..#if !de
66d70 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
66d80 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
66d90 54 45 53 54 29 0d 0a 53 51 4c 49 54 45 5f 50 52  TEST)..SQLITE_PR
66da0 49 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c  IVATE   Pgno sql
66db0 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d  ite3PagerPagenum
66dc0 62 65 72 28 44 62 50 61 67 65 2a 29 3b 0d 0a 53  ber(DbPage*);..S
66dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
66de0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
66df0 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61  Iswriteable(DbPa
66e00 67 65 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23  ge*);..#endif..#
66e10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
66e20 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  T..SQLITE_PRIVAT
66e30 45 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33  E   int *sqlite3
66e40 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72  PagerStats(Pager
66e50 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
66e60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
66e70 65 33 50 61 67 65 72 52 65 66 64 75 6d 70 28 50  e3PagerRefdump(P
66e80 61 67 65 72 2a 29 3b 0d 0a 20 20 76 6f 69 64 20  ager*);..  void 
66e90 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
66ea0 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64  d_io_errors(void
66eb0 29 3b 0d 0a 20 20 76 6f 69 64 20 65 6e 61 62 6c  );..  void enabl
66ec0 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
66ed0 72 72 6f 72 73 28 76 6f 69 64 29 3b 0d 0a 23 65  rrors(void);..#e
66ee0 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 64 69  lse..# define di
66ef0 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
66f00 69 6f 5f 65 72 72 6f 72 73 28 29 0d 0a 23 20 64  io_errors()..# d
66f10 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d  efine enable_sim
66f20 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
66f30 28 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 65  ()..#endif....#e
66f40 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48  ndif /* _PAGER_H
66f50 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  _ */..../*******
66f60 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
66f70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.h *********
66f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66fa0 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
66fb0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
66fc0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
66fd0 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
66fe0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
66ff0 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
67000 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
67010 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68  e pcache.h in th
67020 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
67030 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
67040 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
67050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
67060 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 2a   file pcache.h *
67070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
670a0 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30  ** 2008 August 0
670b0 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  5..**..** The au
670c0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
670d0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
670e0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
670f0 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
67100 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
67110 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
67120 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
67130 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
67140 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
67150 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
67160 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
67170 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
67180 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
67190 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
671a0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
671b0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
671c0 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
671d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
671e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
671f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67210 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
67220 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
67230 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
67240 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71  face that the sq
67250 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0d  lite page cache.
67260 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0d  .** subsystem. .
67270 0a 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 5f  .*/....#ifndef _
67280 50 43 41 43 48 45 5f 48 5f 0d 0a 0d 0a 74 79 70  PCACHE_H_....typ
67290 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64  edef struct PgHd
672a0 72 20 50 67 48 64 72 3b 0d 0a 74 79 70 65 64 65  r PgHdr;..typede
672b0 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20  f struct PCache 
672c0 50 43 61 63 68 65 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  PCache;..../*..*
672d0 2a 20 45 76 65 72 79 20 70 61 67 65 20 69 6e 20  * Every page in 
672e0 74 68 65 20 63 61 63 68 65 20 69 73 20 63 6f 6e  the cache is con
672f0 74 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e  trolled by an in
67300 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
67310 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 73 74 72 75  llowing..** stru
67320 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  cture...*/..stru
67330 63 74 20 50 67 48 64 72 20 7b 0d 0a 20 20 73 71  ct PgHdr {..  sq
67340 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67  lite3_pcache_pag
67350 65 20 2a 70 50 61 67 65 3b 20 20 20 20 2f 2a 20  e *pPage;    /* 
67360 50 63 61 63 68 65 20 6f 62 6a 65 63 74 20 70 61  Pcache object pa
67370 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  ge handle */..  
67380 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20  void *pData;    
67390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
673a0 2a 20 50 61 67 65 20 64 61 74 61 20 2a 2f 0d 0a  * Page data */..
673b0 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20    void *pExtra; 
673c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
673d0 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e   /* Extra conten
673e0 74 20 2a 2f 0d 0a 20 20 50 67 48 64 72 20 2a 70  t */..  PgHdr *p
673f0 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20  Dirty;          
67400 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69         /* Transi
67410 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ent list of dirt
67420 79 20 70 61 67 65 73 20 2a 2f 0d 0a 20 20 50 67  y pages */..  Pg
67430 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
67440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67450 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  Page number for 
67460 74 68 69 73 20 70 61 67 65 20 2a 2f 0d 0a 20 20  this page */..  
67470 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20  Pager *pPager;  
67480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67490 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 69 73  * The pager this
674a0 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66   page is part of
674b0 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
674c0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0d 0a  TE_CHECK_PAGES..
674d0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20    u32 pageHash; 
674e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
674f0 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65   /* Hash of page
67500 20 63 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 23 65 6e   content */..#en
67510 64 69 66 0d 0a 20 20 75 31 36 20 66 6c 61 67 73  dif..  u16 flags
67520 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
67530 20 20 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66        /* PGHDR f
67540 6c 61 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c  lags defined bel
67550 6f 77 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 2a 2a 2a  ow */....  /****
67560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
675a0 2a 2a 0d 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e 74  **..  ** Element
675b0 73 20 61 62 6f 76 65 20 61 72 65 20 70 75 62 6c  s above are publ
675c0 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 6f  ic.  All that fo
675d0 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 61 74 65  llows is private
675e0 20 74 6f 20 70 63 61 63 68 65 2e 63 0d 0a 20 20   to pcache.c..  
675f0 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  ** and should no
67600 74 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  t be accessed by
67610 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0d   other modules..
67620 0a 20 20 2a 2f 0d 0a 20 20 69 31 36 20 6e 52 65  .  */..  i16 nRe
67630 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
67640 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
67650 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74 68  r of users of th
67660 69 73 20 70 61 67 65 20 2a 2f 0d 0a 20 20 50 43  is page */..  PC
67670 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20  ache *pCache;   
67680 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67690 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 20  Cache that owns 
676a0 74 68 69 73 20 70 61 67 65 20 2a 2f 0d 0a 0d 0a  this page */....
676b0 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e    PgHdr *pDirtyN
676c0 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
676d0 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74   /* Next element
676e0 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
676f0 79 20 70 61 67 65 73 20 2a 2f 0d 0a 20 20 50 67  y pages */..  Pg
67700 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b  Hdr *pDirtyPrev;
67710 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67720 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  Previous element
67730 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
67740 79 20 70 61 67 65 73 20 2a 2f 0d 0a 7d 3b 0d 0a  y pages */..};..
67750 0d 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73 20  ../* Bit values 
67760 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73 20  for PgHdr.flags 
67770 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50 47 48 44  */..#define PGHD
67780 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20  R_DIRTY         
67790 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61      0x002  /* Pa
677a0 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  ge has changed *
677b0 2f 0d 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  /..#define PGHDR
677c0 5f 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20  _NEED_SYNC      
677d0 20 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79     0x004  /* Fsy
677e0 6e 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  nc the rollback 
677f0 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0d 0a  journal before..
67800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67820 20 20 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e         ** writin
67830 67 20 74 68 69 73 20 70 61 67 65 20 74 6f 20 74  g this page to t
67840 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0d 0a  he database */..
67850 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45  #define PGHDR_NE
67860 45 44 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  ED_READ         
67870 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e  0x008  /* Conten
67880 74 20 69 73 20 75 6e 72 65 61 64 20 2a 2f 0d 0a  t is unread */..
67890 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 52 45  #define PGHDR_RE
678a0 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 20 20  USE_UNLIKELY    
678b0 30 78 30 31 30 20 20 2f 2a 20 41 20 68 69 6e 74  0x010  /* A hint
678c0 20 74 68 61 74 20 72 65 75 73 65 20 69 73 20 75   that reuse is u
678d0 6e 6c 69 6b 65 6c 79 20 2a 2f 0d 0a 23 64 65 66  nlikely */..#def
678e0 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57  ine PGHDR_DONT_W
678f0 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 32  RITE        0x02
67900 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69  0  /* Do not wri
67910 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 69  te content to di
67920 73 6b 20 2a 2f 0d 0a 0d 0a 2f 2a 20 49 6e 69 74  sk */..../* Init
67930 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64  ialize and shutd
67940 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63  own the page cac
67950 68 65 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0d  he subsystem */.
67960 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67970 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
67980 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64  eInitialize(void
67990 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
679a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
679b0 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f  cacheShutdown(vo
679c0 69 64 29 3b 0d 0a 0d 0a 2f 2a 20 50 61 67 65 20  id);..../* Page 
679d0 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e  cache buffer man
679e0 61 67 65 6d 65 6e 74 3a 0d 0a 2a 2a 20 54 68 65  agement:..** The
679f0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  se routines impl
67a00 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e  ement SQLITE_CON
67a10 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0d 0a  FIG_PAGECACHE...
67a20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
67a30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
67a40 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70  CacheBufferSetup
67a50 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c  (void *, int sz,
67a60 20 69 6e 74 20 6e 29 3b 0d 0a 0d 0a 2f 2a 20 43   int n);..../* C
67a70 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
67a80 72 20 63 61 63 68 65 2e 0d 0a 2a 2a 20 55 6e 64  r cache...** Und
67a90 65 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73  er memory stress
67aa0 2c 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73  , invoke xStress
67ab0 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20   to try to make 
67ac0 70 61 67 65 73 20 63 6c 65 61 6e 2e 0d 0a 2a 2a  pages clean...**
67ad0 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20   Only clean and 
67ae0 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63  unpinned pages c
67af0 61 6e 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e  an be reclaimed.
67b00 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
67b10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
67b20 33 50 63 61 63 68 65 4f 70 65 6e 28 0d 0a 20 20  3PcacheOpen(..  
67b30 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20  int szPage,     
67b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67b50 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20  * Size of every 
67b60 70 61 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73  page */..  int s
67b70 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  zExtra,         
67b80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
67b90 72 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61  ra space associa
67ba0 74 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61  ted with each pa
67bb0 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 62 50 75  ge */..  int bPu
67bc0 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20  rgeable,        
67bd0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
67be0 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20  if pages are on 
67bf0 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f  backing store */
67c00 0d 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73  ..  int (*xStres
67c10 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a  s)(void*, PgHdr*
67c20 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72  ), /* Call to tr
67c30 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20  y to make pages 
67c40 63 6c 65 61 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64  clean */..  void
67c50 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20   *pStress,      
67c60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
67c70 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
67c80 73 20 2a 2f 0d 0a 20 20 50 43 61 63 68 65 20 2a  s */..  PCache *
67c90 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20  pToInit         
67ca0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
67cb0 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
67cc0 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0d 0a   the PCache */..
67cd0 29 3b 0d 0a 0d 0a 2f 2a 20 4d 6f 64 69 66 79 20  );..../* Modify 
67ce0 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66  the page-size af
67cf0 74 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61  ter the cache ha
67d00 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20  s been created. 
67d10 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
67d20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
67d30 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65  cacheSetPageSize
67d40 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b  (PCache *, int);
67d50 0d 0a 0d 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68  ..../* Return th
67d60 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
67d70 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
67d80 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65  ct.  Used to pre
67d90 61 6c 6c 6f 63 61 74 65 0d 0a 2a 2a 20 73 74 6f  allocate..** sto
67da0 72 61 67 65 20 73 70 61 63 65 2e 0d 0a 2a 2f 0d  rage space...*/.
67db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67dc0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
67dd0 65 53 69 7a 65 28 76 6f 69 64 29 3b 0d 0a 0d 0a  eSize(void);....
67de0 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 70  /* One release p
67df0 65 72 20 73 75 63 63 65 73 73 66 75 6c 20 66 65  er successful fe
67e00 74 63 68 2e 20 20 50 61 67 65 20 69 73 20 70 69  tch.  Page is pi
67e10 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 61  nned until relea
67e20 73 65 64 2e 0d 0a 2a 2a 20 52 65 66 65 72 65 6e  sed...** Referen
67e30 63 65 20 63 6f 75 6e 74 65 64 2e 20 0d 0a 2a 2f  ce counted. ..*/
67e40 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
67e50 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
67e60 68 65 46 65 74 63 68 28 50 43 61 63 68 65 2a 2c  heFetch(PCache*,
67e70 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74   Pgno, int creat
67e80 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b  eFlag, PgHdr**);
67e90 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
67ea0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
67eb0 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72  cheRelease(PgHdr
67ec0 2a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  *);....SQLITE_PR
67ed0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
67ee0 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48  e3PcacheDrop(PgH
67ef0 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  dr*);         /*
67f00 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f   Remove page fro
67f10 6d 20 63 61 63 68 65 20 2a 2f 0d 0a 53 51 4c 49  m cache */..SQLI
67f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
67f30 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
67f40 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20  eDirty(PgHdr*); 
67f50 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
67f60 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64  page is marked d
67f70 69 72 74 79 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  irty */..SQLITE_
67f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
67f90 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
67fa0 65 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20  ean(PgHdr*);    
67fb0 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65  /* Mark a single
67fc0 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a   page as clean *
67fd0 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
67fe0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
67ff0 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61  acheCleanAll(PCa
68000 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72  che*);    /* Mar
68010 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74  k all dirty list
68020 20 70 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20   pages as clean 
68030 2a 2f 0d 0a 0d 0a 2f 2a 20 43 68 61 6e 67 65 20  */..../* Change 
68040 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  a page number.  
68050 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63  Used by incr-vac
68060 75 75 6d 2e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  uum. */..SQLITE_
68070 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
68080 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50  ite3PcacheMove(P
68090 67 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0d 0a 0d  gHdr*, Pgno);...
680a0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70  ./* Remove all p
680b0 61 67 65 73 20 77 69 74 68 20 70 67 6e 6f 3e 78  ages with pgno>x
680c0 2e 20 20 52 65 73 65 74 20 74 68 65 20 63 61 63  .  Reset the cac
680d0 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f 0d 0a 53  he if x==0 */..S
680e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
680f0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
68100 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 2a  Truncate(PCache*
68110 2c 20 50 67 6e 6f 20 78 29 3b 0d 0a 0d 0a 2f 2a  , Pgno x);..../*
68120 20 47 65 74 20 61 20 6c 69 73 74 20 6f 66 20 61   Get a list of a
68130 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69  ll dirty pages i
68140 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72  n the cache, sor
68150 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62  ted by page numb
68160 65 72 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  er */..SQLITE_PR
68170 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c  IVATE PgHdr *sql
68180 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
68190 69 73 74 28 50 43 61 63 68 65 2a 29 3b 0d 0a 0d  ist(PCache*);...
681a0 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 6c  ./* Reset and cl
681b0 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f 62  ose the cache ob
681c0 6a 65 63 74 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ject */..SQLITE_
681d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
681e0 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
681f0 50 43 61 63 68 65 2a 29 3b 0d 0a 0d 0a 2f 2a 20  PCache*);..../* 
68200 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d  Clear flags from
68210 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61   pages of the pa
68220 67 65 20 63 61 63 68 65 20 2a 2f 0d 0a 53 51 4c  ge cache */..SQL
68230 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
68240 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
68250 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61  earSyncFlags(PCa
68260 63 68 65 20 2a 29 3b 0d 0a 0d 0a 2f 2a 20 44 69  che *);..../* Di
68270 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
68280 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 20  ts of the cache 
68290 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
682a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
682b0 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
682c0 65 2a 29 3b 0d 0a 0d 0a 2f 2a 20 52 65 74 75 72  e*);..../* Retur
682d0 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
682e0 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e  er of outstandin
682f0 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65  g page reference
68300 73 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  s */..SQLITE_PRI
68310 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
68320 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50  PcacheRefCount(P
68330 43 61 63 68 65 2a 29 3b 0d 0a 0d 0a 2f 2a 20 49  Cache*);..../* I
68340 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ncrement the ref
68350 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20  erence count of 
68360 61 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 65  an existing page
68370 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
68380 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
68390 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 2a  PcacheRef(PgHdr*
683a0 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  );....SQLITE_PRI
683b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
683c0 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75  PcachePageRefcou
683d0 6e 74 28 50 67 48 64 72 2a 29 3b 0d 0a 0d 0a 2f  nt(PgHdr*);..../
683e0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
683f0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
68400 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  es stored in the
68410 20 63 61 63 68 65 20 2a 2f 0d 0a 53 51 4c 49 54   cache */..SQLIT
68420 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
68430 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63  lite3PcachePagec
68440 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0d 0a  ount(PCache*);..
68450 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
68460 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
68470 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
68480 49 54 45 5f 44 45 42 55 47 29 0d 0a 2f 2a 20 49  ITE_DEBUG)../* I
68490 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
684a0 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63  ll dirty pages c
684b0 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
684c0 69 6e 20 74 68 65 20 63 61 63 68 65 2e 20 54 68  in the cache. Th
684d0 69 73 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  is..** interface
684e0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
684f0 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43 48 45  le if SQLITE_CHE
68500 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66 69  CK_PAGES is defi
68510 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0d 0a 2a  ned when the ..*
68520 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75 69  * library is bui
68530 6c 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  lt...*/..SQLITE_
68540 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
68550 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74  ite3PcacheIterat
68560 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70  eDirty(PCache *p
68570 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49  Cache, void (*xI
68580 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0d  ter)(PgHdr *));.
68590 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 53 65  .#endif..../* Se
685a0 74 20 61 6e 64 20 67 65 74 20 74 68 65 20 73 75  t and get the su
685b0 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
685c0 7a 65 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  ze for the speci
685d0 66 69 65 64 20 70 61 67 65 72 2d 63 61 63 68 65  fied pager-cache
685e0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 20  ...**..** If no 
685f0 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 69  global maximum i
68600 73 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68  s configured, th
68610 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20 61 74  en the system at
68620 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 0d  tempts to limit.
68630 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  .** the total nu
68640 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61  mber of pages ca
68650 63 68 65 64 20 62 79 20 70 75 72 67 65 61 62 6c  ched by purgeabl
68660 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 20 74  e pager-caches t
68670 6f 20 74 68 65 20 73 75 6d 0d 0a 2a 2a 20 6f 66  o the sum..** of
68680 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
68690 61 63 68 65 2d 73 69 7a 65 73 2e 0d 0a 2a 2f 0d  ache-sizes...*/.
686a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
686b0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
686c0 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  heSetCachesize(P
686d0 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0d 0a  Cache *, int);..
686e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
686f0 53 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ST..SQLITE_PRIVA
68700 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
68710 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65  acheGetCachesize
68720 28 50 43 61 63 68 65 20 2a 29 3b 0d 0a 23 65 6e  (PCache *);..#en
68730 64 69 66 0d 0a 0d 0a 2f 2a 20 46 72 65 65 20 75  dif..../* Free u
68740 70 20 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79  p as much memory
68750 20 61 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f   as possible fro
68760 6d 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  m the page cache
68770 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
68780 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
68790 50 63 61 63 68 65 53 68 72 69 6e 6b 28 50 43 61  PcacheShrink(PCa
687a0 63 68 65 2a 29 3b 0d 0a 0d 0a 23 69 66 64 65 66  che*);....#ifdef
687b0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
687c0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
687d0 0d 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 74 75  ../* Try to retu
687e0 72 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62  rn memory used b
687f0 79 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64  y the pcache mod
68800 75 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 6e 20  ule to the main 
68810 6d 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f 0d 0a  memory heap */..
68820 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
68830 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
68840 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
68850 74 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23  t);..#endif....#
68860 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
68870 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  T..SQLITE_PRIVAT
68880 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
68890 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69  acheStats(int*,i
688a0 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0d  nt*,int*,int*);.
688b0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54  .#endif....SQLIT
688c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
688d0 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44  qlite3PCacheSetD
688e0 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0d 0a 0d  efault(void);...
688f0 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43  .#endif /* _PCAC
68900 48 45 5f 48 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a  HE_H_ */..../***
68910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
68920 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  of pcache.h ****
68930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
68960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
68970 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
68980 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
68990 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
689a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a  ***********/....
689b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
689c0 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20  Include os.h in 
689d0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
689e0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
689f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
68a00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
68a10 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68   Begin file os.h
68a20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
68a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
68a50 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65  ../*..** 2001 Se
68a60 70 74 65 6d 62 65 72 20 31 36 0d 0a 2a 2a 0d 0a  ptember 16..**..
68a70 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
68a80 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
68a90 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
68aa0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
68ab0 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
68ac0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
68ad0 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
68ae0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
68af0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
68b00 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
68b10 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
68b20 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
68b30 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
68b40 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
68b50 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
68b60 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
68b70 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
68b80 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
68b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68bd0 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  *******..**..** 
68be0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
68bf0 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20   (together with 
68c00 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73  is companion C s
68c10 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0d  ource-code file.
68c20 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65  .** "os.c") atte
68c30 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20  mpt to abstract 
68c40 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
68c50 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
68c60 73 6f 20 74 68 61 74 0d 0a 2a 2a 20 74 68 65 20  so that..** the 
68c70 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77  SQLite library w
68c80 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68  ill work on both
68c90 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f   POSIX and windo
68ca0 77 73 20 73 79 73 74 65 6d 73 2e 0d 0a 2a 2a 0d  ws systems...**.
68cb0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
68cc0 66 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65  file is #include
68cd0 2d 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74  -ed by sqliteInt
68ce0 2e 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73  .h and thus ends
68cf0 20 75 70 0d 0a 2a 2a 20 62 65 69 6e 67 20 69 6e   up..** being in
68d00 63 6c 75 64 65 64 20 62 79 20 65 76 65 72 79 20  cluded by every 
68d10 73 6f 75 72 63 65 20 66 69 6c 65 2e 0d 0a 2a 2f  source file...*/
68d20 0d 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  ..#ifndef _SQLIT
68d30 45 5f 4f 53 5f 48 5f 0d 0a 23 64 65 66 69 6e 65  E_OS_H_..#define
68d40 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0d 0a   _SQLITE_OS_H_..
68d50 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 67 75 72 65 20  ../*..** Figure 
68d60 6f 75 74 20 69 66 20 77 65 20 61 72 65 20 64 65  out if we are de
68d70 61 6c 69 6e 67 20 77 69 74 68 20 55 6e 69 78 2c  aling with Unix,
68d80 20 57 69 6e 64 6f 77 73 2c 20 6f 72 20 73 6f 6d   Windows, or som
68d90 65 20 6f 74 68 65 72 0d 0a 2a 2a 20 6f 70 65 72  e other..** oper
68da0 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 41  ating system.  A
68db0 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69  fter the followi
68dc0 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70 72 65 70  ng block of prep
68dd0 72 6f 63 65 73 73 20 6d 61 63 72 6f 73 2c 0d 0a  rocess macros,..
68de0 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49 54 45  ** all of SQLITE
68df0 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49 54 45  _OS_UNIX, SQLITE
68e00 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 54 45 5f  _OS_WIN, SQLITE_
68e10 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 51 4c 49  OS_OS2, and SQLI
68e20 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0d 0a 2a 2a  TE_OS_OTHER ..**
68e30 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 74 6f   will defined to
68e40 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e 20   either 1 or 0. 
68e50 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 75 72   One of the four
68e60 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 68 65   will be 1.  The
68e70 20 6f 74 68 65 72 20 0d 0a 2a 2a 20 74 68 72 65   other ..** thre
68e80 65 20 77 69 6c 6c 20 62 65 20 30 2e 0d 0a 2a 2f  e will be 0...*/
68e90 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
68ea0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0d 0a  LITE_OS_OTHER)..
68eb0 23 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  # if SQLITE_OS_O
68ec0 54 48 45 52 3d 3d 31 0d 0a 23 20 20 20 75 6e 64  THER==1..#   und
68ed0 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ef SQLITE_OS_UNI
68ee0 58 0d 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  X..#   define SQ
68ef0 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0d 0a  LITE_OS_UNIX 0..
68f00 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
68f10 5f 4f 53 5f 57 49 4e 0d 0a 23 20 20 20 64 65 66  _OS_WIN..#   def
68f20 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
68f30 4e 20 30 0d 0a 23 20 20 20 75 6e 64 65 66 20 53  N 0..#   undef S
68f40 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0d 0a 23 20  QLITE_OS_OS2..# 
68f50 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
68f60 4f 53 5f 4f 53 32 20 30 0d 0a 23 20 65 6c 73 65  OS_OS2 0..# else
68f70 0d 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49  ..#   undef SQLI
68f80 54 45 5f 4f 53 5f 4f 54 48 45 52 0d 0a 23 20 65  TE_OS_OTHER..# e
68f90 6e 64 69 66 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  ndif..#endif..#i
68fa0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
68fb0 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64  E_OS_UNIX) && !d
68fc0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53  efined(SQLITE_OS
68fd0 5f 4f 54 48 45 52 29 0d 0a 23 20 64 65 66 69 6e  _OTHER)..# defin
68fe0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45  e SQLITE_OS_OTHE
68ff0 52 20 30 0d 0a 23 20 69 66 6e 64 65 66 20 53 51  R 0..# ifndef SQ
69000 4c 49 54 45 5f 4f 53 5f 57 49 4e 0d 0a 23 20 20  LITE_OS_WIN..#  
69010 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e   if defined(_WIN
69020 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57  32) || defined(W
69030 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  IN32) || defined
69040 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20  (__CYGWIN__) || 
69050 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33  defined(__MINGW3
69060 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  2__) || defined(
69070 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0d 0a 23  __BORLANDC__)..#
69080 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
69090 54 45 5f 4f 53 5f 57 49 4e 20 31 0d 0a 23 20 20  TE_OS_WIN 1..#  
690a0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
690b0 5f 4f 53 5f 55 4e 49 58 20 30 0d 0a 23 20 20 20  _OS_UNIX 0..#   
690c0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
690d0 4f 53 5f 4f 53 32 20 30 0d 0a 23 20 20 20 65 6c  OS_OS2 0..#   el
690e0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58  if defined(__EMX
690f0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
69100 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  OS2) || defined(
69110 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  OS2) || defined(
69120 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65  _OS2_) || define
69130 64 28 5f 5f 4f 53 32 5f 5f 29 0d 0a 23 20 20 20  d(__OS2__)..#   
69140 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
69150 4f 53 5f 57 49 4e 20 30 0d 0a 23 20 20 20 20 20  OS_WIN 0..#     
69160 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
69170 5f 55 4e 49 58 20 30 0d 0a 23 20 20 20 20 20 64  _UNIX 0..#     d
69180 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
69190 4f 53 32 20 31 0d 0a 23 20 20 20 65 6c 73 65 0d  OS2 1..#   else.
691a0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
691b0 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0d 0a 23  LITE_OS_WIN 0..#
691c0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
691d0 54 45 5f 4f 53 5f 55 4e 49 58 20 31 0d 0a 23 20  TE_OS_UNIX 1..# 
691e0 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
691f0 45 5f 4f 53 5f 4f 53 32 20 30 0d 0a 23 20 20 65  E_OS_OS2 0..#  e
69200 6e 64 69 66 0d 0a 23 20 65 6c 73 65 0d 0a 23 20  ndif..# else..# 
69210 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
69220 53 5f 55 4e 49 58 20 30 0d 0a 23 20 20 64 65 66  S_UNIX 0..#  def
69230 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  ine SQLITE_OS_OS
69240 32 20 30 0d 0a 23 20 65 6e 64 69 66 0d 0a 23 65  2 0..# endif..#e
69250 6c 73 65 0d 0a 23 20 69 66 6e 64 65 66 20 53 51  lse..# ifndef SQ
69260 4c 49 54 45 5f 4f 53 5f 57 49 4e 0d 0a 23 20 20  LITE_OS_WIN..#  
69270 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
69280 5f 57 49 4e 20 30 0d 0a 23 20 65 6e 64 69 66 0d  _WIN 0..# endif.
69290 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
692a0 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78  * Define the max
692b0 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74  imum size of a t
692c0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d  emporary filenam
692d0 65 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54  e..*/..#if SQLIT
692e0 45 5f 4f 53 5f 57 49 4e 0d 0a 23 20 69 6e 63 6c  E_OS_WIN..# incl
692f0 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0d  ude <windows.h>.
69300 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
69310 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28  _TEMPNAME_SIZE (
69320 4d 41 58 5f 50 41 54 48 2b 35 30 29 0d 0a 23 65  MAX_PATH+50)..#e
69330 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  lif SQLITE_OS_OS
69340 32 0d 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f  2..# if (__GNUC_
69350 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f  _ > 3 || __GNUC_
69360 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43  _ == 3 && __GNUC
69370 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26  _MINOR__ >= 3) &
69380 26 20 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49  & defined(OS2_HI
69390 47 48 5f 4d 45 4d 4f 52 59 29 0d 0a 23 20 20 69  GH_MEMORY)..#  i
693a0 6e 63 6c 75 64 65 20 3c 6f 73 32 73 61 66 65 2e  nclude <os2safe.
693b0 68 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62 65 20  h> /* has to be 
693c0 69 6e 63 6c 75 64 65 64 20 62 65 66 6f 72 65 20  included before 
693d0 6f 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e  os2.h for linkin
693e0 67 20 74 6f 20 77 6f 72 6b 20 2a 2f 0d 0a 23 20  g to work */..# 
693f0 65 6e 64 69 66 0d 0a 23 20 64 65 66 69 6e 65 20  endif..# define 
69400 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45  INCL_DOSDATETIME
69410 0d 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  ..# define INCL_
69420 44 4f 53 46 49 4c 45 4d 47 52 0d 0a 23 20 64 65  DOSFILEMGR..# de
69430 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52  fine INCL_DOSERR
69440 4f 52 53 0d 0a 23 20 64 65 66 69 6e 65 20 49 4e  ORS..# define IN
69450 43 4c 5f 44 4f 53 4d 49 53 43 0d 0a 23 20 64 65  CL_DOSMISC..# de
69460 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f  fine INCL_DOSPRO
69470 43 45 53 53 0d 0a 23 20 64 65 66 69 6e 65 20 49  CESS..# define I
69480 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52  NCL_DOSMODULEMGR
69490 0d 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  ..# define INCL_
694a0 44 4f 53 53 45 4d 41 50 48 4f 52 45 53 0d 0a 23  DOSSEMAPHORES..#
694b0 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e   include <os2.h>
694c0 0d 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f  ..# include <uco
694d0 6e 76 2e 68 3e 0d 0a 23 20 64 65 66 69 6e 65 20  nv.h>..# define 
694e0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
694f0 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48  SIZE (CCHMAXPATH
69500 43 4f 4d 50 29 0d 0a 23 65 6c 73 65 0d 0a 23 20  COMP)..#else..# 
69510 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
69520 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0d  MPNAME_SIZE 200.
69530 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
69540 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77  * Determine if w
69550 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
69560 74 68 20 57 69 6e 64 6f 77 73 20 4e 54 2e 0d 0a  th Windows NT...
69570 2a 2f 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
69580 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 29 0d 0a 23  _WIN32_WINNT)..#
69590 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
695a0 53 5f 57 49 4e 4e 54 20 31 0d 0a 23 65 6c 73 65  S_WINNT 1..#else
695b0 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ..# define SQLIT
695c0 45 5f 4f 53 5f 57 49 4e 4e 54 20 30 0d 0a 23 65  E_OS_WINNT 0..#e
695d0 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44  ndif..../*..** D
695e0 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61  etermine if we a
695f0 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
69600 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63  WindowsCE - whic
69610 68 20 68 61 73 20 61 20 6d 75 63 68 0d 0a 2a 2a  h has a much..**
69620 20 72 65 64 75 63 65 64 20 41 50 49 2e 0d 0a 2a   reduced API...*
69630 2f 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  /..#if defined(_
69640 57 49 4e 33 32 5f 57 43 45 29 0d 0a 23 20 64 65  WIN32_WCE)..# de
69650 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
69660 49 4e 43 45 20 31 0d 0a 23 65 6c 73 65 0d 0a 23  INCE 1..#else..#
69670 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
69680 53 5f 57 49 4e 43 45 20 30 0d 0a 23 65 6e 64 69  S_WINCE 0..#endi
69690 66 0d 0a 0d 0a 2f 2a 20 49 66 20 74 68 65 20 53  f..../* If the S
696a0 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72  ET_FULLSYNC macr
696b0 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  o is not defined
696c0 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b   above, then mak
696d0 65 20 69 74 0d 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  e it..** a no-op
696e0 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 45  ..*/..#ifndef SE
696f0 54 5f 46 55 4c 4c 53 59 4e 43 0d 0a 23 20 64 65  T_FULLSYNC..# de
69700 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e  fine SET_FULLSYN
69710 43 28 78 2c 79 29 0d 0a 23 65 6e 64 69 66 0d 0a  C(x,y)..#endif..
69720 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 64 65 66  ../*..** The def
69730 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64  ault size of a d
69740 69 73 6b 20 73 65 63 74 6f 72 0d 0a 2a 2f 0d 0a  isk sector..*/..
69750 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
69760 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
69770 5a 45 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ZE..# define SQL
69780 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
69790 4f 52 5f 53 49 5a 45 20 34 30 39 36 0d 0a 23 65  OR_SIZE 4096..#e
697a0 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ndif..../*..** T
697b0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61  emporary files a
697c0 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 6e  re named startin
697d0 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65 66  g with this pref
697e0 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31  ix followed by 1
697f0 36 20 72 61 6e 64 6f 6d 0d 0a 2a 2a 20 61 6c 70  6 random..** alp
69800 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63  hanumeric charac
69810 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c  ters, and no fil
69820 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65  e extension. The
69830 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  y are stored in 
69840 74 68 65 0d 0a 2a 2a 20 4f 53 27 73 20 73 74 61  the..** OS's sta
69850 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20  ndard temporary 
69860 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20  file directory, 
69870 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20  and are deleted 
69880 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0d 0a  prior to exit...
69890 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73 20  ** If sqlite is 
698a0 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69  being embedded i
698b0 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61  n another progra
698c0 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20  m, you may wish 
698d0 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0d 0a 2a  to change the..*
698e0 2a 20 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c  * prefix to refl
698f0 65 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d  ect your program
69900 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74  's name, so that
69910 20 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d   if your program
69920 20 65 78 69 74 73 0d 0a 2a 2a 20 70 72 65 6d 61   exits..** prema
69930 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70  turely, old temp
69940 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20  orary files can 
69950 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69  be easily identi
69960 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62  fied. This can b
69970 65 20 64 6f 6e 65 0d 0a 2a 2a 20 75 73 69 6e 67  e done..** using
69980 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46   -DSQLITE_TEMP_F
69990 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72 65  ILE_PREFIX=mypre
699a0 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70  fix_ on the comp
699b0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e  iler command lin
699c0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 32 30 30 36 2d  e...**..** 2006-
699d0 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61  10-31:  The defa
699e0 75 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20  ult prefix used 
699f0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e  to be "sqlite_".
69a00 20 20 42 75 74 20 74 68 65 6e 0d 0a 2a 2a 20 4d    But then..** M
69a10 63 61 66 65 65 20 73 74 61 72 74 65 64 20 75 73  cafee started us
69a20 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68  ing SQLite in th
69a30 65 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70  eir anti-virus p
69a40 72 6f 64 75 63 74 20 61 6e 64 20 69 74 0d 0a 2a  roduct and it..*
69a50 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 69 6e  * started puttin
69a60 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 68 65  g files with the
69a70 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69   "sqlite" name i
69a80 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f  n the c:/temp fo
69a90 6c 64 65 72 2e 0d 0a 2a 2a 20 54 68 69 73 20 61  lder...** This a
69aa0 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e 64  nnoyed many wind
69ab0 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f 73  ows users.  Thos
69ac0 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 68  e users would th
69ad0 65 6e 20 64 6f 20 61 20 0d 0a 2a 2a 20 47 6f 6f  en do a ..** Goo
69ae0 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22  gle search for "
69af0 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68  sqlite", find th
69b00 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62  e telephone numb
69b10 65 72 73 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 64  ers of the..** d
69b20 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61  evelopers and ca
69b30 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20  ll to wake them 
69b40 75 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20  up at night and 
69b50 63 6f 6d 70 6c 61 69 6e 2e 0d 0a 2a 2a 20 46 6f  complain...** Fo
69b60 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74  r this reason, t
69b70 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20  he default name 
69b80 70 72 65 66 69 78 20 69 73 20 63 68 61 6e 67 65  prefix is change
69b90 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22  d to be "sqlite"
69ba0 20 0d 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61   ..** spelled ba
69bb0 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74 68 65  ckwards.  So the
69bc0 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20   temp files are 
69bd0 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 64  still identified
69be0 2c 20 62 75 74 0d 0a 2a 2a 20 61 6e 79 62 6f 64  , but..** anybod
69bf0 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20 74  y smart enough t
69c00 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 65  o figure out the
69c10 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c 69   code is also li
69c20 6b 65 6c 79 20 73 6d 61 72 74 0d 0a 2a 2a 20 65  kely smart..** e
69c30 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68  nough to know th
69c40 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64  at calling the d
69c50 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f  eveloper will no
69c60 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0d 0a  t help get rid..
69c70 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0d  ** of the file..
69c80 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
69c90 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
69ca0 45 46 49 58 0d 0a 23 20 64 65 66 69 6e 65 20 53  EFIX..# define S
69cb0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
69cc0 50 52 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22  PREFIX "etilqs_"
69cd0 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
69ce0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
69cf0 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70   values may be p
69d00 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
69d10 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0d  ond argument to.
69d20 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  .** sqlite3OsLoc
69d30 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f 75 73  k(). The various
69d40 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 20 74   locks exhibit t
69d50 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d  he following sem
69d60 61 6e 74 69 63 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  antics:..**..** 
69d70 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e  SHARED:    Any n
69d80 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73  umber of process
69d90 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48  es may hold a SH
69da0 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74  ARED lock simult
69db0 61 6e 65 6f 75 73 6c 79 2e 0d 0a 2a 2a 20 52 45  aneously...** RE
69dc0 53 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c  SERVED:  A singl
69dd0 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f  e process may ho
69de0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ld a RESERVED lo
69df0 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0d  ck on a file at.
69e00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61  .**            a
69e10 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70  ny time. Other p
69e20 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c  rocesses may hol
69e30 64 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77  d and obtain new
69e40 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0d 0a   SHARED locks...
69e50 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20  ** PENDING:   A 
69e60 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d  single process m
69e70 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e  ay hold a PENDIN
69e80 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65  G lock on a file
69e90 20 61 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20   at..**         
69ea0 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e     any one time.
69eb0 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45 44   Existing SHARED
69ec0 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73 69   locks may persi
69ed0 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0d 0a  st, but no new..
69ee0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48  **            SH
69ef0 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62  ARED locks may b
69f00 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74  e obtained by ot
69f10 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0d 0a  her processes...
69f20 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e  ** EXCLUSIVE: An
69f30 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
69f40 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74  precludes all ot
69f50 68 65 72 20 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a  her locks...**..
69f60 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  ** PENDING_LOCK 
69f70 6d 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65  may not be passe
69f80 64 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71  d directly to sq
69f90 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49  lite3OsLock(). I
69fa0 6e 73 74 65 61 64 2c 20 61 0d 0a 2a 2a 20 70 72  nstead, a..** pr
69fb0 6f 63 65 73 73 20 74 68 61 74 20 72 65 71 75 65  ocess that reque
69fc0 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45  sts an EXCLUSIVE
69fd0 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c   lock may actual
69fe0 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44  ly obtain a PEND
69ff0 49 4e 47 0d 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68  ING..** lock. Th
6a000 69 73 20 63 61 6e 20 62 65 20 75 70 67 72 61 64  is can be upgrad
6a010 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49  ed to an EXCLUSI
6a020 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75 62  VE lock by a sub
6a030 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0d  sequent call to.
6a040 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  .** sqlite3OsLoc
6a050 6b 28 29 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  k()...*/..#defin
6a060 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20  e NO_LOCK       
6a070 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 53 48 41    0..#define SHA
6a080 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0d 0a  RED_LOCK     1..
6a090 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44  #define RESERVED
6a0a0 5f 4c 4f 43 4b 20 20 20 32 0d 0a 23 64 65 66 69  _LOCK   2..#defi
6a0b0 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  ne PENDING_LOCK 
6a0c0 20 20 20 33 0d 0a 23 64 65 66 69 6e 65 20 45 58     3..#define EX
6a0d0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0d  CLUSIVE_LOCK  4.
6a0e0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6c 65 20 4c  .../*..** File L
6a0f0 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28  ocking Notes:  (
6a100 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69 6e  Mostly about win
6a110 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73 6f  dows but also so
6a120 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78  me info for Unix
6a130 29 0d 0a 2a 2a 0d 0a 2a 2a 20 57 65 20 63 61 6e  )..**..** We can
6a140 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65  not use LockFile
6a150 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69  Ex() or UnlockFi
6a160 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f  leEx() on Win95/
6a170 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0d 0a 2a  98/ME because..*
6a180 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e  * those function
6a190 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
6a1a0 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20  ble.  So we use 
6a1b0 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20  only LockFile() 
6a1c0 61 6e 64 0d 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69  and..** UnlockFi
6a1d0 6c 65 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4c 6f  le()...**..** Lo
6a1e0 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e 74  ckFile() prevent
6a1f0 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74 69  s not just writi
6a200 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61 64  ng but also read
6a210 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72 6f  ing by other pro
6a220 63 65 73 73 65 73 2e 0d 0a 2a 2a 20 41 20 53 48  cesses...** A SH
6a230 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  ARED_LOCK is obt
6a240 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
6a250 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d   a single random
6a260 6c 79 2d 63 68 6f 73 65 6e 20 0d 0a 2a 2a 20 62  ly-chosen ..** b
6a270 79 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65  yte out of a spe
6a280 63 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62  cific range of b
6a290 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62  ytes. The lock b
6a2a0 79 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20  yte is obtained 
6a2b0 61 74 20 0d 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73  at ..** random s
6a2c0 6f 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 72  o two separate r
6a2d0 65 61 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61  eaders can proba
6a2e0 62 6c 79 20 61 63 63 65 73 73 20 74 68 65 20 66  bly access the f
6a2f0 69 6c 65 20 61 74 20 74 68 65 20 0d 0a 2a 2a 20  ile at the ..** 
6a300 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73  same time, unles
6a310 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c 75 63  s they are unluc
6a320 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 68  ky and choose th
6a330 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65  e same lock byte
6a340 2e 0d 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49  ...** An EXCLUSI
6a350 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69  VE_LOCK is obtai
6a360 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61  ned by locking a
6a370 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  ll bytes in the 
6a380 72 61 6e 67 65 2e 0d 0a 2a 2a 20 54 68 65 72 65  range...** There
6a390 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65   can only be one
6a3a0 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45   writer.  A RESE
6a3b0 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  RVED_LOCK is obt
6a3c0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
6a3d0 0d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79  ..** a single by
6a3e0 74 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74  te of the file t
6a3f0 68 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65  hat is designate
6a400 64 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65  d as the reserve
6a410 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0d 0a 2a 2a  d lock byte...**
6a420 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20   A PENDING_LOCK 
6a430 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
6a440 6f 63 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61  ocking a designa
6a450 74 65 64 20 62 79 74 65 20 64 69 66 66 65 72 65  ted byte differe
6a460 6e 74 20 66 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20  nt from..** the 
6a470 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79  RESERVED_LOCK by
6a480 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 20 57  te...**..** On W
6a490 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65  inNT/2K/XP syste
6a4a0 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  ms, LockFileEx()
6a4b0 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45   and UnlockFileE
6a4c0 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c  x() are availabl
6a4d0 65 2c 0d 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61  e,..** which mea
6a4e0 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65  ns we can use re
6a4f0 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b  ader/writer lock
6a500 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f  s.  When reader/
6a510 77 72 69 74 65 72 20 6c 6f 63 6b 73 0d 0a 2a 2a  writer locks..**
6a520 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c   are used, the l
6a530 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e  ock is placed on
6a540 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20   the same range 
6a550 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73  of bytes that is
6a560 20 75 73 65 64 0d 0a 2a 2a 20 66 6f 72 20 70 72   used..** for pr
6a570 6f 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b  obabilistic lock
6a580 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f  ing in Win95/98/
6a590 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  ME.  Hence, the 
6a5a0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0d 0a  locking scheme..
6a5b0 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20  ** will support 
6a5c0 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39  two or more Win9
6a5d0 35 20 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f  5 readers or two
6a5e0 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72   or more WinNT r
6a5f0 65 61 64 65 72 73 2e 0d 0a 2a 2a 20 42 75 74 20  eaders...** But 
6a600 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20 72  a single Win95 r
6a610 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20  eader will lock 
6a620 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65  out all WinNT re
6a630 61 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e 67  aders and a sing
6a640 6c 65 0d 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61  le..** WinNT rea
6a650 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75  der will lock ou
6a660 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39  t all other Win9
6a670 35 20 72 65 61 64 65 72 73 2e 0d 0a 2a 2a 0d 0a  5 readers...**..
6a680 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
6a690 20 23 64 65 66 69 6e 65 73 20 73 70 65 63 69 66   #defines specif
6a6a0 79 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62  y the range of b
6a6b0 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f  ytes used for lo
6a6c0 63 6b 69 6e 67 2e 0d 0a 2a 2a 20 53 48 41 52 45  cking...** SHARE
6a6d0 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75  D_SIZE is the nu
6a6e0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76  mber of bytes av
6a6f0 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70  ailable in the p
6a700 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 0d 0a  ool from which..
6a710 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65  ** a random byte
6a720 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72   is selected for
6a730 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20   a shared lock. 
6a740 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74   The pool of byt
6a750 65 73 20 66 6f 72 0d 0a 2a 2a 20 73 68 61 72 65  es for..** share
6a760 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61  d locks begins a
6a770 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20  t SHARED_FIRST. 
6a780 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 61 6d  ..**..** The sam
6a790 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  e locking strate
6a7a0 67 79 20 61 6e 64 0d 0a 2a 2a 20 62 79 74 65 20  gy and..** byte 
6a7b0 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 64 20  ranges are used 
6a7c0 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 73 20  for Unix.  This 
6a7d0 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 65 20  leaves open the 
6a7e0 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 68 61  possiblity of ha
6a7f0 76 69 6e 67 0d 0a 2a 2a 20 63 6c 69 65 6e 74 73  ving..** clients
6a800 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54   on win95, winNT
6a810 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74  , and unix all t
6a820 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 61  alking to the sa
6a830 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0d 0a  me shared file..
6a840 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69  ** and all locki
6a850 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
6a860 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65  o do so would re
6a870 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61  quire that samba
6a880 20 28 6f 72 20 77 68 61 74 65 76 65 72 0d 0a 2a   (or whatever..*
6a890 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20  * tool is being 
6a8a0 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68  used for file sh
6a8b0 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74  aring) implement
6a8c0 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c  s locks correctl
6a8d0 79 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 77 69  y between..** wi
6a8e0 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20  ndows and unix. 
6a8f0 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68   I'm guessing th
6a900 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20  at isn't likely 
6a910 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62  to happen, but b
6a920 79 0d 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  y..** using the 
6a930 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e  same locking ran
6a940 67 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61  ge we are at lea
6a950 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70  st open to the p
6a960 6f 73 73 69 62 69 6c 69 74 79 2e 0d 0a 2a 2a 0d  ossibility...**.
6a970 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77  .** Locking in w
6a980 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 74  indows is mandit
6a990 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73 20 72  ory.  For this r
6a9a0 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74  eason, we cannot
6a9b0 20 73 74 6f 72 65 0d 0a 2a 2a 20 61 63 74 75 61   store..** actua
6a9c0 6c 20 64 61 74 61 20 69 6e 20 74 68 65 20 62 79  l data in the by
6a9d0 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  tes used for loc
6a9e0 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65 72  king.  The pager
6a9f0 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 73   never allocates
6aa00 0d 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69  ..** the pages i
6aa10 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69  nvolved in locki
6aa20 6e 67 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53  ng therefore.  S
6aa30 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65  HARED_SIZE is se
6aa40 6c 65 63 74 65 64 20 73 6f 0d 0a 2a 2a 20 74 68  lected so..** th
6aa50 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c  at all locks wil
6aa60 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c  l fit on a singl
6aa70 65 20 70 61 67 65 20 65 76 65 6e 20 61 74 20 74  e page even at t
6aa80 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20  he minimum page 
6aa90 73 69 7a 65 2e 0d 0a 2a 2a 20 50 45 4e 44 49 4e  size...** PENDIN
6aaa0 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20 74  G_BYTE defines t
6aab0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
6aac0 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64  the locks.  By d
6aad0 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42  efault PENDING_B
6aae0 59 54 45 0d 0a 2a 2a 20 69 73 20 73 65 74 20 68  YTE..** is set h
6aaf0 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64  igh so that we d
6ab00 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c  on't have to all
6ab10 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20  ocate an unused 
6ab20 70 61 67 65 20 65 78 63 65 70 74 0d 0a 2a 2a 20  page except..** 
6ab30 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64  for very large d
6ab40 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f  atabases.  But o
6ab50 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74  ne should test t
6ab60 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67  he page skipping
6ab70 20 6c 6f 67 69 63 20 0d 0a 2a 2a 20 62 79 20 73   logic ..** by s
6ab80 65 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42  etting PENDING_B
6ab90 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e  YTE low and runn
6aba0 69 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72  ing the entire r
6abb0 65 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e  egression suite.
6abc0 0d 0a 2a 2a 0d 0a 2a 2a 20 43 68 61 6e 67 69 6e  ..**..** Changin
6abd0 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  g the value of P
6abe0 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 73 75  ENDING_BYTE resu
6abf0 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c 79 20  lts in a subtly 
6ac00 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0d 0a 2a 2a  incompatible..**
6ac10 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44   file format.  D
6ac20 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20  epending on how 
6ac30 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 79  it is changed, y
6ac40 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74  ou might not not
6ac50 69 63 65 0d 0a 2a 2a 20 74 68 65 20 69 6e 63 6f  ice..** the inco
6ac60 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68  mpatibility righ
6ac70 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e  t away, even run
6ac80 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72  ning a full regr
6ac90 65 73 73 69 6f 6e 20 74 65 73 74 2e 0d 0a 2a 2a  ession test...**
6aca0 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   The default loc
6acb0 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47  ation of PENDING
6acc0 5f 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72  _BYTE is the fir
6acd0 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65  st byte past the
6ace0 0d 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72  ..** 1GB boundar
6acf0 79 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 23 69 66 64  y...**..*/..#ifd
6ad00 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
6ad10 53 44 0d 0a 23 20 64 65 66 69 6e 65 20 50 45 4e  SD..# define PEN
6ad20 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 28 30  DING_BYTE     (0
6ad30 78 34 30 30 30 30 30 30 30 29 0d 0a 23 65 6c 73  x40000000)..#els
6ad40 65 0d 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44  e..# define PEND
6ad50 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 73 71  ING_BYTE      sq
6ad60 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
6ad70 0d 0a 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e  ..#endif..#defin
6ad80 65 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  e RESERVED_BYTE 
6ad90 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54      (PENDING_BYT
6ada0 45 2b 31 29 0d 0a 23 64 65 66 69 6e 65 20 53 48  E+1)..#define SH
6adb0 41 52 45 44 5f 46 49 52 53 54 20 20 20 20 20 20  ARED_FIRST      
6adc0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29  (PENDING_BYTE+2)
6add0 0d 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44  ..#define SHARED
6ade0 5f 53 49 5a 45 20 20 20 20 20 20 20 35 31 30 0d  _SIZE       510.
6adf0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 61 70 70 65  .../*..** Wrappe
6ae00 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63  r around OS spec
6ae10 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ific sqlite3_os_
6ae20 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e  init() function.
6ae30 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
6ae40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6ae50 4f 73 49 6e 69 74 28 76 6f 69 64 29 3b 0d 0a 0d  OsInit(void);...
6ae60 0a 2f 2a 20 0d 0a 2a 2a 20 46 75 6e 63 74 69 6f  ./* ..** Functio
6ae70 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  ns for accessing
6ae80 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
6ae90 74 68 6f 64 73 20 0d 0a 2a 2f 0d 0a 53 51 4c 49  thods ..*/..SQLI
6aea0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6aeb0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71  qlite3OsClose(sq
6aec0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0d 0a 53  lite3_file*);..S
6aed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6aee0 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  t sqlite3OsRead(
6aef0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76  sqlite3_file*, v
6af00 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69  oid*, int amt, i
6af10 36 34 20 6f 66 66 73 65 74 29 3b 0d 0a 53 51 4c  64 offset);..SQL
6af20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6af30 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73  sqlite3OsWrite(s
6af40 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f  qlite3_file*, co
6af50 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61  nst void*, int a
6af60 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b  mt, i64 offset);
6af70 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6af80 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72   int sqlite3OsTr
6af90 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
6afa0 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b  ile*, i64 size);
6afb0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6afc0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79   int sqlite3OsSy
6afd0 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  nc(sqlite3_file*
6afe0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
6aff0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6b000 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
6b010 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34  lite3_file*, i64
6b020 20 2a 70 53 69 7a 65 29 3b 0d 0a 53 51 4c 49 54   *pSize);..SQLIT
6b030 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b040 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69  lite3OsLock(sqli
6b050 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
6b060 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6b070 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e   int sqlite3OsUn
6b080 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
6b090 65 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54  e*, int);..SQLIT
6b0a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b0b0 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65  lite3OsCheckRese
6b0c0 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
6b0d0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
6b0e0 70 52 65 73 4f 75 74 29 3b 0d 0a 53 51 4c 49 54  pResOut);..SQLIT
6b0f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b100 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
6b110 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ol(sqlite3_file*
6b120 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0d 0a 53 51  ,int,void*);..SQ
6b130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6b140 64 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  d sqlite3OsFileC
6b150 6f 6e 74 72 6f 6c 48 69 6e 74 28 73 71 6c 69 74  ontrolHint(sqlit
6b160 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69  e3_file*,int,voi
6b170 64 2a 29 3b 0d 0a 23 64 65 66 69 6e 65 20 53 51  d*);..#define SQ
6b180 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e  LITE_FCNTL_DB_UN
6b190 43 48 41 4e 47 45 44 20 30 78 63 61 30 39 33 66  CHANGED 0xca093f
6b1a0 61 30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  a0..SQLITE_PRIVA
6b1b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6b1c0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
6b1d0 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0d 0a 53  e3_file *id);..S
6b1e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6b1f0 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  t sqlite3OsDevic
6b200 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
6b210 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
6b220 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  d);..SQLITE_PRIV
6b230 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
6b240 73 53 68 6d 4d 61 70 28 73 71 6c 69 74 65 33 5f  sShmMap(sqlite3_
6b250 66 69 6c 65 20 2a 2c 69 6e 74 2c 69 6e 74 2c 69  file *,int,int,i
6b260 6e 74 2c 76 6f 69 64 20 76 6f 6c 61 74 69 6c 65  nt,void volatile
6b270 20 2a 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52   **);..SQLITE_PR
6b280 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6b290 33 4f 73 53 68 6d 4c 6f 63 6b 28 73 71 6c 69 74  3OsShmLock(sqlit
6b2a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
6b2b0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51  , int, int);..SQ
6b2c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6b2d0 64 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 42 61  d sqlite3OsShmBa
6b2e0 72 72 69 65 72 28 73 71 6c 69 74 65 33 5f 66 69  rrier(sqlite3_fi
6b2f0 6c 65 20 2a 69 64 29 3b 0d 0a 53 51 4c 49 54 45  le *id);..SQLITE
6b300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6b310 69 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 73  ite3OsShmUnmap(s
6b320 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
6b330 20 69 6e 74 29 3b 0d 0a 0d 0a 0d 0a 2f 2a 20 0d   int);....../* .
6b340 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f  .** Functions fo
6b350 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69  r accessing sqli
6b360 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20  te3_vfs methods 
6b370 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
6b380 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6b390 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  OsOpen(sqlite3_v
6b3a0 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  fs *, const char
6b3b0 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   *, sqlite3_file
6b3c0 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0d  *, int, int *);.
6b3d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b3e0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  int sqlite3OsDel
6b3f0 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ete(sqlite3_vfs 
6b400 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
6b410 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
6b420 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6b430 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69 74  e3OsAccess(sqlit
6b440 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
6b450 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  char *, int, int
6b460 20 2a 70 52 65 73 4f 75 74 29 3b 0d 0a 53 51 4c   *pResOut);..SQL
6b470 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6b480 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74  sqlite3OsFullPat
6b490 68 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  hname(sqlite3_vf
6b4a0 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
6b4b0 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
6b4c0 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
6b4d0 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
6b4e0 53 49 4f 4e 0d 0a 53 51 4c 49 54 45 5f 50 52 49  SION..SQLITE_PRI
6b4f0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6b500 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74  e3OsDlOpen(sqlit
6b510 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
6b520 63 68 61 72 20 2a 29 3b 0d 0a 53 51 4c 49 54 45  char *);..SQLITE
6b530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6b540 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73  lite3OsDlError(s
6b550 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
6b560 74 2c 20 63 68 61 72 20 2a 29 3b 0d 0a 53 51 4c  t, char *);..SQL
6b570 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b580 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79   (*sqlite3OsDlSy
6b590 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  m(sqlite3_vfs *,
6b5a0 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63   void *, const c
6b5b0 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0d 0a  har *))(void);..
6b5c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6b5d0 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43  oid sqlite3OsDlC
6b5e0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lose(sqlite3_vfs
6b5f0 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0d 0a 23 65   *, void *);..#e
6b600 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
6b610 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
6b620 4f 4e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ON */..SQLITE_PR
6b630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6b640 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
6b650 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
6b660 2c 20 63 68 61 72 20 2a 29 3b 0d 0a 53 51 4c 49  , char *);..SQLI
6b670 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6b680 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71  qlite3OsSleep(sq
6b690 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
6b6a0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
6b6b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6b6c0 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34  CurrentTimeInt64
6b6d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
6b6e0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 3b  sqlite3_int64*);
6b6f0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65  ..../*..** Conve
6b700 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73  nience functions
6b710 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64   for opening and
6b720 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75   closing files u
6b730 73 69 6e 67 20 0d 0a 2a 2a 20 73 71 6c 69 74 65  sing ..** sqlite
6b740 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62  3_malloc() to ob
6b750 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74  tain space for t
6b760 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73  he file-handle s
6b770 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 53  tructure...*/..S
6b780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6b790 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d  t sqlite3OsOpenM
6b7a0 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66  alloc(sqlite3_vf
6b7b0 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
6b7c0 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  *, sqlite3_file 
6b7d0 2a 2a 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0d 0a  **, int,int*);..
6b7e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6b7f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
6b800 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69  eFree(sqlite3_fi
6b810 6c 65 20 2a 29 3b 0d 0a 0d 0a 23 65 6e 64 69 66  le *);....#endif
6b820 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48   /* _SQLITE_OS_H
6b830 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  _ */..../*******
6b840 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
6b850 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
6b860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b880 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
6b890 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
6b8a0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
6b8b0 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
6b8c0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
6b8d0 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
6b8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
6b8f0 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65  e mutex.h in the
6b900 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
6b910 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
6b920 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
6b930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
6b940 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a   file mutex.h **
6b950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
6b980 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
6b990 38 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  8..**..** The au
6b9a0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
6b9b0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
6b9c0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
6b9d0 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
6b9e0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
6b9f0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
6ba00 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
6ba10 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
6ba20 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
6ba30 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
6ba40 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
6ba50 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
6ba60 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
6ba70 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
6ba80 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
6ba90 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
6baa0 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
6bab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6bae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6baf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  *********..**..*
6bb00 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
6bb10 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  ains the common 
6bb20 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d  header for all m
6bb30 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
6bb40 69 6f 6e 73 2e 0d 0a 2a 2a 20 54 68 65 20 73 71  ions...** The sq
6bb50 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72  liteInt.h header
6bb60 20 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20   #includes this 
6bb70 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20  file so that it 
6bb80 69 73 20 61 76 61 69 6c 61 62 6c 65 0d 0a 2a 2a  is available..**
6bb90 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   to all source f
6bba0 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20  iles.  We break 
6bbb0 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66  it out in an eff
6bbc0 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20  ort to keep the 
6bbd0 63 6f 64 65 0d 0a 2a 2a 20 62 65 74 74 65 72 20  code..** better 
6bbe0 6f 72 67 61 6e 69 7a 65 64 2e 0d 0a 2a 2a 0d 0a  organized...**..
6bbf0 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65  ** NOTE:  source
6bc00 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e   files should *n
6bc10 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69  ot* #include thi
6bc20 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69  s header file di
6bc30 72 65 63 74 6c 79 2e 0d 0a 2a 2a 20 53 6f 75 72  rectly...** Sour
6bc40 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20  ce files should 
6bc50 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c  #include the sql
6bc60 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e  iteInt.h file an
6bc70 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0d  d let that file.
6bc80 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 73  .** include this
6bc90 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 2e   one indirectly.
6bca0 0d 0a 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ..*/....../*..**
6bcb0 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74   Figure out what
6bcc0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
6bcd0 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68  code to use.  Th
6bce0 65 20 63 68 6f 69 63 65 73 20 61 72 65 0d 0a 2a  e choices are..*
6bcf0 2a 0d 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  *..**   SQLITE_M
6bd00 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20  UTEX_OMIT       
6bd10 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63    No mutex logic
6bd20 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62  .  Not even stub
6bd30 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 20 20 20 20  s.  The..**     
6bd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bd50 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20          mutexes 
6bd60 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e  implemention can
6bd70 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65  not be overridde
6bd80 6e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
6bd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bda0 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e    at start-time.
6bdb0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 51 4c 49 54  ..**..**   SQLIT
6bdc0 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20  E_MUTEX_NOOP    
6bdd0 20 20 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d       For single-
6bde0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
6bdf0 74 69 6f 6e 73 2e 20 20 4e 6f 0d 0a 2a 2a 20 20  tions.  No..**  
6be00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6be10 20 20 20 20 20 20 20 20 20 20 20 6d 75 74 75 61             mutua
6be20 6c 20 65 78 63 6c 75 73 69 6f 6e 20 69 73 20 70  l exclusion is p
6be30 72 6f 76 69 64 65 64 2e 20 20 42 75 74 20 74 68  rovided.  But th
6be40 69 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  is..**          
6be50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6be60 20 20 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f     implementatio
6be70 6e 20 63 61 6e 20 62 65 20 6f 76 65 72 72 69 64  n can be overrid
6be80 64 65 6e 20 61 74 0d 0a 2a 2a 20 20 20 20 20 20  den at..**      
6be90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bea0 20 20 20 20 20 20 20 73 74 61 72 74 2d 74 69 6d         start-tim
6beb0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 51 4c  e...**..**   SQL
6bec0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
6bed0 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69  DS     For multi
6bee0 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
6bef0 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 2e 0d  ations on Unix..
6bf00 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 51 4c 49 54 45  .**..**   SQLITE
6bf10 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 20 20  _MUTEX_W32      
6bf20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68      For multi-th
6bf30 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
6bf40 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0d 0a 2a  ons on Win32...*
6bf50 2a 0d 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  *..**   SQLITE_M
6bf60 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20  UTEX_OS2        
6bf70 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
6bf80 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
6bf90 73 20 6f 6e 20 4f 53 2f 32 2e 0d 0a 2a 2f 0d 0a  s on OS/2...*/..
6bfa0 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45  #if !SQLITE_THRE
6bfb0 41 44 53 41 46 45 0d 0a 23 20 64 65 66 69 6e 65  ADSAFE..# define
6bfc0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
6bfd0 49 54 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20  IT..#endif..#if 
6bfe0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
6bff0 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  E && !defined(SQ
6c000 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29  LITE_MUTEX_NOOP)
6c010 0d 0a 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f  ..#  if SQLITE_O
6c020 53 5f 55 4e 49 58 0d 0a 23 20 20 20 20 64 65 66  S_UNIX..#    def
6c030 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
6c040 5f 50 54 48 52 45 41 44 53 0d 0a 23 20 20 65 6c  _PTHREADS..#  el
6c050 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
6c060 0d 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  ..#    define SQ
6c070 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0d 0a  LITE_MUTEX_W32..
6c080 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f  #  elif SQLITE_O
6c090 53 5f 4f 53 32 0d 0a 23 20 20 20 20 64 65 66 69  S_OS2..#    defi
6c0a0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
6c0b0 4f 53 32 0d 0a 23 20 20 65 6c 73 65 0d 0a 23 20  OS2..#  else..# 
6c0c0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
6c0d0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0d 0a 23 20 20  _MUTEX_NOOP..#  
6c0e0 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66 0d 0a 0d  endif..#endif...
6c0f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
6c100 55 54 45 58 5f 4f 4d 49 54 0d 0a 2f 2a 0d 0a 2a  UTEX_OMIT../*..*
6c110 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e  * If this is a n
6c120 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74  o-op implementat
6c130 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65  ion, implement e
6c140 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63  verything as mac
6c150 72 6f 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  ros...*/..#defin
6c160 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
6c170 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71  alloc(X)    ((sq
6c180 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0d  lite3_mutex*)8).
6c190 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
6c1a0 5f 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0d 0a  _mutex_free(X)..
6c1b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
6c1c0 6d 75 74 65 78 5f 65 6e 74 65 72 28 58 29 20 20  mutex_enter(X)  
6c1d0 20 20 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    ..#define sqli
6c1e0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 29  te3_mutex_try(X)
6c1f0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0d        SQLITE_OK.
6c200 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
6c210 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 20  _mutex_leave(X) 
6c220 20 20 20 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c     ..#define sql
6c230 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
6c240 58 29 20 20 20 20 20 28 28 76 6f 69 64 29 28 58  X)     ((void)(X
6c250 29 2c 31 29 0d 0a 23 64 65 66 69 6e 65 20 73 71  ),1)..#define sq
6c260 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
6c270 65 6c 64 28 58 29 20 20 28 28 76 6f 69 64 29 28  eld(X)  ((void)(
6c280 58 29 2c 31 29 0d 0a 23 64 65 66 69 6e 65 20 73  X),1)..#define s
6c290 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
6c2a0 28 58 29 20 20 20 20 20 20 28 28 73 71 6c 69 74  (X)      ((sqlit
6c2b0 65 33 5f 6d 75 74 65 78 2a 29 38 29 0d 0a 23 64  e3_mutex*)8)..#d
6c2c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74  efine sqlite3Mut
6c2d0 65 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 20  exInit()        
6c2e0 53 51 4c 49 54 45 5f 4f 4b 0d 0a 23 64 65 66 69  SQLITE_OK..#defi
6c2f0 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45  ne sqlite3MutexE
6c300 6e 64 28 29 0d 0a 23 64 65 66 69 6e 65 20 4d 55  nd()..#define MU
6c310 54 45 58 5f 4c 4f 47 49 43 28 58 29 0d 0a 23 65  TEX_LOGIC(X)..#e
6c320 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20 4d 55 54  lse..#define MUT
6c330 45 58 5f 4c 4f 47 49 43 28 58 29 20 20 20 20 20  EX_LOGIC(X)     
6c340 20 20 20 20 20 20 20 58 0d 0a 23 65 6e 64 69 66         X..#endif
6c350 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
6c360 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a  TE_MUTEX_OMIT) *
6c370 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
6c380 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
6c390 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.h ************
6c3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c3c0 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
6c3d0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
6c3e0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
6c3f0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
6c400 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
6c410 2a 2a 2a 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  ****/....../*..*
6c420 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20  * Each database 
6c430 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73  file to be acces
6c440 73 65 64 20 62 79 20 74 68 65 20 73 79 73 74 65  sed by the syste
6c450 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  m is an instance
6c460 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ..** of the foll
6c470 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
6c480 20 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 6d    There are norm
6c490 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 73  ally two of thes
6c4a0 65 20 73 74 72 75 63 74 75 72 65 73 0d 0a 2a 2a  e structures..**
6c4b0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61   in the sqlite.a
6c4c0 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62  Db[] array.  aDb
6c4d0 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20  [0] is the main 
6c4e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
6c4f0 64 0d 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20  d..** aDb[1] is 
6c500 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
6c510 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74  e used to hold t
6c520 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
6c530 20 20 41 64 64 69 74 69 6f 6e 61 6c 0d 0a 2a 2a    Additional..**
6c540 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 20 62   databases may b
6c550 65 20 61 74 74 61 63 68 65 64 2e 0d 0a 2a 2f 0d  e attached...*/.
6c560 0a 73 74 72 75 63 74 20 44 62 20 7b 0d 0a 20 20  .struct Db {..  
6c570 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
6c580 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
6c590 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
6c5a0 0d 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20  ..  Btree *pBt; 
6c5b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6c5c0 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B*Tree structure
6c5d0 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61   for this databa
6c5e0 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 75 38  se file */..  u8
6c5f0 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20   inTrans;       
6c600 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69     /* 0: not wri
6c610 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73  table.  1: Trans
6c620 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63  action.  2: Chec
6c630 6b 70 6f 69 6e 74 20 2a 2f 0d 0a 20 20 75 38 20  kpoint */..  u8 
6c640 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20  safety_level;   
6c650 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73    /* How aggress
6c660 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64  ive at syncing d
6c670 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0d 0a  ata to disk */..
6c680 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
6c690 61 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  a;     /* Pointe
6c6a0 72 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63  r to database sc
6c6b0 68 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73  hema (possibly s
6c6c0 68 61 72 65 64 29 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  hared) */..};...
6c6d0 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ./*..** An insta
6c6e0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
6c6f0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
6c700 74 6f 72 65 73 20 61 20 64 61 74 61 62 61 73 65  tores a database
6c710 20 73 63 68 65 6d 61 2e 0d 0a 2a 2a 0d 0a 2a 2a   schema...**..**
6c720 20 4d 6f 73 74 20 53 63 68 65 6d 61 20 6f 62 6a   Most Schema obj
6c730 65 63 74 73 20 61 72 65 20 61 73 73 6f 63 69 61  ects are associa
6c740 74 65 64 20 77 69 74 68 20 61 20 42 74 72 65 65  ted with a Btree
6c750 2e 20 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e  .  The exception
6c760 20 69 73 0d 0a 2a 2a 20 74 68 65 20 53 63 68 65   is..** the Sche
6c770 6d 61 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20  ma for the TEMP 
6c780 64 61 74 61 62 61 65 73 20 28 73 71 6c 69 74 65  databaes (sqlite
6c790 33 2e 61 44 62 5b 31 5d 29 20 77 68 69 63 68 20  3.aDb[1]) which 
6c7a0 69 73 20 66 72 65 65 2d 73 74 61 6e 64 69 6e 67  is free-standing
6c7b0 2e 0d 0a 2a 2a 20 49 6e 20 73 68 61 72 65 64 20  ...** In shared 
6c7c0 63 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 73 69  cache mode, a si
6c7d0 6e 67 6c 65 20 53 63 68 65 6d 61 20 6f 62 6a 65  ngle Schema obje
6c7e0 63 74 20 63 61 6e 20 62 65 20 73 68 61 72 65 64  ct can be shared
6c7f0 20 62 79 20 6d 75 6c 74 69 70 6c 65 0d 0a 2a 2a   by multiple..**
6c800 20 42 74 72 65 65 73 20 74 68 61 74 20 72 65 66   Btrees that ref
6c810 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 75  er to the same u
6c820 6e 64 65 72 6c 79 69 6e 67 20 42 74 53 68 61 72  nderlying BtShar
6c830 65 64 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 20 0d  ed object...** .
6c840 0a 2a 2a 20 53 63 68 65 6d 61 20 6f 62 6a 65 63  .** Schema objec
6c850 74 73 20 61 72 65 20 61 75 74 6f 6d 61 74 69 63  ts are automatic
6c860 61 6c 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64  ally deallocated
6c870 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 42   when the last B
6c880 74 72 65 65 20 74 68 61 74 0d 0a 2a 2a 20 72 65  tree that..** re
6c890 66 65 72 65 6e 63 65 73 20 74 68 65 6d 20 69 73  ferences them is
6c8a0 20 64 65 73 74 72 6f 79 65 64 2e 20 20 20 54 68   destroyed.   Th
6c8b0 65 20 54 45 4d 50 20 53 63 68 65 6d 61 20 69 73  e TEMP Schema is
6c8c0 20 6d 61 6e 75 61 6c 6c 79 20 66 72 65 65 64 20   manually freed 
6c8d0 62 79 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63  by..** sqlite3_c
6c8e0 6c 6f 73 65 28 29 2e 0d 0a 2a 0d 0a 2a 2a 20 41  lose()...*..** A
6c8f0 20 74 68 72 65 61 64 20 6d 75 73 74 20 62 65 20   thread must be 
6c900 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78 20  holding a mutex 
6c910 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
6c920 64 69 6e 67 20 42 74 72 65 65 20 69 6e 20 6f 72  ding Btree in or
6c930 64 65 72 0d 0a 2a 2a 20 74 6f 20 61 63 63 65 73  der..** to acces
6c940 73 20 53 63 68 65 6d 61 20 63 6f 6e 74 65 6e 74  s Schema content
6c950 2e 20 20 54 68 69 73 20 69 6d 70 6c 69 65 73 20  .  This implies 
6c960 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20  that the thread 
6c970 6d 75 73 74 20 61 6c 73 6f 20 62 65 0d 0a 2a 2a  must also be..**
6c980 20 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78   holding a mutex
6c990 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 33 20   on the sqlite3 
6c9a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74  connection point
6c9b0 65 72 20 74 68 61 74 20 6f 77 6e 73 20 74 68 65  er that owns the
6c9c0 20 42 74 72 65 65 2e 0d 0a 2a 2a 20 46 6f 72 20   Btree...** For 
6c9d0 61 20 54 45 4d 50 20 53 63 68 65 6d 61 2c 20 6f  a TEMP Schema, o
6c9e0 6e 6c 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  nly the connecti
6c9f0 6f 6e 20 6d 75 74 65 78 20 69 73 20 72 65 71 75  on mutex is requ
6ca00 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  ired...*/..struc
6ca10 74 20 53 63 68 65 6d 61 20 7b 0d 0a 20 20 69 6e  t Schema {..  in
6ca20 74 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b  t schema_cookie;
6ca30 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73     /* Database s
6ca40 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75  chema version nu
6ca50 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69  mber for this fi
6ca60 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 47 65  le */..  int iGe
6ca70 6e 65 72 61 74 69 6f 6e 3b 20 20 20 20 20 2f 2a  neration;     /*
6ca80 20 47 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e   Generation coun
6ca90 74 65 72 2e 20 20 49 6e 63 72 65 6d 65 6e 74 65  ter.  Incremente
6caa0 64 20 77 69 74 68 20 65 61 63 68 20 63 68 61 6e  d with each chan
6cab0 67 65 20 2a 2f 0d 0a 20 20 48 61 73 68 20 74 62  ge */..  Hash tb
6cac0 6c 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a  lHash;        /*
6cad0 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65   All tables inde
6cae0 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0d 0a  xed by name */..
6caf0 20 20 48 61 73 68 20 69 64 78 48 61 73 68 3b 20    Hash idxHash; 
6cb00 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e         /* All (n
6cb10 61 6d 65 64 29 20 69 6e 64 69 63 65 73 20 69 6e  amed) indices in
6cb20 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f  dexed by name */
6cb30 0d 0a 20 20 48 61 73 68 20 74 72 69 67 48 61 73  ..  Hash trigHas
6cb40 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  h;       /* All 
6cb50 74 72 69 67 67 65 72 73 20 69 6e 64 65 78 65 64  triggers indexed
6cb60 20 62 79 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 48   by name */..  H
6cb70 61 73 68 20 66 6b 65 79 48 61 73 68 3b 20 20 20  ash fkeyHash;   
6cb80 20 20 20 20 2f 2a 20 41 6c 6c 20 66 6f 72 65 69      /* All forei
6cb90 67 6e 20 6b 65 79 73 20 62 79 20 72 65 66 65 72  gn keys by refer
6cba0 65 6e 63 65 64 20 74 61 62 6c 65 20 6e 61 6d 65  enced table name
6cbb0 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 53   */..  Table *pS
6cbc0 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54  eqTab;      /* T
6cbd0 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
6cbe0 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79  ce table used by
6cbf0 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
6cc00 2f 0d 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72  /..  u8 file_for
6cc10 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68  mat;      /* Sch
6cc20 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69  ema format versi
6cc30 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  on for this file
6cc40 20 2a 2f 0d 0a 20 20 75 38 20 65 6e 63 3b 20 20   */..  u8 enc;  
6cc50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6cc60 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  ext encoding use
6cc70 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61  d by this databa
6cc80 73 65 20 2a 2f 0d 0a 20 20 75 31 36 20 66 6c 61  se */..  u16 fla
6cc90 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs;           /*
6cca0 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
6ccb0 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65  d with this sche
6ccc0 6d 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 61 63  ma */..  int cac
6ccd0 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a  he_size;      /*
6cce0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
6ccf0 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63   to use in the c
6cd00 61 63 68 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f  ache */..};..../
6cd10 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72  *..** These macr
6cd20 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  os can be used t
6cd30 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 20  o test, set, or 
6cd40 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74 68  clear bits in th
6cd50 65 20 0d 0a 2a 2a 20 44 62 2e 70 53 63 68 65 6d  e ..** Db.pSchem
6cd60 61 2d 3e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0d  a->flags field..
6cd70 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 44 62 48  .*/..#define DbH
6cd80 61 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50  asProperty(D,I,P
6cd90 29 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62  )     (((D)->aDb
6cda0 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61  [I].pSchema->fla
6cdb0 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0d 0a 23  gs&(P))==(P))..#
6cdc0 64 65 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50  define DbHasAnyP
6cdd0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
6cde0 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53  (((D)->aDb[I].pS
6cdf0 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29  chema->flags&(P)
6ce00 29 21 3d 30 29 0d 0a 23 64 65 66 69 6e 65 20 44  )!=0)..#define D
6ce10 62 53 65 74 50 72 6f 70 65 72 74 79 28 44 2c 49  bSetProperty(D,I
6ce20 2c 50 29 20 20 20 20 20 28 44 29 2d 3e 61 44 62  ,P)     (D)->aDb
6ce30 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61  [I].pSchema->fla
6ce40 67 73 7c 3d 28 50 29 0d 0a 23 64 65 66 69 6e 65  gs|=(P)..#define
6ce50 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79   DbClearProperty
6ce60 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d 3e 61  (D,I,P)   (D)->a
6ce70 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66  Db[I].pSchema->f
6ce80 6c 61 67 73 26 3d 7e 28 50 29 0d 0a 0d 0a 2f 2a  lags&=~(P)..../*
6ce90 0d 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  ..** Allowed val
6cea0 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 70  ues for the DB.p
6ceb0 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 66 69  Schema->flags fi
6cec0 65 6c 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  eld...**..** The
6ced0 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
6cee0 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 66 74   flag is set aft
6cef0 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
6cf00 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 0d  schema has been.
6cf10 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f 20 69 6e  .** read into in
6cf20 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c  ternal hash tabl
6cf30 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 42 5f 55  es...**..** DB_U
6cf40 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 61 6e  nresetViews mean
6cf50 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f  s that one or mo
6cf60 72 65 20 76 69 65 77 73 20 68 61 76 65 20 63 6f  re views have co
6cf70 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74 0d  lumn names that.
6cf80 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69  .** have been fi
6cf90 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68  lled out.  If th
6cfa0 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
6cfb0 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e  , these column n
6cfc0 61 6d 65 73 20 6d 69 67 68 74 0d 0a 2a 2a 20 63  ames might..** c
6cfd0 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68  hanges and so th
6cfe0 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64  e view will need
6cff0 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0d 0a 2a   to be reset...*
6d000 2f 0d 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63  /..#define DB_Sc
6d010 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78  hemaLoaded    0x
6d020 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68  0001  /* The sch
6d030 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  ema has been loa
6d040 64 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ded */..#define 
6d050 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20  DB_UnresetViews 
6d060 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f     0x0002  /* So
6d070 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65  me views have de
6d080 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  fined column nam
6d090 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 44  es */..#define D
6d0a0 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20  B_Empty         
6d0b0 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65    0x0004  /* The
6d0c0 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28   file is empty (
6d0d0 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20  length 0 bytes) 
6d0e0 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  */..../*..** The
6d0f0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65   number of diffe
6d100 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68  rent kinds of th
6d110 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 65  ings that can be
6d120 20 6c 69 6d 69 74 65 64 0d 0a 2a 2a 20 75 73 69   limited..** usi
6d130 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c  ng the sqlite3_l
6d140 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  imit() interface
6d150 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ...*/..#define S
6d160 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53  QLITE_N_LIMIT (S
6d170 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47  QLITE_LIMIT_TRIG
6d180 47 45 52 5f 44 45 50 54 48 2b 31 29 0d 0a 0d 0a  GER_DEPTH+1)....
6d190 2f 2a 0d 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65  /*..** Lookaside
6d1a0 20 6d 61 6c 6c 6f 63 20 69 73 20 61 20 73 65 74   malloc is a set
6d1b0 20 6f 66 20 66 69 78 65 64 2d 73 69 7a 65 20 62   of fixed-size b
6d1c0 75 66 66 65 72 73 20 74 68 61 74 20 63 61 6e 20  uffers that can 
6d1d0 62 65 20 75 73 65 64 0d 0a 2a 2a 20 74 6f 20 73  be used..** to s
6d1e0 61 74 69 73 66 79 20 73 6d 61 6c 6c 20 74 72 61  atisfy small tra
6d1f0 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 79 20 61 6c  nsient memory al
6d200 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74  location request
6d210 73 20 66 6f 72 20 6f 62 6a 65 63 74 73 0d 0a 2a  s for objects..*
6d220 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
6d230 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
6d240 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
6d250 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 6f 66 0d  on.  The use of.
6d260 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61  .** lookaside ma
6d270 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 61 20  lloc provides a 
6d280 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 72 66  significant perf
6d290 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 65 6d  ormance enhancem
6d2a0 65 6e 74 0d 0a 2a 2a 20 28 61 70 70 72 6f 78 20  ent..** (approx 
6d2b0 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 67  10%) by avoiding
6d2c0 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f 63   numerous malloc
6d2d0 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 77  /free requests w
6d2e0 68 69 6c 65 20 70 61 72 73 69 6e 67 0d 0a 2a 2a  hile parsing..**
6d2f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
6d300 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 4c 6f 6f  ..**..** The Loo
6d310 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 65  kaside structure
6d320 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61   holds configura
6d330 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
6d340 20 61 62 6f 75 74 20 74 68 65 0d 0a 2a 2a 20 6c   about the..** l
6d350 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20  ookaside malloc 
6d360 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68  subsystem.  Each
6d370 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   available memor
6d380 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0d  y allocation in.
6d390 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  .** the lookasid
6d3a0 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 73  e subsystem is s
6d3b0 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65  tored on a linke
6d3c0 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73  d list of Lookas
6d3d0 69 64 65 53 6c 6f 74 0d 0a 2a 2a 20 6f 62 6a 65  ideSlot..** obje
6d3e0 63 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4c 6f 6f  cts...**..** Loo
6d3f0 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f  kaside allocatio
6d400 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f  ns are only allo
6d410 77 65 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20  wed for objects 
6d420 74 68 61 74 20 61 72 65 20 61 73 73 6f 63 69 61  that are associa
6d430 74 65 64 0d 0a 2a 2a 20 77 69 74 68 20 61 20 70  ted..** with a p
6d440 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
6d450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  se connection.  
6d460 48 65 6e 63 65 2c 20 73 63 68 65 6d 61 20 69 6e  Hence, schema in
6d470 66 6f 72 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  formation cannot
6d480 0d 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 20 69  ..** be stored i
6d490 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 62 65 63 61  n lookaside beca
6d4a0 75 73 65 20 69 6e 20 73 68 61 72 65 64 20 63 61  use in shared ca
6d4b0 63 68 65 20 6d 6f 64 65 20 74 68 65 20 73 63 68  che mode the sch
6d4c0 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0d  ema information.
6d4d0 0a 2a 2a 20 69 73 20 73 68 61 72 65 64 20 62 79  .** is shared by
6d4e0 20 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61   multiple databa
6d4f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20  se connections. 
6d500 20 54 68 65 72 65 66 6f 72 65 2c 20 77 68 69 6c   Therefore, whil
6d510 65 20 70 61 72 73 69 6e 67 0d 0a 2a 2a 20 73 63  e parsing..** sc
6d520 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
6d530 2c 20 74 68 65 20 4c 6f 6f 6b 61 73 69 64 65 2e  , the Lookaside.
6d540 62 45 6e 61 62 6c 65 64 20 66 6c 61 67 20 69 73  bEnabled flag is
6d550 20 63 6c 65 61 72 65 64 20 73 6f 20 74 68 61 74   cleared so that
6d560 0d 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 61  ..** lookaside a
6d570 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 6e  llocations are n
6d580 6f 74 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74  ot used to const
6d590 72 75 63 74 20 74 68 65 20 73 63 68 65 6d 61 20  ruct the schema 
6d5a0 6f 62 6a 65 63 74 73 2e 0d 0a 2a 2f 0d 0a 73 74  objects...*/..st
6d5b0 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 7b  ruct Lookaside {
6d5c0 0d 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20  ..  u16 sz;     
6d5d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6d5e0 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 66  ize of each buff
6d5f0 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  er in bytes */..
6d600 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20    u8 bEnabled;  
6d610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
6d620 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65  se to disable ne
6d630 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f  w lookaside allo
6d640 63 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 75 38  cations */..  u8
6d650 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20   bMalloced;     
6d660 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6d670 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64   pStart obtained
6d680 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
6d690 6c 6c 6f 63 28 29 20 2a 2f 0d 0a 20 20 69 6e 74  lloc() */..  int
6d6a0 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20   nOut;          
6d6b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6d6c0 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 6e  f buffers curren
6d6d0 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20  tly checked out 
6d6e0 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b  */..  int mxOut;
6d6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d700 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   Highwater mark 
6d710 66 6f 72 20 6e 4f 75 74 20 2a 2f 0d 0a 20 20 69  for nOut */..  i
6d720 6e 74 20 61 6e 53 74 61 74 5b 33 5d 3b 20 20 20  nt anStat[3];   
6d730 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 68 69 74         /* 0: hit
6d740 73 2e 20 20 31 3a 20 73 69 7a 65 20 6d 69 73 73  s.  1: size miss
6d750 65 73 2e 20 20 32 3a 20 66 75 6c 6c 20 6d 69 73  es.  2: full mis
6d760 73 65 73 20 2a 2f 0d 0a 20 20 4c 6f 6f 6b 61 73  ses */..  Lookas
6d770 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20  ideSlot *pFree; 
6d780 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61    /* List of ava
6d790 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a  ilable buffers *
6d7a0 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72  /..  void *pStar
6d7b0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
6d7c0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76  First byte of av
6d7d0 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73  ailable memory s
6d7e0 70 61 63 65 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  pace */..  void 
6d7f0 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20  *pEnd;          
6d800 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
6d810 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61   past end of ava
6d820 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0d  ilable space */.
6d830 0a 7d 3b 0d 0a 73 74 72 75 63 74 20 4c 6f 6f 6b  .};..struct Look
6d840 61 73 69 64 65 53 6c 6f 74 20 7b 0d 0a 20 20 4c  asideSlot {..  L
6d850 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e  ookasideSlot *pN
6d860 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20  ext;    /* Next 
6d870 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69  buffer in the li
6d880 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65  st of free buffe
6d890 72 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  rs */..};..../*.
6d8a0 0a 2a 2a 20 41 20 68 61 73 68 20 74 61 62 6c 65  .** A hash table
6d8b0 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 64 65   for function de
6d8c0 66 69 6e 69 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a  finitions...**..
6d8d0 2a 2a 20 48 61 73 68 20 65 61 63 68 20 46 75 6e  ** Hash each Fun
6d8e0 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
6d8f0 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 46  nto one of the F
6d900 75 6e 63 44 65 66 48 61 73 68 2e 61 5b 5d 20 73  uncDefHash.a[] s
6d910 6c 6f 74 73 2e 0d 0a 2a 2a 20 43 6f 6c 6c 69 73  lots...** Collis
6d920 69 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 65 20  ions are on the 
6d930 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 63 68  FuncDef.pHash ch
6d940 61 69 6e 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74  ain...*/..struct
6d950 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b 0d 0a   FuncDefHash {..
6d960 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 5d    FuncDef *a[23]
6d970 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20  ;       /* Hash 
6d980 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69  table for functi
6d990 6f 6e 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  ons */..};..../*
6d9a0 0d 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61  ..** Each databa
6d9b0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
6d9c0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
6d9d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
6d9e0 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ructure...**..**
6d9f0 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74   The sqlite.last
6da00 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74 68  Rowid records th
6da10 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  e last insert ro
6da20 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62 79  wid generated by
6da30 20 61 6e 0d 0a 2a 2a 20 69 6e 73 65 72 74 20 73   an..** insert s
6da40 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72  tatement.  Inser
6da50 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e  ts on views do n
6da60 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61  ot affect its va
6da70 6c 75 65 2e 20 20 45 61 63 68 0d 0a 2a 2a 20 74  lue.  Each..** t
6da80 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f  rigger has its o
6da90 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74  wn context, so t
6daa0 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61  hat lastRowid ca
6dab0 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73  n be updated ins
6dac0 69 64 65 0d 0a 2a 2a 20 74 72 69 67 67 65 72 73  ide..** triggers
6dad0 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20   as usual.  The 
6dae0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77  previous value w
6daf0 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20  ill be restored 
6db00 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72  once the trigger
6db10 0d 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f  ..** exits.  Upo
6db20 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66  n entering a bef
6db30 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f  ore or instead o
6db40 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52  f trigger, lastR
6db50 6f 77 69 64 20 69 73 20 6e 6f 0d 0a 2a 2a 20 6c  owid is no..** l
6db60 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74  onger (since aft
6db70 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31  er version 2.8.1
6db80 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0d  2) reset to -1..
6db90 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  .**..** The sqli
6dba0 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20  te.nChange does 
6dbb0 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65  not count change
6dbc0 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72  s within trigger
6dbd0 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0d 0a  s and keeps no..
6dbe0 2a 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20  ** context.  It 
6dbf0 69 73 20 72 65 73 65 74 20 61 74 20 73 74 61 72  is reset at star
6dc00 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65  t of sqlite3_exe
6dc10 63 2e 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  c...** The sqlit
6dc20 65 2e 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65  e.lsChange repre
6dc30 73 65 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72  sents the number
6dc40 20 6f 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65   of changes made
6dc50 20 62 79 20 74 68 65 20 6c 61 73 74 0d 0a 2a 2a   by the last..**
6dc60 20 69 6e 73 65 72 74 2c 20 75 70 64 61 74 65 2c   insert, update,
6dc70 20 6f 72 20 64 65 6c 65 74 65 20 73 74 61 74 65   or delete state
6dc80 6d 65 6e 74 2e 20 20 49 74 20 72 65 6d 61 69 6e  ment.  It remain
6dc90 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75  s constant throu
6dca0 67 68 6f 75 74 20 74 68 65 0d 0a 2a 2a 20 6c 65  ghout the..** le
6dcb0 6e 67 74 68 20 6f 66 20 61 20 73 74 61 74 65 6d  ngth of a statem
6dcc0 65 6e 74 20 61 6e 64 20 69 73 20 74 68 65 6e 20  ent and is then 
6dcd0 75 70 64 61 74 65 64 20 62 79 20 4f 50 5f 53 65  updated by OP_Se
6dce0 74 43 6f 75 6e 74 73 2e 20 20 49 74 20 6b 65 65  tCounts.  It kee
6dcf0 70 73 20 61 0d 0a 2a 2a 20 63 6f 6e 74 65 78 74  ps a..** context
6dd00 20 73 74 61 63 6b 20 6a 75 73 74 20 6c 69 6b 65   stack just like
6dd10 20 6c 61 73 74 52 6f 77 69 64 20 73 6f 20 74 68   lastRowid so th
6dd20 61 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  at the count of 
6dd30 63 68 61 6e 67 65 73 0d 0a 2a 2a 20 77 69 74 68  changes..** with
6dd40 69 6e 20 61 20 74 72 69 67 67 65 72 20 69 73 20  in a trigger is 
6dd50 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 69 64 65  not seen outside
6dd60 20 74 68 65 20 74 72 69 67 67 65 72 2e 20 20 43   the trigger.  C
6dd70 68 61 6e 67 65 73 20 74 6f 20 76 69 65 77 73 20  hanges to views 
6dd80 64 6f 20 6e 6f 74 0d 0a 2a 2a 20 61 66 66 65 63  do not..** affec
6dd90 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6c  t the value of l
6dda0 73 43 68 61 6e 67 65 2e 0d 0a 2a 2a 20 54 68 65  sChange...** The
6ddb0 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 65   sqlite.csChange
6ddc0 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
6ddd0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75  the number of cu
6dde0 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 73  rrent changes (s
6ddf0 69 6e 63 65 0d 0a 2a 2a 20 74 68 65 20 6c 61 73  ince..** the las
6de00 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64  t statement) and
6de10 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64 61   is used to upda
6de20 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e  te sqlite_lsChan
6de30 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ge...**..** The 
6de40 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73  member variables
6de50 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c   sqlite.errCode,
6de60 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20   sqlite.zErrMsg 
6de70 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d  and sqlite.zErrM
6de80 73 67 31 36 0d 0a 2a 2a 20 73 74 6f 72 65 20 74  sg16..** store t
6de90 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
6dea0 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69  rror code and, i
6deb0 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74  f applicable, st
6dec0 72 69 6e 67 2e 20 54 68 65 0d 0a 2a 2a 20 69 6e  ring. The..** in
6ded0 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
6dee0 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69  sqlite3Error() i
6def0 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
6df00 65 73 65 20 76 61 72 69 61 62 6c 65 73 0d 0a 2a  ese variables..*
6df10 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0d  * consistently..
6df20 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 73 71 6c 69  .*/..struct sqli
6df30 74 65 33 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33  te3 {..  sqlite3
6df40 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20  _vfs *pVfs;     
6df50 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74         /* OS Int
6df60 65 72 66 61 63 65 20 2a 2f 0d 0a 20 20 69 6e 74  erface */..  int
6df70 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   nDb;           
6df80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
6df90 6d 62 65 72 20 6f 66 20 62 61 63 6b 65 6e 64 73  mber of backends
6dfa0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73   currently in us
6dfb0 65 20 2a 2f 0d 0a 20 20 44 62 20 2a 61 44 62 3b  e */..  Db *aDb;
6dfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dfd0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63        /* All bac
6dfe0 6b 65 6e 64 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  kends */..  int 
6dff0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
6e000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 73            /* Mis
6e010 63 65 6c 6c 61 6e 65 6f 75 73 20 66 6c 61 67 73  cellaneous flags
6e020 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0d 0a  . See below */..
6e030 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6f    unsigned int o
6e040 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20  penFlags;       
6e050 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  /* Flags passed 
6e060 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78  to sqlite3_vfs.x
6e070 4f 70 65 6e 28 29 20 2a 2f 0d 0a 20 20 69 6e 74  Open() */..  int
6e080 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20   errCode;       
6e090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f             /* Mo
6e0a0 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
6e0b0 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20  code (SQLITE_*) 
6e0c0 2a 2f 0d 0a 20 20 69 6e 74 20 65 72 72 4d 61 73  */..  int errMas
6e0d0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
6e0e0 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 20      /* & result 
6e0f0 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 20  codes with this 
6e100 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
6e110 20 2a 2f 0d 0a 20 20 75 38 20 61 75 74 6f 43 6f   */..  u8 autoCo
6e120 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  mmit;           
6e130 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f       /* The auto
6e140 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a 2f  -commit flag. */
6e150 0d 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f 72  ..  u8 temp_stor
6e160 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
6e170 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20    /* 1: file 2: 
6e180 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 6c  memory 0: defaul
6e190 74 20 2a 2f 0d 0a 20 20 75 38 20 6d 61 6c 6c 6f  t */..  u8 mallo
6e1a0 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20  cFailed;        
6e1b0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6e1c0 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20   we have seen a 
6e1d0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a  malloc failure *
6e1e0 2f 0d 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b  /..  u8 dfltLock
6e1f0 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
6e200 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f     /* Default lo
6e210 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61  cking-mode for a
6e220 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0d 0a  ttached dbs */..
6e230 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65    signed char ne
6e240 78 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20  xtAutovac;      
6e250 2f 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69  /* Autovac setti
6e260 6e 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ng after VACUUM 
6e270 69 66 20 3e 3d 30 20 2a 2f 0d 0a 20 20 75 38 20  if >=0 */..  u8 
6e280 73 75 70 70 72 65 73 73 45 72 72 3b 20 20 20 20  suppressErr;    
6e290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
6e2a0 20 6e 6f 74 20 69 73 73 75 65 20 65 72 72 6f 72   not issue error
6e2b0 20 6d 65 73 73 61 67 65 73 20 69 66 20 74 72 75   messages if tru
6e2c0 65 20 2a 2f 0d 0a 20 20 75 38 20 76 74 61 62 4f  e */..  u8 vtabO
6e2d0 6e 43 6f 6e 66 6c 69 63 74 3b 20 20 20 20 20 20  nConflict;      
6e2e0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
6e2f0 6f 20 72 65 74 75 72 6e 20 66 6f 72 20 73 33 5f  o return for s3_
6e300 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74  vtab_on_conflict
6e310 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 65 78  () */..  int nex
6e320 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20  tPagesize;      
6e330 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69         /* Pagesi
6e340 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ze after VACUUM 
6e350 69 66 20 3e 30 20 2a 2f 0d 0a 20 20 69 6e 74 20  if >0 */..  int 
6e360 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  nTable;         
6e370 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6e380 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ber of tables in
6e390 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
6e3a0 0d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66  ..  CollSeq *pDf
6e3b0 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  ltColl;         
6e3c0 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74    /* The default
6e3d0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
6e3e0 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0d  nce (BINARY) */.
6e3f0 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64  .  i64 lastRowid
6e400 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6e410 20 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73   /* ROWID of mos
6e420 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20  t recent insert 
6e430 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0d 0a  (see above) */..
6e440 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20    u32 magic;    
6e450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e460 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20  /* Magic number 
6e470 66 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61  for detect libra
6e480 72 79 20 6d 69 73 75 73 65 20 2a 2f 0d 0a 20 20  ry misuse */..  
6e490 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20  int nChange;    
6e4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6e4b0 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
6e4c0 62 79 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  by sqlite3_chang
6e4d0 65 73 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  es() */..  int n
6e4e0 54 6f 74 61 6c 43 68 61 6e 67 65 3b 20 20 20 20  TotalChange;    
6e4f0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
6e500 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
6e510 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
6e520 67 65 73 28 29 20 2a 2f 0d 0a 20 20 73 71 6c 69  ges() */..  sqli
6e530 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
6e540 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ;         /* Con
6e550 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 20 2a 2f  nection mutex */
6e560 0d 0a 20 20 69 6e 74 20 61 4c 69 6d 69 74 5b 53  ..  int aLimit[S
6e570 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 5d 3b 20  QLITE_N_LIMIT]; 
6e580 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a 2f 0d 0a    /* Limits */..
6e590 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
6e5a0 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 20 20  InitInfo {      
6e5b0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  /* Information u
6e5c0 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69  sed during initi
6e5d0 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20  alization */..  
6e5e0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
6e5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6e600 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 62 65   When back is be
6e610 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ing initialized 
6e620 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 65 77 54  */..    int newT
6e630 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  num;            
6e640 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 20      /* Rootpage 
6e650 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69  of table being i
6e660 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0d 0a 20  nitialized */.. 
6e670 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 20     u8 busy;     
6e680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6e690 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 6e  * TRUE if curren
6e6a0 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  tly initializing
6e6b0 20 2a 2f 0d 0a 20 20 20 20 75 38 20 6f 72 70 68   */..    u8 orph
6e6c0 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  anTrigger;      
6e6d0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61       /* Last sta
6e6e0 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e  tement is orphan
6e6f0 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20  ed TEMP trigger 
6e700 2a 2f 0d 0a 20 20 7d 20 69 6e 69 74 3b 0d 0a 20  */..  } init;.. 
6e710 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b   int nExtension;
6e720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6e730 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64  * Number of load
6e740 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f  ed extensions */
6e750 0d 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65  ..  void **aExte
6e760 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  nsion;          
6e770 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68    /* Array of sh
6e780 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e  ared library han
6e790 64 6c 65 73 20 2a 2f 0d 0a 20 20 73 74 72 75 63  dles */..  struc
6e7a0 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20  t Vdbe *pVdbe;  
6e7b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
6e7c0 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75   of active virtu
6e7d0 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0d 0a  al machines */..
6e7e0 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 65    int activeVdbe
6e7f0 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cnt;            
6e800 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42  /* Number of VDB
6e810 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  Es currently exe
6e820 63 75 74 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74  cuting */..  int
6e830 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20   writeVdbeCnt;  
6e840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
6e850 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56  mber of active V
6e860 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72  DBEs that are wr
6e870 69 74 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20  iting */..  int 
6e880 76 64 62 65 45 78 65 63 43 6e 74 3b 20 20 20 20  vdbeExecCnt;    
6e890 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6e8a0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61  ber of nested ca
6e8b0 6c 6c 73 20 74 6f 20 56 64 62 65 45 78 65 63 28  lls to VdbeExec(
6e8c0 29 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78  ) */..  void (*x
6e8d0 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
6e8e0 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20  st char*);      
6e8f0 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74    /* Trace funct
6e900 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a  ion */..  void *
6e910 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 20  pTraceArg;      
6e920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e930 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
6e940 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 6e  to the trace fun
6e950 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64  ction */..  void
6e960 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69   (*xProfile)(voi
6e970 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  d*,const char*,u
6e980 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69  64);  /* Profili
6e990 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a  ng function */..
6e9a0 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65    void *pProfile
6e9b0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
6e9c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
6e9d0 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69  rgument to profi
6e9e0 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a  le function */..
6e9f0 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41    void *pCommitA
6ea00 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
6ea10 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
6ea20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61  to xCommitCallba
6ea30 63 6b 28 29 20 2a 2f 20 20 20 0d 0a 20 20 69 6e  ck() */   ..  in
6ea40 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62  t (*xCommitCallb
6ea50 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ack)(void*);    
6ea60 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76  /* Invoked at ev
6ea70 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0d 0a  ery commit. */..
6ea80 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63    void *pRollbac
6ea90 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  kArg;           
6eaa0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
6eab0 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  to xRollbackCall
6eac0 62 61 63 6b 28 29 20 2a 2f 20 20 20 0d 0a 20 20  back() */   ..  
6ead0 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b  void (*xRollback
6eae0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29  Callback)(void*)
6eaf0 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20  ; /* Invoked at 
6eb00 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f  every commit. */
6eb10 0d 0a 20 20 76 6f 69 64 20 2a 70 55 70 64 61 74  ..  void *pUpdat
6eb20 65 41 72 67 3b 0d 0a 20 20 76 6f 69 64 20 28 2a  eArg;..  void (*
6eb30 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29  xUpdateCallback)
6eb40 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73  (void*,int, cons
6eb50 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
6eb60 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  ar*,sqlite_int64
6eb70 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
6eb80 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 69  TE_OMIT_WAL..  i
6eb90 6e 74 20 28 2a 78 57 61 6c 43 61 6c 6c 62 61 63  nt (*xWalCallbac
6eba0 6b 29 28 76 6f 69 64 20 2a 2c 20 73 71 6c 69 74  k)(void *, sqlit
6ebb0 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  e3 *, const char
6ebc0 20 2a 2c 20 69 6e 74 29 3b 0d 0a 20 20 76 6f 69   *, int);..  voi
6ebd0 64 20 2a 70 57 61 6c 41 72 67 3b 0d 0a 23 65 6e  d *pWalArg;..#en
6ebe0 64 69 66 0d 0a 20 20 76 6f 69 64 28 2a 78 43 6f  dif..  void(*xCo
6ebf0 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c  llNeeded)(void*,
6ec00 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
6ec10 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
6ec20 2a 29 3b 0d 0a 20 20 76 6f 69 64 28 2a 78 43 6f  *);..  void(*xCo
6ec30 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64  llNeeded16)(void
6ec40 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
6ec50 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
6ec60 69 64 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 2a 70  id*);..  void *p
6ec70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0d 0a  CollNeededArg;..
6ec80 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
6ec90 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 20  *pErr;          
6eca0 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65  /* Most recent e
6ecb0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0d  rror message */.
6ecc0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
6ecd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6ece0 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
6ecf0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55  error message (U
6ed00 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f  TF-8 encoded) */
6ed10 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ..  char *zErrMs
6ed20 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  g16;            
6ed30 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
6ed40 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28   error message (
6ed50 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20  UTF-16 encoded) 
6ed60 2a 2f 0d 0a 20 20 75 6e 69 6f 6e 20 7b 0d 0a 20  */..  union {.. 
6ed70 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20     volatile int 
6ed80 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f  isInterrupted; /
6ed90 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 65  * True if sqlite
6eda0 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 20  3_interrupt has 
6edb0 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0d 0a  been called */..
6edc0 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73      double notUs
6edd0 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed1;            
6ede0 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0d 0a 20 20  /* Spacer */..  
6edf0 7d 20 75 31 3b 0d 0a 20 20 4c 6f 6f 6b 61 73 69  } u1;..  Lookasi
6ee00 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20  de lookaside;   
6ee10 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73         /* Lookas
6ee20 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69  ide malloc confi
6ee30 67 75 72 61 74 69 6f 6e 20 2a 2f 0d 0a 23 69 66  guration */..#if
6ee40 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6ee50 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a  _AUTHORIZATION..
6ee60 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
6ee70 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
6ee80 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
6ee90 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
6eea0 73 74 20 63 68 61 72 2a 29 3b 0d 0a 20 20 20 20  st char*);..    
6eeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6eec0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
6eed0 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74  ccess authorizat
6eee0 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d  ion function */.
6eef0 0a 20 20 76 6f 69 64 20 2a 70 41 75 74 68 41 72  .  void *pAuthAr
6ef00 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
6ef10 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74   /* 1st argument
6ef20 20 74 6f 20 74 68 65 20 61 63 63 65 73 73 20 61   to the access a
6ef30 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d  uth function */.
6ef40 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
6ef50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
6ef60 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0d 0a  GRESS_CALLBACK..
6ef70 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73    int (*xProgres
6ef80 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20  s)(void *);     
6ef90 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20  /* The progress 
6efa0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 20 20 76  callback */..  v
6efb0 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72  oid *pProgressAr
6efc0 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
6efd0 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
6efe0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
6eff0 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50 72 6f  k */..  int nPro
6f000 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20  gressOps;       
6f010 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6f020 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70  of opcodes for p
6f030 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
6f040 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66   */..#endif..#if
6f050 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6f060 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20  _VIRTUALTABLE.. 
6f070 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20   Hash aModule;  
6f080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6f090 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73  * populated by s
6f0a0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
6f0b0 64 75 6c 65 28 29 20 2a 2f 0d 0a 20 20 56 74 61  dule() */..  Vta
6f0c0 62 43 74 78 20 2a 70 56 74 61 62 43 74 78 3b 20  bCtx *pVtabCtx; 
6f0d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
6f0e0 6e 74 65 78 74 20 66 6f 72 20 61 63 74 69 76 65  ntext for active
6f0f0 20 76 74 61 62 20 63 6f 6e 6e 65 63 74 2f 63 72   vtab connect/cr
6f100 65 61 74 65 20 2a 2f 0d 0a 20 20 56 54 61 62 6c  eate */..  VTabl
6f110 65 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20  e **aVTrans;    
6f120 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
6f130 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20  ual tables with 
6f140 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
6f150 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 56 54 72  s */..  int nVTr
6f160 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ans;            
6f170 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
6f180 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61  ed size of aVTra
6f190 6e 73 20 2a 2f 0d 0a 20 20 56 54 61 62 6c 65 20  ns */..  VTable 
6f1a0 2a 70 44 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20  *pDisconnect;   
6f1b0 20 2f 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 74   /* Disconnect t
6f1c0 68 65 73 65 20 69 6e 20 6e 65 78 74 20 73 71 6c  hese in next sql
6f1d0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a  ite3_prepare() *
6f1e0 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 46 75 6e  /..#endif..  Fun
6f1f0 63 44 65 66 48 61 73 68 20 61 46 75 6e 63 3b 20  cDefHash aFunc; 
6f200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
6f210 73 68 20 74 61 62 6c 65 20 6f 66 20 63 6f 6e 6e  sh table of conn
6f220 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  ection functions
6f230 20 2a 2f 0d 0a 20 20 48 61 73 68 20 61 43 6f 6c   */..  Hash aCol
6f240 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  lSeq;           
6f250 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c 6c       /* All coll
6f260 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
6f270 2a 2f 0d 0a 20 20 42 75 73 79 48 61 6e 64 6c 65  */..  BusyHandle
6f280 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 20  r busyHandler;  
6f290 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c 6c      /* Busy call
6f2a0 62 61 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 62  back */..  int b
6f2b0 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20  usyTimeout;     
6f2c0 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79           /* Busy
6f2d0 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74   handler timeout
6f2e0 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0d 0a 20 20  , in msec */..  
6f2f0 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b  Db aDbStatic[2];
6f300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6f310 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f   Static space fo
6f320 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20  r the 2 default 
6f330 62 61 63 6b 65 6e 64 73 20 2a 2f 0d 0a 20 20 53  backends */..  S
6f340 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
6f350 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  oint;        /* 
6f360 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 73  List of active s
6f370 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0d 0a 20 20  avepoints */..  
6f380 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20  int nSavepoint; 
6f390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6f3a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74   Number of non-t
6f3b0 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70  ransaction savep
6f3c0 6f 69 6e 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  oints */..  int 
6f3d0 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20  nStatement;     
6f3e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6f3f0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74  ber of nested st
6f400 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
6f410 69 6f 6e 73 20 20 2a 2f 0d 0a 20 20 75 38 20 69  ions  */..  u8 i
6f420 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
6f430 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75  point;    /* Tru
6f440 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f  e if the outermo
6f450 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  st savepoint is 
6f460 61 20 54 53 20 2a 2f 0d 0a 20 20 69 36 34 20 6e  a TS */..  i64 n
6f470 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20  DeferredCons;   
6f480 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20           /* Net 
6f490 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
6f4a0 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 61  ints this transa
6f4b0 63 74 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 69 6e 74  ction. */..  int
6f4c0 20 2a 70 6e 42 79 74 65 73 46 72 65 65 64 3b 20   *pnBytesFreed; 
6f4d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
6f4e0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 6e 63 72 65   not NULL, incre
6f4f0 6d 65 6e 74 20 74 68 69 73 20 69 6e 20 44 62 46  ment this in DbF
6f500 72 65 65 28 29 20 2a 2f 0d 0a 0d 0a 23 69 66 64  ree() */....#ifd
6f510 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
6f520 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0d 0a  _UNLOCK_NOTIFY..
6f530 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
6f540 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ng variables are
6f550 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62   all protected b
6f560 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53  y the STATIC_MAS
6f570 54 45 52 20 0d 0a 20 20 2a 2a 20 6d 75 74 65 78  TER ..  ** mutex
6f580 2c 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33  , not by sqlite3
6f590 2e 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65  .mutex. They are
6f5a0 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e   used by code in
6f5b0 20 6e 6f 74 69 66 79 2e 63 2e 20 0d 0a 20 20 2a   notify.c. ..  *
6f5c0 2a 0d 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70  *..  ** When X.p
6f5d0 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
6f5e0 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ==Y, that means 
6f5f0 74 68 61 74 20 58 20 69 73 20 77 61 69 74 69 6e  that X is waitin
6f600 67 20 66 6f 72 20 59 20 74 6f 0d 0a 20 20 2a 2a  g for Y to..  **
6f610 20 75 6e 6c 6f 63 6b 20 73 6f 20 74 68 61 74 20   unlock so that 
6f620 69 74 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 0d  it can proceed..
6f630 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57 68 65 6e  .  **..  ** When
6f640 20 58 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e   X.pBlockingConn
6f650 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 20  ection==Y, that 
6f660 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 74  means that somet
6f670 68 69 6e 67 20 74 68 61 74 20 58 20 74 72 69 65  hing that X trie
6f680 64 0d 0a 20 20 2a 2a 20 74 72 69 65 64 20 74 6f  d..  ** tried to
6f690 20 64 6f 20 72 65 63 65 6e 74 6c 79 20 66 61 69   do recently fai
6f6a0 6c 65 64 20 77 69 74 68 20 61 6e 20 53 51 4c 49  led with an SQLI
6f6b0 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20  TE_LOCKED error 
6f6c0 64 75 65 20 74 6f 20 6c 6f 63 6b 73 0d 0a 20 20  due to locks..  
6f6d0 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e 0d 0a 20  ** held by Y... 
6f6e0 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a   */..  sqlite3 *
6f6f0 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
6f700 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 69  ion; /* Connecti
6f710 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 20 53  on that caused S
6f720 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f 0d  QLITE_LOCKED */.
6f730 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c  .  sqlite3 *pUnl
6f740 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20  ockConnection;  
6f750 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
6f760 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20  ection to watch 
6f770 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0d 0a 20  for unlock */.. 
6f780 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72   void *pUnlockAr
6f790 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
6f7a0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
6f7b0 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74  nt to xUnlockNot
6f7c0 69 66 79 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28  ify */..  void (
6f7d0 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28  *xUnlockNotify)(
6f7e0 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20  void **, int);  
6f7f0 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79  /* Unlock notify
6f800 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 20 20   callback */..  
6f810 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c  sqlite3 *pNextBl
6f820 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a  ocked;        /*
6f830 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66   Next in list of
6f840 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   all blocked con
6f850 6e 65 63 74 69 6f 6e 73 20 2a 2f 0d 0a 23 65 6e  nections */..#en
6f860 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  dif..};..../*..*
6f870 2a 20 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73  * A macro to dis
6f880 63 6f 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69  cover the encodi
6f890 6e 67 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  ng of a database
6f8a0 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 45  ...*/..#define E
6f8b0 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e 61 44  NC(db) ((db)->aD
6f8c0 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e 65 6e  b[0].pSchema->en
6f8d0 63 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f 73  c)..../*..** Pos
6f8e0 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
6f8f0 20 74 68 65 20 73 71 6c 69 74 65 33 2e 66 6c 61   the sqlite3.fla
6f900 67 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  gs...*/..#define
6f910 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63   SQLITE_VdbeTrac
6f920 65 20 20 20 20 20 20 30 78 30 30 30 30 30 31 30  e      0x0000010
6f930 30 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72  0  /* True to tr
6f940 61 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69  ace VDBE executi
6f950 6f 6e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  on */..#define S
6f960 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
6f970 67 65 73 20 20 30 78 30 30 30 30 30 32 30 30 20  ges  0x00000200 
6f980 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20   /* Uncommitted 
6f990 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e 67  Hash table chang
6f9a0 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  es */..#define S
6f9b0 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
6f9c0 65 73 20 20 20 30 78 30 30 30 30 30 34 30 30 20  es   0x00000400 
6f9d0 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f   /* Show full co
6f9e0 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45  lumn names on SE
6f9f0 4c 45 43 54 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  LECT */..#define
6fa00 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
6fa10 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 38 30  Names  0x0000080
6fa20 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74  0  /* Show short
6fa30 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a   columns names *
6fa40 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
6fa50 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20  E_CountRows     
6fa60 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20   0x00001000  /* 
6fa70 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67  Count rows chang
6fa80 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f  ed by INSERT, */
6fa90 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
6faa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
6fac0 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41   DELETE, or UPDA
6fad0 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f  TE and return */
6fae0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
6faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fb00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
6fb10 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67   the count using
6fb20 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0d   a callback. */.
6fb30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
6fb40 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30  NullCallback   0
6fb50 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 49 6e  x00002000  /* In
6fb60 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
6fb70 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f  k once if the */
6fb80 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
6fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fba0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
6fbb0 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65   result set is e
6fbc0 6d 70 74 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  mpty */..#define
6fbd0 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65   SQLITE_SqlTrace
6fbe0 20 20 20 20 20 20 20 30 78 30 30 30 30 34 30 30         0x0000400
6fbf0 30 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e  0  /* Debug prin
6fc00 74 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63  t SQL as it exec
6fc10 75 74 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  utes */..#define
6fc20 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74   SQLITE_VdbeList
6fc30 69 6e 67 20 20 20 20 30 78 30 30 30 30 38 30 30  ing    0x0000800
6fc40 30 20 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74  0  /* Debug list
6fc50 69 6e 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f  ings of VDBE pro
6fc60 67 72 61 6d 73 20 2a 2f 0d 0a 23 64 65 66 69 6e  grams */..#defin
6fc70 65 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63  e SQLITE_WriteSc
6fc80 68 65 6d 61 20 20 20 20 30 78 30 30 30 31 30 30  hema    0x000100
6fc90 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64  00  /* OK to upd
6fca0 61 74 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  ate SQLITE_MASTE
6fcb0 52 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51  R */..#define SQ
6fcc0 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20  LITE_NoReadlock 
6fcd0 20 20 20 20 30 78 30 30 30 32 30 30 30 30 20 20      0x00020000  
6fce0 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61 72 65  /* Readlocks are
6fcf0 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 0d 0a   omitted when ..
6fd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fd20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63 63            ** acc
6fd30 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c 79  essing read-only
6fd40 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0d 0a 23   databases */..#
6fd50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67  define SQLITE_Ig
6fd60 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30  noreChecks   0x0
6fd70 30 30 34 30 30 30 30 20 20 2f 2a 20 44 6f 20 6e  0040000  /* Do n
6fd80 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b  ot enforce check
6fd90 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0d   constraints */.
6fda0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
6fdb0 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
6fdc0 30 78 30 30 38 30 30 30 30 20 20 2f 2a 20 46 6f  0x0080000  /* Fo
6fdd0 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  r shared-cache m
6fde0 6f 64 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ode */..#define 
6fdf0 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
6fe00 65 46 6d 74 20 20 30 78 30 30 31 30 30 30 30 30  eFmt  0x00100000
6fe10 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20    /* Create new 
6fe20 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72  databases in for
6fe30 6d 61 74 20 31 20 2a 2f 0d 0a 23 64 65 66 69 6e  mat 1 */..#defin
6fe40 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79  e SQLITE_FullFSy
6fe50 6e 63 20 20 20 20 20 20 30 78 30 30 32 30 30 30  nc      0x002000
6fe60 30 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20  00  /* Use full 
6fe70 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 63  fsync on the bac
6fe80 6b 65 6e 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  kend */..#define
6fe90 20 53 51 4c 49 54 45 5f 43 6b 70 74 46 75 6c 6c   SQLITE_CkptFull
6fea0 46 53 79 6e 63 20 20 30 78 30 30 34 30 30 30 30  FSync  0x0040000
6feb0 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66  0  /* Use full f
6fec0 73 79 6e 63 20 66 6f 72 20 63 68 65 63 6b 70 6f  sync for checkpo
6fed0 69 6e 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  int */..#define 
6fee0 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
6fef0 6f 64 65 20 20 20 30 78 30 30 38 30 30 30 30 30  ode   0x00800000
6ff00 20 20 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65    /* Ignore sche
6ff10 6d 61 20 65 72 72 6f 72 73 20 2a 2f 0d 0a 23 64  ma errors */..#d
6ff20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 76  efine SQLITE_Rev
6ff30 65 72 73 65 4f 72 64 65 72 20 20 20 30 78 30 31  erseOrder   0x01
6ff40 30 30 30 30 30 30 20 20 2f 2a 20 52 65 76 65 72  000000  /* Rever
6ff50 73 65 20 75 6e 6f 72 64 65 72 65 64 20 53 45 4c  se unordered SEL
6ff60 45 43 54 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ECTs */..#define
6ff70 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67   SQLITE_RecTrigg
6ff80 65 72 73 20 20 20 20 30 78 30 32 30 30 30 30 30  ers    0x0200000
6ff90 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 72 65 63  0  /* Enable rec
6ffa0 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
6ffb0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
6ffc0 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 20  TE_ForeignKeys  
6ffd0 20 20 30 78 30 34 30 30 30 30 30 30 20 20 2f 2a    0x04000000  /*
6ffe0 20 45 6e 66 6f 72 63 65 20 66 6f 72 65 69 67 6e   Enforce foreign
6fff0 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
70000 20 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51    */..#define SQ
70010 4c 49 54 45 5f 41 75 74 6f 49 6e 64 65 78 20 20  LITE_AutoIndex  
70020 20 20 20 20 30 78 30 38 30 30 30 30 30 30 20 20      0x08000000  
70030 2f 2a 20 45 6e 61 62 6c 65 20 61 75 74 6f 6d 61  /* Enable automa
70040 74 69 63 20 69 6e 64 65 78 65 73 20 2a 2f 0d 0a  tic indexes */..
70050 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50  #define SQLITE_P
70060 72 65 66 65 72 42 75 69 6c 74 69 6e 20 20 30 78  referBuiltin  0x
70070 31 30 30 30 30 30 30 30 20 20 2f 2a 20 50 72 65  10000000  /* Pre
70080 66 65 72 65 6e 63 65 20 74 6f 20 62 75 69 6c 74  ference to built
70090 2d 69 6e 20 66 75 6e 63 73 20 2a 2f 0d 0a 23 64  -in funcs */..#d
700a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61  efine SQLITE_Loa
700b0 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 32 30  dExtension  0x20
700c0 30 30 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c  000000  /* Enabl
700d0 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e load_extension
700e0 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
700f0 49 54 45 5f 45 6e 61 62 6c 65 54 72 69 67 67 65  ITE_EnableTrigge
70100 72 20 20 30 78 34 30 30 30 30 30 30 30 20 20 2f  r  0x40000000  /
70110 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65  * True to enable
70120 20 74 72 69 67 67 65 72 73 20 2a 2f 0d 0a 0d 0a   triggers */....
70130 2f 2a 0d 0a 2a 2a 20 42 69 74 73 20 6f 66 20 74  /*..** Bits of t
70140 68 65 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73  he sqlite3.flags
70150 20 66 69 65 6c 64 20 74 68 61 74 20 61 72 65 20   field that are 
70160 75 73 65 64 20 62 79 20 74 68 65 0d 0a 2a 2a 20  used by the..** 
70170 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
70180 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
70190 43 54 52 4c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CTRL_OPTIMIZATIO
701a0 4e 53 2c 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63  NS,...) interfac
701b0 65 2e 0d 0a 2a 2a 20 54 68 65 73 65 20 6d 75 73  e...** These mus
701c0 74 20 62 65 20 74 68 65 20 6c 6f 77 2d 6f 72 64  t be the low-ord
701d0 65 72 20 62 69 74 73 20 6f 66 20 74 68 65 20 66  er bits of the f
701e0 6c 61 67 73 20 66 69 65 6c 64 2e 0d 0a 2a 2f 0d  lags field...*/.
701f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
70200 51 75 65 72 79 46 6c 61 74 74 65 6e 65 72 20 30  QueryFlattener 0
70210 78 30 31 20 20 20 20 20 20 20 20 2f 2a 20 44 69  x01        /* Di
70220 73 61 62 6c 65 20 71 75 65 72 79 20 66 6c 61 74  sable query flat
70230 74 65 6e 69 6e 67 20 2a 2f 0d 0a 23 64 65 66 69  tening */..#defi
70240 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6c 75 6d 6e  ne SQLITE_Column
70250 43 61 63 68 65 20 20 20 20 30 78 30 32 20 20 20  Cache    0x02   
70260 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20       /* Disable 
70270 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
70280 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
70290 49 54 45 5f 49 6e 64 65 78 53 6f 72 74 20 20 20  ITE_IndexSort   
702a0 20 20 20 30 78 30 34 20 20 20 20 20 20 20 20 2f     0x04        /
702b0 2a 20 44 69 73 61 62 6c 65 20 69 6e 64 65 78 65  * Disable indexe
702c0 73 20 66 6f 72 20 73 6f 72 74 69 6e 67 20 2a 2f  s for sorting */
702d0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
702e0 5f 49 6e 64 65 78 53 65 61 72 63 68 20 20 20 20  _IndexSearch    
702f0 30 78 30 38 20 20 20 20 20 20 20 20 2f 2a 20 44  0x08        /* D
70300 69 73 61 62 6c 65 20 69 6e 64 65 78 65 73 20 66  isable indexes f
70310 6f 72 20 73 65 61 72 63 68 69 6e 67 20 2a 2f 0d  or searching */.
70320 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
70330 49 6e 64 65 78 43 6f 76 65 72 20 20 20 20 20 30  IndexCover     0
70340 78 31 30 20 20 20 20 20 20 20 20 2f 2a 20 44 69  x10        /* Di
70350 73 61 62 6c 65 20 69 6e 64 65 78 20 63 6f 76 65  sable index cove
70360 72 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0d 0a 23  ring table */..#
70370 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 47 72  define SQLITE_Gr
70380 6f 75 70 42 79 4f 72 64 65 72 20 20 20 30 78 32  oupByOrder   0x2
70390 30 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61  0        /* Disa
703a0 62 6c 65 20 47 52 4f 55 50 42 59 20 63 6f 76 65  ble GROUPBY cove
703b0 72 20 6f 66 20 4f 52 44 45 52 42 59 20 2a 2f 0d  r of ORDERBY */.
703c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
703d0 46 61 63 74 6f 72 4f 75 74 43 6f 6e 73 74 20 30  FactorOutConst 0
703e0 78 34 30 20 20 20 20 20 20 20 20 2f 2a 20 44 69  x40        /* Di
703f0 73 61 62 6c 65 20 66 61 63 74 6f 72 69 6e 67 20  sable factoring 
70400 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f  out constants */
70410 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
70420 5f 49 64 78 52 65 61 6c 41 73 49 6e 74 20 20 20  _IdxRealAsInt   
70430 30 78 38 30 20 20 20 20 20 20 20 20 2f 2a 20 53  0x80        /* S
70440 74 6f 72 65 20 52 45 41 4c 20 61 73 20 49 4e 54  tore REAL as INT
70450 20 69 6e 20 69 6e 64 69 63 65 73 20 2a 2f 0d 0a   in indices */..
70460 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
70470 69 73 74 69 6e 63 74 4f 70 74 20 20 20 20 30 78  istinctOpt    0x
70480 38 30 20 20 20 20 20 20 20 20 2f 2a 20 44 49 53  80        /* DIS
70490 54 49 4e 43 54 20 75 73 69 6e 67 20 69 6e 64 65  TINCT using inde
704a0 78 65 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  xes */..#define 
704b0 53 51 4c 49 54 45 5f 4f 70 74 4d 61 73 6b 20 20  SQLITE_OptMask  
704c0 20 20 20 20 20 20 30 78 66 66 20 20 20 20 20 20        0xff      
704d0 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c    /* Mask of all
704e0 20 64 69 73 61 62 6c 61 62 6c 65 20 6f 70 74 73   disablable opts
704f0 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f   */..../*..** Po
70500 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
70510 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  r the sqlite.mag
70520 69 63 20 66 69 65 6c 64 2e 0d 0a 2a 2a 20 54 68  ic field...** Th
70530 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62  e numbers are ob
70540 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d  tained at random
70550 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65   and have no spe
70560 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74  cial meaning, ot
70570 68 65 72 0d 0a 2a 2a 20 74 68 61 6e 20 62 65 69  her..** than bei
70580 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d  ng distinct from
70590 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0d 0a 2a   one another...*
705a0 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
705b0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20  E_MAGIC_OPEN    
705c0 20 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20   0xa029a697  /* 
705d0 44 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  Database is open
705e0 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
705f0 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44  ITE_MAGIC_CLOSED
70600 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20 2f     0x9f3c2d33  /
70610 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63 6c  * Database is cl
70620 6f 73 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  osed */..#define
70630 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49   SQLITE_MAGIC_SI
70640 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39  CK     0x4b77129
70650 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20  0  /* Error and 
70660 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a  awaiting close *
70670 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
70680 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20  E_MAGIC_BUSY    
70690 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20   0xf03b7906  /* 
706a0 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74  Database current
706b0 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0d 0a 23 64  ly in use */..#d
706c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
706d0 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35  IC_ERROR    0xb5
706e0 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51  357930  /* An SQ
706f0 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f  LITE_MISUSE erro
70700 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0d 0a 0d  r occurred */...
70710 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 53 51 4c  ./*..** Each SQL
70720 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 66   function is def
70730 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61  ined by an insta
70740 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
70750 77 69 6e 67 0d 0a 2a 2a 20 73 74 72 75 63 74 75  wing..** structu
70760 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74  re.  A pointer t
70770 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
70780 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
70790 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63 0d 0a  e sqlite.aFunc..
707a0 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  ** hash table.  
707b0 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66 75  When multiple fu
707c0 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68 65  nctions have the
707d0 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65 20   same name, the 
707e0 68 61 73 68 20 74 61 62 6c 65 0d 0a 2a 2a 20 70  hash table..** p
707f0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65  oints to a linke
70800 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20  d list of these 
70810 73 74 72 75 63 74 75 72 65 73 2e 0d 0a 2a 2f 0d  structures...*/.
70820 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20  .struct FuncDef 
70830 7b 0d 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 20  {..  i16 nArg;  
70840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
70850 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
70860 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69  .  -1 means unli
70870 6d 69 74 65 64 20 2a 2f 0d 0a 20 20 75 38 20 69  mited */..  u8 i
70880 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20  PrefEnc;        
70890 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65   /* Preferred te
708a0 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c  xt encoding (SQL
708b0 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20  ITE_UTF8, 16LE, 
708c0 31 36 42 45 29 20 2a 2f 0d 0a 20 20 75 38 20 66  16BE) */..  u8 f
708d0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
708e0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
708f0 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46  tion of SQLITE_F
70900 55 4e 43 5f 2a 20 2a 2f 0d 0a 20 20 76 6f 69 64  UNC_* */..  void
70910 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20   *pUserData;    
70920 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61   /* User data pa
70930 72 61 6d 65 74 65 72 20 2a 2f 0d 0a 20 20 46 75  rameter */..  Fu
70940 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20  ncDef *pNext;   
70950 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74     /* Next funct
70960 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  ion with same na
70970 6d 65 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a  me */..  void (*
70980 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
70990 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
709a0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a  te3_value**); /*
709b0 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f   Regular functio
709c0 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78  n */..  void (*x
709d0 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
709e0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
709f0 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20  e3_value**); /* 
70a00 41 67 67 72 65 67 61 74 65 20 73 74 65 70 20 2a  Aggregate step *
70a10 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e  /..  void (*xFin
70a20 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63  alize)(sqlite3_c
70a30 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20  ontext*);       
70a40 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72           /* Aggr
70a50 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 20  egate finalizer 
70a60 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  */..  char *zNam
70a70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  e;         /* SQ
70a80 4c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  L name of the fu
70a90 6e 63 74 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 46 75  nction. */..  Fu
70aa0 6e 63 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20  ncDef *pHash;   
70ab0 20 20 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20     /* Next with 
70ac0 61 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65  a different name
70ad0 20 62 75 74 20 74 68 65 20 73 61 6d 65 20 68 61   but the same ha
70ae0 73 68 20 2a 2f 0d 0a 20 20 46 75 6e 63 44 65 73  sh */..  FuncDes
70af0 74 72 75 63 74 6f 72 20 2a 70 44 65 73 74 72 75  tructor *pDestru
70b00 63 74 6f 72 3b 20 20 20 2f 2a 20 52 65 66 65 72  ctor;   /* Refer
70b10 65 6e 63 65 20 63 6f 75 6e 74 65 64 20 64 65 73  ence counted des
70b20 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
70b30 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
70b40 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
70b50 20 65 6e 63 61 70 73 75 6c 61 74 65 73 20 61 20   encapsulates a 
70b60 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65  user-function de
70b70 73 74 72 75 63 74 6f 72 20 63 61 6c 6c 62 61 63  structor callbac
70b80 6b 20 28 61 73 0d 0a 2a 2a 20 63 6f 6e 66 69 67  k (as..** config
70b90 75 72 65 64 20 75 73 69 6e 67 20 63 72 65 61 74  ured using creat
70ba0 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28 29 29  e_function_v2())
70bb0 20 61 6e 64 20 61 20 72 65 66 65 72 65 6e 63 65   and a reference
70bc0 20 63 6f 75 6e 74 65 72 2e 20 57 68 65 6e 0d 0a   counter. When..
70bd0 2a 2a 20 63 72 65 61 74 65 5f 66 75 6e 63 74 69  ** create_functi
70be0 6f 6e 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65  on_v2() is calle
70bf0 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 66 75  d to create a fu
70c00 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 64 65  nction with a de
70c10 73 74 72 75 63 74 6f 72 2c 0d 0a 2a 2a 20 61 20  structor,..** a 
70c20 73 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 6f 66  single object of
70c30 20 74 68 69 73 20 74 79 70 65 20 69 73 20 61 6c   this type is al
70c40 6c 6f 63 61 74 65 64 2e 20 46 75 6e 63 44 65 73  located. FuncDes
70c50 74 72 75 63 74 6f 72 2e 6e 52 65 66 20 69 73 20  tructor.nRef is 
70c60 73 65 74 20 74 6f 20 0d 0a 2a 2a 20 74 68 65 20  set to ..** the 
70c70 6e 75 6d 62 65 72 20 6f 66 20 46 75 6e 63 44 65  number of FuncDe
70c80 66 20 6f 62 6a 65 63 74 73 20 63 72 65 61 74 65  f objects create
70c90 64 20 28 65 69 74 68 65 72 20 31 20 6f 72 20 33  d (either 1 or 3
70ca0 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  , depending on w
70cb0 68 65 74 68 65 72 0d 0a 2a 2a 20 6f 72 20 6e 6f  hether..** or no
70cc0 74 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  t the specified 
70cd0 65 6e 63 6f 64 69 6e 67 20 69 73 20 53 51 4c 49  encoding is SQLI
70ce0 54 45 5f 41 4e 59 29 2e 20 54 68 65 20 46 75 6e  TE_ANY). The Fun
70cf0 63 44 65 66 2e 70 44 65 73 74 72 75 63 74 6f 72  cDef.pDestructor
70d00 0d 0a 2a 2a 20 6d 65 6d 62 65 72 20 6f 66 20 65  ..** member of e
70d10 61 63 68 20 6f 66 20 74 68 65 20 6e 65 77 20 46  ach of the new F
70d20 75 6e 63 44 65 66 20 6f 62 6a 65 63 74 73 20 69  uncDef objects i
70d30 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
70d40 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 0d  o the allocated.
70d50 0a 2a 2a 20 46 75 6e 63 44 65 73 74 72 75 63 74  .** FuncDestruct
70d60 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72  or...**..** Ther
70d70 65 61 66 74 65 72 2c 20 77 68 65 6e 20 6f 6e 65  eafter, when one
70d80 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 20   of the FuncDef 
70d90 6f 62 6a 65 63 74 73 20 69 73 20 64 65 6c 65 74  objects is delet
70da0 65 64 2c 20 74 68 65 20 72 65 66 65 72 65 6e 63  ed, the referenc
70db0 65 0d 0a 2a 2a 20 63 6f 75 6e 74 20 6f 6e 20 74  e..** count on t
70dc0 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 64 65  his object is de
70dd0 63 72 65 6d 65 6e 74 65 64 2e 20 57 68 65 6e 20  cremented. When 
70de0 69 74 20 72 65 61 63 68 65 73 20 30 2c 20 74 68  it reaches 0, th
70df0 65 20 64 65 73 74 72 75 63 74 6f 72 0d 0a 2a 2a  e destructor..**
70e00 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 6e 64 20   is invoked and 
70e10 74 68 65 20 46 75 6e 63 44 65 73 74 72 75 63 74  the FuncDestruct
70e20 6f 72 20 73 74 72 75 63 74 75 72 65 20 66 72 65  or structure fre
70e30 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  ed...*/..struct 
70e40 46 75 6e 63 44 65 73 74 72 75 63 74 6f 72 20 7b  FuncDestructor {
70e50 0d 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0d 0a 20  ..  int nRef;.. 
70e60 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
70e70 29 28 76 6f 69 64 20 2a 29 3b 0d 0a 20 20 76 6f  )(void *);..  vo
70e80 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b 0d 0a  id *pUserData;..
70e90 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f 73  };..../*..** Pos
70ea0 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
70eb0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0d 0a   FuncDef.flags..
70ec0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
70ed0 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20  TE_FUNC_LIKE    
70ee0 20 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61   0x01 /* Candida
70ef0 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  te for the LIKE 
70f00 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0d  optimization */.
70f10 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
70f20 46 55 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78  FUNC_CASE     0x
70f30 30 32 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69  02 /* Case-sensi
70f40 74 69 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66  tive LIKE-type f
70f50 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 23 64 65 66  unction */..#def
70f60 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
70f70 45 50 48 45 4d 20 20 20 20 30 78 30 34 20 2f 2a  EPHEM    0x04 /*
70f80 20 45 70 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c   Ephemeral.  Del
70f90 65 74 65 20 77 69 74 68 20 56 44 42 45 20 2a 2f  ete with VDBE */
70fa0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
70fb0 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 30  _FUNC_NEEDCOLL 0
70fc0 78 30 38 20 2f 2a 20 73 71 6c 69 74 65 33 47 65  x08 /* sqlite3Ge
70fd0 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 20 6d  tFuncCollSeq() m
70fe0 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 2a  ight be called *
70ff0 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
71000 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 45 20 20  E_FUNC_PRIVATE  
71010 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 65 64 20  0x10 /* Allowed 
71020 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
71030 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e   only */..#defin
71040 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f  e SQLITE_FUNC_CO
71050 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a 20 42  UNT    0x20 /* B
71060 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 2a 29  uilt-in count(*)
71070 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0d 0a 23   aggregate */..#
71080 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
71090 4e 43 5f 43 4f 41 4c 45 53 43 45 20 30 78 34 30  NC_COALESCE 0x40
710a0 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 61   /* Built-in coa
710b0 6c 65 73 63 65 28 29 20 6f 72 20 69 66 6e 75 6c  lesce() or ifnul
710c0 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d  l() function */.
710d0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
710e0 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d 61  llowing three ma
710f0 63 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29  cros, FUNCTION()
71100 2c 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e 64  , LIKEFUNC() and
71110 20 41 47 47 52 45 47 41 54 45 28 29 20 61 72 65   AGGREGATE() are
71120 0d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65  ..** used to cre
71130 61 74 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69  ate the initiali
71140 7a 65 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e  zers for the Fun
71150 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73 2e  cDef structures.
71160 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 46 55 4e 43 54  ..**..**   FUNCT
71170 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  ION(zName, nArg,
71180 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e   iArg, bNC, xFun
71190 63 29 0d 0a 2a 2a 20 20 20 20 20 55 73 65 64 20  c)..**     Used 
711a0 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 6c  to create a scal
711b0 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  ar function defi
711c0 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63  nition of a func
711d0 74 69 6f 6e 20 7a 4e 61 6d 65 20 0d 0a 2a 2a 20  tion zName ..** 
711e0 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20      implemented 
711f0 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46  by C function xF
71200 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73  unc that accepts
71210 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e   nArg arguments.
71220 20 54 68 65 0d 0a 2a 2a 20 20 20 20 20 76 61 6c   The..**     val
71230 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72  ue passed as iAr
71240 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28  g is cast to a (
71250 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20  void*) and made 
71260 61 76 61 69 6c 61 62 6c 65 0d 0a 2a 2a 20 20 20  available..**   
71270 20 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61    as the user-da
71280 74 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72  ta (sqlite3_user
71290 5f 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65  _data()) for the
712a0 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0d 0a   function. If ..
712b0 2a 2a 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20  **     argument 
712c0 62 4e 43 20 69 73 20 74 72 75 65 2c 20 74 68 65  bNC is true, the
712d0 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e  n the SQLITE_FUN
712e0 43 5f 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20  C_NEEDCOLL flag 
712f0 69 73 20 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  is set...**..** 
71300 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d    AGGREGATE(zNam
71310 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
71320 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  NC, xStep, xFina
71330 6c 29 0d 0a 2a 2a 20 20 20 20 20 55 73 65 64 20  l)..**     Used 
71340 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67  to create an agg
71350 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
71360 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65  definition imple
71370 6d 65 6e 74 65 64 20 62 79 0d 0a 2a 2a 20 20 20  mented by..**   
71380 20 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e    the C function
71390 73 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e  s xStep and xFin
713a0 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f  al. The first fo
713b0 75 72 20 70 61 72 61 6d 65 74 65 72 73 0d 0a 2a  ur parameters..*
713c0 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72 70  *     are interp
713d0 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d  reted in the sam
713e0 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69 72  e way as the fir
713f0 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73 20  st 4 parameters 
71400 74 6f 0d 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54  to..**     FUNCT
71410 49 4f 4e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ION()...**..**  
71420 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c   LIKEFUNC(zName,
71430 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 66 6c 61   nArg, pArg, fla
71440 67 73 29 0d 0a 2a 2a 20 20 20 20 20 55 73 65 64  gs)..**     Used
71450 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61   to create a sca
71460 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  lar function def
71470 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e  inition of a fun
71480 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0d 0a 2a 2a  ction zName ..**
71490 20 20 20 20 20 74 68 61 74 20 61 63 63 65 70 74       that accept
714a0 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73  s nArg arguments
714b0 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e   and is implemen
714c0 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ted by a call to
714d0 20 43 20 0d 0a 2a 2a 20 20 20 20 20 66 75 6e 63   C ..**     func
714e0 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 41  tion likeFunc. A
714f0 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73 20  rgument pArg is 
71500 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 20  cast to a (void 
71510 2a 29 20 61 6e 64 20 6d 61 64 65 0d 0a 2a 2a 20  *) and made..** 
71520 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73      available as
71530 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73   the function us
71540 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33  er-data (sqlite3
71550 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54  _user_data()). T
71560 68 65 0d 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44  he..**     FuncD
71570 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c  ef.flags variabl
71580 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
71590 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
715a0 74 68 65 20 66 6c 61 67 73 0d 0a 2a 2a 20 20 20  the flags..**   
715b0 20 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a 2a 2f    parameter...*/
715c0 0d 0a 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49  ..#define FUNCTI
715d0 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  ON(zName, nArg, 
715e0 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63  iArg, bNC, xFunc
715f0 29 20 5c 0d 0a 20 20 7b 6e 41 72 67 2c 20 53 51  ) \..  {nArg, SQ
71600 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53  LITE_UTF8, bNC*S
71610 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
71620 4f 4c 4c 2c 20 5c 0d 0a 20 20 20 53 51 4c 49 54  OLL, \..   SQLIT
71630 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 69 41 72  E_INT_TO_PTR(iAr
71640 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c  g), 0, xFunc, 0,
71650 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 2c 20 30   0, #zName, 0, 0
71660 7d 0d 0a 23 64 65 66 69 6e 65 20 53 54 52 5f 46  }..#define STR_F
71670 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e  UNCTION(zName, n
71680 41 72 67 2c 20 70 41 72 67 2c 20 62 4e 43 2c 20  Arg, pArg, bNC, 
71690 78 46 75 6e 63 29 20 5c 0d 0a 20 20 7b 6e 41 72  xFunc) \..  {nAr
716a0 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
716b0 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f  bNC*SQLITE_FUNC_
716c0 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0d 0a 20 20 20  NEEDCOLL, \..   
716d0 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20  pArg, 0, xFunc, 
716e0 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 2c  0, 0, #zName, 0,
716f0 20 30 7d 0d 0a 23 64 65 66 69 6e 65 20 4c 49 4b   0}..#define LIK
71700 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72  EFUNC(zName, nAr
71710 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 20 5c  g, arg, flags) \
71720 0d 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  ..  {nArg, SQLIT
71730 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 28  E_UTF8, flags, (
71740 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c  void *)arg, 0, l
71750 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23  ikeFunc, 0, 0, #
71760 7a 4e 61 6d 65 2c 20 30 2c 20 30 7d 0d 0a 23 64  zName, 0, 0}..#d
71770 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45 28  efine AGGREGATE(
71780 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67  zName, nArg, arg
71790 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  , nc, xStep, xFi
717a0 6e 61 6c 29 20 5c 0d 0a 20 20 7b 6e 41 72 67 2c  nal) \..  {nArg,
717b0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63   SQLITE_UTF8, nc
717c0 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45  *SQLITE_FUNC_NEE
717d0 44 43 4f 4c 4c 2c 20 5c 0d 0a 20 20 20 53 51 4c  DCOLL, \..   SQL
717e0 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 61  ITE_INT_TO_PTR(a
717f0 72 67 29 2c 20 30 2c 20 30 2c 20 78 53 74 65 70  rg), 0, 0, xStep
71800 2c 78 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30  ,xFinal,#zName,0
71810 2c 30 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c  ,0}..../*..** Al
71820 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65 70 6f  l current savepo
71830 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ints are stored 
71840 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  in a linked list
71850 20 73 74 61 72 74 69 6e 67 20 61 74 0d 0a 2a 2a   starting at..**
71860 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f   sqlite3.pSavepo
71870 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20 65  int. The first e
71880 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c 69  lement in the li
71890 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20 72  st is the most r
718a0 65 63 65 6e 74 6c 79 0d 0a 2a 2a 20 6f 70 65 6e  ecently..** open
718b0 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61  ed savepoint. Sa
718c0 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64  vepoints are add
718d0 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62  ed to the list b
718e0 79 20 74 68 65 20 76 64 62 65 0d 0a 2a 2a 20 4f  y the vdbe..** O
718f0 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74  P_Savepoint inst
71900 72 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74  ruction...*/..st
71910 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b  ruct Savepoint {
71920 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  ..  char *zName;
71930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71940 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70          /* Savep
71950 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74  oint name (nul-t
71960 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0d 0a 20  erminated) */.. 
71970 20 69 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f   i64 nDeferredCo
71980 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
71990 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
719a0 66 20 64 65 66 65 72 72 65 64 20 66 6b 20 76 69  f deferred fk vi
719b0 6f 6c 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 53  olations */..  S
719c0 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b  avepoint *pNext;
719d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
719e0 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76     /* Parent sav
719f0 65 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20  epoint (if any) 
71a00 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
71a10 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
71a20 72 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73  re used as the s
71a30 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
71a40 74 6f 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f  to sqlite3Savepo
71a50 69 6e 74 28 29 2c 0d 0a 2a 2a 20 61 6e 64 20 61  int(),..** and a
71a60 73 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e  s the P1 argumen
71a70 74 20 74 6f 20 74 68 65 20 4f 50 5f 53 61 76 65  t to the OP_Save
71a80 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  point instructio
71a90 6e 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  n...*/..#define 
71aa0 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20  SAVEPOINT_BEGIN 
71ab0 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e 65 20       0..#define 
71ac0 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
71ad0 45 20 20 20 20 31 0d 0a 23 64 65 66 69 6e 65 20  E    1..#define 
71ae0 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
71af0 43 4b 20 20 20 32 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  CK   2....../*..
71b00 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d  ** Each SQLite m
71b10 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74  odule (virtual t
71b20 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29  able definition)
71b30 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61   is defined by a
71b40 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  n..** instance o
71b50 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
71b60 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65  structure, store
71b70 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
71b80 2e 61 4d 6f 64 75 6c 65 0d 0a 2a 2a 20 68 61 73  .aModule..** has
71b90 68 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74  h table...*/..st
71ba0 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0d 0a 20  ruct Module {.. 
71bb0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
71bc0 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20  odule *pModule; 
71bd0 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63        /* Callbac
71be0 6b 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0d 0a 20  k pointers */.. 
71bf0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
71c00 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
71c10 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61        /* Name pa
71c20 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d  ssed to create_m
71c30 6f 64 75 6c 65 28 29 20 2a 2f 0d 0a 20 20 76 6f  odule() */..  vo
71c40 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20  id *pAux;       
71c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71c60 20 20 20 2f 2a 20 70 41 75 78 20 70 61 73 73 65     /* pAux passe
71c70 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75  d to create_modu
71c80 6c 65 28 29 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  le() */..  void 
71c90 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
71ca0 20 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20   *);            
71cb0 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75  /* Module destru
71cc0 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
71cd0 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 69  ..};..../*..** i
71ce0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
71cf0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   each column of 
71d00 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20  an SQL table is 
71d10 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 61  held in an insta
71d20 6e 63 65 0d 0a 2a 2a 20 6f 66 20 74 68 69 73 20  nce..** of this 
71d30 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a  structure...*/..
71d40 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0d  struct Column {.
71d50 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
71d60 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
71d70 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20  his column */.. 
71d80 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20   Expr *pDflt;   
71d90 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c    /* Default val
71da0 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue of this colum
71db0 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 44  n */..  char *zD
71dc0 66 6c 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  flt;     /* Orig
71dd0 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
71de0 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a   default value *
71df0 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65  /..  char *zType
71e00 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79  ;     /* Data ty
71e10 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75  pe for this colu
71e20 6d 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  mn */..  char *z
71e30 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c  Coll;     /* Col
71e40 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  lating sequence.
71e50 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74    If NULL, use t
71e60 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0d 0a 20  he default */.. 
71e70 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20   u8 notNull;    
71e80 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
71e90 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c  re is a NOT NULL
71ea0 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a   constraint */..
71eb0 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20    u8 isPrimKey; 
71ec0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
71ed0 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  is column is par
71ee0 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
71ef0 20 4b 45 59 20 2a 2f 0d 0a 20 20 63 68 61 72 20   KEY */..  char 
71f00 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f  affinity;   /* O
71f10 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
71f20 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20  _AFF_... values 
71f30 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
71f40 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
71f50 41 42 4c 45 0d 0a 20 20 75 38 20 69 73 48 69 64  ABLE..  u8 isHid
71f60 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  den;     /* True
71f70 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   if this column 
71f80 69 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0d 0a  is 'hidden' */..
71f90 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  #endif..};..../*
71fa0 0d 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e  ..** A "Collatin
71fb0 67 20 53 65 71 75 65 6e 63 65 22 20 69 73 20 64  g Sequence" is d
71fc0 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73  efined by an ins
71fd0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
71fe0 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 73 74 72 75 63  lowing..** struc
71ff0 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c  ture. Conceptual
72000 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20  ly, a collating 
72010 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 74  sequence consist
72020 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 0d  s of a name and.
72030 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  .** a comparison
72040 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65   routine that de
72050 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20  fines the order 
72060 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65  of that sequence
72070 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20  ...**..** There 
72080 6d 61 79 20 74 77 6f 20 73 65 70 61 72 61 74 65  may two separate
72090 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
720a0 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   of the collatio
720b0 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0d  n function, one.
720c0 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73  .** that process
720d0 65 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38  es text in UTF-8
720e0 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53   encoding (CollS
720f0 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f  eq.xCmp) and ano
72100 74 68 65 72 20 74 68 61 74 0d 0a 2a 2a 20 70 72  ther that..** pr
72110 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e 63  ocesses text enc
72120 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28  oded in UTF-16 (
72130 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c  CollSeq.xCmp16),
72140 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69   using the machi
72150 6e 65 0d 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79  ne..** native by
72160 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61  te order. When a
72170 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
72180 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20  nce is invoked, 
72190 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0d 0a  SQLite selects..
721a0 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74  ** the version t
721b0 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65  hat will require
721c0 20 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e   the least expen
721d0 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0d 0a 2a  sive encoding..*
721e0 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20  * translations, 
721f0 69 66 20 61 6e 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  if any...**..** 
72200 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65  The CollSeq.pUse
72210 72 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c  r member variabl
72220 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61  e is an extra pa
72230 72 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73  rameter that pas
72240 73 65 64 20 69 6e 0d 0a 2a 2a 20 61 73 20 74 68  sed in..** as th
72250 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
72260 20 74 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f   to the UTF-8 co
72270 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
72280 6e 2c 20 78 43 6d 70 2e 0d 0a 2a 2a 20 43 6f 6c  n, xCmp...** Col
72290 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 73 20  lSeq.pUser16 is 
722a0 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66  the equivalent f
722b0 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 63 6f  or the UTF-16 co
722c0 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
722d0 6e 2c 0d 0a 2a 2a 20 78 43 6d 70 31 36 2e 0d 0a  n,..** xCmp16...
722e0 2a 2a 0d 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43  **..** If both C
722f0 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 20  ollSeq.xCmp and 
72300 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 61  CollSeq.xCmp16 a
72310 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e  re NULL, it mean
72320 73 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a 20 63  s that the..** c
72330 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
72340 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20  e is undefined. 
72350 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20 6f   Indices built o
72360 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0d 0a  n an undefined..
72370 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
72380 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65  uence may not be
72390 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
723a0 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 43 6f  ...*/..struct Co
723b0 6c 6c 53 65 71 20 7b 0d 0a 20 20 63 68 61 72 20  llSeq {..  char 
723c0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
723d0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
723e0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
723f0 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  ce, UTF-8 encode
72400 64 20 2a 2f 0d 0a 20 20 75 38 20 65 6e 63 3b 20  d */..  u8 enc; 
72410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
72420 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68   Text encoding h
72430 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29  andled by xCmp()
72440 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 55 73   */..  void *pUs
72450 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
72460 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  First argument t
72470 6f 20 78 43 6d 70 28 29 20 2a 2f 0d 0a 20 20 69  o xCmp() */..  i
72480 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a  nt (*xCmp)(void*
72490 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ,int, const void
724a0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  *, int, const vo
724b0 69 64 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a  id*);..  void (*
724c0 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f  xDel)(void*);  /
724d0 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
724e0 20 70 55 73 65 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d   pUser */..};...
724f0 0a 2f 2a 0d 0a 2a 2a 20 41 20 73 6f 72 74 20 6f  ./*..** A sort o
72500 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 74 68  rder can be eith
72510 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0d  er ASC or DESC..
72520 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  .*/..#define SQL
72530 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20  ITE_SO_ASC      
72540 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61   0  /* Sort in a
72550 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  scending order *
72560 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
72570 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31  E_SO_DESC      1
72580 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63    /* Sort in asc
72590 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0d  ending order */.
725a0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6c 75 6d 6e  .../*..** Column
725b0 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e   affinity types.
725c0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 75  ..**..** These u
725d0 73 65 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d  sed to have mnem
725e0 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27  onic name like '
725f0 69 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46  i' for SQLITE_AF
72600 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 0d 0a 2a  F_INTEGER and..*
72610 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 45  * 't' for SQLITE
72620 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 74 20  _AFF_TEXT.  But 
72630 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c 69  we can save a li
72640 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 20 69  ttle space and i
72650 6d 70 72 6f 76 65 0d 0a 2a 2a 20 74 68 65 20 73  mprove..** the s
72660 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 79  peed a little by
72670 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 76   numbering the v
72680 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 76  alues consecutiv
72690 65 6c 79 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 42  ely.  ..**..** B
726a0 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73  ut rather than s
726b0 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31  tart with 0 or 1
726c0 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20  , we begin with 
726d0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0d  'a'.  That way,.
726e0 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c  .** when multipl
726f0 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73  e affinity types
72700 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65   are concatenate
72710 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20  d into a string 
72720 61 6e 64 0d 0a 2a 2a 20 75 73 65 64 20 61 73 20  and..** used as 
72730 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20  the P4 operand, 
72740 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72  they will be mor
72750 65 20 72 65 61 64 61 62 6c 65 2e 0d 0a 2a 2a 0d  e readable...**.
72760 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68  .** Note also th
72770 61 74 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74  at the numeric t
72780 79 70 65 73 20 61 72 65 20 67 72 6f 75 70 65 64  ypes are grouped
72790 20 74 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61   together so tha
727a0 74 20 74 65 73 74 69 6e 67 0d 0a 2a 2a 20 66 6f  t testing..** fo
727b0 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 70 65  r a numeric type
727c0 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d   is a single com
727d0 70 61 72 69 73 6f 6e 2e 0d 0a 2a 2f 0d 0a 23 64  parison...*/..#d
727e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
727f0 5f 54 45 58 54 20 20 20 20 20 27 61 27 0d 0a 23  _TEXT     'a'..#
72800 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46  define SQLITE_AF
72810 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0d 0a  F_NONE     'b'..
72820 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72830 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0d  FF_NUMERIC  'c'.
72840 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
72850 41 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27  AFF_INTEGER  'd'
72860 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
72870 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65  _AFF_REAL     'e
72880 27 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  '....#define sql
72890 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66  ite3IsNumericAff
728a0 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 3e 3d  inity(X)  ((X)>=
728b0 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
728c0 49 43 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  IC)..../*..** Th
728d0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  e SQLITE_AFF_MAS
728e0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f  K values masks o
728f0 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61  ff the significa
72900 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0d 0a 2a  nt bits of an..*
72910 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65  * affinity value
72920 2e 20 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  . ..*/..#define 
72930 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
72940 20 20 20 20 30 78 36 37 0d 0a 0d 0a 2f 2a 0d 0a      0x67..../*..
72950 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69  ** Additional bi
72960 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 61  t values that ca
72970 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 61  n be ORed with a
72980 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 6f  n affinity witho
72990 75 74 0d 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20  ut..** changing 
729a0 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0d 0a 2a  the affinity...*
729b0 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
729c0 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30  E_JUMPIFNULL   0
729d0 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66  x08  /* jumps if
729e0 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
729f0 69 73 20 4e 55 4c 4c 20 2a 2f 0d 0a 23 64 65 66  is NULL */..#def
72a00 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  ine SQLITE_STORE
72a10 50 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a  P2      0x10  /*
72a20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e   Store result in
72a30 20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20   reg[P2] rather 
72a40 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0d 0a 23 64  than jump */..#d
72a50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c  efine SQLITE_NUL
72a60 4c 45 51 20 20 20 20 20 20 20 30 78 38 30 20 20  LEQ       0x80  
72a70 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0d  /* NULL=NULL */.
72a80 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 6f 62 6a  .../*..** An obj
72a90 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 65  ect of this type
72aa0 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
72ab0 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 62  each virtual tab
72ac0 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0d 0a 2a  le present in..*
72ad0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  * the database s
72ae0 63 68 65 6d 61 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20  chema. ..**..** 
72af0 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
72b00 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64  schema is shared
72b10 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
72b20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  one instance of 
72b30 74 68 69 73 0d 0a 2a 2a 20 73 74 72 75 63 74 75  this..** structu
72b40 72 65 20 66 6f 72 20 65 61 63 68 20 64 61 74 61  re for each data
72b50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
72b60 28 73 71 6c 69 74 65 33 2a 29 20 74 68 61 74 20  (sqlite3*) that 
72b70 75 73 65 73 20 74 68 65 20 73 68 61 72 65 64 0d  uses the shared.
72b80 0a 2a 2a 20 73 63 68 65 6d 61 2e 20 54 68 69 73  .** schema. This
72b90 20 69 73 20 62 65 63 61 75 73 65 20 65 61 63 68   is because each
72ba0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
72bb0 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 69 74  tion requires it
72bc0 73 20 6f 77 6e 20 75 6e 69 71 75 65 0d 0a 2a 2a  s own unique..**
72bd0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
72be0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68   sqlite3_vtab* h
72bf0 61 6e 64 6c 65 20 75 73 65 64 20 74 6f 20 61 63  andle used to ac
72c00 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
72c10 20 74 61 62 6c 65 20 0d 0a 2a 2a 20 69 6d 70 6c   table ..** impl
72c20 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 73 71 6c 69  ementation. sqli
72c30 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65  te3_vtab* handle
72c40 73 20 63 61 6e 20 6e 6f 74 20 62 65 20 73 68 61  s can not be sha
72c50 72 65 64 20 62 65 74 77 65 65 6e 20 0d 0a 2a 2a  red between ..**
72c60 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
72c70 74 69 6f 6e 73 2c 20 65 76 65 6e 20 77 68 65 6e  tions, even when
72c80 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
72c90 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
72ca0 61 73 65 20 0d 0a 2a 2a 20 73 63 68 65 6d 61 20  ase ..** schema 
72cb0 69 73 20 73 68 61 72 65 64 2c 20 61 73 20 74 68  is shared, as th
72cc0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
72cd0 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 20 74 68   often stores th
72ce0 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63  e database..** c
72cf0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
72d00 20 70 61 73 73 65 64 20 74 6f 20 69 74 20 76 69   passed to it vi
72d10 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29  a the xConnect()
72d20 20 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d 65   or xCreate() me
72d30 74 68 6f 64 0d 0a 2a 2a 20 64 75 72 69 6e 67 20  thod..** during 
72d40 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
72d50 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 20  nternally. This 
72d60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
72d70 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0d 0a  ion handle may..
72d80 2a 2a 20 74 68 65 6e 20 62 65 20 75 73 65 64 20  ** then be used 
72d90 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  by the virtual t
72da0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
72db0 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65  ion to access re
72dc0 61 6c 20 74 61 62 6c 65 73 20 0d 0a 2a 2a 20 77  al tables ..** w
72dd0 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 61  ithin the databa
72de0 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79  se. So that they
72df0 20 61 70 70 65 61 72 20 61 73 20 70 61 72 74 20   appear as part 
72e00 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0d  of the callers .
72e10 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  .** transaction,
72e20 20 74 68 65 73 65 20 61 63 63 65 73 73 65 73 20   these accesses 
72e30 6e 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20  need to be made 
72e40 76 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 74  via the same dat
72e50 61 62 61 73 65 20 0d 0a 2a 2a 20 63 6f 6e 6e 65  abase ..** conne
72e60 63 74 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73  ction as that us
72e70 65 64 20 74 6f 20 65 78 65 63 75 74 65 20 53 51  ed to execute SQ
72e80 4c 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20  L operations on 
72e90 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
72ea0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 56  e...**..** All V
72eb0 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 74 68  Table objects th
72ec0 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f  at correspond to
72ed0 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
72ee0 69 6e 20 61 20 73 68 61 72 65 64 0d 0a 2a 2a 20  in a shared..** 
72ef0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
72f00 61 72 65 20 69 6e 69 74 69 61 6c 6c 79 20 73 74  are initially st
72f10 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64  ored in a linked
72f20 2d 6c 69 73 74 20 70 6f 69 6e 74 65 64 20 74 6f  -list pointed to
72f30 20 62 79 0d 0a 2a 2a 20 74 68 65 20 54 61 62 6c   by..** the Tabl
72f40 65 2e 70 56 54 61 62 6c 65 20 6d 65 6d 62 65 72  e.pVTable member
72f50 20 76 61 72 69 61 62 6c 65 20 6f 66 20 74 68 65   variable of the
72f60 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 54   corresponding T
72f70 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a  able object...**
72f80 20 57 68 65 6e 20 61 6e 20 73 71 6c 69 74 65 33   When an sqlite3
72f90 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72 61  _prepare() opera
72fa0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
72fb0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76   to access the v
72fc0 69 72 74 75 61 6c 0d 0a 2a 2a 20 74 61 62 6c 65  irtual..** table
72fd0 2c 20 69 74 20 73 65 61 72 63 68 65 73 20 74 68  , it searches th
72fe0 65 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 56  e list for the V
72ff0 54 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65  Table that corre
73000 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 0d 0a 2a  sponds to the..*
73010 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
73020 63 74 69 6f 6e 20 64 6f 69 6e 67 20 74 68 65 20  ction doing the 
73030 70 72 65 70 61 72 69 6e 67 20 73 6f 20 61 73 20  preparing so as 
73040 74 6f 20 75 73 65 20 74 68 65 20 63 6f 72 72 65  to use the corre
73050 63 74 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ct..** sqlite3_v
73060 74 61 62 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74  tab* handle in t
73070 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72  he compiled quer
73080 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20  y...**..** When 
73090 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 62  an in-memory Tab
730a0 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 65 6c  le object is del
730b0 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eted (for exampl
730c0 65 20 77 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 73  e when the..** s
730d0 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 72  chema is being r
730e0 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d 65  eloaded for some
730f0 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 54   reason), the VT
73100 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65  able objects are
73110 20 6e 6f 74 20 0d 0a 2a 2a 20 64 65 6c 65 74 65   not ..** delete
73120 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65  d and the sqlite
73130 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20  3_vtab* handles 
73140 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e  are not xDisconn
73150 65 63 74 28 29 65 64 20 0d 0a 2a 2a 20 69 6d 6d  ect()ed ..** imm
73160 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61  ediately. Instea
73170 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65  d, they are move
73180 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65  d from the Table
73190 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f  .pVTable list to
731a0 0d 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e  ..** another lin
731b0 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20  ked list headed 
731c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70  by the sqlite3.p
731d0 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65  Disconnect membe
731e0 72 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 63 6f 72  r of the..** cor
731f0 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74  responding sqlit
73200 65 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  e3 structure. Th
73210 65 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65  ey are then dele
73220 74 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65  ted/xDisconnecte
73230 64 20 0d 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65  d ..** next time
73240 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20   a statement is 
73250 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73  prepared using s
73260 61 69 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68  aid sqlite3*. Th
73270 69 73 20 69 73 20 64 6f 6e 65 0d 0a 2a 2a 20 74  is is done..** t
73280 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b  o avoid deadlock
73290 20 69 73 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e   issues involvin
732a0 67 20 6d 75 6c 74 69 70 6c 65 20 73 71 6c 69 74  g multiple sqlit
732b0 65 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 65 73  e3.mutex mutexes
732c0 2e 0d 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63  ...** Refer to c
732d0 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75  omments above fu
732e0 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 56 74  nction sqlite3Vt
732f0 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66  abUnlockList() f
73300 6f 72 20 61 6e 0d 0a 2a 2a 20 65 78 70 6c 61 6e  or an..** explan
73310 61 74 69 6f 6e 20 61 73 20 74 6f 20 77 68 79 20  ation as to why 
73320 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 64  it is safe to ad
73330 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 61 6e  d an entry to an
73340 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
73350 6e 65 63 74 0d 0a 2a 2a 20 6c 69 73 74 20 77 69  nect..** list wi
73360 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 74 68  thout holding th
73370 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
73380 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d 75  sqlite3.mutex mu
73390 74 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  tex...**..** The
733a0 20 6d 65 6d 6f 72 79 20 66 6f 72 20 6f 62 6a 65   memory for obje
733b0 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 65  cts of this type
733c0 20 69 73 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63   is always alloc
733d0 61 74 65 64 20 62 79 20 0d 0a 2a 2a 20 73 71 6c  ated by ..** sql
733e0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2c 20  ite3DbMalloc(), 
733f0 75 73 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63  using the connec
73400 74 69 6f 6e 20 68 61 6e 64 6c 65 20 73 74 6f 72  tion handle stor
73410 65 64 20 69 6e 20 56 54 61 62 6c 65 2e 64 62 20  ed in VTable.db 
73420 61 73 20 0d 0a 2a 2a 20 74 68 65 20 66 69 72 73  as ..** the firs
73430 74 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d  t argument...*/.
73440 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 7b  .struct VTable {
73450 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ..  sqlite3 *db;
73460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
73470 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
73480 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  tion associated 
73490 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20  with this table 
734a0 2a 2f 0d 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  */..  Module *pM
734b0 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  od;             
734c0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f  /* Pointer to mo
734d0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
734e0 69 6f 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ion */..  sqlite
734f0 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20  3_vtab *pVtab;  
73500 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
73510 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20  o vtab instance 
73520 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20  */..  int nRef; 
73530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73540 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
73550 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
73560 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20 75 38  ructure */..  u8
73570 20 62 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20   bConstraint;   
73580 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
73590 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  if constraints a
735a0 72 65 20 73 75 70 70 6f 72 74 65 64 20 2a 2f 0d  re supported */.
735b0 0a 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  .  int iSavepoin
735c0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
735d0 44 65 70 74 68 20 6f 66 20 74 68 65 20 53 41 56  Depth of the SAV
735e0 45 50 4f 49 4e 54 20 73 74 61 63 6b 20 2a 2f 0d  EPOINT stack */.
735f0 0a 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74  .  VTable *pNext
73600 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
73610 4e 65 78 74 20 69 6e 20 6c 69 6e 6b 65 64 20 6c  Next in linked l
73620 69 73 74 20 28 73 65 65 20 61 62 6f 76 65 29 20  ist (see above) 
73630 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
73640 20 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20   Each SQL table 
73650 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69  is represented i
73660 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69  n memory by an i
73670 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0d 0a  nstance of the..
73680 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  ** following str
73690 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ucture...**..** 
736a0 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 20 74  Table.zName is t
736b0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
736c0 61 62 6c 65 2e 20 20 54 68 65 20 63 61 73 65 20  able.  The case 
736d0 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0d  of the original.
736e0 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45  .** CREATE TABLE
736f0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74   statement is st
73700 6f 72 65 64 2c 20 62 75 74 20 63 61 73 65 20 69  ored, but case i
73710 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 6e  s not significan
73720 74 20 66 6f 72 0d 0a 2a 2a 20 63 6f 6d 70 61 72  t for..** compar
73730 69 73 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  isons...**..** T
73740 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65  able.nCol is the
73750 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
73760 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
73770 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73  .  Table.aCol is
73780 20 61 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74   a..** pointer t
73790 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f  o an array of Co
737a0 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c  lumn structures,
737b0 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f   one for each co
737c0 6c 75 6d 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  lumn...**..** If
737d0 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
737e0 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
737f0 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c  Y KEY, then Tabl
73800 65 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 69  e.iPKey is the i
73810 6e 64 65 78 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  ndex of..** the 
73820 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74  column that is t
73830 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72  hat key.   Other
73840 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79  wise Table.iPKey
73850 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e   is negative.  N
73860 6f 74 65 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65  ote..** that the
73870 20 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 65   datatype of the
73880 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73   PRIMARY KEY mus
73890 74 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f 72  t be INTEGER for
738a0 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0d 0a   this field to..
738b0 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49  ** be set.  An I
738c0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
738d0 45 59 20 69 73 20 75 73 65 64 20 61 73 20 74 68  EY is used as th
738e0 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68  e rowid for each
738f0 20 72 6f 77 20 6f 66 0d 0a 2a 2a 20 74 68 65 20   row of..** the 
73900 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62  table.  If a tab
73910 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45  le has no INTEGE
73920 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74  R PRIMARY KEY, t
73930 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77  hen a random row
73940 69 64 0d 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61  id..** is genera
73950 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ted for each row
73960 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
73970 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
73980 20 69 73 20 73 65 74 20 69 66 0d 0a 2a 2a 20 74   is set if..** t
73990 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79  he table has any
739a0 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e   PRIMARY KEY, IN
739b0 54 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69  TEGER or otherwi
739c0 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 61 62 6c  se...**..** Tabl
739d0 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 20 70 61  e.tnum is the pa
739e0 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
739f0 65 20 72 6f 6f 74 20 42 54 72 65 65 20 70 61 67  e root BTree pag
73a00 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
73a10 6e 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61  n the..** databa
73a20 73 65 20 66 69 6c 65 2e 20 20 49 66 20 54 61 62  se file.  If Tab
73a30 6c 65 2e 69 44 62 20 69 73 20 74 68 65 20 69 6e  le.iDb is the in
73a40 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
73a50 61 73 65 20 74 61 62 6c 65 20 62 61 63 6b 65 6e  ase table backen
73a60 64 0d 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 2e  d..** in sqlite.
73a70 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f 72  aDb[].  0 is for
73a80 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
73a90 73 65 20 61 6e 64 20 31 20 69 73 20 66 6f 72 20  se and 1 is for 
73aa0 74 68 65 20 66 69 6c 65 20 74 68 61 74 0d 0a 2a  the file that..*
73ab0 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72  * holds temporar
73ac0 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64  y tables and ind
73ad0 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 70 68  ices.  If TF_Eph
73ae0 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0d 0a 2a  emeral is set..*
73af0 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  * then the table
73b00 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
73b10 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 74  file that is aut
73b20 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74  omatically delet
73b30 65 64 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ed..** when the 
73b40 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74  VDBE cursor to t
73b50 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73  he table is clos
73b60 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73  ed.  In this cas
73b70 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0d 0a 2a  e Table.tnum ..*
73b80 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75  * refers VDBE cu
73b90 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74  rsor number that
73ba0 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65   holds the table
73bb0 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68   open, not to th
73bc0 65 20 72 6f 6f 74 0d 0a 2a 2a 20 70 61 67 65 20  e root..** page 
73bd0 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65  number.  Transie
73be0 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  nt tables are us
73bf0 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ed to hold the r
73c00 65 73 75 6c 74 73 20 6f 66 20 61 0d 0a 2a 2a 20  esults of a..** 
73c10 73 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 61  sub-query that a
73c20 70 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f  ppears instead o
73c30 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e  f a real table n
73c40 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ame in the FROM 
73c50 63 6c 61 75 73 65 20 0d 0a 2a 2a 20 6f 66 20 61  clause ..** of a
73c60 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
73c70 74 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 54  t...*/..struct T
73c80 61 62 6c 65 20 7b 0d 0a 20 20 63 68 61 72 20 2a  able {..  char *
73c90 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f  zName;         /
73ca0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
73cb0 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0d 0a  ble or view */..
73cc0 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20    int iPKey;    
73cd0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74         /* If not
73ce0 20 6e 65 67 61 74 69 76 65 2c 20 75 73 65 20 61   negative, use a
73cf0 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68  Col[iPKey] as th
73d00 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f  e primary key */
73d10 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  ..  int nCol;   
73d20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
73d30 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
73d40 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0d 0a   this table */..
73d50 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20    Column *aCol; 
73d60 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d         /* Inform
73d70 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68  ation about each
73d80 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 49 6e   column */..  In
73d90 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20  dex *pIndex;    
73da0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51     /* List of SQ
73db0 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69  L indexes on thi
73dc0 73 20 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 69  s table. */..  i
73dd0 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20  nt tnum;        
73de0 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65      /* Root BTre
73df0 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20  e node for this 
73e00 74 61 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20  table (see note 
73e10 61 62 6f 76 65 29 20 2a 2f 0d 0a 20 20 74 52 6f  above) */..  tRo
73e20 77 63 6e 74 20 6e 52 6f 77 45 73 74 3b 20 20 20  wcnt nRowEst;   
73e30 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 72    /* Estimated r
73e40 6f 77 73 20 69 6e 20 74 61 62 6c 65 20 2d 20 66  ows in table - f
73e50 72 6f 6d 20 73 71 6c 69 74 65 5f 73 74 61 74 31  rom sqlite_stat1
73e60 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 53 65 6c   table */..  Sel
73e70 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20  ect *pSelect;   
73e80 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61    /* NULL for ta
73e90 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f  bles.  Points to
73ea0 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61   definition if a
73eb0 20 76 69 65 77 2e 20 2a 2f 0d 0a 20 20 75 31 36   view. */..  u16
73ec0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
73ed0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
73ee0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20  ointers to this 
73ef0 54 61 62 6c 65 20 2a 2f 0d 0a 20 20 75 38 20 74  Table */..  u8 t
73f00 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  abFlags;        
73f10 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a   /* Mask of TF_*
73f20 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 20 20 75 38   values */..  u8
73f30 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20   keyConf;       
73f40 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
73f50 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71   in case of uniq
73f60 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20  ueness conflict 
73f70 6f 6e 20 69 50 4b 65 79 20 2a 2f 0d 0a 20 20 46  on iPKey */..  F
73f80 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20  Key *pFKey;     
73f90 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69      /* Linked li
73fa0 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67  st of all foreig
73fb0 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74  n keys in this t
73fc0 61 62 6c 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20  able */..  char 
73fd0 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 20  *zColAff;       
73fe0 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69  /* String defini
73ff0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ng the affinity 
74000 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a  of each column *
74010 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
74020 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0d 0a 20 20  E_OMIT_CHECK..  
74030 45 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20  Expr *pCheck;   
74040 20 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20       /* The AND 
74050 6f 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e  of all CHECK con
74060 73 74 72 61 69 6e 74 73 20 2a 2f 0d 0a 23 65 6e  straints */..#en
74070 64 69 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
74080 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
74090 42 4c 45 0d 0a 20 20 69 6e 74 20 61 64 64 43 6f  BLE..  int addCo
740a0 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f  lOffset;    /* O
740b0 66 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20  ffset in CREATE 
740c0 54 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 61 64  TABLE stmt to ad
740d0 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a  d a new column *
740e0 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64  /..#endif..#ifnd
740f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
74100 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 56  IRTUALTABLE..  V
74110 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20  Table *pVTable; 
74120 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56      /* List of V
74130 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a  Table objects. *
74140 2f 0d 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65  /..  int nModule
74150 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  Arg;      /* Num
74160 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
74170 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   to the module *
74180 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f  /..  char **azMo
74190 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78  duleArg;  /* Tex
741a0 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20  t of all module 
741b0 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64  args. [0] is mod
741c0 75 6c 65 20 6e 61 6d 65 20 2a 2f 0d 0a 23 65 6e  ule name */..#en
741d0 64 69 66 0d 0a 20 20 54 72 69 67 67 65 72 20 2a  dif..  Trigger *
741e0 70 54 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 4c  pTrigger;   /* L
741f0 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
74200 73 74 6f 72 65 64 20 69 6e 20 70 53 63 68 65 6d  stored in pSchem
74210 61 20 2a 2f 0d 0a 20 20 53 63 68 65 6d 61 20 2a  a */..  Schema *
74220 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  pSchema;     /* 
74230 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74  Schema that cont
74240 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20  ains this table 
74250 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65  */..  Table *pNe
74260 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65  xtZombie;  /* Ne
74270 78 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e  xt on the Parse.
74280 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20  pZombieTab list 
74290 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
742a0 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
742b0 66 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67  for Tabe.tabFlag
742c0 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  s...*/..#define 
742d0 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 20  TF_Readonly     
742e0 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 52 65     0x01    /* Re
742f0 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74  ad-only system t
74300 61 62 6c 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  able */..#define
74310 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 20 20   TF_Ephemeral   
74320 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 41      0x02    /* A
74330 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  n ephemeral tabl
74340 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 54 46  e */..#define TF
74350 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 20  _HasPrimaryKey  
74360 20 30 78 30 34 20 20 20 20 2f 2a 20 54 61 62 6c   0x04    /* Tabl
74370 65 20 68 61 73 20 61 20 70 72 69 6d 61 72 79 20  e has a primary 
74380 6b 65 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  key */..#define 
74390 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
743a0 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 49 6e     0x08    /* In
743b0 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65  teger primary ke
743c0 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  y is autoincreme
743d0 6e 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 54  nt */..#define T
743e0 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 20  F_Virtual       
743f0 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 20    0x10    /* Is 
74400 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
74410 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e  */..#define TF_N
74420 65 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 30  eedMetadata    0
74430 78 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d  x20    /* aCol[]
74440 2e 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b  .zType and aCol[
74450 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20  ].pColl missing 
74460 2a 2f 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */......../*..**
74470 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65   Test to see whe
74480 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61  ther or not a ta
74490 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c  ble is a virtual
744a0 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
744b0 0d 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d  ..** done as a m
744c0 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20  acro so that it 
744d0 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65  will be optimize
744e0 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75  d out when virtu
744f0 61 6c 0d 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70  al..** table sup
74500 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 20  port is omitted 
74510 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0d  from the build..
74520 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
74530 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
74540 54 41 42 4c 45 0d 0a 23 20 20 64 65 66 69 6e 65  TABLE..#  define
74550 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 20   IsVirtual(X)   
74560 20 20 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61     (((X)->tabFla
74570 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
74580 21 3d 30 29 0d 0a 23 20 20 64 65 66 69 6e 65 20  !=0)..#  define 
74590 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58  IsHiddenColumn(X
745a0 29 20 28 28 58 29 2d 3e 69 73 48 69 64 64 65 6e  ) ((X)->isHidden
745b0 29 0d 0a 23 65 6c 73 65 0d 0a 23 20 20 64 65 66  )..#else..#  def
745c0 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 29  ine IsVirtual(X)
745d0 20 20 20 20 20 20 30 0d 0a 23 20 20 64 65 66 69        0..#  defi
745e0 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  ne IsHiddenColum
745f0 6e 28 58 29 20 30 0d 0a 23 65 6e 64 69 66 0d 0a  n(X) 0..#endif..
74600 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 66 6f  ../*..** Each fo
74610 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
74620 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61  aint is an insta
74630 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
74640 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0d  wing structure..
74650 0a 2a 2a 0d 0a 2a 2a 20 41 20 66 6f 72 65 69 67  .**..** A foreig
74660 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61  n key is associa
74670 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62  ted with two tab
74680 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22  les.  The "from"
74690 20 74 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 74 68   table is..** th
746a0 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e  e table that con
746b0 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45  tains the REFERE
746c0 4e 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74  NCES clause that
746d0 20 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72   creates the for
746e0 65 69 67 6e 0d 0a 2a 2a 20 6b 65 79 2e 20 20 54  eign..** key.  T
746f0 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73  he "to" table is
74700 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
74710 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  is named in the 
74720 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
74730 65 2e 0d 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20  e...** Consider 
74740 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0d 0a 2a  this example:..*
74750 2a 0d 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45  *..**     CREATE
74760 20 54 41 42 4c 45 20 65 78 31 28 0d 0a 2a 2a 20   TABLE ex1(..** 
74770 20 20 20 20 20 20 61 20 49 4e 54 45 47 45 52 20        a INTEGER 
74780 50 52 49 4d 41 52 59 20 4b 45 59 2c 0d 0a 2a 2a  PRIMARY KEY,..**
74790 20 20 20 20 20 20 20 62 20 49 4e 54 45 47 45 52         b INTEGER
747a0 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 31 20   CONSTRAINT fk1 
747b0 52 45 46 45 52 45 4e 43 45 53 20 65 78 32 28 78  REFERENCES ex2(x
747c0 29 0d 0a 2a 2a 20 20 20 20 20 29 3b 0d 0a 2a 2a  )..**     );..**
747d0 0d 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e  ..** For foreign
747e0 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65 20   key "fk1", the 
747f0 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 65  from-table is "e
74800 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d 74  x1" and the to-t
74810 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0d 0a  able is "ex2"...
74820 2a 2a 0d 0a 2a 2a 20 45 61 63 68 20 52 45 46 45  **..** Each REFE
74830 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65  RENCES clause ge
74840 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61  nerates an insta
74850 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
74860 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0d 0a  wing structure..
74870 2a 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61  ** which is atta
74880 63 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d  ched to the from
74890 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d  -table.  The to-
748a0 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65  table need not e
748b0 78 69 73 74 20 77 68 65 6e 0d 0a 2a 2a 20 74 68  xist when..** th
748c0 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20  e from-table is 
748d0 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 65 78  created.  The ex
748e0 69 73 74 65 6e 63 65 20 6f 66 20 74 68 65 20 74  istence of the t
748f0 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63  o-table is not c
74900 68 65 63 6b 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 72  hecked...*/..str
74910 75 63 74 20 46 4b 65 79 20 7b 0d 0a 20 20 54 61  uct FKey {..  Ta
74920 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20  ble *pFrom;     
74930 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e  /* Table contain
74940 69 6e 67 20 74 68 65 20 52 45 46 45 52 45 4e 43  ing the REFERENC
74950 45 53 20 63 6c 61 75 73 65 20 28 61 6b 61 3a 20  ES clause (aka: 
74960 43 68 69 6c 64 29 20 2a 2f 0d 0a 20 20 46 4b 65  Child) */..  FKe
74970 79 20 2a 70 4e 65 78 74 46 72 6f 6d 3b 20 20 2f  y *pNextFrom;  /
74980 2a 20 4e 65 78 74 20 66 6f 72 65 69 67 6e 20 6b  * Next foreign k
74990 65 79 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0d 0a  ey in pFrom */..
749a0 20 20 63 68 61 72 20 2a 7a 54 6f 3b 20 20 20 20    char *zTo;    
749b0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
749c0 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 6b 65  able that the ke
749d0 79 20 70 6f 69 6e 74 73 20 74 6f 20 28 61 6b 61  y points to (aka
749e0 3a 20 50 61 72 65 6e 74 29 20 2a 2f 0d 0a 20 20  : Parent) */..  
749f0 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f 3b 20 20  FKey *pNextTo;  
74a00 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65 69 67    /* Next foreig
74a10 6e 20 6b 65 79 20 6f 6e 20 74 61 62 6c 65 20 6e  n key on table n
74a20 61 6d 65 64 20 7a 54 6f 20 2a 2f 0d 0a 20 20 46  amed zTo */..  F
74a30 4b 65 79 20 2a 70 50 72 65 76 54 6f 3b 20 20 20  Key *pPrevTo;   
74a40 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 66 6f 72   /* Previous for
74a50 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 62 6c  eign key on tabl
74a60 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f 0d 0a  e named zTo */..
74a70 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
74a80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
74a90 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73   columns in this
74aa0 20 6b 65 79 20 2a 2f 0d 0a 20 20 2f 2a 20 45 56   key */..  /* EV
74ab0 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
74ac0 2a 2f 0d 0a 20 20 75 38 20 69 73 44 65 66 65 72  */..  u8 isDefer
74ad0 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  red;    /* True 
74ae0 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  if constraint ch
74af0 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72  ecking is deferr
74b00 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a  ed till COMMIT *
74b10 2f 0d 0a 20 20 75 38 20 61 41 63 74 69 6f 6e 5b  /..  u8 aAction[
74b20 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2];          /* 
74b30 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e  ON DELETE and ON
74b40 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 2c   UPDATE actions,
74b50 20 72 65 73 70 65 63 74 69 76 65 6c 79 20 2a 2f   respectively */
74b60 0d 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70 54  ..  Trigger *apT
74b70 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54  rigger[2];  /* T
74b80 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63 74  riggers for aAct
74b90 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f  ion[] actions */
74ba0 0d 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d  ..  struct sColM
74bb0 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67  ap {  /* Mapping
74bc0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70   of columns in p
74bd0 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20  From to columns 
74be0 69 6e 20 7a 54 6f 20 2a 2f 0d 0a 20 20 20 20 69  in zTo */..    i
74bf0 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20  nt iFrom;       
74c00 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
74c10 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f  lumn in pFrom */
74c20 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  ..    char *zCol
74c30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
74c40 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54   of column in zT
74c50 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49  o.  If 0 use PRI
74c60 4d 41 52 59 20 4b 45 59 20 2a 2f 0d 0a 20 20 7d  MARY KEY */..  }
74c70 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20   aCol[1];       
74c80 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
74c90 72 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63  r each of nCol c
74ca0 6f 6c 75 6d 6e 20 73 20 2a 2f 0d 0a 7d 3b 0d 0a  olumn s */..};..
74cb0 0d 0a 2f 2a 0d 0a 2a 2a 20 53 51 4c 69 74 65 20  ../*..** SQLite 
74cc0 73 75 70 70 6f 72 74 73 20 6d 61 6e 79 20 64 69  supports many di
74cd0 66 66 65 72 65 6e 74 20 77 61 79 73 20 74 6f 20  fferent ways to 
74ce0 72 65 73 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72  resolve a constr
74cf0 61 69 6e 74 0d 0a 2a 2a 20 65 72 72 6f 72 2e 20  aint..** error. 
74d00 20 52 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65 73   ROLLBACK proces
74d10 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20  sing means that 
74d20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  a constraint vio
74d30 6c 61 74 69 6f 6e 0d 0a 2a 2a 20 63 61 75 73 65  lation..** cause
74d40 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  s the operation 
74d50 69 6e 20 70 72 6f 63 65 73 73 20 74 6f 20 66 61  in process to fa
74d60 69 6c 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63  il and for the c
74d70 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
74d80 6f 6e 0d 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c  on..** to be rol
74d90 6c 65 64 20 62 61 63 6b 2e 20 20 41 42 4f 52 54  led back.  ABORT
74da0 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e   processing mean
74db0 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  s the operation 
74dc0 69 6e 20 70 72 6f 63 65 73 73 0d 0a 2a 2a 20 66  in process..** f
74dd0 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 70 72 69  ails and any pri
74de0 6f 72 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20  or changes from 
74df0 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 61 74 69  that one operati
74e00 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 20 6f 75  on are backed ou
74e10 74 2c 0d 0a 2a 2a 20 62 75 74 20 74 68 65 20 74  t,..** but the t
74e20 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f  ransaction is no
74e30 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20  t rolled back.  
74e40 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e 67 20  FAIL processing 
74e50 6d 65 61 6e 73 20 74 68 61 74 0d 0a 2a 2a 20 74  means that..** t
74e60 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20  he operation in 
74e70 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 61  progress stops a
74e80 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  nd returns an er
74e90 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 70  ror code.  But p
74ea0 72 69 6f 72 0d 0a 2a 2a 20 63 68 61 6e 67 65 73  rior..** changes
74eb0 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65   due to the same
74ec0 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e   operation are n
74ed0 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e  ot backed out an
74ee0 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0d 0a 2a  d no rollback..*
74ef0 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52  * occurs.  IGNOR
74f00 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  E means that the
74f10 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20   particular row 
74f20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20  that caused the 
74f30 63 6f 6e 73 74 72 61 69 6e 74 0d 0a 2a 2a 20 65  constraint..** e
74f40 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65  rror is not inse
74f50 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e  rted or updated.
74f60 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e    Processing con
74f70 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72  tinues and no er
74f80 72 6f 72 0d 0a 2a 2a 20 69 73 20 72 65 74 75 72  ror..** is retur
74f90 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65  ned.  REPLACE me
74fa0 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73  ans that preexis
74fb0 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f  ting database ro
74fc0 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0d 0a  ws that caused..
74fd0 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73  ** a UNIQUE cons
74fe0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
74ff0 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20   are removed so 
75000 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73  that the new ins
75010 65 72 74 20 6f 72 0d 0a 2a 2a 20 75 70 64 61 74  ert or..** updat
75020 65 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20  e can proceed.  
75030 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69  Processing conti
75040 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f  nues and no erro
75050 72 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0d 0a  r is reported...
75060 2a 2a 0d 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c  **..** RESTRICT,
75070 20 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41   SETNULL, and CA
75080 53 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61 70  SCADE actions ap
75090 70 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65  ply only to fore
750a0 69 67 6e 20 6b 65 79 73 2e 0d 0a 2a 2a 20 52 45  ign keys...** RE
750b0 53 54 52 49 43 54 20 69 73 20 74 68 65 20 73 61  STRICT is the sa
750c0 6d 65 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20  me as ABORT for 
750d0 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67  IMMEDIATE foreig
750e0 6e 20 6b 65 79 73 20 61 6e 64 20 74 68 65 0d 0a  n keys and the..
750f0 2a 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42  ** same as ROLLB
75100 41 43 4b 20 66 6f 72 20 44 45 46 45 52 52 45 44  ACK for DEFERRED
75110 20 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20   keys.  SETNULL 
75120 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66  means that the f
75130 6f 72 65 69 67 6e 0d 0a 2a 2a 20 6b 65 79 20 69  oreign..** key i
75140 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20  s set to NULL.  
75150 43 41 53 43 41 44 45 20 6d 65 61 6e 73 20 74 68  CASCADE means th
75160 61 74 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55  at a DELETE or U
75170 50 44 41 54 45 20 6f 66 20 74 68 65 0d 0a 2a 2a  PDATE of the..**
75180 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c   referenced tabl
75190 65 20 72 6f 77 20 69 73 20 70 72 6f 70 61 67 61  e row is propaga
751a0 74 65 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 77  ted into the row
751b0 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0d   that holds the.
751c0 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  .** foreign key.
751d0 0d 0a 2a 2a 20 0d 0a 2a 2a 20 54 68 65 20 66 6f  ..** ..** The fo
751e0 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69 63  llowing symbolic
751f0 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 65 64   values are used
75200 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68   to record which
75210 20 74 79 70 65 0d 0a 2a 2a 20 6f 66 20 61 63 74   type..** of act
75220 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0d 0a 2a 2f  ion to take...*/
75230 0d 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e  ..#define OE_Non
75240 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65  e     0   /* The
75250 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61  re is no constra
75260 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0d  int to check */.
75270 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c  .#define OE_Roll
75280 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c  back 1   /* Fail
75290 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61   the operation a
752a0 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  nd rollback the 
752b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a  transaction */..
752c0 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74  #define OE_Abort
752d0 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20      2   /* Back 
752e0 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20  out changes but 
752f0 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  do no rollback t
75300 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a 23  ransaction */..#
75310 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20  define OE_Fail  
75320 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74     3   /* Stop t
75330 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74  he operation but
75340 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72   leave all prior
75350 20 63 68 61 6e 67 65 73 20 2a 2f 0d 0a 23 64 65   changes */..#de
75360 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20  fine OE_Ignore  
75370 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74   4   /* Ignore t
75380 68 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74  he error. Do not
75390 20 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f   do the INSERT o
753a0 72 20 55 50 44 41 54 45 20 2a 2f 0d 0a 23 64 65  r UPDATE */..#de
753b0 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20  fine OE_Replace 
753c0 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65   5   /* Delete e
753d0 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20  xisting record, 
753e0 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f  then do INSERT o
753f0 72 20 55 50 44 41 54 45 20 2a 2f 0d 0a 0d 0a 23  r UPDATE */....#
75400 64 65 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69  define OE_Restri
75410 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f  ct 6   /* OE_Abo
75420 72 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  rt for IMMEDIATE
75430 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f  , OE_Rollback fo
75440 72 20 44 45 46 45 52 52 45 44 20 2a 2f 0d 0a 23  r DEFERRED */..#
75450 64 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c  define OE_SetNul
75460 6c 20 20 37 20 20 20 2f 2a 20 53 65 74 20 74 68  l  7   /* Set th
75470 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61  e foreign key va
75480 6c 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0d 0a  lue to NULL */..
75490 23 64 65 66 69 6e 65 20 4f 45 5f 53 65 74 44 66  #define OE_SetDf
754a0 6c 74 20 20 38 20 20 20 2f 2a 20 53 65 74 20 74  lt  8   /* Set t
754b0 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76  he foreign key v
754c0 61 6c 75 65 20 74 6f 20 69 74 73 20 64 65 66 61  alue to its defa
754d0 75 6c 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ult */..#define 
754e0 4f 45 5f 43 61 73 63 61 64 65 20 20 39 20 20 20  OE_Cascade  9   
754f0 2f 2a 20 43 61 73 63 61 64 65 20 74 68 65 20 63  /* Cascade the c
75500 68 61 6e 67 65 73 20 2a 2f 0d 0a 0d 0a 23 64 65  hanges */....#de
75510 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c 74 20  fine OE_Default 
75520 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 61 74 65   99  /* Do whate
75530 76 65 72 20 74 68 65 20 64 65 66 61 75 6c 74 20  ver the default 
75540 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0d 0a 0d 0a  action is */....
75550 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74  ../*..** An inst
75560 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
75570 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
75580 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
75590 20 66 69 72 73 74 0d 0a 2a 2a 20 61 72 67 75 6d   first..** argum
755a0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64  ent to sqlite3Vd
755b0 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64  beKeyCompare and
755c0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74   is used to cont
755d0 72 6f 6c 20 74 68 65 20 0d 0a 2a 2a 20 63 6f 6d  rol the ..** com
755e0 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74  parison of the t
755f0 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0d 0a  wo index keys...
75600 2a 2f 0d 0a 73 74 72 75 63 74 20 4b 65 79 49 6e  */..struct KeyIn
75610 66 6f 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  fo {..  sqlite3 
75620 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  *db;        /* T
75630 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
75640 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 75 38 20  ection */..  u8 
75650 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  enc;            
75660 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e   /* Text encodin
75670 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20 53  g - one of the S
75680 51 4c 49 54 45 5f 55 54 46 2a 20 76 61 6c 75 65  QLITE_UTF* value
75690 73 20 2a 2f 0d 0a 20 20 75 31 36 20 6e 46 69 65  s */..  u16 nFie
756a0 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ld;         /* N
756b0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
756c0 20 69 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0d 0a   in aColl[] */..
756d0 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72    u8 *aSortOrder
756e0 3b 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72  ;     /* Sort or
756f0 64 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  der for each col
75700 75 6d 6e 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  umn.  May be NUL
75710 4c 20 2a 2f 0d 0a 20 20 43 6f 6c 6c 53 65 71 20  L */..  CollSeq 
75720 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43  *aColl[1];  /* C
75730 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
75740 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  e for each term 
75750 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0d 0a 7d  of the key */..}
75760 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69  ;..../*..** An i
75770 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
75780 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
75790 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61  re holds informa
757a0 74 69 6f 6e 20 61 62 6f 75 74 20 61 0d 0a 2a 2a  tion about a..**
757b0 20 73 69 6e 67 6c 65 20 69 6e 64 65 78 20 72 65   single index re
757c0 63 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c  cord that has al
757d0 72 65 61 64 79 20 62 65 65 6e 20 70 61 72 73 65  ready been parse
757e0 64 20 6f 75 74 20 69 6e 74 6f 20 69 6e 64 69 76  d out into indiv
757f0 69 64 75 61 6c 0d 0a 2a 2a 20 76 61 6c 75 65 73  idual..** values
75800 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 72 65 63 6f  ...**..** A reco
75810 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20  rd is an object 
75820 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  that contains on
75830 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 73  e or more fields
75840 20 6f 66 20 64 61 74 61 2e 0d 0a 2a 2a 20 52 65   of data...** Re
75850 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74  cords are used t
75860 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74  o store the cont
75870 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72  ent of a table r
75880 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0d  ow and to store.
75890 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61  .** the key of a
758a0 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62  n index.  A blob
758b0 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72   encoding of a r
758c0 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64  ecord is created
758d0 20 62 79 0d 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d   by..** the OP_M
758e0 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
758f0 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64   of the VDBE and
75900 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65 64   is disassembled
75910 20 62 79 20 74 68 65 0d 0a 2a 2a 20 4f 50 5f 43   by the..** OP_C
75920 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0d 0a 2a  olumn opcode...*
75930 2a 0d 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  *..** This struc
75940 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63  ture holds a rec
75950 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72  ord that has alr
75960 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73  eady been disass
75970 65 6d 62 6c 65 64 0d 0a 2a 2a 20 69 6e 74 6f 20  embled..** into 
75980 69 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20  its constituent 
75990 66 69 65 6c 64 73 2e 0d 0a 2a 2f 0d 0a 73 74 72  fields...*/..str
759a0 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  uct UnpackedReco
759b0 72 64 20 7b 0d 0a 20 20 4b 65 79 49 6e 66 6f 20  rd {..  KeyInfo 
759c0 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43  *pKeyInfo;  /* C
759d0 6f 6c 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72  ollation and sor
759e0 74 2d 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74  t-order informat
759f0 69 6f 6e 20 2a 2f 0d 0a 20 20 75 31 36 20 6e 46  ion */..  u16 nF
75a00 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  ield;         /*
75a10 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
75a20 65 73 20 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f  es in apMem[] */
75a30 0d 0a 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20  ..  u8 flags;   
75a40 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65          /* Boole
75a50 61 6e 20 73 65 74 74 69 6e 67 73 2e 20 20 55 4e  an settings.  UN
75a60 50 41 43 4b 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77  PACKED_... below
75a70 20 2a 2f 0d 0a 20 20 69 36 34 20 72 6f 77 69 64   */..  i64 rowid
75a80 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ;          /* Us
75a90 65 64 20 62 79 20 55 4e 50 41 43 4b 45 44 5f 50  ed by UNPACKED_P
75aa0 52 45 46 49 58 5f 53 45 41 52 43 48 20 2a 2f 0d  REFIX_SEARCH */.
75ab0 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20  .  Mem *aMem;   
75ac0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
75ad0 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
75ae0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
75af0 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f   of UnpackedReco
75b00 72 64 2e 66 6c 61 67 73 0d 0a 2a 2f 0d 0a 23 64  rd.flags..*/..#d
75b10 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49  efine UNPACKED_I
75b20 4e 43 52 4b 45 59 20 20 20 20 20 20 20 30 78 30  NCRKEY       0x0
75b30 31 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20  1  /* Make this 
75b40 6b 65 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c  key an epsilon l
75b50 61 72 67 65 72 20 2a 2f 0d 0a 23 64 65 66 69 6e  arger */..#defin
75b60 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  e UNPACKED_PREFI
75b70 58 5f 4d 41 54 43 48 20 20 30 78 30 32 20 20 2f  X_MATCH  0x02  /
75b80 2a 20 41 20 70 72 65 66 69 78 20 6d 61 74 63 68  * A prefix match
75b90 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f   is considered O
75ba0 4b 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 55 4e  K */..#define UN
75bb0 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45  PACKED_PREFIX_SE
75bc0 41 52 43 48 20 30 78 30 34 20 20 2f 2a 20 49 67  ARCH 0x04  /* Ig
75bd0 6e 6f 72 65 20 66 69 6e 61 6c 20 28 72 6f 77 69  nore final (rowi
75be0 64 29 20 66 69 65 6c 64 20 2a 2f 0d 0a 0d 0a 2f  d) field */..../
75bf0 2a 0d 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 69  *..** Each SQL i
75c00 6e 64 65 78 20 69 73 20 72 65 70 72 65 73 65 6e  ndex is represen
75c10 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79  ted in memory by
75c20 20 61 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65   an..** instance
75c30 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
75c40 67 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a  g structure...**
75c50 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  ..** The columns
75c60 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
75c70 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
75c80 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
75c90 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20 61 69  ed..** by the ai
75ca0 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f  Column[] field o
75cb0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
75cc0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
75cd0 73 75 70 70 6f 73 65 0d 0a 2a 2a 20 77 65 20 68  suppose..** we h
75ce0 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ave the followin
75cf0 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  g table and inde
75d00 78 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 43  x:..**..**     C
75d10 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28  REATE TABLE Ex1(
75d20 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20  c1 int, c2 int, 
75d30 63 33 20 74 65 78 74 29 3b 0d 0a 2a 2a 20 20 20  c3 text);..**   
75d40 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45    CREATE INDEX E
75d50 78 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29  x2 ON Ex1(c3,c1)
75d60 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74 68 65  ;..**..** In the
75d70 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
75d80 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 31 2c   describing Ex1,
75d90 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65   nCol==3 because
75da0 20 74 68 65 72 65 20 61 72 65 0d 0a 2a 2a 20 74   there are..** t
75db0 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  hree columns in 
75dc0 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 74  the table.  In t
75dd0 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
75de0 72 65 20 64 65 73 63 72 69 62 69 6e 67 0d 0a 2a  re describing..*
75df0 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d  * Ex2, nColumn==
75e00 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65  2 since 2 of the
75e10 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78   3 columns of Ex
75e20 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0d 0a  1 are indexed...
75e30 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
75e40 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20  aiColumn is {2, 
75e50 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d  0}.  aiColumn[0]
75e60 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20  ==2 because the 
75e70 0d 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d  ..** first colum
75e80 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  n to be indexed 
75e90 28 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65  (c3) has an inde
75ea0 78 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43  x of 2 in Ex1.aC
75eb0 6f 6c 5b 5d 2e 0d 0a 2a 2a 20 54 68 65 20 73 65  ol[]...** The se
75ec0 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62  cond column to b
75ed0 65 20 69 6e 64 65 78 65 64 20 28 63 31 29 20 68  e indexed (c1) h
75ee0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30  as an index of 0
75ef0 20 69 6e 0d 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c   in..** Ex1.aCol
75f00 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61 69  [], hence Ex2.ai
75f10 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0d 0a 2a  Column[1]==0...*
75f20 2a 0d 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e  *..** The Index.
75f30 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 65  onError field de
75f40 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72  termines whether
75f50 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65   or not the inde
75f60 78 65 64 20 63 6f 6c 75 6d 6e 73 0d 0a 2a 2a 20  xed columns..** 
75f70 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
75f80 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
75f90 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
75fa0 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
75fb0 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0d 0a 2a 2a 20  or=OE_None,..** 
75fc0 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73  it means this is
75fd0 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e   not a unique in
75fe0 64 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20  dex.  Otherwise 
75ff0 69 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69  it is a unique i
76000 6e 64 65 78 0d 0a 2a 2a 20 61 6e 64 20 74 68 65  ndex..** and the
76010 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e   value of Index.
76020 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65  onError indicate
76030 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c   the which confl
76040 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0d  ict resolution .
76050 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f  .** algorithm to
76060 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 72   employ whenever
76070 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
76080 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61 20  ade to insert a 
76090 6e 6f 6e 2d 75 6e 69 71 75 65 0d 0a 2a 2a 20 65  non-unique..** e
760a0 6c 65 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 72  lement...*/..str
760b0 75 63 74 20 49 6e 64 65 78 20 7b 0d 0a 20 20 63  uct Index {..  c
760c0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
760d0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
760e0 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20  index */..  int 
760f0 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20  nColumn;     /* 
76100 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
76110 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75  s in the table u
76120 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65  sed by this inde
76130 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 69 43  x */..  int *aiC
76140 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63  olumn;   /* Whic
76150 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73  h columns are us
76160 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
76170 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0d 0a  .  1st is 0 */..
76180 20 20 74 52 6f 77 63 6e 74 20 2a 61 69 52 6f 77    tRowcnt *aiRow
76190 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f  Est; /* Result o
761a0 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20  f ANALYZE: Est. 
761b0 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79  rows selected by
761c0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0d   each column */.
761d0 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  .  Table *pTable
761e0 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74  ;   /* The SQL t
761f0 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78  able being index
76200 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 74 6e 75  ed */..  int tnu
76210 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67  m;        /* Pag
76220 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f  e containing roo
76230 74 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20  t of this index 
76240 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
76250 20 2a 2f 0d 0a 20 20 75 38 20 6f 6e 45 72 72 6f   */..  u8 onErro
76260 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62  r;      /* OE_Ab
76270 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
76280 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f  OE_Replace, or O
76290 45 5f 4e 6f 6e 65 20 2a 2f 0d 0a 20 20 75 38 20  E_None */..  u8 
762a0 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a  autoIndex;    /*
762b0 20 54 72 75 65 20 69 66 20 69 73 20 61 75 74 6f   True if is auto
762c0 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65  matically create
762d0 64 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55 45  d (ex: by UNIQUE
762e0 29 20 2a 2f 0d 0a 20 20 75 38 20 62 55 6e 6f 72  ) */..  u8 bUnor
762f0 64 65 72 65 64 3b 20 20 20 2f 2a 20 55 73 65 20  dered;   /* Use 
76300 74 68 69 73 20 69 6e 64 65 78 20 66 6f 72 20 3d  this index for =
76310 3d 20 6f 72 20 49 4e 20 71 75 65 72 69 65 73 20  = or IN queries 
76320 6f 6e 6c 79 20 2a 2f 0d 0a 20 20 63 68 61 72 20  only */..  char 
76330 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53  *zColAff;   /* S
76340 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74  tring defining t
76350 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65  he affinity of e
76360 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20  ach column */.. 
76370 20 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20   Index *pNext;  
76380 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e    /* The next in
76390 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
763a0 69 74 68 20 74 68 65 20 73 61 6d 65 20 74 61 62  ith the same tab
763b0 6c 65 20 2a 2f 0d 0a 20 20 53 63 68 65 6d 61 20  le */..  Schema 
763c0 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68  *pSchema; /* Sch
763d0 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ema containing t
763e0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  his index */..  
763f0 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20  u8 *aSortOrder; 
76400 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 7a   /* Array of siz
76410 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e  e Index.nColumn.
76420 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c   True==DESC, Fal
76430 73 65 3d 3d 41 53 43 20 2a 2f 0d 0a 20 20 63 68  se==ASC */..  ch
76440 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f  ar **azColl;   /
76450 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61  * Array of colla
76460 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
76470 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f  mes for index */
76480 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
76490 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20 20  ENABLE_STAT3..  
764a0 69 6e 74 20 6e 53 61 6d 70 6c 65 3b 20 20 20 20  int nSample;    
764b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
764c0 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
764d0 6e 20 61 53 61 6d 70 6c 65 5b 5d 20 2a 2f 0d 0a  n aSample[] */..
764e0 20 20 74 52 6f 77 63 6e 74 20 61 76 67 45 71 3b    tRowcnt avgEq;
764f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 76             /* Av
76500 65 72 61 67 65 20 6e 45 71 20 76 61 6c 75 65 20  erage nEq value 
76510 66 6f 72 20 6b 65 79 20 76 61 6c 75 65 73 20 6e  for key values n
76520 6f 74 20 69 6e 20 61 53 61 6d 70 6c 65 20 2a 2f  ot in aSample */
76530 0d 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20  ..  IndexSample 
76540 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20  *aSample;    /* 
76550 53 61 6d 70 6c 65 73 20 6f 66 20 74 68 65 20 6c  Samples of the l
76560 65 66 74 2d 6d 6f 73 74 20 6b 65 79 20 2a 2f 0d  eft-most key */.
76570 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f  .#endif..};..../
76580 2a 0d 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c  *..** Each sampl
76590 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
765a0 73 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61 62  sqlite_stat3 tab
765b0 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  le is represente
765c0 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0d 0a 2a 2a  d in memory ..**
765d0 20 75 73 69 6e 67 20 61 20 73 74 72 75 63 74 75   using a structu
765e0 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e  re of this type.
765f0 20 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 61 74    See documentat
76600 69 6f 6e 20 61 74 20 74 68 65 20 74 6f 70 20 6f  ion at the top o
76610 66 20 74 68 65 0d 0a 2a 2a 20 61 6e 61 6c 79 7a  f the..** analyz
76620 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 20  e.c source file 
76630 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
76640 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d  nformation...*/.
76650 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d  .struct IndexSam
76660 70 6c 65 20 7b 0d 0a 20 20 75 6e 69 6f 6e 20 7b  ple {..  union {
76670 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20  ..    char *z;  
76680 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69        /* Value i
76690 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 54  f eType is SQLIT
766a0 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45  E_TEXT or SQLITE
766b0 5f 42 4c 4f 42 20 2a 2f 0d 0a 20 20 20 20 64 6f  _BLOB */..    do
766c0 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a  uble r;       /*
766d0 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20   Value if eType 
766e0 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  is SQLITE_FLOAT 
766f0 2a 2f 0d 0a 20 20 20 20 69 36 34 20 69 3b 20 20  */..    i64 i;  
76700 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
76710 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c   if eType is SQL
76720 49 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0d 0a  ITE_INTEGER */..
76730 20 20 7d 20 75 3b 0d 0a 20 20 75 38 20 65 54 79    } u;..  u8 eTy
76740 70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  pe;         /* S
76750 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49  QLITE_NULL, SQLI
76760 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65  TE_INTEGER ... e
76770 74 63 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42  tc. */..  int nB
76780 79 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  yte;        /* S
76790 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 20 74  ize in byte of t
767a0 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0d  ext or blob. */.
767b0 0a 20 20 74 52 6f 77 63 6e 74 20 6e 45 71 3b 20  .  tRowcnt nEq; 
767c0 20 20 20 20 20 2f 2a 20 45 73 74 2e 20 6e 75 6d       /* Est. num
767d0 62 65 72 20 6f 66 20 72 6f 77 73 20 77 68 65 72  ber of rows wher
767e0 65 20 74 68 65 20 6b 65 79 20 65 71 75 61 6c 73  e the key equals
767f0 20 74 68 69 73 20 73 61 6d 70 6c 65 20 2a 2f 0d   this sample */.
76800 0a 20 20 74 52 6f 77 63 6e 74 20 6e 4c 74 3b 20  .  tRowcnt nLt; 
76810 20 20 20 20 20 2f 2a 20 45 73 74 2e 20 6e 75 6d       /* Est. num
76820 62 65 72 20 6f 66 20 72 6f 77 73 20 77 68 65 72  ber of rows wher
76830 65 20 6b 65 79 20 69 73 20 6c 65 73 73 20 74 68  e key is less th
76840 61 6e 20 74 68 69 73 20 73 61 6d 70 6c 65 20 2a  an this sample *
76850 2f 0d 0a 20 20 74 52 6f 77 63 6e 74 20 6e 44 4c  /..  tRowcnt nDL
76860 74 3b 20 20 20 20 20 2f 2a 20 45 73 74 2e 20 6e  t;     /* Est. n
76870 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63  umber of distinc
76880 74 20 6b 65 79 73 20 6c 65 73 73 20 74 68 61 6e  t keys less than
76890 20 74 68 69 73 20 73 61 6d 70 6c 65 20 2a 2f 0d   this sample */.
768a0 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61  .};..../*..** Ea
768b0 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20  ch token coming 
768c0 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 78 65 72  out of the lexer
768d0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
768e0 6f 66 0d 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  of..** this stru
768f0 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61  cture.  Tokens a
76900 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 73 20  re also used as 
76910 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
76920 73 73 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e  ssion...**..** N
76930 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d  ote if Token.z==
76940 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e  0 then Token.dyn
76950 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65   and Token.n are
76960 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0d 0a   undefined and..
76970 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72  ** may contain r
76980 61 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44  andom values.  D
76990 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61  o not make any a
769a0 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74  ssumptions about
769b0 20 54 6f 6b 65 6e 2e 64 79 6e 0d 0a 2a 2a 20 61   Token.dyn..** a
769c0 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20  nd Token.n when 
769d0 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0d 0a 2a 2f 0d  Token.z==0...*/.
769e0 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0d  .struct Token {.
769f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
76a00 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66  ;     /* Text of
76a10 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f 74   the token.  Not
76a20 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64   NULL-terminated
76a30 21 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64  ! */..  unsigned
76a40 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a 20 4e 75   int n;    /* Nu
76a50 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
76a60 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65 6e  rs in this token
76a70 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
76a80 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
76a90 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
76aa0 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
76ab0 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67  tion needed to g
76ac0 65 6e 65 72 61 74 65 0d 0a 2a 2a 20 63 6f 64 65  enerate..** code
76ad0 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68   for a SELECT th
76ae0 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72  at contains aggr
76af0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
76b00 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 45 78 70 72  ..**..** If Expr
76b10 2e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  .op==TK_AGG_COLU
76b20 4d 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e  MN or TK_AGG_FUN
76b30 43 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e  CTION then Expr.
76b40 70 41 67 67 49 6e 66 6f 20 69 73 20 61 0d 0a 2a  pAggInfo is a..*
76b50 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  * pointer to thi
76b60 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  s structure.  Th
76b70 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66  e Expr.iColumn f
76b80 69 65 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65  ield is the inde
76b90 78 20 69 6e 0d 0a 2a 2a 20 41 67 67 49 6e 66 6f  x in..** AggInfo
76ba0 2e 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e  .aCol[] or AggIn
76bb0 66 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e  fo.aFunc[] of in
76bc0 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64  formation needed
76bd0 20 74 6f 20 67 65 6e 65 72 61 74 65 0d 0a 2a 2a   to generate..**
76be0 20 63 6f 64 65 20 66 6f 72 20 74 68 61 74 20 6e   code for that n
76bf0 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 67 67  ode...**..** Agg
76c00 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 61 6e  Info.pGroupBy an
76c10 64 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 2e  d AggInfo.aFunc.
76c20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 66  pExpr point to f
76c30 69 65 6c 64 73 20 77 69 74 68 69 6e 20 74 68 65  ields within the
76c40 0d 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 65  ..** original Se
76c50 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20 74  lect structure t
76c60 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
76c70 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
76c80 6e 74 2e 20 20 54 68 65 73 65 0d 0a 2a 2a 20 66  nt.  These..** f
76c90 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20 6e 65 65  ields do not nee
76ca0 64 20 74 6f 20 62 65 20 66 72 65 65 64 20 77 68  d to be freed wh
76cb0 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20  en deallocating 
76cc0 74 68 65 20 41 67 67 49 6e 66 6f 20 73 74 72 75  the AggInfo stru
76cd0 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  cture...*/..stru
76ce0 63 74 20 41 67 67 49 6e 66 6f 20 7b 0d 0a 20 20  ct AggInfo {..  
76cf0 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20  u8 directMode;  
76d00 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
76d10 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65  t rendering mode
76d20 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61   means take data
76d30 20 64 69 72 65 63 74 6c 79 0d 0a 20 20 20 20 20   directly..     
76d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76d50 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75       ** from sou
76d60 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 65  rce tables rathe
76d70 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75  r than from accu
76d80 6d 75 6c 61 74 6f 72 73 20 2a 2f 0d 0a 20 20 75  mulators */..  u
76d90 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
76da0 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
76db0 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
76dc0 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
76dd0 69 6e 64 65 78 20 72 61 74 68 65 72 0d 0a 20 20  index rather..  
76de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76df0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20          ** than 
76e00 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
76e10 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 6f 72 74 69   */..  int sorti
76e20 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f  ngIdx;         /
76e30 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
76e40 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  of the sorting i
76e50 6e 64 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 73  ndex */..  int s
76e60 6f 72 74 69 6e 67 49 64 78 50 54 61 62 3b 20 20  ortingIdxPTab;  
76e70 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
76e80 62 65 72 20 6f 66 20 70 73 65 75 64 6f 2d 74 61  ber of pseudo-ta
76e90 62 6c 65 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69  ble */..  ExprLi
76ea0 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
76eb0 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62    /* The group b
76ec0 79 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69  y clause */..  i
76ed0 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  nt nSortingColum
76ee0 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
76ef0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
76f00 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
76f10 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 41 67   */..  struct Ag
76f20 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f  gInfo_col {    /
76f30 2a 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  * For each colum
76f40 6e 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65  n used in source
76f50 20 74 61 62 6c 65 73 20 2a 2f 0d 0a 20 20 20 20   tables */..    
76f60 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
76f70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
76f80 63 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  ce table */..   
76f90 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20   int iTable;    
76fa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
76fb0 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
76fc0 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a  e source table *
76fd0 2f 0d 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  /..    int iColu
76fe0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
76ff0 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
77000 20 77 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72   within the sour
77010 63 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  ce table */..   
77020 20 69 6e 74 20 69 53 6f 72 74 65 72 43 6f 6c 75   int iSorterColu
77030 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  mn;       /* Col
77040 75 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68  umn number in th
77050 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
77060 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d  */..    int iMem
77070 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
77080 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74   /* Memory locat
77090 69 6f 6e 20 74 68 61 74 20 61 63 74 73 20 61 73  ion that acts as
770a0 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0d   accumulator */.
770b0 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
770c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
770d0 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 65 78   The original ex
770e0 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 7d  pression */..  }
770f0 20 2a 61 43 6f 6c 3b 0d 0a 20 20 69 6e 74 20 6e   *aCol;..  int n
77100 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
77110 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
77120 75 73 65 64 20 65 6e 74 72 69 65 73 20 69 6e 20  used entries in 
77130 61 43 6f 6c 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74  aCol[] */..  int
77140 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20 20   nColumnAlloc;  
77150 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
77160 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65  f slots allocate
77170 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f 0d  d for aCol[] */.
77180 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61  .  int nAccumula
77190 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  tor;       /* Nu
771a0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
771b0 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67  that show throug
771c0 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e  h to the output.
771d0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
771e0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41              ** A
771f0 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e  dditional column
77200 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20  s are used only 
77210 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  as parameters to
77220 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
77230 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
77240 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
77250 6e 73 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  ns */..  struct 
77260 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20  AggInfo_func {  
77270 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67   /* For each agg
77280 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
77290 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45  */..    Expr *pE
772a0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
772b0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65   /* Expression e
772c0 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63  ncoding the func
772d0 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 46 75 6e  tion */..    Fun
772e0 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
772f0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67        /* The agg
77300 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
77310 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
77320 2f 0d 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b  /..    int iMem;
77330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77340 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  /* Memory locati
77350 6f 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20  on that acts as 
77360 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0d 0a  accumulator */..
77370 20 20 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63      int iDistinc
77380 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
77390 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  Ephemeral table 
773a0 75 73 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20  used to enforce 
773b0 44 49 53 54 49 4e 43 54 20 2a 2f 0d 0a 20 20 7d  DISTINCT */..  }
773c0 20 2a 61 46 75 6e 63 3b 0d 0a 20 20 69 6e 74 20   *aFunc;..  int 
773d0 6e 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  nFunc;          
773e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
773f0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 46 75 6e   entries in aFun
77400 63 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46  c[] */..  int nF
77410 75 6e 63 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  uncAlloc;       
77420 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
77430 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lots allocated f
77440 6f 72 20 61 46 75 6e 63 5b 5d 20 2a 2f 0d 0a 7d  or aFunc[] */..}
77450 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  ;..../*..** The 
77460 64 61 74 61 74 79 70 65 20 79 6e 56 61 72 20 69  datatype ynVar i
77470 73 20 61 20 73 69 67 6e 65 64 20 69 6e 74 65 67  s a signed integ
77480 65 72 2c 20 65 69 74 68 65 72 20 31 36 2d 62 69  er, either 16-bi
77490 74 20 6f 72 20 33 32 2d 62 69 74 2e 0d 0a 2a 2a  t or 32-bit...**
774a0 20 55 73 75 61 6c 6c 79 20 69 74 20 69 73 20 31   Usually it is 1
774b0 36 2d 62 69 74 73 2e 20 20 42 75 74 20 69 66 20  6-bits.  But if 
774c0 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
774d0 42 4c 45 5f 4e 55 4d 42 45 52 20 69 73 20 67 72  BLE_NUMBER is gr
774e0 65 61 74 65 72 0d 0a 2a 2a 20 74 68 61 6e 20 33  eater..** than 3
774f0 32 37 36 37 20 77 65 20 68 61 76 65 20 74 6f 20  2767 we have to 
77500 6d 61 6b 65 20 69 74 20 33 32 2d 62 69 74 2e 20  make it 32-bit. 
77510 20 31 36 2d 62 69 74 20 69 73 20 70 72 65 66 65   16-bit is prefe
77520 72 72 65 64 20 62 65 63 61 75 73 65 0d 0a 2a 2a  rred because..**
77530 20 69 74 20 75 73 65 73 20 6c 65 73 73 20 6d 65   it uses less me
77540 6d 6f 72 79 20 69 6e 20 74 68 65 20 45 78 70 72  mory in the Expr
77550 20 6f 62 6a 65 63 74 2c 20 77 68 69 63 68 20 69   object, which i
77560 73 20 61 20 62 69 67 20 6d 65 6d 6f 72 79 20 75  s a big memory u
77570 73 65 72 0d 0a 2a 2a 20 69 6e 20 73 79 73 74 65  ser..** in syste
77580 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 6f 66 20  ms with lots of 
77590 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
775a0 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 20 61 70  nts.  And few ap
775b0 70 6c 69 63 61 74 69 6f 6e 73 0d 0a 2a 2a 20 6e  plications..** n
775c0 65 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 61 62  eed more than ab
775d0 6f 75 74 20 31 30 20 6f 72 20 32 30 20 76 61 72  out 10 or 20 var
775e0 69 61 62 6c 65 73 2e 20 20 42 75 74 20 73 6f 6d  iables.  But som
775f0 65 20 65 78 74 72 65 6d 65 20 75 73 65 72 73 20  e extreme users 
77600 77 61 6e 74 0d 0a 2a 2a 20 74 6f 20 68 61 76 65  want..** to have
77610 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
77620 65 6e 74 73 20 77 69 74 68 20 6f 76 65 72 20 33  ents with over 3
77630 32 37 36 37 20 76 61 72 69 61 62 6c 65 73 2c 20  2767 variables, 
77640 61 6e 64 20 66 6f 72 20 74 68 65 6d 0d 0a 2a 2a  and for them..**
77650 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 61   the option is a
77660 76 61 69 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d  vailable (at com
77670 70 69 6c 65 2d 74 69 6d 65 29 2e 0d 0a 2a 2f 0d  pile-time)...*/.
77680 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
77690 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c  VARIABLE_NUMBER<
776a0 3d 33 32 37 36 37 0d 0a 74 79 70 65 64 65 66 20  =32767..typedef 
776b0 69 31 36 20 79 6e 56 61 72 3b 0d 0a 23 65 6c 73  i16 ynVar;..#els
776c0 65 0d 0a 74 79 70 65 64 65 66 20 69 6e 74 20 79  e..typedef int y
776d0 6e 56 61 72 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  nVar;..#endif...
776e0 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 6e 6f 64  ./*..** Each nod
776f0 65 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  e of an expressi
77700 6f 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 20  on in the parse 
77710 74 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 61  tree is an insta
77720 6e 63 65 0d 0a 2a 2a 20 6f 66 20 74 68 69 73 20  nce..** of this 
77730 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a  structure...**..
77740 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 20 74 68  ** Expr.op is th
77750 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 69 6e  e opcode. The in
77760 74 65 67 65 72 20 70 61 72 73 65 72 20 74 6f 6b  teger parser tok
77770 65 6e 20 63 6f 64 65 73 20 61 72 65 20 72 65 75  en codes are reu
77780 73 65 64 0d 0a 2a 2a 20 61 73 20 6f 70 63 6f 64  sed..** as opcod
77790 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 78 61  es here. For exa
777a0 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 65 72  mple, the parser
777b0 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 20 74   defines TK_GE t
777c0 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 0d  o be an integer.
777d0 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 65  .** code represe
777e0 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 6f  nting the ">=" o
777f0 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 61  perator. This sa
77800 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20  me integer code 
77810 69 73 20 72 65 75 73 65 64 0d 0a 2a 2a 20 74 6f  is reused..** to
77820 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67   represent the g
77830 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65  reater-than-or-e
77840 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72  qual-to operator
77850 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
77860 6f 6e 0d 0a 2a 2a 20 74 72 65 65 2e 0d 0a 2a 2a  on..** tree...**
77870 0d 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  ..** If the expr
77880 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c  ession is an SQL
77890 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54   literal (TK_INT
778a0 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20  EGER, TK_FLOAT, 
778b0 54 4b 5f 42 4c 4f 42 2c 20 0d 0a 2a 2a 20 6f 72  TK_BLOB, ..** or
778c0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65   TK_STRING), the
778d0 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e  n Expr.token con
778e0 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
778f0 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61  f the SQL litera
77900 6c 2e 20 49 66 0d 0a 2a 2a 20 74 68 65 20 65 78  l. If..** the ex
77910 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61  pression is a va
77920 72 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41  riable (TK_VARIA
77930 42 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e  BLE), then Expr.
77940 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74  token contains t
77950 68 65 20 0d 0a 2a 2a 20 76 61 72 69 61 62 6c 65  he ..** variable
77960 20 6e 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20   name. Finally, 
77970 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
77980 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63  n is an SQL func
77990 74 69 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f  tion (TK_FUNCTIO
779a0 4e 29 2c 0d 0a 2a 2a 20 74 68 65 6e 20 45 78 70  N),..** then Exp
779b0 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73  r.token contains
779c0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
779d0 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a   function...**..
779e0 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 20 61  ** Expr.pRight a
779f0 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 61 72  nd Expr.pLeft ar
77a00 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72  e the left and r
77a10 69 67 68 74 20 73 75 62 65 78 70 72 65 73 73 69  ight subexpressi
77a20 6f 6e 73 20 6f 66 20 61 0d 0a 2a 2a 20 62 69 6e  ons of a..** bin
77a30 61 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 69  ary operator. Ei
77a40 74 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 79  ther or both may
77a50 20 62 65 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a   be NULL...**..*
77a60 2a 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69  * Expr.x.pList i
77a70 73 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75  s a list of argu
77a80 6d 65 6e 74 73 20 69 66 20 74 68 65 20 65 78 70  ments if the exp
77a90 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51  ression is an SQ
77aa0 4c 20 66 75 6e 63 74 69 6f 6e 2c 0d 0a 2a 2a 20  L function,..** 
77ab0 61 20 43 41 53 45 20 65 78 70 72 65 73 73 69 6f  a CASE expressio
77ac0 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65  n or an IN expre
77ad0 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72  ssion of the for
77ae0 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 3e  m "<lhs> IN (<y>
77af0 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0d 0a 2a 2a 20  , <z>...)"...** 
77b00 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 69  Expr.x.pSelect i
77b10 73 20 75 73 65 64 20 69 66 20 74 68 65 20 65 78  s used if the ex
77b20 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75  pression is a su
77b30 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e 20 65  b-select or an e
77b40 78 70 72 65 73 73 69 6f 6e 20 6f 66 0d 0a 2a 2a  xpression of..**
77b50 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e   the form "<lhs>
77b60 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
77b70 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 73  ". If the EP_xIs
77b80 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 65  Select bit is se
77b90 74 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 45 78 70  t in the..** Exp
77ba0 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
77bb0 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63  en Expr.x.pSelec
77bc0 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65  t is valid. Othe
77bd0 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c  rwise, Expr.x.pL
77be0 69 73 74 20 69 73 20 0d 0a 2a 2a 20 76 61 6c 69  ist is ..** vali
77bf0 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 65 78  d...**..** An ex
77c00 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20  pression of the 
77c10 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 44  form ID or ID.ID
77c20 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f 6c   refers to a col
77c30 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 0d  umn in a table..
77c40 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70  .** For such exp
77c50 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f  ressions, Expr.o
77c60 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43  p is set to TK_C
77c70 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69  OLUMN and Expr.i
77c80 54 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 74 68 65  Table is..** the
77c90 20 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20   integer cursor 
77ca0 6e 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45  number of a VDBE
77cb0 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
77cc0 20 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61   to that table a
77cd0 6e 64 0d 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c  nd..** Expr.iCol
77ce0 75 6d 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d  umn is the colum
77cf0 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  n number for the
77d00 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
77d10 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a 20 65 78  .  If the..** ex
77d20 70 72 65 73 73 69 6f 6e 20 69 73 20 75 73 65 64  pression is used
77d30 20 61 73 20 61 20 72 65 73 75 6c 74 20 69 6e 20   as a result in 
77d40 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 45 4c  an aggregate SEL
77d50 45 43 54 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a  ECT, then the..*
77d60 2a 20 76 61 6c 75 65 20 69 73 20 61 6c 73 6f 20  * value is also 
77d70 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 45 78  stored in the Ex
77d80 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e 20 69  pr.iAgg column i
77d90 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  n the aggregate 
77da0 73 6f 20 74 68 61 74 0d 0a 2a 2a 20 69 74 20 63  so that..** it c
77db0 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 20 61  an be accessed a
77dc0 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 67 61  fter all aggrega
77dd0 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64  tes are computed
77de0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
77df0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
77e00 6e 20 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62  n unbound variab
77e10 6c 65 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65  le marker (a que
77e20 73 74 69 6f 6e 20 6d 61 72 6b 20 0d 0a 2a 2a 20  stion mark ..** 
77e30 63 68 61 72 61 63 74 65 72 20 27 3f 27 20 69 6e  character '?' in
77e40 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
77e50 4c 29 20 74 68 65 6e 20 74 68 65 20 45 78 70 72  L) then the Expr
77e60 2e 69 54 61 62 6c 65 20 68 6f 6c 64 73 20 74 68  .iTable holds th
77e70 65 20 69 6e 64 65 78 20 0d 0a 2a 2a 20 6e 75 6d  e index ..** num
77e80 62 65 72 20 66 6f 72 20 74 68 61 74 20 76 61 72  ber for that var
77e90 69 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  iable...**..** I
77ea0 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
77eb0 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 74   is a subquery t
77ec0 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  hen Expr.iColumn
77ed0 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65   holds an intege
77ee0 72 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e  r..** register n
77ef0 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  umber containing
77f00 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
77f10 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66  he subquery.  If
77f20 20 74 68 65 0d 0a 2a 2a 20 73 75 62 71 75 65 72   the..** subquer
77f30 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
77f40 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
77f50 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
77f60 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0d 0a  f the subquery..
77f70 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65  ** gives a diffe
77f80 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64  rent answer at d
77f90 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64  ifferent times d
77fa0 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  uring statement 
77fb0 70 72 6f 63 65 73 73 69 6e 67 0d 0a 2a 2a 20 74  processing..** t
77fc0 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68  hen iTable is th
77fd0 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 73  e address of a s
77fe0 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63  ubroutine that c
77ff0 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62 71  omputes the subq
78000 75 65 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  uery...**..** If
78010 20 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20   the Expr is of 
78020 74 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  type OP_Column, 
78030 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74  and the table it
78040 20 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72   is selecting fr
78050 6f 6d 0d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b  om..** is a disk
78060 20 74 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f   table or the "o
78070 6c 64 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62  ld.*" pseudo-tab
78080 6c 65 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f  le, then pTab po
78090 69 6e 74 73 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  ints to the..** 
780a0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
780b0 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0d  ble definition..
780c0 0a 2a 2a 0d 0a 2a 2a 20 41 4c 4c 4f 43 41 54 49  .**..** ALLOCATI
780d0 4f 4e 20 4e 4f 54 45 53 3a 0d 0a 2a 2a 0d 0a 2a  ON NOTES:..**..*
780e0 2a 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63  * Expr objects c
780f0 61 6e 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20  an use a lot of 
78100 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20  memory space in 
78110 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
78120 20 20 54 6f 0d 0a 2a 2a 20 68 65 6c 70 20 72 65    To..** help re
78130 64 75 63 65 20 6d 65 6d 6f 72 79 20 72 65 71 75  duce memory requ
78140 69 72 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69  irements, someti
78150 6d 65 73 20 61 6e 20 45 78 70 72 20 6f 62 6a 65  mes an Expr obje
78160 63 74 20 77 69 6c 6c 20 62 65 0d 0a 2a 2a 20 74  ct will be..** t
78170 72 75 6e 63 61 74 65 64 2e 20 20 41 6e 64 20 74  runcated.  And t
78180 6f 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d  o reduce the num
78190 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c  ber of memory al
781a0 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 74  locations, somet
781b0 69 6d 65 73 0d 0a 2a 2a 20 74 77 6f 20 6f 72 20  imes..** two or 
781c0 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 63 74  more Expr object
781d0 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64  s will be stored
781e0 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d   in a single mem
781f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0d  ory allocation,.
78200 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74  .** together wit
78210 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 74  h Expr.zToken st
78220 72 69 6e 67 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  rings...**..** I
78230 66 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64  f the EP_Reduced
78240 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c   and EP_TokenOnl
78250 79 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20  y flags are set 
78260 77 68 65 6e 0d 0a 2a 2a 20 61 6e 20 45 78 70 72  when..** an Expr
78270 20 6f 62 6a 65 63 74 20 69 73 20 74 72 75 6e 63   object is trunc
78280 61 74 65 64 2e 20 20 57 68 65 6e 20 45 50 5f 52  ated.  When EP_R
78290 65 64 75 63 65 64 20 69 73 20 73 65 74 2c 20 74  educed is set, t
782a0 68 65 6e 20 61 6c 6c 0d 0a 2a 2a 20 74 68 65 20  hen all..** the 
782b0 63 68 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 63  child Expr objec
782c0 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 70  ts in the Expr.p
782d0 4c 65 66 74 20 61 6e 64 20 45 78 70 72 2e 70 52  Left and Expr.pR
782e0 69 67 68 74 20 73 75 62 74 72 65 65 73 0d 0a 2a  ight subtrees..*
782f0 2a 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  * are contained 
78300 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20  within the same 
78310 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
78320 6e 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65  n.  Note, howeve
78330 72 2c 20 74 68 61 74 0d 0a 2a 2a 20 74 68 65 20  r, that..** the 
78340 73 75 62 74 72 65 65 73 20 69 6e 20 45 78 70 72  subtrees in Expr
78350 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72  .x.pList or Expr
78360 2e 78 2e 70 53 65 6c 65 63 74 20 61 72 65 20 61  .x.pSelect are a
78370 6c 77 61 79 73 20 73 65 70 61 72 61 74 65 6c 79  lways separately
78380 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20  ..** allocated, 
78390 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
783a0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 45 50 5f  ether or not EP_
783b0 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2e 0d  Reduced is set..
783c0 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 45 78 70 72  .*/..struct Expr
783d0 20 7b 0d 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   {..  u8 op;    
783e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
783f0 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 6f 72  Operation perfor
78400 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65  med by this node
78410 20 2a 2f 0d 0a 20 20 63 68 61 72 20 61 66 66 69   */..  char affi
78420 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  nity;         /*
78430 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   The affinity of
78440 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30   the column or 0
78450 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e   if not a column
78460 20 2a 2f 0d 0a 20 20 75 31 36 20 66 6c 61 67 73   */..  u16 flags
78470 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
78480 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20   Various flags. 
78490 20 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20   EP_* See below 
784a0 2a 2f 0d 0a 20 20 75 6e 69 6f 6e 20 7b 0d 0a 20  */..  union {.. 
784b0 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b     char *zToken;
784c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
784d0 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 6f 20 74  en value. Zero t
784e0 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 64 65  erminated and de
784f0 71 75 6f 74 65 64 20 2a 2f 0d 0a 20 20 20 20 69  quoted */..    i
78500 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 20 20  nt iValue;      
78510 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 6e 65 67        /* Non-neg
78520 61 74 69 76 65 20 69 6e 74 65 67 65 72 20 76 61  ative integer va
78530 6c 75 65 20 69 66 20 45 50 5f 49 6e 74 56 61 6c  lue if EP_IntVal
78540 75 65 20 2a 2f 0d 0a 20 20 7d 20 75 3b 0d 0a 0d  ue */..  } u;...
78550 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f  .  /* If the EP_
78560 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69  TokenOnly flag i
78570 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70  s set in the Exp
78580 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
78590 65 6e 20 6e 6f 0d 0a 20 20 2a 2a 20 73 70 61 63  en no..  ** spac
785a0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
785b0 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65  or the fields be
785c0 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  low this point. 
785d0 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0d 0a 20  An attempt to.. 
785e0 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20   ** access them 
785f0 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61  will result in a
78600 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c   segfault or mal
78610 66 75 6e 63 74 69 6f 6e 2e 20 0d 0a 20 20 2a 2a  function. ..  **
78620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78660 2a 2a 2a 2f 0d 0a 0d 0a 20 20 45 78 70 72 20 2a  ***/....  Expr *
78670 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20  pLeft;          
78680 20 2f 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 65   /* Left subnode
78690 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 52 69   */..  Expr *pRi
786a0 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ght;          /*
786b0 20 52 69 67 68 74 20 73 75 62 6e 6f 64 65 20 2a   Right subnode *
786c0 2f 0d 0a 20 20 75 6e 69 6f 6e 20 7b 0d 0a 20 20  /..  union {..  
786d0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
786e0 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69  t;     /* Functi
786f0 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20  on arguments or 
78700 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c  in "<expr> IN (<
78710 65 78 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0d 0a  expr-list)" */..
78720 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
78730 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64  ect;     /* Used
78740 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73   for sub-selects
78750 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 4e 20   and "<expr> IN 
78760 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0d 0a  (<select>)" */..
78770 20 20 7d 20 78 3b 0d 0a 20 20 43 6f 6c 6c 53 65    } x;..  CollSe
78780 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  q *pColl;       
78790 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f   /* The collatio
787a0 6e 20 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f  n type of the co
787b0 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0d 0a 0d 0a  lumn or 0 */....
787c0 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 52    /* If the EP_R
787d0 65 64 75 63 65 64 20 66 6c 61 67 20 69 73 20 73  educed flag is s
787e0 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  et in the Expr.f
787f0 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20  lags mask, then 
78800 6e 6f 0d 0a 20 20 2a 2a 20 73 70 61 63 65 20 69  no..  ** space i
78810 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
78820 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77  the fields below
78830 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20   this point. An 
78840 61 74 74 65 6d 70 74 20 74 6f 0d 0a 20 20 2a 2a  attempt to..  **
78850 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c   access them wil
78860 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65  l result in a se
78870 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e  gfault or malfun
78880 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2a 2a 2a 2a 2a  ction...  ******
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 2a 2a 2a 2a 2a 2a 2a  ****************
788c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
788d0 0d 0a 0d 0a 20 20 69 6e 74 20 69 54 61 62 6c 65  ....  int iTable
788e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
788f0 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f  TK_COLUMN: curso
78900 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  r number of tabl
78910 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e  e holding column
78920 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
78930 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b             ** TK
78940 5f 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73  _REGISTER: regis
78950 74 65 72 20 6e 75 6d 62 65 72 0d 0a 20 20 20 20  ter number..    
78960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78970 20 20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47       ** TK_TRIGG
78980 45 52 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20  ER: 1 -> new, 0 
78990 2d 3e 20 6f 6c 64 20 2a 2f 0d 0a 20 20 79 6e 56  -> old */..  ynV
789a0 61 72 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  ar iColumn;     
789b0 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e      /* TK_COLUMN
789c0 3a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20  : column index. 
789d0 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 2e 0d 0a   -1 for rowid...
789e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
789f0 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 56           ** TK_V
78a00 41 52 49 41 42 4c 45 3a 20 76 61 72 69 61 62 6c  ARIABLE: variabl
78a10 65 20 6e 75 6d 62 65 72 20 28 61 6c 77 61 79 73  e number (always
78a20 20 3e 3d 20 31 29 2e 20 2a 2f 0d 0a 20 20 69 31   >= 1). */..  i1
78a30 36 20 69 41 67 67 3b 20 20 20 20 20 20 20 20 20  6 iAgg;         
78a40 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e       /* Which en
78a50 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  try in pAggInfo-
78a60 3e 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75  >aCol[] or ->aFu
78a70 6e 63 5b 5d 20 2a 2f 0d 0a 20 20 69 31 36 20 69  nc[] */..  i16 i
78a80 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 20  RightJoinTable; 
78a90 20 20 2f 2a 20 49 66 20 45 50 5f 46 72 6f 6d 4a    /* If EP_FromJ
78aa0 6f 69 6e 2c 20 74 68 65 20 72 69 67 68 74 20 74  oin, the right t
78ab0 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e  able of the join
78ac0 20 2a 2f 0d 0a 20 20 75 38 20 66 6c 61 67 73 32   */..  u8 flags2
78ad0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
78ae0 20 53 65 63 6f 6e 64 20 73 65 74 20 6f 66 20 66   Second set of f
78af0 6c 61 67 73 2e 20 20 45 50 32 5f 2e 2e 2e 20 2a  lags.  EP2_... *
78b00 2f 0d 0a 20 20 75 38 20 6f 70 32 3b 20 20 20 20  /..  u8 op2;    
78b10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
78b20 66 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 2c  f a TK_REGISTER,
78b30 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76 61   the original va
78b40 6c 75 65 20 6f 66 20 45 78 70 72 2e 6f 70 20 2a  lue of Expr.op *
78b50 2f 0d 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41  /..  AggInfo *pA
78b60 67 67 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55  ggInfo;     /* U
78b70 73 65 64 20 62 79 20 54 4b 5f 41 47 47 5f 43 4f  sed by TK_AGG_CO
78b80 4c 55 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 47 5f  LUMN and TK_AGG_
78b90 46 55 4e 43 54 49 4f 4e 20 2a 2f 0d 0a 20 20 54  FUNCTION */..  T
78ba0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
78bb0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66        /* Table f
78bc0 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70  or TK_COLUMN exp
78bd0 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0d 0a 23 69  ressions. */..#i
78be0 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
78bf0 52 5f 44 45 50 54 48 3e 30 0d 0a 20 20 69 6e 74  R_DEPTH>0..  int
78c00 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   nHeight;       
78c10 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66      /* Height of
78c20 20 74 68 65 20 74 72 65 65 20 68 65 61 64 65 64   the tree headed
78c30 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f   by this node */
78c40 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a  ..#endif..};....
78c50 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  /*..** The follo
78c60 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61  wing are the mea
78c70 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e  nings of bits in
78c80 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20   the Expr.flags 
78c90 66 69 65 6c 64 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  field...*/..#def
78ca0 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20  ine EP_FromJoin 
78cb0 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 69    0x0001  /* Ori
78cc0 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72  ginated in ON or
78cd0 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
78ce0 20 61 20 6a 6f 69 6e 20 2a 2f 0d 0a 23 64 65 66   a join */..#def
78cf0 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20 20 20  ine EP_Agg      
78d00 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e    0x0002  /* Con
78d10 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
78d20 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  e aggregate func
78d30 74 69 6f 6e 73 20 2a 2f 0d 0a 23 64 65 66 69 6e  tions */..#defin
78d40 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 20  e EP_Resolved   
78d50 30 78 30 30 30 34 20 20 2f 2a 20 49 44 73 20 68  0x0004  /* IDs h
78d60 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65  ave been resolve
78d70 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0d  d to COLUMNs */.
78d80 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f  .#define EP_Erro
78d90 72 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f  r      0x0008  /
78da0 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  * Expression con
78db0 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
78dc0 65 20 65 72 72 6f 72 73 20 2a 2f 0d 0a 23 64 65  e errors */..#de
78dd0 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74  fine EP_Distinct
78de0 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 67     0x0010  /* Ag
78df0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
78e00 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b   with DISTINCT k
78e10 65 79 77 6f 72 64 20 2a 2f 0d 0a 23 64 65 66 69  eyword */..#defi
78e20 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20  ne EP_VarSelect 
78e30 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c   0x0020  /* pSel
78e40 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ect is correlate
78e50 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  d, not constant 
78e60 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 45 50 5f 44  */..#define EP_D
78e70 62 6c 51 75 6f 74 65 64 20 20 30 78 30 30 34 30  blQuoted  0x0040
78e80 20 20 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 73    /* token.z was
78e90 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20 22   originally in "
78ea0 2e 2e 2e 22 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ..." */..#define
78eb0 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 30   EP_InfixFunc  0
78ec0 78 30 30 38 30 20 20 2f 2a 20 54 72 75 65 20 66  x0080  /* True f
78ed0 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e 63  or an infix func
78ee0 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42  tion: LIKE, GLOB
78ef0 2c 20 65 74 63 20 2a 2f 0d 0a 23 64 65 66 69 6e  , etc */..#defin
78f00 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  e EP_ExpCollate 
78f10 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61  0x0100  /* Colla
78f20 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70  ting sequence sp
78f30 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74  ecified explicit
78f40 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 45  ly */..#define E
78f50 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30  P_FixedDest  0x0
78f60 32 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e  200  /* Result n
78f70 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69  eeded in a speci
78f80 66 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0d  fic register */.
78f90 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56  .#define EP_IntV
78fa0 61 6c 75 65 20 20 20 30 78 30 34 30 30 20 20 2f  alue   0x0400  /
78fb0 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * Integer value 
78fc0 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69  contained in u.i
78fd0 56 61 6c 75 65 20 2a 2f 0d 0a 23 64 65 66 69 6e  Value */..#defin
78fe0 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20  e EP_xIsSelect  
78ff0 30 78 30 38 30 30 20 20 2f 2a 20 78 2e 70 53 65  0x0800  /* x.pSe
79000 6c 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f  lect is valid (o
79010 74 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74  therwise x.pList
79020 20 69 73 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65   is) */..#define
79030 20 45 50 5f 48 69 6e 74 20 20 20 20 20 20 20 30   EP_Hint       0
79040 78 31 30 30 30 20 20 2f 2a 20 4f 70 74 69 6d 69  x1000  /* Optimi
79050 7a 65 72 20 68 69 6e 74 2e 20 4e 6f 74 20 72 65  zer hint. Not re
79060 71 75 69 72 65 64 20 66 6f 72 20 63 6f 72 72 65  quired for corre
79070 63 74 6e 65 73 73 20 2a 2f 0d 0a 23 64 65 66 69  ctness */..#defi
79080 6e 65 20 45 50 5f 52 65 64 75 63 65 64 20 20 20  ne EP_Reduced   
79090 20 30 78 32 30 30 30 20 20 2f 2a 20 45 78 70 72   0x2000  /* Expr
790a0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f   struct is EXPR_
790b0 52 45 44 55 43 45 44 53 49 5a 45 20 62 79 74 65  REDUCEDSIZE byte
790c0 73 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69  s only */..#defi
790d0 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  ne EP_TokenOnly 
790e0 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 70 72   0x4000  /* Expr
790f0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f   struct is EXPR_
79100 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79  TOKENONLYSIZE by
79110 74 65 73 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65  tes only */..#de
79120 66 69 6e 65 20 45 50 5f 53 74 61 74 69 63 20 20  fine EP_Static  
79130 20 20 20 30 78 38 30 30 30 20 20 2f 2a 20 48 65     0x8000  /* He
79140 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6e 6f 74  ld in memory not
79150 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
79160 61 6c 6c 6f 63 28 29 20 2a 2f 0d 0a 0d 0a 2f 2a  alloc() */..../*
79170 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
79180 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69  ng are the meani
79190 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74  ngs of bits in t
791a0 68 65 20 45 78 70 72 2e 66 6c 61 67 73 32 20 66  he Expr.flags2 f
791b0 69 65 6c 64 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ield...*/..#defi
791c0 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54  ne EP2_MallocedT
791d0 6f 6b 65 6e 20 20 30 78 30 30 30 31 20 20 2f 2a  oken  0x0001  /*
791e0 20 4e 65 65 64 20 74 6f 20 73 71 6c 69 74 65 33   Need to sqlite3
791f0 44 62 46 72 65 65 28 29 20 45 78 70 72 2e 7a 54  DbFree() Expr.zT
79200 6f 6b 65 6e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  oken */..#define
79210 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65   EP2_Irreducible
79220 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43      0x0002  /* C
79230 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 5f 52 45  annot EXPRDUP_RE
79240 44 55 43 45 20 74 68 69 73 20 45 78 70 72 20 2a  DUCE this Expr *
79250 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  /..../*..** The 
79260 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73  pseudo-routine s
79270 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72  qlite3ExprSetIrr
79280 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68  educible sets th
79290 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c  e EP2_Irreducibl
792a0 65 0d 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e  e..** flag on an
792b0 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75   expression stru
792c0 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61  cture.  This fla
792d0 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56  g is used for VV
792e0 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0d 0a 2a  &A only.  The..*
792f0 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70  * routine is imp
79300 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61  lemented as a ma
79310 63 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f  cro that only wo
79320 72 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75  rks when in debu
79330 67 67 69 6e 67 20 6d 6f 64 65 2c 0d 0a 2a 2a 20  gging mode,..** 
79340 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72  so as not to bur
79350 64 65 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63  den production c
79360 6f 64 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  ode...*/..#ifdef
79370 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 23   SQLITE_DEBUG..#
79380 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49   define ExprSetI
79390 72 72 65 64 75 63 69 62 6c 65 28 58 29 20 20 28  rreducible(X)  (
793a0 58 29 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 45 50  X)->flags2 |= EP
793b0 32 5f 49 72 72 65 64 75 63 69 62 6c 65 0d 0a 23  2_Irreducible..#
793c0 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 45  else..# define E
793d0 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
793e0 65 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  e(X)..#endif....
793f0 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63  /*..** These mac
79400 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  ros can be used 
79410 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72  to test, set, or
79420 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74   clear bits in t
79430 68 65 20 0d 0a 2a 2a 20 45 78 70 72 2e 66 6c 61  he ..** Expr.fla
79440 67 73 20 66 69 65 6c 64 2e 0d 0a 2a 2f 0d 0a 23  gs field...*/..#
79450 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72  define ExprHasPr
79460 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
79470 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
79480 29 3d 3d 28 50 29 29 0d 0a 23 64 65 66 69 6e 65  )==(P))..#define
79490 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
794a0 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d  rty(E,P)  (((E)-
794b0 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0d  >flags&(P))!=0).
794c0 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74  .#define ExprSet
794d0 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20  Property(E,P)   
794e0 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50    (E)->flags|=(P
794f0 29 0d 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43  )..#define ExprC
79500 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50  learProperty(E,P
79510 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d  )   (E)->flags&=
79520 7e 28 50 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  ~(P)..../*..** M
79530 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69  acros to determi
79540 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ne the number of
79550 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
79560 62 79 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72  by a normal Expr
79570 20 0d 0a 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e   ..** struct, an
79580 20 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74   Expr struct wit
79590 68 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64  h the EP_Reduced
795a0 20 66 6c 61 67 20 73 65 74 20 69 6e 20 45 78 70   flag set in Exp
795b0 72 2e 66 6c 61 67 73 20 0d 0a 2a 2a 20 61 6e 64  r.flags ..** and
795c0 20 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20   an Expr struct 
795d0 77 69 74 68 20 74 68 65 20 45 50 5f 54 6f 6b 65  with the EP_Toke
795e0 6e 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 2e 0d  nOnly flag set..
795f0 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 45 58 50  .*/..#define EXP
79600 52 5f 46 55 4c 4c 53 49 5a 45 20 20 20 20 20 20  R_FULLSIZE      
79610 20 20 20 20 20 73 69 7a 65 6f 66 28 45 78 70 72       sizeof(Expr
79620 29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  )           /* F
79630 75 6c 6c 20 73 69 7a 65 20 2a 2f 0d 0a 23 64 65  ull size */..#de
79640 66 69 6e 65 20 45 58 50 52 5f 52 45 44 55 43 45  fine EXPR_REDUCE
79650 44 53 49 5a 45 20 20 20 20 20 20 20 20 6f 66 66  DSIZE        off
79660 73 65 74 6f 66 28 45 78 70 72 2c 69 54 61 62 6c  setof(Expr,iTabl
79670 65 29 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 66 65  e)  /* Common fe
79680 61 74 75 72 65 73 20 2a 2f 0d 0a 23 64 65 66 69  atures */..#defi
79690 6e 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c  ne EXPR_TOKENONL
796a0 59 53 49 5a 45 20 20 20 20 20 20 6f 66 66 73 65  YSIZE      offse
796b0 74 6f 66 28 45 78 70 72 2c 70 4c 65 66 74 29 20  tof(Expr,pLeft) 
796c0 20 20 2f 2a 20 46 65 77 65 72 20 66 65 61 74 75    /* Fewer featu
796d0 72 65 73 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  res */..../*..**
796e0 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f   Flags passed to
796f0 20 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 72   the sqlite3Expr
79700 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  Dup() function. 
79710 53 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63  See the header c
79720 6f 6d 6d 65 6e 74 20 0d 0a 2a 2a 20 61 62 6f 76  omment ..** abov
79730 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  e sqlite3ExprDup
79740 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0d  () for details..
79750 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 45 58 50  .*/..#define EXP
79760 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20  RDUP_REDUCE     
79770 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55      0x0001  /* U
79780 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65  sed reduced-size
79790 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0d 0a   Expr nodes */..
797a0 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 6c 69 73 74 20  ../*..** A list 
797b0 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  of expressions. 
797c0 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   Each expression
797d0 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   may optionally 
797e0 68 61 76 65 20 61 0d 0a 2a 2a 20 6e 61 6d 65 2e  have a..** name.
797f0 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63    An expr/name c
79800 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62  ombination can b
79810 65 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61  e used in severa
79820 6c 20 77 61 79 73 2c 20 73 75 63 68 0d 0a 2a 2a  l ways, such..**
79830 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20   as the list of 
79840 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69 65  "expr AS ID" fie
79850 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  lds following a 
79860 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74  "SELECT" or in t
79870 68 65 0d 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22  he..** list of "
79880 49 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73  ID = expr" items
79890 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20   in an UPDATE.  
798a0 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73  A list of expres
798b0 73 69 6f 6e 73 20 63 61 6e 0d 0a 2a 2a 20 61 6c  sions can..** al
798c0 73 6f 20 62 65 20 75 73 65 64 20 61 73 20 74 68  so be used as th
798d0 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20  e argument to a 
798e0 66 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69  function, in whi
798f0 63 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e  ch case the a.zN
79900 61 6d 65 0d 0a 2a 2a 20 66 69 65 6c 64 20 69 73  ame..** field is
79910 20 6e 6f 74 20 75 73 65 64 2e 0d 0a 2a 2f 0d 0a   not used...*/..
79920 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 20  struct ExprList 
79930 7b 0d 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 20  {..  int nExpr; 
79940 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
79950 75 6d 62 65 72 20 6f 66 20 65 78 70 72 65 73 73  umber of express
79960 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  ions on the list
79970 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f   */..  int nAllo
79980 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
79990 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
799a0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c  es allocated bel
799b0 6f 77 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 45 43  ow */..  int iEC
799c0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
799d0 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 61  /* VDBE Cursor a
799e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
799f0 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0d  his ExprList */.
79a00 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
79a10 73 74 5f 69 74 65 6d 20 7b 0d 0a 20 20 20 20 45  st_item {..    E
79a20 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
79a30 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
79a40 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
79a50 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a   */..    char *z
79a60 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
79a70 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61  /* Token associa
79a80 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78  ted with this ex
79a90 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 20  pression */..   
79aa0 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20   char *zSpan;   
79ab0 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69          /* Origi
79ac0 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20  nal text of the 
79ad0 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20  expression */.. 
79ae0 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b     u8 sortOrder;
79af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
79b00 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72  or DESC or 0 for
79b10 20 41 53 43 20 2a 2f 0d 0a 20 20 20 20 75 38 20   ASC */..    u8 
79b20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20  done;           
79b30 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f      /* A flag to
79b40 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70   indicate when p
79b50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e  rocessing is fin
79b60 69 73 68 65 64 20 2a 2f 0d 0a 20 20 20 20 75 31  ished */..    u1
79b70 36 20 69 4f 72 64 65 72 42 79 43 6f 6c 3b 20 20  6 iOrderByCol;  
79b80 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45       /* For ORDE
79b90 52 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d  R BY, column num
79ba0 62 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65  ber in result se
79bb0 74 20 2a 2f 0d 0a 20 20 20 20 75 31 36 20 69 41  t */..    u16 iA
79bc0 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 20  lias;           
79bd0 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50   /* Index into P
79be0 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f  arse.aAlias[] fo
79bf0 72 20 7a 4e 61 6d 65 20 2a 2f 0d 0a 20 20 7d 20  r zName */..  } 
79c00 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *a;             
79c10 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72       /* One entr
79c20 79 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65  y for each expre
79c30 73 73 69 6f 6e 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  ssion */..};....
79c40 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  /*..** An instan
79c50 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
79c60 74 75 72 65 20 69 73 20 75 73 65 64 20 62 79 20  ture is used by 
79c70 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 72 65  the parser to re
79c80 63 6f 72 64 20 62 6f 74 68 0d 0a 2a 2a 20 74 68  cord both..** th
79c90 65 20 70 61 72 73 65 20 74 72 65 65 20 66 6f 72  e parse tree for
79ca0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61   an expression a
79cb0 6e 64 20 74 68 65 20 73 70 61 6e 20 6f 66 20 69  nd the span of i
79cc0 6e 70 75 74 20 74 65 78 74 20 66 6f 72 20 61 6e  nput text for an
79cd0 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e  ..** expression.
79ce0 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 45 78 70  ..*/..struct Exp
79cf0 72 53 70 61 6e 20 7b 0d 0a 20 20 45 78 70 72 20  rSpan {..  Expr 
79d00 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
79d10 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
79d20 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a 2f  on parse tree */
79d30 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
79d40 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72  zStart;   /* Fir
79d50 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20  st character of 
79d60 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0d 0a 20  input text */.. 
79d70 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e   const char *zEn
79d80 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68  d;     /* One ch
79d90 61 72 61 63 74 65 72 20 70 61 73 74 20 74 68 65  aracter past the
79da0 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65   end of input te
79db0 78 74 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  xt */..};..../*.
79dc0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
79dd0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
79de0 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d  e can hold a sim
79df0 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e  ple list of iden
79e00 74 69 66 69 65 72 73 2c 0d 0a 2a 2a 20 73 75 63  tifiers,..** suc
79e10 68 20 61 73 20 74 68 65 20 6c 69 73 74 20 22 61  h as the list "a
79e20 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c  ,b,c" in the fol
79e30 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
79e40 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  s:..**..**      
79e50 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c  INSERT INTO t(a,
79e60 62 2c 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b  b,c) VALUES ...;
79e70 0d 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45  ..**      CREATE
79e80 20 49 4e 44 45 58 20 69 64 78 20 4f 4e 20 74 28   INDEX idx ON t(
79e90 61 2c 62 2c 63 29 3b 0d 0a 2a 2a 20 20 20 20 20  a,b,c);..**     
79ea0 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
79eb0 74 72 69 67 20 42 45 46 4f 52 45 20 55 50 44 41  trig BEFORE UPDA
79ec0 54 45 20 4f 4e 20 74 28 61 2c 62 2c 63 29 20 2e  TE ON t(a,b,c) .
79ed0 2e 2e 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ..;..**..** The 
79ee0 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 66 69 65  IdList.a.idx fie
79ef0 6c 64 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  ld is used when 
79f00 74 68 65 20 49 64 4c 69 73 74 20 72 65 70 72 65  the IdList repre
79f10 73 65 6e 74 73 20 74 68 65 20 6c 69 73 74 20 6f  sents the list o
79f20 66 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d  f..** column nam
79f30 65 73 20 61 66 74 65 72 20 61 20 74 61 62 6c 65  es after a table
79f40 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e 53 45   name in an INSE
79f50 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  RT statement.  I
79f60 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0d  n the statement.
79f70 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 49 4e 53 45  .**..**     INSE
79f80 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
79f90 20 2e 2e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20   .....**..** If 
79fa0 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20  "a" is the k-th 
79fb0 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20  column of table 
79fc0 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74  "t", then IdList
79fd0 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0d 0a 2a  .a[0].idx==k...*
79fe0 2f 0d 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74  /..struct IdList
79ff0 20 7b 0d 0a 20 20 73 74 72 75 63 74 20 49 64 4c   {..  struct IdL
7a000 69 73 74 5f 69 74 65 6d 20 7b 0d 0a 20 20 20 20  ist_item {..    
7a010 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
7a020 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
7a030 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0d 0a   identifier */..
7a040 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20      int idx;    
7a050 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
7a060 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f  n some Table.aCo
7a070 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  l[] of a column 
7a080 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0d 0a  named zName */..
7a090 20 20 7d 20 2a 61 3b 0d 0a 20 20 69 6e 74 20 6e    } *a;..  int n
7a0a0 49 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  Id;         /* N
7a0b0 75 6d 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66  umber of identif
7a0c0 69 65 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  iers on the list
7a0d0 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f   */..  int nAllo
7a0e0 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  c;      /* Numbe
7a0f0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c  r of entries all
7a100 6f 63 61 74 65 64 20 66 6f 72 20 61 5b 5d 20 62  ocated for a[] b
7a110 65 6c 6f 77 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f  elow */..};..../
7a120 2a 0d 0a 2a 2a 20 54 68 65 20 62 69 74 6d 61 73  *..** The bitmas
7a130 6b 20 64 61 74 61 74 79 70 65 20 64 65 66 69 6e  k datatype defin
7a140 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73 65 64  ed below is used
7a150 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f 70 74   for various opt
7a160 69 6d 69 7a 61 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d  imizations...**.
7a170 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69  .** Changing thi
7a180 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20  s from a 64-bit 
7a190 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65  to a 32-bit type
7a1a0 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62   limits the numb
7a1b0 65 72 20 6f 66 0d 0a 2a 2a 20 74 61 62 6c 65 73  er of..** tables
7a1c0 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32   in a join to 32
7a1d0 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20   instead of 64. 
7a1e0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64   But it also red
7a1f0 75 63 65 73 20 74 68 65 20 73 69 7a 65 0d 0a 2a  uces the size..*
7a200 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  * of the library
7a210 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e   by 738 bytes on
7a220 20 69 78 38 36 2e 0d 0a 2a 2f 0d 0a 74 79 70 65   ix86...*/..type
7a230 64 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b  def u64 Bitmask;
7a240 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6e  ..../*..** The n
7a250 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
7a260 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d   a Bitmask.  "BM
7a270 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73  S" means "BitMas
7a280 6b 20 53 69 7a 65 22 2e 0d 0a 2a 2f 0d 0a 23 64  k Size"...*/..#d
7a290 65 66 69 6e 65 20 42 4d 53 20 20 28 28 69 6e 74  efine BMS  ((int
7a2a0 29 28 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b  )(sizeof(Bitmask
7a2b0 29 2a 38 29 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  )*8))..../*..** 
7a2c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
7a2d0 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65  ructure describe
7a2e0 73 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  s the FROM claus
7a2f0 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  e of a SELECT st
7a300 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 20 45 61 63  atement...** Eac
7a310 68 20 74 61 62 6c 65 20 6f 72 20 73 75 62 71 75  h table or subqu
7a320 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ery in the FROM 
7a330 63 6c 61 75 73 65 20 69 73 20 61 20 73 65 70 61  clause is a sepa
7a340 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 6f 66 0d  rate element of.
7a350 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 74 2e  .** the SrcList.
7a360 61 5b 5d 20 61 72 72 61 79 2e 0d 0a 2a 2a 0d 0a  a[] array...**..
7a370 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69  ** With the addi
7a380 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65  tion of multiple
7a390 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
7a3a0 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
7a3b0 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 63   structure..** c
7a3c0 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  an also be used 
7a3d0 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
7a3e0 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73  rticular table s
7a3f0 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65  uch as the table
7a400 20 74 68 61 74 0d 0a 2a 2a 20 69 73 20 6d 6f 64   that..** is mod
7a410 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45  ified by an INSE
7a420 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  RT, DELETE, or U
7a430 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
7a440 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51    In standard SQ
7a450 4c 2c 0d 0a 2a 2a 20 73 75 63 68 20 61 20 74 61  L,..** such a ta
7a460 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69  ble must be a si
7a470 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20  mple name: ID.  
7a480 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74  But in SQLite, t
7a490 68 65 20 74 61 62 6c 65 20 63 61 6e 0d 0a 2a 2a  he table can..**
7a4a0 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69   now be identifi
7a4b0 65 64 20 62 79 20 61 20 64 61 74 61 62 61 73 65  ed by a database
7a4c0 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68   name, a dot, th
7a4d0 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  en the table nam
7a4e0 65 3a 20 49 44 2e 49 44 2e 0d 0a 2a 2a 0d 0a 2a  e: ID.ID...**..*
7a4f0 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73  * The jointype s
7a500 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e  tarts out showin
7a510 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20  g the join type 
7a520 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72 72  between the curr
7a530 65 6e 74 20 74 61 62 6c 65 0d 0a 2a 2a 20 61 6e  ent table..** an
7a540 64 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65  d the next table
7a550 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54   on the list.  T
7a560 68 65 20 70 61 72 73 65 72 20 62 75 69 6c 64 73  he parser builds
7a570 20 74 68 65 20 6c 69 73 74 20 74 68 69 73 20 77   the list this w
7a580 61 79 2e 0d 0a 2a 2a 20 42 75 74 20 73 71 6c 69  ay...** But sqli
7a590 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
7a5a0 6f 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20  oinType() later 
7a5b0 73 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74  shifts the joint
7a5c0 79 70 65 73 20 73 6f 20 74 68 61 74 20 65 61 63  ypes so that eac
7a5d0 68 0d 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65  h..** jointype e
7a5e0 78 70 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69  xpresses the joi
7a5f0 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61  n between the ta
7a600 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76  ble and the prev
7a610 69 6f 75 73 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d  ious table...**.
7a620 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 6c 55 73  .** In the colUs
7a630 65 64 20 66 69 65 6c 64 2c 20 74 68 65 20 68 69  ed field, the hi
7a640 67 68 2d 6f 72 64 65 72 20 62 69 74 20 28 62 69  gh-order bit (bi
7a650 74 20 36 33 29 20 69 73 20 73 65 74 20 69 66 20  t 63) is set if 
7a660 74 68 65 20 74 61 62 6c 65 0d 0a 2a 2a 20 63 6f  the table..** co
7a670 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
7a680 20 36 33 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20   63 columns and 
7a690 74 68 65 20 36 34 2d 74 68 20 6f 72 20 6c 61 74  the 64-th or lat
7a6a0 65 72 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65  er column is use
7a6b0 64 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 53  d...*/..struct S
7a6c0 72 63 4c 69 73 74 20 7b 0d 0a 20 20 69 31 36 20  rcList {..  i16 
7a6d0 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20  nSrc;        /* 
7a6e0 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73  Number of tables
7a6f0 20 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 69   or subqueries i
7a700 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
7a710 65 20 2a 2f 0d 0a 20 20 69 31 36 20 6e 41 6c 6c  e */..  i16 nAll
7a720 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  oc;      /* Numb
7a730 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c  er of entries al
7a740 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62  located in a[] b
7a750 65 6c 6f 77 20 2a 2f 0d 0a 20 20 73 74 72 75 63  elow */..  struc
7a760 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b  t SrcList_item {
7a770 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74  ..    char *zDat
7a780 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  abase;  /* Name 
7a790 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64  of database hold
7a7a0 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ing this table *
7a7b0 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  /..    char *zNa
7a7c0 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  me;      /* Name
7a7d0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
7a7e0 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69  ..    char *zAli
7a7f0 61 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22  as;     /* The "
7a800 42 22 20 70 61 72 74 20 6f 66 20 61 20 22 41 20  B" part of a "A 
7a810 41 53 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a  AS B" phrase.  z
7a820 4e 61 6d 65 20 69 73 20 74 68 65 20 22 41 22 20  Name is the "A" 
7a830 2a 2f 0d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  */..    Table *p
7a840 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20  Tab;      /* An 
7a850 53 51 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73  SQL table corres
7a860 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65  ponding to zName
7a870 20 2a 2f 0d 0a 20 20 20 20 53 65 6c 65 63 74 20   */..    Select 
7a880 2a 70 53 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20  *pSelect;  /* A 
7a890 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
7a8a0 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f   used in place o
7a8b0 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  f a table name *
7a8c0 2f 0d 0a 20 20 20 20 69 6e 74 20 61 64 64 72 46  /..    int addrF
7a8d0 69 6c 6c 53 75 62 3b 20 20 2f 2a 20 41 64 64 72  illSub;  /* Addr
7a8e0 65 73 73 20 6f 66 20 73 75 62 72 6f 75 74 69 6e  ess of subroutin
7a8f0 65 20 74 6f 20 6d 61 6e 69 66 65 73 74 20 61 20  e to manifest a 
7a900 73 75 62 71 75 65 72 79 20 2a 2f 0d 0a 20 20 20  subquery */..   
7a910 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 3b 20   int regReturn; 
7a920 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
7a930 6f 6c 64 69 6e 67 20 72 65 74 75 72 6e 20 61 64  olding return ad
7a940 64 72 65 73 73 20 6f 66 20 61 64 64 72 46 69 6c  dress of addrFil
7a950 6c 53 75 62 20 2a 2f 0d 0a 20 20 20 20 75 38 20  lSub */..    u8 
7a960 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f  jointype;      /
7a970 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62  * Type of join b
7a980 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65  etween this able
7a990 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
7a9a0 73 20 2a 2f 0d 0a 20 20 20 20 75 38 20 6e 6f 74  s */..    u8 not
7a9b0 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54  Indexed;    /* T
7a9c0 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
7a9d0 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  a NOT INDEXED cl
7a9e0 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20 75 38 20  ause */..    u8 
7a9f0 69 73 43 6f 72 72 65 6c 61 74 65 64 3b 20 20 2f  isCorrelated;  /
7aa00 2a 20 54 72 75 65 20 69 66 20 73 75 62 2d 71 75  * True if sub-qu
7aa10 65 72 79 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ery is correlate
7aa20 64 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51  d */..#ifndef SQ
7aa30 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
7aa40 4e 0d 0a 20 20 20 20 75 38 20 69 53 65 6c 65 63  N..    u8 iSelec
7aa50 74 49 64 3b 20 20 20 20 20 2f 2a 20 49 66 20 70  tId;     /* If p
7aa60 53 65 6c 65 63 74 21 3d 30 2c 20 74 68 65 20 69  Select!=0, the i
7aa70 64 20 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c  d of the sub-sel
7aa80 65 63 74 20 69 6e 20 45 51 50 20 2a 2f 0d 0a 23  ect in EQP */..#
7aa90 65 6e 64 69 66 0d 0a 20 20 20 20 69 6e 74 20 69  endif..    int i
7aaa0 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20  Cursor;      /* 
7aab0 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
7aac0 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61  number used to a
7aad0 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65  ccess this table
7aae0 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a 70   */..    Expr *p
7aaf0 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  On;        /* Th
7ab00 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61  e ON clause of a
7ab10 20 6a 6f 69 6e 20 2a 2f 0d 0a 20 20 20 20 49 64   join */..    Id
7ab20 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20  List *pUsing;   
7ab30 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61  /* The USING cla
7ab40 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f  use of a join */
7ab50 0d 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f  ..    Bitmask co
7ab60 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e  lUsed;  /* Bit N
7ab70 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63   (1<<N) set if c
7ab80 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20  olumn N of pTab 
7ab90 69 73 20 75 73 65 64 20 2a 2f 0d 0a 20 20 20 20  is used */..    
7aba0 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20  char *zIndex;   
7abb0 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 20    /* Identifier 
7abc0 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 59  from "INDEXED BY
7abd0 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 73   <zIndex>" claus
7abe0 65 20 2a 2f 0d 0a 20 20 20 20 49 6e 64 65 78 20  e */..    Index 
7abf0 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49  *pIndex;    /* I
7ac00 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 63  ndex structure c
7ac10 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
7ac20 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a  zIndex, if any *
7ac30 2f 0d 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20  /..  } a[1];    
7ac40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
7ac50 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69  entry for each i
7ac60 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65  dentifier on the
7ac70 20 6c 69 73 74 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a   list */..};....
7ac80 2f 2a 0d 0a 2a 2a 20 50 65 72 6d 69 74 74 65 64  /*..** Permitted
7ac90 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 53   values of the S
7aca0 72 63 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74 79 70  rcList.a.jointyp
7acb0 65 20 66 69 65 6c 64 0d 0a 2a 2f 0d 0a 23 64 65  e field..*/..#de
7acc0 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 20 20 20  fine JT_INNER   
7acd0 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 41    0x0001    /* A
7ace0 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e 6e 65 72  ny kind of inner
7acf0 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 6e 20 2a   or cross join *
7ad00 2f 0d 0a 23 64 65 66 69 6e 65 20 4a 54 5f 43 52  /..#define JT_CR
7ad10 4f 53 53 20 20 20 20 20 30 78 30 30 30 32 20 20  OSS     0x0002  
7ad20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 75 73    /* Explicit us
7ad30 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53 20 6b  e of the CROSS k
7ad40 65 79 77 6f 72 64 20 2a 2f 0d 0a 23 64 65 66 69  eyword */..#defi
7ad50 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20  ne JT_NATURAL   
7ad60 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72 75  0x0004    /* Tru
7ad70 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61 6c  e for a "natural
7ad80 22 20 6a 6f 69 6e 20 2a 2f 0d 0a 23 64 65 66 69  " join */..#defi
7ad90 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20  ne JT_LEFT      
7ada0 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66  0x0008    /* Lef
7adb0 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0d  t outer join */.
7adc0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
7add0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
7ade0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
7adf0 6f 69 6e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  oin */..#define 
7ae00 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30  JT_OUTER     0x0
7ae10 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f  020    /* The "O
7ae20 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73  UTER" keyword is
7ae30 20 70 72 65 73 65 6e 74 20 2a 2f 0d 0a 23 64 65   present */..#de
7ae40 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20  fine JT_ERROR   
7ae50 20 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75    0x0040    /* u
7ae60 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70  nknown or unsupp
7ae70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
7ae80 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  */....../*..** A
7ae90 20 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63   WherePlan objec
7aea0 74 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74  t holds informat
7aeb0 69 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62  ion that describ
7aec0 65 73 20 61 20 6c 6f 6f 6b 75 70 0d 0a 2a 2a 20  es a lookup..** 
7aed0 73 74 72 61 74 65 67 79 2e 0d 0a 2a 2a 0d 0a 2a  strategy...**..*
7aee0 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73  * This object is
7aef0 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
7af00 6f 70 61 71 75 65 20 6f 75 74 73 69 64 65 20 6f  opaque outside o
7af10 66 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f  f the where.c mo
7af20 64 75 6c 65 2e 0d 0a 2a 2a 20 49 74 20 69 73 20  dule...** It is 
7af30 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 6f 6e  included here on
7af40 6c 79 20 73 6f 20 74 68 61 74 20 74 68 61 74 20  ly so that that 
7af50 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6b 6e  compiler will kn
7af60 6f 77 20 68 6f 77 20 62 69 67 20 69 74 0d 0a 2a  ow how big it..*
7af70 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 6f 66 20 74  * is.  None of t
7af80 68 65 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69  he fields in thi
7af90 73 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20  s object should 
7afa0 62 65 20 75 73 65 64 20 6f 75 74 73 69 64 65 20  be used outside 
7afb0 6f 66 0d 0a 2a 2a 20 74 68 65 20 77 68 65 72 65  of..** the where
7afc0 2e 63 20 6d 6f 64 75 6c 65 2e 0d 0a 2a 2a 0d 0a  .c module...**..
7afd0 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75 6e  ** Within the un
7afe0 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e 6c  ion, pIdx is onl
7aff0 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c  y used when wsFl
7b000 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58 45  ags&WHERE_INDEXE
7b010 44 20 69 73 20 74 72 75 65 2e 0d 0a 2a 2a 20 70  D is true...** p
7b020 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65  Term is only use
7b030 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57  d when wsFlags&W
7b040 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73  HERE_MULTI_OR is
7b050 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61   true.  And pVta
7b060 62 49 64 78 0d 0a 2a 2a 20 69 73 20 6f 6e 6c 79  bIdx..** is only
7b070 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61   used when wsFla
7b080 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c  gs&WHERE_VIRTUAL
7b090 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20  TABLE is true.  
7b0a0 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0d  It is never the.
7b0b0 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f  .** case that mo
7b0c0 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74  re than one of t
7b0d0 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hese conditions 
7b0e0 69 73 20 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 73 74  is true...*/..st
7b0f0 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b  ruct WherePlan {
7b100 0d 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b  ..  u32 wsFlags;
7b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7b120 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c     /* WHERE_* fl
7b130 61 67 73 20 74 68 61 74 20 64 65 73 63 72 69 62  ags that describ
7b140 65 20 74 68 65 20 73 74 72 61 74 65 67 79 20 2a  e the strategy *
7b150 2f 0d 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 20  /..  u32 nEq;   
7b160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7b170 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
7b180 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   == constraints 
7b190 2a 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f  */..  double nRo
7b1a0 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
7b1b0 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
7b1c0 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  d number of rows
7b1d0 20 28 66 6f 72 20 45 51 50 29 20 2a 2f 0d 0a 20   (for EQP) */.. 
7b1e0 20 75 6e 69 6f 6e 20 7b 0d 0a 20 20 20 20 49 6e   union {..    In
7b1f0 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
7b200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7b210 49 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 45  Index when WHERE
7b220 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65  _INDEXED is true
7b230 20 2a 2f 0d 0a 20 20 20 20 73 74 72 75 63 74 20   */..    struct 
7b240 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
7b250 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45  ;       /* WHERE
7b260 20 63 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72   clause term for
7b270 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0d 0a 20   OR-search */.. 
7b280 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
7b290 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b  _info *pVtabIdx;
7b2a0 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
7b2b0 6c 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  le index to use 
7b2c0 2a 2f 0d 0a 20 20 7d 20 75 3b 0d 0a 7d 3b 0d 0a  */..  } u;..};..
7b2d0 0d 0a 2f 2a 0d 0a 2a 2a 20 46 6f 72 20 65 61 63  ../*..** For eac
7b2e0 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e  h nested loop in
7b2f0 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
7b300 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
7b310 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0d 0a 2a  the WhereInfo..*
7b320 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  * structure cont
7b330 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e  ains a single in
7b340 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
7b350 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20  tructure.  This 
7b360 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 69 73  structure..** is
7b370 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
7b380 70 72 69 76 61 74 65 20 74 68 65 20 74 68 65 20  private the the 
7b390 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61  where.c module a
7b3a0 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  nd should not be
7b3b0 0d 0a 2a 2a 20 61 63 63 65 73 73 20 6f 72 20 6d  ..** access or m
7b3c0 6f 64 69 66 69 65 64 20 62 79 20 6f 74 68 65 72  odified by other
7b3d0 20 6d 6f 64 75 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a   modules...**..*
7b3e0 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 66  * The pIdxInfo f
7b3f0 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f 20  ield is used to 
7b400 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62 65  help pick the be
7b410 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0d 0a 2a  st index on a..*
7b420 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  * virtual table.
7b430 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70    The pIdxInfo p
7b440 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  ointer contains 
7b450 69 6e 64 65 78 69 6e 67 0d 0a 2a 2a 20 69 6e 66  indexing..** inf
7b460 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
7b470 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74   i-th table in t
7b480 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62  he FROM clause b
7b490 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67  efore reordering
7b4a0 2e 0d 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49  ...** All the pI
7b4b0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20  dxInfo pointers 
7b4c0 61 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65  are freed by whe
7b4d0 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20  reInfoFree() in 
7b4e0 77 68 65 72 65 2e 63 2e 0d 0a 2a 2a 20 41 6c 6c  where.c...** All
7b4f0 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
7b500 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57  on in the i-th W
7b510 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74  hereLevel object
7b520 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61   for the i-th ta
7b530 62 6c 65 0d 0a 2a 2a 20 61 66 74 65 72 20 46 52  ble..** after FR
7b540 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69  OM clause orderi
7b550 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  ng...*/..struct 
7b560 57 68 65 72 65 4c 65 76 65 6c 20 7b 0d 0a 20 20  WhereLevel {..  
7b570 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20  WherePlan plan; 
7b580 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79 20 70        /* query p
7b590 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65 6c 65  lan for this ele
7b5a0 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d  ment of the FROM
7b5b0 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69 6e   clause */..  in
7b5c0 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20  t iLeftJoin;    
7b5d0 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
7b5e0 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  ll used to imple
7b5f0 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20  ment LEFT OUTER 
7b600 4a 4f 49 4e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  JOIN */..  int i
7b610 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 20 20  TabCur;         
7b620 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
7b630 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65  sor used to acce
7b640 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d  ss the table */.
7b650 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20  .  int iIdxCur; 
7b660 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
7b670 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64  VDBE cursor used
7b680 20 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 20   to access pIdx 
7b690 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72 42 72  */..  int addrBr
7b6a0 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  k;          /* J
7b6b0 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61  ump here to brea
7b6c0 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f  k out of the loo
7b6d0 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72  p */..  int addr
7b6e0 4e 78 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Nxt;          /*
7b6f0 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 74   Jump here to st
7b700 61 72 74 20 74 68 65 20 6e 65 78 74 20 49 4e 20  art the next IN 
7b710 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 2a 2f 0d 0a  combination */..
7b720 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20    int addrCont; 
7b730 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
7b740 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  here to continue
7b750 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 6c   with the next l
7b760 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0d 0a 20 20  oop cycle */..  
7b770 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b 20 20  int addrFirst;  
7b780 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 69        /* First i
7b790 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 69 6e  nstruction of in
7b7a0 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20 6c 6f  terior of the lo
7b7b0 6f 70 20 2a 2f 0d 0a 20 20 75 38 20 69 46 72 6f  op */..  u8 iFro
7b7c0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  m;             /
7b7d0 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e  * Which entry in
7b7e0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
7b7f0 20 2a 2f 0d 0a 20 20 75 38 20 6f 70 2c 20 70 35   */..  u8 op, p5
7b800 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
7b810 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66  Opcode and P5 of
7b820 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74   the opcode that
7b830 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a   ends the loop *
7b840 2f 0d 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b  /..  int p1, p2;
7b850 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
7b860 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70  erands of the op
7b870 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64  code used to end
7b880 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0d 0a 20  s the loop */.. 
7b890 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20   union {        
7b8a0 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d         /* Inform
7b8b0 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e  ation that depen
7b8c0 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61  ds on plan.wsFla
7b8d0 67 73 20 2a 2f 0d 0a 20 20 20 20 73 74 72 75 63  gs */..    struc
7b8e0 74 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e  t {..      int n
7b8f0 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
7b900 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
7b910 74 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70  tries in aInLoop
7b920 5b 5d 20 2a 2f 0d 0a 20 20 20 20 20 20 73 74 72  [] */..      str
7b930 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0d 0a 20 20  uct InLoop {..  
7b940 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 3b 20        int iCur; 
7b950 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7b960 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
7b970 75 73 65 64 20 62 79 20 74 68 69 73 20 49 4e 20  used by this IN 
7b980 6f 70 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 20  operator */..   
7b990 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 6e 54       int addrInT
7b9a0 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  op;         /* T
7b9b0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f  op of the IN loo
7b9c0 70 20 2a 2f 0d 0a 20 20 20 20 20 20 7d 20 2a 61  p */..      } *a
7b9d0 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20  InLoop;         
7b9e0 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
7b9f0 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74   about each nest
7ba00 65 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  ed IN operator *
7ba10 2f 0d 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20  /..    } in;    
7ba20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7ba30 55 73 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77  Used when plan.w
7ba40 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f  sFlags&WHERE_IN_
7ba50 41 42 4c 45 20 2a 2f 0d 0a 20 20 7d 20 75 3b 0d  ABLE */..  } u;.
7ba60 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ...  /* The foll
7ba70 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73 20 72  owing field is r
7ba80 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74 20 6f  eally not part o
7ba90 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  f the current le
7baa0 76 65 6c 2e 20 20 42 75 74 0d 0a 20 20 2a 2a 20  vel.  But..  ** 
7bab0 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65 20  we need a place 
7bac0 74 6f 20 63 61 63 68 65 20 76 69 72 74 75 61 6c  to cache virtual
7bad0 20 74 61 62 6c 65 20 69 6e 64 65 78 20 69 6e 66   table index inf
7bae0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61 63  ormation for eac
7baf0 68 0d 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  h..  ** virtual 
7bb00 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
7bb10 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65  M clause and the
7bb20 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 72 75   WhereLevel stru
7bb30 63 74 75 72 65 20 69 73 0d 0a 20 20 2a 2a 20 61  cture is..  ** a
7bb40 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63   convenient plac
7bb50 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73  e since there is
7bb60 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c 20   one WhereLevel 
7bb70 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 6c  for each FROM cl
7bb80 61 75 73 65 0d 0a 20 20 2a 2a 20 65 6c 65 6d 65  ause..  ** eleme
7bb90 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  nt...  */..  sql
7bba0 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
7bbb0 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49  *pIdxInfo;  /* I
7bbc0 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d  ndex info for n-
7bbd0 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  th source table 
7bbe0 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
7bbf0 20 46 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61   Flags appropria
7bc00 74 65 20 66 6f 72 20 74 68 65 20 77 63 74 72 6c  te for the wctrl
7bc10 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  Flags parameter 
7bc20 6f 66 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  of sqlite3WhereB
7bc30 65 67 69 6e 28 29 0d 0a 2a 2a 20 61 6e 64 20 74  egin()..** and t
7bc40 68 65 20 57 68 65 72 65 49 6e 66 6f 2e 77 63 74  he WhereInfo.wct
7bc50 72 6c 46 6c 61 67 73 20 6d 65 6d 62 65 72 2e 0d  rlFlags member..
7bc60 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48 45  .*/..#define WHE
7bc70 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
7bc80 4c 20 20 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f  L   0x0000 /* No
7bc90 2d 6f 70 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  -op */..#define 
7bca0 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49  WHERE_ORDERBY_MI
7bcb0 4e 20 20 20 20 20 20 30 78 30 30 30 31 20 2f 2a  N      0x0001 /*
7bcc0 20 4f 52 44 45 52 20 42 59 20 70 72 6f 63 65 73   ORDER BY proces
7bcd0 73 69 6e 67 20 66 6f 72 20 6d 69 6e 28 29 20 66  sing for min() f
7bce0 75 6e 63 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  unc */..#define 
7bcf0 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41  WHERE_ORDERBY_MA
7bd00 58 20 20 20 20 20 20 30 78 30 30 30 32 20 2f 2a  X      0x0002 /*
7bd10 20 4f 52 44 45 52 20 42 59 20 70 72 6f 63 65 73   ORDER BY proces
7bd20 73 69 6e 67 20 66 6f 72 20 6d 61 78 28 29 20 66  sing for max() f
7bd30 75 6e 63 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  unc */..#define 
7bd40 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45  WHERE_ONEPASS_DE
7bd50 53 49 52 45 44 20 20 30 78 30 30 30 34 20 2f 2a  SIRED  0x0004 /*
7bd60 20 57 61 6e 74 20 74 6f 20 64 6f 20 6f 6e 65 2d   Want to do one-
7bd70 70 61 73 73 20 55 50 44 41 54 45 2f 44 45 4c 45  pass UPDATE/DELE
7bd80 54 45 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57  TE */..#define W
7bd90 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
7bda0 4f 4b 20 20 20 20 30 78 30 30 30 38 20 2f 2a 20  OK    0x0008 /* 
7bdb0 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 61 20 72  Ok to return a r
7bdc0 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ow more than onc
7bdd0 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48  e */..#define WH
7bde0 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 5f 43 4c  ERE_OMIT_OPEN_CL
7bdf0 4f 53 45 20 20 30 78 30 30 31 30 20 2f 2a 20 54  OSE  0x0010 /* T
7be00 61 62 6c 65 20 63 75 72 73 6f 72 73 20 61 72 65  able cursors are
7be10 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a 2f   already open */
7be20 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  ..#define WHERE_
7be30 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20  FORCE_TABLE     
7be40 20 30 78 30 30 32 30 20 2f 2a 20 44 6f 20 6e 6f   0x0020 /* Do no
7be50 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f  t use an index-o
7be60 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0d 0a 23  nly search */..#
7be70 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45  define WHERE_ONE
7be80 54 41 42 4c 45 5f 4f 4e 4c 59 20 20 20 20 30 78  TABLE_ONLY    0x
7be90 30 30 34 30 20 2f 2a 20 4f 6e 6c 79 20 63 6f 64  0040 /* Only cod
7bea0 65 20 74 68 65 20 31 73 74 20 74 61 62 6c 65 20  e the 1st table 
7beb0 69 6e 20 70 54 61 62 4c 69 73 74 20 2a 2f 0d 0a  in pTabList */..
7bec0 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 41 4e  #define WHERE_AN
7bed0 44 5f 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30  D_ONLY         0
7bee0 78 30 30 38 30 20 2f 2a 20 44 6f 6e 27 74 20 75  x0080 /* Don't u
7bef0 73 65 20 69 6e 64 69 63 65 73 20 66 6f 72 20 4f  se indices for O
7bf00 52 20 74 65 72 6d 73 20 2a 2f 0d 0a 0d 0a 2f 2a  R terms */..../*
7bf10 0d 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63  ..** The WHERE c
7bf20 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
7bf30 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f   routine has two
7bf40 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0d 0a 2a   halves.  The..*
7bf50 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65  * first part doe
7bf60 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  s the start of t
7bf70 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e  he WHERE loop an
7bf80 64 20 74 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a  d the second..**
7bf90 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74   half does the t
7bfa0 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45  ail of the WHERE
7bfb0 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61   loop.  An insta
7bfc0 6e 63 65 20 6f 66 0d 0a 2a 2a 20 74 68 69 73 20  nce of..** this 
7bfd0 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74  structure is ret
7bfe0 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72  urned by the fir
7bff0 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73  st half and pass
7c000 65 64 0d 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  ed..** into the 
7c010 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67  second half to g
7c020 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75  ive some continu
7c030 69 74 79 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74  ity...*/..struct
7c040 20 57 68 65 72 65 49 6e 66 6f 20 7b 0d 0a 20 20   WhereInfo {..  
7c050 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
7c060 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
7c070 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
7c080 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  ing context */..
7c090 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73    u16 wctrlFlags
7c0a0 3b 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20  ;      /* Flags 
7c0b0 6f 72 69 67 69 6e 61 6c 6c 79 20 70 61 73 73 65  originally passe
7c0c0 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72  d to sqlite3Wher
7c0d0 65 42 65 67 69 6e 28 29 20 2a 2f 0d 0a 20 20 75  eBegin() */..  u
7c0e0 38 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20  8 okOnePass;    
7c0f0 20 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65      /* Ok to use
7c100 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69   one-pass algori
7c110 74 68 6d 20 66 6f 72 20 55 50 44 41 54 45 20 6f  thm for UPDATE o
7c120 72 20 44 45 4c 45 54 45 20 2a 2f 0d 0a 20 20 75  r DELETE */..  u
7c130 38 20 75 6e 74 65 73 74 65 64 54 65 72 6d 73 3b  8 untestedTerms;
7c140 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 20 57      /* Not all W
7c150 48 45 52 45 20 74 65 72 6d 73 20 72 65 73 6f 6c  HERE terms resol
7c160 76 65 64 20 62 79 20 6f 75 74 65 72 20 6c 6f 6f  ved by outer loo
7c170 70 20 2a 2f 0d 0a 20 20 75 38 20 65 44 69 73 74  p */..  u8 eDist
7c180 69 6e 63 74 3b 0d 0a 20 20 53 72 63 4c 69 73 74  inct;..  SrcList
7c190 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20   *pTabList;     
7c1a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
7c1b0 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
7c1c0 20 6a 6f 69 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20   join */..  int 
7c1d0 69 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  iTop;           
7c1e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
7c1f0 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67  e very beginning
7c200 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f   of the WHERE lo
7c210 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f  op */..  int iCo
7c220 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20  ntinue;         
7c230 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
7c240 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  here to continue
7c250 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72   with next recor
7c260 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 42 72 65  d */..  int iBre
7c270 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ak;             
7c280 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
7c290 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ere to break out
7c2a0 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0d   of the loop */.
7c2b0 0a 20 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 20  .  int nLevel;  
7c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7c2d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
7c2e0 65 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0d 0a 20  ested loop */.. 
7c2f0 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61   struct WhereCla
7c300 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20  use *pWC;       
7c310 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e  /* Decomposition
7c320 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
7c330 61 75 73 65 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c  ause */..  doubl
7c340 65 20 73 61 76 65 64 4e 51 75 65 72 79 4c 6f 6f  e savedNQueryLoo
7c350 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61  p;        /* pPa
7c360 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 20  rse->nQueryLoop 
7c370 6f 75 74 73 69 64 65 20 74 68 65 20 57 48 45 52  outside the WHER
7c380 45 20 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 64 6f 75  E loop */..  dou
7c390 62 6c 65 20 6e 52 6f 77 4f 75 74 3b 20 20 20 20  ble nRowOut;    
7c3a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
7c3b0 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20  stimated number 
7c3c0 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20 2a  of output rows *
7c3d0 2f 0d 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  /..  WhereLevel 
7c3e0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  a[1];           
7c3f0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
7c400 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65  on about each ne
7c410 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45  st loop in WHERE
7c420 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 23 64 65 66 69   */..};....#defi
7c430 6e 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43  ne WHERE_DISTINC
7c440 54 5f 55 4e 49 51 55 45 20 31 0d 0a 23 64 65 66  T_UNIQUE 1..#def
7c450 69 6e 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e  ine WHERE_DISTIN
7c460 43 54 5f 4f 52 44 45 52 45 44 20 32 0d 0a 0d 0a  CT_ORDERED 2....
7c470 2f 2a 0d 0a 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e  /*..** A NameCon
7c480 74 65 78 74 20 64 65 66 69 6e 65 73 20 61 20 63  text defines a c
7c490 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20  ontext in which 
7c4a0 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65  to resolve table
7c4b0 20 61 6e 64 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20   and column..** 
7c4c0 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e 74  names.  The cont
7c4d0 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ext consists of 
7c4e0 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  a list of tables
7c4f0 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 29 20   (the pSrcList) 
7c500 66 69 65 6c 64 20 61 6e 64 0d 0a 2a 2a 20 61 20  field and..** a 
7c510 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78  list of named ex
7c520 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 74  pression (pEList
7c530 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 78  ).  The named ex
7c540 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61  pression list ma
7c550 79 0d 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20  y..** be NULL.  
7c560 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70  The pSrc corresp
7c570 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d  onds to the FROM
7c580 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
7c590 45 43 54 20 6f 72 0d 0a 2a 2a 20 74 6f 20 74 68  ECT or..** to th
7c5a0 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70  e table being op
7c5b0 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53  erated on by INS
7c5c0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
7c5d0 44 45 4c 45 54 45 2e 20 20 54 68 65 0d 0a 2a 2a  DELETE.  The..**
7c5e0 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f   pEList correspo
7c5f0 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c  nds to the resul
7c600 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t set of a SELEC
7c610 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f  T and is NULL fo
7c620 72 0d 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74  r..** other stat
7c630 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ements...**..** 
7c640 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e  NameContexts can
7c650 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65   be nested.  Whe
7c660 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  n resolving name
7c670 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73  s, the inner-mos
7c680 74 20 0d 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69  t ..** context i
7c690 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74  s searched first
7c6a0 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69  .  If no match i
7c6b0 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78  s found, the nex
7c6c0 74 20 6f 75 74 65 72 0d 0a 2a 2a 20 63 6f 6e 74  t outer..** cont
7c6d0 65 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20  ext is checked. 
7c6e0 20 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69   If there is sti
7c6f0 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65  ll no match, the
7c700 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0d 0a 2a   next context..*
7c710 2a 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54  * is checked.  T
7c720 68 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74  his process cont
7c730 69 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68  inues until eith
7c740 65 72 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f  er a match is fo
7c750 75 6e 64 0d 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63  und..** or all c
7c760 6f 6e 74 65 78 74 73 20 61 72 65 20 63 68 65 63  ontexts are chec
7c770 6b 2e 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68  k.  When a match
7c780 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e   is found, the n
7c790 52 65 66 20 6d 65 6d 62 65 72 20 6f 66 0d 0a 2a  Ref member of..*
7c7a0 2a 20 74 68 65 20 63 6f 6e 74 65 78 74 20 63 6f  * the context co
7c7b0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 74  ntaining the mat
7c7c0 63 68 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ch is incremente
7c7d0 64 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61 63 68  d. ..**..** Each
7c7e0 20 73 75 62 71 75 65 72 79 20 67 65 74 73 20 61   subquery gets a
7c7f0 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74 65 78 74   new NameContext
7c800 2e 20 20 54 68 65 20 70 4e 65 78 74 20 66 69 65  .  The pNext fie
7c810 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ld points to the
7c820 0d 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ..** NameContext
7c830 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 71   in the parent q
7c840 75 65 72 79 2e 20 20 54 68 75 73 20 74 68 65 20  uery.  Thus the 
7c850 70 72 6f 63 65 73 73 20 6f 66 20 73 63 61 6e 6e  process of scann
7c860 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 4e 61 6d 65  ing the..** Name
7c870 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 6f 72  Context list cor
7c880 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 61 72  responds to sear
7c890 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 73 75  ching through su
7c8a0 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 65 72  ccessively outer
7c8b0 0d 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20  ..** subqueries 
7c8c0 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61  looking for a ma
7c8d0 74 63 68 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74  tch...*/..struct
7c8e0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0d 0a   NameContext {..
7c8f0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
7c900 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
7c910 72 73 65 72 20 2a 2f 0d 0a 20 20 53 72 63 4c 69  rser */..  SrcLi
7c920 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20  st *pSrcList;   
7c930 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  /* One or more t
7c940 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65  ables used to re
7c950 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0d 0a  solve names */..
7c960 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
7c970 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e  st;    /* Option
7c980 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64  al list of named
7c990 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0d   expressions */.
7c9a0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
7c9b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
7c9c0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c  r of names resol
7c9d0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74  ved by this cont
7c9e0 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 45  ext */..  int nE
7c9f0 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rr;            /
7ca00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
7ca10 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77  rs encountered w
7ca20 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e  hile resolving n
7ca30 61 6d 65 73 20 2a 2f 0d 0a 20 20 75 38 20 61 6c  ames */..  u8 al
7ca40 6c 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20  lowAgg;         
7ca50 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e  /* Aggregate fun
7ca60 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68  ctions allowed h
7ca70 65 72 65 20 2a 2f 0d 0a 20 20 75 38 20 68 61 73  ere */..  u8 has
7ca80 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Agg;           /
7ca90 2a 20 54 72 75 65 20 69 66 20 61 67 67 72 65 67  * True if aggreg
7caa0 61 74 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f  ates are seen */
7cab0 0d 0a 20 20 75 38 20 69 73 43 68 65 63 6b 3b 20  ..  u8 isCheck; 
7cac0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
7cad0 20 69 66 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61   if resolving na
7cae0 6d 65 73 20 69 6e 20 61 20 43 48 45 43 4b 20 63  mes in a CHECK c
7caf0 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a 20 20  onstraint */..  
7cb00 69 6e 74 20 6e 44 65 70 74 68 3b 20 20 20 20 20  int nDepth;     
7cb10 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66       /* Depth of
7cb20 20 73 75 62 71 75 65 72 79 20 72 65 63 75 72 73   subquery recurs
7cb30 69 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f 20 72 65  ion. 1 for no re
7cb40 63 75 72 73 69 6f 6e 20 2a 2f 0d 0a 20 20 41 67  cursion */..  Ag
7cb50 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
7cb60 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
7cb70 6e 20 61 62 6f 75 74 20 61 67 67 72 65 67 61 74  n about aggregat
7cb80 65 73 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c  es at this level
7cb90 20 2a 2f 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74 65   */..  NameConte
7cba0 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  xt *pNext;  /* N
7cbb0 65 78 74 20 6f 75 74 65 72 20 6e 61 6d 65 20 63  ext outer name c
7cbc0 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c 20 66 6f  ontext.  NULL fo
7cbd0 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a 2f 0d 0a  r outermost */..
7cbe0 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20  };..../*..** An 
7cbf0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
7cc00 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
7cc10 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ure contains all
7cc20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a   information..**
7cc30 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72   needed to gener
7cc40 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ate code for a s
7cc50 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61  ingle SELECT sta
7cc60 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tement...**..** 
7cc70 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f  nLimit is set to
7cc80 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20   -1 if there is 
7cc90 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e  no LIMIT clause.
7cca0 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74    nOffset is set
7ccb0 20 74 6f 20 30 2e 0d 0a 2a 2a 20 49 66 20 74 68   to 0...** If th
7ccc0 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63  ere is a LIMIT c
7ccd0 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 65  lause, the parse
7cce0 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f  r sets nLimit to
7ccf0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
7cd00 65 0d 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20  e..** limit and 
7cd10 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76  nOffset to the v
7cd20 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73  alue of the offs
7cd30 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72  et (or 0 if ther
7cd40 65 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 6f 66 66  e is not..** off
7cd50 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72  set).  But later
7cd60 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20   on, nLimit and 
7cd70 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74  nOffset become t
7cd80 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
7cd90 6f 6e 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 56  ons..** in the V
7cda0 44 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20  DBE that record 
7cdb0 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66  the limit and of
7cdc0 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0d 0a  fset counters...
7cdd0 2a 2a 0d 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45  **..** addrOpenE
7cde0 70 68 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f  phm[] entries co
7cdf0 6e 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73  ntain the addres
7ce00 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65  s of OP_OpenEphe
7ce10 6d 65 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0d 0a  meral opcodes...
7ce20 2a 2a 20 54 68 65 73 65 20 61 64 64 72 65 73 73  ** These address
7ce30 65 73 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65  es must be store
7ce40 64 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  d so that we can
7ce50 20 67 6f 20 62 61 63 6b 20 61 6e 64 20 66 69 6c   go back and fil
7ce60 6c 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 50 34 5f  l in..** the P4_
7ce70 4b 45 59 49 4e 46 4f 20 61 6e 64 20 50 32 20 70  KEYINFO and P2 p
7ce80 61 72 61 6d 65 74 65 72 73 20 6c 61 74 65 72 2e  arameters later.
7ce90 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 4b 65    Neither the Ke
7cea0 79 49 6e 66 6f 20 6e 6f 72 0d 0a 2a 2a 20 74 68  yInfo nor..** th
7ceb0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
7cec0 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e 20 62 65  mns in P2 can be
7ced0 20 63 6f 6d 70 75 74 65 64 20 61 74 20 74 68 65   computed at the
7cee0 20 73 61 6d 65 20 74 69 6d 65 0d 0a 2a 2a 20 61   same time..** a
7cef0 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68  s the OP_OpenEph
7cf00 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  m instruction is
7cf10 20 63 6f 64 65 64 20 62 65 63 61 75 73 65 20 6e   coded because n
7cf20 6f 74 0d 0a 2a 2a 20 65 6e 6f 75 67 68 20 69 6e  ot..** enough in
7cf30 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
7cf40 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  the compound que
7cf50 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 74  ry is known at t
7cf60 68 61 74 20 70 6f 69 6e 74 2e 0d 0a 2a 2a 20 54  hat point...** T
7cf70 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61  he KeyInfo for a
7cf80 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61  ddrOpenTran[0] a
7cf90 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20  nd [1] contains 
7cfa0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
7cfb0 63 65 73 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ces..** for the 
7cfc0 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65  result set.  The
7cfd0 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64   KeyInfo for add
7cfe0 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e  rOpenTran[2] con
7cff0 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0d  tains collating.
7d000 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f  .** sequences fo
7d010 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  r the ORDER BY c
7d020 6c 61 75 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  lause...*/..stru
7d030 63 74 20 53 65 6c 65 63 74 20 7b 0d 0a 20 20 45  ct Select {..  E
7d040 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
7d050 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65        /* The fie
7d060 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c  lds of the resul
7d070 74 20 2a 2f 0d 0a 20 20 75 38 20 6f 70 3b 20 20  t */..  u8 op;  
7d080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
7d090 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49  * One of: TK_UNI
7d0a0 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54  ON TK_ALL TK_INT
7d0b0 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54  ERSECT TK_EXCEPT
7d0c0 20 2a 2f 0d 0a 20 20 63 68 61 72 20 61 66 66 69   */..  char affi
7d0d0 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  nity;         /*
7d0e0 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68   MakeRecord with
7d0f0 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66   this affinity f
7d100 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f 0d 0a 20  or SRT_Set */.. 
7d110 20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20   u16 selFlags;  
7d120 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f          /* Vario
7d130 75 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a  us SF_* values *
7d140 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  /..  SrcList *pS
7d150 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  rc;         /* T
7d160 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
7d170 2f 0d 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  /..  Expr *pWher
7d180 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
7d190 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
7d1a0 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  */..  ExprList *
7d1b0 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20  pGroupBy;    /* 
7d1c0 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  The GROUP BY cla
7d1d0 75 73 65 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  use */..  Expr *
7d1e0 70 48 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20  pHaving;        
7d1f0 20 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63   /* The HAVING c
7d200 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 45 78 70 72  lause */..  Expr
7d210 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20  List *pOrderBy; 
7d220 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
7d230 42 59 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20  BY clause */..  
7d240 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20  Select *pPrior; 
7d250 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f 72 20         /* Prior 
7d260 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70  select in a comp
7d270 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74  ound select stat
7d280 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 53 65 6c 65  ement */..  Sele
7d290 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ct *pNext;      
7d2a0 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c 65 63     /* Next selec
7d2b0 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 69 6e  t to the left in
7d2c0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0d 0a   a compound */..
7d2d0 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 74    Select *pRight
7d2e0 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 68  most;    /* Righ
7d2f0 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e  t-most select in
7d300 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
7d310 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d  ct statement */.
7d320 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
7d330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d            /* LIM
7d340 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  IT expression. N
7d350 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
7d360 65 64 2e 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  ed. */..  Expr *
7d370 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  pOffset;        
7d380 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65   /* OFFSET expre
7d390 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e  ssion. NULL mean
7d3a0 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0d 0a  s not used. */..
7d3b0 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f    int iLimit, iO
7d3c0 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f  ffset;   /* Memo
7d3d0 72 79 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c  ry registers hol
7d3e0 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46  ding LIMIT & OFF
7d3f0 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d  SET counters */.
7d400 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45  .  int addrOpenE
7d410 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f  phm[3];   /* OP_
7d420 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65  OpenEphem opcode
7d430 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69  s related to thi
7d440 73 20 73 65 6c 65 63 74 20 2a 2f 0d 0a 20 20 64  s select */..  d
7d450 6f 75 62 6c 65 20 6e 53 65 6c 65 63 74 52 6f 77  ouble nSelectRow
7d460 3b 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74  ;     /* Estimat
7d470 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  ed number of res
7d480 75 6c 74 20 72 6f 77 73 20 2a 2f 0d 0a 7d 3b 0d  ult rows */..};.
7d490 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 77 65  .../*..** Allowe
7d4a0 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c  d values for Sel
7d4b0 65 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54  ect.selFlags.  T
7d4c0 68 65 20 22 53 46 22 20 70 72 65 66 69 78 20 73  he "SF" prefix s
7d4d0 74 61 6e 64 73 20 66 6f 72 0d 0a 2a 2a 20 22 53  tands for..** "S
7d4e0 65 6c 65 63 74 20 46 6c 61 67 22 2e 0d 0a 2a 2f  elect Flag"...*/
7d4f0 0d 0a 23 64 65 66 69 6e 65 20 53 46 5f 44 69 73  ..#define SF_Dis
7d500 74 69 6e 63 74 20 20 20 20 20 20 20 20 30 78 30  tinct        0x0
7d510 31 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f  1  /* Output sho
7d520 75 6c 64 20 62 65 20 44 49 53 54 49 4e 43 54 20  uld be DISTINCT 
7d530 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 46 5f 52  */..#define SF_R
7d540 65 73 6f 6c 76 65 64 20 20 20 20 20 20 20 20 30  esolved        0
7d550 78 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69  x02  /* Identifi
7d560 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 72 65  ers have been re
7d570 73 6f 6c 76 65 64 20 2a 2f 0d 0a 23 64 65 66 69  solved */..#defi
7d580 6e 65 20 53 46 5f 41 67 67 72 65 67 61 74 65 20  ne SF_Aggregate 
7d590 20 20 20 20 20 20 30 78 30 34 20 20 2f 2a 20 43        0x04  /* C
7d5a0 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74  ontains aggregat
7d5b0 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0d 0a  e functions */..
7d5c0 23 64 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45  #define SF_UsesE
7d5d0 70 68 65 6d 65 72 61 6c 20 20 20 30 78 30 38 20  phemeral   0x08 
7d5e0 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65   /* Uses the Ope
7d5f0 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64  nEphemeral opcod
7d600 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 46  e */..#define SF
7d610 5f 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20  _Expanded       
7d620 20 30 78 31 30 20 20 2f 2a 20 73 71 6c 69 74 65   0x10  /* sqlite
7d630 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
7d640 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a  called on this *
7d650 2f 0d 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61  /..#define SF_Ha
7d660 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78  sTypeInfo     0x
7d670 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71  20  /* FROM subq
7d680 75 65 72 69 65 73 20 68 61 76 65 20 54 61 62 6c  ueries have Tabl
7d690 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0d 0a 23  e metadata */..#
7d6a0 64 65 66 69 6e 65 20 53 46 5f 55 73 65 53 6f 72  define SF_UseSor
7d6b0 74 65 72 20 20 20 20 20 20 20 30 78 34 30 20 20  ter       0x40  
7d6c0 2f 2a 20 53 6f 72 74 20 75 73 69 6e 67 20 61 20  /* Sort using a 
7d6d0 73 6f 72 74 65 72 20 2a 2f 0d 0a 0d 0a 0d 0a 2f  sorter */....../
7d6e0 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  *..** The result
7d6f0 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61  s of a select ca
7d700 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65 64  n be distributed
7d710 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73   in several ways
7d720 2e 20 20 54 68 65 0d 0a 2a 2a 20 22 53 52 54 22  .  The..** "SRT"
7d730 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53   prefix means "S
7d740 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 70  ELECT Result Typ
7d750 65 22 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  e"...*/..#define
7d760 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20   SRT_Union      
7d770 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    1  /* Store re
7d780 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20  sult as keys in 
7d790 61 6e 20 69 6e 64 65 78 20 2a 2f 0d 0a 23 64 65  an index */..#de
7d7a0 66 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20  fine SRT_Except 
7d7b0 20 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f        2  /* Remo
7d7c0 76 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61  ve result from a
7d7d0 20 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0d   UNION index */.
7d7e0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69  .#define SRT_Exi
7d7f0 73 74 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20  sts       3  /* 
7d800 53 74 6f 72 65 20 31 20 69 66 20 74 68 65 20 72  Store 1 if the r
7d810 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70  esult is not emp
7d820 74 79 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  ty */..#define S
7d830 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 20 20  RT_Discard      
7d840 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76  4  /* Do not sav
7d850 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 6e  e the results an
7d860 79 77 68 65 72 65 20 2a 2f 0d 0a 0d 0a 2f 2a 20  ywhere */..../* 
7d870 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
7d880 75 73 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66  use is ignored f
7d890 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62  or all of the ab
7d8a0 6f 76 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ove */..#define 
7d8b0 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79  IgnorableOrderby
7d8c0 28 58 29 20 28 28 58 2d 3e 65 44 65 73 74 29 3c  (X) ((X->eDest)<
7d8d0 3d 53 52 54 5f 44 69 73 63 61 72 64 29 0d 0a 0d  =SRT_Discard)...
7d8e0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f 75 74  .#define SRT_Out
7d8f0 70 75 74 20 20 20 20 20 20 20 35 20 20 2f 2a 20  put       5  /* 
7d900 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f 77 20  Output each row 
7d910 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0d 0a 23 64  of result */..#d
7d920 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 20  efine SRT_Mem   
7d930 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6f         6  /* Sto
7d940 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 6d  re result in a m
7d950 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0d 0a 23  emory cell */..#
7d960 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20  define SRT_Set  
7d970 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74          7  /* St
7d980 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b  ore results as k
7d990 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20  eys in an index 
7d9a0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 52 54 5f  */..#define SRT_
7d9b0 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20  Table        8  
7d9c0 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
7d9d0 61 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20  as data with an 
7d9e0 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20  automatic rowid 
7d9f0 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 52 54 5f  */..#define SRT_
7da00 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20  EphemTab     9  
7da10 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 69  /* Create transi
7da20 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72  ent tab and stor
7da30 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65  e like SRT_Table
7da40 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 52 54   */..#define SRT
7da50 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20  _Coroutine   10 
7da60 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
7da70 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73  ingle row of res
7da80 75 6c 74 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ult */..../*..**
7da90 20 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65   A structure use
7daa0 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  d to customize t
7dab0 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73  he behavior of s
7dac0 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20  qlite3Select(). 
7dad0 53 65 65 0d 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73  See..** comments
7dae0 20 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65   above sqlite3Se
7daf0 6c 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69  lect() for detai
7db00 6c 73 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  ls...*/..typedef
7db10 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 44 65   struct SelectDe
7db20 73 74 20 53 65 6c 65 63 74 44 65 73 74 3b 0d 0a  st SelectDest;..
7db30 73 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 73  struct SelectDes
7db40 74 20 7b 0d 0a 20 20 75 38 20 65 44 65 73 74 3b  t {..  u8 eDest;
7db50 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
7db60 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
7db70 65 20 72 65 73 75 6c 74 73 20 2a 2f 0d 0a 20 20  e results */..  
7db80 75 38 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20  u8 affinity;    
7db90 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 75 73    /* Affinity us
7dba0 65 64 20 77 68 65 6e 20 65 44 65 73 74 3d 3d 53  ed when eDest==S
7dbb0 52 54 5f 53 65 74 20 2a 2f 0d 0a 20 20 69 6e 74  RT_Set */..  int
7dbc0 20 69 50 61 72 6d 3b 20 20 20 20 20 20 20 20 2f   iParm;        /
7dbd0 2a 20 41 20 70 61 72 61 6d 65 74 65 72 20 75 73  * A parameter us
7dbe0 65 64 20 62 79 20 74 68 65 20 65 44 65 73 74 20  ed by the eDest 
7dbf0 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20  disposal method 
7dc00 2a 2f 0d 0a 20 20 69 6e 74 20 69 4d 65 6d 3b 20  */..  int iMem; 
7dc10 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20          /* Base 
7dc20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72  register where r
7dc30 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 74 74  esults are writt
7dc40 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65  en */..  int nMe
7dc50 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  m;         /* Nu
7dc60 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72  mber of register
7dc70 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0d 0a  s allocated */..
7dc80 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 75 72  };..../*..** Dur
7dc90 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ing code generat
7dca0 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  ion of statement
7dcb0 73 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74  s that do insert
7dcc0 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45  s into AUTOINCRE
7dcd0 4d 45 4e 54 20 0d 0a 2a 2a 20 74 61 62 6c 65 73  MENT ..** tables
7dce0 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
7dcf0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61  information is a
7dd00 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 54  ttached to the T
7dd10 61 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70  able.u.autoInc.p
7dd20 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20  ..** pointer of 
7dd30 65 61 63 68 20 61 75 74 6f 69 6e 63 72 65 6d 65  each autoincreme
7dd40 6e 74 20 74 61 62 6c 65 20 74 6f 20 72 65 63 6f  nt table to reco
7dd50 72 64 20 73 6f 6d 65 20 73 69 64 65 20 69 6e 66  rd some side inf
7dd60 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0d 0a 2a  ormation that..*
7dd70 2a 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * the code gener
7dd80 61 74 6f 72 20 6e 65 65 64 73 2e 20 20 57 65 20  ator needs.  We 
7dd90 68 61 76 65 20 74 6f 20 6b 65 65 70 20 70 65 72  have to keep per
7dda0 2d 74 61 62 6c 65 20 61 75 74 6f 69 6e 63 72 65  -table autoincre
7ddb0 6d 65 6e 74 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ment..** informa
7ddc0 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 69 6e 73  tion in case ins
7ddd0 65 72 74 73 20 61 72 65 20 64 6f 77 6e 20 77 69  erts are down wi
7dde0 74 68 69 6e 20 74 72 69 67 67 65 72 73 2e 20 20  thin triggers.  
7ddf0 54 72 69 67 67 65 72 73 20 64 6f 20 6e 6f 74 0d  Triggers do not.
7de00 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6f  .** normally coo
7de10 72 64 69 6e 61 74 65 20 74 68 65 69 72 20 61 63  rdinate their ac
7de20 74 69 76 69 74 69 65 73 2c 20 62 75 74 20 77 65  tivities, but we
7de30 20 64 6f 20 6e 65 65 64 20 74 6f 20 63 6f 6f 72   do need to coor
7de40 64 69 6e 61 74 65 20 74 68 65 0d 0a 2a 2a 20 6c  dinate the..** l
7de50 6f 61 64 69 6e 67 20 61 6e 64 20 73 61 76 69 6e  oading and savin
7de60 67 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d 65  g of autoincreme
7de70 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d  nt information..
7de80 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 41 75 74 6f  .*/..struct Auto
7de90 69 6e 63 49 6e 66 6f 20 7b 0d 0a 20 20 41 75 74  incInfo {..  Aut
7dea0 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e 65 78 74 3b  oincInfo *pNext;
7deb0 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 66 6f 20     /* Next info 
7dec0 62 6c 6f 63 6b 20 69 6e 20 61 20 6c 69 73 74 20  block in a list 
7ded0 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0d 0a  of them all */..
7dee0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
7def0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
7df00 20 74 68 69 73 20 69 6e 66 6f 20 62 6c 6f 63 6b   this info block
7df10 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0d 0a 20   refers to */.. 
7df20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
7df30 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
7df40 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d  in sqlite3.aDb[]
7df50 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c   of database hol
7df60 64 69 6e 67 20 70 54 61 62 20 2a 2f 0d 0a 20 20  ding pTab */..  
7df70 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 20 20  int regCtr;     
7df80 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
7df90 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  register holding
7dfa0 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 6e 74   the rowid count
7dfb0 65 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  er */..};..../*.
7dfc0 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20  .** Size of the 
7dfd0 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0d 0a 2a 2f  column cache..*/
7dfe0 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7dff0 5f 4e 5f 43 4f 4c 43 41 43 48 45 0d 0a 23 20 64  _N_COLCACHE..# d
7e000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43  efine SQLITE_N_C
7e010 4f 4c 43 41 43 48 45 20 31 30 0d 0a 23 65 6e 64  OLCACHE 10..#end
7e020 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 20  if..../*..** At 
7e030 6c 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e  least one instan
7e040 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
7e050 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
7e060 20 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63   created for eac
7e070 68 20 0d 0a 2a 2a 20 74 72 69 67 67 65 72 20 74  h ..** trigger t
7e080 68 61 74 20 6d 61 79 20 62 65 20 66 69 72 65 64  hat may be fired
7e090 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 61   while parsing a
7e0a0 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  n INSERT, UPDATE
7e0b0 20 6f 72 20 44 45 4c 45 54 45 0d 0a 2a 2a 20 73   or DELETE..** s
7e0c0 74 61 74 65 6d 65 6e 74 2e 20 41 6c 6c 20 73 75  tatement. All su
7e0d0 63 68 20 6f 62 6a 65 63 74 73 20 61 72 65 20 73  ch objects are s
7e0e0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6c 69 6e  tored in the lin
7e0f0 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20  ked list headed 
7e100 61 74 0d 0a 2a 2a 20 50 61 72 73 65 2e 70 54 72  at..** Parse.pTr
7e110 69 67 67 65 72 50 72 67 20 61 6e 64 20 64 65 6c  iggerPrg and del
7e120 65 74 65 64 20 6f 6e 63 65 20 73 74 61 74 65 6d  eted once statem
7e130 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ent compilation 
7e140 68 61 73 20 62 65 65 6e 0d 0a 2a 2a 20 63 6f 6d  has been..** com
7e150 70 6c 65 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  pleted...**..** 
7e160 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 72  A Vdbe sub-progr
7e170 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  am that implemen
7e180 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 20  ts the body and 
7e190 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74  WHEN clause of t
7e1a0 72 69 67 67 65 72 0d 0a 2a 2a 20 54 72 69 67 67  rigger..** Trigg
7e1b0 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20  erPrg.pTrigger, 
7e1c0 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75  assuming a defau
7e1d0 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  lt ON CONFLICT c
7e1e0 6c 61 75 73 65 20 6f 66 0d 0a 2a 2a 20 54 72 69  lause of..** Tri
7e1f0 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20  ggerPrg.orconf, 
7e200 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
7e210 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f   TriggerPrg.pPro
7e220 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0d 0a  gram variable...
7e230 2a 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72  ** The Parse.pTr
7e240 69 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65  iggerPrg list ne
7e250 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f  ver contains two
7e260 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68   entries with th
7e270 65 20 73 61 6d 65 0d 0a 2a 2a 20 76 61 6c 75 65  e same..** value
7e280 73 20 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67  s for both pTrig
7e290 67 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0d  ger and orconf..
7e2a0 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 54 72 69 67  .**..** The Trig
7e2b0 67 65 72 50 72 67 2e 61 43 6f 6c 6d 61 73 6b 5b  gerPrg.aColmask[
7e2c0 30 5d 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  0] variable is s
7e2d0 65 74 20 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20  et to a mask of 
7e2e0 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 0d 0a 2a  old.* columns..*
7e2f0 2a 20 61 63 63 65 73 73 65 64 20 28 6f 72 20 73  * accessed (or s
7e300 65 74 20 74 6f 20 30 20 66 6f 72 20 74 72 69 67  et to 0 for trig
7e310 67 65 72 73 20 66 69 72 65 64 20 61 73 20 61 20  gers fired as a 
7e320 72 65 73 75 6c 74 20 6f 66 20 49 4e 53 45 52 54  result of INSERT
7e330 20 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73   ..** statements
7e340 29 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68  ). Similarly, th
7e350 65 20 54 72 69 67 67 65 72 50 72 67 2e 61 43 6f  e TriggerPrg.aCo
7e360 6c 6d 61 73 6b 5b 31 5d 20 76 61 72 69 61 62 6c  lmask[1] variabl
7e370 65 20 69 73 20 73 65 74 20 74 6f 0d 0a 2a 2a 20  e is set to..** 
7e380 61 20 6d 61 73 6b 20 6f 66 20 6e 65 77 2e 2a 20  a mask of new.* 
7e390 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 62 79 20  columns used by 
7e3a0 74 68 65 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f  the program...*/
7e3b0 0d 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  ..struct Trigger
7e3c0 50 72 67 20 7b 0d 0a 20 20 54 72 69 67 67 65 72  Prg {..  Trigger
7e3d0 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
7e3e0 20 2f 2a 20 54 72 69 67 67 65 72 20 74 68 69 73   /* Trigger this
7e3f0 20 70 72 6f 67 72 61 6d 20 77 61 73 20 63 6f 64   program was cod
7e400 65 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e  ed from */..  in
7e410 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20  t orconf;       
7e420 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
7e430 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
7e440 69 63 79 20 2a 2f 0d 0a 20 20 53 75 62 50 72 6f  icy */..  SubPro
7e450 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
7e460 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70    /* Program imp
7e470 6c 65 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67  lementing pTrigg
7e480 65 72 2f 6f 72 63 6f 6e 66 20 2a 2f 0d 0a 20 20  er/orconf */..  
7e490 75 33 32 20 61 43 6f 6c 6d 61 73 6b 5b 32 5d 3b  u32 aColmask[2];
7e4a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 73          /* Masks
7e4b0 20 6f 66 20 6f 6c 64 2e 2a 2c 20 6e 65 77 2e 2a   of old.*, new.*
7e4c0 20 63 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 65   columns accesse
7e4d0 64 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 50  d */..  TriggerP
7e4e0 72 67 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  rg *pNext;      
7e4f0 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 20 69 6e  /* Next entry in
7e500 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50   Parse.pTriggerP
7e510 72 67 20 6c 69 73 74 20 2a 2f 0d 0a 7d 3b 0d 0a  rg list */..};..
7e520 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 79 44 62  ../*..** The yDb
7e530 4d 61 73 6b 20 64 61 74 61 74 79 70 65 20 66 6f  Mask datatype fo
7e540 72 20 74 68 65 20 62 69 74 6d 61 73 6b 20 6f 66  r the bitmask of
7e550 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61   all attached da
7e560 74 61 62 61 73 65 73 2e 0d 0a 2a 2f 0d 0a 23 69  tabases...*/..#i
7e570 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  f SQLITE_MAX_ATT
7e580 41 43 48 45 44 3e 33 30 0d 0a 20 20 74 79 70 65  ACHED>30..  type
7e590 64 65 66 20 73 71 6c 69 74 65 33 5f 75 69 6e 74  def sqlite3_uint
7e5a0 36 34 20 79 44 62 4d 61 73 6b 3b 0d 0a 23 65 6c  64 yDbMask;..#el
7e5b0 73 65 0d 0a 20 20 74 79 70 65 64 65 66 20 75 6e  se..  typedef un
7e5c0 73 69 67 6e 65 64 20 69 6e 74 20 79 44 62 4d 61  signed int yDbMa
7e5d0 73 6b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  sk;..#endif..../
7e5e0 2a 0d 0a 2a 2a 20 41 6e 20 53 51 4c 20 70 61 72  *..** An SQL par
7e5f0 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 41 20  ser context.  A 
7e600 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73 74 72  copy of this str
7e610 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64  ucture is passed
7e620 20 74 68 72 6f 75 67 68 0d 0a 2a 2a 20 74 68 65   through..** the
7e630 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77 6e   parser and down
7e640 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70 61   into all the pa
7e650 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74  rser action rout
7e660 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0d  ine in order to.
7e670 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64  .** carry around
7e680 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
7e690 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74  t is global to t
7e6a0 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e  he entire parse.
7e6b0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 74 72  ..**..** The str
7e6c0 75 63 74 75 72 65 20 69 73 20 64 69 76 69 64 65  ucture is divide
7e6d0 64 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73  d into two parts
7e6e0 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73  .  When the pars
7e6f0 65 72 20 61 6e 64 20 63 6f 64 65 0d 0a 2a 2a 20  er and code..** 
7e700 67 65 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 68  generate call th
7e710 65 6d 73 65 6c 76 65 73 20 72 65 63 75 72 73 69  emselves recursi
7e720 76 65 6c 79 2c 20 74 68 65 20 66 69 72 73 74 20  vely, the first 
7e730 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 72 75  part of the stru
7e740 63 74 75 72 65 0d 0a 2a 2a 20 69 73 20 63 6f 6e  cture..** is con
7e750 73 74 61 6e 74 20 62 75 74 20 74 68 65 20 73 65  stant but the se
7e760 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72 65 73  cond part is res
7e770 65 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  et at the beginn
7e780 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 0d 0a  ing and end of..
7e790 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f  ** each recursio
7e7a0 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e  n...**..** The n
7e7b0 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54  TableLock and aT
7e7c0 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c  ableLock variabl
7e7d0 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 64  es are only used
7e7e0 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63   if the shared-c
7e7f0 61 63 68 65 20 0d 0a 2a 2a 20 66 65 61 74 75 72  ache ..** featur
7e800 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
7e810 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
7e820 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
7e830 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0d  true). They are.
7e840 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72  .** used to stor
7e850 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62  e the set of tab
7e860 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65  le-locks require
7e870 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
7e880 6e 74 20 62 65 69 6e 67 0d 0a 2a 2a 20 63 6f 6d  nt being..** com
7e890 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20  piled. Function 
7e8a0 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
7e8b0 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64  () is used to ad
7e8c0 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  d entries to the
7e8d0 0d 0a 2a 2a 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a  ..** list...*/..
7e8e0 73 74 72 75 63 74 20 50 61 72 73 65 20 7b 0d 0a  struct Parse {..
7e8f0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
7e900 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
7e910 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75  in database stru
7e920 63 74 75 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  cture */..  int 
7e930 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
7e940 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
7e950 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20 2a  from execution *
7e960 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  /..  char *zErrM
7e970 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20  sg;       /* An 
7e980 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f  error message */
7e990 0d 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  ..  Vdbe *pVdbe;
7e9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65           /* An e
7e9b0 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74  ngine for execut
7e9c0 69 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74  ing database byt
7e9d0 65 63 6f 64 65 20 2a 2f 0d 0a 20 20 75 38 20 63  ecode */..  u8 c
7e9e0 6f 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20  olNamesSet;     
7e9f0 20 2f 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f   /* TRUE after O
7ea00 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73  P_ColumnName has
7ea10 20 62 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20   been issued to 
7ea20 70 56 64 62 65 20 2a 2f 0d 0a 20 20 75 38 20 63  pVdbe */..  u8 c
7ea30 68 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20  heckSchema;     
7ea40 20 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d   /* Causes schem
7ea50 61 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61  a cookie check a
7ea60 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f  fter an error */
7ea70 0d 0a 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20  ..  u8 nested;  
7ea80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
7ea90 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c  er of nested cal
7eaa0 6c 73 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  ls to the parser
7eab0 2f 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20  /code generator 
7eac0 2a 2f 0d 0a 20 20 75 38 20 6e 54 65 6d 70 52 65  */..  u8 nTempRe
7ead0 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  g;         /* Nu
7eae0 6d 62 65 72 20 6f 66 20 74 65 6d 70 6f 72 61 72  mber of temporar
7eaf0 79 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 61  y registers in a
7eb00 54 65 6d 70 52 65 67 5b 5d 20 2a 2f 0d 0a 20 20  TempReg[] */..  
7eb10 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 3b 20 20  u8 nTempInUse;  
7eb20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
7eb30 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 63 75 72  f aTempReg[] cur
7eb40 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f  rently checked o
7eb50 75 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 54 65  ut */..  int aTe
7eb60 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 2f 2a  mpReg[8];     /*
7eb70 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 66 6f   Holding area fo
7eb80 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  r temporary regi
7eb90 73 74 65 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  sters */..  int 
7eba0 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20  nRangeReg;      
7ebb0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
7ebc0 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
7ebd0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69  er block */..  i
7ebe0 6e 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20  nt iRangeReg;   
7ebf0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
7ec00 69 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61  ister in tempora
7ec10 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63  ry register bloc
7ec20 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 45 72 72  k */..  int nErr
7ec30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
7ec40 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
7ec50 20 73 65 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20   seen */..  int 
7ec60 6e 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  nTab;           
7ec70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72   /* Number of pr
7ec80 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
7ec90 65 64 20 56 44 42 45 20 63 75 72 73 6f 72 73 20  ed VDBE cursors 
7eca0 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20  */..  int nMem; 
7ecb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
7ecc0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63  mber of memory c
7ecd0 65 6c 6c 73 20 75 73 65 64 20 73 6f 20 66 61 72  ells used so far
7ece0 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 65 74 3b   */..  int nSet;
7ecf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
7ed00 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 75 73  umber of sets us
7ed10 65 64 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20 20  ed so far */..  
7ed20 69 6e 74 20 6e 4f 6e 63 65 3b 20 20 20 20 20 20  int nOnce;      
7ed30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
7ed40 66 20 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 75  f OP_Once instru
7ed50 63 74 69 6f 6e 73 20 73 6f 20 66 61 72 20 2a 2f  ctions so far */
7ed60 0d 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20  ..  int ckBase; 
7ed70 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
7ed80 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74   register of dat
7ed90 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63  a during check c
7eda0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0d 0a 20  onstraints */.. 
7edb0 20 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c   int iCacheLevel
7edc0 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68  ;     /* ColCach
7edd0 65 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f  e valid when aCo
7ede0 6c 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c  lCache[].iLevel<
7edf0 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0d  =iCacheLevel */.
7ee00 0a 20 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74  .  int iCacheCnt
7ee10 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74  ;       /* Count
7ee20 65 72 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72  er used to gener
7ee30 61 74 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e  ate aColCache[].
7ee40 6c 72 75 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 20  lru values */.. 
7ee50 20 75 38 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20   u8 nColCache;  
7ee60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
7ee70 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
7ee80 6f 6c 43 61 63 68 65 5b 5d 20 2a 2f 0d 0a 20 20  olCache[] */..  
7ee90 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20  u8 iColCache;   
7eea0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74       /* Next ent
7eeb0 72 79 20 69 6e 20 61 43 6f 6c 43 61 63 68 65 5b  ry in aColCache[
7eec0 5d 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0d  ] to replace */.
7eed0 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
7eee0 63 68 65 20 7b 0d 0a 20 20 20 20 69 6e 74 20 69  che {..    int i
7eef0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
7ef00 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72   /* Table cursor
7ef10 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20 20   number */..    
7ef20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
7ef30 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
7ef40 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0d  olumn number */.
7ef50 0a 20 20 20 20 75 38 20 74 65 6d 70 52 65 67 3b  .    u8 tempReg;
7ef60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 52             /* iR
7ef70 65 67 20 69 73 20 61 20 74 65 6d 70 20 72 65 67  eg is a temp reg
7ef80 69 73 74 65 72 20 74 68 61 74 20 6e 65 65 64 73  ister that needs
7ef90 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0d   to be freed */.
7efa0 0a 20 20 20 20 69 6e 74 20 69 4c 65 76 65 6c 3b  .    int iLevel;
7efb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
7efc0 73 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f 0d 0a  sting level */..
7efd0 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20      int iReg;   
7efe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
7eff0 20 77 69 74 68 20 76 61 6c 75 65 20 6f 66 20 74   with value of t
7f000 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d 65  his column. 0 me
7f010 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0d 0a 20 20  ans none. */..  
7f020 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20    int lru;      
7f030 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 73 74          /* Least
7f040 20 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 65   recently used e
7f050 6e 74 72 79 20 68 61 73 20 74 68 65 20 73 6d 61  ntry has the sma
7f060 6c 6c 65 73 74 20 76 61 6c 75 65 20 2a 2f 0d 0a  llest value */..
7f070 20 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b 53 51    } aColCache[SQ
7f080 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d  LITE_N_COLCACHE]
7f090 3b 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 61  ;  /* One for ea
7f0a0 63 68 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  ch column cache 
7f0b0 65 6e 74 72 79 20 2a 2f 0d 0a 20 20 79 44 62 4d  entry */..  yDbM
7f0c0 61 73 6b 20 77 72 69 74 65 4d 61 73 6b 3b 20 20  ask writeMask;  
7f0d0 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74   /* Start a writ
7f0e0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
7f0f0 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73   these databases
7f100 20 2a 2f 0d 0a 20 20 79 44 62 4d 61 73 6b 20 63   */..  yDbMask c
7f110 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 2f 2a 20 42  ookieMask;  /* B
7f120 69 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61  itmask of schema
7f130 20 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61   verified databa
7f140 73 65 73 20 2a 2f 0d 0a 20 20 75 38 20 69 73 4d  ses */..  u8 isM
7f150 75 6c 74 69 57 72 69 74 65 3b 20 20 20 20 20 2f  ultiWrite;     /
7f160 2a 20 54 72 75 65 20 69 66 20 73 74 61 74 65 6d  * True if statem
7f170 65 6e 74 20 6d 61 79 20 61 66 66 65 63 74 2f 69  ent may affect/i
7f180 6e 73 65 72 74 20 6d 75 6c 74 69 70 6c 65 20 72  nsert multiple r
7f190 6f 77 73 20 2a 2f 0d 0a 20 20 75 38 20 6d 61 79  ows */..  u8 may
7f1a0 41 62 6f 72 74 3b 20 20 20 20 20 20 20 20 20 2f  Abort;         /
7f1b0 2a 20 54 72 75 65 20 69 66 20 73 74 61 74 65 6d  * True if statem
7f1c0 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 20 61 6e  ent may throw an
7f1d0 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e   ABORT exception
7f1e0 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 6f 6f 6b 69   */..  int cooki
7f1f0 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 41  eGoto;      /* A
7f200 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f 74  ddress of OP_Got
7f210 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 69  o to cookie veri
7f220 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 20  fier subroutine 
7f230 2a 2f 0d 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65  */..  int cookie
7f240 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
7f250 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
7f260 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
7f270 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
7f280 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7f290 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
7f2a0 48 45 0d 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65  HE..  int nTable
7f2b0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
7f2c0 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  Number of locks 
7f2d0 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f  in aTableLock */
7f2e0 0d 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61  ..  TableLock *a
7f2f0 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65  TableLock; /* Re
7f300 71 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63  quired table loc
7f310 6b 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61  ks for shared-ca
7f320 63 68 65 20 6d 6f 64 65 20 2a 2f 0d 0a 23 65 6e  che mode */..#en
7f330 64 69 66 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f  dif..  int regRo
7f340 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 52  wid;        /* R
7f350 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
7f360 72 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 20  rowid of CREATE 
7f370 54 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0d 0a  TABLE entry */..
7f380 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20    int regRoot;  
7f390 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
7f3a0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20  er holding root 
7f3b0 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
7f3c0 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0d 0a  new objects */..
7f3d0 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70    AutoincInfo *p
7f3e0 41 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f 72 6d  Ainc;  /* Inform
7f3f0 61 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 54 4f  ation about AUTO
7f400 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65  INCREMENT counte
7f410 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4d 61  rs */..  int nMa
7f420 78 41 72 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  xArg;         /*
7f430 20 4d 61 78 20 61 72 67 73 20 70 61 73 73 65 64   Max args passed
7f440 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f   to user functio
7f450 6e 20 62 79 20 73 75 62 2d 70 72 6f 67 72 61 6d  n by sub-program
7f460 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 66 6f   */....  /* Info
7f470 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 77 68 69  rmation used whi
7f480 6c 65 20 63 6f 64 69 6e 67 20 74 72 69 67 67 65  le coding trigge
7f490 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0d 0a  r programs. */..
7f4a0 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
7f4b0 65 6c 3b 20 20 20 20 2f 2a 20 50 61 72 73 65 20  el;    /* Parse 
7f4c0 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 6d 61  structure for ma
7f4d0 69 6e 20 70 72 6f 67 72 61 6d 20 28 6f 72 20 4e  in program (or N
7f4e0 55 4c 4c 29 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  ULL) */..  Table
7f4f0 20 2a 70 54 72 69 67 67 65 72 54 61 62 3b 20 20   *pTriggerTab;  
7f500 2f 2a 20 54 61 62 6c 65 20 74 72 69 67 67 65 72  /* Table trigger
7f510 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 64 65  s are being code
7f520 64 20 66 6f 72 20 2a 2f 0d 0a 20 20 75 33 32 20  d for */..  u32 
7f530 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  oldmask;        
7f540 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e   /* Mask of old.
7f550 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65  * columns refere
7f560 6e 63 65 64 20 2a 2f 0d 0a 20 20 75 33 32 20 6e  nced */..  u32 n
7f570 65 77 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  ewmask;         
7f580 2f 2a 20 4d 61 73 6b 20 6f 66 20 6e 65 77 2e 2a  /* Mask of new.*
7f590 20 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e   columns referen
7f5a0 63 65 64 20 2a 2f 0d 0a 20 20 75 38 20 65 54 72  ced */..  u8 eTr
7f5b0 69 67 67 65 72 4f 70 3b 20 20 20 20 20 20 20 2f  iggerOp;       /
7f5c0 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  * TK_UPDATE, TK_
7f5d0 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f 44 45 4c  INSERT or TK_DEL
7f5e0 45 54 45 20 2a 2f 0d 0a 20 20 75 38 20 65 4f 72  ETE */..  u8 eOr
7f5f0 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f  conf;          /
7f600 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e  * Default ON CON
7f610 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 6f 72  FLICT policy for
7f620 20 74 72 69 67 67 65 72 20 73 74 65 70 73 20 2a   trigger steps *
7f630 2f 0d 0a 20 20 75 38 20 64 69 73 61 62 6c 65 54  /..  u8 disableT
7f640 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 72 75  riggers;  /* Tru
7f650 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72 69  e to disable tri
7f660 67 67 65 72 73 20 2a 2f 0d 0a 20 20 64 6f 75 62  ggers */..  doub
7f670 6c 65 20 6e 51 75 65 72 79 4c 6f 6f 70 3b 20 20  le nQueryLoop;  
7f680 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75   /* Estimated nu
7f690 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 69 6f  mber of iteratio
7f6a0 6e 73 20 6f 66 20 61 20 71 75 65 72 79 20 2a 2f  ns of a query */
7f6b0 0d 0a 0d 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69  ....  /* Above i
7f6c0 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 65  s constant betwe
7f6d0 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 20  en recursions.  
7f6e0 42 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 62  Below is reset b
7f6f0 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 0d  efore and after.
7f700 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72  .  ** each recur
7f710 73 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 69 6e 74  sion */....  int
7f720 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20   nVar;          
7f730 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27    /* Number of '
7f740 3f 27 20 76 61 72 69 61 62 6c 65 73 20 73 65 65  ?' variables see
7f750 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 73 6f 20  n in the SQL so 
7f760 66 61 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 7a  far */..  int nz
7f770 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
7f780 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 76 61 69  * Number of avai
7f790 6c 61 62 6c 65 20 73 6c 6f 74 73 20 69 6e 20 61  lable slots in a
7f7a0 7a 56 61 72 5b 5d 20 2a 2f 0d 0a 20 20 63 68 61  zVar[] */..  cha
7f7b0 72 20 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20  r **azVar;      
7f7c0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f    /* Pointers to
7f7d0 20 6e 61 6d 65 73 20 6f 66 20 70 61 72 61 6d 65   names of parame
7f7e0 74 65 72 73 20 2a 2f 0d 0a 20 20 56 64 62 65 20  ters */..  Vdbe 
7f7f0 2a 70 52 65 70 72 65 70 61 72 65 3b 20 20 20 20  *pReprepare;    
7f800 2f 2a 20 56 4d 20 62 65 69 6e 67 20 72 65 70 72  /* VM being repr
7f810 65 70 61 72 65 64 20 28 73 71 6c 69 74 65 33 52  epared (sqlite3R
7f820 65 70 72 65 70 61 72 65 28 29 29 20 2a 2f 0d 0a  eprepare()) */..
7f830 20 20 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20    int nAlias;   
7f840 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
7f850 20 6f 66 20 61 6c 69 61 73 65 64 20 72 65 73 75   of aliased resu
7f860 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a  lt set columns *
7f870 2f 0d 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73  /..  int *aAlias
7f880 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ;         /* Reg
7f890 69 73 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f  ister used to ho
7f8a0 6c 64 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c  ld aliased resul
7f8b0 74 20 2a 2f 0d 0a 20 20 75 38 20 65 78 70 6c 61  t */..  u8 expla
7f8c0 69 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  in;          /* 
7f8d0 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 4c  True if the EXPL
7f8e0 41 49 4e 20 66 6c 61 67 20 69 73 20 66 6f 75 6e  AIN flag is foun
7f8f0 64 20 6f 6e 20 74 68 65 20 71 75 65 72 79 20 2a  d on the query *
7f900 2f 0d 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65  /..  Token sName
7f910 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b  Token;    /* Tok
7f920 65 6e 20 77 69 74 68 20 75 6e 71 75 61 6c 69 66  en with unqualif
7f930 69 65 64 20 73 63 68 65 6d 61 20 6f 62 6a 65 63  ied schema objec
7f940 74 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 54 6f 6b  t name */..  Tok
7f950 65 6e 20 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 20  en sLastToken;  
7f960 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 74 6f    /* The last to
7f970 6b 65 6e 20 70 61 72 73 65 64 20 2a 2f 0d 0a 20  ken parsed */.. 
7f980 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
7f990 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c  il;   /* All SQL
7f9a0 20 74 65 78 74 20 70 61 73 74 20 74 68 65 20 6c   text past the l
7f9b0 61 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 70 61  ast semicolon pa
7f9c0 72 73 65 64 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  rsed */..  Table
7f9d0 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20 20 20 20   *pNewTable;    
7f9e0 2f 2a 20 41 20 74 61 62 6c 65 20 62 65 69 6e 67  /* A table being
7f9f0 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20   constructed by 
7fa00 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0d  CREATE TABLE */.
7fa10 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65 77  .  Trigger *pNew
7fa20 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f 2a 20  Trigger;     /* 
7fa30 54 72 69 67 67 65 72 20 75 6e 64 65 72 20 63 6f  Trigger under co
7fa40 6e 73 74 72 75 63 74 20 62 79 20 61 20 43 52 45  nstruct by a CRE
7fa50 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0d 0a  ATE TRIGGER */..
7fa60 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
7fa70 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 54  uthContext; /* T
7fa80 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72  he 6th parameter
7fa90 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 61   to db->xAuth ca
7faa0 6c 6c 62 61 63 6b 73 20 2a 2f 0d 0a 23 69 66 6e  llbacks */..#ifn
7fab0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7fac0 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20  VIRTUALTABLE..  
7fad0 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20  Token sArg;     
7fae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
7faf0 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61  mplete text of a
7fb00 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74   module argument
7fb10 20 2a 2f 0d 0a 20 20 75 38 20 64 65 63 6c 61 72   */..  u8 declar
7fb20 65 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20  eVtab;          
7fb30 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73    /* True if ins
7fb40 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ide sqlite3_decl
7fb50 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0d 0a 20  are_vtab() */.. 
7fb60 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20   int nVtabLock; 
7fb70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
7fb80 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61 6c  umber of virtual
7fb90 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20   tables to lock 
7fba0 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70  */..  Table **ap
7fbb0 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20  VtabLock;       
7fbc0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
7fbd0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65  irtual tables ne
7fbe0 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f  eding locking */
7fbf0 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 6e 74 20  ..#endif..  int 
7fc00 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
7fc10 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
7fc20 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66  n tree height of
7fc30 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c   current sub-sel
7fc40 65 63 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20  ect */..  Table 
7fc50 2a 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20  *pZombieTab;    
7fc60 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62    /* List of Tab
7fc70 6c 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65  le objects to de
7fc80 6c 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20  lete after code 
7fc90 67 65 6e 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65  gen */..  Trigge
7fca0 72 50 72 67 20 2a 70 54 72 69 67 67 65 72 50 72  rPrg *pTriggerPr
7fcb0 67 3b 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20  g;    /* Linked 
7fcc0 6c 69 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72  list of coded tr
7fcd0 69 67 67 65 72 73 20 2a 2f 0d 0a 0d 0a 23 69 66  iggers */....#if
7fce0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
7fcf0 5f 45 58 50 4c 41 49 4e 0d 0a 20 20 69 6e 74 20  _EXPLAIN..  int 
7fd00 69 53 65 6c 65 63 74 49 64 3b 0d 0a 20 20 69 6e  iSelectId;..  in
7fd10 74 20 69 4e 65 78 74 53 65 6c 65 63 74 49 64 3b  t iNextSelectId;
7fd20 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a  ..#endif..};....
7fd30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
7fd40 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d  IT_VIRTUALTABLE.
7fd50 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45  .  #define IN_DE
7fd60 43 4c 41 52 45 5f 56 54 41 42 20 30 0d 0a 23 65  CLARE_VTAB 0..#e
7fd70 6c 73 65 0d 0a 20 20 23 64 65 66 69 6e 65 20 49  lse..  #define I
7fd80 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28  N_DECLARE_VTAB (
7fd90 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56  pParse->declareV
7fda0 74 61 62 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  tab)..#endif....
7fdb0 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  /*..** An instan
7fdc0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
7fdd0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61  ing structure ca
7fde0 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e  n be declared on
7fdf0 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65   a stack and use
7fe00 64 0d 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68  d..** to save th
7fe10 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e  e Parse.zAuthCon
7fe20 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68  text value so th
7fe30 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73  at it can be res
7fe40 74 6f 72 65 64 20 6c 61 74 65 72 2e 0d 0a 2a 2f  tored later...*/
7fe50 0d 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e  ..struct AuthCon
7fe60 74 65 78 74 20 7b 0d 0a 20 20 63 6f 6e 73 74 20  text {..  const 
7fe70 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65  char *zAuthConte
7fe80 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76  xt;   /* Put sav
7fe90 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f  ed Parse.zAuthCo
7fea0 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0d 0a 20  ntext here */.. 
7feb0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
7fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7fed0 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  The Parse struct
7fee0 75 72 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  ure */..};..../*
7fef0 0d 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20 66 6c  ..** Bitfield fl
7ff00 61 67 73 20 66 6f 72 20 50 35 20 76 61 6c 75 65  ags for P5 value
7ff10 20 69 6e 20 4f 50 5f 49 6e 73 65 72 74 20 61 6e   in OP_Insert an
7ff20 64 20 4f 50 5f 44 65 6c 65 74 65 0d 0a 2a 2f 0d  d OP_Delete..*/.
7ff30 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
7ff40 4e 43 48 41 4e 47 45 20 20 20 20 20 20 20 30 78  NCHANGE       0x
7ff50 30 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20  01    /* Set to 
7ff60 75 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61 6e  update db->nChan
7ff70 67 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f  ge */..#define O
7ff80 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
7ff90 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 53      0x02    /* S
7ffa0 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d  et to update db-
7ffb0 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f 0d 0a 23  >lastRowid */..#
7ffc0 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53  define OPFLAG_IS
7ffd0 55 50 44 41 54 45 20 20 20 20 20 20 30 78 30 34  UPDATE      0x04
7ffe0 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 5f 49      /* This OP_I
7fff0 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71 6c 20  nsert is an sql 
80000 55 50 44 41 54 45 20 2a 2f 0d 0a 23 64 65 66 69  UPDATE */..#defi
80010 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  ne OPFLAG_APPEND
80020 20 20 20 20 20 20 20 20 30 78 30 38 20 20 20 20          0x08    
80030 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 6c  /* This is likel
80040 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
80050 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f 50  d */..#define OP
80060 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55  FLAG_USESEEKRESU
80070 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 72  LT 0x10    /* Tr
80080 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 65  y to avoid a see
80090 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 74  k in BtreeInsert
800a0 28 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4f  () */..#define O
800b0 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45  PFLAG_CLEARCACHE
800c0 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43      0x20    /* C
800d0 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c  lear pseudo-tabl
800e0 65 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f  e cache in OP_Co
800f0 6c 75 6d 6e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 20  lumn */..../*.. 
80100 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 20 70  * Each trigger p
80110 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61  resent in the da
80120 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
80130 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e   stored as an in
80140 73 74 61 6e 63 65 20 6f 66 0d 0a 20 2a 20 73 74  stance of.. * st
80150 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0d 0a  ruct Trigger. ..
80160 20 2a 0d 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20   *.. * Pointers 
80170 74 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  to instances of 
80180 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61  struct Trigger a
80190 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f  re stored in two
801a0 20 77 61 79 73 2e 0d 0a 20 2a 20 31 2e 20 49 6e   ways... * 1. In
801b0 20 74 68 65 20 22 74 72 69 67 48 61 73 68 22 20   the "trigHash" 
801c0 68 61 73 68 20 74 61 62 6c 65 20 28 70 61 72 74  hash table (part
801d0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a   of the sqlite3*
801e0 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73   that represents
801f0 20 74 68 65 20 0d 0a 20 2a 20 20 20 20 64 61 74   the .. *    dat
80200 61 62 61 73 65 29 2e 20 54 68 69 73 20 61 6c 6c  abase). This all
80210 6f 77 73 20 54 72 69 67 67 65 72 20 73 74 72 75  ows Trigger stru
80220 63 74 75 72 65 73 20 74 6f 20 62 65 20 72 65 74  ctures to be ret
80230 72 69 65 76 65 64 20 62 79 20 6e 61 6d 65 2e 0d  rieved by name..
80240 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 72 69 67 67  . * 2. All trigg
80250 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ers associated w
80260 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 61 62  ith a single tab
80270 6c 65 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64  le form a linked
80280 20 6c 69 73 74 2c 20 75 73 69 6e 67 20 74 68 65   list, using the
80290 0d 0a 20 2a 20 20 20 20 70 4e 65 78 74 20 6d 65  .. *    pNext me
802a0 6d 62 65 72 20 6f 66 20 73 74 72 75 63 74 20 54  mber of struct T
802b0 72 69 67 67 65 72 2e 20 41 20 70 6f 69 6e 74 65  rigger. A pointe
802c0 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
802d0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0d 0a 20  lement of the.. 
802e0 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c 69 73 74  *    linked list
802f0 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74 68   is stored as th
80300 65 20 22 70 54 72 69 67 67 65 72 22 20 6d 65 6d  e "pTrigger" mem
80310 62 65 72 20 6f 66 20 74 68 65 20 61 73 73 6f 63  ber of the assoc
80320 69 61 74 65 64 0d 0a 20 2a 20 20 20 20 73 74 72  iated.. *    str
80330 75 63 74 20 54 61 62 6c 65 2e 0d 0a 20 2a 0d 0a  uct Table... *..
80340 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 73   * The "step_lis
80350 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73  t" member points
80360 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c   to the first el
80370 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65  ement of a linke
80380 64 20 6c 69 73 74 0d 0a 20 2a 20 63 6f 6e 74 61  d list.. * conta
80390 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74  ining the SQL st
803a0 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69  atements specifi
803b0 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65  ed as the trigge
803c0 72 20 70 72 6f 67 72 61 6d 2e 0d 0a 20 2a 2f 0d  r program... */.
803d0 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20  .struct Trigger 
803e0 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  {..  char *zName
803f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
80400 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
80410 74 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20  trigger         
80420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
80430 2f 0d 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65  /..  char *table
80440 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
80450 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  The table or vie
80460 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 74  w to which the t
80470 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a  rigger applies *
80480 2f 0d 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20  /..  u8 op;     
80490 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
804a0 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45  One of TK_DELETE
804b0 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  , TK_UPDATE, TK_
804c0 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 2a  INSERT         *
804d0 2f 0d 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20  /..  u8 tr_tm;  
804e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
804f0 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42  One of TRIGGER_B
80500 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41  EFORE, TRIGGER_A
80510 46 54 45 52 20 2a 2f 0d 0a 20 20 45 78 70 72 20  FTER */..  Expr 
80520 2a 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20  *pWhen;         
80530 20 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63     /* The WHEN c
80540 6c 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70  lause of the exp
80550 72 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20  ression (may be 
80560 4e 55 4c 4c 29 20 2a 2f 0d 0a 20 20 49 64 4c 69  NULL) */..  IdLi
80570 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20  st *pColumns;   
80580 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
80590 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c  s an UPDATE OF <
805a0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69  column-list> tri
805b0 67 67 65 72 2c 0d 0a 20 20 20 20 20 20 20 20 20  gger,..         
805c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
805d0 20 20 20 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d      the <column-
805e0 6c 69 73 74 3e 20 69 73 20 73 74 6f 72 65 64 20  list> is stored 
805f0 68 65 72 65 20 2a 2f 0d 0a 20 20 53 63 68 65 6d  here */..  Schem
80600 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
80610 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e     /* Schema con
80620 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67  taining the trig
80630 67 65 72 20 2a 2f 0d 0a 20 20 53 63 68 65 6d 61  ger */..  Schema
80640 20 2a 70 54 61 62 53 63 68 65 6d 61 3b 20 20 20   *pTabSchema;   
80650 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74    /* Schema cont
80660 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
80670 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 53 74   */..  TriggerSt
80680 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b 20 2f  ep *step_list; /
80690 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 20 74  * Link list of t
806a0 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73  rigger program s
806b0 74 65 70 73 20 20 20 20 20 20 20 20 20 20 20 20  teps            
806c0 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 20 2a   */..  Trigger *
806d0 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f  pNext;         /
806e0 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 61  * Next trigger a
806f0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
80700 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 7d 3b 0d  he table */..};.
80710 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 74 72 69 67  .../*..** A trig
80720 67 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20  ger is either a 
80730 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54  BEFORE or an AFT
80740 45 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65  ER trigger.  The
80750 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
80760 61 6e 74 73 0d 0a 2a 2a 20 64 65 74 65 72 6d 69  ants..** determi
80770 6e 65 20 77 68 69 63 68 2e 20 0d 0a 2a 2a 0d 0a  ne which. ..**..
80780 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
80790 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 72  multiple trigger
807a0 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 20  s, you might of 
807b0 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 20  some BEFORE and 
807c0 73 6f 6d 65 20 41 46 54 45 52 2e 0d 0a 2a 2a 20  some AFTER...** 
807d0 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 74  In that cases, t
807e0 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c  he constants bel
807f0 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74  ow can be ORed t
80800 6f 67 65 74 68 65 72 2e 0d 0a 2a 2f 0d 0a 23 64  ogether...*/..#d
80810 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45  efine TRIGGER_BE
80820 46 4f 52 45 20 20 31 0d 0a 23 64 65 66 69 6e 65  FORE  1..#define
80830 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 20   TRIGGER_AFTER  
80840 20 32 0d 0a 0d 0a 2f 2a 0d 0a 20 2a 20 41 6e 20   2..../*.. * An 
80850 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75  instance of stru
80860 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 69  ct TriggerStep i
80870 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
80880 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61  a single SQL sta
80890 74 65 6d 65 6e 74 0d 0a 20 2a 20 74 68 61 74 20  tement.. * that 
808a0 69 73 20 61 20 70 61 72 74 20 6f 66 20 61 20 74  is a part of a t
808b0 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 20  rigger-program. 
808c0 0d 0a 20 2a 0d 0a 20 2a 20 49 6e 73 74 61 6e 63  .. *.. * Instanc
808d0 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69  es of struct Tri
808e0 67 67 65 72 53 74 65 70 20 61 72 65 20 73 74 6f  ggerStep are sto
808f0 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20  red in a singly 
80900 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e  linked list (lin
80910 6b 65 64 0d 0a 20 2a 20 75 73 69 6e 67 20 74 68  ked.. * using th
80920 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72  e "pNext" member
80930 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  ) referenced by 
80940 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20  the "step_list" 
80950 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0d 0a  member of the ..
80960 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74   * associated st
80970 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73  ruct Trigger ins
80980 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74  tance. The first
80990 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
809a0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0d 0a  linked list is..
809b0 20 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65   * the first ste
809c0 70 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  p of the trigger
809d0 2d 70 72 6f 67 72 61 6d 2e 0d 0a 20 2a 20 0d 0a  -program... * ..
809e0 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62   * The "op" memb
809f0 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 68 65  er indicates whe
80a00 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 22  ther this is a "
80a10 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54  DELETE", "INSERT
80a20 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 0d 0a  ", "UPDATE" or..
80a30 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 74   * "SELECT" stat
80a40 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69  ement. The meani
80a50 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 72  ngs of the other
80a60 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 65   members is dete
80a70 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0d 0a  rmined by the ..
80a80 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22   * value of "op"
80a90 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 20 2a   as follows:.. *
80aa0 0d 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49  .. * (op == TK_I
80ab0 4e 53 45 52 54 29 0d 0a 20 2a 20 6f 72 63 6f 6e  NSERT).. * orcon
80ac0 66 20 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74  f    -> stores t
80ad0 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61  he ON CONFLICT a
80ae0 6c 67 6f 72 69 74 68 6d 0d 0a 20 2a 20 70 53 65  lgorithm.. * pSe
80af0 6c 65 63 74 20 20 20 2d 3e 20 49 66 20 74 68 69  lect   -> If thi
80b00 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49  s is an INSERT I
80b10 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e  NTO ... SELECT .
80b20 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  .. statement, th
80b30 65 6e 0d 0a 20 2a 20 20 20 20 20 20 20 20 20 20  en.. *          
80b40 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20      this stores 
80b50 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
80b60 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
80b70 74 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  t. Otherwise NUL
80b80 4c 2e 0d 0a 20 2a 20 74 61 72 67 65 74 20 20 20  L... * target   
80b90 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64   -> A token hold
80ba0 69 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20 6e  ing the quoted n
80bb0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
80bc0 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e   to insert into.
80bd0 0d 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d  .. * pExprList -
80be0 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  > If this is an 
80bf0 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20  INSERT INTO ... 
80c00 56 41 4c 55 45 53 20 2e 2e 2e 20 73 74 61 74 65  VALUES ... state
80c10 6d 65 6e 74 2c 20 74 68 65 6e 0d 0a 20 2a 20 20  ment, then.. *  
80c20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69 73              this
80c30 20 73 74 6f 72 65 73 20 76 61 6c 75 65 73 20 74   stores values t
80c40 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 4f  o be inserted. O
80c50 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0d 0a  therwise NULL...
80c60 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d 3e 20   * pIdList   -> 
80c70 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e  If this is an IN
80c80 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c  SERT INTO ... (<
80c90 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56  column-names>) V
80ca0 41 4c 55 45 53 20 2e 2e 2e 20 0d 0a 20 2a 20 20  ALUES ... .. *  
80cb0 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
80cc0 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73  ement, then this
80cd0 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c 75   stores the colu
80ce0 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0d 0a  mn-names to be..
80cf0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
80d00 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0d 0a  inserted into...
80d10 20 2a 0d 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b   *.. * (op == TK
80d20 5f 44 45 4c 45 54 45 29 0d 0a 20 2a 20 74 61 72  _DELETE).. * tar
80d30 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65  get    -> A toke
80d40 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75  n holding the qu
80d50 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  oted name of the
80d60 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65   table to delete
80d70 20 66 72 6f 6d 2e 0d 0a 20 2a 20 70 57 68 65 72   from... * pWher
80d80 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52  e    -> The WHER
80d90 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
80da0 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
80db0 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69   if one is speci
80dc0 66 69 65 64 2e 0d 0a 20 2a 20 20 20 20 20 20 20  fied... *       
80dd0 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65         Otherwise
80de0 20 4e 55 4c 4c 2e 0d 0a 20 2a 20 0d 0a 20 2a 20   NULL... * .. * 
80df0 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45  (op == TK_UPDATE
80e00 29 0d 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20  ).. * target    
80e10 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69  -> A token holdi
80e20 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61  ng the quoted na
80e30 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
80e40 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f  to update rows o
80e50 66 2e 0d 0a 20 2a 20 70 57 68 65 72 65 20 20 20  f... * pWhere   
80e60 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
80e70 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
80e80 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
80e90 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
80ea0 2e 0d 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
80eb0 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c     Otherwise NUL
80ec0 4c 2e 0d 0a 20 2a 20 70 45 78 70 72 4c 69 73 74  L... * pExprList
80ed0 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68   -> A list of th
80ee0 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64  e columns to upd
80ef0 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72  ate and the expr
80f00 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74  essions to updat
80f10 65 0d 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  e.. *           
80f20 20 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20     them to. See 
80f30 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20  sqlite3Update() 
80f40 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  documentation of
80f50 20 22 70 43 68 61 6e 67 65 73 22 0d 0a 20 2a 20   "pChanges".. * 
80f60 20 20 20 20 20 20 20 20 20 20 20 20 20 61 72 67               arg
80f70 75 6d 65 6e 74 2e 0d 0a 20 2a 20 0d 0a 20 2a 2f  ument... * .. */
80f80 0d 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  ..struct Trigger
80f90 53 74 65 70 20 7b 0d 0a 20 20 75 38 20 6f 70 3b  Step {..  u8 op;
80fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
80fb0 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * One of TK_DELE
80fc0 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54  TE, TK_UPDATE, T
80fd0 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c  K_INSERT, TK_SEL
80fe0 45 43 54 20 2a 2f 0d 0a 20 20 75 38 20 6f 72 63  ECT */..  u8 orc
80ff0 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f  onf;           /
81000 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74  * OE_Rollback et
81010 63 2e 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72  c. */..  Trigger
81020 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a   *pTrig;      /*
81030 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 61   The trigger tha
81040 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 61  t this step is a
81050 20 70 61 72 74 20 6f 66 20 2a 2f 0d 0a 20 20 53   part of */..  S
81060 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
81070 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74      /* SELECT st
81080 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 6f 66  atment or RHS of
81090 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20   INSERT INTO .. 
810a0 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0d 0a 20  SELECT ... */.. 
810b0 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20   Token target;  
810c0 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 20        /* Target 
810d0 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 45  table for DELETE
810e0 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54  , UPDATE, INSERT
810f0 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 57 68   */..  Expr *pWh
81100 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ere;        /* T
81110 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
81120 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50  for DELETE or UP
81130 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0d 0a 20  DATE steps */.. 
81140 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72   ExprList *pExpr
81150 4c 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61  List; /* SET cla
81160 75 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20  use for UPDATE. 
81170 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66   VALUES clause f
81180 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0d 0a 20 20  or INSERT */..  
81190 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b  IdList *pIdList;
811a0 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
811b0 61 6d 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20  ames for INSERT 
811c0 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 53 74 65  */..  TriggerSte
811d0 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65  p *pNext;  /* Ne
811e0 78 74 20 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c  xt in the link-l
811f0 69 73 74 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65  ist */..  Trigge
81200 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 20 2f  rStep *pLast;  /
81210 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69  * Last element i
81220 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c  n link-list. Val
81230 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 6d 20  id for 1st elem 
81240 6f 6e 6c 79 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f  only */..};..../
81250 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
81260 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f  ing structure co
81270 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69  ntains informati
81280 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 73  on used by the s
81290 71 6c 69 74 65 46 69 78 2e 2e 2e 0d 0a 2a 2a 20  qliteFix.....** 
812a0 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 79  routines as they
812b0 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20   walk the parse 
812c0 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 74  tree to make dat
812d0 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73  abase references
812e0 0d 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  ..** explicit.  
812f0 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74  ..*/..typedef st
81300 72 75 63 74 20 44 62 46 69 78 65 72 20 44 62 46  ruct DbFixer DbF
81310 69 78 65 72 3b 0d 0a 73 74 72 75 63 74 20 44 62  ixer;..struct Db
81320 46 69 78 65 72 20 7b 0d 0a 20 20 50 61 72 73 65  Fixer {..  Parse
81330 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 2f   *pParse;      /
81340 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
81350 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d 65  ntext.  Error me
81360 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 68  ssages written h
81370 65 72 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ere */..  const 
81380 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a  char *zDb;    /*
81390 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f   Make sure all o
813a0 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61  bjects are conta
813b0 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 74  ined in this dat
813c0 61 62 61 73 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73  abase */..  cons
813d0 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20  t char *zType;  
813e0 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63  /* Type of the c
813f0 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20  ontainer - used 
81400 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
81410 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 54  es */..  const T
81420 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20  oken *pName; /* 
81430 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74  Name of the cont
81440 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72  ainer - used for
81450 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
81460 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
81470 20 41 6e 20 6f 62 6a 65 63 74 65 64 20 75 73 65   An objected use
81480 64 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20  d to accumulate 
81490 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 74  the text of a st
814a0 72 69 6e 67 20 77 68 65 72 65 20 77 65 0d 0a 2a  ring where we..*
814b0 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61  * do not necessa
814c0 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 20 62 69  rily know how bi
814d0 67 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c  g the string wil
814e0 6c 20 62 65 20 69 6e 20 74 68 65 20 65 6e 64 2e  l be in the end.
814f0 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 53 74 72  ..*/..struct Str
81500 41 63 63 75 6d 20 7b 0d 0a 20 20 73 71 6c 69 74  Accum {..  sqlit
81510 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
81520 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61  /* Optional data
81530 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69  base for lookasi
81540 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c  de.  Can be NULL
81550 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 42 61   */..  char *zBa
81560 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  se;         /* A
81570 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   base allocation
81580 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c  .  Not from mall
81590 6f 63 2e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  oc. */..  char *
815a0 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f  zText;         /
815b0 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c  * The string col
815c0 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f  lected so far */
815d0 0d 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20  ..  int  nChar; 
815e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
815f0 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  th of the string
81600 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20 20 69 6e   so far */..  in
81610 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20  t  nAlloc;      
81620 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20     /* Amount of 
81630 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20  space allocated 
81640 69 6e 20 7a 54 65 78 74 20 2a 2f 0d 0a 20 20 69  in zText */..  i
81650 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20  nt  mxAlloc;    
81660 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
81670 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c 65  llowed string le
81680 6e 67 74 68 20 2a 2f 0d 0a 20 20 75 38 20 20 20  ngth */..  u8   
81690 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20  mallocFailed;   
816a0 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20  /* Becomes true 
816b0 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c  if any memory al
816c0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a  location fails *
816d0 2f 0d 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c  /..  u8   useMal
816e0 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 30 3a 20  loc;      /* 0: 
816f0 6e 6f 6e 65 2c 20 20 31 3a 20 73 71 6c 69 74 65  none,  1: sqlite
81700 33 44 62 4d 61 6c 6c 6f 63 2c 20 20 32 3a 20 73  3DbMalloc,  2: s
81710 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f  qlite3_malloc */
81720 0d 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 67 3b  ..  u8   tooBig;
81730 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63 6f           /* Beco
81740 6d 65 73 20 74 72 75 65 20 69 66 20 73 74 72 69  mes true if stri
81750 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64 73 20  ng size exceeds 
81760 6c 69 6d 69 74 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  limits */..};...
81770 0a 2f 2a 0d 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  ./*..** A pointe
81780 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
81790 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ure is used to c
817a0 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72  ommunicate infor
817b0 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 66 72 6f 6d 20  mation..** from 
817c0 73 71 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20  sqlite3Init and 
817d0 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69  OP_ParseSchema i
817e0 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49  nto the sqlite3I
817f0 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0d 0a 2a 2f  nitCallback...*/
81800 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
81810 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   {..  sqlite3 *d
81820 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
81830 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
81840 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0d 0a  initialized */..
81850 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
81860 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d        /* 0 for m
81870 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31  ain database.  1
81880 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66   for TEMP, 2.. f
81890 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0d 0a  or ATTACHed */..
818a0 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73    char **pzErrMs
818b0 67 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d  g;    /* Error m
818c0 65 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65  essage stored he
818d0 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b  re */..  int rc;
818e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
818f0 52 65 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72  Result code stor
81900 65 64 20 68 65 72 65 20 2a 2f 0d 0a 7d 20 49 6e  ed here */..} In
81910 69 74 44 61 74 61 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  itData;..../*..*
81920 2a 20 53 74 72 75 63 74 75 72 65 20 63 6f 6e 74  * Structure cont
81930 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c 20 63 6f  aining global co
81940 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 61 74 61  nfiguration data
81950 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20   for the SQLite 
81960 6c 69 62 72 61 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a  library...**..**
81970 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
81980 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 6f  also contains so
81990 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  me state informa
819a0 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  tion...*/..struc
819b0 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
819c0 7b 0d 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61  {..  int bMemsta
819d0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
819e0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
819f0 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20  o enable memory 
81a00 73 74 61 74 75 73 20 2a 2f 0d 0a 20 20 69 6e 74  status */..  int
81a10 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20   bCoreMutex;    
81a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
81a30 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65  * True to enable
81a40 20 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a   core mutexing *
81a50 2f 0d 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75  /..  int bFullMu
81a60 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
81a70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
81a80 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75  o enable full mu
81a90 74 65 78 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74  texing */..  int
81aa0 20 62 4f 70 65 6e 55 72 69 3b 20 20 20 20 20 20   bOpenUri;      
81ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
81ac0 2a 20 54 72 75 65 20 74 6f 20 69 6e 74 65 72 70  * True to interp
81ad0 72 65 74 20 66 69 6c 65 6e 61 6d 65 73 20 61 73  ret filenames as
81ae0 20 55 52 49 73 20 2a 2f 0d 0a 20 20 69 6e 74 20   URIs */..  int 
81af0 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20  mxStrlen;       
81b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
81b10 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20   Maximum string 
81b20 6c 65 6e 67 74 68 20 2a 2f 0d 0a 20 20 69 6e 74  length */..  int
81b30 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20   szLookaside;   
81b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
81b50 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73  * Default lookas
81b60 69 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 20  ide buffer size 
81b70 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61  */..  int nLooka
81b80 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  side;           
81b90 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
81ba0 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  lt lookaside buf
81bb0 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0d 0a 20 20  fer count */..  
81bc0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
81bd0 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ods m;          
81be0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
81bf0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
81c00 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0d 0a 20   interface */.. 
81c10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
81c20 65 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20  ethods mutex;   
81c30 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20     /* Low-level 
81c40 6d 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20  mutex interface 
81c50 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70 63  */..  sqlite3_pc
81c60 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 70 63  ache_methods2 pc
81c70 61 63 68 65 32 3b 20 20 2f 2a 20 4c 6f 77 2d 6c  ache2;  /* Low-l
81c80 65 76 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20  evel page-cache 
81c90 69 6e 74 65 72 66 61 63 65 20 2a 2f 0d 0a 20 20  interface */..  
81ca0 76 6f 69 64 20 2a 70 48 65 61 70 3b 20 20 20 20  void *pHeap;    
81cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81cc0 20 20 2f 2a 20 48 65 61 70 20 73 74 6f 72 61 67    /* Heap storag
81cd0 65 20 73 70 61 63 65 20 2a 2f 0d 0a 20 20 69 6e  e space */..  in
81ce0 74 20 6e 48 65 61 70 3b 20 20 20 20 20 20 20 20  t nHeap;        
81cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81d00 2f 2a 20 53 69 7a 65 20 6f 66 20 70 48 65 61 70  /* Size of pHeap
81d10 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 6e 52  [] */..  int mnR
81d20 65 71 2c 20 6d 78 52 65 71 3b 20 20 20 20 20 20  eq, mxReq;      
81d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69             /* Mi
81d40 6e 20 61 6e 64 20 6d 61 78 20 68 65 61 70 20 72  n and max heap r
81d50 65 71 75 65 73 74 73 20 73 69 7a 65 73 20 2a 2f  equests sizes */
81d60 0d 0a 20 20 76 6f 69 64 20 2a 70 53 63 72 61 74  ..  void *pScrat
81d70 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
81d80 20 20 20 20 20 20 2f 2a 20 53 63 72 61 74 63 68        /* Scratch
81d90 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 69 6e   memory */..  in
81da0 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20  t szScratch;    
81db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81dc0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
81dd0 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 2a  scratch buffer *
81de0 2f 0d 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 63  /..  int nScratc
81df0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
81e00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
81e10 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 66 66   of scratch buff
81e20 65 72 73 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a  ers */..  void *
81e30 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  pPage;          
81e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
81e50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79  age cache memory
81e60 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 50 61 67   */..  int szPag
81e70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
81e80 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
81e90 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e   of each page in
81ea0 20 70 50 61 67 65 5b 5d 20 2a 2f 0d 0a 20 20 69   pPage[] */..  i
81eb0 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  nt nPage;       
81ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81ed0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
81ee0 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  ges in pPage[] *
81ef0 2f 0d 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65  /..  int mxParse
81f00 72 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20  rStack;         
81f10 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75         /* maximu
81f20 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70  m depth of the p
81f30 61 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0d 0a  arser stack */..
81f40 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 63 68    int sharedCach
81f50 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20  eEnabled;       
81f60 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73      /* true if s
81f70 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65  hared-cache mode
81f80 20 65 6e 61 62 6c 65 64 20 2a 2f 0d 0a 20 20 2f   enabled */..  /
81f90 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 68  * The above migh
81fa0 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  t be initialized
81fb0 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54   to non-zero.  T
81fc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65  he following nee
81fd0 64 20 74 6f 20 61 6c 77 61 79 73 0d 0a 20 20 2a  d to always..  *
81fe0 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a  * initially be z
81ff0 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f  ero, however. */
82000 0d 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  ..  int isInit; 
82010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82020 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
82030 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
82040 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  on has finished 
82050 2a 2f 0d 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67  */..  int inProg
82060 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20  ress;           
82070 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
82080 77 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61  while initializa
82090 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
820a0 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 4d 75 74   */..  int isMut
820b0 65 78 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  exInit;         
820c0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
820d0 20 61 66 74 65 72 20 6d 75 74 65 78 65 73 20 61   after mutexes a
820e0 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  re initialized *
820f0 2f 0d 0a 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f  /..  int isMallo
82100 63 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20  cInit;          
82110 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
82120 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69  fter malloc is i
82130 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0d 0a 20  nitialized */.. 
82140 20 69 6e 74 20 69 73 50 43 61 63 68 65 49 6e 69   int isPCacheIni
82150 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
82160 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72     /* True after
82170 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 69   malloc is initi
82180 61 6c 69 7a 65 64 20 2a 2f 0d 0a 20 20 73 71 6c  alized */..  sql
82190 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 49 6e 69  ite3_mutex *pIni
821a0 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 2f  tMutex;        /
821b0 2a 20 4d 75 74 65 78 20 75 73 65 64 20 62 79 20  * Mutex used by 
821c0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
821d0 7a 65 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ze() */..  int n
821e0 52 65 66 49 6e 69 74 4d 75 74 65 78 3b 20 20 20  RefInitMutex;   
821f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
82200 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20  Number of users 
82210 6f 66 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f  of pInitMutex */
82220 0d 0a 20 20 76 6f 69 64 20 28 2a 78 4c 6f 67 29  ..  void (*xLog)
82230 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
82240 20 63 68 61 72 2a 29 3b 20 2f 2a 20 46 75 6e 63   char*); /* Func
82250 74 69 6f 6e 20 66 6f 72 20 6c 6f 67 67 69 6e 67  tion for logging
82260 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 4c 6f   */..  void *pLo
82270 67 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  gArg;           
82280 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
82290 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
822a0 20 78 4c 6f 67 28 29 20 2a 2f 0d 0a 20 20 69 6e   xLog() */..  in
822b0 74 20 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c  t bLocaltimeFaul
822c0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
822d0 2f 2a 20 54 72 75 65 20 74 6f 20 66 61 69 6c 20  /* True to fail 
822e0 6c 6f 63 61 6c 74 69 6d 65 28 29 20 63 61 6c 6c  localtime() call
822f0 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  s */..};..../*..
82300 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  ** Context point
82310 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74  er passed down t
82320 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d  hrough the tree-
82330 77 61 6c 6b 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  walk...*/..struc
82340 74 20 57 61 6c 6b 65 72 20 7b 0d 0a 20 20 69 6e  t Walker {..  in
82350 74 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63  t (*xExprCallbac
82360 6b 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72  k)(Walker*, Expr
82370 2a 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62  *);     /* Callb
82380 61 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69  ack for expressi
82390 6f 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a  ons */..  int (*
823a0 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29  xSelectCallback)
823b0 28 57 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a  (Walker*,Select*
823c0 29 3b 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20  );  /* Callback 
823d0 66 6f 72 20 53 45 4c 45 43 54 73 20 2a 2f 0d 0a  for SELECTs */..
823e0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
823f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82400 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
82410 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20  arser context.  
82420 2a 2f 0d 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20  */..  union {   
82430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82450 2f 2a 20 45 78 74 72 61 20 64 61 74 61 20 66 6f  /* Extra data fo
82460 72 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 20  r callback */.. 
82470 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a     NameContext *
82480 70 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  pNC;            
82490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
824a0 20 4e 61 6d 69 6e 67 20 63 6f 6e 74 65 78 74 20   Naming context 
824b0 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 3b 20 20  */..    int i;  
824c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
824d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
824e0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
824f0 6c 75 65 20 2a 2f 0d 0a 20 20 7d 20 75 3b 0d 0a  lue */..  } u;..
82500 7d 3b 0d 0a 0d 0a 2f 2a 20 46 6f 72 77 61 72 64  };..../* Forward
82510 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   declarations */
82520 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
82530 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
82540 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78  Expr(Walker*, Ex
82550 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  pr*);..SQLITE_PR
82560 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
82570 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61  3WalkExprList(Wa
82580 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a  lker*, ExprList*
82590 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
825a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
825b0 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a  lkSelect(Walker*
825c0 2c 20 53 65 6c 65 63 74 2a 29 3b 0d 0a 53 51 4c  , Select*);..SQL
825d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
825e0 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
825f0 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53  tExpr(Walker*, S
82600 65 6c 65 63 74 2a 29 3b 0d 0a 53 51 4c 49 54 45  elect*);..SQLITE
82610 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
82620 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72  ite3WalkSelectFr
82630 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65  om(Walker*, Sele
82640 63 74 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ct*);..../*..** 
82650 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
82660 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 20   the parse-tree 
82670 77 61 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76  walking primitiv
82680 65 73 20 61 6e 64 20 74 68 65 69 72 0d 0a 2a 2a  es and their..**
82690 20 63 61 6c 6c 62 61 63 6b 73 2e 0d 0a 2a 2f 0d   callbacks...*/.
826a0 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 43 6f 6e  .#define WRC_Con
826b0 74 69 6e 75 65 20 20 20 20 30 20 20 20 2f 2a 20  tinue    0   /* 
826c0 43 6f 6e 74 69 6e 75 65 20 64 6f 77 6e 20 69 6e  Continue down in
826d0 74 6f 20 63 68 69 6c 64 72 65 6e 20 2a 2f 0d 0a  to children */..
826e0 23 64 65 66 69 6e 65 20 57 52 43 5f 50 72 75 6e  #define WRC_Prun
826f0 65 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f  e       1   /* O
82700 6d 69 74 20 63 68 69 6c 64 72 65 6e 20 62 75 74  mit children but
82710 20 63 6f 6e 74 69 6e 75 65 20 77 61 6c 6b 69 6e   continue walkin
82720 67 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0d 0a 23  g siblings */..#
82730 64 65 66 69 6e 65 20 57 52 43 5f 41 62 6f 72 74  define WRC_Abort
82740 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 41 62         2   /* Ab
82750 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77  andon the tree w
82760 61 6c 6b 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  alk */..../*..**
82770 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f   Assuming zIn po
82780 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
82790 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d  t byte of a UTF-
827a0 38 20 63 68 61 72 61 63 74 65 72 2c 0d 0a 2a 2a  8 character,..**
827b0 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20   advance zIn to 
827c0 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72  point to the fir
827d0 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e  st byte of the n
827e0 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63  ext UTF-8 charac
827f0 74 65 72 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  ter...*/..#defin
82800 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54  e SQLITE_SKIP_UT
82810 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20  F8(zIn) {       
82820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82830 20 5c 0d 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e   \..  if( (*(zIn
82840 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20  ++))>=0xc0 ){   
82850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82860 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
82870 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26    while( (*zIn &
82880 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20   0xc0)==0x80 ){ 
82890 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20  zIn++; }        
828a0 20 20 20 20 20 5c 0d 0a 20 20 7d 20 20 20 20 20       \..  }     
828b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
828c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
828d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
828e0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
828f0 65 20 53 51 4c 49 54 45 5f 2a 5f 42 4b 50 54 20  e SQLITE_*_BKPT 
82900 6d 61 63 72 6f 73 20 61 72 65 20 73 75 62 73 74  macros are subst
82910 69 74 75 74 65 73 20 66 6f 72 20 74 68 65 20 65  itutes for the e
82920 72 72 6f 72 20 63 6f 64 65 73 20 77 69 74 68 0d  rror codes with.
82930 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  .** the same nam
82940 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74 68  e but without th
82950 65 20 5f 42 4b 50 54 20 73 75 66 66 69 78 2e 20  e _BKPT suffix. 
82960 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 69 6e   These macros in
82970 76 6f 6b 65 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65  voke..** routine
82980 73 20 74 68 61 74 20 72 65 70 6f 72 74 20 74 68  s that report th
82990 65 20 6c 69 6e 65 2d 6e 75 6d 62 65 72 20 6f 6e  e line-number on
829a0 20 77 68 69 63 68 20 74 68 65 20 65 72 72 6f 72   which the error
829b0 20 6f 72 69 67 69 6e 61 74 65 64 0d 0a 2a 2a 20   originated..** 
829c0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6c 6f  using sqlite3_lo
829d0 67 28 29 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  g().  The routin
829e0 65 73 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 20  es also provide 
829f0 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61  a convenient pla
82a00 63 65 0d 0a 2a 2a 20 74 6f 20 73 65 74 20 61 20  ce..** to set a 
82a10 64 65 62 75 67 67 65 72 20 62 72 65 61 6b 70 6f  debugger breakpo
82a20 69 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  int...*/..SQLITE
82a30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
82a40 69 74 65 33 43 6f 72 72 75 70 74 45 72 72 6f 72  ite3CorruptError
82a50 28 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  (int);..SQLITE_P
82a60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
82a70 65 33 4d 69 73 75 73 65 45 72 72 6f 72 28 69 6e  e3MisuseError(in
82a80 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
82a90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
82aa0 61 6e 74 6f 70 65 6e 45 72 72 6f 72 28 69 6e 74  antopenError(int
82ab0 29 3b 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  );..#define SQLI
82ac0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20  TE_CORRUPT_BKPT 
82ad0 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 45 72  sqlite3CorruptEr
82ae0 72 6f 72 28 5f 5f 4c 49 4e 45 5f 5f 29 0d 0a 23  ror(__LINE__)..#
82af0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49  define SQLITE_MI
82b00 53 55 53 45 5f 42 4b 50 54 20 73 71 6c 69 74 65  SUSE_BKPT sqlite
82b10 33 4d 69 73 75 73 65 45 72 72 6f 72 28 5f 5f 4c  3MisuseError(__L
82b20 49 4e 45 5f 5f 29 0d 0a 23 64 65 66 69 6e 65 20  INE__)..#define 
82b30 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f  SQLITE_CANTOPEN_
82b40 42 4b 50 54 20 73 71 6c 69 74 65 33 43 61 6e 74  BKPT sqlite3Cant
82b50 6f 70 65 6e 45 72 72 6f 72 28 5f 5f 4c 49 4e 45  openError(__LINE
82b60 5f 5f 29 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  __)....../*..** 
82b70 46 54 53 34 20 69 73 20 72 65 61 6c 6c 79 20 61  FTS4 is really a
82b80 6e 20 65 78 74 65 6e 73 69 6f 6e 20 66 6f 72 20  n extension for 
82b90 46 54 53 33 2e 20 20 49 74 20 69 73 20 65 6e 61  FTS3.  It is ena
82ba0 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65 0d 0a  bled using the..
82bb0 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
82bc0 5f 46 54 53 33 20 6d 61 63 72 6f 2e 20 20 42 75  _FTS3 macro.  Bu
82bd0 74 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 75  t to avoid confu
82be0 73 69 6f 6e 20 77 65 20 61 6c 73 6f 20 61 6c 6c  sion we also all
82bf0 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  ..** the SQLITE_
82c00 45 4e 41 42 4c 45 5f 46 54 53 34 20 6d 61 63 72  ENABLE_FTS4 macr
82c10 6f 20 74 6f 20 73 65 72 76 65 20 61 73 20 61 6e  o to serve as an
82c20 20 61 6c 69 73 73 65 20 66 6f 72 20 53 51 4c 49   alisse for SQLI
82c30 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 2e 0d  TE_ENABLE_FTS3..
82c40 0a 2a 2f 0d 0a 23 69 66 20 64 65 66 69 6e 65 64  .*/..#if defined
82c50 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
82c60 54 53 34 29 20 26 26 20 21 64 65 66 69 6e 65 64  TS4) && !defined
82c70 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
82c80 54 53 33 29 0d 0a 23 20 64 65 66 69 6e 65 20 53  TS3)..# define S
82c90 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
82ca0 33 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  3..#endif..../*.
82cb0 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 20  .** The ctype.h 
82cc0 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 64  header is needed
82cd0 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 73   for non-ASCII s
82ce0 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 61  ystems.  It is a
82cf0 6c 73 6f 0d 0a 2a 2a 20 6e 65 65 64 65 64 20 62  lso..** needed b
82d00 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33  y FTS3 when FTS3
82d10 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20   is included in 
82d20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
82d30 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65 66 69  ...*/..#if !defi
82d40 6e 65 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49  ned(SQLITE_ASCII
82d50 29 20 7c 7c 20 5c 0d 0a 20 20 20 20 28 64 65 66  ) || \..    (def
82d60 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
82d70 4c 45 5f 46 54 53 33 29 20 26 26 20 64 65 66 69  LE_FTS3) && defi
82d80 6e 65 64 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47  ned(SQLITE_AMALG
82d90 41 4d 41 54 49 4f 4e 29 29 0d 0a 23 20 69 6e 63  AMATION))..# inc
82da0 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0d 0a  lude <ctype.h>..
82db0 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
82dc0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
82dd0 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68 65 20  acros mimic the 
82de0 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79  standard library
82df0 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75 70 70   functions toupp
82e00 65 72 28 29 2c 0d 0a 2a 2a 20 69 73 73 70 61 63  er(),..** isspac
82e10 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c 20  e(), isalnum(), 
82e20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69 73  isdigit() and is
82e30 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65 63  xdigit(), respec
82e40 74 69 76 65 6c 79 2e 20 54 68 65 0d 0a 2a 2a 20  tively. The..** 
82e50 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20  sqlite versions 
82e60 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53  only work for AS
82e70 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20  CII characters, 
82e80 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f  regardless of lo
82e90 63 61 6c 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65  cale...*/..#ifde
82ea0 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0d 0a  f SQLITE_ASCII..
82eb0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
82ec0 54 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 29  Toupper(x)  ((x)
82ed0 26 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d  &~(sqlite3CtypeM
82ee0 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ap[(unsigned cha
82ef0 72 29 28 78 29 5d 26 30 78 32 30 29 29 0d 0a 23  r)(x)]&0x20))..#
82f00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
82f10 73 73 70 61 63 65 28 78 29 20 20 20 28 73 71 6c  sspace(x)   (sql
82f20 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
82f30 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
82f40 26 30 78 30 31 29 0d 0a 23 20 64 65 66 69 6e 65  &0x01)..# define
82f50 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28   sqlite3Isalnum(
82f60 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79  x)   (sqlite3Cty
82f70 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20  peMap[(unsigned 
82f80 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 0d  char)(x)]&0x06).
82f90 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
82fa0 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73  3Isalpha(x)   (s
82fb0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
82fc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
82fd0 29 5d 26 30 78 30 32 29 0d 0a 23 20 64 65 66 69  )]&0x02)..# defi
82fe0 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
82ff0 74 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43  t(x)   (sqlite3C
83000 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65  typeMap[(unsigne
83010 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34  d char)(x)]&0x04
83020 29 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )..# define sqli
83030 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20  te3Isxdigit(x)  
83040 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
83050 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
83060 28 78 29 5d 26 30 78 30 38 29 0d 0a 23 20 64 65  (x)]&0x08)..# de
83070 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f  fine sqlite3Tolo
83080 77 65 72 28 78 29 20 20 20 28 6f 61 73 55 70 70  wer(x)   (oasUpp
83090 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e  er2Lower[(unsign
830a0 65 64 20 63 68 61 72 29 28 78 29 5d 29 0d 0a 23  ed char)(x)])..#
830b0 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 73  else..# define s
830c0 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29  qlite3Toupper(x)
830d0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69     toupper((unsi
830e0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0d 0a  gned char)(x))..
830f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
83100 49 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73  Isspace(x)   iss
83110 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63  pace((unsigned c
83120 68 61 72 29 28 78 29 29 0d 0a 23 20 64 65 66 69  har)(x))..# defi
83130 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75  ne sqlite3Isalnu
83140 6d 28 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28  m(x)   isalnum((
83150 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
83160 29 29 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ))..# define sql
83170 69 74 65 33 49 73 61 6c 70 68 61 28 78 29 20 20  ite3Isalpha(x)  
83180 20 69 73 61 6c 70 68 61 28 28 75 6e 73 69 67 6e   isalpha((unsign
83190 65 64 20 63 68 61 72 29 28 78 29 29 0d 0a 23 20  ed char)(x))..# 
831a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
831b0 64 69 67 69 74 28 78 29 20 20 20 69 73 64 69 67  digit(x)   isdig
831c0 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  it((unsigned cha
831d0 72 29 28 78 29 29 0d 0a 23 20 64 65 66 69 6e 65  r)(x))..# define
831e0 20 73 71 6c 69 74 65 33 49 73 78 64 69 67 69 74   sqlite3Isxdigit
831f0 28 78 29 20 20 69 73 78 64 69 67 69 74 28 28 75  (x)  isxdigit((u
83200 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29  nsigned char)(x)
83210 29 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )..# define sqli
83220 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20  te3Tolower(x)   
83230 74 6f 6c 6f 77 65 72 28 28 75 6e 73 69 67 6e 65  tolower((unsigne
83240 64 20 63 68 61 72 29 28 78 29 29 0d 0a 23 65 6e  d char)(x))..#en
83250 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  dif..../*..** In
83260 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
83270 70 72 6f 74 6f 74 79 70 65 73 0d 0a 2a 2f 0d 0a  prototypes..*/..
83280 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
83290 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  nt sqlite3StrICm
832a0 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  p(const char *, 
832b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0d 0a  const char *);..
832c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
832d0 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  nt sqlite3Strlen
832e0 33 30 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  30(const char*);
832f0 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
83300 33 53 74 72 4e 49 43 6d 70 20 73 71 6c 69 74 65  3StrNICmp sqlite
83310 33 5f 73 74 72 6e 69 63 6d 70 0d 0a 0d 0a 53 51  3_strnicmp....SQ
83320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
83330 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e   sqlite3MallocIn
83340 69 74 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54  it(void);..SQLIT
83350 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
83360 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
83370 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  void);..SQLITE_P
83380 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
83390 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b  ite3Malloc(int);
833a0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
833b0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61   void *sqlite3Ma
833c0 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b 0d 0a  llocZero(int);..
833d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
833e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
833f0 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33  llocZero(sqlite3
83400 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
83410 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
83420 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
83430 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  w(sqlite3*, int)
83440 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
83450 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44  E char *sqlite3D
83460 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a  bStrDup(sqlite3*
83470 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a  ,const char*);..
83480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
83490 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74  har *sqlite3DbSt
834a0 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63  rNDup(sqlite3*,c
834b0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
834c0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
834d0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52  E void *sqlite3R
834e0 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e  ealloc(void*, in
834f0 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
83500 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
83510 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
83520 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64  (sqlite3 *, void
83530 20 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54   *, int);..SQLIT
83540 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
83550 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
83560 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64  (sqlite3 *, void
83570 20 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54   *, int);..SQLIT
83580 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
83590 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c  qlite3DbFree(sql
835a0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0d 0a  ite3*, void*);..
835b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
835c0 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  nt sqlite3Malloc
835d0 53 69 7a 65 28 76 6f 69 64 2a 29 3b 0d 0a 53 51  Size(void*);..SQ
835e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
835f0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
83600 53 69 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76  Size(sqlite3*, v
83610 6f 69 64 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  oid*);..SQLITE_P
83620 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
83630 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f  ite3ScratchMallo
83640 63 28 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  c(int);..SQLITE_
83650 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
83660 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28  ite3ScratchFree(
83670 76 6f 69 64 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  void*);..SQLITE_
83680 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
83690 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
836a0 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
836b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
836c0 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 2a  e3PageFree(void*
836d0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
836e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
836f0 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69  emSetDefault(voi
83700 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  d);..SQLITE_PRIV
83710 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
83720 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
83730 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29  s(void (*)(void)
83740 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29  , void (*)(void)
83750 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
83760 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 48 65  TE int sqlite3He
83770 61 70 4e 65 61 72 6c 79 46 75 6c 6c 28 76 6f 69  apNearlyFull(voi
83780 64 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 6e  d);..../*..** On
83790 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 6d   systems with am
837a0 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 20  ple stack space 
837b0 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 74  and that support
837c0 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 0d   alloca(), make.
837d0 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63  .** use of alloc
837e0 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70  a() to obtain sp
837f0 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75  ace for large au
83800 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e  tomatic objects.
83810 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0d 0a 2a    By default,..*
83820 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66  * obtain space f
83830 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a  rom malloc()...*
83840 2a 0d 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61  *..** The alloca
83850 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72  () routine never
83860 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20   returns NULL.  
83870 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20  This will cause 
83880 63 6f 64 65 20 70 61 74 68 73 0d 0a 2a 2a 20 74  code paths..** t
83890 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 71  hat deal with sq
838a0 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28  lite3StackAlloc(
838b0 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 65  ) failures to be
838c0 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0d 0a 2a   unreachable...*
838d0 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
838e0 5f 55 53 45 5f 41 4c 4c 4f 43 41 0d 0a 23 20 64  _USE_ALLOCA..# d
838f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61  efine sqlite3Sta
83900 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20  ckAllocRaw(D,N) 
83910 20 20 61 6c 6c 6f 63 61 28 4e 29 0d 0a 23 20 64    alloca(N)..# d
83920 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61  efine sqlite3Sta
83930 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29  ckAllocZero(D,N)
83940 20 20 6d 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28    memset(alloca(
83950 4e 29 2c 20 30 2c 20 4e 29 0d 0a 23 20 64 65 66  N), 0, N)..# def
83960 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
83970 46 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 20  Free(D,P)       
83980 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
83990 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c  e sqlite3StackAl
839a0 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 73 71  locRaw(D,N)   sq
839b0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
839c0 28 44 2c 4e 29 0d 0a 23 20 64 65 66 69 6e 65 20  (D,N)..# define 
839d0 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
839e0 63 5a 65 72 6f 28 44 2c 4e 29 20 20 73 71 6c 69  cZero(D,N)  sqli
839f0 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
83a00 44 2c 4e 29 0d 0a 23 20 64 65 66 69 6e 65 20 73  D,N)..# define s
83a10 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28  qlite3StackFree(
83a20 44 2c 50 29 20 20 20 20 20 20 20 73 71 6c 69 74  D,P)       sqlit
83a30 65 33 44 62 46 72 65 65 28 44 2c 50 29 0d 0a 23  e3DbFree(D,P)..#
83a40 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20  endif....#ifdef 
83a50 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
83a60 4d 53 59 53 33 0d 0a 53 51 4c 49 54 45 5f 50 52  MSYS3..SQLITE_PR
83a70 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
83a80 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
83a90 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
83aa0 6d 73 79 73 33 28 76 6f 69 64 29 3b 0d 0a 23 65  msys3(void);..#e
83ab0 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
83ac0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
83ad0 53 35 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  S5..SQLITE_PRIVA
83ae0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
83af0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71  _mem_methods *sq
83b00 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79  lite3MemGetMemsy
83b10 73 35 28 76 6f 69 64 29 3b 0d 0a 23 65 6e 64 69  s5(void);..#endi
83b20 66 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  f......#ifndef S
83b30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
83b40 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
83b50 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
83b60 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
83b70 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
83b80 74 65 78 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49  tex(void);..SQLI
83b90 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c  TE_PRIVATE   sql
83ba0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
83bb0 64 73 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65  ds const *sqlite
83bc0 33 4e 6f 6f 70 4d 75 74 65 78 28 76 6f 69 64 29  3NoopMutex(void)
83bd0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
83be0 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E   sqlite3_mute
83bf0 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41  x *sqlite3MutexA
83c00 6c 6c 6f 63 28 69 6e 74 29 3b 0d 0a 53 51 4c 49  lloc(int);..SQLI
83c10 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
83c20 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69   sqlite3MutexIni
83c30 74 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54 45  t(void);..SQLITE
83c40 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
83c50 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76  qlite3MutexEnd(v
83c60 6f 69 64 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  oid);..#endif...
83c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
83c80 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75  int sqlite3Statu
83c90 73 56 61 6c 75 65 28 69 6e 74 29 3b 0d 0a 53 51  sValue(int);..SQ
83ca0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
83cb0 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  d sqlite3StatusA
83cc0 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53  dd(int, int);..S
83cd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
83ce0 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  id sqlite3Status
83cf0 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a  Set(int, int);..
83d00 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
83d10 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
83d20 4f 49 4e 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49  OINT..SQLITE_PRI
83d30 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
83d40 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b  e3IsNaN(double);
83d50 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
83d60 65 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 58  e sqlite3IsNaN(X
83d70 29 20 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  )  0..#endif....
83d80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
83d90 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 69  oid sqlite3VXPri
83da0 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69  ntf(StrAccum*, i
83db0 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
83dc0 20 76 61 5f 6c 69 73 74 29 3b 0d 0a 23 69 66 6e   va_list);..#ifn
83dd0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
83de0 54 52 41 43 45 0d 0a 53 51 4c 49 54 45 5f 50 52  TRACE..SQLITE_PR
83df0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
83e00 65 33 58 50 72 69 6e 74 66 28 53 74 72 41 63 63  e3XPrintf(StrAcc
83e10 75 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  um*, const char*
83e20 2c 20 2e 2e 2e 29 3b 0d 0a 23 65 6e 64 69 66 0d  , ...);..#endif.
83e30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
83e40 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72  char *sqlite3MPr
83e50 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f  intf(sqlite3*,co
83e60 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
83e70 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
83e80 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d   char *sqlite3VM
83e90 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c  Printf(sqlite3*,
83ea0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
83eb0 6c 69 73 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  list);..SQLITE_P
83ec0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
83ed0 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c  ite3MAppendf(sql
83ee0 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e 73  ite3*,char*,cons
83ef0 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0d 0a 23  t char*,...);..#
83f00 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
83f10 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
83f20 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
83f30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
83f40 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44     void sqlite3D
83f50 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74  ebugPrintf(const
83f60 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a 23   char*, ...);..#
83f70 65 6e 64 69 66 0d 0a 23 69 66 20 64 65 66 69 6e  endif..#if defin
83f80 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0d  ed(SQLITE_TEST).
83f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
83fa0 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54    void *sqlite3T
83fb0 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e  estTextToPtr(con
83fc0 73 74 20 63 68 61 72 2a 29 3b 0d 0a 23 65 6e 64  st char*);..#end
83fd0 69 66 0d 0a 0d 0a 2f 2a 20 4f 75 74 70 75 74 20  if..../* Output 
83fe0 66 6f 72 6d 61 74 74 69 6e 67 20 66 6f 72 20 53  formatting for S
83ff0 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 45  QLITE_TESTCTRL_E
84000 58 50 4c 41 49 4e 20 2a 2f 0d 0a 23 69 66 20 64  XPLAIN */..#if d
84010 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
84020 41 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49  ABLE_TREE_EXPLAI
84030 4e 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  N)..SQLITE_PRIVA
84040 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
84050 33 45 78 70 6c 61 69 6e 42 65 67 69 6e 28 56 64  3ExplainBegin(Vd
84060 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  be*);..SQLITE_PR
84070 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
84080 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74  ite3ExplainPrint
84090 66 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63  f(Vdbe*, const c
840a0 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a 53 51 4c  har*, ...);..SQL
840b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
840c0 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  id sqlite3Explai
840d0 6e 4e 4c 28 56 64 62 65 2a 29 3b 0d 0a 53 51 4c  nNL(Vdbe*);..SQL
840e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
840f0 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  id sqlite3Explai
84100 6e 50 75 73 68 28 56 64 62 65 2a 29 3b 0d 0a 53  nPush(Vdbe*);..S
84110 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
84120 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c  void sqlite3Expl
84130 61 69 6e 50 6f 70 28 56 64 62 65 2a 29 3b 0d 0a  ainPop(Vdbe*);..
84140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
84150 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
84160 6c 61 69 6e 46 69 6e 69 73 68 28 56 64 62 65 2a  lainFinish(Vdbe*
84170 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
84180 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
84190 33 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 56  3ExplainSelect(V
841a0 64 62 65 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0d  dbe*, Select*);.
841b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
841c0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78    void sqlite3Ex
841d0 70 6c 61 69 6e 45 78 70 72 28 56 64 62 65 2a 2c  plainExpr(Vdbe*,
841e0 20 45 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45   Expr*);..SQLITE
841f0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
84200 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
84210 70 72 4c 69 73 74 28 56 64 62 65 2a 2c 20 45 78  prList(Vdbe*, Ex
84220 70 72 4c 69 73 74 2a 29 3b 0d 0a 53 51 4c 49 54  prList*);..SQLIT
84230 45 5f 50 52 49 56 41 54 45 20 20 20 63 6f 6e 73  E_PRIVATE   cons
84240 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56  t char *sqlite3V
84250 64 62 65 45 78 70 6c 61 6e 61 74 69 6f 6e 28 56  dbeExplanation(V
84260 64 62 65 2a 29 3b 0d 0a 23 65 6c 73 65 0d 0a 23  dbe*);..#else..#
84270 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45   define sqlite3E
84280 78 70 6c 61 69 6e 42 65 67 69 6e 28 58 29 0d 0a  xplainBegin(X)..
84290 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
842a0 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 41 2c  ExplainSelect(A,
842b0 42 29 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  B)..# define sql
842c0 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28  ite3ExplainExpr(
842d0 41 2c 42 29 0d 0a 23 20 64 65 66 69 6e 65 20 73  A,B)..# define s
842e0 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70  qlite3ExplainExp
842f0 72 4c 69 73 74 28 41 2c 42 29 0d 0a 23 20 64 65  rList(A,B)..# de
84300 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 6c  fine sqlite3Expl
84310 61 69 6e 46 69 6e 69 73 68 28 58 29 0d 0a 23 20  ainFinish(X)..# 
84320 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64  define sqlite3Vd
84330 62 65 45 78 70 6c 61 6e 61 74 69 6f 6e 28 58 29  beExplanation(X)
84340 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a   0..#endif......
84350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84360 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74  oid sqlite3SetSt
84370 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71  ring(char **, sq
84380 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68  lite3*, const ch
84390 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a 53 51 4c 49  ar*, ...);..SQLI
843a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
843b0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
843c0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Parse*, const ch
843d0 61 72 2a 2c 20 2e 2e 2e 29 3b 0d 0a 53 51 4c 49  ar*, ...);..SQLI
843e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
843f0 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68  qlite3Dequote(ch
84400 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  ar*);..SQLITE_PR
84410 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
84420 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e  3KeywordCode(con
84430 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
84440 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
84450 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
84460 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61  ite3RunParser(Pa
84470 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  rse*, const char
84480 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0d 0a 53 51  *, char **);..SQ
84490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
844a0 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
844b0 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0d 0a  oding(Parse*);..
844c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
844d0 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
844e0 70 52 65 67 28 50 61 72 73 65 2a 29 3b 0d 0a 53  pReg(Parse*);..S
844f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
84500 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
84510 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c  eTempReg(Parse*,
84520 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
84530 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
84540 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61  3GetTempRange(Pa
84550 72 73 65 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49  rse*,int);..SQLI
84560 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
84570 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
84580 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69  mpRange(Parse*,i
84590 6e 74 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  nt,int);..SQLITE
845a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
845b0 6c 69 74 65 33 43 6c 65 61 72 54 65 6d 70 52 65  lite3ClearTempRe
845c0 67 43 61 63 68 65 28 50 61 72 73 65 2a 29 3b 0d  gCache(Parse*);.
845d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
845e0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
845f0 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c  rAlloc(sqlite3*,
84600 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a  int,const Token*
84610 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ,int);..SQLITE_P
84620 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
84630 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33  ite3Expr(sqlite3
84640 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
84650 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
84660 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
84670 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65  ExprAttachSubtre
84680 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72  es(sqlite3*,Expr
84690 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0d  *,Expr*,Expr*);.
846a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
846b0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78  Expr *sqlite3PEx
846c0 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  pr(Parse*, int, 
846d0 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f  Expr*, Expr*, co
846e0 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51  nst Token*);..SQ
846f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
84700 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
84710 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  d(sqlite3*,Expr*
84720 2c 20 45 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54  , Expr*);..SQLIT
84730 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
84740 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
84750 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c  ion(Parse*,ExprL
84760 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a  ist*, Token*);..
84770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84780 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
84790 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50  ssignVarNumber(P
847a0 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0d 0a  arse*, Expr*);..
847b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
847c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44  oid sqlite3ExprD
847d0 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20  elete(sqlite3*, 
847e0 45 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  Expr*);..SQLITE_
847f0 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
84800 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
84810 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45  tAppend(Parse*,E
84820 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b  xprList*,Expr*);
84830 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
84840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
84850 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72  rListSetName(Par
84860 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f  se*,ExprList*,To
84870 6b 65 6e 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49  ken*,int);..SQLI
84880 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
84890 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
848a0 65 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78  etSpan(Parse*,Ex
848b0 70 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e  prList*,ExprSpan
848c0 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
848d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
848e0 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
848f0 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
84900 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  t*);..SQLITE_PRI
84910 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
84920 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63  Init(sqlite3*, c
84930 68 61 72 2a 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  har**);..SQLITE_
84940 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
84950 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28  te3InitCallback(
84960 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72  void*, int, char
84970 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0d 0a 53 51  **, char**);..SQ
84980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84990 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  d sqlite3Pragma(
849a0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Parse*,Token*,To
849b0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29  ken*,Token*,int)
849c0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
849d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
849e0 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
849f0 61 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  a(sqlite3*, int)
84a00 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
84a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
84a20 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
84a30 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
84a40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
84a50 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
84a60 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a  Changes(sqlite3*
84a70 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
84a80 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
84a90 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
84aa0 63 74 28 50 61 72 73 65 2a 2c 53 65 6c 65 63 74  ct(Parse*,Select
84ab0 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
84ac0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
84ad0 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
84ae0 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 3b 0d 0a  Parse *, int);..
84af0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84b00 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 74  oid sqlite3Start
84b10 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f 6b  Table(Parse*,Tok
84b20 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69  en*,Token*,int,i
84b30 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0d 0a 53 51  nt,int,int);..SQ
84b40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84b50 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  d sqlite3AddColu
84b60 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  mn(Parse*,Token*
84b70 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
84b80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
84b90 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a  ddNotNull(Parse*
84ba0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
84bb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
84bc0 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
84bd0 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69  y(Parse*, ExprLi
84be0 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  st*, int, int, i
84bf0 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
84c00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
84c10 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61  3AddCheckConstra
84c20 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72  int(Parse*, Expr
84c30 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
84c40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
84c50 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61  AddColumnType(Pa
84c60 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0d 0a 53  rse*,Token*);..S
84c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
84c80 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
84c90 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a  aultValue(Parse*
84ca0 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0d 0a 53 51  ,ExprSpan*);..SQ
84cb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84cc0 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c  d sqlite3AddColl
84cd0 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20  ateType(Parse*, 
84ce0 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45  Token*);..SQLITE
84cf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
84d00 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61  lite3EndTable(Pa
84d10 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65  rse*,Token*,Toke
84d20 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b 0d 0a 53 51  n*,Select*);..SQ
84d30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
84d40 20 73 71 6c 69 74 65 33 50 61 72 73 65 55 72 69   sqlite3ParseUri
84d50 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  (const char*,con
84d60 73 74 20 63 68 61 72 2a 2c 75 6e 73 69 67 6e 65  st char*,unsigne
84d70 64 20 69 6e 74 2a 2c 0d 0a 20 20 20 20 20 20 20  d int*,..       
84d80 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
84d90 69 74 65 33 5f 76 66 73 2a 2a 2c 63 68 61 72 2a  ite3_vfs**,char*
84da0 2a 2c 63 68 61 72 20 2a 2a 29 3b 0d 0a 53 51 4c  *,char **);..SQL
84db0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84dc0 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63 65 28  sqlite3CodeOnce(
84dd0 50 61 72 73 65 20 2a 29 3b 0d 0a 0d 0a 53 51 4c  Parse *);....SQL
84de0 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76  ITE_PRIVATE Bitv
84df0 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65  ec *sqlite3Bitve
84e00 63 43 72 65 61 74 65 28 75 33 32 29 3b 0d 0a 53  cCreate(u32);..S
84e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
84e20 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  t sqlite3BitvecT
84e30 65 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32  est(Bitvec*, u32
84e40 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
84e50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
84e60 74 76 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c  tvecSet(Bitvec*,
84e70 20 75 33 32 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   u32);..SQLITE_P
84e80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
84e90 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 42  te3BitvecClear(B
84ea0 69 74 76 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69  itvec*, u32, voi
84eb0 64 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  d*);..SQLITE_PRI
84ec0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
84ed0 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42  3BitvecDestroy(B
84ee0 69 74 76 65 63 2a 29 3b 0d 0a 53 51 4c 49 54 45  itvec*);..SQLITE
84ef0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
84f00 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42  ite3BitvecSize(B
84f10 69 74 76 65 63 2a 29 3b 0d 0a 53 51 4c 49 54 45  itvec*);..SQLITE
84f20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
84f30 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
84f40 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b  nTest(int,int*);
84f50 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ....SQLITE_PRIVA
84f60 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74  TE RowSet *sqlit
84f70 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c  e3RowSetInit(sql
84f80 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e  ite3*, void*, un
84f90 73 69 67 6e 65 64 20 69 6e 74 29 3b 0d 0a 53 51  signed int);..SQ
84fa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84fb0 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43  d sqlite3RowSetC
84fc0 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b 0d 0a  lear(RowSet*);..
84fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84fe0 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65  oid sqlite3RowSe
84ff0 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a 2c  tInsert(RowSet*,
85000 20 69 36 34 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   i64);..SQLITE_P
85010 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
85020 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77  e3RowSetTest(Row
85030 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c  Set*, u8 iBatch,
85040 20 69 36 34 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   i64);..SQLITE_P
85050 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
85060 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77  e3RowSetNext(Row
85070 53 65 74 2a 2c 20 69 36 34 2a 29 3b 0d 0a 0d 0a  Set*, i64*);....
85080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
85090 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
850a0 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b  eView(Parse*,Tok
850b0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  en*,Token*,Token
850c0 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e  *,Select*,int,in
850d0 74 29 3b 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69  t);....#if !defi
850e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
850f0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
85100 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
85110 52 54 55 41 4c 54 41 42 4c 45 29 0d 0a 53 51 4c  RTUALTABLE)..SQL
85120 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
85130 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  t sqlite3ViewGet
85140 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73  ColumnNames(Pars
85150 65 2a 2c 54 61 62 6c 65 2a 29 3b 0d 0a 23 65 6c  e*,Table*);..#el
85160 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  se..# define sql
85170 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
85180 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0d 0a 23  nNames(A,B) 0..#
85190 65 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45 5f  endif....SQLITE_
851a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
851b0 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61  ite3DropTable(Pa
851c0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
851d0 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49  int, int);..SQLI
851e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
851f0 73 71 6c 69 74 65 33 43 6f 64 65 44 72 6f 70 54  sqlite3CodeDropT
85200 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 54 61 62  able(Parse*, Tab
85210 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d  le*, int, int);.
85220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85230 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
85240 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a  teTable(sqlite3*
85250 2c 20 54 61 62 6c 65 2a 29 3b 0d 0a 23 69 66 6e  , Table*);..#ifn
85260 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
85270 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0d 0a 53  AUTOINCREMENT..S
85280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
85290 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f  void sqlite3Auto
852a0 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 50  incrementBegin(P
852b0 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0d 0a  arse *pParse);..
852c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
852d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
852e0 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50 61  oincrementEnd(Pa
852f0 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0d 0a 23  rse *pParse);..#
85300 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 73  else..# define s
85310 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
85320 65 6e 74 42 65 67 69 6e 28 58 29 0d 0a 23 20 64  entBegin(X)..# d
85330 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
85340 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29  oincrementEnd(X)
85350 0d 0a 23 65 6e 64 69 66 0d 0a 53 51 4c 49 54 45  ..#endif..SQLITE
85360 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
85370 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73  lite3Insert(Pars
85380 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
85390 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a  prList*, Select*
853a0 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b  , IdList*, int);
853b0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
853c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72   void *sqlite3Ar
853d0 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69  rayAllocate(sqli
853e0 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69  te3*,void*,int,i
853f0 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74  nt,int*,int*,int
85400 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
85410 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
85420 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
85430 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74  sqlite3*, IdList
85440 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c  *, Token*);..SQL
85450 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
85460 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64  sqlite3IdListInd
85470 65 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74  ex(IdList*,const
85480 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45   char*);..SQLITE
85490 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
854a0 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
854b0 45 6e 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a  Enlarge(sqlite3*
854c0 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c  , SrcList*, int,
854d0 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
854e0 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
854f0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
85500 70 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 53  pend(sqlite3*, S
85510 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c  rcList*, Token*,
85520 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c 49 54   Token*);..SQLIT
85530 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
85540 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
85550 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28  tAppendFromTerm(
85560 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
85570 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
85580 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
85590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
855a0 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c           Token*,
855b0 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 2a 2c   Select*, Expr*,
855c0 20 49 64 4c 69 73 74 2a 29 3b 0d 0a 53 51 4c 49   IdList*);..SQLI
855d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
855e0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e  sqlite3SrcListIn
855f0 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 2c  dexedBy(Parse *,
85600 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 6f 6b 65   SrcList *, Toke
85610 6e 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  n *);..SQLITE_PR
85620 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
85630 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
85640 28 50 61 72 73 65 20 2a 2c 20 73 74 72 75 63 74  (Parse *, struct
85650 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 29   SrcList_item *)
85660 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85670 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
85680 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79  cListShiftJoinTy
85690 70 65 28 53 72 63 4c 69 73 74 2a 29 3b 0d 0a 53  pe(SrcList*);..S
856a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
856b0 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
856c0 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50  tAssignCursors(P
856d0 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29  arse*, SrcList*)
856e0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
856f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64  E void sqlite3Id
85700 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
85710 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0d 0a  e3*, IdList*);..
85720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
85730 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
85740 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
85750 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0d 0a 53  *, SrcList*);..S
85760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e  QLITE_PRIVATE In
85770 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61  dex *sqlite3Crea
85780 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54  teIndex(Parse*,T
85790 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63  oken*,Token*,Src
857a0 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  List*,ExprList*,
857b0 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0d 0a 20 20 20  int,Token*,..   
857c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
857d0 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74       Token*, int
857e0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
857f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
85800 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61  ite3DropIndex(Pa
85810 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
85820 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
85830 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
85840 33 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20  3Select(Parse*, 
85850 53 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44  Select*, SelectD
85860 65 73 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  est*);..SQLITE_P
85870 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
85880 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
85890 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
858a0 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c  ,SrcList*,Expr*,
858b0 45 78 70 72 4c 69 73 74 2a 2c 0d 0a 20 20 20 20  ExprList*,..    
858c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
858d0 20 20 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c       Expr*,ExprL
858e0 69 73 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45  ist*,int,Expr*,E
858f0 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  xpr*);..SQLITE_P
85900 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
85910 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
85920 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74  sqlite3*, Select
85930 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
85940 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
85950 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
85960 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
85970 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
85980 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
85990 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 2a 2c  ReadOnly(Parse*,
859a0 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0d 0a   Table*, int);..
859b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
859c0 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  oid sqlite3OpenT
859d0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  able(Parse*, int
859e0 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c 20   iCur, int iDb, 
859f0 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0d 0a 23  Table*, int);..#
85a00 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
85a10 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f  E_ENABLE_UPDATE_
85a20 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26  DELETE_LIMIT) &&
85a30 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
85a40 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0d  _OMIT_SUBQUERY).
85a50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85a60 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d  Expr *sqlite3Lim
85a70 69 74 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c  itWhere(Parse *,
85a80 20 53 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72   SrcList *, Expr
85a90 20 2a 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20   *, ExprList *, 
85aa0 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20  Expr *, Expr *, 
85ab0 63 68 61 72 20 2a 29 3b 0d 0a 23 65 6e 64 69 66  char *);..#endif
85ac0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
85ad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c   void sqlite3Del
85ae0 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c 20  eteFrom(Parse*, 
85af0 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 29  SrcList*, Expr*)
85b00 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85b10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70  E void sqlite3Up
85b20 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 63  date(Parse*, Src
85b30 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a  List*, ExprList*
85b40 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0d 0a  , Expr*, int);..
85b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
85b60 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
85b70 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73  3WhereBegin(Pars
85b80 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
85b90 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c  pr*, ExprList**,
85ba0 45 78 70 72 4c 69 73 74 2a 2c 75 31 36 29 3b 0d  ExprList*,u16);.
85bb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85bc0 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72  void sqlite3Wher
85bd0 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a 29  eEnd(WhereInfo*)
85be0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85bf0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
85c00 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50  rCodeGetColumn(P
85c10 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
85c20 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a  nt, int, int);..
85c30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
85c40 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
85c50 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 54 61  odeGetColumnOfTa
85c60 62 6c 65 28 56 64 62 65 2a 2c 20 54 61 62 6c 65  ble(Vdbe*, Table
85c70 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
85c80 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
85c90 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
85ca0 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73  xprCodeMove(Pars
85cb0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
85cc0 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
85cd0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
85ce0 45 78 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72  ExprCodeCopy(Par
85cf0 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  se*, int, int, i
85d00 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
85d10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85d20 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28  3ExprCacheStore(
85d30 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
85d40 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
85d50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
85d60 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73  ite3ExprCachePus
85d70 68 28 50 61 72 73 65 2a 29 3b 0d 0a 53 51 4c 49  h(Parse*);..SQLI
85d80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
85d90 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
85da0 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  Pop(Parse*, int)
85db0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85dc0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
85dd0 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61  prCacheRemove(Pa
85de0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
85df0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
85e00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
85e10 72 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73  rCacheClear(Pars
85e20 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  e*);..SQLITE_PRI
85e30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85e40 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
85e50 74 79 43 68 61 6e 67 65 28 50 61 72 73 65 2a 2c  tyChange(Parse*,
85e60 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c   int, int);..SQL
85e70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
85e80 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
85e90 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
85ea0 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
85eb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
85ec0 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72  ExprCodeTemp(Par
85ed0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a  se*, Expr*, int*
85ee0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
85ef0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
85f00 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72  prCodeTarget(Par
85f10 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
85f20 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85f30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
85f40 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61  rCodeAndCache(Pa
85f50 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
85f60 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
85f70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
85f80 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
85f90 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
85fa0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
85fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
85fc0 43 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72  CodeExprList(Par
85fd0 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
85fe0 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49  int, int);..SQLI
85ff0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86000 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
86010 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
86020 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c   int, int);..SQL
86030 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
86040 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
86050 6c 73 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72  lse(Parse*, Expr
86060 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53  *, int, int);..S
86070 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
86080 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  ble *sqlite3Find
86090 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 63  Table(sqlite3*,c
860a0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
860b0 74 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54  t char*);..SQLIT
860c0 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
860d0 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61  *sqlite3LocateTa
860e0 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 20 69  ble(Parse*,int i
860f0 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 61 72  sView,const char
86100 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
86110 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
86120 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46   Index *sqlite3F
86130 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33  indIndex(sqlite3
86140 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  *,const char*, c
86150 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a 53 51  onst char*);..SQ
86160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
86170 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
86180 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71  ndDeleteTable(sq
86190 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
861a0 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45   char*);..SQLITE
861b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
861c0 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
861d0 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65  leteIndex(sqlite
861e0 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  3*,int,const cha
861f0 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
86200 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86210 33 56 61 63 75 75 6d 28 50 61 72 73 65 2a 29 3b  3Vacuum(Parse*);
86220 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
86230 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56   int sqlite3RunV
86240 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c 20 73 71  acuum(char**, sq
86250 6c 69 74 65 33 2a 29 3b 0d 0a 53 51 4c 49 54 45  lite3*);..SQLITE
86260 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
86270 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
86280 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f  ken(sqlite3*, To
86290 6b 65 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ken*);..SQLITE_P
862a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
862b0 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78  e3ExprCompare(Ex
862c0 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0d 0a 53 51  pr*, Expr*);..SQ
862d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
862e0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
862f0 43 6f 6d 70 61 72 65 28 45 78 70 72 4c 69 73 74  Compare(ExprList
86300 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0d 0a  *, ExprList*);..
86310 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86320 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
86330 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
86340 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45  (NameContext*, E
86350 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  xpr*);..SQLITE_P
86360 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
86370 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
86380 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78  gList(NameContex
86390 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0d 0a  t*,ExprList*);..
863a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
863b0 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
863c0 64 62 65 28 50 61 72 73 65 2a 29 3b 0d 0a 53 51  dbe(Parse*);..SQ
863d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
863e0 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76  d sqlite3PrngSav
863f0 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0d 0a 53  eState(void);..S
86400 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
86410 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
86420 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29  storeState(void)
86430 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
86440 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
86450 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 6f 69  ngResetState(voi
86460 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  d);..SQLITE_PRIV
86470 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86480 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69  RollbackAll(sqli
86490 74 65 33 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  te3*);..SQLITE_P
864a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
864b0 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
864c0 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ema(Parse*, int)
864d0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
864e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
864f0 64 65 56 65 72 69 66 79 4e 61 6d 65 64 53 63 68  deVerifyNamedSch
86500 65 6d 61 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  ema(Parse*, cons
86510 74 20 63 68 61 72 20 2a 7a 44 62 29 3b 0d 0a 53  t char *zDb);..S
86520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
86530 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
86540 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
86550 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
86560 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86570 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73  lite3CommitTrans
86580 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0d  action(Parse*);.
86590 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
865a0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c  void sqlite3Roll
865b0 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28  backTransaction(
865c0 50 61 72 73 65 2a 29 3b 0d 0a 53 51 4c 49 54 45  Parse*);..SQLITE
865d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
865e0 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 50  lite3Savepoint(P
865f0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65  arse*, int, Toke
86600 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  n*);..SQLITE_PRI
86610 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86620 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
86630 28 73 71 6c 69 74 65 33 20 2a 29 3b 0d 0a 53 51  (sqlite3 *);..SQ
86640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
86650 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
86660 6e 73 74 61 6e 74 28 45 78 70 72 2a 29 3b 0d 0a  nstant(Expr*);..
86670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
86680 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
86690 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28  ConstantNotJoin(
866a0 45 78 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  Expr*);..SQLITE_
866b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
866c0 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
866d0 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72  tOrFunction(Expr
866e0 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
866f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
86700 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 70  xprIsInteger(Exp
86710 72 2a 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51 4c 49  r*, int*);..SQLI
86720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
86730 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e  qlite3ExprCanBeN
86740 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a 29  ull(const Expr*)
86750 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
86760 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
86770 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70  prCodeIsNullJump
86780 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78  (Vdbe*, const Ex
86790 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d  pr*, int, int);.
867a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
867b0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e  int sqlite3ExprN
867c0 65 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68  eedsNoAffinityCh
867d0 61 6e 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a  ange(const Expr*
867e0 2c 20 63 68 61 72 29 3b 0d 0a 53 51 4c 49 54 45  , char);..SQLITE
867f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
86800 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73  ite3IsRowid(cons
86810 74 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54  t char*);..SQLIT
86820 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
86830 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
86840 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20  wDelete(Parse*, 
86850 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
86860 2c 20 69 6e 74 2c 20 54 72 69 67 67 65 72 20 2a  , int, Trigger *
86870 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
86880 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
86890 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
868a0 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73 65  ndexDelete(Parse
868b0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
868c0 69 6e 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  int*);..SQLITE_P
868d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
868e0 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b  e3GenerateIndexK
868f0 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78  ey(Parse*, Index
86900 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
86910 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
86920 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
86930 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
86940 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54  tChecks(Parse*,T
86950 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0d 0a  able*,int,int,..
86960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86980 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e       int*,int,in
86990 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b  t,int,int,int*);
869a0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
869b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
869c0 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50  pleteInsertion(P
869d0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
869e0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69  nt, int, int*, i
869f0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a  nt, int, int);..
86a00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
86a10 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  nt sqlite3OpenTa
86a20 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61  bleAndIndices(Pa
86a30 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
86a40 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  t, int);..SQLITE
86a50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86a60 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
86a70 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c  peration(Parse*,
86a80 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c   int, int);..SQL
86a90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
86aa0 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69   sqlite3MultiWri
86ab0 74 65 28 50 61 72 73 65 2a 29 3b 0d 0a 53 51 4c  te(Parse*);..SQL
86ac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
86ad0 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
86ae0 28 50 61 72 73 65 2a 29 3b 0d 0a 53 51 4c 49 54  (Parse*);..SQLIT
86af0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
86b00 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
86b10 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e 74  aint(Parse*, int
86b20 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0d 0a  , char*, int);..
86b30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
86b40 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
86b50 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70  Dup(sqlite3*,Exp
86b60 72 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  r*,int);..SQLITE
86b70 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
86b80 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
86b90 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  stDup(sqlite3*,E
86ba0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0d 0a  xprList*,int);..
86bb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
86bc0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
86bd0 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  rcListDup(sqlite
86be0 33 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29  3*,SrcList*,int)
86bf0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
86c00 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65  E IdList *sqlite
86c10 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74  3IdListDup(sqlit
86c20 65 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0d 0a 53  e3*,IdList*);..S
86c30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65  QLITE_PRIVATE Se
86c40 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c  lect *sqlite3Sel
86c50 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  ectDup(sqlite3*,
86c60 53 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 0d 0a 53  Select*,int);..S
86c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
86c80 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65  id sqlite3FuncDe
86c90 66 49 6e 73 65 72 74 28 46 75 6e 63 44 65 66 48  fInsert(FuncDefH
86ca0 61 73 68 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b  ash*, FuncDef*);
86cb0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
86cc0 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65   FuncDef *sqlite
86cd0 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 73 71  3FindFunction(sq
86ce0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
86cf0 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e  r*,int,int,u8,in
86d00 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
86d10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86d20 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46  RegisterBuiltinF
86d30 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
86d40 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
86d50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86d60 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65  RegisterDateTime
86d70 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b  Functions(void);
86d80 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
86d90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
86da0 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74  isterGlobalFunct
86db0 69 6f 6e 73 28 76 6f 69 64 29 3b 0d 0a 53 51 4c  ions(void);..SQL
86dc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
86dd0 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
86de0 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0d  ckOk(sqlite3*);.
86df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
86e00 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
86e10 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
86e20 71 6c 69 74 65 33 2a 29 3b 0d 0a 53 51 4c 49 54  qlite3*);..SQLIT
86e30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
86e40 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
86e50 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b  ie(Parse*, int);
86e60 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
86e70 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
86e80 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  W) && !defined(S
86e90 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
86ea0 45 52 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ER)..SQLITE_PRIV
86eb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86ec0 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28  MaterializeView(
86ed0 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
86ee0 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0d 0a 23 65  Expr*, int);..#e
86ef0 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ndif....#ifndef 
86f00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
86f10 47 45 52 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  GER..SQLITE_PRIV
86f20 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
86f30 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28 50  e3BeginTrigger(P
86f40 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f  arse*, Token*,To
86f50 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c  ken*,int,int,IdL
86f60 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0d 0a  ist*,SrcList*,..
86f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86f80 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 2a             Expr*
86f90 2c 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c  ,int, int);..SQL
86fa0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
86fb0 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  id sqlite3Finish
86fc0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20  Trigger(Parse*, 
86fd0 54 72 69 67 67 65 72 53 74 65 70 2a 2c 20 54 6f  TriggerStep*, To
86fe0 6b 65 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ken*);..SQLITE_P
86ff0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
87000 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
87010 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
87020 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
87030 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
87040 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
87050 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54 72  erPtr(Parse*, Tr
87060 69 67 67 65 72 2a 29 3b 0d 0a 53 51 4c 49 54 45  igger*);..SQLITE
87070 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
87080 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  er *sqlite3Trigg
87090 65 72 73 45 78 69 73 74 28 50 61 72 73 65 20 2a  ersExist(Parse *
870a0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45  , Table*, int, E
870b0 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a 70  xprList*, int *p
870c0 4d 61 73 6b 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  Mask);..SQLITE_P
870d0 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
870e0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
870f0 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 54 61  List(Parse *, Ta
87100 62 6c 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  ble *);..SQLITE_
87110 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
87120 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
87130 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69  gger(Parse*, Tri
87140 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 70  gger *, int, Exp
87150 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62  rList*, int, Tab
87160 6c 65 20 2a 2c 0d 0a 20 20 20 20 20 20 20 20 20  le *,..         
87170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87180 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74     int, int, int
87190 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
871a0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
871b0 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
871c0 69 72 65 63 74 28 50 61 72 73 65 20 2a 2c 20 54  irect(Parse *, T
871d0 72 69 67 67 65 72 20 2a 2c 20 54 61 62 6c 65 20  rigger *, Table 
871e0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
871f0 29 3b 0d 0a 20 20 76 6f 69 64 20 73 71 6c 69 74  );..  void sqlit
87200 65 56 69 65 77 54 72 69 67 67 65 72 73 28 50 61  eViewTriggers(Pa
87210 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78  rse*, Table*, Ex
87220 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69  pr*, int, ExprLi
87230 73 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  st*);..SQLITE_PR
87240 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
87250 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
87260 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 20  rStep(sqlite3*, 
87270 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0d 0a  TriggerStep*);..
87280 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
87290 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
872a0 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65  lite3TriggerSele
872b0 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c  ctStep(sqlite3*,
872c0 53 65 6c 65 63 74 2a 29 3b 0d 0a 53 51 4c 49 54  Select*);..SQLIT
872d0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
872e0 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
872f0 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65  TriggerInsertSte
87300 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
87310 2a 2c 20 49 64 4c 69 73 74 2a 2c 0d 0a 20 20 20  *, IdList*,..   
87320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87340 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53       ExprList*,S
87350 65 6c 65 63 74 2a 2c 75 38 29 3b 0d 0a 53 51 4c  elect*,u8);..SQL
87360 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
87370 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
87380 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53  e3TriggerUpdateS
87390 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b  tep(sqlite3*,Tok
873a0 65 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45  en*,ExprList*, E
873b0 78 70 72 2a 2c 20 75 38 29 3b 0d 0a 53 51 4c 49  xpr*, u8);..SQLI
873c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
873d0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
873e0 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74  3TriggerDeleteSt
873f0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
87400 6e 2a 2c 20 45 78 70 72 2a 29 3b 0d 0a 53 51 4c  n*, Expr*);..SQL
87410 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
87420 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
87430 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a  Trigger(sqlite3*
87440 2c 20 54 72 69 67 67 65 72 2a 29 3b 0d 0a 53 51  , Trigger*);..SQ
87450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
87460 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
87470 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65  kAndDeleteTrigge
87480 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63  r(sqlite3*,int,c
87490 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a 53 51  onst char*);..SQ
874a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 75  LITE_PRIVATE   u
874b0 33 32 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  32 sqlite3Trigge
874c0 72 43 6f 6c 6d 61 73 6b 28 50 61 72 73 65 2a 2c  rColmask(Parse*,
874d0 54 72 69 67 67 65 72 2a 2c 45 78 70 72 4c 69 73  Trigger*,ExprLis
874e0 74 2a 2c 69 6e 74 2c 69 6e 74 2c 54 61 62 6c 65  t*,int,int,Table
874f0 2a 2c 69 6e 74 29 3b 0d 0a 23 20 64 65 66 69 6e  *,int);..# defin
87500 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  e sqlite3ParseTo
87510 70 6c 65 76 65 6c 28 70 29 20 28 28 70 29 2d 3e  plevel(p) ((p)->
87520 70 54 6f 70 6c 65 76 65 6c 20 3f 20 28 70 29 2d  pToplevel ? (p)-
87530 3e 70 54 6f 70 6c 65 76 65 6c 20 3a 20 28 70 29  >pToplevel : (p)
87540 29 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  )..#else..# defi
87550 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  ne sqlite3Trigge
87560 72 73 45 78 69 73 74 28 42 2c 43 2c 44 2c 45 2c  rsExist(B,C,D,E,
87570 46 29 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73  F) 0..# define s
87580 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
87590 67 65 72 28 41 2c 42 29 0d 0a 23 20 64 65 66 69  ger(A,B)..# defi
875a0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  ne sqlite3DropTr
875b0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0d 0a 23  iggerPtr(A,B)..#
875c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55   define sqlite3U
875d0 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72  nlinkAndDeleteTr
875e0 69 67 67 65 72 28 41 2c 42 2c 43 29 0d 0a 23 20  igger(A,B,C)..# 
875f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
87600 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42  deRowTrigger(A,B
87610 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29 0d  ,C,D,E,F,G,H,I).
87620 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
87630 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
87640 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c 45 2c  irect(A,B,C,D,E,
87650 46 29 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  F)..# define sql
87660 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
87670 58 2c 20 59 29 20 30 0d 0a 23 20 64 65 66 69 6e  X, Y) 0..# defin
87680 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  e sqlite3ParseTo
87690 70 6c 65 76 65 6c 28 70 29 20 70 0d 0a 23 20 64  plevel(p) p..# d
876a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69  efine sqlite3Tri
876b0 67 67 65 72 43 6f 6c 6d 61 73 6b 28 41 2c 42 2c  ggerColmask(A,B,
876c0 43 2c 44 2c 45 2c 46 2c 47 29 20 30 0d 0a 23 65  C,D,E,F,G) 0..#e
876d0 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50  ndif....SQLITE_P
876e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
876f0 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
87700 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
87710 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c  *, Token*);..SQL
87720 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87730 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f   sqlite3CreateFo
87740 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c  reignKey(Parse*,
87750 20 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65   ExprList*, Toke
87760 6e 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69  n*, ExprList*, i
87770 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
87780 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
87790 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79  3DeferForeignKey
877a0 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0d 0a  (Parse*, int);..
877b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
877c0 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
877d0 4e 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  N..SQLITE_PRIVAT
877e0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
877f0 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a 2c  AuthRead(Parse*,
87800 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c 53 72  Expr*,Schema*,Sr
87810 63 4c 69 73 74 2a 29 3b 0d 0a 53 51 4c 49 54 45  cList*);..SQLITE
87820 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
87830 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
87840 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e 73  Parse*,int, cons
87850 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
87860 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
87870 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
87880 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
87890 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
878a0 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f  h(Parse*, AuthCo
878b0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68  ntext*, const ch
878c0 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  ar*);..SQLITE_PR
878d0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
878e0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
878f0 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29  op(AuthContext*)
87900 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
87910 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41  E   int sqlite3A
87920 75 74 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65  uthReadCol(Parse
87930 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
87940 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
87950 6e 74 29 3b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64  nt);..#else..# d
87960 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
87970 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0d 0a  hRead(a,b,c,d)..
87980 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
87990 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c  AuthCheck(a,b,c,
879a0 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f  d,e)    SQLITE_O
879b0 4b 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  K..# define sqli
879c0 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
879d0 73 68 28 61 2c 62 2c 63 29 0d 0a 23 20 64 65 66  sh(a,b,c)..# def
879e0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43  ine sqlite3AuthC
879f0 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 28  ontextPop(a)  ((
87a00 76 6f 69 64 29 28 61 29 29 0d 0a 23 65 6e 64 69  void)(a))..#endi
87a10 66 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
87a20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74  E void sqlite3At
87a30 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70  tach(Parse*, Exp
87a40 72 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a  r*, Expr*, Expr*
87a50 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
87a60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
87a70 65 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78  etach(Parse*, Ex
87a80 70 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  pr*);..SQLITE_PR
87a90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
87aa0 33 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72  3FixInit(DbFixer
87ab0 2a 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  *, Parse*, int, 
87ac0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
87ad0 73 74 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c  st Token*);..SQL
87ae0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
87af0 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
87b00 74 28 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c  t(DbFixer*, SrcL
87b10 69 73 74 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ist*);..SQLITE_P
87b20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
87b30 65 33 46 69 78 53 65 6c 65 63 74 28 44 62 46 69  e3FixSelect(DbFi
87b40 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0d  xer*, Select*);.
87b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87b60 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78  int sqlite3FixEx
87b70 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70  pr(DbFixer*, Exp
87b80 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
87b90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
87ba0 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46 69  FixExprList(DbFi
87bb0 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  xer*, ExprList*)
87bc0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
87bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
87be0 54 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69  TriggerStep(DbFi
87bf0 78 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65  xer*, TriggerSte
87c00 70 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  p*);..SQLITE_PRI
87c10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
87c20 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20  AtoF(const char 
87c30 2a 7a 2c 20 64 6f 75 62 6c 65 2a 2c 20 69 6e 74  *z, double*, int
87c40 2c 20 75 38 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  , u8);..SQLITE_P
87c50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
87c60 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74  e3GetInt32(const
87c70 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0d   char *, int*);.
87c80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87c90 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 28  int sqlite3Atoi(
87ca0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a 53  const char*);..S
87cb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
87cc0 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  t sqlite3Utf16By
87cd0 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64  teLen(const void
87ce0 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68   *pData, int nCh
87cf0 61 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ar);..SQLITE_PRI
87d00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
87d10 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73  Utf8CharLen(cons
87d20 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
87d30 6e 74 20 6e 42 79 74 65 29 3b 0d 0a 53 51 4c 49  nt nByte);..SQLI
87d40 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
87d50 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 63  qlite3Utf8Read(c
87d60 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20  onst u8*, const 
87d70 75 38 2a 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  u8**);..../*..**
87d80 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61   Routines to rea
87d90 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 69  d and write vari
87da0 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
87db0 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 65  gers.  These use
87dc0 64 20 74 6f 0d 0a 2a 2a 20 62 65 20 64 65 66 69  d to..** be defi
87dd0 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74  ned locally, but
87de0 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 20   now we use the 
87df0 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20  varint routines 
87e00 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0d 0a 2a  in the util.c..*
87e10 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68  * file.  Code sh
87e20 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
87e30 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20  RO forms below, 
87e40 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20  as the Varint32 
87e50 76 65 72 73 69 6f 6e 73 0d 0a 2a 2a 20 61 72 65  versions..** are
87e60 20 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65   coded to assume
87e70 20 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65   the single byte
87e80 20 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79   case is already
87e90 20 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20   handled (which 
87ea0 0d 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66  ..** the MACRO f
87eb0 6f 72 6d 20 64 6f 65 73 29 2e 0d 0a 2a 2f 0d 0a  orm does)...*/..
87ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
87ed0 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
87ee0 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  int(unsigned cha
87ef0 72 2a 2c 20 75 36 34 29 3b 0d 0a 53 51 4c 49 54  r*, u64);..SQLIT
87f00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
87f10 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
87f20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c  (unsigned char*,
87f30 20 75 33 32 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   u32);..SQLITE_P
87f40 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
87f50 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74  3GetVarint(const
87f60 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
87f70 2c 20 75 36 34 20 2a 29 3b 0d 0a 53 51 4c 49 54  , u64 *);..SQLIT
87f80 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
87f90 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
87fa0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
87fb0 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0d 0a  har *, u32 *);..
87fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
87fd0 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74  nt sqlite3Varint
87fe0 4c 65 6e 28 75 36 34 20 76 29 3b 0d 0a 0d 0a 2f  Len(u64 v);..../
87ff0 2a 0d 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72  *..** The header
88000 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e   of a record con
88010 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75 65  sists of a seque
88020 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  nce variable-len
88030 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0d 0a 2a  gth integers...*
88040 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73  * These integers
88050 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61   are almost alwa
88060 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65  ys small and are
88070 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69   encoded as a si
88080 6e 67 6c 65 20 62 79 74 65 2e 0d 0a 2a 2a 20 54  ngle byte...** T
88090 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
880a0 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61  ros take advanta
880b0 67 65 20 74 68 69 73 20 66 61 63 74 20 74 6f 20  ge this fact to 
880c0 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 65  provide a fast e
880d0 6e 63 6f 64 65 0d 0a 2a 2a 20 61 6e 64 20 64 65  ncode..** and de
880e0 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65  code of the inte
880f0 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64  gers in a record
88100 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 20   header.  It is 
88110 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63  faster for the c
88120 6f 6d 6d 6f 6e 0d 0a 2a 2a 20 63 61 73 65 20 77  ommon..** case w
88130 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 72  here the integer
88140 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74   is a single byt
88150 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 74  e.  It is a litt
88160 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74  le slower when t
88170 68 65 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69  he..** integer i
88180 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79  s two or more by
88190 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c  tes.  But overal
881a0 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0d  l it is faster..
881b0 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  .**..** The foll
881c0 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  owing expression
881d0 73 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74  s are equivalent
881e0 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 78 20  :..**..**     x 
881f0 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  = sqlite3GetVari
88200 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b 0d 0a  nt32( A, &B );..
88210 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74  **     x = sqlit
88220 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 20 41  e3PutVarint32( A
88230 2c 20 42 20 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 20  , B );..**..**  
88240 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74     x = getVarint
88250 33 32 28 20 41 2c 20 42 20 29 3b 0d 0a 2a 2a 20  32( A, B );..** 
88260 20 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e      x = putVarin
88270 74 33 32 28 20 41 2c 20 42 20 29 3b 0d 0a 2a 2a  t32( A, B );..**
88280 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 67 65  ..*/..#define ge
88290 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20  tVarint32(A,B)  
882a0 28 75 38 29 28 28 2a 28 41 29 3c 28 75 38 29 30  (u8)((*(A)<(u8)0
882b0 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20 28 75  x80) ? ((B) = (u
882c0 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c  32)*(A)),1 : sql
882d0 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
882e0 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28 42 29  (A), (u32 *)&(B)
882f0 29 29 0d 0a 23 64 65 66 69 6e 65 20 70 75 74 56  ))..#define putV
88300 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75  arint32(A,B)  (u
88310 38 29 28 28 28 75 33 32 29 28 42 29 3c 28 75 33  8)(((u32)(B)<(u3
88320 32 29 30 78 38 30 29 20 3f 20 28 2a 28 41 29 20  2)0x80) ? (*(A) 
88330 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
88340 29 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 74 65  )(B)),1 : sqlite
88350 33 50 75 74 56 61 72 69 6e 74 33 32 28 28 41 29  3PutVarint32((A)
88360 2c 20 28 42 29 29 29 0d 0a 23 64 65 66 69 6e 65  , (B)))..#define
88370 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 73 71   getVarint    sq
88380 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 0d 0a  lite3GetVarint..
88390 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e  #define putVarin
883a0 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 56  t    sqlite3PutV
883b0 61 72 69 6e 74 0d 0a 0d 0a 0d 0a 53 51 4c 49 54  arint......SQLIT
883c0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
883d0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64  char *sqlite3Ind
883e0 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
883f0 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0d  be *, Index *);.
88400 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
88410 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c  void sqlite3Tabl
88420 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  eAffinityStr(Vdb
88430 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0d 0a  e *, Table *);..
88440 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
88450 68 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  har sqlite3Compa
88460 72 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  reAffinity(Expr 
88470 2a 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66  *pExpr, char aff
88480 32 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  2);..SQLITE_PRIV
88490 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
884a0 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45  ndexAffinityOk(E
884b0 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72  xpr *pExpr, char
884c0 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0d   idx_affinity);.
884d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
884e0 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70 72  char sqlite3Expr
884f0 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
88500 45 78 70 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  Expr);..SQLITE_P
88510 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
88520 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 63  e3Atoi64(const c
88530 68 61 72 2a 2c 20 69 36 34 2a 2c 20 69 6e 74 2c  har*, i64*, int,
88540 20 75 38 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52   u8);..SQLITE_PR
88550 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
88560 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2a  e3Error(sqlite3*
88570 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
88580 72 2a 2c 2e 2e 2e 29 3b 0d 0a 53 51 4c 49 54 45  r*,...);..SQLITE
88590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
885a0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
885b0 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
885c0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b  char *z, int n);
885d0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
885e0 20 75 38 20 73 71 6c 69 74 65 33 48 65 78 54 6f   u8 sqlite3HexTo
885f0 49 6e 74 28 69 6e 74 20 68 29 3b 0d 0a 53 51 4c  Int(int h);..SQL
88600 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
88610 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
88620 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65  me(Parse *, Toke
88630 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f  n *, Token *, To
88640 6b 65 6e 20 2a 2a 29 3b 0d 0a 53 51 4c 49 54 45  ken **);..SQLITE
88650 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
88660 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53  har *sqlite3ErrS
88670 74 72 28 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  tr(int);..SQLITE
88680 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
88690 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 50  ite3ReadSchema(P
886a0 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0d 0a  arse *pParse);..
886b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
886c0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46  ollSeq *sqlite3F
886d0 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74  indCollSeq(sqlit
886e0 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73  e3*,u8 enc, cons
886f0 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0d 0a 53  t char*,int);..S
88700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
88710 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f  llSeq *sqlite3Lo
88720 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  cateCollSeq(Pars
88730 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
88740 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0d 0a 53   char*zName);..S
88750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
88760 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78  llSeq *sqlite3Ex
88770 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  prCollSeq(Parse 
88780 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
88790 45 78 70 72 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  Expr);..SQLITE_P
887a0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
887b0 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
887c0 45 78 70 72 2a 2c 20 43 6f 6c 6c 53 65 71 2a 29  Expr*, CollSeq*)
887d0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
887e0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
887f0 78 70 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b 65  xprSetCollByToke
88800 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
88810 20 45 78 70 72 2a 2c 20 54 6f 6b 65 6e 2a 29 3b   Expr*, Token*);
88820 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
88830 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63   int sqlite3Chec
88840 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  kCollSeq(Parse *
88850 2c 20 43 6f 6c 6c 53 65 71 20 2a 29 3b 0d 0a 53  , CollSeq *);..S
88860 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
88870 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  t sqlite3CheckOb
88880 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a  jectName(Parse *
88890 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
888a0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
888b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
888c0 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69  eSetChanges(sqli
888d0 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51  te3 *, int);..SQ
888e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
888f0 20 73 71 6c 69 74 65 33 41 64 64 49 6e 74 36 34   sqlite3AddInt64
88900 28 69 36 34 2a 2c 69 36 34 29 3b 0d 0a 53 51 4c  (i64*,i64);..SQL
88910 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
88920 73 71 6c 69 74 65 33 53 75 62 49 6e 74 36 34 28  sqlite3SubInt64(
88930 69 36 34 2a 2c 69 36 34 29 3b 0d 0a 53 51 4c 49  i64*,i64);..SQLI
88940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
88950 71 6c 69 74 65 33 4d 75 6c 49 6e 74 36 34 28 69  qlite3MulInt64(i
88960 36 34 2a 2c 69 36 34 29 3b 0d 0a 53 51 4c 49 54  64*,i64);..SQLIT
88970 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
88980 6c 69 74 65 33 41 62 73 49 6e 74 33 32 28 69 6e  lite3AbsInt32(in
88990 74 29 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  t);..#ifdef SQLI
889a0 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41  TE_ENABLE_8_3_NA
889b0 4d 45 53 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  MES..SQLITE_PRIV
889c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
889d0 46 69 6c 65 53 75 66 66 69 78 33 28 63 6f 6e 73  FileSuffix3(cons
889e0 74 20 63 68 61 72 2a 2c 20 63 68 61 72 2a 29 3b  t char*, char*);
889f0 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
88a00 65 20 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66  e sqlite3FileSuf
88a10 66 69 78 33 28 58 2c 59 29 0d 0a 23 65 6e 64 69  fix3(X,Y)..#endi
88a20 66 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
88a30 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 42  E u8 sqlite3GetB
88a40 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61  oolean(const cha
88a50 72 20 2a 7a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45  r *z);....SQLITE
88a60 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
88a70 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
88a80 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
88a90 6c 75 65 2a 2c 20 75 38 29 3b 0d 0a 53 51 4c 49  lue*, u8);..SQLI
88aa0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
88ab0 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73  qlite3ValueBytes
88ac0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c  (sqlite3_value*,
88ad0 20 75 38 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52   u8);..SQLITE_PR
88ae0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
88af0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 73 71  e3ValueSetStr(sq
88b00 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e  lite3_value*, in
88b10 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c  t, const void *,
88b20 75 38 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  u8, ..          
88b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f                vo
88b40 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a  id(*)(void*));..
88b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
88b60 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
88b70 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  Free(sqlite3_val
88b80 75 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  ue*);..SQLITE_PR
88b90 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61  IVATE sqlite3_va
88ba0 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  lue *sqlite3Valu
88bb0 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b  eNew(sqlite3 *);
88bc0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
88bd0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74   char *sqlite3Ut
88be0 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a  f16to8(sqlite3 *
88bf0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
88c00 6e 74 2c 20 75 38 29 3b 0d 0a 23 69 66 64 65 66  nt, u8);..#ifdef
88c10 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
88c20 54 41 54 33 0d 0a 53 51 4c 49 54 45 5f 50 52 49  TAT3..SQLITE_PRI
88c30 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
88c40 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74  e3Utf8to16(sqlit
88c50 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a  e3 *, u8, char *
88c60 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0d 0a  , int, int *);..
88c70 23 65 6e 64 69 66 0d 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
88c80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
88c90 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
88ca0 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20  sqlite3 *, Expr 
88cb0 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74  *, u8, u8, sqlit
88cc0 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0d 0a 53  e3_value **);..S
88cd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
88ce0 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41  id sqlite3ValueA
88cf0 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c  pplyAffinity(sql
88d00 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38  ite3_value *, u8
88d10 2c 20 75 38 29 3b 0d 0a 23 69 66 6e 64 65 66 20  , u8);..#ifndef 
88d20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
88d30 49 4f 4e 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ION..SQLITE_PRIV
88d40 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
88d50 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 4f  ed char sqlite3O
88d60 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 3b  pcodeProperty[];
88d70 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
88d80 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
88d90 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65  char sqlite3Uppe
88da0 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0d 0a 53 51 4c  rToLower[];..SQL
88db0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
88dc0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
88dd0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
88de0 5d 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ];..SQLITE_PRIVA
88df0 54 45 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 73  TE const Token s
88e00 71 6c 69 74 65 33 49 6e 74 54 6f 6b 65 6e 73 5b  qlite3IntTokens[
88e10 5d 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ];..SQLITE_PRIVA
88e20 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  TE SQLITE_WSD st
88e30 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
88e40 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67  ig sqlite3Config
88e50 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
88e60 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  E SQLITE_WSD Fun
88e70 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33  cDefHash sqlite3
88e80 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b  GlobalFunctions;
88e90 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
88ea0 5f 4f 4d 49 54 5f 57 53 44 0d 0a 53 51 4c 49 54  _OMIT_WSD..SQLIT
88eb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
88ec0 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
88ed0 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 65 6e 64 69  ;..#endif..#endi
88ee0 66 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
88ef0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
88f00 6f 74 50 61 67 65 4d 6f 76 65 64 28 73 71 6c 69  otPageMoved(sqli
88f10 74 65 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  te3*, int, int, 
88f20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
88f30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
88f40 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 2a  e3Reindex(Parse*
88f50 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
88f60 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
88f70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
88f80 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 76 6f  lterFunctions(vo
88f90 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  id);..SQLITE_PRI
88fa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
88fb0 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c  3AlterRenameTabl
88fc0 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  e(Parse*, SrcLis
88fd0 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53 51  t*, Token*);..SQ
88fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
88ff0 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
89000 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
89010 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0d  char *, int *);.
89020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
89030 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74  void sqlite3Nest
89040 65 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20  edParse(Parse*, 
89050 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
89060 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
89070 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
89080 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61  xpirePreparedSta
89090 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a  tements(sqlite3*
890a0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
890b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f  TE int sqlite3Co
890c0 64 65 53 75 62 73 65 6c 65 63 74 28 50 61 72 73  deSubselect(Pars
890d0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74  e *, Expr *, int
890e0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
890f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
89100 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 50  ite3SelectPrep(P
89110 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  arse*, Select*, 
89120 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0d 0a  NameContext*);..
89130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
89140 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  nt sqlite3Resolv
89150 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43  eExprNames(NameC
89160 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b  ontext*, Expr*);
89170 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
89180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73   void sqlite3Res
89190 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28  olveSelectNames(
891a0 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c  Parse*, Select*,
891b0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0d   NameContext*);.
891c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
891d0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  int sqlite3Resol
891e0 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50  veOrderGroupBy(P
891f0 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  arse*, Select*, 
89200 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74  ExprList*, const
89210 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45   char*);..SQLITE
89220 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
89230 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
89240 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65  lt(Vdbe *, Table
89250 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a   *, int, int);..
89260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89270 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
89280 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
89290 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Parse *, Token *
892a0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
892b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
892c0 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75  lterBeginAddColu
892d0 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c  mn(Parse *, SrcL
892e0 69 73 74 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  ist *);..SQLITE_
892f0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
89300 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53  *sqlite3GetCollS
89310 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c  eq(sqlite3*, u8,
89320 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73   CollSeq *, cons
89330 74 20 63 68 61 72 2a 29 3b 0d 0a 53 51 4c 49 54  t char*);..SQLIT
89340 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73  E_PRIVATE char s
89350 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
89360 70 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  pe(const char*);
89370 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
89380 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61   void sqlite3Ana
89390 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  lyze(Parse*, Tok
893a0 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a 53  en*, Token*);..S
893b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
893c0 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42  t sqlite3InvokeB
893d0 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48  usyHandler(BusyH
893e0 61 6e 64 6c 65 72 2a 29 3b 0d 0a 53 51 4c 49 54  andler*);..SQLIT
893f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
89400 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69  lite3FindDb(sqli
89410 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0d 0a  te3*, Token*);..
89420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
89430 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62  nt sqlite3FindDb
89440 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c 20  Name(sqlite3 *, 
89450 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0d 0a  const char *);..
89460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
89470 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
89480 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c  isLoad(sqlite3*,
89490 69 6e 74 20 69 44 42 29 3b 0d 0a 53 51 4c 49 54  int iDB);..SQLIT
894a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
894b0 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65  qlite3DeleteInde
894c0 78 53 61 6d 70 6c 65 73 28 73 71 6c 69 74 65 33  xSamples(sqlite3
894d0 2a 2c 49 6e 64 65 78 2a 29 3b 0d 0a 53 51 4c 49  *,Index*);..SQLI
894e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
894f0 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
89500 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0d 0a 53  wEst(Index*);..S
89510 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
89520 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
89530 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28  erLikeFunctions(
89540 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0d  sqlite3*, int);.
89550 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
89560 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b  int sqlite3IsLik
89570 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  eFunction(sqlite
89580 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68  3*,Expr*,int*,ch
89590 61 72 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  ar*);..SQLITE_PR
895a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
895b0 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72  e3MinimumFileFor
895c0 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  mat(Parse*, int,
895d0 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
895e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
895f0 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 76  te3SchemaClear(v
89600 6f 69 64 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  oid *);..SQLITE_
89610 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a  PRIVATE Schema *
89620 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
89630 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65  (sqlite3 *, Btre
89640 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
89650 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
89660 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73  3SchemaToIndex(s
89670 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65  qlite3 *db, Sche
89680 6d 61 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ma *);..SQLITE_P
89690 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a  RIVATE KeyInfo *
896a0 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
896b0 6e 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e 64  nfo(Parse *, Ind
896c0 65 78 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ex *);..SQLITE_P
896d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
896e0 65 33 43 72 65 61 74 65 46 75 6e 63 28 73 71 6c  e3CreateFunc(sql
896f0 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68  ite3 *, const ch
89700 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ar *, int, int, 
89710 76 6f 69 64 20 2a 2c 20 0d 0a 20 20 76 6f 69 64  void *, ..  void
89720 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e   (*)(sqlite3_con
89730 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
89740 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0d 0a 20 20  3_value **),..  
89750 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33  void (*)(sqlite3
89760 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
89770 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c  lite3_value **),
89780 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65   void (*)(sqlite
89790 33 5f 63 6f 6e 74 65 78 74 2a 29 2c 0d 0a 20 20  3_context*),..  
897a0 46 75 6e 63 44 65 73 74 72 75 63 74 6f 72 20 2a  FuncDestructor *
897b0 70 44 65 73 74 72 75 63 74 6f 72 0d 0a 29 3b 0d  pDestructor..);.
897c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
897d0 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78  int sqlite3ApiEx
897e0 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  it(sqlite3 *db, 
897f0 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
89800 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
89810 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
89820 65 28 50 61 72 73 65 20 2a 29 3b 0d 0a 0d 0a 53  e(Parse *);....S
89830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
89840 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
89850 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a  umInit(StrAccum*
89860 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e  , char*, int, in
89870 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
89880 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
89890 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53  StrAccumAppend(S
898a0 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63  trAccum*,const c
898b0 68 61 72 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c 49  har*,int);..SQLI
898c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
898d0 73 71 6c 69 74 65 33 41 70 70 65 6e 64 53 70 61  sqlite3AppendSpa
898e0 63 65 28 53 74 72 41 63 63 75 6d 2a 2c 69 6e 74  ce(StrAccum*,int
898f0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
89900 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
89910 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53  StrAccumFinish(S
89920 74 72 41 63 63 75 6d 2a 29 3b 0d 0a 53 51 4c 49  trAccum*);..SQLI
89930 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
89940 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
89950 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b  eset(StrAccum*);
89960 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
89970 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
89980 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65  ectDestInit(Sele
89990 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29  ctDest*,int,int)
899a0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
899b0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43  E Expr *sqlite3C
899c0 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28  reateColumnExpr(
899d0 73 71 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c 69  sqlite3 *, SrcLi
899e0 73 74 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  st *, int, int);
899f0 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ....SQLITE_PRIVA
89a00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
89a10 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c  ackupRestart(sql
89a20 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0d  ite3_backup *);.
89a30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
89a40 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b  void sqlite3Back
89a50 75 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 33  upUpdate(sqlite3
89a60 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c  _backup *, Pgno,
89a70 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b 0d 0a 0d   const u8 *);...
89a80 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ./*..** The inte
89a90 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d  rface to the LEM
89aa0 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72  ON-generated par
89ab0 73 65 72 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ser..*/..SQLITE_
89ac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
89ad0 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
89ae0 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74  (void*(*)(size_t
89af0 29 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ));..SQLITE_PRIV
89b00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
89b10 50 61 72 73 65 72 46 72 65 65 28 76 6f 69 64 2a  ParserFree(void*
89b20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
89b30 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
89b40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
89b50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e 74  arser(void*, int
89b60 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a 29  , Token, Parse*)
89b70 3b 0d 0a 23 69 66 64 65 66 20 59 59 54 52 41 43  ;..#ifdef YYTRAC
89b80 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0d 0a  KMAXSTACKDEPTH..
89b90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
89ba0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73   int sqlite3Pars
89bb0 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64  erStackPeak(void
89bc0 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 53  *);..#endif....S
89bd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
89be0 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f  id sqlite3AutoLo
89bf0 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c  adExtensions(sql
89c00 69 74 65 33 2a 29 3b 0d 0a 23 69 66 6e 64 65 66  ite3*);..#ifndef
89c10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
89c20 44 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a 53 51 4c  D_EXTENSION..SQL
89c30 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
89c40 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  id sqlite3CloseE
89c50 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
89c60 33 2a 29 3b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64  3*);..#else..# d
89c70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f  efine sqlite3Clo
89c80 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0d  seExtensions(X).
89c90 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64  .#endif....#ifnd
89ca0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
89cb0 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 53 51 4c  HARED_CACHE..SQL
89cc0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
89cd0 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  id sqlite3TableL
89ce0 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74  ock(Parse *, int
89cf0 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74  , int, u8, const
89d00 20 63 68 61 72 20 2a 29 3b 0d 0a 23 65 6c 73 65   char *);..#else
89d10 0d 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ..  #define sqli
89d20 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77  te3TableLock(v,w
89d30 2c 78 2c 79 2c 7a 29 0d 0a 23 65 6e 64 69 66 0d  ,x,y,z)..#endif.
89d40 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
89d50 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 50 52  _TEST..SQLITE_PR
89d60 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
89d70 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67  te3Utf8To8(unsig
89d80 6e 65 64 20 63 68 61 72 2a 29 3b 0d 0a 23 65 6e  ned char*);..#en
89d90 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51  dif....#ifdef SQ
89da0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
89db0 4c 54 41 42 4c 45 0d 0a 23 20 20 64 65 66 69 6e  LTABLE..#  defin
89dc0 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  e sqlite3VtabCle
89dd0 61 72 28 59 29 0d 0a 23 20 20 64 65 66 69 6e 65  ar(Y)..#  define
89de0 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
89df0 28 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0d  (X,Y) SQLITE_OK.
89e00 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
89e10 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58  e3VtabRollback(X
89e20 29 0d 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  )..#  define sql
89e30 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 58  ite3VtabCommit(X
89e40 29 0d 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  )..#  define sql
89e50 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64  ite3VtabInSync(d
89e60 62 29 20 30 0d 0a 23 20 20 64 65 66 69 6e 65 20  b) 0..#  define 
89e70 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
89e80 58 29 20 0d 0a 23 20 20 64 65 66 69 6e 65 20 73  X) ..#  define s
89e90 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
89ea0 28 58 29 0d 0a 23 20 20 64 65 66 69 6e 65 20 73  (X)..#  define s
89eb0 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
89ec0 4c 69 73 74 28 58 29 0d 0a 23 20 20 64 65 66 69  List(X)..#  defi
89ed0 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 53 61  ne sqlite3VtabSa
89ee0 76 65 70 6f 69 6e 74 28 58 2c 20 59 2c 20 5a 29  vepoint(X, Y, Z)
89ef0 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 23 20 20 64   SQLITE_OK..#  d
89f00 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 65 74  efine sqlite3Get
89f10 56 54 61 62 6c 65 28 58 2c 59 29 20 20 28 28 56  VTable(X,Y)  ((V
89f20 54 61 62 6c 65 2a 29 30 29 0d 0a 23 65 6c 73 65  Table*)0)..#else
89f30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
89f40 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33      void sqlite3
89f50 56 74 61 62 43 6c 65 61 72 28 73 71 6c 69 74 65  VtabClear(sqlite
89f60 33 20 2a 64 62 2c 20 54 61 62 6c 65 2a 29 3b 0d  3 *db, Table*);.
89f70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
89f80 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74     int sqlite3Vt
89f90 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a  abSync(sqlite3 *
89fa0 64 62 2c 20 63 68 61 72 20 2a 2a 29 3b 0d 0a 53  db, char **);..S
89fb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
89fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
89fd0 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33  Rollback(sqlite3
89fe0 20 2a 64 62 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   *db);..SQLITE_P
89ff0 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71  RIVATE    int sq
8a000 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28  lite3VtabCommit(
8a010 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0d 0a 53  sqlite3 *db);..S
8a020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
8a030 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
8a040 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b  bLock(VTable *);
8a050 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8a060 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33      void sqlite3
8a070 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c  VtabUnlock(VTabl
8a080 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
8a090 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71  IVATE    void sq
8a0a0 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c  lite3VtabUnlockL
8a0b0 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a  ist(sqlite3*);..
8a0c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
8a0d0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61    int sqlite3Vta
8a0e0 62 53 61 76 65 70 6f 69 6e 74 28 73 71 6c 69 74  bSavepoint(sqlit
8a0f0 65 33 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  e3 *, int, int);
8a100 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8a110 20 20 20 20 56 54 61 62 6c 65 20 2a 73 71 6c 69      VTable *sqli
8a120 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c  te3GetVTable(sql
8a130 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0d  ite3*, Table*);.
8a140 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
8a150 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
8a160 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e   ((db)->nVTrans>
8a170 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61  0 && (db)->aVTra
8a180 6e 73 3d 3d 30 29 0d 0a 23 65 6e 64 69 66 0d 0a  ns==0)..#endif..
8a190 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8a1a0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d  oid sqlite3VtabM
8a1b0 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73  akeWritable(Pars
8a1c0 65 2a 2c 54 61 62 6c 65 2a 29 3b 0d 0a 53 51 4c  e*,Table*);..SQL
8a1d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
8a1e0 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
8a1f0 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54  nParse(Parse*, T
8a200 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  oken*, Token*, T
8a210 6f 6b 65 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  oken*);..SQLITE_
8a220 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
8a230 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
8a240 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  rse(Parse*, Toke
8a250 6e 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  n*);..SQLITE_PRI
8a260 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
8a270 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72  3VtabArgInit(Par
8a280 73 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  se*);..SQLITE_PR
8a290 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
8a2a0 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
8a2b0 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Parse*, Token*);
8a2c0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8a2d0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
8a2e0 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74  CallCreate(sqlit
8a2f0 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  e3*, int, const 
8a300 63 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29  char *, char **)
8a310 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8a320 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
8a330 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72  bCallConnect(Par
8a340 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0d 0a 53  se*, Table*);..S
8a350 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
8a360 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  t sqlite3VtabCal
8a370 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  lDestroy(sqlite3
8a380 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
8a390 61 72 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  ar *);..SQLITE_P
8a3a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
8a3b0 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69  e3VtabBegin(sqli
8a3c0 74 65 33 20 2a 2c 20 56 54 61 62 6c 65 20 2a 29  te3 *, VTable *)
8a3d0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8a3e0 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74  E FuncDef *sqlit
8a3f0 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75  e3VtabOverloadFu
8a400 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a  nction(sqlite3 *
8a410 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e  ,FuncDef*, int n
8a420 41 72 67 2c 20 45 78 70 72 2a 29 3b 0d 0a 53 51  Arg, Expr*);..SQ
8a430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8a440 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64  d sqlite3Invalid
8a450 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
8a460 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
8a470 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0d  lite3_value**);.
8a480 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8a490 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 50  int sqlite3VdbeP
8a4a0 61 72 61 6d 65 74 65 72 49 6e 64 65 78 28 56 64  arameterIndex(Vd
8a4b0 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
8a4c0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
8a4d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
8a4e0 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69  te3TransferBindi
8a4f0 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
8a500 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74   *, sqlite3_stmt
8a510 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49   *);..SQLITE_PRI
8a520 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
8a530 52 65 70 72 65 70 61 72 65 28 56 64 62 65 2a 29  Reprepare(Vdbe*)
8a540 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8a550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
8a560 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
8a570 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69  h(Parse*, ExprLi
8a580 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  st*, const char*
8a590 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
8a5a0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
8a5b0 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
8a5c0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c  CollSeq(Parse *,
8a5d0 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 29   Expr *, Expr *)
8a5e0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8a5f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d  E int sqlite3Tem
8a600 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20  pInMemory(const 
8a610 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 53 51 4c 49  sqlite3*);..SQLI
8a620 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
8a630 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4a 6f   char *sqlite3Jo
8a640 75 72 6e 61 6c 4d 6f 64 65 6e 61 6d 65 28 69 6e  urnalModename(in
8a650 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
8a660 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
8a670 68 65 63 6b 70 6f 69 6e 74 28 73 71 6c 69 74 65  heckpoint(sqlite
8a680 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  3*, int, int, in
8a690 74 2a 2c 20 69 6e 74 2a 29 3b 0d 0a 53 51 4c 49  t*, int*);..SQLI
8a6a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
8a6b0 71 6c 69 74 65 33 57 61 6c 44 65 66 61 75 6c 74  qlite3WalDefault
8a6c0 48 6f 6f 6b 28 76 6f 69 64 2a 2c 73 71 6c 69 74  Hook(void*,sqlit
8a6d0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
8a6e0 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 20 44 65 63 6c  int);..../* Decl
8a6f0 61 72 61 74 69 6f 6e 73 20 66 6f 72 20 66 75 6e  arations for fun
8a700 63 74 69 6f 6e 73 20 69 6e 20 66 6b 65 79 2e 63  ctions in fkey.c
8a710 2e 20 41 6c 6c 20 6f 66 20 74 68 65 73 65 20 61  . All of these a
8a720 72 65 20 72 65 70 6c 61 63 65 64 20 62 79 0d 0a  re replaced by..
8a730 2a 2a 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20  ** no-op macros 
8a740 69 66 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  if OMIT_FOREIGN_
8a750 4b 45 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20  KEY is defined. 
8a760 49 6e 20 74 68 69 73 20 63 61 73 65 20 6e 6f 20  In this case no 
8a770 66 6f 72 65 69 67 6e 0d 0a 2a 2a 20 6b 65 79 20  foreign..** key 
8a780 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73  functionality is
8a790 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 66 20 4f   available. If O
8a7a0 4d 49 54 5f 54 52 49 47 47 45 52 20 69 73 20 64  MIT_TRIGGER is d
8a7b0 65 66 69 6e 65 64 20 62 75 74 0d 0a 2a 2a 20 4f  efined but..** O
8a7c0 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20  MIT_FOREIGN_KEY 
8a7d0 69 73 20 6e 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d  is not, only som
8a7e0 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
8a7f0 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 65 64 2e 20  ns are no-oped. 
8a800 49 6e 0d 0a 2a 2a 20 74 68 69 73 20 63 61 73 65  In..** this case
8a810 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 72   foreign keys ar
8a820 65 20 70 61 72 73 65 64 2c 20 62 75 74 20 6e 6f  e parsed, but no
8a830 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 61   other functiona
8a840 6c 69 74 79 20 69 73 20 0d 0a 2a 2a 20 70 72 6f  lity is ..** pro
8a850 76 69 64 65 64 20 28 65 6e 66 6f 72 63 65 6d 65  vided (enforceme
8a860 6e 74 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61  nt of FK constra
8a870 69 6e 74 73 20 72 65 71 75 69 72 65 73 20 74 68  ints requires th
8a880 65 20 74 72 69 67 67 65 72 73 20 73 75 62 2d 73  e triggers sub-s
8a890 79 73 74 65 6d 29 2e 0d 0a 2a 2f 0d 0a 23 69 66  ystem)...*/..#if
8a8a0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
8a8b0 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
8a8c0 59 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  Y) && !defined(S
8a8d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
8a8e0 45 52 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ER)..SQLITE_PRIV
8a8f0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
8a900 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73 65 2a  e3FkCheck(Parse*
8a910 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
8a920 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
8a930 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
8a940 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 50  te3FkDropTable(P
8a950 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20 2a  arse*, SrcList *
8a960 2c 20 54 61 62 6c 65 2a 29 3b 0d 0a 53 51 4c 49  , Table*);..SQLI
8a970 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
8a980 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f  d sqlite3FkActio
8a990 6e 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  ns(Parse*, Table
8a9a0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
8a9b0 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
8a9c0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
8a9d0 33 46 6b 52 65 71 75 69 72 65 64 28 50 61 72 73  3FkRequired(Pars
8a9e0 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a  e*, Table*, int*
8a9f0 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
8aa00 50 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71  PRIVATE   u32 sq
8aa10 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50  lite3FkOldmask(P
8aa20 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0d  arse*, Table*);.
8aa30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8aa40 20 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 33 46    FKey *sqlite3F
8aa50 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 62 6c  kReferences(Tabl
8aa60 65 20 2a 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20  e *);..#else..  
8aa70 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46  #define sqlite3F
8aa80 6b 41 63 74 69 6f 6e 73 28 61 2c 62 2c 63 2c 64  kActions(a,b,c,d
8aa90 29 0d 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  )..  #define sql
8aaa0 69 74 65 33 46 6b 43 68 65 63 6b 28 61 2c 62 2c  ite3FkCheck(a,b,
8aab0 63 2c 64 29 0d 0a 20 20 23 64 65 66 69 6e 65 20  c,d)..  #define 
8aac0 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62  sqlite3FkDropTab
8aad0 6c 65 28 61 2c 62 2c 63 29 0d 0a 20 20 23 64 65  le(a,b,c)..  #de
8aae0 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 4f 6c  fine sqlite3FkOl
8aaf0 64 6d 61 73 6b 28 61 2c 62 29 20 20 20 20 20 20  dmask(a,b)      
8ab00 30 0d 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  0..  #define sql
8ab10 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 61  ite3FkRequired(a
8ab20 2c 62 2c 63 2c 64 29 20 30 0d 0a 23 65 6e 64 69  ,b,c,d) 0..#endi
8ab30 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
8ab40 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
8ab50 45 59 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EY..SQLITE_PRIVA
8ab60 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
8ab70 33 46 6b 44 65 6c 65 74 65 28 73 71 6c 69 74 65  3FkDelete(sqlite
8ab80 33 20 2a 2c 20 54 61 62 6c 65 2a 29 3b 0d 0a 23  3 *, Table*);..#
8ab90 65 6c 73 65 0d 0a 20 20 23 64 65 66 69 6e 65 20  else..  #define 
8aba0 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28  sqlite3FkDelete(
8abb0 61 2c 62 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  a,b)..#endif....
8abc0 0d 0a 2f 2a 0d 0a 2a 2a 20 41 76 61 69 6c 61 62  ../*..** Availab
8abd0 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f  le fault injecto
8abe0 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e  rs.  Should be n
8abf0 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e  umbered beginnin
8ac00 67 20 77 69 74 68 20 30 2e 0d 0a 2a 2f 0d 0a 23  g with 0...*/..#
8ac10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41  define SQLITE_FA
8ac20 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c  ULTINJECTOR_MALL
8ac30 4f 43 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e  OC     0..#defin
8ac40 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e  e SQLITE_FAULTIN
8ac50 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20  JECTOR_COUNT    
8ac60 20 20 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68    1..../*..** Th
8ac70 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
8ac80 68 65 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74  he code in fault
8ac90 2e 63 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e  .c used for iden
8aca0 74 69 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22  tifying "benign"
8acb0 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  ..** malloc fail
8acc0 75 72 65 73 2e 20 54 68 69 73 20 69 73 20 6f 6e  ures. This is on
8acd0 6c 79 20 70 72 65 73 65 6e 74 20 69 66 20 53 51  ly present if SQ
8ace0 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
8acf0 4e 5f 54 45 53 54 0d 0a 2a 2a 20 69 73 20 6e 6f  N_TEST..** is no
8ad00 74 20 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a  t defined...*/..
8ad10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
8ad20 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
8ad30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8ad40 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
8ad50 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
8ad60 28 76 6f 69 64 29 3b 0d 0a 53 51 4c 49 54 45 5f  (void);..SQLITE_
8ad70 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
8ad80 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
8ad90 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0d 0a 23 65  alloc(void);..#e
8ada0 6c 73 65 0d 0a 20 20 23 64 65 66 69 6e 65 20 73  lse..  #define s
8adb0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
8adc0 6e 4d 61 6c 6c 6f 63 28 29 0d 0a 20 20 23 64 65  nMalloc()..  #de
8add0 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42  fine sqlite3EndB
8ade0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0d 0a 23  enignMalloc()..#
8adf0 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65  endif....#define
8ae00 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20   IN_INDEX_ROWID 
8ae10 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23 64 65            1..#de
8ae20 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50  fine IN_INDEX_EP
8ae30 48 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0d  H             2.
8ae40 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45  .#define IN_INDE
8ae50 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  X_INDEX         
8ae60 20 20 33 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56    3..SQLITE_PRIV
8ae70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
8ae80 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65  indInIndex(Parse
8ae90 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a   *, Expr *, int*
8aea0 29 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  );....#ifdef SQL
8aeb0 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
8aec0 43 5f 57 52 49 54 45 0d 0a 53 51 4c 49 54 45 5f  C_WRITE..SQLITE_
8aed0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
8aee0 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e  lite3JournalOpen
8aef0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
8af00 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71  const char *, sq
8af10 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69 6e  lite3_file *, in
8af20 74 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  t, int);..SQLITE
8af30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
8af40 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
8af50 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29  e(sqlite3_vfs *)
8af60 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8af70 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
8af80 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c  ournalCreate(sql
8af90 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0d 0a 23  ite3_file *);..#
8afa0 65 6c 73 65 0d 0a 20 20 23 64 65 66 69 6e 65 20  else..  #define 
8afb0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
8afc0 7a 65 28 70 56 66 73 29 20 28 28 70 56 66 73 29  ze(pVfs) ((pVfs)
8afd0 2d 3e 73 7a 4f 73 46 69 6c 65 29 0d 0a 23 65 6e  ->szOsFile)..#en
8afe0 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  dif....SQLITE_PR
8aff0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
8b000 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
8b010 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
8b020 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8b030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
8b040 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64  JournalSize(void
8b050 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
8b060 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
8b070 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74  MemJournal(sqlit
8b080 65 33 5f 66 69 6c 65 20 2a 29 3b 0d 0a 0d 0a 23  e3_file *);....#
8b090 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  if SQLITE_MAX_EX
8b0a0 50 52 5f 44 45 50 54 48 3e 30 0d 0a 53 51 4c 49  PR_DEPTH>0..SQLI
8b0b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
8b0c0 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74  d sqlite3ExprSet
8b0d0 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50  Height(Parse *pP
8b0e0 61 72 73 65 2c 20 45 78 70 72 20 2a 70 29 3b 0d  arse, Expr *p);.
8b0f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8b100 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c    int sqlite3Sel
8b110 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65  ectExprHeight(Se
8b120 6c 65 63 74 20 2a 29 3b 0d 0a 53 51 4c 49 54 45  lect *);..SQLITE
8b130 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
8b140 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48  qlite3ExprCheckH
8b150 65 69 67 68 74 28 50 61 72 73 65 2a 2c 20 69 6e  eight(Parse*, in
8b160 74 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 23 64  t);..#else..  #d
8b170 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70  efine sqlite3Exp
8b180 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 29 0d  rSetHeight(x,y).
8b190 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
8b1a0 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
8b1b0 68 74 28 78 29 20 30 0d 0a 20 20 23 64 65 66 69  ht(x) 0..  #defi
8b1c0 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  ne sqlite3ExprCh
8b1d0 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0d 0a  eckHeight(x,y)..
8b1e0 23 65 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45  #endif....SQLITE
8b1f0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
8b200 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e  ite3Get4byte(con
8b210 73 74 20 75 38 2a 29 3b 0d 0a 53 51 4c 49 54 45  st u8*);..SQLITE
8b220 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
8b230 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38  lite3Put4byte(u8
8b240 2a 2c 20 75 33 32 29 3b 0d 0a 0d 0a 23 69 66 64  *, u32);....#ifd
8b250 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8b260 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0d 0a  _UNLOCK_NOTIFY..
8b270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
8b280 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e   void sqlite3Con
8b290 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73  nectionBlocked(s
8b2a0 71 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65  qlite3 *, sqlite
8b2b0 33 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  3 *);..SQLITE_PR
8b2c0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
8b2d0 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e  ite3ConnectionUn
8b2e0 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
8b2f0 64 62 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  db);..SQLITE_PRI
8b300 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
8b310 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f  te3ConnectionClo
8b320 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  sed(sqlite3 *db)
8b330 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 23 64 65 66  ;..#else..  #def
8b340 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  ine sqlite3Conne
8b350 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 78 2c 79  ctionBlocked(x,y
8b360 29 0d 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  )..  #define sql
8b370 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e  ite3ConnectionUn
8b380 6c 6f 63 6b 65 64 28 78 29 0d 0a 20 20 23 64 65  locked(x)..  #de
8b390 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e  fine sqlite3Conn
8b3a0 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 78 29 0d  ectionClosed(x).
8b3b0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65  .#endif....#ifde
8b3c0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a  f SQLITE_DEBUG..
8b3d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
8b3e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
8b3f0 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20  serTrace(FILE*, 
8b400 63 68 61 72 20 2a 29 3b 0d 0a 23 65 6e 64 69 66  char *);..#endif
8b410 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  ..../*..** If th
8b420 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20  e SQLITE_ENABLE 
8b430 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74  IOTRACE exists t
8b440 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76  hen the global v
8b450 61 72 69 61 62 6c 65 0d 0a 2a 2a 20 73 71 6c 69  ariable..** sqli
8b460 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20  te3IoTrace is a 
8b470 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69  pointer to a pri
8b480 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65  ntf-like routine
8b490 20 75 73 65 64 20 74 6f 0d 0a 2a 2a 20 70 72 69   used to..** pri
8b4a0 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d  nt I/O tracing m
8b4b0 65 73 73 61 67 65 73 2e 20 0d 0a 2a 2f 0d 0a 23  essages. ..*/..#
8b4c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
8b4d0 42 4c 45 5f 49 4f 54 52 41 43 45 0d 0a 23 20 64  BLE_IOTRACE..# d
8b4e0 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29  efine IOTRACE(A)
8b4f0 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54    if( sqlite3IoT
8b500 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 49  race ){ sqlite3I
8b510 6f 54 72 61 63 65 20 41 3b 20 7d 0d 0a 53 51 4c  oTrace A; }..SQL
8b520 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
8b530 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f  id sqlite3VdbeIO
8b540 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b  TraceSql(Vdbe*);
8b550 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8b560 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49   void (*sqlite3I
8b570 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68  oTrace)(const ch
8b580 61 72 2a 2c 2e 2e 2e 29 3b 0d 0a 23 65 6c 73 65  ar*,...);..#else
8b590 0d 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41  ..# define IOTRA
8b5a0 43 45 28 41 29 0d 0a 23 20 64 65 66 69 6e 65 20  CE(A)..# define 
8b5b0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
8b5c0 63 65 53 71 6c 28 58 29 0d 0a 23 65 6e 64 69 66  ceSql(X)..#endif
8b5d0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65  ..../*..** These
8b5e0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 76   routines are av
8b5f0 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 20  ailable for the 
8b600 6d 65 6d 32 2e 63 20 64 65 62 75 67 67 69 6e 67  mem2.c debugging
8b610 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
8b620 72 0d 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65  r..** only.  The
8b630 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 76 65  y are used to ve
8b640 72 69 66 79 20 74 68 61 74 20 64 69 66 66 65 72  rify that differ
8b650 65 6e 74 20 22 74 79 70 65 73 22 20 6f 66 20 6d  ent "types" of m
8b660 65 6d 6f 72 79 0d 0a 2a 2a 20 61 6c 6c 6f 63 61  emory..** alloca
8b670 74 69 6f 6e 73 20 61 72 65 20 70 72 6f 70 65 72  tions are proper
8b680 6c 79 20 74 72 61 63 6b 65 64 20 62 79 20 74 68  ly tracked by th
8b690 65 20 73 79 73 74 65 6d 2e 0d 0a 2a 2a 0d 0a 2a  e system...**..*
8b6a0 2a 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  * sqlite3Memdebu
8b6b0 67 53 65 74 54 79 70 65 28 29 20 73 65 74 73 20  gSetType() sets 
8b6c0 74 68 65 20 22 74 79 70 65 22 20 6f 66 20 61 6e  the "type" of an
8b6d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6f   allocation to o
8b6e0 6e 65 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 4d 45  ne of..** the ME
8b6f0 4d 54 59 50 45 5f 2a 20 6d 61 63 72 6f 73 20 64  MTYPE_* macros d
8b700 65 66 69 6e 65 64 20 62 65 6c 6f 77 2e 20 20 54  efined below.  T
8b710 68 65 20 74 79 70 65 20 6d 75 73 74 20 62 65 20  he type must be 
8b720 61 20 62 69 74 6d 61 73 6b 20 77 69 74 68 0d 0a  a bitmask with..
8b730 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 69 74 20  ** a single bit 
8b740 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c  set...**..** sql
8b750 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
8b760 79 70 65 28 29 20 72 65 74 75 72 6e 73 20 74 72  ype() returns tr
8b770 75 65 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  ue if any of the
8b780 20 62 69 74 73 20 69 6e 20 69 74 73 20 73 65 63   bits in its sec
8b790 6f 6e 64 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ond..** argument
8b7a0 20 6d 61 74 63 68 20 74 68 65 20 74 79 70 65 20   match the type 
8b7b0 73 65 74 20 62 79 20 74 68 65 20 70 72 65 76 69  set by the previ
8b7c0 6f 75 73 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  ous sqlite3Memde
8b7d0 62 75 67 53 65 74 54 79 70 65 28 29 2e 0d 0a 2a  bugSetType()...*
8b7e0 2a 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  * sqlite3Memdebu
8b7f0 67 48 61 73 54 79 70 65 28 29 20 69 73 20 69 6e  gHasType() is in
8b800 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
8b810 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
8b820 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a  tatements...**..
8b830 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  ** sqlite3Memdeb
8b840 75 67 4e 6f 54 79 70 65 28 29 20 72 65 74 75 72  ugNoType() retur
8b850 6e 73 20 74 72 75 65 20 69 66 20 6e 6f 6e 65 20  ns true if none 
8b860 6f 66 20 74 68 65 20 62 69 74 73 20 69 6e 20 69  of the bits in i
8b870 74 73 20 73 65 63 6f 6e 64 0d 0a 2a 2a 20 61 72  ts second..** ar
8b880 67 75 6d 65 6e 74 20 6d 61 74 63 68 20 74 68 65  gument match the
8b890 20 74 79 70 65 20 73 65 74 20 62 79 20 74 68 65   type set by the
8b8a0 20 70 72 65 76 69 6f 75 73 20 73 71 6c 69 74 65   previous sqlite
8b8b0 33 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65  3MemdebugSetType
8b8c0 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 65 72 68  ()...**..** Perh
8b8d0 61 70 73 20 74 68 65 20 6d 6f 73 74 20 69 6d 70  aps the most imp
8b8e0 6f 72 74 61 6e 74 20 70 6f 69 6e 74 20 69 73 20  ortant point is 
8b8f0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  the difference b
8b900 65 74 77 65 65 6e 20 4d 45 4d 54 59 50 45 5f 48  etween MEMTYPE_H
8b910 45 41 50 0d 0a 2a 2a 20 61 6e 64 20 4d 45 4d 54  EAP..** and MEMT
8b920 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 2e 20 20  YPE_LOOKASIDE.  
8b930 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  If an allocation
8b940 20 69 73 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b   is MEMTYPE_LOOK
8b950 41 53 49 44 45 2c 20 74 68 61 74 20 6d 65 61 6e  ASIDE, that mean
8b960 73 0d 0a 2a 2a 20 69 74 20 6d 69 67 68 74 20 68  s..** it might h
8b970 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ave been allocat
8b980 65 64 20 62 79 20 6c 6f 6f 6b 61 73 69 64 65 2c  ed by lookaside,
8b990 20 65 78 63 65 70 74 20 74 68 65 20 61 6c 6c 6f   except the allo
8b9a0 63 61 74 69 6f 6e 20 77 61 73 0d 0a 2a 2a 20 74  cation was..** t
8b9b0 6f 6f 20 6c 61 72 67 65 20 6f 72 20 6c 6f 6f 6b  oo large or look
8b9c0 61 73 69 64 65 20 77 61 73 20 61 6c 72 65 61 64  aside was alread
8b9d0 79 20 66 75 6c 6c 2e 20 20 49 74 20 69 73 20 69  y full.  It is i
8b9e0 6d 70 6f 72 74 61 6e 74 20 74 6f 20 76 65 72 69  mportant to veri
8b9f0 66 79 0d 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 6f  fy..** that allo
8ba00 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67  cations that mig
8ba10 68 74 20 68 61 76 65 20 62 65 65 6e 20 73 61 74  ht have been sat
8ba20 69 73 66 69 65 64 20 62 79 20 6c 6f 6f 6b 61 73  isfied by lookas
8ba30 69 64 65 20 61 72 65 20 6e 6f 74 0d 0a 2a 2a 20  ide are not..** 
8ba40 70 61 73 73 65 64 20 62 61 63 6b 20 74 6f 20 6e  passed back to n
8ba50 6f 6e 2d 6c 6f 6f 6b 61 73 69 64 65 20 66 72 65  on-lookaside fre
8ba60 65 28 29 20 72 6f 75 74 69 6e 65 73 2e 20 20 41  e() routines.  A
8ba70 73 73 65 72 74 73 20 73 75 63 68 20 61 73 20 74  sserts such as t
8ba80 68 65 0d 0a 2a 2a 20 65 78 61 6d 70 6c 65 20 61  he..** example a
8ba90 62 6f 76 65 20 61 72 65 20 70 6c 61 63 65 64 20  bove are placed 
8baa0 6f 6e 20 74 68 65 20 6e 6f 6e 2d 6c 6f 6f 6b 61  on the non-looka
8bab0 73 69 64 65 20 66 72 65 65 28 29 20 72 6f 75 74  side free() rout
8bac0 69 6e 65 73 20 74 6f 20 76 65 72 69 66 79 0d 0a  ines to verify..
8bad0 2a 2a 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  ** this constrai
8bae0 6e 74 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c  nt. ..**..** All
8baf0 20 6f 66 20 74 68 69 73 20 69 73 20 6e 6f 2d 6f   of this is no-o
8bb00 70 20 66 6f 72 20 61 20 70 72 6f 64 75 63 74 69  p for a producti
8bb10 6f 6e 20 62 75 69 6c 64 2e 20 20 49 74 20 6f 6e  on build.  It on
8bb20 6c 79 20 63 6f 6d 65 73 20 69 6e 74 6f 0d 0a 2a  ly comes into..*
8bb30 2a 20 70 6c 61 79 20 77 68 65 6e 20 74 68 65 20  * play when the 
8bb40 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20  SQLITE_MEMDEBUG 
8bb50 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
8bb60 69 6f 6e 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2f  ion is used...*/
8bb70 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
8bb80 4d 45 4d 44 45 42 55 47 0d 0a 53 51 4c 49 54 45  MEMDEBUG..SQLITE
8bb90 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
8bba0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
8bbb0 65 74 54 79 70 65 28 76 6f 69 64 2a 2c 75 38 29  etType(void*,u8)
8bbc0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
8bbd0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
8bbe0 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28 76  emdebugHasType(v
8bbf0 6f 69 64 2a 2c 75 38 29 3b 0d 0a 53 51 4c 49 54  oid*,u8);..SQLIT
8bc00 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
8bc10 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4e  sqlite3MemdebugN
8bc20 6f 54 79 70 65 28 76 6f 69 64 2a 2c 75 38 29 3b  oType(void*,u8);
8bc30 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
8bc40 65 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  e sqlite3Memdebu
8bc50 67 53 65 74 54 79 70 65 28 58 2c 59 29 20 20 2f  gSetType(X,Y)  /
8bc60 2a 20 6e 6f 2d 6f 70 20 2a 2f 0d 0a 23 20 64 65  * no-op */..# de
8bc70 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 65 6d 64  fine sqlite3Memd
8bc80 65 62 75 67 48 61 73 54 79 70 65 28 58 2c 59 29  ebugHasType(X,Y)
8bc90 20 20 31 0d 0a 23 20 64 65 66 69 6e 65 20 73 71    1..# define sq
8bca0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54  lite3MemdebugNoT
8bcb0 79 70 65 28 58 2c 59 29 20 20 20 31 0d 0a 23 65  ype(X,Y)   1..#e
8bcc0 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65 20 4d 45  ndif..#define ME
8bcd0 4d 54 59 50 45 5f 48 45 41 50 20 20 20 20 20 20  MTYPE_HEAP      
8bce0 20 30 78 30 31 20 20 2f 2a 20 47 65 6e 65 72 61   0x01  /* Genera
8bcf0 6c 20 68 65 61 70 20 61 6c 6c 6f 63 61 74 69 6f  l heap allocatio
8bd00 6e 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d  ns */..#define M
8bd10 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45  EMTYPE_LOOKASIDE
8bd20 20 20 30 78 30 32 20 20 2f 2a 20 4d 69 67 68 74    0x02  /* Might
8bd30 20 68 61 76 65 20 62 65 65 6e 20 6c 6f 6f 6b 61   have been looka
8bd40 73 69 64 65 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a  side memory */..
8bd50 23 64 65 66 69 6e 65 20 4d 45 4d 54 59 50 45 5f  #define MEMTYPE_
8bd60 53 43 52 41 54 43 48 20 20 20 20 30 78 30 34 20  SCRATCH    0x04 
8bd70 20 2f 2a 20 53 63 72 61 74 63 68 20 61 6c 6c 6f   /* Scratch allo
8bd80 63 61 74 69 6f 6e 73 20 2a 2f 0d 0a 23 64 65 66  cations */..#def
8bd90 69 6e 65 20 4d 45 4d 54 59 50 45 5f 50 43 41 43  ine MEMTYPE_PCAC
8bda0 48 45 20 20 20 20 20 30 78 30 38 20 20 2f 2a 20  HE     0x08  /* 
8bdb0 50 61 67 65 20 63 61 63 68 65 20 61 6c 6c 6f 63  Page cache alloc
8bdc0 61 74 69 6f 6e 73 20 2a 2f 0d 0a 23 64 65 66 69  ations */..#defi
8bdd0 6e 65 20 4d 45 4d 54 59 50 45 5f 44 42 20 20 20  ne MEMTYPE_DB   
8bde0 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20 55        0x10  /* U
8bdf0 73 65 73 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  ses sqlite3DbMal
8be00 6c 6f 63 2c 20 6e 6f 74 20 73 71 6c 69 74 65 5f  loc, not sqlite_
8be10 6d 61 6c 6c 6f 63 20 2a 2f 0d 0a 0d 0a 23 65 6e  malloc */....#en
8be20 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 49 4e  dif /* _SQLITEIN
8be30 54 5f 48 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  T_H_ */..../****
8be40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
8be50 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
8be60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8be70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8be80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
8be90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
8bea0 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20  n file global.c 
8beb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
8bee0 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 33  .** 2008 June 13
8bef0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74  ..**..** The aut
8bf00 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
8bf10 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
8bf20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
8bf30 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20   place of..** a 
8bf40 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
8bf50 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
8bf60 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79  :..**..**    May
8bf70 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
8bf80 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20   not evil...**  
8bf90 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
8bfa0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
8bfb0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
8bfc0 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20  ive others...** 
8bfd0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
8bfe0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
8bff0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
8c000 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a  you give...**..*
8c010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c050 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
8c060 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
8c070 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20  ins definitions 
8c080 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
8c090 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73  les and contants
8c0a0 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 20 41 6e 20 61  ...*/..../* An a
8c0b0 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20  rray to map all 
8c0c0 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61  upper-case chara
8c0d0 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72  cters into their
8c0e0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0d 0a   corresponding..
8c0f0 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68  ** lower-case ch
8c100 61 72 61 63 74 65 72 2e 20 0d 0a 2a 2a 0d 0a 2a  aracter. ..**..*
8c110 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f  * SQLite only co
8c120 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 49  nsiders US-ASCII
8c130 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 61   (or EBCDIC) cha
8c140 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f 20  racters.  We do 
8c150 6e 6f 74 0d 0a 2a 2a 20 68 61 6e 64 6c 65 20 63  not..** handle c
8c160 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  ase conversions 
8c170 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 72  for the UTF char
8c180 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 20  acter set since 
8c190 74 68 65 20 74 61 62 6c 65 73 0d 0a 2a 2a 20 69  the tables..** i
8c1a0 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72  nvolved are near
8c1b0 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67  ly as big or big
8c1c0 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20  ger than SQLite 
8c1d0 69 74 73 65 6c 66 2e 0d 0a 2a 2f 0d 0a 65 78 74  itself...*/..ext
8c1e0 65 72 6e 20 75 6e 73 69 67 6e 65 64 20 73 68 6f  ern unsigned sho
8c1f0 72 74 20 6f 61 73 55 70 70 65 72 32 4c 6f 77 65  rt oasUpper2Lowe
8c200 72 5b 30 78 46 46 46 46 5d 3b 0d 0a 2f 2f 20 cd  r[0xFFFF];..// .
8c210 e5 20 e8 f1 ef ee eb fc e7 f3 e5 f2 f1 ff 0d 0a  . ..............
8c220 23 69 66 20 30 0d 0a 53 51 4c 49 54 45 5f 50 52  #if 0..SQLITE_PR
8c230 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69  IVATE const unsi
8c240 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65  gned char sqlite
8c250 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20  3UpperToLower[] 
8c260 3d 20 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  = {..#ifdef SQLI
8c270 54 45 5f 41 53 43 49 49 0d 0a 20 20 20 20 20 20  TE_ASCII..      
8c280 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20  0,  1,  2,  3,  
8c290 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20  4,  5,  6,  7,  
8c2a0 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31  8,  9, 10, 11, 1
8c2b0 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 31  2, 13, 14, 15, 1
8c2c0 36 2c 20 31 37 2c 0d 0a 20 20 20 20 20 31 38 2c  6, 17,..     18,
8c2d0 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c   19, 20, 21, 22,
8c2e0 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c   23, 24, 25, 26,
8c2f0 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c   27, 28, 29, 30,
8c300 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c   31, 32, 33, 34,
8c310 20 33 35 2c 0d 0a 20 20 20 20 20 33 36 2c 20 33   35,..     36, 3
8c320 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34  7, 38, 39, 40, 4
8c330 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34  1, 42, 43, 44, 4
8c340 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34  5, 46, 47, 48, 4
8c350 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35  9, 50, 51, 52, 5
8c360 33 2c 0d 0a 20 20 20 20 20 35 34 2c 20 35 35 2c  3,..     54, 55,
8c370 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c   56, 57, 58, 59,
8c380 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c   60, 61, 62, 63,
8c390 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c   64, 97, 98, 99,
8c3a0 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c  100,101,102,103,
8c3b0 0d 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30  ..    104,105,10
8c3c0 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31  6,107,108,109,11
8c3d0 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31  0,111,112,113,11
8c3e0 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31  4,115,116,117,11
8c3f0 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0d 0a  8,119,120,121,..
8c400 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c      122, 91, 92,
8c410 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c   93, 94, 95, 96,
8c420 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
8c430 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
8c440 31 30 35 2c 31 30 36 2c 31 30 37 2c 0d 0a 20 20  105,106,107,..  
8c450 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31    108,109,110,11
8c460 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31  1,112,113,114,11
8c470 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31  5,116,117,118,11
8c480 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32  9,120,121,122,12
8c490 33 2c 31 32 34 2c 31 32 35 2c 0d 0a 20 20 20 20  3,124,125,..    
8c4a0 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c  126,127,128,129,
8c4b0 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c  130,131,132,133,
8c4c0 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c  134,135,136,137,
8c4d0 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c  138,139,140,141,
8c4e0 31 34 32 2c 31 34 33 2c 0d 0a 20 20 20 20 31 34  142,143,..    14
8c4f0 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34  4,145,146,147,14
8c500 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35  8,149,150,151,15
8c510 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35  2,153,154,155,15
8c520 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36  6,157,158,159,16
8c530 30 2c 31 36 31 2c 0d 0a 20 20 20 20 31 36 32 2c  0,161,..    162,
8c540 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c  163,164,165,166,
8c550 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c  167,168,169,170,
8c560 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c  171,172,173,174,
8c570 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c  175,176,177,178,
8c580 31 37 39 2c 0d 0a 20 20 20 20 31 38 30 2c 31 38  179,..    180,18
8c590 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38  1,182,183,184,18
8c5a0 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38  5,186,187,188,18
8c5b0 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 39  9,190,191,192,19
8c5c0 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 39  3,194,195,196,19
8c5d0 37 2c 0d 0a 20 20 20 20 31 39 38 2c 31 39 39 2c  7,..    198,199,
8c5e0 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c  200,201,202,203,
8c5f0 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c  204,205,206,207,
8c600 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c  208,209,210,211,
8c610 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c  212,213,214,215,
8c620 0d 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31  ..    216,217,21
8c630 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32  8,219,220,221,22
8c640 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32  2,223,224,225,22
8c650 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33  6,227,228,229,23
8c660 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0d 0a  0,231,232,233,..
8c670 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c      234,235,236,
8c680 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c  237,238,239,240,
8c690 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c  241,242,243,244,
8c6a0 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c  245,246,247,248,
8c6b0 32 34 39 2c 32 35 30 2c 32 35 31 2c 0d 0a 20 20  249,250,251,..  
8c6c0 20 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35    252,253,254,25
8c6d0 35 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  5..#endif..#ifde
8c6e0 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0d  f SQLITE_EBCDIC.
8c6f0 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32  .      0,  1,  2
8c700 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36  ,  3,  4,  5,  6
8c710 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30  ,  7,  8,  9, 10
8c720 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34  , 11, 12, 13, 14
8c730 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0d 0a  , 15, /* 0x */..
8c740 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c       16, 17, 18,
8c750 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c   19, 20, 21, 22,
8c760 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c   23, 24, 25, 26,
8c770 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c   27, 28, 29, 30,
8c780 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0d 0a 20   31, /* 1x */.. 
8c790 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20      32, 33, 34, 
8c7a0 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20  35, 36, 37, 38, 
8c7b0 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20  39, 40, 41, 42, 
8c7c0 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20  43, 44, 45, 46, 
8c7d0 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0d 0a 20 20  47, /* 2x */..  
8c7e0 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35     48, 49, 50, 5
8c7f0 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35  1, 52, 53, 54, 5
8c800 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35  5, 56, 57, 58, 5
8c810 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36  9, 60, 61, 62, 6
8c820 33 2c 20 2f 2a 20 33 78 20 2a 2f 0d 0a 20 20 20  3, /* 3x */..   
8c830 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37    64, 65, 66, 67
8c840 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31  , 68, 69, 70, 71
8c850 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35  , 72, 73, 74, 75
8c860 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39  , 76, 77, 78, 79
8c870 2c 20 2f 2a 20 34 78 20 2a 2f 0d 0a 20 20 20 20  , /* 4x */..    
8c880 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c   80, 81, 82, 83,
8c890 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c   84, 85, 86, 87,
8c8a0 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c   88, 89, 90, 91,
8c8b0 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c   92, 93, 94, 95,
8c8c0 20 2f 2a 20 35 78 20 2a 2f 0d 0a 20 20 20 20 20   /* 5x */..     
8c8d0 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20  96, 97, 66, 67, 
8c8e0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20  68, 69, 70, 71, 
8c8f0 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31  72, 73,106,107,1
8c900 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20  08,109,110,111, 
8c910 2f 2a 20 36 78 20 2a 2f 0d 0a 20 20 20 20 31 31  /* 6x */..    11
8c920 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38  2, 81, 82, 83, 8
8c930 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38  4, 85, 86, 87, 8
8c940 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32  8, 89,122,123,12
8c950 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f  4,125,126,127, /
8c960 2a 20 37 78 20 2a 2f 0d 0a 20 20 20 20 31 32 38  * 7x */..    128
8c970 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32  ,129,130,131,132
8c980 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36  ,133,134,135,136
8c990 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30  ,137,138,139,140
8c9a0 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a  ,141,142,143, /*
8c9b0 20 38 78 20 2a 2f 0d 0a 20 20 20 20 31 34 34 2c   8x */..    144,
8c9c0 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c  145,146,147,148,
8c9d0 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c  149,150,151,152,
8c9e0 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c  153,154,155,156,
8c9f0 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20  157,156,159, /* 
8ca00 39 78 20 2a 2f 0d 0a 20 20 20 20 31 36 30 2c 31  9x */..    160,1
8ca10 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31  61,162,163,164,1
8ca20 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
8ca30 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31  69,170,171,140,1
8ca40 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41  41,142,175, /* A
8ca50 78 20 2a 2f 0d 0a 20 20 20 20 31 37 36 2c 31 37  x */..    176,17
8ca60 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38  7,178,179,180,18
8ca70 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38  1,182,183,184,18
8ca80 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38  5,186,187,188,18
8ca90 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78  9,190,191, /* Bx
8caa0 20 2a 2f 0d 0a 20 20 20 20 31 39 32 2c 31 32 39   */..    192,129
8cab0 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33  ,130,131,132,133
8cac0 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37  ,134,135,136,137
8cad0 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35  ,202,203,204,205
8cae0 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20  ,206,207, /* Cx 
8caf0 2a 2f 0d 0a 20 20 20 20 32 30 38 2c 31 34 35 2c  */..    208,145,
8cb00 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c  146,147,148,149,
8cb10 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c  150,151,152,153,
8cb20 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c  218,219,220,221,
8cb30 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a  222,223, /* Dx *
8cb40 2f 0d 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31  /..    224,225,1
8cb50 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31  62,163,164,165,1
8cb60 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32  66,167,168,169,2
8cb70 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  32,203,204,205,2
8cb80 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f  06,207, /* Ex */
8cb90 0d 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 34  ..    239,240,24
8cba0 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34  1,242,243,244,24
8cbb0 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34  5,246,247,248,24
8cbc0 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32  9,219,220,221,22
8cbd0 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0d  2,255, /* Fx */.
8cbe0 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 23 65 6e  .#endif..};..#en
8cbf0 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
8cc00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 35 36 20  e following 256 
8cc10 62 79 74 65 20 6c 6f 6f 6b 75 70 20 74 61 62 6c  byte lookup tabl
8cc20 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 75 70  e is used to sup
8cc30 70 6f 72 74 20 53 51 4c 69 74 65 73 20 62 75 69  port SQLites bui
8cc40 6c 74 2d 69 6e 0d 0a 2a 2a 20 65 71 75 69 76 61  lt-in..** equiva
8cc50 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 66 6f 6c  lents to the fol
8cc60 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 72 64 20  lowing standard 
8cc70 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e  library function
8cc80 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 69 73 73  s:..**..**   iss
8cc90 70 61 63 65 28 29 20 20 20 20 20 20 20 20 20 20  pace()          
8cca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
8ccb0 30 31 0d 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61  01..**   isalpha
8ccc0 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
8ccd0 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0d 0a            0x02..
8cce0 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20  **   isdigit()  
8ccf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8cd00 20 20 20 20 20 20 30 78 30 34 0d 0a 2a 2a 20 20        0x04..**  
8cd10 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20   isalnum()      
8cd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8cd30 20 20 30 78 30 36 0d 0a 2a 2a 20 20 20 69 73 78    0x06..**   isx
8cd40 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20  digit()         
8cd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
8cd60 30 38 0d 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72  08..**   toupper
8cd70 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
8cd80 20 20 20 20 20 20 20 20 20 20 30 78 32 30 0d 0a            0x20..
8cd90 2a 2a 20 20 20 53 51 4c 69 74 65 20 69 64 65 6e  **   SQLite iden
8cda0 74 69 66 69 65 72 20 63 68 61 72 61 63 74 65 72  tifier character
8cdb0 20 20 20 20 20 20 30 78 34 30 0d 0a 2a 2a 0d 0a        0x40..**..
8cdc0 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73  ** Bit 0x20 is s
8cdd0 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64  et if the mapped
8cde0 20 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69   character requi
8cdf0 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20  res translation 
8ce00 74 6f 20 75 70 70 65 72 0d 0a 2a 2a 20 63 61 73  to upper..** cas
8ce10 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63  e. i.e. if the c
8ce20 68 61 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f  haracter is a lo
8ce30 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63  wer-case ASCII c
8ce40 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 20 49 66  haracter...** If
8ce50 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61   x is a lower-ca
8ce60 73 65 20 41 53 43 49 49 20 63 68 61 72 61 63 74  se ASCII charact
8ce70 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70  er, then its upp
8ce80 65 72 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65  er-case equivale
8ce90 6e 74 0d 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30  nt..** is (x - 0
8cea0 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65 20  x20). Therefore 
8ceb0 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62 65  toupper() can be
8cec0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a   implemented as:
8ced0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28 78 20 26 20  ..**..**   (x & 
8cee0 7e 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0d  ~(map[x]&0x20)).
8cef0 0a 2a 2a 0d 0a 2a 2a 20 53 74 61 6e 64 61 72 64  .**..** Standard
8cf00 20 66 75 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65   function tolowe
8cf10 72 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  r() is implement
8cf20 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  ed using the sql
8cf30 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
8cf40 5b 5d 0d 0a 2a 2a 20 61 72 72 61 79 2e 20 74 6f  []..** array. to
8cf50 6c 6f 77 65 72 28 29 20 69 73 20 75 73 65 64 20  lower() is used 
8cf60 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20  more often than 
8cf70 74 6f 75 70 70 65 72 28 29 20 62 79 20 53 51 4c  toupper() by SQL
8cf80 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 42 69 74  ite...**..** Bit
8cf90 20 30 78 34 30 20 69 73 20 73 65 74 20 69 66 20   0x40 is set if 
8cfa0 74 68 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f  the character no
8cfb0 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 61  n-alphanumeric a
8cfc0 6e 64 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  nd can be used i
8cfd0 6e 20 61 6e 20 0d 0a 2a 2a 20 53 51 4c 69 74 65  n an ..** SQLite
8cfe0 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 49 64   identifier.  Id
8cff0 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 61 6c  entifiers are al
8d000 70 68 61 6e 75 6d 65 72 69 63 73 2c 20 22 5f 22  phanumerics, "_"
8d010 2c 20 22 24 22 2c 20 61 6e 64 20 61 6e 79 0d 0a  , "$", and any..
8d020 2a 2a 20 6e 6f 6e 2d 41 53 43 49 49 20 55 54 46  ** non-ASCII UTF
8d030 20 63 68 61 72 61 63 74 65 72 2e 20 48 65 6e 63   character. Henc
8d040 65 20 74 68 65 20 74 65 73 74 20 66 6f 72 20 77  e the test for w
8d050 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20  hether or not a 
8d060 63 68 61 72 61 63 74 65 72 20 69 73 0d 0a 2a 2a  character is..**
8d070 20 70 61 72 74 20 6f 66 20 61 6e 20 69 64 65 6e   part of an iden
8d080 74 69 66 69 65 72 20 69 73 20 30 78 34 36 2e 0d  tifier is 0x46..
8d090 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74 65 27 73  .**..** SQLite's
8d0a0 20 76 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64   versions are id
8d0b0 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73  entical to the s
8d0c0 74 61 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73  tandard versions
8d0d0 20 61 73 73 75 6d 69 6e 67 20 61 0d 0a 2a 2a 20   assuming a..** 
8d0e0 6c 6f 63 61 6c 65 20 6f 66 20 22 43 22 2e 20 54  locale of "C". T
8d0f0 68 65 79 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e  hey are implemen
8d100 74 65 64 20 61 73 20 6d 61 63 72 6f 73 20 69 6e  ted as macros in
8d110 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0d 0a 2a   sqliteInt.h...*
8d120 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
8d130 5f 41 53 43 49 49 0d 0a 53 51 4c 49 54 45 5f 50  _ASCII..SQLITE_P
8d140 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73  RIVATE const uns
8d150 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
8d160 65 33 43 74 79 70 65 4d 61 70 5b 32 35 36 5d 20  e3CtypeMap[256] 
8d170 3d 20 7b 0d 0a 20 20 30 78 30 30 2c 20 30 78 30  = {..  0x00, 0x0
8d180 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
8d190 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
8d1a0 20 30 78 30 30 2c 20 20 2f 2a 20 30 30 2e 2e 30   0x00,  /* 00..0
8d1b0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
8d1c0 0d 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ..  0x00, 0x01, 
8d1d0 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31  0x01, 0x01, 0x01
8d1e0 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78  , 0x01, 0x00, 0x
8d1f0 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 66 20 20  00,  /* 08..0f  
8d200 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20    ........ */.. 
8d210 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
8d220 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
8d230 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
8d240 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20 20 2e    /* 10..17    .
8d250 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30 78  ....... */..  0x
8d260 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d270 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d280 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
8d290 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e 2e  * 18..1f    ....
8d2a0 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30 78 30 31 2c  .... */..  0x01,
8d2b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
8d2c0 30 2c 20 30 78 34 30 2c 20 30 78 30 30 2c 20 30  0, 0x40, 0x00, 0
8d2d0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32  x00, 0x00,  /* 2
8d2e0 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 26  0..27     !"#$%&
8d2f0 27 20 2a 2f 0d 0a 20 20 30 78 30 30 2c 20 30 78  ' */..  0x00, 0x
8d300 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d310 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d320 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e  , 0x00,  /* 28..
8d330 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a  2f    ()*+,-./ *
8d340 2f 0d 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c  /..  0x0c, 0x0c,
8d350 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
8d360 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  c, 0x0c, 0x0c, 0
8d370 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20  x0c,  /* 30..37 
8d380 20 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0d 0a     01234567 */..
8d390 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78    0x0c, 0x0c, 0x
8d3a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d3b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d3c0 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20  ,  /* 38..3f    
8d3d0 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0d 0a 0d 0a 20  89:;<=>? */.... 
8d3e0 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 78 30   0x00, 0x0a, 0x0
8d3f0 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30  a, 0x0a, 0x0a, 0
8d400 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 32 2c  x0a, 0x0a, 0x02,
8d410 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 40    /* 40..47    @
8d420 41 42 43 44 45 46 47 20 2a 2f 0d 0a 20 20 30 78  ABCDEFG */..  0x
8d430 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
8d440 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
8d450 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f  , 0x02, 0x02,  /
8d460 2a 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b  * 48..4f    HIJK
8d470 4c 4d 4e 4f 20 2a 2f 0d 0a 20 20 30 78 30 32 2c  LMNO */..  0x02,
8d480 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
8d490 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
8d4a0 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35  x02, 0x02,  /* 5
8d4b0 30 2e 2e 35 37 20 20 20 20 50 51 52 53 54 55 56  0..57    PQRSTUV
8d4c0 57 20 2a 2f 0d 0a 20 20 30 78 30 32 2c 20 30 78  W */..  0x02, 0x
8d4d0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20  02, 0x02, 0x00, 
8d4e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d4f0 2c 20 30 78 34 30 2c 20 20 2f 2a 20 35 38 2e 2e  , 0x40,  /* 58..
8d500 35 66 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a  5f    XYZ[\]^_ *
8d510 2f 0d 0a 20 20 30 78 30 30 2c 20 30 78 32 61 2c  /..  0x00, 0x2a,
8d520 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32   0x2a, 0x2a, 0x2
8d530 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30  a, 0x2a, 0x2a, 0
8d540 78 32 32 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 20  x22,  /* 60..67 
8d550 20 20 20 60 61 62 63 64 65 66 67 20 2a 2f 0d 0a     `abcdefg */..
8d560 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78    0x22, 0x22, 0x
8d570 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
8d580 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
8d590 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 20 20 20 20  ,  /* 68..6f    
8d5a0 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0d 0a 20 20 30  hijklmno */..  0
8d5b0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
8d5c0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
8d5d0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20  2, 0x22, 0x22,  
8d5e0 2f 2a 20 37 30 2e 2e 37 37 20 20 20 20 70 71 72  /* 70..77    pqr
8d5f0 73 74 75 76 77 20 2a 2f 0d 0a 20 20 30 78 32 32  stuvw */..  0x22
8d600 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
8d610 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d620 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
8d630 37 38 2e 2e 37 66 20 20 20 20 78 79 7a 7b 7c 7d  78..7f    xyz{|}
8d640 7e 2e 20 2a 2f 0d 0a 0d 0a 20 20 30 78 34 30 2c  ~. */....  0x40,
8d650 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d660 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8d670 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38  x40, 0x40,  /* 8
8d680 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..87    .......
8d690 2e 20 2a 2f 0d 0a 20 20 30 78 34 30 2c 20 30 78  . */..  0x40, 0x
8d6a0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d6b0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8d6c0 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38 38 2e 2e  , 0x40,  /* 88..
8d6d0 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  8f    ........ *
8d6e0 2f 0d 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  /..  0x40, 0x40,
8d6f0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d700 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8d710 78 34 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20  x40,  /* 90..97 
8d720 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a     ........ */..
8d730 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
8d740 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d750 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8d760 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20  ,  /* 98..9f    
8d770 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30  ........ */..  0
8d780 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8d790 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d7a0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
8d7b0 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e  /* a0..a7    ...
8d7c0 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30 78 34 30  ..... */..  0x40
8d7d0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8d7e0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d7f0 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20  0x40, 0x40,  /* 
8d800 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e  a8..af    ......
8d810 2e 2e 20 2a 2f 0d 0a 20 20 30 78 34 30 2c 20 30  .. */..  0x40, 0
8d820 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8d830 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d840 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 62 30 2e  0, 0x40,  /* b0.
8d850 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .b7    ........ 
8d860 2a 2f 0d 0a 20 20 30 78 34 30 2c 20 30 78 34 30  */..  0x40, 0x40
8d870 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8d880 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d890 30 78 34 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66  0x40,  /* b8..bf
8d8a0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d      ........ */.
8d8b0 0a 0d 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  ...  0x40, 0x40,
8d8c0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d8d0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8d8e0 78 34 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20  x40,  /* c0..c7 
8d8f0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a     ........ */..
8d900 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
8d910 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d920 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8d930 2c 20 20 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20  ,  /* c8..cf    
8d940 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30  ........ */..  0
8d950 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8d960 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8d970 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
8d980 2f 2a 20 64 30 2e 2e 64 37 20 20 20 20 2e 2e 2e  /* d0..d7    ...
8d990 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20 30 78 34 30  ..... */..  0x40
8d9a0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8d9b0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8d9c0 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20  0x40, 0x40,  /* 
8d9d0 64 38 2e 2e 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e  d8..df    ......
8d9e0 2e 2e 20 2a 2f 0d 0a 20 20 30 78 34 30 2c 20 30  .. */..  0x40, 0
8d9f0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8da00 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8da10 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 30 2e  0, 0x40,  /* e0.
8da20 2e 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .e7    ........ 
8da30 2a 2f 0d 0a 20 20 30 78 34 30 2c 20 30 78 34 30  */..  0x40, 0x40
8da40 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8da50 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8da60 30 78 34 30 2c 20 20 2f 2a 20 65 38 2e 2e 65 66  0x40,  /* e8..ef
8da70 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d      ........ */.
8da80 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  .  0x40, 0x40, 0
8da90 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8daa0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8dab0 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 20 20 20  0,  /* f0..f7   
8dac0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 20 20   ........ */..  
8dad0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dae0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8daf0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 20 20  40, 0x40, 0x40  
8db00 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20 2e 2e   /* f8..ff    ..
8db10 2e 2e 2e 2e 2e 2e 20 2a 2f 0d 0a 7d 3b 0d 0a 23  ...... */..};..#
8db20 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66  endif....#ifndef
8db30 20 53 51 4c 49 54 45 5f 55 53 45 5f 55 52 49 0d   SQLITE_USE_URI.
8db40 0a 23 20 64 65 66 69 6e 65 20 20 53 51 4c 49 54  .# define  SQLIT
8db50 45 5f 55 53 45 5f 55 52 49 20 30 0d 0a 23 65 6e  E_USE_URI 0..#en
8db60 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
8db70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67  e following sing
8db80 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  leton contains t
8db90 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  he global config
8dba0 75 72 61 74 69 6f 6e 20 66 6f 72 0d 0a 2a 2a 20  uration for..** 
8dbb0 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
8dbc0 72 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ry...*/..SQLITE_
8dbd0 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
8dbe0 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65  SD struct Sqlite
8dbf0 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43  3Config sqlite3C
8dc00 6f 6e 66 69 67 20 3d 20 7b 0d 0a 20 20 20 53 51  onfig = {..   SQ
8dc10 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d  LITE_DEFAULT_MEM
8dc20 53 54 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d  STATUS,  /* bMem
8dc30 73 74 61 74 20 2a 2f 0d 0a 20 20 20 31 2c 20 20  stat */..   1,  
8dc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8dc50 20 20 20 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d         /* bCoreM
8dc60 75 74 65 78 20 2a 2f 0d 0a 20 20 20 53 51 4c 49  utex */..   SQLI
8dc70 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 31  TE_THREADSAFE==1
8dc80 2c 20 20 20 20 20 20 2f 2a 20 62 46 75 6c 6c 4d  ,      /* bFullM
8dc90 75 74 65 78 20 2a 2f 0d 0a 20 20 20 53 51 4c 49  utex */..   SQLI
8dca0 54 45 5f 55 53 45 5f 55 52 49 2c 20 20 20 20 20  TE_USE_URI,     
8dcb0 20 20 20 20 20 20 20 2f 2a 20 62 4f 70 65 6e 55         /* bOpenU
8dcc0 72 69 20 2a 2f 0d 0a 20 20 20 30 78 37 66 66 66  ri */..   0x7fff
8dcd0 66 66 66 65 2c 20 20 20 20 20 20 20 20 20 20 20  fffe,           
8dce0 20 20 20 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e       /* mxStrlen
8dcf0 20 2a 2f 0d 0a 20 20 20 31 32 38 2c 20 20 20 20   */..   128,    
8dd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8dd10 20 20 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64     /* szLookasid
8dd20 65 20 2a 2f 0d 0a 20 20 20 35 30 30 2c 20 20 20  e */..   500,   
8dd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8dd40 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64      /* nLookasid
8dd50 65 20 2a 2f 0d 0a 20 20 20 7b 30 2c 30 2c 30 2c  e */..   {0,0,0,
8dd60 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20  0,0,0,0,0},     
8dd70 20 20 20 20 2f 2a 20 6d 20 2a 2f 0d 0a 20 20 20      /* m */..   
8dd80 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  {0,0,0,0,0,0,0,0
8dd90 2c 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 6d 75  ,0},       /* mu
8dda0 74 65 78 20 2a 2f 0d 0a 20 20 20 7b 30 2c 30 2c  tex */..   {0,0,
8ddb0 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c  0,0,0,0,0,0,0,0,
8ddc0 30 2c 30 2c 30 7d 2c 2f 2a 20 70 63 61 63 68 65  0,0,0},/* pcache
8ddd0 32 20 2a 2f 0d 0a 20 20 20 28 76 6f 69 64 2a 29  2 */..   (void*)
8dde0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
8ddf0 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a 2f 0d      /* pHeap */.
8de00 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
8de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
8de20 2a 20 6e 48 65 61 70 20 2a 2f 0d 0a 20 20 20 30  * nHeap */..   0
8de30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  , 0,            
8de40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48            /* mnH
8de50 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0d 0a  eap, mxHeap */..
8de60 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20     (void*)0,    
8de70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8de80 20 70 53 63 72 61 74 63 68 20 2a 2f 0d 0a 20 20   pScratch */..  
8de90 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
8dea0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
8deb0 7a 53 63 72 61 74 63 68 20 2a 2f 0d 0a 20 20 20  zScratch */..   
8dec0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
8ded0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53             /* nS
8dee0 63 72 61 74 63 68 20 2a 2f 0d 0a 20 20 20 28 76  cratch */..   (v
8def0 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20  oid*)0,         
8df00 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67           /* pPag
8df10 65 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 20 20 20  e */..   0,     
8df20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8df30 20 20 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f      /* szPage */
8df40 0d 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  ..   0,         
8df50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8df60 2f 2a 20 6e 50 61 67 65 20 2a 2f 0d 0a 20 20 20  /* nPage */..   
8df70 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
8df80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78             /* mx
8df90 50 61 72 73 65 72 53 74 61 63 6b 20 2a 2f 0d 0a  ParserStack */..
8dfa0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
8dfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8dfc0 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62   sharedCacheEnab
8dfd0 6c 65 64 20 2a 2f 0d 0a 20 20 20 2f 2a 20 41 6c  led */..   /* Al
8dfe0 6c 20 74 68 65 20 72 65 73 74 20 73 68 6f 75 6c  l the rest shoul
8dff0 64 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74  d always be init
8e000 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 20  ialized to zero 
8e010 2a 2f 0d 0a 20 20 20 30 2c 20 20 20 20 20 20 20  */..   0,       
8e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e030 20 20 2f 2a 20 69 73 49 6e 69 74 20 2a 2f 0d 0a    /* isInit */..
8e040 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
8e050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8e060 20 69 6e 50 72 6f 67 72 65 73 73 20 2a 2f 0d 0a   inProgress */..
8e070 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
8e080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8e090 20 69 73 4d 75 74 65 78 49 6e 69 74 20 2a 2f 0d   isMutexInit */.
8e0a0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
8e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
8e0c0 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 2a  * isMallocInit *
8e0d0 2f 0d 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  /..   0,        
8e0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e0f0 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e 69 74   /* isPCacheInit
8e100 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 20 20 20 20   */..   0,      
8e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e120 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65 78     /* pInitMutex
8e130 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 20 20 20 20   */..   0,      
8e140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e150 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75     /* nRefInitMu
8e160 74 65 78 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 20  tex */..   0,   
8e170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e180 20 20 20 20 20 20 2f 2a 20 78 4c 6f 67 20 2a 2f        /* xLog */
8e190 0d 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  ..   0,         
8e1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e1b0 2f 2a 20 70 4c 6f 67 41 72 67 20 2a 2f 0d 0a 20  /* pLogArg */.. 
8e1c0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
8e1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8e1e0 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c 74 20  bLocaltimeFault 
8e1f0 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  */..};....../*..
8e200 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f  ** Hash table fo
8e210 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f  r global functio
8e220 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63  ns - functions c
8e230 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0d 0a 2a 2a  ommon to all..**
8e240 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
8e250 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e  tions.  After in
8e260 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68  itialization, th
8e270 69 73 20 74 61 62 6c 65 20 69 73 0d 0a 2a 2a 20  is table is..** 
8e280 72 65 61 64 2d 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a  read-only...*/..
8e290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
8e2a0 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65  QLITE_WSD FuncDe
8e2b0 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f  fHash sqlite3Glo
8e2c0 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0d 0a 0d  balFunctions;...
8e2d0 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74  ./*..** Constant
8e2e0 20 74 6f 6b 65 6e 73 20 66 6f 72 20 76 61 6c 75   tokens for valu
8e2f0 65 73 20 30 20 61 6e 64 20 31 2e 0d 0a 2a 2f 0d  es 0 and 1...*/.
8e300 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8e310 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 73 71 6c 69  const Token sqli
8e320 74 65 33 49 6e 74 54 6f 6b 65 6e 73 5b 5d 20 3d  te3IntTokens[] =
8e330 20 7b 0d 0a 20 20 20 7b 20 22 30 22 2c 20 31 20   {..   { "0", 1 
8e340 7d 2c 0d 0a 20 20 20 7b 20 22 31 22 2c 20 31 20  },..   { "1", 1 
8e350 7d 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  }..};....../*..*
8e360 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * The value of t
8e370 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74  he "pending" byt
8e380 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 30  e must be 0x4000
8e390 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 73  0000 (1 byte pas
8e3a0 74 20 74 68 65 0d 0a 2a 2a 20 31 2d 67 69 62 61  t the..** 1-giba
8e3b0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69  byte boundary) i
8e3c0 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64  n a compatible d
8e3d0 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65  atabase.  SQLite
8e3e0 20 6e 65 76 65 72 20 75 73 65 73 0d 0a 2a 2a 20   never uses..** 
8e3f0 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  the database pag
8e400 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
8e410 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
8e420 2e 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 65  .  It never atte
8e430 6d 70 74 73 0d 0a 2a 2a 20 74 6f 20 72 65 61 64  mpts..** to read
8e440 20 6f 72 20 77 72 69 74 65 20 74 68 61 74 20 70   or write that p
8e450 61 67 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  age.  The pendin
8e460 67 20 62 79 74 65 20 70 61 67 65 20 69 73 20 73  g byte page is s
8e470 65 74 20 61 73 73 69 67 6e 0d 0a 2a 2a 20 66 6f  et assign..** fo
8e480 72 20 75 73 65 20 62 79 20 74 68 65 20 56 46 53  r use by the VFS
8e490 20 6c 61 79 65 72 73 20 61 73 20 73 70 61 63 65   layers as space
8e4a0 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 66 69   for managing fi
8e4b0 6c 65 20 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a  le locks...**..*
8e4c0 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67  * During testing
8e4d0 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65  , it is often de
8e4e0 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20  sirable to move 
8e4f0 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
8e500 20 74 6f 0d 0a 2a 2a 20 61 20 64 69 66 66 65 72   to..** a differ
8e510 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
8e520 74 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20  the file.  This 
8e530 61 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 74  allows code that
8e540 20 68 61 73 20 74 6f 0d 0a 2a 2a 20 64 65 61 6c   has to..** deal
8e550 20 77 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e   with the pendin
8e560 67 20 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e  g byte to run on
8e570 20 66 69 6c 65 73 20 74 68 61 74 20 61 72 65 20   files that are 
8e580 6d 75 63 68 20 73 6d 61 6c 6c 65 72 0d 0a 2a 2a  much smaller..**
8e590 20 74 68 61 6e 20 31 20 47 69 42 2e 20 20 54 68   than 1 GiB.  Th
8e5a0 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
8e5b0 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61  ontrol() interfa
8e5c0 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ce can be used t
8e5d0 6f 0d 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 20 70  o..** move the p
8e5e0 65 6e 64 69 6e 67 20 62 79 74 65 2e 0d 0a 2a 2a  ending byte...**
8e5f0 0d 0a 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20  ..** IMPORTANT: 
8e600 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65   Changing the pe
8e610 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 61 6e  nding byte to an
8e620 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68  y value other th
8e630 61 6e 0d 0a 2a 2a 20 30 78 34 30 30 30 30 30 30  an..** 0x4000000
8e640 30 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20  0 results in an 
8e650 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74  incompatible dat
8e660 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d 61  abase file forma
8e670 74 21 0d 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  t!..** Changing 
8e680 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
8e690 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74 69 6e   during operatin
8e6a0 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  g results in und
8e6b0 65 66 69 6e 65 64 0d 0a 2a 2a 20 61 6e 64 20 64  efined..** and d
8e6c0 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 76  ileterious behav
8e6d0 69 6f 72 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  ior...*/..#ifnde
8e6e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  f SQLITE_OMIT_WS
8e6f0 44 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  D..SQLITE_PRIVAT
8e700 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e  E int sqlite3Pen
8e710 64 69 6e 67 42 79 74 65 20 3d 20 30 78 34 30 30  dingByte = 0x400
8e720 30 30 30 30 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a  00000;..#endif..
8e730 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 6f 70 65 72 74  ../*..** Propert
8e740 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20  ies of opcodes. 
8e750 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49   The OPFLG_INITI
8e760 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 69 73 0d  ALIZER macro is.
8e770 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 6d  .** created by m
8e780 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 75 72  kopcodeh.awk dur
8e790 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e  ing compilation.
8e7a0 20 20 44 61 74 61 20 69 73 20 6f 62 74 61 69 6e    Data is obtain
8e7b0 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ed..** from the 
8e7c0 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69  comments followi
8e7d0 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f  ng the "case OP_
8e7e0 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e 74  xxxx:" statement
8e7f0 73 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 76 64 62  s in..** the vdb
8e800 65 2e 63 20 66 69 6c 65 2e 20 20 0d 0a 2a 2f 0d  e.c file.  ..*/.
8e810 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8e820 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
8e830 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 6f 64  har sqlite3Opcod
8e840 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50  eProperty[] = OP
8e850 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b  FLG_INITIALIZER;
8e860 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
8e870 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61  *** End of globa
8e880 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
8e890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e8b0 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
8e8c0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
8e8d0 63 74 69 6d 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ctime.c ********
8e8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e900 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 31  ***/../*..** 201
8e910 30 20 46 65 62 72 75 61 72 79 20 32 33 0d 0a 2a  0 February 23..*
8e920 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
8e930 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
8e940 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
8e950 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
8e960 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
8e970 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
8e980 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
8e990 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
8e9a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
8e9b0 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
8e9c0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
8e9d0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
8e9e0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
8e9f0 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
8ea00 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
8ea10 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
8ea20 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
8ea30 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
8ea40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea80 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
8ea90 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
8eaa0 74 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64  ts routines used
8eab0 20 74 6f 20 72 65 70 6f 72 74 20 77 68 61 74 20   to report what 
8eac0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
8ead0 69 6f 6e 73 0d 0a 2a 2a 20 53 51 4c 69 74 65 20  ions..** SQLite 
8eae0 77 61 73 20 62 75 69 6c 74 20 77 69 74 68 2e 0d  was built with..
8eaf0 0a 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  .*/....#ifndef S
8eb00 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49  QLITE_OMIT_COMPI
8eb10 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0d 0a  LEOPTION_DIAGS..
8eb20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 61 72  ..../*..** An ar
8eb30 72 61 79 20 6f 66 20 6e 61 6d 65 73 20 6f 66 20  ray of names of 
8eb40 61 6c 6c 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  all compile-time
8eb50 20 6f 70 74 69 6f 6e 73 2e 20 20 54 68 69 73 20   options.  This 
8eb60 61 72 72 61 79 20 73 68 6f 75 6c 64 20 0d 0a 2a  array should ..*
8eb70 2a 20 62 65 20 73 6f 72 74 65 64 20 41 2d 5a 2e  * be sorted A-Z.
8eb80 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 61 72  ..**..** This ar
8eb90 72 61 79 20 6c 6f 6f 6b 73 20 6c 61 72 67 65 2c  ray looks large,
8eba0 20 62 75 74 20 69 6e 20 61 20 74 79 70 69 63 61   but in a typica
8ebb0 6c 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 20 61  l installation a
8ebc0 63 74 75 61 6c 6c 79 20 75 73 65 73 0d 0a 2a 2a  ctually uses..**
8ebd0 20 6f 6e 6c 79 20 61 20 68 61 6e 64 66 75 6c 20   only a handful 
8ebe0 6f 66 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  of compile-time 
8ebf0 6f 70 74 69 6f 6e 73 2c 20 73 6f 20 6d 6f 73 74  options, so most
8ec00 20 74 69 6d 65 73 20 74 68 69 73 20 61 72 72 61   times this arra
8ec10 79 20 69 73 20 75 73 75 61 6c 6c 79 0d 0a 2a 2a  y is usually..**
8ec20 20 72 61 74 68 65 72 20 73 68 6f 72 74 20 61 6e   rather short an
8ec30 64 20 75 73 65 73 20 6c 69 74 74 6c 65 20 6d 65  d uses little me
8ec40 6d 6f 72 79 20 73 70 61 63 65 2e 0d 0a 2a 2f 0d  mory space...*/.
8ec50 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
8ec60 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 43 6f 6d  ar * const azCom
8ec70 70 69 6c 65 4f 70 74 5b 5d 20 3d 20 7b 0d 0a 0d  pileOpt[] = {...
8ec80 0a 2f 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73  ./* These macros
8ec90 20 61 72 65 20 70 72 6f 76 69 64 65 64 20 74 6f   are provided to
8eca0 20 22 73 74 72 69 6e 67 69 66 79 22 20 74 68 65   "stringify" the
8ecb0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 65   value of the de
8ecc0 66 69 6e 65 0d 0a 2a 2a 20 66 6f 72 20 74 68 6f  fine..** for tho
8ecd0 73 65 20 6f 70 74 69 6f 6e 73 20 69 6e 20 77 68  se options in wh
8ece0 69 63 68 20 74 68 65 20 76 61 6c 75 65 20 69 73  ich the value is
8ecf0 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 2a 2f 0d   meaningful. */.
8ed00 0a 23 64 65 66 69 6e 65 20 43 54 49 4d 45 4f 50  .#define CTIMEOP
8ed10 54 5f 56 41 4c 5f 28 6f 70 74 29 20 23 6f 70 74  T_VAL_(opt) #opt
8ed20 0d 0a 23 64 65 66 69 6e 65 20 43 54 49 4d 45 4f  ..#define CTIMEO
8ed30 50 54 5f 56 41 4c 28 6f 70 74 29 20 43 54 49 4d  PT_VAL(opt) CTIM
8ed40 45 4f 50 54 5f 56 41 4c 5f 28 6f 70 74 29 0d 0a  EOPT_VAL_(opt)..
8ed50 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
8ed60 33 32 42 49 54 5f 52 4f 57 49 44 0d 0a 20 20 22  32BIT_ROWID..  "
8ed70 33 32 42 49 54 5f 52 4f 57 49 44 22 2c 0d 0a 23  32BIT_ROWID",..#
8ed80 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
8ed90 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49 47  LITE_4_BYTE_ALIG
8eda0 4e 45 44 5f 4d 41 4c 4c 4f 43 0d 0a 20 20 22 34  NED_MALLOC..  "4
8edb0 5f 42 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d 41  _BYTE_ALIGNED_MA
8edc0 4c 4c 4f 43 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  LLOC",..#endif..
8edd0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41  #ifdef SQLITE_CA
8ede0 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b  SE_SENSITIVE_LIK
8edf0 45 0d 0a 20 20 22 43 41 53 45 5f 53 45 4e 53 49  E..  "CASE_SENSI
8ee00 54 49 56 45 5f 4c 49 4b 45 22 2c 0d 0a 23 65 6e  TIVE_LIKE",..#en
8ee10 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8ee20 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0d 0a  TE_CHECK_PAGES..
8ee30 20 20 22 43 48 45 43 4b 5f 50 41 47 45 53 22 2c    "CHECK_PAGES",
8ee40 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
8ee50 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45   SQLITE_COVERAGE
8ee60 5f 54 45 53 54 0d 0a 20 20 22 43 4f 56 45 52 41  _TEST..  "COVERA
8ee70 47 45 5f 54 45 53 54 22 2c 0d 0a 23 65 6e 64 69  GE_TEST",..#endi
8ee80 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8ee90 5f 44 45 42 55 47 0d 0a 20 20 22 44 45 42 55 47  _DEBUG..  "DEBUG
8eea0 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8eeb0 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
8eec0 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0d 0a  T_LOCKING_MODE..
8eed0 20 20 22 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49    "DEFAULT_LOCKI
8eee0 4e 47 5f 4d 4f 44 45 3d 22 20 43 54 49 4d 45 4f  NG_MODE=" CTIMEO
8eef0 50 54 5f 56 41 4c 28 53 51 4c 49 54 45 5f 44 45  PT_VAL(SQLITE_DE
8ef00 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
8ef10 44 45 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  DE),..#endif..#i
8ef20 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  fdef SQLITE_DISA
8ef30 42 4c 45 5f 44 49 52 53 59 4e 43 0d 0a 20 20 22  BLE_DIRSYNC..  "
8ef40 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 22  DISABLE_DIRSYNC"
8ef50 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
8ef60 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
8ef70 5f 4c 46 53 0d 0a 20 20 22 44 49 53 41 42 4c 45  _LFS..  "DISABLE
8ef80 5f 4c 46 53 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  _LFS",..#endif..
8ef90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
8efa0 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
8efb0 45 0d 0a 20 20 22 45 4e 41 42 4c 45 5f 41 54 4f  E..  "ENABLE_ATO
8efc0 4d 49 43 5f 57 52 49 54 45 22 2c 0d 0a 23 65 6e  MIC_WRITE",..#en
8efd0 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8efe0 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0d  TE_ENABLE_CEROD.
8eff0 0a 20 20 22 45 4e 41 42 4c 45 5f 43 45 52 4f 44  .  "ENABLE_CEROD
8f000 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8f010 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8f020 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
8f030 0d 0a 20 20 22 45 4e 41 42 4c 45 5f 43 4f 4c 55  ..  "ENABLE_COLU
8f040 4d 4e 5f 4d 45 54 41 44 41 54 41 22 2c 0d 0a 23  MN_METADATA",..#
8f050 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
8f060 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45  LITE_ENABLE_EXPE
8f070 4e 53 49 56 45 5f 41 53 53 45 52 54 0d 0a 20 20  NSIVE_ASSERT..  
8f080 22 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  "ENABLE_EXPENSIV
8f090 45 5f 41 53 53 45 52 54 22 2c 0d 0a 23 65 6e 64  E_ASSERT",..#end
8f0a0 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
8f0b0 45 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0d 0a 20  E_ENABLE_FTS1.. 
8f0c0 20 22 45 4e 41 42 4c 45 5f 46 54 53 31 22 2c 0d   "ENABLE_FTS1",.
8f0d0 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
8f0e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
8f0f0 53 32 0d 0a 20 20 22 45 4e 41 42 4c 45 5f 46 54  S2..  "ENABLE_FT
8f100 53 32 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  S2",..#endif..#i
8f110 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8f120 4c 45 5f 46 54 53 33 0d 0a 20 20 22 45 4e 41 42  LE_FTS3..  "ENAB
8f130 4c 45 5f 46 54 53 33 22 2c 0d 0a 23 65 6e 64 69  LE_FTS3",..#endi
8f140 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f150 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52  _ENABLE_FTS3_PAR
8f160 45 4e 54 48 45 53 49 53 0d 0a 20 20 22 45 4e 41  ENTHESIS..  "ENA
8f170 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48  BLE_FTS3_PARENTH
8f180 45 53 49 53 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  ESIS",..#endif..
8f190 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
8f1a0 41 42 4c 45 5f 46 54 53 34 0d 0a 20 20 22 45 4e  ABLE_FTS4..  "EN
8f1b0 41 42 4c 45 5f 46 54 53 34 22 2c 0d 0a 23 65 6e  ABLE_FTS4",..#en
8f1c0 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8f1d0 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0d 0a 20  TE_ENABLE_ICU.. 
8f1e0 20 22 45 4e 41 42 4c 45 5f 49 43 55 22 2c 0d 0a   "ENABLE_ICU",..
8f1f0 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
8f200 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54  QLITE_ENABLE_IOT
8f210 52 41 43 45 0d 0a 20 20 22 45 4e 41 42 4c 45 5f  RACE..  "ENABLE_
8f220 49 4f 54 52 41 43 45 22 2c 0d 0a 23 65 6e 64 69  IOTRACE",..#endi
8f230 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f240 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 54  _ENABLE_LOAD_EXT
8f250 45 4e 53 49 4f 4e 0d 0a 20 20 22 45 4e 41 42 4c  ENSION..  "ENABL
8f260 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  E_LOAD_EXTENSION
8f270 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8f280 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8f290 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a  _LOCKING_STYLE..
8f2a0 20 20 22 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e    "ENABLE_LOCKIN
8f2b0 47 5f 53 54 59 4c 45 3d 22 20 43 54 49 4d 45 4f  G_STYLE=" CTIMEO
8f2c0 50 54 5f 56 41 4c 28 53 51 4c 49 54 45 5f 45 4e  PT_VAL(SQLITE_EN
8f2d0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
8f2e0 4c 45 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  LE),..#endif..#i
8f2f0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8f300 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
8f310 4d 45 4e 54 0d 0a 20 20 22 45 4e 41 42 4c 45 5f  MENT..  "ENABLE_
8f320 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
8f330 54 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  T",..#endif..#if
8f340 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
8f350 45 5f 4d 45 4d 53 59 53 33 0d 0a 20 20 22 45 4e  E_MEMSYS3..  "EN
8f360 41 42 4c 45 5f 4d 45 4d 53 59 53 33 22 2c 0d 0a  ABLE_MEMSYS3",..
8f370 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
8f380 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8f390 53 59 53 35 0d 0a 20 20 22 45 4e 41 42 4c 45 5f  SYS5..  "ENABLE_
8f3a0 4d 45 4d 53 59 53 35 22 2c 0d 0a 23 65 6e 64 69  MEMSYS5",..#endi
8f3b0 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f3c0 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45  _ENABLE_OVERSIZE
8f3d0 5f 43 45 4c 4c 5f 43 48 45 43 4b 0d 0a 20 20 22  _CELL_CHECK..  "
8f3e0 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f  ENABLE_OVERSIZE_
8f3f0 43 45 4c 4c 5f 43 48 45 43 4b 22 2c 0d 0a 23 65  CELL_CHECK",..#e
8f400 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
8f410 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45  ITE_ENABLE_RTREE
8f420 0d 0a 20 20 22 45 4e 41 42 4c 45 5f 52 54 52 45  ..  "ENABLE_RTRE
8f430 45 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  E",..#endif..#if
8f440 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
8f450 45 5f 53 54 41 54 33 0d 0a 20 20 22 45 4e 41 42  E_STAT3..  "ENAB
8f460 4c 45 5f 53 54 41 54 33 22 2c 0d 0a 23 65 6e 64  LE_STAT3",..#end
8f470 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
8f480 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
8f490 4e 4f 54 49 46 59 0d 0a 20 20 22 45 4e 41 42 4c  NOTIFY..  "ENABL
8f4a0 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 22  E_UNLOCK_NOTIFY"
8f4b0 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
8f4c0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
8f4d0 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
8f4e0 4d 49 54 0d 0a 20 20 22 45 4e 41 42 4c 45 5f 55  MIT..  "ENABLE_U
8f4f0 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
8f500 49 54 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  IT",..#endif..#i
8f510 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
8f520 43 4f 44 45 43 0d 0a 20 20 22 48 41 53 5f 43 4f  CODEC..  "HAS_CO
8f530 44 45 43 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23  DEC",..#endif..#
8f540 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56  ifdef SQLITE_HAV
8f550 45 5f 49 53 4e 41 4e 0d 0a 20 20 22 48 41 56 45  E_ISNAN..  "HAVE
8f560 5f 49 53 4e 41 4e 22 2c 0d 0a 23 65 6e 64 69 66  _ISNAN",..#endif
8f570 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
8f580 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
8f590 49 56 45 5f 4d 55 54 45 58 0d 0a 20 20 22 48 4f  IVE_MUTEX..  "HO
8f5a0 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
8f5b0 45 5f 4d 55 54 45 58 22 2c 0d 0a 23 65 6e 64 69  E_MUTEX",..#endi
8f5c0 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f5d0 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b  _IGNORE_AFP_LOCK
8f5e0 5f 45 52 52 4f 52 53 0d 0a 20 20 22 49 47 4e 4f  _ERRORS..  "IGNO
8f5f0 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f  RE_AFP_LOCK_ERRO
8f600 52 53 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  RS",..#endif..#i
8f610 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f  fdef SQLITE_IGNO
8f620 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
8f630 52 4f 52 53 0d 0a 20 20 22 49 47 4e 4f 52 45 5f  RORS..  "IGNORE_
8f640 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
8f650 53 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  S",..#endif..#if
8f660 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34  def SQLITE_INT64
8f670 5f 54 59 50 45 0d 0a 20 20 22 49 4e 54 36 34 5f  _TYPE..  "INT64_
8f680 54 59 50 45 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  TYPE",..#endif..
8f690 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f  #ifdef SQLITE_LO
8f6a0 43 4b 5f 54 52 41 43 45 0d 0a 20 20 22 4c 4f 43  CK_TRACE..  "LOC
8f6b0 4b 5f 54 52 41 43 45 22 2c 0d 0a 23 65 6e 64 69  K_TRACE",..#endi
8f6c0 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f6d0 5f 4d 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52  _MAX_SCHEMA_RETR
8f6e0 59 0d 0a 20 20 22 4d 41 58 5f 53 43 48 45 4d 41  Y..  "MAX_SCHEMA
8f6f0 5f 52 45 54 52 59 3d 22 20 43 54 49 4d 45 4f 50  _RETRY=" CTIMEOP
8f700 54 5f 56 41 4c 28 53 51 4c 49 54 45 5f 4d 41 58  T_VAL(SQLITE_MAX
8f710 5f 53 43 48 45 4d 41 5f 52 45 54 52 59 29 2c 0d  _SCHEMA_RETRY),.
8f720 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
8f730 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0d  SQLITE_MEMDEBUG.
8f740 0a 20 20 22 4d 45 4d 44 45 42 55 47 22 2c 0d 0a  .  "MEMDEBUG",..
8f750 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
8f760 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49  QLITE_MIXED_ENDI
8f770 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0d 0a  AN_64BIT_FLOAT..
8f780 20 20 22 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f    "MIXED_ENDIAN_
8f790 36 34 42 49 54 5f 46 4c 4f 41 54 22 2c 0d 0a 23  64BIT_FLOAT",..#
8f7a0 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
8f7b0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0d 0a 20 20  LITE_NO_SYNC..  
8f7c0 22 4e 4f 5f 53 59 4e 43 22 2c 0d 0a 23 65 6e 64  "NO_SYNC",..#end
8f7d0 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
8f7e0 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
8f7f0 45 0d 0a 20 20 22 4f 4d 49 54 5f 41 4c 54 45 52  E..  "OMIT_ALTER
8f800 54 41 42 4c 45 22 2c 0d 0a 23 65 6e 64 69 66 0d  TABLE",..#endif.
8f810 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
8f820 4d 49 54 5f 41 4e 41 4c 59 5a 45 0d 0a 20 20 22  MIT_ANALYZE..  "
8f830 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 22 2c 0d 0a  OMIT_ANALYZE",..
8f840 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
8f850 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
8f860 48 0d 0a 20 20 22 4f 4d 49 54 5f 41 54 54 41 43  H..  "OMIT_ATTAC
8f870 48 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  H",..#endif..#if
8f880 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
8f890 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a 20  AUTHORIZATION.. 
8f8a0 20 22 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41   "OMIT_AUTHORIZA
8f8b0 54 49 4f 4e 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  TION",..#endif..
8f8c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
8f8d0 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
8f8e0 0d 0a 20 20 22 4f 4d 49 54 5f 41 55 54 4f 49 4e  ..  "OMIT_AUTOIN
8f8f0 43 52 45 4d 45 4e 54 22 2c 0d 0a 23 65 6e 64 69  CREMENT",..#endi
8f900 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f910 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a  _OMIT_AUTOINIT..
8f920 20 20 22 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54    "OMIT_AUTOINIT
8f930 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8f940 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
8f950 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0d 0a  UTOMATIC_INDEX..
8f960 20 20 22 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49    "OMIT_AUTOMATI
8f970 43 5f 49 4e 44 45 58 22 2c 0d 0a 23 65 6e 64 69  C_INDEX",..#endi
8f980 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8f990 5f 4f 4d 49 54 5f 41 55 54 4f 52 45 53 45 54 0d  _OMIT_AUTORESET.
8f9a0 0a 20 20 22 4f 4d 49 54 5f 41 55 54 4f 52 45 53  .  "OMIT_AUTORES
8f9b0 45 54 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  ET",..#endif..#i
8f9c0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
8f9d0 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 22  _AUTOVACUUM..  "
8f9e0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 22  OMIT_AUTOVACUUM"
8f9f0 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
8fa00 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45  f SQLITE_OMIT_BE
8fa10 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49  TWEEN_OPTIMIZATI
8fa20 4f 4e 0d 0a 20 20 22 4f 4d 49 54 5f 42 45 54 57  ON..  "OMIT_BETW
8fa30 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  EEN_OPTIMIZATION
8fa40 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8fa50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
8fa60 4c 4f 42 5f 4c 49 54 45 52 41 4c 0d 0a 20 20 22  LOB_LITERAL..  "
8fa70 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
8fa80 4c 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  L",..#endif..#if
8fa90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
8faa0 42 54 52 45 45 43 4f 55 4e 54 0d 0a 20 20 22 4f  BTREECOUNT..  "O
8fab0 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 22 2c  MIT_BTREECOUNT",
8fac0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
8fad0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
8fae0 4c 54 49 4e 5f 54 45 53 54 0d 0a 20 20 22 4f 4d  LTIN_TEST..  "OM
8faf0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 22  IT_BUILTIN_TEST"
8fb00 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
8fb10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
8fb20 53 54 0d 0a 20 20 22 4f 4d 49 54 5f 43 41 53 54  ST..  "OMIT_CAST
8fb30 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8fb40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
8fb50 48 45 43 4b 0d 0a 20 20 22 4f 4d 49 54 5f 43 48  HECK..  "OMIT_CH
8fb60 45 43 4b 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 2f  ECK",..#endif../
8fb70 2a 20 2f 2f 20 72 65 64 75 6e 64 61 6e 74 0d 0a  * // redundant..
8fb80 2a 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  ** #ifdef SQLITE
8fb90 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54  _OMIT_COMPILEOPT
8fba0 49 4f 4e 5f 44 49 41 47 53 0d 0a 2a 2a 20 20 20  ION_DIAGS..**   
8fbb0 22 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54  "OMIT_COMPILEOPT
8fbc0 49 4f 4e 5f 44 49 41 47 53 22 2c 0d 0a 2a 2a 20  ION_DIAGS",..** 
8fbd0 23 65 6e 64 69 66 0d 0a 2a 2f 0d 0a 23 69 66 64  #endif..*/..#ifd
8fbe0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
8fbf0 4f 4d 50 4c 45 54 45 0d 0a 20 20 22 4f 4d 49 54  OMPLETE..  "OMIT
8fc00 5f 43 4f 4d 50 4c 45 54 45 22 2c 0d 0a 23 65 6e  _COMPLETE",..#en
8fc10 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8fc20 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
8fc30 5f 53 45 4c 45 43 54 0d 0a 20 20 22 4f 4d 49 54  _SELECT..  "OMIT
8fc40 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
8fc50 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8fc60 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
8fc70 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0d 0a 20  ATETIME_FUNCS.. 
8fc80 20 22 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f   "OMIT_DATETIME_
8fc90 46 55 4e 43 53 22 2c 0d 0a 23 65 6e 64 69 66 0d  FUNCS",..#endif.
8fca0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
8fcb0 4d 49 54 5f 44 45 43 4c 54 59 50 45 0d 0a 20 20  MIT_DECLTYPE..  
8fcc0 22 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 22 2c  "OMIT_DECLTYPE",
8fcd0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
8fce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
8fcf0 52 45 43 41 54 45 44 0d 0a 20 20 22 4f 4d 49 54  RECATED..  "OMIT
8fd00 5f 44 45 50 52 45 43 41 54 45 44 22 2c 0d 0a 23  _DEPRECATED",..#
8fd10 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
8fd20 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
8fd30 0d 0a 20 20 22 4f 4d 49 54 5f 44 49 53 4b 49 4f  ..  "OMIT_DISKIO
8fd40 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8fd50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
8fd60 58 50 4c 41 49 4e 0d 0a 20 20 22 4f 4d 49 54 5f  XPLAIN..  "OMIT_
8fd70 45 58 50 4c 41 49 4e 22 2c 0d 0a 23 65 6e 64 69  EXPLAIN",..#endi
8fd80 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8fd90 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
8fda0 41 53 0d 0a 20 20 22 4f 4d 49 54 5f 46 4c 41 47  AS..  "OMIT_FLAG
8fdb0 5f 50 52 41 47 4d 41 53 22 2c 0d 0a 23 65 6e 64  _PRAGMAS",..#end
8fdc0 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
8fdd0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
8fde0 50 4f 49 4e 54 0d 0a 20 20 22 4f 4d 49 54 5f 46  POINT..  "OMIT_F
8fdf0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 22 2c 0d  LOATING_POINT",.
8fe00 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
8fe10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
8fe20 49 47 4e 5f 4b 45 59 0d 0a 20 20 22 4f 4d 49 54  IGN_KEY..  "OMIT
8fe30 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 22 2c 0d 0a  _FOREIGN_KEY",..
8fe40 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
8fe50 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54  QLITE_OMIT_GET_T
8fe60 41 42 4c 45 0d 0a 20 20 22 4f 4d 49 54 5f 47 45  ABLE..  "OMIT_GE
8fe70 54 5f 54 41 42 4c 45 22 2c 0d 0a 23 65 6e 64 69  T_TABLE",..#endi
8fe80 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8fe90 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0d 0a  _OMIT_INCRBLOB..
8fea0 20 20 22 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42    "OMIT_INCRBLOB
8feb0 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
8fec0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
8fed0 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0d 0a  NTEGRITY_CHECK..
8fee0 20 20 22 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54    "OMIT_INTEGRIT
8fef0 59 5f 43 48 45 43 4b 22 2c 0d 0a 23 65 6e 64 69  Y_CHECK",..#endi
8ff00 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8ff10 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
8ff20 49 5a 41 54 49 4f 4e 0d 0a 20 20 22 4f 4d 49 54  IZATION..  "OMIT
8ff30 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
8ff40 4f 4e 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  ON",..#endif..#i
8ff50 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
8ff60 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0d  _LOAD_EXTENSION.
8ff70 0a 20 20 22 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  .  "OMIT_LOAD_EX
8ff80 54 45 4e 53 49 4f 4e 22 2c 0d 0a 23 65 6e 64 69  TENSION",..#endi
8ff90 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8ffa0 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0d  _OMIT_LOCALTIME.
8ffb0 0a 20 20 22 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49  .  "OMIT_LOCALTI
8ffc0 4d 45 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  ME",..#endif..#i
8ffd0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
8ffe0 5f 4c 4f 4f 4b 41 53 49 44 45 0d 0a 20 20 22 4f  _LOOKASIDE..  "O
8fff0 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 22 2c 0d  MIT_LOOKASIDE",.
90000 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
90010 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
90020 52 59 44 42 0d 0a 20 20 22 4f 4d 49 54 5f 4d 45  RYDB..  "OMIT_ME
90030 4d 4f 52 59 44 42 22 2c 0d 0a 23 65 6e 64 69 66  MORYDB",..#endif
90040 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
90050 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0d  OMIT_MERGE_SORT.
90060 0a 20 20 22 4f 4d 49 54 5f 4d 45 52 47 45 5f 53  .  "OMIT_MERGE_S
90070 4f 52 54 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23  ORT",..#endif..#
90080 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
90090 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
900a0 4e 0d 0a 20 20 22 4f 4d 49 54 5f 4f 52 5f 4f 50  N..  "OMIT_OR_OP
900b0 54 49 4d 49 5a 41 54 49 4f 4e 22 2c 0d 0a 23 65  TIMIZATION",..#e
900c0 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
900d0 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
900e0 52 41 47 4d 41 53 0d 0a 20 20 22 4f 4d 49 54 5f  RAGMAS..  "OMIT_
900f0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 22 2c 0d  PAGER_PRAGMAS",.
90100 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
90110 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47  SQLITE_OMIT_PRAG
90120 4d 41 0d 0a 20 20 22 4f 4d 49 54 5f 50 52 41 47  MA..  "OMIT_PRAG
90130 4d 41 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  MA",..#endif..#i
90140 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90150 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41  _PROGRESS_CALLBA
90160 43 4b 0d 0a 20 20 22 4f 4d 49 54 5f 50 52 4f 47  CK..  "OMIT_PROG
90170 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 22 2c 0d  RESS_CALLBACK",.
90180 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
90190 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43  SQLITE_OMIT_QUIC
901a0 4b 42 41 4c 41 4e 43 45 0d 0a 20 20 22 4f 4d 49  KBALANCE..  "OMI
901b0 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 22 2c  T_QUICKBALANCE",
901c0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
901d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
901e0 4e 44 45 58 0d 0a 20 20 22 4f 4d 49 54 5f 52 45  NDEX..  "OMIT_RE
901f0 49 4e 44 45 58 22 2c 0d 0a 23 65 6e 64 69 66 0d  INDEX",..#endif.
90200 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
90210 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d  MIT_SCHEMA_PRAGM
90220 41 53 0d 0a 20 20 22 4f 4d 49 54 5f 53 43 48 45  AS..  "OMIT_SCHE
90230 4d 41 5f 50 52 41 47 4d 41 53 22 2c 0d 0a 23 65  MA_PRAGMAS",..#e
90240 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
90250 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
90260 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0d  VERSION_PRAGMAS.
90270 0a 20 20 22 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  .  "OMIT_SCHEMA_
90280 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 22  VERSION_PRAGMAS"
90290 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
902a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
902b0 41 52 45 44 5f 43 41 43 48 45 0d 0a 20 20 22 4f  ARED_CACHE..  "O
902c0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
902d0 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  ",..#endif..#ifd
902e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
902f0 55 42 51 55 45 52 59 0d 0a 20 20 22 4f 4d 49 54  UBQUERY..  "OMIT
90300 5f 53 55 42 51 55 45 52 59 22 2c 0d 0a 23 65 6e  _SUBQUERY",..#en
90310 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
90320 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
90330 41 42 4c 45 0d 0a 20 20 22 4f 4d 49 54 5f 54 43  ABLE..  "OMIT_TC
90340 4c 5f 56 41 52 49 41 42 4c 45 22 2c 0d 0a 23 65  L_VARIABLE",..#e
90350 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
90360 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0d  ITE_OMIT_TEMPDB.
90370 0a 20 20 22 4f 4d 49 54 5f 54 45 4d 50 44 42 22  .  "OMIT_TEMPDB"
90380 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
90390 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
903a0 41 43 45 0d 0a 20 20 22 4f 4d 49 54 5f 54 52 41  ACE..  "OMIT_TRA
903b0 43 45 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  CE",..#endif..#i
903c0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
903d0 5f 54 52 49 47 47 45 52 0d 0a 20 20 22 4f 4d 49  _TRIGGER..  "OMI
903e0 54 5f 54 52 49 47 47 45 52 22 2c 0d 0a 23 65 6e  T_TRIGGER",..#en
903f0 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
90400 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45  TE_OMIT_TRUNCATE
90410 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0d 0a 20  _OPTIMIZATION.. 
90420 20 22 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f   "OMIT_TRUNCATE_
90430 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 22 2c 0d 0a  OPTIMIZATION",..
90440 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
90450 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
90460 0d 0a 20 20 22 4f 4d 49 54 5f 55 54 46 31 36 22  ..  "OMIT_UTF16"
90470 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  ,..#endif..#ifde
90480 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  f SQLITE_OMIT_VA
90490 43 55 55 4d 0d 0a 20 20 22 4f 4d 49 54 5f 56 41  CUUM..  "OMIT_VA
904a0 43 55 55 4d 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  CUUM",..#endif..
904b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
904c0 49 54 5f 56 49 45 57 0d 0a 20 20 22 4f 4d 49 54  IT_VIEW..  "OMIT
904d0 5f 56 49 45 57 22 2c 0d 0a 23 65 6e 64 69 66 0d  _VIEW",..#endif.
904e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
904f0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
90500 0d 0a 20 20 22 4f 4d 49 54 5f 56 49 52 54 55 41  ..  "OMIT_VIRTUA
90510 4c 54 41 42 4c 45 22 2c 0d 0a 23 65 6e 64 69 66  LTABLE",..#endif
90520 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
90530 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 22 4f 4d 49  OMIT_WAL..  "OMI
90540 54 5f 57 41 4c 22 2c 0d 0a 23 65 6e 64 69 66 0d  T_WAL",..#endif.
90550 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
90560 4d 49 54 5f 57 53 44 0d 0a 20 20 22 4f 4d 49 54  MIT_WSD..  "OMIT
90570 5f 57 53 44 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a  _WSD",..#endif..
90580 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
90590 49 54 5f 58 46 45 52 5f 4f 50 54 0d 0a 20 20 22  IT_XFER_OPT..  "
905a0 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 22 2c 0d  OMIT_XFER_OPT",.
905b0 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
905c0 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e  SQLITE_PERFORMAN
905d0 43 45 5f 54 52 41 43 45 0d 0a 20 20 22 50 45 52  CE_TRACE..  "PER
905e0 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 22 2c  FORMANCE_TRACE",
905f0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
90600 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45   SQLITE_PROXY_DE
90610 42 55 47 0d 0a 20 20 22 50 52 4f 58 59 5f 44 45  BUG..  "PROXY_DE
90620 42 55 47 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23  BUG",..#endif..#
90630 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43  ifdef SQLITE_SEC
90640 55 52 45 5f 44 45 4c 45 54 45 0d 0a 20 20 22 53  URE_DELETE..  "S
90650 45 43 55 52 45 5f 44 45 4c 45 54 45 22 2c 0d 0a  ECURE_DELETE",..
90660 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
90670 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43  QLITE_SMALL_STAC
90680 4b 0d 0a 20 20 22 53 4d 41 4c 4c 5f 53 54 41 43  K..  "SMALL_STAC
90690 4b 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  K",..#endif..#if
906a0 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44  def SQLITE_SOUND
906b0 45 58 0d 0a 20 20 22 53 4f 55 4e 44 45 58 22 2c  EX..  "SOUNDEX",
906c0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
906d0 20 53 51 4c 49 54 45 5f 54 43 4c 0d 0a 20 20 22   SQLITE_TCL..  "
906e0 54 43 4c 22 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23  TCL",..#endif..#
906f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d  ifdef SQLITE_TEM
90700 50 5f 53 54 4f 52 45 0d 0a 20 20 22 54 45 4d 50  P_STORE..  "TEMP
90710 5f 53 54 4f 52 45 3d 22 20 43 54 49 4d 45 4f 50  _STORE=" CTIMEOP
90720 54 5f 56 41 4c 28 53 51 4c 49 54 45 5f 54 45 4d  T_VAL(SQLITE_TEM
90730 50 5f 53 54 4f 52 45 29 2c 0d 0a 23 65 6e 64 69  P_STORE),..#endi
90740 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
90750 5f 54 45 53 54 0d 0a 20 20 22 54 45 53 54 22 2c  _TEST..  "TEST",
90760 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
90770 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
90780 46 45 0d 0a 20 20 22 54 48 52 45 41 44 53 41 46  FE..  "THREADSAF
90790 45 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c  E=" CTIMEOPT_VAL
907a0 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  (SQLITE_THREADSA
907b0 46 45 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  FE),..#endif..#i
907c0 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f  fdef SQLITE_USE_
907d0 41 4c 4c 4f 43 41 0d 0a 20 20 22 55 53 45 5f 41  ALLOCA..  "USE_A
907e0 4c 4c 4f 43 41 22 2c 0d 0a 23 65 6e 64 69 66 0d  LLOCA",..#endif.
907f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a  .#ifdef SQLITE_Z
90800 45 52 4f 5f 4d 41 4c 4c 4f 43 0d 0a 20 20 22 5a  ERO_MALLOC..  "Z
90810 45 52 4f 5f 4d 41 4c 4c 4f 43 22 0d 0a 23 65 6e  ERO_MALLOC"..#en
90820 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  dif..};..../*..*
90830 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  * Given the name
90840 20 6f 66 20 61 20 63 6f 6d 70 69 6c 65 2d 74 69   of a compile-ti
90850 6d 65 20 6f 70 74 69 6f 6e 2c 20 72 65 74 75 72  me option, retur
90860 6e 20 74 72 75 65 20 69 66 20 74 68 61 74 20 6f  n true if that o
90870 70 74 69 6f 6e 0d 0a 2a 2a 20 77 61 73 20 75 73  ption..** was us
90880 65 64 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  ed and false if 
90890 6e 6f 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  not...**..** The
908a0 20 6e 61 6d 65 20 63 61 6e 20 6f 70 74 69 6f 6e   name can option
908b0 61 6c 6c 79 20 62 65 67 69 6e 20 77 69 74 68 20  ally begin with 
908c0 22 53 51 4c 49 54 45 5f 22 20 62 75 74 20 74 68  "SQLITE_" but th
908d0 65 20 22 53 51 4c 49 54 45 5f 22 20 70 72 65 66  e "SQLITE_" pref
908e0 69 78 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65  ix..** is not re
908f0 71 75 69 72 65 64 20 66 6f 72 20 61 20 6d 61 74  quired for a mat
90900 63 68 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ch...*/..SQLITE_
90910 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
90920 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
90930 65 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ed(const char *z
90940 4f 70 74 4e 61 6d 65 29 7b 0d 0a 20 20 69 6e 74  OptName){..  int
90950 20 69 2c 20 6e 3b 0d 0a 20 20 69 66 28 20 73 71   i, n;..  if( sq
90960 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4f  lite3StrNICmp(zO
90970 70 74 4e 61 6d 65 2c 20 22 53 51 4c 49 54 45 5f  ptName, "SQLITE_
90980 22 2c 20 37 29 3d 3d 30 20 29 20 7a 4f 70 74 4e  ", 7)==0 ) zOptN
90990 61 6d 65 20 2b 3d 20 37 3b 0d 0a 20 20 6e 20 3d  ame += 7;..  n =
909a0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
909b0 28 7a 4f 70 74 4e 61 6d 65 29 3b 0d 0a 0d 0a 20  (zOptName);.... 
909c0 20 2f 2a 20 53 69 6e 63 65 20 41 72 72 61 79 53   /* Since ArrayS
909d0 69 7a 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 74  ize(azCompileOpt
909e0 29 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e  ) is normally in
909f0 20 73 69 6e 67 6c 65 20 64 69 67 69 74 73 2c 20   single digits, 
90a00 61 0d 0a 20 20 2a 2a 20 6c 69 6e 65 61 72 20 73  a..  ** linear s
90a10 65 61 72 63 68 20 69 73 20 61 64 65 71 75 61 74  earch is adequat
90a20 65 2e 20 20 4e 6f 20 6e 65 65 64 20 66 6f 72 20  e.  No need for 
90a30 61 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 2e  a binary search.
90a40 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20   */..  for(i=0; 
90a50 69 3c 41 72 72 61 79 53 69 7a 65 28 61 7a 43 6f  i<ArraySize(azCo
90a60 6d 70 69 6c 65 4f 70 74 29 3b 20 69 2b 2b 29 7b  mpileOpt); i++){
90a70 0d 0a 20 20 20 20 69 66 28 20 20 20 28 73 71 6c  ..    if(   (sql
90a80 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4f 70  ite3StrNICmp(zOp
90a90 74 4e 61 6d 65 2c 20 61 7a 43 6f 6d 70 69 6c 65  tName, azCompile
90aa0 4f 70 74 5b 69 5d 2c 20 6e 29 3d 3d 30 29 0d 0a  Opt[i], n)==0)..
90ab0 20 20 20 20 20 20 20 26 26 20 28 20 28 61 7a 43         && ( (azC
90ac0 6f 6d 70 69 6c 65 4f 70 74 5b 69 5d 5b 6e 5d 3d  ompileOpt[i][n]=
90ad0 3d 30 29 20 7c 7c 20 28 61 7a 43 6f 6d 70 69 6c  =0) || (azCompil
90ae0 65 4f 70 74 5b 69 5d 5b 6e 5d 3d 3d 27 3d 27 29  eOpt[i][n]=='=')
90af0 20 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a   ) ) return 1;..
90b00 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
90b10 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
90b20 74 75 72 6e 20 74 68 65 20 4e 2d 74 68 20 63 6f  turn the N-th co
90b30 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
90b40 6e 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20  n string.  If N 
90b50 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
90b60 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 61 20 4e 55  ..** return a NU
90b70 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d  LL pointer...*/.
90b80 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
90b90 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
90ba0 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65  compileoption_ge
90bb0 74 28 69 6e 74 20 4e 29 7b 0d 0a 20 20 69 66 28  t(int N){..  if(
90bc0 20 4e 3e 3d 30 20 26 26 20 4e 3c 41 72 72 61 79   N>=0 && N<Array
90bd0 53 69 7a 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70  Size(azCompileOp
90be0 74 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  t) ){..    retur
90bf0 6e 20 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 4e  n azCompileOpt[N
90c00 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  ];..  }..  retur
90c10 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69  n 0;..}....#endi
90c20 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
90c30 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44  _COMPILEOPTION_D
90c40 49 41 47 53 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  IAGS */..../****
90c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
90c60 66 20 63 74 69 6d 65 2e 63 20 2a 2a 2a 2a 2a 2a  f ctime.c ******
90c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
90ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
90cb0 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20  n file status.c 
90cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
90cf0 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38  .** 2008 June 18
90d00 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74  ..**..** The aut
90d10 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
90d20 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
90d30 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
90d40 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20   place of..** a 
90d50 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
90d60 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
90d70 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79  :..**..**    May
90d80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
90d90 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20   not evil...**  
90da0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
90db0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
90dc0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
90dd0 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20  ive others...** 
90de0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
90df0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
90e00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
90e10 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a  you give...**..*
90e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e60 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
90e70 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70   This module imp
90e80 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69  lements the sqli
90e90 74 65 33 5f 73 74 61 74 75 73 28 29 20 69 6e 74  te3_status() int
90ea0 65 72 66 61 63 65 20 61 6e 64 20 72 65 6c 61 74  erface and relat
90eb0 65 64 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 61  ed..** functiona
90ec0 6c 69 74 79 2e 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a  lity...*/../****
90ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
90ee0 64 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20  de vdbeInt.h in 
90ef0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 74  the middle of st
90f00 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  atus.c *********
90f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
90f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
90f30 6e 20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68  n file vdbeInt.h
90f40 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
90f70 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62  .** 2003 Septemb
90f80 65 72 20 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  er 6..**..** The
90f90 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
90fa0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
90fb0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
90fc0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
90fd0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
90fe0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
90ff0 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
91000 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
91010 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
91020 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
91030 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
91040 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
91050 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
91060 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
91070 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
91080 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
91090 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
910a0 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
910b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
910f0 20 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61   This is the hea
91100 64 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66  der file for inf
91110 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73  ormation that is
91120 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0d   private to the.
91130 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20  .** VDBE.  This 
91140 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  information used
91150 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68   to all be at th
91160 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e  e top of the sin
91170 67 6c 65 0d 0a 2a 2a 20 73 6f 75 72 63 65 20 63  gle..** source c
91180 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e 63  ode file "vdbe.c
91190 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66 69  ".  When that fi
911a0 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 69  le became too bi
911b0 67 20 28 6f 76 65 72 0d 0a 2a 2a 20 36 30 30 30  g (over..** 6000
911c0 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20   lines long) it 
911d0 77 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74  was split up int
911e0 6f 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65  o several smalle
911f0 72 20 66 69 6c 65 73 20 61 6e 64 0d 0a 2a 2a 20  r files and..** 
91200 74 68 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f  this header info
91210 72 6d 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74  rmation was fact
91220 6f 72 65 64 20 6f 75 74 2e 0d 0a 2a 2f 0d 0a 23  ored out...*/..#
91230 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e 54 5f  ifndef _VDBEINT_
91240 48 5f 0d 0a 23 64 65 66 69 6e 65 20 5f 56 44 42  H_..#define _VDB
91250 45 49 4e 54 5f 48 5f 0d 0a 0d 0a 2f 2a 0d 0a 2a  EINT_H_..../*..*
91260 2a 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61  * SQL is transla
91270 74 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65  ted into a seque
91280 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69  nce of instructi
91290 6f 6e 73 20 74 6f 20 62 65 0d 0a 2a 2a 20 65 78  ons to be..** ex
912a0 65 63 75 74 65 64 20 62 79 20 61 20 76 69 72 74  ecuted by a virt
912b0 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61  ual machine.  Ea
912c0 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  ch instruction i
912d0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0d 0a 2a  s an instance..*
912e0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
912f0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a  ng structure...*
91300 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
91310 74 20 56 64 62 65 4f 70 20 4f 70 3b 0d 0a 0d 0a  t VdbeOp Op;....
91320 2f 2a 0d 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76  /*..** Boolean v
91330 61 6c 75 65 73 0d 0a 2a 2f 0d 0a 74 79 70 65 64  alues..*/..typed
91340 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ef unsigned char
91350 20 42 6f 6f 6c 3b 0d 0a 0d 0a 2f 2a 20 4f 70 61   Bool;..../* Opa
91360 71 75 65 20 74 79 70 65 20 75 73 65 64 20 62 79  que type used by
91370 20 63 6f 64 65 20 69 6e 20 76 64 62 65 73 6f 72   code in vdbesor
91380 74 2e 63 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20  t.c */..typedef 
91390 73 74 72 75 63 74 20 56 64 62 65 53 6f 72 74 65  struct VdbeSorte
913a0 72 20 56 64 62 65 53 6f 72 74 65 72 3b 0d 0a 0d  r VdbeSorter;...
913b0 0a 2f 2a 20 4f 70 61 71 75 65 20 74 79 70 65 20  ./* Opaque type 
913c0 75 73 65 64 20 62 79 20 74 68 65 20 65 78 70 6c  used by the expl
913d0 61 69 6e 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65  ainer */..typede
913e0 66 20 73 74 72 75 63 74 20 45 78 70 6c 61 69 6e  f struct Explain
913f0 20 45 78 70 6c 61 69 6e 3b 0d 0a 0d 0a 2f 2a 0d   Explain;..../*.
91400 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
91410 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61  a pointer into a
91420 20 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69   single BTree wi
91430 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
91440 66 69 6c 65 2e 0d 0a 2a 2a 20 54 68 65 20 63 75  file...** The cu
91450 72 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f  rsor can seek to
91460 20 61 20 42 54 72 65 65 20 65 6e 74 72 79 20 77   a BTree entry w
91470 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
91480 20 6b 65 79 2c 20 6f 72 0d 0a 2a 2a 20 6c 6f 6f   key, or..** loo
91490 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 69  p over all entri
914a0 65 73 20 6f 66 20 74 68 65 20 42 74 72 65 65 2e  es of the Btree.
914b0 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69    You can also i
914c0 6e 73 65 72 74 20 6e 65 77 20 42 54 72 65 65 0d  nsert new BTree.
914d0 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 20 72  .** entries or r
914e0 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 79 20  etrieve the key 
914f0 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  or data from the
91500 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
91510 63 75 72 73 6f 72 0d 0a 2a 2a 20 69 73 20 63 75  cursor..** is cu
91520 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
91530 20 74 6f 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 45 76   to...** ..** Ev
91540 65 72 79 20 63 75 72 73 6f 72 20 74 68 61 74 20  ery cursor that 
91550 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
91560 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20  ine has open is 
91570 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
91580 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  n..** instance o
91590 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
915a0 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a  structure...*/..
915b0 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f  struct VdbeCurso
915c0 72 20 7b 0d 0a 20 20 42 74 43 75 72 73 6f 72 20  r {..  BtCursor 
915d0 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20  *pCursor;    /* 
915e0 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 63  The cursor struc
915f0 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b  ture of the back
91600 65 6e 64 20 2a 2f 0d 0a 20 20 42 74 72 65 65 20  end */..  Btree 
91610 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  *pBt;           
91620 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 6c 65  /* Separate file
91630 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61   holding tempora
91640 72 79 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 4b  ry table */..  K
91650 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
91660 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f  ;    /* Info abo
91670 75 74 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65  ut index keys ne
91680 65 64 65 64 20 62 79 20 69 6e 64 65 78 20 63 75  eded by index cu
91690 72 73 6f 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  rsors */..  int 
916a0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
916b0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
916c0 72 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e  rsor database in
916d0 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d   db->aDb[] (or -
916e0 31 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 73 65  1) */..  int pse
916f0 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f  udoTableReg;   /
91700 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
91710 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63  ng pseudotable c
91720 6f 6e 74 65 6e 74 2e 20 2a 2f 0d 0a 20 20 69 6e  ontent. */..  in
91730 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  t nField;       
91740 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
91750 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68   fields in the h
91760 65 61 64 65 72 20 2a 2f 0d 0a 20 20 42 6f 6f 6c  eader */..  Bool
91770 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20   zeroed;        
91780 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72    /* True if zer
91790 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64  oed out and read
917a0 79 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0d 0a  y for reuse */..
917b0 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61    Bool rowidIsVa
917c0 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  lid;    /* True 
917d0 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20  if lastRowid is 
917e0 76 61 6c 69 64 20 2a 2f 0d 0a 20 20 42 6f 6f 6c  valid */..  Bool
917f0 20 61 74 46 69 72 73 74 3b 20 20 20 20 20 20 20   atFirst;       
91800 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69    /* True if poi
91810 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20 65  nting to first e
91820 6e 74 72 79 20 2a 2f 0d 0a 20 20 42 6f 6f 6c 20  ntry */..  Bool 
91830 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20  useRandomRowid; 
91840 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77   /* Generate new
91850 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20   record numbers 
91860 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f  semi-randomly */
91870 0d 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77  ..  Bool nullRow
91880 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
91890 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  e if pointing to
918a0 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64   a row with no d
918b0 61 74 61 20 2a 2f 0d 0a 20 20 42 6f 6f 6c 20 64  ata */..  Bool d
918c0 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20  eferredMoveto;  
918d0 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c  /* A call to sql
918e0 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
918f0 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0d 0a  ) is needed */..
91900 20 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20    Bool isTable; 
91910 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
91920 69 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69  if a table requi
91930 72 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79  ring integer key
91940 73 20 2a 2f 0d 0a 20 20 42 6f 6f 6c 20 69 73 49  s */..  Bool isI
91950 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  ndex;         /*
91960 20 54 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65   True if an inde
91970 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79  x containing key
91980 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61  s only - no data
91990 20 2a 2f 0d 0a 20 20 42 6f 6f 6c 20 69 73 4f 72   */..  Bool isOr
919a0 64 65 72 65 64 3b 20 20 20 20 20 20 20 2f 2a 20  dered;       /* 
919b0 54 72 75 65 20 69 66 20 74 68 65 20 75 6e 64 65  True if the unde
919c0 72 6c 79 69 6e 67 20 74 61 62 6c 65 20 69 73 20  rlying table is 
919d0 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 20  BTREE_UNORDERED 
919e0 2a 2f 0d 0a 20 20 42 6f 6f 6c 20 69 73 53 6f 72  */..  Bool isSor
919f0 74 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ter;        /* T
91a00 72 75 65 20 69 66 20 61 20 6e 65 77 2d 73 74 79  rue if a new-sty
91a10 6c 65 20 73 6f 72 74 65 72 20 2a 2f 0d 0a 20 20  le sorter */..  
91a20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
91a30 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72  sor *pVtabCursor
91a40 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72  ;  /* The cursor
91a50 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74   for a virtual t
91a60 61 62 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  able */..  const
91a70 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
91a80 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a  *pModule;     /*
91a90 20 4d 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73   Module for curs
91aa0 6f 72 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a  or pVtabCursor *
91ab0 2f 0d 0a 20 20 69 36 34 20 73 65 71 43 6f 75 6e  /..  i64 seqCoun
91ac0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  t;         /* Se
91ad0 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a  quence counter *
91ae0 2f 0d 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54  /..  i64 movetoT
91af0 61 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72  arget;     /* Ar
91b00 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65  gument to the de
91b10 66 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74  ferred sqlite3Bt
91b20 72 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0d 0a  reeMoveto() */..
91b30 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b    i64 lastRowid;
91b40 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
91b50 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78  rowid from a Nex
91b60 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65  t or NextIdx ope
91b70 72 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 56 64 62  ration */..  Vdb
91b80 65 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72  eSorter *pSorter
91b90 3b 20 20 2f 2a 20 53 6f 72 74 65 72 20 6f 62 6a  ;  /* Sorter obj
91ba0 65 63 74 20 66 6f 72 20 4f 50 5f 53 6f 72 74 65  ect for OP_Sorte
91bb0 72 4f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f  rOpen cursors */
91bc0 0d 0a 0d 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20  ....  /* Result 
91bd0 6f 66 20 6c 61 73 74 20 73 71 6c 69 74 65 33 42  of last sqlite3B
91be0 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e  treeMoveto() don
91bf0 65 20 62 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78  e by an OP_NotEx
91c00 69 73 74 73 20 6f 72 20 0d 0a 20 20 2a 2a 20 4f  ists or ..  ** O
91c10 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64  P_IsUnique opcod
91c20 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72  e on this cursor
91c30 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 65 65 6b  . */..  int seek
91c40 52 65 73 75 6c 74 3b 0d 0a 0d 0a 20 20 2f 2a 20  Result;....  /* 
91c50 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69  Cached informati
91c60 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61  on about the hea
91c70 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  der for the data
91c80 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65   record that the
91c90 0d 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73  ..  ** cursor is
91ca0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
91cb0 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61  ing to.  Only va
91cc0 6c 69 64 20 69 66 20 63 61 63 68 65 53 74 61 74  lid if cacheStat
91cd0 75 73 20 6d 61 74 63 68 65 73 0d 0a 20 20 2a 2a  us matches..  **
91ce0 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 2e 20   Vdbe.cacheCtr. 
91cf0 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 77   Vdbe.cacheCtr w
91d00 69 6c 6c 20 6e 65 76 65 72 20 74 61 6b 65 20 6f  ill never take o
91d10 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0d 0a  n the value of..
91d20 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 41 4c 45    ** CACHE_STALE
91d30 20 61 6e 64 20 73 6f 20 73 65 74 74 69 6e 67 20   and so setting 
91d40 63 61 63 68 65 53 74 61 74 75 73 3d 43 41 43 48  cacheStatus=CACH
91d50 45 5f 53 54 41 4c 45 20 67 75 61 72 61 6e 74 65  E_STALE guarante
91d60 65 73 20 74 68 61 74 0d 0a 20 20 2a 2a 20 74 68  es that..  ** th
91d70 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 20 6f  e cache is out o
91d80 66 20 64 61 74 65 2e 0d 0a 20 20 2a 2a 0d 0a 20  f date...  **.. 
91d90 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70   ** aRow might p
91da0 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72  oint to (ephemer
91db0 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65  al) data for the
91dc0 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72   current row, or
91dd0 20 69 74 20 6d 69 67 68 74 0d 0a 20 20 2a 2a 20   it might..  ** 
91de0 62 65 20 4e 55 4c 4c 2e 0d 0a 20 20 2a 2f 0d 0a  be NULL...  */..
91df0 20 20 75 33 32 20 63 61 63 68 65 53 74 61 74 75    u32 cacheStatu
91e00 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65  s;      /* Cache
91e10 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 69   is valid if thi
91e20 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e 63  s matches Vdbe.c
91e30 61 63 68 65 43 74 72 20 2a 2f 0d 0a 20 20 69 6e  acheCtr */..  in
91e40 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  t payloadSize;  
91e50 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
91e60 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
91e70 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20  the record */.. 
91e80 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20   u32 *aType;    
91e90 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76         /* Type v
91ea0 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e  alues for all en
91eb0 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63  tries in the rec
91ec0 6f 72 64 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 61  ord */..  u32 *a
91ed0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
91ee0 2f 2a 20 43 61 63 68 65 64 20 6f 66 66 73 65 74  /* Cached offset
91ef0 73 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  s to the start o
91f00 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64  f each columns d
91f10 61 74 61 20 2a 2f 0d 0a 20 20 75 38 20 2a 61 52  ata */..  u8 *aR
91f20 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ow;             
91f30 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68 65 20  /* Data for the 
91f40 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 69 66 20  current row, if 
91f50 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20  all on one page 
91f60 2a 2f 0d 0a 7d 3b 0d 0a 74 79 70 65 64 65 66 20  */..};..typedef 
91f70 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f  struct VdbeCurso
91f80 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0d 0a 0d  r VdbeCursor;...
91f90 0a 2f 2a 0d 0a 2a 2a 20 57 68 65 6e 20 61 20 73  ./*..** When a s
91fa0 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 65 78  ub-program is ex
91fb0 65 63 75 74 65 64 20 28 4f 50 5f 50 72 6f 67 72  ecuted (OP_Progr
91fc0 61 6d 29 2c 20 61 20 73 74 72 75 63 74 75 72 65  am), a structure
91fd0 20 6f 66 20 74 68 69 73 20 74 79 70 65 0d 0a 2a   of this type..*
91fe0 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74  * is allocated t
91ff0 6f 20 73 74 6f 72 65 20 74 68 65 20 63 75 72 72  o store the curr
92000 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
92010 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   program counter
92020 2c 20 61 73 0d 0a 2a 2a 20 77 65 6c 6c 20 61 73  , as..** well as
92030 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 6d   the current mem
92040 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 20 61  ory cell array a
92050 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 65 72  nd various other
92060 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 63 0d   frame specific.
92070 0a 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65  .** values store
92080 64 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 74  d in the Vdbe st
92090 72 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 73  ruct. When the s
920a0 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 69  ub-program is fi
920b0 6e 69 73 68 65 64 2c 20 0d 0a 2a 2a 20 74 68 65  nished, ..** the
920c0 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  se values are co
920d0 70 69 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65  pied back to the
920e0 20 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56   Vdbe from the V
920f0 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75  dbeFrame structu
92100 72 65 2c 0d 0a 2a 2a 20 72 65 73 74 6f 72 69 6e  re,..** restorin
92110 67 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  g the state of t
92120 68 65 20 56 4d 20 74 6f 20 61 73 20 69 74 20 77  he VM to as it w
92130 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 75  as before the su
92140 62 2d 70 72 6f 67 72 61 6d 0d 0a 2a 2a 20 62 65  b-program..** be
92150 67 61 6e 20 65 78 65 63 75 74 69 6e 67 2e 0d 0a  gan executing...
92160 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72  **..** The memor
92170 79 20 66 6f 72 20 61 20 56 64 62 65 46 72 61 6d  y for a VdbeFram
92180 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f  e object is allo
92190 63 61 74 65 64 20 61 6e 64 20 6d 61 6e 61 67 65  cated and manage
921a0 64 20 62 79 20 61 20 6d 65 6d 6f 72 79 0d 0a 2a  d by a memory..*
921b0 2a 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61  * cell in the pa
921c0 72 65 6e 74 20 28 63 61 6c 6c 69 6e 67 29 20 66  rent (calling) f
921d0 72 61 6d 65 2e 20 57 68 65 6e 20 74 68 65 20 6d  rame. When the m
921e0 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 64 65  emory cell is de
921f0 6c 65 74 65 64 20 6f 72 0d 0a 2a 2a 20 6f 76 65  leted or..** ove
92200 72 77 72 69 74 74 65 6e 2c 20 74 68 65 20 56 64  rwritten, the Vd
92210 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 20 69  beFrame object i
92220 73 20 6e 6f 74 20 66 72 65 65 64 20 69 6d 6d 65  s not freed imme
92230 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64  diately. Instead
92240 2c 20 69 74 0d 0a 2a 2a 20 69 73 20 6c 69 6e 6b  , it..** is link
92250 65 64 20 69 6e 74 6f 20 74 68 65 20 56 64 62 65  ed into the Vdbe
92260 2e 70 44 65 6c 46 72 61 6d 65 20 6c 69 73 74 2e  .pDelFrame list.
92270 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   The contents of
92280 20 74 68 65 20 56 64 62 65 2e 70 44 65 6c 46 72   the Vdbe.pDelFr
92290 61 6d 65 0d 0a 2a 2a 20 6c 69 73 74 20 69 73 20  ame..** list is 
922a0 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
922b0 20 56 4d 20 69 73 20 72 65 73 65 74 20 69 6e 20   VM is reset in 
922c0 56 64 62 65 48 61 6c 74 28 29 2e 20 54 68 65 20  VdbeHalt(). The 
922d0 72 65 61 73 6f 6e 20 66 6f 72 20 64 6f 69 6e 67  reason for doing
922e0 0d 0a 2a 2a 20 74 68 69 73 20 69 6e 73 74 65 61  ..** this instea
922f0 64 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68  d of deleting th
92300 65 20 56 64 62 65 46 72 61 6d 65 20 69 6d 6d 65  e VdbeFrame imme
92310 64 69 61 74 65 6c 79 20 69 73 20 74 6f 20 61 76  diately is to av
92320 6f 69 64 20 72 65 63 75 72 73 69 76 65 0d 0a 2a  oid recursive..*
92330 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
92340 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
92350 28 29 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  () when the memo
92360 72 79 20 63 65 6c 6c 73 20 62 65 6c 6f 6e 67 69  ry cells belongi
92370 6e 67 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 63 68  ng to the..** ch
92380 69 6c 64 20 66 72 61 6d 65 20 61 72 65 20 72 65  ild frame are re
92390 6c 65 61 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  leased...**..** 
923a0 54 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78  The currently ex
923b0 65 63 75 74 69 6e 67 20 66 72 61 6d 65 20 69 73  ecuting frame is
923c0 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65 2e   stored in Vdbe.
923d0 70 46 72 61 6d 65 2e 20 56 64 62 65 2e 70 46 72  pFrame. Vdbe.pFr
923e0 61 6d 65 20 69 73 0d 0a 2a 2a 20 73 65 74 20 74  ame is..** set t
923f0 6f 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 63 75  o NULL if the cu
92400 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
92410 67 20 66 72 61 6d 65 20 69 73 20 74 68 65 20 6d  g frame is the m
92420 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f  ain program...*/
92430 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
92440 20 56 64 62 65 46 72 61 6d 65 20 56 64 62 65 46   VdbeFrame VdbeF
92450 72 61 6d 65 3b 0d 0a 73 74 72 75 63 74 20 56 64  rame;..struct Vd
92460 62 65 46 72 61 6d 65 20 7b 0d 0a 20 20 56 64 62  beFrame {..  Vdb
92470 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
92480 20 20 20 20 20 2f 2a 20 56 4d 20 74 68 69 73 20       /* VM this 
92490 66 72 61 6d 65 20 62 65 6c 6f 6e 67 73 20 74 6f  frame belongs to
924a0 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 63 3b 20 20   */..  int pc;  
924b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
924c0 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75 6e 74 65  * Program Counte
924d0 72 20 69 6e 20 70 61 72 65 6e 74 20 28 63 61 6c  r in parent (cal
924e0 6c 69 6e 67 29 20 66 72 61 6d 65 20 2a 2f 0d 0a  ling) frame */..
924f0 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20    Op *aOp;      
92500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
92510 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
92520 73 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61  s for parent fra
92530 6d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 70  me */..  int nOp
92540 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
92550 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 4f 70 20   /* Size of aOp 
92560 61 72 72 61 79 20 2a 2f 0d 0a 20 20 4d 65 6d 20  array */..  Mem 
92570 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *aMem;          
92580 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
92590 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72  memory cells for
925a0 20 70 61 72 65 6e 74 20 66 72 61 6d 65 20 2a 2f   parent frame */
925b0 0d 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  ..  int nMem;   
925c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
925d0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
925e0 20 69 6e 20 61 4d 65 6d 20 2a 2f 0d 0a 20 20 75   in aMem */..  u
925f0 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b 20 20 20  8 *aOnceFlag;   
92600 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
92610 6f 66 20 4f 50 5f 4f 6e 63 65 20 66 6c 61 67 73  of OP_Once flags
92620 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61 6d   for parent fram
92630 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 6e 63  e */..  int nOnc
92640 65 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20  eFlag;          
92650 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
92660 72 69 65 73 20 69 6e 20 61 4f 6e 63 65 46 6c 61  ries in aOnceFla
92670 67 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73  g */..  VdbeCurs
92680 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20  or **apCsr;     
92690 2f 2a 20 41 72 72 61 79 20 6f 66 20 56 64 62 65  /* Array of Vdbe
926a0 20 63 75 72 73 6f 72 73 20 66 6f 72 20 70 61 72   cursors for par
926b0 65 6e 74 20 66 72 61 6d 65 20 2a 2f 0d 0a 20 20  ent frame */..  
926c0 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20  u16 nCursor;    
926d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
926e0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
926f0 61 70 43 73 72 20 2a 2f 0d 0a 20 20 76 6f 69 64  apCsr */..  void
92700 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   *token;        
92710 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 53      /* Copy of S
92720 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 20  ubProgram.token 
92730 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64  */..  int nChild
92740 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
92750 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
92760 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 68 69 6c  y cells for chil
92770 64 20 66 72 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e  d frame */..  in
92780 74 20 6e 43 68 69 6c 64 43 73 72 3b 20 20 20 20  t nChildCsr;    
92790 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
927a0 6f 66 20 63 75 72 73 6f 72 73 20 66 6f 72 20 63  of cursors for c
927b0 68 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0d 0a 20  hild frame */.. 
927c0 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20   i64 lastRowid; 
927d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
927e0 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73   insert rowid (s
927f0 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64  qlite3.lastRowid
92800 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 68 61  ) */..  int nCha
92810 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  nge;            
92820 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61  /* Statement cha
92830 6e 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e  nges (Vdbe.nChan
92840 67 65 73 29 20 20 20 20 20 2a 2f 0d 0a 20 20 56  ges)     */..  V
92850 64 62 65 46 72 61 6d 65 20 2a 70 50 61 72 65 6e  dbeFrame *pParen
92860 74 3b 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74  t;     /* Parent
92870 20 6f 66 20 74 68 69 73 20 66 72 61 6d 65 2c 20   of this frame, 
92880 6f 72 20 4e 55 4c 4c 20 69 66 20 70 61 72 65 6e  or NULL if paren
92890 74 20 69 73 20 6d 61 69 6e 20 2a 2f 0d 0a 7d 3b  t is main */..};
928a0 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 56 64 62 65  ....#define Vdbe
928b0 46 72 61 6d 65 4d 65 6d 28 70 29 20 28 28 4d 65  FrameMem(p) ((Me
928c0 6d 20 2a 29 26 28 28 75 38 20 2a 29 70 29 5b 52  m *)&((u8 *)p)[R
928d0 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62  OUND8(sizeof(Vdb
928e0 65 46 72 61 6d 65 29 29 5d 29 0d 0a 0d 0a 2f 2a  eFrame))])..../*
928f0 0d 0a 2a 2a 20 41 20 76 61 6c 75 65 20 66 6f 72  ..** A value for
92900 20 56 64 62 65 43 75 72 73 6f 72 2e 63 61 63 68   VdbeCursor.cach
92910 65 56 61 6c 69 64 20 74 68 61 74 20 6d 65 61 6e  eValid that mean
92920 73 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61  s the cache is a
92930 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 2e 0d 0a  lways invalid...
92940 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 43 41 43 48  */..#define CACH
92950 45 5f 53 54 41 4c 45 20 30 0d 0a 0d 0a 2f 2a 0d  E_STALE 0..../*.
92960 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20  .** Internally, 
92970 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c  the vdbe manipul
92980 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20  ates nearly all 
92990 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65  SQL values as Me
929a0 6d 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  m..** structures
929b0 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75 63  . Each Mem struc
929c0 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c 74  t may cache mult
929d0 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74  iple representat
929e0 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0d 0a 2a  ions (string,..*
929f0 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20  * integer etc.) 
92a00 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  of the same valu
92a10 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 4d  e...*/..struct M
92a20 65 6d 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  em {..  sqlite3 
92a30 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  *db;        /* T
92a40 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61  he associated da
92a50 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
92a60 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 3b  n */..  char *z;
92a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
92a80 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61  tring or BLOB va
92a90 6c 75 65 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c 65  lue */..  double
92aa0 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   r;           /*
92ab0 20 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f 0d 0a   Real value */..
92ac0 20 20 75 6e 69 6f 6e 20 7b 0d 0a 20 20 20 20 69    union {..    i
92ad0 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  64 i;           
92ae0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
92af0 6c 75 65 20 75 73 65 64 20 77 68 65 6e 20 4d 45  lue used when ME
92b00 4d 5f 49 6e 74 20 69 73 20 73 65 74 20 69 6e 20  M_Int is set in 
92b10 66 6c 61 67 73 20 2a 2f 0d 0a 20 20 20 20 69 6e  flags */..    in
92b20 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20  t nZero;        
92b30 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62    /* Used when b
92b40 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73  it MEM_Zero is s
92b50 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0d 0a  et in flags */..
92b60 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65      FuncDef *pDe
92b70 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20  f;      /* Used 
92b80 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d  only when flags=
92b90 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0d 0a 20 20 20  =MEM_Agg */..   
92ba0 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74   RowSet *pRowSet
92bb0 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c  ;    /* Used onl
92bc0 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  y when flags==ME
92bd0 4d 5f 52 6f 77 53 65 74 20 2a 2f 0d 0a 20 20 20  M_RowSet */..   
92be0 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61   VdbeFrame *pFra
92bf0 6d 65 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65  me;  /* Used whe
92c00 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 61  n flags==MEM_Fra
92c10 6d 65 20 2a 2f 0d 0a 20 20 7d 20 75 3b 0d 0a 20  me */..  } u;.. 
92c20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
92c30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
92c40 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  f characters in 
92c50 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 65 78  string value, ex
92c60 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a 2f 0d  cluding '\0' */.
92c70 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20  .  u16 flags;   
92c80 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63         /* Some c
92c90 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d 45  ombination of ME
92ca0 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 2c  M_Null, MEM_Str,
92cb0 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 2a   MEM_Dyn, etc. *
92cc0 2f 0d 0a 20 20 75 38 20 20 74 79 70 65 3b 20 20  /..  u8  type;  
92cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
92ce0 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  of SQLITE_NULL, 
92cf0 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 4c  SQLITE_TEXT, SQL
92d00 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 63  ITE_INTEGER, etc
92d10 20 2a 2f 0d 0a 20 20 75 38 20 20 65 6e 63 3b 20   */..  u8  enc; 
92d20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
92d30 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
92d40 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54  E_UTF16BE, SQLIT
92d50 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0d 0a 23 69  E_UTF16LE */..#i
92d60 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
92d70 47 0d 0a 20 20 4d 65 6d 20 2a 70 53 63 6f 70 79  G..  Mem *pScopy
92d80 46 72 6f 6d 3b 20 20 20 20 2f 2a 20 54 68 69 73  From;    /* This
92d90 20 4d 65 6d 20 69 73 20 61 20 73 68 61 6c 6c 6f   Mem is a shallo
92da0 77 20 63 6f 70 79 20 6f 66 20 70 53 63 6f 70 79  w copy of pScopy
92db0 46 72 6f 6d 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  From */..  void 
92dc0 2a 70 46 69 6c 6c 65 72 3b 20 20 20 20 20 20 2f  *pFiller;      /
92dd0 2a 20 53 6f 20 74 68 61 74 20 73 69 7a 65 6f 66  * So that sizeof
92de0 28 4d 65 6d 29 20 69 73 20 61 20 6d 75 6c 74 69  (Mem) is a multi
92df0 70 6c 65 20 6f 66 20 38 20 2a 2f 0d 0a 23 65 6e  ple of 8 */..#en
92e00 64 69 66 0d 0a 20 20 76 6f 69 64 20 28 2a 78 44  dif..  void (*xD
92e10 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a  el)(void *);  /*
92e20 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61   If not null, ca
92e30 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
92e40 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a   to delete Mem.z
92e50 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4d 61   */..  char *zMa
92e60 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79  lloc;      /* Dy
92e70 6e 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c  namic buffer all
92e80 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
92e90 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0d 0a 7d  3_malloc() */..}
92ea0 3b 0d 0a 0d 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d  ;..../* One or m
92eb0 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ore of the follo
92ec0 77 69 6e 67 20 66 6c 61 67 73 20 61 72 65 20 73  wing flags are s
92ed0 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  et to indicate t
92ee0 68 65 20 76 61 6c 69 64 4f 4b 0d 0a 2a 2a 20 72  he validOK..** r
92ef0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f  epresentations o
92f00 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  f the value stor
92f10 65 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 73 74  ed in the Mem st
92f20 72 75 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  ruct...**..** If
92f30 20 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c   the MEM_Null fl
92f40 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
92f50 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20  the value is an 
92f60 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0d  SQL NULL value..
92f70 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 66 6c 61  .** No other fla
92f80 67 73 20 6d 61 79 20 62 65 20 73 65 74 20 69 6e  gs may be set in
92f90 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d   this case...**.
92fa0 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 53  .** If the MEM_S
92fb0 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  tr flag is set t
92fc0 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73  hen Mem.z points
92fd0 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65 70   at a string rep
92fe0 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0d 0a 2a 2a  resentation...**
92ff0 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73   Usually this is
93000 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
93010 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63  same unicode enc
93020 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69  oding as the mai
93030 6e 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28  n..** database (
93040 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78  see below for ex
93050 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68  ceptions). If th
93060 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20  e MEM_Term flag 
93070 69 73 20 61 6c 73 6f 0d 0a 2a 2a 20 73 65 74 2c  is also..** set,
93080 20 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67   then the string
93090 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74   is nul terminat
930a0 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20  ed. The MEM_Int 
930b0 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0d 0a 2a  and MEM_Real ..*
930c0 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78  * flags may coex
930d0 69 73 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d  ist with the MEM
930e0 5f 53 74 72 20 66 6c 61 67 2e 0d 0a 2a 2f 0d 0a  _Str flag...*/..
930f0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c  #define MEM_Null
93100 20 20 20 20 20 20 30 78 30 30 30 31 20 20 20 2f        0x0001   /
93110 2a 20 56 61 6c 75 65 20 69 73 20 4e 55 4c 4c 20  * Value is NULL 
93120 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f  */..#define MEM_
93130 53 74 72 20 20 20 20 20 20 20 30 78 30 30 30 32  Str       0x0002
93140 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61     /* Value is a
93150 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 23 64 65 66   string */..#def
93160 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20  ine MEM_Int     
93170 20 20 30 78 30 30 30 34 20 20 20 2f 2a 20 56 61    0x0004   /* Va
93180 6c 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65  lue is an intege
93190 72 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45  r */..#define ME
931a0 4d 5f 52 65 61 6c 20 20 20 20 20 20 30 78 30 30  M_Real      0x00
931b0 30 38 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  08   /* Value is
931c0 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 2a   a real number *
931d0 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 42  /..#define MEM_B
931e0 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31 30 20  lob      0x0010 
931f0 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20    /* Value is a 
93200 42 4c 4f 42 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  BLOB */..#define
93210 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 20 30   MEM_RowSet    0
93220 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 75 65  x0020   /* Value
93230 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f 62 6a   is a RowSet obj
93240 65 63 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ect */..#define 
93250 4d 45 4d 5f 46 72 61 6d 65 20 20 20 20 20 30 78  MEM_Frame     0x
93260 30 30 34 30 20 20 20 2f 2a 20 56 61 6c 75 65 20  0040   /* Value 
93270 69 73 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f  is a VdbeFrame o
93280 62 6a 65 63 74 20 2a 2f 0d 0a 23 64 65 66 69 6e  bject */..#defin
93290 65 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 20 20 20  e MEM_Invalid   
932a0 30 78 30 30 38 30 20 20 20 2f 2a 20 56 61 6c 75  0x0080   /* Valu
932b0 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 2a  e is undefined *
932c0 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54  /..#define MEM_T
932d0 79 70 65 4d 61 73 6b 20 20 30 78 30 30 66 66 20  ypeMask  0x00ff 
932e0 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 79 70    /* Mask of typ
932f0 65 20 62 69 74 73 20 2a 2f 0d 0a 0d 0a 2f 2a 20  e bits */..../* 
93300 57 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e  Whenever Mem con
93310 74 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74  tains a valid st
93320 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70  ring or blob rep
93330 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65  resentation, one
93340 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c   of..** the foll
93350 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74  owing flags must
93360 20 62 65 20 73 65 74 20 74 6f 20 64 65 74 65 72   be set to deter
93370 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  mine the memory 
93380 6d 61 6e 61 67 65 6d 65 6e 74 0d 0a 2a 2a 20 70  management..** p
93390 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e  olicy for Mem.z.
933a0 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66    The MEM_Term f
933b0 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 68 65  lag tells us whe
933c0 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0d  ther or not the.
933d0 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c 30  .** string is \0
933e0 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 72  00 or \u0000 ter
933f0 6d 69 6e 61 74 65 64 0d 0a 2a 2f 0d 0a 23 64 65  minated..*/..#de
93400 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20  fine MEM_Term   
93410 20 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53     0x0200   /* S
93420 74 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c  tring rep is nul
93430 20 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0d 0a   terminated */..
93440 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20  #define MEM_Dyn 
93450 20 20 20 20 20 20 30 78 30 34 30 30 20 20 20 2f        0x0400   /
93460 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73  * Need to call s
93470 71 6c 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d  qliteFree() on M
93480 65 6d 2e 7a 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  em.z */..#define
93490 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 20 30   MEM_Static    0
934a0 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a  x0800   /* Mem.z
934b0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 61   points to a sta
934c0 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 23  tic string */..#
934d0 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d  define MEM_Ephem
934e0 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f 2a       0x1000   /*
934f0 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
93500 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   an ephemeral st
93510 72 69 6e 67 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ring */..#define
93520 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30   MEM_Agg       0
93530 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a  x2000   /* Mem.z
93540 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67   points to an ag
93550 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65  g function conte
93560 78 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d  xt */..#define M
93570 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34  EM_Zero      0x4
93580 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63  000   /* Mem.i c
93590 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66  ontains count of
935a0 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   0s appended to 
935b0 62 6c 6f 62 20 2a 2f 0d 0a 23 69 66 64 65 66 20  blob */..#ifdef 
935c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
935d0 42 4c 4f 42 0d 0a 20 20 23 75 6e 64 65 66 20 4d  BLOB..  #undef M
935e0 45 4d 5f 5a 65 72 6f 0d 0a 20 20 23 64 65 66 69  EM_Zero..  #defi
935f0 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30  ne MEM_Zero 0x00
93600 30 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  00..#endif..../*
93610 0d 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 65  ..** Clear any e
93620 78 69 73 74 69 6e 67 20 74 79 70 65 20 66 6c 61  xisting type fla
93630 67 73 20 66 72 6f 6d 20 61 20 4d 65 6d 20 61 6e  gs from a Mem an
93640 64 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 77  d replace them w
93650 69 74 68 20 66 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ith f..*/..#defi
93660 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  ne MemSetTypeFla
93670 67 28 70 2c 20 66 29 20 5c 0d 0a 20 20 20 28 28  g(p, f) \..   ((
93680 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 29  p)->flags = ((p)
93690 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 79  ->flags&~(MEM_Ty
936a0 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f 29  peMask|MEM_Zero)
936b0 29 7c 66 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  )|f)..../*..** R
936c0 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 20  eturn true if a 
936d0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e  memory cell is n
936e0 6f 74 20 6d 61 72 6b 65 64 20 61 73 20 69 6e 76  ot marked as inv
936f0 61 6c 69 64 2e 20 20 54 68 69 73 20 6d 61 63 72  alid.  This macr
93700 6f 0d 0a 2a 2a 20 69 73 20 66 6f 72 20 75 73 65  o..** is for use
93710 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
93720 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
93730 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
93740 4c 49 54 45 5f 44 45 42 55 47 0d 0a 23 64 65 66  LITE_DEBUG..#def
93750 69 6e 65 20 6d 65 6d 49 73 56 61 6c 69 64 28 4d  ine memIsValid(M
93760 29 20 20 28 28 4d 29 2d 3e 66 6c 61 67 73 20 26  )  ((M)->flags &
93770 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 29 3d 3d 30   MEM_Invalid)==0
93780 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a  ..#endif....../*
93790 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a   A VdbeFunc is j
937a0 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64  ust a FuncDef (d
937b0 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
937c0 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74  Int.h) that cont
937d0 61 69 6e 73 0d 0a 2a 2a 20 61 64 64 69 74 69 6f  ains..** additio
937e0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
937f0 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20  about auxiliary 
93800 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e  information boun
93810 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0d 0a  d to arguments..
93820 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  ** of the functi
93830 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  on.  This is use
93840 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
93850 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  he sqlite3_get_a
93860 75 78 64 61 74 61 28 29 0d 0a 2a 2a 20 61 6e 64  uxdata()..** and
93870 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
93880 64 61 74 61 28 29 20 41 50 49 73 2e 20 20 54 68  data() APIs.  Th
93890 65 20 22 61 75 78 64 61 74 61 22 20 69 73 20 73  e "auxdata" is s
938a0 6f 6d 65 20 61 75 78 69 6c 69 61 72 79 20 64 61  ome auxiliary da
938b0 74 61 0d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  ta..** that can 
938c0 62 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  be associated wi
938d0 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 72  th a constant ar
938e0 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63  gument to a func
938f0 74 69 6f 6e 2e 20 20 54 68 69 73 0d 0a 2a 2a 20  tion.  This..** 
93900 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f 6e 73  allows functions
93910 20 73 75 63 68 20 61 73 20 22 72 65 67 65 78 70   such as "regexp
93920 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74 68 65  " to compile the
93930 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65 67 75  ir constant regu
93940 6c 61 72 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69  lar..** expressi
93950 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63 65  on argument once
93960 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65 20   and reused the 
93970 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66 6f  compiled code fo
93980 72 20 6d 75 6c 74 69 70 6c 65 0d 0a 2a 2a 20 69  r multiple..** i
93990 6e 76 6f 63 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d  nvocations...*/.
939a0 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 6e 63  .struct VdbeFunc
939b0 20 7b 0d 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   {..  FuncDef *p
939c0 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Func;           
939d0 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e      /* The defin
939e0 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e  ition of the fun
939f0 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ction */..  int 
93a00 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  nAux;           
93a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
93a20 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
93a30 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41  llocated for apA
93a40 75 78 5b 5d 20 2a 2f 0d 0a 20 20 73 74 72 75 63  ux[] */..  struc
93a50 74 20 41 75 78 44 61 74 61 20 7b 0d 0a 20 20 20  t AuxData {..   
93a60 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20   void *pAux;    
93a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
93a80 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72 20 74  * Aux data for t
93a90 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74  he i-th argument
93aa0 20 2a 2f 0d 0a 20 20 20 20 76 6f 69 64 20 28 2a   */..    void (*
93ab0 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a 29  xDelete)(void *)
93ac0 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75  ;      /* Destru
93ad0 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75 78  ctor for the aux
93ae0 20 64 61 74 61 20 2a 2f 0d 0a 20 20 7d 20 61 70   data */..  } ap
93af0 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  Aux[1];         
93b00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
93b10 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66   slot for each f
93b20 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
93b30 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
93b40 2a 20 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20  * The "context" 
93b50 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69  argument for a i
93b60 6e 73 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74  nstallable funct
93b70 69 6f 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20  ion.  A pointer 
93b80 74 6f 20 61 6e 0d 0a 2a 2a 20 69 6e 73 74 61 6e  to an..** instan
93b90 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
93ba0 74 75 72 65 20 69 73 20 74 68 65 20 66 69 72 73  ture is the firs
93bb0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
93bc0 65 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 0d  e routines used.
93bd0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  .** implement th
93be0 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e  e SQL functions.
93bf0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 69  ..**..** There i
93c00 73 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20  s a typedef for 
93c10 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
93c20 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20  n sqlite.h.  So 
93c30 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0d 0a 2a  all routines,..*
93c40 2a 20 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69  * even the publi
93c50 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53  c interface to S
93c60 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61  QLite, can use a
93c70 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
93c80 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 20   structure...** 
93c90 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 69 73  But this file is
93ca0 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20   the only place 
93cb0 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 72 6e  where the intern
93cc0 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  al details of th
93cd0 69 73 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  is..** structure
93ce0 20 61 72 65 20 6b 6e 6f 77 6e 2e 0d 0a 2a 2a 0d   are known...**.
93cf0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
93d00 72 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e  re is defined in
93d10 73 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e  side of vdbeInt.
93d20 68 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65  h because it use
93d30 73 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0d  s substructures.
93d40 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20  .** (Mem) which 
93d50 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64  are only defined
93d60 20 74 68 65 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 72   there...*/..str
93d70 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  uct sqlite3_cont
93d80 65 78 74 20 7b 0d 0a 20 20 46 75 6e 63 44 65 66  ext {..  FuncDef
93d90 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f   *pFunc;       /
93da0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e  * Pointer to fun
93db0 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ction informatio
93dc0 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53  n.  MUST BE FIRS
93dd0 54 20 2a 2f 0d 0a 20 20 56 64 62 65 46 75 6e 63  T */..  VdbeFunc
93de0 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a   *pVdbeFunc;  /*
93df0 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20   Auxilary data, 
93e00 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0d 0a  if created. */..
93e10 20 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20    Mem s;        
93e20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
93e30 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73  eturn value is s
93e40 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0d 0a 20  tored here */.. 
93e50 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20   Mem *pMem;     
93e60 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
93e70 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74   cell used to st
93e80 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 63 6f  ore aggregate co
93e90 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  ntext */..  int 
93ea0 69 73 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20  isError;        
93eb0 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20    /* Error code 
93ec0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
93ed0 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0d 0a 20 20  function. */..  
93ee0 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
93ef0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
93f00 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0d 0a  ng sequence */..
93f10 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20  };..../*..** An 
93f20 45 78 70 6c 61 69 6e 20 6f 62 6a 65 63 74 20 61  Explain object a
93f30 63 63 75 6d 75 6c 61 74 65 73 20 69 6e 64 65 6e  ccumulates inden
93f40 74 65 64 20 6f 75 74 70 75 74 20 77 68 69 63 68  ted output which
93f50 20 69 73 20 68 65 6c 70 66 75 6c 0d 0a 2a 2a 20   is helpful..** 
93f60 69 6e 20 64 65 73 63 72 69 62 69 6e 67 20 72 65  in describing re
93f70 63 75 72 73 69 76 65 20 64 61 74 61 20 73 74 72  cursive data str
93f80 75 63 74 75 72 65 73 2e 0d 0a 2a 2f 0d 0a 73 74  uctures...*/..st
93f90 72 75 63 74 20 45 78 70 6c 61 69 6e 20 7b 0d 0a  ruct Explain {..
93fa0 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20    Vdbe *pVdbe;  
93fb0 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
93fc0 68 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 74  he explanation t
93fd0 6f 20 74 68 69 73 20 56 64 62 65 20 2a 2f 0d 0a  o this Vdbe */..
93fe0 20 20 53 74 72 41 63 63 75 6d 20 73 74 72 3b 20    StrAccum str; 
93ff0 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69       /* The stri
94000 6e 67 20 62 65 69 6e 67 20 61 63 63 75 6d 75 6c  ng being accumul
94010 61 74 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ated */..  int n
94020 49 6e 64 65 6e 74 3b 20 20 20 20 20 20 20 2f 2a  Indent;       /*
94030 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
94040 6e 74 73 20 69 6e 20 61 49 6e 64 65 6e 74 20 2a  nts in aIndent *
94050 2f 0d 0a 20 20 75 31 36 20 61 49 6e 64 65 6e 74  /..  u16 aIndent
94060 5b 31 30 30 5d 3b 20 20 2f 2a 20 4c 65 76 65 6c  [100];  /* Level
94070 73 20 6f 66 20 69 6e 64 65 6e 74 61 74 69 6f 6e  s of indentation
94080 20 2a 2f 0d 0a 20 20 63 68 61 72 20 7a 42 61 73   */..  char zBas
94090 65 5b 31 30 30 5d 3b 20 20 20 2f 2a 20 49 6e 69  e[100];   /* Ini
940a0 74 69 61 6c 20 73 70 61 63 65 20 2a 2f 0d 0a 7d  tial space */..}
940b0 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69  ;..../*..** An i
940c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 76  nstance of the v
940d0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
940e0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
940f0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d  contains the com
94100 70 6c 65 74 65 0d 0a 2a 2a 20 73 74 61 74 65 20  plete..** state 
94110 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
94120 61 63 68 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  achine...**..** 
94130 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d  The "sqlite3_stm
94140 74 22 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  t" structure poi
94150 6e 74 65 72 20 74 68 61 74 20 69 73 20 72 65 74  nter that is ret
94160 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
94170 5f 70 72 65 70 61 72 65 28 29 0d 0a 2a 2a 20 69  _prepare()..** i
94180 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74  s really a point
94190 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
941a0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
941b0 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ure...**..** The
941c0 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68   Vdbe.inVtabMeth
941d0 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  od variable is s
941e0 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66  et to non-zero f
941f0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
94200 6f 66 0d 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75  of..** any virtu
94210 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20  al table method 
94220 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65  invocations made
94230 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f   by the vdbe pro
94240 67 72 61 6d 2e 20 49 74 20 69 73 0d 0a 2a 2a 20  gram. It is..** 
94250 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65  set to 2 for xDe
94260 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c  stroy method cal
94270 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c  ls and 1 for all
94280 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20   other methods. 
94290 54 68 69 73 0d 0a 2a 2a 20 76 61 72 69 61 62 6c  This..** variabl
942a0 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77  e is used for tw
942b0 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61  o purposes: to a
942c0 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65  llow xDestroy me
942d0 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65  thods to execute
942e0 0d 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45  ..** "DROP TABLE
942f0 22 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64  " statements and
94300 20 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65   to prevent some
94310 20 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 65   nasty side effe
94320 63 74 73 20 6f 66 0d 0a 2a 2a 20 6d 61 6c 6c 6f  cts of..** mallo
94330 63 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53  c failure when S
94340 51 4c 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64  QLite is invoked
94350 20 72 65 63 75 72 73 69 76 65 6c 79 20 62 79 20   recursively by 
94360 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
94370 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63  ..** method func
94380 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  tion...*/..struc
94390 74 20 56 64 62 65 20 7b 0d 0a 20 20 73 71 6c 69  t Vdbe {..  sqli
943a0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
943b0 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
943c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
943d0 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20 73 74  hat owns this st
943e0 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 4f 70  atement */..  Op
943f0 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
94400 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74        /* Space t
94410 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74 75  o hold the virtu
94420 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72 6f  al machine's pro
94430 67 72 61 6d 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a  gram */..  Mem *
94440 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  aMem;           
94450 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79     /* The memory
94460 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20   locations */.. 
94470 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20   Mem **apArg;   
94480 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
94490 6d 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74  ments to current
944a0 6c 79 20 65 78 65 63 75 74 69 6e 67 20 75 73 65  ly executing use
944b0 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20  r function */.. 
944c0 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20   Mem *aColName; 
944d0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
944e0 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 75  mn names to retu
944f0 72 6e 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 52  rn */..  Mem *pR
94500 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20  esultSet;       
94510 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
94520 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c  n array of resul
94530 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65  ts */..  int nMe
94540 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
94550 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
94560 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63  mory locations c
94570 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74  urrently allocat
94580 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 70  ed */..  int nOp
94590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
945a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e   /* Number of in
945b0 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68  structions in th
945c0 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20 20  e program */..  
945d0 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20  int nOpAlloc;   
945e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
945f0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
94600 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a  ated for aOp[] *
94610 2f 0d 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b  /..  int nLabel;
94620 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
94630 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65 6c 73  Number of labels
94640 20 75 73 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20   used */..  int 
94650 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20 20  nLabelAlloc;    
94660 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
94670 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64   slots allocated
94680 20 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0d   in aLabel[] */.
94690 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20  .  int *aLabel; 
946a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
946b0 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
946c0 6c 61 62 65 6c 73 20 2a 2f 0d 0a 20 20 75 31 36  labels */..  u16
946d0 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20   nResColumn;    
946e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
946f0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65  f columns in one
94700 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75   row of the resu
94710 6c 74 20 73 65 74 20 2a 2f 0d 0a 20 20 75 31 36  lt set */..  u16
94720 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20   nCursor;       
94730 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
94740 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43 73 72  f slots in apCsr
94750 5b 5d 20 2a 2f 0d 0a 20 20 75 33 32 20 6d 61 67  [] */..  u32 mag
94760 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ic;             
94770 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72   /* Magic number
94780 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63   for sanity chec
94790 6b 69 6e 67 20 2a 2f 0d 0a 20 20 63 68 61 72 20  king */..  char 
947a0 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
947b0 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
947c0 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65  age written here
947d0 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 70 50 72   */..  Vdbe *pPr
947e0 65 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f  ev,*pNext;     /
947f0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * Linked list of
94800 20 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20   VDBEs with the 
94810 73 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0d  same Vdbe.db */.
94820 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a  .  VdbeCursor **
94830 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f 6e  apCsr;     /* On
94840 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69  e element of thi
94850 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 63 68  s array for each
94860 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0d   open cursor */.
94870 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20  .  Mem *aVar;   
94880 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
94890 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f  lues for the OP_
948a0 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e  Variable opcode.
948b0 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 61 7a   */..  char **az
948c0 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
948d0 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61 62  * Name of variab
948e0 6c 65 73 20 2a 2f 0d 0a 20 20 79 6e 56 61 72 20  les */..  ynVar 
948f0 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20  nVar;           
94900 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
94910 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d  ntries in aVar[]
94920 20 2a 2f 0d 0a 20 20 79 6e 56 61 72 20 6e 7a 56   */..  ynVar nzV
94930 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ar;            /
94940 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
94950 69 65 73 20 69 6e 20 61 7a 56 61 72 5b 5d 20 2a  ies in azVar[] *
94960 2f 0d 0a 20 20 75 33 32 20 63 61 63 68 65 43 74  /..  u32 cacheCt
94970 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
94980 56 64 62 65 43 75 72 73 6f 72 20 72 6f 77 20 63  VdbeCursor row c
94990 61 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20  ache generation 
949a0 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  counter */..  in
949b0 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20  t pc;           
949c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f        /* The pro
949d0 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0d  gram counter */.
949e0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
949f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
94a00 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
94a10 0d 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69  ..  u8 errorActi
94a20 6f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  on;         /* R
94a30 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74  ecovery action t
94a40 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20  o do in case of 
94a50 61 6e 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 75  an error */..  u
94a60 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
94a70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
94a80 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
94a90 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
94aa0 20 2a 2f 0d 0a 20 20 75 38 20 63 68 61 6e 67 65   */..  u8 change
94ab0 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f  CntOn;         /
94ac0 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65  * True to update
94ad0 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
94ae0 74 65 72 20 2a 2f 0d 0a 20 20 75 38 20 65 78 70  ter */..  u8 exp
94af0 69 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  ired;           
94b00 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
94b10 20 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20   VM needs to be 
94b20 72 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0d 0a 20  recompiled */.. 
94b30 20 75 38 20 72 75 6e 4f 6e 6c 79 4f 6e 63 65 3b   u8 runOnlyOnce;
94b40 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 6f           /* Auto
94b50 6d 61 74 69 63 61 6c 6c 79 20 65 78 70 69 72 65  matically expire
94b60 20 6f 6e 20 72 65 73 65 74 20 2a 2f 0d 0a 20 20   on reset */..  
94b70 75 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  u8 minWriteFileF
94b80 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d  ormat;  /* Minim
94b90 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66  um file format f
94ba0 6f 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 61  or writable data
94bb0 62 61 73 65 20 66 69 6c 65 73 20 2a 2f 0d 0a 20  base files */.. 
94bc0 20 75 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64   u8 inVtabMethod
94bd0 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20  ;        /* See 
94be0 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a  comments above *
94bf0 2f 0d 0a 20 20 75 38 20 75 73 65 73 53 74 6d 74  /..  u8 usesStmt
94c00 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20  Journal;     /* 
94c10 54 72 75 65 20 69 66 20 75 73 65 73 20 61 20 73  True if uses a s
94c20 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
94c30 20 2a 2f 0d 0a 20 20 75 38 20 72 65 61 64 4f 6e   */..  u8 readOn
94c40 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ly;            /
94c50 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61 64 2d  * True for read-
94c60 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20  only statements 
94c70 2a 2f 0d 0a 20 20 75 38 20 69 73 50 72 65 70 61  */..  u8 isPrepa
94c80 72 65 56 32 3b 20 20 20 20 20 20 20 20 20 2f 2a  reV2;         /*
94c90 20 54 72 75 65 20 69 66 20 70 72 65 70 61 72 65   True if prepare
94ca0 64 20 77 69 74 68 20 70 72 65 70 61 72 65 5f 76  d with prepare_v
94cb0 32 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43  2() */..  int nC
94cc0 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20  hange;          
94cd0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
94ce0 62 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73  b changes made s
94cf0 69 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20  ince last reset 
94d00 2a 2f 0d 0a 20 20 79 44 62 4d 61 73 6b 20 62 74  */..  yDbMask bt
94d10 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a  reeMask;      /*
94d20 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d 3e   Bitmask of db->
94d30 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 65  aDb[] entries re
94d40 66 65 72 65 6e 63 65 64 20 2a 2f 0d 0a 20 20 79  ferenced */..  y
94d50 44 62 4d 61 73 6b 20 6c 6f 63 6b 4d 61 73 6b 3b  DbMask lockMask;
94d60 20 20 20 20 20 20 20 2f 2a 20 53 75 62 73 65 74         /* Subset
94d70 20 6f 66 20 62 74 72 65 65 4d 61 73 6b 20 74 68   of btreeMask th
94d80 61 74 20 72 65 71 75 69 72 65 73 20 61 20 6c 6f  at requires a lo
94d90 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 53 74  ck */..  int iSt
94da0 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  atement;        
94db0 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75   /* Statement nu
94dc0 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61  mber (or 0 if ha
94dd0 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d  s not opened stm
94de0 74 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 43 6f  t) */..  int aCo
94df0 75 6e 74 65 72 5b 33 5d 3b 20 20 20 20 20 20 20  unter[3];       
94e00 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65   /* Counters use
94e10 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d  d by sqlite3_stm
94e20 74 5f 73 74 61 74 75 73 28 29 20 2a 2f 0d 0a 23  t_status() */..#
94e30 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
94e40 49 54 5f 54 52 41 43 45 0d 0a 20 20 69 36 34 20  IT_TRACE..  i64 
94e50 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20  startTime;      
94e60 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e      /* Time when
94e70 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d   query started -
94e80 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c   used for profil
94e90 69 6e 67 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a  ing */..#endif..
94ea0 20 20 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61    i64 nFkConstra
94eb0 69 6e 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  int;      /* Num
94ec0 62 65 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63  ber of imm. FK c
94ed0 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 69 73 20  onstraints this 
94ee0 56 4d 20 2a 2f 0d 0a 20 20 69 36 34 20 6e 53 74  VM */..  i64 nSt
94ef0 6d 74 44 65 66 43 6f 6e 73 3b 20 20 20 20 20 20  mtDefCons;      
94f00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65   /* Number of de
94f10 66 2e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77  f. constraints w
94f20 68 65 6e 20 73 74 6d 74 20 73 74 61 72 74 65 64  hen stmt started
94f30 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 53 71   */..  char *zSq
94f40 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
94f50 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51  * Text of the SQ
94f60 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
94f70 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20   generated this 
94f80 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 46 72 65  */..  void *pFre
94f90 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
94fa0 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e 20   Free this when 
94fb0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62  deleting the vdb
94fc0 65 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c  e */..#ifdef SQL
94fd0 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 46 49 4c  ITE_DEBUG..  FIL
94fe0 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20 20  E *trace;       
94ff0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e       /* Write an
95000 20 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65   execution trace
95010 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55   here, if not NU
95020 4c 4c 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23  LL */..#endif..#
95030 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
95040 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e  BLE_TREE_EXPLAIN
95050 0d 0a 20 20 45 78 70 6c 61 69 6e 20 2a 70 45 78  ..  Explain *pEx
95060 70 6c 61 69 6e 3b 20 20 20 20 20 20 2f 2a 20 54  plain;      /* T
95070 68 65 20 65 78 70 6c 61 69 6e 65 72 20 2a 2f 0d  he explainer */.
95080 0a 20 20 63 68 61 72 20 2a 7a 45 78 70 6c 61 69  .  char *zExplai
95090 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78  n;         /* Ex
950a0 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 64 61 74  planation of dat
950b0 61 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0d  a structures */.
950c0 0a 23 65 6e 64 69 66 0d 0a 20 20 56 64 62 65 46  .#endif..  VdbeF
950d0 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20  rame *pFrame;   
950e0 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 61     /* Parent fra
950f0 6d 65 20 2a 2f 0d 0a 20 20 56 64 62 65 46 72 61  me */..  VdbeFra
95100 6d 65 20 2a 70 44 65 6c 46 72 61 6d 65 3b 20 20  me *pDelFrame;  
95110 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 72 61 6d   /* List of fram
95120 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 66 72 65  e objects to fre
95130 65 20 6f 6e 20 56 4d 20 72 65 73 65 74 20 2a 2f  e on VM reset */
95140 0d 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b 20  ..  int nFrame; 
95150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
95160 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20  umber of frames 
95170 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 20 2a  in pFrame list *
95180 2f 0d 0a 20 20 75 33 32 20 65 78 70 6d 61 73 6b  /..  u32 expmask
95190 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
951a0 42 69 6e 64 69 6e 67 20 74 6f 20 74 68 65 73 65  Binding to these
951b0 20 76 61 72 73 20 69 6e 76 61 6c 69 64 61 74 65   vars invalidate
951c0 73 20 56 4d 20 2a 2f 0d 0a 20 20 53 75 62 50 72  s VM */..  SubPr
951d0 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b  ogram *pProgram;
951e0 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73     /* Linked lis
951f0 74 20 6f 66 20 61 6c 6c 20 73 75 62 2d 70 72 6f  t of all sub-pro
95200 67 72 61 6d 73 20 75 73 65 64 20 62 79 20 56 4d  grams used by VM
95210 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 6e 63 65   */..  int nOnce
95220 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20 2f  Flag;          /
95230 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20  * Size of array 
95240 61 4f 6e 63 65 46 6c 61 67 5b 5d 20 2a 2f 0d 0a  aOnceFlag[] */..
95250 20 20 75 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b    u8 *aOnceFlag;
95260 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
95270 67 73 20 66 6f 72 20 4f 50 5f 4f 6e 63 65 20 2a  gs for OP_Once *
95280 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  /..};..../*..** 
95290 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  The following ar
952a0 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  e allowed values
952b0 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0d   for Vdbe.magic.
952c0 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 56 44 42  .*/..#define VDB
952d0 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20  E_MAGIC_INIT    
952e0 20 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f   0x26bceaa5    /
952f0 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42  * Building a VDB
95300 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 23 64  E program */..#d
95310 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43  efine VDBE_MAGIC
95320 5f 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 32  _RUN      0xbdf2
95330 30 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20  0da3    /* VDBE 
95340 69 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 63  is ready to exec
95350 75 74 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ute */..#define 
95360 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
95370 20 20 20 20 30 78 35 31 39 63 32 39 37 33 20 20      0x519c2973  
95380 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f    /* VDBE has co
95390 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f  mpleted executio
953a0 6e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 56 44  n */..#define VD
953b0 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20 20  BE_MAGIC_DEAD   
953c0 20 20 30 78 62 36 30 36 63 33 63 38 20 20 20 20    0xb606c3c8    
953d0 2f 2a 20 54 68 65 20 56 44 42 45 20 68 61 73 20  /* The VDBE has 
953e0 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64  been deallocated
953f0 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 75   */..../*..** Fu
95400 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65  nction prototype
95410 73 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  s..*/..SQLITE_PR
95420 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
95430 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
95440 28 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72  (Vdbe *, VdbeCur
95450 73 6f 72 2a 29 3b 0d 0a 76 6f 69 64 20 73 71 6c  sor*);..void sql
95460 69 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28  iteVdbePopStack(
95470 56 64 62 65 2a 2c 69 6e 74 29 3b 0d 0a 53 51 4c  Vdbe*,int);..SQL
95480 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
95490 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
954a0 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73  rMoveto(VdbeCurs
954b0 6f 72 2a 29 3b 0d 0a 23 69 66 20 64 65 66 69 6e  or*);..#if defin
954c0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
954d0 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45   || defined(VDBE
954e0 5f 50 52 4f 46 49 4c 45 29 0d 0a 53 51 4c 49 54  _PROFILE)..SQLIT
954f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
95500 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
95510 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70  p(FILE*, int, Op
95520 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 53 51 4c  *);..#endif..SQL
95530 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
95540 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
95550 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0d 0a  lTypeLen(u32);..
95560 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
95570 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
95580 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69  rialType(Mem*, i
95590 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
955a0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
955b0 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e  VdbeSerialPut(un
955c0 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e  signed char*, in
955d0 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0d 0a  t, Mem*, int);..
955e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
955f0 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
95600 72 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e  rialGet(const un
95610 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33  signed char*, u3
95620 32 2c 20 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54  2, Mem*);..SQLIT
95630 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
95640 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
95650 41 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63  AuxData(VdbeFunc
95660 2a 2c 20 69 6e 74 29 3b 0d 0a 0d 0a 69 6e 74 20  *, int);....int 
95670 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65 79 43  sqlite2BtreeKeyC
95680 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f 72 20  ompare(BtCursor 
95690 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c  *, const void *,
956a0 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a   int, int, int *
956b0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
956c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
956d0 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28  beIdxKeyCompare(
956e0 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e 70 61  VdbeCursor*,Unpa
956f0 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e 74 2a  ckedRecord*,int*
95700 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
95710 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
95720 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74  beIdxRowid(sqlit
95730 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 2a 2c  e3*, BtCursor *,
95740 20 69 36 34 20 2a 29 3b 0d 0a 53 51 4c 49 54 45   i64 *);..SQLITE
95750 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
95760 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63  ite3MemCompare(c
95770 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74  onst Mem*, const
95780 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c   Mem*, const Col
95790 6c 53 65 71 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  lSeq*);..SQLITE_
957a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
957b0 74 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65  te3VdbeExec(Vdbe
957c0 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
957d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
957e0 64 62 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0d  dbeList(Vdbe*);.
957f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
95800 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 48  int sqlite3VdbeH
95810 61 6c 74 28 56 64 62 65 2a 29 3b 0d 0a 53 51 4c  alt(Vdbe*);..SQL
95820 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
95830 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
95840 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c  eEncoding(Mem *,
95850 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
95860 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
95870 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
95880 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  Mem*);..SQLITE_P
95890 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
958a0 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65  e3VdbeMemCopy(Me
958b0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b  m*, const Mem*);
958c0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
958d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
958e0 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
958f0 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a  Mem*, const Mem*
95900 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
95910 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
95920 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
95930 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0d 0a 53 51  Mem*, Mem*);..SQ
95940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
95950 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
95960 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a  ulTerminate(Mem*
95970 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
95980 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
95990 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a  beMemSetStr(Mem*
959a0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
959b0 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28  nt, u8, void(*)(
959c0 76 6f 69 64 2a 29 29 3b 0d 0a 53 51 4c 49 54 45  void*));..SQLITE
959d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
959e0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
959f0 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b  nt64(Mem*, i64);
95a00 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
95a10 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
95a20 49 4e 54 0d 0a 23 20 64 65 66 69 6e 65 20 73 71  INT..# define sq
95a30 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
95a40 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62  ouble sqlite3Vdb
95a50 65 4d 65 6d 53 65 74 49 6e 74 36 34 0d 0a 23 65  eMemSetInt64..#e
95a60 6c 73 65 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  lse..SQLITE_PRIV
95a70 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
95a80 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62  e3VdbeMemSetDoub
95a90 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29  le(Mem*, double)
95aa0 3b 0d 0a 23 65 6e 64 69 66 0d 0a 53 51 4c 49 54  ;..#endif..SQLIT
95ab0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
95ac0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
95ad0 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0d 0a 53 51 4c  Null(Mem*);..SQL
95ae0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
95af0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
95b00 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c  etZeroBlob(Mem*,
95b10 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
95b20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
95b30 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
95b40 65 74 28 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54  et(Mem*);..SQLIT
95b50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
95b60 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
95b70 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b  Writeable(Mem*);
95b80 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
95b90 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
95ba0 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d  MemStringify(Mem
95bb0 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
95bc0 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c  _PRIVATE i64 sql
95bd0 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
95be0 28 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  (Mem*);..SQLITE_
95bf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
95c00 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
95c10 72 69 66 79 28 4d 65 6d 2a 29 3b 0d 0a 53 51 4c  rify(Mem*);..SQL
95c20 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62  ITE_PRIVATE doub
95c30 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  le sqlite3VdbeRe
95c40 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0d 0a  alValue(Mem*);..
95c50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
95c60 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
95c70 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d  ntegerAffinity(M
95c80 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  em*);..SQLITE_PR
95c90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
95ca0 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28  3VdbeMemRealify(
95cb0 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50  Mem*);..SQLITE_P
95cc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
95cd0 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66  e3VdbeMemNumerif
95ce0 79 28 4d 65 6d 2a 29 3b 0d 0a 53 51 4c 49 54 45  y(Mem*);..SQLITE
95cf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
95d00 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
95d10 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a 2c 69  tree(BtCursor*,i
95d20 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29  nt,int,int,Mem*)
95d30 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
95d40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
95d50 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d  beMemRelease(Mem
95d60 20 2a 70 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52   *p);..SQLITE_PR
95d70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
95d80 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
95d90 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29  External(Mem *p)
95da0 3b 0d 0a 23 64 65 66 69 6e 65 20 56 64 62 65 4d  ;..#define VdbeM
95db0 65 6d 52 65 6c 65 61 73 65 28 58 29 20 20 5c 0d  emRelease(X)  \.
95dc0 0a 20 20 69 66 28 28 58 29 2d 3e 66 6c 61 67 73  .  if((X)->flags
95dd0 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79  &(MEM_Agg|MEM_Dy
95de0 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d  n|MEM_RowSet|MEM
95df0 5f 46 72 61 6d 65 29 29 20 5c 0d 0a 20 20 20 20  _Frame)) \..    
95e00 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
95e10 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 58 29  leaseExternal(X)
95e20 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
95e30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
95e40 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d  eMemFinalize(Mem
95e50 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0d 0a 53  *, FuncDef*);..S
95e60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
95e70 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
95e80 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29  3OpcodeName(int)
95e90 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
95ea0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
95eb0 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d  eMemGrow(Mem *pM
95ec0 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70  em, int n, int p
95ed0 72 65 73 65 72 76 65 29 3b 0d 0a 53 51 4c 49 54  reserve);..SQLIT
95ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
95ef0 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
95f00 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 2c 20  atement(Vdbe *, 
95f10 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
95f20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
95f30 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c 65 74  e3VdbeFrameDelet
95f40 65 28 56 64 62 65 46 72 61 6d 65 2a 29 3b 0d 0a  e(VdbeFrame*);..
95f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
95f60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  nt sqlite3VdbeFr
95f70 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46  ameRestore(VdbeF
95f80 72 61 6d 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45  rame *);..SQLITE
95f90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
95fa0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
95fb0 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 29  eType(Mem *pMem)
95fc0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
95fd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
95fe0 65 54 72 61 6e 73 66 65 72 45 72 72 6f 72 28 56  eTransferError(V
95ff0 64 62 65 20 2a 70 29 3b 0d 0a 0d 0a 23 69 66 64  dbe *p);....#ifd
96000 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
96010 45 52 47 45 5f 53 4f 52 54 0d 0a 23 20 64 65 66  ERGE_SORT..# def
96020 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 53  ine sqlite3VdbeS
96030 6f 72 74 65 72 49 6e 69 74 28 59 2c 5a 29 20 20  orterInit(Y,Z)  
96040 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 23      SQLITE_OK..#
96050 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
96060 64 62 65 53 6f 72 74 65 72 57 72 69 74 65 28 58  dbeSorterWrite(X
96070 2c 59 2c 5a 29 20 20 20 53 51 4c 49 54 45 5f 4f  ,Y,Z)   SQLITE_O
96080 4b 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  K..# define sqli
96090 74 65 33 56 64 62 65 53 6f 72 74 65 72 43 6c 6f  te3VdbeSorterClo
960a0 73 65 28 59 2c 5a 29 0d 0a 23 20 64 65 66 69 6e  se(Y,Z)..# defin
960b0 65 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72  e sqlite3VdbeSor
960c0 74 65 72 52 6f 77 6b 65 79 28 59 2c 5a 29 20 20  terRowkey(Y,Z)  
960d0 20 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 23 20 64    SQLITE_OK..# d
960e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62  efine sqlite3Vdb
960f0 65 53 6f 72 74 65 72 52 65 77 69 6e 64 28 58 2c  eSorterRewind(X,
96100 59 2c 5a 29 20 20 53 51 4c 49 54 45 5f 4f 4b 0d  Y,Z)  SQLITE_OK.
96110 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
96120 33 56 64 62 65 53 6f 72 74 65 72 4e 65 78 74 28  3VdbeSorterNext(
96130 58 2c 59 2c 5a 29 20 20 20 20 53 51 4c 49 54 45  X,Y,Z)    SQLITE
96140 5f 4f 4b 0d 0a 23 20 64 65 66 69 6e 65 20 73 71  _OK..# define sq
96150 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 43  lite3VdbeSorterC
96160 6f 6d 70 61 72 65 28 58 2c 59 2c 5a 29 20 53 51  ompare(X,Y,Z) SQ
96170 4c 49 54 45 5f 4f 4b 0d 0a 23 65 6c 73 65 0d 0a  LITE_OK..#else..
96180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
96190 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  nt sqlite3VdbeSo
961a0 72 74 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33  rterInit(sqlite3
961b0 20 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 20 2a   *, VdbeCursor *
961c0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
961d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
961e0 64 62 65 53 6f 72 74 65 72 43 6c 6f 73 65 28 73  dbeSorterClose(s
961f0 71 6c 69 74 65 33 20 2a 2c 20 56 64 62 65 43 75  qlite3 *, VdbeCu
96200 72 73 6f 72 20 2a 29 3b 0d 0a 53 51 4c 49 54 45  rsor *);..SQLITE
96210 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
96220 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 52 6f  ite3VdbeSorterRo
96230 77 6b 65 79 28 56 64 62 65 43 75 72 73 6f 72 20  wkey(VdbeCursor 
96240 2a 2c 20 4d 65 6d 20 2a 29 3b 0d 0a 53 51 4c 49  *, Mem *);..SQLI
96250 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
96260 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72  qlite3VdbeSorter
96270 4e 65 78 74 28 73 71 6c 69 74 65 33 20 2a 2c 20  Next(sqlite3 *, 
96280 56 64 62 65 43 75 72 73 6f 72 20 2a 2c 20 69 6e  VdbeCursor *, in
96290 74 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  t *);..SQLITE_PR
962a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
962b0 33 56 64 62 65 53 6f 72 74 65 72 52 65 77 69 6e  3VdbeSorterRewin
962c0 64 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 64 62  d(sqlite3 *, Vdb
962d0 65 43 75 72 73 6f 72 20 2a 2c 20 69 6e 74 20 2a  eCursor *, int *
962e0 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
962f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
96300 62 65 53 6f 72 74 65 72 57 72 69 74 65 28 73 71  beSorterWrite(sq
96310 6c 69 74 65 33 20 2a 2c 20 56 64 62 65 43 75 72  lite3 *, VdbeCur
96320 73 6f 72 20 2a 2c 20 4d 65 6d 20 2a 29 3b 0d 0a  sor *, Mem *);..
96330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
96340 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  nt sqlite3VdbeSo
96350 72 74 65 72 43 6f 6d 70 61 72 65 28 56 64 62 65  rterCompare(Vdbe
96360 43 75 72 73 6f 72 20 2a 2c 20 4d 65 6d 20 2a 2c  Cursor *, Mem *,
96370 20 69 6e 74 20 2a 29 3b 0d 0a 23 65 6e 64 69 66   int *);..#endif
96380 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
96390 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
963a0 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51  RED_CACHE) && SQ
963b0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3e  LITE_THREADSAFE>
963c0 30 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  0..SQLITE_PRIVAT
963d0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
963e0 56 64 62 65 45 6e 74 65 72 28 56 64 62 65 2a 29  VdbeEnter(Vdbe*)
963f0 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
96400 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
96410 56 64 62 65 4c 65 61 76 65 28 56 64 62 65 2a 29  VdbeLeave(Vdbe*)
96420 3b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  ;..#else..# defi
96430 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 6e  ne sqlite3VdbeEn
96440 74 65 72 28 58 29 0d 0a 23 20 64 65 66 69 6e 65  ter(X)..# define
96450 20 73 71 6c 69 74 65 33 56 64 62 65 4c 65 61 76   sqlite3VdbeLeav
96460 65 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  e(X)..#endif....
96470 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
96480 42 55 47 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG..SQLITE_PRIV
96490 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
964a0 56 64 62 65 4d 65 6d 41 62 6f 75 74 54 6f 43 68  VdbeMemAboutToCh
964b0 61 6e 67 65 28 56 64 62 65 2a 2c 4d 65 6d 2a 29  ange(Vdbe*,Mem*)
964c0 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66  ;..#endif....#if
964d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
964e0 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0d 0a 53 51  _FOREIGN_KEY..SQ
964f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
96500 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63   sqlite3VdbeChec
96510 6b 46 6b 28 56 64 62 65 20 2a 2c 20 69 6e 74 29  kFk(Vdbe *, int)
96520 3b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  ;..#else..# defi
96530 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  ne sqlite3VdbeCh
96540 65 63 6b 46 6b 28 70 2c 69 29 20 30 0d 0a 23 65  eckFk(p,i) 0..#e
96550 6e 64 69 66 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50  ndif....SQLITE_P
96560 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
96570 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
96580 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0d 0a 23  te(Mem*, u8);..#
96590 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
965a0 55 47 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  UG..SQLITE_PRIVA
965b0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
965c0 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64  3VdbePrintSql(Vd
965d0 62 65 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  be*);..SQLITE_PR
965e0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
965f0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
96600 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d  yPrint(Mem *pMem
96610 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0d 0a  , char *zBuf);..
96620 23 65 6e 64 69 66 0d 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
96630 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
96640 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42  e3VdbeMemHandleB
96650 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0d 0a  om(Mem *pMem);..
96660 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
96670 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0d 0a  _OMIT_INCRBLOB..
96680 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
96690 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
966a0 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65  MemExpandBlob(Me
966b0 6d 20 2a 29 3b 0d 0a 20 20 23 64 65 66 69 6e 65  m *);..  #define
966c0 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28   ExpandBlob(P) (
966d0 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  ((P)->flags&MEM_
966e0 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62  Zero)?sqlite3Vdb
966f0 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50  eMemExpandBlob(P
96700 29 3a 30 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 23  ):0)..#else..  #
96710 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64  define sqlite3Vd
96720 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
96730 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20 20  x) SQLITE_OK..  
96740 23 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c  #define ExpandBl
96750 6f 62 28 50 29 20 53 51 4c 49 54 45 5f 4f 4b 0d  ob(P) SQLITE_OK.
96760 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 65 6e 64 69  .#endif....#endi
96770 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56  f /* !defined(_V
96780 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0d 0a 0d  DBEINT_H_) */...
96790 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
967a0 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e   End of vdbeInt.
967b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
967c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
967d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
967e0 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
967f0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
96800 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
96810 6e 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a  n status.c *****
96820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96830 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 56 61 72 69  /..../*..** Vari
96840 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74  ables in which t
96850 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73 20  o record status 
96860 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f  information...*/
96870 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
96880 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
96890 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
968a0 3b 0d 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  ;..static SQLITE
968b0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
968c0 74 65 33 53 74 61 74 54 79 70 65 20 7b 0d 0a 20  te3StatType {.. 
968d0 20 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 31 30   int nowValue[10
968e0 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ];         /* Cu
968f0 72 72 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0d 0a  rrent value */..
96900 20 20 69 6e 74 20 6d 78 56 61 6c 75 65 5b 31 30    int mxValue[10
96910 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ];          /* M
96920 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 2a 2f 0d  aximum value */.
96930 0a 7d 20 73 71 6c 69 74 65 33 53 74 61 74 20 3d  .} sqlite3Stat =
96940 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b   { {0,}, {0,} };
96950 0d 0a 0d 0a 0d 0a 2f 2a 20 54 68 65 20 22 77 73  ....../* The "ws
96960 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69 6c  dStat" macro wil
96970 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
96980 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
96990 69 6f 6e 0d 0a 2a 2a 20 73 74 61 74 65 20 76 65  ion..** state ve
969a0 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62  ctor.  If writab
969b0 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
969c0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
969d0 20 74 68 65 20 74 61 72 67 65 74 2c 0d 0a 2a 2a   the target,..**
969e0 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61   we have to loca
969f0 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63  te the state vec
96a00 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e  tor at run-time.
96a10 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f    In the more co
96a20 6d 6d 6f 6e 0d 0a 2a 2a 20 63 61 73 65 20 77 68  mmon..** case wh
96a30 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
96a40 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
96a50 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63  orted, wsdStat c
96a60 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
96a70 79 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  y..** to the "sq
96a80 6c 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65  lite3Stat" state
96a90 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64   vector declared
96aa0 20 61 62 6f 76 65 2e 0d 0a 2a 2f 0d 0a 23 69 66   above...*/..#if
96ab0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
96ac0 57 53 44 0d 0a 23 20 64 65 66 69 6e 65 20 77 73  WSD..# define ws
96ad0 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74  dStatInit  sqlit
96ae0 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20  e3StatType *x = 
96af0 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53  &GLOBAL(sqlite3S
96b00 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53  tatType,sqlite3S
96b10 74 61 74 29 0d 0a 23 20 64 65 66 69 6e 65 20 77  tat)..# define w
96b20 73 64 53 74 61 74 20 78 5b 30 5d 0d 0a 23 65 6c  sdStat x[0]..#el
96b30 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 77 73 64  se..# define wsd
96b40 53 74 61 74 49 6e 69 74 0d 0a 23 20 64 65 66 69  StatInit..# defi
96b50 6e 65 20 77 73 64 53 74 61 74 20 73 71 6c 69 74  ne wsdStat sqlit
96b60 65 33 53 74 61 74 0d 0a 23 65 6e 64 69 66 0d 0a  e3Stat..#endif..
96b70 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
96b80 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
96b90 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 70 61  e of a status pa
96ba0 72 61 6d 65 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51  rameter...*/..SQ
96bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
96bc0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61   sqlite3StatusVa
96bd0 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0d 0a 20 20  lue(int op){..  
96be0 77 73 64 53 74 61 74 49 6e 69 74 3b 0d 0a 20 20  wsdStatInit;..  
96bf0 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
96c00 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
96c10 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
96c20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 77 73 64  );..  return wsd
96c30 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
96c40 5d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ];..}..../*..** 
96c50 41 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c  Add N to the val
96c60 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 72  ue of a status r
96c70 65 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73  ecord.  It is as
96c80 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0d 0a  sumed that the..
96c90 2a 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20  ** caller holds 
96ca0 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  appropriate lock
96cb0 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  s...*/..SQLITE_P
96cc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
96cd0 74 65 33 53 74 61 74 75 73 41 64 64 28 69 6e 74  te3StatusAdd(int
96ce0 20 6f 70 2c 20 69 6e 74 20 4e 29 7b 0d 0a 20 20   op, int N){..  
96cf0 77 73 64 53 74 61 74 49 6e 69 74 3b 0d 0a 20 20  wsdStatInit;..  
96d00 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
96d10 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
96d20 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
96d30 29 3b 0d 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f  );..  wsdStat.no
96d40 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d 20 4e 3b  wValue[op] += N;
96d50 0d 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 2e  ..  if( wsdStat.
96d60 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64  nowValue[op]>wsd
96d70 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d  Stat.mxValue[op]
96d80 20 29 7b 0d 0a 20 20 20 20 77 73 64 53 74 61 74   ){..    wsdStat
96d90 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77  .mxValue[op] = w
96da0 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
96db0 6f 70 5d 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  op];..  }..}....
96dc0 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 76  /*..** Set the v
96dd0 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73  alue of a status
96de0 20 74 6f 20 58 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   to X...*/..SQLI
96df0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
96e00 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74  sqlite3StatusSet
96e10 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b  (int op, int X){
96e20 0d 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b  ..  wsdStatInit;
96e30 0d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d  ..  assert( op>=
96e40 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a  0 && op<ArraySiz
96e50 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
96e60 75 65 29 20 29 3b 0d 0a 20 20 77 73 64 53 74 61  ue) );..  wsdSta
96e70 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d  t.nowValue[op] =
96e80 20 58 3b 0d 0a 20 20 69 66 28 20 77 73 64 53 74   X;..  if( wsdSt
96e90 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e  at.nowValue[op]>
96ea0 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b  wsdStat.mxValue[
96eb0 6f 70 5d 20 29 7b 0d 0a 20 20 20 20 77 73 64 53  op] ){..    wsdS
96ec0 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20  tat.mxValue[op] 
96ed0 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  = wsdStat.nowVal
96ee0 75 65 5b 6f 70 5d 3b 0d 0a 20 20 7d 0d 0a 7d 0d  ue[op];..  }..}.
96ef0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 51 75 65 72 79 20  .../*..** Query 
96f00 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
96f10 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  on...**..** This
96f20 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
96f30 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61  assumes that rea
96f40 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
96f50 61 6e 20 61 6c 69 67 6e 65 64 0d 0a 2a 2a 20 33  an aligned..** 3
96f60 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73  2-bit integer is
96f70 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61   an atomic opera
96f80 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61  tion.  If that a
96f90 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74  ssumption is not
96fa0 20 74 72 75 65 2c 0d 0a 2a 2a 20 74 68 65 6e 20   true,..** then 
96fb0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
96fc0 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 0d  not threadsafe..
96fd0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
96fe0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74  int sqlite3_stat
96ff0 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a  us(int op, int *
97000 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70  pCurrent, int *p
97010 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72  Highwater, int r
97020 65 73 65 74 46 6c 61 67 29 7b 0d 0a 20 20 77 73  esetFlag){..  ws
97030 64 53 74 61 74 49 6e 69 74 3b 0d 0a 20 20 69 66  dStatInit;..  if
97040 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72  ( op<0 || op>=Ar
97050 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e  raySize(wsdStat.
97060 6e 6f 77 56 61 6c 75 65 29 20 29 7b 0d 0a 20 20  nowValue) ){..  
97070 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
97080 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20  MISUSE_BKPT;..  
97090 7d 0d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 3d  }..  *pCurrent =
970a0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
970b0 65 5b 6f 70 5d 3b 0d 0a 20 20 2a 70 48 69 67 68  e[op];..  *pHigh
970c0 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e  water = wsdStat.
970d0 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0d 0a 20 20  mxValue[op];..  
970e0 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
970f0 0d 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78  ..    wsdStat.mx
97100 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53  Value[op] = wsdS
97110 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
97120 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
97130 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
97140 0d 0a 2f 2a 0d 0a 2a 2a 20 51 75 65 72 79 20 73  ../*..** Query s
97150 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
97160 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64  n for a single d
97170 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
97180 6f 6e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  on..*/..SQLITE_A
97190 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
971a0 62 5f 73 74 61 74 75 73 28 0d 0a 20 20 73 71 6c  b_status(..  sql
971b0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
971c0 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
971d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68  se connection wh
971e0 6f 73 65 20 73 74 61 74 75 73 20 69 73 20 64 65  ose status is de
971f0 73 69 72 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  sired */..  int 
97200 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
97210 20 20 2f 2a 20 53 74 61 74 75 73 20 76 65 72 62    /* Status verb
97220 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 43 75 72   */..  int *pCur
97230 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  rent,        /* 
97240 57 72 69 74 65 20 63 75 72 72 65 6e 74 20 76 61  Write current va
97250 6c 75 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69  lue here */..  i
97260 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20  nt *pHighwater, 
97270 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 69       /* Write hi
97280 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 68 65  gh-water mark he
97290 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 73  re */..  int res
972a0 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 2f  etFlag         /
972b0 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 74  * Reset high-wat
972c0 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 20  er mark if true 
972d0 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
972e0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
972f0 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
97300 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  /..  sqlite3_mut
97310 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
97320 65 78 29 3b 0d 0a 20 20 73 77 69 74 63 68 28 20  ex);..  switch( 
97330 6f 70 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  op ){..    case 
97340 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
97350 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20  LOOKASIDE_USED: 
97360 7b 0d 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65  {..      *pCurre
97370 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  nt = db->lookasi
97380 64 65 2e 6e 4f 75 74 3b 0d 0a 20 20 20 20 20 20  de.nOut;..      
97390 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62  *pHighwater = db
973a0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75  ->lookaside.mxOu
973b0 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 65  t;..      if( re
973c0 73 65 74 46 6c 61 67 20 29 7b 0d 0a 20 20 20 20  setFlag ){..    
973d0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
973e0 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
973f0 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0d 0a 20  okaside.nOut;.. 
97400 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72       }..      br
97410 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  eak;..    }.... 
97420 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44     case SQLITE_D
97430 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
97440 45 5f 48 49 54 3a 0d 0a 20 20 20 20 63 61 73 65  E_HIT:..    case
97450 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
97460 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f  _LOOKASIDE_MISS_
97470 53 49 5a 45 3a 0d 0a 20 20 20 20 63 61 73 65 20  SIZE:..    case 
97480 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
97490 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46  LOOKASIDE_MISS_F
974a0 55 4c 4c 3a 20 7b 0d 0a 20 20 20 20 20 20 74 65  ULL: {..      te
974b0 73 74 63 61 73 65 28 20 6f 70 3d 3d 53 51 4c 49  stcase( op==SQLI
974c0 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
974d0 41 53 49 44 45 5f 48 49 54 20 29 3b 0d 0a 20 20  ASIDE_HIT );..  
974e0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
974f0 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  ==SQLITE_DBSTATU
97500 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53  S_LOOKASIDE_MISS
97510 5f 53 49 5a 45 20 29 3b 0d 0a 20 20 20 20 20 20  _SIZE );..      
97520 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 53 51  testcase( op==SQ
97530 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f  LITE_DBSTATUS_LO
97540 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46 55 4c  OKASIDE_MISS_FUL
97550 4c 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  L );..      asse
97560 72 74 28 20 28 6f 70 2d 53 51 4c 49 54 45 5f 44  rt( (op-SQLITE_D
97570 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
97580 45 5f 48 49 54 29 3e 3d 30 20 29 3b 0d 0a 20 20  E_HIT)>=0 );..  
97590 20 20 20 20 61 73 73 65 72 74 28 20 28 6f 70 2d      assert( (op-
975a0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
975b0 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 29 3c 33  LOOKASIDE_HIT)<3
975c0 20 29 3b 0d 0a 20 20 20 20 20 20 2a 70 43 75 72   );..      *pCur
975d0 72 65 6e 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20  rent = 0;..     
975e0 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 64   *pHighwater = d
975f0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 61 6e 53  b->lookaside.anS
97600 74 61 74 5b 6f 70 20 2d 20 53 51 4c 49 54 45 5f  tat[op - SQLITE_
97610 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
97620 44 45 5f 48 49 54 5d 3b 0d 0a 20 20 20 20 20 20  DE_HIT];..      
97630 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
97640 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f  ..        db->lo
97650 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 6f  okaside.anStat[o
97660 70 20 2d 20 53 51 4c 49 54 45 5f 44 42 53 54 41  p - SQLITE_DBSTA
97670 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49  TUS_LOOKASIDE_HI
97680 54 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d  T] = 0;..      }
97690 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
976a0 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
976b0 0d 0a 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20  ..    ** Return 
976c0 61 6e 20 61 70 70 72 6f 78 69 6d 61 74 69 6f 6e  an approximation
976d0 20 66 6f 72 20 74 68 65 20 61 6d 6f 75 6e 74 20   for the amount 
976e0 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e  of memory curren
976f0 74 6c 79 20 75 73 65 64 0d 0a 20 20 20 20 2a 2a  tly used..    **
97700 20 62 79 20 61 6c 6c 20 70 61 67 65 72 73 20 61   by all pagers a
97710 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
97720 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73  he given databas
97730 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  e connection.  T
97740 68 65 0d 0a 20 20 20 20 2a 2a 20 68 69 67 68 77  he..    ** highw
97750 61 74 65 72 20 6d 61 72 6b 20 69 73 20 6d 65 61  ater mark is mea
97760 6e 69 6e 67 6c 65 73 73 20 61 6e 64 20 69 73 20  ningless and is 
97770 72 65 74 75 72 6e 65 64 20 61 73 20 7a 65 72 6f  returned as zero
97780 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  ...    */..    c
97790 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ase SQLITE_DBSTA
977a0 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 3a 20  TUS_CACHE_USED: 
977b0 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 74  {..      int tot
977c0 61 6c 55 73 65 64 20 3d 20 30 3b 0d 0a 20 20 20  alUsed = 0;..   
977d0 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20     int i;..     
977e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
977f0 65 72 41 6c 6c 28 64 62 29 3b 0d 0a 20 20 20 20  erAll(db);..    
97800 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
97810 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
97820 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
97830 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
97840 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ;..        if( p
97850 42 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  Bt ){..         
97860 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
97870 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
97880 65 72 28 70 42 74 29 3b 0d 0a 20 20 20 20 20 20  er(pBt);..      
97890 20 20 20 20 74 6f 74 61 6c 55 73 65 64 20 2b 3d      totalUsed +=
978a0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 65 6d   sqlite3PagerMem
978b0 55 73 65 64 28 70 50 61 67 65 72 29 3b 0d 0a 20  Used(pPager);.. 
978c0 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
978d0 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
978e0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
978f0 29 3b 0d 0a 20 20 20 20 20 20 2a 70 43 75 72 72  );..      *pCurr
97900 65 6e 74 20 3d 20 74 6f 74 61 6c 55 73 65 64 3b  ent = totalUsed;
97910 0d 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77 61  ..      *pHighwa
97920 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ter = 0;..      
97930 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  break;..    }...
97940 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a 20  .    /*..    ** 
97950 2a 70 43 75 72 72 65 6e 74 20 67 65 74 73 20 61  *pCurrent gets a
97960 6e 20 61 63 63 75 72 61 74 65 20 65 73 74 69 6d  n accurate estim
97970 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e  ate of the amoun
97980 74 20 6f 66 20 6d 65 6d 6f 72 79 20 75 73 65 64  t of memory used
97990 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72  ..    ** to stor
979a0 65 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72  e the schema for
979b0 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 28   all databases (
979c0 6d 61 69 6e 2c 20 74 65 6d 70 2c 20 61 6e 64 20  main, temp, and 
979d0 61 6e 79 20 41 54 54 41 43 48 65 64 0d 0a 20 20  any ATTACHed..  
979e0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 73 2e 20    ** databases. 
979f0 20 2a 70 48 69 67 68 77 61 74 65 72 20 69 73 20   *pHighwater is 
97a00 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0d 0a 20 20  set to zero...  
97a10 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53    */..    case S
97a20 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53  QLITE_DBSTATUS_S
97a30 43 48 45 4d 41 5f 55 53 45 44 3a 20 7b 0d 0a 20  CHEMA_USED: {.. 
97a40 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20       int i;     
97a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97a60 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
97a70 61 74 65 20 74 68 72 6f 75 67 68 20 73 63 68 65  ate through sche
97a80 6d 61 73 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e  mas */..      in
97a90 74 20 6e 42 79 74 65 20 3d 20 30 3b 20 20 20 20  t nByte = 0;    
97aa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
97ab0 64 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20  d to accumulate 
97ac0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0d  return value */.
97ad0 0a 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ...      sqlite3
97ae0 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62  BtreeEnterAll(db
97af0 29 3b 0d 0a 20 20 20 20 20 20 64 62 2d 3e 70 6e  );..      db->pn
97b00 42 79 74 65 73 46 72 65 65 64 20 3d 20 26 6e 42  BytesFreed = &nB
97b10 79 74 65 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28  yte;..      for(
97b20 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
97b30 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 53  i++){..        S
97b40 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
97b50 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68   db->aDb[i].pSch
97b60 65 6d 61 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ema;..        if
97b70 28 20 41 4c 57 41 59 53 28 70 53 63 68 65 6d 61  ( ALWAYS(pSchema
97b80 21 3d 30 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  !=0) ){..       
97b90 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0d     HashElem *p;.
97ba0 0a 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 42 79  ...          nBy
97bb0 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 47 6c 6f  te += sqlite3Glo
97bc0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75  balConfig.m.xRou
97bd0 6e 64 75 70 28 73 69 7a 65 6f 66 28 48 61 73 68  ndup(sizeof(Hash
97be0 45 6c 65 6d 29 29 20 2a 20 28 0d 0a 20 20 20 20  Elem)) * (..    
97bf0 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d            pSchem
97c00 61 2d 3e 74 62 6c 48 61 73 68 2e 63 6f 75 6e 74  a->tblHash.count
97c10 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2b   ..            +
97c20 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61   pSchema->trigHa
97c30 73 68 2e 63 6f 75 6e 74 0d 0a 20 20 20 20 20 20  sh.count..      
97c40 20 20 20 20 20 20 2b 20 70 53 63 68 65 6d 61 2d        + pSchema-
97c50 3e 69 64 78 48 61 73 68 2e 63 6f 75 6e 74 0d 0a  >idxHash.count..
97c60 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 70 53              + pS
97c70 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2e  chema->fkeyHash.
97c80 63 6f 75 6e 74 0d 0a 20 20 20 20 20 20 20 20 20  count..         
97c90 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e   );..          n
97ca0 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d  Byte += sqlite3M
97cb0 61 6c 6c 6f 63 53 69 7a 65 28 70 53 63 68 65 6d  allocSize(pSchem
97cc0 61 2d 3e 74 62 6c 48 61 73 68 2e 68 74 29 3b 0d  a->tblHash.ht);.
97cd0 0a 20 20 20 20 20 20 20 20 20 20 6e 42 79 74 65  .          nByte
97ce0 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   += sqlite3Mallo
97cf0 63 53 69 7a 65 28 70 53 63 68 65 6d 61 2d 3e 74  cSize(pSchema->t
97d00 72 69 67 48 61 73 68 2e 68 74 29 3b 0d 0a 20 20  rigHash.ht);..  
97d10 20 20 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d          nByte +=
97d20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
97d30 7a 65 28 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  ze(pSchema->idxH
97d40 61 73 68 2e 68 74 29 3b 0d 0a 20 20 20 20 20 20  ash.ht);..      
97d50 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c      nByte += sql
97d60 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
97d70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68  Schema->fkeyHash
97d80 2e 68 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  .ht);....       
97d90 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48     for(p=sqliteH
97da0 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d  ashFirst(&pSchem
97db0 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 20 70 3b  a->trigHash); p;
97dc0 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   p=sqliteHashNex
97dd0 74 28 70 29 29 7b 0d 0a 20 20 20 20 20 20 20 20  t(p)){..        
97de0 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
97df0 65 54 72 69 67 67 65 72 28 64 62 2c 20 28 54 72  eTrigger(db, (Tr
97e00 69 67 67 65 72 2a 29 73 71 6c 69 74 65 48 61 73  igger*)sqliteHas
97e10 68 44 61 74 61 28 70 29 29 3b 0d 0a 20 20 20 20  hData(p));..    
97e20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
97e30 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48     for(p=sqliteH
97e40 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d  ashFirst(&pSchem
97e50 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 70 3b 20  a->tblHash); p; 
97e60 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  p=sqliteHashNext
97e70 28 70 29 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  (p)){..         
97e80 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
97e90 54 61 62 6c 65 28 64 62 2c 20 28 54 61 62 6c 65  Table(db, (Table
97ea0 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
97eb0 61 28 70 29 29 3b 0d 0a 20 20 20 20 20 20 20 20  a(p));..        
97ec0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a    }..        }..
97ed0 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 64        }..      d
97ee0 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64 20  b->pnBytesFreed 
97ef0 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  = 0;..      sqli
97f00 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
97f10 28 64 62 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2a  (db);....      *
97f20 70 48 69 67 68 77 61 74 65 72 20 3d 20 30 3b 0d  pHighwater = 0;.
97f30 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e 74  .      *pCurrent
97f40 20 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 20   = nByte;..     
97f50 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
97f60 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a  ..    /*..    **
97f70 20 2a 70 43 75 72 72 65 6e 74 20 67 65 74 73 20   *pCurrent gets 
97f80 61 6e 20 61 63 63 75 72 61 74 65 20 65 73 74 69  an accurate esti
97f90 6d 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75  mate of the amou
97fa0 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 75 73 65  nt of memory use
97fb0 64 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f  d..    ** to sto
97fc0 72 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20  re all prepared 
97fd0 73 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 20 20 20  statements...   
97fe0 20 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20   ** *pHighwater 
97ff0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0d  is set to zero..
98000 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73  .    */..    cas
98010 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  e SQLITE_DBSTATU
98020 53 5f 53 54 4d 54 5f 55 53 45 44 3a 20 7b 0d 0a  S_STMT_USED: {..
98030 20 20 20 20 20 20 73 74 72 75 63 74 20 56 64 62        struct Vdb
98040 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
98050 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
98060 72 61 74 65 20 74 68 72 6f 75 67 68 20 56 4d 73  rate through VMs
98070 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   */..      int n
98080 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Byte = 0;       
98090 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
980a0 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 72 65 74  o accumulate ret
980b0 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0d 0a 0d 0a  urn value */....
980c0 20 20 20 20 20 20 64 62 2d 3e 70 6e 42 79 74 65        db->pnByte
980d0 73 46 72 65 65 64 20 3d 20 26 6e 42 79 74 65 3b  sFreed = &nByte;
980e0 0d 0a 20 20 20 20 20 20 66 6f 72 28 70 56 64 62  ..      for(pVdb
980f0 65 3d 64 62 2d 3e 70 56 64 62 65 3b 20 70 56 64  e=db->pVdbe; pVd
98100 62 65 3b 20 70 56 64 62 65 3d 70 56 64 62 65 2d  be; pVdbe=pVdbe-
98110 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 20 20  >pNext){..      
98120 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
98130 65 74 65 4f 62 6a 65 63 74 28 64 62 2c 20 70 56  eteObject(db, pV
98140 64 62 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  dbe);..      }..
98150 20 20 20 20 20 20 64 62 2d 3e 70 6e 42 79 74 65        db->pnByte
98160 73 46 72 65 65 64 20 3d 20 30 3b 0d 0a 0d 0a 20  sFreed = 0;.... 
98170 20 20 20 20 20 2a 70 48 69 67 68 77 61 74 65 72       *pHighwater
98180 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 2a 70 43   = 0;..      *pC
98190 75 72 72 65 6e 74 20 3d 20 6e 42 79 74 65 3b 0d  urrent = nByte;.
981a0 0a 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ...      break;.
981b0 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
981c0 0d 0a 20 20 20 20 2a 2a 20 53 65 74 20 2a 70 43  ..    ** Set *pC
981d0 75 72 72 65 6e 74 20 74 6f 20 74 68 65 20 74 6f  urrent to the to
981e0 74 61 6c 20 63 61 63 68 65 20 68 69 74 73 20 6f  tal cache hits o
981f0 72 20 6d 69 73 73 65 73 20 65 6e 63 6f 75 6e 74  r misses encount
98200 65 72 65 64 20 62 79 20 61 6c 6c 0d 0a 20 20 20  ered by all..   
98210 20 2a 2a 20 70 61 67 65 72 73 20 74 68 65 20 64   ** pagers the d
98220 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69  atabase handle i
98230 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 2e 20  s connected to. 
98240 2a 70 48 69 67 68 77 61 74 65 72 20 69 73 20 61  *pHighwater is a
98250 6c 77 61 79 73 20 73 65 74 20 0d 0a 20 20 20 20  lways set ..    
98260 2a 2a 20 74 6f 20 7a 65 72 6f 2e 0d 0a 20 20 20  ** to zero...   
98270 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 51   */..    case SQ
98280 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41  LITE_DBSTATUS_CA
98290 43 48 45 5f 48 49 54 3a 0d 0a 20 20 20 20 63 61  CHE_HIT:..    ca
982a0 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  se SQLITE_DBSTAT
982b0 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 3a 20 7b  US_CACHE_MISS: {
982c0 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a  ..      int i;..
982d0 20 20 20 20 20 20 69 6e 74 20 6e 52 65 74 20 3d        int nRet =
982e0 20 30 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   0;..      asser
982f0 74 28 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  t( SQLITE_DBSTAT
98300 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 3d 3d 53  US_CACHE_MISS==S
98310 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43  QLITE_DBSTATUS_C
98320 41 43 48 45 5f 48 49 54 2b 31 20 29 3b 0d 0a 0d  ACHE_HIT+1 );...
98330 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
98340 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
98350 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  ..        if( db
98360 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0d  ->aDb[i].pBt ){.
98370 0a 20 20 20 20 20 20 20 20 20 20 50 61 67 65 72  .          Pager
98380 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74   *pPager = sqlit
98390 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d  e3BtreePager(db-
983a0 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0d 0a 20  >aDb[i].pBt);.. 
983b0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
983c0 50 61 67 65 72 43 61 63 68 65 53 74 61 74 28 70  PagerCacheStat(p
983d0 50 61 67 65 72 2c 20 6f 70 2c 20 72 65 73 65 74  Pager, op, reset
983e0 46 6c 61 67 2c 20 26 6e 52 65 74 29 3b 0d 0a 20  Flag, &nRet);.. 
983f0 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
98400 7d 0d 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77  }..      *pHighw
98410 61 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 20  ater = 0;..     
98420 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 6e 52 65   *pCurrent = nRe
98430 74 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t;..      break;
98440 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 64  ..    }....    d
98450 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20  efault: {..     
98460 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
98470 4f 52 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  OR;..    }..  }.
98480 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
98490 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
984a0 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
984b0 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ..}..../********
984c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74  ****** End of st
984d0 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  atus.c *********
984e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
984f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98500 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
98510 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
98520 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  le date.c ******
98530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98550 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20  ******/../*..** 
98560 32 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 0d  2003 October 31.
98570 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
98580 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
98590 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
985a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
985b0 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
985c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
985d0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
985e0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
985f0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
98600 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
98610 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
98620 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
98630 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
98640 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
98650 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
98660 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
98670 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
98680 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
98690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986d0 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  *******..** This
986e0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
986f0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
98700 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61  hat implement da
98710 74 65 20 61 6e 64 20 74 69 6d 65 0d 0a 2a 2a 20  te and time..** 
98720 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51  functions for SQ
98730 4c 69 74 65 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20  Lite.  ..**..** 
98740 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e  There is only on
98750 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f  e exported symbo
98760 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d  l in this file -
98770 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a   the function..*
98780 2a 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  * sqlite3Registe
98790 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
987a0 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68  ns() found at th
987b0 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20  e bottom of the 
987c0 66 69 6c 65 2e 0d 0a 2a 2a 20 41 6c 6c 20 6f 74  file...** All ot
987d0 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c  her code has fil
987e0 65 20 73 63 6f 70 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  e scope...**..**
987f0 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65   SQLite processe
98800 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20  s all times and 
98810 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20  dates as Julian 
98820 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68  Day numbers.  Th
98830 65 0d 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20  e..** dates and 
98840 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64  times are stored
98850 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   as the number o
98860 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f  f days since noo
98870 6e 0d 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69  n..** in Greenwi
98880 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32  ch on November 2
98890 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63  4, 4714 B.C. acc
988a0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72  ording to the Gr
988b0 65 67 6f 72 69 61 6e 0d 0a 2a 2a 20 63 61 6c 65  egorian..** cale
988c0 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0d 0a 2a  ndar system. ..*
988d0 2a 0d 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31  *..** 1970-01-01
988e0 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20   00:00:00 is JD 
988f0 32 34 34 30 35 38 37 2e 35 0d 0a 2a 2a 20 32 30  2440587.5..** 20
98900 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30  00-01-01 00:00:0
98910 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e  0 is JD 2451544.
98920 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  5..**..** This i
98930 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75  mplemention requ
98940 69 72 65 73 20 79 65 61 72 73 20 74 6f 20 62 65  ires years to be
98950 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20   expressed as a 
98960 34 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 0d 0a  4-digit number..
98970 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74  ** which means t
98980 68 61 74 20 6f 6e 6c 79 20 64 61 74 65 73 20 62  hat only dates b
98990 65 74 77 65 65 6e 20 30 30 30 30 2d 30 31 2d 30  etween 0000-01-0
989a0 31 20 61 6e 64 20 39 39 39 39 2d 31 32 2d 33 31  1 and 9999-12-31
989b0 20 63 61 6e 0d 0a 2a 2a 20 62 65 20 72 65 70 72   can..** be repr
989c0 65 73 65 6e 74 65 64 2c 20 65 76 65 6e 20 74 68  esented, even th
989d0 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 61 79 20  ough julian day 
989e0 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 20 61 20  numbers allow a 
989f0 6d 75 63 68 20 77 69 64 65 72 0d 0a 2a 2a 20 72  much wider..** r
98a00 61 6e 67 65 20 6f 66 20 64 61 74 65 73 2e 0d 0a  ange of dates...
98a10 2a 2a 0d 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f  **..** The Grego
98a20 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 79  rian calendar sy
98a30 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f 72  stem is used for
98a40 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 74   all dates and t
98a50 69 6d 65 73 2c 0d 0a 2a 2a 20 65 76 65 6e 20 74  imes,..** even t
98a60 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 74  hose that predat
98a70 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20  e the Gregorian 
98a80 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f  calendar.  Histo
98a90 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0d 0a 2a  rians usually..*
98aa0 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e  * use the Julian
98ab0 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61   calendar for da
98ac0 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38  tes prior to 158
98ad0 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20  2-10-15 and for 
98ae0 73 6f 6d 65 0d 0a 2a 2a 20 64 61 74 65 73 20 61  some..** dates a
98af0 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e  fterwards, depen
98b00 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20  ding on locale. 
98b10 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 20   Beware of this 
98b20 64 69 66 66 65 72 65 6e 63 65 2e 0d 0a 2a 2a 0d  difference...**.
98b30 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69  .** The conversi
98b40 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  on algorithms ar
98b50 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61  e implemented ba
98b60 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69  sed on descripti
98b70 6f 6e 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 66  ons..** in the f
98b80 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0d 0a  ollowing text:..
98b90 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e  **..**      Jean
98ba0 20 4d 65 65 75 73 0d 0a 2a 2a 20 20 20 20 20 20   Meeus..**      
98bb0 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67  Astronomical Alg
98bc0 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69  orithms, 2nd Edi
98bd0 74 69 6f 6e 2c 20 31 39 39 38 0d 0a 2a 2a 20 20  tion, 1998..**  
98be0 20 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39      ISBM 0-94339
98bf0 36 2d 36 31 2d 31 0d 0a 2a 2a 20 20 20 20 20 20  6-61-1..**      
98c00 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49  Willmann-Bell, I
98c10 6e 63 0d 0a 2a 2a 20 20 20 20 20 20 52 69 63 68  nc..**      Rich
98c20 6d 6f 6e 64 2c 20 56 69 72 67 69 6e 69 61 20 28  mond, Virginia (
98c30 55 53 41 29 0d 0a 2a 2f 0d 0a 2f 2a 20 23 69 6e  USA)..*/../* #in
98c40 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e  clude <stdlib.h>
98c50 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65   */../* #include
98c60 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a   <assert.h> */..
98c70 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68  #include <time.h
98c80 3e 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  >....#ifndef SQL
98c90 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
98ca0 45 5f 46 55 4e 43 53 0d 0a 0d 0a 0d 0a 2f 2a 0d  E_FUNCS....../*.
98cb0 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20  .** A structure 
98cc0 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69  for holding a si
98cd0 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69  ngle date and ti
98ce0 6d 65 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  me...*/..typedef
98cf0 20 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65   struct DateTime
98d00 20 44 61 74 65 54 69 6d 65 3b 0d 0a 73 74 72 75   DateTime;..stru
98d10 63 74 20 44 61 74 65 54 69 6d 65 20 7b 0d 0a 20  ct DateTime {.. 
98d20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
98d30 4a 44 3b 20 2f 2a 20 54 68 65 20 6a 75 6c 69 61  JD; /* The julia
98d40 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 74 69 6d  n day number tim
98d50 65 73 20 38 36 34 30 30 30 30 30 20 2a 2f 0d 0a  es 86400000 */..
98d60 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 3b 20 20    int Y, M, D;  
98d70 20 20 20 20 20 2f 2a 20 59 65 61 72 2c 20 6d 6f       /* Year, mo
98d80 6e 74 68 2c 20 61 6e 64 20 64 61 79 20 2a 2f 0d  nth, and day */.
98d90 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20 20 20 20  .  int h, m;    
98da0 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 20 61 6e        /* Hour an
98db0 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0d 0a 20 20  d minutes */..  
98dc0 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 20 20  int tz;         
98dd0 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 6f     /* Timezone o
98de0 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 74 65 73  ffset in minutes
98df0 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 73 3b   */..  double s;
98e00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
98e10 6f 6e 64 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20  onds */..  char 
98e20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f 2a  validYMD;     /*
98e30 20 54 72 75 65 20 28 31 29 20 69 66 20 59 2c 4d   True (1) if Y,M
98e40 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0d  ,D are valid */.
98e50 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53  .  char validHMS
98e60 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31  ;     /* True (1
98e70 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76  ) if h,m,s are v
98e80 61 6c 69 64 20 2a 2f 0d 0a 20 20 63 68 61 72 20  alid */..  char 
98e90 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a  validJD;      /*
98ea0 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44   True (1) if iJD
98eb0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0d 0a 20 20   is valid */..  
98ec0 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20  char validTZ;   
98ed0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
98ee0 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f  f tz is valid */
98ef0 0d 0a 20 20 63 68 61 72 20 64 61 74 65 31 43 3b  ..  char date1C;
98f00 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ..};....../*..**
98f10 20 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69   Convert zDate i
98f20 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  nto one or more 
98f30 69 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74  integers.  Addit
98f40 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0d  ional arguments.
98f50 0a 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75  .** come in grou
98f60 70 73 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f  ps of 5 as follo
98f70 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  ws:..**..**     
98f80 20 20 4e 20 20 20 20 20 20 20 6e 75 6d 62 65 72    N       number
98f90 20 6f 66 20 64 69 67 69 74 73 20 69 6e 20 74 68   of digits in th
98fa0 65 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 20 20  e integer..**   
98fb0 20 20 20 20 6d 69 6e 20 20 20 20 20 6d 69 6e 69      min     mini
98fc0 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  mum allowed valu
98fd0 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
98fe0 0d 0a 2a 2a 20 20 20 20 20 20 20 6d 61 78 20 20  ..**       max  
98ff0 20 20 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77     maximum allow
99000 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ed value of the 
99010 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 20 20 20 20  integer..**     
99020 20 20 6e 65 78 74 43 20 20 20 66 69 72 73 74 20    nextC   first 
99030 63 68 61 72 61 63 74 65 72 20 61 66 74 65 72 20  character after 
99040 74 68 65 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20  the integer..** 
99050 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 77 68        pVal    wh
99060 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ere to write the
99070 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65 2e   integers value.
99080 0d 0a 2a 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 73  ..**..** Convers
99090 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e  ions continue un
990a0 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78  til one with nex
990b0 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74  tC==0 is encount
990c0 65 72 65 64 2e 0d 0a 2a 2a 20 54 68 65 20 66 75  ered...** The fu
990d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
990e0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63  he number of suc
990f0 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69  cessful conversi
99100 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ons...*/..static
99110 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 63   int getDigits(c
99120 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
99130 2c 20 2e 2e 2e 29 7b 0d 0a 20 20 76 61 5f 6c 69  , ...){..  va_li
99140 73 74 20 61 70 3b 0d 0a 20 20 69 6e 74 20 76 61  st ap;..  int va
99150 6c 3b 0d 0a 20 20 69 6e 74 20 4e 3b 0d 0a 20 20  l;..  int N;..  
99160 69 6e 74 20 6d 69 6e 3b 0d 0a 20 20 69 6e 74 20  int min;..  int 
99170 6d 61 78 3b 0d 0a 20 20 69 6e 74 20 6e 65 78 74  max;..  int next
99180 43 3b 0d 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b  C;..  int *pVal;
99190 0d 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b  ..  int cnt = 0;
991a0 0d 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ..  va_start(ap,
991b0 20 7a 44 61 74 65 29 3b 0d 0a 20 20 64 6f 7b 0d   zDate);..  do{.
991c0 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 67 28  .    N = va_arg(
991d0 61 70 2c 20 69 6e 74 29 3b 0d 0a 20 20 20 20 6d  ap, int);..    m
991e0 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  in = va_arg(ap, 
991f0 69 6e 74 29 3b 0d 0a 20 20 20 20 6d 61 78 20 3d  int);..    max =
99200 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
99210 3b 0d 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76  ;..    nextC = v
99220 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d  a_arg(ap, int);.
99230 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61  .    pVal = va_a
99240 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0d 0a 20  rg(ap, int*);.. 
99250 20 20 20 76 61 6c 20 3d 20 30 3b 0d 0a 20 20 20     val = 0;..   
99260 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0d 0a   while( N-- ){..
99270 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
99280 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65  e3Isdigit(*zDate
99290 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67 6f  ) ){..        go
992a0 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73  to end_getDigits
992b0 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
992c0 20 20 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b    val = val*10 +
992d0 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0d 0a   *zDate - '0';..
992e0 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0d 0a        zDate++;..
992f0 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 76      }..    if( v
99300 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c 3e 6d 61  al<min || val>ma
99310 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d 30 20 26  x || (nextC!=0 &
99320 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61 74 65 29  & nextC!=*zDate)
99330 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20   ){..      goto 
99340 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0d 0a  end_getDigits;..
99350 20 20 20 20 7d 0d 0a 20 20 20 20 2a 70 56 61 6c      }..    *pVal
99360 20 3d 20 76 61 6c 3b 0d 0a 20 20 20 20 7a 44 61   = val;..    zDa
99370 74 65 2b 2b 3b 0d 0a 20 20 20 20 63 6e 74 2b 2b  te++;..    cnt++
99380 3b 0d 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78  ;..  }while( nex
99390 74 43 20 29 3b 0d 0a 65 6e 64 5f 67 65 74 44 69  tC );..end_getDi
993a0 67 69 74 73 3a 0d 0a 20 20 76 61 5f 65 6e 64 28  gits:..  va_end(
993b0 61 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 63  ap);..  return c
993c0 6e 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  nt;..}..../*..**
993d0 20 50 61 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e   Parse a timezon
993e0 65 20 65 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74  e extension on t
993f0 68 65 20 65 6e 64 20 6f 66 20 61 20 64 61 74 65  he end of a date
99400 2d 74 69 6d 65 2e 0d 0a 2a 2a 20 54 68 65 20 65  -time...** The e
99410 78 74 65 6e 73 69 6f 6e 20 69 73 20 6f 66 20 74  xtension is of t
99420 68 65 20 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a  he form:..**..**
99430 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48 48 3a          (+/-)HH:
99440 4d 4d 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 72 20 74 68  MM..**..** Or th
99450 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f  e "zulu" notatio
99460 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  n:..**..**      
99470 20 20 5a 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74    Z..**..** If t
99480 68 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63  he parse is succ
99490 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68  essful, write th
994a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75  e number of minu
994b0 74 65 73 0d 0a 2a 2a 20 6f 66 20 63 68 61 6e 67  tes..** of chang
994c0 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72  e in p->tz and r
994d0 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70  eturn 0.  If a p
994e0 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75  arser error occu
994f0 72 73 2c 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 6e  rs,..** return n
99500 6f 6e 2d 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a  on-zero...**..**
99510 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69   A missing speci
99520 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73  fier is not cons
99530 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e  idered an error.
99540 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
99550 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63   parseTimezone(c
99560 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
99570 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0d  , DateTime *p){.
99580 0a 20 20 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0d  .  int sgn = 0;.
99590 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b  .  int nHr, nMn;
995a0 0d 0a 20 20 69 6e 74 20 63 3b 0d 0a 20 20 77 68  ..  int c;..  wh
995b0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70  ile( sqlite3Issp
995c0 61 63 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a  ace(*zDate) ){ z
995d0 44 61 74 65 2b 2b 3b 20 7d 0d 0a 20 20 70 2d 3e  Date++; }..  p->
995e0 74 7a 20 3d 20 30 3b 0d 0a 20 20 63 20 3d 20 2a  tz = 0;..  c = *
995f0 7a 44 61 74 65 3b 0d 0a 20 20 69 66 28 20 63 3d  zDate;..  if( c=
99600 3d 27 2d 27 20 29 7b 0d 0a 20 20 20 20 73 67 6e  ='-' ){..    sgn
99610 20 3d 20 2d 31 3b 0d 0a 20 20 7d 65 6c 73 65 20   = -1;..  }else 
99620 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0d 0a 20  if( c=='+' ){.. 
99630 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0d 0a 20 20     sgn = +1;..  
99640 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27  }else if( c=='Z'
99650 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0d 0a 20   || c=='z' ){.. 
99660 20 20 20 7a 44 61 74 65 2b 2b 3b 0d 0a 20 20 20     zDate++;..   
99670 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b   goto zulu_time;
99680 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
99690 72 65 74 75 72 6e 20 63 21 3d 30 3b 0d 0a 20 20  return c!=0;..  
996a0 7d 0d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0d 0a 20  }..  zDate++;.. 
996b0 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a   if( getDigits(z
996c0 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20  Date, 2, 0, 14, 
996d0 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c  ':', &nHr, 2, 0,
996e0 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32   59, 0, &nMn)!=2
996f0 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
99700 31 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 44 61 74 65  1;..  }..  zDate
99710 20 2b 3d 20 35 3b 0d 0a 20 20 70 2d 3e 74 7a 20   += 5;..  p->tz 
99720 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72  = sgn*(nMn + nHr
99730 2a 36 30 29 3b 0d 0a 7a 75 6c 75 5f 74 69 6d 65  *60);..zulu_time
99740 3a 0d 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  :..  while( sqli
99750 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74  te3Isspace(*zDat
99760 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d  e) ){ zDate++; }
99770 0d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74  ..  return *zDat
99780 65 21 3d 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  e!=0;..}..../*..
99790 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f  ** Parse times o
997a0 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d  f the form HH:MM
997b0 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20   or HH:MM:SS or 
997c0 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0d 0a  HH:MM:SS.FFFF...
997d0 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61  ** The HH, MM, a
997e0 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20  nd SS must each 
997f0 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67  be exactly 2 dig
99800 69 74 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 66 72  its.  The..** fr
99810 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
99820 20 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65   FFFF can be one
99830 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e   or more digits.
99840 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ..**..** Return 
99850 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  1 if there is a 
99860 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 6e  parsing error an
99870 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0d  d 0 on success..
99880 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
99890 70 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73  parseHhMmSs(cons
998a0 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44  t char *zDate, D
998b0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0d 0a 20 20  ateTime *p){..  
998c0 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0d 0a 20 20  int h, m, s;..  
998d0 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b  double ms = 0.0;
998e0 0d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74  ..  if( getDigit
998f0 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 32  s(zDate, 2, 0, 2
99900 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20 30  4, ':', &h, 2, 0
99910 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32 20  , 59, 0, &m)!=2 
99920 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ){..    return 1
99930 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 44 61 74 65 20  ;..  }..  zDate 
99940 2b 3d 20 35 3b 0d 0a 20 20 69 66 28 20 2a 7a 44  += 5;..  if( *zD
99950 61 74 65 3d 3d 27 3a 27 20 29 7b 0d 0a 20 20 20  ate==':' ){..   
99960 20 7a 44 61 74 65 2b 2b 3b 0d 0a 20 20 20 20 69   zDate++;..    i
99970 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61  f( getDigits(zDa
99980 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  te, 2, 0, 59, 0,
99990 20 26 73 29 21 3d 31 20 29 7b 0d 0a 20 20 20 20   &s)!=1 ){..    
999a0 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20    return 1;..   
999b0 20 7d 0d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d   }..    zDate +=
999c0 20 32 3b 0d 0a 20 20 20 20 69 66 28 20 2a 7a 44   2;..    if( *zD
999d0 61 74 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69  ate=='.' && sqli
999e0 74 65 33 49 73 64 69 67 69 74 28 7a 44 61 74 65  te3Isdigit(zDate
999f0 5b 31 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 64  [1]) ){..      d
99a00 6f 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31  ouble rScale = 1
99a10 2e 30 3b 0d 0a 20 20 20 20 20 20 7a 44 61 74 65  .0;..      zDate
99a20 2b 2b 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65  ++;..      while
99a30 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
99a40 28 2a 7a 44 61 74 65 29 20 29 7b 0d 0a 20 20 20  (*zDate) ){..   
99a50 20 20 20 20 20 6d 73 20 3d 20 6d 73 2a 31 30 2e       ms = ms*10.
99a60 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27  0 + *zDate - '0'
99a70 3b 0d 0a 20 20 20 20 20 20 20 20 72 53 63 61 6c  ;..        rScal
99a80 65 20 2a 3d 20 31 30 2e 30 3b 0d 0a 20 20 20 20  e *= 10.0;..    
99a90 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0d 0a 20 20      zDate++;..  
99aa0 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 6d 73 20      }..      ms 
99ab0 2f 3d 20 72 53 63 61 6c 65 3b 0d 0a 20 20 20 20  /= rScale;..    
99ac0 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  }..  }else{..   
99ad0 20 73 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20   s = 0;..  }..  
99ae0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0d  p->validJD = 0;.
99af0 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d  .  p->validHMS =
99b00 20 31 3b 0d 0a 20 20 70 2d 3e 68 20 3d 20 68 3b   1;..  p->h = h;
99b10 0d 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0d 0a 20  ..  p->m = m;.. 
99b20 20 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0d   p->s = s + ms;.
99b30 0a 20 20 69 66 28 20 70 61 72 73 65 54 69 6d 65  .  if( parseTime
99b40 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29  zone(zDate, p) )
99b50 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 70 2d   return 1;..  p-
99b60 3e 76 61 6c 69 64 54 5a 20 3d 20 28 70 2d 3e 74  >validTZ = (p->t
99b70 7a 21 3d 30 29 3f 31 3a 30 3b 0d 0a 20 20 72 65  z!=0)?1:0;..  re
99b80 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn 0;..}..../*
99b90 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f  ..** Convert fro
99ba0 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a  m YYYY-MM-DD HH:
99bb0 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20  MM:SS to julian 
99bc0 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20  day.  We always 
99bd0 61 73 73 75 6d 65 0d 0a 2a 2a 20 74 68 61 74 20  assume..** that 
99be0 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69  the YYYY-MM-DD i
99bf0 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  s according to t
99c00 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  he Gregorian cal
99c10 65 6e 64 61 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52  endar...**..** R
99c20 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73  eference:  Meeus
99c30 20 70 61 67 65 20 36 31 0d 0a 2a 2f 0d 0a 73 74   page 61..*/..st
99c40 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
99c50 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29  eJD(DateTime *p)
99c60 7b 0d 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  {..  int Y, M, D
99c70 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0d  , A, B, X1, X2;.
99c80 0a 0d 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  ...  if( p->vali
99c90 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  dJD ) return;.. 
99ca0 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44   if( p->validYMD
99cb0 20 29 7b 0d 0a 20 20 20 20 59 20 3d 20 70 2d 3e   ){..    Y = p->
99cc0 59 3b 0d 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d  Y;..    M = p->M
99cd0 3b 0d 0a 20 20 20 20 44 20 3d 20 70 2d 3e 44 3b  ;..    D = p->D;
99ce0 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
99cf0 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49 66  Y = 2000;  /* If
99d00 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69 65   no YMD specifie
99d10 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d 4a  d, assume 2000-J
99d20 61 6e 2d 30 31 20 2a 2f 0d 0a 20 20 20 20 4d 20  an-01 */..    M 
99d30 3d 20 31 3b 0d 0a 20 20 20 20 44 20 3d 20 31 3b  = 1;..    D = 1;
99d40 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 4d 3c 3d  ..  }..  if( M<=
99d50 32 20 29 7b 0d 0a 20 20 20 20 59 2d 2d 3b 0d 0a  2 ){..    Y--;..
99d60 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0d 0a 20 20      M += 12;..  
99d70 7d 0d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0d  }..  A = Y/100;.
99d80 0a 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28  .  B = 2 - A + (
99d90 41 2f 34 29 3b 0d 0a 20 20 58 31 20 3d 20 33 36  A/4);..  X1 = 36
99da0 35 32 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30  525*(Y+4716)/100
99db0 3b 0d 0a 20 20 58 32 20 3d 20 33 30 36 30 30 31  ;..  X2 = 306001
99dc0 2a 28 4d 2b 31 29 2f 31 30 30 30 30 3b 0d 0a 20  *(M+1)/10000;.. 
99dd0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
99de0 65 33 5f 69 6e 74 36 34 29 28 28 58 31 20 2b 20  e3_int64)((X1 + 
99df0 58 32 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32  X2 + D + B - 152
99e00 34 2e 35 20 29 20 2a 20 38 36 34 30 30 30 30 30  4.5 ) * 86400000
99e10 29 3b 0d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44  );..  p->validJD
99e20 20 3d 20 31 3b 0d 0a 20 20 69 66 28 20 70 2d 3e   = 1;..  if( p->
99e30 76 61 6c 69 64 48 4d 53 20 29 7b 0d 0a 20 20 20  validHMS ){..   
99e40 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 2d 3e 68 2a   p->iJD += p->h*
99e50 33 36 30 30 30 30 30 20 2b 20 70 2d 3e 6d 2a 36  3600000 + p->m*6
99e60 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f  0000 + (sqlite3_
99e70 69 6e 74 36 34 29 28 70 2d 3e 73 2a 31 30 30 30  int64)(p->s*1000
99e80 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 76  );..    if( p->v
99e90 61 6c 69 64 54 5a 20 29 7b 0d 0a 20 20 20 20 20  alidTZ ){..     
99ea0 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e 74 7a   p->iJD -= p->tz
99eb0 2a 36 30 30 30 30 3b 0d 0a 20 20 20 20 20 20 70  *60000;..      p
99ec0 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0d  ->validYMD = 0;.
99ed0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48  .      p->validH
99ee0 4d 53 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70  MS = 0;..      p
99ef0 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0d 0a  ->validTZ = 0;..
99f00 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d      }..  }..}...
99f10 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 73 65 20 64 61  ./*..** Parse da
99f20 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0d  tes of the form.
99f30 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 59 59 59 59  .**..**     YYYY
99f40 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e  -MM-DD HH:MM:SS.
99f50 46 46 46 0d 0a 2a 2a 20 20 20 20 20 59 59 59 59  FFF..**     YYYY
99f60 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0d  -MM-DD HH:MM:SS.
99f70 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
99f80 44 44 20 48 48 3a 4d 4d 0d 0a 2a 2a 20 20 20 20  DD HH:MM..**    
99f90 20 59 59 59 59 2d 4d 4d 2d 44 44 0d 0a 2a 2a 0d   YYYY-MM-DD..**.
99fa0 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72 65  .** Write the re
99fb0 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44 61  sult into the Da
99fc0 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65  teTime structure
99fd0 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0d 0a 2a   and return 0..*
99fe0 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  * on success and
99ff0 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20   1 if the input 
9a000 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20  string is not a 
9a010 77 65 6c 6c 2d 66 6f 72 6d 65 64 0d 0a 2a 2a 20  well-formed..** 
9a020 64 61 74 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  date...*/..stati
9a030 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d  c int parseYyyyM
9a040 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mDd(const char *
9a050 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20  zDate, DateTime 
9a060 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 59 2c 20 4d  *p){..  int Y, M
9a070 2c 20 44 2c 20 6e 65 67 3b 0d 0a 0d 0a 20 20 69  , D, neg;....  i
9a080 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27  f( zDate[0]=='-'
9a090 20 29 7b 0d 0a 20 20 20 20 7a 44 61 74 65 2b 2b   ){..    zDate++
9a0a0 3b 0d 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0d  ;..    neg = 1;.
9a0b0 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6e  .  }else{..    n
9a0c0 65 67 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  eg = 0;..  }..  
9a0d0 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44  if( getDigits(zD
9a0e0 61 74 65 2c 34 2c 30 2c 39 39 39 39 2c 27 2d 27  ate,4,0,9999,'-'
9a0f0 2c 26 59 2c 32 2c 31 2c 31 32 2c 27 2d 27 2c 26  ,&Y,2,1,12,'-',&
9a100 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26 44 29 21 3d  M,2,1,31,0,&D)!=
9a110 33 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  3 ){..    return
9a120 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 44 61 74   1;..  }..  zDat
9a130 65 20 2b 3d 20 31 30 3b 0d 0a 20 20 77 68 69 6c  e += 10;..  whil
9a140 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
9a150 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 27  e(*zDate) || 'T'
9a160 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b  ==*(u8*)zDate ){
9a170 20 7a 44 61 74 65 2b 2b 3b 20 7d 0d 0a 20 20 69   zDate++; }..  i
9a180 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a  f( parseHhMmSs(z
9a190 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0d 0a  Date, p)==0 ){..
9a1a0 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68      /* We got th
9a1b0 65 20 74 69 6d 65 20 2a 2f 0d 0a 20 20 7d 65 6c  e time */..  }el
9a1c0 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30  se if( *zDate==0
9a1d0 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 76 61 6c 69   ){..    p->vali
9a1e0 64 48 4d 53 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  dHMS = 0;..  }el
9a1f0 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  se{..    return 
9a200 31 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e 76 61  1;..  }..  p->va
9a210 6c 69 64 4a 44 20 3d 20 30 3b 0d 0a 20 20 70 2d  lidJD = 0;..  p-
9a220 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0d 0a  >validYMD = 1;..
9a230 20 20 70 2d 3e 59 20 3d 20 6e 65 67 20 3f 20 2d    p->Y = neg ? -
9a240 59 20 3a 20 59 3b 0d 0a 20 20 70 2d 3e 4d 20 3d  Y : Y;..  p->M =
9a250 20 4d 3b 0d 0a 20 20 70 2d 3e 44 20 3d 20 44 3b   M;..  p->D = D;
9a260 0d 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64  ..  if( p->valid
9a270 54 5a 20 29 7b 0d 0a 20 20 20 20 63 6f 6d 70 75  TZ ){..    compu
9a280 74 65 4a 44 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20  teJD(p);..  }.. 
9a290 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d   return 0;..}...
9a2a0 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
9a2b0 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72  time to the curr
9a2c0 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65  ent time reporte
9a2d0 64 20 62 79 20 74 68 65 20 56 46 53 2e 0d 0a 2a  d by the VFS...*
9a2e0 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
9a2f0 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
9a300 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
9a310 6e 74 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f  nt setDateTimeTo
9a320 43 75 72 72 65 6e 74 28 73 71 6c 69 74 65 33 5f  Current(sqlite3_
9a330 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
9a340 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0d  , DateTime *p){.
9a350 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
9a360 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
9a370 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
9a380 78 74 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  xt);..  if( sqli
9a390 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
9a3a0 49 6e 74 36 34 28 64 62 2d 3e 70 56 66 73 2c 20  Int64(db->pVfs, 
9a3b0 26 70 2d 3e 69 4a 44 29 3d 3d 53 51 4c 49 54 45  &p->iJD)==SQLITE
9a3c0 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 76  _OK ){..    p->v
9a3d0 61 6c 69 64 4a 44 20 3d 20 31 3b 0d 0a 20 20 20  alidJD = 1;..   
9a3e0 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 65   return 0;..  }e
9a3f0 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  lse{..    return
9a400 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f   1;..  }..}..../
9a410 2a 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  *..** Attempt to
9a420 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e   parse the given
9a430 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a   string into a J
9a440 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72  ulian Day Number
9a450 2e 20 20 52 65 74 75 72 6e 0d 0a 2a 2a 20 74 68  .  Return..** th
9a460 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
9a470 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  rs...**..** The 
9a480 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63  following are ac
9a490 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66  ceptable forms f
9a4a0 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  or the input str
9a4b0 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
9a4c0 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
9a4d0 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48  MM:SS.FFF  +/-HH
9a4e0 3a 4d 4d 0d 0a 2a 2a 20 20 20 20 20 20 44 44 44  :MM..**      DDD
9a4f0 44 2e 44 44 20 0d 0a 2a 2a 20 20 20 20 20 20 6e  D.DD ..**      n
9a500 6f 77 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74 68  ow..**..** In th
9a510 65 20 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 68  e first form, th
9a520 65 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c  e +/-HH:MM is al
9a530 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20  ways optional.  
9a540 54 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 0d 0a  The fractional..
9a550 2a 2a 20 73 65 63 6f 6e 64 73 20 65 78 74 65 6e  ** seconds exten
9a560 73 69 6f 6e 20 28 74 68 65 20 22 2e 46 46 46 22  sion (the ".FFF"
9a570 29 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20  ) is optional.  
9a580 54 68 65 20 73 65 63 6f 6e 64 73 20 70 6f 72 74  The seconds port
9a590 69 6f 6e 0d 0a 2a 2a 20 28 22 3a 53 53 2e 46 46  ion..** (":SS.FF
9a5a0 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20  F") is option.  
9a5b0 54 68 65 20 79 65 61 72 20 61 6e 64 20 64 61 74  The year and dat
9a5c0 65 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  e can be omitted
9a5d0 20 61 73 20 6c 6f 6e 67 0d 0a 2a 2a 20 61 73 20   as long..** as 
9a5e0 74 68 65 72 65 20 69 73 20 61 20 74 69 6d 65 20  there is a time 
9a5f0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 74 69 6d  string.  The tim
9a600 65 20 73 74 72 69 6e 67 20 63 61 6e 20 62 65 20  e string can be 
9a610 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0d  omitted as long.
9a620 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 20  .** as there is 
9a630 61 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 2e  a year and date.
9a640 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
9a650 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65   parseDateOrTime
9a660 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  (..  sqlite3_con
9a670 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0d  text *context, .
9a680 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
9a690 44 61 74 65 2c 20 0d 0a 20 20 44 61 74 65 54 69  Date, ..  DateTi
9a6a0 6d 65 20 2a 70 0d 0a 29 7b 0d 0a 20 20 64 6f 75  me *p..){..  dou
9a6b0 62 6c 65 20 72 3b 0d 0a 20 20 69 66 28 20 70 61  ble r;..  if( pa
9a6c0 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44 61 74  rseYyyyMmDd(zDat
9a6d0 65 2c 70 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  e,p)==0 ){..    
9a6e0 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 65 6c  return 0;..  }el
9a6f0 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d  se if( parseHhMm
9a700 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20  Ss(zDate, p)==0 
9a710 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
9a720 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  ;..  }else if( s
9a730 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44  qlite3StrICmp(zD
9a740 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0d  ate,"now")==0){.
9a750 0a 20 20 20 20 72 65 74 75 72 6e 20 73 65 74 44  .    return setD
9a760 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
9a770 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0d 0a 20  (context, p);.. 
9a780 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
9a790 65 33 41 74 6f 46 28 7a 44 61 74 65 2c 20 26 72  e3AtoF(zDate, &r
9a7a0 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
9a7b0 30 28 7a 44 61 74 65 29 2c 20 53 51 4c 49 54 45  0(zDate), SQLITE
9a7c0 5f 55 54 46 38 29 20 29 7b 0d 0a 20 20 20 20 70  _UTF8) ){..    p
9a7d0 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33  ->iJD = (sqlite3
9a7e0 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30  _int64)(r*864000
9a7f0 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0d 0a 20 20  00.0 + 0.5);..  
9a800 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31    p->validJD = 1
9a810 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ;..    return 0;
9a820 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
9a830 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  1;..}..../*..** 
9a840 43 6f 6d 70 75 74 65 20 74 68 65 20 59 65 61 72  Compute the Year
9a850 2c 20 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79  , Month, and Day
9a860 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e   from the julian
9a870 20 64 61 79 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2f   day number...*/
9a880 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  ..static void co
9a890 6d 70 75 74 65 59 4d 44 28 44 61 74 65 54 69 6d  mputeYMD(DateTim
9a8a0 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 5a 2c  e *p){..  int Z,
9a8b0 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45 2c 20   A, B, C, D, E, 
9a8c0 58 31 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 76 61  X1;..  if( p->va
9a8d0 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e 3b  lidYMD ) return;
9a8e0 0d 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69  ..  if( !p->vali
9a8f0 64 4a 44 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 59  dJD ){..    p->Y
9a900 20 3d 20 32 30 30 30 3b 0d 0a 20 20 20 20 70 2d   = 2000;..    p-
9a910 3e 4d 20 3d 20 31 3b 0d 0a 20 20 20 20 70 2d 3e  >M = 1;..    p->
9a920 44 20 3d 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b  D = 1;..  }else{
9a930 0d 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 28  ..    Z = (int)(
9a940 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30  (p->iJD + 432000
9a950 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0d 0a  00)/86400000);..
9a960 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a      A = (int)((Z
9a970 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33   - 1867216.25)/3
9a980 36 35 32 34 2e 32 35 29 3b 0d 0a 20 20 20 20 41  6524.25);..    A
9a990 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28   = Z + 1 + A - (
9a9a0 41 2f 34 29 3b 0d 0a 20 20 20 20 42 20 3d 20 41  A/4);..    B = A
9a9b0 20 2b 20 31 35 32 34 3b 0d 0a 20 20 20 20 43 20   + 1524;..    C 
9a9c0 3d 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32  = (int)((B - 122
9a9d0 2e 31 29 2f 33 36 35 2e 32 35 29 3b 0d 0a 20 20  .1)/365.25);..  
9a9e0 20 20 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f    D = (36525*C)/
9a9f0 31 30 30 3b 0d 0a 20 20 20 20 45 20 3d 20 28 69  100;..    E = (i
9aa00 6e 74 29 28 28 42 2d 44 29 2f 33 30 2e 36 30 30  nt)((B-D)/30.600
9aa10 31 29 3b 0d 0a 20 20 20 20 58 31 20 3d 20 28 69  1);..    X1 = (i
9aa20 6e 74 29 28 33 30 2e 36 30 30 31 2a 45 29 3b 0d  nt)(30.6001*E);.
9aa30 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20  .    p->D = B - 
9aa40 44 20 2d 20 58 31 3b 0d 0a 20 20 20 20 70 2d 3e  D - X1;..    p->
9aa50 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a  M = E<14 ? E-1 :
9aa60 20 45 2d 31 33 3b 0d 0a 20 20 20 20 70 2d 3e 59   E-13;..    p->Y
9aa70 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20   = p->M>2 ? C - 
9aa80 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 35 3b  4716 : C - 4715;
9aa90 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e 76 61 6c 69  ..  }..  p->vali
9aaa0 64 59 4d 44 20 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a  dYMD = 1;..}....
9aab0 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  /*..** Compute t
9aac0 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c  he Hour, Minute,
9aad0 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f   and Seconds fro
9aae0 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  m the julian day
9aaf0 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2f 0d 0a 73 74   number...*/..st
9ab00 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
9ab10 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70  eHMS(DateTime *p
9ab20 29 7b 0d 0a 20 20 69 6e 74 20 73 3b 0d 0a 20 20  ){..  int s;..  
9ab30 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20  if( p->validHMS 
9ab40 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 63 6f 6d  ) return;..  com
9ab50 70 75 74 65 4a 44 28 70 29 3b 0d 0a 20 20 73 20  puteJD(p);..  s 
9ab60 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20  = (int)((p->iJD 
9ab70 2b 20 34 33 32 30 30 30 30 30 29 20 25 20 38 36  + 43200000) % 86
9ab80 34 30 30 30 30 30 29 3b 0d 0a 20 20 70 2d 3e 73  400000);..  p->s
9ab90 20 3d 20 73 2f 31 30 30 30 2e 30 3b 0d 0a 20 20   = s/1000.0;..  
9aba0 73 20 3d 20 28 69 6e 74 29 70 2d 3e 73 3b 0d 0a  s = (int)p->s;..
9abb0 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0d 0a 20 20    p->s -= s;..  
9abc0 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0d 0a  p->h = s/3600;..
9abd0 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30    s -= p->h*3600
9abe0 3b 0d 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30  ;..  p->m = s/60
9abf0 3b 0d 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d  ;..  p->s += s -
9ac00 20 70 2d 3e 6d 2a 36 30 3b 0d 0a 20 20 70 2d 3e   p->m*60;..  p->
9ac10 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0d 0a 7d  validHMS = 1;..}
9ac20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75  ..../*..** Compu
9ac30 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20  te both YMD and 
9ac40 48 4d 53 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  HMS..*/..static 
9ac50 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f  void computeYMD_
9ac60 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29  HMS(DateTime *p)
9ac70 7b 0d 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28  {..  computeYMD(
9ac80 70 29 3b 0d 0a 20 20 63 6f 6d 70 75 74 65 48 4d  p);..  computeHM
9ac90 53 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  S(p);..}..../*..
9aca0 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d 44  ** Clear the YMD
9acb0 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 65   and HMS and the
9acc0 20 54 5a 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20   TZ..*/..static 
9acd0 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d  void clearYMD_HM
9ace0 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70  S_TZ(DateTime *p
9acf0 29 7b 0d 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d  ){..  p->validYM
9ad00 44 20 3d 20 30 3b 0d 0a 20 20 70 2d 3e 76 61 6c  D = 0;..  p->val
9ad10 69 64 48 4d 53 20 3d 20 30 3b 0d 0a 20 20 70 2d  idHMS = 0;..  p-
9ad20 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0d 0a 7d  >validTZ = 0;..}
9ad30 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 6e 20 72 65  ..../*..** On re
9ad40 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61  cent Windows pla
9ad50 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61  tforms, the loca
9ad60 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69  ltime_s() functi
9ad70 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0d  on is available.
9ad80 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74  .** as part of t
9ad90 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e  he "Secure CRT".
9ada0 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c   It is essential
9adb0 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  ly equivalent to
9adc0 20 0d 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f   ..** localtime_
9add0 72 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e  r() available un
9ade0 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70  der most POSIX p
9adf0 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74  latforms, except
9ae00 20 74 68 61 74 20 74 68 65 20 0d 0a 2a 2a 20 6f   that the ..** o
9ae10 72 64 65 72 20 6f 66 20 74 68 65 20 70 61 72 61  rder of the para
9ae20 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73  meters is revers
9ae30 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20  ed...**..** See 
9ae40 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72  http://msdn.micr
9ae50 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f  osoft.com/en-us/
9ae60 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 65  library/a442x3ye
9ae70 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0d 0a 2a  (VS.80).aspx...*
9ae80 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65  *..** If the use
9ae90 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61  r has not indica
9aea0 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c  ted to use local
9aeb0 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61  time_r() or loca
9aec0 6c 74 69 6d 65 5f 73 28 29 0d 0a 2a 2a 20 61 6c  ltime_s()..** al
9aed0 72 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72  ready, check for
9aee0 20 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65   an MSVC build e
9aef0 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20  nvironment that 
9af00 70 72 6f 76 69 64 65 73 20 0d 0a 2a 2a 20 6c 6f  provides ..** lo
9af10 63 61 6c 74 69 6d 65 5f 73 28 29 2e 0d 0a 2a 2f  caltime_s()...*/
9af20 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 48  ..#if !defined(H
9af30 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29  AVE_LOCALTIME_R)
9af40 20 26 26 20 21 64 65 66 69 6e 65 64 28 48 41 56   && !defined(HAV
9af50 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26  E_LOCALTIME_S) &
9af60 26 20 5c 0d 0a 20 20 20 20 20 64 65 66 69 6e 65  & \..     define
9af70 64 28 5f 4d 53 43 5f 56 45 52 29 20 26 26 20 64  d(_MSC_VER) && d
9af80 65 66 69 6e 65 64 28 5f 43 52 54 5f 49 4e 53 45  efined(_CRT_INSE
9af90 43 55 52 45 5f 44 45 50 52 45 43 41 54 45 29 0d  CURE_DEPRECATE).
9afa0 0a 23 64 65 66 69 6e 65 20 48 41 56 45 5f 4c 4f  .#define HAVE_LO
9afb0 43 41 4c 54 49 4d 45 5f 53 20 31 0d 0a 23 65 6e  CALTIME_S 1..#en
9afc0 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  dif....#ifndef S
9afd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c  QLITE_OMIT_LOCAL
9afe0 54 49 4d 45 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  TIME../*..** The
9aff0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
9b000 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  ne implements th
9b010 65 20 72 6f 75 67 68 20 65 71 75 69 76 61 6c 65  e rough equivale
9b020 6e 74 20 6f 66 20 6c 6f 63 61 6c 74 69 6d 65 5f  nt of localtime_
9b030 72 28 29 0d 0a 2a 2a 20 75 73 69 6e 67 20 77 68  r()..** using wh
9b040 61 74 65 76 65 72 20 6f 70 65 72 61 74 69 6e 67  atever operating
9b050 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63  -system specific
9b060 20 6c 6f 63 61 6c 74 69 6d 65 20 66 61 63 69 6c   localtime facil
9b070 69 74 79 20 74 68 61 74 0d 0a 2a 2a 20 69 73 20  ity that..** is 
9b080 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73  available.  This
9b090 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
9b0a0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
9b0b0 64 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 6f  d..** non-zero o
9b0c0 6e 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72  n any kind of er
9b0d0 72 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ror...**..** If 
9b0e0 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  the sqlite3Globa
9b0f0 6c 43 6f 6e 66 69 67 2e 62 4c 6f 63 61 6c 74 69  lConfig.bLocalti
9b100 6d 65 46 61 75 6c 74 20 76 61 72 69 61 62 6c 65  meFault variable
9b110 20 69 73 20 74 72 75 65 20 74 68 65 6e 20 74 68   is true then th
9b120 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77  is..** routine w
9b130 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69 6c 2e  ill always fail.
9b140 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
9b150 20 6f 73 4c 6f 63 61 6c 74 69 6d 65 28 74 69 6d   osLocaltime(tim
9b160 65 5f 74 20 2a 74 2c 20 73 74 72 75 63 74 20 74  e_t *t, struct t
9b170 6d 20 2a 70 54 6d 29 7b 0d 0a 20 20 69 6e 74 20  m *pTm){..  int 
9b180 72 63 3b 0d 0a 23 69 66 20 28 21 64 65 66 69 6e  rc;..#if (!defin
9b190 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
9b1a0 45 5f 52 29 20 7c 7c 20 21 48 41 56 45 5f 4c 4f  E_R) || !HAVE_LO
9b1b0 43 41 4c 54 49 4d 45 5f 52 29 20 5c 0d 0a 20 20  CALTIME_R) \..  
9b1c0 20 20 20 20 26 26 20 28 21 64 65 66 69 6e 65 64      && (!defined
9b1d0 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f  (HAVE_LOCALTIME_
9b1e0 53 29 20 7c 7c 20 21 48 41 56 45 5f 4c 4f 43 41  S) || !HAVE_LOCA
9b1f0 4c 54 49 4d 45 5f 53 29 0d 0a 20 20 73 74 72 75  LTIME_S)..  stru
9b200 63 74 20 74 6d 20 2a 70 58 3b 0d 0a 23 69 66 20  ct tm *pX;..#if 
9b210 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9b220 45 3e 30 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  E>0..  sqlite3_m
9b230 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
9b240 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
9b250 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
9b260 54 49 43 5f 4d 41 53 54 45 52 29 3b 0d 0a 23 65  TIC_MASTER);..#e
9b270 6e 64 69 66 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ndif..  sqlite3_
9b280 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
9b290 78 29 3b 0d 0a 20 20 70 58 20 3d 20 6c 6f 63 61  x);..  pX = loca
9b2a0 6c 74 69 6d 65 28 74 29 3b 0d 0a 23 69 66 6e 64  ltime(t);..#ifnd
9b2b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
9b2c0 55 49 4c 54 49 4e 5f 54 45 53 54 0d 0a 20 20 69  UILTIN_TEST..  i
9b2d0 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
9b2e0 43 6f 6e 66 69 67 2e 62 4c 6f 63 61 6c 74 69 6d  Config.bLocaltim
9b2f0 65 46 61 75 6c 74 20 29 20 70 58 20 3d 20 30 3b  eFault ) pX = 0;
9b300 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 66 28 20  ..#endif..  if( 
9b310 70 58 20 29 20 2a 70 54 6d 20 3d 20 2a 70 58 3b  pX ) *pTm = *pX;
9b320 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
9b330 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0d  x_leave(mutex);.
9b340 0a 20 20 72 63 20 3d 20 70 58 3d 3d 30 3b 0d 0a  .  rc = pX==0;..
9b350 23 65 6c 73 65 0d 0a 23 69 66 6e 64 65 66 20 53  #else..#ifndef S
9b360 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
9b370 49 4e 5f 54 45 53 54 0d 0a 20 20 69 66 28 20 73  IN_TEST..  if( s
9b380 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b390 69 67 2e 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75  ig.bLocaltimeFau
9b3a0 6c 74 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a  lt ) return 1;..
9b3b0 23 65 6e 64 69 66 0d 0a 23 69 66 20 64 65 66 69  #endif..#if defi
9b3c0 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49  ned(HAVE_LOCALTI
9b3d0 4d 45 5f 52 29 20 26 26 20 48 41 56 45 5f 4c 4f  ME_R) && HAVE_LO
9b3e0 43 41 4c 54 49 4d 45 5f 52 0d 0a 20 20 72 63 20  CALTIME_R..  rc 
9b3f0 3d 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 74 2c  = localtime_r(t,
9b400 20 70 54 6d 29 3d 3d 30 3b 0d 0a 23 65 6c 73 65   pTm)==0;..#else
9b410 0d 0a 20 20 72 63 20 3d 20 6c 6f 63 61 6c 74 69  ..  rc = localti
9b420 6d 65 5f 73 28 70 54 6d 2c 20 74 29 3b 0d 0a 23  me_s(pTm, t);..#
9b430 65 6e 64 69 66 20 2f 2a 20 48 41 56 45 5f 4c 4f  endif /* HAVE_LO
9b440 43 41 4c 54 49 4d 45 5f 52 20 2a 2f 0d 0a 23 65  CALTIME_R */..#e
9b450 6e 64 69 66 20 2f 2a 20 48 41 56 45 5f 4c 4f 43  ndif /* HAVE_LOC
9b460 41 4c 54 49 4d 45 5f 52 20 7c 7c 20 48 41 56 45  ALTIME_R || HAVE
9b470 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 2a 2f 0d  _LOCALTIME_S */.
9b480 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
9b490 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
9b4a0 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d  TE_OMIT_LOCALTIM
9b4b0 45 20 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65  E */......#ifnde
9b4c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
9b4d0 43 41 4c 54 49 4d 45 0d 0a 2f 2a 0d 0a 2a 2a 20  CALTIME../*..** 
9b4e0 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66  Compute the diff
9b4f0 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c 69  erence (in milli
9b500 73 65 63 6f 6e 64 73 29 20 62 65 74 77 65 65 6e  seconds) between
9b510 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e 64 20 55   localtime and U
9b520 54 43 0d 0a 2a 2a 20 28 61 2e 6b 2e 61 2e 20 47  TC..** (a.k.a. G
9b530 4d 54 29 20 66 6f 72 20 74 68 65 20 74 69 6d 65  MT) for the time
9b540 20 76 61 6c 75 65 20 70 20 77 68 65 72 65 20 70   value p where p
9b550 20 69 73 20 69 6e 20 55 54 43 2e 20 49 66 20 6e   is in UTC. If n
9b560 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0d  o error occurs,.
9b570 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 69 73 20  .** return this 
9b580 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 2a 70  value and set *p
9b590 52 63 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e  Rc to SQLITE_OK.
9b5a0 20 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 72 2c 20 69 66   ..**..** Or, if
9b5b0 20 61 6e 20 65 72 72 6f 72 20 64 6f 65 73 20 6f   an error does o
9b5c0 63 63 75 72 2c 20 73 65 74 20 2a 70 52 63 20 74  ccur, set *pRc t
9b5d0 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20  o SQLITE_ERROR. 
9b5e0 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
9b5f0 75 65 0d 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ue..** is undefi
9b600 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  ned in this case
9b610 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 71  ...*/..static sq
9b620 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61  lite3_int64 loca
9b630 6c 74 69 6d 65 4f 66 66 73 65 74 28 0d 0a 20 20  ltimeOffset(..  
9b640 44 61 74 65 54 69 6d 65 20 2a 70 2c 20 20 20 20  DateTime *p,    
9b650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b660 2f 2a 20 44 61 74 65 20 61 74 20 77 68 69 63 68  /* Date at which
9b670 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 6f 66   to calculate of
9b680 66 73 65 74 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  fset */..  sqlit
9b690 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
9b6a0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ,          /* Wr
9b6b0 69 74 65 20 65 72 72 6f 72 20 68 65 72 65 20 69  ite error here i
9b6c0 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 2a 2f 0d  f one occurs */.
9b6d0 0a 20 20 69 6e 74 20 2a 70 52 63 20 20 20 20 20  .  int *pRc     
9b6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b6f0 20 20 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72     /* OUT: Error
9b700 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f 4f 4b   code. SQLITE_OK
9b710 20 6f 72 20 45 52 52 4f 52 20 2a 2f 0d 0a 29 7b   or ERROR */..){
9b720 0d 0a 20 20 44 61 74 65 54 69 6d 65 20 78 2c 20  ..  DateTime x, 
9b730 79 3b 0d 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0d  y;..  time_t t;.
9b740 0a 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f  .  struct tm sLo
9b750 63 61 6c 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 69  cal;....  /* Ini
9b760 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74  tialize the cont
9b770 65 6e 74 73 20 6f 66 20 73 4c 6f 63 61 6c 20 74  ents of sLocal t
9b780 6f 20 61 76 6f 69 64 20 61 20 63 6f 6d 70 69 6c  o avoid a compil
9b790 65 72 20 77 61 72 6e 69 6e 67 2e 20 2a 2f 0d 0a  er warning. */..
9b7a0 20 20 6d 65 6d 73 65 74 28 26 73 4c 6f 63 61 6c    memset(&sLocal
9b7b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4c 6f 63  , 0, sizeof(sLoc
9b7c0 61 6c 29 29 3b 0d 0a 0d 0a 20 20 78 20 3d 20 2a  al));....  x = *
9b7d0 70 3b 0d 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44  p;..  computeYMD
9b7e0 5f 48 4d 53 28 26 78 29 3b 0d 0a 20 20 69 66 28  _HMS(&x);..  if(
9b7f0 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59   x.Y<1971 || x.Y
9b800 3e 3d 32 30 33 38 20 29 7b 0d 0a 20 20 20 20 78  >=2038 ){..    x
9b810 2e 59 20 3d 20 32 30 30 30 3b 0d 0a 20 20 20 20  .Y = 2000;..    
9b820 78 2e 4d 20 3d 20 31 3b 0d 0a 20 20 20 20 78 2e  x.M = 1;..    x.
9b830 44 20 3d 20 31 3b 0d 0a 20 20 20 20 78 2e 68 20  D = 1;..    x.h 
9b840 3d 20 30 3b 0d 0a 20 20 20 20 78 2e 6d 20 3d 20  = 0;..    x.m = 
9b850 30 3b 0d 0a 20 20 20 20 78 2e 73 20 3d 20 30 2e  0;..    x.s = 0.
9b860 30 3b 0d 0a 20 20 7d 20 65 6c 73 65 20 7b 0d 0a  0;..  } else {..
9b870 20 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74      int s = (int
9b880 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0d 0a 20  )(x.s + 0.5);.. 
9b890 20 20 20 78 2e 73 20 3d 20 73 3b 0d 0a 20 20 7d     x.s = s;..  }
9b8a0 0d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0d 0a 20  ..  x.tz = 0;.. 
9b8b0 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0d   x.validJD = 0;.
9b8c0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29  .  computeJD(&x)
9b8d0 3b 0d 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f 74  ;..  t = (time_t
9b8e0 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32  )(x.iJD/1000 - 2
9b8f0 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30  1086676*(i64)100
9b900 30 30 29 3b 0d 0a 20 20 69 66 28 20 6f 73 4c 6f  00);..  if( osLo
9b910 63 61 6c 74 69 6d 65 28 26 74 2c 20 26 73 4c 6f  caltime(&t, &sLo
9b920 63 61 6c 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  cal) ){..    sql
9b930 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
9b940 72 28 70 43 74 78 2c 20 22 6c 6f 63 61 6c 20 74  r(pCtx, "local t
9b950 69 6d 65 20 75 6e 61 76 61 69 6c 61 62 6c 65 22  ime unavailable"
9b960 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 2a 70 52 63  , -1);..    *pRc
9b970 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
9b980 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
9b990 0a 20 20 7d 0d 0a 20 20 79 2e 59 20 3d 20 73 4c  .  }..  y.Y = sL
9b9a0 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31  ocal.tm_year + 1
9b9b0 39 30 30 3b 0d 0a 20 20 79 2e 4d 20 3d 20 73 4c  900;..  y.M = sL
9b9c0 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b  ocal.tm_mon + 1;
9b9d0 0d 0a 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c  ..  y.D = sLocal
9b9e0 2e 74 6d 5f 6d 64 61 79 3b 0d 0a 20 20 79 2e 68  .tm_mday;..  y.h
9b9f0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75   = sLocal.tm_hou
9ba00 72 3b 0d 0a 20 20 79 2e 6d 20 3d 20 73 4c 6f 63  r;..  y.m = sLoc
9ba10 61 6c 2e 74 6d 5f 6d 69 6e 3b 0d 0a 20 20 79 2e  al.tm_min;..  y.
9ba20 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65  s = sLocal.tm_se
9ba30 63 3b 0d 0a 20 20 79 2e 76 61 6c 69 64 59 4d 44  c;..  y.validYMD
9ba40 20 3d 20 31 3b 0d 0a 20 20 79 2e 76 61 6c 69 64   = 1;..  y.valid
9ba50 48 4d 53 20 3d 20 31 3b 0d 0a 20 20 79 2e 76 61  HMS = 1;..  y.va
9ba60 6c 69 64 4a 44 20 3d 20 30 3b 0d 0a 20 20 79 2e  lidJD = 0;..  y.
9ba70 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0d 0a 20 20  validTZ = 0;..  
9ba80 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0d 0a  computeJD(&y);..
9ba90 20 20 2a 70 52 63 20 3d 20 53 51 4c 49 54 45 5f    *pRc = SQLITE_
9baa0 4f 4b 3b 0d 0a 20 20 72 65 74 75 72 6e 20 79 2e  OK;..  return y.
9bab0 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0d 0a 7d 0d  iJD - x.iJD;..}.
9bac0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
9bad0 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
9bae0 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72   */..../*..** Pr
9baf0 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 69 65 72  ocess a modifier
9bb00 20 74 6f 20 61 20 64 61 74 65 2d 74 69 6d 65 20   to a date-time 
9bb10 73 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f 64 69  stamp.  The modi
9bb20 66 69 65 72 73 20 61 72 65 0d 0a 2a 2a 20 61 73  fiers are..** as
9bb30 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a   follows:..**..*
9bb40 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0d 0a  *     NNN days..
9bb50 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73  **     NNN hours
9bb60 0d 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e  ..**     NNN min
9bb70 75 74 65 73 0d 0a 2a 2a 20 20 20 20 20 4e 4e 4e  utes..**     NNN
9bb80 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0d 0a 2a  .NNNN seconds..*
9bb90 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73  *     NNN months
9bba0 0d 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61  ..**     NNN yea
9bbb0 72 73 0d 0a 2a 2a 20 20 20 20 20 73 74 61 72 74  rs..**     start
9bbc0 20 6f 66 20 6d 6f 6e 74 68 0d 0a 2a 2a 20 20 20   of month..**   
9bbd0 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 0d    start of year.
9bbe0 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66  .**     start of
9bbf0 20 77 65 65 6b 0d 0a 2a 2a 20 20 20 20 20 73 74   week..**     st
9bc00 61 72 74 20 6f 66 20 64 61 79 0d 0a 2a 2a 20 20  art of day..**  
9bc10 20 20 20 77 65 65 6b 64 61 79 20 4e 0d 0a 2a 2a     weekday N..**
9bc20 20 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0d 0a       unixepoch..
9bc30 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65  **     localtime
9bc40 0d 0a 2a 2a 20 20 20 20 20 75 74 63 0d 0a 2a 2a  ..**     utc..**
9bc50 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e  ..** Return 0 on
9bc60 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 69   success and 1 i
9bc70 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
9bc80 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 20 49 66  ind of error. If
9bc90 20 74 68 65 20 65 72 72 6f 72 0d 0a 2a 2a 20 69   the error..** i
9bca0 73 20 69 6e 20 61 20 73 79 73 74 65 6d 20 63 61  s in a system ca
9bcb0 6c 6c 20 28 69 2e 65 2e 20 6c 6f 63 61 6c 74 69  ll (i.e. localti
9bcc0 6d 65 28 29 29 2c 20 74 68 65 6e 20 61 6e 20 65  me()), then an e
9bcd0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
9bce0 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 74 6f 20 63  written..** to c
9bcf0 6f 6e 74 65 78 74 20 70 43 74 78 2e 20 49 66 20  ontext pCtx. If 
9bd00 74 68 65 20 65 72 72 6f 72 20 69 73 20 61 6e 20  the error is an 
9bd10 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 6d 6f 64  unrecognized mod
9bd20 69 66 69 65 72 2c 20 6e 6f 20 65 72 72 6f 72 20  ifier, no error 
9bd30 69 73 0d 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74  is..** written t
9bd40 6f 20 70 43 74 78 2e 0d 0a 2a 2f 0d 0a 73 74 61  o pCtx...*/..sta
9bd50 74 69 63 20 69 6e 74 20 70 61 72 73 65 4d 6f 64  tic int parseMod
9bd60 69 66 69 65 72 28 73 71 6c 69 74 65 33 5f 63 6f  ifier(sqlite3_co
9bd70 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e  ntext *pCtx, con
9bd80 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44  st char *zMod, D
9bd90 61 74 65 54 69 6d 65 20 2a 70 29 7b 0d 0a 20 20  ateTime *p){..  
9bda0 69 6e 74 20 72 63 20 3d 20 31 3b 0d 0a 20 20 69  int rc = 1;..  i
9bdb0 6e 74 20 6e 3b 0d 0a 20 20 64 6f 75 62 6c 65 20  nt n;..  double 
9bdc0 72 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a  r;..  char *z, z
9bdd0 42 75 66 5b 33 30 5d 3b 0d 0a 20 20 7a 20 3d 20  Buf[30];..  z = 
9bde0 7a 42 75 66 3b 0d 0a 20 20 66 6f 72 28 6e 3d 30  zBuf;..  for(n=0
9bdf0 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42  ; n<ArraySize(zB
9be00 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d  uf)-1 && zMod[n]
9be10 3b 20 6e 2b 2b 29 7b 0d 0a 20 20 20 20 7a 5b 6e  ; n++){..    z[n
9be20 5d 20 3d 20 28 63 68 61 72 29 6f 61 73 55 70 70  ] = (char)oasUpp
9be30 65 72 32 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f  er2Lower[(u8)zMo
9be40 64 5b 6e 5d 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 7a  d[n]];..  }..  z
9be50 5b 6e 5d 20 3d 20 30 3b 0d 0a 20 20 73 77 69 74  [n] = 0;..  swit
9be60 63 68 28 20 7a 5b 30 5d 20 29 7b 0d 0a 23 69 66  ch( z[0] ){..#if
9be70 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9be80 5f 4c 4f 43 41 4c 54 49 4d 45 0d 0a 20 20 20 20  _LOCALTIME..    
9be90 63 61 73 65 20 27 6c 27 3a 20 7b 0d 0a 20 20 20  case 'l': {..   
9bea0 20 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69     /*    localti
9beb0 6d 65 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20  me..      **..  
9bec0 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20      ** Assuming 
9bed0 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65  the current time
9bee0 20 76 61 6c 75 65 20 69 73 20 55 54 43 20 28 61   value is UTC (a
9bef0 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 68 69 66  .k.a. GMT), shif
9bf00 74 20 69 74 20 74 6f 0d 0a 20 20 20 20 20 20 2a  t it to..      *
9bf10 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 69 6d  * show local tim
9bf20 65 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  e...      */..  
9bf30 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
9bf40 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d  , "localtime")==
9bf50 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f  0 ){..        co
9bf60 6d 70 75 74 65 4a 44 28 70 29 3b 0d 0a 20 20 20  mputeJD(p);..   
9bf70 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c       p->iJD += l
9bf80 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70  ocaltimeOffset(p
9bf90 2c 20 70 43 74 78 2c 20 26 72 63 29 3b 0d 0a 20  , pCtx, &rc);.. 
9bfa0 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f         clearYMD_
9bfb0 48 4d 53 5f 54 5a 28 70 29 3b 0d 0a 20 20 20 20  HMS_TZ(p);..    
9bfc0 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b    }..      break
9bfd0 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  ;..    }..#endif
9bfe0 0d 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a 20  ..    case 'u': 
9bff0 7b 0d 0a 20 20 20 20 20 20 2f 2a 0d 0a 20 20 20  {..      /*..   
9c000 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f     **    unixepo
9c010 63 68 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ch..      **..  
9c020 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65      ** Treat the
9c030 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f   current value o
9c040 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20  f p->iJD as the 
9c050 6e 75 6d 62 65 72 20 6f 66 0d 0a 20 20 20 20 20  number of..     
9c060 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   ** seconds sinc
9c070 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 74  e 1970.  Convert
9c080 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 61   to a real julia
9c090 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0d 0a 20  n day number... 
9c0a0 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69       */..      i
9c0b0 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e  f( strcmp(z, "un
9c0c0 69 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20  ixepoch")==0 && 
9c0d0 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0d 0a 20  p->validJD ){.. 
9c0e0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20         p->iJD = 
9c0f0 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29  (p->iJD + 43200)
9c100 2f 38 36 34 30 30 20 2b 20 32 31 30 38 36 36 37  /86400 + 2108667
9c110 36 2a 28 69 36 34 29 31 30 30 30 30 30 30 30 3b  6*(i64)10000000;
9c120 0d 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59  ..        clearY
9c130 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0d 0a 20  MD_HMS_TZ(p);.. 
9c140 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a         rc = 0;..
9c150 20 20 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66        }..#ifndef
9c160 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43   SQLITE_OMIT_LOC
9c170 41 4c 54 49 4d 45 0d 0a 20 20 20 20 20 20 65 6c  ALTIME..      el
9c180 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
9c190 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0d 0a 20   "utc")==0 ){.. 
9c1a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
9c1b0 6e 74 36 34 20 63 31 3b 0d 0a 20 20 20 20 20 20  nt64 c1;..      
9c1c0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0d    computeJD(p);.
9c1d0 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f  .        c1 = lo
9c1e0 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 2c  caltimeOffset(p,
9c1f0 20 70 43 74 78 2c 20 26 72 63 29 3b 0d 0a 20 20   pCtx, &rc);..  
9c200 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
9c210 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
9c220 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20        p->iJD -= 
9c230 63 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 63  c1;..          c
9c240 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
9c250 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d  );..          p-
9c260 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63  >iJD += c1 - loc
9c270 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 2c 20  altimeOffset(p, 
9c280 70 43 74 78 2c 20 26 72 63 29 3b 0d 0a 20 20 20  pCtx, &rc);..   
9c290 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
9c2a0 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 62  .#endif..      b
9c2b0 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
9c2c0 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0d 0a 20    case 'w': {.. 
9c2d0 20 20 20 20 20 2f 2a 0d 0a 20 20 20 20 20 20 2a       /*..      *
9c2e0 2a 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0d 0a  *    weekday N..
9c2f0 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
9c300 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65  ** Move the date
9c310 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 6d   to the same tim
9c320 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f 63  e on the next oc
9c330 63 75 72 72 65 6e 63 65 20 6f 66 0d 0a 20 20 20  currence of..   
9c340 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20     ** weekday N 
9c350 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c  where 0==Sunday,
9c360 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20   1==Monday, and 
9c370 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68  so forth.  If th
9c380 65 0d 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65  e..      ** date
9c390 20 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74   is already on t
9c3a0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77  he appropriate w
9c3b0 65 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20  eekday, this is 
9c3c0 61 20 6e 6f 2d 6f 70 2e 0d 0a 20 20 20 20 20 20  a no-op...      
9c3d0 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 73 74  */..      if( st
9c3e0 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61  rncmp(z, "weekda
9c3f0 79 20 22 2c 20 38 29 3d 3d 30 0d 0a 20 20 20 20  y ", 8)==0..    
9c400 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71             && sq
9c410 6c 69 74 65 33 41 74 6f 46 28 26 7a 5b 38 5d 2c  lite3AtoF(&z[8],
9c420 20 26 72 2c 20 73 71 6c 69 74 65 33 53 74 72 6c   &r, sqlite3Strl
9c430 65 6e 33 30 28 26 7a 5b 38 5d 29 2c 20 53 51 4c  en30(&z[8]), SQL
9c440 49 54 45 5f 55 54 46 38 29 0d 0a 20 20 20 20 20  ITE_UTF8)..     
9c450 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6e 3d            && (n=
9c460 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 6e 3e  (int)r)==r && n>
9c470 3d 30 20 26 26 20 72 3c 37 20 29 7b 0d 0a 20 20  =0 && r<7 ){..  
9c480 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
9c490 74 36 34 20 5a 3b 0d 0a 20 20 20 20 20 20 20 20  t64 Z;..        
9c4a0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
9c4b0 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 76  );..        p->v
9c4c0 61 6c 69 64 54 5a 20 3d 20 30 3b 0d 0a 20 20 20  alidTZ = 0;..   
9c4d0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20       p->validJD 
9c4e0 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 63 6f  = 0;..        co
9c4f0 6d 70 75 74 65 4a 44 28 70 29 3b 0d 0a 20 20 20  mputeJD(p);..   
9c500 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a       Z = ((p->iJ
9c510 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f 38  D + 129600000)/8
9c520 36 34 30 30 30 30 30 29 20 25 20 37 3b 0d 0a 20  6400000) % 7;.. 
9c530 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29         if( Z>n )
9c540 20 5a 20 2d 3d 20 37 3b 0d 0a 20 20 20 20 20 20   Z -= 7;..      
9c550 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d    p->iJD += (n -
9c560 20 5a 29 2a 38 36 34 30 30 30 30 30 3b 0d 0a 20   Z)*86400000;.. 
9c570 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f         clearYMD_
9c580 48 4d 53 5f 54 5a 28 70 29 3b 0d 0a 20 20 20 20  HMS_TZ(p);..    
9c590 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a 20 20 20      rc = 0;..   
9c5a0 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61     }..      brea
9c5b0 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  k;..    }..    c
9c5c0 61 73 65 20 27 73 27 3a 20 7b 0d 0a 20 20 20 20  ase 's': {..    
9c5d0 20 20 2f 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 20    /*..      **  
9c5e0 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 54 54    start of TTTTT
9c5f0 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20  ..      **..    
9c600 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61    ** Move the da
9c610 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20  te backwards to 
9c620 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
9c630 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79   the current day
9c640 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d  ,..      ** or m
9c650 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e 0d 0a 20  onth or year... 
9c660 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69       */..      i
9c670 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73  f( strncmp(z, "s
9c680 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 3d 30  tart of ", 9)!=0
9c690 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
9c6a0 20 7a 20 2b 3d 20 39 3b 0d 0a 20 20 20 20 20 20   z += 9;..      
9c6b0 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0d 0a  computeYMD(p);..
9c6c0 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d        p->validHM
9c6d0 53 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 70 2d  S = 1;..      p-
9c6e0 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0d 0a  >h = p->m = 0;..
9c6f0 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30        p->s = 0.0
9c700 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  ;..      p->vali
9c710 64 54 5a 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  dTZ = 0;..      
9c720 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0d  p->validJD = 0;.
9c730 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
9c740 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20  p(z,"month")==0 
9c750 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 44  ){..        p->D
9c760 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 72   = 1;..        r
9c770 63 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 65  c = 0;..      }e
9c780 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
9c790 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0d 0a  ,"year")==0 ){..
9c7a0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59          computeY
9c7b0 4d 44 28 70 29 3b 0d 0a 20 20 20 20 20 20 20 20  MD(p);..        
9c7c0 70 2d 3e 4d 20 3d 20 31 3b 0d 0a 20 20 20 20 20  p->M = 1;..     
9c7d0 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0d 0a 20 20     p->D = 1;..  
9c7e0 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a 20        rc = 0;.. 
9c7f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
9c800 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d  trcmp(z,"day")==
9c810 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63  0 ){..        rc
9c820 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a   = 0;..      }..
9c830 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
9c840 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 27 2b    }..    case '+
9c850 27 3a 0d 0a 20 20 20 20 63 61 73 65 20 27 2d 27  ':..    case '-'
9c860 3a 0d 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a  :..    case '0':
9c870 0d 0a 20 20 20 20 63 61 73 65 20 27 31 27 3a 0d  ..    case '1':.
9c880 0a 20 20 20 20 63 61 73 65 20 27 32 27 3a 0d 0a  .    case '2':..
9c890 20 20 20 20 63 61 73 65 20 27 33 27 3a 0d 0a 20      case '3':.. 
9c8a0 20 20 20 63 61 73 65 20 27 34 27 3a 0d 0a 20 20     case '4':..  
9c8b0 20 20 63 61 73 65 20 27 35 27 3a 0d 0a 20 20 20    case '5':..   
9c8c0 20 63 61 73 65 20 27 36 27 3a 0d 0a 20 20 20 20   case '6':..    
9c8d0 63 61 73 65 20 27 37 27 3a 0d 0a 20 20 20 20 63  case '7':..    c
9c8e0 61 73 65 20 27 38 27 3a 0d 0a 20 20 20 20 63 61  ase '8':..    ca
9c8f0 73 65 20 27 39 27 3a 20 7b 0d 0a 20 20 20 20 20  se '9': {..     
9c900 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 72   double rRounder
9c910 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31  ;..      for(n=1
9c920 3b 20 7a 5b 6e 5d 20 26 26 20 7a 5b 6e 5d 21 3d  ; z[n] && z[n]!=
9c930 27 3a 27 20 26 26 20 21 73 71 6c 69 74 65 33 49  ':' && !sqlite3I
9c940 73 73 70 61 63 65 28 7a 5b 6e 5d 29 3b 20 6e 2b  sspace(z[n]); n+
9c950 2b 29 7b 7d 0d 0a 20 20 20 20 20 20 69 66 28 20  +){}..      if( 
9c960 21 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20  !sqlite3AtoF(z, 
9c970 26 72 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54  &r, n, SQLITE_UT
9c980 46 38 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  F8) ){..        
9c990 72 63 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  rc = 1;..       
9c9a0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
9c9b0 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d  ..      if( z[n]
9c9c0 3d 3d 27 3a 27 20 29 7b 0d 0a 20 20 20 20 20 20  ==':' ){..      
9c9d0 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20    /* A modifier 
9c9e0 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d  of the form (+|-
9c9f0 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64  )HH:MM:SS.FFF ad
9ca00 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73  ds (or subtracts
9ca10 29 20 74 68 65 0d 0a 20 20 20 20 20 20 20 20 2a  ) the..        *
9ca20 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62  * specified numb
9ca30 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e  er of hours, min
9ca40 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61  utes, seconds, a
9ca50 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65  nd fractional se
9ca60 63 6f 6e 64 73 0d 0a 20 20 20 20 20 20 20 20 2a  conds..        *
9ca70 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20  * to the time.  
9ca80 54 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62  The ".FFF" may b
9ca90 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20  e omitted.  The 
9caa0 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65  ":SS.FFF" may be
9cab0 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69  ..        ** omi
9cac0 74 74 65 64 2e 0d 0a 20 20 20 20 20 20 20 20 2a  tted...        *
9cad0 2f 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  /..        const
9cae0 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0d 0a   char *z2 = z;..
9caf0 20 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65          DateTime
9cb00 20 74 78 3b 0d 0a 20 20 20 20 20 20 20 20 73 71   tx;..        sq
9cb10 6c 69 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b  lite3_int64 day;
9cb20 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73  ..        if( !s
9cb30 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
9cb40 32 29 20 29 20 7a 32 2b 2b 3b 0d 0a 20 20 20 20  2) ) z2++;..    
9cb50 20 20 20 20 6d 65 6d 73 65 74 28 26 74 78 2c 20      memset(&tx, 
9cb60 30 2c 20 73 69 7a 65 6f 66 28 74 78 29 29 3b 0d  0, sizeof(tx));.
9cb70 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 61 72  .        if( par
9cb80 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26 74 78  seHhMmSs(z2, &tx
9cb90 29 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20  ) ) break;..    
9cba0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 74      computeJD(&t
9cbb0 78 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 78 2e  x);..        tx.
9cbc0 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b  iJD -= 43200000;
9cbd0 0d 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20  ..        day = 
9cbe0 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b  tx.iJD/86400000;
9cbf0 0d 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44  ..        tx.iJD
9cc00 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30   -= day*86400000
9cc10 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  ;..        if( z
9cc20 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a  [0]=='-' ) tx.iJ
9cc30 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0d 0a 20 20  D = -tx.iJD;..  
9cc40 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
9cc50 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 63 6c 65  p);..        cle
9cc60 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
9cc70 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  ..        p->iJD
9cc80 20 2b 3d 20 74 78 2e 69 4a 44 3b 0d 0a 20 20 20   += tx.iJD;..   
9cc90 20 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a 20 20       rc = 0;..  
9cca0 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
9ccb0 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7a 20 2b      }..      z +
9ccc0 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c  = n;..      whil
9ccd0 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
9cce0 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0d 0a 20 20  e(*z) ) z++;..  
9ccf0 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53      n = sqlite3S
9cd00 74 72 6c 65 6e 33 30 28 7a 29 3b 0d 0a 20 20 20  trlen30(z);..   
9cd10 20 20 20 69 66 28 20 6e 3e 31 30 20 7c 7c 20 6e     if( n>10 || n
9cd20 3c 33 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20  <3 ) break;..   
9cd30 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d 3d 27     if( z[n-1]=='
9cd40 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d 20 30  s' ){ z[n-1] = 0
9cd50 3b 20 6e 2d 2d 3b 20 7d 0d 0a 20 20 20 20 20 20  ; n--; }..      
9cd60 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0d 0a 20  computeJD(p);.. 
9cd70 20 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a 20 20       rc = 0;..  
9cd80 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72      rRounder = r
9cd90 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35  <0 ? -0.5 : +0.5
9cda0 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d  ;..      if( n==
9cdb0 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64  3 && strcmp(z,"d
9cdc0 61 79 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ay")==0 ){..    
9cdd0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
9cde0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
9cdf0 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f  86400000.0 + rRo
9ce00 75 6e 64 65 72 29 3b 0d 0a 20 20 20 20 20 20 7d  under);..      }
9ce10 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 26  else if( n==4 &&
9ce20 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22   strcmp(z,"hour"
9ce30 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )==0 ){..       
9ce40 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
9ce50 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
9ce60 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b  400000.0/24.0) +
9ce70 20 72 52 6f 75 6e 64 65 72 29 3b 0d 0a 20 20 20   rRounder);..   
9ce80 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d     }else if( n==
9ce90 36 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d  6 && strcmp(z,"m
9cea0 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0d 0a 20  inute")==0 ){.. 
9ceb0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
9cec0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
9ced0 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28  (r*(86400000.0/(
9cee0 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52  24.0*60.0)) + rR
9cef0 6f 75 6e 64 65 72 29 3b 0d 0a 20 20 20 20 20 20  ounder);..      
9cf00 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26  }else if( n==6 &
9cf10 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f  & strcmp(z,"seco
9cf20 6e 64 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  nd")==0 ){..    
9cf30 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
9cf40 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
9cf50 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e  (86400000.0/(24.
9cf60 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20  0*60.0*60.0)) + 
9cf70 72 52 6f 75 6e 64 65 72 29 3b 0d 0a 20 20 20 20  rRounder);..    
9cf80 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35    }else if( n==5
9cf90 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f   && strcmp(z,"mo
9cfa0 6e 74 68 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  nth")==0 ){..   
9cfb0 20 20 20 20 20 69 6e 74 20 78 2c 20 79 3b 0d 0a       int x, y;..
9cfc0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59          computeY
9cfd0 4d 44 5f 48 4d 53 28 70 29 3b 0d 0a 20 20 20 20  MD_HMS(p);..    
9cfe0 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74      p->M += (int
9cff0 29 72 3b 0d 0a 20 20 20 20 20 20 20 20 78 20 3d  )r;..        x =
9d000 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d 2d   p->M>0 ? (p->M-
9d010 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d 31 32  1)/12 : (p->M-12
9d020 29 2f 31 32 3b 0d 0a 20 20 20 20 20 20 20 20 70  )/12;..        p
9d030 2d 3e 59 20 2b 3d 20 78 3b 0d 0a 20 20 20 20 20  ->Y += x;..     
9d040 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b     p->M -= x*12;
9d050 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ..        p->val
9d060 69 64 4a 44 20 3d 20 30 3b 0d 0a 20 20 20 20 20  idJD = 0;..     
9d070 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
9d080 0d 0a 20 20 20 20 20 20 20 20 79 20 3d 20 28 69  ..        y = (i
9d090 6e 74 29 72 3b 0d 0a 20 20 20 20 20 20 20 20 69  nt)r;..        i
9d0a0 66 28 20 79 21 3d 72 20 29 7b 0d 0a 20 20 20 20  f( y!=r ){..    
9d0b0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
9d0c0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
9d0d0 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34  (r - y)*30.0*864
9d0e0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
9d0f0 65 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  er);..        }.
9d100 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
9d110 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28   n==4 && strcmp(
9d120 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0d  z,"year")==0 ){.
9d130 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20 3d  .        int y =
9d140 20 28 69 6e 74 29 72 3b 0d 0a 20 20 20 20 20 20   (int)r;..      
9d150 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
9d160 28 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d  (p);..        p-
9d170 3e 59 20 2b 3d 20 79 3b 0d 0a 20 20 20 20 20 20  >Y += y;..      
9d180 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
9d190 3b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  ;..        compu
9d1a0 74 65 4a 44 28 70 29 3b 0d 0a 20 20 20 20 20 20  teJD(p);..      
9d1b0 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0d 0a 20    if( y!=r ){.. 
9d1c0 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20           p->iJD 
9d1d0 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  += (sqlite3_int6
9d1e0 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35 2e 30  4)((r - y)*365.0
9d1f0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52  *86400000.0 + rR
9d200 6f 75 6e 64 65 72 29 3b 0d 0a 20 20 20 20 20 20  ounder);..      
9d210 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65    }..      }else
9d220 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
9d230 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  1;..      }..   
9d240 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
9d250 54 5a 28 70 29 3b 0d 0a 20 20 20 20 20 20 62 72  TZ(p);..      br
9d260 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
9d270 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20   default: {..   
9d280 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
9d290 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
9d2a0 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
9d2b0 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20 66 75   Process time fu
9d2c0 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  nction arguments
9d2d0 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 61 20  .  argv[0] is a 
9d2e0 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e  date-time stamp.
9d2f0 0d 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e 64  ..** argv[1] and
9d300 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 6d   following are m
9d310 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73 65  odifiers.  Parse
9d320 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77 72   them all and wr
9d330 69 74 65 0d 0a 2a 2a 20 74 68 65 20 72 65 73 75  ite..** the resu
9d340 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20  lting time into 
9d350 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
9d360 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72  ucture p.  Retur
9d370 6e 20 30 0d 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  n 0..** on succe
9d380 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72  ss and 1 if ther
9d390 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73  e are any errors
9d3a0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
9d3b0 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61  re are zero para
9d3c0 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 6e 20  meters (if even 
9d3d0 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66  argv[0] is undef
9d3e0 69 6e 65 64 29 0d 0a 2a 2a 20 74 68 65 6e 20 61  ined)..** then a
9d3f0 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 20  ssume a default 
9d400 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 66  value of "now" f
9d410 6f 72 20 61 72 67 76 5b 30 5d 2e 0d 0a 2a 2f 0d  or argv[0]...*/.
9d420 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61  .static int isDa
9d430 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63  te(..  sqlite3_c
9d440 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
9d450 20 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0d   ..  int argc, .
9d460 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9d470 20 2a 2a 61 72 67 76 2c 20 0d 0a 20 20 44 61 74   **argv, ..  Dat
9d480 65 54 69 6d 65 20 2a 70 0d 0a 29 7b 0d 0a 20 20  eTime *p..){..  
9d490 69 6e 74 20 69 3b 0d 0a 20 20 63 6f 6e 73 74 20  int i;..  const 
9d4a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
9d4b0 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
9d4c0 61 72 20 64 61 74 65 31 43 5b 31 31 5d 3b 0d 0a  ar date1C[11];..
9d4d0 20 20 69 6e 74 20 65 54 79 70 65 3b 0d 0a 20 20    int eType;..  
9d4e0 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
9d4f0 65 6f 66 28 2a 70 29 29 3b 0d 0a 20 20 69 66 28  eof(*p));..  if(
9d500 20 61 72 67 63 3d 3d 30 20 29 7b 0d 0a 20 20 20   argc==0 ){..   
9d510 20 72 65 74 75 72 6e 20 73 65 74 44 61 74 65 54   return setDateT
9d520 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
9d530 74 65 78 74 2c 20 70 29 3b 0d 0a 20 20 7d 0d 0a  text, p);..  }..
9d540 20 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73    if( (eType = s
9d550 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
9d560 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c  e(argv[0]))==SQL
9d570 49 54 45 5f 46 4c 4f 41 54 0d 0a 20 20 20 20 20  ITE_FLOAT..     
9d580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
9d590 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49   eType==SQLITE_I
9d5a0 4e 54 45 47 45 52 20 29 7b 0d 0a 20 20 20 20 70  NTEGER ){..    p
9d5b0 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33  ->iJD = (sqlite3
9d5c0 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
9d5d0 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
9d5e0 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30  v[0])*86400000.0
9d5f0 20 2b 20 30 2e 35 29 3b 0d 0a 20 20 20 20 70 2d   + 0.5);..    p-
9d600 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0d 0a 20  >validJD = 1;.. 
9d610 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 20 3d   }else{..    z =
9d620 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
9d630 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 09  ext(argv[0]);...
9d640 69 66 28 7a 20 26 26 20 38 20 3d 3d 20 73 74 72  if(z && 8 == str
9d650 6c 65 6e 28 28 63 6f 6e 73 74 20 63 68 61 72 2a  len((const char*
9d660 29 7a 29 29 0d 0a 09 7b 0d 0a 09 09 70 2d 3e 64  )z))...{....p->d
9d670 61 74 65 31 43 20 3d 20 31 3b 0d 0a 09 09 2a 28  ate1C = 1;....*(
9d680 44 57 4f 52 44 2a 29 64 61 74 65 31 43 20 3d 20  DWORD*)date1C = 
9d690 2a 28 44 57 4f 52 44 2a 29 7a 3b 0d 0a 09 09 64  *(DWORD*)z;....d
9d6a0 61 74 65 31 43 5b 34 5d 20 3d 20 27 2d 27 3b 0d  ate1C[4] = '-';.
9d6b0 0a 09 09 2a 28 57 4f 52 44 2a 29 28 64 61 74 65  ...*(WORD*)(date
9d6c0 31 43 20 2b 20 35 29 20 3d 20 2a 28 57 4f 52 44  1C + 5) = *(WORD
9d6d0 2a 29 28 7a 20 2b 20 34 29 3b 0d 0a 09 09 64 61  *)(z + 4);....da
9d6e0 74 65 31 43 5b 37 5d 20 3d 20 27 2d 27 3b 0d 0a  te1C[7] = '-';..
9d6f0 09 09 2a 28 57 4f 52 44 2a 29 28 64 61 74 65 31  ..*(WORD*)(date1
9d700 43 20 2b 20 38 29 20 3d 20 2a 28 57 4f 52 44 2a  C + 8) = *(WORD*
9d710 29 28 7a 20 2b 20 36 29 3b 0d 0a 09 09 64 61 74  )(z + 6);....dat
9d720 65 31 43 5b 31 30 5d 20 3d 20 30 3b 0d 0a 09 09  e1C[10] = 0;....
9d730 7a 20 3d 20 64 61 74 65 31 43 3b 0d 0a 09 7d 0d  z = date1C;...}.
9d740 0a 20 20 20 20 69 66 28 21 7a 20 7c 7c 20 70 61  .    if(!z || pa
9d750 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28 63 6f  rseDateOrTime(co
9d760 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c  ntext, (char*)z,
9d770 20 70 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65   p) ){..      re
9d780 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 1;..    }..
9d790 20 20 7d 0d 0a 20 20 66 6f 72 28 69 3d 31 3b 20    }..  for(i=1; 
9d7a0 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0d 0a 20  i<argc; i++){.. 
9d7b0 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
9d7c0 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69  alue_text(argv[i
9d7d0 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a 3d 3d  ]);..    if( z==
9d7e0 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69  0 || parseModifi
9d7f0 65 72 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  er(context, (cha
9d800 72 2a 29 7a 2c 20 70 29 20 29 20 72 65 74 75 72  r*)z, p) ) retur
9d810 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  n 1;..  }..  ret
9d820 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  urn 0;..}....../
9d830 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
9d840 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70  ing routines imp
9d850 6c 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f  lement the vario
9d860 75 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  us date and time
9d870 20 66 75 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 6f   functions..** o
9d880 66 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 0d  f SQLite...*/...
9d890 0a 2f 2a 0d 0a 2a 2a 20 20 20 20 6a 75 6c 69 61  ./*..**    julia
9d8a0 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e 47  nday( TIMESTRING
9d8b0 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29  , MOD, MOD, ...)
9d8c0 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ..**..** Return 
9d8d0 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e  the julian day n
9d8e0 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74  umber of the dat
9d8f0 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  e specified in t
9d900 68 65 20 61 72 67 75 6d 65 6e 74 73 0d 0a 2a 2f  he arguments..*/
9d910 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75  ..static void ju
9d920 6c 69 61 6e 64 61 79 46 75 6e 63 28 0d 0a 20 20  liandayFunc(..  
9d930 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
9d940 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74  *context,..  int
9d950 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65   argc,..  sqlite
9d960 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a  3_value **argv..
9d970 29 7b 0d 0a 20 20 44 61 74 65 54 69 6d 65 20 78  ){..  DateTime x
9d980 3b 0d 0a 20 20 69 66 28 20 69 73 44 61 74 65 28  ;..  if( isDate(
9d990 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
9d9a0 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0d 0a  rgv, &x)==0 ){..
9d9b0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78      computeJD(&x
9d9c0 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
9d9d0 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
9d9e0 6e 74 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34  ntext, x.iJD/864
9d9f0 30 30 30 30 30 2e 30 29 3b 0d 0a 20 20 7d 0d 0a  00000.0);..  }..
9da00 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 20 20 20 64  }..../*..**    d
9da10 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52  atetime( TIMESTR
9da20 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
9da30 2e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75  ..)..**..** Retu
9da40 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48  rn YYYY-MM-DD HH
9da50 3a 4d 4d 3a 53 53 0d 0a 2a 2f 0d 0a 73 74 61 74  :MM:SS..*/..stat
9da60 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d 65  ic void datetime
9da70 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33  Func(..  sqlite3
9da80 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
9da90 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d  t,..  int argc,.
9daa0 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9dab0 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 44   **argv..){..  D
9dac0 61 74 65 54 69 6d 65 20 78 3b 0d 0a 20 20 69 66  ateTime x;..  if
9dad0 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74  ( isDate(context
9dae0 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78  , argc, argv, &x
9daf0 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63 68 61  )==0 ){..    cha
9db00 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0d 0a 20 20  r zBuf[100];..  
9db10 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
9db20 28 26 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  (&x);..    sqlit
9db30 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
9db40 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20  of(zBuf), zBuf, 
9db50 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 20  "%04d-%02d-%02d 
9db60 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c  %02d:%02d:%02d",
9db70 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
9db80 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e 4d 2c         x.Y, x.M,
9db90 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20   x.D, x.h, x.m, 
9dba0 28 69 6e 74 29 28 78 2e 73 29 29 3b 0d 0a 20 20  (int)(x.s));..  
9dbb0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
9dbc0 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
9dbd0 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
9dbe0 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20 7d  TRANSIENT);..  }
9dbf0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 20 20  ..}..../*..**   
9dc00 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e   time( TIMESTRIN
9dc10 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
9dc20 29 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  )..**..** Return
9dc30 20 48 48 3a 4d 4d 3a 53 53 0d 0a 2a 2f 0d 0a 73   HH:MM:SS..*/..s
9dc40 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 46  tatic void timeF
9dc50 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  unc(..  sqlite3_
9dc60 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
9dc70 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a  ,..  int argc,..
9dc80 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
9dc90 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 44 61  **argv..){..  Da
9dca0 74 65 54 69 6d 65 20 78 3b 0d 0a 20 20 69 66 28  teTime x;..  if(
9dcb0 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
9dcc0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
9dcd0 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63 68 61 72  ==0 ){..    char
9dce0 20 7a 42 75 66 5b 31 30 30 5d 3b 0d 0a 20 20 20   zBuf[100];..   
9dcf0 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b   computeHMS(&x);
9dd00 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
9dd10 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42  printf(sizeof(zB
9dd20 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64  uf), zBuf, "%02d
9dd30 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68  :%02d:%02d", x.h
9dd40 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29  , x.m, (int)x.s)
9dd50 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ;..    sqlite3_r
9dd60 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
9dd70 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51  xt, zBuf, -1, SQ
9dd80 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
9dd90 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
9dda0 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 4d 45  **    date( TIME
9ddb0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
9ddc0 2c 20 2e 2e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 52  , ...)..**..** R
9ddd0 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
9dde0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
9ddf0 64 20 64 61 74 65 46 75 6e 63 28 0d 0a 20 20 73  d dateFunc(..  s
9de00 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
9de10 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20  context,..  int 
9de20 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33  argc,..  sqlite3
9de30 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29  _value **argv..)
9de40 7b 0d 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b  {..  DateTime x;
9de50 0d 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ..  if( isDate(c
9de60 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
9de70 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0d 0a 20  gv, &x)==0 ){.. 
9de80 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
9de90 5d 3b 0d 0a 20 20 20 20 63 6f 6d 70 75 74 65 59  ];..    computeY
9dea0 4d 44 28 26 78 29 3b 0d 0a 20 20 20 20 73 71 6c  MD(&x);..    sql
9deb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
9dec0 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66  zeof(zBuf), zBuf
9ded0 2c 20 30 20 3d 3d 20 78 2e 64 61 74 65 31 43 20  , 0 == x.date1C 
9dee0 3f 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32  ? "%04d-%02d-%02
9def0 64 22 20 3a 20 22 25 30 34 64 25 30 32 64 25 30  d" : "%04d%02d%0
9df00 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78  2d", x.Y, x.M, x
9df10 2e 44 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  .D);..    sqlite
9df20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
9df30 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
9df40 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
9df50 54 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  T);..  }..}..../
9df60 2a 0d 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d  *..**    strftim
9df70 65 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53  e( FORMAT, TIMES
9df80 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c  TRING, MOD, MOD,
9df90 20 2e 2e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65   ...)..**..** Re
9dfa0 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20 64 65  turn a string de
9dfb0 73 63 72 69 62 65 64 20 62 79 20 46 4f 52 4d 41  scribed by FORMA
9dfc0 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20  T.  Conversions 
9dfd0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d  as follows:..**.
9dfe0 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f 66  .**   %d  day of
9dff0 20 6d 6f 6e 74 68 0d 0a 2a 2a 20 20 20 25 66 20   month..**   %f 
9e000 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73   ** fractional s
9e010 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0d 0a  econds  SS.SSS..
9e020 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30  **   %H  hour 00
9e030 2d 32 34 0d 0a 2a 2a 20 20 20 25 6a 20 20 64 61  -24..**   %j  da
9e040 79 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36  y of year 000-36
9e050 36 0d 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a  6..**   %J  ** J
9e060 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
9e070 0d 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68  ..**   %m  month
9e080 20 30 31 2d 31 32 0d 0a 2a 2a 20 20 20 25 4d 20   01-12..**   %M 
9e090 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0d 0a 2a   minute 00-59..*
9e0a0 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20  *   %s  seconds 
9e0b0 73 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31  since 1970-01-01
9e0c0 0d 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e  ..**   %S  secon
9e0d0 64 73 20 30 30 2d 35 39 0d 0a 2a 2a 20 20 20 25  ds 00-59..**   %
9e0e0 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 30  w  day of week 0
9e0f0 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0d 0a 2a  -6  sunday==0..*
9e100 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20  *   %W  week of 
9e110 79 65 61 72 20 30 30 2d 35 33 0d 0a 2a 2a 20 20  year 00-53..**  
9e120 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39   %Y  year 0000-9
9e130 39 39 39 0d 0a 2a 2a 20 20 20 25 25 20 20 25 0d  999..**   %%  %.
9e140 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
9e150 20 73 74 72 66 74 69 6d 65 46 75 6e 63 28 0d 0a   strftimeFunc(..
9e160 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
9e170 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69  t *context,..  i
9e180 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69  nt argc,..  sqli
9e190 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
9e1a0 0d 0a 29 7b 0d 0a 20 20 44 61 74 65 54 69 6d 65  ..){..  DateTime
9e1b0 20 78 3b 0d 0a 20 20 75 36 34 20 6e 3b 0d 0a 20   x;..  u64 n;.. 
9e1c0 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0d 0a 20 20   size_t i,j;..  
9e1d0 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 73 71 6c 69  char *z;..  sqli
9e1e0 74 65 33 20 2a 64 62 3b 0d 0a 20 20 63 6f 6e 73  te3 *db;..  cons
9e1f0 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28  t char *zFmt = (
9e200 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
9e210 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
9e220 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 63 68 61 72  rgv[0]);..  char
9e230 20 7a 42 75 66 5b 31 30 30 5d 3b 0d 0a 20 20 69   zBuf[100];..  i
9e240 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73  f( zFmt==0 || is
9e250 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
9e260 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78  gc-1, argv+1, &x
9e270 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 64  ) ) return;..  d
9e280 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
9e290 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
9e2a0 6e 74 65 78 74 29 3b 0d 0a 20 20 66 6f 72 28 69  ntext);..  for(i
9e2b0 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d  =0, n=1; zFmt[i]
9e2c0 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b 0d 0a 20 20  ; i++, n++){..  
9e2d0 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27    if( zFmt[i]=='
9e2e0 25 27 20 29 7b 0d 0a 20 20 20 20 20 20 73 77 69  %' ){..      swi
9e2f0 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 5d 20 29  tch( zFmt[i+1] )
9e300 7b 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  {..        case 
9e310 27 64 27 3a 0d 0a 20 20 20 20 20 20 20 20 63 61  'd':..        ca
9e320 73 65 20 27 48 27 3a 0d 0a 20 20 20 20 20 20 20  se 'H':..       
9e330 20 63 61 73 65 20 27 6d 27 3a 0d 0a 20 20 20 20   case 'm':..    
9e340 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0d 0a 20      case 'M':.. 
9e350 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a         case 'S':
9e360 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ..        case '
9e370 57 27 3a 0d 0a 20 20 20 20 20 20 20 20 20 20 6e  W':..          n
9e380 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f  ++;..          /
9e390 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0d 0a  * fall thru */..
9e3a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27          case 'w'
9e3b0 3a 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  :..        case 
9e3c0 27 25 27 3a 0d 0a 09 09 63 61 73 65 20 27 71 27  '%':....case 'q'
9e3d0 3a 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  :..          bre
9e3e0 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 73  ak;..        cas
9e3f0 65 20 27 66 27 3a 0d 0a 20 20 20 20 20 20 20 20  e 'f':..        
9e400 20 20 6e 20 2b 3d 20 38 3b 0d 0a 20 20 20 20 20    n += 8;..     
9e410 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
9e420 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0d 0a       case 'j':..
9e430 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33            n += 3
9e440 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ;..          bre
9e450 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 73  ak;..        cas
9e460 65 20 27 59 27 3a 0d 0a 20 20 20 20 20 20 20 20  e 'Y':..        
9e470 20 20 6e 20 2b 3d 20 38 3b 0d 0a 20 20 20 20 20    n += 8;..     
9e480 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
9e490 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0d 0a       case 's':..
9e4a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
9e4b0 3a 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  :..          n +
9e4c0 3d 20 35 30 3b 0d 0a 20 20 20 20 20 20 20 20 20  = 50;..         
9e4d0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
9e4e0 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20 20   default:..     
9e4f0 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a       return;  /*
9e500 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e 20   ERROR.  return 
9e510 61 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 20 20 20  a NULL */..     
9e520 20 7d 0d 0a 20 20 20 20 20 20 69 2b 2b 3b 0d 0a   }..      i++;..
9e530 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 74 65      }..  }..  te
9e540 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f  stcase( n==sizeo
9e550 66 28 7a 42 75 66 29 2d 31 20 29 3b 0d 0a 20 20  f(zBuf)-1 );..  
9e560 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a  testcase( n==siz
9e570 65 6f 66 28 7a 42 75 66 29 20 29 3b 0d 0a 20 20  eof(zBuf) );..  
9e580 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36  testcase( n==(u6
9e590 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  4)db->aLimit[SQL
9e5a0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
9e5b0 5d 2b 31 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  ]+1 );..  testca
9e5c0 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e  se( n==(u64)db->
9e5d0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
9e5e0 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0d 0a  MIT_LENGTH] );..
9e5f0 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f 66 28 7a    if( n<sizeof(z
9e600 42 75 66 29 20 29 7b 0d 0a 20 20 20 20 7a 20 3d  Buf) ){..    z =
9e610 20 7a 42 75 66 3b 0d 0a 20 20 7d 65 6c 73 65 20   zBuf;..  }else 
9e620 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e 61  if( n>(u64)db->a
9e630 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
9e640 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0d 0a 20  IT_LENGTH] ){.. 
9e650 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
9e660 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
9e670 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 72 65  ontext);..    re
9e680 74 75 72 6e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  turn;..  }else{.
9e690 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
9e6a0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
9e6b0 28 69 6e 74 29 6e 29 3b 0d 0a 20 20 20 20 69 66  (int)n);..    if
9e6c0 28 20 7a 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ( z==0 ){..     
9e6d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
9e6e0 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
9e6f0 65 78 74 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  ext);..      ret
9e700 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  urn;..    }..  }
9e710 0d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78  ..  computeJD(&x
9e720 29 3b 0d 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44  );..  computeYMD
9e730 5f 48 4d 53 28 26 78 29 3b 0d 0a 20 20 66 6f 72  _HMS(&x);..  for
9e740 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b  (i=j=0; zFmt[i];
9e750 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20   i++){..    if( 
9e760 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0d  zFmt[i]!='%' ){.
9e770 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20  .      z[j++] = 
9e780 7a 46 6d 74 5b 69 5d 3b 0d 0a 20 20 20 20 7d 65  zFmt[i];..    }e
9e790 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69 2b 2b 3b  lse{..      i++;
9e7a0 0d 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  ..      switch( 
9e7b0 7a 46 6d 74 5b 69 5d 20 29 7b 0d 0a 20 20 20 20  zFmt[i] ){..    
9e7c0 20 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73      case 'd':  s
9e7d0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
9e7e0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
9e7f0 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.D); j+=2; brea
9e800 6b 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65  k;..        case
9e810 20 27 66 27 3a 20 7b 0d 0a 20 20 20 20 20 20 20   'f': {..       
9e820 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e     double s = x.
9e830 73 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  s;..          if
9e840 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 3d  ( s>59.999 ) s =
9e850 20 35 39 2e 39 39 39 3b 0d 0a 20 20 20 20 20 20   59.999;..      
9e860 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
9e870 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(7, &z[j],"%
9e880 30 36 2e 33 66 22 2c 20 73 29 3b 0d 0a 20 20 20  06.3f", s);..   
9e890 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69         j += sqli
9e8a0 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a  te3Strlen30(&z[j
9e8b0 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62  ]);..          b
9e8c0 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d  reak;..        }
9e8d0 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ..        case '
9e8e0 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  H':  sqlite3_snp
9e8f0 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
9e900 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32  %02d",x.h); j+=2
9e910 3b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ; break;..      
9e920 20 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46    case 'W': /* F
9e930 61 6c 6c 20 74 68 72 75 20 2a 2f 0d 0a 20 20 20  all thru */..   
9e940 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b       case 'j': {
9e950 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ..          int 
9e960 6e 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 20  nDay;           
9e970 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
9e980 61 79 73 20 73 69 6e 63 65 20 31 73 74 20 64 61  ays since 1st da
9e990 79 20 6f 66 20 79 65 61 72 20 2a 2f 0d 0a 20 20  y of year */..  
9e9a0 20 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65          DateTime
9e9b0 20 79 20 3d 20 78 3b 0d 0a 20 20 20 20 20 20 20   y = x;..       
9e9c0 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30     y.validJD = 0
9e9d0 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 79 2e 4d  ;..          y.M
9e9e0 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 1;..         
9e9f0 20 79 2e 44 20 3d 20 31 3b 0d 0a 20 20 20 20 20   y.D = 1;..     
9ea00 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26       computeJD(&
9ea10 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e  y);..          n
9ea20 44 61 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69  Day = (int)((x.i
9ea30 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30  JD-y.iJD+4320000
9ea40 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0d 0a 20  0)/86400000);.. 
9ea50 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d           if( zFm
9ea60 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0d 0a 20 20  t[i]=='W' ){..  
9ea70 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 64            int wd
9ea80 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c  ;   /* 0=Monday,
9ea90 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20   1=Tuesday, ... 
9eaa0 36 3d 53 75 6e 64 61 79 20 2a 2f 0d 0a 20 20 20  6=Sunday */..   
9eab0 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69           wd = (i
9eac0 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30  nt)(((x.iJD+4320
9ead0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25  0000)/86400000)%
9eae0 37 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  7);..           
9eaf0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
9eb00 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
9eb10 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29  ",(nDay+7-wd)/7)
9eb20 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a  ;..            j
9eb30 20 2b 3d 20 32 3b 0d 0a 20 20 20 20 20 20 20 20   += 2;..        
9eb40 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
9eb50 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
9eb60 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c  printf(4, &z[j],
9eb70 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0d  "%03d",nDay+1);.
9eb80 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
9eb90 3d 20 33 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  = 3;..          
9eba0 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  }..          bre
9ebb0 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ak;..        }..
9ebc0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
9ebd0 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  : {..          s
9ebe0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
9ebf0 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36 67  20, &z[j],"%.16g
9ec00 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30  ",x.iJD/86400000
9ec10 2e 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  .0);..          
9ec20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  j+=sqlite3Strlen
9ec30 33 30 28 26 7a 5b 6a 5d 29 3b 0d 0a 20 20 20 20  30(&z[j]);..    
9ec40 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
9ec50 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
9ec60 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69   case 'm':  sqli
9ec70 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
9ec80 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d  &z[j],"%02d",x.M
9ec90 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0d  ); j+=2; break;.
9eca0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d  .        case 'M
9ecb0 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ':  sqlite3_snpr
9ecc0 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(3, &z[j],"%
9ecd0 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b  02d",x.m); j+=2;
9ece0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
9ecf0 20 63 61 73 65 20 27 73 27 3a 20 7b 0d 0a 20 20   case 's': {..  
9ed00 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
9ed10 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a  snprintf(30,&z[j
9ed20 5d 2c 22 25 6c 6c 64 22 2c 0d 0a 20 20 20 20 20  ],"%lld",..     
9ed30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ed40 20 20 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a        (i64)(x.iJ
9ed50 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37  D/1000 - 2108667
9ed60 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 3b 0d  6*(i64)10000));.
9ed70 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20  .          j += 
9ed80 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
9ed90 26 7a 5b 6a 5d 29 3b 0d 0a 20 20 20 20 20 20 20  &z[j]);..       
9eda0 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
9edb0 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 63 61     }..        ca
9edc0 73 65 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33  se 'S':  sqlite3
9edd0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a  _snprintf(3,&z[j
9ede0 5d 2c 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e  ],"%02d",(int)x.
9edf0 73 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b  s); j+=2; break;
9ee00 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ..        case '
9ee10 77 27 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 20  w': {..         
9ee20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 68 61 72 29   z[j++] = (char)
9ee30 28 28 28 78 2e 69 4a 44 2b 31 32 39 36 30 30 30  (((x.iJD+1296000
9ee40 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20  00)/86400000) % 
9ee50 37 29 20 2b 20 27 30 27 3b 0d 0a 20 20 20 20 20  7) + '0';..     
9ee60 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
9ee70 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
9ee80 63 61 73 65 20 27 71 27 3a 20 7b 0d 0a 20 20 20  case 'q': {..   
9ee90 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
9eea0 28 63 68 61 72 29 28 28 78 2e 4d 20 2d 20 31 29  (char)((x.M - 1)
9eeb0 20 2f 20 33 29 20 2b 20 27 31 27 3b 0d 0a 20 20   / 3) + '1';..  
9eec0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
9eed0 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20          }....   
9eee0 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b       case 'Y': {
9eef0 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
9ef00 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26  te3_snprintf(5,&
9ef10 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29  z[j],"%04d",x.Y)
9ef20 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c  ; j+=sqlite3Strl
9ef30 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0d 0a 20 20  en30(&z[j]);..  
9ef40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
9ef50 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
9ef60 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b     default:   z[
9ef70 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61  j++] = '%'; brea
9ef80 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
9ef90 20 7d 0d 0a 20 20 7d 0d 0a 20 20 7a 5b 6a 5d 20   }..  }..  z[j] 
9efa0 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
9efb0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
9efc0 65 78 74 2c 20 7a 2c 20 2d 31 2c 0d 0a 20 20 20  ext, z, -1,..   
9efd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9efe0 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51 4c     z==zBuf ? SQL
9eff0 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a 20  ITE_TRANSIENT : 
9f000 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b  SQLITE_DYNAMIC);
9f010 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 63 75  ..}..../*..** cu
9f020 72 72 65 6e 74 5f 74 69 6d 65 28 29 0d 0a 2a 2a  rrent_time()..**
9f030 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
9f040 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
9f050 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d  ame value as tim
9f060 65 28 27 6e 6f 77 27 29 2e 0d 0a 2a 2f 0d 0a 73  e('now')...*/..s
9f070 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65  tatic void ctime
9f080 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33  Func(..  sqlite3
9f090 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
9f0a0 74 2c 0d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  t,..  int NotUse
9f0b0 64 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  d,..  sqlite3_va
9f0c0 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0d 0a  lue **NotUsed2..
9f0d0 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  ){..  UNUSED_PAR
9f0e0 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
9f0f0 20 4e 6f 74 55 73 65 64 32 29 3b 0d 0a 20 20 74   NotUsed2);..  t
9f100 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c  imeFunc(context,
9f110 20 30 2c 20 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   0, 0);..}..../*
9f120 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74  ..** current_dat
9f130 65 28 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  e()..**..** This
9f140 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
9f150 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
9f160 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e   as date('now').
9f170 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
9f180 64 20 63 64 61 74 65 46 75 6e 63 28 0d 0a 20 20  d cdateFunc(..  
9f190 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
9f1a0 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74  *context,..  int
9f1b0 20 4e 6f 74 55 73 65 64 2c 0d 0a 20 20 73 71 6c   NotUsed,..  sql
9f1c0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
9f1d0 55 73 65 64 32 0d 0a 29 7b 0d 0a 20 20 55 4e 55  Used2..){..  UNU
9f1e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
9f1f0 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
9f200 29 3b 0d 0a 20 20 64 61 74 65 46 75 6e 63 28 63  );..  dateFunc(c
9f210 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0d 0a  ontext, 0, 0);..
9f220 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 63 75 72 72  }..../*..** curr
9f230 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0d  ent_timestamp().
9f240 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  .**..** This fun
9f250 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
9f260 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20  e same value as 
9f270 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e  datetime('now').
9f280 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
9f290 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63  d ctimestampFunc
9f2a0 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  (..  sqlite3_con
9f2b0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a  text *context,..
9f2c0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a    int NotUsed,..
9f2d0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
9f2e0 2a 2a 4e 6f 74 55 73 65 64 32 0d 0a 29 7b 0d 0a  **NotUsed2..){..
9f2f0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
9f300 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
9f310 55 73 65 64 32 29 3b 0d 0a 20 20 64 61 74 65 74  Used2);..  datet
9f320 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c  imeFunc(context,
9f330 20 30 2c 20 30 29 3b 0d 0a 7d 0d 0a 23 65 6e 64   0, 0);..}..#end
9f340 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
9f350 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
9f360 49 4d 45 5f 46 55 4e 43 53 29 20 2a 2f 0d 0a 0d  IME_FUNCS) */...
9f370 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
9f380 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
9f390 43 53 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  CS../*..** If th
9f3a0 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d  e library is com
9f3b0 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74 68  piled to omit th
9f3c0 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61 74  e full-scale dat
9f3d0 65 20 61 6e 64 20 74 69 6d 65 0d 0a 2a 2a 20 68  e and time..** h
9f3e0 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20  andling (to get 
9f3f0 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79  a smaller binary
9f400 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ), the following
9f410 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e   minimal version
9f420 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63  ..** of the func
9f430 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69  tions current_ti
9f440 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61  me(), current_da
9f450 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74  te() and current
9f460 5f 74 69 6d 65 73 74 61 6d 70 28 29 0d 0a 2a 2a  _timestamp()..**
9f470 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e   are included in
9f480 73 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 74  stead. This is t
9f490 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e  o support column
9f4a0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68   declarations th
9f4b0 61 74 0d 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22  at..** include "
9f4c0 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f  DEFAULT CURRENT_
9f4d0 54 49 4d 45 22 20 65 74 63 2e 0d 0a 2a 2a 0d 0a  TIME" etc...**..
9f4e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
9f4f0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72   uses the C-libr
9f500 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69  ary functions ti
9f510 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0d 0a  me(), gmtime()..
9f520 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28  ** and strftime(
9f530 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74  ). The format st
9f540 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20  ring to pass to 
9f550 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75  strftime() is su
9f560 70 70 6c 69 65 64 0d 0a 2a 2a 20 61 73 20 74 68  pplied..** as th
9f570 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20  e user-data for 
9f580 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a  the function...*
9f590 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  /..static void c
9f5a0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0d  urrentTimeFunc(.
9f5b0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
9f5c0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20  xt *context,..  
9f5d0 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c  int argc,..  sql
9f5e0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
9f5f0 76 0d 0a 29 7b 0d 0a 20 20 74 69 6d 65 5f 74 20  v..){..  time_t 
9f600 74 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46 6f 72  t;..  char *zFor
9f610 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73 71  mat = (char *)sq
9f620 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
9f630 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 73 71 6c  context);..  sql
9f640 69 74 65 33 20 2a 64 62 3b 0d 0a 20 20 73 71 6c  ite3 *db;..  sql
9f650 69 74 65 33 5f 69 6e 74 36 34 20 69 54 3b 0d 0a  ite3_int64 iT;..
9f660 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d    struct tm *pTm
9f670 3b 0d 0a 20 20 73 74 72 75 63 74 20 74 6d 20 73  ;..  struct tm s
9f680 4e 6f 77 3b 0d 0a 20 20 63 68 61 72 20 7a 42 75  Now;..  char zBu
9f690 66 5b 32 30 5d 3b 0d 0a 0d 0a 20 20 55 4e 55 53  f[20];....  UNUS
9f6a0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
9f6b0 63 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  c);..  UNUSED_PA
9f6c0 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0d 0a  RAMETER(argv);..
9f6d0 0d 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  ..  db = sqlite3
9f6e0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
9f6f0 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  le(context);..  
9f700 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43 75 72  if( sqlite3OsCur
9f710 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 64 62  rentTimeInt64(db
9f720 2d 3e 70 56 66 73 2c 20 26 69 54 29 20 29 20 72  ->pVfs, &iT) ) r
9f730 65 74 75 72 6e 3b 0d 0a 20 20 74 20 3d 20 69 54  eturn;..  t = iT
9f740 2f 31 30 30 30 20 2d 20 31 30 30 30 30 2a 28 73  /1000 - 10000*(s
9f750 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 31 30  qlite3_int64)210
9f760 38 36 36 37 36 3b 0d 0a 23 69 66 64 65 66 20 48  86676;..#ifdef H
9f770 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0d 0a 20 20  AVE_GMTIME_R..  
9f780 70 54 6d 20 3d 20 67 6d 74 69 6d 65 5f 72 28 26  pTm = gmtime_r(&
9f790 74 2c 20 26 73 4e 6f 77 29 3b 0d 0a 23 65 6c 73  t, &sNow);..#els
9f7a0 65 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  e..  sqlite3_mut
9f7b0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
9f7c0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
9f7d0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
9f7e0 41 53 54 45 52 29 29 3b 0d 0a 20 20 70 54 6d 20  ASTER));..  pTm 
9f7f0 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0d 0a 20  = gmtime(&t);.. 
9f800 20 69 66 28 20 70 54 6d 20 29 20 6d 65 6d 63 70   if( pTm ) memcp
9f810 79 28 26 73 4e 6f 77 2c 20 70 54 6d 2c 20 73 69  y(&sNow, pTm, si
9f820 7a 65 6f 66 28 73 4e 6f 77 29 29 3b 0d 0a 20 20  zeof(sNow));..  
9f830 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9f840 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
9f850 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
9f860 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
9f870 29 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69  ));..#endif..  i
9f880 66 28 20 70 54 6d 20 29 7b 0d 0a 20 20 20 20 73  f( pTm ){..    s
9f890 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30  trftime(zBuf, 20
9f8a0 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77  , zFormat, &sNow
9f8b0 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
9f8c0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
9f8d0 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
9f8e0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
9f8f0 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69  ;..  }..}..#endi
9f900 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  f..../*..** This
9f910 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
9f920 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
9f930 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
9f940 73 20 61 73 20 53 51 4c 0d 0a 2a 2a 20 66 75 6e  s as SQL..** fun
9f950 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68  ctions.  This sh
9f960 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
9f970 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
9f980 20 66 69 6c 65 20 77 69 74 68 0d 0a 2a 2a 20 65   file with..** e
9f990 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e  xternal linkage.
9f9a0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
9f9b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
9f9c0 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d  3RegisterDateTim
9f9d0 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29  eFunctions(void)
9f9e0 7b 0d 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49  {..  static SQLI
9f9f0 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61  TE_WSD FuncDef a
9fa00 44 61 74 65 54 69 6d 65 46 75 6e 63 73 5b 5d 20  DateTimeFuncs[] 
9fa10 3d 20 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  = {..#ifndef SQL
9fa20 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
9fa30 45 5f 46 55 4e 43 53 0d 0a 20 20 20 20 46 55 4e  E_FUNCS..    FUN
9fa40 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 2c  CTION(julianday,
9fa50 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30          -1, 0, 0
9fa60 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 20  , juliandayFunc 
9fa70 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  ),..    FUNCTION
9fa80 28 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  (date,          
9fa90 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74     -1, 0, 0, dat
9faa0 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0d 0a 20  eFunc      ),.. 
9fab0 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65     FUNCTION(time
9fac0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31  ,             -1
9fad0 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63  , 0, 0, timeFunc
9fae0 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 46 55        ),..    FU
9faf0 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c  NCTION(datetime,
9fb00 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
9fb10 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20  0, datetimeFunc 
9fb20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f   ),..    FUNCTIO
9fb30 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 20  N(strftime,     
9fb40 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74      -1, 0, 0, st
9fb50 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0d 0a  rftimeFunc  ),..
9fb60 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
9fb70 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20  rent_time,      
9fb80 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75  0, 0, 0, ctimeFu
9fb90 6e 63 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 46  nc     ),..    F
9fba0 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f  UNCTION(current_
9fbb0 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c  timestamp, 0, 0,
9fbc0 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75   0, ctimestampFu
9fbd0 6e 63 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49  nc),..    FUNCTI
9fbe0 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c  ON(current_date,
9fbf0 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63        0, 0, 0, c
9fc00 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0d  dateFunc     ),.
9fc10 0a 23 65 6c 73 65 0d 0a 20 20 20 20 53 54 52 5f  .#else..    STR_
9fc20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
9fc30 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22  _time,      0, "
9fc40 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20  %H:%M:%S",      
9fc50 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69      0, currentTi
9fc60 6d 65 46 75 6e 63 29 2c 0d 0a 20 20 20 20 53 54  meFunc),..    ST
9fc70 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
9fc80 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c  nt_date,      0,
9fc90 20 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20   "%Y-%m-%d",    
9fca0 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74        0, current
9fcb0 54 69 6d 65 46 75 6e 63 29 2c 0d 0a 20 20 20 20  TimeFunc),..    
9fcc0 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72  STR_FUNCTION(cur
9fcd0 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20  rent_timestamp, 
9fce0 30 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a  0, "%Y-%m-%d %H:
9fcf0 25 4d 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65  %M:%S", 0, curre
9fd00 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0d 0a 23 65  ntTimeFunc),..#e
9fd10 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a 20 20 69 6e  ndif..  };..  in
9fd20 74 20 69 3b 0d 0a 20 20 46 75 6e 63 44 65 66 48  t i;..  FuncDefH
9fd30 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c  ash *pHash = &GL
9fd40 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68  OBAL(FuncDefHash
9fd50 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  , sqlite3GlobalF
9fd60 75 6e 63 74 69 6f 6e 73 29 3b 0d 0a 20 20 46 75  unctions);..  Fu
9fd70 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28  ncDef *aFunc = (
9fd80 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c  FuncDef*)&GLOBAL
9fd90 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 54  (FuncDef, aDateT
9fda0 69 6d 65 46 75 6e 63 73 29 3b 0d 0a 0d 0a 20 20  imeFuncs);....  
9fdb0 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
9fdc0 53 69 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75  Size(aDateTimeFu
9fdd0 6e 63 73 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  ncs); i++){..   
9fde0 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49   sqlite3FuncDefI
9fdf0 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61 46  nsert(pHash, &aF
9fe00 75 6e 63 5b 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 7d  unc[i]);..  }..}
9fe10 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
9fe20 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e  *** End of date.
9fe30 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
9fe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe60 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
9fe70 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
9fe80 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os.c ***********
9fe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9feb0 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
9fec0 35 20 4e 6f 76 65 6d 62 65 72 20 32 39 0d 0a 2a  5 November 29..*
9fed0 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
9fee0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
9fef0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
9ff00 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
9ff10 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
9ff20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
9ff30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
9ff40 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
9ff50 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
9ff60 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
9ff70 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
9ff80 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
9ff90 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
9ffa0 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
9ffb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
9ffc0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
9ffd0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
9ffe0 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
9fff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a  **********..**..
a0040 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
a0050 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61  tains OS interfa
a0060 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ce code that is 
a0070 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0d 0a 2a  common to all..*
a0080 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e  * architectures.
a0090 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 5f 53  ..*/..#define _S
a00a0 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0d 0a 23  QLITE_OS_C_ 1..#
a00b0 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53  undef _SQLITE_OS
a00c0 5f 43 5f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  _C_..../*..** Th
a00d0 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65  e default SQLite
a00e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70   sqlite3_vfs imp
a00f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20  lementations do 
a0100 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0d 0a 2a 2a  not allocate..**
a0110 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c   memory (actuall
a0120 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c  y, os_unix.c all
a0130 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 61  ocates a small a
a0140 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0d  mount of memory.
a0150 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  .** from within 
a0160 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73  OsOpen()), but s
a0170 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20  ome third-party 
a0180 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
a0190 6d 61 79 2e 0d 0a 2a 2a 20 53 6f 20 77 65 20 74  may...** So we t
a01a0 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20  est the effects 
a01b0 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  of a malloc() fa
a01c0 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71  iling and the sq
a01d0 6c 69 74 65 33 4f 73 58 58 58 28 29 0d 0a 2a 2a  lite3OsXXX()..**
a01e0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
a01f0 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ing SQLITE_IOERR
a0200 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65  _NOMEM using the
a0210 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
a0220 53 54 20 6d 61 63 72 6f 2e 0d 0a 2a 2a 0d 0a 2a  ST macro...**..*
a0230 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
a0240 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e  functions are in
a0250 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d  strumented for m
a0260 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
a0270 0d 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0d 0a 2a  ..** testing:..*
a0280 2a 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  *..**     sqlite
a0290 33 4f 73 52 65 61 64 28 29 0d 0a 2a 2a 20 20 20  3OsRead()..**   
a02a0 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65    sqlite3OsWrite
a02b0 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  ()..**     sqlit
a02c0 65 33 4f 73 53 79 6e 63 28 29 0d 0a 2a 2a 20 20  e3OsSync()..**  
a02d0 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65     sqlite3OsFile
a02e0 53 69 7a 65 28 29 0d 0a 2a 2a 20 20 20 20 20 73  Size()..**     s
a02f0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0d 0a  qlite3OsLock()..
a0300 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
a0310 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
a0320 6b 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69  k()..**     sqli
a0330 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
a0340 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  ()..**     sqlit
a0350 65 33 4f 73 53 68 6d 4d 61 70 28 29 0d 0a 2a 2a  e3OsShmMap()..**
a0360 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70       sqlite3OsOp
a0370 65 6e 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c  en()..**     sql
a0380 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 0d 0a  ite3OsDelete()..
a0390 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
a03a0 41 63 63 65 73 73 28 29 0d 0a 2a 2a 20 20 20 20  Access()..**    
a03b0 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61   sqlite3OsFullPa
a03c0 74 68 6e 61 6d 65 28 29 0d 0a 2a 2a 0d 0a 2a 2f  thname()..**..*/
a03d0 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
a03e0 4c 49 54 45 5f 54 45 53 54 29 0d 0a 53 51 4c 49  LITE_TEST)..SQLI
a03f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
a0400 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66 73 5f  e3_memdebug_vfs_
a0410 6f 6f 6d 5f 74 65 73 74 20 3d 20 31 3b 0d 0a 20  oom_test = 1;.. 
a0420 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d   #define DO_OS_M
a0430 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 20 20  ALLOC_TEST(x)   
a0440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0460 20 20 20 20 5c 0d 0a 20 20 69 66 20 28 73 71 6c      \..  if (sql
a0470 69 74 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66  ite3_memdebug_vf
a0480 73 5f 6f 6f 6d 5f 74 65 73 74 20 26 26 20 28 21  s_oom_test && (!
a0490 78 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d  x || !sqlite3IsM
a04a0 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 29 29 20 7b  emJournal(x))) {
a04b0 20 20 5c 0d 0a 20 20 20 20 76 6f 69 64 20 2a 70    \..    void *p
a04c0 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  TstAlloc = sqlit
a04d0 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20  e3Malloc(10);   
a04e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a04f0 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20            \..   
a0500 20 69 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29   if (!pTstAlloc)
a0510 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
a0520 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20  OERR_NOMEM;     
a0530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0540 20 20 5c 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    \..    sqlite3
a0550 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63 29  _free(pTstAlloc)
a0560 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a0570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0580 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 7d            \..  }
a0590 0d 0a 23 65 6c 73 65 0d 0a 20 20 23 64 65 66 69  ..#else..  #defi
a05a0 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  ne DO_OS_MALLOC_
a05b0 54 45 53 54 28 78 29 0d 0a 23 65 6e 64 69 66 0d  TEST(x)..#endif.
a05c0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
a05d0 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
a05e0 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65   are convenience
a05f0 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64   wrappers around
a0600 20 6d 65 74 68 6f 64 73 0d 0a 2a 2a 20 6f 66 20   methods..** of 
a0610 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  the sqlite3_file
a0620 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69   object.  This i
a0630 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79  s mostly just sy
a0640 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 41  ntactic sugar. A
a0650 6c 6c 0d 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77  ll..** of this w
a0660 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65  ould be complete
a0670 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20  ly automatic if 
a0680 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65  SQLite were code
a0690 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 43 2b 2b 20  d using..** C++ 
a06a0 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e  instead of plain
a06b0 20 6f 6c 64 20 43 2e 0d 0a 2a 2f 0d 0a 53 51 4c   old C...*/..SQL
a06c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a06d0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73  sqlite3OsClose(s
a06e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64  qlite3_file *pId
a06f0 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
a0700 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28  QLITE_OK;..  if(
a0710 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29   pId->pMethods )
a0720 7b 0d 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d  {..    rc = pId-
a0730 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73  >pMethods->xClos
a0740 65 28 70 49 64 29 3b 0d 0a 20 20 20 20 70 49 64  e(pId);..    pId
a0750 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0d  ->pMethods = 0;.
a0760 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
a0770 63 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52  c;..}..SQLITE_PR
a0780 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a0790 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f  3OsRead(sqlite3_
a07a0 66 69 6c 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a  file *id, void *
a07b0 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69  pBuf, int amt, i
a07c0 36 34 20 6f 66 66 73 65 74 29 7b 0d 0a 20 20 44  64 offset){..  D
a07d0 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
a07e0 28 69 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  (id);..  return 
a07f0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52  id->pMethods->xR
a0800 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ead(id, pBuf, am
a0810 74 2c 20 6f 66 66 73 65 74 29 3b 0d 0a 7d 0d 0a  t, offset);..}..
a0820 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a0830 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
a0840 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
a0850 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  id, const void *
a0860 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69  pBuf, int amt, i
a0870 36 34 20 6f 66 66 73 65 74 29 7b 0d 0a 20 20 44  64 offset){..  D
a0880 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
a0890 28 69 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  (id);..  return 
a08a0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57  id->pMethods->xW
a08b0 72 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61  rite(id, pBuf, a
a08c0 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0d 0a 7d 0d  mt, offset);..}.
a08d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a08e0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75  int sqlite3OsTru
a08f0 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  ncate(sqlite3_fi
a0900 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a 65  le *id, i64 size
a0910 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  ){..  return id-
a0920 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e  >pMethods->xTrun
a0930 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0d  cate(id, size);.
a0940 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
a0950 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
a0960 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
a0970 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73  e *id, int flags
a0980 29 7b 0d 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  ){..  DO_OS_MALL
a0990 4f 43 5f 54 45 53 54 28 69 64 29 3b 0d 0a 20 20  OC_TEST(id);..  
a09a0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
a09b0 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66  ods->xSync(id, f
a09c0 6c 61 67 73 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54  lags);..}..SQLIT
a09d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a09e0 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
a09f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
a0a00 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0d 0a  , i64 *pSize){..
a0a10 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
a0a20 45 53 54 28 69 64 29 3b 0d 0a 20 20 72 65 74 75  EST(id);..  retu
a0a30 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
a0a40 3e 78 46 69 6c 65 53 69 7a 65 28 69 64 2c 20 70  >xFileSize(id, p
a0a50 53 69 7a 65 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54  Size);..}..SQLIT
a0a60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a0a70 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69  lite3OsLock(sqli
a0a80 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
a0a90 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0d 0a 20 20  t lockType){..  
a0aa0 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
a0ab0 54 28 69 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e  T(id);..  return
a0ac0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
a0ad0 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70  Lock(id, lockTyp
a0ae0 65 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50  e);..}..SQLITE_P
a0af0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a0b00 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  e3OsUnlock(sqlit
a0b10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
a0b20 20 6c 6f 63 6b 54 79 70 65 29 7b 0d 0a 20 20 72   lockType){..  r
a0b30 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
a0b40 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20  ds->xUnlock(id, 
a0b50 6c 6f 63 6b 54 79 70 65 29 3b 0d 0a 7d 0d 0a 53  lockType);..}..S
a0b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a0b70 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b  t sqlite3OsCheck
a0b80 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
a0b90 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
a0ba0 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0d 0a 20  nt *pResOut){.. 
a0bb0 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
a0bc0 53 54 28 69 64 29 3b 0d 0a 20 20 72 65 74 75 72  ST(id);..  retur
a0bd0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
a0be0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
a0bf0 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29 3b  ck(id, pResOut);
a0c00 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 73  ..}..../*..** Us
a0c10 65 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  e sqlite3OsFileC
a0c20 6f 6e 74 72 6f 6c 28 29 20 77 68 65 6e 20 77 65  ontrol() when we
a0c30 20 61 72 65 20 64 6f 69 6e 67 20 73 6f 6d 65 74   are doing somet
a0c40 68 69 6e 67 20 74 68 61 74 20 6d 69 67 68 74 20  hing that might 
a0c50 66 61 69 6c 0d 0a 2a 2a 20 61 6e 64 20 77 65 20  fail..** and we 
a0c60 6e 65 65 64 20 74 6f 20 6b 6e 6f 77 20 61 62 6f  need to know abo
a0c70 75 74 20 74 68 65 20 66 61 69 6c 75 72 65 73 2e  ut the failures.
a0c80 20 20 55 73 65 20 73 71 6c 69 74 65 33 4f 73 46    Use sqlite3OsF
a0c90 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 29  ileControlHint()
a0ca0 0d 0a 2a 2a 20 77 68 65 6e 20 73 69 6d 70 6c 79  ..** when simply
a0cb0 20 74 6f 73 73 69 6e 67 20 69 6e 66 6f 72 6d 61   tossing informa
a0cc0 74 69 6f 6e 20 6f 76 65 72 20 74 68 65 20 77 61  tion over the wa
a0cd0 6c 6c 20 74 6f 20 74 68 65 20 56 46 53 20 61 6e  ll to the VFS an
a0ce0 64 20 77 65 20 64 6f 20 6e 6f 74 0d 0a 2a 2a 20  d we do not..** 
a0cf0 72 65 61 6c 6c 79 20 63 61 72 65 20 69 66 20 74  really care if t
a0d00 68 65 20 56 46 53 20 72 65 63 65 69 76 65 73 20  he VFS receives 
a0d10 61 6e 64 20 75 6e 64 65 72 73 74 61 6e 64 73 20  and understands 
a0d20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
a0d30 73 69 6e 63 65 20 69 74 0d 0a 2a 2a 20 69 73 20  since it..** is 
a0d40 6f 6e 6c 79 20 61 20 68 69 6e 74 20 61 6e 64 20  only a hint and 
a0d50 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 69 67  can be safely ig
a0d60 6e 6f 72 65 64 2e 20 20 54 68 65 20 73 71 6c 69  nored.  The sqli
a0d70 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
a0d80 48 69 6e 74 28 29 0d 0a 2a 2a 20 72 6f 75 74 69  Hint()..** routi
a0d90 6e 65 20 68 61 73 20 6e 6f 20 72 65 74 75 72 6e  ne has no return
a0da0 20 76 61 6c 75 65 20 73 69 6e 63 65 20 74 68 65   value since the
a0db0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 6f   return value wo
a0dc0 75 6c 64 20 62 65 20 6d 65 61 6e 69 6e 67 6c 65  uld be meaningle
a0dd0 73 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ss...*/..SQLITE_
a0de0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a0df0 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
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 70 2c 20 76 6f 69 64 20  d, int op, void 
a0e20 2a 70 41 72 67 29 7b 0d 0a 20 20 44 4f 5f 4f 53  *pArg){..  DO_OS
a0e30 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
a0e40 3b 0d 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ;..  return id->
a0e50 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43  pMethods->xFileC
a0e60 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70  ontrol(id, op, p
a0e70 41 72 67 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45  Arg);..}..SQLITE
a0e80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a0e90 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
a0ea0 6f 6c 48 69 6e 74 28 73 71 6c 69 74 65 33 5f 66  olHint(sqlite3_f
a0eb0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c  ile *id, int op,
a0ec0 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0d 0a 20   void *pArg){.. 
a0ed0 20 28 76 6f 69 64 29 69 64 2d 3e 70 4d 65 74 68   (void)id->pMeth
a0ee0 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 6f  ods->xFileContro
a0ef0 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b  l(id, op, pArg);
a0f00 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  ..}....SQLITE_PR
a0f10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a0f20 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71  3OsSectorSize(sq
a0f30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
a0f40 0d 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f  ..  int (*xSecto
a0f50 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66  rSize)(sqlite3_f
a0f60 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74  ile*) = id->pMet
a0f70 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a  hods->xSectorSiz
a0f80 65 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 78 53  e;..  return (xS
a0f90 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63  ectorSize ? xSec
a0fa0 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51  torSize(id) : SQ
a0fb0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
a0fc0 54 4f 52 5f 53 49 5a 45 29 3b 0d 0a 7d 0d 0a 53  TOR_SIZE);..}..S
a0fd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a0fe0 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  t sqlite3OsDevic
a0ff0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
a1000 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
a1010 64 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 69 64  d){..  return id
a1020 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76  ->pMethods->xDev
a1030 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
a1040 63 73 28 69 64 29 3b 0d 0a 7d 0d 0a 53 51 4c 49  cs(id);..}..SQLI
a1050 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a1060 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63 6b 28  qlite3OsShmLock(
a1070 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
a1080 2c 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 69 6e  , int offset, in
a1090 74 20 6e 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  t n, int flags){
a10a0 0d 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ..  return id->p
a10b0 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4c 6f 63  Methods->xShmLoc
a10c0 6b 28 69 64 2c 20 6f 66 66 73 65 74 2c 20 6e 2c  k(id, offset, n,
a10d0 20 66 6c 61 67 73 29 3b 0d 0a 7d 0d 0a 53 51 4c   flags);..}..SQL
a10e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a10f0 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 42 61 72   sqlite3OsShmBar
a1100 72 69 65 72 28 73 71 6c 69 74 65 33 5f 66 69 6c  rier(sqlite3_fil
a1110 65 20 2a 69 64 29 7b 0d 0a 20 20 69 64 2d 3e 70  e *id){..  id->p
a1120 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 42 61 72  Methods->xShmBar
a1130 72 69 65 72 28 69 64 29 3b 0d 0a 7d 0d 0a 53 51  rier(id);..}..SQ
a1140 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a1150 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 55 6e 6d   sqlite3OsShmUnm
a1160 61 70 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ap(sqlite3_file 
a1170 2a 69 64 2c 20 69 6e 74 20 64 65 6c 65 74 65 46  *id, int deleteF
a1180 6c 61 67 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20  lag){..  return 
a1190 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53  id->pMethods->xS
a11a0 68 6d 55 6e 6d 61 70 28 69 64 2c 20 64 65 6c 65  hmUnmap(id, dele
a11b0 74 65 46 6c 61 67 29 3b 0d 0a 7d 0d 0a 53 51 4c  teFlag);..}..SQL
a11c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a11d0 73 71 6c 69 74 65 33 4f 73 53 68 6d 4d 61 70 28  sqlite3OsShmMap(
a11e0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ..  sqlite3_file
a11f0 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
a1200 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
a1210 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a  file handle */..
a1220 20 20 69 6e 74 20 69 50 61 67 65 2c 0d 0a 20 20    int iPage,..  
a1230 69 6e 74 20 70 67 73 7a 2c 0d 0a 20 20 69 6e 74  int pgsz,..  int
a1240 20 62 45 78 74 65 6e 64 2c 20 20 20 20 20 20 20   bExtend,       
a1250 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1260 54 72 75 65 20 74 6f 20 65 78 74 65 6e 64 20 66  True to extend f
a1270 69 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  ile if necessary
a1280 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 76 6f 6c 61   */..  void vola
a1290 74 69 6c 65 20 2a 2a 70 70 20 20 20 20 20 20 20  tile **pp       
a12a0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
a12b0 6f 69 6e 74 65 72 20 74 6f 20 6d 61 70 70 69 6e  ointer to mappin
a12c0 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 44 4f 5f 4f  g */..){..  DO_O
a12d0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64  S_MALLOC_TEST(id
a12e0 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  );..  return id-
a12f0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4d  >pMethods->xShmM
a1300 61 70 28 69 64 2c 20 69 50 61 67 65 2c 20 70 67  ap(id, iPage, pg
a1310 73 7a 2c 20 62 45 78 74 65 6e 64 2c 20 70 70 29  sz, bExtend, pp)
a1320 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
a1330 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66  he next group of
a1340 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f   routines are co
a1350 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65  nvenience wrappe
a1360 72 73 20 61 72 6f 75 6e 64 20 74 68 65 0d 0a 2a  rs around the..*
a1370 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0d 0a  * VFS methods...
a1380 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
a1390 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
a13a0 4f 70 65 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33  Open(..  sqlite3
a13b0 5f 76 66 73 20 2a 70 56 66 73 2c 20 0d 0a 20 20  _vfs *pVfs, ..  
a13c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
a13d0 68 2c 20 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  h, ..  sqlite3_f
a13e0 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0d 0a 20 20  ile *pFile, ..  
a13f0 69 6e 74 20 66 6c 61 67 73 2c 20 0d 0a 20 20 69  int flags, ..  i
a1400 6e 74 20 2a 70 46 6c 61 67 73 4f 75 74 0d 0a 29  nt *pFlagsOut..)
a1410 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  {..  int rc;..  
a1420 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
a1430 54 28 30 29 3b 0d 0a 20 20 2f 2a 20 30 78 38 37  T(0);..  /* 0x87
a1440 66 37 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 66  f7f is a mask of
a1450 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c   SQLITE_OPEN_ fl
a1460 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61 6c  ags that are val
a1470 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 0d  id to be passed.
a1480 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20  .  ** down into 
a1490 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20  the VFS layer.  
a14a0 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e  Some SQLITE_OPEN
a14b0 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61  _ flags (for exa
a14c0 6d 70 6c 65 2c 0d 0a 20 20 2a 2a 20 53 51 4c 49  mple,..  ** SQLI
a14d0 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
a14e0 58 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e  X or SQLITE_OPEN
a14f0 5f 53 48 41 52 45 44 43 41 43 48 45 29 20 61 72  _SHAREDCACHE) ar
a1500 65 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65  e blocked before
a1510 0d 0a 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20  ..  ** reaching 
a1520 74 68 65 20 56 46 53 2e 20 2a 2f 0d 0a 20 20 72  the VFS. */..  r
a1530 63 20 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28  c = pVfs->xOpen(
a1540 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69  pVfs, zPath, pFi
a1550 6c 65 2c 20 66 6c 61 67 73 20 26 20 30 78 38 37  le, flags & 0x87
a1560 66 37 66 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b  f7f, pFlagsOut);
a1570 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ..  assert( rc==
a1580 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69  SQLITE_OK || pFi
a1590 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20  le->pMethods==0 
a15a0 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
a15b0 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..}..SQLITE_PRIV
a15c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
a15d0 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f  sDelete(sqlite3_
a15e0 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
a15f0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e   char *zPath, in
a1600 74 20 64 69 72 53 79 6e 63 29 7b 0d 0a 20 20 44  t dirSync){..  D
a1610 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
a1620 28 30 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  (0);..  assert( 
a1630 64 69 72 53 79 6e 63 3d 3d 30 20 7c 7c 20 64 69  dirSync==0 || di
a1640 72 53 79 6e 63 3d 3d 31 20 29 3b 0d 0a 20 20 72  rSync==1 );..  r
a1650 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c  eturn pVfs->xDel
a1660 65 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  ete(pVfs, zPath,
a1670 20 64 69 72 53 79 6e 63 29 3b 0d 0a 7d 0d 0a 53   dirSync);..}..S
a1680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a1690 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  t sqlite3OsAcces
a16a0 73 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  s(..  sqlite3_vf
a16b0 73 20 2a 70 56 66 73 2c 20 0d 0a 20 20 63 6f 6e  s *pVfs, ..  con
a16c0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
a16d0 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0d  ..  int flags, .
a16e0 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0d  .  int *pResOut.
a16f0 0a 29 7b 0d 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  .){..  DO_OS_MAL
a1700 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0d 0a 20 20  LOC_TEST(0);..  
a1710 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63  return pVfs->xAc
a1720 63 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68  cess(pVfs, zPath
a1730 2c 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74  , flags, pResOut
a1740 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52  );..}..SQLITE_PR
a1750 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a1760 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  3OsFullPathname(
a1770 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ..  sqlite3_vfs 
a1780 2a 70 56 66 73 2c 20 0d 0a 20 20 63 6f 6e 73 74  *pVfs, ..  const
a1790 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0d 0a   char *zPath, ..
a17a0 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20    int nPathOut, 
a17b0 0d 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f  ..  char *zPathO
a17c0 75 74 0d 0a 29 7b 0d 0a 20 20 44 4f 5f 4f 53 5f  ut..){..  DO_OS_
a17d0 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0d  MALLOC_TEST(0);.
a17e0 0a 20 20 7a 50 61 74 68 4f 75 74 5b 30 5d 20 3d  .  zPathOut[0] =
a17f0 20 30 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 56   0;..  return pV
a1800 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d  fs->xFullPathnam
a1810 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e  e(pVfs, zPath, n
a1820 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75  PathOut, zPathOu
a1830 74 29 3b 0d 0a 7d 0d 0a 23 69 66 6e 64 65 66 20  t);..}..#ifndef 
a1840 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
a1850 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a 53 51 4c 49  _EXTENSION..SQLI
a1860 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a1870 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
a1880 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
a1890 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
a18a0 7a 50 61 74 68 29 7b 0d 0a 20 20 72 65 74 75 72  zPath){..  retur
a18b0 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28  n pVfs->xDlOpen(
a18c0 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0d 0a 7d  pVfs, zPath);..}
a18d0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
a18e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44   void sqlite3OsD
a18f0 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
a1900 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
a1910 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  yte, char *zBufO
a1920 75 74 29 7b 0d 0a 20 20 70 56 66 73 2d 3e 78 44  ut){..  pVfs->xD
a1930 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79  lError(pVfs, nBy
a1940 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0d 0a 7d  te, zBufOut);..}
a1950 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
a1960 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f   void (*sqlite3O
a1970 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  sDlSym(sqlite3_v
a1980 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
a1990 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  pHdle, const cha
a19a0 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b  r *zSym))(void){
a19b0 0d 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  ..  return pVfs-
a19c0 3e 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48  >xDlSym(pVfs, pH
a19d0 64 6c 65 2c 20 7a 53 79 6d 29 3b 0d 0a 7d 0d 0a  dle, zSym);..}..
a19e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a19f0 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43  oid sqlite3OsDlC
a1a00 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lose(sqlite3_vfs
a1a10 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
a1a20 61 6e 64 6c 65 29 7b 0d 0a 20 20 70 56 66 73 2d  andle){..  pVfs-
a1a30 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20  >xDlClose(pVfs, 
a1a40 70 48 61 6e 64 6c 65 29 3b 0d 0a 7d 0d 0a 23 65  pHandle);..}..#e
a1a50 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
a1a60 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
a1a70 4f 4e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ON */..SQLITE_PR
a1a80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a1a90 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
a1aa0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
a1ab0 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72   int nByte, char
a1ac0 20 2a 7a 42 75 66 4f 75 74 29 7b 0d 0a 20 20 72   *zBufOut){..  r
a1ad0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e  eturn pVfs->xRan
a1ae0 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42  domness(pVfs, nB
a1af0 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0d 0a  yte, zBufOut);..
a1b00 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
a1b10 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
a1b20 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73  leep(sqlite3_vfs
a1b30 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63   *pVfs, int nMic
a1b40 72 6f 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70  ro){..  return p
a1b50 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 73  Vfs->xSleep(pVfs
a1b60 2c 20 6e 4d 69 63 72 6f 29 3b 0d 0a 7d 0d 0a 53  , nMicro);..}..S
a1b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a1b80 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  t sqlite3OsCurre
a1b90 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71 6c 69  ntTimeInt64(sqli
a1ba0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 73  te3_vfs *pVfs, s
a1bb0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 54  qlite3_int64 *pT
a1bc0 69 6d 65 4f 75 74 29 7b 0d 0a 20 20 69 6e 74 20  imeOut){..  int 
a1bd0 72 63 3b 0d 0a 20 20 2f 2a 20 49 4d 50 4c 45 4d  rc;..  /* IMPLEM
a1be0 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 34  ENTATION-OF: R-4
a1bf0 39 30 34 35 2d 34 32 34 39 33 20 53 51 4c 69 74  9045-42493 SQLit
a1c00 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 78  e will use the x
a1c10 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34  CurrentTimeInt64
a1c20 28 29 0d 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20  ()..  ** method 
a1c30 74 6f 20 67 65 74 20 74 68 65 20 63 75 72 72 65  to get the curre
a1c40 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  nt date and time
a1c50 20 69 66 20 74 68 61 74 20 6d 65 74 68 6f 64 20   if that method 
a1c60 69 73 20 61 76 61 69 6c 61 62 6c 65 0d 0a 20 20  is available..  
a1c70 2a 2a 20 28 69 66 20 69 56 65 72 73 69 6f 6e 20  ** (if iVersion 
a1c80 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 72 20  is 2 or greater 
a1c90 61 6e 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  and the function
a1ca0 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
a1cb0 4e 55 4c 4c 29 20 61 6e 64 0d 0a 20 20 2a 2a 20  NULL) and..  ** 
a1cc0 77 69 6c 6c 20 66 61 6c 6c 20 62 61 63 6b 20 74  will fall back t
a1cd0 6f 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29  o xCurrentTime()
a1ce0 20 69 66 20 78 43 75 72 72 65 6e 74 54 69 6d 65   if xCurrentTime
a1cf0 49 6e 74 36 34 28 29 20 69 73 0d 0a 20 20 2a 2a  Int64() is..  **
a1d00 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 0d 0a 20   unavailable... 
a1d10 20 2a 2f 0d 0a 20 20 69 66 28 20 70 56 66 73 2d   */..  if( pVfs-
a1d20 3e 69 56 65 72 73 69 6f 6e 3e 3d 32 20 26 26 20  >iVersion>=2 && 
a1d30 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69  pVfs->xCurrentTi
a1d40 6d 65 49 6e 74 36 34 20 29 7b 0d 0a 20 20 20 20  meInt64 ){..    
a1d50 72 63 20 3d 20 70 56 66 73 2d 3e 78 43 75 72 72  rc = pVfs->xCurr
a1d60 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 70 56 66  entTimeInt64(pVf
a1d70 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0d 0a 20  s, pTimeOut);.. 
a1d80 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 64 6f 75   }else{..    dou
a1d90 62 6c 65 20 72 3b 0d 0a 20 20 20 20 72 63 20 3d  ble r;..    rc =
a1da0 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54   pVfs->xCurrentT
a1db0 69 6d 65 28 70 56 66 73 2c 20 26 72 29 3b 0d 0a  ime(pVfs, &r);..
a1dc0 20 20 20 20 2a 70 54 69 6d 65 4f 75 74 20 3d 20      *pTimeOut = 
a1dd0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
a1de0 72 2a 38 36 34 30 30 30 30 30 2e 30 29 3b 0d 0a  r*86400000.0);..
a1df0 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
a1e00 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50  ;..}....SQLITE_P
a1e10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a1e20 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0d  e3OsOpenMalloc(.
a1e30 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
a1e40 70 56 66 73 2c 20 0d 0a 20 20 63 6f 6e 73 74 20  pVfs, ..  const 
a1e50 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 0d 0a 20  char *zFile, .. 
a1e60 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a   sqlite3_file **
a1e70 70 70 46 69 6c 65 2c 20 0d 0a 20 20 69 6e 74 20  ppFile, ..  int 
a1e80 66 6c 61 67 73 2c 0d 0a 20 20 69 6e 74 20 2a 70  flags,..  int *p
a1e90 4f 75 74 46 6c 61 67 73 0d 0a 29 7b 0d 0a 20 20  OutFlags..){..  
a1ea0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
a1eb0 4e 4f 4d 45 4d 3b 0d 0a 20 20 73 71 6c 69 74 65  NOMEM;..  sqlite
a1ec0 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0d 0a  3_file *pFile;..
a1ed0 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74    pFile = (sqlit
a1ee0 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65  e3_file *)sqlite
a1ef0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 73  3MallocZero(pVfs
a1f00 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0d 0a 20 20  ->szOsFile);..  
a1f10 69 66 28 20 70 46 69 6c 65 20 29 7b 0d 0a 20 20  if( pFile ){..  
a1f20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
a1f30 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
a1f40 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
a1f50 70 4f 75 74 46 6c 61 67 73 29 3b 0d 0a 20 20 20  pOutFlags);..   
a1f60 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
a1f70 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  OK ){..      sql
a1f80 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29  ite3_free(pFile)
a1f90 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
a1fa0 20 20 20 20 20 2a 70 70 46 69 6c 65 20 3d 20 70       *ppFile = p
a1fb0 46 69 6c 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  File;..    }..  
a1fc0 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
a1fd0 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
a1fe0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
a1ff0 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65  CloseFree(sqlite
a2000 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0d  3_file *pFile){.
a2010 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
a2020 54 45 5f 4f 4b 3b 0d 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
a2030 28 20 70 46 69 6c 65 20 29 3b 0d 0a 20 20 72 63  ( pFile );..  rc
a2040 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73   = sqlite3OsClos
a2050 65 28 70 46 69 6c 65 29 3b 0d 0a 20 20 73 71 6c  e(pFile);..  sql
a2060 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29  ite3_free(pFile)
a2070 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
a2080 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
a2090 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
a20a0 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74  wrapper around t
a20b0 68 65 20 4f 53 20 73 70 65 63 69 66 69 63 20 69  he OS specific i
a20c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
a20d0 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ..** sqlite3_os_
a20e0 69 6e 69 74 28 29 2e 20 54 68 65 20 70 75 72 70  init(). The purp
a20f0 6f 73 65 20 6f 66 20 74 68 65 20 77 72 61 70 70  ose of the wrapp
a2100 65 72 20 69 73 20 74 6f 20 70 72 6f 76 69 64 65  er is to provide
a2110 20 74 68 65 0d 0a 2a 2a 20 61 62 69 6c 69 74 79   the..** ability
a2120 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 6d   to simulate a m
a2130 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2c 20 73  alloc failure, s
a2140 6f 20 74 68 61 74 20 74 68 65 20 68 61 6e 64 6c  o that the handl
a2150 69 6e 67 20 6f 66 20 61 6e 0d 0a 2a 2a 20 65 72  ing of an..** er
a2160 72 6f 72 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f  ror in sqlite3_o
a2170 73 5f 69 6e 69 74 28 29 20 62 79 20 74 68 65 20  s_init() by the 
a2180 75 70 70 65 72 20 6c 61 79 65 72 73 20 63 61 6e  upper layers can
a2190 20 62 65 20 74 65 73 74 65 64 2e 0d 0a 2a 2f 0d   be tested...*/.
a21a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a21b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69  int sqlite3OsIni
a21c0 74 28 76 6f 69 64 29 7b 0d 0a 20 20 76 6f 69 64  t(void){..  void
a21d0 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61   *p = sqlite3_ma
a21e0 6c 6c 6f 63 28 31 30 29 3b 0d 0a 20 20 69 66 28  lloc(10);..  if(
a21f0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53   p==0 ) return S
a2200 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
a2210 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
a2220 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
a2230 65 33 5f 6f 73 5f 69 6e 69 74 28 29 3b 0d 0a 7d  e3_os_init();..}
a2240 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6c  ..../*..** The l
a2250 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73  ist of all regis
a2260 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d  tered VFS implem
a2270 65 6e 74 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a  entations...*/..
a2280 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
a2290 66 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20  fs * SQLITE_WSD 
a22a0 76 66 73 4c 69 73 74 20 3d 20 30 3b 0d 0a 23 64  vfsList = 0;..#d
a22b0 65 66 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c  efine vfsList GL
a22c0 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73  OBAL(sqlite3_vfs
a22d0 20 2a 2c 20 76 66 73 4c 69 73 74 29 0d 0a 0d 0a   *, vfsList)....
a22e0 2f 2a 0d 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20  /*..** Locate a 
a22f0 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66  VFS by name.  If
a2300 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65   no name is give
a2310 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e  n, simply return
a2320 20 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 56   the..** first V
a2330 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0d  FS on the list..
a2340 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
a2350 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c  sqlite3_vfs *sql
a2360 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f  ite3_vfs_find(co
a2370 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b  nst char *zVfs){
a2380 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ..  sqlite3_vfs 
a2390 2a 70 56 66 73 20 3d 20 30 3b 0d 0a 23 69 66 20  *pVfs = 0;..#if 
a23a0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
a23b0 45 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  E..  sqlite3_mut
a23c0 65 78 20 2a 6d 75 74 65 78 3b 0d 0a 23 65 6e 64  ex *mutex;..#end
a23d0 69 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
a23e0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
a23f0 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ..  int rc = sql
a2400 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
a2410 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 20 72  );..  if( rc ) r
a2420 65 74 75 72 6e 20 30 3b 0d 0a 23 65 6e 64 69 66  eturn 0;..#endif
a2430 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ..#if SQLITE_THR
a2440 45 41 44 53 41 46 45 0d 0a 20 20 6d 75 74 65 78  EADSAFE..  mutex
a2450 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
a2460 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
a2470 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
a2480 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c  ;..#endif..  sql
a2490 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
a24a0 28 6d 75 74 65 78 29 3b 0d 0a 20 20 66 6f 72 28  (mutex);..  for(
a24b0 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 3b 20  pVfs = vfsList; 
a24c0 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 73 2d  pVfs; pVfs=pVfs-
a24d0 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66  >pNext){..    if
a24e0 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 61  ( zVfs==0 ) brea
a24f0 6b 3b 0d 0a 20 20 20 20 69 66 28 20 73 74 72 63  k;..    if( strc
a2500 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a  mp(zVfs, pVfs->z
a2510 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
a2520 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
a2530 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
a2540 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  tex);..  return 
a2550 70 56 66 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  pVfs;..}..../*..
a2560 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20  ** Unlink a VFS 
a2570 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20  from the linked 
a2580 6c 69 73 74 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  list..*/..static
a2590 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28   void vfsUnlink(
a25a0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a25b0 73 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  s){..  assert( s
a25c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
a25d0 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  d(sqlite3MutexAl
a25e0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
a25f0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
a2600 20 29 3b 0d 0a 20 20 69 66 28 20 70 56 66 73 3d   );..  if( pVfs=
a2610 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 4e 6f  =0 ){..    /* No
a2620 2d 6f 70 20 2a 2f 0d 0a 20 20 7d 65 6c 73 65 20  -op */..  }else 
a2630 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70 56 66  if( vfsList==pVf
a2640 73 20 29 7b 0d 0a 20 20 20 20 76 66 73 4c 69 73  s ){..    vfsLis
a2650 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b  t = pVfs->pNext;
a2660 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66  ..  }else if( vf
a2670 73 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20 73 71  sList ){..    sq
a2680 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76  lite3_vfs *p = v
a2690 66 73 4c 69 73 74 3b 0d 0a 20 20 20 20 77 68 69  fsList;..    whi
a26a0 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20  le( p->pNext && 
a26b0 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29  p->pNext!=pVfs )
a26c0 7b 0d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  {..      p = p->
a26d0 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  pNext;..    }.. 
a26e0 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d     if( p->pNext=
a26f0 3d 70 56 66 73 20 29 7b 0d 0a 20 20 20 20 20 20  =pVfs ){..      
a2700 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d  p->pNext = pVfs-
a2710 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a  >pNext;..    }..
a2720 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
a2730 20 52 65 67 69 73 74 65 72 20 61 20 56 46 53 20   Register a VFS 
a2740 77 69 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e  with the system.
a2750 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73    It is harmless
a2760 20 74 6f 20 72 65 67 69 73 74 65 72 20 74 68 65   to register the
a2770 20 73 61 6d 65 0d 0a 2a 2a 20 56 46 53 20 6d 75   same..** VFS mu
a2780 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20 20 54  ltiple times.  T
a2790 68 65 20 6e 65 77 20 56 46 53 20 62 65 63 6f 6d  he new VFS becom
a27a0 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 69  es the default i
a27b0 66 20 6d 61 6b 65 44 66 6c 74 20 69 73 0d 0a 2a  f makeDflt is..*
a27c0 2a 20 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  * true...*/..SQL
a27d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
a27e0 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72  te3_vfs_register
a27f0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
a2800 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74  fs, int makeDflt
a2810 29 7b 0d 0a 20 20 4d 55 54 45 58 5f 4c 4f 47 49  ){..  MUTEX_LOGI
a2820 43 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  C(sqlite3_mutex 
a2830 2a 6d 75 74 65 78 3b 29 0d 0a 23 69 66 6e 64 65  *mutex;)..#ifnde
a2840 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
a2850 54 4f 49 4e 49 54 0d 0a 20 20 69 6e 74 20 72 63  TOINIT..  int rc
a2860 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
a2870 61 6c 69 7a 65 28 29 3b 0d 0a 20 20 69 66 28 20  alize();..  if( 
a2880 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d  rc ) return rc;.
a2890 0a 23 65 6e 64 69 66 0d 0a 20 20 4d 55 54 45 58  .#endif..  MUTEX
a28a0 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78 20 3d 20  _LOGIC( mutex = 
a28b0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
a28c0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
a28d0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 20 29  TATIC_MASTER); )
a28e0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
a28f0 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0d  x_enter(mutex);.
a2900 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66  .  vfsUnlink(pVf
a2910 73 29 3b 0d 0a 20 20 69 66 28 20 6d 61 6b 65 44  s);..  if( makeD
a2920 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d  flt || vfsList==
a2930 30 20 29 7b 0d 0a 20 20 20 20 70 56 66 73 2d 3e  0 ){..    pVfs->
a2940 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b  pNext = vfsList;
a2950 0d 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20  ..    vfsList = 
a2960 70 56 66 73 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  pVfs;..  }else{.
a2970 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74  .    pVfs->pNext
a2980 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78   = vfsList->pNex
a2990 74 3b 0d 0a 20 20 20 20 76 66 73 4c 69 73 74 2d  t;..    vfsList-
a29a0 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b 0d 0a  >pNext = pVfs;..
a29b0 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 76 66    }..  assert(vf
a29c0 73 4c 69 73 74 29 3b 0d 0a 20 20 73 71 6c 69 74  sList);..  sqlit
a29d0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a29e0 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
a29f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
a2a00 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 72 65 67 69 73  ../*..** Unregis
a2a10 74 65 72 20 61 20 56 46 53 20 73 6f 20 74 68 61  ter a VFS so tha
a2a20 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  t it is no longe
a2a30 72 20 61 63 63 65 73 73 69 62 6c 65 2e 0d 0a 2a  r accessible...*
a2a40 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
a2a50 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e  t sqlite3_vfs_un
a2a60 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33  register(sqlite3
a2a70 5f 76 66 73 20 2a 70 56 66 73 29 7b 0d 0a 23 69  _vfs *pVfs){..#i
a2a80 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
a2a90 41 46 45 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  AFE..  sqlite3_m
a2aa0 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
a2ab0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
a2ac0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
a2ad0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0d 0a 23 65  TIC_MASTER);..#e
a2ae0 6e 64 69 66 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ndif..  sqlite3_
a2af0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
a2b00 78 29 3b 0d 0a 20 20 76 66 73 55 6e 6c 69 6e 6b  x);..  vfsUnlink
a2b10 28 70 56 66 73 29 3b 0d 0a 20 20 73 71 6c 69 74  (pVfs);..  sqlit
a2b20 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a2b30 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
a2b40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
a2b50 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
a2b60 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a  * End of os.c **
a2b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2ba0 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
a2bb0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61  ** Begin file fa
a2bc0 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ult.c **********
a2bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2bf0 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20  */../*..** 2008 
a2c00 4a 61 6e 20 32 32 0d 0a 2a 2a 0d 0a 2a 2a 20 54  Jan 22..**..** T
a2c10 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
a2c20 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
a2c30 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
a2c40 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
a2c50 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
a2c60 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
a2c70 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
a2c80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
a2c90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
a2ca0 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a2cb0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
a2cc0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
a2cd0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
a2ce0 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
a2cf0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
a2d00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
a2d10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
a2d20 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
a2d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
a2d70 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
a2d80 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
a2d90 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f  o support the co
a2da0 6e 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e  ncept of "benign
a2db0 22 20 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  " ..** malloc fa
a2dc0 69 6c 75 72 65 73 20 28 77 68 65 6e 20 74 68 65  ilures (when the
a2dd0 20 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52   xMalloc() or xR
a2de0 65 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20  ealloc() method 
a2df0 6f 66 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74  of the..** sqlit
a2e00 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73  e3_mem_methods s
a2e10 74 72 75 63 74 75 72 65 20 66 61 69 6c 73 20 74  tructure fails t
a2e20 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f  o allocate a blo
a2e30 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0d 0a 2a 2a  ck of memory..**
a2e40 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 29 2e   and returns 0).
a2e50 20 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 73 74 20 6d   ..**..** Most m
a2e60 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61  alloc failures a
a2e70 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41  re non-benign. A
a2e80 66 74 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c  fter they occur,
a2e90 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 61 62 61 6e   SQLite..** aban
a2ea0 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74  dons the current
a2eb0 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72   operation and r
a2ec0 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
a2ed0 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0d 0a 2a  code (usually..*
a2ee0 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20  * SQLITE_NOMEM) 
a2ef0 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77  to the user. How
a2f00 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20  ever, sometimes 
a2f10 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e  a fault is not n
a2f20 65 63 65 73 73 61 72 69 6c 79 0d 0a 2a 2a 20 66  ecessarily..** f
a2f30 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c  atal. For exampl
a2f40 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66  e, if a malloc f
a2f50 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a  ails while resiz
a2f60 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65  ing a hash table
a2f70 2c 20 74 68 69 73 20 0d 0a 2a 2a 20 69 73 20 63  , this ..** is c
a2f80 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65  ompletely recove
a2f90 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20  rable simply by 
a2fa0 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74  not carrying out
a2fb0 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65   the resize. The
a2fc0 20 0d 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65   ..** hash table
a2fd0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
a2fe0 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61  o function norma
a2ff0 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f  lly.  So a mallo
a3000 63 20 66 61 69 6c 75 72 65 20 0d 0a 2a 2a 20 64  c failure ..** d
a3010 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 62  uring a hash tab
a3020 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 62  le resize is a b
a3030 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0d 0a 2a 2f  enign fault...*/
a3040 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  ......#ifndef SQ
a3050 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
a3060 4e 5f 54 45 53 54 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  N_TEST..../*..**
a3070 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   Global variable
a3080 73 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20  s...*/..typedef 
a3090 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c  struct BenignMal
a30a0 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d  locHooks BenignM
a30b0 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0d 0a 73 74 61  allocHooks;..sta
a30c0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
a30d0 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c  truct BenignMall
a30e0 6f 63 48 6f 6f 6b 73 20 7b 0d 0a 20 20 76 6f 69  ocHooks {..  voi
a30f0 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e  d (*xBenignBegin
a3100 29 28 76 6f 69 64 29 3b 0d 0a 20 20 76 6f 69 64  )(void);..  void
a3110 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76   (*xBenignEnd)(v
a3120 6f 69 64 29 3b 0d 0a 7d 20 73 71 6c 69 74 65 33  oid);..} sqlite3
a3130 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d  Hooks = { 0, 0 }
a3140 3b 0d 0a 0d 0a 2f 2a 20 54 68 65 20 22 77 73 64  ;..../* The "wsd
a3150 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c  Hooks" macro wil
a3160 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
a3170 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e   appropriate Ben
a3180 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0d 0a  ignMallocHooks..
a3190 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 49  ** structure.  I
a31a0 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  f writable stati
a31b0 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70  c data is unsupp
a31c0 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72  orted on the tar
a31d0 67 65 74 2c 0d 0a 2a 2a 20 77 65 20 68 61 76 65  get,..** we have
a31e0 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73   to locate the s
a31f0 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72  tate vector at r
a3200 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65  un-time.  In the
a3210 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0d 0a 2a 2a   more common..**
a3220 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74   case where writ
a3230 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
a3240 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77   is supported, w
a3250 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65  sdHooks can refe
a3260 72 20 64 69 72 65 63 74 6c 79 0d 0a 2a 2a 20 74  r directly..** t
a3270 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f  o the "sqlite3Ho
a3280 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f  oks" state vecto
a3290 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65  r declared above
a32a0 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
a32b0 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0d 0a 23  LITE_OMIT_WSD..#
a32c0 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73   define wsdHooks
a32d0 49 6e 69 74 20 5c 0d 0a 20 20 42 65 6e 69 67 6e  Init \..  Benign
a32e0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d  MallocHooks *x =
a32f0 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d   &GLOBAL(BenignM
a3300 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74  allocHooks,sqlit
a3310 65 33 48 6f 6f 6b 73 29 0d 0a 23 20 64 65 66 69  e3Hooks)..# defi
a3320 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d  ne wsdHooks x[0]
a3330 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
a3340 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0d 0a  e wsdHooksInit..
a3350 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b  # define wsdHook
a3360 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0d 0a  s sqlite3Hooks..
a3370 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  #endif....../*..
a3380 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b  ** Register hook
a3390 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73  s to call when s
a33a0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
a33b0 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0d 0a 2a  nMalloc() and..*
a33c0 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  * sqlite3EndBeni
a33d0 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63  gnMalloc() are c
a33e0 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76  alled, respectiv
a33f0 65 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ely...*/..SQLITE
a3400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a3410 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
a3420 63 48 6f 6f 6b 73 28 0d 0a 20 20 76 6f 69 64 20  cHooks(..  void 
a3430 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28  (*xBenignBegin)(
a3440 76 6f 69 64 29 2c 0d 0a 20 20 76 6f 69 64 20 28  void),..  void (
a3450 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69  *xBenignEnd)(voi
a3460 64 29 0d 0a 29 7b 0d 0a 20 20 77 73 64 48 6f 6f  d)..){..  wsdHoo
a3470 6b 73 49 6e 69 74 3b 0d 0a 20 20 77 73 64 48 6f  ksInit;..  wsdHo
a3480 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e  oks.xBenignBegin
a3490 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b   = xBenignBegin;
a34a0 0d 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65  ..  wsdHooks.xBe
a34b0 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67  nignEnd = xBenig
a34c0 6e 45 6e 64 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  nEnd;..}..../*..
a34d0 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33  ** This (sqlite3
a34e0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
a34f0 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  )) is called by 
a3500 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69  SQLite code to i
a3510 6e 64 69 63 61 74 65 20 74 68 61 74 0d 0a 2a 2a  ndicate that..**
a3520 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c   subsequent mall
a3530 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20  oc failures are 
a3540 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74  benign. A call t
a3550 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  o sqlite3EndBeni
a3560 67 6e 4d 61 6c 6c 6f 63 28 29 0d 0a 2a 2a 20 69  gnMalloc()..** i
a3570 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 75  ndicates that su
a3580 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20  bsequent malloc 
a3590 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e  failures are non
a35a0 2d 62 65 6e 69 67 6e 2e 0d 0a 2a 2f 0d 0a 53 51  -benign...*/..SQ
a35b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a35c0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  d sqlite3BeginBe
a35d0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29  nignMalloc(void)
a35e0 7b 0d 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69  {..  wsdHooksIni
a35f0 74 3b 0d 0a 20 20 69 66 28 20 77 73 64 48 6f 6f  t;..  if( wsdHoo
a3600 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20  ks.xBenignBegin 
a3610 29 7b 0d 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73  ){..    wsdHooks
a3620 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b  .xBenignBegin();
a3630 0d 0a 20 20 7d 0d 0a 7d 0d 0a 53 51 4c 49 54 45  ..  }..}..SQLITE
a3640 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a3650 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
a3660 6c 6c 6f 63 28 76 6f 69 64 29 7b 0d 0a 20 20 77  lloc(void){..  w
a3670 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0d 0a 20 20  sdHooksInit;..  
a3680 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65  if( wsdHooks.xBe
a3690 6e 69 67 6e 45 6e 64 20 29 7b 0d 0a 20 20 20 20  nignEnd ){..    
a36a0 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
a36b0 45 6e 64 28 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  End();..  }..}..
a36c0 0d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69  ..#endif   /* #i
a36d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a36e0 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
a36f0 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
a3700 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c  **** End of faul
a3710 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
a3720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3740 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
a3750 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
a3760 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem0.c ********
a3770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3790 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
a37a0 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0d 0a 2a  08 October 28..*
a37b0 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
a37c0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
a37d0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
a37e0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
a37f0 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
a3800 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
a3810 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
a3820 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
a3830 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
a3840 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
a3850 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
a3860 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
a3870 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
a3880 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
a3890 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
a38a0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
a38b0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
a38c0 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
a38d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a38e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a38f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3910 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
a3920 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
a3930 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20   a no-op memory 
a3940 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65  allocation drive
a3950 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0d  rs for use when.
a3960 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f  .** SQLITE_ZERO_
a3970 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65  MALLOC is define
a3980 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  d.  The allocati
a3990 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65  on drivers imple
a39a0 6d 65 6e 74 65 64 0d 0a 2a 2a 20 68 65 72 65 20  mented..** here 
a39b0 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51  always fail.  SQ
a39c0 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70  Lite will not op
a39d0 65 72 61 74 65 20 77 69 74 68 20 74 68 65 73 65  erate with these
a39e0 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 65   drivers.  These
a39f0 0d 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20  ..** are merely 
a3a00 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52  placeholders.  R
a3a10 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74  eal drivers must
a3a20 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
a3a30 75 73 69 6e 67 0d 0a 2a 2a 20 73 71 6c 69 74 65  using..** sqlite
a3a40 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72  3_config() befor
a3a50 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70  e SQLite will op
a3a60 65 72 61 74 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a  erate...*/..../*
a3a70 0d 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  ..** This versio
a3a80 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
a3a90 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65  allocator is the
a3aa0 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73   default.  It is
a3ab0 0d 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e  ..** used when n
a3ac0 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61  o other memory a
a3ad0 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63  llocator is spec
a3ae0 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70  ified using comp
a3af0 69 6c 65 2d 74 69 6d 65 0d 0a 2a 2a 20 6d 61 63  ile-time..** mac
a3b00 72 6f 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  ros...*/..#ifdef
a3b10 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c   SQLITE_ZERO_MAL
a3b20 4c 4f 43 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4e 6f  LOC..../*..** No
a3b30 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  -op versions of 
a3b40 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  all memory alloc
a3b50 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0d 0a  ation routines..
a3b60 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
a3b70 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f  *sqlite3MemMallo
a3b80 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65  c(int nByte){ re
a3b90 74 75 72 6e 20 30 3b 20 7d 0d 0a 73 74 61 74 69  turn 0; }..stati
a3ba0 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
a3bb0 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69  mFree(void *pPri
a3bc0 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0d 0a  or){ return; }..
a3bd0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
a3be0 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76  ite3MemRealloc(v
a3bf0 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74  oid *pPrior, int
a3c00 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20   nByte){ return 
a3c10 30 3b 20 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74  0; }..static int
a3c20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28   sqlite3MemSize(
a3c30 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72  void *pPrior){ r
a3c40 65 74 75 72 6e 20 30 3b 20 7d 0d 0a 73 74 61 74  eturn 0; }..stat
a3c50 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
a3c60 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b  mRoundup(int n){
a3c70 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0d 0a 73 74   return n; }..st
a3c80 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
a3c90 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  MemInit(void *No
a3ca0 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53  tUsed){ return S
a3cb0 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0d 0a 73 74 61  QLITE_OK; }..sta
a3cc0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
a3cd0 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  MemShutdown(void
a3ce0 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75   *NotUsed){ retu
a3cf0 72 6e 3b 20 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  rn; }..../*..** 
a3d00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
a3d10 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
a3d20 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
a3d30 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  th external link
a3d40 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 6f 70  age...**..** Pop
a3d50 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65  ulate the low-le
a3d60 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
a3d70 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70  ation function p
a3d80 6f 69 6e 74 65 72 73 20 69 6e 0d 0a 2a 2a 20 73  ointers in..** s
a3d90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a3da0 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65  ig.m with pointe
a3db0 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e  rs to the routin
a3dc0 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e  es in this file.
a3dd0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
a3de0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a3df0 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76  3MemSetDefault(v
a3e00 6f 69 64 29 7b 0d 0a 20 20 73 74 61 74 69 63 20  oid){..  static 
a3e10 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
a3e20 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
a3e30 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0d 0a 20 20  tMethods = {..  
a3e40 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
a3e50 6c 6f 63 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74  loc,..     sqlit
a3e60 65 33 4d 65 6d 46 72 65 65 2c 0d 0a 20 20 20 20  e3MemFree,..    
a3e70 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c   sqlite3MemReall
a3e80 6f 63 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65  oc,..     sqlite
a3e90 33 4d 65 6d 53 69 7a 65 2c 0d 0a 20 20 20 20 20  3MemSize,..     
a3ea0 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75  sqlite3MemRoundu
a3eb0 70 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65 33  p,..     sqlite3
a3ec0 4d 65 6d 49 6e 69 74 2c 0d 0a 20 20 20 20 20 73  MemInit,..     s
a3ed0 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77  qlite3MemShutdow
a3ee0 6e 2c 0d 0a 20 20 20 20 20 30 0d 0a 20 20 7d 3b  n,..     0..  };
a3ef0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ..  sqlite3_conf
a3f00 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
a3f10 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
a3f20 74 4d 65 74 68 6f 64 73 29 3b 0d 0a 7d 0d 0a 0d  tMethods);..}...
a3f30 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a3f40 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f  E_ZERO_MALLOC */
a3f50 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
a3f60 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30 2e  *** End of mem0.
a3f70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a3f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3fa0 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
a3fb0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
a3fc0 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem1.c *********
a3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3ff0 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
a4000 37 20 41 75 67 75 73 74 20 31 34 0d 0a 2a 2a 0d  7 August 14..**.
a4010 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
a4020 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
a4030 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
a4040 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
a4050 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c  e of..** a legal
a4060 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
a4070 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a   a blessing:..**
a4080 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a4090 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
a40a0 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  evil...**    May
a40b0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
a40c0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
a40d0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
a40e0 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61  thers...**    Ma
a40f0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
a4100 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
a4110 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
a4120 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a  ive...**..******
a4130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4170 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ***..**..** This
a4180 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c   file contains l
a4190 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
a41a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65  allocation drive
a41b0 72 73 20 66 6f 72 20 77 68 65 6e 0d 0a 2a 2a 20  rs for when..** 
a41c0 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20  SQLite will use 
a41d0 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c  the standard C-l
a41e0 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65  ibrary malloc/re
a41f0 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72  alloc/free inter
a4200 66 61 63 65 0d 0a 2a 2a 20 74 6f 20 6f 62 74 61  face..** to obta
a4210 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 74  in the memory it
a4220 20 6e 65 65 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   needs...**..** 
a4230 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
a4240 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ns implementatio
a4250 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65  ns of the low-le
a4260 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
a4270 61 74 69 6f 6e 0d 0a 2a 2a 20 72 6f 75 74 69 6e  ation..** routin
a4280 65 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  es specified in 
a4290 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  the sqlite3_mem_
a42a0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0d  methods object..
a42b0 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  .*/..../*..** Th
a42c0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
a42d0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
a42e0 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  or is the defaul
a42f0 74 2e 20 20 49 74 20 69 73 0d 0a 2a 2a 20 75 73  t.  It is..** us
a4300 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72  ed when no other
a4310 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
a4320 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75  r is specified u
a4330 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  sing compile-tim
a4340 65 0d 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0d 0a 2a  e..** macros...*
a4350 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
a4360 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0d 0a  _SYSTEM_MALLOC..
a4370 0d 0a 2f 2a 0d 0a 2a 2a 20 57 69 6e 64 6f 77 73  ../*..** Windows
a4380 20 73 79 73 74 65 6d 73 20 68 61 76 65 20 6d 61   systems have ma
a4390 6c 6c 6f 63 5f 75 73 61 62 6c 65 5f 73 69 7a 65  lloc_usable_size
a43a0 28 29 20 62 75 74 20 69 74 20 69 73 20 63 61 6c  () but it is cal
a43b0 6c 65 64 20 5f 6d 73 69 7a 65 28 29 0d 0a 2a 2f  led _msize()..*/
a43c0 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 48  ..#if !defined(H
a43d0 41 56 45 5f 4d 41 4c 4c 4f 43 5f 55 53 41 42 4c  AVE_MALLOC_USABL
a43e0 45 5f 53 49 5a 45 29 20 26 26 20 53 51 4c 49 54  E_SIZE) && SQLIT
a43f0 45 5f 4f 53 5f 57 49 4e 0d 0a 23 20 64 65 66 69  E_OS_WIN..# defi
a4400 6e 65 20 48 41 56 45 5f 4d 41 4c 4c 4f 43 5f 55  ne HAVE_MALLOC_U
a4410 53 41 42 4c 45 5f 53 49 5a 45 20 31 0d 0a 23 20  SABLE_SIZE 1..# 
a4420 64 65 66 69 6e 65 20 6d 61 6c 6c 6f 63 5f 75 73  define malloc_us
a4430 61 62 6c 65 5f 73 69 7a 65 20 5f 6d 73 69 7a 65  able_size _msize
a4440 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20  ..#endif....#if 
a4450 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
a4460 5f 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 73 65  _)..../*..** Use
a4470 20 74 68 65 20 7a 6f 6e 65 20 61 6c 6c 6f 63 61   the zone alloca
a4480 74 6f 72 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  tor available on
a4490 20 61 70 70 6c 65 20 70 72 6f 64 75 63 74 73 0d   apple products.
a44a0 0a 2a 2f 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73  .*/..#include <s
a44b0 79 73 2f 73 79 73 63 74 6c 2e 68 3e 0d 0a 23 69  ys/sysctl.h>..#i
a44c0 6e 63 6c 75 64 65 20 3c 6d 61 6c 6c 6f 63 2f 6d  nclude <malloc/m
a44d0 61 6c 6c 6f 63 2e 68 3e 0d 0a 23 69 6e 63 6c 75  alloc.h>..#inclu
a44e0 64 65 20 3c 6c 69 62 6b 65 72 6e 2f 4f 53 41 74  de <libkern/OSAt
a44f0 6f 6d 69 63 2e 68 3e 0d 0a 73 74 61 74 69 63 20  omic.h>..static 
a4500 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 74 2a 20 5f  malloc_zone_t* _
a4510 73 71 6c 69 74 65 5a 6f 6e 65 5f 3b 0d 0a 23 64  sqliteZone_;..#d
a4520 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c  efine SQLITE_MAL
a4530 4c 4f 43 28 78 29 20 6d 61 6c 6c 6f 63 5f 7a 6f  LOC(x) malloc_zo
a4540 6e 65 5f 6d 61 6c 6c 6f 63 28 5f 73 71 6c 69 74  ne_malloc(_sqlit
a4550 65 5a 6f 6e 65 5f 2c 20 28 78 29 29 0d 0a 23 64  eZone_, (x))..#d
a4560 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 52 45  efine SQLITE_FRE
a4570 45 28 78 29 20 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65  E(x) malloc_zone
a4580 5f 66 72 65 65 28 5f 73 71 6c 69 74 65 5a 6f 6e  _free(_sqliteZon
a4590 65 5f 2c 20 28 78 29 29 3b 0d 0a 23 64 65 66 69  e_, (x));..#defi
a45a0 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 4c 4c 4f  ne SQLITE_REALLO
a45b0 43 28 78 2c 79 29 20 6d 61 6c 6c 6f 63 5f 7a 6f  C(x,y) malloc_zo
a45c0 6e 65 5f 72 65 61 6c 6c 6f 63 28 5f 73 71 6c 69  ne_realloc(_sqli
a45d0 74 65 5a 6f 6e 65 5f 2c 20 28 78 29 2c 20 28 79  teZone_, (x), (y
a45e0 29 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  ))..#define SQLI
a45f0 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 28 78 29  TE_MALLOCSIZE(x)
a4600 20 5c 0d 0a 20 20 20 20 20 20 20 20 28 5f 73 71   \..        (_sq
a4610 6c 69 74 65 5a 6f 6e 65 5f 20 3f 20 5f 73 71 6c  liteZone_ ? _sql
a4620 69 74 65 5a 6f 6e 65 5f 2d 3e 73 69 7a 65 28 5f  iteZone_->size(_
a4630 73 71 6c 69 74 65 5a 6f 6e 65 5f 2c 78 29 20 3a  sqliteZone_,x) :
a4640 20 6d 61 6c 6c 6f 63 5f 73 69 7a 65 28 78 29 29   malloc_size(x))
a4650 0d 0a 0d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  ....#else /* if 
a4660 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f 5f 20 2a 2f  not __APPLE__ */
a4670 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 73 65 20 73  ..../*..** Use s
a4680 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
a4690 79 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 66 72 65  y malloc and fre
a46a0 65 20 6f 6e 20 6e 6f 6e 2d 41 70 70 6c 65 20 73  e on non-Apple s
a46b0 79 73 74 65 6d 73 2e 0d 0a 2a 2f 0d 0a 23 64 65  ystems...*/..#de
a46c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c  fine SQLITE_MALL
a46d0 4f 43 28 78 29 20 20 20 20 6d 61 6c 6c 6f 63 28  OC(x)    malloc(
a46e0 78 29 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  x)..#define SQLI
a46f0 54 45 5f 46 52 45 45 28 78 29 20 20 20 20 20 20  TE_FREE(x)      
a4700 66 72 65 65 28 78 29 0d 0a 23 64 65 66 69 6e 65  free(x)..#define
a4710 20 53 51 4c 49 54 45 5f 52 45 41 4c 4c 4f 43 28   SQLITE_REALLOC(
a4720 78 2c 79 29 20 72 65 61 6c 6c 6f 63 28 28 78 29  x,y) realloc((x)
a4730 2c 28 79 29 29 0d 0a 0d 0a 23 69 66 64 65 66 20  ,(y))....#ifdef 
a4740 48 41 56 45 5f 4d 41 4c 4c 4f 43 5f 55 53 41 42  HAVE_MALLOC_USAB
a4750 4c 45 5f 53 49 5a 45 0d 0a 23 69 6e 63 6c 75 64  LE_SIZE..#includ
a4760 65 20 3c 6d 61 6c 6c 6f 63 2e 68 3e 0d 0a 23 64  e <malloc.h>..#d
a4770 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c  efine SQLITE_MAL
a4780 4c 4f 43 53 49 5a 45 28 78 29 20 6d 61 6c 6c 6f  LOCSIZE(x) mallo
a4790 63 5f 75 73 61 62 6c 65 5f 73 69 7a 65 28 78 29  c_usable_size(x)
a47a0 0d 0a 23 65 6c 73 65 0d 0a 23 75 6e 64 65 66 20  ..#else..#undef 
a47b0 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a  SQLITE_MALLOCSIZ
a47c0 45 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 65 6e  E..#endif....#en
a47d0 64 69 66 20 2f 2a 20 5f 5f 41 50 50 4c 45 5f 5f  dif /* __APPLE__
a47e0 20 6f 72 20 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f   or not __APPLE_
a47f0 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c  _ */..../*..** L
a4800 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75  ike malloc(), bu
a4810 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73  t remember the s
a4820 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
a4830 61 74 69 6f 6e 0d 0a 2a 2a 20 73 6f 20 74 68 61  ation..** so tha
a4840 74 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74  t we can find it
a4850 20 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c   later using sql
a4860 69 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0d 0a  ite3MemSize()...
a4870 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  **..** For this 
a4880 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e  low-level routin
a4890 65 2c 20 77 65 20 61 72 65 20 67 75 61 72 61 6e  e, we are guaran
a48a0 74 65 65 64 20 74 68 61 74 20 6e 42 79 74 65 3e  teed that nByte>
a48b0 30 20 62 65 63 61 75 73 65 0d 0a 2a 2a 20 63 61  0 because..** ca
a48c0 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d 30 20  ses of nByte<=0 
a48d0 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 65 70  will be intercep
a48e0 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69  ted and dealt wi
a48f0 74 68 20 62 79 20 68 69 67 68 65 72 20 6c 65 76  th by higher lev
a4900 65 6c 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e  el..** routines.
a4910 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
a4920 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
a4930 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0d  loc(int nByte){.
a4940 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
a4950 41 4c 4c 4f 43 53 49 5a 45 0d 0a 20 20 76 6f 69  ALLOCSIZE..  voi
a4960 64 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f 4d 41  d *p = SQLITE_MA
a4970 4c 4c 4f 43 28 20 6e 42 79 74 65 20 29 3b 0d 0a  LLOC( nByte );..
a4980 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0d 0a 20    if( p==0 ){.. 
a4990 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c     testcase( sql
a49a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a49b0 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d 0a 20 20 20  .xLog!=0 );..   
a49c0 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c   sqlite3_log(SQL
a49d0 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c  ITE_NOMEM, "fail
a49e0 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 25  ed to allocate %
a49f0 75 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  u bytes of memor
a4a00 79 22 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 7d  y", nByte);..  }
a4a10 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 23  ..  return p;..#
a4a20 65 6c 73 65 0d 0a 20 20 73 71 6c 69 74 65 33 5f  else..  sqlite3_
a4a30 69 6e 74 36 34 20 2a 70 3b 0d 0a 20 20 61 73 73  int64 *p;..  ass
a4a40 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0d  ert( nByte>0 );.
a4a50 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44  .  nByte = ROUND
a4a60 38 28 6e 42 79 74 65 29 3b 0d 0a 20 20 70 20 3d  8(nByte);..  p =
a4a70 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 20   SQLITE_MALLOC( 
a4a80 6e 42 79 74 65 2b 38 20 29 3b 0d 0a 20 20 69 66  nByte+8 );..  if
a4a90 28 20 70 20 29 7b 0d 0a 20 20 20 20 70 5b 30 5d  ( p ){..    p[0]
a4aa0 20 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 70   = nByte;..    p
a4ab0 2b 2b 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  ++;..  }else{.. 
a4ac0 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c     testcase( sql
a4ad0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a4ae0 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d 0a 20 20 20  .xLog!=0 );..   
a4af0 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c   sqlite3_log(SQL
a4b00 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c  ITE_NOMEM, "fail
a4b10 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 25  ed to allocate %
a4b20 75 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  u bytes of memor
a4b30 79 22 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 7d  y", nByte);..  }
a4b40 0d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ..  return (void
a4b50 20 2a 29 70 3b 0d 0a 23 65 6e 64 69 66 0d 0a 7d   *)p;..#endif..}
a4b60 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 69 6b 65 20  ..../*..** Like 
a4b70 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73  free() but works
a4b80 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   for allocations
a4b90 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a4ba0 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
a4bb0 29 0d 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  )..** or sqlite3
a4bc0 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0d 0a 2a  MemRealloc()...*
a4bd0 2a 0d 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c  *..** For this l
a4be0 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  ow-level routine
a4bf0 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f  , we already kno
a4c00 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30  w that pPrior!=0
a4c10 20 73 69 6e 63 65 0d 0a 2a 2a 20 63 61 73 65 73   since..** cases
a4c20 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d 30   where pPrior==0
a4c30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
a4c40 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 64  intecepted and d
a4c50 65 61 6c 74 20 77 69 74 68 0d 0a 2a 2a 20 62 79  ealt with..** by
a4c60 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f   higher-level ro
a4c70 75 74 69 6e 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  utines...*/..sta
a4c80 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
a4c90 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
a4ca0 72 69 6f 72 29 7b 0d 0a 23 69 66 64 65 66 20 53  rior){..#ifdef S
a4cb0 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45  QLITE_MALLOCSIZE
a4cc0 0d 0a 20 20 53 51 4c 49 54 45 5f 46 52 45 45 28  ..  SQLITE_FREE(
a4cd0 70 50 72 69 6f 72 29 3b 0d 0a 23 65 6c 73 65 0d  pPrior);..#else.
a4ce0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
a4cf0 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69   *p = (sqlite3_i
a4d00 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0d 0a 20  nt64*)pPrior;.. 
a4d10 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
a4d20 3d 30 20 29 3b 0d 0a 20 20 70 2d 2d 3b 0d 0a 20  =0 );..  p--;.. 
a4d30 20 53 51 4c 49 54 45 5f 46 52 45 45 28 70 29 3b   SQLITE_FREE(p);
a4d40 0d 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f  ..#endif..}..../
a4d50 2a 0d 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65  *..** Report the
a4d60 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   allocated size 
a4d70 6f 66 20 61 20 70 72 69 6f 72 20 72 65 74 75 72  of a prior retur
a4d80 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29  n from xMalloc()
a4d90 0d 0a 2a 2a 20 6f 72 20 78 52 65 61 6c 6c 6f 63  ..** or xRealloc
a4da0 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ()...*/..static 
a4db0 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69  int sqlite3MemSi
a4dc0 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ze(void *pPrior)
a4dd0 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  {..#ifdef SQLITE
a4de0 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0d 0a 20 20 72  _MALLOCSIZE..  r
a4df0 65 74 75 72 6e 20 70 50 72 69 6f 72 20 3f 20 28  eturn pPrior ? (
a4e00 69 6e 74 29 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  int)SQLITE_MALLO
a4e10 43 53 49 5a 45 28 70 50 72 69 6f 72 29 20 3a 20  CSIZE(pPrior) : 
a4e20 30 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 73 71 6c  0;..#else..  sql
a4e30 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0d 0a  ite3_int64 *p;..
a4e40 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20    if( pPrior==0 
a4e50 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 70  ) return 0;..  p
a4e60 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
a4e70 34 2a 29 70 50 72 69 6f 72 3b 0d 0a 20 20 70 2d  4*)pPrior;..  p-
a4e80 2d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e  -;..  return (in
a4e90 74 29 70 5b 30 5d 3b 0d 0a 23 65 6e 64 69 66 0d  t)p[0];..#endif.
a4ea0 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 69 6b  .}..../*..** Lik
a4eb0 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65  e realloc().  Re
a4ec0 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69  size an allocati
a4ed0 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62  on previously ob
a4ee0 74 61 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20  tained from..** 
a4ef0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
a4f00 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20  ()...**..** For 
a4f10 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69  this low-level i
a4f20 6e 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f  nterface, we kno
a4f30 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30  w that pPrior!=0
a4f40 2e 20 20 43 61 73 65 73 20 77 68 65 72 65 0d 0a  .  Cases where..
a4f50 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69  ** pPrior==0 whi
a4f60 6c 65 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74  le have been int
a4f70 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67 68  ercepted by high
a4f80 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er-level routine
a4f90 20 61 6e 64 0d 0a 2a 2a 20 72 65 64 69 72 65 63   and..** redirec
a4fa0 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20  ted to xMalloc. 
a4fb0 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b   Similarly, we k
a4fc0 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 3e 30  now that nByte>0
a4fd0 20 62 65 63 61 75 73 65 73 0d 0a 2a 2a 20 63 61   becauses..** ca
a4fe0 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 3c  ses where nByte<
a4ff0 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  =0 will have bee
a5000 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79  n intercepted by
a5010 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0d 0a 2a   higher-level..*
a5020 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72  * routines and r
a5030 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72  edirected to xFr
a5040 65 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ee...*/..static 
a5050 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
a5060 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
a5070 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
a5080 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  {..#ifdef SQLITE
a5090 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0d 0a 20 20 76  _MALLOCSIZE..  v
a50a0 6f 69 64 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f  oid *p = SQLITE_
a50b0 52 45 41 4c 4c 4f 43 28 70 50 72 69 6f 72 2c 20  REALLOC(pPrior, 
a50c0 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66 28 20 70  nByte);..  if( p
a50d0 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 74 65 73 74  ==0 ){..    test
a50e0 63 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f  case( sqlite3Glo
a50f0 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d  balConfig.xLog!=
a5100 30 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0 );..    sqlite
a5110 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d  3_log(SQLITE_NOM
a5120 45 4d 2c 0d 0a 20 20 20 20 20 20 22 66 61 69 6c  EM,..      "fail
a5130 65 64 20 6d 65 6d 6f 72 79 20 72 65 73 69 7a 65  ed memory resize
a5140 20 25 75 20 74 6f 20 25 75 20 62 79 74 65 73 22   %u to %u bytes"
a5150 2c 0d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ,..      SQLITE_
a5160 4d 41 4c 4c 4f 43 53 49 5a 45 28 70 50 72 69 6f  MALLOCSIZE(pPrio
a5170 72 29 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 7d  r), nByte);..  }
a5180 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 23  ..  return p;..#
a5190 65 6c 73 65 0d 0a 20 20 73 71 6c 69 74 65 33 5f  else..  sqlite3_
a51a0 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69  int64 *p = (sqli
a51b0 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f  te3_int64*)pPrio
a51c0 72 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  r;..  assert( pP
a51d0 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 74 65  rior!=0 && nByte
a51e0 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  >0 );..  assert(
a51f0 20 6e 42 79 74 65 3d 3d 52 4f 55 4e 44 38 28 6e   nByte==ROUND8(n
a5200 42 79 74 65 29 20 29 3b 20 2f 2a 20 45 56 3a 20  Byte) ); /* EV: 
a5210 52 2d 34 36 31 39 39 2d 33 30 32 34 39 20 2a 2f  R-46199-30249 */
a5220 0d 0a 20 20 70 2d 2d 3b 0d 0a 20 20 70 20 3d 20  ..  p--;..  p = 
a5230 53 51 4c 49 54 45 5f 52 45 41 4c 4c 4f 43 28 70  SQLITE_REALLOC(p
a5240 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0d 0a 20 20  , nByte+8 );..  
a5250 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 70 5b  if( p ){..    p[
a5260 30 5d 20 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20  0] = nByte;..   
a5270 20 70 2b 2b 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d   p++;..  }else{.
a5280 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73  .    testcase( s
a5290 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a52a0 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d 0a 20  ig.xLog!=0 );.. 
a52b0 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53     sqlite3_log(S
a52c0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 0d 0a 20 20  QLITE_NOMEM,..  
a52d0 20 20 20 20 22 66 61 69 6c 65 64 20 6d 65 6d 6f      "failed memo
a52e0 72 79 20 72 65 73 69 7a 65 20 25 75 20 74 6f 20  ry resize %u to 
a52f0 25 75 20 62 79 74 65 73 22 2c 0d 0a 20 20 20 20  %u bytes",..    
a5300 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65    sqlite3MemSize
a5310 28 70 50 72 69 6f 72 29 2c 20 6e 42 79 74 65 29  (pPrior), nByte)
a5320 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
a5330 20 28 76 6f 69 64 2a 29 70 3b 0d 0a 23 65 6e 64   (void*)p;..#end
a5340 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  if..}..../*..** 
a5350 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65  Round up a reque
a5360 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e  st size to the n
a5370 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61  ext valid alloca
a5380 74 69 6f 6e 20 73 69 7a 65 2e 0d 0a 2a 2f 0d 0a  tion size...*/..
a5390 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
a53a0 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74  e3MemRoundup(int
a53b0 20 6e 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 52   n){..  return R
a53c0 4f 55 4e 44 38 28 6e 29 3b 0d 0a 7d 0d 0a 0d 0a  OUND8(n);..}....
a53d0 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  /*..** Initializ
a53e0 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0d 0a  e this module...
a53f0 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  */..static int s
a5400 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f  qlite3MemInit(vo
a5410 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a 23  id *NotUsed){..#
a5420 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
a5430 4c 45 5f 5f 29 0d 0a 20 20 69 6e 74 20 63 70 75  LE__)..  int cpu
a5440 43 6f 75 6e 74 3b 0d 0a 20 20 73 69 7a 65 5f 74  Count;..  size_t
a5450 20 6c 65 6e 3b 0d 0a 20 20 69 66 28 20 5f 73 71   len;..  if( _sq
a5460 6c 69 74 65 5a 6f 6e 65 5f 20 29 7b 0d 0a 20 20  liteZone_ ){..  
a5470 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a5480 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 6c 65 6e 20  OK;..  }..  len 
a5490 3d 20 73 69 7a 65 6f 66 28 63 70 75 43 6f 75 6e  = sizeof(cpuCoun
a54a0 74 29 3b 0d 0a 20 20 2f 2a 20 4f 6e 65 20 75 73  t);..  /* One us
a54b0 75 61 6c 6c 79 20 77 61 6e 74 73 20 74 6f 20 75  ually wants to u
a54c0 73 65 20 68 77 2e 61 63 63 74 69 76 65 63 70 75  se hw.acctivecpu
a54d0 20 66 6f 72 20 4d 54 20 64 65 63 69 73 69 6f 6e   for MT decision
a54e0 73 2c 20 62 75 74 20 6e 6f 74 20 68 65 72 65 20  s, but not here 
a54f0 2a 2f 0d 0a 20 20 73 79 73 63 74 6c 62 79 6e 61  */..  sysctlbyna
a5500 6d 65 28 22 68 77 2e 6e 63 70 75 22 2c 20 26 63  me("hw.ncpu", &c
a5510 70 75 43 6f 75 6e 74 2c 20 26 6c 65 6e 2c 20 4e  puCount, &len, N
a5520 55 4c 4c 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20  ULL, 0);..  if( 
a5530 63 70 75 43 6f 75 6e 74 3e 31 20 29 7b 0d 0a 20  cpuCount>1 ){.. 
a5540 20 20 20 2f 2a 20 64 65 66 65 72 20 4d 54 20 64     /* defer MT d
a5550 65 63 69 73 69 6f 6e 73 20 74 6f 20 73 79 73 74  ecisions to syst
a5560 65 6d 20 6d 61 6c 6c 6f 63 20 2a 2f 0d 0a 20 20  em malloc */..  
a5570 20 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 20 3d    _sqliteZone_ =
a5580 20 6d 61 6c 6c 6f 63 5f 64 65 66 61 75 6c 74 5f   malloc_default_
a5590 7a 6f 6e 65 28 29 3b 0d 0a 20 20 7d 65 6c 73 65  zone();..  }else
a55a0 7b 0d 0a 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 31  {..    /* only 1
a55b0 20 63 6f 72 65 2c 20 75 73 65 20 6f 75 72 20 6f   core, use our o
a55c0 77 6e 20 7a 6f 6e 65 20 74 6f 20 63 6f 6e 74 65  wn zone to conte
a55d0 6e 74 69 6f 6e 20 6f 76 65 72 20 67 6c 6f 62 61  ntion over globa
a55e0 6c 20 6c 6f 63 6b 73 2c 20 0d 0a 20 20 20 20 2a  l locks, ..    *
a55f0 2a 20 65 2e 67 2e 20 77 65 20 68 61 76 65 20 6f  * e.g. we have o
a5600 75 72 20 6f 77 6e 20 64 65 64 69 63 61 74 65 64  ur own dedicated
a5610 20 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 20 20 62   locks */..    b
a5620 6f 6f 6c 20 73 75 63 63 65 73 73 3b 09 09 0d 0a  ool success;....
a5630 20 20 20 20 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f      malloc_zone_
a5640 74 2a 20 6e 65 77 7a 6f 6e 65 20 3d 20 6d 61 6c  t* newzone = mal
a5650 6c 6f 63 5f 63 72 65 61 74 65 5f 7a 6f 6e 65 28  loc_create_zone(
a5660 34 30 39 36 2c 20 30 29 3b 0d 0a 20 20 20 20 6d  4096, 0);..    m
a5670 61 6c 6c 6f 63 5f 73 65 74 5f 7a 6f 6e 65 5f 6e  alloc_set_zone_n
a5680 61 6d 65 28 6e 65 77 7a 6f 6e 65 2c 20 22 53 71  ame(newzone, "Sq
a5690 6c 69 74 65 5f 48 65 61 70 22 29 3b 0d 0a 20 20  lite_Heap");..  
a56a0 20 20 64 6f 7b 0d 0a 20 20 20 20 20 20 73 75 63    do{..      suc
a56b0 63 65 73 73 20 3d 20 4f 53 41 74 6f 6d 69 63 43  cess = OSAtomicC
a56c0 6f 6d 70 61 72 65 41 6e 64 53 77 61 70 50 74 72  ompareAndSwapPtr
a56d0 42 61 72 72 69 65 72 28 4e 55 4c 4c 2c 20 6e 65  Barrier(NULL, ne
a56e0 77 7a 6f 6e 65 2c 20 0d 0a 20 20 20 20 20 20 20  wzone, ..       
a56f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5700 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 20            (void 
a5710 2a 20 76 6f 6c 61 74 69 6c 65 20 2a 29 26 5f 73  * volatile *)&_s
a5720 71 6c 69 74 65 5a 6f 6e 65 5f 29 3b 0d 0a 20 20  qliteZone_);..  
a5730 20 20 7d 77 68 69 6c 65 28 21 5f 73 71 6c 69 74    }while(!_sqlit
a5740 65 5a 6f 6e 65 5f 29 3b 0d 0a 20 20 20 20 69 66  eZone_);..    if
a5750 28 20 21 73 75 63 63 65 73 73 20 29 7b 09 0d 0a  ( !success ){...
a5760 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 62 6f 64        /* somebod
a5770 79 20 72 65 67 69 73 74 65 72 65 64 20 61 20 7a  y registered a z
a5780 6f 6e 65 20 66 69 72 73 74 20 2a 2f 0d 0a 20 20  one first */..  
a5790 20 20 20 20 6d 61 6c 6c 6f 63 5f 64 65 73 74 72      malloc_destr
a57a0 6f 79 5f 7a 6f 6e 65 28 6e 65 77 7a 6f 6e 65 29  oy_zone(newzone)
a57b0 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23  ;..    }..  }..#
a57c0 65 6e 64 69 66 0d 0a 20 20 55 4e 55 53 45 44 5f  endif..  UNUSED_
a57d0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
a57e0 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  d);..  return SQ
a57f0 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
a5800 2a 0d 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69  *..** Deinitiali
a5810 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0d  ze this module..
a5820 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
a5830 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
a5840 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  own(void *NotUse
a5850 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  d){..  UNUSED_PA
a5860 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
a5870 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d 0a 7d 0d  ;..  return;..}.
a5880 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  .../*..** This r
a5890 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
a58a0 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
a58b0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
a58c0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0d 0a  ernal linkage...
a58d0 2a 2a 0d 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  **..** Populate 
a58e0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
a58f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
a5900 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
a5910 73 20 69 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  s in..** sqlite3
a5920 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77  GlobalConfig.m w
a5930 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
a5940 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  the routines in 
a5950 74 68 69 73 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a  this file...*/..
a5960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a5970 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65  oid sqlite3MemSe
a5980 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0d  tDefault(void){.
a5990 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
a59a0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
a59b0 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
a59c0 64 73 20 3d 20 7b 0d 0a 20 20 20 20 20 73 71 6c  ds = {..     sql
a59d0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0d 0a  ite3MemMalloc,..
a59e0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46       sqlite3MemF
a59f0 72 65 65 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74  ree,..     sqlit
a5a00 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0d 0a 20  e3MemRealloc,.. 
a5a10 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69      sqlite3MemSi
a5a20 7a 65 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65  ze,..     sqlite
a5a30 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0d 0a 20 20  3MemRoundup,..  
a5a40 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
a5a50 74 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65 33  t,..     sqlite3
a5a60 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0d 0a 20 20  MemShutdown,..  
a5a70 20 20 20 30 0d 0a 20 20 7d 3b 0d 0a 20 20 73 71     0..  };..  sq
a5a80 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c  lite3_config(SQL
a5a90 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
a5aa0 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f  C, &defaultMetho
a5ab0 64 73 29 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69  ds);..}....#endi
a5ac0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 54  f /* SQLITE_SYST
a5ad0 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0d 0a 0d 0a  EM_MALLOC */....
a5ae0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a5af0 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a  End of mem1.c **
a5b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
a5b30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a5b40 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32   Begin file mem2
a5b50 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a5b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a5b80 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 37 20 41 75  ../*..** 2007 Au
a5b90 67 75 73 74 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20  gust 15..**..** 
a5ba0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
a5bb0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
a5bc0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
a5bd0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
a5be0 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  ..** a legal not
a5bf0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
a5c00 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  lessing:..**..**
a5c10 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
a5c20 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
a5c30 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
a5c40 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
a5c50 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
a5c60 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
a5c70 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  s...**    May yo
a5c80 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
a5c90 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
a5ca0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
a5cb0 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
a5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
a5d00 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .**..** This fil
a5d10 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c  e contains low-l
a5d20 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  evel memory allo
a5d30 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66  cation drivers f
a5d40 6f 72 20 77 68 65 6e 0d 0a 2a 2a 20 53 51 4c 69  or when..** SQLi
a5d50 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20  te will use the 
a5d60 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61  standard C-libra
a5d70 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f  ry malloc/reallo
a5d80 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 65  c/free interface
a5d90 0d 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74  ..** to obtain t
a5da0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65  he memory it nee
a5db0 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 20  ds while adding 
a5dc0 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f 6e  lots of addition
a5dd0 61 6c 20 64 65 62 75 67 67 69 6e 67 0d 0a 2a 2a  al debugging..**
a5de0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
a5df0 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  each allocation 
a5e00 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70  in order to help
a5e10 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20   detect and fix 
a5e20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 6c 65 61 6b 73  memory..** leaks
a5e30 20 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67   and memory usag
a5e40 65 20 65 72 72 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a  e errors...**..*
a5e50 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
a5e60 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ains implementat
a5e70 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d  ions of the low-
a5e80 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
a5e90 6f 63 61 74 69 6f 6e 0d 0a 2a 2a 20 72 6f 75 74  ocation..** rout
a5ea0 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69  ines specified i
a5eb0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65  n the sqlite3_me
a5ec0 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  m_methods object
a5ed0 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
a5ee0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
a5ef0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
a5f00 61 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c  ator is used onl
a5f10 79 20 69 66 20 74 68 65 0d 0a 2a 2a 20 53 51 4c  y if the..** SQL
a5f20 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63  ITE_MEMDEBUG mac
a5f30 72 6f 20 69 73 20 64 65 66 69 6e 65 64 0d 0a 2a  ro is defined..*
a5f40 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
a5f50 5f 4d 45 4d 44 45 42 55 47 0d 0a 0d 0a 2f 2a 0d  _MEMDEBUG..../*.
a5f60 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63  .** The backtrac
a5f70 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20  e functionality 
a5f80 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
a5f90 65 20 77 69 74 68 20 47 4c 49 42 43 0d 0a 2a 2f  e with GLIBC..*/
a5fa0 0d 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43  ..#ifdef __GLIBC
a5fb0 5f 5f 0d 0a 20 20 65 78 74 65 72 6e 20 69 6e 74  __..  extern int
a5fc0 20 62 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a   backtrace(void*
a5fd0 2a 2c 69 6e 74 29 3b 0d 0a 20 20 65 78 74 65 72  *,int);..  exter
a5fe0 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 61 63 65  n void backtrace
a5ff0 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64  _symbols_fd(void
a6000 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29  *const*,int,int)
a6010 3b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  ;..#else..# defi
a6020 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c 42  ne backtrace(A,B
a6030 29 20 31 0d 0a 23 20 64 65 66 69 6e 65 20 62 61  ) 1..# define ba
a6040 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f  cktrace_symbols_
a6050 66 64 28 41 2c 42 2c 43 29 0d 0a 23 65 6e 64 69  fd(A,B,C)..#endi
a6060 66 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  f../* #include <
a6070 73 74 64 69 6f 2e 68 3e 20 2a 2f 0d 0a 0d 0a 2f  stdio.h> */..../
a6080 2a 0d 0a 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72  *..** Each memor
a6090 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f  y allocation loo
a60a0 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 2a  ks like this:..*
a60b0 2a 0d 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  *..**  ---------
a60c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a60d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a60e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a60f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d  ---------------.
a6100 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20  .**  | Title |  
a6110 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65  backtrace pointe
a6120 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64  rs |  MemBlockHd
a6130 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  r |  allocation 
a6140 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0d 0a 2a  |  EndGuard |..*
a6150 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
a6160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6170 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6180 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6190 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d  -----------..**.
a61a0 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
a61b0 69 6f 6e 20 63 6f 64 65 20 73 65 65 73 20 6f 6e  ion code sees on
a61c0 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ly a pointer to 
a61d0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  the allocation. 
a61e0 20 57 65 20 68 61 76 65 0d 0a 2a 2a 20 74 6f 20   We have..** to 
a61f0 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 74 68 65  back up from the
a6200 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f 69 6e   allocation poin
a6210 74 65 72 20 74 6f 20 66 69 6e 64 20 74 68 65 20  ter to find the 
a6220 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 20 54 68  MemBlockHdr.  Th
a6230 65 0d 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64  e..** MemBlockHd
a6240 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 20 73  r tells us the s
a6250 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
a6260 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 6e 75  ation and the nu
a6270 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 62 61 63 6b  mber of..** back
a6280 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e 20  trace pointers. 
a6290 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61   There is also a
a62a0 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 74   guard word at t
a62b0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0d 0a 2a  he end of the..*
a62c0 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0d 0a  * MemBlockHdr...
a62d0 2a 2f 0d 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c  */..struct MemBl
a62e0 6f 63 6b 48 64 72 20 7b 0d 0a 20 20 69 36 34 20  ockHdr {..  i64 
a62f0 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  iSize;          
a6300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6310 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20  /* Size of this 
a6320 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a 20  allocation */.. 
a6330 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
a6340 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  Hdr *pNext, *pPr
a6350 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c  ev;  /* Linked l
a6360 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  ist of all unfre
a6370 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20  ed memory */..  
a6380 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 3b  char nBacktrace;
a6390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63a0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
a63b0 20 62 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 74   backtraces on t
a63c0 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0d 0a 20 20  his alloc */..  
a63d0 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 53  char nBacktraceS
a63e0 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 20 20  lots;           
a63f0 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65      /* Available
a6400 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f 74 73   backtrace slots
a6410 20 2a 2f 0d 0a 20 20 75 38 20 6e 54 69 74 6c 65   */..  u8 nTitle
a6420 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a6430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
a6440 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 6e  tes of title; in
a6450 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0d 0a  cludes '\0' */..
a6460 20 20 75 38 20 65 54 79 70 65 3b 20 20 20 20 20    u8 eType;     
a6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6480 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
a6490 69 6f 6e 20 74 79 70 65 20 63 6f 64 65 20 2a 2f  ion type code */
a64a0 0d 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61  ..  int iForeGua
a64b0 72 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rd;             
a64c0 20 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64          /* Guard
a64d0 20 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79   word for sanity
a64e0 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
a64f0 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0d 0a 2a  * Guard words..*
a6500 2f 0d 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47  /..#define FOREG
a6510 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0d  UARD 0x80F5E153.
a6520 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41  .#define REARGUA
a6530 52 44 20 30 78 45 34 36 37 36 42 35 33 0d 0a 0d  RD 0xE4676B53...
a6540 0a 2f 2a 0d 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f  ./*..** Number o
a6550 66 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e  f malloc size in
a6560 63 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63  crements to trac
a6570 6b 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  k...*/..#define 
a6580 4e 43 53 49 5a 45 20 20 31 30 30 30 0d 0a 0d 0a  NCSIZE  1000....
a6590 2f 2a 0d 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  /*..** All of th
a65a0 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
a65b0 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
a65c0 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
a65d0 63 74 65 64 0d 0a 2a 2a 20 69 6e 74 6f 20 61 20  cted..** into a 
a65e0 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65  single structure
a65f0 20 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54   named "mem".  T
a6600 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74  his is to keep t
a6610 68 65 0d 0a 2a 2a 20 73 74 61 74 69 63 20 76 61  he..** static va
a6620 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65  riables organize
a6630 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20  d and to reduce 
a6640 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74  namespace pollut
a6650 69 6f 6e 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 69  ion..** when thi
a6660 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62  s module is comb
a6670 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20  ined with other 
a6680 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  in the amalgamat
a6690 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
a66a0 20 73 74 72 75 63 74 20 7b 0d 0a 20 20 0d 0a 20   struct {..  .. 
a66b0 20 2f 2a 0d 0a 20 20 2a 2a 20 4d 75 74 65 78 20   /*..  ** Mutex 
a66c0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
a66d0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
a66e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
a66f0 73 74 65 6d 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73  stem...  */..  s
a6700 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
a6710 74 65 78 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20  tex;....  /*..  
a6720 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 69 6c  ** Head and tail
a6730 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
a6740 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e  t of all outstan
a6750 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  ding allocations
a6760 0d 0a 20 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  ..  */..  struct
a6770 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46   MemBlockHdr *pF
a6780 69 72 73 74 3b 0d 0a 20 20 73 74 72 75 63 74 20  irst;..  struct 
a6790 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61  MemBlockHdr *pLa
a67a0 73 74 3b 0d 0a 20 20 0d 0a 20 20 2f 2a 0d 0a 20  st;..  ..  /*.. 
a67b0 20 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   ** The number o
a67c0 66 20 6c 65 76 65 6c 73 20 6f 66 20 62 61 63 6b  f levels of back
a67d0 74 72 61 63 65 20 74 6f 20 73 61 76 65 20 69 6e  trace to save in
a67e0 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   new allocations
a67f0 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ...  */..  int n
a6800 42 61 63 6b 74 72 61 63 65 3b 0d 0a 20 20 76 6f  Backtrace;..  vo
a6810 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29  id (*xBacktrace)
a6820 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20  (int, int, void 
a6830 2a 2a 29 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20  **);....  /*..  
a6840 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f  ** Title text to
a6850 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74   insert in front
a6860 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0d 0a   of each block..
a6870 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 69 74    */..  int nTit
a6880 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79  le;        /* By
a6890 74 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f  tes of zTitle to
a68a0 20 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73   save.  Includes
a68b0 20 27 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e   '\0' and paddin
a68c0 67 20 2a 2f 0d 0a 20 20 63 68 61 72 20 7a 54 69  g */..  char zTi
a68d0 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68  tle[100];  /* Th
a68e0 65 20 74 69 74 6c 65 20 74 65 78 74 20 2a 2f 0d  e title text */.
a68f0 0a 0d 0a 20 20 2f 2a 20 0d 0a 20 20 2a 2a 20 73  ...  /* ..  ** s
a6900 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61  qlite3MallocDisa
a6910 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74  llow() increment
a6920 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
a6930 63 6f 75 6e 74 65 72 2e 0d 0a 20 20 2a 2a 20 73  counter...  ** s
a6940 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
a6950 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69  w() decrements i
a6960 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20  t...  */..  int 
a6970 64 69 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20  disallow; /* Do 
a6980 6e 6f 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79  not allow memory
a6990 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a   allocation */..
a69a0 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 47 61 74  ..  /*..  ** Gat
a69b0 68 65 72 20 73 74 61 74 69 73 74 69 63 73 20 6f  her statistics o
a69c0 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 20 6d  n the sizes of m
a69d0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
a69e0 73 2e 0d 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 5b  s...  ** nAlloc[
a69f0 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  i] is the number
a6a00 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61   of allocation a
a6a10 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0d 0a  ttempts of i*8..
a6a20 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d    ** bytes.  i==
a6a30 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75  NCSIZE is the nu
a6a40 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
a6a50 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0d  on attempts for.
a6a60 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65  .  ** sizes more
a6a70 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62   than NCSIZE*8 b
a6a80 79 74 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ytes...  */..  i
a6a90 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  nt nAlloc[NCSIZE
a6aa0 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c  ];      /* Total
a6ab0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
a6ac0 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74  ations */..  int
a6ad0 20 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45   nCurrent[NCSIZE
a6ae0 5d 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74  ];    /* Current
a6af0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
a6b00 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74  ations */..  int
a6b10 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a   mxCurrent[NCSIZ
a6b20 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 74  E];   /* Highwat
a6b30 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72  er mark for nCur
a6b40 72 65 6e 74 20 2a 2f 0d 0a 0d 0a 7d 20 6d 65 6d  rent */....} mem
a6b50 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64  ;....../*..** Ad
a6b60 6a 75 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67  just memory usag
a6b70 65 20 73 74 61 74 69 73 74 69 63 73 0d 0a 2a 2f  e statistics..*/
a6b80 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64  ..static void ad
a6b90 6a 75 73 74 53 74 61 74 73 28 69 6e 74 20 69 53  justStats(int iS
a6ba0 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 65 6d 65  ize, int increme
a6bb0 6e 74 29 7b 0d 0a 20 20 69 6e 74 20 69 20 3d 20  nt){..  int i = 
a6bc0 52 4f 55 4e 44 38 28 69 53 69 7a 65 29 2f 38 3b  ROUND8(iSize)/8;
a6bd0 0d 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 5a 45  ..  if( i>NCSIZE
a6be0 2d 31 20 29 7b 0d 0a 20 20 20 20 69 20 3d 20 4e  -1 ){..    i = N
a6bf0 43 53 49 5a 45 20 2d 20 31 3b 0d 0a 20 20 7d 0d  CSIZE - 1;..  }.
a6c00 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74  .  if( increment
a6c10 3e 30 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 2e 6e  >0 ){..    mem.n
a6c20 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0d 0a 20 20 20  Alloc[i]++;..   
a6c30 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
a6c40 2b 2b 3b 0d 0a 20 20 20 20 69 66 28 20 6d 65 6d  ++;..    if( mem
a6c50 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d  .nCurrent[i]>mem
a6c60 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b  .mxCurrent[i] ){
a6c70 0d 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75  ..      mem.mxCu
a6c80 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e  rrent[i] = mem.n
a6c90 43 75 72 72 65 6e 74 5b 69 5d 3b 0d 0a 20 20 20  Current[i];..   
a6ca0 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20   }..  }else{..  
a6cb0 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69    mem.nCurrent[i
a6cc0 5d 2d 2d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  ]--;..    assert
a6cd0 28 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69  ( mem.nCurrent[i
a6ce0 5d 3e 3d 30 20 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  ]>=0 );..  }..}.
a6cf0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65 6e 20  .../*..** Given 
a6d00 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 66  an allocation, f
a6d10 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b  ind the MemBlock
a6d20 48 64 72 20 66 6f 72 20 74 68 61 74 20 61 6c 6c  Hdr for that all
a6d30 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a  ocation...**..**
a6d40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
a6d50 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20  ecks the guards 
a6d60 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66  at either end of
a6d70 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
a6d80 61 6e 64 0d 0a 2a 2a 20 69 66 20 74 68 65 79 20  and..** if they 
a6d90 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74  are incorrect it
a6da0 20 61 73 73 65 72 74 73 2e 0d 0a 2a 2f 0d 0a 73   asserts...*/..s
a6db0 74 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d  tatic struct Mem
a6dc0 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65  BlockHdr *sqlite
a6dd0 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72  3MemsysGetHeader
a6de0 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69  (void *pAllocati
a6df0 6f 6e 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 4d  on){..  struct M
a6e00 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0d 0a  emBlockHdr *p;..
a6e10 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0d 0a 20 20    int *pInt;..  
a6e20 75 38 20 2a 70 55 38 3b 0d 0a 20 20 69 6e 74 20  u8 *pU8;..  int 
a6e30 6e 52 65 73 65 72 76 65 3b 0d 0a 0d 0a 20 20 70  nReserve;....  p
a6e40 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c   = (struct MemBl
a6e50 6f 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74  ockHdr*)pAllocat
a6e60 69 6f 6e 3b 0d 0a 20 20 70 2d 2d 3b 0d 0a 20 20  ion;..  p--;..  
a6e70 61 73 73 65 72 74 28 20 70 2d 3e 69 46 6f 72 65  assert( p->iFore
a6e80 47 75 61 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45  Guard==(int)FORE
a6e90 47 55 41 52 44 20 29 3b 0d 0a 20 20 6e 52 65 73  GUARD );..  nRes
a6ea0 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 70 2d  erve = ROUND8(p-
a6eb0 3e 69 53 69 7a 65 29 3b 0d 0a 20 20 70 49 6e 74  >iSize);..  pInt
a6ec0 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61   = (int*)pAlloca
a6ed0 74 69 6f 6e 3b 0d 0a 20 20 70 55 38 20 3d 20 28  tion;..  pU8 = (
a6ee0 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  u8*)pAllocation;
a6ef0 0d 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 74  ..  assert( pInt
a6f00 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66  [nReserve/sizeof
a6f10 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 41  (int)]==(int)REA
a6f20 52 47 55 41 52 44 20 29 3b 0d 0a 20 20 2f 2a 20  RGUARD );..  /* 
a6f30 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79 20  This checks any 
a6f40 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20 62  of the "extra" b
a6f50 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 64  ytes allocated d
a6f60 75 65 0d 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e  ue..  ** to roun
a6f70 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20  ding up to an 8 
a6f80 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f  byte boundary to
a6f90 20 65 6e 73 75 72 65 20 0d 0a 20 20 2a 2a 20 74   ensure ..  ** t
a6fa0 68 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e  hey haven't been
a6fb0 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0d 0a 20   overwritten... 
a6fc0 20 2a 2f 0d 0a 20 20 77 68 69 6c 65 28 20 6e 52   */..  while( nR
a6fd0 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53  eserve-- > p->iS
a6fe0 69 7a 65 20 29 20 61 73 73 65 72 74 28 20 70 55  ize ) assert( pU
a6ff0 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d 30 78 36  8[nReserve]==0x6
a7000 35 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70  5 );..  return p
a7010 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
a7020 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
a7030 20 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e   of bytes curren
a7040 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74  tly allocated at
a7050 20 61 64 64 72 65 73 73 20 70 2e 0d 0a 2a 2f 0d   address p...*/.
a7060 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
a7070 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20  te3MemSize(void 
a7080 2a 70 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 4d  *p){..  struct M
a7090 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
a70a0 3b 0d 0a 20 20 69 66 28 20 21 70 20 29 7b 0d 0a  ;..  if( !p ){..
a70b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
a70c0 20 7d 0d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c   }..  pHdr = sql
a70d0 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61  ite3MemsysGetHea
a70e0 64 65 72 28 70 29 3b 0d 0a 20 20 72 65 74 75 72  der(p);..  retur
a70f0 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0d 0a  n pHdr->iSize;..
a7100 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74  }..../*..** Init
a7110 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
a7120 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
a7130 73 79 73 74 65 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61  system...*/..sta
a7140 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
a7150 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74  emInit(void *Not
a7160 55 73 65 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44  Used){..  UNUSED
a7170 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
a7180 65 64 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ed);..  assert( 
a7190 28 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d  (sizeof(struct M
a71a0 65 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d  emBlockHdr)&7) =
a71b0 3d 20 30 20 29 3b 0d 0a 20 20 69 66 28 20 21 73  = 0 );..  if( !s
a71c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a71d0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0d 0a  ig.bMemstat ){..
a71e0 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79      /* If memory
a71f0 20 73 74 61 74 75 73 20 69 73 20 65 6e 61 62 6c   status is enabl
a7200 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 6c  ed, then the mal
a7210 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 20 77 69  loc.c wrapper wi
a7220 6c 6c 20 61 6c 72 65 61 64 79 0d 0a 20 20 20 20  ll already..    
a7230 2a 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 41 54  ** hold the STAT
a7240 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 68 65  IC_MEM mutex whe
a7250 6e 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 68  n the routines h
a7260 65 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e  ere are invoked.
a7270 20 2a 2f 0d 0a 20 20 20 20 6d 65 6d 2e 6d 75 74   */..    mem.mut
a7280 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
a7290 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
a72a0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
a72b0 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
a72c0 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
a72d0 0a 2f 2a 0d 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ./*..** Deinitia
a72e0 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
a72f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
a7300 73 74 65 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  stem...*/..stati
a7310 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
a7320 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a  mShutdown(void *
a7330 4e 6f 74 55 73 65 64 29 7b 0d 0a 20 20 55 4e 55  NotUsed){..  UNU
a7340 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
a7350 74 55 73 65 64 29 3b 0d 0a 20 20 6d 65 6d 2e 6d  tUsed);..  mem.m
a7360 75 74 65 78 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a  utex = 0;..}....
a7370 2f 2a 0d 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  /*..** Round up 
a7380 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
a7390 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
a73a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
a73b0 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
a73c0 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
a73d0 64 75 70 28 69 6e 74 20 6e 29 7b 0d 0a 20 20 72  dup(int n){..  r
a73e0 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b  eturn ROUND8(n);
a73f0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69  ..}..../*..** Fi
a7400 6c 6c 20 61 20 62 75 66 66 65 72 20 77 69 74 68  ll a buffer with
a7410 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 62   pseudo-random b
a7420 79 74 65 73 2e 20 20 54 68 69 73 20 69 73 20 75  ytes.  This is u
a7430 73 65 64 20 74 6f 20 70 72 65 73 65 74 0d 0a 2a  sed to preset..*
a7440 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
a7450 20 61 20 6e 65 77 20 6d 65 6d 6f 72 79 20 61 6c   a new memory al
a7460 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 75 6e 70 72  location to unpr
a7470 65 64 69 63 74 61 62 6c 65 20 76 61 6c 75 65 73  edictable values
a7480 20 61 6e 64 0d 0a 2a 2a 20 74 6f 20 63 6c 65 61   and..** to clea
a7490 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  r the content of
a74a0 20 61 20 66 72 65 65 64 20 61 6c 6c 6f 63 61 74   a freed allocat
a74b0 69 6f 6e 20 74 6f 20 75 6e 70 72 65 64 69 63 74  ion to unpredict
a74c0 61 62 6c 65 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f  able values...*/
a74d0 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 61  ..static void ra
a74e0 6e 64 6f 6d 46 69 6c 6c 28 63 68 61 72 20 2a 70  ndomFill(char *p
a74f0 42 75 66 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  Buf, int nByte){
a7500 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
a7510 20 78 2c 20 79 2c 20 72 3b 0d 0a 20 20 78 20 3d   x, y, r;..  x =
a7520 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
a7530 4e 54 28 70 42 75 66 29 3b 0d 0a 20 20 79 20 3d  NT(pBuf);..  y =
a7540 20 6e 42 79 74 65 20 7c 20 31 3b 0d 0a 20 20 77   nByte | 1;..  w
a7550 68 69 6c 65 28 20 6e 42 79 74 65 20 3e 3d 20 34  hile( nByte >= 4
a7560 20 29 7b 0d 0a 20 20 20 20 78 20 3d 20 28 78 3e   ){..    x = (x>
a7570 3e 31 29 20 5e 20 28 2d 28 78 26 31 29 20 26 20  >1) ^ (-(x&1) & 
a7580 30 78 64 30 30 30 30 30 30 31 29 3b 0d 0a 20 20  0xd0000001);..  
a7590 20 20 79 20 3d 20 79 2a 31 31 30 33 35 31 35 32    y = y*11035152
a75a0 34 35 20 2b 20 31 32 33 34 35 3b 0d 0a 20 20 20  45 + 12345;..   
a75b0 20 72 20 3d 20 78 20 5e 20 79 3b 0d 0a 20 20 20   r = x ^ y;..   
a75c0 20 2a 28 69 6e 74 2a 29 70 42 75 66 20 3d 20 72   *(int*)pBuf = r
a75d0 3b 0d 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 34  ;..    pBuf += 4
a75e0 3b 0d 0a 20 20 20 20 6e 42 79 74 65 20 2d 3d 20  ;..    nByte -= 
a75f0 34 3b 0d 0a 20 20 7d 0d 0a 20 20 77 68 69 6c 65  4;..  }..  while
a7600 28 20 6e 42 79 74 65 2d 2d 20 3e 20 30 20 29 7b  ( nByte-- > 0 ){
a7610 0d 0a 20 20 20 20 78 20 3d 20 28 78 3e 3e 31 29  ..    x = (x>>1)
a7620 20 5e 20 28 2d 28 78 26 31 29 20 26 20 30 78 64   ^ (-(x&1) & 0xd
a7630 30 30 30 30 30 30 31 29 3b 0d 0a 20 20 20 20 79  0000001);..    y
a7640 20 3d 20 79 2a 31 31 30 33 35 31 35 32 34 35 20   = y*1103515245 
a7650 2b 20 31 32 33 34 35 3b 0d 0a 20 20 20 20 72 20  + 12345;..    r 
a7660 3d 20 78 20 5e 20 79 3b 0d 0a 20 20 20 20 2a 28  = x ^ y;..    *(
a7670 70 42 75 66 2b 2b 29 20 3d 20 72 20 26 20 30 78  pBuf++) = r & 0x
a7680 66 66 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  ff;..  }..}..../
a7690 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e  *..** Allocate n
a76a0 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65  Byte bytes of me
a76b0 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  mory...*/..stati
a76c0 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  c void *sqlite3M
a76d0 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  emMalloc(int nBy
a76e0 74 65 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 4d  te){..  struct M
a76f0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
a7700 3b 0d 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b  ;..  void **pBt;
a7710 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20  ..  char *z;..  
a7720 69 6e 74 20 2a 70 49 6e 74 3b 0d 0a 20 20 76 6f  int *pInt;..  vo
a7730 69 64 20 2a 70 20 3d 20 30 3b 0d 0a 20 20 69 6e  id *p = 0;..  in
a7740 74 20 74 6f 74 61 6c 53 69 7a 65 3b 0d 0a 20 20  t totalSize;..  
a7750 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0d 0a 20  int nReserve;.. 
a7760 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
a7770 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b  nter(mem.mutex);
a7780 0d 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e  ..  assert( mem.
a7790 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0d 0a  disallow==0 );..
a77a0 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55    nReserve = ROU
a77b0 4e 44 38 28 6e 42 79 74 65 29 3b 0d 0a 20 20 74  ND8(nByte);..  t
a77c0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65  otalSize = nRese
a77d0 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48  rve + sizeof(*pH
a77e0 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74  dr) + sizeof(int
a77f0 29 20 2b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ) +..           
a7800 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61      mem.nBacktra
a7810 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  ce*sizeof(void*)
a7820 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0d 0a   + mem.nTitle;..
a7830 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74    p = malloc(tot
a7840 61 6c 53 69 7a 65 29 3b 0d 0a 20 20 69 66 28 20  alSize);..  if( 
a7850 70 20 29 7b 0d 0a 20 20 20 20 7a 20 3d 20 70 3b  p ){..    z = p;
a7860 0d 0a 20 20 20 20 70 42 74 20 3d 20 28 76 6f 69  ..    pBt = (voi
a7870 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c  d**)&z[mem.nTitl
a7880 65 5d 3b 0d 0a 20 20 20 20 70 48 64 72 20 3d 20  e];..    pHdr = 
a7890 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b  (struct MemBlock
a78a0 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e 42  Hdr*)&pBt[mem.nB
a78b0 61 63 6b 74 72 61 63 65 5d 3b 0d 0a 20 20 20 20  acktrace];..    
a78c0 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pHdr->pNext = 0;
a78d0 0d 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65  ..    pHdr->pPre
a78e0 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0d 0a  v = mem.pLast;..
a78f0 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73      if( mem.pLas
a7900 74 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 2e  t ){..      mem.
a7910 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70  pLast->pNext = p
a7920 48 64 72 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  Hdr;..    }else{
a7930 0d 0a 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72  ..      mem.pFir
a7940 73 74 20 3d 20 70 48 64 72 3b 0d 0a 20 20 20 20  st = pHdr;..    
a7950 7d 0d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74  }..    mem.pLast
a7960 20 3d 20 70 48 64 72 3b 0d 0a 20 20 20 20 70 48   = pHdr;..    pH
a7970 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 3d  dr->iForeGuard =
a7980 20 46 4f 52 45 47 55 41 52 44 3b 0d 0a 20 20 20   FOREGUARD;..   
a7990 20 70 48 64 72 2d 3e 65 54 79 70 65 20 3d 20 4d   pHdr->eType = M
a79a0 45 4d 54 59 50 45 5f 48 45 41 50 3b 0d 0a 20 20  EMTYPE_HEAP;..  
a79b0 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61    pHdr->nBacktra
a79c0 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42  ceSlots = mem.nB
a79d0 61 63 6b 74 72 61 63 65 3b 0d 0a 20 20 20 20 70  acktrace;..    p
a79e0 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65  Hdr->nTitle = me
a79f0 6d 2e 6e 54 69 74 6c 65 3b 0d 0a 20 20 20 20 69  m.nTitle;..    i
a7a00 66 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63  f( mem.nBacktrac
a7a10 65 20 29 7b 0d 0a 20 20 20 20 20 20 76 6f 69 64  e ){..      void
a7a20 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0d 0a 20 20   *aAddr[40];..  
a7a30 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74      pHdr->nBackt
a7a40 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61 63 65  race = backtrace
a7a50 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 63  (aAddr, mem.nBac
a7a60 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0d 0a 20 20  ktrace+1)-1;..  
a7a70 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c 20      memcpy(pBt, 
a7a80 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 2d  &aAddr[1], pHdr-
a7a90 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65  >nBacktrace*size
a7aa0 6f 66 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 20  of(void*));..   
a7ab0 20 20 20 61 73 73 65 72 74 28 70 42 74 5b 30 5d     assert(pBt[0]
a7ac0 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6d 65  );..      if( me
a7ad0 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29 7b 0d  m.xBacktrace ){.
a7ae0 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42 61  .        mem.xBa
a7af0 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20 70  cktrace(nByte, p
a7b00 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d  Hdr->nBacktrace-
a7b10 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0d 0a  1, &aAddr[1]);..
a7b20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c        }..    }el
a7b30 73 65 7b 0d 0a 20 20 20 20 20 20 70 48 64 72 2d  se{..      pHdr-
a7b40 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b  >nBacktrace = 0;
a7b50 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
a7b60 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0d 0a   mem.nTitle ){..
a7b70 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20        memcpy(z, 
a7b80 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e  mem.zTitle, mem.
a7b90 6e 54 69 74 6c 65 29 3b 0d 0a 20 20 20 20 7d 0d  nTitle);..    }.
a7ba0 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65  .    pHdr->iSize
a7bb0 20 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 61   = nByte;..    a
a7bc0 64 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65  djustStats(nByte
a7bd0 2c 20 2b 31 29 3b 0d 0a 20 20 20 20 70 49 6e 74  , +1);..    pInt
a7be0 20 3d 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31   = (int*)&pHdr[1
a7bf0 5d 3b 0d 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65  ];..    pInt[nRe
a7c00 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74  serve/sizeof(int
a7c10 29 5d 20 3d 20 52 45 41 52 47 55 41 52 44 3b 0d  )] = REARGUARD;.
a7c20 0a 20 20 20 20 72 61 6e 64 6f 6d 46 69 6c 6c 28  .    randomFill(
a7c30 28 63 68 61 72 2a 29 70 49 6e 74 2c 20 6e 42 79  (char*)pInt, nBy
a7c40 74 65 29 3b 0d 0a 20 20 20 20 6d 65 6d 73 65 74  te);..    memset
a7c50 28 28 28 63 68 61 72 2a 29 70 49 6e 74 29 2b 6e  (((char*)pInt)+n
a7c60 42 79 74 65 2c 20 30 78 36 35 2c 20 6e 52 65 73  Byte, 0x65, nRes
a7c70 65 72 76 65 2d 6e 42 79 74 65 29 3b 0d 0a 20 20  erve-nByte);..  
a7c80 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e    p = (void*)pIn
a7c90 74 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  t;..  }..  sqlit
a7ca0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a7cb0 65 6d 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  em.mutex);..  re
a7cc0 74 75 72 6e 20 70 3b 20 0d 0a 7d 0d 0a 0d 0a 2f  turn p; ..}..../
a7cd0 2a 0d 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  *..** Free memor
a7ce0 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  y...*/..static v
a7cf0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72  oid sqlite3MemFr
a7d00 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ee(void *pPrior)
a7d10 7b 0d 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  {..  struct MemB
a7d20 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0d 0a  lockHdr *pHdr;..
a7d30 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0d 0a 20    void **pBt;.. 
a7d40 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 61 73 73   char *z;..  ass
a7d50 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62  ert( sqlite3Glob
a7d60 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
a7d70 74 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  t || sqlite3Glob
a7d80 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
a7d90 74 65 78 3d 3d 30 20 0d 0a 20 20 20 20 20 20 20  tex==0 ..       
a7da0 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20  || mem.mutex!=0 
a7db0 29 3b 0d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c  );..  pHdr = sql
a7dc0 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61  ite3MemsysGetHea
a7dd0 64 65 72 28 70 50 72 69 6f 72 29 3b 0d 0a 20 20  der(pPrior);..  
a7de0 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48  pBt = (void**)pH
a7df0 64 72 3b 0d 0a 20 20 70 42 74 20 2d 3d 20 70 48  dr;..  pBt -= pH
a7e00 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
a7e10 6f 74 73 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ots;..  sqlite3_
a7e20 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
a7e30 6d 75 74 65 78 29 3b 0d 0a 20 20 69 66 28 20 70  mutex);..  if( p
a7e40 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0d 0a 20  Hdr->pPrev ){.. 
a7e50 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
a7e60 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70  >pPrev->pNext==p
a7e70 48 64 72 20 29 3b 0d 0a 20 20 20 20 70 48 64 72  Hdr );..    pHdr
a7e80 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
a7e90 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0d 0a 20   pHdr->pNext;.. 
a7ea0 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73   }else{..    ass
a7eb0 65 72 74 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d  ert( mem.pFirst=
a7ec0 3d 70 48 64 72 20 29 3b 0d 0a 20 20 20 20 6d 65  =pHdr );..    me
a7ed0 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 2d  m.pFirst = pHdr-
a7ee0 3e 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20  >pNext;..  }..  
a7ef0 69 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 20  if( pHdr->pNext 
a7f00 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
a7f10 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  pHdr->pNext->pPr
a7f20 65 76 3d 3d 70 48 64 72 20 29 3b 0d 0a 20 20 20  ev==pHdr );..   
a7f30 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50   pHdr->pNext->pP
a7f40 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72 65  rev = pHdr->pPre
a7f50 76 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  v;..  }else{..  
a7f60 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c    assert( mem.pL
a7f70 61 73 74 3d 3d 70 48 64 72 20 29 3b 0d 0a 20 20  ast==pHdr );..  
a7f80 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48    mem.pLast = pH
a7f90 64 72 2d 3e 70 50 72 65 76 3b 0d 0a 20 20 7d 0d  dr->pPrev;..  }.
a7fa0 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 42  .  z = (char*)pB
a7fb0 74 3b 0d 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d  t;..  z -= pHdr-
a7fc0 3e 6e 54 69 74 6c 65 3b 0d 0a 20 20 61 64 6a 75  >nTitle;..  adju
a7fd0 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53  stStats(pHdr->iS
a7fe0 69 7a 65 2c 20 2d 31 29 3b 0d 0a 20 20 72 61 6e  ize, -1);..  ran
a7ff0 64 6f 6d 46 69 6c 6c 28 7a 2c 20 73 69 7a 65 6f  domFill(z, sizeo
a8000 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e  f(void*)*pHdr->n
a8010 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b  BacktraceSlots +
a8020 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b   sizeof(*pHdr) +
a8030 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
a8040 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20    pHdr->iSize + 
a8050 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 48  sizeof(int) + pH
a8060 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0d 0a 20 20  dr->nTitle);..  
a8070 66 72 65 65 28 7a 29 3b 0d 0a 20 20 73 71 6c 69  free(z);..  sqli
a8080 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
a8090 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0d 0a 7d  mem.mutex);  ..}
a80a0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67  ..../*..** Chang
a80b0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
a80c0 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
a80d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2a   allocation...**
a80e0 0d 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 64 65  ..** For this de
a80f0 62 75 67 67 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  bugging implemen
a8100 74 61 74 69 6f 6e 2c 20 77 65 20 2a 61 6c 77 61  tation, we *alwa
a8110 79 73 2a 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  ys* make a copy 
a8120 6f 66 20 74 68 65 0d 0a 2a 2a 20 61 6c 6c 6f 63  of the..** alloc
a8130 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20 6e 65 77  ation into a new
a8140 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d 6f 72 79   place in memory
a8150 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20  .  In this way, 
a8160 69 66 20 74 68 65 20 0d 0a 2a 2a 20 68 69 67 68  if the ..** high
a8170 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20 69 73  er level code is
a8180 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 20 74   using pointer t
a8190 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61  o the old alloca
a81a0 74 69 6f 6e 2c 20 69 74 20 69 73 20 0d 0a 2a 2a  tion, it is ..**
a81b0 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c   much more likel
a81c0 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20 77  y to break and w
a81d0 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65 20  e are much more 
a81e0 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0d 0a  liking to find..
a81f0 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0d 0a 2a  ** the error...*
a8200 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  /..static void *
a8210 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
a8220 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
a8230 69 6e 74 20 6e 42 79 74 65 29 7b 0d 0a 20 20 73  int nByte){..  s
a8240 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
a8250 72 20 2a 70 4f 6c 64 48 64 72 3b 0d 0a 20 20 76  r *pOldHdr;..  v
a8260 6f 69 64 20 2a 70 4e 65 77 3b 0d 0a 20 20 61 73  oid *pNew;..  as
a8270 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c  sert( mem.disall
a8280 6f 77 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  ow==0 );..  asse
a8290 72 74 28 20 28 6e 42 79 74 65 20 26 20 37 29 3d  rt( (nByte & 7)=
a82a0 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 45 56 3a  =0 );     /* EV:
a82b0 20 52 2d 34 36 31 39 39 2d 33 30 32 34 39 20 2a   R-46199-30249 *
a82c0 2f 0d 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 73  /..  pOldHdr = s
a82d0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
a82e0 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0d 0a  eader(pPrior);..
a82f0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
a8300 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  MemMalloc(nByte)
a8310 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  ;..  if( pNew ){
a8320 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65  ..    memcpy(pNe
a8330 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65  w, pPrior, nByte
a8340 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20  <pOldHdr->iSize 
a8350 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64  ? nByte : pOldHd
a8360 72 2d 3e 69 53 69 7a 65 29 3b 0d 0a 20 20 20 20  r->iSize);..    
a8370 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64  if( nByte>pOldHd
a8380 72 2d 3e 69 53 69 7a 65 20 29 7b 0d 0a 20 20 20  r->iSize ){..   
a8390 20 20 20 72 61 6e 64 6f 6d 46 69 6c 6c 28 26 28     randomFill(&(
a83a0 28 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c  (char*)pNew)[pOl
a83b0 64 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 6e 42  dHdr->iSize], nB
a83c0 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69  yte - pOldHdr->i
a83d0 53 69 7a 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Size);..    }.. 
a83e0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
a83f0 65 28 70 50 72 69 6f 72 29 3b 0d 0a 20 20 7d 0d  e(pPrior);..  }.
a8400 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0d  .  return pNew;.
a8410 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f 70  .}..../*..** Pop
a8420 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65  ulate the low-le
a8430 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
a8440 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70  ation function p
a8450 6f 69 6e 74 65 72 73 20 69 6e 0d 0a 2a 2a 20 73  ointers in..** s
a8460 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a8470 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65  ig.m with pointe
a8480 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e  rs to the routin
a8490 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e  es in this file.
a84a0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
a84b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a84c0 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76  3MemSetDefault(v
a84d0 6f 69 64 29 7b 0d 0a 20 20 73 74 61 74 69 63 20  oid){..  static 
a84e0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
a84f0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
a8500 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0d 0a 20 20  tMethods = {..  
a8510 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
a8520 6c 6f 63 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74  loc,..     sqlit
a8530 65 33 4d 65 6d 46 72 65 65 2c 0d 0a 20 20 20 20  e3MemFree,..    
a8540 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c   sqlite3MemReall
a8550 6f 63 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65  oc,..     sqlite
a8560 33 4d 65 6d 53 69 7a 65 2c 0d 0a 20 20 20 20 20  3MemSize,..     
a8570 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75  sqlite3MemRoundu
a8580 70 2c 0d 0a 20 20 20 20 20 73 71 6c 69 74 65 33  p,..     sqlite3
a8590 4d 65 6d 49 6e 69 74 2c 0d 0a 20 20 20 20 20 73  MemInit,..     s
a85a0 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77  qlite3MemShutdow
a85b0 6e 2c 0d 0a 20 20 20 20 20 30 0d 0a 20 20 7d 3b  n,..     0..  };
a85c0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ..  sqlite3_conf
a85d0 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
a85e0 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
a85f0 74 4d 65 74 68 6f 64 73 29 3b 0d 0a 7d 0d 0a 0d  tMethods);..}...
a8600 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
a8610 22 74 79 70 65 22 20 6f 66 20 61 6e 20 61 6c 6c  "type" of an all
a8620 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51  ocation...*/..SQ
a8630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a8640 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
a8650 67 53 65 74 54 79 70 65 28 76 6f 69 64 20 2a 70  gSetType(void *p
a8660 2c 20 75 38 20 65 54 79 70 65 29 7b 0d 0a 20 20  , u8 eType){..  
a8670 69 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 33  if( p && sqlite3
a8680 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
a8690 4d 61 6c 6c 6f 63 3d 3d 73 71 6c 69 74 65 33 4d  Malloc==sqlite3M
a86a0 65 6d 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20  emMalloc ){..   
a86b0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
a86c0 48 64 72 20 2a 70 48 64 72 3b 0d 0a 20 20 20 20  Hdr *pHdr;..    
a86d0 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  pHdr = sqlite3Me
a86e0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 29  msysGetHeader(p)
a86f0 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
a8700 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d  Hdr->iForeGuard=
a8710 3d 46 4f 52 45 47 55 41 52 44 20 29 3b 0d 0a 20  =FOREGUARD );.. 
a8720 20 20 20 70 48 64 72 2d 3e 65 54 79 70 65 20 3d     pHdr->eType =
a8730 20 65 54 79 70 65 3b 0d 0a 20 20 7d 0d 0a 7d 0d   eType;..  }..}.
a8740 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
a8750 20 54 52 55 45 20 69 66 20 74 68 65 20 6d 61 73   TRUE if the mas
a8760 6b 20 6f 66 20 74 79 70 65 20 69 6e 20 65 54 79  k of type in eTy
a8770 70 65 20 6d 61 74 63 68 65 73 20 74 68 65 20 74  pe matches the t
a8780 79 70 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 61  ype of the..** a
a8790 6c 6c 6f 63 61 74 69 6f 6e 20 70 2e 20 20 41 6c  llocation p.  Al
a87a0 73 6f 20 72 65 74 75 72 6e 20 74 72 75 65 20 69  so return true i
a87b0 66 20 70 3d 3d 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a  f p==NULL...**..
a87c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
a87d0 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20  is designed for 
a87e0 75 73 65 20 77 69 74 68 69 6e 20 61 6e 20 61 73  use within an as
a87f0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
a8800 2c 20 74 6f 0d 0a 2a 2a 20 76 65 72 69 66 79 20  , to..** verify 
a8810 74 68 65 20 74 79 70 65 20 6f 66 20 61 6e 20 61  the type of an a
a8820 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f 72 20  llocation.  For 
a8830 65 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a  example:..**..**
a8840 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
a8850 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
a8860 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44  ype(p, MEMTYPE_D
a8870 42 29 20 29 3b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  B) );..*/..SQLIT
a8880 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a8890 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73  lite3MemdebugHas
a88a0 54 79 70 65 28 76 6f 69 64 20 2a 70 2c 20 75 38  Type(void *p, u8
a88b0 20 65 54 79 70 65 29 7b 0d 0a 20 20 69 6e 74 20   eType){..  int 
a88c0 72 63 20 3d 20 31 3b 0d 0a 20 20 69 66 28 20 70  rc = 1;..  if( p
a88d0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
a88e0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
a88f0 63 3d 3d 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  c==sqlite3MemMal
a8900 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 73 74 72 75  loc ){..    stru
a8910 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
a8920 70 48 64 72 3b 0d 0a 20 20 20 20 70 48 64 72 20  pHdr;..    pHdr 
a8930 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
a8940 65 74 48 65 61 64 65 72 28 70 29 3b 0d 0a 20 20  etHeader(p);..  
a8950 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e    assert( pHdr->
a8960 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f 52 45  iForeGuard==FORE
a8970 47 55 41 52 44 20 29 3b 20 20 20 20 20 20 20 20  GUARD );        
a8980 20 2f 2a 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 69   /* Allocation i
a8990 73 20 76 61 6c 69 64 20 2a 2f 0d 0a 20 20 20 20  s valid */..    
a89a0 69 66 28 20 28 70 48 64 72 2d 3e 65 54 79 70 65  if( (pHdr->eType
a89b0 26 65 54 79 70 65 29 3d 3d 30 20 29 7b 0d 0a 20  &eType)==0 ){.. 
a89c0 20 20 20 20 20 72 63 20 3d 20 30 3b 0d 0a 20 20       rc = 0;..  
a89d0 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
a89e0 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
a89f0 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
a8a00 69 66 20 74 68 65 20 6d 61 73 6b 20 6f 66 20 74  if the mask of t
a8a10 79 70 65 20 69 6e 20 65 54 79 70 65 20 6d 61 74  ype in eType mat
a8a20 63 68 65 73 20 6e 6f 20 62 69 74 73 20 6f 66 20  ches no bits of 
a8a30 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 0d  the type of the.
a8a40 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  .** allocation p
a8a50 2e 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 74  .  Also return t
a8a60 72 75 65 20 69 66 20 70 3d 3d 4e 55 4c 4c 2e 0d  rue if p==NULL..
a8a70 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
a8a80 74 69 6e 65 20 69 73 20 64 65 73 69 67 6e 65 64  tine is designed
a8a90 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20   for use within 
a8aa0 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  an assert() stat
a8ab0 65 6d 65 6e 74 2c 20 74 6f 0d 0a 2a 2a 20 76 65  ement, to..** ve
a8ac0 72 69 66 79 20 74 68 65 20 74 79 70 65 20 6f 66  rify the type of
a8ad0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20   an allocation. 
a8ae0 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0d 0a 2a   For example:..*
a8af0 2a 0d 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74  *..**     assert
a8b00 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  ( sqlite3Memdebu
a8b10 67 4e 6f 54 79 70 65 28 70 2c 20 4d 45 4d 54 59  gNoType(p, MEMTY
a8b20 50 45 5f 44 42 29 20 29 3b 0d 0a 2a 2f 0d 0a 53  PE_DB) );..*/..S
a8b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a8b40 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  t sqlite3Memdebu
a8b50 67 4e 6f 54 79 70 65 28 76 6f 69 64 20 2a 70 2c  gNoType(void *p,
a8b60 20 75 38 20 65 54 79 70 65 29 7b 0d 0a 20 20 69   u8 eType){..  i
a8b70 6e 74 20 72 63 20 3d 20 31 3b 0d 0a 20 20 69 66  nt rc = 1;..  if
a8b80 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 47 6c  ( p && sqlite3Gl
a8b90 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
a8ba0 6c 6c 6f 63 3d 3d 73 71 6c 69 74 65 33 4d 65 6d  lloc==sqlite3Mem
a8bb0 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 73  Malloc ){..    s
a8bc0 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
a8bd0 72 20 2a 70 48 64 72 3b 0d 0a 20 20 20 20 70 48  r *pHdr;..    pH
a8be0 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73  dr = sqlite3Mems
a8bf0 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0d  ysGetHeader(p);.
a8c00 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 64  .    assert( pHd
a8c10 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46  r->iForeGuard==F
a8c20 4f 52 45 47 55 41 52 44 20 29 3b 20 20 20 20 20  OREGUARD );     
a8c30 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 69 6f      /* Allocatio
a8c40 6e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0d 0a 20  n is valid */.. 
a8c50 20 20 20 69 66 28 20 28 70 48 64 72 2d 3e 65 54     if( (pHdr->eT
a8c60 79 70 65 26 65 54 79 70 65 29 21 3d 30 20 29 7b  ype&eType)!=0 ){
a8c70 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0d  ..      rc = 0;.
a8c80 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
a8c90 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
a8ca0 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  /*..** Set the n
a8cb0 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72 61  umber of backtra
a8cc0 63 65 20 6c 65 76 65 6c 73 20 6b 65 70 74 20 66  ce levels kept f
a8cd0 6f 72 20 65 61 63 68 20 61 6c 6c 6f 63 61 74 69  or each allocati
a8ce0 6f 6e 2e 0d 0a 2a 2a 20 41 20 76 61 6c 75 65 20  on...** A value 
a8cf0 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73 20 6f 66  of zero turns of
a8d00 66 20 62 61 63 6b 74 72 61 63 69 6e 67 2e 20 20  f backtracing.  
a8d10 54 68 65 20 6e 75 6d 62 65 72 20 69 73 20 61 6c  The number is al
a8d20 77 61 79 73 20 72 6f 75 6e 64 65 64 0d 0a 2a 2a  ways rounded..**
a8d30 20 75 70 20 74 6f 20 61 20 6d 75 6c 74 69 70 6c   up to a multipl
a8d40 65 20 6f 66 20 32 2e 0d 0a 2a 2f 0d 0a 53 51 4c  e of 2...*/..SQL
a8d50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a8d60 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
a8d70 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64 65  Backtrace(int de
a8d80 70 74 68 29 7b 0d 0a 20 20 69 66 28 20 64 65 70  pth){..  if( dep
a8d90 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20  th<0 ){ depth = 
a8da0 30 3b 20 7d 0d 0a 20 20 69 66 28 20 64 65 70 74  0; }..  if( dept
a8db0 68 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20  h>20 ){ depth = 
a8dc0 32 30 3b 20 7d 0d 0a 20 20 64 65 70 74 68 20 3d  20; }..  depth =
a8dd0 20 28 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b   (depth+1)&0xfe;
a8de0 0d 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  ..  mem.nBacktra
a8df0 63 65 20 3d 20 64 65 70 74 68 3b 0d 0a 7d 0d 0a  ce = depth;..}..
a8e00 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
a8e10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
a8e20 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61  debugBacktraceCa
a8e30 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42  llback(void (*xB
a8e40 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69  acktrace)(int, i
a8e50 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0d 0a  nt, void **)){..
a8e60 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
a8e70 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0d 0a   = xBacktrace;..
a8e80 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20  }..../*..** Set 
a8e90 74 68 65 20 74 69 74 6c 65 20 73 74 72 69 6e 67  the title string
a8ea0 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20   for subsequent 
a8eb0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0d 0a 2a 2f  allocations...*/
a8ec0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
a8ed0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
a8ee0 64 65 62 75 67 53 65 74 74 69 74 6c 65 28 63 6f  debugSettitle(co
a8ef0 6e 73 74 20 63 68 61 72 20 2a 7a 54 69 74 6c 65  nst char *zTitle
a8f00 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  ){..  unsigned i
a8f10 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
a8f20 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29 20 2b  rlen30(zTitle) +
a8f30 20 31 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d   1;..  sqlite3_m
a8f40 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
a8f50 75 74 65 78 29 3b 0d 0a 20 20 69 66 28 20 6e 3e  utex);..  if( n>
a8f60 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74  =sizeof(mem.zTit
a8f70 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66  le) ) n = sizeof
a8f80 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0d  (mem.zTitle)-1;.
a8f90 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54  .  memcpy(mem.zT
a8fa0 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29  itle, zTitle, n)
a8fb0 3b 0d 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b  ;..  mem.zTitle[
a8fc0 6e 5d 20 3d 20 30 3b 0d 0a 20 20 6d 65 6d 2e 6e  n] = 0;..  mem.n
a8fd0 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e  Title = ROUND8(n
a8fe0 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
a8ff0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
a9000 74 65 78 29 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49  tex);..}....SQLI
a9010 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a9020 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
a9030 79 6e 63 28 29 7b 0d 0a 20 20 73 74 72 75 63 74  ync(){..  struct
a9040 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
a9050 64 72 3b 0d 0a 20 20 66 6f 72 28 70 48 64 72 3d  dr;..  for(pHdr=
a9060 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72  mem.pFirst; pHdr
a9070 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65  ; pHdr=pHdr->pNe
a9080 78 74 29 7b 0d 0a 20 20 20 20 76 6f 69 64 20 2a  xt){..    void *
a9090 2a 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70  *pBt = (void**)p
a90a0 48 64 72 3b 0d 0a 20 20 20 20 70 42 74 20 2d 3d  Hdr;..    pBt -=
a90b0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
a90c0 65 53 6c 6f 74 73 3b 0d 0a 20 20 20 20 6d 65 6d  eSlots;..    mem
a90d0 2e 78 42 61 63 6b 74 72 61 63 65 28 70 48 64 72  .xBacktrace(pHdr
a90e0 2d 3e 69 53 69 7a 65 2c 20 70 48 64 72 2d 3e 6e  ->iSize, pHdr->n
a90f0 42 61 63 6b 74 72 61 63 65 2d 31 2c 20 26 70 42  Backtrace-1, &pB
a9100 74 5b 31 5d 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  t[1]);..  }..}..
a9110 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e 20 74 68  ../*..** Open th
a9120 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
a9130 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
a9140 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
a9150 6d 65 6d 6f 72 79 20 0d 0a 2a 2a 20 61 6c 6c 6f  memory ..** allo
a9160 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61  cations into tha
a9170 74 20 6c 6f 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  t log...*/..SQLI
a9180 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a9190 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 44  sqlite3MemdebugD
a91a0 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ump(const char *
a91b0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0d 0a 20 20 46  zFilename){..  F
a91c0 49 4c 45 20 2a 6f 75 74 3b 0d 0a 20 20 73 74 72  ILE *out;..  str
a91d0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
a91e0 2a 70 48 64 72 3b 0d 0a 20 20 76 6f 69 64 20 2a  *pHdr;..  void *
a91f0 2a 70 42 74 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d  *pBt;..  int i;.
a9200 0a 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a  .  out = fopen(z
a9210 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0d  Filename, "w");.
a9220 0a 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b  .  if( out==0 ){
a9230 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74  ..    fprintf(st
a9240 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65  derr, "** Unable
a9250 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72   to output memor
a9260 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c  y debug output l
a9270 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0d 0a 20  og: %s **\n",.. 
a9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9290 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a     zFilename);..
a92a0 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
a92b0 0d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d  ..  for(pHdr=mem
a92c0 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70  .pFirst; pHdr; p
a92d0 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29  Hdr=pHdr->pNext)
a92e0 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  {..    char *z =
a92f0 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0d 0a 20   (char*)pHdr;.. 
a9300 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42     z -= pHdr->nB
a9310 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69  acktraceSlots*si
a9320 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48  zeof(void*) + pH
a9330 64 72 2d 3e 6e 54 69 74 6c 65 3b 0d 0a 20 20 20  dr->nTitle;..   
a9340 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a   fprintf(out, "*
a9350 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61  *** %lld bytes a
a9360 74 20 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a  t %p from %s ***
a9370 2a 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20 20 20  *\n", ..        
a9380 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c      pHdr->iSize,
a9390 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d   &pHdr[1], pHdr-
a93a0 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f  >nTitle ? z : "?
a93b0 3f 3f 22 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  ??");..    if( p
a93c0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
a93d0 29 7b 0d 0a 20 20 20 20 20 20 66 66 6c 75 73 68  ){..      fflush
a93e0 28 6f 75 74 29 3b 0d 0a 20 20 20 20 20 20 70 42  (out);..      pB
a93f0 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
a9400 3b 0d 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20  ;..      pBt -= 
a9410 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
a9420 53 6c 6f 74 73 3b 0d 0a 20 20 20 20 20 20 62 61  Slots;..      ba
a9430 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f  cktrace_symbols_
a9440 66 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42  fd(pBt, pHdr->nB
a9450 61 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f  acktrace, fileno
a9460 28 6f 75 74 29 29 3b 0d 0a 20 20 20 20 20 20 66  (out));..      f
a9470 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
a9480 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
a9490 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
a94a0 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b 0d 0a 20 20  COUNTS:\n");..  
a94b0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a  for(i=0; i<NCSIZ
a94c0 45 2d 31 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  E-1; i++){..    
a94d0 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69  if( mem.nAlloc[i
a94e0 5d 20 29 7b 0d 0a 20 20 20 20 20 20 66 70 72 69  ] ){..      fpri
a94f0 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64  ntf(out, "   %5d
a9500 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64  : %10d %10d %10d
a9510 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  \n", ..         
a9520 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c     i*8, mem.nAll
a9530 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72  oc[i], mem.nCurr
a9540 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75  ent[i], mem.mxCu
a9550 72 72 65 6e 74 5b 69 5d 29 3b 0d 0a 20 20 20 20  rrent[i]);..    
a9560 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6d 65  }..  }..  if( me
a9570 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d  m.nAlloc[NCSIZE-
a9580 31 5d 20 29 7b 0d 0a 20 20 20 20 66 70 72 69 6e  1] ){..    fprin
a9590 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a  tf(out, "   %5d:
a95a0 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 5c   %10d %10d %10d\
a95b0 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  n",..           
a95c0 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65    NCSIZE*8-8, me
a95d0 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d  m.nAlloc[NCSIZE-
a95e0 31 5d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  1],..           
a95f0 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e    mem.nCurrent[N
a9600 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78  CSIZE-1], mem.mx
a9610 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31  Current[NCSIZE-1
a9620 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 66 63 6c 6f  ]);..  }..  fclo
a9630 73 65 28 6f 75 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  se(out);..}..../
a9640 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
a9650 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
a9660 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
a9670 63 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c  c() has been cal
a9680 6c 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  led...*/..SQLITE
a9690 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a96a0 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61 6c 6c  ite3MemdebugMall
a96b0 6f 63 43 6f 75 6e 74 28 29 7b 0d 0a 20 20 69 6e  ocCount(){..  in
a96c0 74 20 69 3b 0d 0a 20 20 69 6e 74 20 6e 54 6f 74  t i;..  int nTot
a96d0 61 6c 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28 69  al = 0;..  for(i
a96e0 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b  =0; i<NCSIZE; i+
a96f0 2b 29 7b 0d 0a 20 20 20 20 6e 54 6f 74 61 6c 20  +){..    nTotal 
a9700 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d  += mem.nAlloc[i]
a9710 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
a9720 20 6e 54 6f 74 61 6c 3b 0d 0a 7d 0d 0a 0d 0a 0d   nTotal;..}.....
a9730 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a9740 45 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0d 0a 0d  E_MEMDEBUG */...
a9750 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a9760 20 45 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a   End of mem2.c *
a9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a97a0 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
a97b0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
a97c0 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
a97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a97f0 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 37 20 4f  /../*..** 2007 O
a9800 63 74 6f 62 65 72 20 31 34 0d 0a 2a 2a 0d 0a 2a  ctober 14..**..*
a9810 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
a9820 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
a9830 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
a9840 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
a9850 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
a9860 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
a9870 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
a9880 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
a9890 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
a98a0 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
a98b0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
a98c0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
a98d0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
a98e0 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
a98f0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
a9900 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
a9910 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
a9920 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
a9930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
a9980 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
a9990 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
a99a0 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79  plement a memory
a99b0 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ..** allocation 
a99c0 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73  subsystem for us
a99d0 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0d 0a 2a  e by SQLite. ..*
a99e0 2a 0d 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  *..** This versi
a99f0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
a9a00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
a9a10 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0d  ystem omits all.
a9a20 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f  .** use of mallo
a9a30 63 28 29 2e 20 54 68 65 20 53 51 4c 69 74 65 20  c(). The SQLite 
a9a40 75 73 65 72 20 73 75 70 70 6c 69 65 73 20 61 20  user supplies a 
a9a50 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0d  block of memory.
a9a60 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  .** before calli
a9a70 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ng sqlite3_initi
a9a80 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69  alize() from whi
a9a90 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0d 0a  ch allocations..
a9aa0 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20  ** are made and 
a9ab0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
a9ac0 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52  xMalloc() and xR
a9ad0 65 61 6c 6c 6f 63 28 29 20 0d 0a 2a 2a 20 69 6d  ealloc() ..** im
a9ae0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f  plementations. O
a9af0 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  nce sqlite3_init
a9b00 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65  ialize() has bee
a9b10 6e 20 63 61 6c 6c 65 64 2c 0d 0a 2a 2a 20 74 68  n called,..** th
a9b20 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
a9b30 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  ry available to 
a9b40 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20  SQLite is fixed 
a9b50 61 6e 64 20 63 61 6e 6e 6f 74 0d 0a 2a 2a 20 62  and cannot..** b
a9b60 65 20 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a 0d 0a  e changed...**..
a9b70 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
a9b80 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
a9b90 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
a9ba0 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0d 0a  em is included..
a9bb0 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20  ** in the build 
a9bc0 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45  only if SQLITE_E
a9bd0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73  NABLE_MEMSYS3 is
a9be0 20 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 0d   defined...*/...
a9bf0 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 76 65 72  ./*..** This ver
a9c00 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
a9c10 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
a9c20 6f 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20  only built into 
a9c30 74 68 65 20 6c 69 62 72 61 72 79 0d 0a 2a 2a 20  the library..** 
a9c40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
a9c50 4d 53 59 53 33 20 69 73 20 64 65 66 69 6e 65 64  MSYS3 is defined
a9c60 2e 20 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20  . Defining this 
a9c70 73 79 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0d  symbol does not.
a9c80 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 74 68  .** mean that th
a9c90 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 75  e library will u
a9ca0 73 65 20 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c  se a memory-pool
a9cb0 20 62 79 20 64 65 66 61 75 6c 74 2c 20 6a 75 73   by default, jus
a9cc0 74 20 74 68 61 74 0d 0a 2a 2a 20 69 74 20 69 73  t that..** it is
a9cd0 20 61 76 61 69 6c 61 62 6c 65 2e 20 54 68 65 20   available. The 
a9ce0 6d 65 6d 70 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f  mempool allocato
a9cf0 72 20 69 73 20 61 63 74 69 76 61 74 65 64 20 62  r is activated b
a9d00 79 20 63 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 73 71  y calling..** sq
a9d10 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 0d  lite3_config()..
a9d20 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
a9d30 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
a9d40 33 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 78 69  3..../*..** Maxi
a9d50 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d  mum size (in Mem
a9d60 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73  3Blocks) of a "s
a9d70 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0d 0a 2a 2f  mall" chunk...*/
a9d80 0d 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41  ..#define MX_SMA
a9d90 4c 4c 20 31 30 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  LL 10....../*..*
a9da0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
a9db0 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0d  list hash slots.
a9dc0 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4e 5f 48  .*/..#define N_H
a9dd0 41 53 48 20 20 36 31 0d 0a 0d 0a 2f 2a 0d 0a 2a  ASH  61..../*..*
a9de0 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
a9df0 61 74 69 6f 6e 20 28 61 6c 73 6f 20 63 61 6c 6c  ation (also call
a9e00 65 64 20 61 20 22 63 68 75 6e 6b 22 29 20 63 6f  ed a "chunk") co
a9e10 6e 73 69 73 74 73 20 6f 66 20 74 77 6f 20 6f 72  nsists of two or
a9e20 20 0d 0a 2a 2a 20 6d 6f 72 65 20 62 6c 6f 63 6b   ..** more block
a9e30 73 20 77 68 65 72 65 20 65 61 63 68 20 62 6c 6f  s where each blo
a9e40 63 6b 20 69 73 20 38 20 62 79 74 65 73 2e 20 20  ck is 8 bytes.  
a9e50 54 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65  The first 8 byte
a9e60 73 20 61 72 65 20 0d 0a 2a 2a 20 61 20 68 65 61  s are ..** a hea
a9e70 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f 74 20  der that is not 
a9e80 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
a9e90 75 73 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20  user...**..** A 
a9ea0 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20  chunk is two or 
a9eb0 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74  more blocks that
a9ec0 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b   is either check
a9ed0 65 64 20 6f 75 74 20 6f 72 0d 0a 2a 2a 20 66 72  ed out or..** fr
a9ee0 65 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62  ee.  The first b
a9ef0 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20  lock has format 
a9f00 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69  u.hdr.  u.hdr.si
a9f10 7a 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20  ze4x is 4 times 
a9f20 74 68 65 0d 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  the..** size of 
a9f30 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
a9f40 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20  n blocks if the 
a9f50 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72  allocation is fr
a9f60 65 65 2e 0d 0a 2a 2a 20 54 68 65 20 75 2e 68 64  ee...** The u.hd
a9f70 72 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20 69  r.size4x&1 bit i
a9f80 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63 68  s true if the ch
a9f90 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f  unk is checked o
a9fa0 75 74 20 61 6e 64 0d 0a 2a 2a 20 66 61 6c 73 65  ut and..** false
a9fb0 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73   if the chunk is
a9fc0 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
a9fd0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a  .  The u.hdr.siz
a9fe0 65 34 78 26 32 20 62 69 74 0d 0a 2a 2a 20 69 73  e4x&2 bit..** is
a9ff0 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 65   true if the pre
aa000 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63  vious chunk is c
aa010 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 66  hecked out and f
aa020 61 6c 73 65 20 69 66 20 74 68 65 0d 0a 2a 2a 20  alse if the..** 
aa030 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
aa040 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68  s free.  The u.h
aa050 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c  dr.prevSize fiel
aa060 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
aa070 0d 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75  ..** the previou
aa080 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b  s chunk in block
aa090 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  s if the previou
aa0a0 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68  s chunk is on th
aa0b0 65 0d 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20  e..** freelist. 
aa0c0 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  If the previous 
aa0d0 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64  chunk is checked
aa0e0 20 6f 75 74 2c 20 74 68 65 6e 0d 0a 2a 2a 20 75   out, then..** u
aa0f0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61  .hdr.prevSize ca
aa100 6e 20 62 65 20 70 61 72 74 20 6f 66 20 74 68 65  n be part of the
aa110 20 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 63   data for that c
aa120 68 75 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0d  hunk and should.
aa130 0a 2a 2a 20 6e 6f 74 20 62 65 20 72 65 61 64 20  .** not be read 
aa140 6f 72 20 77 72 69 74 74 65 6e 2e 0d 0a 2a 2a 0d  or written...**.
aa150 0a 2a 2a 20 57 65 20 6f 66 74 65 6e 20 69 64 65  .** We often ide
aa160 6e 74 69 66 79 20 61 20 63 68 75 6e 6b 20 62 79  ntify a chunk by
aa170 20 69 74 73 20 69 6e 64 65 78 20 69 6e 20 6d 65   its index in me
aa180 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 20 57 68 65  m3.aPool[].  Whe
aa190 6e 0d 0a 2a 2a 20 74 68 69 73 20 69 73 20 64 6f  n..** this is do
aa1a0 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b 20 69 6e  ne, the chunk in
aa1b0 64 65 78 20 72 65 66 65 72 73 20 74 6f 20 74 68  dex refers to th
aa1c0 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f  e second block o
aa1d0 66 0d 0a 2a 2a 20 74 68 65 20 63 68 75 6e 6b 2e  f..** the chunk.
aa1e0 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 74    In this way, t
aa1f0 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b 20 68  he first chunk h
aa200 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
aa210 2e 0d 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 6e  ...** A chunk in
aa220 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20 22  dex of 0 means "
aa230 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20 61  no such chunk" a
aa240 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76 61  nd is the equiva
aa250 6c 65 6e 74 0d 0a 2a 2a 20 6f 66 20 61 20 4e 55  lent..** of a NU
aa260 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d  LL pointer...**.
aa270 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62  .** The second b
aa280 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75  lock of free chu
aa290 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f  nks is of the fo
aa2a0 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0d  rm u.list.  The.
aa2b0 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 66  .** two fields f
aa2c0 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e  orm a double-lin
aa2d0 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75 6e  ked list of chun
aa2e0 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73 69  ks of related si
aa2f0 7a 65 73 2e 0d 0a 2a 2a 20 50 6f 69 6e 74 65 72  zes...** Pointer
aa300 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  s to the head of
aa310 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 73 74   the list are st
aa320 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53  ored in mem3.aiS
aa330 6d 61 6c 6c 5b 5d 20 0d 0a 2a 2a 20 66 6f 72 20  mall[] ..** for 
aa340 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61  smaller chunks a
aa350 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d  nd mem3.aiHash[]
aa360 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 6e   for larger chun
aa370 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ks...**..** The 
aa380 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20  second block of 
aa390 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20  a chunk is user 
aa3a0 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e  data if the chun
aa3b0 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0d 0a 2a  k is checked ..*
aa3c0 2a 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75  * out.  If a chu
aa3d0 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75  nk is checked ou
aa3e0 74 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61  t, the user data
aa3f0 20 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f   may extend into
aa400 0d 0a 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70  ..** the u.hdr.p
aa410 72 65 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66  revSize value of
aa420 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
aa430 68 75 6e 6b 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64  hunk...*/..typed
aa440 65 66 20 73 74 72 75 63 74 20 4d 65 6d 33 42 6c  ef struct Mem3Bl
aa450 6f 63 6b 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0d 0a  ock Mem3Block;..
aa460 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b  struct Mem3Block
aa470 20 7b 0d 0a 20 20 75 6e 69 6f 6e 20 7b 0d 0a 20   {..  union {.. 
aa480 20 20 20 73 74 72 75 63 74 20 7b 0d 0a 20 20 20     struct {..   
aa490 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65 3b     u32 prevSize;
aa4a0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 72     /* Size of pr
aa4b0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20  evious chunk in 
aa4c0 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e  Mem3Block elemen
aa4d0 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20 75 33 32  ts */..      u32
aa4e0 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20   size4x;     /* 
aa4f0 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63  4x the size of c
aa500 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20  urrent chunk in 
aa510 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e  Mem3Block elemen
aa520 74 73 20 2a 2f 0d 0a 20 20 20 20 7d 20 68 64 72  ts */..    } hdr
aa530 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0d  ;..    struct {.
aa540 0a 20 20 20 20 20 20 75 33 32 20 6e 65 78 74 3b  .      u32 next;
aa550 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
aa560 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20  in mem3.aPool[] 
aa570 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75  of next free chu
aa580 6e 6b 20 2a 2f 0d 0a 20 20 20 20 20 20 75 33 32  nk */..      u32
aa590 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
aa5a0 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50  Index in mem3.aP
aa5b0 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75  ool[] of previou
aa5c0 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0d  s free chunk */.
aa5d0 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0d 0a 20 20  .    } list;..  
aa5e0 7d 20 75 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  } u;..};..../*..
aa5f0 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74  ** All of the st
aa600 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75  atic variables u
aa610 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75  sed by this modu
aa620 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64  le are collected
aa630 0d 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67  ..** into a sing
aa640 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d  le structure nam
aa650 65 64 20 22 6d 65 6d 33 22 2e 20 20 54 68 69 73  ed "mem3".  This
aa660 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0d   is to keep the.
aa670 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61  .** static varia
aa680 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61  bles organized a
aa690 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d  nd to reduce nam
aa6a0 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e  espace pollution
aa6b0 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d  ..** when this m
aa6c0 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65  odule is combine
aa6d0 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20  d with other in 
aa6e0 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
aa6f0 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 53 51  ...*/..static SQ
aa700 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
aa710 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0d 0a 20 20  Mem3Global {..  
aa720 2f 2a 0d 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20  /*..  ** Memory 
aa730 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c  available for al
aa740 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20  location. nPool 
aa750 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
aa760 68 65 20 61 72 72 61 79 0d 0a 20 20 2a 2a 20 28  he array..  ** (
aa770 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70  in Mem3Blocks) p
aa780 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f  ointed to by aPo
aa790 6f 6c 20 6c 65 73 73 20 32 2e 0d 0a 20 20 2a 2f  ol less 2...  */
aa7a0 0d 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0d 0a  ..  u32 nPool;..
aa7b0 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f    Mem3Block *aPo
aa7c0 6f 6c 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  ol;....  /*..  *
aa7d0 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
aa7e0 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f   evaluating an o
aa7f0 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c  ut-of-memory cal
aa800 6c 62 61 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  lback...  */..  
aa810 69 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b 0d 0a  int alarmBusy;..
aa820 20 20 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 4d    ..  /*..  ** M
aa830 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  utex to control 
aa840 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65  access to the me
aa850 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
aa860 73 75 62 73 79 73 74 65 6d 2e 0d 0a 20 20 2a 2f  subsystem...  */
aa870 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
aa880 78 20 2a 6d 75 74 65 78 3b 0d 0a 20 20 0d 0a 20  x *mutex;..  .. 
aa890 20 2f 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 6d 69   /*..  ** The mi
aa8a0 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  nimum amount of 
aa8b0 66 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20  free space that 
aa8c0 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0d 0a 20  we have seen... 
aa8d0 20 2a 2f 0d 0a 20 20 75 33 32 20 6d 6e 4d 61 73   */..  u32 mnMas
aa8e0 74 65 72 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20  ter;....  /*..  
aa8f0 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68  ** iMaster is th
aa900 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d  e index of the m
aa910 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f  aster chunk.  Mo
aa920 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f  st new allocatio
aa930 6e 73 0d 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f  ns..  ** occur o
aa940 66 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b  ff of this chunk
aa950 2e 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74  .  szMaster is t
aa960 68 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33  he size (in Mem3
aa970 42 6c 6f 63 6b 73 29 0d 0a 20 20 2a 2a 20 6f 66  Blocks)..  ** of
aa980 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73   the current mas
aa990 74 65 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73  ter.  iMaster is
aa9a0 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e   0 if there is n
aa9b0 6f 74 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e  ot master chunk.
aa9c0 0d 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65  ..  ** The maste
aa9d0 72 20 63 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69  r chunk is not i
aa9e0 6e 20 65 69 74 68 65 72 20 74 68 65 20 61 69 48  n either the aiH
aa9f0 61 73 68 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c  ash[] or aiSmall
aaa00 5b 5d 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75 33 32  []...  */..  u32
aaa10 20 69 4d 61 73 74 65 72 3b 0d 0a 20 20 75 33 32   iMaster;..  u32
aaa20 20 73 7a 4d 61 73 74 65 72 3b 0d 0a 0d 0a 20 20   szMaster;....  
aaa30 2f 2a 0d 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f  /*..  ** Array o
aaa40 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20  f lists of free 
aaa50 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67  blocks according
aaa60 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69   to the block si
aaa70 7a 65 20 0d 0a 20 20 2a 2a 20 66 6f 72 20 73 6d  ze ..  ** for sm
aaa80 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72  aller chunks, or
aaa90 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62   a hash on the b
aaaa0 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61  lock size for la
aaab0 72 67 65 72 0d 0a 20 20 2a 2a 20 63 68 75 6e 6b  rger..  ** chunk
aaac0 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75 33 32 20  s...  */..  u32 
aaad0 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c  aiSmall[MX_SMALL
aaae0 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69  -1];   /* For si
aaaf0 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58  zes 2 through MX
aab00 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76  _SMALL, inclusiv
aab10 65 20 2a 2f 0d 0a 20 20 75 33 32 20 61 69 48 61  e */..  u32 aiHa
aab20 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20  sh[N_HASH];     
aab30 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20     /* For sizes 
aab40 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c  MX_SMALL+1 and l
aab50 61 72 67 65 72 20 2a 2f 0d 0a 7d 20 6d 65 6d 33  arger */..} mem3
aab60 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b   = { 97535575 };
aab70 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33  ....#define mem3
aab80 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d   GLOBAL(struct M
aab90 65 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29  em3Global, mem3)
aaba0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 6c 69 6e  ..../*..** Unlin
aabb0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d  k the chunk at m
aabc0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f  em3.aPool[i] fro
aabd0 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 75 72  m list it is cur
aabe0 72 65 6e 74 6c 79 0d 0a 2a 2a 20 6f 6e 2e 20 20  rently..** on.  
aabf0 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 20 6c 69  *pRoot is the li
aac00 73 74 20 74 68 61 74 20 69 20 69 73 20 61 20 6d  st that i is a m
aac10 65 6d 62 65 72 20 6f 66 2e 0d 0a 2a 2f 0d 0a 73  ember of...*/..s
aac20 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
aac30 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
aac40 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f  (u32 i, u32 *pRo
aac50 6f 74 29 7b 0d 0a 20 20 75 33 32 20 6e 65 78 74  ot){..  u32 next
aac60 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d   = mem3.aPool[i]
aac70 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0d 0a 20  .u.list.next;.. 
aac80 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33   u32 prev = mem3
aac90 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
aaca0 2e 70 72 65 76 3b 0d 0a 20 20 61 73 73 65 72 74  .prev;..  assert
aacb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
aacc0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
aacd0 20 29 3b 0d 0a 20 20 69 66 28 20 70 72 65 76 3d   );..  if( prev=
aace0 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70 52 6f 6f  =0 ){..    *pRoo
aacf0 74 20 3d 20 6e 65 78 74 3b 0d 0a 20 20 7d 65 6c  t = next;..  }el
aad00 73 65 7b 0d 0a 20 20 20 20 6d 65 6d 33 2e 61 50  se{..    mem3.aP
aad10 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74  ool[prev].u.list
aad20 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0d 0a 20  .next = next;.. 
aad30 20 7d 0d 0a 20 20 69 66 28 20 6e 65 78 74 20 29   }..  if( next )
aad40 7b 0d 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  {..    mem3.aPoo
aad50 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70  l[next].u.list.p
aad60 72 65 76 20 3d 20 70 72 65 76 3b 0d 0a 20 20 7d  rev = prev;..  }
aad70 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ..  mem3.aPool[i
aad80 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20  ].u.list.next = 
aad90 30 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  0;..  mem3.aPool
aada0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20  [i].u.list.prev 
aadb0 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  = 0;..}..../*..*
aadc0 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75  * Unlink the chu
aadd0 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66 72  nk at index i fr
aade0 6f 6d 20 0d 0a 2a 2a 20 77 68 61 74 65 76 65 72  om ..** whatever
aadf0 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74   list is current
aae00 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0d  ly a member of..
aae10 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
aae20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75   memsys3Unlink(u
aae30 33 32 20 69 29 7b 0d 0a 20 20 75 33 32 20 73 69  32 i){..  u32 si
aae40 7a 65 2c 20 68 61 73 68 3b 0d 0a 20 20 61 73 73  ze, hash;..  ass
aae50 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
aae60 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
aae70 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ex) );..  assert
aae80 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  ( (mem3.aPool[i-
aae90 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
aaea0 26 20 31 29 3d 3d 30 20 29 3b 0d 0a 20 20 61 73  & 1)==0 );..  as
aaeb0 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0d 0a 20  sert( i>=1 );.. 
aaec0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
aaed0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
aaee0 7a 65 34 78 2f 34 3b 0d 0a 20 20 61 73 73 65 72  ze4x/4;..  asser
aaef0 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50  t( size==mem3.aP
aaf00 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e  ool[i+size-1].u.
aaf10 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0d  hdr.prevSize );.
aaf20 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e  .  assert( size>
aaf30 3d 32 20 29 3b 0d 0a 20 20 69 66 28 20 73 69 7a  =2 );..  if( siz
aaf40 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b  e <= MX_SMALL ){
aaf50 0d 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c  ..    memsys3Unl
aaf60 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26  inkFromList(i, &
aaf70 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a  mem3.aiSmall[siz
aaf80 65 2d 32 5d 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b  e-2]);..  }else{
aaf90 0d 0a 20 20 20 20 68 61 73 68 20 3d 20 73 69 7a  ..    hash = siz
aafa0 65 20 25 20 4e 5f 48 41 53 48 3b 0d 0a 20 20 20  e % N_HASH;..   
aafb0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
aafc0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
aafd0 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 0d 0a  aiHash[hash]);..
aafe0 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
aaff0 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20   Link the chunk 
ab000 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  at mem3.aPool[i]
ab010 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74   so that is on t
ab020 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0d 0a  he list rooted..
ab030 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0d 0a 2a  ** at *pRoot...*
ab040 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  /..static void m
ab050 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
ab060 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
ab070 52 6f 6f 74 29 7b 0d 0a 20 20 61 73 73 65 72 74  Root){..  assert
ab080 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
ab090 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
ab0a0 20 29 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f   );..  mem3.aPoo
ab0b0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[i].u.list.next
ab0c0 20 3d 20 2a 70 52 6f 6f 74 3b 0d 0a 20 20 6d 65   = *pRoot;..  me
ab0d0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
ab0e0 73 74 2e 70 72 65 76 20 3d 20 30 3b 0d 0a 20 20  st.prev = 0;..  
ab0f0 69 66 28 20 2a 70 52 6f 6f 74 20 29 7b 0d 0a 20  if( *pRoot ){.. 
ab100 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70     mem3.aPool[*p
ab110 52 6f 6f 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  Root].u.list.pre
ab120 76 20 3d 20 69 3b 0d 0a 20 20 7d 0d 0a 20 20 2a  v = i;..  }..  *
ab130 70 52 6f 6f 74 20 3d 20 69 3b 0d 0a 7d 0d 0a 0d  pRoot = i;..}...
ab140 0a 2f 2a 0d 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65  ./*..** Link the
ab150 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20   chunk at index 
ab160 69 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68  i into either th
ab170 65 20 61 70 70 72 6f 70 72 69 61 74 65 0d 0a 2a  e appropriate..*
ab180 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69  * small chunk li
ab190 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20  st, or into the 
ab1a0 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68  large chunk hash
ab1b0 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61   table...*/..sta
ab1c0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
ab1d0 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0d 0a 20 20  Link(u32 i){..  
ab1e0 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0d  u32 size, hash;.
ab1f0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
ab200 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
ab210 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  m3.mutex) );..  
ab220 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0d  assert( i>=1 );.
ab230 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33  .  assert( (mem3
ab240 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
ab250 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30  r.size4x & 1)==0
ab260 20 29 3b 0d 0a 20 20 73 69 7a 65 20 3d 20 6d 65   );..  size = me
ab270 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
ab280 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0d 0a 20  hdr.size4x/4;.. 
ab290 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d   assert( size==m
ab2a0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
ab2b0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
ab2c0 7a 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ze );..  assert(
ab2d0 20 73 69 7a 65 3e 3d 32 20 29 3b 0d 0a 20 20 69   size>=2 );..  i
ab2e0 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d  f( size <= MX_SM
ab2f0 41 4c 4c 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 73  ALL ){..    mems
ab300 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28  ys3LinkIntoList(
ab310 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  i, &mem3.aiSmall
ab320 5b 73 69 7a 65 2d 32 5d 29 3b 0d 0a 20 20 7d 65  [size-2]);..  }e
ab330 6c 73 65 7b 0d 0a 20 20 20 20 68 61 73 68 20 3d  lse{..    hash =
ab340 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0d   size % N_HASH;.
ab350 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
ab360 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
ab370 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
ab380 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
ab390 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43  ** If the STATIC
ab3a0 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f  _MEM mutex is no
ab3b0 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20  t already held, 
ab3c0 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54  obtain it now. T
ab3d0 68 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 77 69 6c  he mutex..** wil
ab3e0 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c  l already be hel
ab3f0 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63  d (obtained by c
ab400 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29  ode in malloc.c)
ab410 20 69 66 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 47   if..** sqlite3G
ab420 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
ab430 53 74 61 74 20 69 73 20 74 72 75 65 2e 0d 0a 2a  Stat is true...*
ab440 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  /..static void m
ab450 65 6d 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64  emsys3Enter(void
ab460 29 7b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ){..  if( sqlite
ab470 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
ab480 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d  emstat==0 && mem
ab490 33 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0d 0a 20  3.mutex==0 ){.. 
ab4a0 20 20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20     mem3.mutex = 
ab4b0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
ab4c0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
ab4d0 54 41 54 49 43 5f 4d 45 4d 29 3b 0d 0a 20 20 7d  TATIC_MEM);..  }
ab4e0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
ab4f0 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74  x_enter(mem3.mut
ab500 65 78 29 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20  ex);..}..static 
ab510 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61 76  void memsys3Leav
ab520 65 28 76 6f 69 64 29 7b 0d 0a 20 20 73 71 6c 69  e(void){..  sqli
ab530 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
ab540 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0d 0a 7d 0d  mem3.mutex);..}.
ab550 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c 65 64  .../*..** Called
ab560 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61   when we are una
ab570 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61  ble to satisfy a
ab580 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  n allocation of 
ab590 6e 42 79 74 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  nBytes...*/..sta
ab5a0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
ab5b0 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20  OutOfMemory(int 
ab5c0 6e 42 79 74 65 29 7b 0d 0a 20 20 69 66 28 20 21  nByte){..  if( !
ab5d0 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29  mem3.alarmBusy )
ab5e0 7b 0d 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72  {..    mem3.alar
ab5f0 6d 42 75 73 79 20 3d 20 31 3b 0d 0a 20 20 20 20  mBusy = 1;..    
ab600 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
ab610 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
ab620 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 20 20 73  mutex) );..    s
ab630 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
ab640 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0d  ve(mem3.mutex);.
ab650 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c  .    sqlite3_rel
ab660 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6e 42 79 74  ease_memory(nByt
ab670 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
ab680 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
ab690 33 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20 6d  3.mutex);..    m
ab6a0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  em3.alarmBusy = 
ab6b0 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 0d 0a  0;..  }..}......
ab6c0 2f 2a 0d 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69  /*..** Chunk i i
ab6d0 73 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20 74  s a free chunk t
ab6e0 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e 6c  hat has been unl
ab6f0 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 69  inked.  Adjust i
ab700 74 73 20 0d 0a 2a 2a 20 73 69 7a 65 20 70 61 72  ts ..** size par
ab710 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 65 63  ameters for chec
ab720 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e  k-out and return
ab730 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
ab740 65 20 0d 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74  e ..** user port
ab750 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e 6b  ion of the chunk
ab760 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
ab770 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b  id *memsys3Check
ab780 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e  out(u32 i, u32 n
ab790 42 6c 6f 63 6b 29 7b 0d 0a 20 20 75 33 32 20 78  Block){..  u32 x
ab7a0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
ab7b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
ab7c0 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0d 0a  mem3.mutex) );..
ab7d0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29    assert( i>=1 )
ab7e0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  ;..  assert( mem
ab7f0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
ab800 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c  dr.size4x/4==nBl
ab810 6f 63 6b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ock );..  assert
ab820 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e  ( mem3.aPool[i+n
ab830 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70  Block-1].u.hdr.p
ab840 72 65 76 53 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20  revSize==nBlock 
ab850 29 3b 0d 0a 20 20 78 20 3d 20 6d 65 6d 33 2e 61  );..  x = mem3.a
ab860 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
ab870 73 69 7a 65 34 78 3b 0d 0a 20 20 6d 65 6d 33 2e  size4x;..  mem3.
ab880 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
ab890 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b  .size4x = nBlock
ab8a0 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29 3b 0d  *4 | 1 | (x&2);.
ab8b0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  .  mem3.aPool[i+
ab8c0 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e  nBlock-1].u.hdr.
ab8d0 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63  prevSize = nBloc
ab8e0 6b 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  k;..  mem3.aPool
ab8f0 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
ab900 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0d  dr.size4x |= 2;.
ab910 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e  .  return &mem3.
ab920 61 50 6f 6f 6c 5b 69 5d 3b 0d 0a 7d 0d 0a 0d 0a  aPool[i];..}....
ab930 2f 2a 0d 0a 2a 2a 20 43 61 72 76 65 20 61 20 70  /*..** Carve a p
ab940 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20  iece off of the 
ab950 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e  end of the mem3.
ab960 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 75  iMaster free chu
ab970 6e 6b 2e 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61  nk...** Return a
ab980 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
ab990 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
ab9a0 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74   Or, if the mast
ab9b0 65 72 20 63 68 75 6e 6b 0d 0a 2a 2a 20 69 73 20  er chunk..** is 
ab9c0 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  not large enough
ab9d0 2c 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f 0d  , return 0...*/.
ab9e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
ab9f0 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
aba00 75 33 32 20 6e 42 6c 6f 63 6b 29 7b 0d 0a 20 20  u32 nBlock){..  
aba10 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
aba20 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
aba30 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73  mutex) );..  ass
aba40 65 72 74 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  ert( mem3.szMast
aba50 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 3b 0d 0a 20  er>=nBlock );.. 
aba60 20 69 66 28 20 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d   if( nBlock>=mem
aba70 33 2e 73 7a 4d 61 73 74 65 72 2d 31 20 29 7b 0d  3.szMaster-1 ){.
aba80 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20  .    /* Use the 
aba90 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 2a 2f  entire master */
abaa0 0d 0a 20 20 20 20 76 6f 69 64 20 2a 70 20 3d 20  ..    void *p = 
abab0 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28  memsys3Checkout(
abac0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 6d 65  mem3.iMaster, me
abad0 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0d 0a 20  m3.szMaster);.. 
abae0 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20     mem3.iMaster 
abaf0 3d 20 30 3b 0d 0a 20 20 20 20 6d 65 6d 33 2e 73  = 0;..    mem3.s
abb00 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0d 0a 20 20  zMaster = 0;..  
abb10 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
abb20 3d 20 30 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  = 0;..    return
abb30 20 70 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   p;..  }else{.. 
abb40 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20     /* Split the 
abb50 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52  master block.  R
abb60 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20  eturn the tail. 
abb70 2a 2f 0d 0a 20 20 20 20 75 33 32 20 6e 65 77 69  */..    u32 newi
abb80 2c 20 78 3b 0d 0a 20 20 20 20 6e 65 77 69 20 3d  , x;..    newi =
abb90 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20   mem3.iMaster + 
abba0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20  mem3.szMaster - 
abbb0 6e 42 6c 6f 63 6b 3b 0d 0a 20 20 20 20 61 73 73  nBlock;..    ass
abbc0 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33  ert( newi > mem3
abbd0 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0d 0a 20  .iMaster+1 );.. 
abbe0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
abbf0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
abc00 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
abc10 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c  r.prevSize = nBl
abc20 6f 63 6b 3b 0d 0a 20 20 20 20 6d 65 6d 33 2e 61  ock;..    mem3.a
abc30 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
abc40 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
abc50 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
abc60 7c 3d 20 32 3b 0d 0a 20 20 20 20 6d 65 6d 33 2e  |= 2;..    mem3.
abc70 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e  aPool[newi-1].u.
abc80 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c  hdr.size4x = nBl
abc90 6f 63 6b 2a 34 20 2b 20 31 3b 0d 0a 20 20 20 20  ock*4 + 1;..    
abca0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 3d  mem3.szMaster -=
abcb0 20 6e 42 6c 6f 63 6b 3b 0d 0a 20 20 20 20 6d 65   nBlock;..    me
abcc0 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d  m3.aPool[newi-1]
abcd0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
abce0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
abcf0 0d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61  ..    x = mem3.a
abd00 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
abd10 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
abd20 78 20 26 20 32 3b 0d 0a 20 20 20 20 6d 65 6d 33  x & 2;..    mem3
abd30 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
abd40 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
abd50 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  e4x = mem3.szMas
abd60 74 65 72 2a 34 20 7c 20 78 3b 0d 0a 20 20 20 20  ter*4 | x;..    
abd70 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  if( mem3.szMaste
abd80 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65  r < mem3.mnMaste
abd90 72 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 33  r ){..      mem3
abda0 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .mnMaster = mem3
abdb0 2e 73 7a 4d 61 73 74 65 72 3b 0d 0a 20 20 20 20  .szMaster;..    
abdc0 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76  }..    return (v
abdd0 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c  oid*)&mem3.aPool
abde0 5b 6e 65 77 69 5d 3b 0d 0a 20 20 7d 0d 0a 7d 0d  [newi];..  }..}.
abdf0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 2a 70 52 6f 6f 74  .../*..** *pRoot
abe00 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20   is the head of 
abe10 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63  a list of free c
abe20 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d  hunks of the sam
abe30 65 20 73 69 7a 65 0d 0a 2a 2a 20 6f 72 20 73 61  e size..** or sa
abe40 6d 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49  me size hash.  I
abe50 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a  n other words, *
abe60 70 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72  pRoot is an entr
abe70 79 20 69 6e 20 65 69 74 68 65 72 0d 0a 2a 2a 20  y in either..** 
abe80 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f  mem3.aiSmall[] o
abe90 72 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e  r mem3.aiHash[].
abea0 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20    ..**..** This 
abeb0 72 6f 75 74 69 6e 65 20 65 78 61 6d 69 6e 65 73  routine examines
abec0 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 6e 20   all entries on 
abed0 74 68 65 20 67 69 76 65 6e 20 6c 69 73 74 20 61  the given list a
abee0 6e 64 20 74 72 69 65 73 0d 0a 2a 2a 20 74 6f 20  nd tries..** to 
abef0 63 6f 61 6c 65 73 63 65 20 65 61 63 68 20 65 6e  coalesce each en
abf00 74 72 69 65 73 20 77 69 74 68 20 61 64 6a 61 63  tries with adjac
abf10 65 6e 74 20 66 72 65 65 20 63 68 75 6e 6b 73 2e  ent free chunks.
abf20 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69 74    ..**..** If it
abf30 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20 74 68   sees a chunk th
abf40 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  at is larger tha
abf50 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20  n mem3.iMaster, 
abf60 69 74 20 72 65 70 6c 61 63 65 73 20 0d 0a 2a 2a  it replaces ..**
abf70 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 6d   the current mem
abf80 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20 74  3.iMaster with t
abf90 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63 68  he new larger ch
abfa0 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20 66  unk.  In order f
abfb0 6f 72 0d 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33  or..** this mem3
abfc0 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65  .iMaster replace
abfd0 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68  ment to work, th
abfe0 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d  e master chunk m
abff0 75 73 74 20 62 65 0d 0a 2a 2a 20 6c 69 6e 6b 65  ust be..** linke
ac000 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  d into the hash 
ac010 74 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73  tables.  That is
ac020 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20   not the normal 
ac030 73 74 61 74 65 20 6f 66 0d 0a 2a 2a 20 61 66 66  state of..** aff
ac040 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e  airs, of course.
ac050 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f    The calling ro
ac060 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20  utine must link 
ac070 74 68 65 20 6d 61 73 74 65 72 0d 0a 2a 2a 20 63  the master..** c
ac080 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f  hunk before invo
ac090 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  king this routin
ac0a0 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c  e, then must unl
ac0b0 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62 6c  ink the (possibl
ac0c0 79 0d 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d  y..** changed) m
ac0d0 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65  aster chunk once
ac0e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   this routine ha
ac0f0 73 20 66 69 6e 69 73 68 65 64 2e 0d 0a 2a 2f 0d  s finished...*/.
ac100 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
ac110 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70  sys3Merge(u32 *p
ac120 52 6f 6f 74 29 7b 0d 0a 20 20 75 33 32 20 69 4e  Root){..  u32 iN
ac130 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c  ext, prev, size,
ac140 20 69 2c 20 78 3b 0d 0a 0d 0a 20 20 61 73 73 65   i, x;....  asse
ac150 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
ac160 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
ac170 78 29 20 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 2a  x) );..  for(i=*
ac180 70 52 6f 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e  pRoot; i>0; i=iN
ac190 65 78 74 29 7b 0d 0a 20 20 20 20 69 4e 65 78 74  ext){..    iNext
ac1a0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d   = mem3.aPool[i]
ac1b0 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0d 0a 20  .u.list.next;.. 
ac1c0 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61     size = mem3.a
ac1d0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
ac1e0 73 69 7a 65 34 78 3b 0d 0a 20 20 20 20 61 73 73  size4x;..    ass
ac1f0 65 72 74 28 20 28 73 69 7a 65 26 31 29 3d 3d 30  ert( (size&1)==0
ac200 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 28 73 69   );..    if( (si
ac210 7a 65 26 32 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  ze&2)==0 ){..   
ac220 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
ac230 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f 6f  FromList(i, pRoo
ac240 74 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  t);..      asser
ac250 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f  t( i > mem3.aPoo
ac260 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  l[i-1].u.hdr.pre
ac270 76 53 69 7a 65 20 29 3b 0d 0a 20 20 20 20 20 20  vSize );..      
ac280 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e  prev = i - mem3.
ac290 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
ac2a0 2e 70 72 65 76 53 69 7a 65 3b 0d 0a 20 20 20 20  .prevSize;..    
ac2b0 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78    if( prev==iNex
ac2c0 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 4e  t ){..        iN
ac2d0 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ext = mem3.aPool
ac2e0 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65  [prev].u.list.ne
ac2f0 78 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  xt;..      }..  
ac300 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
ac310 6b 28 70 72 65 76 29 3b 0d 0a 20 20 20 20 20 20  k(prev);..      
ac320 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f  size = i + size/
ac330 34 20 2d 20 70 72 65 76 3b 0d 0a 20 20 20 20 20  4 - prev;..     
ac340 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
ac350 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  prev-1].u.hdr.si
ac360 7a 65 34 78 20 26 20 32 3b 0d 0a 20 20 20 20 20  ze4x & 2;..     
ac370 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76   mem3.aPool[prev
ac380 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
ac390 20 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0d 0a   = size*4 | x;..
ac3a0 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c        mem3.aPool
ac3b0 5b 70 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e  [prev+size-1].u.
ac3c0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73  hdr.prevSize = s
ac3d0 69 7a 65 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 73  ize;..      mems
ac3e0 79 73 33 4c 69 6e 6b 28 70 72 65 76 29 3b 0d 0a  ys3Link(prev);..
ac3f0 20 20 20 20 20 20 69 20 3d 20 70 72 65 76 3b 0d        i = prev;.
ac400 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
ac410 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b 0d 0a 20     size /= 4;.. 
ac420 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73 69     }..    if( si
ac430 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  ze>mem3.szMaster
ac440 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 33 2e   ){..      mem3.
ac450 69 4d 61 73 74 65 72 20 3d 20 69 3b 0d 0a 20 20  iMaster = i;..  
ac460 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65      mem3.szMaste
ac470 72 20 3d 20 73 69 7a 65 3b 0d 0a 20 20 20 20 7d  r = size;..    }
ac480 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
ac490 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63  ** Return a bloc
ac4a0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61  k of memory of a
ac4b0 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69  t least nBytes i
ac4c0 6e 20 73 69 7a 65 2e 0d 0a 2a 2a 20 52 65 74 75  n size...** Retu
ac4d0 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c  rn NULL if unabl
ac4e0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  e...**..** This 
ac4f0 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
ac500 20 74 68 61 74 20 74 68 65 20 6e 65 63 65 73 73   that the necess
ac510 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69 66 20  ary mutexes, if 
ac520 61 6e 79 2c 20 61 72 65 0d 0a 2a 2a 20 61 6c 72  any, are..** alr
ac530 65 61 64 79 20 68 65 6c 64 20 62 79 20 74 68 65  eady held by the
ac540 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65 20 22   caller. Hence "
ac550 55 6e 73 61 66 65 22 2e 0d 0a 2a 2f 0d 0a 73 74  Unsafe"...*/..st
ac560 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
ac570 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69  s3MallocUnsafe(i
ac580 6e 74 20 6e 42 79 74 65 29 7b 0d 0a 20 20 75 33  nt nByte){..  u3
ac590 32 20 69 3b 0d 0a 20 20 75 33 32 20 6e 42 6c 6f  2 i;..  u32 nBlo
ac5a0 63 6b 3b 0d 0a 20 20 75 33 32 20 74 6f 46 72 65  ck;..  u32 toFre
ac5b0 65 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  e;....  assert( 
ac5c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
ac5d0 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
ac5e0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  ;..  assert( siz
ac5f0 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d  eof(Mem3Block)==
ac600 38 20 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74  8 );..  if( nByt
ac610 65 3c 3d 31 32 20 29 7b 0d 0a 20 20 20 20 6e 42  e<=12 ){..    nB
ac620 6c 6f 63 6b 20 3d 20 32 3b 0d 0a 20 20 7d 65 6c  lock = 2;..  }el
ac630 73 65 7b 0d 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  se{..    nBlock 
ac640 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38  = (nByte + 11)/8
ac650 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
ac660 28 20 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0d 0a  ( nBlock>=2 );..
ac670 0d 0a 20 20 2f 2a 20 53 54 45 50 20 31 3a 0d 0a  ..  /* STEP 1:..
ac680 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e    ** Look for an
ac690 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 6f   entry of the co
ac6a0 72 72 65 63 74 20 73 69 7a 65 20 69 6e 20 65 69  rrect size in ei
ac6b0 74 68 65 72 20 74 68 65 20 73 6d 61 6c 6c 0d 0a  ther the small..
ac6c0 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61 62 6c 65    ** chunk table
ac6d0 20 6f 72 20 69 6e 20 74 68 65 20 6c 61 72 67 65   or in the large
ac6e0 20 63 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c   chunk hash tabl
ac6f0 65 2e 20 20 54 68 69 73 20 69 73 0d 0a 20 20 2a  e.  This is..  *
ac700 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d 6f 73  * successful mos
ac710 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20 28 61  t of the time (a
ac720 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f 75 74  bout 9 times out
ac730 20 6f 66 20 31 30 29 2e 0d 0a 20 20 2a 2f 0d 0a   of 10)...  */..
ac740 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20    if( nBlock <= 
ac750 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0d 0a 20 20 20  MX_SMALL ){..   
ac760 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c   i = mem3.aiSmal
ac770 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0d 0a 20 20  l[nBlock-2];..  
ac780 20 20 69 66 28 20 69 3e 30 20 29 7b 0d 0a 20 20    if( i>0 ){..  
ac790 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
ac7a0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65  kFromList(i, &me
ac7b0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63  m3.aiSmall[nBloc
ac7c0 6b 2d 32 5d 29 3b 0d 0a 20 20 20 20 20 20 72 65  k-2]);..      re
ac7d0 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
ac7e0 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
ac7f0 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65  ..    }..  }else
ac800 7b 0d 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20  {..    int hash 
ac810 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53  = nBlock % N_HAS
ac820 48 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65  H;..    for(i=me
ac830 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b  m3.aiHash[hash];
ac840 20 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f   i>0; i=mem3.aPo
ac850 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
ac860 74 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 6d  t){..      if( m
ac870 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
ac880 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e  .hdr.size4x/4==n
ac890 42 6c 6f 63 6b 20 29 7b 0d 0a 20 20 20 20 20 20  Block ){..      
ac8a0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46    memsys3UnlinkF
ac8b0 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33  romList(i, &mem3
ac8c0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 0d  .aiHash[hash]);.
ac8d0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
ac8e0 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28  memsys3Checkout(
ac8f0 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20  i, nBlock);..   
ac900 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
ac910 0d 0a 0d 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a  ....  /* STEP 2:
ac920 0d 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61  ..  ** Try to sa
ac930 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61  tisfy the alloca
ac940 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20  tion by carving 
ac950 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74  a piece off of t
ac960 68 65 20 65 6e 64 0d 0a 20 20 2a 2a 20 6f 66 20  he end..  ** of 
ac970 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b  the master chunk
ac980 2e 20 20 54 68 69 73 20 73 74 65 70 20 75 73 75  .  This step usu
ac990 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74  ally works if st
ac9a0 65 70 20 31 20 66 61 69 6c 73 2e 0d 0a 20 20 2a  ep 1 fails...  *
ac9b0 2f 0d 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  /..  if( mem3.sz
ac9c0 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29  Master>=nBlock )
ac9d0 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65  {..    return me
ac9e0 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
ac9f0 6e 42 6c 6f 63 6b 29 3b 0d 0a 20 20 7d 0d 0a 0d  nBlock);..  }...
aca00 0a 0d 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20  ...  /* STEP 3: 
aca10 20 0d 0a 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72   ..  ** Loop thr
aca20 6f 75 67 68 20 74 68 65 20 65 6e 74 69 72 65 20  ough the entire 
aca30 6d 65 6d 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f  memory pool.  Co
aca40 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20  alesce adjacent 
aca50 66 72 65 65 0d 0a 20 20 2a 2a 20 63 68 75 6e 6b  free..  ** chunk
aca60 73 2e 20 20 52 65 63 6f 6d 70 75 74 65 20 74 68  s.  Recompute th
aca70 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 61  e master chunk a
aca80 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 66 72  s the largest fr
aca90 65 65 20 63 68 75 6e 6b 2e 0d 0a 20 20 2a 2a 20  ee chunk...  ** 
acaa0 54 68 65 6e 20 74 72 79 20 61 67 61 69 6e 20 74  Then try again t
acab0 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c  o satisfy the al
acac0 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76  location by carv
acad0 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 0d  ing a piece off.
acae0 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 6e 64  .  ** of the end
acaf0 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
acb00 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70  hunk.  This step
acb10 20 68 61 70 70 65 6e 73 20 76 65 72 79 0d 0a 20   happens very.. 
acb20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65 20 68   ** rarely (we h
acb30 6f 70 65 21 29 0d 0a 20 20 2a 2f 0d 0a 20 20 66  ope!)..  */..  f
acb40 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b  or(toFree=nBlock
acb50 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d  *16; toFree<(mem
acb60 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46  3.nPool*16); toF
acb70 72 65 65 20 2a 3d 20 32 29 7b 0d 0a 20 20 20 20  ree *= 2){..    
acb80 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f  memsys3OutOfMemo
acb90 72 79 28 74 6f 46 72 65 65 29 3b 0d 0a 20 20 20  ry(toFree);..   
acba0 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65   if( mem3.iMaste
acbb0 72 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 73  r ){..      mems
acbc0 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61  ys3Link(mem3.iMa
acbd0 73 74 65 72 29 3b 0d 0a 20 20 20 20 20 20 6d 65  ster);..      me
acbe0 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0d  m3.iMaster = 0;.
acbf0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
acc00 73 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  ster = 0;..    }
acc10 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
acc20 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0d 0a  <N_HASH; i++){..
acc30 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72        memsys3Mer
acc40 67 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  ge(&mem3.aiHash[
acc50 69 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  i]);..    }..   
acc60 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53   for(i=0; i<MX_S
acc70 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0d 0a 20  MALL-1; i++){.. 
acc80 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67       memsys3Merg
acc90 65 28 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b  e(&mem3.aiSmall[
acca0 69 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  i]);..    }..   
accb0 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
accc0 65 72 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d  er ){..      mem
accd0 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e  sys3Unlink(mem3.
acce0 69 4d 61 73 74 65 72 29 3b 0d 0a 20 20 20 20 20  iMaster);..     
accf0 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
acd00 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0d 0a 20  er>=nBlock ){.. 
acd10 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65         return me
acd20 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
acd30 6e 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20 20 20 20  nBlock);..      
acd40 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  }..    }..  }...
acd50 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66  .  /* If none of
acd60 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b 65   the above worke
acd70 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c 2e  d, then we fail.
acd80 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b   */..  return 0;
acd90 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72  ..}..../*..** Fr
acda0 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  ee an outstandin
acdb0 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  g memory allocat
acdc0 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ion...**..** Thi
acdd0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
acde0 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 63 65  es that the nece
acdf0 73 73 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69  ssary mutexes, i
ace00 66 20 61 6e 79 2c 20 61 72 65 0d 0a 2a 2a 20 61  f any, are..** a
ace10 6c 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 74  lready held by t
ace20 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65  he caller. Hence
ace30 20 22 55 6e 73 61 66 65 22 2e 0d 0a 2a 2f 0d 0a   "Unsafe"...*/..
ace40 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
ace50 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys3FreeUnsafe(vo
ace60 69 64 20 2a 70 4f 6c 64 29 7b 0d 0a 20 20 4d 65  id *pOld){..  Me
ace70 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65  m3Block *p = (Me
ace80 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0d 0a  m3Block*)pOld;..
ace90 20 20 69 6e 74 20 69 3b 0d 0a 20 20 75 33 32 20    int i;..  u32 
acea0 73 69 7a 65 2c 20 78 3b 0d 0a 20 20 61 73 73 65  size, x;..  asse
aceb0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
acec0 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
aced0 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  x) );..  assert(
acee0 20 70 3e 6d 65 6d 33 2e 61 50 6f 6f 6c 20 26 26   p>mem3.aPool &&
acef0 20 70 3c 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d   p<&mem3.aPool[m
acf00 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 29 3b 0d 0a 20  em3.nPool] );.. 
acf10 20 69 20 3d 20 70 20 2d 20 6d 65 6d 33 2e 61 50   i = p - mem3.aP
acf20 6f 6f 6c 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ool;..  assert( 
acf30 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d  (mem3.aPool[i-1]
acf40 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29  .u.hdr.size4x&1)
acf50 3d 3d 31 20 29 3b 0d 0a 20 20 73 69 7a 65 20 3d  ==1 );..  size =
acf60 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
acf70 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
acf80 0d 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73 69  ..  assert( i+si
acf90 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b 31  ze<=mem3.nPool+1
acfa0 20 29 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f   );..  mem3.aPoo
acfb0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
acfc0 65 34 78 20 26 3d 20 7e 31 3b 0d 0a 20 20 6d 65  e4x &= ~1;..  me
acfd0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d  m3.aPool[i+size-
acfe0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
acff0 65 20 3d 20 73 69 7a 65 3b 0d 0a 20 20 6d 65 6d  e = size;..  mem
ad000 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
ad010 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
ad020 3d 20 7e 32 3b 0d 0a 20 20 6d 65 6d 73 79 73 33  = ~2;..  memsys3
ad030 4c 69 6e 6b 28 69 29 3b 0d 0a 0d 0a 20 20 2f 2a  Link(i);....  /*
ad040 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 74   Try to expand t
ad050 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 20  he master using 
ad060 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20  the newly freed 
ad070 63 68 75 6e 6b 20 2a 2f 0d 0a 20 20 69 66 28 20  chunk */..  if( 
ad080 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0d  mem3.iMaster ){.
ad090 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
ad0a0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
ad0b0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
ad0c0 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0d 0a 20  ze4x&2)==0 ){.. 
ad0d0 20 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33       size = mem3
ad0e0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
ad0f0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
ad100 76 53 69 7a 65 3b 0d 0a 20 20 20 20 20 20 6d 65  vSize;..      me
ad110 6d 33 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69  m3.iMaster -= si
ad120 7a 65 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 33 2e  ze;..      mem3.
ad130 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73 69 7a 65  szMaster += size
ad140 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ;..      memsys3
ad150 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73  Unlink(mem3.iMas
ad160 74 65 72 29 3b 0d 0a 20 20 20 20 20 20 78 20 3d  ter);..      x =
ad170 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
ad180 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
ad190 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0d 0a 20  r.size4x & 2;.. 
ad1a0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
ad1b0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
ad1c0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d  u.hdr.size4x = m
ad1d0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  em3.szMaster*4 |
ad1e0 20 78 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 33 2e   x;..      mem3.
ad1f0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
ad200 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
ad210 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
ad220 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  ze = mem3.szMast
ad230 65 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  er;..    }..    
ad240 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
ad250 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
ad260 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
ad270 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65  ..    while( (me
ad280 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
ad290 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
ad2a0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
ad2b0 65 34 78 26 31 29 3d 3d 30 20 29 7b 0d 0a 20 20  e4x&1)==0 ){..  
ad2c0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
ad2d0 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  k(mem3.iMaster+m
ad2e0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0d 0a  em3.szMaster);..
ad2f0 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73        mem3.szMas
ad300 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f  ter += mem3.aPoo
ad310 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
ad320 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
ad330 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0d  u.hdr.size4x/4;.
ad340 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  .      mem3.aPoo
ad350 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31  l[mem3.iMaster-1
ad360 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
ad370 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34   mem3.szMaster*4
ad380 20 7c 20 78 3b 0d 0a 20 20 20 20 20 20 6d 65 6d   | x;..      mem
ad390 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
ad3a0 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
ad3b0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
ad3c0 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61  Size = mem3.szMa
ad3d0 73 74 65 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ster;..    }..  
ad3e0 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  }..}..../*..** R
ad3f0 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
ad400 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  f an outstanding
ad410 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
ad420 62 79 74 65 73 2e 20 20 54 68 65 0d 0a 2a 2a 20  bytes.  The..** 
ad430 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d  size returned om
ad440 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68  its the 8-byte h
ad450 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20  eader overhead. 
ad460 20 54 68 69 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 77   This only..** w
ad470 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20  orks for chunks 
ad480 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74  that are current
ad490 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0d  ly checked out..
ad4a0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
ad4b0 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 64  memsys3Size(void
ad4c0 20 2a 70 29 7b 0d 0a 20 20 4d 65 6d 33 42 6c 6f   *p){..  Mem3Blo
ad4d0 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0d 0a 20 20 69  ck *pBlock;..  i
ad4e0 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
ad4f0 20 30 3b 0d 0a 20 20 70 42 6c 6f 63 6b 20 3d 20   0;..  pBlock = 
ad500 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0d 0a  (Mem3Block*)p;..
ad510 20 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f 63    assert( (pBloc
ad520 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  k[-1].u.hdr.size
ad530 34 78 26 31 29 21 3d 30 20 29 3b 0d 0a 20 20 72  4x&1)!=0 );..  r
ad540 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31  eturn (pBlock[-1
ad550 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e  ].u.hdr.size4x&~
ad560 33 29 2a 32 20 2d 20 34 3b 0d 0a 7d 0d 0a 0d 0a  3)*2 - 4;..}....
ad570 2f 2a 0d 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  /*..** Round up 
ad580 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
ad590 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
ad5a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
ad5b0 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
ad5c0 74 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70  t memsys3Roundup
ad5d0 28 69 6e 74 20 6e 29 7b 0d 0a 20 20 69 66 28 20  (int n){..  if( 
ad5e0 6e 3c 3d 31 32 20 29 7b 0d 0a 20 20 20 20 72 65  n<=12 ){..    re
ad5f0 74 75 72 6e 20 31 32 3b 0d 0a 20 20 7d 65 6c 73  turn 12;..  }els
ad600 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 28  e{..    return (
ad610 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 3b 0d  (n+11)&~7) - 4;.
ad620 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
ad630 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
ad640 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f  s of memory...*/
ad650 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  ..static void *m
ad660 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74  emsys3Malloc(int
ad670 20 6e 42 79 74 65 73 29 7b 0d 0a 20 20 73 71 6c   nBytes){..  sql
ad680 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0d 0a  ite3_int64 *p;..
ad690 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73    assert( nBytes
ad6a0 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f  >0 );          /
ad6b0 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65  * malloc.c filte
ad6c0 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65  rs out 0 byte re
ad6d0 71 75 65 73 74 73 20 2a 2f 0d 0a 20 20 6d 65 6d  quests */..  mem
ad6e0 73 79 73 33 45 6e 74 65 72 28 29 3b 0d 0a 20 20  sys3Enter();..  
ad6f0 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  p = memsys3Mallo
ad700 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
ad710 0d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65  ..  memsys3Leave
ad720 28 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 76  ();..  return (v
ad730 6f 69 64 2a 29 70 3b 20 0d 0a 7d 0d 0a 0d 0a 2f  oid*)p; ..}..../
ad740 2a 0d 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  *..** Free memor
ad750 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  y...*/..static v
ad760 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28  oid memsys3Free(
ad770 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0d 0a  void *pPrior){..
ad780 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
ad790 20 29 3b 0d 0a 20 20 6d 65 6d 73 79 73 33 45 6e   );..  memsys3En
ad7a0 74 65 72 28 29 3b 0d 0a 20 20 6d 65 6d 73 79 73  ter();..  memsys
ad7b0 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69  3FreeUnsafe(pPri
ad7c0 6f 72 29 3b 0d 0a 20 20 6d 65 6d 73 79 73 33 4c  or);..  memsys3L
ad7d0 65 61 76 65 28 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  eave();..}..../*
ad7e0 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  ..** Change the 
ad7f0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
ad800 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
ad810 61 74 69 6f 6e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ation..*/..stati
ad820 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52  c void *memsys3R
ad830 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
ad840 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29  ior, int nBytes)
ad850 7b 0d 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0d 0a  {..  int nOld;..
ad860 20 20 76 6f 69 64 20 2a 70 3b 0d 0a 20 20 69 66    void *p;..  if
ad870 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0d 0a  ( pPrior==0 ){..
ad880 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
ad890 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73  e3_malloc(nBytes
ad8a0 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6e  );..  }..  if( n
ad8b0 42 79 74 65 73 3c 3d 30 20 29 7b 0d 0a 20 20 20  Bytes<=0 ){..   
ad8c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
ad8d0 72 69 6f 72 29 3b 0d 0a 20 20 20 20 72 65 74 75  rior);..    retu
ad8e0 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 6e 4f  rn 0;..  }..  nO
ad8f0 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65  ld = memsys3Size
ad900 28 70 50 72 69 6f 72 29 3b 0d 0a 20 20 69 66 28  (pPrior);..  if(
ad910 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26   nBytes<=nOld &&
ad920 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32   nBytes>=nOld-12
ad930 38 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  8 ){..    return
ad940 20 70 50 72 69 6f 72 3b 0d 0a 20 20 7d 0d 0a 20   pPrior;..  }.. 
ad950 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
ad960 0d 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d  ..  p = memsys3M
ad970 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
ad980 65 73 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b  es);..  if( p ){
ad990 0d 0a 20 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e  ..    if( nOld<n
ad9a0 42 79 74 65 73 20 29 7b 0d 0a 20 20 20 20 20 20  Bytes ){..      
ad9b0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72  memcpy(p, pPrior
ad9c0 2c 20 6e 4f 6c 64 29 3b 0d 0a 20 20 20 20 7d 65  , nOld);..    }e
ad9d0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 63  lse{..      memc
ad9e0 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 42  py(p, pPrior, nB
ad9f0 79 74 65 73 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ytes);..    }.. 
ada00 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e     memsys3FreeUn
ada10 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0d 0a 20  safe(pPrior);.. 
ada20 20 7d 0d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61   }..  memsys3Lea
ada30 76 65 28 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ve();..  return 
ada40 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
ada50 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
ada60 6d 6f 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  module...*/..sta
ada70 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49  tic int memsys3I
ada80 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
ada90 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  d){..  UNUSED_PA
adaa0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
adab0 3b 0d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ;..  if( !sqlite
adac0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48  3GlobalConfig.pH
adad0 65 61 70 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  eap ){..    retu
adae0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
adaf0 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 74  ..  }....  /* St
adb00 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ore a pointer to
adb10 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c 6f 63   the memory bloc
adb20 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74 72 75  k in global stru
adb30 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f 0d 0a  cture mem3. */..
adb40 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
adb50 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29  (Mem3Block)==8 )
adb60 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20  ;..  mem3.aPool 
adb70 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73  = (Mem3Block *)s
adb80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
adb90 69 67 2e 70 48 65 61 70 3b 0d 0a 20 20 6d 65 6d  ig.pHeap;..  mem
adba0 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74  3.nPool = (sqlit
adbb0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
adbc0 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65  Heap / sizeof(Me
adbd0 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0d 0a  m3Block)) - 2;..
adbe0 0d 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
adbf0 65 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f  e the master blo
adc00 63 6b 2e 20 2a 2f 0d 0a 20 20 6d 65 6d 33 2e 73  ck. */..  mem3.s
adc10 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e  zMaster = mem3.n
adc20 50 6f 6f 6c 3b 0d 0a 20 20 6d 65 6d 33 2e 6d 6e  Pool;..  mem3.mn
adc30 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a  Master = mem3.sz
adc40 4d 61 73 74 65 72 3b 0d 0a 20 20 6d 65 6d 33 2e  Master;..  mem3.
adc50 69 4d 61 73 74 65 72 20 3d 20 31 3b 0d 0a 20 20  iMaster = 1;..  
adc60 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e  mem3.aPool[0].u.
adc70 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65  hdr.size4x = (me
adc80 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20  m3.szMaster<<2) 
adc90 2b 20 32 3b 0d 0a 20 20 6d 65 6d 33 2e 61 50 6f  + 2;..  mem3.aPo
adca0 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75  ol[mem3.nPool].u
adcb0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20  .hdr.prevSize = 
adcc0 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0d 0a 20 20 6d  mem3.nPool;..  m
adcd0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e  em3.aPool[mem3.n
adce0 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  Pool].u.hdr.size
adcf0 34 78 20 3d 20 31 3b 0d 0a 0d 0a 20 20 72 65 74  4x = 1;....  ret
add00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
add10 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 69 6e  }..../*..** Dein
add20 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
add30 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  dule...*/..stati
add40 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68  c void memsys3Sh
add50 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
add60 55 73 65 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44  Used){..  UNUSED
add70 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
add80 65 64 29 3b 0d 0a 20 20 6d 65 6d 33 2e 6d 75 74  ed);..  mem3.mut
add90 65 78 20 3d 20 30 3b 0d 0a 20 20 72 65 74 75 72  ex = 0;..  retur
adda0 6e 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d  n;..}......../*.
addb0 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c  .** Open the fil
addc0 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20  e indicated and 
addd0 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61  write a log of a
adde0 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72  ll unfreed memor
addf0 79 20 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  y ..** allocatio
ade00 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67  ns into that log
ade10 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
ade20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ade30 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 6f  e3Memsys3Dump(co
ade40 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
ade50 61 6d 65 29 7b 0d 0a 23 69 66 64 65 66 20 53 51  ame){..#ifdef SQ
ade60 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 46 49  LITE_DEBUG..  FI
ade70 4c 45 20 2a 6f 75 74 3b 0d 0a 20 20 75 33 32 20  LE *out;..  u32 
ade80 69 2c 20 6a 3b 0d 0a 20 20 75 33 32 20 73 69 7a  i, j;..  u32 siz
ade90 65 3b 0d 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  e;..  if( zFilen
adea0 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
adeb0 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0d 0a 20 20  ame[0]==0 ){..  
adec0 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0d    out = stdout;.
aded0 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6f  .  }else{..    o
adee0 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65  ut = fopen(zFile
adef0 6e 61 6d 65 2c 20 22 77 22 29 3b 0d 0a 20 20 20  name, "w");..   
adf00 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0d 0a   if( out==0 ){..
adf10 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
adf20 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65  derr, "** Unable
adf30 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72   to output memor
adf40 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c  y debug output l
adf50 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0d 0a 20  og: %s **\n",.. 
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf70 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b       zFilename);
adf80 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d  ..      return;.
adf90 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 6d  .    }..  }..  m
adfa0 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0d 0a  emsys3Enter();..
adfb0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
adfc0 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0d 0a 20 20  CHUNKS:\n");..  
adfd0 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33  for(i=1; i<=mem3
adfe0 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f  .nPool; i+=size/
adff0 34 29 7b 0d 0a 20 20 20 20 73 69 7a 65 20 3d 20  4){..    size = 
ae000 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
ae010 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0d 0a 20  u.hdr.size4x;.. 
ae020 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31     if( size/4<=1
ae030 20 29 7b 0d 0a 20 20 20 20 20 20 66 70 72 69 6e   ){..      fprin
ae040 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a 65  tf(out, "%p size
ae050 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33   error\n", &mem3
ae060 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0d 0a 20 20 20  .aPool[i]);..   
ae070 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0d     assert( 0 );.
ae080 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
ae090 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 28 73     }..    if( (s
ae0a0 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d  ize&1)==0 && mem
ae0b0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34  3.aPool[i+size/4
ae0c0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
ae0d0 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0d 0a 20  ze!=size/4 ){.. 
ae0e0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
ae0f0 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20  , "%p tail size 
ae100 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e  does not match\n
ae110 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ", &mem3.aPool[i
ae120 5d 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  ]);..      asser
ae130 74 28 20 30 20 29 3b 0d 0a 20 20 20 20 20 20 62  t( 0 );..      b
ae140 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
ae150 20 20 69 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f    if( ((mem3.aPo
ae160 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75  ol[i+size/4-1].u
ae170 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e  .hdr.size4x&2)>>
ae180 31 29 21 3d 28 73 69 7a 65 26 31 29 20 29 7b 0d  1)!=(size&1) ){.
ae190 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
ae1a0 75 74 2c 20 22 25 70 20 74 61 69 6c 20 63 68 65  ut, "%p tail che
ae1b0 63 6b 6f 75 74 20 62 69 74 20 69 73 20 69 6e 63  ckout bit is inc
ae1c0 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d 65 6d 33  orrect\n", &mem3
ae1d0 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0d 0a 20 20 20  .aPool[i]);..   
ae1e0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0d     assert( 0 );.
ae1f0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
ae200 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73 69     }..    if( si
ae210 7a 65 26 31 20 29 7b 0d 0a 20 20 20 20 20 20 66  ze&1 ){..      f
ae220 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20  printf(out, "%p 
ae230 25 36 64 20 62 79 74 65 73 20 63 68 65 63 6b 65  %6d bytes checke
ae240 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e  d out\n", &mem3.
ae250 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f  aPool[i], (size/
ae260 34 29 2a 38 2d 38 29 3b 0d 0a 20 20 20 20 7d 65  4)*8-8);..    }e
ae270 6c 73 65 7b 0d 0a 20 20 20 20 20 20 66 70 72 69  lse{..      fpri
ae280 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64  ntf(out, "%p %6d
ae290 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22   bytes free%s\n"
ae2a0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
ae2b0 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0d  , (size/4)*8-8,.
ae2c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ae2d0 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74     i==mem3.iMast
ae2e0 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a  er ? " **master*
ae2f0 2a 22 20 3a 20 22 22 29 3b 0d 0a 20 20 20 20 7d  *" : "");..    }
ae300 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69 3d 30  ..  }..  for(i=0
ae310 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20  ; i<MX_SMALL-1; 
ae320 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 6d  i++){..    if( m
ae330 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d  em3.aiSmall[i]==
ae340 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20  0 ) continue;.. 
ae350 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
ae360 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69  "small(%2d):", i
ae370 29 3b 0d 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20  );..    for(j = 
ae380 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b  mem3.aiSmall[i];
ae390 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f   j>0; j=mem3.aPo
ae3a0 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[j].u.list.nex
ae3b0 74 29 7b 0d 0a 20 20 20 20 20 20 66 70 72 69 6e  t){..      fprin
ae3c0 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29  tf(out, " %p(%d)
ae3d0 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a  ", &mem3.aPool[j
ae3e0 5d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ],..            
ae3f0 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d    (mem3.aPool[j-
ae400 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
ae410 34 29 2a 38 2d 38 29 3b 0d 0a 20 20 20 20 7d 0d  4)*8-8);..    }.
ae420 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
ae430 2c 20 22 5c 6e 22 29 3b 20 0d 0a 20 20 7d 0d 0a  , "\n"); ..  }..
ae440 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48    for(i=0; i<N_H
ae450 41 53 48 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  ASH; i++){..    
ae460 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  if( mem3.aiHash[
ae470 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  i]==0 ) continue
ae480 3b 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  ;..    fprintf(o
ae490 75 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a 22  ut, "hash(%2d):"
ae4a0 2c 20 69 29 3b 0d 0a 20 20 20 20 66 6f 72 28 6a  , i);..    for(j
ae4b0 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69   = mem3.aiHash[i
ae4c0 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61  ]; j>0; j=mem3.a
ae4d0 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[j].u.list.n
ae4e0 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 66 70 72  ext){..      fpr
ae4f0 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25  intf(out, " %p(%
ae500 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  d)", &mem3.aPool
ae510 5b 6a 5d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  [j],..          
ae520 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b      (mem3.aPool[
ae530 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  j-1].u.hdr.size4
ae540 78 2f 34 29 2a 38 2d 38 29 3b 0d 0a 20 20 20 20  x/4)*8-8);..    
ae550 7d 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  }..    fprintf(o
ae560 75 74 2c 20 22 5c 6e 22 29 3b 20 0d 0a 20 20 7d  ut, "\n"); ..  }
ae570 0d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ..  fprintf(out,
ae580 20 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20   "master=%d\n", 
ae590 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0d 0a  mem3.iMaster);..
ae5a0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
ae5b0 6e 6f 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d  nowUsed=%d\n", m
ae5c0 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65  em3.nPool*8 - me
ae5d0 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0d  m3.szMaster*8);.
ae5e0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
ae5f0 22 6d 78 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d  "mxUsed=%d\n", m
ae600 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65  em3.nPool*8 - me
ae610 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0d  m3.mnMaster*8);.
ae620 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
ae630 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65  _leave(mem3.mute
ae640 78 29 3b 0d 0a 20 20 69 66 28 20 6f 75 74 3d 3d  x);..  if( out==
ae650 73 74 64 6f 75 74 20 29 7b 0d 0a 20 20 20 20 66  stdout ){..    f
ae660 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0d 0a  flush(stdout);..
ae670 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 66 63    }else{..    fc
ae680 6c 6f 73 65 28 6f 75 74 29 3b 0d 0a 20 20 7d 0d  lose(out);..  }.
ae690 0a 23 65 6c 73 65 0d 0a 20 20 55 4e 55 53 45 44  .#else..  UNUSED
ae6a0 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65  _PARAMETER(zFile
ae6b0 6e 61 6d 65 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  name);..#endif..
ae6c0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
ae6d0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
ae6e0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  only routine in 
ae6f0 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65  this file with e
ae700 78 74 65 72 6e 61 6c 20 0d 0a 2a 2a 20 6c 69 6e  xternal ..** lin
ae710 6b 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 6f  kage...**..** Po
ae720 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c  pulate the low-l
ae730 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  evel memory allo
ae740 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  cation function 
ae750 70 6f 69 6e 74 65 72 73 20 69 6e 0d 0a 2a 2a 20  pointers in..** 
ae760 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
ae770 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74  fig.m with point
ae780 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69  ers to the routi
ae790 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
ae7a0 2e 20 54 68 65 0d 0a 2a 2a 20 61 72 67 75 6d 65  . The..** argume
ae7b0 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 65 20  nts specify the 
ae7c0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
ae7d0 74 6f 20 6d 61 6e 61 67 65 2e 0d 0a 2a 2a 0d 0a  to manage...**..
ae7e0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ae7f0 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62  is only called b
ae800 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  y sqlite3_config
ae810 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72  (), and therefor
ae820 65 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71  e..** is not req
ae830 75 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65  uired to be thre
ae840 61 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f  adsafe (it is no
ae850 74 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  t)...*/..SQLITE_
ae860 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
ae870 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
ae880 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
ae890 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0d 0a  Memsys3(void){..
ae8a0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
ae8b0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
ae8c0 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64  ds mempoolMethod
ae8d0 73 20 3d 20 7b 0d 0a 20 20 20 20 20 6d 65 6d 73  s = {..     mems
ae8e0 79 73 33 4d 61 6c 6c 6f 63 2c 0d 0a 20 20 20 20  ys3Malloc,..    
ae8f0 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0d 0a 20   memsys3Free,.. 
ae900 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c      memsys3Reall
ae910 6f 63 2c 0d 0a 20 20 20 20 20 6d 65 6d 73 79 73  oc,..     memsys
ae920 33 53 69 7a 65 2c 0d 0a 20 20 20 20 20 6d 65 6d  3Size,..     mem
ae930 73 79 73 33 52 6f 75 6e 64 75 70 2c 0d 0a 20 20  sys3Roundup,..  
ae940 20 20 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0d     memsys3Init,.
ae950 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 68 75  .     memsys3Shu
ae960 74 64 6f 77 6e 2c 0d 0a 20 20 20 20 20 30 0d 0a  tdown,..     0..
ae970 20 20 7d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 26    };..  return &
ae980 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 3b 0d  mempoolMethods;.
ae990 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  .}....#endif /* 
ae9a0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
ae9b0 4d 53 59 53 33 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a  MSYS3 */..../***
ae9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
ae9d0 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a  of mem3.c ******
ae9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aea00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
aea10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
aea20 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a  in file mem5.c *
aea30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aea40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aea50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
aea60 0d 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65  ..** 2007 Octobe
aea70 72 20 31 34 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  r 14..**..** The
aea80 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
aea90 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
aeaa0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
aeab0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
aeac0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
aead0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
aeae0 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
aeaf0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
aeb00 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
aeb10 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
aeb20 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
aeb30 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
aeb40 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
aeb50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
aeb60 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
aeb70 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
aeb80 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
aeb90 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
aeba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aebb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aebc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aebd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
aebe0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
aebf0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
aec00 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
aec10 6e 74 20 61 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20  nt a memory..** 
aec20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
aec30 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20  stem for use by 
aec40 53 51 4c 69 74 65 2e 20 0d 0a 2a 2a 0d 0a 2a 2a  SQLite. ..**..**
aec50 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
aec60 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
aec70 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
aec80 20 6f 6d 69 74 73 20 61 6c 6c 0d 0a 2a 2a 20 75   omits all..** u
aec90 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20  se of malloc(). 
aeca0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
aecb0 67 69 76 65 73 20 53 51 4c 69 74 65 20 61 20 62  gives SQLite a b
aecc0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0d 0a  lock of memory..
aecd0 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  ** before callin
aece0 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  g sqlite3_initia
aecf0 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69 63  lize() from whic
aed00 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0d 0a 2a  h allocations..*
aed10 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20 72  * are made and r
aed20 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
aed30 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52 65  Malloc() and xRe
aed40 61 6c 6c 6f 63 28 29 20 0d 0a 2a 2a 20 69 6d 70  alloc() ..** imp
aed50 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f 6e  lementations. On
aed60 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ce sqlite3_initi
aed70 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65 6e  alize() has been
aed80 20 63 61 6c 6c 65 64 2c 0d 0a 2a 2a 20 74 68 65   called,..** the
aed90 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
aeda0 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53  y available to S
aedb0 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 61  QLite is fixed a
aedc0 6e 64 20 63 61 6e 6e 6f 74 0d 0a 2a 2a 20 62 65  nd cannot..** be
aedd0 20 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a 0d 0a 2a   changed...**..*
aede0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
aedf0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
aee00 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
aee10 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0d 0a 2a  m is included..*
aee20 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f  * in the build o
aee30 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e  nly if SQLITE_EN
aee40 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20  ABLE_MEMSYS5 is 
aee50 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  defined...**..**
aee60 20 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c   This memory all
aee70 6f 63 61 74 6f 72 20 75 73 65 73 20 74 68 65 20  ocator uses the 
aee80 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f 72 69  following algori
aee90 74 68 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31  thm:..**..**   1
aeea0 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c  .  All memory al
aeeb0 6c 6f 63 61 74 69 6f 6e 73 20 73 69 7a 65 73 20  locations sizes 
aeec0 61 72 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74  are rounded up t
aeed0 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0d  o a power of 2..
aeee0 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 2e 20 20 49 66  .**..**   2.  If
aeef0 20 74 77 6f 20 61 64 6a 61 63 65 6e 74 20 66 72   two adjacent fr
aef00 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20 74 68  ee blocks are th
aef10 65 20 68 61 6c 76 65 73 20 6f 66 20 61 20 6c 61  e halves of a la
aef20 72 67 65 72 20 62 6c 6f 63 6b 2c 0d 0a 2a 2a 20  rger block,..** 
aef30 20 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 74        then the t
aef40 77 6f 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f  wo blocks are co
aef50 61 6c 65 73 65 64 20 69 6e 74 6f 20 74 68 65 20  alesed into the 
aef60 73 69 6e 67 6c 65 20 6c 61 72 67 65 72 20 62 6c  single larger bl
aef70 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33  ock...**..**   3
aef80 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 79 20 69 73  .  New memory is
aef90 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20   allocated from 
aefa0 74 68 65 20 66 69 72 73 74 20 61 76 61 69 6c 61  the first availa
aefb0 62 6c 65 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0d  ble free block..
aefc0 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 61 6c 67  .**..** This alg
aefd0 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 72 69  orithm is descri
aefe0 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52 6f  bed in: J. M. Ro
aeff0 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 66 6f  bson. "Bounds fo
af000 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f 6e 73  r Some Functions
af010 0d 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 20  ..** Concerning 
af020 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 20  Dynamic Storage 
af030 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f 75  Allocation". Jou
af040 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 6f  rnal of the Asso
af050 63 69 61 74 69 6f 6e 20 66 6f 72 0d 0a 2a 2a 20  ciation for..** 
af060 43 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e  Computing Machin
af070 65 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20  ery, Volume 21, 
af080 4e 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31  Number 8, July 1
af090 39 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34  974, pages 491-4
af0a0 39 39 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 4c 65 74  99...** ..** Let
af0b0 20 6e 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f   n be the size o
af0c0 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c  f the largest al
af0d0 6c 6f 63 61 74 69 6f 6e 20 64 69 76 69 64 65 64  location divided
af0e0 20 62 79 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0d   by the minimum.
af0f0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  .** allocation s
af100 69 7a 65 20 28 61 66 74 65 72 20 72 6f 75 6e 64  ize (after round
af110 69 6e 67 20 61 6c 6c 20 73 69 7a 65 73 20 75 70  ing all sizes up
af120 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32   to a power of 2
af130 2e 29 20 20 4c 65 74 20 4d 0d 0a 2a 2a 20 62 65  .)  Let M..** be
af140 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f   the maximum amo
af150 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 76  unt of memory ev
af160 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  er outstanding a
af170 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 4c 65 74  t one time.  Let
af180 0d 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 74 6f  ..** N be the to
af190 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  tal amount of me
af1a0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
af1b0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  or allocation.  
af1c0 52 6f 62 73 6f 6e 0d 0a 2a 2a 20 70 72 6f 76 65  Robson..** prove
af1d0 64 20 74 68 61 74 20 74 68 69 73 20 6d 65 6d 6f  d that this memo
af1e0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 6c  ry allocator wil
af1f0 6c 20 6e 65 76 65 72 20 62 72 65 61 6b 64 6f 77  l never breakdow
af200 6e 20 64 75 65 20 74 6f 20 0d 0a 2a 2a 20 66 72  n due to ..** fr
af210 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c  agmentation as l
af220 6f 6e 67 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f  ong as the follo
af230 77 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20  wing constraint 
af240 68 6f 6c 64 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  holds:..**..**  
af250 20 20 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b      N >=  M*(1 +
af260 20 6c 6f 67 32 28 6e 29 2f 32 29 20 2d 20 6e 20   log2(n)/2) - n 
af270 2b 20 31 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  + 1..**..** The 
af280 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
af290 20 6c 6f 67 69 63 20 74 72 61 63 6b 73 20 74 68   logic tracks th
af2a0 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 73  e maximum values
af2b0 20 6f 66 20 6e 20 61 6e 64 20 4d 20 73 6f 0d 0a   of n and M so..
af2c0 2a 2a 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69  ** that an appli
af2d0 63 61 74 69 6f 6e 20 63 61 6e 2c 20 61 74 20 61  cation can, at a
af2e0 6e 79 20 74 69 6d 65 2c 20 76 65 72 69 66 79 20  ny time, verify 
af2f0 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e  this constraint.
af300 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ..*/..../*..** T
af310 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
af320 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
af330 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  tor is used only
af340 20 77 68 65 6e 20 0d 0a 2a 2a 20 53 51 4c 49 54   when ..** SQLIT
af350 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
af360 20 69 73 20 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f   is defined...*/
af370 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
af380 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0d 0a  ENABLE_MEMSYS5..
af390 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 6d 69 6e 69 6d  ../*..** A minim
af3a0 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  um allocation is
af3b0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
af3c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
af3d0 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 20 4c 61 72  ructure...** Lar
af3e0 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ger allocations 
af3f0 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  are an array of 
af400 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73  these structures
af410 20 77 68 65 72 65 20 74 68 65 0d 0a 2a 2a 20 73   where the..** s
af420 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79  ize of the array
af430 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 32   is a power of 2
af440 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 69  ...**..** The si
af450 7a 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ze of this objec
af460 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  t must be a powe
af470 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20  r of two.  That 
af480 66 61 63 74 20 69 73 0d 0a 2a 2a 20 76 65 72 69  fact is..** veri
af490 66 69 65 64 20 69 6e 20 6d 65 6d 73 79 73 35 49  fied in memsys5I
af4a0 6e 69 74 28 29 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  nit()...*/..type
af4b0 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 35 4c  def struct Mem5L
af4c0 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0d 0a 73  ink Mem5Link;..s
af4d0 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 7b  truct Mem5Link {
af4e0 0d 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20  ..  int next;   
af4f0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
af500 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
af510 2a 2f 0d 0a 20 20 69 6e 74 20 70 72 65 76 3b 20  */..  int prev; 
af520 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
af530 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20  f previous free 
af540 63 68 75 6e 6b 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  chunk */..};....
af550 2f 2a 0d 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73  /*..** Maximum s
af560 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63  ize of any alloc
af570 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f  ation is ((1<<LO
af580 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f  GMAX)*mem5.szAto
af590 6d 29 2e 20 53 69 6e 63 65 0d 0a 2a 2a 20 6d 65  m). Since..** me
af5a0 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 6c 77  m5.szAtom is alw
af5b0 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 20 61  ays at least 8 a
af5c0 6e 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  nd 32-bit intege
af5d0 72 73 20 61 72 65 20 75 73 65 64 2c 0d 0a 2a 2a  rs are used,..**
af5e0 20 69 74 20 69 73 20 6e 6f 74 20 61 63 74 75 61   it is not actua
af5f0 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  lly possible to 
af600 72 65 61 63 68 20 74 68 69 73 20 6c 69 6d 69 74  reach this limit
af610 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4c  ...*/..#define L
af620 4f 47 4d 41 58 20 33 30 0d 0a 0d 0a 2f 2a 0d 0a  OGMAX 30..../*..
af630 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f  ** Masks used fo
af640 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65  r mem5.aCtrl[] e
af650 6c 65 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 23 64  lements...*/..#d
af660 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49  efine CTRL_LOGSI
af670 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c  ZE  0x1f    /* L
af680 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73  og2 Size of this
af690 20 62 6c 6f 63 6b 20 2a 2f 0d 0a 23 64 65 66 69   block */..#defi
af6a0 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20 20  ne CTRL_FREE    
af6b0 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75 65   0x20    /* True
af6c0 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64 20   if not checked 
af6d0 6f 75 74 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  out */..../*..**
af6e0 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74   All of the stat
af6f0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65  ic variables use
af700 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65  d by this module
af710 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0d 0a   are collected..
af720 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
af730 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64   structure named
af740 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69   "mem5".  This i
af750 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0d 0a 2a  s to keep the..*
af760 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  * static variabl
af770 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64  es organized and
af780 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73   to reduce names
af790 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0d 0a  pace pollution..
af7a0 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64  ** when this mod
af7b0 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20  ule is combined 
af7c0 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68  with other in th
af7d0 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0d  e amalgamation..
af7e0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 53 51 4c 49  .*/..static SQLI
af7f0 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65  TE_WSD struct Me
af800 6d 35 47 6c 6f 62 61 6c 20 7b 0d 0a 20 20 2f 2a  m5Global {..  /*
af810 0d 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76  ..  ** Memory av
af820 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f  ailable for allo
af830 63 61 74 69 6f 6e 0d 0a 20 20 2a 2f 0d 0a 20 20  cation..  */..  
af840 69 6e 74 20 73 7a 41 74 6f 6d 3b 20 20 20 20 20  int szAtom;     
af850 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73   /* Smallest pos
af860 73 69 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  sible allocation
af870 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20   in bytes */..  
af880 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20  int nBlock;     
af890 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 7a   /* Number of sz
af8a0 41 74 6f 6d 20 73 69 7a 65 64 20 62 6c 6f 63 6b  Atom sized block
af8b0 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0d 0a 20  s in zPool */.. 
af8c0 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20 20 20 20   u8 *zPool;     
af8d0 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69    /* Memory avai
af8e0 6c 61 62 6c 65 20 74 6f 20 62 65 20 61 6c 6c 6f  lable to be allo
af8f0 63 61 74 65 64 20 2a 2f 0d 0a 20 20 0d 0a 20 20  cated */..  ..  
af900 2f 2a 0d 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74  /*..  ** Mutex t
af910 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
af920 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
af930 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
af940 74 65 6d 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71  tem...  */..  sq
af950 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
af960 65 78 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  ex;....  /*..  *
af970 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74  * Performance st
af980 61 74 69 73 74 69 63 73 0d 0a 20 20 2a 2f 0d 0a  atistics..  */..
af990 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20    u64 nAlloc;   
af9a0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
af9b0 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74  umber of calls t
af9c0 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0d 0a 20 20 75  o malloc */..  u
af9d0 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20  64 totalAlloc;  
af9e0 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61     /* Total of a
af9f0 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20  ll malloc calls 
afa00 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72  - includes inter
afa10 6e 61 6c 20 66 72 61 67 20 2a 2f 0d 0a 20 20 75  nal frag */..  u
afa20 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b 20  64 totalExcess; 
afa30 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65     /* Total inte
afa40 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69  rnal fragmentati
afa50 6f 6e 20 2a 2f 0d 0a 20 20 75 33 32 20 63 75 72  on */..  u32 cur
afa60 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20  rentOut;     /* 
afa70 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74  Current checkout
afa80 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65  , including inte
afa90 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69  rnal fragmentati
afaa0 6f 6e 20 2a 2f 0d 0a 20 20 75 33 32 20 63 75 72  on */..  u32 cur
afab0 72 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20  rentCount;   /* 
afac0 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  Current number o
afad0 66 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b  f distinct check
afae0 6f 75 74 73 20 2a 2f 0d 0a 20 20 75 33 32 20 6d  outs */..  u32 m
afaf0 61 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f  axOut;         /
afb00 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e  * Maximum instan
afb10 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f  taneous currentO
afb20 75 74 20 2a 2f 0d 0a 20 20 75 33 32 20 6d 61 78  ut */..  u32 max
afb30 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20  Count;       /* 
afb40 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61  Maximum instanta
afb50 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 43 6f 75  neous currentCou
afb60 6e 74 20 2a 2f 0d 0a 20 20 75 33 32 20 6d 61 78  nt */..  u32 max
afb70 52 65 71 75 65 73 74 3b 20 20 20 20 20 2f 2a 20  Request;     /* 
afb80 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69  Largest allocati
afb90 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66  on (exclusive of
afba0 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 29 20   internal frag) 
afbb0 2a 2f 0d 0a 20 20 0d 0a 20 20 2f 2a 0d 0a 20 20  */..  ..  /*..  
afbc0 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65  ** Lists of free
afbd0 20 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65   blocks.  aiFree
afbe0 6c 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73  list[0] is a lis
afbf0 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  t of free blocks
afc00 20 6f 66 0d 0a 20 20 2a 2a 20 73 69 7a 65 20 6d   of..  ** size m
afc10 65 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46  em5.szAtom.  aiF
afc20 72 65 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73  reelist[1] holds
afc30 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20   blocks of size 
afc40 73 7a 41 74 6f 6d 2a 32 2e 0d 0a 20 20 2a 2a 20  szAtom*2...  ** 
afc50 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0d 0a 20  and so forth... 
afc60 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 69 46 72 65   */..  int aiFre
afc70 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b  elist[LOGMAX+1];
afc80 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 53  ....  /*..  ** S
afc90 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e  pace for trackin
afca0 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61  g which blocks a
afcb0 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  re checked out a
afcc0 6e 64 20 74 68 65 20 73 69 7a 65 0d 0a 20 20 2a  nd the size..  *
afcd0 2a 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e  * of each block.
afce0 20 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62    One byte per b
afcf0 6c 6f 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75  lock...  */..  u
afd00 38 20 2a 61 43 74 72 6c 3b 0d 0a 0d 0a 7d 20 6d  8 *aCtrl;....} m
afd10 65 6d 35 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  em5;..../*..** A
afd20 63 63 65 73 73 20 74 68 65 20 73 74 61 74 69 63  ccess the static
afd30 20 76 61 72 69 61 62 6c 65 20 74 68 72 6f 75 67   variable throug
afd40 68 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 53 51  h a macro for SQ
afd50 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0d 0a 2a  LITE_OMIT_WSD..*
afd60 2f 0d 0a 23 64 65 66 69 6e 65 20 6d 65 6d 35 20  /..#define mem5 
afd70 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65  GLOBAL(struct Me
afd80 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0d  m5Global, mem5).
afd90 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 73 73 75 6d 69  .../*..** Assumi
afda0 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73  ng mem5.zPool is
afdb0 20 64 69 76 69 64 65 64 20 75 70 20 69 6e 74 6f   divided up into
afdc0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4d 65 6d   an array of Mem
afdd0 35 4c 69 6e 6b 0d 0a 2a 2a 20 73 74 72 75 63 74  5Link..** struct
afde0 75 72 65 73 2c 20 72 65 74 75 72 6e 20 61 20 70  ures, return a p
afdf0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 64  ointer to the id
afe00 78 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e 0d 0a  x-th such lik...
afe10 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35  */..#define MEM5
afe20 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 35  LINK(idx) ((Mem5
afe30 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50  Link *)(&mem5.zP
afe40 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73  ool[(idx)*mem5.s
afe50 7a 41 74 6f 6d 5d 29 29 0d 0a 0d 0a 2f 2a 0d 0a  zAtom]))..../*..
afe60 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68  ** Unlink the ch
afe70 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f  unk at mem5.aPoo
afe80 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69  l[i] from list i
afe90 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0d 0a  t is currently..
afea0 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c  ** on.  It shoul
afeb0 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65  d be found on me
afec0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
afed0 6f 67 73 69 7a 65 5d 2e 0d 0a 2a 2f 0d 0a 73 74  ogsize]...*/..st
afee0 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
afef0 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69  5Unlink(int i, i
aff00 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0d 0a 20  nt iLogsize){.. 
aff10 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b   int next, prev;
aff20 0d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ..  assert( i>=0
aff30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
aff40 6b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  k );..  assert( 
aff50 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
aff60 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
aff70 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 6d  );..  assert( (m
aff80 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43  em5.aCtrl[i] & C
aff90 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c  TRL_LOGSIZE)==iL
affa0 6f 67 73 69 7a 65 20 29 3b 0d 0a 0d 0a 20 20 6e  ogsize );....  n
affb0 65 78 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69  ext = MEM5LINK(i
affc0 29 2d 3e 6e 65 78 74 3b 0d 0a 20 20 70 72 65 76  )->next;..  prev
affd0 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e   = MEM5LINK(i)->
affe0 70 72 65 76 3b 0d 0a 20 20 69 66 28 20 70 72 65  prev;..  if( pre
afff0 76 3c 30 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 35  v<0 ){..    mem5
b0000 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
b0010 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0d 0a 20  size] = next;.. 
b0020 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 4d 45 4d   }else{..    MEM
b0030 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78  5LINK(prev)->nex
b0040 74 20 3d 20 6e 65 78 74 3b 0d 0a 20 20 7d 0d 0a  t = next;..  }..
b0050 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b    if( next>=0 ){
b0060 0d 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e  ..    MEM5LINK(n
b0070 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 72 65  ext)->prev = pre
b0080 76 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  v;..  }..}..../*
b0090 0d 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68  ..** Link the ch
b00a0 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f  unk at mem5.aPoo
b00b0 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20  l[i] so that is 
b00c0 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0d  on the iLogsize.
b00d0 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0d 0a  .** free list...
b00e0 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
b00f0 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20  memsys5Link(int 
b0100 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29  i, int iLogsize)
b0110 7b 0d 0a 20 20 69 6e 74 20 78 3b 0d 0a 20 20 61  {..  int x;..  a
b0120 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
b0130 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d  utex_held(mem5.m
b0140 75 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65  utex) );..  asse
b0150 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65  rt( i>=0 && i<me
b0160 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0d 0a 20 20  m5.nBlock );..  
b0170 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
b0180 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
b0190 3d 4c 4f 47 4d 41 58 20 29 3b 0d 0a 20 20 61 73  =LOGMAX );..  as
b01a0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72  sert( (mem5.aCtr
b01b0 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53  l[i] & CTRL_LOGS
b01c0 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29  IZE)==iLogsize )
b01d0 3b 0d 0a 0d 0a 20 20 78 20 3d 20 4d 45 4d 35 4c  ;....  x = MEM5L
b01e0 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d  INK(i)->next = m
b01f0 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  em5.aiFreelist[i
b0200 4c 6f 67 73 69 7a 65 5d 3b 0d 0a 20 20 4d 45 4d  Logsize];..  MEM
b0210 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d  5LINK(i)->prev =
b0220 20 2d 31 3b 0d 0a 20 20 69 66 28 20 78 3e 3d 30   -1;..  if( x>=0
b0230 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
b0240 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29   x<mem5.nBlock )
b0250 3b 0d 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28  ;..    MEM5LINK(
b0260 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0d 0a 20  x)->prev = i;.. 
b0270 20 7d 0d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65   }..  mem5.aiFre
b0280 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
b0290 3d 20 69 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  = i;..}..../*..*
b02a0 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f  * If the STATIC_
b02b0 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  MEM mutex is not
b02c0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f   already held, o
b02d0 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68  btain it now. Th
b02e0 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 77 69 6c 6c  e mutex..** will
b02f0 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64   already be held
b0300 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f   (obtained by co
b0310 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20  de in malloc.c) 
b0320 69 66 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c  if..** sqlite3Gl
b0330 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53  obalConfig.bMemS
b0340 74 61 74 20 69 73 20 74 72 75 65 2e 0d 0a 2a 2f  tat is true...*/
b0350 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  ..static void me
b0360 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29  msys5Enter(void)
b0370 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  {..  sqlite3_mut
b0380 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75  ex_enter(mem5.mu
b0390 74 65 78 29 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63  tex);..}..static
b03a0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61   void memsys5Lea
b03b0 76 65 28 76 6f 69 64 29 7b 0d 0a 20 20 73 71 6c  ve(void){..  sql
b03c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
b03d0 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0d 0a 7d  (mem5.mutex);..}
b03e0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
b03f0 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  n the size of an
b0400 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c   outstanding all
b0410 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65  ocation, in byte
b0420 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 73 69 7a 65  s.  The..** size
b0430 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20   returned omits 
b0440 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65  the 8-byte heade
b0450 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69  r overhead.  Thi
b0460 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 77 6f 72 6b 73  s only..** works
b0470 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74   for chunks that
b0480 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63   are currently c
b0490 68 65 63 6b 65 64 20 6f 75 74 2e 0d 0a 2a 2f 0d  hecked out...*/.
b04a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
b04b0 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  ys5Size(void *p)
b04c0 7b 0d 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d  {..  int iSize =
b04d0 20 30 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d   0;..  if( p ){.
b04e0 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75  .    int i = ((u
b04f0 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c  8 *)p-mem5.zPool
b0500 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0d 0a  )/mem5.szAtom;..
b0510 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
b0520 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
b0530 6b 20 29 3b 0d 0a 20 20 20 20 69 53 69 7a 65 20  k );..    iSize 
b0540 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20  = mem5.szAtom * 
b0550 28 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72  (1 << (mem5.aCtr
b0560 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a  l[i]&CTRL_LOGSIZ
b0570 45 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  E));..  }..  ret
b0580 75 72 6e 20 69 53 69 7a 65 3b 0d 0a 7d 0d 0a 0d  urn iSize;..}...
b0590 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ./*..** Find the
b05a0 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20   first entry on 
b05b0 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f  the freelist iLo
b05c0 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74  gsize.  Unlink t
b05d0 68 61 74 0d 0a 2a 2a 20 65 6e 74 72 79 20 61 6e  hat..** entry an
b05e0 64 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64  d return its ind
b05f0 65 78 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ex. ..*/..static
b0600 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69   int memsys5Unli
b0610 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f 67  nkFirst(int iLog
b0620 73 69 7a 65 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  size){..  int i;
b0630 0d 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0d  ..  int iFirst;.
b0640 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f  ...  assert( iLo
b0650 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67  gsize>=0 && iLog
b0660 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0d  size<=LOGMAX );.
b0670 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20  .  i = iFirst = 
b0680 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
b0690 69 4c 6f 67 73 69 7a 65 5d 3b 0d 0a 20 20 61 73  iLogsize];..  as
b06a0 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20  sert( iFirst>=0 
b06b0 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 69 3e 30  );..  while( i>0
b06c0 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 69 3c 69   ){..    if( i<i
b06d0 46 69 72 73 74 20 29 20 69 46 69 72 73 74 20 3d  First ) iFirst =
b06e0 20 69 3b 0d 0a 20 20 20 20 69 20 3d 20 4d 45 4d   i;..    i = MEM
b06f0 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0d  5LINK(i)->next;.
b0700 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 79 73 35 55  .  }..  memsys5U
b0710 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c 20 69 4c  nlink(iFirst, iL
b0720 6f 67 73 69 7a 65 29 3b 0d 0a 20 20 72 65 74 75  ogsize);..  retu
b0730 72 6e 20 69 46 69 72 73 74 3b 0d 0a 7d 0d 0a 0d  rn iFirst;..}...
b0740 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ./*..** Return a
b0750 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
b0760 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79   of at least nBy
b0770 74 65 73 20 69 6e 20 73 69 7a 65 2e 0d 0a 2a 2a  tes in size...**
b0780 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
b0790 75 6e 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20  unable.  Return 
b07a0 4e 55 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d 3d  NULL if nBytes==
b07b0 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63  0...**..** The c
b07c0 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73  aller guarantees
b07d0 20 74 68 61 74 20 6e 42 79 74 65 20 70 6f 73 69   that nByte posi
b07e0 74 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  tive...**..** Th
b07f0 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74  e caller has obt
b0800 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 72  ained a mutex pr
b0810 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
b0820 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65  this..** routine
b0830 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76   so there is nev
b0840 65 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68  er any chance th
b0850 61 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0d 0a  at two or more..
b0860 2a 2a 20 74 68 72 65 61 64 73 20 63 61 6e 20 62  ** threads can b
b0870 65 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  e in this routin
b0880 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  e at the same ti
b0890 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  me...*/..static 
b08a0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c  void *memsys5Mal
b08b0 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42  locUnsafe(int nB
b08c0 79 74 65 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 20  yte){..  int i; 
b08d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
b08e0 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50 6f  ex of a mem5.aPo
b08f0 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0d 0a 20 20  ol[] slot */..  
b0900 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 20  int iBin;       
b0910 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d   /* Index into m
b0920 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d  em5.aiFreelist[]
b0930 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 46 75 6c 6c   */..  int iFull
b0940 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  Sz;     /* Size 
b0950 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f  of allocation ro
b0960 75 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65  unded up to powe
b0970 72 20 6f 66 20 32 20 2a 2f 0d 0a 20 20 69 6e 74  r of 2 */..  int
b0980 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a   iLogsize;    /*
b0990 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a   Log2 of iFullSz
b09a0 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0d 0a 0d 0a  /POW2_MIN */....
b09b0 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 73 74 20    /* nByte must 
b09c0 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 2a 2f  be a positive */
b09d0 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74  ..  assert( nByt
b09e0 65 3e 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4b  e>0 );....  /* K
b09f0 65 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65  eep track of the
b0a00 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74   maximum allocat
b0a10 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76  ion request.  Ev
b0a20 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0d 0a  en unfulfilled..
b0a30 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72    ** requests ar
b0a40 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0d 0a 20 20  e counted */..  
b0a50 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e 6d  if( (u32)nByte>m
b0a60 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 29  em5.maxRequest )
b0a70 7b 0d 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52  {..    mem5.maxR
b0a80 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0d  equest = nByte;.
b0a90 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 62 6f  .  }....  /* Abo
b0aa0 72 74 20 69 66 20 74 68 65 20 72 65 71 75 65 73  rt if the reques
b0ab0 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ted allocation s
b0ac0 69 7a 65 20 69 73 20 6c 61 72 67 65 72 20 74 68  ize is larger th
b0ad0 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74 0d 0a  an the largest..
b0ae0 20 20 2a 2a 20 70 6f 77 65 72 20 6f 66 20 74 77    ** power of tw
b0af0 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 72 65  o that we can re
b0b00 70 72 65 73 65 6e 74 20 75 73 69 6e 67 20 33 32  present using 32
b0b10 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
b0b20 67 65 72 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  gers...  */..  i
b0b30 66 28 20 6e 42 79 74 65 20 3e 20 30 78 34 30 30  f( nByte > 0x400
b0b40 30 30 30 30 30 20 29 7b 0d 0a 20 20 20 20 72 65  00000 ){..    re
b0b50 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a  turn 0;..  }....
b0b60 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65    /* Round nByte
b0b70 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20   up to the next 
b0b80 76 61 6c 69 64 20 70 6f 77 65 72 20 6f 66 20 74  valid power of t
b0b90 77 6f 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 46 75  wo */..  for(iFu
b0ba0 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d  llSz=mem5.szAtom
b0bb0 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 46  , iLogsize=0; iF
b0bc0 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 46 75  ullSz<nByte; iFu
b0bd0 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 73  llSz *= 2, iLogs
b0be0 69 7a 65 2b 2b 29 7b 7d 0d 0a 0d 0a 20 20 2f 2a  ize++){}....  /*
b0bf0 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e   Make sure mem5.
b0c00 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
b0c10 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74  ize] contains at
b0c20 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0d   least one free.
b0c30 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66  .  ** block.  If
b0c40 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74   not, then split
b0c50 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20   a block of the 
b0c60 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65  next larger powe
b0c70 72 20 6f 66 0d 0a 20 20 2a 2a 20 74 77 6f 20 69  r of..  ** two i
b0c80 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74  n order to creat
b0c90 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f  e a new free blo
b0ca0 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73  ck of size iLogs
b0cb0 69 7a 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f  ize...  */..  fo
b0cc0 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b  r(iBin=iLogsize;
b0cd0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
b0ce0 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e  [iBin]<0 && iBin
b0cf0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b  <=LOGMAX; iBin++
b0d00 29 7b 7d 0d 0a 20 20 69 66 28 20 69 42 69 6e 3e  ){}..  if( iBin>
b0d10 4c 4f 47 4d 41 58 20 29 7b 0d 0a 20 20 20 20 74  LOGMAX ){..    t
b0d20 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33  estcase( sqlite3
b0d30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f  GlobalConfig.xLo
b0d40 67 21 3d 30 20 29 3b 0d 0a 20 20 20 20 73 71 6c  g!=0 );..    sql
b0d50 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f  ite3_log(SQLITE_
b0d60 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20 74  NOMEM, "failed t
b0d70 6f 20 61 6c 6c 6f 63 61 74 65 20 25 75 20 62 79  o allocate %u by
b0d80 74 65 73 22 2c 20 6e 42 79 74 65 29 3b 0d 0a 20  tes", nByte);.. 
b0d90 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
b0da0 7d 0d 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35  }..  i = memsys5
b0db0 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e  UnlinkFirst(iBin
b0dc0 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 69 42 69  );..  while( iBi
b0dd0 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b 0d 0a 20  n>iLogsize ){.. 
b0de0 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0d     int newSize;.
b0df0 0a 0d 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0d 0a  ...    iBin--;..
b0e00 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20      newSize = 1 
b0e10 3c 3c 20 69 42 69 6e 3b 0d 0a 20 20 20 20 6d 65  << iBin;..    me
b0e20 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69  m5.aCtrl[i+newSi
b0e30 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ze] = CTRL_FREE 
b0e40 7c 20 69 42 69 6e 3b 0d 0a 20 20 20 20 6d 65 6d  | iBin;..    mem
b0e50 73 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69  sys5Link(i+newSi
b0e60 7a 65 2c 20 69 42 69 6e 29 3b 0d 0a 20 20 7d 0d  ze, iBin);..  }.
b0e70 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d  .  mem5.aCtrl[i]
b0e80 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0d 0a 0d 0a   = iLogsize;....
b0e90 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f    /* Update allo
b0ea0 63 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  cator performanc
b0eb0 65 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a 2f  e statistics. */
b0ec0 0d 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b  ..  mem5.nAlloc+
b0ed0 2b 3b 0d 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c  +;..  mem5.total
b0ee0 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a  Alloc += iFullSz
b0ef0 3b 0d 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45  ;..  mem5.totalE
b0f00 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a  xcess += iFullSz
b0f10 20 2d 20 6e 42 79 74 65 3b 0d 0a 20 20 6d 65 6d   - nByte;..  mem
b0f20 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b  5.currentCount++
b0f30 3b 0d 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ;..  mem5.curren
b0f40 74 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b  tOut += iFullSz;
b0f50 0d 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78  ..  if( mem5.max
b0f60 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65  Count<mem5.curre
b0f70 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d  ntCount ) mem5.m
b0f80 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63  axCount = mem5.c
b0f90 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0d 0a 20 20  urrentCount;..  
b0fa0 69 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c  if( mem5.maxOut<
b0fb0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
b0fc0 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20  ) mem5.maxOut = 
b0fd0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b  mem5.currentOut;
b0fe0 0d 0a 0d 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  ....  /* Return 
b0ff0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
b1000 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
b1010 79 2e 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20  y. */..  return 
b1020 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f  (void*)&mem5.zPo
b1030 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d  ol[i*mem5.szAtom
b1040 5d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ];..}..../*..** 
b1050 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64  Free an outstand
b1060 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
b1070 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ation...*/..stat
b1080 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
b1090 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a  reeUnsafe(void *
b10a0 70 4f 6c 64 29 7b 0d 0a 20 20 75 33 32 20 73 69  pOld){..  u32 si
b10b0 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0d 0a 20  ze, iLogsize;.. 
b10c0 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0d 0a 0d 0a   int iBlock;....
b10d0 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20    /* Set iBlock 
b10e0 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  to the index of 
b10f0 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65  the block pointe
b1100 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20  d to by pOld in 
b1110 0d 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79  ..  ** the array
b1120 20 6f 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20   of mem5.szAtom 
b1130 62 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e  byte blocks poin
b1140 74 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a  ted to by mem5.z
b1150 50 6f 6f 6c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  Pool...  */..  i
b1160 42 6c 6f 63 6b 20 3d 20 28 28 75 38 20 2a 29 70  Block = ((u8 *)p
b1170 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f  Old-mem5.zPool)/
b1180 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0d 0a 0d 0a  mem5.szAtom;....
b1190 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
b11a0 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f 6c 64  the pointer pOld
b11b0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c   points to a val
b11c0 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 6c 6f  id, non-free blo
b11d0 63 6b 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ck. */..  assert
b11e0 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 69  ( iBlock>=0 && i
b11f0 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f 63  Block<mem5.nBloc
b1200 6b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  k );..  assert( 
b1210 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
b1220 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41  .zPool)%mem5.szA
b1230 74 6f 6d 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73  tom==0 );..  ass
b1240 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
b1250 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f  [iBlock] & CTRL_
b1260 46 52 45 45 29 3d 3d 30 20 29 3b 0d 0a 0d 0a 20  FREE)==0 );.... 
b1270 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35   iLogsize = mem5
b1280 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26  .aCtrl[iBlock] &
b1290 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0d 0a   CTRL_LOGSIZE;..
b12a0 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67    size = 1<<iLog
b12b0 73 69 7a 65 3b 0d 0a 20 20 61 73 73 65 72 74 28  size;..  assert(
b12c0 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28   iBlock+size-1<(
b12d0 75 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20  u32)mem5.nBlock 
b12e0 29 3b 0d 0a 0d 0a 20 20 6d 65 6d 35 2e 61 43 74  );....  mem5.aCt
b12f0 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54  rl[iBlock] |= CT
b1300 52 4c 5f 46 52 45 45 3b 0d 0a 20 20 6d 65 6d 35  RL_FREE;..  mem5
b1310 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69  .aCtrl[iBlock+si
b1320 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  ze-1] |= CTRL_FR
b1330 45 45 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6d  EE;..  assert( m
b1340 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
b1350 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  >0 );..  assert(
b1360 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74   mem5.currentOut
b1370 3e 3d 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41  >=(size*mem5.szA
b1380 74 6f 6d 29 20 29 3b 0d 0a 20 20 6d 65 6d 35 2e  tom) );..  mem5.
b1390 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0d  currentCount--;.
b13a0 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  .  mem5.currentO
b13b0 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e  ut -= size*mem5.
b13c0 73 7a 41 74 6f 6d 3b 0d 0a 20 20 61 73 73 65 72  szAtom;..  asser
b13d0 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  t( mem5.currentO
b13e0 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72  ut>0 || mem5.cur
b13f0 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b 0d  rentCount==0 );.
b1400 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e  .  assert( mem5.
b1410 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c  currentCount>0 |
b1420 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  | mem5.currentOu
b1430 74 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 6d 65 6d  t==0 );....  mem
b1440 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
b1450 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
b1460 6f 67 73 69 7a 65 3b 0d 0a 20 20 77 68 69 6c 65  ogsize;..  while
b1470 28 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a  ( ALWAYS(iLogsiz
b1480 65 3c 4c 4f 47 4d 41 58 29 20 29 7b 0d 0a 20 20  e<LOGMAX) ){..  
b1490 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0d 0a 20    int iBuddy;.. 
b14a0 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e     if( (iBlock>>
b14b0 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29 7b  iLogsize) & 1 ){
b14c0 0d 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d  ..      iBuddy =
b14d0 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0d   iBlock - size;.
b14e0 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
b14f0 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f     iBuddy = iBlo
b1500 63 6b 20 2b 20 73 69 7a 65 3b 0d 0a 20 20 20 20  ck + size;..    
b1510 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  }..    assert( i
b1520 42 75 64 64 79 3e 3d 30 20 29 3b 0d 0a 20 20 20  Buddy>=0 );..   
b1530 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c   if( (iBuddy+(1<
b1540 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35  <iLogsize))>mem5
b1550 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b  .nBlock ) break;
b1560 0d 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61  ..    if( mem5.a
b1570 43 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43  Ctrl[iBuddy]!=(C
b1580 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
b1590 69 7a 65 29 20 29 20 62 72 65 61 6b 3b 0d 0a 20  ize) ) break;.. 
b15a0 20 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b     memsys5Unlink
b15b0 28 69 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a  (iBuddy, iLogsiz
b15c0 65 29 3b 0d 0a 20 20 20 20 69 4c 6f 67 73 69 7a  e);..    iLogsiz
b15d0 65 2b 2b 3b 0d 0a 20 20 20 20 69 66 28 20 69 42  e++;..    if( iB
b15e0 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29 7b 0d 0a  uddy<iBlock ){..
b15f0 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c        mem5.aCtrl
b1600 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52 4c 5f  [iBuddy] = CTRL_
b1610 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b  FREE | iLogsize;
b1620 0d 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  ..      mem5.aCt
b1630 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b 0d  rl[iBlock] = 0;.
b1640 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20  .      iBlock = 
b1650 69 42 75 64 64 79 3b 0d 0a 20 20 20 20 7d 65 6c  iBuddy;..    }el
b1660 73 65 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 35 2e  se{..      mem5.
b1670 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20  aCtrl[iBlock] = 
b1680 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67  CTRL_FREE | iLog
b1690 73 69 7a 65 3b 0d 0a 20 20 20 20 20 20 6d 65 6d  size;..      mem
b16a0 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20  5.aCtrl[iBuddy] 
b16b0 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  = 0;..    }..   
b16c0 20 73 69 7a 65 20 2a 3d 20 32 3b 0d 0a 20 20 7d   size *= 2;..  }
b16d0 0d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28  ..  memsys5Link(
b16e0 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65  iBlock, iLogsize
b16f0 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
b1700 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20  Allocate nBytes 
b1710 6f 66 20 6d 65 6d 6f 72 79 0d 0a 2a 2f 0d 0a 73  of memory..*/..s
b1720 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
b1730 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42  ys5Malloc(int nB
b1740 79 74 65 73 29 7b 0d 0a 20 20 73 71 6c 69 74 65  ytes){..  sqlite
b1750 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30 3b 0d  3_int64 *p = 0;.
b1760 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30 20  .  if( nBytes>0 
b1770 29 7b 0d 0a 20 20 20 20 6d 65 6d 73 79 73 35 45  ){..    memsys5E
b1780 6e 74 65 72 28 29 3b 0d 0a 20 20 20 20 70 20 3d  nter();..    p =
b1790 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e   memsys5MallocUn
b17a0 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0d 0a 20  safe(nBytes);.. 
b17b0 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28     memsys5Leave(
b17c0 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  );..  }..  retur
b17d0 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0d 0a 7d 0d  n (void*)p; ..}.
b17e0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 6d  .../*..** Free m
b17f0 65 6d 6f 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  emory...**..** T
b1800 68 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d  he outer layer m
b1810 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
b1820 70 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f  prevents this ro
b1830 75 74 69 6e 65 20 66 72 6f 6d 0d 0a 2a 2a 20 62  utine from..** b
b1840 65 69 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68  eing called with
b1850 20 70 50 72 69 6f 72 3d 3d 30 2e 0d 0a 2a 2f 0d   pPrior==0...*/.
b1860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
b1870 73 79 73 35 46 72 65 65 28 76 6f 69 64 20 2a 70  sys5Free(void *p
b1880 50 72 69 6f 72 29 7b 0d 0a 20 20 61 73 73 65 72  Prior){..  asser
b1890 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0d  t( pPrior!=0 );.
b18a0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
b18b0 29 3b 0d 0a 20 20 6d 65 6d 73 79 73 35 46 72 65  );..  memsys5Fre
b18c0 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
b18d0 0d 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  ..  memsys5Leave
b18e0 28 29 3b 20 20 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ();  ..}..../*..
b18f0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69  ** Change the si
b1900 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e  ze of an existin
b1910 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  g memory allocat
b1920 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ion...**..** The
b1930 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 6d   outer layer mem
b1940 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72  ory allocator pr
b1950 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 74  events this rout
b1960 69 6e 65 20 66 72 6f 6d 0d 0a 2a 2a 20 62 65 69  ine from..** bei
b1970 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  ng called with p
b1980 50 72 69 6f 72 3d 3d 30 2e 20 20 0d 0a 2a 2a 0d  Prior==0.  ..**.
b1990 0a 2a 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c  .** nBytes is al
b19a0 77 61 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74  ways a value obt
b19b0 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69  ained from a pri
b19c0 6f 72 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 6d  or call to..** m
b19d0 65 6d 73 79 73 35 52 6f 75 6e 64 28 29 2e 20 20  emsys5Round().  
b19e0 48 65 6e 63 65 20 6e 42 79 74 65 73 20 69 73 20  Hence nBytes is 
b19f0 61 6c 77 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67  always a non-neg
b1a00 61 74 69 76 65 20 70 6f 77 65 72 0d 0a 2a 2a 20  ative power..** 
b1a10 6f 66 20 74 77 6f 2e 20 20 49 66 20 6e 42 79 74  of two.  If nByt
b1a20 65 73 3d 3d 30 20 74 68 61 74 20 6d 65 61 6e 73  es==0 that means
b1a30 20 74 68 61 74 20 61 6e 20 6f 76 65 72 73 69 7a   that an oversiz
b1a40 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d 0a 2a 2a  e allocation..**
b1a50 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   (an allocation 
b1a60 6c 61 72 67 65 72 20 74 68 61 6e 20 30 78 34 30  larger than 0x40
b1a70 30 30 30 30 30 30 29 20 77 61 73 20 72 65 71 75  000000) was requ
b1a80 65 73 74 65 64 20 61 6e 64 20 74 68 69 73 0d 0a  ested and this..
b1a90 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  ** routine shoul
b1aa0 64 20 72 65 74 75 72 6e 20 30 20 77 69 74 68 6f  d return 0 witho
b1ab0 75 74 20 66 72 65 65 69 6e 67 20 70 50 72 69 6f  ut freeing pPrio
b1ac0 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  r...*/..static v
b1ad0 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c  oid *memsys5Real
b1ae0 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
b1af0 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0d 0a  , int nBytes){..
b1b00 20 20 69 6e 74 20 6e 4f 6c 64 3b 0d 0a 20 20 76    int nOld;..  v
b1b10 6f 69 64 20 2a 70 3b 0d 0a 20 20 61 73 73 65 72  oid *p;..  asser
b1b20 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0d  t( pPrior!=0 );.
b1b30 0a 20 20 61 73 73 65 72 74 28 20 28 6e 42 79 74  .  assert( (nByt
b1b40 65 73 26 28 6e 42 79 74 65 73 2d 31 29 29 3d 3d  es&(nBytes-1))==
b1b50 30 20 29 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 34  0 );  /* EV: R-4
b1b60 36 31 39 39 2d 33 30 32 34 39 20 2a 2f 0d 0a 20  6199-30249 */.. 
b1b70 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
b1b80 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79  =0 );..  if( nBy
b1b90 74 65 73 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  tes==0 ){..    r
b1ba0 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
b1bb0 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 53   nOld = memsys5S
b1bc0 69 7a 65 28 70 50 72 69 6f 72 29 3b 0d 0a 20 20  ize(pPrior);..  
b1bd0 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64  if( nBytes<=nOld
b1be0 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
b1bf0 70 50 72 69 6f 72 3b 0d 0a 20 20 7d 0d 0a 20 20  pPrior;..  }..  
b1c00 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0d  memsys5Enter();.
b1c10 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61  .  p = memsys5Ma
b1c20 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
b1c30 73 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d  s);..  if( p ){.
b1c40 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
b1c50 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0d 0a 20  Prior, nOld);.. 
b1c60 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e     memsys5FreeUn
b1c70 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0d 0a 20  safe(pPrior);.. 
b1c80 20 7d 0d 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61   }..  memsys5Lea
b1c90 76 65 28 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ve();..  return 
b1ca0 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
b1cb0 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65  Round up a reque
b1cc0 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e  st size to the n
b1cd0 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61  ext valid alloca
b1ce0 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0d 0a  tion size.  If..
b1cf0 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  ** the allocatio
b1d00 6e 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74  n is too large t
b1d10 6f 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20  o be handled by 
b1d20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  this allocation 
b1d30 73 79 73 74 65 6d 2c 0d 0a 2a 2a 20 72 65 74 75  system,..** retu
b1d40 72 6e 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c  rn 0...**..** Al
b1d50 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 75  l allocations mu
b1d60 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
b1d70 20 74 77 6f 20 61 6e 64 20 6d 75 73 74 20 62 65   two and must be
b1d80 20 65 78 70 72 65 73 73 65 64 20 62 79 20 61 0d   expressed by a.
b1d90 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 67 6e 65  .** 32-bit signe
b1da0 64 20 69 6e 74 65 67 65 72 2e 20 20 48 65 6e 63  d integer.  Henc
b1db0 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c  e the largest al
b1dc0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 78 34 30  location is 0x40
b1dd0 30 30 30 30 30 30 0d 0a 2a 2a 20 6f 72 20 31 30  000000..** or 10
b1de0 37 33 37 34 31 38 32 34 20 62 79 74 65 73 2e 0d  73741824 bytes..
b1df0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
b1e00 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69  memsys5Roundup(i
b1e10 6e 74 20 6e 29 7b 0d 0a 20 20 69 6e 74 20 69 46  nt n){..  int iF
b1e20 75 6c 6c 53 7a 3b 0d 0a 20 20 69 66 28 20 6e 20  ullSz;..  if( n 
b1e30 3e 20 30 78 34 30 30 30 30 30 30 30 20 29 20 72  > 0x40000000 ) r
b1e40 65 74 75 72 6e 20 30 3b 0d 0a 20 20 66 6f 72 28  eturn 0;..  for(
b1e50 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41  iFullSz=mem5.szA
b1e60 74 6f 6d 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20  tom; iFullSz<n; 
b1e70 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0d 0a  iFullSz *= 2);..
b1e80 20 20 72 65 74 75 72 6e 20 69 46 75 6c 6c 53 7a    return iFullSz
b1e90 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
b1ea0 65 74 75 72 6e 20 74 68 65 20 63 65 69 6c 69 6e  eturn the ceilin
b1eb0 67 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74  g of the logarit
b1ec0 68 6d 20 62 61 73 65 20 32 20 6f 66 20 69 56 61  hm base 2 of iVa
b1ed0 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 61  lue...**..** Exa
b1ee0 6d 70 6c 65 73 3a 20 20 20 6d 65 6d 73 79 73 35  mples:   memsys5
b1ef0 4c 6f 67 28 31 29 20 2d 3e 20 30 0d 0a 2a 2a 20  Log(1) -> 0..** 
b1f00 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73              mems
b1f10 79 73 35 4c 6f 67 28 32 29 20 2d 3e 20 31 0d 0a  ys5Log(2) -> 1..
b1f20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d  **             m
b1f30 65 6d 73 79 73 35 4c 6f 67 28 34 29 20 2d 3e 20  emsys5Log(4) -> 
b1f40 32 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  2..**           
b1f50 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 35 29 20    memsys5Log(5) 
b1f60 2d 3e 20 33 0d 0a 2a 2a 20 20 20 20 20 20 20 20  -> 3..**        
b1f70 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28       memsys5Log(
b1f80 38 29 20 2d 3e 20 33 0d 0a 2a 2a 20 20 20 20 20  8) -> 3..**     
b1f90 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c          memsys5L
b1fa0 6f 67 28 39 29 20 2d 3e 20 34 0d 0a 2a 2f 0d 0a  og(9) -> 4..*/..
b1fb0 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
b1fc0 73 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65  s5Log(int iValue
b1fd0 29 7b 0d 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0d  ){..  int iLog;.
b1fe0 0a 20 20 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28  .  for(iLog=0; (
b1ff0 69 4c 6f 67 3c 28 69 6e 74 29 28 28 73 69 7a 65  iLog<(int)((size
b2000 6f 66 28 69 6e 74 29 2a 38 29 2d 31 29 29 20 26  of(int)*8)-1)) &
b2010 26 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 6c  & (1<<iLog)<iVal
b2020 75 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0d 0a 20 20  ue; iLog++);..  
b2030 72 65 74 75 72 6e 20 69 4c 6f 67 3b 0d 0a 7d 0d  return iLog;..}.
b2040 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61  .../*..** Initia
b2050 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
b2060 61 6c 6c 6f 63 61 74 6f 72 2e 0d 0a 2a 2a 0d 0a  allocator...**..
b2070 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
b2080 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
b2090 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  e.  The caller m
b20a0 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61  ust be holding a
b20b0 20 6d 75 74 65 78 0d 0a 2a 2a 20 74 6f 20 70 72   mutex..** to pr
b20c0 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20 74  event multiple t
b20d0 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e 74 65  hreads from ente
b20e0 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 6d 65  ring at the same
b20f0 20 74 69 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   time...*/..stat
b2100 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e  ic int memsys5In
b2110 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
b2120 29 7b 0d 0a 20 20 69 6e 74 20 69 69 3b 20 20 20  ){..  int ii;   
b2130 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
b2140 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69   counter */..  i
b2150 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
b2160 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
b2170 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61  ytes of memory a
b2180 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 73  vailable to this
b2190 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0d 0a 20   allocator */.. 
b21a0 20 75 38 20 2a 7a 42 79 74 65 3b 20 20 20 20 20   u8 *zByte;     
b21b0 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73      /* Memory us
b21c0 61 62 6c 65 20 62 79 20 74 68 69 73 20 61 6c 6c  able by this all
b21d0 6f 63 61 74 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74  ocator */..  int
b21e0 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 20 20 20   nMinLog;       
b21f0 2f 2a 20 4c 6f 67 20 62 61 73 65 20 32 20 6f 66  /* Log base 2 of
b2200 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74   minimum allocat
b2210 69 6f 6e 20 73 69 7a 65 20 69 6e 20 62 79 74 65  ion size in byte
b2220 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4f 66 66  s */..  int iOff
b2230 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  set;       /* An
b2240 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 6d 65 6d   offset into mem
b2250 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f 0d 0a 0d 0a  5.aCtrl[] */....
b2260 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
b2270 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 0d 0a  ER(NotUsed);....
b2280 20 20 2f 2a 20 46 6f 72 20 74 68 65 20 70 75 72    /* For the pur
b2290 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f  poses of this ro
b22a0 75 74 69 6e 65 2c 20 64 69 73 61 62 6c 65 20 74  utine, disable t
b22b0 68 65 20 6d 75 74 65 78 20 2a 2f 0d 0a 20 20 6d  he mutex */..  m
b22c0 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0d 0a  em5.mutex = 0;..
b22d0 0d 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20  ..  /* The size 
b22e0 6f 66 20 61 20 4d 65 6d 35 4c 69 6e 6b 20 6f 62  of a Mem5Link ob
b22f0 6a 65 63 74 20 6d 75 73 74 20 62 65 20 61 20 70  ject must be a p
b2300 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 56 65  ower of two.  Ve
b2310 72 69 66 79 20 74 68 61 74 0d 0a 20 20 2a 2a 20  rify that..  ** 
b2320 74 68 69 73 20 69 73 20 63 61 73 65 2e 0d 0a 20  this is case... 
b2330 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28   */..  assert( (
b2340 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29  sizeof(Mem5Link)
b2350 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e  &(sizeof(Mem5Lin
b2360 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0d 0a 0d 0a  k)-1))==0 );....
b2370 20 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65    nByte = sqlite
b2380 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
b2390 65 61 70 3b 0d 0a 20 20 7a 42 79 74 65 20 3d 20  eap;..  zByte = 
b23a0 28 75 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  (u8*)sqlite3Glob
b23b0 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0d  alConfig.pHeap;.
b23c0 0a 20 20 61 73 73 65 72 74 28 20 7a 42 79 74 65  .  assert( zByte
b23d0 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74  !=0 );  /* sqlit
b23e0 65 33 5f 63 6f 6e 66 69 67 28 29 20 64 6f 65 73  e3_config() does
b23f0 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72   not allow other
b2400 77 69 73 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  wise */....  /* 
b2410 62 6f 75 6e 64 61 72 69 65 73 20 6f 6e 20 73 71  boundaries on sq
b2420 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
b2430 67 2e 6d 6e 52 65 71 20 61 72 65 20 65 6e 66 6f  g.mnReq are enfo
b2440 72 63 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f  rced in sqlite3_
b2450 63 6f 6e 66 69 67 28 29 20 2a 2f 0d 0a 20 20 6e  config() */..  n
b2460 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35  MinLog = memsys5
b2470 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  Log(sqlite3Globa
b2480 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0d  lConfig.mnReq);.
b2490 0a 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d  .  mem5.szAtom =
b24a0 20 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0d 0a   (1<<nMinLog);..
b24b0 20 20 77 68 69 6c 65 28 20 28 69 6e 74 29 73 69    while( (int)si
b24c0 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d  zeof(Mem5Link)>m
b24d0 65 6d 35 2e 73 7a 41 74 6f 6d 20 29 7b 0d 0a 20  em5.szAtom ){.. 
b24e0 20 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d     mem5.szAtom =
b24f0 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20   mem5.szAtom << 
b2500 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 6d 65 6d  1;..  }....  mem
b2510 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74  5.nBlock = (nByt
b2520 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d  e / (mem5.szAtom
b2530 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b 0d 0a  +sizeof(u8)));..
b2540 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a    mem5.zPool = z
b2550 42 79 74 65 3b 0d 0a 20 20 6d 65 6d 35 2e 61 43  Byte;..  mem5.aC
b2560 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d  trl = (u8 *)&mem
b2570 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c  5.zPool[mem5.nBl
b2580 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d  ock*mem5.szAtom]
b2590 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b  ;....  for(ii=0;
b25a0 20 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b   ii<=LOGMAX; ii+
b25b0 2b 29 7b 0d 0a 20 20 20 20 6d 65 6d 35 2e 61 69  +){..    mem5.ai
b25c0 46 72 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d  Freelist[ii] = -
b25d0 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 4f 66  1;..  }....  iOf
b25e0 66 73 65 74 20 3d 20 30 3b 0d 0a 20 20 66 6f 72  fset = 0;..  for
b25f0 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e 3d  (ii=LOGMAX; ii>=
b2600 30 3b 20 69 69 2d 2d 29 7b 0d 0a 20 20 20 20 69  0; ii--){..    i
b2610 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c  nt nAlloc = (1<<
b2620 69 69 29 3b 0d 0a 20 20 20 20 69 66 28 20 28 69  ii);..    if( (i
b2630 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d  Offset+nAlloc)<=
b2640 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0d 0a  mem5.nBlock ){..
b2650 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c        mem5.aCtrl
b2660 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 7c  [iOffset] = ii |
b2670 20 43 54 52 4c 5f 46 52 45 45 3b 0d 0a 20 20 20   CTRL_FREE;..   
b2680 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69     memsys5Link(i
b2690 4f 66 66 73 65 74 2c 20 69 69 29 3b 0d 0a 20 20  Offset, ii);..  
b26a0 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e      iOffset += n
b26b0 41 6c 6c 6f 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Alloc;..    }.. 
b26c0 20 20 20 61 73 73 65 72 74 28 28 69 4f 66 66 73     assert((iOffs
b26d0 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e  et+nAlloc)>mem5.
b26e0 6e 42 6c 6f 63 6b 29 3b 0d 0a 20 20 7d 0d 0a 0d  nBlock);..  }...
b26f0 0a 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78  .  /* If a mutex
b2700 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
b2710 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f   normal operatio
b2720 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20  n, allocate one 
b2730 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
b2740 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
b2750 65 6d 73 74 61 74 3d 3d 30 20 29 7b 0d 0a 20 20  emstat==0 ){..  
b2760 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73    mem5.mutex = s
b2770 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
b2780 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
b2790 41 54 49 43 5f 4d 45 4d 29 3b 0d 0a 20 20 7d 0d  ATIC_MEM);..  }.
b27a0 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ...  return SQLI
b27b0 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  TE_OK;..}..../*.
b27c0 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
b27d0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0d 0a 2a   this module...*
b27e0 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  /..static void m
b27f0 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 28 76  emsys5Shutdown(v
b2800 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a  oid *NotUsed){..
b2810 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
b2820 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20  ER(NotUsed);..  
b2830 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0d  mem5.mutex = 0;.
b2840 0a 20 20 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 0d  .  return;..}...
b2850 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
b2860 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e  EST../*..** Open
b2870 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
b2880 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
b2890 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
b28a0 65 64 20 6d 65 6d 6f 72 79 20 0d 0a 2a 2a 20 61  ed memory ..** a
b28b0 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20  llocations into 
b28c0 74 68 61 74 20 6c 6f 67 2e 0d 0a 2a 2f 0d 0a 53  that log...*/..S
b28d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b28e0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73  id sqlite3Memsys
b28f0 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  5Dump(const char
b2900 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0d 0a 20   *zFilename){.. 
b2910 20 46 49 4c 45 20 2a 6f 75 74 3b 0d 0a 20 20 69   FILE *out;..  i
b2920 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0d 0a 20 20 69  nt i, j, n;..  i
b2930 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0d 0a 0d 0a 20  nt nMinLog;.... 
b2940 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d   if( zFilename==
b2950 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30  0 || zFilename[0
b2960 5d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 6f 75 74  ]==0 ){..    out
b2970 20 3d 20 73 74 64 6f 75 74 3b 0d 0a 20 20 7d 65   = stdout;..  }e
b2980 6c 73 65 7b 0d 0a 20 20 20 20 6f 75 74 20 3d 20  lse{..    out = 
b2990 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  fopen(zFilename,
b29a0 20 22 77 22 29 3b 0d 0a 20 20 20 20 69 66 28 20   "w");..    if( 
b29b0 6f 75 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  out==0 ){..     
b29c0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
b29d0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
b29e0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
b29f0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
b2a00 73 20 2a 2a 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  s **\n",..      
b2a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2a20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 20  zFilename);..   
b2a30 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20     return;..    
b2a40 7d 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 79 73  }..  }..  memsys
b2a50 35 45 6e 74 65 72 28 29 3b 0d 0a 20 20 6e 4d 69  5Enter();..  nMi
b2a60 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f  nLog = memsys5Lo
b2a70 67 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b 0d  g(mem5.szAtom);.
b2a80 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c  .  for(i=0; i<=L
b2a90 4f 47 4d 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c  OGMAX && i+nMinL
b2aa0 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b 0d 0a 20 20  og<32; i++){..  
b2ab0 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d    for(n=0, j=mem
b2ac0 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b  5.aiFreelist[i];
b2ad0 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c   j>=0; j = MEM5L
b2ae0 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b  INK(j)->next, n+
b2af0 2b 29 7b 7d 0d 0a 20 20 20 20 66 70 72 69 6e 74  +){}..    fprint
b2b00 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69 73 74  f(out, "freelist
b2b10 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65 20 25   items of size %
b2b20 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73  d: %d\n", mem5.s
b2b30 7a 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0d  zAtom << i, n);.
b2b40 0a 20 20 7d 0d 0a 20 20 66 70 72 69 6e 74 66 28  .  }..  fprintf(
b2b50 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f  out, "mem5.nAllo
b2b60 63 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e  c       = %llu\n
b2b70 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b  ", mem5.nAlloc);
b2b80 0d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ..  fprintf(out,
b2b90 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f   "mem5.totalAllo
b2ba0 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d  c   = %llu\n", m
b2bb0 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b  em5.totalAlloc);
b2bc0 0d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ..  fprintf(out,
b2bd0 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65   "mem5.totalExce
b2be0 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d  ss  = %llu\n", m
b2bf0 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 29  em5.totalExcess)
b2c00 3b 0d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  ;..  fprintf(out
b2c10 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  , "mem5.currentO
b2c20 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  ut   = %u\n", me
b2c30 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b 0d  m5.currentOut);.
b2c40 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
b2c50 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75  "mem5.currentCou
b2c60 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35  nt = %u\n", mem5
b2c70 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0d  .currentCount);.
b2c80 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
b2c90 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20  "mem5.maxOut    
b2ca0 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
b2cb0 2e 6d 61 78 4f 75 74 29 3b 0d 0a 20 20 66 70 72  .maxOut);..  fpr
b2cc0 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
b2cd0 6d 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25  maxCount     = %
b2ce0 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f  u\n", mem5.maxCo
b2cf0 75 6e 74 29 3b 0d 0a 20 20 66 70 72 69 6e 74 66  unt);..  fprintf
b2d00 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52  (out, "mem5.maxR
b2d10 65 71 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22  equest   = %u\n"
b2d20 2c 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73  , mem5.maxReques
b2d30 74 29 3b 0d 0a 20 20 6d 65 6d 73 79 73 35 4c 65  t);..  memsys5Le
b2d40 61 76 65 28 29 3b 0d 0a 20 20 69 66 28 20 6f 75  ave();..  if( ou
b2d50 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0d 0a 20 20  t==stdout ){..  
b2d60 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29    fflush(stdout)
b2d70 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
b2d80 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0d 0a 20   fclose(out);.. 
b2d90 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d   }..}..#endif...
b2da0 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./*..** This rou
b2db0 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
b2dc0 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
b2dd0 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72   file with exter
b2de0 6e 61 6c 20 0d 0a 2a 2a 20 6c 69 6e 6b 61 67 65  nal ..** linkage
b2df0 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 70  . It returns a p
b2e00 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
b2e10 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ic sqlite3_mem_m
b2e20 65 74 68 6f 64 73 0d 0a 2a 2a 20 73 74 72 75 63  ethods..** struc
b2e30 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  t populated with
b2e40 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74   the memsys5 met
b2e50 68 6f 64 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  hods...*/..SQLIT
b2e60 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
b2e70 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
b2e80 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
b2e90 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b  etMemsys5(void){
b2ea0 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ..  static const
b2eb0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
b2ec0 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68  hods memsys5Meth
b2ed0 6f 64 73 20 3d 20 7b 0d 0a 20 20 20 20 20 6d 65  ods = {..     me
b2ee0 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0d 0a 20 20  msys5Malloc,..  
b2ef0 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c 0d     memsys5Free,.
b2f00 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61  .     memsys5Rea
b2f10 6c 6c 6f 63 2c 0d 0a 20 20 20 20 20 6d 65 6d 73  lloc,..     mems
b2f20 79 73 35 53 69 7a 65 2c 0d 0a 20 20 20 20 20 6d  ys5Size,..     m
b2f30 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 2c 0d 0a  emsys5Roundup,..
b2f40 20 20 20 20 20 6d 65 6d 73 79 73 35 49 6e 69 74       memsys5Init
b2f50 2c 0d 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53  ,..     memsys5S
b2f60 68 75 74 64 6f 77 6e 2c 0d 0a 20 20 20 20 20 30  hutdown,..     0
b2f70 0d 0a 20 20 7d 3b 0d 0a 20 20 72 65 74 75 72 6e  ..  };..  return
b2f80 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73   &memsys5Methods
b2f90 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f  ;..}....#endif /
b2fa0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
b2fb0 4d 45 4d 53 59 53 35 20 2a 2f 0d 0a 0d 0a 2f 2a  MEMSYS5 */..../*
b2fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b2fd0 64 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a  d of mem5.c ****
b2fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
b3010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
b3020 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e  egin file mutex.
b3030 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b3040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
b3060 2f 2a 0d 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  /*..** 2007 Augu
b3070 73 74 20 31 34 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  st 14..**..** Th
b3080 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
b3090 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
b30a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
b30b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
b30c0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
b30d0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
b30e0 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
b30f0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
b3100 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
b3110 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
b3120 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
b3130 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
b3140 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
b3150 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b3160 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
b3170 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
b3180 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
b3190 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
b31a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b31b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b31c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b31d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
b31e0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
b31f0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
b3200 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
b3210 65 6e 74 20 6d 75 74 65 78 65 73 2e 0d 0a 2a 2a  ent mutexes...**
b3220 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  ..** This file c
b3230 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
b3240 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f  t is common acro
b3250 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  ss all mutex imp
b3260 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0d 0a 2a  lementations...*
b3270 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64  /....#if defined
b3280 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 26  (SQLITE_DEBUG) &
b3290 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
b32a0 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 0d 0a 2f  E_MUTEX_OMIT)../
b32b0 2a 0d 0a 2a 2a 20 46 6f 72 20 64 65 62 75 67 67  *..** For debugg
b32c0 69 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 72 65  ing purposes, re
b32d0 63 6f 72 64 20 77 68 65 6e 20 74 68 65 20 6d 75  cord when the mu
b32e0 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73  tex subsystem is
b32f0 20 69 6e 69 74 69 61 6c 69 7a 65 64 0d 0a 2a 2a   initialized..**
b3300 20 61 6e 64 20 75 6e 69 6e 69 74 69 61 6c 69 7a   and uninitializ
b3310 65 64 20 73 6f 20 74 68 61 74 20 77 65 20 63 61  ed so that we ca
b3320 6e 20 61 73 73 65 72 74 28 29 20 69 66 20 74 68  n assert() if th
b3330 65 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70  ere is an attemp
b3340 74 20 74 6f 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74  t to..** allocat
b3350 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c 65 20  e a mutex while 
b3360 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 75 6e  the system is un
b3370 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0d 0a 2a 2f  initialized...*/
b3380 0d 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  ..static SQLITE_
b3390 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49  WSD int mutexIsI
b33a0 6e 69 74 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66  nit = 0;..#endif
b33b0 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47   /* SQLITE_DEBUG
b33c0 20 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66   */......#ifndef
b33d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
b33e0 49 54 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69  IT../*..** Initi
b33f0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
b3400 73 79 73 74 65 6d 2e 0d 0a 2a 2f 0d 0a 53 51 4c  system...*/..SQL
b3410 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b3420 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74  sqlite3MutexInit
b3430 28 76 6f 69 64 29 7b 20 0d 0a 20 20 69 6e 74 20  (void){ ..  int 
b3440 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
b3450 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47  .  if( !sqlite3G
b3460 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
b3470 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b  x.xMutexAlloc ){
b3480 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
b3490 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 68  xMutexAlloc meth
b34a0 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  od has not been 
b34b0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 73  set, then the us
b34c0 65 72 20 64 69 64 20 6e 6f 74 0d 0a 20 20 20 20  er did not..    
b34d0 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74  ** install a mut
b34e0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
b34f0 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f 63 6f  n via sqlite3_co
b3500 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74 6f 20  nfig() prior to 
b3510 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  ..    ** sqlite3
b3520 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65  _initialize() be
b3530 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73  ing called. This
b3540 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f   block copies po
b3550 69 6e 74 65 72 73 20 74 6f 0d 0a 20 20 20 20 2a  inters to..    *
b3560 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 6d  * the default im
b3570 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74  plementation int
b3580 6f 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f  o the sqlite3Glo
b3590 62 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74  balConfig struct
b35a0 75 72 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ure...    */..  
b35b0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
b35c0 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
b35d0 46 72 6f 6d 3b 0d 0a 20 20 20 20 73 71 6c 69 74  From;..    sqlit
b35e0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
b35f0 20 2a 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33   *pTo = &sqlite3
b3600 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
b3610 65 78 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 73  ex;....    if( s
b3620 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
b3630 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b  ig.bCoreMutex ){
b3640 0d 0a 20 20 20 20 20 20 70 46 72 6f 6d 20 3d 20  ..      pFrom = 
b3650 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
b3660 74 65 78 28 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  tex();..    }els
b3670 65 7b 0d 0a 20 20 20 20 20 20 70 46 72 6f 6d 20  e{..      pFrom 
b3680 3d 20 73 71 6c 69 74 65 33 4e 6f 6f 70 4d 75 74  = sqlite3NoopMut
b3690 65 78 28 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ex();..    }..  
b36a0 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
b36b0 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28 73 71  rom, offsetof(sq
b36c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
b36d0 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c 6f 63  ods, xMutexAlloc
b36e0 29 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28  ));..    memcpy(
b36f0 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 72 65 65  &pTo->xMutexFree
b3700 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78  , &pFrom->xMutex
b3710 46 72 65 65 2c 0d 0a 20 20 20 20 20 20 20 20 20  Free,..         
b3720 20 20 73 69 7a 65 6f 66 28 2a 70 54 6f 29 20 2d    sizeof(*pTo) -
b3730 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74 65   offsetof(sqlite
b3740 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2c  3_mutex_methods,
b3750 20 78 4d 75 74 65 78 46 72 65 65 29 29 3b 0d 0a   xMutexFree));..
b3760 20 20 20 20 70 54 6f 2d 3e 78 4d 75 74 65 78 41      pTo->xMutexA
b3770 6c 6c 6f 63 20 3d 20 70 46 72 6f 6d 2d 3e 78 4d  lloc = pFrom->xM
b3780 75 74 65 78 41 6c 6c 6f 63 3b 0d 0a 20 20 7d 0d  utexAlloc;..  }.
b3790 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47  .  rc = sqlite3G
b37a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
b37b0 78 2e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0d  x.xMutexInit();.
b37c0 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
b37d0 5f 44 45 42 55 47 0d 0a 20 20 47 4c 4f 42 41 4c  _DEBUG..  GLOBAL
b37e0 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69  (int, mutexIsIni
b37f0 74 29 20 3d 20 31 3b 0d 0a 23 65 6e 64 69 66 0d  t) = 1;..#endif.
b3800 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
b3810 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 68 75  .}..../*..** Shu
b3820 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 20  tdown the mutex 
b3830 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 6c  system. This cal
b3840 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 65  l frees resource
b3850 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0d 0a  s allocated by..
b3860 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  ** sqlite3MutexI
b3870 6e 69 74 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  nit()...*/..SQLI
b3880 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b3890 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76  qlite3MutexEnd(v
b38a0 6f 69 64 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20  oid){..  int rc 
b38b0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
b38c0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
b38d0 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
b38e0 75 74 65 78 45 6e 64 20 29 7b 0d 0a 20 20 20 20  utexEnd ){..    
b38f0 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  rc = sqlite3Glob
b3900 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
b3910 4d 75 74 65 78 45 6e 64 28 29 3b 0d 0a 20 20 7d  MutexEnd();..  }
b3920 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
b3930 45 5f 44 45 42 55 47 0d 0a 20 20 47 4c 4f 42 41  E_DEBUG..  GLOBA
b3940 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e  L(int, mutexIsIn
b3950 69 74 29 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66  it) = 0;..#endif
b3960 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ....  return rc;
b3970 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
b3980 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72  trieve a pointer
b3990 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74   to a static mut
b39a0 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61  ex or allocate a
b39b0 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65   new dynamic one
b39c0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
b39d0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
b39e0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
b39f0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0d 0a 23  lloc(int id){..#
b3a00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
b3a10 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a 20 20 69  IT_AUTOINIT..  i
b3a20 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
b3a30 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e  alize() ) return
b3a40 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 72   0;..#endif..  r
b3a50 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
b3a60 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
b3a70 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b  xMutexAlloc(id);
b3a80 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  ..}....SQLITE_PR
b3a90 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
b3aa0 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65  tex *sqlite3Mute
b3ab0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0d  xAlloc(int id){.
b3ac0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47  .  if( !sqlite3G
b3ad0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
b3ae0 65 4d 75 74 65 78 20 29 7b 0d 0a 20 20 20 20 72  eMutex ){..    r
b3af0 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
b3b00 20 61 73 73 65 72 74 28 20 47 4c 4f 42 41 4c 28   assert( GLOBAL(
b3b10 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74  int, mutexIsInit
b3b20 29 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 73  ) );..  return s
b3b30 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
b3b40 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41  ig.mutex.xMutexA
b3b50 6c 6c 6f 63 28 69 64 29 3b 0d 0a 7d 0d 0a 0d 0a  lloc(id);..}....
b3b60 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 20 64 79  /*..** Free a dy
b3b70 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0d 0a 2a 2f  namic mutex...*/
b3b80 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ..SQLITE_API voi
b3b90 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
b3ba0 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  free(sqlite3_mut
b3bb0 65 78 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70  ex *p){..  if( p
b3bc0 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
b3bd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
b3be0 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29  ex.xMutexFree(p)
b3bf0 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
b3c00 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d  .** Obtain the m
b3c10 75 74 65 78 20 70 2e 20 49 66 20 73 6f 6d 65 20  utex p. If some 
b3c20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 6c 72  other thread alr
b3c30 65 61 64 79 20 68 61 73 20 74 68 65 20 6d 75 74  eady has the mut
b3c40 65 78 2c 20 62 6c 6f 63 6b 0d 0a 2a 2a 20 75 6e  ex, block..** un
b3c50 74 69 6c 20 69 74 20 63 61 6e 20 62 65 20 6f 62  til it can be ob
b3c60 74 61 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  tained...*/..SQL
b3c70 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
b3c80 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
b3c90 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
b3ca0 70 29 7b 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d  p){..  if( p ){.
b3cb0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
b3cc0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
b3cd0 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0d 0a  MutexEnter(p);..
b3ce0 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
b3cf0 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65   Obtain the mute
b3d00 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 66  x p. If successf
b3d10 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ul, return SQLIT
b3d20 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
b3d30 20 69 66 20 61 6e 6f 74 68 65 72 0d 0a 2a 2a 20   if another..** 
b3d40 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
b3d50 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61   mutex and it ca
b3d60 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
b3d70 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
b3d80 42 55 53 59 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  BUSY...*/..SQLIT
b3d90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
b3da0 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69  3_mutex_try(sqli
b3db0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a  te3_mutex *p){..
b3dc0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
b3dd0 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 20 29  E_OK;..  if( p )
b3de0 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  {..    return sq
b3df0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
b3e00 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 54 72  g.mutex.xMutexTr
b3e10 79 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  y(p);..  }..  re
b3e20 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
b3e30 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  *..** The sqlite
b3e40 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
b3e50 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
b3e60 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70  mutex that was p
b3e70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 65 6e  reviously..** en
b3e80 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
b3e90 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
b3ea0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
b3eb0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
b3ec0 78 20 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75  x ..** is not cu
b3ed0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e  rrently entered.
b3ee0 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   If a NULL point
b3ef0 65 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20  er is passed as 
b3f00 61 6e 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20  an argument..** 
b3f10 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
b3f20 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 53   a no-op...*/..S
b3f30 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
b3f40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
b3f50 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
b3f60 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70 20 29   *p){..  if( p )
b3f70 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  {..    sqlite3Gl
b3f80 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
b3f90 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b  .xMutexLeave(p);
b3fa0 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e  ..  }..}....#ifn
b3fb0 64 65 66 20 4e 44 45 42 55 47 0d 0a 2f 2a 0d 0a  def NDEBUG../*..
b3fc0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
b3fd0 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20  utex_held() and 
b3fe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
b3ff0 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20  theld() routine 
b4000 61 72 65 0d 0a 2a 2a 20 69 6e 74 65 6e 64 65 64  are..** intended
b4010 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20   for use inside 
b4020 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
b4030 6e 74 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  nts...*/..SQLITE
b4040 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
b4050 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69  _mutex_held(sqli
b4060 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a  te3_mutex *p){..
b4070 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
b4080 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
b4090 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
b40a0 78 48 65 6c 64 28 70 29 3b 0d 0a 7d 0d 0a 53 51  xHeld(p);..}..SQ
b40b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
b40c0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
b40d0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
b40e0 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20   *p){..  return 
b40f0 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47  p==0 || sqlite3G
b4100 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
b4110 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  x.xMutexNotheld(
b4120 70 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a  p);..}..#endif..
b4130 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
b4140 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
b4150 58 5f 4f 4d 49 54 29 20 2a 2f 0d 0a 0d 0a 2f 2a  X_OMIT) */..../*
b4160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b4170 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a  d of mutex.c ***
b4180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b41a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
b41b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
b41c0 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
b41d0 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
b41e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b41f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
b4200 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  /*..** 2008 Octo
b4210 62 65 72 20 30 37 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ber 07..**..** T
b4220 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
b4230 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
b4240 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
b4250 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
b4260 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
b4270 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
b4280 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
b4290 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
b42a0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
b42b0 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b42c0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
b42d0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
b42e0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
b42f0 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
b4300 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
b4310 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
b4320 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
b4330 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
b4340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
b4380 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
b4390 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
b43a0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
b43b0 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0d 0a 2a  ment mutexes...*
b43c0 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  *..** This imple
b43d0 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 69  mentation in thi
b43e0 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  s file does not 
b43f0 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 75  provide any mutu
b4400 61 6c 0d 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e  al..** exclusion
b4410 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 69   and is thus sui
b4420 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e  table for use on
b4430 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f  ly in applicatio
b4440 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 75 73 65 20  ns..** that use 
b4450 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67  SQLite in a sing
b4460 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
b4470 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
b4480 0d 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c  ..** here are pl
b4490 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70  ace-holders.  Ap
b44a0 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73  plications can s
b44b0 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e  ubstitute workin
b44c0 67 0d 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74  g..** mutex rout
b44d0 69 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69  ines at start-ti
b44e0 6d 65 20 75 73 69 6e 67 20 74 68 65 0d 0a 2a 2a  me using the..**
b44f0 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ..**     sqlite3
b4500 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
b4510 4f 4e 46 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29  ONFIG_MUTEX,...)
b4520 0d 0a 2a 2a 0d 0a 2a 2a 20 69 6e 74 65 72 66 61  ..**..** interfa
b4530 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 63  ce...**..** If c
b4540 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c  ompiled with SQL
b4550 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65 6e 20  ITE_DEBUG, then 
b4560 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 69 63  additional logic
b4570 20 69 73 20 69 6e 73 65 72 74 65 64 0d 0a 2a 2a   is inserted..**
b4580 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f 72   that does error
b4590 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74   checking on mut
b45a0 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72  exes to make sur
b45b0 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e 67  e they are being
b45c0 0d 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72  ..** called corr
b45d0 65 63 74 6c 79 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69  ectly...*/....#i
b45e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  fndef SQLITE_MUT
b45f0 45 58 5f 4f 4d 49 54 0d 0a 0d 0a 23 69 66 6e 64  EX_OMIT....#ifnd
b4600 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
b4610 0a 2f 2a 0d 0a 2a 2a 20 53 74 75 62 20 72 6f 75  ./*..** Stub rou
b4620 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d 75  tines for all mu
b4630 74 65 78 20 6d 65 74 68 6f 64 73 2e 0d 0a 2a 2a  tex methods...**
b4640 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
b4650 65 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75  es provide no mu
b4660 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f  tual exclusion o
b4670 72 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  r error checking
b4680 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
b4690 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28  t noopMutexInit(
b46a0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
b46b0 4c 49 54 45 5f 4f 4b 3b 20 7d 0d 0a 73 74 61 74  LITE_OK; }..stat
b46c0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78  ic int noopMutex
b46d0 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72  End(void){ retur
b46e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0d 0a  n SQLITE_OK; }..
b46f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
b4700 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41  utex *noopMutexA
b4710 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 0d 0a  lloc(int id){ ..
b4720 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
b4730 45 52 28 69 64 29 3b 0d 0a 20 20 72 65 74 75 72  ER(id);..  retur
b4740 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  n (sqlite3_mutex
b4750 2a 29 38 3b 20 0d 0a 7d 0d 0a 73 74 61 74 69 63  *)8; ..}..static
b4760 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 46   void noopMutexF
b4770 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ree(sqlite3_mute
b4780 78 20 2a 70 29 7b 20 55 4e 55 53 45 44 5f 50 41  x *p){ UNUSED_PA
b4790 52 41 4d 45 54 45 52 28 70 29 3b 20 72 65 74 75  RAMETER(p); retu
b47a0 72 6e 3b 20 7d 0d 0a 73 74 61 74 69 63 20 76 6f  rn; }..static vo
b47b0 69 64 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65  id noopMutexEnte
b47c0 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
b47d0 2a 70 29 7b 20 55 4e 55 53 45 44 5f 50 41 52 41  *p){ UNUSED_PARA
b47e0 4d 45 54 45 52 28 70 29 3b 20 72 65 74 75 72 6e  METER(p); return
b47f0 3b 20 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  ; }..static int 
b4800 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 71 6c  noopMutexTry(sql
b4810 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d  ite3_mutex *p){.
b4820 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
b4830 54 45 52 28 70 29 3b 0d 0a 20 20 72 65 74 75 72  TER(p);..  retur
b4840 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
b4850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f  .static void noo
b4860 70 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  pMutexLeave(sqli
b4870 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 55  te3_mutex *p){ U
b4880 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
b4890 70 29 3b 20 72 65 74 75 72 6e 3b 20 7d 0d 0a 0d  p); return; }...
b48a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b48b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
b48c0 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 73 71 6c  thods const *sql
b48d0 69 74 65 33 4e 6f 6f 70 4d 75 74 65 78 28 76 6f  ite3NoopMutex(vo
b48e0 69 64 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63  id){..  static c
b48f0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  onst sqlite3_mut
b4900 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65  ex_methods sMute
b4910 78 20 3d 20 7b 0d 0a 20 20 20 20 6e 6f 6f 70 4d  x = {..    noopM
b4920 75 74 65 78 49 6e 69 74 2c 0d 0a 20 20 20 20 6e  utexInit,..    n
b4930 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0d 0a 20 20  oopMutexEnd,..  
b4940 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63    noopMutexAlloc
b4950 2c 0d 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  ,..    noopMutex
b4960 46 72 65 65 2c 0d 0a 20 20 20 20 6e 6f 6f 70 4d  Free,..    noopM
b4970 75 74 65 78 45 6e 74 65 72 2c 0d 0a 20 20 20 20  utexEnter,..    
b4980 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0d 0a 20  noopMutexTry,.. 
b4990 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76     noopMutexLeav
b49a0 65 2c 0d 0a 0d 0a 20 20 20 20 30 2c 0d 0a 20 20  e,....    0,..  
b49b0 20 20 30 2c 0d 0a 20 20 7d 3b 0d 0a 0d 0a 20 20    0,..  };....  
b49c0 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0d  return &sMutex;.
b49d0 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  .}..#endif /* !S
b49e0 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0d 0a  QLITE_DEBUG */..
b49f0 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
b4a00 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  DEBUG../*..** In
b4a10 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
b4a20 74 69 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 63  tion, error chec
b4a30 6b 69 6e 67 20 69 73 20 70 72 6f 76 69 64 65 64  king is provided
b4a40 20 66 6f 72 20 74 65 73 74 69 6e 67 0d 0a 2a 2a   for testing..**
b4a50 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 70   and debugging p
b4a60 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 6d 75  urposes.  The mu
b4a70 74 65 78 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e  texes still do n
b4a80 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 0d 0a  ot provide any..
b4a90 2a 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73  ** mutual exclus
b4aa0 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  ion...*/..../*..
b4ab0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a  ** The mutex obj
b4ac0 65 63 74 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  ect..*/..typedef
b4ad0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
b4ae0 64 65 62 75 67 5f 6d 75 74 65 78 20 7b 0d 0a 20  debug_mutex {.. 
b4af0 20 69 6e 74 20 69 64 3b 20 20 20 20 20 2f 2a 20   int id;     /* 
b4b00 54 68 65 20 6d 75 74 65 78 20 74 79 70 65 20 2a  The mutex type *
b4b10 2f 0d 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  /..  int cnt;   
b4b20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
b4b30 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 61 20  tries without a 
b4b40 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 20 2a  matching leave *
b4b50 2f 0d 0a 7d 20 73 71 6c 69 74 65 33 5f 64 65 62  /..} sqlite3_deb
b4b60 75 67 5f 6d 75 74 65 78 3b 0d 0a 0d 0a 2f 2a 0d  ug_mutex;..../*.
b4b70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
b4b80 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64  mutex_held() and
b4b90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
b4ba0 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65  otheld() routine
b4bb0 20 61 72 65 0d 0a 2a 2a 20 69 6e 74 65 6e 64 65   are..** intende
b4bc0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
b4bd0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
b4be0 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ents...*/..stati
b4bf0 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78  c int debugMutex
b4c00 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  Held(sqlite3_mut
b4c10 65 78 20 2a 70 58 29 7b 0d 0a 20 20 73 71 6c 69  ex *pX){..  sqli
b4c20 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20  te3_debug_mutex 
b4c30 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65  *p = (sqlite3_de
b4c40 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0d 0a  bug_mutex*)pX;..
b4c50 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
b4c60 20 70 2d 3e 63 6e 74 3e 30 3b 0d 0a 7d 0d 0a 73   p->cnt>0;..}..s
b4c70 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d  tatic int debugM
b4c80 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
b4c90 74 65 33 5f 6d 75 74 65 78 20 2a 70 58 29 7b 0d  te3_mutex *pX){.
b4ca0 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 62 75 67  .  sqlite3_debug
b4cb0 5f 6d 75 74 65 78 20 2a 70 20 3d 20 28 73 71 6c  _mutex *p = (sql
b4cc0 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78  ite3_debug_mutex
b4cd0 2a 29 70 58 3b 0d 0a 20 20 72 65 74 75 72 6e 20  *)pX;..  return 
b4ce0 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d  p==0 || p->cnt==
b4cf0 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
b4d00 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
b4d10 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
b4d20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
b4d30 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
b4d40 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28   debugMutexInit(
b4d50 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
b4d60 4c 49 54 45 5f 4f 4b 3b 20 7d 0d 0a 73 74 61 74  LITE_OK; }..stat
b4d70 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
b4d80 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
b4d90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0d  rn SQLITE_OK; }.
b4da0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71  .../*..** The sq
b4db0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
b4dc0 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
b4dd0 63 61 74 65 73 20 61 20 6e 65 77 0d 0a 2a 2a 20  cates a new..** 
b4de0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
b4df0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
b4e00 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
b4e10 73 20 4e 55 4c 4c 0d 0a 2a 2a 20 74 68 61 74 20  s NULL..** that 
b4e20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
b4e30 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
b4e40 61 6c 6c 6f 63 61 74 65 64 2e 20 0d 0a 2a 2f 0d  allocated. ..*/.
b4e50 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
b4e60 6d 75 74 65 78 20 2a 64 65 62 75 67 4d 75 74 65  mutex *debugMute
b4e70 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0d  xAlloc(int id){.
b4e80 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
b4e90 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20 61 53  3_debug_mutex aS
b4ea0 74 61 74 69 63 5b 36 5d 3b 0d 0a 20 20 73 71 6c  tatic[6];..  sql
b4eb0 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78  ite3_debug_mutex
b4ec0 20 2a 70 4e 65 77 20 3d 20 30 3b 0d 0a 20 20 73   *pNew = 0;..  s
b4ed0 77 69 74 63 68 28 20 69 64 20 29 7b 0d 0a 20 20  witch( id ){..  
b4ee0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
b4ef0 54 45 58 5f 46 41 53 54 3a 0d 0a 20 20 20 20 63  TEX_FAST:..    c
b4f00 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
b4f10 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0d 0a 20  _RECURSIVE: {.. 
b4f20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
b4f30 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  te3Malloc(sizeof
b4f40 28 2a 70 4e 65 77 29 29 3b 0d 0a 20 20 20 20 20  (*pNew));..     
b4f50 20 69 66 28 20 70 4e 65 77 20 29 7b 0d 0a 20 20   if( pNew ){..  
b4f60 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d        pNew->id =
b4f70 20 69 64 3b 0d 0a 20 20 20 20 20 20 20 20 70 4e   id;..        pN
b4f80 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0d 0a 20 20  ew->cnt = 0;..  
b4f90 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65      }..      bre
b4fa0 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
b4fb0 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20  default: {..    
b4fc0 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e    assert( id-2 >
b4fd0 3d 20 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  = 0 );..      as
b4fe0 73 65 72 74 28 20 69 64 2d 32 20 3c 20 28 69 6e  sert( id-2 < (in
b4ff0 74 29 28 73 69 7a 65 6f 66 28 61 53 74 61 74 69  t)(sizeof(aStati
b5000 63 29 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69  c)/sizeof(aStati
b5010 63 5b 30 5d 29 29 20 29 3b 0d 0a 20 20 20 20 20  c[0])) );..     
b5020 20 70 4e 65 77 20 3d 20 26 61 53 74 61 74 69 63   pNew = &aStatic
b5030 5b 69 64 2d 32 5d 3b 0d 0a 20 20 20 20 20 20 70  [id-2];..      p
b5040 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0d 0a 20  New->id = id;.. 
b5050 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
b5060 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
b5070 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  n (sqlite3_mutex
b5080 2a 29 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  *)pNew;..}..../*
b5090 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
b50a0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
b50b0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
b50c0 61 74 65 64 20 6d 75 74 65 78 2e 0d 0a 2a 2f 0d  ated mutex...*/.
b50d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
b50e0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  ugMutexFree(sqli
b50f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 58 29 7b 0d  te3_mutex *pX){.
b5100 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 62 75 67  .  sqlite3_debug
b5110 5f 6d 75 74 65 78 20 2a 70 20 3d 20 28 73 71 6c  _mutex *p = (sql
b5120 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78  ite3_debug_mutex
b5130 2a 29 70 58 3b 0d 0a 20 20 61 73 73 65 72 74 28  *)pX;..  assert(
b5140 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0d 0a 20   p->cnt==0 );.. 
b5150 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
b5160 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
b5170 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  T || p->id==SQLI
b5180 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
b5190 56 45 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  VE );..  sqlite3
b51a0 5f 66 72 65 65 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a  _free(p);..}....
b51b0 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  /*..** The sqlit
b51c0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
b51d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
b51e0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
b51f0 73 20 61 74 74 65 6d 70 74 0d 0a 2a 2a 20 74 6f  s attempt..** to
b5200 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20   enter a mutex. 
b5210 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
b5220 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69  ad is already wi
b5230 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0d  thin the mutex,.
b5240 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
b5250 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
b5260 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
b5270 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
b5280 6c 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 53 51 4c  l return..** SQL
b5290 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
b52a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
b52b0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
b52c0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a  urns SQLITE_OK..
b52d0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
b52e0 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
b52f0 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
b5300 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
b5310 43 55 52 53 49 56 45 20 63 61 6e 0d 0a 2a 2a 20  CURSIVE can..** 
b5320 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
b5330 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
b5340 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
b5350 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
b5360 2c 0d 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  ,..** mutex must
b5370 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71   be exited an eq
b5380 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  ual number of ti
b5390 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68  mes before anoth
b53a0 65 72 20 74 68 72 65 61 64 0d 0a 2a 2a 20 63 61  er thread..** ca
b53b0 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
b53c0 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
b53d0 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
b53e0 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
b53f0 74 65 78 0d 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  tex..** more tha
b5400 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
b5410 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
b5420 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  d...*/..static v
b5430 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 45 6e  oid debugMutexEn
b5440 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ter(sqlite3_mute
b5450 78 20 2a 70 58 29 7b 0d 0a 20 20 73 71 6c 69 74  x *pX){..  sqlit
b5460 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20 2a  e3_debug_mutex *
b5470 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65 62  p = (sqlite3_deb
b5480 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0d 0a 20  ug_mutex*)pX;.. 
b5490 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
b54a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b54b0 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
b54c0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 58 29 20  utexNotheld(pX) 
b54d0 29 3b 0d 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0d  );..  p->cnt++;.
b54e0 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .}..static int d
b54f0 65 62 75 67 4d 75 74 65 78 54 72 79 28 73 71 6c  ebugMutexTry(sql
b5500 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 58 29 7b  ite3_mutex *pX){
b5510 0d 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 62 75  ..  sqlite3_debu
b5520 67 5f 6d 75 74 65 78 20 2a 70 20 3d 20 28 73 71  g_mutex *p = (sq
b5530 6c 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65  lite3_debug_mute
b5540 78 2a 29 70 58 3b 0d 0a 20 20 61 73 73 65 72 74  x*)pX;..  assert
b5550 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
b5560 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
b5570 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74  || debugMutexNot
b5580 68 65 6c 64 28 70 58 29 20 29 3b 0d 0a 20 20 70  held(pX) );..  p
b5590 2d 3e 63 6e 74 2b 2b 3b 0d 0a 20 20 72 65 74 75  ->cnt++;..  retu
b55a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
b55b0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73  ..../*..** The s
b55c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
b55d0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
b55e0 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
b55f0 77 61 73 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73  was..** previous
b5600 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
b5610 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
b5620 54 68 65 20 62 65 68 61 76 69 6f 72 0d 0a 2a 2a  The behavior..**
b5630 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
b5640 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
b5650 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
b5660 72 65 64 20 6f 72 0d 0a 2a 2a 20 69 73 20 6e 6f  red or..** is no
b5670 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  t currently allo
b5680 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77  cated.  SQLite w
b5690 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74  ill never do eit
b56a0 68 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  her...*/..static
b56b0 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78   void debugMutex
b56c0 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75  Leave(sqlite3_mu
b56d0 74 65 78 20 2a 70 58 29 7b 0d 0a 20 20 73 71 6c  tex *pX){..  sql
b56e0 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78  ite3_debug_mutex
b56f0 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64   *p = (sqlite3_d
b5700 65 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0d  ebug_mutex*)pX;.
b5710 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67  .  assert( debug
b5720 4d 75 74 65 78 48 65 6c 64 28 70 58 29 20 29 3b  MutexHeld(pX) );
b5730 0d 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0d 0a 20  ..  p->cnt--;.. 
b5740 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
b5750 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b5760 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
b5770 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 58 29 20  utexNotheld(pX) 
b5780 29 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f  );..}....SQLITE_
b5790 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
b57a0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 63 6f  mutex_methods co
b57b0 6e 73 74 20 2a 73 71 6c 69 74 65 33 4e 6f 6f 70  nst *sqlite3Noop
b57c0 4d 75 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20 20  Mutex(void){..  
b57d0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
b57e0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
b57f0 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0d 0a 20  ds sMutex = {.. 
b5800 20 20 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69     debugMutexIni
b5810 74 2c 0d 0a 20 20 20 20 64 65 62 75 67 4d 75 74  t,..    debugMut
b5820 65 78 45 6e 64 2c 0d 0a 20 20 20 20 64 65 62 75  exEnd,..    debu
b5830 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0d 0a 20 20  gMutexAlloc,..  
b5840 20 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65    debugMutexFree
b5850 2c 0d 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  ,..    debugMute
b5860 78 45 6e 74 65 72 2c 0d 0a 20 20 20 20 64 65 62  xEnter,..    deb
b5870 75 67 4d 75 74 65 78 54 72 79 2c 0d 0a 20 20 20  ugMutexTry,..   
b5880 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65   debugMutexLeave
b5890 2c 0d 0a 0d 0a 20 20 20 20 64 65 62 75 67 4d 75  ,....    debugMu
b58a0 74 65 78 48 65 6c 64 2c 0d 0a 20 20 20 20 64 65  texHeld,..    de
b58b0 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0d  bugMutexNotheld.
b58c0 0a 20 20 7d 3b 0d 0a 0d 0a 20 20 72 65 74 75 72  .  };....  retur
b58d0 6e 20 26 73 4d 75 74 65 78 3b 0d 0a 7d 0d 0a 23  n &sMutex;..}..#
b58e0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
b58f0 44 45 42 55 47 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  DEBUG */..../*..
b5900 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 77  ** If compiled w
b5910 69 74 68 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ith SQLITE_MUTEX
b5920 5f 4e 4f 4f 50 2c 20 74 68 65 6e 20 74 68 65 20  _NOOP, then the 
b5930 6e 6f 2d 6f 70 20 6d 75 74 65 78 20 69 6d 70 6c  no-op mutex impl
b5940 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a 20 69  ementation..** i
b5950 73 20 75 73 65 64 20 72 65 67 61 72 64 6c 65 73  s used regardles
b5960 73 20 6f 66 20 74 68 65 20 72 75 6e 2d 74 69 6d  s of the run-tim
b5970 65 20 74 68 72 65 61 64 73 61 66 65 74 79 20 73  e threadsafety s
b5980 65 74 74 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23 69 66  etting...*/..#if
b5990 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
b59a0 5f 4e 4f 4f 50 0d 0a 53 51 4c 49 54 45 5f 50 52  _NOOP..SQLITE_PR
b59b0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
b59c0 74 65 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73  tex_methods cons
b59d0 74 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  t *sqlite3Defaul
b59e0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20  tMutex(void){.. 
b59f0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4e   return sqlite3N
b5a00 6f 6f 70 4d 75 74 65 78 28 29 3b 0d 0a 7d 0d 0a  oopMutex();..}..
b5a10 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
b5a20 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  d(SQLITE_MUTEX_N
b5a30 4f 4f 50 29 20 2a 2f 0d 0a 23 65 6e 64 69 66 20  OOP) */..#endif 
b5a40 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
b5a50 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a  TE_MUTEX_OMIT) *
b5a60 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
b5a70 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
b5a80 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  x_noop.c *******
b5a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5ab0 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
b5ac0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b5ad0 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a   mutex_os2.c ***
b5ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5b00 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
b5b10 30 37 20 41 75 67 75 73 74 20 32 38 0d 0a 2a 2a  07 August 28..**
b5b20 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
b5b30 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
b5b40 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
b5b50 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
b5b60 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61  ce of..** a lega
b5b70 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
b5b80 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a  s a blessing:..*
b5b90 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  *..**    May you
b5ba0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
b5bb0 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61   evil...**    Ma
b5bc0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
b5bd0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
b5be0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
b5bf0 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d  others...**    M
b5c00 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
b5c10 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
b5c20 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
b5c30 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a  give...**..*****
b5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5c80 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69  ****..** This fi
b5c90 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
b5ca0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
b5cb0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
b5cc0 65 73 20 66 6f 72 20 4f 53 2f 32 0d 0a 2a 2f 0d  es for OS/2..*/.
b5cd0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f  .../*..** The co
b5ce0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
b5cf0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
b5d00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
b5d10 20 69 73 20 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a   is defined...**
b5d20 20 53 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68   See the mutex.h
b5d30 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c   file for detail
b5d40 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53  s...*/..#ifdef S
b5d50 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0d  QLITE_MUTEX_OS2.
b5d60 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
b5d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32 20  ********** OS/2 
b5d80 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65 6e 74 61  Mutex Implementa
b5d90 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
b5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d  ***********..**.
b5db0 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
b5dc0 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65 78  ntation of mutex
b5dd0 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 69 6e  es is built usin
b5de0 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 2e 0d  g the OS/2 API..
b5df0 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  .*/..../*..** Th
b5e00 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0d 0a  e mutex object..
b5e10 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76  ** Each recursiv
b5e20 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e  e mutex is an in
b5e30 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
b5e40 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
b5e50 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 73  e...*/..struct s
b5e60 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0d 0a  qlite3_mutex {..
b5e70 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20    HMTX mutex;   
b5e80 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
b5e90 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63  trolling the loc
b5ea0 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 20 69 64 3b  k */..  int  id;
b5eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74            /* Mut
b5ec0 65 78 20 74 79 70 65 20 2a 2f 0d 0a 23 69 66 64  ex type */..#ifd
b5ed0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
b5ee0 0a 20 69 6e 74 20 20 20 74 72 61 63 65 3b 20 20  . int   trace;  
b5ef0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
b5f00 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f  trace changes */
b5f10 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a  ..#endif..};....
b5f20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
b5f30 42 55 47 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c  BUG..#define SQL
b5f40 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
b5f50 41 4c 49 5a 45 52 20 7b 20 30 2c 20 30 2c 20 30  ALIZER { 0, 0, 0
b5f60 20 7d 0d 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69   }..#else..#defi
b5f70 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58  ne SQLITE3_MUTEX
b5f80 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 30  _INITIALIZER { 0
b5f90 2c 20 30 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  , 0 }..#endif...
b5fa0 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ./*..** Initiali
b5fb0 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
b5fc0 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
b5fd0 62 73 79 73 74 65 6d 2e 0d 0a 2a 2f 0d 0a 73 74  bsystem...*/..st
b5fe0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
b5ff0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
b6000 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
b6010 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  ..static int os2
b6020 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
b6030 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
b6040 3b 20 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ; }..../*..** Th
b6050 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
b6060 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20  alloc() routine 
b6070 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0d  allocates a new.
b6080 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
b6090 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
b60a0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
b60b0 74 75 72 6e 73 20 4e 55 4c 4c 0d 0a 2a 2a 20 74  turns NULL..** t
b60c0 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
b60d0 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
b60e0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0d   be allocated. .
b60f0 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
b6100 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b  unwind its stack
b6110 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
b6120 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d  rror.  The argum
b6130 65 6e 74 0d 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ent..** to sqlit
b6140 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
b6150 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
b6160 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
b6170 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e  ts:..**..** <ul>
b6180 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  ..** <li>  SQLIT
b6190 45 5f 4d 55 54 45 58 5f 46 41 53 54 0d 0a 2a 2a  E_MUTEX_FAST..**
b61a0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
b61b0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0d 0a 2a  TEX_RECURSIVE..*
b61c0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
b61d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
b61e0 45 52 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  ER..** <li>  SQL
b61f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
b6200 5f 4d 45 4d 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _MEM..** <li>  S
b6210 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
b6220 49 43 5f 4d 45 4d 32 0d 0a 2a 2a 20 3c 6c 69 3e  IC_MEM2..** <li>
b6230 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
b6240 54 41 54 49 43 5f 50 52 4e 47 0d 0a 2a 2a 20 3c  TATIC_PRNG..** <
b6250 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
b6260 58 5f 53 54 41 54 49 43 5f 4c 52 55 0d 0a 2a 2a  X_STATIC_LRU..**
b6270 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
b6280 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0d  TEX_STATIC_LRU2.
b6290 0a 2a 2a 20 3c 2f 75 6c 3e 0d 0a 2a 2a 0d 0a 2a  .** </ul>..**..*
b62a0 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20  * The first two 
b62b0 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20  constants cause 
b62c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
b62d0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0d  loc() to create.
b62e0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
b62f0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
b6300 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
b6310 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
b6320 45 43 55 52 53 49 56 45 0d 0a 2a 2a 20 69 73 20  ECURSIVE..** is 
b6330 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63  used but not nec
b6340 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e  essarily so when
b6350 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
b6360 53 54 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 20  ST is used...** 
b6370 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  The mutex implem
b6380 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
b6390 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  t need to make a
b63a0 20 64 69 73 74 69 6e 63 74 69 6f 6e 0d 0a 2a 2a   distinction..**
b63b0 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f   between SQLITE_
b63c0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
b63d0 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  and SQLITE_MUTEX
b63e0 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73  _FAST if it does
b63f0 0d 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  ..** not want to
b6400 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  .  But SQLite wi
b6410 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
b6420 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
b6430 78 20 69 6e 0d 0a 2a 2a 20 63 61 73 65 73 20 77  x in..** cases w
b6440 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
b6450 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
b6460 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
b6470 73 69 76 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 69  sive mutex..** i
b6480 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
b6490 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
b64a0 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c  e host platform,
b64b0 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
b64c0 73 74 65 6d 0d 0a 2a 2a 20 6d 69 67 68 74 20 72  stem..** might r
b64d0 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
b64e0 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
b64f0 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
b6500 41 53 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  AST...**..** The
b6510 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70   other allowed p
b6520 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c  arameters to sql
b6530 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b6540 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0d 0a  () each return..
b6550 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
b6560 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
b6570 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78  ting mutex.  Six
b6580 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
b6590 61 72 65 0d 0a 2a 2a 20 75 73 65 64 20 62 79 20  are..** used by 
b65a0 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
b65b0 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
b65c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
b65d0 6f 66 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 6d 61  of SQLite..** ma
b65e0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
b65f0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
b6600 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
b6610 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
b6620 6c 0d 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c  l..** use by SQL
b6630 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ite only.  Appli
b6640 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65  cations that use
b6650 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20   SQLite mutexes 
b6660 73 68 6f 75 6c 64 0d 0a 2a 2a 20 75 73 65 20 6f  should..** use o
b6670 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20  nly the dynamic 
b6680 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64  mutexes returned
b6690 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
b66a0 5f 46 41 53 54 20 6f 72 0d 0a 2a 2a 20 53 51 4c  _FAST or..** SQL
b66b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
b66c0 49 56 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74  IVE...**..** Not
b66d0 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66  e that if one of
b66e0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
b66f0 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53  ex parameters (S
b6700 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
b6710 0d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  ..** or SQLITE_M
b6720 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
b6730 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
b6740 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b6750 28 29 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  ()..** returns a
b6760 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
b6770 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
b6780 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61   But for the sta
b6790 74 69 63 0d 0a 2a 2a 20 6d 75 74 65 78 20 74 79  tic..** mutex ty
b67a0 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75  pes, the same mu
b67b0 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20  tex is returned 
b67c0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68  on every call th
b67d0 61 74 20 68 61 73 0d 0a 2a 2a 20 74 68 65 20 73  at has..** the s
b67e0 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e  ame type number.
b67f0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 71 6c  ..*/..static sql
b6800 69 74 65 33 5f 6d 75 74 65 78 20 2a 6f 73 32 4d  ite3_mutex *os2M
b6810 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54  utexAlloc(int iT
b6820 79 70 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  ype){..  sqlite3
b6830 5f 6d 75 74 65 78 20 2a 70 20 3d 20 4e 55 4c 4c  _mutex *p = NULL
b6840 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 69 54 79  ;..  switch( iTy
b6850 70 65 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  pe ){..    case 
b6860 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
b6870 54 3a 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c  T:..    case SQL
b6880 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
b6890 49 56 45 3a 20 7b 0d 0a 20 20 20 20 20 20 70 20  IVE: {..      p 
b68a0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
b68b0 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
b68c0 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 20  );..      if( p 
b68d0 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  ){..        p->i
b68e0 64 20 3d 20 69 54 79 70 65 3b 0d 0a 20 20 20 20  d = iType;..    
b68f0 20 20 20 20 69 66 28 20 44 6f 73 43 72 65 61 74      if( DosCreat
b6900 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70  eMutexSem( 0, &p
b6910 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53  ->mutex, 0, FALS
b6920 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  E ) != NO_ERROR 
b6930 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71  ){..          sq
b6940 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b  lite3_free( p );
b6950 0d 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  ..          p = 
b6960 4e 55 4c 4c 3b 0d 0a 20 20 20 20 20 20 20 20 7d  NULL;..        }
b6970 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
b6980 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
b6990 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a      default: {..
b69a0 20 20 20 20 20 20 73 74 61 74 69 63 20 76 6f 6c        static vol
b69b0 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 69 74  atile int isInit
b69c0 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 74 61   = 0;..      sta
b69d0 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
b69e0 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  x staticMutexes[
b69f0 36 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20 20  6] = {..        
b6a00 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
b6a10 49 54 49 41 4c 49 5a 45 52 2c 0d 0a 20 20 20 20  ITIALIZER,..    
b6a20 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
b6a30 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0d 0a  X_INITIALIZER,..
b6a40 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 33 5f          SQLITE3_
b6a50 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
b6a60 52 2c 0d 0a 20 20 20 20 20 20 20 20 53 51 4c 49  R,..        SQLI
b6a70 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
b6a80 4c 49 5a 45 52 2c 0d 0a 20 20 20 20 20 20 20 20  LIZER,..        
b6a90 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
b6aa0 49 54 49 41 4c 49 5a 45 52 2c 0d 0a 20 20 20 20  ITIALIZER,..    
b6ab0 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
b6ac0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0d 0a  X_INITIALIZER,..
b6ad0 20 20 20 20 20 20 7d 3b 0d 0a 20 20 20 20 20 20        };..      
b6ae0 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b 0d  if ( !isInit ){.
b6af0 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20  .        APIRET 
b6b00 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 50 54 49  rc;..        PTI
b6b10 42 20 70 74 69 62 3b 0d 0a 20 20 20 20 20 20 20  B ptib;..       
b6b20 20 50 50 49 42 20 70 70 69 62 3b 0d 0a 20 20 20   PPIB ppib;..   
b6b30 20 20 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b       HMTX mutex;
b6b40 0d 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 6e  ..        char n
b6b50 61 6d 65 5b 33 32 5d 3b 0d 0a 20 20 20 20 20 20  ame[32];..      
b6b60 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
b6b70 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 62  ks( &ptib, &ppib
b6b80 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   );..        sql
b6b90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73  ite3_snprintf( s
b6ba0 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d  izeof(name), nam
b6bb0 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c  e, "\\SEM32\\SQL
b6bc0 49 54 45 25 30 34 78 22 2c 0d 0a 20 20 20 20 20  ITE%04x",..     
b6bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b6be0 20 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75       ppib->pib_u
b6bf0 6c 70 69 64 20 29 3b 0d 0a 20 20 20 20 20 20 20  lpid );..       
b6c00 20 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20   while( !isInit 
b6c10 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6d 75  ){..          mu
b6c20 74 65 78 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  tex = 0;..      
b6c30 20 20 20 20 72 63 20 3d 20 44 6f 73 43 72 65 61      rc = DosCrea
b6c40 74 65 4d 75 74 65 78 53 65 6d 28 20 6e 61 6d 65  teMutexSem( name
b6c50 2c 20 26 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c  , &mutex, 0, FAL
b6c60 53 45 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  SE);..          
b6c70 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  if( rc == NO_ERR
b6c80 4f 52 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OR ){..         
b6c90 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
b6ca0 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  i;..            
b6cb0 69 66 28 20 21 69 73 49 6e 69 74 20 29 7b 0d 0a  if( !isInit ){..
b6cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
b6cd0 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 73 69  r( i = 0; i < si
b6ce0 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
b6cf0 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69  es)/sizeof(stati
b6d00 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69 2b  cMutexes[0]); i+
b6d10 2b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  + ){..          
b6d20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65 4d        DosCreateM
b6d30 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74 61  utexSem( 0, &sta
b6d40 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75  ticMutexes[i].mu
b6d50 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 3b  tex, 0, FALSE );
b6d60 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
b6d70 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  }..             
b6d80 20 69 73 49 6e 69 74 20 3d 20 31 3b 0d 0a 20 20   isInit = 1;..  
b6d90 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
b6da0 20 20 20 20 20 20 20 20 20 44 6f 73 43 6c 6f 73           DosClos
b6db0 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 65 78  eMutexSem( mutex
b6dc0 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d   );..          }
b6dd0 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d 20 45  else if( rc == E
b6de0 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f 4e  RROR_DUPLICATE_N
b6df0 41 4d 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  AME ){..        
b6e00 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 20      DosSleep( 1 
b6e10 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65  );..          }e
b6e20 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
b6e30 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 20 20 20    return p;..   
b6e40 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
b6e50 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
b6e60 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
b6e70 65 2d 32 20 3e 3d 20 30 20 29 3b 0d 0a 20 20 20  e-2 >= 0 );..   
b6e80 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65     assert( iType
b6e90 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74  -2 < sizeof(stat
b6ea0 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f  icMutexes)/sizeo
b6eb0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  f(staticMutexes[
b6ec0 30 5d 29 20 29 3b 0d 0a 20 20 20 20 20 20 70 20  0]) );..      p 
b6ed0 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  = &staticMutexes
b6ee0 5b 69 54 79 70 65 2d 32 5d 3b 0d 0a 20 20 20 20  [iType-2];..    
b6ef0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
b6f00 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
b6f10 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
b6f20 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  turn p;..}......
b6f30 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
b6f40 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
b6f50 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
b6f60 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0d 0a 2a  ocated mutex...*
b6f70 2a 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  * SQLite is care
b6f80 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
b6f90 65 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68  e every mutex th
b6fa0 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
b6fb0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
b6fc0 64 20 6f 73 32 4d 75 74 65 78 46 72 65 65 28 73  d os2MutexFree(s
b6fd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
b6fe0 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  {..#ifdef SQLITE
b6ff0 5f 44 45 42 55 47 0d 0a 20 20 54 49 44 20 74 69  _DEBUG..  TID ti
b7000 64 3b 0d 0a 20 20 50 49 44 20 70 69 64 3b 0d 0a  d;..  PID pid;..
b7010 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b    ULONG ulCount;
b7020 0d 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65  ..  DosQueryMute
b7030 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
b7040 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f  pid, &tid, &ulCo
b7050 75 6e 74 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  unt);..  assert(
b7060 20 75 6c 43 6f 75 6e 74 3d 3d 30 20 29 3b 0d 0a   ulCount==0 );..
b7070 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
b7080 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  =SQLITE_MUTEX_FA
b7090 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  ST || p->id==SQL
b70a0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
b70b0 49 56 45 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  IVE );..#endif..
b70c0 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53    DosCloseMutexS
b70d0 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0d  em( p->mutex );.
b70e0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
b70f0 20 70 20 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 64   p );..}....#ifd
b7100 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
b7110 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ./*..** The sqli
b7120 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
b7130 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
b7140 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
b7150 74 69 6e 65 20 61 72 65 0d 0a 2a 2a 20 69 6e 74  tine are..** int
b7160 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
b7170 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
b7180 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73  atements...*/..s
b7190 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
b71a0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
b71b0 75 74 65 78 20 2a 70 29 7b 0d 0a 20 20 54 49 44  utex *p){..  TID
b71c0 20 74 69 64 3b 0d 0a 20 20 50 49 44 20 70 69 64   tid;..  PID pid
b71d0 3b 0d 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75  ;..  ULONG ulCou
b71e0 6e 74 3b 0d 0a 20 20 50 54 49 42 20 70 74 69 62  nt;..  PTIB ptib
b71f0 3b 0d 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74  ;..  DosQueryMut
b7200 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20  exSem(p->mutex, 
b7210 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43  &pid, &tid, &ulC
b7220 6f 75 6e 74 29 3b 0d 0a 20 20 69 66 28 20 75 6c  ount);..  if( ul
b7230 43 6f 75 6e 74 3d 3d 30 20 7c 7c 20 28 20 75 6c  Count==0 || ( ul
b7240 43 6f 75 6e 74 3e 31 20 26 26 20 70 2d 3e 69 64  Count>1 && p->id
b7250 21 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  !=SQLITE_MUTEX_R
b7260 45 43 55 52 53 49 56 45 20 29 20 29 0d 0a 20 20  ECURSIVE ) )..  
b7270 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 44    return 0;..  D
b7280 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
b7290 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0d 0a 20  &ptib, NULL);.. 
b72a0 20 72 65 74 75 72 6e 20 74 69 64 3d 3d 70 74 69   return tid==pti
b72b0 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69  b->tib_ptib2->ti
b72c0 62 32 5f 75 6c 74 69 64 3b 0d 0a 7d 0d 0a 73 74  b2_ultid;..}..st
b72d0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
b72e0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
b72f0 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a 20 20 54  _mutex *p){..  T
b7300 49 44 20 74 69 64 3b 0d 0a 20 20 50 49 44 20 70  ID tid;..  PID p
b7310 69 64 3b 0d 0a 20 20 55 4c 4f 4e 47 20 75 6c 43  id;..  ULONG ulC
b7320 6f 75 6e 74 3b 0d 0a 20 20 50 54 49 42 20 70 74  ount;..  PTIB pt
b7330 69 62 3b 0d 0a 20 20 44 6f 73 51 75 65 72 79 4d  ib;..  DosQueryM
b7340 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
b7350 2c 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75  , &pid, &tid, &u
b7360 6c 43 6f 75 6e 74 29 3b 0d 0a 20 20 69 66 28 20  lCount);..  if( 
b7370 75 6c 43 6f 75 6e 74 3d 3d 30 20 29 0d 0a 20 20  ulCount==0 )..  
b7380 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 44    return 1;..  D
b7390 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
b73a0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0d 0a 20  &ptib, NULL);.. 
b73b0 20 72 65 74 75 72 6e 20 74 69 64 21 3d 70 74 69   return tid!=pti
b73c0 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69  b->tib_ptib2->ti
b73d0 62 32 5f 75 6c 74 69 64 3b 0d 0a 7d 0d 0a 73 74  b2_ultid;..}..st
b73e0 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74  atic void os2Mut
b73f0 65 78 54 72 61 63 65 28 73 71 6c 69 74 65 33 5f  exTrace(sqlite3_
b7400 6d 75 74 65 78 20 2a 70 2c 20 63 68 61 72 20 2a  mutex *p, char *
b7410 70 41 63 74 69 6f 6e 29 7b 0d 0a 20 20 54 49 44  pAction){..  TID
b7420 20 20 20 74 69 64 3b 0d 0a 20 20 50 49 44 20 20     tid;..  PID  
b7430 20 70 69 64 3b 0d 0a 20 20 55 4c 4f 4e 47 20 75   pid;..  ULONG u
b7440 6c 43 6f 75 6e 74 3b 0d 0a 20 20 44 6f 73 51 75  lCount;..  DosQu
b7450 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  eryMutexSem(p->m
b7460 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64  utex, &pid, &tid
b7470 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0d 0a 20 20  , &ulCount);..  
b7480 70 72 69 6e 74 66 28 22 25 73 20 6d 75 74 65 78  printf("%s mutex
b7490 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52   %p (%d) with nR
b74a0 65 66 3d 25 6c 64 5c 6e 22 2c 20 70 41 63 74 69  ef=%ld\n", pActi
b74b0 6f 6e 2c 20 28 76 6f 69 64 2a 29 70 2c 20 70 2d  on, (void*)p, p-
b74c0 3e 74 72 61 63 65 2c 20 75 6c 43 6f 75 6e 74 29  >trace, ulCount)
b74d0 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
b74e0 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  /*..** The sqlit
b74f0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
b7500 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
b7510 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
b7520 73 20 61 74 74 65 6d 70 74 0d 0a 2a 2a 20 74 6f  s attempt..** to
b7530 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20   enter a mutex. 
b7540 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
b7550 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69  ad is already wi
b7560 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0d  thin the mutex,.
b7570 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
b7580 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
b7590 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
b75a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
b75b0 6c 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 53 51 4c  l return..** SQL
b75c0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
b75d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
b75e0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
b75f0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a  urns SQLITE_OK..
b7600 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
b7610 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
b7620 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
b7630 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
b7640 43 55 52 53 49 56 45 20 63 61 6e 0d 0a 2a 2a 20  CURSIVE can..** 
b7650 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
b7660 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
b7670 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
b7680 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
b7690 2c 0d 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  ,..** mutex must
b76a0 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71   be exited an eq
b76b0 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  ual number of ti
b76c0 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68  mes before anoth
b76d0 65 72 20 74 68 72 65 61 64 0d 0a 2a 2a 20 63 61  er thread..** ca
b76e0 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
b76f0 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
b7700 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
b7710 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
b7720 74 65 78 0d 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  tex..** more tha
b7730 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
b7740 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
b7750 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  d...*/..static v
b7760 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65  oid os2MutexEnte
b7770 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
b7780 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  *p){..  assert( 
b7790 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
b77a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
b77b0 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64   os2MutexNotheld
b77c0 28 70 29 20 29 3b 0d 0a 20 20 44 6f 73 52 65 71  (p) );..  DosReq
b77d0 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e  uestMutexSem(p->
b77e0 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46  mutex, SEM_INDEF
b77f0 49 4e 49 54 45 5f 57 41 49 54 29 3b 0d 0a 23 69  INITE_WAIT);..#i
b7800 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
b7810 47 0d 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63  G..  if( p->trac
b7820 65 20 29 20 6f 73 32 4d 75 74 65 78 54 72 61 63  e ) os2MutexTrac
b7830 65 28 70 2c 20 22 65 6e 74 65 72 22 29 3b 0d 0a  e(p, "enter");..
b7840 23 65 6e 64 69 66 0d 0a 7d 0d 0a 73 74 61 74 69  #endif..}..stati
b7850 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 54 72  c int os2MutexTr
b7860 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  y(sqlite3_mutex 
b7870 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  *p){..  int rc =
b7880 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
b7890 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
b78a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b78b0 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74  URSIVE || os2Mut
b78c0 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0d  exNotheld(p) );.
b78d0 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 73  .  if( DosReques
b78e0 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
b78f0 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54  ex, SEM_IMMEDIAT
b7900 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f  E_RETURN) == NO_
b7910 45 52 52 4f 52 20 29 20 7b 0d 0a 20 20 20 20 72  ERROR ) {..    r
b7920 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
b7930 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
b7940 42 55 47 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e  BUG..    if( p->
b7950 74 72 61 63 65 20 29 20 6f 73 32 4d 75 74 65 78  trace ) os2Mutex
b7960 54 72 61 63 65 28 70 2c 20 22 74 72 79 22 29 3b  Trace(p, "try");
b7970 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 20  ..#endif..  }.. 
b7980 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
b7990 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c  ../*..** The sql
b79a0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
b79b0 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
b79c0 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
b79d0 73 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  s..** previously
b79e0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
b79f0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
b7a00 65 20 62 65 68 61 76 69 6f 72 0d 0a 2a 2a 20 69  e behavior..** i
b7a10 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74  s undefined if t
b7a20 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20  he mutex is not 
b7a30 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65  currently entere
b7a40 64 20 6f 72 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20  d or..** is not 
b7a50 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
b7a60 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ted.  SQLite wil
b7a70 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  l never do eithe
b7a80 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  r...*/..static v
b7a90 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c 65 61 76  oid os2MutexLeav
b7aa0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
b7ab0 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  *p){..  assert( 
b7ac0 6f 73 32 4d 75 74 65 78 48 65 6c 64 28 70 29 20  os2MutexHeld(p) 
b7ad0 29 3b 0d 0a 20 20 44 6f 73 52 65 6c 65 61 73 65  );..  DosRelease
b7ae0 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
b7af0 78 29 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  x);..#ifdef SQLI
b7b00 54 45 5f 44 45 42 55 47 0d 0a 20 20 69 66 28 20  TE_DEBUG..  if( 
b7b10 70 2d 3e 74 72 61 63 65 20 29 20 6f 73 32 4d 75  p->trace ) os2Mu
b7b20 74 65 78 54 72 61 63 65 28 70 2c 20 22 6c 65 61  texTrace(p, "lea
b7b30 76 65 22 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 7d  ve");..#endif..}
b7b40 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ....SQLITE_PRIVA
b7b50 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
b7b60 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
b7b70 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
b7b80 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20 20 73 74  tex(void){..  st
b7b90 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
b7ba0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
b7bb0 20 73 4d 75 74 65 78 20 3d 20 7b 0d 0a 20 20 20   sMutex = {..   
b7bc0 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0d 0a   os2MutexInit,..
b7bd0 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c      os2MutexEnd,
b7be0 0d 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 41 6c  ..    os2MutexAl
b7bf0 6c 6f 63 2c 0d 0a 20 20 20 20 6f 73 32 4d 75 74  loc,..    os2Mut
b7c00 65 78 46 72 65 65 2c 0d 0a 20 20 20 20 6f 73 32  exFree,..    os2
b7c10 4d 75 74 65 78 45 6e 74 65 72 2c 0d 0a 20 20 20  MutexEnter,..   
b7c20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0d 0a 20   os2MutexTry,.. 
b7c30 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65     os2MutexLeave
b7c40 2c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ,..#ifdef SQLITE
b7c50 5f 44 45 42 55 47 0d 0a 20 20 20 20 6f 73 32 4d  _DEBUG..    os2M
b7c60 75 74 65 78 48 65 6c 64 2c 0d 0a 20 20 20 20 6f  utexHeld,..    o
b7c70 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0d 0a  s2MutexNotheld..
b7c80 23 65 6c 73 65 0d 0a 20 20 20 20 30 2c 0d 0a 20  #else..    0,.. 
b7c90 20 20 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 20 20     0..#endif..  
b7ca0 7d 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 26  };....  return &
b7cb0 73 4d 75 74 65 78 3b 0d 0a 7d 0d 0a 23 65 6e 64  sMutex;..}..#end
b7cc0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
b7cd0 45 58 5f 4f 53 32 20 2a 2f 0d 0a 0d 0a 2f 2a 2a  EX_OS2 */..../**
b7ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
b7cf0 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20   of mutex_os2.c 
b7d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
b7d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b7d40 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75  gin file mutex_u
b7d50 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
b7d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
b7d80 2a 0d 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  *..** 2007 Augus
b7d90 74 20 32 38 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  t 28..**..** The
b7da0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
b7db0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
b7dc0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
b7dd0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
b7de0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
b7df0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
b7e00 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
b7e10 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
b7e20 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
b7e30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
b7e40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
b7e50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
b7e60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
b7e70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
b7e80 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
b7e90 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
b7ea0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
b7eb0 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
b7ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
b7f00 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
b7f10 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
b7f20 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
b7f30 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 70  nt mutexes for p
b7f40 74 68 72 65 61 64 73 0d 0a 2a 2f 0d 0a 0d 0a 2f  threads..*/..../
b7f50 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  *..** The code i
b7f60 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
b7f70 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 61  nly used if we a
b7f80 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72  re compiling thr
b7f90 65 61 64 73 61 66 65 0d 0a 2a 2a 20 75 6e 64 65  eadsafe..** unde
b7fa0 72 20 75 6e 69 78 20 77 69 74 68 20 70 74 68 72  r unix with pthr
b7fb0 65 61 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f  eads...**..** No
b7fc0 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d 70  te that this imp
b7fd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
b7fe0 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20 6f  ires a version o
b7ff0 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74 0d  f pthreads that.
b8000 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63  .** supports rec
b8010 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0d  ursive mutexes..
b8020 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
b8030 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
b8040 53 0d 0a 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 70  S....#include <p
b8050 74 68 72 65 61 64 2e 68 3e 0d 0a 0d 0a 2f 2a 0d  thread.h>..../*.
b8060 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
b8070 6d 75 74 65 78 2e 69 64 2c 20 73 71 6c 69 74 65  mutex.id, sqlite
b8080 33 5f 6d 75 74 65 78 2e 6e 52 65 66 2c 20 61 6e  3_mutex.nRef, an
b8090 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2e  d sqlite3_mutex.
b80a0 6f 77 6e 65 72 20 66 69 65 6c 64 73 0d 0a 2a 2a  owner fields..**
b80b0 20 61 72 65 20 6e 65 63 65 73 73 61 72 79 20 75   are necessary u
b80c0 6e 64 65 72 20 74 77 6f 20 63 6f 6e 64 69 64 74  nder two condidt
b80d0 69 6f 6e 73 3a 20 20 28 31 29 20 44 65 62 75 67  ions:  (1) Debug
b80e0 20 62 75 69 6c 64 73 20 61 6e 64 20 28 32 29 20   builds and (2) 
b80f0 75 73 69 6e 67 0d 0a 2a 2a 20 68 6f 6d 65 2d 67  using..** home-g
b8100 72 6f 77 6e 20 6d 75 74 65 78 65 73 2e 20 20 45  rown mutexes.  E
b8110 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65 73 65  ncapsulate these
b8120 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 74 6f   conditions into
b8130 20 61 20 73 69 6e 67 6c 65 20 23 64 65 66 69 6e   a single #defin
b8140 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66 69  e...*/..#if defi
b8150 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
b8160 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
b8170 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
b8180 43 55 52 53 49 56 45 5f 4d 55 54 45 58 29 0d 0a  CURSIVE_MUTEX)..
b8190 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
b81a0 4d 55 54 45 58 5f 4e 52 45 46 20 31 0d 0a 23 65  MUTEX_NREF 1..#e
b81b0 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 53 51  lse..# define SQ
b81c0 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46 20  LITE_MUTEX_NREF 
b81d0 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  0..#endif..../*.
b81e0 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
b81f0 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
b8200 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
b8210 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
b8220 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  re...*/..struct 
b8230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0d  sqlite3_mutex {.
b8240 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
b8250 5f 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a  _t mutex;     /*
b8260 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
b8270 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0d 0a  ng the lock */..
b8280 23 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  #if SQLITE_MUTEX
b8290 5f 4e 52 45 46 0d 0a 20 20 69 6e 74 20 69 64 3b  _NREF..  int id;
b82a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b82b0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70      /* Mutex typ
b82c0 65 20 2a 2f 0d 0a 20 20 76 6f 6c 61 74 69 6c 65  e */..  volatile
b82d0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
b82e0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
b82f0 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0d 0a 20 20  entrances */..  
b8300 76 6f 6c 61 74 69 6c 65 20 70 74 68 72 65 61 64  volatile pthread
b8310 5f 74 20 6f 77 6e 65 72 3b 20 20 2f 2a 20 54 68  _t owner;  /* Th
b8320 72 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74  read that is wit
b8330 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a  hin this mutex *
b8340 2f 0d 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20  /..  int trace; 
b8350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b8360 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
b8370 20 63 68 61 6e 67 65 73 20 2a 2f 0d 0a 23 65 6e   changes */..#en
b8380 64 69 66 0d 0a 7d 3b 0d 0a 23 69 66 20 53 51 4c  dif..};..#if SQL
b8390 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46 0d 0a  ITE_MUTEX_NREF..
b83a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
b83b0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
b83c0 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45  R { PTHREAD_MUTE
b83d0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
b83e0 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29  , 0, (pthread_t)
b83f0 30 2c 20 30 20 7d 0d 0a 23 65 6c 73 65 0d 0a 23  0, 0 }..#else..#
b8400 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d  define SQLITE3_M
b8410 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
b8420 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58   { PTHREAD_MUTEX
b8430 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7d 0d 0a  _INITIALIZER }..
b8440 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
b8450 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
b8460 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
b8470 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
b8480 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
b8490 65 0d 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  e..** intended f
b84a0 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69  or use only insi
b84b0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
b84c0 65 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65  ements.  On some
b84d0 20 70 6c 61 74 66 6f 72 6d 73 2c 0d 0a 2a 2a 20   platforms,..** 
b84e0 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72  there might be r
b84f0 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74  ace conditions t
b8500 68 61 74 20 63 61 6e 20 63 61 75 73 65 20 74 68  hat can cause th
b8510 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0d  ese routines to.
b8520 0a 2a 2a 20 64 65 6c 69 76 65 72 20 69 6e 63 6f  .** deliver inco
b8530 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20  rrect results.  
b8540 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69  In particular, i
b8550 66 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  f pthread_equal(
b8560 29 20 69 73 0d 0a 2a 2a 20 6e 6f 74 20 61 6e 20  ) is..** not an 
b8570 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e  atomic operation
b8580 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75  , then these rou
b8590 74 69 6e 65 73 20 6d 69 67 68 74 20 64 65 6c 69  tines might deli
b85a0 76 65 72 79 0d 0a 2a 2a 20 69 6e 63 6f 72 72 65  very..** incorre
b85b0 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f 6e 20  ct results.  On 
b85c0 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73 2c 20  most platforms, 
b85d0 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20  pthread_equal() 
b85e0 69 73 20 61 20 0d 0a 2a 2a 20 63 6f 6d 70 61 72  is a ..** compar
b85f0 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74 65  ison of two inte
b8600 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 65 72  gers and is ther
b8610 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20 42  efore atomic.  B
b8620 75 74 20 77 65 20 61 72 65 0d 0a 2a 2a 20 74 6f  ut we are..** to
b8630 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 20  ld that HPUX is 
b8640 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 66  not such a platf
b8650 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65  orm.  If so, the
b8660 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
b8670 0d 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c  ..** will not al
b8680 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63  ways work correc
b8690 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0d 0a 2a 2a  tly on HPUX...**
b86a0 0d 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c  ..** On those pl
b86b0 61 74 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74  atforms where pt
b86c0 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73  hread_equal() is
b86d0 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c   not atomic, SQL
b86e0 69 74 65 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  ite..** should b
b86f0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f  e compiled witho
b8700 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55  ut -DSQLITE_DEBU
b8710 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e 44 45  G and with -DNDE
b8720 42 55 47 20 74 6f 0d 0a 2a 2a 20 6d 61 6b 65 20  BUG to..** make 
b8730 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28 29  sure no assert()
b8740 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
b8750 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68 65  evaluated and he
b8760 6e 63 65 20 74 68 65 73 65 0d 0a 2a 2a 20 72 6f  nce these..** ro
b8770 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72  utines are never
b8780 20 63 61 6c 6c 65 64 2e 0d 0a 2a 2f 0d 0a 23 69   called...*/..#i
b8790 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  f !defined(NDEBU
b87a0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
b87b0 4c 49 54 45 5f 44 45 42 55 47 29 0d 0a 73 74 61  LITE_DEBUG)..sta
b87c0 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
b87d0 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33  utexHeld(sqlite3
b87e0 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a 20 20 72  _mutex *p){..  r
b87f0 65 74 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d  eturn (p->nRef!=
b8800 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75  0 && pthread_equ
b8810 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68  al(p->owner, pth
b8820 72 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0d 0a  read_self()));..
b8830 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  }..static int pt
b8840 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c  hreadMutexNothel
b8850 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
b8860 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70  *p){..  return p
b8870 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 74 68  ->nRef==0 || pth
b8880 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77  read_equal(p->ow
b8890 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c  ner, pthread_sel
b88a0 66 28 29 29 3d 3d 30 3b 0d 0a 7d 0d 0a 23 65 6e  f())==0;..}..#en
b88b0 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  dif..../*..** In
b88c0 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
b88d0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75  nitialize the mu
b88e0 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0d 0a  tex subsystem...
b88f0 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  */..static int p
b8900 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 28  threadMutexInit(
b8910 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
b8920 4c 49 54 45 5f 4f 4b 3b 20 7d 0d 0a 73 74 61 74  LITE_OK; }..stat
b8930 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
b8940 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
b8950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
b8960 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  }..../*..** The 
b8970 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
b8980 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
b8990 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0d 0a 2a  locates a new..*
b89a0 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75  * mutex and retu
b89b0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
b89c0 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75   it.  If it retu
b89d0 72 6e 73 20 4e 55 4c 4c 0d 0a 2a 2a 20 74 68 61  rns NULL..** tha
b89e0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d  t means that a m
b89f0 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62  utex could not b
b8a00 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  e allocated.  SQ
b8a10 4c 69 74 65 0d 0a 2a 2a 20 77 69 6c 6c 20 75 6e  Lite..** will un
b8a20 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61  wind its stack a
b8a30 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
b8a40 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  or.  The argumen
b8a50 74 0d 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  t..** to sqlite3
b8a60 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
b8a70 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
b8a80 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
b8a90 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a  :..**..** <ul>..
b8aa0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
b8ab0 4d 55 54 45 58 5f 46 41 53 54 0d 0a 2a 2a 20 3c  MUTEX_FAST..** <
b8ac0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
b8ad0 58 5f 52 45 43 55 52 53 49 56 45 0d 0a 2a 2a 20  X_RECURSIVE..** 
b8ae0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
b8af0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
b8b00 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  ..** <li>  SQLIT
b8b10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
b8b20 45 4d 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM..** <li>  SQL
b8b30 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
b8b40 5f 4d 45 4d 32 0d 0a 2a 2a 20 3c 6c 69 3e 20 20  _MEM2..** <li>  
b8b50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
b8b60 54 49 43 5f 50 52 4e 47 0d 0a 2a 2a 20 3c 6c 69  TIC_PRNG..** <li
b8b70 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
b8b80 53 54 41 54 49 43 5f 4c 52 55 0d 0a 2a 2a 20 3c  STATIC_LRU..** <
b8b90 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
b8ba0 58 5f 53 54 41 54 49 43 5f 50 4d 45 4d 0d 0a 2a  X_STATIC_PMEM..*
b8bb0 2a 20 3c 2f 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20  * </ul>..**..** 
b8bc0 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f  The first two co
b8bd0 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71  nstants cause sq
b8be0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
b8bf0 63 28 29 20 74 6f 20 63 72 65 61 74 65 0d 0a 2a  c() to create..*
b8c00 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20  * a new mutex.  
b8c10 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73  The new mutex is
b8c20 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20   recursive when 
b8c30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b8c40 55 52 53 49 56 45 0d 0a 2a 2a 20 69 73 20 75 73  URSIVE..** is us
b8c50 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73  ed but not neces
b8c60 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53  sarily so when S
b8c70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
b8c80 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 20 54 68   is used...** Th
b8c90 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
b8ca0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
b8cb0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
b8cc0 69 73 74 69 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 62  istinction..** b
b8cd0 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55  etween SQLITE_MU
b8ce0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e  TEX_RECURSIVE an
b8cf0 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  d SQLITE_MUTEX_F
b8d00 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0d 0a  AST if it does..
b8d10 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
b8d20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c   But SQLite will
b8d30 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20   only request a 
b8d40 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
b8d50 69 6e 0d 0a 2a 2a 20 63 61 73 65 73 20 77 68 65  in..** cases whe
b8d60 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65  re it really nee
b8d70 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61  ds one.  If a fa
b8d80 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69  ster non-recursi
b8d90 76 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 69 6d 70  ve mutex..** imp
b8da0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
b8db0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
b8dc0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
b8dd0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
b8de0 65 6d 0d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  em..** might ret
b8df0 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
b8e00 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
b8e10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
b8e20 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f  T...**..** The o
b8e30 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72  ther allowed par
b8e40 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74  ameters to sqlit
b8e50 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
b8e60 20 65 61 63 68 20 72 65 74 75 72 6e 0d 0a 2a 2a   each return..**
b8e70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
b8e80 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69  static preexisti
b8e90 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73  ng mutex.  Six s
b8ea0 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
b8eb0 65 0d 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68  e..** used by th
b8ec0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f  e current versio
b8ed0 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75  n of SQLite.  Fu
b8ee0 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
b8ef0 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 6d 61 79 20   SQLite..** may 
b8f00 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
b8f10 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
b8f20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
b8f30 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0d  re for internal.
b8f40 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
b8f50 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
b8f60 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
b8f70 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
b8f80 6f 75 6c 64 0d 0a 2a 2a 20 75 73 65 20 6f 6e 6c  ould..** use onl
b8f90 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
b8fa0 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
b8fb0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
b8fc0 41 53 54 20 6f 72 0d 0a 2a 2a 20 53 51 4c 49 54  AST or..** SQLIT
b8fd0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
b8fe0 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20  E...**..** Note 
b8ff0 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74  that if one of t
b9000 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
b9010 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c   parameters (SQL
b9020 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0d 0a  ITE_MUTEX_FAST..
b9030 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54  ** or SQLITE_MUT
b9040 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73  EX_RECURSIVE) is
b9050 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74   used then sqlit
b9060 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
b9070 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64  ..** returns a d
b9080 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f  ifferent mutex o
b9090 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42  n every call.  B
b90a0 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69  ut for the stati
b90b0 63 20 0d 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70  c ..** mutex typ
b90c0 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74  es, the same mut
b90d0 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  ex is returned o
b90e0 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61  n every call tha
b90f0 74 20 68 61 73 0d 0a 2a 2a 20 74 68 65 20 73 61  t has..** the sa
b9100 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0d  me type number..
b9110 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 71 6c 69  .*/..static sqli
b9120 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65  te3_mutex *pthre
b9130 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  adMutexAlloc(int
b9140 20 69 54 79 70 65 29 7b 0d 0a 20 20 73 74 61 74   iType){..  stat
b9150 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
b9160 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d   staticMutexes[]
b9170 20 3d 20 7b 0d 0a 20 20 20 20 53 51 4c 49 54 45   = {..    SQLITE
b9180 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b9190 5a 45 52 2c 0d 0a 20 20 20 20 53 51 4c 49 54 45  ZER,..    SQLITE
b91a0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b91b0 5a 45 52 2c 0d 0a 20 20 20 20 53 51 4c 49 54 45  ZER,..    SQLITE
b91c0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b91d0 5a 45 52 2c 0d 0a 20 20 20 20 53 51 4c 49 54 45  ZER,..    SQLITE
b91e0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b91f0 5a 45 52 2c 0d 0a 20 20 20 20 53 51 4c 49 54 45  ZER,..    SQLITE
b9200 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b9210 5a 45 52 2c 0d 0a 20 20 20 20 53 51 4c 49 54 45  ZER,..    SQLITE
b9220 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b9230 5a 45 52 0d 0a 20 20 7d 3b 0d 0a 20 20 73 71 6c  ZER..  };..  sql
b9240 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0d 0a  ite3_mutex *p;..
b9250 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20    switch( iType 
b9260 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ){..    case SQL
b9270 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
b9280 49 56 45 3a 20 7b 0d 0a 20 20 20 20 20 20 70 20  IVE: {..      p 
b9290 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
b92a0 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
b92b0 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 20  );..      if( p 
b92c0 29 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ){..#ifdef SQLIT
b92d0 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55  E_HOMEGROWN_RECU
b92e0 52 53 49 56 45 5f 4d 55 54 45 58 0d 0a 20 20 20  RSIVE_MUTEX..   
b92f0 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 72       /* If recur
b9300 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65  sive mutexes are
b9310 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20   not available, 
b9320 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 0d  we will have to.
b9330 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c  .        ** buil
b9340 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20  d our own.  See 
b9350 62 65 6c 6f 77 2e 20 2a 2f 0d 0a 20 20 20 20 20  below. */..     
b9360 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
b9370 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c  _init(&p->mutex,
b9380 20 30 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20   0);..#else..   
b9390 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 65       /* Use a re
b93a0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 66  cursive mutex if
b93b0 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65   it is available
b93c0 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 70 74 68   */..        pth
b93d0 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 74  read_mutexattr_t
b93e0 20 72 65 63 75 72 73 69 76 65 41 74 74 72 3b 0d   recursiveAttr;.
b93f0 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
b9400 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28  _mutexattr_init(
b9410 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b  &recursiveAttr);
b9420 0d 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ..        pthrea
b9430 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74  d_mutexattr_sett
b9440 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74  ype(&recursiveAt
b9450 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45  tr, PTHREAD_MUTE
b9460 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0d 0a 20  X_RECURSIVE);.. 
b9470 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
b9480 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
b9490 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41  tex, &recursiveA
b94a0 74 74 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  ttr);..        p
b94b0 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72  thread_mutexattr
b94c0 5f 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73  _destroy(&recurs
b94d0 69 76 65 41 74 74 72 29 3b 0d 0a 23 65 6e 64 69  iveAttr);..#endi
b94e0 66 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 55  f..#if SQLITE_MU
b94f0 54 45 58 5f 4e 52 45 46 0d 0a 20 20 20 20 20 20  TEX_NREF..      
b9500 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
b9510 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20  ..#endif..      
b9520 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  }..      break;.
b9530 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65  .    }..    case
b9540 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
b9550 53 54 3a 20 7b 0d 0a 20 20 20 20 20 20 70 20 3d  ST: {..      p =
b9560 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
b9570 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  ro( sizeof(*p) )
b9580 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  ;..      if( p )
b9590 7b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 55  {..#if SQLITE_MU
b95a0 54 45 58 5f 4e 52 45 46 0d 0a 20 20 20 20 20 20  TEX_NREF..      
b95b0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
b95c0 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20  ..#endif..      
b95d0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
b95e0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
b95f0 30 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  0);..      }..  
b9600 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
b9610 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  }..    default: 
b9620 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
b9630 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
b9640 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
b9650 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53  iType-2 < ArrayS
b9660 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65  ize(staticMutexe
b9670 73 29 20 29 3b 0d 0a 20 20 20 20 20 20 70 20 3d  s) );..      p =
b9680 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b   &staticMutexes[
b9690 69 54 79 70 65 2d 32 5d 3b 0d 0a 23 69 66 20 53  iType-2];..#if S
b96a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46  QLITE_MUTEX_NREF
b96b0 0d 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ..      p->id = 
b96c0 69 54 79 70 65 3b 0d 0a 23 65 6e 64 69 66 0d 0a  iType;..#endif..
b96d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
b96e0 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
b96f0 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a  rn p;..}....../*
b9700 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
b9710 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
b9720 70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 61  previously..** a
b9730 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20  llocated mutex. 
b9740 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66   SQLite is caref
b9750 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ul to deallocate
b9760 20 65 76 65 72 79 0d 0a 2a 2a 20 6d 75 74 65 78   every..** mutex
b9770 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74   that it allocat
b9780 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
b9790 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65  void pthreadMute
b97a0 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
b97b0 74 65 78 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65  tex *p){..  asse
b97c0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
b97d0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
b97e0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
b97f0 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d  _FAST || p->id==
b9800 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b9810 55 52 53 49 56 45 20 29 3b 0d 0a 20 20 70 74 68  URSIVE );..  pth
b9820 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72  read_mutex_destr
b9830 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0d 0a  oy(&p->mutex);..
b9840 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
b9850 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
b9860 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
b9870 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
b9880 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
b9890 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
b98a0 70 74 0d 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  pt..** to enter 
b98b0 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
b98c0 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
b98d0 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
b98e0 65 20 6d 75 74 65 78 2c 0d 0a 2a 2a 20 73 71 6c  e mutex,..** sql
b98f0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
b9900 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e  () will block an
b9910 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
b9920 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72  try() will retur
b9930 6e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  n..** SQLITE_BUS
b9940 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  Y.  The sqlite3_
b9950 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
b9960 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51  rface returns SQ
b9970 4c 49 54 45 5f 4f 4b 0d 0a 2a 2a 20 75 70 6f 6e  LITE_OK..** upon
b9980 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
b9990 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
b99a0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
b99b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
b99c0 20 63 61 6e 0d 0a 2a 2a 20 62 65 20 65 6e 74 65   can..** be ente
b99d0 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  red multiple tim
b99e0 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  es by the same t
b99f0 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20  hread.  In such 
b9a00 63 61 73 65 73 20 74 68 65 2c 0d 0a 2a 2a 20 6d  cases the,..** m
b9a10 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69  utex must be exi
b9a20 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d  ted an equal num
b9a30 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66  ber of times bef
b9a40 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65  ore another thre
b9a50 61 64 0d 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ad..** can enter
b9a60 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
b9a70 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
b9a80 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
b9a90 69 6e 64 20 6f 66 20 6d 75 74 65 78 0d 0a 2a 2a  ind of mutex..**
b9aa0 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
b9ab0 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
b9ac0 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d   undefined...*/.
b9ad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68  .static void pth
b9ae0 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73  readMutexEnter(s
b9af0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
b9b00 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  {..  assert( p->
b9b10 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
b9b20 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74  _RECURSIVE || pt
b9b30 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c  hreadMutexNothel
b9b40 64 28 70 29 20 29 3b 0d 0a 0d 0a 23 69 66 64 65  d(p) );....#ifde
b9b50 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f  f SQLITE_HOMEGRO
b9b60 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54  WN_RECURSIVE_MUT
b9b70 45 58 0d 0a 20 20 2f 2a 20 49 66 20 72 65 63 75  EX..  /* If recu
b9b80 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72  rsive mutexes ar
b9b90 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  e not available,
b9ba0 20 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f   then we have to
b9bb0 20 67 72 6f 77 0d 0a 20 20 2a 2a 20 6f 75 72 20   grow..  ** our 
b9bc0 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65  own.  This imple
b9bd0 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65  mentation assume
b9be0 73 20 74 68 61 74 20 70 74 68 72 65 61 64 5f 65  s that pthread_e
b9bf0 71 75 61 6c 28 29 0d 0a 20 20 2a 2a 20 69 73 20  qual()..  ** is 
b9c00 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 74  atomic - that it
b9c10 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 69   cannot be decei
b9c20 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e  ved into thinkin
b9c30 67 20 73 65 6c 66 0d 0a 20 20 2a 2a 20 61 6e 64  g self..  ** and
b9c40 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
b9c50 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
b9c60 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
b9c70 74 77 6f 20 76 61 6c 75 65 73 0d 0a 20 20 2a 2a  two values..  **
b9c80 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71   that are not eq
b9c90 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c  ual to self whil
b9ca0 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
b9cb0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
b9cc0 2e 0d 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70  ...  ** This imp
b9cd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f  lementation also
b9ce0 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72   assumes a coher
b9cf0 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74  ent cache - that
b9d00 20 0d 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65   ..  ** separate
b9d10 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f   processes canno
b9d20 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  t read different
b9d30 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
b9d40 20 73 61 6d 65 0d 0a 20 20 2a 2a 20 61 64 64 72   same..  ** addr
b9d50 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ess at the same 
b9d60 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72  time.  If either
b9d70 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f   of these two co
b9d80 6e 64 69 74 69 6f 6e 73 0d 0a 20 20 2a 2a 20 61  nditions..  ** a
b9d90 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e  re not met, then
b9da0 20 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c   the mutexes wil
b9db0 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c  l fail and probl
b9dc0 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e  ems will result.
b9dd0 0d 0a 20 20 2a 2f 0d 0a 20 20 7b 0d 0a 20 20 20  ..  */..  {..   
b9de0 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20   pthread_t self 
b9df0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
b9e00 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  ;..    if( p->nR
b9e10 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f  ef>0 && pthread_
b9e20 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
b9e30 73 65 6c 66 29 20 29 7b 0d 0a 20 20 20 20 20 20  self) ){..      
b9e40 70 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20 20 20  p->nRef++;..    
b9e50 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 74  }else{..      pt
b9e60 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
b9e70 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (&p->mutex);..  
b9e80 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
b9e90 52 65 66 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20  Ref==0 );..     
b9ea0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66   p->owner = self
b9eb0 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  ;..      p->nRef
b9ec0 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 1;..    }..  
b9ed0 7d 0d 0a 23 65 6c 73 65 0d 0a 20 20 2f 2a 20 55  }..#else..  /* U
b9ee0 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  se the built-in 
b9ef0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
b9f00 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76  s if they are av
b9f10 61 69 6c 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a  ailable...  */..
b9f20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
b9f30 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
b9f40 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 55 54  ..#if SQLITE_MUT
b9f50 45 58 5f 4e 52 45 46 0d 0a 20 20 61 73 73 65 72  EX_NREF..  asser
b9f60 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20  t( p->nRef>0 || 
b9f70 70 2d 3e 6f 77 6e 65 72 3d 3d 30 20 29 3b 0d 0a  p->owner==0 );..
b9f80 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68    p->owner = pth
b9f90 72 65 61 64 5f 73 65 6c 66 28 29 3b 0d 0a 20 20  read_self();..  
b9fa0 70 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 23 65 6e 64  p->nRef++;..#end
b9fb0 69 66 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  if..#endif....#i
b9fc0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
b9fd0 47 0d 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63  G..  if( p->trac
b9fe0 65 20 29 7b 0d 0a 20 20 20 20 70 72 69 6e 74 66  e ){..    printf
b9ff0 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70  ("enter mutex %p
ba000 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d   (%d) with nRef=
ba010 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61  %d\n", p, p->tra
ba020 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0d 0a 20  ce, p->nRef);.. 
ba030 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a 73   }..#endif..}..s
ba040 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
ba050 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  dMutexTry(sqlite
ba060 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a 20 20  3_mutex *p){..  
ba070 69 6e 74 20 72 63 3b 0d 0a 20 20 61 73 73 65 72  int rc;..  asser
ba080 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
ba090 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
ba0a0 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78   || pthreadMutex
ba0b0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0d 0a 0d  Notheld(p) );...
ba0c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
ba0d0 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49  OMEGROWN_RECURSI
ba0e0 56 45 5f 4d 55 54 45 58 0d 0a 20 20 2f 2a 20 49  VE_MUTEX..  /* I
ba0f0 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
ba100 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
ba110 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  lable, then we h
ba120 61 76 65 20 74 6f 20 67 72 6f 77 0d 0a 20 20 2a  ave to grow..  *
ba130 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73  * our own.  This
ba140 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
ba150 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68  assumes that pth
ba160 72 65 61 64 5f 65 71 75 61 6c 28 29 0d 0a 20 20  read_equal()..  
ba170 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74  ** is atomic - t
ba180 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65  hat it cannot be
ba190 20 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74   deceived into t
ba1a0 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0d 0a 20 20  hinking self..  
ba1b0 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20  ** and p->owner 
ba1c0 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e  are equal if p->
ba1d0 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65  owner changes be
ba1e0 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73  tween two values
ba1f0 0d 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 20  ..  ** that are 
ba200 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 6c  not equal to sel
ba210 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d 70  f while the comp
ba220 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67  arison is taking
ba230 20 70 6c 61 63 65 2e 0d 0a 20 20 2a 2a 20 54 68   place...  ** Th
ba240 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
ba250 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61  n also assumes a
ba260 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20   coherent cache 
ba270 2d 20 74 68 61 74 20 0d 0a 20 20 2a 2a 20 73 65  - that ..  ** se
ba280 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73  parate processes
ba290 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66   cannot read dif
ba2a0 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72  ferent values fr
ba2b0 6f 6d 20 74 68 65 20 73 61 6d 65 0d 0a 20 20 2a  om the same..  *
ba2c0 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65  * address at the
ba2d0 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20   same time.  If 
ba2e0 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
ba2f0 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0d 0a  two conditions..
ba300 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74    ** are not met
ba310 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78  , then the mutex
ba320 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  es will fail and
ba330 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72   problems will r
ba340 65 73 75 6c 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  esult...  */..  
ba350 7b 0d 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74  {..    pthread_t
ba360 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f   self = pthread_
ba370 73 65 6c 66 28 29 3b 0d 0a 20 20 20 20 69 66 28  self();..    if(
ba380 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 74   p->nRef>0 && pt
ba390 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f  hread_equal(p->o
ba3a0 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0d 0a  wner, self) ){..
ba3b0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
ba3c0 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
ba3d0 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 65 6c  ITE_OK;..    }el
ba3e0 73 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d  se if( pthread_m
ba3f0 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d  utex_trylock(&p-
ba400 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0d 0a 20  >mutex)==0 ){.. 
ba410 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
ba420 6e 52 65 66 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  nRef==0 );..    
ba430 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c    p->owner = sel
ba440 66 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65  f;..      p->nRe
ba450 66 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 72 63  f = 1;..      rc
ba460 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
ba470 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
ba480 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
ba490 59 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  Y;..    }..  }..
ba4a0 23 65 6c 73 65 0d 0a 20 20 2f 2a 20 55 73 65 20  #else..  /* Use 
ba4b0 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63  the built-in rec
ba4c0 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69  ursive mutexes i
ba4d0 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c  f they are avail
ba4e0 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  able...  */..  i
ba4f0 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  f( pthread_mutex
ba500 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  _trylock(&p->mut
ba510 65 78 29 3d 3d 30 20 29 7b 0d 0a 23 69 66 20 53  ex)==0 ){..#if S
ba520 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46  QLITE_MUTEX_NREF
ba530 0d 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  ..    p->owner =
ba540 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
ba550 0d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  ..    p->nRef++;
ba560 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 72 63  ..#endif..    rc
ba570 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
ba580 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20   }else{..    rc 
ba590 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a  = SQLITE_BUSY;..
ba5a0 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23    }..#endif....#
ba5b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
ba5c0 55 47 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  UG..  if( rc==SQ
ba5d0 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72  LITE_OK && p->tr
ba5e0 61 63 65 20 29 7b 0d 0a 20 20 20 20 70 72 69 6e  ace ){..    prin
ba5f0 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20  tf("enter mutex 
ba600 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65  %p (%d) with nRe
ba610 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74  f=%d\n", p, p->t
ba620 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0d  race, p->nRef);.
ba630 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  .  }..#endif..  
ba640 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
ba650 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ./*..** The sqli
ba660 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
ba670 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
ba680 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
ba690 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  ..** previously 
ba6a0 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
ba6b0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
ba6c0 20 62 65 68 61 76 69 6f 72 0d 0a 2a 2a 20 69 73   behavior..** is
ba6d0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
ba6e0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
ba6f0 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
ba700 20 6f 72 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 63   or..** is not c
ba710 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74  urrently allocat
ba720 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  ed.  SQLite will
ba730 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72   never do either
ba740 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
ba750 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c  id pthreadMutexL
ba760 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
ba770 65 78 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72  ex *p){..  asser
ba780 74 28 20 70 74 68 72 65 61 64 4d 75 74 65 78 48  t( pthreadMutexH
ba790 65 6c 64 28 70 29 20 29 3b 0d 0a 23 69 66 20 53  eld(p) );..#if S
ba7a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46  QLITE_MUTEX_NREF
ba7b0 0d 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a  ..  p->nRef--;..
ba7c0 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30    if( p->nRef==0
ba7d0 20 29 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 30 3b   ) p->owner = 0;
ba7e0 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 61 73 73 65  ..#endif..  asse
ba7f0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c  rt( p->nRef==0 |
ba800 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
ba810 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
ba820 29 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  );....#ifdef SQL
ba830 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
ba840 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0d 0a 20  CURSIVE_MUTEX.. 
ba850 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
ba860 29 7b 0d 0a 20 20 20 20 70 74 68 72 65 61 64 5f  ){..    pthread_
ba870 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d  mutex_unlock(&p-
ba880 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a 23  >mutex);..  }..#
ba890 65 6c 73 65 0d 0a 20 20 70 74 68 72 65 61 64 5f  else..  pthread_
ba8a0 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d  mutex_unlock(&p-
ba8b0 3e 6d 75 74 65 78 29 3b 0d 0a 23 65 6e 64 69 66  >mutex);..#endif
ba8c0 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
ba8d0 45 5f 44 45 42 55 47 0d 0a 20 20 69 66 28 20 70  E_DEBUG..  if( p
ba8e0 2d 3e 74 72 61 63 65 20 29 7b 0d 0a 20 20 20 20  ->trace ){..    
ba8f0 70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75  printf("leave mu
ba900 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68  tex %p (%d) with
ba910 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20   nRef=%d\n", p, 
ba920 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65  p->trace, p->nRe
ba930 66 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  f);..  }..#endif
ba940 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  ..}....SQLITE_PR
ba950 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
ba960 74 65 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73  tex_methods cons
ba970 74 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  t *sqlite3Defaul
ba980 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20  tMutex(void){.. 
ba990 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
ba9a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
ba9b0 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0d 0a  ods sMutex = {..
ba9c0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
ba9d0 49 6e 69 74 2c 0d 0a 20 20 20 20 70 74 68 72 65  Init,..    pthre
ba9e0 61 64 4d 75 74 65 78 45 6e 64 2c 0d 0a 20 20 20  adMutexEnd,..   
ba9f0 20 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c   pthreadMutexAll
baa00 6f 63 2c 0d 0a 20 20 20 20 70 74 68 72 65 61 64  oc,..    pthread
baa10 4d 75 74 65 78 46 72 65 65 2c 0d 0a 20 20 20 20  MutexFree,..    
baa20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
baa30 72 2c 0d 0a 20 20 20 20 70 74 68 72 65 61 64 4d  r,..    pthreadM
baa40 75 74 65 78 54 72 79 2c 0d 0a 20 20 20 20 70 74  utexTry,..    pt
baa50 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c  hreadMutexLeave,
baa60 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
baa70 44 45 42 55 47 0d 0a 20 20 20 20 70 74 68 72 65  DEBUG..    pthre
baa80 61 64 4d 75 74 65 78 48 65 6c 64 2c 0d 0a 20 20  adMutexHeld,..  
baa90 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f    pthreadMutexNo
baaa0 74 68 65 6c 64 0d 0a 23 65 6c 73 65 0d 0a 20 20  theld..#else..  
baab0 20 20 30 2c 0d 0a 20 20 20 20 30 0d 0a 23 65 6e    0,..    0..#en
baac0 64 69 66 0d 0a 20 20 7d 3b 0d 0a 0d 0a 20 20 72  dif..  };....  r
baad0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0d 0a  eturn &sMutex;..
baae0 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  }....#endif /* S
baaf0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
bab00 45 41 44 53 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  EADS */..../****
bab10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
bab20 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a  f mutex_unix.c *
bab30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
bab60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
bab70 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32  n file mutex_w32
bab80 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
bab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
babb0 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20  .** 2007 August 
babc0 31 34 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  14..**..** The a
babd0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
babe0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
babf0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
bac00 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
bac10 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
bac20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
bac30 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
bac40 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
bac50 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
bac60 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
bac70 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
bac80 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
bac90 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
baca0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
bacb0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
bacc0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
bacd0 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
bace0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
bacf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54  **********..** T
bad30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
bad40 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
bad50 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
bad60 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 69 6e   mutexes for win
bad70 33 32 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  32..*/..../*..**
bad80 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
bad90 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75  s file is only u
bada0 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f  sed if we are co
badb0 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72  mpiling multithr
badc0 65 61 64 65 64 0d 0a 2a 2a 20 6f 6e 20 61 20 77  eaded..** on a w
badd0 69 6e 33 32 20 73 79 73 74 65 6d 2e 0d 0a 2a 2f  in32 system...*/
bade0 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
badf0 4d 55 54 45 58 5f 57 33 32 0d 0a 0d 0a 2f 2a 0d  MUTEX_W32..../*.
bae00 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
bae10 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
bae20 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
bae30 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
bae40 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  re...*/..struct 
bae50 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0d  sqlite3_mutex {.
bae60 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45 43 54  .  CRITICAL_SECT
bae70 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a  ION mutex;    /*
bae80 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
bae90 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0d 0a  ng the lock */..
baea0 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20    int id;       
baeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
baec0 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0d 0a 23  Mutex type */..#
baed0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
baee0 55 47 0d 0a 20 20 76 6f 6c 61 74 69 6c 65 20 69  UG..  volatile i
baef0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
baf00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
baf10 74 65 72 61 6e 63 65 73 20 2a 2f 0d 0a 20 20 76  terances */..  v
baf20 6f 6c 61 74 69 6c 65 20 44 57 4f 52 44 20 6f 77  olatile DWORD ow
baf30 6e 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 72  ner;      /* Thr
baf40 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ead holding this
baf50 20 6d 75 74 65 78 20 2a 2f 0d 0a 20 20 69 6e 74   mutex */..  int
baf60 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20   trace;         
baf70 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
baf80 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73  to trace changes
baf90 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d   */..#endif..};.
bafa0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
bafb0 57 33 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  W32_MUTEX_INITIA
bafc0 4c 49 5a 45 52 20 7b 20 30 20 7d 0d 0a 23 69 66  LIZER { 0 }..#if
bafd0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
bafe0 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
baff0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
bb000 5a 45 52 20 7b 20 53 51 4c 49 54 45 5f 57 33 32  ZER { SQLITE_W32
bb010 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
bb020 45 52 2c 20 30 2c 20 30 4c 2c 20 28 44 57 4f 52  ER, 0, 0L, (DWOR
bb030 44 29 30 2c 20 30 20 7d 0d 0a 23 65 6c 73 65 0d  D)0, 0 }..#else.
bb040 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33  .#define SQLITE3
bb050 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
bb060 45 52 20 7b 20 53 51 4c 49 54 45 5f 57 33 32 5f  ER { SQLITE_W32_
bb070 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
bb080 52 2c 20 30 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  R, 0 }..#endif..
bb090 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
bb0a0 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  true (non-zero) 
bb0b0 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  if we are runnin
bb0c0 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57  g under WinNT, W
bb0d0 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0d 0a 2a 2a  in2K, WinXP,..**
bb0e0 20 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75   or WinCE.  Retu
bb0f0 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20  rn false (zero) 
bb100 66 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38  for Win95, Win98
bb110 2c 20 6f 72 20 57 69 6e 4d 45 2e 0d 0a 2a 2a 0d  , or WinME...**.
bb120 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e 20 69  .** Here is an i
bb130 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 65 72  nteresting obser
bb140 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20  vation:  Win95, 
bb150 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45  Win98, and WinME
bb160 20 6c 61 63 6b 0d 0a 2a 2a 20 74 68 65 20 4c 6f   lack..** the Lo
bb170 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 20  ckFileEx() API. 
bb180 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 6c   But we can stil
bb190 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e  l statically lin
bb1a0 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0d 0a  k against that..
bb1b0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
bb1c0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
bb1d0 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
bb1e0 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
bb1f0 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 74 68 69 73 20  all to..** this 
bb200 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
bb210 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  to determine if 
bb220 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39  the host is Win9
bb230 35 2f 39 38 2f 4d 45 20 6f 72 0d 0a 2a 2a 20 57  5/98/ME or..** W
bb240 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68  inNT/2K/XP so th
bb250 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20  at we will know 
bb260 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77  whether or not w
bb270 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c  e can safely cal
bb280 6c 0d 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  l..** the LockFi
bb290 6c 65 45 78 28 29 20 41 50 49 2e 0d 0a 2a 2a 0d  leEx() API...**.
bb2a0 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 20  .** mutexIsNT() 
bb2b0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  is only used for
bb2c0 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 69   the TryEnterCri
bb2d0 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 41  ticalSection() A
bb2e0 50 49 20 63 61 6c 6c 2c 0d 0a 2a 2a 20 77 68 69  PI call,..** whi
bb2f0 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ch is only avail
bb300 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70  able if your app
bb310 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d  lication was com
bb320 70 69 6c 65 64 20 77 69 74 68 20 0d 0a 2a 2a 20  piled with ..** 
bb330 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66  _WIN32_WINNT def
bb340 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20  ined to a value 
bb350 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72  >= 0x0400.  Curr
bb360 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0d  ently, the only.
bb370 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45  .** call to TryE
bb380 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74  nterCriticalSect
bb390 69 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27  ion() is #ifdef'
bb3a0 65 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65  ed out, so #ifde
bb3b0 66 20 0d 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20  f ..** this out 
bb3c0 61 73 20 77 65 6c 6c 2e 0d 0a 2a 2f 0d 0a 23 69  as well...*/..#i
bb3d0 66 20 30 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  f 0..#if SQLITE_
bb3e0 4f 53 5f 57 49 4e 43 45 0d 0a 23 20 64 65 66 69  OS_WINCE..# defi
bb3f0 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20  ne mutexIsNT()  
bb400 28 31 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 73 74  (1)..#else..  st
bb410 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49 73  atic int mutexIs
bb420 4e 54 28 76 6f 69 64 29 7b 0d 0a 20 20 20 20 73  NT(void){..    s
bb430 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65  tatic int osType
bb440 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 6f   = 0;..    if( o
bb450 73 54 79 70 65 3d 3d 30 20 29 7b 0d 0a 20 20 20  sType==0 ){..   
bb460 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f     OSVERSIONINFO
bb470 20 73 49 6e 66 6f 3b 0d 0a 20 20 20 20 20 20 73   sInfo;..      s
bb480 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e  Info.dwOSVersion
bb490 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f  InfoSize = sizeo
bb4a0 66 28 73 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20  f(sInfo);..     
bb4b0 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73   GetVersionEx(&s
bb4c0 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 6f 73  Info);..      os
bb4d0 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50  Type = sInfo.dwP
bb4e0 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50  latformId==VER_P
bb4f0 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54  LATFORM_WIN32_NT
bb500 20 3f 20 32 20 3a 20 31 3b 0d 0a 20 20 20 20 7d   ? 2 : 1;..    }
bb510 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54  ..    return osT
bb520 79 70 65 3d 3d 32 3b 0d 0a 20 20 7d 0d 0a 23 65  ype==2;..  }..#e
bb530 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
bb540 53 5f 57 49 4e 43 45 20 2a 2f 0d 0a 23 65 6e 64  S_WINCE */..#end
bb550 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  if....#ifdef SQL
bb560 49 54 45 5f 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a  ITE_DEBUG../*..*
bb570 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
bb580 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
bb590 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
bb5a0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
bb5b0 72 65 0d 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20  re..** intended 
bb5c0 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73  for use only ins
bb5d0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
bb5e0 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73 74  tements...*/..st
bb5f0 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65  atic int winMute
bb600 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
bb610 74 65 78 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75  tex *p){..  retu
bb620 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26  rn p->nRef!=0 &&
bb630 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
bb640 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b  rrentThreadId();
bb650 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  ..}..static int 
bb660 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 32  winMutexNotheld2
bb670 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
bb680 70 2c 20 44 57 4f 52 44 20 74 69 64 29 7b 0d 0a  p, DWORD tid){..
bb690 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66    return p->nRef
bb6a0 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21  ==0 || p->owner!
bb6b0 3d 74 69 64 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63  =tid;..}..static
bb6c0 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74   int winMutexNot
bb6d0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
bb6e0 65 78 20 2a 70 29 7b 0d 0a 20 20 44 57 4f 52 44  ex *p){..  DWORD
bb6f0 20 74 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e   tid = GetCurren
bb700 74 54 68 72 65 61 64 49 64 28 29 3b 20 0d 0a 20  tThreadId(); .. 
bb710 20 72 65 74 75 72 6e 20 77 69 6e 4d 75 74 65 78   return winMutex
bb720 4e 6f 74 68 65 6c 64 32 28 70 2c 20 74 69 64 29  Notheld2(p, tid)
bb730 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
bb740 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c  ../*..** Initial
bb750 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
bb760 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
bb770 75 62 73 79 73 74 65 6d 2e 0d 0a 2a 2f 0d 0a 73  ubsystem...*/..s
bb780 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
bb790 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 74 61  tex winMutex_sta
bb7a0 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 20 3d 20  ticMutexes[6] = 
bb7b0 7b 0d 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  {..  SQLITE3_MUT
bb7c0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0d  EX_INITIALIZER,.
bb7d0 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58  .  SQLITE3_MUTEX
bb7e0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0d 0a 20  _INITIALIZER,.. 
bb7f0 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
bb800 4e 49 54 49 41 4c 49 5a 45 52 2c 0d 0a 20 20 53  NITIALIZER,..  S
bb810 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
bb820 54 49 41 4c 49 5a 45 52 2c 0d 0a 20 20 53 51 4c  TIALIZER,..  SQL
bb830 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
bb840 41 4c 49 5a 45 52 2c 0d 0a 20 20 53 51 4c 49 54  ALIZER,..  SQLIT
bb850 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
bb860 49 5a 45 52 0d 0a 7d 3b 0d 0a 73 74 61 74 69 63  IZER..};..static
bb870 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 5f 69 73   int winMutex_is
bb880 49 6e 69 74 20 3d 20 30 3b 0d 0a 2f 2a 20 41 73  Init = 0;../* As
bb890 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 29 20   winMutexInit() 
bb8a0 61 6e 64 20 77 69 6e 4d 75 74 65 78 45 6e 64 28  and winMutexEnd(
bb8b0 29 20 61 72 65 20 63 61 6c 6c 65 64 20 61 73 20  ) are called as 
bb8c0 70 61 72 74 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  part..** of the 
bb8d0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
bb8e0 7a 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73  ze and sqlite3_s
bb8f0 68 75 74 64 6f 77 6e 28 29 0d 0a 2a 2a 20 70 72  hutdown()..** pr
bb900 6f 63 65 73 73 69 6e 67 2c 20 74 68 65 20 22 69  ocessing, the "i
bb910 6e 74 65 72 6c 6f 63 6b 65 64 22 20 6d 61 67 69  nterlocked" magi
bb920 63 20 69 73 20 70 72 6f 62 61 62 6c 79 20 6e 6f  c is probably no
bb930 74 0d 0a 2a 2a 20 73 74 72 69 63 74 6c 79 20 6e  t..** strictly n
bb940 65 63 65 73 73 61 72 79 2e 0d 0a 2a 2f 0d 0a 73  ecessary...*/..s
bb950 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d 75  tatic long winMu
bb960 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0d 0a 0d  tex_lock = 0;...
bb970 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
bb980 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
bb990 0d 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74  ..  /* The first
bb9a0 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f   to increment to
bb9b0 20 31 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69   1 does actual i
bb9c0 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f  nitialization */
bb9d0 0d 0a 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63  ..  if( Interloc
bb9e0 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e  kedCompareExchan
bb9f0 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63  ge(&winMutex_loc
bba00 6b 2c 20 31 2c 20 30 29 3d 3d 30 20 29 7b 0d 0a  k, 1, 0)==0 ){..
bba10 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20      int i;..    
bba20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
bba30 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74  Size(winMutex_st
bba40 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b  aticMutexes); i+
bba50 2b 29 7b 0d 0a 20 20 20 20 20 20 49 6e 69 74 69  +){..      Initi
bba60 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63  alizeCriticalSec
bba70 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73  tion(&winMutex_s
bba80 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e  taticMutexes[i].
bba90 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20 7d 0d 0a  mutex);..    }..
bbaa0 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49      winMutex_isI
bbab0 6e 69 74 20 3d 20 31 3b 0d 0a 20 20 7d 65 6c 73  nit = 1;..  }els
bbac0 65 7b 0d 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f  e{..    /* Someo
bbad0 6e 65 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68  ne else is in th
bbae0 65 20 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69  e process of ini
bbaf0 74 69 6e 67 20 74 68 65 20 73 74 61 74 69 63 20  ting the static 
bbb00 6d 75 74 65 78 65 73 20 2a 2f 0d 0a 20 20 20 20  mutexes */..    
bbb10 77 68 69 6c 65 28 20 21 77 69 6e 4d 75 74 65 78  while( !winMutex
bbb20 5f 69 73 49 6e 69 74 20 29 7b 0d 0a 20 20 20 20  _isInit ){..    
bbb30 20 20 53 6c 65 65 70 28 31 29 3b 0d 0a 20 20 20    Sleep(1);..   
bbb40 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
bbb50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0d 0a 7d  n SQLITE_OK; ..}
bbb60 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ....static int w
bbb70 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  inMutexEnd(void)
bbb80 7b 20 0d 0a 20 20 2f 2a 20 54 68 65 20 66 69 72  { ..  /* The fir
bbb90 73 74 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 20  st to decrement 
bbba0 74 6f 20 30 20 64 6f 65 73 20 61 63 74 75 61 6c  to 0 does actual
bbbb0 20 73 68 75 74 64 6f 77 6e 20 0d 0a 20 20 2a 2a   shutdown ..  **
bbbc0 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62   (which should b
bbbd0 65 20 74 68 65 20 6c 61 73 74 20 74 6f 20 73 68  e the last to sh
bbbe0 75 74 64 6f 77 6e 2e 29 20 2a 2f 0d 0a 20 20 69  utdown.) */..  i
bbbf0 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 6f  f( InterlockedCo
bbc00 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 77  mpareExchange(&w
bbc10 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c  inMutex_lock, 0,
bbc20 20 31 29 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 69   1)==1 ){..    i
bbc30 66 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e  f( winMutex_isIn
bbc40 69 74 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20  it==1 ){..      
bbc50 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20 66 6f  int i;..      fo
bbc60 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69  r(i=0; i<ArraySi
bbc70 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74  ze(winMutex_stat
bbc80 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29  icMutexes); i++)
bbc90 7b 0d 0a 20 20 20 20 20 20 20 20 44 65 6c 65 74  {..        Delet
bbca0 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
bbcb0 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69  (&winMutex_stati
bbcc0 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65  cMutexes[i].mute
bbcd0 78 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  x);..      }..  
bbce0 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49      winMutex_isI
bbcf0 6e 69 74 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d  nit = 0;..    }.
bbd00 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
bbd10 51 4c 49 54 45 5f 4f 4b 3b 20 0d 0a 7d 0d 0a 0d  QLITE_OK; ..}...
bbd20 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ./*..** The sqli
bbd30 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
bbd40 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61  ) routine alloca
bbd50 74 65 73 20 61 20 6e 65 77 0d 0a 2a 2a 20 6d 75  tes a new..** mu
bbd60 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
bbd70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
bbd80 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20    If it returns 
bbd90 4e 55 4c 4c 0d 0a 2a 2a 20 74 68 61 74 20 6d 65  NULL..** that me
bbda0 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
bbdb0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
bbdc0 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
bbdd0 0d 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  ..** will unwind
bbde0 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
bbdf0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
bbe00 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0d 0a 2a   The argument..*
bbe10 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
bbe20 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
bbe30 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
bbe40 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0d 0a 2a  er constants:..*
bbe50 2a 0d 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c  *..** <ul>..** <
bbe60 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
bbe70 58 5f 46 41 53 54 0d 0a 2a 2a 20 3c 6c 69 3e 20  X_FAST..** <li> 
bbe80 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
bbe90 43 55 52 53 49 56 45 0d 0a 2a 2a 20 3c 6c 69 3e  CURSIVE..** <li>
bbea0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
bbeb0 54 41 54 49 43 5f 4d 41 53 54 45 52 0d 0a 2a 2a  TATIC_MASTER..**
bbec0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
bbed0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0d 0a  TEX_STATIC_MEM..
bbee0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
bbef0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
bbf00 32 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  2..** <li>  SQLI
bbf10 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
bbf20 50 52 4e 47 0d 0a 2a 2a 20 3c 6c 69 3e 20 20 53  PRNG..** <li>  S
bbf30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
bbf40 49 43 5f 4c 52 55 0d 0a 2a 2a 20 3c 6c 69 3e 20  IC_LRU..** <li> 
bbf50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
bbf60 41 54 49 43 5f 50 4d 45 4d 0d 0a 2a 2a 20 3c 2f  ATIC_PMEM..** </
bbf70 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ul>..**..** The 
bbf80 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
bbf90 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65  nts cause sqlite
bbfa0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
bbfb0 74 6f 20 63 72 65 61 74 65 0d 0a 2a 2a 20 61 20  to create..** a 
bbfc0 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
bbfd0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
bbfe0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
bbff0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
bc000 56 45 0d 0a 2a 2a 20 69 73 20 75 73 65 64 20 62  VE..** is used b
bc010 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  ut not necessari
bc020 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54  ly so when SQLIT
bc030 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20  E_MUTEX_FAST is 
bc040 75 73 65 64 2e 0d 0a 2a 2a 20 54 68 65 20 6d 75  used...** The mu
bc050 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
bc060 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
bc070 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
bc080 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 62 65 74 77 65  nction..** betwe
bc090 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
bc0a0 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51  RECURSIVE and SQ
bc0b0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
bc0c0 69 66 20 69 74 20 64 6f 65 73 0d 0a 2a 2a 20 6e  if it does..** n
bc0d0 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74  ot want to.  But
bc0e0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c   SQLite will onl
bc0f0 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75  y request a recu
bc100 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0d 0a  rsive mutex in..
bc110 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69  ** cases where i
bc120 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f  t really needs o
bc130 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72  ne.  If a faster
bc140 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d   non-recursive m
bc150 75 74 65 78 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65  utex..** impleme
bc160 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
bc170 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
bc180 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
bc190 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0d 0a  utex subsystem..
bc1a0 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ** might return 
bc1b0 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20  such a mutex in 
bc1c0 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49  response to SQLI
bc1d0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0d 0a  TE_MUTEX_FAST...
bc1e0 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72  **..** The other
bc1f0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74   allowed paramet
bc200 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ers to sqlite3_m
bc210 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63  utex_alloc() eac
bc220 68 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 61 20 70  h return..** a p
bc230 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
bc240 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
bc250 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 69  utex.  Six stati
bc260 63 20 6d 75 74 65 78 65 73 20 61 72 65 0d 0a 2a  c mutexes are..*
bc270 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
bc280 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
bc290 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
bc2a0 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
bc2b0 69 74 65 0d 0a 2a 2a 20 6d 61 79 20 61 64 64 20  ite..** may add 
bc2c0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
bc2d0 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
bc2e0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
bc2f0 6f 72 20 69 6e 74 65 72 6e 61 6c 0d 0a 2a 2a 20  or internal..** 
bc300 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e  use by SQLite on
bc310 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ly.  Application
bc320 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74  s that use SQLit
bc330 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64  e mutexes should
bc340 0d 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68  ..** use only th
bc350 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65  e dynamic mutexe
bc360 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  s returned by SQ
bc370 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
bc380 6f 72 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  or..** SQLITE_MU
bc390 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0d 0a  TEX_RECURSIVE...
bc3a0 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  **..** Note that
bc3b0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
bc3c0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
bc3d0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
bc3e0 4d 55 54 45 58 5f 46 41 53 54 0d 0a 2a 2a 20 6f  MUTEX_FAST..** o
bc3f0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
bc400 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
bc410 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
bc420 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0d 0a 2a 2a  utex_alloc()..**
bc430 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65   returns a diffe
bc440 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76  rent mutex on ev
bc450 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66  ery call.  But f
bc460 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0d 0a  or the static ..
bc470 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20  ** mutex types, 
bc480 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69  the same mutex i
bc490 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76  s returned on ev
bc4a0 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61  ery call that ha
bc4b0 73 0d 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74  s..** the same t
bc4c0 79 70 65 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2f 0d  ype number...*/.
bc4d0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
bc4e0 6d 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41  mutex *winMutexA
bc4f0 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
bc500 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
bc510 78 20 2a 70 3b 0d 0a 0d 0a 20 20 73 77 69 74 63  x *p;....  switc
bc520 68 28 20 69 54 79 70 65 20 29 7b 0d 0a 20 20 20  h( iType ){..   
bc530 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
bc540 45 58 5f 46 41 53 54 3a 0d 0a 20 20 20 20 63 61  EX_FAST:..    ca
bc550 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
bc560 52 45 43 55 52 53 49 56 45 3a 20 7b 0d 0a 20 20  RECURSIVE: {..  
bc570 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
bc580 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
bc590 66 28 2a 70 29 20 29 3b 0d 0a 20 20 20 20 20 20  f(*p) );..      
bc5a0 69 66 28 20 70 20 29 7b 20 20 0d 0a 23 69 66 64  if( p ){  ..#ifd
bc5b0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
bc5c0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  .        p->id =
bc5d0 20 69 54 79 70 65 3b 0d 0a 23 65 6e 64 69 66 0d   iType;..#endif.
bc5e0 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c  .        Initial
bc5f0 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69  izeCriticalSecti
bc600 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0d 0a  on(&p->mutex);..
bc610 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62        }..      b
bc620 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
bc630 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20    default: {..  
bc640 20 20 20 20 61 73 73 65 72 74 28 20 77 69 6e 4d      assert( winM
bc650 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29  utex_isInit==1 )
bc660 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
bc670 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
bc680 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
bc690 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53  iType-2 < ArrayS
bc6a0 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 61  ize(winMutex_sta
bc6b0 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0d 0a  ticMutexes) );..
bc6c0 20 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d 75        p = &winMu
bc6d0 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65  tex_staticMutexe
bc6e0 73 5b 69 54 79 70 65 2d 32 5d 3b 0d 0a 23 69 66  s[iType-2];..#if
bc6f0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
bc700 0d 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ..      p->id = 
bc710 69 54 79 70 65 3b 0d 0a 23 65 6e 64 69 66 0d 0a  iType;..#endif..
bc720 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
bc730 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
bc740 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a  rn p;..}....../*
bc750 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
bc760 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
bc770 70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 61  previously..** a
bc780 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20  llocated mutex. 
bc790 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66   SQLite is caref
bc7a0 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ul to deallocate
bc7b0 20 65 76 65 72 79 0d 0a 2a 2a 20 6d 75 74 65 78   every..** mutex
bc7c0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74   that it allocat
bc7d0 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
bc7e0 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 65  void winMutexFre
bc7f0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
bc800 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  *p){..  assert( 
bc810 70 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  p );..  assert( 
bc820 70 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 2d  p->nRef==0 && p-
bc830 3e 6f 77 6e 65 72 3d 3d 30 20 29 3b 0d 0a 20 20  >owner==0 );..  
bc840 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
bc850 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
bc860 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54   || p->id==SQLIT
bc870 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
bc880 45 20 29 3b 0d 0a 20 20 44 65 6c 65 74 65 43 72  E );..  DeleteCr
bc890 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70  iticalSection(&p
bc8a0 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 73 71 6c  ->mutex);..  sql
bc8b0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 7d  ite3_free(p);..}
bc8c0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73  ..../*..** The s
bc8d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
bc8e0 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  er() and sqlite3
bc8f0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75  _mutex_try() rou
bc900 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0d 0a 2a  tines attempt..*
bc910 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
bc920 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20  ex.  If another 
bc930 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
bc940 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
bc950 65 78 2c 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ex,..** sqlite3_
bc960 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69  mutex_enter() wi
bc970 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c  ll block and sql
bc980 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
bc990 20 77 69 6c 6c 20 72 65 74 75 72 6e 0d 0a 2a 2a   will return..**
bc9a0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
bc9b0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
bc9c0 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
bc9d0 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
bc9e0 4f 4b 0d 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  OK..** upon succ
bc9f0 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d  essful entry.  M
bca00 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
bca10 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45  sing SQLITE_MUTE
bca20 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0d  X_RECURSIVE can.
bca30 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
bca40 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
bca50 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
bca60 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
bca70 20 74 68 65 2c 0d 0a 2a 2a 20 6d 75 74 65 78 20   the,..** mutex 
bca80 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
bca90 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
bcaa0 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
bcab0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0d 0a 2a  nother thread..*
bcac0 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66  * can enter.  If
bcad0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
bcae0 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
bcaf0 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
bcb00 66 20 6d 75 74 65 78 0d 0a 2a 2a 20 6d 6f 72 65  f mutex..** more
bcb10 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20   than once, the 
bcb20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
bcb30 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  fined...*/..stat
bcb40 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78  ic void winMutex
bcb50 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  Enter(sqlite3_mu
bcb60 74 65 78 20 2a 70 29 7b 0d 0a 23 69 66 64 65 66  tex *p){..#ifdef
bcb70 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
bcb80 20 44 57 4f 52 44 20 74 69 64 20 3d 20 47 65 74   DWORD tid = Get
bcb90 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28  CurrentThreadId(
bcba0 29 3b 20 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ); ..  assert( p
bcbb0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
bcbc0 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
bcbd0 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 32  winMutexNotheld2
bcbe0 28 70 2c 20 74 69 64 29 20 29 3b 0d 0a 23 65 6e  (p, tid) );..#en
bcbf0 64 69 66 0d 0a 20 20 45 6e 74 65 72 43 72 69 74  dif..  EnterCrit
bcc00 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
bcc10 6d 75 74 65 78 29 3b 0d 0a 23 69 66 64 65 66 20  mutex);..#ifdef 
bcc20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20  SQLITE_DEBUG..  
bcc30 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
bcc40 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 3d 3d 30  0 || p->owner==0
bcc50 20 29 3b 0d 0a 20 20 70 2d 3e 6f 77 6e 65 72 20   );..  p->owner 
bcc60 3d 20 74 69 64 3b 20 0d 0a 20 20 70 2d 3e 6e 52  = tid; ..  p->nR
bcc70 65 66 2b 2b 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  ef++;..  if( p->
bcc80 74 72 61 63 65 20 29 7b 0d 0a 20 20 20 20 70 72  trace ){..    pr
bcc90 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65  intf("enter mute
bcca0 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e  x %p (%d) with n
bccb0 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d  Ref=%d\n", p, p-
bccc0 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29  >trace, p->nRef)
bccd0 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
bcce0 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  }..static int wi
bccf0 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  nMutexTry(sqlite
bcd00 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a 23 69  3_mutex *p){..#i
bcd10 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20  fndef NDEBUG..  
bcd20 44 57 4f 52 44 20 74 69 64 20 3d 20 47 65 74 43  DWORD tid = GetC
bcd30 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29  urrentThreadId()
bcd40 3b 20 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 6e  ; ..#endif..  in
bcd50 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  t rc = SQLITE_BU
bcd60 53 59 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  SY;..  assert( p
bcd70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
bcd80 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
bcd90 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 32  winMutexNotheld2
bcda0 28 70 2c 20 74 69 64 29 20 29 3b 0d 0a 20 20 2f  (p, tid) );..  /
bcdb0 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  *..  ** The sqli
bcdc0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
bcdd0 72 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20  routine is very 
bcde0 72 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64  rarely used, and
bcdf0 20 77 68 65 6e 20 69 74 0d 0a 20 20 2a 2a 20 69   when it..  ** i
bce00 73 20 75 73 65 64 20 69 74 20 69 73 20 6d 65 72  s used it is mer
bce10 65 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74  ely an optimizat
bce20 69 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f  ion.  So it is O
bce30 4b 20 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61  K for it to alwa
bce40 79 73 0d 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20  ys..  ** fail.  
bce50 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65  ..  **..  ** The
bce60 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61   TryEnterCritica
bce70 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 65 72  lSection() inter
bce80 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61  face is only ava
bce90 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e  ilable on WinNT.
bcea0 0d 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 20  ..  ** And some 
bceb0 77 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 72  windows compiler
bcec0 73 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 6f  s complain if yo
bced0 75 20 74 72 79 20 74 6f 20 75 73 65 20 69 74 20  u try to use it 
bcee0 77 69 74 68 6f 75 74 0d 0a 20 20 2a 2a 20 66 69  without..  ** fi
bcef0 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23  rst doing some #
bcf00 64 65 66 69 6e 65 73 20 74 68 61 74 20 70 72 65  defines that pre
bcf10 76 65 6e 74 20 53 51 4c 69 74 65 20 66 72 6f 6d  vent SQLite from
bcf20 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e   building on Win
bcf30 39 38 2e 0d 0a 20 20 2a 2a 20 46 6f 72 20 74 68  98...  ** For th
bcf40 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 77 69  at reason, we wi
bcf50 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f 70 74  ll omit this opt
bcf60 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f  imization for no
bcf70 77 2e 20 20 53 65 65 0d 0a 20 20 2a 2a 20 74 69  w.  See..  ** ti
bcf80 63 6b 65 74 20 23 32 36 38 35 2e 0d 0a 20 20 2a  cket #2685...  *
bcf90 2f 0d 0a 23 69 66 20 30 0d 0a 20 20 69 66 28 20  /..#if 0..  if( 
bcfa0 6d 75 74 65 78 49 73 4e 54 28 29 20 26 26 20 54  mutexIsNT() && T
bcfb0 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53  ryEnterCriticalS
bcfc0 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78  ection(&p->mutex
bcfd0 29 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6f 77 6e  ) ){..    p->own
bcfe0 65 72 20 3d 20 74 69 64 3b 0d 0a 20 20 20 20 70  er = tid;..    p
bcff0 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20 20 20 72  ->nRef++;..    r
bd000 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
bd010 20 20 7d 0d 0a 23 65 6c 73 65 0d 0a 20 20 55 4e    }..#else..  UN
bd020 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
bd030 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  );..#endif..#ifd
bd040 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
bd050 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
bd060 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65  E_OK && p->trace
bd070 20 29 7b 0d 0a 20 20 20 20 70 72 69 6e 74 66 28   ){..    printf(
bd080 22 74 72 79 20 6d 75 74 65 78 20 25 70 20 28 25  "try mutex %p (%
bd090 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
bd0a0 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
bd0b0 20 70 2d 3e 6e 52 65 66 29 3b 0d 0a 20 20 7d 0d   p->nRef);..  }.
bd0c0 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65 74 75 72  .#endif..  retur
bd0d0 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
bd0e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
bd0f0 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75  utex_leave() rou
bd100 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74  tine exits a mut
bd110 65 78 20 74 68 61 74 20 77 61 73 0d 0a 2a 2a 20  ex that was..** 
bd120 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
bd130 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
bd140 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
bd150 76 69 6f 72 0d 0a 2a 2a 20 69 73 20 75 6e 64 65  vior..** is unde
bd160 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74  fined if the mut
bd170 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ex is not curren
bd180 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0d 0a  tly entered or..
bd190 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
bd1a0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
bd1b0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
bd1c0 72 20 64 6f 20 65 69 74 68 65 72 2e 0d 0a 2a 2f  r do either...*/
bd1d0 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  ..static void wi
bd1e0 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  nMutexLeave(sqli
bd1f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0d 0a  te3_mutex *p){..
bd200 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
bd210 20 20 44 57 4f 52 44 20 74 69 64 20 3d 20 47 65    DWORD tid = Ge
bd220 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
bd230 28 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ();..  assert( p
bd240 2d 3e 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 61  ->nRef>0 );..  a
bd250 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
bd260 3d 74 69 64 20 29 3b 0d 0a 20 20 70 2d 3e 6e 52  =tid );..  p->nR
bd270 65 66 2d 2d 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  ef--;..  if( p->
bd280 6e 52 65 66 3d 3d 30 20 29 20 70 2d 3e 6f 77 6e  nRef==0 ) p->own
bd290 65 72 20 3d 20 30 3b 0d 0a 20 20 61 73 73 65 72  er = 0;..  asser
bd2a0 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  t( p->nRef==0 ||
bd2b0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
bd2c0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29  UTEX_RECURSIVE )
bd2d0 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 4c 65 61  ;..#endif..  Lea
bd2e0 76 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  veCriticalSectio
bd2f0 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0d 0a 23  n(&p->mutex);..#
bd300 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
bd310 55 47 0d 0a 20 20 69 66 28 20 70 2d 3e 74 72 61  UG..  if( p->tra
bd320 63 65 20 29 7b 0d 0a 20 20 20 20 70 72 69 6e 74  ce ){..    print
bd330 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25  f("leave mutex %
bd340 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66  p (%d) with nRef
bd350 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72  =%d\n", p, p->tr
bd360 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0d 0a  ace, p->nRef);..
bd370 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a    }..#endif..}..
bd380 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
bd390 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
bd3a0 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 73 71  ethods const *sq
bd3b0 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65  lite3DefaultMute
bd3c0 78 28 76 6f 69 64 29 7b 0d 0a 20 20 73 74 61 74  x(void){..  stat
bd3d0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
bd3e0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
bd3f0 4d 75 74 65 78 20 3d 20 7b 0d 0a 20 20 20 20 77  Mutex = {..    w
bd400 69 6e 4d 75 74 65 78 49 6e 69 74 2c 0d 0a 20 20  inMutexInit,..  
bd410 20 20 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0d 0a    winMutexEnd,..
bd420 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f      winMutexAllo
bd430 63 2c 0d 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  c,..    winMutex
bd440 46 72 65 65 2c 0d 0a 20 20 20 20 77 69 6e 4d 75  Free,..    winMu
bd450 74 65 78 45 6e 74 65 72 2c 0d 0a 20 20 20 20 77  texEnter,..    w
bd460 69 6e 4d 75 74 65 78 54 72 79 2c 0d 0a 20 20 20  inMutexTry,..   
bd470 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0d   winMutexLeave,.
bd480 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
bd490 45 42 55 47 0d 0a 20 20 20 20 77 69 6e 4d 75 74  EBUG..    winMut
bd4a0 65 78 48 65 6c 64 2c 0d 0a 20 20 20 20 77 69 6e  exHeld,..    win
bd4b0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0d 0a 23 65  MutexNotheld..#e
bd4c0 6c 73 65 0d 0a 20 20 20 20 30 2c 0d 0a 20 20 20  lse..    0,..   
bd4d0 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 3b   0..#endif..  };
bd4e0 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  ....  return &sM
bd4f0 75 74 65 78 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  utex;..}..#endif
bd500 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   /* SQLITE_MUTEX
bd510 5f 57 33 32 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  _W32 */..../****
bd520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
bd530 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a  f mutex_w32.c **
bd540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
bd570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
bd580 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20  n file malloc.c 
bd590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
bd5c0 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
bd5d0 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  er 15..**..** Th
bd5e0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
bd5f0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
bd600 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
bd610 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
bd620 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
bd630 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
bd640 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
bd650 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
bd660 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
bd670 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
bd680 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
bd690 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
bd6a0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
bd6b0 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bd6c0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
bd6d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
bd6e0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
bd6f0 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
bd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
bd740 2a 0d 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c  *..** Memory all
bd750 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
bd760 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75  s used throughou
bd770 74 20 73 71 6c 69 74 65 2e 0d 0a 2a 2f 0d 0a 2f  t sqlite...*/../
bd780 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61  * #include <stda
bd790 72 67 2e 68 3e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  rg.h> */..../*..
bd7a0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65  ** Attempt to re
bd7b0 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 62 79  lease up to n by
bd7c0 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e  tes of non-essen
bd7d0 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 72  tial memory curr
bd7e0 65 6e 74 6c 79 0d 0a 2a 2a 20 68 65 6c 64 20 62  ently..** held b
bd7f0 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 61  y SQLite. An exa
bd800 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65  mple of non-esse
bd810 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20  ntial memory is 
bd820 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0d 0a  memory used to..
bd830 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73  ** cache databas
bd840 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65  e pages that are
bd850 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69   not currently i
bd860 6e 20 75 73 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  n use...*/..SQLI
bd870 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bd880 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
bd890 79 28 69 6e 74 20 6e 29 7b 0d 0a 23 69 66 64 65  y(int n){..#ifde
bd8a0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
bd8b0 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
bd8c0 54 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  T..  return sqli
bd8d0 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65  te3PcacheRelease
bd8e0 4d 65 6d 6f 72 79 28 6e 29 3b 0d 0a 23 65 6c 73  Memory(n);..#els
bd8f0 65 0d 0a 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e  e..  /* IMPLEMEN
bd900 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 33 34 33  TATION-OF: R-343
bd910 39 31 2d 32 34 39 32 31 20 54 68 65 20 73 71 6c  91-24921 The sql
bd920 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
bd930 6f 72 79 28 29 20 72 6f 75 74 69 6e 65 0d 0a 20  ory() routine.. 
bd940 20 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20 72   ** is a no-op r
bd950 65 74 75 72 6e 69 6e 67 20 7a 65 72 6f 20 69 66  eturning zero if
bd960 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 63   SQLite is not c
bd970 6f 6d 70 69 6c 65 64 20 77 69 74 68 0d 0a 20 20  ompiled with..  
bd980 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
bd990 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
bd9a0 4e 54 2e 20 2a 2f 0d 0a 20 20 55 4e 55 53 45 44  NT. */..  UNUSED
bd9b0 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0d 0a  _PARAMETER(n);..
bd9c0 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 23 65 6e    return 0;..#en
bd9d0 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  dif..}..../*..**
bd9e0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
bd9f0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62  the following ob
bda00 6a 65 63 74 20 72 65 63 6f 72 64 73 20 74 68 65  ject records the
bda10 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 0d 0a 2a 2a   location of..**
bda20 20 65 61 63 68 20 75 6e 75 73 65 64 20 73 63 72   each unused scr
bda30 61 74 63 68 20 62 75 66 66 65 72 2e 0d 0a 2a 2f  atch buffer...*/
bda40 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
bda50 20 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74   ScratchFreeslot
bda60 20 7b 0d 0a 20 20 73 74 72 75 63 74 20 53 63 72   {..  struct Scr
bda70 61 74 63 68 46 72 65 65 73 6c 6f 74 20 2a 70 4e  atchFreeslot *pN
bda80 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 75  ext;   /* Next u
bda90 6e 75 73 65 64 20 73 63 72 61 74 63 68 20 62 75  nused scratch bu
bdaa0 66 66 65 72 20 2a 2f 0d 0a 7d 20 53 63 72 61 74  ffer */..} Scrat
bdab0 63 68 46 72 65 65 73 6c 6f 74 3b 0d 0a 0d 0a 2f  chFreeslot;..../
bdac0 2a 0d 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f  *..** State info
bdad0 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f  rmation local to
bdae0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
bdaf0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
bdb00 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 53 51  ...*/..static SQ
bdb10 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
bdb20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0d 0a 20 20  Mem0Global {..  
bdb30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
bdb40 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  utex;         /*
bdb50 20 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c   Mutex to serial
bdb60 69 7a 65 20 61 63 63 65 73 73 20 2a 2f 0d 0a 0d  ize access */...
bdb70 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 54 68 65 20  .  /*..  ** The 
bdb80 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61  alarm callback a
bdb90 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73  nd its arguments
bdba0 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65  .  The mem0.mute
bdbb0 78 20 6c 6f 63 6b 20 77 69 6c 6c 0d 0a 20 20 2a  x lock will..  *
bdbc0 2a 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20  * be held while 
bdbd0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
bdbe0 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73  running.  Recurs
bdbf0 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0d 0a  ive calls into..
bdc00 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20    ** the memory 
bdc10 73 75 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c  subsystem are al
bdc20 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65  lowed, but no ne
bdc30 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c  w callbacks will
bdc40 20 62 65 0d 0a 20 20 2a 2a 20 69 73 73 75 65 64   be..  ** issued
bdc50 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ...  */..  sqlit
bdc60 65 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68  e3_int64 alarmTh
bdc70 72 65 73 68 6f 6c 64 3b 0d 0a 20 20 76 6f 69 64  reshold;..  void
bdc80 20 28 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b   (*alarmCallback
bdc90 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33  )(void*, sqlite3
bdca0 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0d 0a 20 20  _int64,int);..  
bdcb0 76 6f 69 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0d  void *alarmArg;.
bdcc0 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 50 6f  ...  /*..  ** Po
bdcd0 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65 6e  inters to the en
bdce0 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c 6f 62  d of sqlite3Glob
bdcf0 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
bdd00 68 20 6d 65 6d 6f 72 79 0d 0a 20 20 2a 2a 20 28  h memory..  ** (
bdd10 73 6f 20 74 68 61 74 20 61 20 72 61 6e 67 65 20  so that a range 
bdd20 74 65 73 74 20 63 61 6e 20 62 65 20 75 73 65 64  test can be used
bdd30 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
bdd40 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d 0a   an allocation..
bdd50 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64    ** being freed
bdd60 20 63 61 6d 65 20 66 72 6f 6d 20 70 53 63 72 61   came from pScra
bdd70 74 63 68 29 20 61 6e 64 20 61 20 70 6f 69 6e 74  tch) and a point
bdd80 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  er to the list o
bdd90 66 0d 0a 20 20 2a 2a 20 75 6e 75 73 65 64 20 73  f..  ** unused s
bdda0 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f  cratch allocatio
bddb0 6e 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 76 6f 69  ns...  */..  voi
bddc0 64 20 2a 70 53 63 72 61 74 63 68 45 6e 64 3b 0d  d *pScratchEnd;.
bddd0 0a 20 20 53 63 72 61 74 63 68 46 72 65 65 73 6c  .  ScratchFreesl
bdde0 6f 74 20 2a 70 53 63 72 61 74 63 68 46 72 65 65  ot *pScratchFree
bddf0 3b 0d 0a 20 20 75 33 32 20 6e 53 63 72 61 74 63  ;..  u32 nScratc
bde00 68 46 72 65 65 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a  hFree;....  /*..
bde10 20 20 2a 2a 20 54 72 75 65 20 69 66 20 68 65 61    ** True if hea
bde20 70 20 69 73 20 6e 65 61 72 6c 79 20 22 66 75 6c  p is nearly "ful
bde30 6c 22 20 77 68 65 72 65 20 22 66 75 6c 6c 22 20  l" where "full" 
bde40 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  is defined by th
bde50 65 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  e..  ** sqlite3_
bde60 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
bde70 29 20 73 65 74 74 69 6e 67 2e 0d 0a 20 20 2a 2f  ) setting...  */
bde80 0d 0a 20 20 69 6e 74 20 6e 65 61 72 6c 79 46 75  ..  int nearlyFu
bde90 6c 6c 3b 0d 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20  ll;..} mem0 = { 
bdea0 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
bdeb0 2c 20 30 2c 20 30 20 7d 3b 0d 0a 0d 0a 23 64 65  , 0, 0 };....#de
bdec0 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c  fine mem0 GLOBAL
bded0 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62  (struct Mem0Glob
bdee0 61 6c 2c 20 6d 65 6d 30 29 0d 0a 0d 0a 2f 2a 0d  al, mem0)..../*.
bdef0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
bdf00 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d   runs when the m
bdf10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
bdf20 73 65 65 73 20 74 68 61 74 20 74 68 65 0d 0a 2a  sees that the..*
bdf30 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61  * total memory a
bdf40 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f  llocation is abo
bdf50 75 74 20 74 6f 20 65 78 63 65 65 64 20 74 68 65  ut to exceed the
bdf60 20 73 6f 66 74 20 68 65 61 70 0d 0a 2a 2a 20 6c   soft heap..** l
bdf70 69 6d 69 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  imit...*/..stati
bdf80 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c  c void softHeapL
bdf90 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0d 0a 20  imitEnforcer(.. 
bdfa0 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20   void *NotUsed, 
bdfb0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
bdfc0 34 20 4e 6f 74 55 73 65 64 32 2c 0d 0a 20 20 69  4 NotUsed2,..  i
bdfd0 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 0d 0a 29 7b  nt allocSize..){
bdfe0 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
bdff0 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
be000 6f 74 55 73 65 64 32 29 3b 0d 0a 20 20 73 71 6c  otUsed2);..  sql
be010 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
be020 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b 0d  ory(allocSize);.
be030 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61  .}..../*..** Cha
be040 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61  nge the alarm ca
be050 6c 6c 62 61 63 6b 0d 0a 2a 2f 0d 0a 73 74 61 74  llback..*/..stat
be060 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
be070 6d 6f 72 79 41 6c 61 72 6d 28 0d 0a 20 20 76 6f  moryAlarm(..  vo
be080 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  id(*xCallback)(v
be090 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74  oid *pArg, sqlit
be0a0 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e  e3_int64 used,in
be0b0 74 20 4e 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70  t N),..  void *p
be0c0 41 72 67 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  Arg,..  sqlite3_
be0d0 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64  int64 iThreshold
be0e0 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 6e 55 73 65  ..){..  int nUse
be0f0 64 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d;..  sqlite3_mu
be100 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
be110 75 74 65 78 29 3b 0d 0a 20 20 6d 65 6d 30 2e 61  utex);..  mem0.a
be120 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78  larmCallback = x
be130 43 61 6c 6c 62 61 63 6b 3b 0d 0a 20 20 6d 65 6d  Callback;..  mem
be140 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72  0.alarmArg = pAr
be150 67 3b 0d 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  g;..  mem0.alarm
be160 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72  Threshold = iThr
be170 65 73 68 6f 6c 64 3b 0d 0a 20 20 6e 55 73 65 64  eshold;..  nUsed
be180 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73   = sqlite3Status
be190 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41  Value(SQLITE_STA
be1a0 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29  TUS_MEMORY_USED)
be1b0 3b 0d 0a 20 20 6d 65 6d 30 2e 6e 65 61 72 6c 79  ;..  mem0.nearly
be1c0 46 75 6c 6c 20 3d 20 28 69 54 68 72 65 73 68 6f  Full = (iThresho
be1d0 6c 64 3e 30 20 26 26 20 69 54 68 72 65 73 68 6f  ld>0 && iThresho
be1e0 6c 64 3c 3d 6e 55 73 65 64 29 3b 0d 0a 20 20 73  ld<=nUsed);..  s
be1f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
be200 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0d  ve(mem0.mutex);.
be210 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
be220 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64  _OK;..}....#ifnd
be230 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
be240 45 50 52 45 43 41 54 45 44 0d 0a 2f 2a 0d 0a 2a  EPRECATED../*..*
be250 2a 20 44 65 70 72 65 63 61 74 65 64 20 65 78 74  * Deprecated ext
be260 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e  ernal interface.
be270 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20    Internal/core 
be280 53 51 4c 69 74 65 20 63 6f 64 65 0d 0a 2a 2a 20  SQLite code..** 
be290 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69  should call sqli
be2a0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 2e 0d  te3MemoryAlarm..
be2b0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
be2c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  int sqlite3_memo
be2d0 72 79 5f 61 6c 61 72 6d 28 0d 0a 20 20 76 6f 69  ry_alarm(..  voi
be2e0 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f  d(*xCallback)(vo
be2f0 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65  id *pArg, sqlite
be300 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74  3_int64 used,int
be310 20 4e 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41   N),..  void *pA
be320 72 67 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69  rg,..  sqlite3_i
be330 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0d  nt64 iThreshold.
be340 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71  .){..  return sq
be350 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
be360 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67  (xCallback, pArg
be370 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0d 0a  , iThreshold);..
be380 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  }..#endif..../*.
be390 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74  .** Set the soft
be3a0 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74   heap-size limit
be3b0 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79   for the library
be3c0 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f  . Passing a zero
be3d0 20 6f 72 20 0d 0a 2a 2a 20 6e 65 67 61 74 69 76   or ..** negativ
be3e0 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  e value indicate
be3f0 73 20 6e 6f 20 6c 69 6d 69 74 2e 0d 0a 2a 2f 0d  s no limit...*/.
be400 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
be410 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
be420 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
be430 74 36 34 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  t64(sqlite3_int6
be440 34 20 6e 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  4 n){..  sqlite3
be450 5f 69 6e 74 36 34 20 70 72 69 6f 72 4c 69 6d 69  _int64 priorLimi
be460 74 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  t;..  sqlite3_in
be470 74 36 34 20 65 78 63 65 73 73 3b 0d 0a 23 69 66  t64 excess;..#if
be480 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
be490 5f 41 55 54 4f 49 4e 49 54 0d 0a 20 20 69 6e 74  _AUTOINIT..  int
be4a0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
be4b0 69 74 69 61 6c 69 7a 65 28 29 3b 0d 0a 20 20 69  itialize();..  i
be4c0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 2d  f( rc ) return -
be4d0 31 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71  1;..#endif..  sq
be4e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
be4f0 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a  r(mem0.mutex);..
be500 20 20 70 72 69 6f 72 4c 69 6d 69 74 20 3d 20 6d    priorLimit = m
be510 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f  em0.alarmThresho
be520 6c 64 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ld;..  sqlite3_m
be530 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
be540 6d 75 74 65 78 29 3b 0d 0a 20 20 69 66 28 20 6e  mutex);..  if( n
be550 3c 30 20 29 20 72 65 74 75 72 6e 20 70 72 69 6f  <0 ) return prio
be560 72 4c 69 6d 69 74 3b 0d 0a 20 20 69 66 28 20 6e  rLimit;..  if( n
be570 3e 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  >0 ){..    sqlit
be580 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f  e3MemoryAlarm(so
be590 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72  ftHeapLimitEnfor
be5a0 63 65 72 2c 20 30 2c 20 6e 29 3b 0d 0a 20 20 7d  cer, 0, n);..  }
be5b0 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74  else{..    sqlit
be5c0 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c  e3MemoryAlarm(0,
be5d0 20 30 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20 20   0, 0);..  }..  
be5e0 65 78 63 65 73 73 20 3d 20 73 71 6c 69 74 65 33  excess = sqlite3
be5f0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d  _memory_used() -
be600 20 6e 3b 0d 0a 20 20 69 66 28 20 65 78 63 65 73   n;..  if( exces
be610 73 3e 30 20 29 20 73 71 6c 69 74 65 33 5f 72 65  s>0 ) sqlite3_re
be620 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 28 69 6e  lease_memory((in
be630 74 29 28 65 78 63 65 73 73 20 26 20 30 78 37 66  t)(excess & 0x7f
be640 66 66 66 66 66 66 29 29 3b 0d 0a 20 20 72 65 74  ffffff));..  ret
be650 75 72 6e 20 70 72 69 6f 72 4c 69 6d 69 74 3b 0d  urn priorLimit;.
be660 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .}..SQLITE_API v
be670 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  oid sqlite3_soft
be680 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 20  _heap_limit(int 
be690 6e 29 7b 0d 0a 20 20 69 66 28 20 6e 3c 30 20 29  n){..  if( n<0 )
be6a0 20 6e 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74   n = 0;..  sqlit
be6b0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
be6c0 69 74 36 34 28 6e 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  it64(n);..}..../
be6d0 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  *..** Initialize
be6e0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
be6f0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
be700 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
be710 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
be720 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64  3MallocInit(void
be730 29 7b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ){..  if( sqlite
be740 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
be750 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0d 0a 20  xMalloc==0 ){.. 
be760 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74     sqlite3MemSet
be770 44 65 66 61 75 6c 74 28 29 3b 0d 0a 20 20 7d 0d  Default();..  }.
be780 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c  .  memset(&mem0,
be790 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29   0, sizeof(mem0)
be7a0 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
be7b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
be7c0 6f 72 65 4d 75 74 65 78 20 29 7b 0d 0a 20 20 20  oreMutex ){..   
be7d0 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71   mem0.mutex = sq
be7e0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
be7f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
be800 54 49 43 5f 4d 45 4d 29 3b 0d 0a 20 20 7d 0d 0a  TIC_MEM);..  }..
be810 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
be820 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
be830 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  ch && sqlite3Glo
be840 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
be850 74 63 68 3e 3d 31 30 30 0d 0a 20 20 20 20 20 20  tch>=100..      
be860 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
be870 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e  Config.nScratch>
be880 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 2c  0 ){..    int i,
be890 20 6e 2c 20 73 7a 3b 0d 0a 20 20 20 20 53 63 72   n, sz;..    Scr
be8a0 61 74 63 68 46 72 65 65 73 6c 6f 74 20 2a 70 53  atchFreeslot *pS
be8b0 6c 6f 74 3b 0d 0a 20 20 20 20 73 7a 20 3d 20 52  lot;..    sz = R
be8c0 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 65  OUNDDOWN8(sqlite
be8d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
be8e0 53 63 72 61 74 63 68 29 3b 0d 0a 20 20 20 20 73  Scratch);..    s
be8f0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
be900 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 73  ig.szScratch = s
be910 7a 3b 0d 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20  z;..    pSlot = 
be920 28 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74  (ScratchFreeslot
be930 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
be940 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 3b 0d  onfig.pScratch;.
be950 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  .    n = sqlite3
be960 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
be970 72 61 74 63 68 3b 0d 0a 20 20 20 20 6d 65 6d 30  ratch;..    mem0
be980 2e 70 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .pScratchFree = 
be990 70 53 6c 6f 74 3b 0d 0a 20 20 20 20 6d 65 6d 30  pSlot;..    mem0
be9a0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .nScratchFree = 
be9b0 6e 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  n;..    for(i=0;
be9c0 20 69 3c 6e 2d 31 3b 20 69 2b 2b 29 7b 0d 0a 20   i<n-1; i++){.. 
be9d0 20 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78       pSlot->pNex
be9e0 74 20 3d 20 28 53 63 72 61 74 63 68 46 72 65 65  t = (ScratchFree
be9f0 73 6c 6f 74 2a 29 28 73 7a 2b 28 63 68 61 72 2a  slot*)(sz+(char*
bea00 29 70 53 6c 6f 74 29 3b 0d 0a 20 20 20 20 20 20  )pSlot);..      
bea10 70 53 6c 6f 74 20 3d 20 70 53 6c 6f 74 2d 3e 70  pSlot = pSlot->p
bea20 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Next;..    }..  
bea30 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d    pSlot->pNext =
bea40 20 30 3b 0d 0a 20 20 20 20 6d 65 6d 30 2e 70 53   0;..    mem0.pS
bea50 63 72 61 74 63 68 45 6e 64 20 3d 20 28 76 6f 69  cratchEnd = (voi
bea60 64 2a 29 26 70 53 6c 6f 74 5b 31 5d 3b 0d 0a 20  d*)&pSlot[1];.. 
bea70 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6d 65 6d   }else{..    mem
bea80 30 2e 70 53 63 72 61 74 63 68 45 6e 64 20 3d 20  0.pScratchEnd = 
bea90 30 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47  0;..    sqlite3G
beaa0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
beab0 61 74 63 68 20 3d 20 30 3b 0d 0a 20 20 20 20 73  atch = 0;..    s
beac0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bead0 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30  ig.szScratch = 0
beae0 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ;..    sqlite3Gl
beaf0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
beb00 74 63 68 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  tch = 0;..  }.. 
beb10 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
beb20 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 3d 3d  alConfig.pPage==
beb30 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
beb40 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3c  alConfig.szPage<
beb50 35 31 32 0d 0a 20 20 20 20 20 20 7c 7c 20 73 71  512..      || sq
beb60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
beb70 67 2e 6e 50 61 67 65 3c 31 20 29 7b 0d 0a 20 20  g.nPage<1 ){..  
beb80 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
beb90 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30 3b  onfig.pPage = 0;
beba0 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ..    sqlite3Glo
bebb0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
bebc0 20 3d 20 30 3b 0d 0a 20 20 20 20 73 71 6c 69 74   = 0;..    sqlit
bebd0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
bebe0 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  Page = 0;..  }..
bebf0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
bec00 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
bec10 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62  Init(sqlite3Glob
bec20 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44  alConfig.m.pAppD
bec30 61 74 61 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ata);..}..../*..
bec40 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
bec50 66 20 74 68 65 20 68 65 61 70 20 69 73 20 63 75  f the heap is cu
bec60 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 6d 65  rrently under me
bec70 6d 6f 72 79 20 70 72 65 73 73 75 72 65 20 2d 20  mory pressure - 
bec80 69 6e 20 6f 74 68 65 72 0d 0a 2a 2a 20 77 6f 72  in other..** wor
bec90 64 73 20 69 66 20 74 68 65 20 61 6d 6f 75 6e 74  ds if the amount
beca0 20 6f 66 20 68 65 61 70 20 75 73 65 64 20 69 73   of heap used is
becb0 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 6c 69   close to the li
becc0 6d 69 74 20 73 65 74 20 62 79 0d 0a 2a 2a 20 73  mit set by..** s
becd0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
bece0 5f 6c 69 6d 69 74 28 29 2e 0d 0a 2a 2f 0d 0a 53  _limit()...*/..S
becf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
bed00 74 20 73 71 6c 69 74 65 33 48 65 61 70 4e 65 61  t sqlite3HeapNea
bed10 72 6c 79 46 75 6c 6c 28 76 6f 69 64 29 7b 0d 0a  rlyFull(void){..
bed20 20 20 72 65 74 75 72 6e 20 6d 65 6d 30 2e 6e 65    return mem0.ne
bed30 61 72 6c 79 46 75 6c 6c 3b 0d 0a 7d 0d 0a 0d 0a  arlyFull;..}....
bed40 2f 2a 0d 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c  /*..** Deinitial
bed50 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
bed60 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
bed70 74 65 6d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  tem...*/..SQLITE
bed80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
bed90 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76  lite3MallocEnd(v
beda0 6f 69 64 29 7b 0d 0a 20 20 69 66 28 20 73 71 6c  oid){..  if( sql
bedb0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
bedc0 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0d  .m.xShutdown ){.
bedd0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
bede0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74  alConfig.m.xShut
bedf0 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62  down(sqlite3Glob
bee00 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44  alConfig.m.pAppD
bee10 61 74 61 29 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65  ata);..  }..  me
bee20 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73  mset(&mem0, 0, s
bee30 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0d 0a 7d  izeof(mem0));..}
bee40 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
bee50 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  n the amount of 
bee60 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79  memory currently
bee70 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0d 0a 2a   checked out...*
bee80 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  /..SQLITE_API sq
bee90 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
beea0 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28  te3_memory_used(
beeb0 76 6f 69 64 29 7b 0d 0a 20 20 69 6e 74 20 6e 2c  void){..  int n,
beec0 20 6d 78 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f   mx;..  sqlite3_
beed0 69 6e 74 36 34 20 72 65 73 3b 0d 0a 20 20 73 71  int64 res;..  sq
beee0 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c  lite3_status(SQL
beef0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
bef00 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c  Y_USED, &n, &mx,
bef10 20 30 29 3b 0d 0a 20 20 72 65 73 20 3d 20 28 73   0);..  res = (s
bef20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e 3b 20  qlite3_int64)n; 
bef30 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20   /* Work around 
bef40 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43  bug in Borland C
bef50 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 2a  . Ticket #3216 *
bef60 2f 0d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  /..  return res;
bef70 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
bef80 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  turn the maximum
bef90 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
befa0 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20  y that has ever 
befb0 62 65 65 6e 0d 0a 2a 2a 20 63 68 65 63 6b 65 64  been..** checked
befc0 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65   out since eithe
befd0 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  r the beginning 
befe0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0d  of this process.
beff0 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65  .** or since the
bf000 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73   most recent res
bf010 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  et...*/..SQLITE_
bf020 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
bf030 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
bf040 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72  _highwater(int r
bf050 65 73 65 74 46 6c 61 67 29 7b 0d 0a 20 20 69 6e  esetFlag){..  in
bf060 74 20 6e 2c 20 6d 78 3b 0d 0a 20 20 73 71 6c 69  t n, mx;..  sqli
bf070 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0d 0a  te3_int64 res;..
bf080 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
bf090 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
bf0a0 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20  EMORY_USED, &n, 
bf0b0 26 6d 78 2c 20 72 65 73 65 74 46 6c 61 67 29 3b  &mx, resetFlag);
bf0c0 0d 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74  ..  res = (sqlit
bf0d0 65 33 5f 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a  e3_int64)mx;  /*
bf0e0 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67   Work around bug
bf0f0 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54   in Borland C. T
bf100 69 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0d 0a  icket #3216 */..
bf110 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0d 0a 7d    return res;..}
bf120 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72 69 67 67  ..../*..** Trigg
bf130 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0d 0a 2a  er the alarm ..*
bf140 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  /..static void s
bf150 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
bf160 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0d 0a 20  m(int nByte){.. 
bf170 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
bf180 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  k)(void*,sqlite3
bf190 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0d 0a 20 20  _int64,int);..  
bf1a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f  sqlite3_int64 no
bf1b0 77 55 73 65 64 3b 0d 0a 20 20 76 6f 69 64 20 2a  wUsed;..  void *
bf1c0 70 41 72 67 3b 0d 0a 20 20 69 66 28 20 6d 65 6d  pArg;..  if( mem
bf1d0 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d  0.alarmCallback=
bf1e0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  =0 ) return;..  
bf1f0 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30  xCallback = mem0
bf200 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0d  .alarmCallback;.
bf210 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c  .  nowUsed = sql
bf220 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
bf230 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
bf240 4d 4f 52 59 5f 55 53 45 44 29 3b 0d 0a 20 20 70  MORY_USED);..  p
bf250 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d  Arg = mem0.alarm
bf260 41 72 67 3b 0d 0a 20 20 6d 65 6d 30 2e 61 6c 61  Arg;..  mem0.ala
bf270 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0d  rmCallback = 0;.
bf280 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
bf290 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
bf2a0 78 29 3b 0d 0a 20 20 78 43 61 6c 6c 62 61 63 6b  x);..  xCallback
bf2b0 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 64 2c 20  (pArg, nowUsed, 
bf2c0 6e 42 79 74 65 29 3b 0d 0a 20 20 73 71 6c 69 74  nByte);..  sqlit
bf2d0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
bf2e0 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 6d  em0.mutex);..  m
bf2f0 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
bf300 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0d 0a  k = xCallback;..
bf310 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20    mem0.alarmArg 
bf320 3d 20 70 41 72 67 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  = pArg;..}..../*
bf330 0d 0a 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79  ..** Do a memory
bf340 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68   allocation with
bf350 20 73 74 61 74 69 73 74 69 63 73 20 61 6e 64 20   statistics and 
bf360 61 6c 61 72 6d 73 2e 20 20 41 73 73 75 6d 65 20  alarms.  Assume 
bf370 74 68 65 0d 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20  the..** lock is 
bf380 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0d 0a 2a  already held...*
bf390 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61  /..static int ma
bf3a0 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 69 6e  llocWithAlarm(in
bf3b0 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 70 29 7b  t n, void **pp){
bf3c0 0d 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b 0d 0a  ..  int nFull;..
bf3d0 20 20 76 6f 69 64 20 2a 70 3b 0d 0a 20 20 61 73    void *p;..  as
bf3e0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
bf3f0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75  tex_held(mem0.mu
bf400 74 65 78 29 20 29 3b 0d 0a 20 20 6e 46 75 6c 6c  tex) );..  nFull
bf410 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
bf420 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75  Config.m.xRoundu
bf430 70 28 6e 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  p(n);..  sqlite3
bf440 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
bf450 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53  _STATUS_MALLOC_S
bf460 49 5a 45 2c 20 6e 29 3b 0d 0a 20 20 69 66 28 20  IZE, n);..  if( 
bf470 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
bf480 63 6b 21 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e  ck!=0 ){..    in
bf490 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65  t nUsed = sqlite
bf4a0 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c  3StatusValue(SQL
bf4b0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
bf4c0 59 5f 55 53 45 44 29 3b 0d 0a 20 20 20 20 69 66  Y_USED);..    if
bf4d0 28 20 6e 55 73 65 64 20 3e 3d 20 6d 65 6d 30 2e  ( nUsed >= mem0.
bf4e0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 2d  alarmThreshold -
bf4f0 20 6e 46 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 20   nFull ){..     
bf500 20 6d 65 6d 30 2e 6e 65 61 72 6c 79 46 75 6c 6c   mem0.nearlyFull
bf510 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 73 71 6c   = 1;..      sql
bf520 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28  ite3MallocAlarm(
bf530 6e 46 75 6c 6c 29 3b 0d 0a 20 20 20 20 7d 65 6c  nFull);..    }el
bf540 73 65 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 30 2e  se{..      mem0.
bf550 6e 65 61 72 6c 79 46 75 6c 6c 20 3d 20 30 3b 0d  nearlyFull = 0;.
bf560 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70  .    }..  }..  p
bf570 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
bf580 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
bf590 28 6e 46 75 6c 6c 29 3b 0d 0a 23 69 66 64 65 66  (nFull);..#ifdef
bf5a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
bf5b0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
bf5c0 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20  ..  if( p==0 && 
bf5d0 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
bf5e0 63 6b 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ck ){..    sqlit
bf5f0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46  e3MallocAlarm(nF
bf600 75 6c 6c 29 3b 0d 0a 20 20 20 20 70 20 3d 20 73  ull);..    p = s
bf610 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bf620 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75  ig.m.xMalloc(nFu
bf630 6c 6c 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  ll);..  }..#endi
bf640 66 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20  f..  if( p ){.. 
bf650 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74     nFull = sqlit
bf660 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
bf670 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ..    sqlite3Sta
bf680 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
bf690 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
bf6a0 2c 20 6e 46 75 6c 6c 29 3b 0d 0a 20 20 20 20 73  , nFull);..    s
bf6b0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
bf6c0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41  SQLITE_STATUS_MA
bf6d0 4c 4c 4f 43 5f 43 4f 55 4e 54 2c 20 31 29 3b 0d  LLOC_COUNT, 1);.
bf6e0 0a 20 20 7d 0d 0a 20 20 2a 70 70 20 3d 20 70 3b  .  }..  *pp = p;
bf6f0 0d 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c 6c  ..  return nFull
bf700 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ;..}..../*..** A
bf710 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20  llocate memory. 
bf720 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
bf730 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61   like sqlite3_ma
bf740 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 68  lloc() except th
bf750 61 74 20 69 74 0d 0a 2a 2a 20 61 73 73 75 6d 65  at it..** assume
bf760 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62  s the memory sub
bf770 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61  system has alrea
bf780 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  dy been initiali
bf790 7a 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  zed...*/..SQLITE
bf7a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
bf7b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74  qlite3Malloc(int
bf7c0 20 6e 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 70 3b   n){..  void *p;
bf7d0 0d 0a 20 20 69 66 28 20 6e 3c 3d 30 20 20 20 20  ..  if( n<=0    
bf7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4d             /* IM
bf7f0 50 3a 20 52 2d 36 35 33 31 32 2d 30 34 39 31 37  P: R-65312-04917
bf800 20 2a 2f 20 0d 0a 20 20 20 7c 7c 20 6e 3e 3d 30   */ ..   || n>=0
bf810 78 37 66 66 66 66 66 30 30 0d 0a 20 20 29 7b 0d  x7fffff00..  ){.
bf820 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79  .    /* A memory
bf830 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61   allocation of a
bf840 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
bf850 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74   which is near t
bf860 68 65 20 6d 61 78 69 6d 75 6d 0d 0a 20 20 20 20  he maximum..    
bf870 2a 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  ** signed intege
bf880 72 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61  r value might ca
bf890 75 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f  use an integer o
bf8a0 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f  verflow inside o
bf8b0 66 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 78 4d  f the..    ** xM
bf8c0 61 6c 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20  alloc().  Hence 
bf8d0 77 65 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78  we limit the max
bf8e0 69 6d 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37  imum size to 0x7
bf8f0 66 66 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0d  fffff00, giving.
bf900 0a 20 20 20 20 2a 2a 20 32 35 35 20 62 79 74 65  .    ** 255 byte
bf910 73 20 6f 66 20 6f 76 65 72 68 65 61 64 2e 20 20  s of overhead.  
bf920 53 51 4c 69 74 65 20 69 74 73 65 6c 66 20 77 69  SQLite itself wi
bf930 6c 6c 20 6e 65 76 65 72 20 75 73 65 20 61 6e 79  ll never use any
bf940 74 68 69 6e 67 20 6e 65 61 72 0d 0a 20 20 20 20  thing near..    
bf950 2a 2a 20 74 68 69 73 20 61 6d 6f 75 6e 74 2e 20  ** this amount. 
bf960 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f   The only way to
bf970 20 72 65 61 63 68 20 74 68 65 20 6c 69 6d 69 74   reach the limit
bf980 20 69 73 20 77 69 74 68 20 73 71 6c 69 74 65 33   is with sqlite3
bf990 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0d 0a 20 20  _malloc() */..  
bf9a0 20 20 70 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73    p = 0;..  }els
bf9b0 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f  e if( sqlite3Glo
bf9c0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
bf9d0 61 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  at ){..    sqlit
bf9e0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
bf9f0 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 20  em0.mutex);..   
bfa00 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
bfa10 28 6e 2c 20 26 70 29 3b 0d 0a 20 20 20 20 73 71  (n, &p);..    sq
bfa20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
bfa30 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a  e(mem0.mutex);..
bfa40 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 20    }else{..    p 
bfa50 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
bfa60 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28  onfig.m.xMalloc(
bfa70 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65  n);..  }..  asse
bfa80 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
bfa90 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 20 20  LIGNMENT(p) );  
bfaa0 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 36 37 35 2d  /* IMP: R-04675-
bfab0 34 34 38 35 30 20 2a 2f 0d 0a 20 20 72 65 74 75  44850 */..  retu
bfac0 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn p;..}..../*..
bfad0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
bfae0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
bfaf0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20  location is for 
bfb00 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c 69  use by the appli
bfb10 63 61 74 69 6f 6e 2e 0d 0a 2a 2a 20 46 69 72 73  cation...** Firs
bfb20 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  t make sure the 
bfb30 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
bfb40 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c   is initialized,
bfb50 20 74 68 65 6e 20 64 6f 20 74 68 65 0d 0a 2a 2a   then do the..**
bfb60 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2f   allocation...*/
bfb70 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ..SQLITE_API voi
bfb80 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  d *sqlite3_mallo
bfb90 63 28 69 6e 74 20 6e 29 7b 0d 0a 23 69 66 6e 64  c(int n){..#ifnd
bfba0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
bfbb0 55 54 4f 49 4e 49 54 0d 0a 20 20 69 66 28 20 73  UTOINIT..  if( s
bfbc0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
bfbd0 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  e() ) return 0;.
bfbe0 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65 74 75 72  .#endif..  retur
bfbf0 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  n sqlite3Malloc(
bfc00 6e 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  n);..}..../*..**
bfc10 20 45 61 63 68 20 74 68 72 65 61 64 20 6d 61 79   Each thread may
bfc20 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e   only have a sin
bfc30 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  gle outstanding 
bfc40 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 0d  allocation from.
bfc50 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c 6c  .** xScratchMall
bfc60 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66 79  oc().  We verify
bfc70 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
bfc80 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 74   in the single-t
bfc90 68 72 65 61 64 65 64 0d 0a 2a 2a 20 63 61 73 65  hreaded..** case
bfca0 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61   by setting scra
bfcb0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31  tchAllocOut to 1
bfcc0 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74   when an allocat
bfcd0 69 6f 6e 0d 0a 2a 2a 20 69 73 20 6f 75 74 73 74  ion..** is outst
bfce0 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20  anding clearing 
bfcf0 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f  it when the allo
bfd00 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
bfd10 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45  ..*/..#if SQLITE
bfd20 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
bfd30 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
bfd40 47 29 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  G)..static int s
bfd50 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
bfd60 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d   0;..#endif.....
bfd70 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ./*..** Allocate
bfd80 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 69 73 20   memory that is 
bfd90 74 6f 20 62 65 20 75 73 65 64 20 61 6e 64 20 72  to be used and r
bfda0 65 6c 65 61 73 65 64 20 72 69 67 68 74 20 61 77  eleased right aw
bfdb0 61 79 2e 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ay...** This rou
bfdc0 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20  tine is similar 
bfdd0 74 6f 20 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74  to alloca() in t
bfde0 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 69 6e  hat it is not in
bfdf0 74 65 6e 64 65 64 0d 0a 2a 2a 20 66 6f 72 20 73  tended..** for s
bfe00 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20  ituations where 
bfe10 74 68 65 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74  the memory might
bfe20 20 62 65 20 68 65 6c 64 20 6c 6f 6e 67 2d 74 65   be held long-te
bfe30 72 6d 2e 20 20 54 68 69 73 0d 0a 2a 2a 20 72 6f  rm.  This..** ro
bfe40 75 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65  utine is intende
bfe50 64 20 74 6f 20 67 65 74 20 6d 65 6d 6f 72 79 20  d to get memory 
bfe60 74 6f 20 6f 6c 64 20 6c 61 72 67 65 20 74 72 61  to old large tra
bfe70 6e 73 69 65 6e 74 20 64 61 74 61 0d 0a 2a 2a 20  nsient data..** 
bfe80 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
bfe90 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c  would not normal
bfea0 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 20 73 74  ly fit on the st
bfeb0 61 63 6b 20 6f 66 20 61 6e 0d 0a 2a 2a 20 65 6d  ack of an..** em
bfec0 62 65 64 64 65 64 20 70 72 6f 63 65 73 73 6f 72  bedded processor
bfed0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
bfee0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
bfef0 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  te3ScratchMalloc
bff00 28 69 6e 74 20 6e 29 7b 0d 0a 20 20 76 6f 69 64  (int n){..  void
bff10 20 2a 70 3b 0d 0a 20 20 61 73 73 65 72 74 28 20   *p;..  assert( 
bff20 6e 3e 30 20 29 3b 0d 0a 0d 0a 20 20 73 71 6c 69  n>0 );....  sqli
bff30 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
bff40 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a 20 20  mem0.mutex);..  
bff50 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63  if( mem0.nScratc
bff60 68 46 72 65 65 20 26 26 20 73 71 6c 69 74 65 33  hFree && sqlite3
bff70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
bff80 63 72 61 74 63 68 3e 3d 6e 20 29 7b 0d 0a 20 20  cratch>=n ){..  
bff90 20 20 70 20 3d 20 6d 65 6d 30 2e 70 53 63 72 61    p = mem0.pScra
bffa0 74 63 68 46 72 65 65 3b 0d 0a 20 20 20 20 6d 65  tchFree;..    me
bffb0 6d 30 2e 70 53 63 72 61 74 63 68 46 72 65 65 20  m0.pScratchFree 
bffc0 3d 20 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46  = mem0.pScratchF
bffd0 72 65 65 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20  ree->pNext;..   
bffe0 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72   mem0.nScratchFr
bfff0 65 65 2d 2d 3b 0d 0a 20 20 20 20 73 71 6c 69 74  ee--;..    sqlit
c0000 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
c0010 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
c0020 48 5f 55 53 45 44 2c 20 31 29 3b 0d 0a 20 20 20  H_USED, 1);..   
c0030 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
c0040 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
c0050 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29  SCRATCH_SIZE, n)
c0060 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ;..    sqlite3_m
c0070 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
c0080 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 65 6c 73 65  mutex);..  }else
c0090 7b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  {..    if( sqlit
c00a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
c00b0 4d 65 6d 73 74 61 74 20 29 7b 0d 0a 20 20 20 20  Memstat ){..    
c00c0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
c00d0 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
c00e0 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e  _SCRATCH_SIZE, n
c00f0 29 3b 0d 0a 20 20 20 20 20 20 6e 20 3d 20 6d 61  );..      n = ma
c0100 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c  llocWithAlarm(n,
c0110 20 26 70 29 3b 0d 0a 20 20 20 20 20 20 69 66 28   &p);..      if(
c0120 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74   p ) sqlite3Stat
c0130 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
c0140 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52  TUS_SCRATCH_OVER
c0150 46 4c 4f 57 2c 20 6e 29 3b 0d 0a 20 20 20 20 20  FLOW, n);..     
c0160 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
c0170 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
c0180 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
c0190 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
c01a0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
c01b0 74 65 78 29 3b 0d 0a 20 20 20 20 20 20 70 20 3d  tex);..      p =
c01c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
c01d0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e  nfig.m.xMalloc(n
c01e0 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73  );..    }..    s
c01f0 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65  qlite3MemdebugSe
c0200 74 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  tType(p, MEMTYPE
c0210 5f 53 43 52 41 54 43 48 29 3b 0d 0a 20 20 7d 0d  _SCRATCH);..  }.
c0220 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
c0230 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
c0240 28 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0d  (mem0.mutex) );.
c0250 0a 0d 0a 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .....#if SQLITE_
c0260 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26  THREADSAFE==0 &&
c0270 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
c0280 29 0d 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74  )..  /* Verify t
c0290 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  hat no more than
c02a0 20 74 77 6f 20 73 63 72 61 74 63 68 20 61 6c 6c   two scratch all
c02b0 6f 63 61 74 69 6f 6e 73 20 70 65 72 20 74 68 72  ocations per thr
c02c0 65 61 64 0d 0a 20 20 2a 2a 20 61 72 65 20 6f 75  ead..  ** are ou
c02d0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
c02e0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
c02f0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
c0300 20 74 68 65 0d 0a 20 20 2a 2a 20 73 69 6e 67 6c   the..  ** singl
c0310 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20  e-threaded case 
c0320 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69  since checking i
c0330 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65  n the multi-thre
c0340 61 64 65 64 20 63 61 73 65 0d 0a 20 20 2a 2a 20  aded case..  ** 
c0350 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f  would be much mo
c0360 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29  re complicated.)
c0370 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
c0380 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3c 3d  cratchAllocOut<=
c0390 31 20 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 20  1 );..  if( p ) 
c03a0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 2b  scratchAllocOut+
c03b0 2b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20  +;..#endif....  
c03c0 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 53 51  return p;..}..SQ
c03d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
c03e0 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68  d sqlite3Scratch
c03f0 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0d 0a  Free(void *p){..
c0400 20 20 69 66 28 20 70 20 29 7b 0d 0a 0d 0a 23 69    if( p ){....#i
c0410 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
c0420 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e  AFE==0 && !defin
c0430 65 64 28 4e 44 45 42 55 47 29 0d 0a 20 20 20 20  ed(NDEBUG)..    
c0440 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 6e  /* Verify that n
c0450 6f 20 6d 6f 72 65 20 74 68 61 6e 20 74 77 6f 20  o more than two 
c0460 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69  scratch allocati
c0470 6f 6e 20 70 65 72 20 74 68 72 65 61 64 0d 0a 20  on per thread.. 
c0480 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e     ** is outstan
c0490 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65  ding at one time
c04a0 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c 79  .  (This is only
c04b0 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 0d   checked in the.
c04c0 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74  .    ** single-t
c04d0 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e  hreaded case sin
c04e0 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74  ce checking in t
c04f0 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  he multi-threade
c0500 64 20 63 61 73 65 0d 0a 20 20 20 20 2a 2a 20 77  d case..    ** w
c0510 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72  ould be much mor
c0520 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20  e complicated.) 
c0530 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
c0540 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3e  scratchAllocOut>
c0550 3d 31 20 26 26 20 73 63 72 61 74 63 68 41 6c 6c  =1 && scratchAll
c0560 6f 63 4f 75 74 3c 3d 32 20 29 3b 0d 0a 20 20 20  ocOut<=2 );..   
c0570 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
c0580 2d 2d 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  --;..#endif.... 
c0590 20 20 20 69 66 28 20 70 3e 3d 73 71 6c 69 74 65     if( p>=sqlite
c05a0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
c05b0 63 72 61 74 63 68 20 26 26 20 70 3c 6d 65 6d 30  cratch && p<mem0
c05c0 2e 70 53 63 72 61 74 63 68 45 6e 64 20 29 7b 0d  .pScratchEnd ){.
c05d0 0a 20 20 20 20 20 20 2f 2a 20 52 65 6c 65 61 73  .      /* Releas
c05e0 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68  e memory from th
c05f0 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
c0600 53 43 52 41 54 43 48 20 61 6c 6c 6f 63 61 74 69  SCRATCH allocati
c0610 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 53 63 72  on */..      Scr
c0620 61 74 63 68 46 72 65 65 73 6c 6f 74 20 2a 70 53  atchFreeslot *pS
c0630 6c 6f 74 3b 0d 0a 20 20 20 20 20 20 70 53 6c 6f  lot;..      pSlo
c0640 74 20 3d 20 28 53 63 72 61 74 63 68 46 72 65 65  t = (ScratchFree
c0650 73 6c 6f 74 2a 29 70 3b 0d 0a 20 20 20 20 20 20  slot*)p;..      
c0660 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
c0670 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
c0680 0d 0a 20 20 20 20 20 20 70 53 6c 6f 74 2d 3e 70  ..      pSlot->p
c0690 4e 65 78 74 20 3d 20 6d 65 6d 30 2e 70 53 63 72  Next = mem0.pScr
c06a0 61 74 63 68 46 72 65 65 3b 0d 0a 20 20 20 20 20  atchFree;..     
c06b0 20 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46 72   mem0.pScratchFr
c06c0 65 65 20 3d 20 70 53 6c 6f 74 3b 0d 0a 20 20 20  ee = pSlot;..   
c06d0 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68     mem0.nScratch
c06e0 46 72 65 65 2b 2b 3b 0d 0a 20 20 20 20 20 20 61  Free++;..      a
c06f0 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72  ssert( mem0.nScr
c0700 61 74 63 68 46 72 65 65 20 3c 3d 20 28 75 33 32  atchFree <= (u32
c0710 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  )sqlite3GlobalCo
c0720 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b  nfig.nScratch );
c0730 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ..      sqlite3S
c0740 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
c0750 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55  STATUS_SCRATCH_U
c0760 53 45 44 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20  SED, -1);..     
c0770 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
c0780 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
c0790 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
c07a0 20 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20       /* Release 
c07b0 6d 65 6d 6f 72 79 20 62 61 63 6b 20 74 6f 20 74  memory back to t
c07c0 68 65 20 68 65 61 70 20 2a 2f 0d 0a 20 20 20 20  he heap */..    
c07d0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
c07e0 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65  3MemdebugHasType
c07f0 28 70 2c 20 4d 45 4d 54 59 50 45 5f 53 43 52 41  (p, MEMTYPE_SCRA
c0800 54 43 48 29 20 29 3b 0d 0a 20 20 20 20 20 20 61  TCH) );..      a
c0810 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65  ssert( sqlite3Me
c0820 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20  mdebugNoType(p, 
c0830 7e 4d 45 4d 54 59 50 45 5f 53 43 52 41 54 43 48  ~MEMTYPE_SCRATCH
c0840 29 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ) );..      sqli
c0850 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54 79  te3MemdebugSetTy
c0860 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 48 45  pe(p, MEMTYPE_HE
c0870 41 50 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  AP);..      if( 
c0880 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
c0890 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0d  fig.bMemstat ){.
c08a0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 69  .        int iSi
c08b0 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ze = sqlite3Mall
c08c0 6f 63 53 69 7a 65 28 70 29 3b 0d 0a 20 20 20 20  ocSize(p);..    
c08d0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
c08e0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
c08f0 65 78 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  ex);..        sq
c0900 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
c0910 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
c0920 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d  ATCH_OVERFLOW, -
c0930 69 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 20  iSize);..       
c0940 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
c0950 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
c0960 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53  MEMORY_USED, -iS
c0970 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  ize);..        s
c0980 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
c0990 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41  SQLITE_STATUS_MA
c09a0 4c 4c 4f 43 5f 43 4f 55 4e 54 2c 20 2d 31 29 3b  LLOC_COUNT, -1);
c09b0 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
c09c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
c09d0 78 46 72 65 65 28 70 29 3b 0d 0a 20 20 20 20 20  xFree(p);..     
c09e0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
c09f0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
c0a00 78 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  x);..      }else
c0a10 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
c0a20 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
c0a30 2e 78 46 72 65 65 28 70 29 3b 0d 0a 20 20 20 20  .xFree(p);..    
c0a40 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
c0a50 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 52 55  .}..../*..** TRU
c0a60 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b  E if p is a look
c0a70 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
c0a80 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0d  ocation from db.
c0a90 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
c0aa0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49  ITE_OMIT_LOOKASI
c0ab0 44 45 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69  DE..static int i
c0ac0 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74  sLookaside(sqlit
c0ad0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
c0ae0 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 20 26 26  {..  return p &&
c0af0 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   p>=db->lookasid
c0b00 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62  e.pStart && p<db
c0b10 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
c0b20 3b 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 64 65  ;..}..#else..#de
c0b30 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65  fine isLookaside
c0b40 28 41 2c 42 29 20 30 0d 0a 23 65 6e 64 69 66 0d  (A,B) 0..#endif.
c0b50 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
c0b60 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6d   the size of a m
c0b70 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
c0b80 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61   previously obta
c0b90 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 73 71  ined from..** sq
c0ba0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72  lite3Malloc() or
c0bb0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
c0bc0 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  )...*/..SQLITE_P
c0bd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
c0be0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69  e3MallocSize(voi
c0bf0 64 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74  d *p){..  assert
c0c00 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  ( sqlite3Memdebu
c0c10 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54  gHasType(p, MEMT
c0c20 59 50 45 5f 48 45 41 50 29 20 29 3b 0d 0a 20 20  YPE_HEAP) );..  
c0c30 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d  assert( sqlite3M
c0c40 65 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c  emdebugNoType(p,
c0c50 20 4d 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0d   MEMTYPE_DB) );.
c0c60 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
c0c70 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
c0c80 78 53 69 7a 65 28 70 29 3b 0d 0a 7d 0d 0a 53 51  xSize(p);..}..SQ
c0c90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
c0ca0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
c0cb0 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Size(sqlite3 *db
c0cc0 2c 20 76 6f 69 64 20 2a 70 29 7b 0d 0a 20 20 61  , void *p){..  a
c0cd0 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20  ssert( db==0 || 
c0ce0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
c0cf0 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
c0d00 0d 0a 20 20 69 66 28 20 64 62 20 26 26 20 69 73  ..  if( db && is
c0d10 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29  Lookaside(db, p)
c0d20 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
c0d30 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
c0d40 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
c0d50 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
c0d60 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28  MemdebugHasType(
c0d70 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42 29 20 29  p, MEMTYPE_DB) )
c0d80 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ;..    assert( s
c0d90 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61  qlite3MemdebugHa
c0da0 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  sType(p, MEMTYPE
c0db0 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d 45 4d 54 59  _LOOKASIDE|MEMTY
c0dc0 50 45 5f 48 45 41 50 29 20 29 3b 0d 0a 20 20 20  PE_HEAP) );..   
c0dd0 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 7c   assert( db!=0 |
c0de0 7c 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  | sqlite3Memdebu
c0df0 67 4e 6f 54 79 70 65 28 70 2c 20 4d 45 4d 54 59  gNoType(p, MEMTY
c0e00 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 29 20 29 3b  PE_LOOKASIDE) );
c0e10 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  ..    return sql
c0e20 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
c0e30 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0d 0a 20 20  .m.xSize(p);..  
c0e40 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46  }..}..../*..** F
c0e50 72 65 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 69  ree memory previ
c0e60 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66  ously obtained f
c0e70 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  rom sqlite3Mallo
c0e80 63 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  c()...*/..SQLITE
c0e90 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
c0ea0 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  3_free(void *p){
c0eb0 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
c0ec0 65 74 75 72 6e 3b 20 20 2f 2a 20 49 4d 50 3a 20  eturn;  /* IMP: 
c0ed0 52 2d 34 39 30 35 33 2d 35 34 35 35 34 20 2a 2f  R-49053-54554 */
c0ee0 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
c0ef0 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70  te3MemdebugNoTyp
c0f00 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42 29  e(p, MEMTYPE_DB)
c0f10 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
c0f20 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61  qlite3MemdebugHa
c0f30 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  sType(p, MEMTYPE
c0f40 5f 48 45 41 50 29 20 29 3b 0d 0a 20 20 69 66 28  _HEAP) );..  if(
c0f50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
c0f60 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b  nfig.bMemstat ){
c0f70 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ..    sqlite3_mu
c0f80 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
c0f90 75 74 65 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69  utex);..    sqli
c0fa0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
c0fb0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
c0fc0 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33  Y_USED, -sqlite3
c0fd0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0d  MallocSize(p));.
c0fe0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74  .    sqlite3Stat
c0ff0 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
c1000 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55 4e 54  TUS_MALLOC_COUNT
c1010 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69  , -1);..    sqli
c1020 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
c1030 6d 2e 78 46 72 65 65 28 70 29 3b 0d 0a 20 20 20  m.xFree(p);..   
c1040 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
c1050 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
c1060 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
c1070 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
c1080 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b  nfig.m.xFree(p);
c1090 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
c10a0 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74  ** Free memory t
c10b0 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73  hat might be ass
c10c0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70  ociated with a p
c10d0 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
c10e0 73 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  se..** connectio
c10f0 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  n...*/..SQLITE_P
c1100 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
c1110 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65  te3DbFree(sqlite
c1120 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b  3 *db, void *p){
c1130 0d 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d  ..  assert( db==
c1140 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
c1150 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
c1160 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 64 62 20  x) );..  if( db 
c1170 29 7b 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  ){..    if( db->
c1180 70 6e 42 79 74 65 73 46 72 65 65 64 20 29 7b 0d  pnBytesFreed ){.
c1190 0a 20 20 20 20 20 20 2a 64 62 2d 3e 70 6e 42 79  .      *db->pnBy
c11a0 74 65 73 46 72 65 65 64 20 2b 3d 20 73 71 6c 69  tesFreed += sqli
c11b0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
c11c0 64 62 2c 20 70 29 3b 0d 0a 20 20 20 20 20 20 72  db, p);..      r
c11d0 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20  eturn;..    }.. 
c11e0 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69     if( isLookasi
c11f0 64 65 28 64 62 2c 20 70 29 20 29 7b 0d 0a 20 20  de(db, p) ){..  
c1200 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f      LookasideSlo
c1210 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61  t *pBuf = (Looka
c1220 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0d 0a 20 20  sideSlot*)p;..  
c1230 20 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20      pBuf->pNext 
c1240 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
c1250 70 46 72 65 65 3b 0d 0a 20 20 20 20 20 20 64 62  pFree;..      db
c1260 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65  ->lookaside.pFre
c1270 65 20 3d 20 70 42 75 66 3b 0d 0a 20 20 20 20 20  e = pBuf;..     
c1280 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
c1290 4f 75 74 2d 2d 3b 0d 0a 20 20 20 20 20 20 72 65  Out--;..      re
c12a0 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  turn;..    }..  
c12b0 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  }..  assert( sql
c12c0 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
c12d0 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44  ype(p, MEMTYPE_D
c12e0 42 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  B) );..  assert(
c12f0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
c1300 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59  HasType(p, MEMTY
c1310 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d 45 4d  PE_LOOKASIDE|MEM
c1320 54 59 50 45 5f 48 45 41 50 29 20 29 3b 0d 0a 20  TYPE_HEAP) );.. 
c1330 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 7c   assert( db!=0 |
c1340 7c 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  | sqlite3Memdebu
c1350 67 4e 6f 54 79 70 65 28 70 2c 20 4d 45 4d 54 59  gNoType(p, MEMTY
c1360 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 29 20 29 3b  PE_LOOKASIDE) );
c1370 0d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  ..  sqlite3Memde
c1380 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d 45  bugSetType(p, ME
c1390 4d 54 59 50 45 5f 48 45 41 50 29 3b 0d 0a 20 20  MTYPE_HEAP);..  
c13a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
c13b0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68  ..}..../*..** Ch
c13c0 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ange the size of
c13d0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d   an existing mem
c13e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d 0a  ory allocation..
c13f0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
c1400 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
c1410 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f  Realloc(void *pO
c1420 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b  ld, int nBytes){
c1430 0d 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e 4e  ..  int nOld, nN
c1440 65 77 2c 20 6e 44 69 66 66 3b 0d 0a 20 20 76 6f  ew, nDiff;..  vo
c1450 69 64 20 2a 70 4e 65 77 3b 0d 0a 20 20 69 66 28  id *pNew;..  if(
c1460 20 70 4f 6c 64 3d 3d 30 20 29 7b 0d 0a 20 20 20   pOld==0 ){..   
c1470 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d   return sqlite3M
c1480 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 20 2f  alloc(nBytes); /
c1490 2a 20 49 4d 50 3a 20 52 2d 32 38 33 35 34 2d 32  * IMP: R-28354-2
c14a0 35 37 36 39 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20  5769 */..  }..  
c14b0 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b  if( nBytes<=0 ){
c14c0 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
c14d0 65 65 28 70 4f 6c 64 29 3b 20 2f 2a 20 49 4d 50  ee(pOld); /* IMP
c14e0 3a 20 52 2d 33 31 35 39 33 2d 31 30 35 37 34 20  : R-31593-10574 
c14f0 2a 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  */..    return 0
c1500 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6e 42  ;..  }..  if( nB
c1510 79 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30  ytes>=0x7fffff00
c1520 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20   ){..    /* The 
c1530 30 78 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20  0x7ffff00 limit 
c1540 74 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65  term is explaine
c1550 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e  d in comments on
c1560 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
c1570 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   */..    return 
c1580 30 3b 0d 0a 20 20 7d 0d 0a 20 20 6e 4f 6c 64 20  0;..  }..  nOld 
c1590 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
c15a0 69 7a 65 28 70 4f 6c 64 29 3b 0d 0a 20 20 2f 2a  ize(pOld);..  /*
c15b0 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d   IMPLEMENTATION-
c15c0 4f 46 3a 20 52 2d 34 36 31 39 39 2d 33 30 32 34  OF: R-46199-3024
c15d0 39 20 53 51 4c 69 74 65 20 67 75 61 72 61 6e 74  9 SQLite guarant
c15e0 65 65 73 20 74 68 61 74 20 74 68 65 20 73 65 63  ees that the sec
c15f0 6f 6e 64 0d 0a 20 20 2a 2a 20 61 72 67 75 6d 65  ond..  ** argume
c1600 6e 74 20 74 6f 20 78 52 65 61 6c 6c 6f 63 20 69  nt to xRealloc i
c1610 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c 75 65  s always a value
c1620 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70   returned by a p
c1630 72 69 6f 72 20 63 61 6c 6c 20 74 6f 0d 0a 20 20  rior call to..  
c1640 2a 2a 20 78 52 6f 75 6e 64 75 70 2e 20 2a 2f 0d  ** xRoundup. */.
c1650 0a 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  nNew = sqlite
c1660 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
c1670 78 52 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29  xRoundup(nBytes)
c1680 3b 0d 0a 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e  ;..  if( nOld==n
c1690 4e 65 77 20 29 7b 0d 0a 20 20 20 20 70 4e 65 77  New ){..    pNew
c16a0 20 3d 20 70 4f 6c 64 3b 0d 0a 20 20 7d 65 6c 73   = pOld;..  }els
c16b0 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f  e if( sqlite3Glo
c16c0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
c16d0 61 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  at ){..    sqlit
c16e0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
c16f0 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 20  em0.mutex);..   
c1700 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
c1710 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
c1720 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79  MALLOC_SIZE, nBy
c1730 74 65 73 29 3b 0d 0a 20 20 20 20 6e 44 69 66 66  tes);..    nDiff
c1740 20 3d 20 6e 4e 65 77 20 2d 20 6e 4f 6c 64 3b 0d   = nNew - nOld;.
c1750 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
c1760 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49  StatusValue(SQLI
c1770 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
c1780 5f 55 53 45 44 29 20 3e 3d 20 0d 0a 20 20 20 20  _USED) >= ..    
c1790 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d        mem0.alarm
c17a0 54 68 72 65 73 68 6f 6c 64 2d 6e 44 69 66 66 20  Threshold-nDiff 
c17b0 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
c17c0 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 44 69  3MallocAlarm(nDi
c17d0 66 66 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ff);..    }..   
c17e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
c17f0 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28  MemdebugHasType(
c1800 70 4f 6c 64 2c 20 4d 45 4d 54 59 50 45 5f 48 45  pOld, MEMTYPE_HE
c1810 41 50 29 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  AP) );..    asse
c1820 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  rt( sqlite3Memde
c1830 62 75 67 4e 6f 54 79 70 65 28 70 4f 6c 64 2c 20  bugNoType(pOld, 
c1840 7e 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29  ~MEMTYPE_HEAP) )
c1850 3b 0d 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  ;..    pNew = sq
c1860 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
c1870 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c  g.m.xRealloc(pOl
c1880 64 2c 20 6e 4e 65 77 29 3b 0d 0a 20 20 20 20 69  d, nNew);..    i
c1890 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 6d 65  f( pNew==0 && me
c18a0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
c18b0 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
c18c0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 42  e3MallocAlarm(nB
c18d0 79 74 65 73 29 3b 0d 0a 20 20 20 20 20 20 70 4e  ytes);..      pN
c18e0 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ew = sqlite3Glob
c18f0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c  alConfig.m.xReal
c1900 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b  loc(pOld, nNew);
c1910 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
c1920 20 70 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20   pNew ){..      
c1930 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61  nNew = sqlite3Ma
c1940 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b 0d  llocSize(pNew);.
c1950 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
c1960 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
c1970 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
c1980 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0d 0a  D, nNew-nOld);..
c1990 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
c19a0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
c19b0 65 6d 30 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d  em0.mutex);..  }
c19c0 65 6c 73 65 7b 0d 0a 20 20 20 20 70 4e 65 77 20  else{..    pNew 
c19d0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
c19e0 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63  onfig.m.xRealloc
c19f0 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0d 0a 20  (pOld, nNew);.. 
c1a00 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 45 49   }..  assert( EI
c1a10 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
c1a20 4e 54 28 70 4e 65 77 29 20 29 3b 20 2f 2a 20 49  NT(pNew) ); /* I
c1a30 4d 50 3a 20 52 2d 30 34 36 37 35 2d 34 34 38 35  MP: R-04675-4485
c1a40 30 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 70  0 */..  return p
c1a50 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  New;..}..../*..*
c1a60 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74  * The public int
c1a70 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65  erface to sqlite
c1a80 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20  3Realloc.  Make 
c1a90 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65  sure that the me
c1aa0 6d 6f 72 79 0d 0a 2a 2a 20 73 75 62 73 79 73 74  mory..** subsyst
c1ab0 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  em is initialize
c1ac0 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
c1ad0 69 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f  ing sqliteReallo
c1ae0 63 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  c...*/..SQLITE_A
c1af0 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
c1b00 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  _realloc(void *p
c1b10 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0d 0a 23 69  Old, int n){..#i
c1b20 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
c1b30 54 5f 41 55 54 4f 49 4e 49 54 0d 0a 20 20 69 66  T_AUTOINIT..  if
c1b40 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
c1b50 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
c1b60 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65  0;..#endif..  re
c1b70 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c  turn sqlite3Real
c1b80 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0d 0a 7d  loc(pOld, n);..}
c1b90 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c  ....../*..** All
c1ba0 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
c1bb0 65 6d 6f 72 79 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c  emory...*/ ..SQL
c1bc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
c1bd0 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a   *sqlite3MallocZ
c1be0 65 72 6f 28 69 6e 74 20 6e 29 7b 0d 0a 20 20 76  ero(int n){..  v
c1bf0 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
c1c00 4d 61 6c 6c 6f 63 28 6e 29 3b 0d 0a 20 20 69 66  Malloc(n);..  if
c1c10 28 20 70 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 73  ( p ){..    mems
c1c20 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0d 0a 20 20  et(p, 0, n);..  
c1c30 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a  }..  return p;..
c1c40 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f  }..../*..** Allo
c1c50 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65  cate and zero me
c1c60 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c  mory.  If the al
c1c70 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
c1c80 6d 61 6b 65 0d 0a 2a 2a 20 74 68 65 20 6d 61 6c  make..** the mal
c1c90 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
c1ca0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
c1cb0 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a 53   pointer...*/..S
c1cc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
c1cd0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c  id *sqlite3DbMal
c1ce0 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20  locZero(sqlite3 
c1cf0 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20  *db, int n){..  
c1d00 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
c1d10 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
c1d20 20 6e 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b   n);..  if( p ){
c1d30 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20  ..    memset(p, 
c1d40 30 2c 20 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  0, n);..  }..  r
c1d50 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn p;..}..../
c1d60 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  *..** Allocate a
c1d70 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20  nd zero memory. 
c1d80 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   If the allocati
c1d90 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0d 0a  on fails, make..
c1da0 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  ** the mallocFai
c1db0 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
c1dc0 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74  connection point
c1dd0 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 64  er...**..** If d
c1de0 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c  b!=0 and db->mal
c1df0 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75  locFailed is tru
c1e00 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20  e (indicating a 
c1e10 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0d 0a 2a 2a  prior malloc..**
c1e20 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20   failure on the 
c1e30 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
c1e40 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61  nnection) then a
c1e50 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0d  lways return 0..
c1e60 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20  .** Hence for a 
c1e70 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
c1e80 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
c1e90 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72  once malloc star
c1ea0 74 73 0d 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20  ts..** failing, 
c1eb0 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74  it fails consist
c1ec0 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c  ently until mall
c1ed0 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 65  ocFailed is rese
c1ee0 74 2e 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  t...** This is a
c1ef0 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75  n important assu
c1f00 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61  mption.  There a
c1f10 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69  re many places i
c1f20 6e 20 74 68 65 0d 0a 2a 2a 20 63 6f 64 65 20 74  n the..** code t
c1f30 68 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69  hat do things li
c1f40 6b 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  ke this:..**..**
c1f50 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a 61 20           int *a 
c1f60 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44  = (int*)sqlite3D
c1f70 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31  bMallocRaw(db, 1
c1f80 30 30 29 3b 0d 0a 2a 2a 20 20 20 20 20 20 20 20  00);..**        
c1f90 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74 2a 29   int *b = (int*)
c1fa0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
c1fb0 61 77 28 64 62 2c 20 32 30 30 29 3b 0d 0a 2a 2a  aw(db, 200);..**
c1fc0 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20 29           if( b )
c1fd0 20 61 5b 31 30 5d 20 3d 20 39 3b 0d 0a 2a 2a 0d   a[10] = 9;..**.
c1fe0 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
c1ff0 64 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75  ds, if a subsequ
c2000 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20  ent malloc (ex: 
c2010 22 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20  "b") worked, it 
c2020 69 73 20 61 73 73 75 6d 65 64 0d 0a 2a 2a 20 74  is assumed..** t
c2030 68 61 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61  hat all prior ma
c2040 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20  llocs (ex: "a") 
c2050 77 6f 72 6b 65 64 20 74 6f 6f 2e 0d 0a 2a 2f 0d  worked too...*/.
c2060 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c2070 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d  void *sqlite3DbM
c2080 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33  allocRaw(sqlite3
c2090 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20   *db, int n){.. 
c20a0 20 76 6f 69 64 20 2a 70 3b 0d 0a 20 20 61 73 73   void *p;..  ass
c20b0 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71  ert( db==0 || sq
c20c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
c20d0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  (db->mutex) );..
c20e0 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20    assert( db==0 
c20f0 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72  || db->pnBytesFr
c2100 65 65 64 3d 3d 30 20 29 3b 0d 0a 23 69 66 6e 64  eed==0 );..#ifnd
c2110 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
c2120 4f 4f 4b 41 53 49 44 45 0d 0a 20 20 69 66 28 20  OOKASIDE..  if( 
c2130 64 62 20 29 7b 0d 0a 20 20 20 20 4c 6f 6f 6b 61  db ){..    Looka
c2140 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0d  sideSlot *pBuf;.
c2150 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
c2160 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
c2170 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
c2180 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 64 62     }..    if( db
c2190 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
c21a0 62 6c 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 69  bled ){..      i
c21b0 66 28 20 6e 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69  f( n>db->lookasi
c21c0 64 65 2e 73 7a 20 29 7b 0d 0a 20 20 20 20 20 20  de.sz ){..      
c21d0 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
c21e0 61 6e 53 74 61 74 5b 31 5d 2b 2b 3b 0d 0a 20 20  anStat[1]++;..  
c21f0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
c2200 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Buf = db->lookas
c2210 69 64 65 2e 70 46 72 65 65 29 3d 3d 30 20 29 7b  ide.pFree)==0 ){
c2220 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f  ..        db->lo
c2230 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 32  okaside.anStat[2
c2240 5d 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  ]++;..      }els
c2250 65 7b 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  e{..        db->
c2260 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20  lookaside.pFree 
c2270 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0d 0a  = pBuf->pNext;..
c2280 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b          db->look
c2290 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0d 0a 20  aside.nOut++;.. 
c22a0 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61         db->looka
c22b0 73 69 64 65 2e 61 6e 53 74 61 74 5b 30 5d 2b 2b  side.anStat[0]++
c22c0 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  ;..        if( d
c22d0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
c22e0 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  t>db->lookaside.
c22f0 6d 78 4f 75 74 20 29 7b 0d 0a 20 20 20 20 20 20  mxOut ){..      
c2300 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
c2310 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
c2320 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0d 0a 20  okaside.nOut;.. 
c2330 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
c2340 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
c2350 70 42 75 66 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  pBuf;..      }..
c2360 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6c 73      }..  }..#els
c2370 65 0d 0a 20 20 69 66 28 20 64 62 20 26 26 20 64  e..  if( db && d
c2380 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
c2390 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
c23a0 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
c23b0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
c23c0 6c 6f 63 28 6e 29 3b 0d 0a 20 20 69 66 28 20 21  loc(n);..  if( !
c23d0 70 20 26 26 20 64 62 20 29 7b 0d 0a 20 20 20 20  p && db ){..    
c23e0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
c23f0 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71   = 1;..  }..  sq
c2400 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
c2410 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f  Type(p, MEMTYPE_
c2420 44 42 20 7c 0d 0a 20 20 20 20 20 20 20 20 20 28  DB |..         (
c2430 28 64 62 20 26 26 20 64 62 2d 3e 6c 6f 6f 6b 61  (db && db->looka
c2440 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 29 20 3f  side.bEnabled) ?
c2450 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49   MEMTYPE_LOOKASI
c2460 44 45 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45 41  DE : MEMTYPE_HEA
c2470 50 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70  P));..  return p
c2480 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
c2490 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20  esize the block 
c24a0 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  of memory pointe
c24b0 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62  d to by p to n b
c24c0 79 74 65 73 2e 20 49 66 20 74 68 65 0d 0a 2a 2a  ytes. If the..**
c24d0 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73   resize fails, s
c24e0 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  et the mallocFai
c24f0 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
c2500 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63  connection objec
c2510 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  t...*/..SQLITE_P
c2520 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
c2530 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71  ite3DbRealloc(sq
c2540 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
c2550 2a 70 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20 76  *p, int n){..  v
c2560 6f 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0d 0a  oid *pNew = 0;..
c2570 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
c2580 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
c2590 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
c25a0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  (db->mutex) );..
c25b0 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
c25c0 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0d 0a 20 20  Failed==0 ){..  
c25d0 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0d 0a 20    if( p==0 ){.. 
c25e0 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69       return sqli
c25f0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
c2600 62 2c 20 6e 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  b, n);..    }.. 
c2610 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69     if( isLookasi
c2620 64 65 28 64 62 2c 20 70 29 20 29 7b 0d 0a 20 20  de(db, p) ){..  
c2630 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c      if( n<=db->l
c2640 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0d 0a  ookaside.sz ){..
c2650 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
c2660 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
c2670 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
c2680 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
c2690 6e 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  n);..      if( p
c26a0 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  New ){..        
c26b0 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20  memcpy(pNew, p, 
c26c0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
c26d0 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
c26e0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
c26f0 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
c2700 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 73  }else{..      as
c2710 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d  sert( sqlite3Mem
c2720 64 65 62 75 67 48 61 73 54 79 70 65 28 70 2c 20  debugHasType(p, 
c2730 4d 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0d 0a  MEMTYPE_DB) );..
c2740 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71        assert( sq
c2750 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73  lite3MemdebugHas
c2760 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f  Type(p, MEMTYPE_
c2770 4c 4f 4f 4b 41 53 49 44 45 7c 4d 45 4d 54 59 50  LOOKASIDE|MEMTYP
c2780 45 5f 48 45 41 50 29 20 29 3b 0d 0a 20 20 20 20  E_HEAP) );..    
c2790 20 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75    sqlite3Memdebu
c27a0 67 53 65 74 54 79 70 65 28 70 2c 20 4d 45 4d 54  gSetType(p, MEMT
c27b0 59 50 45 5f 48 45 41 50 29 3b 0d 0a 20 20 20 20  YPE_HEAP);..    
c27c0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
c27d0 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0d  _realloc(p, n);.
c27e0 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
c27f0 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
c2800 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54  ite3MemdebugSetT
c2810 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44  ype(p, MEMTYPE_D
c2820 42 7c 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 3b  B|MEMTYPE_HEAP);
c2830 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61  ..        db->ma
c2840 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d  llocFailed = 1;.
c2850 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
c2860 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
c2870 65 74 54 79 70 65 28 70 4e 65 77 2c 20 4d 45 4d  etType(pNew, MEM
c2880 54 59 50 45 5f 44 42 20 7c 20 0d 0a 20 20 20 20  TYPE_DB | ..    
c2890 20 20 20 20 20 20 20 20 28 64 62 2d 3e 6c 6f 6f          (db->loo
c28a0 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
c28b0 3f 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53  ? MEMTYPE_LOOKAS
c28c0 49 44 45 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45  IDE : MEMTYPE_HE
c28d0 41 50 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  AP));..    }..  
c28e0 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  }..  return pNew
c28f0 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ;..}..../*..** A
c2900 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f  ttempt to reallo
c2910 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 20  cate p.  If the 
c2920 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  reallocation fai
c2930 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 0d  ls, then free p.
c2940 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20  .** and set the 
c2950 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
c2960 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  g in the databas
c2970 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a  e connection...*
c2980 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
c2990 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
c29a0 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73  bReallocOrFree(s
c29b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
c29c0 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20   *p, int n){..  
c29d0 76 6f 69 64 20 2a 70 4e 65 77 3b 0d 0a 20 20 70  void *pNew;..  p
c29e0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
c29f0 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29  ealloc(db, p, n)
c2a00 3b 0d 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29  ;..  if( !pNew )
c2a10 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  {..    sqlite3Db
c2a20 46 72 65 65 28 64 62 2c 20 70 29 3b 0d 0a 20 20  Free(db, p);..  
c2a30 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  }..  return pNew
c2a40 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  ;..}..../*..** M
c2a50 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20  ake a copy of a 
c2a60 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79  string in memory
c2a70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
c2a80 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54  qliteMalloc(). T
c2a90 68 65 73 65 20 0d 0a 2a 2a 20 66 75 6e 63 74 69  hese ..** functi
c2aa0 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ons call sqlite3
c2ab0 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 65  MallocRaw() dire
c2ac0 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20  ctly instead of 
c2ad0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
c2ae0 54 68 69 73 0d 0a 2a 2a 20 69 73 20 62 65 63 61  This..** is beca
c2af0 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  use when memory 
c2b00 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75 72  debugging is tur
c2b10 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77  ned on, these tw
c2b20 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  o functions are 
c2b30 0d 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20  ..** called via 
c2b40 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f  macros that reco
c2b50 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  rd the current f
c2b60 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d  ile and line num
c2b70 62 65 72 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 54  ber in the..** T
c2b80 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63 74  hreadData struct
c2b90 75 72 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ure...*/..SQLITE
c2ba0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
c2bb0 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73  qlite3DbStrDup(s
c2bc0 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
c2bd0 74 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 20 20 63  t char *z){..  c
c2be0 68 61 72 20 2a 7a 4e 65 77 3b 0d 0a 20 20 73 69  har *zNew;..  si
c2bf0 7a 65 5f 74 20 6e 3b 0d 0a 20 20 69 66 28 20 7a  ze_t n;..  if( z
c2c00 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  ==0 ){..    retu
c2c10 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 6e 20  rn 0;..  }..  n 
c2c20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
c2c30 30 28 7a 29 20 2b 20 31 3b 0d 0a 20 20 61 73 73  0(z) + 1;..  ass
c2c40 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66  ert( (n&0x7fffff
c2c50 66 66 29 3d 3d 6e 20 29 3b 0d 0a 20 20 7a 4e 65  ff)==n );..  zNe
c2c60 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
c2c70 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29  locRaw(db, (int)
c2c80 6e 29 3b 0d 0a 20 20 69 66 28 20 7a 4e 65 77 20  n);..  if( zNew 
c2c90 29 7b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  ){..    memcpy(z
c2ca0 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0d 0a 20 20 7d  New, z, n);..  }
c2cb0 0d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  ..  return zNew;
c2cc0 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..}..SQLITE_PRIV
c2cd0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
c2ce0 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74  3DbStrNDup(sqlit
c2cf0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
c2d00 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0d 0a  ar *z, int n){..
c2d10 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0d 0a 20    char *zNew;.. 
c2d20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0d 0a 20 20   if( z==0 ){..  
c2d30 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
c2d40 0d 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30  ..  assert( (n&0
c2d50 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b  x7fffffff)==n );
c2d60 0d 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ..  zNew = sqlit
c2d70 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
c2d80 2c 20 6e 2b 31 29 3b 0d 0a 20 20 69 66 28 20 7a  , n+1);..  if( z
c2d90 4e 65 77 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 63  New ){..    memc
c2da0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0d  py(zNew, z, n);.
c2db0 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30  .    zNew[n] = 0
c2dc0 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
c2dd0 20 7a 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   zNew;..}..../*.
c2de0 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72  .** Create a str
c2df0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 72  ing from the zFr
c2e00 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 6e  omat argument an
c2e10 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 68  d the va_list th
c2e20 61 74 20 66 6f 6c 6c 6f 77 73 2e 0d 0a 2a 2a 20  at follows...** 
c2e30 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67  Store the string
c2e40 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
c2e50 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
c2e60 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65  alloc() and make
c2e70 20 2a 70 7a 0d 0a 2a 2a 20 70 6f 69 6e 74 20 74   *pz..** point t
c2e80 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0d 0a  o that string...
c2e90 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
c2ea0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
c2eb0 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a  etString(char **
c2ec0 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  pz, sqlite3 *db,
c2ed0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
c2ee0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20 76  rmat, ...){..  v
c2ef0 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20 20 63 68  a_list ap;..  ch
c2f00 61 72 20 2a 7a 3b 0d 0a 0d 0a 20 20 76 61 5f 73  ar *z;....  va_s
c2f10 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
c2f20 29 3b 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  );..  z = sqlite
c2f30 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
c2f40 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76  ormat, ap);..  v
c2f50 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 73 71  a_end(ap);..  sq
c2f60 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
c2f70 2a 70 7a 29 3b 0d 0a 20 20 2a 70 7a 20 3d 20 7a  *pz);..  *pz = z
c2f80 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ;..}....../*..**
c2f90 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
c2fa0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65  ust be called be
c2fb0 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79  fore exiting any
c2fc0 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69   API function (i
c2fd0 2e 65 2e 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 69  .e. ..** returni
c2fe0 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68  ng control to th
c2ff0 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73  e user) that has
c3000 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f   called sqlite3_
c3010 6d 61 6c 6c 6f 63 20 6f 72 0d 0a 2a 2a 20 73 71  malloc or..** sq
c3020 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0d 0a  lite3_realloc...
c3030 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  **..** The retur
c3040 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72  ned value is nor
c3050 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20  mally a copy of 
c3060 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
c3070 65 6e 74 20 74 6f 20 74 68 69 73 0d 0a 2a 2a 20  ent to this..** 
c3080 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65  function. Howeve
c3090 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  r, if a malloc()
c30a0 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63   failure has occ
c30b0 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20  urred since the 
c30c0 70 72 65 76 69 6f 75 73 0d 0a 2a 2a 20 69 6e 76  previous..** inv
c30d0 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e  ocation SQLITE_N
c30e0 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
c30f0 20 69 6e 73 74 65 61 64 2e 20 0d 0a 2a 2a 0d 0a   instead. ..**..
c3100 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 74 20  ** If the first 
c3110 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73  argument, db, is
c3120 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20   not NULL and a 
c3130 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 68  malloc() error h
c3140 61 73 20 6f 63 63 75 72 72 65 64 2c 0d 0a 2a 2a  as occurred,..**
c3150 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63   then the connec
c3160 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20  tion error-code 
c3170 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72  (the value retur
c3180 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65  ned by sqlite3_e
c3190 72 72 63 6f 64 65 28 29 29 0d 0a 2a 2a 20 69 73  rrcode())..** is
c31a0 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
c31b0 4f 4d 45 4d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  OMEM...*/..SQLIT
c31c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
c31d0 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 71 6c  lite3ApiExit(sql
c31e0 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63  ite3* db, int rc
c31f0 29 7b 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ){..  /* If the 
c3200 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74  db handle is not
c3210 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d   NULL, then we m
c3220 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e  ust hold the con
c3230 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 0d 0a  nection handle..
c3240 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 2e    ** mutex here.
c3250 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 72   Otherwise the r
c3260 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62 6c  ead (and possibl
c3270 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d 3e  e write) of db->
c3280 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0d 0a 20  mallocFailed .. 
c3290 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61   ** is unsafe, a
c32a0 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f  s is the call to
c32b0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e   sqlite3Error().
c32c0 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ..  */..  assert
c32d0 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33  ( !db || sqlite3
c32e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
c32f0 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 69 66 28  mutex) );..  if(
c3300 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c   db && (db->mall
c3310 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d  ocFailed || rc==
c3320 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
c3330 45 4d 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  EM) ){..    sqli
c3340 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
c3350 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0d 0a  ITE_NOMEM, 0);..
c3360 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
c3370 69 6c 65 64 20 3d 20 30 3b 0d 0a 20 20 20 20 72  iled = 0;..    r
c3380 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
c3390 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
c33a0 20 72 63 20 26 20 28 64 62 20 3f 20 64 62 2d 3e   rc & (db ? db->
c33b0 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b  errMask : 0xff);
c33c0 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ..}..../********
c33d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61  ****** End of ma
c33e0 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lloc.c *********
c33f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3410 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
c3420 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
c3430 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a  le printf.c ****
c3440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3460 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20  ******/../*..** 
c3470 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f 64  The "printf" cod
c3480 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64  e that follows d
c3490 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31 39  ates from the 19
c34a0 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e 0d  80's.  It is in.
c34b0 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64  .** the public d
c34c0 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67  omain.  The orig
c34d0 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72  inal comments ar
c34e0 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20  e included here 
c34f0 66 6f 72 0d 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65  for..** complete
c3500 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20  ness.  They are 
c3510 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65  very out-of-date
c3520 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73   but might be us
c3530 65 66 75 6c 20 61 73 0d 0a 2a 2a 20 61 6e 20 68  eful as..** an h
c3540 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65  istorical refere
c3550 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68  nce.  Most of th
c3560 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22  e "enhancements"
c3570 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65   have been backe
c3580 64 0d 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61  d..** out so tha
c3590 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c  t the functional
c35a0 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73  ity is now the s
c35b0 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20  ame as standard 
c35c0 70 72 69 6e 74 66 28 29 2e 0d 0a 2a 2a 0d 0a 2a  printf()...**..*
c35d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c35e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c35f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3610 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  *********..**..*
c3620 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
c3630 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 61 20  ains code for a 
c3640 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 2d  set of "printf"-
c3650 6c 69 6b 65 20 72 6f 75 74 69 6e 65 73 2e 20 20  like routines.  
c3660 54 68 65 73 65 0d 0a 2a 2a 20 72 6f 75 74 69 6e  These..** routin
c3670 65 73 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  es format string
c3680 73 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20  s much like the 
c3690 70 72 69 6e 74 66 28 29 20 66 72 6f 6d 20 74 68  printf() from th
c36a0 65 20 73 74 61 6e 64 61 72 64 20 43 0d 0a 2a 2a  e standard C..**
c36b0 20 6c 69 62 72 61 72 79 2c 20 74 68 6f 75 67 68   library, though
c36c0 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
c36d0 69 6f 6e 20 68 65 72 65 20 68 61 73 20 65 6e 68  ion here has enh
c36e0 61 6e 63 65 6d 65 6e 74 73 20 74 6f 20 73 75 70  ancements to sup
c36f0 70 6f 72 74 0d 0a 2a 2a 20 53 51 4c 6c 69 74 65  port..** SQLlite
c3700 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
c3710 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73  Conversion types
c3720 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f   fall into vario
c3730 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 73  us categories as
c3740 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 0d   defined by the.
c3750 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e  .** following en
c3760 75 6d 65 72 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  umeration...*/..
c3770 23 64 65 66 69 6e 65 20 65 74 52 41 44 49 58 20  #define etRADIX 
c3780 20 20 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67        1 /* Integ
c3790 65 72 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25  er types.  %d, %
c37a0 78 2c 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f  x, %o, and so fo
c37b0 72 74 68 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  rth */..#define 
c37c0 65 74 46 4c 4f 41 54 20 20 20 20 20 20 20 32 20  etFLOAT       2 
c37d0 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  /* Floating poin
c37e0 74 2e 20 20 25 66 20 2a 2f 0d 0a 23 64 65 66 69  t.  %f */..#defi
c37f0 6e 65 20 65 74 45 58 50 20 20 20 20 20 20 20 20  ne etEXP        
c3800 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 6f   3 /* Exponentio
c3810 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65  nal notation. %e
c3820 20 61 6e 64 20 25 45 20 2a 2f 0d 0a 23 64 65 66   and %E */..#def
c3830 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20 20  ine etGENERIC   
c3840 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20    4 /* Floating 
c3850 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c 20  or exponential, 
c3860 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 70  depending on exp
c3870 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0d 0a 23 64  onent. %g */..#d
c3880 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20  efine etSIZE    
c3890 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20      5 /* Return 
c38a0 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
c38b0 74 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73  ters processed s
c38c0 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0d 0a 23 64  o far. %n */..#d
c38d0 65 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20  efine etSTRING  
c38e0 20 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73      6 /* Strings
c38f0 2e 20 25 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  . %s */..#define
c3900 20 65 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37   etDYNSTRING   7
c3910 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20   /* Dynamically 
c3920 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67  allocated string
c3930 73 2e 20 25 7a 20 2a 2f 0d 0a 23 64 65 66 69 6e  s. %z */..#defin
c3940 65 20 65 74 50 45 52 43 45 4e 54 20 20 20 20 20  e etPERCENT     
c3950 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 73 79 6d  8 /* Percent sym
c3960 62 6f 6c 2e 20 25 25 20 2a 2f 0d 0a 23 64 65 66  bol. %% */..#def
c3970 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 20 20  ine etCHARX     
c3980 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 65 72    9 /* Character
c3990 73 2e 20 25 63 20 2a 2f 0d 0a 2f 2a 20 54 68 65  s. %c */../* The
c39a0 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e 73   rest are extens
c39b0 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c  ions, not normal
c39c0 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e  ly found in prin
c39d0 74 66 28 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  tf() */..#define
c39e0 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30   etSQLESCAPE  10
c39f0 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68   /* Strings with
c3a00 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20   '\'' doubled.  
c3a10 25 71 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 65  %q */..#define e
c3a20 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f  tSQLESCAPE2 11 /
c3a30 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  * Strings with '
c3a40 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20  \'' doubled and 
c3a50 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0d  enclosed in '',.
c3a60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c3a70 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20             NULL 
c3a80 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65  pointers replace
c3a90 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20  d by SQL NULL.  
c3aa0 25 51 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 65  %Q */..#define e
c3ab0 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f  tTOKEN      12 /
c3ac0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
c3ad0 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65   Token structure
c3ae0 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 65 74 53   */..#define etS
c3af0 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20  RCLIST    13 /* 
c3b00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53  a pointer to a S
c3b10 72 63 4c 69 73 74 20 2a 2f 0d 0a 23 64 65 66 69  rcList */..#defi
c3b20 6e 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20  ne etPOINTER    
c3b30 31 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e  14 /* The %p con
c3b40 76 65 72 73 69 6f 6e 20 2a 2f 0d 0a 23 64 65 66  version */..#def
c3b50 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 33  ine etSQLESCAPE3
c3b60 20 31 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72   15 /* %w -> Str
c3b70 69 6e 67 73 20 77 69 74 68 20 27 5c 22 27 20 64  ings with '\"' d
c3b80 6f 75 62 6c 65 64 20 2a 2f 0d 0a 23 64 65 66 69  oubled */..#defi
c3b90 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 20 20 20  ne etORDINAL    
c3ba0 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c  16 /* %r -> 1st,
c3bb0 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 68 2c 20   2nd, 3rd, 4th, 
c3bc0 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e  etc.  English on
c3bd0 6c 79 20 2a 2f 0d 0a 0d 0a 23 64 65 66 69 6e 65  ly */....#define
c3be0 20 65 74 49 4e 56 41 4c 49 44 20 20 20 20 20 30   etINVALID     0
c3bf0 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 6e   /* Any unrecogn
c3c00 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ized conversion 
c3c10 74 79 70 65 20 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d  type */....../*.
c3c20 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 65 22 20  .** An "etByte" 
c3c30 69 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69  is an 8-bit unsi
c3c40 67 6e 65 64 20 76 61 6c 75 65 2e 0d 0a 2a 2f 0d  gned value...*/.
c3c50 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
c3c60 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0d 0a  d char etByte;..
c3c70 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 62 75  ../*..** Each bu
c3c80 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e  iltin conversion
c3c90 20 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20   character (ex: 
c3ca0 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29  the 'd' in "%d")
c3cb0 20 69 73 20 64 65 73 63 72 69 62 65 64 0d 0a 2a   is described..*
c3cc0 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  * by an instance
c3cd0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
c3ce0 67 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2f 0d  g structure..*/.
c3cf0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
c3d00 65 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49  et_info {   /* I
c3d10 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
c3d20 20 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65   each format fie
c3d30 6c 64 20 2a 2f 0d 0a 20 20 63 68 61 72 20 66 6d  ld */..  char fm
c3d40 74 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  ttype;          
c3d50 20 20 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20    /* The format 
c3d60 66 69 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65  field code lette
c3d70 72 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20 62  r */..  etByte b
c3d80 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
c3d90 20 2f 2a 20 54 68 65 20 62 61 73 65 20 66 6f 72   /* The base for
c3da0 20 72 61 64 69 78 20 63 6f 6e 76 65 72 73 69 6f   radix conversio
c3db0 6e 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20 66  n */..  etByte f
c3dc0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
c3dd0 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20   /* One or more 
c3de0 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 61 6e  of FLAG_ constan
c3df0 74 73 20 62 65 6c 6f 77 20 2a 2f 0d 0a 20 20 65  ts below */..  e
c3e00 74 42 79 74 65 20 74 79 70 65 3b 20 20 20 20 20  tByte type;     
c3e10 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
c3e20 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a  rsion paradigm *
c3e30 2f 0d 0a 20 20 65 74 42 79 74 65 20 63 68 61 72  /..  etByte char
c3e40 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  set;          /*
c3e50 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44 69   Offset into aDi
c3e60 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64 69  gits[] of the di
c3e70 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0d 0a  gits string */..
c3e80 20 20 65 74 42 79 74 65 20 70 72 65 66 69 78 3b    etByte prefix;
c3e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
c3ea0 66 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69  fset into aPrefi
c3eb0 78 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69  x[] of the prefi
c3ec0 78 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 7d 20 65  x string */..} e
c3ed0 74 5f 69 6e 66 6f 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  t_info;..../*..*
c3ee0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
c3ef0 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61   for et_info.fla
c3f00 67 73 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  gs..*/..#define 
c3f10 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20  FLAG_SIGNED  1  
c3f20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
c3f30 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65  e value to conve
c3f40 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0d  rt is signed */.
c3f50 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e  .#define FLAG_IN
c3f60 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54  TERN  2     /* T
c3f70 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72  rue if for inter
c3f80 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0d  nal use only */.
c3f90 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54  .#define FLAG_ST
c3fa0 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41  RING  4     /* A
c3fb0 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72  llow infinity pr
c3fc0 65 63 69 73 69 6f 6e 20 2a 2f 0d 0a 0d 0a 0d 0a  ecision */......
c3fd0 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  /*..** The follo
c3fe0 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 73 65  wing table is se
c3ff0 61 72 63 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c  arched linearly,
c4000 20 73 6f 20 69 74 20 69 73 20 67 6f 6f 64 20 74   so it is good t
c4010 6f 20 70 75 74 20 74 68 65 0d 0a 2a 2a 20 6d 6f  o put the..** mo
c4020 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75 73  st frequently us
c4030 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79  ed conversion ty
c4040 70 65 73 20 66 69 72 73 74 2e 0d 0a 2a 2f 0d 0a  pes first...*/..
c4050 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
c4060 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22 30  r aDigits[] = "0
c4070 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 30  123456789ABCDEF0
c4080 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 22  123456789abcdef"
c4090 3b 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ;..static const 
c40a0 63 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d  char aPrefix[] =
c40b0 20 22 2d 78 30 5c 30 30 30 58 30 22 3b 0d 0a 73   "-x0\000X0";..s
c40c0 74 61 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69  tatic const et_i
c40d0 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20  nfo fmtinfo[] = 
c40e0 7b 0d 0a 20 20 7b 20 20 27 64 27 2c 20 31 30 2c  {..  {  'd', 10,
c40f0 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20   1, etRADIX,    
c4100 20 20 30 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b 20    0,  0 },..  { 
c4110 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 74 53   's',  0, 4, etS
c4120 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 20 30  TRING,     0,  0
c4130 20 7d 2c 0d 0a 20 20 7b 20 20 27 67 27 2c 20 20   },..  {  'g',  
c4140 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
c4150 20 20 20 20 33 30 2c 20 30 20 7d 2c 0d 0a 20 20      30, 0 },..  
c4160 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65  {  'z',  0, 4, e
c4170 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20  tDYNSTRING,  0, 
c4180 20 30 20 7d 2c 0d 0a 20 20 7b 20 20 27 71 27 2c   0 },..  {  'q',
c4190 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
c41a0 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0d 0a  APE,  0,  0 },..
c41b0 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c    {  'Q',  0, 4,
c41c0 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30   etSQLESCAPE2, 0
c41d0 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b 20 20 27 77  ,  0 },..  {  'w
c41e0 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
c41f0 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c  SCAPE3, 0,  0 },
c4200 0d 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20  ..  {  'c',  0, 
c4210 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20  0, etCHARX,     
c4220 20 30 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b 20 20   0,  0 },..  {  
c4230 27 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41  'o',  8, 0, etRA
c4240 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20  DIX,      0,  2 
c4250 7d 2c 0d 0a 20 20 7b 20 20 27 75 27 2c 20 31 30  },..  {  'u', 10
c4260 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20  , 0, etRADIX,   
c4270 20 20 20 30 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b     0,  0 },..  {
c4280 20 20 27 78 27 2c 20 31 36 2c 20 30 2c 20 65 74    'x', 16, 0, et
c4290 52 41 44 49 58 2c 20 20 20 20 20 20 31 36 2c 20  RADIX,      16, 
c42a0 31 20 7d 2c 0d 0a 20 20 7b 20 20 27 58 27 2c 20  1 },..  {  'X', 
c42b0 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20  16, 0, etRADIX, 
c42c0 20 20 20 20 20 30 2c 20 20 34 20 7d 2c 0d 0a 23       0,  4 },..#
c42d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
c42e0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
c42f0 54 0d 0a 20 20 7b 20 20 27 66 27 2c 20 20 30 2c  T..  {  'f',  0,
c4300 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20 20   1, etFLOAT,    
c4310 20 20 30 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b 20    0,  0 },..  { 
c4320 20 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45   'e',  0, 1, etE
c4330 58 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30  XP,        30, 0
c4340 20 7d 2c 0d 0a 20 20 7b 20 20 27 45 27 2c 20 20   },..  {  'E',  
c4350 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20  0, 1, etEXP,    
c4360 20 20 20 20 31 34 2c 20 30 20 7d 2c 0d 0a 20 20      14, 0 },..  
c4370 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65  {  'G',  0, 1, e
c4380 74 47 45 4e 45 52 49 43 2c 20 20 20 20 31 34 2c  tGENERIC,    14,
c4390 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20   0 },..#endif.. 
c43a0 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c 20   {  'i', 10, 1, 
c43b0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
c43c0 20 20 30 20 7d 2c 0d 0a 20 20 7b 20 20 27 6e 27    0 },..  {  'n'
c43d0 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c  ,  0, 0, etSIZE,
c43e0 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0d         0,  0 },.
c43f0 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30  .  {  '%',  0, 0
c4400 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20  , etPERCENT,    
c4410 30 2c 20 20 30 20 7d 2c 0d 0a 20 20 7b 20 20 27  0,  0 },..  {  '
c4420 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49  p', 16, 0, etPOI
c4430 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d  NTER,    0,  1 }
c4440 2c 0d 0a 0d 0a 2f 2a 20 41 6c 6c 20 74 68 65 20  ,..../* All the 
c4450 72 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c  rest have the FL
c4460 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65  AG_INTERN bit se
c4470 74 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66  t and are thus f
c4480 6f 72 20 69 6e 74 65 72 6e 61 6c 0d 0a 2a 2a 20  or internal..** 
c4490 75 73 65 20 6f 6e 6c 79 20 2a 2f 0d 0a 20 20 7b  use only */..  {
c44a0 20 20 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74    'T',  0, 2, et
c44b0 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20  TOKEN,      0,  
c44c0 30 20 7d 2c 0d 0a 20 20 7b 20 20 27 53 27 2c 20  0 },..  {  'S', 
c44d0 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 53 54   0, 2, etSRCLIST
c44e0 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0d 0a 20  ,    0,  0 },.. 
c44f0 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c 20   {  'r', 10, 3, 
c4500 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 2c  etORDINAL,    0,
c4510 20 20 30 20 7d 2c 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a    0 },..};..../*
c4520 0d 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  ..** If SQLITE_O
c4530 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
c4540 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  NT is defined, t
c4550 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  hen none of the 
c4560 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0d 0a  floating point..
c4570 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77  ** conversions w
c4580 69 6c 6c 20 77 6f 72 6b 2e 0d 0a 2a 2f 0d 0a 23  ill work...*/..#
c4590 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
c45a0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
c45b0 54 0d 0a 2f 2a 0d 0a 2a 2a 20 22 2a 76 61 6c 22  T../*..** "*val"
c45c0 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63   is a double suc
c45d0 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76  h that 0.1 <= *v
c45e0 61 6c 20 3c 20 31 30 2e 30 0d 0a 2a 2a 20 52 65  al < 10.0..** Re
c45f0 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 20 63  turn the ascii c
c4600 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61 64  ode for the lead
c4610 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76 61  ing digit of *va
c4620 6c 2c 20 74 68 65 6e 0d 0a 2a 2a 20 6d 75 6c 74  l, then..** mult
c4630 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31  iply "*val" by 1
c4640 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69  0.0 to renormali
c4650 7a 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 61 6d  ze...**..** Exam
c4660 70 6c 65 3a 0d 0a 2a 2a 20 20 20 20 20 69 6e 70  ple:..**     inp
c4670 75 74 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 33  ut:     *val = 3
c4680 2e 31 34 31 35 39 0d 0a 2a 2a 20 20 20 20 20 6f  .14159..**     o
c4690 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d  utput:    *val =
c46a0 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e 63 74   1.4159    funct
c46b0 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27 33 27  ion return = '3'
c46c0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 75  ..**..** The cou
c46d0 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63  nter *cnt is inc
c46e0 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69  remented each ti
c46f0 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74  me.  After count
c4700 65 72 20 65 78 63 65 65 64 73 0d 0a 2a 2a 20 31  er exceeds..** 1
c4710 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  6 (the number of
c4720 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
c4730 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20  its in a 64-bit 
c4740 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0d 0a 2a  float) '0' is..*
c4750 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65  * always returne
c4760 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63  d...*/..static c
c4770 68 61 72 20 65 74 5f 67 65 74 64 69 67 69 74 28  har et_getdigit(
c4780 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
c4790 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b  *val, int *cnt){
c47a0 0d 0a 20 20 69 6e 74 20 64 69 67 69 74 3b 0d 0a  ..  int digit;..
c47b0 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50    LONGDOUBLE_TYP
c47c0 45 20 64 3b 0d 0a 20 20 69 66 28 20 28 2a 63 6e  E d;..  if( (*cn
c47d0 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 74  t)++ >= 16 ) ret
c47e0 75 72 6e 20 27 30 27 3b 0d 0a 20 20 64 69 67 69  urn '0';..  digi
c47f0 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0d 0a  t = (int)*val;..
c4800 20 20 64 20 3d 20 64 69 67 69 74 3b 0d 0a 20 20    d = digit;..  
c4810 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0d 0a 20  digit += '0';.. 
c4820 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20   *val = (*val - 
c4830 64 29 2a 31 30 2e 30 3b 0d 0a 20 20 72 65 74 75  d)*10.0;..  retu
c4840 72 6e 20 28 63 68 61 72 29 64 69 67 69 74 3b 0d  rn (char)digit;.
c4850 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
c4860 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
c4870 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0d 0a 0d 0a 2f  NG_POINT */..../
c4880 2a 0d 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73  *..** Append N s
c4890 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20  pace characters 
c48a0 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  to the given str
c48b0 69 6e 67 20 62 75 66 66 65 72 2e 0d 0a 2a 2f 0d  ing buffer...*/.
c48c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c48d0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 70 70 65  void sqlite3Appe
c48e0 6e 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d  ndSpace(StrAccum
c48f0 20 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29   *pAccum, int N)
c4900 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  {..  static cons
c4910 74 20 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d  t char zSpaces[]
c4920 20 3d 20 22 20 20 20 20 20 20 20 20 20 20 20 20   = "            
c4930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4940 20 22 3b 0d 0a 20 20 77 68 69 6c 65 28 20 4e 3e   ";..  while( N>
c4950 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70  =(int)sizeof(zSp
c4960 61 63 65 73 29 2d 31 20 29 7b 0d 0a 20 20 20 20  aces)-1 ){..    
c4970 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
c4980 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53  ppend(pAccum, zS
c4990 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28 7a 53  paces, sizeof(zS
c49a0 70 61 63 65 73 29 2d 31 29 3b 0d 0a 20 20 20 20  paces)-1);..    
c49b0 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70 61  N -= sizeof(zSpa
c49c0 63 65 73 29 2d 31 3b 0d 0a 20 20 7d 0d 0a 20 20  ces)-1;..  }..  
c49d0 69 66 28 20 4e 3e 30 20 29 7b 0d 0a 20 20 20 20  if( N>0 ){..    
c49e0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
c49f0 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53  ppend(pAccum, zS
c4a00 70 61 63 65 73 2c 20 4e 29 3b 0d 0a 20 20 7d 0d  paces, N);..  }.
c4a10 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 6e 20  .}..../*..** On 
c4a20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20  machines with a 
c4a30 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65  small stack size
c4a40 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69  , you can redefi
c4a50 6e 65 20 74 68 65 0d 0a 2a 2a 20 53 51 4c 49 54  ne the..** SQLIT
c4a60 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
c4a70 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67   to be something
c4a80 20 73 6d 61 6c 6c 65 72 2c 20 69 66 20 64 65 73   smaller, if des
c4a90 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  ired...*/..#ifnd
c4aa0 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ef SQLITE_PRINT_
c4ab0 42 55 46 5f 53 49 5a 45 0d 0a 23 20 64 65 66 69  BUF_SIZE..# defi
c4ac0 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ne SQLITE_PRINT_
c4ad0 42 55 46 5f 53 49 5a 45 20 37 30 0d 0a 23 65 6e  BUF_SIZE 70..#en
c4ae0 64 69 66 0d 0a 23 64 65 66 69 6e 65 20 65 74 42  dif..#define etB
c4af0 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52  UFSIZE SQLITE_PR
c4b00 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a  INT_BUF_SIZE  /*
c4b10 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   Size of the out
c4b20 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0d 0a 0d  put buffer */...
c4b30 0a 2f 2a 0d 0a 2a 2a 20 52 65 6e 64 65 72 20 61  ./*..** Render a
c4b40 20 73 74 72 69 6e 67 20 67 69 76 65 6e 20 62 79   string given by
c4b50 20 22 66 6d 74 22 20 69 6e 74 6f 20 74 68 65 20   "fmt" into the 
c4b60 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e  StrAccum object.
c4b70 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
c4b80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
c4b90 33 56 58 50 72 69 6e 74 66 28 0d 0a 20 20 53 74  3VXPrintf(..  St
c4ba0 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20  rAccum *pAccum, 
c4bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4bc0 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72   /* Accumulate r
c4bd0 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0d 0a  esults here */..
c4be0 20 20 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65    int useExtende
c4bf0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
c4c00 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78       /* Allow ex
c4c10 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73  tended %-convers
c4c20 69 6f 6e 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ions */..  const
c4c30 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 20   char *fmt,     
c4c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c4c50 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a   Format string *
c4c60 2f 0d 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20  /..  va_list ap 
c4c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4c80 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 6d          /* argum
c4c90 65 6e 74 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  ents */..){..  i
c4ca0 6e 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20  nt c;           
c4cb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
c4cc0 74 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74  t character in t
c4cd0 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
c4ce0 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 62 75 66   */..  char *buf
c4cf0 70 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pt;             
c4d00 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
c4d10 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62  the conversion b
c4d20 75 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  uffer */..  int 
c4d30 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 20 20 20  precision;      
c4d40 20 20 20 20 20 20 20 2f 2a 20 50 72 65 63 69 73         /* Precis
c4d50 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65  ion of the curre
c4d60 6e 74 20 66 69 65 6c 64 20 2a 2f 0d 0a 20 20 69  nt field */..  i
c4d70 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20  nt length;      
c4d80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
c4d90 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 6c 64  gth of the field
c4da0 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 64 78 3b 20   */..  int idx; 
c4db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4dc0 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 70    /* A general p
c4dd0 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 6e  urpose loop coun
c4de0 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 77 69  ter */..  int wi
c4df0 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  dth;            
c4e00 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66       /* Width of
c4e10 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65   the current fie
c4e20 6c 64 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  ld */..  etByte 
c4e30 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
c4e40 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22  ;   /* True if "
c4e50 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  -" flag is prese
c4e60 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  nt */..  etByte 
c4e70 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20  flag_plussign;  
c4e80 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
c4e90 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  +" flag is prese
c4ea0 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  nt */..  etByte 
c4eb0 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20  flag_blanksign; 
c4ec0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
c4ed0 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65   " flag is prese
c4ee0 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  nt */..  etByte 
c4ef0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
c4f00 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22  rm; /* True if "
c4f10 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  #" flag is prese
c4f20 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  nt */..  etByte 
c4f30 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20  flag_altform2;  
c4f40 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
c4f50 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  !" flag is prese
c4f60 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65 20  nt */..  etByte 
c4f70 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20  flag_zeropad;   
c4f80 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66      /* True if f
c4f90 69 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74  ield width const
c4fa0 61 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 20  ant starts with 
c4fb0 7a 65 72 6f 20 2a 2f 0d 0a 20 20 65 74 42 79 74  zero */..  etByt
c4fc0 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20  e flag_long;    
c4fd0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
c4fe0 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65   "l" flag is pre
c4ff0 73 65 6e 74 20 2a 2f 0d 0a 20 20 65 74 42 79 74  sent */..  etByt
c5000 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b  e flag_longlong;
c5010 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
c5020 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 69   the "ll" flag i
c5030 73 20 70 72 65 73 65 6e 74 20 2a 2f 0d 0a 20 20  s present */..  
c5040 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20  etByte done;    
c5050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
c5060 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66  op termination f
c5070 6c 61 67 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65  lag */..  etByte
c5080 20 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20   xtype = 0;     
c5090 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
c50a0 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0d 0a  on paradigm */..
c50b0 20 20 63 68 61 72 20 70 72 65 66 69 78 3b 20 20    char prefix;  
c50c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c50d0 50 72 65 66 69 78 20 63 68 61 72 61 63 74 65 72  Prefix character
c50e0 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72  .  "+" or "-" or
c50f0 20 22 20 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f   " " or '\0'. */
c5100 0d 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ..  sqlite_uint6
c5110 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f  4 longvalue;   /
c5120 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65  * Value for inte
c5130 67 65 72 20 74 79 70 65 73 20 2a 2f 0d 0a 20 20  ger types */..  
c5140 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
c5150 72 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61  realvalue; /* Va
c5160 6c 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70  lue for real typ
c5170 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 65  es */..  const e
c5180 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20  t_info *infop;  
c5190 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
c51a0 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
c51b0 65 20 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  e info structure
c51c0 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4f 75   */..  char *zOu
c51d0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
c51e0 20 20 2f 2a 20 52 65 6e 64 65 72 69 6e 67 20 62    /* Rendering b
c51f0 75 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  uffer */..  int 
c5200 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  nOut;           
c5210 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
c5220 66 20 74 68 65 20 72 65 6e 64 65 72 69 6e 67 20  f the rendering 
c5230 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 63 68 61  buffer */..  cha
c5240 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 20  r *zExtra;      
c5250 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f          /* Mallo
c5260 63 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  ced memory used 
c5270 62 79 20 73 6f 6d 65 20 63 6f 6e 76 65 72 73 69  by some conversi
c5280 6f 6e 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  on */..#ifndef S
c5290 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
c52a0 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 69 6e 74  ING_POINT..  int
c52b0 20 20 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20    exp, e2;      
c52c0 20 20 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e          /* expon
c52d0 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62  ent of real numb
c52e0 65 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 73  ers */..  int ns
c52f0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
c5300 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
c5310 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  f significant di
c5320 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f  gits returned */
c5330 0d 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64  ..  double round
c5340 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  er;            /
c5350 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64  * Used for round
c5360 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  ing floating poi
c5370 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 20 20  nt values */..  
c5380 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20  etByte flag_dp; 
c5390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
c53a0 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f  ue if decimal po
c53b0 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68  int should be sh
c53c0 6f 77 6e 20 2a 2f 0d 0a 20 20 65 74 42 79 74 65  own */..  etByte
c53d0 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20   flag_rtz;      
c53e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
c53f0 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73  trailing zeros s
c5400 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64  hould be removed
c5410 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 63   */..#endif..  c
c5420 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49 5a  har buf[etBUFSIZ
c5430 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  E];       /* Con
c5440 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a  version buffer *
c5450 2f 0d 0a 0d 0a 20 20 62 75 66 70 74 20 3d 20 30  /....  bufpt = 0
c5460 3b 0d 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a  ;..  for(; (c=(*
c5470 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29  fmt))!=0; ++fmt)
c5480 7b 0d 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25  {..    if( c!='%
c5490 27 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  ' ){..      int 
c54a0 61 6d 74 3b 0d 0a 20 20 20 20 20 20 62 75 66 70  amt;..      bufp
c54b0 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b  t = (char *)fmt;
c54c0 0d 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b  ..      amt = 1;
c54d0 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  ..      while( (
c54e0 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27  c=(*++fmt))!='%'
c54f0 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b   && c!=0 ) amt++
c5500 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
c5510 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
c5520 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d  Accum, bufpt, am
c5530 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 63  t);..      if( c
c5540 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  ==0 ) break;..  
c5550 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 28 63 3d    }..    if( (c=
c5560 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0d  (*++fmt))==0 ){.
c5570 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
c5580 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
c5590 63 75 6d 2c 20 22 25 22 2c 20 31 29 3b 0d 0a 20  cum, "%", 1);.. 
c55a0 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
c55b0 20 7d 0d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20   }..    /* Find 
c55c0 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 61  out what flags a
c55d0 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0d 0a 20  re present */.. 
c55e0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
c55f0 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73  ify = flag_pluss
c5600 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b  ign = flag_blank
c5610 73 69 67 6e 20 3d 20 0d 0a 20 20 20 20 20 66 6c  sign = ..     fl
c5620 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
c5630 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32   = flag_altform2
c5640 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20   = flag_zeropad 
c5650 3d 20 30 3b 0d 0a 20 20 20 20 64 6f 6e 65 20 3d  = 0;..    done =
c5660 20 30 3b 0d 0a 20 20 20 20 64 6f 7b 0d 0a 20 20   0;..    do{..  
c5670 20 20 20 20 73 77 69 74 63 68 28 20 63 20 29 7b      switch( c ){
c5680 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ..        case '
c5690 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a  -':   flag_leftj
c56a0 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20  ustify = 1;     
c56b0 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
c56c0 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67  case '+':   flag
c56d0 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20  _plussign = 1;  
c56e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
c56f0 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 20        case ' ': 
c5700 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e    flag_blanksign
c5710 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65 61   = 1;       brea
c5720 6b 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65  k;..        case
c5730 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74   '#':   flag_alt
c5740 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20  ernateform = 1; 
c5750 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
c5760 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c    case '!':   fl
c5770 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b  ag_altform2 = 1;
c5780 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
c5790 20 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27          case '0'
c57a0 3a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  :   flag_zeropad
c57b0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72   = 1;         br
c57c0 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 64 65  eak;..        de
c57d0 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d  fault:    done =
c57e0 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   1;             
c57f0 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
c5800 20 20 7d 0d 0a 20 20 20 20 7d 77 68 69 6c 65 28    }..    }while(
c5810 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a 2b   !done && (c=(*+
c5820 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0d 0a 20 20  +fmt))!=0 );..  
c5830 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69 65    /* Get the fie
c5840 6c 64 20 77 69 64 74 68 20 2a 2f 0d 0a 20 20 20  ld width */..   
c5850 20 77 69 64 74 68 20 3d 20 30 3b 0d 0a 20 20 20   width = 0;..   
c5860 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0d 0a   if( c=='*' ){..
c5870 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 61        width = va
c5880 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0d 0a 20  _arg(ap,int);.. 
c5890 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30       if( width<0
c58a0 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 66 6c 61   ){..        fla
c58b0 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20  g_leftjustify = 
c58c0 31 3b 0d 0a 20 20 20 20 20 20 20 20 77 69 64 74  1;..        widt
c58d0 68 20 3d 20 2d 77 69 64 74 68 3b 0d 0a 20 20 20  h = -width;..   
c58e0 20 20 20 7d 0d 0a 20 20 20 20 20 20 63 20 3d 20     }..      c = 
c58f0 2a 2b 2b 66 6d 74 3b 0d 0a 20 20 20 20 7d 65 6c  *++fmt;..    }el
c5900 73 65 7b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65  se{..      while
c5910 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  ( c>='0' && c<='
c5920 39 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 77  9' ){..        w
c5930 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20  idth = width*10 
c5940 2b 20 63 20 2d 20 27 30 27 3b 0d 0a 20 20 20 20  + c - '0';..    
c5950 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0d      c = *++fmt;.
c5960 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
c5970 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20  .    /* Get the 
c5980 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0d 0a 20 20  precision */..  
c5990 20 20 69 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0d    if( c=='.' ){.
c59a0 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e  .      precision
c59b0 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 63 20 3d   = 0;..      c =
c59c0 20 2a 2b 2b 66 6d 74 3b 0d 0a 20 20 20 20 20 20   *++fmt;..      
c59d0 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0d 0a 20  if( c=='*' ){.. 
c59e0 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e         precision
c59f0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
c5a00 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
c5a10 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72  precision<0 ) pr
c5a20 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69  ecision = -preci
c5a30 73 69 6f 6e 3b 0d 0a 20 20 20 20 20 20 20 20 63  sion;..        c
c5a40 20 3d 20 2a 2b 2b 66 6d 74 3b 0d 0a 20 20 20 20   = *++fmt;..    
c5a50 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
c5a60 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20    while( c>='0' 
c5a70 26 26 20 63 3c 3d 27 39 27 20 29 7b 0d 0a 20 20  && c<='9' ){..  
c5a80 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f          precisio
c5a90 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30  n = precision*10
c5aa0 20 2b 20 63 20 2d 20 27 30 27 3b 0d 0a 20 20 20   + c - '0';..   
c5ab0 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
c5ac0 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  t;..        }.. 
c5ad0 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73       }..    }els
c5ae0 65 7b 0d 0a 20 20 20 20 20 20 70 72 65 63 69 73  e{..      precis
c5af0 69 6f 6e 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d  ion = -1;..    }
c5b00 0d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65  ..    /* Get the
c5b10 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65   conversion type
c5b20 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0d 0a 20 20   modifier */..  
c5b30 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0d    if( c=='l' ){.
c5b40 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
c5b50 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 63 20 3d   = 1;..      c =
c5b60 20 2a 2b 2b 66 6d 74 3b 0d 0a 20 20 20 20 20 20   *++fmt;..      
c5b70 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0d 0a 20  if( c=='l' ){.. 
c5b80 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
c5b90 6c 6f 6e 67 20 3d 20 31 3b 0d 0a 20 20 20 20 20  long = 1;..     
c5ba0 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0d 0a     c = *++fmt;..
c5bb0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
c5bc0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c        flag_longl
c5bd0 6f 6e 67 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ong = 0;..      
c5be0 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  }..    }else{.. 
c5bf0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d       flag_long =
c5c00 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d   flag_longlong =
c5c10 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   0;..    }..    
c5c20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66  /* Fetch the inf
c5c30 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  o entry for the 
c5c40 66 69 65 6c 64 20 2a 2f 0d 0a 20 20 20 20 69 6e  field */..    in
c5c50 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30  fop = &fmtinfo[0
c5c60 5d 3b 0d 0a 20 20 20 20 78 74 79 70 65 20 3d 20  ];..    xtype = 
c5c70 65 74 49 4e 56 41 4c 49 44 3b 0d 0a 20 20 20 20  etINVALID;..    
c5c80 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78 3c 41  for(idx=0; idx<A
c5c90 72 72 61 79 53 69 7a 65 28 66 6d 74 69 6e 66 6f  rraySize(fmtinfo
c5ca0 29 3b 20 69 64 78 2b 2b 29 7b 0d 0a 20 20 20 20  ); idx++){..    
c5cb0 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e 66 6f    if( c==fmtinfo
c5cc0 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 20 29 7b  [idx].fmttype ){
c5cd0 0d 0a 20 20 20 20 20 20 20 20 69 6e 66 6f 70 20  ..        infop 
c5ce0 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 3b  = &fmtinfo[idx];
c5cf0 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 73  ..        if( us
c5d00 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e  eExtended || (in
c5d10 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41  fop->flags & FLA
c5d20 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0d  G_INTERN)==0 ){.
c5d30 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65  .          xtype
c5d40 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0d   = infop->type;.
c5d50 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
c5d60 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
c5d70 6e 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  n;..        }.. 
c5d80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
c5d90 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
c5da0 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0d 0a     zExtra = 0;..
c5db0 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a  ..    /*..    **
c5dc0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
c5dd0 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e  variables are in
c5de0 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c  itialized as fol
c5df0 6c 6f 77 73 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20  lows:..    **.. 
c5e00 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
c5e10 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20  ernateform      
c5e20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23      TRUE if a '#
c5e30 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0d 0a 20  ' is present... 
c5e40 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
c5e50 66 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20  form2           
c5e60 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21      TRUE if a '!
c5e70 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0d 0a 20  ' is present... 
c5e80 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75     **   flag_plu
c5e90 73 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20  ssign           
c5ea0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2b      TRUE if a '+
c5eb0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0d 0a 20  ' is present... 
c5ec0 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66     **   flag_lef
c5ed0 74 6a 75 73 74 69 66 79 20 20 20 20 20 20 20 20  tjustify        
c5ee0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2d      TRUE if a '-
c5ef0 27 20 69 73 20 70 72 65 73 65 6e 74 20 6f 72 20  ' is present or 
c5f00 69 66 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 20  if the..    **  
c5f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5f20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 65               fie
c5f30 6c 64 20 77 69 64 74 68 20 77 61 73 20 6e 65 67  ld width was neg
c5f40 61 74 69 76 65 2e 0d 0a 20 20 20 20 2a 2a 20 20  ative...    **  
c5f50 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 20 20   flag_zeropad   
c5f60 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
c5f70 45 20 69 66 20 74 68 65 20 77 69 64 74 68 20 62  E if the width b
c5f80 65 67 61 6e 20 77 69 74 68 20 30 2e 0d 0a 20 20  egan with 0...  
c5f90 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67    **   flag_long
c5fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5fb0 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c     TRUE if the l
c5fc0 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 20  etter 'l' (ell) 
c5fd0 70 72 65 66 69 78 65 64 0d 0a 20 20 20 20 2a 2a  prefixed..    **
c5fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
c6000 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68  he conversion ch
c6010 61 72 61 63 74 65 72 2e 0d 0a 20 20 20 20 2a 2a  aracter...    **
c6020 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67     flag_longlong
c6030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
c6040 52 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65  RUE if the lette
c6050 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29  r 'll' (ell ell)
c6060 20 70 72 65 66 69 78 65 64 0d 0a 20 20 20 20 2a   prefixed..    *
c6070 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
c6080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6090 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63  the conversion c
c60a0 68 61 72 61 63 74 65 72 2e 0d 0a 20 20 20 20 2a  haracter...    *
c60b0 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  *   flag_blanksi
c60c0 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  gn              
c60d0 54 52 55 45 20 69 66 20 61 20 27 20 27 20 69 73  TRUE if a ' ' is
c60e0 20 70 72 65 73 65 6e 74 2e 0d 0a 20 20 20 20 2a   present...    *
c60f0 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 20 20  *   width       
c6100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6110 54 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  The specified fi
c6120 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 73  eld width.  This
c6130 20 69 73 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20   is..    **     
c6140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6150 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 73            always
c6160 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20   non-negative.  
c6170 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 61  Zero is the defa
c6180 75 6c 74 2e 0d 0a 20 20 20 20 2a 2a 20 20 20 70  ult...    **   p
c6190 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20  recision        
c61a0 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73             The s
c61b0 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69  pecified precisi
c61c0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
c61d0 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
c61e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c61f0 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0d 0a 20         is -1... 
c6200 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20     **   xtype   
c6210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6220 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66      The class of
c6230 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e   the conversion.
c6240 0d 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70  ..    **   infop
c6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6260 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74         Pointer t
c6270 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
c6280 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0d 0a  e info struct...
c6290 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 77 69 74      */..    swit
c62a0 63 68 28 20 78 74 79 70 65 20 29 7b 0d 0a 20 20  ch( xtype ){..  
c62b0 20 20 20 20 63 61 73 65 20 65 74 50 4f 49 4e 54      case etPOINT
c62c0 45 52 3a 0d 0a 20 20 20 20 20 20 20 20 66 6c 61  ER:..        fla
c62d0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a  g_longlong = siz
c62e0 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65  eof(char*)==size
c62f0 6f 66 28 69 36 34 29 3b 0d 0a 20 20 20 20 20 20  of(i64);..      
c6300 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 73 69    flag_long = si
c6310 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a  zeof(char*)==siz
c6320 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b 0d 0a  eof(long int);..
c6330 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
c6340 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65  through into the
c6350 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0d 0a 20   next case */.. 
c6360 20 20 20 20 20 63 61 73 65 20 65 74 4f 52 44 49       case etORDI
c6370 4e 41 4c 3a 0d 0a 20 20 20 20 20 20 63 61 73 65  NAL:..      case
c6380 20 65 74 52 41 44 49 58 3a 0d 0a 20 20 20 20 20   etRADIX:..     
c6390 20 20 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c     if( infop->fl
c63a0 61 67 73 20 26 20 46 4c 41 47 5f 53 49 47 4e 45  ags & FLAG_SIGNE
c63b0 44 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  D ){..          
c63c0 69 36 34 20 76 3b 0d 0a 20 20 20 20 20 20 20 20  i64 v;..        
c63d0 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c    if( flag_longl
c63e0 6f 6e 67 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ong ){..        
c63f0 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61      v = va_arg(a
c6400 70 2c 69 36 34 29 3b 0d 0a 20 20 20 20 20 20 20  p,i64);..       
c6410 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61     }else if( fla
c6420 67 5f 6c 6f 6e 67 20 29 7b 0d 0a 20 20 20 20 20  g_long ){..     
c6430 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72         v = va_ar
c6440 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0d  g(ap,long int);.
c6450 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
c6460 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 76  {..            v
c6470 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
c6480 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d  );..          }.
c6490 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76  .          if( v
c64a0 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  <0 ){..         
c64b0 20 20 20 69 66 28 20 76 3d 3d 53 4d 41 4c 4c 45     if( v==SMALLE
c64c0 53 54 5f 49 4e 54 36 34 20 29 7b 0d 0a 20 20 20  ST_INT64 ){..   
c64d0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76             longv
c64e0 61 6c 75 65 20 3d 20 28 28 75 36 34 29 31 29 3c  alue = ((u64)1)<
c64f0 3c 36 33 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  <63;..          
c6500 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
c6510 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75          longvalu
c6520 65 20 3d 20 2d 76 3b 0d 0a 20 20 20 20 20 20 20  e = -v;..       
c6530 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
c6540 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27      prefix = '-'
c6550 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  ;..          }el
c6560 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  se{..           
c6570 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0d   longvalue = v;.
c6580 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
c6590 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29   flag_plussign )
c65a0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
c65b0 20 27 2b 27 3b 0d 0a 20 20 20 20 20 20 20 20 20   '+';..         
c65c0 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67     else if( flag
c65d0 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 72  _blanksign )  pr
c65e0 65 66 69 78 20 3d 20 27 20 27 3b 0d 0a 20 20 20  efix = ' ';..   
c65f0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20           else   
c6600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6610 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0d      prefix = 0;.
c6620 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
c6630 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
c6640 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
c6650 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0d 0a 20 20  _longlong ){..  
c6660 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
c6670 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
c6680 75 36 34 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  u64);..         
c6690 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f   }else if( flag_
c66a0 6c 6f 6e 67 20 29 7b 0d 0a 20 20 20 20 20 20 20  long ){..       
c66b0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
c66c0 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67   va_arg(ap,unsig
c66d0 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0d 0a  ned long int);..
c66e0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
c66f0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f  ..            lo
c6700 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  ngvalue = va_arg
c6710 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74  (ap,unsigned int
c6720 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d  );..          }.
c6730 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69  .          prefi
c6740 78 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  x = 0;..        
c6750 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c  }..        if( l
c6760 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c  ongvalue==0 ) fl
c6770 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
c6780 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 69   = 0;..        i
c6790 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  f( flag_zeropad 
c67a0 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 69 64  && precision<wid
c67b0 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 20 29  th-(prefix!=0) )
c67c0 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 72 65  {..          pre
c67d0 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d 28  cision = width-(
c67e0 70 72 65 66 69 78 21 3d 30 29 3b 0d 0a 20 20 20  prefix!=0);..   
c67f0 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
c6800 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 65 74  if( precision<et
c6810 42 55 46 53 49 5a 45 2d 31 30 20 29 7b 0d 0a 20  BUFSIZE-10 ){.. 
c6820 20 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20           nOut = 
c6830 65 74 42 55 46 53 49 5a 45 3b 0d 0a 20 20 20 20  etBUFSIZE;..    
c6840 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 62 75 66        zOut = buf
c6850 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
c6860 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 75  {..          nOu
c6870 74 20 3d 20 70 72 65 63 69 73 69 6f 6e 20 2b 20  t = precision + 
c6880 31 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a  10;..          z
c6890 4f 75 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73  Out = zExtra = s
c68a0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 4f  qlite3Malloc( nO
c68b0 75 74 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  ut );..         
c68c0 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0d   if( zOut==0 ){.
c68d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 41 63  .            pAc
c68e0 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  cum->mallocFaile
c68f0 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  d = 1;..        
c6900 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20      return;..   
c6910 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
c6920 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 75 66    }..        buf
c6930 70 74 20 3d 20 26 7a 4f 75 74 5b 6e 4f 75 74 2d  pt = &zOut[nOut-
c6940 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  1];..        if(
c6950 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41   xtype==etORDINA
c6960 4c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  L ){..          
c6970 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
c6980 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74  r zOrd[] = "thst
c6990 6e 64 72 64 22 3b 0d 0a 20 20 20 20 20 20 20 20  ndrd";..        
c69a0 20 20 69 6e 74 20 78 20 3d 20 28 69 6e 74 29 28    int x = (int)(
c69b0 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 30 29 3b  longvalue % 10);
c69c0 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
c69d0 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c  x>=4 || (longval
c69e0 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 7b 0d  ue/10)%10==1 ){.
c69f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 3d  .            x =
c6a00 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d   0;..          }
c6a10 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d  ..          *(--
c6a20 62 75 66 70 74 29 20 3d 20 7a 4f 72 64 5b 78 2a  bufpt) = zOrd[x*
c6a30 32 2b 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20  2+1];..         
c6a40 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 7a 4f   *(--bufpt) = zO
c6a50 72 64 5b 78 2a 32 5d 3b 0d 0a 20 20 20 20 20 20  rd[x*2];..      
c6a60 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7b 0d 0a    }..        {..
c6a70 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74            regist
c6a80 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  er const char *c
c6a90 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 65  set;      /* Use
c6aa0 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73   registers for s
c6ab0 70 65 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20 20  peed */..       
c6ac0 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20     register int 
c6ad0 62 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20 20  base;..         
c6ae0 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73   cset = &aDigits
c6af0 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
c6b00 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 61 73  ;..          bas
c6b10 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b  e = infop->base;
c6b20 0d 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20  ..          do{ 
c6b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6b50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
c6b60 76 65 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f  vert to ascii */
c6b70 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  ..            *(
c6b80 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 65 74 5b  --bufpt) = cset[
c6b90 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 65 5d 3b  longvalue%base];
c6ba0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f  ..            lo
c6bb0 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 61  ngvalue = longva
c6bc0 6c 75 65 2f 62 61 73 65 3b 0d 0a 20 20 20 20 20  lue/base;..     
c6bd0 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f 6e       }while( lon
c6be0 67 76 61 6c 75 65 3e 30 20 29 3b 0d 0a 20 20 20  gvalue>0 );..   
c6bf0 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
c6c00 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 26  length = (int)(&
c6c10 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d 2d 62 75 66  zOut[nOut-1]-buf
c6c20 70 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f  pt);..        fo
c6c30 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d  r(idx=precision-
c6c40 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69  length; idx>0; i
c6c50 64 78 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 20 20  dx--){..        
c6c60 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27    *(--bufpt) = '
c6c70 30 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  0';             
c6c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6c90 2f 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0d 0a  /* Zero pad */..
c6ca0 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
c6cb0 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 20     if( prefix ) 
c6cc0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 70 72 65  *(--bufpt) = pre
c6cd0 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
c6ce0 20 20 20 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a     /* Add sign *
c6cf0 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  /..        if( f
c6d00 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
c6d10 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 72 65 66  m && infop->pref
c6d20 69 78 20 29 7b 20 20 20 20 20 20 2f 2a 20 41 64  ix ){      /* Ad
c6d30 64 20 22 30 22 20 6f 72 20 22 30 78 22 20 2a 2f  d "0" or "0x" */
c6d40 0d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  ..          cons
c6d50 74 20 63 68 61 72 20 2a 70 72 65 3b 0d 0a 20 20  t char *pre;..  
c6d60 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b 0d          char x;.
c6d70 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d  .          pre =
c6d80 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d   &aPrefix[infop-
c6d90 3e 70 72 65 66 69 78 5d 3b 0d 0a 20 20 20 20 20  >prefix];..     
c6da0 20 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a       for(; (x=(*
c6db0 70 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29  pre))!=0; pre++)
c6dc0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b   *(--bufpt) = x;
c6dd0 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
c6de0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69       length = (i
c6df0 6e 74 29 28 26 7a 4f 75 74 5b 6e 4f 75 74 2d 31  nt)(&zOut[nOut-1
c6e00 5d 2d 62 75 66 70 74 29 3b 0d 0a 20 20 20 20 20  ]-bufpt);..     
c6e10 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
c6e20 20 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0d 0a   case etFLOAT:..
c6e30 20 20 20 20 20 20 63 61 73 65 20 65 74 45 58 50        case etEXP
c6e40 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  :..      case et
c6e50 47 45 4e 45 52 49 43 3a 0d 0a 20 20 20 20 20 20  GENERIC:..      
c6e60 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61    realvalue = va
c6e70 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b  _arg(ap,double);
c6e80 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
c6e90 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
c6ea0 49 4e 54 0d 0a 20 20 20 20 20 20 20 20 6c 65 6e  INT..        len
c6eb0 67 74 68 20 3d 20 30 3b 0d 0a 23 65 6c 73 65 0d  gth = 0;..#else.
c6ec0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
c6ed0 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69  cision<0 ) preci
c6ee0 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20  sion = 6;       
c6ef0 20 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74    /* Set default
c6f00 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0d 0a 20   precision */.. 
c6f10 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
c6f20 61 6c 75 65 3c 30 2e 30 20 29 7b 0d 0a 20 20 20  alue<0.0 ){..   
c6f30 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
c6f40 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0d 0a   = -realvalue;..
c6f50 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78            prefix
c6f60 20 3d 20 27 2d 27 3b 0d 0a 20 20 20 20 20 20 20   = '-';..       
c6f70 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
c6f80 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73     if( flag_plus
c6f90 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 20 20  sign )          
c6fa0 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0d 0a 20  prefix = '+';.. 
c6fb0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66           else if
c6fc0 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e  ( flag_blanksign
c6fd0 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 27   )    prefix = '
c6fe0 20 27 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 65   ';..          e
c6ff0 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
c7000 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66              pref
c7010 69 78 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  ix = 0;..       
c7020 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
c7030 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43  xtype==etGENERIC
c7040 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20   && precision>0 
c7050 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0d 0a  ) precision--;..
c7060 23 69 66 20 30 0d 0a 20 20 20 20 20 20 20 20 2f  #if 0..        /
c7070 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73  * Rounding works
c7080 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74   like BSD when t
c7090 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39  he constant 0.49
c70a0 39 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 65  99 is used.  Wie
c70b0 72 64 21 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  rd! */..        
c70c0 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f  for(idx=precisio
c70d0 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39  n, rounder=0.499
c70e0 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c  9; idx>0; idx--,
c70f0 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0d   rounder*=0.1);.
c7100 0a 23 65 6c 73 65 0d 0a 20 20 20 20 20 20 20 20  .#else..        
c7110 2f 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65  /* It makes more
c7120 20 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e   sense to use 0.
c7130 35 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 66 6f  5 */..        fo
c7140 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c  r(idx=precision,
c7150 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64   rounder=0.5; id
c7160 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e  x>0; idx--, roun
c7170 64 65 72 2a 3d 30 2e 31 29 7b 7d 0d 0a 23 65 6e  der*=0.1){}..#en
c7180 64 69 66 0d 0a 20 20 20 20 20 20 20 20 69 66 28  dif..        if(
c7190 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54 20   xtype==etFLOAT 
c71a0 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72  ) realvalue += r
c71b0 6f 75 6e 64 65 72 3b 0d 0a 20 20 20 20 20 20 20  ounder;..       
c71c0 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65   /* Normalize re
c71d0 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69  alvalue to withi
c71e0 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c  n 10.0 > realval
c71f0 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0d 0a 20 20  ue >= 1.0 */..  
c7200 20 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0d 0a        exp = 0;..
c7210 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
c7220 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65  te3IsNaN((double
c7230 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0d 0a  )realvalue) ){..
c7240 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
c7250 3d 20 22 4e 61 4e 22 3b 0d 0a 20 20 20 20 20 20  = "NaN";..      
c7260 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0d      length = 3;.
c7270 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
c7280 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
c7290 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 61        if( realva
c72a0 6c 75 65 3e 30 2e 30 20 29 7b 0d 0a 20 20 20 20  lue>0.0 ){..    
c72b0 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
c72c0 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26 26 20  lvalue>=1e32 && 
c72d0 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c  exp<=350 ){ real
c72e0 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20  value *= 1e-32; 
c72f0 65 78 70 2b 3d 33 32 3b 20 7d 0d 0a 20 20 20 20  exp+=32; }..    
c7300 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
c7310 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20 65  lvalue>=1e8 && e
c7320 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76  xp<=350 ){ realv
c7330 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65 78  alue *= 1e-8; ex
c7340 70 2b 3d 38 3b 20 7d 0d 0a 20 20 20 20 20 20 20  p+=8; }..       
c7350 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
c7360 6c 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70  lue>=10.0 && exp
c7370 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c  <=350 ){ realval
c7380 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b  ue *= 0.1; exp++
c7390 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 77  ; }..          w
c73a0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c  hile( realvalue<
c73b0 31 65 2d 38 20 29 7b 20 72 65 61 6c 76 61 6c 75  1e-8 ){ realvalu
c73c0 65 20 2a 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38  e *= 1e8; exp-=8
c73d0 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 77  ; }..          w
c73e0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c  hile( realvalue<
c73f0 31 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  1.0 ){ realvalue
c7400 20 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d 3b   *= 10.0; exp--;
c7410 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66   }..          if
c7420 28 20 65 78 70 3e 33 35 30 20 29 7b 0d 0a 20 20  ( exp>350 ){..  
c7430 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 72            if( pr
c7440 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0d 0a 20 20  efix=='-' ){..  
c7450 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
c7460 74 20 3d 20 22 2d 49 6e 66 22 3b 0d 0a 20 20 20  t = "-Inf";..   
c7470 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
c7480 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29  f( prefix=='+' )
c7490 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  {..             
c74a0 20 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b   bufpt = "+Inf";
c74b0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ..            }e
c74c0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
c74d0 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e 66      bufpt = "Inf
c74e0 22 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ";..            
c74f0 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  }..            l
c7500 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53  ength = sqlite3S
c7510 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0d  trlen30(bufpt);.
c7520 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
c7530 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  ak;..          }
c7540 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
c7550 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
c7560 3b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 0d 0a 20  ;..        /*.. 
c7570 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65         ** If the
c7580 20 66 69 65 6c 64 20 74 79 70 65 20 69 73 20 65   field type is e
c7590 74 47 45 4e 45 52 49 43 2c 20 74 68 65 6e 20 63  tGENERIC, then c
c75a0 6f 6e 76 65 72 74 20 74 6f 20 65 69 74 68 65 72  onvert to either
c75b0 20 65 74 45 58 50 0d 0a 20 20 20 20 20 20 20 20   etEXP..        
c75c0 2a 2a 20 6f 72 20 65 74 46 4c 4f 41 54 2c 20 61  ** or etFLOAT, a
c75d0 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0d 0a  s appropriate...
c75e0 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20          */..    
c75f0 20 20 20 20 69 66 28 20 78 74 79 70 65 21 3d 65      if( xtype!=e
c7600 74 46 4c 4f 41 54 20 29 7b 0d 0a 20 20 20 20 20  tFLOAT ){..     
c7610 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20 2b       realvalue +
c7620 3d 20 72 6f 75 6e 64 65 72 3b 0d 0a 20 20 20 20  = rounder;..    
c7630 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 61        if( realva
c7640 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65 61  lue>=10.0 ){ rea
c7650 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 65  lvalue *= 0.1; e
c7660 78 70 2b 2b 3b 20 7d 0d 0a 20 20 20 20 20 20 20  xp++; }..       
c7670 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
c7680 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43  xtype==etGENERIC
c7690 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66   ){..          f
c76a0 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f  lag_rtz = !flag_
c76b0 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0d 0a  alternateform;..
c76c0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
c76d0 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63  p<-4 || exp>prec
c76e0 69 73 69 6f 6e 20 29 7b 0d 0a 20 20 20 20 20 20  ision ){..      
c76f0 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 74        xtype = et
c7700 45 58 50 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  EXP;..          
c7710 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
c7720 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
c7730 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b  precision - exp;
c7740 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74  ..            xt
c7750 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0d 0a  ype = etFLOAT;..
c7760 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
c7770 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
c7780 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20         flag_rtz 
c7790 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  = 0;..        }.
c77a0 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
c77b0 70 65 3d 3d 65 74 45 58 50 20 29 7b 0d 0a 20 20  pe==etEXP ){..  
c77c0 20 20 20 20 20 20 20 20 65 32 20 3d 20 30 3b 0d          e2 = 0;.
c77d0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
c77e0 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20  .          e2 = 
c77f0 65 78 70 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  exp;..        }.
c7800 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 32 2b  .        if( e2+
c7810 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74 68 20  precision+width 
c7820 3e 20 65 74 42 55 46 53 49 5a 45 20 2d 20 31 35  > etBUFSIZE - 15
c7830 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 62   ){..          b
c7840 75 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20  ufpt = zExtra = 
c7850 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 65  sqlite3Malloc( e
c7860 32 2b 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74  2+precision+widt
c7870 68 2b 31 35 20 29 3b 0d 0a 20 20 20 20 20 20 20  h+15 );..       
c7880 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20     if( bufpt==0 
c7890 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
c78a0 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61  pAccum->mallocFa
c78b0 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20  iled = 1;..     
c78c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a         return;..
c78d0 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
c78e0 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
c78f0 7a 4f 75 74 20 3d 20 62 75 66 70 74 3b 0d 0a 20  zOut = bufpt;.. 
c7900 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b 0d         nsd = 0;.
c7910 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70  .        flag_dp
c7920 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20   = (precision>0 
c7930 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74  ?1:0) | flag_alt
c7940 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61  ernateform | fla
c7950 67 5f 61 6c 74 66 6f 72 6d 32 3b 0d 0a 20 20 20  g_altform2;..   
c7960 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e       /* The sign
c7970 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65   in front of the
c7980 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20 20   number */..    
c7990 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29      if( prefix )
c79a0 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  {..          *(b
c79b0 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78  ufpt++) = prefix
c79c0 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
c79d0 20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20        /* Digits 
c79e0 70 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63  prior to the dec
c79f0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0d 0a 20  imal point */.. 
c7a00 20 20 20 20 20 20 20 69 66 28 20 65 32 3c 30 20         if( e2<0 
c7a10 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 28  ){..          *(
c7a20 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0d  bufpt++) = '0';.
c7a30 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
c7a40 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b  .          for(;
c7a50 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b 0d 0a   e2>=0; e2--){..
c7a60 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75              *(bu
c7a70 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64  fpt++) = et_getd
c7a80 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c  igit(&realvalue,
c7a90 26 6e 73 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  &nsd);..        
c7aa0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a    }..        }..
c7ab0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
c7ac0 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0d  ecimal point */.
c7ad0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
c7ae0 67 5f 64 70 20 29 7b 0d 0a 20 20 20 20 20 20 20  g_dp ){..       
c7af0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
c7b00 27 2e 27 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  '.';..        }.
c7b10 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20  .        /* "0" 
c7b20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
c7b30 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62   decimal point b
c7b40 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  ut before the fi
c7b50 72 73 74 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  rst..        ** 
c7b60 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
c7b70 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
c7b80 2a 2f 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28  */..        for(
c7b90 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63  e2++; e2<0; prec
c7ba0 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0d  ision--, e2++){.
c7bb0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
c7bc0 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29  t( precision>0 )
c7bd0 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  ;..          *(b
c7be0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0d 0a  ufpt++) = '0';..
c7bf0 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
c7c00 20 20 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e     /* Significan
c7c10 74 20 64 69 67 69 74 73 20 61 66 74 65 72 20 74  t digits after t
c7c20 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
c7c30 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 77 68 69   */..        whi
c7c40 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d  le( (precision--
c7c50 29 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  )>0 ){..        
c7c60 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65    *(bufpt++) = e
c7c70 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c  t_getdigit(&real
c7c80 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0d 0a 20 20  value,&nsd);..  
c7c90 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
c7ca0 20 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c   /* Remove trail
c7cb0 69 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68  ing zeros and th
c7cc0 65 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69  e "." if no digi
c7cd0 74 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e  ts follow the ".
c7ce0 22 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66  " */..        if
c7cf0 28 20 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c  ( flag_rtz && fl
c7d00 61 67 5f 64 70 20 29 7b 0d 0a 20 20 20 20 20 20  ag_dp ){..      
c7d10 20 20 20 20 77 68 69 6c 65 28 20 62 75 66 70 74      while( bufpt
c7d20 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d  [-1]=='0' ) *(--
c7d30 62 75 66 70 74 29 20 3d 20 30 3b 0d 0a 20 20 20  bufpt) = 0;..   
c7d40 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 62         assert( b
c7d50 75 66 70 74 3e 7a 4f 75 74 20 29 3b 0d 0a 20 20  ufpt>zOut );..  
c7d60 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
c7d70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0d 0a 20  t[-1]=='.' ){.. 
c7d80 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66             if( f
c7d90 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0d  lag_altform2 ){.
c7da0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
c7db0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b  (bufpt++) = '0';
c7dc0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ..            }e
c7dd0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
c7de0 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d      *(--bufpt) =
c7df0 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   0;..           
c7e00 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d   }..          }.
c7e10 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
c7e20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 22      /* Add the "
c7e30 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a 2f 0d  eNNN" suffix */.
c7e40 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
c7e50 70 65 3d 3d 65 74 45 58 50 20 29 7b 0d 0a 20 20  pe==etEXP ){..  
c7e60 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
c7e70 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69 6e 66  +) = aDigits[inf
c7e80 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0d 0a 20  op->charset];.. 
c7e90 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70           if( exp
c7ea0 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  <0 ){..         
c7eb0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
c7ec0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b  '-'; exp = -exp;
c7ed0 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ..          }els
c7ee0 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e{..            
c7ef0 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27  *(bufpt++) = '+'
c7f00 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
c7f10 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
c7f20 70 3e 3d 31 30 30 20 29 7b 0d 0a 20 20 20 20 20  p>=100 ){..     
c7f30 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
c7f40 29 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f  ) = (char)((exp/
c7f50 31 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20  100)+'0');      
c7f60 20 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74    /* 100's digit
c7f70 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20   */..           
c7f80 20 65 78 70 20 25 3d 20 31 30 30 3b 0d 0a 20 20   exp %= 100;..  
c7f90 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
c7fa0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
c7fb0 3d 20 28 63 68 61 72 29 28 65 78 70 2f 31 30 2b  = (char)(exp/10+
c7fc0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
c7fd0 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69 74 20    /* 10's digit 
c7fe0 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 28  */..          *(
c7ff0 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72  bufpt++) = (char
c8000 29 28 65 78 70 25 31 30 2b 27 30 27 29 3b 20 20  )(exp%10+'0');  
c8010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 27             /* 1'
c8020 73 20 64 69 67 69 74 20 2a 2f 0d 0a 20 20 20 20  s digit */..    
c8030 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 2a      }..        *
c8040 62 75 66 70 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20  bufpt = 0;....  
c8050 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
c8060 76 65 72 74 65 64 20 6e 75 6d 62 65 72 20 69 73  verted number is
c8070 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64 20 7a 65   in buf[] and ze
c8080 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 4f  ro terminated. O
c8090 75 74 70 75 74 20 69 74 2e 0d 0a 20 20 20 20 20  utput it...     
c80a0 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20     ** Note that 
c80b0 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 69 6e  the number is in
c80c0 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64 65 72   the usual order
c80d0 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64 20 61  , not reversed a
c80e0 73 20 77 69 74 68 0d 0a 20 20 20 20 20 20 20 20  s with..        
c80f0 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 65  ** integer conve
c8100 72 73 69 6f 6e 73 2e 20 2a 2f 0d 0a 20 20 20 20  rsions. */..    
c8110 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
c8120 74 29 28 62 75 66 70 74 2d 7a 4f 75 74 29 3b 0d  t)(bufpt-zOut);.
c8130 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
c8140 20 7a 4f 75 74 3b 0d 0a 0d 0a 20 20 20 20 20 20   zOut;....      
c8150 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
c8160 65 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20  e:  Add leading 
c8170 7a 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61  zeros if the fla
c8180 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69  g_zeropad flag i
c8190 73 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65  s..        ** se
c81a0 74 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74  t and we are not
c81b0 20 6c 65 66 74 20 6a 75 73 74 69 66 69 65 64 20   left justified 
c81c0 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  */..        if( 
c81d0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20  flag_zeropad && 
c81e0 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66  !flag_leftjustif
c81f0 79 20 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 69  y && length < wi
c8200 64 74 68 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  dth){..         
c8210 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20 20   int i;..       
c8220 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77 69     int nPad = wi
c8230 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0d 0a 20  dth - length;.. 
c8240 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77           for(i=w
c8250 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69  idth; i>=nPad; i
c8260 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  --){..          
c8270 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66    bufpt[i] = buf
c8280 70 74 5b 69 2d 6e 50 61 64 5d 3b 0d 0a 20 20 20  pt[i-nPad];..   
c8290 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
c82a0 20 20 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d      i = prefix!=
c82b0 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 77 68  0;..          wh
c82c0 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20 62 75  ile( nPad-- ) bu
c82d0 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0d  fpt[i++] = '0';.
c82e0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
c82f0 68 20 3d 20 77 69 64 74 68 3b 0d 0a 20 20 20 20  h = width;..    
c8300 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a      }..#endif /*
c8310 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
c8320 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
c8330 4f 49 4e 54 29 20 2a 2f 0d 0a 20 20 20 20 20 20  OINT) */..      
c8340 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
c8350 63 61 73 65 20 65 74 53 49 5a 45 3a 0d 0a 20 20  case etSIZE:..  
c8360 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28 61        *(va_arg(a
c8370 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 75  p,int*)) = pAccu
c8380 6d 2d 3e 6e 43 68 61 72 3b 0d 0a 20 20 20 20 20  m->nChar;..     
c8390 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
c83a0 68 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  h = 0;..        
c83b0 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
c83c0 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0d 0a 20  se etPERCENT:.. 
c83d0 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20         buf[0] = 
c83e0 27 25 27 3b 0d 0a 20 20 20 20 20 20 20 20 62 75  '%';..        bu
c83f0 66 70 74 20 3d 20 62 75 66 3b 0d 0a 20 20 20 20  fpt = buf;..    
c8400 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0d      length = 1;.
c8410 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
c8420 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 43 48  .      case etCH
c8430 41 52 58 3a 0d 0a 20 20 20 20 20 20 20 20 63 20  ARX:..        c 
c8440 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
c8450 3b 0d 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30  ;..        buf[0
c8460 5d 20 3d 20 28 63 68 61 72 29 63 3b 0d 0a 20 20  ] = (char)c;..  
c8470 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
c8480 69 6f 6e 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ion>=0 ){..     
c8490 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20       for(idx=1; 
c84a0 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69  idx<precision; i
c84b0 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d  dx++) buf[idx] =
c84c0 20 28 63 68 61 72 29 63 3b 0d 0a 20 20 20 20 20   (char)c;..     
c84d0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 72       length = pr
c84e0 65 63 69 73 69 6f 6e 3b 0d 0a 20 20 20 20 20 20  ecision;..      
c84f0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
c8500 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b 0d 0a      length =1;..
c8510 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
c8520 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0d     bufpt = buf;.
c8530 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
c8540 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 54  .      case etST
c8550 52 49 4e 47 3a 0d 0a 20 20 20 20 20 20 63 61 73  RING:..      cas
c8560 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0d 0a  e etDYNSTRING:..
c8570 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
c8580 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29  va_arg(ap,char*)
c8590 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 62  ;..        if( b
c85a0 75 66 70 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ufpt==0 ){..    
c85b0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22        bufpt = ""
c85c0 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
c85d0 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59   if( xtype==etDY
c85e0 4e 53 54 52 49 4e 47 20 29 7b 0d 0a 20 20 20 20  NSTRING ){..    
c85f0 20 20 20 20 20 20 7a 45 78 74 72 61 20 3d 20 62        zExtra = b
c8600 75 66 70 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d  ufpt;..        }
c8610 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  ..        if( pr
c8620 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0d 0a 20  ecision>=0 ){.. 
c8630 20 20 20 20 20 20 20 20 20 66 6f 72 28 6c 65 6e           for(len
c8640 67 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72  gth=0; length<pr
c8650 65 63 69 73 69 6f 6e 20 26 26 20 62 75 66 70 74  ecision && bufpt
c8660 5b 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68  [length]; length
c8670 2b 2b 29 7b 7d 0d 0a 20 20 20 20 20 20 20 20 7d  ++){}..        }
c8680 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
c8690 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65   length = sqlite
c86a0 33 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29  3Strlen30(bufpt)
c86b0 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
c86c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
c86d0 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
c86e0 43 41 50 45 3a 0d 0a 20 20 20 20 20 20 63 61 73  CAPE:..      cas
c86f0 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0d  e etSQLESCAPE2:.
c8700 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51  .      case etSQ
c8710 4c 45 53 43 41 50 45 33 3a 20 7b 0d 0a 20 20 20  LESCAPE3: {..   
c8720 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b       int i, j, k
c8730 2c 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0d 0a 20 20  , n, isnull;..  
c8740 20 20 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75        int needQu
c8750 6f 74 65 3b 0d 0a 20 20 20 20 20 20 20 20 63 68  ote;..        ch
c8760 61 72 20 63 68 3b 0d 0a 20 20 20 20 20 20 20 20  ar ch;..        
c8770 63 68 61 72 20 71 20 3d 20 28 28 78 74 79 70 65  char q = ((xtype
c8780 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33 29 3f  ==etSQLESCAPE3)?
c8790 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f 2a 20  '"':'\'');   /* 
c87a0 51 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20  Quote character 
c87b0 2a 2f 0d 0a 20 20 20 20 20 20 20 20 63 68 61 72  */..        char
c87c0 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61 72   *escarg = va_ar
c87d0 67 28 61 70 2c 63 68 61 72 2a 29 3b 0d 0a 20 20  g(ap,char*);..  
c87e0 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65        isnull = e
c87f0 73 63 61 72 67 3d 3d 30 3b 0d 0a 20 20 20 20 20  scarg==0;..     
c8800 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20     if( isnull ) 
c8810 65 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d  escarg = (xtype=
c8820 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20  =etSQLESCAPE2 ? 
c8830 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29  "NULL" : "(NULL)
c8840 22 29 3b 0d 0a 20 20 20 20 20 20 20 20 6b 20 3d  ");..        k =
c8850 20 70 72 65 63 69 73 69 6f 6e 3b 0d 0a 20 20 20   precision;..   
c8860 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20       for(i=n=0; 
c8870 6b 21 3d 30 20 26 26 20 28 63 68 3d 65 73 63 61  k!=0 && (ch=esca
c8880 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 2c 20  rg[i])!=0; i++, 
c8890 6b 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  k--){..         
c88a0 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b   if( ch==q )  n+
c88b0 2b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  +;..        }.. 
c88c0 20 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65         needQuote
c88d0 20 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74   = !isnull && xt
c88e0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
c88f0 32 3b 0d 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d  2;..        n +=
c8900 20 69 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f   i + 1 + needQuo
c8910 74 65 2a 32 3b 0d 0a 20 20 20 20 20 20 20 20 69  te*2;..        i
c8920 66 28 20 6e 3e 65 74 42 55 46 53 49 5a 45 20 29  f( n>etBUFSIZE )
c8930 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  {..          buf
c8940 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73 71  pt = zExtra = sq
c8950 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29  lite3Malloc( n )
c8960 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
c8970 20 62 75 66 70 74 3d 3d 30 20 29 7b 0d 0a 20 20   bufpt==0 ){..  
c8980 20 20 20 20 20 20 20 20 20 20 70 41 63 63 75 6d            pAccum
c8990 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
c89a0 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   1;..           
c89b0 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20   return;..      
c89c0 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
c89d0 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
c89e0 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0d 0a 20   bufpt = buf;.. 
c89f0 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
c8a00 20 20 6a 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20    j = 0;..      
c8a10 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20    if( needQuote 
c8a20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71  ) bufpt[j++] = q
c8a30 3b 0d 0a 20 20 20 20 20 20 20 20 6b 20 3d 20 69  ;..        k = i
c8a40 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
c8a50 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0d 0a  =0; i<k; i++){..
c8a60 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b            bufpt[
c8a70 6a 2b 2b 5d 20 3d 20 63 68 20 3d 20 65 73 63 61  j++] = ch = esca
c8a80 72 67 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 20 20  rg[i];..        
c8a90 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75    if( ch==q ) bu
c8aa0 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0d 0a  fpt[j++] = ch;..
c8ab0 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
c8ac0 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65     if( needQuote
c8ad0 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20   ) bufpt[j++] = 
c8ae0 71 3b 0d 0a 20 20 20 20 20 20 20 20 62 75 66 70  q;..        bufp
c8af0 74 5b 6a 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 20  t[j] = 0;..     
c8b00 20 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0d 0a     length = j;..
c8b10 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
c8b20 72 65 63 69 73 69 6f 6e 20 69 6e 20 25 71 20 61  recision in %q a
c8b30 6e 64 20 25 51 20 6d 65 61 6e 73 20 68 6f 77 20  nd %Q means how 
c8b40 6d 61 6e 79 20 69 6e 70 75 74 20 63 68 61 72 61  many input chara
c8b50 63 74 65 72 73 20 74 6f 0d 0a 20 20 20 20 20 20  cters to..      
c8b60 20 20 2a 2a 20 63 6f 6e 73 75 6d 65 2c 20 6e 6f    ** consume, no
c8b70 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  t the length of 
c8b80 74 68 65 20 6f 75 74 70 75 74 2e 2e 2e 0d 0a 20  the output..... 
c8b90 20 20 20 20 20 20 20 2a 2a 20 69 66 28 20 70 72         ** if( pr
c8ba0 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72  ecision>=0 && pr
c8bb0 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29  ecision<length )
c8bc0 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73   length = precis
c8bd0 69 6f 6e 3b 20 2a 2f 0d 0a 20 20 20 20 20 20 20  ion; */..       
c8be0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
c8bf0 0d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54  ..      case etT
c8c00 4f 4b 45 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 20  OKEN: {..       
c8c10 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d   Token *pToken =
c8c20 20 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65   va_arg(ap, Toke
c8c30 6e 2a 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  n*);..        if
c8c40 28 20 70 54 6f 6b 65 6e 20 29 7b 0d 0a 20 20 20  ( pToken ){..   
c8c50 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
c8c60 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
c8c70 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  cum, (const char
c8c80 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f  *)pToken->z, pTo
c8c90 6b 65 6e 2d 3e 6e 29 3b 0d 0a 20 20 20 20 20 20  ken->n);..      
c8ca0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 6c 65 6e    }..        len
c8cb0 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b  gth = width = 0;
c8cc0 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
c8cd0 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
c8ce0 20 63 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a   case etSRCLIST:
c8cf0 20 7b 0d 0a 20 20 20 20 20 20 20 20 53 72 63 4c   {..        SrcL
c8d00 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f 61  ist *pSrc = va_a
c8d10 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a 29  rg(ap, SrcList*)
c8d20 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b  ;..        int k
c8d30 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
c8d40 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 74 72  t);..        str
c8d50 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
c8d60 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d   *pItem = &pSrc-
c8d70 3e 61 5b 6b 5d 3b 0d 0a 20 20 20 20 20 20 20 20  >a[k];..        
c8d80 61 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20  assert( k>=0 && 
c8d90 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0d  k<pSrc->nSrc );.
c8da0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74  .        if( pIt
c8db0 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b  em->zDatabase ){
c8dc0 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
c8dd0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
c8de0 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d  d(pAccum, pItem-
c8df0 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 29 3b  >zDatabase, -1);
c8e00 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
c8e10 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
c8e20 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31  d(pAccum, ".", 1
c8e30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
c8e40 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
c8e50 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
c8e60 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  cum, pItem->zNam
c8e70 65 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20 20  e, -1);..       
c8e80 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20   length = width 
c8e90 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  = 0;..        br
c8ea0 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
c8eb0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d       default: {.
c8ec0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c8ed0 20 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49   xtype==etINVALI
c8ee0 44 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 65  D );..        re
c8ef0 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  turn;..      }..
c8f00 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77 69 74      }/* End swit
c8f10 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d  ch over the form
c8f20 61 74 20 74 79 70 65 20 2a 2f 0d 0a 20 20 20 20  at type */..    
c8f30 2f 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 74  /*..    ** The t
c8f40 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65  ext of the conve
c8f50 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64  rsion is pointed
c8f60 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61   to by "bufpt" a
c8f70 6e 64 20 69 73 0d 0a 20 20 20 20 2a 2a 20 22 6c  nd is..    ** "l
c8f80 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72  ength" character
c8f90 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65  s long.  The fie
c8fa0 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64  ld width is "wid
c8fb0 74 68 22 2e 20 20 44 6f 0d 0a 20 20 20 20 2a 2a  th".  Do..    **
c8fc0 20 74 68 65 20 6f 75 74 70 75 74 2e 0d 0a 20 20   the output...  
c8fd0 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 21 66    */..    if( !f
c8fe0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
c8ff0 29 7b 0d 0a 20 20 20 20 20 20 72 65 67 69 73 74  ){..      regist
c9000 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0d 0a  er int nspace;..
c9010 20 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77        nspace = w
c9020 69 64 74 68 2d 6c 65 6e 67 74 68 3b 0d 0a 20 20  idth-length;..  
c9030 20 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30      if( nspace>0
c9040 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
c9050 69 74 65 33 41 70 70 65 6e 64 53 70 61 63 65 28  ite3AppendSpace(
c9060 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b  pAccum, nspace);
c9070 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
c9080 0d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68  ..    if( length
c9090 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  >0 ){..      sql
c90a0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
c90b0 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74  nd(pAccum, bufpt
c90c0 2c 20 6c 65 6e 67 74 68 29 3b 0d 0a 20 20 20 20  , length);..    
c90d0 7d 0d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f  }..    if( flag_
c90e0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0d 0a  leftjustify ){..
c90f0 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69        register i
c9100 6e 74 20 6e 73 70 61 63 65 3b 0d 0a 20 20 20 20  nt nspace;..    
c9110 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68    nspace = width
c9120 2d 6c 65 6e 67 74 68 3b 0d 0a 20 20 20 20 20 20  -length;..      
c9130 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0d  if( nspace>0 ){.
c9140 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
c9150 41 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63  AppendSpace(pAcc
c9160 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0d 0a 20 20  um, nspace);..  
c9170 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
c9180 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
c9190 45 78 74 72 61 29 3b 0d 0a 20 20 7d 2f 2a 20 45  Extra);..  }/* E
c91a0 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65 72  nd for loop over
c91b0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
c91c0 6e 67 20 2a 2f 0d 0a 7d 20 2f 2a 20 45 6e 64 20  ng */..} /* End 
c91d0 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a  of function */..
c91e0 0d 0a 2f 2a 0d 0a 2a 2a 20 41 70 70 65 6e 64 20  ../*..** Append 
c91f0 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 74 20  N bytes of text 
c9200 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 53 74  from z to the St
c9210 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e 0d 0a  rAccum object...
c9220 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
c9230 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
c9240 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74  trAccumAppend(St
c9250 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74  rAccum *p, const
c9260 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29   char *z, int N)
c9270 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d  {..  assert( z!=
c9280 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0d 0a 20 20  0 || N==0 );..  
c9290 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20  if( p->tooBig | 
c92a0 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  p->mallocFailed 
c92b0 29 7b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65  ){..    testcase
c92c0 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0d 0a 20 20  (p->tooBig);..  
c92d0 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d 61    testcase(p->ma
c92e0 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0d 0a 20 20  llocFailed);..  
c92f0 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
c9300 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 54 65    assert( p->zTe
c9310 78 74 21 3d 30 20 7c 7c 20 70 2d 3e 6e 43 68 61  xt!=0 || p->nCha
c9320 72 3d 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 4e  r==0 );..  if( N
c9330 3c 30 20 29 7b 0d 0a 20 20 20 20 4e 20 3d 20 73  <0 ){..    N = s
c9340 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
c9350 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 4e  );..  }..  if( N
c9360 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d  ==0 || NEVER(z==
c9370 30 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  0) ){..    retur
c9380 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  n;..  }..  if( p
c9390 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e  ->nChar+N >= p->
c93a0 6e 41 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 63  nAlloc ){..    c
c93b0 68 61 72 20 2a 7a 4e 65 77 3b 0d 0a 20 20 20 20  har *zNew;..    
c93c0 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f  if( !p->useMallo
c93d0 63 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 74  c ){..      p->t
c93e0 6f 6f 42 69 67 20 3d 20 31 3b 0d 0a 20 20 20 20  ooBig = 1;..    
c93f0 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20    N = p->nAlloc 
c9400 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0d  - p->nChar - 1;.
c9410 0a 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20  .      if( N<=0 
c9420 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
c9430 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  rn;..      }..  
c9440 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
c9450 63 68 61 72 20 2a 7a 4f 6c 64 20 3d 20 28 70 2d  char *zOld = (p-
c9460 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65  >zText==p->zBase
c9470 20 3f 20 30 20 3a 20 70 2d 3e 7a 54 65 78 74 29   ? 0 : p->zText)
c9480 3b 0d 0a 20 20 20 20 20 20 69 36 34 20 73 7a 4e  ;..      i64 szN
c9490 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b 0d 0a  ew = p->nChar;..
c94a0 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 4e        szNew += N
c94b0 20 2b 20 31 3b 0d 0a 20 20 20 20 20 20 69 66 28   + 1;..      if(
c94c0 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c   szNew > p->mxAl
c94d0 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  loc ){..        
c94e0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
c94f0 65 73 65 74 28 70 29 3b 0d 0a 20 20 20 20 20 20  eset(p);..      
c9500 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b    p->tooBig = 1;
c9510 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
c9520 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
c9530 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c  .        p->nAll
c9540 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 3b  oc = (int)szNew;
c9550 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
c9560 20 69 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f   if( p->useMallo
c9570 63 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20  c==1 ){..       
c9580 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
c9590 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20  bRealloc(p->db, 
c95a0 7a 4f 6c 64 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 29  zOld, p->nAlloc)
c95b0 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
c95c0 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 20 3d 20  .        zNew = 
c95d0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
c95e0 7a 4f 6c 64 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 29  zOld, p->nAlloc)
c95f0 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
c9600 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0d 0a 20    if( zNew ){.. 
c9610 20 20 20 20 20 20 20 69 66 28 20 7a 4f 6c 64 3d         if( zOld=
c9620 3d 30 20 26 26 20 70 2d 3e 6e 43 68 61 72 3e 30  =0 && p->nChar>0
c9630 20 29 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20   ) memcpy(zNew, 
c9640 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68  p->zText, p->nCh
c9650 61 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d  ar);..        p-
c9660 3e 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0d 0a  >zText = zNew;..
c9670 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
c9680 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46        p->mallocF
c9690 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20  ailed = 1;..    
c96a0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
c96b0 63 75 6d 52 65 73 65 74 28 70 29 3b 0d 0a 20 20  cumReset(p);..  
c96c0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20        return;.. 
c96d0 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
c96e0 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d   }..  assert( p-
c96f0 3e 7a 54 65 78 74 20 29 3b 0d 0a 20 20 6d 65 6d  >zText );..  mem
c9700 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d  cpy(&p->zText[p-
c9710 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0d  >nChar], z, N);.
c9720 0a 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e  .  p->nChar += N
c9730 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46  ;..}..../*..** F
c9740 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 72 69  inish off a stri
c9750 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 75 72  ng by making sur
c9760 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 65 72  e it is zero-ter
c9770 6d 69 6e 61 74 65 64 2e 0d 0a 2a 2a 20 52 65 74  minated...** Ret
c9780 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
c9790 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73   the resulting s
c97a0 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61  tring.  Return a
c97b0 20 4e 55 4c 4c 0d 0a 2a 2a 20 70 6f 69 6e 74 65   NULL..** pointe
c97c0 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66  r if any kind of
c97d0 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75   error was encou
c97e0 6e 74 65 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ntered...*/..SQL
c97f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
c9800 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 75   *sqlite3StrAccu
c9810 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d  mFinish(StrAccum
c9820 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e   *p){..  if( p->
c9830 7a 54 65 78 74 20 29 7b 0d 0a 20 20 20 20 70 2d  zText ){..    p-
c9840 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d  >zText[p->nChar]
c9850 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 70   = 0;..    if( p
c9860 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70  ->useMalloc && p
c9870 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73  ->zText==p->zBas
c9880 65 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  e ){..      if( 
c9890 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d 31 20  p->useMalloc==1 
c98a0 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a  ){..        p->z
c98b0 54 65 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Text = sqlite3Db
c98c0 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c  MallocRaw(p->db,
c98d0 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0d 0a   p->nChar+1 );..
c98e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
c98f0 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d        p->zText =
c9900 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
c9910 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0d 0a 20 20  p->nChar+1);..  
c9920 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
c9930 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0d 0a 20 20   p->zText ){..  
c9940 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e        memcpy(p->
c9950 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 2c  zText, p->zBase,
c9960 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0d 0a 20   p->nChar+1);.. 
c9970 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
c9980 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61       p->mallocFa
c9990 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20  iled = 1;..     
c99a0 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
c99b0 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78    return p->zTex
c99c0 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  t;..}..../*..** 
c99d0 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75  Reset an StrAccu
c99e0 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61  m string.  Recla
c99f0 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20  im all malloced 
c9a00 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c  memory...*/..SQL
c9a10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
c9a20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
c9a30 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a  Reset(StrAccum *
c9a40 70 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e 7a 54  p){..  if( p->zT
c9a50 65 78 74 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b  ext!=p->zBase ){
c9a60 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65  ..    if( p->use
c9a70 4d 61 6c 6c 6f 63 3d 3d 31 20 29 7b 0d 0a 20 20  Malloc==1 ){..  
c9a80 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
c9a90 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78  e(p->db, p->zTex
c9aa0 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  t);..    }else{.
c9ab0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
c9ac0 72 65 65 28 70 2d 3e 7a 54 65 78 74 29 3b 0d 0a  ree(p->zText);..
c9ad0 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70 2d      }..  }..  p-
c9ae0 3e 7a 54 65 78 74 20 3d 20 30 3b 0d 0a 7d 0d 0a  >zText = 0;..}..
c9af0 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c  ../*..** Initial
c9b00 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63  ize a string acc
c9b10 75 6d 75 6c 61 74 6f 72 0d 0a 2a 2f 0d 0a 53 51  umulator..*/..SQ
c9b20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
c9b30 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  d sqlite3StrAccu
c9b40 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a  mInit(StrAccum *
c9b50 70 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20  p, char *zBase, 
c9b60 69 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0d  int n, int mx){.
c9b70 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d  .  p->zText = p-
c9b80 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0d  >zBase = zBase;.
c9b90 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0d 0a 20  .  p->db = 0;.. 
c9ba0 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0d 0a   p->nChar = 0;..
c9bb0 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b    p->nAlloc = n;
c9bc0 0d 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d  ..  p->mxAlloc =
c9bd0 20 6d 78 3b 0d 0a 20 20 70 2d 3e 75 73 65 4d 61   mx;..  p->useMa
c9be0 6c 6c 6f 63 20 3d 20 31 3b 0d 0a 20 20 70 2d 3e  lloc = 1;..  p->
c9bf0 74 6f 6f 42 69 67 20 3d 20 30 3b 0d 0a 20 20 70  tooBig = 0;..  p
c9c00 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
c9c10 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
c9c20 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f   Print into memo
c9c30 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
c9c40 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
c9c50 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e    Use the intern
c9c60 61 6c 0d 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73  al..** %-convers
c9c70 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0d  ion extensions..
c9c80 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
c9c90 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
c9ca0 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  3VMPrintf(sqlite
c9cb0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
c9cc0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
c9cd0 69 73 74 20 61 70 29 7b 0d 0a 20 20 63 68 61 72  ist ap){..  char
c9ce0 20 2a 7a 3b 0d 0a 20 20 63 68 61 72 20 7a 42 61   *z;..  char zBa
c9cf0 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  se[SQLITE_PRINT_
c9d00 42 55 46 5f 53 49 5a 45 5d 3b 0d 0a 20 20 53 74  BUF_SIZE];..  St
c9d10 72 41 63 63 75 6d 20 61 63 63 3b 0d 0a 20 20 61  rAccum acc;..  a
c9d20 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0d  ssert( db!=0 );.
c9d30 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
c9d40 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61  umInit(&acc, zBa
c9d50 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65  se, sizeof(zBase
c9d60 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ),..            
c9d70 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c            db->aL
c9d80 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
c9d90 54 5f 4c 45 4e 47 54 48 5d 29 3b 0d 0a 20 20 61  T_LENGTH]);..  a
c9da0 63 63 2e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 73  cc.db = db;..  s
c9db0 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26  qlite3VXPrintf(&
c9dc0 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c  acc, 1, zFormat,
c9dd0 20 61 70 29 3b 0d 0a 20 20 7a 20 3d 20 73 71 6c   ap);..  z = sql
c9de0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
c9df0 73 68 28 26 61 63 63 29 3b 0d 0a 20 20 69 66 28  sh(&acc);..  if(
c9e00 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65   acc.mallocFaile
c9e10 64 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d 61  d ){..    db->ma
c9e20 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d  llocFailed = 1;.
c9e30 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 7a  .  }..  return z
c9e40 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50  ;..}..../*..** P
c9e50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
c9e60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
c9e70 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
c9e80 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  Use the internal
c9e90 0d 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  ..** %-conversio
c9ea0 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0d 0a 2a  n extensions...*
c9eb0 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
c9ec0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d  E char *sqlite3M
c9ed0 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a  Printf(sqlite3 *
c9ee0 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
c9ef0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a  zFormat, ...){..
c9f00 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20    va_list ap;.. 
c9f10 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 76 61 5f   char *z;..  va_
c9f20 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
c9f30 74 29 3b 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74  t);..  z = sqlit
c9f40 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
c9f50 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20  Format, ap);..  
c9f60 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 72  va_end(ap);..  r
c9f70 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn z;..}..../
c9f80 2a 0d 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 74  *..** Like sqlit
c9f90 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 75 74  e3MPrintf(), but
c9fa0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
c9fb0 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 61 66  ree() on zStr af
c9fc0 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 0d 0a  ter formatting..
c9fd0 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 6e  ** the string an
c9fe0 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 6e  d before returnn
c9ff0 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ing.  This routi
ca000 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ne is intended t
ca010 6f 20 62 65 20 75 73 65 64 0d 0a 2a 2a 20 74 6f  o be used..** to
ca020 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74   modify an exist
ca030 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  ing string.  For
ca040 20 65 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a   example:..**..*
ca050 2a 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69  *       x = sqli
ca060 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78  te3MPrintf(db, x
ca070 2c 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66  , "prefix %s suf
ca080 66 69 78 22 2c 20 78 29 3b 0d 0a 2a 2a 0d 0a 2a  fix", x);..**..*
ca090 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
ca0a0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d  E char *sqlite3M
ca0b0 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 20  Appendf(sqlite3 
ca0c0 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 74 72 2c  *db, char *zStr,
ca0d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
ca0e0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20 76  rmat, ...){..  v
ca0f0 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20 20 63 68  a_list ap;..  ch
ca100 61 72 20 2a 7a 3b 0d 0a 20 20 76 61 5f 73 74 61  ar *z;..  va_sta
ca110 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
ca120 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56  ..  z = sqlite3V
ca130 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
ca140 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76 61 5f  mat, ap);..  va_
ca150 65 6e 64 28 61 70 29 3b 0d 0a 20 20 73 71 6c 69  end(ap);..  sqli
ca160 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
ca170 74 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 7a  tr);..  return z
ca180 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50  ;..}..../*..** P
ca190 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
ca1a0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
ca1b0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
ca1c0 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72    Omit the inter
ca1d0 6e 61 6c 0d 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  nal..** %-conver
ca1e0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
ca1f0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
ca200 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
ca210 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
ca220 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f  ar *zFormat, va_
ca230 6c 69 73 74 20 61 70 29 7b 0d 0a 20 20 63 68 61  list ap){..  cha
ca240 72 20 2a 7a 3b 0d 0a 20 20 63 68 61 72 20 7a 42  r *z;..  char zB
ca250 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54  ase[SQLITE_PRINT
ca260 5f 42 55 46 5f 53 49 5a 45 5d 3b 0d 0a 20 20 53  _BUF_SIZE];..  S
ca270 74 72 41 63 63 75 6d 20 61 63 63 3b 0d 0a 23 69  trAccum acc;..#i
ca280 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
ca290 54 5f 41 55 54 4f 49 4e 49 54 0d 0a 20 20 69 66  T_AUTOINIT..  if
ca2a0 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
ca2b0 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
ca2c0 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71  0;..#endif..  sq
ca2d0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
ca2e0 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
ca2f0 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51  izeof(zBase), SQ
ca300 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29  LITE_MAX_LENGTH)
ca310 3b 0d 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c  ;..  acc.useMall
ca320 6f 63 20 3d 20 32 3b 0d 0a 20 20 73 71 6c 69 74  oc = 2;..  sqlit
ca330 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c  e3VXPrintf(&acc,
ca340 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   0, zFormat, ap)
ca350 3b 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  ;..  z = sqlite3
ca360 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
ca370 61 63 63 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  acc);..  return 
ca380 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  z;..}..../*..** 
ca390 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
ca3a0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
ca3b0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
ca3c0 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e  ().  Omit the in
ca3d0 74 65 72 6e 61 6c 0d 0a 2a 2a 20 25 2d 63 6f 6e  ternal..** %-con
ca3e0 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f  version extensio
ca3f0 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ns...*/..SQLITE_
ca400 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
ca410 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  3_mprintf(const 
ca420 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
ca430 2e 2e 29 7b 0d 0a 20 20 76 61 5f 6c 69 73 74 20  ..){..  va_list 
ca440 61 70 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d  ap;..  char *z;.
ca450 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
ca460 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a 20  OMIT_AUTOINIT.. 
ca470 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69   if( sqlite3_ini
ca480 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75  tialize() ) retu
ca490 72 6e 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  rn 0;..#endif.. 
ca4a0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
ca4b0 6f 72 6d 61 74 29 3b 0d 0a 20 20 7a 20 3d 20 73  ormat);..  z = s
ca4c0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
ca4d0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20  zFormat, ap);.. 
ca4e0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20   va_end(ap);..  
ca4f0 72 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 0d 0a  return z;..}....
ca500 2f 2a 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73  /*..** sqlite3_s
ca510 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20  nprintf() works 
ca520 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20  like snprintf() 
ca530 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69  except that it i
ca540 67 6e 6f 72 65 73 20 74 68 65 0d 0a 2a 2a 20 63  gnores the..** c
ca550 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65  urrent locale se
ca560 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73  ttings.  This is
ca570 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53   important for S
ca580 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 77 65  QLite because we
ca590 0d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c  ..** are not abl
ca5a0 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 61  e to use a "," a
ca5b0 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  s the decimal po
ca5c0 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  int in place of 
ca5d0 22 2e 22 20 61 73 0d 0a 2a 2a 20 73 70 65 63 69  "." as..** speci
ca5e0 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63  fied by some loc
ca5f0 61 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6f  ales...**..** Oo
ca600 70 73 3a 20 20 54 68 65 20 66 69 72 73 74 20 74  ps:  The first t
ca610 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 6f 66 20  wo arguments of 
ca620 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
ca630 28 29 20 61 72 65 20 62 61 63 6b 77 61 72 64 73  () are backwards
ca640 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6e  ..** from the sn
ca650 70 72 69 6e 74 66 28 29 20 73 74 61 6e 64 61 72  printf() standar
ca660 64 2e 20 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c  d.  Unfortunatel
ca670 79 2c 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 74  y, it is too lat
ca680 65 20 74 6f 20 63 68 61 6e 67 65 0d 0a 2a 2a 20  e to change..** 
ca690 74 68 69 73 20 77 69 74 68 6f 75 74 20 62 72 65  this without bre
ca6a0 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c  aking compatibil
ca6b0 69 74 79 2c 20 73 6f 20 77 65 20 6a 75 73 74 20  ity, so we just 
ca6c0 68 61 76 65 20 74 6f 20 6c 69 76 65 20 77 69 74  have to live wit
ca6d0 68 20 74 68 65 0d 0a 2a 2a 20 6d 69 73 74 61 6b  h the..** mistak
ca6e0 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69 74  e...**..** sqlit
ca6f0 65 33 5f 76 73 6e 70 72 69 6e 74 66 28 29 20 69  e3_vsnprintf() i
ca700 73 20 74 68 65 20 76 61 72 61 72 67 73 20 76 65  s the varargs ve
ca710 72 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  rsion...*/..SQLI
ca720 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
ca730 69 74 65 33 5f 76 73 6e 70 72 69 6e 74 66 28 69  ite3_vsnprintf(i
ca740 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66  nt n, char *zBuf
ca750 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
ca760 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61  ormat, va_list a
ca770 70 29 7b 0d 0a 20 20 53 74 72 41 63 63 75 6d 20  p){..  StrAccum 
ca780 61 63 63 3b 0d 0a 20 20 69 66 28 20 6e 3c 3d 30  acc;..  if( n<=0
ca790 20 29 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0d   ) return zBuf;.
ca7a0 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
ca7b0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75  umInit(&acc, zBu
ca7c0 66 2c 20 6e 2c 20 30 29 3b 0d 0a 20 20 61 63 63  f, n, 0);..  acc
ca7d0 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d  .useMalloc = 0;.
ca7e0 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
ca7f0 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
ca800 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 72 65 74  mat, ap);..  ret
ca810 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63  urn sqlite3StrAc
ca820 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
ca830 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..}..SQLITE_API 
ca840 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e  char *sqlite3_sn
ca850 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68  printf(int n, ch
ca860 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20  ar *zBuf, const 
ca870 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
ca880 2e 2e 29 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b  ..){..  char *z;
ca890 0d 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d  ..  va_list ap;.
ca8a0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a  .  va_start(ap,z
ca8b0 46 6f 72 6d 61 74 29 3b 0d 0a 20 20 7a 20 3d 20  Format);..  z = 
ca8c0 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e 74  sqlite3_vsnprint
ca8d0 66 28 6e 2c 20 7a 42 75 66 2c 20 7a 46 6f 72 6d  f(n, zBuf, zForm
ca8e0 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76 61 5f 65  at, ap);..  va_e
ca8f0 6e 64 28 61 70 29 3b 0d 0a 20 20 72 65 74 75 72  nd(ap);..  retur
ca900 6e 20 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n z;..}..../*..*
ca910 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 72 6f  * This is the ro
ca920 75 74 69 6e 65 20 74 68 61 74 20 61 63 74 75 61  utine that actua
ca930 6c 6c 79 20 66 6f 72 6d 61 74 73 20 74 68 65 20  lly formats the 
ca940 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 6d 65  sqlite3_log() me
ca950 73 73 61 67 65 2e 0d 0a 2a 2a 20 57 65 20 68 6f  ssage...** We ho
ca960 75 73 65 20 69 74 20 69 6e 20 61 20 73 65 70 61  use it in a sepa
ca970 72 61 74 65 20 72 6f 75 74 69 6e 65 20 66 72 6f  rate routine fro
ca980 6d 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20  m sqlite3_log() 
ca990 74 6f 20 61 76 6f 69 64 20 75 73 69 6e 67 0d 0a  to avoid using..
ca9a0 2a 2a 20 73 74 61 63 6b 20 73 70 61 63 65 20 6f  ** stack space o
ca9b0 6e 20 73 6d 61 6c 6c 2d 73 74 61 63 6b 20 73 79  n small-stack sy
ca9c0 73 74 65 6d 73 20 77 68 65 6e 20 6c 6f 67 67 69  stems when loggi
ca9d0 6e 67 20 69 73 20 64 69 73 61 62 6c 65 64 2e 0d  ng is disabled..
ca9e0 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  .**..** sqlite3_
ca9f0 6c 6f 67 28 29 20 6d 75 73 74 20 72 65 6e 64 65  log() must rende
caa00 72 20 69 6e 74 6f 20 61 20 73 74 61 74 69 63 20  r into a static 
caa10 62 75 66 66 65 72 2e 20 20 49 74 20 63 61 6e 6e  buffer.  It cann
caa20 6f 74 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0d 0a  ot dynamically..
caa30 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  ** allocate memo
caa40 72 79 20 62 65 63 61 75 73 65 20 69 74 20 6d 69  ry because it mi
caa50 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 77 68  ght be called wh
caa60 69 6c 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ile the memory a
caa70 6c 6c 6f 63 61 74 6f 72 0d 0a 2a 2a 20 6d 75 74  llocator..** mut
caa80 65 78 20 69 73 20 68 65 6c 64 2e 0d 0a 2a 2f 0d  ex is held...*/.
caa90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e  .static void ren
caaa0 64 65 72 4c 6f 67 4d 73 67 28 69 6e 74 20 69 45  derLogMsg(int iE
caab0 72 72 43 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68  rrCode, const ch
caac0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f  ar *zFormat, va_
caad0 6c 69 73 74 20 61 70 29 7b 0d 0a 20 20 53 74 72  list ap){..  Str
caae0 41 63 63 75 6d 20 61 63 63 3b 20 20 20 20 20 20  Accum acc;      
caaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cab00 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 61 63      /* String ac
cab10 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0d 0a 20 20  cumulator */..  
cab20 63 68 61 72 20 7a 4d 73 67 5b 53 51 4c 49 54 45  char zMsg[SQLITE
cab30 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 2a  _PRINT_BUF_SIZE*
cab40 33 5d 3b 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65  3];    /* Comple
cab50 74 65 20 6c 6f 67 20 6d 65 73 73 61 67 65 20 2a  te log message *
cab60 2f 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 53 74  /....  sqlite3St
cab70 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c  rAccumInit(&acc,
cab80 20 7a 4d 73 67 2c 20 73 69 7a 65 6f 66 28 7a 4d   zMsg, sizeof(zM
cab90 73 67 29 2c 20 30 29 3b 0d 0a 20 20 61 63 63 2e  sg), 0);..  acc.
caba0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a  useMalloc = 0;..
cabb0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
cabc0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d  f(&acc, 0, zForm
cabd0 61 74 2c 20 61 70 29 3b 0d 0a 20 20 73 71 6c 69  at, ap);..  sqli
cabe0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
cabf0 78 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62  xLog(sqlite3Glob
cac00 61 6c 43 6f 6e 66 69 67 2e 70 4c 6f 67 41 72 67  alConfig.pLogArg
cac10 2c 20 69 45 72 72 43 6f 64 65 2c 0d 0a 20 20 20  , iErrCode,..   
cac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cac30 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
cac40 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61  trAccumFinish(&a
cac50 63 63 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  cc));..}..../*..
cac60 2a 2a 20 46 6f 72 6d 61 74 20 61 6e 64 20 77 72  ** Format and wr
cac70 69 74 65 20 61 20 6d 65 73 73 61 67 65 20 74 6f  ite a message to
cac80 20 74 68 65 20 6c 6f 67 20 69 66 20 6c 6f 67 67   the log if logg
cac90 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 2e 0d  ing is enabled..
caca0 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
cacb0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6c 6f 67  void sqlite3_log
cacc0 28 69 6e 74 20 69 45 72 72 43 6f 64 65 2c 20 63  (int iErrCode, c
cacd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
cace0 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20 76 61 5f  at, ...){..  va_
cacf0 6c 69 73 74 20 61 70 3b 20 20 20 20 20 20 20 20  list ap;        
cad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cad10 20 20 20 20 20 2f 2a 20 56 61 72 61 72 67 20 6c       /* Vararg l
cad20 69 73 74 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71  ist */..  if( sq
cad30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
cad40 67 2e 78 4c 6f 67 20 29 7b 0d 0a 20 20 20 20 76  g.xLog ){..    v
cad50 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
cad60 6d 61 74 29 3b 0d 0a 20 20 20 20 72 65 6e 64 65  mat);..    rende
cad70 72 4c 6f 67 4d 73 67 28 69 45 72 72 43 6f 64 65  rLogMsg(iErrCode
cad80 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0d  , zFormat, ap);.
cad90 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  .    va_end(ap);
cada0 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 69 66 20  ..  }..}....#if 
cadb0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
cadc0 45 42 55 47 29 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20  EBUG)../*..** A 
cadd0 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74  version of print
cade0 66 28 29 20 74 68 61 74 20 75 6e 64 65 72 73 74  f() that underst
cadf0 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64  ands %lld.  Used
cae00 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0d   for debugging..
cae10 0a 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 28 29  .** The printf()
cae20 20 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65   built into some
cae30 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e   versions of win
cae40 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e  dows does not un
cae50 64 65 72 73 74 61 6e 64 20 25 6c 6c 64 0d 0a 2a  derstand %lld..*
cae60 2a 20 61 6e 64 20 73 65 67 66 61 75 6c 74 73 20  * and segfaults 
cae70 69 66 20 79 6f 75 20 67 69 76 65 20 69 74 20 61  if you give it a
cae80 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e 0d   long long int..
cae90 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
caea0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
caeb0 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73  DebugPrintf(cons
caec0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
caed0 20 2e 2e 2e 29 7b 0d 0a 20 20 76 61 5f 6c 69 73   ...){..  va_lis
caee0 74 20 61 70 3b 0d 0a 20 20 53 74 72 41 63 63 75  t ap;..  StrAccu
caef0 6d 20 61 63 63 3b 0d 0a 20 20 63 68 61 72 20 7a  m acc;..  char z
caf00 42 75 66 5b 35 30 30 5d 3b 0d 0a 20 20 73 71 6c  Buf[500];..  sql
caf10 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
caf20 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a  (&acc, zBuf, siz
caf30 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0d 0a  eof(zBuf), 0);..
caf40 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20    acc.useMalloc 
caf50 3d 20 30 3b 0d 0a 20 20 76 61 5f 73 74 61 72 74  = 0;..  va_start
caf60 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0d 0a 20  (ap,zFormat);.. 
caf70 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66   sqlite3VXPrintf
caf80 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61  (&acc, 0, zForma
caf90 74 2c 20 61 70 29 3b 0d 0a 20 20 76 61 5f 65 6e  t, ap);..  va_en
cafa0 64 28 61 70 29 3b 0d 0a 20 20 73 71 6c 69 74 65  d(ap);..  sqlite
cafb0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
cafc0 26 61 63 63 29 3b 0d 0a 20 20 66 70 72 69 6e 74  &acc);..  fprint
cafd0 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a  f(stdout,"%s", z
cafe0 42 75 66 29 3b 0d 0a 20 20 66 66 6c 75 73 68 28  Buf);..  fflush(
caff0 73 74 64 6f 75 74 29 3b 0d 0a 7d 0d 0a 23 65 6e  stdout);..}..#en
cb000 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  dif....#ifndef S
cb010 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
cb020 0d 0a 2f 2a 0d 0a 2a 2a 20 76 61 72 69 61 62 6c  ../*..** variabl
cb030 65 2d 61 72 67 75 6d 65 6e 74 20 77 72 61 70 70  e-argument wrapp
cb040 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74 65  er around sqlite
cb050 33 56 58 50 72 69 6e 74 66 28 29 2e 0d 0a 2a 2f  3VXPrintf()...*/
cb060 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
cb070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 58 50 72   void sqlite3XPr
cb080 69 6e 74 66 28 53 74 72 41 63 63 75 6d 20 2a 70  intf(StrAccum *p
cb090 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
cb0a0 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20  ormat, ...){..  
cb0b0 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20 20 76  va_list ap;..  v
cb0c0 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d  a_start(ap,zForm
cb0d0 61 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  at);..  sqlite3V
cb0e0 58 50 72 69 6e 74 66 28 70 2c 20 31 2c 20 7a 46  XPrintf(p, 1, zF
cb0f0 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76  ormat, ap);..  v
cb100 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 7d 0d 0a 23  a_end(ap);..}..#
cb110 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  endif..../******
cb120 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
cb130 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  printf.c *******
cb140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb160 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
cb170 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
cb180 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a  file random.c **
cb190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
cb1c0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
cb1d0 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   15..**..** The 
cb1e0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
cb1f0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
cb200 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
cb210 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
cb220 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
cb230 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
cb240 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
cb250 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
cb260 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
cb270 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
cb280 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
cb290 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
cb2a0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
cb2b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
cb2c0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
cb2d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
cb2e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
cb2f0 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
cb300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20  ***********..** 
cb340 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
cb350 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  ns code to imple
cb360 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 61  ment a pseudo-ra
cb370 6e 64 6f 6d 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20  ndom number..** 
cb380 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29  generator (PRNG)
cb390 20 66 6f 72 20 53 51 4c 69 74 65 2e 0d 0a 2a 2a   for SQLite...**
cb3a0 0d 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62  ..** Random numb
cb3b0 65 72 73 20 61 72 65 20 75 73 65 64 20 62 79 20  ers are used by 
cb3c0 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  some of the data
cb3d0 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e  base backends in
cb3e0 20 6f 72 64 65 72 0d 0a 2a 2a 20 74 6f 20 67 65   order..** to ge
cb3f0 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e  nerate random in
cb400 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 74  teger keys for t
cb410 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20  ables or random 
cb420 66 69 6c 65 6e 61 6d 65 73 2e 0d 0a 2a 2f 0d 0a  filenames...*/..
cb430 0d 0a 0d 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61  ..../* All threa
cb440 64 73 20 73 68 61 72 65 20 61 20 73 69 6e 67 6c  ds share a singl
cb450 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
cb460 67 65 6e 65 72 61 74 6f 72 2e 0d 0a 2a 2a 20 54  generator...** T
cb470 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
cb480 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
cb490 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  te of the genera
cb4a0 74 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  tor...*/..static
cb4b0 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
cb4c0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
cb4d0 70 65 20 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  pe {..  unsigned
cb4e0 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20   char isInit;   
cb4f0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
cb500 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  f initialized */
cb510 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ..  unsigned cha
cb520 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  r i, j;         
cb530 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69     /* State vari
cb540 61 62 6c 65 73 20 2a 2f 0d 0a 20 20 75 6e 73 69  ables */..  unsi
cb550 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d  gned char s[256]
cb560 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ;          /* St
cb570 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  ate variables */
cb580 0d 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 3b  ..} sqlite3Prng;
cb590 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 74 20 61  ..../*..** Get a
cb5a0 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61   single 8-bit ra
cb5b0 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20  ndom value from 
cb5c0 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54  the RC4 PRNG.  T
cb5d0 68 65 20 4d 75 74 65 78 0d 0a 2a 2a 20 6d 75 73  he Mutex..** mus
cb5e0 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20  t be held while 
cb5f0 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 72  executing this r
cb600 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  outine...**..** 
cb610 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65  Why not just use
cb620 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f   a library rando
cb630 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65  m generator like
cb640 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74   lrand48() for t
cb650 68 69 73 3f 0d 0a 2a 2a 20 42 65 63 61 75 73 65  his?..** Because
cb660 20 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64   the OP_NewRowid
cb670 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56   opcode in the V
cb680 44 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68  DBE depends on h
cb690 61 76 69 6e 67 20 61 20 76 65 72 79 0d 0a 2a 2a  aving a very..**
cb6a0 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20   good source of 
cb6b0 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20  random numbers. 
cb6c0 20 54 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c   The lrand48() l
cb6d0 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20  ibrary function 
cb6e0 6d 61 79 0d 0a 2a 2a 20 77 65 6c 6c 20 62 65 20  may..** well be 
cb6f0 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 42 75  good enough.  Bu
cb700 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72  t maybe not.  Or
cb710 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 28 29   maybe lrand48()
cb720 20 68 61 73 20 73 6f 6d 65 0d 0a 2a 2a 20 73 75   has some..** su
cb730 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f 6e  btle problems on
cb740 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68   some systems th
cb750 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70  at could cause p
cb760 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 20  roblems.  It is 
cb770 68 61 72 64 0d 0a 2a 2a 20 74 6f 20 6b 6e 6f 77  hard..** to know
cb780 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74  .  To minimize t
cb790 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c  he risk of probl
cb7a0 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c  ems due to bad l
cb7b0 72 61 6e 64 34 38 28 29 0d 0a 2a 2a 20 69 6d 70  rand48()..** imp
cb7c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51  lementations, SQ
cb7d0 4c 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72  Lite uses this r
cb7e0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
cb7f0 65 72 61 74 6f 72 20 62 61 73 65 64 0d 0a 2a 2a  erator based..**
cb800 20 6f 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77   on RC4, which w
cb810 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72  e know works ver
cb820 79 20 77 65 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  y well...**..** 
cb830 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75 61 6c  (Later):  Actual
cb840 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  ly, OP_NewRowid 
cb850 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e 64 20  does not depend 
cb860 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 63 65  on a good source
cb870 20 6f 66 0d 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65   of..** randomne
cb880 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 75  ss any more.  Bu
cb890 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20  t we will leave 
cb8a0 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c 6c  this code in all
cb8b0 20 74 68 65 20 73 61 6d 65 2e 0d 0a 2a 2f 0d 0a   the same...*/..
cb8c0 73 74 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d  static u8 random
cb8d0 42 79 74 65 28 76 6f 69 64 29 7b 0d 0a 20 20 75  Byte(void){..  u
cb8e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0d  nsigned char t;.
cb8f0 0a 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 22 77  .....  /* The "w
cb900 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69  sdPrng" macro wi
cb910 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
cb920 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  e pseudo-random 
cb930 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
cb940 0d 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63  ..  ** state vec
cb950 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c  tor.  If writabl
cb960 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
cb970 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20   unsupported on 
cb980 74 68 65 20 74 61 72 67 65 74 2c 0d 0a 20 20 2a  the target,..  *
cb990 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
cb9a0 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
cb9b0 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
cb9c0 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
cb9d0 6f 6d 6d 6f 6e 0d 0a 20 20 2a 2a 20 63 61 73 65  ommon..  ** case
cb9e0 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
cb9f0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
cba00 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 72 6e  upported, wsdPrn
cba10 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  g can refer dire
cba20 63 74 6c 79 0d 0a 20 20 2a 2a 20 74 6f 20 74 68  ctly..  ** to th
cba30 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 20  e "sqlite3Prng" 
cba40 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63  state vector dec
cba50 6c 61 72 65 64 20 61 62 6f 76 65 2e 0d 0a 20 20  lared above...  
cba60 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
cba70 45 5f 4f 4d 49 54 5f 57 53 44 0d 0a 20 20 73 74  E_OMIT_WSD..  st
cba80 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
cba90 54 79 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41  Type *p = &GLOBA
cbaa0 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  L(struct sqlite3
cbab0 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65  PrngType, sqlite
cbac0 33 50 72 6e 67 29 3b 0d 0a 23 20 64 65 66 69 6e  3Prng);..# defin
cbad0 65 20 77 73 64 50 72 6e 67 20 70 5b 30 5d 0d 0a  e wsdPrng p[0]..
cbae0 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
cbaf0 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 33 50  wsdPrng sqlite3P
cbb00 72 6e 67 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  rng..#endif.....
cbb10 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
cbb20 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
cbb30 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
cbb40 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0d  generator once,.
cbb50 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
cbb60 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e  time this routin
cbb70 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  e is called.  Th
cbb80 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65  e seed value doe
cbb90 73 0d 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64  s..  ** not need
cbba0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f   to contain a lo
cbbb0 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20  t of randomness 
cbbc0 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74  since we are not
cbbd0 0d 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f  ..  ** trying to
cbbe0 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79   do secure encry
cbbf0 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e  ption or anythin
cbc00 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0d 0a  g like that.....
cbc10 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f 74 68 69    **..  ** Nothi
cbc20 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ng in this file 
cbc30 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65  or anywhere else
cbc40 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20   in SQLite does 
cbc50 61 6e 79 20 6b 69 6e 64 20 6f 66 0d 0a 20 20 2a  any kind of..  *
cbc60 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54  * encryption.  T
cbc70 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d  he RC4 algorithm
cbc80 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 61   is being used a
cbc90 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f  s a PRNG (pseudo
cbca0 2d 72 61 6e 64 6f 6d 0d 0a 20 20 2a 2a 20 6e 75  -random..  ** nu
cbcb0 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20  mber generator) 
cbcc0 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70  not as an encryp
cbcd0 74 69 6f 6e 20 64 65 76 69 63 65 2e 0d 0a 20 20  tion device...  
cbce0 2a 2f 0d 0a 20 20 69 66 28 20 21 77 73 64 50 72  */..  if( !wsdPr
cbcf0 6e 67 2e 69 73 49 6e 69 74 20 29 7b 0d 0a 20 20  ng.isInit ){..  
cbd00 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 63 68    int i;..    ch
cbd10 61 72 20 6b 5b 32 35 36 5d 3b 0d 0a 20 20 20 20  ar k[256];..    
cbd20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b 0d 0a  wsdPrng.j = 0;..
cbd30 20 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d 20      wsdPrng.i = 
cbd40 30 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  0;..    sqlite3O
cbd50 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  sRandomness(sqli
cbd60 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c  te3_vfs_find(0),
cbd70 20 32 35 36 2c 20 6b 29 3b 0d 0a 20 20 20 20 66   256, k);..    f
cbd80 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69  or(i=0; i<256; i
cbd90 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 77 73 64 50  ++){..      wsdP
cbda0 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69  rng.s[i] = (u8)i
cbdb0 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 6f  ;..    }..    fo
cbdc0 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b  r(i=0; i<256; i+
cbdd0 2b 29 7b 0d 0a 20 20 20 20 20 20 77 73 64 50 72  +){..      wsdPr
cbde0 6e 67 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e  ng.j += wsdPrng.
cbdf0 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0d 0a 20 20  s[i] + k[i];..  
cbe00 20 20 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e      t = wsdPrng.
cbe10 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0d 0a 20  s[wsdPrng.j];.. 
cbe20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 77       wsdPrng.s[w
cbe30 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 50  sdPrng.j] = wsdP
cbe40 72 6e 67 2e 73 5b 69 5d 3b 0d 0a 20 20 20 20 20  rng.s[i];..     
cbe50 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20   wsdPrng.s[i] = 
cbe60 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 77  t;..    }..    w
cbe70 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20  sdPrng.isInit = 
cbe80 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  1;..  }....  /* 
cbe90 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72 65 74  Generate and ret
cbea0 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f  urn single rando
cbeb0 6d 20 62 79 74 65 0d 0a 20 20 2a 2f 0d 0a 20 20  m byte..  */..  
cbec0 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0d 0a 20 20  wsdPrng.i++;..  
cbed0 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73  t = wsdPrng.s[ws
cbee0 64 50 72 6e 67 2e 69 5d 3b 0d 0a 20 20 77 73 64  dPrng.i];..  wsd
cbef0 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0d 0a 20 20  Prng.j += t;..  
cbf00 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
cbf10 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73  g.i] = wsdPrng.s
cbf20 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0d 0a 20 20  [wsdPrng.j];..  
cbf30 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
cbf40 67 2e 6a 5d 20 3d 20 74 3b 0d 0a 20 20 74 20 2b  g.j] = t;..  t +
cbf50 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50  = wsdPrng.s[wsdP
cbf60 72 6e 67 2e 69 5d 3b 0d 0a 20 20 72 65 74 75 72  rng.i];..  retur
cbf70 6e 20 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0d  n wsdPrng.s[t];.
cbf80 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
cbf90 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74  urn N random byt
cbfa0 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  es...*/..SQLITE_
cbfb0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
cbfc0 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20  _randomness(int 
cbfd0 4e 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0d  N, void *pBuf){.
cbfe0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
cbff0 20 2a 7a 42 75 66 20 3d 20 70 42 75 66 3b 0d 0a   *zBuf = pBuf;..
cc000 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
cc010 44 53 41 46 45 0d 0a 20 20 73 71 6c 69 74 65 33  DSAFE..  sqlite3
cc020 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
cc030 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
cc040 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
cc050 54 41 54 49 43 5f 50 52 4e 47 29 3b 0d 0a 23 65  TATIC_PRNG);..#e
cc060 6e 64 69 66 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ndif..  sqlite3_
cc070 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
cc080 78 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 4e 2d  x);..  while( N-
cc090 2d 20 29 7b 0d 0a 20 20 20 20 2a 28 7a 42 75 66  - ){..    *(zBuf
cc0a0 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65  ++) = randomByte
cc0b0 28 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  ();..  }..  sqli
cc0c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
cc0d0 6d 75 74 65 78 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69  mutex);..}....#i
cc0e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
cc0f0 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0d 0a  T_BUILTIN_TEST..
cc100 2f 2a 0d 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69  /*..** For testi
cc110 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20  ng purposes, we 
cc120 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74  sometimes want t
cc130 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73  o preserve the s
cc140 74 61 74 65 20 6f 66 0d 0a 2a 2a 20 50 52 4e 47  tate of..** PRNG
cc150 20 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65   and restore the
cc160 20 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76   PRNG to its sav
cc170 65 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61  ed state at a la
cc180 74 65 72 20 74 69 6d 65 2c 20 6f 72 0d 0a 2a 2a  ter time, or..**
cc190 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 50 52   to reset the PR
cc1a0 4e 47 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61  NG to its initia
cc1b0 6c 20 73 74 61 74 65 2e 20 20 54 68 65 73 65 20  l state.  These 
cc1c0 72 6f 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c  routines accompl
cc1d0 69 73 68 0d 0a 2a 2a 20 74 68 6f 73 65 20 74 61  ish..** those ta
cc1e0 73 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  sks...**..** The
cc1f0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
cc200 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63  ntrol() interfac
cc210 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 72 6f  e calls these ro
cc220 75 74 69 6e 65 73 20 74 6f 0d 0a 2a 2a 20 63 6f  utines to..** co
cc230 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e 0d  ntrol the PRNG..
cc240 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 53 51 4c 49  .*/..static SQLI
cc250 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71  TE_WSD struct sq
cc260 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 71  lite3PrngType sq
cc270 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 3b 0d  lite3SavedPrng;.
cc280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
cc290 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67  void sqlite3Prng
cc2a0 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 7b  SaveState(void){
cc2b0 0d 0a 20 20 6d 65 6d 63 70 79 28 0d 0a 20 20 20  ..  memcpy(..   
cc2c0 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   &GLOBAL(struct 
cc2d0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
cc2e0 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e   sqlite3SavedPrn
cc2f0 67 29 2c 0d 0a 20 20 20 20 26 47 4c 4f 42 41 4c  g),..    &GLOBAL
cc300 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50  (struct sqlite3P
cc310 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33  rngType, sqlite3
cc320 50 72 6e 67 29 2c 0d 0a 20 20 20 20 73 69 7a 65  Prng),..    size
cc330 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0d  of(sqlite3Prng).
cc340 0a 20 20 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45  .  );..}..SQLITE
cc350 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
cc360 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
cc370 53 74 61 74 65 28 76 6f 69 64 29 7b 0d 0a 20 20  State(void){..  
cc380 6d 65 6d 63 70 79 28 0d 0a 20 20 20 20 26 47 4c  memcpy(..    &GL
cc390 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
cc3a0 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
cc3b0 69 74 65 33 50 72 6e 67 29 2c 0d 0a 20 20 20 20  ite3Prng),..    
cc3c0 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
cc3d0 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
cc3e0 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
cc3f0 29 2c 0d 0a 20 20 20 20 73 69 7a 65 6f 66 28 73  ),..    sizeof(s
cc400 71 6c 69 74 65 33 50 72 6e 67 29 0d 0a 20 20 29  qlite3Prng)..  )
cc410 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ;..}..SQLITE_PRI
cc420 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
cc430 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28  3PrngResetState(
cc440 76 6f 69 64 29 7b 0d 0a 20 20 47 4c 4f 42 41 4c  void){..  GLOBAL
cc450 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50  (struct sqlite3P
cc460 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33  rngType, sqlite3
cc470 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 30  Prng).isInit = 0
cc480 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
cc490 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
cc4a0 54 49 4e 5f 54 45 53 54 20 2a 2f 0d 0a 0d 0a 2f  TIN_TEST */..../
cc4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
cc4c0 6e 64 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a  nd of random.c *
cc4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
cc500 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
cc510 42 65 67 69 6e 20 66 69 6c 65 20 75 74 66 2e 63  Begin file utf.c
cc520 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
cc530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
cc550 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 34 20 41 70 72  ./*..** 2004 Apr
cc560 69 6c 20 31 33 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  il 13..**..** Th
cc570 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
cc580 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
cc590 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
cc5a0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
cc5b0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
cc5c0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
cc5d0 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
cc5e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
cc5f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
cc600 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
cc610 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
cc620 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
cc630 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
cc640 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
cc650 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
cc660 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
cc670 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
cc680 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
cc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
cc6d0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
cc6e0 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
cc6f0 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20  ed to translate 
cc700 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 0d  between UTF-8, .
cc710 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d  .** UTF-16, UTF-
cc720 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36  16BE, and UTF-16
cc730 4c 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65  LE...**..** Note
cc740 73 20 6f 6e 20 55 54 46 2d 38 3a 0d 0a 2a 2a 0d  s on UTF-8:..**.
cc750 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 20 20  .**   Byte-0    
cc760 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 2d 32  Byte-1    Byte-2
cc770 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 56 61      Byte-3    Va
cc780 6c 75 65 0d 0a 2a 2a 20 20 30 78 78 78 78 78 78  lue..**  0xxxxxx
cc790 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
cc7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cc7b0 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30    00000000 00000
cc7c0 30 30 30 20 30 78 78 78 78 78 78 78 0d 0a 2a 2a  000 0xxxxxxx..**
cc7d0 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78    110yyyyy  10xx
cc7e0 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
cc7f0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
cc800 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78  000 00000yyy yyx
cc810 78 78 78 78 78 0d 0a 2a 2a 20 20 31 31 31 30 7a  xxxxx..**  1110z
cc820 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31  zzz  10yyyyyy  1
cc830 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20  0xxxxxx         
cc840 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a      00000000 zzz
cc850 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0d 0a  zyyyy yyxxxxxx..
cc860 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 30  **  11110uuu  10
cc870 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79  uuzzzz  10yyyyyy
cc880 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30 30    10xxxxxx   000
cc890 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79  uuuuu zzzzyyyy y
cc8a0 79 78 78 78 78 78 78 0d 0a 2a 2a 0d 0a 2a 2a 0d  yxxxxxx..**..**.
cc8b0 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46  .** Notes on UTF
cc8c0 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 77 77  -16:  (with wwww
cc8d0 2b 31 3d 3d 75 75 75 75 75 29 0d 0a 2a 2a 0d 0a  +1==uuuuu)..**..
cc8e0 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20  **      Word-0  
cc8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72               Wor
cc900 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c  d-1          Val
cc910 75 65 0d 0a 2a 2a 20 20 31 31 30 31 31 30 77 77  ue..**  110110ww
cc920 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31   wwzzzzyy   1101
cc930 31 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20  11yy yyxxxxxx   
cc940 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79   000uuuuu zzzzyy
cc950 79 79 20 79 79 78 78 78 78 78 78 0d 0a 2a 2a 20  yy yyxxxxxx..** 
cc960 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
cc970 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xx              
cc980 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
cc990 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  00 zzzzyyyy yyxx
cc9a0 78 78 78 78 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  xxxx..**..**..**
cc9b0 20 42 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64   BOM or Byte Ord
cc9c0 65 72 20 4d 61 72 6b 3a 0d 0a 2a 2a 20 20 20 20  er Mark:..**    
cc9d0 20 30 78 66 66 20 30 78 66 65 20 20 20 6c 69 74   0xff 0xfe   lit
cc9e0 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31  tle-endian utf-1
cc9f0 36 20 66 6f 6c 6c 6f 77 73 0d 0a 2a 2a 20 20 20  6 follows..**   
cca00 20 20 30 78 66 65 20 30 78 66 66 20 20 20 62 69    0xfe 0xff   bi
cca10 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20  g-endian utf-16 
cca20 66 6f 6c 6c 6f 77 73 0d 0a 2a 2a 0d 0a 2a 2f 0d  follows..**..*/.
cca30 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61 73  ./* #include <as
cca40 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a 0d 0a 23 69  sert.h> */....#i
cca50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  fndef SQLITE_AMA
cca60 4c 47 41 4d 41 54 49 4f 4e 0d 0a 2f 2a 0d 0a 2a  LGAMATION../*..*
cca70 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
cca80 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69  constant value i
cca90 73 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51  s used by the SQ
ccaa0 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61  LITE_BIGENDIAN a
ccab0 6e 64 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49  nd..** SQLITE_LI
ccac0 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f  TTLEENDIAN macro
ccad0 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  s...*/..SQLITE_P
ccae0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74  RIVATE const int
ccaf0 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b   sqlite3one = 1;
ccb00 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
ccb10 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20  TE_AMALGAMATION 
ccb20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  */..../*..** Thi
ccb30 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69  s lookup table i
ccb40 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64  s used to help d
ccb50 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74 20  ecode the first 
ccb60 62 79 74 65 20 6f 66 0d 0a 2a 2a 20 61 20 6d 75  byte of..** a mu
ccb70 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 63 68  lti-byte UTF8 ch
ccb80 61 72 61 63 74 65 72 2e 0d 0a 2a 2f 0d 0a 73 74  aracter...*/..st
ccb90 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
ccba0 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33  ned char sqlite3
ccbb0 55 74 66 38 54 72 61 6e 73 31 5b 5d 20 3d 20 7b  Utf8Trans1[] = {
ccbc0 0d 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ..  0x00, 0x01, 
ccbd0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
ccbe0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
ccbf0 30 37 2c 0d 0a 20 20 30 78 30 38 2c 20 30 78 30  07,..  0x08, 0x0
ccc00 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30  9, 0x0a, 0x0b, 0
ccc10 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c  x0c, 0x0d, 0x0e,
ccc20 20 30 78 30 66 2c 0d 0a 20 20 30 78 31 30 2c 20   0x0f,..  0x10, 
ccc30 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33  0x11, 0x12, 0x13
ccc40 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78  , 0x14, 0x15, 0x
ccc50 31 36 2c 20 30 78 31 37 2c 0d 0a 20 20 30 78 31  16, 0x17,..  0x1
ccc60 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30  8, 0x19, 0x1a, 0
ccc70 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c  x1b, 0x1c, 0x1d,
ccc80 20 30 78 31 65 2c 20 30 78 31 66 2c 0d 0a 20 20   0x1e, 0x1f,..  
ccc90 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
ccca0 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78  , 0x03, 0x04, 0x
cccb0 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0d  05, 0x06, 0x07,.
cccc0 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30  .  0x08, 0x09, 0
cccd0 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c  x0a, 0x0b, 0x0c,
ccce0 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30   0x0d, 0x0e, 0x0
cccf0 66 2c 0d 0a 20 20 30 78 30 30 2c 20 30 78 30 31  f,..  0x00, 0x01
ccd00 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
ccd10 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
ccd20 30 78 30 37 2c 0d 0a 20 20 30 78 30 30 2c 20 30  0x07,..  0x00, 0
ccd30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c  x01, 0x02, 0x03,
ccd40 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
ccd50 30 2c 20 30 78 30 30 2c 0d 0a 7d 3b 0d 0a 0d 0a  0, 0x00,..};....
ccd60 0d 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f  ..#define WRITE_
ccd70 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20  UTF8(zOut, c) { 
ccd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccd90 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 69 66           \..  if
ccda0 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b 20 20  ( c<0x00080 ){  
ccdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccdd0 20 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b     \..    *zOut+
ccde0 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46 29  + = (u8)(c&0xFF)
ccdf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cce00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
cce10 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
cce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cce30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cce40 20 20 20 20 20 20 20 5c 0d 0a 20 20 65 6c 73 65         \..  else
cce50 20 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29   if( c<0x00800 )
cce60 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
cce70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cce80 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20   \..    *zOut++ 
cce90 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28 28 63  = 0xC0 + (u8)((c
ccea0 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20 20 20  >>6)&0x1F);     
cceb0 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
ccec0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
cced0 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46   + (u8)(c & 0x3F
ccee0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ccef0 20 20 20 20 20 5c 0d 0a 20 20 7d 20 20 20 20 20       \..  }     
ccf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ccf30 0d 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30  ..  else if( c<0
ccf40 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20  x10000 ){       
ccf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccf60 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20           \..    
ccf70 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b  *zOut++ = 0xE0 +
ccf80 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78   (u8)((c>>12)&0x
ccf90 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0F);            
ccfa0 20 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b     \..    *zOut+
ccfb0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28  + = 0x80 + (u8)(
ccfc0 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20  (c>>6) & 0x3F); 
ccfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
ccfe0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ccff0 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78  80 + (u8)(c & 0x
cd000 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
cd010 20 20 20 20 20 20 20 5c 0d 0a 20 20 7d 65 6c 73         \..  }els
cd020 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
cd030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd050 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20   \..    *zOut++ 
cd060 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63  = 0xF0 + (u8)((c
cd070 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20  >>18) & 0x07);  
cd080 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
cd090 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
cd0a0 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20   + (u8)((c>>12) 
cd0b0 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
cd0c0 20 20 20 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75       \..    *zOu
cd0d0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
cd0e0 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29  )((c>>6) & 0x3F)
cd0f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
cd100 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  ..    *zOut++ = 
cd110 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20  0x80 + (u8)(c & 
cd120 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x3F);          
cd130 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 7d 20           \..  } 
cd140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd170 20 20 20 5c 0d 0a 7d 0d 0a 0d 0a 23 64 65 66 69     \..}....#defi
cd180 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45  ne WRITE_UTF16LE
cd190 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20  (zOut, c) {     
cd1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
cd1c0 0d 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46  ..  if( c<=0xFFF
cd1d0 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  F ){            
cd1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd200 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f        \..    *zO
cd210 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78  ut++ = (u8)(c&0x
cd220 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
cd230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd240 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
cd250 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
cd260 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46  8)((c>>8)&0x00FF
cd270 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
cd280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd290 20 20 20 20 5c 0d 0a 20 20 7d 65 6c 73 65 7b 20      \..  }else{ 
cd2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd2d0 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
cd2e0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
cd2f0 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46  (((c>>10)&0x003F
cd300 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30  ) + (((c-0x10000
cd310 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b  )>>10)&0x00C0));
cd320 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b    \..    *zOut++
cd330 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 2b   = (u8)(0x00D8 +
cd340 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e   (((c-0x10000)>>
cd350 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20  18)&0x03));     
cd360 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20           \..    
cd370 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63  *zOut++ = (u8)(c
cd380 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20  &0x00FF);       
cd390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd3b0 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d  \..    *zOut++ =
cd3c0 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28   (u8)(0x00DC + (
cd3d0 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20  (c>>8)&0x03));  
cd3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd3f0 20 20 20 20 20 20 20 5c 0d 0a 20 20 7d 20 20 20         \..  }   
cd400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d                \.
cd440 0a 7d 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 57 52  .}....#define WR
cd450 49 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74  ITE_UTF16BE(zOut
cd460 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20  , c) {          
cd470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd480 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 69            \..  i
cd490 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20  f( c<=0xFFFF ){ 
cd4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd4d0 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20   \..    *zOut++ 
cd4e0 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78  = (u8)((c>>8)&0x
cd4f0 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
cd500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd510 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 2a          \..    *
cd520 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26  zOut++ = (u8)(c&
cd530 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
cd540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
cd560 0d 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20  ..  }else{      
cd570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd5a0 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f        \..    *zO
cd5b0 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30  ut++ = (u8)(0x00
cd5c0 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30  D8 + (((c-0x1000
cd5d0 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20  0)>>18)&0x03)); 
cd5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
cd5f0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
cd600 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30  8)(((c>>10)&0x00
cd610 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30  3F) + (((c-0x100
cd620 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29  00)>>10)&0x00C0)
cd630 29 3b 20 20 5c 0d 0a 20 20 20 20 2a 7a 4f 75 74  );  \..    *zOut
cd640 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43  ++ = (u8)(0x00DC
cd650 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29   + ((c>>8)&0x03)
cd660 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
cd670 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
cd680 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
cd690 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
cd6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd6c0 20 20 5c 0d 0a 20 20 7d 20 20 20 20 20 20 20 20    \..  }        
cd6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd700 20 20 20 20 20 20 20 20 20 5c 0d 0a 7d 0d 0a 0d           \..}...
cd710 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54  .#define READ_UT
cd720 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d 2c  F16LE(zIn, TERM,
cd730 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 20   c){            
cd740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd750 20 20 20 20 20 20 20 5c 0d 0a 20 20 63 20 3d 20         \..  c = 
cd760 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
cd770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd7a0 5c 0d 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e  \..  c += ((*zIn
cd7b0 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20  ++)<<8);        
cd7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd7e0 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 69 66           \..  if
cd7f0 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63  ( c>=0xD800 && c
cd800 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d 20  <0xE000 && TERM 
cd810 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
cd820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd830 20 20 5c 0d 0a 20 20 20 20 69 6e 74 20 63 32 20    \..    int c2 
cd840 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = (*zIn++);     
cd850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd870 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
cd880 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b    c2 += ((*zIn++
cd890 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
cd8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd8c0 20 20 20 20 5c 0d 0a 20 20 20 20 63 20 3d 20 28      \..    c = (
cd8d0 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
cd8e0 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
cd8f0 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
cd900 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0d 0a  040)<<10);   \..
cd910 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
cd920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd950 20 20 20 20 20 20 5c 0d 0a 7d 0d 0a 0d 0a 23 64        \..}....#d
cd960 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36  efine READ_UTF16
cd970 42 45 28 7a 49 6e 2c 20 54 45 52 4d 2c 20 63 29  BE(zIn, TERM, c)
cd980 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
cd990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd9a0 20 20 20 20 5c 0d 0a 20 20 63 20 3d 20 28 28 2a      \..  c = ((*
cd9b0 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20  zIn++)<<8);     
cd9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
cd9f0 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b    c += (*zIn++);
cda00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda30 20 20 20 20 20 20 5c 0d 0a 20 20 69 66 28 20 63        \..  if( c
cda40 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78  >=0xD800 && c<0x
cda50 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b 20  E000 && TERM ){ 
cda60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
cda80 0d 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28  ..    int c2 = (
cda90 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
cdaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdac0 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 63          \..    c
cdad0 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20  2 += (*zIn++);  
cdae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb10 20 5c 0d 0a 20 20 20 20 63 20 3d 20 28 63 32 26   \..    c = (c2&
cdb20 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78  0x03FF) + ((c&0x
cdb30 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28  003F)<<10) + (((
cdb40 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30  c&0x03C0)+0x0040
cdb50 29 3c 3c 31 30 29 3b 20 20 20 5c 0d 0a 20 20 7d  )<<10);   \..  }
cdb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdba0 20 20 20 5c 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a     \..}..../*..*
cdbb0 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69  * Translate a si
cdbc0 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61  ngle UTF-8 chara
cdbd0 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68  cter.  Return th
cdbe0 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e  e unicode value.
cdbf0 0d 0a 2a 2a 0d 0a 2a 2a 20 44 75 72 69 6e 67 20  ..**..** During 
cdc00 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73  translation, ass
cdc10 75 6d 65 20 74 68 61 74 20 74 68 65 20 62 79 74  ume that the byt
cdc20 65 20 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69  e that zTerm poi
cdc30 6e 74 73 0d 0a 2a 2a 20 69 73 20 61 20 30 78 30  nts..** is a 0x0
cdc40 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65  0...**..** Write
cdc50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
cdc60 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62 79  e next unread by
cdc70 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a  te back into *pz
cdc80 4e 65 78 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f  Next...**..** No
cdc90 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55  tes On Invalid U
cdca0 54 46 2d 38 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 2a  TF-8:..**..**  *
cdcb0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e    This routine n
cdcc0 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d  ever allows a 7-
cdcd0 62 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30  bit character (0
cdce0 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66  x00 through 0x7f
cdcf0 29 20 74 6f 0d 0a 2a 2a 20 20 20 20 20 62 65 20  ) to..**     be 
cdd00 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c  encoded as a mul
cdd10 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65  ti-byte characte
cdd20 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79  r.  Any multi-by
cdd30 74 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61  te character tha
cdd40 74 0d 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70  t..**     attemp
cdd50 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76  ts to encode a v
cdd60 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 30  alue between 0x0
cdd70 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 65  0 and 0x7f is re
cdd80 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64  ndered as 0xfffd
cdd90 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 2a 20 20 54 68  ...**..**  *  Th
cdda0 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72  is routine never
cddb0 20 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20   allows a UTF16 
cddc0 73 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20  surrogate value 
cddd0 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0d 0a  to be encoded...
cdde0 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74  **     If a mult
cddf0 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  i-byte character
cde00 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63   attempts to enc
cde10 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77  ode a value betw
cde20 65 65 6e 0d 0a 2a 2a 20 20 20 20 20 30 78 64 38  een..**     0xd8
cde30 30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74 68  00 and 0xe000 th
cde40 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72 65  en it is rendere
cde50 64 20 61 73 20 30 78 66 66 66 64 2e 0d 0a 2a 2a  d as 0xfffd...**
cde60 0d 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69  ..**  *  Bytes i
cde70 6e 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30  n the range of 0
cde80 78 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66  x80 through 0xbf
cde90 20 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20   which occur as 
cdea0 74 68 65 20 66 69 72 73 74 0d 0a 2a 2a 20 20 20  the first..**   
cdeb0 20 20 62 79 74 65 20 6f 66 20 61 20 63 68 61 72    byte of a char
cdec0 61 63 74 65 72 20 61 72 65 20 69 6e 74 65 72 70  acter are interp
cded0 72 65 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d  reted as single-
cdee0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 73 0d  byte characters.
cdef0 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72 65 6e 64  .**     and rend
cdf00 65 72 65 64 20 61 73 20 74 68 65 6d 73 65 6c 76  ered as themselv
cdf10 65 73 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74  es even though t
cdf20 68 65 79 20 61 72 65 20 74 65 63 68 6e 69 63 61  hey are technica
cdf30 6c 6c 79 0d 0a 2a 2a 20 20 20 20 20 69 6e 76 61  lly..**     inva
cdf40 6c 69 64 20 63 68 61 72 61 63 74 65 72 73 2e 0d  lid characters..
cdf50 0a 2a 2a 0d 0a 2a 2a 20 20 2a 20 20 54 68 69 73  .**..**  *  This
cdf60 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73   routine accepts
cdf70 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d   an infinite num
cdf80 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ber of different
cdf90 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0d   UTF8 encodings.
cdfa0 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63  .**     for unic
cdfb0 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20  ode values 0x80 
cdfc0 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74  and greater.  It
cdfd0 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f   do not change o
cdfe0 76 65 72 2d 6c 65 6e 67 74 68 0d 0a 2a 2a 20 20  ver-length..**  
cdff0 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20     encodings to 
ce000 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73  0xfffd as some s
ce010 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64  ystems recommend
ce020 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 52  ...*/..#define R
ce030 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54  EAD_UTF8(zIn, zT
ce040 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20  erm, c)         
ce050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce060 20 20 5c 0d 0a 20 20 63 20 3d 20 2a 28 7a 49 6e    \..  c = *(zIn
ce070 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
ce080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce0a0 5c 0d 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30  \..  if( c>=0xc0
ce0b0 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ce0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d                \.
ce0e0 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
ce0f0 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63  Utf8Trans1[c-0xc
ce100 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  0];             
ce110 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20              \.. 
ce120 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a     while( zIn!=z
ce130 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20  Term && (*zIn & 
ce140 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20  0xc0)==0x80 ){  
ce150 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20            \..   
ce160 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20     c = (c<<6) + 
ce170 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29  (0x3f & *(zIn++)
ce180 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ce190 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 7d          \..    }
ce1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce1d0 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 69 66 28        \..    if(
ce1e0 20 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20   c<0x80         
ce1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce210 20 20 20 20 5c 0d 0a 20 20 20 20 20 20 20 20 7c      \..        |
ce220 7c 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29  | (c&0xFFFFF800)
ce230 3d 3d 30 78 44 38 30 30 20 20 20 20 20 20 20 20  ==0xD800        
ce240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce250 20 20 5c 0d 0a 20 20 20 20 20 20 20 20 7c 7c 20    \..        || 
ce260 28 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d  (c&0xFFFFFFFE)==
ce270 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30  0xFFFE ){  c = 0
ce280 78 46 46 46 44 3b 20 7d 20 20 20 20 20 20 20 20  xFFFD; }        
ce290 5c 0d 0a 20 20 7d 0d 0a 53 51 4c 49 54 45 5f 50  \..  }..SQLITE_P
ce2a0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
ce2b0 65 33 55 74 66 38 52 65 61 64 28 0d 0a 20 20 63  e3Utf8Read(..  c
ce2c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
ce2d0 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 2f  ar *zIn,       /
ce2e0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
ce2f0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 20  UTF-8 character 
ce300 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  */..  const unsi
ce310 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65  gned char **pzNe
ce320 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66  xt    /* Write f
ce330 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 55  irst byte past U
ce340 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a  TF-8 char here *
ce350 2f 0d 0a 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65  /..){..  unsigne
ce360 64 20 69 6e 74 20 63 3b 0d 0a 0d 0a 20 20 2f 2a  d int c;....  /*
ce370 20 53 61 6d 65 20 61 73 20 52 45 41 44 5f 55 54   Same as READ_UT
ce380 46 38 28 29 20 61 62 6f 76 65 20 62 75 74 20 77  F8() above but w
ce390 69 74 68 6f 75 74 20 74 68 65 20 7a 54 65 72 6d  ithout the zTerm
ce3a0 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a 20 20 2a   parameter...  *
ce3b0 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 74 69  * For this routi
ce3c0 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 74 68  ne, we assume th
ce3d0 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 69 73  e UTF8 string is
ce3e0 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72   always zero-ter
ce3f0 6d 69 6e 61 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a  minated...  */..
ce400 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0d    c = *(zIn++);.
ce410 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29  .  if( c>=0xc0 )
ce420 7b 0d 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74  {..    c = sqlit
ce430 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30  e3Utf8Trans1[c-0
ce440 78 63 30 5d 3b 0d 0a 20 20 20 20 77 68 69 6c 65  xc0];..    while
ce450 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d  ( (*zIn & 0xc0)=
ce460 3d 30 78 38 30 20 29 7b 0d 0a 20 20 20 20 20 20  =0x80 ){..      
ce470 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78  c = (c<<6) + (0x
ce480 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0d  3f & *(zIn++));.
ce490 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
ce4a0 63 3c 30 78 38 30 0d 0a 20 20 20 20 20 20 20 20  c<0x80..        
ce4b0 7c 7c 20 28 63 26 30 78 46 46 46 46 46 38 30 30  || (c&0xFFFFF800
ce4c0 29 3d 3d 30 78 44 38 30 30 0d 0a 20 20 20 20 20  )==0xD800..     
ce4d0 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46     || (c&0xFFFFF
ce4e0 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20  FFE)==0xFFFE ){ 
ce4f0 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 0d 0a   c = 0xFFFD; }..
ce500 20 20 7d 0d 0a 20 20 2a 70 7a 4e 65 78 74 20 3d    }..  *pzNext =
ce510 20 7a 49 6e 3b 0d 0a 20 20 72 65 74 75 72 6e 20   zIn;..  return 
ce520 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 0d 0a 0d 0a 2f  c;..}........../
ce530 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41  *..** If the TRA
ce540 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63  NSLATE_TRACE mac
ce550 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  ro is defined, t
ce560 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68  he value of each
ce570 20 4d 65 6d 20 69 73 0d 0a 2a 2a 20 70 72 69 6e   Mem is..** prin
ce580 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e  ted on stderr on
ce590 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e   the way into an
ce5a0 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33  d out of sqlite3
ce5b0 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65  VdbeMemTranslate
ce5c0 28 29 2e 0d 0a 2a 2f 20 0d 0a 2f 2a 20 23 64 65  ()...*/ ../* #de
ce5d0 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54  fine TRANSLATE_T
ce5e0 52 41 43 45 20 31 20 2a 2f 0d 0a 0d 0a 23 69 66  RACE 1 */....#if
ce5f0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
ce600 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a 2a 20 54  _UTF16../*..** T
ce610 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e  his routine tran
ce620 73 66 6f 72 6d 73 20 74 68 65 20 69 6e 74 65 72  sforms the inter
ce630 6e 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  nal text encodin
ce640 67 20 75 73 65 64 20 62 79 20 70 4d 65 6d 20 74  g used by pMem t
ce650 6f 0d 0a 2a 2a 20 64 65 73 69 72 65 64 45 6e 63  o..** desiredEnc
ce660 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72  . It is an error
ce670 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   if the string i
ce680 73 20 61 6c 72 65 61 64 79 20 6f 66 20 74 68 65  s already of the
ce690 20 64 65 73 69 72 65 64 0d 0a 2a 2a 20 65 6e 63   desired..** enc
ce6a0 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20 2a 70 4d  oding, or if *pM
ce6b0 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  em does not cont
ce6c0 61 69 6e 20 61 20 73 74 72 69 6e 67 20 76 61 6c  ain a string val
ce6d0 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ue...*/..SQLITE_
ce6e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ce6f0 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
ce700 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 75  ate(Mem *pMem, u
ce710 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0d 0a  8 desiredEnc){..
ce720 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20    int len;      
ce730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ce740 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
ce750 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67  of output string
ce760 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20   in bytes */..  
ce770 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
ce780 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
ce790 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
ce7a0 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 75 6e 73  buffer */..  uns
ce7b0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
ce7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce7d0 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72     /* Input iter
ce7e0 61 74 6f 72 20 2a 2f 0d 0a 20 20 75 6e 73 69 67  ator */..  unsig
ce7f0 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  ned char *zTerm;
ce800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce810 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74   /* End of input
ce820 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20   */..  unsigned 
ce830 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
ce840 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ce850 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20  Output iterator 
ce860 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  */..  unsigned i
ce870 6e 74 20 63 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  nt c;....  asser
ce880 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
ce890 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
ce8a0 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
ce8b0 75 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65  utex) );..  asse
ce8c0 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  rt( pMem->flags&
ce8d0 4d 45 4d 5f 53 74 72 20 29 3b 0d 0a 20 20 61 73  MEM_Str );..  as
ce8e0 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21  sert( pMem->enc!
ce8f0 3d 64 65 73 69 72 65 64 45 6e 63 20 29 3b 0d 0a  =desiredEnc );..
ce900 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
ce910 65 6e 63 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  enc!=0 );..  ass
ce920 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20  ert( pMem->n>=0 
ce930 29 3b 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  );....#if define
ce940 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  d(TRANSLATE_TRAC
ce950 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
ce960 4c 49 54 45 5f 44 45 42 55 47 29 0d 0a 20 20 7b  LITE_DEBUG)..  {
ce970 0d 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  ..    char zBuf[
ce980 31 30 30 5d 3b 0d 0a 20 20 20 20 73 71 6c 69 74  100];..    sqlit
ce990 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
ce9a0 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29  rint(pMem, zBuf)
ce9b0 3b 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ;..    fprintf(s
ce9c0 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20  tderr, "INPUT:  
ce9d0 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0d 0a 20  %s\n", zBuf);.. 
ce9e0 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
ce9f0 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c  /* If the transl
cea00 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e  ation is between
cea10 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61   UTF-16 little a
cea20 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74  nd big endian, t
cea30 68 65 6e 20 0d 0a 20 20 2a 2a 20 61 6c 6c 20 74  hen ..  ** all t
cea40 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20  hat is required 
cea50 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 62  is to swap the b
cea60 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20  yte order. This 
cea70 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0d  case is handled.
cea80 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c  .  ** differentl
cea90 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  y from the other
ceaa0 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  s...  */..  if( 
ceab0 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54  pMem->enc!=SQLIT
ceac0 45 5f 55 54 46 38 20 26 26 20 64 65 73 69 72 65  E_UTF8 && desire
cead0 64 45 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc!=SQLITE_UTF
ceae0 38 20 29 7b 0d 0a 20 20 20 20 75 38 20 74 65 6d  8 ){..    u8 tem
ceaf0 70 3b 0d 0a 20 20 20 20 69 6e 74 20 72 63 3b 0d  p;..    int rc;.
ceb00 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
ceb10 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
ceb20 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0d 0a 20 20  eable(pMem);..  
ceb30 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ceb40 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 61 73  _OK ){..      as
ceb50 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
ceb60 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20 20 20 20  _NOMEM );..     
ceb70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
ceb80 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  OMEM;..    }..  
ceb90 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65    zIn = (u8*)pMe
ceba0 6d 2d 3e 7a 3b 0d 0a 20 20 20 20 7a 54 65 72 6d  m->z;..    zTerm
cebb0 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26   = &zIn[pMem->n&
cebc0 7e 31 5d 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  ~1];..    while(
cebd0 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0d 0a 20   zIn<zTerm ){.. 
cebe0 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e       temp = *zIn
cebf0 3b 0d 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20  ;..      *zIn = 
cec00 2a 28 7a 49 6e 2b 31 29 3b 0d 0a 20 20 20 20 20  *(zIn+1);..     
cec10 20 7a 49 6e 2b 2b 3b 0d 0a 20 20 20 20 20 20 2a   zIn++;..      *
cec20 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0d 0a 20  zIn++ = temp;.. 
cec30 20 20 20 7d 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e     }..    pMem->
cec40 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63  enc = desiredEnc
cec50 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e  ;..    goto tran
cec60 73 6c 61 74 65 5f 6f 75 74 3b 0d 0a 20 20 7d 0d  slate_out;..  }.
cec70 0a 0d 0a 20 20 2f 2a 20 53 65 74 20 6c 65 6e 20  ...  /* Set len 
cec80 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  to the maximum n
cec90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
ceca0 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f  equired in the o
cecb0 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f  utput buffer. */
cecc0 0d 0a 20 20 69 66 28 20 64 65 73 69 72 65 64 45  ..  if( desiredE
cecd0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
cece0 29 7b 0d 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20  ){..    /* When 
cecf0 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20  converting from 
ced00 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 78 69  UTF-16, the maxi
ced10 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 75 6c  mum growth resul
ced20 74 73 20 66 72 6f 6d 0d 0a 20 20 20 20 2a 2a 20  ts from..    ** 
ced30 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d  translating a 2-
ced40 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
ced50 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d 38  o a 4-byte UTF-8
ced60 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 20 20 20   character...   
ced70 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74   ** A single byt
ced80 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
ced90 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72  r the output str
ceda0 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d  ing..    ** nul-
cedb0 74 65 72 6d 69 6e 61 74 6f 72 2e 0d 0a 20 20 20  terminator...   
cedc0 20 2a 2f 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e   */..    pMem->n
cedd0 20 26 3d 20 7e 31 3b 0d 0a 20 20 20 20 6c 65 6e   &= ~1;..    len
cede0 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b   = pMem->n * 2 +
cedf0 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   1;..  }else{.. 
cee00 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65     /* When conve
cee10 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38  rting from UTF-8
cee20 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d   to UTF-16 the m
cee30 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73  aximum growth is
cee40 20 63 61 75 73 65 64 0d 0a 20 20 20 20 2a 2a 20   caused..    ** 
cee50 77 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54  when a 1-byte UT
cee60 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73  F-8 character is
cee70 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
cee80 20 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36   a 2-byte UTF-16
cee90 0d 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74  ..    ** charact
ceea0 65 72 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72  er. Two bytes ar
ceeb0 65 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68  e required in th
ceec0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  e output buffer 
ceed0 66 6f 72 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  for the..    ** 
ceee0 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0d  nul-terminator..
ceef0 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 6c 65 6e  .    */..    len
cef00 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b   = pMem->n * 2 +
cef10 20 32 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   2;..  }....  /*
cef20 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e   Set zIn to poin
cef30 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  t at the start o
cef40 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66  f the input buff
cef50 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20  er and zTerm to 
cef60 70 6f 69 6e 74 20 31 0d 0a 20 20 2a 2a 20 62 79  point 1..  ** by
cef70 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e  te past the end.
cef80 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 56 61 72  ..  **..  ** Var
cef90 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65  iable zOut is se
cefa0 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
cefb0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c  e output buffer,
cefc0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0d   space obtained.
cefd0 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  .  ** from sqlit
cefe0 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 20 20  e3_malloc()...  
ceff0 2a 2f 0d 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a  */..  zIn = (u8*
cf000 29 70 4d 65 6d 2d 3e 7a 3b 0d 0a 20 20 7a 54 65  )pMem->z;..  zTe
cf010 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e  rm = &zIn[pMem->
cf020 6e 5d 3b 0d 0a 20 20 7a 4f 75 74 20 3d 20 73 71  n];..  zOut = sq
cf030 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
cf040 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b  (pMem->db, len);
cf050 0d 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b  ..  if( !zOut ){
cf060 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
cf070 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d  ITE_NOMEM;..  }.
cf080 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0d 0a 0d 0a  .  z = zOut;....
cf090 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
cf0a0 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0d  =SQLITE_UTF8 ){.
cf0b0 0a 20 20 20 20 69 66 28 20 64 65 73 69 72 65 64  .    if( desired
cf0c0 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  Enc==SQLITE_UTF1
cf0d0 36 4c 45 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  6LE ){..      /*
cf0e0 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36   UTF-8 -> UTF-16
cf0f0 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a   Little-endian *
cf100 2f 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  /..      while( 
cf110 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0d 0a 20 20  zIn<zTerm ){..  
cf120 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c        /* c = sql
cf130 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e  ite3Utf8Read(zIn
cf140 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
cf150 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0d 0a  u8**)&zIn); */..
cf160 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
cf170 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29  8(zIn, zTerm, c)
cf180 3b 0d 0a 20 20 20 20 20 20 20 20 57 52 49 54 45  ;..        WRITE
cf190 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0d  _UTF16LE(z, c);.
cf1a0 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65  .      }..    }e
cf1b0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  lse{..      asse
cf1c0 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d  rt( desiredEnc==
cf1d0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
cf1e0 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  ;..      /* UTF-
cf1f0 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d  8 -> UTF-16 Big-
cf200 65 6e 64 69 61 6e 20 2a 2f 0d 0a 20 20 20 20 20  endian */..     
cf210 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
cf220 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  m ){..        /*
cf230 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38   c = sqlite3Utf8
cf240 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c  Read(zIn, zTerm,
cf250 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49   (const u8**)&zI
cf260 6e 29 3b 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  n); */..        
cf270 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a  READ_UTF8(zIn, z
cf280 54 65 72 6d 2c 20 63 29 3b 0d 0a 20 20 20 20 20  Term, c);..     
cf290 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 45     WRITE_UTF16BE
cf2a0 28 7a 2c 20 63 29 3b 0d 0a 20 20 20 20 20 20 7d  (z, c);..      }
cf2b0 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4d 65  ..    }..    pMe
cf2c0 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d  m->n = (int)(z -
cf2d0 20 7a 4f 75 74 29 3b 0d 0a 20 20 20 20 2a 7a 2b   zOut);..    *z+
cf2e0 2b 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b  + = 0;..  }else{
cf2f0 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65  ..    assert( de
cf300 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
cf310 5f 55 54 46 38 20 29 3b 0d 0a 20 20 20 20 69 66  _UTF8 );..    if
cf320 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c  ( pMem->enc==SQL
cf330 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0d 0a  ITE_UTF16LE ){..
cf340 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20        /* UTF-16 
cf350 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e  Little-endian ->
cf360 20 55 54 46 2d 38 20 2a 2f 0d 0a 20 20 20 20 20   UTF-8 */..     
cf370 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
cf380 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 52 45  m ){..        RE
cf390 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20  AD_UTF16LE(zIn, 
cf3a0 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0d  zIn<zTerm, c); .
cf3b0 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
cf3c0 54 46 38 28 7a 2c 20 63 29 3b 0d 0a 20 20 20 20  TF8(z, c);..    
cf3d0 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d    }..    }else{.
cf3e0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36  .      /* UTF-16
cf3f0 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55   Big-endian -> U
cf400 54 46 2d 38 20 2a 2f 0d 0a 20 20 20 20 20 20 77  TF-8 */..      w
cf410 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
cf420 29 7b 0d 0a 20 20 20 20 20 20 20 20 52 45 41 44  ){..        READ
cf430 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 7a 49  _UTF16BE(zIn, zI
cf440 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0d 0a 20  n<zTerm, c); .. 
cf450 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46         WRITE_UTF
cf460 38 28 7a 2c 20 63 29 3b 0d 0a 20 20 20 20 20 20  8(z, c);..      
cf470 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4d  }..    }..    pM
cf480 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20  em->n = (int)(z 
cf490 2d 20 7a 4f 75 74 29 3b 0d 0a 20 20 7d 0d 0a 20  - zOut);..  }.. 
cf4a0 20 2a 7a 20 3d 20 30 3b 0d 0a 20 20 61 73 73 65   *z = 0;..  asse
cf4b0 72 74 28 20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65  rt( (pMem->n+(de
cf4c0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
cf4d0 5f 55 54 46 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e  _UTF8?1:2))<=len
cf4e0 20 29 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33   );....  sqlite3
cf4f0 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
cf500 4d 65 6d 29 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 66  Mem);..  pMem->f
cf510 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 53 74  lags &= ~(MEM_St
cf520 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  atic|MEM_Dyn|MEM
cf530 5f 45 70 68 65 6d 29 3b 0d 0a 20 20 70 4d 65 6d  _Ephem);..  pMem
cf540 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45  ->enc = desiredE
cf550 6e 63 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  nc;..  pMem->fla
cf560 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c  gs |= (MEM_Term|
cf570 4d 45 4d 5f 44 79 6e 29 3b 0d 0a 20 20 70 4d 65  MEM_Dyn);..  pMe
cf580 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f  m->z = (char*)zO
cf590 75 74 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61  ut;..  pMem->zMa
cf5a0 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0d  lloc = pMem->z;.
cf5b0 0a 0d 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74  ...translate_out
cf5c0 3a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  :..#if defined(T
cf5d0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20  RANSLATE_TRACE) 
cf5e0 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
cf5f0 45 5f 44 45 42 55 47 29 0d 0a 20 20 7b 0d 0a 20  E_DEBUG)..  {.. 
cf600 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
cf610 5d 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ];..    sqlite3V
cf620 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
cf630 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0d 0a  t(pMem, zBuf);..
cf640 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
cf650 72 72 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c  rr, "OUTPUT: %s\
cf660 6e 22 2c 20 7a 42 75 66 29 3b 0d 0a 20 20 7d 0d  n", zBuf);..  }.
cf670 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65 74 75 72  .#endif..  retur
cf680 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
cf690 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  .../*..** This r
cf6a0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f  outine checks fo
cf6b0 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d  r a byte-order m
cf6c0 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e  ark at the begin
cf6d0 6e 69 6e 67 20 6f 66 20 74 68 65 20 0d 0a 2a 2a  ning of the ..**
cf6e0 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73   UTF-16 string s
cf6f0 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20  tored in *pMem. 
cf700 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e  If one is presen
cf710 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64  t, it is removed
cf720 20 61 6e 64 0d 0a 2a 2a 20 74 68 65 20 65 6e 63   and..** the enc
cf730 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d  oding of the Mem
cf740 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20   adjusted. This 
cf750 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74  routine does not
cf760 20 64 6f 20 61 6e 79 0d 0a 2a 2a 20 62 79 74 65   do any..** byte
cf770 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75  -swapping, it ju
cf780 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20  st sets Mem.enc 
cf790 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0d 0a  appropriately...
cf7a0 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63  **..** The alloc
cf7b0 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64  ation (static, d
cf7c0 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64  ynamic etc.) and
cf7d0 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
cf7e0 20 4d 65 6d 20 6d 61 79 20 62 65 0d 0a 2a 2a 20   Mem may be..** 
cf7f0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
cf800 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53  function...*/..S
cf810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
cf820 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
cf830 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70  HandleBom(Mem *p
cf840 4d 65 6d 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20  Mem){..  int rc 
cf850 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
cf860 75 38 20 62 6f 6d 20 3d 20 30 3b 0d 0a 0d 0a 20  u8 bom = 0;.... 
cf870 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e   assert( pMem->n
cf880 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 4d  >=0 );..  if( pM
cf890 65 6d 2d 3e 6e 3e 31 20 29 7b 0d 0a 20 20 20 20  em->n>1 ){..    
cf8a0 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70  u8 b1 = *(u8 *)p
cf8b0 4d 65 6d 2d 3e 7a 3b 0d 0a 20 20 20 20 75 38 20  Mem->z;..    u8 
cf8c0 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d  b2 = *(((u8 *)pM
cf8d0 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0d 0a 20 20  em->z) + 1);..  
cf8e0 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26    if( b1==0xFE &
cf8f0 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0d 0a 20  & b2==0xFF ){.. 
cf900 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54       bom = SQLIT
cf910 45 5f 55 54 46 31 36 42 45 3b 0d 0a 20 20 20 20  E_UTF16BE;..    
cf920 7d 0d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30  }..    if( b1==0
cf930 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 20  xFF && b2==0xFE 
cf940 29 7b 0d 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20  ){..      bom = 
cf950 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0d  SQLITE_UTF16LE;.
cf960 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d  .    }..  }..  .
cf970 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0d 0a 20  .  if( bom ){.. 
cf980 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
cf990 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
cf9a0 62 6c 65 28 70 4d 65 6d 29 3b 0d 0a 20 20 20 20  ble(pMem);..    
cf9b0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
cf9c0 4b 20 29 7b 0d 0a 20 20 20 20 20 20 70 4d 65 6d  K ){..      pMem
cf9d0 2d 3e 6e 20 2d 3d 20 32 3b 0d 0a 20 20 20 20 20  ->n -= 2;..     
cf9e0 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a   memmove(pMem->z
cf9f0 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70  , &pMem->z[2], p
cfa00 4d 65 6d 2d 3e 6e 29 3b 0d 0a 20 20 20 20 20 20  Mem->n);..      
cfa10 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
cfa20 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20 20 20   = '\0';..      
cfa30 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b  pMem->z[pMem->n+
cfa40 31 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20  1] = '\0';..    
cfa50 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
cfa60 20 4d 45 4d 5f 54 65 72 6d 3b 0d 0a 20 20 20 20   MEM_Term;..    
cfa70 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f    pMem->enc = bo
cfa80 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  m;..    }..  }..
cfa90 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
cfaa0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
cfab0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d  E_OMIT_UTF16 */.
cfac0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 70 5a 20 69 73 20  .../*..** pZ is 
cfad0 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  a UTF-8 encoded 
cfae0 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20  unicode string. 
cfaf0 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73  If nByte is less
cfb00 20 74 68 61 6e 20 7a 65 72 6f 2c 0d 0a 2a 2a 20   than zero,..** 
cfb10 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
cfb20 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61  r of unicode cha
cfb30 72 61 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70  racters in pZ up
cfb40 20 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63   to (but not inc
cfb50 6c 75 64 69 6e 67 29 0d 0a 2a 2a 20 74 68 65 20  luding)..** the 
cfb60 66 69 72 73 74 20 30 78 30 30 20 62 79 74 65 2e  first 0x00 byte.
cfb70 20 49 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 74   If nByte is not
cfb80 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
cfb90 20 72 65 74 75 72 6e 20 74 68 65 0d 0a 2a 2a 20   return the..** 
cfba0 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64  number of unicod
cfbb0 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  e characters in 
cfbc0 74 68 65 20 66 69 72 73 74 20 6e 42 79 74 65 20  the first nByte 
cfbd0 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74 6f 20  of pZ (or up to 
cfbe0 0d 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30  ..** the first 0
cfbf0 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20 63  x00, whichever c
cfc00 6f 6d 65 73 20 66 69 72 73 74 29 2e 0d 0a 2a 2f  omes first)...*/
cfc10 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
cfc20 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
cfc30 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68  CharLen(const ch
cfc40 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79  ar *zIn, int nBy
cfc50 74 65 29 7b 0d 0a 20 20 69 6e 74 20 72 20 3d 20  te){..  int r = 
cfc60 30 3b 0d 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  0;..  const u8 *
cfc70 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a  z = (const u8*)z
cfc80 49 6e 3b 0d 0a 20 20 63 6f 6e 73 74 20 75 38 20  In;..  const u8 
cfc90 2a 7a 54 65 72 6d 3b 0d 0a 20 20 69 66 28 20 6e  *zTerm;..  if( n
cfca0 42 79 74 65 3e 3d 30 20 29 7b 0d 0a 20 20 20 20  Byte>=0 ){..    
cfcb0 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65  zTerm = &z[nByte
cfcc0 5d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ];..  }else{..  
cfcd0 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74    zTerm = (const
cfce0 20 75 38 2a 29 28 2d 31 29 3b 0d 0a 20 20 7d 0d   u8*)(-1);..  }.
cfcf0 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54  .  assert( z<=zT
cfd00 65 72 6d 20 29 3b 0d 0a 20 20 77 68 69 6c 65 28  erm );..  while(
cfd10 20 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72   *z!=0 && z<zTer
cfd20 6d 20 29 7b 0d 0a 20 20 20 20 53 51 4c 49 54 45  m ){..    SQLITE
cfd30 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0d 0a  _SKIP_UTF8(z);..
cfd40 20 20 20 20 72 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20      r++;..  }.. 
cfd50 20 72 65 74 75 72 6e 20 72 3b 0d 0a 7d 0d 0a 0d   return r;..}...
cfd60 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66 75  ./* This test fu
cfd70 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75  nction is not cu
cfd80 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79 20  rrently used by 
cfd90 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74 65  the automated te
cfda0 73 74 2d 73 75 69 74 65 2e 20 0d 0a 2a 2a 20 48  st-suite. ..** H
cfdb0 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20  ence it is only 
cfdc0 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62  available in deb
cfdd0 75 67 20 62 75 69 6c 64 73 2e 0d 0a 2a 2f 0d 0a  ug builds...*/..
cfde0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
cfdf0 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69  TE_TEST) && defi
cfe00 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
cfe10 29 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72 61 6e 73 6c  )../*..** Transl
cfe20 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46  ate UTF-8 to UTF
cfe30 2d 38 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  -8...**..** This
cfe40 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
cfe50 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74  of making sure t
cfe60 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69  hat the string i
cfe70 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0d 0a 2a  s well-formed..*
cfe80 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64  * UTF-8.  Miscod
cfe90 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72  ed characters ar
cfea0 65 20 72 65 6d 6f 76 65 64 2e 0d 0a 2a 2a 0d 0a  e removed...**..
cfeb0 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69  ** The translati
cfec0 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c  on is done in-pl
cfed0 61 63 65 20 61 6e 64 20 61 62 6f 72 74 65 64 20  ace and aborted 
cfee0 69 66 20 74 68 65 20 6f 75 74 70 75 74 0d 0a 2a  if the output..*
cfef0 2a 20 6f 76 65 72 72 75 6e 73 20 74 68 65 20 69  * overruns the i
cff00 6e 70 75 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  nput...*/..SQLIT
cff10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
cff20 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73  lite3Utf8To8(uns
cff30 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 29  igned char *zIn)
cff40 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  {..  unsigned ch
cff50 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0d  ar *zOut = zIn;.
cff60 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
cff70 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0d   *zStart = zIn;.
cff80 0a 20 20 75 33 32 20 63 3b 0d 0a 0d 0a 20 20 77  .  u32 c;....  w
cff90 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 26 26 20  hile( zIn[0] && 
cffa0 7a 4f 75 74 3c 3d 7a 49 6e 20 29 7b 0d 0a 20 20  zOut<=zIn ){..  
cffb0 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
cffc0 38 52 65 61 64 28 7a 49 6e 2c 20 28 63 6f 6e 73  8Read(zIn, (cons
cffd0 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0d 0a 20  t u8**)&zIn);.. 
cffe0 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 64     if( c!=0xfffd
cfff0 20 29 7b 0d 0a 20 20 20 20 20 20 57 52 49 54 45   ){..      WRITE
d0000 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0d  _UTF8(zOut, c);.
d0010 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 2a  .    }..  }..  *
d0020 7a 4f 75 74 20 3d 20 30 3b 0d 0a 20 20 72 65 74  zOut = 0;..  ret
d0030 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d  urn (int)(zOut -
d0040 20 7a 53 74 61 72 74 29 3b 0d 0a 7d 0d 0a 23 65   zStart);..}..#e
d0050 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ndif....#ifndef 
d0060 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
d0070 36 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72  6../*..** Conver
d0080 74 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  t a UTF-16 strin
d0090 67 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  g in the native 
d00a0 65 6e 63 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20  encoding into a 
d00b0 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0d 0a 2a  UTF-8 string...*
d00c0 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64  * Memory to hold
d00d0 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   the UTF-8 strin
d00e0 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
d00f0 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
d0100 63 20 61 6e 64 20 6d 75 73 74 0d 0a 2a 2a 20 62  c and must..** b
d0110 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
d0120 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
d0130 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 55 4c 4c 20 69 73  ..**..** NULL is
d0140 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
d0150 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 61 74  re is an allocat
d0160 69 6f 6e 20 65 72 72 6f 72 2e 0d 0a 2a 2f 0d 0a  ion error...*/..
d0170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
d0180 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31  har *sqlite3Utf1
d0190 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62  6to8(sqlite3 *db
d01a0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
d01b0 20 69 6e 74 20 6e 42 79 74 65 2c 20 75 38 20 65   int nByte, u8 e
d01c0 6e 63 29 7b 0d 0a 20 20 4d 65 6d 20 6d 3b 0d 0a  nc){..  Mem m;..
d01d0 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20    memset(&m, 0, 
d01e0 73 69 7a 65 6f 66 28 6d 29 29 3b 0d 0a 20 20 6d  sizeof(m));..  m
d01f0 2e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 73 71 6c  .db = db;..  sql
d0200 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
d0210 72 28 26 6d 2c 20 7a 2c 20 6e 42 79 74 65 2c 20  r(&m, z, nByte, 
d0220 65 6e 63 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  enc, SQLITE_STAT
d0230 49 43 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  IC);..  sqlite3V
d0240 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
d0250 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46  g(&m, SQLITE_UTF
d0260 38 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  8);..  if( db->m
d0270 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a  allocFailed ){..
d0280 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
d0290 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0d 0a  emRelease(&m);..
d02a0 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0d 0a 20 20      m.z = 0;..  
d02b0 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e  }..  assert( (m.
d02c0 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d  flags & MEM_Term
d02d0 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  )!=0 || db->mall
d02e0 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 61  ocFailed );..  a
d02f0 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 20  ssert( (m.flags 
d0300 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c  & MEM_Str)!=0 ||
d0310 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
d0320 64 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  d );..  assert( 
d0330 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  (m.flags & MEM_D
d0340 79 6e 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  yn)!=0 || db->ma
d0350 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20  llocFailed );.. 
d0360 20 61 73 73 65 72 74 28 20 6d 2e 7a 20 7c 7c 20   assert( m.z || 
d0370 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
d0380 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6d 2e   );..  return m.
d0390 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  z;..}..../*..** 
d03a0 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20  Convert a UTF-8 
d03b0 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20 55 54  string to the UT
d03c0 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 73 70  F-16 encoding sp
d03d0 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
d03e0 65 74 65 72 0d 0a 2a 2a 20 65 6e 63 2e 20 41 20  eter..** enc. A 
d03f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
d0400 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  ew string is ret
d0410 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20 76  urned, and the v
d0420 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0d 0a  alue of *pnOut..
d0430 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
d0440 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72   length of the r
d0450 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
d0460 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c  n bytes. The cal
d0470 6c 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 61 72 72  l should..** arr
d0480 61 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c  ange to call sql
d0490 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20  ite3DbFree() on 
d04a0 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69  the returned poi
d04b0 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0d  nter when it is.
d04c0 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65  .** no longer re
d04d0 71 75 69 72 65 64 2e 0d 0a 2a 2a 20 0d 0a 2a 2a  quired...** ..**
d04e0 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   If a malloc fai
d04f0 6c 75 72 65 20 6f 63 63 75 72 73 2c 20 4e 55 4c  lure occurs, NUL
d0500 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  L is returned an
d0510 64 20 74 68 65 20 64 62 2e 6d 61 6c 6c 6f 63 46  d the db.mallocF
d0520 61 69 6c 65 64 0d 0a 2a 2a 20 66 6c 61 67 20 73  ailed..** flag s
d0530 65 74 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  et...*/..#ifdef 
d0540 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
d0550 41 54 33 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  AT3..SQLITE_PRIV
d0560 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
d0570 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74 65  3Utf8to16(sqlite
d0580 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 2c 20 63  3 *db, u8 enc, c
d0590 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  har *z, int n, i
d05a0 6e 74 20 2a 70 6e 4f 75 74 29 7b 0d 0a 20 20 4d  nt *pnOut){..  M
d05b0 65 6d 20 6d 3b 0d 0a 20 20 6d 65 6d 73 65 74 28  em m;..  memset(
d05c0 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
d05d0 29 3b 0d 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b  );..  m.db = db;
d05e0 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
d05f0 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20  emSetStr(&m, z, 
d0600 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  n, SQLITE_UTF8, 
d0610 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d  SQLITE_STATIC);.
d0620 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
d0630 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26  beMemTranslate(&
d0640 6d 2c 20 65 6e 63 29 20 29 7b 0d 0a 20 20 20 20  m, enc) ){..    
d0650 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
d0660 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20  ocFailed );..   
d0670 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d   return 0;..  }.
d0680 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d  .  assert( m.z==
d0690 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0d 0a 20 20  m.zMalloc );..  
d06a0 2a 70 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0d 0a 20  *pnOut = m.n;.. 
d06b0 20 72 65 74 75 72 6e 20 6d 2e 7a 3b 0d 0a 7d 0d   return m.z;..}.
d06c0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
d06d0 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 46 2d 31  * zIn is a UTF-1
d06e0 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64  6 encoded unicod
d06f0 65 20 73 74 72 69 6e 67 20 61 74 20 6c 65 61 73  e string at leas
d0700 74 20 6e 43 68 61 72 20 63 68 61 72 61 63 74 65  t nChar characte
d0710 72 73 20 6c 6f 6e 67 2e 0d 0a 2a 2a 20 52 65 74  rs long...** Ret
d0720 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
d0730 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66  f bytes in the f
d0740 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f  irst nChar unico
d0750 64 65 20 63 68 61 72 61 63 74 65 72 73 0d 0a 2a  de characters..*
d0760 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 20  * in pZ.  nChar 
d0770 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 61  must be non-nega
d0780 74 69 76 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  tive...*/..SQLIT
d0790 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
d07a0 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65  lite3Utf16ByteLe
d07b0 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49  n(const void *zI
d07c0 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0d 0a  n, int nChar){..
d07d0 20 20 69 6e 74 20 63 3b 0d 0a 20 20 75 6e 73 69    int c;..  unsi
d07e0 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
d07f0 2a 7a 20 3d 20 7a 49 6e 3b 0d 0a 20 20 69 6e 74  *z = zIn;..  int
d0800 20 6e 20 3d 20 30 3b 0d 0a 20 20 0d 0a 20 20 69   n = 0;..  ..  i
d0810 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  f( SQLITE_UTF16N
d0820 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54  ATIVE==SQLITE_UT
d0830 46 31 36 42 45 20 29 7b 0d 0a 20 20 20 20 77 68  F16BE ){..    wh
d0840 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0d  ile( n<nChar ){.
d0850 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31  .      READ_UTF1
d0860 36 42 45 28 7a 2c 20 31 2c 20 63 29 3b 0d 0a 20  6BE(z, 1, c);.. 
d0870 20 20 20 20 20 6e 2b 2b 3b 0d 0a 20 20 20 20 7d       n++;..    }
d0880 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
d0890 77 68 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29  while( n<nChar )
d08a0 7b 0d 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54  {..      READ_UT
d08b0 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29 3b 0d  F16LE(z, 1, c);.
d08c0 0a 20 20 20 20 20 20 6e 2b 2b 3b 0d 0a 20 20 20  .      n++;..   
d08d0 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
d08e0 6e 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67  n (int)(z-(unsig
d08f0 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
d0900 29 7a 49 6e 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66  )zIn);..}....#if
d0910 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
d0920 54 45 53 54 29 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  TEST)../*..** Th
d0930 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
d0940 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 43  lled from the TC
d0950 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  L test function 
d0960 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74  "translate_selft
d0970 65 73 74 22 2e 0d 0a 2a 2a 20 49 74 20 63 68 65  est"...** It che
d0980 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69  cks that the pri
d0990 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69  mitives for seri
d09a0 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65  alizing and dese
d09b0 72 69 61 6c 69 7a 69 6e 67 0d 0a 2a 2a 20 63 68  rializing..** ch
d09c0 61 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68  aracters in each
d09d0 20 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e   encoding are in
d09e0 76 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f  verses of each o
d09f0 74 68 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ther...*/..SQLIT
d0a00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
d0a10 71 6c 69 74 65 33 55 74 66 53 65 6c 66 54 65 73  qlite3UtfSelfTes
d0a20 74 28 76 6f 69 64 29 7b 0d 0a 20 20 75 6e 73 69  t(void){..  unsi
d0a30 67 6e 65 64 20 69 6e 74 20 69 2c 20 74 3b 0d 0a  gned int i, t;..
d0a40 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
d0a50 7a 42 75 66 5b 32 30 5d 3b 0d 0a 20 20 75 6e 73  zBuf[20];..  uns
d0a60 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0d 0a  igned char *z;..
d0a70 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 75 6e 73 69    int n;..  unsi
d0a80 67 6e 65 64 20 69 6e 74 20 63 3b 0d 0a 0d 0a 20  gned int c;.... 
d0a90 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30   for(i=0; i<0x00
d0aa0 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0d 0a 20  110000; i++){.. 
d0ab0 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0d 0a 20 20     z = zBuf;..  
d0ac0 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20    WRITE_UTF8(z, 
d0ad0 69 29 3b 0d 0a 20 20 20 20 6e 20 3d 20 28 69 6e  i);..    n = (in
d0ae0 74 29 28 7a 2d 7a 42 75 66 29 3b 0d 0a 20 20 20  t)(z-zBuf);..   
d0af0 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20   assert( n>0 && 
d0b00 6e 3c 3d 34 20 29 3b 0d 0a 20 20 20 20 7a 5b 30  n<=4 );..    z[0
d0b10 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7a 20 3d 20  ] = 0;..    z = 
d0b20 7a 42 75 66 3b 0d 0a 20 20 20 20 63 20 3d 20 73  zBuf;..    c = s
d0b30 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
d0b40 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a  , (const u8**)&z
d0b50 29 3b 0d 0a 20 20 20 20 74 20 3d 20 69 3b 0d 0a  );..    t = i;..
d0b60 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30      if( i>=0xD80
d0b70 30 20 26 26 20 69 3c 3d 30 78 44 46 46 46 20 29  0 && i<=0xDFFF )
d0b80 20 74 20 3d 20 30 78 46 46 46 44 3b 0d 0a 20 20   t = 0xFFFD;..  
d0b90 20 20 69 66 28 20 28 69 26 30 78 46 46 46 46 46    if( (i&0xFFFFF
d0ba0 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 20 74  FFE)==0xFFFE ) t
d0bb0 20 3d 20 30 78 46 46 46 44 3b 0d 0a 20 20 20 20   = 0xFFFD;..    
d0bc0 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29 3b 0d  assert( c==t );.
d0bd0 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
d0be0 7a 42 75 66 29 3d 3d 6e 20 29 3b 0d 0a 20 20 7d  zBuf)==n );..  }
d0bf0 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30  ..  for(i=0; i<0
d0c00 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b  x00110000; i++){
d0c10 0d 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44  ..    if( i>=0xD
d0c20 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20  800 && i<0xE000 
d0c30 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
d0c40 20 7a 20 3d 20 7a 42 75 66 3b 0d 0a 20 20 20 20   z = zBuf;..    
d0c50 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c  WRITE_UTF16LE(z,
d0c60 20 69 29 3b 0d 0a 20 20 20 20 6e 20 3d 20 28 69   i);..    n = (i
d0c70 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0d 0a 20 20  nt)(z-zBuf);..  
d0c80 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26    assert( n>0 &&
d0c90 20 6e 3c 3d 34 20 29 3b 0d 0a 20 20 20 20 7a 5b   n<=4 );..    z[
d0ca0 30 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7a 20 3d  0] = 0;..    z =
d0cb0 20 7a 42 75 66 3b 0d 0a 20 20 20 20 52 45 41 44   zBuf;..    READ
d0cc0 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63  _UTF16LE(z, 1, c
d0cd0 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
d0ce0 63 3d 3d 69 20 29 3b 0d 0a 20 20 20 20 61 73 73  c==i );..    ass
d0cf0 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e  ert( (z-zBuf)==n
d0d00 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28   );..  }..  for(
d0d10 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30  i=0; i<0x0011000
d0d20 30 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  0; i++){..    if
d0d30 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69  ( i>=0xD800 && i
d0d40 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e  <0xE000 ) contin
d0d50 75 65 3b 0d 0a 20 20 20 20 7a 20 3d 20 7a 42 75  ue;..    z = zBu
d0d60 66 3b 0d 0a 20 20 20 20 57 52 49 54 45 5f 55 54  f;..    WRITE_UT
d0d70 46 31 36 42 45 28 7a 2c 20 69 29 3b 0d 0a 20 20  F16BE(z, i);..  
d0d80 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42    n = (int)(z-zB
d0d90 75 66 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  uf);..    assert
d0da0 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b  ( n>0 && n<=4 );
d0db0 0d 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0d  ..    z[0] = 0;.
d0dc0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0d 0a  .    z = zBuf;..
d0dd0 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 45      READ_UTF16BE
d0de0 28 7a 2c 20 31 2c 20 63 29 3b 0d 0a 20 20 20 20  (z, 1, c);..    
d0df0 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0d  assert( c==i );.
d0e00 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
d0e10 7a 42 75 66 29 3d 3d 6e 20 29 3b 0d 0a 20 20 7d  zBuf)==n );..  }
d0e20 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
d0e30 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0d 0a 23  QLITE_TEST */..#
d0e40 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
d0e50 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 0d  OMIT_UTF16 */...
d0e60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d0e70 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a   End of utf.c **
d0e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d0eb0 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d0ec0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69  * Begin file uti
d0ed0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
d0ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0f00 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53  /../*..** 2001 S
d0f10 65 70 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d  eptember 15..**.
d0f20 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
d0f30 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
d0f40 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
d0f50 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
d0f60 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c  e of..** a legal
d0f70 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
d0f80 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a   a blessing:..**
d0f90 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
d0fa0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
d0fb0 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  evil...**    May
d0fc0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
d0fd0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
d0fe0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
d0ff0 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61  thers...**    Ma
d1000 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
d1010 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
d1020 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
d1030 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a  ive...**..******
d1040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1080 2a 2a 2a 0d 0a 2a 2a 20 55 74 69 6c 69 74 79 20  ***..** Utility 
d1090 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
d10a0 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65  hroughout sqlite
d10b0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  ...**..** This f
d10c0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
d10d0 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63  ctions for alloc
d10e0 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f  ating memory, co
d10f0 6d 70 61 72 69 6e 67 0d 0a 2a 2a 20 73 74 72 69  mparing..** stri
d1100 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c  ngs, and stuff l
d1110 69 6b 65 20 74 68 61 74 2e 0d 0a 2a 2a 0d 0a 2a  ike that...**..*
d1120 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  /../* #include <
d1130 73 74 64 61 72 67 2e 68 3e 20 2a 2f 0d 0a 23 69  stdarg.h> */..#i
d1140 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45  fdef SQLITE_HAVE
d1150 5f 49 53 4e 41 4e 0d 0a 23 20 69 6e 63 6c 75 64  _ISNAN..# includ
d1160 65 20 3c 6d 61 74 68 2e 68 3e 0d 0a 23 65 6e 64  e <math.h>..#end
d1170 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 75  if..../*..** Rou
d1180 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73  tine needed to s
d1190 75 70 70 6f 72 74 20 74 68 65 20 74 65 73 74 63  upport the testc
d11a0 61 73 65 28 29 20 6d 61 63 72 6f 2e 0d 0a 2a 2f  ase() macro...*/
d11b0 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
d11c0 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0d 0a 53  COVERAGE_TEST..S
d11d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
d11e0 69 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61  id sqlite3Covera
d11f0 67 65 28 69 6e 74 20 78 29 7b 0d 0a 20 20 73 74  ge(int x){..  st
d1200 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 64 75  atic unsigned du
d1210 6d 6d 79 20 3d 20 30 3b 0d 0a 20 20 64 75 6d 6d  mmy = 0;..  dumm
d1220 79 20 2b 3d 20 28 75 6e 73 69 67 6e 65 64 29 78  y += (unsigned)x
d1230 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
d1240 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
d1250 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
d1260 4e 54 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  NT../*..** Retur
d1270 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 6c  n true if the fl
d1280 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
d1290 75 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62  ue is Not a Numb
d12a0 65 72 20 28 4e 61 4e 29 2e 0d 0a 2a 2a 0d 0a 2a  er (NaN)...**..*
d12b0 2a 20 55 73 65 20 74 68 65 20 6d 61 74 68 20 6c  * Use the math l
d12c0 69 62 72 61 72 79 20 69 73 6e 61 6e 28 29 20 66  ibrary isnan() f
d12d0 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69  unction if compi
d12e0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  led with SQLITE_
d12f0 48 41 56 45 5f 49 53 4e 41 4e 2e 0d 0a 2a 2a 20  HAVE_ISNAN...** 
d1300 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 68 61  Otherwise, we ha
d1310 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 6c 65  ve our own imple
d1320 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 77  mentation that w
d1330 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 79 73  orks on most sys
d1340 74 65 6d 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  tems...*/..SQLIT
d1350 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
d1360 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c  lite3IsNaN(doubl
d1370 65 20 78 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  e x){..  int rc;
d1380 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
d1390 72 65 74 75 72 6e 20 2a 2f 0d 0a 23 69 66 20 21  return */..#if !
d13a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48  defined(SQLITE_H
d13b0 41 56 45 5f 49 53 4e 41 4e 29 0d 0a 20 20 2f 2a  AVE_ISNAN)..  /*
d13c0 0d 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 20 74  ..  ** Systems t
d13d0 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 65 20  hat support the 
d13e0 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 79 20  isnan() library 
d13f0 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
d1400 70 72 6f 62 61 62 6c 79 0d 0a 20 20 2a 2a 20 6d  probably..  ** m
d1410 61 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 79  ake use of it by
d1420 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20   compiling with 
d1430 2d 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  -DSQLITE_HAVE_IS
d1440 4e 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 76  NAN.  But we hav
d1450 65 0d 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68  e..  ** found th
d1460 61 74 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20  at many systems 
d1470 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f  do not have a wo
d1480 72 6b 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75  rking isnan() fu
d1490 6e 63 74 69 6f 6e 20 73 6f 0d 0a 20 20 2a 2a 20  nction so..  ** 
d14a0 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  this implementat
d14b0 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 20  ion is provided 
d14c0 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  as an alternativ
d14d0 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54  e...  **..  ** T
d14e0 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 6f 6d  his NaN test som
d14f0 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 66 20  etimes fails if 
d1500 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 43 20  compiled on GCC 
d1510 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 74 68  with -ffast-math
d1520 2e 0d 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f  ...  ** On the o
d1530 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 75  ther hand, the u
d1540 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 74  se of -ffast-mat
d1550 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 65  h comes with the
d1560 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 20 20 2a 2a   following..  **
d1570 20 77 61 72 6e 69 6e 67 3a 0d 0a 20 20 2a 2a 0d   warning:..  **.
d1580 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20  .  **      This 
d1590 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d  option [-ffast-m
d15a0 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65  ath] should neve
d15b0 72 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62  r be turned on b
d15c0 79 20 61 6e 79 0d 0a 20 20 2a 2a 20 20 20 20 20  y any..  **     
d15d0 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65   -O option since
d15e0 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 20 69   it can result i
d15f0 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 74 70  n incorrect outp
d1600 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d 73 0d  ut for programs.
d1610 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 68  .  **      which
d1620 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78   depend on an ex
d1630 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  act implementati
d1640 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 53  on of IEEE or IS
d1650 4f 20 0d 0a 20 20 2a 2a 20 20 20 20 20 20 72 75  O ..  **      ru
d1660 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f  les/specificatio
d1670 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63  ns for math func
d1680 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  tions...  **..  
d1690 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74  ** Under MSVC, t
d16a0 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79  his NaN test may
d16b0 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65   fail if compile
d16c0 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e  d with a floatin
d16d0 67 2d 0d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70  g-..  ** point p
d16e0 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74  recision mode ot
d16f0 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65  her than /fp:pre
d1700 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20  cise.  From the 
d1710 4d 53 44 4e 20 0d 0a 20 20 2a 2a 20 64 6f 63 75  MSDN ..  ** docu
d1720 6d 65 6e 74 61 74 69 6f 6e 3a 0d 0a 20 20 2a 2a  mentation:..  **
d1730 0d 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20  ..  **      The 
d1740 63 6f 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f  compiler [with /
d1750 66 70 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c  fp:precise] will
d1760 20 70 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65   properly handle
d1770 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 0d 0a 20   comparisons .. 
d1780 20 2a 2a 20 20 20 20 20 20 69 6e 76 6f 6c 76 69   **      involvi
d1790 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d  ng NaN. For exam
d17a0 70 6c 65 2c 20 78 20 21 3d 20 78 20 65 76 61 6c  ple, x != x eval
d17b0 75 61 74 65 73 20 74 6f 20 74 72 75 65 20 69 66  uates to true if
d17c0 20 78 20 69 73 20 4e 61 4e 20 0d 0a 20 20 2a 2a   x is NaN ..  **
d17d0 20 20 20 20 20 20 2e 2e 2e 0d 0a 20 20 2a 2f 0d        .....  */.
d17e0 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f 4d  .#ifdef __FAST_M
d17f0 41 54 48 5f 5f 0d 0a 23 20 65 72 72 6f 72 20 53  ATH__..# error S
d1800 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77  QLite will not w
d1810 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69  ork correctly wi
d1820 74 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61  th the -ffast-ma
d1830 74 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43  th option of GCC
d1840 2e 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 76 6f 6c  ...#endif..  vol
d1850 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d  atile double y =
d1860 20 78 3b 0d 0a 20 20 76 6f 6c 61 74 69 6c 65 20   x;..  volatile 
d1870 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0d 0a 20  double z = y;.. 
d1880 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0d 0a 23   rc = (y!=z);..#
d1890 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69  else  /* if defi
d18a0 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f  ned(SQLITE_HAVE_
d18b0 49 53 4e 41 4e 29 20 2a 2f 0d 0a 20 20 72 63 20  ISNAN) */..  rc 
d18c0 3d 20 69 73 6e 61 6e 28 78 29 3b 0d 0a 23 65 6e  = isnan(x);..#en
d18d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41  dif /* SQLITE_HA
d18e0 56 45 5f 49 53 4e 41 4e 20 2a 2f 0d 0a 20 20 74  VE_ISNAN */..  t
d18f0 65 73 74 63 61 73 65 28 20 72 63 20 29 3b 0d 0a  estcase( rc );..
d1900 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
d1910 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
d1920 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
d1930 50 4f 49 4e 54 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  POINT */..../*..
d1940 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72  ** Compute a str
d1950 69 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20  ing length that 
d1960 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68  is limited to wh
d1970 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  at can be stored
d1980 20 69 6e 0d 0a 2a 2a 20 6c 6f 77 65 72 20 33 30   in..** lower 30
d1990 20 62 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69   bits of a 32-bi
d19a0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
d19b0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 76 61  ...**..** The va
d19c0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c  lue returned wil
d19d0 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 61 74  l never be negat
d19e0 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 69  ive.  Nor will i
d19f0 74 20 65 76 65 72 20 62 65 20 67 72 65 61 74 65  t ever be greate
d1a00 72 0d 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61  r..** than the a
d1a10 63 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 20  ctual length of 
d1a20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  the string.  For
d1a30 20 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 6e   very long strin
d1a40 67 73 20 28 67 72 65 61 74 65 72 0d 0a 2a 2a 20  gs (greater..** 
d1a50 74 68 61 6e 20 31 47 69 42 29 20 74 68 65 20 76  than 1GiB) the v
d1a60 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69  alue returned mi
d1a70 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e  ght be less than
d1a80 20 74 68 65 20 74 72 75 65 20 73 74 72 69 6e 67   the true string
d1a90 20 6c 65 6e 67 74 68 2e 0d 0a 2a 2f 0d 0a 53 51   length...*/..SQ
d1aa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
d1ab0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
d1ac0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
d1ad0 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
d1ae0 7a 32 20 3d 20 7a 3b 0d 0a 20 20 69 66 28 20 7a  z2 = z;..  if( z
d1af0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ==0 ) return 0;.
d1b00 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b  .  while( *z2 ){
d1b10 20 7a 32 2b 2b 3b 20 7d 0d 0a 20 20 72 65 74 75   z2++; }..  retu
d1b20 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 20  rn 0x3fffffff & 
d1b30 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0d 0a  (int)(z2 - z);..
d1b40 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20  }..../*..** Set 
d1b50 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
d1b60 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
d1b70 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20  rror string for 
d1b80 74 68 65 20 73 71 6c 69 74 65 0d 0a 2a 2a 20 68  the sqlite..** h
d1b90 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20  andle "db". The 
d1ba0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65  error code is se
d1bb0 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e  t to "err_code".
d1bc0 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69 74 20 69  ..**..** If it i
d1bd0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69  s not NULL, stri
d1be0 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69  ng zFormat speci
d1bf0 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20  fies the format 
d1c00 6f 66 20 74 68 65 0d 0a 2a 2a 20 65 72 72 6f 72  of the..** error
d1c10 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 73   string in the s
d1c20 74 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69 6e  tyle of the prin
d1c30 74 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68  tf functions: Th
d1c40 65 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20  e following..** 
d1c50 66 6f 72 6d 61 74 20 63 68 61 72 61 63 74 65 72  format character
d1c60 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0d 0a  s are allowed:..
d1c70 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 25 73 20 20  **..**      %s  
d1c80 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72      Insert a str
d1c90 69 6e 67 0d 0a 2a 2a 20 20 20 20 20 20 25 7a 20  ing..**      %z 
d1ca0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68       A string th
d1cb0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  at should be fre
d1cc0 65 64 20 61 66 74 65 72 20 75 73 65 0d 0a 2a 2a  ed after use..**
d1cd0 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e        %d      In
d1ce0 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0d  sert an integer.
d1cf0 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20  .**      %T     
d1d00 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0d   Insert a token.
d1d10 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
d1d20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
d1d30 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
d1d40 72 63 4c 69 73 74 0d 0a 2a 2a 0d 0a 2a 2a 20 7a  rcList..**..** z
d1d50 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73  Format and any s
d1d60 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61  tring tokens tha
d1d70 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20  t follow it are 
d1d80 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0d 0a 2a  assumed to be..*
d1d90 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  * encoded in UTF
d1da0 2d 38 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 63  -8...**..** To c
d1db0 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65  lear the most re
d1dc0 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73  cent error for s
d1dd0 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62  qlite handle "db
d1de0 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0d  ", sqlite3Error.
d1df0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61  .** should be ca
d1e00 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f  lled with err_co
d1e10 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  de set to SQLITE
d1e20 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20  _OK and zFormat 
d1e30 73 65 74 0d 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e  set..** to NULL.
d1e40 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
d1e50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
d1e60 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20 2a  3Error(sqlite3 *
d1e70 64 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64 65  db, int err_code
d1e80 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
d1e90 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20  ormat, ...){..  
d1ea0 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70  if( db && (db->p
d1eb0 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72  Err || (db->pErr
d1ec0 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
d1ed0 65 77 28 64 62 29 29 21 3d 30 29 20 29 7b 0d 0a  ew(db))!=0) ){..
d1ee0 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
d1ef0 3d 20 65 72 72 5f 63 6f 64 65 3b 0d 0a 20 20 20  = err_code;..   
d1f00 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b 0d   if( zFormat ){.
d1f10 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0d  .      char *z;.
d1f20 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 61  .      va_list a
d1f30 70 3b 0d 0a 20 20 20 20 20 20 76 61 5f 73 74 61  p;..      va_sta
d1f40 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
d1f50 0d 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  ..      z = sqli
d1f60 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20  te3VMPrintf(db, 
d1f70 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20  zFormat, ap);.. 
d1f80 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b       va_end(ap);
d1f90 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
d1fa0 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
d1fb0 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49  Err, -1, z, SQLI
d1fc0 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
d1fd0 44 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 20 20 7d  DYNAMIC);..    }
d1fe0 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c  else{..      sql
d1ff0 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
d2000 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20  db->pErr, 0, 0, 
d2010 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
d2020 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20  ITE_STATIC);..  
d2030 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f    }..  }..}..../
d2040 2a 0d 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 72  *..** Add an err
d2050 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50  or message to pP
d2060 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e  arse->zErrMsg an
d2070 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 72  d increment pPar
d2080 73 65 2d 3e 6e 45 72 72 2e 0d 0a 2a 2a 20 54 68  se->nErr...** Th
d2090 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
d20a0 61 74 74 69 6e 67 20 63 68 61 72 61 63 74 65 72  atting character
d20b0 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0d 0a  s are allowed:..
d20c0 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 25 73 20 20  **..**      %s  
d20d0 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72      Insert a str
d20e0 69 6e 67 0d 0a 2a 2a 20 20 20 20 20 20 25 7a 20  ing..**      %z 
d20f0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68       A string th
d2100 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  at should be fre
d2110 65 64 20 61 66 74 65 72 20 75 73 65 0d 0a 2a 2a  ed after use..**
d2120 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e        %d      In
d2130 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0d  sert an integer.
d2140 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20  .**      %T     
d2150 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0d   Insert a token.
d2160 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
d2170 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
d2180 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
d2190 72 63 4c 69 73 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54  rcList..**..** T
d21a0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
d21b0 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 72  uld be used to r
d21c0 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20  eport any error 
d21d0 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c  that occurs whil
d21e0 73 74 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67  st..** compiling
d21f0 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
d2200 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73  t (i.e. within s
d2210 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
d2220 29 2e 20 54 68 65 0d 0a 2a 2a 20 6c 61 73 74 20  ). The..** last 
d2230 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  thing the sqlite
d2240 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63  3_prepare() func
d2250 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70  tion does is cop
d2260 79 20 74 68 65 20 65 72 72 6f 72 0d 0a 2a 2a 20  y the error..** 
d2270 73 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66  stored by this f
d2280 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  unction into the
d2290 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
d22a0 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72   using sqlite3Er
d22b0 72 6f 72 28 29 2e 0d 0a 2a 2a 20 46 75 6e 63 74  ror()...** Funct
d22c0 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ion sqlite3Error
d22d0 28 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  () should be use
d22e0 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65  d during stateme
d22f0 6e 74 20 65 78 65 63 75 74 69 6f 6e 0d 0a 2a 2a  nt execution..**
d2300 20 28 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   (sqlite3_step()
d2310 20 65 74 63 2e 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c   etc.)...*/..SQL
d2320 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
d2330 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
d2340 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
d2350 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
d2360 6d 61 74 2c 20 2e 2e 2e 29 7b 0d 0a 20 20 63 68  mat, ...){..  ch
d2370 61 72 20 2a 7a 4d 73 67 3b 0d 0a 20 20 76 61 5f  ar *zMsg;..  va_
d2380 6c 69 73 74 20 61 70 3b 0d 0a 20 20 73 71 6c 69  list ap;..  sqli
d2390 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
d23a0 2d 3e 64 62 3b 0d 0a 20 20 76 61 5f 73 74 61 72  ->db;..  va_star
d23b0 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d  t(ap, zFormat);.
d23c0 0a 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65  .  zMsg = sqlite
d23d0 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
d23e0 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76  ormat, ap);..  v
d23f0 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 69 66  a_end(ap);..  if
d2400 28 20 64 62 2d 3e 73 75 70 70 72 65 73 73 45 72  ( db->suppressEr
d2410 72 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  r ){..    sqlite
d2420 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 73 67  3DbFree(db, zMsg
d2430 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  );..  }else{..  
d2440 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
d2450 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
d2460 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d  Free(db, pParse-
d2470 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20  >zErrMsg);..    
d2480 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
d2490 3d 20 7a 4d 73 67 3b 0d 0a 20 20 20 20 70 50 61  = zMsg;..    pPa
d24a0 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
d24b0 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 7d 0d  _ERROR;..  }..}.
d24c0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72  .../*..** Conver
d24d0 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71  t an SQL-style q
d24e0 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74  uoted string int
d24f0 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e  o a normal strin
d2500 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0d 0a 2a  g by removing..*
d2510 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72  * the quote char
d2520 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e  acters.  The con
d2530 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20  version is done 
d2540 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68  in-place.  If th
d2550 65 0d 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73  e..** input does
d2560 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20   not begin with 
d2570 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  a quote characte
d2580 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
d2590 74 69 6e 65 0d 0a 2a 2a 20 69 73 20 61 20 6e 6f  tine..** is a no
d25a0 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  -op...**..** The
d25b0 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d 75   input string mu
d25c0 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69  st be zero-termi
d25d0 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a 65  nated.  A new ze
d25e0 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0d 0a 2a  ro-terminator..*
d25f0 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
d2600 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e  e dequoted strin
d2610 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  g...**..** The r
d2620 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d  eturn value is -
d2630 31 20 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e  1 if no dequotin
d2640 67 20 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20  g occurs or the 
d2650 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 0d 0a 2a  length of the..*
d2660 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e  * dequoted strin
d2670 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 20  g, exclusive of 
d2680 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  the zero termina
d2690 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 69 6e  tor, if dequotin
d26a0 67 20 64 6f 65 73 0d 0a 2a 2a 20 6f 63 63 75 72  g does..** occur
d26b0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 32 30 30 32 2d 46  ...**..** 2002-F
d26c0 65 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74  eb-14: This rout
d26d0 69 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20  ine is extended 
d26e0 74 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63  to remove MS-Acc
d26f0 65 73 73 20 73 74 79 6c 65 0d 0a 2a 2a 20 62 72  ess style..** br
d2700 61 63 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75  ackets from arou
d2710 6e 64 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20  nd identifers.  
d2720 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b  For example:  "[
d2730 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0d  a-b-c]" becomes.
d2740 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0d 0a 2a 2f  .** "a-b-c"...*/
d2750 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
d2760 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 75   int sqlite3Dequ
d2770 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0d 0a 20  ote(char *z){.. 
d2780 20 63 68 61 72 20 71 75 6f 74 65 3b 0d 0a 20 20   char quote;..  
d2790 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 69 66 28  int i, j;..  if(
d27a0 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d   z==0 ) return -
d27b0 31 3b 0d 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b  1;..  quote = z[
d27c0 30 5d 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 71  0];..  switch( q
d27d0 75 6f 74 65 20 29 7b 0d 0a 20 20 20 20 63 61 73  uote ){..    cas
d27e0 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0d  e '\'':  break;.
d27f0 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20  .    case '"':  
d2800 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
d2810 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20  e '`':   break; 
d2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d2830 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70  * For MySQL comp
d2840 61 74 69 62 69 6c 69 74 79 20 2a 2f 0d 0a 20 20  atibility */..  
d2850 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75    case '[':   qu
d2860 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61  ote = ']';  brea
d2870 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71  k;  /* For MS Sq
d2880 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62  lServer compatib
d2890 69 6c 69 74 79 20 2a 2f 0d 0a 20 20 20 20 64 65  ility */..    de
d28a0 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e  fault:    return
d28b0 20 2d 31 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72   -1;..  }..  for
d28c0 28 69 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59  (i=1, j=0; ALWAY
d28d0 53 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0d 0a  S(z[i]); i++){..
d28e0 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75      if( z[i]==qu
d28f0 6f 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  ote ){..      if
d2900 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20  ( z[i+1]==quote 
d2910 29 7b 0d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b  ){..        z[j+
d2920 2b 5d 20 3d 20 71 75 6f 74 65 3b 0d 0a 20 20 20  +] = quote;..   
d2930 20 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 20 20 20       i++;..     
d2940 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
d2950 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
d2960 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
d2970 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69      z[j++] = z[i
d2980 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  ];..    }..  }..
d2990 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0d 0a 20 20 72    z[j] = 0;..  r
d29a0 65 74 75 72 6e 20 6a 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn j;..}..../
d29b0 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f  * Convenient sho
d29c0 72 74 2d 68 61 6e 64 20 2a 2f 0d 0a 23 64 65 66  rt-hand */..#def
d29d0 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ine UpperToLower
d29e0 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
d29f0 6f 77 65 72 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53  ower..../*..** S
d2a00 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65  ome systems have
d2a10 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68   stricmp().  Oth
d2a20 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73 65  ers have strcase
d2a30 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 0d  cmp().  Because.
d2a40 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
d2a50 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20  consistency, we 
d2a60 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20  will define our 
d2a70 6f 77 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 4d 50  own...**..** IMP
d2a80 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20  LEMENTATION-OF: 
d2a90 52 2d 32 30 35 32 32 2d 32 34 36 33 39 20 54 68  R-20522-24639 Th
d2aa0 65 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  e sqlite3_strnic
d2ab0 6d 70 28 29 20 41 50 49 20 61 6c 6c 6f 77 73 0d  mp() API allows.
d2ac0 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  .** applications
d2ad0 20 61 6e 64 20 65 78 74 65 6e 73 69 6f 6e 73 20   and extensions 
d2ae0 74 6f 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  to compare the c
d2af0 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f 20 62  ontents of two b
d2b00 75 66 66 65 72 73 0d 0a 2a 2a 20 63 6f 6e 74 61  uffers..** conta
d2b10 69 6e 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69  ining UTF-8 stri
d2b20 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e  ngs in a case-in
d2b30 64 65 70 65 6e 64 65 6e 74 20 66 61 73 68 69 6f  dependent fashio
d2b40 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  n, using the sam
d2b50 65 0d 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f 6e  e..** definition
d2b60 20 6f 66 20 63 61 73 65 20 69 6e 64 65 70 65 6e   of case indepen
d2b70 64 65 6e 63 65 20 74 68 61 74 20 53 51 4c 69 74  dence that SQLit
d2b80 65 20 75 73 65 73 20 69 6e 74 65 72 6e 61 6c 6c  e uses internall
d2b90 79 20 77 68 65 6e 0d 0a 2a 2a 20 63 6f 6d 70 61  y when..** compa
d2ba0 72 69 6e 67 20 69 64 65 6e 74 69 66 69 65 72 73  ring identifiers
d2bb0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
d2bc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
d2bd0 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63  3StrICmp(const c
d2be0 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73  har *zLeft, cons
d2bf0 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b  t char *zRight){
d2c00 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ..  unsigned cha
d2c10 72 20 2a 61 2c 20 2a 62 3b 0d 0a 20 20 72 65 67  r *a, *b;..  reg
d2c20 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 73  ister unsigned s
d2c30 68 6f 72 74 20 73 61 20 3d 20 30 2c 20 73 62 20  hort sa = 0, sb 
d2c40 3d 20 30 3b 0d 0a 20 20 61 20 3d 20 28 75 6e 73  = 0;..  a = (uns
d2c50 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65  igned char *)zLe
d2c60 66 74 3b 0d 0a 20 20 62 20 3d 20 28 75 6e 73 69  ft;..  b = (unsi
d2c70 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67  gned char *)zRig
d2c80 68 74 3b 0d 0a 20 20 66 6f 72 28 3b 3b 29 0d 0a  ht;..  for(;;)..
d2c90 20 20 7b 0d 0a 09 20 20 73 61 20 3d 20 73 71 6c    {...  sa = sql
d2ca0 69 74 65 33 55 74 66 38 52 65 61 64 28 61 2c 20  ite3Utf8Read(a, 
d2cb0 26 61 29 3b 0d 0a 09 20 20 73 62 20 3d 20 73 71  &a);...  sb = sq
d2cc0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 62 2c  lite3Utf8Read(b,
d2cd0 20 26 62 29 3b 0d 0a 09 20 20 69 66 28 21 73 61   &b);...  if(!sa
d2ce0 29 0d 0a 09 09 20 20 62 72 65 61 6b 3b 0d 0a 09  )....  break;...
d2cf0 20 20 69 66 28 6f 61 73 55 70 70 65 72 32 4c 6f    if(oasUpper2Lo
d2d00 77 65 72 5b 73 61 5d 20 21 3d 20 6f 61 73 55 70  wer[sa] != oasUp
d2d10 70 65 72 32 4c 6f 77 65 72 5b 73 62 5d 29 0d 0a  per2Lower[sb])..
d2d20 09 09 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d  ..  break;..  }.
d2d30 0a 20 20 72 65 74 75 72 6e 20 6f 61 73 55 70 70  .  return oasUpp
d2d40 65 72 32 4c 6f 77 65 72 5b 73 61 5d 20 2d 20 6f  er2Lower[sa] - o
d2d50 61 73 55 70 70 65 72 32 4c 6f 77 65 72 5b 73 62  asUpper2Lower[sb
d2d60 5d 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50  ];..}..SQLITE_AP
d2d70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
d2d80 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61  rnicmp(const cha
d2d90 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20  r *zLeft, const 
d2da0 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e  char *zRight, in
d2db0 74 20 4e 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65  t N){..  unsigne
d2dc0 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0d 0a  d char *a, *b;..
d2dd0 20 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74    unsigned short
d2de0 20 73 61 20 3d 20 30 2c 20 73 62 20 3d 20 30 3b   sa = 0, sb = 0;
d2df0 0d 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65  ..  a = (unsigne
d2e00 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0d  d char *)zLeft;.
d2e10 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
d2e20 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0d   char *)zRight;.
d2e30 0a 20 20 77 68 69 6c 65 28 4e 2d 2d 20 3e 20 30  .  while(N-- > 0
d2e40 29 0d 0a 20 20 7b 0d 0a 09 20 20 73 61 20 3d 20  )..  {...  sa = 
d2e50 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
d2e60 61 2c 20 26 61 29 3b 0d 0a 09 20 20 73 62 20 3d  a, &a);...  sb =
d2e70 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
d2e80 28 62 2c 20 26 62 29 3b 0d 0a 09 20 20 69 66 28  (b, &b);...  if(
d2e90 21 73 61 29 0d 0a 09 09 20 20 62 72 65 61 6b 3b  !sa)....  break;
d2ea0 0d 0a 09 20 20 69 66 28 6f 61 73 55 70 70 65 72  ...  if(oasUpper
d2eb0 32 4c 6f 77 65 72 5b 73 61 5d 20 21 3d 20 6f 61  2Lower[sa] != oa
d2ec0 73 55 70 70 65 72 32 4c 6f 77 65 72 5b 73 62 5d  sUpper2Lower[sb]
d2ed0 29 0d 0a 09 09 20 20 62 72 65 61 6b 3b 0d 0a 20  )....  break;.. 
d2ee0 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30   }..  return N<0
d2ef0 20 3f 20 30 20 3a 20 6f 61 73 55 70 70 65 72 32   ? 0 : oasUpper2
d2f00 4c 6f 77 65 72 5b 73 61 5d 20 2d 20 6f 61 73 55  Lower[sa] - oasU
d2f10 70 70 65 72 32 4c 6f 77 65 72 5b 73 62 5d 3b 0d  pper2Lower[sb];.
d2f20 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  .}..../*..** The
d2f30 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61   string z[] is a
d2f40 6e 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74  n text represent
d2f50 61 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20  ation of a real 
d2f60 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 20 43 6f 6e 76  number...** Conv
d2f70 65 72 74 20 74 68 69 73 20 73 74 72 69 6e 67 20  ert this string 
d2f80 74 6f 20 61 20 64 6f 75 62 6c 65 20 61 6e 64 20  to a double and 
d2f90 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 2a 70  write it into *p
d2fa0 52 65 73 75 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  Result...**..** 
d2fb0 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69  The string z[] i
d2fc0 73 20 6c 65 6e 67 74 68 20 62 79 74 65 73 20 69  s length bytes i
d2fd0 6e 20 6c 65 6e 67 74 68 20 28 62 79 74 65 73 2c  n length (bytes,
d2fe0 20 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 29   not characters)
d2ff0 20 61 6e 64 0d 0a 2a 2a 20 75 73 65 73 20 74 68   and..** uses th
d3000 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20  e encoding enc. 
d3010 20 54 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e   The string is n
d3020 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 7a  ot necessarily z
d3030 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0d  ero-terminated..
d3040 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 54  .**..** Return T
d3050 52 55 45 20 69 66 20 74 68 65 20 72 65 73 75 6c  RUE if the resul
d3060 74 20 69 73 20 61 20 76 61 6c 69 64 20 72 65 61  t is a valid rea
d3070 6c 20 6e 75 6d 62 65 72 20 28 6f 72 20 69 6e 74  l number (or int
d3080 65 67 65 72 29 20 61 6e 64 20 46 41 4c 53 45 0d  eger) and FALSE.
d3090 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 72 69 6e  .** if the strin
d30a0 67 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f  g is empty or co
d30b0 6e 74 61 69 6e 73 20 65 78 74 72 61 6e 65 6f 75  ntains extraneou
d30c0 73 20 74 65 78 74 2e 20 20 56 61 6c 69 64 20 6e  s text.  Valid n
d30d0 75 6d 62 65 72 73 0d 0a 2a 2a 20 61 72 65 20 69  umbers..** are i
d30e0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 66  n one of these f
d30f0 6f 72 6d 61 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ormats:..**..** 
d3100 20 20 20 5b 2b 2d 5d 64 69 67 69 74 73 5b 45 5b     [+-]digits[E[
d3110 2b 2d 5d 64 69 67 69 74 73 5d 0d 0a 2a 2a 20 20  +-]digits]..**  
d3120 20 20 5b 2b 2d 5d 64 69 67 69 74 73 2e 5b 64 69    [+-]digits.[di
d3130 67 69 74 73 5d 5b 45 5b 2b 2d 5d 64 69 67 69 74  gits][E[+-]digit
d3140 73 5d 0d 0a 2a 2a 20 20 20 20 5b 2b 2d 5d 2e 64  s]..**    [+-].d
d3150 69 67 69 74 73 5b 45 5b 2b 2d 5d 64 69 67 69 74  igits[E[+-]digit
d3160 73 5d 0d 0a 2a 2a 0d 0a 2a 2a 20 4c 65 61 64 69  s]..**..** Leadi
d3170 6e 67 20 61 6e 64 20 74 72 61 69 6c 69 6e 67 20  ng and trailing 
d3180 77 68 69 74 65 73 70 61 63 65 20 69 73 20 69 67  whitespace is ig
d3190 6e 6f 72 65 64 20 66 6f 72 20 74 68 65 20 70 75  nored for the pu
d31a0 72 70 6f 73 65 20 6f 66 20 64 65 74 65 72 6d 69  rpose of determi
d31b0 6e 69 6e 67 0d 0a 2a 2a 20 76 61 6c 69 64 69 74  ning..** validit
d31c0 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 6f  y...**..** If so
d31d0 6d 65 20 70 72 65 66 69 78 20 6f 66 20 74 68 65  me prefix of the
d31e0 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73   input string is
d31f0 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2c   a valid number,
d3200 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a   this routine..*
d3210 2a 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 20  * returns FALSE 
d3220 62 75 74 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e  but it still con
d3230 76 65 72 74 73 20 74 68 65 20 70 72 65 66 69 78  verts the prefix
d3240 20 61 6e 64 20 77 72 69 74 65 73 20 74 68 65 20   and writes the 
d3250 72 65 73 75 6c 74 0d 0a 2a 2a 20 69 6e 74 6f 20  result..** into 
d3260 2a 70 52 65 73 75 6c 74 2e 0d 0a 2a 2f 0d 0a 53  *pResult...*/..S
d3270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d3280 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f  t sqlite3AtoF(co
d3290 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75  nst char *z, dou
d32a0 62 6c 65 20 2a 70 52 65 73 75 6c 74 2c 20 69 6e  ble *pResult, in
d32b0 74 20 6c 65 6e 67 74 68 2c 20 75 38 20 65 6e 63  t length, u8 enc
d32c0 29 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){..#ifndef SQLI
d32d0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
d32e0 5f 50 4f 49 4e 54 0d 0a 20 20 69 6e 74 20 69 6e  _POINT..  int in
d32f0 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54  cr = (enc==SQLIT
d3300 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0d 0a 20 20  E_UTF8?1:2);..  
d3310 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64  const char *zEnd
d3320 20 3d 20 7a 20 2b 20 6c 65 6e 67 74 68 3b 0d 0a   = z + length;..
d3330 20 20 2f 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e    /* sign * sign
d3340 69 66 69 63 61 6e 64 20 2a 20 28 31 30 20 5e 20  ificand * (10 ^ 
d3350 28 65 73 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e  (esign * exponen
d3360 74 29 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 69  t)) */..  int si
d3370 67 6e 20 3d 20 31 3b 20 20 20 20 2f 2a 20 73 69  gn = 1;    /* si
d3380 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e  gn of significan
d3390 64 20 2a 2f 0d 0a 20 20 69 36 34 20 73 20 3d 20  d */..  i64 s = 
d33a0 30 3b 20 20 20 20 20 20 20 2f 2a 20 73 69 67 6e  0;       /* sign
d33b0 69 66 69 63 61 6e 64 20 2a 2f 0d 0a 20 20 69 6e  ificand */..  in
d33c0 74 20 64 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  t d = 0;       /
d33d0 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e  * adjust exponen
d33e0 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67 20 64  t for shifting d
d33f0 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0d  ecimal point */.
d3400 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 31  .  int esign = 1
d3410 3b 20 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65  ;   /* sign of e
d3420 78 70 6f 6e 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e  xponent */..  in
d3430 74 20 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  t e = 0;       /
d3440 2a 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0d 0a 20  * exponent */.. 
d3450 20 69 6e 74 20 65 56 61 6c 69 64 20 3d 20 31 3b   int eValid = 1;
d3460 20 20 2f 2a 20 54 72 75 65 20 65 78 70 6f 6e 65    /* True expone
d3470 6e 74 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74  nt is either not
d3480 20 75 73 65 64 20 6f 72 20 69 73 20 77 65 6c 6c   used or is well
d3490 2d 66 6f 72 6d 65 64 20 2a 2f 0d 0a 20 20 64 6f  -formed */..  do
d34a0 75 62 6c 65 20 72 65 73 75 6c 74 3b 0d 0a 20 20  uble result;..  
d34b0 69 6e 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b  int nDigits = 0;
d34c0 0d 0a 0d 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d  ....  *pResult =
d34d0 20 30 2e 30 3b 20 20 20 2f 2a 20 44 65 66 61 75   0.0;   /* Defau
d34e0 6c 74 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c  lt return value,
d34f0 20 69 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65   in case of an e
d3500 72 72 6f 72 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28  rror */....  if(
d3510 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
d3520 31 36 42 45 20 29 20 7a 2b 2b 3b 0d 0a 0d 0a 20  16BE ) z++;.... 
d3530 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67   /* skip leading
d3540 20 73 70 61 63 65 73 20 2a 2f 0d 0a 20 20 77 68   spaces */..  wh
d3550 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73  ile( z<zEnd && s
d3560 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
d3570 29 20 29 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20 20  ) ) z+=incr;..  
d3580 69 66 28 20 7a 3e 3d 7a 45 6e 64 20 29 20 72 65  if( z>=zEnd ) re
d3590 74 75 72 6e 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20  turn 0;....  /* 
d35a0 67 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 6e  get sign of sign
d35b0 69 66 69 63 61 6e 64 20 2a 2f 0d 0a 20 20 69 66  ificand */..  if
d35c0 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0d 0a 20 20  ( *z=='-' ){..  
d35d0 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0d 0a 20 20    sign = -1;..  
d35e0 20 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20 20 7d 65    z+=incr;..  }e
d35f0 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20  lse if( *z=='+' 
d3600 29 7b 0d 0a 20 20 20 20 7a 2b 3d 69 6e 63 72 3b  ){..    z+=incr;
d3610 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 73 6b  ..  }....  /* sk
d3620 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65  ip leading zeroe
d3630 73 20 2a 2f 0d 0a 20 20 77 68 69 6c 65 28 20 7a  s */..  while( z
d3640 3c 7a 45 6e 64 20 26 26 20 7a 5b 30 5d 3d 3d 27  <zEnd && z[0]=='
d3650 30 27 20 29 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44  0' ) z+=incr, nD
d3660 69 67 69 74 73 2b 2b 3b 0d 0a 0d 0a 20 20 2f 2a  igits++;....  /*
d3670 20 63 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 66   copy max signif
d3680 69 63 61 6e 74 20 64 69 67 69 74 73 20 74 6f 20  icant digits to 
d3690 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0d 0a  significand */..
d36a0 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20    while( z<zEnd 
d36b0 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  && sqlite3Isdigi
d36c0 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c 41 52  t(*z) && s<((LAR
d36d0 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30  GEST_INT64-9)/10
d36e0 29 20 29 7b 0d 0a 20 20 20 20 73 20 3d 20 73 2a  ) ){..    s = s*
d36f0 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b  10 + (*z - '0');
d3700 0d 0a 20 20 20 20 7a 2b 3d 69 6e 63 72 2c 20 6e  ..    z+=incr, n
d3710 44 69 67 69 74 73 2b 2b 3b 0d 0a 20 20 7d 0d 0a  Digits++;..  }..
d3720 0d 0a 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d  ..  /* skip non-
d3730 73 69 67 6e 69 66 69 63 61 6e 74 20 73 69 67 6e  significant sign
d3740 69 66 69 63 61 6e 64 20 64 69 67 69 74 73 0d 0a  ificand digits..
d3750 20 20 2a 2a 20 28 69 6e 63 72 65 61 73 65 20 65    ** (increase e
d3760 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 74 6f 20  xponent by d to 
d3770 73 68 69 66 74 20 64 65 63 69 6d 61 6c 20 6c 65  shift decimal le
d3780 66 74 29 20 2a 2f 0d 0a 20 20 77 68 69 6c 65 28  ft) */..  while(
d3790 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69 74   z<zEnd && sqlit
d37a0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20  e3Isdigit(*z) ) 
d37b0 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69 74 73  z+=incr, nDigits
d37c0 2b 2b 2c 20 64 2b 2b 3b 0d 0a 20 20 69 66 28 20  ++, d++;..  if( 
d37d0 7a 3e 3d 7a 45 6e 64 20 29 20 67 6f 74 6f 20 64  z>=zEnd ) goto d
d37e0 6f 5f 61 74 6f 66 5f 63 61 6c 63 3b 0d 0a 0d 0a  o_atof_calc;....
d37f0 20 20 2f 2a 20 69 66 20 64 65 63 69 6d 61 6c 20    /* if decimal 
d3800 70 6f 69 6e 74 20 69 73 20 70 72 65 73 65 6e 74  point is present
d3810 20 2a 2f 0d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27   */..  if( *z=='
d3820 2e 27 20 29 7b 0d 0a 20 20 20 20 7a 2b 3d 69 6e  .' ){..    z+=in
d3830 63 72 3b 0d 0a 20 20 20 20 2f 2a 20 63 6f 70 79  cr;..    /* copy
d3840 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66 74   digits from aft
d3850 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73 69  er decimal to si
d3860 67 6e 69 66 69 63 61 6e 64 0d 0a 20 20 20 20 2a  gnificand..    *
d3870 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f  * (decrease expo
d3880 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69  nent by d to shi
d3890 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74  ft decimal right
d38a0 29 20 2a 2f 0d 0a 20 20 20 20 77 68 69 6c 65 28  ) */..    while(
d38b0 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69 74   z<zEnd && sqlit
d38c0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26  e3Isdigit(*z) &&
d38d0 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54   s<((LARGEST_INT
d38e0 36 34 2d 39 29 2f 31 30 29 20 29 7b 0d 0a 20 20  64-9)/10) ){..  
d38f0 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28      s = s*10 + (
d3900 2a 7a 20 2d 20 27 30 27 29 3b 0d 0a 20 20 20 20  *z - '0');..    
d3910 20 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69    z+=incr, nDigi
d3920 74 73 2b 2b 2c 20 64 2d 2d 3b 0d 0a 20 20 20 20  ts++, d--;..    
d3930 7d 0d 0a 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e  }..    /* skip n
d3940 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 20 64  on-significant d
d3950 69 67 69 74 73 20 2a 2f 0d 0a 20 20 20 20 77 68  igits */..    wh
d3960 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73  ile( z<zEnd && s
d3970 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
d3980 29 20 29 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69  ) ) z+=incr, nDi
d3990 67 69 74 73 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20  gits++;..  }..  
d39a0 69 66 28 20 7a 3e 3d 7a 45 6e 64 20 29 20 67 6f  if( z>=zEnd ) go
d39b0 74 6f 20 64 6f 5f 61 74 6f 66 5f 63 61 6c 63 3b  to do_atof_calc;
d39c0 0d 0a 0d 0a 20 20 2f 2a 20 69 66 20 65 78 70 6f  ....  /* if expo
d39d0 6e 65 6e 74 20 69 73 20 70 72 65 73 65 6e 74 20  nent is present 
d39e0 2a 2f 0d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65  */..  if( *z=='e
d39f0 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0d  ' || *z=='E' ){.
d3a00 0a 20 20 20 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20  .    z+=incr;.. 
d3a10 20 20 20 65 56 61 6c 69 64 20 3d 20 30 3b 0d 0a     eValid = 0;..
d3a20 20 20 20 20 69 66 28 20 7a 3e 3d 7a 45 6e 64 20      if( z>=zEnd 
d3a30 29 20 67 6f 74 6f 20 64 6f 5f 61 74 6f 66 5f 63  ) goto do_atof_c
d3a40 61 6c 63 3b 0d 0a 20 20 20 20 2f 2a 20 67 65 74  alc;..    /* get
d3a50 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e   sign of exponen
d3a60 74 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 2a 7a  t */..    if( *z
d3a70 3d 3d 27 2d 27 20 29 7b 0d 0a 20 20 20 20 20 20  =='-' ){..      
d3a80 65 73 69 67 6e 20 3d 20 2d 31 3b 0d 0a 20 20 20  esign = -1;..   
d3a90 20 20 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20 20 20     z+=incr;..   
d3aa0 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27   }else if( *z=='
d3ab0 2b 27 20 29 7b 0d 0a 20 20 20 20 20 20 7a 2b 3d  +' ){..      z+=
d3ac0 69 6e 63 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  incr;..    }..  
d3ad0 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73    /* copy digits
d3ae0 20 74 6f 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0d   to exponent */.
d3af0 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45  .    while( z<zE
d3b00 6e 64 20 26 26 20 73 71 6c 69 74 65 33 49 73 64  nd && sqlite3Isd
d3b10 69 67 69 74 28 2a 7a 29 20 29 7b 0d 0a 20 20 20  igit(*z) ){..   
d3b20 20 20 20 65 20 3d 20 65 3c 31 30 30 30 30 20 3f     e = e<10000 ?
d3b30 20 28 65 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27   (e*10 + (*z - '
d3b40 30 27 29 29 20 3a 20 31 30 30 30 30 3b 0d 0a 20  0')) : 10000;.. 
d3b50 20 20 20 20 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20       z+=incr;.. 
d3b60 20 20 20 20 20 65 56 61 6c 69 64 20 3d 20 31 3b       eValid = 1;
d3b70 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
d3b80 20 20 2f 2a 20 73 6b 69 70 20 74 72 61 69 6c 69    /* skip traili
d3b90 6e 67 20 73 70 61 63 65 73 20 2a 2f 0d 0a 20 20  ng spaces */..  
d3ba0 69 66 28 20 6e 44 69 67 69 74 73 20 26 26 20 65  if( nDigits && e
d3bb0 56 61 6c 69 64 20 29 7b 0d 0a 20 20 20 20 77 68  Valid ){..    wh
d3bc0 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73  ile( z<zEnd && s
d3bd0 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
d3be0 29 20 29 20 7a 2b 3d 69 6e 63 72 3b 0d 0a 20 20  ) ) z+=incr;..  
d3bf0 7d 0d 0a 0d 0a 64 6f 5f 61 74 6f 66 5f 63 61 6c  }....do_atof_cal
d3c00 63 3a 0d 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20  c:..  /* adjust 
d3c10 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61  exponent by d, a
d3c20 6e 64 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a  nd update sign *
d3c30 2f 0d 0a 20 20 65 20 3d 20 28 65 2a 65 73 69 67  /..  e = (e*esig
d3c40 6e 29 20 2b 20 64 3b 0d 0a 20 20 69 66 28 20 65  n) + d;..  if( e
d3c50 3c 30 20 29 20 7b 0d 0a 20 20 20 20 65 73 69 67  <0 ) {..    esig
d3c60 6e 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 65 20 2a  n = -1;..    e *
d3c70 3d 20 2d 31 3b 0d 0a 20 20 7d 20 65 6c 73 65 20  = -1;..  } else 
d3c80 7b 0d 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31  {..    esign = 1
d3c90 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 69  ;..  }....  /* i
d3ca0 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64 20  f 0 significand 
d3cb0 2a 2f 0d 0a 20 20 69 66 28 20 21 73 20 29 20 7b  */..  if( !s ) {
d3cc0 0d 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20  ..    /* In the 
d3cd0 49 45 45 45 20 37 35 34 20 73 74 61 6e 64 61 72  IEEE 754 standar
d3ce0 64 2c 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65  d, zero is signe
d3cf0 64 2e 0d 0a 20 20 20 20 2a 2a 20 41 64 64 20 74  d...    ** Add t
d3d00 68 65 20 73 69 67 6e 20 69 66 20 77 65 27 76 65  he sign if we've
d3d10 20 73 65 65 6e 20 61 74 20 6c 65 61 73 74 20 6f   seen at least o
d3d20 6e 65 20 64 69 67 69 74 20 2a 2f 0d 0a 20 20 20  ne digit */..   
d3d30 20 72 65 73 75 6c 74 20 3d 20 28 73 69 67 6e 3c   result = (sign<
d3d40 30 20 26 26 20 6e 44 69 67 69 74 73 29 20 3f 20  0 && nDigits) ? 
d3d50 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 28 64 6f  -(double)0 : (do
d3d60 75 62 6c 65 29 30 3b 0d 0a 20 20 7d 20 65 6c 73  uble)0;..  } els
d3d70 65 20 7b 0d 0a 20 20 20 20 2f 2a 20 61 74 74 65  e {..    /* atte
d3d80 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 78  mpt to reduce ex
d3d90 70 6f 6e 65 6e 74 20 2a 2f 0d 0a 20 20 20 20 69  ponent */..    i
d3da0 66 28 20 65 73 69 67 6e 3e 30 20 29 7b 0d 0a 20  f( esign>0 ){.. 
d3db0 20 20 20 20 20 77 68 69 6c 65 28 20 73 3c 28 4c       while( s<(L
d3dc0 41 52 47 45 53 54 5f 49 4e 54 36 34 2f 31 30 29  ARGEST_INT64/10)
d3dd0 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a   && e>0 ) e--,s*
d3de0 3d 31 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  =10;..    }else{
d3df0 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 21  ..      while( !
d3e00 28 73 25 31 30 29 20 26 26 20 65 3e 30 20 29 20  (s%10) && e>0 ) 
d3e10 65 2d 2d 2c 73 2f 3d 31 30 3b 0d 0a 20 20 20 20  e--,s/=10;..    
d3e20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 61 64 6a 75  }....    /* adju
d3e30 73 74 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73  st the sign of s
d3e40 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0d 0a 20  ignificand */.. 
d3e50 20 20 20 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20     s = sign<0 ? 
d3e60 2d 73 20 3a 20 73 3b 0d 0a 0d 0a 20 20 20 20 2f  -s : s;....    /
d3e70 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20 73  * if exponent, s
d3e80 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e 64  cale significand
d3e90 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 0d   as appropriate.
d3ea0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72  .    ** and stor
d3eb0 65 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0d  e in result. */.
d3ec0 0a 20 20 20 20 69 66 28 20 65 20 29 7b 0d 0a 20  .    if( e ){.. 
d3ed0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 63 61 6c       double scal
d3ee0 65 20 3d 20 31 2e 30 3b 0d 0a 20 20 20 20 20 20  e = 1.0;..      
d3ef0 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 68 61  /* attempt to ha
d3f00 6e 64 6c 65 20 65 78 74 72 65 6d 65 6c 79 20 73  ndle extremely s
d3f10 6d 61 6c 6c 2f 6c 61 72 67 65 20 6e 75 6d 62 65  mall/large numbe
d3f20 72 73 20 62 65 74 74 65 72 20 2a 2f 0d 0a 20 20  rs better */..  
d3f30 20 20 20 20 69 66 28 20 65 3e 33 30 37 20 26 26      if( e>307 &&
d3f40 20 65 3c 33 34 32 20 29 7b 0d 0a 20 20 20 20 20   e<342 ){..     
d3f50 20 20 20 77 68 69 6c 65 28 20 65 25 33 30 38 20     while( e%308 
d3f60 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30  ) { scale *= 1.0
d3f70 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0d 0a  e+1; e -= 1; }..
d3f80 20 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67          if( esig
d3f90 6e 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  n<0 ){..        
d3fa0 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73    result = s / s
d3fb0 63 61 6c 65 3b 0d 0a 20 20 20 20 20 20 20 20 20  cale;..         
d3fc0 20 72 65 73 75 6c 74 20 2f 3d 20 31 2e 30 65 2b   result /= 1.0e+
d3fd0 33 30 38 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  308;..        }e
d3fe0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
d3ff0 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 61  result = s * sca
d4000 6c 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72  le;..          r
d4010 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 30  esult *= 1.0e+30
d4020 38 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  8;..        }.. 
d4030 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65       }else if( e
d4040 3e 3d 33 34 32 20 29 7b 0d 0a 20 20 20 20 20 20  >=342 ){..      
d4050 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b    if( esign<0 ){
d4060 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75  ..          resu
d4070 6c 74 20 3d 20 30 2e 30 2a 73 3b 0d 0a 20 20 20  lt = 0.0*s;..   
d4080 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
d4090 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20         result = 
d40a0 31 65 33 30 38 2a 31 65 33 30 38 2a 73 3b 20 20  1e308*1e308*s;  
d40b0 2f 2a 20 49 6e 66 69 6e 69 74 79 20 2a 2f 0d 0a  /* Infinity */..
d40c0 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
d40d0 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
d40e0 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 20 74   /* 1.0e+22 is t
d40f0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 65 72  he largest power
d4100 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 6e 20   of 10 than can 
d4110 62 65 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  be ..        ** 
d4120 72 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 63  represented exac
d4130 74 6c 79 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20  tly. */..       
d4140 20 77 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b   while( e%22 ) {
d4150 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31   scale *= 1.0e+1
d4160 3b 20 65 20 2d 3d 20 31 3b 20 7d 0d 0a 20 20 20  ; e -= 1; }..   
d4170 20 20 20 20 20 77 68 69 6c 65 28 20 65 3e 30 20       while( e>0 
d4180 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30  ) { scale *= 1.0
d4190 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d  e+22; e -= 22; }
d41a0 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 73  ..        if( es
d41b0 69 67 6e 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20  ign<0 ){..      
d41c0 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2f      result = s /
d41d0 20 73 63 61 6c 65 3b 0d 0a 20 20 20 20 20 20 20   scale;..       
d41e0 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
d41f0 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20     result = s * 
d4200 73 63 61 6c 65 3b 0d 0a 20 20 20 20 20 20 20 20  scale;..        
d4210 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
d4220 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 20 20  } else {..      
d4230 72 65 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65  result = (double
d4240 29 73 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  )s;..    }..  }.
d4250 0a 0d 0a 20 20 2f 2a 20 73 74 6f 72 65 20 74 68  ...  /* store th
d4260 65 20 72 65 73 75 6c 74 20 2a 2f 0d 0a 20 20 2a  e result */..  *
d4270 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 6c 74  pResult = result
d4280 3b 0d 0a 0d 0a 20 20 2f 2a 20 72 65 74 75 72 6e  ;....  /* return
d4290 20 74 72 75 65 20 69 66 20 6e 75 6d 62 65 72 20   true if number 
d42a0 61 6e 64 20 6e 6f 20 65 78 74 72 61 20 6e 6f 6e  and no extra non
d42b0 2d 77 68 69 74 65 73 70 61 63 65 20 63 68 72 61  -whitespace chra
d42c0 63 74 65 72 73 20 61 66 74 65 72 20 2a 2f 0d 0a  cters after */..
d42d0 20 20 72 65 74 75 72 6e 20 7a 3e 3d 7a 45 6e 64    return z>=zEnd
d42e0 20 26 26 20 6e 44 69 67 69 74 73 3e 30 20 26 26   && nDigits>0 &&
d42f0 20 65 56 61 6c 69 64 3b 0d 0a 23 65 6c 73 65 0d   eValid;..#else.
d4300 0a 20 20 72 65 74 75 72 6e 20 21 73 71 6c 69 74  .  return !sqlit
d4310 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73  e3Atoi64(z, pRes
d4320 75 6c 74 2c 20 6c 65 6e 67 74 68 2c 20 65 6e 63  ult, length, enc
d4330 29 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  );..#endif /* SQ
d4340 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
d4350 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0d 0a 7d 0d 0a  NG_POINT */..}..
d4360 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 61 72 65  ../*..** Compare
d4370 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65   the 19-characte
d4380 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67  r string zNum ag
d4390 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72  ainst the text r
d43a0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0d 0a 2a  epresentation..*
d43b0 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39  * value 2^63:  9
d43c0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
d43d0 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61  08.  Return nega
d43e0 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
d43f0 6f 73 69 74 69 76 65 0d 0a 2a 2a 20 69 66 20 7a  ositive..** if z
d4400 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e  Num is less than
d4410 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
d4420 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
d4430 73 74 72 69 6e 67 2e 0d 0a 2a 2a 20 4e 6f 74 65  string...** Note
d4440 20 74 68 61 74 20 7a 4e 75 6d 20 6d 75 73 74 20   that zNum must 
d4450 63 6f 6e 74 61 69 6e 20 65 78 61 63 74 6c 79 20  contain exactly 
d4460 31 39 20 63 68 61 72 61 63 74 65 72 73 2e 0d 0a  19 characters...
d4470 2a 2a 0d 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65  **..** Unlike me
d4480 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74  mcmp() this rout
d4490 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 65  ine is guarantee
d44a0 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20  d to return the 
d44b0 64 69 66 66 65 72 65 6e 63 65 0d 0a 2a 2a 20 69  difference..** i
d44c0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  n the values of 
d44d0 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69  the last digit i
d44e0 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  f the only diffe
d44f0 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0d  rence is in the.
d4500 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20  .** last digit. 
d4510 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65   So, for example
d4520 2c 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 63  ,..**..**      c
d4530 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39 32  ompare2pow63("92
d4540 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
d4550 30 22 2c 20 31 29 0d 0a 2a 2a 0d 0a 2a 2a 20 77  0", 1)..**..** w
d4560 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0d 0a  ill return -8...
d4570 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */..static int c
d4580 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f 6e  ompare2pow63(con
d4590 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69  st char *zNum, i
d45a0 6e 74 20 69 6e 63 72 29 7b 0d 0a 20 20 69 6e 74  nt incr){..  int
d45b0 20 63 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69   c = 0;..  int i
d45c0 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
d45d0 20 20 20 20 20 20 20 2f 2a 20 30 31 32 33 34 35         /* 012345
d45e0 36 37 38 39 30 31 32 33 34 35 36 37 20 2a 2f 0d  678901234567 */.
d45f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
d4600 6f 77 36 33 20 3d 20 22 39 32 32 33 33 37 32 30  ow63 = "92233720
d4610 33 36 38 35 34 37 37 35 38 30 22 3b 0d 0a 20 20  3685477580";..  
d4620 66 6f 72 28 69 3d 30 3b 20 63 3d 3d 30 20 26 26  for(i=0; c==0 &&
d4630 20 69 3c 31 38 3b 20 69 2b 2b 29 7b 0d 0a 20 20   i<18; i++){..  
d4640 20 20 63 20 3d 20 28 7a 4e 75 6d 5b 69 2a 69 6e    c = (zNum[i*in
d4650 63 72 5d 2d 70 6f 77 36 33 5b 69 5d 29 2a 31 30  cr]-pow63[i])*10
d4660 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 63 3d  ;..  }..  if( c=
d4670 3d 30 20 29 7b 0d 0a 20 20 20 20 63 20 3d 20 7a  =0 ){..    c = z
d4680 4e 75 6d 5b 31 38 2a 69 6e 63 72 5d 20 2d 20 27  Num[18*incr] - '
d4690 38 27 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73  8';..    testcas
d46a0 65 28 20 63 3d 3d 28 2d 31 29 20 29 3b 0d 0a 20  e( c==(-1) );.. 
d46b0 20 20 20 74 65 73 74 63 61 73 65 28 20 63 3d 3d     testcase( c==
d46c0 30 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61  0 );..    testca
d46d0 73 65 28 20 63 3d 3d 28 2b 31 29 20 29 3b 0d 0a  se( c==(+1) );..
d46e0 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 63 3b    }..  return c;
d46f0 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
d4700 43 6f 6e 76 65 72 74 20 7a 4e 75 6d 20 74 6f 20  Convert zNum to 
d4710 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
d4720 69 6e 74 65 67 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  integer...**..**
d4730 20 49 66 20 74 68 65 20 7a 4e 75 6d 20 76 61 6c   If the zNum val
d4740 75 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 61  ue is representa
d4750 62 6c 65 20 61 73 20 61 20 36 34 2d 62 69 74 20  ble as a 64-bit 
d4760 74 77 6f 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20  twos-complement 
d4770 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68  ..** integer, th
d4780 65 6e 20 77 72 69 74 65 20 74 68 61 74 20 76 61  en write that va
d4790 6c 75 65 20 69 6e 74 6f 20 2a 70 4e 75 6d 20 61  lue into *pNum a
d47a0 6e 64 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2a  nd return 0...**
d47b0 0d 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 69 73 20  ..** If zNum is 
d47c0 65 78 61 63 74 6c 79 20 39 32 32 33 33 37 32 30  exactly 92233720
d47d0 33 36 38 35 34 36 36 35 38 30 38 2c 20 72 65 74  36854665808, ret
d47e0 75 72 6e 20 32 2e 20 20 54 68 69 73 20 73 70 65  urn 2.  This spe
d47f0 63 69 61 6c 0d 0a 2a 2a 20 63 61 73 65 20 69 73  cial..** case is
d4800 20 62 72 6f 6b 65 6e 20 6f 75 74 20 62 65 63 61   broken out beca
d4810 75 73 65 20 77 68 69 6c 65 20 39 32 32 33 33 37  use while 922337
d4820 32 30 33 36 38 35 34 36 36 35 38 30 38 20 63 61  2036854665808 ca
d4830 6e 6e 6f 74 20 62 65 20 61 20 0d 0a 2a 2a 20 73  nnot be a ..** s
d4840 69 67 6e 65 64 20 36 34 2d 62 69 74 20 69 6e 74  igned 64-bit int
d4850 65 67 65 72 2c 20 69 74 73 20 6e 65 67 61 74 69  eger, its negati
d4860 76 65 20 2d 39 32 32 33 33 37 32 30 33 36 38 35  ve -922337203685
d4870 34 36 36 35 38 30 38 20 63 61 6e 20 62 65 2e 0d  4665808 can be..
d4880 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20  .**..** If zNum 
d4890 69 73 20 74 6f 6f 20 62 69 67 20 66 6f 72 20 61  is too big for a
d48a0 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
d48b0 61 6e 64 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 39  and is not..** 9
d48c0 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38  2233720368546658
d48d0 30 38 20 74 68 65 6e 20 72 65 74 75 72 6e 20 31  08 then return 1
d48e0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 6c 65 6e 67 74 68  ...**..** length
d48f0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
d4900 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73  f bytes in the s
d4910 74 72 69 6e 67 20 28 62 79 74 65 73 2c 20 6e 6f  tring (bytes, no
d4920 74 20 63 68 61 72 61 63 74 65 72 73 29 2e 0d 0a  t characters)...
d4930 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 69 73  ** The string is
d4940 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
d4950 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
d4960 2e 20 20 54 68 65 20 65 6e 63 6f 64 69 6e 67 20  .  The encoding 
d4970 69 73 0d 0a 2a 2a 20 67 69 76 65 6e 20 62 79 20  is..** given by 
d4980 65 6e 63 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  enc...*/..SQLITE
d4990 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
d49a0 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74  ite3Atoi64(const
d49b0 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34   char *zNum, i64
d49c0 20 2a 70 4e 75 6d 2c 20 69 6e 74 20 6c 65 6e 67   *pNum, int leng
d49d0 74 68 2c 20 75 38 20 65 6e 63 29 7b 0d 0a 20 20  th, u8 enc){..  
d49e0 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d  int incr = (enc=
d49f0 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32  =SQLITE_UTF8?1:2
d4a00 29 3b 0d 0a 20 20 75 36 34 20 75 20 3d 20 30 3b  );..  u64 u = 0;
d4a10 0d 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b  ..  int neg = 0;
d4a20 20 2f 2a 20 61 73 73 75 6d 65 20 70 6f 73 69 74   /* assume posit
d4a30 69 76 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b  ive */..  int i;
d4a40 0d 0a 20 20 69 6e 74 20 63 20 3d 20 30 3b 0d 0a  ..  int c = 0;..
d4a50 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
d4a60 74 61 72 74 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  tart;..  const c
d4a70 68 61 72 20 2a 7a 45 6e 64 20 3d 20 7a 4e 75 6d  har *zEnd = zNum
d4a80 20 2b 20 6c 65 6e 67 74 68 3b 0d 0a 20 20 69 66   + length;..  if
d4a90 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ( enc==SQLITE_UT
d4aa0 46 31 36 42 45 20 29 20 7a 4e 75 6d 2b 2b 3b 0d  F16BE ) zNum++;.
d4ab0 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 3c 7a  .  while( zNum<z
d4ac0 45 6e 64 20 26 26 20 73 71 6c 69 74 65 33 49 73  End && sqlite3Is
d4ad0 73 70 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a  space(*zNum) ) z
d4ae0 4e 75 6d 2b 3d 69 6e 63 72 3b 0d 0a 20 20 69 66  Num+=incr;..  if
d4af0 28 20 7a 4e 75 6d 3c 7a 45 6e 64 20 29 7b 0d 0a  ( zNum<zEnd ){..
d4b00 20 20 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27      if( *zNum=='
d4b10 2d 27 20 29 7b 0d 0a 20 20 20 20 20 20 6e 65 67  -' ){..      neg
d4b20 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7a 4e 75   = 1;..      zNu
d4b30 6d 2b 3d 69 6e 63 72 3b 0d 0a 20 20 20 20 7d 65  m+=incr;..    }e
d4b40 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27  lse if( *zNum=='
d4b50 2b 27 20 29 7b 0d 0a 20 20 20 20 20 20 7a 4e 75  +' ){..      zNu
d4b60 6d 2b 3d 69 6e 63 72 3b 0d 0a 20 20 20 20 7d 0d  m+=incr;..    }.
d4b70 0a 20 20 7d 0d 0a 20 20 7a 53 74 61 72 74 20 3d  .  }..  zStart =
d4b80 20 7a 4e 75 6d 3b 0d 0a 20 20 77 68 69 6c 65 28   zNum;..  while(
d4b90 20 7a 4e 75 6d 3c 7a 45 6e 64 20 26 26 20 7a 4e   zNum<zEnd && zN
d4ba0 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e  um[0]=='0' ){ zN
d4bb0 75 6d 2b 3d 69 6e 63 72 3b 20 7d 20 2f 2a 20 53  um+=incr; } /* S
d4bc0 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f  kip leading zero
d4bd0 73 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30  s. */..  for(i=0
d4be0 3b 20 26 7a 4e 75 6d 5b 69 5d 3c 7a 45 6e 64 20  ; &zNum[i]<zEnd 
d4bf0 26 26 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d  && (c=zNum[i])>=
d4c00 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 69  '0' && c<='9'; i
d4c10 2b 3d 69 6e 63 72 29 7b 0d 0a 20 20 20 20 75 20  +=incr){..    u 
d4c20 3d 20 75 2a 31 30 20 2b 20 63 20 2d 20 27 30 27  = u*10 + c - '0'
d4c30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 75 3e  ;..  }..  if( u>
d4c40 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 29 7b  LARGEST_INT64 ){
d4c50 0d 0a 20 20 20 20 2a 70 4e 75 6d 20 3d 20 53 4d  ..    *pNum = SM
d4c60 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0d 0a 20  ALLEST_INT64;.. 
d4c70 20 7d 65 6c 73 65 20 69 66 28 20 6e 65 67 20 29   }else if( neg )
d4c80 7b 0d 0a 20 20 20 20 2a 70 4e 75 6d 20 3d 20 2d  {..    *pNum = -
d4c90 28 69 36 34 29 75 3b 0d 0a 20 20 7d 65 6c 73 65  (i64)u;..  }else
d4ca0 7b 0d 0a 20 20 20 20 2a 70 4e 75 6d 20 3d 20 28  {..    *pNum = (
d4cb0 69 36 34 29 75 3b 0d 0a 20 20 7d 0d 0a 20 20 74  i64)u;..  }..  t
d4cc0 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38 20 29  estcase( i==18 )
d4cd0 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 69  ;..  testcase( i
d4ce0 3d 3d 31 39 20 29 3b 0d 0a 20 20 74 65 73 74 63  ==19 );..  testc
d4cf0 61 73 65 28 20 69 3d 3d 32 30 20 29 3b 0d 0a 20  ase( i==20 );.. 
d4d00 20 69 66 28 20 28 63 21 3d 30 20 26 26 20 26 7a   if( (c!=0 && &z
d4d10 4e 75 6d 5b 69 5d 3c 7a 45 6e 64 29 20 7c 7c 20  Num[i]<zEnd) || 
d4d20 28 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d  (i==0 && zStart=
d4d30 3d 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 2a 69  =zNum) || i>19*i
d4d40 6e 63 72 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 7a  ncr ){..    /* z
d4d50 4e 75 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20  Num is empty or 
d4d60 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d  contains non-num
d4d70 65 72 69 63 20 74 65 78 74 20 6f 72 20 69 73 20  eric text or is 
d4d80 6c 6f 6e 67 65 72 0d 0a 20 20 20 20 2a 2a 20 74  longer..    ** t
d4d90 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 28 74  han 19 digits (t
d4da0 68 75 73 20 67 75 61 72 61 6e 74 65 65 69 6e 67  hus guaranteeing
d4db0 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f 20   that it is too 
d4dc0 6c 61 72 67 65 29 20 2a 2f 0d 0a 20 20 20 20 72  large) */..    r
d4dd0 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 65 6c 73  eturn 1;..  }els
d4de0 65 20 69 66 28 20 69 3c 31 39 2a 69 6e 63 72 20  e if( i<19*incr 
d4df0 29 7b 0d 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20  ){..    /* Less 
d4e00 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20  than 19 digits, 
d4e10 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  so we know that 
d4e20 69 74 20 66 69 74 73 20 69 6e 20 36 34 20 62 69  it fits in 64 bi
d4e30 74 73 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72  ts */..    asser
d4e40 74 28 20 75 3c 3d 4c 41 52 47 45 53 54 5f 49 4e  t( u<=LARGEST_IN
d4e50 54 36 34 20 29 3b 0d 0a 20 20 20 20 72 65 74 75  T64 );..    retu
d4e60 72 6e 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  rn 0;..  }else{.
d4e70 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20  .    /* zNum is 
d4e80 61 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65  a 19-digit numbe
d4e90 72 73 2e 20 20 43 6f 6d 70 61 72 65 20 69 74 20  rs.  Compare it 
d4ea0 61 67 61 69 6e 73 74 20 39 32 32 33 33 37 32 30  against 92233720
d4eb0 33 36 38 35 34 37 37 35 38 30 38 2e 20 2a 2f 0d  36854775808. */.
d4ec0 0a 20 20 20 20 63 20 3d 20 63 6f 6d 70 61 72 65  .    c = compare
d4ed0 32 70 6f 77 36 33 28 7a 4e 75 6d 2c 20 69 6e 63  2pow63(zNum, inc
d4ee0 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 63 3c 30  r);..    if( c<0
d4ef0 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 7a 4e   ){..      /* zN
d4f00 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  um is less than 
d4f10 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
d4f20 38 30 38 20 73 6f 20 69 74 20 66 69 74 73 20 2a  808 so it fits *
d4f30 2f 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  /..      assert(
d4f40 20 75 3c 3d 4c 41 52 47 45 53 54 5f 49 4e 54 36   u<=LARGEST_INT6
d4f50 34 20 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  4 );..      retu
d4f60 72 6e 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  rn 0;..    }else
d4f70 20 69 66 28 20 63 3e 30 20 29 7b 0d 0a 20 20 20   if( c>0 ){..   
d4f80 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 67 72     /* zNum is gr
d4f90 65 61 74 65 72 20 74 68 61 6e 20 39 32 32 33 33  eater than 92233
d4fa0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 20 73  72036854775808 s
d4fb0 6f 20 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 2a  o it overflows *
d4fc0 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  /..      return 
d4fd0 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  1;..    }else{..
d4fe0 20 20 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73        /* zNum is
d4ff0 20 65 78 61 63 74 6c 79 20 39 32 32 33 33 37 32   exactly 9223372
d5000 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 46  036854775808.  F
d5010 69 74 73 20 69 66 20 6e 65 67 61 74 69 76 65 2e  its if negative.
d5020 20 20 54 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20    The..      ** 
d5030 73 70 65 63 69 61 6c 20 63 61 73 65 20 32 20 6f  special case 2 o
d5040 76 65 72 66 6c 6f 77 20 69 66 20 70 6f 73 69 74  verflow if posit
d5050 69 76 65 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73  ive */..      as
d5060 73 65 72 74 28 20 75 2d 31 3d 3d 4c 41 52 47 45  sert( u-1==LARGE
d5070 53 54 5f 49 4e 54 36 34 20 29 3b 0d 0a 20 20 20  ST_INT64 );..   
d5080 20 20 20 61 73 73 65 72 74 28 20 28 2a 70 4e 75     assert( (*pNu
d5090 6d 29 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54  m)==SMALLEST_INT
d50a0 36 34 20 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  64 );..      ret
d50b0 75 72 6e 20 6e 65 67 20 3f 20 30 20 3a 20 32 3b  urn neg ? 0 : 2;
d50c0 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d  ..    }..  }..}.
d50d0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 7a 4e 75  .../*..** If zNu
d50e0 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  m represents an 
d50f0 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c  integer that wil
d5100 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73  l fit in 32-bits
d5110 2c 20 74 68 65 6e 20 73 65 74 0d 0a 2a 2a 20 2a  , then set..** *
d5120 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69  pValue to that i
d5130 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72  nteger and retur
d5140 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  n true.  Otherwi
d5150 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  se return false.
d5160 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 79 20 6e 6f 6e  ..**..** Any non
d5170 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 74  -numeric charact
d5180 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69  ers that followi
d5190 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f  ng zNum are igno
d51a0 72 65 64 2e 0d 0a 2a 2a 20 54 68 69 73 20 69 73  red...** This is
d51b0 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
d51c0 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29 20  sqlite3Atoi64() 
d51d0 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20 74  which requires t
d51e0 68 65 0d 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d  he..** input num
d51f0 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74  ber to be zero-t
d5200 65 72 6d 69 6e 61 74 65 64 2e 0d 0a 2a 2f 0d 0a  erminated...*/..
d5210 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
d5220 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  nt sqlite3GetInt
d5230 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  32(const char *z
d5240 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65  Num, int *pValue
d5250 29 7b 0d 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  ){..  sqlite_int
d5260 36 34 20 76 20 3d 20 30 3b 0d 0a 20 20 69 6e 74  64 v = 0;..  int
d5270 20 69 2c 20 63 3b 0d 0a 20 20 69 6e 74 20 6e 65   i, c;..  int ne
d5280 67 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 7a 4e  g = 0;..  if( zN
d5290 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0d 0a 20  um[0]=='-' ){.. 
d52a0 20 20 20 6e 65 67 20 3d 20 31 3b 0d 0a 20 20 20     neg = 1;..   
d52b0 20 7a 4e 75 6d 2b 2b 3b 0d 0a 20 20 7d 65 6c 73   zNum++;..  }els
d52c0 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  e if( zNum[0]=='
d52d0 2b 27 20 29 7b 0d 0a 20 20 20 20 7a 4e 75 6d 2b  +' ){..    zNum+
d52e0 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 77 68 69 6c 65  +;..  }..  while
d52f0 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29  ( zNum[0]=='0' )
d5300 20 7a 4e 75 6d 2b 2b 3b 0d 0a 20 20 66 6f 72 28   zNum++;..  for(
d5310 69 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20  i=0; i<11 && (c 
d5320 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29  = zNum[i] - '0')
d5330 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b  >=0 && c<=9; i++
d5340 29 7b 0d 0a 20 20 20 20 76 20 3d 20 76 2a 31 30  ){..    v = v*10
d5350 20 2b 20 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20   + c;..  }....  
d5360 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64  /* The longest d
d5370 65 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74  ecimal represent
d5380 61 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69  ation of a 32 bi
d5390 74 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20  t integer is 10 
d53a0 64 69 67 69 74 73 3a 0d 0a 20 20 2a 2a 0d 0a 20  digits:..  **.. 
d53b0 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
d53c0 31 32 33 34 35 36 37 38 39 30 0d 0a 20 20 2a 2a  1234567890..  **
d53d0 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 31 34       2^31 -> 214
d53e0 37 34 38 33 36 34 38 0d 0a 20 20 2a 2f 0d 0a 20  7483648..  */.. 
d53f0 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
d5400 20 29 3b 0d 0a 20 20 69 66 28 20 69 3e 31 30 20   );..  if( i>10 
d5410 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
d5420 3b 0d 0a 20 20 7d 0d 0a 20 20 74 65 73 74 63 61  ;..  }..  testca
d5430 73 65 28 20 76 2d 6e 65 67 3d 3d 32 31 34 37 34  se( v-neg==21474
d5440 38 33 36 34 37 20 29 3b 0d 0a 20 20 69 66 28 20  83647 );..  if( 
d5450 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37  v-neg>2147483647
d5460 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
d5470 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6e  0;..  }..  if( n
d5480 65 67 20 29 7b 0d 0a 20 20 20 20 76 20 3d 20 2d  eg ){..    v = -
d5490 76 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 56 61 6c  v;..  }..  *pVal
d54a0 75 65 20 3d 20 28 69 6e 74 29 76 3b 0d 0a 20 20  ue = (int)v;..  
d54b0 72 65 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 0d 0a  return 1;..}....
d54c0 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /*..** Return a 
d54d0 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  32-bit integer v
d54e0 61 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 66  alue extracted f
d54f0 72 6f 6d 20 61 20 73 74 72 69 6e 67 2e 20 20 49  rom a string.  I
d5500 66 20 74 68 65 0d 0a 2a 2a 20 73 74 72 69 6e 67  f the..** string
d5510 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
d5520 65 72 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20  er, just return 
d5530 30 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  0...*/..SQLITE_P
d5540 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
d5550 65 33 41 74 6f 69 28 63 6f 6e 73 74 20 63 68 61  e3Atoi(const cha
d5560 72 20 2a 7a 29 7b 0d 0a 20 20 69 6e 74 20 78 20  r *z){..  int x 
d5570 3d 20 30 3b 0d 0a 20 20 69 66 28 20 7a 20 29 20  = 0;..  if( z ) 
d5580 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28  sqlite3GetInt32(
d5590 7a 2c 20 26 78 29 3b 0d 0a 20 20 72 65 74 75 72  z, &x);..  retur
d55a0 6e 20 78 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n x;..}..../*..*
d55b0 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c  * The variable-l
d55c0 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e  ength integer en
d55d0 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c  coding is as fol
d55e0 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 4b 45  lows:..**..** KE
d55f0 59 3a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 41  Y:..**         A
d5600 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37   = 0xxxxxxx    7
d5610 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
d5620 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0d 0a  d one flag bit..
d5630 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31  **         B = 1
d5640 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74  xxxxxxx    7 bit
d5650 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e  s of data and on
d5660 65 20 66 6c 61 67 20 62 69 74 0d 0a 2a 2a 20 20  e flag bit..**  
d5670 20 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78         C = xxxxx
d5680 78 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66  xxx    8 bits of
d5690 20 64 61 74 61 0d 0a 2a 2a 0d 0a 2a 2a 20 20 37   data..**..**  7
d56a0 20 62 69 74 73 20 2d 20 41 0d 0a 2a 2a 20 31 34   bits - A..** 14
d56b0 20 62 69 74 73 20 2d 20 42 41 0d 0a 2a 2a 20 32   bits - BA..** 2
d56c0 31 20 62 69 74 73 20 2d 20 42 42 41 0d 0a 2a 2a  1 bits - BBA..**
d56d0 20 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0d   28 bits - BBBA.
d56e0 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42  .** 35 bits - BB
d56f0 42 42 41 0d 0a 2a 2a 20 34 32 20 62 69 74 73 20  BBA..** 42 bits 
d5700 2d 20 42 42 42 42 42 41 0d 0a 2a 2a 20 34 39 20  - BBBBBA..** 49 
d5710 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0d 0a  bits - BBBBBBA..
d5720 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42  ** 56 bits - BBB
d5730 42 42 42 42 41 0d 0a 2a 2a 20 36 34 20 62 69 74  BBBBA..** 64 bit
d5740 73 20 2d 20 42 42 42 42 42 42 42 42 43 0d 0a 2a  s - BBBBBBBBC..*
d5750 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74  /..../*..** Writ
d5760 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61  e a 64-bit varia
d5770 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
d5780 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61  er to memory sta
d5790 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0d 0a  rting at p[0]...
d57a0 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  ** The length of
d57b0 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c   data write will
d57c0 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e   be between 1 an
d57d0 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20  d 9 bytes.  The 
d57e0 6e 75 6d 62 65 72 0d 0a 2a 2a 20 6f 66 20 62 79  number..** of by
d57f0 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72  tes written is r
d5800 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  eturned...**..**
d5810 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67   A variable-leng
d5820 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69  th integer consi
d5830 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 72  sts of the lower
d5840 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68 20   7 bits of each 
d5850 62 79 74 65 0d 0a 2a 2a 20 66 6f 72 20 61 6c 6c  byte..** for all
d5860 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65   bytes that have
d5870 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65 74   the 8th bit set
d5880 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77 69   and one byte wi
d5890 74 68 20 74 68 65 20 38 74 68 0d 0a 2a 2a 20 62  th the 8th..** b
d58a0 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70  it clear.  Excep
d58b0 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20  t, if we get to 
d58c0 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74  the 9th byte, it
d58d0 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c   stores the full
d58e0 0d 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20  ..** 8 bits and 
d58f0 69 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65  is the last byte
d5900 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
d5910 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
d5920 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67  3PutVarint(unsig
d5930 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34  ned char *p, u64
d5940 20 76 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a   v){..  int i, j
d5950 2c 20 6e 3b 0d 0a 20 20 75 38 20 62 75 66 5b 31  , n;..  u8 buf[1
d5960 30 5d 3b 0d 0a 20 20 69 66 28 20 76 20 26 20 28  0];..  if( v & (
d5970 28 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30  ((u64)0xff000000
d5980 29 3c 3c 33 32 29 20 29 7b 0d 0a 20 20 20 20 70  )<<32) ){..    p
d5990 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0d 0a 20 20  [8] = (u8)v;..  
d59a0 20 20 76 20 3e 3e 3d 20 38 3b 0d 0a 20 20 20 20    v >>= 8;..    
d59b0 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69  for(i=7; i>=0; i
d59c0 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 70 5b 69 5d  --){..      p[i]
d59d0 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37   = (u8)((v & 0x7
d59e0 66 29 20 7c 20 30 78 38 30 29 3b 0d 0a 20 20 20  f) | 0x80);..   
d59f0 20 20 20 76 20 3e 3e 3d 20 37 3b 0d 0a 20 20 20     v >>= 7;..   
d5a00 20 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 39   }..    return 9
d5a10 3b 0d 0a 20 20 7d 20 20 20 20 0d 0a 20 20 6e 20  ;..  }    ..  n 
d5a20 3d 20 30 3b 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20  = 0;..  do{..   
d5a30 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29   buf[n++] = (u8)
d5a40 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78  ((v & 0x7f) | 0x
d5a50 38 30 29 3b 0d 0a 20 20 20 20 76 20 3e 3e 3d 20  80);..    v >>= 
d5a60 37 3b 0d 0a 20 20 7d 77 68 69 6c 65 28 20 76 21  7;..  }while( v!
d5a70 3d 30 20 29 3b 0d 0a 20 20 62 75 66 5b 30 5d 20  =0 );..  buf[0] 
d5a80 26 3d 20 30 78 37 66 3b 0d 0a 20 20 61 73 73 65  &= 0x7f;..  asse
d5a90 72 74 28 20 6e 3c 3d 39 20 29 3b 0d 0a 20 20 66  rt( n<=9 );..  f
d5aa0 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a  or(i=0, j=n-1; j
d5ab0 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0d  >=0; j--, i++){.
d5ac0 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b  .    p[i] = buf[
d5ad0 6a 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  j];..  }..  retu
d5ae0 72 6e 20 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn n;..}..../*..
d5af0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
d5b00 69 73 20 61 20 66 61 73 74 65 72 20 76 65 72 73  is a faster vers
d5b10 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 50 75  ion of sqlite3Pu
d5b20 74 56 61 72 69 6e 74 28 29 20 74 68 61 74 20 6f  tVarint() that o
d5b30 6e 6c 79 0d 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f  nly..** works fo
d5b40 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74 69 76  r 32-bit positiv
d5b50 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 77  e integers and w
d5b60 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69 7a 65  hich is optimize
d5b70 64 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 63 6f  d for..** the co
d5b80 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d 61  mmon case of sma
d5b90 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20  ll integers.  A 
d5ba0 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70  MACRO version, p
d5bb0 75 74 56 61 72 69 6e 74 33 32 2c 0d 0a 2a 2a 20  utVarint32,..** 
d5bc0 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63  is provided whic
d5bd0 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69  h inlines the si
d5be0 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20  ngle-byte case. 
d5bf0 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64   All code should
d5c00 20 75 73 65 0d 0a 2a 2a 20 74 68 65 20 4d 41 43   use..** the MAC
d5c10 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68  RO version as th
d5c20 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
d5c30 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  mes the single-b
d5c40 79 74 65 20 63 61 73 65 20 68 61 73 0d 0a 2a 2a  yte case has..**
d5c50 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61   already been ha
d5c60 6e 64 6c 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  ndled...*/..SQLI
d5c70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
d5c80 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33  qlite3PutVarint3
d5c90 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  2(unsigned char 
d5ca0 2a 70 2c 20 75 33 32 20 76 29 7b 0d 0a 23 69 66  *p, u32 v){..#if
d5cb0 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 32  ndef putVarint32
d5cc0 0d 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78  ..  if( (v & ~0x
d5cd0 37 66 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70  7f)==0 ){..    p
d5ce0 5b 30 5d 20 3d 20 76 3b 0d 0a 20 20 20 20 72 65  [0] = v;..    re
d5cf0 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 23 65  turn 1;..  }..#e
d5d00 6e 64 69 66 0d 0a 20 20 69 66 28 20 28 76 20 26  ndif..  if( (v &
d5d10 20 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0d   ~0x3fff)==0 ){.
d5d20 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29  .    p[0] = (u8)
d5d30 28 28 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b  ((v>>7) | 0x80);
d5d40 0d 0a 20 20 20 20 70 5b 31 5d 20 3d 20 28 75 38  ..    p[1] = (u8
d5d50 29 28 76 20 26 20 30 78 37 66 29 3b 0d 0a 20 20  )(v & 0x7f);..  
d5d60 20 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20 7d    return 2;..  }
d5d70 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
d5d80 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76  e3PutVarint(p, v
d5d90 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
d5da0 42 69 74 6d 61 73 6b 73 20 75 73 65 64 20 62 79  Bitmasks used by
d5db0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
d5dc0 74 28 29 2e 20 20 54 68 65 73 65 20 70 72 65 63  t().  These prec
d5dd0 6f 6d 70 75 74 65 64 20 63 6f 6e 73 74 61 6e 74  omputed constant
d5de0 73 0d 0a 2a 2a 20 61 72 65 20 64 65 66 69 6e 65  s..** are define
d5df0 64 20 68 65 72 65 20 72 61 74 68 65 72 20 74 68  d here rather th
d5e00 61 6e 20 73 69 6d 70 6c 79 20 70 75 74 74 69 6e  an simply puttin
d5e10 67 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 65  g the constant e
d5e20 78 70 72 65 73 73 69 6f 6e 73 0d 0a 2a 2a 20 69  xpressions..** i
d5e30 6e 6c 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  nline in order t
d5e40 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75  o work around bu
d5e50 67 73 20 69 6e 20 74 68 65 20 52 56 54 20 63 6f  gs in the RVT co
d5e60 6d 70 69 6c 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  mpiler...**..** 
d5e70 53 4c 4f 54 5f 32 5f 30 20 20 20 20 20 41 20 6d  SLOT_2_0     A m
d5e80 61 73 6b 20 66 6f 72 20 20 28 30 78 37 66 3c 3c  ask for  (0x7f<<
d5e90 31 34 29 20 7c 20 30 78 37 66 0d 0a 2a 2a 0d 0a  14) | 0x7f..**..
d5ea0 2a 2a 20 53 4c 4f 54 5f 34 5f 32 5f 30 20 20 20  ** SLOT_4_2_0   
d5eb0 41 20 6d 61 73 6b 20 66 6f 72 20 20 28 30 78 37  A mask for  (0x7
d5ec0 66 3c 3c 32 38 29 20 7c 20 53 4c 4f 54 5f 32 5f  f<<28) | SLOT_2_
d5ed0 30 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53  0..*/..#define S
d5ee0 4c 4f 54 5f 32 5f 30 20 20 20 20 20 30 78 30 30  LOT_2_0     0x00
d5ef0 31 66 63 30 37 66 0d 0a 23 64 65 66 69 6e 65 20  1fc07f..#define 
d5f00 53 4c 4f 54 5f 34 5f 32 5f 30 20 20 20 30 78 66  SLOT_4_2_0   0xf
d5f10 30 31 66 63 30 37 66 0d 0a 0d 0a 0d 0a 2f 2a 0d  01fc07f....../*.
d5f20 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69  .** Read a 64-bi
d5f30 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
d5f40 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
d5f50 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
d5f60 74 20 70 5b 30 5d 2e 0d 0a 2a 2a 20 52 65 74 75  t p[0]...** Retu
d5f70 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
d5f80 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68   bytes read.  Th
d5f90 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
d5fa0 64 20 69 6e 20 2a 76 2e 0d 0a 2a 2f 0d 0a 53 51  d in *v...*/..SQ
d5fb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20  LITE_PRIVATE u8 
d5fc0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
d5fd0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
d5fe0 63 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29  char *p, u64 *v)
d5ff0 7b 0d 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0d  {..  u32 a,b,s;.
d6000 0a 0d 0a 20 20 61 20 3d 20 2a 70 3b 0d 0a 20 20  ...  a = *p;..  
d6010 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b  /* a: p0 (unmask
d6020 65 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28 21 28  ed) */..  if (!(
d6030 61 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d 0a 20  a&0x80))..  {.. 
d6040 20 20 20 2a 76 20 3d 20 61 3b 0d 0a 20 20 20 20     *v = a;..    
d6050 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a  return 1;..  }..
d6060 0d 0a 20 20 70 2b 2b 3b 0d 0a 20 20 62 20 3d 20  ..  p++;..  b = 
d6070 2a 70 3b 0d 0a 20 20 2f 2a 20 62 3a 20 70 31 20  *p;..  /* b: p1 
d6080 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0d 0a 20  (unmasked) */.. 
d6090 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0d   if (!(b&0x80)).
d60a0 0a 20 20 7b 0d 0a 20 20 20 20 61 20 26 3d 20 30  .  {..    a &= 0
d60b0 78 37 66 3b 0d 0a 20 20 20 20 61 20 3d 20 61 3c  x7f;..    a = a<
d60c0 3c 37 3b 0d 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <7;..    a |= b;
d60d0 0d 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0d 0a 20  ..    *v = a;.. 
d60e0 20 20 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20     return 2;..  
d60f0 7d 0d 0a 0d 0a 20 20 2f 2a 20 56 65 72 69 66 79  }....  /* Verify
d6100 20 74 68 61 74 20 63 6f 6e 73 74 61 6e 74 73 20   that constants 
d6110 61 72 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20  are precomputed 
d6120 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0d 0a 20 20  correctly */..  
d6130 61 73 73 65 72 74 28 20 53 4c 4f 54 5f 32 5f 30  assert( SLOT_2_0
d6140 20 3d 3d 20 28 28 30 78 37 66 3c 3c 31 34 29 20   == ((0x7f<<14) 
d6150 7c 20 28 30 78 37 66 29 29 20 29 3b 0d 0a 20 20  | (0x7f)) );..  
d6160 61 73 73 65 72 74 28 20 53 4c 4f 54 5f 34 5f 32  assert( SLOT_4_2
d6170 5f 30 20 3d 3d 20 28 28 30 78 66 55 3c 3c 32 38  _0 == ((0xfU<<28
d6180 29 20 7c 20 28 30 78 37 66 3c 3c 31 34 29 20 7c  ) | (0x7f<<14) |
d6190 20 28 30 78 37 66 29 29 20 29 3b 0d 0a 0d 0a 20   (0x7f)) );.... 
d61a0 20 70 2b 2b 3b 0d 0a 20 20 61 20 3d 20 61 3c 3c   p++;..  a = a<<
d61b0 31 34 3b 0d 0a 20 20 61 20 7c 3d 20 2a 70 3b 0d  14;..  a |= *p;.
d61c0 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20  .  /* a: p0<<14 
d61d0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p2 (unmasked) 
d61e0 2a 2f 0d 0a 20 20 69 66 20 28 21 28 61 26 30 78  */..  if (!(a&0x
d61f0 38 30 29 29 0d 0a 20 20 7b 0d 0a 20 20 20 20 61  80))..  {..    a
d6200 20 26 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0d 0a 20   &= SLOT_2_0;.. 
d6210 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0d 0a 20     b &= 0x7f;.. 
d6220 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0d 0a 20 20     b = b<<7;..  
d6230 20 20 61 20 7c 3d 20 62 3b 0d 0a 20 20 20 20 2a    a |= b;..    *
d6240 76 20 3d 20 61 3b 0d 0a 20 20 20 20 72 65 74 75  v = a;..    retu
d6250 72 6e 20 33 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 3;..  }....  
d6260 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c  /* CSE1 from bel
d6270 6f 77 20 2a 2f 0d 0a 20 20 61 20 26 3d 20 53 4c  ow */..  a &= SL
d6280 4f 54 5f 32 5f 30 3b 0d 0a 20 20 70 2b 2b 3b 0d  OT_2_0;..  p++;.
d6290 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0d 0a 20  .  b = b<<14;.. 
d62a0 20 62 20 7c 3d 20 2a 70 3b 0d 0a 20 20 2f 2a 20   b |= *p;..  /* 
d62b0 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28  b: p1<<14 | p3 (
d62c0 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0d 0a 20 20  unmasked) */..  
d62d0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0d 0a  if (!(b&0x80))..
d62e0 20 20 7b 0d 0a 20 20 20 20 62 20 26 3d 20 53 4c    {..    b &= SL
d62f0 4f 54 5f 32 5f 30 3b 0d 0a 20 20 20 20 2f 2a 20  OT_2_0;..    /* 
d6300 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f  moved CSE1 up */
d6310 0d 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  ..    /* a &= (0
d6320 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
d6330 20 2a 2f 0d 0a 20 20 20 20 61 20 3d 20 61 3c 3c   */..    a = a<<
d6340 37 3b 0d 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0d  7;..    a |= b;.
d6350 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0d 0a 20 20  .    *v = a;..  
d6360 20 20 72 65 74 75 72 6e 20 34 3b 0d 0a 20 20 7d    return 4;..  }
d6370 0d 0a 0d 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c  ....  /* a: p0<<
d6380 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
d6390 20 2a 2f 0d 0a 20 20 2f 2a 20 62 3a 20 70 31 3c   */..  /* b: p1<
d63a0 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b  <14 | p3 (unmask
d63b0 65 64 29 20 2a 2f 0d 0a 20 20 2f 2a 20 31 3a 73  ed) */..  /* 1:s
d63c0 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c  ave off p0<<21 |
d63d0 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20   p1<<14 | p2<<7 
d63e0 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f  | p3 (masked) */
d63f0 0d 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45  ..  /* moved CSE
d6400 31 20 75 70 20 2a 2f 0d 0a 20 20 2f 2a 20 61 20  1 up */..  /* a 
d6410 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
d6420 78 37 66 29 3b 20 2a 2f 0d 0a 20 20 62 20 26 3d  x7f); */..  b &=
d6430 20 53 4c 4f 54 5f 32 5f 30 3b 0d 0a 20 20 73 20   SLOT_2_0;..  s 
d6440 3d 20 61 3b 0d 0a 20 20 2f 2a 20 73 3a 20 70 30  = a;..  /* s: p0
d6450 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65  <<14 | p2 (maske
d6460 64 29 20 2a 2f 0d 0a 0d 0a 20 20 70 2b 2b 3b 0d  d) */....  p++;.
d6470 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0d 0a 20  .  a = a<<14;.. 
d6480 20 61 20 7c 3d 20 2a 70 3b 0d 0a 20 20 2f 2a 20   a |= *p;..  /* 
d6490 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c  a: p0<<28 | p2<<
d64a0 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65  14 | p4 (unmaske
d64b0 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28 21 28 61  d) */..  if (!(a
d64c0 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d 0a 20 20  &0x80))..  {..  
d64d0 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70    /* we can skip
d64e0 20 74 68 65 73 65 20 63 61 75 73 65 20 74 68 65   these cause the
d64f0 79 20 77 65 72 65 20 28 65 66 66 65 63 74 69 76  y were (effectiv
d6500 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20  ely) done above 
d6510 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f  in calc'ing s */
d6520 0d 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  ..    /* a &= (0
d6530 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c  x7f<<28)|(0x7f<<
d6540 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0d 0a  14)|(0x7f); */..
d6550 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37      /* b &= (0x7
d6560 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a  f<<14)|(0x7f); *
d6570 2f 0d 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  /..    b = b<<7;
d6580 0d 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0d 0a 20  ..    a |= b;.. 
d6590 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0d 0a 20     s = s>>18;.. 
d65a0 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
d65b0 3c 3c 33 32 20 7c 20 61 3b 0d 0a 20 20 20 20 72  <<32 | a;..    r
d65c0 65 74 75 72 6e 20 35 3b 0d 0a 20 20 7d 0d 0a 0d  eturn 5;..  }...
d65d0 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66  .  /* 2:save off
d65e0 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34   p0<<21 | p1<<14
d65f0 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d   | p2<<7 | p3 (m
d6600 61 73 6b 65 64 29 20 2a 2f 0d 0a 20 20 73 20 3d  asked) */..  s =
d6610 20 73 3c 3c 37 3b 0d 0a 20 20 73 20 7c 3d 20 62   s<<7;..  s |= b
d6620 3b 0d 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32  ;..  /* s: p0<<2
d6630 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c  1 | p1<<14 | p2<
d6640 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29  <7 | p3 (masked)
d6650 20 2a 2f 0d 0a 0d 0a 20 20 70 2b 2b 3b 0d 0a 20   */....  p++;.. 
d6660 20 62 20 3d 20 62 3c 3c 31 34 3b 0d 0a 20 20 62   b = b<<14;..  b
d6670 20 7c 3d 20 2a 70 3b 0d 0a 20 20 2f 2a 20 62 3a   |= *p;..  /* b:
d6680 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34   p1<<28 | p3<<14
d6690 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 64 29   | p5 (unmasked)
d66a0 20 2a 2f 0d 0a 20 20 69 66 20 28 21 28 62 26 30   */..  if (!(b&0
d66b0 78 38 30 29 29 0d 0a 20 20 7b 0d 0a 20 20 20 20  x80))..  {..    
d66c0 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74  /* we can skip t
d66d0 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61 73  his cause it was
d66e0 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64   (effectively) d
d66f0 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c  one above in cal
d6700 63 27 69 6e 67 20 73 20 2a 2f 0d 0a 20 20 20 20  c'ing s */..    
d6710 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* b &= (0x7f<<2
d6720 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
d6730 78 37 66 29 3b 20 2a 2f 0d 0a 20 20 20 20 61 20  x7f); */..    a 
d6740 26 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0d 0a 20 20  &= SLOT_2_0;..  
d6750 20 20 61 20 3d 20 61 3c 3c 37 3b 0d 0a 20 20 20    a = a<<7;..   
d6760 20 61 20 7c 3d 20 62 3b 0d 0a 20 20 20 20 73 20   a |= b;..    s 
d6770 3d 20 73 3e 3e 31 38 3b 0d 0a 20 20 20 20 2a 76  = s>>18;..    *v
d6780 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20   = ((u64)s)<<32 
d6790 7c 20 61 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  | a;..    return
d67a0 20 36 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 2b   6;..  }....  p+
d67b0 2b 3b 0d 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  +;..  a = a<<14;
d67c0 0d 0a 20 20 61 20 7c 3d 20 2a 70 3b 0d 0a 20 20  ..  a |= *p;..  
d67d0 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70  /* a: p2<<28 | p
d67e0 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d 61  4<<14 | p6 (unma
d67f0 73 6b 65 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28  sked) */..  if (
d6800 21 28 61 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d  !(a&0x80))..  {.
d6810 0a 20 20 20 20 61 20 26 3d 20 53 4c 4f 54 5f 34  .    a &= SLOT_4
d6820 5f 32 5f 30 3b 0d 0a 20 20 20 20 62 20 26 3d 20  _2_0;..    b &= 
d6830 53 4c 4f 54 5f 32 5f 30 3b 0d 0a 20 20 20 20 62  SLOT_2_0;..    b
d6840 20 3d 20 62 3c 3c 37 3b 0d 0a 20 20 20 20 61 20   = b<<7;..    a 
d6850 7c 3d 20 62 3b 0d 0a 20 20 20 20 73 20 3d 20 73  |= b;..    s = s
d6860 3e 3e 31 31 3b 0d 0a 20 20 20 20 2a 76 20 3d 20  >>11;..    *v = 
d6870 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
d6880 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b  ;..    return 7;
d6890 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 53  ..  }....  /* CS
d68a0 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f  E2 from below */
d68b0 0d 0a 20 20 61 20 26 3d 20 53 4c 4f 54 5f 32 5f  ..  a &= SLOT_2_
d68c0 30 3b 0d 0a 20 20 70 2b 2b 3b 0d 0a 20 20 62 20  0;..  p++;..  b 
d68d0 3d 20 62 3c 3c 31 34 3b 0d 0a 20 20 62 20 7c 3d  = b<<14;..  b |=
d68e0 20 2a 70 3b 0d 0a 20 20 2f 2a 20 62 3a 20 70 33   *p;..  /* b: p3
d68f0 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20  <<28 | p5<<14 | 
d6900 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p7 (unmasked) */
d6910 0d 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30  ..  if (!(b&0x80
d6920 29 29 0d 0a 20 20 7b 0d 0a 20 20 20 20 62 20 26  ))..  {..    b &
d6930 3d 20 53 4c 4f 54 5f 34 5f 32 5f 30 3b 0d 0a 20  = SLOT_4_2_0;.. 
d6940 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32     /* moved CSE2
d6950 20 75 70 20 2a 2f 0d 0a 20 20 20 20 2f 2a 20 61   up */..    /* a
d6960 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
d6970 30 78 37 66 29 3b 20 2a 2f 0d 0a 20 20 20 20 61  0x7f); */..    a
d6980 20 3d 20 61 3c 3c 37 3b 0d 0a 20 20 20 20 61 20   = a<<7;..    a 
d6990 7c 3d 20 62 3b 0d 0a 20 20 20 20 73 20 3d 20 73  |= b;..    s = s
d69a0 3e 3e 34 3b 0d 0a 20 20 20 20 2a 76 20 3d 20 28  >>4;..    *v = (
d69b0 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b  (u64)s)<<32 | a;
d69c0 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0d  ..    return 8;.
d69d0 0a 20 20 7d 0d 0a 0d 0a 20 20 70 2b 2b 3b 0d 0a  .  }....  p++;..
d69e0 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0d 0a 20 20    a = a<<15;..  
d69f0 61 20 7c 3d 20 2a 70 3b 0d 0a 20 20 2f 2a 20 61  a |= *p;..  /* a
d6a00 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31  : p4<<29 | p6<<1
d6a10 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64  5 | p8 (unmasked
d6a20 29 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 6d 6f 76  ) */....  /* mov
d6a30 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0d 0a 20  ed CSE2 up */.. 
d6a40 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
d6a50 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28  29)|(0x7f<<15)|(
d6a60 30 78 66 66 29 3b 20 2a 2f 0d 0a 20 20 62 20 26  0xff); */..  b &
d6a70 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0d 0a 20 20 62  = SLOT_2_0;..  b
d6a80 20 3d 20 62 3c 3c 38 3b 0d 0a 20 20 61 20 7c 3d   = b<<8;..  a |=
d6a90 20 62 3b 0d 0a 0d 0a 20 20 73 20 3d 20 73 3c 3c   b;....  s = s<<
d6aa0 34 3b 0d 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b  4;..  b = p[-4];
d6ab0 0d 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0d 0a  ..  b &= 0x7f;..
d6ac0 20 20 62 20 3d 20 62 3e 3e 33 3b 0d 0a 20 20 73    b = b>>3;..  s
d6ad0 20 7c 3d 20 62 3b 0d 0a 0d 0a 20 20 2a 76 20 3d   |= b;....  *v =
d6ae0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20   ((u64)s)<<32 | 
d6af0 61 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 39  a;....  return 9
d6b00 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
d6b10 65 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72  ead a 32-bit var
d6b20 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
d6b30 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79  eger from memory
d6b40 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30   starting at p[0
d6b50 5d 2e 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ]...** Return th
d6b60 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
d6b70 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c  s read.  The val
d6b80 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ue is stored in 
d6b90 2a 76 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  *v...**..** If t
d6ba0 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72 65 64  he varint stored
d6bb0 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61 72 67   in p[0] is larg
d6bc0 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69 74 20  er than can fit 
d6bd0 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e 73 69  in a 32-bit unsi
d6be0 67 6e 65 64 0d 0a 2a 2a 20 69 6e 74 65 67 65 72  gned..** integer
d6bf0 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74 6f  , then set *v to
d6c00 20 30 78 66 66 66 66 66 66 66 66 2e 0d 0a 2a 2a   0xffffffff...**
d6c10 0d 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72  ..** A MACRO ver
d6c20 73 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33  sion, getVarint3
d6c30 32 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77  2, is provided w
d6c40 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65  hich inlines the
d6c50 20 0d 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74   ..** single-byt
d6c60 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64  e case.  All cod
d6c70 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65  e should use the
d6c80 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61   MACRO version a
d6c90 73 20 0d 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  s ..** this func
d6ca0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65  tion assumes the
d6cb0 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73   single-byte cas
d6cc0 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
d6cd0 65 6e 20 68 61 6e 64 6c 65 64 2e 0d 0a 2a 2f 0d  en handled...*/.
d6ce0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d6cf0 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  u8 sqlite3GetVar
d6d00 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69  int32(const unsi
d6d10 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33  gned char *p, u3
d6d20 32 20 2a 76 29 7b 0d 0a 20 20 75 33 32 20 61 2c  2 *v){..  u32 a,
d6d30 62 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 31  b;....  /* The 1
d6d40 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f 76 65  -byte case.  Ove
d6d50 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68 65 20  rwhelmingly the 
d6d60 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 61  most common.  Ha
d6d70 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0d 0a 20 20  ndled inline..  
d6d80 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61 72  ** by the getVar
d6d90 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f 0d  in32() macro */.
d6da0 0a 20 20 61 20 3d 20 2a 70 3b 0d 0a 20 20 2f 2a  .  a = *p;..  /*
d6db0 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64   a: p0 (unmasked
d6dc0 29 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 67 65  ) */..#ifndef ge
d6dd0 74 56 61 72 69 6e 74 33 32 0d 0a 20 20 69 66 20  tVarint32..  if 
d6de0 28 21 28 61 26 30 78 38 30 29 29 0d 0a 20 20 7b  (!(a&0x80))..  {
d6df0 0d 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  ..    /* Values 
d6e00 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 32  between 0 and 12
d6e10 37 20 2a 2f 0d 0a 20 20 20 20 2a 76 20 3d 20 61  7 */..    *v = a
d6e20 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ;..    return 1;
d6e30 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
d6e40 0a 20 20 2f 2a 20 54 68 65 20 32 2d 62 79 74 65  .  /* The 2-byte
d6e50 20 63 61 73 65 20 2a 2f 0d 0a 20 20 70 2b 2b 3b   case */..  p++;
d6e60 0d 0a 20 20 62 20 3d 20 2a 70 3b 0d 0a 20 20 2f  ..  b = *p;..  /
d6e70 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65  * b: p1 (unmaske
d6e80 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28 21 28 62  d) */..  if (!(b
d6e90 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d 0a 20 20  &0x80))..  {..  
d6ea0 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
d6eb0 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38  een 128 and 1638
d6ec0 33 20 2a 2f 0d 0a 20 20 20 20 61 20 26 3d 20 30  3 */..    a &= 0
d6ed0 78 37 66 3b 0d 0a 20 20 20 20 61 20 3d 20 61 3c  x7f;..    a = a<
d6ee0 3c 37 3b 0d 0a 20 20 20 20 2a 76 20 3d 20 61 20  <7;..    *v = a 
d6ef0 7c 20 62 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  | b;..    return
d6f00 20 32 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   2;..  }....  /*
d6f10 20 54 68 65 20 33 2d 62 79 74 65 20 63 61 73 65   The 3-byte case
d6f20 20 2a 2f 0d 0a 20 20 70 2b 2b 3b 0d 0a 20 20 61   */..  p++;..  a
d6f30 20 3d 20 61 3c 3c 31 34 3b 0d 0a 20 20 61 20 7c   = a<<14;..  a |
d6f40 3d 20 2a 70 3b 0d 0a 20 20 2f 2a 20 61 3a 20 70  = *p;..  /* a: p
d6f50 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61  0<<14 | p2 (unma
d6f60 73 6b 65 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28  sked) */..  if (
d6f70 21 28 61 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d  !(a&0x80))..  {.
d6f80 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62  .    /* Values b
d6f90 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e 64  etween 16384 and
d6fa0 20 32 30 39 37 31 35 31 20 2a 2f 0d 0a 20 20 20   2097151 */..   
d6fb0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
d6fc0 7c 28 30 78 37 66 29 3b 0d 0a 20 20 20 20 62 20  |(0x7f);..    b 
d6fd0 26 3d 20 30 78 37 66 3b 0d 0a 20 20 20 20 62 20  &= 0x7f;..    b 
d6fe0 3d 20 62 3c 3c 37 3b 0d 0a 20 20 20 20 2a 76 20  = b<<7;..    *v 
d6ff0 3d 20 61 20 7c 20 62 3b 0d 0a 20 20 20 20 72 65  = a | b;..    re
d7000 74 75 72 6e 20 33 3b 0d 0a 20 20 7d 0d 0a 0d 0a  turn 3;..  }....
d7010 20 20 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61    /* A 32-bit va
d7020 72 69 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  rint is used to 
d7030 73 74 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72  store size infor
d7040 6d 61 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73  mation in btrees
d7050 2e 0d 0a 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20  ...  ** Objects 
d7060 61 72 65 20 72 61 72 65 6c 79 20 6c 61 72 67 65  are rarely large
d7070 72 20 74 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69  r than 2MiB limi
d7080 74 20 6f 66 20 61 20 33 2d 62 79 74 65 20 76 61  t of a 3-byte va
d7090 72 69 6e 74 2e 0d 0a 20 20 2a 2a 20 41 20 33 2d  rint...  ** A 3-
d70a0 62 79 74 65 20 76 61 72 69 6e 74 20 69 73 20 73  byte varint is s
d70b0 75 66 66 69 63 69 65 6e 74 2c 20 66 6f 72 20 65  ufficient, for e
d70c0 78 61 6d 70 6c 65 2c 20 74 6f 20 72 65 63 6f 72  xample, to recor
d70d0 64 20 74 68 65 20 73 69 7a 65 0d 0a 20 20 2a 2a  d the size..  **
d70e0 20 6f 66 20 61 20 31 30 34 38 35 36 39 2d 62 79   of a 1048569-by
d70f0 74 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e  te BLOB or strin
d7100 67 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57  g...  **..  ** W
d7110 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c 20 74 68  e only unroll th
d7120 65 20 66 69 72 73 74 20 31 2d 2c 20 32 2d 2c 20  e first 1-, 2-, 
d7130 61 6e 64 20 33 2d 20 62 79 74 65 20 63 61 73 65  and 3- byte case
d7140 73 2e 20 20 54 68 65 20 76 65 72 79 0d 0a 20 20  s.  The very..  
d7150 2a 2a 20 72 61 72 65 20 6c 61 72 67 65 72 20 63  ** rare larger c
d7160 61 73 65 73 20 63 61 6e 20 62 65 20 68 61 6e 64  ases can be hand
d7170 6c 65 64 20 62 79 20 74 68 65 20 73 6c 6f 77 65  led by the slowe
d7180 72 20 36 34 2d 62 69 74 20 76 61 72 69 6e 74 0d  r 64-bit varint.
d7190 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e 0d 0a  .  ** routine...
d71a0 20 20 2a 2f 0d 0a 23 69 66 20 31 0d 0a 20 20 7b    */..#if 1..  {
d71b0 0d 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0d 0a  ..    u64 v64;..
d71c0 20 20 20 20 75 38 20 6e 3b 0d 0a 0d 0a 20 20 20      u8 n;....   
d71d0 20 70 20 2d 3d 20 32 3b 0d 0a 20 20 20 20 6e 20   p -= 2;..    n 
d71e0 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  = sqlite3GetVari
d71f0 6e 74 28 70 2c 20 26 76 36 34 29 3b 0d 0a 20 20  nt(p, &v64);..  
d7200 20 20 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26    assert( n>3 &&
d7210 20 6e 3c 3d 39 20 29 3b 0d 0a 20 20 20 20 69 66   n<=9 );..    if
d7220 28 20 28 76 36 34 20 26 20 53 51 4c 49 54 45 5f  ( (v64 & SQLITE_
d7230 4d 41 58 5f 55 33 32 29 21 3d 76 36 34 20 29 7b  MAX_U32)!=v64 ){
d7240 0d 0a 20 20 20 20 20 20 2a 76 20 3d 20 30 78 66  ..      *v = 0xf
d7250 66 66 66 66 66 66 66 3b 0d 0a 20 20 20 20 7d 65  fffffff;..    }e
d7260 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2a 76 20 3d  lse{..      *v =
d7270 20 28 75 33 32 29 76 36 34 3b 0d 0a 20 20 20 20   (u32)v64;..    
d7280 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b  }..    return n;
d7290 0d 0a 20 20 7d 0d 0a 0d 0a 23 65 6c 73 65 0d 0a  ..  }....#else..
d72a0 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69    /* For followi
d72b0 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f  ng code (kept fo
d72c0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63  r historical rec
d72d0 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20  ord only) shows 
d72e0 61 6e 0d 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69  an..  ** unrolli
d72f0 6e 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e  ng for the 3- an
d7300 64 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20  d 4-byte varint 
d7310 63 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64  cases.  This cod
d7320 65 20 69 73 0d 0a 20 20 2a 2a 20 73 6c 69 67 68  e is..  ** sligh
d7330 74 6c 79 20 66 61 73 74 65 72 2c 20 62 75 74 20  tly faster, but 
d7340 69 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65  it is also large
d7350 72 20 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65  r and much harde
d7360 72 20 74 6f 20 74 65 73 74 2e 0d 0a 20 20 2a 2f  r to test...  */
d7370 0d 0a 20 20 70 2b 2b 3b 0d 0a 20 20 62 20 3d 20  ..  p++;..  b = 
d7380 62 3c 3c 31 34 3b 0d 0a 20 20 62 20 7c 3d 20 2a  b<<14;..  b |= *
d7390 70 3b 0d 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c  p;..  /* b: p1<<
d73a0 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65  14 | p3 (unmaske
d73b0 64 29 20 2a 2f 0d 0a 20 20 69 66 20 28 21 28 62  d) */..  if (!(b
d73c0 26 30 78 38 30 29 29 0d 0a 20 20 7b 0d 0a 20 20  &0x80))..  {..  
d73d0 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
d73e0 65 65 6e 20 32 30 39 37 31 35 32 20 61 6e 64 20  een 2097152 and 
d73f0 32 36 38 34 33 35 34 35 35 20 2a 2f 0d 0a 20 20  268435455 */..  
d7400 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    b &= (0x7f<<14
d7410 29 7c 28 30 78 37 66 29 3b 0d 0a 20 20 20 20 61  )|(0x7f);..    a
d7420 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
d7430 30 78 37 66 29 3b 0d 0a 20 20 20 20 61 20 3d 20  0x7f);..    a = 
d7440 61 3c 3c 37 3b 0d 0a 20 20 20 20 2a 76 20 3d 20  a<<7;..    *v = 
d7450 61 20 7c 20 62 3b 0d 0a 20 20 20 20 72 65 74 75  a | b;..    retu
d7460 72 6e 20 34 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 4;..  }....  
d7470 70 2b 2b 3b 0d 0a 20 20 61 20 3d 20 61 3c 3c 31  p++;..  a = a<<1
d7480 34 3b 0d 0a 20 20 61 20 7c 3d 20 2a 70 3b 0d 0a  4;..  a |= *p;..
d7490 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c    /* a: p0<<28 |
d74a0 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e   p2<<14 | p4 (un
d74b0 6d 61 73 6b 65 64 29 20 2a 2f 0d 0a 20 20 69 66  masked) */..  if
d74c0 20 28 21 28 61 26 30 78 38 30 29 29 0d 0a 20 20   (!(a&0x80))..  
d74d0 7b 0d 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73  {..    /* Values
d74e0 20 20 62 65 74 77 65 65 6e 20 32 36 38 34 33 35    between 268435
d74f0 34 35 36 20 61 6e 64 20 33 34 33 35 39 37 33 38  456 and 34359738
d7500 33 36 37 20 2a 2f 0d 0a 20 20 20 20 61 20 26 3d  367 */..    a &=
d7510 20 53 4c 4f 54 5f 34 5f 32 5f 30 3b 0d 0a 20 20   SLOT_4_2_0;..  
d7520 20 20 62 20 26 3d 20 53 4c 4f 54 5f 34 5f 32 5f    b &= SLOT_4_2_
d7530 30 3b 0d 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37  0;..    b = b<<7
d7540 3b 0d 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20  ;..    *v = a | 
d7550 62 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 35  b;..    return 5
d7560 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 57  ;..  }....  /* W
d7570 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68  e can only reach
d7580 20 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e   this point when
d7590 20 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75   reading a corru
d75a0 70 74 20 64 61 74 61 62 61 73 65 0d 0a 20 20 2a  pt database..  *
d75b0 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74  * file.  In that
d75c0 20 63 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74   case we are not
d75d0 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20   in any hurry.  
d75e0 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 69 76  Use the (relativ
d75f0 65 6c 79 0d 0a 20 20 2a 2a 20 73 6c 6f 77 29 20  ely..  ** slow) 
d7600 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20  general-purpose 
d7610 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
d7620 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 78  () routine to ex
d7630 74 72 61 63 74 20 74 68 65 0d 0a 20 20 2a 2a 20  tract the..  ** 
d7640 76 61 6c 75 65 2e 20 2a 2f 0d 0a 20 20 7b 0d 0a  value. */..  {..
d7650 20 20 20 20 75 36 34 20 76 36 34 3b 0d 0a 20 20      u64 v64;..  
d7660 20 20 75 38 20 6e 3b 0d 0a 0d 0a 20 20 20 20 70    u8 n;....    p
d7670 20 2d 3d 20 34 3b 0d 0a 20 20 20 20 6e 20 3d 20   -= 4;..    n = 
d7680 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
d7690 28 70 2c 20 26 76 36 34 29 3b 0d 0a 20 20 20 20  (p, &v64);..    
d76a0 61 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e  assert( n>5 && n
d76b0 3c 3d 39 20 29 3b 0d 0a 20 20 20 20 2a 76 20 3d  <=9 );..    *v =
d76c0 20 28 75 33 32 29 76 36 34 3b 0d 0a 20 20 20 20   (u32)v64;..    
d76d0 72 65 74 75 72 6e 20 6e 3b 0d 0a 20 20 7d 0d 0a  return n;..  }..
d76e0 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  #endif..}..../*.
d76f0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
d7700 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
d7710 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 64  hat will be need
d7720 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
d7730 67 69 76 65 6e 0d 0a 2a 2a 20 36 34 2d 62 69 74  given..** 64-bit
d7740 20 69 6e 74 65 67 65 72 2e 0d 0a 2a 2f 0d 0a 53   integer...*/..S
d7750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d7760 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  t sqlite3VarintL
d7770 65 6e 28 75 36 34 20 76 29 7b 0d 0a 20 20 69 6e  en(u64 v){..  in
d7780 74 20 69 20 3d 20 30 3b 0d 0a 20 20 64 6f 7b 0d  t i = 0;..  do{.
d7790 0a 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 20 20 76  .    i++;..    v
d77a0 20 3e 3e 3d 20 37 3b 0d 0a 20 20 7d 77 68 69 6c   >>= 7;..  }whil
d77b0 65 28 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59  e( v!=0 && ALWAY
d77c0 53 28 69 3c 39 29 20 29 3b 0d 0a 20 20 72 65 74  S(i<9) );..  ret
d77d0 75 72 6e 20 69 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  urn i;..}....../
d77e0 2a 0d 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72  *..** Read or wr
d77f0 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20  ite a four-byte 
d7800 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
d7810 65 72 20 76 61 6c 75 65 2e 0d 0a 2a 2f 0d 0a 53  er value...*/..S
d7820 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
d7830 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  2 sqlite3Get4byt
d7840 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0d  e(const u8 *p){.
d7850 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c  .  return (p[0]<
d7860 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36  <24) | (p[1]<<16
d7870 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20  ) | (p[2]<<8) | 
d7880 70 5b 33 5d 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45  p[3];..}..SQLITE
d7890 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
d78a0 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e  lite3Put4byte(un
d78b0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
d78c0 75 33 32 20 76 29 7b 0d 0a 20 20 70 5b 30 5d 20  u32 v){..  p[0] 
d78d0 3d 20 28 75 38 29 28 76 3e 3e 32 34 29 3b 0d 0a  = (u8)(v>>24);..
d78e0 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e    p[1] = (u8)(v>
d78f0 3e 31 36 29 3b 0d 0a 20 20 70 5b 32 5d 20 3d 20  >16);..  p[2] = 
d7900 28 75 38 29 28 76 3e 3e 38 29 3b 0d 0a 20 20 70  (u8)(v>>8);..  p
d7910 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0d 0a 7d 0d  [3] = (u8)v;..}.
d7920 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72  ......./*..** Tr
d7930 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
d7940 20 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74   byte of Hex int
d7950 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a 2a  o an integer...*
d7960 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
d7970 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72  nly works if h r
d7980 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64  eally is a valid
d7990 20 68 65 78 61 64 65 63 69 6d 61 6c 0d 0a 2a 2a   hexadecimal..**
d79a0 20 63 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e   character:  0..
d79b0 39 61 2e 2e 66 41 2e 2e 46 0d 0a 2a 2f 0d 0a 53  9a..fA..F..*/..S
d79c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
d79d0 20 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e 74   sqlite3HexToInt
d79e0 28 69 6e 74 20 68 29 7b 0d 0a 20 20 61 73 73 65  (int h){..  asse
d79f0 72 74 28 20 28 68 3e 3d 27 30 27 20 26 26 20 68  rt( (h>='0' && h
d7a00 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27  <='9') ||  (h>='
d7a10 61 27 20 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c  a' && h<='f') ||
d7a20 20 20 28 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d    (h>='A' && h<=
d7a30 27 46 27 29 20 29 3b 0d 0a 23 69 66 64 65 66 20  'F') );..#ifdef 
d7a40 53 51 4c 49 54 45 5f 41 53 43 49 49 0d 0a 20 20  SQLITE_ASCII..  
d7a50 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29  h += 9*(1&(h>>6)
d7a60 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  );..#endif..#ifd
d7a70 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
d7a80 0d 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28  ..  h += 9*(1&~(
d7a90 68 3e 3e 34 29 29 3b 0d 0a 23 65 6e 64 69 66 0d  h>>4));..#endif.
d7aa0 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 68  .  return (u8)(h
d7ab0 20 26 20 30 78 66 29 3b 0d 0a 7d 0d 0a 0d 0a 23   & 0xf);..}....#
d7ac0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
d7ad0 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
d7ae0 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  ERAL) || defined
d7af0 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45  (SQLITE_HAS_CODE
d7b00 43 29 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65  C)../*..** Conve
d7b10 72 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61  rt a BLOB litera
d7b20 6c 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78  l of the form "x
d7b30 27 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69  'hhhhhh'" into i
d7b40 74 73 20 62 69 6e 61 72 79 0d 0a 2a 2a 20 76 61  ts binary..** va
d7b50 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  lue.  Return a p
d7b60 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69  ointer to its bi
d7b70 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61  nary value.  Spa
d7b80 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0d 0a  ce to hold the..
d7b90 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 65 20  ** binary value 
d7ba0 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
d7bb0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e  d from malloc an
d7bc0 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  d must be freed 
d7bd0 62 79 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69  by..** the calli
d7be0 6e 67 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d  ng routine...*/.
d7bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d7c00 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78  void *sqlite3Hex
d7c10 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a  ToBlob(sqlite3 *
d7c20 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
d7c30 7a 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20 63 68  z, int n){..  ch
d7c40 61 72 20 2a 7a 42 6c 6f 62 3b 0d 0a 20 20 69 6e  ar *zBlob;..  in
d7c50 74 20 69 3b 0d 0a 0d 0a 20 20 7a 42 6c 6f 62 20  t i;....  zBlob 
d7c60 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
d7c70 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
d7c80 20 6e 2f 32 20 2b 20 31 29 3b 0d 0a 20 20 6e 2d   n/2 + 1);..  n-
d7c90 2d 3b 0d 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20  -;..  if( zBlob 
d7ca0 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ){..    for(i=0;
d7cb0 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0d 0a 20 20   i<n; i+=2){..  
d7cc0 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d      zBlob[i/2] =
d7cd0 20 28 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e   (sqlite3HexToIn
d7ce0 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 73 71  t(z[i])<<4) | sq
d7cf0 6c 69 74 65 33 48 65 78 54 6f 49 6e 74 28 7a 5b  lite3HexToInt(z[
d7d00 69 2b 31 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  i+1]);..    }.. 
d7d10 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20     zBlob[i/2] = 
d7d20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  0;..  }..  retur
d7d30 6e 20 7a 42 6c 6f 62 3b 0d 0a 7d 0d 0a 23 65 6e  n zBlob;..}..#en
d7d40 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
d7d50 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
d7d60 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43   || SQLITE_HAS_C
d7d70 4f 44 45 43 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  ODEC */..../*..*
d7d80 2a 20 4c 6f 67 20 61 6e 20 65 72 72 6f 72 20 74  * Log an error t
d7d90 68 61 74 20 69 73 20 61 6e 20 41 50 49 20 63 61  hat is an API ca
d7da0 6c 6c 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69  ll on a connecti
d7db0 6f 6e 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20  on pointer that 
d7dc0 73 68 6f 75 6c 64 0d 0a 2a 2a 20 6e 6f 74 20 68  should..** not h
d7dd0 61 76 65 20 62 65 65 6e 20 75 73 65 64 2e 20 20  ave been used.  
d7de0 54 68 65 20 22 74 79 70 65 22 20 6f 66 20 63 6f  The "type" of co
d7df0 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  nnection pointer
d7e00 20 69 73 20 67 69 76 65 6e 20 61 73 20 74 68 65   is given as the
d7e10 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20  ..** argument.  
d7e20 54 68 65 20 7a 54 79 70 65 20 69 73 20 61 20 77  The zType is a w
d7e30 6f 72 64 20 6c 69 6b 65 20 22 4e 55 4c 4c 22 20  ord like "NULL" 
d7e40 6f 72 20 22 63 6c 6f 73 65 64 22 20 6f 72 20 22  or "closed" or "
d7e50 69 6e 76 61 6c 69 64 22 2e 0d 0a 2a 2f 0d 0a 73  invalid"...*/..s
d7e60 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 67 42 61  tatic void logBa
d7e70 64 43 6f 6e 6e 65 63 74 69 6f 6e 28 63 6f 6e 73  dConnection(cons
d7e80 74 20 63 68 61 72 20 2a 7a 54 79 70 65 29 7b 0d  t char *zType){.
d7e90 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53  .  sqlite3_log(S
d7ea0 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 0d 0a  QLITE_MISUSE, ..
d7eb0 20 20 20 20 20 22 41 50 49 20 63 61 6c 6c 20 77       "API call w
d7ec0 69 74 68 20 25 73 20 64 61 74 61 62 61 73 65 20  ith %s database 
d7ed0 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74  connection point
d7ee0 65 72 22 2c 0d 0a 20 20 20 20 20 7a 54 79 70 65  er",..     zType
d7ef0 0d 0a 20 20 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ..  );..}..../*.
d7f00 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b  .** Check to mak
d7f10 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61  e sure we have a
d7f20 20 76 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65   valid db pointe
d7f30 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73  r.  This test is
d7f40 20 6e 6f 74 0d 0a 2a 2a 20 66 6f 6f 6c 70 72 6f   not..** foolpro
d7f50 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73 20 70  of but it does p
d7f60 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65 61 73  rovide some meas
d7f70 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74 69 6f  ure of protectio
d7f80 6e 20 61 67 61 69 6e 73 74 0d 0a 2a 2a 20 6d 69  n against..** mi
d7f90 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74 65  suse of the inte
d7fa0 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70 61  rface such as pa
d7fb0 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e  ssing in db poin
d7fc0 74 65 72 73 20 74 68 61 74 20 61 72 65 0d 0a 2a  ters that are..*
d7fd0 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20  * NULL or which 
d7fe0 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f  have been previo
d7ff0 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66  usly closed.  If
d8000 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
d8010 74 75 72 6e 73 0d 0a 2a 2a 20 31 20 69 74 20 6d  turns..** 1 it m
d8020 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62  eans that the db
d8030 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69   pointer is vali
d8040 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68  d and 0 if it sh
d8050 6f 75 6c 64 20 6e 6f 74 20 62 65 0d 0a 2a 2a 20  ould not be..** 
d8060 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72  dereferenced for
d8070 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68   any reason.  Th
d8080 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
d8090 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65  on should invoke
d80a0 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55  ..** SQLITE_MISU
d80b0 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0d  SE immediately..
d80c0 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 53  .**..** sqlite3S
d80d0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72  afetyCheckOk() r
d80e0 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68 65  equires that the
d80f0 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76   db pointer be v
d8100 61 6c 69 64 20 66 6f 72 0d 0a 2a 2a 20 75 73 65  alid for..** use
d8110 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  .  sqlite3Safety
d8120 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20  CheckSickOrOk() 
d8130 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e  allows a db poin
d8140 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20  ter that failed 
d8150 74 6f 0d 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70  to..** open prop
d8160 65 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20  erly and is not 
d8170 66 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20  fit for general 
d8180 75 73 65 20 62 75 74 20 77 68 69 63 68 20 63 61  use but which ca
d8190 6e 20 62 65 0d 0a 2a 2a 20 75 73 65 64 20 61 73  n be..** used as
d81a0 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
d81b0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
d81c0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73   or sqlite3_clos
d81d0 65 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  e()...*/..SQLITE
d81e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
d81f0 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
d8200 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d  k(sqlite3 *db){.
d8210 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0d 0a 20  .  u32 magic;.. 
d8220 20 69 66 28 20 64 62 3d 3d 30 20 29 7b 0d 0a 20   if( db==0 ){.. 
d8230 20 20 20 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74     logBadConnect
d8240 69 6f 6e 28 22 4e 55 4c 4c 22 29 3b 0d 0a 20 20  ion("NULL");..  
d8250 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
d8260 0d 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e  ..  magic = db->
d8270 6d 61 67 69 63 3b 0d 0a 20 20 69 66 28 20 6d 61  magic;..  if( ma
d8280 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic!=SQLITE_MAGI
d8290 43 5f 4f 50 45 4e 20 29 7b 0d 0a 20 20 20 20 69  C_OPEN ){..    i
d82a0 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
d82b0 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
d82c0 29 20 29 7b 0d 0a 20 20 20 20 20 20 74 65 73 74  ) ){..      test
d82d0 63 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f  case( sqlite3Glo
d82e0 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d  balConfig.xLog!=
d82f0 30 20 29 3b 0d 0a 20 20 20 20 20 20 6c 6f 67 42  0 );..      logB
d8300 61 64 43 6f 6e 6e 65 63 74 69 6f 6e 28 22 75 6e  adConnection("un
d8310 6f 70 65 6e 65 64 22 29 3b 0d 0a 20 20 20 20 7d  opened");..    }
d8320 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
d8330 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72  .  }else{..    r
d8340 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 7d  eturn 1;..  }..}
d8350 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
d8360 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
d8370 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
d8380 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20  sqlite3 *db){.. 
d8390 20 75 33 32 20 6d 61 67 69 63 3b 0d 0a 20 20 6d   u32 magic;..  m
d83a0 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63  agic = db->magic
d83b0 3b 0d 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d  ;..  if( magic!=
d83c0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43  SQLITE_MAGIC_SIC
d83d0 4b 20 26 26 0d 0a 20 20 20 20 20 20 6d 61 67 69  K &&..      magi
d83e0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
d83f0 4f 50 45 4e 20 26 26 0d 0a 20 20 20 20 20 20 6d  OPEN &&..      m
d8400 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
d8410 49 43 5f 42 55 53 59 20 29 7b 0d 0a 20 20 20 20  IC_BUSY ){..    
d8420 74 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65  testcase( sqlite
d8430 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c  3GlobalConfig.xL
d8440 6f 67 21 3d 30 20 29 3b 0d 0a 20 20 20 20 6c 6f  og!=0 );..    lo
d8450 67 42 61 64 43 6f 6e 6e 65 63 74 69 6f 6e 28 22  gBadConnection("
d8460 69 6e 76 61 6c 69 64 22 29 3b 0d 0a 20 20 20 20  invalid");..    
d8470 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 65 6c  return 0;..  }el
d8480 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  se{..    return 
d8490 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  1;..  }..}..../*
d84a0 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ..** Attempt to 
d84b0 61 64 64 2c 20 73 75 62 73 74 72 61 63 74 2c 20  add, substract, 
d84c0 6f 72 20 6d 75 6c 74 69 70 6c 79 20 74 68 65 20  or multiply the 
d84d0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 76 61  64-bit signed va
d84e0 6c 75 65 20 69 42 20 61 67 61 69 6e 73 74 0d 0a  lue iB against..
d84f0 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 36 34 2d  ** the other 64-
d8500 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
d8510 65 72 20 61 74 20 2a 70 41 20 61 6e 64 20 73 74  er at *pA and st
d8520 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
d8530 6e 20 2a 70 41 2e 0d 0a 2a 2a 20 52 65 74 75 72  n *pA...** Retur
d8540 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  n 0 on success. 
d8550 20 4f 72 20 69 66 20 74 68 65 20 6f 70 65 72 61   Or if the opera
d8560 74 69 6f 6e 20 77 6f 75 6c 64 20 68 61 76 65 20  tion would have 
d8570 72 65 73 75 6c 74 65 64 20 69 6e 20 61 6e 0d 0a  resulted in an..
d8580 2a 2a 20 6f 76 65 72 66 6c 6f 77 2c 20 6c 65 61  ** overflow, lea
d8590 76 65 20 2a 70 41 20 75 6e 63 68 61 6e 67 65 64  ve *pA unchanged
d85a0 20 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 0d 0a   and return 1...
d85b0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
d85c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 64  TE int sqlite3Ad
d85d0 64 49 6e 74 36 34 28 69 36 34 20 2a 70 41 2c 20  dInt64(i64 *pA, 
d85e0 69 36 34 20 69 42 29 7b 0d 0a 20 20 69 36 34 20  i64 iB){..  i64 
d85f0 69 41 20 3d 20 2a 70 41 3b 0d 0a 20 20 74 65 73  iA = *pA;..  tes
d8600 74 63 61 73 65 28 20 69 41 3d 3d 30 20 29 3b 20  tcase( iA==0 ); 
d8610 74 65 73 74 63 61 73 65 28 20 69 41 3d 3d 31 20  testcase( iA==1 
d8620 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
d8630 69 42 3d 3d 2d 31 20 29 3b 20 74 65 73 74 63 61  iB==-1 ); testca
d8640 73 65 28 20 69 42 3d 3d 30 20 29 3b 0d 0a 20 20  se( iB==0 );..  
d8650 69 66 28 20 69 42 3e 3d 30 20 29 7b 0d 0a 20 20  if( iB>=0 ){..  
d8660 20 20 74 65 73 74 63 61 73 65 28 20 69 41 3e 30    testcase( iA>0
d8670 20 26 26 20 4c 41 52 47 45 53 54 5f 49 4e 54 36   && LARGEST_INT6
d8680 34 20 2d 20 69 41 20 3d 3d 20 69 42 20 29 3b 0d  4 - iA == iB );.
d8690 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
d86a0 41 3e 30 20 26 26 20 4c 41 52 47 45 53 54 5f 49  A>0 && LARGEST_I
d86b0 4e 54 36 34 20 2d 20 69 41 20 3d 3d 20 69 42 20  NT64 - iA == iB 
d86c0 2d 20 31 20 29 3b 0d 0a 20 20 20 20 69 66 28 20  - 1 );..    if( 
d86d0 69 41 3e 30 20 26 26 20 4c 41 52 47 45 53 54 5f  iA>0 && LARGEST_
d86e0 49 4e 54 36 34 20 2d 20 69 41 20 3c 20 69 42 20  INT64 - iA < iB 
d86f0 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20  ) return 1;..   
d8700 20 2a 70 41 20 2b 3d 20 69 42 3b 0d 0a 20 20 7d   *pA += iB;..  }
d8710 65 6c 73 65 7b 0d 0a 20 20 20 20 74 65 73 74 63  else{..    testc
d8720 61 73 65 28 20 69 41 3c 30 20 26 26 20 2d 28 69  ase( iA<0 && -(i
d8730 41 20 2b 20 4c 41 52 47 45 53 54 5f 49 4e 54 36  A + LARGEST_INT6
d8740 34 29 20 3d 3d 20 69 42 20 2b 20 31 20 29 3b 0d  4) == iB + 1 );.
d8750 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
d8760 41 3c 30 20 26 26 20 2d 28 69 41 20 2b 20 4c 41  A<0 && -(iA + LA
d8770 52 47 45 53 54 5f 49 4e 54 36 34 29 20 3d 3d 20  RGEST_INT64) == 
d8780 69 42 20 2b 20 32 20 29 3b 0d 0a 20 20 20 20 69  iB + 2 );..    i
d8790 66 28 20 69 41 3c 30 20 26 26 20 2d 28 69 41 20  f( iA<0 && -(iA 
d87a0 2b 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29  + LARGEST_INT64)
d87b0 20 3e 20 69 42 20 2b 20 31 20 29 20 72 65 74 75   > iB + 1 ) retu
d87c0 72 6e 20 31 3b 0d 0a 20 20 20 20 2a 70 41 20 2b  rn 1;..    *pA +
d87d0 3d 20 69 42 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  = iB;..  }..  re
d87e0 74 75 72 6e 20 30 3b 20 0d 0a 7d 0d 0a 53 51 4c  turn 0; ..}..SQL
d87f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
d8800 73 71 6c 69 74 65 33 53 75 62 49 6e 74 36 34 28  sqlite3SubInt64(
d8810 69 36 34 20 2a 70 41 2c 20 69 36 34 20 69 42 29  i64 *pA, i64 iB)
d8820 7b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 69  {..  testcase( i
d8830 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36  B==SMALLEST_INT6
d8840 34 2b 31 20 29 3b 0d 0a 20 20 69 66 28 20 69 42  4+1 );..  if( iB
d8850 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34  ==SMALLEST_INT64
d8860 20 29 7b 0d 0a 20 20 20 20 74 65 73 74 63 61 73   ){..    testcas
d8870 65 28 20 28 2a 70 41 29 3d 3d 28 2d 31 29 20 29  e( (*pA)==(-1) )
d8880 3b 20 74 65 73 74 63 61 73 65 28 20 28 2a 70 41  ; testcase( (*pA
d8890 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66 28  )==0 );..    if(
d88a0 20 28 2a 70 41 29 3e 3d 30 20 29 20 72 65 74 75   (*pA)>=0 ) retu
d88b0 72 6e 20 31 3b 0d 0a 20 20 20 20 2a 70 41 20 2d  rn 1;..    *pA -
d88c0 3d 20 69 42 3b 0d 0a 20 20 20 20 72 65 74 75 72  = iB;..    retur
d88d0 6e 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  n 0;..  }else{..
d88e0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
d88f0 65 33 41 64 64 49 6e 74 36 34 28 70 41 2c 20 2d  e3AddInt64(pA, -
d8900 69 42 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 64  iB);..  }..}..#d
d8910 65 66 69 6e 65 20 54 57 4f 50 4f 57 45 52 33 32  efine TWOPOWER32
d8920 20 28 28 28 69 36 34 29 31 29 3c 3c 33 32 29 0d   (((i64)1)<<32).
d8930 0a 23 64 65 66 69 6e 65 20 54 57 4f 50 4f 57 45  .#define TWOPOWE
d8940 52 33 31 20 28 28 28 69 36 34 29 31 29 3c 3c 33  R31 (((i64)1)<<3
d8950 31 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  1)..SQLITE_PRIVA
d8960 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75  TE int sqlite3Mu
d8970 6c 49 6e 74 36 34 28 69 36 34 20 2a 70 41 2c 20  lInt64(i64 *pA, 
d8980 69 36 34 20 69 42 29 7b 0d 0a 20 20 69 36 34 20  i64 iB){..  i64 
d8990 69 41 20 3d 20 2a 70 41 3b 0d 0a 20 20 69 36 34  iA = *pA;..  i64
d89a0 20 69 41 31 2c 20 69 41 30 2c 20 69 42 31 2c 20   iA1, iA0, iB1, 
d89b0 69 42 30 2c 20 72 3b 0d 0a 0d 0a 20 20 69 41 31  iB0, r;....  iA1
d89c0 20 3d 20 69 41 2f 54 57 4f 50 4f 57 45 52 33 32   = iA/TWOPOWER32
d89d0 3b 0d 0a 20 20 69 41 30 20 3d 20 69 41 20 25 20  ;..  iA0 = iA % 
d89e0 54 57 4f 50 4f 57 45 52 33 32 3b 0d 0a 20 20 69  TWOPOWER32;..  i
d89f0 42 31 20 3d 20 69 42 2f 54 57 4f 50 4f 57 45 52  B1 = iB/TWOPOWER
d8a00 33 32 3b 0d 0a 20 20 69 42 30 20 3d 20 69 42 20  32;..  iB0 = iB 
d8a10 25 20 54 57 4f 50 4f 57 45 52 33 32 3b 0d 0a 20  % TWOPOWER32;.. 
d8a20 20 69 66 28 20 69 41 31 2a 69 42 31 20 21 3d 20   if( iA1*iB1 != 
d8a30 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20  0 ) return 1;.. 
d8a40 20 61 73 73 65 72 74 28 20 69 41 31 2a 69 42 30   assert( iA1*iB0
d8a50 3d 3d 30 20 7c 7c 20 69 41 30 2a 69 42 31 3d 3d  ==0 || iA0*iB1==
d8a60 30 20 29 3b 0d 0a 20 20 72 20 3d 20 69 41 31 2a  0 );..  r = iA1*
d8a70 69 42 30 20 2b 20 69 41 30 2a 69 42 31 3b 0d 0a  iB0 + iA0*iB1;..
d8a80 20 20 74 65 73 74 63 61 73 65 28 20 72 3d 3d 28    testcase( r==(
d8a90 2d 54 57 4f 50 4f 57 45 52 33 31 29 2d 31 20 29  -TWOPOWER31)-1 )
d8aa0 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 72  ;..  testcase( r
d8ab0 3d 3d 28 2d 54 57 4f 50 4f 57 45 52 33 31 29 20  ==(-TWOPOWER31) 
d8ac0 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
d8ad0 72 3d 3d 54 57 4f 50 4f 57 45 52 33 31 20 29 3b  r==TWOPOWER31 );
d8ae0 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 3d  ..  testcase( r=
d8af0 3d 54 57 4f 50 4f 57 45 52 33 31 2d 31 20 29 3b  =TWOPOWER31-1 );
d8b00 0d 0a 20 20 69 66 28 20 72 3c 28 2d 54 57 4f 50  ..  if( r<(-TWOP
d8b10 4f 57 45 52 33 31 29 20 7c 7c 20 72 3e 3d 54 57  OWER31) || r>=TW
d8b20 4f 50 4f 57 45 52 33 31 20 29 20 72 65 74 75 72  OPOWER31 ) retur
d8b30 6e 20 31 3b 0d 0a 20 20 72 20 2a 3d 20 54 57 4f  n 1;..  r *= TWO
d8b40 50 4f 57 45 52 33 32 3b 0d 0a 20 20 69 66 28 20  POWER32;..  if( 
d8b50 73 71 6c 69 74 65 33 41 64 64 49 6e 74 36 34 28  sqlite3AddInt64(
d8b60 26 72 2c 20 69 41 30 2a 69 42 30 29 20 29 20 72  &r, iA0*iB0) ) r
d8b70 65 74 75 72 6e 20 31 3b 0d 0a 20 20 2a 70 41 20  eturn 1;..  *pA 
d8b80 3d 20 72 3b 0d 0a 20 20 72 65 74 75 72 6e 20 30  = r;..  return 0
d8b90 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
d8ba0 6f 6d 70 75 74 65 20 74 68 65 20 61 62 73 6f 6c  ompute the absol
d8bb0 75 74 65 20 76 61 6c 75 65 20 6f 66 20 61 20 33  ute value of a 3
d8bc0 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
d8bd0 65 67 65 72 2c 20 6f 66 20 70 6f 73 73 69 62 6c  eger, of possibl
d8be0 65 2e 20 20 4f 72 20 0d 0a 2a 2a 20 69 66 20 74  e.  Or ..** if t
d8bf0 68 65 20 69 6e 74 65 67 65 72 20 68 61 73 20 61  he integer has a
d8c00 20 76 61 6c 75 65 20 6f 66 20 2d 32 31 34 37 34   value of -21474
d8c10 38 33 36 34 38 2c 20 72 65 74 75 72 6e 20 2b 32  83648, return +2
d8c20 31 34 37 34 38 33 36 34 37 0d 0a 2a 2f 0d 0a 53  147483647..*/..S
d8c30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d8c40 74 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33  t sqlite3AbsInt3
d8c50 32 28 69 6e 74 20 78 29 7b 0d 0a 20 20 69 66 28  2(int x){..  if(
d8c60 20 78 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 78   x>=0 ) return x
d8c70 3b 0d 0a 20 20 69 66 28 20 78 3d 3d 28 69 6e 74  ;..  if( x==(int
d8c80 29 30 78 38 30 30 30 30 30 30 30 20 29 20 72 65  )0x80000000 ) re
d8c90 74 75 72 6e 20 30 78 37 66 66 66 66 66 66 66 3b  turn 0x7fffffff;
d8ca0 0d 0a 20 20 72 65 74 75 72 6e 20 2d 78 3b 0d 0a  ..  return -x;..
d8cb0 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  }....#ifdef SQLI
d8cc0 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41  TE_ENABLE_8_3_NA
d8cd0 4d 45 53 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 53  MES../*..** If S
d8ce0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33  QLITE_ENABLE_8_3
d8cf0 5f 4e 41 4d 45 53 20 69 73 20 73 65 74 20 61 74  _NAMES is set at
d8d00 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 61 6e   compile-time an
d8d10 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  d if the databas
d8d20 65 0d 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69  e..** filename i
d8d30 6e 20 7a 42 61 73 65 46 69 6c 65 6e 61 6d 65 20  n zBaseFilename 
d8d40 69 73 20 61 20 55 52 49 20 77 69 74 68 20 74 68  is a URI with th
d8d50 65 20 22 38 5f 33 5f 6e 61 6d 65 73 3d 31 22 20  e "8_3_names=1" 
d8d60 70 61 72 61 6d 65 74 65 72 20 61 6e 64 0d 0a 2a  parameter and..*
d8d70 2a 20 69 66 20 66 69 6c 65 6e 61 6d 65 20 69 6e  * if filename in
d8d80 20 7a 5b 5d 20 68 61 73 20 61 20 73 75 66 66 69   z[] has a suffi
d8d90 78 20 28 61 2e 6b 2e 61 2e 20 22 65 78 74 65 6e  x (a.k.a. "exten
d8da0 73 69 6f 6e 22 29 20 74 68 61 74 20 69 73 20 6c  sion") that is l
d8db0 6f 6e 67 65 72 20 74 68 61 6e 0d 0a 2a 2a 20 74  onger than..** t
d8dc0 68 72 65 65 20 63 68 61 72 61 63 74 65 72 73 2c  hree characters,
d8dd0 20 74 68 65 6e 20 73 68 6f 72 74 65 6e 20 74 68   then shorten th
d8de0 65 20 73 75 66 66 69 78 20 6f 6e 20 7a 5b 5d 20  e suffix on z[] 
d8df0 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74 20 74  to be the last t
d8e00 68 72 65 65 0d 0a 2a 2a 20 63 68 61 72 61 63 74  hree..** charact
d8e10 65 72 73 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ers of the origi
d8e20 6e 61 6c 20 73 75 66 66 69 78 2e 0d 0a 2a 2a 0d  nal suffix...**.
d8e30 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e  .** If SQLITE_EN
d8e40 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d 45 53 20 69  ABLE_8_3_NAMES i
d8e50 73 20 73 65 74 20 74 6f 20 32 20 61 74 20 63 6f  s set to 2 at co
d8e60 6d 70 69 6c 65 2d 74 69 6d 65 2c 20 74 68 65 6e  mpile-time, then
d8e70 20 61 6c 77 61 79 73 0d 0a 2a 2a 20 64 6f 20 74   always..** do t
d8e80 68 65 20 73 75 66 66 69 78 20 73 68 6f 72 74 65  he suffix shorte
d8e90 6e 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20  ning regardless 
d8ea0 6f 66 20 55 52 49 20 70 61 72 61 6d 65 74 65 72  of URI parameter
d8eb0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 61 6d 70 6c  ...**..** Exampl
d8ec0 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  es:..**..**     
d8ed0 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20  test.db-journal 
d8ee0 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 6e 61 6c     =>   test.nal
d8ef0 0d 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e 64 62  ..**     test.db
d8f00 2d 77 61 6c 20 20 20 20 20 20 20 20 3d 3e 20 20  -wal        =>  
d8f10 20 74 65 73 74 2e 77 61 6c 0d 0a 2a 2a 20 20 20   test.wal..**   
d8f20 20 20 74 65 73 74 2e 64 62 2d 73 68 6d 20 20 20    test.db-shm   
d8f30 20 20 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 73       =>   test.s
d8f40 68 6d 0d 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e  hm..**     test.
d8f50 64 62 2d 6d 6a 37 66 33 33 31 39 66 61 20 3d 3e  db-mj7f3319fa =>
d8f60 20 20 20 74 65 73 74 2e 39 66 61 0d 0a 2a 2f 0d     test.9fa..*/.
d8f70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d8f80 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6c 65  void sqlite3File
d8f90 53 75 66 66 69 78 33 28 63 6f 6e 73 74 20 63 68  Suffix3(const ch
d8fa0 61 72 20 2a 7a 42 61 73 65 46 69 6c 65 6e 61 6d  ar *zBaseFilenam
d8fb0 65 2c 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 23 69  e, char *z){..#i
d8fc0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
d8fd0 38 5f 33 5f 4e 41 4d 45 53 3c 32 0d 0a 20 20 69  8_3_NAMES<2..  i
d8fe0 66 28 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62  f( sqlite3_uri_b
d8ff0 6f 6f 6c 65 61 6e 28 7a 42 61 73 65 46 69 6c 65  oolean(zBaseFile
d9000 6e 61 6d 65 2c 20 22 38 5f 33 5f 6e 61 6d 65 73  name, "8_3_names
d9010 22 2c 20 30 29 20 29 0d 0a 23 65 6e 64 69 66 0d  ", 0) )..#endif.
d9020 0a 20 20 7b 0d 0a 20 20 20 20 69 6e 74 20 69 2c  .  {..    int i,
d9030 20 73 7a 3b 0d 0a 20 20 20 20 73 7a 20 3d 20 73   sz;..    sz = s
d9040 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
d9050 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 73 7a  );..    for(i=sz
d9060 2d 31 3b 20 69 3e 30 20 26 26 20 7a 5b 69 5d 21  -1; i>0 && z[i]!
d9070 3d 27 2f 27 20 26 26 20 7a 5b 69 5d 21 3d 27 2e  ='/' && z[i]!='.
d9080 27 3b 20 69 2d 2d 29 7b 7d 0d 0a 20 20 20 20 69  '; i--){}..    i
d9090 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27 20 26 26 20  f( z[i]=='.' && 
d90a0 41 4c 57 41 59 53 28 73 7a 3e 69 2b 34 29 20 29  ALWAYS(sz>i+4) )
d90b0 20 6d 65 6d 6d 6f 76 65 28 26 7a 5b 69 2b 31 5d   memmove(&z[i+1]
d90c0 2c 20 26 7a 5b 73 7a 2d 33 5d 2c 20 34 29 3b 0d  , &z[sz-3], 4);.
d90d0 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  .  }..}..#endif.
d90e0 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
d90f0 2a 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63  ** End of util.c
d9100 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
d9110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9130 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
d9140 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
d9150 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.c **********
d9160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9180 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31  **/../*..** 2001
d9190 20 53 65 70 74 65 6d 62 65 72 20 32 32 0d 0a 2a   September 22..*
d91a0 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
d91b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
d91c0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
d91d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
d91e0 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
d91f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
d9200 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
d9210 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
d9220 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
d9230 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
d9240 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
d9250 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
d9260 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
d9270 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
d9280 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
d9290 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
d92a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
d92b0 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
d92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9300 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  *****..** This i
d9310 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
d9320 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20  tion of generic 
d9330 68 61 73 68 2d 74 61 62 6c 65 73 0d 0a 2a 2a 20  hash-tables..** 
d9340 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0d  used in SQLite..
d9350 0a 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  .*/../* #include
d9360 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a   <assert.h> */..
d9370 0d 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d  ../* Turn bulk m
d9380 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73  emory into a has
d9390 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62  h table object b
d93a0 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
d93b0 68 65 0d 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66  he..** fields of
d93c0 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74   the Hash struct
d93d0 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 22 70 4e  ure...**..** "pN
d93e0 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ew" is a pointer
d93f0 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62   to the hash tab
d9400 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
d9410 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0d 0a 2a   initialized...*
d9420 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
d9430 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
d9440 73 68 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65  shInit(Hash *pNe
d9450 77 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  w){..  assert( p
d9460 4e 65 77 21 3d 30 20 29 3b 0d 0a 20 20 70 4e 65  New!=0 );..  pNe
d9470 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0d 0a 20  w->first = 0;.. 
d9480 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30   pNew->count = 0
d9490 3b 0d 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a  ;..  pNew->htsiz
d94a0 65 20 3d 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e  e = 0;..  pNew->
d94b0 68 74 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ht = 0;..}..../*
d94c0 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72   Remove all entr
d94d0 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20  ies from a hash 
d94e0 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20  table.  Reclaim 
d94f0 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 20  all memory...** 
d9500 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  Call this routin
d9510 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61  e to delete a ha
d9520 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72  sh table or to r
d9530 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c  eset a hash tabl
d9540 65 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  e..** to the emp
d9550 74 79 20 73 74 61 74 65 2e 0d 0a 2a 2f 0d 0a 53  ty state...*/..S
d9560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
d9570 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  id sqlite3HashCl
d9580 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0d 0a  ear(Hash *pH){..
d9590 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
d95a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  ;         /* For
d95b0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
d95c0 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
d95d0 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20  e table */....  
d95e0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
d95f0 0d 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66  ..  elem = pH->f
d9600 69 72 73 74 3b 0d 0a 20 20 70 48 2d 3e 66 69 72  irst;..  pH->fir
d9610 73 74 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74  st = 0;..  sqlit
d9620 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b  e3_free(pH->ht);
d9630 0d 0a 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0d  ..  pH->ht = 0;.
d9640 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20  .  pH->htsize = 
d9650 30 3b 0d 0a 20 20 77 68 69 6c 65 28 20 65 6c 65  0;..  while( ele
d9660 6d 20 29 7b 0d 0a 20 20 20 20 48 61 73 68 45 6c  m ){..    HashEl
d9670 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20  em *next_elem = 
d9680 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0d 0a 20 20 20  elem->next;..   
d9690 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c   sqlite3_free(el
d96a0 65 6d 29 3b 0d 0a 20 20 20 20 65 6c 65 6d 20 3d  em);..    elem =
d96b0 20 6e 65 78 74 5f 65 6c 65 6d 3b 0d 0a 20 20 7d   next_elem;..  }
d96c0 0d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  ..  pH->count = 
d96d0 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
d96e0 54 68 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63  The hashing func
d96f0 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  tion...*/..stati
d9700 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73  c unsigned int s
d9710 74 72 48 61 73 68 28 63 6f 6e 73 74 20 63 68 61  trHash(const cha
d9720 72 20 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b  r *z, int nKey){
d9730 0d 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0d 0a  ..  int h = 0;..
d9740 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d    assert( nKey>=
d9750 30 20 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 6e  0 );..  while( n
d9760 4b 65 79 20 3e 20 30 20 20 29 7b 0d 0a 09 20 20  Key > 0  ){...  
d9770 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b 0d 0a  const char *p;..
d9780 09 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20  .unsigned short 
d9790 73 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  s = sqlite3Utf8R
d97a0 65 61 64 28 7a 2c 20 26 70 29 3b 0d 0a 20 20 20  ead(z, &p);..   
d97b0 20 6e 4b 65 79 2d 3d 70 2d 7a 3b 0d 0a 09 7a 20   nKey-=p-z;...z 
d97c0 3d 20 70 3b 0d 0a 09 68 20 3d 20 28 68 3c 3c 33  = p;...h = (h<<3
d97d0 29 20 5e 20 68 20 5e 20 6f 61 73 55 70 70 65 72  ) ^ h ^ oasUpper
d97e0 32 4c 6f 77 65 72 5b 73 5d 3b 0d 0a 09 20 20 2f  2Lower[s];...  /
d97f0 2f 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20  /h = (h<<3) ^ h 
d9800 5e 20 2a 7a 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20  ^ *z++;..  }..  
d9810 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66  return h & 0x7ff
d9820 66 66 66 66 66 3b 0d 0a 7d 0d 0a 2f 2a 0d 0a 73  fffff;..}../*..s
d9830 74 61 74 69 63 20 69 6e 74 20 73 74 72 48 61 73  tatic int strHas
d9840 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  h(const void *pK
d9850 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0d 0a  ey, int nKey){..
d9860 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
d9870 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
d9880 70 4b 65 79 3b 0d 0a 20 20 69 6e 74 20 68 20 3d  pKey;..  int h =
d9890 20 30 3b 0d 0a 20 20 69 66 28 20 6e 4b 65 79 3c   0;..  if( nKey<
d98a0 3d 30 20 29 20 6e 4b 65 79 20 3d 20 73 74 72 6c  =0 ) nKey = strl
d98b0 65 6e 28 7a 29 3b 0d 0a 20 20 77 68 69 6c 65 28  en(z);..  while(
d98c0 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0d 0a 20   nKey > 0  ){.. 
d98d0 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
d98e0 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 72  h ^ sqlite3Upper
d98f0 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65  ToLower[(unsigne
d9900 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0d 0a 20  d char)*z++];.. 
d9910 20 20 20 6e 4b 65 79 2d 2d 3b 0d 0a 20 20 7d 0d     nKey--;..  }.
d9920 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78  .  return h & 0x
d9930 37 66 66 66 66 66 66 66 3b 0d 0a 7d 0d 0a 2a 2f  7fffffff;..}..*/
d9940 0d 0a 0d 0a 0d 0a 2f 2a 20 4c 69 6e 6b 20 70 4e  ....../* Link pN
d9950 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20  ew element into 
d9960 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
d9970 48 2e 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30  H.  If pEntry!=0
d9980 20 74 68 65 6e 20 61 6c 73 6f 0d 0a 2a 2a 20 69   then also..** i
d9990 6e 73 65 72 74 20 70 4e 65 77 20 69 6e 74 6f 20  nsert pNew into 
d99a0 74 68 65 20 70 45 6e 74 72 79 20 68 61 73 68 20  the pEntry hash 
d99b0 62 75 63 6b 65 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  bucket...*/..sta
d99c0 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45  tic void insertE
d99d0 6c 65 6d 65 6e 74 28 0d 0a 20 20 48 61 73 68 20  lement(..  Hash 
d99e0 2a 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pH,            
d99f0 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    /* The complet
d9a00 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0d  e hash table */.
d9a10 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70  .  struct _ht *p
d9a20 45 6e 74 72 79 2c 20 20 20 20 2f 2a 20 54 68 65  Entry,    /* The
d9a30 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63   entry into whic
d9a40 68 20 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74  h pNew is insert
d9a50 65 64 20 2a 2f 0d 0a 20 20 48 61 73 68 45 6c 65  ed */..  HashEle
d9a60 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  m *pNew         
d9a70 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
d9a80 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
d9a90 0d 0a 29 7b 0d 0a 20 20 48 61 73 68 45 6c 65 6d  ..){..  HashElem
d9aa0 20 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 2f   *pHead;       /
d9ab0 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  * First element 
d9ac0 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72  already in pEntr
d9ad0 79 20 2a 2f 0d 0a 20 20 69 66 28 20 70 45 6e 74  y */..  if( pEnt
d9ae0 72 79 20 29 7b 0d 0a 20 20 20 20 70 48 65 61 64  ry ){..    pHead
d9af0 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74   = pEntry->count
d9b00 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e   ? pEntry->chain
d9b10 20 3a 20 30 3b 0d 0a 20 20 20 20 70 45 6e 74 72   : 0;..    pEntr
d9b20 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0d 0a 20 20 20  y->count++;..   
d9b30 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
d9b40 20 70 4e 65 77 3b 0d 0a 20 20 7d 65 6c 73 65 7b   pNew;..  }else{
d9b50 0d 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b  ..    pHead = 0;
d9b60 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 48 65  ..  }..  if( pHe
d9b70 61 64 20 29 7b 0d 0a 20 20 20 20 70 4e 65 77 2d  ad ){..    pNew-
d9b80 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0d 0a  >next = pHead;..
d9b90 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d      pNew->prev =
d9ba0 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0d 0a 20   pHead->prev;.. 
d9bb0 20 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72     if( pHead->pr
d9bc0 65 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65  ev ){ pHead->pre
d9bd0 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20  v->next = pNew; 
d9be0 7d 0d 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20  }..    else     
d9bf0 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69          { pH->fi
d9c00 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0d 0a 20  rst = pNew; }.. 
d9c10 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20 3d     pHead->prev =
d9c20 20 70 4e 65 77 3b 0d 0a 20 20 7d 65 6c 73 65 7b   pNew;..  }else{
d9c30 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74  ..    pNew->next
d9c40 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0d 0a 20   = pH->first;.. 
d9c50 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74     if( pH->first
d9c60 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70   ){ pH->first->p
d9c70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0d 0a 20  rev = pNew; }.. 
d9c80 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
d9c90 30 3b 0d 0a 20 20 20 20 70 48 2d 3e 66 69 72 73  0;..    pH->firs
d9ca0 74 20 3d 20 70 4e 65 77 3b 0d 0a 20 20 7d 0d 0a  t = pNew;..  }..
d9cb0 7d 0d 0a 0d 0a 0d 0a 2f 2a 20 52 65 73 69 7a 65  }....../* Resize
d9cc0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
d9cd0 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 61  so that it canta
d9ce0 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62  ins "new_size" b
d9cf0 75 63 6b 65 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  uckets...**..** 
d9d00 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d  The hash table m
d9d10 69 67 68 74 20 66 61 69 6c 20 74 6f 20 72 65 73  ight fail to res
d9d20 69 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d  ize if sqlite3_m
d9d30 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f 72  alloc() fails or
d9d40 0d 0a 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 20  ..** if the new 
d9d50 73 69 7a 65 20 69 73 20 74 68 65 20 73 61 6d 65  size is the same
d9d60 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 73 69   as the prior si
d9d70 7a 65 2e 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ze...** Return T
d9d80 52 55 45 20 69 66 20 74 68 65 20 72 65 73 69 7a  RUE if the resiz
d9d90 65 20 6f 63 63 75 72 73 20 61 6e 64 20 66 61 6c  e occurs and fal
d9da0 73 65 20 69 66 20 6e 6f 74 2e 0d 0a 2a 2f 0d 0a  se if not...*/..
d9db0 73 74 61 74 69 63 20 69 6e 74 20 72 65 68 61 73  static int rehas
d9dc0 68 28 48 61 73 68 20 2a 70 48 2c 20 75 6e 73 69  h(Hash *pH, unsi
d9dd0 67 6e 65 64 20 69 6e 74 20 6e 65 77 5f 73 69 7a  gned int new_siz
d9de0 65 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 5f 68  e){..  struct _h
d9df0 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20 20  t *new_ht;      
d9e00 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
d9e10 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0d 0a   hash table */..
d9e20 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
d9e30 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 20  , *next_elem;   
d9e40 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
d9e50 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c  over existing el
d9e60 65 6d 65 6e 74 73 20 2a 2f 0d 0a 0d 0a 23 69 66  ements */....#if
d9e70 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53   SQLITE_MALLOC_S
d9e80 4f 46 54 5f 4c 49 4d 49 54 3e 30 0d 0a 20 20 69  OFT_LIMIT>0..  i
d9e90 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65  f( new_size*size
d9ea0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53  of(struct _ht)>S
d9eb0 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
d9ec0 54 5f 4c 49 4d 49 54 20 29 7b 0d 0a 20 20 20 20  T_LIMIT ){..    
d9ed0 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54  new_size = SQLIT
d9ee0 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
d9ef0 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  MIT/sizeof(struc
d9f00 74 20 5f 68 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20  t _ht);..  }..  
d9f10 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48  if( new_size==pH
d9f20 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72  ->htsize ) retur
d9f30 6e 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  n 0;..#endif....
d9f40 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69    /* The inabili
d9f50 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20  ty to allocates 
d9f60 73 70 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67  space for a larg
d9f70 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  er hash table is
d9f80 0d 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d  ..  ** a perform
d9f90 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 74 20  ance hit but it 
d9fa0 69 73 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65  is not a fatal e
d9fb0 72 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74  rror.  So mark t
d9fc0 68 65 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  he..  ** allocat
d9fd0 69 6f 6e 20 61 73 20 61 20 62 65 6e 69 67 6e 2e  ion as a benign.
d9fe0 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
d9ff0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
da000 6f 63 28 29 3b 0d 0a 20 20 6e 65 77 5f 68 74 20  oc();..  new_ht 
da010 3d 20 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29  = (struct _ht *)
da020 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e  sqlite3Malloc( n
da030 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73  ew_size*sizeof(s
da040 74 72 75 63 74 20 5f 68 74 29 20 29 3b 0d 0a 20  truct _ht) );.. 
da050 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
da060 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 0d 0a 20 20  nMalloc();....  
da070 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20  if( new_ht==0 ) 
da080 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 73 71 6c  return 0;..  sql
da090 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 74  ite3_free(pH->ht
da0a0 29 3b 0d 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e  );..  pH->ht = n
da0b0 65 77 5f 68 74 3b 0d 0a 20 20 70 48 2d 3e 68 74  ew_ht;..  pH->ht
da0c0 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20  size = new_size 
da0d0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
da0e0 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65  ize(new_ht)/size
da0f0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0d  of(struct _ht);.
da100 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74  .  memset(new_ht
da110 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69  , 0, new_size*si
da120 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
da130 29 3b 0d 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70  );..  for(elem=p
da140 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69  H->first, pH->fi
da150 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65  rst=0; elem; ele
da160 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0d  m = next_elem){.
da170 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  .    unsigned in
da180 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c  t h = strHash(el
da190 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e  em->pKey, elem->
da1a0 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65  nKey) % new_size
da1b0 3b 0d 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d  ;..    next_elem
da1c0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0d 0a   = elem->next;..
da1d0 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e      insertElemen
da1e0 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d  t(pH, &new_ht[h]
da1f0 2c 20 65 6c 65 6d 29 3b 0d 0a 20 20 7d 0d 0a 20  , elem);..  }.. 
da200 20 72 65 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 0d   return 1;..}...
da210 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
da220 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  n (for internal 
da230 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65  use only) locate
da240 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20  s an element in 
da250 61 6e 0d 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c  an..** hash tabl
da260 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74  e that matches t
da270 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54  he given key.  T
da280 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73  he hash for this
da290 20 6b 65 79 20 68 61 73 0d 0a 2a 2a 20 61 6c 72   key has..** alr
da2a0 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74  eady been comput
da2b0 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64  ed and is passed
da2c0 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72 61   as the 4th para
da2d0 6d 65 74 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  meter...*/..stat
da2e0 69 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e  ic HashElem *fin
da2f0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  dElementGivenHas
da300 68 28 0d 0a 20 20 63 6f 6e 73 74 20 48 61 73 68  h(..  const Hash
da310 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65   *pH,     /* The
da320 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63 68   pH to be search
da330 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  ed */..  const c
da340 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20  har *pKey,   /* 
da350 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73  The key we are s
da360 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0d  earching for */.
da370 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
da380 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
da390 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e  in key (not coun
da3a0 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e  ting zero termin
da3b0 61 74 6f 72 29 20 2a 2f 0d 0a 20 20 75 6e 73 69  ator) */..  unsi
da3c0 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20  gned int h      
da3d0 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20  /* The hash for 
da3e0 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0d 0a 29 7b  this key. */..){
da3f0 0d 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  ..  HashElem *el
da400 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
da410 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
da420 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
da430 65 6e 74 20 6c 69 73 74 20 2a 2f 0d 0a 20 20 69  ent list */..  i
da440 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
da450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
da460 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
da470 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74  nts left to test
da480 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 70 48 2d   */....  if( pH-
da490 3e 68 74 20 29 7b 0d 0a 20 20 20 20 73 74 72 75  >ht ){..    stru
da4a0 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d  ct _ht *pEntry =
da4b0 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0d 0a 20 20   &pH->ht[h];..  
da4c0 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d    elem = pEntry-
da4d0 3e 63 68 61 69 6e 3b 0d 0a 20 20 20 20 63 6f 75  >chain;..    cou
da4e0 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75  nt = pEntry->cou
da4f0 6e 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  nt;..  }else{.. 
da500 20 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69     elem = pH->fi
da510 72 73 74 3b 0d 0a 20 20 20 20 63 6f 75 6e 74 20  rst;..    count 
da520 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0d 0a 20 20  = pH->count;..  
da530 7d 0d 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 6e  }..  while( coun
da540 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 65 6c  t-- && ALWAYS(el
da550 65 6d 29 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  em) ){..    if( 
da560 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79  elem->nKey==nKey
da570 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
da580 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70  Cmp(elem->pKey,p
da590 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20  Key,nKey)==0 ){ 
da5a0 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65  ..      return e
da5b0 6c 65 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  lem;..    }..   
da5c0 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65   elem = elem->ne
da5d0 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  xt;..  }..  retu
da5e0 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 52  rn 0;..}..../* R
da5f0 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65  emove a single e
da600 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61  ntry from the ha
da610 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61  sh table given a
da620 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
da630 0d 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64  ..** element and
da640 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65   a hash on the e
da650 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0d 0a 2a  lement's key...*
da660 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  /..static void r
da670 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65  emoveElementGive
da680 6e 48 61 73 68 28 0d 0a 20 20 48 61 73 68 20 2a  nHash(..  Hash *
da690 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  pH,         /* T
da6a0 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67  he pH containing
da6b0 20 22 65 6c 65 6d 22 20 2a 2f 0d 0a 20 20 48 61   "elem" */..  Ha
da6c0 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20  shElem* elem,   
da6d0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
da6e0 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f  o be removed fro
da6f0 6d 20 74 68 65 20 70 48 20 2a 2f 0d 0a 20 20 75  m the pH */..  u
da700 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20  nsigned int h   
da710 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66   /* Hash value f
da720 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a  or the element *
da730 2f 0d 0a 29 7b 0d 0a 20 20 73 74 72 75 63 74 20  /..){..  struct 
da740 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0d 0a 20 20  _ht *pEntry;..  
da750 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
da760 7b 0d 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65  {..    elem->pre
da770 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e  v->next = elem->
da780 6e 65 78 74 3b 20 0d 0a 20 20 7d 65 6c 73 65 7b  next; ..  }else{
da790 0d 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20  ..    pH->first 
da7a0 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0d 0a 20  = elem->next;.. 
da7b0 20 7d 0d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e   }..  if( elem->
da7c0 6e 65 78 74 20 29 7b 0d 0a 20 20 20 20 65 6c 65  next ){..    ele
da7d0 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20  m->next->prev = 
da7e0 65 6c 65 6d 2d 3e 70 72 65 76 3b 0d 0a 20 20 7d  elem->prev;..  }
da7f0 0d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29  ..  if( pH->ht )
da800 7b 0d 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20  {..    pEntry = 
da810 26 70 48 2d 3e 68 74 5b 68 5d 3b 0d 0a 20 20 20  &pH->ht[h];..   
da820 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61   if( pEntry->cha
da830 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0d 0a 20 20 20  in==elem ){..   
da840 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e     pEntry->chain
da850 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0d 0a   = elem->next;..
da860 20 20 20 20 7d 0d 0a 20 20 20 20 70 45 6e 74 72      }..    pEntr
da870 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0d 0a 20 20 20  y->count--;..   
da880 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 79 2d   assert( pEntry-
da890 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0d 0a 20 20  >count>=0 );..  
da8a0 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
da8b0 65 28 20 65 6c 65 6d 20 29 3b 0d 0a 20 20 70 48  e( elem );..  pH
da8c0 2d 3e 63 6f 75 6e 74 2d 2d 3b 0d 0a 20 20 69 66  ->count--;..  if
da8d0 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29  ( pH->count<=0 )
da8e0 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
da8f0 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0d 0a  H->first==0 );..
da900 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
da910 63 6f 75 6e 74 3d 3d 30 20 29 3b 0d 0a 20 20 20  count==0 );..   
da920 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
da930 72 28 70 48 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  r(pH);..  }..}..
da940 0d 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ../* Attempt to 
da950 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
da960 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
da970 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
da980 79 0d 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68  y..** that match
da990 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52  es pKey,nKey.  R
da9a0 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 66  eturn the data f
da9b0 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  or this element 
da9c0 69 66 20 69 74 20 69 73 0d 0a 2a 2a 20 66 6f 75  if it is..** fou
da9d0 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  nd, or NULL if t
da9e0 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
da9f0 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
daa00 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
daa10 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73  te3HashFind(cons
daa20 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73  t Hash *pH, cons
daa30 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e  t char *pKey, in
daa40 74 20 6e 4b 65 79 29 7b 0d 0a 20 20 48 61 73 68  t nKey){..  Hash
daa50 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f  Elem *elem;    /
daa60 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68  * The element th
daa70 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a  at matches key *
daa80 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  /..  unsigned in
daa90 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 73  t h;    /* A has
daaa0 68 20 6f 6e 20 6b 65 79 20 2a 2f 0d 0a 0d 0a 20  h on key */.... 
daab0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
daac0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  ;..  assert( pKe
daad0 79 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  y!=0 );..  asser
daae0 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0d 0a 20  t( nKey>=0 );.. 
daaf0 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0d 0a   if( pH->ht ){..
dab00 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28      h = strHash(
dab10 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48  pKey, nKey) % pH
dab20 2d 3e 68 74 73 69 7a 65 3b 0d 0a 20 20 7d 65 6c  ->htsize;..  }el
dab30 73 65 7b 0d 0a 20 20 20 20 68 20 3d 20 30 3b 0d  se{..    h = 0;.
dab40 0a 20 20 7d 0d 0a 20 20 65 6c 65 6d 20 3d 20 66  .  }..  elem = f
dab50 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48  indElementGivenH
dab60 61 73 68 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b  ash(pH, pKey, nK
dab70 65 79 2c 20 68 29 3b 0d 0a 20 20 72 65 74 75 72  ey, h);..  retur
dab80 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64  n elem ? elem->d
dab90 61 74 61 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  ata : 0;..}..../
daba0 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d  * Insert an elem
dabb0 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
dabc0 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65  h table pH.  The
dabd0 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65   key is pKey,nKe
dabe0 79 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  y..** and the da
dabf0 74 61 20 69 73 20 22 64 61 74 61 22 2e 0d 0a 2a  ta is "data"...*
dac00 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  *..** If no elem
dac10 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
dac20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
dac30 74 68 65 6e 20 61 20 6e 65 77 0d 0a 2a 2a 20 65  then a new..** e
dac40 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65  lement is create
dac50 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65  d and NULL is re
dac60 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  turned...**..** 
dac70 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65  If another eleme
dac80 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  nt already exist
dac90 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
daca0 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a  key, then the..*
dacb0 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
dacc0 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
dacd0 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
dace0 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  a is returned...
dacf0 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f  ** The key is no
dad00 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73  t copied in this
dad10 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61   instance.  If a
dad20 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74   malloc fails, t
dad30 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 6e 65 77 20  hen..** the new 
dad40 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
dad50 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61   and the hash ta
dad60 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
dad70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
dad80 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 74 65   "data" paramete
dad90 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
dada0 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  on is NULL, then
dadb0 20 74 68 65 0d 0a 2a 2a 20 65 6c 65 6d 65 6e 74   the..** element
dadc0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
dadd0 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76  o "key" is remov
dade0 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ed from the hash
dadf0 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c   table...*/..SQL
dae00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
dae10 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73   *sqlite3HashIns
dae20 65 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63 6f  ert(Hash *pH, co
dae30 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20  nst char *pKey, 
dae40 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
dae50 64 61 74 61 29 7b 0d 0a 20 20 75 6e 73 69 67 6e  data){..  unsign
dae60 65 64 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  ed int h;       
dae70 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74  /* the hash of t
dae80 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61  he key modulo ha
dae90 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f  sh table size */
daea0 0d 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  ..  HashElem *el
daeb0 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65  em;       /* Use
daec0 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74  d to loop thru t
daed0 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20  he element list 
daee0 2a 2f 0d 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  */..  HashElem *
daef0 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e  new_elem;   /* N
daf00 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64  ew element added
daf10 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0d 0a 0d   to the pH */...
daf20 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30  .  assert( pH!=0
daf30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
daf40 4b 65 79 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  Key!=0 );..  ass
daf50 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0d  ert( nKey>=0 );.
daf60 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a  .  if( pH->htsiz
daf70 65 20 29 7b 0d 0a 20 20 20 20 68 20 3d 20 73 74  e ){..    h = st
daf80 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79  rHash(pKey, nKey
daf90 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0d  ) % pH->htsize;.
dafa0 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 68  .  }else{..    h
dafb0 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 65 6c   = 0;..  }..  el
dafc0 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74  em = findElement
dafd0 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65  GivenHash(pH,pKe
dafe0 79 2c 6e 4b 65 79 2c 68 29 3b 0d 0a 20 20 69 66  y,nKey,h);..  if
daff0 28 20 65 6c 65 6d 20 29 7b 0d 0a 20 20 20 20 76  ( elem ){..    v
db000 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20  oid *old_data = 
db010 65 6c 65 6d 2d 3e 64 61 74 61 3b 0d 0a 20 20 20  elem->data;..   
db020 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b 0d   if( data==0 ){.
db030 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45 6c 65  .      removeEle
db040 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
db050 2c 65 6c 65 6d 2c 68 29 3b 0d 0a 20 20 20 20 7d  ,elem,h);..    }
db060 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 65 6c 65  else{..      ele
db070 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0d  m->data = data;.
db080 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65  .      elem->pKe
db090 79 20 3d 20 70 4b 65 79 3b 0d 0a 20 20 20 20 20  y = pKey;..     
db0a0 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c   assert(nKey==el
db0b0 65 6d 2d 3e 6e 4b 65 79 29 3b 0d 0a 20 20 20 20  em->nKey);..    
db0c0 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c  }..    return ol
db0d0 64 5f 64 61 74 61 3b 0d 0a 20 20 7d 0d 0a 20 20  d_data;..  }..  
db0e0 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65  if( data==0 ) re
db0f0 74 75 72 6e 20 30 3b 0d 0a 20 20 6e 65 77 5f 65  turn 0;..  new_e
db100 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a  lem = (HashElem*
db110 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20  )sqlite3Malloc( 
db120 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29  sizeof(HashElem)
db130 20 29 3b 0d 0a 20 20 69 66 28 20 6e 65 77 5f 65   );..  if( new_e
db140 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  lem==0 ) return 
db150 64 61 74 61 3b 0d 0a 20 20 6e 65 77 5f 65 6c 65  data;..  new_ele
db160 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0d  m->pKey = pKey;.
db170 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65  .  new_elem->nKe
db180 79 20 3d 20 6e 4b 65 79 3b 0d 0a 20 20 6e 65 77  y = nKey;..  new
db190 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61  _elem->data = da
db1a0 74 61 3b 0d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74  ta;..  pH->count
db1b0 2b 2b 3b 0d 0a 20 20 69 66 28 20 70 48 2d 3e 63  ++;..  if( pH->c
db1c0 6f 75 6e 74 3e 3d 31 30 20 26 26 20 70 48 2d 3e  ount>=10 && pH->
db1d0 63 6f 75 6e 74 20 3e 20 32 2a 70 48 2d 3e 68 74  count > 2*pH->ht
db1e0 73 69 7a 65 20 29 7b 0d 0a 20 20 20 20 69 66 28  size ){..    if(
db1f0 20 72 65 68 61 73 68 28 70 48 2c 20 70 48 2d 3e   rehash(pH, pH->
db200 63 6f 75 6e 74 2a 32 29 20 29 7b 0d 0a 20 20 20  count*2) ){..   
db210 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68     assert( pH->h
db220 74 73 69 7a 65 3e 30 20 29 3b 0d 0a 20 20 20 20  tsize>0 );..    
db230 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b    h = strHash(pK
db240 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e  ey, nKey) % pH->
db250 68 74 73 69 7a 65 3b 0d 0a 20 20 20 20 7d 0d 0a  htsize;..    }..
db260 20 20 7d 0d 0a 20 20 69 66 28 20 70 48 2d 3e 68    }..  if( pH->h
db270 74 20 29 7b 0d 0a 20 20 20 20 69 6e 73 65 72 74  t ){..    insert
db280 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d  Element(pH, &pH-
db290 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d  >ht[h], new_elem
db2a0 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  );..  }else{..  
db2b0 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28    insertElement(
db2c0 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29  pH, 0, new_elem)
db2d0 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
db2e0 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a   0;..}..../*****
db2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
db300 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   hash.c ********
db310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db330 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
db340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
db350 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 63 20   file opcodes.c 
db360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 20 41  *********/../* A
db390 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
db3a0 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20  erated.  Do not 
db3b0 65 64 69 74 20 2a 2f 0d 0a 2f 2a 20 53 65 65 20  edit */../* See 
db3c0 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 77  the mkopcodec.aw
db3d0 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
db3e0 61 69 6c 73 2e 20 2a 2f 0d 0a 23 69 66 20 21 64  ails. */..#if !d
db3f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
db400 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21  IT_EXPLAIN) || !
db410 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
db420 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f  || defined(VDBE_
db430 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69  PROFILE) || defi
db440 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
db450 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  )..SQLITE_PRIVAT
db460 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
db470 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28  lite3OpcodeName(
db480 69 6e 74 20 69 29 7b 0d 0a 20 73 74 61 74 69 63  int i){.. static
db490 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
db4a0 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20  st azName[] = { 
db4b0 22 3f 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 20  "?",..     /*   
db4c0 31 20 2a 2f 20 22 47 6f 74 6f 22 2c 0d 0a 20 20  1 */ "Goto",..  
db4d0 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f     /*   2 */ "Go
db4e0 73 75 62 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  sub",..     /*  
db4f0 20 33 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0d   3 */ "Return",.
db500 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20  .     /*   4 */ 
db510 22 59 69 65 6c 64 22 2c 0d 0a 20 20 20 20 20 2f  "Yield",..     /
db520 2a 20 20 20 35 20 2a 2f 20 22 48 61 6c 74 49 66  *   5 */ "HaltIf
db530 4e 75 6c 6c 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  Null",..     /* 
db540 20 20 36 20 2a 2f 20 22 48 61 6c 74 22 2c 0d 0a    6 */ "Halt",..
db550 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22       /*   7 */ "
db560 49 6e 74 65 67 65 72 22 2c 0d 0a 20 20 20 20 20  Integer",..     
db570 2f 2a 20 20 20 38 20 2a 2f 20 22 49 6e 74 36 34  /*   8 */ "Int64
db580 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 20 39 20  ",..     /*   9 
db590 2a 2f 20 22 53 74 72 69 6e 67 22 2c 0d 0a 20 20  */ "String",..  
db5a0 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4e 75     /*  10 */ "Nu
db5b0 6c 6c 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 31  ll",..     /*  1
db5c0 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0d 0a 20 20  1 */ "Blob",..  
db5d0 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 56 61     /*  12 */ "Va
db5e0 72 69 61 62 6c 65 22 2c 0d 0a 20 20 20 20 20 2f  riable",..     /
db5f0 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 65 22 2c  *  13 */ "Move",
db600 0d 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a 2f  ..     /*  14 */
db610 20 22 43 6f 70 79 22 2c 0d 0a 20 20 20 20 20 2f   "Copy",..     /
db620 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 22  *  15 */ "SCopy"
db630 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a  ,..     /*  16 *
db640 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0d 0a  / "ResultRow",..
db650 20 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22       /*  17 */ "
db660 43 6f 6c 6c 53 65 71 22 2c 0d 0a 20 20 20 20 20  CollSeq",..     
db670 2f 2a 20 20 31 38 20 2a 2f 20 22 46 75 6e 63 74  /*  18 */ "Funct
db680 69 6f 6e 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  ion",..     /*  
db690 31 39 20 2a 2f 20 22 4e 6f 74 22 2c 0d 0a 20 20  19 */ "Not",..  
db6a0 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22 41 64     /*  20 */ "Ad
db6b0 64 49 6d 6d 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  dImm",..     /* 
db6c0 20 32 31 20 2a 2f 20 22 4d 75 73 74 42 65 49 6e   21 */ "MustBeIn
db6d0 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 32 32  t",..     /*  22
db6e0 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 74   */ "RealAffinit
db6f0 79 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 32 33  y",..     /*  23
db700 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e   */ "Permutation
db710 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 32 34 20  ",..     /*  24 
db720 2a 2f 20 22 43 6f 6d 70 61 72 65 22 2c 0d 0a 20  */ "Compare",.. 
db730 20 20 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 4a      /*  25 */ "J
db740 75 6d 70 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  ump",..     /*  
db750 32 36 20 2a 2f 20 22 4f 6e 63 65 22 2c 0d 0a 20  26 */ "Once",.. 
db760 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 49      /*  27 */ "I
db770 66 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 32 38  f",..     /*  28
db780 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0d 0a 20 20   */ "IfNot",..  
db790 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22 43 6f     /*  29 */ "Co
db7a0 6c 75 6d 6e 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  lumn",..     /* 
db7b0 20 33 30 20 2a 2f 20 22 41 66 66 69 6e 69 74 79   30 */ "Affinity
db7c0 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 33 31 20  ",..     /*  31 
db7d0 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c  */ "MakeRecord",
db7e0 0d 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f  ..     /*  32 */
db7f0 20 22 43 6f 75 6e 74 22 2c 0d 0a 20 20 20 20 20   "Count",..     
db800 2f 2a 20 20 33 33 20 2a 2f 20 22 53 61 76 65 70  /*  33 */ "Savep
db810 6f 69 6e 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  oint",..     /* 
db820 20 33 34 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d   34 */ "AutoComm
db830 69 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 33  it",..     /*  3
db840 35 20 2a 2f 20 22 54 72 61 6e 73 61 63 74 69 6f  5 */ "Transactio
db850 6e 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 33 36  n",..     /*  36
db860 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65 22   */ "ReadCookie"
db870 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 33 37 20 2a  ,..     /*  37 *
db880 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0d 0a  / "SetCookie",..
db890 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f 20 22       /*  38 */ "
db8a0 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0d 0a  VerifyCookie",..
db8b0 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22       /*  39 */ "
db8c0 4f 70 65 6e 52 65 61 64 22 2c 0d 0a 20 20 20 20  OpenRead",..    
db8d0 20 2f 2a 20 20 34 30 20 2a 2f 20 22 4f 70 65 6e   /*  40 */ "Open
db8e0 57 72 69 74 65 22 2c 0d 0a 20 20 20 20 20 2f 2a  Write",..     /*
db8f0 20 20 34 31 20 2a 2f 20 22 4f 70 65 6e 41 75 74    41 */ "OpenAut
db900 6f 69 6e 64 65 78 22 2c 0d 0a 20 20 20 20 20 2f  oindex",..     /
db910 2a 20 20 34 32 20 2a 2f 20 22 4f 70 65 6e 45 70  *  42 */ "OpenEp
db920 68 65 6d 65 72 61 6c 22 2c 0d 0a 20 20 20 20 20  hemeral",..     
db930 2f 2a 20 20 34 33 20 2a 2f 20 22 53 6f 72 74 65  /*  43 */ "Sorte
db940 72 4f 70 65 6e 22 2c 0d 0a 20 20 20 20 20 2f 2a  rOpen",..     /*
db950 20 20 34 34 20 2a 2f 20 22 4f 70 65 6e 50 73 65    44 */ "OpenPse
db960 75 64 6f 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  udo",..     /*  
db970 34 35 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0d 0a  45 */ "Close",..
db980 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22       /*  46 */ "
db990 53 65 65 6b 4c 74 22 2c 0d 0a 20 20 20 20 20 2f  SeekLt",..     /
db9a0 2a 20 20 34 37 20 2a 2f 20 22 53 65 65 6b 4c 65  *  47 */ "SeekLe
db9b0 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 34 38 20  ",..     /*  48 
db9c0 2a 2f 20 22 53 65 65 6b 47 65 22 2c 0d 0a 20 20  */ "SeekGe",..  
db9d0 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 53 65     /*  49 */ "Se
db9e0 65 6b 47 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  ekGt",..     /* 
db9f0 20 35 30 20 2a 2f 20 22 53 65 65 6b 22 2c 0d 0a   50 */ "Seek",..
dba00 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22       /*  51 */ "
dba10 4e 6f 74 46 6f 75 6e 64 22 2c 0d 0a 20 20 20 20  NotFound",..    
dba20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 46 6f 75 6e   /*  52 */ "Foun
dba30 64 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 35 33  d",..     /*  53
dba40 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 0d   */ "IsUnique",.
dba50 0a 20 20 20 20 20 2f 2a 20 20 35 34 20 2a 2f 20  .     /*  54 */ 
dba60 22 4e 6f 74 45 78 69 73 74 73 22 2c 0d 0a 20 20  "NotExists",..  
dba70 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65     /*  55 */ "Se
dba80 71 75 65 6e 63 65 22 2c 0d 0a 20 20 20 20 20 2f  quence",..     /
dba90 2a 20 20 35 36 20 2a 2f 20 22 4e 65 77 52 6f 77  *  56 */ "NewRow
dbaa0 69 64 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 35  id",..     /*  5
dbab0 37 20 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0d 0a  7 */ "Insert",..
dbac0 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22       /*  58 */ "
dbad0 49 6e 73 65 72 74 49 6e 74 22 2c 0d 0a 20 20 20  InsertInt",..   
dbae0 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 44 65 6c    /*  59 */ "Del
dbaf0 65 74 65 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  ete",..     /*  
dbb00 36 30 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e  60 */ "ResetCoun
dbb10 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 36 31  t",..     /*  61
dbb20 20 2a 2f 20 22 53 6f 72 74 65 72 43 6f 6d 70 61   */ "SorterCompa
dbb30 72 65 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 36  re",..     /*  6
dbb40 32 20 2a 2f 20 22 53 6f 72 74 65 72 44 61 74 61  2 */ "SorterData
dbb50 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 36 33 20  ",..     /*  63 
dbb60 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0d 0a 20 20  */ "RowKey",..  
dbb70 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 52 6f     /*  64 */ "Ro
dbb80 77 44 61 74 61 22 2c 0d 0a 20 20 20 20 20 2f 2a  wData",..     /*
dbb90 20 20 36 35 20 2a 2f 20 22 52 6f 77 69 64 22 2c    65 */ "Rowid",
dbba0 0d 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f  ..     /*  66 */
dbbb0 20 22 4e 75 6c 6c 52 6f 77 22 2c 0d 0a 20 20 20   "NullRow",..   
dbbc0 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 4c 61 73    /*  67 */ "Las
dbbd0 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 36 38  t",..     /*  68
dbbe0 20 2a 2f 20 22 4f 72 22 2c 0d 0a 20 20 20 20 20   */ "Or",..     
dbbf0 2f 2a 20 20 36 39 20 2a 2f 20 22 41 6e 64 22 2c  /*  69 */ "And",
dbc00 0d 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f  ..     /*  70 */
dbc10 20 22 53 6f 72 74 65 72 53 6f 72 74 22 2c 0d 0a   "SorterSort",..
dbc20 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22       /*  71 */ "
dbc30 53 6f 72 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  Sort",..     /* 
dbc40 20 37 32 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c   72 */ "Rewind",
dbc50 0d 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f  ..     /*  73 */
dbc60 20 22 49 73 4e 75 6c 6c 22 2c 0d 0a 20 20 20 20   "IsNull",..    
dbc70 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 4e   /*  74 */ "NotN
dbc80 75 6c 6c 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  ull",..     /*  
dbc90 37 35 20 2a 2f 20 22 4e 65 22 2c 0d 0a 20 20 20  75 */ "Ne",..   
dbca0 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22    /*  76 */ "Eq"
dbcb0 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a  ,..     /*  77 *
dbcc0 2f 20 22 47 74 22 2c 0d 0a 20 20 20 20 20 2f 2a  / "Gt",..     /*
dbcd0 20 20 37 38 20 2a 2f 20 22 4c 65 22 2c 0d 0a 20    78 */ "Le",.. 
dbce0 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c      /*  79 */ "L
dbcf0 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 38 30  t",..     /*  80
dbd00 20 2a 2f 20 22 47 65 22 2c 0d 0a 20 20 20 20 20   */ "Ge",..     
dbd10 2f 2a 20 20 38 31 20 2a 2f 20 22 53 6f 72 74 65  /*  81 */ "Sorte
dbd20 72 4e 65 78 74 22 2c 0d 0a 20 20 20 20 20 2f 2a  rNext",..     /*
dbd30 20 20 38 32 20 2a 2f 20 22 42 69 74 41 6e 64 22    82 */ "BitAnd"
dbd40 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ,..     /*  83 *
dbd50 2f 20 22 42 69 74 4f 72 22 2c 0d 0a 20 20 20 20  / "BitOr",..    
dbd60 20 2f 2a 20 20 38 34 20 2a 2f 20 22 53 68 69 66   /*  84 */ "Shif
dbd70 74 4c 65 66 74 22 2c 0d 0a 20 20 20 20 20 2f 2a  tLeft",..     /*
dbd80 20 20 38 35 20 2a 2f 20 22 53 68 69 66 74 52 69    85 */ "ShiftRi
dbd90 67 68 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  ght",..     /*  
dbda0 38 36 20 2a 2f 20 22 41 64 64 22 2c 0d 0a 20 20  86 */ "Add",..  
dbdb0 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 53 75     /*  87 */ "Su
dbdc0 62 74 72 61 63 74 22 2c 0d 0a 20 20 20 20 20 2f  btract",..     /
dbdd0 2a 20 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 70  *  88 */ "Multip
dbde0 6c 79 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 38  ly",..     /*  8
dbdf0 39 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0d 0a  9 */ "Divide",..
dbe00 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22       /*  90 */ "
dbe10 52 65 6d 61 69 6e 64 65 72 22 2c 0d 0a 20 20 20  Remainder",..   
dbe20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 43 6f 6e    /*  91 */ "Con
dbe30 63 61 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20  cat",..     /*  
dbe40 39 32 20 2a 2f 20 22 50 72 65 76 22 2c 0d 0a 20  92 */ "Prev",.. 
dbe50 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42      /*  93 */ "B
dbe60 69 74 4e 6f 74 22 2c 0d 0a 20 20 20 20 20 2f 2a  itNot",..     /*
dbe70 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67 38    94 */ "String8
dbe80 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 39 35 20  ",..     /*  95 
dbe90 2a 2f 20 22 4e 65 78 74 22 2c 0d 0a 20 20 20 20  */ "Next",..    
dbea0 20 2f 2a 20 20 39 36 20 2a 2f 20 22 53 6f 72 74   /*  96 */ "Sort
dbeb0 65 72 49 6e 73 65 72 74 22 2c 0d 0a 20 20 20 20  erInsert",..    
dbec0 20 2f 2a 20 20 39 37 20 2a 2f 20 22 49 64 78 49   /*  97 */ "IdxI
dbed0 6e 73 65 72 74 22 2c 0d 0a 20 20 20 20 20 2f 2a  nsert",..     /*
dbee0 20 20 39 38 20 2a 2f 20 22 49 64 78 44 65 6c 65    98 */ "IdxDele
dbef0 74 65 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 20 39  te",..     /*  9
dbf00 39 20 2a 2f 20 22 49 64 78 52 6f 77 69 64 22 2c  9 */ "IdxRowid",
dbf10 0d 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f  ..     /* 100 */
dbf20 20 22 49 64 78 4c 54 22 2c 0d 0a 20 20 20 20 20   "IdxLT",..     
dbf30 2f 2a 20 31 30 31 20 2a 2f 20 22 49 64 78 47 45  /* 101 */ "IdxGE
dbf40 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 30 32 20  ",..     /* 102 
dbf50 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0d 0a 20  */ "Destroy",.. 
dbf60 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22 43      /* 103 */ "C
dbf70 6c 65 61 72 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  lear",..     /* 
dbf80 31 30 34 20 2a 2f 20 22 43 72 65 61 74 65 49 6e  104 */ "CreateIn
dbf90 64 65 78 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  dex",..     /* 1
dbfa0 30 35 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62  05 */ "CreateTab
dbfb0 6c 65 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 30  le",..     /* 10
dbfc0 36 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d  6 */ "ParseSchem
dbfd0 61 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 30 37  a",..     /* 107
dbfe0 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 79 73 69   */ "LoadAnalysi
dbff0 73 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 30 38  s",..     /* 108
dc000 20 2a 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c   */ "DropTable",
dc010 0d 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f  ..     /* 109 */
dc020 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0d 0a 20   "DropIndex",.. 
dc030 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22 44      /* 110 */ "D
dc040 72 6f 70 54 72 69 67 67 65 72 22 2c 0d 0a 20 20  ropTrigger",..  
dc050 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 49 6e     /* 111 */ "In
dc060 74 65 67 72 69 74 79 43 6b 22 2c 0d 0a 20 20 20  tegrityCk",..   
dc070 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 6f 77    /* 112 */ "Row
dc080 53 65 74 41 64 64 22 2c 0d 0a 20 20 20 20 20 2f  SetAdd",..     /
dc090 2a 20 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 74  * 113 */ "RowSet
dc0a0 52 65 61 64 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  Read",..     /* 
dc0b0 31 31 34 20 2a 2f 20 22 52 6f 77 53 65 74 54 65  114 */ "RowSetTe
dc0c0 73 74 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 31  st",..     /* 11
dc0d0 35 20 2a 2f 20 22 50 72 6f 67 72 61 6d 22 2c 0d  5 */ "Program",.
dc0e0 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20  .     /* 116 */ 
dc0f0 22 50 61 72 61 6d 22 2c 0d 0a 20 20 20 20 20 2f  "Param",..     /
dc100 2a 20 31 31 37 20 2a 2f 20 22 46 6b 43 6f 75 6e  * 117 */ "FkCoun
dc110 74 65 72 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  ter",..     /* 1
dc120 31 38 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22  18 */ "FkIfZero"
dc130 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 2a  ,..     /* 119 *
dc140 2f 20 22 4d 65 6d 4d 61 78 22 2c 0d 0a 20 20 20  / "MemMax",..   
dc150 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 49 66 50    /* 120 */ "IfP
dc160 6f 73 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 32  os",..     /* 12
dc170 31 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0d 0a 20  1 */ "IfNeg",.. 
dc180 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 49      /* 122 */ "I
dc190 66 5a 65 72 6f 22 2c 0d 0a 20 20 20 20 20 2f 2a  fZero",..     /*
dc1a0 20 31 32 33 20 2a 2f 20 22 41 67 67 53 74 65 70   123 */ "AggStep
dc1b0 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 32 34 20  ",..     /* 124 
dc1c0 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0d 0a  */ "AggFinal",..
dc1d0 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20 22       /* 125 */ "
dc1e0 43 68 65 63 6b 70 6f 69 6e 74 22 2c 0d 0a 20 20  Checkpoint",..  
dc1f0 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 4a 6f     /* 126 */ "Jo
dc200 75 72 6e 61 6c 4d 6f 64 65 22 2c 0d 0a 20 20 20  urnalMode",..   
dc210 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 61 63    /* 127 */ "Vac
dc220 75 75 6d 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  uum",..     /* 1
dc230 32 38 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75  28 */ "IncrVacuu
dc240 6d 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 32 39  m",..     /* 129
dc250 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0d 0a 20   */ "Expire",.. 
dc260 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 22 52      /* 130 */ "R
dc270 65 61 6c 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  eal",..     /* 1
dc280 33 31 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b  31 */ "TableLock
dc290 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 33 32 20  ",..     /* 132 
dc2a0 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0d 0a 20 20  */ "VBegin",..  
dc2b0 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 56 43     /* 133 */ "VC
dc2c0 72 65 61 74 65 22 2c 0d 0a 20 20 20 20 20 2f 2a  reate",..     /*
dc2d0 20 31 33 34 20 2a 2f 20 22 56 44 65 73 74 72 6f   134 */ "VDestro
dc2e0 79 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 33 35  y",..     /* 135
dc2f0 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0d 0a 20 20   */ "VOpen",..  
dc300 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 56 46     /* 136 */ "VF
dc310 69 6c 74 65 72 22 2c 0d 0a 20 20 20 20 20 2f 2a  ilter",..     /*
dc320 20 31 33 37 20 2a 2f 20 22 56 43 6f 6c 75 6d 6e   137 */ "VColumn
dc330 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 33 38 20  ",..     /* 138 
dc340 2a 2f 20 22 56 4e 65 78 74 22 2c 0d 0a 20 20 20  */ "VNext",..   
dc350 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 56 52 65    /* 139 */ "VRe
dc360 6e 61 6d 65 22 2c 0d 0a 20 20 20 20 20 2f 2a 20  name",..     /* 
dc370 31 34 30 20 2a 2f 20 22 56 55 70 64 61 74 65 22  140 */ "VUpdate"
dc380 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a  ,..     /* 141 *
dc390 2f 20 22 54 6f 54 65 78 74 22 2c 0d 0a 20 20 20  / "ToText",..   
dc3a0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42    /* 142 */ "ToB
dc3b0 6c 6f 62 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  lob",..     /* 1
dc3c0 34 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63  43 */ "ToNumeric
dc3d0 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 34 34 20  ",..     /* 144 
dc3e0 2a 2f 20 22 54 6f 49 6e 74 22 2c 0d 0a 20 20 20  */ "ToInt",..   
dc3f0 20 20 2f 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52    /* 145 */ "ToR
dc400 65 61 6c 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31  eal",..     /* 1
dc410 34 36 20 2a 2f 20 22 50 61 67 65 63 6f 75 6e 74  46 */ "Pagecount
dc420 22 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 34 37 20  ",..     /* 147 
dc430 2a 2f 20 22 4d 61 78 50 67 63 6e 74 22 2c 0d 0a  */ "MaxPgcnt",..
dc440 20 20 20 20 20 2f 2a 20 31 34 38 20 2a 2f 20 22       /* 148 */ "
dc450 54 72 61 63 65 22 2c 0d 0a 20 20 20 20 20 2f 2a  Trace",..     /*
dc460 20 31 34 39 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0d   149 */ "Noop",.
dc470 0a 20 20 20 20 20 2f 2a 20 31 35 30 20 2a 2f 20  .     /* 150 */ 
dc480 22 45 78 70 6c 61 69 6e 22 2c 0d 0a 20 20 7d 3b  "Explain",..  };
dc490 0d 0a 20 20 72 65 74 75 72 6e 20 61 7a 4e 61 6d  ..  return azNam
dc4a0 65 5b 69 5d 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  e[i];..}..#endif
dc4b0 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
dc4c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64  *** End of opcod
dc4d0 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.c ***********
dc4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc500 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
dc510 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
dc520 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
dc530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc550 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
dc560 36 20 46 65 62 20 31 34 0d 0a 2a 2a 0d 0a 2a 2a  6 Feb 14..**..**
dc570 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
dc580 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
dc590 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
dc5a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
dc5b0 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
dc5c0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
dc5d0 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
dc5e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
dc5f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
dc600 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
dc610 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
dc620 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
dc630 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
dc640 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
dc650 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
dc660 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
dc670 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
dc680 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
dc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc6d0 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
dc6e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
dc6f0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
dc700 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e 0d  ecific to OS/2..
dc710 0a 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 20 53 51 4c  .*/......#if SQL
dc720 49 54 45 5f 4f 53 5f 4f 53 32 0d 0a 0d 0a 2f 2a  ITE_OS_OS2..../*
dc730 0d 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75  ..** A Note Abou
dc740 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  t Memory Allocat
dc750 69 6f 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ion:..**..** Thi
dc760 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61  s driver uses ma
dc770 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69  lloc()/free() di
dc780 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68  rectly rather th
dc790 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68  an going through
dc7a0 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d  ..** the SQLite-
dc7b0 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33  wrappers sqlite3
dc7c0 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65  _malloc()/sqlite
dc7d0 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65  3_free().  Those
dc7e0 20 77 72 61 70 70 65 72 73 0d 0a 2a 2a 20 61 72   wrappers..** ar
dc7f0 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  e designed for u
dc800 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  se on embedded s
dc810 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d  ystems where mem
dc820 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e  ory is scarce an
dc830 64 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  d..** malloc fai
dc840 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65  lures happen fre
dc850 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64  quently.  OS/2 d
dc860 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c  oes not typicall
dc870 79 20 72 75 6e 20 6f 6e 0d 0a 2a 2a 20 65 6d 62  y run on..** emb
dc880 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61  edded systems, a
dc890 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20  nd when it does 
dc8a0 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e  the developers n
dc8b0 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67  ormally have big
dc8c0 67 65 72 0d 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73  ger..** problems
dc8d0 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20   to worry about 
dc8e0 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74  than running out
dc8f0 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20   of memory.  So 
dc900 74 68 65 72 65 20 69 73 20 6e 6f 74 0d 0a 2a 2a  there is not..**
dc910 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65   a compelling ne
dc920 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 72  ed to use the wr
dc930 61 70 70 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  appers...**..** 
dc940 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67  But there is a g
dc950 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f  ood reason to no
dc960 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  t use the wrappe
dc970 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74  rs.  If we use t
dc980 68 65 0d 0a 2a 2a 20 77 72 61 70 70 65 72 73 20  he..** wrappers 
dc990 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74  then we will get
dc9a0 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f   simulated mallo
dc9b0 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74  c() failures wit
dc9c0 68 69 6e 20 74 68 69 73 0d 0a 2a 2a 20 64 72 69  hin this..** dri
dc9d0 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63  ver.  And that c
dc9e0 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20  auses all kinds 
dc9f0 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  of problems for 
dca00 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0d 0a  our tests.  We..
dca10 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65  ** could enhance
dca20 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20   SQLite to deal 
dca30 77 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d  with simulated m
dca40 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77  alloc failures w
dca50 69 74 68 69 6e 0d 0a 2a 2a 20 74 68 65 20 4f 53  ithin..** the OS
dca60 20 64 72 69 76 65 72 2c 20 62 75 74 20 74 68 65   driver, but the
dca70 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69   code to deal wi
dca80 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65  th those failure
dca90 20 77 6f 75 6c 64 20 6e 6f 74 0d 0a 2a 2a 20 62   would not..** b
dcaa0 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20 4c  e exercised on L
dcab0 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65 73  inux (which does
dcac0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c   not need to mal
dcad0 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72 69  loc() in the dri
dcae0 76 65 72 29 0d 0a 2a 2a 20 61 6e 64 20 73 6f 20  ver)..** and so 
dcaf0 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69  we would have di
dcb00 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67  fficulty writing
dcb10 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20   coverage tests 
dcb20 66 6f 72 20 74 68 61 74 0d 0a 2a 2a 20 63 6f 64  for that..** cod
dcb30 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
dcb40 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
dcb50 2c 20 77 65 20 74 68 69 6e 6b 2e 0d 0a 2a 2a 0d  , we think...**.
dcb60 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66  .** The point of
dcb70 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e   this discussion
dcb80 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20   is as follows: 
dcb90 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61   When creating a
dcba0 20 6e 65 77 0d 0a 2a 2a 20 4f 53 20 6c 61 79 65   new..** OS laye
dcbb0 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65  r for an embedde
dcbc0 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75  d system, if you
dcbd0 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61   use this file a
dcbe0 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0d 0a 2a  s an example,..*
dcbf0 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20  * avoid the use 
dcc00 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  of malloc()/free
dcc10 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69  ().  Those routi
dcc20 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f  nes work ok on O
dcc30 53 2f 32 0d 0a 2a 2a 20 64 65 73 6b 74 6f 70 73  S/2..** desktops
dcc40 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c   but not so well
dcc50 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   in embedded sys
dcc60 74 65 6d 73 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d  tems...*/..../*.
dcc70 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20  .** Macros used 
dcc80 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
dcc90 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75  ther or not to u
dcca0 73 65 20 74 68 72 65 61 64 73 2e 0d 0a 2a 2f 0d  se threads...*/.
dccb0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
dccc0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 20  ITE_THREADSAFE) 
dccd0 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
dcce0 53 41 46 45 0d 0a 23 20 64 65 66 69 6e 65 20 53  SAFE..# define S
dccf0 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44  QLITE_OS2_THREAD
dcd00 53 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  S 1..#endif..../
dcd10 2a 0d 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f  *..** Include co
dcd20 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
dcd30 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20  n to all os_*.c 
dcd40 66 69 6c 65 73 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a  files..*/../****
dcd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
dcd60 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69  de os_common.h i
dcd70 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
dcd80 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
dcd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
dcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
dcdb0 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
dcdc0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
dcdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
dcdf0 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0d  .** 2004 May 22.
dce00 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
dce10 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
dce20 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
dce30 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
dce40 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
dce50 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
dce60 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
dce70 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
dce80 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
dce90 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
dcea0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
dceb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
dcec0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
dced0 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
dcee0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
dcef0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
dcf00 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
dcf10 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
dcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcf50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcf60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
dcf70 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  ..** This file c
dcf80 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61  ontains macros a
dcf90 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20  nd a little bit 
dcfa0 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  of code that is 
dcfb0 63 6f 6d 6d 6f 6e 20 74 6f 0d 0a 2a 2a 20 61 6c  common to..** al
dcfc0 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72  l of the platfor
dcfd0 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73  m-specific files
dcfe0 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73   (os_*.c) and is
dcff0 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20   #included into 
dd000 74 68 6f 73 65 0d 0a 2a 2a 20 66 69 6c 65 73 2e  those..** files.
dd010 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69  ..**..** This fi
dd020 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
dd030 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
dd040 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
dd050 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0d 0a 2a    It is not a..*
dd060 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
dd070 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0d 0a  e header file...
dd080 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f  */..#ifndef _OS_
dd090 43 4f 4d 4d 4f 4e 5f 48 5f 0d 0a 23 64 65 66 69  COMMON_H_..#defi
dd0a0 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ne _OS_COMMON_H_
dd0b0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 20 6c 65  ..../*..** At le
dd0c0 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76  ast two bugs hav
dd0d0 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63  e slipped in bec
dd0e0 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20  ause we changed 
dd0f0 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  the MEMORY_DEBUG
dd100 0d 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  ..** macro to SQ
dd110 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
dd120 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
dd130 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
dd140 20 6d 61 64 65 20 74 68 65 0d 0a 2a 2a 20 73 77   made the..** sw
dd150 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
dd160 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
dd170 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
dd180 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
dd190 69 6d 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  ime...*/..#ifdef
dd1a0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0d 0a 23   MEMORY_DEBUG..#
dd1b0 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
dd1c0 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
dd1d0 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
dd1e0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
dd1f0 73 74 65 61 64 2e 22 0d 0a 23 65 6e 64 69 66 0d  stead."..#endif.
dd200 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  ...#if defined(S
dd210 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
dd220 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
dd230 42 55 47 29 0d 0a 23 20 69 66 6e 64 65 66 20 53  BUG)..# ifndef S
dd240 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54  QLITE_DEBUG_OS_T
dd250 52 41 43 45 0d 0a 23 20 20 20 64 65 66 69 6e 65  RACE..#   define
dd260 20 53 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53   SQLITE_DEBUG_OS
dd270 5f 54 52 41 43 45 20 30 0d 0a 23 20 65 6e 64 69  _TRACE 0..# endi
dd280 66 0d 0a 20 20 69 6e 74 20 73 71 6c 69 74 65 33  f..  int sqlite3
dd290 4f 53 54 72 61 63 65 20 3d 20 53 51 4c 49 54 45  OSTrace = SQLITE
dd2a0 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45 3b  _DEBUG_OS_TRACE;
dd2b0 0d 0a 23 20 64 65 66 69 6e 65 20 4f 53 54 52 41  ..# define OSTRA
dd2c0 43 45 28 58 29 20 20 20 20 20 20 20 20 20 20 69  CE(X)          i
dd2d0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
dd2e0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
dd2f0 50 72 69 6e 74 66 20 58 0d 0a 23 65 6c 73 65 0d  Printf X..#else.
dd300 0a 23 20 64 65 66 69 6e 65 20 4f 53 54 52 41 43  .# define OSTRAC
dd310 45 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  E(X)..#endif....
dd320 2f 2a 0d 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  /*..** Macros fo
dd330 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
dd340 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
dd350 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
dd360 6c 79 20 77 6f 72 6b 73 0d 0a 2a 2a 20 6f 6e 20  ly works..** on 
dd370 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0d 0a  i486 hardware...
dd380 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
dd390 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52  E_PERFORMANCE_TR
dd3a0 41 43 45 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 68  ACE..../* ..** h
dd3b0 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73  wtime.h contains
dd3c0 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65   inline assemble
dd3d0 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65  r code for imple
dd3e0 6d 65 6e 74 69 6e 67 20 0d 0a 2a 2a 20 68 69 67  menting ..** hig
dd3f0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
dd400 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0d 0a  ming routines...
dd410 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
dd420 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
dd430 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
dd440 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
dd450 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
dd460 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
dd470 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
dd480 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
dd490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd4b0 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
dd4c0 38 20 4d 61 79 20 32 37 0d 0a 2a 2a 0d 0a 2a 2a  8 May 27..**..**
dd4d0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
dd4e0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
dd4f0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
dd500 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
dd510 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
dd520 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
dd530 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
dd540 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
dd550 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
dd560 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
dd570 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
dd580 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
dd590 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
dd5a0 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
dd5b0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
dd5c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
dd5d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
dd5e0 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
dd5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd630 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
dd640 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
dd650 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
dd660 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
dd670 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
dd680 65 22 0d 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20  e"..** counters 
dd690 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50  for x86 class CP
dd6a0 55 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  Us...*/..#ifndef
dd6b0 20 5f 48 57 54 49 4d 45 5f 48 5f 0d 0a 23 64 65   _HWTIME_H_..#de
dd6c0 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0d  fine _HWTIME_H_.
dd6d0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
dd6e0 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
dd6f0 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
dd700 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
dd710 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
dd720 73 2e 0d 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  s...** It uses t
dd730 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
dd740 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
dd750 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
dd760 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 70 72 6f  t of the..** pro
dd770 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72  cessor and retur
dd780 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20  ns that value.  
dd790 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
dd7a0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0d 0a 2a   for high-res..*
dd7b0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0d 0a 2a 2f  * profiling...*/
dd7c0 0d 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  ..#if (defined(_
dd7d0 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
dd7e0 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
dd7f0 26 20 5c 0d 0a 20 20 20 20 20 20 28 64 65 66 69  & \..      (defi
dd800 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
dd810 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
dd820 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
dd830 36 29 29 0d 0a 0d 0a 20 20 23 69 66 20 64 65 66  6))....  #if def
dd840 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0d 0a  ined(__GNUC__)..
dd850 0d 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
dd860 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
dd870 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
dd880 7b 0d 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {..     unsigned
dd890 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0d 0a 20 20   int lo, hi;..  
dd8a0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
dd8b0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
dd8c0 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
dd8d0 64 22 20 28 68 69 29 29 3b 0d 0a 20 20 20 20 20  d" (hi));..     
dd8e0 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75  return (sqlite_u
dd8f0 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c  int64)hi << 32 |
dd900 20 6c 6f 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23   lo;..  }....  #
dd910 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
dd920 43 5f 56 45 52 29 0d 0a 0d 0a 20 20 5f 5f 64 65  C_VER)....  __de
dd930 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f  clspec(naked) __
dd940 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69  inline sqlite_ui
dd950 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c  nt64 __cdecl sql
dd960 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
dd970 7b 0d 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0d  {..     __asm {.
dd980 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0d 0a  .        rdtsc..
dd990 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
dd9a0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
dd9b0 20 61 74 20 45 44 58 3a 45 41 58 0d 0a 20 20 20   at EDX:EAX..   
dd9c0 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23 65    }..  }....  #e
dd9d0 6e 64 69 66 0d 0a 0d 0a 23 65 6c 69 66 20 28 64  ndif....#elif (d
dd9e0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
dd9f0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38   && defined(__x8
dda00 36 5f 36 34 5f 5f 29 29 0d 0a 0d 0a 20 20 5f 5f  6_64__))....  __
dda10 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
dda20 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
dda30 74 69 6d 65 28 76 6f 69 64 29 7b 0d 0a 20 20 20  time(void){..   
dda40 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
dda50 20 76 61 6c 3b 0d 0a 20 20 20 20 20 20 5f 5f 61   val;..      __a
dda60 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
dda70 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41  _ ("rdtsc" : "=A
dda80 22 20 28 76 61 6c 29 29 3b 0d 0a 20 20 20 20 20  " (val));..     
dda90 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 20 20   return val;..  
ddaa0 7d 0d 0a 20 0d 0a 23 65 6c 69 66 20 28 64 65 66  }.. ..#elif (def
ddab0 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
ddac0 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
ddad0 5f 29 29 0d 0a 0d 0a 20 20 5f 5f 69 6e 6c 69 6e  _))....  __inlin
ddae0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
ddaf0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
ddb00 76 6f 69 64 29 7b 0d 0a 20 20 20 20 20 20 75 6e  void){..      un
ddb10 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
ddb20 20 72 65 74 76 61 6c 3b 0d 0a 20 20 20 20 20 20   retval;..      
ddb30 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
ddb40 6e 6b 3b 0d 0a 20 20 20 20 20 20 5f 5f 61 73 6d  nk;..      __asm
ddb50 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
ddb60 28 22 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20 20  ("\n\..         
ddb70 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20   1:      mftbu  
ddb80 20 25 31 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20   %1\n\..        
ddb90 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
ddba0 20 20 25 4c 30 5c 6e 5c 0d 0a 20 20 20 20 20 20    %L0\n\..      
ddbb0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
ddbc0 75 20 20 20 25 30 5c 6e 5c 0d 0a 20 20 20 20 20  u   %0\n\..     
ddbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
ddbe0 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0d 0a 20  w    %0,%1\n\.. 
ddbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ddc00 20 62 6e 65 20 20 20 20 20 31 62 22 0d 0a 20 20   bne     1b"..  
ddc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ddc20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c  : "=r" (retval),
ddc30 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0d 0a   "=r" (junk));..
ddc40 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74        return ret
ddc50 76 61 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 65 6c  val;..  }....#el
ddc60 73 65 0d 0a 0d 0a 20 20 23 65 72 72 6f 72 20 4e  se....  #error N
ddc70 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  eed implementati
ddc80 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74  on of sqlite3Hwt
ddc90 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
ddca0 6c 61 74 66 6f 72 6d 2e 0d 0a 0d 0a 20 20 2f 2a  latform.....  /*
ddcb0 0d 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  ..  ** To compil
ddcc0 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
ddcd0 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
ddce0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
ddcf0 70 6c 61 74 66 6f 72 6d 2c 0d 0a 20 20 2a 2a 20  platform,..  ** 
ddd00 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
ddd10 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
ddd20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
ddd30 6f 77 69 6e 67 0d 0a 20 20 2a 2a 20 73 74 75 62  owing..  ** stub
ddd40 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20   function.  You 
ddd50 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67  will lose timing
ddd60 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e   support for man
ddd70 79 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  y..  ** of the d
ddd80 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
ddd90 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
ddda0 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
dddb0 0d 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d  ..  ** least com
dddc0 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0d 0a 20  pile and run... 
dddd0 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
ddde0 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e  ATE   sqlite_uin
dddf0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
dde00 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  e(void){ return 
dde10 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  ((sqlite_uint64)
dde20 30 29 3b 20 7d 0d 0a 0d 0a 23 65 6e 64 69 66 0d  0); }....#endif.
dde30 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  ...#endif /* !de
dde40 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
dde50 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ) */..../*******
dde60 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
dde70 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
dde80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dde90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddea0 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
ddeb0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
ddec0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
dded0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d  t off in os_comm
ddee0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
ddef0 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 73 74 61 74  *******/....stat
ddf00 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
ddf10 20 67 5f 73 74 61 72 74 3b 0d 0a 73 74 61 74 69   g_start;..stati
ddf20 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
ddf30 67 5f 65 6c 61 70 73 65 64 3b 0d 0a 23 64 65 66  g_elapsed;..#def
ddf40 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
ddf50 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71        g_start=sq
ddf60 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0d 0a 23  lite3Hwtime()..#
ddf70 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
ddf80 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73           g_elaps
ddf90 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ed=sqlite3Hwtime
ddfa0 28 29 2d 67 5f 73 74 61 72 74 0d 0a 23 64 65 66  ()-g_start..#def
ddfb0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
ddfc0 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0d  D     g_elapsed.
ddfd0 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20  .#else..#define 
ddfe0 54 49 4d 45 52 5f 53 54 41 52 54 0d 0a 23 64 65  TIMER_START..#de
ddff0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0d 0a  fine TIMER_END..
de000 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
de010 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69  APSED     ((sqli
de020 74 65 5f 75 69 6e 74 36 34 29 30 29 0d 0a 23 65  te_uint64)0)..#e
de030 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ndif..../*..** I
de040 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
de050 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53  h the SQLITE_TES
de060 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65  T macro set, the
de070 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
de080 62 6c 6f 63 6b 0d 0a 2a 2a 20 6f 66 20 63 6f 64  block..** of cod
de090 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
de0a0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
de0b0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
de0c0 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0d 0a  O error.  This..
de0d0 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ** is used for t
de0e0 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72  esting the I/O r
de0f0 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0d 0a  ecovery logic...
de100 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
de110 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41  E_TEST..SQLITE_A
de120 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
de130 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
de140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
de150 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49  otal number of I
de160 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0d 0a 53 51  /O Errors */..SQ
de170 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
de180 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
de190 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  rdhit = 0;      
de1a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
de1b0 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73  on-benign errors
de1c0 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20   */..SQLITE_API 
de1d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
de1e0 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30  rror_pending = 0
de1f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
de200 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20  t down to first 
de210 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0d 0a 53 51  I/O error */..SQ
de220 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
de230 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
de240 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
de250 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
de260 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
de270 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
de280 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
de290 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20  ror_benign = 0; 
de2a0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
de2b0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65  if errors are be
de2c0 6e 69 67 6e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  nign */..SQLITE_
de2d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
de2e0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
de2f0 20 3d 20 30 3b 0d 0a 53 51 4c 49 54 45 5f 41 50   = 0;..SQLITE_AP
de300 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
de310 73 6b 66 75 6c 6c 20 3d 20 30 3b 0d 0a 23 64 65  skfull = 0;..#de
de320 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
de330 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71  rrorBenign(X) sq
de340 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
de350 65 6e 69 67 6e 3d 28 58 29 0d 0a 23 64 65 66 69  enign=(X)..#defi
de360 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
de370 6f 72 28 43 4f 44 45 29 20 20 5c 0d 0a 20 20 69  or(CODE)  \..  i
de380 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
de390 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
de3a0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
de3b0 5f 68 69 74 29 20 5c 0d 0a 20 20 20 20 20 20 20  _hit) \..       
de3c0 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  || sqlite3_io_er
de3d0 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d  ror_pending-- ==
de3e0 20 31 20 29 20 20 5c 0d 0a 20 20 20 20 20 20 20   1 )  \..       
de3f0 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69         { local_i
de400 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0d  oerr(); CODE; }.
de410 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
de420 61 6c 5f 69 6f 65 72 72 28 29 7b 0d 0a 20 20 49  al_ioerr(){..  I
de430 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e  OTRACE(("IOERR\n
de440 22 29 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  "));..  sqlite3_
de450 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0d  io_error_hit++;.
de460 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
de470 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
de480 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
de490 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0d 0a 7d  or_hardhit++;..}
de4a0 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ..#define Simula
de4b0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
de4c0 43 4f 44 45 29 20 5c 0d 0a 20 20 20 69 66 28 20  CODE) \..   if( 
de4d0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
de4e0 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0d 0a 20  _pending ){ \.. 
de4f0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
de500 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
de510 20 3d 3d 20 31 20 29 7b 20 5c 0d 0a 20 20 20 20   == 1 ){ \..    
de520 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29     local_ioerr()
de530 3b 20 5c 0d 0a 20 20 20 20 20 20 20 73 71 6c 69  ; \..       sqli
de540 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
de550 3b 20 5c 0d 0a 20 20 20 20 20 20 20 73 71 6c 69  ; \..       sqli
de560 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
de570 20 3d 20 31 3b 20 5c 0d 0a 20 20 20 20 20 20 20   = 1; \..       
de580 43 4f 44 45 3b 20 5c 0d 0a 20 20 20 20 20 7d 65  CODE; \..     }e
de590 6c 73 65 7b 20 5c 0d 0a 20 20 20 20 20 20 20 73  lse{ \..       s
de5a0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
de5b0 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0d 0a 20 20  pending--; \..  
de5c0 20 20 20 7d 20 5c 0d 0a 20 20 20 7d 0d 0a 23 65     } \..   }..#e
de5d0 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d  lse..#define Sim
de5e0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
de5f0 67 6e 28 58 29 0d 0a 23 64 65 66 69 6e 65 20 53  gn(X)..#define S
de600 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
de610 29 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  )..#define Simul
de620 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
de630 28 41 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  (A)..#endif..../
de640 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  *..** When testi
de650 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
de660 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
de670 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0d 0a 2a  f open files...*
de680 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
de690 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50  _TEST..SQLITE_AP
de6a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
de6b0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20  en_file_count = 
de6c0 30 3b 0d 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  0;..#define Open
de6d0 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69  Counter(X)  sqli
de6e0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
de6f0 75 6e 74 2b 3d 28 58 29 0d 0a 23 65 6c 73 65 0d  unt+=(X)..#else.
de700 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
de710 6e 74 65 72 28 58 29 0d 0a 23 65 6e 64 69 66 0d  nter(X)..#endif.
de720 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  ...#endif /* !de
de730 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
de740 5f 48 5f 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  _H_) */..../****
de750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
de760 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
de770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
de7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
de7b0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
de7c0 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 6f  left off in os_o
de7d0 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s2.c ***********
de7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f  **********/..../
de7f0 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
de800 6e 63 65 73 20 2a 2f 0d 0a 74 79 70 65 64 65 66  nces */..typedef
de810 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20   struct os2File 
de820 6f 73 32 46 69 6c 65 3b 20 20 20 20 20 20 20 20  os2File;        
de830 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 73 74 72   /* The file str
de840 75 63 74 75 72 65 20 2a 2f 0d 0a 74 79 70 65 64  ucture */..typed
de850 65 66 20 73 74 72 75 63 74 20 6f 73 32 53 68 6d  ef struct os2Shm
de860 4e 6f 64 65 20 6f 73 32 53 68 6d 4e 6f 64 65 3b  Node os2ShmNode;
de870 20 20 20 2f 2a 20 41 20 73 68 61 72 65 64 20 64     /* A shared d
de880 65 73 63 72 69 74 69 76 65 20 6d 65 6d 6f 72 79  escritive memory
de890 20 6e 6f 64 65 20 2a 2f 0d 0a 74 79 70 65 64 65   node */..typede
de8a0 66 20 73 74 72 75 63 74 20 6f 73 32 53 68 6d 4c  f struct os2ShmL
de8b0 69 6e 6b 20 6f 73 32 53 68 6d 4c 69 6e 6b 3b 20  ink os2ShmLink; 
de8c0 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f    /* A connectio
de8d0 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d 65 6d 6f  n to shared-memo
de8e0 72 79 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ry */..../*..** 
de8f0 54 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75  The os2File stru
de900 63 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73  cture is subclas
de910 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c  s of sqlite3_fil
de920 65 20 73 70 65 63 69 66 69 63 20 66 6f 72 20 74  e specific for t
de930 68 65 20 4f 53 2f 32 0d 0a 2a 2a 20 70 72 6f 74  he OS/2..** prot
de940 61 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0d 0a  ability layer...
de950 2a 2f 0d 0a 73 74 72 75 63 74 20 6f 73 32 46 69  */..struct os2Fi
de960 6c 65 20 7b 0d 0a 20 20 63 6f 6e 73 74 20 73 71  le {..  const sq
de970 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
de980 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41   *pMethod;  /* A
de990 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20  lways the first 
de9a0 65 6e 74 72 79 20 2a 2f 0d 0a 20 20 48 46 49 4c  entry */..  HFIL
de9b0 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  E h;            
de9c0 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20        /* Handle 
de9d0 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
de9e0 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74  e file */..  int
de9f0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
dea00 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
dea10 70 72 6f 76 69 64 65 64 20 74 6f 20 6f 73 32 4f  provided to os2O
dea20 70 65 6e 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20  pen() */..  int 
dea30 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20  locktype;       
dea40 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
dea50 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20   lock currently 
dea60 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c  held on this fil
dea70 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 43 68  e */..  int szCh
dea80 75 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  unk;            
dea90 20 20 2f 2a 20 43 68 75 6e 6b 20 73 69 7a 65 20    /* Chunk size 
deaa0 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 46 43  configured by FC
deab0 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 2a  NTL_CHUNK_SIZE *
deac0 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c  /..  char *zFull
dead0 50 61 74 68 43 70 3b 20 20 20 20 20 20 20 20 2f  PathCp;        /
deae0 2a 20 46 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65  * Full path name
deaf0 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   of this file */
deb00 0d 0a 20 20 6f 73 32 53 68 6d 4c 69 6e 6b 20 2a  ..  os2ShmLink *
deb10 70 53 68 6d 4c 69 6e 6b 3b 20 20 20 20 20 2f 2a  pShmLink;     /*
deb20 20 49 6e 73 74 61 6e 63 65 20 6f 66 20 73 68 61   Instance of sha
deb30 72 65 64 20 6d 65 6d 6f 72 79 20 6f 6e 20 74 68  red memory on th
deb40 69 73 20 66 69 6c 65 20 2a 2f 0d 0a 7d 3b 0d 0a  is file */..};..
deb50 0d 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54  ..#define LOCK_T
deb60 49 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68  IMEOUT 10L /* th
deb70 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
deb80 67 20 74 69 6d 65 6f 75 74 20 2a 2f 0d 0a 0d 0a  g timeout */....
deb90 2f 2a 0d 0a 2a 2a 20 4d 69 73 73 69 6e 67 20 66  /*..** Missing f
deba0 72 6f 6d 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e  rom some version
debb0 73 20 6f 66 20 74 68 65 20 4f 53 2f 32 20 74 6f  s of the OS/2 to
debc0 6f 6c 6b 69 74 20 2d 0d 0a 2a 2a 20 75 73 65 64  olkit -..** used
debd0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 72 6f   to allocate fro
debe0 6d 20 68 69 67 68 20 6d 65 6d 6f 72 79 20 69 66  m high memory if
debf0 20 70 6f 73 73 69 62 6c 65 0d 0a 2a 2f 0d 0a 23   possible..*/..#
dec00 69 66 6e 64 65 66 20 4f 42 4a 5f 41 4e 59 0d 0a  ifndef OBJ_ANY..
dec10 23 20 64 65 66 69 6e 65 20 4f 42 4a 5f 41 4e 59  # define OBJ_ANY
dec20 20 30 78 30 30 30 30 30 34 30 30 0d 0a 23 65 6e   0x00000400..#en
dec30 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  dif..../********
dec40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dec50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dec60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dec70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dec80 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e 65  *****..** The ne
dec90 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  xt group of rout
deca0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74  ines implement t
decb0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73  he I/O methods s
decc0 70 65 63 69 66 69 65 64 0d 0a 2a 2a 20 62 79 20  pecified..** by 
decd0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
dece0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0d 0a  ethods object...
decf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ded00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ded10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ded20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ded30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
ded40 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20  .../*..** Close 
ded50 61 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  a file...*/..sta
ded60 74 69 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65  tic int os2Close
ded70 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
ded80 69 64 20 29 7b 0d 0a 20 20 41 50 49 52 45 54 20  id ){..  APIRET 
ded90 72 63 3b 0d 0a 20 20 6f 73 32 46 69 6c 65 20 2a  rc;..  os2File *
deda0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
dedb0 2a 29 69 64 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  *)id;....  asser
dedc0 74 28 20 69 64 21 3d 30 20 29 3b 0d 0a 20 20 4f  t( id!=0 );..  O
dedd0 53 54 52 41 43 45 28 28 20 22 43 4c 4f 53 45 20  STRACE(( "CLOSE 
dede0 25 64 20 28 25 73 29 5c 6e 22 2c 20 70 46 69 6c  %d (%s)\n", pFil
dedf0 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 7a 46 75  e->h, pFile->zFu
dee00 6c 6c 50 61 74 68 43 70 20 29 29 3b 0d 0a 0d 0a  llPathCp ));....
dee10 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28    rc = DosClose(
dee20 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0d 0a 0d 0a   pFile->h );....
dee30 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 66 6c 61    if( pFile->fla
dee40 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
dee50 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29  _DELETEONCLOSE )
dee60 0d 0a 20 20 20 20 44 6f 73 46 6f 72 63 65 44 65  ..    DosForceDe
dee70 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c 65  lete( (PSZ)pFile
dee80 2d 3e 7a 46 75 6c 6c 50 61 74 68 43 70 20 29 3b  ->zFullPathCp );
dee90 0d 0a 0d 0a 20 20 66 72 65 65 28 20 70 46 69 6c  ....  free( pFil
deea0 65 2d 3e 7a 46 75 6c 6c 50 61 74 68 43 70 20 29  e->zFullPathCp )
deeb0 3b 0d 0a 20 20 70 46 69 6c 65 2d 3e 7a 46 75 6c  ;..  pFile->zFul
deec0 6c 50 61 74 68 43 70 20 3d 20 4e 55 4c 4c 3b 0d  lPathCp = NULL;.
deed0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
deee0 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0d 0a 20  pe = NO_LOCK;.. 
deef0 20 70 46 69 6c 65 2d 3e 68 20 3d 20 28 48 46 49   pFile->h = (HFI
def00 4c 45 29 2d 31 3b 0d 0a 20 20 70 46 69 6c 65 2d  LE)-1;..  pFile-
def10 3e 66 6c 61 67 73 20 3d 20 30 3b 0d 0a 0d 0a 20  >flags = 0;.... 
def20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20 2d 31   OpenCounter( -1
def30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63   );..  return rc
def40 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53   == NO_ERROR ? S
def50 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
def60 45 5f 49 4f 45 52 52 3b 0d 0a 7d 0d 0a 0d 0a 2f  E_IOERR;..}..../
def70 2a 0d 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  *..** Read data 
def80 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f  from a file into
def90 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75   a buffer.  Retu
defa0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
defb0 61 6c 6c 0d 0a 2a 2a 20 62 79 74 65 73 20 77 65  all..** bytes we
defc0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
defd0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
defe0 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
deff0 67 20 67 6f 65 73 0d 0a 2a 2a 20 77 72 6f 6e 67  g goes..** wrong
df000 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
df010 74 20 6f 73 32 52 65 61 64 28 0d 0a 20 20 73 71  t os2Read(..  sq
df020 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
df030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
df040 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72   File to read fr
df050 6f 6d 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70  om */..  void *p
df060 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
df070 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
df080 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74  e content into t
df090 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20  his buffer */.. 
df0a0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
df0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
df0c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
df0d0 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0d 0a  tes to read */..
df0e0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
df0f0 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20  offset          
df100 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69    /* Begin readi
df110 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65  ng at this offse
df120 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 55 4c 4f 4e  t */..){..  ULON
df130 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d  G fileLocation =
df140 20 30 4c 3b 0d 0a 20 20 55 4c 4f 4e 47 20 67 6f   0L;..  ULONG go
df150 74 3b 0d 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  t;..  os2File *p
df160 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
df170 29 69 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  )id;..  assert( 
df180 69 64 21 3d 30 20 29 3b 0d 0a 20 20 53 69 6d 75  id!=0 );..  Simu
df190 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
df1a0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
df1b0 5f 52 45 41 44 20 29 3b 0d 0a 20 20 4f 53 54 52  _READ );..  OSTR
df1c0 41 43 45 28 28 20 22 52 45 41 44 20 25 64 20 6c  ACE(( "READ %d l
df1d0 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
df1e0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
df1f0 74 79 70 65 20 29 29 3b 0d 0a 20 20 69 66 28 20  type ));..  if( 
df200 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 46  DosSetFilePtr(pF
df210 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  ile->h, offset, 
df220 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c  FILE_BEGIN, &fil
df230 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f  eLocation) != NO
df240 5f 45 52 52 4f 52 20 29 7b 0d 0a 20 20 20 20 72  _ERROR ){..    r
df250 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
df260 52 52 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  RR;..  }..  if( 
df270 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e  DosRead( pFile->
df280 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67  h, pBuf, amt, &g
df290 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ot ) != NO_ERROR
df2a0 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
df2b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
df2c0 44 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 67  D;..  }..  if( g
df2d0 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74  ot == (ULONG)amt
df2e0 20 29 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53   )..    return S
df2f0 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 65 6c 73  QLITE_OK;..  els
df300 65 20 7b 0d 0a 20 20 20 20 2f 2a 20 55 6e 72 65  e {..    /* Unre
df310 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74  ad portions of t
df320 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20  he input buffer 
df330 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c  must be zero-fil
df340 6c 65 64 20 2a 2f 0d 0a 20 20 20 20 6d 65 6d 73  led */..    mems
df350 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66  et(&((char*)pBuf
df360 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67  )[got], 0, amt-g
df370 6f 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ot);..    return
df380 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
df390 4f 52 54 5f 52 45 41 44 3b 0d 0a 20 20 7d 0d 0a  ORT_READ;..  }..
df3a0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74  }..../*..** Writ
df3b0 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
df3c0 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
df3d0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
df3e0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0d 0a  _OK on success..
df3f0 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  ** or some other
df400 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66   error code on f
df410 61 69 6c 75 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  ailure...*/..sta
df420 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65  tic int os2Write
df430 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  (..  sqlite3_fil
df440 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
df450 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
df460 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0d 0a 20  write into */.. 
df470 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
df480 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
df490 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f   /* The bytes to
df4a0 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0d 0a   be written */..
df4b0 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20    int amt,      
df4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
df4d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
df4e0 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f  ytes to write */
df4f0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
df500 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20  4 offset        
df510 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
df520 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  to the file to b
df530 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
df540 2a 2f 0d 0a 29 7b 0d 0a 20 20 55 4c 4f 4e 47 20  */..){..  ULONG 
df550 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30  fileLocation = 0
df560 4c 3b 0d 0a 20 20 41 50 49 52 45 54 20 72 63 20  L;..  APIRET rc 
df570 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0d 0a 20 20 55  = NO_ERROR;..  U
df580 4c 4f 4e 47 20 77 72 6f 74 65 3b 0d 0a 20 20 6f  LONG wrote;..  o
df590 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
df5a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0d 0a 20  (os2File*)id;.. 
df5b0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
df5c0 3b 0d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
df5d0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
df5e0 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20  ITE_IOERR_WRITE 
df5f0 29 3b 0d 0a 20 20 53 69 6d 75 6c 61 74 65 44 69  );..  SimulateDi
df600 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74  skfullError( ret
df610 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  urn SQLITE_FULL 
df620 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 20  );..  OSTRACE(( 
df630 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25  "WRITE %d lock=%
df640 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
df650 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
df660 29 29 3b 0d 0a 20 20 69 66 28 20 44 6f 73 53 65  ));..  if( DosSe
df670 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e  tFilePtr(pFile->
df680 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f  h, offset, FILE_
df690 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61  BEGIN, &fileLoca
df6a0 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f  tion) != NO_ERRO
df6b0 52 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  R ){..    return
df6c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0d 0a   SQLITE_IOERR;..
df6d0 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 61    }..  assert( a
df6e0 6d 74 3e 30 20 29 3b 0d 0a 20 20 77 68 69 6c 65  mt>0 );..  while
df6f0 28 20 61 6d 74 20 3e 20 30 20 26 26 0d 0a 20 20  ( amt > 0 &&..  
df700 20 20 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f         ( rc = Do
df710 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68  sWrite( pFile->h
df720 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61  , (PVOID)pBuf, a
df730 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d  mt, &wrote ) ) =
df740 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0d 0a 20  = NO_ERROR &&.. 
df750 20 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e 20          wrote > 
df760 30 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 61 6d 74  0..  ){..    amt
df770 20 2d 3d 20 77 72 6f 74 65 3b 0d 0a 20 20 20 20   -= wrote;..    
df780 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29  pBuf = &((char*)
df790 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0d 0a 20  pBuf)[wrote];.. 
df7a0 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 28   }....  return (
df7b0 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
df7c0 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74 29 77 72  || amt > (int)wr
df7d0 6f 74 65 20 29 20 3f 20 53 51 4c 49 54 45 5f 46  ote ) ? SQLITE_F
df7e0 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b  ULL : SQLITE_OK;
df7f0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72  ..}..../*..** Tr
df800 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66  uncate an open f
df810 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69  ile to a specifi
df820 65 64 20 73 69 7a 65 0d 0a 2a 2f 0d 0a 73 74 61  ed size..*/..sta
df830 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63  tic int os2Trunc
df840 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ate( sqlite3_fil
df850 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65  e *id, i64 nByte
df860 20 29 7b 0d 0a 20 20 41 50 49 52 45 54 20 72 63   ){..  APIRET rc
df870 3b 0d 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  ;..  os2File *pF
df880 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
df890 69 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69  id;..  assert( i
df8a0 64 21 3d 30 20 29 3b 0d 0a 20 20 4f 53 54 52 41  d!=0 );..  OSTRA
df8b0 43 45 28 28 20 22 54 52 55 4e 43 41 54 45 20 25  CE(( "TRUNCATE %
df8c0 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65  d %lld\n", pFile
df8d0 2d 3e 68 2c 20 6e 42 79 74 65 20 29 29 3b 0d 0a  ->h, nByte ));..
df8e0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
df8f0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
df900 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20  _IOERR_TRUNCATE 
df910 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68  );....  /* If th
df920 65 20 75 73 65 72 20 68 61 73 20 63 6f 6e 66 69  e user has confi
df930 67 75 72 65 64 20 61 20 63 68 75 6e 6b 2d 73 69  gured a chunk-si
df940 7a 65 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  ze for this file
df950 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0d 0a  , truncate the..
df960 20 20 2a 2a 20 66 69 6c 65 20 73 6f 20 74 68 61    ** file so tha
df970 74 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f 66  t it consists of
df980 20 61 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62   an integer numb
df990 65 72 20 6f 66 20 63 68 75 6e 6b 73 20 28 69 2e  er of chunks (i.
df9a0 65 2e 20 74 68 65 0d 0a 20 20 2a 2a 20 61 63 74  e. the..  ** act
df9b0 75 61 6c 20 66 69 6c 65 20 73 69 7a 65 20 61 66  ual file size af
df9c0 74 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ter the operatio
df9d0 6e 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20  n may be larger 
df9e0 74 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 74  than the request
df9f0 65 64 0d 0a 20 20 2a 2a 20 73 69 7a 65 29 2e 0d  ed..  ** size)..
dfa00 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46 69  .  */..  if( pFi
dfa10 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 29 7b 0d 0a  le->szChunk ){..
dfa20 20 20 20 20 6e 42 79 74 65 20 3d 20 28 28 6e 42      nByte = ((nB
dfa30 79 74 65 20 2b 20 70 46 69 6c 65 2d 3e 73 7a 43  yte + pFile->szC
dfa40 68 75 6e 6b 20 2d 20 31 29 2f 70 46 69 6c 65 2d  hunk - 1)/pFile-
dfa50 3e 73 7a 43 68 75 6e 6b 29 20 2a 20 70 46 69 6c  >szChunk) * pFil
dfa60 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0d 0a 20 20 7d  e->szChunk;..  }
dfa70 0d 0a 20 20 0d 0a 20 20 72 63 20 3d 20 44 6f 73  ..  ..  rc = Dos
dfa80 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69  SetFileSize( pFi
dfa90 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0d  le->h, nByte );.
dfaa0 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20  .  return rc == 
dfab0 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
dfac0 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
dfad0 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0d 0a 7d  ERR_TRUNCATE;..}
dfae0 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
dfaf0 45 5f 54 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 43  E_TEST../*..** C
dfb00 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
dfb10 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64  of fullsyncs and
dfb20 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20   normal syncs.  
dfb30 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
dfb40 74 65 73 74 0d 0a 2a 2a 20 74 68 61 74 20 73 79  test..** that sy
dfb50 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63  ncs and fullsync
dfb60 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61  s are occuring a
dfb70 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
dfb80 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  s...*/..SQLITE_A
dfb90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
dfba0 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a  ync_count = 0;..
dfbb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
dfbc0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
dfbd0 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a 23 65 6e 64  count = 0;..#end
dfbe0 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b  if..../*..** Mak
dfbf0 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
dfc00 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
dfc10 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
dfc20 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0d 0a 2a  tted to disk...*
dfc30 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  /..static int os
dfc40 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66  2Sync( sqlite3_f
dfc50 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
dfc60 67 73 20 29 7b 0d 0a 20 20 6f 73 32 46 69 6c 65  gs ){..  os2File
dfc70 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69   *pFile = (os2Fi
dfc80 6c 65 2a 29 69 64 3b 0d 0a 20 20 4f 53 54 52 41  le*)id;..  OSTRA
dfc90 43 45 28 28 20 22 53 59 4e 43 20 25 64 20 6c 6f  CE(( "SYNC %d lo
dfca0 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
dfcb0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
dfcc0 79 70 65 20 29 29 3b 0d 0a 23 69 66 64 65 66 20  ype ));..#ifdef 
dfcd0 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 69  SQLITE_TEST..  i
dfce0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
dfcf0 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0d 0a 20  E_SYNC_FULL){.. 
dfd00 20 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73     sqlite3_fulls
dfd10 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0d 0a 20 20  ync_count++;..  
dfd20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e  }..  sqlite3_syn
dfd30 63 5f 63 6f 75 6e 74 2b 2b 3b 0d 0a 23 65 6e 64  c_count++;..#end
dfd40 69 66 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
dfd50 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
dfd60 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
dfd70 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
dfd80 6e 67 20 69 73 20 61 0d 0a 20 20 2a 2a 20 6e 6f  ng is a..  ** no
dfd90 2d 6f 70 0d 0a 20 20 2a 2f 0d 0a 23 69 66 64 65  -op..  */..#ifde
dfda0 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
dfdb0 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
dfdc0 45 54 45 52 28 70 46 69 6c 65 29 3b 0d 0a 20 20  ETER(pFile);..  
dfdd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dfde0 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 72 65 74 75  ;..#else..  retu
dfdf0 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65  rn DosResetBuffe
dfe00 72 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d  r( pFile->h ) ==
dfe10 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49   NO_ERROR ? SQLI
dfe20 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
dfe30 4f 45 52 52 3b 0d 0a 23 65 6e 64 69 66 0d 0a 7d  OERR;..#endif..}
dfe40 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 74 65 72  ..../*..** Deter
dfe50 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74  mine the current
dfe60 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20   size of a file 
dfe70 69 6e 20 62 79 74 65 73 0d 0a 2a 2f 0d 0a 73 74  in bytes..*/..st
dfe80 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65  atic int os2File
dfe90 53 69 7a 65 28 20 73 71 6c 69 74 65 33 5f 66 69  Size( sqlite3_fi
dfea0 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f  le *id, sqlite3_
dfeb0 69 6e 74 36 34 20 2a 70 53 69 7a 65 20 29 7b 0d  int64 *pSize ){.
dfec0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
dfed0 4f 5f 45 52 52 4f 52 3b 0d 0a 20 20 46 49 4c 45  O_ERROR;..  FILE
dfee0 53 54 41 54 55 53 33 20 66 73 74 73 33 46 69 6c  STATUS3 fsts3Fil
dfef0 65 49 6e 66 6f 3b 0d 0a 20 20 6d 65 6d 73 65 74  eInfo;..  memset
dff00 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c  (&fsts3FileInfo,
dff10 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33   0, sizeof(fsts3
dff20 46 69 6c 65 49 6e 66 6f 29 29 3b 0d 0a 20 20 61  FileInfo));..  a
dff30 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0d  ssert( id!=0 );.
dff40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
dff50 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
dff60 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b  E_IOERR_FSTAT );
dff70 0d 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72  ..  rc = DosQuer
dff80 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32  yFileInfo( ((os2
dff90 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49  File*)id)->h, FI
dffa0 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74  L_STANDARD, &fst
dffb0 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65  s3FileInfo, size
dffc0 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
dffd0 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 3d 3d 20  );..  if( rc == 
dffe0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0d 0a 20 20 20  NO_ERROR ){..   
dfff0 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46   *pSize = fsts3F
e0000 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0d  ileInfo.cbFile;.
e0010 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e0020 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65 7b  TE_OK;..  }else{
e0030 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
e0040 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
e0050 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
e0060 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61  ** Acquire a rea
e0070 64 65 72 20 6c 6f 63 6b 2e 0d 0a 2a 2f 0d 0a 73  der lock...*/..s
e0080 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61  tatic int getRea
e0090 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a  dLock( os2File *
e00a0 70 46 69 6c 65 20 29 7b 0d 0a 20 20 46 49 4c 45  pFile ){..  FILE
e00b0 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0d  LOCK  LockArea,.
e00c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
e00d0 6f 63 6b 41 72 65 61 3b 0d 0a 20 20 41 50 49 52  ockArea;..  APIR
e00e0 45 54 20 72 65 73 3b 0d 0a 20 20 6d 65 6d 73 65  ET res;..  memse
e00f0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
e0100 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
e0110 29 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  );..  memset(&Un
e0120 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
e0130 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
e0140 3b 0d 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  ;..  LockArea.lO
e0150 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
e0160 49 52 53 54 3b 0d 0a 20 20 4c 6f 63 6b 41 72 65  IRST;..  LockAre
e0170 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45  a.lRange = SHARE
e0180 44 5f 53 49 5a 45 3b 0d 0a 20 20 55 6e 6c 6f 63  D_SIZE;..  Unloc
e0190 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
e01a0 30 4c 3b 0d 0a 20 20 55 6e 6c 6f 63 6b 41 72 65  0L;..  UnlockAre
e01b0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0d 0a  a.lRange = 0L;..
e01c0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
e01d0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
e01e0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
e01f0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
e0200 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0d 0a  TIMEOUT, 1L );..
e0210 20 20 4f 53 54 52 41 43 45 28 28 20 22 47 45 54    OSTRACE(( "GET
e0220 52 45 41 44 4c 4f 43 4b 20 25 64 20 72 65 73 3d  READLOCK %d res=
e0230 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
e0240 20 72 65 73 20 29 29 3b 0d 0a 20 20 72 65 74 75   res ));..  retu
e0250 72 6e 20 72 65 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  rn res;..}..../*
e0260 0d 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64  ..** Undo a read
e0270 6c 6f 63 6b 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  lock..*/..static
e0280 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c   int unlockReadL
e0290 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64  ock( os2File *id
e02a0 20 29 7b 0d 0a 20 20 46 49 4c 45 4c 4f 43 4b 20   ){..  FILELOCK 
e02b0 20 4c 6f 63 6b 41 72 65 61 2c 0d 0a 20 20 20 20   LockArea,..    
e02c0 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
e02d0 65 61 3b 0d 0a 20 20 41 50 49 52 45 54 20 72 65  ea;..  APIRET re
e02e0 73 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f  s;..  memset(&Lo
e02f0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
e0300 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0d 0a 20  f(LockArea));.. 
e0310 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41   memset(&UnlockA
e0320 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55  rea, 0, sizeof(U
e0330 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0d 0a 20 20  nlockArea));..  
e0340 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
e0350 20 3d 20 30 4c 3b 0d 0a 20 20 4c 6f 63 6b 41 72   = 0L;..  LockAr
e0360 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0d  ea.lRange = 0L;.
e0370 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f  .  UnlockArea.lO
e0380 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
e0390 49 52 53 54 3b 0d 0a 20 20 55 6e 6c 6f 63 6b 41  IRST;..  UnlockA
e03a0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
e03b0 52 45 44 5f 53 49 5a 45 3b 0d 0a 20 20 72 65 73  RED_SIZE;..  res
e03c0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
e03d0 6b 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f  ks( id->h, &Unlo
e03e0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
e03f0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
e0400 20 31 4c 20 29 3b 0d 0a 20 20 4f 53 54 52 41 43   1L );..  OSTRAC
e0410 45 28 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 41 44  E(( "UNLOCK-READ
e0420 4c 4f 43 4b 20 66 69 6c 65 20 68 61 6e 64 6c 65  LOCK file handle
e0430 3d 25 64 20 72 65 73 3d 25 64 3f 5c 6e 22 2c 20  =%d res=%d?\n", 
e0440 69 64 2d 3e 68 2c 20 72 65 73 20 29 29 3b 0d 0a  id->h, res ));..
e0450 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0d 0a 7d    return res;..}
e0460 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63 6b 20  ..../*..** Lock 
e0470 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
e0480 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
e0490 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f   by parameter lo
e04a0 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0d 0a 2a 2a  cktype - one..**
e04b0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
e04c0 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 28  g:..**..**     (
e04d0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0d 0a  1) SHARED_LOCK..
e04e0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
e04f0 56 45 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20  VED_LOCK..**    
e0500 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
e0510 4b 0d 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  K..**     (4) EX
e0520 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0d 0a 2a 2a  CLUSIVE_LOCK..**
e0530 0d 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  ..** Sometimes w
e0540 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
e0550 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
e0560 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
e0570 74 61 74 65 73 0d 0a 2a 2a 20 61 72 65 20 69 6e  tates..** are in
e0580 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65  serted in betwee
e0590 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  n.  The locking 
e05a0 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e  might fail on on
e05b0 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0d 0a  e of the later..
e05c0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
e05d0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
e05e0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
e05f0 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
e0600 72 74 65 64 20 62 75 74 0d 0a 2a 2a 20 73 74 69  rted but..** sti
e0610 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
e0620 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
e0630 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
e0640 20 74 68 65 20 61 6c 6c 6f 77 65 64 0d 0a 2a 2a   the allowed..**
e0650 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64   transitions and
e0660 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e   the inserted in
e0670 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65  termediate state
e0680 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 55 4e  s:..**..**    UN
e0690 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
e06a0 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  ..**    SHARED -
e06b0 3e 20 52 45 53 45 52 56 45 44 0d 0a 2a 2a 20 20  > RESERVED..**  
e06c0 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
e06d0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
e06e0 56 45 0d 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  VE..**    RESERV
e06f0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
e0700 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a  -> EXCLUSIVE..**
e0710 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
e0720 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 0d 0a 2a 2a  XCLUSIVE..**..**
e0730 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
e0740 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
e0750 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73   a lock.  The os
e0760 32 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e  2Unlock() routin
e0770 65 0d 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c  e..** erases all
e0780 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20 61   locks at once a
e0790 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69 6d  nd returns us im
e07a0 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63  mediately to loc
e07b0 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0d 0a 2a  king level 0...*
e07c0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
e07d0 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68  ible to lower th
e07e0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
e07f0 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69  one step at a ti
e0800 6d 65 2e 20 20 59 6f 75 0d 0a 2a 2a 20 6d 75 73  me.  You..** mus
e0810 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f  t go straight to
e0820 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30   locking level 0
e0830 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
e0840 74 20 6f 73 32 4c 6f 63 6b 28 20 73 71 6c 69 74  t os2Lock( sqlit
e0850 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
e0860 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0d 0a 20 20   locktype ){..  
e0870 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e0880 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74  OK;       /* Ret
e0890 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  urn code from su
e08a0 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0d 0a 20 20  broutines */..  
e08b0 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f  APIRET res = NO_
e08c0 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73  ERROR;    /* Res
e08d0 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c  ult of an OS/2 l
e08e0 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0d 0a 20 20 69  ock call */..  i
e08f0 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20  nt newLocktype; 
e0900 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69        /* Set pFi
e0910 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20  le->locktype to 
e0920 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72  this value befor
e0930 65 20 65 78 69 74 69 6e 67 20 2a 2f 0d 0a 20 20  e exiting */..  
e0940 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f  int gotPendingLo
e0950 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69  ck = 0;/* True i
e0960 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20  f we acquired a 
e0970 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69  PENDING lock thi
e0980 73 20 74 69 6d 65 20 2a 2f 0d 0a 20 20 46 49 4c  s time */..  FIL
e0990 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
e09a0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e  ..            Un
e09b0 6c 6f 63 6b 41 72 65 61 3b 0d 0a 20 20 6f 73 32  lockArea;..  os2
e09c0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
e09d0 73 32 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 6d  s2File*)id;..  m
e09e0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
e09f0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
e0a00 72 65 61 29 29 3b 0d 0a 20 20 6d 65 6d 73 65 74  rea));..  memset
e0a10 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
e0a20 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
e0a30 65 61 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ea));..  assert(
e0a40 20 70 46 69 6c 65 21 3d 30 20 29 3b 0d 0a 20 20   pFile!=0 );..  
e0a50 4f 53 54 52 41 43 45 28 28 20 22 4c 4f 43 4b 20  OSTRACE(( "LOCK 
e0a60 25 64 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c  %d %d was %d\n",
e0a70 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
e0a80 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ype, pFile->lock
e0a90 74 79 70 65 20 29 29 3b 0d 0a 0d 0a 20 20 2f 2a  type ));....  /*
e0aa0 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   If there is alr
e0ab0 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74  eady a lock of t
e0ac0 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65  his type or more
e0ad0 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20   restrictive on 
e0ae0 74 68 65 0d 0a 20 20 2a 2a 20 6f 73 32 46 69 6c  the..  ** os2Fil
e0af0 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
e0b00 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64  on't use the end
e0b10 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
e0b20 2c 20 61 73 0d 0a 20 20 2a 2a 20 73 71 6c 69 74  , as..  ** sqlit
e0b30 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
e0b40 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c   hasn't been cal
e0b50 6c 65 64 20 79 65 74 2e 0d 0a 20 20 2a 2f 0d 0a  led yet...  */..
e0b60 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
e0b70 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  ktype>=locktype 
e0b80 29 7b 0d 0a 20 20 20 20 4f 53 54 52 41 43 45 28  ){..    OSTRACE(
e0b90 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 6f 6b  ( "LOCK %d %d ok
e0ba0 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c   (already held)\
e0bb0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
e0bc0 63 6b 74 79 70 65 20 29 29 3b 0d 0a 20 20 20 20  cktype ));..    
e0bd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e0be0 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d  ;..  }....  /* M
e0bf0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
e0c00 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
e0c10 20 63 6f 72 72 65 63 74 0d 0a 20 20 2a 2f 0d 0a   correct..  */..
e0c20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
e0c30 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
e0c40 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK || locktype==
e0c50 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a  SHARED_LOCK );..
e0c60 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
e0c70 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe!=PENDING_LOCK
e0c80 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6c   );..  assert( l
e0c90 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45  ocktype!=RESERVE
e0ca0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d  D_LOCK || pFile-
e0cb0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
e0cc0 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a 20 20 2f  D_LOCK );....  /
e0cd0 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49  * Lock the PENDI
e0ce0 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20  NG_LOCK byte if 
e0cf0 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69  we need to acqui
e0d00 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
e0d10 6b 20 6f 72 0d 0a 20 20 2a 2a 20 61 20 53 48 41  k or..  ** a SHA
e0d20 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65  RED lock.  If we
e0d30 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61   are acquiring a
e0d40 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
e0d50 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66  e acquisition of
e0d60 0d 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49  ..  ** the PENDI
e0d70 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20  NG_LOCK byte is 
e0d80 74 65 6d 70 6f 72 61 72 79 2e 0d 0a 20 20 2a 2f  temporary...  */
e0d90 0d 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20  ..  newLocktype 
e0da0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
e0db0 65 3b 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  e;..  if( pFile-
e0dc0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f  >locktype==NO_LO
e0dd0 43 4b 0d 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  CK..      || (lo
e0de0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
e0df0 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
e0e00 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52  >locktype==RESER
e0e10 56 45 44 5f 4c 4f 43 4b 29 0d 0a 20 20 29 7b 0d  VED_LOCK)..  ){.
e0e20 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
e0e30 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f  ffset = PENDING_
e0e40 42 59 54 45 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41  BYTE;..    LockA
e0e50 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
e0e60 0d 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ..    UnlockArea
e0e70 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0d 0a  .lOffset = 0L;..
e0e80 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
e0e90 52 61 6e 67 65 20 3d 20 30 4c 3b 0d 0a 0d 0a 20  Range = 0L;.... 
e0ea0 20 20 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65     /* wait longe
e0eb0 72 20 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45  r than LOCK_TIME
e0ec0 4f 55 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20  OUT here not to 
e0ed0 68 61 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74  have to try mult
e0ee0 69 70 6c 65 20 74 69 6d 65 73 20 2a 2f 0d 0a 20  iple times */.. 
e0ef0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46     res = DosSetF
e0f00 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d  ileLocks( pFile-
e0f10 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
e0f20 20 26 4c 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c   &LockArea, 100L
e0f30 2c 20 30 4c 20 29 3b 0d 0a 20 20 20 20 69 66 28  , 0L );..    if(
e0f40 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
e0f50 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 50 65   ){..      gotPe
e0f60 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b 0d 0a  ndingLock = 1;..
e0f70 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 20        OSTRACE(( 
e0f80 22 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67  "LOCK %d pending
e0f90 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 65   lock boolean se
e0fa0 74 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  t.  res=%d\n", p
e0fb0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 29 3b  File->h, res ));
e0fc0 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
e0fd0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73    /* Acquire a s
e0fe0 68 61 72 65 64 20 6c 6f 63 6b 0d 0a 20 20 2a 2f  hared lock..  */
e0ff0 0d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  ..  if( locktype
e1000 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
e1010 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
e1020 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
e1030 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
e1040 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20  ==NO_LOCK );..  
e1050 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c    res = getReadL
e1060 6f 63 6b 28 70 46 69 6c 65 29 3b 0d 0a 20 20 20  ock(pFile);..   
e1070 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45   if( res == NO_E
e1080 52 52 4f 52 20 29 7b 0d 0a 20 20 20 20 20 20 6e  RROR ){..      n
e1090 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ewLocktype = SHA
e10a0 52 45 44 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 7d  RED_LOCK;..    }
e10b0 0d 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 20  ..    OSTRACE(( 
e10c0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65  "LOCK %d acquire
e10d0 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65   shared lock. re
e10e0 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
e10f0 68 2c 20 72 65 73 20 29 29 3b 0d 0a 20 20 7d 0d  h, res ));..  }.
e1100 0a 0d 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  ...  /* Acquire 
e1110 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0d  a RESERVED lock.
e1120 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6c 6f 63  .  */..  if( loc
e1130 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
e1140 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e  LOCK && res == N
e1150 4f 5f 45 52 52 4f 52 20 29 7b 0d 0a 20 20 20 20  O_ERROR ){..    
e1160 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
e1170 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
e1180 4c 4f 43 4b 20 29 3b 0d 0a 20 20 20 20 4c 6f 63  LOCK );..    Loc
e1190 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
e11a0 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0d 0a  RESERVED_BYTE;..
e11b0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
e11c0 6e 67 65 20 3d 20 31 4c 3b 0d 0a 20 20 20 20 55  nge = 1L;..    U
e11d0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
e11e0 74 20 3d 20 30 4c 3b 0d 0a 20 20 20 20 55 6e 6c  t = 0L;..    Unl
e11f0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
e1200 20 30 4c 3b 0d 0a 20 20 20 20 72 65 73 20 3d 20   0L;..    res = 
e1210 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
e1220 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
e1230 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
e1240 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
e1250 20 30 4c 20 29 3b 0d 0a 20 20 20 20 69 66 28 20   0L );..    if( 
e1260 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
e1270 29 7b 0d 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63  ){..      newLoc
e1280 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44  ktype = RESERVED
e1290 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _LOCK;..    }.. 
e12a0 20 20 20 4f 53 54 52 41 43 45 28 28 20 22 4c 4f     OSTRACE(( "LO
e12b0 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 72 65  CK %d acquire re
e12c0 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73  served lock. res
e12d0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
e12e0 2c 20 72 65 73 20 29 29 3b 0d 0a 20 20 7d 0d 0a  , res ));..  }..
e12f0 0d 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
e1300 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0d 0a 20   PENDING lock.. 
e1310 20 2a 2f 0d 0a 20 20 69 66 28 20 6c 6f 63 6b 74   */..  if( lockt
e1320 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
e1330 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f  OCK && res == NO
e1340 5f 45 52 52 4f 52 20 29 7b 0d 0a 20 20 20 20 6e  _ERROR ){..    n
e1350 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e  ewLocktype = PEN
e1360 44 49 4e 47 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20  DING_LOCK;..    
e1370 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
e1380 20 30 3b 0d 0a 20 20 20 20 4f 53 54 52 41 43 45   0;..    OSTRACE
e1390 28 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75  (( "LOCK %d acqu
e13a0 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  ire pending lock
e13b0 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62  . pending lock b
e13c0 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22  oolean unset.\n"
e13d0 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
e13e0 20 20 70 46 69 6c 65 2d 3e 68 20 29 29 3b 0d 0a    pFile->h ));..
e13f0 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 63 71 75    }....  /* Acqu
e1400 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
e1410 20 6c 6f 63 6b 0d 0a 20 20 2a 2f 0d 0a 20 20 69   lock..  */..  i
e1420 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
e1430 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72  LUSIVE_LOCK && r
e1440 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
e1450 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
e1460 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
e1470 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a  SHARED_LOCK );..
e1480 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b      res = unlock
e1490 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
e14a0 0d 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 20  ..    OSTRACE(( 
e14b0 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64  "unreadlock = %d
e14c0 5c 6e 22 2c 20 72 65 73 20 29 29 3b 0d 0a 20 20  \n", res ));..  
e14d0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
e14e0 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
e14f0 54 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  T;..    LockArea
e1500 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
e1510 5f 53 49 5a 45 3b 0d 0a 20 20 20 20 55 6e 6c 6f  _SIZE;..    Unlo
e1520 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
e1530 20 30 4c 3b 0d 0a 20 20 20 20 55 6e 6c 6f 63 6b   0L;..    Unlock
e1540 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
e1550 3b 0d 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  ;..    res = Dos
e1560 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
e1570 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
e1580 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
e1590 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
e15a0 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 65 73   );..    if( res
e15b0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0d   == NO_ERROR ){.
e15c0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
e15d0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  pe = EXCLUSIVE_L
e15e0 4f 43 4b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  OCK;..    }else{
e15f0 0d 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28  ..      OSTRACE(
e1600 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f  ( "OS/2 error-co
e1610 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  de = %d\n", res 
e1620 29 29 3b 0d 0a 20 20 20 20 20 20 67 65 74 52 65  ));..      getRe
e1630 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0d 0a  adLock(pFile);..
e1640 20 20 20 20 7d 0d 0a 20 20 20 20 4f 53 54 52 41      }..    OSTRA
e1650 43 45 28 28 20 22 4c 4f 43 4b 20 25 64 20 61 63  CE(( "LOCK %d ac
e1660 71 75 69 72 65 20 65 78 63 6c 75 73 69 76 65 20  quire exclusive 
e1670 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22  lock.  res=%d\n"
e1680 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
e1690 29 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ));..  }....  /*
e16a0 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69   If we are holdi
e16b0 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ng a PENDING loc
e16c0 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20  k that ought to 
e16d0 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65  be released, the
e16e0 6e 0d 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20  n..  ** release 
e16f0 69 74 20 6e 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a 20  it now...  */.. 
e1700 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c   if( gotPendingL
e1710 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  ock && locktype=
e1720 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0d  =SHARED_LOCK ){.
e1730 0a 20 20 20 20 69 6e 74 20 72 3b 0d 0a 20 20 20  .    int r;..   
e1740 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
e1750 74 20 3d 20 30 4c 3b 0d 0a 20 20 20 20 4c 6f 63  t = 0L;..    Loc
e1760 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
e1770 4c 3b 0d 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  L;..    UnlockAr
e1780 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
e1790 44 49 4e 47 5f 42 59 54 45 3b 0d 0a 20 20 20 20  DING_BYTE;..    
e17a0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
e17b0 65 20 3d 20 31 4c 3b 0d 0a 20 20 20 20 72 20 3d  e = 1L;..    r =
e17c0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
e17d0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
e17e0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
e17f0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
e1800 2c 20 30 4c 20 29 3b 0d 0a 20 20 20 20 4f 53 54  , 0L );..    OST
e1810 52 41 43 45 28 28 20 22 4c 4f 43 4b 20 25 64 20  RACE(( "LOCK %d 
e1820 75 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e  unlocking pendin
e1830 67 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25  g/is shared. r=%
e1840 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
e1850 72 20 29 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  r ));..  }....  
e1860 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74  /* Update the st
e1870 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20  ate of the lock 
e1880 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20  has held in the 
e1890 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e18a0 74 68 65 6e 0d 0a 20 20 2a 2a 20 72 65 74 75 72  then..  ** retur
e18b0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
e18c0 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0d 0a  e result code...
e18d0 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72 65 73 20    */..  if( res 
e18e0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0d 0a  == NO_ERROR ){..
e18f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e1900 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  OK;..  }else{.. 
e1910 20 20 20 4f 53 54 52 41 43 45 28 28 20 22 4c 4f     OSTRACE(( "LO
e1920 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79  CK FAILED %d try
e1930 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67  ing for %d but g
e1940 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ot %d\n", pFile-
e1950 3e 68 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  >h,..           
e1960 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77     locktype, new
e1970 4c 6f 63 6b 74 79 70 65 20 29 29 3b 0d 0a 20 20  Locktype ));..  
e1980 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
e1990 53 59 3b 0d 0a 20 20 7d 0d 0a 20 20 70 46 69 6c  SY;..  }..  pFil
e19a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65  e->locktype = ne
e19b0 77 4c 6f 63 6b 74 79 70 65 3b 0d 0a 20 20 4f 53  wLocktype;..  OS
e19c0 54 52 41 43 45 28 28 20 22 4c 4f 43 4b 20 25 64  TRACE(( "LOCK %d
e19d0 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c   now %d\n", pFil
e19e0 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
e19f0 6b 74 79 70 65 20 29 29 3b 0d 0a 20 20 72 65 74  ktype ));..  ret
e1a00 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
e1a10 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
e1a20 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
e1a30 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
e1a40 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
e1a50 20 73 70 65 63 69 66 69 65 64 0d 0a 2a 2a 20 66   specified..** f
e1a60 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
e1a70 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
e1a80 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
e1a90 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e   is held, return
e1aa0 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f  ..** non-zero, o
e1ab0 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0d 0a  therwise zero...
e1ac0 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  */..static int o
e1ad0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  s2CheckReservedL
e1ae0 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ock( sqlite3_fil
e1af0 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 74  e *id, int *pOut
e1b00 20 29 7b 0d 0a 20 20 69 6e 74 20 72 20 3d 20 30   ){..  int r = 0
e1b10 3b 0d 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  ;..  os2File *pF
e1b20 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
e1b30 69 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  id;..  assert( p
e1b40 46 69 6c 65 21 3d 30 20 29 3b 0d 0a 20 20 69 66  File!=0 );..  if
e1b50 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
e1b60 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
e1b70 20 29 7b 0d 0a 20 20 20 20 72 20 3d 20 31 3b 0d   ){..    r = 1;.
e1b80 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 20 22  .    OSTRACE(( "
e1b90 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
e1ba0 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70  %d (local)\n", p
e1bb0 46 69 6c 65 2d 3e 68 2c 20 72 20 29 29 3b 0d 0a  File->h, r ));..
e1bc0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 46 49    }else{..    FI
e1bd0 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61  LELOCK  LockArea
e1be0 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
e1bf0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0d 0a 20 20   UnlockArea;..  
e1c00 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
e1c10 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 6d 65 6d  _ERROR;..    mem
e1c20 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
e1c30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
e1c40 61 29 29 3b 0d 0a 20 20 20 20 6d 65 6d 73 65 74  a));..    memset
e1c50 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
e1c60 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
e1c70 65 61 29 29 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41  ea));..    LockA
e1c80 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45  rea.lOffset = RE
e1c90 53 45 52 56 45 44 5f 42 59 54 45 3b 0d 0a 20 20  SERVED_BYTE;..  
e1ca0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
e1cb0 65 20 3d 20 31 4c 3b 0d 0a 20 20 20 20 55 6e 6c  e = 1L;..    Unl
e1cc0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
e1cd0 3d 20 30 4c 3b 0d 0a 20 20 20 20 55 6e 6c 6f 63  = 0L;..    Unloc
e1ce0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
e1cf0 4c 3b 0d 0a 20 20 20 20 72 63 20 3d 20 44 6f 73  L;..    rc = Dos
e1d00 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
e1d10 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
e1d20 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
e1d30 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
e1d40 20 29 3b 0d 0a 20 20 20 20 4f 53 54 52 41 43 45   );..    OSTRACE
e1d50 28 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  (( "TEST WR-LOCK
e1d60 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65   %d lock reserve
e1d70 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c  d byte rc=%d\n",
e1d80 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 29   pFile->h, rc ))
e1d90 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d  ;..    if( rc ==
e1da0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0d 0a 20 20   NO_ERROR ){..  
e1db0 20 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d      APIRET rcu =
e1dc0 20 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65   NO_ERROR; /* re
e1dd0 74 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e  turn code for un
e1de0 6c 6f 63 6b 69 6e 67 20 2a 2f 0d 0a 20 20 20 20  locking */..    
e1df0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
e1e00 65 74 20 3d 20 30 4c 3b 0d 0a 20 20 20 20 20 20  et = 0L;..      
e1e10 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
e1e20 3d 20 30 4c 3b 0d 0a 20 20 20 20 20 20 55 6e 6c  = 0L;..      Unl
e1e30 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
e1e40 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b  = RESERVED_BYTE;
e1e50 0d 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72  ..      UnlockAr
e1e60 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0d  ea.lRange = 1L;.
e1e70 0a 20 20 20 20 20 20 72 63 75 20 3d 20 44 6f 73  .      rcu = Dos
e1e80 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
e1e90 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
e1ea0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
e1eb0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
e1ec0 20 29 3b 0d 0a 20 20 20 20 20 20 4f 53 54 52 41   );..      OSTRA
e1ed0 43 45 28 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE(( "TEST WR-LO
e1ee0 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 72 65 73  CK %d unlock res
e1ef0 65 72 76 65 64 20 62 79 74 65 20 72 3d 25 64 5c  erved byte r=%d\
e1f00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
e1f10 75 20 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  u ));..    }..  
e1f20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e 4f    r = !(rc == NO
e1f30 5f 45 52 52 4f 52 29 3b 0d 0a 20 20 20 20 4f 53  _ERROR);..    OS
e1f40 54 52 41 43 45 28 28 20 22 54 45 53 54 20 57 52  TRACE(( "TEST WR
e1f50 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d  -LOCK %d %d (rem
e1f60 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ote)\n", pFile->
e1f70 68 2c 20 72 20 29 29 3b 0d 0a 20 20 7d 0d 0a 20  h, r ));..  }.. 
e1f80 20 2a 70 4f 75 74 20 3d 20 72 3b 0d 0a 20 20 72   *pOut = r;..  r
e1f90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e1fa0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f  ..}..../*..** Lo
e1fb0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
e1fc0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
e1fd0 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c  scriptor id to l
e1fe0 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79  ocktype.  lockty
e1ff0 70 65 0d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  pe..** must be e
e2000 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
e2010 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0d 0a 2a   SHARED_LOCK...*
e2020 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  *..** If the loc
e2030 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
e2040 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
e2050 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
e2060 6f 72 20 62 65 6c 6f 77 0d 0a 2a 2a 20 74 68 65  or below..** the
e2070 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
e2080 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
e2090 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
e20a0 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74 20 69 73  p...**..** It is
e20b0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
e20c0 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  r this routine t
e20d0 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 65  o fail if the se
e20e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0d 0a 2a  cond argument..*
e20f0 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49  * is NO_LOCK.  I
e2100 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
e2110 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f  ument is SHARED_
e2120 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72  LOCK then this r
e2130 6f 75 74 69 6e 65 0d 0a 2a 2a 20 6d 69 67 68 74  outine..** might
e2140 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
e2150 4f 45 52 52 3b 0d 0a 2a 2f 0d 0a 73 74 61 74 69  OERR;..*/..stati
e2160 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28  c int os2Unlock(
e2170 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
e2180 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20  d, int locktype 
e2190 29 7b 0d 0a 20 20 69 6e 74 20 74 79 70 65 3b 0d  ){..  int type;.
e21a0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
e21b0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
e21c0 3b 0d 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ;..  APIRET rc =
e21d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 41   SQLITE_OK;..  A
e21e0 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45  PIRET res = NO_E
e21f0 52 52 4f 52 3b 0d 0a 20 20 46 49 4c 45 4c 4f 43  RROR;..  FILELOC
e2200 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0d 0a 20 20  K  LockArea,..  
e2210 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b            Unlock
e2220 41 72 65 61 3b 0d 0a 20 20 6d 65 6d 73 65 74 28  Area;..  memset(
e2230 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  &LockArea, 0, si
e2240 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b  zeof(LockArea));
e2250 0d 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f  ..  memset(&Unlo
e2260 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
e2270 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0d  f(UnlockArea));.
e2280 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
e2290 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  !=0 );..  assert
e22a0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
e22b0 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 4f 53  ED_LOCK );..  OS
e22c0 54 52 41 43 45 28 28 20 22 55 4e 4c 4f 43 4b 20  TRACE(( "UNLOCK 
e22d0 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c  %d to %d was %d\
e22e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
e22f0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c  cktype, pFile->l
e2300 6f 63 6b 74 79 70 65 20 29 29 3b 0d 0a 20 20 74  ocktype ));..  t
e2310 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
e2320 6b 74 79 70 65 3b 0d 0a 20 20 69 66 28 20 74 79  ktype;..  if( ty
e2330 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
e2340 43 4b 20 29 7b 0d 0a 20 20 20 20 4c 6f 63 6b 41  CK ){..    LockA
e2350 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
e2360 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  ;..    LockArea.
e2370 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0d 0a 20 20  lRange = 0L;..  
e2380 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
e2390 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
e23a0 52 53 54 3b 0d 0a 20 20 20 20 55 6e 6c 6f 63 6b  RST;..    Unlock
e23b0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
e23c0 41 52 45 44 5f 53 49 5a 45 3b 0d 0a 20 20 20 20  ARED_SIZE;..    
e23d0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
e23e0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
e23f0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
e2400 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
e2410 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0d 0a 20 20  MEOUT, 0L );..  
e2420 20 20 4f 53 54 52 41 43 45 28 28 20 22 55 4e 4c    OSTRACE(( "UNL
e2430 4f 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65  OCK %d exclusive
e2440 20 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c   lock res=%d\n",
e2450 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
e2460 29 3b 0d 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b  );..    if( lock
e2470 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
e2480 4b 20 26 26 20 67 65 74 52 65 61 64 4c 6f 63 6b  K && getReadLock
e2490 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52  (pFile) != NO_ER
e24a0 52 4f 52 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  ROR ){..      /*
e24b0 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76   This should nev
e24c0 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73  er happen.  We s
e24d0 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
e24e0 61 62 6c 65 20 74 6f 0d 0a 20 20 20 20 20 20 2a  able to..      *
e24f0 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  * reacquire the 
e2500 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20  read lock */..  
e2510 20 20 20 20 4f 53 54 52 41 43 45 28 28 20 22 55      OSTRACE(( "U
e2520 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 67  NLOCK %d to %d g
e2530 65 74 52 65 61 64 4c 6f 63 6b 28 29 20 66 61 69  etReadLock() fai
e2540 6c 65 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  led\n", pFile->h
e2550 2c 20 6c 6f 63 6b 74 79 70 65 20 29 29 3b 0d 0a  , locktype ));..
e2560 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e2570 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0d  E_IOERR_UNLOCK;.
e2580 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69  .    }..  }..  i
e2590 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45  f( type>=RESERVE
e25a0 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 4c  D_LOCK ){..    L
e25b0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
e25c0 3d 20 30 4c 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41  = 0L;..    LockA
e25d0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
e25e0 0d 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ..    UnlockArea
e25f0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
e2600 56 45 44 5f 42 59 54 45 3b 0d 0a 20 20 20 20 55  VED_BYTE;..    U
e2610 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
e2620 20 3d 20 31 4c 3b 0d 0a 20 20 20 20 72 65 73 20   = 1L;..    res 
e2630 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
e2640 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
e2650 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
e2660 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
e2670 54 2c 20 30 4c 20 29 3b 0d 0a 20 20 20 20 4f 53  T, 0L );..    OS
e2680 54 52 41 43 45 28 28 20 22 55 4e 4c 4f 43 4b 20  TRACE(( "UNLOCK 
e2690 25 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d  %d reserved res=
e26a0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
e26b0 20 72 65 73 20 29 29 3b 0d 0a 20 20 7d 0d 0a 20   res ));..  }.. 
e26c0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e   if( locktype==N
e26d0 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d  O_LOCK && type>=
e26e0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a  SHARED_LOCK ){..
e26f0 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b      res = unlock
e2700 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
e2710 0d 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 20  ..    OSTRACE(( 
e2720 22 55 4e 4c 4f 43 4b 20 25 64 20 69 73 20 25 64  "UNLOCK %d is %d
e2730 20 77 61 6e 74 20 25 64 20 72 65 73 3d 25 64 5c   want %d res=%d\
e2740 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  n",..           
e2750 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 74 79 70     pFile->h, typ
e2760 65 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 72 65 73  e, locktype, res
e2770 20 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28   ));..  }..  if(
e2780 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c   type>=PENDING_L
e2790 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 4c 6f 63 6b  OCK ){..    Lock
e27a0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
e27b0 4c 3b 0d 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  L;..    LockArea
e27c0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0d 0a 20  .lRange = 0L;.. 
e27d0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
e27e0 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f  ffset = PENDING_
e27f0 42 59 54 45 3b 0d 0a 20 20 20 20 55 6e 6c 6f 63  BYTE;..    Unloc
e2800 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
e2810 4c 3b 0d 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  L;..    res = Do
e2820 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
e2830 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
e2840 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
e2850 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
e2860 4c 20 29 3b 0d 0a 20 20 20 20 4f 53 54 52 41 43  L );..    OSTRAC
e2870 45 28 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70  E(( "UNLOCK %d p
e2880 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22  ending res=%d\n"
e2890 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
e28a0 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 46 69 6c  ));..  }..  pFil
e28b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
e28c0 63 6b 74 79 70 65 3b 0d 0a 20 20 4f 53 54 52 41  cktype;..  OSTRA
e28d0 43 45 28 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE(( "UNLOCK %d 
e28e0 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  now %d\n", pFile
e28f0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
e2900 74 79 70 65 20 29 29 3b 0d 0a 20 20 72 65 74 75  type ));..  retu
e2910 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
e2920 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20  .** Control and 
e2930 71 75 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65  query of the ope
e2940 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0d 0a  n file handle...
e2950 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  */..static int o
e2960 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  s2FileControl(sq
e2970 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
e2980 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41  int op, void *pA
e2990 72 67 29 7b 0d 0a 20 20 73 77 69 74 63 68 28 20  rg){..  switch( 
e29a0 6f 70 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  op ){..    case 
e29b0 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
e29c0 4b 53 54 41 54 45 3a 20 7b 0d 0a 20 20 20 20 20  KSTATE: {..     
e29d0 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
e29e0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (os2File*)id)->l
e29f0 6f 63 6b 74 79 70 65 3b 0d 0a 20 20 20 20 20 20  ocktype;..      
e2a00 4f 53 54 52 41 43 45 28 28 20 22 46 43 4e 54 4c  OSTRACE(( "FCNTL
e2a10 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f  _LOCKSTATE %d lo
e2a20 63 6b 3d 25 64 5c 6e 22 2c 0d 0a 20 20 20 20 20  ck=%d\n",..     
e2a30 20 20 20 20 20 20 20 20 20 20 20 28 28 6f 73 32             ((os2
e2a40 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28  File*)id)->h, ((
e2a50 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
e2a60 63 6b 74 79 70 65 20 29 29 3b 0d 0a 20 20 20 20  cktype ));..    
e2a70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e2a80 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  OK;..    }..    
e2a90 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  case SQLITE_FCNT
e2aa0 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 3a 20 7b 0d  L_CHUNK_SIZE: {.
e2ab0 0a 20 20 20 20 20 20 28 28 6f 73 32 46 69 6c 65  .      ((os2File
e2ac0 2a 29 69 64 29 2d 3e 73 7a 43 68 75 6e 6b 20 3d  *)id)->szChunk =
e2ad0 20 2a 28 69 6e 74 2a 29 70 41 72 67 3b 0d 0a 20   *(int*)pArg;.. 
e2ae0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
e2af0 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
e2b00 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
e2b10 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 3a 20  CNTL_SIZE_HINT: 
e2b20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
e2b30 5f 69 6e 74 36 34 20 73 7a 20 3d 20 2a 28 73 71  _int64 sz = *(sq
e2b40 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 41 72  lite3_int64*)pAr
e2b50 67 3b 0d 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  g;..      Simula
e2b60 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
e2b70 31 29 3b 0d 0a 20 20 20 20 20 20 6f 73 32 54 72  1);..      os2Tr
e2b80 75 6e 63 61 74 65 28 69 64 2c 20 73 7a 29 3b 0d  uncate(id, sz);.
e2b90 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
e2ba0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
e2bb0 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
e2bc0 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
e2bd0 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
e2be0 45 5f 46 43 4e 54 4c 5f 53 59 4e 43 5f 4f 4d 49  E_FCNTL_SYNC_OMI
e2bf0 54 54 45 44 3a 20 7b 0d 0a 20 20 20 20 20 20 72  TTED: {..      r
e2c00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e2c10 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
e2c20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
e2c30 54 46 4f 55 4e 44 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  TFOUND;..}..../*
e2c40 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
e2c50 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62  sector size in b
e2c60 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65  ytes of the unde
e2c70 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76  rlying block dev
e2c80 69 63 65 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20  ice for..** the 
e2c90 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20  specified file. 
e2ca0 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61  This is almost a
e2cb0 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c  lways 512 bytes,
e2cc0 20 62 75 74 20 6d 61 79 20 62 65 0d 0a 2a 2a 20   but may be..** 
e2cd0 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20  larger for some 
e2ce0 64 65 76 69 63 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  devices...**..**
e2cf0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
e2d00 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
e2d10 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
e2d20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
e2d30 74 68 61 74 0d 0a 2a 2a 20 69 66 20 74 77 6f 20  that..** if two 
e2d40 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65  files are create
e2d50 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69  d in the same fi
e2d60 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74  le-system direct
e2d70 6f 72 79 20 28 69 2e 65 2e 0d 0a 2a 2a 20 61 20  ory (i.e...** a 
e2d80 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
e2d90 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
e2da0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
e2db0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0d  ize will be the.
e2dc0 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
e2dd0 68 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  h...*/..static i
e2de0 6e 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65  nt os2SectorSize
e2df0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
e2e00 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  d){..  UNUSED_PA
e2e10 52 41 4d 45 54 45 52 28 69 64 29 3b 0d 0a 20 20  RAMETER(id);..  
e2e20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45  return SQLITE_DE
e2e30 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a  FAULT_SECTOR_SIZ
e2e40 45 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  E;..}..../*..** 
e2e50 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20  Return a vector 
e2e60 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63  of device charac
e2e70 74 65 72 69 73 74 69 63 73 2e 0d 0a 2a 2f 0d 0a  teristics...*/..
e2e80 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65  static int os2De
e2e90 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
e2ea0 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
e2eb0 20 2a 69 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44   *id){..  UNUSED
e2ec0 5f 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0d  _PARAMETER(id);.
e2ed0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e2ee0 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41 42  _IOCAP_UNDELETAB
e2ef0 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 3b 0d 0a 7d  LE_WHEN_OPEN;..}
e2f00 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61  ....../*..** Cha
e2f10 72 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65  racter set conve
e2f20 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73  rsion objects us
e2f30 65 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e  ed by conversion
e2f40 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a 2f 0d 0a   routines...*/..
e2f50 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65  static UconvObje
e2f60 63 74 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c  ct ucUtf8 = NULL
e2f70 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74  ; /* convert bet
e2f80 77 65 65 6e 20 55 54 46 2d 38 20 61 6e 64 20 55  ween UTF-8 and U
e2f90 43 53 2d 32 20 2a 2f 0d 0a 73 74 61 74 69 63 20  CS-2 */..static 
e2fa0 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 6c 43  UconvObject uclC
e2fb0 70 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f  p = NULL;  /* co
e2fc0 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 6c 6f  nvert between lo
e2fd0 63 61 6c 20 63 6f 64 65 70 61 67 65 20 61 6e 64  cal codepage and
e2fe0 20 55 43 53 2d 32 20 2a 2f 0d 0a 0d 0a 2f 2a 0d   UCS-2 */..../*.
e2ff0 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
e3000 69 6f 6e 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  ion to initializ
e3010 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  e the conversion
e3020 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e   objects from an
e3030 64 20 74 6f 20 55 54 46 2d 38 2e 0d 0a 2a 2f 0d  d to UTF-8...*/.
e3040 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 69  .static void ini
e3050 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76  tUconvObjects( v
e3060 6f 69 64 20 29 7b 0d 0a 20 20 69 66 28 20 55 6e  oid ){..  if( Un
e3070 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65  iCreateUconvObje
e3080 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 55 74  ct( UTF_8, &ucUt
e3090 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43  f8 ) != ULS_SUCC
e30a0 45 53 53 20 29 0d 0a 20 20 20 20 75 63 55 74 66  ESS )..    ucUtf
e30b0 38 20 3d 20 4e 55 4c 4c 3b 0d 0a 20 20 69 66 20  8 = NULL;..  if 
e30c0 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76  ( UniCreateUconv
e30d0 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72  Object( (UniChar
e30e0 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c   *)L"@path=yes",
e30f0 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53   &uclCp ) != ULS
e3100 5f 53 55 43 43 45 53 53 20 29 0d 0a 20 20 20 20  _SUCCESS )..    
e3110 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0d 0a 7d  uclCp = NULL;..}
e3120 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65  ..../*..** Helpe
e3130 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72  r function to fr
e3140 65 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  ee the conversio
e3150 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61  n objects from a
e3160 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0d 0a 2a 2f  nd to UTF-8...*/
e3170 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  ..static void fr
e3180 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20  eeUconvObjects( 
e3190 76 6f 69 64 20 29 7b 0d 0a 20 20 69 66 20 28 20  void ){..  if ( 
e31a0 75 63 55 74 66 38 20 29 0d 0a 20 20 20 20 55 6e  ucUtf8 )..    Un
e31b0 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74  iFreeUconvObject
e31c0 28 20 75 63 55 74 66 38 20 29 3b 0d 0a 20 20 69  ( ucUtf8 );..  i
e31d0 66 20 28 20 75 63 6c 43 70 20 29 0d 0a 20 20 20  f ( uclCp )..   
e31e0 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a   UniFreeUconvObj
e31f0 65 63 74 28 20 75 63 6c 43 70 20 29 3b 0d 0a 20  ect( uclCp );.. 
e3200 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0d   ucUtf8 = NULL;.
e3210 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b  .  uclCp = NULL;
e3220 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65  ..}..../*..** He
e3230 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f  lper function to
e3240 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66   convert UTF-8 f
e3250 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61  ilenames to loca
e3260 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e  l OS/2 codepage.
e3270 0d 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65  ..** The two-ste
e3280 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74  p process: first
e3290 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63   convert the inc
e32a0 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69  oming UTF-8 stri
e32b0 6e 67 0d 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d  ng..** into UCS-
e32c0 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20  2 and then from 
e32d0 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72  UCS-2 to the cur
e32e0 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0d 0a  rent codepage...
e32f0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
e3300 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73  char pointer has
e3310 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0d 0a 2a   to be freed...*
e3320 2f 0d 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  /..static char *
e3330 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54  convertUtf8PathT
e3340 6f 43 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20  oCp( const char 
e3350 2a 69 6e 20 29 7b 0d 0a 20 20 55 6e 69 43 68 61  *in ){..  UniCha
e3360 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41  r tempPath[CCHMA
e3370 58 50 41 54 48 5d 3b 0d 0a 20 20 63 68 61 72 20  XPATH];..  char 
e3380 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63  *out = (char *)c
e3390 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54  alloc( CCHMAXPAT
e33a0 48 2c 20 31 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  H, 1 );....  if(
e33b0 20 21 6f 75 74 20 29 0d 0a 20 20 20 20 72 65 74   !out )..    ret
e33c0 75 72 6e 20 4e 55 4c 4c 3b 0d 0a 0d 0a 20 20 69  urn NULL;....  i
e33d0 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75  f( !ucUtf8 || !u
e33e0 63 6c 43 70 20 29 0d 0a 20 20 20 20 69 6e 69 74  clCp )..    init
e33f0 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0d  UconvObjects();.
e3400 0a 0d 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e  ...  /* determin
e3410 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  e string for the
e3420 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55   conversion of U
e3430 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50  TF-8 which is CP
e3440 31 32 30 38 20 2a 2f 0d 0a 20 20 69 66 28 20 55  1208 */..  if( U
e3450 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 74  niStrToUcs( ucUt
e3460 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63  f8, tempPath, (c
e3470 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58  har *)in, CCHMAX
e3480 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55  PATH ) != ULS_SU
e3490 43 43 45 53 53 20 29 0d 0a 20 20 20 20 72 65 74  CCESS )..    ret
e34a0 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63  urn out; /* if c
e34b0 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c  onversion fails,
e34c0 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74   return the empt
e34d0 79 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 0d 0a 20  y string */.... 
e34e0 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   /* conversion f
e34f0 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  or current codep
e3500 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65  age which can be
e3510 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20   used for paths 
e3520 2a 2f 0d 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d  */..  UniStrFrom
e3530 55 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c  Ucs( uclCp, out,
e3540 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41   tempPath, CCHMA
e3550 58 50 41 54 48 20 29 3b 0d 0a 0d 0a 20 20 72 65  XPATH );....  re
e3560 74 75 72 6e 20 6f 75 74 3b 0d 0a 7d 0d 0a 0d 0a  turn out;..}....
e3570 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  /*..** Helper fu
e3580 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72  nction to conver
e3590 74 20 66 69 6c 65 6e 61 6d 65 73 20 66 72 6f 6d  t filenames from
e35a0 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20   local codepage 
e35b0 74 6f 20 55 54 46 2d 38 2e 0d 0a 2a 2a 20 54 68  to UTF-8...** Th
e35c0 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65  e two-step proce
e35d0 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72  ss: first conver
e35e0 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 63  t the incoming c
e35f0 6f 64 65 70 61 67 65 2d 73 70 65 63 69 66 69 63  odepage-specific
e3600 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 74 6f  ..** string into
e3610 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65 6e 20   UCS-2 and then 
e3620 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 68  from UCS-2 to th
e3630 65 20 63 6f 64 65 70 61 67 65 20 6f 66 20 55 54  e codepage of UT
e3640 46 2d 38 2e 0d 0a 2a 2a 20 54 68 65 20 72 65 74  F-8...** The ret
e3650 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e 74  urned char point
e3660 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 65  er has to be fre
e3670 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ed...**..** This
e3680 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e   function is non
e3690 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62  -static to be ab
e36a0 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  le to use this i
e36b0 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0d 0a 2a  n shell.c and..*
e36c0 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63  * similar applic
e36d0 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65  ations that take
e36e0 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72   command line ar
e36f0 67 75 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 63 68  guments...*/..ch
e3700 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74  ar *convertCpPat
e3710 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63  hToUtf8( const c
e3720 68 61 72 20 2a 69 6e 20 29 7b 0d 0a 20 20 55 6e  har *in ){..  Un
e3730 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43  iChar tempPath[C
e3740 43 48 4d 41 58 50 41 54 48 5d 3b 0d 0a 20 20 63  CHMAXPATH];..  c
e3750 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72  har *out = (char
e3760 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41   *)calloc( CCHMA
e3770 58 50 41 54 48 2c 20 31 20 29 3b 0d 0a 0d 0a 20  XPATH, 1 );.... 
e3780 20 69 66 28 20 21 6f 75 74 20 29 0d 0a 20 20 20   if( !out )..   
e3790 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0d 0a 0d   return NULL;...
e37a0 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c  .  if( !ucUtf8 |
e37b0 7c 20 21 75 63 6c 43 70 20 29 0d 0a 20 20 20 20  | !uclCp )..    
e37c0 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
e37d0 28 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 63 6f 6e 76  ();....  /* conv
e37e0 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65  ersion for curre
e37f0 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68 69 63  nt codepage whic
e3800 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  h can be used fo
e3810 72 20 70 61 74 68 73 20 2a 2f 0d 0a 20 20 69 66  r paths */..  if
e3820 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75  ( UniStrToUcs( u
e3830 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 2c 20  clCp, tempPath, 
e3840 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d  (char *)in, CCHM
e3850 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f  AXPATH ) != ULS_
e3860 53 55 43 43 45 53 53 20 29 0d 0a 20 20 20 20 72  SUCCESS )..    r
e3870 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66  eturn out; /* if
e3880 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c   conversion fail
e3890 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d  s, return the em
e38a0 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 0d  pty string */...
e38b0 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20  .  /* determine 
e38c0 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63  string for the c
e38d0 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46  onversion of UTF
e38e0 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32  -8 which is CP12
e38f0 30 38 20 2a 2f 0d 0a 20 20 55 6e 69 53 74 72 46  08 */..  UniStrF
e3900 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20  romUcs( ucUtf8, 
e3910 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43  out, tempPath, C
e3920 43 48 4d 41 58 50 41 54 48 20 29 3b 0d 0a 0d 0a  CHMAXPATH );....
e3930 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0d 0a 7d    return out;..}
e3940 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  ......#ifndef SQ
e3950 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 0d  LITE_OMIT_WAL...
e3960 0a 2f 2a 0d 0a 2a 2a 20 55 73 65 20 6d 61 69 6e  ./*..** Use main
e3970 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
e3980 6f 72 20 69 6e 74 65 72 70 72 6f 63 65 73 73 20  or interprocess 
e3990 6c 6f 63 6b 69 6e 67 2e 20 49 66 20 75 6e 2d 64  locking. If un-d
e39a0 65 66 69 6e 65 64 0d 0a 2a 2a 20 61 20 73 65 70  efined..** a sep
e39b0 61 72 61 74 65 20 66 69 6c 65 20 69 73 20 63 72  arate file is cr
e39c0 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 70  eated for this p
e39d0 75 72 70 6f 73 65 2e 20 54 68 65 20 66 69 6c 65  urpose. The file
e39e0 20 77 69 6c 6c 20 62 65 0d 0a 2a 2a 20 75 73 65   will be..** use
e39f0 64 20 6f 6e 6c 79 20 74 6f 20 73 65 74 20 66 69  d only to set fi
e3a00 6c 65 20 6c 6f 63 6b 73 2e 20 54 68 65 72 65 20  le locks. There 
e3a10 77 69 6c 6c 20 62 65 20 6e 6f 20 64 61 74 61 20  will be no data 
e3a20 77 72 69 74 74 65 6e 20 74 6f 20 69 74 2e 0d 0a  written to it...
e3a30 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
e3a40 54 45 5f 4f 53 32 5f 4e 4f 5f 57 41 4c 5f 4c 4f  TE_OS2_NO_WAL_LO
e3a50 43 4b 5f 46 49 4c 45 20 20 20 20 20 0d 0a 0d 0a  CK_FILE     ....
e3a60 23 69 66 20 30 0d 0a 73 74 61 74 69 63 20 76 6f  #if 0..static vo
e3a70 69 64 20 5f 45 52 52 5f 54 52 41 43 45 28 20 63  id _ERR_TRACE( c
e3a80 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20  onst char *fmt, 
e3a90 2e 2e 2e 20 29 20 7b 0d 0a 20 20 76 61 5f 6c 69  ... ) {..  va_li
e3aa0 73 74 20 20 61 70 3b 0d 0a 20 20 76 61 5f 73 74  st  ap;..  va_st
e3ab0 61 72 74 28 61 70 2c 20 66 6d 74 29 3b 0d 0a 20  art(ap, fmt);.. 
e3ac0 20 76 66 70 72 69 6e 74 66 28 73 74 64 65 72 72   vfprintf(stderr
e3ad0 2c 20 66 6d 74 2c 20 61 70 29 3b 0d 0a 20 20 66  , fmt, ap);..  f
e3ae0 66 6c 75 73 68 28 73 74 64 65 72 72 29 3b 0d 0a  flush(stderr);..
e3af0 7d 0d 0a 23 64 65 66 69 6e 65 20 45 52 52 5f 54  }..#define ERR_T
e3b00 52 41 43 45 28 72 63 2c 20 6d 73 67 29 20 20 20  RACE(rc, msg)   
e3b10 20 20 20 20 20 5c 0d 0a 20 20 20 20 20 20 20 20       \..        
e3b20 69 66 28 20 28 72 63 29 20 21 3d 20 53 51 4c 49  if( (rc) != SQLI
e3b30 54 45 5f 4f 4b 20 29 20 5f 45 52 52 5f 54 52 41  TE_OK ) _ERR_TRA
e3b40 43 45 20 6d 73 67 3b 0d 0a 23 65 6c 73 65 0d 0a  CE msg;..#else..
e3b50 23 64 65 66 69 6e 65 20 45 52 52 5f 54 52 41 43  #define ERR_TRAC
e3b60 45 28 72 63 2c 20 6d 73 67 29 0d 0a 23 65 6e 64  E(rc, msg)..#end
e3b70 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 6c  if..../*..** Hel
e3b80 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  per functions to
e3b90 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69   obtain and reli
e3ba0 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61  nquish the globa
e3bb0 6c 20 6d 75 74 65 78 2e 20 54 68 65 0d 0a 2a 2a  l mutex. The..**
e3bc0 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73   global mutex is
e3bd0 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74   used to protect
e3be0 20 6f 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74 2e   os2ShmNodeList.
e3bf0 0d 0a 2a 2a 0d 0a 2a 2a 20 46 75 6e 63 74 69 6f  ..**..** Functio
e3c00 6e 20 6f 73 32 53 68 6d 4d 75 74 65 78 48 65 6c  n os2ShmMutexHel
e3c10 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61  d() is used to a
e3c20 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
e3c30 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0d 0a   global mutex ..
e3c40 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20  ** is held when 
e3c50 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66  required. This f
e3c60 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
e3c70 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
e3c80 61 73 73 65 72 74 28 29 20 0d 0a 2a 2a 20 73 74  assert() ..** st
e3c90 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0d 0a  atements. e.g...
e3ca0 2a 2a 0d 0a 2a 2a 20 20 20 6f 73 32 53 68 6d 45  **..**   os2ShmE
e3cb0 6e 74 65 72 4d 75 74 65 78 28 29 0d 0a 2a 2a 20  nterMutex()..** 
e3cc0 20 20 20 20 61 73 73 65 72 74 28 20 6f 73 32 53      assert( os2S
e3cd0 68 6d 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b  hmMutexHeld() );
e3ce0 0d 0a 2a 2a 20 20 20 6f 73 32 53 68 6d 4c 65 61  ..**   os2ShmLea
e3cf0 76 65 4d 75 74 65 78 28 29 0d 0a 2a 2f 0d 0a 73  veMutex()..*/..s
e3d00 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 53 68  tatic void os2Sh
e3d10 6d 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64  mEnterMutex(void
e3d20 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ){..  sqlite3_mu
e3d30 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
e3d40 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
e3d50 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
e3d60 4d 41 53 54 45 52 29 29 3b 0d 0a 7d 0d 0a 73 74  MASTER));..}..st
e3d70 61 74 69 63 20 76 6f 69 64 20 6f 73 32 53 68 6d  atic void os2Shm
e3d80 4c 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29  LeaveMutex(void)
e3d90 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  {..  sqlite3_mut
e3da0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
e3db0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
e3dc0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
e3dd0 41 53 54 45 52 29 29 3b 0d 0a 7d 0d 0a 23 69 66  ASTER));..}..#if
e3de0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
e3df0 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  ..static int os2
e3e00 53 68 6d 4d 75 74 65 78 48 65 6c 64 28 76 6f 69  ShmMutexHeld(voi
e3e10 64 29 20 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73  d) {..  return s
e3e20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
e3e30 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  d(sqlite3MutexAl
e3e40 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
e3e50 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
e3e60 3b 0d 0a 7d 0d 0a 69 6e 74 20 47 65 74 43 75 72  ;..}..int GetCur
e3e70 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 76 6f  rentProcessId(vo
e3e80 69 64 29 20 7b 0d 0a 20 20 50 50 49 42 20 70 69  id) {..  PPIB pi
e3e90 62 3b 0d 0a 20 20 44 6f 73 47 65 74 49 6e 66 6f  b;..  DosGetInfo
e3ea0 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 69  Blocks(NULL, &pi
e3eb0 62 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 69  b);..  return (i
e3ec0 6e 74 29 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69  nt)pib->pib_ulpi
e3ed0 64 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  d;..}..#endif...
e3ee0 0a 2f 2a 0d 0a 2a 2a 20 4f 62 6a 65 63 74 20 75  ./*..** Object u
e3ef0 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74  sed to represent
e3f00 20 61 20 74 68 65 20 73 68 61 72 65 64 20 6d 65   a the shared me
e3f10 6d 6f 72 79 20 61 72 65 61 20 66 6f 72 20 61 20  mory area for a 
e3f20 73 69 6e 67 6c 65 20 6c 6f 67 20 66 69 6c 65 2e  single log file.
e3f30 0d 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70  ..** When multip
e3f40 6c 65 20 74 68 72 65 61 64 73 20 61 6c 6c 20 72  le threads all r
e3f50 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 61 6d  eference the sam
e3f60 65 20 6c 6f 67 2d 73 75 6d 6d 61 72 79 2c 20 65  e log-summary, e
e3f70 61 63 68 20 74 68 72 65 61 64 20 68 61 73 0d 0a  ach thread has..
e3f80 2a 2a 20 69 74 73 20 6f 77 6e 20 6f 73 32 46 69  ** its own os2Fi
e3f90 6c 65 20 6f 62 6a 65 63 74 2c 20 62 75 74 20 74  le object, but t
e3fa0 68 65 79 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f  hey all point to
e3fb0 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e   a single instan
e3fc0 63 65 20 6f 66 20 74 68 69 73 20 0d 0a 2a 2a 20  ce of this ..** 
e3fd0 6f 62 6a 65 63 74 2e 20 20 49 6e 20 6f 74 68 65  object.  In othe
e3fe0 72 20 77 6f 72 64 73 2c 20 65 61 63 68 20 6c 6f  r words, each lo
e3ff0 67 2d 73 75 6d 6d 61 72 79 20 69 73 20 6f 70 65  g-summary is ope
e4000 6e 65 64 20 6f 6e 6c 79 20 6f 6e 63 65 20 70 65  ned only once pe
e4010 72 20 70 72 6f 63 65 73 73 2e 0d 0a 2a 2a 0d 0a  r process...**..
e4020 2a 2a 20 6f 73 32 53 68 6d 4d 75 74 65 78 48 65  ** os2ShmMutexHe
e4030 6c 64 28 29 20 6d 75 73 74 20 62 65 20 74 72 75  ld() must be tru
e4040 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  e when creating 
e4050 6f 72 20 64 65 73 74 72 6f 79 69 6e 67 0d 0a 2a  or destroying..*
e4060 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6f 72  * this object or
e4070 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 6f   while reading o
e4080 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 66 6f  r writing the fo
e4090 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0d  llowing fields:.
e40a0 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 6e 52 65  .**..**      nRe
e40b0 66 0d 0a 2a 2a 20 20 20 20 20 20 70 4e 65 78 74  f..**      pNext
e40c0 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f   ..**..** The fo
e40d0 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 20 61  llowing fields a
e40e0 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74  re read-only aft
e40f0 65 72 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73  er the object is
e4100 20 63 72 65 61 74 65 64 3a 0d 0a 2a 2a 20 0d 0a   created:..** ..
e4110 2a 2a 20 20 20 20 20 20 73 7a 52 65 67 69 6f 6e  **      szRegion
e4120 0d 0a 2a 2a 20 20 20 20 20 20 68 4c 6f 63 6b 46  ..**      hLockF
e4130 69 6c 65 0d 0a 2a 2a 20 20 20 20 20 20 73 68 6d  ile..**      shm
e4140 42 61 73 65 4e 61 6d 65 0d 0a 2a 2a 0d 0a 2a 2a  BaseName..**..**
e4150 20 45 69 74 68 65 72 20 6f 73 32 53 68 6d 4e 6f   Either os2ShmNo
e4160 64 65 2e 6d 75 74 65 78 20 6d 75 73 74 20 62 65  de.mutex must be
e4170 20 68 65 6c 64 20 6f 72 20 6f 73 32 53 68 6d 4e   held or os2ShmN
e4180 6f 64 65 2e 6e 52 65 66 3d 3d 30 20 61 6e 64 0d  ode.nRef==0 and.
e4190 0a 2a 2a 20 6f 73 32 53 68 6d 4d 75 74 65 78 48  .** os2ShmMutexH
e41a0 65 6c 64 28 29 20 69 73 20 74 72 75 65 20 77 68  eld() is true wh
e41b0 65 6e 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  en reading or wr
e41c0 69 74 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20  iting any other 
e41d0 66 69 65 6c 64 0d 0a 2a 2a 20 69 6e 20 74 68 69  field..** in thi
e41e0 73 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a  s structure...**
e41f0 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 6f 73 32  ..*/..struct os2
e4200 53 68 6d 4e 6f 64 65 20 7b 0d 0a 20 20 73 71 6c  ShmNode {..  sql
e4210 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
e4220 78 3b 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78  x;      /* Mutex
e4230 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20   to access this 
e4240 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 6f 73 32  object */..  os2
e4250 53 68 6d 4e 6f 64 65 20 2a 70 4e 65 78 74 3b 20  ShmNode *pNext; 
e4260 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
e4270 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f  in list of all o
e4280 73 32 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74  s2ShmNode object
e4290 73 20 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 73 7a  s */....  int sz
e42a0 52 65 67 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  Region;         
e42b0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
e42c0 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65  shared-memory re
e42d0 67 69 6f 6e 73 20 2a 2f 0d 0a 0d 0a 20 20 69 6e  gions */....  in
e42e0 74 20 6e 52 65 67 69 6f 6e 3b 20 20 20 20 20 20  t nRegion;      
e42f0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
e4300 20 6f 66 20 61 72 72 61 79 20 61 70 52 65 67 69   of array apRegi
e4310 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 2a  on */..  void **
e4320 61 70 52 65 67 69 6f 6e 3b 20 20 20 20 20 20 20  apRegion;       
e4330 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
e4340 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 68 61 72  pointers to shar
e4350 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e  ed-memory region
e4360 73 20 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 6e 52  s */....  int nR
e4370 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
e4380 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
e4390 66 20 6f 73 32 53 68 6d 4c 69 6e 6b 20 6f 62 6a  f os2ShmLink obj
e43a0 65 63 74 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ects pointing to
e43b0 20 74 68 69 73 20 2a 2f 0d 0a 20 20 6f 73 32 53   this */..  os2S
e43c0 68 6d 4c 69 6e 6b 20 2a 70 46 69 72 73 74 3b 20  hmLink *pFirst; 
e43d0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
e43e0 6f 73 32 53 68 6d 4c 69 6e 6b 20 6f 62 6a 65 63  os2ShmLink objec
e43f0 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  t pointing to th
e4400 69 73 20 2a 2f 0d 0a 0d 0a 20 20 48 46 49 4c 45  is */....  HFILE
e4410 20 68 4c 6f 63 6b 46 69 6c 65 3b 20 20 20 20 20   hLockFile;     
e4420 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 75 73        /* File us
e4430 65 64 20 66 6f 72 20 69 6e 74 65 72 2d 70 72 6f  ed for inter-pro
e4440 63 65 73 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 6b  cess memory lock
e4450 69 6e 67 20 2a 2f 0d 0a 20 20 63 68 61 72 20 73  ing */..  char s
e4460 68 6d 42 61 73 65 4e 61 6d 65 5b 31 5d 3b 20 20  hmBaseName[1];  
e4470 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
e4480 74 68 65 20 6d 65 6d 6f 72 79 20 6f 62 6a 65 63  the memory objec
e4490 74 20 21 21 21 20 6d 75 73 74 20 6c 61 73 74 20  t !!! must last 
e44a0 21 21 21 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a  !!! */..};......
e44b0 2f 2a 0d 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  /*..** Structure
e44c0 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   used internally
e44d0 20 62 79 20 74 68 69 73 20 56 46 53 20 74 6f 20   by this VFS to 
e44e0 72 65 63 6f 72 64 20 74 68 65 20 73 74 61 74 65  record the state
e44f0 20 6f 66 20 61 6e 0d 0a 2a 2a 20 6f 70 65 6e 20   of an..** open 
e4500 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f  shared memory co
e4510 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  nnection...**..*
e4520 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
e4530 66 69 65 6c 64 73 20 61 72 65 20 69 6e 69 74 69  fields are initi
e4540 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69 73  alized when this
e4550 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
e4560 65 64 20 61 6e 64 0d 0a 2a 2a 20 61 72 65 20 72  ed and..** are r
e4570 65 61 64 2d 6f 6e 6c 79 20 74 68 65 72 65 61 66  ead-only thereaf
e4580 74 65 72 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ter:..**..**    
e4590 6f 73 32 53 68 6d 2e 70 53 68 6d 4e 6f 64 65 0d  os2Shm.pShmNode.
e45a0 0a 2a 2a 20 20 20 20 6f 73 32 53 68 6d 2e 69 64  .**    os2Shm.id
e45b0 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 6f 74 68  ..**..** All oth
e45c0 65 72 20 66 69 65 6c 64 73 20 61 72 65 20 72 65  er fields are re
e45d0 61 64 2f 77 72 69 74 65 2e 20 20 54 68 65 20 6f  ad/write.  The o
e45e0 73 32 53 68 6d 2e 70 53 68 6d 4e 6f 64 65 2d 3e  s2Shm.pShmNode->
e45f0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
e4600 6c 64 0d 0a 2a 2a 20 77 68 69 6c 65 20 61 63 63  ld..** while acc
e4610 65 73 73 69 6e 67 20 61 6e 79 20 72 65 61 64 2f  essing any read/
e4620 77 72 69 74 65 20 66 69 65 6c 64 73 2e 0d 0a 2a  write fields...*
e4630 2f 0d 0a 73 74 72 75 63 74 20 6f 73 32 53 68 6d  /..struct os2Shm
e4640 4c 69 6e 6b 20 7b 0d 0a 20 20 6f 73 32 53 68 6d  Link {..  os2Shm
e4650 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 20  Node *pShmNode; 
e4660 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65       /* The unde
e4670 72 6c 79 69 6e 67 20 6f 73 32 53 68 6d 4e 6f 64  rlying os2ShmNod
e4680 65 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 6f  e object */..  o
e4690 73 32 53 68 6d 4c 69 6e 6b 20 2a 70 4e 65 78 74  s2ShmLink *pNext
e46a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ;         /* Nex
e46b0 74 20 6f 73 32 53 68 6d 20 77 69 74 68 20 74 68  t os2Shm with th
e46c0 65 20 73 61 6d 65 20 6f 73 32 53 68 6d 4e 6f 64  e same os2ShmNod
e46d0 65 20 2a 2f 0d 0a 20 20 75 33 32 20 73 68 61 72  e */..  u32 shar
e46e0 65 64 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  edMask;         
e46f0 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68     /* Mask of sh
e4700 61 72 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20  ared locks held 
e4710 2a 2f 0d 0a 20 20 75 33 32 20 65 78 63 6c 4d 61  */..  u32 exclMa
e4720 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
e4730 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63 6c   /* Mask of excl
e4740 75 73 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c 64  usive locks held
e4750 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
e4760 54 45 5f 44 45 42 55 47 0d 0a 20 20 75 38 20 69  TE_DEBUG..  u8 i
e4770 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
e4780 20 20 20 20 20 20 20 2f 2a 20 49 64 20 6f 66 20         /* Id of 
e4790 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  this connection 
e47a0 77 69 74 68 20 69 74 73 20 6f 73 32 53 68 6d 4e  with its os2ShmN
e47b0 6f 64 65 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a  ode */..#endif..
e47c0 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  };....../*..** A
e47d0 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 6f 66 20   global list of 
e47e0 61 6c 6c 20 6f 73 32 53 68 6d 4e 6f 64 65 20 6f  all os2ShmNode o
e47f0 62 6a 65 63 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  bjects...**..** 
e4800 54 68 65 20 6f 73 32 53 68 6d 4d 75 74 65 78 48  The os2ShmMutexH
e4810 65 6c 64 28 29 20 6d 75 73 74 20 62 65 20 74 72  eld() must be tr
e4820 75 65 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67  ue while reading
e4830 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 69 73   or writing this
e4840 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   list...*/..stat
e4850 69 63 20 6f 73 32 53 68 6d 4e 6f 64 65 20 2a 6f  ic os2ShmNode *o
e4860 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74 20 3d 20  s2ShmNodeList = 
e4870 4e 55 4c 4c 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  NULL;..../*..** 
e4880 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64 20 66  Constants used f
e4890 6f 72 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2f 0d 0a  or locking..*/..
e48a0 23 69 66 64 65 66 20 20 53 51 4c 49 54 45 5f 4f  #ifdef  SQLITE_O
e48b0 53 32 5f 4e 4f 5f 57 41 4c 5f 4c 4f 43 4b 5f 46  S2_NO_WAL_LOCK_F
e48c0 49 4c 45 0d 0a 23 64 65 66 69 6e 65 20 4f 53 32  ILE..#define OS2
e48d0 5f 53 48 4d 5f 42 41 53 45 20 20 20 28 50 45 4e  _SHM_BASE   (PEN
e48e0 44 49 4e 47 5f 42 59 54 45 20 2b 20 30 78 31 30  DING_BYTE + 0x10
e48f0 30 30 30 29 20 20 20 20 20 20 20 20 20 2f 2a 20  000)         /* 
e4900 66 69 72 73 74 20 6c 6f 63 6b 20 62 79 74 65 20  first lock byte 
e4910 2a 2f 0d 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69  */..#else..#defi
e4920 6e 65 20 4f 53 32 5f 53 48 4d 5f 42 41 53 45 20  ne OS2_SHM_BASE 
e4930 20 20 28 28 32 32 2b 53 51 4c 49 54 45 5f 53 48    ((22+SQLITE_SH
e4940 4d 5f 4e 4c 4f 43 4b 29 2a 34 29 20 20 20 20 20  M_NLOCK)*4)     
e4950 20 20 20 2f 2a 20 66 69 72 73 74 20 6c 6f 63 6b     /* first lock
e4960 20 62 79 74 65 20 2a 2f 0d 0a 23 65 6e 64 69 66   byte */..#endif
e4970 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f  ....#define OS2_
e4980 53 48 4d 5f 44 4d 53 20 20 20 20 28 4f 53 32 5f  SHM_DMS    (OS2_
e4990 53 48 4d 5f 42 41 53 45 2b 53 51 4c 49 54 45 5f  SHM_BASE+SQLITE_
e49a0 53 48 4d 5f 4e 4c 4f 43 4b 29 20 20 2f 2a 20 64  SHM_NLOCK)  /* d
e49b0 65 61 64 6d 61 6e 20 73 77 69 74 63 68 20 2a 2f  eadman switch */
e49c0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 70 70 6c 79  ..../*..** Apply
e49d0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20   advisory locks 
e49e0 66 6f 72 20 61 6c 6c 20 6e 20 62 79 74 65 73 20  for all n bytes 
e49f0 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 6f 66 73  beginning at ofs
e4a00 74 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  t...*/..#define 
e4a10 5f 53 48 4d 5f 55 4e 4c 43 4b 20 20 31 20 20 20  _SHM_UNLCK  1   
e4a20 2f 2a 20 6e 6f 20 6c 6f 63 6b 20 2a 2f 0d 0a 23  /* no lock */..#
e4a30 64 65 66 69 6e 65 20 5f 53 48 4d 5f 52 44 4c 43  define _SHM_RDLC
e4a40 4b 20 20 32 20 20 20 2f 2a 20 73 68 61 72 65 64  K  2   /* shared
e4a50 20 6c 6f 63 6b 2c 20 6e 6f 20 77 61 69 74 20 2a   lock, no wait *
e4a60 2f 0d 0a 23 64 65 66 69 6e 65 20 5f 53 48 4d 5f  /..#define _SHM_
e4a70 57 52 4c 43 4b 20 20 33 20 20 20 2f 2a 20 65 78  WRLCK  3   /* ex
e4a80 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 6e 6f 20  lusive lock, no 
e4a90 77 61 69 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  wait */..#define
e4aa0 20 5f 53 48 4d 5f 57 52 4c 43 4b 5f 57 41 49 54   _SHM_WRLCK_WAIT
e4ab0 20 34 20 2f 2a 20 65 78 63 6c 75 73 69 76 65 20   4 /* exclusive 
e4ac0 6c 6f 63 6b 2c 20 77 61 69 74 20 2a 2f 0d 0a 73  lock, wait */..s
e4ad0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 68 6d  tatic int os2Shm
e4ae0 53 79 73 74 65 6d 4c 6f 63 6b 28 0d 0a 20 20 6f  SystemLock(..  o
e4af0 73 32 53 68 6d 4e 6f 64 65 20 2a 70 4e 6f 64 65  s2ShmNode *pNode
e4b00 2c 20 20 20 20 2f 2a 20 41 70 70 6c 79 20 6c 6f  ,    /* Apply lo
e4b10 63 6b 73 20 74 6f 20 74 68 69 73 20 6f 70 65 6e  cks to this open
e4b20 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 73   shared-memory s
e4b30 65 67 6d 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74  egment */..  int
e4b40 20 6c 6f 63 6b 54 79 70 65 2c 20 20 20 20 20 20   lockType,      
e4b50 20 20 20 2f 2a 20 5f 53 48 4d 5f 55 4e 4c 43 4b     /* _SHM_UNLCK
e4b60 2c 20 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 5f 53  , _SHM_RDLCK, _S
e4b70 48 4d 5f 57 52 4c 43 4b 20 6f 72 20 5f 53 48 4d  HM_WRLCK or _SHM
e4b80 5f 57 52 4c 43 4b 5f 57 41 49 54 20 2a 2f 0d 0a  _WRLCK_WAIT */..
e4b90 20 20 69 6e 74 20 6f 66 73 74 2c 20 20 20 20 20    int ofst,     
e4ba0 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
e4bb0 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  t to first byte 
e4bc0 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2f 75 6e 6c  to be locked/unl
e4bd0 6f 63 6b 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  ocked */..  int 
e4be0 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20 20  nByte           
e4bf0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
e4c00 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 6f 72 20  ytes to lock or 
e4c10 75 6e 6c 6f 63 6b 20 2a 2f 0d 0a 29 7b 0d 0a 20  unlock */..){.. 
e4c20 20 41 50 49 52 45 54 20 72 63 3b 0d 0a 20 20 46   APIRET rc;..  F
e4c30 49 4c 45 4c 4f 43 4b 20 61 72 65 61 3b 0d 0a 20  ILELOCK area;.. 
e4c40 20 55 4c 4f 4e 47 20 6d 6f 64 65 2c 20 74 69 6d   ULONG mode, tim
e4c50 65 6f 75 74 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 63  eout;....  /* Ac
e4c60 63 65 73 73 20 74 6f 20 74 68 65 20 6f 73 32 53  cess to the os2S
e4c70 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20 69 73  hmNode object is
e4c80 20 73 65 72 69 61 6c 69 7a 65 64 20 62 79 20 74   serialized by t
e4c90 68 65 20 63 61 6c 6c 65 72 20 2a 2f 0d 0a 20 20  he caller */..  
e4ca0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
e4cb0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4e 6f 64 65  mutex_held(pNode
e4cc0 2d 3e 6d 75 74 65 78 29 20 7c 7c 20 70 4e 6f 64  ->mutex) || pNod
e4cd0 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0d 0a 0d  e->nRef==0 );...
e4ce0 0a 20 20 6d 6f 64 65 20 3d 20 31 3b 20 20 20 20  .  mode = 1;    
e4cf0 20 2f 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   /* shared lock 
e4d00 2a 2f 0d 0a 20 20 74 69 6d 65 6f 75 74 20 3d 20  */..  timeout = 
e4d10 30 3b 20 20 2f 2a 20 6e 6f 20 77 61 69 74 20 2a  0;  /* no wait *
e4d20 2f 0d 0a 20 20 61 72 65 61 2e 6c 4f 66 66 73 65  /..  area.lOffse
e4d30 74 20 3d 20 6f 66 73 74 3b 0d 0a 20 20 61 72 65  t = ofst;..  are
e4d40 61 2e 6c 52 61 6e 67 65 20 3d 20 6e 42 79 74 65  a.lRange = nByte
e4d50 3b 0d 0a 0d 0a 20 20 73 77 69 74 63 68 28 20 6c  ;....  switch( l
e4d60 6f 63 6b 54 79 70 65 20 29 20 7b 0d 0a 20 20 20  ockType ) {..   
e4d70 20 63 61 73 65 20 5f 53 48 4d 5f 57 52 4c 43 4b   case _SHM_WRLCK
e4d80 5f 57 41 49 54 3a 0d 0a 20 20 20 20 20 20 74 69  _WAIT:..      ti
e4d90 6d 65 6f 75 74 20 3d 20 28 55 4c 4f 4e 47 29 2d  meout = (ULONG)-
e4da0 31 3b 20 20 20 20 20 20 2f 2a 20 77 61 69 74 20  1;      /* wait 
e4db0 66 6f 72 65 76 65 72 20 2a 2f 0d 0a 20 20 20 20  forever */..    
e4dc0 63 61 73 65 20 5f 53 48 4d 5f 57 52 4c 43 4b 3a  case _SHM_WRLCK:
e4dd0 0d 0a 20 20 20 20 20 20 6d 6f 64 65 20 3d 20 30  ..      mode = 0
e4de0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
e4df0 20 20 2f 2a 20 65 78 63 6c 75 73 69 76 65 20 6c    /* exclusive l
e4e00 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65  ock */..    case
e4e10 20 5f 53 48 4d 5f 52 44 4c 43 4b 3a 0d 0a 20 20   _SHM_RDLCK:..  
e4e20 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46      rc = DosSetF
e4e30 69 6c 65 4c 6f 63 6b 73 28 70 4e 6f 64 65 2d 3e  ileLocks(pNode->
e4e40 68 4c 6f 63 6b 46 69 6c 65 2c 20 0d 0a 20 20 20  hLockFile, ..   
e4e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4e60 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 20 26 61          NULL, &a
e4e70 72 65 61 2c 20 74 69 6d 65 6f 75 74 2c 20 6d 6f  rea, timeout, mo
e4e80 64 65 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  de);..      brea
e4e90 6b 3b 0d 0a 20 20 20 20 2f 2a 20 63 61 73 65 20  k;..    /* case 
e4ea0 5f 53 48 4d 5f 55 4e 4c 43 4b 3a 20 2a 2f 0d 0a  _SHM_UNLCK: */..
e4eb0 20 20 20 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20      default:..  
e4ec0 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46      rc = DosSetF
e4ed0 69 6c 65 4c 6f 63 6b 73 28 70 4e 6f 64 65 2d 3e  ileLocks(pNode->
e4ee0 68 4c 6f 63 6b 46 69 6c 65 2c 20 0d 0a 20 20 20  hLockFile, ..   
e4ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4f00 20 20 20 20 20 20 20 20 26 61 72 65 61 2c 20 4e          &area, N
e4f10 55 4c 4c 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  ULL, 0, 0);..   
e4f20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a     break;..  }..
e4f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4f40 20 20 20 20 20 20 20 20 20 20 0d 0a 20 20 4f 53            ..  OS
e4f50 54 52 41 43 45 28 28 22 53 48 4d 2d 4c 4f 43 4b  TRACE(("SHM-LOCK
e4f60 20 25 64 20 25 73 20 25 73 20 30 78 25 30 38 6c   %d %s %s 0x%08l
e4f70 78 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20 20 20  x\n", ..        
e4f80 20 20 20 70 4e 6f 64 65 2d 3e 68 4c 6f 63 6b 46     pNode->hLockF
e4f90 69 6c 65 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  ile,..          
e4fa0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
e4fb0 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
e4fc0 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  ,..           lo
e4fd0 63 6b 54 79 70 65 3d 3d 5f 53 48 4d 5f 55 4e 4c  ckType==_SHM_UNL
e4fe0 43 4b 20 3f 20 22 55 6e 6c 6f 63 6b 22 20 3a 20  CK ? "Unlock" : 
e4ff0 22 4c 6f 63 6b 22 2c 0d 0a 20 20 20 20 20 20 20  "Lock",..       
e5000 20 20 20 20 72 63 29 29 3b 0d 0a 0d 0a 20 20 45      rc));....  E
e5010 52 52 5f 54 52 41 43 45 28 72 63 2c 20 28 22 6f  RR_TRACE(rc, ("o
e5020 73 32 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 3a  s2ShmSystemLock:
e5030 20 25 64 20 25 73 5c 6e 22 2c 20 72 63 2c 20 70   %d %s\n", rc, p
e5040 4e 6f 64 65 2d 3e 73 68 6d 42 61 73 65 4e 61 6d  Node->shmBaseNam
e5050 65 29 29 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20  e))....  return 
e5060 28 20 72 63 20 3d 3d 20 30 20 29 20 3f 20 20 53  ( rc == 0 ) ?  S
e5070 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
e5080 45 5f 42 55 53 59 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  E_BUSY;..}..../*
e5090 0d 0a 2a 2a 20 46 69 6e 64 20 61 6e 20 6f 73 32  ..** Find an os2
e50a0 53 68 6d 4e 6f 64 65 20 69 6e 20 67 6c 6f 62 61  ShmNode in globa
e50b0 6c 20 6c 69 73 74 20 6f 72 20 61 6c 6c 6f 63 61  l list or alloca
e50c0 74 65 20 61 20 6e 65 77 20 6f 6e 65 2c 20 69 66  te a new one, if
e50d0 20 6e 6f 74 20 66 6f 75 6e 64 2e 0d 0a 2a 2a 0d   not found...**.
e50e0 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20  .** This is not 
e50f0 61 20 56 46 53 20 73 68 61 72 65 64 2d 6d 65 6d  a VFS shared-mem
e5100 6f 72 79 20 6d 65 74 68 6f 64 3b 20 69 74 20 69  ory method; it i
e5110 73 20 61 20 75 74 69 6c 69 74 79 20 66 75 6e 63  s a utility func
e5120 74 69 6f 6e 20 63 61 6c 6c 65 64 0d 0a 2a 2a 20  tion called..** 
e5130 62 79 20 56 46 53 20 73 68 61 72 65 64 2d 6d 65  by VFS shared-me
e5140 6d 6f 72 79 20 6d 65 74 68 6f 64 73 2e 0d 0a 2a  mory methods...*
e5150 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  /..static int os
e5160 32 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72  2OpenSharedMemor
e5170 79 28 20 6f 73 32 46 69 6c 65 20 2a 66 64 2c 20  y( os2File *fd, 
e5180 69 6e 74 20 73 7a 52 65 67 69 6f 6e 20 29 20 7b  int szRegion ) {
e5190 0d 0a 20 20 6f 73 32 53 68 6d 4c 69 6e 6b 20 2a  ..  os2ShmLink *
e51a0 70 4c 69 6e 6b 3b 0d 0a 20 20 6f 73 32 53 68 6d  pLink;..  os2Shm
e51b0 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0d 0a 20 20  Node *pNode;..  
e51c0 69 6e 74 20 63 62 53 68 6d 4e 61 6d 65 2c 20 72  int cbShmName, r
e51d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
e51e0 20 20 63 68 61 72 20 73 68 6d 4e 61 6d 65 5b 43    char shmName[C
e51f0 43 48 4d 41 58 50 41 54 48 20 2b 20 33 30 5d 3b  CHMAXPATH + 30];
e5200 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
e5210 5f 4f 53 32 5f 4e 4f 5f 57 41 4c 5f 4c 4f 43 4b  _OS2_NO_WAL_LOCK
e5220 5f 46 49 4c 45 0d 0a 20 20 55 4c 4f 4e 47 20 61  _FILE..  ULONG a
e5230 63 74 69 6f 6e 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ction;..#endif..
e5240 20 20 0d 0a 20 20 2f 2a 20 57 65 20 6e 65 65 64    ..  /* We need
e5250 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c   some additional
e5260 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e   space at the en
e5270 64 20 74 6f 20 61 70 70 65 6e 64 20 74 68 65 20  d to append the 
e5280 72 65 67 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f  region number */
e5290 0d 0a 20 20 63 62 53 68 6d 4e 61 6d 65 20 3d 20  ..  cbShmName = 
e52a0 73 70 72 69 6e 74 66 28 73 68 6d 4e 61 6d 65 2c  sprintf(shmName,
e52b0 20 22 5c 5c 53 48 41 52 45 4d 45 4d 5c 5c 25 73   "\\SHAREMEM\\%s
e52c0 22 2c 20 66 64 2d 3e 7a 46 75 6c 6c 50 61 74 68  ", fd->zFullPath
e52d0 43 70 20 29 3b 0d 0a 20 20 69 66 28 20 63 62 53  Cp );..  if( cbS
e52e0 68 6d 4e 61 6d 65 20 3e 3d 20 43 43 48 4d 41 58  hmName >= CCHMAX
e52f0 50 41 54 48 2d 38 20 29 0d 0a 20 20 20 20 72 65  PATH-8 )..    re
e5300 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
e5310 52 5f 53 48 4d 4f 50 45 4e 3b 20 0d 0a 0d 0a 20  R_SHMOPEN; .... 
e5320 20 2f 2a 20 52 65 70 6c 61 63 65 20 63 6f 6c 6f   /* Replace colo
e5330 6e 20 69 6e 20 66 69 6c 65 20 6e 61 6d 65 20 74  n in file name t
e5340 6f 20 66 6f 72 6d 20 61 20 76 61 6c 69 64 20 73  o form a valid s
e5350 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 6e 61 6d  hared memory nam
e5360 65 20 2a 2f 0d 0a 20 20 73 68 6d 4e 61 6d 65 5b  e */..  shmName[
e5370 31 30 2b 31 5d 20 3d 20 27 21 27 3b 0d 0a 0d 0a  10+1] = '!';....
e5380 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6c 69    /* Allocate li
e5390 6e 6b 20 6f 62 6a 65 63 74 20 28 77 65 20 66 72  nk object (we fr
e53a0 65 65 20 69 74 20 6c 61 74 65 72 20 69 6e 20 63  ee it later in c
e53b0 61 73 65 20 6f 66 20 66 61 69 6c 75 72 65 29 20  ase of failure) 
e53c0 2a 2f 0d 0a 20 20 70 4c 69 6e 6b 20 3d 20 73 71  */..  pLink = sq
e53d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
e53e0 7a 65 6f 66 28 2a 70 4c 69 6e 6b 29 20 29 3b 0d  zeof(*pLink) );.
e53f0 0a 20 20 69 66 28 20 21 70 4c 69 6e 6b 20 29 0d  .  if( !pLink ).
e5400 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e5410 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 0d 0a 20 20 2f  TE_NOMEM;....  /
e5420 2a 20 41 63 63 65 73 73 20 6e 6f 64 65 20 6c 69  * Access node li
e5430 73 74 20 2a 2f 0d 0a 20 20 6f 73 32 53 68 6d 45  st */..  os2ShmE
e5440 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 0d 0a  nterMutex();....
e5450 20 20 2f 2a 20 46 69 6e 64 20 6e 6f 64 65 20 62    /* Find node b
e5460 79 20 69 74 27 73 20 73 68 61 72 65 64 20 6d 65  y it's shared me
e5470 6d 6f 72 79 20 62 61 73 65 20 6e 61 6d 65 20 2a  mory base name *
e5480 2f 0d 0a 20 20 66 6f 72 28 20 70 4e 6f 64 65 20  /..  for( pNode 
e5490 3d 20 6f 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74  = os2ShmNodeList
e54a0 3b 20 0d 0a 20 20 20 20 20 20 20 70 4e 6f 64 65  ; ..       pNode
e54b0 20 26 26 20 73 74 72 69 63 6d 70 28 73 68 6d 4e   && stricmp(shmN
e54c0 61 6d 65 2c 20 70 4e 6f 64 65 2d 3e 73 68 6d 42  ame, pNode->shmB
e54d0 61 73 65 4e 61 6d 65 29 20 21 3d 20 30 3b 20 0d  aseName) != 0; .
e54e0 0a 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d 20  .       pNode = 
e54f0 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 29 20 20  pNode->pNext )  
e5500 20 3b 0d 0a 0d 0a 20 20 2f 2a 20 4e 6f 74 20 66   ;....  /* Not f
e5510 6f 75 6e 64 3a 20 61 6c 6c 6f 63 61 74 65 20 61  ound: allocate a
e5520 20 6e 65 77 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20   new node */..  
e5530 69 66 28 20 21 70 4e 6f 64 65 20 29 20 7b 0d 0a  if( !pNode ) {..
e5540 20 20 20 20 70 4e 6f 64 65 20 3d 20 73 71 6c 69      pNode = sqli
e5550 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
e5560 6f 66 28 2a 70 4e 6f 64 65 29 20 2b 20 63 62 53  of(*pNode) + cbS
e5570 68 6d 4e 61 6d 65 20 29 3b 0d 0a 20 20 20 20 69  hmName );..    i
e5580 66 28 20 70 4e 6f 64 65 20 29 20 7b 0d 0a 20 20  f( pNode ) {..  
e5590 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65      memset(pNode
e55a0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 6f  , 0, sizeof(*pNo
e55b0 64 65 29 20 29 3b 0d 0a 20 20 20 20 20 20 70 4e  de) );..      pN
e55c0 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e 20 3d 20  ode->szRegion = 
e55d0 73 7a 52 65 67 69 6f 6e 3b 0d 0a 20 20 20 20 20  szRegion;..     
e55e0 20 70 4e 6f 64 65 2d 3e 68 4c 6f 63 6b 46 69 6c   pNode->hLockFil
e55f0 65 20 3d 20 28 48 46 49 4c 45 29 2d 31 3b 20 20  e = (HFILE)-1;  
e5600 20 20 20 20 0d 0a 20 20 20 20 20 20 73 74 72 63      ..      strc
e5610 70 79 28 70 4e 6f 64 65 2d 3e 73 68 6d 42 61 73  py(pNode->shmBas
e5620 65 4e 61 6d 65 2c 20 73 68 6d 4e 61 6d 65 29 3b  eName, shmName);
e5630 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
e5640 45 5f 4f 53 32 5f 4e 4f 5f 57 41 4c 5f 4c 4f 43  E_OS2_NO_WAL_LOC
e5650 4b 5f 46 49 4c 45 0d 0a 20 20 20 20 20 20 69 66  K_FILE..      if
e5660 28 20 44 6f 73 44 75 70 48 61 6e 64 6c 65 28 66  ( DosDupHandle(f
e5670 64 2d 3e 68 2c 20 26 70 4e 6f 64 65 2d 3e 68 4c  d->h, &pNode->hL
e5680 6f 63 6b 46 69 6c 65 29 20 21 3d 20 30 20 29 20  ockFile) != 0 ) 
e5690 7b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 20 20  {..#else..      
e56a0 73 70 72 69 6e 74 66 28 73 68 6d 4e 61 6d 65 2c  sprintf(shmName,
e56b0 20 22 25 73 2d 6c 63 6b 22 2c 20 66 64 2d 3e 7a   "%s-lck", fd->z
e56c0 46 75 6c 6c 50 61 74 68 43 70 29 3b 0d 0a 20 20  FullPathCp);..  
e56d0 20 20 20 20 69 66 28 20 44 6f 73 4f 70 65 6e 28      if( DosOpen(
e56e0 28 50 53 5a 29 73 68 6d 4e 61 6d 65 2c 20 26 70  (PSZ)shmName, &p
e56f0 4e 6f 64 65 2d 3e 68 4c 6f 63 6b 46 69 6c 65 2c  Node->hLockFile,
e5700 20 26 61 63 74 69 6f 6e 2c 20 30 2c 20 46 49 4c   &action, 0, FIL
e5710 45 5f 4e 4f 52 4d 41 4c 2c 20 0d 0a 20 20 20 20  E_NORMAL, ..    
e5720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
e5730 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49  EN_ACTION_OPEN_I
e5740 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f  F_EXISTS | OPEN_
e5750 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46  ACTION_CREATE_IF
e5760 5f 4e 45 57 2c 0d 0a 20 20 20 20 20 20 20 20 20  _NEW,..         
e5770 20 20 20 20 20 20 20 20 20 4f 50 45 4e 5f 41 43           OPEN_AC
e5780 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 7c  CESS_READWRITE |
e5790 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59   OPEN_SHARE_DENY
e57a0 4e 4f 4e 45 20 7c 20 0d 0a 20 20 20 20 20 20 20  NONE | ..       
e57b0 20 20 20 20 20 20 20 20 20 20 20 4f 50 45 4e 5f             OPEN_
e57c0 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 49 54 20  FLAGS_NOINHERIT 
e57d0 7c 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41 49  | OPEN_FLAGS_FAI
e57e0 4c 5f 4f 4e 5f 45 52 52 4f 52 2c 0d 0a 20 20 20  L_ON_ERROR,..   
e57f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
e5800 55 4c 4c 29 20 21 3d 20 30 20 29 20 7b 0d 0a 23  ULL) != 0 ) {..#
e5810 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 20 20 73  endif..        s
e5820 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
e5830 65 29 3b 20 20 0d 0a 20 20 20 20 20 20 20 20 72  e);  ..        r
e5840 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e5850 3b 0d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  ;..      } else 
e5860 7b 0d 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65  {..        pNode
e5870 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  ->mutex = sqlite
e5880 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
e5890 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29  LITE_MUTEX_FAST)
e58a0 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ;..        if( !
e58b0 70 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 29 20 7b  pNode->mutex ) {
e58c0 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
e58d0 74 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b  te3_free(pNode);
e58e0 20 20 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63    ..          rc
e58f0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e5900 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
e5910 20 20 20 7d 20 20 20 0d 0a 20 20 20 20 7d 20 65     }   ..    } e
e5920 6c 73 65 20 7b 0d 0a 20 20 20 20 20 20 72 63 20  lse {..      rc 
e5930 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
e5940 0a 20 20 20 20 7d 0d 0a 20 20 20 20 0d 0a 20 20  .    }..    ..  
e5950 20 20 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49    if( rc == SQLI
e5960 54 45 5f 4f 4b 20 29 20 7b 0d 0a 20 20 20 20 20  TE_OK ) {..     
e5970 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20   pNode->pNext = 
e5980 6f 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74 3b 0d  os2ShmNodeList;.
e5990 0a 20 20 20 20 20 20 6f 73 32 53 68 6d 4e 6f 64  .      os2ShmNod
e59a0 65 4c 69 73 74 20 3d 20 70 4e 6f 64 65 3b 0d 0a  eList = pNode;..
e59b0 20 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20      } else {..  
e59c0 20 20 20 20 70 4e 6f 64 65 20 3d 20 4e 55 4c 4c      pNode = NULL
e59d0 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 20 65 6c  ;..    }..  } el
e59e0 73 65 20 69 66 28 20 70 4e 6f 64 65 2d 3e 73 7a  se if( pNode->sz
e59f0 52 65 67 69 6f 6e 20 21 3d 20 73 7a 52 65 67 69  Region != szRegi
e5a00 6f 6e 20 29 20 7b 0d 0a 20 20 20 20 72 63 20 3d  on ) {..    rc =
e5a10 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
e5a20 4d 53 49 5a 45 3b 0d 0a 20 20 20 20 70 4e 6f 64  MSIZE;..    pNod
e5a30 65 20 3d 20 4e 55 4c 4c 3b 0d 0a 20 20 7d 0d 0a  e = NULL;..  }..
e5a40 0d 0a 20 20 69 66 28 20 70 4e 6f 64 65 20 29 20  ..  if( pNode ) 
e5a50 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  {..    sqlite3_m
e5a60 75 74 65 78 5f 65 6e 74 65 72 28 70 4e 6f 64 65  utex_enter(pNode
e5a70 2d 3e 6d 75 74 65 78 29 3b 0d 0a 0d 0a 20 20 20  ->mutex);....   
e5a80 20 6d 65 6d 73 65 74 28 70 4c 69 6e 6b 2c 20 30   memset(pLink, 0
e5a90 2c 20 73 69 7a 65 6f 66 28 2a 70 4c 69 6e 6b 29  , sizeof(*pLink)
e5aa0 29 3b 0d 0a 0d 0a 20 20 20 20 70 4c 69 6e 6b 2d  );....    pLink-
e5ab0 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 4e 6f 64  >pShmNode = pNod
e5ac0 65 3b 0d 0a 20 20 20 20 70 4c 69 6e 6b 2d 3e 70  e;..    pLink->p
e5ad0 4e 65 78 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 46  Next = pNode->pF
e5ae0 69 72 73 74 3b 0d 0a 20 20 20 20 70 4e 6f 64 65  irst;..    pNode
e5af0 2d 3e 70 46 69 72 73 74 20 3d 20 70 4c 69 6e 6b  ->pFirst = pLink
e5b00 3b 0d 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52  ;..    pNode->nR
e5b10 65 66 2b 2b 3b 0d 0a 0d 0a 20 20 20 20 66 64 2d  ef++;....    fd-
e5b20 3e 70 53 68 6d 4c 69 6e 6b 20 3d 20 70 4c 69 6e  >pShmLink = pLin
e5b30 6b 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65  k;....    sqlite
e5b40 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4e  3_mutex_leave(pN
e5b50 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  ode->mutex);..  
e5b60 20 20 0d 0a 20 20 7d 20 65 6c 73 65 20 7b 0d 0a    ..  } else {..
e5b70 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6f 63 63      /* Error occ
e5b80 75 72 65 64 2e 20 46 72 65 65 20 6f 75 72 20 6c  ured. Free our l
e5b90 69 6e 6b 20 6f 62 6a 65 63 74 2e 20 2a 2f 0d 0a  ink object. */..
e5ba0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
e5bb0 28 70 4c 69 6e 6b 29 3b 20 20 0d 0a 20 20 7d 0d  (pLink);  ..  }.
e5bc0 0a 0d 0a 20 20 6f 73 32 53 68 6d 4c 65 61 76 65  ...  os2ShmLeave
e5bd0 4d 75 74 65 78 28 29 3b 0d 0a 0d 0a 20 20 45 52  Mutex();....  ER
e5be0 52 5f 54 52 41 43 45 28 72 63 2c 20 28 22 6f 73  R_TRACE(rc, ("os
e5bf0 32 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72  2OpenSharedMemor
e5c00 79 3a 20 25 64 20 20 25 73 5c 6e 22 2c 20 72 63  y: %d  %s\n", rc
e5c10 2c 20 66 64 2d 3e 7a 46 75 6c 6c 50 61 74 68 43  , fd->zFullPathC
e5c20 70 29 29 20 20 0d 0a 20 20 0d 0a 20 20 72 65 74  p))  ..  ..  ret
e5c30 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
e5c40 0d 0a 2a 2a 20 50 75 72 67 65 20 74 68 65 20 6f  ..** Purge the o
e5c50 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74 20 6c 69  s2ShmNodeList li
e5c60 73 74 20 6f 66 20 61 6c 6c 20 65 6e 74 72 69 65  st of all entrie
e5c70 73 20 77 69 74 68 20 6e 52 65 66 3d 3d 30 2e 0d  s with nRef==0..
e5c80 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20  .**..** This is 
e5c90 6e 6f 74 20 61 20 56 46 53 20 73 68 61 72 65 64  not a VFS shared
e5ca0 2d 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 3b 20  -memory method; 
e5cb0 69 74 20 69 73 20 61 20 75 74 69 6c 69 74 79 20  it is a utility 
e5cc0 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 0d  function called.
e5cd0 0a 2a 2a 20 62 79 20 56 46 53 20 73 68 61 72 65  .** by VFS share
e5ce0 64 2d 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 73  d-memory methods
e5cf0 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
e5d00 69 64 20 6f 73 32 50 75 72 67 65 53 68 6d 4e 6f  id os2PurgeShmNo
e5d10 64 65 73 28 20 69 6e 74 20 64 65 6c 65 74 65 46  des( int deleteF
e5d20 6c 61 67 20 29 20 7b 0d 0a 20 20 6f 73 32 53 68  lag ) {..  os2Sh
e5d30 6d 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0d 0a 20  mNode *pNode;.. 
e5d40 20 6f 73 32 53 68 6d 4e 6f 64 65 20 2a 2a 70 70   os2ShmNode **pp
e5d50 4e 6f 64 65 3b 0d 0a 0d 0a 20 20 6f 73 32 53 68  Node;....  os2Sh
e5d60 6d 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a  mEnterMutex();..
e5d70 20 20 0d 0a 20 20 70 70 4e 6f 64 65 20 3d 20 26    ..  ppNode = &
e5d80 6f 73 32 53 68 6d 4e 6f 64 65 4c 69 73 74 3b 0d  os2ShmNodeList;.
e5d90 0a 0d 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 4e  ...  while( *ppN
e5da0 6f 64 65 20 29 20 7b 0d 0a 20 20 20 20 70 4e 6f  ode ) {..    pNo
e5db0 64 65 20 3d 20 2a 70 70 4e 6f 64 65 3b 0d 0a 0d  de = *ppNode;...
e5dc0 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  .    if( pNode->
e5dd0 6e 52 65 66 20 3d 3d 20 30 20 29 20 7b 0d 0a 20  nRef == 0 ) {.. 
e5de0 20 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70       *ppNode = p
e5df0 4e 6f 64 65 2d 3e 70 4e 65 78 74 3b 20 20 20 0d  Node->pNext;   .
e5e00 0a 20 20 20 20 20 0d 0a 20 20 20 20 20 20 69 66  .     ..      if
e5e10 28 20 70 4e 6f 64 65 2d 3e 61 70 52 65 67 69 6f  ( pNode->apRegio
e5e20 6e 20 29 20 7b 0d 0a 20 20 20 20 20 20 20 20 2f  n ) {..        /
e5e30 2a 20 50 72 65 76 65 6e 74 20 6f 74 68 65 72 20  * Prevent other 
e5e40 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 72  processes from r
e5e50 65 73 69 7a 69 6e 67 20 74 68 65 20 73 68 61 72  esizing the shar
e5e60 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20  ed memory */..  
e5e70 20 20 20 20 20 20 6f 73 32 53 68 6d 53 79 73 74        os2ShmSyst
e5e80 65 6d 4c 6f 63 6b 28 70 4e 6f 64 65 2c 20 5f 53  emLock(pNode, _S
e5e90 48 4d 5f 57 52 4c 43 4b 5f 57 41 49 54 2c 20 4f  HM_WRLCK_WAIT, O
e5ea0 53 32 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3b 0d  S2_SHM_DMS, 1);.
e5eb0 0a 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  ...        while
e5ec0 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e  ( pNode->nRegion
e5ed0 2d 2d 20 29 20 7b 0d 0a 23 69 66 64 65 66 20 53  -- ) {..#ifdef S
e5ee0 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 20  QLITE_DEBUG..   
e5ef0 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20         int rc = 
e5f00 0d 0a 23 65 6e 64 69 66 20 20 20 20 20 20 20 20  ..#endif        
e5f10 20 20 0d 0a 20 20 20 20 20 20 20 20 20 20 44 6f    ..          Do
e5f20 73 46 72 65 65 4d 65 6d 28 70 4e 6f 64 65 2d 3e  sFreeMem(pNode->
e5f30 61 70 52 65 67 69 6f 6e 5b 70 4e 6f 64 65 2d 3e  apRegion[pNode->
e5f40 6e 52 65 67 69 6f 6e 5d 29 3b 0d 0a 0d 0a 20 20  nRegion]);....  
e5f50 20 20 20 20 20 20 20 20 4f 53 54 52 41 43 45 28          OSTRACE(
e5f60 28 22 53 48 4d 2d 50 55 52 47 45 20 70 69 64 2d  ("SHM-PURGE pid-
e5f70 25 64 20 75 6e 6d 61 70 20 72 65 67 69 6f 6e 3d  %d unmap region=
e5f80 25 64 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20  %d %s\n",..     
e5f90 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
e5fa0 74 29 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63  t)GetCurrentProc
e5fb0 65 73 73 49 64 28 29 2c 20 70 4e 6f 64 65 2d 3e  essId(), pNode->
e5fc0 6e 52 65 67 69 6f 6e 2c 0d 0a 20 20 20 20 20 20  nRegion,..      
e5fd0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
e5fe0 3d 20 30 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  = 0 ? "ok" : "fa
e5ff0 69 6c 65 64 22 29 29 3b 0d 0a 20 20 20 20 20 20  iled"));..      
e6000 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 2f    }....        /
e6010 2a 20 41 6c 6c 6f 77 20 6f 74 68 65 72 20 70 72  * Allow other pr
e6020 6f 63 65 73 73 65 73 20 74 6f 20 72 65 73 69 7a  ocesses to resiz
e6030 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d  e the shared mem
e6040 6f 72 79 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ory */..        
e6050 6f 73 32 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b  os2ShmSystemLock
e6060 28 70 4e 6f 64 65 2c 20 5f 53 48 4d 5f 55 4e 4c  (pNode, _SHM_UNL
e6070 43 4b 2c 20 4f 53 32 5f 53 48 4d 5f 44 4d 53 2c  CK, OS2_SHM_DMS,
e6080 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20   1);....        
e6090 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f  sqlite3_free(pNo
e60a0 64 65 2d 3e 61 70 52 65 67 69 6f 6e 29 3b 0d 0a  de->apRegion);..
e60b0 20 20 20 20 20 20 7d 20 20 0d 0a 0d 0a 20 20 20        }  ....   
e60c0 20 20 20 44 6f 73 43 6c 6f 73 65 28 70 4e 6f 64     DosClose(pNod
e60d0 65 2d 3e 68 4c 6f 63 6b 46 69 6c 65 29 3b 0d 0a  e->hLockFile);..
e60e0 20 20 20 20 20 20 0d 0a 23 69 66 6e 64 65 66 20        ..#ifndef 
e60f0 53 51 4c 49 54 45 5f 4f 53 32 5f 4e 4f 5f 57 41  SQLITE_OS2_NO_WA
e6100 4c 5f 4c 4f 43 4b 5f 46 49 4c 45 0d 0a 20 20 20  L_LOCK_FILE..   
e6110 20 20 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61     if( deleteFla
e6120 67 20 29 20 7b 0d 0a 20 20 20 20 20 20 20 20 20  g ) {..         
e6130 63 68 61 72 20 66 69 6c 65 4e 61 6d 65 5b 43 43  char fileName[CC
e6140 48 4d 41 58 50 41 54 48 5d 3b 0d 0a 20 20 20 20  HMAXPATH];..    
e6150 20 20 20 20 20 2f 2a 20 53 6b 69 70 20 22 5c 5c       /* Skip "\\
e6160 53 48 41 52 45 4d 45 4d 5c 5c 22 20 2a 2f 0d 0a  SHAREMEM\\" */..
e6170 20 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66           sprintf
e6180 28 66 69 6c 65 4e 61 6d 65 2c 20 22 25 73 2d 6c  (fileName, "%s-l
e6190 63 6b 22 2c 20 70 4e 6f 64 65 2d 3e 73 68 6d 42  ck", pNode->shmB
e61a0 61 73 65 4e 61 6d 65 20 2b 20 31 30 29 3b 0d 0a  aseName + 10);..
e61b0 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 73 74           /* rest
e61c0 6f 72 65 20 63 6f 6c 6f 6e 20 2a 2f 0d 0a 20 20  ore colon */..  
e61d0 20 20 20 20 20 20 20 66 69 6c 65 4e 61 6d 65 5b         fileName[
e61e0 31 5d 20 3d 20 27 3a 27 3b 0d 0a 20 20 20 20 20  1] = ':';..     
e61f0 20 20 20 20 0d 0a 20 20 20 20 20 20 20 20 20 44      ..         D
e6200 6f 73 46 6f 72 63 65 44 65 6c 65 74 65 28 66 69  osForceDelete(fi
e6210 6c 65 4e 61 6d 65 29 3b 20 0d 0a 20 20 20 20 20  leName); ..     
e6220 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
e6230 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
e6240 78 5f 66 72 65 65 28 70 4e 6f 64 65 2d 3e 6d 75  x_free(pNode->mu
e6250 74 65 78 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 73  tex);....      s
e6260 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
e6270 65 29 3b 0d 0a 20 20 20 20 20 20 0d 0a 20 20 20  e);..      ..   
e6280 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 20   } else {..     
e6290 20 70 70 4e 6f 64 65 20 3d 20 26 70 4e 6f 64 65   ppNode = &pNode
e62a0 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d  ->pNext;..    }.
e62b0 0a 20 20 7d 20 0d 0a 0d 0a 20 20 6f 73 32 53 68  .  } ....  os2Sh
e62c0 6d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a  mLeaveMutex();..
e62d0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
e62e0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
e62f0 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  led to obtain a 
e6300 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 67 69 6f  pointer to regio
e6310 6e 20 69 52 65 67 69 6f 6e 20 6f 66 20 74 68 65  n iRegion of the
e6320 0d 0a 2a 2a 20 73 68 61 72 65 64 2d 6d 65 6d 6f  ..** shared-memo
e6330 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
e6340 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
e6350 66 69 6c 65 20 69 64 2e 20 53 68 61 72 65 64 2d  file id. Shared-
e6360 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 0d 0a  memory regions..
e6370 2a 2a 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20  ** are numbered 
e6380 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 7a 65  starting from ze
e6390 72 6f 2e 20 45 61 63 68 20 73 68 61 72 65 64 2d  ro. Each shared-
e63a0 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 69 73  memory region is
e63b0 20 73 7a 52 65 67 69 6f 6e 0d 0a 2a 2a 20 62 79   szRegion..** by
e63c0 74 65 73 20 69 6e 20 73 69 7a 65 2e 0d 0a 2a 2a  tes in size...**
e63d0 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  ..** If an error
e63e0 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f   occurs, an erro
e63f0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
e6400 65 64 20 61 6e 64 20 2a 70 70 20 69 73 20 73 65  ed and *pp is se
e6410 74 20 74 6f 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a  t to NULL...**..
e6420 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
e6430 20 74 68 65 20 62 45 78 74 65 6e 64 20 70 61 72   the bExtend par
e6440 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e 64 20  ameter is 0 and 
e6450 74 68 65 20 72 65 71 75 65 73 74 65 64 20 73 68  the requested sh
e6460 61 72 65 64 2d 6d 65 6d 6f 72 79 0d 0a 2a 2a 20  ared-memory..** 
e6470 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62  region has not b
e6480 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 28 62  een allocated (b
e6490 79 20 61 6e 79 20 63 6c 69 65 6e 74 2c 20 69 6e  y any client, in
e64a0 63 6c 75 64 69 6e 67 20 6f 6e 65 20 72 75 6e 6e  cluding one runn
e64b0 69 6e 67 20 69 6e 20 61 0d 0a 2a 2a 20 73 65 70  ing in a..** sep
e64c0 61 72 61 74 65 20 70 72 6f 63 65 73 73 29 2c 20  arate process), 
e64d0 74 68 65 6e 20 2a 70 70 20 69 73 20 73 65 74 20  then *pp is set 
e64e0 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51 4c 49  to NULL and SQLI
e64f0 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
e6500 49 66 0d 0a 2a 2a 20 62 45 78 74 65 6e 64 20 69  If..** bExtend i
e6510 73 20 6e 6f 6e 2d 7a 65 72 6f 20 61 6e 64 20 74  s non-zero and t
e6520 68 65 20 72 65 71 75 65 73 74 65 64 20 73 68 61  he requested sha
e6530 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f  red-memory regio
e6540 6e 20 68 61 73 20 6e 6f 74 20 79 65 74 0d 0a 2a  n has not yet..*
e6550 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  * been allocated
e6560 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  , it is allocate
e6570 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
e6580 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  on...**..** If t
e6590 68 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  he shared-memory
e65a0 20 72 65 67 69 6f 6e 20 68 61 73 20 61 6c 72 65   region has alre
e65b0 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
e65c0 65 64 20 6f 72 20 69 73 20 61 6c 6c 6f 63 61 74  ed or is allocat
e65d0 65 64 20 62 79 0d 0a 2a 2a 20 74 68 69 73 20 63  ed by..** this c
e65e0 61 6c 6c 20 61 73 20 64 65 73 63 72 69 62 65 64  all as described
e65f0 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 69 74 20   above, then it 
e6600 69 73 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 74  is mapped into t
e6610 68 69 73 20 70 72 6f 63 65 73 73 65 73 0d 0a 2a  his processes..*
e6620 2a 20 61 64 64 72 65 73 73 20 73 70 61 63 65 20  * address space 
e6630 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c  (if it is not al
e6640 72 65 61 64 79 29 2c 20 2a 70 70 20 69 73 20 73  ready), *pp is s
e6650 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
e6660 68 65 20 6d 61 70 70 65 64 0d 0a 2a 2a 20 6d 65  he mapped..** me
e6670 6d 6f 72 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  mory and SQLITE_
e6680 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f  OK returned...*/
e6690 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  ..static int os2
e66a0 53 68 6d 4d 61 70 28 0d 0a 20 20 73 71 6c 69 74  ShmMap(..  sqlit
e66b0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
e66c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
e66d0 6e 64 6c 65 20 6f 70 65 6e 20 6f 6e 20 64 61 74  ndle open on dat
e66e0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20  abase file */.. 
e66f0 20 69 6e 74 20 69 52 65 67 69 6f 6e 2c 20 20 20   int iRegion,   
e6700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6710 20 2f 2a 20 52 65 67 69 6f 6e 20 74 6f 20 72 65   /* Region to re
e6720 74 72 69 65 76 65 20 2a 2f 0d 0a 20 20 69 6e 74  trieve */..  int
e6730 20 73 7a 52 65 67 69 6f 6e 2c 20 20 20 20 20 20   szRegion,      
e6740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e6750 53 69 7a 65 20 6f 66 20 72 65 67 69 6f 6e 73 20  Size of regions 
e6760 2a 2f 0d 0a 20 20 69 6e 74 20 62 45 78 74 65 6e  */..  int bExten
e6770 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
e6780 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
e6790 20 65 78 74 65 6e 64 20 62 6c 6f 63 6b 20 69 66   extend block if
e67a0 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0d 0a 20   necessary */.. 
e67b0 20 76 6f 69 64 20 76 6f 6c 61 74 69 6c 65 20 2a   void volatile *
e67c0 2a 70 70 20 20 20 20 20 20 20 20 20 20 20 20 20  *pp             
e67d0 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 70 65 64 20   /* OUT: Mapped 
e67e0 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 29 7b 0d 0a 20  memory */..){.. 
e67f0 20 50 56 4f 49 44 20 70 76 54 65 6d 70 3b 0d 0a   PVOID pvTemp;..
e6800 20 20 76 6f 69 64 20 2a 2a 61 70 52 65 67 69 6f    void **apRegio
e6810 6e 3b 0d 0a 20 20 6f 73 32 53 68 6d 4e 6f 64 65  n;..  os2ShmNode
e6820 20 2a 70 4e 6f 64 65 3b 0d 0a 20 20 69 6e 74 20   *pNode;..  int 
e6830 6e 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  n, rc = SQLITE_O
e6840 4b 3b 0d 0a 20 20 63 68 61 72 20 73 68 6d 4e 61  K;..  char shmNa
e6850 6d 65 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0d  me[CCHMAXPATH];.
e6860 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
e6870 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
e6880 3b 0d 0a 20 20 0d 0a 20 20 2a 70 70 20 3d 20 4e  ;..  ..  *pp = N
e6890 55 4c 4c 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 70  ULL;....  if( !p
e68a0 46 69 6c 65 2d 3e 70 53 68 6d 4c 69 6e 6b 20 29  File->pShmLink )
e68b0 0d 0a 20 20 20 20 72 63 20 3d 20 6f 73 32 4f 70  ..    rc = os2Op
e68c0 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 20  enSharedMemory( 
e68d0 70 46 69 6c 65 2c 20 73 7a 52 65 67 69 6f 6e 20  pFile, szRegion 
e68e0 29 3b 0d 0a 20 20 0d 0a 20 20 69 66 28 20 72 63  );..  ..  if( rc
e68f0 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20   == SQLITE_OK ) 
e6900 7b 0d 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20 70  {..    pNode = p
e6910 46 69 6c 65 2d 3e 70 53 68 6d 4c 69 6e 6b 2d 3e  File->pShmLink->
e6920 70 53 68 6d 4e 6f 64 65 20 3b 0d 0a 20 20 20 20  pShmNode ;..    
e6930 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ..    sqlite3_mu
e6940 74 65 78 5f 65 6e 74 65 72 28 70 4e 6f 64 65 2d  tex_enter(pNode-
e6950 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20 0d 0a  >mutex);..    ..
e6960 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 52 65      assert( szRe
e6970 67 69 6f 6e 3d 3d 70 4e 6f 64 65 2d 3e 73 7a 52  gion==pNode->szR
e6980 65 67 69 6f 6e 20 29 3b 0d 0a 0d 0a 20 20 20 20  egion );....    
e6990 2f 2a 20 55 6e 6d 61 70 70 65 64 20 72 65 67 69  /* Unmapped regi
e69a0 6f 6e 20 3f 20 2a 2f 0d 0a 20 20 20 20 69 66 28  on ? */..    if(
e69b0 20 69 52 65 67 69 6f 6e 20 3e 3d 20 70 4e 6f 64   iRegion >= pNod
e69c0 65 2d 3e 6e 52 65 67 69 6f 6e 20 29 20 7b 0d 0a  e->nRegion ) {..
e69d0 20 20 20 20 20 20 2f 2a 20 50 72 65 76 65 6e 74        /* Prevent
e69e0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
e69f0 20 66 72 6f 6d 20 72 65 73 69 7a 69 6e 67 20 74   from resizing t
e6a00 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
e6a10 20 2a 2f 0d 0a 20 20 20 20 20 20 6f 73 32 53 68   */..      os2Sh
e6a20 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 4e 6f 64  mSystemLock(pNod
e6a30 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b 5f 57 41  e, _SHM_WRLCK_WA
e6a40 49 54 2c 20 4f 53 32 5f 53 48 4d 5f 44 4d 53 2c  IT, OS2_SHM_DMS,
e6a50 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 61 70   1);....      ap
e6a60 52 65 67 69 6f 6e 20 3d 20 73 71 6c 69 74 65 33  Region = sqlite3
e6a70 5f 72 65 61 6c 6c 6f 63 28 0d 0a 20 20 20 20 20  _realloc(..     
e6a80 20 20 20 70 4e 6f 64 65 2d 3e 61 70 52 65 67 69     pNode->apRegi
e6a90 6f 6e 2c 20 28 69 52 65 67 69 6f 6e 20 2b 20 31  on, (iRegion + 1
e6aa0 29 20 2a 20 73 69 7a 65 6f 66 28 61 70 52 65 67  ) * sizeof(apReg
e6ab0 69 6f 6e 5b 30 5d 29 29 3b 0d 0a 0d 0a 20 20 20  ion[0]));....   
e6ac0 20 20 20 69 66 28 20 61 70 52 65 67 69 6f 6e 20     if( apRegion 
e6ad0 29 20 7b 0d 0a 20 20 20 20 20 20 20 20 70 4e 6f  ) {..        pNo
e6ae0 64 65 2d 3e 61 70 52 65 67 69 6f 6e 20 3d 20 61  de->apRegion = a
e6af0 70 52 65 67 69 6f 6e 3b 0d 0a 0d 0a 20 20 20 20  pRegion;....    
e6b00 20 20 20 20 77 68 69 6c 65 28 20 70 4e 6f 64 65      while( pNode
e6b10 2d 3e 6e 52 65 67 69 6f 6e 20 3c 3d 20 69 52 65  ->nRegion <= iRe
e6b20 67 69 6f 6e 20 29 20 7b 0d 0a 20 20 20 20 20 20  gion ) {..      
e6b30 20 20 20 20 73 70 72 69 6e 74 66 28 73 68 6d 4e      sprintf(shmN
e6b40 61 6d 65 2c 20 22 25 73 2d 25 75 22 2c 20 0d 0a  ame, "%s-%u", ..
e6b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6b60 20 20 70 4e 6f 64 65 2d 3e 73 68 6d 42 61 73 65    pNode->shmBase
e6b70 4e 61 6d 65 2c 20 70 4e 6f 64 65 2d 3e 6e 52 65  Name, pNode->nRe
e6b80 67 69 6f 6e 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  gion);....      
e6b90 20 20 20 20 69 66 28 20 44 6f 73 47 65 74 4e 61      if( DosGetNa
e6ba0 6d 65 64 53 68 61 72 65 64 4d 65 6d 28 26 70 76  medSharedMem(&pv
e6bb0 54 65 6d 70 2c 20 28 50 53 5a 29 73 68 6d 4e 61  Temp, (PSZ)shmNa
e6bc0 6d 65 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  me, ..          
e6bd0 20 20 20 20 20 20 50 41 47 5f 52 45 41 44 20 7c        PAG_READ |
e6be0 20 50 41 47 5f 57 52 49 54 45 29 20 21 3d 20 4e   PAG_WRITE) != N
e6bf0 4f 5f 45 52 52 4f 52 20 29 20 7b 0d 0a 20 20 20  O_ERROR ) {..   
e6c00 20 20 20 20 20 20 20 20 20 69 66 28 20 21 62 45           if( !bE
e6c10 78 74 65 6e 64 20 29 0d 0a 20 20 20 20 20 20 20  xtend )..       
e6c20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d         break;...
e6c30 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
e6c40 20 44 6f 73 41 6c 6c 6f 63 53 68 61 72 65 64 4d   DosAllocSharedM
e6c50 65 6d 28 26 70 76 54 65 6d 70 2c 20 28 50 53 5a  em(&pvTemp, (PSZ
e6c60 29 73 68 6d 4e 61 6d 65 2c 20 73 7a 52 65 67 69  )shmName, szRegi
e6c70 6f 6e 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  on,..           
e6c80 20 20 20 20 20 20 20 50 41 47 5f 52 45 41 44 20         PAG_READ 
e6c90 7c 20 50 41 47 5f 57 52 49 54 45 20 7c 20 50 41  | PAG_WRITE | PA
e6ca0 47 5f 43 4f 4d 4d 49 54 20 7c 20 4f 42 4a 5f 41  G_COMMIT | OBJ_A
e6cb0 4e 59 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  NY) != NO_ERROR 
e6cc0 26 26 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  && ..           
e6cd0 20 20 20 20 20 44 6f 73 41 6c 6c 6f 63 53 68 61       DosAllocSha
e6ce0 72 65 64 4d 65 6d 28 26 70 76 54 65 6d 70 2c 20  redMem(&pvTemp, 
e6cf0 28 50 53 5a 29 73 68 6d 4e 61 6d 65 2c 20 73 7a  (PSZ)shmName, sz
e6d00 52 65 67 69 6f 6e 2c 0d 0a 20 20 20 20 20 20 20  Region,..       
e6d10 20 20 20 20 20 20 20 20 20 20 20 50 41 47 5f 52             PAG_R
e6d20 45 41 44 20 7c 20 50 41 47 5f 57 52 49 54 45 20  EAD | PAG_WRITE 
e6d30 7c 20 50 41 47 5f 43 4f 4d 4d 49 54 29 20 21 3d  | PAG_COMMIT) !=
e6d40 20 4e 4f 5f 45 52 52 4f 52 20 29 20 7b 20 0d 0a   NO_ERROR ) { ..
e6d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
e6d60 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e6d70 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
e6d80 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
e6d90 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
e6da0 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 20 20   }....          
e6db0 61 70 52 65 67 69 6f 6e 5b 70 4e 6f 64 65 2d 3e  apRegion[pNode->
e6dc0 6e 52 65 67 69 6f 6e 2b 2b 5d 20 3d 20 70 76 54  nRegion++] = pvT
e6dd0 65 6d 70 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  emp;..        }.
e6de0 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 7a 65  ...        /* ze
e6df0 72 6f 20 6f 75 74 20 72 65 6d 61 69 6e 69 6e 67  ro out remaining
e6e00 20 65 6e 74 72 69 65 73 20 2a 2f 20 0d 0a 20 20   entries */ ..  
e6e10 20 20 20 20 20 20 66 6f 72 28 20 6e 20 3d 20 70        for( n = p
e6e20 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 3b 20 6e  Node->nRegion; n
e6e30 20 3c 3d 20 69 52 65 67 69 6f 6e 3b 20 6e 2b 2b   <= iRegion; n++
e6e40 20 29 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4e   )..          pN
e6e50 6f 64 65 2d 3e 61 70 52 65 67 69 6f 6e 5b 6e 5d  ode->apRegion[n]
e6e60 20 3d 20 4e 55 4c 4c 3b 0d 0a 0d 0a 20 20 20 20   = NULL;....    
e6e70 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68      /* Return th
e6e80 69 73 20 72 65 67 69 6f 6e 20 28 6d 61 79 62 65  is region (maybe
e6e90 20 7a 65 72 6f 29 20 2a 2f 0d 0a 20 20 20 20 20   zero) */..     
e6ea0 20 20 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e     *pp = pNode->
e6eb0 61 70 52 65 67 69 6f 6e 5b 69 52 65 67 69 6f 6e  apRegion[iRegion
e6ec0 5d 3b 0d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  ];..      } else
e6ed0 20 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d   {..        rc =
e6ee0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
e6ef0 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
e6f00 20 2f 2a 20 41 6c 6c 6f 77 20 6f 74 68 65 72 20   /* Allow other 
e6f10 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 73  processes to res
e6f20 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d  ize the shared m
e6f30 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 20 20 20 20  emory */..      
e6f40 6f 73 32 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b  os2ShmSystemLock
e6f50 28 70 4e 6f 64 65 2c 20 5f 53 48 4d 5f 55 4e 4c  (pNode, _SHM_UNL
e6f60 43 4b 2c 20 4f 53 32 5f 53 48 4d 5f 44 4d 53 2c  CK, OS2_SHM_DMS,
e6f70 20 31 29 3b 0d 0a 20 20 20 20 20 20 0d 0a 20 20   1);..      ..  
e6f80 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20    } else {..    
e6f90 20 20 2f 2a 20 52 65 67 69 6f 6e 20 68 61 73 20    /* Region has 
e6fa0 62 65 65 6e 20 6d 61 70 70 65 64 20 70 72 65 76  been mapped prev
e6fb0 69 6f 75 73 6c 79 20 2a 2f 0d 0a 20 20 20 20 20  iously */..     
e6fc0 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 61 70   *pp = pNode->ap
e6fd0 52 65 67 69 6f 6e 5b 69 52 65 67 69 6f 6e 5d 3b  Region[iRegion];
e6fe0 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73  ..    }....    s
e6ff0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
e7000 76 65 28 70 4e 6f 64 65 2d 3e 6d 75 74 65 78 29  ve(pNode->mutex)
e7010 3b 0d 0a 20 20 7d 20 0d 0a 0d 0a 20 20 45 52 52  ;..  } ....  ERR
e7020 5f 54 52 41 43 45 28 72 63 2c 20 28 22 6f 73 32  _TRACE(rc, ("os2
e7030 53 68 6d 4d 61 70 3a 20 25 73 20 69 52 67 6e 20  ShmMap: %s iRgn 
e7040 3d 20 25 64 2c 20 73 7a 52 67 6e 20 3d 20 25 64  = %d, szRgn = %d
e7050 2c 20 62 45 78 74 20 3d 20 25 64 20 3a 20 25 64  , bExt = %d : %d
e7060 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  \n", ..         
e7070 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 7a          pFile->z
e7080 46 75 6c 6c 50 61 74 68 43 70 2c 20 69 52 65 67  FullPathCp, iReg
e7090 69 6f 6e 2c 20 73 7a 52 65 67 69 6f 6e 2c 20 62  ion, szRegion, b
e70a0 45 78 74 65 6e 64 2c 20 72 63 29 29 0d 0a 20 20  Extend, rc))..  
e70b0 20 20 20 20 20 20 20 20 0d 0a 20 20 72 65 74 75          ..  retu
e70c0 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
e70d0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 6f 6e 6e  .** Close a conn
e70e0 65 63 74 69 6f 6e 20 74 6f 20 73 68 61 72 65 64  ection to shared
e70f0 2d 6d 65 6d 6f 72 79 2e 20 20 44 65 6c 65 74 65  -memory.  Delete
e7100 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0d   the underlying.
e7110 0a 2a 2a 20 73 74 6f 72 61 67 65 20 69 66 20 64  .** storage if d
e7120 65 6c 65 74 65 46 6c 61 67 20 69 73 20 74 72 75  eleteFlag is tru
e7130 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  e...**..** If th
e7140 65 72 65 20 69 73 20 6e 6f 20 73 68 61 72 65 64  ere is no shared
e7150 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
e7160 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 6e 6e  ed with the conn
e7170 65 63 74 69 6f 6e 20 74 68 65 6e 20 74 68 69 73  ection then this
e7180 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  ..** routine is 
e7190 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
e71a0 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
e71b0 74 20 6f 73 32 53 68 6d 55 6e 6d 61 70 28 0d 0a  t os2ShmUnmap(..
e71c0 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
e71d0 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
e71e0 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79    /* The underly
e71f0 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c  ing database fil
e7200 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 64 65 6c 65  e */..  int dele
e7210 74 65 46 6c 61 67 20 20 20 20 20 20 20 20 20 20  teFlag          
e7220 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74          /* Delet
e7230 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  e shared-memory 
e7240 69 66 20 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a  if true */..){..
e7250 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
e7260 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
e7270 0d 0a 20 20 6f 73 32 53 68 6d 4c 69 6e 6b 20 2a  ..  os2ShmLink *
e7280 70 4c 69 6e 6b 20 3d 20 70 46 69 6c 65 2d 3e 70  pLink = pFile->p
e7290 53 68 6d 4c 69 6e 6b 3b 0d 0a 20 20 0d 0a 20 20  ShmLink;..  ..  
e72a0 69 66 28 20 70 4c 69 6e 6b 20 29 20 7b 0d 0a 20  if( pLink ) {.. 
e72b0 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 2d 31     int nRef = -1
e72c0 3b 0d 0a 20 20 20 20 6f 73 32 53 68 6d 4c 69 6e  ;..    os2ShmLin
e72d0 6b 20 2a 2a 70 70 4c 69 6e 6b 3b 0d 0a 20 20 20  k **ppLink;..   
e72e0 20 6f 73 32 53 68 6d 4e 6f 64 65 20 2a 70 4e 6f   os2ShmNode *pNo
e72f0 64 65 20 3d 20 70 4c 69 6e 6b 2d 3e 70 53 68 6d  de = pLink->pShm
e7300 4e 6f 64 65 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c  Node;....    sql
e7310 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
e7320 28 70 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d  (pNode->mutex);.
e7330 0a 20 20 20 20 0d 0a 20 20 20 20 66 6f 72 28 20  .    ..    for( 
e7340 70 70 4c 69 6e 6b 20 3d 20 26 70 4e 6f 64 65 2d  ppLink = &pNode-
e7350 3e 70 46 69 72 73 74 3b 0d 0a 20 20 20 20 20 20  >pFirst;..      
e7360 20 20 20 2a 70 70 4c 69 6e 6b 20 26 26 20 2a 70     *ppLink && *p
e7370 70 4c 69 6e 6b 20 21 3d 20 70 4c 69 6e 6b 3b 0d  pLink != pLink;.
e7380 0a 20 20 20 20 20 20 20 20 20 70 70 4c 69 6e 6b  .         ppLink
e7390 20 3d 20 26 28 2a 70 70 4c 69 6e 6b 29 2d 3e 70   = &(*ppLink)->p
e73a0 4e 65 78 74 20 29 20 20 20 3b 0d 0a 20 20 20 20  Next )   ;..    
e73b0 20 20 20 20 20 0d 0a 20 20 20 20 61 73 73 65 72       ..    asser
e73c0 74 28 2a 70 70 4c 69 6e 6b 29 3b 0d 0a 0d 0a 20  t(*ppLink);.... 
e73d0 20 20 20 69 66 28 20 2a 70 70 4c 69 6e 6b 20 29     if( *ppLink )
e73e0 20 7b 0d 0a 20 20 20 20 20 20 2a 70 70 4c 69 6e   {..      *ppLin
e73f0 6b 20 3d 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74  k = pLink->pNext
e7400 3b 0d 0a 20 20 20 20 20 20 6e 52 65 66 20 3d 20  ;..      nRef = 
e7410 2d 2d 70 4e 6f 64 65 2d 3e 6e 52 65 66 3b 0d 0a  --pNode->nRef;..
e7420 20 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20      } else {..  
e7430 20 20 20 20 45 52 52 5f 54 52 41 43 45 28 31 2c      ERR_TRACE(1,
e7440 20 28 22 6f 73 32 53 68 6d 55 6e 6d 61 70 3a 20   ("os2ShmUnmap: 
e7450 6c 69 6e 6b 20 6e 6f 74 20 66 6f 75 6e 64 20 21  link not found !
e7460 20 25 73 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20   %s\n", ..      
e7470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
e7480 6f 64 65 2d 3e 73 68 6d 42 61 73 65 4e 61 6d 65  ode->shmBaseName
e7490 29 29 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 0d  ))..    }..    .
e74a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 53 68 6d  .    pFile->pShm
e74b0 4c 69 6e 6b 20 3d 20 4e 55 4c 4c 3b 0d 0a 20 20  Link = NULL;..  
e74c0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
e74d0 4c 69 6e 6b 29 3b 0d 0a 0d 0a 20 20 20 20 73 71  Link);....    sq
e74e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
e74f0 65 28 70 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  e(pNode->mutex);
e7500 0d 0a 20 20 20 20 0d 0a 20 20 20 20 69 66 28 20  ..    ..    if( 
e7510 6e 52 65 66 20 3d 3d 20 30 20 29 0d 0a 20 20 20  nRef == 0 )..   
e7520 20 20 20 6f 73 32 50 75 72 67 65 53 68 6d 4e 6f     os2PurgeShmNo
e7530 64 65 73 28 20 64 65 6c 65 74 65 46 6c 61 67 20  des( deleteFlag 
e7540 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  );..  }....  ret
e7550 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
e7560 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e  }..../*..** Chan
e7570 67 65 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ge the lock stat
e7580 65 20 66 6f 72 20 61 20 73 68 61 72 65 64 2d 6d  e for a shared-m
e7590 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 2e 0d 0a  emory segment...
e75a0 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  **..** Note that
e75b0 20 74 68 65 20 72 65 6c 61 74 69 6f 6e 73 68 69   the relationshi
e75c0 70 20 62 65 74 77 65 65 6e 20 53 48 41 52 45 64  p between SHAREd
e75d0 20 61 6e 64 20 45 58 43 4c 55 53 49 56 45 20 6c   and EXCLUSIVE l
e75e0 6f 63 6b 73 20 69 73 20 61 20 6c 69 74 74 6c 65  ocks is a little
e75f0 0d 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 68  ..** different h
e7600 65 72 65 20 74 68 61 6e 20 69 6e 20 70 6f 73 69  ere than in posi
e7610 78 2e 20 20 49 6e 20 78 53 68 6d 4c 6f 63 6b 28  x.  In xShmLock(
e7620 29 2c 20 6f 6e 65 20 63 61 6e 20 67 6f 20 66 72  ), one can go fr
e7630 6f 6d 20 75 6e 6c 6f 63 6b 65 64 0d 0a 2a 2a 20  om unlocked..** 
e7640 74 6f 20 73 68 61 72 65 64 20 61 6e 64 20 62 61  to shared and ba
e7650 63 6b 20 6f 72 20 66 72 6f 6d 20 75 6e 6c 6f 63  ck or from unloc
e7660 6b 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65  ked to exclusive
e7670 20 61 6e 64 20 62 61 63 6b 2e 20 20 42 75 74 20   and back.  But 
e7680 6f 6e 65 20 6d 61 79 0d 0a 2a 2a 20 6e 6f 74 20  one may..** not 
e7690 67 6f 20 66 72 6f 6d 20 73 68 61 72 65 64 20 74  go from shared t
e76a0 6f 20 65 78 63 6c 75 73 69 76 65 20 6f 72 20 66  o exclusive or f
e76b0 72 6f 6d 20 65 78 63 6c 75 73 69 76 65 20 74 6f  rom exclusive to
e76c0 20 73 68 61 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74   shared...*/..st
e76d0 61 74 69 63 20 69 6e 74 20 6f 73 32 53 68 6d 4c  atic int os2ShmL
e76e0 6f 63 6b 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ock(..  sqlite3_
e76f0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
e7700 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
e7710 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ile holding the 
e7720 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  shared memory */
e7730 0d 0a 20 20 69 6e 74 20 6f 66 73 74 2c 20 20 20  ..  int ofst,   
e7740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e7750 2a 20 46 69 72 73 74 20 6c 6f 63 6b 20 74 6f 20  * First lock to 
e7760 61 63 71 75 69 72 65 20 6f 72 20 72 65 6c 65 61  acquire or relea
e7770 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 2c 20  se */..  int n, 
e7780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7790 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
e77a0 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71 75 69 72   locks to acquir
e77b0 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f 0d  e or release */.
e77c0 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20  .  int flags    
e77d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e77e0 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
e77f0 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0d 0a 29 7b   the lock */..){
e7800 0d 0a 20 20 75 33 32 20 6d 61 73 6b 3b 20 20 20  ..  u32 mask;   
e7810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
e7830 6b 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 74 61  k of locks to ta
e7840 6b 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f  ke or release */
e7850 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
e7860 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
e7870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
e7880 75 6c 74 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 6f  ult code */..  o
e7890 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
e78a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0d 0a 20  (os2File*)id;.. 
e78b0 20 6f 73 32 53 68 6d 4c 69 6e 6b 20 2a 70 20 3d   os2ShmLink *p =
e78c0 20 70 46 69 6c 65 2d 3e 70 53 68 6d 4c 69 6e 6b   pFile->pShmLink
e78d0 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68  ;      /* The sh
e78e0 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 65 69 6e  ared memory bein
e78f0 67 20 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 20 20 6f  g locked */..  o
e7900 73 32 53 68 6d 4c 69 6e 6b 20 2a 70 58 3b 20 20  s2ShmLink *pX;  
e7910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7920 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
e7930 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 73 69 62  ing over all sib
e7940 6c 69 6e 67 73 20 2a 2f 0d 0a 20 20 6f 73 32 53  lings */..  os2S
e7950 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65  hmNode *pShmNode
e7960 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 20   = p->pShmNode; 
e7970 20 20 2f 2a 20 4f 75 72 20 6e 6f 64 65 20 2a 2f    /* Our node */
e7980 0d 0a 20 20 0d 0a 20 20 61 73 73 65 72 74 28 20  ..  ..  assert( 
e7990 6f 66 73 74 3e 3d 30 20 26 26 20 6f 66 73 74 2b  ofst>=0 && ofst+
e79a0 6e 3c 3d 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c  n<=SQLITE_SHM_NL
e79b0 4f 43 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  OCK );..  assert
e79c0 28 20 6e 3e 3d 31 20 29 3b 0d 0a 20 20 61 73 73  ( n>=1 );..  ass
e79d0 65 72 74 28 20 66 6c 61 67 73 3d 3d 28 53 51 4c  ert( flags==(SQL
e79e0 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53  ITE_SHM_LOCK | S
e79f0 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44  QLITE_SHM_SHARED
e7a00 29 0d 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c 61  )..       || fla
e7a10 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d 5f  gs==(SQLITE_SHM_
e7a20 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48  LOCK | SQLITE_SH
e7a30 4d 5f 45 58 43 4c 55 53 49 56 45 29 0d 0a 20 20  M_EXCLUSIVE)..  
e7a40 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d 28       || flags==(
e7a50 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43  SQLITE_SHM_UNLOC
e7a60 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53  K | SQLITE_SHM_S
e7a70 48 41 52 45 44 29 0d 0a 20 20 20 20 20 20 20 7c  HARED)..       |
e7a80 7c 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45  | flags==(SQLITE
e7a90 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51  _SHM_UNLOCK | SQ
e7aa0 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49  LITE_SHM_EXCLUSI
e7ab0 56 45 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  VE) );..  assert
e7ac0 28 20 6e 3d 3d 31 20 7c 7c 20 28 66 6c 61 67 73  ( n==1 || (flags
e7ad0 20 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58   & SQLITE_SHM_EX
e7ae0 43 4c 55 53 49 56 45 29 21 3d 30 20 29 3b 0d 0a  CLUSIVE)!=0 );..
e7af0 0d 0a 20 20 6d 61 73 6b 20 3d 20 28 75 33 32 29  ..  mask = (u32)
e7b00 28 28 31 55 3c 3c 28 6f 66 73 74 2b 6e 29 29 20  ((1U<<(ofst+n)) 
e7b10 2d 20 28 31 55 3c 3c 6f 66 73 74 29 29 3b 0d 0a  - (1U<<ofst));..
e7b20 20 20 61 73 73 65 72 74 28 20 6e 3e 31 20 7c 7c    assert( n>1 ||
e7b30 20 6d 61 73 6b 3d 3d 28 31 3c 3c 6f 66 73 74 29   mask==(1<<ofst)
e7b40 20 29 3b 0d 0a 0d 0a 0d 0a 20 20 73 71 6c 69 74   );......  sqlit
e7b50 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
e7b60 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  ShmNode->mutex);
e7b70 0d 0a 0d 0a 20 20 69 66 28 20 66 6c 61 67 73 20  ....  if( flags 
e7b80 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c  & SQLITE_SHM_UNL
e7b90 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 75 33 32 20  OCK ){..    u32 
e7ba0 61 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f 2a 20  allMask = 0; /* 
e7bb0 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20 68 65  Mask of locks he
e7bc0 6c 64 20 62 79 20 73 69 62 6c 69 6e 67 73 20 2a  ld by siblings *
e7bd0 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 65 65 20  /....    /* See 
e7be0 69 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 73 20  if any siblings 
e7bf0 68 6f 6c 64 20 74 68 69 73 20 73 61 6d 65 20 6c  hold this same l
e7c00 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28  ock */..    for(
e7c10 70 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69  pX=pShmNode->pFi
e7c20 72 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e  rst; pX; pX=pX->
e7c30 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 69  pNext){..      i
e7c40 66 28 20 70 58 3d 3d 70 20 29 20 63 6f 6e 74 69  f( pX==p ) conti
e7c50 6e 75 65 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  nue;..      asse
e7c60 72 74 28 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73  rt( (pX->exclMas
e7c70 6b 20 26 20 28 70 2d 3e 65 78 63 6c 4d 61 73 6b  k & (p->exclMask
e7c80 7c 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b 29 29  |p->sharedMask))
e7c90 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 6c  ==0 );..      al
e7ca0 6c 4d 61 73 6b 20 7c 3d 20 70 58 2d 3e 73 68 61  lMask |= pX->sha
e7cb0 72 65 64 4d 61 73 6b 3b 0d 0a 20 20 20 20 7d 0d  redMask;..    }.
e7cc0 0a 0d 0a 20 20 20 20 2f 2a 20 55 6e 6c 6f 63 6b  ...    /* Unlock
e7cd0 20 74 68 65 20 73 79 73 74 65 6d 2d 6c 65 76 65   the system-leve
e7ce0 6c 20 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 20 20  l locks */..    
e7cf0 69 66 28 20 28 6d 61 73 6b 20 26 20 61 6c 6c 4d  if( (mask & allM
e7d00 61 73 6b 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ask)==0 ){..    
e7d10 20 20 72 63 20 3d 20 6f 73 32 53 68 6d 53 79 73    rc = os2ShmSys
e7d20 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65  temLock(pShmNode
e7d30 2c 20 5f 53 48 4d 5f 55 4e 4c 43 4b 2c 20 6f 66  , _SHM_UNLCK, of
e7d40 73 74 2b 4f 53 32 5f 53 48 4d 5f 42 41 53 45 2c  st+OS2_SHM_BASE,
e7d50 20 6e 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b   n);..    }else{
e7d60 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
e7d70 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a  ITE_OK;..    }..
e7d80 0d 0a 20 20 20 20 2f 2a 20 55 6e 64 6f 20 74 68  ..    /* Undo th
e7d90 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 2a 2f  e local locks */
e7da0 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
e7db0 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
e7dc0 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d    p->exclMask &=
e7dd0 20 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 70   ~mask;..      p
e7de0 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 3d 20  ->sharedMask &= 
e7df0 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20 7d 20 0d 0a  ~mask;..    } ..
e7e00 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67    }else if( flag
e7e10 73 20 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53  s & SQLITE_SHM_S
e7e20 48 41 52 45 44 20 29 7b 0d 0a 20 20 20 20 75 33  HARED ){..    u3
e7e30 32 20 61 6c 6c 53 68 61 72 65 64 20 3d 20 30 3b  2 allShared = 0;
e7e40 20 20 2f 2a 20 55 6e 69 6f 6e 20 6f 66 20 6c 6f    /* Union of lo
e7e50 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e  cks held by conn
e7e60 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 20 74 68  ections other th
e7e70 61 6e 20 22 70 22 20 2a 2f 0d 0a 0d 0a 20 20 20  an "p" */....   
e7e80 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 69   /* Find out whi
e7e90 63 68 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20  ch shared locks 
e7ea0 61 72 65 20 61 6c 72 65 61 64 79 20 68 65 6c 64  are already held
e7eb0 20 62 79 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e   by sibling conn
e7ec0 65 63 74 69 6f 6e 73 2e 0d 0a 20 20 20 20 2a 2a  ections...    **
e7ed0 20 49 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 20   If any sibling 
e7ee0 61 6c 72 65 61 64 79 20 68 6f 6c 64 73 20 61 6e  already holds an
e7ef0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c   exclusive lock,
e7f00 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65   go ahead and re
e7f10 74 75 72 6e 0d 0a 20 20 20 20 2a 2a 20 53 51 4c  turn..    ** SQL
e7f20 49 54 45 5f 42 55 53 59 2e 0d 0a 20 20 20 20 2a  ITE_BUSY...    *
e7f30 2f 0d 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 53  /..    for(pX=pS
e7f40 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20  hmNode->pFirst; 
e7f50 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 74  pX; pX=pX->pNext
e7f60 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70  ){..      if( (p
e7f70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d 61  X->exclMask & ma
e7f80 73 6b 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  sk)!=0 ){..     
e7f90 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
e7fa0 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  USY;..        br
e7fb0 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
e7fc0 20 20 20 20 20 61 6c 6c 53 68 61 72 65 64 20 7c       allShared |
e7fd0 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b  = pX->sharedMask
e7fe0 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
e7ff0 2f 2a 20 47 65 74 20 73 68 61 72 65 64 20 6c 6f  /* Get shared lo
e8000 63 6b 73 20 61 74 20 74 68 65 20 73 79 73 74 65  cks at the syste
e8010 6d 20 6c 65 76 65 6c 2c 20 69 66 20 6e 65 63 65  m level, if nece
e8020 73 73 61 72 79 20 2a 2f 0d 0a 20 20 20 20 69 66  ssary */..    if
e8030 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
e8040 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 61  ){..      if( (a
e8050 6c 6c 53 68 61 72 65 64 20 26 20 6d 61 73 6b 29  llShared & mask)
e8060 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
e8070 72 63 20 3d 20 6f 73 32 53 68 6d 53 79 73 74 65  rc = os2ShmSyste
e8080 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20  mLock(pShmNode, 
e8090 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 6f 66 73 74  _SHM_RDLCK, ofst
e80a0 2b 4f 53 32 5f 53 48 4d 5f 42 41 53 45 2c 20 6e  +OS2_SHM_BASE, n
e80b0 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  );..      }else{
e80c0 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ..        rc = S
e80d0 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
e80e0 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
e80f0 20 2f 2a 20 47 65 74 20 74 68 65 20 6c 6f 63 61   /* Get the loca
e8100 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 2a  l shared locks *
e8110 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  /..    if( rc==S
e8120 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
e8130 20 20 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b     p->sharedMask
e8140 20 7c 3d 20 6d 61 73 6b 3b 0d 0a 20 20 20 20 7d   |= mask;..    }
e8150 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
e8160 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20  /* Make sure no 
e8170 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69  sibling connecti
e8180 6f 6e 73 20 68 6f 6c 64 20 6c 6f 63 6b 73 20 74  ons hold locks t
e8190 68 61 74 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 74  hat will block t
e81a0 68 69 73 0d 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b  his..    ** lock
e81b0 2e 20 20 49 66 20 61 6e 79 20 64 6f 2c 20 72 65  .  If any do, re
e81c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
e81d0 20 72 69 67 68 74 20 61 77 61 79 2e 0d 0a 20 20   right away...  
e81e0 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 70 58    */..    for(pX
e81f0 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73  =pShmNode->pFirs
e8200 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e  t; pX; pX=pX->pN
e8210 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  ext){..      if(
e8220 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26   (pX->exclMask &
e8230 20 6d 61 73 6b 29 21 3d 30 20 7c 7c 20 28 70 58   mask)!=0 || (pX
e8240 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 20 6d  ->sharedMask & m
e8250 61 73 6b 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  ask)!=0 ){..    
e8260 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e8270 42 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 62  BUSY;..        b
e8280 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
e8290 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f      }..  ..    /
e82a0 2a 20 47 65 74 20 74 68 65 20 65 78 63 6c 75 73  * Get the exclus
e82b0 69 76 65 20 6c 6f 63 6b 73 20 61 74 20 74 68 65  ive locks at the
e82c0 20 73 79 73 74 65 6d 20 6c 65 76 65 6c 2e 20 20   system level.  
e82d0 54 68 65 6e 20 69 66 20 73 75 63 63 65 73 73 66  Then if successf
e82e0 75 6c 0d 0a 20 20 20 20 2a 2a 20 61 6c 73 6f 20  ul..    ** also 
e82f0 6d 61 72 6b 20 74 68 65 20 6c 6f 63 61 6c 20 63  mark the local c
e8300 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 62 65 69  onnection as bei
e8310 6e 67 20 6c 6f 63 6b 65 64 2e 0d 0a 20 20 20 20  ng locked...    
e8320 2a 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  */..    if( rc==
e8330 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
e8340 20 20 20 20 72 63 20 3d 20 6f 73 32 53 68 6d 53      rc = os2ShmS
e8350 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f  ystemLock(pShmNo
e8360 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b 2c 20  de, _SHM_WRLCK, 
e8370 6f 66 73 74 2b 4f 53 32 5f 53 48 4d 5f 42 41 53  ofst+OS2_SHM_BAS
e8380 45 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20 69 66  E, n);..      if
e8390 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
e83a0 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
e83b0 72 74 28 20 28 70 2d 3e 73 68 61 72 65 64 4d 61  rt( (p->sharedMa
e83c0 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 3b  sk & mask)==0 );
e83d0 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 78 63  ..        p->exc
e83e0 6c 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0d 0a  lMask |= mask;..
e83f0 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
e8400 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33    }....  sqlite3
e8410 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68  _mutex_leave(pSh
e8420 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a  mNode->mutex);..
e8430 20 20 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22    ..  OSTRACE(("
e8440 53 48 4d 2d 4c 4f 43 4b 20 73 68 6d 69 64 2d 25  SHM-LOCK shmid-%
e8450 64 2c 20 70 69 64 2d 25 64 20 67 6f 74 20 25 30  d, pid-%d got %0
e8460 33 78 2c 25 30 33 78 20 25 73 5c 6e 22 2c 0d 0a  3x,%03x %s\n",..
e8470 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 64             p->id
e8480 2c 20 28 69 6e 74 29 47 65 74 43 75 72 72 65 6e  , (int)GetCurren
e8490 74 50 72 6f 63 65 73 73 49 64 28 29 2c 20 70 2d  tProcessId(), p-
e84a0 3e 73 68 61 72 65 64 4d 61 73 6b 2c 20 70 2d 3e  >sharedMask, p->
e84b0 65 78 63 6c 4d 61 73 6b 2c 0d 0a 20 20 20 20 20  exclMask,..     
e84c0 20 20 20 20 20 20 72 63 20 3f 20 22 66 61 69 6c        rc ? "fail
e84d0 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0d 0a 0d  ed" : "ok"));...
e84e0 0a 20 20 45 52 52 5f 54 52 41 43 45 28 72 63 2c  .  ERR_TRACE(rc,
e84f0 20 28 22 6f 73 32 53 68 6d 4c 6f 63 6b 3a 20 6f   ("os2ShmLock: o
e8500 66 73 74 20 3d 20 25 64 2c 20 6e 20 3d 20 25 64  fst = %d, n = %d
e8510 2c 20 66 6c 61 67 73 20 3d 20 30 78 25 78 20 2d  , flags = 0x%x -
e8520 3e 20 25 64 20 5c 6e 22 2c 20 0d 0a 20 20 20 20  > %d \n", ..    
e8530 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 73               ofs
e8540 74 2c 20 6e 2c 20 66 6c 61 67 73 2c 20 72 63 29  t, n, flags, rc)
e8550 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )..             
e8560 20 20 20 20 20 0d 0a 20 20 72 65 74 75 72 6e 20       ..  return 
e8570 72 63 3b 20 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  rc; ..}..../*..*
e8580 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65  * Implement a me
e8590 6d 6f 72 79 20 62 61 72 72 69 65 72 20 6f 72 20  mory barrier or 
e85a0 6d 65 6d 6f 72 79 20 66 65 6e 63 65 20 6f 6e 20  memory fence on 
e85b0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 2e 0d 0a  shared memory...
e85c0 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 6c 6f 61 64 73  **..** All loads
e85d0 20 61 6e 64 20 73 74 6f 72 65 73 20 62 65 67 75   and stores begu
e85e0 6e 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 72  n before the bar
e85f0 72 69 65 72 20 6d 75 73 74 20 63 6f 6d 70 6c 65  rier must comple
e8600 74 65 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 61 6e  te before..** an
e8610 79 20 6c 6f 61 64 20 6f 72 20 73 74 6f 72 65 20  y load or store 
e8620 62 65 67 75 6e 20 61 66 74 65 72 20 74 68 65 20  begun after the 
e8630 62 61 72 72 69 65 72 2e 0d 0a 2a 2f 0d 0a 73 74  barrier...*/..st
e8640 61 74 69 63 20 76 6f 69 64 20 6f 73 32 53 68 6d  atic void os2Shm
e8650 42 61 72 72 69 65 72 28 0d 0a 20 20 73 71 6c 69  Barrier(..  sqli
e8660 74 65 33 5f 66 69 6c 65 20 2a 69 64 20 20 20 20  te3_file *id    
e8670 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
e8680 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 6f 6c  atabase file hol
e8690 64 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  ding the shared 
e86a0 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 29 7b 0d 0a 20  memory */..){.. 
e86b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e86c0 52 28 69 64 29 3b 0d 0a 20 20 6f 73 32 53 68 6d  R(id);..  os2Shm
e86d0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20  EnterMutex();.. 
e86e0 20 6f 73 32 53 68 6d 4c 65 61 76 65 4d 75 74 65   os2ShmLeaveMute
e86f0 78 28 29 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6c 73 65  x();..}....#else
e8700 0d 0a 23 20 64 65 66 69 6e 65 20 6f 73 32 53 68  ..# define os2Sh
e8710 6d 4d 61 70 20 20 20 20 20 30 0d 0a 23 20 64 65  mMap     0..# de
e8720 66 69 6e 65 20 6f 73 32 53 68 6d 4c 6f 63 6b 20  fine os2ShmLock 
e8730 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 6f     0..# define o
e8740 73 32 53 68 6d 42 61 72 72 69 65 72 20 30 0d 0a  s2ShmBarrier 0..
e8750 23 20 64 65 66 69 6e 65 20 6f 73 32 53 68 6d 55  # define os2ShmU
e8760 6e 6d 61 70 20 20 20 30 0d 0a 23 65 6e 64 69 66  nmap   0..#endif
e8770 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
e8780 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f 0d 0a  TE_OMIT_WAL */..
e8790 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
e87a0 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61  vector defines a
e87b0 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74  ll the methods t
e87c0 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20  hat can operate 
e87d0 6f 6e 20 61 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65  on an..** sqlite
e87e0 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73 32 2e 0d  3_file for os2..
e87f0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73  .*/..static cons
e8800 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e8810 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f 64  hods os2IoMethod
e8820 20 3d 20 7b 0d 0a 20 20 32 2c 20 20 20 20 20 20   = {..  2,      
e8830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8840 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
e8850 69 6f 6e 20 2a 2f 0d 0a 20 20 6f 73 32 43 6c 6f  ion */..  os2Clo
e8860 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e8870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
e8880 6f 73 65 20 2a 2f 0d 0a 20 20 6f 73 32 52 65 61  ose */..  os2Rea
e8890 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
e88a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65            /* xRe
e88b0 61 64 20 2a 2f 0d 0a 20 20 6f 73 32 57 72 69 74  ad */..  os2Writ
e88c0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e88d0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69           /* xWri
e88e0 74 65 20 2a 2f 0d 0a 20 20 6f 73 32 54 72 75 6e  te */..  os2Trun
e88f0 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  cate,           
e8900 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75           /* xTru
e8910 6e 63 61 74 65 20 2a 2f 0d 0a 20 20 6f 73 32 53  ncate */..  os2S
e8920 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  ync,            
e8930 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e8940 53 79 6e 63 20 2a 2f 0d 0a 20 20 6f 73 32 46 69  Sync */..  os2Fi
e8950 6c 65 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20  leSize,         
e8960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
e8970 69 6c 65 53 69 7a 65 20 2a 2f 0d 0a 20 20 6f 73  ileSize */..  os
e8980 32 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  2Lock,          
e8990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e89a0 20 78 4c 6f 63 6b 20 2a 2f 0d 0a 20 20 6f 73 32   xLock */..  os2
e89b0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e89c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e89d0 78 55 6e 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 6f 73  xUnlock */..  os
e89e0 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  2CheckReservedLo
e89f0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ck,           /*
e8a00 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e8a10 6f 63 6b 20 2a 2f 0d 0a 20 20 6f 73 32 46 69 6c  ock */..  os2Fil
e8a20 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 20  eControl,       
e8a30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
e8a40 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0d 0a 20 20  leControl */..  
e8a50 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 20 20  os2SectorSize,  
e8a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8a70 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a  /* xSectorSize *
e8a80 2f 0d 0a 20 20 6f 73 32 44 65 76 69 63 65 43 68  /..  os2DeviceCh
e8a90 61 72 61 63 74 65 72 69 73 74 69 63 73 2c 20 20  aracteristics,  
e8aa0 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43       /* xDeviceC
e8ab0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a  haracteristics *
e8ac0 2f 0d 0a 20 20 6f 73 32 53 68 6d 4d 61 70 2c 20  /..  os2ShmMap, 
e8ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8ae0 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20       /* xShmMap 
e8af0 2a 2f 0d 0a 20 20 6f 73 32 53 68 6d 4c 6f 63 6b  */..  os2ShmLock
e8b00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e8b10 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 4c 6f 63        /* xShmLoc
e8b20 6b 20 2a 2f 0d 0a 20 20 6f 73 32 53 68 6d 42 61  k */..  os2ShmBa
e8b30 72 72 69 65 72 2c 20 20 20 20 20 20 20 20 20 20  rrier,          
e8b40 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 42          /* xShmB
e8b50 61 72 72 69 65 72 20 2a 2f 0d 0a 20 20 6f 73 32  arrier */..  os2
e8b60 53 68 6d 55 6e 6d 61 70 20 20 20 20 20 20 20 20  ShmUnmap        
e8b70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e8b80 78 53 68 6d 55 6e 6d 61 70 20 2a 2f 0d 0a 7d 3b  xShmUnmap */..};
e8b90 0d 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ....../*********
e8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8be0 2a 2a 0d 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73  **..** Here ends
e8bf0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
e8c00 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73   that form the s
e8c10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e8c20 73 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a  s object...**..*
e8c30 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b  * The next block
e8c40 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65   of code impleme
e8c50 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65 74 68  nts the VFS meth
e8c60 6f 64 73 2e 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ods...**********
e8c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8cb0 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72  **/..../*..** Cr
e8cc0 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79  eate a temporary
e8cd0 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42   file name in zB
e8ce0 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62  uf.  zBuf must b
e8cf0 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0d  e big enough to.
e8d00 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73  .** hold at pVfs
e8d10 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61  ->mxPathname cha
e8d20 72 61 63 74 65 72 73 2e 0d 0a 2a 2f 0d 0a 73 74  racters...*/..st
e8d30 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70  atic int getTemp
e8d40 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63  name(int nBuf, c
e8d50 68 61 72 20 2a 7a 42 75 66 20 29 7b 0d 0a 20 20  har *zBuf ){..  
e8d60 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
e8d70 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0d 0a 20 20  r zChars[] =..  
e8d80 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d    "abcdefghijklm
e8d90 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0d 0a  nopqrstuvwxyz"..
e8da0 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b      "ABCDEFGHIJK
e8db0 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22  LMNOPQRSTUVWXYZ"
e8dc0 0d 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38  ..    "012345678
e8dd0 39 22 3b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  9";..  int i, j;
e8de0 0d 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74  ..  PSZ zTempPat
e8df0 68 43 70 3b 20 20 20 20 20 20 0d 0a 20 20 63 68  hCp;      ..  ch
e8e00 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 43 43 48  ar zTempPath[CCH
e8e10 4d 41 58 50 41 54 48 5d 3b 0d 0a 20 20 55 4c 4f  MAXPATH];..  ULO
e8e20 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 2c 20 75  NG ulDriveNum, u
e8e30 6c 44 72 69 76 65 4d 61 70 3b 0d 0a 20 20 0d 0a  lDriveMap;..  ..
e8e40 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f    /* It's odd to
e8e50 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d   simulate an io-
e8e60 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20  error here, but 
e8e70 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a  really this is j
e8e80 75 73 74 0d 0a 20 20 2a 2a 20 75 73 69 6e 67 20  ust..  ** using 
e8e90 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66  the io-error inf
e8ea0 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74  rastructure to t
e8eb0 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20  est that SQLite 
e8ec0 68 61 6e 64 6c 65 73 20 74 68 69 73 0d 0a 20 20  handles this..  
e8ed0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c  ** function fail
e8ee0 69 6e 67 2e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 53  ing. ..  */..  S
e8ef0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e8f00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e8f10 45 52 52 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20  ERR );....  if( 
e8f20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
e8f30 65 63 74 6f 72 79 20 29 20 7b 0d 0a 20 20 20 20  ectory ) {..    
e8f40 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
e8f50 28 43 43 48 4d 41 58 50 41 54 48 2d 33 30 2c 20  (CCHMAXPATH-30, 
e8f60 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c  zTempPath, "%s",
e8f70 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
e8f80 72 65 63 74 6f 72 79 29 3b 0d 0a 20 20 7d 20 65  rectory);..  } e
e8f90 6c 73 65 20 69 66 28 20 44 6f 73 53 63 61 6e 45  lse if( DosScanE
e8fa0 6e 76 28 20 28 50 53 5a 29 22 54 45 4d 50 22 2c  nv( (PSZ)"TEMP",
e8fb0 20 20 20 26 7a 54 65 6d 70 50 61 74 68 43 70 20     &zTempPathCp 
e8fc0 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c  ) == NO_ERROR ||
e8fd0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 44  ..             D
e8fe0 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29  osScanEnv( (PSZ)
e8ff0 22 54 4d 50 22 2c 20 20 20 20 26 7a 54 65 6d 70  "TMP",    &zTemp
e9000 50 61 74 68 43 70 20 29 20 3d 3d 20 4e 4f 5f 45  PathCp ) == NO_E
e9010 52 52 4f 52 20 7c 7c 0d 0a 20 20 20 20 20 20 20  RROR ||..       
e9020 20 20 20 20 20 20 44 6f 73 53 63 61 6e 45 6e 76        DosScanEnv
e9030 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c  ( (PSZ)"TMPDIR",
e9040 20 26 7a 54 65 6d 70 50 61 74 68 43 70 20 29 20   &zTempPathCp ) 
e9050 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 20 7b 0d  == NO_ERROR ) {.
e9060 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70  .    char *zTemp
e9070 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72  PathUTF = conver
e9080 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 28  tCpPathToUtf8( (
e9090 63 68 61 72 20 2a 29 7a 54 65 6d 70 50 61 74 68  char *)zTempPath
e90a0 43 70 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Cp );..    sqlit
e90b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 43 43 48 4d  e3_snprintf(CCHM
e90c0 41 58 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70  AXPATH-30, zTemp
e90d0 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 54 65 6d  Path, "%s", zTem
e90e0 70 50 61 74 68 55 54 46 29 3b 0d 0a 20 20 20 20  pPathUTF);..    
e90f0 66 72 65 65 28 20 7a 54 65 6d 70 50 61 74 68 55  free( zTempPathU
e9100 54 46 20 29 3b 0d 0a 20 20 7d 20 65 6c 73 65 20  TF );..  } else 
e9110 69 66 28 20 44 6f 73 51 75 65 72 79 43 75 72 72  if( DosQueryCurr
e9120 65 6e 74 44 69 73 6b 28 20 26 75 6c 44 72 69 76  entDisk( &ulDriv
e9130 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61  eNum, &ulDriveMa
e9140 70 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  p ) == NO_ERROR 
e9150 29 20 7b 0d 0a 20 20 20 20 7a 54 65 6d 70 50 61  ) {..    zTempPa
e9160 74 68 5b 30 5d 20 3d 20 28 63 68 61 72 29 28 27  th[0] = (char)('
e9170 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d 20  A' + ulDriveNum 
e9180 2d 20 31 29 3b 0d 0a 20 20 20 20 7a 54 65 6d 70  - 1);..    zTemp
e9190 50 61 74 68 5b 31 5d 20 3d 20 27 3a 27 3b 20 0d  Path[1] = ':'; .
e91a0 0a 20 20 20 20 7a 54 65 6d 70 50 61 74 68 5b 32  .    zTempPath[2
e91b0 5d 20 3d 20 27 5c 30 27 3b 20 0d 0a 20 20 7d 20  ] = '\0'; ..  } 
e91c0 65 6c 73 65 20 7b 0d 0a 20 20 20 20 7a 54 65 6d  else {..    zTem
e91d0 70 50 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b  pPath[0] = '\0';
e91e0 20 0d 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a   ..  }..  ..  /*
e91f0 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61   Strip off a tra
e9200 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72  iling slashes or
e9210 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74   backslashes, ot
e9220 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64  herwise we would
e9230 20 67 65 74 20 2a 0d 0a 20 20 20 2a 20 6d 75 6c   get *..   * mul
e9240 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73  tiple (back)slas
e9250 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73  hes which causes
e9260 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61   DosOpen() to fa
e9270 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  il.             
e9280 20 2a 0d 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e   *..   * Trailin
e9290 67 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74  g spaces are not
e92a0 20 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72   allowed, either
e92b0 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
e92c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d               */.
e92d0 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  j = sqlite3St
e92e0 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68  rlen30(zTempPath
e92f0 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e  );..  while( j >
e9300 20 30 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74   0 && ( zTempPat
e9310 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c  h[j-1] == '\\' |
e9320 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d  | zTempPath[j-1]
e9330 20 3d 3d 20 27 2f 27 20 7c 7c 20 0d 0a 20 20 20   == '/' || ..   
e9340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e9350 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20   zTempPath[j-1] 
e9360 3d 3d 20 27 20 27 20 29 20 29 7b 0d 0a 20 20 20  == ' ' ) ){..   
e9370 20 6a 2d 2d 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 54   j--;..  }..  zT
e9380 65 6d 70 50 61 74 68 5b 6a 5d 20 3d 20 27 5c 30  empPath[j] = '\0
e9390 27 3b 0d 0a 20 20 0d 0a 20 20 2f 2a 20 57 65 20  ';..  ..  /* We 
e93a0 75 73 65 20 32 30 20 62 79 74 65 73 20 74 6f 20  use 20 bytes to 
e93b0 72 61 6e 64 6f 6d 69 7a 65 20 74 68 65 20 6e 61  randomize the na
e93c0 6d 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  me */..  sqlite3
e93d0 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 32  _snprintf(nBuf-2
e93e0 32 2c 20 7a 42 75 66 2c 0d 0a 20 20 20 20 20 20  2, zBuf,..      
e93f0 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73               "%s
e9400 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  \\"SQLITE_TEMP_F
e9410 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d  ILE_PREFIX, zTem
e9420 70 50 61 74 68 29 3b 0d 0a 20 20 6a 20 3d 20 73  pPath);..  j = s
e9430 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
e9440 42 75 66 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  Buf);..  sqlite3
e9450 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c  _randomness( 20,
e9460 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0d 0a 20 20   &zBuf[j] );..  
e9470 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
e9480 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0d  20; i++, j++ ){.
e9490 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 7a  .    zBuf[j] = z
e94a0 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65  Chars[ ((unsigne
e94b0 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25  d char)zBuf[j])%
e94c0 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d  (sizeof(zChars)-
e94d0 31 29 20 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 42  1) ];..  }..  zB
e94e0 75 66 5b 6a 5d 20 3d 20 30 3b 0d 0a 0d 0a 20 20  uf[j] = 0;....  
e94f0 4f 53 54 52 41 43 45 28 28 20 22 54 45 4d 50 20  OSTRACE(( "TEMP 
e9500 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c  FILENAME: %s\n",
e9510 20 7a 42 75 66 20 29 29 3b 0d 0a 20 20 72 65 74   zBuf ));..  ret
e9520 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
e9530 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 75  }....../*..** Tu
e9540 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
e9550 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
e9560 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72  ll pathname.  Wr
e9570 69 74 65 20 74 68 65 20 66 75 6c 6c 0d 0a 2a 2a  ite the full..**
e9580 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a   pathname into z
e9590 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d  Full[].  zFull[]
e95a0 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73   will be at leas
e95b0 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  t pVfs->mxPathna
e95c0 6d 65 0d 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20  me..** bytes in 
e95d0 73 69 7a 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  size...*/..stati
e95e0 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c 50 61 74  c int os2FullPat
e95f0 68 6e 61 6d 65 28 0d 0a 20 20 73 71 6c 69 74 65  hname(..  sqlite
e9600 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
e9610 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
e9620 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a   to vfs object *
e9630 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
e9640 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20  *zRelative,     
e9650 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c   /* Possibly rel
e9660 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68  ative input path
e9670 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46 75 6c 6c   */..  int nFull
e9680 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e9690 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75     /* Size of ou
e96a0 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62  tput buffer in b
e96b0 79 74 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20  ytes */..  char 
e96c0 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20  *zFull          
e96d0 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
e96e0 20 62 75 66 66 65 72 20 2a 2f 0d 0a 29 7b 0d 0a   buffer */..){..
e96f0 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76    char *zRelativ
e9700 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  eCp = convertUtf
e9710 38 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c 61  8PathToCp( zRela
e9720 74 69 76 65 20 29 3b 0d 0a 20 20 63 68 61 72 20  tive );..  char 
e9730 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41  zFullCp[CCHMAXPA
e9740 54 48 5d 20 3d 20 22 5c 30 22 3b 0d 0a 20 20 63  TH] = "\0";..  c
e9750 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0d 0a  har *zFullUTF;..
e9760 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f    APIRET rc = Do
e9770 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20  sQueryPathInfo( 
e9780 28 50 53 5a 29 7a 52 65 6c 61 74 69 76 65 43 70  (PSZ)zRelativeCp
e9790 2c 20 46 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e  , FIL_QUERYFULLN
e97a0 41 4d 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  AME, ..         
e97b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e97c0 20 20 20 20 20 20 20 7a 46 75 6c 6c 43 70 2c 20         zFullCp, 
e97d0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0d 0a 20  CCHMAXPATH );.. 
e97e0 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 76 65   free( zRelative
e97f0 43 70 20 29 3b 0d 0a 20 20 7a 46 75 6c 6c 55 54  Cp );..  zFullUT
e9800 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74  F = convertCpPat
e9810 68 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 70  hToUtf8( zFullCp
e9820 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73   );..  sqlite3_s
e9830 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20  nprintf( nFull, 
e9840 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20  zFull, zFullUTF 
e9850 29 3b 0d 0a 20 20 66 72 65 65 28 20 7a 46 75 6c  );..  free( zFul
e9860 6c 55 54 46 20 29 3b 0d 0a 20 20 72 65 74 75 72  lUTF );..  retur
e9870 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  n rc == NO_ERROR
e9880 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
e9890 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0d 0a 7d 0d  QLITE_IOERR;..}.
e98a0 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e  ...../*..** Open
e98b0 20 61 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74   a file...*/..st
e98c0 61 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e  atic int os2Open
e98d0 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  (..  sqlite3_vfs
e98e0 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
e98f0 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a     /* Not used *
e9900 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
e9910 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
e9920 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
e9930 65 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20 2a  e file (UTF-8) *
e9940 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  /..  sqlite3_fil
e9950 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
e9960 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
e9970 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64  SQLite file hand
e9980 6c 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e  le here */..  in
e9990 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
e99a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
e99b0 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a  pen mode flags *
e99c0 2f 0d 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  /..  int *pOutFl
e99d0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
e99e0 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74     /* Status ret
e99f0 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0d 0a 29 7b  urn flags */..){
e9a00 0d 0a 20 20 48 46 49 4c 45 20 68 3b 0d 0a 20 20  ..  HFILE h;..  
e9a10 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67  ULONG ulOpenFlag
e9a20 73 20 3d 20 30 3b 0d 0a 20 20 55 4c 4f 4e 47 20  s = 0;..  ULONG 
e9a30 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0d  ulOpenMode = 0;.
e9a40 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f  .  ULONG ulActio
e9a50 6e 20 3d 20 30 3b 0d 0a 20 20 55 4c 4f 4e 47 20  n = 0;..  ULONG 
e9a60 72 63 3b 0d 0a 20 20 6f 73 32 46 69 6c 65 20 2a  rc;..  os2File *
e9a70 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
e9a80 2a 29 69 64 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  *)id;..  const c
e9a90 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d  har *zUtf8Name =
e9aa0 20 7a 4e 61 6d 65 3b 0d 0a 20 20 63 68 61 72 20   zName;..  char 
e9ab0 2a 7a 4e 61 6d 65 43 70 3b 0d 0a 20 20 63 68 61  *zNameCp;..  cha
e9ac0 72 20 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d  r  zTmpname[CCHM
e9ad0 41 58 50 41 54 48 5d 3b 0d 0a 0d 0a 20 20 69 6e  AXPATH];....  in
e9ae0 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d  t isExclusive  =
e9af0 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e9b00 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29  _OPEN_EXCLUSIVE)
e9b10 3b 0d 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74  ;..  int isCreat
e9b20 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26  e     = (flags &
e9b30 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
e9b40 41 54 45 29 3b 0d 0a 20 20 69 6e 74 20 69 73 52  ATE);..  int isR
e9b50 65 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61  eadWrite  = (fla
e9b60 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e9b70 5f 52 45 41 44 57 52 49 54 45 29 3b 0d 0a 23 69  _READWRITE);..#i
e9b80 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20  fndef NDEBUG..  
e9b90 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20  int isDelete    
e9ba0 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
e9bb0 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
e9bc0 43 4c 4f 53 45 29 3b 0d 0a 20 20 69 6e 74 20 69  CLOSE);..  int i
e9bd0 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66  sReadonly   = (f
e9be0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
e9bf0 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0d 0a 20  EN_READONLY);.. 
e9c00 20 69 6e 74 20 65 54 79 70 65 20 20 20 20 20 20   int eType      
e9c10 20 20 3d 20 28 66 6c 61 67 73 20 26 20 30 78 46    = (flags & 0xF
e9c20 46 46 46 46 46 30 30 29 3b 0d 0a 20 20 69 6e 74  FFFFF00);..  int
e9c30 20 69 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 20 3d   isOpenJournal =
e9c40 20 28 69 73 43 72 65 61 74 65 20 26 26 20 28 0d   (isCreate && (.
e9c50 0a 20 20 20 20 20 20 20 20 65 54 79 70 65 3d 3d  .        eType==
e9c60 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
e9c70 45 52 5f 4a 4f 55 52 4e 41 4c 20 0d 0a 20 20 20  ER_JOURNAL ..   
e9c80 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
e9c90 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
e9ca0 52 4e 41 4c 20 0d 0a 20 20 20 20 20 7c 7c 20 65  RNAL ..     || e
e9cb0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
e9cc0 4e 5f 57 41 4c 0d 0a 20 20 29 29 3b 0d 0a 23 65  N_WAL..  ));..#e
e9cd0 6e 64 69 66 0d 0a 0d 0a 20 20 55 4e 55 53 45 44  ndif....  UNUSED
e9ce0 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
e9cf0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  ;..  assert( id!
e9d00 3d 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68  =0 );....  /* Ch
e9d10 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  eck the followin
e9d20 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  g statements are
e9d30 20 74 72 75 65 3a 20 0d 0a 20 20 2a 2a 0d 0a 20   true: ..  **.. 
e9d40 20 2a 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c   **   (a) Exactl
e9d50 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41  y one of the REA
e9d60 44 57 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f  DWRITE and READO
e9d70 4e 4c 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62  NLY flags must b
e9d80 65 20 73 65 74 2c 20 61 6e 64 20 0d 0a 20 20 2a  e set, and ..  *
e9d90 2a 20 20 20 28 62 29 20 69 66 20 43 52 45 41 54  *   (b) if CREAT
e9da0 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 52  E is set, then R
e9db0 45 41 44 57 52 49 54 45 20 6d 75 73 74 20 61 6c  EADWRITE must al
e9dc0 73 6f 20 62 65 20 73 65 74 2c 20 61 6e 64 0d 0a  so be set, and..
e9dd0 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 45 58    **   (c) if EX
e9de0 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 2c 20  CLUSIVE is set, 
e9df0 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74  then CREATE must
e9e00 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0d 0a 20   also be set... 
e9e10 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 4c   **   (d) if DEL
e9e20 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65  ETEONCLOSE is se
e9e30 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
e9e40 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
e9e50 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ..  */..  assert
e9e60 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20  ((isReadonly==0 
e9e70 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d  || isReadWrite==
e9e80 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69  0) && (isReadWri
e9e90 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79  te || isReadonly
e9ea0 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 69 73  ));..  assert(is
e9eb0 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52  Create==0 || isR
e9ec0 65 61 64 57 72 69 74 65 29 3b 0d 0a 20 20 61 73  eadWrite);..  as
e9ed0 73 65 72 74 28 69 73 45 78 63 6c 75 73 69 76 65  sert(isExclusive
e9ee0 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29  ==0 || isCreate)
e9ef0 3b 0d 0a 20 20 61 73 73 65 72 74 28 69 73 44 65  ;..  assert(isDe
e9f00 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65  lete==0 || isCre
e9f10 61 74 65 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68  ate);....  /* Th
e9f20 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20  e main DB, main 
e9f30 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c 20 66 69 6c  journal, WAL fil
e9f40 65 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f 75  e and master jou
e9f50 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20 0d  rnal are never .
e9f60 0a 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61  .  ** automatica
e9f70 6c 6c 79 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72  lly deleted. Nor
e9f80 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74   are they ever t
e9f90 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20  emporary files. 
e9fa0 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28   */..  assert( (
e9fb0 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61  !isDelete && zNa
e9fc0 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51  me) || eType!=SQ
e9fd0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
e9fe0 42 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  B );..  assert( 
e9ff0 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e  (!isDelete && zN
ea000 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53  ame) || eType!=S
ea010 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
ea020 4a 4f 55 52 4e 41 4c 20 29 3b 0d 0a 20 20 61 73  JOURNAL );..  as
ea030 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65  sert( (!isDelete
ea040 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54   && zName) || eT
ea050 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype!=SQLITE_OPEN
ea060 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
ea070 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 21  );..  assert( (!
ea080 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d  isDelete && zNam
ea090 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c  e) || eType!=SQL
ea0a0 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0d  ITE_OPEN_WAL );.
ea0b0 0a 0d 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ...  /* Assert t
ea0c0 68 61 74 20 74 68 65 20 75 70 70 65 72 20 6c 61  hat the upper la
ea0d0 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20  yer has set one 
ea0e0 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70  of the "file-typ
ea0f0 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0d 0a 20 20  e" flags. */..  
ea100 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53  assert( eType==S
ea110 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
ea120 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  DB      || eType
ea130 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  ==SQLITE_OPEN_TE
ea140 4d 50 5f 44 42 20 0d 0a 20 20 20 20 20 20 20 7c  MP_DB ..       |
ea150 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
ea160 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
ea170 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49  L || eType==SQLI
ea180 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55  TE_OPEN_TEMP_JOU
ea190 52 4e 41 4c 20 0d 0a 20 20 20 20 20 20 20 7c 7c  RNAL ..       ||
ea1a0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
ea1b0 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20  PEN_SUBJOURNAL  
ea1c0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
ea1d0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
ea1e0 55 52 4e 41 4c 20 0d 0a 20 20 20 20 20 20 20 7c  URNAL ..       |
ea1f0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
ea200 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
ea210 42 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49  B || eType==SQLI
ea220 54 45 5f 4f 50 45 4e 5f 57 41 4c 0d 0a 20 20 29  TE_OPEN_WAL..  )
ea230 3b 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 20 70  ;....  memset( p
ea240 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  File, 0, sizeof(
ea250 2a 70 46 69 6c 65 29 20 29 3b 0d 0a 20 20 70 46  *pFile) );..  pF
ea260 69 6c 65 2d 3e 68 20 3d 20 28 48 46 49 4c 45 29  ile->h = (HFILE)
ea270 2d 31 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74  -1;....  /* If t
ea280 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
ea290 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
ea2a0 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e  ion is NULL, gen
ea2b0 65 72 61 74 65 20 61 20 0d 0a 20 20 2a 2a 20 74  erate a ..  ** t
ea2c0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
ea2d0 6d 65 20 74 6f 20 75 73 65 20 0d 0a 20 20 2a 2f  me to use ..  */
ea2e0 0d 0a 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61  ..  if( !zUtf8Na
ea2f0 6d 65 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  me ){..    asser
ea300 74 28 69 73 44 65 6c 65 74 65 20 26 26 20 21 69  t(isDelete && !i
ea310 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 29 3b 0d 0a  sOpenJournal);..
ea320 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 6d 70      rc = getTemp
ea330 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 54 48 2c  name(CCHMAXPATH,
ea340 20 7a 54 6d 70 6e 61 6d 65 29 3b 0d 0a 20 20 20   zTmpname);..   
ea350 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
ea360 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  OK ){..      ret
ea370 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a  urn rc;..    }..
ea380 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20      zUtf8Name = 
ea390 7a 54 6d 70 6e 61 6d 65 3b 0d 0a 20 20 7d 0d 0a  zTmpname;..  }..
ea3a0 0d 0a 20 20 69 66 28 20 69 73 52 65 61 64 57 72  ..  if( isReadWr
ea3b0 69 74 65 20 29 7b 0d 0a 20 20 20 20 75 6c 4f 70  ite ){..    ulOp
ea3c0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41  enMode |= OPEN_A
ea3d0 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b  CCESS_READWRITE;
ea3e0 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
ea3f0 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
ea400 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e  EN_ACCESS_READON
ea410 4c 59 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  LY;..  }....  /*
ea420 20 4f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20   Open in random 
ea430 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20  access mode for 
ea440 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20  possibly better 
ea450 73 70 65 65 64 2e 20 20 41 6c 6c 6f 77 20 66 75  speed.  Allow fu
ea460 6c 6c 0d 0a 20 20 2a 2a 20 73 68 61 72 69 6e 67  ll..  ** sharing
ea470 20 62 65 63 61 75 73 65 20 66 69 6c 65 20 6c 6f   because file lo
ea480 63 6b 73 20 77 69 6c 6c 20 70 72 6f 76 69 64 65  cks will provide
ea490 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73   exclusive acces
ea4a0 73 20 77 68 65 6e 20 6e 65 65 64 65 64 2e 0d 0a  s when needed...
ea4b0 20 20 2a 2a 20 54 68 65 20 68 61 6e 64 6c 65 20    ** The handle 
ea4c0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 69 6e  should not be in
ea4d0 68 65 72 69 74 65 64 20 62 79 20 63 68 69 6c 64  herited by child
ea4e0 20 70 72 6f 63 65 73 73 65 73 20 61 6e 64 20 77   processes and w
ea4f0 65 20 64 6f 6e 27 74 20 0d 0a 20 20 2a 2a 20 77  e don't ..  ** w
ea500 61 6e 74 20 70 6f 70 75 70 73 20 66 72 6f 6d 20  ant popups from 
ea510 74 68 65 20 63 72 69 74 69 63 61 6c 20 65 72 72  the critical err
ea520 6f 72 20 68 61 6e 64 6c 65 72 2e 0d 0a 20 20 2a  or handler...  *
ea530 2f 0d 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  /..  ulOpenMode 
ea540 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41  |= OPEN_FLAGS_RA
ea550 4e 44 4f 4d 20 7c 20 4f 50 45 4e 5f 53 48 41 52  NDOM | OPEN_SHAR
ea560 45 5f 44 45 4e 59 4e 4f 4e 45 20 7c 20 0d 0a 20  E_DENYNONE | .. 
ea570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f                 O
ea580 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45  PEN_FLAGS_NOINHE
ea590 52 49 54 20 7c 20 4f 50 45 4e 5f 46 4c 41 47 53  RIT | OPEN_FLAGS
ea5a0 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0d  _FAIL_ON_ERROR;.
ea5b0 0a 0d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ...  /* SQLITE_O
ea5c0 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 73  PEN_EXCLUSIVE is
ea5d0 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
ea5e0 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66 69  re that a new fi
ea5f0 6c 65 20 69 73 20 0d 0a 20 20 2a 2a 20 63 72 65  le is ..  ** cre
ea600 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65  ated. SQLite doe
ea610 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69  sn't use it to i
ea620 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69  ndicate "exclusi
ea630 76 65 20 61 63 63 65 73 73 22 20 0d 0a 20 20 2a  ve access" ..  *
ea640 2a 20 61 73 20 69 74 20 69 73 20 75 73 75 61 6c  * as it is usual
ea650 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0d 0a  ly understood...
ea660 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69 73 45 78    */..  if( isEx
ea670 63 6c 75 73 69 76 65 20 29 7b 0d 0a 20 20 20 20  clusive ){..    
ea680 2f 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77  /* Creates a new
ea690 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69   file, only if i
ea6a0 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61  t does not alrea
ea6b0 64 79 20 65 78 69 73 74 2e 20 2a 2f 0d 0a 20 20  dy exist. */..  
ea6c0 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
ea6d0 20 65 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c   exists, it fail
ea6e0 73 2e 20 2a 2f 0d 0a 20 20 20 20 75 6c 4f 70 65  s. */..    ulOpe
ea6f0 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41  nFlags |= OPEN_A
ea700 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 5f  CTION_CREATE_IF_
ea710 4e 45 57 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f  NEW | OPEN_ACTIO
ea720 4e 5f 46 41 49 4c 5f 49 46 5f 45 58 49 53 54 53  N_FAIL_IF_EXISTS
ea730 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  ;..  }else if( i
ea740 73 43 72 65 61 74 65 20 29 7b 0d 0a 20 20 20 20  sCreate ){..    
ea750 2f 2a 20 4f 70 65 6e 20 65 78 69 73 74 69 6e 67  /* Open existing
ea760 20 66 69 6c 65 2c 20 6f 72 20 63 72 65 61 74 65   file, or create
ea770 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 65   if it doesn't e
ea780 78 69 73 74 20 2a 2f 0d 0a 20 20 20 20 75 6c 4f  xist */..    ulO
ea790 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e  penFlags |= OPEN
ea7a0 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49  _ACTION_CREATE_I
ea7b0 46 5f 4e 45 57 20 7c 20 4f 50 45 4e 5f 41 43 54  F_NEW | OPEN_ACT
ea7c0 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53  ION_OPEN_IF_EXIS
ea7d0 54 53 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  TS;..  }else{.. 
ea7e0 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69     /* Opens a fi
ea7f0 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65  le, only if it e
ea800 78 69 73 74 73 2e 20 2a 2f 0d 0a 20 20 20 20 75  xists. */..    u
ea810 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50  lOpenFlags |= OP
ea820 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49  EN_ACTION_FAIL_I
ea830 46 5f 4e 45 57 20 7c 20 4f 50 45 4e 5f 41 43 54  F_NEW | OPEN_ACT
ea840 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53  ION_OPEN_IF_EXIS
ea850 54 53 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 7a 4e  TS;..  }....  zN
ea860 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55  ameCp = convertU
ea870 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 55 74  tf8PathToCp( zUt
ea880 66 38 4e 61 6d 65 20 29 3b 0d 0a 20 20 72 63 20  f8Name );..  rc 
ea890 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29  = DosOpen( (PSZ)
ea8a0 7a 4e 61 6d 65 43 70 2c 0d 0a 20 20 20 20 20 20  zNameCp,..      
ea8b0 20 20 20 20 20 20 20 20 20 20 26 68 2c 0d 0a 20            &h,.. 
ea8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
ea8d0 75 6c 41 63 74 69 6f 6e 2c 0d 0a 20 20 20 20 20  ulAction,..     
ea8e0 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0d 0a             0L,..
ea8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ea900 46 49 4c 45 5f 4e 4f 52 4d 41 4c 2c 0d 0a 20 20  FILE_NORMAL,..  
ea910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c                ul
ea920 4f 70 65 6e 46 6c 61 67 73 2c 0d 0a 20 20 20 20  OpenFlags,..    
ea930 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70              ulOp
ea940 65 6e 4d 6f 64 65 2c 0d 0a 20 20 20 20 20 20 20  enMode,..       
ea950 20 20 20 20 20 20 20 20 20 28 50 45 41 4f 50 32           (PEAOP2
ea960 29 4e 55 4c 4c 20 29 3b 0d 0a 20 20 66 72 65 65  )NULL );..  free
ea970 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0d 0a 0d 0a  ( zNameCp );....
ea980 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45    if( rc != NO_E
ea990 52 52 4f 52 20 29 7b 0d 0a 20 20 20 20 4f 53 54  RROR ){..    OST
ea9a0 52 41 43 45 28 28 20 22 4f 50 45 4e 20 49 6e 76  RACE(( "OPEN Inv
ea9b0 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d 25  alid handle rc=%
ea9c0 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41  d: zName=%s, ulA
ea9d0 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 46 6c  ction=%#lx, ulFl
ea9e0 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65  ags=%#lx, ulMode
ea9f0 3d 25 23 6c 78 5c 6e 22 2c 0d 0a 20 20 20 20 20  =%#lx\n",..     
eaa00 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 55 74           rc, zUt
eaa10 66 38 4e 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e  f8Name, ulAction
eaa20 2c 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75  , ulOpenFlags, u
eaa30 6c 4f 70 65 6e 4d 6f 64 65 20 29 29 3b 0d 0a 0d  lOpenMode ));...
eaa40 0a 20 20 20 20 69 66 28 20 69 73 52 65 61 64 57  .    if( isReadW
eaa50 72 69 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 72  rite ){..      r
eaa60 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28 20 70  eturn os2Open( p
eaa70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 0d  Vfs, zName, id,.
eaa80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
eaa90 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53         ((flags|S
eaaa0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
eaab0 4e 4c 59 29 26 7e 28 53 51 4c 49 54 45 5f 4f 50  NLY)&~(SQLITE_OP
eaac0 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 54 45  EN_CREATE|SQLITE
eaad0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
eaae0 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ),..            
eaaf0 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 6c            pOutFl
eab00 61 67 73 20 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  ags );..    }els
eab10 65 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e{..      return
eab20 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
eab30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
eab40 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73  .  if( pOutFlags
eab50 20 29 7b 0d 0a 20 20 20 20 2a 70 4f 75 74 46 6c   ){..    *pOutFl
eab60 61 67 73 20 3d 20 69 73 52 65 61 64 57 72 69 74  ags = isReadWrit
eab70 65 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  e ? SQLITE_OPEN_
eab80 52 45 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49  READWRITE : SQLI
eab90 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
eaba0 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 6f 73 32 46  ;..  }....  os2F
eabb0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66  ullPathname( pVf
eabc0 73 2c 20 7a 55 74 66 38 4e 61 6d 65 2c 20 73 69  s, zUtf8Name, si
eabd0 7a 65 6f 66 28 20 7a 54 6d 70 6e 61 6d 65 20 29  zeof( zTmpname )
eabe0 2c 20 7a 54 6d 70 6e 61 6d 65 20 29 3b 0d 0a 20  , zTmpname );.. 
eabf0 20 70 46 69 6c 65 2d 3e 7a 46 75 6c 6c 50 61 74   pFile->zFullPat
eac00 68 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  hCp = convertUtf
eac10 38 50 61 74 68 54 6f 43 70 28 20 7a 54 6d 70 6e  8PathToCp( zTmpn
eac20 61 6d 65 20 29 3b 0d 0a 20 20 70 46 69 6c 65 2d  ame );..  pFile-
eac30 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 49  >pMethod = &os2I
eac40 6f 4d 65 74 68 6f 64 3b 0d 0a 20 20 70 46 69 6c  oMethod;..  pFil
eac50 65 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  e->flags = flags
eac60 3b 0d 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  ;..  pFile->h = 
eac70 68 3b 0d 0a 0d 0a 20 20 4f 70 65 6e 43 6f 75 6e  h;....  OpenCoun
eac80 74 65 72 28 2b 31 29 3b 0d 0a 20 20 4f 53 54 52  ter(+1);..  OSTR
eac90 41 43 45 28 28 20 22 4f 50 45 4e 20 25 64 20 70  ACE(( "OPEN %d p
eaca0 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20  OutFlags=%d\n", 
eacb0 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c  pFile->h, pOutFl
eacc0 61 67 73 20 29 29 3b 0d 0a 20 20 72 65 74 75 72  ags ));..  retur
eacd0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
eace0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65  .../*..** Delete
eacf0 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e   the named file.
ead00 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
ead10 20 6f 73 32 44 65 6c 65 74 65 28 0d 0a 20 20 73   os2Delete(..  s
ead20 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
ead30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
ead40 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
ead50 64 20 6f 6e 20 6f 73 32 20 2a 2f 0d 0a 20 20 63  d on os2 */..  c
ead60 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
ead70 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  name,           
ead80 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ead90 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20   file to delete 
eada0 2a 2f 0d 0a 20 20 69 6e 74 20 73 79 6e 63 44 69  */..  int syncDi
eadb0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
eadc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
eadd0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
eade0 2a 2f 0d 0a 29 7b 0d 0a 20 20 41 50 49 52 45 54  */..){..  APIRET
eadf0 20 72 63 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46   rc;..  char *zF
eae00 69 6c 65 6e 61 6d 65 43 70 3b 0d 0a 20 20 53 69  ilenameCp;..  Si
eae10 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
eae20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
eae30 52 52 5f 44 45 4c 45 54 45 20 29 3b 0d 0a 20 20  RR_DELETE );..  
eae40 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f  zFilenameCp = co
eae50 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
eae60 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0d  p( zFilename );.
eae70 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74  .  rc = DosDelet
eae80 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  e( (PSZ)zFilenam
eae90 65 43 70 20 29 3b 0d 0a 20 20 66 72 65 65 28 20  eCp );..  free( 
eaea0 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0d 0a  zFilenameCp );..
eaeb0 20 20 4f 53 54 52 41 43 45 28 28 20 22 44 45 4c    OSTRACE(( "DEL
eaec0 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a  ETE \"%s\"\n", z
eaed0 46 69 6c 65 6e 61 6d 65 20 29 29 3b 0d 0a 20 20  Filename ));..  
eaee0 72 65 74 75 72 6e 20 28 72 63 20 3d 3d 20 4e 4f  return (rc == NO
eaef0 5f 45 52 52 4f 52 20 7c 7c 0d 0a 20 20 20 20 20  _ERROR ||..     
eaf00 20 20 20 20 20 72 63 20 3d 3d 20 45 52 52 4f 52       rc == ERROR
eaf10 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20  _FILE_NOT_FOUND 
eaf20 7c 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63  ||..          rc
eaf30 20 3d 3d 20 45 52 52 4f 52 5f 50 41 54 48 5f 4e   == ERROR_PATH_N
eaf40 4f 54 5f 46 4f 55 4e 44 20 29 20 3f 20 53 51 4c  OT_FOUND ) ? SQL
eaf50 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
eaf60 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0d 0a 7d  IOERR_DELETE;..}
eaf70 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b  ..../*..** Check
eaf80 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61   the existance a
eaf90 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66  nd status of a f
eafa0 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ile...*/..static
eafb0 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0d   int os2Access(.
eafc0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
eafd0 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
eafe0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
eaff0 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
eb000 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
eb010 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
eb020 74 6f 20 63 68 65 63 6b 20 2a 2f 0d 0a 20 20 69  to check */..  i
eb030 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
eb040 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65           /* Type
eb050 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65   of test to make
eb060 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
eb070 0d 0a 20 20 69 6e 74 20 2a 70 4f 75 74 20 20 20  ..  int *pOut   
eb080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
eb090 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20 68   Write results h
eb0a0 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 41 50  ere */..){..  AP
eb0b0 49 52 45 54 20 72 63 3b 0d 0a 20 20 46 49 4c 45  IRET rc;..  FILE
eb0c0 53 54 41 54 55 53 33 20 66 73 74 73 33 43 6f 6e  STATUS3 fsts3Con
eb0d0 66 69 67 49 6e 66 6f 3b 0d 0a 20 20 63 68 61 72  figInfo;..  char
eb0e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 3b 0d 0a   *zFilenameCp;..
eb0f0 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
eb100 45 54 45 52 28 70 56 66 73 29 3b 0d 0a 20 20 53  ETER(pVfs);..  S
eb110 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
eb120 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
eb130 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0d 0a  ERR_ACCESS; );..
eb140 20 20 0d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 43    ..  zFilenameC
eb150 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
eb160 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61  athToCp( zFilena
eb170 6d 65 20 29 3b 0d 0a 20 20 72 63 20 3d 20 44 6f  me );..  rc = Do
eb180 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20  sQueryPathInfo( 
eb190 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70  (PSZ)zFilenameCp
eb1a0 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 0d  , FIL_STANDARD,.
eb1b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
eb1c0 20 20 20 20 20 20 20 20 20 20 26 66 73 74 73 33            &fsts3
eb1d0 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65  ConfigInfo, size
eb1e0 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
eb1f0 29 3b 0d 0a 20 20 66 72 65 65 28 20 7a 46 69 6c  );..  free( zFil
eb200 65 6e 61 6d 65 43 70 20 29 3b 0d 0a 20 20 4f 53  enameCp );..  OS
eb210 54 52 41 43 45 28 28 20 22 41 43 43 45 53 53 20  TRACE(( "ACCESS 
eb220 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e  fsts3ConfigInfo.
eb230 61 74 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 67  attrFile=%d flag
eb240 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0d 0a  s=%d rc=%d\n",..
eb250 20 20 20 20 20 20 20 20 20 20 20 20 66 73 74 73              fsts
eb260 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72  3ConfigInfo.attr
eb270 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 20  File, flags, rc 
eb280 29 29 3b 0d 0a 0d 0a 20 20 73 77 69 74 63 68 28  ));....  switch(
eb290 20 66 6c 61 67 73 20 29 7b 0d 0a 20 20 20 20 63   flags ){..    c
eb2a0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
eb2b0 53 5f 45 58 49 53 54 53 3a 0d 0a 20 20 20 20 20  S_EXISTS:..     
eb2c0 20 2f 2a 20 46 6f 72 20 61 6e 20 53 51 4c 49 54   /* For an SQLIT
eb2d0 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20  E_ACCESS_EXISTS 
eb2e0 71 75 65 72 79 2c 20 74 72 65 61 74 20 61 20 7a  query, treat a z
eb2f0 65 72 6f 2d 6c 65 6e 67 74 68 20 66 69 6c 65 0d  ero-length file.
eb300 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 69 66 20  .      ** as if 
eb310 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  it does not exis
eb320 74 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  t...      */..  
eb330 20 20 20 20 69 66 28 20 66 73 74 73 33 43 6f 6e      if( fsts3Con
eb340 66 69 67 49 6e 66 6f 2e 63 62 46 69 6c 65 20 3d  figInfo.cbFile =
eb350 3d 20 30 20 29 20 0d 0a 20 20 20 20 20 20 20 20  = 0 ) ..        
eb360 72 63 20 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f  rc = ERROR_FILE_
eb370 4e 4f 54 5f 46 4f 55 4e 44 3b 0d 0a 20 20 20 20  NOT_FOUND;..    
eb380 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61    break;..    ca
eb390 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
eb3a0 5f 52 45 41 44 3a 0d 0a 20 20 20 20 20 20 62 72  _READ:..      br
eb3b0 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65 20 53  eak;..    case S
eb3c0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
eb3d0 44 57 52 49 54 45 3a 0d 0a 20 20 20 20 20 20 69  DWRITE:..      i
eb3e0 66 28 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  f( fsts3ConfigIn
eb3f0 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49  fo.attrFile & FI
eb400 4c 45 5f 52 45 41 44 4f 4e 4c 59 20 29 0d 0a 20  LE_READONLY ).. 
eb410 20 20 20 20 20 20 20 72 63 20 3d 20 45 52 52 4f         rc = ERRO
eb420 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 3b  R_ACCESS_DENIED;
eb430 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
eb440 20 20 20 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20      default:..  
eb450 20 20 20 20 72 63 20 3d 20 45 52 52 4f 52 5f 46      rc = ERROR_F
eb460 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 3b 0d 0a  ILE_NOT_FOUND;..
eb470 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22        assert( !"
eb480 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72  Invalid flags ar
eb490 67 75 6d 65 6e 74 22 20 29 3b 0d 0a 20 20 7d 0d  gument" );..  }.
eb4a0 0a 0d 0a 20 20 2a 70 4f 75 74 20 3d 20 28 72 63  ...  *pOut = (rc
eb4b0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0d 0a   == NO_ERROR);..
eb4c0 20 20 4f 53 54 52 41 43 45 28 28 20 22 41 43 43    OSTRACE(( "ACC
eb4d0 45 53 53 20 25 73 20 66 6c 61 67 73 20 25 64 3a  ESS %s flags %d:
eb4e0 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65   rc=%d\n", zFile
eb4f0 6e 61 6d 65 2c 20 66 6c 61 67 73 2c 20 2a 70 4f  name, flags, *pO
eb500 75 74 20 29 29 3b 0d 0a 0d 0a 20 20 72 65 74 75  ut ));....  retu
eb510 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
eb520 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  ......#ifndef SQ
eb530 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
eb540 58 54 45 4e 53 49 4f 4e 0d 0a 2f 2a 0d 0a 2a 2a  XTENSION../*..**
eb550 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
eb560 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
eb570 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
eb580 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0d 0a  g entry points..
eb590 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  ** within the sh
eb5a0 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e  ared library, an
eb5b0 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68  d closing the sh
eb5c0 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0d 0a 2a  ared library...*
eb5d0 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74 65 72 66  /../*..** Interf
eb5e0 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67  aces for opening
eb5f0 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72   a shared librar
eb600 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79  y, finding entry
eb610 20 70 6f 69 6e 74 73 0d 0a 2a 2a 20 77 69 74 68   points..** with
eb620 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
eb630 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
eb640 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
eb650 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  brary...*/..stat
eb660 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70  ic void *os2DlOp
eb670 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
eb680 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
eb690 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0d 0a 20   *zFilename){.. 
eb6a0 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b 0d 0a   HMODULE hmod;..
eb6b0 20 20 41 50 49 52 45 54 20 72 63 3b 0d 0a 20 20    APIRET rc;..  
eb6c0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
eb6d0 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
eb6e0 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d  athToCp(zFilenam
eb6f0 65 29 3b 0d 0a 20 20 72 63 20 3d 20 44 6f 73 4c  e);..  rc = DosL
eb700 6f 61 64 4d 6f 64 75 6c 65 28 4e 55 4c 4c 2c 20  oadModule(NULL, 
eb710 30 2c 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  0, (PSZ)zFilenam
eb720 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0d 0a 20 20  eCp, &hmod);..  
eb730 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70  free(zFilenameCp
eb740 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 20  );..  return rc 
eb750 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20  != NO_ERROR ? 0 
eb760 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0d 0a  : (void*)hmod;..
eb770 7d 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 6e 6f 2d 6f  }../*..** A no-o
eb780 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f  p since the erro
eb790 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
eb7a0 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61  ed on the DosLoa
eb7b0 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0d 0a 2a  dModule call...*
eb7c0 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75  * os2Dlopen retu
eb7d0 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c  rns zero if DosL
eb7e0 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74  oadModule is not
eb7f0 20 73 75 63 63 65 73 73 66 75 6c 2e 0d 0a 2a 2f   successful...*/
eb800 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  ..static void os
eb810 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33  2DlError(sqlite3
eb820 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
eb830 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
eb840 4f 75 74 29 7b 0d 0a 2f 2a 20 6e 6f 2d 6f 70 20  Out){../* no-op 
eb850 2a 2f 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f  */..}..static vo
eb860 69 64 20 28 2a 6f 73 32 44 6c 53 79 6d 28 73 71  id (*os2DlSym(sq
eb870 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
eb880 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20   void *pHandle, 
eb890 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d  const char *zSym
eb8a0 62 6f 6c 29 29 28 76 6f 69 64 29 7b 0d 0a 20 20  bol))(void){..  
eb8b0 50 46 4e 20 70 66 6e 3b 0d 0a 20 20 41 50 49 52  PFN pfn;..  APIR
eb8c0 45 54 20 72 63 3b 0d 0a 20 20 72 63 20 3d 20 44  ET rc;..  rc = D
eb8d0 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28  osQueryProcAddr(
eb8e0 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65  (HMODULE)pHandle
eb8f0 2c 20 30 4c 2c 20 28 50 53 5a 29 7a 53 79 6d 62  , 0L, (PSZ)zSymb
eb900 6f 6c 2c 20 26 70 66 6e 29 3b 0d 0a 20 20 69 66  ol, &pfn);..  if
eb910 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ( rc != NO_ERROR
eb920 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 69 66 20 74   ){..    /* if t
eb930 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c 66  he symbol itself
eb940 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20   was not found, 
eb950 73 65 61 72 63 68 20 61 67 61 69 6e 20 66 6f 72  search again for
eb960 20 74 68 65 20 73 61 6d 65 0d 0a 20 20 20 20 20   the same..     
eb970 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e  * symbol with an
eb980 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f 72   extra underscor
eb990 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  e, that might be
eb9a0 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69 6e   needed dependin
eb9b0 67 0d 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65  g..     * on the
eb9c0 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74   calling convent
eb9d0 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72  ion */..    char
eb9e0 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d   _zSymbol[256] =
eb9f0 20 22 5f 22 3b 0d 0a 20 20 20 20 73 74 72 6e 63   "_";..    strnc
eba00 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79  at(_zSymbol, zSy
eba10 6d 62 6f 6c 2c 20 32 35 34 29 3b 0d 0a 20 20 20  mbol, 254);..   
eba20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72   rc = DosQueryPr
eba30 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29  ocAddr((HMODULE)
eba40 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 28 50 53  pHandle, 0L, (PS
eba50 5a 29 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e  Z)_zSymbol, &pfn
eba60 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  );..  }..  retur
eba70 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  n rc != NO_ERROR
eba80 20 3f 20 30 20 3a 20 28 76 6f 69 64 28 2a 29 28   ? 0 : (void(*)(
eba90 76 6f 69 64 29 29 70 66 6e 3b 0d 0a 7d 0d 0a 73  void))pfn;..}..s
ebaa0 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c  tatic void os2Dl
ebab0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
ebac0 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
ebad0 48 61 6e 64 6c 65 29 7b 0d 0a 20 20 44 6f 73 46  Handle){..  DosF
ebae0 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55  reeModule((HMODU
ebaf0 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0d 0a 7d 0d  LE)pHandle);..}.
ebb00 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c  .#else /* if SQL
ebb10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
ebb20 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e  TENSION is defin
ebb30 65 64 3a 20 2a 2f 0d 0a 20 20 23 64 65 66 69 6e  ed: */..  #defin
ebb40 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0d 0a 20  e os2DlOpen 0.. 
ebb50 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72   #define os2DlEr
ebb60 72 6f 72 20 30 0d 0a 20 20 23 64 65 66 69 6e 65  ror 0..  #define
ebb70 20 6f 73 32 44 6c 53 79 6d 20 30 0d 0a 20 20 23   os2DlSym 0..  #
ebb80 64 65 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73  define os2DlClos
ebb90 65 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  e 0..#endif.....
ebba0 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 75 70  ./*..** Write up
ebbb0 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f   to nBuf bytes o
ebbc0 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74  f randomness int
ebbd0 6f 20 7a 42 75 66 2e 0d 0a 2a 2f 0d 0a 73 74 61  o zBuf...*/..sta
ebbe0 74 69 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f  tic int os2Rando
ebbf0 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
ebc00 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
ebc10 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b  f, char *zBuf ){
ebc20 0d 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0d 0a  ..  int n = 0;..
ebc30 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
ebc40 54 45 5f 54 45 53 54 29 0d 0a 20 20 6e 20 3d 20  TE_TEST)..  n = 
ebc50 6e 42 75 66 3b 0d 0a 20 20 6d 65 6d 73 65 74 28  nBuf;..  memset(
ebc60 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0d  zBuf, 0, nBuf);.
ebc70 0a 23 65 6c 73 65 0d 0a 20 20 69 6e 74 20 69 3b  .#else..  int i;
ebc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebc90 20 20 20 20 20 20 20 20 20 20 20 0d 0a 20 20 50             ..  P
ebca0 50 49 42 20 70 70 69 62 3b 0d 0a 20 20 50 54 49  PIB ppib;..  PTI
ebcb0 42 20 70 74 69 62 3b 0d 0a 20 20 44 41 54 45 54  B ptib;..  DATET
ebcc0 49 4d 45 20 64 74 3b 20 0d 0a 20 20 73 74 61 74  IME dt; ..  stat
ebcd0 69 63 20 75 6e 73 69 67 6e 65 64 20 63 20 3d 20  ic unsigned c = 
ebce0 30 3b 0d 0a 20 20 2f 2a 20 4f 72 64 65 72 65 64  0;..  /* Ordered
ebcf0 20 62 79 20 76 61 72 69 61 74 69 6f 6e 20 70 72   by variation pr
ebd00 6f 62 61 62 69 6c 69 74 79 20 2a 2f 0d 0a 20 20  obability */..  
ebd10 73 74 61 74 69 63 20 55 4c 4f 4e 47 20 73 76 49  static ULONG svI
ebd20 64 78 5b 36 5d 20 3d 20 7b 20 51 53 56 5f 4d 53  dx[6] = { QSV_MS
ebd30 5f 43 4f 55 4e 54 2c 20 51 53 56 5f 54 49 4d 45  _COUNT, QSV_TIME
ebd40 5f 4c 4f 57 2c 0d 0a 20 20 20 20 20 20 20 20 20  _LOW,..         
ebd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebd60 20 20 20 51 53 56 5f 4d 41 58 50 52 4d 45 4d 2c     QSV_MAXPRMEM,
ebd70 20 51 53 56 5f 4d 41 58 53 48 4d 45 4d 2c 0d 0a   QSV_MAXSHMEM,..
ebd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebd90 20 20 20 20 20 20 20 20 20 20 20 20 51 53 56 5f              QSV_
ebda0 54 4f 54 41 56 41 49 4c 4d 45 4d 2c 20 51 53 56  TOTAVAILMEM, QSV
ebdb0 5f 54 4f 54 52 45 53 4d 45 4d 20 7d 3b 0d 0a 0d  _TOTRESMEM };...
ebdc0 0a 20 20 2f 2a 20 38 20 62 79 74 65 73 3b 20 74  .  /* 8 bytes; t
ebdd0 69 6d 65 7a 6f 6e 65 20 61 6e 64 20 77 65 65 6b  imezone and week
ebde0 64 61 79 20 64 6f 6e 27 74 20 69 6e 63 72 65 61  day don't increa
ebdf0 73 65 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73  se the randomnes
ebe00 73 20 6d 75 63 68 20 2a 2f 0d 0a 20 20 69 66 28  s much */..  if(
ebe10 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 64 74 29   (int)sizeof(dt)
ebe20 2d 33 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  -3 <= nBuf - n )
ebe30 7b 0d 0a 20 20 20 20 63 20 2b 3d 20 30 78 30 31  {..    c += 0x01
ebe40 30 30 3b 0d 0a 20 20 20 20 44 6f 73 47 65 74 44  00;..    DosGetD
ebe50 61 74 65 54 69 6d 65 28 26 64 74 29 3b 0d 0a 20  ateTime(&dt);.. 
ebe60 20 20 20 64 74 2e 79 65 61 72 20 3d 20 28 55 53     dt.year = (US
ebe70 48 4f 52 54 29 28 28 64 74 2e 79 65 61 72 20 2d  HORT)((dt.year -
ebe80 20 31 39 30 30 29 20 7c 20 63 29 3b 0d 0a 20 20   1900) | c);..  
ebe90 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
ebea0 5d 2c 20 26 64 74 2c 20 73 69 7a 65 6f 66 28 64  ], &dt, sizeof(d
ebeb0 74 29 2d 33 29 3b 0d 0a 20 20 20 20 6e 20 2b 3d  t)-3);..    n +=
ebec0 20 73 69 7a 65 6f 66 28 64 74 29 2d 33 3b 0d 0a   sizeof(dt)-3;..
ebed0 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 34 20 62 79    }....  /* 4 by
ebee0 74 65 73 3b 20 50 49 44 73 20 61 6e 64 20 54 49  tes; PIDs and TI
ebef0 44 73 20 61 72 65 20 31 36 20 62 69 74 20 69 6e  Ds are 16 bit in
ebf00 74 65 72 6e 61 6c 6c 79 2c 20 73 6f 20 63 6f 6d  ternally, so com
ebf10 62 69 6e 65 20 74 68 65 6d 20 2a 2f 0d 0a 20 20  bine them */..  
ebf20 69 66 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28  if( (int)sizeof(
ebf30 55 4c 4f 4e 47 29 20 3c 3d 20 6e 42 75 66 20 2d  ULONG) <= nBuf -
ebf40 20 6e 20 29 7b 0d 0a 20 20 20 20 44 6f 73 47 65   n ){..    DosGe
ebf50 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69  tInfoBlocks(&pti
ebf60 62 2c 20 26 70 70 69 62 29 3b 0d 0a 20 20 20 20  b, &ppib);..    
ebf70 2a 28 50 55 4c 4f 4e 47 29 26 7a 42 75 66 5b 6e  *(PULONG)&zBuf[n
ebf80 5d 20 3d 20 4d 41 4b 45 4c 4f 4e 47 28 70 70 69  ] = MAKELONG(ppi
ebf90 62 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 0d 0a 20  b->pib_ulpid,.. 
ebfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebfc0 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d  ptib->tib_ptib2-
ebfd0 3e 74 69 62 32 5f 75 6c 74 69 64 29 3b 0d 0a 20  >tib2_ultid);.. 
ebfe0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 55     n += sizeof(U
ebff0 4c 4f 4e 47 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  LONG);..  }.... 
ec000 20 2f 2a 20 55 70 20 74 6f 20 36 20 2a 20 34 20   /* Up to 6 * 4 
ec010 62 79 74 65 73 3b 20 76 61 72 69 61 62 6c 65 73  bytes; variables
ec020 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 73   depend on the s
ec030 79 73 74 65 6d 20 73 74 61 74 65 20 2a 2f 0d 0a  ystem state */..
ec040 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20    for( i = 0; i 
ec050 3c 20 36 20 26 26 20 28 69 6e 74 29 73 69 7a 65  < 6 && (int)size
ec060 6f 66 28 55 4c 4f 4e 47 29 20 3c 3d 20 6e 42 75  of(ULONG) <= nBu
ec070 66 20 2d 20 6e 3b 20 69 2b 2b 20 29 7b 0d 0a 20  f - n; i++ ){.. 
ec080 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 49 6e     DosQuerySysIn
ec090 66 6f 28 73 76 49 64 78 5b 69 5d 2c 20 73 76 49  fo(svIdx[i], svI
ec0a0 64 78 5b 69 5d 2c 20 0d 0a 20 20 20 20 20 20 20  dx[i], ..       
ec0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 55               (PU
ec0c0 4c 4f 4e 47 29 26 7a 42 75 66 5b 6e 5d 2c 20 73  LONG)&zBuf[n], s
ec0d0 69 7a 65 6f 66 28 55 4c 4f 4e 47 29 29 3b 0d 0a  izeof(ULONG));..
ec0e0 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28      n += sizeof(
ec0f0 55 4c 4f 4e 47 29 3b 0d 0a 20 20 7d 20 0d 0a 23  ULONG);..  } ..#
ec100 65 6e 64 69 66 0d 0a 0d 0a 20 20 72 65 74 75 72  endif....  retur
ec110 6e 20 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n n;..}..../*..*
ec120 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69  * Sleep for a li
ec130 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74  ttle while.  Ret
ec140 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  urn the amount o
ec150 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0d 0a 2a  f time slept...*
ec160 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
ec170 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
ec180 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20  microseconds we 
ec190 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0d 0a  want to sleep...
ec1a0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
ec1b0 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  lue is the numbe
ec1c0 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
ec1d0 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
ec1e0 6c 6c 79 0d 0a 2a 2a 20 72 65 71 75 65 73 74 65  lly..** requeste
ec1f0 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72  d from the under
ec200 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
ec210 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72  system, a number
ec220 20 77 68 69 63 68 0d 0a 2a 2a 20 6d 69 67 68 74   which..** might
ec230 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
ec240 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
ec250 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
ec260 6f 74 20 6c 65 73 73 0d 0a 2a 2a 20 74 68 61 6e  ot less..** than
ec270 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0d 0a   the argument...
ec280 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  */..static int o
ec290 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33  s2Sleep( sqlite3
ec2a0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
ec2b0 6d 69 63 72 6f 73 65 63 20 29 7b 0d 0a 20 20 44  microsec ){..  D
ec2c0 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73  osSleep( (micros
ec2d0 65 63 2f 31 30 30 30 29 20 29 3b 0d 0a 20 20 72  ec/1000) );..  r
ec2e0 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0d  eturn microsec;.
ec2f0 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  .}..../*..** The
ec300 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
ec310 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61  ble, if set to a
ec320 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
ec330 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73   becomes the res
ec340 75 6c 74 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ult..** returned
ec350 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43   from sqlite3OsC
ec360 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54  urrentTime().  T
ec370 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
ec380 74 65 73 74 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23 69  testing...*/..#i
ec390 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
ec3a0 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
ec3b0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ec3c0 5f 74 69 6d 65 20 3d 20 30 3b 0d 0a 23 65 6e 64  _time = 0;..#end
ec3d0 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e  if..../*..** Fin
ec3e0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69  d the current ti
ec3f0 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c  me (in Universal
ec400 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d   Coordinated Tim
ec410 65 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20  e).  Write into 
ec420 2a 70 69 4e 6f 77 0d 0a 2a 2a 20 74 68 65 20 63  *piNow..** the c
ec430 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
ec440 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
ec450 20 44 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65   Day number time
ec460 73 20 38 36 5f 34 30 30 5f 30 30 30 2e 20 20 49  s 86_400_000.  I
ec470 6e 0d 0a 2a 2a 20 6f 74 68 65 72 20 77 6f 72 64  n..** other word
ec480 73 2c 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 70  s, write into *p
ec490 69 4e 6f 77 20 74 68 65 20 6e 75 6d 62 65 72 20  iNow the number 
ec4a0 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  of milliseconds 
ec4b0 73 69 6e 63 65 20 74 68 65 20 4a 75 6c 69 61 6e  since the Julian
ec4c0 0d 0a 2a 2a 20 65 70 6f 63 68 20 6f 66 20 6e 6f  ..** epoch of no
ec4d0 6f 6e 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20  on in Greenwich 
ec4e0 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20  on November 24, 
ec4f0 34 37 31 34 20 42 2e 43 20 61 63 63 6f 72 64 69  4714 B.C accordi
ec500 6e 67 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 72  ng to the..** pr
ec510 6f 6c 65 70 74 69 63 20 47 72 65 67 6f 72 69 61  oleptic Gregoria
ec520 6e 20 63 61 6c 65 6e 64 61 72 2e 0d 0a 2a 2a 0d  n calendar...**.
ec530 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
ec540 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72  return 0.  Retur
ec550 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20  n 1 if the time 
ec560 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20  and date cannot 
ec570 62 65 20 66 6f 75 6e 64 2e 0d 0a 2a 2f 0d 0a 73  be found...*/..s
ec580 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 75 72  tatic int os2Cur
ec590 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71  rentTimeInt64(sq
ec5a0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
ec5b0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
ec5c0 70 69 4e 6f 77 29 7b 0d 0a 23 69 66 64 65 66 20  piNow){..#ifdef 
ec5d0 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 73  SQLITE_TEST..  s
ec5e0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
ec5f0 74 65 33 5f 69 6e 74 36 34 20 75 6e 69 78 45 70  te3_int64 unixEp
ec600 6f 63 68 20 3d 20 32 34 34 30 35 38 37 35 2a 28  och = 24405875*(
ec610 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36  sqlite3_int64)86
ec620 34 30 30 30 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a  40000;..#endif..
ec630 20 20 69 6e 74 20 79 65 61 72 2c 20 6d 6f 6e 74    int year, mont
ec640 68 2c 20 64 61 74 65 70 61 72 74 2c 20 74 69 6d  h, datepart, tim
ec650 65 70 61 72 74 3b 0d 0a 20 0d 0a 20 20 44 41 54  epart;.. ..  DAT
ec660 45 54 49 4d 45 20 64 74 3b 0d 0a 20 20 44 6f 73  ETIME dt;..  Dos
ec670 47 65 74 44 61 74 65 54 69 6d 65 28 20 26 64 74  GetDateTime( &dt
ec680 20 29 3b 0d 0a 0d 0a 20 20 79 65 61 72 20 3d 20   );....  year = 
ec690 64 74 2e 79 65 61 72 3b 0d 0a 20 20 6d 6f 6e 74  dt.year;..  mont
ec6a0 68 20 3d 20 64 74 2e 6d 6f 6e 74 68 3b 0d 0a 0d  h = dt.month;...
ec6b0 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f  .  /* Calculatio
ec6c0 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77  ns from http://w
ec6d0 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61  ww.astro.keele.a
ec6e0 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e  c.uk/~rno/Astron
ec6f0 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0d 0a 20 20  omy/hjd.html..  
ec700 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73  ** http://www.as
ec710 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f  tro.keele.ac.uk/
ec720 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68  ~rno/Astronomy/h
ec730 6a 64 2d 30 2e 31 2e 63 0d 0a 20 20 2a 2a 20 43  jd-0.1.c..  ** C
ec740 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c  alculate the Jul
ec750 69 61 6e 20 64 61 79 73 0d 0a 20 20 2a 2f 0d 0a  ian days..  */..
ec760 20 20 64 61 74 65 70 61 72 74 20 3d 20 28 69 6e    datepart = (in
ec770 74 29 64 74 2e 64 61 79 20 2d 20 33 32 30 37 36  t)dt.day - 32076
ec780 20 2b 0d 0a 20 20 20 20 31 34 36 31 2a 28 79 65   +..    1461*(ye
ec790 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e  ar + 4800 + (mon
ec7a0 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b  th - 14)/12)/4 +
ec7b0 0d 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68  ..    367*(month
ec7c0 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20   - 2 - (month - 
ec7d0 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0d  14)/12*12)/12 -.
ec7e0 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20  .    3*((year + 
ec7f0 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4900 + (month - 
ec800 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0d  14)/12)/100)/4;.
ec810 0a 0d 0a 20 20 2f 2a 20 54 69 6d 65 20 69 6e 20  ...  /* Time in 
ec820 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20 68 6f  milliseconds, ho
ec830 75 72 73 20 74 6f 20 6e 6f 6f 6e 20 61 64 64 65  urs to noon adde
ec840 64 20 2a 2f 0d 0a 20 20 74 69 6d 65 70 61 72 74  d */..  timepart
ec850 20 3d 20 31 32 2a 33 36 30 30 2a 31 30 30 30 20   = 12*3600*1000 
ec860 2b 20 64 74 2e 68 75 6e 64 72 65 64 74 68 73 2a  + dt.hundredths*
ec870 31 30 20 2b 20 64 74 2e 73 65 63 6f 6e 64 73 2a  10 + dt.seconds*
ec880 31 30 30 30 20 2b 0d 0a 20 20 20 20 28 28 69 6e  1000 +..    ((in
ec890 74 29 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64  t)dt.minutes + d
ec8a0 74 2e 74 69 6d 65 7a 6f 6e 65 29 2a 36 30 2a 31  t.timezone)*60*1
ec8b0 30 30 30 20 2b 20 64 74 2e 68 6f 75 72 73 2a 33  000 + dt.hours*3
ec8c0 36 30 30 2a 31 30 30 30 3b 0d 0a 0d 0a 20 20 2a  600*1000;....  *
ec8d0 70 69 4e 6f 77 20 3d 20 28 73 71 6c 69 74 65 33  piNow = (sqlite3
ec8e0 5f 69 6e 74 36 34 29 64 61 74 65 70 61 72 74 2a  _int64)datepart*
ec8f0 38 36 34 30 30 2a 31 30 30 30 20 2b 20 74 69 6d  86400*1000 + tim
ec900 65 70 61 72 74 3b 0d 0a 20 20 20 0d 0a 23 69 66  epart;..   ..#if
ec910 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d  def SQLITE_TEST.
ec920 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
ec930 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0d 0a  urrent_time ){..
ec940 20 20 20 20 2a 70 69 4e 6f 77 20 3d 20 31 30 30      *piNow = 100
ec950 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  0*(sqlite3_int64
ec960 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74  )sqlite3_current
ec970 5f 74 69 6d 65 20 2b 20 75 6e 69 78 45 70 6f 63  _time + unixEpoc
ec980 68 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  h;..  }..#endif.
ec990 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ...  UNUSED_PARA
ec9a0 4d 45 54 45 52 28 70 56 66 73 29 3b 0d 0a 20 20  METER(pVfs);..  
ec9b0 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a  return 0;..}....
ec9c0 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  /*..** Find the 
ec9d0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e  current time (in
ec9e0 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64   Universal Coord
ec9f0 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57  inated Time).  W
eca00 72 69 74 65 20 74 68 65 0d 0a 2a 2a 20 63 75 72  rite the..** cur
eca10 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61  rent time and da
eca20 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44  te as a Julian D
eca30 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a  ay number into *
eca40 70 72 4e 6f 77 20 61 6e 64 0d 0a 2a 2a 20 72 65  prNow and..** re
eca50 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20  turn 0.  Return 
eca60 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e  1 if the time an
eca70 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65  d date cannot be
eca80 20 66 6f 75 6e 64 2e 0d 0a 2a 2f 0d 0a 73 74 61   found...*/..sta
eca90 74 69 63 20 69 6e 74 20 6f 73 32 43 75 72 72 65  tic int os2Curre
ecaa0 6e 74 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f  ntTime( sqlite3_
ecab0 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c  vfs *pVfs, doubl
ecac0 65 20 2a 70 72 4e 6f 77 20 29 7b 0d 0a 20 20 69  e *prNow ){..  i
ecad0 6e 74 20 72 63 3b 0d 0a 20 20 73 71 6c 69 74 65  nt rc;..  sqlite
ecae0 33 5f 69 6e 74 36 34 20 69 3b 0d 0a 20 20 72 63  3_int64 i;..  rc
ecaf0 20 3d 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d   = os2CurrentTim
ecb00 65 49 6e 74 36 34 28 70 56 66 73 2c 20 26 69 29  eInt64(pVfs, &i)
ecb10 3b 0d 0a 20 20 69 66 28 20 21 72 63 20 29 7b 0d  ;..  if( !rc ){.
ecb20 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 69 2f  .    *prNow = i/
ecb30 38 36 34 30 30 30 30 30 2e 30 3b 0d 0a 20 20 7d  86400000.0;..  }
ecb40 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
ecb50 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  }..../*..** The 
ecb60 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69  idea is that thi
ecb70 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73  s function works
ecb80 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74   like a combinat
ecb90 69 6f 6e 20 6f 66 0d 0a 2a 2a 20 47 65 74 4c 61  ion of..** GetLa
ecba0 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f  stError() and Fo
ecbb0 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e  rmatMessage() on
ecbc0 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72   windows (or err
ecbd0 6e 6f 20 61 6e 64 0d 0a 2a 2a 20 73 74 72 65 72  no and..** strer
ecbe0 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29  ror_r() on unix)
ecbf0 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72  . After an error
ecc00 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20   is returned by 
ecc10 61 6e 20 4f 53 0d 0a 2a 2a 20 66 75 6e 63 74 69  an OS..** functi
ecc20 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73  on, SQLite calls
ecc30 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
ecc40 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e  ith zBuf pointin
ecc50 67 20 74 6f 0d 0a 2a 2a 20 61 20 62 75 66 66 65  g to..** a buffe
ecc60 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 2e  r of nBuf bytes.
ecc70 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 68   The OS layer sh
ecc80 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 68  ould populate th
ecc90 65 0d 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74  e..** buffer wit
ecca0 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  h a nul-terminat
eccb0 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  ed UTF-8 encoded
eccc0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0d 0a   error message..
eccd0 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ** describing th
ecce0 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20  e last IO error 
eccf0 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  to have occurred
ecd00 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c   within the call
ecd10 69 6e 67 0d 0a 2a 2a 20 74 68 72 65 61 64 2e 0d  ing..** thread..
ecd20 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 65  .**..** If the e
ecd30 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
ecd40 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 68  too large for th
ecd50 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 65  e supplied buffe
ecd60 72 2c 0d 0a 2a 2a 20 69 74 20 73 68 6f 75 6c 64  r,..** it should
ecd70 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 54   be truncated. T
ecd80 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
ecd90 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  of xGetLastError
ecda0 0d 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20  ..** is zero if 
ecdb0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
ecdc0 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75  e fits in the bu
ecdd0 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72  ffer, or non-zer
ecde0 6f 0d 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20  o..** otherwise 
ecdf0 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20  (if the message 
ece00 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20  was truncated). 
ece10 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72  If non-zero is r
ece20 65 74 75 72 6e 65 64 2c 0d 0a 2a 2a 20 74 68 65  eturned,..** the
ece30 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65  n it is not nece
ece40 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65  ssary to include
ece50 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61   the nul-termina
ece60 74 6f 72 20 63 68 61 72 61 63 74 65 72 0d 0a 2a  tor character..*
ece70 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  * in the output 
ece80 62 75 66 66 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  buffer...**..** 
ece90 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e  Not supplying an
ecea0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
eceb0 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65  ill have no adve
ecec0 72 73 65 20 65 66 66 65 63 74 0d 0a 2a 2a 20 6f  rse effect..** o
eced0 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20  n SQLite. It is 
ecee0 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20  fine to have an 
ecef0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
ecf00 68 61 74 20 6e 65 76 65 72 0d 0a 2a 2a 20 72 65  hat never..** re
ecf10 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d  turns an error m
ecf20 65 73 73 61 67 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essage:..**..** 
ecf30 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 72    int xGetLastEr
ecf40 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
ecf50 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
ecf60 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0d 0a 2a   char *zBuf){..*
ecf70 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 75  *     assert(zBu
ecf80 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0d 0a 2a 2a  f[0]=='\0');..**
ecf90 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a       return 0;..
ecfa0 2a 2a 20 20 20 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 48  **   }..**..** H
ecfb0 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 72 72  owever if an err
ecfc0 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 73 75  or message is su
ecfd0 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c 6c 20  pplied, it will 
ecfe0 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 64 0d  be incorporated.
ecff0 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 6e  .** by sqlite in
ed000 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  to the error mes
ed010 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 74  sage available t
ed020 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e 67  o the user using
ed030 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72  ..** sqlite3_err
ed040 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20  msg(), possibly 
ed050 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73  making IO errors
ed060 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67   easier to debug
ed070 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
ed080 74 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 6f  t os2GetLastErro
ed090 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
ed0a0 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  Vfs, int nBuf, c
ed0b0 68 61 72 20 2a 7a 42 75 66 29 7b 0d 0a 20 20 61  har *zBuf){..  a
ed0c0 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27  ssert(zBuf[0]=='
ed0d0 5c 30 27 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  \0');..  return 
ed0e0 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
ed0f0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
ed100 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
ed110 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
ed120 20 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2f 0d   interface...*/.
ed130 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
ed140 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
ed150 76 6f 69 64 29 7b 0d 0a 20 20 73 74 61 74 69 63  void){..  static
ed160 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 73 32   sqlite3_vfs os2
ed170 56 66 73 20 3d 20 7b 0d 0a 20 20 20 20 33 2c 20  Vfs = {..    3, 
ed180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ed190 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0d 0a  /* iVersion */..
ed1a0 20 20 20 20 73 69 7a 65 6f 66 28 6f 73 32 46 69      sizeof(os2Fi
ed1b0 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69  le),   /* szOsFi
ed1c0 6c 65 20 2a 2f 0d 0a 20 20 20 20 43 43 48 4d 41  le */..    CCHMA
ed1d0 58 50 41 54 48 2c 20 20 20 20 20 20 20 20 2f 2a  XPATH,        /*
ed1e0 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0d 0a   mxPathname */..
ed1f0 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
ed200 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
ed210 2a 2f 0d 0a 20 20 20 20 22 6f 73 32 22 2c 20 20  */..    "os2",  
ed220 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
ed230 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20  ame */..    0,  
ed240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ed250 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0d 0a 0d  * pAppData */...
ed260 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20  .    os2Open,   
ed270 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
ed280 20 2a 2f 0d 0a 20 20 20 20 6f 73 32 44 65 6c 65   */..    os2Dele
ed290 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  te,         /* x
ed2a0 44 65 6c 65 74 65 20 2a 2f 0d 0a 20 20 20 20 6f  Delete */..    o
ed2b0 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20  s2Access,       
ed2c0 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0d    /* xAccess */.
ed2d0 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68  .    os2FullPath
ed2e0 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c  name,   /* xFull
ed2f0 50 61 74 68 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20  Pathname */..   
ed300 20 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20   os2DlOpen,     
ed310 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a      /* xDlOpen *
ed320 2f 0d 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f  /..    os2DlErro
ed330 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c  r,        /* xDl
ed340 45 72 72 6f 72 20 2a 2f 0d 0a 20 20 20 20 6f 73  Error */..    os
ed350 32 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20  2DlSym,         
ed360 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0d 0a 20   /* xDlSym */.. 
ed370 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c 20 20     os2DlClose,  
ed380 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73        /* xDlClos
ed390 65 20 2a 2f 0d 0a 20 20 20 20 6f 73 32 52 61 6e  e */..    os2Ran
ed3a0 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20  domness,     /* 
ed3b0 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0d 0a  xRandomness */..
ed3c0 20 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20 20      os2Sleep,   
ed3d0 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70         /* xSleep
ed3e0 20 2a 2f 0d 0a 20 20 20 20 6f 73 32 43 75 72 72   */..    os2Curr
ed3f0 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78  entTime,    /* x
ed400 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0d 0a  CurrentTime */..
ed410 20 20 20 20 6f 73 32 47 65 74 4c 61 73 74 45 72      os2GetLastEr
ed420 72 6f 72 2c 20 20 20 2f 2a 20 78 47 65 74 4c 61  ror,   /* xGetLa
ed430 73 74 45 72 72 6f 72 20 2a 2f 0d 0a 20 20 20 20  stError */..    
ed440 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 49 6e  os2CurrentTimeIn
ed450 74 36 34 2c 20 2f 2a 20 78 43 75 72 72 65 6e 74  t64, /* xCurrent
ed460 54 69 6d 65 49 6e 74 36 34 20 2a 2f 0d 0a 20 20  TimeInt64 */..  
ed470 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
ed480 20 20 20 20 20 2f 2a 20 78 53 65 74 53 79 73 74       /* xSetSyst
ed490 65 6d 43 61 6c 6c 20 2a 2f 0d 0a 20 20 20 20 30  emCall */..    0
ed4a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
ed4b0 20 20 2f 2a 20 78 47 65 74 53 79 73 74 65 6d 43    /* xGetSystemC
ed4c0 61 6c 6c 20 2a 2f 0d 0a 20 20 20 20 30 20 20 20  all */..    0   
ed4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ed4e0 2a 20 78 4e 65 78 74 53 79 73 74 65 6d 43 61 6c  * xNextSystemCal
ed4f0 6c 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 20 20 73 71  l */..  };..  sq
ed500 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
ed510 65 72 28 26 6f 73 32 56 66 73 2c 20 31 29 3b 0d  er(&os2Vfs, 1);.
ed520 0a 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65  .  initUconvObje
ed530 63 74 73 28 29 3b 0d 0a 2f 2a 20 20 73 71 6c 69  cts();../*  sqli
ed540 74 65 33 4f 53 54 72 61 63 65 20 3d 20 31 3b 20  te3OSTrace = 1; 
ed550 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  */..  return SQL
ed560 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 53 51 4c 49  ITE_OK;..}..SQLI
ed570 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
ed580 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b  e3_os_end(void){
ed590 0d 0a 20 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a  ..  freeUconvObj
ed5a0 65 63 74 73 28 29 3b 0d 0a 20 20 72 65 74 75 72  ects();..  retur
ed5b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
ed5c0 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  ...#endif /* SQL
ed5d0 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0d 0a 0d  ITE_OS_OS2 */...
ed5e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ed5f0 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e 63   End of os_os2.c
ed600 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
ed610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ed630 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ed640 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
ed650 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
ed660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed680 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 34 20 4d  /../*..** 2004 M
ed690 61 79 20 32 32 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ay 22..**..** Th
ed6a0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
ed6b0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
ed6c0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
ed6d0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
ed6e0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
ed6f0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
ed700 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
ed710 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
ed720 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
ed730 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
ed740 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
ed750 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
ed760 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
ed770 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
ed780 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
ed790 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
ed7a0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
ed7b0 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
ed7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed800 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  **..**..** This 
ed810 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
ed820 65 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  e VFS implementa
ed830 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78 2d 6c 69  tion for unix-li
ed840 6b 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ke operating sys
ed850 74 65 6d 73 0d 0a 2a 2a 20 69 6e 63 6c 75 64 65  tems..** include
ed860 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58 2c 20   Linux, MacOSX, 
ed870 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57 6f 72  *BSD, QNX, VxWor
ed880 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c 20 61  ks, AIX, HPUX, a
ed890 6e 64 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 0d 0a  nd others...**..
ed8a0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 63 74  ** There are act
ed8b0 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69  ually several di
ed8c0 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c  fferent VFS impl
ed8d0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74  ementations in t
ed8e0 68 69 73 20 66 69 6c 65 2e 0d 0a 2a 2a 20 54 68  his file...** Th
ed8f0 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 61 72  e differences ar
ed900 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 68 61  e in the way tha
ed910 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69  t file locking i
ed920 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 66  s done.  The def
ed930 61 75 6c 74 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ault..** impleme
ed940 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73  ntation uses Pos
ed950 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b  ix Advisory Lock
ed960 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20  s.  Alternative 
ed970 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0d  implementations.
ed980 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c  .** use flock(),
ed990 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69   dot-files, vari
ed9a0 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20  ous proprietary 
ed9b0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c  locking schemas,
ed9c0 20 6f 72 20 73 69 6d 70 6c 79 0d 0a 2a 2a 20 73   or simply..** s
ed9d0 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20  kip locking all 
ed9e0 74 6f 67 65 74 68 65 72 2e 0d 0a 2a 2a 0d 0a 2a  together...**..*
ed9f0 2a 20 54 68 69 73 20 73 6f 75 72 63 65 20 66 69  * This source fi
eda00 6c 65 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20  le is organized 
eda10 69 6e 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77  into divisions w
eda20 68 65 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66  here the logic f
eda30 6f 72 20 76 61 72 69 6f 75 73 0d 0a 2a 2a 20 73  or various..** s
eda40 75 62 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63  ubfunctions is c
eda50 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20  ontained within 
eda60 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
eda70 64 69 76 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53  division.  PLEAS
eda80 45 0d 0a 2a 2a 20 4b 45 45 50 20 54 48 45 20 53  E..** KEEP THE S
eda90 54 52 55 43 54 55 52 45 20 4f 46 20 54 48 49 53  TRUCTURE OF THIS
edaa0 20 46 49 4c 45 20 49 4e 54 41 43 54 2e 20 20 4e   FILE INTACT.  N
edab0 65 77 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62  ew code should b
edac0 65 20 70 6c 61 63 65 64 0d 0a 2a 2a 20 69 6e 20  e placed..** in 
edad0 74 68 65 20 63 6f 72 72 65 63 74 20 64 69 76 69  the correct divi
edae0 73 69 6f 6e 20 61 6e 64 20 73 68 6f 75 6c 64 20  sion and should 
edaf0 62 65 20 63 6c 65 61 72 6c 79 20 6c 61 62 65 6c  be clearly label
edb00 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
edb10 6c 61 79 6f 75 74 20 6f 66 20 64 69 76 69 73 69  layout of divisi
edb20 6f 6e 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ons is as follow
edb30 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20  s:..**..**   *  
edb40 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20  General-purpose 
edb50 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 6e 64  declarations and
edb60 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f   utility functio
edb70 6e 73 2e 0d 0a 2a 2a 20 20 20 2a 20 20 55 6e 69  ns...**   *  Uni
edb80 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f 67 69  que file ID logi
edb90 63 20 75 73 65 64 20 62 79 20 56 78 57 6f 72 6b  c used by VxWork
edba0 73 2e 0d 0a 2a 2a 20 20 20 2a 20 20 56 61 72 69  s...**   *  Vari
edbb0 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d  ous locking prim
edbc0 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61  itive implementa
edbd0 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65 70  tions (all excep
edbe0 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 29  t proxy locking)
edbf0 3a 0d 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  :..**      + for
edc00 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20   Posix Advisory 
edc10 4c 6f 63 6b 73 0d 0a 2a 2a 20 20 20 20 20 20 2b  Locks..**      +
edc20 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73   for no-op locks
edc30 0d 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  ..**      + for 
edc40 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0d 0a  dot-file locks..
edc50 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c  **      + for fl
edc60 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0d 0a 2a  ock() locking..*
edc70 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d  *      + for nam
edc80 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
edc90 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79  ks (VxWorks only
edca0 29 0d 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  )..**      + for
edcb0 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20   AFP filesystem 
edcc0 6c 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e  locks (MacOSX on
edcd0 6c 79 29 0d 0a 2a 2a 20 20 20 2a 20 20 73 71 6c  ly)..**   *  sql
edce0 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64  ite3_file method
edcf0 73 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64  s not associated
edd00 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0d 0a   with locking...
edd10 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69  **   *  Definiti
edd20 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  ons of sqlite3_i
edd30 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
edd40 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e  s for all lockin
edd50 67 0d 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f  g..**      metho
edd60 64 73 20 70 6c 75 73 20 22 66 69 6e 64 65 72 22  ds plus "finder"
edd70 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 65   functions for e
edd80 61 63 68 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  ach locking meth
edd90 6f 64 2e 0d 0a 2a 2a 20 20 20 2a 20 20 73 71 6c  od...**   *  sql
edda0 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 20  ite3_vfs method 
eddb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
eddc0 0d 0a 2a 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 6e  ..**   *  Lockin
eddd0 67 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72  g primitives for
edde0 20 74 68 65 20 70 72 6f 78 79 20 75 62 65 72 2d   the proxy uber-
eddf0 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e 20  locking-method. 
ede00 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0d 0a 2a  (MacOSX only)..*
ede10 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f  *   *  Definitio
ede20 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66  ns of sqlite3_vf
ede30 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c  s objects for al
ede40 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  l locking method
ede50 73 0d 0a 2a 2a 20 20 20 20 20 20 70 6c 75 73 20  s..**      plus 
ede60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
ede70 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
ede80 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  it() and sqlite3
ede90 5f 6f 73 5f 65 6e 64 28 29 2e 0d 0a 2a 2f 0d 0a  _os_end()...*/..
edea0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  #if SQLITE_OS_UN
edeb0 49 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IX              
edec0 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  /* This file is 
eded0 75 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c  used on unix onl
edee0 79 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  y */..../*..** T
edef0 68 65 72 65 20 61 72 65 20 76 61 72 69 6f 75 73  here are various
edf00 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 69 6c   methods for fil
edf10 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 66  e locking used f
edf20 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 0d 0a  or concurrency..
edf30 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0d 0a 2a 2a 0d  ** control:..**.
edf40 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c  .**   1. POSIX l
edf50 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61  ocking (the defa
edf60 75 6c 74 29 2c 0d 0a 2a 2a 20 20 20 32 2e 20 4e  ult),..**   2. N
edf70 6f 20 6c 6f 63 6b 69 6e 67 2c 0d 0a 2a 2a 20 20  o locking,..**  
edf80 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63   3. Dot-file loc
edf90 6b 69 6e 67 2c 0d 0a 2a 2a 20 20 20 34 2e 20 66  king,..**   4. f
edfa0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0d  lock() locking,.
edfb0 0a 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63  .**   5. AFP loc
edfc0 6b 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c  king (OSX only),
edfd0 0d 0a 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20  ..**   6. Named 
edfe0 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 73  POSIX semaphores
edff0 20 28 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c   (VXWorks only),
ee000 0d 0a 2a 2a 20 20 20 37 2e 20 70 72 6f 78 79 20  ..**   7. proxy 
ee010 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e  locking. (OSX on
ee020 6c 79 29 0d 0a 2a 2a 0d 0a 2a 2a 20 53 74 79 6c  ly)..**..** Styl
ee030 65 73 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 61  es 4, 5, and 7 a
ee040 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  re only availabl
ee050 65 20 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  e of SQLITE_ENAB
ee060 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
ee070 0d 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20  ..** is defined 
ee080 74 6f 20 31 2e 20 20 54 68 65 20 53 51 4c 49 54  to 1.  The SQLIT
ee090 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
ee0a0 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62  _STYLE also enab
ee0b0 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 0d 0a 2a  les automatic..*
ee0c0 2a 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 20 74  * selection of t
ee0d0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c  he appropriate l
ee0e0 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73  ocking style bas
ee0f0 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 73 79  ed on the filesy
ee100 73 74 65 6d 0d 0a 2a 2a 20 77 68 65 72 65 20 74  stem..** where t
ee110 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c  he database is l
ee120 6f 63 61 74 65 64 2e 20 20 0d 0a 2a 2f 0d 0a 23  ocated.  ..*/..#
ee130 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
ee140 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
ee150 47 5f 53 54 59 4c 45 29 0d 0a 23 20 20 69 66 20  G_STYLE)..#  if 
ee160 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
ee170 5f 29 0d 0a 23 20 20 20 20 64 65 66 69 6e 65 20  _)..#    define 
ee180 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
ee190 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0d 0a 23  CKING_STYLE 1..#
ee1a0 20 20 65 6c 73 65 0d 0a 23 20 20 20 20 64 65 66    else..#    def
ee1b0 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ine SQLITE_ENABL
ee1c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
ee1d0 30 0d 0a 23 20 20 65 6e 64 69 66 0d 0a 23 65 6e  0..#  endif..#en
ee1e0 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65  dif..../*..** De
ee1f0 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 58 57 4f  fine the OS_VXWO
ee200 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 73 6f  RKS pre-processo
ee210 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 66 20  r macro to 1 if 
ee220 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0d 0a 2a 2a  building on ..**
ee230 20 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 6f   vxworks, or 0 o
ee240 74 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a 23  therwise...*/..#
ee250 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b  ifndef OS_VXWORK
ee260 53 0d 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64  S..#  if defined
ee270 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 65 66  (__RTP__) || def
ee280 69 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e 45 4c  ined(_WRS_KERNEL
ee290 29 0d 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f  )..#    define O
ee2a0 53 5f 56 58 57 4f 52 4b 53 20 31 0d 0a 23 20 20  S_VXWORKS 1..#  
ee2b0 65 6c 73 65 0d 0a 23 20 20 20 20 64 65 66 69 6e  else..#    defin
ee2c0 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0d 0a  e OS_VXWORKS 0..
ee2d0 23 20 20 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66  #  endif..#endif
ee2e0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65  ..../*..** These
ee2f0 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64   #defines should
ee300 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c   enable >2GB fil
ee310 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73  e support on Pos
ee320 69 78 20 69 66 20 74 68 65 0d 0a 2a 2a 20 75 6e  ix if the..** un
ee330 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
ee340 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  ng system suppor
ee350 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f  ts it.  If the O
ee360 53 20 6c 61 63 6b 73 0d 0a 2a 2a 20 6c 61 72 67  S lacks..** larg
ee370 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20  e file support, 
ee380 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20  these should be 
ee390 6e 6f 2d 6f 70 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  no-ops...**..** 
ee3a0 4c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f  Large file suppo
ee3b0 72 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c  rt can be disabl
ee3c0 65 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53  ed using the -DS
ee3d0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
ee3e0 53 20 73 77 69 74 63 68 0d 0a 2a 2a 20 6f 6e 20  S switch..** on 
ee3f0 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d  the compiler com
ee400 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73  mand line.  This
ee410 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 69 66   is necessary if
ee420 20 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69   you are compili
ee430 6e 67 0d 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65  ng..** on a rece
ee440 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20  nt machine (ex: 
ee450 52 65 64 48 61 74 20 37 2e 32 29 20 62 75 74 20  RedHat 7.2) but 
ee460 79 6f 75 20 77 61 6e 74 20 79 6f 75 72 20 63 6f  you want your co
ee470 64 65 20 74 6f 20 77 6f 72 6b 0d 0a 2a 2a 20 6f  de to work..** o
ee480 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69  n an older machi
ee490 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 36  ne (ex: RedHat 6
ee4a0 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d  .0).  If you com
ee4b0 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 74 20 37  pile on RedHat 7
ee4c0 2e 32 0d 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74  .2..** without t
ee4d0 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20  his option, LFS 
ee4e0 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 74 20  is enable.  But 
ee4f0 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  LFS does not exi
ee500 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c  st in the kernel
ee510 0d 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 36  ..** in RedHat 6
ee520 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 20  .0, so the code 
ee530 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e  won't work.  Hen
ee540 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20  ce, for maximum 
ee550 62 69 6e 61 72 79 0d 0a 2a 2a 20 70 6f 72 74 61  binary..** porta
ee560 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c  bility you shoul
ee570 64 20 6f 6d 69 74 20 4c 46 53 2e 0d 0a 2a 2a 0d  d omit LFS...**.
ee580 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75 73  .** The previous
ee590 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20 77   paragraph was w
ee5a0 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e 20  ritten in 2005. 
ee5b0 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70 68   (This paragraph
ee5c0 20 69 73 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20   is written..** 
ee5d0 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20  on 2008-11-28.) 
ee5e0 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20  These days, all 
ee5f0 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75  Linux kernels su
ee600 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65  pport large file
ee610 73 2c 20 73 6f 0d 0a 2a 2a 20 79 6f 75 20 73 68  s, so..** you sh
ee620 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65  ould probably le
ee630 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e  ave LFS enabled.
ee640 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64    But some embed
ee650 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69  ded platforms mi
ee660 67 68 74 0d 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53  ght..** lack LFS
ee670 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
ee680 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  he SQLITE_DISABL
ee690 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68  E_LFS macro migh
ee6a0 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75  t still be usefu
ee6b0 6c 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20  l...*/..#ifndef 
ee6c0 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c  SQLITE_DISABLE_L
ee6d0 46 53 0d 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41  FS..# define _LA
ee6e0 52 47 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31  RGE_FILE       1
ee6f0 0d 0a 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 45  ..# ifndef _FILE
ee700 5f 4f 46 46 53 45 54 5f 42 49 54 53 0d 0a 23 20  _OFFSET_BITS..# 
ee710 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f    define _FILE_O
ee720 46 46 53 45 54 5f 42 49 54 53 20 36 34 0d 0a 23  FFSET_BITS 64..#
ee730 20 65 6e 64 69 66 0d 0a 23 20 64 65 66 69 6e 65   endif..# define
ee740 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
ee750 43 45 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  CE 1..#endif....
ee760 2f 2a 0d 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20  /*..** standard 
ee770 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0d 0a  include files...
ee780 2a 2f 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79  */..#include <sy
ee790 73 2f 74 79 70 65 73 2e 68 3e 0d 0a 23 69 6e 63  s/types.h>..#inc
ee7a0 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68  lude <sys/stat.h
ee7b0 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e  >..#include <fcn
ee7c0 74 6c 2e 68 3e 0d 0a 23 69 6e 63 6c 75 64 65 20  tl.h>..#include 
ee7d0 3c 75 6e 69 73 74 64 2e 68 3e 0d 0a 2f 2a 20 23  <unistd.h>../* #
ee7e0 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e  include <time.h>
ee7f0 20 2a 2f 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73   */..#include <s
ee800 79 73 2f 74 69 6d 65 2e 68 3e 0d 0a 23 69 6e 63  ys/time.h>..#inc
ee810 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0d 0a  lude <errno.h>..
ee820 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
ee830 4d 49 54 5f 57 41 4c 0d 0a 23 69 6e 63 6c 75 64  MIT_WAL..#includ
ee840 65 20 3c 73 79 73 2f 6d 6d 61 6e 2e 68 3e 0d 0a  e <sys/mman.h>..
ee850 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 23 69 66 20  #endif......#if 
ee860 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
ee870 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 23 20 69  CKING_STYLE..# i
ee880 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74  nclude <sys/ioct
ee890 6c 2e 68 3e 0d 0a 23 20 69 66 20 4f 53 5f 56 58  l.h>..# if OS_VX
ee8a0 57 4f 52 4b 53 0d 0a 23 20 20 69 6e 63 6c 75 64  WORKS..#  includ
ee8b0 65 20 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e 0d  e <semaphore.h>.
ee8c0 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d  .#  include <lim
ee8d0 69 74 73 2e 68 3e 0d 0a 23 20 65 6c 73 65 0d 0a  its.h>..# else..
ee8e0 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  #  include <sys/
ee8f0 66 69 6c 65 2e 68 3e 0d 0a 23 20 20 69 6e 63 6c  file.h>..#  incl
ee900 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68  ude <sys/param.h
ee910 3e 0d 0a 23 20 65 6e 64 69 66 0d 0a 23 65 6e 64  >..# endif..#end
ee920 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
ee930 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
ee940 45 20 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69  E */....#if defi
ee950 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c  ned(__APPLE__) |
ee960 7c 20 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  | (SQLITE_ENABLE
ee970 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26  _LOCKING_STYLE &
ee980 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 29 0d 0a  & !OS_VXWORKS)..
ee990 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d  # include <sys/m
ee9a0 6f 75 6e 74 2e 68 3e 0d 0a 23 65 6e 64 69 66 0d  ount.h>..#endif.
ee9b0 0a 0d 0a 23 69 66 64 65 66 20 48 41 56 45 5f 55  ...#ifdef HAVE_U
ee9c0 54 49 4d 45 0d 0a 23 20 69 6e 63 6c 75 64 65 20  TIME..# include 
ee9d0 3c 75 74 69 6d 65 2e 68 3e 0d 0a 23 65 6e 64 69  <utime.h>..#endi
ee9e0 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f  f..../*..** Allo
ee9f0 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 75 6e  wed values of un
eea00 69 78 46 69 6c 65 2e 66 73 46 6c 61 67 73 0d 0a  ixFile.fsFlags..
eea10 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
eea20 54 45 5f 46 53 46 4c 41 47 53 5f 49 53 5f 4d 53  TE_FSFLAGS_IS_MS
eea30 44 4f 53 20 20 20 20 20 30 78 31 0d 0a 0d 0a 2f  DOS     0x1..../
eea40 2a 0d 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  *..** If we are 
eea50 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61 66  to be thread-saf
eea60 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70  e, include the p
eea70 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20 61  threads header a
eea80 6e 64 20 64 65 66 69 6e 65 0d 0a 2a 2a 20 74 68  nd define..** th
eea90 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
eeaa0 52 45 41 44 53 20 6d 61 63 72 6f 2e 0d 0a 2a 2f  READS macro...*/
eeab0 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ..#if SQLITE_THR
eeac0 45 41 44 53 41 46 45 0d 0a 2f 2a 20 23 20 69 6e  EADSAFE../* # in
eead0 63 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e 68  clude <pthread.h
eeae0 3e 20 2a 2f 0d 0a 23 20 64 65 66 69 6e 65 20 53  > */..# define S
eeaf0 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
eeb00 44 53 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  DS 1..#endif....
eeb10 2f 2a 0d 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70  /*..** Default p
eeb20 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20  ermissions when 
eeb30 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66  creating a new f
eeb40 69 6c 65 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  ile..*/..#ifndef
eeb50 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
eeb60 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53  FILE_PERMISSIONS
eeb70 0d 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ..# define SQLIT
eeb80 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
eeb90 45 52 4d 49 53 53 49 4f 4e 53 20 30 36 34 34 0d  ERMISSIONS 0644.
eeba0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 20  .#endif..../*.. 
eebb0 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69  ** Default permi
eebc0 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61  ssions when crea
eebd0 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 20  ting auto proxy 
eebe0 64 69 72 0d 0a 20 2a 2f 0d 0a 23 69 66 6e 64 65  dir.. */..#ifnde
eebf0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
eec00 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
eec10 53 49 4f 4e 53 0d 0a 23 20 64 65 66 69 6e 65 20  SIONS..# define 
eec20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
eec30 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49  ROXYDIR_PERMISSI
eec40 4f 4e 53 20 30 37 35 35 0d 0a 23 65 6e 64 69 66  ONS 0755..#endif
eec50 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 78 69 6d  ..../*..** Maxim
eec60 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 74  um supported pat
eec70 68 2d 6c 65 6e 67 74 68 2e 0d 0a 2a 2f 0d 0a 23  h-length...*/..#
eec80 64 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e  define MAX_PATHN
eec90 41 4d 45 20 35 31 32 0d 0a 0d 0a 2f 2a 0d 0a 2a  AME 512..../*..*
eeca0 2a 20 4f 6e 6c 79 20 73 65 74 20 74 68 65 20 6c  * Only set the l
eecb0 61 73 74 45 72 72 6e 6f 20 69 66 20 74 68 65 20  astErrno if the 
eecc0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 61 20  error code is a 
eecd0 72 65 61 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e  real error and n
eece0 6f 74 20 0d 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c  ot ..** a normal
eecf0 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e   expected return
eed00 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
eed10 42 55 53 59 20 6f 72 20 53 51 4c 49 54 45 5f 4f  BUSY or SQLITE_O
eed20 4b 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 49  K..*/..#define I
eed30 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 29 20  S_LOCK_ERROR(x) 
eed40 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 4f   ((x != SQLITE_O
eed50 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51 4c 49  K) && (x != SQLI
eed60 54 45 5f 42 55 53 59 29 29 0d 0a 0d 0a 2f 2a 20  TE_BUSY))..../* 
eed70 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
eed80 65 73 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73  es */..typedef s
eed90 74 72 75 63 74 20 75 6e 69 78 53 68 6d 20 75 6e  truct unixShm un
eeda0 69 78 53 68 6d 3b 20 20 20 20 20 20 20 20 20 20  ixShm;          
eedb0 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
eedc0 6f 6e 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  on shared memory
eedd0 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72   */..typedef str
eede0 75 63 74 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20  uct unixShmNode 
eedf0 75 6e 69 78 53 68 6d 4e 6f 64 65 3b 20 20 20 20  unixShmNode;    
eee00 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d     /* Shared mem
eee10 6f 72 79 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0d  ory instance */.
eee20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
eee30 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 75 6e  unixInodeInfo un
eee40 69 78 49 6e 6f 64 65 49 6e 66 6f 3b 20 20 20 2f  ixInodeInfo;   /
eee50 2a 20 41 6e 20 69 2d 6e 6f 64 65 20 2a 2f 0d 0a  * An i-node */..
eee60 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55  typedef struct U
eee70 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78  nixUnusedFd Unix
eee80 55 6e 75 73 65 64 46 64 3b 20 20 20 20 20 2f 2a  UnusedFd;     /*
eee90 20 41 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20   An unused file 
eeea0 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0d 0a 0d  descriptor */...
eeeb0 0a 2f 2a 0d 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  ./*..** Sometime
eeec0 73 2c 20 61 66 74 65 72 20 61 20 66 69 6c 65 20  s, after a file 
eeed0 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64  handle is closed
eeee0 20 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20   by SQLite, the 
eeef0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0d  file descriptor.
eef00 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6c  .** cannot be cl
eef10 6f 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  osed immediately
eef20 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73  . In these cases
eef30 2c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74  , instances of t
eef40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a  he following..**
eef50 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 75   structure are u
eef60 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
eef70 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
eef80 20 77 68 69 6c 65 20 77 61 69 74 69 6e 67 20 66   while waiting f
eef90 6f 72 20 61 6e 0d 0a 2a 2a 20 6f 70 70 6f 72 74  or an..** opport
eefa0 75 6e 69 74 79 20 74 6f 20 65 69 74 68 65 72 20  unity to either 
eefb0 63 6c 6f 73 65 20 6f 72 20 72 65 75 73 65 20 69  close or reuse i
eefc0 74 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 55  t...*/..struct U
eefd0 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0d 0a 20  nixUnusedFd {.. 
eefe0 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
eeff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
ef000 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
ef010 20 63 6c 6f 73 65 20 2a 2f 0d 0a 20 20 69 6e 74   close */..  int
ef020 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
ef030 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
ef040 74 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69  this file descri
ef050 70 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20  ptor was opened 
ef060 77 69 74 68 20 2a 2f 0d 0a 20 20 55 6e 69 78 55  with */..  UnixU
ef070 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20  nusedFd *pNext; 
ef080 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75       /* Next unu
ef090 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70  sed file descrip
ef0a0 74 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65  tor on same file
ef0b0 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
ef0c0 2a 20 54 68 65 20 75 6e 69 78 46 69 6c 65 20 73  * The unixFile s
ef0d0 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62 63  tructure is subc
ef0e0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
ef0f0 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 74 6f  file specific to
ef100 20 74 68 65 20 75 6e 69 78 0d 0a 2a 2a 20 56 46   the unix..** VF
ef110 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
ef120 73 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20  s...*/..typedef 
ef130 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20  struct unixFile 
ef140 75 6e 69 78 46 69 6c 65 3b 0d 0a 73 74 72 75 63  unixFile;..struc
ef150 74 20 75 6e 69 78 46 69 6c 65 20 7b 0d 0a 20 20  t unixFile {..  
ef160 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
ef170 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f  ds const *pMetho
ef180 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68  d;  /* Always th
ef190 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  e first entry */
ef1a0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ..  sqlite3_vfs 
ef1b0 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
ef1c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
ef1d0 46 53 20 74 68 61 74 20 63 72 65 61 74 65 64 20  FS that created 
ef1e0 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f  this unixFile */
ef1f0 0d 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66  ..  unixInodeInf
ef200 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20 20 20 20  o *pInode;      
ef210 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20          /* Info 
ef220 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74  about locks on t
ef230 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0d 0a 20 20  his inode */..  
ef240 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
ef250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef260 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20      /* The file 
ef270 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0d 0a 20  descriptor */.. 
ef280 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65   unsigned char e
ef290 46 69 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20  FileLock;       
ef2a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65       /* The type
ef2b0 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e   of lock held on
ef2c0 20 74 68 69 73 20 66 64 20 2a 2f 0d 0a 20 20 75   this fd */..  u
ef2d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 74 72  nsigned char ctr
ef2e0 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  lFlags;         
ef2f0 20 20 20 2f 2a 20 42 65 68 61 76 69 6f 72 61 6c     /* Behavioral
ef300 20 62 69 74 73 2e 20 20 55 4e 49 58 46 49 4c 45   bits.  UNIXFILE
ef310 5f 2a 20 66 6c 61 67 73 20 2a 2f 0d 0a 20 20 69  _* flags */..  i
ef320 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20  nt lastErrno;   
ef330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef340 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65     /* The unix e
ef350 72 72 6e 6f 20 66 72 6f 6d 20 6c 61 73 74 20 49  rrno from last I
ef360 2f 4f 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 76  /O error */..  v
ef370 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74  oid *lockingCont
ef380 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
ef390 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74     /* Locking st
ef3a0 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61  yle specific sta
ef3b0 74 65 20 2a 2f 0d 0a 20 20 55 6e 69 78 55 6e 75  te */..  UnixUnu
ef3c0 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20  sedFd *pUnused; 
ef3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ef3e0 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e  Pre-allocated Un
ef3f0 69 78 55 6e 75 73 65 64 46 64 20 2a 2f 0d 0a 20  ixUnusedFd */.. 
ef400 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
ef410 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
ef420 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
ef430 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 75  the file */..  u
ef440 6e 69 78 53 68 6d 20 2a 70 53 68 6d 3b 20 20 20  nixShm *pShm;   
ef450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef460 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d     /* Shared mem
ef470 6f 72 79 20 73 65 67 6d 65 6e 74 20 69 6e 66 6f  ory segment info
ef480 72 6d 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e  rmation */..  in
ef490 74 20 73 7a 43 68 75 6e 6b 3b 20 20 20 20 20 20  t szChunk;      
ef4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef4b0 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20    /* Configured 
ef4c0 62 79 20 46 43 4e 54 4c 5f 43 48 55 4e 4b 5f 53  by FCNTL_CHUNK_S
ef4d0 49 5a 45 20 2a 2f 0d 0a 23 69 66 20 53 51 4c 49  IZE */..#if SQLI
ef4e0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
ef4f0 47 5f 53 54 59 4c 45 0d 0a 20 20 69 6e 74 20 6f  G_STYLE..  int o
ef500 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20  penFlags;       
ef510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ef520 2a 20 54 68 65 20 66 6c 61 67 73 20 73 70 65 63  * The flags spec
ef530 69 66 69 65 64 20 61 74 20 6f 70 65 6e 28 29 20  ified at open() 
ef540 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20  */..#endif..#if 
ef550 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
ef560 43 4b 49 4e 47 5f 53 54 59 4c 45 20 7c 7c 20 64  CKING_STYLE || d
ef570 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
ef580 29 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 66 73  )..  unsigned fs
ef590 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
ef5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 63 61 63 68           /* cach
ef5b0 65 64 20 64 65 74 61 69 6c 73 20 66 72 6f 6d 20  ed details from 
ef5c0 73 74 61 74 66 73 28 29 20 2a 2f 0d 0a 23 65 6e  statfs() */..#en
ef5d0 64 69 66 0d 0a 23 69 66 20 4f 53 5f 56 58 57 4f  dif..#if OS_VXWO
ef5e0 52 4b 53 0d 0a 20 20 73 74 72 75 63 74 20 76 78  RKS..  struct vx
ef5f0 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64  worksFileId *pId
ef600 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e  ;          /* Un
ef610 69 71 75 65 20 66 69 6c 65 20 49 44 20 2a 2f 0d  ique file ID */.
ef620 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
ef630 20 4e 44 45 42 55 47 0d 0a 20 20 2f 2a 20 54 68   NDEBUG..  /* Th
ef640 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
ef650 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 75 73  variables are us
ef660 65 64 20 74 6f 20 74 72 61 63 6b 20 77 68 65 74  ed to track whet
ef670 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0d 0a  her or not the..
ef680 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
ef690 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79 74 65   counter in byte
ef6a0 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74 61 62  s 24-27 of datab
ef6b0 61 73 65 20 66 69 6c 65 73 20 61 72 65 20 75 70  ase files are up
ef6c0 64 61 74 65 64 0d 0a 20 20 2a 2a 20 77 68 65 6e  dated..  ** when
ef6d0 65 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f 66  ever any part of
ef6e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 68   the database ch
ef6f0 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65 72  anges.  An asser
ef700 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 0d  tion fault will.
ef710 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61  .  ** occur if a
ef720 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64   file is updated
ef730 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70   without also up
ef740 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73  dating the trans
ef750 61 63 74 69 6f 6e 0d 0a 20 20 2a 2a 20 63 6f 75  action..  ** cou
ef760 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74  nter.  This test
ef770 20 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f 69   is made to avoi
ef780 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 73  d new problems s
ef790 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 0d 0a 20  imilar to the.. 
ef7a0 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65   ** one describe
ef7b0 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 35 38  d by ticket #358
ef7c0 34 2e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 75 6e 73  4. ..  */..  uns
ef7d0 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73  igned char trans
ef7e0 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54  CntrChng;   /* T
ef7f0 72 75 65 20 69 66 20 74 68 65 20 74 72 61 6e 73  rue if the trans
ef800 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63  action counter c
ef810 68 61 6e 67 65 64 20 2a 2f 0d 0a 20 20 75 6e 73  hanged */..  uns
ef820 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 64  igned char dbUpd
ef830 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ate;        /* T
ef840 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 20  rue if any part 
ef850 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  of database file
ef860 20 63 68 61 6e 67 65 64 20 2a 2f 0d 0a 20 20 75   changed */..  u
ef870 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e  nsigned char inN
ef880 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a  ormalWrite;   /*
ef890 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f   True if in a no
ef8a0 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61  rmal write opera
ef8b0 74 69 6f 6e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  tion */..#endif.
ef8c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ef8d0 45 53 54 0d 0a 20 20 2f 2a 20 49 6e 20 74 65 73  EST..  /* In tes
ef8e0 74 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65  t mode, increase
ef8f0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69   the size of thi
ef900 73 20 73 74 72 75 63 74 75 72 65 20 61 20 62 69  s structure a bi
ef910 74 20 73 6f 20 74 68 61 74 20 0d 0a 20 20 2a 2a  t so that ..  **
ef920 20 69 74 20 69 73 20 6c 61 72 67 65 72 20 74 68   it is larger th
ef930 61 6e 20 74 68 65 20 73 74 72 75 63 74 20 43 72  an the struct Cr
ef940 61 73 68 46 69 6c 65 20 64 65 66 69 6e 65 64 20  ashFile defined 
ef950 69 6e 20 74 65 73 74 36 2e 63 2e 0d 0a 20 20 2a  in test6.c...  *
ef960 2f 0d 0a 20 20 63 68 61 72 20 61 50 61 64 64 69  /..  char aPaddi
ef970 6e 67 5b 33 32 5d 3b 0d 0a 23 65 6e 64 69 66 0d  ng[32];..#endif.
ef980 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c  .};..../*..** Al
ef990 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72  lowed values for
ef9a0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 63 74   the unixFile.ct
ef9b0 72 6c 46 6c 61 67 73 20 62 69 74 6d 61 73 6b 3a  rlFlags bitmask:
ef9c0 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 55 4e  ..*/..#define UN
ef9d0 49 58 46 49 4c 45 5f 45 58 43 4c 20 20 20 20 20  IXFILE_EXCL     
ef9e0 20 20 20 30 78 30 31 20 20 20 20 20 2f 2a 20 43     0x01     /* C
ef9f0 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20  onnections from 
efa00 6f 6e 65 20 70 72 6f 63 65 73 73 20 6f 6e 6c 79  one process only
efa10 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 55 4e 49   */..#define UNI
efa20 58 46 49 4c 45 5f 52 44 4f 4e 4c 59 20 20 20 20  XFILE_RDONLY    
efa30 20 20 30 78 30 32 20 20 20 20 20 2f 2a 20 43 6f    0x02     /* Co
efa40 6e 6e 65 63 74 69 6f 6e 20 69 73 20 72 65 61 64  nnection is read
efa50 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69 6e   only */..#defin
efa60 65 20 55 4e 49 58 46 49 4c 45 5f 50 45 52 53 49  e UNIXFILE_PERSI
efa70 53 54 5f 57 41 4c 20 30 78 30 34 20 20 20 20 20  ST_WAL 0x04     
efa80 2f 2a 20 50 65 72 73 69 73 74 65 6e 74 20 57 41  /* Persistent WA
efa90 4c 20 6d 6f 64 65 20 2a 2f 0d 0a 23 69 66 6e 64  L mode */..#ifnd
efaa0 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
efab0 45 5f 44 49 52 53 59 4e 43 0d 0a 23 20 64 65 66  E_DIRSYNC..# def
efac0 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 44 49 52  ine UNIXFILE_DIR
efad0 53 59 4e 43 20 20 20 20 30 78 30 38 20 20 20 20  SYNC    0x08    
efae0 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 73 79   /* Directory sy
efaf0 6e 63 20 6e 65 65 64 65 64 20 2a 2f 0d 0a 23 65  nc needed */..#e
efb00 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 55 4e  lse..# define UN
efb10 49 58 46 49 4c 45 5f 44 49 52 53 59 4e 43 20 20  IXFILE_DIRSYNC  
efb20 20 20 30 78 30 30 0d 0a 23 65 6e 64 69 66 0d 0a    0x00..#endif..
efb30 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45  #define UNIXFILE
efb40 5f 50 53 4f 57 20 20 20 20 20 20 20 20 30 78 31  _PSOW        0x1
efb50 30 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  0     /* SQLITE_
efb60 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 45 5f  IOCAP_POWERSAFE_
efb70 4f 56 45 52 57 52 49 54 45 20 2a 2f 0d 0a 23 64  OVERWRITE */..#d
efb80 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 44  efine UNIXFILE_D
efb90 45 4c 45 54 45 20 20 20 20 20 20 30 78 32 30 20  ELETE      0x20 
efba0 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e      /* Delete on
efbb0 20 63 6c 6f 73 65 20 2a 2f 0d 0a 23 64 65 66 69   close */..#defi
efbc0 6e 65 20 55 4e 49 58 46 49 4c 45 5f 55 52 49 20  ne UNIXFILE_URI 
efbd0 20 20 20 20 20 20 20 20 30 78 34 30 20 20 20 20          0x40    
efbe0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 6d 69 67   /* Filename mig
efbf0 68 74 20 68 61 76 65 20 71 75 65 72 79 20 70 61  ht have query pa
efc00 72 61 6d 65 74 65 72 73 20 2a 2f 0d 0a 23 64 65  rameters */..#de
efc10 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 4e 4f  fine UNIXFILE_NO
efc20 4c 4f 43 4b 20 20 20 20 20 20 30 78 38 30 20 20  LOCK      0x80  
efc30 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 66 69 6c 65     /* Do no file
efc40 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0d 0a 0d 0a 2f   locking */..../
efc50 2a 0d 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f  *..** Include co
efc60 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
efc70 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20  n to all os_*.c 
efc80 66 69 6c 65 73 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a  files..*/../****
efc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
efca0 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69  de os_common.h i
efcb0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
efcc0 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  os_unix.c ******
efcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
efce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
efcf0 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
efd00 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
efd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
efd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
efd30 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0d  .** 2004 May 22.
efd40 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
efd50 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
efd60 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
efd70 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
efd80 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
efd90 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
efda0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
efdb0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
efdc0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
efdd0 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
efde0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
efdf0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
efe00 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
efe10 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
efe20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
efe30 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
efe40 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
efe50 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
efe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
efe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
efe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
efe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
efea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
efeb0 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  ..** This file c
efec0 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61  ontains macros a
efed0 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20  nd a little bit 
efee0 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  of code that is 
efef0 63 6f 6d 6d 6f 6e 20 74 6f 0d 0a 2a 2a 20 61 6c  common to..** al
eff00 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72  l of the platfor
eff10 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73  m-specific files
eff20 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73   (os_*.c) and is
eff30 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20   #included into 
eff40 74 68 6f 73 65 0d 0a 2a 2a 20 66 69 6c 65 73 2e  those..** files.
eff50 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69  ..**..** This fi
eff60 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
eff70 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
eff80 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
eff90 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0d 0a 2a    It is not a..*
effa0 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
effb0 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0d 0a  e header file...
effc0 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f  */..#ifndef _OS_
effd0 43 4f 4d 4d 4f 4e 5f 48 5f 0d 0a 23 64 65 66 69  COMMON_H_..#defi
effe0 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ne _OS_COMMON_H_
efff0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 20 6c 65  ..../*..** At le
f0000 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76  ast two bugs hav
f0010 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63  e slipped in bec
f0020 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20  ause we changed 
f0030 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  the MEMORY_DEBUG
f0040 0d 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  ..** macro to SQ
f0050 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
f0060 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
f0070 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
f0080 20 6d 61 64 65 20 74 68 65 0d 0a 2a 2a 20 73 77   made the..** sw
f0090 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
f00a0 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
f00b0 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
f00c0 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
f00d0 69 6d 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  ime...*/..#ifdef
f00e0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0d 0a 23   MEMORY_DEBUG..#
f00f0 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
f0100 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
f0110 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
f0120 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
f0130 73 74 65 61 64 2e 22 0d 0a 23 65 6e 64 69 66 0d  stead."..#endif.
f0140 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  ...#if defined(S
f0150 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
f0160 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
f0170 42 55 47 29 0d 0a 23 20 69 66 6e 64 65 66 20 53  BUG)..# ifndef S
f0180 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54  QLITE_DEBUG_OS_T
f0190 52 41 43 45 0d 0a 23 20 20 20 64 65 66 69 6e 65  RACE..#   define
f01a0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53   SQLITE_DEBUG_OS
f01b0 5f 54 52 41 43 45 20 30 0d 0a 23 20 65 6e 64 69  _TRACE 0..# endi
f01c0 66 0d 0a 20 20 69 6e 74 20 73 71 6c 69 74 65 33  f..  int sqlite3
f01d0 4f 53 54 72 61 63 65 20 3d 20 53 51 4c 49 54 45  OSTrace = SQLITE
f01e0 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45 3b  _DEBUG_OS_TRACE;
f01f0 0d 0a 23 20 64 65 66 69 6e 65 20 4f 53 54 52 41  ..# define OSTRA
f0200 43 45 28 58 29 20 20 20 20 20 20 20 20 20 20 69  CE(X)          i
f0210 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
f0220 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
f0230 50 72 69 6e 74 66 20 58 0d 0a 23 65 6c 73 65 0d  Printf X..#else.
f0240 0a 23 20 64 65 66 69 6e 65 20 4f 53 54 52 41 43  .# define OSTRAC
f0250 45 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  E(X)..#endif....
f0260 2f 2a 0d 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  /*..** Macros fo
f0270 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
f0280 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
f0290 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
f02a0 6c 79 20 77 6f 72 6b 73 0d 0a 2a 2a 20 6f 6e 20  ly works..** on 
f02b0 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0d 0a  i486 hardware...
f02c0 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
f02d0 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52  E_PERFORMANCE_TR
f02e0 41 43 45 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 68  ACE..../* ..** h
f02f0 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73  wtime.h contains
f0300 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65   inline assemble
f0310 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65  r code for imple
f0320 6d 65 6e 74 69 6e 67 20 0d 0a 2a 2a 20 68 69 67  menting ..** hig
f0330 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
f0340 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0d 0a  ming routines...
f0350 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
f0360 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
f0370 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
f0380 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
f0390 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f03a0 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
f03b0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
f03c0 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
f03d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f03e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f03f0 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
f0400 38 20 4d 61 79 20 32 37 0d 0a 2a 2a 0d 0a 2a 2a  8 May 27..**..**
f0410 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
f0420 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
f0430 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
f0440 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
f0450 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
f0460 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
f0470 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
f0480 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
f0490 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
f04a0 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
f04b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
f04c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
f04d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
f04e0 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
f04f0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
f0500 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
f0510 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
f0520 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
f0530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0570 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
f0580 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
f0590 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
f05a0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
f05b0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
f05c0 65 22 0d 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20  e"..** counters 
f05d0 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50  for x86 class CP
f05e0 55 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  Us...*/..#ifndef
f05f0 20 5f 48 57 54 49 4d 45 5f 48 5f 0d 0a 23 64 65   _HWTIME_H_..#de
f0600 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0d  fine _HWTIME_H_.
f0610 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
f0620 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
f0630 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
f0640 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
f0650 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
f0660 73 2e 0d 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  s...** It uses t
f0670 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
f0680 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
f0690 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
f06a0 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 70 72 6f  t of the..** pro
f06b0 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72  cessor and retur
f06c0 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20  ns that value.  
f06d0 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
f06e0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0d 0a 2a   for high-res..*
f06f0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0d 0a 2a 2f  * profiling...*/
f0700 0d 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  ..#if (defined(_
f0710 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
f0720 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
f0730 26 20 5c 0d 0a 20 20 20 20 20 20 28 64 65 66 69  & \..      (defi
f0740 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
f0750 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
f0760 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
f0770 36 29 29 0d 0a 0d 0a 20 20 23 69 66 20 64 65 66  6))....  #if def
f0780 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0d 0a  ined(__GNUC__)..
f0790 0d 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
f07a0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
f07b0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
f07c0 7b 0d 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {..     unsigned
f07d0 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0d 0a 20 20   int lo, hi;..  
f07e0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
f07f0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
f0800 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
f0810 64 22 20 28 68 69 29 29 3b 0d 0a 20 20 20 20 20  d" (hi));..     
f0820 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75  return (sqlite_u
f0830 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c  int64)hi << 32 |
f0840 20 6c 6f 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23   lo;..  }....  #
f0850 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
f0860 43 5f 56 45 52 29 0d 0a 0d 0a 20 20 5f 5f 64 65  C_VER)....  __de
f0870 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f  clspec(naked) __
f0880 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69  inline sqlite_ui
f0890 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c  nt64 __cdecl sql
f08a0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
f08b0 7b 0d 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0d  {..     __asm {.
f08c0 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0d 0a  .        rdtsc..
f08d0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
f08e0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
f08f0 20 61 74 20 45 44 58 3a 45 41 58 0d 0a 20 20 20   at EDX:EAX..   
f0900 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23 65    }..  }....  #e
f0910 6e 64 69 66 0d 0a 0d 0a 23 65 6c 69 66 20 28 64  ndif....#elif (d
f0920 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
f0930 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38   && defined(__x8
f0940 36 5f 36 34 5f 5f 29 29 0d 0a 0d 0a 20 20 5f 5f  6_64__))....  __
f0950 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
f0960 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
f0970 74 69 6d 65 28 76 6f 69 64 29 7b 0d 0a 20 20 20  time(void){..   
f0980 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
f0990 20 76 61 6c 3b 0d 0a 20 20 20 20 20 20 5f 5f 61   val;..      __a
f09a0 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
f09b0 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41  _ ("rdtsc" : "=A
f09c0 22 20 28 76 61 6c 29 29 3b 0d 0a 20 20 20 20 20  " (val));..     
f09d0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 20 20   return val;..  
f09e0 7d 0d 0a 20 0d 0a 23 65 6c 69 66 20 28 64 65 66  }.. ..#elif (def
f09f0 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
f0a00 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
f0a10 5f 29 29 0d 0a 0d 0a 20 20 5f 5f 69 6e 6c 69 6e  _))....  __inlin
f0a20 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
f0a30 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
f0a40 76 6f 69 64 29 7b 0d 0a 20 20 20 20 20 20 75 6e  void){..      un
f0a50 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
f0a60 20 72 65 74 76 61 6c 3b 0d 0a 20 20 20 20 20 20   retval;..      
f0a70 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
f0a80 6e 6b 3b 0d 0a 20 20 20 20 20 20 5f 5f 61 73 6d  nk;..      __asm
f0a90 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
f0aa0 28 22 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20 20  ("\n\..         
f0ab0 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20   1:      mftbu  
f0ac0 20 25 31 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20   %1\n\..        
f0ad0 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
f0ae0 20 20 25 4c 30 5c 6e 5c 0d 0a 20 20 20 20 20 20    %L0\n\..      
f0af0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
f0b00 75 20 20 20 25 30 5c 6e 5c 0d 0a 20 20 20 20 20  u   %0\n\..     
f0b10 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
f0b20 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0d 0a 20  w    %0,%1\n\.. 
f0b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f0b40 20 62 6e 65 20 20 20 20 20 31 62 22 0d 0a 20 20   bne     1b"..  
f0b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f0b60 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c  : "=r" (retval),
f0b70 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0d 0a   "=r" (junk));..
f0b80 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74        return ret
f0b90 76 61 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 65 6c  val;..  }....#el
f0ba0 73 65 0d 0a 0d 0a 20 20 23 65 72 72 6f 72 20 4e  se....  #error N
f0bb0 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  eed implementati
f0bc0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74  on of sqlite3Hwt
f0bd0 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
f0be0 6c 61 74 66 6f 72 6d 2e 0d 0a 0d 0a 20 20 2f 2a  latform.....  /*
f0bf0 0d 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  ..  ** To compil
f0c00 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
f0c10 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
f0c20 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
f0c30 70 6c 61 74 66 6f 72 6d 2c 0d 0a 20 20 2a 2a 20  platform,..  ** 
f0c40 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
f0c50 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
f0c60 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
f0c70 6f 77 69 6e 67 0d 0a 20 20 2a 2a 20 73 74 75 62  owing..  ** stub
f0c80 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20   function.  You 
f0c90 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67  will lose timing
f0ca0 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e   support for man
f0cb0 79 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  y..  ** of the d
f0cc0 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
f0cd0 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
f0ce0 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
f0cf0 0d 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d  ..  ** least com
f0d00 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0d 0a 20  pile and run... 
f0d10 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
f0d20 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e  ATE   sqlite_uin
f0d30 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
f0d40 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  e(void){ return 
f0d50 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  ((sqlite_uint64)
f0d60 30 29 3b 20 7d 0d 0a 0d 0a 23 65 6e 64 69 66 0d  0); }....#endif.
f0d70 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  ...#endif /* !de
f0d80 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
f0d90 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ) */..../*******
f0da0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
f0db0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
f0dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0de0 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
f0df0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
f0e00 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
f0e10 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d  t off in os_comm
f0e20 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
f0e30 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 73 74 61 74  *******/....stat
f0e40 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
f0e50 20 67 5f 73 74 61 72 74 3b 0d 0a 73 74 61 74 69   g_start;..stati
f0e60 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
f0e70 67 5f 65 6c 61 70 73 65 64 3b 0d 0a 23 64 65 66  g_elapsed;..#def
f0e80 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
f0e90 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71        g_start=sq
f0ea0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0d 0a 23  lite3Hwtime()..#
f0eb0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
f0ec0 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73           g_elaps
f0ed0 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ed=sqlite3Hwtime
f0ee0 28 29 2d 67 5f 73 74 61 72 74 0d 0a 23 64 65 66  ()-g_start..#def
f0ef0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
f0f00 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0d  D     g_elapsed.
f0f10 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20  .#else..#define 
f0f20 54 49 4d 45 52 5f 53 54 41 52 54 0d 0a 23 64 65  TIMER_START..#de
f0f30 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0d 0a  fine TIMER_END..
f0f40 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
f0f50 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69  APSED     ((sqli
f0f60 74 65 5f 75 69 6e 74 36 34 29 30 29 0d 0a 23 65  te_uint64)0)..#e
f0f70 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ndif..../*..** I
f0f80 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
f0f90 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53  h the SQLITE_TES
f0fa0 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65  T macro set, the
f0fb0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
f0fc0 62 6c 6f 63 6b 0d 0a 2a 2a 20 6f 66 20 63 6f 64  block..** of cod
f0fd0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
f0fe0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
f0ff0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
f1000 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0d 0a  O error.  This..
f1010 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ** is used for t
f1020 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72  esting the I/O r
f1030 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0d 0a  ecovery logic...
f1040 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
f1050 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41  E_TEST..SQLITE_A
f1060 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
f1070 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
f1080 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
f1090 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49  otal number of I
f10a0 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0d 0a 53 51  /O Errors */..SQ
f10b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f10c0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
f10d0 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  rdhit = 0;      
f10e0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
f10f0 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73  on-benign errors
f1100 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20   */..SQLITE_API 
f1110 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
f1120 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30  rror_pending = 0
f1130 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
f1140 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20  t down to first 
f1150 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0d 0a 53 51  I/O error */..SQ
f1160 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f1170 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
f1180 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
f1190 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
f11a0 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
f11b0 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
f11c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
f11d0 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20  ror_benign = 0; 
f11e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
f11f0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65  if errors are be
f1200 6e 69 67 6e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  nign */..SQLITE_
f1210 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f1220 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
f1230 20 3d 20 30 3b 0d 0a 53 51 4c 49 54 45 5f 41 50   = 0;..SQLITE_AP
f1240 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
f1250 73 6b 66 75 6c 6c 20 3d 20 30 3b 0d 0a 23 64 65  skfull = 0;..#de
f1260 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
f1270 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71  rrorBenign(X) sq
f1280 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
f1290 65 6e 69 67 6e 3d 28 58 29 0d 0a 23 64 65 66 69  enign=(X)..#defi
f12a0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
f12b0 6f 72 28 43 4f 44 45 29 20 20 5c 0d 0a 20 20 69  or(CODE)  \..  i
f12c0 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
f12d0 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
f12e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f12f0 5f 68 69 74 29 20 5c 0d 0a 20 20 20 20 20 20 20  _hit) \..       
f1300 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  || sqlite3_io_er
f1310 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d  ror_pending-- ==
f1320 20 31 20 29 20 20 5c 0d 0a 20 20 20 20 20 20 20   1 )  \..       
f1330 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69         { local_i
f1340 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0d  oerr(); CODE; }.
f1350 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
f1360 61 6c 5f 69 6f 65 72 72 28 29 7b 0d 0a 20 20 49  al_ioerr(){..  I
f1370 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e  OTRACE(("IOERR\n
f1380 22 29 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  "));..  sqlite3_
f1390 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0d  io_error_hit++;.
f13a0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
f13b0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
f13c0 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
f13d0 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0d 0a 7d  or_hardhit++;..}
f13e0 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ..#define Simula
f13f0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
f1400 43 4f 44 45 29 20 5c 0d 0a 20 20 20 69 66 28 20  CODE) \..   if( 
f1410 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
f1420 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0d 0a 20  _pending ){ \.. 
f1430 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
f1440 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
f1450 20 3d 3d 20 31 20 29 7b 20 5c 0d 0a 20 20 20 20   == 1 ){ \..    
f1460 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29     local_ioerr()
f1470 3b 20 5c 0d 0a 20 20 20 20 20 20 20 73 71 6c 69  ; \..       sqli
f1480 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
f1490 3b 20 5c 0d 0a 20 20 20 20 20 20 20 73 71 6c 69  ; \..       sqli
f14a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
f14b0 20 3d 20 31 3b 20 5c 0d 0a 20 20 20 20 20 20 20   = 1; \..       
f14c0 43 4f 44 45 3b 20 5c 0d 0a 20 20 20 20 20 7d 65  CODE; \..     }e
f14d0 6c 73 65 7b 20 5c 0d 0a 20 20 20 20 20 20 20 73  lse{ \..       s
f14e0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
f14f0 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0d 0a 20 20  pending--; \..  
f1500 20 20 20 7d 20 5c 0d 0a 20 20 20 7d 0d 0a 23 65     } \..   }..#e
f1510 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d  lse..#define Sim
f1520 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
f1530 67 6e 28 58 29 0d 0a 23 64 65 66 69 6e 65 20 53  gn(X)..#define S
f1540 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
f1550 29 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  )..#define Simul
f1560 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
f1570 28 41 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  (A)..#endif..../
f1580 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  *..** When testi
f1590 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
f15a0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
f15b0 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0d 0a 2a  f open files...*
f15c0 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
f15d0 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50  _TEST..SQLITE_AP
f15e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
f15f0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20  en_file_count = 
f1600 30 3b 0d 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  0;..#define Open
f1610 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69  Counter(X)  sqli
f1620 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
f1630 75 6e 74 2b 3d 28 58 29 0d 0a 23 65 6c 73 65 0d  unt+=(X)..#else.
f1640 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
f1650 6e 74 65 72 28 58 29 0d 0a 23 65 6e 64 69 66 0d  nter(X)..#endif.
f1660 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  ...#endif /* !de
f1670 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
f1680 5f 48 5f 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  _H_) */..../****
f1690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f16a0 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
f16b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f16c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f16d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
f16e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
f16f0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
f1700 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75  left off in os_u
f1710 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
f1720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f  **********/..../
f1730 2a 0d 0a 2a 2a 20 44 65 66 69 6e 65 20 76 61 72  *..** Define var
f1740 69 6f 75 73 20 6d 61 63 72 6f 73 20 74 68 61 74  ious macros that
f1750 20 61 72 65 20 6d 69 73 73 69 6e 67 20 66 72 6f   are missing fro
f1760 6d 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 0d  m some systems..
f1770 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 4f 5f 4c  .*/..#ifndef O_L
f1780 41 52 47 45 46 49 4c 45 0d 0a 23 20 64 65 66 69  ARGEFILE..# defi
f1790 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
f17a0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
f17b0 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
f17c0 4c 46 53 0d 0a 23 20 75 6e 64 65 66 20 4f 5f 4c  LFS..# undef O_L
f17d0 41 52 47 45 46 49 4c 45 0d 0a 23 20 64 65 66 69  ARGEFILE..# defi
f17e0 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
f17f0 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65  ..#endif..#ifnde
f1800 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0d 0a 23 20  f O_NOFOLLOW..# 
f1810 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f  define O_NOFOLLO
f1820 57 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  W 0..#endif..#if
f1830 6e 64 65 66 20 4f 5f 42 49 4e 41 52 59 0d 0a 23  ndef O_BINARY..#
f1840 20 64 65 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59   define O_BINARY
f1850 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a   0..#endif..../*
f1860 0d 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69  ..** The threadi
f1870 64 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73  d macro resolves
f1880 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69   to the thread-i
f1890 64 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64  d or to 0.  Used
f18a0 20 66 6f 72 0d 0a 2a 2a 20 74 65 73 74 69 6e 67   for..** testing
f18b0 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
f18c0 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51  nly...*/..#if SQ
f18d0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0d  LITE_THREADSAFE.
f18e0 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69  .#define threadi
f18f0 64 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  d pthread_self()
f1900 0d 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69 6e 65  ..#else..#define
f1910 20 74 68 72 65 61 64 69 64 20 30 0d 0a 23 65 6e   threadid 0..#en
f1920 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 69  dif..../*..** Di
f1930 66 66 65 72 65 6e 74 20 55 6e 69 78 20 73 79 73  fferent Unix sys
f1940 74 65 6d 73 20 64 65 63 6c 61 72 65 20 6f 70 65  tems declare ope
f1950 6e 28 29 20 69 6e 20 64 69 66 66 65 72 65 6e 74  n() in different
f1960 20 77 61 79 73 2e 20 20 53 61 6d 65 20 75 73 65   ways.  Same use
f1970 0d 0a 2a 2a 20 6f 70 65 6e 28 63 6f 6e 73 74 20  ..** open(const 
f1980 63 68 61 72 2a 2c 69 6e 74 2c 6d 6f 64 65 5f 74  char*,int,mode_t
f1990 29 2e 20 20 4f 74 68 65 72 73 20 75 73 65 20 6f  ).  Others use o
f19a0 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  pen(const char*,
f19b0 69 6e 74 2c 2e 2e 2e 29 2e 0d 0a 2a 2a 20 54 68  int,...)...** Th
f19c0 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20  e difference is 
f19d0 69 6d 70 6f 72 74 61 6e 74 20 77 68 65 6e 20 75  important when u
f19e0 73 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74  sing a pointer t
f19f0 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0d  o the function..
f1a00 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 61 66 65  .**..** The safe
f1a10 73 74 20 77 61 79 20 74 6f 20 64 65 61 6c 20 77  st way to deal w
f1a20 69 74 68 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ith the problem 
f1a30 69 73 20 74 6f 20 61 6c 77 61 79 73 20 75 73 65  is to always use
f1a40 20 74 68 69 73 20 77 72 61 70 70 65 72 0d 0a 2a   this wrapper..*
f1a50 2a 20 77 68 69 63 68 20 61 6c 77 61 79 73 20 68  * which always h
f1a60 61 73 20 74 68 65 20 73 61 6d 65 20 77 65 6c 6c  as the same well
f1a70 2d 64 65 66 69 6e 65 64 20 69 6e 74 65 72 66 61  -defined interfa
f1a80 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ce...*/..static 
f1a90 69 6e 74 20 70 6f 73 69 78 4f 70 65 6e 28 63 6f  int posixOpen(co
f1aa0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c  nst char *zFile,
f1ab0 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20   int flags, int 
f1ac0 6d 6f 64 65 29 7b 0d 0a 20 20 72 65 74 75 72 6e  mode){..  return
f1ad0 20 6f 70 65 6e 28 7a 46 69 6c 65 2c 20 66 6c 61   open(zFile, fla
f1ae0 67 73 2c 20 6d 6f 64 65 29 3b 0d 0a 7d 0d 0a 0d  gs, mode);..}...
f1af0 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
f1b00 72 65 6e 63 65 20 2a 2f 0d 0a 73 74 61 74 69 63  rence */..static
f1b10 20 69 6e 74 20 6f 70 65 6e 44 69 72 65 63 74 6f   int openDirecto
f1b20 72 79 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  ry(const char*, 
f1b30 69 6e 74 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  int*);..../*..**
f1b40 20 4d 61 6e 79 20 73 79 73 74 65 6d 20 63 61 6c   Many system cal
f1b50 6c 73 20 61 72 65 20 61 63 63 65 73 73 65 64 20  ls are accessed 
f1b60 74 68 72 6f 75 67 68 20 70 6f 69 6e 74 65 72 2d  through pointer-
f1b70 74 6f 2d 66 75 6e 63 74 69 6f 6e 73 20 73 6f 20  to-functions so 
f1b80 74 68 61 74 0d 0a 2a 2a 20 74 68 65 79 20 6d 61  that..** they ma
f1b90 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20  y be overridden 
f1ba0 61 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 66 61  at runtime to fa
f1bb0 63 69 6c 69 74 61 74 65 20 66 61 75 6c 74 20 69  cilitate fault i
f1bc0 6e 6a 65 63 74 69 6f 6e 20 64 75 72 69 6e 67 0d  njection during.
f1bd0 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20  .** testing and 
f1be0 73 61 6e 64 62 6f 78 69 6e 67 2e 20 20 54 68 65  sandboxing.  The
f1bf0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79   following array
f1c00 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 73   holds the names
f1c10 20 61 6e 64 20 70 6f 69 6e 74 65 72 73 0d 0a 2a   and pointers..*
f1c20 2a 20 74 6f 20 61 6c 6c 20 6f 76 65 72 72 69 64  * to all overrid
f1c30 65 61 62 6c 65 20 73 79 73 74 65 6d 20 63 61 6c  eable system cal
f1c40 6c 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ls...*/..static 
f1c50 73 74 72 75 63 74 20 75 6e 69 78 5f 73 79 73 63  struct unix_sysc
f1c60 61 6c 6c 20 7b 0d 0a 20 20 63 6f 6e 73 74 20 63  all {..  const c
f1c70 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
f1c80 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
f1c90 66 20 74 68 65 20 73 79 74 65 6d 20 63 61 6c 6c  f the sytem call
f1ca0 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73   */..  sqlite3_s
f1cb0 79 73 63 61 6c 6c 5f 70 74 72 20 70 43 75 72 72  yscall_ptr pCurr
f1cc0 65 6e 74 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20  ent; /* Current 
f1cd0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 73  value of the sys
f1ce0 74 65 6d 20 63 61 6c 6c 20 2a 2f 0d 0a 20 20 73  tem call */..  s
f1cf0 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
f1d00 74 72 20 70 44 65 66 61 75 6c 74 3b 20 2f 2a 20  tr pDefault; /* 
f1d10 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f  Default value */
f1d20 0d 0a 7d 20 61 53 79 73 63 61 6c 6c 5b 5d 20 3d  ..} aSyscall[] =
f1d30 20 7b 0d 0a 20 20 7b 20 22 6f 70 65 6e 22 2c 20   {..  { "open", 
f1d40 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33          (sqlite3
f1d50 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 70 6f 73  _syscall_ptr)pos
f1d60 69 78 4f 70 65 6e 2c 20 20 30 20 20 7d 2c 0d 0a  ixOpen,  0  },..
f1d70 23 64 65 66 69 6e 65 20 6f 73 4f 70 65 6e 20 20  #define osOpen  
f1d80 20 20 20 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e      ((int(*)(con
f1d90 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74  st char*,int,int
f1da0 29 29 61 53 79 73 63 61 6c 6c 5b 30 5d 2e 70 43  ))aSyscall[0].pC
f1db0 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22  urrent)....  { "
f1dc0 63 6c 6f 73 65 22 2c 20 20 20 20 20 20 20 20 28  close",        (
f1dd0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
f1de0 70 74 72 29 63 6c 6f 73 65 2c 20 20 20 20 20 20  ptr)close,      
f1df0 30 20 20 7d 2c 0d 0a 23 64 65 66 69 6e 65 20 6f  0  },..#define o
f1e00 73 43 6c 6f 73 65 20 20 20 20 20 28 28 69 6e 74  sClose     ((int
f1e10 28 2a 29 28 69 6e 74 29 29 61 53 79 73 63 61 6c  (*)(int))aSyscal
f1e20 6c 5b 31 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a  l[1].pCurrent)..
f1e30 0d 0a 20 20 7b 20 22 61 63 63 65 73 73 22 2c 20  ..  { "access", 
f1e40 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73        (sqlite3_s
f1e50 79 73 63 61 6c 6c 5f 70 74 72 29 61 63 63 65 73  yscall_ptr)acces
f1e60 73 2c 20 20 20 20 20 30 20 20 7d 2c 0d 0a 23 64  s,     0  },..#d
f1e70 65 66 69 6e 65 20 6f 73 41 63 63 65 73 73 20 20  efine osAccess  
f1e80 20 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74    ((int(*)(const
f1e90 20 63 68 61 72 2a 2c 69 6e 74 29 29 61 53 79 73   char*,int))aSys
f1ea0 63 61 6c 6c 5b 32 5d 2e 70 43 75 72 72 65 6e 74  call[2].pCurrent
f1eb0 29 0d 0a 0d 0a 20 20 7b 20 22 67 65 74 63 77 64  )....  { "getcwd
f1ec0 22 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74 65  ",       (sqlite
f1ed0 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 67 65  3_syscall_ptr)ge
f1ee0 74 63 77 64 2c 20 20 20 20 20 30 20 20 7d 2c 0d  tcwd,     0  },.
f1ef0 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 63 77  .#define osGetcw
f1f00 64 20 20 20 20 28 28 63 68 61 72 2a 28 2a 29 28  d    ((char*(*)(
f1f10 63 68 61 72 2a 2c 73 69 7a 65 5f 74 29 29 61 53  char*,size_t))aS
f1f20 79 73 63 61 6c 6c 5b 33 5d 2e 70 43 75 72 72 65  yscall[3].pCurre
f1f30 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 73 74 61 74  nt)....  { "stat
f1f40 22 2c 20 20 20 20 20 20 20 20 20 28 73 71 6c 69  ",         (sqli
f1f50 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29  te3_syscall_ptr)
f1f60 73 74 61 74 2c 20 20 20 20 20 20 20 30 20 20 7d  stat,       0  }
f1f70 2c 0d 0a 23 64 65 66 69 6e 65 20 6f 73 53 74 61  ,..#define osSta
f1f80 74 20 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28  t      ((int(*)(
f1f90 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 74 72 75  const char*,stru
f1fa0 63 74 20 73 74 61 74 2a 29 29 61 53 79 73 63 61  ct stat*))aSysca
f1fb0 6c 6c 5b 34 5d 2e 70 43 75 72 72 65 6e 74 29 0d  ll[4].pCurrent).
f1fc0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 44 4a  .../*..** The DJ
f1fd0 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76  GPP compiler env
f1fe0 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d  ironment looks m
f1ff0 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c  ostly like Unix,
f2000 20 62 75 74 20 69 74 0d 0a 2a 2a 20 6c 61 63 6b   but it..** lack
f2010 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79  s the fcntl() sy
f2020 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72  stem call.  So r
f2030 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20  edefine fcntl() 
f2040 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0d  to be something.
f2050 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20  .** that always 
f2060 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20  succeeds.  This 
f2070 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69  means that locki
f2080 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75  ng does not occu
f2090 72 20 75 6e 64 65 72 0d 0a 2a 2a 20 44 4a 47 50  r under..** DJGP
f20a0 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f  P.  But it is DO
f20b0 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75  S - what did you
f20c0 20 65 78 70 65 63 74 3f 0d 0a 2a 2f 0d 0a 23 69   expect?..*/..#i
f20d0 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0d 0a  fdef __DJGPP__..
f20e0 20 20 7b 20 22 66 73 74 61 74 22 2c 20 20 20 20    { "fstat",    
f20f0 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
f2100 20 20 20 20 20 20 20 30 20 20 7d 2c 0d 0a 23 64         0  },..#d
f2110 65 66 69 6e 65 20 6f 73 46 73 74 61 74 28 61 2c  efine osFstat(a,
f2120 62 2c 63 29 20 20 20 20 30 0d 0a 23 65 6c 73 65  b,c)    0..#else
f2130 20 20 20 20 20 0d 0a 20 20 7b 20 22 66 73 74 61       ..  { "fsta
f2140 74 22 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69  t",        (sqli
f2150 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29  te3_syscall_ptr)
f2160 66 73 74 61 74 2c 20 20 20 20 20 20 30 20 20 7d  fstat,      0  }
f2170 2c 0d 0a 23 64 65 66 69 6e 65 20 6f 73 46 73 74  ,..#define osFst
f2180 61 74 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28  at     ((int(*)(
f2190 69 6e 74 2c 73 74 72 75 63 74 20 73 74 61 74 2a  int,struct stat*
f21a0 29 29 61 53 79 73 63 61 6c 6c 5b 35 5d 2e 70 43  ))aSyscall[5].pC
f21b0 75 72 72 65 6e 74 29 0d 0a 23 65 6e 64 69 66 0d  urrent)..#endif.
f21c0 0a 0d 0a 20 20 7b 20 22 66 74 72 75 6e 63 61 74  ...  { "ftruncat
f21d0 65 22 2c 20 20 20 20 28 73 71 6c 69 74 65 33 5f  e",    (sqlite3_
f21e0 73 79 73 63 61 6c 6c 5f 70 74 72 29 66 74 72 75  syscall_ptr)ftru
f21f0 6e 63 61 74 65 2c 20 20 30 20 20 7d 2c 0d 0a 23  ncate,  0  },..#
f2200 64 65 66 69 6e 65 20 6f 73 46 74 72 75 6e 63 61  define osFtrunca
f2210 74 65 20 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c  te ((int(*)(int,
f2220 6f 66 66 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b  off_t))aSyscall[
f2230 36 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  6].pCurrent)....
f2240 20 20 7b 20 22 66 63 6e 74 6c 22 2c 20 20 20 20    { "fcntl",    
f2250 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73      (sqlite3_sys
f2260 63 61 6c 6c 5f 70 74 72 29 66 63 6e 74 6c 2c 20  call_ptr)fcntl, 
f2270 20 20 20 20 20 30 20 20 7d 2c 0d 0a 23 64 65 66       0  },..#def
f2280 69 6e 65 20 6f 73 46 63 6e 74 6c 20 20 20 20 20  ine osFcntl     
f2290 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c 69 6e 74  ((int(*)(int,int
f22a0 2c 2e 2e 2e 29 29 61 53 79 73 63 61 6c 6c 5b 37  ,...))aSyscall[7
f22b0 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20  ].pCurrent).... 
f22c0 20 7b 20 22 72 65 61 64 22 2c 20 20 20 20 20 20   { "read",      
f22d0 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63     (sqlite3_sysc
f22e0 61 6c 6c 5f 70 74 72 29 72 65 61 64 2c 20 20 20  all_ptr)read,   
f22f0 20 20 20 20 30 20 20 7d 2c 0d 0a 23 64 65 66 69      0  },..#defi
f2300 6e 65 20 6f 73 52 65 61 64 20 20 20 20 20 20 28  ne osRead      (
f2310 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c  (ssize_t(*)(int,
f2320 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 29 29 61 53  void*,size_t))aS
f2330 79 73 63 61 6c 6c 5b 38 5d 2e 70 43 75 72 72 65  yscall[8].pCurre
f2340 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e  nt)....#if defin
f2350 65 64 28 55 53 45 5f 50 52 45 41 44 29 20 7c 7c  ed(USE_PREAD) ||
f2360 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
f2370 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20  OCKING_STYLE..  
f2380 7b 20 22 70 72 65 61 64 22 2c 20 20 20 20 20 20  { "pread",      
f2390 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61    (sqlite3_sysca
f23a0 6c 6c 5f 70 74 72 29 70 72 65 61 64 2c 20 20 20  ll_ptr)pread,   
f23b0 20 20 20 30 20 20 7d 2c 0d 0a 23 65 6c 73 65 0d     0  },..#else.
f23c0 0a 20 20 7b 20 22 70 72 65 61 64 22 2c 20 20 20  .  { "pread",   
f23d0 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79       (sqlite3_sy
f23e0 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20  scall_ptr)0,    
f23f0 20 20 20 20 20 20 30 20 20 7d 2c 0d 0a 23 65 6e        0  },..#en
f2400 64 69 66 0d 0a 23 64 65 66 69 6e 65 20 6f 73 50  dif..#define osP
f2410 72 65 61 64 20 20 20 20 20 28 28 73 73 69 7a 65  read     ((ssize
f2420 5f 74 28 2a 29 28 69 6e 74 2c 76 6f 69 64 2a 2c  _t(*)(int,void*,
f2430 73 69 7a 65 5f 74 2c 6f 66 66 5f 74 29 29 61 53  size_t,off_t))aS
f2440 79 73 63 61 6c 6c 5b 39 5d 2e 70 43 75 72 72 65  yscall[9].pCurre
f2450 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e  nt)....#if defin
f2460 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0d  ed(USE_PREAD64).
f2470 0a 20 20 7b 20 22 70 72 65 61 64 36 34 22 2c 20  .  { "pread64", 
f2480 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79       (sqlite3_sy
f2490 73 63 61 6c 6c 5f 70 74 72 29 70 72 65 61 64 36  scall_ptr)pread6
f24a0 34 2c 20 20 20 20 30 20 20 7d 2c 0d 0a 23 65 6c  4,    0  },..#el
f24b0 73 65 0d 0a 20 20 7b 20 22 70 72 65 61 64 36 34  se..  { "pread64
f24c0 22 2c 20 20 20 20 20 20 28 73 71 6c 69 74 65 33  ",      (sqlite3
f24d0 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20  _syscall_ptr)0, 
f24e0 20 20 20 20 20 20 20 20 20 30 20 20 7d 2c 0d 0a           0  },..
f24f0 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65 20  #endif..#define 
f2500 6f 73 50 72 65 61 64 36 34 20 20 20 28 28 73 73  osPread64   ((ss
f2510 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 76 6f 69  ize_t(*)(int,voi
f2520 64 2a 2c 73 69 7a 65 5f 74 2c 6f 66 66 5f 74 29  d*,size_t,off_t)
f2530 29 61 53 79 73 63 61 6c 6c 5b 31 30 5d 2e 70 43  )aSyscall[10].pC
f2540 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22  urrent)....  { "
f2550 77 72 69 74 65 22 2c 20 20 20 20 20 20 20 20 28  write",        (
f2560 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
f2570 70 74 72 29 77 72 69 74 65 2c 20 20 20 20 20 20  ptr)write,      
f2580 30 20 20 7d 2c 0d 0a 23 64 65 66 69 6e 65 20 6f  0  },..#define o
f2590 73 57 72 69 74 65 20 20 20 20 20 28 28 73 73 69  sWrite     ((ssi
f25a0 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f 6e 73  ze_t(*)(int,cons
f25b0 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 29 29  t void*,size_t))
f25c0 61 53 79 73 63 61 6c 6c 5b 31 31 5d 2e 70 43 75  aSyscall[11].pCu
f25d0 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65  rrent)....#if de
f25e0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29  fined(USE_PREAD)
f25f0 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   || SQLITE_ENABL
f2600 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d  E_LOCKING_STYLE.
f2610 0a 20 20 7b 20 22 70 77 72 69 74 65 22 2c 20 20  .  { "pwrite",  
f2620 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79       (sqlite3_sy
f2630 73 63 61 6c 6c 5f 70 74 72 29 70 77 72 69 74 65  scall_ptr)pwrite
f2640 2c 20 20 20 20 20 30 20 20 7d 2c 0d 0a 23 65 6c  ,     0  },..#el
f2650 73 65 0d 0a 20 20 7b 20 22 70 77 72 69 74 65 22  se..  { "pwrite"
f2660 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33  ,       (sqlite3
f2670 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20  _syscall_ptr)0, 
f2680 20 20 20 20 20 20 20 20 20 30 20 20 7d 2c 0d 0a           0  },..
f2690 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65 20  #endif..#define 
f26a0 6f 73 50 77 72 69 74 65 20 20 20 20 28 28 73 73  osPwrite    ((ss
f26b0 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f 6e  ize_t(*)(int,con
f26c0 73 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c  st void*,size_t,
f26d0 6f 66 66 5f 74 29 29 5c 0d 0a 20 20 20 20 20 20  off_t))\..      
f26e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 53                aS
f26f0 79 73 63 61 6c 6c 5b 31 32 5d 2e 70 43 75 72 72  yscall[12].pCurr
f2700 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69  ent)....#if defi
f2710 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29  ned(USE_PREAD64)
f2720 0d 0a 20 20 7b 20 22 70 77 72 69 74 65 36 34 22  ..  { "pwrite64"
f2730 2c 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73  ,     (sqlite3_s
f2740 79 73 63 61 6c 6c 5f 70 74 72 29 70 77 72 69 74  yscall_ptr)pwrit
f2750 65 36 34 2c 20 20 20 30 20 20 7d 2c 0d 0a 23 65  e64,   0  },..#e
f2760 6c 73 65 0d 0a 20 20 7b 20 22 70 77 72 69 74 65  lse..  { "pwrite
f2770 36 34 22 2c 20 20 20 20 20 28 73 71 6c 69 74 65  64",     (sqlite
f2780 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 2c  3_syscall_ptr)0,
f2790 20 20 20 20 20 20 20 20 20 20 30 20 20 7d 2c 0d            0  },.
f27a0 0a 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65  .#endif..#define
f27b0 20 6f 73 50 77 72 69 74 65 36 34 20 20 28 28 73   osPwrite64  ((s
f27c0 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f  size_t(*)(int,co
f27d0 6e 73 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74  nst void*,size_t
f27e0 2c 6f 66 66 5f 74 29 29 5c 0d 0a 20 20 20 20 20  ,off_t))\..     
f27f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
f2800 53 79 73 63 61 6c 6c 5b 31 33 5d 2e 70 43 75 72  Syscall[13].pCur
f2810 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 53 51 4c  rent)....#if SQL
f2820 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
f2830 4e 47 5f 53 54 59 4c 45 0d 0a 20 20 7b 20 22 66  NG_STYLE..  { "f
f2840 63 68 6d 6f 64 22 2c 20 20 20 20 20 20 20 28 73  chmod",       (s
f2850 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
f2860 74 72 29 66 63 68 6d 6f 64 2c 20 20 20 20 20 30  tr)fchmod,     0
f2870 20 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b    },..#else..  {
f2880 20 22 66 63 68 6d 6f 64 22 2c 20 20 20 20 20 20   "fchmod",      
f2890 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c   (sqlite3_syscal
f28a0 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 20 20  l_ptr)0,        
f28b0 20 20 30 20 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d    0  },..#endif.
f28c0 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 68 6d 6f  .#define osFchmo
f28d0 64 20 20 20 20 28 28 69 6e 74 28 2a 29 28 69 6e  d    ((int(*)(in
f28e0 74 2c 6d 6f 64 65 5f 74 29 29 61 53 79 73 63 61  t,mode_t))aSysca
f28f0 6c 6c 5b 31 34 5d 2e 70 43 75 72 72 65 6e 74 29  ll[14].pCurrent)
f2900 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
f2910 48 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f  HAVE_POSIX_FALLO
f2920 43 41 54 45 29 20 26 26 20 48 41 56 45 5f 50 4f  CATE) && HAVE_PO
f2930 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 0d 0a 20  SIX_FALLOCATE.. 
f2940 20 7b 20 22 66 61 6c 6c 6f 63 61 74 65 22 2c 20   { "fallocate", 
f2950 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63     (sqlite3_sysc
f2960 61 6c 6c 5f 70 74 72 29 70 6f 73 69 78 5f 66 61  all_ptr)posix_fa
f2970 6c 6c 6f 63 61 74 65 2c 20 20 30 20 7d 2c 0d 0a  llocate,  0 },..
f2980 23 65 6c 73 65 0d 0a 20 20 7b 20 22 66 61 6c 6c  #else..  { "fall
f2990 6f 63 61 74 65 22 2c 20 20 20 20 28 73 71 6c 69  ocate",    (sqli
f29a0 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29  te3_syscall_ptr)
f29b0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
f29c0 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a    0 },..#endif..
f29d0 23 64 65 66 69 6e 65 20 6f 73 46 61 6c 6c 6f 63  #define osFalloc
f29e0 61 74 65 20 28 28 69 6e 74 28 2a 29 28 69 6e 74  ate ((int(*)(int
f29f0 2c 6f 66 66 5f 74 2c 6f 66 66 5f 74 29 29 61 53  ,off_t,off_t))aS
f2a00 79 73 63 61 6c 6c 5b 31 35 5d 2e 70 43 75 72 72  yscall[15].pCurr
f2a10 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 75 6e 6c  ent)....  { "unl
f2a20 69 6e 6b 22 2c 20 20 20 20 20 20 20 28 73 71 6c  ink",       (sql
f2a30 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72  ite3_syscall_ptr
f2a40 29 75 6e 6c 69 6e 6b 2c 20 20 20 20 20 20 20 20  )unlink,        
f2a50 20 20 20 30 20 7d 2c 0d 0a 23 64 65 66 69 6e 65     0 },..#define
f2a60 20 6f 73 55 6e 6c 69 6e 6b 20 20 20 20 28 28 69   osUnlink    ((i
f2a70 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 72  nt(*)(const char
f2a80 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 36 5d 2e  *))aSyscall[16].
f2a90 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b  pCurrent)....  {
f2aa0 20 22 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 22   "openDirectory"
f2ab0 2c 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79  ,    (sqlite3_sy
f2ac0 73 63 61 6c 6c 5f 70 74 72 29 6f 70 65 6e 44 69  scall_ptr)openDi
f2ad0 72 65 63 74 6f 72 79 2c 20 20 20 20 20 20 30 20  rectory,      0 
f2ae0 7d 2c 0d 0a 23 64 65 66 69 6e 65 20 6f 73 4f 70  },..#define osOp
f2af0 65 6e 44 69 72 65 63 74 6f 72 79 20 28 28 69 6e  enDirectory ((in
f2b00 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  t(*)(const char*
f2b10 2c 69 6e 74 2a 29 29 61 53 79 73 63 61 6c 6c 5b  ,int*))aSyscall[
f2b20 31 37 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  17].pCurrent)...
f2b30 0a 20 20 7b 20 22 6d 6b 64 69 72 22 2c 20 20 20  .  { "mkdir",   
f2b40 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79       (sqlite3_sy
f2b50 73 63 61 6c 6c 5f 70 74 72 29 6d 6b 64 69 72 2c  scall_ptr)mkdir,
f2b60 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d             0 },.
f2b70 0a 23 64 65 66 69 6e 65 20 6f 73 4d 6b 64 69 72  .#define osMkdir
f2b80 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28 63 6f       ((int(*)(co
f2b90 6e 73 74 20 63 68 61 72 2a 2c 6d 6f 64 65 5f 74  nst char*,mode_t
f2ba0 29 29 61 53 79 73 63 61 6c 6c 5b 31 38 5d 2e 70  ))aSyscall[18].p
f2bb0 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20  Current)....  { 
f2bc0 22 72 6d 64 69 72 22 2c 20 20 20 20 20 20 20 20  "rmdir",        
f2bd0 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c  (sqlite3_syscall
f2be0 5f 70 74 72 29 72 6d 64 69 72 2c 20 20 20 20 20  _ptr)rmdir,     
f2bf0 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 64 65 66        0 },..#def
f2c00 69 6e 65 20 6f 73 52 6d 64 69 72 20 20 20 20 20  ine osRmdir     
f2c10 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63  ((int(*)(const c
f2c20 68 61 72 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31  har*))aSyscall[1
f2c30 39 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  9].pCurrent)....
f2c40 7d 3b 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65  }; /* End of the
f2c50 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 73 79   overrideable sy
f2c60 73 74 65 6d 20 63 61 6c 6c 73 20 2a 2f 0d 0a 0d  stem calls */...
f2c70 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20  ./*..** This is 
f2c80 74 68 65 20 78 53 65 74 53 79 73 74 65 6d 43 61  the xSetSystemCa
f2c90 6c 6c 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 73  ll() method of s
f2ca0 71 6c 69 74 65 33 5f 76 66 73 20 66 6f 72 20 61  qlite3_vfs for a
f2cb0 6c 6c 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 22 75  ll of the..** "u
f2cc0 6e 69 78 22 20 56 46 53 65 73 2e 20 20 52 65 74  nix" VFSes.  Ret
f2cd0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 70  urn SQLITE_OK op
f2ce0 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  on successfully 
f2cf0 75 70 64 61 74 69 6e 67 20 74 68 65 0d 0a 2a 2a  updating the..**
f2d00 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 70 6f 69   system call poi
f2d10 6e 74 65 72 2c 20 6f 72 20 53 51 4c 49 54 45 5f  nter, or SQLITE_
f2d20 4e 4f 54 46 4f 55 4e 44 20 69 66 20 74 68 65 72  NOTFOUND if ther
f2d30 65 20 69 73 20 6e 6f 20 63 6f 6e 66 69 67 75 72  e is no configur
f2d40 61 62 6c 65 0d 0a 2a 2a 20 73 79 73 74 65 6d 20  able..** system 
f2d50 63 61 6c 6c 20 6e 61 6d 65 64 20 7a 4e 61 6d 65  call named zName
f2d60 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
f2d70 74 20 75 6e 69 78 53 65 74 53 79 73 74 65 6d 43  t unixSetSystemC
f2d80 61 6c 6c 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  all(..  sqlite3_
f2d90 76 66 73 20 2a 70 4e 6f 74 55 73 65 64 2c 20 20  vfs *pNotUsed,  
f2da0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
f2db0 20 70 6f 69 6e 74 65 72 2e 20 20 4e 6f 74 20 75   pointer.  Not u
f2dc0 73 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  sed */..  const 
f2dd0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
f2de0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
f2df0 6f 66 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74  of system call t
f2e00 6f 20 6f 76 65 72 72 69 64 65 20 2a 2f 0d 0a 20  o override */.. 
f2e10 20 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c   sqlite3_syscall
f2e20 5f 70 74 72 20 70 4e 65 77 46 75 6e 63 20 20 2f  _ptr pNewFunc  /
f2e30 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77  * Pointer to new
f2e40 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 76 61 6c   system call val
f2e50 75 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 6e 73  ue */..){..  uns
f2e60 69 67 6e 65 64 20 69 6e 74 20 69 3b 0d 0a 20 20  igned int i;..  
f2e70 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f2e80 4e 4f 54 46 4f 55 4e 44 3b 0d 0a 0d 0a 20 20 55  NOTFOUND;....  U
f2e90 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
f2ea0 70 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 69 66  pNotUsed);..  if
f2eb0 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20  ( zName==0 ){.. 
f2ec0 20 20 20 2f 2a 20 49 66 20 6e 6f 20 7a 4e 61 6d     /* If no zNam
f2ed0 65 20 69 73 20 67 69 76 65 6e 2c 20 72 65 73 74  e is given, rest
f2ee0 6f 72 65 20 61 6c 6c 20 73 79 73 74 65 6d 20 63  ore all system c
f2ef0 61 6c 6c 73 20 74 6f 20 74 68 65 69 72 20 64 65  alls to their de
f2f00 66 61 75 6c 74 0d 0a 20 20 20 20 2a 2a 20 73 65  fault..    ** se
f2f10 74 74 69 6e 67 73 20 61 6e 64 20 72 65 74 75 72  ttings and retur
f2f20 6e 20 4e 55 4c 4c 0d 0a 20 20 20 20 2a 2f 0d 0a  n NULL..    */..
f2f30 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
f2f40 4f 4b 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30  OK;..    for(i=0
f2f50 3b 20 69 3c 73 69 7a 65 6f 66 28 61 53 79 73 63  ; i<sizeof(aSysc
f2f60 61 6c 6c 29 2f 73 69 7a 65 6f 66 28 61 53 79 73  all)/sizeof(aSys
f2f70 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0d  call[0]); i++){.
f2f80 0a 20 20 20 20 20 20 69 66 28 20 61 53 79 73 63  .      if( aSysc
f2f90 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 20  all[i].pDefault 
f2fa0 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 53 79 73  ){..        aSys
f2fb0 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74  call[i].pCurrent
f2fc0 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70   = aSyscall[i].p
f2fd0 44 65 66 61 75 6c 74 3b 0d 0a 20 20 20 20 20 20  Default;..      
f2fe0 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  }..    }..  }els
f2ff0 65 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e  e{..    /* If zN
f3000 61 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64  ame is specified
f3010 2c 20 6f 70 65 72 61 74 65 20 6f 6e 20 6f 6e 6c  , operate on onl
f3020 79 20 74 68 65 20 6f 6e 65 20 73 79 73 74 65 6d  y the one system
f3030 20 63 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 73 70   call..    ** sp
f3040 65 63 69 66 69 65 64 2e 0d 0a 20 20 20 20 2a 2f  ecified...    */
f3050 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
f3060 3c 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c  <sizeof(aSyscall
f3070 29 2f 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c  )/sizeof(aSyscal
f3080 6c 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20  l[0]); i++){..  
f3090 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
f30a0 4e 61 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69  Name, aSyscall[i
f30b0 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a  ].zName)==0 ){..
f30c0 20 20 20 20 20 20 20 20 69 66 28 20 61 53 79 73          if( aSys
f30d0 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74  call[i].pDefault
f30e0 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
f30f0 20 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 44    aSyscall[i].pD
f3100 65 66 61 75 6c 74 20 3d 20 61 53 79 73 63 61 6c  efault = aSyscal
f3110 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 3b 0d 0a  l[i].pCurrent;..
f3120 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
f3130 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
f3140 4b 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  K;..        if( 
f3150 70 4e 65 77 46 75 6e 63 3d 3d 30 20 29 20 70 4e  pNewFunc==0 ) pN
f3160 65 77 46 75 6e 63 20 3d 20 61 53 79 73 63 61 6c  ewFunc = aSyscal
f3170 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 3b 0d 0a  l[i].pDefault;..
f3180 20 20 20 20 20 20 20 20 61 53 79 73 63 61 6c 6c          aSyscall
f3190 5b 69 5d 2e 70 43 75 72 72 65 6e 74 20 3d 20 70  [i].pCurrent = p
f31a0 4e 65 77 46 75 6e 63 3b 0d 0a 20 20 20 20 20 20  NewFunc;..      
f31b0 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
f31c0 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  }..    }..  }.. 
f31d0 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
f31e0 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
f31f0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73  the value of a s
f3200 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 52 65 74  ystem call.  Ret
f3210 75 72 6e 20 4e 55 4c 4c 20 69 66 20 7a 4e 61 6d  urn NULL if zNam
f3220 65 20 69 73 20 6e 6f 74 20 61 0d 0a 2a 2a 20 72  e is not a..** r
f3230 65 63 6f 67 6e 69 7a 65 64 20 73 79 73 74 65 6d  ecognized system
f3240 20 63 61 6c 6c 20 6e 61 6d 65 2e 20 20 4e 55 4c   call name.  NUL
f3250 4c 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e  L is also return
f3260 65 64 20 69 66 20 74 68 65 20 73 79 73 74 65 6d  ed if the system
f3270 20 63 61 6c 6c 0d 0a 2a 2a 20 69 73 20 63 75 72   call..** is cur
f3280 72 65 6e 74 6c 79 20 75 6e 64 65 66 69 6e 65 64  rently undefined
f3290 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 71  ...*/..static sq
f32a0 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
f32b0 72 20 75 6e 69 78 47 65 74 53 79 73 74 65 6d 43  r unixGetSystemC
f32c0 61 6c 6c 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  all(..  sqlite3_
f32d0 76 66 73 20 2a 70 4e 6f 74 55 73 65 64 2c 0d 0a  vfs *pNotUsed,..
f32e0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
f32f0 61 6d 65 0d 0a 29 7b 0d 0a 20 20 75 6e 73 69 67  ame..){..  unsig
f3300 6e 65 64 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20  ned int i;....  
f3310 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f3320 28 70 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 66  (pNotUsed);..  f
f3330 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
f3340 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f  (aSyscall)/sizeo
f3350 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20  f(aSyscall[0]); 
f3360 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 73  i++){..    if( s
f3370 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 61 53 79  trcmp(zName, aSy
f3380 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d  scall[i].zName)=
f3390 3d 30 20 29 20 72 65 74 75 72 6e 20 61 53 79 73  =0 ) return aSys
f33a0 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74  call[i].pCurrent
f33b0 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
f33c0 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
f33d0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
f33e0 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 73 79   of the first sy
f33f0 73 74 65 6d 20 63 61 6c 6c 20 61 66 74 65 72 20  stem call after 
f3400 7a 4e 61 6d 65 2e 20 20 49 66 20 7a 4e 61 6d 65  zName.  If zName
f3410 3d 3d 4e 55 4c 4c 0d 0a 2a 2a 20 74 68 65 6e 20  ==NULL..** then 
f3420 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  return the name 
f3430 6f 66 20 74 68 65 20 66 69 72 73 74 20 73 79 73  of the first sys
f3440 74 65 6d 20 63 61 6c 6c 2e 20 20 52 65 74 75 72  tem call.  Retur
f3450 6e 20 4e 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 0d  n NULL if zName.
f3460 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74 20  .** is the last 
f3470 73 79 73 74 65 6d 20 63 61 6c 6c 20 6f 72 20 69  system call or i
f3480 66 20 7a 4e 61 6d 65 20 69 73 20 6e 6f 74 20 74  f zName is not t
f3490 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 61 6c  he name of a val
f34a0 69 64 0d 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61  id..** system ca
f34b0 6c 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ll...*/..static 
f34c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 75 6e 69 78  const char *unix
f34d0 4e 65 78 74 53 79 73 74 65 6d 43 61 6c 6c 28 73  NextSystemCall(s
f34e0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 2c 20 63  qlite3_vfs *p, c
f34f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
f3500 29 7b 0d 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31  ){..  int i = -1
f3510 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  ;....  UNUSED_PA
f3520 52 41 4d 45 54 45 52 28 70 29 3b 0d 0a 20 20 69  RAMETER(p);..  i
f3530 66 28 20 7a 4e 61 6d 65 20 29 7b 0d 0a 20 20 20  f( zName ){..   
f3540 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
f3550 79 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 2d  ySize(aSyscall)-
f3560 31 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  1; i++){..      
f3570 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65  if( strcmp(zName
f3580 2c 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e  , aSyscall[i].zN
f3590 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ame)==0 ) break;
f35a0 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
f35b0 66 6f 72 28 69 2b 2b 3b 20 69 3c 41 72 72 61 79  for(i++; i<Array
f35c0 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 3b 20  Size(aSyscall); 
f35d0 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 61  i++){..    if( a
f35e0 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72  Syscall[i].pCurr
f35f0 65 6e 74 21 3d 30 20 29 20 72 65 74 75 72 6e 20  ent!=0 ) return 
f3600 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d  aSyscall[i].zNam
f3610 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  e;..  }..  retur
f3620 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n 0;..}..../*..*
f3630 2a 20 52 65 74 72 79 20 6f 70 65 6e 28 29 20 63  * Retry open() c
f3640 61 6c 6c 73 20 74 68 61 74 20 66 61 69 6c 20 64  alls that fail d
f3650 75 65 20 74 6f 20 45 49 4e 54 52 0d 0a 2a 2f 0d  ue to EINTR..*/.
f3660 0a 73 74 61 74 69 63 20 69 6e 74 20 72 6f 62 75  .static int robu
f3670 73 74 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68  st_open(const ch
f3680 61 72 20 2a 7a 2c 20 69 6e 74 20 66 2c 20 69 6e  ar *z, int f, in
f3690 74 20 6d 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  t m){..  int rc;
f36a0 0d 0a 20 20 64 6f 7b 20 72 63 20 3d 20 6f 73 4f  ..  do{ rc = osO
f36b0 70 65 6e 28 7a 2c 66 2c 6d 29 3b 20 7d 77 68 69  pen(z,f,m); }whi
f36c0 6c 65 28 20 72 63 3c 30 20 26 26 20 65 72 72 6e  le( rc<0 && errn
f36d0 6f 3d 3d 45 49 4e 54 52 20 29 3b 0d 0a 20 20 72  o==EINTR );..  r
f36e0 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
f36f0 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  /*..** Helper fu
f3700 6e 63 74 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69  nctions to obtai
f3710 6e 20 61 6e 64 20 72 65 6c 69 6e 71 75 69 73 68  n and relinquish
f3720 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   the global mute
f3730 78 2e 20 54 68 65 0d 0a 2a 2a 20 67 6c 6f 62 61  x. The..** globa
f3740 6c 20 6d 75 74 65 78 20 69 73 20 75 73 65 64 20  l mutex is used 
f3750 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65 20 75  to protect the u
f3760 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 61 6e 64  nixInodeInfo and
f3770 0d 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 69 6c 65  ..** vxworksFile
f3780 49 64 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20  Id objects used 
f3790 62 79 20 74 68 69 73 20 66 69 6c 65 2c 20 61 6c  by this file, al
f37a0 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79 20 62  l of which may b
f37b0 65 20 0d 0a 2a 2a 20 73 68 61 72 65 64 20 62 79  e ..** shared by
f37c0 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64   multiple thread
f37d0 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 75 6e 63 74  s...**..** Funct
f37e0 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  ion unixMutexHel
f37f0 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61  d() is used to a
f3800 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
f3810 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0d 0a   global mutex ..
f3820 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20  ** is held when 
f3830 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66  required. This f
f3840 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
f3850 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
f3860 61 73 73 65 72 74 28 29 20 0d 0a 2a 2a 20 73 74  assert() ..** st
f3870 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0d 0a  atements. e.g...
f3880 2a 2a 0d 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74  **..**   unixEnt
f3890 65 72 4d 75 74 65 78 28 29 0d 0a 2a 2a 20 20 20  erMutex()..**   
f38a0 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75    assert( unixMu
f38b0 74 65 78 48 65 6c 64 28 29 20 29 3b 0d 0a 2a 2a  texHeld() );..**
f38c0 20 20 20 75 6e 69 78 45 6e 74 65 72 4c 65 61 76     unixEnterLeav
f38d0 65 28 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  e()..*/..static 
f38e0 76 6f 69 64 20 75 6e 69 78 45 6e 74 65 72 4d 75  void unixEnterMu
f38f0 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20 20 73 71  tex(void){..  sq
f3900 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
f3910 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  r(sqlite3MutexAl
f3920 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
f3930 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
f3940 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69  ;..}..static voi
f3950 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78  d unixLeaveMutex
f3960 28 76 6f 69 64 29 7b 0d 0a 20 20 73 71 6c 69 74  (void){..  sqlit
f3970 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
f3980 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
f3990 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
f39a0 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0d 0a  ATIC_MASTER));..
f39b0 7d 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
f39c0 5f 44 45 42 55 47 0d 0a 73 74 61 74 69 63 20 69  _DEBUG..static i
f39d0 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64  nt unixMutexHeld
f39e0 28 76 6f 69 64 29 20 7b 0d 0a 20 20 72 65 74 75  (void) {..  retu
f39f0 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  rn sqlite3_mutex
f3a00 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74  _held(sqlite3Mut
f3a10 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
f3a20 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
f3a30 45 52 29 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  ER));..}..#endif
f3a40 0d 0a 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  ......#if define
f3a50 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
f3a60 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
f3a70 5f 44 45 42 55 47 29 0d 0a 2f 2a 0d 0a 2a 2a 20  _DEBUG)../*..** 
f3a80 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
f3a90 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74  for printing out
f3aa0 20 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69   trace informati
f3ab0 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e  on from debuggin
f3ac0 67 0d 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20  g..** binaries. 
f3ad0 54 68 69 73 20 72 65 74 75 72 6e 73 20 74 68 65  This returns the
f3ae0 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 74   string represet
f3af0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70  ation of the sup
f3b00 70 6c 69 65 64 0d 0a 2a 2a 20 69 6e 74 65 67 65  plied..** intege
f3b10 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0d 0a 2a 2f  r lock-type...*/
f3b20 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  ..static const c
f3b30 68 61 72 20 2a 61 7a 46 69 6c 65 4c 6f 63 6b 28  har *azFileLock(
f3b40 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 7b 0d  int eFileLock){.
f3b50 0a 20 20 73 77 69 74 63 68 28 20 65 46 69 6c 65  .  switch( eFile
f3b60 4c 6f 63 6b 20 29 7b 0d 0a 20 20 20 20 63 61 73  Lock ){..    cas
f3b70 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74 75 72  e NO_LOCK: retur
f3b80 6e 20 22 4e 4f 4e 45 22 3b 0d 0a 20 20 20 20 63  n "NONE";..    c
f3b90 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3a  ase SHARED_LOCK:
f3ba0 20 72 65 74 75 72 6e 20 22 53 48 41 52 45 44 22   return "SHARED"
f3bb0 3b 0d 0a 20 20 20 20 63 61 73 65 20 52 45 53 45  ;..    case RESE
f3bc0 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72  RVED_LOCK: retur
f3bd0 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0d 0a 20  n "RESERVED";.. 
f3be0 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f     case PENDING_
f3bf0 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45  LOCK: return "PE
f3c00 4e 44 49 4e 47 22 3b 0d 0a 20 20 20 20 63 61 73  NDING";..    cas
f3c10 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  e EXCLUSIVE_LOCK
f3c20 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53  : return "EXCLUS
f3c30 49 56 45 22 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  IVE";..  }..  re
f3c40 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0d 0a 7d  turn "ERROR";..}
f3c50 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64  ..#endif....#ifd
f3c60 65 66 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54  ef SQLITE_LOCK_T
f3c70 52 41 43 45 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 69  RACE../*..** Pri
f3c80 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 74 69  nt out informati
f3c90 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c 6f 63  on about all loc
f3ca0 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e  king operations.
f3cb0 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ..**..** This ro
f3cc0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
f3cd0 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e  r troubleshootin
f3ce0 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69  g locks on multi
f3cf0 74 68 72 65 61 64 65 64 0d 0a 2a 2a 20 70 6c 61  threaded..** pla
f3d00 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20  tforms.  Enable 
f3d10 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74  by compiling wit
f3d20 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c  h the -DSQLITE_L
f3d30 4f 43 4b 5f 54 52 41 43 45 0d 0a 2a 2a 20 63 6f  OCK_TRACE..** co
f3d40 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f  mmand-line optio
f3d50 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  n on the compile
f3d60 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73  r.  This code is
f3d70 20 6e 6f 72 6d 61 6c 6c 79 0d 0a 2a 2a 20 74 75   normally..** tu
f3d80 72 6e 65 64 20 6f 66 66 2e 0d 0a 2a 2f 0d 0a 73  rned off...*/..s
f3d90 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72  tatic int lockTr
f3da0 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ace(int fd, int 
f3db0 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  op, struct flock
f3dc0 20 2a 70 29 7b 0d 0a 20 20 63 68 61 72 20 2a 7a   *p){..  char *z
f3dd0 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0d  OpName, *zType;.
f3de0 0a 20 20 69 6e 74 20 73 3b 0d 0a 20 20 69 6e 74  .  int s;..  int
f3df0 20 73 61 76 65 64 45 72 72 6e 6f 3b 0d 0a 20 20   savedErrno;..  
f3e00 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20  if( op==F_GETLK 
f3e10 29 7b 0d 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20  ){..    zOpName 
f3e20 3d 20 22 47 45 54 4c 4b 22 3b 0d 0a 20 20 7d 65  = "GETLK";..  }e
f3e30 6c 73 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45  lse if( op==F_SE
f3e40 54 4c 4b 20 29 7b 0d 0a 20 20 20 20 7a 4f 70 4e  TLK ){..    zOpN
f3e50 61 6d 65 20 3d 20 22 53 45 54 4c 4b 22 3b 0d 0a  ame = "SETLK";..
f3e60 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 20    }else{..    s 
f3e70 3d 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 6f 70  = osFcntl(fd, op
f3e80 2c 20 70 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  , p);..    sqlit
f3e90 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66  e3DebugPrintf("f
f3ea0 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20  cntl unknown %d 
f3eb0 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70  %d %d\n", fd, op
f3ec0 2c 20 73 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  , s);..    retur
f3ed0 6e 20 73 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  n s;..  }..  if(
f3ee0 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44   p->l_type==F_RD
f3ef0 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 7a 54 79 70  LCK ){..    zTyp
f3f00 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0d 0a 20 20  e = "RDLCK";..  
f3f10 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74  }else if( p->l_t
f3f20 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0d  ype==F_WRLCK ){.
f3f30 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52  .    zType = "WR
f3f40 4c 43 4b 22 3b 0d 0a 20 20 7d 65 6c 73 65 20 69  LCK";..  }else i
f3f50 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  f( p->l_type==F_
f3f60 55 4e 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 7a 54  UNLCK ){..    zT
f3f70 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0d 0a  ype = "UNLCK";..
f3f80 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73    }else{..    as
f3f90 73 65 72 74 28 20 30 20 29 3b 0d 0a 20 20 7d 0d  sert( 0 );..  }.
f3fa0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 5f  .  assert( p->l_
f3fb0 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f 53 45 54  whence==SEEK_SET
f3fc0 20 29 3b 0d 0a 20 20 73 20 3d 20 6f 73 46 63 6e   );..  s = osFcn
f3fd0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0d 0a  tl(fd, op, p);..
f3fe0 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65    savedErrno = e
f3ff0 72 72 6e 6f 3b 0d 0a 20 20 73 71 6c 69 74 65 33  rrno;..  sqlite3
f4000 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
f4010 74 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25  tl %d %d %s %s %
f4020 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0d 0a  d %d %d %d\n",..
f4030 20 20 20 20 20 74 68 72 65 61 64 69 64 2c 20 66       threadid, f
f4040 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70  d, zOpName, zTyp
f4050 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61  e, (int)p->l_sta
f4060 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65  rt, (int)p->l_le
f4070 6e 2c 0d 0a 20 20 20 20 20 28 69 6e 74 29 70 2d  n,..     (int)p-
f4080 3e 6c 5f 70 69 64 2c 20 73 29 3b 0d 0a 20 20 69  >l_pid, s);..  i
f4090 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70  f( s==(-1) && op
f40a0 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d  ==F_SETLK && (p-
f40b0 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b  >l_type==F_RDLCK
f40c0 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46   || p->l_type==F
f40d0 5f 57 52 4c 43 4b 29 20 29 7b 0d 0a 20 20 20 20  _WRLCK) ){..    
f40e0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b  struct flock l2;
f40f0 0d 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0d 0a  ..    l2 = *p;..
f4100 20 20 20 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20      osFcntl(fd, 
f4110 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0d 0a  F_GETLK, &l2);..
f4120 20 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70      if( l2.l_typ
f4130 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0d 0a 20  e==F_RDLCK ){.. 
f4140 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44       zType = "RD
f4150 4c 43 4b 22 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  LCK";..    }else
f4160 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d   if( l2.l_type==
f4170 46 5f 57 52 4c 43 4b 20 29 7b 0d 0a 20 20 20 20  F_WRLCK ){..    
f4180 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b    zType = "WRLCK
f4190 22 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ";..    }else if
f41a0 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55  ( l2.l_type==F_U
f41b0 4e 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 20 20 7a  NLCK ){..      z
f41c0 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0d  Type = "UNLCK";.
f41d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
f41e0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0d     assert( 0 );.
f41f0 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
f4200 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
f4210 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65  fcntl-failure-re
f4220 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25  ason: %s %d %d %
f4230 64 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20 7a 54  d\n",..       zT
f4240 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73  ype, (int)l2.l_s
f4250 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f  tart, (int)l2.l_
f4260 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70  len, (int)l2.l_p
f4270 69 64 29 3b 0d 0a 20 20 7d 0d 0a 20 20 65 72 72  id);..  }..  err
f4280 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b  no = savedErrno;
f4290 0d 0a 20 20 72 65 74 75 72 6e 20 73 3b 0d 0a 7d  ..  return s;..}
f42a0 0d 0a 23 75 6e 64 65 66 20 6f 73 46 63 6e 74 6c  ..#undef osFcntl
f42b0 0d 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 6e 74  ..#define osFcnt
f42c0 6c 20 6c 6f 63 6b 54 72 61 63 65 0d 0a 23 65 6e  l lockTrace..#en
f42d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f  dif /* SQLITE_LO
f42e0 43 4b 5f 54 52 41 43 45 20 2a 2f 0d 0a 0d 0a 2f  CK_TRACE */..../
f42f0 2a 0d 0a 2a 2a 20 52 65 74 72 79 20 66 74 72 75  *..** Retry ftru
f4300 6e 63 61 74 65 28 29 20 63 61 6c 6c 73 20 74 68  ncate() calls th
f4310 61 74 20 66 61 69 6c 20 64 75 65 20 74 6f 20 45  at fail due to E
f4320 49 4e 54 52 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  INTR..*/..static
f4330 20 69 6e 74 20 72 6f 62 75 73 74 5f 66 74 72 75   int robust_ftru
f4340 6e 63 61 74 65 28 69 6e 74 20 68 2c 20 73 71 6c  ncate(int h, sql
f4350 69 74 65 33 5f 69 6e 74 36 34 20 73 7a 29 7b 0d  ite3_int64 sz){.
f4360 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 64 6f  .  int rc;..  do
f4370 7b 20 72 63 20 3d 20 6f 73 46 74 72 75 6e 63 61  { rc = osFtrunca
f4380 74 65 28 68 2c 73 7a 29 3b 20 7d 77 68 69 6c 65  te(h,sz); }while
f4390 28 20 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d  ( rc<0 && errno=
f43a0 3d 45 49 4e 54 52 20 29 3b 0d 0a 20 20 72 65 74  =EINTR );..  ret
f43b0 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
f43c0 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
f43d0 65 20 74 72 61 6e 73 6c 61 74 65 73 20 61 20 73  e translates a s
f43e0 74 61 6e 64 61 72 64 20 50 4f 53 49 58 20 65 72  tandard POSIX er
f43f0 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f 20 73 6f  rno code into so
f4400 6d 65 74 68 69 6e 67 0d 0a 2a 2a 20 75 73 65 66  mething..** usef
f4410 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65 6e 74  ul to the client
f4420 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  s of the sqlite3
f4430 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53 70 65   functions.  Spe
f4440 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 69 73  cifically, it is
f4450 0d 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74 6f  ..** intended to
f4460 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61 72   translate a var
f4470 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67 61  iety of "try aga
f4480 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20  in" errors into 
f4490 53 51 4c 49 54 45 5f 42 55 53 59 0d 0a 2a 2a 20  SQLITE_BUSY..** 
f44a0 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66  and a variety of
f44b0 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74   "please close t
f44c0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
f44d0 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69  or NOW" errors i
f44e0 6e 74 6f 20 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f  nto ..** SQLITE_
f44f0 49 4f 45 52 52 0d 0a 2a 2a 20 0d 0a 2a 2a 20 45  IOERR..** ..** E
f4500 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69 6e 69  rrors during ini
f4510 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c  tialization of l
f4520 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 79  ocks, or file sy
f4530 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66 6f 72  stem support for
f4540 20 6c 6f 63 6b 73 2c 0d 0a 2a 2a 20 73 68 6f 75   locks,..** shou
f4550 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b  ld handle ENOLCK
f4560 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f  , ENOTSUP, EOPNO
f4570 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c 79  TSUPP separately
f4580 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
f4590 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  t sqliteErrorFro
f45a0 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20  mPosixError(int 
f45b0 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20  posixError, int 
f45c0 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0d 0a  sqliteIOErr) {..
f45d0 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45    switch (posixE
f45e0 72 72 6f 72 29 20 7b 0d 0a 23 69 66 20 30 0d 0a  rror) {..#if 0..
f45f0 20 20 2f 2a 20 41 74 20 6f 6e 65 20 70 6f 69 6e    /* At one poin
f4600 74 20 74 68 69 73 20 63 6f 64 65 20 77 61 73 20  t this code was 
f4610 6e 6f 74 20 63 6f 6d 6d 65 6e 74 65 64 20 6f 75  not commented ou
f4620 74 2e 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68  t. In theory, th
f4630 69 73 20 62 72 61 6e 63 68 0d 0a 20 20 2a 2a 20  is branch..  ** 
f4640 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
f4650 68 69 74 2c 20 61 73 20 74 68 69 73 20 66 75 6e  hit, as this fun
f4660 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c  ction should onl
f4670 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  y be called afte
f4680 72 0d 0a 20 20 2a 2a 20 61 20 6c 6f 63 6b 69 6e  r..  ** a lockin
f4690 67 2d 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69  g-related functi
f46a0 6f 6e 20 28 69 2e 65 2e 20 66 63 6e 74 6c 28 29  on (i.e. fcntl()
f46b0 29 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 6e  ) has returned n
f46c0 6f 6e 2d 7a 65 72 6f 20 77 69 74 68 0d 0a 20 20  on-zero with..  
f46d0 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
f46e0 65 72 72 6e 6f 20 61 73 20 74 68 65 20 66 69 72  errno as the fir
f46f0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 53 69 6e  st argument. Sin
f4700 63 65 20 61 20 73 79 73 74 65 6d 20 63 61 6c 6c  ce a system call
f4710 20 68 61 73 20 66 61 69 6c 65 64 2c 0d 0a 20 20   has failed,..  
f4720 2a 2a 20 65 72 72 6e 6f 20 73 68 6f 75 6c 64 20  ** errno should 
f4730 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 0d 0a 20 20  be non-zero...  
f4740 2a 2a 0d 0a 20 20 2a 2a 20 44 65 73 70 69 74 65  **..  ** Despite
f4750 20 74 68 69 73 2c 20 69 66 20 65 72 72 6e 6f 20   this, if errno 
f4760 72 65 61 6c 6c 79 20 69 73 20 7a 65 72 6f 2c 20  really is zero, 
f4770 77 65 20 73 74 69 6c 6c 20 64 6f 6e 27 74 20 77  we still don't w
f4780 61 6e 74 20 74 6f 20 72 65 74 75 72 6e 0d 0a 20  ant to return.. 
f4790 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54   ** SQLITE_OK. T
f47a0 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 66  he system call f
f47b0 61 69 6c 65 64 2c 20 61 6e 64 20 2a 73 6f 6d 65  ailed, and *some
f47c0 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 73  * SQLite error s
f47d0 68 6f 75 6c 64 20 62 65 0d 0a 20 20 2a 2a 20 70  hould be..  ** p
f47e0 72 6f 70 61 67 61 74 65 64 20 62 61 63 6b 20 74  ropagated back t
f47f0 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 43 6f  o the caller. Co
f4800 6d 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 62 72  mmenting this br
f4810 61 6e 63 68 20 6f 75 74 20 6d 65 61 6e 73 20 65  anch out means e
f4820 72 72 6e 6f 3d 3d 30 0d 0a 20 20 2a 2a 20 77 69  rrno==0..  ** wi
f4830 6c 6c 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79  ll be handled by
f4840 20 74 68 65 20 22 64 65 66 61 75 6c 74 3a 22 20   the "default:" 
f4850 63 61 73 65 20 62 65 6c 6f 77 2e 0d 0a 20 20 2a  case below...  *
f4860 2f 0d 0a 20 20 63 61 73 65 20 30 3a 20 0d 0a 20  /..  case 0: .. 
f4870 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f4880 5f 4f 4b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  _OK;..#endif....
f4890 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0d 0a    case EAGAIN:..
f48a0 20 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54    case ETIMEDOUT
f48b0 3a 0d 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a  :..  case EBUSY:
f48c0 0d 0a 20 20 63 61 73 65 20 45 49 4e 54 52 3a 0d  ..  case EINTR:.
f48d0 0a 20 20 63 61 73 65 20 45 4e 4f 4c 43 4b 3a 20  .  case ENOLCK: 
f48e0 20 0d 0a 20 20 20 20 2f 2a 20 72 61 6e 64 6f 6d   ..    /* random
f48f0 20 4e 46 53 20 72 65 74 72 79 20 65 72 72 6f 72   NFS retry error
f4900 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20  , unless during 
f4910 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70  file system supp
f4920 6f 72 74 20 0d 0a 20 20 20 20 20 2a 20 69 6e 74  ort ..     * int
f4930 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77  rospection, in w
f4940 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79  hich it actually
f4950 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73   means what it s
f4960 61 79 73 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75  ays */..    retu
f4970 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d  rn SQLITE_BUSY;.
f4980 0a 20 20 20 20 0d 0a 20 20 63 61 73 65 20 45 41  .    ..  case EA
f4990 43 43 45 53 3a 20 0d 0a 20 20 20 20 2f 2a 20 45  CCES: ..    /* E
f49a0 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41  ACCES is like EA
f49b0 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b  GAIN during lock
f49c0 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20  ing operations, 
f49d0 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65  but not any othe
f49e0 72 20 74 69 6d 65 2a 2f 0d 0a 20 20 20 20 69 66  r time*/..    if
f49f0 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d  ( (sqliteIOErr =
f4a00 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  = SQLITE_IOERR_L
f4a10 4f 43 4b 29 20 7c 7c 20 0d 0a 09 28 73 71 6c 69  OCK) || ...(sqli
f4a20 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54  teIOErr == SQLIT
f4a30 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20  E_IOERR_UNLOCK) 
f4a40 7c 7c 20 0d 0a 09 28 73 71 6c 69 74 65 49 4f 45  || ...(sqliteIOE
f4a50 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  rr == SQLITE_IOE
f4a60 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0d 0a 09  RR_RDLOCK) ||...
f4a70 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
f4a80 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
f4a90 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20  CKRESERVEDLOCK) 
f4aa0 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
f4ab0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
f4ac0 20 20 20 7d 0d 0a 20 20 20 20 2f 2a 20 65 6c 73     }..    /* els
f4ad0 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a  e fall through *
f4ae0 2f 0d 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a  /..  case EPERM:
f4af0 20 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   ..    return SQ
f4b00 4c 49 54 45 5f 50 45 52 4d 3b 0d 0a 20 20 20 20  LITE_PERM;..    
f4b10 0d 0a 20 20 2f 2a 20 45 44 45 41 44 4c 4b 20 69  ..  /* EDEADLK i
f4b20 73 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20  s only possible 
f4b30 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 66 63 6e  if a call to fcn
f4b40 74 6c 28 46 5f 53 45 54 4c 4b 57 29 20 69 73 20  tl(F_SETLKW) is 
f4b50 6d 61 64 65 2e 20 41 6e 64 0d 0a 20 20 2a 2a 20  made. And..  ** 
f4b60 74 68 69 73 20 6d 6f 64 75 6c 65 20 6e 65 76 65  this module neve
f4b70 72 20 6d 61 6b 65 73 20 73 75 63 68 20 61 20 63  r makes such a c
f4b80 61 6c 6c 2e 20 41 6e 64 20 74 68 65 20 63 6f 64  all. And the cod
f4b90 65 20 69 6e 20 53 51 4c 69 74 65 20 69 74 73 65  e in SQLite itse
f4ba0 6c 66 20 0d 0a 20 20 2a 2a 20 61 73 73 65 72 74  lf ..  ** assert
f4bb0 73 20 74 68 61 74 20 53 51 4c 49 54 45 5f 49 4f  s that SQLITE_IO
f4bc0 45 52 52 5f 42 4c 4f 43 4b 45 44 20 69 73 20 6e  ERR_BLOCKED is n
f4bd0 65 76 65 72 20 72 65 74 75 72 6e 65 64 2e 20 46  ever returned. F
f4be0 6f 72 20 74 68 65 73 65 20 72 65 61 73 6f 6e 73  or these reasons
f4bf0 0d 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65  ..  ** this case
f4c00 20 69 73 20 61 6c 73 6f 20 63 6f 6d 6d 65 6e 74   is also comment
f4c10 65 64 20 6f 75 74 2e 20 49 66 20 74 68 65 20 73  ed out. If the s
f4c20 79 73 74 65 6d 20 64 6f 65 73 20 73 65 74 20 65  ystem does set e
f4c30 72 72 6e 6f 20 74 6f 20 45 44 45 41 44 4c 4b 2c  rrno to EDEADLK,
f4c40 0d 0a 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75  ..  ** the defau
f4c50 6c 74 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  lt SQLITE_IOERR_
f4c60 58 58 58 20 63 6f 64 65 20 77 69 6c 6c 20 62 65  XXX code will be
f4c70 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0d 0a 23   returned. */..#
f4c80 69 66 20 30 0d 0a 20 20 63 61 73 65 20 45 44 45  if 0..  case EDE
f4c90 41 44 4c 4b 3a 0d 0a 20 20 20 20 72 65 74 75 72  ADLK:..    retur
f4ca0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42  n SQLITE_IOERR_B
f4cb0 4c 4f 43 4b 45 44 3b 0d 0a 23 65 6e 64 69 66 0d  LOCKED;..#endif.
f4cc0 0a 20 20 20 20 0d 0a 23 69 66 20 45 4f 50 4e 4f  .    ..#if EOPNO
f4cd0 54 53 55 50 50 21 3d 45 4e 4f 54 53 55 50 0d 0a  TSUPP!=ENOTSUP..
f4ce0 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 53 55 50    case EOPNOTSUP
f4cf0 50 3a 20 0d 0a 20 20 20 20 2f 2a 20 73 6f 6d 65  P: ..    /* some
f4d00 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 72 69  thing went terri
f4d10 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 73 73  bly awry, unless
f4d20 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73   during file sys
f4d30 74 65 6d 20 73 75 70 70 6f 72 74 20 0d 0a 20 20  tem support ..  
f4d40 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69     * introspecti
f4d50 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20  on, in which it 
f4d60 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77  actually means w
f4d70 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0d 0a  hat it says */..
f4d80 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 45  #endif..#ifdef E
f4d90 4e 4f 54 53 55 50 0d 0a 20 20 63 61 73 65 20 45  NOTSUP..  case E
f4da0 4e 4f 54 53 55 50 3a 20 0d 0a 20 20 20 20 2f 2a  NOTSUP: ..    /*
f4db0 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c   invalid fd, unl
f4dc0 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20  ess during file 
f4dd0 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 69  system support i
f4de0 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e  ntrospection, in
f4df0 20 77 68 69 63 68 20 0d 0a 20 20 20 20 20 2a 20   which ..     * 
f4e00 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e  it actually mean
f4e10 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a  s what it says *
f4e20 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 63 61 73  /..#endif..  cas
f4e30 65 20 45 49 4f 3a 0d 0a 20 20 63 61 73 65 20 45  e EIO:..  case E
f4e40 42 41 44 46 3a 0d 0a 20 20 63 61 73 65 20 45 49  BADF:..  case EI
f4e50 4e 56 41 4c 3a 0d 0a 20 20 63 61 73 65 20 45 4e  NVAL:..  case EN
f4e60 4f 54 43 4f 4e 4e 3a 0d 0a 20 20 63 61 73 65 20  OTCONN:..  case 
f4e70 45 4e 4f 44 45 56 3a 0d 0a 20 20 63 61 73 65 20  ENODEV:..  case 
f4e80 45 4e 58 49 4f 3a 0d 0a 20 20 63 61 73 65 20 45  ENXIO:..  case E
f4e90 4e 4f 45 4e 54 3a 0d 0a 23 69 66 64 65 66 20 45  NOENT:..#ifdef E
f4ea0 53 54 41 4c 45 20 20 20 20 20 20 20 20 20 20 20  STALE           
f4eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 53 54            /* EST
f4ec0 41 4c 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ALE is not defin
f4ed0 65 64 20 6f 6e 20 49 6e 74 65 72 69 78 20 73 79  ed on Interix sy
f4ee0 73 74 65 6d 73 20 2a 2f 0d 0a 20 20 63 61 73 65  stems */..  case
f4ef0 20 45 53 54 41 4c 45 3a 0d 0a 23 65 6e 64 69 66   ESTALE:..#endif
f4f00 0d 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a  ..  case ENOSYS:
f4f10 0d 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73  ..    /* these s
f4f20 68 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20  hould force the 
f4f30 63 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20  client to close 
f4f40 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63  the file and rec
f4f50 6f 6e 6e 65 63 74 20 2a 2f 0d 0a 20 20 20 20 0d  onnect */..    .
f4f60 0a 20 20 64 65 66 61 75 6c 74 3a 20 0d 0a 20 20  .  default: ..  
f4f70 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 49    return sqliteI
f4f80 4f 45 72 72 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  OErr;..  }..}...
f4f90 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...../**********
f4fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4fe0 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****..**********
f4ff0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55  ******** Begin U
f5000 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74  nique File ID Ut
f5010 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78  ility Used By Vx
f5020 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Works **********
f5030 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e  *****..**..** On
f5040 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f   most versions o
f5050 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67  f unix, we can g
f5060 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 66  et a unique ID f
f5070 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e  or a file by con
f5080 63 61 74 65 6e 61 74 69 6e 67 0d 0a 2a 2a 20 74  catenating..** t
f5090 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72  he device number
f50a0 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e   and the inode n
f50b0 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 73  umber.  But this
f50c0 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f   does not work o
f50d0 6e 20 56 78 57 6f 72 6b 73 2e 0d 0a 2a 2a 20 4f  n VxWorks...** O
f50e0 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69  n VxWorks, a uni
f50f0 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74  que file id must
f5100 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65   be based on the
f5110 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e   canonical filen
f5120 61 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 70  ame...**..** A p
f5130 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
f5140 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
f5150 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
f5160 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
f5170 61 0d 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c  a..** unique fil
f5180 65 20 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e  e ID in VxWorks.
f5190 20 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20    Each instance 
f51a0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
f51b0 65 20 63 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 61  e contains..** a
f51c0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 6e   copy of the can
f51d0 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e  onical filename.
f51e0 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20    There is also 
f51f0 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  a reference coun
f5200 74 2e 20 20 0d 0a 2a 2a 20 54 68 65 20 73 74 72  t.  ..** The str
f5210 75 63 74 75 72 65 20 69 73 20 72 65 63 6c 61 69  ucture is reclai
f5220 6d 65 64 20 77 68 65 6e 20 74 68 65 20 6e 75 6d  med when the num
f5230 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ber of pointers 
f5240 74 6f 20 69 74 20 64 72 6f 70 73 20 74 6f 0d 0a  to it drops to..
f5250 2a 2a 20 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a  ** zero...**..**
f5260 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72   There are never
f5270 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73   very many files
f5280 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d   open at one tim
f5290 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72  e and lookups ar
f52a0 65 20 6e 6f 74 0d 0a 2a 2a 20 61 20 70 65 72 66  e not..** a perf
f52b0 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c  ormance-critical
f52c0 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20   path, so it is 
f52d0 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75  sufficient to pu
f52e0 74 20 74 68 65 73 65 0d 0a 2a 2a 20 73 74 72 75  t these..** stru
f52f0 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b  ctures on a link
f5300 65 64 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74  ed list...*/..st
f5310 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
f5320 49 64 20 7b 0d 0a 20 20 73 74 72 75 63 74 20 76  Id {..  struct v
f5330 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e  xworksFileId *pN
f5340 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e  ext;  /* Next in
f5350 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20   a list of them 
f5360 61 6c 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52  all */..  int nR
f5370 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
f5380 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
f5390 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
f53a0 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0d 0a  to this one */..
f53b0 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20    int nName;    
f53c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f53d0 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
f53e0 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b   zCanonicalName[
f53f0 5d 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 20 20 63  ] string */..  c
f5400 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e  har *zCanonicalN
f5410 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ame;         /* 
f5420 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61  Canonical filena
f5430 6d 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 23 69 66  me */..};....#if
f5440 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 2f 2a 20   OS_VXWORKS../* 
f5450 0d 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71 75 65 20  ..** All unique 
f5460 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20 68 65  filenames are he
f5470 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  ld on a linked l
f5480 69 73 74 20 68 65 61 64 65 64 20 62 79 20 74 68  ist headed by th
f5490 69 73 0d 0a 2a 2a 20 76 61 72 69 61 62 6c 65 3a  is..** variable:
f54a0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 74 72  ..*/..static str
f54b0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
f54c0 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69  d *vxworksFileLi
f54d0 73 74 20 3d 20 30 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  st = 0;..../*..*
f54e0 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c  * Simplify a fil
f54f0 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63  ename into its c
f5500 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0d 0a 2a  anonical form..*
f5510 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20  * by making the 
f5520 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65  following change
f5530 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 2a 20 72 65  s:..**..**  * re
f5540 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c  moving any trail
f5550 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74  ing and duplicat
f5560 65 20 2f 0d 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65  e /..**  * conve
f5570 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74  rt /./ into just
f5580 20 2f 0d 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72   /..**  * conver
f5590 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41  t /A/../ where A
f55a0 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e   is any simple n
f55b0 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0d  ame into just /.
f55c0 0a 2a 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 73 20  .**..** Changes 
f55d0 61 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63  are made in-plac
f55e0 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e  e.  Return the n
f55f0 65 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0d  ew name length..
f5600 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f 72 69 67  .**..** The orig
f5610 69 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73  inal filename is
f5620 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20   in z[0..n-1].  
f5630 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
f5640 72 20 6f 66 0d 0a 2a 2a 20 63 68 61 72 61 63 74  r of..** charact
f5650 65 72 73 20 69 6e 20 74 68 65 20 73 69 6d 70 6c  ers in the simpl
f5660 69 66 69 65 64 20 6e 61 6d 65 2e 0d 0a 2a 2f 0d  ified name...*/.
f5670 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77 6f  .static int vxwo
f5680 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28  rksSimplifyName(
f5690 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
f56a0 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20  ..  int i, j;.. 
f56b0 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a   while( n>1 && z
f56c0 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d  [n-1]=='/' ){ n-
f56d0 2d 3b 20 7d 0d 0a 20 20 66 6f 72 28 69 3d 6a 3d  -; }..  for(i=j=
f56e0 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0d 0a 20  0; i<n; i++){.. 
f56f0 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27     if( z[i]=='/'
f5700 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a   ){..      if( z
f5710 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e  [i+1]=='/' ) con
f5720 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 69 66  tinue;..      if
f5730 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26  ( z[i+1]=='.' &&
f5740 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d   i+2<n && z[i+2]
f5750 3d 3d 27 2f 27 20 29 7b 0d 0a 20 20 20 20 20 20  =='/' ){..      
f5760 20 20 69 20 2b 3d 20 31 3b 0d 0a 20 20 20 20 20    i += 1;..     
f5770 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20     continue;..  
f5780 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
f5790 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20   z[i+1]=='.' && 
f57a0 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d  i+3<n && z[i+2]=
f57b0 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d  ='.' && z[i+3]==
f57c0 27 2f 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  '/' ){..        
f57d0 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b  while( j>0 && z[
f57e0 6a 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d  j-1]!='/' ){ j--
f57f0 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28  ; }..        if(
f5800 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0d 0a   j>0 ){ j--; }..
f5810 20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0d          i += 2;.
f5820 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
f5830 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  e;..      }..   
f5840 20 7d 0d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d   }..    z[j++] =
f5850 20 7a 5b 69 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 7a   z[i];..  }..  z
f5860 5b 6a 5d 20 3d 20 30 3b 0d 0a 20 20 72 65 74 75  [j] = 0;..  retu
f5870 72 6e 20 6a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn j;..}..../*..
f5880 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65  ** Find a unique
f5890 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65   file ID for the
f58a0 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20   given absolute 
f58b0 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72  pathname.  Retur
f58c0 6e 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  n..** a pointer 
f58d0 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69  to the vxworksFi
f58e0 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68  leId object.  Th
f58f0 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68  is pointer is th
f5900 65 20 75 6e 69 71 75 65 0d 0a 2a 2a 20 66 69 6c  e unique..** fil
f5910 65 20 49 44 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  e ID...**..** Th
f5920 65 20 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20  e nRef field of 
f5930 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  the vxworksFileI
f5940 64 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72  d object is incr
f5950 65 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 0d 0a  emented before..
f5960 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73  ** the object is
f5970 20 72 65 74 75 72 6e 65 64 2e 20 20 41 20 6e 65   returned.  A ne
f5980 77 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  w vxworksFileId 
f5990 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65  object is create
f59a0 64 0d 0a 2a 2a 20 61 6e 64 20 61 64 64 65 64 20  d..** and added 
f59b0 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69  to the global li
f59c0 73 74 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  st if necessary.
f59d0 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20 6d 65  ..**..** If a me
f59e0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f59f0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
f5a00 74 75 72 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  turn NULL...*/..
f5a10 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78  static struct vx
f5a20 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77  worksFileId *vxw
f5a30 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63  orksFindFileId(c
f5a40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f  onst char *zAbso
f5a50 6c 75 74 65 4e 61 6d 65 29 7b 0d 0a 20 20 73 74  luteName){..  st
f5a60 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
f5a70 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20  Id *pNew;       
f5a80 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20    /* search key 
f5a90 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20  and new file ID 
f5aa0 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 76 78 77  */..  struct vxw
f5ab0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e  orksFileId *pCan
f5ac0 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72  didate;   /* For
f5ad0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78   looping over ex
f5ae0 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20  isting file IDs 
f5af0 2a 2f 0d 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  */..  int n;    
f5b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f5b10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
f5b20 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65  gth of zAbsolute
f5b30 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0d 0a  Name string */..
f5b40 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 73  ..  assert( zAbs
f5b50 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f  oluteName[0]=='/
f5b60 27 20 29 3b 0d 0a 20 20 6e 20 3d 20 28 69 6e 74  ' );..  n = (int
f5b70 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74  )strlen(zAbsolut
f5b80 65 4e 61 6d 65 29 3b 0d 0a 20 20 70 4e 65 77 20  eName);..  pNew 
f5b90 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
f5ba0 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  ( sizeof(*pNew) 
f5bb0 2b 20 28 6e 2b 31 29 20 29 3b 0d 0a 20 20 69 66  + (n+1) );..  if
f5bc0 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
f5bd0 72 6e 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e 7a  rn 0;..  pNew->z
f5be0 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20  CanonicalName = 
f5bf0 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b  (char*)&pNew[1];
f5c00 0d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d  ..  memcpy(pNew-
f5c10 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c  >zCanonicalName,
f5c20 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20   zAbsoluteName, 
f5c30 6e 2b 31 29 3b 0d 0a 20 20 6e 20 3d 20 76 78 77  n+1);..  n = vxw
f5c40 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65  orksSimplifyName
f5c50 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61  (pNew->zCanonica
f5c60 6c 4e 61 6d 65 2c 20 6e 29 3b 0d 0a 0d 0a 20 20  lName, n);....  
f5c70 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e  /* Search for an
f5c80 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20   existing entry 
f5c90 74 68 61 74 20 6d 61 74 63 68 69 6e 67 20 74 68  that matching th
f5ca0 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e 61 6d 65  e canonical name
f5cb0 2e 0d 0a 20 20 2a 2a 20 49 66 20 66 6f 75 6e 64  ...  ** If found
f5cc0 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  , increment the 
f5cd0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
f5ce0 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
f5cf0 6e 74 65 72 20 74 6f 0d 0a 20 20 2a 2a 20 74 68  nter to..  ** th
f5d00 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20  e existing file 
f5d10 49 44 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75 6e 69  ID...  */..  uni
f5d20 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a  xEnterMutex();..
f5d30 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65    for(pCandidate
f5d40 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74  =vxworksFileList
f5d50 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43  ; pCandidate; pC
f5d60 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64  andidate=pCandid
f5d70 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20  ate->pNext){..  
f5d80 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 65    if( pCandidate
f5d90 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0d 0a 20 20 20  ->nName==n ..   
f5da0 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e    && memcmp(pCan
f5db0 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63  didate->zCanonic
f5dc0 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43  alName, pNew->zC
f5dd0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29  anonicalName, n)
f5de0 3d 3d 30 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20  ==0..    ){..   
f5df0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f5e00 28 70 4e 65 77 29 3b 0d 0a 20 20 20 20 20 20 20  (pNew);..       
f5e10 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66  pCandidate->nRef
f5e20 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 75 6e 69 78  ++;..       unix
f5e30 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 20  LeaveMutex();.. 
f5e40 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 61        return pCa
f5e50 6e 64 69 64 61 74 65 3b 0d 0a 20 20 20 20 7d 0d  ndidate;..    }.
f5e60 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4e 6f 20  .  }....  /* No 
f5e70 6d 61 74 63 68 20 77 61 73 20 66 6f 75 6e 64 2e  match was found.
f5e80 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b 65 20 61    We will make a
f5e90 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f 0d   new file ID */.
f5ea0 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
f5eb0 31 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 6d  1;..  pNew->nNam
f5ec0 65 20 3d 20 6e 3b 0d 0a 20 20 70 4e 65 77 2d 3e  e = n;..  pNew->
f5ed0 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46  pNext = vxworksF
f5ee0 69 6c 65 4c 69 73 74 3b 0d 0a 20 20 76 78 77 6f  ileList;..  vxwo
f5ef0 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e  rksFileList = pN
f5f00 65 77 3b 0d 0a 20 20 75 6e 69 78 4c 65 61 76 65  ew;..  unixLeave
f5f10 4d 75 74 65 78 28 29 3b 0d 0a 20 20 72 65 74 75  Mutex();..  retu
f5f20 72 6e 20 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f  rn pNew;..}..../
f5f30 2a 0d 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  *..** Decrement 
f5f40 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
f5f50 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73  unt on a vxworks
f5f60 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20  FileId object.  
f5f70 46 72 65 65 0d 0a 2a 2a 20 74 68 65 20 6f 62 6a  Free..** the obj
f5f80 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 66  ect when the ref
f5f90 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61  erence count rea
f5fa0 63 68 65 73 20 7a 65 72 6f 2e 0d 0a 2a 2f 0d 0a  ches zero...*/..
f5fb0 73 74 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f  static void vxwo
f5fc0 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64  rksReleaseFileId
f5fd0 28 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46  (struct vxworksF
f5fe0 69 6c 65 49 64 20 2a 70 49 64 29 7b 0d 0a 20 20  ileId *pId){..  
f5ff0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
f6000 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  ;..  assert( pId
f6010 2d 3e 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 70  ->nRef>0 );..  p
f6020 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a 20 20 69  Id->nRef--;..  i
f6030 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d 30 20  f( pId->nRef==0 
f6040 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 76  ){..    struct v
f6050 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a 70  xworksFileId **p
f6060 70 3b 0d 0a 20 20 20 20 66 6f 72 28 70 70 3d 26  p;..    for(pp=&
f6070 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b  vxworksFileList;
f6080 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64   *pp && *pp!=pId
f6090 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e  ; pp = &((*pp)->
f60a0 70 4e 65 78 74 29 29 7b 7d 0d 0a 20 20 20 20 61  pNext)){}..    a
f60b0 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20  ssert( *pp==pId 
f60c0 29 3b 0d 0a 20 20 20 20 2a 70 70 20 3d 20 70 49  );..    *pp = pI
f60d0 64 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 73  d->pNext;..    s
f60e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 29  qlite3_free(pId)
f60f0 3b 0d 0a 20 20 7d 0d 0a 20 20 75 6e 69 78 4c 65  ;..  }..  unixLe
f6100 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 7d 0d 0a  aveMutex();..}..
f6110 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57  #endif /* OS_VXW
f6120 4f 52 4b 53 20 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ORKS */../******
f6130 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f6140 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20   Unique File ID 
f6150 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20  Utility Used By 
f6160 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a  VxWorks ********
f6170 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a  ********..******
f6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f61a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f61b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f61c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 0d 0a 2f  ********/....../
f61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f61e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
f6220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73 69  *********** Posi
f6240 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69  x Advisory Locki
f6250 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
f6260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
f6270 0a 2a 2a 0d 0a 2a 2a 20 50 4f 53 49 58 20 61 64  .**..** POSIX ad
f6280 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65  visory locks are
f6290 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 67   broken by desig
f62a0 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 30  n.  ANSI STD 100
f62b0 33 2e 31 20 28 31 39 39 36 29 0d 0a 2a 2a 20 73  3.1 (1996)..** s
f62c0 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c  ection 6.5.2.2 l
f62d0 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68  ines 483 through
f62e0 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61   490 specify tha
f62f0 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73  t when a process
f6300 0d 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65  ..** sets or cle
f6310 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74  ars a lock, that
f6320 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72   operation overr
f6330 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c  ides any prior l
f6340 6f 63 6b 73 20 73 65 74 0d 0a 2a 2a 20 62 79 20  ocks set..** by 
f6350 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
f6360 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65  .  It does not e
f6370 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f  xplicitly say so
f6380 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69  , but this impli
f6390 65 73 0d 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f  es..** that it o
f63a0 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73  verrides locks s
f63b0 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70  et by the same p
f63c0 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64  rocess using a d
f63d0 69 66 66 65 72 65 6e 74 0d 0a 2a 2a 20 66 69 6c  ifferent..** fil
f63e0 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43  e descriptor.  C
f63f0 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73  onsider this tes
f6400 74 20 63 61 73 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  t case:..**..** 
f6410 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20        int fd1 = 
f6420 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20  open("./file1", 
f6430 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20  O_RDWR|O_CREAT, 
f6440 30 36 34 34 29 3b 0d 0a 2a 2a 20 20 20 20 20 20  0644);..**      
f6450 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28   int fd2 = open(
f6460 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57  "./file2", O_RDW
f6470 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29  R|O_CREAT, 0644)
f6480 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 53 75 70 70 6f 73  ;..**..** Suppos
f6490 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f  e ./file1 and ./
f64a0 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79  file2 are really
f64b0 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28   the same file (
f64c0 62 65 63 61 75 73 65 0d 0a 2a 2a 20 6f 6e 65 20  because..** one 
f64d0 69 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d  is a hard or sym
f64e0 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68  bolic link to th
f64f0 65 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66  e other) then if
f6500 20 79 6f 75 20 73 65 74 0d 0a 2a 2a 20 61 6e 20   you set..** an 
f6510 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f  exclusive lock o
f6520 6e 20 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20  n fd1, then try 
f6530 74 6f 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73  to get an exclus
f6540 69 76 65 20 6c 6f 63 6b 0d 0a 2a 2a 20 6f 6e 20  ive lock..** on 
f6550 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20  fd2, it works.  
f6560 49 20 77 6f 75 6c 64 20 68 61 76 65 20 65 78 70  I would have exp
f6570 65 63 74 65 64 20 74 68 65 20 73 65 63 6f 6e 64  ected the second
f6580 20 6c 6f 63 6b 20 74 6f 0d 0a 2a 2a 20 66 61 69   lock to..** fai
f6590 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 61  l since there wa
f65a0 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b  s already a lock
f65b0 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 75 65   on the file due
f65c0 20 74 6f 20 66 64 31 2e 0d 0a 2a 2a 20 42 75 74   to fd1...** But
f65d0 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 20   not so.  Since 
f65e0 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 20  both locks came 
f65f0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 72  from the same pr
f6600 6f 63 65 73 73 2c 20 74 68 65 0d 0a 2a 2a 20 73  ocess, the..** s
f6610 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20  econd overrides 
f6620 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20  the first, even 
f6630 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 65  though they were
f6640 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0d 0a 2a   on different..*
f6650 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * file descripto
f6660 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66  rs opened on dif
f6670 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65  ferent file name
f6680 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  s...**..** This 
f6690 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 61  means that we ca
f66a0 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c  nnot use POSIX l
f66b0 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e  ocks to synchron
f66c0 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 0d  ize file access.
f66d0 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74  .** among compet
f66e0 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74  ing threads of t
f66f0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e  he same process.
f6700 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69    POSIX locks wi
f6710 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0d 0a 2a 2a  ll work fine..**
f6720 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20   to synchronize 
f6730 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61  access for threa
f6740 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70  ds in separate p
f6750 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f  rocesses, but no
f6760 74 0d 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69  t..** threads wi
f6770 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72  thin the same pr
f6780 6f 63 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ocess...**..** T
f6790 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
f67a0 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74  e problem, SQLit
f67b0 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20  e has to manage 
f67c0 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72  file locks inter
f67d0 6e 61 6c 6c 79 0d 0a 2a 2a 20 6f 6e 20 69 74 73  nally..** on its
f67e0 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20   own.  Whenever 
f67f0 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 69  a new database i
f6800 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76  s opened, we hav
f6810 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0d 0a 2a  e to find the..*
f6820 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65  * specific inode
f6830 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
f6840 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65   file (the inode
f6850 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
f6860 79 20 74 68 65 0d 0a 2a 2a 20 73 74 5f 64 65 76  y the..** st_dev
f6870 20 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c   and st_ino fiel
f6880 64 73 20 6f 66 20 74 68 65 20 73 74 61 74 20 73  ds of the stat s
f6890 74 72 75 63 74 75 72 65 20 74 68 61 74 20 66 73  tructure that fs
f68a0 74 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0d  tat() fills in).
f68b0 0a 2a 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f  .** and check fo
f68c0 72 20 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20  r locks already 
f68d0 65 78 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74  existing on that
f68e0 20 69 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f   inode.  When lo
f68f0 63 6b 73 20 61 72 65 0d 0a 2a 2a 20 63 72 65 61  cks are..** crea
f6900 74 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20  ted or removed, 
f6910 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20  we have to look 
f6920 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72  at our own inter
f6930 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68  nal record of th
f6940 65 0d 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 73  e..** locks to s
f6950 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20 74 68  ee if another th
f6960 72 65 61 64 20 68 61 73 20 70 72 65 76 69 6f 75  read has previou
f6970 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b 20 6f  sly set a lock o
f6980 6e 20 74 68 61 74 20 73 61 6d 65 0d 0a 2a 2a 20  n that same..** 
f6990 69 6e 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 28  inode...**..** (
f69a0 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f  Aside: The use o
f69b0 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20  f inode numbers 
f69c0 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f  as unique IDs do
f69d0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56  es not work on V
f69e0 78 57 6f 72 6b 73 2e 0d 0a 2a 2a 20 46 6f 72 20  xWorks...** For 
f69f0 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65  VxWorks, we have
f6a00 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65   to use the alte
f6a10 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49  rnative unique I
f6a20 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f  D system based o
f6a30 6e 0d 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20  n..** canonical 
f6a40 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70  filename and imp
f6a50 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20  lemented in the 
f6a60 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f  previous divisio
f6a70 6e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  n.)..**..** The 
f6a80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72  sqlite3_file str
f6a90 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58  ucture for POSIX
f6aa0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75   is no longer ju
f6ab0 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69  st an integer fi
f6ac0 6c 65 0d 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f  le..** descripto
f6ad0 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20  r.  It is now a 
f6ae0 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68  structure that h
f6af0 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72  olds the integer
f6b00 20 66 69 6c 65 0d 0a 2a 2a 20 64 65 73 63 72 69   file..** descri
f6b10 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74  ptor and a point
f6b20 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72  er to a structur
f6b30 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
f6b40 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0d 0a 2a   the internal..*
f6b50 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63  * locks on the c
f6b60 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f  orresponding ino
f6b70 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  de.  There is on
f6b80 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74  e locking struct
f6b90 75 72 65 0d 0a 2a 2a 20 70 65 72 20 69 6e 6f 64  ure..** per inod
f6ba0 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d  e, so if the sam
f6bb0 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65  e inode is opene
f6bc0 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e  d twice, both un
f6bd0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  ixFile structure
f6be0 73 0d 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74  s..** point to t
f6bf0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
f6c00 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
f6c10 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72  locking structur
f6c20 65 20 6b 65 65 70 73 0d 0a 2a 2a 20 61 20 72 65  e keeps..** a re
f6c30 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73  ference count (s
f6c40 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  o we will know w
f6c50 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74  hen to delete it
f6c60 29 20 61 6e 64 20 61 20 22 63 6e 74 22 0d 0a 2a  ) and a "cnt"..*
f6c70 2a 20 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c  * field that tel
f6c80 6c 73 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e  ls us its intern
f6c90 61 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20  al lock status. 
f6ca0 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68   cnt==0 means th
f6cb0 65 0d 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e  e..** file is un
f6cc0 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31  locked.  cnt==-1
f6cd0 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20   means the file 
f6ce0 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65  has an exclusive
f6cf0 20 6c 6f 63 6b 2e 0d 0a 2a 2a 20 63 6e 74 3e 30   lock...** cnt>0
f6d00 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65   means there are
f6d10 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f 63 6b   cnt shared lock
f6d20 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0d 0a  s on the file...
f6d30 2a 2a 0d 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d  **..** Any attem
f6d40 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e  pt to lock or un
f6d50 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73  lock a file firs
f6d60 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63  t checks the loc
f6d70 6b 69 6e 67 0d 0a 2a 2a 20 73 74 72 75 63 74 75  king..** structu
f6d80 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29  re.  The fcntl()
f6d90 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20   system call is 
f6da0 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20  only invoked to 
f6db0 73 65 74 20 61 20 0d 0a 2a 2a 20 50 4f 53 49 58  set a ..** POSIX
f6dc0 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74   lock if the int
f6dd0 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63  ernal lock struc
f6de0 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73  ture transitions
f6df0 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 61 20 6c   between..** a l
f6e00 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c  ocked and an unl
f6e10 6f 63 6b 65 64 20 73 74 61 74 65 2e 0d 0a 2a 2a  ocked state...**
f6e20 0d 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20  ..** But wait:  
f6e30 74 68 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f  there are yet mo
f6e40 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68  re problems with
f6e50 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20   POSIX advisory 
f6e60 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  locks...**..** I
f6e70 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69  f you close a fi
f6e80 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68  le descriptor th
f6e90 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
f6ea0 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63  ile that has loc
f6eb0 6b 73 2c 0d 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b  ks,..** all lock
f6ec0 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74  s on that file t
f6ed0 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79  hat are owned by
f6ee0 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f   the current pro
f6ef0 63 65 73 73 20 61 72 65 0d 0a 2a 2a 20 72 65 6c  cess are..** rel
f6f00 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20  eased.  To work 
f6f10 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62  around this prob
f6f20 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 49 6e  lem, each unixIn
f6f30 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 0d 0a  odeInfo object..
f6f40 2a 2a 20 6d 61 69 6e 74 61 69 6e 73 20 61 20 63  ** maintains a c
f6f50 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  ount of the numb
f6f60 65 72 20 6f 66 20 70 65 6e 64 69 6e 67 20 6c 6f  er of pending lo
f6f70 63 6b 73 20 6f 6e 20 74 68 61 20 69 6e 6f 64 65  cks on tha inode
f6f80 2e 0d 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 61 74  ...** When an at
f6f90 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
f6fa0 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78 46 69   close an unixFi
f6fb0 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61 72 65  le, if there are
f6fc0 0d 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78 46  ..** other unixF
f6fd0 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  ile open on the 
f6fe0 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74 20  same inode that 
f6ff0 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b  are holding lock
f7000 73 2c 20 74 68 65 20 63 61 6c 6c 0d 0a 2a 2a 20  s, the call..** 
f7010 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66  to close() the f
f7020 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
f7030 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
f7040 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b   all of the lock
f7050 73 20 63 6c 65 61 72 2e 0d 0a 2a 2a 20 54 68 65  s clear...** The
f7060 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 73   unixInodeInfo s
f7070 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 61  tructure keeps a
f7080 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 64 65   list of file de
f7090 73 63 72 69 70 74 6f 72 73 20 74 68 61 74 20 6e  scriptors that n
f70a0 65 65 64 20 74 6f 0d 0a 2a 2a 20 62 65 20 63 6c  eed to..** be cl
f70b0 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 6c 69  osed and that li
f70c0 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 61 6e  st is walked (an
f70d0 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 6e 20  d cleared) when 
f70e0 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0d 0a 2a  the last lock..*
f70f0 2a 20 63 6c 65 61 72 73 2e 0d 0a 2a 2a 0d 0a 2a  * clears...**..*
f7100 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72  * Yet another pr
f7110 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72  oblem:  LinuxThr
f7120 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79  eads do not play
f7130 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78   well with posix
f7140 20 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   locks...**..** 
f7150 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69  Many older versi
f7160 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65  ons of linux use
f7170 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64   the LinuxThread
f7180 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20  s library which 
f7190 69 73 0d 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78  is..** not posix
f71a0 20 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64   compliant.  Und
f71b0 65 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c  er LinuxThreads,
f71c0 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20   a lock created 
f71d0 62 79 20 74 68 72 65 61 64 0d 0a 2a 2a 20 41 20  by thread..** A 
f71e0 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69  cannot be modifi
f71f0 65 64 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e  ed or overridden
f7200 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20   by a different 
f7210 74 68 72 65 61 64 20 42 2e 0d 0a 2a 2a 20 4f 6e  thread B...** On
f7220 6c 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20  ly thread A can 
f7230 6d 6f 64 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e  modify the lock.
f7240 20 20 4c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69    Locking behavi
f7250 6f 72 20 69 73 20 63 6f 72 72 65 63 74 0d 0a 2a  or is correct..*
f7260 2a 20 69 66 20 74 68 65 20 61 70 70 6c 69 61 74  * if the appliat
f7270 69 6f 6e 20 75 73 65 73 20 74 68 65 20 6e 65 77  ion uses the new
f7280 65 72 20 4e 61 74 69 76 65 20 50 6f 73 69 78 20  er Native Posix 
f7290 54 68 72 65 61 64 20 4c 69 62 72 61 72 79 20 28  Thread Library (
f72a0 4e 50 54 4c 29 0d 0a 2a 2a 20 6f 6e 20 6c 69 6e  NPTL)..** on lin
f72b0 75 78 20 2d 20 77 69 74 68 20 4e 50 54 4c 20 61  ux - with NPTL a
f72c0 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79   lock created by
f72d0 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6f 76   thread A can ov
f72e0 65 72 72 69 64 65 20 6c 6f 63 6b 73 0d 0a 2a 2a  erride locks..**
f72f0 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 20 42   in thread B.  B
f7300 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  ut there is no w
f7310 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 63 6f  ay to know at co
f7320 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 63 68  mpile-time which
f7330 0d 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 6c  ..** threading l
f7340 69 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 20  ibrary is being 
f7350 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 20  used.  So there 
f7360 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f  is no way to kno
f7370 77 20 61 74 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65  w at..** compile
f7380 2d 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72  -time whether or
f7390 20 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 61   not thread A ca
f73a0 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73  n override locks
f73b0 20 6f 6e 20 74 68 72 65 61 64 20 42 2e 0d 0a 2a   on thread B...*
f73c0 2a 20 4f 6e 65 20 68 61 73 20 74 6f 20 64 6f 20  * One has to do 
f73d0 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b  a run-time check
f73e0 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65   to discover the
f73f0 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65   behavior of the
f7400 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 6f  ..** current pro
f7410 63 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51  cess...**..** SQ
f7420 4c 69 74 65 20 75 73 65 64 20 74 6f 20 73 75 70  Lite used to sup
f7430 70 6f 72 74 20 4c 69 6e 75 78 54 68 72 65 61 64  port LinuxThread
f7440 73 2e 20 20 42 75 74 20 73 75 70 70 6f 72 74 20  s.  But support 
f7450 66 6f 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73  for LinuxThreads
f7460 0d 0a 2a 2a 20 77 61 73 20 64 72 6f 70 70 65 64  ..** was dropped
f7470 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
f7480 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 2e 20 20  version 3.7.0.  
f7490 53 51 4c 69 74 65 20 77 69 6c 6c 20 73 74 69 6c  SQLite will stil
f74a0 6c 20 77 6f 72 6b 20 77 69 74 68 0d 0a 2a 2a 20  l work with..** 
f74b0 4c 69 6e 75 78 54 68 72 65 61 64 73 20 70 72 6f  LinuxThreads pro
f74c0 76 69 64 65 64 20 74 68 61 74 20 28 31 29 20 74  vided that (1) t
f74d0 68 65 72 65 20 69 73 20 6e 6f 20 6d 6f 72 65 20  here is no more 
f74e0 74 68 61 6e 20 6f 6e 65 20 63 6f 6e 6e 65 63 74  than one connect
f74f0 69 6f 6e 20 0d 0a 2a 2a 20 70 65 72 20 64 61 74  ion ..** per dat
f7500 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 74 68  abase file in th
f7510 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 61  e same process a
f7520 6e 64 20 28 32 29 20 64 61 74 61 62 61 73 65 20  nd (2) database 
f7530 63 6f 6e 6e 65 63 74 69 6f 6e 73 0d 0a 2a 2a 20  connections..** 
f7540 64 6f 20 6e 6f 74 20 6d 6f 76 65 20 61 63 72 6f  do not move acro
f7550 73 73 20 74 68 72 65 61 64 73 2e 0d 0a 2a 2f 0d  ss threads...*/.
f7560 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73  .../*..** An ins
f7570 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
f7580 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
f7590 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b   serves as the k
f75a0 65 79 20 75 73 65 64 0d 0a 2a 2a 20 74 6f 20 6c  ey used..** to l
f75b0 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c  ocate a particul
f75c0 61 72 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f  ar unixInodeInfo
f75d0 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74   object...*/..st
f75e0 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20  ruct unixFileId 
f75f0 7b 0d 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20  {..  dev_t dev; 
f7600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7610 20 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d 62 65   /* Device numbe
f7620 72 20 2a 2f 0d 0a 23 69 66 20 4f 53 5f 56 58 57  r */..#if OS_VXW
f7630 4f 52 4b 53 0d 0a 20 20 73 74 72 75 63 74 20 76  ORKS..  struct v
f7640 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
f7650 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69  d;  /* Unique fi
f7660 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f 72 6b  le ID for vxwork
f7670 73 2e 20 2a 2f 0d 0a 23 65 6c 73 65 0d 0a 20 20  s. */..#else..  
f7680 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20  ino_t ino;      
f7690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
f76a0 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a  node number */..
f76b0 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  #endif..};..../*
f76c0 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  ..** An instance
f76d0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
f76e0 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
f76f0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
f7700 68 20 6f 70 65 6e 0d 0a 2a 2a 20 69 6e 6f 64 65  h open..** inode
f7710 2e 20 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54  .  Or, on LinuxT
f7720 68 72 65 61 64 73 2c 20 74 68 65 72 65 20 69 73  hreads, there is
f7730 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74   one of these st
f7740 72 75 63 74 75 72 65 73 20 66 6f 72 0d 0a 2a 2a  ructures for..**
f7750 20 65 61 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e   each inode open
f7760 65 64 20 62 79 20 65 61 63 68 20 74 68 72 65 61  ed by each threa
f7770 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 73 69 6e  d...**..** A sin
f7780 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20 68 61  gle inode can ha
f7790 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69 6c 65  ve multiple file
f77a0 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 73 6f   descriptors, so
f77b0 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 0d 0a   each unixFile..
f77c0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ** structure con
f77d0 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
f77e0 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
f77f0 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 6e  f this object an
f7800 64 20 74 68 69 73 0d 0a 2a 2a 20 6f 62 6a 65 63  d this..** objec
f7810 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20  t keeps a count 
f7820 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
f7830 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69   unixFile pointi
f7840 6e 67 20 74 6f 20 69 74 2e 0d 0a 2a 2f 0d 0a 73  ng to it...*/..s
f7850 74 72 75 63 74 20 75 6e 69 78 49 6e 6f 64 65 49  truct unixInodeI
f7860 6e 66 6f 20 7b 0d 0a 20 20 73 74 72 75 63 74 20  nfo {..  struct 
f7870 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49  unixFileId fileI
f7880 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
f7890 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0d 0a 20  lookup key */.. 
f78a0 20 69 6e 74 20 6e 53 68 61 72 65 64 3b 20 20 20   int nShared;   
f78b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f78c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 48   /* Number of SH
f78d0 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20  ARED locks held 
f78e0 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  */..  unsigned c
f78f0 68 61 72 20 65 46 69 6c 65 4c 6f 63 6b 3b 20 20  har eFileLock;  
f7900 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
f7910 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53  SHARED_LOCK, RES
f7920 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20  ERVED_LOCK etc. 
f7930 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  */..  unsigned c
f7940 68 61 72 20 62 50 72 6f 63 65 73 73 4c 6f 63 6b  har bProcessLock
f7950 3b 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 63 6c  ;     /* An excl
f7960 75 73 69 76 65 20 70 72 6f 63 65 73 73 20 6c 6f  usive process lo
f7970 63 6b 20 69 73 20 68 65 6c 64 20 2a 2f 0d 0a 20  ck is held */.. 
f7980 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
f7990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f79a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f   /* Number of po
f79b0 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
f79c0 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20 75  tructure */..  u
f79d0 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d  nixShmNode *pShm
f79e0 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Node;          /
f79f0 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
f7a00 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
f7a10 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0d 0a 20  this inode */.. 
f7a20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20   int nLock;     
f7a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7a40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75   /* Number of ou
f7a50 74 73 74 61 6e 64 69 6e 67 20 66 69 6c 65 20 6c  tstanding file l
f7a60 6f 63 6b 73 20 2a 2f 0d 0a 20 20 55 6e 69 78 55  ocks */..  UnixU
f7a70 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64  nusedFd *pUnused
f7a80 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e  ;          /* Un
f7a90 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69  used file descri
f7aa0 70 74 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a  ptors to close *
f7ab0 2f 0d 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e  /..  unixInodeIn
f7ac0 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  fo *pNext;      
f7ad0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
f7ae0 61 6c 6c 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66  all unixInodeInf
f7af0 6f 20 6f 62 6a 65 63 74 73 20 2a 2f 0d 0a 20 20  o objects */..  
f7b00 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70  unixInodeInfo *p
f7b10 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  Prev;           
f7b20 2f 2a 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c  /*    .... doubl
f7b30 79 20 6c 69 6e 6b 65 64 20 2a 2f 0d 0a 23 69 66  y linked */..#if
f7b40 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
f7b50 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20  OCKING_STYLE..  
f7b60 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
f7b70 6e 67 20 73 68 61 72 65 64 42 79 74 65 3b 20 20  ng sharedByte;  
f7b80 2f 2a 20 66 6f 72 20 41 46 50 20 73 69 6d 75 6c  /* for AFP simul
f7b90 61 74 65 64 20 73 68 61 72 65 64 20 6c 6f 63 6b  ated shared lock
f7ba0 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66   */..#endif..#if
f7bb0 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 73   OS_VXWORKS..  s
f7bc0 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20  em_t *pSem;     
f7bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f7be0 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65  * Named POSIX se
f7bf0 6d 61 70 68 6f 72 65 20 2a 2f 0d 0a 20 20 63 68  maphore */..  ch
f7c00 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f  ar aSemName[MAX_
f7c10 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 2f 2a  PATHNAME+2];  /*
f7c20 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65   Name of that se
f7c30 6d 61 70 68 6f 72 65 20 2a 2f 0d 0a 23 65 6e 64  maphore */..#end
f7c40 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  if..};..../*..**
f7c50 20 41 20 6c 69 73 74 73 20 6f 66 20 61 6c 6c 20   A lists of all 
f7c60 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62  unixInodeInfo ob
f7c70 6a 65 63 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  jects...*/..stat
f7c80 69 63 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f  ic unixInodeInfo
f7c90 20 2a 69 6e 6f 64 65 4c 69 73 74 20 3d 20 30 3b   *inodeList = 0;
f7ca0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ..../*..**..** T
f7cb0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2d 20 75  his function - u
f7cc0 6e 69 78 4c 6f 67 45 72 72 6f 72 5f 78 28 29 2c  nixLogError_x(),
f7cd0 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 63 61   is only ever ca
f7ce0 6c 6c 65 64 20 76 69 61 20 74 68 65 20 6d 61 63  lled via the mac
f7cf0 72 6f 0d 0a 2a 2a 20 75 6e 69 78 4c 6f 67 45 72  ro..** unixLogEr
f7d00 72 6f 72 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  ror()...**..** I
f7d10 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 66 74  t is invoked aft
f7d20 65 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  er an error occu
f7d30 72 73 20 69 6e 20 61 6e 20 4f 53 20 66 75 6e 63  rs in an OS func
f7d40 74 69 6f 6e 20 61 6e 64 20 65 72 72 6e 6f 20 68  tion and errno h
f7d50 61 73 20 62 65 65 6e 0d 0a 2a 2a 20 73 65 74 2e  as been..** set.
f7d60 20 49 74 20 6c 6f 67 73 20 61 20 6d 65 73 73 61   It logs a messa
f7d70 67 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ge using sqlite3
f7d80 5f 6c 6f 67 28 29 20 63 6f 6e 74 61 69 6e 69 6e  _log() containin
f7d90 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  g the current va
f7da0 6c 75 65 20 6f 66 0d 0a 2a 2a 20 65 72 72 6e 6f  lue of..** errno
f7db0 20 61 6e 64 2c 20 69 66 20 70 6f 73 73 69 62 6c   and, if possibl
f7dc0 65 2c 20 74 68 65 20 68 75 6d 61 6e 2d 72 65 61  e, the human-rea
f7dd0 64 61 62 6c 65 20 65 71 75 69 76 61 6c 65 6e 74  dable equivalent
f7de0 20 66 72 6f 6d 20 73 74 72 65 72 72 6f 72 28 29   from strerror()
f7df0 20 6f 72 0d 0a 2a 2a 20 73 74 72 65 72 72 6f 72   or..** strerror
f7e00 5f 72 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  _r()...**..** Th
f7e10 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
f7e20 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 6d   passed to the m
f7e30 61 63 72 6f 20 73 68 6f 75 6c 64 20 62 65 20 74  acro should be t
f7e40 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 68  he error code th
f7e50 61 74 0d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72  at..** will be r
f7e60 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c 69 74  eturned to SQLit
f7e70 65 20 28 65 2e 67 2e 20 53 51 4c 49 54 45 5f 49  e (e.g. SQLITE_I
f7e80 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 53 51 4c  OERR_DELETE, SQL
f7e90 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 2e 20 0d  ITE_CANTOPEN). .
f7ea0 0a 2a 2a 20 54 68 65 20 74 77 6f 20 73 75 62 73  .** The two subs
f7eb0 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73  equent arguments
f7ec0 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6e   should be the n
f7ed0 61 6d 65 20 6f 66 20 74 68 65 20 4f 53 20 66 75  ame of the OS fu
f7ee0 6e 63 74 69 6f 6e 20 74 68 61 74 0d 0a 2a 2a 20  nction that..** 
f7ef0 66 61 69 6c 65 64 20 28 65 2e 67 2e 20 22 75 6e  failed (e.g. "un
f7f00 6c 69 6e 6b 22 2c 20 22 6f 70 65 6e 22 29 20 61  link", "open") a
f7f10 6e 64 20 74 68 65 20 74 68 65 20 61 73 73 6f 63  nd the the assoc
f7f20 69 61 74 65 64 20 66 69 6c 65 2d 73 79 73 74 65  iated file-syste
f7f30 6d 20 70 61 74 68 2c 0d 0a 2a 2a 20 69 66 20 61  m path,..** if a
f7f40 6e 79 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  ny...*/..#define
f7f50 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 61 2c   unixLogError(a,
f7f60 62 2c 63 29 20 20 20 20 20 75 6e 69 78 4c 6f 67  b,c)     unixLog
f7f70 45 72 72 6f 72 41 74 4c 69 6e 65 28 61 2c 62 2c  ErrorAtLine(a,b,
f7f80 63 2c 5f 5f 4c 49 4e 45 5f 5f 29 0d 0a 73 74 61  c,__LINE__)..sta
f7f90 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 67 45  tic int unixLogE
f7fa0 72 72 6f 72 41 74 4c 69 6e 65 28 0d 0a 20 20 69  rrorAtLine(..  i
f7fb0 6e 74 20 65 72 72 63 6f 64 65 2c 20 20 20 20 20  nt errcode,     
f7fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f7fd0 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  * SQLite error c
f7fe0 6f 64 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ode */..  const 
f7ff0 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 20  char *zFunc,    
f8000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
f8010 65 20 6f 66 20 4f 53 20 66 75 6e 63 74 69 6f 6e  e of OS function
f8020 20 74 68 61 74 20 66 61 69 6c 65 64 20 2a 2f 0d   that failed */.
f8030 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
f8040 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20  Path,           
f8050 20 20 20 2f 2a 20 46 69 6c 65 20 70 61 74 68 20     /* File path 
f8060 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
f8070 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  error */..  int 
f8080 69 4c 69 6e 65 20 20 20 20 20 20 20 20 20 20 20  iLine           
f8090 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
f80a0 6f 75 72 63 65 20 6c 69 6e 65 20 6e 75 6d 62 65  ource line numbe
f80b0 72 20 77 68 65 72 65 20 65 72 72 6f 72 20 6f 63  r where error oc
f80c0 63 75 72 72 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20  curred */..){.. 
f80d0 20 63 68 61 72 20 2a 7a 45 72 72 3b 20 20 20 20   char *zErr;    
f80e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f80f0 20 2f 2a 20 4d 65 73 73 61 67 65 20 66 72 6f 6d   /* Message from
f8100 20 73 74 72 65 72 72 6f 72 28 29 20 6f 72 20 65   strerror() or e
f8110 71 75 69 76 61 6c 65 6e 74 20 2a 2f 0d 0a 20 20  quivalent */..  
f8120 69 6e 74 20 69 45 72 72 6e 6f 20 3d 20 65 72 72  int iErrno = err
f8130 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
f8140 2f 2a 20 53 61 76 65 64 20 73 79 73 63 61 6c 6c  /* Saved syscall
f8150 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 20 2a 2f   error number */
f8160 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  ....  /* If this
f8170 20 69 73 20 6e 6f 74 20 61 20 74 68 72 65 61 64   is not a thread
f8180 73 61 66 65 20 62 75 69 6c 64 20 28 53 51 4c 49  safe build (SQLI
f8190 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
f81a0 29 2c 20 74 68 65 6e 20 75 73 65 0d 0a 20 20 2a  ), then use..  *
f81b0 2a 20 74 68 65 20 73 74 72 65 72 72 6f 72 28 29  * the strerror()
f81c0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 6f 62 74   function to obt
f81d0 61 69 6e 20 74 68 65 20 68 75 6d 61 6e 2d 72 65  ain the human-re
f81e0 61 64 61 62 6c 65 20 65 72 72 6f 72 20 6d 65 73  adable error mes
f81f0 73 61 67 65 0d 0a 20 20 2a 2a 20 65 71 75 69 76  sage..  ** equiv
f8200 61 6c 65 6e 74 20 74 6f 20 65 72 72 6e 6f 2e 20  alent to errno. 
f8210 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65 20 73  Otherwise, use s
f8220 74 72 65 72 72 6f 72 5f 72 28 29 2e 0d 0a 20 20  trerror_r()...  
f8230 2a 2f 20 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  */ ..#if SQLITE_
f8240 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
f8250 66 69 6e 65 64 28 48 41 56 45 5f 53 54 52 45 52  fined(HAVE_STRER
f8260 52 4f 52 5f 52 29 0d 0a 20 20 63 68 61 72 20 61  ROR_R)..  char a
f8270 45 72 72 5b 38 30 5d 3b 0d 0a 20 20 6d 65 6d 73  Err[80];..  mems
f8280 65 74 28 61 45 72 72 2c 20 30 2c 20 73 69 7a 65  et(aErr, 0, size
f8290 6f 66 28 61 45 72 72 29 29 3b 0d 0a 20 20 7a 45  of(aErr));..  zE
f82a0 72 72 20 3d 20 61 45 72 72 3b 0d 0a 0d 0a 20 20  rr = aErr;....  
f82b0 2f 2a 20 49 66 20 53 54 52 45 52 52 4f 52 5f 52  /* If STRERROR_R
f82c0 5f 43 48 41 52 5f 50 20 28 73 65 74 20 62 79 20  _CHAR_P (set by 
f82d0 61 75 74 6f 63 6f 6e 66 20 73 63 72 69 70 74 73  autoconf scripts
f82e0 29 20 6f 72 20 5f 5f 55 53 45 5f 47 4e 55 20 69  ) or __USE_GNU i
f82f0 73 20 64 65 66 69 6e 65 64 2c 0d 0a 20 20 2a 2a  s defined,..  **
f8300 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
f8310 20 73 79 73 74 65 6d 20 70 72 6f 76 69 64 65 73   system provides
f8320 20 74 68 65 20 74 68 65 20 47 4e 55 20 76 65 72   the the GNU ver
f8330 73 69 6f 6e 20 6f 66 20 73 74 72 65 72 72 6f 72  sion of strerror
f8340 5f 72 28 29 20 74 68 61 74 20 0d 0a 20 20 2a 2a  _r() that ..  **
f8350 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
f8360 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63  er to a buffer c
f8370 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 72  ontaining the er
f8380 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 54 68 61  ror message. Tha
f8390 74 20 70 6f 69 6e 74 65 72 20 0d 0a 20 20 2a 2a  t pointer ..  **
f83a0 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 45   may point to aE
f83b0 72 72 5b 5d 2c 20 6f 72 20 69 74 20 6d 61 79 20  rr[], or it may 
f83c0 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 73 74  point to some st
f83d0 61 74 69 63 20 73 74 6f 72 61 67 65 20 73 6f 6d  atic storage som
f83e0 65 77 68 65 72 65 2e 20 0d 0a 20 20 2a 2a 20 4f  ewhere. ..  ** O
f83f0 74 68 65 72 77 69 73 65 2c 20 61 73 73 75 6d 65  therwise, assume
f8400 20 74 68 61 74 20 74 68 65 20 73 79 73 74 65 6d   that the system
f8410 20 70 72 6f 76 69 64 65 73 20 74 68 65 20 50 4f   provides the PO
f8420 53 49 58 20 76 65 72 73 69 6f 6e 20 6f 66 20 0d  SIX version of .
f8430 0a 20 20 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72  .  ** strerror_r
f8440 28 29 2c 20 77 68 69 63 68 20 61 6c 77 61 79 73  (), which always
f8450 20 77 72 69 74 65 73 20 61 6e 20 65 72 72 6f 72   writes an error
f8460 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 61 45   message into aE
f8470 72 72 5b 5d 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  rr[]...  **..  *
f8480 2a 20 49 66 20 74 68 65 20 63 6f 64 65 20 69 6e  * If the code in
f8490 63 6f 72 72 65 63 74 6c 79 20 61 73 73 75 6d 65  correctly assume
f84a0 73 20 74 68 61 74 20 69 74 20 69 73 20 74 68 65  s that it is the
f84b0 20 50 4f 53 49 58 20 76 65 72 73 69 6f 6e 20 74   POSIX version t
f84c0 68 61 74 20 69 73 0d 0a 20 20 2a 2a 20 61 76 61  hat is..  ** ava
f84d0 69 6c 61 62 6c 65 2c 20 74 68 65 20 65 72 72 6f  ilable, the erro
f84e0 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 6f  r message will o
f84f0 66 74 65 6e 20 62 65 20 61 6e 20 65 6d 70 74 79  ften be an empty
f8500 20 73 74 72 69 6e 67 2e 20 4e 6f 74 20 61 0d 0a   string. Not a..
f8510 20 20 2a 2a 20 68 75 67 65 20 70 72 6f 62 6c 65    ** huge proble
f8520 6d 2e 20 49 6e 63 6f 72 72 65 63 74 6c 79 20 63  m. Incorrectly c
f8530 6f 6e 63 6c 75 64 69 6e 67 20 74 68 61 74 20 74  oncluding that t
f8540 68 65 20 47 4e 55 20 76 65 72 73 69 6f 6e 20 69  he GNU version i
f8550 73 20 61 76 61 69 6c 61 62 6c 65 20 0d 0a 20 20  s available ..  
f8560 2a 2a 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  ** could lead to
f8570 20 61 20 73 65 67 66 61 75 6c 74 20 74 68 6f 75   a segfault thou
f8580 67 68 2e 0d 0a 20 20 2a 2f 0d 0a 23 69 66 20 64  gh...  */..#if d
f8590 65 66 69 6e 65 64 28 53 54 52 45 52 52 4f 52 5f  efined(STRERROR_
f85a0 52 5f 43 48 41 52 5f 50 29 20 7c 7c 20 64 65 66  R_CHAR_P) || def
f85b0 69 6e 65 64 28 5f 5f 55 53 45 5f 47 4e 55 29 0d  ined(__USE_GNU).
f85c0 0a 20 20 7a 45 72 72 20 3d 20 0d 0a 23 20 65 6e  .  zErr = ..# en
f85d0 64 69 66 0d 0a 20 20 73 74 72 65 72 72 6f 72 5f  dif..  strerror_
f85e0 72 28 69 45 72 72 6e 6f 2c 20 61 45 72 72 2c 20  r(iErrno, aErr, 
f85f0 73 69 7a 65 6f 66 28 61 45 72 72 29 2d 31 29 3b  sizeof(aErr)-1);
f8600 0d 0a 0d 0a 23 65 6c 69 66 20 53 51 4c 49 54 45  ....#elif SQLITE
f8610 5f 54 48 52 45 41 44 53 41 46 45 0d 0a 20 20 2f  _THREADSAFE..  /
f8620 2a 20 54 68 69 73 20 69 73 20 61 20 74 68 72 65  * This is a thre
f8630 61 64 73 61 66 65 20 62 75 69 6c 64 2c 20 62 75  adsafe build, bu
f8640 74 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20 69  t strerror_r() i
f8650 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e  s not available.
f8660 20 2a 2f 0d 0a 20 20 7a 45 72 72 20 3d 20 22 22   */..  zErr = ""
f8670 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 2f 2a 20 4e  ;..#else..  /* N
f8680 6f 6e 2d 74 68 72 65 61 64 73 61 66 65 20 62 75  on-threadsafe bu
f8690 69 6c 64 2c 20 75 73 65 20 73 74 72 65 72 72 6f  ild, use strerro
f86a0 72 28 29 2e 20 2a 2f 0d 0a 20 20 7a 45 72 72 20  r(). */..  zErr 
f86b0 3d 20 73 74 72 65 72 72 6f 72 28 69 45 72 72 6e  = strerror(iErrn
f86c0 6f 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  o);..#endif.... 
f86d0 20 61 73 73 65 72 74 28 20 65 72 72 63 6f 64 65   assert( errcode
f86e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a  !=SQLITE_OK );..
f86f0 20 20 69 66 28 20 7a 50 61 74 68 3d 3d 30 20 29    if( zPath==0 )
f8700 20 7a 50 61 74 68 20 3d 20 22 22 3b 0d 0a 20 20   zPath = "";..  
f8710 73 71 6c 69 74 65 33 5f 6c 6f 67 28 65 72 72 63  sqlite3_log(errc
f8720 6f 64 65 2c 0d 0a 20 20 20 20 20 20 22 6f 73 5f  ode,..      "os_
f8730 75 6e 69 78 2e 63 3a 25 64 3a 20 28 25 64 29 20  unix.c:%d: (%d) 
f8740 25 73 28 25 73 29 20 2d 20 25 73 22 2c 0d 0a 20  %s(%s) - %s",.. 
f8750 20 20 20 20 20 69 4c 69 6e 65 2c 20 69 45 72 72       iLine, iErr
f8760 6e 6f 2c 20 7a 46 75 6e 63 2c 20 7a 50 61 74 68  no, zFunc, zPath
f8770 2c 20 7a 45 72 72 0d 0a 20 20 29 3b 0d 0a 0d 0a  , zErr..  );....
f8780 20 20 72 65 74 75 72 6e 20 65 72 72 63 6f 64 65    return errcode
f8790 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
f87a0 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63  lose a file desc
f87b0 72 69 70 74 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  riptor...**..** 
f87c0 57 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 63  We assume that c
f87d0 6c 6f 73 65 28 29 20 61 6c 6d 6f 73 74 20 61 6c  lose() almost al
f87e0 77 61 79 73 20 77 6f 72 6b 73 2c 20 73 69 6e 63  ways works, sinc
f87f0 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 69 6e 20  e it is only in 
f8800 61 0d 0a 2a 2a 20 76 65 72 79 20 73 69 63 6b 20  a..** very sick 
f8810 61 70 70 6c 69 63 61 74 69 6f 6e 20 6f 72 20 6f  application or o
f8820 6e 20 61 20 76 65 72 79 20 73 69 63 6b 20 70 6c  n a very sick pl
f8830 61 74 66 6f 72 6d 20 74 68 61 74 20 69 74 20 6d  atform that it m
f8840 69 67 68 74 20 66 61 69 6c 2e 0d 0a 2a 2a 20 49  ight fail...** I
f8850 66 20 69 74 20 64 6f 65 73 20 66 61 69 6c 2c 20  f it does fail, 
f8860 73 69 6d 70 6c 79 20 6c 65 61 6b 20 74 68 65 20  simply leak the 
f8870 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c  file descriptor,
f8880 20 62 75 74 20 64 6f 20 6c 6f 67 20 74 68 65 0d   but do log the.
f8890 0a 2a 2a 20 65 72 72 6f 72 2e 0d 0a 2a 2a 0d 0a  .** error...**..
f88a0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 74 20  ** Note that it 
f88b0 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72  is not safe to r
f88c0 65 74 72 79 20 63 6c 6f 73 65 28 29 20 61 66 74  etry close() aft
f88d0 65 72 20 45 49 4e 54 52 20 73 69 6e 63 65 20 74  er EINTR since t
f88e0 68 65 0d 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63  he..** file desc
f88f0 72 69 70 74 6f 72 20 6d 69 67 68 74 20 68 61 76  riptor might hav
f8900 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72  e already been r
f8910 65 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72  eused by another
f8920 20 74 68 72 65 61 64 2e 0d 0a 2a 2a 20 53 6f 20   thread...** So 
f8930 77 65 20 64 6f 6e 27 74 20 65 76 65 6e 20 74 72  we don't even tr
f8940 79 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72 6f  y to recover fro
f8950 6d 20 61 6e 20 45 49 4e 54 52 2e 20 20 4a 75 73  m an EINTR.  Jus
f8960 74 20 6c 6f 67 20 74 68 65 20 65 72 72 6f 72 0d  t log the error.
f8970 0a 2a 2a 20 61 6e 64 20 6d 6f 76 65 20 6f 6e 2e  .** and move on.
f8980 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
f8990 64 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 75  d robust_close(u
f89a0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20  nixFile *pFile, 
f89b0 69 6e 74 20 68 2c 20 69 6e 74 20 6c 69 6e 65 6e  int h, int linen
f89c0 6f 29 7b 0d 0a 20 20 69 66 28 20 6f 73 43 6c 6f  o){..  if( osClo
f89d0 73 65 28 68 29 20 29 7b 0d 0a 20 20 20 20 75 6e  se(h) ){..    un
f89e0 69 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e 65  ixLogErrorAtLine
f89f0 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c  (SQLITE_IOERR_CL
f8a00 4f 53 45 2c 20 22 63 6c 6f 73 65 22 2c 0d 0a 20  OSE, "close",.. 
f8a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8a20 20 20 20 20 20 20 70 46 69 6c 65 20 3f 20 70 46        pFile ? pF
f8a30 69 6c 65 2d 3e 7a 50 61 74 68 20 3a 20 30 2c 20  ile->zPath : 0, 
f8a40 6c 69 6e 65 6e 6f 29 3b 0d 0a 20 20 7d 0d 0a 7d  lineno);..  }..}
f8a50 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65  ..../*..** Close
f8a60 20 61 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 69   all file descri
f8a70 70 74 6f 72 73 20 61 63 63 75 6d 75 61 74 65 64  ptors accumuated
f8a80 20 69 6e 20 74 68 65 20 75 6e 69 78 49 6e 6f 64   in the unixInod
f8a90 65 49 6e 66 6f 2d 3e 70 55 6e 75 73 65 64 20 6c  eInfo->pUnused l
f8aa0 69 73 74 2e 0d 0a 2a 2f 20 0d 0a 73 74 61 74 69  ist...*/ ..stati
f8ab0 63 20 76 6f 69 64 20 63 6c 6f 73 65 50 65 6e 64  c void closePend
f8ac0 69 6e 67 46 64 73 28 75 6e 69 78 46 69 6c 65 20  ingFds(unixFile 
f8ad0 2a 70 46 69 6c 65 29 7b 0d 0a 20 20 75 6e 69 78  *pFile){..  unix
f8ae0 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64  InodeInfo *pInod
f8af0 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  e = pFile->pInod
f8b00 65 3b 0d 0a 20 20 55 6e 69 78 55 6e 75 73 65 64  e;..  UnixUnused
f8b10 46 64 20 2a 70 3b 0d 0a 20 20 55 6e 69 78 55 6e  Fd *p;..  UnixUn
f8b20 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 0d 0a  usedFd *pNext;..
f8b30 20 20 66 6f 72 28 70 3d 70 49 6e 6f 64 65 2d 3e    for(p=pInode->
f8b40 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 3d 70 4e  pUnused; p; p=pN
f8b50 65 78 74 29 7b 0d 0a 20 20 20 20 70 4e 65 78 74  ext){..    pNext
f8b60 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20   = p->pNext;..  
f8b70 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70    robust_close(p
f8b80 46 69 6c 65 2c 20 70 2d 3e 66 64 2c 20 5f 5f 4c  File, p->fd, __L
f8b90 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20 73 71 6c  INE__);..    sql
f8ba0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
f8bb0 20 7d 0d 0a 20 20 70 49 6e 6f 64 65 2d 3e 70 55   }..  pInode->pU
f8bc0 6e 75 73 65 64 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d  nused = 0;..}...
f8bd0 0a 2f 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ./*..** Release 
f8be0 61 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20  a unixInodeInfo 
f8bf0 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
f8c00 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62  usly allocated b
f8c10 79 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28  y findInodeInfo(
f8c20 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6d  )...**..** The m
f8c30 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 69  utex entered usi
f8c40 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72  ng the unixEnter
f8c50 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f 6e  Mutex() function
f8c60 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0d 0a 2a   must be held..*
f8c70 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * when this func
f8c80 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0d  tion is called..
f8c90 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
f8ca0 20 72 65 6c 65 61 73 65 49 6e 6f 64 65 49 6e 66   releaseInodeInf
f8cb0 6f 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  o(unixFile *pFil
f8cc0 65 29 7b 0d 0a 20 20 75 6e 69 78 49 6e 6f 64 65  e){..  unixInode
f8cd0 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 20 3d 20 70  Info *pInode = p
f8ce0 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0d 0a 20  File->pInode;.. 
f8cf0 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74   assert( unixMut
f8d00 65 78 48 65 6c 64 28 29 20 29 3b 0d 0a 20 20 69  exHeld() );..  i
f8d10 66 28 20 41 4c 57 41 59 53 28 70 49 6e 6f 64 65  f( ALWAYS(pInode
f8d20 29 20 29 7b 0d 0a 20 20 20 20 70 49 6e 6f 64 65  ) ){..    pInode
f8d30 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a 20 20 20 20 69  ->nRef--;..    i
f8d40 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 66 3d  f( pInode->nRef=
f8d50 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  =0 ){..      ass
f8d60 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 53 68  ert( pInode->pSh
f8d70 6d 4e 6f 64 65 3d 3d 30 20 29 3b 0d 0a 20 20 20  mNode==0 );..   
f8d80 20 20 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46     closePendingF
f8d90 64 73 28 70 46 69 6c 65 29 3b 0d 0a 20 20 20 20  ds(pFile);..    
f8da0 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 70 50    if( pInode->pP
f8db0 72 65 76 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  rev ){..        
f8dc0 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e  assert( pInode->
f8dd0 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 49  pPrev->pNext==pI
f8de0 6e 6f 64 65 20 29 3b 0d 0a 20 20 20 20 20 20 20  node );..       
f8df0 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 76 2d 3e   pInode->pPrev->
f8e00 70 4e 65 78 74 20 3d 20 70 49 6e 6f 64 65 2d 3e  pNext = pInode->
f8e10 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 7d 65  pNext;..      }e
f8e20 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 61 73  lse{..        as
f8e30 73 65 72 74 28 20 69 6e 6f 64 65 4c 69 73 74 3d  sert( inodeList=
f8e40 3d 70 49 6e 6f 64 65 20 29 3b 0d 0a 20 20 20 20  =pInode );..    
f8e50 20 20 20 20 69 6e 6f 64 65 4c 69 73 74 20 3d 20      inodeList = 
f8e60 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 3b 0d 0a  pInode->pNext;..
f8e70 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
f8e80 66 28 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74  f( pInode->pNext
f8e90 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73   ){..        ass
f8ea0 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 4e 65  ert( pInode->pNe
f8eb0 78 74 2d 3e 70 50 72 65 76 3d 3d 70 49 6e 6f 64  xt->pPrev==pInod
f8ec0 65 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 49  e );..        pI
f8ed0 6e 6f 64 65 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  node->pNext->pPr
f8ee0 65 76 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 50 72  ev = pInode->pPr
f8ef0 65 76 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ev;..      }..  
f8f00 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f8f10 28 70 49 6e 6f 64 65 29 3b 0d 0a 20 20 20 20 7d  (pInode);..    }
f8f20 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
f8f30 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20  ** Given a file 
f8f40 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61  descriptor, loca
f8f50 74 65 20 74 68 65 20 75 6e 69 78 49 6e 6f 64 65  te the unixInode
f8f60 49 6e 66 6f 20 6f 62 6a 65 63 74 20 74 68 61 74  Info object that
f8f70 0d 0a 2a 2a 20 64 65 73 63 72 69 62 65 73 20 74  ..** describes t
f8f80 68 61 74 20 66 69 6c 65 20 64 65 73 63 72 69 70  hat file descrip
f8f90 74 6f 72 2e 20 20 43 72 65 61 74 65 20 61 20 6e  tor.  Create a n
f8fa0 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73  ew one if necess
f8fb0 61 72 79 2e 20 20 54 68 65 0d 0a 2a 2a 20 72 65  ary.  The..** re
f8fc0 74 75 72 6e 20 76 61 6c 75 65 20 6d 69 67 68 74  turn value might
f8fd0 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   be uninitialize
f8fe0 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  d if an error oc
f8ff0 63 75 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  curs...**..** Th
f9000 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20  e mutex entered 
f9010 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e  using the unixEn
f9020 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74  terMutex() funct
f9030 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64  ion must be held
f9040 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66  ..** when this f
f9050 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
f9060 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72  d...**..** Retur
f9070 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  n an appropriate
f9080 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f   error code...*/
f9090 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e  ..static int fin
f90a0 64 49 6e 6f 64 65 49 6e 66 6f 28 0d 0a 20 20 75  dInodeInfo(..  u
f90b0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20  nixFile *pFile, 
f90c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f90d0 20 55 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20   Unix file with 
f90e0 66 69 6c 65 20 64 65 73 63 20 75 73 65 64 20 69  file desc used i
f90f0 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0d 0a 20 20  n the key */..  
f9100 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 2a  unixInodeInfo **
f9110 70 70 49 6e 6f 64 65 20 20 20 20 20 20 20 20 2f  ppInode        /
f9120 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69  * Return the uni
f9130 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63  xInodeInfo objec
f9140 74 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  t here */..){.. 
f9150 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
f9160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f9170 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20 72  /* System call r
f9180 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20  eturn code */.. 
f9190 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
f91a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f91b0 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
f91c0 72 69 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65  riptor for pFile
f91d0 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 75 6e   */..  struct un
f91e0 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b  ixFileId fileId;
f91f0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20        /* Lookup 
f9200 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78  key for the unix
f9210 49 6e 6f 64 65 49 6e 66 6f 20 2a 2f 0d 0a 20 20  InodeInfo */..  
f9220 73 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74  struct stat stat
f9230 62 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f  buf;           /
f9240 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65  * Low-level file
f9250 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0d   information */.
f9260 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f  .  unixInodeInfo
f9270 20 2a 70 49 6e 6f 64 65 20 3d 20 30 3b 20 20 20   *pInode = 0;   
f9280 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75    /* Candidate u
f9290 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a  nixInodeInfo obj
f92a0 65 63 74 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65  ect */....  asse
f92b0 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  rt( unixMutexHel
f92c0 64 28 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 47  d() );....  /* G
f92d0 65 74 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 66  et low-level inf
f92e0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
f92f0 68 65 20 66 69 6c 65 20 74 68 61 74 20 77 65 20  he file that we 
f9300 63 61 6e 20 75 73 65 64 20 74 6f 0d 0a 20 20 2a  can used to..  *
f9310 2a 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75  * create a uniqu
f9320 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66  e name for the f
f9330 69 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 64  ile...  */..  fd
f9340 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0d 0a 20 20   = pFile->h;..  
f9350 72 63 20 3d 20 6f 73 46 73 74 61 74 28 66 64 2c  rc = osFstat(fd,
f9360 20 26 73 74 61 74 62 75 66 29 3b 0d 0a 20 20 69   &statbuf);..  i
f9370 66 28 20 72 63 21 3d 30 20 29 7b 0d 0a 20 20 20  f( rc!=0 ){..   
f9380 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f9390 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 23 69 66 64  o = errno;..#ifd
f93a0 65 66 20 45 4f 56 45 52 46 4c 4f 57 0d 0a 20 20  ef EOVERFLOW..  
f93b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73    if( pFile->las
f93c0 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f  tErrno==EOVERFLO
f93d0 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
f93e0 45 5f 4e 4f 4c 46 53 3b 0d 0a 23 65 6e 64 69 66  E_NOLFS;..#endif
f93f0 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
f9400 49 54 45 5f 49 4f 45 52 52 3b 0d 0a 20 20 7d 0d  ITE_IOERR;..  }.
f9410 0a 0d 0a 23 69 66 64 65 66 20 5f 5f 41 50 50 4c  ...#ifdef __APPL
f9420 45 5f 5f 0d 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20  E__..  /* On OS 
f9430 58 20 6f 6e 20 61 6e 20 6d 73 64 6f 73 20 66 69  X on an msdos fi
f9440 6c 65 73 79 73 74 65 6d 2c 20 74 68 65 20 69 6e  lesystem, the in
f9450 6f 64 65 20 6e 75 6d 62 65 72 20 69 73 20 72 65  ode number is re
f9460 70 6f 72 74 65 64 0d 0a 20 20 2a 2a 20 69 6e 63  ported..  ** inc
f9470 6f 72 72 65 63 74 6c 79 20 66 6f 72 20 7a 65 72  orrectly for zer
f9480 6f 2d 73 69 7a 65 20 66 69 6c 65 73 2e 20 20 53  o-size files.  S
f9490 65 65 20 74 69 63 6b 65 74 20 23 33 32 36 30 2e  ee ticket #3260.
f94a0 20 20 54 6f 20 77 6f 72 6b 0d 0a 20 20 2a 2a 20    To work..  ** 
f94b0 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62  around this prob
f94c0 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69 64 65 72  lem (we consider
f94d0 20 69 74 20 61 20 62 75 67 20 69 6e 20 4f 53 20   it a bug in OS 
f94e0 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65 29 0d 0a  X, not SQLite)..
f94f0 20 20 2a 2a 20 77 65 20 61 6c 77 61 79 73 20 69    ** we always i
f9500 6e 63 72 65 61 73 65 20 74 68 65 20 66 69 6c 65  ncrease the file
f9510 20 73 69 7a 65 20 74 6f 20 31 20 62 79 20 77 72   size to 1 by wr
f9520 69 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 62  iting a single b
f9530 79 74 65 0d 0a 20 20 2a 2a 20 70 72 69 6f 72 20  yte..  ** prior 
f9540 74 6f 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  to accessing the
f9550 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20   inode number.  
f9560 54 68 65 20 6f 6e 65 20 62 79 74 65 20 77 72 69  The one byte wri
f9570 74 74 65 6e 20 69 73 0d 0a 20 20 2a 2a 20 61 6e  tten is..  ** an
f9580 20 41 53 43 49 49 20 27 53 27 20 63 68 61 72 61   ASCII 'S' chara
f9590 63 74 65 72 20 77 68 69 63 68 20 61 6c 73 6f 20  cter which also 
f95a0 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74 68  happens to be th
f95b0 65 20 66 69 72 73 74 20 62 79 74 65 0d 0a 20 20  e first byte..  
f95c0 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ** in the header
f95d0 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69 74 65   of every SQLite
f95e0 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e 20 74   database.  In t
f95f0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 72  his way, if ther
f9600 65 0d 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 63  e..  ** is a rac
f9610 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 68  e condition such
f9620 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68   that another th
f9630 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 64 79  read has already
f9640 20 70 6f 70 75 6c 61 74 65 64 0d 0a 20 20 2a 2a   populated..  **
f9650 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
f9660 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  of the database,
f9670 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f   no damage is do
f9680 6e 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ne...  */..  if(
f9690 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65   statbuf.st_size
f96a0 3d 3d 30 20 26 26 20 28 70 46 69 6c 65 2d 3e 66  ==0 && (pFile->f
f96b0 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  sFlags & SQLITE_
f96c0 46 53 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53  FSFLAGS_IS_MSDOS
f96d0 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 64 6f 7b  )!=0 ){..    do{
f96e0 20 72 63 20 3d 20 6f 73 57 72 69 74 65 28 66 64   rc = osWrite(fd
f96f0 2c 20 22 53 22 2c 20 31 29 3b 20 7d 77 68 69 6c  , "S", 1); }whil
f9700 65 28 20 72 63 3c 30 20 26 26 20 65 72 72 6e 6f  e( rc<0 && errno
f9710 3d 3d 45 49 4e 54 52 20 29 3b 0d 0a 20 20 20 20  ==EINTR );..    
f9720 69 66 28 20 72 63 21 3d 31 20 29 7b 0d 0a 20 20  if( rc!=1 ){..  
f9730 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
f9740 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20  rrno = errno;.. 
f9750 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
f9760 54 45 5f 49 4f 45 52 52 3b 0d 0a 20 20 20 20 7d  TE_IOERR;..    }
f9770 0d 0a 20 20 20 20 72 63 20 3d 20 6f 73 46 73 74  ..    rc = osFst
f9780 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29  at(fd, &statbuf)
f9790 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30  ;..    if( rc!=0
f97a0 20 29 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65   ){..      pFile
f97b0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
f97c0 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  rno;..      retu
f97d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
f97e0 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65  ..    }..  }..#e
f97f0 6e 64 69 66 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74  ndif....  memset
f9800 28 26 66 69 6c 65 49 64 2c 20 30 2c 20 73 69 7a  (&fileId, 0, siz
f9810 65 6f 66 28 66 69 6c 65 49 64 29 29 3b 0d 0a 20  eof(fileId));.. 
f9820 20 66 69 6c 65 49 64 2e 64 65 76 20 3d 20 73 74   fileId.dev = st
f9830 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0d 0a 23  atbuf.st_dev;..#
f9840 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 20  if OS_VXWORKS.. 
f9850 20 66 69 6c 65 49 64 2e 70 49 64 20 3d 20 70 46   fileId.pId = pF
f9860 69 6c 65 2d 3e 70 49 64 3b 0d 0a 23 65 6c 73 65  ile->pId;..#else
f9870 0d 0a 20 20 66 69 6c 65 49 64 2e 69 6e 6f 20 3d  ..  fileId.ino =
f9880 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b   statbuf.st_ino;
f9890 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70 49 6e 6f  ..#endif..  pIno
f98a0 64 65 20 3d 20 69 6e 6f 64 65 4c 69 73 74 3b 0d  de = inodeList;.
f98b0 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 6f 64 65  .  while( pInode
f98c0 20 26 26 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65   && memcmp(&file
f98d0 49 64 2c 20 26 70 49 6e 6f 64 65 2d 3e 66 69 6c  Id, &pInode->fil
f98e0 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69 6c 65  eId, sizeof(file
f98f0 49 64 29 29 20 29 7b 0d 0a 20 20 20 20 70 49 6e  Id)) ){..    pIn
f9900 6f 64 65 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 4e  ode = pInode->pN
f9910 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  ext;..  }..  if(
f9920 20 70 49 6e 6f 64 65 3d 3d 30 20 29 7b 0d 0a 20   pInode==0 ){.. 
f9930 20 20 20 70 49 6e 6f 64 65 20 3d 20 73 71 6c 69     pInode = sqli
f9940 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
f9950 6f 66 28 2a 70 49 6e 6f 64 65 29 20 29 3b 0d 0a  of(*pInode) );..
f9960 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 3d 3d      if( pInode==
f9970 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  0 ){..      retu
f9980 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
f9990 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 6d 65 6d  ..    }..    mem
f99a0 73 65 74 28 70 49 6e 6f 64 65 2c 20 30 2c 20 73  set(pInode, 0, s
f99b0 69 7a 65 6f 66 28 2a 70 49 6e 6f 64 65 29 29 3b  izeof(*pInode));
f99c0 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 49  ..    memcpy(&pI
f99d0 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 26 66  node->fileId, &f
f99e0 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69  ileId, sizeof(fi
f99f0 6c 65 49 64 29 29 3b 0d 0a 20 20 20 20 70 49 6e  leId));..    pIn
f9a00 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0d 0a  ode->nRef = 1;..
f9a10 20 20 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78      pInode->pNex
f9a20 74 20 3d 20 69 6e 6f 64 65 4c 69 73 74 3b 0d 0a  t = inodeList;..
f9a30 20 20 20 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65      pInode->pPre
f9a40 76 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20  v = 0;..    if( 
f9a50 69 6e 6f 64 65 4c 69 73 74 20 29 20 69 6e 6f 64  inodeList ) inod
f9a60 65 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70  eList->pPrev = p
f9a70 49 6e 6f 64 65 3b 0d 0a 20 20 20 20 69 6e 6f 64  Inode;..    inod
f9a80 65 4c 69 73 74 20 3d 20 70 49 6e 6f 64 65 3b 0d  eList = pInode;.
f9a90 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
f9aa0 49 6e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a  Inode->nRef++;..
f9ab0 20 20 7d 0d 0a 20 20 2a 70 70 49 6e 6f 64 65 20    }..  *ppInode 
f9ac0 3d 20 70 49 6e 6f 64 65 3b 0d 0a 20 20 72 65 74  = pInode;..  ret
f9ad0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
f9ae0 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  }....../*..** Th
f9af0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
f9b00 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
f9b10 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
f9b20 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
f9b30 69 65 64 0d 0a 2a 2a 20 66 69 6c 65 20 62 79 20  ied..** file by 
f9b40 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
f9b50 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
f9b60 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
f9b70 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0d  d, set *pResOut.
f9b80 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
f9b90 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
f9ba0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
f9bb0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
f9bc0 72 65 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a  return value..**
f9bd0 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
f9be0 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49  E_OK unless an I
f9bf0 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  /O error occurs 
f9c00 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63  during lock chec
f9c10 6b 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  king...*/..stati
f9c20 63 20 69 6e 74 20 75 6e 69 78 43 68 65 63 6b 52  c int unixCheckR
f9c30 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
f9c40 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
f9c50 74 20 2a 70 52 65 73 4f 75 74 29 7b 0d 0a 20 20  t *pResOut){..  
f9c60 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f9c70 4f 4b 3b 0d 0a 20 20 69 6e 74 20 72 65 73 65 72  OK;..  int reser
f9c80 76 65 64 20 3d 20 30 3b 0d 0a 20 20 75 6e 69 78  ved = 0;..  unix
f9c90 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
f9ca0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 0d 0a  nixFile*)id;....
f9cb0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
f9cc0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
f9cd0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
f9ce0 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0d 0a 0d 0a  RVEDLOCK; );....
f9cf0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
f9d00 29 3b 0d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d  );..  unixEnterM
f9d10 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75  utex(); /* Becau
f9d20 73 65 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  se pFile->pInode
f9d30 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
f9d40 73 20 74 68 72 65 61 64 73 20 2a 2f 0d 0a 0d 0a  s threads */....
f9d50 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
f9d60 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
f9d70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
f9d80 68 20 61 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69  h a lock */..  i
f9d90 66 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  f( pFile->pInode
f9da0 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52  ->eFileLock>SHAR
f9db0 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20  ED_LOCK ){..    
f9dc0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0d 0a 20  reserved = 1;.. 
f9dd0 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4f 74 68 65 72   }....  /* Other
f9de0 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65  wise see if some
f9df0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68   other process h
f9e00 6f 6c 64 73 20 69 74 2e 0d 0a 20 20 2a 2f 0d 0a  olds it...  */..
f9e10 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 50 5f  #ifndef __DJGPP_
f9e20 5f 0d 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  _..  if( !reserv
f9e30 65 64 20 26 26 20 21 70 46 69 6c 65 2d 3e 70 49  ed && !pFile->pI
f9e40 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f  node->bProcessLo
f9e50 63 6b 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63  ck ){..    struc
f9e60 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0d 0a 20  t flock lock;.. 
f9e70 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65     lock.l_whence
f9e80 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0d 0a 20 20   = SEEK_SET;..  
f9e90 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
f9ea0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0d   RESERVED_BYTE;.
f9eb0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
f9ec0 3d 20 31 3b 0d 0a 20 20 20 20 6c 6f 63 6b 2e 6c  = 1;..    lock.l
f9ed0 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b  _type = F_WRLCK;
f9ee0 0d 0a 20 20 20 20 69 66 28 20 6f 73 46 63 6e 74  ..    if( osFcnt
f9ef0 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45  l(pFile->h, F_GE
f9f00 54 4c 4b 2c 20 26 6c 6f 63 6b 29 20 29 7b 0d 0a  TLK, &lock) ){..
f9f10 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
f9f20 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
f9f30 45 52 56 45 44 4c 4f 43 4b 3b 0d 0a 20 20 20 20  ERVEDLOCK;..    
f9f40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
f9f50 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20  no = errno;..   
f9f60 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b   } else if( lock
f9f70 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b  .l_type!=F_UNLCK
f9f80 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 73 65 72   ){..      reser
f9f90 76 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  ved = 1;..    }.
f9fa0 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  .  }..#endif..  
f9fb0 0d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  ..  unixLeaveMut
f9fc0 65 78 28 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45  ex();..  OSTRACE
f9fd0 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  (("TEST WR-LOCK 
f9fe0 25 64 20 25 64 20 25 64 20 28 75 6e 69 78 29 5c  %d %d %d (unix)\
f9ff0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
fa000 2c 20 72 65 73 65 72 76 65 64 29 29 3b 0d 0a 0d  , reserved));...
fa010 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
fa020 73 65 72 76 65 64 3b 0d 0a 20 20 72 65 74 75 72  served;..  retur
fa030 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
fa040 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65  ** Attempt to se
fa050 74 20 61 20 73 79 73 74 65 6d 2d 6c 6f 63 6b 20  t a system-lock 
fa060 6f 6e 20 74 68 65 20 66 69 6c 65 20 70 46 69 6c  on the file pFil
fa070 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69 73 20  e.  The lock is 
fa080 0d 0a 2a 2a 20 64 65 73 63 72 69 62 65 64 20 62  ..** described b
fa090 79 20 70 4c 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  y pLock...**..**
fa0a0 20 49 66 20 74 68 65 20 70 46 69 6c 65 20 77 61   If the pFile wa
fa0b0 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72  s opened read/wr
fa0c0 69 74 65 20 66 72 6f 6d 20 75 6e 69 78 2d 65 78  ite from unix-ex
fa0d0 63 6c 2c 20 74 68 65 6e 20 74 68 65 20 6f 6e 6c  cl, then the onl
fa0e0 79 20 6c 6f 63 6b 0d 0a 2a 2a 20 65 76 65 72 20  y lock..** ever 
fa0f0 6f 62 74 61 69 6e 65 64 20 69 73 20 61 6e 20 65  obtained is an e
fa100 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 61  xclusive lock, a
fa110 6e 64 20 69 74 20 69 73 20 6f 62 74 61 69 6e 65  nd it is obtaine
fa120 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 0d 0a  d exactly once..
fa130 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  ** the first tim
fa140 65 20 61 6e 79 20 6c 6f 63 6b 20 69 73 20 61 74  e any lock is at
fa150 74 65 6d 70 74 65 64 2e 20 20 41 6c 6c 20 73 75  tempted.  All su
fa160 62 73 65 71 75 65 6e 74 20 73 79 73 74 65 6d 20  bsequent system 
fa170 6c 6f 63 6b 69 6e 67 0d 0a 2a 2a 20 6f 70 65 72  locking..** oper
fa180 61 74 69 6f 6e 73 20 62 65 63 6f 6d 65 20 6e 6f  ations become no
fa190 2d 6f 70 73 2e 20 20 4c 6f 63 6b 69 6e 67 20 6f  -ops.  Locking o
fa1a0 70 65 72 61 74 69 6f 6e 73 20 73 74 69 6c 6c 20  perations still 
fa1b0 68 61 70 70 65 6e 20 69 6e 74 65 72 6e 61 6c 6c  happen internall
fa1c0 79 2c 0d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  y,..** in order 
fa1d0 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 61 63  to coordinate ac
fa1e0 63 65 73 73 20 62 65 74 77 65 65 6e 20 73 65 70  cess between sep
fa1f0 61 72 61 74 65 20 64 61 74 61 62 61 73 65 20 63  arate database c
fa200 6f 6e 6e 65 63 74 69 6f 6e 73 0d 0a 2a 2a 20 77  onnections..** w
fa210 69 74 68 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ithin this proce
fa220 73 73 2c 20 62 75 74 20 61 6c 6c 20 6f 66 20 74  ss, but all of t
fa230 68 61 74 20 69 73 20 68 61 6e 64 6c 65 64 20 69  hat is handled i
fa240 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 20 74 68 65  n memory and the
fa250 0d 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73  ..** operating s
fa260 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 70  ystem does not p
fa270 61 72 74 69 63 69 70 61 74 65 2e 0d 0a 2a 2a 0d  articipate...**.
fa280 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
fa290 6e 20 69 73 20 61 20 70 61 73 73 2d 74 68 72 6f  n is a pass-thro
fa2a0 75 67 68 20 74 6f 20 66 63 6e 74 6c 28 46 5f 53  ugh to fcntl(F_S
fa2b0 45 54 4c 4b 29 20 69 66 20 70 46 69 6c 65 20 69  ETLK) if pFile i
fa2c0 73 20 75 73 69 6e 67 0d 0a 2a 2a 20 61 6e 79 20  s using..** any 
fa2d0 56 46 53 20 6f 74 68 65 72 20 74 68 61 6e 20 22  VFS other than "
fa2e0 75 6e 69 78 2d 65 78 63 6c 22 20 6f 72 20 69 66  unix-excl" or if
fa2f0 20 70 46 69 6c 65 20 69 73 20 6f 70 65 6e 65 64   pFile is opened
fa300 20 6f 6e 20 22 75 6e 69 78 2d 65 78 63 6c 22 0d   on "unix-excl".
fa310 0a 2a 2a 20 61 6e 64 20 69 73 20 72 65 61 64 2d  .** and is read-
fa320 6f 6e 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5a 65  only...**..** Ze
fa330 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ro is returned i
fa340 66 20 74 68 65 20 63 61 6c 6c 20 63 6f 6d 70 6c  f the call compl
fa350 65 74 65 73 20 73 75 63 63 65 73 73 66 75 6c 6c  etes successfull
fa360 79 2c 20 6f 72 20 2d 31 20 69 66 20 61 20 63 61  y, or -1 if a ca
fa370 6c 6c 0d 0a 2a 2a 20 74 6f 20 66 63 6e 74 6c 28  ll..** to fcntl(
fa380 29 20 66 61 69 6c 73 2e 20 49 6e 20 74 68 69 73  ) fails. In this
fa390 20 63 61 73 65 2c 20 65 72 72 6e 6f 20 69 73 20   case, errno is 
fa3a0 73 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c  set appropriatel
fa3b0 79 20 28 62 79 20 66 63 6e 74 6c 28 29 29 2e 0d  y (by fcntl())..
fa3c0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
fa3d0 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 75 6e 69  unixFileLock(uni
fa3e0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 73 74  xFile *pFile, st
fa3f0 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 4c 6f 63  ruct flock *pLoc
fa400 6b 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  k){..  int rc;..
fa410 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20    unixInodeInfo 
fa420 2a 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d  *pInode = pFile-
fa430 3e 70 49 6e 6f 64 65 3b 0d 0a 20 20 61 73 73 65  >pInode;..  asse
fa440 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  rt( unixMutexHel
fa450 64 28 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  d() );..  assert
fa460 28 20 70 49 6e 6f 64 65 21 3d 30 20 29 3b 0d 0a  ( pInode!=0 );..
fa470 20 20 69 66 28 20 28 28 70 46 69 6c 65 2d 3e 63    if( ((pFile->c
fa480 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46  trlFlags & UNIXF
fa490 49 4c 45 5f 45 58 43 4c 29 21 3d 30 20 7c 7c 20  ILE_EXCL)!=0 || 
fa4a0 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73  pInode->bProcess
fa4b0 4c 6f 63 6b 29 0d 0a 20 20 20 26 26 20 28 28 70  Lock)..   && ((p
fa4c0 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20  File->ctrlFlags 
fa4d0 26 20 55 4e 49 58 46 49 4c 45 5f 52 44 4f 4e 4c  & UNIXFILE_RDONL
fa4e0 59 29 3d 3d 30 29 0d 0a 20 20 29 7b 0d 0a 20 20  Y)==0)..  ){..  
fa4f0 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 62 50    if( pInode->bP
fa500 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29 7b  rocessLock==0 ){
fa510 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 66  ..      struct f
fa520 6c 6f 63 6b 20 6c 6f 63 6b 3b 0d 0a 20 20 20 20  lock lock;..    
fa530 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65    assert( pInode
fa540 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a 20  ->nLock==0 );.. 
fa550 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e       lock.l_when
fa560 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0d 0a  ce = SEEK_SET;..
fa570 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
fa580 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  rt = SHARED_FIRS
fa590 54 3b 0d 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  T;..      lock.l
fa5a0 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49  _len = SHARED_SI
fa5b0 5a 45 3b 0d 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  ZE;..      lock.
fa5c0 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
fa5d0 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 73  ;..      rc = os
fa5e0 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  Fcntl(pFile->h, 
fa5f0 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b  F_SETLK, &lock);
fa600 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3c 30  ..      if( rc<0
fa610 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20   ) return rc;.. 
fa620 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 62 50 72       pInode->bPr
fa630 6f 63 65 73 73 4c 6f 63 6b 20 3d 20 31 3b 0d 0a  ocessLock = 1;..
fa640 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c        pInode->nL
fa650 6f 63 6b 2b 2b 3b 0d 0a 20 20 20 20 7d 65 6c 73  ock++;..    }els
fa660 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 30  e{..      rc = 0
fa670 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
fa680 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20 6f 73 46  e{..    rc = osF
fa690 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
fa6a0 5f 53 45 54 4c 4b 2c 20 70 4c 6f 63 6b 29 3b 0d  _SETLK, pLock);.
fa6b0 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
fa6c0 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
fa6d0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
fa6e0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
fa6f0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
fa700 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d 20 6f  er eFileLock - o
fa710 6e 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  ne..** of the fo
fa720 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  llowing:..**..**
fa730 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
fa740 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20 28 32 29  LOCK..**     (2)
fa750 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0d 0a   RESERVED_LOCK..
fa760 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
fa770 4e 47 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20  NG_LOCK..**     
fa780 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
fa790 43 4b 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d 65 74  CK..**..** Somet
fa7a0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
fa7b0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
fa7c0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
fa7d0 6c 6f 63 6b 20 73 74 61 74 65 73 0d 0a 2a 2a 20  lock states..** 
fa7e0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
fa7f0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
fa800 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
fa810 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
fa820 61 74 65 72 0d 0a 2a 2a 20 74 72 61 6e 73 69 74  ater..** transit
fa830 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
fa840 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
fa850 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
fa860 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0d 0a  it started but..
fa870 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
fa880 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
fa890 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
fa8a0 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
fa8b0 65 64 0d 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ed..** transitio
fa8c0 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
fa8d0 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
fa8e0 20 73 74 61 74 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a   states:..**..**
fa8f0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
fa900 53 48 41 52 45 44 0d 0a 2a 2a 20 20 20 20 53 48  SHARED..**    SH
fa910 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
fa920 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  ..**    SHARED -
fa930 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
fa940 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 20 20 20  XCLUSIVE..**    
fa950 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
fa960 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
fa970 56 45 0d 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  VE..**    PENDIN
fa980 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a  G -> EXCLUSIVE..
fa990 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
fa9a0 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
fa9b0 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
fa9c0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
fa9d0 73 55 6e 6c 6f 63 6b 28 29 0d 0a 2a 2a 20 72 6f  sUnlock()..** ro
fa9e0 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61  utine to lower a
fa9f0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0d   locking level..
faa00 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
faa10 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33  unixLock(sqlite3
faa20 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65  _file *id, int e
faa30 46 69 6c 65 4c 6f 63 6b 29 7b 0d 0a 20 20 2f 2a  FileLock){..  /*
faa40 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
faa50 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
faa60 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
faa70 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
faa80 20 61 6e 64 0d 0a 20 20 2a 2a 20 6c 6f 63 6b 20   and..  ** lock 
faa90 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74  transitions in t
faaa0 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49  erms of the POSI
faab0 58 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65  X advisory share
faac0 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0d  d and exclusive.
faad0 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69  .  ** lock primi
faae0 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65  tives (called re
faaf0 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69  ad-locks and wri
fab00 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20  te-locks below, 
fab10 74 6f 20 61 76 6f 69 64 0d 0a 20 20 2a 2a 20 63  to avoid..  ** c
fab20 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51  onfusion with SQ
fab30 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29  Lite lock names)
fab40 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73  . The algorithms
fab50 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64   are complicated
fab60 0d 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20  ..  ** slightly 
fab70 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63  in order to be c
fab80 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 77  ompatible with w
fab90 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73  indows systems s
faba0 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0d 0a 20  imultaneously.. 
fabb0 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68   ** accessing th
fabc0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
fabd0 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68  file, in case th
fabe0 61 74 20 69 73 20 65 76 65 72 20 72 65 71 75 69  at is ever requi
fabf0 72 65 64 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  red...  **..  **
fac00 20 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64   Symbols defined
fac10 20 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69   in os.h indenti
fac20 66 79 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  fy the 'pending 
fac30 62 79 74 65 27 20 61 6e 64 20 74 68 65 20 27 72  byte' and the 'r
fac40 65 73 65 72 76 65 64 0d 0a 20 20 2a 2a 20 62 79  eserved..  ** by
fac50 74 65 27 2c 20 65 61 63 68 20 73 69 6e 67 6c 65  te', each single
fac60 20 62 79 74 65 73 20 61 74 20 77 65 6c 6c 20 6b   bytes at well k
fac70 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e  nown offsets, an
fac80 64 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79  d the 'shared by
fac90 74 65 0d 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c  te..  ** range',
faca0 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31 30 20   a range of 510 
facb0 62 79 74 65 73 20 61 74 20 61 20 77 65 6c 6c 20  bytes at a well 
facc0 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0d 0a 20  known offset... 
facd0 20 2a 2a 0d 0a 20 20 2a 2a 20 54 6f 20 6f 62 74   **..  ** To obt
face0 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ain a SHARED loc
facf0 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  k, a read-lock i
fad00 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  s obtained on th
fad10 65 20 27 70 65 6e 64 69 6e 67 0d 0a 20 20 2a 2a  e 'pending..  **
fad20 20 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73   byte'.  If this
fad30 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
fad40 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72  a random byte fr
fad50 6f 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62  om the 'shared b
fad60 79 74 65 0d 0a 20 20 2a 2a 20 72 61 6e 67 65 27  yte..  ** range'
fad70 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20   is read-locked 
fad80 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20  and the lock on 
fad90 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74  the 'pending byt
fada0 65 27 20 72 65 6c 65 61 73 65 64 2e 0d 0a 20 20  e' released...  
fadb0 2a 2a 0d 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65  **..  ** A proce
fadc0 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61  ss may only obta
fadd0 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  in a RESERVED lo
fade0 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20  ck after it has 
fadf0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0d 0a  a SHARED lock...
fae00 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
fae10 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
fae20 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
fae30 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
fae40 74 68 65 0d 0a 20 20 2a 2a 20 27 72 65 73 65 72  the..  ** 'reser
fae50 76 65 64 20 62 79 74 65 27 2e 20 0d 0a 20 20 2a  ved byte'. ..  *
fae60 2a 0d 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73  *..  ** A proces
fae70 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69  s may only obtai
fae80 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  n a PENDING lock
fae90 20 61 66 74 65 72 20 69 74 20 68 61 73 20 6f 62   after it has ob
faea0 74 61 69 6e 65 64 20 61 0d 0a 20 20 2a 2a 20 53  tained a..  ** S
faeb0 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45  HARED lock. A PE
faec0 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d  NDING lock is im
faed0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74  plemented by obt
faee0 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  aining a write-l
faef0 6f 63 6b 0d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65  ock..  ** on the
faf00 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e   'pending byte'.
faf10 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
faf20 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44  at no new SHARED
faf30 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0d 0a 20   locks can be.. 
faf40 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75   ** obtained, bu
faf50 74 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 45  t existing SHARE
faf60 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f  D locks are allo
faf70 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20  wed to persist. 
faf80 41 20 70 72 6f 63 65 73 73 0d 0a 20 20 2a 2a 20  A process..  ** 
faf90 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
fafa0 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56   obtain a RESERV
fafb0 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77  ED lock on the w
fafc0 61 79 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20  ay to a PENDING 
fafd0 6c 6f 63 6b 2e 0d 0a 20 20 2a 2a 20 54 68 69 73  lock...  ** This
fafe0 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65   property is use
faff0 64 20 62 79 20 74 68 65 20 61 6c 67 6f 72 69 74  d by the algorit
fb000 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  hm for rolling b
fb010 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ack a journal fi
fb020 6c 65 0d 0a 20 20 2a 2a 20 61 66 74 65 72 20 61  le..  ** after a
fb030 20 63 72 61 73 68 2e 0d 0a 20 20 2a 2a 0d 0a 20   crash...  **.. 
fb040 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45   ** An EXCLUSIVE
fb050 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20   lock, obtained 
fb060 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20  after a PENDING 
fb070 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73  lock is held, is
fb080 0d 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ..  ** implement
fb090 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20  ed by obtaining 
fb0a0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
fb0b0 74 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72  the entire 'shar
fb0c0 65 64 20 62 79 74 65 0d 0a 20 20 2a 2a 20 72 61  ed byte..  ** ra
fb0d0 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20  nge'. Since all 
fb0e0 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75  other locks requ
fb0f0 69 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  ire a read-lock 
fb100 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79  on one of the by
fb110 74 65 73 0d 0a 20 20 2a 2a 20 77 69 74 68 69 6e  tes..  ** within
fb120 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 68 69   this range, thi
fb130 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e  s ensures that n
fb140 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 72  o other locks ar
fb150 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0d 0a 20  e held on the.. 
fb160 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0d 0a   ** database. ..
fb170 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 72    **..  ** The r
fb180 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62  eason a single b
fb190 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  yte cannot be us
fb1a0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
fb1b0 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0d 0a  e 'shared byte..
fb1c0 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74    ** range' is t
fb1d0 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e  hat some version
fb1e0 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20  s of windows do 
fb1f0 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 64  not support read
fb200 2d 6c 6f 63 6b 73 2e 20 42 79 0d 0a 20 20 2a 2a  -locks. By..  **
fb210 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f   locking a rando
fb220 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61  m byte from a ra
fb230 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20  nge, concurrent 
fb240 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79  SHARED locks may
fb250 20 65 78 69 73 74 0d 0a 20 20 2a 2a 20 65 76 65   exist..  ** eve
fb260 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  n if the locking
fb270 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 20   primitive used 
fb280 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 74  is always a writ
fb290 65 2d 6c 6f 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20  e-lock...  */.. 
fb2a0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
fb2b0 5f 4f 4b 3b 0d 0a 20 20 75 6e 69 78 46 69 6c 65  _OK;..  unixFile
fb2c0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
fb2d0 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 75 6e 69 78  ile*)id;..  unix
fb2e0 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64  InodeInfo *pInod
fb2f0 65 3b 0d 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  e;..  struct flo
fb300 63 6b 20 6c 6f 63 6b 3b 0d 0a 20 20 69 6e 74 20  ck lock;..  int 
fb310 74 45 72 72 6e 6f 20 3d 20 30 3b 0d 0a 0d 0a 20  tErrno = 0;.... 
fb320 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
fb330 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c  ;..  OSTRACE(("L
fb340 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
fb350 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25   %s(%s,%d) pid=%
fb360 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69  d (unix)\n", pFi
fb370 6c 65 2d 3e 68 2c 0d 0a 20 20 20 20 20 20 61 7a  le->h,..      az
fb380 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f  FileLock(eFileLo
fb390 63 6b 29 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28  ck), azFileLock(
fb3a0 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
fb3b0 29 2c 0d 0a 20 20 20 20 20 20 61 7a 46 69 6c 65  ),..      azFile
fb3c0 4c 6f 63 6b 28 70 46 69 6c 65 2d 3e 70 49 6e 6f  Lock(pFile->pIno
fb3d0 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20  de->eFileLock), 
fb3e0 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e  pFile->pInode->n
fb3f0 53 68 61 72 65 64 20 2c 20 67 65 74 70 69 64 28  Shared , getpid(
fb400 29 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  )));....  /* If 
fb410 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
fb420 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20   a lock of this 
fb430 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73  type or more res
fb440 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0d  trictive on the.
fb450 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20  .  ** unixFile, 
fb460 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
fb470 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
fb480 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
fb490 73 0d 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65  s..  ** unixEnte
fb4a0 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20  rMutex() hasn't 
fb4b0 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
fb4c0 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46  ..  */..  if( pF
fb4d0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 3d  ile->eFileLock>=
fb4e0 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d 0a 20 20  eFileLock ){..  
fb4f0 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f 43 4b    OSTRACE(("LOCK
fb500 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c      %d %s ok (al
fb510 72 65 61 64 79 20 68 65 6c 64 29 20 28 75 6e 69  ready held) (uni
fb520 78 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  x)\n", pFile->h,
fb530 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a  ..            az
fb540 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f  FileLock(eFileLo
fb550 63 6b 29 29 29 3b 0d 0a 20 20 20 20 72 65 74 75  ck)));..    retu
fb560 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20  rn SQLITE_OK;.. 
fb570 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20   }....  /* Make 
fb580 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
fb590 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
fb5a0 72 65 63 74 2e 0d 0a 20 20 2a 2a 20 20 28 31 29  rect...  **  (1)
fb5b0 20 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66   We never move f
fb5c0 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20  rom unlocked to 
fb5d0 61 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20  anything higher 
fb5e0 74 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b  than shared lock
fb5f0 2e 0d 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c  ...  **  (2) SQL
fb600 69 74 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63  ite never explic
fb610 69 74 6c 79 20 72 65 71 75 65 73 74 73 20 61 20  itly requests a 
fb620 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0d 0a 20 20  pendig lock...  
fb630 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72 65 64  **  (3) A shared
fb640 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79 73 20   lock is always 
fb650 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65 73 65  held when a rese
fb660 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65 71 75  rve lock is requ
fb670 65 73 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ested...  */..  
fb680 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 65  assert( pFile->e
fb690 46 69 6c 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43  FileLock!=NO_LOC
fb6a0 4b 20 7c 7c 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d  K || eFileLock==
fb6b0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a  SHARED_LOCK );..
fb6c0 20 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c    assert( eFileL
fb6d0 6f 63 6b 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ock!=PENDING_LOC
fb6e0 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  K );..  assert( 
fb6f0 65 46 69 6c 65 4c 6f 63 6b 21 3d 52 45 53 45 52  eFileLock!=RESER
fb700 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c  VED_LOCK || pFil
fb710 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48  e->eFileLock==SH
fb720 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a  ARED_LOCK );....
fb730 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20    /* This mutex 
fb740 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  is needed becaus
fb750 65 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20  e pFile->pInode 
fb760 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
fb770 20 74 68 72 65 61 64 73 0d 0a 20 20 2a 2f 0d 0a   threads..  */..
fb780 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
fb790 28 29 3b 0d 0a 20 20 70 49 6e 6f 64 65 20 3d 20  ();..  pInode = 
fb7a0 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0d 0a  pFile->pInode;..
fb7b0 0d 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74  ..  /* If some t
fb7c0 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73  hread using this
fb7d0 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 6b 20   PID has a lock 
fb7e0 76 69 61 20 61 20 64 69 66 66 65 72 65 6e 74 20  via a different 
fb7f0 75 6e 69 78 46 69 6c 65 2a 0d 0a 20 20 2a 2a 20  unixFile*..  ** 
fb800 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 63  handle that prec
fb810 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 73  ludes the reques
fb820 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e  ted lock, return
fb830 20 42 55 53 59 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   BUSY...  */..  
fb840 69 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 6c  if( (pFile->eFil
fb850 65 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e 65  eLock!=pInode->e
fb860 46 69 6c 65 4c 6f 63 6b 20 26 26 20 0d 0a 20 20  FileLock && ..  
fb870 20 20 20 20 20 20 20 20 28 70 49 6e 6f 64 65 2d          (pInode-
fb880 3e 65 46 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e 44  >eFileLock>=PEND
fb890 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c  ING_LOCK || eFil
fb8a0 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43  eLock>SHARED_LOC
fb8b0 4b 29 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 72  K))..  ){..    r
fb8c0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
fb8d0 0d 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c  ..    goto end_l
fb8e0 6f 63 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  ock;..  }....  /
fb8f0 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f  * If a SHARED lo
fb900 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ck is requested,
fb910 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64   and some thread
fb920 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 20   using this PID 
fb930 61 6c 72 65 61 64 79 0d 0a 20 20 2a 2a 20 68 61  already..  ** ha
fb940 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45  s a SHARED or RE
fb950 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65  SERVED lock, the
fb960 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65  n increment refe
fb970 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64  rence counts and
fb980 0d 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51  ..  ** return SQ
fb990 4c 49 54 45 5f 4f 4b 2e 0d 0a 20 20 2a 2f 0d 0a  LITE_OK...  */..
fb9a0 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d    if( eFileLock=
fb9b0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
fb9c0 0d 0a 20 20 20 20 20 20 28 70 49 6e 6f 64 65 2d  ..      (pInode-
fb9d0 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52  >eFileLock==SHAR
fb9e0 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 6e 6f 64  ED_LOCK || pInod
fb9f0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45  e->eFileLock==RE
fba00 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0d  SERVED_LOCK) ){.
fba10 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 46 69  .    assert( eFi
fba20 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
fba30 4f 43 4b 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  OCK );..    asse
fba40 72 74 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  rt( pFile->eFile
fba50 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  Lock==0 );..    
fba60 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e  assert( pInode->
fba70 6e 53 68 61 72 65 64 3e 30 20 29 3b 0d 0a 20 20  nShared>0 );..  
fba80 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f    pFile->eFileLo
fba90 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  ck = SHARED_LOCK
fbaa0 3b 0d 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e  ;..    pInode->n
fbab0 53 68 61 72 65 64 2b 2b 3b 0d 0a 20 20 20 20 70  Shared++;..    p
fbac0 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0d  Inode->nLock++;.
fbad0 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f  .    goto end_lo
fbae0 63 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20 20  ck;..  }......  
fbaf0 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  /* A PENDING loc
fbb00 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f  k is needed befo
fbb10 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
fbb20 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62  HARED lock and b
fbb30 65 66 6f 72 65 0d 0a 20 20 2a 2a 20 61 63 71 75  efore..  ** acqu
fbb40 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49  iring an EXCLUSI
fbb50 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68  VE lock.  For th
fbb60 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  e SHARED lock, t
fbb70 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0d  he PENDING will.
fbb80 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65  .  ** be release
fbb90 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6c 6f 63 6b  d...  */..  lock
fbba0 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0d 0a 20 20  .l_len = 1L;..  
fbbb0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
fbbc0 53 45 45 4b 5f 53 45 54 3b 0d 0a 20 20 69 66 28  SEEK_SET;..  if(
fbbd0 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52   eFileLock==SHAR
fbbe0 45 44 5f 4c 4f 43 4b 20 0d 0a 20 20 20 20 20 20  ED_LOCK ..      
fbbf0 7c 7c 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45  || (eFileLock==E
fbc00 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
fbc10 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
fbc20 6b 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0d  k<PENDING_LOCK).
fbc30 0a 20 20 29 7b 0d 0a 20 20 20 20 6c 6f 63 6b 2e  .  ){..    lock.
fbc40 6c 5f 74 79 70 65 20 3d 20 28 65 46 69 6c 65 4c  l_type = (eFileL
fbc50 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ock==SHARED_LOCK
fbc60 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b  ?F_RDLCK:F_WRLCK
fbc70 29 3b 0d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73  );..    lock.l_s
fbc80 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  tart = PENDING_B
fbc90 59 54 45 3b 0d 0a 20 20 20 20 69 66 28 20 75 6e  YTE;..    if( un
fbca0 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65  ixFileLock(pFile
fbcb0 2c 20 26 6c 6f 63 6b 29 20 29 7b 0d 0a 20 20 20  , &lock) ){..   
fbcc0 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e     tErrno = errn
fbcd0 6f 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  o;..      rc = s
fbce0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
fbcf0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
fbd00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
fbd10 43 4b 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  CK);..      if( 
fbd20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc!=SQLITE_BUSY 
fbd30 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c  ){..        pFil
fbd40 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
fbd50 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 7d 0d  Errno;..      }.
fbd60 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
fbd70 6c 6f 63 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  lock;..    }..  
fbd80 7d 0d 0a 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 63  }......  /* If c
fbd90 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74  ontrol gets to t
fbda0 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20  his point, then 
fbdb0 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61  actually go ahea
fbdc0 64 20 61 6e 64 20 6d 61 6b 65 0d 0a 20 20 2a 2a  d and make..  **
fbdd0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
fbde0 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20  m calls for the 
fbdf0 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0d  specified lock..
fbe00 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 65 46 69  .  */..  if( eFi
fbe10 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
fbe20 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  OCK ){..    asse
fbe30 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61  rt( pInode->nSha
fbe40 72 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 61  red==0 );..    a
fbe50 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65  ssert( pInode->e
fbe60 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a  FileLock==0 );..
fbe70 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
fbe80 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 0d 0a  SQLITE_OK );....
fbe90 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74      /* Now get t
fbea0 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0d  he read-lock */.
fbeb0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
fbec0 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
fbed0 3b 0d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  ;..    lock.l_le
fbee0 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b  n = SHARED_SIZE;
fbef0 0d 0a 20 20 20 20 69 66 28 20 75 6e 69 78 46 69  ..    if( unixFi
fbf00 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c  leLock(pFile, &l
fbf10 6f 63 6b 29 20 29 7b 0d 0a 20 20 20 20 20 20 74  ock) ){..      t
fbf20 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a  Errno = errno;..
fbf30 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
fbf40 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
fbf50 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
fbf60 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
fbf70 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
fbf80 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f  * Drop the tempo
fbf90 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63  rary PENDING loc
fbfa0 6b 20 2a 2f 0d 0a 20 20 20 20 6c 6f 63 6b 2e 6c  k */..    lock.l
fbfb0 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47  _start = PENDING
fbfc0 5f 42 59 54 45 3b 0d 0a 20 20 20 20 6c 6f 63 6b  _BYTE;..    lock
fbfd0 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0d 0a 20 20  .l_len = 1L;..  
fbfe0 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
fbff0 46 5f 55 4e 4c 43 4b 3b 0d 0a 20 20 20 20 69 66  F_UNLCK;..    if
fc000 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70  ( unixFileLock(p
fc010 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 26 26 20  File, &lock) && 
fc020 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
fc030 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
fc040 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74  could happen wit
fc050 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e  h a network moun
fc060 74 20 2a 2f 0d 0a 20 20 20 20 20 20 74 45 72 72  t */..      tErr
fc070 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20  no = errno;..   
fc080 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
fc090 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 0d 0a 20  OERR_UNLOCK; .. 
fc0a0 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20     }....    if( 
fc0b0 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  rc ){..      if(
fc0c0 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc!=SQLITE_BUSY
fc0d0 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 69   ){..        pFi
fc0e0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
fc0f0 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 7d  tErrno;..      }
fc100 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  ..      goto end
fc110 5f 6c 6f 63 6b 3b 0d 0a 20 20 20 20 7d 65 6c 73  _lock;..    }els
fc120 65 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  e{..      pFile-
fc130 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41  >eFileLock = SHA
fc140 52 45 44 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20  RED_LOCK;..     
fc150 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b   pInode->nLock++
fc160 3b 0d 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 2d  ;..      pInode-
fc170 3e 6e 53 68 61 72 65 64 20 3d 20 31 3b 0d 0a 20  >nShared = 1;.. 
fc180 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 20 69 66     }..  }else if
fc190 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43  ( eFileLock==EXC
fc1a0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
fc1b0 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31  Inode->nShared>1
fc1c0 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 57 65 20 61   ){..    /* We a
fc1d0 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e  re trying for an
fc1e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
fc1f0 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65  but another thre
fc200 61 64 20 69 6e 20 74 68 69 73 0d 0a 20 20 20 20  ad in this..    
fc210 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  ** same process 
fc220 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67  is still holding
fc230 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20   a shared lock. 
fc240 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  */..    rc = SQL
fc250 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20 7d 65 6c  ITE_BUSY;..  }el
fc260 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20  se{..    /* The 
fc270 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20  request was for 
fc280 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58  a RESERVED or EX
fc290 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49  CLUSIVE lock.  I
fc2a0 74 20 69 73 0d 0a 20 20 20 20 2a 2a 20 61 73 73  t is..    ** ass
fc2b0 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20  umed that there 
fc2c0 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67  is a SHARED or g
fc2d0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74  reater lock on t
fc2e0 68 65 20 66 69 6c 65 0d 0a 20 20 20 20 2a 2a 20  he file..    ** 
fc2f0 61 6c 72 65 61 64 79 2e 0d 0a 20 20 20 20 2a 2f  already...    */
fc300 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21  ..    assert( 0!
fc310 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63  =pFile->eFileLoc
fc320 6b 20 29 3b 0d 0a 20 20 20 20 6c 6f 63 6b 2e 6c  k );..    lock.l
fc330 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b  _type = F_WRLCK;
fc340 0d 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ....    assert( 
fc350 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45 53 45 52  eFileLock==RESER
fc360 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c  VED_LOCK || eFil
fc370 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
fc380 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 20 20 69 66  _LOCK );..    if
fc390 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45 53  ( eFileLock==RES
fc3a0 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20  ERVED_LOCK ){.. 
fc3b0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72       lock.l_star
fc3c0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
fc3d0 45 3b 0d 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  E;..      lock.l
fc3e0 5f 6c 65 6e 20 3d 20 31 4c 3b 0d 0a 20 20 20 20  _len = 1L;..    
fc3f0 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6c 6f  }else{..      lo
fc400 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41  ck.l_start = SHA
fc410 52 45 44 5f 46 49 52 53 54 3b 0d 0a 20 20 20 20  RED_FIRST;..    
fc420 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53    lock.l_len = S
fc430 48 41 52 45 44 5f 53 49 5a 45 3b 0d 0a 20 20 20  HARED_SIZE;..   
fc440 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 75 6e   }....    if( un
fc450 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65  ixFileLock(pFile
fc460 2c 20 26 6c 6f 63 6b 29 20 29 7b 0d 0a 20 20 20  , &lock) ){..   
fc470 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e     tErrno = errn
fc480 6f 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  o;..      rc = s
fc490 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
fc4a0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
fc4b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
fc4c0 43 4b 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  CK);..      if( 
fc4d0 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc!=SQLITE_BUSY 
fc4e0 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c  ){..        pFil
fc4f0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
fc500 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 7d 0d  Errno;..      }.
fc510 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d  .    }..  }..  .
fc520 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ...#ifndef NDEBU
fc530 47 0d 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 74  G..  /* Set up t
fc540 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2d 63  he transaction-c
fc550 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 20 63 68  ounter change ch
fc560 65 63 6b 69 6e 67 20 66 6c 61 67 73 20 77 68 65  ecking flags whe
fc570 6e 0d 0a 20 20 2a 2a 20 74 72 61 6e 73 69 74 69  n..  ** transiti
fc580 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53 48 41  oning from a SHA
fc590 52 45 44 20 74 6f 20 61 20 52 45 53 45 52 56 45  RED to a RESERVE
fc5a0 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63 68 61  D lock.  The cha
fc5b0 6e 67 65 0d 0a 20 20 2a 2a 20 66 72 6f 6d 20 53  nge..  ** from S
fc5c0 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 45  HARED to RESERVE
fc5d0 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67 69  D marks the begi
fc5e0 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d 61  nning of a norma
fc5f0 6c 0d 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70  l..  ** write op
fc600 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68  eration (not a h
fc610 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot journal rollb
fc620 61 63 6b 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ack)...  */..  i
fc630 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
fc640 0d 0a 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 65  ..   && pFile->e
fc650 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44  FileLock<=SHARED
fc660 5f 4c 4f 43 4b 0d 0a 20 20 20 26 26 20 65 46 69  _LOCK..   && eFi
fc670 6c 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44  leLock==RESERVED
fc680 5f 4c 4f 43 4b 0d 0a 20 20 29 7b 0d 0a 20 20 20  _LOCK..  ){..   
fc690 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74   pFile->transCnt
fc6a0 72 43 68 6e 67 20 3d 20 30 3b 0d 0a 20 20 20 20  rChng = 0;..    
fc6b0 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20  pFile->dbUpdate 
fc6c0 3d 20 30 3b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  = 0;..    pFile-
fc6d0 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d  >inNormalWrite =
fc6e0 20 31 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66   1;..  }..#endif
fc6f0 0d 0a 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d  ......  if( rc==
fc700 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
fc710 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f    pFile->eFileLo
fc720 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0d  ck = eFileLock;.
fc730 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69  .    pInode->eFi
fc740 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f  leLock = eFileLo
fc750 63 6b 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  ck;..  }else if(
fc760 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c   eFileLock==EXCL
fc770 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0d 0a 20  USIVE_LOCK ){.. 
fc780 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c     pFile->eFileL
fc790 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ock = PENDING_LO
fc7a0 43 4b 3b 0d 0a 20 20 20 20 70 49 6e 6f 64 65 2d  CK;..    pInode-
fc7b0 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 50 45 4e  >eFileLock = PEN
fc7c0 44 49 4e 47 5f 4c 4f 43 4b 3b 0d 0a 20 20 7d 0d  DING_LOCK;..  }.
fc7d0 0a 0d 0a 65 6e 64 5f 6c 6f 63 6b 3a 0d 0a 20 20  ...end_lock:..  
fc7e0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
fc7f0 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c  ;..  OSTRACE(("L
fc800 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 20  OCK    %d %s %s 
fc810 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65  (unix)\n", pFile
fc820 2d 3e 68 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28  ->h, azFileLock(
fc830 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 0d 0a 20 20  eFileLock), ..  
fc840 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
fc850 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
fc860 65 64 22 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e  ed"));..  return
fc870 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
fc880 2a 20 41 64 64 20 74 68 65 20 66 69 6c 65 20 64  * Add the file d
fc890 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 62  escriptor used b
fc8a0 79 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70 46  y file handle pF
fc8b0 69 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72 65  ile to the corre
fc8c0 73 70 6f 6e 64 69 6e 67 0d 0a 2a 2a 20 70 55 6e  sponding..** pUn
fc8d0 75 73 65 64 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a  used list...*/..
fc8e0 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 50  static void setP
fc8f0 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 46 69 6c  endingFd(unixFil
fc900 65 20 2a 70 46 69 6c 65 29 7b 0d 0a 20 20 75 6e  e *pFile){..  un
fc910 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e  ixInodeInfo *pIn
fc920 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e  ode = pFile->pIn
fc930 6f 64 65 3b 0d 0a 20 20 55 6e 69 78 55 6e 75 73  ode;..  UnixUnus
fc940 65 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d  edFd *p = pFile-
fc950 3e 70 55 6e 75 73 65 64 3b 0d 0a 20 20 70 2d 3e  >pUnused;..  p->
fc960 70 4e 65 78 74 20 3d 20 70 49 6e 6f 64 65 2d 3e  pNext = pInode->
fc970 70 55 6e 75 73 65 64 3b 0d 0a 20 20 70 49 6e 6f  pUnused;..  pIno
fc980 64 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b  de->pUnused = p;
fc990 0d 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d  ..  pFile->h = -
fc9a0 31 3b 0d 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e  1;..  pFile->pUn
fc9b0 75 73 65 64 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a  used = 0;..}....
fc9c0 2f 2a 0d 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  /*..** Lower the
fc9d0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
fc9e0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
fc9f0 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65  r pFile to eFile
fca00 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b  Lock.  eFileLock
fca10 0d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  ..** must be eit
fca20 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
fca30 48 41 52 45 44 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d  HARED_LOCK...**.
fca40 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
fca50 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
fca60 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
fca70 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
fca80 20 62 65 6c 6f 77 0d 0a 2a 2a 20 74 68 65 20 72   below..** the r
fca90 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67  equested locking
fcaa0 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75   level, this rou
fcab0 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
fcac0 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49 66 20 68 61 6e  ..** ..** If han
fcad0 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 20 69 73 20  dleNFSUnlock is 
fcae0 74 72 75 65 2c 20 74 68 65 6e 20 6f 6e 20 64 6f  true, then on do
fcaf0 77 6e 67 72 61 64 69 6e 67 20 61 6e 20 45 58 43  wngrading an EXC
fcb00 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 74 6f 20 53  LUSIVE_LOCK to S
fcb10 48 41 52 45 44 0d 0a 2a 2a 20 74 68 65 20 62 79  HARED..** the by
fcb20 74 65 20 72 61 6e 67 65 20 69 73 20 64 69 76 69  te range is divi
fcb30 64 65 64 20 69 6e 74 6f 20 32 20 70 61 72 74 73  ded into 2 parts
fcb40 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 70   and the first p
fcb50 61 72 74 20 69 73 20 75 6e 6c 6f 63 6b 65 64 20  art is unlocked 
fcb60 74 68 65 6e 0d 0a 2a 2a 20 73 65 74 20 74 6f 20  then..** set to 
fcb70 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 74 68 65  a read lock, the
fcb80 6e 20 74 68 65 20 6f 74 68 65 72 20 70 61 72 74  n the other part
fcb90 20 69 73 20 73 69 6d 70 6c 79 20 75 6e 6c 6f 63   is simply unloc
fcba0 6b 65 64 2e 20 20 54 68 69 73 20 77 6f 72 6b 73  ked.  This works
fcbb0 20 0d 0a 2a 2a 20 61 72 6f 75 6e 64 20 61 20 62   ..** around a b
fcbc0 75 67 20 69 6e 20 42 53 44 20 4e 46 53 20 6c 6f  ug in BSD NFS lo
fcbd0 63 6b 64 20 28 61 6c 73 6f 20 73 65 65 6e 20 6f  ckd (also seen o
fcbe0 6e 20 4d 61 63 4f 53 58 20 31 30 2e 33 2b 29 20  n MacOSX 10.3+) 
fcbf0 74 68 61 74 20 66 61 69 6c 73 20 74 6f 20 0d 0a  that fails to ..
fcc00 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 77 72  ** remove the wr
fcc10 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 61 20 72 65  ite lock on a re
fcc20 67 69 6f 6e 20 77 68 65 6e 20 61 20 72 65 61 64  gion when a read
fcc30 20 6c 6f 63 6b 20 69 73 20 73 65 74 2e 0d 0a 2a   lock is set...*
fcc40 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  /..static int po
fcc50 73 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  sixUnlock(sqlite
fcc60 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fcc70 65 46 69 6c 65 4c 6f 63 6b 2c 20 69 6e 74 20 68  eFileLock, int h
fcc80 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 29 7b  andleNFSUnlock){
fcc90 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ..  unixFile *pF
fcca0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
fccb0 29 69 64 3b 0d 0a 20 20 75 6e 69 78 49 6e 6f 64  )id;..  unixInod
fccc0 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b 0d 0a  eInfo *pInode;..
fccd0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
fcce0 6f 63 6b 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  ock;..  int rc =
fccf0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20   SQLITE_OK;.... 
fcd00 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
fcd10 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 55  ;..  OSTRACE(("U
fcd20 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
fcd30 20 25 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25   %d(%d,%d) pid=%
fcd40 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69  d (unix)\n", pFi
fcd50 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 4c 6f 63 6b  le->h, eFileLock
fcd60 2c 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ,..      pFile->
fcd70 65 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65  eFileLock, pFile
fcd80 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c  ->pInode->eFileL
fcd90 6f 63 6b 2c 20 70 46 69 6c 65 2d 3e 70 49 6e 6f  ock, pFile->pIno
fcda0 64 65 2d 3e 6e 53 68 61 72 65 64 2c 0d 0a 20 20  de->nShared,..  
fcdb0 20 20 20 20 67 65 74 70 69 64 28 29 29 29 3b 0d      getpid()));.
fcdc0 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 65 46 69  ...  assert( eFi
fcdd0 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c  leLock<=SHARED_L
fcde0 4f 43 4b 20 29 3b 0d 0a 20 20 69 66 28 20 70 46  OCK );..  if( pF
fcdf0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d  ile->eFileLock<=
fce00 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d 0a 20 20  eFileLock ){..  
fce10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fce20 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 75 6e 69 78  OK;..  }..  unix
fce30 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20  EnterMutex();.. 
fce40 20 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d   pInode = pFile-
fce50 3e 70 49 6e 6f 64 65 3b 0d 0a 20 20 61 73 73 65  >pInode;..  asse
fce60 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61  rt( pInode->nSha
fce70 72 65 64 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28  red!=0 );..  if(
fce80 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
fce90 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  k>SHARED_LOCK ){
fcea0 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ..    assert( pI
fceb0 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d  node->eFileLock=
fcec0 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63  =pFile->eFileLoc
fced0 6b 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  k );....#ifndef 
fcee0 4e 44 45 42 55 47 0d 0a 20 20 20 20 2f 2a 20 57  NDEBUG..    /* W
fcef0 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 6c  hen reducing a l
fcf00 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f 74  ock such that ot
fcf10 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 61  her processes ca
fcf20 6e 20 73 74 61 72 74 0d 0a 20 20 20 20 2a 2a 20  n start..    ** 
fcf30 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
fcf40 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c  base file again,
fcf50 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
fcf60 74 68 65 0d 0a 20 20 20 20 2a 2a 20 74 72 61 6e  the..    ** tran
fcf70 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20  saction counter 
fcf80 77 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61  was updated if a
fcf90 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  ny part of the d
fcfa0 61 74 61 62 61 73 65 0d 0a 20 20 20 20 2a 2a 20  atabase..    ** 
fcfb0 66 69 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49  file changed.  I
fcfc0 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
fcfd0 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74  n counter is not
fcfe0 20 75 70 64 61 74 65 64 2c 0d 0a 20 20 20 20 2a   updated,..    *
fcff0 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  * other connecti
fd000 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ons to the same 
fd010 66 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74 20 72  file might not r
fd020 65 61 6c 69 7a 65 20 74 68 61 74 0d 0a 20 20 20  ealize that..   
fd030 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 68 61 73   ** the file has
fd040 20 63 68 61 6e 67 65 64 20 61 6e 64 20 68 65 6e   changed and hen
fd050 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e 6f  ce might not kno
fd060 77 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72  w to flush their
fd070 0d 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20  ..    ** cache. 
fd080 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 73 74   The use of a st
fd090 61 6c 65 20 63 61 63 68 65 20 63 61 6e 20 6c 65  ale cache can le
fd0a0 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63  ad to database c
fd0b0 6f 72 72 75 70 74 69 6f 6e 2e 0d 0a 20 20 20 20  orruption...    
fd0c0 2a 2f 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69  */..    pFile->i
fd0d0 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30  nNormalWrite = 0
fd0e0 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20  ;..#endif....   
fd0f0 20 2f 2a 20 64 6f 77 6e 67 72 61 64 69 6e 67 20   /* downgrading 
fd100 74 6f 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  to a shared lock
fd110 20 6f 6e 20 4e 46 53 20 69 6e 76 6f 6c 76 65 73   on NFS involves
fd120 20 63 6c 65 61 72 69 6e 67 20 74 68 65 20 77 72   clearing the wr
fd130 69 74 65 20 6c 6f 63 6b 0d 0a 20 20 20 20 2a 2a  ite lock..    **
fd140 20 62 65 66 6f 72 65 20 65 73 74 61 62 6c 69 73   before establis
fd150 68 69 6e 67 20 74 68 65 20 72 65 61 64 6c 6f 63  hing the readloc
fd160 6b 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 72  k - to avoid a r
fd170 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 77 65  ace condition we
fd180 20 64 6f 77 6e 67 72 61 64 65 0d 0a 20 20 20 20   downgrade..    
fd190 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 69 6e 20 32  ** the lock in 2
fd1a0 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 61 74   blocks, so that
fd1b0 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 61 6e   part of the ran
fd1c0 67 65 20 77 69 6c 6c 20 62 65 20 63 6f 76 65 72  ge will be cover
fd1d0 65 64 20 62 79 20 61 20 0d 0a 20 20 20 20 2a 2a  ed by a ..    **
fd1e0 20 77 72 69 74 65 20 6c 6f 63 6b 20 75 6e 74 69   write lock unti
fd1f0 6c 20 74 68 65 20 72 65 73 74 20 69 73 20 63 6f  l the rest is co
fd200 76 65 72 65 64 20 62 79 20 61 20 72 65 61 64 20  vered by a read 
fd210 6c 6f 63 6b 3a 0d 0a 20 20 20 20 2a 2a 20 20 31  lock:..    **  1
fd220 3a 20 20 20 5b 57 57 57 57 57 5d 0d 0a 20 20 20  :   [WWWWW]..   
fd230 20 2a 2a 20 20 32 3a 20 20 20 5b 2e 2e 2e 2e 57   **  2:   [....W
fd240 5d 0d 0a 20 20 20 20 2a 2a 20 20 33 3a 20 20 20  ]..    **  3:   
fd250 5b 52 52 52 52 57 5d 0d 0a 20 20 20 20 2a 2a 20  [RRRRW]..    ** 
fd260 20 34 3a 20 20 20 5b 52 52 52 52 2e 5d 0d 0a 20   4:   [RRRR.].. 
fd270 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 65     */..    if( e
fd280 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44  FileLock==SHARED
fd290 5f 4c 4f 43 4b 20 29 7b 0d 0a 0d 0a 23 69 66 20  _LOCK ){....#if 
fd2a0 21 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45  !defined(__APPLE
fd2b0 5f 5f 29 20 7c 7c 20 21 53 51 4c 49 54 45 5f 45  __) || !SQLITE_E
fd2c0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
fd2d0 59 4c 45 0d 0a 20 20 20 20 20 20 28 76 6f 69 64  YLE..      (void
fd2e0 29 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b  )handleNFSUnlock
fd2f0 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
fd300 20 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b   handleNFSUnlock
fd310 3d 3d 30 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ==0 );..#endif..
fd320 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
fd330 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
fd340 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
fd350 53 54 59 4c 45 0d 0a 20 20 20 20 20 20 69 66 28  STYLE..      if(
fd360 20 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b   handleNFSUnlock
fd370 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74   ){..        int
fd380 20 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20   tErrno;        
fd390 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
fd3a0 63 6f 64 65 20 66 72 6f 6d 20 73 79 73 74 65 6d  code from system
fd3b0 20 63 61 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0d   call errors */.
fd3c0 0a 20 20 20 20 20 20 20 20 6f 66 66 5f 74 20 64  .        off_t d
fd3d0 69 76 53 69 7a 65 20 3d 20 53 48 41 52 45 44 5f  ivSize = SHARED_
fd3e0 53 49 5a 45 20 2d 20 31 3b 0d 0a 20 20 20 20 20  SIZE - 1;..     
fd3f0 20 20 20 0d 0a 20 20 20 20 20 20 20 20 6c 6f 63     ..        loc
fd400 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c  k.l_type = F_UNL
fd410 43 4b 3b 0d 0a 20 20 20 20 20 20 20 20 6c 6f 63  CK;..        loc
fd420 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
fd430 4b 5f 53 45 54 3b 0d 0a 20 20 20 20 20 20 20 20  K_SET;..        
fd440 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53  lock.l_start = S
fd450 48 41 52 45 44 5f 46 49 52 53 54 3b 0d 0a 20 20  HARED_FIRST;..  
fd460 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e        lock.l_len
fd470 20 3d 20 64 69 76 53 69 7a 65 3b 0d 0a 20 20 20   = divSize;..   
fd480 20 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c       if( unixFil
fd490 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f  eLock(pFile, &lo
fd4a0 63 6b 29 3d 3d 28 2d 31 29 20 29 7b 0d 0a 20 20  ck)==(-1) ){..  
fd4b0 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d          tErrno =
fd4c0 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20   errno;..       
fd4d0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
fd4e0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0d 0a 20 20  OERR_UNLOCK;..  
fd4f0 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c          if( IS_L
fd500 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
fd510 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46  ..            pF
fd520 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
fd530 20 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20   tErrno;..      
fd540 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
fd550 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b   goto end_unlock
fd560 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
fd570 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70        lock.l_typ
fd580 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0d 0a 20 20  e = F_RDLCK;..  
fd590 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65        lock.l_whe
fd5a0 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0d  nce = SEEK_SET;.
fd5b0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
fd5c0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
fd5d0 49 52 53 54 3b 0d 0a 20 20 20 20 20 20 20 20 6c  IRST;..        l
fd5e0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 64 69 76 53  ock.l_len = divS
fd5f0 69 7a 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ize;..        if
fd600 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70  ( unixFileLock(p
fd610 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d  File, &lock)==(-
fd620 31 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  1) ){..         
fd630 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
fd640 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ..          rc =
fd650 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
fd660 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
fd670 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
fd680 52 44 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 20 20  RDLOCK);..      
fd690 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
fd6a0 45 52 52 4f 52 28 72 63 29 20 29 7b 0d 0a 20 20  ERROR(rc) ){..  
fd6b0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
fd6c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
fd6d0 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  rno;..          
fd6e0 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  }..          got
fd6f0 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0d 0a 20  o end_unlock;.. 
fd700 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
fd710 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
fd720 46 5f 55 4e 4c 43 4b 3b 0d 0a 20 20 20 20 20 20  F_UNLCK;..      
fd730 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
fd740 3d 20 53 45 45 4b 5f 53 45 54 3b 0d 0a 20 20 20  = SEEK_SET;..   
fd750 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72       lock.l_star
fd760 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
fd770 2b 64 69 76 53 69 7a 65 3b 0d 0a 20 20 20 20 20  +divSize;..     
fd780 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
fd790 53 48 41 52 45 44 5f 53 49 5a 45 2d 64 69 76 53  SHARED_SIZE-divS
fd7a0 69 7a 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ize;..        if
fd7b0 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70  ( unixFileLock(p
fd7c0 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d  File, &lock)==(-
fd7d0 31 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  1) ){..         
fd7e0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
fd7f0 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ..          rc =
fd800 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
fd810 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20 20 20 20 20  LOCK;..         
fd820 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
fd830 4f 52 28 72 63 29 20 29 7b 0d 0a 20 20 20 20 20  OR(rc) ){..     
fd840 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
fd850 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
fd860 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
fd870 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
fd880 6e 64 5f 75 6e 6c 6f 63 6b 3b 0d 0a 20 20 20 20  nd_unlock;..    
fd890 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c      }..      }el
fd8a0 73 65 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  se..#endif /* de
fd8b0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
fd8c0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
fd8d0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
fd8e0 2a 2f 0d 0a 20 20 20 20 20 20 7b 0d 0a 20 20 20  */..      {..   
fd8f0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
fd900 20 3d 20 46 5f 52 44 4c 43 4b 3b 0d 0a 20 20 20   = F_RDLCK;..   
fd910 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e       lock.l_when
fd920 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0d 0a  ce = SEEK_SET;..
fd930 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73          lock.l_s
fd940 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49  tart = SHARED_FI
fd950 52 53 54 3b 0d 0a 20 20 20 20 20 20 20 20 6c 6f  RST;..        lo
fd960 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45  ck.l_len = SHARE
fd970 44 5f 53 49 5a 45 3b 0d 0a 20 20 20 20 20 20 20  D_SIZE;..       
fd980 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63   if( unixFileLoc
fd990 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20  k(pFile, &lock) 
fd9a0 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ){..          /*
fd9b0 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20   In theory, the 
fd9c0 63 61 6c 6c 20 74 6f 20 75 6e 69 78 46 69 6c 65  call to unixFile
fd9d0 4c 6f 63 6b 28 29 20 63 61 6e 6e 6f 74 20 66 61  Lock() cannot fa
fd9e0 69 6c 20 62 65 63 61 75 73 65 20 61 6e 6f 74 68  il because anoth
fd9f0 65 72 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  er..          **
fda00 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
fda10 69 6e 67 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69  ing an incompati
fda20 62 6c 65 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20  ble lock. If it 
fda30 64 6f 65 73 2c 20 74 68 69 73 20 0d 0a 20 20 20  does, this ..   
fda40 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 69 63 61         ** indica
fda50 74 65 73 20 74 68 61 74 20 74 68 65 20 6f 74 68  tes that the oth
fda60 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 6e 6f  er process is no
fda70 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  t following the 
fda80 6c 6f 63 6b 69 6e 67 0d 0a 20 20 20 20 20 20 20  locking..       
fda90 20 20 20 2a 2a 20 70 72 6f 74 6f 63 6f 6c 2e 20     ** protocol. 
fdaa0 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c  If this happens,
fdab0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fdac0 4f 45 52 52 5f 52 44 4c 4f 43 4b 2e 20 52 65 74  OERR_RDLOCK. Ret
fdad0 75 72 6e 69 6e 67 0d 0a 20 20 20 20 20 20 20 20  urning..        
fdae0 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59    ** SQLITE_BUSY
fdaf0 20 77 6f 75 6c 64 20 63 6f 6e 66 75 73 65 20 74   would confuse t
fdb00 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 28  he upper layer (
fdb10 69 6e 20 70 72 61 63 74 69 63 65 20 69 74 20 63  in practice it c
fdb20 61 75 73 65 73 20 0d 0a 20 20 20 20 20 20 20 20  auses ..        
fdb30 20 20 2a 2a 20 61 6e 20 61 73 73 65 72 74 20 74    ** an assert t
fdb40 6f 20 66 61 69 6c 29 2e 20 2a 2f 20 0d 0a 20 20  o fail). */ ..  
fdb50 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
fdb60 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b  ITE_IOERR_RDLOCK
fdb70 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ;..          pFi
fdb80 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
fdb90 65 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20 20  errno;..        
fdba0 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
fdbb0 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  k;..        }.. 
fdbc0 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
fdbd0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
fdbe0 20 46 5f 55 4e 4c 43 4b 3b 0d 0a 20 20 20 20 6c   F_UNLCK;..    l
fdbf0 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  ock.l_whence = S
fdc00 45 45 4b 5f 53 45 54 3b 0d 0a 20 20 20 20 6c 6f  EEK_SET;..    lo
fdc10 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
fdc20 44 49 4e 47 5f 42 59 54 45 3b 0d 0a 20 20 20 20  DING_BYTE;..    
fdc30 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b  lock.l_len = 2L;
fdc40 20 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e    assert( PENDIN
fdc50 47 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56  G_BYTE+1==RESERV
fdc60 45 44 5f 42 59 54 45 20 29 3b 0d 0a 20 20 20 20  ED_BYTE );..    
fdc70 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b  if( unixFileLock
fdc80 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d  (pFile, &lock)==
fdc90 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 49 6e 6f  0 ){..      pIno
fdca0 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  de->eFileLock = 
fdcb0 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0d 0a 20 20  SHARED_LOCK;..  
fdcc0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
fdcd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
fdce0 52 5f 55 4e 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20  R_UNLOCK;..     
fdcf0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fdd00 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20  o = errno;..    
fdd10 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
fdd20 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  k;..    }..  }..
fdd30 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d    if( eFileLock=
fdd40 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20  =NO_LOCK ){..   
fdd50 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68   /* Decrement th
fdd60 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f  e shared lock co
fdd70 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20  unter.  Release 
fdd80 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61  the lock using a
fdd90 6e 0d 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c  n..    ** OS cal
fdda0 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20  l only when all 
fddb0 74 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20  threads in this 
fddc0 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76  same process hav
fddd0 65 20 72 65 6c 65 61 73 65 64 0d 0a 20 20 20 20  e released..    
fdde0 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0d 0a 20 20  ** the lock...  
fddf0 20 20 2a 2f 0d 0a 20 20 20 20 70 49 6e 6f 64 65    */..    pInode
fde00 2d 3e 6e 53 68 61 72 65 64 2d 2d 3b 0d 0a 20 20  ->nShared--;..  
fde10 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 53    if( pInode->nS
fde20 68 61 72 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20  hared==0 ){..   
fde30 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
fde40 20 46 5f 55 4e 4c 43 4b 3b 0d 0a 20 20 20 20 20   F_UNLCK;..     
fde50 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
fde60 20 53 45 45 4b 5f 53 45 54 3b 0d 0a 20 20 20 20   SEEK_SET;..    
fde70 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
fde80 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c   lock.l_len = 0L
fde90 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 75 6e 69  ;..      if( uni
fdea0 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  xFileLock(pFile,
fdeb0 20 26 6c 6f 63 6b 29 3d 3d 30 20 29 7b 0d 0a 20   &lock)==0 ){.. 
fdec0 20 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65         pInode->e
fded0 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f  FileLock = NO_LO
fdee0 43 4b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  CK;..      }else
fdef0 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
fdf00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
fdf10 4f 43 4b 3b 0d 0a 09 70 46 69 6c 65 2d 3e 6c 61  OCK;...pFile->la
fdf20 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
fdf30 0d 0a 20 20 20 20 20 20 20 20 70 49 6e 6f 64 65  ..        pInode
fdf40 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f  ->eFileLock = NO
fdf50 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20 20 20 20  _LOCK;..        
fdf60 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
fdf70 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0d 0a 20 20 20   = NO_LOCK;..   
fdf80 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20     }..    }.... 
fdf90 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20     /* Decrement 
fdfa0 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63  the count of loc
fdfb0 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69 73 20  ks against this 
fdfc0 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e  same file.  When
fdfd0 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 63 6f 75   the..    ** cou
fdfe0 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c  nt reaches zero,
fdff0 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 72   close any other
fe000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
fe010 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0d 0a 20  s whose close.. 
fe020 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72     ** was deferr
fe030 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75  ed because of ou
fe040 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e  tstanding locks.
fe050 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 70 49  ..    */..    pI
fe060 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0d 0a  node->nLock--;..
fe070 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f      assert( pIno
fe080 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0d  de->nLock>=0 );.
fe090 0a 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d  .    if( pInode-
fe0a0 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0d 0a 20 20  >nLock==0 ){..  
fe0b0 20 20 20 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67      closePending
fe0c0 46 64 73 28 70 46 69 6c 65 29 3b 0d 0a 20 20 20  Fds(pFile);..   
fe0d0 20 7d 0d 0a 20 20 7d 0d 0a 09 0d 0a 65 6e 64 5f   }..  }.....end_
fe0e0 75 6e 6c 6f 63 6b 3a 0d 0a 20 20 75 6e 69 78 4c  unlock:..  unixL
fe0f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 20 20  eaveMutex();..  
fe100 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
fe110 4b 20 29 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  K ) pFile->eFile
fe120 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b  Lock = eFileLock
fe130 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
fe140 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 77  .}..../*..** Low
fe150 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
fe160 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
fe170 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
fe180 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69   eFileLock.  eFi
fe190 6c 65 4c 6f 63 6b 0d 0a 2a 2a 20 6d 75 73 74 20  leLock..** must 
fe1a0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
fe1b0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
fe1c0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
fe1d0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
fe1e0 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
fe1f0 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
fe200 20 61 74 20 6f 72 20 62 65 6c 6f 77 0d 0a 2a 2a   at or below..**
fe210 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
fe220 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
fe230 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
fe240 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  no-op...*/..stat
fe250 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63  ic int unixUnloc
fe260 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
fe270 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63  id, int eFileLoc
fe280 6b 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 6f  k){..  return po
fe290 73 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46  sixUnlock(id, eF
fe2a0 69 6c 65 4c 6f 63 6b 2c 20 30 29 3b 0d 0a 7d 0d  ileLock, 0);..}.
fe2b0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
fe2c0 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73  unction performs
fe2d0 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68   the parts of th
fe2e0 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f  e "close file" o
fe2f0 70 65 72 61 74 69 6f 6e 20 0d 0a 2a 2a 20 63 6f  peration ..** co
fe300 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b  mmon to all lock
fe310 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20  ing schemes. It 
fe320 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63  closes the direc
fe330 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0d 0a 2a  tory and file..*
fe340 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68  * handles, if th
fe350 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e  ey are valid, an
fe360 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64  d sets all field
fe370 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
fe380 65 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  e..** structure 
fe390 74 6f 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74  to 0...**..** It
fe3a0 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73   is *not* necess
fe3b0 61 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ary to hold the 
fe3c0 6d 75 74 65 78 20 77 68 65 6e 20 74 68 69 73 20  mutex when this 
fe3d0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
fe3e0 64 2c 0d 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56  d,..** even on V
fe3f0 78 57 6f 72 6b 73 2e 20 20 41 20 6d 75 74 65 78  xWorks.  A mutex
fe400 20 77 69 6c 6c 20 62 65 20 61 63 71 75 69 72 65   will be acquire
fe410 64 20 6f 6e 20 56 78 57 6f 72 6b 73 20 62 79 20  d on VxWorks by 
fe420 74 68 65 0d 0a 2a 2a 20 76 78 77 6f 72 6b 73 52  the..** vxworksR
fe430 65 6c 65 61 73 65 46 69 6c 65 49 64 28 29 20 72  eleaseFileId() r
fe440 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  outine...*/..sta
fe450 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e 69  tic int closeUni
fe460 78 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69  xFile(sqlite3_fi
fe470 6c 65 20 2a 69 64 29 7b 0d 0a 20 20 75 6e 69 78  le *id){..  unix
fe480 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
fe490 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20  nixFile*)id;..  
fe4a0 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20  if( pFile->h>=0 
fe4b0 29 7b 0d 0a 20 20 20 20 72 6f 62 75 73 74 5f 63  ){..    robust_c
fe4c0 6c 6f 73 65 28 70 46 69 6c 65 2c 20 70 46 69 6c  lose(pFile, pFil
fe4d0 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  e->h, __LINE__);
fe4e0 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d  ..    pFile->h =
fe4f0 20 2d 31 3b 0d 0a 20 20 7d 0d 0a 23 69 66 20 4f   -1;..  }..#if O
fe500 53 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 69 66 28  S_VXWORKS..  if(
fe510 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0d 0a   pFile->pId ){..
fe520 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 63      if( pFile->c
fe530 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46  trlFlags & UNIXF
fe540 49 4c 45 5f 44 45 4c 45 54 45 20 29 7b 0d 0a 20  ILE_DELETE ){.. 
fe550 20 20 20 20 20 6f 73 55 6e 6c 69 6e 6b 28 70 46       osUnlink(pF
fe560 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e  ile->pId->zCanon
fe570 69 63 61 6c 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  icalName);..    
fe580 7d 0d 0a 20 20 20 20 76 78 77 6f 72 6b 73 52 65  }..    vxworksRe
fe590 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 6c  leaseFileId(pFil
fe5a0 65 2d 3e 70 49 64 29 3b 0d 0a 20 20 20 20 70 46  e->pId);..    pF
fe5b0 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0d 0a 20  ile->pId = 0;.. 
fe5c0 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 4f 53   }..#endif..  OS
fe5d0 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 20 20  TRACE(("CLOSE   
fe5e0 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  %-3d\n", pFile->
fe5f0 68 29 29 3b 0d 0a 20 20 4f 70 65 6e 43 6f 75 6e  h));..  OpenCoun
fe600 74 65 72 28 2d 31 29 3b 0d 0a 20 20 73 71 6c 69  ter(-1);..  sqli
fe610 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
fe620 70 55 6e 75 73 65 64 29 3b 0d 0a 20 20 6d 65 6d  pUnused);..  mem
fe630 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69  set(pFile, 0, si
fe640 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b  zeof(unixFile));
fe650 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
fe660 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
fe670 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
fe680 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
fe690 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74   unixClose(sqlit
fe6a0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0d 0a 20  e3_file *id){.. 
fe6b0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
fe6c0 5f 4f 4b 3b 0d 0a 20 20 75 6e 69 78 46 69 6c 65  _OK;..  unixFile
fe6d0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
fe6e0 69 6c 65 20 2a 29 69 64 3b 0d 0a 20 20 75 6e 69  ile *)id;..  uni
fe6f0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c  xUnlock(id, NO_L
fe700 4f 43 4b 29 3b 0d 0a 20 20 75 6e 69 78 45 6e 74  OCK);..  unixEnt
fe710 65 72 4d 75 74 65 78 28 29 3b 0d 0a 0d 0a 20 20  erMutex();....  
fe720 2f 2a 20 75 6e 69 78 46 69 6c 65 2e 70 49 6e 6f  /* unixFile.pIno
fe730 64 65 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c  de is always val
fe740 69 64 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69  id here. Otherwi
fe750 73 65 2c 20 61 20 64 69 66 66 65 72 65 6e 74 20  se, a different 
fe760 63 6c 6f 73 65 0d 0a 20 20 2a 2a 20 72 6f 75 74  close..  ** rout
fe770 69 6e 65 20 28 65 2e 67 2e 20 6e 6f 6c 6f 63 6b  ine (e.g. nolock
fe780 43 6c 6f 73 65 28 29 29 20 77 6f 75 6c 64 20 62  Close()) would b
fe790 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64  e called instead
fe7a0 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72  ...  */..  asser
fe7b0 74 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  t( pFile->pInode
fe7c0 2d 3e 6e 4c 6f 63 6b 3e 30 20 7c 7c 20 70 46 69  ->nLock>0 || pFi
fe7d0 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 6f  le->pInode->bPro
fe7e0 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a  cessLock==0 );..
fe7f0 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 46 69    if( ALWAYS(pFi
fe800 6c 65 2d 3e 70 49 6e 6f 64 65 29 20 26 26 20 70  le->pInode) && p
fe810 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c  File->pInode->nL
fe820 6f 63 6b 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  ock ){..    /* I
fe830 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  f there are outs
fe840 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64  tanding locks, d
fe850 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63  o not actually c
fe860 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75  lose the file ju
fe870 73 74 0d 0a 20 20 20 20 2a 2a 20 79 65 74 20 62  st..    ** yet b
fe880 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c  ecause that woul
fe890 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f  d clear those lo
fe8a0 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61  cks.  Instead, a
fe8b0 64 64 20 74 68 65 20 66 69 6c 65 0d 0a 20 20 20  dd the file..   
fe8c0 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74   ** descriptor t
fe8d0 6f 20 70 49 6e 6f 64 65 2d 3e 70 55 6e 75 73 65  o pInode->pUnuse
fe8e0 64 20 6c 69 73 74 2e 20 20 49 74 20 77 69 6c 6c  d list.  It will
fe8f0 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
fe900 79 20 63 6c 6f 73 65 64 20 0d 0a 20 20 20 20 2a  y closed ..    *
fe910 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  * when the last 
fe920 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e  lock is cleared.
fe930 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 65  ..    */..    se
fe940 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c 65  tPendingFd(pFile
fe950 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 6c 65 61  );..  }..  relea
fe960 73 65 49 6e 6f 64 65 49 6e 66 6f 28 70 46 69 6c  seInodeInfo(pFil
fe970 65 29 3b 0d 0a 20 20 72 63 20 3d 20 63 6c 6f 73  e);..  rc = clos
fe980 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0d 0a  eUnixFile(id);..
fe990 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
fe9a0 28 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  ();..  return rc
fe9b0 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ;..}..../*******
fe9c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
fe9d0 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72  he posix advisor
fe9e0 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  y lock implement
fe9f0 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
fea00 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  *******..*******
fea10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea50 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a  *******/..../***
fea60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
feaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a  ***********..***
feab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
feac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f  *********** No-o
fead0 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  p Locking ******
feae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
feaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
feb00 0d 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69  ..** Of the vari
feb10 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c  ous locking impl
feb20 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69  ementations avai
feb30 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62  lable, this is b
feb40 79 20 66 61 72 20 74 68 65 0d 0a 2a 2a 20 73 69  y far the..** si
feb50 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67  mplest:  locking
feb60 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f   is ignored.  No
feb70 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
feb80 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74   to lock the dat
feb90 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 20 66  abase..** file f
feba0 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  or reading or wr
febb0 69 74 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  iting...**..** T
febc0 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65  his locking mode
febd0 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
febe0 66 6f 72 20 75 73 65 20 6f 6e 20 72 65 61 64 2d  for use on read-
febf0 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 0d 0a  only databases..
fec00 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61 73 65  ** (ex: database
fec10 73 20 74 68 61 74 20 61 72 65 20 62 75 72 6e 65  s that are burne
fec20 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66  d into CD-ROM, f
fec30 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20 49 74  or example.)  It
fec40 20 63 61 6e 0d 0a 2a 2a 20 61 6c 73 6f 20 62 65   can..** also be
fec50 20 75 73 65 64 20 69 66 20 74 68 65 20 61 70 70   used if the app
fec60 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73  lication employs
fec70 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d   some external m
fec80 65 63 68 61 6e 69 73 6d 20 74 6f 0d 0a 2a 2a 20  echanism to..** 
fec90 70 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e  prevent simultan
feca0 65 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 74  eous access of t
fecb0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
fecc0 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0d   by two or more.
fecd0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
fece0 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74  nections.  But t
fecf0 68 65 72 65 20 69 73 20 61 20 73 65 72 69 6f 75  here is a seriou
fed00 73 20 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61  s risk of databa
fed10 73 65 0d 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  se..** corruptio
fed20 6e 20 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e  n if this lockin
fed30 67 20 6d 6f 64 65 20 69 73 20 75 73 65 64 20 69  g mode is used i
fed40 6e 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65  n situations whe
fed50 72 65 20 6d 75 6c 74 69 70 6c 65 0d 0a 2a 2a 20  re multiple..** 
fed60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
fed70 69 6f 6e 73 20 61 72 65 20 61 63 63 65 73 73 69  ions are accessi
fed80 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
fed90 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65  base file at the
feda0 20 73 61 6d 65 0d 0a 2a 2a 20 74 69 6d 65 20 61   same..** time a
fedb0 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  nd one or more o
fedc0 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 74 69  f those connecti
fedd0 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e 67 2e  ons are writing.
fede0 0d 0a 2a 2f 0d 0a 0d 0a 73 74 61 74 69 63 20 69  ..*/....static i
fedf0 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65  nt nolockCheckRe
fee00 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
fee10 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64  e3_file *NotUsed
fee20 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
fee30 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
fee40 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a  ETER(NotUsed);..
fee50 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0d    *pResOut = 0;.
fee60 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
fee70 5f 4f 4b 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20  _OK;..}..static 
fee80 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73  int nolockLock(s
fee90 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
feea0 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65  Used, int NotUse
feeb0 64 32 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  d2){..  UNUSED_P
feec0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
feed0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d 0a 20  d, NotUsed2);.. 
feee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
feef0 4b 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e  K;..}..static in
fef00 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73  t nolockUnlock(s
fef10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
fef20 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65  Used, int NotUse
fef30 64 32 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  d2){..  UNUSED_P
fef40 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
fef50 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d 0a 20  d, NotUsed2);.. 
fef60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fef70 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
fef80 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0d  Close the file..
fef90 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
fefa0 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69  nolockClose(sqli
fefb0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0d  te3_file *id) {.
fefc0 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55  .  return closeU
fefd0 6e 69 78 46 69 6c 65 28 69 64 29 3b 0d 0a 7d 0d  nixFile(id);..}.
fefe0 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
feff0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
ff000 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d  he no-op lock im
ff010 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
ff020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff030 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
ff040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff080 2a 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  **/..../********
ff090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff0d0 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  ******..********
ff0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff0f0 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65  * Begin dot-file
ff100 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
ff110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff120 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  *******..**..** 
ff130 54 68 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b  The dotfile lock
ff140 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
ff150 6f 6e 20 75 73 65 73 20 74 68 65 20 65 78 69 73  on uses the exis
ff160 74 61 6e 63 65 20 6f 66 20 73 65 70 61 72 61 74  tance of separat
ff170 65 20 6c 6f 63 6b 0d 0a 2a 2a 20 66 69 6c 65 73  e lock..** files
ff180 20 28 72 65 61 6c 6c 79 20 61 20 64 69 72 65 63   (really a direc
ff190 74 6f 72 79 29 20 74 6f 20 63 6f 6e 74 72 6f 6c  tory) to control
ff1a0 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64   access to the d
ff1b0 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 77  atabase.  This w
ff1c0 6f 72 6b 73 0d 0a 2a 2a 20 6f 6e 20 6a 75 73 74  orks..** on just
ff1d0 20 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 6c   about every fil
ff1e0 65 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 62  esystem imaginab
ff1f0 6c 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 61  le.  But there a
ff200 72 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e 73  re serious downs
ff210 69 64 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ides:..**..**   
ff220 20 28 31 29 20 20 54 68 65 72 65 20 69 73 20 7a   (1)  There is z
ff230 65 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e  ero concurrency.
ff240 20 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 65    A single reade
ff250 72 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68  r blocks all oth
ff260 65 72 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 63  er..**         c
ff270 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20  onnections from 
ff280 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
ff290 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
ff2a0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 28 32 29 20  ..**..**    (2) 
ff2b0 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   An application 
ff2c0 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 6c  crash or power l
ff2d0 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73 74  oss can leave st
ff2e0 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0d 0a  ale lock files..
ff2f0 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69  **         sitti
ff300 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e  ng around that n
ff310 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65  eed to be cleare
ff320 64 20 6d 61 6e 75 61 6c 6c 79 2e 0d 0a 2a 2a 0d  d manually...**.
ff330 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73  .** Nevertheless
ff340 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61  , a dotlock is a
ff350 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f  n appropriate lo
ff360 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75  cking mode for u
ff370 73 65 20 69 66 20 6e 6f 0d 0a 2a 2a 20 6f 74 68  se if no..** oth
ff380 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  er locking strat
ff390 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c 65  egy is available
ff3a0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 6f 74 66 69 6c  ...**..** Dotfil
ff3b0 65 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20  e locking works 
ff3c0 62 79 20 63 72 65 61 74 69 6e 67 20 61 20 73 75  by creating a su
ff3d0 62 64 69 72 65 63 74 6f 72 79 20 69 6e 20 74 68  bdirectory in th
ff3e0 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79  e same directory
ff3f0 20 61 73 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61   as..** the data
ff400 62 61 73 65 20 61 6e 64 20 77 69 74 68 20 74 68  base and with th
ff410 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20  e same name but 
ff420 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65  with a ".lock" e
ff430 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0d  xtension added..
ff440 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e 63  .** The existanc
ff450 65 20 6f 66 20 61 20 6c 6f 63 6b 20 64 69 72 65  e of a lock dire
ff460 63 74 6f 72 79 20 69 6d 70 6c 69 65 73 20 61 6e  ctory implies an
ff470 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
ff480 20 20 41 6c 6c 20 6f 74 68 65 72 0d 0a 2a 2a 20    All other..** 
ff490 6c 6f 63 6b 20 74 79 70 65 73 20 28 53 48 41 52  lock types (SHAR
ff4a0 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45  ED, RESERVED, PE
ff4b0 4e 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65  NDING) are mappe
ff4c0 64 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45  d into EXCLUSIVE
ff4d0 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
ff4e0 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20  The file suffix 
ff4f0 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74  added to the dat
ff500 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  a base filename 
ff510 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
ff520 74 65 20 74 68 65 0d 0a 2a 2a 20 6c 6f 63 6b 20  te the..** lock 
ff530 64 69 72 65 63 74 6f 72 79 2e 0d 0a 2a 2f 0d 0a  directory...*/..
ff540 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f  #define DOTLOCK_
ff550 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0d 0a  SUFFIX ".lock"..
ff560 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ../*..** This ro
ff570 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
ff580 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
ff590 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
ff5a0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0d 0a   the specified..
ff5b0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
ff5c0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
ff5d0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
ff5e0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65  lock is held, se
ff5f0 74 20 2a 70 52 65 73 4f 75 74 0d 0a 2a 2a 20 74  t *pResOut..** t
ff600 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
ff610 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
ff620 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
ff630 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
ff640 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20 69 73 20 73  n value..** is s
ff650 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
ff660 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72  unless an I/O er
ff670 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
ff680 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e  g lock checking.
ff690 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 64 6f 74 66  ..**..** In dotf
ff6a0 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74  ile locking, eit
ff6b0 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  her a lock exist
ff6c0 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e 6f 74  s or it does not
ff6d0 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0d 0a 2a  .  So in this..*
ff6e0 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 43  * variation of C
ff6f0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
ff700 28 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 20  (), *pResOut is 
ff710 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61  set to true if a
ff720 6e 79 20 6c 6f 63 6b 0d 0a 2a 2a 20 69 73 20 68  ny lock..** is h
ff730 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20  eld on the file 
ff740 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 68 65  and false if the
ff750 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
ff760 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  d...*/..static i
ff770 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52  nt dotlockCheckR
ff780 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
ff790 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
ff7a0 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0d 0a 20  t *pResOut) {.. 
ff7b0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
ff7c0 5f 4f 4b 3b 0d 0a 20 20 69 6e 74 20 72 65 73 65  _OK;..  int rese
ff7d0 72 76 65 64 20 3d 20 30 3b 0d 0a 20 20 75 6e 69  rved = 0;..  uni
ff7e0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
ff7f0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 0d  unixFile*)id;...
ff800 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
ff810 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
ff820 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
ff830 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0d 0a 20  ERVEDLOCK; );.. 
ff840 20 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69   ..  assert( pFi
ff850 6c 65 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68  le );....  /* Ch
ff860 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
ff870 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
ff880 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
ff890 6b 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46 69 6c  k */..  if( pFil
ff8a0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41  e->eFileLock>SHA
ff8b0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20  RED_LOCK ){..   
ff8c0 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 73 20   /* Either this 
ff8d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f  connection or so
ff8e0 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  me other connect
ff8f0 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ion in the same 
ff900 70 72 6f 63 65 73 73 0d 0a 20 20 20 20 2a 2a 20  process..    ** 
ff910 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20  holds a lock on 
ff920 74 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e 65  the file.  No ne
ff930 65 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 74  ed to check furt
ff940 68 65 72 2e 20 2a 2f 0d 0a 20 20 20 20 72 65 73  her. */..    res
ff950 65 72 76 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 65  erved = 1;..  }e
ff960 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65  lse{..    /* The
ff970 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 66   lock is held if
ff980 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65   and only if the
ff990 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 73   lockfile exists
ff9a0 20 2a 2f 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63   */..    const c
ff9b0 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
ff9c0 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 46   (const char*)pF
ff9d0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
ff9e0 65 78 74 3b 0d 0a 20 20 20 20 72 65 73 65 72 76  ext;..    reserv
ff9f0 65 64 20 3d 20 6f 73 41 63 63 65 73 73 28 7a 4c  ed = osAccess(zL
ffa00 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0d  ockFile, 0)==0;.
ffa10 0a 20 20 7d 0d 0a 20 20 4f 53 54 52 41 43 45 28  .  }..  OSTRACE(
ffa20 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
ffa30 64 20 25 64 20 25 64 20 28 64 6f 74 6c 6f 63 6b  d %d %d (dotlock
ffa40 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
ffa50 72 63 2c 20 72 65 73 65 72 76 65 64 29 29 3b 0d  rc, reserved));.
ffa60 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
ffa70 73 65 72 76 65 64 3b 0d 0a 20 20 72 65 74 75 72  served;..  retur
ffa80 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
ffa90 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
ffaa0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
ffab0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
ffac0 6d 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20  meter eFileLock 
ffad0 2d 20 6f 6e 65 0d 0a 2a 2a 20 6f 66 20 74 68 65  - one..** of the
ffae0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d   following:..**.
ffaf0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52  .**     (1) SHAR
ffb00 45 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20  ED_LOCK..**     
ffb10 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
ffb20 4b 0d 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  K..**     (3) PE
ffb30 4e 44 49 4e 47 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20  NDING_LOCK..**  
ffb40 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
ffb50 5f 4c 4f 43 4b 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f  _LOCK..**..** So
ffb60 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
ffb70 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
ffb80 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
ffb90 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0d 0a  al lock states..
ffba0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
ffbb0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
ffbc0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
ffbd0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
ffbe0 65 20 6c 61 74 65 72 0d 0a 2a 2a 20 74 72 61 6e  e later..** tran
ffbf0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
ffc00 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
ffc10 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
ffc20 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
ffc30 74 0d 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  t..** still shor
ffc40 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
ffc50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
ffc60 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
ffc70 6c 6f 77 65 64 0d 0a 2a 2a 20 74 72 61 6e 73 69  lowed..** transi
ffc80 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
ffc90 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
ffca0 61 74 65 20 73 74 61 74 65 73 3a 0d 0a 2a 2a 0d  ate states:..**.
ffcb0 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
ffcc0 2d 3e 20 53 48 41 52 45 44 0d 0a 2a 2a 20 20 20  -> SHARED..**   
ffcd0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
ffce0 56 45 44 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45  VED..**    SHARE
ffcf0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
ffd00 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20  > EXCLUSIVE..** 
ffd10 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
ffd20 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
ffd30 55 53 49 56 45 0d 0a 2a 2a 20 20 20 20 50 45 4e  USIVE..**    PEN
ffd40 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
ffd50 45 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  E..**..** This r
ffd60 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
ffd70 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
ffd80 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
ffd90 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0d 0a 2a 2a  e3OsUnlock()..**
ffda0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
ffdb0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
ffdc0 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 69 74 68 20  l...**..** With 
ffdd0 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c  dotfile locking,
ffde0 20 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20   we really only 
ffdf0 73 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 34  support state (4
ffe00 29 3a 20 45 58 43 4c 55 53 49 56 45 2e 0d 0a 2a  ): EXCLUSIVE...*
ffe10 2a 20 42 75 74 20 77 65 20 74 72 61 63 6b 20 74  * But we track t
ffe20 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67  he other locking
ffe30 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c   levels internal
ffe40 6c 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ly...*/..static 
ffe50 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28  int dotlockLock(
ffe60 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
ffe70 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29  , int eFileLock)
ffe80 20 7b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   {..  unixFile *
ffe90 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
ffea0 65 2a 29 69 64 3b 0d 0a 20 20 63 68 61 72 20 2a  e*)id;..  char *
ffeb0 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61  zLockFile = (cha
ffec0 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  r *)pFile->locki
ffed0 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a 20 20 69 6e  ngContext;..  in
ffee0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ffef0 3b 0d 0a 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 77  ;......  /* If w
fff00 65 20 68 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c  e have any lock,
fff10 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66   then the lock f
fff20 69 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73  ile already exis
fff30 74 73 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65  ts.  All we have
fff40 0d 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20  ..  ** to do is 
fff50 61 64 6a 75 73 74 20 6f 75 72 20 69 6e 74 65 72  adjust our inter
fff60 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68  nal record of th
fff70 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e 0d 0a 20  e lock level... 
fff80 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46 69 6c 65   */..  if( pFile
fff90 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3e 20 4e 4f  ->eFileLock > NO
fffa0 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 70 46  _LOCK ){..    pF
fffb0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d  ile->eFileLock =
fffc0 20 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20   eFileLock;..   
fffd0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74   /* Always updat
fffe0 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20  e the timestamp 
ffff0 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20  on the old file 
10000 2a 2f 0d 0a 23 69 66 64 65 66 20 48 41 56 45 5f  */..#ifdef HAVE_
10001 55 54 49 4d 45 0d 0a 20 20 20 20 75 74 69 6d 65  UTIME..    utime
10002 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c  (zLockFile, NULL
10003 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 75  );..#else..    u
10004 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c  times(zLockFile,
10005 20 4e 55 4c 4c 29 3b 0d 0a 23 65 6e 64 69 66 0d   NULL);..#endif.
10006 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10007 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 0d  TE_OK;..  }..  .
10008 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78  .  /* grab an ex
10009 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0d  clusive lock */.
1000a 0a 20 20 72 63 20 3d 20 6f 73 4d 6b 64 69 72 28  .  rc = osMkdir(
1000b 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 37 37 37 29  zLockFile, 0777)
1000c 3b 0d 0a 20 20 69 66 28 20 72 63 3c 30 20 29 7b  ;..  if( rc<0 ){
1000d 0d 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20  ..    /* failed 
1000e 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74  to open/create t
1000f 68 65 20 6c 6f 63 6b 20 64 69 72 65 63 74 6f 72  he lock director
10010 79 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 74 45  y */..    int tE
10011 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20  rrno = errno;.. 
10012 20 20 20 69 66 28 20 45 45 58 49 53 54 20 3d 3d     if( EEXIST ==
10013 20 74 45 72 72 6e 6f 20 29 7b 0d 0a 20 20 20 20   tErrno ){..    
10014 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
10015 53 59 3b 0d 0a 20 20 20 20 7d 20 65 6c 73 65 20  SY;..    } else 
10016 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
10017 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
10018 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
10019 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
1001a 4b 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 49  K);..      if( I
1001b 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
1001c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 69   ){..        pFi
1001d 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
1001e 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 7d  tErrno;..      }
1001f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74  ..    }..    ret
10020 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 20 0d 0a 20  urn rc;..  } .. 
10021 20 0d 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20   ..  /* got it, 
10022 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64  set the type and
10023 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0d 0a 20   return ok */.. 
10024 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
10025 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a  k = eFileLock;..
10026 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
10027 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 77 65 72 20  .../*..** Lower 
10028 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
10029 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
1002a 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 65 46  ptor pFile to eF
1002b 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c  ileLock.  eFileL
1002c 6f 63 6b 0d 0a 2a 2a 20 6d 75 73 74 20 62 65 20  ock..** must be 
1002d 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f  either NO_LOCK o
1002e 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0d 0a  r SHARED_LOCK...
1002f 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  **..** If the lo
10030 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
10031 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
10032 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
10033 20 6f 72 20 62 65 6c 6f 77 0d 0a 2a 2a 20 74 68   or below..** th
10034 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
10035 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
10036 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
10037 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e  op...**..** When
10038 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
10039 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f  el reaches NO_LO
1003a 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c  CK, delete the l
1003b 6f 63 6b 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73  ock file...*/..s
1003c 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
1003d 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
1003e 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46  file *id, int eF
1003f 69 6c 65 4c 6f 63 6b 29 20 7b 0d 0a 20 20 75 6e  ileLock) {..  un
10040 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
10041 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a  (unixFile*)id;..
10042 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
10043 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c  e = (char *)pFil
10044 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
10045 74 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 0d  t;..  int rc;...
10046 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
10047 20 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28   );..  OSTRACE((
10048 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77  "UNLOCK  %d %d w
10049 61 73 20 25 64 20 70 69 64 3d 25 64 20 28 64 6f  as %d pid=%d (do
1004a 74 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65  tlock)\n", pFile
1004b 2d 3e 68 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0d  ->h, eFileLock,.
1004c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c  ..   pFile->eFil
1004d 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64 28 29 29  eLock, getpid())
1004e 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 65 46  );..  assert( eF
1004f 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f  ileLock<=SHARED_
10050 4c 4f 43 4b 20 29 3b 0d 0a 20 20 0d 0a 20 20 2f  LOCK );..  ..  /
10051 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69  * no-op if possi
10052 62 6c 65 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46  ble */..  if( pF
10053 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d  ile->eFileLock==
10054 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d 0a 20 20  eFileLock ){..  
10055 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10056 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  OK;..  }....  /*
10057 20 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f   To downgrade to
10058 20 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20   shared, simply 
10059 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72  update our inter
1005a 6e 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68  nal notion of th
1005b 65 0d 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61  e..  ** lock sta
1005c 74 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20  te.  No need to 
1005d 6d 65 73 73 20 77 69 74 68 20 74 68 65 20 66 69  mess with the fi
1005e 6c 65 20 6f 6e 20 64 69 73 6b 2e 0d 0a 20 20 2a  le on disk...  *
1005f 2f 0d 0a 20 20 69 66 28 20 65 46 69 6c 65 4c 6f  /..  if( eFileLo
10060 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck==SHARED_LOCK 
10061 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65  ){..    pFile->e
10062 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45  FileLock = SHARE
10063 44 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 72 65 74  D_LOCK;..    ret
10064 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
10065 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 54 6f    }..  ..  /* To
10066 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68   fully unlock th
10067 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 6c 65  e database, dele
10068 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  te the lock file
10069 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 65   */..  assert( e
1006a 46 69 6c 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43  FileLock==NO_LOC
1006b 4b 20 29 3b 0d 0a 20 20 72 63 20 3d 20 6f 73 52  K );..  rc = osR
1006c 6d 64 69 72 28 7a 4c 6f 63 6b 46 69 6c 65 29 3b  mdir(zLockFile);
1006d 0d 0a 20 20 69 66 28 20 72 63 3c 30 20 26 26 20  ..  if( rc<0 && 
1006e 65 72 72 6e 6f 3d 3d 45 4e 4f 54 44 49 52 20 29  errno==ENOTDIR )
1006f 20 72 63 20 3d 20 6f 73 55 6e 6c 69 6e 6b 28 7a   rc = osUnlink(z
10070 4c 6f 63 6b 46 69 6c 65 29 3b 0d 0a 20 20 69 66  LockFile);..  if
10071 28 20 72 63 3c 30 20 29 7b 0d 0a 20 20 20 20 69  ( rc<0 ){..    i
10072 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
10073 6f 3b 0d 0a 20 20 20 20 72 63 20 3d 20 30 3b 0d  o;..    rc = 0;.
10074 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20  .    if( ENOENT 
10075 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0d 0a 20 20  != tErrno ){..  
10076 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
10077 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0d 0a 20  IOERR_UNLOCK;.. 
10078 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 49 53     }..    if( IS
10079 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
1007a 29 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ){..      pFile-
1007b 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
1007c 72 6e 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  rno;..    }..   
1007d 20 72 65 74 75 72 6e 20 72 63 3b 20 0d 0a 20 20   return rc; ..  
1007e 7d 0d 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c  }..  pFile->eFil
1007f 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  eLock = NO_LOCK;
10080 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
10081 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
10082 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
10083 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20    Make sure the 
10084 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65  lock has been re
10085 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 63 6c  leased before cl
10086 6f 73 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  osing...*/..stat
10087 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c  ic int dotlockCl
10088 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
10089 20 2a 69 64 29 20 7b 0d 0a 20 20 69 6e 74 20 72   *id) {..  int r
1008a 63 3b 0d 0a 20 20 69 66 28 20 69 64 20 29 7b 0d  c;..  if( id ){.
1008b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
1008c 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
1008d 2a 29 69 64 3b 0d 0a 20 20 20 20 64 6f 74 6c 6f  *)id;..    dotlo
1008e 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ckUnlock(id, NO_
1008f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 73 71 6c 69  LOCK);..    sqli
10090 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
10091 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
10092 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 63 6c  ..  }..  rc = cl
10093 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
10094 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
10095 7d 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
10096 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
10097 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20  e dot-file lock 
10098 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
10099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1009a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
1009b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1009c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1009d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1009e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1009f 2a 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  **/..../********
100a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a4 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  ******..********
100a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a6 2a 2a 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c  ** Begin flock L
100a7 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ocking *********
100a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100a9 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  *******..**..** 
100aa 55 73 65 20 74 68 65 20 66 6c 6f 63 6b 28 29 20  Use the flock() 
100ab 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20 64  system call to d
100ac 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0d  o file locking..
100ad 0a 2a 2a 0d 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20  .**..** flock() 
100ae 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20  locking is like 
100af 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  dot-file locking
100b0 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 61 72   in that the var
100b1 69 6f 75 73 0d 0a 2a 2a 20 66 69 6e 65 2d 67 72  ious..** fine-gr
100b2 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ain locking leve
100b3 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  ls supported by 
100b4 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61  SQLite are colla
100b5 70 73 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 61 20  psed into..** a 
100b6 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65  single exclusive
100b7 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72   lock.  In other
100b8 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20   words, SHARED, 
100b9 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0d 0a 2a  RESERVED, and..*
100ba 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20  * PENDING locks 
100bb 61 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69  are the same thi
100bc 6e 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49  ng as an EXCLUSI
100bd 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65  VE lock.  SQLite
100be 0d 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73  ..** still works
100bf 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69   when you do thi
100c0 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e  s, but concurren
100c1 63 79 20 69 73 20 72 65 64 75 63 65 64 20 73 69  cy is reduced si
100c2 6e 63 65 0d 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73  nce..** only a s
100c3 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61  ingle process ca
100c4 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 65  n be reading the
100c5 20 64 61 74 61 62 61 73 65 20 61 74 20 61 20 74   database at a t
100c6 69 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6d 69  ime...**..** Omi
100c7 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69  t this section i
100c8 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
100c9 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73  LOCKING_STYLE is
100ca 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69   turned off or i
100cb 66 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20  f..** compiling 
100cc 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0d 0a 2a 2f  for VXWORKS...*/
100cd 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
100ce 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
100cf 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53  E && !OS_VXWORKS
100d0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 72 79  ..../*..** Retry
100d1 20 66 6c 6f 63 6b 28 29 20 63 61 6c 6c 73 20 74   flock() calls t
100d2 68 61 74 20 66 61 69 6c 20 77 69 74 68 20 45 49  hat fail with EI
100d3 4e 54 52 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  NTR..*/..#ifdef 
100d4 45 49 4e 54 52 0d 0a 73 74 61 74 69 63 20 69 6e  EINTR..static in
100d5 74 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 69  t robust_flock(i
100d6 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 29 7b 0d  nt fd, int op){.
100d7 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 64 6f  .  int rc;..  do
100d8 7b 20 72 63 20 3d 20 66 6c 6f 63 6b 28 66 64 2c  { rc = flock(fd,
100d9 6f 70 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c  op); }while( rc<
100da 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54  0 && errno==EINT
100db 52 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  R );..  return r
100dc 63 3b 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 20  c;..}..#else..# 
100dd 64 65 66 69 6e 65 20 72 6f 62 75 73 74 5f 66 6c  define robust_fl
100de 6f 63 6b 28 61 2c 62 29 20 66 6c 6f 63 6b 28 61  ock(a,b) flock(a
100df 2c 62 29 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  ,b)..#endif..   
100e0 20 20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69    ..../*..** Thi
100e1 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
100e2 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
100e3 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
100e4 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
100e5 65 64 0d 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ed..** file by t
100e6 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
100e7 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
100e8 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
100e9 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0d 0a  , set *pResOut..
100ea 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  ** to a non-zero
100eb 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65   value otherwise
100ec 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74   *pResOut is set
100ed 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72   to zero.  The r
100ee 65 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20  eturn value..** 
100ef 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
100f0 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
100f1 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
100f2 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
100f3 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ing...*/..static
100f4 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52   int flockCheckR
100f5 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
100f6 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
100f7 74 20 2a 70 52 65 73 4f 75 74 29 7b 0d 0a 20 20  t *pResOut){..  
100f8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
100f9 4f 4b 3b 0d 0a 20 20 69 6e 74 20 72 65 73 65 72  OK;..  int reser
100fa 76 65 64 20 3d 20 30 3b 0d 0a 20 20 75 6e 69 78  ved = 0;..  unix
100fb 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
100fc 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20  nixFile*)id;..  
100fd 0d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
100fe 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
100ff 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
10100 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0d 0a  SERVEDLOCK; );..
10101 20 20 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46    ..  assert( pF
10102 69 6c 65 20 29 3b 0d 0a 20 20 0d 0a 20 20 2f 2a  ile );..  ..  /*
10103 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
10104 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
10105 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
10106 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69 66 28 20 70  lock */..  if( p
10107 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e  File->eFileLock>
10108 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a  SHARED_LOCK ){..
10109 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
1010a 3b 0d 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a  ;..  }..  ..  /*
1010b 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
1010c 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
1010d 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a  cess holds it. *
1010e 2f 0d 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  /..  if( !reserv
1010f 65 64 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 61 74  ed ){..    /* at
10110 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65  tempt to get the
10111 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 69 6e   lock */..    in
10112 74 20 6c 72 63 20 3d 20 72 6f 62 75 73 74 5f 66  t lrc = robust_f
10113 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
10114 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
10115 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 6c 72 63  );..    if( !lrc
10116 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 67 6f   ){..      /* go
10117 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f  t the lock, unlo
10118 63 6b 20 69 74 20 2a 2f 0d 0a 20 20 20 20 20 20  ck it */..      
10119 6c 72 63 20 3d 20 72 6f 62 75 73 74 5f 66 6c 6f  lrc = robust_flo
1011a 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43  ck(pFile->h, LOC
1011b 4b 5f 55 4e 29 3b 0d 0a 20 20 20 20 20 20 69 66  K_UN);..      if
1011c 20 28 20 6c 72 63 20 29 20 7b 0d 0a 20 20 20 20   ( lrc ) {..    
1011d 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
1011e 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20   errno;..       
1011f 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65   /* unlock faile
10120 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
10121 2a 2f 0d 0a 20 20 20 20 20 20 20 20 6c 72 63 20  */..        lrc 
10122 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  = SQLITE_IOERR_U
10123 4e 4c 4f 43 4b 3b 20 0d 0a 20 20 20 20 20 20 20  NLOCK; ..       
10124 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
10125 4f 52 28 6c 72 63 29 20 29 7b 0d 0a 20 20 20 20  OR(lrc) ){..    
10126 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
10127 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
10128 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ..          rc =
10129 20 6c 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d   lrc;..        }
1012a 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
1012b 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 20 20 69   else {..      i
1012c 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
1012d 6f 3b 0d 0a 20 20 20 20 20 20 72 65 73 65 72 76  o;..      reserv
1012e 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 2f  ed = 1;..      /
1012f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  * someone else m
10130 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65 73  ight have it res
10131 65 72 76 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20  erved */..      
10132 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f  lrc = sqliteErro
10133 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
10134 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
10135 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0d 0a 20 20  OERR_LOCK); ..  
10136 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
10137 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0d 0a 20  ERROR(lrc) ){.. 
10138 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
10139 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
1013a 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ;..        rc = 
1013b 6c 72 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  lrc;..      }.. 
1013c 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 4f 53 54     }..  }..  OST
1013d 52 41 43 45 28 28 22 54 45 53 54 20 57 52 2d 4c  RACE(("TEST WR-L
1013e 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 66 6c  OCK %d %d %d (fl
1013f 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ock)\n", pFile->
10140 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
10141 29 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  );....#ifdef SQL
10142 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b  ITE_IGNORE_FLOCK
10143 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0d 0a 20 20  _LOCK_ERRORS..  
10144 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 45  if( (rc & SQLITE
10145 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54  _IOERR) == SQLIT
10146 45 5f 49 4f 45 52 52 20 29 7b 0d 0a 20 20 20 20  E_IOERR ){..    
10147 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
10148 0a 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b  .    reserved=1;
10149 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  ..  }..#endif /*
1014a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46   SQLITE_IGNORE_F
1014b 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53  LOCK_LOCK_ERRORS
1014c 20 2a 2f 0d 0a 20 20 2a 70 52 65 73 4f 75 74 20   */..  *pResOut 
1014d 3d 20 72 65 73 65 72 76 65 64 3b 0d 0a 20 20 72  = reserved;..  r
1014e 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1014f 2f 2a 0d 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  /*..** Lock the 
10150 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f  file with the lo
10151 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ck specified by 
10152 70 61 72 61 6d 65 74 65 72 20 65 46 69 6c 65 4c  parameter eFileL
10153 6f 63 6b 20 2d 20 6f 6e 65 0d 0a 2a 2a 20 6f 66  ock - one..** of
10154 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d   the following:.
10155 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 28 31 29 20  .**..**     (1) 
10156 53 48 41 52 45 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20  SHARED_LOCK..** 
10157 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
10158 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20 28 33  _LOCK..**     (3
10159 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0d 0a  ) PENDING_LOCK..
1015a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
1015b 53 49 56 45 5f 4c 4f 43 4b 0d 0a 2a 2a 0d 0a 2a  SIVE_LOCK..**..*
1015c 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
1015d 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
1015e 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
1015f 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
10160 65 73 0d 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  es..** are inser
10161 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
10162 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
10163 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
10164 66 20 74 68 65 20 6c 61 74 65 72 0d 0a 2a 2a 20  f the later..** 
10165 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
10166 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
10167 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
10168 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
10169 64 20 62 75 74 0d 0a 2a 2a 20 73 74 69 6c 6c 20  d but..** still 
1016a 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
1016b 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
1016c 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
1016d 65 20 61 6c 6c 6f 77 65 64 0d 0a 2a 2a 20 74 72  e allowed..** tr
1016e 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
1016f 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72  e inserted inter
10170 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0d  mediate states:.
10171 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  .**..**    UNLOC
10172 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0d 0a 2a  KED -> SHARED..*
10173 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52  *    SHARED -> R
10174 45 53 45 52 56 45 44 0d 0a 2a 2a 20 20 20 20 53  ESERVED..**    S
10175 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
10176 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d  G) -> EXCLUSIVE.
10177 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
10178 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
10179 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 20 20  EXCLUSIVE..**   
1017a 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
1017b 55 53 49 56 45 0d 0a 2a 2a 0d 0a 2a 2a 20 66 6c  USIVE..**..** fl
1017c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c  ock() only reall
1017d 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53  y support EXCLUS
1017e 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74  IVE locks.  We t
1017f 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74  rack intermediat
10180 65 0d 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65  e..** lock state
10181 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  s in the sqlite3
10182 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c  _file structure,
10183 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53   but all locks S
10184 48 41 52 45 44 20 6f 72 0d 0a 2a 2a 20 61 62 6f  HARED or..** abo
10185 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58  ve are really EX
10186 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e  CLUSIVE locks an
10187 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74  d exclude all ot
10188 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72  her processes fr
10189 6f 6d 0d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68  om..** access th
1018a 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  e file...**..** 
1018b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
1018c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
1018d 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
1018e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
1018f 28 29 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  ()..** routine t
10190 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
10191 67 20 6c 65 76 65 6c 2e 0d 0a 2a 2f 0d 0a 73 74  g level...*/..st
10192 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f  atic int flockLo
10193 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
10194 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f  *id, int eFileLo
10195 63 6b 29 20 7b 0d 0a 20 20 69 6e 74 20 72 63 20  ck) {..  int rc 
10196 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
10197 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
10198 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
10199 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46  ....  assert( pF
1019a 69 6c 65 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 69  ile );....  /* i
1019b 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76  f we already hav
1019c 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20  e a lock, it is 
1019d 65 78 63 6c 75 73 69 76 65 2e 20 20 0d 0a 20 20  exclusive.  ..  
1019e 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c  ** Just adjust l
1019f 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e  evel and punt on
101a0 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0d   outta here. */.
101a1 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 65 46  .  if (pFile->eF
101a2 69 6c 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43  ileLock > NO_LOC
101a3 4b 29 20 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  K) {..    pFile-
101a4 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69  >eFileLock = eFi
101a5 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20 72 65 74  leLock;..    ret
101a6 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
101a7 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 67 72    }..  ..  /* gr
101a8 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ab an exclusive 
101a9 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 0d 0a 20 20 69  lock */..  ..  i
101aa 66 20 28 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28  f (robust_flock(
101ab 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45  pFile->h, LOCK_E
101ac 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0d  X | LOCK_NB)) {.
101ad 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
101ae 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20 2f 2a  = errno;..    /*
101af 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73   didn't get, mus
101b0 74 20 62 65 20 62 75 73 79 20 2a 2f 0d 0a 20 20  t be busy */..  
101b1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
101b2 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
101b3 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
101b4 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  IOERR_LOCK);..  
101b5 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
101b6 52 4f 52 28 72 63 29 20 29 7b 0d 0a 20 20 20 20  ROR(rc) ){..    
101b7 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
101b8 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0d 0a 20 20  no = tErrno;..  
101b9 20 20 7d 0d 0a 20 20 7d 20 65 6c 73 65 20 7b 0d    }..  } else {.
101ba 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20  .    /* got it, 
101bb 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64  set the type and
101bc 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0d 0a 20   return ok */.. 
101bd 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c     pFile->eFileL
101be 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b  ock = eFileLock;
101bf 0d 0a 20 20 7d 0d 0a 20 20 4f 53 54 52 41 43 45  ..  }..  OSTRACE
101c0 28 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  (("LOCK    %d %s
101c1 20 25 73 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c 20   %s (flock)\n", 
101c2 70 46 69 6c 65 2d 3e 68 2c 20 61 7a 46 69 6c 65  pFile->h, azFile
101c3 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 6b 29 2c  Lock(eFileLock),
101c4 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 72 63   ..           rc
101c5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
101c6 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b  k" : "failed"));
101c7 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
101c8 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43  IGNORE_FLOCK_LOC
101c9 4b 5f 45 52 52 4f 52 53 0d 0a 20 20 69 66 28 20  K_ERRORS..  if( 
101ca 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45  (rc & SQLITE_IOE
101cb 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  RR) == SQLITE_IO
101cc 45 52 52 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ERR ){..    rc =
101cd 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
101ce 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   }..#endif /* SQ
101cf 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
101d0 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  K_LOCK_ERRORS */
101d1 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
101d2 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f  }....../*..** Lo
101d3 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
101d4 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
101d5 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
101d6 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46  o eFileLock.  eF
101d7 69 6c 65 4c 6f 63 6b 0d 0a 2a 2a 20 6d 75 73 74  ileLock..** must
101d8 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
101d9 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
101da 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  K...**..** If th
101db 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
101dc 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
101dd 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
101de 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0d 0a 2a  y at or below..*
101df 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
101e0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
101e1 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
101e2 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 73 74 61   no-op...*/..sta
101e3 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c  tic int flockUnl
101e4 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
101e5 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c   *id, int eFileL
101e6 6f 63 6b 29 20 7b 0d 0a 20 20 75 6e 69 78 46 69  ock) {..  unixFi
101e7 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
101e8 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 0d 0a  xFile*)id;..  ..
101e9 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
101ea 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22  );..  OSTRACE(("
101eb 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61  UNLOCK  %d %d wa
101ec 73 20 25 64 20 70 69 64 3d 25 64 20 28 66 6c 6f  s %d pid=%d (flo
101ed 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ck)\n", pFile->h
101ee 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0d 0a 20 20  , eFileLock,..  
101ef 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
101f0 65 46 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69  eFileLock, getpi
101f1 64 28 29 29 29 3b 0d 0a 20 20 61 73 73 65 72 74  d()));..  assert
101f2 28 20 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41  ( eFileLock<=SHA
101f3 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 0d  RED_LOCK );..  .
101f4 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70  .  /* no-op if p
101f5 6f 73 73 69 62 6c 65 20 2a 2f 0d 0a 20 20 69 66  ossible */..  if
101f6 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f  ( pFile->eFileLo
101f7 63 6b 3d 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b  ck==eFileLock ){
101f8 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
101f9 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20  ITE_OK;..  }..  
101fa 0d 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61  ..  /* shared ca
101fb 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65  n just be set be
101fc 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20  cause we always 
101fd 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
101fe 65 20 2a 2f 0d 0a 20 20 69 66 20 28 65 46 69 6c  e */..  if (eFil
101ff 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f  eLock==SHARED_LO
10200 43 4b 29 20 7b 0d 0a 20 20 20 20 70 46 69 6c 65  CK) {..    pFile
10201 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46  ->eFileLock = eF
10202 69 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20 72 65  ileLock;..    re
10203 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
10204 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 6e  .  }..  ..  /* n
10205 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63  o, really, unloc
10206 6b 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 72 6f 62  k. */..  if( rob
10207 75 73 74 5f 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  ust_flock(pFile-
10208 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 20 29 7b 0d  >h, LOCK_UN) ){.
10209 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49  .#ifdef SQLITE_I
1020a 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
1020b 5f 45 52 52 4f 52 53 0d 0a 20 20 20 20 72 65 74  _ERRORS..    ret
1020c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
1020d 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1020e 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
1020f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0d 0a 20 20  CK_ERRORS */..  
10210 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10211 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0d 0a 20  IOERR_UNLOCK;.. 
10212 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 46 69   }else{..    pFi
10213 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
10214 4e 4f 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 72 65  NO_LOCK;..    re
10215 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
10216 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
10217 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0d  * Close a file..
10218 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
10219 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74  flockClose(sqlit
1021a 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0d 0a  e3_file *id) {..
1021b 20 20 69 66 28 20 69 64 20 29 7b 0d 0a 20 20 20    if( id ){..   
1021c 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c   flockUnlock(id,
1021d 20 4e 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 7d 0d   NO_LOCK);..  }.
1021e 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55  .  return closeU
1021f 6e 69 78 46 69 6c 65 28 69 64 29 3b 0d 0a 7d 0d  nixFile(id);..}.
10220 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  ...#endif /* SQL
10221 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
10222 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f  NG_STYLE && !OS_
10223 56 58 57 4f 52 4b 20 2a 2f 0d 0a 0d 0a 2f 2a 2a  VXWORK */..../**
10224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10225 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f  * End of the flo
10226 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  ck lock implemen
10227 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
10228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
10229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1022a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1022b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1022c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1022d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d  ************/...
1022e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1022f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10233 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
10234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10235 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 65   Named Semaphore
10236 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
10237 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10238 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 61 6d 65 64 20  *..**..** Named 
10239 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  semaphore lockin
1023a 67 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72  g is only suppor
1023b 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0d  ted on VxWorks..
1023c 0a 2a 2a 0d 0a 2a 2a 20 53 65 6d 61 70 68 6f 72  .**..** Semaphor
1023d 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b  e locking is lik
1023e 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 66  e dot-lock and f
1023f 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74 20  lock in that it 
10240 72 65 61 6c 6c 79 20 6f 6e 6c 79 0d 0a 2a 2a 20  really only..** 
10241 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49  supports EXCLUSI
10242 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c  VE locking.  Onl
10243 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65  y a single proce
10244 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77  ss can read or w
10245 72 69 74 65 0d 0a 2a 2a 20 74 68 65 20 64 61 74  rite..** the dat
10246 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20  abase file at a 
10247 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75  time.  This redu
10248 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f  ces potential co
10249 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0d 0a  ncurrency, but..
1024a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63  ** makes the loc
1024b 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
1024c 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0d 0a 2a   much easier...*
1024d 2f 0d 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  /..#if OS_VXWORK
1024e 53 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  S..../*..** This
1024f 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
10250 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
10251 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
10252 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
10253 64 0d 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  d..** file by th
10254 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
10255 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
10256 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
10257 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0d 0a 2a   set *pResOut..*
10258 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
10259 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
1025a 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
1025b 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
1025c 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20 69  turn value..** i
1025d 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  s set to SQLITE_
1025e 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f  OK unless an I/O
1025f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
10260 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69  ring lock checki
10261 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ng...*/..static 
10262 69 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65  int semCheckRese
10263 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
10264 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
10265 70 52 65 73 4f 75 74 29 20 7b 0d 0a 20 20 69 6e  pResOut) {..  in
10266 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
10267 3b 0d 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65  ;..  int reserve
10268 64 20 3d 20 30 3b 0d 0a 20 20 75 6e 69 78 46 69  d = 0;..  unixFi
10269 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
1026a 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 0d 0a 20 20  xFile*)id;....  
1026b 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
1026c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
1026d 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
1026e 45 44 4c 4f 43 4b 3b 20 29 3b 0d 0a 20 20 0d 0a  EDLOCK; );..  ..
1026f 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
10270 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b  );....  /* Check
10271 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
10272 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
10273 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
10274 2f 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  /..  if( pFile->
10275 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52 45 44  eFileLock>SHARED
10276 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 72 65  _LOCK ){..    re
10277 73 65 72 76 65 64 20 3d 20 31 3b 0d 0a 20 20 7d  served = 1;..  }
10278 0d 0a 20 20 0d 0a 20 20 2f 2a 20 4f 74 68 65 72  ..  ..  /* Other
10279 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65  wise see if some
1027a 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68   other process h
1027b 6f 6c 64 73 20 69 74 2e 20 2a 2f 0d 0a 20 20 69  olds it. */..  i
1027c 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0d  f( !reserved ){.
1027d 0a 20 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d  .    sem_t *pSem
1027e 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65   = pFile->pInode
1027f 2d 3e 70 53 65 6d 3b 0d 0a 20 20 20 20 73 74 72  ->pSem;..    str
10280 75 63 74 20 73 74 61 74 20 73 74 61 74 42 75 66  uct stat statBuf
10281 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 73 65 6d  ;....    if( sem
10282 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d  _trywait(pSem)==
10283 2d 31 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74  -1 ){..      int
10284 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
10285 0d 0a 20 20 20 20 20 20 69 66 28 20 45 41 47 41  ..      if( EAGA
10286 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0d  IN != tErrno ){.
10287 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
10288 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
10289 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
1028a 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
1028b 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b  CKRESERVEDLOCK);
1028c 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ..        pFile-
1028d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
1028e 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 7d 20 65 6c  rno;..      } el
1028f 73 65 20 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  se {..        /*
10290 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61   someone else ha
10291 73 20 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20  s the lock when 
10292 77 65 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43  we are in NO_LOC
10293 4b 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 65  K */..        re
10294 73 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 2d  served = (pFile-
10295 3e 65 46 69 6c 65 4c 6f 63 6b 20 3c 20 53 48 41  >eFileLock < SHA
10296 52 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20  RED_LOCK);..    
10297 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d    }..    }else{.
10298 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75  .      /* we cou
10299 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65  ld have it if we
1029a 20 77 61 6e 74 20 69 74 20 2a 2f 0d 0a 20 20 20   want it */..   
1029b 20 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d     sem_post(pSem
1029c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
1029d 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 53 54    OSTRACE(("TEST
1029e 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25   WR-LOCK %d %d %
1029f 64 20 28 73 65 6d 29 5c 6e 22 2c 20 70 46 69 6c  d (sem)\n", pFil
102a0 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76  e->h, rc, reserv
102a1 65 64 29 29 3b 0d 0a 0d 0a 20 20 2a 70 52 65 73  ed));....  *pRes
102a2 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0d  Out = reserved;.
102a3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
102a4 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63 6b 20  ..../*..** Lock 
102a5 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
102a6 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
102a7 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 46   by parameter eF
102a8 69 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0d 0a 2a  ileLock - one..*
102a9 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
102aa 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  ng:..**..**     
102ab 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0d  (1) SHARED_LOCK.
102ac 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
102ad 52 56 45 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20  RVED_LOCK..**   
102ae 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
102af 43 4b 0d 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  CK..**     (4) E
102b0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0d 0a 2a  XCLUSIVE_LOCK..*
102b1 2a 0d 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20  *..** Sometimes 
102b2 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20  when requesting 
102b3 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20  one lock state, 
102b4 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20  additional lock 
102b5 73 74 61 74 65 73 0d 0a 2a 2a 20 61 72 65 20 69  states..** are i
102b6 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
102b7 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
102b8 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
102b9 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0d  ne of the later.
102ba 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
102bb 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
102bc 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
102bd 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
102be 61 72 74 65 64 20 62 75 74 0d 0a 2a 2a 20 73 74  arted but..** st
102bf 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73  ill short of its
102c0 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c   goal.  The foll
102c1 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77  owing chart show
102c2 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0d 0a 2a  s the allowed..*
102c3 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
102c4 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
102c5 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
102c6 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 55  es:..**..**    U
102c7 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
102c8 44 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  D..**    SHARED 
102c9 2d 3e 20 52 45 53 45 52 56 45 44 0d 0a 2a 2a 20  -> RESERVED..** 
102ca 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
102cb 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
102cc 49 56 45 0d 0a 2a 2a 20 20 20 20 52 45 53 45 52  IVE..**    RESER
102cd 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
102ce 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a   -> EXCLUSIVE..*
102cf 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
102d0 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 0d 0a 2a  EXCLUSIVE..**..*
102d1 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  * Semaphore lock
102d2 73 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75  s only really su
102d3 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20  pport EXCLUSIVE 
102d4 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b  locks.  We track
102d5 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0d 0a 2a   intermediate..*
102d6 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e  * lock states in
102d7 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   the sqlite3_fil
102d8 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 74  e structure, but
102d9 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45   all locks SHARE
102da 44 20 6f 72 0d 0a 2a 2a 20 61 62 6f 76 65 20 61  D or..** above a
102db 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53  re really EXCLUS
102dc 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78  IVE locks and ex
102dd 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20  clude all other 
102de 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0d 0a  processes from..
102df 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69  ** access the fi
102e0 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  le...**..** This
102e1 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
102e2 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
102e3 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
102e4 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0d 0a  ite3OsUnlock()..
102e5 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
102e6 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
102e7 76 65 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  vel...*/..static
102e8 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 6c   int semLock(sql
102e9 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
102ea 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0d  nt eFileLock) {.
102eb 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
102ec 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
102ed 69 64 3b 0d 0a 20 20 69 6e 74 20 66 64 3b 0d 0a  id;..  int fd;..
102ee 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20    sem_t *pSem = 
102ef 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70  pFile->pInode->p
102f0 53 65 6d 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  Sem;..  int rc =
102f1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20   SQLITE_OK;.... 
102f2 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64   /* if we alread
102f3 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69  y have a lock, i
102f4 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20  t is exclusive. 
102f5 20 0d 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a   ..  ** Just adj
102f6 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75  ust level and pu
102f7 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65  nt on outta here
102f8 2e 20 2a 2f 0d 0a 20 20 69 66 20 28 70 46 69 6c  . */..  if (pFil
102f9 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3e 20 4e  e->eFileLock > N
102fa 4f 5f 4c 4f 43 4b 29 20 7b 0d 0a 20 20 20 20 70  O_LOCK) {..    p
102fb 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20  File->eFileLock 
102fc 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a 20 20  = eFileLock;..  
102fd 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
102fe 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f  ;..    goto sem_
102ff 65 6e 64 5f 6c 6f 63 6b 3b 0d 0a 20 20 7d 0d 0a  end_lock;..  }..
10300 20 20 0d 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65    ..  /* lock se
10301 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 75 74 20  maphore now but 
10302 62 61 69 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c  bail out when al
10303 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f  ready locked. */
10304 0d 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77  ..  if( sem_tryw
10305 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b  ait(pSem)==-1 ){
10306 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
10307 45 5f 42 55 53 59 3b 0d 0a 20 20 20 20 67 6f 74  E_BUSY;..    got
10308 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0d  o sem_end_lock;.
10309 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 67 6f 74  .  }....  /* got
1030a 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70   it, set the typ
1030b 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20  e and return ok 
1030c 2a 2f 0d 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69  */..  pFile->eFi
1030d 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f  leLock = eFileLo
1030e 63 6b 3b 0d 0a 0d 0a 20 73 65 6d 5f 65 6e 64 5f  ck;.... sem_end_
1030f 6c 6f 63 6b 3a 0d 0a 20 20 72 65 74 75 72 6e 20  lock:..  return 
10310 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
10311 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
10312 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
10313 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
10314 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20  e to eFileLock. 
10315 20 65 46 69 6c 65 4c 6f 63 6b 0d 0a 2a 2a 20 6d   eFileLock..** m
10316 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
10317 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
10318 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  LOCK...**..** If
10319 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
1031a 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
1031b 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
1031c 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
1031d 0d 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  ..** the request
1031e 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
1031f 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
10320 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a  s a no-op...*/..
10321 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e  static int semUn
10322 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
10323 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65  e *id, int eFile
10324 4c 6f 63 6b 29 20 7b 0d 0a 20 20 75 6e 69 78 46  Lock) {..  unixF
10325 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
10326 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 73  ixFile*)id;..  s
10327 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69  em_t *pSem = pFi
10328 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d  le->pInode->pSem
10329 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ;....  assert( p
1032a 46 69 6c 65 20 29 3b 0d 0a 20 20 61 73 73 65 72  File );..  asser
1032b 74 28 20 70 53 65 6d 20 29 3b 0d 0a 20 20 4f 53  t( pSem );..  OS
1032c 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 20  TRACE(("UNLOCK  
1032d 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64  %d %d was %d pid
1032e 3d 25 64 20 28 73 65 6d 29 5c 6e 22 2c 20 70 46  =%d (sem)\n", pF
1032f 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 4c 6f 63  ile->h, eFileLoc
10330 6b 2c 0d 0a 09 20 20 20 70 46 69 6c 65 2d 3e 65  k,...   pFile->e
10331 46 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64  FileLock, getpid
10332 28 29 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ()));..  assert(
10333 20 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52   eFileLock<=SHAR
10334 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 0d 0a  ED_LOCK );..  ..
10335 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
10336 73 73 69 62 6c 65 20 2a 2f 0d 0a 20 20 69 66 28  ssible */..  if(
10337 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
10338 6b 3d 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d  k==eFileLock ){.
10339 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1033a 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 0d  TE_OK;..  }..  .
1033b 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e  .  /* shared can
1033c 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63   just be set bec
1033d 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68  ause we always h
1033e 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ave an exclusive
1033f 20 2a 2f 0d 0a 20 20 69 66 20 28 65 46 69 6c 65   */..  if (eFile
10340 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  Lock==SHARED_LOC
10341 4b 29 20 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  K) {..    pFile-
10342 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69  >eFileLock = eFi
10343 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20 72 65 74  leLock;..    ret
10344 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
10345 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 6e 6f    }..  ..  /* no
10346 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e  , really unlock.
10347 20 2a 2f 0d 0a 20 20 69 66 20 28 20 73 65 6d 5f   */..  if ( sem_
10348 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29  post(pSem)==-1 )
10349 20 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 2c 20   {..    int rc, 
1034a 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d  tErrno = errno;.
1034b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1034c 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
1034d 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
1034e 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
1034f 3b 0d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  ;..    if( IS_LO
10350 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0d  CK_ERROR(rc) ){.
10351 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
10352 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
10353 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65  ;..    }..    re
10354 74 75 72 6e 20 72 63 3b 20 0d 0a 20 20 7d 0d 0a  turn rc; ..  }..
10355 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f    pFile->eFileLo
10356 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0d 0a 20  ck = NO_LOCK;.. 
10357 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10358 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 20 2a 2a  K;..}..../*.. **
10359 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0d 0a   Close a file...
1035a 20 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20   */..static int 
1035b 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  semClose(sqlite3
1035c 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0d 0a 20 20  _file *id) {..  
1035d 69 66 28 20 69 64 20 29 7b 0d 0a 20 20 20 20 75  if( id ){..    u
1035e 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
1035f 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d   (unixFile*)id;.
10360 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69  .    semUnlock(i
10361 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  d, NO_LOCK);..  
10362 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
10363 29 3b 0d 0a 20 20 20 20 75 6e 69 78 45 6e 74 65  );..    unixEnte
10364 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 20 20 72  rMutex();..    r
10365 65 6c 65 61 73 65 49 6e 6f 64 65 49 6e 66 6f 28  eleaseInodeInfo(
10366 70 46 69 6c 65 29 3b 0d 0a 20 20 20 20 75 6e 69  pFile);..    uni
10367 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a  xLeaveMutex();..
10368 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c      closeUnixFil
10369 65 28 69 64 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  e(id);..  }..  r
1036a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1036b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a  ..}....#endif /*
1036c 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0d 0a   OS_VXWORKS */..
1036d 2f 2a 0d 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d  /*..** Named sem
1036e 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69  aphore locking i
1036f 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
10370 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0d 0a 2a 2a   on VxWorks...**
10371 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
10372 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 6d  * End of the nam
10373 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
10374 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
10375 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
10376 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
10377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1037a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1037b 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  */....../*******
1037c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1037d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1037e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1037f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10380 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  *******..*******
10381 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10382 2a 2a 2a 2a 20 42 65 67 69 6e 20 41 46 50 20 4c  **** Begin AFP L
10383 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ocking *********
10384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10385 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
10386 20 41 46 50 20 69 73 20 74 68 65 20 41 70 70 6c   AFP is the Appl
10387 65 20 46 69 6c 69 6e 67 20 50 72 6f 74 6f 63 6f  e Filing Protoco
10388 6c 2e 20 20 41 46 50 20 69 73 20 61 20 6e 65 74  l.  AFP is a net
10389 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 20  work filesystem 
1038a 66 6f 75 6e 64 0d 0a 2a 2a 20 6f 6e 20 41 70 70  found..** on App
1038b 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 63 6f 6d  le Macintosh com
1038c 70 75 74 65 72 73 20 2d 20 62 6f 74 68 20 4f 53  puters - both OS
1038d 39 20 61 6e 64 20 4f 53 58 2e 0d 0a 2a 2a 0d 0a  9 and OSX...**..
1038e 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 69  ** Third-party i
1038f 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
10390 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c 61  f AFP are availa
10391 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 63  ble.  But this c
10392 6f 64 65 20 68 65 72 65 0d 0a 2a 2a 20 6f 6e 6c  ode here..** onl
10393 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0d  y works on OSX..
10394 0a 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e  .*/....#if defin
10395 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
10396 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
10397 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 2f 2a  OCKING_STYLE../*
10398 0d 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b  ..** The afpLock
10399 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63  ingContext struc
1039a 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c  ture contains al
1039b 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69  l afp lock speci
1039c 66 69 63 20 73 74 61 74 65 0d 0a 2a 2f 0d 0a 74  fic state..*/..t
1039d 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66  ypedef struct af
1039e 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
1039f 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
103a0 74 3b 0d 0a 73 74 72 75 63 74 20 61 66 70 4c 6f  t;..struct afpLo
103a1 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0d 0a  ckingContext {..
103a2 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 3b 0d    int reserved;.
103a3 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64  .  const char *d
103a4 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20  bPath;          
103a5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
103a6 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0d 0a  e open file */..
103a7 7d 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 42 79 74  };....struct Byt
103a8 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0d 0a 7b  eRangeLockPB2..{
103a9 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ..  unsigned lon
103aa 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20  g long offset;  
103ab 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20        /* offset 
103ac 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74 6f  to first byte to
103ad 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 75 6e 73 69   lock */..  unsi
103ae 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c  gned long long l
103af 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a  ength;        /*
103b0 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f   nbr of bytes to
103b1 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 75 6e 73 69   lock */..  unsi
103b2 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72  gned long long r
103b3 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a  etRangeStart; /*
103b4 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65   nbr of 1st byte
103b5 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65   locked if succe
103b6 73 73 66 75 6c 20 2a 2f 0d 0a 20 20 75 6e 73 69  ssful */..  unsi
103b7 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b  gned char unLock
103b8 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  Flag;         /*
103b9 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d   1 = unlock, 0 =
103ba 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 75 6e 73 69   lock */..  unsi
103bb 67 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45  gned char startE
103bc 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a  ndFlag;       /*
103bd 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66   1=rel to end of
103be 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20   fork, 0=rel to 
103bf 73 74 61 72 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  start */..  int 
103c0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
103c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
103c2 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73   file desc to as
103c3 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69  soc this lock wi
103c4 74 68 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 23 64 65  th */..};....#de
103c5 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61  fine afpfsByteRa
103c6 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20  ngeLock2FSCTL   
103c7 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20       _IOWR('z', 
103c8 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52  23, struct ByteR
103c9 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0d 0a 0d 0a  angeLockPB2)....
103ca 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  /*..** This is a
103cb 20 75 74 69 6c 69 74 79 20 66 6f 72 20 73 65 74   utility for set
103cc 74 69 6e 67 20 6f 72 20 63 6c 65 61 72 69 6e 67  ting or clearing
103cd 20 61 20 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63   a bit-range loc
103ce 6b 20 6f 6e 20 61 6e 0d 0a 2a 2a 20 41 46 50 20  k on an..** AFP 
103cf 66 69 6c 65 73 79 73 74 65 6d 2e 0d 0a 2a 2a 20  filesystem...** 
103d0 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
103d1 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
103d2 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 6e  , SQLITE_BUSY on
103d3 20 66 61 69 6c 75 72 65 2e 0d 0a 2a 2f 0d 0a 73   failure...*/..s
103d4 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 65 74  tatic int afpSet
103d5 4c 6f 63 6b 28 0d 0a 20 20 63 6f 6e 73 74 20 63  Lock(..  const c
103d6 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20  har *path,      
103d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
103d8 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  of the file to b
103d9 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c 6f  e locked or unlo
103da 63 6b 65 64 20 2a 2f 0d 0a 20 20 75 6e 69 78 46  cked */..  unixF
103db 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20  ile *pFile,     
103dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
103dd 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
103de 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0d 0a 20 20  r on path */..  
103df 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
103e0 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f  ng offset,     /
103e1 2a 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20  * First byte to 
103e2 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 20 20  be locked */..  
103e3 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
103e4 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f  ng length,     /
103e5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
103e6 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20  s to lock */..  
103e7 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20  int setLockFlag 
103e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
103e9 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 6c 6f  * True to set lo
103ea 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 6c  ck.  False to cl
103eb 65 61 72 20 6c 6f 63 6b 20 2a 2f 0d 0a 29 7b 0d  ear lock */..){.
103ec 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52 61  .  struct ByteRa
103ed 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0d 0a  ngeLockPB2 pb;..
103ee 20 20 69 6e 74 20 65 72 72 3b 0d 0a 20 20 0d 0a    int err;..  ..
103ef 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20    pb.unLockFlag 
103f0 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20  = setLockFlag ? 
103f1 30 20 3a 20 31 3b 0d 0a 20 20 70 62 2e 73 74 61  0 : 1;..  pb.sta
103f2 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b 0d 0a  rtEndFlag = 0;..
103f3 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f 66    pb.offset = of
103f4 66 73 65 74 3b 0d 0a 20 20 70 62 2e 6c 65 6e 67  fset;..  pb.leng
103f5 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0d 0a 20  th = length; .. 
103f6 20 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e   pb.fd = pFile->
103f7 68 3b 0d 0a 20 20 0d 0a 20 20 4f 53 54 52 41 43  h;..  ..  OSTRAC
103f8 45 28 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 5b  E(("AFPSETLOCK [
103f9 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 6e 20  %s] for %d%s in 
103fa 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c  range %llx:%llx\
103fb 6e 22 2c 20 0d 0a 20 20 20 20 28 73 65 74 4c 6f  n", ..    (setLo
103fc 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46  ckFlag?"ON":"OFF
103fd 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 70  "), pFile->h, (p
103fe 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 76  b.fd==-1?"[testv
103ff 61 6c 2d 31 5d 22 3a 22 22 29 2c 0d 0a 20 20 20  al-1]":""),..   
10400 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29   offset, length)
10401 29 3b 0d 0a 20 20 65 72 72 20 3d 20 66 73 63 74  );..  err = fsct
10402 6c 28 70 61 74 68 2c 20 61 66 70 66 73 42 79 74  l(path, afpfsByt
10403 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c  eRangeLock2FSCTL
10404 2c 20 26 70 62 2c 20 30 29 3b 0d 0a 20 20 69 66  , &pb, 0);..  if
10405 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0d 0a   ( err==-1 ) {..
10406 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20      int rc;..   
10407 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
10408 72 6e 6f 3b 0d 0a 20 20 20 20 4f 53 54 52 41 43  rno;..    OSTRAC
10409 45 28 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66  E(("AFPSETLOCK f
1040a 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29  ailed to fsctl()
1040b 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0d   '%s' %d %s\n",.
1040c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61  .             pa
1040d 74 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65  th, tErrno, stre
1040e 72 72 6f 72 28 74 45 72 72 6e 6f 29 29 29 3b 0d  rror(tErrno)));.
1040f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49  .#ifdef SQLITE_I
10410 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45  GNORE_AFP_LOCK_E
10411 52 52 4f 52 53 0d 0a 20 20 20 20 72 63 20 3d 20  RRORS..    rc = 
10412 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 23 65  SQLITE_BUSY;..#e
10413 6c 73 65 0d 0a 20 20 20 20 72 63 20 3d 20 73 71  lse..    rc = sq
10414 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
10415 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 0d  ixError(tErrno,.
10416 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10417 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 6c 61 67       setLockFlag
10418 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   ? SQLITE_IOERR_
10419 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  LOCK : SQLITE_IO
1041a 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0d 0a 23 65  ERR_UNLOCK);..#e
1041b 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49  ndif /* SQLITE_I
1041c 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45  GNORE_AFP_LOCK_E
1041d 52 52 4f 52 53 20 2a 2f 0d 0a 20 20 20 20 69 66  RRORS */..    if
1041e 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
1041f 72 63 29 20 29 7b 0d 0a 20 20 20 20 20 20 70 46  rc) ){..      pF
10420 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
10421 20 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 7d 0d   tErrno;..    }.
10422 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  .    return rc;.
10423 0a 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20  .  } else {..   
10424 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10425 4b 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  K;..  }..}..../*
10426 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
10427 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
10428 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
10429 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
1042a 20 73 70 65 63 69 66 69 65 64 0d 0a 2a 2a 20 66   specified..** f
1042b 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
1042c 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
1042d 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
1042e 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70   is held, set *p
1042f 52 65 73 4f 75 74 0d 0a 2a 2a 20 74 6f 20 61 20  ResOut..** to a 
10430 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
10431 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
10432 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
10433 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
10434 6c 75 65 0d 0a 2a 2a 20 69 73 20 73 65 74 20 74  lue..** is set t
10435 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65  o SQLITE_OK unle
10436 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  ss an I/O error 
10437 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f  occurs during lo
10438 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0d 0a 2a 2f  ck checking...*/
10439 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70  ..static int afp
1043a 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
1043b 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
1043c 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
1043d 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
1043e 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74  QLITE_OK;..  int
1043f 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0d 0a   reserved = 0;..
10440 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
10441 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
10442 64 3b 0d 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67  d;..  afpLocking
10443 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  Context *context
10444 3b 0d 0a 20 20 0d 0a 20 20 53 69 6d 75 6c 61 74  ;..  ..  Simulat
10445 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
10446 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
10447 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b  ECKRESERVEDLOCK;
10448 20 29 3b 0d 0a 20 20 0d 0a 20 20 61 73 73 65 72   );..  ..  asser
10449 74 28 20 70 46 69 6c 65 20 29 3b 0d 0a 20 20 63  t( pFile );..  c
1044a 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63  ontext = (afpLoc
1044b 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
1044c 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
1044d 74 65 78 74 3b 0d 0a 20 20 69 66 28 20 63 6f 6e  text;..  if( con
1044e 74 65 78 74 2d 3e 72 65 73 65 72 76 65 64 20 29  text->reserved )
1044f 7b 0d 0a 20 20 20 20 2a 70 52 65 73 4f 75 74 20  {..    *pResOut 
10450 3d 20 31 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  = 1;..    return
10451 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d   SQLITE_OK;..  }
10452 0d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ..  unixEnterMut
10453 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65  ex(); /* Because
10454 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20 69   pFile->pInode i
10455 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
10456 74 68 72 65 61 64 73 20 2a 2f 0d 0a 20 20 0d 0a  threads */..  ..
10457 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
10458 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
10459 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
1045a 68 20 61 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69  h a lock */..  i
1045b 66 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  f( pFile->pInode
1045c 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52  ->eFileLock>SHAR
1045d 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20  ED_LOCK ){..    
1045e 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0d 0a 20  reserved = 1;.. 
1045f 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 4f 74 68   }..  ..  /* Oth
10460 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
10461 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
10462 20 68 6f 6c 64 73 20 69 74 2e 0d 0a 20 20 20 2a   holds it...   *
10463 2f 0d 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  /..  if( !reserv
10464 65 64 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 6c 6f  ed ){..    /* lo
10465 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44 20  ck the RESERVED 
10466 62 79 74 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  byte */..    int
10467 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63   lrc = afpSetLoc
10468 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
10469 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
1046a 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20  ED_BYTE, 1,1);  
1046b 0d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ..    if( SQLITE
1046c 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0d 0a 20 20 20  _OK==lrc ){..   
1046d 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63     /* if we succ
1046e 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20  eeded in taking 
1046f 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63  the reserved loc
10470 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20  k, unlock it to 
10471 72 65 73 74 6f 72 65 0d 0a 20 20 20 20 20 20 2a  restore..      *
10472 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73  * the original s
10473 74 61 74 65 20 2a 2f 0d 0a 20 20 20 20 20 20 6c  tate */..      l
10474 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
10475 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
10476 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44   pFile, RESERVED
10477 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0d 0a 20  _BYTE, 1, 0);.. 
10478 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20     } else {..   
10479 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c     /* if we fail
1047a 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f  ed to get the lo
1047b 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20  ck then someone 
1047c 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69  else must have i
1047d 74 20 2a 2f 0d 0a 20 20 20 20 20 20 72 65 73 65  t */..      rese
1047e 72 76 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 7d  rved = 1;..    }
1047f 0d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  ..    if( IS_LOC
10480 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0d  K_ERROR(lrc) ){.
10481 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0d 0a  .      rc=lrc;..
10482 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d 0a      }..  }..  ..
10483 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
10484 28 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28  ();..  OSTRACE((
10485 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
10486 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e 22 2c   %d %d (afp)\n",
10487 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
10488 65 73 65 72 76 65 64 29 29 3b 0d 0a 20 20 0d 0a  eserved));..  ..
10489 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73    *pResOut = res
1048a 65 72 76 65 64 3b 0d 0a 20 20 72 65 74 75 72 6e  erved;..  return
1048b 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
1048c 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
1048d 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
1048e 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
1048f 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d  eter eFileLock -
10490 20 6f 6e 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20   one..** of the 
10491 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a  following:..**..
10492 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
10493 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20 28  D_LOCK..**     (
10494 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  2) RESERVED_LOCK
10495 0d 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  ..**     (3) PEN
10496 44 49 4e 47 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20  DING_LOCK..**   
10497 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
10498 4c 4f 43 4b 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d  LOCK..**..** Som
10499 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
1049a 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
1049b 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
1049c 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0d 0a 2a  l lock states..*
1049d 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
1049e 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
1049f 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
104a0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
104a1 20 6c 61 74 65 72 0d 0a 2a 2a 20 74 72 61 6e 73   later..** trans
104a2 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
104a3 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
104a4 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
104a5 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
104a6 0d 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  ..** still short
104a7 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
104a8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
104a9 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
104aa 6f 77 65 64 0d 0a 2a 2a 20 74 72 61 6e 73 69 74  owed..** transit
104ab 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
104ac 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
104ad 74 65 20 73 74 61 74 65 73 3a 0d 0a 2a 2a 0d 0a  te states:..**..
104ae 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
104af 3e 20 53 48 41 52 45 44 0d 0a 2a 2a 20 20 20 20  > SHARED..**    
104b0 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
104b1 45 44 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  ED..**    SHARED
104b2 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
104b3 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 20   EXCLUSIVE..**  
104b4 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50    RESERVED -> (P
104b5 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
104b6 53 49 56 45 0d 0a 2a 2a 20 20 20 20 50 45 4e 44  SIVE..**    PEND
104b7 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
104b8 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ..**..** This ro
104b9 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
104ba 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
104bb 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65    Use the sqlite
104bc 33 4f 73 55 6e 6c 6f 63 6b 28 29 0d 0a 2a 2a 20  3OsUnlock()..** 
104bd 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
104be 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
104bf 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
104c0 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65  t afpLock(sqlite
104c1 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
104c2 65 46 69 6c 65 4c 6f 63 6b 29 7b 0d 0a 20 20 69  eFileLock){..  i
104c3 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
104c4 4b 3b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  K;..  unixFile *
104c5 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
104c6 65 2a 29 69 64 3b 0d 0a 20 20 75 6e 69 78 49 6e  e*)id;..  unixIn
104c7 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 20  odeInfo *pInode 
104c8 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b  = pFile->pInode;
104c9 0d 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  ..  afpLockingCo
104ca 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d  ntext *context =
104cb 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74   (afpLockingCont
104cc 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f  ext *) pFile->lo
104cd 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a 20  ckingContext;.. 
104ce 20 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69   ..  assert( pFi
104cf 6c 65 20 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45  le );..  OSTRACE
104d0 28 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  (("LOCK    %d %s
104d1 20 77 61 73 20 25 73 28 25 73 2c 25 64 29 20 70   was %s(%s,%d) p
104d2 69 64 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20  id=%d (afp)\n", 
104d3 70 46 69 6c 65 2d 3e 68 2c 0d 0a 20 20 20 20 20  pFile->h,..     
104d4 20 20 20 20 20 20 61 7a 46 69 6c 65 4c 6f 63 6b        azFileLock
104d5 28 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 61 7a 46  (eFileLock), azF
104d6 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2d 3e 65  ileLock(pFile->e
104d7 46 69 6c 65 4c 6f 63 6b 29 2c 0d 0a 20 20 20 20  FileLock),..    
104d8 20 20 20 20 20 20 20 61 7a 46 69 6c 65 4c 6f 63         azFileLoc
104d9 6b 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c  k(pInode->eFileL
104da 6f 63 6b 29 2c 20 70 49 6e 6f 64 65 2d 3e 6e 53  ock), pInode->nS
104db 68 61 72 65 64 20 2c 20 67 65 74 70 69 64 28 29  hared , getpid()
104dc 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74  ));....  /* If t
104dd 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
104de 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
104df 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
104e0 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0d 0a  rictive on the..
104e1 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64    ** unixFile, d
104e2 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74  o nothing. Don't
104e3 20 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64   use the afp_end
104e4 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
104e5 2c 20 61 73 0d 0a 20 20 2a 2a 20 75 6e 69 78 45  , as..  ** unixE
104e6 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e  nterMutex() hasn
104e7 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79  't been called y
104e8 65 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  et...  */..  if(
104e9 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
104ea 6b 3e 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d  k>=eFileLock ){.
104eb 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 4c  .    OSTRACE(("L
104ec 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20  OCK    %d %s ok 
104ed 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 20 28  (already held) (
104ee 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  afp)\n", pFile->
104ef 68 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 61  h,..           a
104f0 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c  zFileLock(eFileL
104f1 6f 63 6b 29 29 29 3b 0d 0a 20 20 20 20 72 65 74  ock)));..    ret
104f2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
104f3 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65    }....  /* Make
104f4 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
104f5 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
104f6 72 72 65 63 74 0d 0a 20 20 2a 2a 20 20 28 31 29  rrect..  **  (1)
104f7 20 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66   We never move f
104f8 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20  rom unlocked to 
104f9 61 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20  anything higher 
104fa 74 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b  than shared lock
104fb 2e 0d 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c  ...  **  (2) SQL
104fc 69 74 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63  ite never explic
104fd 69 74 6c 79 20 72 65 71 75 65 73 74 73 20 61 20  itly requests a 
104fe 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0d 0a 20 20  pendig lock...  
104ff 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72 65 64  **  (3) A shared
10500 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79 73 20   lock is always 
10501 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65 73 65  held when a rese
10502 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65 71 75  rve lock is requ
10503 65 73 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ested...  */..  
10504 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 65  assert( pFile->e
10505 46 69 6c 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43  FileLock!=NO_LOC
10506 4b 20 7c 7c 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d  K || eFileLock==
10507 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a  SHARED_LOCK );..
10508 20 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c    assert( eFileL
10509 6f 63 6b 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ock!=PENDING_LOC
1050a 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  K );..  assert( 
1050b 65 46 69 6c 65 4c 6f 63 6b 21 3d 52 45 53 45 52  eFileLock!=RESER
1050c 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c  VED_LOCK || pFil
1050d 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48  e->eFileLock==SH
1050e 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20  ARED_LOCK );..  
1050f 0d 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65  ..  /* This mute
10510 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61  x is needed beca
10511 75 73 65 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  use pFile->pInod
10512 65 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f  e is shared acro
10513 73 73 20 74 68 72 65 61 64 73 0d 0a 20 20 2a 2f  ss threads..  */
10514 0d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ..  unixEnterMut
10515 65 78 28 29 3b 0d 0a 20 20 70 49 6e 6f 64 65 20  ex();..  pInode 
10516 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b  = pFile->pInode;
10517 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65  ....  /* If some
10518 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
10519 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63  is PID has a loc
1051a 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e  k via a differen
1051b 74 20 75 6e 69 78 46 69 6c 65 2a 0d 0a 20 20 2a  t unixFile*..  *
1051c 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72  * handle that pr
1051d 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75  ecludes the requ
1051e 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75  ested lock, retu
1051f 72 6e 20 42 55 53 59 2e 0d 0a 20 20 2a 2f 0d 0a  rn BUSY...  */..
10520 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 65 46    if( (pFile->eF
10521 69 6c 65 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d  ileLock!=pInode-
10522 3e 65 46 69 6c 65 4c 6f 63 6b 20 26 26 20 0d 0a  >eFileLock && ..
10523 20 20 20 20 20 20 20 28 70 49 6e 6f 64 65 2d 3e         (pInode->
10524 65 46 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49  eFileLock>=PENDI
10525 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c 65  NG_LOCK || eFile
10526 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  Lock>SHARED_LOCK
10527 29 29 0d 0a 20 20 20 20 20 29 7b 0d 0a 20 20 20  ))..     ){..   
10528 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
10529 59 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 61 66 70  Y;..    goto afp
1052a 5f 65 6e 64 5f 6c 6f 63 6b 3b 0d 0a 20 20 7d 0d  _end_lock;..  }.
1052b 0a 20 20 0d 0a 20 20 2f 2a 20 49 66 20 61 20 53  .  ..  /* If a S
1052c 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65  HARED lock is re
1052d 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d  quested, and som
1052e 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74  e thread using t
1052f 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0d  his PID already.
10530 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52  .  ** has a SHAR
10531 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c  ED or RESERVED l
10532 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d  ock, then increm
10533 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f  ent reference co
10534 75 6e 74 73 20 61 6e 64 0d 0a 20 20 2a 2a 20 72  unts and..  ** r
10535 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
10536 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 65 46  ..  */..  if( eF
10537 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f  ileLock==SHARED_
10538 4c 4f 43 4b 20 26 26 20 0d 0a 20 20 20 20 20 28  LOCK && ..     (
10539 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63  pInode->eFileLoc
1053a 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c  k==SHARED_LOCK |
1053b 7c 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c  | pInode->eFileL
1053c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ock==RESERVED_LO
1053d 43 4b 29 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  CK) ){..    asse
1053e 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53  rt( eFileLock==S
1053f 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20  HARED_LOCK );.. 
10540 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
10541 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29  ->eFileLock==0 )
10542 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
10543 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 30  Inode->nShared>0
10544 20 29 3b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e   );..    pFile->
10545 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52  eFileLock = SHAR
10546 45 44 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 70 49  ED_LOCK;..    pI
10547 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2b 2b 3b  node->nShared++;
10548 0d 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c  ..    pInode->nL
10549 6f 63 6b 2b 2b 3b 0d 0a 20 20 20 20 67 6f 74 6f  ock++;..    goto
1054a 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0d 0a   afp_end_lock;..
1054b 20 20 7d 0d 0a 20 20 20 20 0d 0a 20 20 2f 2a 20    }..    ..  /* 
1054c 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  A PENDING lock i
1054d 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20  s needed before 
1054e 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
1054f 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f  ED lock and befo
10550 72 65 0d 0a 20 20 2a 2a 20 61 63 71 75 69 72 69  re..  ** acquiri
10551 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  ng an EXCLUSIVE 
10552 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53  lock.  For the S
10553 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
10554 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0d 0a 20 20  PENDING will..  
10555 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0d  ** be released..
10556 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 65 46 69  .  */..  if( eFi
10557 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
10558 4f 43 4b 20 0d 0a 20 20 20 20 20 20 7c 7c 20 28  OCK ..      || (
10559 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55  eFileLock==EXCLU
1055a 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
1055b 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 50 45  le->eFileLock<PE
1055c 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0d 0a 20 20 29  NDING_LOCK)..  )
1055d 7b 0d 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  {..    int faile
1055e 64 3b 0d 0a 20 20 20 20 66 61 69 6c 65 64 20 3d  d;..    failed =
1055f 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
10560 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
10561 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  le, PENDING_BYTE
10562 2c 20 31 2c 20 31 29 3b 0d 0a 20 20 20 20 69 66  , 1, 1);..    if
10563 20 28 66 61 69 6c 65 64 29 20 7b 0d 0a 20 20 20   (failed) {..   
10564 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0d     rc = failed;.
10565 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f  .      goto afp_
10566 65 6e 64 5f 6c 6f 63 6b 3b 0d 0a 20 20 20 20 7d  end_lock;..    }
10567 0d 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20  ..  }..  ..  /* 
10568 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20  If control gets 
10569 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  to this point, t
1056a 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20  hen actually go 
1056b 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0d 0a  ahead and make..
1056c 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73    ** operating s
1056d 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20  ystem calls for 
1056e 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f  the specified lo
1056f 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ck...  */..  if(
10570 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52   eFileLock==SHAR
10571 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20  ED_LOCK ){..    
10572 69 6e 74 20 6c 72 63 31 2c 20 6c 72 63 32 2c 20  int lrc1, lrc2, 
10573 6c 72 63 31 45 72 72 6e 6f 20 3d 20 30 3b 0d 0a  lrc1Errno = 0;..
10574 20 20 20 20 6c 6f 6e 67 20 6c 6b 2c 20 6d 61 73      long lk, mas
10575 6b 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20 61 73  k;..    ..    as
10576 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53  sert( pInode->nS
10577 68 61 72 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 20  hared==0 );..   
10578 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d   assert( pInode-
10579 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b  >eFileLock==0 );
1057a 0d 0a 20 20 20 20 20 20 20 20 0d 0a 20 20 20 20  ..        ..    
1057b 6d 61 73 6b 20 3d 20 28 73 69 7a 65 6f 66 28 6c  mask = (sizeof(l
1057c 6f 6e 67 29 3d 3d 38 29 20 3f 20 4c 41 52 47 45  ong)==8) ? LARGE
1057d 53 54 5f 49 4e 54 36 34 20 3a 20 30 78 37 66 66  ST_INT64 : 0x7ff
1057e 66 66 66 66 66 3b 0d 0a 20 20 20 20 2f 2a 20 4e  fffff;..    /* N
1057f 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d  ow get the read-
10580 6c 6f 63 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b  lock SHARED_LOCK
10581 20 2a 2f 0d 0a 20 20 20 20 2f 2a 20 6e 6f 74 65   */..    /* note
10582 20 74 68 61 74 20 74 68 65 20 71 75 61 6c 69 74   that the qualit
10583 79 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 6e  y of the randomn
10584 65 73 73 20 64 6f 65 73 6e 27 74 20 6d 61 74 74  ess doesn't matt
10585 65 72 20 74 68 61 74 20 6d 75 63 68 20 2a 2f 0d  er that much */.
10586 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d  .    lk = random
10587 28 29 3b 20 0d 0a 20 20 20 20 70 49 6e 6f 64 65  (); ..    pInode
10588 2d 3e 73 68 61 72 65 64 42 79 74 65 20 3d 20 28  ->sharedByte = (
10589 6c 6b 20 26 20 6d 61 73 6b 29 25 28 53 48 41 52  lk & mask)%(SHAR
1058a 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0d 0a 20  ED_SIZE - 1);.. 
1058b 20 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 74     lrc1 = afpSet
1058c 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
1058d 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0d 0a 20  Path, pFile, .. 
1058e 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
1058f 46 49 52 53 54 2b 70 49 6e 6f 64 65 2d 3e 73 68  FIRST+pInode->sh
10590 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 3b  aredByte, 1, 1);
10591 0d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  ..    if( IS_LOC
10592 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b  K_ERROR(lrc1) ){
10593 0d 0a 20 20 20 20 20 20 6c 72 63 31 45 72 72 6e  ..      lrc1Errn
10594 6f 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  o = pFile->lastE
10595 72 72 6e 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rrno;..    }..  
10596 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65    /* Drop the te
10597 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20  mporary PENDING 
10598 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 6c 72 63  lock */..    lrc
10599 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  2 = afpSetLock(c
1059a 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
1059b 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42  pFile, PENDING_B
1059c 59 54 45 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 20  YTE, 1, 0);..   
1059d 20 0d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f   ..    if( IS_LO
1059e 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29  CK_ERROR(lrc1) )
1059f 20 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   {..      pFile-
105a0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63  >lastErrno = lrc
105a1 31 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 72  1Errno;..      r
105a2 63 20 3d 20 6c 72 63 31 3b 0d 0a 20 20 20 20 20  c = lrc1;..     
105a3 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
105a4 63 6b 3b 0d 0a 20 20 20 20 7d 20 65 6c 73 65 20  ck;..    } else 
105a5 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
105a6 52 28 6c 72 63 32 29 20 29 7b 0d 0a 20 20 20 20  R(lrc2) ){..    
105a7 20 20 72 63 20 3d 20 6c 72 63 32 3b 0d 0a 20 20    rc = lrc2;..  
105a8 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
105a9 5f 6c 6f 63 6b 3b 0d 0a 20 20 20 20 7d 20 65 6c  _lock;..    } el
105aa 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53  se if( lrc1 != S
105ab 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0d 0a 20 20  QLITE_OK ) {..  
105ac 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0d 0a      rc = lrc1;..
105ad 20 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20      } else {..  
105ae 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65      pFile->eFile
105af 4c 6f 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f  Lock = SHARED_LO
105b0 43 4b 3b 0d 0a 20 20 20 20 20 20 70 49 6e 6f 64  CK;..      pInod
105b1 65 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0d 0a 20 20 20  e->nLock++;..   
105b2 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72     pInode->nShar
105b3 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  ed = 1;..    }..
105b4 20 20 7d 65 6c 73 65 20 69 66 28 20 65 46 69 6c    }else if( eFil
105b5 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
105b6 5f 4c 4f 43 4b 20 26 26 20 70 49 6e 6f 64 65 2d  _LOCK && pInode-
105b7 3e 6e 53 68 61 72 65 64 3e 31 20 29 7b 0d 0a 20  >nShared>1 ){.. 
105b8 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 72 79     /* We are try
105b9 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 6c 75  ing for an exclu
105ba 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e  sive lock but an
105bb 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 6e 20  other thread in 
105bc 74 68 69 73 0d 0a 20 20 20 20 20 2a 2a 20 73 61  this..     ** sa
105bd 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74  me process is st
105be 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68  ill holding a sh
105bf 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0d 0a 20  ared lock. */.. 
105c0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
105c1 55 53 59 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  USY;..  }else{..
105c2 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65      /* The reque
105c3 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53  st was for a RES
105c4 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49  ERVED or EXCLUSI
105c5 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0d  VE lock.  It is.
105c6 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20  .    ** assumed 
105c7 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20  that there is a 
105c8 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65  SHARED or greate
105c9 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  r lock on the fi
105ca 6c 65 0d 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61  le..    ** alrea
105cb 64 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  dy...    */..   
105cc 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b   int failed = 0;
105cd 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21  ..    assert( 0!
105ce 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63  =pFile->eFileLoc
105cf 6b 20 29 3b 0d 0a 20 20 20 20 69 66 20 28 65 46  k );..    if (eF
105d0 69 6c 65 4c 6f 63 6b 20 3e 3d 20 52 45 53 45 52  ileLock >= RESER
105d1 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c  VED_LOCK && pFil
105d2 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3c 20 52  e->eFileLock < R
105d3 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0d  ESERVED_LOCK) {.
105d4 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75  .        /* Acqu
105d5 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c  ire a RESERVED l
105d6 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ock */..        
105d7 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c  failed = afpSetL
105d8 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
105d9 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45  ath, pFile, RESE
105da 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b  RVED_BYTE, 1,1);
105db 0d 0a 20 20 20 20 20 20 69 66 28 20 21 66 61 69  ..      if( !fai
105dc 6c 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  led ){..        
105dd 63 6f 6e 74 65 78 74 2d 3e 72 65 73 65 72 76 65  context->reserve
105de 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d  d = 1;..      }.
105df 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 20 28  .    }..    if (
105e0 21 66 61 69 6c 65 64 20 26 26 20 65 46 69 6c 65  !failed && eFile
105e1 4c 6f 63 6b 20 3d 3d 20 45 58 43 4c 55 53 49 56  Lock == EXCLUSIV
105e2 45 5f 4c 4f 43 4b 29 20 7b 0d 0a 20 20 20 20 20  E_LOCK) {..     
105e3 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45   /* Acquire an E
105e4 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f  XCLUSIVE lock */
105e5 0d 0a 20 20 20 20 20 20 20 20 0d 0a 20 20 20 20  ..        ..    
105e6 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
105e7 73 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f  shared lock befo
105e8 72 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61  re trying the ra
105e9 6e 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64  nge.  we'll need
105ea 20 74 6f 20 0d 0a 20 20 20 20 20 20 2a 2a 20 72   to ..      ** r
105eb 65 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73  eestablish the s
105ec 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65  hared lock if we
105ed 20 63 61 6e 27 74 20 67 65 74 20 74 68 65 20 20   can't get the  
105ee 61 66 70 55 6e 6c 6f 63 6b 0d 0a 20 20 20 20 20  afpUnlock..     
105ef 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 21   */..      if( !
105f0 28 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74  (failed = afpSet
105f1 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
105f2 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41  Path, pFile, SHA
105f3 52 45 44 5f 46 49 52 53 54 20 2b 0d 0a 20 20 20  RED_FIRST +..   
105f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105f5 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 73 68        pInode->sh
105f6 61 72 65 64 42 79 74 65 2c 20 31 2c 20 30 29 29  aredByte, 1, 0))
105f7 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74   ){..        int
105f8 20 66 61 69 6c 65 64 32 20 3d 20 53 51 4c 49 54   failed2 = SQLIT
105f9 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 2f  E_OK;..        /
105fa 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74  * now attemmpt t
105fb 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c 75 73  o get the exclus
105fc 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a  ive lock range *
105fd 2f 0d 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65  /..        faile
105fe 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
105ff 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
10600 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
10601 52 53 54 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  RST, ..         
10602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10603 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a        SHARED_SIZ
10604 45 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 20 20  E, 1);..        
10605 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 28 66  if( failed && (f
10606 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c  ailed2 = afpSetL
10607 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
10608 61 74 68 2c 20 70 46 69 6c 65 2c 20 0d 0a 20 20  ath, pFile, ..  
10609 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1060a 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53       SHARED_FIRS
1060b 54 20 2b 20 70 49 6e 6f 64 65 2d 3e 73 68 61 72  T + pInode->shar
1060c 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29  edByte, 1, 1)) )
1060d 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {..          /* 
1060e 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 73  Can't reestablis
1060f 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
10610 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 74  k.  Sqlite can't
10611 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0d 0a   deal, this is..
10612 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63            ** a c
10613 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f  ritical I/O erro
10614 72 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d  r..          */.
10615 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
10616 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54  ((failed & SQLIT
10617 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
10618 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c  TE_IOERR) ? fail
10619 65 64 32 20 3a 20 0d 0a 20 20 20 20 20 20 20 20  ed2 : ..        
1061a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f         SQLITE_IO
1061b 45 52 52 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20  ERR_LOCK;..     
1061c 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e       goto afp_en
1061d 64 5f 6c 6f 63 6b 3b 0d 0a 20 20 20 20 20 20 20  d_lock;..       
1061e 20 7d 20 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65   } ..      }else
1061f 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
10620 66 61 69 6c 65 64 3b 20 0d 0a 20 20 20 20 20 20  failed; ..      
10621 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  }..    }..    if
10622 28 20 66 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20  ( failed ){..   
10623 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0d     rc = failed;.
10624 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d  .    }..  }..  .
10625 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
10626 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 46 69  E_OK ){..    pFi
10627 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
10628 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20  eFileLock;..    
10629 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63  pInode->eFileLoc
1062a 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a  k = eFileLock;..
1062b 20 20 7d 65 6c 73 65 20 69 66 28 20 65 46 69 6c    }else if( eFil
1062c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
1062d 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 70 46  _LOCK ){..    pF
1062e 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d  ile->eFileLock =
1062f 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0d 0a   PENDING_LOCK;..
10630 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c      pInode->eFil
10631 65 4c 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f  eLock = PENDING_
10632 4c 4f 43 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 0d 0a  LOCK;..  }..  ..
10633 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0d 0a 20  afp_end_lock:.. 
10634 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
10635 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22  );..  OSTRACE(("
10636 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73  LOCK    %d %s %s
10637 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65   (afp)\n", pFile
10638 2d 3e 68 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28  ->h, azFileLock(
10639 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 0d 0a 20 20  eFileLock), ..  
1063a 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
1063b 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
1063c 61 69 6c 65 64 22 29 29 3b 0d 0a 20 20 72 65 74  ailed"));..  ret
1063d 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1063e 0d 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  ..** Lower the l
1063f 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
10640 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
10641 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f  pFile to eFileLo
10642 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0d 0a  ck.  eFileLock..
10643 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
10644 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
10645 52 45 44 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a  RED_LOCK...**..*
10646 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
10647 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
10648 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
10649 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
1064a 65 6c 6f 77 0d 0a 2a 2a 20 74 68 65 20 72 65 71  elow..** the req
1064b 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c  uested locking l
1064c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69  evel, this routi
1064d 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a  ne is a no-op...
1064e 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 61  */..static int a
1064f 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  fpUnlock(sqlite3
10650 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65  _file *id, int e
10651 46 69 6c 65 4c 6f 63 6b 29 20 7b 0d 0a 20 20 69  FileLock) {..  i
10652 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
10653 4b 3b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  K;..  unixFile *
10654 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
10655 65 2a 29 69 64 3b 0d 0a 20 20 75 6e 69 78 49 6e  e*)id;..  unixIn
10656 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b  odeInfo *pInode;
10657 0d 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  ..  afpLockingCo
10658 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d  ntext *context =
10659 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74   (afpLockingCont
1065a 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f  ext *) pFile->lo
1065b 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a 20  ckingContext;.. 
1065c 20 69 6e 74 20 73 6b 69 70 53 68 61 72 65 64 20   int skipShared 
1065d 3d 20 30 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c  = 0;..#ifdef SQL
1065e 49 54 45 5f 54 45 53 54 0d 0a 20 20 69 6e 74 20  ITE_TEST..  int 
1065f 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0d 0a 23  h = pFile->h;..#
10660 65 6e 64 69 66 0d 0a 0d 0a 20 20 61 73 73 65 72  endif....  asser
10661 74 28 20 70 46 69 6c 65 20 29 3b 0d 0a 20 20 4f  t( pFile );..  O
10662 53 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20  STRACE(("UNLOCK 
10663 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64   %d %d was %d(%d
10664 2c 25 64 29 20 70 69 64 3d 25 64 20 28 61 66 70  ,%d) pid=%d (afp
10665 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
10666 65 46 69 6c 65 4c 6f 63 6b 2c 0d 0a 20 20 20 20  eFileLock,..    
10667 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 46         pFile->eF
10668 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d 3e  ileLock, pFile->
10669 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63  pInode->eFileLoc
1066a 6b 2c 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  k, pFile->pInode
1066b 2d 3e 6e 53 68 61 72 65 64 2c 0d 0a 20 20 20 20  ->nShared,..    
1066c 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29         getpid())
1066d 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  );....  assert( 
1066e 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45  eFileLock<=SHARE
1066f 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 69 66 28  D_LOCK );..  if(
10670 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
10671 6b 3c 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0d  k<=eFileLock ){.
10672 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10673 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 75  TE_OK;..  }..  u
10674 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
10675 0d 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 46 69  ..  pInode = pFi
10676 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0d 0a 20 20 61  le->pInode;..  a
10677 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e  ssert( pInode->n
10678 53 68 61 72 65 64 21 3d 30 20 29 3b 0d 0a 20 20  Shared!=0 );..  
10679 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  if( pFile->eFile
1067a 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  Lock>SHARED_LOCK
1067b 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
1067c 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f   pInode->eFileLo
1067d 63 6b 3d 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65  ck==pFile->eFile
1067e 4c 6f 63 6b 20 29 3b 0d 0a 20 20 20 20 53 69 6d  Lock );..    Sim
1067f 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
10680 67 6e 28 31 29 3b 0d 0a 20 20 20 20 53 69 6d 75  gn(1);..    Simu
10681 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28  lateIOError( h=(
10682 2d 31 29 20 29 0d 0a 20 20 20 20 53 69 6d 75 6c  -1) )..    Simul
10683 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
10684 28 30 29 3b 0d 0a 20 20 20 20 0d 0a 23 69 66 6e  (0);..    ..#ifn
10685 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20 20 20  def NDEBUG..    
10686 2f 2a 20 57 68 65 6e 20 72 65 64 75 63 69 6e 67  /* When reducing
10687 20 61 20 6c 6f 63 6b 20 73 75 63 68 20 74 68 61   a lock such tha
10688 74 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  t other processe
10689 73 20 63 61 6e 20 73 74 61 72 74 0d 0a 20 20 20  s can start..   
1068a 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20   ** reading the 
1068b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 67  database file ag
1068c 61 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  ain, make sure t
1068d 68 61 74 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  hat the..    ** 
1068e 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e  transaction coun
1068f 74 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20  ter was updated 
10690 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74  if any part of t
10691 68 65 20 64 61 74 61 62 61 73 65 0d 0a 20 20 20  he database..   
10692 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 65 64   ** file changed
10693 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61  .  If the transa
10694 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73  ction counter is
10695 20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0d 0a 20   not updated,.. 
10696 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e     ** other conn
10697 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73  ections to the s
10698 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 20 6e  ame file might n
10699 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 74 0d  ot realize that.
1069a 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c 65  .    ** the file
1069b 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e 64   has changed and
1069c 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74   hence might not
1069d 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 74   know to flush t
1069e 68 65 69 72 0d 0a 20 20 20 20 2a 2a 20 63 61 63  heir..    ** cac
1069f 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  he.  The use of 
106a0 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61  a stale cache ca
106a1 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61  n lead to databa
106a2 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0d 0a  se corruption...
106a3 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65      */..    asse
106a4 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72  rt( pFile->inNor
106a5 6d 61 6c 57 72 69 74 65 3d 3d 30 0d 0a 20 20 20  malWrite==0..   
106a6 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65          || pFile
106a7 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0d 0a 20  ->dbUpdate==0.. 
106a8 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69            || pFi
106a9 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e  le->transCntrChn
106aa 67 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 70 46 69  g==1 );..    pFi
106ab 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74  le->inNormalWrit
106ac 65 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a  e = 0;..#endif..
106ad 20 20 20 20 0d 0a 20 20 20 20 69 66 28 20 70 46      ..    if( pF
106ae 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d  ile->eFileLock==
106af 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
106b0 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66  {..      rc = af
106b1 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
106b2 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
106b3 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53   SHARED_FIRST, S
106b4 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0d  HARED_SIZE, 0);.
106b5 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
106b6 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 46 69  QLITE_OK && (eFi
106b7 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
106b8 4f 43 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 6e  OCK || pInode->n
106b9 53 68 61 72 65 64 3e 31 29 20 29 7b 0d 0a 20 20  Shared>1) ){..  
106ba 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65        /* only re
106bb 2d 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73  -establish the s
106bc 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65  hared lock if ne
106bd 63 65 73 73 61 72 79 20 2a 2f 0d 0a 20 20 20 20  cessary */..    
106be 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f      int sharedLo
106bf 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f  ckByte = SHARED_
106c0 46 49 52 53 54 2b 70 49 6e 6f 64 65 2d 3e 73 68  FIRST+pInode->sh
106c1 61 72 65 64 42 79 74 65 3b 0d 0a 20 20 20 20 20  aredByte;..     
106c2 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f     rc = afpSetLo
106c3 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61  ck(context->dbPa
106c4 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65  th, pFile, share
106c5 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29  dLockByte, 1, 1)
106c6 3b 0d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  ;..      } else 
106c7 7b 0d 0a 20 20 20 20 20 20 20 20 73 6b 69 70 53  {..        skipS
106c8 68 61 72 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20  hared = 1;..    
106c9 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
106ca 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
106cb 4b 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c  K && pFile->eFil
106cc 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c  eLock>=PENDING_L
106cd 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  OCK ){..      rc
106ce 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f   = afpSetLock(co
106cf 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70  ntext->dbPath, p
106d0 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59  File, PENDING_BY
106d1 54 45 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 20 20  TE, 1, 0);..    
106d2 7d 20 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  } ..    if( rc==
106d3 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69  SQLITE_OK && pFi
106d4 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 3d 52  le->eFileLock>=R
106d5 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20  ESERVED_LOCK && 
106d6 63 6f 6e 74 65 78 74 2d 3e 72 65 73 65 72 76 65  context->reserve
106d7 64 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  d ){..      rc =
106d8 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
106d9 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
106da 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
106db 45 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 20 20 20  E, 1, 0);..     
106dc 20 69 66 28 20 21 72 63 20 29 7b 20 0d 0a 20 20   if( !rc ){ ..  
106dd 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 72        context->r
106de 65 73 65 72 76 65 64 20 3d 20 30 3b 20 0d 0a 20  eserved = 0; .. 
106df 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
106e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
106e1 45 5f 4f 4b 20 26 26 20 28 65 46 69 6c 65 4c 6f  E_OK && (eFileLo
106e2 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck==SHARED_LOCK 
106e3 7c 7c 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72  || pInode->nShar
106e4 65 64 3e 31 29 29 7b 0d 0a 20 20 20 20 20 20 70  ed>1)){..      p
106e5 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  Inode->eFileLock
106e6 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0d   = SHARED_LOCK;.
106e7 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69  .    }..  }..  i
106e8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
106e9 20 26 26 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 4e   && eFileLock==N
106ea 4f 5f 4c 4f 43 4b 20 29 7b 0d 0a 0d 0a 20 20 20  O_LOCK ){....   
106eb 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68   /* Decrement th
106ec 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f  e shared lock co
106ed 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20  unter.  Release 
106ee 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61  the lock using a
106ef 6e 0d 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c  n..    ** OS cal
106f0 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20  l only when all 
106f1 74 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20  threads in this 
106f2 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76  same process hav
106f3 65 20 72 65 6c 65 61 73 65 64 0d 0a 20 20 20 20  e released..    
106f4 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0d 0a 20 20  ** the lock...  
106f5 20 20 2a 2f 0d 0a 20 20 20 20 75 6e 73 69 67 6e    */..    unsign
106f6 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61  ed long long sha
106f7 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48  redLockByte = SH
106f8 41 52 45 44 5f 46 49 52 53 54 2b 70 49 6e 6f 64  ARED_FIRST+pInod
106f9 65 2d 3e 73 68 61 72 65 64 42 79 74 65 3b 0d 0a  e->sharedByte;..
106fa 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61      pInode->nSha
106fb 72 65 64 2d 2d 3b 0d 0a 20 20 20 20 69 66 28 20  red--;..    if( 
106fc 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3d  pInode->nShared=
106fd 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 53 69 6d  =0 ){..      Sim
106fe 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
106ff 67 6e 28 31 29 3b 0d 0a 20 20 20 20 20 20 53 69  gn(1);..      Si
10700 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68  mulateIOError( h
10701 3d 28 2d 31 29 20 29 0d 0a 20 20 20 20 20 20 53  =(-1) )..      S
10702 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
10703 6e 69 67 6e 28 30 29 3b 0d 0a 20 20 20 20 20 20  nign(0);..      
10704 69 66 28 20 21 73 6b 69 70 53 68 61 72 65 64 20  if( !skipShared 
10705 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
10706 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
10707 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
10708 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79  le, sharedLockBy
10709 74 65 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 20 20  te, 1, 0);..    
1070a 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 21    }..      if( !
1070b 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  rc ){..        p
1070c 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  Inode->eFileLock
1070d 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0d 0a 20 20 20   = NO_LOCK;..   
1070e 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c       pFile->eFil
1070f 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  eLock = NO_LOCK;
10710 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
10711 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
10712 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
10713 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2d    pInode->nLock-
10714 2d 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  -;..      assert
10715 28 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3e  ( pInode->nLock>
10716 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  =0 );..      if(
10717 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d   pInode->nLock==
10718 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6c  0 ){..        cl
10719 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46  osePendingFds(pF
1071a 69 6c 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ile);..      }..
1071b 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d 0a      }..  }..  ..
1071c 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
1071d 28 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  ();..  if( rc==S
1071e 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65  QLITE_OK ) pFile
1071f 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46  ->eFileLock = eF
10720 69 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 72 65 74 75  ileLock;..  retu
10721 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
10722 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
10723 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73   & cleanup AFP s
10724 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20  pecific locking 
10725 63 6f 6e 74 65 78 74 20 0d 0a 2a 2f 0d 0a 73 74  context ..*/..st
10726 61 74 69 63 20 69 6e 74 20 61 66 70 43 6c 6f 73  atic int afpClos
10727 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
10728 69 64 29 20 7b 0d 0a 20 20 69 6e 74 20 72 63 20  id) {..  int rc 
10729 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
1072a 69 66 28 20 69 64 20 29 7b 0d 0a 20 20 20 20 75  if( id ){..    u
1072b 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
1072c 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d   (unixFile*)id;.
1072d 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69  .    afpUnlock(i
1072e 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  d, NO_LOCK);..  
1072f 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
10730 28 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 46 69  ();..    if( pFi
10731 6c 65 2d 3e 70 49 6e 6f 64 65 20 26 26 20 70 46  le->pInode && pF
10732 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c 6f  ile->pInode->nLo
10733 63 6b 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  ck ){..      /* 
10734 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74  If there are out
10735 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20  standing locks, 
10736 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  do not actually 
10737 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a  close the file j
10738 75 73 74 0d 0a 20 20 20 20 20 20 2a 2a 20 79 65  ust..      ** ye
10739 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77  t because that w
1073a 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65  ould clear those
1073b 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64   locks.  Instead
1073c 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0d 0a  , add the file..
1073d 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
1073e 74 6f 72 20 74 6f 20 70 49 6e 6f 64 65 2d 3e 61  tor to pInode->a
1073f 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c  Pending.  It wil
10740 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
10741 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0d 0a  ly closed when..
10742 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73        ** the las
10743 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65  t lock is cleare
10744 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  d...      */..  
10745 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 64      setPendingFd
10746 28 70 46 69 6c 65 29 3b 0d 0a 20 20 20 20 7d 0d  (pFile);..    }.
10747 0a 20 20 20 20 72 65 6c 65 61 73 65 49 6e 6f 64  .    releaseInod
10748 65 49 6e 66 6f 28 70 46 69 6c 65 29 3b 0d 0a 20  eInfo(pFile);.. 
10749 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1074a 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
1074b 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 72 63 20  ntext);..    rc 
1074c 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28  = closeUnixFile(
1074d 69 64 29 3b 0d 0a 20 20 20 20 75 6e 69 78 4c 65  id);..    unixLe
1074e 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 20 20 7d  aveMutex();..  }
1074f 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
10750 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64  }....#endif /* d
10751 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
10752 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  ) && SQLITE_ENAB
10753 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
10754 20 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20   */../*..** The 
10755 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74 68  code above is th
10756 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65  e AFP lock imple
10757 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 20  mentation.  The 
10758 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 69 63  code is specific
10759 0d 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61  ..** to MacOSX a
1075a 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  nd does not work
1075b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70   on other unix p
1075c 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c  latforms.  No al
1075d 74 65 72 6e 61 74 69 76 65 0d 0a 2a 2a 20 69 73  ternative..** is
1075e 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20   available.  If 
1075f 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c  you don't compil
10760 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65  e for a mac, the
10761 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22  n the "unix-afp"
10762 0d 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20  ..** VFS is not 
10763 61 76 61 69 6c 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a  available...**..
10764 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10765 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65  ***** End of the
10766 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d   AFP lock implem
10767 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
10768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
10769 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1076a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1076b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1076c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1076d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1076e 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
1076f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10771 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10773 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***..***********
10774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10775 20 42 65 67 69 6e 20 4e 46 53 20 4c 6f 63 6b 69   Begin NFS Locki
10776 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
10777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10778 2a 2a 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69  ***/....#if defi
10779 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
1077a 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
1077b 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 2f  LOCKING_STYLE../
1077c 2a 0d 0a 20 2a 2a 20 4c 6f 77 65 72 20 74 68 65  *.. ** Lower the
1077d 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
1077e 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
1077f 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65  r pFile to eFile
10780 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b  Lock.  eFileLock
10781 0d 0a 20 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  .. ** must be ei
10782 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20  ther NO_LOCK or 
10783 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0d 0a 20 2a  SHARED_LOCK... *
10784 2a 0d 0a 20 2a 2a 20 49 66 20 74 68 65 20 6c 6f  *.. ** If the lo
10785 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
10786 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
10787 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
10788 20 6f 72 20 62 65 6c 6f 77 0d 0a 20 2a 2a 20 74   or below.. ** t
10789 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
1078a 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
1078b 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1078c 2d 6f 70 2e 0d 0a 20 2a 2f 0d 0a 73 74 61 74 69  -op... */..stati
1078d 63 20 69 6e 74 20 6e 66 73 55 6e 6c 6f 63 6b 28  c int nfsUnlock(
1078e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
1078f 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29  , int eFileLock)
10790 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 6f 73 69  {..  return posi
10791 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46 69 6c  xUnlock(id, eFil
10792 65 4c 6f 63 6b 2c 20 31 29 3b 0d 0a 7d 0d 0a 0d  eLock, 1);..}...
10793 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
10794 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
10795 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
10796 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0d  OCKING_STYLE */.
10797 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ./*..** The code
10798 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 4e 46   above is the NF
10799 53 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  S lock implement
1079a 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65  ation.  The code
1079b 20 69 73 20 73 70 65 63 69 66 69 63 0d 0a 2a 2a   is specific..**
1079c 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64   to MacOSX and d
1079d 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
1079e 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66  other unix platf
1079f 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e  orms.  No altern
107a0 61 74 69 76 65 0d 0a 2a 2a 20 69 73 20 61 76 61  ative..** is ava
107a1 69 6c 61 62 6c 65 2e 20 20 0d 0a 2a 2a 0d 0a 2a  ilable.  ..**..*
107a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107a3 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
107a4 4e 46 53 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  NFS lock impleme
107a5 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
107a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
107a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
107ac 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
107ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107b1 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
107b2 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67  **** Non-locking
107b3 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
107b4 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  thods **********
107b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107b6 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ***..**..** The 
107b7 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f  next division co
107b8 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74  ntains implement
107b9 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d  ations for all m
107ba 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0d 0a  ethods of the ..
107bb 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ** sqlite3_file 
107bc 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61  object other tha
107bd 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65  n the locking me
107be 74 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b  thods.  The lock
107bf 69 6e 67 0d 0a 2a 2a 20 6d 65 74 68 6f 64 73 20  ing..** methods 
107c0 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20  were defined in 
107c1 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20  divisions above 
107c2 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  (one locking met
107c3 68 6f 64 20 70 65 72 0d 0a 2a 2a 20 64 69 76 69  hod per..** divi
107c4 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65  sion).  Those me
107c5 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 63  thods that are c
107c6 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63  ommon to all loc
107c7 6b 69 6e 67 20 6d 6f 64 65 73 0d 0a 2a 2a 20 61  king modes..** a
107c8 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68  re gather togeth
107c9 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76  er into this div
107ca 69 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a  ision...*/..../*
107cb 0d 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65  ..** Seek to the
107cc 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 61   offset passed a
107cd 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
107ce 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64  ument, then read
107cf 20 63 6e 74 20 0d 0a 2a 2a 20 62 79 74 65 73 20   cnt ..** bytes 
107d0 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72  into pBuf. Retur
107d1 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
107d2 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
107d3 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 42 3a  ead...**..** NB:
107d4 20 20 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20    If you define 
107d5 55 53 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45  USE_PREAD or USE
107d6 5f 50 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69  _PREAD64, then i
107d7 74 20 6d 69 67 68 74 20 61 6c 73 6f 0d 0a 2a 2a  t might also..**
107d8 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f   be necessary to
107d9 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53   define _XOPEN_S
107da 4f 55 52 43 45 20 74 6f 20 62 65 20 35 30 30 2e  OURCE to be 500.
107db 20 20 54 68 69 73 20 76 61 72 69 65 73 20 66 72    This varies fr
107dc 6f 6d 0d 0a 2a 2a 20 6f 6e 65 20 73 79 73 74 65  om..** one syste
107dd 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20 53  m to another.  S
107de 69 6e 63 65 20 53 51 4c 69 74 65 20 64 6f 65 73  ince SQLite does
107df 20 6e 6f 74 20 64 65 66 69 6e 65 20 55 53 45 5f   not define USE_
107e0 50 52 45 41 44 0d 0a 2a 2a 20 61 6e 79 20 61 6e  PREAD..** any an
107e1 79 20 66 6f 72 6d 20 62 79 20 64 65 66 61 75 6c  y form by defaul
107e2 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74 20 61  t, we will not a
107e3 74 74 65 6d 70 74 20 74 6f 20 64 65 66 69 6e 65  ttempt to define
107e4 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 2e 0d   _XOPEN_SOURCE..
107e5 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 73 20  .** See tickets 
107e6 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 31 2e  #2741 and #2681.
107e7 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 61 76 6f 69  ..**..** To avoi
107e8 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65  d stomping the e
107e9 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20  rrno value on a 
107ea 66 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20  failed read the 
107eb 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0d  lastErrno value.
107ec 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72  .** is set befor
107ed 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2f  e returning...*/
107ee 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  ..static int see
107ef 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c  kAndRead(unixFil
107f0 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69  e *id, sqlite3_i
107f1 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69  nt64 offset, voi
107f2 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74  d *pBuf, int cnt
107f3 29 7b 0d 0a 20 20 69 6e 74 20 67 6f 74 3b 0d 0a  ){..  int got;..
107f4 20 20 69 6e 74 20 70 72 69 6f 72 20 3d 20 30 3b    int prior = 0;
107f5 0d 0a 23 69 66 20 28 21 64 65 66 69 6e 65 64 28  ..#if (!defined(
107f6 55 53 45 5f 50 52 45 41 44 29 20 26 26 20 21 64  USE_PREAD) && !d
107f7 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44  efined(USE_PREAD
107f8 36 34 29 29 0d 0a 20 20 69 36 34 20 6e 65 77 4f  64))..  i64 newO
107f9 66 66 73 65 74 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ffset;..#endif..
107fa 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0d 0a    TIMER_START;..
107fb 20 20 64 6f 7b 0d 0a 23 69 66 20 64 65 66 69 6e    do{..#if defin
107fc 65 64 28 55 53 45 5f 50 52 45 41 44 29 0d 0a 20  ed(USE_PREAD).. 
107fd 20 20 20 67 6f 74 20 3d 20 6f 73 50 72 65 61 64     got = osPread
107fe 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
107ff 74 2c 20 6f 66 66 73 65 74 29 3b 0d 0a 20 20 20  t, offset);..   
10800 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
10801 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0d 0a 23  ( got = -1 );..#
10802 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 53 45  elif defined(USE
10803 5f 50 52 45 41 44 36 34 29 0d 0a 20 20 20 20 67  _PREAD64)..    g
10804 6f 74 20 3d 20 6f 73 50 72 65 61 64 36 34 28 69  ot = osPread64(i
10805 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c  d->h, pBuf, cnt,
10806 20 6f 66 66 73 65 74 29 3b 0d 0a 20 20 20 20 53   offset);..    S
10807 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
10808 67 6f 74 20 3d 20 2d 31 20 29 3b 0d 0a 23 65 6c  got = -1 );..#el
10809 73 65 0d 0a 20 20 20 20 6e 65 77 4f 66 66 73 65  se..    newOffse
1080a 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c  t = lseek(id->h,
1080b 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45   offset, SEEK_SE
1080c 54 29 3b 0d 0a 20 20 20 20 53 69 6d 75 6c 61 74  T);..    Simulat
1080d 65 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66  eIOError( newOff
1080e 73 65 74 2d 2d 20 29 3b 0d 0a 20 20 20 20 69 66  set-- );..    if
1080f 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66  ( newOffset!=off
10810 73 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  set ){..      if
10811 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d  ( newOffset == -
10812 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 28 28  1 ){..        ((
10813 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
10814 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
10815 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
10816 0a 20 20 20 20 20 20 20 20 28 28 75 6e 69 78 46  .        ((unixF
10817 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
10818 72 6e 6f 20 3d 20 30 3b 09 09 09 0d 0a 20 20 20  rno = 0;.....   
10819 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74 75     }..      retu
1081a 72 6e 20 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  rn -1;..    }.. 
1081b 20 20 20 67 6f 74 20 3d 20 6f 73 52 65 61 64 28     got = osRead(
1081c 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74  id->h, pBuf, cnt
1081d 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  );..#endif..    
1081e 69 66 28 20 67 6f 74 3d 3d 63 6e 74 20 29 20 62  if( got==cnt ) b
1081f 72 65 61 6b 3b 0d 0a 20 20 20 20 69 66 28 20 67  reak;..    if( g
10820 6f 74 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 69  ot<0 ){..      i
10821 66 28 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20  f( errno==EINTR 
10822 29 7b 20 67 6f 74 20 3d 20 31 3b 20 63 6f 6e 74  ){ got = 1; cont
10823 69 6e 75 65 3b 20 7d 0d 0a 20 20 20 20 20 20 70  inue; }..      p
10824 72 69 6f 72 20 3d 20 30 3b 0d 0a 20 20 20 20 20  rior = 0;..     
10825 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
10826 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
10827 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  rno;..      brea
10828 6b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  k;..    }else if
10829 28 20 67 6f 74 3e 30 20 29 7b 0d 0a 20 20 20 20  ( got>0 ){..    
1082a 20 20 63 6e 74 20 2d 3d 20 67 6f 74 3b 0d 0a 20    cnt -= got;.. 
1082b 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 67       offset += g
1082c 6f 74 3b 0d 0a 20 20 20 20 20 20 70 72 69 6f 72  ot;..      prior
1082d 20 2b 3d 20 67 6f 74 3b 0d 0a 20 20 20 20 20 20   += got;..      
1082e 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 28 67  pBuf = (void*)(g
1082f 6f 74 20 2b 20 28 63 68 61 72 2a 29 70 42 75 66  ot + (char*)pBuf
10830 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 77 68  );..    }..  }wh
10831 69 6c 65 28 20 67 6f 74 3e 30 20 29 3b 0d 0a 20  ile( got>0 );.. 
10832 20 54 49 4d 45 52 5f 45 4e 44 3b 0d 0a 20 20 4f   TIMER_END;..  O
10833 53 54 52 41 43 45 28 28 22 52 45 41 44 20 20 20  STRACE(("READ   
10834 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20   %-3d %5d %7lld 
10835 25 6c 6c 75 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  %llu\n",..      
10836 20 20 20 20 20 20 69 64 2d 3e 68 2c 20 67 6f 74        id->h, got
10837 2b 70 72 69 6f 72 2c 20 6f 66 66 73 65 74 2d 70  +prior, offset-p
10838 72 69 6f 72 2c 20 54 49 4d 45 52 5f 45 4c 41 50  rior, TIMER_ELAP
10839 53 45 44 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e  SED));..  return
1083a 20 67 6f 74 2b 70 72 69 6f 72 3b 0d 0a 7d 0d 0a   got+prior;..}..
1083b 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 61 64 20 64 61  ../*..** Read da
1083c 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69  ta from a file i
1083d 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52  nto a buffer.  R
1083e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
1083f 69 66 20 61 6c 6c 0d 0a 2a 2a 20 62 79 74 65 73  if all..** bytes
10840 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65   were read succe
10841 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49  ssfully and SQLI
10842 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74  TE_IOERR if anyt
10843 68 69 6e 67 20 67 6f 65 73 0d 0a 2a 2a 20 77 72  hing goes..** wr
10844 6f 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ong...*/..static
10845 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28 0d 0a   int unixRead(..
10846 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
10847 69 64 2c 20 0d 0a 20 20 76 6f 69 64 20 2a 70 42  id, ..  void *pB
10848 75 66 2c 20 0d 0a 20 20 69 6e 74 20 61 6d 74 2c  uf, ..  int amt,
10849 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
1084a 34 20 6f 66 66 73 65 74 0d 0a 29 7b 0d 0a 20 20  4 offset..){..  
1084b 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
1084c 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64  = (unixFile *)id
1084d 3b 0d 0a 20 20 69 6e 74 20 67 6f 74 3b 0d 0a 20  ;..  int got;.. 
1084e 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0d 0a   assert( id );..
1084f 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
10850 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  s a database fil
10851 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c  e (not a journal
10852 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  , master-journal
10853 20 6f 72 20 74 65 6d 70 0d 0a 20 20 2a 2a 20 66   or temp..  ** f
10854 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20  ile), the bytes 
10855 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72  in the locking r
10856 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  ange should neve
10857 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  r be read or wri
10858 74 74 65 6e 2e 20 2a 2f 0d 0a 23 69 66 20 30 0d  tten. */..#if 0.
10859 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
1085a 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0d 0a 20 20  ->pUnused==0..  
1085b 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d       || offset>=
1085c 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32  PENDING_BYTE+512
1085d 0d 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73  ..       || offs
1085e 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f  et+amt<=PENDING_
1085f 42 59 54 45 20 0d 0a 20 20 29 3b 0d 0a 23 65 6e  BYTE ..  );..#en
10860 64 69 66 0d 0a 0d 0a 20 20 67 6f 74 20 3d 20 73  dif....  got = s
10861 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65  eekAndRead(pFile
10862 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20  , offset, pBuf, 
10863 61 6d 74 29 3b 0d 0a 20 20 69 66 28 20 67 6f 74  amt);..  if( got
10864 3d 3d 61 6d 74 20 29 7b 0d 0a 20 20 20 20 72 65  ==amt ){..    re
10865 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
10866 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74  .  }else if( got
10867 3c 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 6c 61  <0 ){..    /* la
10868 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73  stErrno set by s
10869 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0d 0a 20  eekAndRead */.. 
1086a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1086b 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0d 0a 20 20  _IOERR_READ;..  
1086c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 46 69 6c  }else{..    pFil
1086d 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  e->lastErrno = 0
1086e 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65  ; /* not a syste
1086f 6d 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 20 20  m error */..    
10870 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 20  /* Unread parts 
10871 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d 75  of the buffer mu
10872 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  st be zero-fille
10873 64 20 2a 2f 0d 0a 20 20 20 20 6d 65 6d 73 65 74  d */..    memset
10874 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b  (&((char*)pBuf)[
10875 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74  got], 0, amt-got
10876 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  );..    return S
10877 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
10878 54 5f 52 45 41 44 3b 0d 0a 20 20 7d 0d 0a 7d 0d  T_READ;..  }..}.
10879 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 65 6b 20 74  .../*..** Seek t
1087a 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20  o the offset in 
1087b 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20  id->offset then 
1087c 72 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 69  read cnt bytes i
1087d 6e 74 6f 20 70 42 75 66 2e 0d 0a 2a 2a 20 52 65  nto pBuf...** Re
1087e 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1087f 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c  of bytes actuall
10880 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 65 20  y read.  Update 
10881 74 68 65 20 6f 66 66 73 65 74 2e 0d 0a 2a 2a 0d  the offset...**.
10882 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f  .** To avoid sto
10883 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20  mping the errno 
10884 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65  value on a faile
10885 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 74  d write the last
10886 45 72 72 6e 6f 20 76 61 6c 75 65 0d 0a 2a 2a 20  Errno value..** 
10887 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65  is set before re
10888 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74  turning...*/..st
10889 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
1088a 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a  Write(unixFile *
1088b 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20  id, i64 offset, 
1088c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
1088d 2c 20 69 6e 74 20 63 6e 74 29 7b 0d 0a 20 20 69  , int cnt){..  i
1088e 6e 74 20 67 6f 74 3b 0d 0a 23 69 66 20 28 21 64  nt got;..#if (!d
1088f 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44  efined(USE_PREAD
10890 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 55 53  ) && !defined(US
10891 45 5f 50 52 45 41 44 36 34 29 29 0d 0a 20 20 69  E_PREAD64))..  i
10892 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0d 0a 23  64 newOffset;..#
10893 65 6e 64 69 66 0d 0a 20 20 54 49 4d 45 52 5f 53  endif..  TIMER_S
10894 54 41 52 54 3b 0d 0a 23 69 66 20 64 65 66 69 6e  TART;..#if defin
10895 65 64 28 55 53 45 5f 50 52 45 41 44 29 0d 0a 20  ed(USE_PREAD).. 
10896 20 64 6f 7b 20 67 6f 74 20 3d 20 6f 73 50 77 72   do{ got = osPwr
10897 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  ite(id->h, pBuf,
10898 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 20 7d   cnt, offset); }
10899 77 68 69 6c 65 28 20 67 6f 74 3c 30 20 26 26 20  while( got<0 && 
1089a 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b 0d  errno==EINTR );.
1089b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55  .#elif defined(U
1089c 53 45 5f 50 52 45 41 44 36 34 29 0d 0a 20 20 64  SE_PREAD64)..  d
1089d 6f 7b 20 67 6f 74 20 3d 20 6f 73 50 77 72 69 74  o{ got = osPwrit
1089e 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  e64(id->h, pBuf,
1089f 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 7d 77   cnt, offset);}w
108a0 68 69 6c 65 28 20 67 6f 74 3c 30 20 26 26 20 65  hile( got<0 && e
108a1 72 72 6e 6f 3d 3d 45 49 4e 54 52 29 3b 0d 0a 23  rrno==EINTR);..#
108a2 65 6c 73 65 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20  else..  do{..   
108a3 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65   newOffset = lse
108a4 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74  ek(id->h, offset
108a5 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0d 0a 20 20  , SEEK_SET);..  
108a6 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
108a7 72 28 20 6e 65 77 4f 66 66 73 65 74 2d 2d 20 29  r( newOffset-- )
108a8 3b 0d 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66  ;..    if( newOf
108a9 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0d  fset!=offset ){.
108aa 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 4f 66  .      if( newOf
108ab 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0d 0a 20  fset == -1 ){.. 
108ac 20 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c         ((unixFil
108ad 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e  e*)id)->lastErrn
108ae 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20  o = errno;..    
108af 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
108b0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
108b1 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  )->lastErrno = 0
108b2 3b 09 09 09 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ;.....      }.. 
108b3 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0d       return -1;.
108b4 0a 20 20 20 20 7d 0d 0a 20 20 20 20 67 6f 74 20  .    }..    got 
108b5 3d 20 6f 73 57 72 69 74 65 28 69 64 2d 3e 68 2c  = osWrite(id->h,
108b6 20 70 42 75 66 2c 20 63 6e 74 29 3b 0d 0a 20 20   pBuf, cnt);..  
108b7 7d 77 68 69 6c 65 28 20 67 6f 74 3c 30 20 26 26  }while( got<0 &&
108b8 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b   errno==EINTR );
108b9 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 54 49 4d 45  ..#endif..  TIME
108ba 52 5f 45 4e 44 3b 0d 0a 20 20 69 66 28 20 67 6f  R_END;..  if( go
108bb 74 3c 30 20 29 7b 0d 0a 20 20 20 20 28 28 75 6e  t<0 ){..    ((un
108bc 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
108bd 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d  tErrno = errno;.
108be 0a 20 20 7d 0d 0a 0d 0a 20 20 4f 53 54 52 41 43  .  }....  OSTRAC
108bf 45 28 28 22 57 52 49 54 45 20 20 20 25 2d 33 64  E(("WRITE   %-3d
108c0 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c   %5d %7lld %llu\
108c1 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c 20  n", id->h, got, 
108c2 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 4c  offset, TIMER_EL
108c3 41 50 53 45 44 29 29 3b 0d 0a 20 20 72 65 74 75  APSED));..  retu
108c4 72 6e 20 67 6f 74 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  rn got;..}......
108c5 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  /*..** Write dat
108c6 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
108c7 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65  into a file.  Re
108c8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
108c9 6e 20 73 75 63 63 65 73 73 0d 0a 2a 2a 20 6f 72  n success..** or
108ca 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
108cb 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
108cc 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
108cd 6e 74 20 75 6e 69 78 57 72 69 74 65 28 0d 0a 20  nt unixWrite(.. 
108ce 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
108cf 64 2c 20 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d, ..  const voi
108d0 64 20 2a 70 42 75 66 2c 20 0d 0a 20 20 69 6e 74  d *pBuf, ..  int
108d1 20 61 6d 74 2c 0d 0a 20 20 73 71 6c 69 74 65 33   amt,..  sqlite3
108d2 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 0d 0a  _int64 offset ..
108d3 29 7b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ){..  unixFile *
108d4 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
108d5 65 2a 29 69 64 3b 0d 0a 20 20 69 6e 74 20 77 72  e*)id;..  int wr
108d6 6f 74 65 20 3d 20 30 3b 0d 0a 20 20 61 73 73 65  ote = 0;..  asse
108d7 72 74 28 20 69 64 20 29 3b 0d 0a 20 20 61 73 73  rt( id );..  ass
108d8 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0d 0a 0d  ert( amt>0 );...
108d9 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
108da 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
108db 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c   (not a journal,
108dc 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
108dd 6f 72 20 74 65 6d 70 0d 0a 20 20 2a 2a 20 66 69  or temp..  ** fi
108de 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20 69  le), the bytes i
108df 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61  n the locking ra
108e0 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  nge should never
108e1 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74   be read or writ
108e2 74 65 6e 2e 20 2a 2f 0d 0a 23 69 66 20 30 0d 0a  ten. */..#if 0..
108e3 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
108e4 3e 70 55 6e 75 73 65 64 3d 3d 30 0d 0a 20 20 20  >pUnused==0..   
108e5 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50      || offset>=P
108e6 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0d  ENDING_BYTE+512.
108e7 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65  .       || offse
108e8 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42  t+amt<=PENDING_B
108e9 59 54 45 20 0d 0a 20 20 29 3b 0d 0a 23 65 6e 64  YTE ..  );..#end
108ea 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44  if....#ifndef ND
108eb 45 42 55 47 0d 0a 20 20 2f 2a 20 49 66 20 77 65  EBUG..  /* If we
108ec 20 61 72 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72   are doing a nor
108ed 6d 61 6c 20 77 72 69 74 65 20 74 6f 20 61 20 64  mal write to a d
108ee 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 73  atabase file (as
108ef 20 6f 70 70 6f 73 65 64 20 74 6f 0d 0a 20 20 2a   opposed to..  *
108f0 2a 20 64 6f 69 6e 67 20 61 20 68 6f 74 2d 6a 6f  * doing a hot-jo
108f1 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f  urnal rollback o
108f2 72 20 61 20 77 72 69 74 65 20 74 6f 20 73 6f 6d  r a write to som
108f3 65 20 66 69 6c 65 20 6f 74 68 65 72 20 74 68 61  e file other tha
108f4 6e 20 61 0d 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c  n a..  ** normal
108f5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20   database file) 
108f6 74 68 65 6e 20 72 65 63 6f 72 64 20 74 68 65 20  then record the 
108f7 66 61 63 74 20 74 68 61 74 20 74 68 65 20 64 61  fact that the da
108f8 74 61 62 61 73 65 0d 0a 20 20 2a 2a 20 68 61 73  tabase..  ** has
108f9 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 74 68   changed.  If th
108fa 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
108fb 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 66 69 65  unter is modifie
108fc 64 2c 20 72 65 63 6f 72 64 20 74 68 61 74 0d 0a  d, record that..
108fd 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e 0d 0a    ** fact too...
108fe 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46 69 6c    */..  if( pFil
108ff 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65  e->inNormalWrite
10900 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e   ){..    pFile->
10901 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f  dbUpdate = 1;  /
10902 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68  * The database h
10903 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
10904 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 6f 66 66   */..    if( off
10905 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65  set<=24 && offse
10906 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0d 0a 20 20  t+amt>=27 ){..  
10907 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20      int rc;..   
10908 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b     char oldCntr[
10909 34 5d 3b 0d 0a 20 20 20 20 20 20 53 69 6d 75 6c  4];..      Simul
1090a 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
1090b 28 31 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  (1);..      rc =
1090c 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69   seekAndRead(pFi
1090d 6c 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c  le, 24, oldCntr,
1090e 20 34 29 3b 0d 0a 20 20 20 20 20 20 53 69 6d 75   4);..      Simu
1090f 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
10910 6e 28 30 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  n(0);..      if(
10911 20 72 63 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70   rc!=4 || memcmp
10912 28 6f 6c 64 43 6e 74 72 2c 20 26 28 28 63 68 61  (oldCntr, &((cha
10913 72 2a 29 70 42 75 66 29 5b 32 34 2d 6f 66 66 73  r*)pBuf)[24-offs
10914 65 74 5d 2c 20 34 29 21 3d 30 20 29 7b 0d 0a 20  et], 4)!=0 ){.. 
10915 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 74 72         pFile->tr
10916 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b  ansCntrChng = 1;
10917 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63    /* The transac
10918 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 68 61 73  tion counter has
10919 20 63 68 61 6e 67 65 64 20 2a 2f 0d 0a 20 20 20   changed */..   
1091a 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
1091b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 77 68  ..#endif....  wh
1091c 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77  ile( amt>0 && (w
1091d 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72  rote = seekAndWr
1091e 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 65  ite(pFile, offse
1091f 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e 30  t, pBuf, amt))>0
10920 20 29 7b 0d 0a 20 20 20 20 61 6d 74 20 2d 3d 20   ){..    amt -= 
10921 77 72 6f 74 65 3b 0d 0a 20 20 20 20 6f 66 66 73  wrote;..    offs
10922 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0d 0a 20 20  et += wrote;..  
10923 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72    pBuf = &((char
10924 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0d  *)pBuf)[wrote];.
10925 0a 20 20 7d 0d 0a 20 20 53 69 6d 75 6c 61 74 65  .  }..  Simulate
10926 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d  IOError(( wrote=
10927 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0d  (-1), amt=1 ));.
10928 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
10929 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65  ullError(( wrote
1092a 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0d 0a 0d  =0, amt=1 ));...
1092b 0a 20 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0d  .  if( amt>0 ){.
1092c 0a 20 20 20 20 69 66 28 20 77 72 6f 74 65 3c 30  .    if( wrote<0
1092d 20 26 26 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45   && pFile->lastE
1092e 72 72 6e 6f 21 3d 45 4e 4f 53 50 43 20 29 7b 0d  rrno!=ENOSPC ){.
1092f 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72  .      /* lastEr
10930 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41  rno set by seekA
10931 6e 64 57 72 69 74 65 20 2a 2f 0d 0a 20 20 20 20  ndWrite */..    
10932 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10933 49 4f 45 52 52 5f 57 52 49 54 45 3b 0d 0a 20 20  IOERR_WRITE;..  
10934 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
10935 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
10936 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73   = 0; /* not a s
10937 79 73 74 65 6d 20 65 72 72 6f 72 20 2a 2f 0d 0a  ystem error */..
10938 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10939 49 54 45 5f 46 55 4c 4c 3b 0d 0a 20 20 20 20 7d  ITE_FULL;..    }
1093a 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
1093b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
1093c 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
1093d 5f 54 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f  _TEST../*..** Co
1093e 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
1093f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20  f fullsyncs and 
10940 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54  normal syncs.  T
10941 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74  his is used to t
10942 65 73 74 0d 0a 2a 2a 20 74 68 61 74 20 73 79 6e  est..** that syn
10943 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73  cs and fullsyncs
10944 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61   are occurring a
10945 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
10946 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  s...*/..SQLITE_A
10947 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
10948 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a  ync_count = 0;..
10949 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1094a 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
1094b 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a 23 65 6e 64  count = 0;..#end
1094c 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 65 20  if..../*..** We 
1094d 64 6f 20 6e 6f 74 20 74 72 75 73 74 20 73 79 73  do not trust sys
1094e 74 65 6d 73 20 74 6f 20 70 72 6f 76 69 64 65 20  tems to provide 
1094f 61 20 77 6f 72 6b 69 6e 67 20 66 64 61 74 61 73  a working fdatas
10950 79 6e 63 28 29 2e 20 20 53 6f 6d 65 20 64 6f 2e  ync().  Some do.
10951 0d 0a 2a 2a 20 4f 74 68 65 72 73 20 64 6f 20 6e  ..** Others do n
10952 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 65 2c 20  o.  To be safe, 
10953 77 65 20 77 69 6c 6c 20 73 74 69 63 6b 20 77 69  we will stick wi
10954 74 68 20 74 68 65 20 28 73 6c 69 67 68 74 6c 79  th the (slightly
10955 20 73 6c 6f 77 65 72 29 0d 0a 2a 2a 20 66 73 79   slower)..** fsy
10956 6e 63 28 29 2e 20 49 66 20 79 6f 75 20 6b 6e 6f  nc(). If you kno
10957 77 20 74 68 61 74 20 79 6f 75 72 20 73 79 73 74  w that your syst
10958 65 6d 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20  em does support 
10959 66 64 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72  fdatasync() corr
1095a 65 63 74 6c 79 2c 0d 0a 2a 2a 20 74 68 65 6e 20  ectly,..** then 
1095b 73 69 6d 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77  simply compile w
1095c 69 74 68 20 2d 44 66 64 61 74 61 73 79 6e 63 3d  ith -Dfdatasync=
1095d 66 64 61 74 61 73 79 6e 63 0d 0a 2a 2f 0d 0a 23  fdatasync..*/..#
1095e 69 66 20 21 64 65 66 69 6e 65 64 28 66 64 61 74  if !defined(fdat
1095f 61 73 79 6e 63 29 0d 0a 23 20 64 65 66 69 6e 65  async)..# define
10960 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63   fdatasync fsync
10961 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
10962 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46  ** Define HAVE_F
10963 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72  ULLFSYNC to 0 or
10964 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20   1 depending on 
10965 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0d 0a  whether or not..
10966 2a 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59  ** the F_FULLFSY
10967 4e 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  NC macro is defi
10968 6e 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e  ned.  F_FULLFSYN
10969 43 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0d 0a  C is currently..
1096a 2a 2a 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  ** only availabl
1096b 65 20 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20  e on Mac OS X.  
1096c 42 75 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63  But that could c
1096d 68 61 6e 67 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64  hange...*/..#ifd
1096e 65 66 20 46 5f 46 55 4c 4c 46 53 59 4e 43 0d 0a  ef F_FULLFSYNC..
1096f 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f 46 55  # define HAVE_FU
10970 4c 4c 46 53 59 4e 43 20 31 0d 0a 23 65 6c 73 65  LLFSYNC 1..#else
10971 0d 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f  ..# define HAVE_
10972 46 55 4c 4c 46 53 59 4e 43 20 30 0d 0a 23 65 6e  FULLFSYNC 0..#en
10973 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  dif....../*..** 
10974 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74  The fsync() syst
10975 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74  em call does not
10976 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69   work as adverti
10977 73 65 64 20 6f 6e 20 6d 61 6e 79 0d 0a 2a 2a 20  sed on many..** 
10978 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54  unix systems.  T
10979 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f  he following pro
1097a 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74  cedure is an att
1097b 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0d 0a 2a 2a  empt to make..**
1097c 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e   it work better.
1097d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 53 51 4c  ..**..** The SQL
1097e 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72  ITE_NO_SYNC macr
1097f 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66  o disables all f
10980 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69  sync()s.  This i
10981 73 20 75 73 65 66 75 6c 0d 0a 2a 2a 20 66 6f 72  s useful..** for
10982 20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65   testing when we
10983 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72   want to run thr
10984 6f 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75  ough the test su
10985 69 74 65 20 71 75 69 63 6b 6c 79 2e 0d 0a 2a 2a  ite quickly...**
10986 20 59 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c   You are strongl
10987 79 20 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20  y advised *not* 
10988 74 6f 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53  to deploy with S
10989 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0d 0a 2a  QLITE_NO_SYNC..*
1098a 2a 20 65 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76  * enabled, howev
1098b 65 72 2c 20 73 69 6e 63 65 20 77 69 74 68 20 53  er, since with S
1098c 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e  QLITE_NO_SYNC en
1098d 61 62 6c 65 64 2c 20 61 6e 20 4f 53 20 63 72 61  abled, an OS cra
1098e 73 68 0d 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20  sh..** or power 
1098f 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 6c 69 6b  failure will lik
10990 65 6c 79 20 63 6f 72 72 75 70 74 20 74 68 65 20  ely corrupt the 
10991 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
10992 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 73 65  **..** SQLite se
10993 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c 79 20  ts the dataOnly 
10994 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 7a 65  flag if the size
10995 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   of the file is 
10996 75 6e 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a 20 54  unchanged...** T
10997 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 64  he idea behind d
10998 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 20  ataOnly is that 
10999 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 77  it should only w
1099a 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 6f  rite the file co
1099b 6e 74 65 6e 74 0d 0a 2a 2a 20 74 6f 20 64 69 73  ntent..** to dis
1099c 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65  k, not the inode
1099d 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64  .  We only set d
1099e 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66  ataOnly if the f
1099f 69 6c 65 20 73 69 7a 65 20 69 73 20 0d 0a 2a 2a  ile size is ..**
109a0 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65   unchanged since
109a1 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69   the file size i
109a2 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e  s part of the in
109a3 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0d  ode.  However, .
109a4 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c  .** Ted Ts'o tel
109a5 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61  ls us that fdata
109a6 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f  sync() will also
109a7 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65   write the inode
109a8 20 69 66 20 74 68 65 0d 0a 2a 2a 20 66 69 6c 65   if the..** file
109a9 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65   size has change
109aa 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61  d.  The only rea
109ab 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  l difference bet
109ac 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29  ween fdatasync()
109ad 0d 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29  ..** and fsync()
109ae 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20  , Ted tells us, 
109af 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e  is that fdatasyn
109b0 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75  c() will not flu
109b1 73 68 20 74 68 65 0d 0a 2a 2a 20 69 6e 6f 64 65  sh the..** inode
109b2 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72   if the mtime or
109b3 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20   owner or other 
109b4 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73  inode attributes
109b5 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0d 0a   have changed...
109b6 2a 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20  ** We only care 
109b7 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73  about the file s
109b8 69 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68  ize, not the oth
109b9 65 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 74  er file attribut
109ba 65 73 2c 20 73 6f 0d 0a 2a 2a 20 61 73 20 66 61  es, so..** as fa
109bb 72 20 61 73 20 53 51 4c 69 74 65 20 69 73 20 63  r as SQLite is c
109bc 6f 6e 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61  oncerned, an fda
109bd 74 61 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61  tasync() is alwa
109be 79 73 20 61 64 65 71 75 61 74 65 2e 0d 0a 2a 2a  ys adequate...**
109bf 20 53 6f 2c 20 77 65 20 61 6c 77 61 79 73 20 75   So, we always u
109c0 73 65 20 66 64 61 74 61 73 79 6e 63 28 29 20 69  se fdatasync() i
109c1 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  f it is availabl
109c2 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  e, regardless of
109c3 0d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  ..** the value o
109c4 66 20 74 68 65 20 64 61 74 61 4f 6e 6c 79 20 66  f the dataOnly f
109c5 6c 61 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  lag...*/..static
109c6 20 69 6e 74 20 66 75 6c 6c 5f 66 73 79 6e 63 28   int full_fsync(
109c7 69 6e 74 20 66 64 2c 20 69 6e 74 20 66 75 6c 6c  int fd, int full
109c8 53 79 6e 63 2c 20 69 6e 74 20 64 61 74 61 4f 6e  Sync, int dataOn
109c9 6c 79 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  ly){..  int rc;.
109ca 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ...  /* The foll
109cb 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 6c 69  owing "ifdef/eli
109cc 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b 20 68  f/else/" block h
109cd 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 72 75  as the same stru
109ce 63 74 75 72 65 20 61 73 0d 0a 20 20 2a 2a 20 74  cture as..  ** t
109cf 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 74  he one below. It
109d0 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 68   is replicated h
109d1 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 76  ere solely to av
109d2 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 0d  oid cluttering .
109d3 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
109d4 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
109d5 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
109d6 28 29 20 6d 61 63 72 6f 73 2e 0d 0a 20 20 2a 2f  () macros...  */
109d7 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
109d8 4e 4f 5f 53 59 4e 43 0d 0a 20 20 55 4e 55 53 45  NO_SYNC..  UNUSE
109d9 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b  D_PARAMETER(fd);
109da 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
109db 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0d  ETER(fullSync);.
109dc 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
109dd 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0d 0a  TER(dataOnly);..
109de 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  #elif HAVE_FULLF
109df 53 59 4e 43 0d 0a 20 20 55 4e 55 53 45 44 5f 50  SYNC..  UNUSED_P
109e0 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c  ARAMETER(dataOnl
109e1 79 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 55 4e  y);..#else..  UN
109e2 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66  USED_PARAMETER(f
109e3 75 6c 6c 53 79 6e 63 29 3b 0d 0a 20 20 55 4e 55  ullSync);..  UNU
109e4 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 61  SED_PARAMETER(da
109e5 74 61 4f 6e 6c 79 29 3b 0d 0a 23 65 6e 64 69 66  taOnly);..#endif
109e6 0d 0a 0d 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20  ....  /* Record 
109e7 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
109e8 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f 20 61  mes that we do a
109e9 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20   normal fsync() 
109ea 61 6e 64 20 0d 0a 20 20 2a 2a 20 46 55 4c 4c 53  and ..  ** FULLS
109eb 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75 73  YNC.  This is us
109ec 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e  ed during testin
109ed 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  g to verify that
109ee 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 0d   this procedure.
109ef 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65  .  ** gets calle
109f0 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65  d with the corre
109f1 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0d 0a 20  ct arguments... 
109f2 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
109f3 54 45 5f 54 45 53 54 0d 0a 20 20 69 66 28 20 66  TE_TEST..  if( f
109f4 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65  ullSync ) sqlite
109f5 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
109f6 2b 2b 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73  ++;..  sqlite3_s
109f7 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0d 0a 23 65  ync_count++;..#e
109f8 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  ndif....  /* If 
109f9 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  we compiled with
109fa 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   the SQLITE_NO_S
109fb 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73  YNC flag, then s
109fc 79 6e 63 69 6e 67 20 69 73 20 61 0d 0a 20 20 2a  yncing is a..  *
109fd 2a 20 6e 6f 2d 6f 70 0d 0a 20 20 2a 2f 0d 0a 23  * no-op..  */..#
109fe 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  ifdef SQLITE_NO_
109ff 53 59 4e 43 0d 0a 20 20 72 63 20 3d 20 53 51 4c  SYNC..  rc = SQL
10a00 49 54 45 5f 4f 4b 3b 0d 0a 23 65 6c 69 66 20 48  ITE_OK;..#elif H
10a01 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0d 0a 20  AVE_FULLFSYNC.. 
10a02 20 69 66 28 20 66 75 6c 6c 53 79 6e 63 20 29 7b   if( fullSync ){
10a03 0d 0a 20 20 20 20 72 63 20 3d 20 6f 73 46 63 6e  ..    rc = osFcn
10a04 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 59  tl(fd, F_FULLFSY
10a05 4e 43 2c 20 30 29 3b 0d 0a 20 20 7d 65 6c 73 65  NC, 0);..  }else
10a06 7b 0d 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a  {..    rc = 1;..
10a07 20 20 7d 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
10a08 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65   FULLFSYNC faile
10a09 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  d, fall back to 
10a0a 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73  attempting an fs
10a0b 79 6e 63 28 29 2e 0d 0a 20 20 2a 2a 20 49 74 20  ync()...  ** It 
10a0c 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73  shouldn't be pos
10a0d 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73  sible for fullfs
10a0e 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74  ync to fail on t
10a0f 68 65 20 6c 6f 63 61 6c 20 0d 0a 20 20 2a 2a 20  he local ..  ** 
10a10 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20  file system (on 
10a11 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65  OSX), so failure
10a12 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
10a13 46 55 4c 4c 46 53 59 4e 43 0d 0a 20 20 2a 2a 20  FULLFSYNC..  ** 
10a14 69 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20  isn't supported 
10a15 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 79  for this file sy
10a16 73 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70  stem. So, attemp
10a17 74 20 61 6e 20 66 73 79 6e 63 20 0d 0a 20 20 2a  t an fsync ..  *
10a18 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20  * and (for now) 
10a19 69 67 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68  ignore the overh
10a1a 65 61 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c  ead of a superfl
10a1b 75 6f 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e  uous fcntl call.
10a1c 20 20 0d 0a 20 20 2a 2a 20 49 74 27 64 20 62 65    ..  ** It'd be
10a1d 20 62 65 74 74 65 72 20 74 6f 20 64 65 74 65 63   better to detec
10a1e 74 20 66 75 6c 6c 66 73 79 6e 63 20 73 75 70 70  t fullfsync supp
10a1f 6f 72 74 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f  ort once and avo
10a20 69 64 20 0d 0a 20 20 2a 2a 20 74 68 65 20 66 63  id ..  ** the fc
10a21 6e 74 6c 20 63 61 6c 6c 20 65 76 65 72 79 20 74  ntl call every t
10a22 69 6d 65 20 73 79 6e 63 20 69 73 20 63 61 6c 6c  ime sync is call
10a23 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ed...  */..  if(
10a24 20 72 63 20 29 20 72 63 20 3d 20 66 73 79 6e 63   rc ) rc = fsync
10a25 28 66 64 29 3b 0d 0a 0d 0a 23 65 6c 69 66 20 64  (fd);....#elif d
10a26 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
10a27 29 0d 0a 20 20 2f 2a 20 66 64 61 74 61 73 79 6e  )..  /* fdatasyn
10a28 63 28 29 20 6f 6e 20 48 46 53 2b 20 64 6f 65 73  c() on HFS+ does
10a29 6e 27 74 20 79 65 74 20 66 6c 75 73 68 20 74 68  n't yet flush th
10a2a 65 20 66 69 6c 65 20 73 69 7a 65 20 69 66 20 69  e file size if i
10a2b 74 20 63 68 61 6e 67 65 64 20 63 6f 72 72 65 63  t changed correc
10a2c 74 6c 79 0d 0a 20 20 2a 2a 20 73 6f 20 63 75 72  tly..  ** so cur
10a2d 72 65 6e 74 6c 79 20 77 65 20 64 65 66 61 75 6c  rently we defaul
10a2e 74 20 74 6f 20 74 68 65 20 6d 61 63 72 6f 20 74  t to the macro t
10a2f 68 61 74 20 72 65 64 65 66 69 6e 65 73 20 66 64  hat redefines fd
10a30 61 74 61 73 79 6e 63 20 74 6f 20 66 73 79 6e 63  atasync to fsync
10a31 0d 0a 20 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 66  ..  */..  rc = f
10a32 73 79 6e 63 28 66 64 29 3b 0d 0a 23 65 6c 73 65  sync(fd);..#else
10a33 20 0d 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73   ..  rc = fdatas
10a34 79 6e 63 28 66 64 29 3b 0d 0a 23 69 66 20 4f 53  ync(fd);..#if OS
10a35 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 69 66 28 20  _VXWORKS..  if( 
10a36 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d  rc==-1 && errno=
10a37 3d 45 4e 4f 54 53 55 50 20 29 7b 0d 0a 20 20 20  =ENOTSUP ){..   
10a38 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b   rc = fsync(fd);
10a39 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  ..  }..#endif /*
10a3a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0d 0a   OS_VXWORKS */..
10a3b 23 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20  #endif /* ifdef 
10a3c 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65  SQLITE_NO_SYNC e
10a3d 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
10a3e 4e 43 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 4f  NC */....  if( O
10a3f 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 72 63 21  S_VXWORKS && rc!
10a40 3d 20 2d 31 20 29 7b 0d 0a 20 20 20 20 72 63 20  = -1 ){..    rc 
10a41 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  = 0;..  }..  ret
10a42 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
10a43 0d 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65  ..** Open a file
10a44 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74   descriptor to t
10a45 68 65 20 64 69 72 65 63 74 6f 72 79 20 63 6f 6e  he directory con
10a46 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a 46 69  taining file zFi
10a47 6c 65 6e 61 6d 65 2e 0d 0a 2a 2a 20 49 66 20 73  lename...** If s
10a48 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 64 20  uccessful, *pFd 
10a49 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 70  is set to the op
10a4a 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 72 69  ened file descri
10a4b 70 74 6f 72 20 61 6e 64 0d 0a 2a 2a 20 53 51 4c  ptor and..** SQL
10a4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
10a4d 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ed. If an error 
10a4e 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53  occurs, either S
10a4f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0d 0a 2a 2a 20  QLITE_NOMEM..** 
10a50 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  or SQLITE_CANTOP
10a51 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  EN is returned a
10a52 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74  nd *pFd is set t
10a53 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0d 0a  o an undefined..
10a54 2a 2a 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a  ** value...**..*
10a55 2a 20 54 68 65 20 64 69 72 65 63 74 6f 72 79 20  * The directory 
10a56 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
10a57 69 73 20 75 73 65 64 20 66 6f 72 20 6f 6e 6c 79  is used for only
10a58 20 6f 6e 65 20 74 68 69 6e 67 20 2d 20 74 6f 0d   one thing - to.
10a59 0a 2a 2a 20 66 73 79 6e 63 28 29 20 61 20 64 69  .** fsync() a di
10a5a 72 65 63 74 6f 72 79 20 74 6f 20 6d 61 6b 65 20  rectory to make 
10a5b 73 75 72 65 20 66 69 6c 65 20 63 72 65 61 74 69  sure file creati
10a5c 6f 6e 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e 20  on and deletion 
10a5d 65 76 65 6e 74 73 0d 0a 2a 2a 20 61 72 65 20 66  events..** are f
10a5e 6c 75 73 68 65 64 20 74 6f 20 64 69 73 6b 2e 20  lushed to disk. 
10a5f 20 53 75 63 68 20 66 73 79 6e 63 73 20 61 72 65   Such fsyncs are
10a60 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 6e   not needed on n
10a61 65 77 65 72 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  ewer..** journal
10a62 69 6e 67 20 66 69 6c 65 73 79 73 74 65 6d 73 2c  ing filesystems,
10a63 20 62 75 74 20 61 72 65 20 72 65 71 75 69 72 65   but are require
10a64 64 20 6f 6e 20 6f 6c 64 65 72 20 66 69 6c 65 73  d on older files
10a65 79 73 74 65 6d 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ystems...**..** 
10a66 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
10a67 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 75   be overridden u
10a68 73 69 6e 67 20 74 68 65 20 78 53 65 74 53 79 73  sing the xSetSys
10a69 43 61 6c 6c 20 69 6e 74 65 72 66 61 63 65 2e 0d  Call interface..
10a6a 0a 2a 2a 20 54 68 65 20 61 62 69 6c 69 74 79 20  .** The ability 
10a6b 74 6f 20 6f 76 65 72 72 69 64 65 20 74 68 69 73  to override this
10a6c 20 72 6f 75 74 69 6e 65 20 77 61 73 20 61 64 64   routine was add
10a6d 65 64 20 69 6e 20 73 75 70 70 6f 72 74 20 6f 66  ed in support of
10a6e 20 74 68 65 0d 0a 2a 2a 20 63 68 72 6f 6d 69 75   the..** chromiu
10a6f 6d 20 73 61 6e 64 62 6f 78 2e 20 20 4f 70 65 6e  m sandbox.  Open
10a70 69 6e 67 20 61 20 64 69 72 65 63 74 6f 72 79 20  ing a directory 
10a71 69 73 20 61 20 73 65 63 75 72 69 74 79 20 72 69  is a security ri
10a72 73 6b 20 28 77 65 20 61 72 65 0d 0a 2a 2a 20 74  sk (we are..** t
10a73 6f 6c 64 29 20 73 6f 20 6d 61 6b 69 6e 67 20 69  old) so making i
10a74 74 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 61  t overrideable a
10a75 6c 6c 6f 77 73 20 74 68 65 20 63 68 72 6f 6d 69  llows the chromi
10a76 75 6d 20 73 61 6e 64 62 6f 78 20 74 6f 0d 0a 2a  um sandbox to..*
10a77 2a 20 72 65 70 6c 61 63 65 20 74 68 69 73 20 72  * replace this r
10a78 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 68 61  outine with a ha
10a79 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 20 20 54  rmless no-op.  T
10a7a 6f 20 6d 61 6b 65 20 74 68 69 73 20 72 6f 75 74  o make this rout
10a7b 69 6e 65 0d 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2c  ine..** a no-op,
10a7c 20 72 65 70 6c 61 63 65 20 69 74 20 77 69 74 68   replace it with
10a7d 20 61 20 73 74 75 62 20 74 68 61 74 20 72 65 74   a stub that ret
10a7e 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 62  urns SQLITE_OK b
10a7f 75 74 20 6c 65 61 76 65 73 0d 0a 2a 2a 20 2a 70  ut leaves..** *p
10a80 46 64 20 73 65 74 20 74 6f 20 61 20 6e 65 67 61  Fd set to a nega
10a81 74 69 76 65 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2a  tive number...**
10a82 0d 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  ..** If SQLITE_O
10a83 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  K is returned, t
10a84 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
10a85 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f  ponsible for clo
10a86 73 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 66 69 6c  sing..** the fil
10a87 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46  e descriptor *pF
10a88 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e  d using close().
10a89 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
10a8a 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63   openDirectory(c
10a8b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
10a8c 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b  name, int *pFd){
10a8d 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 69  ..  int ii;..  i
10a8e 6e 74 20 66 64 20 3d 20 2d 31 3b 0d 0a 20 20 63  nt fd = -1;..  c
10a8f 68 61 72 20 7a 44 69 72 6e 61 6d 65 5b 4d 41 58  har zDirname[MAX
10a90 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 0d 0a 0d  _PATHNAME+1];...
10a91 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
10a92 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ntf(MAX_PATHNAME
10a93 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25 73 22  , zDirname, "%s"
10a94 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20  , zFilename);.. 
10a95 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74 72   for(ii=(int)str
10a96 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69  len(zDirname); i
10a97 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b  i>1 && zDirname[
10a98 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b  ii]!='/'; ii--);
10a99 0d 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0d  ..  if( ii>0 ){.
10a9a 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69  .    zDirname[ii
10a9b 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20 66  ] = '\0';..    f
10a9c 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28  d = robust_open(
10a9d 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e  zDirname, O_RDON
10a9e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b  LY|O_BINARY, 0);
10a9f 0d 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20  ..    if( fd>=0 
10aa0 29 7b 0d 0a 23 69 66 64 65 66 20 46 44 5f 43 4c  ){..#ifdef FD_CL
10aa1 4f 45 58 45 43 0d 0a 20 20 20 20 20 20 6f 73 46  OEXEC..      osF
10aa2 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44  cntl(fd, F_SETFD
10aa3 2c 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 5f  , osFcntl(fd, F_
10aa4 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43  GETFD, 0) | FD_C
10aa5 4c 4f 45 58 45 43 29 3b 0d 0a 23 65 6e 64 69 66  LOEXEC);..#endif
10aa6 0d 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28  ..      OSTRACE(
10aa7 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25  ("OPENDIR %-3d %
10aa8 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61  s\n", fd, zDirna
10aa9 6d 65 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  me));..    }..  
10aaa 7d 0d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0d  }..  *pFd = fd;.
10aab 0a 20 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30  .  return (fd>=0
10aac 3f 53 51 4c 49 54 45 5f 4f 4b 3a 75 6e 69 78 4c  ?SQLITE_OK:unixL
10aad 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43  ogError(SQLITE_C
10aae 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f  ANTOPEN_BKPT, "o
10aaf 70 65 6e 22 2c 20 7a 44 69 72 6e 61 6d 65 29 29  pen", zDirname))
10ab0 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  ;..}..../*..** M
10ab1 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69  ake sure all wri
10ab2 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75  tes to a particu
10ab3 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d  lar file are com
10ab4 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0d  mitted to disk..
10ab5 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 64 61 74 61 4f  .**..** If dataO
10ab6 6e 6c 79 3d 3d 30 20 74 68 65 6e 20 62 6f 74 68  nly==0 then both
10ab7 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66   the file itself
10ab8 20 61 6e 64 20 69 74 73 20 6d 65 74 61 64 61 74   and its metadat
10ab9 61 20 28 66 69 6c 65 0d 0a 2a 2a 20 73 69 7a 65  a (file..** size
10aba 2c 20 61 63 63 65 73 73 20 74 69 6d 65 2c 20 65  , access time, e
10abb 74 63 29 20 61 72 65 20 73 79 6e 63 65 64 2e 20  tc) are synced. 
10abc 20 49 66 20 64 61 74 61 4f 6e 6c 79 21 3d 30 20   If dataOnly!=0 
10abd 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 0d 0a 2a  then only the..*
10abe 2a 20 66 69 6c 65 20 64 61 74 61 20 69 73 20 73  * file data is s
10abf 79 6e 63 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 55  ynced...**..** U
10ac0 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f 20  nder Unix, also 
10ac1 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
10ac2 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74  he directory ent
10ac3 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 0d  ry for the file.
10ac4 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
10ac5 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e  ated by fsync-in
10ac6 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  g the directory 
10ac7 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
10ac8 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 49 66 20 77  e file...** If w
10ac9 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73  e do not do this
10aca 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65   and we encounte
10acb 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  r a power failur
10acc 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  e, the directory
10acd 0d 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74  ..** entry for t
10ace 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74  he journal might
10acf 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72   not exist after
10ad0 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65   we reboot.  The
10ad1 20 6e 65 78 74 0d 0a 2a 2a 20 53 51 4c 69 74 65   next..** SQLite
10ad2 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66   to access the f
10ad3 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f  ile will not kno
10ad4 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  w that the journ
10ad5 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75  al exists (becau
10ad6 73 65 0d 0a 2a 2a 20 74 68 65 20 64 69 72 65 63  se..** the direc
10ad7 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74  tory entry for t
10ad8 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e  he journal was n
10ad9 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e  ever created) an
10ada 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
10adb 6e 0d 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  n..** will not r
10adc 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69  oll back - possi
10add 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64  bly leading to d
10ade 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
10adf 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  on...*/..static 
10ae0 69 6e 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c  int unixSync(sql
10ae1 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
10ae2 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 20 20 69 6e  nt flags){..  in
10ae3 74 20 72 63 3b 0d 0a 20 20 75 6e 69 78 46 69 6c  t rc;..  unixFil
10ae4 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
10ae5 46 69 6c 65 2a 29 69 64 3b 0d 0a 0d 0a 20 20 69  File*)id;....  i
10ae6 6e 74 20 69 73 44 61 74 61 4f 6e 6c 79 20 3d 20  nt isDataOnly = 
10ae7 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 53 59  (flags&SQLITE_SY
10ae8 4e 43 5f 44 41 54 41 4f 4e 4c 59 29 3b 0d 0a 20  NC_DATAONLY);.. 
10ae9 20 69 6e 74 20 69 73 46 75 6c 6c 73 79 6e 63 20   int isFullsync 
10aea 3d 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d  = (flags&0x0F)==
10aeb 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
10aec 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;....  /* Check 
10aed 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c 49  that one of SQLI
10aee 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f  TE_SYNC_NORMAL o
10aef 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 73 65  r FULL was passe
10af0 64 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 28  d */..  assert((
10af1 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c  flags&0x0F)==SQL
10af2 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0d  ITE_SYNC_NORMAL.
10af3 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73  .      || (flags
10af4 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
10af5 59 4e 43 5f 46 55 4c 4c 0d 0a 20 20 29 3b 0d 0a  YNC_FULL..  );..
10af6 0d 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e  ..  /* Unix cann
10af7 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73  ot, but some sys
10af8 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20  tems may return 
10af9 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d  SQLITE_FULL from
10afa 20 68 65 72 65 2e 20 54 68 69 73 0d 0a 20 20 2a   here. This..  *
10afb 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73  * line is to tes
10afc 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20  t that doing so 
10afd 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61  does not cause a
10afe 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0d 0a 20 20  ny problems...  
10aff 2a 2f 0d 0a 20 20 53 69 6d 75 6c 61 74 65 44 69  */..  SimulateDi
10b00 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74  skfullError( ret
10b01 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  urn SQLITE_FULL 
10b02 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  );....  assert( 
10b03 70 46 69 6c 65 20 29 3b 0d 0a 20 20 4f 53 54 52  pFile );..  OSTR
10b04 41 43 45 28 28 22 53 59 4e 43 20 20 20 20 25 2d  ACE(("SYNC    %-
10b05 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29  3d\n", pFile->h)
10b06 29 3b 0d 0a 20 20 72 63 20 3d 20 66 75 6c 6c 5f  );..  rc = full_
10b07 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 68 2c 20  fsync(pFile->h, 
10b08 69 73 46 75 6c 6c 73 79 6e 63 2c 20 69 73 44 61  isFullsync, isDa
10b09 74 61 4f 6e 6c 79 29 3b 0d 0a 20 20 53 69 6d 75  taOnly);..  Simu
10b0a 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d  lateIOError( rc=
10b0b 31 20 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29  1 );..  if( rc )
10b0c 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  {..    pFile->la
10b0d 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
10b0e 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69  ..    return uni
10b0f 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  xLogError(SQLITE
10b10 5f 49 4f 45 52 52 5f 46 53 59 4e 43 2c 20 22 66  _IOERR_FSYNC, "f
10b11 75 6c 6c 5f 66 73 79 6e 63 22 2c 20 70 46 69 6c  ull_fsync", pFil
10b12 65 2d 3e 7a 50 61 74 68 29 3b 0d 0a 20 20 7d 0d  e->zPath);..  }.
10b13 0a 0d 0a 20 20 2f 2a 20 41 6c 73 6f 20 66 73 79  ...  /* Also fsy
10b14 6e 63 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  nc the directory
10b15 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
10b16 66 69 6c 65 20 69 66 20 74 68 65 20 44 49 52 53  file if the DIRS
10b17 59 4e 43 20 66 6c 61 67 0d 0a 20 20 2a 2a 20 69  YNC flag..  ** i
10b18 73 20 73 65 74 2e 20 20 54 68 69 73 20 69 73 20  s set.  This is 
10b19 61 20 6f 6e 65 2d 74 69 6d 65 20 6f 63 63 75 72  a one-time occur
10b1a 72 61 6e 63 65 2e 20 20 4d 61 6e 79 20 73 79 73  rance.  Many sys
10b1b 74 65 6d 73 20 28 65 78 61 6d 70 6c 65 73 3a 20  tems (examples: 
10b1c 41 49 58 29 0d 0a 20 20 2a 2a 20 61 72 65 20 75  AIX)..  ** are u
10b1d 6e 61 62 6c 65 20 74 6f 20 66 73 79 6e 63 20 61  nable to fsync a
10b1e 20 64 69 72 65 63 74 6f 72 79 2c 20 73 6f 20 69   directory, so i
10b1f 67 6e 6f 72 65 20 65 72 72 6f 72 73 20 6f 6e 20  gnore errors on 
10b20 74 68 65 20 66 73 79 6e 63 2e 0d 0a 20 20 2a 2f  the fsync...  */
10b21 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 63  ..  if( pFile->c
10b22 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46  trlFlags & UNIXF
10b23 49 4c 45 5f 44 49 52 53 59 4e 43 20 29 7b 0d 0a  ILE_DIRSYNC ){..
10b24 20 20 20 20 69 6e 74 20 64 69 72 66 64 3b 0d 0a      int dirfd;..
10b25 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 44 49      OSTRACE(("DI
10b26 52 53 59 4e 43 20 25 73 20 28 68 61 76 65 5f 66  RSYNC %s (have_f
10b27 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c  ullfsync=%d full
10b28 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69  sync=%d)\n", pFi
10b29 6c 65 2d 3e 7a 50 61 74 68 2c 0d 0a 20 20 20 20  le->zPath,..    
10b2a 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c          HAVE_FUL
10b2b 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79  LFSYNC, isFullsy
10b2c 6e 63 29 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  nc));..    rc = 
10b2d 6f 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 28  osOpenDirectory(
10b2e 70 46 69 6c 65 2d 3e 7a 50 61 74 68 2c 20 26 64  pFile->zPath, &d
10b2f 69 72 66 64 29 3b 0d 0a 20 20 20 20 69 66 28 20  irfd);..    if( 
10b30 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
10b31 20 64 69 72 66 64 3e 3d 30 20 29 7b 0d 0a 20 20   dirfd>=0 ){..  
10b32 20 20 20 20 66 75 6c 6c 5f 66 73 79 6e 63 28 64      full_fsync(d
10b33 69 72 66 64 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  irfd, 0, 0);..  
10b34 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65      robust_close
10b35 28 70 46 69 6c 65 2c 20 64 69 72 66 64 2c 20 5f  (pFile, dirfd, _
10b36 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20 7d  _LINE__);..    }
10b37 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c  else if( rc==SQL
10b38 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0d  ITE_CANTOPEN ){.
10b39 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
10b3a 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
10b3b 20 20 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c     pFile->ctrlFl
10b3c 61 67 73 20 26 3d 20 7e 55 4e 49 58 46 49 4c 45  ags &= ~UNIXFILE
10b3d 5f 44 49 52 53 59 4e 43 3b 0d 0a 20 20 7d 0d 0a  _DIRSYNC;..  }..
10b3e 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
10b3f 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72 75 6e 63 61  .../*..** Trunca
10b40 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  te an open file 
10b41 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73  to a specified s
10b42 69 7a 65 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ize..*/..static 
10b43 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65  int unixTruncate
10b44 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
10b45 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0d 0a  d, i64 nByte){..
10b46 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
10b47 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29  e = (unixFile *)
10b48 69 64 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  id;..  int rc;..
10b49 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
10b4a 29 3b 0d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  );..  SimulateIO
10b4b 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
10b4c 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
10b4d 41 54 45 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  ATE );....  /* I
10b4e 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 63  f the user has c
10b4f 6f 6e 66 69 67 75 72 65 64 20 61 20 63 68 75 6e  onfigured a chun
10b50 6b 2d 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20  k-size for this 
10b51 66 69 6c 65 2c 20 74 72 75 6e 63 61 74 65 20 74  file, truncate t
10b52 68 65 0d 0a 20 20 2a 2a 20 66 69 6c 65 20 73 6f  he..  ** file so
10b53 20 74 68 61 74 20 69 74 20 63 6f 6e 73 69 73 74   that it consist
10b54 73 20 6f 66 20 61 6e 20 69 6e 74 65 67 65 72 20  s of an integer 
10b55 6e 75 6d 62 65 72 20 6f 66 20 63 68 75 6e 6b 73  number of chunks
10b56 20 28 69 2e 65 2e 20 74 68 65 0d 0a 20 20 2a 2a   (i.e. the..  **
10b57 20 61 63 74 75 61 6c 20 66 69 6c 65 20 73 69 7a   actual file siz
10b58 65 20 61 66 74 65 72 20 74 68 65 20 6f 70 65 72  e after the oper
10b59 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 6c 61 72  ation may be lar
10b5a 67 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71  ger than the req
10b5b 75 65 73 74 65 64 0d 0a 20 20 2a 2a 20 73 69 7a  uested..  ** siz
10b5c 65 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  e)...  */..  if(
10b5d 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20   pFile->szChunk 
10b5e 29 7b 0d 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  ){..    nByte = 
10b5f 28 28 6e 42 79 74 65 20 2b 20 70 46 69 6c 65 2d  ((nByte + pFile-
10b60 3e 73 7a 43 68 75 6e 6b 20 2d 20 31 29 2f 70 46  >szChunk - 1)/pF
10b61 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 29 20 2a 20  ile->szChunk) * 
10b62 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0d  pFile->szChunk;.
10b63 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 72  .  }....  rc = r
10b64 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28  obust_ftruncate(
10b65 70 46 69 6c 65 2d 3e 68 2c 20 28 6f 66 66 5f 74  pFile->h, (off_t
10b66 29 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66 28 20  )nByte);..  if( 
10b67 72 63 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65  rc ){..    pFile
10b68 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
10b69 72 6e 6f 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  rno;..    return
10b6a 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51   unixLogError(SQ
10b6b 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
10b6c 41 54 45 2c 20 22 66 74 72 75 6e 63 61 74 65 22  ATE, "ftruncate"
10b6d 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b  , pFile->zPath);
10b6e 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 23 69 66 6e  ..  }else{..#ifn
10b6f 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20 20 20  def NDEBUG..    
10b70 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
10b71 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74  ng a normal writ
10b72 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
10b73 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64  file (as opposed
10b74 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 64 6f 69 6e   to..    ** doin
10b75 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  g a hot-journal 
10b76 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72  rollback or a wr
10b77 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65  ite to some file
10b78 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0d 0a 20   other than a.. 
10b79 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74     ** normal dat
10b7a 61 62 61 73 65 20 66 69 6c 65 29 20 61 6e 64 20  abase file) and 
10b7b 77 65 20 74 72 75 6e 63 61 74 65 20 74 68 65 20  we truncate the 
10b7c 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e  file to zero len
10b7d 67 74 68 2c 0d 0a 20 20 20 20 2a 2a 20 74 68 61  gth,..    ** tha
10b7e 74 20 65 66 66 65 63 74 69 76 65 6c 79 20 75 70  t effectively up
10b7f 64 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65  dates the change
10b80 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20   counter.  This 
10b81 6d 69 67 68 74 20 68 61 70 70 65 6e 0d 0a 20 20  might happen..  
10b82 20 20 2a 2a 20 77 68 65 6e 20 72 65 73 74 6f 72    ** when restor
10b83 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 75  ing a database u
10b84 73 69 6e 67 20 74 68 65 20 62 61 63 6b 75 70 20  sing the backup 
10b85 41 50 49 20 66 72 6f 6d 20 61 20 7a 65 72 6f 2d  API from a zero-
10b86 6c 65 6e 67 74 68 0d 0a 20 20 20 20 2a 2a 20 73  length..    ** s
10b87 6f 75 72 63 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  ource...    */..
10b88 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 69      if( pFile->i
10b89 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 26 26 20  nNormalWrite && 
10b8a 6e 42 79 74 65 3d 3d 30 20 29 7b 0d 0a 20 20 20  nByte==0 ){..   
10b8b 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43     pFile->transC
10b8c 6e 74 72 43 68 6e 67 20 3d 20 31 3b 0d 0a 20 20  ntrChng = 1;..  
10b8d 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20    }..#endif.... 
10b8e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10b8f 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  _OK;..  }..}....
10b90 2f 2a 0d 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  /*..** Determine
10b91 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
10b92 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62  e of a file in b
10b93 79 74 65 73 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ytes..*/..static
10b94 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69 7a   int unixFileSiz
10b95 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
10b96 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b  id, i64 *pSize){
10b97 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73  ..  int rc;..  s
10b98 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0d  truct stat buf;.
10b99 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b  .  assert( id );
10b9a 0d 0a 20 20 72 63 20 3d 20 6f 73 46 73 74 61 74  ..  rc = osFstat
10b9b 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29  (((unixFile*)id)
10b9c 2d 3e 68 2c 20 26 62 75 66 29 3b 0d 0a 20 20 53  ->h, &buf);..  S
10b9d 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
10b9e 72 63 3d 31 20 29 3b 0d 0a 20 20 69 66 28 20 72  rc=1 );..  if( r
10b9f 63 21 3d 30 20 29 7b 0d 0a 20 20 20 20 28 28 75  c!=0 ){..    ((u
10ba0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
10ba1 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
10ba2 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
10ba3 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
10ba4 0d 0a 20 20 7d 0d 0a 20 20 2a 70 53 69 7a 65 20  ..  }..  *pSize 
10ba5 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0d 0a  = buf.st_size;..
10ba6 0d 0a 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e  ..  /* When open
10ba7 69 6e 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20  ing a zero-size 
10ba8 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 66 69  database, the fi
10ba9 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 29 20 70 72  ndInodeInfo() pr
10baa 6f 63 65 64 75 72 65 0d 0a 20 20 2a 2a 20 77 72  ocedure..  ** wr
10bab 69 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62 79  ites a single by
10bac 74 65 20 69 6e 74 6f 20 74 68 61 74 20 66 69 6c  te into that fil
10bad 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 6f  e in order to wo
10bae 72 6b 20 61 72 6f 75 6e 64 20 61 20 62 75 67 0d  rk around a bug.
10baf 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d  .  ** in the OS-
10bb0 58 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74  X msdos filesyst
10bb1 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f  em.  In order to
10bb2 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20   avoid problems 
10bb3 77 69 74 68 20 75 70 70 65 72 0d 0a 20 20 2a 2a  with upper..  **
10bb4 20 6c 61 79 65 72 73 2c 20 77 65 20 6e 65 65 64   layers, we need
10bb5 20 74 6f 20 72 65 70 6f 72 74 20 74 68 69 73 20   to report this 
10bb6 66 69 6c 65 20 73 69 7a 65 20 61 73 20 7a 65 72  file size as zer
10bb7 6f 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74  o even though it
10bb8 20 69 73 0d 0a 20 20 2a 2a 20 72 65 61 6c 6c 79   is..  ** really
10bb9 20 31 2e 20 20 20 54 69 63 6b 65 74 20 23 33 32   1.   Ticket #32
10bba 36 30 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  60...  */..  if(
10bbb 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a 70 53   *pSize==1 ) *pS
10bbc 69 7a 65 20 3d 20 30 3b 0d 0a 0d 0a 0d 0a 20 20  ize = 0;......  
10bbd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10bbe 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20 53 51 4c 49  ;..}....#if SQLI
10bbf 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
10bc0 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e  G_STYLE && defin
10bc1 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0d 0a 2f  ed(__APPLE__)../
10bc2 2a 0d 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f  *..** Handler fo
10bc3 72 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20  r proxy-locking 
10bc4 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72  file-control ver
10bc5 62 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 6c  bs.  Defined bel
10bc6 6f 77 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 70 72  ow in the..** pr
10bc7 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64  oxying locking d
10bc8 69 76 69 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74  ivision...*/..st
10bc9 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69  atic int proxyFi
10bca 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
10bcb 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64  3_file*,int,void
10bcc 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  *);..#endif..../
10bcd 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  * ..** This func
10bce 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
10bcf 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 53 51 4c  o handle the SQL
10bd0 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48  ITE_FCNTL_SIZE_H
10bd1 49 4e 54 20 0d 0a 2a 2a 20 66 69 6c 65 2d 63 6f  INT ..** file-co
10bd2 6e 74 72 6f 6c 20 6f 70 65 72 61 74 69 6f 6e 2e  ntrol operation.
10bd3 20 20 45 6e 6c 61 72 67 65 20 74 68 65 20 64 61    Enlarge the da
10bd4 74 61 62 61 73 65 20 74 6f 20 6e 42 79 74 65 73  tabase to nBytes
10bd5 20 69 6e 20 73 69 7a 65 0d 0a 2a 2a 20 28 72 6f   in size..** (ro
10bd6 75 6e 64 65 64 20 75 70 20 74 6f 20 74 68 65 20  unded up to the 
10bd7 6e 65 78 74 20 63 68 75 6e 6b 2d 73 69 7a 65 29  next chunk-size)
10bd8 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61  .  If the databa
10bd9 73 65 20 69 73 20 61 6c 72 65 61 64 79 0d 0a 2a  se is already..*
10bda 2a 20 6e 42 79 74 65 73 20 6f 72 20 6c 61 72 67  * nBytes or larg
10bdb 65 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  er, this routine
10bdc 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f   is a no-op...*/
10bdd 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 63 6e  ..static int fcn
10bde 74 6c 53 69 7a 65 48 69 6e 74 28 75 6e 69 78 46  tlSizeHint(unixF
10bdf 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 36 34 20  ile *pFile, i64 
10be0 6e 42 79 74 65 29 7b 0d 0a 20 20 69 66 28 20 70  nByte){..  if( p
10be1 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 3e 30 20  File->szChunk>0 
10be2 29 7b 0d 0a 20 20 20 20 69 36 34 20 6e 53 69 7a  ){..    i64 nSiz
10be3 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
10be4 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65        /* Require
10be5 64 20 66 69 6c 65 20 73 69 7a 65 20 2a 2f 0d 0a  d file size */..
10be6 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20      struct stat 
10be7 62 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  buf;            
10be8 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 68 6f 6c    /* Used to hol
10be9 64 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20  d return values 
10bea 6f 66 20 66 73 74 61 74 28 29 20 2a 2f 0d 0a 20  of fstat() */.. 
10beb 20 20 0d 0a 20 20 20 20 69 66 28 20 6f 73 46 73    ..    if( osFs
10bec 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62  tat(pFile->h, &b
10bed 75 66 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  uf) ) return SQL
10bee 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
10bef 0d 0a 0d 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20  ....    nSize = 
10bf0 28 28 6e 42 79 74 65 2b 70 46 69 6c 65 2d 3e 73  ((nByte+pFile->s
10bf1 7a 43 68 75 6e 6b 2d 31 29 20 2f 20 70 46 69 6c  zChunk-1) / pFil
10bf2 65 2d 3e 73 7a 43 68 75 6e 6b 29 20 2a 20 70 46  e->szChunk) * pF
10bf3 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0d 0a 20  ile->szChunk;.. 
10bf4 20 20 20 69 66 28 20 6e 53 69 7a 65 3e 28 69 36     if( nSize>(i6
10bf5 34 29 62 75 66 2e 73 74 5f 73 69 7a 65 20 29 7b  4)buf.st_size ){
10bf6 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
10bf7 48 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f  HAVE_POSIX_FALLO
10bf8 43 41 54 45 29 20 26 26 20 48 41 56 45 5f 50 4f  CATE) && HAVE_PO
10bf9 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 0d 0a 20  SIX_FALLOCATE.. 
10bfa 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65       /* The code
10bfb 20 62 65 6c 6f 77 20 69 73 20 68 61 6e 64 6c 69   below is handli
10bfc 6e 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ng the return va
10bfd 6c 75 65 20 6f 66 20 6f 73 46 61 6c 6c 6f 63 61  lue of osFalloca
10bfe 74 65 28 29 20 0d 0a 20 20 20 20 20 20 2a 2a 20  te() ..      ** 
10bff 63 6f 72 72 65 63 74 6c 79 2e 20 70 6f 73 69 78  correctly. posix
10c00 5f 66 61 6c 6c 6f 63 61 74 65 28 29 20 69 73 20  _fallocate() is 
10c01 64 65 66 69 6e 65 64 20 74 6f 20 22 72 65 74 75  defined to "retu
10c02 72 6e 73 20 7a 65 72 6f 20 6f 6e 20 73 75 63 63  rns zero on succ
10c03 65 73 73 2c 20 0d 0a 20 20 20 20 20 20 2a 2a 20  ess, ..      ** 
10c04 6f 72 20 61 6e 20 65 72 72 6f 72 20 6e 75 6d 62  or an error numb
10c05 65 72 20 6f 6e 20 20 66 61 69 6c 75 72 65 22 2e  er on  failure".
10c06 20 53 65 65 20 74 68 65 20 6d 61 6e 70 61 67 65   See the manpage
10c07 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f   for details. */
10c08 0d 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 3b  ..      int err;
10c09 0d 0a 20 20 20 20 20 20 64 6f 7b 0d 0a 20 20 20  ..      do{..   
10c0a 20 20 20 20 20 65 72 72 20 3d 20 6f 73 46 61 6c       err = osFal
10c0b 6c 6f 63 61 74 65 28 70 46 69 6c 65 2d 3e 68 2c  locate(pFile->h,
10c0c 20 62 75 66 2e 73 74 5f 73 69 7a 65 2c 20 6e 53   buf.st_size, nS
10c0d 69 7a 65 2d 62 75 66 2e 73 74 5f 73 69 7a 65 29  ize-buf.st_size)
10c0e 3b 0d 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28  ;..      }while(
10c0f 20 65 72 72 3d 3d 45 49 4e 54 52 20 29 3b 0d 0a   err==EINTR );..
10c10 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 20        if( err ) 
10c11 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
10c12 45 52 52 5f 57 52 49 54 45 3b 0d 0a 23 65 6c 73  ERR_WRITE;..#els
10c13 65 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  e..      /* If t
10c14 68 65 20 4f 53 20 64 6f 65 73 20 6e 6f 74 20 68  he OS does not h
10c15 61 76 65 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63  ave posix_falloc
10c16 61 74 65 28 29 2c 20 66 61 6b 65 20 69 74 2e 20  ate(), fake it. 
10c17 46 69 72 73 74 20 75 73 65 0d 0a 20 20 20 20 20  First use..     
10c18 20 2a 2a 20 66 74 72 75 6e 63 61 74 65 28 29 20   ** ftruncate() 
10c19 74 6f 20 73 65 74 20 74 68 65 20 66 69 6c 65 20  to set the file 
10c1a 73 69 7a 65 2c 20 74 68 65 6e 20 77 72 69 74 65  size, then write
10c1b 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 74   a single byte t
10c1c 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  o..      ** the 
10c1d 6c 61 73 74 20 62 79 74 65 20 69 6e 20 65 61 63  last byte in eac
10c1e 68 20 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 74  h block within t
10c1f 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 67 69  he extended regi
10c20 6f 6e 2e 20 54 68 69 73 0d 0a 20 20 20 20 20 20  on. This..      
10c21 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 74  ** is the same t
10c22 65 63 68 6e 69 71 75 65 20 75 73 65 64 20 62 79  echnique used by
10c23 20 67 6c 69 62 63 20 74 6f 20 69 6d 70 6c 65 6d   glibc to implem
10c24 65 6e 74 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63  ent posix_falloc
10c25 61 74 65 28 29 0d 0a 20 20 20 20 20 20 2a 2a 20  ate()..      ** 
10c26 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  on systems that 
10c27 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 72 65  do not have a re
10c28 61 6c 20 66 61 6c 6c 6f 63 61 74 65 28 29 20 73  al fallocate() s
10c29 79 73 74 65 6d 20 63 61 6c 6c 2e 0d 0a 20 20 20  ystem call...   
10c2a 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74     */..      int
10c2b 20 6e 42 6c 6b 20 3d 20 62 75 66 2e 73 74 5f 62   nBlk = buf.st_b
10c2c 6c 6b 73 69 7a 65 3b 20 20 2f 2a 20 46 69 6c 65  lksize;  /* File
10c2d 2d 73 79 73 74 65 6d 20 62 6c 6f 63 6b 20 73 69  -system block si
10c2e 7a 65 20 2a 2f 0d 0a 20 20 20 20 20 20 69 36 34  ze */..      i64
10c2f 20 69 57 72 69 74 65 3b 20 20 20 20 20 20 20 20   iWrite;        
10c30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
10c31 20 6f 66 66 73 65 74 20 74 6f 20 77 72 69 74 65   offset to write
10c32 20 74 6f 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20   to */....      
10c33 69 66 28 20 72 6f 62 75 73 74 5f 66 74 72 75 6e  if( robust_ftrun
10c34 63 61 74 65 28 70 46 69 6c 65 2d 3e 68 2c 20 6e  cate(pFile->h, n
10c35 53 69 7a 65 29 20 29 7b 0d 0a 20 20 20 20 20 20  Size) ){..      
10c36 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
10c37 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20  no = errno;..   
10c38 20 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78       return unix
10c39 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
10c3a 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 2c 20  IOERR_TRUNCATE, 
10c3b 22 66 74 72 75 6e 63 61 74 65 22 2c 20 70 46 69  "ftruncate", pFi
10c3c 6c 65 2d 3e 7a 50 61 74 68 29 3b 0d 0a 20 20 20  le->zPath);..   
10c3d 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 57 72 69     }..      iWri
10c3e 74 65 20 3d 20 28 28 62 75 66 2e 73 74 5f 73 69  te = ((buf.st_si
10c3f 7a 65 20 2b 20 32 2a 6e 42 6c 6b 20 2d 20 31 29  ze + 2*nBlk - 1)
10c40 2f 6e 42 6c 6b 29 2a 6e 42 6c 6b 2d 31 3b 0d 0a  /nBlk)*nBlk-1;..
10c41 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 57 72        while( iWr
10c42 69 74 65 3c 6e 53 69 7a 65 20 29 7b 0d 0a 20 20  ite<nSize ){..  
10c43 20 20 20 20 20 20 69 6e 74 20 6e 57 72 69 74 65        int nWrite
10c44 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28   = seekAndWrite(
10c45 70 46 69 6c 65 2c 20 69 57 72 69 74 65 2c 20 22  pFile, iWrite, "
10c46 22 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 20 20  ", 1);..        
10c47 69 66 28 20 6e 57 72 69 74 65 21 3d 31 20 29 20  if( nWrite!=1 ) 
10c48 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
10c49 45 52 52 5f 57 52 49 54 45 3b 0d 0a 20 20 20 20  ERR_WRITE;..    
10c4a 20 20 20 20 69 57 72 69 74 65 20 2b 3d 20 6e 42      iWrite += nB
10c4b 6c 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65  lk;..      }..#e
10c4c 6e 64 69 66 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ndif..    }..  }
10c4d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ....  return SQL
10c4e 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
10c4f 0d 0a 2a 2a 20 49 66 20 2a 70 41 72 67 20 69 73  ..** If *pArg is
10c50 20 69 6e 69 74 69 74 69 61 6c 6c 79 20 6e 65 67   inititially neg
10c51 61 74 69 76 65 20 74 68 65 6e 20 74 68 69 73 20  ative then this 
10c52 69 73 20 61 20 71 75 65 72 79 2e 20 20 53 65 74  is a query.  Set
10c53 20 2a 70 41 72 67 20 74 6f 0d 0a 2a 2a 20 31 20   *pArg to..** 1 
10c54 6f 72 20 30 20 64 65 70 65 6e 64 69 6e 67 20 6f  or 0 depending o
10c55 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
10c56 20 62 69 74 20 6d 61 73 6b 20 6f 66 20 70 46 69   bit mask of pFi
10c57 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 69 73  le->ctrlFlags is
10c58 20 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66   set...**..** If
10c59 20 2a 70 41 72 67 20 69 73 20 30 20 6f 72 20 31   *pArg is 0 or 1
10c5a 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 6f 72 20  , then clear or 
10c5b 73 65 74 20 74 68 65 20 6d 61 73 6b 20 62 69 74  set the mask bit
10c5c 20 6f 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46   of pFile->ctrlF
10c5d 6c 61 67 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  lags...*/..stati
10c5e 63 20 76 6f 69 64 20 75 6e 69 78 4d 6f 64 65 42  c void unixModeB
10c5f 69 74 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  it(unixFile *pFi
10c60 6c 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61  le, unsigned cha
10c61 72 20 6d 61 73 6b 2c 20 69 6e 74 20 2a 70 41 72  r mask, int *pAr
10c62 67 29 7b 0d 0a 20 20 69 66 28 20 2a 70 41 72 67  g){..  if( *pArg
10c63 3c 30 20 29 7b 0d 0a 20 20 20 20 2a 70 41 72 67  <0 ){..    *pArg
10c64 20 3d 20 28 70 46 69 6c 65 2d 3e 63 74 72 6c 46   = (pFile->ctrlF
10c65 6c 61 67 73 20 26 20 6d 61 73 6b 29 21 3d 30 3b  lags & mask)!=0;
10c66 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 2a  ..  }else if( (*
10c67 70 41 72 67 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  pArg)==0 ){..   
10c68 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67   pFile->ctrlFlag
10c69 73 20 26 3d 20 7e 6d 61 73 6b 3b 0d 0a 20 20 7d  s &= ~mask;..  }
10c6a 65 6c 73 65 7b 0d 0a 20 20 20 20 70 46 69 6c 65  else{..    pFile
10c6b 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 6d  ->ctrlFlags |= m
10c6c 61 73 6b 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  ask;..  }..}....
10c6d 2f 2a 0d 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69  /*..** Informati
10c6e 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f  on and control o
10c6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68  f an open file h
10c70 61 6e 64 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  andle...*/..stat
10c71 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43  ic int unixFileC
10c72 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
10c73 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c  ile *id, int op,
10c74 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0d 0a 20   void *pArg){.. 
10c75 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
10c76 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
10c77 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  ;..  switch( op 
10c78 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ){..    case SQL
10c79 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
10c7a 41 54 45 3a 20 7b 0d 0a 20 20 20 20 20 20 2a 28  ATE: {..      *(
10c7b 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46 69 6c  int*)pArg = pFil
10c7c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3b 0d 0a 20  e->eFileLock;.. 
10c7d 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
10c7e 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
10c7f 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c     case SQLITE_L
10c80 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0d 0a 20 20  AST_ERRNO: {..  
10c81 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
10c82 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  = pFile->lastErr
10c83 6e 6f 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  no;..      retur
10c84 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
10c85 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
10c86 4c 49 54 45 5f 46 43 4e 54 4c 5f 43 48 55 4e 4b  LITE_FCNTL_CHUNK
10c87 5f 53 49 5a 45 3a 20 7b 0d 0a 20 20 20 20 20 20  _SIZE: {..      
10c88 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 3d  pFile->szChunk =
10c89 20 2a 28 69 6e 74 20 2a 29 70 41 72 67 3b 0d 0a   *(int *)pArg;..
10c8a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10c8b 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a  ITE_OK;..    }..
10c8c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
10c8d 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 3a  FCNTL_SIZE_HINT:
10c8e 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 63   {..      int rc
10c8f 3b 0d 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  ;..      Simulat
10c90 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31  eIOErrorBenign(1
10c91 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66  );..      rc = f
10c92 63 6e 74 6c 53 69 7a 65 48 69 6e 74 28 70 46 69  cntlSizeHint(pFi
10c93 6c 65 2c 20 2a 28 69 36 34 20 2a 29 70 41 72 67  le, *(i64 *)pArg
10c94 29 3b 0d 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  );..      Simula
10c95 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
10c96 30 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  0);..      retur
10c97 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  n rc;..    }..  
10c98 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43    case SQLITE_FC
10c99 4e 54 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c 3a  NTL_PERSIST_WAL:
10c9a 20 7b 0d 0a 20 20 20 20 20 20 75 6e 69 78 4d 6f   {..      unixMo
10c9b 64 65 42 69 74 28 70 46 69 6c 65 2c 20 55 4e 49  deBit(pFile, UNI
10c9c 58 46 49 4c 45 5f 50 45 52 53 49 53 54 5f 57 41  XFILE_PERSIST_WA
10c9d 4c 2c 20 28 69 6e 74 2a 29 70 41 72 67 29 3b 0d  L, (int*)pArg);.
10c9e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
10c9f 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d  LITE_OK;..    }.
10ca0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
10ca1 5f 46 43 4e 54 4c 5f 50 4f 57 45 52 53 41 46 45  _FCNTL_POWERSAFE
10ca2 5f 4f 56 45 52 57 52 49 54 45 3a 20 7b 0d 0a 20  _OVERWRITE: {.. 
10ca3 20 20 20 20 20 75 6e 69 78 4d 6f 64 65 42 69 74       unixModeBit
10ca4 28 70 46 69 6c 65 2c 20 55 4e 49 58 46 49 4c 45  (pFile, UNIXFILE
10ca5 5f 50 53 4f 57 2c 20 28 69 6e 74 2a 29 70 41 72  _PSOW, (int*)pAr
10ca6 67 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  g);..      retur
10ca7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
10ca8 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
10ca9 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41  LITE_FCNTL_VFSNA
10caa 4d 45 3a 20 7b 0d 0a 20 20 20 20 20 20 2a 28 63  ME: {..      *(c
10cab 68 61 72 2a 2a 29 70 41 72 67 20 3d 20 73 71 6c  har**)pArg = sql
10cac 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
10cad 22 2c 20 70 46 69 6c 65 2d 3e 70 56 66 73 2d 3e  ", pFile->pVfs->
10cae 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 72  zName);..      r
10caf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
10cb0 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66  ..    }..#ifndef
10cb1 20 4e 44 45 42 55 47 0d 0a 20 20 20 20 2f 2a 20   NDEBUG..    /* 
10cb2 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73 20  The pager calls 
10cb3 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 73  this method to s
10cb4 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68 61  ignal that it ha
10cb5 73 20 64 6f 6e 65 0d 0a 20 20 20 20 2a 2a 20 61  s done..    ** a
10cb6 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68   rollback and th
10cb7 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
10cb8 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63  is therefore unc
10cb9 68 61 6e 67 65 64 20 61 6e 64 0d 0a 20 20 20 20  hanged and..    
10cba 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69  ** it hence it i
10cbb 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61  s OK for the tra
10cbc 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20  nsaction change 
10cbd 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0d 0a 20  counter to be.. 
10cbe 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e     ** unchanged.
10cbf 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61  ..    */..    ca
10cc0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  se SQLITE_FCNTL_
10cc1 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0d  DB_UNCHANGED: {.
10cc2 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
10cc3 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65  e*)id)->dbUpdate
10cc4 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72 65 74   = 0;..      ret
10cc5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
10cc6 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 23      }..#endif..#
10cc7 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
10cc8 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26  _LOCKING_STYLE &
10cc9 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  & defined(__APPL
10cca 45 5f 5f 29 0d 0a 20 20 20 20 63 61 73 65 20 53  E__)..    case S
10ccb 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
10ccc 4f 58 59 46 49 4c 45 3a 0d 0a 20 20 20 20 63 61  OXYFILE:..    ca
10ccd 73 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f  se SQLITE_GET_LO
10cce 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0d 0a  CKPROXYFILE: {..
10ccf 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f        return pro
10cd0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64  xyFileControl(id
10cd1 2c 6f 70 2c 70 41 72 67 29 3b 0d 0a 20 20 20 20  ,op,pArg);..    
10cd2 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
10cd3 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
10cd4 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
10cd5 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a  ned(__APPLE__) *
10cd6 2f 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  /..  }..  return
10cd7 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44   SQLITE_NOTFOUND
10cd8 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
10cd9 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72  eturn the sector
10cda 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
10cdb 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
10cdc 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f   block device fo
10cdd 72 0d 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  r..** the specif
10cde 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
10cdf 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
10ce0 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
10ce1 61 79 20 62 65 0d 0a 2a 2a 20 6c 61 72 67 65 72  ay be..** larger
10ce2 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65   for some device
10ce3 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74  s...**..** SQLit
10ce4 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74  e code assumes t
10ce5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  his function can
10ce6 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73  not fail. It als
10ce7 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0d 0a  o assumes that..
10ce8 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20  ** if two files 
10ce9 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74  are created in t
10cea 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73  he same file-sys
10ceb 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69  tem directory (i
10cec 2e 65 2e 0d 0a 2a 2a 20 61 20 64 61 74 61 62 61  .e...** a databa
10ced 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e  se and its journ
10cee 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68  al file) that th
10cef 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69  e sector size wi
10cf0 6c 6c 20 62 65 20 74 68 65 0d 0a 2a 2a 20 73 61  ll be the..** sa
10cf1 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0d 0a 2a 2f  me for both...*/
10cf2 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  ..static int uni
10cf3 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69  xSectorSize(sqli
10cf4 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29  te3_file *pFile)
10cf5 7b 0d 0a 20 20 28 76 6f 69 64 29 70 46 69 6c 65  {..  (void)pFile
10cf6 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
10cf7 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
10cf8 52 5f 53 49 5a 45 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  R_SIZE;..}..../*
10cf9 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
10cfa 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
10cfb 69 73 74 69 63 73 20 66 6f 72 20 74 68 65 20 66  istics for the f
10cfc 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ile...**..** Thi
10cfd 73 20 56 46 53 20 69 73 20 73 65 74 20 75 70 20  s VFS is set up 
10cfe 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  to return SQLITE
10cff 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 45  _IOCAP_POWERSAFE
10d00 5f 4f 56 45 52 57 52 49 54 45 20 62 79 20 64 65  _OVERWRITE by de
10d01 66 61 75 6c 74 2e 0d 0a 2a 2a 20 48 6f 77 65 76  fault...** Howev
10d02 65 72 2c 20 74 68 61 74 20 63 68 6f 69 63 65 20  er, that choice 
10d03 69 73 20 63 6f 6e 74 72 61 76 65 72 73 69 61 6c  is contraversial
10d04 20 73 69 6e 63 65 20 74 65 63 68 6e 69 63 61 6c   since technical
10d05 6c 79 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  ly the underlyin
10d06 67 0d 0a 2a 2a 20 66 69 6c 65 20 73 79 73 74 65  g..** file syste
10d07 6d 20 64 6f 65 73 20 6e 6f 74 20 61 6c 77 61 79  m does not alway
10d08 73 20 70 72 6f 76 69 64 65 20 70 6f 77 65 72 73  s provide powers
10d09 61 66 65 20 6f 76 65 72 77 72 69 74 65 73 2e 20  afe overwrites. 
10d0a 20 28 49 6e 20 6f 74 68 65 72 0d 0a 2a 2a 20 77   (In other..** w
10d0b 6f 72 64 73 2c 20 61 66 74 65 72 20 61 20 70 6f  ords, after a po
10d0c 77 65 72 2d 6c 6f 73 73 20 65 76 65 6e 74 2c 20  wer-loss event, 
10d0d 70 61 72 74 73 20 6f 66 20 74 68 65 20 66 69 6c  parts of the fil
10d0e 65 20 74 68 61 74 20 77 65 72 65 20 6e 65 76 65  e that were neve
10d0f 72 0d 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6d 69  r..** written mi
10d10 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67  ght end up being
10d11 20 61 6c 74 65 72 65 64 2e 29 20 20 48 6f 77 65   altered.)  Howe
10d12 76 65 72 2c 20 6e 6f 6e 2d 50 53 4f 57 20 62 65  ver, non-PSOW be
10d13 68 61 76 69 6f 72 20 69 73 20 76 65 72 79 2c 0d  havior is very,.
10d14 0a 2a 2a 20 76 65 72 79 20 72 61 72 65 2e 20 20  .** very rare.  
10d15 41 6e 64 20 61 73 73 65 72 74 69 6e 67 20 50 53  And asserting PS
10d16 4f 57 20 6d 61 6b 65 73 20 61 20 6c 61 72 67 65  OW makes a large
10d17 20 72 65 64 75 63 74 69 6f 6e 20 69 6e 20 74 68   reduction in th
10d18 65 20 61 6d 6f 75 6e 74 0d 0a 2a 2a 20 6f 66 20  e amount..** of 
10d19 72 65 71 75 69 72 65 64 20 49 2f 4f 20 66 6f 72  required I/O for
10d1a 20 6a 6f 75 72 6e 61 6c 69 6e 67 2c 20 73 69 6e   journaling, sin
10d1b 63 65 20 61 20 6c 6f 74 20 6f 66 20 70 61 64 64  ce a lot of padd
10d1c 69 6e 67 20 69 73 20 65 6c 69 6d 69 6e 61 74 65  ing is eliminate
10d1d 64 2e 0d 0a 2a 2a 20 20 48 65 6e 63 65 2c 20 77  d...**  Hence, w
10d1e 68 69 6c 65 20 50 4f 57 45 52 53 41 46 45 5f 4f  hile POWERSAFE_O
10d1f 56 45 52 57 52 49 54 45 20 69 73 20 6f 6e 20 62  VERWRITE is on b
10d20 79 20 64 65 66 61 75 6c 74 2c 20 74 68 65 72 65  y default, there
10d21 20 69 73 20 61 20 66 69 6c 65 2d 63 6f 6e 74 72   is a file-contr
10d22 6f 6c 0d 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65  ol..** available
10d23 20 74 6f 20 74 75 72 6e 20 69 74 20 6f 66 66 20   to turn it off 
10d24 61 6e 64 20 55 52 49 20 71 75 65 72 79 20 70 61  and URI query pa
10d25 72 61 6d 65 74 65 72 20 61 76 61 69 6c 61 62 6c  rameter availabl
10d26 65 20 74 6f 20 74 75 72 6e 20 69 74 20 6f 66 66  e to turn it off
10d27 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
10d28 74 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72  t unixDeviceChar
10d29 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
10d2a 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0d 0a  te3_file *id){..
10d2b 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d 20    unixFile *p = 
10d2c 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a  (unixFile*)id;..
10d2d 20 20 69 66 28 20 70 2d 3e 63 74 72 6c 46 6c 61    if( p->ctrlFla
10d2e 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 50 53  gs & UNIXFILE_PS
10d2f 4f 57 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  OW ){..    retur
10d30 6e 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 50  n SQLITE_IOCAP_P
10d31 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49  OWERSAFE_OVERWRI
10d32 54 45 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  TE;..  }else{.. 
10d33 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
10d34 7d 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  }..}....#ifndef 
10d35 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d  SQLITE_OMIT_WAL.
10d36 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 62 6a 65  ...../*..** Obje
10d37 63 74 20 75 73 65 64 20 74 6f 20 72 65 70 72 65  ct used to repre
10d38 73 65 6e 74 20 61 6e 20 73 68 61 72 65 64 20 6d  sent an shared m
10d39 65 6d 6f 72 79 20 62 75 66 66 65 72 2e 20 20 0d  emory buffer.  .
10d3a 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c  .**..** When mul
10d3b 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 61 6c  tiple threads al
10d3c 6c 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  l reference the 
10d3d 73 61 6d 65 20 77 61 6c 2d 69 6e 64 65 78 2c 20  same wal-index, 
10d3e 65 61 63 68 20 74 68 72 65 61 64 0d 0a 2a 2a 20  each thread..** 
10d3f 68 61 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 78  has its own unix
10d40 53 68 6d 20 6f 62 6a 65 63 74 2c 20 62 75 74 20  Shm object, but 
10d41 74 68 65 79 20 61 6c 6c 20 70 6f 69 6e 74 20 74  they all point t
10d42 6f 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61  o a single insta
10d43 6e 63 65 0d 0a 2a 2a 20 6f 66 20 74 68 69 73 20  nce..** of this 
10d44 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65  unixShmNode obje
10d45 63 74 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ct.  In other wo
10d46 72 64 73 2c 20 65 61 63 68 20 77 61 6c 2d 69 6e  rds, each wal-in
10d47 64 65 78 20 69 73 20 6f 70 65 6e 65 64 0d 0a 2a  dex is opened..*
10d48 2a 20 6f 6e 6c 79 20 6f 6e 63 65 20 70 65 72 20  * only once per 
10d49 70 72 6f 63 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  process...**..**
10d4a 20 45 61 63 68 20 75 6e 69 78 53 68 6d 4e 6f 64   Each unixShmNod
10d4b 65 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 6e  e object is conn
10d4c 65 63 74 65 64 20 74 6f 20 61 20 73 69 6e 67 6c  ected to a singl
10d4d 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20  e unixInodeInfo 
10d4e 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 20 57 65 20 63  object...** We c
10d4f 6f 75 6c 64 20 63 6f 61 6c 65 73 63 65 20 74 68  ould coalesce th
10d50 69 73 20 6f 62 6a 65 63 74 20 69 6e 74 6f 20 75  is object into u
10d51 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 2c 20 62 75  nixInodeInfo, bu
10d52 74 20 74 68 61 74 20 77 6f 75 6c 64 20 6d 65 61  t that would mea
10d53 6e 0d 0a 2a 2a 20 65 76 65 72 79 20 6f 70 65 6e  n..** every open
10d54 20 66 69 6c 65 20 74 68 61 74 20 64 6f 65 73 20   file that does 
10d55 6e 6f 74 20 75 73 65 20 73 68 61 72 65 64 20 6d  not use shared m
10d56 65 6d 6f 72 79 20 28 69 6e 20 6f 74 68 65 72 20  emory (in other 
10d57 77 6f 72 64 73 2c 20 6d 6f 73 74 0d 0a 2a 2a 20  words, most..** 
10d58 6f 70 65 6e 20 66 69 6c 65 73 29 20 77 6f 75 6c  open files) woul
10d59 64 20 68 61 76 65 20 74 6f 20 63 61 72 72 79 20  d have to carry 
10d5a 61 72 6f 75 6e 64 20 74 68 69 73 20 65 78 74 72  around this extr
10d5b 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  a information.  
10d5c 53 6f 0d 0a 2a 2a 20 74 68 65 20 75 6e 69 78 49  So..** the unixI
10d5d 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 20  nodeInfo object 
10d5e 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
10d5f 65 72 20 74 6f 20 74 68 69 73 20 75 6e 69 78 53  er to this unixS
10d60 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 0d 0a 2a  hmNode object..*
10d61 2a 20 61 6e 64 20 74 68 65 20 75 6e 69 78 53 68  * and the unixSh
10d62 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20 69 73 20  mNode object is 
10d63 63 72 65 61 74 65 64 20 6f 6e 6c 79 20 77 68 65  created only whe
10d64 6e 20 6e 65 65 64 65 64 2e 0d 0a 2a 2a 0d 0a 2a  n needed...**..*
10d65 2a 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  * unixMutexHeld(
10d66 29 20 6d 75 73 74 20 62 65 20 74 72 75 65 20 77  ) must be true w
10d67 68 65 6e 20 63 72 65 61 74 69 6e 67 20 6f 72 20  hen creating or 
10d68 64 65 73 74 72 6f 79 69 6e 67 0d 0a 2a 2a 20 74  destroying..** t
10d69 68 69 73 20 6f 62 6a 65 63 74 20 6f 72 20 77 68  his object or wh
10d6a 69 6c 65 20 72 65 61 64 69 6e 67 20 6f 72 20 77  ile reading or w
10d6b 72 69 74 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f  riting the follo
10d6c 77 69 6e 67 20 66 69 65 6c 64 73 3a 0d 0a 2a 2a  wing fields:..**
10d6d 0d 0a 2a 2a 20 20 20 20 20 20 6e 52 65 66 0d 0a  ..**      nRef..
10d6e 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  **..** The follo
10d6f 77 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65 20  wing fields are 
10d70 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20  read-only after 
10d71 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63 72  the object is cr
10d72 65 61 74 65 64 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20  eated:..** ..** 
10d73 20 20 20 20 20 66 69 64 0d 0a 2a 2a 20 20 20 20       fid..**    
10d74 20 20 7a 46 69 6c 65 6e 61 6d 65 0d 0a 2a 2a 0d    zFilename..**.
10d75 0a 2a 2a 20 45 69 74 68 65 72 20 75 6e 69 78 53  .** Either unixS
10d76 68 6d 4e 6f 64 65 2e 6d 75 74 65 78 20 6d 75 73  hmNode.mutex mus
10d77 74 20 62 65 20 68 65 6c 64 20 6f 72 20 75 6e 69  t be held or uni
10d78 78 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30  xShmNode.nRef==0
10d79 20 61 6e 64 0d 0a 2a 2a 20 75 6e 69 78 4d 75 74   and..** unixMut
10d7a 65 78 48 65 6c 64 28 29 20 69 73 20 74 72 75 65  exHeld() is true
10d7b 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 6f 72   when reading or
10d7c 20 77 72 69 74 69 6e 67 20 61 6e 79 20 6f 74 68   writing any oth
10d7d 65 72 20 66 69 65 6c 64 0d 0a 2a 2a 20 69 6e 20  er field..** in 
10d7e 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0d  this structure..
10d7f 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 75 6e 69 78  .*/..struct unix
10d80 53 68 6d 4e 6f 64 65 20 7b 0d 0a 20 20 75 6e 69  ShmNode {..  uni
10d81 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f  xInodeInfo *pIno
10d82 64 65 3b 20 20 20 20 20 2f 2a 20 75 6e 69 78 49  de;     /* unixI
10d83 6e 6f 64 65 49 6e 66 6f 20 74 68 61 74 20 6f 77  nodeInfo that ow
10d84 6e 73 20 74 68 69 73 20 53 48 4d 20 6e 6f 64 65  ns this SHM node
10d85 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d   */..  sqlite3_m
10d86 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
10d87 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 61 63    /* Mutex to ac
10d88 63 65 73 73 20 74 68 69 73 20 6f 62 6a 65 63 74  cess this object
10d89 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 46 69   */..  char *zFi
10d8a 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  lename;         
10d8b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
10d8c 20 6d 6d 61 70 70 65 64 20 66 69 6c 65 20 2a 2f   mmapped file */
10d8d 0d 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20  ..  int h;      
10d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10d8f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63  * Open file desc
10d90 72 69 70 74 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74  riptor */..  int
10d91 20 73 7a 52 65 67 69 6f 6e 3b 20 20 20 20 20 20   szRegion;      
10d92 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
10d93 6f 66 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  of shared-memory
10d94 20 72 65 67 69 6f 6e 73 20 2a 2f 0d 0a 20 20 75   regions */..  u
10d95 31 36 20 6e 52 65 67 69 6f 6e 3b 20 20 20 20 20  16 nRegion;     
10d96 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
10d97 65 20 6f 66 20 61 72 72 61 79 20 61 70 52 65 67  e of array apReg
10d98 69 6f 6e 20 2a 2f 0d 0a 20 20 75 38 20 69 73 52  ion */..  u8 isR
10d99 65 61 64 6f 6e 6c 79 3b 20 20 20 20 20 20 20 20  eadonly;        
10d9a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
10d9b 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0d 0a 20 20  read-only */..  
10d9c 63 68 61 72 20 2a 2a 61 70 52 65 67 69 6f 6e 3b  char **apRegion;
10d9d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
10d9e 72 61 79 20 6f 66 20 6d 61 70 70 65 64 20 73 68  ray of mapped sh
10d9f 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69  ared-memory regi
10da0 6f 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52  ons */..  int nR
10da1 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
10da2 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
10da3 66 20 75 6e 69 78 53 68 6d 20 6f 62 6a 65 63 74  f unixShm object
10da4 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  s pointing to th
10da5 69 73 20 2a 2f 0d 0a 20 20 75 6e 69 78 53 68 6d  is */..  unixShm
10da6 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 20   *pFirst;       
10da7 20 20 20 20 2f 2a 20 41 6c 6c 20 75 6e 69 78 53      /* All unixS
10da8 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 6e 74  hm objects point
10da9 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f 0d 0a  ing to this */..
10daa 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
10dab 42 55 47 0d 0a 20 20 75 38 20 65 78 63 6c 4d 61  BUG..  u8 exclMa
10dac 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
10dad 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63    /* Mask of exc
10dae 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c  lusive locks hel
10daf 64 20 2a 2f 0d 0a 20 20 75 38 20 73 68 61 72 65  d */..  u8 share
10db0 64 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  dMask;          
10db1 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68     /* Mask of sh
10db2 61 72 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20  ared locks held 
10db3 2a 2f 0d 0a 20 20 75 38 20 6e 65 78 74 53 68 6d  */..  u8 nextShm
10db4 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Id;             
10db5 20 2f 2a 20 4e 65 78 74 20 61 76 61 69 6c 61 62   /* Next availab
10db6 6c 65 20 75 6e 69 78 53 68 6d 2e 69 64 20 76 61  le unixShm.id va
10db7 6c 75 65 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a  lue */..#endif..
10db8 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 74 72  };..../*..** Str
10db9 75 63 74 75 72 65 20 75 73 65 64 20 69 6e 74 65  ucture used inte
10dba 72 6e 61 6c 6c 79 20 62 79 20 74 68 69 73 20 56  rnally by this V
10dbb 46 53 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65  FS to record the
10dbc 20 73 74 61 74 65 20 6f 66 20 61 6e 0d 0a 2a 2a   state of an..**
10dbd 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6d 65 6d   open shared mem
10dbe 6f 72 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d  ory connection..
10dbf 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  .**..** The foll
10dc0 6f 77 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65  owing fields are
10dc1 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65   initialized whe
10dc2 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73  n this object is
10dc3 20 63 72 65 61 74 65 64 20 61 6e 64 0d 0a 2a 2a   created and..**
10dc4 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74   are read-only t
10dc5 68 65 72 65 61 66 74 65 72 3a 0d 0a 2a 2a 0d 0a  hereafter:..**..
10dc6 2a 2a 20 20 20 20 75 6e 69 78 53 68 6d 2e 70 46  **    unixShm.pF
10dc7 69 6c 65 0d 0a 2a 2a 20 20 20 20 75 6e 69 78 53  ile..**    unixS
10dc8 68 6d 2e 69 64 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c  hm.id..**..** Al
10dc9 6c 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 61  l other fields a
10dca 72 65 20 72 65 61 64 2f 77 72 69 74 65 2e 20 20  re read/write.  
10dcb 54 68 65 20 75 6e 69 78 53 68 6d 2e 70 46 69 6c  The unixShm.pFil
10dcc 65 2d 3e 6d 75 74 65 78 20 6d 75 73 74 20 62 65  e->mutex must be
10dcd 20 68 65 6c 64 0d 0a 2a 2a 20 77 68 69 6c 65 20   held..** while 
10dce 61 63 63 65 73 73 69 6e 67 20 61 6e 79 20 72 65  accessing any re
10dcf 61 64 2f 77 72 69 74 65 20 66 69 65 6c 64 73 2e  ad/write fields.
10dd0 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 75 6e 69  ..*/..struct uni
10dd1 78 53 68 6d 20 7b 0d 0a 20 20 75 6e 69 78 53 68  xShm {..  unixSh
10dd2 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b  mNode *pShmNode;
10dd3 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65       /* The unde
10dd4 72 6c 79 69 6e 67 20 75 6e 69 78 53 68 6d 4e 6f  rlying unixShmNo
10dd5 64 65 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20  de object */..  
10dd6 75 6e 69 78 53 68 6d 20 2a 70 4e 65 78 74 3b 20  unixShm *pNext; 
10dd7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
10dd8 78 74 20 75 6e 69 78 53 68 6d 20 77 69 74 68 20  xt unixShm with 
10dd9 74 68 65 20 73 61 6d 65 20 75 6e 69 78 53 68 6d  the same unixShm
10dda 4e 6f 64 65 20 2a 2f 0d 0a 20 20 75 38 20 68 61  Node */..  u8 ha
10ddb 73 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  sMutex;         
10ddc 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
10ddd 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 75 6e 69   holding the uni
10dde 78 53 68 6d 4e 6f 64 65 20 6d 75 74 65 78 20 2a  xShmNode mutex *
10ddf 2f 0d 0a 20 20 75 38 20 69 64 3b 20 20 20 20 20  /..  u8 id;     
10de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10de1 2f 2a 20 49 64 20 6f 66 20 74 68 69 73 20 63 6f  /* Id of this co
10de2 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20  nnection within 
10de3 69 74 73 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20  its unixShmNode 
10de4 2a 2f 0d 0a 20 20 75 31 36 20 73 68 61 72 65 64  */..  u16 shared
10de5 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Mask;           
10de6 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72   /* Mask of shar
10de7 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f  ed locks held */
10de8 0d 0a 20 20 75 31 36 20 65 78 63 6c 4d 61 73 6b  ..  u16 exclMask
10de9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
10dea 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63 6c 75 73  * Mask of exclus
10deb 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a  ive locks held *
10dec 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  /..};..../*..** 
10ded 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64 20 66  Constants used f
10dee 6f 72 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2f 0d 0a  or locking..*/..
10def 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48 4d  #define UNIX_SHM
10df0 5f 42 41 53 45 20 20 20 28 28 32 32 2b 53 51 4c  _BASE   ((22+SQL
10df1 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34  ITE_SHM_NLOCK)*4
10df2 29 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72  )         /* fir
10df3 73 74 20 6c 6f 63 6b 20 62 79 74 65 20 2a 2f 0d  st lock byte */.
10df4 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48  .#define UNIX_SH
10df5 4d 5f 44 4d 53 20 20 20 20 28 55 4e 49 58 5f 53  M_DMS    (UNIX_S
10df6 48 4d 5f 42 41 53 45 2b 53 51 4c 49 54 45 5f 53  HM_BASE+SQLITE_S
10df7 48 4d 5f 4e 4c 4f 43 4b 29 20 20 2f 2a 20 64 65  HM_NLOCK)  /* de
10df8 61 64 6d 61 6e 20 73 77 69 74 63 68 20 2a 2f 0d  adman switch */.
10df9 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 70 70 6c 79 20  .../*..** Apply 
10dfa 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c  posix advisory l
10dfb 6f 63 6b 73 20 66 6f 72 20 61 6c 6c 20 62 79 74  ocks for all byt
10dfc 65 73 20 66 72 6f 6d 20 6f 66 73 74 20 74 68 72  es from ofst thr
10dfd 6f 75 67 68 20 6f 66 73 74 2b 6e 2d 31 2e 0d 0a  ough ofst+n-1...
10dfe 2a 2a 0d 0a 2a 2a 20 4c 6f 63 6b 73 20 62 6c 6f  **..** Locks blo
10dff 63 6b 20 69 66 20 74 68 65 20 6d 61 73 6b 20 69  ck if the mask i
10e00 73 20 65 78 61 63 74 6c 79 20 55 4e 49 58 5f 53  s exactly UNIX_S
10e01 48 4d 5f 43 20 61 6e 64 20 61 72 65 20 6e 6f 6e  HM_C and are non
10e02 2d 62 6c 6f 63 6b 69 6e 67 0d 0a 2a 2a 20 6f 74  -blocking..** ot
10e03 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a 73 74  herwise...*/..st
10e04 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 68 6d  atic int unixShm
10e05 53 79 73 74 65 6d 4c 6f 63 6b 28 0d 0a 20 20 75  SystemLock(..  u
10e06 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d  nixShmNode *pShm
10e07 4e 6f 64 65 2c 20 2f 2a 20 41 70 70 6c 79 20 6c  Node, /* Apply l
10e08 6f 63 6b 73 20 74 6f 20 74 68 69 73 20 6f 70 65  ocks to this ope
10e09 6e 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  n shared-memory 
10e0a 73 65 67 6d 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e  segment */..  in
10e0b 74 20 6c 6f 63 6b 54 79 70 65 2c 20 20 20 20 20  t lockType,     
10e0c 20 20 20 20 20 2f 2a 20 46 5f 55 4e 4c 43 4b 2c       /* F_UNLCK,
10e0d 20 46 5f 52 44 4c 43 4b 2c 20 6f 72 20 46 5f 57   F_RDLCK, or F_W
10e0e 52 4c 43 4b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f  RLCK */..  int o
10e0f 66 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  fst,            
10e10 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
10e11 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72  of the locking r
10e12 61 6e 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ange */..  int n
10e13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e14 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
10e15 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0d  ytes to lock */.
10e16 0a 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 66 6c  .){..  struct fl
10e17 6f 63 6b 20 66 3b 20 20 20 20 20 20 20 2f 2a 20  ock f;       /* 
10e18 54 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f  The posix adviso
10e19 72 79 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63  ry locking struc
10e1a 74 75 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  ture */..  int r
10e1b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
10e1c 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
10e1d 66 6f 72 6d 20 66 63 6e 74 6c 28 29 20 2a 2f 0d  form fcntl() */.
10e1e 0a 0d 0a 20 20 2f 2a 20 41 63 63 65 73 73 20 74  ...  /* Access t
10e1f 6f 20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64  o the unixShmNod
10e20 65 20 6f 62 6a 65 63 74 20 69 73 20 73 65 72 69  e object is seri
10e21 61 6c 69 7a 65 64 20 62 79 20 74 68 65 20 63 61  alized by the ca
10e22 6c 6c 65 72 20 2a 2f 0d 0a 20 20 61 73 73 65 72  ller */..  asser
10e23 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
10e24 5f 68 65 6c 64 28 70 53 68 6d 4e 6f 64 65 2d 3e  _held(pShmNode->
10e25 6d 75 74 65 78 29 20 7c 7c 20 70 53 68 6d 4e 6f  mutex) || pShmNo
10e26 64 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0d 0a  de->nRef==0 );..
10e27 0d 0a 20 20 2f 2a 20 53 68 61 72 65 64 20 6c 6f  ..  /* Shared lo
10e28 63 6b 73 20 6e 65 76 65 72 20 73 70 61 6e 20 6d  cks never span m
10e29 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 79 74  ore than one byt
10e2a 65 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
10e2b 6e 3d 3d 31 20 7c 7c 20 6c 6f 63 6b 54 79 70 65  n==1 || lockType
10e2c 21 3d 46 5f 52 44 4c 43 4b 20 29 3b 0d 0a 0d 0a  !=F_RDLCK );....
10e2d 20 20 2f 2a 20 4c 6f 63 6b 73 20 61 72 65 20 77    /* Locks are w
10e2e 69 74 68 69 6e 20 72 61 6e 67 65 20 2a 2f 0d 0a  ithin range */..
10e2f 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 26    assert( n>=1 &
10e30 26 20 6e 3c 53 51 4c 49 54 45 5f 53 48 4d 5f 4e  & n<SQLITE_SHM_N
10e31 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  LOCK );....  if(
10e32 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30 20   pShmNode->h>=0 
10e33 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69  ){..    /* Initi
10e34 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 6b 69 6e  alize the lockin
10e35 67 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0d  g parameters */.
10e36 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 66 2c 20  .    memset(&f, 
10e37 30 2c 20 73 69 7a 65 6f 66 28 66 29 29 3b 0d 0a  0, sizeof(f));..
10e38 20 20 20 20 66 2e 6c 5f 74 79 70 65 20 3d 20 6c      f.l_type = l
10e39 6f 63 6b 54 79 70 65 3b 0d 0a 20 20 20 20 66 2e  ockType;..    f.
10e3a 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
10e3b 53 45 54 3b 0d 0a 20 20 20 20 66 2e 6c 5f 73 74  SET;..    f.l_st
10e3c 61 72 74 20 3d 20 6f 66 73 74 3b 0d 0a 20 20 20  art = ofst;..   
10e3d 20 66 2e 6c 5f 6c 65 6e 20 3d 20 6e 3b 0d 0a 0d   f.l_len = n;...
10e3e 0a 20 20 20 20 72 63 20 3d 20 6f 73 46 63 6e 74  .    rc = osFcnt
10e3f 6c 28 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 46  l(pShmNode->h, F
10e40 5f 53 45 54 4c 4b 2c 20 26 66 29 3b 0d 0a 20 20  _SETLK, &f);..  
10e41 20 20 72 63 20 3d 20 28 72 63 21 3d 28 2d 31 29    rc = (rc!=(-1)
10e42 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  ) ? SQLITE_OK : 
10e43 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20  SQLITE_BUSY;..  
10e44 7d 0d 0a 0d 0a 20 20 2f 2a 20 55 70 64 61 74 65  }....  /* Update
10e45 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 6f 63 6b   the global lock
10e46 20 73 74 61 74 65 20 61 6e 64 20 64 6f 20 64 65   state and do de
10e47 62 75 67 20 74 72 61 63 69 6e 67 20 2a 2f 0d 0a  bug tracing */..
10e48 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
10e49 42 55 47 0d 0a 20 20 7b 20 75 31 36 20 6d 61 73  BUG..  { u16 mas
10e4a 6b 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22  k;..  OSTRACE(("
10e4b 53 48 4d 2d 4c 4f 43 4b 20 22 29 29 3b 0d 0a 20  SHM-LOCK "));.. 
10e4c 20 6d 61 73 6b 20 3d 20 28 31 3c 3c 28 6f 66 73   mask = (1<<(ofs
10e4d 74 2b 6e 29 29 20 2d 20 28 31 3c 3c 6f 66 73 74  t+n)) - (1<<ofst
10e4e 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
10e4f 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
10e50 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d 46 5f  if( lockType==F_
10e51 55 4e 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 20 20  UNLCK ){..      
10e52 4f 53 54 52 41 43 45 28 28 22 75 6e 6c 6f 63 6b  OSTRACE(("unlock
10e53 20 25 64 20 6f 6b 22 2c 20 6f 66 73 74 29 29 3b   %d ok", ofst));
10e54 0d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65  ..      pShmNode
10e55 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e 6d  ->exclMask &= ~m
10e56 61 73 6b 3b 0d 0a 20 20 20 20 20 20 70 53 68 6d  ask;..      pShm
10e57 4e 6f 64 65 2d 3e 73 68 61 72 65 64 4d 61 73 6b  Node->sharedMask
10e58 20 26 3d 20 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20   &= ~mask;..    
10e59 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 54 79  }else if( lockTy
10e5a 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0d 0a  pe==F_RDLCK ){..
10e5b 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22        OSTRACE(("
10e5c 72 65 61 64 2d 6c 6f 63 6b 20 25 64 20 6f 6b 22  read-lock %d ok"
10e5d 2c 20 6f 66 73 74 29 29 3b 0d 0a 20 20 20 20 20  , ofst));..     
10e5e 20 70 53 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d   pShmNode->exclM
10e5f 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0d 0a 20  ask &= ~mask;.. 
10e60 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 73       pShmNode->s
10e61 68 61 72 65 64 4d 61 73 6b 20 7c 3d 20 6d 61 73  haredMask |= mas
10e62 6b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  k;..    }else{..
10e63 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f        assert( lo
10e64 63 6b 54 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20  ckType==F_WRLCK 
10e65 29 3b 0d 0a 20 20 20 20 20 20 4f 53 54 52 41 43  );..      OSTRAC
10e66 45 28 28 22 77 72 69 74 65 2d 6c 6f 63 6b 20 25  E(("write-lock %
10e67 64 20 6f 6b 22 2c 20 6f 66 73 74 29 29 3b 0d 0a  d ok", ofst));..
10e68 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e        pShmNode->
10e69 65 78 63 6c 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b  exclMask |= mask
10e6a 3b 0d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64  ;..      pShmNod
10e6b 65 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 3d  e->sharedMask &=
10e6c 20 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   ~mask;..    }..
10e6d 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 66    }else{..    if
10e6e 28 20 6c 6f 63 6b 54 79 70 65 3d 3d 46 5f 55 4e  ( lockType==F_UN
10e6f 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 20 20 4f 53  LCK ){..      OS
10e70 54 52 41 43 45 28 28 22 75 6e 6c 6f 63 6b 20 25  TRACE(("unlock %
10e71 64 20 66 61 69 6c 65 64 22 2c 20 6f 66 73 74 29  d failed", ofst)
10e72 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  );..    }else if
10e73 28 20 6c 6f 63 6b 54 79 70 65 3d 3d 46 5f 52 44  ( lockType==F_RD
10e74 4c 43 4b 20 29 7b 0d 0a 20 20 20 20 20 20 4f 53  LCK ){..      OS
10e75 54 52 41 43 45 28 28 22 72 65 61 64 2d 6c 6f 63  TRACE(("read-loc
10e76 6b 20 66 61 69 6c 65 64 22 29 29 3b 0d 0a 20 20  k failed"));..  
10e77 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
10e78 61 73 73 65 72 74 28 20 6c 6f 63 6b 54 79 70 65  assert( lockType
10e79 3d 3d 46 5f 57 52 4c 43 4b 20 29 3b 0d 0a 20 20  ==F_WRLCK );..  
10e7a 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 77 72      OSTRACE(("wr
10e7b 69 74 65 2d 6c 6f 63 6b 20 25 64 20 66 61 69 6c  ite-lock %d fail
10e7c 65 64 22 2c 20 6f 66 73 74 29 29 3b 0d 0a 20 20  ed", ofst));..  
10e7d 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 4f 53 54 52    }..  }..  OSTR
10e7e 41 43 45 28 28 22 20 2d 20 61 66 74 65 72 77 61  ACE((" - afterwa
10e7f 72 64 73 20 25 30 33 78 2c 25 30 33 78 5c 6e 22  rds %03x,%03x\n"
10e80 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 70 53  ,..           pS
10e81 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65 64 4d 61  hmNode->sharedMa
10e82 73 6b 2c 20 70 53 68 6d 4e 6f 64 65 2d 3e 65 78  sk, pShmNode->ex
10e83 63 6c 4d 61 73 6b 29 29 3b 0d 0a 20 20 7d 0d 0a  clMask));..  }..
10e84 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 72 65 74 75  #endif....  retu
10e85 72 6e 20 72 63 3b 20 20 20 20 20 20 20 20 0d 0a  rn rc;        ..
10e86 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 75  }....../*..** Pu
10e87 72 67 65 20 74 68 65 20 75 6e 69 78 53 68 6d 4e  rge the unixShmN
10e88 6f 64 65 4c 69 73 74 20 6c 69 73 74 20 6f 66 20  odeList list of 
10e89 61 6c 6c 20 65 6e 74 72 69 65 73 20 77 69 74 68  all entries with
10e8a 20 75 6e 69 78 53 68 6d 4e 6f 64 65 2e 6e 52 65   unixShmNode.nRe
10e8b 66 3d 3d 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  f==0...**..** Th
10e8c 69 73 20 69 73 20 6e 6f 74 20 61 20 56 46 53 20  is is not a VFS 
10e8d 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65  shared-memory me
10e8e 74 68 6f 64 3b 20 69 74 20 69 73 20 61 20 75 74  thod; it is a ut
10e8f 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 20 63  ility function c
10e90 61 6c 6c 65 64 0d 0a 2a 2a 20 62 79 20 56 46 53  alled..** by VFS
10e91 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d   shared-memory m
10e92 65 74 68 6f 64 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  ethods...*/..sta
10e93 74 69 63 20 76 6f 69 64 20 75 6e 69 78 53 68 6d  tic void unixShm
10e94 50 75 72 67 65 28 75 6e 69 78 46 69 6c 65 20 2a  Purge(unixFile *
10e95 70 46 64 29 7b 0d 0a 20 20 75 6e 69 78 53 68 6d  pFd){..  unixShm
10e96 4e 6f 64 65 20 2a 70 20 3d 20 70 46 64 2d 3e 70  Node *p = pFd->p
10e97 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 3b  Inode->pShmNode;
10e98 0d 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78  ..  assert( unix
10e99 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0d 0a  MutexHeld() );..
10e9a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 6e 52    if( p && p->nR
10e9b 65 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e  ef==0 ){..    in
10e9c 74 20 69 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  t i;..    assert
10e9d 28 20 70 2d 3e 70 49 6e 6f 64 65 3d 3d 70 46 64  ( p->pInode==pFd
10e9e 2d 3e 70 49 6e 6f 64 65 20 29 3b 0d 0a 20 20 20  ->pInode );..   
10e9f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
10ea0 72 65 65 28 70 2d 3e 6d 75 74 65 78 29 3b 0d 0a  ree(p->mutex);..
10ea1 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
10ea2 2d 3e 6e 52 65 67 69 6f 6e 3b 20 69 2b 2b 29 7b  ->nRegion; i++){
10ea3 0d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 68  ..      if( p->h
10ea4 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  >=0 ){..        
10ea5 6d 75 6e 6d 61 70 28 70 2d 3e 61 70 52 65 67 69  munmap(p->apRegi
10ea6 6f 6e 5b 69 5d 2c 20 70 2d 3e 73 7a 52 65 67 69  on[i], p->szRegi
10ea7 6f 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  on);..      }els
10ea8 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e{..        sqli
10ea9 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 52 65  te3_free(p->apRe
10eaa 67 69 6f 6e 5b 69 5d 29 3b 0d 0a 20 20 20 20 20  gion[i]);..     
10eab 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73   }..    }..    s
10eac 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
10ead 70 52 65 67 69 6f 6e 29 3b 0d 0a 20 20 20 20 69  pRegion);..    i
10eae 66 28 20 70 2d 3e 68 3e 3d 30 20 29 7b 0d 0a 20  f( p->h>=0 ){.. 
10eaf 20 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73       robust_clos
10eb0 65 28 70 46 64 2c 20 70 2d 3e 68 2c 20 5f 5f 4c  e(pFd, p->h, __L
10eb1 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20 20 20 70  INE__);..      p
10eb2 2d 3e 68 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d  ->h = -1;..    }
10eb3 0d 0a 20 20 20 20 70 2d 3e 70 49 6e 6f 64 65 2d  ..    p->pInode-
10eb4 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 30 3b 0d 0a  >pShmNode = 0;..
10eb5 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10eb6 28 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  (p);..  }..}....
10eb7 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e 20 61 20 73 68  /*..** Open a sh
10eb8 61 72 65 64 2d 6d 65 6d 6f 72 79 20 61 72 65 61  ared-memory area
10eb9 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
10eba 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66   open database f
10ebb 69 6c 65 20 70 44 62 46 64 2e 20 20 0d 0a 2a 2a  ile pDbFd.  ..**
10ebc 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   This particular
10ebd 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10ebe 75 73 65 73 20 6d 6d 61 70 70 65 64 20 66 69 6c  uses mmapped fil
10ebf 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  es...**..** The 
10ec0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 69 6d 70  file used to imp
10ec1 6c 65 6d 65 6e 74 20 73 68 61 72 65 64 2d 6d 65  lement shared-me
10ec2 6d 6f 72 79 20 69 73 20 69 6e 20 74 68 65 20 73  mory is in the s
10ec3 61 6d 65 20 64 69 72 65 63 74 6f 72 79 0d 0a 2a  ame directory..*
10ec4 2a 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61  * as the open da
10ec5 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
10ec6 68 61 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  has the same nam
10ec7 65 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61  e as the open da
10ec8 74 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 20  tabase..** file 
10ec9 77 69 74 68 20 74 68 65 20 22 2d 73 68 6d 22 20  with the "-shm" 
10eca 73 75 66 66 69 78 20 61 64 64 65 64 2e 20 20 46  suffix added.  F
10ecb 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  or example, if t
10ecc 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10ecd 0d 0a 2a 2a 20 69 73 20 22 2f 68 6f 6d 65 2f 75  ..** is "/home/u
10ece 73 65 72 31 2f 63 6f 6e 66 69 67 2e 64 62 22 20  ser1/config.db" 
10ecf 74 68 65 6e 20 74 68 65 20 66 69 6c 65 20 74 68  then the file th
10ed0 61 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e  at is created an
10ed1 64 20 6d 6d 61 70 70 65 64 0d 0a 2a 2a 20 66 6f  d mmapped..** fo
10ed2 72 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  r shared memory 
10ed3 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 22  will be called "
10ed4 2f 68 6f 6d 65 2f 75 73 65 72 31 2f 63 6f 6e 66  /home/user1/conf
10ed5 69 67 2e 64 62 2d 73 68 6d 22 2e 20 20 0d 0a 2a  ig.db-shm".  ..*
10ed6 2a 0d 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 61 70  *..** Another ap
10ed7 70 72 6f 61 63 68 20 74 6f 20 69 73 20 74 6f 20  proach to is to 
10ed8 75 73 65 20 66 69 6c 65 73 20 69 6e 20 2f 64 65  use files in /de
10ed9 76 2f 73 68 6d 20 6f 72 20 2f 64 65 76 2f 74 6d  v/shm or /dev/tm
10eda 70 20 6f 72 20 61 6e 0d 0a 2a 2a 20 73 6f 6d 65  p or an..** some
10edb 20 6f 74 68 65 72 20 74 6d 70 66 73 20 6d 6f 75   other tmpfs mou
10edc 6e 74 2e 20 42 75 74 20 69 66 20 61 20 66 69 6c  nt. But if a fil
10edd 65 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  e in a different
10ede 20 64 69 72 65 63 74 6f 72 79 0d 0a 2a 2a 20 66   directory..** f
10edf 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
10ee0 20 66 69 6c 65 20 69 73 20 75 73 65 64 2c 20 74   file is used, t
10ee1 68 65 6e 20 64 69 66 66 65 72 69 6e 67 20 61 63  hen differing ac
10ee2 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73  cess permissions
10ee3 0d 0a 2a 2a 20 6f 72 20 61 20 63 68 72 6f 6f 74  ..** or a chroot
10ee4 28 29 20 6d 69 67 68 74 20 63 61 75 73 65 20 74  () might cause t
10ee5 77 6f 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f  wo different pro
10ee6 63 65 73 73 65 73 20 6f 6e 20 74 68 65 20 73 61  cesses on the sa
10ee7 6d 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  me..** database 
10ee8 74 6f 20 65 6e 64 20 75 70 20 75 73 69 6e 67 20  to end up using 
10ee9 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 73 20  different files 
10eea 66 6f 72 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  for shared memor
10eeb 79 20 2d 20 0d 0a 2a 2a 20 6d 65 61 6e 69 6e 67  y - ..** meaning
10eec 20 74 68 61 74 20 74 68 65 69 72 20 6d 65 6d 6f   that their memo
10eed 72 79 20 77 6f 75 6c 64 20 6e 6f 74 20 72 65 61  ry would not rea
10eee 6c 6c 79 20 62 65 20 73 68 61 72 65 64 20 2d 20  lly be shared - 
10eef 72 65 73 75 6c 74 69 6e 67 0d 0a 2a 2a 20 69 6e  resulting..** in
10ef0 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
10ef1 74 69 6f 6e 2e 20 20 4e 65 76 65 72 74 68 65 6c  tion.  Neverthel
10ef2 65 73 73 2c 20 74 68 69 73 20 74 6d 70 66 73 20  ess, this tmpfs 
10ef3 66 69 6c 65 20 75 73 61 67 65 0d 0a 2a 2a 20 63  file usage..** c
10ef4 61 6e 20 62 65 20 65 6e 61 62 6c 65 64 20 61 74  an be enabled at
10ef5 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 75 73   compile-time us
10ef6 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 53 48 4d  ing -DSQLITE_SHM
10ef7 5f 44 49 52 45 43 54 4f 52 59 3d 22 2f 64 65 76  _DIRECTORY="/dev
10ef8 2f 73 68 6d 22 0d 0a 2a 2a 20 6f 72 20 74 68 65  /shm"..** or the
10ef9 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 54 68   equivalent.  Th
10efa 65 20 75 73 65 20 6f 66 20 74 68 65 20 53 51 4c  e use of the SQL
10efb 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52  ITE_SHM_DIRECTOR
10efc 59 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0d 0a  Y compile-time..
10efd 2a 2a 20 6f 70 74 69 6f 6e 20 72 65 73 75 6c 74  ** option result
10efe 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74  s in an incompat
10eff 69 62 6c 65 20 62 75 69 6c 64 20 6f 66 20 53 51  ible build of SQ
10f00 4c 69 74 65 3b 20 20 62 75 69 6c 64 73 20 6f 66  Lite;  builds of
10f01 20 53 51 4c 69 74 65 0d 0a 2a 2a 20 74 68 61 74   SQLite..** that
10f02 20 77 69 74 68 20 64 69 66 66 65 72 69 6e 67 20   with differing 
10f03 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43  SQLITE_SHM_DIREC
10f04 54 4f 52 59 20 73 65 74 74 69 6e 67 73 20 61 74  TORY settings at
10f05 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65  tempt to use the
10f06 0d 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 62 61  ..** same databa
10f07 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73  se file at the s
10f08 61 6d 65 20 74 69 6d 65 2c 20 64 61 74 61 62 61  ame time, databa
10f09 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69  se corruption wi
10f0a 6c 6c 20 6c 69 6b 65 6c 79 0d 0a 2a 2a 20 72 65  ll likely..** re
10f0b 73 75 6c 74 2e 20 54 68 65 20 53 51 4c 49 54 45  sult. The SQLITE
10f0c 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 20 63  _SHM_DIRECTORY c
10f0d 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
10f0e 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  on is considered
10f0f 0d 0a 2a 2a 20 22 75 6e 73 75 70 70 6f 72 74 65  ..** "unsupporte
10f10 64 22 20 61 6e 64 20 6d 61 79 20 67 6f 20 61 77  d" and may go aw
10f11 61 79 20 69 6e 20 61 20 66 75 74 75 72 65 20 53  ay in a future S
10f12 51 4c 69 74 65 20 72 65 6c 65 61 73 65 2e 0d 0a  QLite release...
10f13 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 6f 70 65 6e  **..** When open
10f14 69 6e 67 20 61 20 6e 65 77 20 73 68 61 72 65 64  ing a new shared
10f15 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2c 20 69 66  -memory file, if
10f16 20 6e 6f 20 6f 74 68 65 72 20 69 6e 73 74 61 6e   no other instan
10f17 63 65 73 20 6f 66 20 74 68 61 74 0d 0a 2a 2a 20  ces of that..** 
10f18 66 69 6c 65 20 61 72 65 20 63 75 72 72 65 6e 74  file are current
10f19 6c 79 20 6f 70 65 6e 2c 20 69 6e 20 74 68 69 73  ly open, in this
10f1a 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 6f   process or in o
10f1b 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2c 20  ther processes, 
10f1c 74 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 66 69 6c  then..** the fil
10f1d 65 20 6d 75 73 74 20 62 65 20 74 72 75 6e 63 61  e must be trunca
10f1e 74 65 64 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67  ted to zero leng
10f1f 74 68 20 6f 72 20 68 61 76 65 20 69 74 73 20 68  th or have its h
10f20 65 61 64 65 72 20 63 6c 65 61 72 65 64 2e 0d 0a  eader cleared...
10f21 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72  **..** If the or
10f22 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
10f23 66 69 6c 65 20 28 70 44 62 46 64 29 20 69 73 20  file (pDbFd) is 
10f24 75 73 69 6e 67 20 74 68 65 20 22 75 6e 69 78 2d  using the "unix-
10f25 65 78 63 6c 22 20 56 46 53 0d 0a 2a 2a 20 74 68  excl" VFS..** th
10f26 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e  at means that an
10f27 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
10f28 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64  is held on the d
10f29 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
10f2a 0d 0a 2a 2a 20 74 68 61 74 20 6e 6f 20 6f 74 68  ..** that no oth
10f2b 65 72 20 70 72 6f 63 65 73 73 65 73 20 61 72 65  er processes are
10f2c 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 6f 72   able to read or
10f2d 20 77 72 69 74 65 20 74 68 65 20 64 61 74 61 62   write the datab
10f2e 61 73 65 2e 20 20 49 6e 0d 0a 2a 2a 20 74 68 61  ase.  In..** tha
10f2f 74 20 63 61 73 65 2c 20 77 65 20 64 6f 20 6e 6f  t case, we do no
10f30 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 20 73 68  t really need sh
10f31 61 72 65 64 20 6d 65 6d 6f 72 79 2e 20 20 4e 6f  ared memory.  No
10f32 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 0d 0a   shared memory..
10f33 2a 2a 20 66 69 6c 65 20 69 73 20 63 72 65 61 74  ** file is creat
10f34 65 64 2e 20 20 54 68 65 20 73 68 61 72 65 64 20  ed.  The shared 
10f35 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 20 73  memory will be s
10f36 69 6d 75 6c 61 74 65 64 20 77 69 74 68 20 68 65  imulated with he
10f37 61 70 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a  ap memory...*/..
10f38 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4f  static int unixO
10f39 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 28  penSharedMemory(
10f3a 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 29  unixFile *pDbFd)
10f3b 7b 0d 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  {..  struct unix
10f3c 53 68 6d 20 2a 70 20 3d 20 30 3b 20 20 20 20 20  Shm *p = 0;     
10f3d 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e       /* The conn
10f3e 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 6f 70 65  ection to be ope
10f3f 6e 65 64 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  ned */..  struct
10f40 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53   unixShmNode *pS
10f41 68 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 54 68 65  hmNode;   /* The
10f42 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d 61 70   underlying mmap
10f43 70 65 64 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69  ped file */..  i
10f44 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
10f45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10f46 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
10f47 0d 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66  ..  unixInodeInf
10f48 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20 20 20 20  o *pInode;      
10f49 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 6f 64 65      /* The inode
10f4a 20 6f 66 20 66 64 20 2a 2f 0d 0a 20 20 63 68 61   of fd */..  cha
10f4b 72 20 2a 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 3b  r *zShmFilename;
10f4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10f4d 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
10f4e 20 75 73 65 64 20 66 6f 72 20 53 48 4d 20 2a 2f   used for SHM */
10f4f 0d 0a 20 20 69 6e 74 20 6e 53 68 6d 46 69 6c 65  ..  int nShmFile
10f50 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
10f51 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
10f52 68 65 20 53 48 4d 20 66 69 6c 65 6e 61 6d 65 20  he SHM filename 
10f53 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 0d 0a 20  in bytes */.... 
10f54 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
10f55 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 75  ce for the new u
10f56 6e 69 78 53 68 6d 20 6f 62 6a 65 63 74 2e 20 2a  nixShm object. *
10f57 2f 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  /..  p = sqlite3
10f58 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
10f59 2a 70 29 20 29 3b 0d 0a 20 20 69 66 28 20 70 3d  *p) );..  if( p=
10f5a 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
10f5b 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 6d 65 6d  TE_NOMEM;..  mem
10f5c 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
10f5d 28 2a 70 29 29 3b 0d 0a 20 20 61 73 73 65 72 74  (*p));..  assert
10f5e 28 20 70 44 62 46 64 2d 3e 70 53 68 6d 3d 3d 30  ( pDbFd->pShm==0
10f5f 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63   );....  /* Chec
10f60 6b 20 74 6f 20 73 65 65 20 69 66 20 61 20 75 6e  k to see if a un
10f61 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74  ixShmNode object
10f62 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
10f63 20 52 65 75 73 65 20 61 6e 20 65 78 69 73 74 69   Reuse an existi
10f64 6e 67 0d 0a 20 20 2a 2a 20 6f 6e 65 20 69 66 20  ng..  ** one if 
10f65 70 72 65 73 65 6e 74 2e 20 43 72 65 61 74 65 20  present. Create 
10f66 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63  a new one if nec
10f67 65 73 73 61 72 79 2e 0d 0a 20 20 2a 2f 0d 0a 20  essary...  */.. 
10f68 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
10f69 29 3b 0d 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70  );..  pInode = p
10f6a 44 62 46 64 2d 3e 70 49 6e 6f 64 65 3b 0d 0a 20  DbFd->pInode;.. 
10f6b 20 70 53 68 6d 4e 6f 64 65 20 3d 20 70 49 6e 6f   pShmNode = pIno
10f6c 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0d 0a 20  de->pShmNode;.. 
10f6d 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 30   if( pShmNode==0
10f6e 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20   ){..    struct 
10f6f 73 74 61 74 20 73 53 74 61 74 3b 20 20 20 20 20  stat sStat;     
10f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
10f71 73 74 61 74 28 29 20 69 6e 66 6f 20 66 6f 72 20  stat() info for 
10f72 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
10f73 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20  ....    /* Call 
10f74 66 73 74 61 74 28 29 20 74 6f 20 66 69 67 75 72  fstat() to figur
10f75 65 20 6f 75 74 20 74 68 65 20 70 65 72 6d 69 73  e out the permis
10f76 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 64 61 74  sions on the dat
10f77 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0d 0a  abase file. If..
10f78 20 20 20 20 2a 2a 20 61 20 6e 65 77 20 2a 2d 73      ** a new *-s
10f79 68 6d 20 66 69 6c 65 20 69 73 20 63 72 65 61 74  hm file is creat
10f7a 65 64 2c 20 61 6e 20 61 74 74 65 6d 70 74 20 77  ed, an attempt w
10f7b 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 63  ill be made to c
10f7c 72 65 61 74 65 20 69 74 0d 0a 20 20 20 20 2a 2a  reate it..    **
10f7d 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 70   with the same p
10f7e 65 72 6d 69 73 73 69 6f 6e 73 2e 20 54 68 65 20  ermissions. The 
10f7f 61 63 74 75 61 6c 20 70 65 72 6d 69 73 73 69 6f  actual permissio
10f80 6e 73 20 74 68 65 20 66 69 6c 65 20 69 73 20 63  ns the file is c
10f81 72 65 61 74 65 64 0d 0a 20 20 20 20 2a 2a 20 77  reated..    ** w
10f82 69 74 68 20 61 72 65 20 73 75 62 6a 65 63 74 20  ith are subject 
10f83 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 75  to the current u
10f84 6d 61 73 6b 20 73 65 74 74 69 6e 67 2e 0d 0a 20  mask setting... 
10f85 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 6f     */..    if( o
10f86 73 46 73 74 61 74 28 70 44 62 46 64 2d 3e 68 2c  sFstat(pDbFd->h,
10f87 20 26 73 53 74 61 74 29 20 26 26 20 70 49 6e 6f   &sStat) && pIno
10f88 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b  de->bProcessLock
10f89 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  ==0 ){..      rc
10f8a 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
10f8b 46 53 54 41 54 3b 0d 0a 20 20 20 20 20 20 67 6f  FSTAT;..      go
10f8c 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b  to shm_open_err;
10f8d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69 66 64 65  ..    }....#ifde
10f8e 66 20 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52  f SQLITE_SHM_DIR
10f8f 45 43 54 4f 52 59 0d 0a 20 20 20 20 6e 53 68 6d  ECTORY..    nShm
10f90 46 69 6c 65 6e 61 6d 65 20 3d 20 73 69 7a 65 6f  Filename = sizeo
10f91 66 28 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52  f(SQLITE_SHM_DIR
10f92 45 43 54 4f 52 59 29 20 2b 20 33 31 3b 0d 0a 23  ECTORY) + 31;..#
10f93 65 6c 73 65 0d 0a 20 20 20 20 6e 53 68 6d 46 69  else..    nShmFi
10f94 6c 65 6e 61 6d 65 20 3d 20 36 20 2b 20 28 69 6e  lename = 6 + (in
10f95 74 29 73 74 72 6c 65 6e 28 70 44 62 46 64 2d 3e  t)strlen(pDbFd->
10f96 7a 50 61 74 68 29 3b 0d 0a 23 65 6e 64 69 66 0d  zPath);..#endif.
10f97 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65 20 3d 20  .    pShmNode = 
10f98 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
10f99 73 69 7a 65 6f 66 28 2a 70 53 68 6d 4e 6f 64 65  sizeof(*pShmNode
10f9a 29 20 2b 20 6e 53 68 6d 46 69 6c 65 6e 61 6d 65  ) + nShmFilename
10f9b 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 53 68   );..    if( pSh
10f9c 6d 4e 6f 64 65 3d 3d 30 20 29 7b 0d 0a 20 20 20  mNode==0 ){..   
10f9d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
10f9e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 67 6f 74  OMEM;..      got
10f9f 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0d  o shm_open_err;.
10fa0 0a 20 20 20 20 7d 0d 0a 20 20 20 20 6d 65 6d 73  .    }..    mems
10fa1 65 74 28 70 53 68 6d 4e 6f 64 65 2c 20 30 2c 20  et(pShmNode, 0, 
10fa2 73 69 7a 65 6f 66 28 2a 70 53 68 6d 4e 6f 64 65  sizeof(*pShmNode
10fa3 29 2b 6e 53 68 6d 46 69 6c 65 6e 61 6d 65 29 3b  )+nShmFilename);
10fa4 0d 0a 20 20 20 20 7a 53 68 6d 46 69 6c 65 6e 61  ..    zShmFilena
10fa5 6d 65 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e 7a  me = pShmNode->z
10fa6 46 69 6c 65 6e 61 6d 65 20 3d 20 28 63 68 61 72  Filename = (char
10fa7 2a 29 26 70 53 68 6d 4e 6f 64 65 5b 31 5d 3b 0d  *)&pShmNode[1];.
10fa8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
10fa9 48 4d 5f 44 49 52 45 43 54 4f 52 59 0d 0a 20 20  HM_DIRECTORY..  
10faa 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
10fab 74 66 28 6e 53 68 6d 46 69 6c 65 6e 61 6d 65 2c  tf(nShmFilename,
10fac 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 2c 20 0d   zShmFilename, .
10fad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10fae 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d        SQLITE_SHM
10faf 5f 44 49 52 45 43 54 4f 52 59 20 22 2f 73 71 6c  _DIRECTORY "/sql
10fb0 69 74 65 2d 73 68 6d 2d 25 78 2d 25 78 22 2c 0d  ite-shm-%x-%x",.
10fb1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10fb2 20 20 20 20 20 20 28 75 33 32 29 73 53 74 61 74        (u32)sStat
10fb3 2e 73 74 5f 69 6e 6f 2c 20 28 75 33 32 29 73 53  .st_ino, (u32)sS
10fb4 74 61 74 2e 73 74 5f 64 65 76 29 3b 0d 0a 23 65  tat.st_dev);..#e
10fb5 6c 73 65 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  lse..    sqlite3
10fb6 5f 73 6e 70 72 69 6e 74 66 28 6e 53 68 6d 46 69  _snprintf(nShmFi
10fb7 6c 65 6e 61 6d 65 2c 20 7a 53 68 6d 46 69 6c 65  lename, zShmFile
10fb8 6e 61 6d 65 2c 20 22 25 73 2d 73 68 6d 22 2c 20  name, "%s-shm", 
10fb9 70 44 62 46 64 2d 3e 7a 50 61 74 68 29 3b 0d 0a  pDbFd->zPath);..
10fba 20 20 20 20 73 71 6c 69 74 65 33 46 69 6c 65 53      sqlite3FileS
10fbb 75 66 66 69 78 33 28 70 44 62 46 64 2d 3e 7a 50  uffix3(pDbFd->zP
10fbc 61 74 68 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d  ath, zShmFilenam
10fbd 65 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  e);..#endif..   
10fbe 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 20 3d 20 2d   pShmNode->h = -
10fbf 31 3b 0d 0a 20 20 20 20 70 44 62 46 64 2d 3e 70  1;..    pDbFd->p
10fc0 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 20  Inode->pShmNode 
10fc1 3d 20 70 53 68 6d 4e 6f 64 65 3b 0d 0a 20 20 20  = pShmNode;..   
10fc2 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64   pShmNode->pInod
10fc3 65 20 3d 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64  e = pDbFd->pInod
10fc4 65 3b 0d 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65  e;..    pShmNode
10fc5 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  ->mutex = sqlite
10fc6 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
10fc7 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29  LITE_MUTEX_FAST)
10fc8 3b 0d 0a 20 20 20 20 69 66 28 20 70 53 68 6d 4e  ;..    if( pShmN
10fc9 6f 64 65 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b  ode->mutex==0 ){
10fca 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
10fcb 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
10fcc 20 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f    goto shm_open_
10fcd 65 72 72 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  err;..    }.... 
10fce 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 62     if( pInode->b
10fcf 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29  ProcessLock==0 )
10fd0 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 65  {..      int ope
10fd1 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 44 57 52 20  nFlags = O_RDWR 
10fd2 7c 20 4f 5f 43 52 45 41 54 3b 0d 0a 20 20 20 20  | O_CREAT;..    
10fd3 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75 72    if( sqlite3_ur
10fd4 69 5f 62 6f 6f 6c 65 61 6e 28 70 44 62 46 64 2d  i_boolean(pDbFd-
10fd5 3e 7a 50 61 74 68 2c 20 22 72 65 61 64 6f 6e 6c  >zPath, "readonl
10fd6 79 5f 73 68 6d 22 2c 20 30 29 20 29 7b 0d 0a 20  y_shm", 0) ){.. 
10fd7 20 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73         openFlags
10fd8 20 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0d 0a 20 20   = O_RDONLY;..  
10fd9 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e        pShmNode->
10fda 69 73 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b 0d  isReadonly = 1;.
10fdb 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
10fdc 70 53 68 6d 4e 6f 64 65 2d 3e 68 20 3d 20 72 6f  pShmNode->h = ro
10fdd 62 75 73 74 5f 6f 70 65 6e 28 7a 53 68 6d 46 69  bust_open(zShmFi
10fde 6c 65 6e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67  lename, openFlag
10fdf 73 2c 20 28 73 53 74 61 74 2e 73 74 5f 6d 6f 64  s, (sStat.st_mod
10fe0 65 26 30 37 37 37 29 29 3b 0d 0a 20 20 20 20 20  e&0777));..     
10fe1 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68   if( pShmNode->h
10fe2 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  <0 ){..        i
10fe3 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30  f( pShmNode->h<0
10fe4 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   ){..          r
10fe5 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72  c = unixLogError
10fe6 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e  (SQLITE_CANTOPEN
10fe7 5f 42 4b 50 54 2c 20 22 6f 70 65 6e 22 2c 20 7a  _BKPT, "open", z
10fe8 53 68 6d 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20  ShmFilename);.. 
10fe9 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 68           goto sh
10fea 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0d 0a 20 20 20  m_open_err;..   
10feb 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
10fec 0a 20 20 0d 0a 20 20 20 20 20 20 2f 2a 20 43 68  .  ..      /* Ch
10fed 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e  eck to see if an
10fee 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73  other process is
10fef 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 64 65 61   holding the dea
10ff0 64 2d 6d 61 6e 20 73 77 69 74 63 68 2e 0d 0a 20  d-man switch... 
10ff1 20 20 20 20 20 2a 2a 20 49 66 20 6e 6f 74 2c 20       ** If not, 
10ff2 74 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c  truncate the fil
10ff3 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68  e to zero length
10ff4 2e 20 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  . ..      */..  
10ff5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
10ff6 4f 4b 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 75  OK;..      if( u
10ff7 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b  nixShmSystemLock
10ff8 28 70 53 68 6d 4e 6f 64 65 2c 20 46 5f 57 52 4c  (pShmNode, F_WRL
10ff9 43 4b 2c 20 55 4e 49 58 5f 53 48 4d 5f 44 4d 53  CK, UNIX_SHM_DMS
10ffa 2c 20 31 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  , 1)==SQLITE_OK 
10ffb 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
10ffc 72 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65  robust_ftruncate
10ffd 28 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 30 29  (pShmNode->h, 0)
10ffe 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   ){..          r
10fff 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72  c = unixLogError
11000 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  (SQLITE_IOERR_SH
11001 4d 4f 50 45 4e 2c 20 22 66 74 72 75 6e 63 61 74  MOPEN, "ftruncat
11002 65 22 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65  e", zShmFilename
11003 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
11004 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
11005 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11006 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
11007 20 75 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f   unixShmSystemLo
11008 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 46 5f 52  ck(pShmNode, F_R
11009 44 4c 43 4b 2c 20 55 4e 49 58 5f 53 48 4d 5f 44  DLCK, UNIX_SHM_D
1100a 4d 53 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 7d  MS, 1);..      }
1100b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ..      if( rc )
1100c 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65   goto shm_open_e
1100d 72 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  rr;..    }..  }.
1100e 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 74 68 65  ...  /* Make the
1100f 20 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   new connection 
11010 61 20 63 68 69 6c 64 20 6f 66 20 74 68 65 20 75  a child of the u
11011 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 2f 0d 0a 20  nixShmNode */.. 
11012 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70   p->pShmNode = p
11013 53 68 6d 4e 6f 64 65 3b 0d 0a 23 69 66 64 65 66  ShmNode;..#ifdef
11014 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
11015 20 70 2d 3e 69 64 20 3d 20 70 53 68 6d 4e 6f 64   p->id = pShmNod
11016 65 2d 3e 6e 65 78 74 53 68 6d 49 64 2b 2b 3b 0d  e->nextShmId++;.
11017 0a 23 65 6e 64 69 66 0d 0a 20 20 70 53 68 6d 4e  .#endif..  pShmN
11018 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20  ode->nRef++;..  
11019 70 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 70 3b  pDbFd->pShm = p;
1101a 0d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  ..  unixLeaveMut
1101b 65 78 28 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68  ex();....  /* Th
1101c 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
1101d 74 20 6f 6e 20 70 53 68 6d 4e 6f 64 65 20 68 61  t on pShmNode ha
1101e 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  s already been i
1101f 6e 63 72 65 6d 65 6e 74 65 64 20 75 6e 64 65 72  ncremented under
11020 0d 0a 20 20 2a 2a 20 74 68 65 20 63 6f 76 65 72  ..  ** the cover
11021 20 6f 66 20 74 68 65 20 75 6e 69 78 45 6e 74 65   of the unixEnte
11022 72 4d 75 74 65 78 28 29 20 6d 75 74 65 78 20 61  rMutex() mutex a
11023 6e 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 66  nd the pointer f
11024 72 6f 6d 20 74 68 65 0d 0a 20 20 2a 2a 20 6e 65  rom the..  ** ne
11025 77 20 28 73 74 72 75 63 74 20 75 6e 69 78 53 68  w (struct unixSh
11026 6d 29 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65  m) object to the
11027 20 70 53 68 6d 4e 6f 64 65 20 68 61 73 20 62 65   pShmNode has be
11028 65 6e 20 73 65 74 2e 20 41 6c 6c 20 74 68 61 74  en set. All that
11029 20 69 73 0d 0a 20 20 2a 2a 20 6c 65 66 74 20 74   is..  ** left t
1102a 6f 20 64 6f 20 69 73 20 74 6f 20 6c 69 6e 6b 20  o do is to link 
1102b 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 20 69  the new object i
1102c 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  nto the linked l
1102d 69 73 74 20 73 74 61 72 74 69 6e 67 0d 0a 20 20  ist starting..  
1102e 2a 2a 20 61 74 20 70 53 68 6d 4e 6f 64 65 2d 3e  ** at pShmNode->
1102f 70 46 69 72 73 74 2e 20 54 68 69 73 20 6d 75 73  pFirst. This mus
11030 74 20 62 65 20 64 6f 6e 65 20 77 68 69 6c 65 20  t be done while 
11031 68 6f 6c 64 69 6e 67 20 74 68 65 20 70 53 68 6d  holding the pShm
11032 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 0d 0a 20 20  Node->mutex ..  
11033 2a 2a 20 6d 75 74 65 78 2e 0d 0a 20 20 2a 2f 0d  ** mutex...  */.
11034 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
11035 5f 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d  _enter(pShmNode-
11036 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 70 2d 3e 70  >mutex);..  p->p
11037 4e 65 78 74 20 3d 20 70 53 68 6d 4e 6f 64 65 2d  Next = pShmNode-
11038 3e 70 46 69 72 73 74 3b 0d 0a 20 20 70 53 68 6d  >pFirst;..  pShm
11039 4e 6f 64 65 2d 3e 70 46 69 72 73 74 20 3d 20 70  Node->pFirst = p
1103a 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
1103b 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64  ex_leave(pShmNod
1103c 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  e->mutex);..  re
1103d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
1103e 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  ...  /* Jump her
1103f 65 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 20 2a  e on any error *
11040 2f 0d 0a 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3a  /..shm_open_err:
11041 0d 0a 20 20 75 6e 69 78 53 68 6d 50 75 72 67 65  ..  unixShmPurge
11042 28 70 44 62 46 64 29 3b 20 20 20 20 20 20 20 2f  (pDbFd);       /
11043 2a 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65  * This call free
11044 73 20 70 53 68 6d 4e 6f 64 65 20 69 66 20 72 65  s pShmNode if re
11045 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 73 71 6c  quired */..  sql
11046 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
11047 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
11048 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
11049 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
1104a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1104b 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20  alled to obtain 
1104c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 67  a pointer to reg
1104d 69 6f 6e 20 69 52 65 67 69 6f 6e 20 6f 66 20 74  ion iRegion of t
1104e 68 65 20 0d 0a 2a 2a 20 73 68 61 72 65 64 2d 6d  he ..** shared-m
1104f 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
11050 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
11051 73 65 20 66 69 6c 65 20 66 64 2e 20 53 68 61 72  se file fd. Shar
11052 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e  ed-memory region
11053 73 20 0d 0a 2a 2a 20 61 72 65 20 6e 75 6d 62 65  s ..** are numbe
11054 72 65 64 20 73 74 61 72 74 69 6e 67 20 66 72 6f  red starting fro
11055 6d 20 7a 65 72 6f 2e 20 45 61 63 68 20 73 68 61  m zero. Each sha
11056 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f  red-memory regio
11057 6e 20 69 73 20 73 7a 52 65 67 69 6f 6e 20 0d 0a  n is szRegion ..
11058 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
11059 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20  ...**..** If an 
1105a 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
1105b 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
1105c 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20  eturned and *pp 
1105d 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0d  is set to NULL..
1105e 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  .**..** Otherwis
1105f 65 2c 20 69 66 20 74 68 65 20 62 45 78 74 65 6e  e, if the bExten
11060 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  d parameter is 0
11061 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
11062 65 64 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  ed shared-memory
11063 0d 0a 2a 2a 20 72 65 67 69 6f 6e 20 68 61 73 20  ..** region has 
11064 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  not been allocat
11065 65 64 20 28 62 79 20 61 6e 79 20 63 6c 69 65 6e  ed (by any clien
11066 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f 6e 65  t, including one
11067 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 0d 0a 2a   running in a..*
11068 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65  * separate proce
11069 73 73 29 2c 20 74 68 65 6e 20 2a 70 70 20 69 73  ss), then *pp is
1106a 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64   set to NULL and
1106b 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
1106c 6e 65 64 2e 20 49 66 20 0d 0a 2a 2a 20 62 45 78  ned. If ..** bEx
1106d 74 65 6e 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  tend is non-zero
1106e 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
1106f 65 64 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  ed shared-memory
11070 20 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20   region has not 
11071 79 65 74 20 0d 0a 2a 2a 20 62 65 65 6e 20 61 6c  yet ..** been al
11072 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 61  located, it is a
11073 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
11074 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a   function...**..
11075 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 64  ** If the shared
11076 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 68  -memory region h
11077 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
11078 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 69 73 20  allocated or is 
11079 61 6c 6c 6f 63 61 74 65 64 20 62 79 0d 0a 2a 2a  allocated by..**
1107a 20 74 68 69 73 20 63 61 6c 6c 20 61 73 20 64 65   this call as de
1107b 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 74  scribed above, t
1107c 68 65 6e 20 69 74 20 69 73 20 6d 61 70 70 65 64  hen it is mapped
1107d 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f 63 65   into this proce
1107e 73 73 65 73 20 0d 0a 2a 2a 20 61 64 64 72 65 73  sses ..** addres
1107f 73 20 73 70 61 63 65 20 28 69 66 20 69 74 20 69  s space (if it i
11080 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 29 2c 20  s not already), 
11081 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 70 6f  *pp is set to po
11082 69 6e 74 20 74 6f 20 74 68 65 20 6d 61 70 70 65  int to the mappe
11083 64 20 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e  d ..** memory an
11084 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
11085 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rned...*/..stati
11086 63 20 69 6e 74 20 75 6e 69 78 53 68 6d 4d 61 70  c int unixShmMap
11087 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  (..  sqlite3_fil
11088 65 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20  e *fd,          
11089 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f       /* Handle o
1108a 70 65 6e 20 6f 6e 20 64 61 74 61 62 61 73 65 20  pen on database 
1108b 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  file */..  int i
1108c 52 65 67 69 6f 6e 2c 20 20 20 20 20 20 20 20 20  Region,         
1108d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1108e 67 69 6f 6e 20 74 6f 20 72 65 74 72 69 65 76 65  gion to retrieve
1108f 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 52 65 67   */..  int szReg
11090 69 6f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ion,            
11091 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11092 66 20 72 65 67 69 6f 6e 73 20 2a 2f 0d 0a 20 20  f regions */..  
11093 69 6e 74 20 62 45 78 74 65 6e 64 2c 20 20 20 20  int bExtend,    
11094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11095 2f 2a 20 54 72 75 65 20 74 6f 20 65 78 74 65 6e  /* True to exten
11096 64 20 66 69 6c 65 20 69 66 20 6e 65 63 65 73 73  d file if necess
11097 61 72 79 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 76  ary */..  void v
11098 6f 6c 61 74 69 6c 65 20 2a 2a 70 70 20 20 20 20  olatile **pp    
11099 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
1109a 3a 20 4d 61 70 70 65 64 20 6d 65 6d 6f 72 79 20  : Mapped memory 
1109b 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 6e 69 78 46 69  */..){..  unixFi
1109c 6c 65 20 2a 70 44 62 46 64 20 3d 20 28 75 6e 69  le *pDbFd = (uni
1109d 78 46 69 6c 65 2a 29 66 64 3b 0d 0a 20 20 75 6e  xFile*)fd;..  un
1109e 69 78 53 68 6d 20 2a 70 3b 0d 0a 20 20 75 6e 69  ixShm *p;..  uni
1109f 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f  xShmNode *pShmNo
110a0 64 65 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  de;..  int rc = 
110a1 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20  SQLITE_OK;....  
110a2 2f 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 64  /* If the shared
110a3 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 68 61 73  -memory file has
110a4 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70   not yet been op
110a5 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f  ened, open it no
110a6 77 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 70 44 62  w. */..  if( pDb
110a7 46 64 2d 3e 70 53 68 6d 3d 3d 30 20 29 7b 0d 0a  Fd->pShm==0 ){..
110a8 20 20 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65      rc = unixOpe
110a9 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 70 44  nSharedMemory(pD
110aa 62 46 64 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  bFd);..    if( r
110ab 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
110ac 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a  eturn rc;..  }..
110ad 0d 0a 20 20 70 20 3d 20 70 44 62 46 64 2d 3e 70  ..  p = pDbFd->p
110ae 53 68 6d 3b 0d 0a 20 20 70 53 68 6d 4e 6f 64 65  Shm;..  pShmNode
110af 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0d   = p->pShmNode;.
110b0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
110b1 5f 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d  _enter(pShmNode-
110b2 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 61 73 73 65  >mutex);..  asse
110b3 72 74 28 20 73 7a 52 65 67 69 6f 6e 3d 3d 70 53  rt( szRegion==pS
110b4 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e  hmNode->szRegion
110b5 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52   || pShmNode->nR
110b6 65 67 69 6f 6e 3d 3d 30 20 29 3b 0d 0a 20 20 61  egion==0 );..  a
110b7 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d  ssert( pShmNode-
110b8 3e 70 49 6e 6f 64 65 3d 3d 70 44 62 46 64 2d 3e  >pInode==pDbFd->
110b9 70 49 6e 6f 64 65 20 29 3b 0d 0a 20 20 61 73 73  pInode );..  ass
110ba 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68  ert( pShmNode->h
110bb 3e 3d 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49  >=0 || pDbFd->pI
110bc 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f  node->bProcessLo
110bd 63 6b 3d 3d 31 20 29 3b 0d 0a 20 20 61 73 73 65  ck==1 );..  asse
110be 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c  rt( pShmNode->h<
110bf 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e 6f  0 || pDbFd->pIno
110c0 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b  de->bProcessLock
110c1 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20  ==0 );....  if( 
110c2 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f  pShmNode->nRegio
110c3 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b 0d 0a 20  n<=iRegion ){.. 
110c4 20 20 20 63 68 61 72 20 2a 2a 61 70 4e 65 77 3b     char **apNew;
110c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110c6 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 61 70 52        /* New apR
110c7 65 67 69 6f 6e 5b 5d 20 61 72 72 61 79 20 2a 2f  egion[] array */
110c8 0d 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20  ..    int nByte 
110c9 3d 20 28 69 52 65 67 69 6f 6e 2b 31 29 2a 73 7a  = (iRegion+1)*sz
110ca 52 65 67 69 6f 6e 3b 20 20 2f 2a 20 4d 69 6e 69  Region;  /* Mini
110cb 6d 75 6d 20 72 65 71 75 69 72 65 64 20 66 69 6c  mum required fil
110cc 65 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 20 20 73  e size */..    s
110cd 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74  truct stat sStat
110ce 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
110cf 20 20 2f 2a 20 55 73 65 64 20 62 79 20 66 73 74    /* Used by fst
110d0 61 74 28 29 20 2a 2f 0d 0a 0d 0a 20 20 20 20 70  at() */....    p
110d1 53 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f  ShmNode->szRegio
110d2 6e 20 3d 20 73 7a 52 65 67 69 6f 6e 3b 0d 0a 0d  n = szRegion;...
110d3 0a 20 20 20 20 69 66 28 20 70 53 68 6d 4e 6f 64  .    if( pShmNod
110d4 65 2d 3e 68 3e 3d 30 20 29 7b 0d 0a 20 20 20 20  e->h>=0 ){..    
110d5 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
110d6 65 64 20 72 65 67 69 6f 6e 20 69 73 20 6e 6f 74  ed region is not
110d7 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69   mapped into thi
110d8 73 20 70 72 6f 63 65 73 73 65 73 20 61 64 64 72  s processes addr
110d9 65 73 73 20 73 70 61 63 65 2e 0d 0a 20 20 20 20  ess space...    
110da 20 20 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    ** Check to se
110db 65 20 69 66 20 69 74 20 68 61 73 20 62 65 65 6e  e if it has been
110dc 20 61 6c 6c 6f 63 61 74 65 64 20 28 69 2e 65 2e   allocated (i.e.
110dd 20 69 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65   if the wal-inde
110de 78 20 66 69 6c 65 20 69 73 0d 0a 20 20 20 20 20  x file is..     
110df 20 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   ** large enough
110e0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
110e1 72 65 71 75 65 73 74 65 64 20 72 65 67 69 6f 6e  requested region
110e2 29 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  )...      */..  
110e3 20 20 20 20 69 66 28 20 6f 73 46 73 74 61 74 28      if( osFstat(
110e4 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 26 73 53  pShmNode->h, &sS
110e5 74 61 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  tat) ){..       
110e6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
110e7 52 52 5f 53 48 4d 53 49 5a 45 3b 0d 0a 20 20 20  RR_SHMSIZE;..   
110e8 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67       goto shmpag
110e9 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20 7d 0d  e_out;..      }.
110ea 0a 20 20 0d 0a 20 20 20 20 20 20 69 66 28 20 73  .  ..      if( s
110eb 53 74 61 74 2e 73 74 5f 73 69 7a 65 3c 6e 42 79  Stat.st_size<nBy
110ec 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f  te ){..        /
110ed 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20  * The requested 
110ee 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 64 6f  memory region do
110ef 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 49 66  es not exist. If
110f0 20 62 45 78 74 65 6e 64 20 69 73 20 73 65 74 20   bExtend is set 
110f1 74 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 66  to..        ** f
110f2 61 6c 73 65 2c 20 65 78 69 74 20 65 61 72 6c 79  alse, exit early
110f3 2e 20 2a 70 70 20 77 69 6c 6c 20 62 65 20 73 65  . *pp will be se
110f4 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51  t to NULL and SQ
110f5 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
110f6 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20  ...        **.. 
110f7 20 20 20 20 20 20 20 2a 2a 20 41 6c 74 65 72 6e         ** Altern
110f8 61 74 69 76 65 6c 79 2c 20 69 66 20 62 45 78 74  atively, if bExt
110f9 65 6e 64 20 69 73 20 74 72 75 65 2c 20 75 73 65  end is true, use
110fa 20 66 74 72 75 6e 63 61 74 65 28 29 20 74 6f 20   ftruncate() to 
110fb 61 6c 6c 6f 63 61 74 65 0d 0a 20 20 20 20 20 20  allocate..      
110fc 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74    ** the request
110fd 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e  ed memory region
110fe 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
110ff 20 20 20 20 20 20 20 69 66 28 20 21 62 45 78 74         if( !bExt
11100 65 6e 64 20 29 20 67 6f 74 6f 20 73 68 6d 70 61  end ) goto shmpa
11101 67 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20 20  ge_out;..       
11102 20 69 66 28 20 72 6f 62 75 73 74 5f 66 74 72 75   if( robust_ftru
11103 6e 63 61 74 65 28 70 53 68 6d 4e 6f 64 65 2d 3e  ncate(pShmNode->
11104 68 2c 20 6e 42 79 74 65 29 20 29 7b 0d 0a 20 20  h, nByte) ){..  
11105 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69          rc = uni
11106 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  xLogError(SQLITE
11107 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 2c 20  _IOERR_SHMSIZE, 
11108 22 66 74 72 75 6e 63 61 74 65 22 2c 0d 0a 20 20  "ftruncate",..  
11109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1110a 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f            pShmNo
1110b 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d  de->zFilename);.
1110c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1110d 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0d 0a 20 20  shmpage_out;..  
1110e 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
1110f 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
11110 2a 20 4d 61 70 20 74 68 65 20 72 65 71 75 65 73  * Map the reques
11111 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 6f  ted memory regio
11112 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f 63  n into this proc
11113 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73 70  esses address sp
11114 61 63 65 2e 20 2a 2f 0d 0a 20 20 20 20 61 70 4e  ace. */..    apN
11115 65 77 20 3d 20 28 63 68 61 72 20 2a 2a 29 73 71  ew = (char **)sq
11116 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0d 0a  lite3_realloc(..
11117 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65          pShmNode
11118 2d 3e 61 70 52 65 67 69 6f 6e 2c 20 28 69 52 65  ->apRegion, (iRe
11119 67 69 6f 6e 2b 31 29 2a 73 69 7a 65 6f 66 28 63  gion+1)*sizeof(c
1111a 68 61 72 20 2a 29 0d 0a 20 20 20 20 29 3b 0d 0a  har *)..    );..
1111b 20 20 20 20 69 66 28 20 21 61 70 4e 65 77 20 29      if( !apNew )
1111c 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  {..      rc = SQ
1111d 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
1111e 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 68  ;..      goto sh
1111f 6d 70 61 67 65 5f 6f 75 74 3b 0d 0a 20 20 20 20  mpage_out;..    
11120 7d 0d 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d  }..    pShmNode-
11121 3e 61 70 52 65 67 69 6f 6e 20 3d 20 61 70 4e 65  >apRegion = apNe
11122 77 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 70 53  w;..    while(pS
11123 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 3c  hmNode->nRegion<
11124 3d 69 52 65 67 69 6f 6e 29 7b 0d 0a 20 20 20 20  =iRegion){..    
11125 20 20 76 6f 69 64 20 2a 70 4d 65 6d 3b 0d 0a 20    void *pMem;.. 
11126 20 20 20 20 20 69 66 28 20 70 53 68 6d 4e 6f 64       if( pShmNod
11127 65 2d 3e 68 3e 3d 30 20 29 7b 0d 0a 20 20 20 20  e->h>=0 ){..    
11128 20 20 20 20 70 4d 65 6d 20 3d 20 6d 6d 61 70 28      pMem = mmap(
11129 30 2c 20 73 7a 52 65 67 69 6f 6e 2c 0d 0a 20 20  0, szRegion,..  
1112a 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f            pShmNo
1112b 64 65 2d 3e 69 73 52 65 61 64 6f 6e 6c 79 20 3f  de->isReadonly ?
1112c 20 50 52 4f 54 5f 52 45 41 44 20 3a 20 50 52 4f   PROT_READ : PRO
1112d 54 5f 52 45 41 44 7c 50 52 4f 54 5f 57 52 49 54  T_READ|PROT_WRIT
1112e 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  E, ..           
1112f 20 4d 41 50 5f 53 48 41 52 45 44 2c 20 70 53 68   MAP_SHARED, pSh
11130 6d 4e 6f 64 65 2d 3e 68 2c 20 70 53 68 6d 4e 6f  mNode->h, pShmNo
11131 64 65 2d 3e 6e 52 65 67 69 6f 6e 2a 73 7a 52 65  de->nRegion*szRe
11132 67 69 6f 6e 0d 0a 20 20 20 20 20 20 20 20 29 3b  gion..        );
11133 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4d  ..        if( pM
11134 65 6d 3d 3d 4d 41 50 5f 46 41 49 4c 45 44 20 29  em==MAP_FAILED )
11135 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  {..          rc 
11136 3d 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53  = unixLogError(S
11137 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 4d  QLITE_IOERR_SHMM
11138 41 50 2c 20 22 6d 6d 61 70 22 2c 20 70 53 68 6d  AP, "mmap", pShm
11139 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29  Node->zFilename)
1113a 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ;..          got
1113b 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0d 0a  o shmpage_out;..
1113c 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
1113d 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
1113e 20 70 4d 65 6d 20 3d 20 73 71 6c 69 74 65 33 5f   pMem = sqlite3_
1113f 6d 61 6c 6c 6f 63 28 73 7a 52 65 67 69 6f 6e 29  malloc(szRegion)
11140 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ;..        if( p
11141 4d 65 6d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Mem==0 ){..     
11142 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
11143 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 20  _NOMEM;..       
11144 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f     goto shmpage_
11145 6f 75 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  out;..        }.
11146 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
11147 70 4d 65 6d 2c 20 30 2c 20 73 7a 52 65 67 69 6f  pMem, 0, szRegio
11148 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  n);..      }..  
11149 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 61 70      pShmNode->ap
1114a 52 65 67 69 6f 6e 5b 70 53 68 6d 4e 6f 64 65 2d  Region[pShmNode-
1114b 3e 6e 52 65 67 69 6f 6e 5d 20 3d 20 70 4d 65 6d  >nRegion] = pMem
1114c 3b 0d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64  ;..      pShmNod
1114d 65 2d 3e 6e 52 65 67 69 6f 6e 2b 2b 3b 0d 0a 20  e->nRegion++;.. 
1114e 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 73 68 6d     }..  }....shm
1114f 70 61 67 65 5f 6f 75 74 3a 0d 0a 20 20 69 66 28  page_out:..  if(
11150 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69   pShmNode->nRegi
11151 6f 6e 3e 69 52 65 67 69 6f 6e 20 29 7b 0d 0a 20  on>iRegion ){.. 
11152 20 20 20 2a 70 70 20 3d 20 70 53 68 6d 4e 6f 64     *pp = pShmNod
11153 65 2d 3e 61 70 52 65 67 69 6f 6e 5b 69 52 65 67  e->apRegion[iReg
11154 69 6f 6e 5d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  ion];..  }else{.
11155 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b 0d 0a 20  .    *pp = 0;.. 
11156 20 7d 0d 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f   }..  if( pShmNo
11157 64 65 2d 3e 69 73 52 65 61 64 6f 6e 6c 79 20 26  de->isReadonly &
11158 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
11159 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45  ) rc = SQLITE_RE
1115a 41 44 4f 4e 4c 59 3b 0d 0a 20 20 73 71 6c 69 74  ADONLY;..  sqlit
1115b 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1115c 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  ShmNode->mutex);
1115d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
1115e 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e  }..../*..** Chan
1115f 67 65 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ge the lock stat
11160 65 20 66 6f 72 20 61 20 73 68 61 72 65 64 2d 6d  e for a shared-m
11161 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 2e 0d 0a  emory segment...
11162 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  **..** Note that
11163 20 74 68 65 20 72 65 6c 61 74 69 6f 6e 73 68 69   the relationshi
11164 70 20 62 65 74 77 65 65 6e 20 53 48 41 52 45 64  p between SHAREd
11165 20 61 6e 64 20 45 58 43 4c 55 53 49 56 45 20 6c   and EXCLUSIVE l
11166 6f 63 6b 73 20 69 73 20 61 20 6c 69 74 74 6c 65  ocks is a little
11167 0d 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 68  ..** different h
11168 65 72 65 20 74 68 61 6e 20 69 6e 20 70 6f 73 69  ere than in posi
11169 78 2e 20 20 49 6e 20 78 53 68 6d 4c 6f 63 6b 28  x.  In xShmLock(
1116a 29 2c 20 6f 6e 65 20 63 61 6e 20 67 6f 20 66 72  ), one can go fr
1116b 6f 6d 20 75 6e 6c 6f 63 6b 65 64 0d 0a 2a 2a 20  om unlocked..** 
1116c 74 6f 20 73 68 61 72 65 64 20 61 6e 64 20 62 61  to shared and ba
1116d 63 6b 20 6f 72 20 66 72 6f 6d 20 75 6e 6c 6f 63  ck or from unloc
1116e 6b 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65  ked to exclusive
1116f 20 61 6e 64 20 62 61 63 6b 2e 20 20 42 75 74 20   and back.  But 
11170 6f 6e 65 20 6d 61 79 0d 0a 2a 2a 20 6e 6f 74 20  one may..** not 
11171 67 6f 20 66 72 6f 6d 20 73 68 61 72 65 64 20 74  go from shared t
11172 6f 20 65 78 63 6c 75 73 69 76 65 20 6f 72 20 66  o exclusive or f
11173 72 6f 6d 20 65 78 63 6c 75 73 69 76 65 20 74 6f  rom exclusive to
11174 20 73 68 61 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74   shared...*/..st
11175 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 68 6d  atic int unixShm
11176 4c 6f 63 6b 28 0d 0a 20 20 73 71 6c 69 74 65 33  Lock(..  sqlite3
11177 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 20 20 20  _file *fd,      
11178 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
11179 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65  file holding the
1117a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a   shared memory *
1117b 2f 0d 0a 20 20 69 6e 74 20 6f 66 73 74 2c 20 20  /..  int ofst,  
1117c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1117d 2f 2a 20 46 69 72 73 74 20 6c 6f 63 6b 20 74 6f  /* First lock to
1117e 20 61 63 71 75 69 72 65 20 6f 72 20 72 65 6c 65   acquire or rele
1117f 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 2c  ase */..  int n,
11180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11181 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
11182 66 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71 75 69  f locks to acqui
11183 72 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f  re or release */
11184 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20  ..  int flags   
11185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11186 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
11187 68 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0d 0a 29  h the lock */..)
11188 7b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  {..  unixFile *p
11189 44 62 46 64 20 3d 20 28 75 6e 69 78 46 69 6c 65  DbFd = (unixFile
1118a 2a 29 66 64 3b 20 20 20 20 20 20 2f 2a 20 43 6f  *)fd;      /* Co
1118b 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67  nnection holding
1118c 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a   shared memory *
1118d 2f 0d 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 20  /..  unixShm *p 
1118e 3d 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 20 20  = pDbFd->pShm;  
1118f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
11190 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  e shared memory 
11191 62 65 69 6e 67 20 6c 6f 63 6b 65 64 20 2a 2f 0d  being locked */.
11192 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 58 3b 20  .  unixShm *pX; 
11193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11194 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
11195 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c  looping over all
11196 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0d 0a 20 20   siblings */..  
11197 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68  unixShmNode *pSh
11198 6d 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e  mNode = p->pShmN
11199 6f 64 65 3b 20 20 2f 2a 20 54 68 65 20 75 6e 64  ode;  /* The und
1119a 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 4e 6f  erlying file iNo
1119b 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20  de */..  int rc 
1119c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
1119d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1119e 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
1119f 0d 0a 20 20 75 31 36 20 6d 61 73 6b 3b 20 20 20  ..  u16 mask;   
111a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
111a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
111a2 6b 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 74 61  k of locks to ta
111a3 6b 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f  ke or release */
111a4 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 53  ....  assert( pS
111a5 68 6d 4e 6f 64 65 3d 3d 70 44 62 46 64 2d 3e 70  hmNode==pDbFd->p
111a6 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 20  Inode->pShmNode 
111a7 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 53  );..  assert( pS
111a8 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d 3d  hmNode->pInode==
111a9 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 20 29 3b  pDbFd->pInode );
111aa 0d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 73 74  ..  assert( ofst
111ab 3e 3d 30 20 26 26 20 6f 66 73 74 2b 6e 3c 3d 53  >=0 && ofst+n<=S
111ac 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 20  QLITE_SHM_NLOCK 
111ad 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  );..  assert( n>
111ae 3d 31 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =1 );..  assert(
111af 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f   flags==(SQLITE_
111b0 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54  SHM_LOCK | SQLIT
111b1 45 5f 53 48 4d 5f 53 48 41 52 45 44 29 0d 0a 20  E_SHM_SHARED).. 
111b2 20 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d        || flags==
111b3 28 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b  (SQLITE_SHM_LOCK
111b4 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58   | SQLITE_SHM_EX
111b5 43 4c 55 53 49 56 45 29 0d 0a 20 20 20 20 20 20  CLUSIVE)..      
111b6 20 7c 7c 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49   || flags==(SQLI
111b7 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20  TE_SHM_UNLOCK | 
111b8 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45  SQLITE_SHM_SHARE
111b9 44 29 0d 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c  D)..       || fl
111ba 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d  ags==(SQLITE_SHM
111bb 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45  _UNLOCK | SQLITE
111bc 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 20  _SHM_EXCLUSIVE) 
111bd 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 3d  );..  assert( n=
111be 3d 31 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53  =1 || (flags & S
111bf 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53  QLITE_SHM_EXCLUS
111c0 49 56 45 29 21 3d 30 20 29 3b 0d 0a 20 20 61 73  IVE)!=0 );..  as
111c1 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e  sert( pShmNode->
111c2 68 3e 3d 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70  h>=0 || pDbFd->p
111c3 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c  Inode->bProcessL
111c4 6f 63 6b 3d 3d 31 20 29 3b 0d 0a 20 20 61 73 73  ock==1 );..  ass
111c5 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68  ert( pShmNode->h
111c6 3c 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e  <0 || pDbFd->pIn
111c7 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63  ode->bProcessLoc
111c8 6b 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 6d 61 73  k==0 );....  mas
111c9 6b 20 3d 20 28 31 3c 3c 28 6f 66 73 74 2b 6e 29  k = (1<<(ofst+n)
111ca 29 20 2d 20 28 31 3c 3c 6f 66 73 74 29 3b 0d 0a  ) - (1<<ofst);..
111cb 20 20 61 73 73 65 72 74 28 20 6e 3e 31 20 7c 7c    assert( n>1 ||
111cc 20 6d 61 73 6b 3d 3d 28 31 3c 3c 6f 66 73 74 29   mask==(1<<ofst)
111cd 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d   );..  sqlite3_m
111ce 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d 4e  utex_enter(pShmN
111cf 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  ode->mutex);..  
111d0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
111d1 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 29 7b  TE_SHM_UNLOCK ){
111d2 0d 0a 20 20 20 20 75 31 36 20 61 6c 6c 4d 61 73  ..    u16 allMas
111d3 6b 20 3d 20 30 3b 20 2f 2a 20 4d 61 73 6b 20 6f  k = 0; /* Mask o
111d4 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20  f locks held by 
111d5 73 69 62 6c 69 6e 67 73 20 2a 2f 0d 0a 0d 0a 20  siblings */.... 
111d6 20 20 20 2f 2a 20 53 65 65 20 69 66 20 61 6e 79     /* See if any
111d7 20 73 69 62 6c 69 6e 67 73 20 68 6f 6c 64 20 74   siblings hold t
111d8 68 69 73 20 73 61 6d 65 20 6c 6f 63 6b 20 2a 2f  his same lock */
111d9 0d 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 53 68  ..    for(pX=pSh
111da 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20 70  mNode->pFirst; p
111db 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 74 29  X; pX=pX->pNext)
111dc 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 58 3d  {..      if( pX=
111dd 3d 70 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  =p ) continue;..
111de 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
111df 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 28 70  X->exclMask & (p
111e0 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 2d 3e 73 68  ->exclMask|p->sh
111e1 61 72 65 64 4d 61 73 6b 29 29 3d 3d 30 20 29 3b  aredMask))==0 );
111e2 0d 0a 20 20 20 20 20 20 61 6c 6c 4d 61 73 6b 20  ..      allMask 
111e3 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73  |= pX->sharedMas
111e4 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  k;..    }....   
111e5 20 2f 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 73   /* Unlock the s
111e6 79 73 74 65 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b  ystem-level lock
111e7 73 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 28 6d  s */..    if( (m
111e8 61 73 6b 20 26 20 61 6c 6c 4d 61 73 6b 29 3d 3d  ask & allMask)==
111e9 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  0 ){..      rc =
111ea 20 75 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f   unixShmSystemLo
111eb 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 46 5f 55  ck(pShmNode, F_U
111ec 4e 4c 43 4b 2c 20 6f 66 73 74 2b 55 4e 49 58 5f  NLCK, ofst+UNIX_
111ed 53 48 4d 5f 42 41 53 45 2c 20 6e 29 3b 0d 0a 20  SHM_BASE, n);.. 
111ee 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
111ef 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
111f0 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
111f1 2a 20 55 6e 64 6f 20 74 68 65 20 6c 6f 63 61 6c  * Undo the local
111f2 20 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 20 20 69   locks */..    i
111f3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
111f4 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 65 78   ){..      p->ex
111f5 63 6c 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b  clMask &= ~mask;
111f6 0d 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 65  ..      p->share
111f7 64 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0d  dMask &= ~mask;.
111f8 0a 20 20 20 20 7d 20 0d 0a 20 20 7d 65 6c 73 65  .    } ..  }else
111f9 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
111fa 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20 29  ITE_SHM_SHARED )
111fb 7b 0d 0a 20 20 20 20 75 31 36 20 61 6c 6c 53 68  {..    u16 allSh
111fc 61 72 65 64 20 3d 20 30 3b 20 20 2f 2a 20 55 6e  ared = 0;  /* Un
111fd 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c  ion of locks hel
111fe 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  d by connections
111ff 20 6f 74 68 65 72 20 74 68 61 6e 20 22 70 22 20   other than "p" 
11200 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 6e  */....    /* Fin
11201 64 20 6f 75 74 20 77 68 69 63 68 20 73 68 61 72  d out which shar
11202 65 64 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 72  ed locks are alr
11203 65 61 64 79 20 68 65 6c 64 20 62 79 20 73 69 62  eady held by sib
11204 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ling connections
11205 2e 0d 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 79  ...    ** If any
11206 20 73 69 62 6c 69 6e 67 20 61 6c 72 65 61 64 79   sibling already
11207 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 73   holds an exclus
11208 69 76 65 20 6c 6f 63 6b 2c 20 67 6f 20 61 68 65  ive lock, go ahe
11209 61 64 20 61 6e 64 20 72 65 74 75 72 6e 0d 0a 20  ad and return.. 
1120a 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53     ** SQLITE_BUS
1120b 59 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  Y...    */..    
1120c 66 6f 72 28 70 58 3d 70 53 68 6d 4e 6f 64 65 2d  for(pX=pShmNode-
1120d 3e 70 46 69 72 73 74 3b 20 70 58 3b 20 70 58 3d  >pFirst; pX; pX=
1120e 70 58 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20  pX->pNext){..   
1120f 20 20 20 69 66 28 20 28 70 58 2d 3e 65 78 63 6c     if( (pX->excl
11210 4d 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20  Mask & mask)!=0 
11211 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
11212 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
11213 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
11214 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 6c       }..      al
11215 6c 53 68 61 72 65 64 20 7c 3d 20 70 58 2d 3e 73  lShared |= pX->s
11216 68 61 72 65 64 4d 61 73 6b 3b 0d 0a 20 20 20 20  haredMask;..    
11217 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47 65 74 20  }....    /* Get 
11218 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 61 74 20  shared locks at 
11219 74 68 65 20 73 79 73 74 65 6d 20 6c 65 76 65 6c  the system level
1121a 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a  , if necessary *
1121b 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  /..    if( rc==S
1121c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1121d 20 20 20 69 66 28 20 28 61 6c 6c 53 68 61 72 65     if( (allShare
1121e 64 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0d  d & mask)==0 ){.
1121f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e  .        rc = un
11220 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28  ixShmSystemLock(
11221 70 53 68 6d 4e 6f 64 65 2c 20 46 5f 52 44 4c 43  pShmNode, F_RDLC
11222 4b 2c 20 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d  K, ofst+UNIX_SHM
11223 5f 42 41 53 45 2c 20 6e 29 3b 0d 0a 20 20 20 20  _BASE, n);..    
11224 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
11225 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
11226 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
11227 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47 65 74 20  }....    /* Get 
11228 74 68 65 20 6c 6f 63 61 6c 20 73 68 61 72 65 64  the local shared
11229 20 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 20 20 69   locks */..    i
1122a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1122b 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 73 68   ){..      p->sh
1122c 61 72 65 64 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b  aredMask |= mask
1122d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
1122e 65 7b 0d 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20  e{..    /* Make 
1122f 73 75 72 65 20 6e 6f 20 73 69 62 6c 69 6e 67 20  sure no sibling 
11230 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 6f 6c 64  connections hold
11231 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 69 6c 6c   locks that will
11232 20 62 6c 6f 63 6b 20 74 68 69 73 0d 0a 20 20 20   block this..   
11233 20 2a 2a 20 6c 6f 63 6b 2e 20 20 49 66 20 61 6e   ** lock.  If an
11234 79 20 64 6f 2c 20 72 65 74 75 72 6e 20 53 51 4c  y do, return SQL
11235 49 54 45 5f 42 55 53 59 20 72 69 67 68 74 20 61  ITE_BUSY right a
11236 77 61 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  way...    */..  
11237 20 20 66 6f 72 28 70 58 3d 70 53 68 6d 4e 6f 64    for(pX=pShmNod
11238 65 2d 3e 70 46 69 72 73 74 3b 20 70 58 3b 20 70  e->pFirst; pX; p
11239 58 3d 70 58 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20  X=pX->pNext){.. 
1123a 20 20 20 20 20 69 66 28 20 28 70 58 2d 3e 65 78       if( (pX->ex
1123b 63 6c 4d 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d  clMask & mask)!=
1123c 30 20 7c 7c 20 28 70 58 2d 3e 73 68 61 72 65 64  0 || (pX->shared
1123d 4d 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20  Mask & mask)!=0 
1123e 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
1123f 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
11240 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
11241 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
11242 20 0d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68   ..    /* Get th
11243 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
11244 73 20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20  s at the system 
11245 6c 65 76 65 6c 2e 20 20 54 68 65 6e 20 69 66 20  level.  Then if 
11246 73 75 63 63 65 73 73 66 75 6c 0d 0a 20 20 20 20  successful..    
11247 2a 2a 20 61 6c 73 6f 20 6d 61 72 6b 20 74 68 65  ** also mark the
11248 20 6c 6f 63 61 6c 20 63 6f 6e 6e 65 63 74 69 6f   local connectio
11249 6e 20 61 73 20 62 65 69 6e 67 20 6c 6f 63 6b 65  n as being locke
1124a 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  d...    */..    
1124b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1124c 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  K ){..      rc =
1124d 20 75 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f   unixShmSystemLo
1124e 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 46 5f 57  ck(pShmNode, F_W
1124f 52 4c 43 4b 2c 20 6f 66 73 74 2b 55 4e 49 58 5f  RLCK, ofst+UNIX_
11250 53 48 4d 5f 42 41 53 45 2c 20 6e 29 3b 0d 0a 20  SHM_BASE, n);.. 
11251 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
11252 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
11253 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
11254 68 61 72 65 64 4d 61 73 6b 20 26 20 6d 61 73 6b  haredMask & mask
11255 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20  )==0 );..       
11256 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 7c 3d 20   p->exclMask |= 
11257 6d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  mask;..      }..
11258 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71      }..  }..  sq
11259 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1125a 65 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65  e(pShmNode->mute
1125b 78 29 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28  x);..  OSTRACE((
1125c 22 53 48 4d 2d 4c 4f 43 4b 20 73 68 6d 69 64 2d  "SHM-LOCK shmid-
1125d 25 64 2c 20 70 69 64 2d 25 64 20 67 6f 74 20 25  %d, pid-%d got %
1125e 30 33 78 2c 25 30 33 78 5c 6e 22 2c 0d 0a 20 20  03x,%03x\n",..  
1125f 20 20 20 20 20 20 20 20 20 70 2d 3e 69 64 2c 20           p->id, 
11260 67 65 74 70 69 64 28 29 2c 20 70 2d 3e 73 68 61  getpid(), p->sha
11261 72 65 64 4d 61 73 6b 2c 20 70 2d 3e 65 78 63 6c  redMask, p->excl
11262 4d 61 73 6b 29 29 3b 0d 0a 20 20 72 65 74 75 72  Mask));..  retur
11263 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
11264 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 61 20 6d  ** Implement a m
11265 65 6d 6f 72 79 20 62 61 72 72 69 65 72 20 6f 72  emory barrier or
11266 20 6d 65 6d 6f 72 79 20 66 65 6e 63 65 20 6f 6e   memory fence on
11267 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 2e 20   shared memory. 
11268 20 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 6c 6f   ..**..** All lo
11269 61 64 73 20 61 6e 64 20 73 74 6f 72 65 73 20 62  ads and stores b
1126a 65 67 75 6e 20 62 65 66 6f 72 65 20 74 68 65 20  egun before the 
1126b 62 61 72 72 69 65 72 20 6d 75 73 74 20 63 6f 6d  barrier must com
1126c 70 6c 65 74 65 20 62 65 66 6f 72 65 0d 0a 2a 2a  plete before..**
1126d 20 61 6e 79 20 6c 6f 61 64 20 6f 72 20 73 74 6f   any load or sto
1126e 72 65 20 62 65 67 75 6e 20 61 66 74 65 72 20 74  re begun after t
1126f 68 65 20 62 61 72 72 69 65 72 2e 0d 0a 2a 2f 0d  he barrier...*/.
11270 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69  .static void uni
11271 78 53 68 6d 42 61 72 72 69 65 72 28 0d 0a 20 20  xShmBarrier(..  
11272 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
11273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11274 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
11275 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 68 61   holding the sha
11276 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 29  red memory */..)
11277 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  {..  UNUSED_PARA
11278 4d 45 54 45 52 28 66 64 29 3b 0d 0a 20 20 75 6e  METER(fd);..  un
11279 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d  ixEnterMutex();.
1127a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
1127b 78 28 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  x();..}..../*..*
1127c 2a 20 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63  * Close a connec
1127d 74 69 6f 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d  tion to shared-m
1127e 65 6d 6f 72 79 2e 20 20 44 65 6c 65 74 65 20 74  emory.  Delete t
1127f 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 0d 0a  he underlying ..
11280 2a 2a 20 73 74 6f 72 61 67 65 20 69 66 20 64 65  ** storage if de
11281 6c 65 74 65 46 6c 61 67 20 69 73 20 74 72 75 65  leteFlag is true
11282 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
11283 72 65 20 69 73 20 6e 6f 20 73 68 61 72 65 64 20  re is no shared 
11284 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
11285 64 20 77 69 74 68 20 74 68 65 20 63 6f 6e 6e 65  d with the conne
11286 63 74 69 6f 6e 20 74 68 65 6e 20 74 68 69 73 0d  ction then this.
11287 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61  .** routine is a
11288 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e   harmless no-op.
11289 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1128a 20 75 6e 69 78 53 68 6d 55 6e 6d 61 70 28 0d 0a   unixShmUnmap(..
1128b 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1128c 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fd,             
1128d 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79    /* The underly
1128e 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c  ing database fil
1128f 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 64 65 6c 65  e */..  int dele
11290 74 65 46 6c 61 67 20 20 20 20 20 20 20 20 20 20  teFlag          
11291 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74          /* Delet
11292 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  e shared-memory 
11293 69 66 20 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a  if true */..){..
11294 20 20 75 6e 69 78 53 68 6d 20 2a 70 3b 20 20 20    unixShm *p;   
11295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11296 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e 65 63 74    /* The connect
11297 69 6f 6e 20 74 6f 20 62 65 20 63 6c 6f 73 65 64  ion to be closed
11298 20 2a 2f 0d 0a 20 20 75 6e 69 78 53 68 6d 4e 6f   */..  unixShmNo
11299 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 20 20  de *pShmNode;   
1129a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e         /* The un
1129b 64 65 72 6c 79 69 6e 67 20 73 68 61 72 65 64 2d  derlying shared-
1129c 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0d 0a  memory file */..
1129d 20 20 75 6e 69 78 53 68 6d 20 2a 2a 70 70 3b 20    unixShm **pp; 
1129e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1129f 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
112a0 20 6f 76 65 72 20 73 69 62 6c 69 6e 67 20 63 6f   over sibling co
112a1 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20  nnections */..  
112a2 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 3b  unixFile *pDbFd;
112a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112a4 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  /* The underlyin
112a5 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  g database file 
112a6 2a 2f 0d 0a 0d 0a 20 20 70 44 62 46 64 20 3d 20  */....  pDbFd = 
112a7 28 75 6e 69 78 46 69 6c 65 2a 29 66 64 3b 0d 0a  (unixFile*)fd;..
112a8 20 20 70 20 3d 20 70 44 62 46 64 2d 3e 70 53 68    p = pDbFd->pSh
112a9 6d 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  m;..  if( p==0 )
112aa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
112ab 4b 3b 0d 0a 20 20 70 53 68 6d 4e 6f 64 65 20 3d  K;..  pShmNode =
112ac 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0d 0a 0d   p->pShmNode;...
112ad 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e  .  assert( pShmN
112ae 6f 64 65 3d 3d 70 44 62 46 64 2d 3e 70 49 6e 6f  ode==pDbFd->pIno
112af 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 20 29 3b 0d  de->pShmNode );.
112b0 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e  .  assert( pShmN
112b1 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d 3d 70 44 62  ode->pInode==pDb
112b2 46 64 2d 3e 70 49 6e 6f 64 65 20 29 3b 0d 0a 0d  Fd->pInode );...
112b3 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e  .  /* Remove con
112b4 6e 65 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74  nection p from t
112b5 68 65 20 73 65 74 20 6f 66 20 63 6f 6e 6e 65 63  he set of connec
112b6 74 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64  tions associated
112b7 0d 0a 20 20 2a 2a 20 77 69 74 68 20 70 53 68 6d  ..  ** with pShm
112b8 4e 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  Node */..  sqlit
112b9 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
112ba 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  ShmNode->mutex);
112bb 0d 0a 20 20 66 6f 72 28 70 70 3d 26 70 53 68 6d  ..  for(pp=&pShm
112bc 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20 28 2a  Node->pFirst; (*
112bd 70 70 29 21 3d 70 3b 20 70 70 20 3d 20 26 28 2a  pp)!=p; pp = &(*
112be 70 70 29 2d 3e 70 4e 65 78 74 29 7b 7d 0d 0a 20  pp)->pNext){}.. 
112bf 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b   *pp = p->pNext;
112c0 0d 0a 0d 0a 20 20 2f 2a 20 46 72 65 65 20 74 68  ....  /* Free th
112c1 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 2a  e connection p *
112c2 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  /..  sqlite3_fre
112c3 65 28 70 29 3b 0d 0a 20 20 70 44 62 46 64 2d 3e  e(p);..  pDbFd->
112c4 70 53 68 6d 20 3d 20 30 3b 0d 0a 20 20 73 71 6c  pShm = 0;..  sql
112c5 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
112c6 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78  (pShmNode->mutex
112c7 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 70 53  );....  /* If pS
112c8 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 20 68 61 73  hmNode->nRef has
112c9 20 72 65 61 63 68 65 64 20 30 2c 20 74 68 65 6e   reached 0, then
112ca 20 63 6c 6f 73 65 20 74 68 65 20 75 6e 64 65 72   close the under
112cb 6c 79 69 6e 67 0d 0a 20 20 2a 2a 20 73 68 61 72  lying..  ** shar
112cc 65 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2c 20  ed-memory file, 
112cd 74 6f 6f 20 2a 2f 0d 0a 20 20 75 6e 69 78 45 6e  too */..  unixEn
112ce 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 61  terMutex();..  a
112cf 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d  ssert( pShmNode-
112d0 3e 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 70 53  >nRef>0 );..  pS
112d1 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b 0d  hmNode->nRef--;.
112d2 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d  .  if( pShmNode-
112d3 3e 6e 52 65 66 3d 3d 30 20 29 7b 0d 0a 20 20 20  >nRef==0 ){..   
112d4 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67 20   if( deleteFlag 
112d5 26 26 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d  && pShmNode->h>=
112d6 30 20 29 20 6f 73 55 6e 6c 69 6e 6b 28 70 53 68  0 ) osUnlink(pSh
112d7 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65  mNode->zFilename
112d8 29 3b 0d 0a 20 20 20 20 75 6e 69 78 53 68 6d 50  );..    unixShmP
112d9 75 72 67 65 28 70 44 62 46 64 29 3b 0d 0a 20 20  urge(pDbFd);..  
112da 7d 0d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  }..  unixLeaveMu
112db 74 65 78 28 29 3b 0d 0a 0d 0a 20 20 72 65 74 75  tex();....  retu
112dc 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
112dd 0d 0a 0d 0a 0d 0a 23 65 6c 73 65 0d 0a 23 20 64  ......#else..# d
112de 65 66 69 6e 65 20 75 6e 69 78 53 68 6d 4d 61 70  efine unixShmMap
112df 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65       0..# define
112e0 20 75 6e 69 78 53 68 6d 4c 6f 63 6b 20 20 20 20   unixShmLock    
112e1 30 0d 0a 23 20 64 65 66 69 6e 65 20 75 6e 69 78  0..# define unix
112e2 53 68 6d 42 61 72 72 69 65 72 20 30 0d 0a 23 20  ShmBarrier 0..# 
112e3 64 65 66 69 6e 65 20 75 6e 69 78 53 68 6d 55 6e  define unixShmUn
112e4 6d 61 70 20 20 20 30 0d 0a 23 65 6e 64 69 66 20  map   0..#endif 
112e5 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
112e6 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f 0d 0a 0d  E_OMIT_WAL */...
112e7 0a 2f 2a 0d 0a 2a 2a 20 48 65 72 65 20 65 6e 64  ./*..** Here end
112e8 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
112e9 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69  tion of all sqli
112ea 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
112eb 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
112ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
112ed 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d  d sqlite3_file M
112ee 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ethods *********
112ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
112f0 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  ******..********
112f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
112f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
112f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
112f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
112f5 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  ******/..../*..*
112f6 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20  * This division 
112f7 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74  contains definit
112f8 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  ions of sqlite3_
112f9 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
112fa 74 73 20 74 68 61 74 0d 0a 2a 2a 20 69 6d 70 6c  ts that..** impl
112fb 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69  ement various fi
112fc 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  le locking strat
112fd 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20  egies.  It also 
112fe 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74  contains definit
112ff 69 6f 6e 73 0d 0a 2a 2a 20 6f 66 20 22 66 69 6e  ions..** of "fin
11300 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20  der" functions. 
11301 20 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69   A finder-functi
11302 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f  on is used to lo
11303 63 61 74 65 20 74 68 65 20 61 70 70 72 6f 70 72  cate the appropr
11304 69 61 74 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  iate..** sqlite3
11305 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
11306 63 74 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ct for a particu
11307 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c  lar database fil
11308 65 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61  e.  The pAppData
11309 0d 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68  ..** field of th
1130a 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 56 46  e sqlite3_vfs VF
1130b 53 20 6f 62 6a 65 63 74 73 20 61 72 65 20 69 6e  S objects are in
1130c 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 65 20  itialized to be 
1130d 70 6f 69 6e 74 65 72 73 20 74 6f 0d 0a 2a 2a 20  pointers to..** 
1130e 74 68 65 20 63 6f 72 72 65 63 74 20 66 69 6e 64  the correct find
1130f 65 72 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  er-function for 
11310 74 68 61 74 20 56 46 53 2e 0d 0a 2a 2a 0d 0a 2a  that VFS...**..*
11311 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 66 75  * Most finder fu
11312 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61  nctions return a
11313 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 69   pointer to a fi
11314 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  xed sqlite3_io_m
11315 65 74 68 6f 64 73 0d 0a 2a 2a 20 6f 62 6a 65 63  ethods..** objec
11316 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e 74  t.  The only int
11317 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65 72 2d  eresting finder-
11318 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 6f  function is auto
11319 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 68  lockIoFinder, wh
1131a 69 63 68 0d 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74  ich..** looks at
1131b 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20   the filesystem 
1131c 74 79 70 65 20 61 6e 64 20 74 72 69 65 73 20 74  type and tries t
1131d 6f 20 67 75 65 73 73 20 74 68 65 20 62 65 73 74  o guess the best
1131e 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2a 20 73 74 72   locking..** str
1131f 61 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e  ategy from that.
11320 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 66 69 6e  ..**..** For fin
11321 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c 20 74  der-funtion F, t
11322 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63  wo objects are c
11323 72 65 61 74 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  reated:..**..** 
11324 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c 20     (1) The real 
11325 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
11326 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 2e  named "FImpt()".
11327 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 28 32 29 20  ..**..**    (2) 
11328 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74  A constant point
11329 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  er to this funct
1132a 69 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22  ion named just "
1132b 46 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  F"...**..**..** 
1132c 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
1132d 20 46 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73   F pointer is us
1132e 65 64 20 61 73 20 74 68 65 20 70 41 70 70 44 61  ed as the pAppDa
1132f 74 61 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53  ta value for VFS
11330 0d 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57  ..** objects.  W
11331 65 20 68 61 76 65 20 74 6f 20 64 6f 20 74 68 69  e have to do thi
11332 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6c 65 74  s instead of let
11333 74 69 6e 67 20 70 41 70 70 44 61 74 61 20 70 6f  ting pAppData po
11334 69 6e 74 0d 0a 2a 2a 20 64 69 72 65 63 74 6c 79  int..** directly
11335 20 61 74 20 74 68 65 20 66 69 6e 64 65 72 2d 66   at the finder-f
11336 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65 20 43 39  unction since C9
11337 30 20 72 75 6c 65 73 20 70 72 65 76 65 6e 74 20  0 rules prevent 
11338 61 20 76 6f 69 64 2a 0d 0a 2a 2a 20 66 72 6f 6d  a void*..** from
11339 20 62 65 20 63 61 73 74 20 69 6e 74 6f 20 61 20   be cast into a 
1133a 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
1133b 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61  ...**..**..** Ea
1133c 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ch instance of t
1133d 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 61  his macro genera
1133e 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 3a  tes two objects:
1133f 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20 41 20  ..**..**   *  A 
11340 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33  constant sqlite3
11341 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
11342 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74  ct call METHOD t
11343 68 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0d  hat has locking.
11344 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73  .**      methods
11345 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e   CLOSE, LOCK, UN
11346 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e  LOCK, CKRESLOCK.
11347 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20 41 6e  ..**..**   *  An
11348 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64   I/O method find
11349 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  er function call
1134a 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72  ed FINDER that r
1134b 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1134c 0d 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65  ..**      to the
1134d 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69   METHOD object i
1134e 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  n the previous b
1134f 75 6c 6c 65 74 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  ullet...*/..#def
11350 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49  ine IOMETHODS(FI
11351 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 56 45  NDER, METHOD, VE
11352 52 53 49 4f 4e 2c 20 43 4c 4f 53 45 2c 20 4c 4f  RSION, CLOSE, LO
11353 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f  CK, UNLOCK, CKLO
11354 43 4b 29 20 20 20 20 20 20 5c 0d 0a 73 74 61 74  CK)      \..stat
11355 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
11356 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48  _io_methods METH
11357 4f 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 20  OD = {          
11358 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11359 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 56           \..   V
1135a 45 52 53 49 4f 4e 2c 20 20 20 20 20 20 20 20 20  ERSION,         
1135b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
1135c 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20  ersion */       
1135d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1135e 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 43           \..   C
1135f 4c 4f 53 45 2c 20 20 20 20 20 20 20 20 20 20 20  LOSE,           
11360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
11361 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20  lose */         
11362 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11363 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11364 6e 69 78 52 65 61 64 2c 20 20 20 20 20 20 20 20  nixRead,        
11365 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52             /* xR
11366 65 61 64 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ead */          
11367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11368 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11369 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20 20 20  nixWrite,       
1136a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57             /* xW
1136b 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20  rite */         
1136c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1136d 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
1136e 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 20 20 20  nixTruncate,    
1136f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54             /* xT
11370 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 20 20 20  runcate */      
11371 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11372 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11373 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 20 20  nixSync,        
11374 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
11375 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ync */          
11376 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11377 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11378 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 20  nixFileSize,    
11379 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1137a 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 20  ileSize */      
1137b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1137c 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 4c           \..   L
1137d 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20  OCK,            
1137e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
1137f 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
11380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11381 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 55           \..   U
11382 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20  NLOCK,          
11383 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
11384 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20  nlock */        
11385 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11386 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 43           \..   C
11387 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20  KLOCK,          
11388 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
11389 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1138a 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
1138b 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
1138c 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20  nixFileControl, 
1138d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1138e 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20  ileControl */   
1138f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11390 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11391 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20  nixSectorSize,  
11392 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
11393 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20  ectorSize */    
11394 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11395 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
11396 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74  nixDeviceCharact
11397 65 72 69 73 74 69 63 73 2c 20 20 2f 2a 20 78 44  eristics,  /* xD
11398 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65  eviceCapabilitie
11399 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
1139a 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
1139b 6e 69 78 53 68 6d 4d 61 70 2c 20 20 20 20 20 20  nixShmMap,      
1139c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
1139d 68 6d 4d 61 70 20 2a 2f 20 20 20 20 20 20 20 20  hmMap */        
1139e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1139f 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
113a0 6e 69 78 53 68 6d 4c 6f 63 6b 2c 20 20 20 20 20  nixShmLock,     
113a1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
113a2 68 6d 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20  hmLock */       
113a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113a4 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
113a5 6e 69 78 53 68 6d 42 61 72 72 69 65 72 2c 20 20  nixShmBarrier,  
113a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
113a7 68 6d 42 61 72 72 69 65 72 20 2a 2f 20 20 20 20  hmBarrier */    
113a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113a9 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 75           \..   u
113aa 6e 69 78 53 68 6d 55 6e 6d 61 70 20 20 20 20 20  nixShmUnmap     
113ab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
113ac 68 6d 55 6e 6d 61 70 20 2a 2f 20 20 20 20 20 20  hmUnmap */      
113ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ae 20 20 20 20 20 20 20 20 20 5c 0d 0a 7d 3b 20 20           \..};  
113af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b3 20 20 20 20 20 20 20 20 20 5c 0d 0a 73 74 61 74           \..stat
113b4 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
113b5 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e  _io_methods *FIN
113b6 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20  DER##Impl(const 
113b7 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c  char *z, unixFil
113b8 65 20 2a 70 29 7b 20 20 20 5c 0d 0a 20 20 55 4e  e *p){   \..  UN
113b9 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
113ba 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  ); UNUSED_PARAME
113bb 54 45 52 28 70 29 3b 20 20 20 20 20 20 20 20 20  TER(p);         
113bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113bd 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 72 65           \..  re
113be 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20  turn &METHOD;   
113bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c2 20 20 20 20 20 20 20 20 20 5c 0d 0a 7d 20 20 20           \..}   
113c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113c7 20 20 20 20 20 20 20 20 20 5c 0d 0a 73 74 61 74           \..stat
113c8 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
113c9 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63  _io_methods *(*c
113ca 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f 6e  onst FINDER)(con
113cb 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c  st char*,unixFil
113cc 65 20 2a 70 29 20 20 20 20 5c 0d 0a 20 20 20 20  e *p)    \..    
113cd 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0d  = FINDER##Impl;.
113ce 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 72 65 20 61  .../*..** Here a
113cf 72 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 71  re all of the sq
113d0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
113d1 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 65 61 63   objects for eac
113d2 68 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 6c 6f 63  h of the..** loc
113d3 6b 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e  king strategies.
113d4 20 20 46 75 6e 63 74 69 6f 6e 73 20 74 68 61 74    Functions that
113d5 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73   return pointers
113d6 20 74 6f 20 74 68 65 73 65 20 6d 65 74 68 6f 64   to these method
113d7 73 0d 0a 2a 2a 20 61 72 65 20 61 6c 73 6f 20 63  s..** are also c
113d8 72 65 61 74 65 64 2e 0d 0a 2a 2f 0d 0a 49 4f 4d  reated...*/..IOM
113d9 45 54 48 4f 44 53 28 0d 0a 20 20 70 6f 73 69 78  ETHODS(..  posix
113da 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
113db 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
113dc 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0d  unction name */.
113dd 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  .  posixIoMethod
113de 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
113df 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
113e0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
113e1 2f 0d 0a 20 20 32 2c 20 20 20 20 20 20 20 20 20  /..  2,         
113e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
113e3 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * shared memory 
113e4 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0d 0a 20  is enabled */.. 
113e5 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20   unixClose,     
113e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
113e7 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a  lose method */..
113e8 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20    unixLock,     
113e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
113ea 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a  Lock method */..
113eb 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20    unixUnlock,   
113ec 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
113ed 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
113ee 0d 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73  ..  unixCheckRes
113ef 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a  ervedLock     /*
113f0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
113f1 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 29  ock method */..)
113f2 0d 0a 49 4f 4d 45 54 48 4f 44 53 28 0d 0a 20 20  ..IOMETHODS(..  
113f3 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20  nolockIoFinder, 
113f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
113f5 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
113f6 65 20 2a 2f 0d 0a 20 20 6e 6f 6c 6f 63 6b 49 6f  e */..  nolockIo
113f7 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20  Methods,        
113f8 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f    /* sqlite3_io_
113f9 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e  methods object n
113fa 61 6d 65 20 2a 2f 0d 0a 20 20 31 2c 20 20 20 20  ame */..  1,    
113fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113fc 20 20 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65      /* shared me
113fd 6d 6f 72 79 20 69 73 20 64 69 73 61 62 6c 65 64  mory is disabled
113fe 20 2a 2f 0d 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f   */..  nolockClo
113ff 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
11400 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
11401 64 20 2a 2f 0d 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f  d */..  nolockLo
11402 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
11403 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
11404 64 20 2a 2f 0d 0a 20 20 6e 6f 6c 6f 63 6b 55 6e  d */..  nolockUn
11405 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
11406 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74    /* xUnlock met
11407 68 6f 64 20 2a 2f 0d 0a 20 20 6e 6f 6c 6f 63 6b  hod */..  nolock
11408 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
11409 6b 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73  k   /* xCheckRes
1140a 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64  ervedLock method
1140b 20 2a 2f 0d 0a 29 0d 0a 49 4f 4d 45 54 48 4f 44   */..)..IOMETHOD
1140c 53 28 0d 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 46  S(..  dotlockIoF
1140d 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20  inder,          
1140e 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
1140f 6f 6e 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 64 6f  on name */..  do
11410 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20  tlockIoMethods, 
11411 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
11412 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
11413 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20  ject name */..  
11414 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
11415 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61            /* sha
11416 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69  red memory is di
11417 73 61 62 6c 65 64 20 2a 2f 0d 0a 20 20 64 6f 74  sabled */..  dot
11418 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20  lockClose,      
11419 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
1141a 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 64 6f   method */..  do
1141b 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20  tlockLock,      
1141c 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
1141d 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 64 6f   method */..  do
1141e 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20  tlockUnlock,    
1141f 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
11420 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20  ck method */..  
11421 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65  dotlockCheckRese
11422 72 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68  rvedLock  /* xCh
11423 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
11424 6d 65 74 68 6f 64 20 2a 2f 0d 0a 29 0d 0a 0d 0a  method */..)....
11425 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
11426 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
11427 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a  && !OS_VXWORKS..
11428 49 4f 4d 45 54 48 4f 44 53 28 0d 0a 20 20 66 6c  IOMETHODS(..  fl
11429 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
1142a 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
1142b 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
1142c 2a 2f 0d 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74  */..  flockIoMet
1142d 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20  hods,           
1142e 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
1142f 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
11430 65 20 2a 2f 0d 0a 20 20 31 2c 20 20 20 20 20 20  e */..  1,      
11431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11432 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f    /* shared memo
11433 72 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a  ry is disabled *
11434 2f 0d 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 2c  /..  flockClose,
11435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11436 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
11437 2a 2f 0d 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c  */..  flockLock,
11438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11439 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
1143a 2a 2f 0d 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63  */..  flockUnloc
1143b 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
1143c 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
1143d 64 20 2a 2f 0d 0a 20 20 66 6c 6f 63 6b 43 68 65  d */..  flockChe
1143e 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
1143f 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
11440 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
11441 2f 0d 0a 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  /..)..#endif....
11442 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a  #if OS_VXWORKS..
11443 49 4f 4d 45 54 48 4f 44 53 28 0d 0a 20 20 73 65  IOMETHODS(..  se
11444 6d 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  mIoFinder,      
11445 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
11446 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
11447 2a 2f 0d 0a 20 20 73 65 6d 49 6f 4d 65 74 68 6f  */..  semIoMetho
11448 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ds,             
11449 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
1144a 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
1144b 65 20 2a 2f 0d 0a 20 20 31 2c 20 20 20 20 20 20  e */..  1,      
1144c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1144d 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f    /* shared memo
1144e 72 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a  ry is disabled *
1144f 2f 0d 0a 20 20 73 65 6d 43 6c 6f 73 65 2c 20 20  /..  semClose,  
11450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11451 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
11452 2a 2f 0d 0a 20 20 73 65 6d 4c 6f 63 6b 2c 20 20  */..  semLock,  
11453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11454 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
11455 2a 2f 0d 0a 20 20 73 65 6d 55 6e 6c 6f 63 6b 2c  */..  semUnlock,
11456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11457 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
11458 64 20 2a 2f 0d 0a 20 20 73 65 6d 43 68 65 63 6b  d */..  semCheck
11459 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
1145a 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
1145b 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
1145c 2f 0d 0a 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  /..)..#endif....
1145d 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
1145e 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
1145f 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
11460 53 54 59 4c 45 0d 0a 49 4f 4d 45 54 48 4f 44 53  STYLE..IOMETHODS
11461 28 0d 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72  (..  afpIoFinder
11462 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
11463 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
11464 6e 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 61 66 70  n name */..  afp
11465 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
11466 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
11467 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
11468 65 63 74 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 31  ect name */..  1
11469 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1146a 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72           /* shar
1146b 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 73  ed memory is dis
1146c 61 62 6c 65 64 20 2a 2f 0d 0a 20 20 61 66 70 43  abled */..  afpC
1146d 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
1146e 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
1146f 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 61 66 70  method */..  afp
11470 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
11471 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
11472 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 61 66 70  method */..  afp
11473 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
11474 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
11475 6b 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 61  k method */..  a
11476 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  fpCheckReservedL
11477 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65  ock      /* xChe
11478 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
11479 65 74 68 6f 64 20 2a 2f 0d 0a 29 0d 0a 23 65 6e  ethod */..)..#en
1147a 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
1147b 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  e proxy locking 
1147c 6d 65 74 68 6f 64 20 69 73 20 61 20 22 73 75 70  method is a "sup
1147d 65 72 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 68  er-method" in th
1147e 65 20 73 65 6e 73 65 20 74 68 61 74 20 69 74 0d  e sense that it.
1147f 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 6f 6e 64  .** opens second
11480 61 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70  ary file descrip
11481 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6e  tors for the con
11482 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65  ch and lock file
11483 73 20 61 6e 64 0d 0a 2a 2a 20 69 74 20 75 73 65  s and..** it use
11484 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 69 6c  s proxy, dot-fil
11485 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c 6f 63  e, AFP, and floc
11486 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  k() locking meth
11487 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0d 0a 2a 2a  ods on those..**
11488 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 73   secondary files
11489 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73  .  For this reas
1148a 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e  on, the division
1148b 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
1148c 0d 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69  ..** proxy locki
1148d 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75  ng is located mu
1148e 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20  ch further down 
1148f 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75  in the file.  Bu
11490 74 20 77 65 20 6e 65 65 64 0d 0a 2a 2a 20 74 6f  t we need..** to
11491 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65   go ahead and de
11492 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33  fine the sqlite3
11493 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20  _io_methods and 
11494 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0d  finder function.
11495 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f  .** for proxy lo
11496 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20  cking here.  So 
11497 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61  we forward decla
11498 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  re the I/O metho
11499 64 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66  ds...*/..#if def
1149a 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
1149b 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
1149c 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a  _LOCKING_STYLE..
1149d 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
1149e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
1149f 6c 65 2a 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e  le*);..static in
114a0 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69  t proxyLock(sqli
114a1 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
114a2 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  ..static int pro
114a3 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  xyUnlock(sqlite3
114a4 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0d 0a 73  _file*, int);..s
114a5 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
114a6 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
114a7 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
114a8 69 6e 74 2a 29 3b 0d 0a 49 4f 4d 45 54 48 4f 44  int*);..IOMETHOD
114a9 53 28 0d 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e  S(..  proxyIoFin
114aa 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  der,            
114ab 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
114ac 6f 6e 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 70 72  on name */..  pr
114ad 6f 78 79 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  oxyIoMethods,   
114ae 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
114af 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
114b0 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20  ject name */..  
114b1 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
114b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61            /* sha
114b3 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69  red memory is di
114b4 73 61 62 6c 65 64 20 2a 2f 0d 0a 20 20 70 72 6f  sabled */..  pro
114b5 78 79 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  xyClose,        
114b6 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
114b7 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 70 72   method */..  pr
114b8 6f 78 79 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  oxyLock,        
114b9 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
114ba 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 70 72   method */..  pr
114bb 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  oxyUnlock,      
114bc 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
114bd 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20  ck method */..  
114be 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76  proxyCheckReserv
114bf 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68  edLock    /* xCh
114c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
114c1 6d 65 74 68 6f 64 20 2a 2f 0d 0a 29 0d 0a 23 65  method */..)..#e
114c2 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 6e 66 73 20 6c  ndif..../* nfs l
114c3 6f 63 6b 64 20 6f 6e 20 4f 53 58 20 31 30 2e 33  ockd on OSX 10.3
114c4 2b 20 64 6f 65 73 6e 27 74 20 63 6c 65 61 72 20  + doesn't clear 
114c5 77 72 69 74 65 20 6c 6f 63 6b 73 20 77 68 65 6e  write locks when
114c6 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20   a read lock is 
114c7 73 65 74 20 2a 2f 0d 0a 23 69 66 20 64 65 66 69  set */..#if defi
114c8 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
114c9 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
114ca 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 49  LOCKING_STYLE..I
114cb 4f 4d 45 54 48 4f 44 53 28 0d 0a 20 20 6e 66 73  OMETHODS(..  nfs
114cc 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
114cd 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
114ce 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
114cf 2a 2f 0d 0a 20 20 6e 66 73 49 6f 4d 65 74 68 6f  */..  nfsIoMetho
114d0 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ds,             
114d1 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
114d2 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
114d3 6d 65 20 2a 2f 0d 0a 20 20 31 2c 20 20 20 20 20  me */..  1,     
114d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
114d5 20 20 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65      /* shared me
114d6 6d 6f 72 79 20 69 73 20 64 69 73 61 62 6c 65 64  mory is disabled
114d7 20 2a 2f 0d 0a 20 20 75 6e 69 78 43 6c 6f 73 65   */..  unixClose
114d8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
114d9 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
114da 6f 64 20 2a 2f 0d 0a 20 20 75 6e 69 78 4c 6f 63  od */..  unixLoc
114db 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
114dc 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74      /* xLock met
114dd 68 6f 64 20 2a 2f 0d 0a 20 20 6e 66 73 55 6e 6c  hod */..  nfsUnl
114de 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
114df 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20       /* xUnlock 
114e0 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20 20 75 6e 69  method */..  uni
114e1 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
114e2 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
114e3 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
114e4 74 68 6f 64 20 2a 2f 0d 0a 29 0d 0a 23 65 6e 64  thod */..)..#end
114e5 69 66 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  if....#if define
114e6 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
114e7 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
114e8 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 2f 2a 20  CKING_STYLE../* 
114e9 0d 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 65  ..** This "finde
114ea 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65  r" function atte
114eb 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  mpts to determin
114ec 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69  e the best locki
114ed 6e 67 20 73 74 72 61 74 65 67 79 20 0d 0a 2a 2a  ng strategy ..**
114ee 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
114ef 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68  e file "filePath
114f0 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75  ".  It then retu
114f1 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  rns the sqlite3_
114f2 69 6f 5f 6d 65 74 68 6f 64 73 0d 0a 2a 2a 20 6f  io_methods..** o
114f3 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65  bject that imple
114f4 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74  ments that strat
114f5 65 67 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  egy...**..** Thi
114f6 73 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20  s is for MacOSX 
114f7 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  only...*/..stati
114f8 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
114f9 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f  io_methods *auto
114fa 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
114fb 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  (..  const char 
114fc 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a  *filePath,    /*
114fd 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
114fe 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20  abase file */.. 
114ff 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20   unixFile *pNew 
11500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 70 65            /* ope
11501 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 66 6f  n file object fo
11502 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
11503 69 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 74  ile */..){..  st
11504 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
11505 74 20 4d 61 70 70 69 6e 67 20 7b 0d 0a 20 20 20  t Mapping {..   
11506 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
11507 6c 65 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20  lesystem;       
11508 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79         /* Filesy
11509 73 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a  stem type name *
1150a 2f 0d 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  /..    const sql
1150b 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
1150c 2a 70 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20  *pMethods;   /* 
1150d 41 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  Appropriate lock
1150e 69 6e 67 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a 20  ing method */.. 
1150f 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b 0d 0a 20   } aMap[] = {.. 
11510 20 20 20 7b 20 22 68 66 73 22 2c 20 20 20 20 26     { "hfs",    &
11511 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d  posixIoMethods }
11512 2c 0d 0a 20 20 20 20 7b 20 22 75 66 73 22 2c 20  ,..    { "ufs", 
11513 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f     &posixIoMetho
11514 64 73 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 61 66  ds },..    { "af
11515 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74  pfs",  &afpIoMet
11516 68 6f 64 73 20 7d 2c 0d 0a 20 20 20 20 7b 20 22  hods },..    { "
11517 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d  smbfs",  &afpIoM
11518 65 74 68 6f 64 73 20 7d 2c 0d 0a 20 20 20 20 7b  ethods },..    {
11519 20 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f   "webdav", &nolo
1151a 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0d 0a  ckIoMethods },..
1151b 20 20 20 20 7b 20 30 2c 20 30 20 7d 0d 0a 20 20      { 0, 0 }..  
1151c 7d 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  };..  int i;..  
1151d 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73  struct statfs fs
1151e 49 6e 66 6f 3b 0d 0a 20 20 73 74 72 75 63 74 20  Info;..  struct 
1151f 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0d  flock lockInfo;.
11520 0a 0d 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61  ...  if( !filePa
11521 74 68 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66  th ){..    /* If
11522 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20   filePath==NULL 
11523 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72  that means we ar
11524 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
11525 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0d   transient file.
11526 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65  .    ** that doe
11527 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
11528 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0d 0a 20 20 20   locked. */..   
11529 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49   return &nolockI
1152a 6f 4d 65 74 68 6f 64 73 3b 0d 0a 20 20 7d 0d 0a  oMethods;..  }..
1152b 20 20 69 66 28 20 73 74 61 74 66 73 28 66 69 6c    if( statfs(fil
1152c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20  ePath, &fsInfo) 
1152d 21 3d 20 2d 31 20 29 7b 0d 0a 20 20 20 20 69 66  != -1 ){..    if
1152e 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73  ( fsInfo.f_flags
1152f 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b   & MNT_RDONLY ){
11530 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 26  ..      return &
11531 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
11532 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72  ..    }..    for
11533 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46  (i=0; aMap[i].zF
11534 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b  ilesystem; i++){
11535 0d 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63  ..      if( strc
11536 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79  mp(fsInfo.f_fsty
11537 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e  pename, aMap[i].
11538 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20  zFilesystem)==0 
11539 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
1153a 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68  rn aMap[i].pMeth
1153b 6f 64 73 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ods;..      }.. 
1153c 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
1153d 2a 20 44 65 66 61 75 6c 74 20 63 61 73 65 2e 20  * Default case. 
1153e 48 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74  Handles, amongst
1153f 20 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 2e 0d   others, "nfs"..
11540 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 74 65 2d  .  ** Test byte-
11541 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67  range lock using
11542 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 65   fcntl(). If the
11543 20 63 61 6c 6c 20 73 75 63 63 65 65 64 73 2c 20   call succeeds, 
11544 0d 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68  ..  ** assume th
11545 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  at the file-syst
11546 65 6d 20 73 75 70 70 6f 72 74 73 20 50 4f 53 49  em supports POSI
11547 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0d  X style locks. .
11548 0a 20 20 2a 2f 0d 0a 20 20 6c 6f 63 6b 49 6e 66  .  */..  lockInf
11549 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0d 0a 20 20  o.l_len = 1;..  
1154a 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74  lockInfo.l_start
1154b 20 3d 20 30 3b 0d 0a 20 20 6c 6f 63 6b 49 6e 66   = 0;..  lockInf
1154c 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  o.l_whence = SEE
1154d 4b 5f 53 45 54 3b 0d 0a 20 20 6c 6f 63 6b 49 6e  K_SET;..  lockIn
1154e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44  fo.l_type = F_RD
1154f 4c 43 4b 3b 0d 0a 20 20 69 66 28 20 6f 73 46 63  LCK;..  if( osFc
11550 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47  ntl(pNew->h, F_G
11551 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29  ETLK, &lockInfo)
11552 21 3d 2d 31 20 29 20 7b 0d 0a 20 20 20 20 69 66  !=-1 ) {..    if
11553 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e  ( strcmp(fsInfo.
11554 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 6e  f_fstypename, "n
11555 66 73 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  fs")==0 ){..    
11556 20 20 72 65 74 75 72 6e 20 26 6e 66 73 49 6f 4d    return &nfsIoM
11557 65 74 68 6f 64 73 3b 0d 0a 20 20 20 20 7d 20 65  ethods;..    } e
11558 6c 73 65 20 7b 0d 0a 20 20 20 20 20 20 72 65 74  lse {..      ret
11559 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68  urn &posixIoMeth
1155a 6f 64 73 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ods;..    }..  }
1155b 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72  else{..    retur
1155c 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68  n &dotlockIoMeth
1155d 6f 64 73 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73 74  ods;..  }..}..st
1155e 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
1155f 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0d 0a  e3_io_methods ..
11560 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c    *(*const autol
11561 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e  ockIoFinder)(con
11562 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c  st char*,unixFil
11563 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f  e*) = autolockIo
11564 46 69 6e 64 65 72 49 6d 70 6c 3b 0d 0a 0d 0a 23  FinderImpl;....#
11565 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
11566 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
11567 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
11568 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0d 0a 0d  KING_STYLE */...
11569 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 20  .#if OS_VXWORKS 
1156a 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
1156b 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a  _LOCKING_STYLE..
1156c 2f 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 22 66 69  /* ..** This "fi
1156d 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61  nder" function a
1156e 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
1156f 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f  mine the best lo
11570 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0d  cking strategy .
11571 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61  .** for the data
11572 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50  base file "fileP
11573 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72  ath".  It then r
11574 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74  eturns the sqlit
11575 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0d 0a 2a  e3_io_methods..*
11576 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d  * object that im
11577 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74  plements that st
11578 72 61 74 65 67 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  rategy...**..** 
11579 54 68 69 73 20 69 73 20 66 6f 72 20 56 58 57 6f  This is for VXWo
1157a 72 6b 73 20 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 73  rks only...*/..s
1157b 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
1157c 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
1157d 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
1157e 49 6d 70 6c 28 0d 0a 20 20 63 6f 6e 73 74 20 63  Impl(..  const c
1157f 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20  har *filePath,  
11580 20 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65    /* name of the
11581 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
11582 2f 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  /..  unixFile *p
11583 4e 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a  New           /*
11584 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 6f   the open file o
11585 62 6a 65 63 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  bject */..){..  
11586 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
11587 6b 49 6e 66 6f 3b 0d 0a 0d 0a 20 20 69 66 28 20  kInfo;....  if( 
11588 21 66 69 6c 65 50 61 74 68 20 29 7b 0d 0a 20 20  !filePath ){..  
11589 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68    /* If filePath
1158a 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e  ==NULL that mean
1158b 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  s we are dealing
1158c 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e   with a transien
1158d 74 20 66 69 6c 65 0d 0a 20 20 20 20 2a 2a 20 74  t file..    ** t
1158e 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  hat does not nee
1158f 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20  d to be locked. 
11590 2a 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 26  */..    return &
11591 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
11592 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 65  ..  }....  /* Te
11593 73 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73  st if fcntl() is
11594 20 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75   supported and u
11595 73 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c  se POSIX style l
11596 6f 63 6b 73 2e 0d 0a 20 20 2a 2a 20 4f 74 68 65  ocks...  ** Othe
11597 72 77 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20  rwise fall back 
11598 74 6f 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d  to the named sem
11599 61 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0d 0a  aphore method...
1159a 20 20 2a 2f 0d 0a 20 20 6c 6f 63 6b 49 6e 66 6f    */..  lockInfo
1159b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0d 0a 20 20 6c  .l_len = 1;..  l
1159c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20  ockInfo.l_start 
1159d 3d 20 30 3b 0d 0a 20 20 6c 6f 63 6b 49 6e 66 6f  = 0;..  lockInfo
1159e 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
1159f 5f 53 45 54 3b 0d 0a 20 20 6c 6f 63 6b 49 6e 66  _SET;..  lockInf
115a0 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c  o.l_type = F_RDL
115a1 43 4b 3b 0d 0a 20 20 69 66 28 20 6f 73 46 63 6e  CK;..  if( osFcn
115a2 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45  tl(pNew->h, F_GE
115a3 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21  TLK, &lockInfo)!
115a4 3d 2d 31 20 29 20 7b 0d 0a 20 20 20 20 72 65 74  =-1 ) {..    ret
115a5 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68  urn &posixIoMeth
115a6 6f 64 73 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ods;..  }else{..
115a7 20 20 20 20 72 65 74 75 72 6e 20 26 73 65 6d 49      return &semI
115a8 6f 4d 65 74 68 6f 64 73 3b 0d 0a 20 20 7d 0d 0a  oMethods;..  }..
115a9 7d 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  }..static const 
115aa 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
115ab 64 73 20 0d 0a 20 20 2a 28 2a 63 6f 6e 73 74 20  ds ..  *(*const 
115ac 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
115ad 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e  )(const char*,un
115ae 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c  ixFile*) = autol
115af 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b  ockIoFinderImpl;
115b0 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53  ....#endif /* OS
115b1 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49  _VXWORKS && SQLI
115b2 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
115b3 47 5f 53 54 59 4c 45 20 2a 2f 0d 0a 0d 0a 2f 2a  G_STYLE */..../*
115b4 0d 0a 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 74  ..** An abstract
115b5 20 74 79 70 65 20 66 6f 72 20 61 20 70 6f 69 6e   type for a poin
115b6 74 65 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 68  ter to a IO meth
115b7 6f 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69  od finder functi
115b8 6f 6e 3a 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  on:..*/..typedef
115b9 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
115ba 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69 6e  o_methods *(*fin
115bb 64 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74 20  der_type)(const 
115bc 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29  char*,unixFile*)
115bd 3b 0d 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ;....../********
115be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115c2 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****..**********
115c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115c4 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d  ** sqlite3_vfs m
115c5 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ethods *********
115c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115c7 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ***..**..** This
115c8 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69   division contai
115c9 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ns the implement
115ca 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73  ation of methods
115cb 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69   on the..** sqli
115cc 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0d  te3_vfs object..
115cd 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  .*/..../*..** In
115ce 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e  itialize the con
115cf 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69  tents of the uni
115d0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  xFile structure 
115d1 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49  pointed to by pI
115d2 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  d...*/..static i
115d3 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c  nt fillInUnixFil
115d4 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  e(..  sqlite3_vf
115d5 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f 2a  s *pVfs,      /*
115d6 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20   Pointer to vfs 
115d7 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74  object */..  int
115d8 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
115d9 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
115da 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 20  e descriptor of 
115db 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
115dc 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  d */..  sqlite3_
115dd 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 20  file *pId,      
115de 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65 20  /* Write to the 
115df 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
115e0 72 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 63 6f  re here */..  co
115e1 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
115e2 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ame,  /* Name of
115e3 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20   the file being 
115e4 6f 70 65 6e 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  opened */..  int
115e5 20 63 74 72 6c 46 6c 61 67 73 20 20 20 20 20 20   ctrlFlags      
115e6 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20       /* Zero or 
115e7 6d 6f 72 65 20 55 4e 49 58 46 49 4c 45 5f 2a 20  more UNIXFILE_* 
115e8 76 61 6c 75 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20  values */..){.. 
115e9 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
115ea 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b  o_methods *pLock
115eb 69 6e 67 53 74 79 6c 65 3b 0d 0a 20 20 75 6e 69  ingStyle;..  uni
115ec 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75  xFile *pNew = (u
115ed 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0d 0a  nixFile *)pId;..
115ee 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
115ef 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  E_OK;....  asser
115f0 74 28 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 3d  t( pNew->pInode=
115f1 3d 4e 55 4c 4c 20 29 3b 0d 0a 0d 0a 20 20 2f 2a  =NULL );....  /*
115f2 20 55 73 75 61 6c 6c 79 20 74 68 65 20 70 61 74   Usually the pat
115f3 68 20 7a 46 69 6c 65 6e 61 6d 65 20 73 68 6f 75  h zFilename shou
115f4 6c 64 20 6e 6f 74 20 62 65 20 61 20 72 65 6c 61  ld not be a rela
115f5 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2e 20 54  tive pathname. T
115f6 68 65 0d 0a 20 20 2a 2a 20 65 78 63 65 70 74 69  he..  ** excepti
115f7 6f 6e 20 69 73 20 77 68 65 6e 20 6f 70 65 6e 69  on is when openi
115f8 6e 67 20 74 68 65 20 70 72 6f 78 79 20 22 63 6f  ng the proxy "co
115f9 6e 63 68 22 20 66 69 6c 65 20 69 6e 20 62 75 69  nch" file in bui
115fa 6c 64 73 20 74 68 61 74 0d 0a 20 20 2a 2a 20 69  lds that..  ** i
115fb 6e 63 6c 75 64 65 20 74 68 65 20 73 70 65 63 69  nclude the speci
115fc 61 6c 20 41 70 70 6c 65 20 6c 6f 63 6b 69 6e 67  al Apple locking
115fd 20 73 74 79 6c 65 73 2e 0d 0a 20 20 2a 2f 0d 0a   styles...  */..
115fe 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
115ff 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
11600 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
11601 53 54 59 4c 45 0d 0a 20 20 61 73 73 65 72 74 28  STYLE..  assert(
11602 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c   zFilename==0 ||
11603 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 27   zFilename[0]=='
11604 2f 27 20 0d 0a 20 20 20 20 7c 7c 20 70 56 66 73  /' ..    || pVfs
11605 2d 3e 70 41 70 70 44 61 74 61 3d 3d 28 76 6f 69  ->pAppData==(voi
11606 64 2a 29 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  d*)&autolockIoFi
11607 6e 64 65 72 20 29 3b 0d 0a 23 65 6c 73 65 0d 0a  nder );..#else..
11608 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e    assert( zFilen
11609 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
1160a 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0d 0a  ame[0]=='/' );..
1160b 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 4e  #endif....  /* N
1160c 6f 20 6c 6f 63 6b 69 6e 67 20 6f 63 63 75 72 73  o locking occurs
1160d 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 66 69   in temporary fi
1160e 6c 65 73 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  les */..  assert
1160f 28 20 7a 46 69 6c 65 6e 61 6d 65 21 3d 30 20 7c  ( zFilename!=0 |
11610 7c 20 28 63 74 72 6c 46 6c 61 67 73 20 26 20 55  | (ctrlFlags & U
11611 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b 29 21  NIXFILE_NOLOCK)!
11612 3d 30 20 29 3b 0d 0a 0d 0a 20 20 4f 53 54 52 41  =0 );....  OSTRA
11613 43 45 28 28 22 4f 50 45 4e 20 20 20 20 25 2d 33  CE(("OPEN    %-3
11614 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c  d %s\n", h, zFil
11615 65 6e 61 6d 65 29 29 3b 0d 0a 20 20 70 4e 65 77  ename));..  pNew
11616 2d 3e 68 20 3d 20 68 3b 0d 0a 20 20 70 4e 65 77  ->h = h;..  pNew
11617 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0d 0a  ->pVfs = pVfs;..
11618 20 20 70 4e 65 77 2d 3e 7a 50 61 74 68 20 3d 20    pNew->zPath = 
11619 7a 46 69 6c 65 6e 61 6d 65 3b 0d 0a 20 20 70 4e  zFilename;..  pN
1161a 65 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20 3d 20  ew->ctrlFlags = 
1161b 28 75 38 29 63 74 72 6c 46 6c 61 67 73 3b 0d 0a  (u8)ctrlFlags;..
1161c 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75 72    if( sqlite3_ur
1161d 69 5f 62 6f 6f 6c 65 61 6e 28 28 28 63 74 72 6c  i_boolean(((ctrl
1161e 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45  Flags & UNIXFILE
1161f 5f 55 52 49 29 20 3f 20 7a 46 69 6c 65 6e 61 6d  _URI) ? zFilenam
11620 65 20 3a 20 30 29 2c 0d 0a 20 20 20 20 20 20 20  e : 0),..       
11621 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11622 20 20 20 20 22 70 73 6f 77 22 2c 20 53 51 4c 49      "psow", SQLI
11623 54 45 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45  TE_POWERSAFE_OVE
11624 52 57 52 49 54 45 29 20 29 7b 0d 0a 20 20 20 20  RWRITE) ){..    
11625 70 4e 65 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20  pNew->ctrlFlags 
11626 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57  |= UNIXFILE_PSOW
11627 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6d 65  ;..  }..  if( me
11628 6d 63 6d 70 28 70 56 66 73 2d 3e 7a 4e 61 6d 65  mcmp(pVfs->zName
11629 2c 22 75 6e 69 78 2d 65 78 63 6c 22 2c 31 30 29  ,"unix-excl",10)
1162a 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 4e 65 77  ==0 ){..    pNew
1162b 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55  ->ctrlFlags |= U
1162c 4e 49 58 46 49 4c 45 5f 45 58 43 4c 3b 0d 0a 20  NIXFILE_EXCL;.. 
1162d 20 7d 0d 0a 0d 0a 23 69 66 20 4f 53 5f 56 58 57   }....#if OS_VXW
1162e 4f 52 4b 53 0d 0a 20 20 70 4e 65 77 2d 3e 70 49  ORKS..  pNew->pI
1162f 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46  d = vxworksFindF
11630 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29  ileId(zFilename)
11631 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70  ;..  if( pNew->p
11632 49 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63 74  Id==0 ){..    ct
11633 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46  rlFlags |= UNIXF
11634 49 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0d 0a 20 20 20  ILE_NOLOCK;..   
11635 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
11636 45 4d 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  EM;..  }..#endif
11637 0d 0a 0d 0a 20 20 69 66 28 20 63 74 72 6c 46 6c  ....  if( ctrlFl
11638 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 4e  ags & UNIXFILE_N
11639 4f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 70 4c  OLOCK ){..    pL
1163a 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e  ockingStyle = &n
1163b 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0d  olockIoMethods;.
1163c 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
1163d 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28  LockingStyle = (
1163e 2a 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a 29  **(finder_type*)
1163f 70 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 28  pVfs->pAppData)(
11640 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29  zFilename, pNew)
11641 3b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  ;..#if SQLITE_EN
11642 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
11643 4c 45 0d 0a 20 20 20 20 2f 2a 20 43 61 63 68 65  LE..    /* Cache
11644 20 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68   zFilename in th
11645 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  e locking contex
11646 74 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f  t (AFP and dotlo
11647 63 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72  ck override) for
11648 0d 0a 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f  ..    ** proxyLo
11649 63 6b 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73  ck activation is
1164a 20 70 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74   possible (remot
1164b 65 20 70 72 6f 78 79 20 69 73 20 62 61 73 65 64  e proxy is based
1164c 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0d 0a 20 20   on db name)..  
1164d 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72    ** zFilename r
1164e 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74  emains valid unt
1164f 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65  il file is close
11650 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f  d, to support */
11651 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b  ..    pNew->lock
11652 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76 6f  ingContext = (vo
11653 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0d 0a  id*)zFilename;..
11654 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20  #endif..  }.... 
11655 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79   if( pLockingSty
11656 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65  le == &posixIoMe
11657 74 68 6f 64 73 0d 0a 23 69 66 20 64 65 66 69 6e  thods..#if defin
11658 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
11659 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
1165a 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20  OCKING_STYLE..  
1165b 20 20 7c 7c 20 70 4c 6f 63 6b 69 6e 67 53 74 79    || pLockingSty
1165c 6c 65 20 3d 3d 20 26 6e 66 73 49 6f 4d 65 74 68  le == &nfsIoMeth
1165d 6f 64 73 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 29  ods..#endif..  )
1165e 7b 0d 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  {..    unixEnter
1165f 4d 75 74 65 78 28 29 3b 0d 0a 20 20 20 20 72 63  Mutex();..    rc
11660 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f   = findInodeInfo
11661 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 49  (pNew, &pNew->pI
11662 6e 6f 64 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  node);..    if( 
11663 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
11664 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e  ..      /* If an
11665 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69   error occured i
11666 6e 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28  n findInodeInfo(
11667 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c  ), close the fil
11668 65 20 64 65 73 63 72 69 70 74 6f 72 0d 0a 20 20  e descriptor..  
11669 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65      ** immediate
1166a 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61  ly, before relea
1166b 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20  sing the mutex. 
1166c 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 29 20  findInodeInfo() 
1166d 6d 61 79 20 66 61 69 6c 0d 0a 20 20 20 20 20 20  may fail..      
1166e 2a 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72  ** in two scenar
1166f 69 6f 73 3a 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a  ios:..      **..
11670 20 20 20 20 20 20 2a 2a 20 20 20 28 61 29 20 41        **   (a) A
11671 20 63 61 6c 6c 20 74 6f 20 66 73 74 61 74 28 29   call to fstat()
11672 20 66 61 69 6c 65 64 2e 0d 0a 20 20 20 20 20 20   failed...      
11673 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 6c 6c 6f  **   (b) A mallo
11674 63 20 66 61 69 6c 65 64 2e 0d 0a 20 20 20 20 20  c failed...     
11675 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 53 63   **..      ** Sc
11676 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 79 20 6f  enario (b) may o
11677 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 68 65  nly occur if the
11678 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
11679 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0d 0a 20 20  ing no other..  
1167a 20 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 73 63      ** file desc
1167b 72 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  riptors open on 
1167c 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 20 49  the same file. I
1167d 66 20 74 68 65 72 65 20 77 65 72 65 20 6f 74 68  f there were oth
1167e 65 72 20 66 69 6c 65 0d 0a 20 20 20 20 20 20 2a  er file..      *
1167f 2a 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 6e  * descriptors on
11680 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e   this file, then
11681 20 6e 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64   no malloc would
11682 20 62 65 20 72 65 71 75 69 72 65 64 20 62 79 0d   be required by.
11683 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e 64 49 6e  .      ** findIn
11684 6f 64 65 49 6e 66 6f 28 29 2e 20 49 66 20 74 68  odeInfo(). If th
11685 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
11686 69 74 20 69 73 20 71 75 69 74 65 20 73 61 66 65  it is quite safe
11687 20 74 6f 20 63 6c 6f 73 65 0d 0a 20 20 20 20 20   to close..     
11688 20 2a 2a 20 68 61 6e 64 6c 65 20 68 20 2d 20 61   ** handle h - a
11689 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  s it is guarante
1168a 65 64 20 74 68 61 74 20 6e 6f 20 70 6f 73 69 78  ed that no posix
1168b 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 62 65 20 72   locks will be r
1168c 65 6c 65 61 73 65 64 0d 0a 20 20 20 20 20 20 2a  eleased..      *
1168d 2a 20 62 79 20 64 6f 69 6e 67 20 73 6f 2e 0d 0a  * by doing so...
1168e 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
1168f 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f 20 28  ** If scenario (
11690 61 29 20 63 61 75 73 65 64 20 74 68 65 20 65 72  a) caused the er
11691 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 73 20  ror then things 
11692 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 65 2e  are not so safe.
11693 20 54 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20 69   The..      ** i
11694 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 69  mplicit assumpti
11695 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20  on here is that 
11696 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c 73  if fstat() fails
11697 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e 0d  , things are in.
11698 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62  .      ** such b
11699 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72  ad shape that dr
1169a 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72  opping a lock or
1169b 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74   two doesn't mat
1169c 74 65 72 20 6d 75 63 68 2e 0d 0a 20 20 20 20 20  ter much...     
1169d 20 2a 2f 0d 0a 20 20 20 20 20 20 72 6f 62 75 73   */..      robus
1169e 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c  t_close(pNew, h,
1169f 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20   __LINE__);..   
116a0 20 20 20 68 20 3d 20 2d 31 3b 0d 0a 20 20 20 20     h = -1;..    
116a1 7d 0d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  }..    unixLeave
116a2 4d 75 74 65 78 28 29 3b 0d 0a 20 20 7d 0d 0a 0d  Mutex();..  }...
116a3 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
116a4 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
116a5 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   && defined(__AP
116a6 50 4c 45 5f 5f 29 0d 0a 20 20 65 6c 73 65 20 69  PLE__)..  else i
116a7 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
116a8 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64   == &afpIoMethod
116a9 73 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41 46 50  s ){..    /* AFP
116aa 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68   locking uses th
116ab 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69  e file path so i
116ac 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e  t needs to be in
116ad 63 6c 75 64 65 64 20 69 6e 0d 0a 20 20 20 20 2a  cluded in..    *
116ae 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67  * the afpLocking
116af 43 6f 6e 74 65 78 74 2e 0d 0a 20 20 20 20 2a 2f  Context...    */
116b0 0d 0a 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67  ..    afpLocking
116b1 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0d 0a  Context *pCtx;..
116b2 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e      pNew->lockin
116b3 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20  gContext = pCtx 
116b4 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
116b5 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20  ( sizeof(*pCtx) 
116b6 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 74 78  );..    if( pCtx
116b7 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  ==0 ){..      rc
116b8 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
116b9 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
116ba 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65      /* NB: zFile
116bb 6e 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20  name exists and 
116bc 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e  remains valid un
116bd 74 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20  til the file is 
116be 63 6c 6f 73 65 64 0d 0a 20 20 20 20 20 20 2a 2a  closed..      **
116bf 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65   according to re
116c0 71 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31  quirement F11141
116c1 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20  .  So we do not 
116c2 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0d 0a  need to make a..
116c3 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66        ** copy of
116c4 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 2a   the filename. *
116c5 2f 0d 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 64  /..      pCtx->d
116c6 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 6e 61 6d  bPath = zFilenam
116c7 65 3b 0d 0a 20 20 20 20 20 20 70 43 74 78 2d 3e  e;..      pCtx->
116c8 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0d 0a 20  reserved = 0;.. 
116c9 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28       srandomdev(
116ca 29 3b 0d 0a 20 20 20 20 20 20 75 6e 69 78 45 6e  );..      unixEn
116cb 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 20  terMutex();..   
116cc 20 20 20 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64     rc = findInod
116cd 65 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65  eInfo(pNew, &pNe
116ce 77 2d 3e 70 49 6e 6f 64 65 29 3b 0d 0a 20 20 20  w->pInode);..   
116cf 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
116d0 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
116d1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e   sqlite3_free(pN
116d2 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
116d3 78 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 6f  xt);..        ro
116d4 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c  bust_close(pNew,
116d5 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a   h, __LINE__);..
116d6 20 20 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0d          h = -1;.
116d7 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
116d8 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
116d9 3b 20 20 20 20 20 20 20 20 0d 0a 20 20 20 20 7d  ;        ..    }
116da 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
116db 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63  .  else if( pLoc
116dc 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f  kingStyle == &do
116dd 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29  tlockIoMethods )
116de 7b 0d 0a 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c  {..    /* Dotfil
116df 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74  e locking uses t
116e0 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20  he file path so 
116e1 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  it needs to be i
116e2 6e 63 6c 75 64 65 64 20 69 6e 0d 0a 20 20 20 20  ncluded in..    
116e3 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f  ** the dotlockLo
116e4 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0d 0a 20  ckingContext .. 
116e5 20 20 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20     */..    char 
116e6 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0d 0a 20 20 20  *zLockFile;..   
116e7 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0d   int nFilename;.
116e8 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 46 69  .    assert( zFi
116e9 6c 65 6e 61 6d 65 21 3d 30 20 29 3b 0d 0a 20 20  lename!=0 );..  
116ea 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69    nFilename = (i
116eb 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e  nt)strlen(zFilen
116ec 61 6d 65 29 20 2b 20 36 3b 0d 0a 20 20 20 20 7a  ame) + 6;..    z
116ed 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72  LockFile = (char
116ee 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
116ef 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20  c(nFilename);.. 
116f0 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c 65     if( zLockFile
116f1 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  ==0 ){..      rc
116f2 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
116f3 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
116f4 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
116f5 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20  intf(nFilename, 
116f6 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20  zLockFile, "%s" 
116f7 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20  DOTLOCK_SUFFIX, 
116f8 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 20  zFilename);..   
116f9 20 7d 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f   }..    pNew->lo
116fa 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a  ckingContext = z
116fb 4c 6f 63 6b 46 69 6c 65 3b 0d 0a 20 20 7d 0d 0a  LockFile;..  }..
116fc 0d 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ..#if OS_VXWORKS
116fd 0d 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f  ..  else if( pLo
116fe 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73  ckingStyle == &s
116ff 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0d 0a  emIoMethods ){..
11700 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 6d      /* Named sem
11701 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 75  aphore locking u
11702 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74  ses the file pat
11703 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  h so it needs to
11704 20 62 65 0d 0a 20 20 20 20 2a 2a 20 69 6e 63 6c   be..    ** incl
11705 75 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c  uded in the semL
11706 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0d 0a 20  ockingContext.. 
11707 20 20 20 2a 2f 0d 0a 20 20 20 20 75 6e 69 78 45     */..    unixE
11708 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20  nterMutex();..  
11709 20 20 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64 65    rc = findInode
1170a 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77  Info(pNew, &pNew
1170b 2d 3e 70 49 6e 6f 64 65 29 3b 0d 0a 20 20 20 20  ->pInode);..    
1170c 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  if( (rc==SQLITE_
1170d 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 49  OK) && (pNew->pI
1170e 6e 6f 64 65 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c  node->pSem==NULL
1170f 29 20 29 7b 0d 0a 20 20 20 20 20 20 63 68 61 72  ) ){..      char
11710 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65   *zSemName = pNe
11711 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53 65 6d 4e  w->pInode->aSemN
11712 61 6d 65 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20  ame;..      int 
11713 6e 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n;..      sqlite
11714 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50  3_snprintf(MAX_P
11715 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d  ATHNAME, zSemNam
11716 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c 0d 0a 20  e, "/%s.sem",.. 
11717 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11718 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d        pNew->pId-
11719 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29  >zCanonicalName)
1171a 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d  ;..      for( n=
1171b 31 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20  1; zSemName[n]; 
1171c 6e 2b 2b 20 29 0d 0a 20 20 20 20 20 20 20 20 69  n++ )..        i
1171d 66 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d  f( zSemName[n]==
1171e 27 2f 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e  '/' ) zSemName[n
1171f 5d 20 3d 20 27 5f 27 3b 0d 0a 20 20 20 20 20 20  ] = '_';..      
11720 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53  pNew->pInode->pS
11721 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 6e 28 7a 53  em = sem_open(zS
11722 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 45 41 54 2c  emName, O_CREAT,
11723 20 30 36 36 36 2c 20 31 29 3b 0d 0a 20 20 20 20   0666, 1);..    
11724 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 6f    if( pNew->pIno
11725 64 65 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 4d 5f  de->pSem == SEM_
11726 46 41 49 4c 45 44 20 29 7b 0d 0a 20 20 20 20 20  FAILED ){..     
11727 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
11728 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 20 20 70  OMEM;..        p
11729 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53 65  New->pInode->aSe
1172a 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b  mName[0] = '\0';
1172b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
1172c 0d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  ..    unixLeaveM
1172d 75 74 65 78 28 29 3b 0d 0a 20 20 7d 0d 0a 23 65  utex();..  }..#e
1172e 6e 64 69 66 0d 0a 20 20 0d 0a 20 20 70 4e 65 77  ndif..  ..  pNew
1172f 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b  ->lastErrno = 0;
11730 0d 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ..#if OS_VXWORKS
11731 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
11732 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 66  TE_OK ){..    if
11733 28 20 68 3e 3d 30 20 29 20 72 6f 62 75 73 74 5f  ( h>=0 ) robust_
11734 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c 20 5f  close(pNew, h, _
11735 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20 68  _LINE__);..    h
11736 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 6f 73 55 6e   = -1;..    osUn
11737 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  link(zFilename);
11738 0d 0a 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d  ..    isDelete =
11739 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   0;..  }..  if( 
1173a 69 73 44 65 6c 65 74 65 20 29 20 70 4e 65 77 2d  isDelete ) pNew-
1173b 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e  >ctrlFlags |= UN
1173c 49 58 46 49 4c 45 5f 44 45 4c 45 54 45 3b 0d 0a  IXFILE_DELETE;..
1173d 23 65 6e 64 69 66 0d 0a 20 20 69 66 28 20 72 63  #endif..  if( rc
1173e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
1173f 20 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20 72      if( h>=0 ) r
11740 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77  obust_close(pNew
11741 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d  , h, __LINE__);.
11742 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
11743 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70  New->pMethod = p
11744 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0d 0a 20  LockingStyle;.. 
11745 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b     OpenCounter(+
11746 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  1);..  }..  retu
11747 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
11748 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
11749 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f  ame of a directo
1174a 72 79 20 69 6e 20 77 68 69 63 68 20 74 6f 20 70  ry in which to p
1174b 75 74 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ut temporary fil
1174c 65 73 2e 0d 0a 2a 2a 20 49 66 20 6e 6f 20 73 75  es...** If no su
1174d 69 74 61 62 6c 65 20 74 65 6d 70 6f 72 61 72 79  itable temporary
1174e 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 20   file directory 
1174f 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 72 65  can be found, re
11750 74 75 72 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  turn NULL...*/..
11751 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
11752 72 20 2a 75 6e 69 78 54 65 6d 70 46 69 6c 65 44  r *unixTempFileD
11753 69 72 28 76 6f 69 64 29 7b 0d 0a 20 20 73 74 61  ir(void){..  sta
11754 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
11755 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0d 0a 20 20  azDirs[] = {..  
11756 20 20 20 30 2c 0d 0a 20 20 20 20 20 30 2c 0d 0a     0,..     0,..
11757 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c       "/var/tmp",
11758 0d 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70  ..     "/usr/tmp
11759 22 2c 0d 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c  ",..     "/tmp",
1175a 0d 0a 20 20 20 20 20 30 20 20 20 20 20 20 20 20  ..     0        
1175b 2f 2a 20 4c 69 73 74 20 74 65 72 6d 69 6e 61 74  /* List terminat
1175c 6f 72 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 20 20 75  or */..  };..  u
1175d 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0d 0a  nsigned int i;..
1175e 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75    struct stat bu
1175f 66 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  f;..  const char
11760 20 2a 7a 44 69 72 20 3d 20 30 3b 0d 0a 0d 0a 20   *zDir = 0;.... 
11761 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c   azDirs[0] = sql
11762 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
11763 6f 72 79 3b 0d 0a 20 20 69 66 28 20 21 61 7a 44  ory;..  if( !azD
11764 69 72 73 5b 31 5d 20 29 20 61 7a 44 69 72 73 5b  irs[1] ) azDirs[
11765 31 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50  1] = getenv("TMP
11766 44 49 52 22 29 3b 0d 0a 20 20 66 6f 72 28 69 3d  DIR");..  for(i=
11767 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69  0; i<sizeof(azDi
11768 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72  rs)/sizeof(azDir
11769 73 5b 30 5d 29 3b 20 7a 44 69 72 3d 61 7a 44 69  s[0]); zDir=azDi
1176a 72 73 5b 69 2b 2b 5d 29 7b 0d 0a 20 20 20 20 69  rs[i++]){..    i
1176b 66 28 20 7a 44 69 72 3d 3d 30 20 29 20 63 6f 6e  f( zDir==0 ) con
1176c 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28 20  tinue;..    if( 
1176d 6f 73 53 74 61 74 28 7a 44 69 72 2c 20 26 62 75  osStat(zDir, &bu
1176e 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  f) ) continue;..
1176f 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 52      if( !S_ISDIR
11770 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20  (buf.st_mode) ) 
11771 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69  continue;..    i
11772 66 28 20 6f 73 41 63 63 65 73 73 28 7a 44 69 72  f( osAccess(zDir
11773 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65  , 07) ) continue
11774 3b 0d 0a 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  ;..    break;.. 
11775 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 7a 44 69   }..  return zDi
11776 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  r;..}..../*..** 
11777 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61  Create a tempora
11778 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20  ry file name in 
11779 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74  zBuf.  zBuf must
1177a 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 2a   be allocated..*
1177b 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  * by the calling
1177c 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73   process and mus
1177d 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20  t be big enough 
1177e 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74  to hold at least
1177f 0d 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74  ..** pVfs->mxPat
11780 68 6e 61 6d 65 20 62 79 74 65 73 2e 0d 0a 2a 2f  hname bytes...*/
11781 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  ..static int uni
11782 78 47 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74  xGetTempname(int
11783 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
11784 66 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  f){..  static co
11785 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
11786 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0d 0a 20 20  r zChars[] =..  
11787 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d    "abcdefghijklm
11788 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0d 0a  nopqrstuvwxyz"..
11789 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b      "ABCDEFGHIJK
1178a 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22  LMNOPQRSTUVWXYZ"
1178b 0d 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38  ..    "012345678
1178c 39 22 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20  9";..  unsigned 
1178d 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 63 6f 6e  int i, j;..  con
1178e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 3b 0d 0a  st char *zDir;..
1178f 0d 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20  ..  /* It's odd 
11790 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69  to simulate an i
11791 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75  o-error here, bu
11792 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73  t really this is
11793 20 6a 75 73 74 0d 0a 20 20 2a 2a 20 75 73 69 6e   just..  ** usin
11794 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69  g the io-error i
11795 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f  nfrastructure to
11796 20 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74   test that SQLit
11797 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0d 0a  e handles this..
11798 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61    ** function fa
11799 69 6c 69 6e 67 2e 20 0d 0a 20 20 2a 2f 0d 0a 20  iling. ..  */.. 
1179a 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
1179b 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
1179c 49 4f 45 52 52 20 29 3b 0d 0a 0d 0a 20 20 7a 44  IOERR );....  zD
1179d 69 72 20 3d 20 75 6e 69 78 54 65 6d 70 46 69 6c  ir = unixTempFil
1179e 65 44 69 72 28 29 3b 0d 0a 20 20 69 66 28 20 7a  eDir();..  if( z
1179f 44 69 72 3d 3d 30 20 29 20 7a 44 69 72 20 3d 20  Dir==0 ) zDir = 
117a0 22 2e 22 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65  ".";....  /* Che
117a1 63 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74 70  ck that the outp
117a2 75 74 20 62 75 66 66 65 72 20 69 73 20 6c 61 72  ut buffer is lar
117a3 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68  ge enough for th
117a4 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  e temporary file
117a5 20 0d 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66   ..  ** name. If
117a6 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75   it is not, retu
117a7 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
117a8 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 73  ..  */..  if( (s
117a9 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74  trlen(zDir) + st
117aa 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50  rlen(SQLITE_TEMP
117ab 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20  _FILE_PREFIX) + 
117ac 31 38 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 6e  18) >= (size_t)n
117ad 42 75 66 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  Buf ){..    retu
117ae 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
117af 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 64 6f 7b 0d 0a  ..  }....  do{..
117b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
117b1 69 6e 74 66 28 6e 42 75 66 2d 31 38 2c 20 7a 42  intf(nBuf-18, zB
117b2 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 45 5f  uf, "%s/"SQLITE_
117b3 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
117b4 2c 20 7a 44 69 72 29 3b 0d 0a 20 20 20 20 6a 20  , zDir);..    j 
117b5 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 42  = (int)strlen(zB
117b6 75 66 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  uf);..    sqlite
117b7 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c  3_randomness(15,
117b8 20 26 7a 42 75 66 5b 6a 5d 29 3b 0d 0a 20 20 20   &zBuf[j]);..   
117b9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20   for(i=0; i<15; 
117ba 69 2b 2b 2c 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20  i++, j++){..    
117bb 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61    zBuf[j] = (cha
117bc 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69  r)zChars[ ((unsi
117bd 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a  gned char)zBuf[j
117be 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72  ])%(sizeof(zChar
117bf 73 29 2d 31 29 20 5d 3b 0d 0a 20 20 20 20 7d 0d  s)-1) ];..    }.
117c0 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30  .    zBuf[j] = 0
117c1 3b 0d 0a 20 20 20 20 7a 42 75 66 5b 6a 2b 31 5d  ;..    zBuf[j+1]
117c2 20 3d 20 30 3b 0d 0a 20 20 7d 77 68 69 6c 65 28   = 0;..  }while(
117c3 20 6f 73 41 63 63 65 73 73 28 7a 42 75 66 2c 30   osAccess(zBuf,0
117c4 29 3d 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72  )==0 );..  retur
117c5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
117c6 0a 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  ...#if SQLITE_EN
117c7 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
117c8 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
117c9 41 50 50 4c 45 5f 5f 29 0d 0a 2f 2a 0d 0a 2a 2a  APPLE__)../*..**
117ca 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e   Routine to tran
117cb 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65  sform a unixFile
117cc 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f   into a proxy-lo
117cd 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0d  cking unixFile..
117ce 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
117cf 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d  on in the proxy-
117d0 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62  lock division, b
117d1 75 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f  ut used by unixO
117d2 70 65 6e 28 29 0d 0a 2a 2a 20 69 66 20 53 51 4c  pen()..** if SQL
117d3 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59  ITE_PREFER_PROXY
117d4 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69  _LOCKING is defi
117d5 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ned...*/..static
117d6 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66   int proxyTransf
117d7 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78  ormUnixFile(unix
117d8 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  File*, const cha
117d9 72 2a 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  r*);..#endif....
117da 2f 2a 0d 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f  /*..** Search fo
117db 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65  r an unused file
117dc 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74   descriptor that
117dd 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74   was opened on t
117de 68 65 20 64 61 74 61 62 61 73 65 20 0d 0a 2a 2a  he database ..**
117df 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75   file (not a jou
117e0 72 6e 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a  rnal or master-j
117e1 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65  ournal file) ide
117e2 6e 74 69 66 69 65 64 20 62 79 20 70 61 74 68 6e  ntified by pathn
117e3 61 6d 65 0d 0a 2a 2a 20 7a 50 61 74 68 20 77 69  ame..** zPath wi
117e4 74 68 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58  th SQLITE_OPEN_X
117e5 58 58 20 66 6c 61 67 73 20 6d 61 74 63 68 69 6e  XX flags matchin
117e6 67 20 74 68 6f 73 65 20 70 61 73 73 65 64 20 61  g those passed a
117e7 73 20 74 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a  s the second..**
117e8 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
117e9 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d  s function...**.
117ea 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c 65 20  .** Such a file 
117eb 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20 65  descriptor may e
117ec 78 69 73 74 20 69 66 20 61 20 64 61 74 61 62 61  xist if a databa
117ed 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 61  se connection wa
117ee 73 20 63 6c 6f 73 65 64 0d 0a 2a 2a 20 62 75 74  s closed..** but
117ef 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   the associated 
117f0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
117f1 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f  could not be clo
117f2 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d 65  sed because some
117f3 0d 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20  ..** other file 
117f4 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20  descriptor open 
117f5 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  on the same file
117f6 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69   is holding a fi
117f7 6c 65 2d 6c 6f 63 6b 2e 0d 0a 2a 2a 20 52 65 66  le-lock...** Ref
117f8 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69  er to comments i
117f9 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28  n the unixClose(
117fa 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74  ) function and t
117fb 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65  he lengthy comme
117fc 6e 74 0d 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e  nt..** describin
117fd 67 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72  g "Posix Advisor
117fe 79 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68  y Locking" at th
117ff 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20  e start of this 
11800 66 69 6c 65 20 66 6f 72 20 0d 0a 2a 2a 20 66 75  file for ..** fu
11801 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41  rther details. A
11802 6c 73 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31  lso, ticket #401
11803 38 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20  8...**..** If a 
11804 73 75 69 74 61 62 6c 65 20 66 69 6c 65 20 64 65  suitable file de
11805 73 63 72 69 70 74 6f 72 20 69 73 20 66 6f 75 6e  scriptor is foun
11806 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 72 65  d, then it is re
11807 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 0d 0a 2a  turned. If no..*
11808 2a 20 73 75 63 68 20 66 69 6c 65 20 64 65 73 63  * such file desc
11809 72 69 70 74 6f 72 20 69 73 20 6c 6f 63 61 74 65  riptor is locate
1180a 64 2c 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65  d, -1 is returne
1180b 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 55  d...*/..static U
1180c 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66 69 6e  nixUnusedFd *fin
1180d 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f 6e 73  dReusableFd(cons
1180e 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69  t char *zPath, i
1180f 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 20 20 55 6e  nt flags){..  Un
11810 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75  ixUnusedFd *pUnu
11811 73 65 64 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a  sed = 0;....  /*
11812 20 44 6f 20 6e 6f 74 20 73 65 61 72 63 68 20 66   Do not search f
11813 6f 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c  or an unused fil
11814 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20  e descriptor on 
11815 76 78 77 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63  vxworks. Not bec
11816 61 75 73 65 0d 0a 20 20 2a 2a 20 76 78 77 6f 72  ause..  ** vxwor
11817 6b 73 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 6e  ks would not ben
11818 65 66 69 74 20 66 72 6f 6d 20 74 68 65 20 63 68  efit from the ch
11819 61 6e 67 65 20 28 69 74 20 6d 69 67 68 74 2c 20  ange (it might, 
1181a 77 65 27 72 65 20 6e 6f 74 20 73 75 72 65 29 2c  we're not sure),
1181b 0d 0a 20 20 2a 2a 20 62 75 74 20 62 65 63 61 75  ..  ** but becau
1181c 73 65 20 6e 6f 20 77 61 79 20 74 6f 20 74 65 73  se no way to tes
1181d 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
1181e 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 74 20  y available. It 
1181f 69 73 20 62 65 74 74 65 72 20 0d 0a 20 20 2a 2a  is better ..  **
11820 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62 72 65   not to risk bre
11821 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20 73 75  aking vxworks su
11822 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20 73 61  pport for the sa
11823 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20 6f 62  ke of such an ob
11824 73 63 75 72 65 20 0d 0a 20 20 2a 2a 20 66 65 61  scure ..  ** fea
11825 74 75 72 65 2e 20 20 2a 2f 0d 0a 23 69 66 20 21  ture.  */..#if !
11826 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 73 74  OS_VXWORKS..  st
11827 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b  ruct stat sStat;
11828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11829 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66     /* Results of
1182a 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0d   stat() call */.
1182b 0a 0d 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29  ...  /* A stat()
1182c 20 63 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66   call may fail f
1182d 6f 72 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f  or various reaso
1182e 6e 73 2e 20 49 66 20 74 68 69 73 20 68 61 70 70  ns. If this happ
1182f 65 6e 73 2c 20 69 74 20 69 73 0d 0a 20 20 2a 2a  ens, it is..  **
11830 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20   almost certain 
11831 74 68 61 74 20 61 6e 20 6f 70 65 6e 28 29 20 63  that an open() c
11832 61 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  all on the same 
11833 70 61 74 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66  path will also f
11834 61 69 6c 2e 0d 0a 20 20 2a 2a 20 46 6f 72 20 74  ail...  ** For t
11835 68 69 73 20 72 65 61 73 6f 6e 2c 20 69 66 20 61  his reason, if a
11836 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69  n error occurs i
11837 6e 20 74 68 65 20 73 74 61 74 28 29 20 63 61 6c  n the stat() cal
11838 6c 20 68 65 72 65 2c 20 69 74 20 69 73 0d 0a 20  l here, it is.. 
11839 20 2a 2a 20 69 67 6e 6f 72 65 64 20 61 6e 64 20   ** ignored and 
1183a 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  -1 is returned. 
1183b 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  The caller will 
1183c 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 6e 65  try to open a ne
1183d 77 20 66 69 6c 65 0d 0a 20 20 2a 2a 20 64 65 73  w file..  ** des
1183e 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 73  criptor on the s
1183f 61 6d 65 20 70 61 74 68 2c 20 66 61 69 6c 2c 20  ame path, fail, 
11840 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
11841 72 6f 72 20 74 6f 20 53 51 4c 69 74 65 2e 0d 0a  ror to SQLite...
11842 20 20 2a 2a 0d 0a 20 20 2a 2a 20 45 76 65 6e 20    **..  ** Even 
11843 69 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20  if a subsequent 
11844 6f 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65 73  open() call does
11845 20 73 75 63 63 65 65 64 2c 20 74 68 65 20 63 6f   succeed, the co
11846 6e 73 65 71 75 65 6e 63 65 73 20 6f 66 0d 0a 20  nsequences of.. 
11847 20 2a 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e   ** not searchin
11848 67 20 66 6f 72 20 61 20 72 65 73 75 73 61 62 6c  g for a resusabl
11849 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
1184a 72 20 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20  r are not dire. 
1184b 20 2a 2f 0d 0a 20 20 69 66 28 20 30 3d 3d 6f 73   */..  if( 0==os
1184c 53 74 61 74 28 7a 50 61 74 68 2c 20 26 73 53 74  Stat(zPath, &sSt
1184d 61 74 29 20 29 7b 0d 0a 20 20 20 20 75 6e 69 78  at) ){..    unix
1184e 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64  InodeInfo *pInod
1184f 65 3b 0d 0a 0d 0a 20 20 20 20 75 6e 69 78 45 6e  e;....    unixEn
11850 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 20  terMutex();..   
11851 20 70 49 6e 6f 64 65 20 3d 20 69 6e 6f 64 65 4c   pInode = inodeL
11852 69 73 74 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  ist;..    while(
11853 20 70 49 6e 6f 64 65 20 26 26 20 28 70 49 6e 6f   pInode && (pIno
11854 64 65 2d 3e 66 69 6c 65 49 64 2e 64 65 76 21 3d  de->fileId.dev!=
11855 73 53 74 61 74 2e 73 74 5f 64 65 76 0d 0a 20 20  sStat.st_dev..  
11856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11857 20 20 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 66 69     || pInode->fi
11858 6c 65 49 64 2e 69 6e 6f 21 3d 73 53 74 61 74 2e  leId.ino!=sStat.
11859 73 74 5f 69 6e 6f 29 20 29 7b 0d 0a 20 20 20 20  st_ino) ){..    
1185a 20 20 20 70 49 6e 6f 64 65 20 3d 20 70 49 6e 6f     pInode = pIno
1185b 64 65 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  de->pNext;..    
1185c 7d 0d 0a 20 20 20 20 69 66 28 20 70 49 6e 6f 64  }..    if( pInod
1185d 65 20 29 7b 0d 0a 20 20 20 20 20 20 55 6e 69 78  e ){..      Unix
1185e 55 6e 75 73 65 64 46 64 20 2a 2a 70 70 3b 0d 0a  UnusedFd **pp;..
1185f 20 20 20 20 20 20 66 6f 72 28 70 70 3d 26 70 49        for(pp=&pI
11860 6e 6f 64 65 2d 3e 70 55 6e 75 73 65 64 3b 20 2a  node->pUnused; *
11861 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c 61  pp && (*pp)->fla
11862 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 28  gs!=flags; pp=&(
11863 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0d  (*pp)->pNext));.
11864 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d  .      pUnused =
11865 20 2a 70 70 3b 0d 0a 20 20 20 20 20 20 69 66 28   *pp;..      if(
11866 20 70 55 6e 75 73 65 64 20 29 7b 0d 0a 20 20 20   pUnused ){..   
11867 20 20 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73       *pp = pUnus
11868 65 64 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  ed->pNext;..    
11869 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
1186a 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
1186b 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20 20  ;..  }..#endif  
1186c 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 57 4f    /* if !OS_VXWO
1186d 52 4b 53 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e  RKS */..  return
1186e 20 70 55 6e 75 73 65 64 3b 0d 0a 7d 0d 0a 0d 0a   pUnused;..}....
1186f 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
11870 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
11871 79 20 75 6e 69 78 4f 70 65 6e 28 29 20 74 6f 20  y unixOpen() to 
11872 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 75 6e  determine the un
11873 69 78 20 70 65 72 6d 69 73 73 69 6f 6e 73 0d 0a  ix permissions..
11874 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 6e 65 77  ** to create new
11875 20 66 69 6c 65 73 20 77 69 74 68 2e 20 49 66 20   files with. If 
11876 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  no error occurs,
11877 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20   then SQLITE_OK 
11878 69 73 20 72 65 74 75 72 6e 65 64 0d 0a 2a 2a 20  is returned..** 
11879 61 6e 64 20 61 20 76 61 6c 75 65 20 73 75 69 74  and a value suit
1187a 61 62 6c 65 20 66 6f 72 20 70 61 73 73 69 6e 67  able for passing
1187b 20 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72   as the third ar
1187c 67 75 6d 65 6e 74 20 74 6f 20 6f 70 65 6e 28 32  gument to open(2
1187d 29 20 69 73 0d 0a 2a 2a 20 77 72 69 74 74 65 6e  ) is..** written
1187e 20 74 6f 20 2a 70 4d 6f 64 65 2e 20 49 66 20 61   to *pMode. If a
1187f 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
11880 73 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  s, an SQLite err
11881 6f 72 20 63 6f 64 65 20 69 73 20 0d 0a 2a 2a 20  or code is ..** 
11882 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
11883 20 76 61 6c 75 65 20 6f 66 20 2a 70 4d 6f 64 65   value of *pMode
11884 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64   is not modified
11885 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
11886 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e   file being open
11887 65 64 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72  ed is a temporar
11888 79 20 66 69 6c 65 2c 20 69 74 20 69 73 20 61 6c  y file, it is al
11889 77 61 79 73 20 63 72 65 61 74 65 64 20 77 69 74  ways created wit
1188a 68 0d 0a 2a 2a 20 74 68 65 20 6f 63 74 61 6c 20  h..** the octal 
1188b 70 65 72 6d 69 73 73 69 6f 6e 73 20 30 36 30 30  permissions 0600
1188c 20 28 72 65 61 64 2f 77 72 69 74 61 62 6c 65 20   (read/writable 
1188d 62 79 20 6f 77 6e 65 72 20 6f 6e 6c 79 29 2e 20  by owner only). 
1188e 49 66 20 74 68 65 20 66 69 6c 65 0d 0a 2a 2a 20  If the file..** 
1188f 69 73 20 61 20 64 61 74 61 62 61 73 65 20 6f 72  is a database or
11890 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11891 66 69 6c 65 2c 20 69 74 20 69 73 20 63 72 65 61  file, it is crea
11892 74 65 64 20 77 69 74 68 20 74 68 65 20 70 65 72  ted with the per
11893 6d 69 73 73 69 6f 6e 73 20 0d 0a 2a 2a 20 6d 61  missions ..** ma
11894 73 6b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  sk SQLITE_DEFAUL
11895 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f  T_FILE_PERMISSIO
11896 4e 53 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 69 6e 61  NS...**..** Fina
11897 6c 6c 79 2c 20 69 66 20 74 68 65 20 66 69 6c 65  lly, if the file
11898 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 69 73   being opened is
11899 20 61 20 57 41 4c 20 6f 72 20 72 65 67 75 6c 61   a WAL or regula
1189a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  r journal file, 
1189b 74 68 65 6e 20 0d 0a 2a 2a 20 74 68 69 73 20 66  then ..** this f
1189c 75 6e 63 74 69 6f 6e 20 71 75 65 72 69 65 73 20  unction queries 
1189d 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
1189e 66 6f 72 20 74 68 65 20 70 65 72 6d 69 73 73 69  for the permissi
1189f 6f 6e 73 20 6f 6e 20 74 68 65 20 0d 0a 2a 2a 20  ons on the ..** 
118a0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61  corresponding da
118a1 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
118a2 73 65 74 73 20 2a 70 4d 6f 64 65 20 74 6f 20 74  sets *pMode to t
118a3 68 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e 65  his value. Whene
118a4 76 65 72 20 0d 0a 2a 2a 20 70 6f 73 73 69 62 6c  ver ..** possibl
118a5 65 2c 20 57 41 4c 20 61 6e 64 20 6a 6f 75 72 6e  e, WAL and journ
118a6 61 6c 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  al files are cre
118a7 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 73  ated using the s
118a8 61 6d 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  ame permissions 
118a9 0d 0a 2a 2a 20 61 73 20 74 68 65 20 61 73 73 6f  ..** as the asso
118aa 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20  ciated database 
118ab 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  file...**..** If
118ac 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42   the SQLITE_ENAB
118ad 4c 45 5f 38 5f 33 5f 4e 41 4d 45 53 20 6f 70 74  LE_8_3_NAMES opt
118ae 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ion is enabled, 
118af 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 6f 72 69  then the..** ori
118b0 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69  ginal filename i
118b1 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 20  s unavailable.  
118b2 42 75 74 20 38 5f 33 5f 4e 41 4d 45 53 20 69 73  But 8_3_NAMES is
118b3 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 0d 0a   only used for..
118b4 2a 2a 20 46 41 54 20 66 69 6c 65 73 79 73 74 65  ** FAT filesyste
118b5 6d 73 20 61 6e 64 20 70 65 72 6d 69 73 73 69 6f  ms and permissio
118b6 6e 73 20 64 6f 20 6e 6f 74 20 6d 61 74 74 65 72  ns do not matter
118b7 20 74 68 65 72 65 2c 20 73 6f 20 6a 75 73 74 20   there, so just 
118b8 75 73 65 0d 0a 2a 2a 20 74 68 65 20 64 65 66 61  use..** the defa
118b9 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 2e  ult permissions.
118ba 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
118bb 20 66 69 6e 64 43 72 65 61 74 65 46 69 6c 65 4d   findCreateFileM
118bc 6f 64 65 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68  ode(..  const ch
118bd 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
118be 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20          /* Path 
118bf 6f 66 20 66 69 6c 65 20 28 70 6f 73 73 69 62 6c  of file (possibl
118c0 79 29 20 62 65 69 6e 67 20 63 72 65 61 74 65 64  y) being created
118c1 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73   */..  int flags
118c2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
118c3 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
118c4 70 61 73 73 65 64 20 61 73 20 34 74 68 20 61 72  passed as 4th ar
118c5 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28  gument to xOpen(
118c6 29 20 2a 2f 0d 0a 20 20 6d 6f 64 65 5f 74 20 2a  ) */..  mode_t *
118c7 70 4d 6f 64 65 20 20 20 20 20 20 20 20 20 20 20  pMode           
118c8 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
118c9 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 6f  Permissions to o
118ca 70 65 6e 20 66 69 6c 65 20 77 69 74 68 20 2a 2f  pen file with */
118cb 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  ..){..  int rc =
118cc 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
118cd 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
118ce 6e 20 43 6f 64 65 20 2a 2f 0d 0a 20 20 2a 70 4d  n Code */..  *pM
118cf 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  ode = SQLITE_DEF
118d0 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53  AULT_FILE_PERMIS
118d1 53 49 4f 4e 53 3b 0d 0a 20 20 69 66 28 20 66 6c  SIONS;..  if( fl
118d2 61 67 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50  ags & (SQLITE_OP
118d3 45 4e 5f 57 41 4c 7c 53 51 4c 49 54 45 5f 4f 50  EN_WAL|SQLITE_OP
118d4 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
118d5 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 7a 44   ){..    char zD
118d6 62 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31  b[MAX_PATHNAME+1
118d7 5d 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61  ];     /* Databa
118d8 73 65 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0d  se file path */.
118d9 0a 20 20 20 20 69 6e 74 20 6e 44 62 3b 20 20 20  .    int nDb;   
118da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118db 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
118dc 76 61 6c 69 64 20 62 79 74 65 73 20 69 6e 20 7a  valid bytes in z
118dd 44 62 20 2a 2f 0d 0a 20 20 20 20 73 74 72 75 63  Db */..    struc
118de 74 20 73 74 61 74 20 73 53 74 61 74 3b 20 20 20  t stat sStat;   
118df 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
118e0 75 74 20 6f 66 20 73 74 61 74 28 29 20 6f 6e 20  ut of stat() on 
118e1 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
118e2 0d 0a 0d 0a 20 20 20 20 2f 2a 20 7a 50 61 74 68  ....    /* zPath
118e3 20 69 73 20 61 20 70 61 74 68 20 74 6f 20 61 20   is a path to a 
118e4 57 41 4c 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66  WAL or journal f
118e5 69 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ile. The followi
118e6 6e 67 20 62 6c 6f 63 6b 20 64 65 72 69 76 65 73  ng block derives
118e7 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 61 74  ..    ** the pat
118e8 68 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61  h to the associa
118e9 74 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c  ted database fil
118ea 65 20 66 72 6f 6d 20 7a 50 61 74 68 2e 20 54 68  e from zPath. Th
118eb 69 73 20 62 6c 6f 63 6b 20 68 61 6e 64 6c 65 73  is block handles
118ec 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c  ..    ** the fol
118ed 6c 6f 77 69 6e 67 20 6e 61 6d 69 6e 67 20 63 6f  lowing naming co
118ee 6e 76 65 6e 74 69 6f 6e 73 3a 0d 0a 20 20 20 20  nventions:..    
118ef 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 22 3c 70  **..    **   "<p
118f0 61 74 68 20 74 6f 20 64 62 3e 2d 6a 6f 75 72 6e  ath to db>-journ
118f1 61 6c 22 0d 0a 20 20 20 20 2a 2a 20 20 20 22 3c  al"..    **   "<
118f2 70 61 74 68 20 74 6f 20 64 62 3e 2d 77 61 6c 22  path to db>-wal"
118f3 0d 0a 20 20 20 20 2a 2a 20 20 20 22 3c 70 61 74  ..    **   "<pat
118f4 68 20 74 6f 20 64 62 3e 2d 6a 6f 75 72 6e 61 6c  h to db>-journal
118f5 4e 4e 22 0d 0a 20 20 20 20 2a 2a 20 20 20 22 3c  NN"..    **   "<
118f6 70 61 74 68 20 74 6f 20 64 62 3e 2d 77 61 6c 4e  path to db>-walN
118f7 4e 22 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  N"..    **..    
118f8 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20 61  ** where NN is a
118f9 20 64 65 63 69 6d 61 6c 20 6e 75 6d 62 65 72 2e   decimal number.
118fa 20 54 68 65 20 4e 4e 20 6e 61 6d 69 6e 67 20 73   The NN naming s
118fb 63 68 65 6d 65 73 20 61 72 65 20 0d 0a 20 20 20  chemes are ..   
118fc 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20   ** used by the 
118fd 74 65 73 74 5f 6d 75 6c 74 69 70 6c 65 78 2e 63  test_multiplex.c
118fe 20 6d 6f 64 75 6c 65 2e 0d 0a 20 20 20 20 2a 2f   module...    */
118ff 0d 0a 20 20 20 20 6e 44 62 20 3d 20 73 71 6c 69  ..    nDb = sqli
11900 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74  te3Strlen30(zPat
11901 68 29 20 2d 20 31 3b 20 0d 0a 23 69 66 64 65 66  h) - 1; ..#ifdef
11902 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 38   SQLITE_ENABLE_8
11903 5f 33 5f 4e 41 4d 45 53 0d 0a 20 20 20 20 77 68  _3_NAMES..    wh
11904 69 6c 65 28 20 6e 44 62 3e 30 20 26 26 20 73 71  ile( nDb>0 && sq
11905 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 50 61  lite3Isalnum(zPa
11906 74 68 5b 6e 44 62 5d 29 20 29 20 6e 44 62 2d 2d  th[nDb]) ) nDb--
11907 3b 0d 0a 20 20 20 20 69 66 28 20 6e 44 62 3d 3d  ;..    if( nDb==
11908 30 20 7c 7c 20 7a 50 61 74 68 5b 6e 44 62 5d 21  0 || zPath[nDb]!
11909 3d 27 2d 27 20 29 20 72 65 74 75 72 6e 20 53 51  ='-' ) return SQ
1190a 4c 49 54 45 5f 4f 4b 3b 0d 0a 23 65 6c 73 65 0d  LITE_OK;..#else.
1190b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 50 61 74  .    while( zPat
1190c 68 5b 6e 44 62 5d 21 3d 27 2d 27 20 29 7b 0d 0a  h[nDb]!='-' ){..
1190d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 44        assert( nD
1190e 62 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  b>0 );..      as
1190f 73 65 72 74 28 20 7a 50 61 74 68 5b 6e 44 62 5d  sert( zPath[nDb]
11910 21 3d 27 5c 6e 27 20 29 3b 0d 0a 20 20 20 20 20  !='\n' );..     
11911 20 6e 44 62 2d 2d 3b 0d 0a 20 20 20 20 7d 0d 0a   nDb--;..    }..
11912 23 65 6e 64 69 66 0d 0a 20 20 20 20 6d 65 6d 63  #endif..    memc
11913 70 79 28 7a 44 62 2c 20 7a 50 61 74 68 2c 20 6e  py(zDb, zPath, n
11914 44 62 29 3b 0d 0a 20 20 20 20 7a 44 62 5b 6e 44  Db);..    zDb[nD
11915 62 5d 20 3d 20 27 5c 30 27 3b 0d 0a 0d 0a 20 20  b] = '\0';....  
11916 20 20 69 66 28 20 30 3d 3d 6f 73 53 74 61 74 28    if( 0==osStat(
11917 7a 44 62 2c 20 26 73 53 74 61 74 29 20 29 7b 0d  zDb, &sStat) ){.
11918 0a 20 20 20 20 20 20 2a 70 4d 6f 64 65 20 3d 20  .      *pMode = 
11919 73 53 74 61 74 2e 73 74 5f 6d 6f 64 65 20 26 20  sStat.st_mode & 
1191a 30 37 37 37 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  0777;..    }else
1191b 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  {..      rc = SQ
1191c 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
1191d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
1191e 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51  e if( flags & SQ
1191f 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
11920 4f 4e 43 4c 4f 53 45 20 29 7b 0d 0a 20 20 20 20  ONCLOSE ){..    
11921 2a 70 4d 6f 64 65 20 3d 20 30 36 30 30 3b 0d 0a  *pMode = 0600;..
11922 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
11923 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f  ;..}..../*..** O
11924 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61  pen the file zPa
11925 74 68 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 50 72 65  th...** ..** Pre
11926 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c  viously, the SQL
11927 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65  ite OS layer use
11928 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e  d three function
11929 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  s in place of th
1192a 69 73 0d 0a 2a 2a 20 6f 6e 65 3a 0d 0a 2a 2a 0d  is..** one:..**.
1192b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
1192c 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 29  sOpenReadWrite()
1192d 3b 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ;..**     sqlite
1192e 33 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28  3OsOpenReadOnly(
1192f 29 3b 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  );..**     sqlit
11930 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76  e3OsOpenExclusiv
11931 65 28 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  e();..**..** The
11932 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 73 70  se calls corresp
11933 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ond to the follo
11934 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  wing combination
11935 73 20 6f 66 20 66 6c 61 67 73 3a 0d 0a 2a 2a 0d  s of flags:..**.
11936 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 74  .**     ReadWrit
11937 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44  e() ->     (READ
11938 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 0d  WRITE | CREATE).
11939 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79  .**     ReadOnly
1193a 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44  ()  ->     (READ
1193b 4f 4e 4c 59 29 20 0d 0a 2a 2a 20 20 20 20 20 4f  ONLY) ..**     O
1193c 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d  penExclusive() -
1193d 3e 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43  > (READWRITE | C
1193e 52 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56  REATE | EXCLUSIV
1193f 45 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6f  E)..**..** The o
11940 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65  ld OpenExclusive
11941 28 29 20 61 63 63 65 70 74 65 64 20 61 20 62 6f  () accepted a bo
11942 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d  olean argument -
11943 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0d 0a   "delFlag". If..
11944 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c  ** true, the fil
11945 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64  e was configured
11946 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63   to be automatic
11947 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65  ally deleted whe
11948 6e 20 74 68 65 0d 0a 2a 2a 20 66 69 6c 65 20 68  n the..** file h
11949 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f  andle closed. To
1194a 20 61 63 68 69 65 76 65 20 74 68 65 20 73 61 6d   achieve the sam
1194b 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 74  e effect using t
1194c 68 69 73 20 6e 65 77 20 0d 0a 2a 2a 20 69 6e 74  his new ..** int
1194d 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 20  erface, add the 
1194e 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c  DELETEONCLOSE fl
1194f 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63  ag to those spec
11950 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20  ified above for 
11951 0d 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69  ..** OpenExclusi
11952 76 65 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ve()...*/..stati
11953 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0d  c int unixOpen(.
11954 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
11955 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
11956 2f 2a 20 54 68 65 20 56 46 53 20 66 6f 72 20 77  /* The VFS for w
11957 68 69 63 68 20 74 68 69 73 20 69 73 20 74 68 65  hich this is the
11958 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f   xOpen method */
11959 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
1195a 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20  zPath,          
1195b 20 2f 2a 20 50 61 74 68 6e 61 6d 65 20 6f 66 20   /* Pathname of 
1195c 66 69 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65  file to be opene
1195d 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  d */..  sqlite3_
1195e 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20  file *pFile,    
1195f 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65       /* The file
11960 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 62   descriptor to b
11961 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0d 0a  e filled in */..
11962 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
11963 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11964 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20 74 6f  * Input flags to
11965 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f 70 65   control the ope
11966 6e 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  ning */..  int *
11967 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20  pOutFlags       
11968 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
11969 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64  t flags returned
1196a 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65 20   to SQLite core 
1196b 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 6e 69 78 46 69  */..){..  unixFi
1196c 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c  le *p = (unixFil
1196d 65 20 2a 29 70 46 69 6c 65 3b 0d 0a 20 20 69 6e  e *)pFile;..  in
1196e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20  t fd = -1;      
1196f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11970 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
11971 72 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e  returned by open
11972 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70 65  () */..  int ope
11973 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20  nFlags = 0;     
11974 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
11975 20 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e   to pass to open
11976 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 54 79  () */..  int eTy
11977 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46  pe = flags&0xFFF
11978 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20  FFF00;  /* Type 
11979 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  of file to open 
1197a 2a 2f 0d 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b  */..  int noLock
1197b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1197c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
1197d 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69  omit locking pri
1197e 6d 69 74 69 76 65 73 20 2a 2f 0d 0a 20 20 69 6e  mitives */..  in
1197f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
11980 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
11981 46 75 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20  Function Return 
11982 43 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 63  Code */..  int c
11983 74 72 6c 46 6c 61 67 73 20 3d 20 30 3b 20 20 20  trlFlags = 0;   
11984 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 4e 49            /* UNI
11985 58 46 49 4c 45 5f 2a 20 66 6c 61 67 73 20 2a 2f  XFILE_* flags */
11986 0d 0a 0d 0a 20 20 69 6e 74 20 69 73 45 78 63 6c  ....  int isExcl
11987 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20  usive  = (flags 
11988 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  & SQLITE_OPEN_EX
11989 43 4c 55 53 49 56 45 29 3b 0d 0a 20 20 69 6e 74  CLUSIVE);..  int
1198a 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20   isDelete     = 
1198b 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
1198c 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
1198d 53 45 29 3b 0d 0a 20 20 69 6e 74 20 69 73 43 72  SE);..  int isCr
1198e 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67  eate     = (flag
1198f 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
11990 43 52 45 41 54 45 29 3b 0d 0a 20 20 69 6e 74 20  CREATE);..  int 
11991 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28  isReadonly   = (
11992 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
11993 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0d 0a  PEN_READONLY);..
11994 20 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 74    int isReadWrit
11995 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51  e  = (flags & SQ
11996 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
11997 49 54 45 29 3b 0d 0a 23 69 66 20 53 51 4c 49 54  ITE);..#if SQLIT
11998 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
11999 5f 53 54 59 4c 45 0d 0a 20 20 69 6e 74 20 69 73  _STYLE..  int is
1199a 41 75 74 6f 50 72 6f 78 79 20 20 3d 20 28 66 6c  AutoProxy  = (fl
1199b 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
1199c 4e 5f 41 55 54 4f 50 52 4f 58 59 29 3b 0d 0a 23  N_AUTOPROXY);..#
1199d 65 6e 64 69 66 0d 0a 23 69 66 20 64 65 66 69 6e  endif..#if defin
1199e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c 7c  ed(__APPLE__) ||
1199f 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
119a0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20  OCKING_STYLE..  
119a1 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73  struct statfs fs
119a2 49 6e 66 6f 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  Info;..#endif...
119a3 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e  .  /* If creatin
119a4 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 6d 61  g a master or ma
119a5 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c  in-file journal,
119a6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
119a7 69 6c 6c 20 6f 70 65 6e 0d 0a 20 20 2a 2a 20 61  ill open..  ** a
119a8 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
119a9 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72   on the director
119aa 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 74  y too. The first
119ab 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 29   time unixSync()
119ac 0d 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ..  ** is called
119ad 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66   the directory f
119ae 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77  ile descriptor w
119af 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64  ill be fsync()ed
119b0 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0d 0a   and close()d...
119b1 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 79 6e 63    */..  int sync
119b2 44 69 72 20 3d 20 28 69 73 43 72 65 61 74 65 20  Dir = (isCreate 
119b3 26 26 20 28 0d 0a 20 20 20 20 20 20 20 20 65 54  && (..        eT
119b4 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
119b5 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
119b6 0d 0a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d  ..     || eType=
119b7 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
119b8 4e 5f 4a 4f 55 52 4e 41 4c 20 0d 0a 20 20 20 20  N_JOURNAL ..    
119b9 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
119ba 45 5f 4f 50 45 4e 5f 57 41 4c 0d 0a 20 20 29 29  E_OPEN_WAL..  ))
119bb 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 72 67  ;....  /* If arg
119bc 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 20 61  ument zPath is a
119bd 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
119be 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
119bf 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 65 6e  required to open
119c0 0d 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61  ..  ** a tempora
119c1 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 69  ry file. Use thi
119c2 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f 72  s buffer to stor
119c3 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 20  e the file name 
119c4 69 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 63 68 61  in...  */..  cha
119c5 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50  r zTmpname[MAX_P
119c6 41 54 48 4e 41 4d 45 2b 32 5d 3b 0d 0a 20 20 63  ATHNAME+2];..  c
119c7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
119c8 20 3d 20 7a 50 61 74 68 3b 0d 0a 0d 0a 20 20 2f   = zPath;....  /
119c9 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f 6c 6c  * Check the foll
119ca 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73  owing statements
119cb 20 61 72 65 20 74 72 75 65 3a 20 0d 0a 20 20 2a   are true: ..  *
119cc 2a 0d 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78  *..  **   (a) Ex
119cd 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65  actly one of the
119ce 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52   READWRITE and R
119cf 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75  EADONLY flags mu
119d0 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0d  st be set, and .
119d1 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43  .  **   (b) if C
119d2 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68  REATE is set, th
119d3 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73  en READWRITE mus
119d4 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61  t also be set, a
119d5 6e 64 0d 0a 20 20 2a 2a 20 20 20 28 63 29 20 69  nd..  **   (c) i
119d6 66 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73  f EXCLUSIVE is s
119d7 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20  et, then CREATE 
119d8 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74  must also be set
119d9 2e 0d 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66  ...  **   (d) if
119da 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69   DELETEONCLOSE i
119db 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41  s set, then CREA
119dc 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20  TE must also be 
119dd 73 65 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73  set...  */..  as
119de 73 65 72 74 28 28 69 73 52 65 61 64 6f 6e 6c 79  sert((isReadonly
119df 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69  ==0 || isReadWri
119e0 74 65 3d 3d 30 29 20 26 26 20 28 69 73 52 65 61  te==0) && (isRea
119e1 64 57 72 69 74 65 20 7c 7c 20 69 73 52 65 61 64  dWrite || isRead
119e2 6f 6e 6c 79 29 29 3b 0d 0a 20 20 61 73 73 65 72  only));..  asser
119e3 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c  t(isCreate==0 ||
119e4 20 69 73 52 65 61 64 57 72 69 74 65 29 3b 0d 0a   isReadWrite);..
119e5 20 20 61 73 73 65 72 74 28 69 73 45 78 63 6c 75    assert(isExclu
119e6 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65  sive==0 || isCre
119e7 61 74 65 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ate);..  assert(
119e8 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69  isDelete==0 || i
119e9 73 43 72 65 61 74 65 29 3b 0d 0a 0d 0a 20 20 2f  sCreate);....  /
119ea 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d  * The main DB, m
119eb 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c  ain journal, WAL
119ec 20 66 69 6c 65 20 61 6e 64 20 6d 61 73 74 65 72   file and master
119ed 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76   journal are nev
119ee 65 72 20 0d 0a 20 20 2a 2a 20 61 75 74 6f 6d 61  er ..  ** automa
119ef 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 2e  tically deleted.
119f0 20 4e 6f 72 20 61 72 65 20 74 68 65 79 20 65 76   Nor are they ev
119f1 65 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  er temporary fil
119f2 65 73 2e 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72  es.  */..  asser
119f3 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26  t( (!isDelete &&
119f4 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65   zName) || eType
119f5 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
119f6 49 4e 5f 44 42 20 29 3b 0d 0a 20 20 61 73 73 65  IN_DB );..  asse
119f7 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26  rt( (!isDelete &
119f8 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70  & zName) || eTyp
119f9 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
119fa 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0d 0a  AIN_JOURNAL );..
119fb 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44 65    assert( (!isDe
119fc 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c  lete && zName) |
119fd 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f  | eType!=SQLITE_
119fe 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
119ff 4e 41 4c 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  NAL );..  assert
11a00 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20  ( (!isDelete && 
11a01 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21  zName) || eType!
11a02 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c  =SQLITE_OPEN_WAL
11a03 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 73 73 65   );....  /* Asse
11a04 72 74 20 74 68 61 74 20 74 68 65 20 75 70 70 65  rt that the uppe
11a05 72 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 20  r layer has set 
11a06 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65  one of the "file
11a07 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f  -type" flags. */
11a08 0d 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  ..  assert( eTyp
11a09 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
11a0a 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65  AIN_DB      || e
11a0b 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
11a0c 4e 5f 54 45 4d 50 5f 44 42 20 0d 0a 20 20 20 20  N_TEMP_DB ..    
11a0d 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
11a0e 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
11a0f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d  URNAL || eType==
11a10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
11a11 5f 4a 4f 55 52 4e 41 4c 20 0d 0a 20 20 20 20 20  _JOURNAL ..     
11a12 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
11a13 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
11a14 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53  AL   || eType==S
11a15 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
11a16 52 5f 4a 4f 55 52 4e 41 4c 20 0d 0a 20 20 20 20  R_JOURNAL ..    
11a17 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
11a18 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45  ITE_OPEN_TRANSIE
11a19 4e 54 5f 44 42 20 7c 7c 20 65 54 79 70 65 3d 3d  NT_DB || eType==
11a1a 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 0d  SQLITE_OPEN_WAL.
11a1b 0a 20 20 29 3b 0d 0a 0d 0a 20 20 6d 65 6d 73 65  .  );....  memse
11a1c 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  t(p, 0, sizeof(u
11a1d 6e 69 78 46 69 6c 65 29 29 3b 0d 0a 0d 0a 20 20  nixFile));....  
11a1e 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  if( eType==SQLIT
11a1f 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29  E_OPEN_MAIN_DB )
11a20 7b 0d 0a 20 20 20 20 55 6e 69 78 55 6e 75 73 65  {..    UnixUnuse
11a21 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 0d 0a 20  dFd *pUnused;.. 
11a22 20 20 20 70 55 6e 75 73 65 64 20 3d 20 66 69 6e     pUnused = fin
11a23 64 52 65 75 73 61 62 6c 65 46 64 28 7a 4e 61 6d  dReusableFd(zNam
11a24 65 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20 20 20  e, flags);..    
11a25 69 66 28 20 70 55 6e 75 73 65 64 20 29 7b 0d 0a  if( pUnused ){..
11a26 20 20 20 20 20 20 66 64 20 3d 20 70 55 6e 75 73        fd = pUnus
11a27 65 64 2d 3e 66 64 3b 0d 0a 20 20 20 20 7d 65 6c  ed->fd;..    }el
11a28 73 65 7b 0d 0a 20 20 20 20 20 20 70 55 6e 75 73  se{..      pUnus
11a29 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ed = sqlite3_mal
11a2a 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75  loc(sizeof(*pUnu
11a2b 73 65 64 29 29 3b 0d 0a 20 20 20 20 20 20 69 66  sed));..      if
11a2c 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0d 0a 20  ( !pUnused ){.. 
11a2d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
11a2e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
11a2f 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
11a30 20 70 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 55   p->pUnused = pU
11a31 6e 75 73 65 64 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  nused;....    /*
11a32 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   Database filena
11a33 6d 65 73 20 61 72 65 20 64 6f 75 62 6c 65 2d 7a  mes are double-z
11a34 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 69  ero terminated i
11a35 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0d 0a  f they are not..
11a36 20 20 20 20 2a 2a 20 55 52 49 73 20 77 69 74 68      ** URIs with
11a37 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 48 65   parameters.  He
11a38 6e 63 65 2c 20 74 68 65 79 20 63 61 6e 20 61 6c  nce, they can al
11a39 77 61 79 73 20 62 65 20 70 61 73 73 65 64 20 69  ways be passed i
11a3a 6e 74 6f 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69  nto..    ** sqli
11a3b 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65  te3_uri_paramete
11a3c 72 28 29 2e 20 2a 2f 0d 0a 20 20 20 20 61 73 73  r(). */..    ass
11a3d 65 72 74 28 20 28 66 6c 61 67 73 20 26 20 53 51  ert( (flags & SQ
11a3e 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 20 7c  LITE_OPEN_URI) |
11a3f 7c 20 7a 4e 61 6d 65 5b 73 74 72 6c 65 6e 28 7a  | zName[strlen(z
11a40 4e 61 6d 65 29 2b 31 5d 3d 3d 30 20 29 3b 0d 0a  Name)+1]==0 );..
11a41 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a  ..  }else if( !z
11a42 4e 61 6d 65 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  Name ){..    /* 
11a43 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c  If zName is NULL
11a44 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65  , the upper laye
11a45 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20  r is requesting 
11a46 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0d  a temp file. */.
11a47 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65  .    assert(isDe
11a48 6c 65 74 65 20 26 26 20 21 73 79 6e 63 44 69 72  lete && !syncDir
11a49 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 75 6e 69  );..    rc = uni
11a4a 78 47 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58  xGetTempname(MAX
11a4b 5f 50 41 54 48 4e 41 4d 45 2b 32 2c 20 7a 54 6d  _PATHNAME+2, zTm
11a4c 70 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28  pname);..    if(
11a4d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11a4e 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
11a4f 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  rc;..    }..    
11a50 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65  zName = zTmpname
11a51 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47 65 6e 65  ;....    /* Gene
11a52 72 61 74 65 64 20 74 65 6d 70 6f 72 61 72 79 20  rated temporary 
11a53 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20 61 6c  filenames are al
11a54 77 61 79 73 20 64 6f 75 62 6c 65 2d 7a 65 72 6f  ways double-zero
11a55 20 74 65 72 6d 69 6e 61 74 65 64 0d 0a 20 20 20   terminated..   
11a56 20 2a 2a 20 66 6f 72 20 75 73 65 20 62 79 20 73   ** for use by s
11a57 71 6c 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d  qlite3_uri_param
11a58 65 74 65 72 28 29 2e 20 2a 2f 0d 0a 20 20 20 20  eter(). */..    
11a59 61 73 73 65 72 74 28 20 7a 4e 61 6d 65 5b 73 74  assert( zName[st
11a5a 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31 5d 3d 3d  rlen(zName)+1]==
11a5b 30 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  0 );..  }....  /
11a5c 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
11a5d 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c 61  value of the fla
11a5e 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 73  gs parameter pas
11a5f 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 6e  sed to POSIX fun
11a60 63 74 69 6f 6e 0d 0a 20 20 2a 2a 20 6f 70 65 6e  ction..  ** open
11a61 28 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62  (). These must b
11a62 65 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65  e calculated eve
11a63 6e 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e  n if open() is n
11a64 6f 74 20 63 61 6c 6c 65 64 2c 20 61 73 0d 0a 20  ot called, as.. 
11a65 20 2a 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20   ** they may be 
11a66 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f  stored as part o
11a67 66 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c  f the file handl
11a68 65 20 61 6e 64 20 75 73 65 64 20 62 79 20 74 68  e and used by th
11a69 65 20 0d 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20  e ..  ** 'conch 
11a6a 66 69 6c 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75  file' locking fu
11a6b 6e 63 74 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e  nctions later on
11a6c 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69 73 52  .  */..  if( isR
11a6d 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70 65 6e 46  eadonly )  openF
11a6e 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59  lags |= O_RDONLY
11a6f 3b 0d 0a 20 20 69 66 28 20 69 73 52 65 61 64 57  ;..  if( isReadW
11a70 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61 67 73  rite ) openFlags
11a71 20 7c 3d 20 4f 5f 52 44 57 52 3b 0d 0a 20 20 69   |= O_RDWR;..  i
11a72 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20 20  f( isCreate )   
11a73 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f   openFlags |= O_
11a74 43 52 45 41 54 3b 0d 0a 20 20 69 66 28 20 69 73  CREAT;..  if( is
11a75 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e  Exclusive ) open
11a76 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c  Flags |= (O_EXCL
11a77 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0d 0a 20  |O_NOFOLLOW);.. 
11a78 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
11a79 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e  _LARGEFILE|O_BIN
11a7a 41 52 59 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 66  ARY);....  if( f
11a7b 64 3c 30 20 29 7b 0d 0a 20 20 20 20 6d 6f 64 65  d<0 ){..    mode
11a7c 5f 74 20 6f 70 65 6e 4d 6f 64 65 3b 20 20 20 20  _t openMode;    
11a7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65 72            /* Per
11a7e 6d 69 73 73 69 6f 6e 73 20 74 6f 20 63 72 65 61  missions to crea
11a7f 74 65 20 66 69 6c 65 20 77 69 74 68 20 2a 2f 0d  te file with */.
11a80 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 43 72  .    rc = findCr
11a81 65 61 74 65 46 69 6c 65 4d 6f 64 65 28 7a 4e 61  eateFileMode(zNa
11a82 6d 65 2c 20 66 6c 61 67 73 2c 20 26 6f 70 65 6e  me, flags, &open
11a83 4d 6f 64 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  Mode);..    if( 
11a84 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
11a85 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
11a86 21 70 2d 3e 70 55 6e 75 73 65 64 20 29 3b 0d 0a  !p->pUnused );..
11a87 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
11a88 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
11a89 5f 57 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53  _WAL || eType==S
11a8a 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
11a8b 4a 4f 55 52 4e 41 4c 20 29 3b 0d 0a 20 20 20 20  JOURNAL );..    
11a8c 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20    return rc;..  
11a8d 20 20 7d 0d 0a 20 20 20 20 66 64 20 3d 20 72 6f    }..    fd = ro
11a8e 62 75 73 74 5f 6f 70 65 6e 28 7a 4e 61 6d 65 2c  bust_open(zName,
11a8f 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e   openFlags, open
11a90 4d 6f 64 65 29 3b 0d 0a 20 20 20 20 4f 53 54 52  Mode);..    OSTR
11a91 41 43 45 28 28 22 4f 50 45 4e 58 20 20 20 25 2d  ACE(("OPENX   %-
11a92 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64  3d %s 0%o\n", fd
11a93 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61  , zName, openFla
11a94 67 73 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 66  gs));..    if( f
11a95 64 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49  d<0 && errno!=EI
11a96 53 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72  SDIR && isReadWr
11a97 69 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73  ite && !isExclus
11a98 69 76 65 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  ive ){..      /*
11a99 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20   Failed to open 
11a9a 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61  the file for rea
11a9b 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d/write access. 
11a9c 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a  Try read-only. *
11a9d 2f 0d 0a 20 20 20 20 20 20 66 6c 61 67 73 20 26  /..      flags &
11a9e 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  = ~(SQLITE_OPEN_
11a9f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
11aa0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0d 0a  _OPEN_CREATE);..
11aa1 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20        openFlags 
11aa2 26 3d 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 52  &= ~(O_RDWR|O_CR
11aa3 45 41 54 29 3b 0d 0a 20 20 20 20 20 20 66 6c 61  EAT);..      fla
11aa4 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45  gs |= SQLITE_OPE
11aa5 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0d 0a 20 20 20  N_READONLY;..   
11aa6 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20     openFlags |= 
11aa7 4f 5f 52 44 4f 4e 4c 59 3b 0d 0a 20 20 20 20 20  O_RDONLY;..     
11aa8 20 69 73 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b   isReadonly = 1;
11aa9 0d 0a 20 20 20 20 20 20 66 64 20 3d 20 72 6f 62  ..      fd = rob
11aaa 75 73 74 5f 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20  ust_open(zName, 
11aab 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d  openFlags, openM
11aac 6f 64 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ode);..    }..  
11aad 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0d 0a 20    if( fd<0 ){.. 
11aae 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f       rc = unixLo
11aaf 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41  gError(SQLITE_CA
11ab0 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f 70  NTOPEN_BKPT, "op
11ab1 65 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20  en", zName);..  
11ab2 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69      goto open_fi
11ab3 6e 69 73 68 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a  nished;..    }..
11ab4 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 66    }..  assert( f
11ab5 64 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70  d>=0 );..  if( p
11ab6 4f 75 74 46 6c 61 67 73 20 29 7b 0d 0a 20 20 20  OutFlags ){..   
11ab7 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c   *pOutFlags = fl
11ab8 61 67 73 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  ags;..  }....  i
11ab9 66 28 20 70 2d 3e 70 55 6e 75 73 65 64 20 29 7b  f( p->pUnused ){
11aba 0d 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64  ..    p->pUnused
11abb 2d 3e 66 64 20 3d 20 66 64 3b 0d 0a 20 20 20 20  ->fd = fd;..    
11abc 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67  p->pUnused->flag
11abd 73 20 3d 20 66 6c 61 67 73 3b 0d 0a 20 20 7d 0d  s = flags;..  }.
11abe 0a 0d 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74  ...  if( isDelet
11abf 65 20 29 7b 0d 0a 23 69 66 20 4f 53 5f 56 58 57  e ){..#if OS_VXW
11ac0 4f 52 4b 53 0d 0a 20 20 20 20 7a 50 61 74 68 20  ORKS..    zPath 
11ac1 3d 20 7a 4e 61 6d 65 3b 0d 0a 23 65 6c 73 65 0d  = zName;..#else.
11ac2 0a 20 20 20 20 6f 73 55 6e 6c 69 6e 6b 28 7a 4e  .    osUnlink(zN
11ac3 61 6d 65 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  ame);..#endif.. 
11ac4 20 7d 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45   }..#if SQLITE_E
11ac5 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
11ac6 59 4c 45 0d 0a 20 20 65 6c 73 65 7b 0d 0a 20 20  YLE..  else{..  
11ac7 20 20 70 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d    p->openFlags =
11ac8 20 6f 70 65 6e 46 6c 61 67 73 3b 0d 0a 20 20 7d   openFlags;..  }
11ac9 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64  ..#endif....#ifd
11aca 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0d 0a 20  ef FD_CLOEXEC.. 
11acb 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 5f 53   osFcntl(fd, F_S
11acc 45 54 46 44 2c 20 6f 73 46 63 6e 74 6c 28 66 64  ETFD, osFcntl(fd
11acd 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20  , F_GETFD, 0) | 
11ace 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0d 0a 23 65  FD_CLOEXEC);..#e
11acf 6e 64 69 66 0d 0a 0d 0a 20 20 6e 6f 4c 6f 63 6b  ndif....  noLock
11ad0 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45   = eType!=SQLITE
11ad1 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0d 0a  _OPEN_MAIN_DB;..
11ad2 0d 0a 20 20 0d 0a 23 69 66 20 64 65 66 69 6e 65  ..  ..#if define
11ad3 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c 7c 20  d(__APPLE__) || 
11ad4 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
11ad5 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20 69  CKING_STYLE..  i
11ad6 66 28 20 66 73 74 61 74 66 73 28 66 64 2c 20 26  f( fstatfs(fd, &
11ad7 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b  fsInfo) == -1 ){
11ad8 0d 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65  ..    ((unixFile
11ad9 2a 29 70 46 69 6c 65 29 2d 3e 6c 61 73 74 45 72  *)pFile)->lastEr
11ada 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20  rno = errno;..  
11adb 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70    robust_close(p
11adc 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  , fd, __LINE__);
11add 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
11ade 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
11adf 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 20 28 30 20  ;..  }..  if (0 
11ae0 3d 3d 20 73 74 72 6e 63 6d 70 28 22 6d 73 64 6f  == strncmp("msdo
11ae1 73 22 2c 20 66 73 49 6e 66 6f 2e 66 5f 66 73 74  s", fsInfo.f_fst
11ae2 79 70 65 6e 61 6d 65 2c 20 35 29 29 20 7b 0d 0a  ypename, 5)) {..
11ae3 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
11ae4 70 46 69 6c 65 29 2d 3e 66 73 46 6c 61 67 73 20  pFile)->fsFlags 
11ae5 7c 3d 20 53 51 4c 49 54 45 5f 46 53 46 4c 41 47  |= SQLITE_FSFLAG
11ae6 53 5f 49 53 5f 4d 53 44 4f 53 3b 0d 0a 20 20 7d  S_IS_MSDOS;..  }
11ae7 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a  ..#endif....  /*
11ae8 20 53 65 74 20 75 70 20 61 70 70 72 6f 70 72 69   Set up appropri
11ae9 61 74 65 20 63 74 72 6c 46 6c 61 67 73 20 2a 2f  ate ctrlFlags */
11aea 0d 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 65  ..  if( isDelete
11aeb 20 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20   )              
11aec 20 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55    ctrlFlags |= U
11aed 4e 49 58 46 49 4c 45 5f 44 45 4c 45 54 45 3b 0d  NIXFILE_DELETE;.
11aee 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c  .  if( isReadonl
11aef 79 20 29 20 20 20 20 20 20 20 20 20 20 20 20 20  y )             
11af0 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e   ctrlFlags |= UN
11af1 49 58 46 49 4c 45 5f 52 44 4f 4e 4c 59 3b 0d 0a  IXFILE_RDONLY;..
11af2 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 20 20    if( noLock )  
11af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11af4 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49  ctrlFlags |= UNI
11af5 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0d 0a 20  XFILE_NOLOCK;.. 
11af6 20 69 66 28 20 73 79 6e 63 44 69 72 20 29 20 20   if( syncDir )  
11af7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
11af8 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58  trlFlags |= UNIX
11af9 46 49 4c 45 5f 44 49 52 53 59 4e 43 3b 0d 0a 20  FILE_DIRSYNC;.. 
11afa 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
11afb 49 54 45 5f 4f 50 45 4e 5f 55 52 49 20 29 20 63  ITE_OPEN_URI ) c
11afc 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58  trlFlags |= UNIX
11afd 46 49 4c 45 5f 55 52 49 3b 0d 0a 0d 0a 23 69 66  FILE_URI;....#if
11afe 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
11aff 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a 23 69  OCKING_STYLE..#i
11b00 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f  f SQLITE_PREFER_
11b01 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0d 0a 20  PROXY_LOCKING.. 
11b02 20 69 73 41 75 74 6f 50 72 6f 78 79 20 3d 20 31   isAutoProxy = 1
11b03 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 66 28  ;..#endif..  if(
11b04 20 69 73 41 75 74 6f 50 72 6f 78 79 20 26 26 20   isAutoProxy && 
11b05 28 7a 50 61 74 68 21 3d 4e 55 4c 4c 29 20 26 26  (zPath!=NULL) &&
11b06 20 28 21 6e 6f 4c 6f 63 6b 29 20 26 26 20 70 56   (!noLock) && pV
11b07 66 73 2d 3e 78 4f 70 65 6e 20 29 7b 0d 0a 20 20  fs->xOpen ){..  
11b08 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72 63 65    char *envforce
11b09 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c 49 54   = getenv("SQLIT
11b0a 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f  E_FORCE_PROXY_LO
11b0b 43 4b 49 4e 47 22 29 3b 0d 0a 20 20 20 20 69 6e  CKING");..    in
11b0c 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b 0d  t useProxy = 0;.
11b0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ...    /* SQLITE
11b0e 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
11b0f 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f  KING==1 means fo
11b10 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70  rce always use p
11b11 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0d 0a  roxy, 0 means ..
11b12 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65      ** never use
11b13 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61   proxy, NULL mea
11b14 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72  ns use proxy for
11b15 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73   non-local files
11b16 20 6f 6e 6c 79 2e 20 20 2a 2f 0d 0a 20 20 20 20   only.  */..    
11b17 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55  if( envforce!=NU
11b18 4c 4c 20 29 7b 0d 0a 20 20 20 20 20 20 75 73 65  LL ){..      use
11b19 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e 76  Proxy = atoi(env
11b1a 66 6f 72 63 65 29 3e 30 3b 0d 0a 20 20 20 20 7d  force)>0;..    }
11b1b 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69 66 28  else{..      if(
11b1c 20 73 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26   statfs(zPath, &
11b1d 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b  fsInfo) == -1 ){
11b1e 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20  ..        /* In 
11b1f 74 68 65 6f 72 79 2c 20 74 68 65 20 63 6c 6f 73  theory, the clos
11b20 65 28 66 64 29 20 63 61 6c 6c 20 69 73 20 73 75  e(fd) call is su
11b21 62 2d 6f 70 74 69 6d 61 6c 2e 20 49 66 20 74 68  b-optimal. If th
11b22 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 0d 0a 20  e file opened.. 
11b23 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 66         ** with f
11b24 64 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20  d is a database 
11b25 66 69 6c 65 2c 20 61 6e 64 20 74 68 65 72 65 20  file, and there 
11b26 61 72 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63  are other connec
11b27 74 69 6f 6e 73 20 6f 70 65 6e 0d 0a 20 20 20 20  tions open..    
11b28 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61 74 20 66      ** on that f
11b29 69 6c 65 20 74 68 61 74 20 61 72 65 20 63 75 72  ile that are cur
11b2a 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 61  rently holding a
11b2b 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 6f 6e  dvisory locks on
11b2c 20 69 74 2c 0d 0a 20 20 20 20 20 20 20 20 2a 2a   it,..        **
11b2d 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 20 74   then the call t
11b2e 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c 20 63  o close() will c
11b2f 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f 63 6b  ancel those lock
11b30 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65 2c 0d  s. In practice,.
11b31 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27 72  .        ** we'r
11b32 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20  e assuming that 
11b33 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27 74  statfs() doesn't
11b34 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65 6e   fail very often
11b35 2e 20 41 74 20 6c 65 61 73 74 0d 0a 20 20 20 20  . At least..    
11b36 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65      ** not while
11b37 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63   other file desc
11b38 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62  riptors opened b
11b39 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
11b3a 73 73 20 6f 6e 0d 0a 20 20 20 20 20 20 20 20 2a  ss on..        *
11b3b 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  * the same file 
11b3c 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f  are working.  */
11b3d 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73  ..        p->las
11b3e 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d  tErrno = errno;.
11b3f 0a 20 20 20 20 20 20 20 20 72 6f 62 75 73 74 5f  .        robust_
11b40 63 6c 6f 73 65 28 70 2c 20 66 64 2c 20 5f 5f 4c  close(p, fd, __L
11b41 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20 20 20 20  INE__);..       
11b42 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
11b43 52 52 5f 41 43 43 45 53 53 3b 0d 0a 20 20 20 20  RR_ACCESS;..    
11b44 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69      goto open_fi
11b45 6e 69 73 68 65 64 3b 0d 0a 20 20 20 20 20 20 7d  nished;..      }
11b46 0d 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 79  ..      useProxy
11b47 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c   = !(fsInfo.f_fl
11b48 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0d  ags&MNT_LOCAL);.
11b49 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
11b4a 75 73 65 50 72 6f 78 79 20 29 7b 0d 0a 20 20 20  useProxy ){..   
11b4b 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e     rc = fillInUn
11b4c 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c  ixFile(pVfs, fd,
11b4d 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 63   pFile, zPath, c
11b4e 74 72 6c 46 6c 61 67 73 29 3b 0d 0a 20 20 20 20  trlFlags);..    
11b4f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11b50 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
11b51 72 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66  rc = proxyTransf
11b52 6f 72 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e 69  ormUnixFile((uni
11b53 78 46 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 3a  xFile*)pFile, ":
11b54 61 75 74 6f 3a 22 29 3b 0d 0a 20 20 20 20 20 20  auto:");..      
11b55 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11b56 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
11b57 20 20 2f 2a 20 55 73 65 20 75 6e 69 78 43 6c 6f    /* Use unixClo
11b58 73 65 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74  se to clean up t
11b59 68 65 20 72 65 73 6f 75 72 63 65 73 20 61 64 64  he resources add
11b5a 65 64 20 69 6e 20 66 69 6c 6c 49 6e 55 6e 69 78  ed in fillInUnix
11b5b 46 69 6c 65 20 0d 0a 20 20 20 20 20 20 20 20 20  File ..         
11b5c 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 20 61 6c   ** and clear al
11b5d 6c 20 74 68 65 20 73 74 72 75 63 74 75 72 65 27  l the structure'
11b5e 73 20 72 65 66 65 72 65 6e 63 65 73 2e 20 20 53  s references.  S
11b5f 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 0d 0a 20  pecifically, .. 
11b60 20 20 20 20 20 20 20 20 20 2a 2a 20 70 46 69 6c           ** pFil
11b61 65 2d 3e 70 4d 65 74 68 6f 64 73 20 77 69 6c 6c  e->pMethods will
11b62 20 62 65 20 4e 55 4c 4c 20 73 6f 20 73 71 6c 69   be NULL so sqli
11b63 74 65 33 4f 73 43 6c 6f 73 65 20 77 69 6c 6c 20  te3OsClose will 
11b64 62 65 20 61 20 6e 6f 2d 6f 70 20 0d 0a 20 20 20  be a no-op ..   
11b65 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
11b66 20 20 20 20 20 75 6e 69 78 43 6c 6f 73 65 28 70       unixClose(p
11b67 46 69 6c 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  File);..        
11b68 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20    return rc;..  
11b69 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
11b6a 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65  ..      goto ope
11b6b 6e 5f 66 69 6e 69 73 68 65 64 3b 0d 0a 20 20 20  n_finished;..   
11b6c 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d   }..  }..#endif.
11b6d 0a 20 20 0d 0a 20 20 72 63 20 3d 20 66 69 6c 6c  .  ..  rc = fill
11b6e 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c  InUnixFile(pVfs,
11b6f 20 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74   fd, pFile, zPat
11b70 68 2c 20 63 74 72 6c 46 6c 61 67 73 29 3b 0d 0a  h, ctrlFlags);..
11b71 0d 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3a  ..open_finished:
11b72 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
11b73 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71  TE_OK ){..    sq
11b74 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 55  lite3_free(p->pU
11b75 6e 75 73 65 64 29 3b 0d 0a 20 20 7d 0d 0a 20 20  nused);..  }..  
11b76 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
11b77 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65  .../*..** Delete
11b78 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61   the file at zPa
11b79 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79  th. If the dirSy
11b7a 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nc argument is t
11b7b 72 75 65 2c 20 66 73 79 6e 63 28 29 0d 0a 2a 2a  rue, fsync()..**
11b7c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61   the directory a
11b7d 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68  fter deleting th
11b7e 65 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  e file...*/..sta
11b7f 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65  tic int unixDele
11b80 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  te(..  sqlite3_v
11b81 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20  fs *NotUsed,    
11b82 20 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e 69   /* VFS containi
11b83 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 20 78  ng this as the x
11b84 44 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f  Delete method */
11b85 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
11b86 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a  zPath,        /*
11b87 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
11b88 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0d 0a   be deleted */..
11b89 20 20 69 6e 74 20 64 69 72 53 79 6e 63 20 20 20    int dirSync   
11b8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
11b8b 66 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 20  f true, fsync() 
11b8c 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20  directory after 
11b8d 64 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f  deleting file */
11b8e 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  ..){..  int rc =
11b8f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 55   SQLITE_OK;..  U
11b90 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
11b91 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 53 69 6d  NotUsed);..  Sim
11b92 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
11b93 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
11b94 5f 44 45 4c 45 54 45 29 3b 0d 0a 20 20 69 66 28  _DELETE);..  if(
11b95 20 6f 73 55 6e 6c 69 6e 6b 28 7a 50 61 74 68 29   osUnlink(zPath)
11b96 3d 3d 28 2d 31 29 20 26 26 20 65 72 72 6e 6f 21  ==(-1) && errno!
11b97 3d 45 4e 4f 45 4e 54 20 29 7b 0d 0a 20 20 20 20  =ENOENT ){..    
11b98 72 65 74 75 72 6e 20 75 6e 69 78 4c 6f 67 45 72  return unixLogEr
11b99 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52  ror(SQLITE_IOERR
11b9a 5f 44 45 4c 45 54 45 2c 20 22 75 6e 6c 69 6e 6b  _DELETE, "unlink
11b9b 22 2c 20 7a 50 61 74 68 29 3b 0d 0a 20 20 7d 0d  ", zPath);..  }.
11b9c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
11b9d 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0d  DISABLE_DIRSYNC.
11b9e 0a 20 20 69 66 28 20 28 64 69 72 53 79 6e 63 20  .  if( (dirSync 
11b9f 26 20 31 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  & 1)!=0 ){..    
11ba0 69 6e 74 20 66 64 3b 0d 0a 20 20 20 20 72 63 20  int fd;..    rc 
11ba1 3d 20 6f 73 4f 70 65 6e 44 69 72 65 63 74 6f 72  = osOpenDirector
11ba2 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b 0d 0a  y(zPath, &fd);..
11ba3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
11ba4 54 45 5f 4f 4b 20 29 7b 0d 0a 23 69 66 20 4f 53  TE_OK ){..#if OS
11ba5 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 20 20 20 20  _VXWORKS..      
11ba6 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d  if( fsync(fd)==-
11ba7 31 20 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20  1 )..#else..    
11ba8 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 20    if( fsync(fd) 
11ba9 29 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20  )..#endif..     
11baa 20 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d   {..        rc =
11bab 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51   unixLogError(SQ
11bac 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46  LITE_IOERR_DIR_F
11bad 53 59 4e 43 2c 20 22 66 73 79 6e 63 22 2c 20 7a  SYNC, "fsync", z
11bae 50 61 74 68 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  Path);..      }.
11baf 0a 20 20 20 20 20 20 72 6f 62 75 73 74 5f 63 6c  .      robust_cl
11bb0 6f 73 65 28 30 2c 20 66 64 2c 20 5f 5f 4c 49 4e  ose(0, fd, __LIN
11bb1 45 5f 5f 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  E__);..    }else
11bb2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11bb3 43 41 4e 54 4f 50 45 4e 20 29 7b 0d 0a 20 20 20  CANTOPEN ){..   
11bb4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
11bb5 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  K;..    }..  }..
11bb6 23 65 6e 64 69 66 0d 0a 20 20 72 65 74 75 72 6e  #endif..  return
11bb7 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
11bb8 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 73 74  * Test the exist
11bb9 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 65 73  ance of or acces
11bba 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66  s permissions of
11bbb 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 65   file zPath. The
11bbc 0d 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f 72  ..** test perfor
11bbd 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  med depends on t
11bbe 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 67  he value of flag
11bbf 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53  s:..**..**     S
11bc0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
11bc1 53 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66  STS: Return 1 if
11bc2 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73   the file exists
11bc3 0d 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  ..**     SQLITE_
11bc4 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
11bc5 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68  : Return 1 if th
11bc6 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 20 61  e file is read a
11bc7 6e 64 20 77 72 69 74 61 62 6c 65 2e 0d 0a 2a 2a  nd writable...**
11bc8 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45       SQLITE_ACCE
11bc9 53 53 5f 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74  SS_READONLY: Ret
11bca 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c  urn 1 if the fil
11bcb 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0d 0a  e is readable...
11bcc 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  **..** Otherwise
11bcd 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f 0d 0a   return 0...*/..
11bce 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41  static int unixA
11bcf 63 63 65 73 73 28 0d 0a 20 20 73 71 6c 69 74 65  ccess(..  sqlite
11bd0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
11bd1 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e    /* The VFS con
11bd2 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63  taining this xAc
11bd3 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0d 0a  cess method */..
11bd4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
11bd5 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74  ath,      /* Pat
11bd6 68 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  h of the file to
11bd7 20 65 78 61 6d 69 6e 65 20 2a 2f 0d 0a 20 20 69   examine */..  i
11bd8 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
11bd9 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64         /* What d
11bda 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61  o we want to lea
11bdb 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61  rn about the zPa
11bdc 74 68 20 66 69 6c 65 3f 20 2a 2f 0d 0a 20 20 69  th file? */..  i
11bdd 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20  nt *pResOut     
11bde 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
11bdf 72 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68  result boolean h
11be0 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ere */..){..  in
11be1 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0d 0a 20 20  t amode = 0;..  
11be2 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
11be3 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 53 69  (NotUsed);..  Si
11be4 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
11be5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
11be6 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0d 0a 20  RR_ACCESS; );.. 
11be7 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29   switch( flags )
11be8 7b 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  {..    case SQLI
11be9 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
11bea 3a 0d 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d  :..      amode =
11beb 20 46 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 62 72   F_OK;..      br
11bec 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65 20 53  eak;..    case S
11bed 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
11bee 44 57 52 49 54 45 3a 0d 0a 20 20 20 20 20 20 61  DWRITE:..      a
11bef 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b  mode = W_OK|R_OK
11bf0 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
11bf1 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
11bf2 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0d 0a 20  _ACCESS_READ:.. 
11bf3 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f       amode = R_O
11bf4 4b 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  K;..      break;
11bf5 0d 0a 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  ....    default:
11bf6 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  ..      assert(!
11bf7 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
11bf8 72 67 75 6d 65 6e 74 22 29 3b 0d 0a 20 20 7d 0d  rgument");..  }.
11bf9 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 6f  .  *pResOut = (o
11bfa 73 41 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61  sAccess(zPath, a
11bfb 6d 6f 64 65 29 3d 3d 30 29 3b 0d 0a 20 20 69 66  mode)==0);..  if
11bfc 28 20 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f  ( flags==SQLITE_
11bfd 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 26 26  ACCESS_EXISTS &&
11bfe 20 2a 70 52 65 73 4f 75 74 20 29 7b 0d 0a 20 20   *pResOut ){..  
11bff 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75    struct stat bu
11c00 66 3b 0d 0a 20 20 20 20 69 66 28 20 30 3d 3d 6f  f;..    if( 0==o
11c01 73 53 74 61 74 28 7a 50 61 74 68 2c 20 26 62 75  sStat(zPath, &bu
11c02 66 29 20 26 26 20 62 75 66 2e 73 74 5f 73 69 7a  f) && buf.st_siz
11c03 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 2a  e==0 ){..      *
11c04 70 52 65 73 4f 75 74 20 3d 20 30 3b 0d 0a 20 20  pResOut = 0;..  
11c05 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
11c06 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
11c07 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 75 72  ....../*..** Tur
11c08 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
11c09 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
11c0a 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65 20  l pathname. The 
11c0b 72 65 6c 61 74 69 76 65 20 70 61 74 68 0d 0a 2a  relative path..*
11c0c 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61  * is stored as a
11c0d 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
11c0e 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 62 75  string in the bu
11c0f 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
11c10 62 79 0d 0a 2a 2a 20 7a 50 61 74 68 2e 20 0d 0a  by..** zPath. ..
11c11 2a 2a 0d 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e  **..** zOut poin
11c12 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f  ts to a buffer o
11c13 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74  f at least sqlit
11c14 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d  e3_vfs.mxPathnam
11c15 65 20 62 79 74 65 73 20 0d 0a 2a 2a 20 28 69 6e  e bytes ..** (in
11c16 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f   this case, MAX_
11c17 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e  PATHNAME bytes).
11c18 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69   The full-path i
11c19 73 20 77 72 69 74 74 65 6e 20 74 6f 0d 0a 2a 2a  s written to..**
11c1a 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66   this buffer bef
11c1b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  ore returning...
11c1c 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75  */..static int u
11c1d 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  nixFullPathname(
11c1e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ..  sqlite3_vfs 
11c1f 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
11c20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
11c21 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20  vfs object */.. 
11c22 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
11c23 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  th,            /
11c24 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74  * Possibly relat
11c25 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a  ive input path *
11c26 2f 0d 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20  /..  int nOut,  
11c27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c28 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75     /* Size of ou
11c29 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62  tput buffer in b
11c2a 79 74 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20  ytes */..  char 
11c2b 2a 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20  *zOut           
11c2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
11c2d 75 74 20 62 75 66 66 65 72 20 2a 2f 0d 0a 29 7b  ut buffer */..){
11c2e 0d 0a 0d 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64  ....  /* It's od
11c2f 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e  d to simulate an
11c30 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20   io-error here, 
11c31 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20  but really this 
11c32 69 73 20 6a 75 73 74 0d 0a 20 20 2a 2a 20 75 73  is just..  ** us
11c33 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72  ing the io-error
11c34 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20   infrastructure 
11c35 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 51 4c  to test that SQL
11c36 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73  ite handles this
11c37 0d 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ..  ** function 
11c38 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 66 75  failing. This fu
11c39 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 61 69  nction could fai
11c3a 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d 70 6c  l if, for exampl
11c3b 65 2c 20 74 68 65 0d 0a 20 20 2a 2a 20 63 75 72  e, the..  ** cur
11c3c 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 72  rent working dir
11c3d 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20  ectory has been 
11c3e 75 6e 6c 69 6e 6b 65 64 2e 0d 0a 20 20 2a 2f 0d  unlinked...  */.
11c3f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
11c40 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
11c41 45 5f 45 52 52 4f 52 20 29 3b 0d 0a 0d 0a 20 20  E_ERROR );....  
11c42 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78  assert( pVfs->mx
11c43 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41  Pathname==MAX_PA
11c44 54 48 4e 41 4d 45 20 29 3b 0d 0a 20 20 55 4e 55  THNAME );..  UNU
11c45 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
11c46 66 73 29 3b 0d 0a 0d 0a 20 20 7a 4f 75 74 5b 6e  fs);....  zOut[n
11c47 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0d 0a  Out-1] = '\0';..
11c48 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d    if( zPath[0]==
11c49 27 2f 27 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  '/' ){..    sqli
11c4a 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75  te3_snprintf(nOu
11c4b 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a  t, zOut, "%s", z
11c4c 50 61 74 68 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b  Path);..  }else{
11c4d 0d 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0d  ..    int nCwd;.
11c4e 0a 20 20 20 20 69 66 28 20 6f 73 47 65 74 63 77  .    if( osGetcw
11c4f 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d 31 29 3d  d(zOut, nOut-1)=
11c50 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  =0 ){..      ret
11c51 75 72 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72  urn unixLogError
11c52 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e  (SQLITE_CANTOPEN
11c53 5f 42 4b 50 54 2c 20 22 67 65 74 63 77 64 22 2c  _BKPT, "getcwd",
11c54 20 7a 50 61 74 68 29 3b 0d 0a 20 20 20 20 7d 0d   zPath);..    }.
11c55 0a 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e 74  .    nCwd = (int
11c56 29 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0d 0a  )strlen(zOut);..
11c57 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
11c58 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20  intf(nOut-nCwd, 
11c59 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25  &zOut[nCwd], "/%
11c5a 73 22 2c 20 7a 50 61 74 68 29 3b 0d 0a 20 20 7d  s", zPath);..  }
11c5b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
11c5c 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 23 69  E_OK;..}......#i
11c5d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
11c5e 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
11c5f 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74 65 72 66 61  ../*..** Interfa
11c60 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
11c61 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
11c62 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
11c63 70 6f 69 6e 74 73 0d 0a 2a 2a 20 77 69 74 68 69  points..** withi
11c64 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  n the shared lib
11c65 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e  rary, and closin
11c66 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  g the shared lib
11c67 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 23 69 6e 63 6c  rary...*/..#incl
11c68 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0d 0a 73  ude <dlfcn.h>..s
11c69 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78  tatic void *unix
11c6a 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
11c6b 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e  fs *NotUsed, con
11c6c 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
11c6d 6d 65 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  me){..  UNUSED_P
11c6e 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
11c6f 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f  );..  return dlo
11c70 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52  pen(zFilename, R
11c71 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47  TLD_NOW | RTLD_G
11c72 4c 4f 42 41 4c 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  LOBAL);..}..../*
11c73 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c  ..** SQLite call
11c74 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
11c75 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
11c76 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78  r a call to unix
11c77 44 6c 53 79 6d 28 29 20 6f 72 0d 0a 2a 2a 20 75  DlSym() or..** u
11c78 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c  nixDlOpen() fail
11c79 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 6c  s (returns a nul
11c7a 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 61  l pointer). If a
11c7b 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 65   more detailed e
11c7c 72 72 6f 72 0d 0a 2a 2a 20 6d 65 73 73 61 67 65  rror..** message
11c7d 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69   is available, i
11c7e 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
11c7f 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65  zBufOut. If no e
11c80 72 72 6f 72 20 6d 65 73 73 61 67 65 0d 0a 2a 2a  rror message..**
11c81 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a   is available, z
11c82 42 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75  BufOut is left u
11c83 6e 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51  nmodified and SQ
11c84 4c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61  Lite uses a defa
11c85 75 6c 74 0d 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  ult..** error me
11c86 73 73 61 67 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ssage...*/..stat
11c87 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 45 72  ic void unixDlEr
11c88 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
11c89 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42  *NotUsed, int nB
11c8a 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  uf, char *zBufOu
11c8b 74 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  t){..  const cha
11c8c 72 20 2a 7a 45 72 72 3b 0d 0a 20 20 55 4e 55 53  r *zErr;..  UNUS
11c8d 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
11c8e 55 73 65 64 29 3b 0d 0a 20 20 75 6e 69 78 45 6e  Used);..  unixEn
11c8f 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 7a  terMutex();..  z
11c90 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b  Err = dlerror();
11c91 0d 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0d  ..  if( zErr ){.
11c92 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
11c93 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66  rintf(nBuf, zBuf
11c94 4f 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  Out, "%s", zErr)
11c95 3b 0d 0a 20 20 7d 0d 0a 20 20 75 6e 69 78 4c 65  ;..  }..  unixLe
11c96 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 7d 0d 0a  aveMutex();..}..
11c97 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a 75 6e  static void (*un
11c98 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  ixDlSym(sqlite3_
11c99 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f  vfs *NotUsed, vo
11c9a 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  id *p, const cha
11c9b 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b 0d  r*zSym))(void){.
11c9c 0a 20 20 2f 2a 20 0d 0a 20 20 2a 2a 20 47 43 43  .  /* ..  ** GCC
11c9d 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d   with -pedantic-
11c9e 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74  errors says that
11c9f 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c   C90 does not al
11ca0 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62  low a void* to b
11ca1 65 0d 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74  e..  ** cast int
11ca2 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  o a pointer to a
11ca3 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20   function.  And 
11ca4 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  yet the library 
11ca5 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0d  dlsym() routine.
11ca6 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  .  ** returns a 
11ca7 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72  void* which is r
11ca8 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
11ca9 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  to a function.  
11caa 53 6f 20 68 6f 77 20 64 6f 20 77 65 0d 0a 20 20  So how do we..  
11cab 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77  ** use dlsym() w
11cac 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72  ith -pedantic-er
11cad 72 6f 72 73 3f 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  rors?..  **..  *
11cae 2a 20 56 61 72 69 61 62 6c 65 20 78 20 62 65 6c  * Variable x bel
11caf 6f 77 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f  ow is defined to
11cb0 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
11cb1 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69   a function taki
11cb2 6e 67 0d 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74  ng..  ** paramet
11cb3 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f  ers void* and co
11cb4 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65  nst char* and re
11cb5 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
11cb6 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r to a function.
11cb7 0d 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 69 61  ..  ** We initia
11cb8 6c 69 7a 65 20 78 20 62 79 20 61 73 73 69 67 6e  lize x by assign
11cb9 69 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 65 72  ing it a pointer
11cba 20 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 29 20   to the dlsym() 
11cbb 66 75 6e 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2a 20  function...  ** 
11cbc 28 54 68 61 74 20 61 73 73 69 67 6e 6d 65 6e 74  (That assignment
11cbd 20 72 65 71 75 69 72 65 73 20 61 20 63 61 73 74   requires a cast
11cbe 2e 29 20 20 54 68 65 6e 20 77 65 20 63 61 6c 6c  .)  Then we call
11cbf 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68   the function th
11cc0 61 74 0d 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74  at..  ** x point
11cc1 73 20 74 6f 2e 20 20 0d 0a 20 20 2a 2a 0d 0a 20  s to.  ..  **.. 
11cc2 20 2a 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72   ** This work-ar
11cc3 6f 75 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79  ound is unlikely
11cc4 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74   to work correct
11cc5 6c 79 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d  ly on any system
11cc6 20 77 68 65 72 65 0d 0a 20 20 2a 2a 20 79 6f 75   where..  ** you
11cc7 20 72 65 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63   really cannot c
11cc8 61 73 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 70  ast a function p
11cc9 6f 69 6e 74 65 72 20 69 6e 74 6f 20 76 6f 69 64  ointer into void
11cca 2a 2e 20 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e  *.  But then, on
11ccb 20 74 68 65 0d 0a 20 20 2a 2a 20 6f 74 68 65 72   the..  ** other
11ccc 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28 29 20 77   hand, dlsym() w
11ccd 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  ill not work on 
11cce 73 75 63 68 20 61 20 73 79 73 74 65 6d 20 65 69  such a system ei
11ccf 74 68 65 72 2c 20 73 6f 20 77 65 20 68 61 76 65  ther, so we have
11cd0 0d 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61 6c 6c  ..  ** not reall
11cd1 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e 67 2e  y lost anything.
11cd2 0d 0a 20 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28  ..  */..  void (
11cd3 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  *(*x)(void*,cons
11cd4 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b  t char*))(void);
11cd5 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
11cd6 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a  ETER(NotUsed);..
11cd7 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29    x = (void(*(*)
11cd8 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
11cd9 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d  r*))(void))dlsym
11cda 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29  ;..  return (*x)
11cdb 28 70 2c 20 7a 53 79 6d 29 3b 0d 0a 7d 0d 0a 73  (p, zSym);..}..s
11cdc 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44  tatic void unixD
11cdd 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
11cde 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69  fs *NotUsed, voi
11cdf 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0d 0a 20 20  d *pHandle){..  
11ce0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
11ce1 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 64 6c  (NotUsed);..  dl
11ce2 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0d  close(pHandle);.
11ce3 0a 7d 0d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  .}..#else /* if 
11ce4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
11ce5 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65  _EXTENSION is de
11ce6 66 69 6e 65 64 3a 20 2a 2f 0d 0a 20 20 23 64 65  fined: */..  #de
11ce7 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20  fine unixDlOpen 
11ce8 20 30 0d 0a 20 20 23 64 65 66 69 6e 65 20 75 6e   0..  #define un
11ce9 69 78 44 6c 45 72 72 6f 72 20 30 0d 0a 20 20 23  ixDlError 0..  #
11cea 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d  define unixDlSym
11ceb 20 20 20 30 0d 0a 20 20 23 64 65 66 69 6e 65 20     0..  #define 
11cec 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0d 0a 23  unixDlClose 0..#
11ced 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
11cee 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73  Write nBuf bytes
11cef 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20   of random data 
11cf0 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  to the supplied 
11cf1 62 75 66 66 65 72 20 7a 42 75 66 2e 0d 0a 2a 2f  buffer zBuf...*/
11cf2 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  ..static int uni
11cf3 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  xRandomness(sqli
11cf4 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
11cf5 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
11cf6 20 2a 7a 42 75 66 29 7b 0d 0a 20 20 55 4e 55 53   *zBuf){..  UNUS
11cf7 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
11cf8 55 73 65 64 29 3b 0d 0a 20 20 61 73 73 65 72 74  Used);..  assert
11cf9 28 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28  ((size_t)nBuf>=(
11cfa 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73  sizeof(time_t)+s
11cfb 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0d 0a 0d  izeof(int)));...
11cfc 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f  .  /* We have to
11cfd 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66   initialize zBuf
11cfe 20 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67   to prevent valg
11cff 72 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74  rind from report
11d00 69 6e 67 0d 0a 20 20 2a 2a 20 65 72 72 6f 72 73  ing..  ** errors
11d01 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73 20 69  .  The reports i
11d02 73 73 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e  ssued by valgrin
11d03 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20  d are incorrect 
11d04 2d 20 77 65 20 77 6f 75 6c 64 0d 0a 20 20 2a 2a  - we would..  **
11d05 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68 65   prefer that the
11d06 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69   randomness be i
11d07 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b 69  ncreased by maki
11d08 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0d 0a 20  ng use of the.. 
11d09 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   ** uninitialize
11d0a 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20  d space in zBuf 
11d0b 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65  - but valgrind e
11d0c 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f  rrors tend to wo
11d0d 72 72 79 0d 0a 20 20 2a 2a 20 73 6f 6d 65 20 75  rry..  ** some u
11d0e 73 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68  sers.  Rather th
11d0f 61 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65  an argue, it see
11d10 6d 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74  ms easier just t
11d11 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0d 0a 20 20  o initialize..  
11d12 2a 2a 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72  ** the whole arr
11d13 61 79 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76  ay and silence v
11d14 61 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66  algrind, even if
11d15 20 74 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73   that means less
11d16 20 72 61 6e 64 6f 6d 6e 65 73 73 0d 0a 20 20 2a   randomness..  *
11d17 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20  * in the random 
11d18 73 65 65 64 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  seed...  **..  *
11d19 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
11d1a 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 75  initializing zBu
11d1b 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 61  f[] to zero is a
11d1c 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 20  ll we do.  That 
11d1d 6d 65 61 6e 73 0d 0a 20 20 2a 2a 20 74 68 61 74  means..  ** that
11d1e 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74   we always use t
11d1f 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e  he same random n
11d20 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20  umber sequence. 
11d21 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0d   This makes the.
11d22 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65  .  ** tests repe
11d23 61 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20  atable...  */.. 
11d24 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c   memset(zBuf, 0,
11d25 20 6e 42 75 66 29 3b 0d 0a 23 69 66 20 21 64 65   nBuf);..#if !de
11d26 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
11d27 54 29 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 6e 74  T)..  {..    int
11d28 20 70 69 64 2c 20 66 64 3b 0d 0a 20 20 20 20 66   pid, fd;..    f
11d29 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28  d = robust_open(
11d2a 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20  "/dev/urandom", 
11d2b 4f 5f 52 44 4f 4e 4c 59 2c 20 30 29 3b 0d 0a 20  O_RDONLY, 0);.. 
11d2c 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0d 0a     if( fd<0 ){..
11d2d 20 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0d        time_t t;.
11d2e 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b  .      time(&t);
11d2f 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  ..      memcpy(z
11d30 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28  Buf, &t, sizeof(
11d31 74 29 29 3b 0d 0a 20 20 20 20 20 20 70 69 64 20  t));..      pid 
11d32 3d 20 67 65 74 70 69 64 28 29 3b 0d 0a 20 20 20  = getpid();..   
11d33 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
11d34 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64  sizeof(t)], &pid
11d35 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0d  , sizeof(pid));.
11d36 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
11d37 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 28  izeof(t)+sizeof(
11d38 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42  pid)<=(size_t)nB
11d39 75 66 20 29 3b 0d 0a 20 20 20 20 20 20 6e 42 75  uf );..      nBu
11d3a 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20  f = sizeof(t) + 
11d3b 73 69 7a 65 6f 66 28 70 69 64 29 3b 0d 0a 20 20  sizeof(pid);..  
11d3c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
11d3d 64 6f 7b 20 6e 42 75 66 20 3d 20 6f 73 52 65 61  do{ nBuf = osRea
11d3e 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66  d(fd, zBuf, nBuf
11d3f 29 3b 20 7d 77 68 69 6c 65 28 20 6e 42 75 66 3c  ); }while( nBuf<
11d40 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54  0 && errno==EINT
11d41 52 20 29 3b 0d 0a 20 20 20 20 20 20 72 6f 62 75  R );..      robu
11d42 73 74 5f 63 6c 6f 73 65 28 30 2c 20 66 64 2c 20  st_close(0, fd, 
11d43 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a 20 20 20 20  __LINE__);..    
11d44 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  }..  }..#endif..
11d45 20 20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0d 0a    return nBuf;..
11d46 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 6c  }....../*..** Sl
11d47 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
11d48 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
11d49 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
11d4a 6d 65 20 73 6c 65 70 74 2e 0d 0a 2a 2a 20 54 68  me slept...** Th
11d4b 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
11d4c 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
11d4d 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74  oseconds we want
11d4e 20 74 6f 20 73 6c 65 65 70 2e 0d 0a 2a 2a 20 54   to sleep...** T
11d4f 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
11d50 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
11d51 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66   microseconds of
11d52 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0d   sleep actually.
11d53 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72  .** requested fr
11d54 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  om the underlyin
11d55 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
11d56 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69  em, a number whi
11d57 63 68 0d 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20  ch..** might be 
11d58 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
11d59 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67  equal to the arg
11d5a 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c  ument, but not l
11d5b 65 73 73 0d 0a 2a 2a 20 74 68 61 6e 20 74 68 65  ess..** than the
11d5c 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a   argument...*/..
11d5d 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53  static int unixS
11d5e 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73  leep(sqlite3_vfs
11d5f 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6d   *NotUsed, int m
11d60 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0d 0a 23  icroseconds){..#
11d61 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 20  if OS_VXWORKS.. 
11d62 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63   struct timespec
11d63 20 73 70 3b 0d 0a 0d 0a 20 20 73 70 2e 74 76 5f   sp;....  sp.tv_
11d64 73 65 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e  sec = microsecon
11d65 64 73 20 2f 20 31 30 30 30 30 30 30 3b 0d 0a 20  ds / 1000000;.. 
11d66 20 73 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d   sp.tv_nsec = (m
11d67 69 63 72 6f 73 65 63 6f 6e 64 73 20 25 20 31 30  icroseconds % 10
11d68 30 30 30 30 30 29 20 2a 20 31 30 30 30 3b 0d 0a  00000) * 1000;..
11d69 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c    nanosleep(&sp,
11d6a 20 4e 55 4c 4c 29 3b 0d 0a 20 20 55 4e 55 53 45   NULL);..  UNUSE
11d6b 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
11d6c 73 65 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  sed);..  return 
11d6d 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0d 0a 23  microseconds;..#
11d6e 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56  elif defined(HAV
11d6f 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
11d70 45 5f 55 53 4c 45 45 50 0d 0a 20 20 75 73 6c 65  E_USLEEP..  usle
11d71 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29  ep(microseconds)
11d72 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
11d73 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d  METER(NotUsed);.
11d74 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73  .  return micros
11d75 65 63 6f 6e 64 73 3b 0d 0a 23 65 6c 73 65 0d 0a  econds;..#else..
11d76 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20    int seconds = 
11d77 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39  (microseconds+99
11d78 39 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0d 0a  9999)/1000000;..
11d79 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29    sleep(seconds)
11d7a 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
11d7b 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d  METER(NotUsed);.
11d7c 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64  .  return second
11d7d 73 2a 31 30 30 30 30 30 30 3b 0d 0a 23 65 6e 64  s*1000000;..#end
11d7e 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  if..}..../*..** 
11d7f 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
11d80 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74  riable, if set t
11d81 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
11d82 75 65 2c 20 69 73 20 69 6e 74 65 72 70 72 65 74  ue, is interpret
11d83 65 64 20 61 73 0d 0a 2a 2a 20 74 68 65 20 6e 75  ed as..** the nu
11d84 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64 73 20  mber of seconds 
11d85 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64 20 69  since 1970 and i
11d86 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
11d87 65 20 72 65 73 75 6c 74 20 6f 66 0d 0a 2a 2a 20  e result of..** 
11d88 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
11d89 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74 65  Time() during te
11d8a 73 74 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23 69 66 64  sting...*/..#ifd
11d8b 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a  ef SQLITE_TEST..
11d8c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
11d8d 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
11d8e 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b  ime = 0;  /* Fak
11d8f 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e  e system time in
11d90 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31   seconds since 1
11d91 39 37 30 2e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  970. */..#endif.
11d92 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74  .../*..** Find t
11d93 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
11d94 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
11d95 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
11d96 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69    Write into *pi
11d97 4e 6f 77 0d 0a 2a 2a 20 74 68 65 20 63 75 72 72  Now..** the curr
11d98 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74  ent time and dat
11d99 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61  e as a Julian Da
11d9a 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38  y number times 8
11d9b 36 5f 34 30 30 5f 30 30 30 2e 20 20 49 6e 0d 0a  6_400_000.  In..
11d9c 2a 2a 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  ** other words, 
11d9d 77 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 4e 6f  write into *piNo
11d9e 77 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  w the number of 
11d9f 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 69 6e  milliseconds sin
11da0 63 65 20 74 68 65 20 4a 75 6c 69 61 6e 0d 0a 2a  ce the Julian..*
11da1 2a 20 65 70 6f 63 68 20 6f 66 20 6e 6f 6f 6e 20  * epoch of noon 
11da2 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20  in Greenwich on 
11da3 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31  November 24, 471
11da4 34 20 42 2e 43 20 61 63 63 6f 72 64 69 6e 67 20  4 B.C according 
11da5 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 72 6f 6c 65  to the..** prole
11da6 70 74 69 63 20 47 72 65 67 6f 72 69 61 6e 20 63  ptic Gregorian c
11da7 61 6c 65 6e 64 61 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  alendar...**..**
11da8 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 72 65 74   On success, ret
11da9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20  urn SQLITE_OK.  
11daa 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  Return SQLITE_ER
11dab 52 4f 52 20 69 66 20 74 68 65 20 74 69 6d 65 20  ROR if the time 
11dac 61 6e 64 20 64 61 74 65 20 0d 0a 2a 2a 20 63 61  and date ..** ca
11dad 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0d 0a  nnot be found...
11dae 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75  */..static int u
11daf 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e  nixCurrentTimeIn
11db0 74 36 34 28 73 71 6c 69 74 65 33 5f 76 66 73 20  t64(sqlite3_vfs 
11db1 2a 4e 6f 74 55 73 65 64 2c 20 73 71 6c 69 74 65  *NotUsed, sqlite
11db2 33 5f 69 6e 74 36 34 20 2a 70 69 4e 6f 77 29 7b  3_int64 *piNow){
11db3 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ..  static const
11db4 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75   sqlite3_int64 u
11db5 6e 69 78 45 70 6f 63 68 20 3d 20 32 34 34 30 35  nixEpoch = 24405
11db6 38 37 35 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74  875*(sqlite3_int
11db7 36 34 29 38 36 34 30 30 30 30 3b 0d 0a 20 20 69  64)8640000;..  i
11db8 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
11db9 4b 3b 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  K;..#if defined(
11dba 4e 4f 5f 47 45 54 54 4f 44 29 0d 0a 20 20 74 69  NO_GETTOD)..  ti
11dbb 6d 65 5f 74 20 74 3b 0d 0a 20 20 74 69 6d 65 28  me_t t;..  time(
11dbc 26 74 29 3b 0d 0a 20 20 2a 70 69 4e 6f 77 20 3d  &t);..  *piNow =
11dbd 20 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34   ((sqlite3_int64
11dbe 29 74 29 2a 31 30 30 30 20 2b 20 75 6e 69 78 45  )t)*1000 + unixE
11dbf 70 6f 63 68 3b 0d 0a 23 65 6c 69 66 20 4f 53 5f  poch;..#elif OS_
11dc0 56 58 57 4f 52 4b 53 0d 0a 20 20 73 74 72 75 63  VXWORKS..  struc
11dc1 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b  t timespec sNow;
11dc2 0d 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d  ..  clock_gettim
11dc3 65 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45  e(CLOCK_REALTIME
11dc4 2c 20 26 73 4e 6f 77 29 3b 0d 0a 20 20 2a 70 69  , &sNow);..  *pi
11dc5 4e 6f 77 20 3d 20 75 6e 69 78 45 70 6f 63 68 20  Now = unixEpoch 
11dc6 2b 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  + 1000*(sqlite3_
11dc7 69 6e 74 36 34 29 73 4e 6f 77 2e 74 76 5f 73 65  int64)sNow.tv_se
11dc8 63 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63  c + sNow.tv_nsec
11dc9 2f 31 30 30 30 30 30 30 3b 0d 0a 23 65 6c 73 65  /1000000;..#else
11dca 0d 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76  ..  struct timev
11dcb 61 6c 20 73 4e 6f 77 3b 0d 0a 20 20 69 66 28 20  al sNow;..  if( 
11dcc 67 65 74 74 69 6d 65 6f 66 64 61 79 28 26 73 4e  gettimeofday(&sN
11dcd 6f 77 2c 20 30 29 3d 3d 30 20 29 7b 0d 0a 20 20  ow, 0)==0 ){..  
11dce 20 20 2a 70 69 4e 6f 77 20 3d 20 75 6e 69 78 45    *piNow = unixE
11dcf 70 6f 63 68 20 2b 20 31 30 30 30 2a 28 73 71 6c  poch + 1000*(sql
11dd0 69 74 65 33 5f 69 6e 74 36 34 29 73 4e 6f 77 2e  ite3_int64)sNow.
11dd1 74 76 5f 73 65 63 20 2b 20 73 4e 6f 77 2e 74 76  tv_sec + sNow.tv
11dd2 5f 75 73 65 63 2f 31 30 30 30 3b 0d 0a 20 20 7d  _usec/1000;..  }
11dd3 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20  else{..    rc = 
11dd4 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
11dd5 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69   }..#endif....#i
11dd6 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
11dd7 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
11dd8 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0d  current_time ){.
11dd9 0a 20 20 20 20 2a 70 69 4e 6f 77 20 3d 20 31 30  .    *piNow = 10
11dda 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  00*(sqlite3_int6
11ddb 34 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  4)sqlite3_curren
11ddc 74 5f 74 69 6d 65 20 2b 20 75 6e 69 78 45 70 6f  t_time + unixEpo
11ddd 63 68 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  ch;..  }..#endif
11dde 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
11ddf 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a  ETER(NotUsed);..
11de0 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
11de1 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74  .../*..** Find t
11de2 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
11de3 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
11de4 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
11de5 20 20 57 72 69 74 65 20 74 68 65 0d 0a 2a 2a 20    Write the..** 
11de6 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64  current time and
11de7 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61   date as a Julia
11de8 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74  n Day number int
11de9 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0d 0a 2a 2a  o *prNow and..**
11dea 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
11deb 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
11dec 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
11ded 20 62 65 20 66 6f 75 6e 64 2e 0d 0a 2a 2f 0d 0a   be found...*/..
11dee 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
11def 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
11df0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
11df1 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b   double *prNow){
11df2 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
11df3 34 20 69 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20  4 i = 0;..  int 
11df4 72 63 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  rc;..  UNUSED_PA
11df5 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
11df6 3b 0d 0a 20 20 72 63 20 3d 20 75 6e 69 78 43 75  ;..  rc = unixCu
11df7 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 30  rrentTimeInt64(0
11df8 2c 20 26 69 29 3b 0d 0a 20 20 2a 70 72 4e 6f 77  , &i);..  *prNow
11df9 20 3d 20 69 2f 38 36 34 30 30 30 30 30 2e 30 3b   = i/86400000.0;
11dfa 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
11dfb 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 65 20 61  }..../*..** We a
11dfc 64 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 73  dded the xGetLas
11dfd 74 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 20  tError() method 
11dfe 77 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 69  with the intenti
11dff 6f 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 0d  on of providing.
11e00 0a 2a 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c  .** better low-l
11e01 65 76 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61  evel error messa
11e02 67 65 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69  ges when operati
11e03 6e 67 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65  ng-system proble
11e04 6d 73 20 63 6f 6d 65 20 75 70 0d 0a 2a 2a 20 64  ms come up..** d
11e05 75 72 69 6e 67 20 53 51 4c 69 74 65 20 6f 70 65  uring SQLite ope
11e06 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20  ration.  But so 
11e07 66 61 72 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61  far, none of tha
11e08 74 20 68 61 73 20 62 65 65 6e 20 69 6d 70 6c 65  t has been imple
11e09 6d 65 6e 74 65 64 0d 0a 2a 2a 20 69 6e 20 74 68  mented..** in th
11e0a 65 20 63 6f 72 65 2e 20 20 53 6f 20 74 68 69 73  e core.  So this
11e0b 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65   routine is neve
11e0c 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f 72 20 6e  r called.  For n
11e0d 6f 77 2c 20 69 74 20 69 73 20 6d 65 72 65 6c 79  ow, it is merely
11e0e 0d 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68 6f 6c  ..** a place-hol
11e0f 64 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  der...*/..static
11e10 20 69 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74   int unixGetLast
11e11 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
11e12 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  s *NotUsed, int 
11e13 4e 6f 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a  NotUsed2, char *
11e14 4e 6f 74 55 73 65 64 33 29 7b 0d 0a 20 20 55 4e  NotUsed3){..  UN
11e15 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
11e16 6f 74 55 73 65 64 29 3b 0d 0a 20 20 55 4e 55 53  otUsed);..  UNUS
11e17 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
11e18 55 73 65 64 32 29 3b 0d 0a 20 20 55 4e 55 53 45  Used2);..  UNUSE
11e19 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
11e1a 73 65 64 33 29 3b 0d 0a 20 20 72 65 74 75 72 6e  sed3);..  return
11e1b 20 30 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a   0;..}....../*..
11e1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e1d 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
11e1e 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68  sqlite3_vfs meth
11e1f 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ods ************
11e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
11e21 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
11e22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
11e26 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
11e27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e2b 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***..***********
11e2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
11e2d 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f 63 6b  Begin Proxy Lock
11e2e 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
11e2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11e30 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 72 6f  ****..**..** Pro
11e31 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20  xy locking is a 
11e32 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65  "uber-locking-me
11e33 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65  thod" in this se
11e34 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 68  nse:  It uses th
11e35 65 0d 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b  e..** other lock
11e36 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73  ing methods on s
11e37 65 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69  econdary lock fi
11e38 6c 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b  les.  Proxy lock
11e39 69 6e 67 20 69 73 20 61 0d 0a 2a 2a 20 6d 65 74  ing is a..** met
11e3a 61 2d 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70  a-layer over top
11e3b 20 6f 66 20 74 68 65 20 70 72 69 6d 69 74 69 76   of the primitiv
11e3c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d  e locking implem
11e3d 65 6e 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f  ented above.  Fo
11e3e 72 0d 0a 2a 2a 20 74 68 69 73 20 72 65 61 73 6f  r..** this reaso
11e3f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e 20  n, the division 
11e40 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
11e41 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  of proxy locking
11e42 20 69 73 20 64 65 66 65 72 72 65 64 0d 0a 2a 2a   is deferred..**
11e43 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e 20 74   until late in t
11e44 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 20 61  he file (here) a
11e45 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  fter all of the 
11e46 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 6f 64  other I/O method
11e47 73 20 68 61 76 65 0d 0a 2a 2a 20 62 65 65 6e 20  s have..** been 
11e48 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 61  defined - so tha
11e49 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  t the primitive 
11e4a 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20  locking methods 
11e4b 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0d 0a 2a  are available..*
11e4c 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f  * as services to
11e4d 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69   help with the i
11e4e 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
11e4f 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0d   proxy locking..
11e50 0a 2a 2a 0d 0a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  .**..****..**..*
11e51 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f  * The default lo
11e52 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e  cking schemes in
11e53 20 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 65   SQLite use byte
11e54 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20  -range locks on 
11e55 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65  the..** database
11e56 20 66 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e   file to coordin
11e57 61 74 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72  ate safe, concur
11e58 72 65 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d  rent access by m
11e59 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 0d  ultiple readers.
11e5a 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 72 73 20  .** and writers 
11e5b 5b 68 74 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f  [http://sqlite.o
11e5c 72 67 2f 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d  rg/lockingv3.htm
11e5d 6c 5d 2e 20 20 54 68 65 20 66 69 76 65 20 66 69  l].  The five fi
11e5e 6c 65 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2a 20 73  le locking..** s
11e5f 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b 45 44 2c  tates (UNLOCKED,
11e60 20 50 45 4e 44 49 4e 47 2c 20 53 48 41 52 45 44   PENDING, SHARED
11e61 2c 20 52 45 53 45 52 56 45 44 2c 20 45 58 43 4c  , RESERVED, EXCL
11e62 55 53 49 56 45 29 20 61 72 65 20 69 6d 70 6c 65  USIVE) are imple
11e63 6d 65 6e 74 65 64 0d 0a 2a 2a 20 61 73 20 50 4f  mented..** as PO
11e64 53 49 58 20 72 65 61 64 20 26 20 77 72 69 74 65  SIX read & write
11e65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69 78 65   locks over fixe
11e66 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74 69 6f  d set of locatio
11e67 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29 2c 0d  ns (via fsctl),.
11e68 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20 53  .** on AFP and S
11e69 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69 76  MB only exclusiv
11e6a 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  e byte-range loc
11e6b 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ks are available
11e6c 20 76 69 61 20 66 73 63 74 6c 0d 0a 2a 2a 20 77   via fsctl..** w
11e6d 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32  ith _IOWR('z', 2
11e6e 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61  3, struct ByteRa
11e6f 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74  ngeLockPB2) to t
11e70 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20  rack the same 5 
11e71 73 74 61 74 65 73 2e 0d 0a 2a 2a 20 54 6f 20 73  states...** To s
11e72 69 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43  imulate a F_RDLC
11e73 4b 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20  K on the shared 
11e74 72 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20  range, on AFP a 
11e75 72 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65  randomly selecte
11e76 64 0d 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e  d..** address in
11e77 20 74 68 65 20 73 68 61 72 65 64 20 72 61 6e 67   the shared rang
11e78 65 20 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61  e is taken for a
11e79 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
11e7a 65 20 65 6e 74 69 72 65 0d 0a 2a 2a 20 73 68 61  e entire..** sha
11e7b 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b  red range is tak
11e7c 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 4c 55 53  en for an EXCLUS
11e7d 49 56 45 20 6c 6f 63 6b 29 3a 0d 0a 2a 2a 0d 0a  IVE lock):..**..
11e7e 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f  **      PENDING_
11e7f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34 30  BYTE        0x40
11e80 30 30 30 30 30 30 09 09 20 20 20 09 0d 0a 2a 2a  000000..   ...**
11e81 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42        RESERVED_B
11e82 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30  YTE       0x4000
11e83 30 30 30 31 0d 0a 2a 2a 20 20 20 20 20 20 53 48  0001..**      SH
11e84 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20  ARED_RANGE      
11e85 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20    0x40000002 -> 
11e86 30 78 34 30 30 30 30 32 30 30 0d 0a 2a 2a 0d 0a  0x40000200..**..
11e87 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65  ** This works we
11e88 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ll on the local 
11e89 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74  file system, but
11e8a 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20   shows a nearly 
11e8b 31 30 30 78 0d 0a 2a 2a 20 73 6c 6f 77 64 6f 77  100x..** slowdow
11e8c 6e 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f 72  n in read perfor
11e8d 6d 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 63  mance on AFP bec
11e8e 61 75 73 65 20 74 68 65 20 41 46 50 20 63 6c 69  ause the AFP cli
11e8f 65 6e 74 20 64 69 73 61 62 6c 65 73 0d 0a 2a 2a  ent disables..**
11e90 20 74 68 65 20 72 65 61 64 20 63 61 63 68 65 20   the read cache 
11e91 77 68 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20  when byte-range 
11e92 6c 6f 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e  locks are presen
11e93 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65  t.  Enabling the
11e94 20 72 65 61 64 0d 0a 2a 2a 20 63 61 63 68 65 20   read..** cache 
11e95 65 78 70 6f 73 65 73 20 61 20 63 61 63 68 65 20  exposes a cache 
11e96 63 6f 68 65 72 65 6e 63 79 20 70 72 6f 62 6c 65  coherency proble
11e97 6d 20 74 68 61 74 20 69 73 20 70 72 65 73 65 6e  m that is presen
11e98 74 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0d 0a 2a  t on all OS X..*
11e99 2a 20 73 75 70 70 6f 72 74 65 64 20 6e 65 74 77  * supported netw
11e9a 6f 72 6b 20 66 69 6c 65 20 73 79 73 74 65 6d 73  ork file systems
11e9b 2e 20 20 4e 46 53 20 61 6e 64 20 41 46 50 20 62  .  NFS and AFP b
11e9c 6f 74 68 20 6f 62 73 65 72 76 65 20 74 68 65 0d  oth observe the.
11e9d 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65  .** close-to-ope
11e9e 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f 72 20  n semantics for 
11e9f 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65 20 63  ensuring cache c
11ea0 6f 68 65 72 65 6e 63 79 0d 0a 2a 2a 20 5b 68 74  oherency..** [ht
11ea1 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65 66  tp://nfs.sourcef
11ea2 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61 38  orge.net/#faq_a8
11ea3 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f  ], which does no
11ea4 74 20 65 66 66 65 63 74 69 76 65 6c 79 0d 0a 2a  t effectively..*
11ea5 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 72 65  * address the re
11ea6 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63  quirements for c
11ea7 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 62 61  oncurrent databa
11ea8 73 65 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c  se access by mul
11ea9 74 69 70 6c 65 0d 0a 2a 2a 20 72 65 61 64 65 72  tiple..** reader
11eaa 73 20 61 6e 64 20 77 72 69 74 65 72 73 0d 0a 2a  s and writers..*
11eab 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61  * [http://www.na
11eac 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d  bble.com/SQLite-
11ead 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68  on-NFS-cache-coh
11eae 65 72 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30  erency-td1565570
11eaf 31 2e 68 74 6d 6c 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a  1.html]...**..**
11eb0 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 65 20   To address the 
11eb1 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 20  performance and 
11eb2 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20  cache coherency 
11eb3 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 66 69  issues, proxy fi
11eb4 6c 65 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2a 20 63  le locking..** c
11eb5 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 64  hanges the way d
11eb6 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 69  atabase access i
11eb7 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  s controlled by 
11eb8 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 20  limiting access 
11eb9 74 6f 20 61 0d 0a 2a 2a 20 73 69 6e 67 6c 65 20  to a..** single 
11eba 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61  host at a time a
11ebb 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c  nd moving file l
11ebc 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20  ocks off of the 
11ebd 64 61 74 61 62 61 73 65 20 66 69 6c 65 0d 0a 2a  database file..*
11ebe 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f  * and onto a pro
11ebf 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c  xy file on the l
11ec0 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  ocal file system
11ec1 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  .  ..**..**..** 
11ec2 55 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b  Using proxy lock
11ec3 73 0d 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s..** ----------
11ec4 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20  -------..**..** 
11ec5 43 20 41 50 49 73 0d 0a 2a 2a 0d 0a 2a 2a 20 20  C APIs..**..**  
11ec6 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
11ec7 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 2c  trol(db, dbname,
11ec8 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b   SQLITE_SET_LOCK
11ec9 50 52 4f 58 59 46 49 4c 45 2c 0d 0a 2a 2a 20 20  PROXYFILE,..**  
11eca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ecb 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68       <proxy_path
11ecc 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0d 0a  > | ":auto:");..
11ecd 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  **  sqlite3_file
11ece 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e  _control(db, dbn
11ecf 61 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f  ame, SQLITE_GET_
11ed0 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26  LOCKPROXYFILE, &
11ed1 3c 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0d 0a  <proxy_path>);..
11ed2 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 20 70  **..**..** SQL p
11ed3 72 61 67 6d 61 73 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ragmas..**..**  
11ed4 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
11ed5 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  .]lock_proxy_fil
11ed6 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c  e=<proxy_path> |
11ed7 20 3a 61 75 74 6f 3a 0d 0a 2a 2a 20 20 50 52 41   :auto:..**  PRA
11ed8 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
11ed9 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0d 0a  ock_proxy_file..
11eda 2a 2a 0d 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e  **..** Specifyin
11edb 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73  g ":auto:" means
11edc 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69   that if there i
11edd 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77  s a conch file w
11ede 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 0d 0a  ith a matching..
11edf 2a 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 74  ** host ID in it
11ee0 2c 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 68  , the proxy path
11ee1 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69   in the conch fi
11ee2 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c  le will be used,
11ee3 20 6f 74 68 65 72 77 69 73 65 0d 0a 2a 2a 20 61   otherwise..** a
11ee4 20 70 72 6f 78 79 20 70 61 74 68 20 62 61 73 65   proxy path base
11ee5 64 20 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20  d on the user's 
11ee6 74 65 6d 70 20 64 69 72 0d 0a 2a 2a 20 28 76 69  temp dir..** (vi
11ee7 61 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41  a confstr(_CS_DA
11ee8 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44  RWIN_USER_TEMP_D
11ee9 49 52 2c 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65  IR,...)) will be
11eea 20 75 73 65 64 20 61 6e 64 20 74 68 65 0d 0a 2a   used and the..*
11eeb 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 79 20 66  * actual proxy f
11eec 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 65 6e 65  ile name is gene
11eed 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6e  rated from the n
11eee 61 6d 65 20 61 6e 64 20 70 61 74 68 20 6f 66 20  ame and path of 
11eef 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65  the..** database
11ef0 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d   file.  For exam
11ef1 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ple:..**..**    
11ef2 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20     For database 
11ef3 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f  path "/Users/me/
11ef4 66 6f 6f 2e 64 62 22 20 0d 0a 2a 2a 20 20 20 20  foo.db" ..**    
11ef5 20 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68     The lock path
11ef6 20 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69   will be "<tmpdi
11ef7 72 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f  r>/sqliteplocks/
11ef8 5f 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62  _Users_me_foo.db
11ef9 3a 61 75 74 6f 3a 22 29 0d 0a 2a 2a 0d 0a 2a 2a  :auto:")..**..**
11efa 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f   Once a lock pro
11efb 78 79 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64  xy is configured
11efc 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20   for a database 
11efd 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63  connection, it c
11efe 61 6e 20 6e 6f 74 0d 0a 2a 2a 20 62 65 20 72 65  an not..** be re
11eff 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 20 69  moved, however i
11f00 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 68 65  t may be switche
11f01 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74  d to a different
11f02 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 61 0d   proxy path via.
11f03 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 50  .** the above AP
11f04 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65  Is (assuming the
11f05 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 6e   conch file is n
11f06 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 79  ot being held by
11f07 20 61 6e 6f 74 68 65 72 0d 0a 2a 2a 20 63 6f 6e   another..** con
11f08 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65  nection or proce
11f09 73 73 29 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a  ss). ..**..**..*
11f0a 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b  * How proxy lock
11f0b 69 6e 67 20 77 6f 72 6b 73 0d 0a 2a 2a 20 2d 2d  ing works..** --
11f0c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f0d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20 50 72  -----..**..** Pr
11f0e 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  oxy file locking
11f0f 20 72 65 6c 69 65 73 20 70 72 69 6d 61 72 69 6c   relies primaril
11f10 79 20 6f 6e 20 74 77 6f 20 6e 65 77 20 73 75 70  y on two new sup
11f11 70 6f 72 74 69 6e 67 20 66 69 6c 65 73 3a 20 0d  porting files: .
11f12 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20 63 6f 6e  .**..**   *  con
11f13 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d 69 74  ch file to limit
11f14 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64   access to the d
11f15 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
11f16 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0d 0a 2a  a single host..*
11f17 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d 65  *      at a time
11f18 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20 70 72  ..**..**   *  pr
11f19 6f 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20  oxy file to act 
11f1a 61 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74  as a proxy for t
11f1b 68 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  he advisory lock
11f1c 73 20 6e 6f 72 6d 61 6c 6c 79 0d 0a 2a 2a 20 20  s normally..**  
11f1d 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65      taken on the
11f1e 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 0d 0a 2a   database..**..*
11f1f 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  * The conch file
11f20 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 6f 78   - to use a prox
11f21 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 6d  y file, sqlite m
11f22 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c 64 20  ust first "hold 
11f23 74 68 65 20 63 6f 6e 63 68 22 0d 0a 2a 2a 20 62  the conch"..** b
11f24 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69  y taking an sqli
11f25 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 20  te-style shared 
11f26 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63  lock on the conc
11f27 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 20  h file, reading 
11f28 74 68 65 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73  the..** contents
11f29 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74   and comparing t
11f2a 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65  he host's unique
11f2b 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65   host ID (see be
11f2c 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0d 0a 2a  low) and lock..*
11f2d 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61  * proxy path aga
11f2e 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20  inst the values 
11f2f 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f  stored in the co
11f30 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20  nch.  The conch 
11f31 66 69 6c 65 20 69 73 0d 0a 2a 2a 20 73 74 6f 72  file is..** stor
11f32 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64  ed in the same d
11f33 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 20  irectory as the 
11f34 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
11f35 64 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0d  d the file name.
11f36 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64  .** is patterned
11f37 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62   after the datab
11f38 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73  ase file name as
11f39 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65   ".<databasename
11f3a 3e 2d 63 6f 6e 63 68 22 2e 0d 0a 2a 2a 20 49 66  >-conch"...** If
11f3b 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
11f3c 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
11f3d 6f 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 73  or it's contents
11f3e 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 68   do not match th
11f3f 65 0d 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e  e..** host ID an
11f40 64 2f 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c  d/or proxy path,
11f41 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69   then the lock i
11f42 73 20 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61  s escalated to a
11f43 6e 20 65 78 63 6c 75 73 69 76 65 0d 0a 2a 2a 20  n exclusive..** 
11f44 6c 6f 63 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e  lock and the con
11f45 63 68 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73  ch file contents
11f46 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68   is updated with
11f47 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64   the host ID and
11f48 20 70 72 6f 78 79 0d 0a 2a 2a 20 70 61 74 68 20   proxy..** path 
11f49 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  and the lock is 
11f4a 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20  downgraded to a 
11f4b 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 67 61 69  shared lock agai
11f4c 6e 2e 20 20 49 66 20 74 68 65 20 63 6f 6e 63 68  n.  If the conch
11f4d 0d 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 79 20  ..** is held by 
11f4e 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  another process 
11f4f 28 77 69 74 68 20 61 20 73 68 61 72 65 64 20 6c  (with a shared l
11f50 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c 75 73  ock), the exclus
11f51 69 76 65 20 6c 6f 63 6b 0d 0a 2a 2a 20 77 69 6c  ive lock..** wil
11f52 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 54  l fail and SQLIT
11f53 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e  E_BUSY is return
11f54 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
11f55 70 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73  proxy file - a s
11f56 69 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20  ingle-byte file 
11f57 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76  used for all adv
11f58 69 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73  isory file locks
11f59 0d 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61  ..** normally ta
11f5a 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ken on the datab
11f5b 61 73 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73  ase file.   This
11f5c 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65   allows for safe
11f5d 20 73 68 61 72 69 6e 67 0d 0a 2a 2a 20 6f 66 20   sharing..** of 
11f5e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11f5f 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72  e for multiple r
11f60 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65  eaders and write
11f61 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 0d 0a  rs on the same..
11f62 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 6f 6e  ** host (the con
11f63 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  ch ensures that 
11f64 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 68 65  they all use the
11f65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b   same local lock
11f66 20 66 69 6c 65 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   file)...**..** 
11f67 52 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 6c  Requesting the l
11f68 6f 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20 6e  ock proxy does n
11f69 6f 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  ot immediately t
11f6a 61 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20 69  ake the conch, i
11f6b 74 20 69 73 0d 0a 2a 2a 20 6f 6e 6c 79 20 74 61  t is..** only ta
11f6c 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72  ken when the fir
11f6d 73 74 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f  st request to lo
11f6e 63 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ck database file
11f6f 20 69 73 20 6d 61 64 65 2e 20 20 0d 0a 2a 2a 20   is made.  ..** 
11f70 54 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65  This matches the
11f71 20 73 65 6d 61 6e 74 69 63 73 20 6f 66 20 74 68   semantics of th
11f72 65 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f  e traditional lo
11f73 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 2c 20  cking behavior, 
11f74 77 68 65 72 65 0d 0a 2a 2a 20 6f 70 65 6e 69 6e  where..** openin
11f75 67 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  g a connection t
11f76 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  o a database fil
11f77 65 20 64 6f 65 73 20 6e 6f 74 20 74 61 6b 65 20  e does not take 
11f78 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0d 0a 2a  a lock on it...*
11f79 2a 20 54 68 65 20 73 68 61 72 65 64 20 6c 6f 63  * The shared loc
11f7a 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20 66 69  k and an open fi
11f7b 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 72  le descriptor ar
11f7c 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 75 6e 74  e maintained unt
11f7d 69 6c 20 0d 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e  il ..** the conn
11f7e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 61  ection to the da
11f7f 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64  tabase is closed
11f80 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70  . ..**..** The p
11f81 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68  roxy file and th
11f82 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20  e lock file are 
11f83 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f  never deleted so
11f84 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0d   they only need.
11f85 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65  .** to be create
11f86 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  d the first time
11f87 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0d   they are used..
11f88 0a 2a 2a 0d 0a 2a 2a 20 43 6f 6e 66 69 67 75 72  .**..** Configur
11f89 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0d 0a 2a  ation options..*
11f8a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
11f8b 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20  -------..**..** 
11f8c 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50   SQLITE_PREFER_P
11f8d 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0d 0a 2a 2a  ROXY_LOCKING..**
11f8e 0d 0a 2a 2a 20 20 20 20 20 20 20 44 61 74 61 62  ..**       Datab
11f8f 61 73 65 20 66 69 6c 65 73 20 61 63 63 65 73 73  ase files access
11f90 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20  ed on non-local 
11f91 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61 72 65  file systems are
11f92 0d 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f 6d  ..**       autom
11f93 61 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67 75  atically configu
11f94 72 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f  red for proxy lo
11f95 63 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c 65  cking, lock file
11f96 73 20 61 72 65 0d 0a 2a 2a 20 20 20 20 20 20 20  s are..**       
11f97 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61  named automatica
11f98 6c 6c 79 20 75 73 69 6e 67 20 74 68 65 20 73 61  lly using the sa
11f99 6d 65 20 6c 6f 67 69 63 20 61 73 0d 0a 2a 2a 20  me logic as..** 
11f9a 20 20 20 20 20 20 50 52 41 47 4d 41 20 6c 6f 63        PRAGMA loc
11f9b 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61  k_proxy_file=":a
11f9c 75 74 6f 3a 22 0d 0a 2a 2a 20 20 20 20 0d 0a 2a  uto:"..**    ..*
11f9d 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f  *  SQLITE_PROXY_
11f9e 44 45 42 55 47 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  DEBUG..**..**   
11f9f 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 20      Enables the 
11fa0 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f 72  logging of error
11fa1 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e 67   messages during
11fa2 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0d 0a 2a   host id file..*
11fa3 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61  *       retrieva
11fa4 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0d 0a  l and creation..
11fa5 2a 2a 0d 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58  **..**  LOCKPROX
11fa6 59 44 49 52 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  YDIR..**..**    
11fa7 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 65     Overrides the
11fa8 20 64 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f   default directo
11fa9 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  ry used for lock
11faa 20 70 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61   proxy files tha
11fab 74 0d 0a 2a 2a 20 20 20 20 20 20 20 61 72 65 20  t..**       are 
11fac 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61  named automatica
11fad 6c 6c 79 20 76 69 61 20 74 68 65 20 22 3a 61 75  lly via the ":au
11fae 74 6f 3a 22 20 73 65 74 74 69 6e 67 0d 0a 2a 2a  to:" setting..**
11faf 0d 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44 45 46  ..**  SQLITE_DEF
11fb0 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45  AULT_PROXYDIR_PE
11fb1 52 4d 49 53 53 49 4f 4e 53 0d 0a 2a 2a 0d 0a 2a  RMISSIONS..**..*
11fb2 2a 20 20 20 20 20 20 20 50 65 72 6d 69 73 73 69  *       Permissi
11fb3 6f 6e 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20  ons to use when 
11fb4 63 72 65 61 74 69 6e 67 20 61 20 64 69 72 65 63  creating a direc
11fb5 74 6f 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67  tory for storing
11fb6 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20 20 20 6c   the..**       l
11fb7 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 2c  ock proxy files,
11fb8 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
11fb9 4c 4f 43 4b 50 52 4f 58 59 44 49 52 20 69 73 20  LOCKPROXYDIR is 
11fba 6e 6f 74 20 73 65 74 2e 0d 0a 2a 2a 20 20 20 20  not set...**    
11fbb 0d 0a 2a 2a 20 20 20 20 0d 0a 2a 2a 20 41 73 20  ..**    ..** As 
11fbc 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 65 2c  mentioned above,
11fbd 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 77   when compiled w
11fbe 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 46 45  ith SQLITE_PREFE
11fbf 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c  R_PROXY_LOCKING,
11fc0 0d 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 65  ..** setting the
11fc1 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72   environment var
11fc2 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f 52  iable SQLITE_FOR
11fc3 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  CE_PROXY_LOCKING
11fc4 20 74 6f 20 31 20 77 69 6c 6c 0d 0a 2a 2a 20 66   to 1 will..** f
11fc5 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69  orce proxy locki
11fc6 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f  ng to be used fo
11fc7 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65  r every database
11fc8 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e   file opened, an
11fc9 64 20 30 0d 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72  d 0..** will for
11fca 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f  ce automatic pro
11fcb 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65  xy locking to be
11fcc 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c   disabled for al
11fcd 6c 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 66  l database..** f
11fce 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 20  iles (explicity 
11fcf 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49  calling the SQLI
11fd0 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
11fd1 46 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0d 0a  FILE pragma or..
11fd2 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63  ** sqlite_file_c
11fd3 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f  ontrol API is no
11fd4 74 20 61 66 66 65 63 74 65 64 20 62 79 20 53 51  t affected by SQ
11fd5 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
11fd6 5f 4c 4f 43 4b 49 4e 47 29 2e 0d 0a 2a 2f 0d 0a  _LOCKING)...*/..
11fd7 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 6f 78 79 20 6c  ../*..** Proxy l
11fd8 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61  ocking is only a
11fd9 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f  vailable on MacO
11fda 53 58 20 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66  SX ..*/..#if def
11fdb 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
11fdc 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
11fdd 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0d 0a  _LOCKING_STYLE..
11fde 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 70 72 6f  ../*..** The pro
11fdf 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
11fe0 20 68 61 73 20 74 68 65 20 70 61 74 68 20 61 6e   has the path an
11fe1 64 20 66 69 6c 65 20 73 74 72 75 63 74 75 72 65  d file structure
11fe2 73 20 66 6f 72 20 74 68 65 20 72 65 6d 6f 74 65  s for the remote
11fe3 20 0d 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20   ..** and local 
11fe4 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e 20 69  proxy files in i
11fe5 74 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73  t..*/..typedef s
11fe6 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69  truct proxyLocki
11fe7 6e 67 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c  ngContext proxyL
11fe8 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a  ockingContext;..
11fe9 73 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b  struct proxyLock
11fea 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0d 0a 20 20  ingContext {..  
11feb 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46  unixFile *conchF
11fec 69 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ile;         /* 
11fed 4f 70 65 6e 20 63 6f 6e 63 68 20 66 69 6c 65 20  Open conch file 
11fee 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63  */..  char *conc
11fef 68 46 69 6c 65 50 61 74 68 3b 20 20 20 20 20 20  hFilePath;      
11ff0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
11ff1 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0d  e conch file */.
11ff2 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63  .  unixFile *loc
11ff3 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 20 20  kProxy;         
11ff4 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 6c 6f  /* Open proxy lo
11ff5 63 6b 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 63 68  ck file */..  ch
11ff6 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 74  ar *lockProxyPat
11ff7 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  h;         /* Na
11ff8 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20  me of the proxy 
11ff9 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  lock file */..  
11ffa 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20  char *dbPath;   
11ffb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11ffc 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e  Name of the open
11ffd 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   file */..  int 
11ffe 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20  conchHeld;      
11fff 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 69 66           /* 1 if
12000 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 68 65   the conch is he
12001 6c 64 2c 20 2d 31 20 69 66 20 6c 6f 63 6b 6c 65  ld, -1 if lockle
12002 73 73 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 6f  ss */..  void *o
12003 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ldLockingContext
12004 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61  ;     /* Origina
12005 6c 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74  l lockingcontext
12006 20 74 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63   to restore on c
12007 6c 6f 73 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  lose */..  sqlit
12008 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f  e3_io_methods co
12009 6e 73 74 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b  nst *pOldMethod;
1200a 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
1200b 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72   I/O methods for
1200c 20 63 6c 6f 73 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d   close */..};...
1200d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 65 20 70 72 6f  ./* ..** The pro
1200e 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  xy lock file pat
1200f 68 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  h for the databa
12010 73 65 20 61 74 20 64 62 50 61 74 68 20 69 73 20  se at dbPath is 
12011 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 6c 50 61  written into lPa
12012 74 68 2c 20 0d 0a 2a 2a 20 77 68 69 63 68 20 6d  th, ..** which m
12013 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 76 61 6c  ust point to val
12014 69 64 2c 20 77 72 69 74 61 62 6c 65 20 6d 65 6d  id, writable mem
12015 6f 72 79 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  ory large enough
12016 20 66 6f 72 20 61 20 6d 61 78 4c 65 6e 20 6c 65   for a maxLen le
12017 6e 67 74 68 0d 0a 2a 2a 20 66 69 6c 65 20 70 61  ngth..** file pa
12018 74 68 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  th. ..*/..static
12019 20 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63   int proxyGetLoc
1201a 6b 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72  kPath(const char
1201b 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a   *dbPath, char *
1201c 6c 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61  lPath, size_t ma
1201d 78 4c 65 6e 29 7b 0d 0a 20 20 69 6e 74 20 6c 65  xLen){..  int le
1201e 6e 3b 0d 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b  n;..  int dbLen;
1201f 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 23 69  ..  int i;....#i
12020 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49  fdef LOCKPROXYDI
12021 52 0d 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  R..  len = strlc
12022 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52  py(lPath, LOCKPR
12023 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b  OXYDIR, maxLen);
12024 0d 0a 23 65 6c 73 65 0d 0a 23 20 69 66 64 65 66  ..#else..# ifdef
12025 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52   _CS_DARWIN_USER
12026 5f 54 45 4d 50 5f 44 49 52 0d 0a 20 20 7b 0d 0a  _TEMP_DIR..  {..
12027 20 20 20 20 69 66 28 20 21 63 6f 6e 66 73 74 72      if( !confstr
12028 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52  (_CS_DARWIN_USER
12029 5f 54 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68  _TEMP_DIR, lPath
1202a 2c 20 6d 61 78 4c 65 6e 29 20 29 7b 0d 0a 20 20  , maxLen) ){..  
1202b 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 47 45      OSTRACE(("GE
1202c 54 4c 4f 43 4b 50 41 54 48 20 20 66 61 69 6c 65  TLOCKPATH  faile
1202d 64 20 25 73 20 65 72 72 6e 6f 3d 25 64 20 70 69  d %s errno=%d pi
1202e 64 3d 25 64 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  d=%d\n",..      
1202f 20 20 20 20 20 20 20 20 20 6c 50 61 74 68 2c 20           lPath, 
12030 65 72 72 6e 6f 2c 20 67 65 74 70 69 64 28 29 29  errno, getpid())
12031 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  );..      return
12032 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
12033 43 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  CK;..    }..    
12034 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50  len = strlcat(lP
12035 61 74 68 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63  ath, "sqliteploc
12036 6b 73 22 2c 20 6d 61 78 4c 65 6e 29 3b 20 20 20  ks", maxLen);   
12037 20 0d 0a 20 20 7d 0d 0a 23 20 65 6c 73 65 0d 0a   ..  }..# else..
12038 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28    len = strlcpy(
12039 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20  lPath, "/tmp/", 
1203a 6d 61 78 4c 65 6e 29 3b 0d 0a 23 20 65 6e 64 69  maxLen);..# endi
1203b 66 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 69  f..#endif....  i
1203c 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d 21  f( lPath[len-1]!
1203d 3d 27 2f 27 20 29 7b 0d 0a 20 20 20 20 6c 65 6e  ='/' ){..    len
1203e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68   = strlcat(lPath
1203f 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0d  , "/", maxLen);.
12040 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 74  .  }..  ..  /* t
12041 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20  ransform the db 
12042 70 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65  path to a unique
12043 20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0d 0a   cache name */..
12044 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73    dbLen = (int)s
12045 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 0d 0a  trlen(dbPath);..
12046 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62    for( i=0; i<db
12047 4c 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29  Len && (i+len+7)
12048 3c 28 69 6e 74 29 6d 61 78 4c 65 6e 3b 20 69 2b  <(int)maxLen; i+
12049 2b 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 63 20  +){..    char c 
1204a 3d 20 64 62 50 61 74 68 5b 69 5d 3b 0d 0a 20 20  = dbPath[i];..  
1204b 20 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d    lPath[i+len] =
1204c 20 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b   (c=='/')?'_':c;
1204d 0d 0a 20 20 7d 0d 0a 20 20 6c 50 61 74 68 5b 69  ..  }..  lPath[i
1204e 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0d 0a 20 20 73  +len]='\0';..  s
1204f 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 3a  trlcat(lPath, ":
12050 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b  auto:", maxLen);
12051 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 47 45  ..  OSTRACE(("GE
12052 54 4c 4f 43 4b 50 41 54 48 20 20 70 72 6f 78 79  TLOCKPATH  proxy
12053 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 20 70 69   lock path=%s pi
12054 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20  d=%d\n", lPath, 
12055 67 65 74 70 69 64 28 29 29 29 3b 0d 0a 20 20 72  getpid()));..  r
12056 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12057 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 20 2a 2a 20  ..}..../* .. ** 
12058 43 72 65 61 74 65 73 20 74 68 65 20 6c 6f 63 6b  Creates the lock
12059 20 66 69 6c 65 20 61 6e 64 20 61 6e 79 20 6d 69   file and any mi
1205a 73 73 69 6e 67 20 64 69 72 65 63 74 6f 72 69 65  ssing directorie
1205b 73 20 69 6e 20 6c 6f 63 6b 50 61 74 68 0d 0a 20  s in lockPath.. 
1205c 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  */..static int p
1205d 72 6f 78 79 43 72 65 61 74 65 4c 6f 63 6b 50 61  roxyCreateLockPa
1205e 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c  th(const char *l
1205f 6f 63 6b 50 61 74 68 29 7b 0d 0a 20 20 69 6e 74  ockPath){..  int
12060 20 69 2c 20 6c 65 6e 3b 0d 0a 20 20 63 68 61 72   i, len;..  char
12061 20 62 75 66 5b 4d 41 58 50 41 54 48 4c 45 4e 5d   buf[MAXPATHLEN]
12062 3b 0d 0a 20 20 69 6e 74 20 73 74 61 72 74 20 3d  ;..  int start =
12063 20 30 3b 0d 0a 20 20 0d 0a 20 20 61 73 73 65 72   0;..  ..  asser
12064 74 28 6c 6f 63 6b 50 61 74 68 21 3d 4e 55 4c 4c  t(lockPath!=NULL
12065 29 3b 0d 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20  );..  /* try to 
12066 63 72 65 61 74 65 20 61 6c 6c 20 74 68 65 20 69  create all the i
12067 6e 74 65 72 6d 65 64 69 61 74 65 20 64 69 72 65  ntermediate dire
12068 63 74 6f 72 69 65 73 20 2a 2f 0d 0a 20 20 6c 65  ctories */..  le
12069 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  n = (int)strlen(
1206a 6c 6f 63 6b 50 61 74 68 29 3b 0d 0a 20 20 62 75  lockPath);..  bu
1206b 66 5b 30 5d 20 3d 20 6c 6f 63 6b 50 61 74 68 5b  f[0] = lockPath[
1206c 30 5d 3b 0d 0a 20 20 66 6f 72 28 20 69 3d 31 3b  0];..  for( i=1;
1206d 20 69 3c 6c 65 6e 3b 20 69 2b 2b 20 29 7b 0d 0a   i<len; i++ ){..
1206e 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 61 74 68      if( lockPath
1206f 5b 69 5d 20 3d 3d 20 27 2f 27 20 26 26 20 28 69  [i] == '/' && (i
12070 20 2d 20 73 74 61 72 74 20 3e 20 30 29 20 29 7b   - start > 0) ){
12071 0d 0a 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20  ..      /* only 
12072 6d 6b 64 69 72 20 69 66 20 6c 65 61 66 20 64 69  mkdir if leaf di
12073 72 20 21 3d 20 22 2e 22 20 6f 72 20 22 2f 22 20  r != "." or "/" 
12074 6f 72 20 22 2e 2e 22 20 2a 2f 0d 0a 20 20 20 20  or ".." */..    
12075 20 20 69 66 28 20 69 2d 73 74 61 72 74 3e 32 20    if( i-start>2 
12076 7c 7c 20 28 69 2d 73 74 61 72 74 3d 3d 31 20 26  || (i-start==1 &
12077 26 20 62 75 66 5b 73 74 61 72 74 5d 20 21 3d 20  & buf[start] != 
12078 27 2e 27 20 26 26 20 62 75 66 5b 73 74 61 72 74  '.' && buf[start
12079 5d 20 21 3d 20 27 2f 27 29 20 0d 0a 20 20 20 20  ] != '/') ..    
1207a 20 20 20 20 20 7c 7c 20 28 69 2d 73 74 61 72 74       || (i-start
1207b 3d 3d 32 20 26 26 20 62 75 66 5b 73 74 61 72 74  ==2 && buf[start
1207c 5d 20 21 3d 20 27 2e 27 20 26 26 20 62 75 66 5b  ] != '.' && buf[
1207d 73 74 61 72 74 2b 31 5d 20 21 3d 20 27 2e 27 29  start+1] != '.')
1207e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 62 75 66   ){..        buf
1207f 5b 69 5d 3d 27 5c 30 27 3b 0d 0a 20 20 20 20 20  [i]='\0';..     
12080 20 20 20 69 66 28 20 6f 73 4d 6b 64 69 72 28 62     if( osMkdir(b
12081 75 66 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55  uf, SQLITE_DEFAU
12082 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d  LT_PROXYDIR_PERM
12083 49 53 53 49 4f 4e 53 29 20 29 7b 0d 0a 20 20 20  ISSIONS) ){..   
12084 20 20 20 20 20 20 20 69 6e 74 20 65 72 72 3d 65         int err=e
12085 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20 20 20  rrno;..         
12086 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53 54   if( err!=EEXIST
12087 20 29 20 7b 0d 0a 20 20 20 20 20 20 20 20 20 20   ) {..          
12088 20 20 4f 53 54 52 41 43 45 28 28 22 43 52 45 41    OSTRACE(("CREA
12089 54 45 4c 4f 43 4b 50 41 54 48 20 20 46 41 49 4c  TELOCKPATH  FAIL
1208a 45 44 20 63 72 65 61 74 69 6e 67 20 25 73 2c 20  ED creating %s, 
1208b 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  "..             
1208c 20 20 20 20 20 20 20 20 22 27 25 73 27 20 70 72          "'%s' pr
1208d 6f 78 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73  oxy lock path=%s
1208e 20 70 69 64 3d 25 64 5c 6e 22 2c 0d 0a 20 20 20   pid=%d\n",..   
1208f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12090 20 20 62 75 66 2c 20 73 74 72 65 72 72 6f 72 28    buf, strerror(
12091 65 72 72 29 2c 20 6c 6f 63 6b 50 61 74 68 2c 20  err), lockPath, 
12092 67 65 74 70 69 64 28 29 29 29 3b 0d 0a 20 20 20  getpid()));..   
12093 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
12094 65 72 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  err;..          
12095 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  }..        }..  
12096 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 74 61      }..      sta
12097 72 74 3d 69 2b 31 3b 0d 0a 20 20 20 20 7d 0d 0a  rt=i+1;..    }..
12098 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 6c 6f 63      buf[i] = loc
12099 6b 50 61 74 68 5b 69 5d 3b 0d 0a 20 20 7d 0d 0a  kPath[i];..  }..
1209a 20 20 4f 53 54 52 41 43 45 28 28 22 43 52 45 41    OSTRACE(("CREA
1209b 54 45 4c 4f 43 4b 50 41 54 48 20 20 70 72 6f 78  TELOCKPATH  prox
1209c 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 20 70  y lock path=%s p
1209d 69 64 3d 25 64 5c 6e 22 2c 20 6c 6f 63 6b 50 61  id=%d\n", lockPa
1209e 74 68 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0d  th, getpid()));.
1209f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d  .  return 0;..}.
120a0 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74 65  .../*..** Create
120a1 20 61 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20   a new VFS file 
120a2 64 65 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72  descriptor (stor
120a3 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ed in memory obt
120a4 61 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 73  ained from..** s
120a5 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61  qlite3_malloc) a
120a6 6e 64 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65  nd open the file
120a7 20 6e 61 6d 65 64 20 22 70 61 74 68 22 20 69 6e   named "path" in
120a8 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
120a9 70 74 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ptor...**..** Th
120aa 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
120ab 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c 79  onsible not only
120ac 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 74 68 65   for closing the
120ad 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
120ae 0d 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 66 6f  ..** but also fo
120af 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d 65  r freeing the me
120b0 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
120b1 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 64 65  with the file de
120b2 73 63 72 69 70 74 6f 72 2e 0d 0a 2a 2f 0d 0a 73  scriptor...*/..s
120b3 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
120b4 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 0d 0a  reateUnixFile(..
120b5 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
120b6 70 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a 20  path,        /* 
120b7 70 61 74 68 20 66 6f 72 20 74 68 65 20 6e 65 77  path for the new
120b8 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0d 0a 20 20   unixFile */..  
120b9 20 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46    unixFile **ppF
120ba 69 6c 65 2c 20 20 20 20 20 20 20 2f 2a 20 75 6e  ile,       /* un
120bb 69 78 46 69 6c 65 20 63 72 65 61 74 65 64 20 61  ixFile created a
120bc 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 72  nd returned by r
120bd 65 66 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69  ef */..    int i
120be 73 6c 6f 63 6b 66 69 6c 65 20 20 20 20 20 20 20  slockfile       
120bf 20 20 20 20 2f 2a 20 69 66 20 6e 6f 6e 20 7a 65      /* if non ze
120c0 72 6f 20 6d 69 73 73 69 6e 67 20 64 69 72 73 20  ro missing dirs 
120c1 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20  will be created 
120c2 2a 2f 0d 0a 29 20 7b 0d 0a 20 20 69 6e 74 20 66  */..) {..  int f
120c3 64 20 3d 20 2d 31 3b 0d 0a 20 20 75 6e 69 78 46  d = -1;..  unixF
120c4 69 6c 65 20 2a 70 4e 65 77 3b 0d 0a 20 20 69 6e  ile *pNew;..  in
120c5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
120c6 3b 0d 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61  ;..  int openFla
120c7 67 73 20 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f  gs = O_RDWR | O_
120c8 43 52 45 41 54 3b 0d 0a 20 20 73 71 6c 69 74 65  CREAT;..  sqlite
120c9 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0d  3_vfs dummyVfs;.
120ca 0a 20 20 69 6e 74 20 74 65 72 72 6e 6f 20 3d 20  .  int terrno = 
120cb 30 3b 0d 0a 20 20 55 6e 69 78 55 6e 75 73 65 64  0;..  UnixUnused
120cc 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 4e 55  Fd *pUnused = NU
120cd 4c 4c 3b 0d 0a 0d 0a 20 20 2f 2a 20 31 2e 20 66  LL;....  /* 1. f
120ce 69 72 73 74 20 74 72 79 20 74 6f 20 6f 70 65 6e  irst try to open
120cf 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65  /create the file
120d0 0d 0a 20 20 2a 2a 20 32 2e 20 69 66 20 74 68 61  ..  ** 2. if tha
120d1 74 20 66 61 69 6c 73 2c 20 61 6e 64 20 74 68 69  t fails, and thi
120d2 73 20 69 73 20 61 20 6c 6f 63 6b 20 66 69 6c 65  s is a lock file
120d3 20 28 6e 6f 74 2d 63 6f 6e 63 68 29 2c 20 74 72   (not-conch), tr
120d4 79 20 63 72 65 61 74 69 6e 67 0d 0a 20 20 2a 2a  y creating..  **
120d5 20 74 68 65 20 70 61 72 65 6e 74 20 64 69 72 65   the parent dire
120d6 63 74 6f 72 69 65 73 20 61 6e 64 20 74 68 65 6e  ctories and then
120d7 20 74 72 79 20 61 67 61 69 6e 2e 0d 0a 20 20 2a   try again...  *
120d8 2a 20 33 2e 20 69 66 20 74 68 61 74 20 66 61 69  * 3. if that fai
120d9 6c 73 2c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20  ls, try to open 
120da 74 68 65 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e  the file read-on
120db 6c 79 0d 0a 20 20 2a 2a 20 6f 74 68 65 72 77 69  ly..  ** otherwi
120dc 73 65 20 72 65 74 75 72 6e 20 42 55 53 59 20 28  se return BUSY (
120dd 69 66 20 6c 6f 63 6b 20 66 69 6c 65 29 20 6f 72  if lock file) or
120de 20 43 41 4e 54 4f 50 45 4e 20 66 6f 72 20 74 68   CANTOPEN for th
120df 65 20 63 6f 6e 63 68 20 66 69 6c 65 0d 0a 20 20  e conch file..  
120e0 2a 2f 0d 0a 20 20 70 55 6e 75 73 65 64 20 3d 20  */..  pUnused = 
120e1 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28 70  findReusableFd(p
120e2 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 29 3b  ath, openFlags);
120e3 0d 0a 20 20 69 66 28 20 70 55 6e 75 73 65 64 20  ..  if( pUnused 
120e4 29 7b 0d 0a 20 20 20 20 66 64 20 3d 20 70 55 6e  ){..    fd = pUn
120e5 75 73 65 64 2d 3e 66 64 3b 0d 0a 20 20 7d 65 6c  used->fd;..  }el
120e6 73 65 7b 0d 0a 20 20 20 20 70 55 6e 75 73 65 64  se{..    pUnused
120e7 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
120e8 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75 73 65  c(sizeof(*pUnuse
120e9 64 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70  d));..    if( !p
120ea 55 6e 75 73 65 64 20 29 7b 0d 0a 20 20 20 20 20  Unused ){..     
120eb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
120ec 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  OMEM;..    }..  
120ed 7d 0d 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b  }..  if( fd<0 ){
120ee 0d 0a 20 20 20 20 66 64 20 3d 20 72 6f 62 75 73  ..    fd = robus
120ef 74 5f 6f 70 65 6e 28 70 61 74 68 2c 20 6f 70 65  t_open(path, ope
120f0 6e 46 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 44  nFlags, SQLITE_D
120f1 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d  EFAULT_FILE_PERM
120f2 49 53 53 49 4f 4e 53 29 3b 0d 0a 20 20 20 20 74  ISSIONS);..    t
120f3 65 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a  errno = errno;..
120f4 20 20 20 20 69 66 28 20 66 64 3c 30 20 26 26 20      if( fd<0 && 
120f5 65 72 72 6e 6f 3d 3d 45 4e 4f 45 4e 54 20 26 26  errno==ENOENT &&
120f6 20 69 73 6c 6f 63 6b 66 69 6c 65 20 29 7b 0d 0a   islockfile ){..
120f7 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79 43        if( proxyC
120f8 72 65 61 74 65 4c 6f 63 6b 50 61 74 68 28 70 61  reateLockPath(pa
120f9 74 68 29 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b  th) == SQLITE_OK
120fa 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 66 64 20   ){..        fd 
120fb 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 61  = robust_open(pa
120fc 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 53  th, openFlags, S
120fd 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
120fe 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b  LE_PERMISSIONS);
120ff 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
12100 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 66 64 3c  ..  }..  if( fd<
12101 30 20 29 7b 0d 0a 20 20 20 20 6f 70 65 6e 46 6c  0 ){..    openFl
12102 61 67 73 20 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0d  ags = O_RDONLY;.
12103 0a 20 20 20 20 66 64 20 3d 20 72 6f 62 75 73 74  .    fd = robust
12104 5f 6f 70 65 6e 28 70 61 74 68 2c 20 6f 70 65 6e  _open(path, open
12105 46 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 44 45  Flags, SQLITE_DE
12106 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
12107 53 53 49 4f 4e 53 29 3b 0d 0a 20 20 20 20 74 65  SSIONS);..    te
12108 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20  rrno = errno;.. 
12109 20 7d 0d 0a 20 20 69 66 28 20 66 64 3c 30 20 29   }..  if( fd<0 )
1210a 7b 0d 0a 20 20 20 20 69 66 28 20 69 73 6c 6f 63  {..    if( isloc
1210b 6b 66 69 6c 65 20 29 7b 0d 0a 20 20 20 20 20 20  kfile ){..      
1210c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
1210d 53 59 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  SY;..    }..    
1210e 73 77 69 74 63 68 20 28 74 65 72 72 6e 6f 29 20  switch (terrno) 
1210f 7b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 45 41  {..      case EA
12110 43 43 45 53 3a 0d 0a 20 20 20 20 20 20 20 20 72  CCES:..        r
12111 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52  eturn SQLITE_PER
12112 4d 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 45  M;..      case E
12113 49 4f 3a 20 0d 0a 20 20 20 20 20 20 20 20 72 65  IO: ..        re
12114 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
12115 52 5f 4c 4f 43 4b 3b 20 2f 2a 20 65 76 65 6e 20  R_LOCK; /* even 
12116 74 68 6f 75 67 68 20 69 74 20 69 73 20 74 68 65  though it is the
12117 20 63 6f 6e 63 68 20 2a 2f 0d 0a 20 20 20 20 20   conch */..     
12118 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20 20   default:..     
12119 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1211a 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0d  _CANTOPEN_BKPT;.
1211b 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d  .    }..  }..  .
1211c 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 78 46  .  pNew = (unixF
1211d 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  ile *)sqlite3_ma
1211e 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e 65  lloc(sizeof(*pNe
1211f 77 29 29 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77  w));..  if( pNew
12120 3d 3d 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 72  ==NULL ){..    r
12121 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
12122 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  ;..    goto end_
12123 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0d 0a 20  create_proxy;.. 
12124 20 7d 0d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65   }..  memset(pNe
12125 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69  w, 0, sizeof(uni
12126 78 46 69 6c 65 29 29 3b 0d 0a 20 20 70 4e 65 77  xFile));..  pNew
12127 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70  ->openFlags = op
12128 65 6e 46 6c 61 67 73 3b 0d 0a 20 20 6d 65 6d 73  enFlags;..  mems
12129 65 74 28 26 64 75 6d 6d 79 56 66 73 2c 20 30 2c  et(&dummyVfs, 0,
1212a 20 73 69 7a 65 6f 66 28 64 75 6d 6d 79 56 66 73   sizeof(dummyVfs
1212b 29 29 3b 0d 0a 20 20 64 75 6d 6d 79 56 66 73 2e  ));..  dummyVfs.
1212c 70 41 70 70 44 61 74 61 20 3d 20 28 76 6f 69 64  pAppData = (void
1212d 2a 29 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  *)&autolockIoFin
1212e 64 65 72 3b 0d 0a 20 20 64 75 6d 6d 79 56 66 73  der;..  dummyVfs
1212f 2e 7a 4e 61 6d 65 20 3d 20 22 64 75 6d 6d 79 22  .zName = "dummy"
12130 3b 0d 0a 20 20 70 55 6e 75 73 65 64 2d 3e 66 64  ;..  pUnused->fd
12131 20 3d 20 66 64 3b 0d 0a 20 20 70 55 6e 75 73 65   = fd;..  pUnuse
12132 64 2d 3e 66 6c 61 67 73 20 3d 20 6f 70 65 6e 46  d->flags = openF
12133 6c 61 67 73 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70  lags;..  pNew->p
12134 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 64  Unused = pUnused
12135 3b 0d 0a 20 20 0d 0a 20 20 72 63 20 3d 20 66 69  ;..  ..  rc = fi
12136 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 26 64 75  llInUnixFile(&du
12137 6d 6d 79 56 66 73 2c 20 66 64 2c 20 28 73 71 6c  mmyVfs, fd, (sql
12138 69 74 65 33 5f 66 69 6c 65 2a 29 70 4e 65 77 2c  ite3_file*)pNew,
12139 20 70 61 74 68 2c 20 30 29 3b 0d 0a 20 20 69 66   path, 0);..  if
1213a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1213b 29 7b 0d 0a 20 20 20 20 2a 70 70 46 69 6c 65 20  ){..    *ppFile 
1213c 3d 20 70 4e 65 77 3b 0d 0a 20 20 20 20 72 65 74  = pNew;..    ret
1213d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
1213e 20 20 7d 0d 0a 65 6e 64 5f 63 72 65 61 74 65 5f    }..end_create_
1213f 70 72 6f 78 79 3a 20 20 20 20 0d 0a 20 20 72 6f  proxy:    ..  ro
12140 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c  bust_close(pNew,
12141 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d   fd, __LINE__);.
12142 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
12143 70 4e 65 77 29 3b 0d 0a 20 20 73 71 6c 69 74 65  pNew);..  sqlite
12144 33 5f 66 72 65 65 28 70 55 6e 75 73 65 64 29 3b  3_free(pUnused);
12145 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
12146 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  }....#ifdef SQLI
12147 54 45 5f 54 45 53 54 0d 0a 2f 2a 20 73 69 6d 75  TE_TEST../* simu
12148 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f  late multiple ho
12149 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20  sts by creating 
1214a 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 69  unique hostid fi
1214b 6c 65 20 70 61 74 68 73 20 2a 2f 0d 0a 53 51 4c  le paths */..SQL
1214c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1214d 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d  te3_hostid_num =
1214e 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23   0;..#endif....#
1214f 64 65 66 69 6e 65 20 50 52 4f 58 59 5f 48 4f 53  define PROXY_HOS
12150 54 49 44 4c 45 4e 20 20 20 20 31 36 20 20 2f 2a  TIDLEN    16  /*
12151 20 63 6f 6e 63 68 20 66 69 6c 65 20 68 6f 73 74   conch file host
12152 20 69 64 20 6c 65 6e 67 74 68 20 2a 2f 0d 0a 0d   id length */...
12153 0a 2f 2a 20 4e 6f 74 20 61 6c 77 61 79 73 20 64  ./* Not always d
12154 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65  efined in the he
12155 61 64 65 72 73 20 61 73 20 69 74 20 6f 75 67 68  aders as it ough
12156 74 20 74 6f 20 62 65 20 2a 2f 0d 0a 65 78 74 65  t to be */..exte
12157 72 6e 20 69 6e 74 20 67 65 74 68 6f 73 74 75 75  rn int gethostuu
12158 69 64 28 75 75 69 64 5f 74 20 69 64 2c 20 63 6f  id(uuid_t id, co
12159 6e 73 74 20 73 74 72 75 63 74 20 74 69 6d 65 73  nst struct times
1215a 70 65 63 20 2a 77 61 69 74 29 3b 0d 0a 0d 0a 2f  pec *wait);..../
1215b 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49  * get the host I
1215c 44 20 76 69 61 20 67 65 74 68 6f 73 74 75 75 69  D via gethostuui
1215d 64 28 29 2c 20 70 48 6f 73 74 49 44 20 6d 75 73  d(), pHostID mus
1215e 74 20 70 6f 69 6e 74 20 74 6f 20 50 52 4f 58 59  t point to PROXY
1215f 5f 48 4f 53 54 49 44 4c 45 4e 20 0d 0a 2a 2a 20  _HOSTIDLEN ..** 
12160 62 79 74 65 73 20 6f 66 20 77 72 69 74 61 62 6c  bytes of writabl
12161 65 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 73  e memory...*/..s
12162 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47  tatic int proxyG
12163 65 74 48 6f 73 74 49 44 28 75 6e 73 69 67 6e 65  etHostID(unsigne
12164 64 20 63 68 61 72 20 2a 70 48 6f 73 74 49 44 2c  d char *pHostID,
12165 20 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0d 0a   int *pError){..
12166 20 20 61 73 73 65 72 74 28 50 52 4f 58 59 5f 48    assert(PROXY_H
12167 4f 53 54 49 44 4c 45 4e 20 3d 3d 20 73 69 7a 65  OSTIDLEN == size
12168 6f 66 28 75 75 69 64 5f 74 29 29 3b 0d 0a 20 20  of(uuid_t));..  
12169 6d 65 6d 73 65 74 28 70 48 6f 73 74 49 44 2c 20  memset(pHostID, 
1216a 30 2c 20 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c  0, PROXY_HOSTIDL
1216b 45 4e 29 3b 0d 0a 23 69 66 20 64 65 66 69 6e 65  EN);..#if define
1216c 64 28 5f 5f 4d 41 58 5f 4f 53 5f 58 5f 56 45 52  d(__MAX_OS_X_VER
1216d 53 49 4f 4e 5f 4d 49 4e 5f 52 45 51 55 49 52 45  SION_MIN_REQUIRE
1216e 44 29 5c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  D)\..           
1216f 20 20 20 20 26 26 20 5f 5f 4d 41 43 5f 4f 53 5f      && __MAC_OS_
12170 58 5f 56 45 52 53 49 4f 4e 5f 4d 49 4e 5f 52 45  X_VERSION_MIN_RE
12171 51 55 49 52 45 44 3c 31 30 35 30 0d 0a 20 20 7b  QUIRED<1050..  {
12172 0d 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  ..    static con
12173 73 74 20 73 74 72 75 63 74 20 74 69 6d 65 73 70  st struct timesp
12174 65 63 20 74 69 6d 65 6f 75 74 20 3d 20 7b 31 2c  ec timeout = {1,
12175 20 30 7d 3b 20 2f 2a 20 31 20 73 65 63 20 74 69   0}; /* 1 sec ti
12176 6d 65 6f 75 74 20 2a 2f 0d 0a 20 20 20 20 69 66  meout */..    if
12177 28 20 67 65 74 68 6f 73 74 75 75 69 64 28 70 48  ( gethostuuid(pH
12178 6f 73 74 49 44 2c 20 26 74 69 6d 65 6f 75 74 29  ostID, &timeout)
12179 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 65   ){..      int e
1217a 72 72 20 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20  rr = errno;..   
1217b 20 20 20 69 66 28 20 70 45 72 72 6f 72 20 29 7b     if( pError ){
1217c 0d 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 6f  ..        *pErro
1217d 72 20 3d 20 65 72 72 3b 0d 0a 20 20 20 20 20 20  r = err;..      
1217e 7d 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  }..      return 
1217f 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0d 0a 20  SQLITE_IOERR;.. 
12180 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6c 73 65     }..  }..#else
12181 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
12182 45 54 45 52 28 70 45 72 72 6f 72 29 3b 0d 0a 23  ETER(pError);..#
12183 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
12184 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 2f 2a 20  LITE_TEST..  /* 
12185 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c  simulate multipl
12186 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 61 74  e hosts by creat
12187 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69  ing unique hosti
12188 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0d  d file paths */.
12189 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68  .  if( sqlite3_h
1218a 6f 73 74 69 64 5f 6e 75 6d 20 21 3d 20 30 29 7b  ostid_num != 0){
1218b 0d 0a 20 20 20 20 70 48 6f 73 74 49 44 5b 30 5d  ..    pHostID[0]
1218c 20 3d 20 28 63 68 61 72 29 28 70 48 6f 73 74 49   = (char)(pHostI
1218d 44 5b 30 5d 20 2b 20 28 63 68 61 72 29 28 73 71  D[0] + (char)(sq
1218e 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d  lite3_hostid_num
1218f 20 26 20 30 78 46 46 29 29 3b 0d 0a 20 20 7d 0d   & 0xFF));..  }.
12190 0a 23 65 6e 64 69 66 0d 0a 20 20 0d 0a 20 20 72  .#endif..  ..  r
12191 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12192 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68 65 20 63 6f  ..}..../* The co
12193 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  nch file contain
12194 73 20 74 68 65 20 68 65 61 64 65 72 2c 20 68 6f  s the header, ho
12195 73 74 20 69 64 20 61 6e 64 20 6c 6f 63 6b 20 66  st id and lock f
12196 69 6c 65 20 70 61 74 68 0d 0a 20 2a 2f 0d 0a 23  ile path.. */..#
12197 64 65 66 69 6e 65 20 50 52 4f 58 59 5f 43 4f 4e  define PROXY_CON
12198 43 48 56 45 52 53 49 4f 4e 20 32 20 20 20 2f 2a  CHVERSION 2   /*
12199 20 31 2d 62 79 74 65 20 68 65 61 64 65 72 2c 20   1-byte header, 
1219a 31 36 2d 62 79 74 65 20 68 6f 73 74 20 69 64 2c  16-byte host id,
1219b 20 70 61 74 68 20 2a 2f 0d 0a 23 64 65 66 69 6e   path */..#defin
1219c 65 20 50 52 4f 58 59 5f 48 45 41 44 45 52 4c 45  e PROXY_HEADERLE
1219d 4e 20 20 20 20 31 20 20 20 2f 2a 20 63 6f 6e 63  N    1   /* conc
1219e 68 20 66 69 6c 65 20 68 65 61 64 65 72 20 6c 65  h file header le
1219f 6e 67 74 68 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ngth */..#define
121a0 20 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58   PROXY_PATHINDEX
121a1 20 20 20 20 28 50 52 4f 58 59 5f 48 45 41 44 45      (PROXY_HEADE
121a2 52 4c 45 4e 2b 50 52 4f 58 59 5f 48 4f 53 54 49  RLEN+PROXY_HOSTI
121a3 44 4c 45 4e 29 0d 0a 23 64 65 66 69 6e 65 20 50  DLEN)..#define P
121a4 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e  ROXY_MAXCONCHLEN
121a5 20 20 28 50 52 4f 58 59 5f 48 45 41 44 45 52 4c    (PROXY_HEADERL
121a6 45 4e 2b 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c  EN+PROXY_HOSTIDL
121a7 45 4e 2b 4d 41 58 50 41 54 48 4c 45 4e 29 0d 0a  EN+MAXPATHLEN)..
121a8 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 61 6b 65 73 20  ../* ..** Takes 
121a9 61 6e 20 6f 70 65 6e 20 63 6f 6e 63 68 20 66 69  an open conch fi
121aa 6c 65 2c 20 63 6f 70 69 65 73 20 74 68 65 20 63  le, copies the c
121ab 6f 6e 74 65 6e 74 73 20 74 6f 20 61 20 6e 65 77  ontents to a new
121ac 20 70 61 74 68 20 61 6e 64 20 74 68 65 6e 20 6d   path and then m
121ad 6f 76 65 73 20 0d 0a 2a 2a 20 69 74 20 62 61 63  oves ..** it bac
121ae 6b 2e 20 20 54 68 65 20 6e 65 77 6c 79 20 63 72  k.  The newly cr
121af 65 61 74 65 64 20 66 69 6c 65 27 73 20 66 69 6c  eated file's fil
121b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
121b1 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 0d  assigned to the.
121b2 0a 2a 2a 20 63 6f 6e 63 68 20 66 69 6c 65 20 73  .** conch file s
121b3 74 72 75 63 74 75 72 65 20 61 6e 64 20 66 69 6e  tructure and fin
121b4 61 6c 6c 79 20 74 68 65 20 6f 72 69 67 69 6e 61  ally the origina
121b5 6c 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 65 73  l conch file des
121b6 63 72 69 70 74 6f 72 20 69 73 20 0d 0a 2a 2a 20  criptor is ..** 
121b7 63 6c 6f 73 65 64 2e 20 20 52 65 74 75 72 6e 73  closed.  Returns
121b8 20 7a 65 72 6f 20 69 66 20 73 75 63 63 65 73 73   zero if success
121b9 66 75 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ful...*/..static
121ba 20 69 6e 74 20 70 72 6f 78 79 42 72 65 61 6b 43   int proxyBreakC
121bb 6f 6e 63 68 4c 6f 63 6b 28 75 6e 69 78 46 69 6c  onchLock(unixFil
121bc 65 20 2a 70 46 69 6c 65 2c 20 75 75 69 64 5f 74  e *pFile, uuid_t
121bd 20 6d 79 48 6f 73 74 49 44 29 7b 0d 0a 20 20 70   myHostID){..  p
121be 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
121bf 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
121c0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
121c1 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
121c2 43 6f 6e 74 65 78 74 3b 20 0d 0a 20 20 75 6e 69  Context; ..  uni
121c3 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65  xFile *conchFile
121c4 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
121c5 6c 65 3b 0d 0a 20 20 63 68 61 72 20 74 50 61 74  le;..  char tPat
121c6 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0d 0a  h[MAXPATHLEN];..
121c7 20 20 63 68 61 72 20 62 75 66 5b 50 52 4f 58 59    char buf[PROXY
121c8 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d 3b 0d 0a  _MAXCONCHLEN];..
121c9 20 20 63 68 61 72 20 2a 63 50 61 74 68 20 3d 20    char *cPath = 
121ca 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50  pCtx->conchFileP
121cb 61 74 68 3b 0d 0a 20 20 73 69 7a 65 5f 74 20 72  ath;..  size_t r
121cc 65 61 64 4c 65 6e 20 3d 20 30 3b 0d 0a 20 20 73  eadLen = 0;..  s
121cd 69 7a 65 5f 74 20 70 61 74 68 4c 65 6e 20 3d 20  ize_t pathLen = 
121ce 30 3b 0d 0a 20 20 63 68 61 72 20 65 72 72 6d 73  0;..  char errms
121cf 67 5b 36 34 5d 20 3d 20 22 22 3b 0d 0a 20 20 69  g[64] = "";..  i
121d0 6e 74 20 66 64 20 3d 20 2d 31 3b 0d 0a 20 20 69  nt fd = -1;..  i
121d1 6e 74 20 72 63 20 3d 20 2d 31 3b 0d 0a 20 20 55  nt rc = -1;..  U
121d2 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
121d3 6d 79 48 6f 73 74 49 44 29 3b 0d 0a 0d 0a 20 20  myHostID);....  
121d4 2f 2a 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  /* create a new 
121d5 70 61 74 68 20 62 79 20 72 65 70 6c 61 63 65 20  path by replace 
121d6 74 68 65 20 74 72 61 69 6c 69 6e 67 20 27 2d 63  the trailing '-c
121d7 6f 6e 63 68 27 20 77 69 74 68 20 27 2d 62 72 65  onch' with '-bre
121d8 61 6b 27 20 2a 2f 0d 0a 20 20 70 61 74 68 4c 65  ak' */..  pathLe
121d9 6e 20 3d 20 73 74 72 6c 63 70 79 28 74 50 61 74  n = strlcpy(tPat
121da 68 2c 20 63 50 61 74 68 2c 20 4d 41 58 50 41 54  h, cPath, MAXPAT
121db 48 4c 45 4e 29 3b 0d 0a 20 20 69 66 28 20 70 61  HLEN);..  if( pa
121dc 74 68 4c 65 6e 3e 4d 41 58 50 41 54 48 4c 45 4e  thLen>MAXPATHLEN
121dd 20 7c 7c 20 70 61 74 68 4c 65 6e 3c 36 20 7c 7c   || pathLen<6 ||
121de 20 0d 0a 20 20 20 20 20 28 73 74 72 6c 63 70 79   ..     (strlcpy
121df 28 26 74 50 61 74 68 5b 70 61 74 68 4c 65 6e 2d  (&tPath[pathLen-
121e0 35 5d 2c 20 22 62 72 65 61 6b 22 2c 20 36 29 20  5], "break", 6) 
121e1 21 3d 20 35 29 20 29 7b 0d 0a 20 20 20 20 73 71  != 5) ){..    sq
121e2 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
121e3 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 65 72  izeof(errmsg),er
121e4 72 6d 73 67 2c 22 70 61 74 68 20 65 72 72 6f 72  rmsg,"path error
121e5 20 28 6c 65 6e 20 25 64 29 22 2c 28 69 6e 74 29   (len %d)",(int)
121e6 70 61 74 68 4c 65 6e 29 3b 0d 0a 20 20 20 20 67  pathLen);..    g
121e7 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f 63  oto end_breakloc
121e8 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 72 65  k;..  }..  /* re
121e9 61 64 20 74 68 65 20 63 6f 6e 63 68 20 63 6f 6e  ad the conch con
121ea 74 65 6e 74 20 2a 2f 0d 0a 20 20 72 65 61 64 4c  tent */..  readL
121eb 65 6e 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e  en = osPread(con
121ec 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2c 20  chFile->h, buf, 
121ed 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45  PROXY_MAXCONCHLE
121ee 4e 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 65  N, 0);..  if( re
121ef 61 64 4c 65 6e 3c 50 52 4f 58 59 5f 50 41 54 48  adLen<PROXY_PATH
121f0 49 4e 44 45 58 20 29 7b 0d 0a 20 20 20 20 73 71  INDEX ){..    sq
121f1 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
121f2 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 65 72  izeof(errmsg),er
121f3 72 6d 73 67 2c 22 72 65 61 64 20 65 72 72 6f 72  rmsg,"read error
121f4 20 28 6c 65 6e 20 25 64 29 22 2c 28 69 6e 74 29   (len %d)",(int)
121f5 72 65 61 64 4c 65 6e 29 3b 0d 0a 20 20 20 20 67  readLen);..    g
121f6 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f 63  oto end_breakloc
121f7 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 77 72  k;..  }..  /* wr
121f8 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68  ite it out to th
121f9 65 20 74 65 6d 70 6f 72 61 72 79 20 62 72 65 61  e temporary brea
121fa 6b 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 66 64 20  k file */..  fd 
121fb 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 74 50  = robust_open(tP
121fc 61 74 68 2c 20 28 4f 5f 52 44 57 52 7c 4f 5f 43  ath, (O_RDWR|O_C
121fd 52 45 41 54 7c 4f 5f 45 58 43 4c 29 2c 0d 0a 20  REAT|O_EXCL),.. 
121fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ff 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54    SQLITE_DEFAULT
12200 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e  _FILE_PERMISSION
12201 53 29 3b 0d 0a 20 20 69 66 28 20 66 64 3c 30 20  S);..  if( fd<0 
12202 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
12203 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
12204 65 72 72 6d 73 67 29 2c 20 65 72 72 6d 73 67 2c  errmsg), errmsg,
12205 20 22 63 72 65 61 74 65 20 66 61 69 6c 65 64 20   "create failed 
12206 28 25 64 29 22 2c 20 65 72 72 6e 6f 29 3b 0d 0a  (%d)", errno);..
12207 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 62 72 65      goto end_bre
12208 61 6b 6c 6f 63 6b 3b 0d 0a 20 20 7d 0d 0a 20 20  aklock;..  }..  
12209 69 66 28 20 6f 73 50 77 72 69 74 65 28 66 64 2c  if( osPwrite(fd,
1220a 20 62 75 66 2c 20 72 65 61 64 4c 65 6e 2c 20 30   buf, readLen, 0
1220b 29 20 21 3d 20 28 73 73 69 7a 65 5f 74 29 72 65  ) != (ssize_t)re
1220c 61 64 4c 65 6e 20 29 7b 0d 0a 20 20 20 20 73 71  adLen ){..    sq
1220d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1220e 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65  izeof(errmsg), e
1220f 72 72 6d 73 67 2c 20 22 77 72 69 74 65 20 66 61  rrmsg, "write fa
12210 69 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e  iled (%d)", errn
12211 6f 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 6e  o);..    goto en
12212 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0d 0a 20 20  d_breaklock;..  
12213 7d 0d 0a 20 20 69 66 28 20 72 65 6e 61 6d 65 28  }..  if( rename(
12214 74 50 61 74 68 2c 20 63 50 61 74 68 29 20 29 7b  tPath, cPath) ){
12215 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
12216 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 65 72  printf(sizeof(er
12217 72 6d 73 67 29 2c 20 65 72 72 6d 73 67 2c 20 22  rmsg), errmsg, "
12218 72 65 6e 61 6d 65 20 66 61 69 6c 65 64 20 28 25  rename failed (%
12219 64 29 22 2c 20 65 72 72 6e 6f 29 3b 0d 0a 20 20  d)", errno);..  
1221a 20 20 67 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b    goto end_break
1221b 6c 6f 63 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63  lock;..  }..  rc
1221c 20 3d 20 30 3b 0d 0a 20 20 66 70 72 69 6e 74 66   = 0;..  fprintf
1221d 28 73 74 64 65 72 72 2c 20 22 62 72 6f 6b 65 20  (stderr, "broke 
1221e 73 74 61 6c 65 20 6c 6f 63 6b 20 6f 6e 20 25 73  stale lock on %s
1221f 5c 6e 22 2c 20 63 50 61 74 68 29 3b 0d 0a 20 20  \n", cPath);..  
12220 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 46 69  robust_close(pFi
12221 6c 65 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  le, conchFile->h
12222 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d 0a 20 20  , __LINE__);..  
12223 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 20 3d 20 66  conchFile->h = f
12224 64 3b 0d 0a 20 20 63 6f 6e 63 68 46 69 6c 65 2d  d;..  conchFile-
12225 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 4f 5f 52  >openFlags = O_R
12226 44 57 52 20 7c 20 4f 5f 43 52 45 41 54 3b 0d 0a  DWR | O_CREAT;..
12227 0d 0a 65 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3a  ..end_breaklock:
12228 0d 0a 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20  ..  if( rc ){.. 
12229 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0d     if( fd>=0 ){.
1222a 0a 20 20 20 20 20 20 6f 73 55 6e 6c 69 6e 6b 28  .      osUnlink(
1222b 74 50 61 74 68 29 3b 0d 0a 20 20 20 20 20 20 72  tPath);..      r
1222c 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 46 69 6c  obust_close(pFil
1222d 65 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29  e, fd, __LINE__)
1222e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 70  ;..    }..    fp
1222f 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66  rintf(stderr, "f
12230 61 69 6c 65 64 20 74 6f 20 62 72 65 61 6b 20 73  ailed to break s
12231 74 61 6c 65 20 6c 6f 63 6b 20 6f 6e 20 25 73 2c  tale lock on %s,
12232 20 25 73 5c 6e 22 2c 20 63 50 61 74 68 2c 20 65   %s\n", cPath, e
12233 72 72 6d 73 67 29 3b 0d 0a 20 20 7d 0d 0a 20 20  rrmsg);..  }..  
12234 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
12235 0a 2f 2a 20 54 61 6b 65 20 74 68 65 20 72 65 71  ./* Take the req
12236 75 65 73 74 65 64 20 6c 6f 63 6b 20 6f 6e 20 74  uested lock on t
12237 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 61 6e  he conch file an
12238 64 20 62 72 65 61 6b 20 61 20 73 74 61 6c 65 20  d break a stale 
12239 6c 6f 63 6b 20 69 66 20 74 68 65 20 0d 0a 2a 2a  lock if the ..**
1223a 20 68 6f 73 74 20 69 64 20 6d 61 74 63 68 65 73   host id matches
1223b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
1223c 74 20 70 72 6f 78 79 43 6f 6e 63 68 4c 6f 63 6b  t proxyConchLock
1223d 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
1223e 2c 20 75 75 69 64 5f 74 20 6d 79 48 6f 73 74 49  , uuid_t myHostI
1223f 44 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29  D, int lockType)
12240 7b 0d 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  {..  proxyLockin
12241 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
12242 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
12243 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c  ntext *)pFile->l
12244 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0d  ockingContext; .
12245 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e  .  unixFile *con
12246 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
12247 6f 6e 63 68 46 69 6c 65 3b 0d 0a 20 20 69 6e 74  onchFile;..  int
12248 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
12249 0d 0a 20 20 69 6e 74 20 6e 54 72 69 65 73 20 3d  ..  int nTries =
1224a 20 30 3b 0d 0a 20 20 73 74 72 75 63 74 20 74 69   0;..  struct ti
1224b 6d 65 73 70 65 63 20 63 6f 6e 63 68 4d 6f 64 54  mespec conchModT
1224c 69 6d 65 3b 0d 0a 20 20 0d 0a 20 20 6d 65 6d 73  ime;..  ..  mems
1224d 65 74 28 26 63 6f 6e 63 68 4d 6f 64 54 69 6d 65  et(&conchModTime
1224e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 6f 6e 63  , 0, sizeof(conc
1224f 68 4d 6f 64 54 69 6d 65 29 29 3b 0d 0a 20 20 64  hModTime));..  d
12250 6f 20 7b 0d 0a 20 20 20 20 72 63 20 3d 20 63 6f  o {..    rc = co
12251 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
12252 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33  ->xLock((sqlite3
12253 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
12254 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0d 0a 20 20  , lockType);..  
12255 20 20 6e 54 72 69 65 73 20 2b 2b 3b 0d 0a 20 20    nTries ++;..  
12256 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12257 5f 42 55 53 59 20 29 7b 0d 0a 20 20 20 20 20 20  _BUSY ){..      
12258 2f 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 66  /* If the lock f
12259 61 69 6c 65 64 20 28 62 75 73 79 29 3a 0d 0a 20  ailed (busy):.. 
1225a 20 20 20 20 20 20 2a 20 31 73 74 20 74 72 79 3a        * 1st try:
1225b 20 67 65 74 20 74 68 65 20 6d 6f 64 20 74 69 6d   get the mod tim
1225c 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 2c 20  e of the conch, 
1225d 77 61 69 74 20 30 2e 35 73 20 61 6e 64 20 74 72  wait 0.5s and tr
1225e 79 20 61 67 61 69 6e 2e 20 0d 0a 20 20 20 20 20  y again. ..     
1225f 20 20 2a 20 32 6e 64 20 74 72 79 3a 20 66 61 69    * 2nd try: fai
12260 6c 20 69 66 20 74 68 65 20 6d 6f 64 20 74 69 6d  l if the mod tim
12261 65 20 63 68 61 6e 67 65 64 20 6f 72 20 68 6f 73  e changed or hos
12262 74 20 69 64 20 69 73 20 64 69 66 66 65 72 65 6e  t id is differen
12263 74 2c 20 77 61 69 74 20 0d 0a 20 20 20 20 20 20  t, wait ..      
12264 20 2a 20 20 20 20 20 20 20 20 20 20 20 31 30 20   *           10 
12265 73 65 63 20 61 6e 64 20 74 72 79 20 61 67 61 69  sec and try agai
12266 6e 0d 0a 20 20 20 20 20 20 20 2a 20 33 72 64 20  n..       * 3rd 
12267 74 72 79 3a 20 62 72 65 61 6b 20 74 68 65 20 6c  try: break the l
12268 6f 63 6b 20 75 6e 6c 65 73 73 20 74 68 65 20 6d  ock unless the m
12269 6f 64 20 74 69 6d 65 20 68 61 73 20 63 68 61 6e  od time has chan
1226a 67 65 64 2e 0d 0a 20 20 20 20 20 20 20 2a 2f 0d  ged...       */.
1226b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74  .      struct st
1226c 61 74 20 62 75 66 3b 0d 0a 20 20 20 20 20 20 69  at buf;..      i
1226d 66 28 20 6f 73 46 73 74 61 74 28 63 6f 6e 63 68  f( osFstat(conch
1226e 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 20 29  File->h, &buf) )
1226f 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  {..        pFile
12270 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
12271 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 20 20 72 65  rno;..        re
12272 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
12273 52 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20 20 7d  R_LOCK;..      }
12274 0d 0a 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20  ..      ..      
12275 69 66 28 20 6e 54 72 69 65 73 3d 3d 31 20 29 7b  if( nTries==1 ){
12276 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 63 68 4d  ..        conchM
12277 6f 64 54 69 6d 65 20 3d 20 62 75 66 2e 73 74 5f  odTime = buf.st_
12278 6d 74 69 6d 65 73 70 65 63 3b 0d 0a 20 20 20 20  mtimespec;..    
12279 20 20 20 20 75 73 6c 65 65 70 28 35 30 30 30 30      usleep(50000
1227a 30 29 3b 20 2f 2a 20 77 61 69 74 20 30 2e 35 20  0); /* wait 0.5 
1227b 73 65 63 20 61 6e 64 20 74 72 79 20 74 68 65 20  sec and try the 
1227c 6c 6f 63 6b 20 61 67 61 69 6e 2a 2f 0d 0a 20 20  lock again*/..  
1227d 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 20        continue; 
1227e 20 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20   ..      }....  
1227f 20 20 20 20 61 73 73 65 72 74 28 20 6e 54 72 69      assert( nTri
12280 65 73 3e 31 20 29 3b 0d 0a 20 20 20 20 20 20 69  es>1 );..      i
12281 66 28 20 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 2e  f( conchModTime.
12282 74 76 5f 73 65 63 20 21 3d 20 62 75 66 2e 73 74  tv_sec != buf.st
12283 5f 6d 74 69 6d 65 73 70 65 63 2e 74 76 5f 73 65  _mtimespec.tv_se
12284 63 20 7c 7c 20 0d 0a 20 20 20 20 20 20 20 20 20  c || ..         
12285 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 2e 74 76 5f  conchModTime.tv_
12286 6e 73 65 63 20 21 3d 20 62 75 66 2e 73 74 5f 6d  nsec != buf.st_m
12287 74 69 6d 65 73 70 65 63 2e 74 76 5f 6e 73 65 63  timespec.tv_nsec
12288 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74   ){..        ret
12289 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
1228a 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
1228b 20 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 54 72   ..      if( nTr
1228c 69 65 73 3d 3d 32 20 29 7b 20 20 0d 0a 20 20 20  ies==2 ){  ..   
1228d 20 20 20 20 20 63 68 61 72 20 74 42 75 66 5b 50       char tBuf[P
1228e 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e  ROXY_MAXCONCHLEN
1228f 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ];..        int 
12290 6c 65 6e 20 3d 20 6f 73 50 72 65 61 64 28 63 6f  len = osPread(co
12291 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 74 42 75 66  nchFile->h, tBuf
12292 2c 20 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48  , PROXY_MAXCONCH
12293 4c 45 4e 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  LEN, 0);..      
12294 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0d 0a    if( len<0 ){..
12295 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
12296 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
12297 6e 6f 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72  no;..          r
12298 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
12299 52 52 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 20 20  RR_LOCK;..      
1229a 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
1229b 20 6c 65 6e 3e 50 52 4f 58 59 5f 50 41 54 48 49   len>PROXY_PATHI
1229c 4e 44 45 58 20 26 26 20 74 42 75 66 5b 30 5d 3d  NDEX && tBuf[0]=
1229d 3d 28 63 68 61 72 29 50 52 4f 58 59 5f 43 4f 4e  =(char)PROXY_CON
1229e 43 48 56 45 52 53 49 4f 4e 29 7b 0d 0a 20 20 20  CHVERSION){..   
1229f 20 20 20 20 20 20 20 2f 2a 20 64 6f 6e 27 74 20         /* don't 
122a0 62 72 65 61 6b 20 74 68 65 20 6c 6f 63 6b 20 69  break the lock i
122a1 66 20 74 68 65 20 68 6f 73 74 20 69 64 20 64 6f  f the host id do
122a2 65 73 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0d 0a  esn't match */..
122a3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 30 21            if( 0!
122a4 3d 6d 65 6d 63 6d 70 28 26 74 42 75 66 5b 50 52  =memcmp(&tBuf[PR
122a5 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e 5d 2c 20  OXY_HEADERLEN], 
122a6 6d 79 48 6f 73 74 49 44 2c 20 50 52 4f 58 59 5f  myHostID, PROXY_
122a7 48 4f 53 54 49 44 4c 45 4e 29 20 29 7b 0d 0a 20  HOSTIDLEN) ){.. 
122a8 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
122a9 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a  n SQLITE_BUSY;..
122aa 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
122ab 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
122ac 20 20 20 20 20 20 20 2f 2a 20 64 6f 6e 27 74 20         /* don't 
122ad 62 72 65 61 6b 20 74 68 65 20 6c 6f 63 6b 20 6f  break the lock o
122ae 6e 20 73 68 6f 72 74 20 72 65 61 64 20 6f 72 20  n short read or 
122af 61 20 76 65 72 73 69 6f 6e 20 6d 69 73 6d 61 74  a version mismat
122b0 63 68 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  ch */..         
122b1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
122b2 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  USY;..        }.
122b3 0a 20 20 20 20 20 20 20 20 75 73 6c 65 65 70 28  .        usleep(
122b4 31 30 30 30 30 30 30 30 29 3b 20 2f 2a 20 77 61  10000000); /* wa
122b5 69 74 20 31 30 20 73 65 63 20 61 6e 64 20 74 72  it 10 sec and tr
122b6 79 20 74 68 65 20 6c 6f 63 6b 20 61 67 61 69 6e  y the lock again
122b7 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e   */..        con
122b8 74 69 6e 75 65 3b 20 0d 0a 20 20 20 20 20 20 7d  tinue; ..      }
122b9 0d 0a 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20  ..      ..      
122ba 61 73 73 65 72 74 28 20 6e 54 72 69 65 73 3d 3d  assert( nTries==
122bb 33 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  3 );..      if( 
122bc 30 3d 3d 70 72 6f 78 79 42 72 65 61 6b 43 6f 6e  0==proxyBreakCon
122bd 63 68 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 6d 79  chLock(pFile, my
122be 48 6f 73 74 49 44 29 20 29 7b 0d 0a 20 20 20 20  HostID) ){..    
122bf 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
122c0 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  OK;..        if(
122c1 20 6c 6f 63 6b 54 79 70 65 3d 3d 45 58 43 4c 55   lockType==EXCLU
122c2 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20  SIVE_LOCK ){..  
122c3 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e          rc = con
122c4 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
122c5 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  >xLock((sqlite3_
122c6 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
122c7 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 20 20   SHARED_LOCK);  
122c8 20 20 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20          ..      
122c9 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
122ca 20 21 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20   !rc ){..       
122cb 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c     rc = conchFil
122cc 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63  e->pMethod->xLoc
122cd 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
122ce 29 63 6f 6e 63 68 46 69 6c 65 2c 20 6c 6f 63 6b  )conchFile, lock
122cf 54 79 70 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Type);..        
122d0 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
122d1 7d 0d 0a 20 20 7d 20 77 68 69 6c 65 28 20 72 63  }..  } while( rc
122d2 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26  ==SQLITE_BUSY &&
122d3 20 6e 54 72 69 65 73 3c 33 20 29 3b 0d 0a 20 20   nTries<3 );..  
122d4 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
122d5 7d 0d 0a 0d 0a 2f 2a 20 54 61 6b 65 73 20 74 68  }..../* Takes th
122d6 65 20 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e  e conch by takin
122d7 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  g a shared lock 
122d8 61 6e 64 20 72 65 61 64 20 74 68 65 20 63 6f 6e  and read the con
122d9 74 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 20  tents conch, if 
122da 0d 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73  ..** lockPath is
122db 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68   non-NULL, the h
122dc 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20  ost ID and lock 
122dd 66 69 6c 65 20 70 61 74 68 20 6d 75 73 74 20 6d  file path must m
122de 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0d 0a  atch.  A NULL ..
122df 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e  ** lockPath mean
122e0 73 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 50  s that the lockP
122e1 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68  ath in the conch
122e2 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73   file will be us
122e3 65 64 20 69 66 20 74 68 65 20 0d 0a 2a 2a 20 68  ed if the ..** h
122e4 6f 73 74 20 49 44 73 20 6d 61 74 63 68 2c 20 6f  ost IDs match, o
122e5 72 20 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 74  r a new lock pat
122e6 68 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61  h will be genera
122e7 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
122e8 79 20 0d 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74  y ..** and writt
122e9 65 6e 20 74 6f 20 74 68 65 20 63 6f 6e 63 68 20  en to the conch 
122ea 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  file...*/..stati
122eb 63 20 69 6e 74 20 70 72 6f 78 79 54 61 6b 65 43  c int proxyTakeC
122ec 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  onch(unixFile *p
122ed 46 69 6c 65 29 7b 0d 0a 20 20 70 72 6f 78 79 4c  File){..  proxyL
122ee 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
122ef 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
122f0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
122f1 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
122f2 78 74 3b 20 0d 0a 20 20 0d 0a 20 20 69 66 28 20  xt; ..  ..  if( 
122f3 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 21  pCtx->conchHeld!
122f4 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
122f5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
122f6 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 75 6e 69 78  }else{..    unix
122f7 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20  File *conchFile 
122f8 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
122f9 65 3b 0d 0a 20 20 20 20 75 75 69 64 5f 74 20 6d  e;..    uuid_t m
122fa 79 48 6f 73 74 49 44 3b 0d 0a 20 20 20 20 69 6e  yHostID;..    in
122fb 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0d 0a 20  t pError = 0;.. 
122fc 20 20 20 63 68 61 72 20 72 65 61 64 42 75 66 5b     char readBuf[
122fd 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45  PROXY_MAXCONCHLE
122fe 4e 5d 3b 0d 0a 20 20 20 20 63 68 61 72 20 6c 6f  N];..    char lo
122ff 63 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45  ckPath[MAXPATHLE
12300 4e 5d 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a 74  N];..    char *t
12301 65 6d 70 4c 6f 63 6b 50 61 74 68 20 3d 20 4e 55  empLockPath = NU
12302 4c 4c 3b 0d 0a 20 20 20 20 69 6e 74 20 72 63 20  LL;..    int rc 
12303 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
12304 20 20 69 6e 74 20 63 72 65 61 74 65 43 6f 6e 63    int createConc
12305 68 20 3d 20 30 3b 0d 0a 20 20 20 20 69 6e 74 20  h = 0;..    int 
12306 68 6f 73 74 49 64 4d 61 74 63 68 20 3d 20 30 3b  hostIdMatch = 0;
12307 0d 0a 20 20 20 20 69 6e 74 20 72 65 61 64 4c 65  ..    int readLe
12308 6e 20 3d 20 30 3b 0d 0a 20 20 20 20 69 6e 74 20  n = 0;..    int 
12309 74 72 79 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 3d  tryOldLockPath =
1230a 20 30 3b 0d 0a 20 20 20 20 69 6e 74 20 66 6f 72   0;..    int for
1230b 63 65 4e 65 77 4c 6f 63 6b 50 61 74 68 20 3d 20  ceNewLockPath = 
1230c 30 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20 4f 53  0;..    ..    OS
1230d 54 52 41 43 45 28 28 22 54 41 4b 45 43 4f 4e 43  TRACE(("TAKECONC
1230e 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64  H  %d for %s pid
1230f 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c  =%d\n", conchFil
12310 65 2d 3e 68 2c 0d 0a 20 20 20 20 20 20 20 20 20  e->h,..         
12311 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50      (pCtx->lockP
12312 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d  roxyPath ? pCtx-
12313 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a  >lockProxyPath :
12314 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70   ":auto:"), getp
12315 69 64 28 29 29 29 3b 0d 0a 0d 0a 20 20 20 20 72  id()));....    r
12316 63 20 3d 20 70 72 6f 78 79 47 65 74 48 6f 73 74  c = proxyGetHost
12317 49 44 28 6d 79 48 6f 73 74 49 44 2c 20 26 70 45  ID(myHostID, &pE
12318 72 72 6f 72 29 3b 0d 0a 20 20 20 20 69 66 28 20  rror);..    if( 
12319 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54  (rc&0xff)==SQLIT
1231a 45 5f 49 4f 45 52 52 20 29 7b 0d 0a 20 20 20 20  E_IOERR ){..    
1231b 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
1231c 6e 6f 20 3d 20 70 45 72 72 6f 72 3b 0d 0a 20 20  no = pError;..  
1231d 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b      goto end_tak
1231e 65 63 6f 6e 63 68 3b 0d 0a 20 20 20 20 7d 0d 0a  econch;..    }..
1231f 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 6f      rc = proxyCo
12320 6e 63 68 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 6d  nchLock(pFile, m
12321 79 48 6f 73 74 49 44 2c 20 53 48 41 52 45 44 5f  yHostID, SHARED_
12322 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 69 66 28 20  LOCK);..    if( 
12323 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12324 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  ..      goto end
12325 5f 74 61 6b 65 63 6f 6e 63 68 3b 0d 0a 20 20 20  _takeconch;..   
12326 20 7d 0d 0a 20 20 20 20 2f 2a 20 72 65 61 64 20   }..    /* read 
12327 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 6e  the existing con
12328 63 68 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20  ch file */..    
12329 72 65 61 64 4c 65 6e 20 3d 20 73 65 65 6b 41 6e  readLen = seekAn
1232a 64 52 65 61 64 28 28 75 6e 69 78 46 69 6c 65 2a  dRead((unixFile*
1232b 29 63 6f 6e 63 68 46 69 6c 65 2c 20 30 2c 20 72  )conchFile, 0, r
1232c 65 61 64 42 75 66 2c 20 50 52 4f 58 59 5f 4d 41  eadBuf, PROXY_MA
1232d 58 43 4f 4e 43 48 4c 45 4e 29 3b 0d 0a 20 20 20  XCONCHLEN);..   
1232e 20 69 66 28 20 72 65 61 64 4c 65 6e 3c 30 20 29   if( readLen<0 )
1232f 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 2f 4f 20  {..      /* I/O 
12330 65 72 72 6f 72 3a 20 6c 61 73 74 45 72 72 6e 6f  error: lastErrno
12331 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 52   set by seekAndR
12332 65 61 64 20 2a 2f 0d 0a 20 20 20 20 20 20 70 46  ead */..      pF
12333 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
12334 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74   conchFile->last
12335 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 72 63  Errno;..      rc
12336 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
12337 52 45 41 44 3b 0d 0a 20 20 20 20 20 20 67 6f 74  READ;..      got
12338 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b  o end_takeconch;
12339 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
1233a 72 65 61 64 4c 65 6e 3c 3d 28 50 52 4f 58 59 5f  readLen<=(PROXY_
1233b 48 45 41 44 45 52 4c 45 4e 2b 50 52 4f 58 59 5f  HEADERLEN+PROXY_
1233c 48 4f 53 54 49 44 4c 45 4e 29 20 7c 7c 20 0d 0a  HOSTIDLEN) || ..
1233d 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 61               rea
1233e 64 42 75 66 5b 30 5d 21 3d 28 63 68 61 72 29 50  dBuf[0]!=(char)P
1233f 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 4f  ROXY_CONCHVERSIO
12340 4e 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 61  N ){..      /* a
12341 20 73 68 6f 72 74 20 72 65 61 64 20 6f 72 20 76   short read or v
12342 65 72 73 69 6f 6e 20 66 6f 72 6d 61 74 20 6d 69  ersion format mi
12343 73 6d 61 74 63 68 20 6d 65 61 6e 73 20 77 65 20  smatch means we 
12344 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  need to create a
12345 20 6e 65 77 20 0d 0a 20 20 20 20 20 20 2a 2a 20   new ..      ** 
12346 63 6f 6e 63 68 20 66 69 6c 65 2e 20 0d 0a 20 20  conch file. ..  
12347 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 63 72      */..      cr
12348 65 61 74 65 43 6f 6e 63 68 20 3d 20 31 3b 0d 0a  eateConch = 1;..
12349 20 20 20 20 7d 0d 0a 20 20 20 20 2f 2a 20 69 66      }..    /* if
1234a 20 74 68 65 20 68 6f 73 74 20 69 64 20 6d 61 74   the host id mat
1234b 63 68 65 73 20 61 6e 64 20 74 68 65 20 6c 6f 63  ches and the loc
1234c 6b 20 70 61 74 68 20 61 6c 72 65 61 64 79 20 65  k path already e
1234d 78 69 73 74 73 20 69 6e 20 74 68 65 20 63 6f 6e  xists in the con
1234e 63 68 0d 0a 20 20 20 20 2a 2a 20 77 65 27 6c 6c  ch..    ** we'll
1234f 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 20   try to use the 
12350 70 61 74 68 20 74 68 65 72 65 2c 20 69 66 20 77  path there, if w
12351 65 20 63 61 6e 27 74 20 6f 70 65 6e 20 74 68 61  e can't open tha
12352 74 20 70 61 74 68 2c 20 77 65 27 6c 6c 20 0d 0a  t path, we'll ..
12353 20 20 20 20 2a 2a 20 72 65 74 72 79 20 77 69 74      ** retry wit
12354 68 20 61 20 6e 65 77 20 61 75 74 6f 2d 67 65 6e  h a new auto-gen
12355 65 72 61 74 65 64 20 70 61 74 68 20 0d 0a 20 20  erated path ..  
12356 20 20 2a 2f 0d 0a 20 20 20 20 64 6f 20 7b 20 2f    */..    do { /
12357 2a 20 69 6e 20 63 61 73 65 20 77 65 20 6e 65 65  * in case we nee
12358 64 20 74 6f 20 74 72 79 20 61 67 61 69 6e 20 66  d to try again f
12359 6f 72 20 61 6e 20 3a 61 75 74 6f 3a 20 6e 61 6d  or an :auto: nam
1235a 65 64 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0d  ed lock file */.
1235b 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 21 63 72  ...      if( !cr
1235c 65 61 74 65 43 6f 6e 63 68 20 26 26 20 21 66 6f  eateConch && !fo
1235d 72 63 65 4e 65 77 4c 6f 63 6b 50 61 74 68 20 29  rceNewLockPath )
1235e 7b 0d 0a 20 20 20 20 20 20 20 20 68 6f 73 74 49  {..        hostI
1235f 64 4d 61 74 63 68 20 3d 20 21 6d 65 6d 63 6d 70  dMatch = !memcmp
12360 28 26 72 65 61 64 42 75 66 5b 50 52 4f 58 59 5f  (&readBuf[PROXY_
12361 48 45 41 44 45 52 4c 45 4e 5d 2c 20 6d 79 48 6f  HEADERLEN], myHo
12362 73 74 49 44 2c 20 0d 0a 20 20 20 20 20 20 20 20  stID, ..        
12363 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12364 20 20 20 20 20 20 20 20 20 20 50 52 4f 58 59 5f            PROXY_
12365 48 4f 53 54 49 44 4c 45 4e 29 3b 0d 0a 20 20 20  HOSTIDLEN);..   
12366 20 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 63       /* if the c
12367 6f 6e 63 68 20 68 61 73 20 64 61 74 61 20 63 6f  onch has data co
12368 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e  mpare the conten
12369 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69  ts */..        i
1236a 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  f( !pCtx->lockPr
1236b 6f 78 79 50 61 74 68 20 29 7b 0d 0a 20 20 20 20  oxyPath ){..    
1236c 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74        /* for aut
1236d 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f  o-named local lo
1236e 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68  ck file, just ch
1236f 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20  eck the host ID 
12370 61 6e 64 20 77 65 27 6c 6c 0d 0a 20 20 20 20 20  and we'll..     
12371 20 20 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65        ** use the
12372 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65   local lock file
12373 20 70 61 74 68 20 74 68 61 74 27 73 20 61 6c 72   path that's alr
12374 65 61 64 79 20 69 6e 20 74 68 65 72 65 0d 0a 20  eady in there.. 
12375 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20            */..  
12376 20 20 20 20 20 20 20 20 69 66 28 20 68 6f 73 74          if( host
12377 49 64 4d 61 74 63 68 20 29 7b 0d 0a 20 20 20 20  IdMatch ){..    
12378 20 20 20 20 20 20 20 20 73 69 7a 65 5f 74 20 70          size_t p
12379 61 74 68 4c 65 6e 20 3d 20 28 72 65 61 64 4c 65  athLen = (readLe
1237a 6e 20 2d 20 50 52 4f 58 59 5f 50 41 54 48 49 4e  n - PROXY_PATHIN
1237b 44 45 58 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  DEX);..         
1237c 20 20 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20     ..           
1237d 20 69 66 28 20 70 61 74 68 4c 65 6e 3e 3d 4d 41   if( pathLen>=MA
1237e 58 50 41 54 48 4c 45 4e 20 29 7b 0d 0a 20 20 20  XPATHLEN ){..   
1237f 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 4c             pathL
12380 65 6e 3d 4d 41 58 50 41 54 48 4c 45 4e 2d 31 3b  en=MAXPATHLEN-1;
12381 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d  ..            }.
12382 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
12383 63 70 79 28 6c 6f 63 6b 50 61 74 68 2c 20 26 72  cpy(lockPath, &r
12384 65 61 64 42 75 66 5b 50 52 4f 58 59 5f 50 41 54  eadBuf[PROXY_PAT
12385 48 49 4e 44 45 58 5d 2c 20 70 61 74 68 4c 65 6e  HINDEX], pathLen
12386 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
12387 6c 6f 63 6b 50 61 74 68 5b 70 61 74 68 4c 65 6e  lockPath[pathLen
12388 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  ] = 0;..        
12389 20 20 20 20 74 65 6d 70 4c 6f 63 6b 50 61 74 68      tempLockPath
1238a 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0d 0a 20 20   = lockPath;..  
1238b 20 20 20 20 20 20 20 20 20 20 74 72 79 4f 6c 64            tryOld
1238c 4c 6f 63 6b 50 61 74 68 20 3d 20 31 3b 0d 0a 20  LockPath = 1;.. 
1238d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 72             /* cr
1238e 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74  eate a copy of t
1238f 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 69 66 20  he lock path if 
12390 74 68 65 20 63 6f 6e 63 68 20 69 73 20 74 61 6b  the conch is tak
12391 65 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  en */..         
12392 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65     goto end_take
12393 63 6f 6e 63 68 3b 0d 0a 20 20 20 20 20 20 20 20  conch;..        
12394 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c    }..        }el
12395 73 65 20 69 66 28 20 68 6f 73 74 49 64 4d 61 74  se if( hostIdMat
12396 63 68 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ch..            
12397 20 20 20 26 26 20 21 73 74 72 6e 63 6d 70 28 70     && !strncmp(p
12398 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
12399 74 68 2c 20 26 72 65 61 64 42 75 66 5b 50 52 4f  th, &readBuf[PRO
1239a 58 59 5f 50 41 54 48 49 4e 44 45 58 5d 2c 0d 0a  XY_PATHINDEX],..
1239b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1239c 20 20 20 20 20 20 20 20 20 20 20 72 65 61 64 4c             readL
1239d 65 6e 2d 50 52 4f 58 59 5f 50 41 54 48 49 4e 44  en-PROXY_PATHIND
1239e 45 58 29 0d 0a 20 20 20 20 20 20 20 20 29 7b 0d  EX)..        ){.
1239f 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  .          /* co
123a0 6e 63 68 20 68 6f 73 74 20 61 6e 64 20 6c 6f 63  nch host and loc
123a1 6b 20 70 61 74 68 20 6d 61 74 63 68 20 2a 2f 0d  k path match */.
123a2 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
123a3 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 20 0d  end_takeconch; .
123a4 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
123a5 20 20 7d 0d 0a 20 20 20 20 20 20 0d 0a 20 20 20    }..      ..   
123a6 20 20 20 2f 2a 20 69 66 20 74 68 65 20 63 6f 6e     /* if the con
123a7 63 68 20 69 73 6e 27 74 20 77 72 69 74 61 62 6c  ch isn't writabl
123a8 65 20 61 6e 64 20 64 6f 65 73 6e 27 74 20 6d 61  e and doesn't ma
123a9 74 63 68 2c 20 77 65 20 63 61 6e 27 74 20 74 61  tch, we can't ta
123aa 6b 65 20 69 74 20 2a 2f 0d 0a 20 20 20 20 20 20  ke it */..      
123ab 69 66 28 20 28 63 6f 6e 63 68 46 69 6c 65 2d 3e  if( (conchFile->
123ac 6f 70 65 6e 46 6c 61 67 73 26 4f 5f 52 44 57 52  openFlags&O_RDWR
123ad 29 20 3d 3d 20 30 20 29 7b 0d 0a 20 20 20 20 20  ) == 0 ){..     
123ae 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
123af 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f  USY;..        go
123b0 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68  to end_takeconch
123b1 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
123b2 20 20 0d 0a 20 20 20 20 20 20 2f 2a 20 65 69 74    ..      /* eit
123b3 68 65 72 20 74 68 65 20 63 6f 6e 63 68 20 64 69  her the conch di
123b4 64 6e 27 74 20 6d 61 74 63 68 20 6f 72 20 77 65  dn't match or we
123b5 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
123b6 61 20 6e 65 77 20 6f 6e 65 20 2a 2f 0d 0a 20 20  a new one */..  
123b7 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c      if( !pCtx->l
123b8 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0d  ockProxyPath ){.
123b9 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 47 65  .        proxyGe
123ba 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e  tLockPath(pCtx->
123bb 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68  dbPath, lockPath
123bc 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0d 0a  , MAXPATHLEN);..
123bd 20 20 20 20 20 20 20 20 74 65 6d 70 4c 6f 63 6b          tempLock
123be 50 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b  Path = lockPath;
123bf 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 72 65  ..        /* cre
123c0 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ate a copy of th
123c1 65 20 6c 6f 63 6b 20 70 61 74 68 20 5f 6f 6e 6c  e lock path _onl
123c2 79 5f 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20  y_ if the conch 
123c3 69 73 20 74 61 6b 65 6e 20 2a 2f 0d 0a 20 20 20  is taken */..   
123c4 20 20 20 7d 0d 0a 20 20 20 20 20 20 0d 0a 20 20     }..      ..  
123c5 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f      /* update co
123c6 6e 63 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e  nch with host an
123c7 64 20 70 61 74 68 20 28 74 68 69 73 20 77 69 6c  d path (this wil
123c8 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20  l fail if other 
123c9 70 72 6f 63 65 73 73 0d 0a 20 20 20 20 20 20 2a  process..      *
123ca 2a 20 68 61 73 20 61 20 73 68 61 72 65 64 20 6c  * has a shared l
123cb 6f 63 6b 20 61 6c 72 65 61 64 79 29 2c 20 69 66  ock already), if
123cc 20 74 68 65 20 68 6f 73 74 20 69 64 20 6d 61 74   the host id mat
123cd 63 68 65 73 2c 20 75 73 65 20 74 68 65 20 62 69  ches, use the bi
123ce 67 0d 0a 20 20 20 20 20 20 2a 2a 20 73 74 69 63  g..      ** stic
123cf 6b 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  k...      */..  
123d0 20 20 20 20 66 75 74 69 6d 65 73 28 63 6f 6e 63      futimes(conc
123d1 68 46 69 6c 65 2d 3e 68 2c 20 4e 55 4c 4c 29 3b  hFile->h, NULL);
123d2 0d 0a 20 20 20 20 20 20 69 66 28 20 68 6f 73 74  ..      if( host
123d3 49 64 4d 61 74 63 68 20 26 26 20 21 63 72 65 61  IdMatch && !crea
123d4 74 65 43 6f 6e 63 68 20 29 7b 0d 0a 20 20 20 20  teConch ){..    
123d5 20 20 20 20 69 66 28 20 63 6f 6e 63 68 46 69 6c      if( conchFil
123d6 65 2d 3e 70 49 6e 6f 64 65 20 26 26 20 63 6f 6e  e->pInode && con
123d7 63 68 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e  chFile->pInode->
123d8 6e 53 68 61 72 65 64 3e 31 20 29 7b 0d 0a 20 20  nShared>1 ){..  
123d9 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72          /* We ar
123da 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20  e trying for an 
123db 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62  exclusive lock b
123dc 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  ut another threa
123dd 64 20 69 6e 20 74 68 69 73 0d 0a 20 20 20 20 20  d in this..     
123de 20 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72        ** same pr
123df 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68  ocess is still h
123e0 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20  olding a shared 
123e1 6c 6f 63 6b 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  lock. */..      
123e2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
123e3 42 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 7d  BUSY;..        }
123e4 20 65 6c 73 65 20 7b 20 20 20 20 20 20 20 20 20   else {         
123e5 20 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20   ..          rc 
123e6 3d 20 70 72 6f 78 79 43 6f 6e 63 68 4c 6f 63 6b  = proxyConchLock
123e7 28 70 46 69 6c 65 2c 20 6d 79 48 6f 73 74 49 44  (pFile, myHostID
123e8 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  , EXCLUSIVE_LOCK
123e9 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
123ea 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
123eb 20 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46       rc = conchF
123ec 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c  ile->pMethod->xL
123ed 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
123ee 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58  e*)conchFile, EX
123ef 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0d 0a  CLUSIVE_LOCK);..
123f0 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
123f1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
123f2 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61   ){..        cha
123f3 72 20 77 72 69 74 65 42 75 66 66 65 72 5b 50 52  r writeBuffer[PR
123f4 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d  OXY_MAXCONCHLEN]
123f5 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 77  ;..        int w
123f6 72 69 74 65 53 69 7a 65 20 3d 20 30 3b 0d 0a 20  riteSize = 0;.. 
123f7 20 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20 20         ..       
123f8 20 77 72 69 74 65 42 75 66 66 65 72 5b 30 5d 20   writeBuffer[0] 
123f9 3d 20 28 63 68 61 72 29 50 52 4f 58 59 5f 43 4f  = (char)PROXY_CO
123fa 4e 43 48 56 45 52 53 49 4f 4e 3b 0d 0a 20 20 20  NCHVERSION;..   
123fb 20 20 20 20 20 6d 65 6d 63 70 79 28 26 77 72 69       memcpy(&wri
123fc 74 65 42 75 66 66 65 72 5b 50 52 4f 58 59 5f 48  teBuffer[PROXY_H
123fd 45 41 44 45 52 4c 45 4e 5d 2c 20 6d 79 48 6f 73  EADERLEN], myHos
123fe 74 49 44 2c 20 50 52 4f 58 59 5f 48 4f 53 54 49  tID, PROXY_HOSTI
123ff 44 4c 45 4e 29 3b 0d 0a 20 20 20 20 20 20 20 20  DLEN);..        
12400 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  if( pCtx->lockPr
12401 6f 78 79 50 61 74 68 21 3d 4e 55 4c 4c 20 29 7b  oxyPath!=NULL ){
12402 0d 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 6c  ..          strl
12403 63 70 79 28 26 77 72 69 74 65 42 75 66 66 65 72  cpy(&writeBuffer
12404 5b 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58  [PROXY_PATHINDEX
12405 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f  ], pCtx->lockPro
12406 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  xyPath, MAXPATHL
12407 45 4e 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  EN);..        }e
12408 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
12409 73 74 72 6c 63 70 79 28 26 77 72 69 74 65 42 75  strlcpy(&writeBu
1240a 66 66 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 49  ffer[PROXY_PATHI
1240b 4e 44 45 58 5d 2c 20 74 65 6d 70 4c 6f 63 6b 50  NDEX], tempLockP
1240c 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
1240d 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
1240e 20 20 20 20 20 20 77 72 69 74 65 53 69 7a 65 20        writeSize 
1240f 3d 20 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45  = PROXY_PATHINDE
12410 58 20 2b 20 73 74 72 6c 65 6e 28 26 77 72 69 74  X + strlen(&writ
12411 65 42 75 66 66 65 72 5b 50 52 4f 58 59 5f 50 41  eBuffer[PROXY_PA
12412 54 48 49 4e 44 45 58 5d 29 3b 0d 0a 20 20 20 20  THINDEX]);..    
12413 20 20 20 20 72 6f 62 75 73 74 5f 66 74 72 75 6e      robust_ftrun
12414 63 61 74 65 28 63 6f 6e 63 68 46 69 6c 65 2d 3e  cate(conchFile->
12415 68 2c 20 77 72 69 74 65 53 69 7a 65 29 3b 0d 0a  h, writeSize);..
12416 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69          rc = uni
12417 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 5f  xWrite((sqlite3_
12418 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65  file *)conchFile
12419 2c 20 77 72 69 74 65 42 75 66 66 65 72 2c 20 77  , writeBuffer, w
1241a 72 69 74 65 53 69 7a 65 2c 20 30 29 3b 0d 0a 20  riteSize, 0);.. 
1241b 20 20 20 20 20 20 20 66 73 79 6e 63 28 63 6f 6e         fsync(con
1241c 63 68 46 69 6c 65 2d 3e 68 29 3b 0d 0a 20 20 20  chFile->h);..   
1241d 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 72       /* If we cr
1241e 65 61 74 65 64 20 61 20 6e 65 77 20 63 6f 6e 63  eated a new conc
1241f 68 20 66 69 6c 65 20 28 6e 6f 74 20 6a 75 73 74  h file (not just
12420 20 75 70 64 61 74 65 64 20 74 68 65 20 63 6f 6e   updated the con
12421 74 65 6e 74 73 20 6f 66 20 61 20 0d 0a 20 20 20  tents of a ..   
12422 20 20 20 20 20 20 2a 2a 20 76 61 6c 69 64 20 63        ** valid c
12423 6f 6e 63 68 20 66 69 6c 65 29 2c 20 74 72 79 20  onch file), try 
12424 74 6f 20 6d 61 74 63 68 20 74 68 65 20 70 65 72  to match the per
12425 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  missions of the 
12426 64 61 74 61 62 61 73 65 20 0d 0a 20 20 20 20 20  database ..     
12427 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
12428 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12429 4b 20 26 26 20 63 72 65 61 74 65 43 6f 6e 63 68  K && createConch
1242a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
1242b 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0d  truct stat buf;.
1242c 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 65  .          int e
1242d 72 72 20 3d 20 6f 73 46 73 74 61 74 28 70 46 69  rr = osFstat(pFi
1242e 6c 65 2d 3e 68 2c 20 26 62 75 66 29 3b 0d 0a 20  le->h, &buf);.. 
1242f 20 20 20 20 20 20 20 20 20 69 66 28 20 65 72 72           if( err
12430 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
12431 20 20 20 20 6d 6f 64 65 5f 74 20 63 6d 6f 64 65      mode_t cmode
12432 20 3d 20 62 75 66 2e 73 74 5f 6d 6f 64 65 26 28   = buf.st_mode&(
12433 53 5f 49 52 55 53 52 7c 53 5f 49 57 55 53 52 20  S_IRUSR|S_IWUSR 
12434 7c 20 53 5f 49 52 47 52 50 7c 53 5f 49 57 47 52  | S_IRGRP|S_IWGR
12435 50 20 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  P |..           
12436 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12437 20 20 20 20 20 20 20 20 20 20 20 20 20 53 5f 49               S_I
12438 52 4f 54 48 7c 53 5f 49 57 4f 54 48 29 3b 0d 0a  ROTH|S_IWOTH);..
12439 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
1243a 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ry to match the 
1243b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 52 2f  database file R/
1243c 57 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c 20 69  W permissions, i
1243d 67 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 2a 2f  gnore failure */
1243e 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1243f 5f 50 52 4f 58 59 5f 44 45 42 55 47 0d 0a 20 20  _PROXY_DEBUG..  
12440 20 20 20 20 20 20 20 20 20 20 6f 73 46 63 68 6d            osFchm
12441 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  od(conchFile->h,
12442 20 63 6d 6f 64 65 29 3b 0d 0a 23 65 6c 73 65 0d   cmode);..#else.
12443 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6f 7b  .            do{
12444 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
12445 72 63 20 3d 20 6f 73 46 63 68 6d 6f 64 28 63 6f  rc = osFchmod(co
12446 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 63 6d 6f 64  nchFile->h, cmod
12447 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  e);..           
12448 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 28 2d 31   }while( rc==(-1
12449 29 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54  ) && errno==EINT
1244a 52 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  R );..          
1244b 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0d 0a    if( rc!=0 ){..
1244c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
1244d 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0d  t code = errno;.
1244e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
1244f 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
12450 66 63 68 6d 6f 64 20 25 6f 20 46 41 49 4c 45 44  fchmod %o FAILED
12451 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 0d   with %d %s\n",.
12452 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
12453 20 20 20 20 20 20 20 63 6d 6f 64 65 2c 20 63 6f         cmode, co
12454 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64  de, strerror(cod
12455 65 29 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  e));..          
12456 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20    } else {..    
12457 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
12458 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f  f(stderr, "fchmo
12459 64 20 25 6f 20 53 55 43 43 45 44 45 44 5c 6e 22  d %o SUCCEDED\n"
1245a 2c 63 6d 6f 64 65 29 3b 0d 0a 20 20 20 20 20 20  ,cmode);..      
1245b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
1245c 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1245d 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
1245e 3d 20 65 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20  = errno;..      
1245f 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
12460 64 65 72 72 2c 20 22 53 54 41 54 20 46 41 49 4c  derr, "STAT FAIL
12461 45 44 5b 25 64 5d 20 77 69 74 68 20 25 64 20 25  ED[%d] with %d %
12462 73 5c 6e 22 2c 20 0d 0a 20 20 20 20 20 20 20 20  s\n", ..        
12463 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c              err,
12464 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28   code, strerror(
12465 63 6f 64 65 29 29 3b 0d 0a 23 65 6e 64 69 66 0d  code));..#endif.
12466 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
12467 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
12468 0d 0a 20 20 20 20 20 20 63 6f 6e 63 68 46 69 6c  ..      conchFil
12469 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c  e->pMethod->xUnl
1246a 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
1246b 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48  e*)conchFile, SH
1246c 41 52 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20  ARED_LOCK);..   
1246d 20 20 20 0d 0a 20 20 20 20 65 6e 64 5f 74 61 6b     ..    end_tak
1246e 65 63 6f 6e 63 68 3a 0d 0a 20 20 20 20 20 20 4f  econch:..      O
1246f 53 54 52 41 43 45 28 28 22 54 52 41 4e 53 50 52  STRACE(("TRANSPR
12470 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e  OXY: CLOSE  %d\n
12471 22 2c 20 70 46 69 6c 65 2d 3e 68 29 29 3b 0d 0a  ", pFile->h));..
12472 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
12473 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65  LITE_OK && pFile
12474 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0d 0a  ->openFlags ){..
12475 20 20 20 20 20 20 20 20 69 6e 74 20 66 64 3b 0d          int fd;.
12476 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 46 69  .        if( pFi
12477 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0d 0a 20 20 20  le->h>=0 ){..   
12478 20 20 20 20 20 20 20 72 6f 62 75 73 74 5f 63 6c         robust_cl
12479 6f 73 65 28 70 46 69 6c 65 2c 20 70 46 69 6c 65  ose(pFile, pFile
1247a 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0d  ->h, __LINE__);.
1247b 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
1247c 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
1247d 31 3b 0d 0a 20 20 20 20 20 20 20 20 66 64 20 3d  1;..        fd =
1247e 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 43 74   robust_open(pCt
1247f 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
12480 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 0d 0a 20 20  ->openFlags,..  
12481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12482 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55      SQLITE_DEFAU
12483 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
12484 4f 4e 53 29 3b 0d 0a 20 20 20 20 20 20 20 20 4f  ONS);..        O
12485 53 54 52 41 43 45 28 28 22 54 52 41 4e 53 50 52  STRACE(("TRANSPR
12486 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64 5c 6e 22  OXY: OPEN  %d\n"
12487 2c 20 66 64 29 29 3b 0d 0a 20 20 20 20 20 20 20  , fd));..       
12488 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0d 0a 20   if( fd>=0 ){.. 
12489 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
1248a 68 20 3d 20 66 64 3b 0d 0a 20 20 20 20 20 20 20  h = fd;..       
1248b 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
1248c 20 20 20 72 63 3d 53 51 4c 49 54 45 5f 43 41 4e     rc=SQLITE_CAN
1248d 54 4f 50 45 4e 5f 42 4b 50 54 3b 20 2f 2a 20 53  TOPEN_BKPT; /* S
1248e 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 6f 78  QLITE_BUSY? prox
1248f 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c 65  yTakeConch calle
12490 64 0d 0a 20 20 20 20 20 20 20 20 20 20 20 64 75  d..           du
12491 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0d  ring locking */.
12492 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
12493 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
12494 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
12495 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  !pCtx->lockProxy
12496 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61   ){..        cha
12497 72 20 2a 70 61 74 68 20 3d 20 74 65 6d 70 4c 6f  r *path = tempLo
12498 63 6b 50 61 74 68 20 3f 20 74 65 6d 70 4c 6f 63  ckPath ? tempLoc
12499 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e 6c 6f  kPath : pCtx->lo
1249a 63 6b 50 72 6f 78 79 50 61 74 68 3b 0d 0a 20 20  ckProxyPath;..  
1249b 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
1249c 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70  CreateUnixFile(p
1249d 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 6b  ath, &pCtx->lock
1249e 50 72 6f 78 79 2c 20 31 29 3b 0d 0a 20 20 20 20  Proxy, 1);..    
1249f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
124a0 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
124a1 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 74 72 79  ITE_NOMEM && try
124a2 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 29 7b 0d 0a  OldLockPath ){..
124a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65 20            /* we 
124a4 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 20  couldn't create 
124a5 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66  the proxy lock f
124a6 69 6c 65 20 77 69 74 68 20 74 68 65 20 6f 6c 64  ile with the old
124a7 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 0d   lock file path.
124a8 0a 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73  .           ** s
124a9 6f 20 74 72 79 20 61 67 61 69 6e 20 76 69 61 20  o try again via 
124aa 61 75 74 6f 2d 6e 61 6d 69 6e 67 20 0d 0a 20 20  auto-naming ..  
124ab 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20           */..   
124ac 20 20 20 20 20 20 20 66 6f 72 63 65 4e 65 77 4c         forceNewL
124ad 6f 63 6b 50 61 74 68 20 3d 20 31 3b 0d 0a 20 20  ockPath = 1;..  
124ae 20 20 20 20 20 20 20 20 74 72 79 4f 6c 64 4c 6f          tryOldLo
124af 63 6b 50 61 74 68 20 3d 20 30 3b 0d 0a 20 20 20  ckPath = 0;..   
124b0 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
124b1 20 2f 2a 20 67 6f 20 62 61 63 6b 20 74 6f 20 74   /* go back to t
124b2 68 65 20 64 6f 20 7b 7d 20 77 68 69 6c 65 20 73  he do {} while s
124b3 74 61 72 74 20 70 6f 69 6e 74 2c 20 74 72 79 20  tart point, try 
124b4 61 67 61 69 6e 20 2a 2f 0d 0a 20 20 20 20 20 20  again */..      
124b5 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
124b6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
124b7 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
124b8 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 6d 61 6b    /* Need to mak
124b9 65 20 61 20 63 6f 70 79 20 6f 66 20 70 61 74 68  e a copy of path
124ba 20 69 66 20 77 65 20 65 78 74 72 61 63 74 65 64   if we extracted
124bb 20 74 68 65 20 76 61 6c 75 65 0d 0a 20 20 20 20   the value..    
124bc 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65       ** from the
124bd 20 63 6f 6e 63 68 20 66 69 6c 65 20 6f 72 20 74   conch file or t
124be 68 65 20 70 61 74 68 20 77 61 73 20 61 6c 6c 6f  he path was allo
124bf 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61  cated on the sta
124c0 63 6b 0d 0a 20 20 20 20 20 20 20 20 20 2a 2f 0d  ck..         */.
124c1 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 65 6d  .        if( tem
124c2 70 4c 6f 63 6b 50 61 74 68 20 29 7b 0d 0a 20 20  pLockPath ){..  
124c3 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f          pCtx->lo
124c4 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71  ckProxyPath = sq
124c5 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
124c6 20 74 65 6d 70 4c 6f 63 6b 50 61 74 68 29 3b 0d   tempLockPath);.
124c7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
124c8 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
124c9 61 74 68 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ath ){..        
124ca 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
124cb 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 20 20  NOMEM;..        
124cc 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a    }..        }..
124cd 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
124ce 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
124cf 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 74   ){..        pCt
124d0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 31  x->conchHeld = 1
124d1 3b 0d 0a 20 20 20 20 20 20 20 20 0d 0a 20 20 20  ;..        ..   
124d2 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c       if( pCtx->l
124d3 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f  ockProxy->pMetho
124d4 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f  d == &afpIoMetho
124d5 64 73 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  ds ){..         
124d6 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
124d7 78 74 20 2a 61 66 70 43 74 78 3b 0d 0a 20 20 20  xt *afpCtx;..   
124d8 20 20 20 20 20 20 20 61 66 70 43 74 78 20 3d 20         afpCtx = 
124d9 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
124da 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b 50  xt *)pCtx->lockP
124db 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  roxy->lockingCon
124dc 74 65 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 20  text;..         
124dd 20 61 66 70 43 74 78 2d 3e 64 62 50 61 74 68 20   afpCtx->dbPath 
124de 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
124df 79 50 61 74 68 3b 0d 0a 20 20 20 20 20 20 20 20  yPath;..        
124e0 7d 0d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  }..      } else 
124e1 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 63 68  {..        conch
124e2 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
124e3 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  Unlock((sqlite3_
124e4 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
124e5 20 4e 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20   NO_LOCK);..    
124e6 20 20 7d 0d 0a 20 20 20 20 20 20 4f 53 54 52 41    }..      OSTRA
124e7 43 45 28 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE(("TAKECONCH  
124e8 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46  %d %s\n", conchF
124e9 69 6c 65 2d 3e 68 2c 0d 0a 20 20 20 20 20 20 20  ile->h,..       
124ea 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49          rc==SQLI
124eb 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c  TE_OK?"ok":"fail
124ec 65 64 22 29 29 3b 0d 0a 20 20 20 20 20 20 72 65  ed"));..      re
124ed 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 20  turn rc;..    } 
124ee 77 68 69 6c 65 20 28 31 29 3b 20 2f 2a 20 69 6e  while (1); /* in
124ef 20 63 61 73 65 20 77 65 20 6e 65 65 64 20 74 6f   case we need to
124f0 20 72 65 74 72 79 20 74 68 65 20 3a 61 75 74 6f   retry the :auto
124f1 3a 20 6c 6f 63 6b 20 66 69 6c 65 20 2d 20 0d 0a  : lock file - ..
124f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
124f3 20 2a 2a 20 77 65 20 73 68 6f 75 6c 64 20 6e 65   ** we should ne
124f4 76 65 72 20 67 65 74 20 68 65 72 65 20 65 78 63  ver get here exc
124f5 65 70 74 20 76 69 61 20 74 68 65 20 27 63 6f 6e  ept via the 'con
124f6 74 69 6e 75 65 27 20 63 61 6c 6c 2e 20 2a 2f 0d  tinue' call. */.
124f7 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
124f8 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73  * If pFile holds
124f9 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e   a lock on a con
124fa 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65  ch file, then re
124fb 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e  lease that lock.
124fc 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
124fd 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e   proxyReleaseCon
124fe 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  ch(unixFile *pFi
124ff 6c 65 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  le){..  int rc =
12500 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
12501 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e      /* Subroutin
12502 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  e return code */
12503 0d 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  ..  proxyLocking
12504 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20  Context *pCtx;  
12505 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63  /* The locking c
12506 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 70  ontext for the p
12507 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20  roxy lock */..  
12508 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46  unixFile *conchF
12509 69 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ile;        /* N
1250a 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68  ame of the conch
1250b 20 66 69 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 70 43   file */....  pC
1250c 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
1250d 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
1250e 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
1250f 74 3b 0d 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20  t;..  conchFile 
12510 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
12511 65 3b 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22  e;..  OSTRACE(("
12512 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64  RELEASECONCH  %d
12513 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e   for %s pid=%d\n
12514 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  ", conchFile->h,
12515 0d 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 43  ..           (pC
12516 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
12517 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  h ? pCtx->lockPr
12518 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f  oxyPath : ":auto
12519 3a 22 29 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  :"), ..         
1251a 20 20 67 65 74 70 69 64 28 29 29 29 3b 0d 0a 20    getpid()));.. 
1251b 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
1251c 48 65 6c 64 3e 30 20 29 7b 0d 0a 20 20 20 20 72  Held>0 ){..    r
1251d 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
1251e 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28  Method->xUnlock(
1251f 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
12520 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43  onchFile, NO_LOC
12521 4b 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 43 74 78  K);..  }..  pCtx
12522 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b  ->conchHeld = 0;
12523 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 52 45  ..  OSTRACE(("RE
12524 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25  LEASECONCH  %d %
12525 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d  s\n", conchFile-
12526 3e 68 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  >h,..           
12527 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f  (rc==SQLITE_OK ?
12528 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
12529 29 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  )));..  return r
1252a 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1252b 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  Given the name o
1252c 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  f a database fil
1252d 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e  e, compute the n
1252e 61 6d 65 20 6f 66 20 69 74 73 20 63 6f 6e 63 68  ame of its conch
1252f 20 66 69 6c 65 2e 0d 0a 2a 2a 20 53 74 6f 72 65   file...** Store
12530 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e   the conch filen
12531 61 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  ame in memory ob
12532 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
12533 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a  te3_malloc()...*
12534 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 61  * Make *pConchPa
12535 74 68 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  th point to the 
12536 6e 65 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 72  new name.  Retur
12537 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
12538 75 63 63 65 73 73 0d 0a 2a 2a 20 6f 72 20 53 51  uccess..** or SQ
12539 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e  LITE_NOMEM if un
1253a 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d  able to obtain m
1253b 65 6d 6f 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  emory...**..** T
1253c 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
1253d 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73  ponsible for ens
1253e 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 61  uring that the a
1253f 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0d  llocated memory.
12540 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 65  .** space is eve
12541 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0d 0a  ntually freed...
12542 2a 2a 0d 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61  **..** *pConchPa
12543 74 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  th is set to NUL
12544 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  L if a memory al
12545 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
12546 63 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ccurs...*/..stat
12547 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65 61  ic int proxyCrea
12548 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28  teConchPathname(
12549 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68  char *dbPath, ch
1254a 61 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29  ar **pConchPath)
1254b 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  {..  int i;     
1254c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1254d 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1254e 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c 65 6e  er */..  int len
1254f 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64   = (int)strlen(d
12550 62 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74  bPath); /* Lengt
12551 68 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  h of database fi
12552 6c 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 68 20  lename - dbPath 
12553 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63  */..  char *conc
12554 68 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20  hPath;          
12555 20 20 20 20 2f 2a 20 62 75 66 66 65 72 20 69 6e      /* buffer in
12556 20 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 72   which to constr
12557 75 63 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 2a  uct conch name *
12558 2f 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  /....  /* Alloca
12559 74 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  te space for the
1255a 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20   conch filename 
1255b 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  and initialize t
1255c 68 65 20 6e 61 6d 65 20 74 6f 0d 0a 20 20 2a 2a  he name to..  **
1255d 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1255e 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
1255f 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 0d 0a 20  se file. */  .. 
12560 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d 20 63   *pConchPath = c
12561 6f 6e 63 68 50 61 74 68 20 3d 20 28 63 68 61 72  onchPath = (char
12562 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
12563 63 28 6c 65 6e 20 2b 20 38 29 3b 0d 0a 20 20 69  c(len + 8);..  i
12564 66 28 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 20  f( conchPath==0 
12565 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ){..    return S
12566 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
12567 7d 0d 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e 63  }..  memcpy(conc
12568 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c 20 6c  hPath, dbPath, l
12569 65 6e 2b 31 29 3b 0d 0a 20 20 0d 0a 20 20 2f 2a  en+1);..  ..  /*
1256a 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e   now insert a ".
1256b 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73  " before the las
1256c 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f  t / character */
1256d 0d 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d  ..  for( i=(len-
1256e 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b  1); i>=0; i-- ){
1256f 0d 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50  ..    if( conchP
12570 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0d 0a  ath[i]=='/' ){..
12571 20 20 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 20 20        i++;..    
12572 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
12573 0a 20 20 7d 0d 0a 20 20 63 6f 6e 63 68 50 61 74  .  }..  conchPat
12574 68 5b 69 5d 3d 27 2e 27 3b 0d 0a 20 20 77 68 69  h[i]='.';..  whi
12575 6c 65 20 28 20 69 3c 6c 65 6e 20 29 7b 0d 0a 20  le ( i<len ){.. 
12576 20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31     conchPath[i+1
12577 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0d 0a 20 20  ]=dbPath[i];..  
12578 20 20 69 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20    i++;..  }.... 
12579 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 65 20 22   /* append the "
1257a 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 78 20 74  -conch" suffix t
1257b 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a 20  o the file */.. 
1257c 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 50 61   memcpy(&conchPa
1257d 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 68  th[i+1], "-conch
1257e 22 2c 20 37 29 3b 0d 0a 20 20 61 73 73 65 72 74  ", 7);..  assert
1257f 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f  ( (int)strlen(co
12580 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b  nchPath) == len+
12581 37 20 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  7 );....  return
12582 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
12583 0d 0a 0d 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66  ..../* Takes a f
12584 75 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20  ully configured 
12585 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74  proxy locking-st
12586 79 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 61 6e  yle unix file an
12587 64 20 73 77 69 74 63 68 65 73 0d 0a 2a 2a 20 74  d switches..** t
12588 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69  he local lock fi
12589 6c 65 20 70 61 74 68 20 0d 0a 2a 2f 0d 0a 73 74  le path ..*/..st
1258a 61 74 69 63 20 69 6e 74 20 73 77 69 74 63 68 4c  atic int switchL
1258b 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 75 6e 69  ockProxyPath(uni
1258c 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f  xFile *pFile, co
1258d 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 20  nst char *path) 
1258e 7b 0d 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  {..  proxyLockin
1258f 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
12590 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
12591 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f  ntext*)pFile->lo
12592 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a 20  ckingContext;.. 
12593 20 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 20 3d   char *oldPath =
12594 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
12595 50 61 74 68 3b 0d 0a 20 20 69 6e 74 20 72 63 20  Path;..  int rc 
12596 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a  = SQLITE_OK;....
12597 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69    if( pFile->eFi
12598 6c 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20  leLock!=NO_LOCK 
12599 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ){..    return S
1259a 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20 7d  QLITE_BUSY;..  }
1259b 20 20 0d 0a 0d 0a 20 20 2f 2a 20 6e 6f 74 68 69    ....  /* nothi
1259c 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20  ng to do if the 
1259d 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61  path is NULL, :a
1259e 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20  uto: or matches 
1259f 74 68 65 20 65 78 69 73 74 69 6e 67 20 70 61 74  the existing pat
125a0 68 20 2a 2f 0d 0a 20 20 69 66 28 20 21 70 61 74  h */..  if( !pat
125a1 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c  h || path[0]=='\
125a2 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61  0' || !strcmp(pa
125a3 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c  th, ":auto:") ||
125a4 0d 0a 20 20 20 20 28 6f 6c 64 50 61 74 68 20 26  ..    (oldPath &
125a5 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61  & !strncmp(oldPa
125a6 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 54  th, path, MAXPAT
125a7 48 4c 45 4e 29 29 20 29 7b 0d 0a 20 20 20 20 72  HLEN)) ){..    r
125a8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
125a9 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
125aa 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72  unixFile *lockPr
125ab 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b  oxy = pCtx->lock
125ac 50 72 6f 78 79 3b 0d 0a 20 20 20 20 70 43 74 78  Proxy;..    pCtx
125ad 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c  ->lockProxy=NULL
125ae 3b 0d 0a 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e  ;..    pCtx->con
125af 63 68 48 65 6c 64 20 3d 20 30 3b 0d 0a 20 20 20  chHeld = 0;..   
125b0 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 21 3d   if( lockProxy!=
125b1 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 20 20 72  NULL ){..      r
125b2 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65  c=lockProxy->pMe
125b3 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71  thod->xClose((sq
125b4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f 63  lite3_file *)loc
125b5 6b 50 72 6f 78 79 29 3b 0d 0a 20 20 20 20 20 20  kProxy);..      
125b6 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
125b7 72 63 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  rc;..      sqlit
125b8 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78  e3_free(lockProx
125b9 79 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  y);..    }..    
125ba 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c 64  sqlite3_free(old
125bb 50 61 74 68 29 3b 0d 0a 20 20 20 20 70 43 74 78  Path);..    pCtx
125bc 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
125bd 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
125be 70 28 30 2c 20 70 61 74 68 29 3b 0d 0a 20 20 7d  p(0, path);..  }
125bf 0d 0a 20 20 0d 0a 20 20 72 65 74 75 72 6e 20 72  ..  ..  return r
125c0 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
125c1 70 46 69 6c 65 20 69 73 20 61 20 66 69 6c 65 20  pFile is a file 
125c2 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 6f 70  that has been op
125c3 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ened by a prior 
125c4 78 4f 70 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50  xOpen call.  dbP
125c5 61 74 68 0d 0a 2a 2a 20 69 73 20 61 20 73 74 72  ath..** is a str
125c6 69 6e 67 20 62 75 66 66 65 72 20 61 74 20 6c 65  ing buffer at le
125c7 61 73 74 20 4d 41 58 50 41 54 48 4c 45 4e 2b 31  ast MAXPATHLEN+1
125c8 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 73   characters in s
125c9 69 7a 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ize...**..** Thi
125ca 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 64 20 74  s routine find t
125cb 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 73 73 6f  he filename asso
125cc 63 69 61 74 65 64 20 77 69 74 68 20 70 46 69 6c  ciated with pFil
125cd 65 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 0d  e and writes it.
125ce 0a 2a 2a 20 69 6e 74 20 64 62 50 61 74 68 2e 0d  .** int dbPath..
125cf 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
125d0 70 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f  proxyGetDbPathFo
125d1 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69  rUnixFile(unixFi
125d2 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 72 20  le *pFile, char 
125d3 2a 64 62 50 61 74 68 29 7b 0d 0a 23 69 66 20 64  *dbPath){..#if d
125d4 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
125d5 29 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  )..  if( pFile->
125d6 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49  pMethod == &afpI
125d7 6f 4d 65 74 68 6f 64 73 20 29 7b 0d 0a 20 20 20  oMethods ){..   
125d8 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65   /* afp style ke
125d9 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20  eps a reference 
125da 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69  to the db path i
125db 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66  n the filePath f
125dc 69 65 6c 64 20 0d 0a 20 20 20 20 2a 2a 20 6f 66  ield ..    ** of
125dd 20 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0d 0a   the struct */..
125de 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 74      assert( (int
125df 29 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70  )strlen((char*)p
125e0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
125e1 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45  text)<=MAXPATHLE
125e2 4e 20 29 3b 0d 0a 20 20 20 20 73 74 72 6c 63 70  N );..    strlcp
125e3 79 28 64 62 50 61 74 68 2c 20 28 28 61 66 70 4c  y(dbPath, ((afpL
125e4 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
125e5 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
125e6 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 2c 20  ntext)->dbPath, 
125e7 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0d 0a 20 20  MAXPATHLEN);..  
125e8 7d 20 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a  } else..#endif..
125e9 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
125ea 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b  thod == &dotlock
125eb 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0d 0a 20 20  IoMethods ){..  
125ec 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74    /* dot lock st
125ed 79 6c 65 20 75 73 65 73 20 74 68 65 20 6c 6f 63  yle uses the loc
125ee 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20  king context to 
125ef 73 74 6f 72 65 20 74 68 65 20 64 6f 74 20 6c 6f  store the dot lo
125f0 63 6b 0d 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20  ck..    ** file 
125f1 70 61 74 68 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  path */..    int
125f2 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63   len = strlen((c
125f3 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  har *)pFile->loc
125f4 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 2d 20 73  kingContext) - s
125f5 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55  trlen(DOTLOCK_SU
125f6 46 46 49 58 29 3b 0d 0a 20 20 20 20 6d 65 6d 63  FFIX);..    memc
125f7 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 72  py(dbPath, (char
125f8 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
125f9 67 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 2b 20  gContext, len + 
125fa 31 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  1);..  }else{.. 
125fb 20 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20     /* all other 
125fc 73 74 79 6c 65 73 20 75 73 65 20 74 68 65 20 6c  styles use the l
125fd 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  ocking context t
125fe 6f 20 73 74 6f 72 65 20 74 68 65 20 64 62 20 66  o store the db f
125ff 69 6c 65 20 70 61 74 68 20 2a 2f 0d 0a 20 20 20  ile path */..   
12600 20 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28   assert( strlen(
12601 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f  (char*)pFile->lo
12602 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d  ckingContext)<=M
12603 41 58 50 41 54 48 4c 45 4e 20 29 3b 0d 0a 20 20  AXPATHLEN );..  
12604 20 20 73 74 72 6c 63 70 79 28 64 62 50 61 74 68    strlcpy(dbPath
12605 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  , (char *)pFile-
12606 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c  >lockingContext,
12607 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0d 0a 20   MAXPATHLEN);.. 
12608 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
12609 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
1260a 0d 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c  ..** Takes an al
1260b 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e 20  ready filled in 
1260c 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 6c  unix file and al
1260d 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 66  ters it so all f
1260e 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0d 0a 2a 2a  ile locking ..**
1260f 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d   will be perform
12610 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ed on the local 
12611 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e  proxy lock file.
12612 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
12613 66 69 65 6c 64 73 0d 0a 2a 2a 20 61 72 65 20 70  fields..** are p
12614 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20  reserved in the 
12615 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
12616 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e  so that they can
12617 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64   be restored and
12618 20 0d 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73   ..** the unix s
12619 74 72 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c  tructure properl
1261a 79 20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20  y cleaned up at 
1261b 63 6c 6f 73 65 20 74 69 6d 65 3a 0d 0a 2a 2a 20  close time:..** 
1261c 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78   ->lockingContex
1261d 74 0d 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64  t..**  ->pMethod
1261e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1261f 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55   proxyTransformU
12620 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65  nixFile(unixFile
12621 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63   *pFile, const c
12622 68 61 72 20 2a 70 61 74 68 29 20 7b 0d 0a 20 20  har *path) {..  
12623 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
12624 65 78 74 20 2a 70 43 74 78 3b 0d 0a 20 20 63 68  ext *pCtx;..  ch
12625 61 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 54  ar dbPath[MAXPAT
12626 48 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f  HLEN+1];       /
12627 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
12628 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a  tabase file */..
12629 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68    char *lockPath
1262a 3d 4e 55 4c 4c 3b 0d 0a 20 20 69 6e 74 20 72 63  =NULL;..  int rc
1262b 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
1262c 20 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e   ..  if( pFile->
1262d 65 46 69 6c 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f  eFileLock!=NO_LO
1262e 43 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  CK ){..    retur
1262f 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a  n SQLITE_BUSY;..
12630 20 20 7d 0d 0a 20 20 70 72 6f 78 79 47 65 74 44    }..  proxyGetD
12631 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65  bPathForUnixFile
12632 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b  (pFile, dbPath);
12633 0d 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c  ..  if( !path ||
12634 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c   path[0]=='\0' |
12635 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20  | !strcmp(path, 
12636 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0d 0a 20 20  ":auto:") ){..  
12637 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b    lockPath=NULL;
12638 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
12639 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 72 20 2a  lockPath=(char *
1263a 29 70 61 74 68 3b 0d 0a 20 20 7d 0d 0a 20 20 0d  )path;..  }..  .
1263b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 52 41  .  OSTRACE(("TRA
1263c 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f 72 20  NSPROXY  %d for 
1263d 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %s pid=%d\n", pF
1263e 69 6c 65 2d 3e 68 2c 0d 0a 20 20 20 20 20 20 20  ile->h,..       
1263f 20 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20      (lockPath ? 
12640 6c 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 74  lockPath : ":aut
12641 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 29  o:"), getpid()))
12642 3b 0d 0a 0d 0a 20 20 70 43 74 78 20 3d 20 73 71  ;....  pCtx = sq
12643 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
12644 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0d 0a  zeof(*pCtx) );..
12645 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b    if( pCtx==0 ){
12646 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
12647 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d  ITE_NOMEM;..  }.
12648 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c 20  .  memset(pCtx, 
12649 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  0, sizeof(*pCtx)
1264a 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 70 72 6f  );....  rc = pro
1264b 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74  xyCreateConchPat
1264c 68 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70  hname(dbPath, &p
1264d 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61  Ctx->conchFilePa
1264e 74 68 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d  th);..  if( rc==
1264f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
12650 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61    rc = proxyCrea
12651 74 65 55 6e 69 78 46 69 6c 65 28 70 43 74 78 2d  teUnixFile(pCtx-
12652 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c 20  >conchFilePath, 
12653 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  &pCtx->conchFile
12654 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  , 0);..    if( r
12655 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  c==SQLITE_CANTOP
12656 45 4e 20 26 26 20 28 28 70 46 69 6c 65 2d 3e 6f  EN && ((pFile->o
12657 70 65 6e 46 6c 61 67 73 26 4f 5f 52 44 57 52 29  penFlags&O_RDWR)
12658 20 3d 3d 20 30 29 20 29 7b 0d 0a 20 20 20 20 20   == 0) ){..     
12659 20 2f 2a 20 69 66 20 28 61 29 20 74 68 65 20 6f   /* if (a) the o
1265a 70 65 6e 20 66 6c 61 67 73 20 61 72 65 20 6e 6f  pen flags are no
1265b 74 20 4f 5f 52 44 57 52 2c 20 28 62 29 20 74 68  t O_RDWR, (b) th
1265c 65 20 63 6f 6e 63 68 20 69 73 6e 27 74 20 74 68  e conch isn't th
1265d 65 72 65 2c 20 61 6e 64 0d 0a 20 20 20 20 20 20  ere, and..      
1265e 2a 2a 20 28 63 29 20 74 68 65 20 66 69 6c 65 20  ** (c) the file 
1265f 73 79 73 74 65 6d 20 69 73 20 72 65 61 64 2d 6f  system is read-o
12660 6e 6c 79 2c 20 74 68 65 6e 20 65 6e 61 62 6c 65  nly, then enable
12661 20 6e 6f 2d 6c 6f 63 6b 69 6e 67 20 61 63 63 65   no-locking acce
12662 73 73 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 55 67  ss...      ** Ug
12663 68 2c 20 73 69 6e 63 65 20 4f 5f 52 44 4f 4e 4c  h, since O_RDONL
12664 59 3d 3d 30 78 30 30 30 30 20 77 65 20 74 65 73  Y==0x0000 we tes
12665 74 20 66 6f 72 20 21 4f 5f 52 44 57 52 20 73 69  t for !O_RDWR si
12666 6e 63 65 20 75 6e 69 78 4f 70 65 6e 20 61 73 73  nce unixOpen ass
12667 65 72 74 73 0d 0a 20 20 20 20 20 20 2a 2a 20 74  erts..      ** t
12668 68 61 74 20 6f 70 65 6e 46 6c 61 67 73 20 77 69  hat openFlags wi
12669 6c 6c 20 68 61 76 65 20 6f 6e 6c 79 20 6f 6e 65  ll have only one
1266a 20 6f 66 20 4f 5f 52 44 4f 4e 4c 59 20 6f 72 20   of O_RDONLY or 
1266b 4f 5f 52 44 57 52 2e 0d 0a 20 20 20 20 20 20 2a  O_RDWR...      *
1266c 2f 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  /..      struct 
1266d 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0d 0a  statfs fsInfo;..
1266e 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61        struct sta
1266f 74 20 63 6f 6e 63 68 49 6e 66 6f 3b 0d 0a 20 20  t conchInfo;..  
12670 20 20 20 20 69 6e 74 20 67 6f 4c 6f 63 6b 6c 65      int goLockle
12671 73 73 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 20  ss = 0;....     
12672 20 69 66 28 20 6f 73 53 74 61 74 28 70 43 74 78   if( osStat(pCtx
12673 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c  ->conchFilePath,
12674 20 26 63 6f 6e 63 68 49 6e 66 6f 29 20 3d 3d 20   &conchInfo) == 
12675 2d 31 20 29 20 7b 0d 0a 20 20 20 20 20 20 20 20  -1 ) {..        
12676 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b  int err = errno;
12677 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 65  ..        if( (e
12678 72 72 3d 3d 45 4e 4f 45 4e 54 29 20 26 26 20 28  rr==ENOENT) && (
12679 73 74 61 74 66 73 28 64 62 50 61 74 68 2c 20 26  statfs(dbPath, &
1267a 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 29 20 29  fsInfo) != -1) )
1267b 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 4c  {..          goL
1267c 6f 63 6b 6c 65 73 73 20 3d 20 28 66 73 49 6e 66  ockless = (fsInf
1267d 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f 52 44  o.f_flags&MNT_RD
1267e 4f 4e 4c 59 29 20 3d 3d 20 4d 4e 54 5f 52 44 4f  ONLY) == MNT_RDO
1267f 4e 4c 59 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  NLY;..        }.
12680 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
12681 69 66 28 20 67 6f 4c 6f 63 6b 6c 65 73 73 20 29  if( goLockless )
12682 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d  {..        pCtx-
12683 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 2d 31 3b  >conchHeld = -1;
12684 20 2f 2a 20 72 65 61 64 20 6f 6e 6c 79 20 46 53   /* read only FS
12685 2f 20 6c 6f 63 6b 6c 65 73 73 20 2a 2f 0d 0a 20  / lockless */.. 
12686 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
12687 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 7d 0d  TE_OK;..      }.
12688 0a 20 20 20 20 7d 0d 0a 20 20 7d 20 20 0d 0a 20  .    }..  }  .. 
12689 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1268a 4f 4b 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29  OK && lockPath )
1268b 7b 0d 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63  {..    pCtx->loc
1268c 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c  kProxyPath = sql
1268d 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20  ite3DbStrDup(0, 
1268e 6c 6f 63 6b 50 61 74 68 29 3b 0d 0a 20 20 7d 0d  lockPath);..  }.
1268f 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ...  if( rc==SQL
12690 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70  ITE_OK ){..    p
12691 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 73 71  Ctx->dbPath = sq
12692 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
12693 20 64 62 50 61 74 68 29 3b 0d 0a 20 20 20 20 69   dbPath);..    i
12694 66 28 20 70 43 74 78 2d 3e 64 62 50 61 74 68 3d  f( pCtx->dbPath=
12695 3d 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 20 20  =NULL ){..      
12696 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
12697 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  M;..    }..  }..
12698 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12699 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 61  _OK ){..    /* a
1269a 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c  ll memory is all
1269b 6f 63 61 74 65 64 2c 20 70 72 6f 78 79 73 20 61  ocated, proxys a
1269c 72 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 61  re created and a
1269d 73 73 69 67 6e 65 64 2c 20 0d 0a 20 20 20 20 2a  ssigned, ..    *
1269e 2a 20 73 77 69 74 63 68 20 74 68 65 20 6c 6f 63  * switch the loc
1269f 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 6e 64  king context and
126a0 20 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 72 65   pMethod then re
126a1 74 75 72 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  turn...    */.. 
126a2 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b     pCtx->oldLock
126a3 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 69  ingContext = pFi
126a4 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
126a5 78 74 3b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e  xt;..    pFile->
126a6 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
126a7 20 70 43 74 78 3b 0d 0a 20 20 20 20 70 43 74 78   pCtx;..    pCtx
126a8 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70  ->pOldMethod = p
126a9 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0d 0a  File->pMethod;..
126aa 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68      pFile->pMeth
126ab 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74  od = &proxyIoMet
126ac 68 6f 64 73 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  hods;..  }else{.
126ad 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63  .    if( pCtx->c
126ae 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0d 0a 20 20  onchFile ){ ..  
126af 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46      pCtx->conchF
126b0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
126b1 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69  lose((sqlite3_fi
126b2 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68  le *)pCtx->conch
126b3 46 69 6c 65 29 3b 0d 0a 20 20 20 20 20 20 73 71  File);..      sq
126b4 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d  lite3_free(pCtx-
126b5 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0d 0a 20 20  >conchFile);..  
126b6 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
126b7 44 62 46 72 65 65 28 30 2c 20 70 43 74 78 2d 3e  DbFree(0, pCtx->
126b8 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0d  lockProxyPath);.
126b9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
126ba 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  e(pCtx->conchFil
126bb 65 50 61 74 68 29 3b 20 0d 0a 20 20 20 20 73 71  ePath); ..    sq
126bc 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 29  lite3_free(pCtx)
126bd 3b 0d 0a 20 20 7d 0d 0a 20 20 4f 53 54 52 41 43  ;..  }..  OSTRAC
126be 45 28 28 22 54 52 41 4e 53 50 52 4f 58 59 20 20  E(("TRANSPROXY  
126bf 25 64 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d  %d %s\n", pFile-
126c0 3e 68 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  >h,..           
126c1 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f  (rc==SQLITE_OK ?
126c2 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
126c3 29 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  )));..  return r
126c4 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  c;..}....../*..*
126c5 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
126c6 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66  andles sqlite3_f
126c7 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61  ile_control() ca
126c8 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65  lls that are spe
126c9 63 69 66 69 63 0d 0a 2a 2a 20 74 6f 20 70 72 6f  cific..** to pro
126ca 78 79 20 6c 6f 63 6b 69 6e 67 2e 0d 0a 2a 2f 0d  xy locking...*/.
126cb 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
126cc 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  yFileControl(sql
126cd 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
126ce 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
126cf 67 29 7b 0d 0a 20 20 73 77 69 74 63 68 28 20 6f  g){..  switch( o
126d0 70 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 53  p ){..    case S
126d1 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52  QLITE_GET_LOCKPR
126d2 4f 58 59 46 49 4c 45 3a 20 7b 0d 0a 20 20 20 20  OXYFILE: {..    
126d3 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
126d4 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
126d5 64 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 46  d;..      if( pF
126d6 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
126d7 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20  &proxyIoMethods 
126d8 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 72 6f 78  ){..        prox
126d9 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
126da 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
126db 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46  ckingContext*)pF
126dc 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
126dd 65 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 70 72  ext;..        pr
126de 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
126df 6c 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  le);..        if
126e0 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  ( pCtx->lockProx
126e1 79 50 61 74 68 20 29 7b 0d 0a 20 20 20 20 20 20  yPath ){..      
126e2 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72      *(const char
126e3 20 2a 2a 29 70 41 72 67 20 3d 20 70 43 74 78 2d   **)pArg = pCtx-
126e4 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0d  >lockProxyPath;.
126e5 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
126e6 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e  .          *(con
126e7 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
126e8 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 20 68  = ":auto: (not h
126e9 65 6c 64 29 22 3b 0d 0a 20 20 20 20 20 20 20 20  eld)";..        
126ea 7d 0d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  }..      } else 
126eb 7b 0d 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e  {..        *(con
126ec 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
126ed 3d 20 4e 55 4c 4c 3b 0d 0a 20 20 20 20 20 20 7d  = NULL;..      }
126ee 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
126ef 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
126f0 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
126f1 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
126f2 49 4c 45 3a 20 7b 0d 0a 20 20 20 20 20 20 75 6e  ILE: {..      un
126f3 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
126f4 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a  (unixFile*)id;..
126f5 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53        int rc = S
126f6 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
126f7 20 69 6e 74 20 69 73 50 72 6f 78 79 53 74 79 6c   int isProxyStyl
126f8 65 20 3d 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74  e = (pFile->pMet
126f9 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d  hod == &proxyIoM
126fa 65 74 68 6f 64 73 29 3b 0d 0a 20 20 20 20 20 20  ethods);..      
126fb 69 66 28 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c  if( pArg==NULL |
126fc 7c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  | (const char *)
126fd 70 41 72 67 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  pArg==0 ){..    
126fe 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53      if( isProxyS
126ff 74 79 6c 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  tyle ){..       
12700 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70     /* turn off p
12701 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 6e  roxy locking - n
12702 6f 74 20 73 75 70 70 6f 72 74 65 64 20 2a 2f 0d  ot supported */.
12703 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12704 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 2f 2a 53  SQLITE_ERROR /*S
12705 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 3f 20  QLITE_PROTOCOL? 
12706 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3f 2a 2f  SQLITE_MISUSE?*/
12707 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
12708 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {..          /* 
12709 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c  turn off proxy l
1270a 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 64 79  ocking - already
1270b 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f 0d 0a   off - NOOP */..
1270c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1270d 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
1270e 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73     }..      }els
1270f 65 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  e{..        cons
12710 74 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74  t char *proxyPat
12711 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  h = (const char 
12712 2a 29 70 41 72 67 3b 0d 0a 20 20 20 20 20 20 20  *)pArg;..       
12713 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c   if( isProxyStyl
12714 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  e ){..          
12715 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
12716 65 78 74 20 2a 70 43 74 78 20 3d 20 0d 0a 20 20  ext *pCtx = ..  
12717 20 20 20 20 20 20 20 20 20 20 28 70 72 6f 78 79            (proxy
12718 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29  LockingContext*)
12719 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
1271a 6e 74 65 78 74 3b 0d 0a 20 20 20 20 20 20 20 20  ntext;..        
1271b 20 20 69 66 28 20 21 73 74 72 63 6d 70 28 70 41    if( !strcmp(pA
1271c 72 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 0d 0a  rg, ":auto:") ..
1271d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70             || (p
1271e 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
1271f 74 68 20 26 26 0d 0a 20 20 20 20 20 20 20 20 20  th &&..         
12720 20 20 20 20 20 20 21 73 74 72 6e 63 6d 70 28 70        !strncmp(p
12721 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
12722 74 68 2c 20 70 72 6f 78 79 50 61 74 68 2c 20 4d  th, proxyPath, M
12723 41 58 50 41 54 48 4c 45 4e 29 29 0d 0a 20 20 20  AXPATHLEN))..   
12724 20 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20         ){..     
12725 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
12726 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20  TE_OK;..        
12727 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
12728 20 20 20 20 20 20 72 63 20 3d 20 73 77 69 74 63        rc = switc
12729 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 70  hLockProxyPath(p
1272a 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 29  File, proxyPath)
1272b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
1272c 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
1272d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72            /* tur
1272e 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 20  n on proxy file 
1272f 6c 6f 63 6b 69 6e 67 20 2a 2f 0d 0a 20 20 20 20  locking */..    
12730 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
12731 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
12732 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61  e(pFile, proxyPa
12733 74 68 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  th);..        }.
12734 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
12735 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20  return rc;..    
12736 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  }..    default: 
12737 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
12738 20 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 63 61   0 );  /* The ca
12739 6c 6c 20 61 73 73 75 72 65 73 20 74 68 61 74 20  ll assures that 
1273a 6f 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 6f 64  only valid opcod
1273b 65 73 20 61 72 65 20 73 65 6e 74 20 2a 2f 0d 0a  es are sent */..
1273c 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 2f 2a      }..  }..  /*
1273d 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0d 0a 20 20  NOTREACHED*/..  
1273e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1273f 52 4f 52 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ROR;..}..../*..*
12740 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 64 69  * Within this di
12741 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72 6f 78  vision (the prox
12742 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 6d 70  ying locking imp
12743 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 68 65  lementation) the
12744 20 70 72 6f 63 65 64 75 72 65 73 0d 0a 2a 2a 20   procedures..** 
12745 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74  above this point
12746 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 69   are all utiliti
12747 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 65  es.  The lock-re
12748 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f 66  lated methods of
12749 20 74 68 65 0d 0a 2a 2a 20 70 72 6f 78 79 2d 6c   the..** proxy-l
1274a 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  ocking sqlite3_i
1274b 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20  o_method object 
1274c 66 6f 6c 6c 6f 77 2e 0d 0a 2a 2f 0d 0a 0d 0a 0d  follow...*/.....
1274d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./*..** This rou
1274e 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
1274f 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
12750 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
12751 74 68 65 20 73 70 65 63 69 66 69 65 64 0d 0a 2a  the specified..*
12752 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
12753 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
12754 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
12755 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
12756 20 2a 70 52 65 73 4f 75 74 0d 0a 2a 2a 20 74 6f   *pResOut..** to
12757 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   a non-zero valu
12758 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65  e otherwise *pRe
12759 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a  sOut is set to z
1275a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ero.  The return
1275b 20 76 61 6c 75 65 0d 0a 2a 2a 20 69 73 20 73 65   value..** is se
1275c 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
1275d 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
1275e 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
1275f 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0d   lock checking..
12760 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
12761 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76  proxyCheckReserv
12762 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
12763 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
12764 65 73 4f 75 74 29 20 7b 0d 0a 20 20 75 6e 69 78  esOut) {..  unix
12765 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
12766 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20  nixFile*)id;..  
12767 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61  int rc = proxyTa
12768 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0d  keConch(pFile);.
12769 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1276a 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 72 6f  E_OK ){..    pro
1276b 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
1276c 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
1276d 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
1276e 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
1276f 6e 74 65 78 74 3b 0d 0a 20 20 20 20 69 66 28 20  ntext;..    if( 
12770 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e  pCtx->conchHeld>
12771 30 20 29 7b 0d 0a 20 20 20 20 20 20 75 6e 69 78  0 ){..      unix
12772 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43  File *proxy = pC
12773 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0d 0a  tx->lockProxy;..
12774 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f        return pro
12775 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68  xy->pMethod->xCh
12776 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
12777 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70  (sqlite3_file*)p
12778 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0d  roxy, pResOut);.
12779 0a 20 20 20 20 7d 65 6c 73 65 7b 20 2f 2a 20 63  .    }else{ /* c
1277a 6f 6e 63 68 48 65 6c 64 20 3c 20 30 20 69 73 20  onchHeld < 0 is 
1277b 6c 6f 63 6b 6c 65 73 73 20 2a 2f 0d 0a 20 20 20  lockless */..   
1277c 20 20 20 70 52 65 73 4f 75 74 3d 30 3b 0d 0a 20     pResOut=0;.. 
1277d 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
1277e 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1277f 0d 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  ..** Lock the fi
12780 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
12781 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
12782 72 61 6d 65 74 65 72 20 65 46 69 6c 65 4c 6f 63  rameter eFileLoc
12783 6b 20 2d 20 6f 6e 65 0d 0a 2a 2a 20 6f 66 20 74  k - one..** of t
12784 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a  he following:..*
12785 2a 0d 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  *..**     (1) SH
12786 41 52 45 44 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20  ARED_LOCK..**   
12787 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
12788 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20 28 33 29 20  OCK..**     (3) 
12789 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0d 0a 2a 2a  PENDING_LOCK..**
1278a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
1278b 56 45 5f 4c 4f 43 4b 0d 0a 2a 2a 0d 0a 2a 2a 20  VE_LOCK..**..** 
1278c 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
1278d 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
1278e 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
1278f 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
12790 0d 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  ..** are inserte
12791 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
12792 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
12793 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
12794 74 68 65 20 6c 61 74 65 72 0d 0a 2a 2a 20 74 72  the later..** tr
12795 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
12796 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
12797 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
12798 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
12799 62 75 74 0d 0a 2a 2a 20 73 74 69 6c 6c 20 73 68  but..** still sh
1279a 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
1279b 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
1279c 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
1279d 61 6c 6c 6f 77 65 64 0d 0a 2a 2a 20 74 72 61 6e  allowed..** tran
1279e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
1279f 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
127a0 64 69 61 74 65 20 73 74 61 74 65 73 3a 0d 0a 2a  diate states:..*
127a1 2a 0d 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45  *..**    UNLOCKE
127a2 44 20 2d 3e 20 53 48 41 52 45 44 0d 0a 2a 2a 20  D -> SHARED..** 
127a3 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
127a4 45 52 56 45 44 0d 0a 2a 2a 20 20 20 20 53 48 41  ERVED..**    SHA
127a5 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
127a6 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a   -> EXCLUSIVE..*
127a7 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e  *    RESERVED ->
127a8 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
127a9 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 20 20 20 50  CLUSIVE..**    P
127aa 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
127ab 49 56 45 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  IVE..**..** This
127ac 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
127ad 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
127ae 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
127af 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0d 0a  ite3OsUnlock()..
127b0 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
127b1 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
127b2 76 65 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  vel...*/..static
127b3 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73   int proxyLock(s
127b4 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
127b5 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20   int eFileLock) 
127b6 7b 0d 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  {..  unixFile *p
127b7 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
127b8 2a 29 69 64 3b 0d 0a 20 20 69 6e 74 20 72 63 20  *)id;..  int rc 
127b9 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  = proxyTakeConch
127ba 28 70 46 69 6c 65 29 3b 0d 0a 20 20 69 66 28 20  (pFile);..  if( 
127bb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
127bc 0d 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69  ..    proxyLocki
127bd 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
127be 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  = (proxyLockingC
127bf 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e  ontext *)pFile->
127c0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0d  lockingContext;.
127c1 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63  .    if( pCtx->c
127c2 6f 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0d 0a 20  onchHeld>0 ){.. 
127c3 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70       unixFile *p
127c4 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  roxy = pCtx->loc
127c5 6b 50 72 6f 78 79 3b 0d 0a 20 20 20 20 20 20 72  kProxy;..      r
127c6 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68  c = proxy->pMeth
127c7 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74  od->xLock((sqlit
127c8 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20  e3_file*)proxy, 
127c9 65 46 69 6c 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20  eFileLock);..   
127ca 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c     pFile->eFileL
127cb 6f 63 6b 20 3d 20 70 72 6f 78 79 2d 3e 65 46 69  ock = proxy->eFi
127cc 6c 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20 7d 65 6c  leLock;..    }el
127cd 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 63 6f  se{..      /* co
127ce 6e 63 68 48 65 6c 64 20 3c 20 30 20 69 73 20 6c  nchHeld < 0 is l
127cf 6f 63 6b 6c 65 73 73 20 2a 2f 0d 0a 20 20 20 20  ockless */..    
127d0 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
127d1 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d   rc;..}....../*.
127d2 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
127d3 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
127d4 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
127d5 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63  File to eFileLoc
127d6 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0d 0a 2a  k.  eFileLock..*
127d7 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
127d8 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
127d9 45 44 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a  ED_LOCK...**..**
127da 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
127db 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
127dc 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
127dd 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
127de 6c 6f 77 0d 0a 2a 2a 20 74 68 65 20 72 65 71 75  low..** the requ
127df 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
127e0 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
127e1 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  e is a no-op...*
127e2 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  /..static int pr
127e3 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  oxyUnlock(sqlite
127e4 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
127e5 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0d 0a 20 20  eFileLock) {..  
127e6 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
127e7 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
127e8 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ..  int rc = pro
127e9 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
127ea 65 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  e);..  if( rc==S
127eb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
127ec 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
127ed 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
127ee 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
127ef 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
127f0 6e 67 43 6f 6e 74 65 78 74 3b 0d 0a 20 20 20 20  ngContext;..    
127f1 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
127f2 65 6c 64 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  eld>0 ){..      
127f3 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20  unixFile *proxy 
127f4 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
127f5 79 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 70  y;..      rc = p
127f6 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
127f7 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  Unlock((sqlite3_
127f8 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 65 46 69  file*)proxy, eFi
127f9 6c 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20 20 20 20  leLock);..      
127fa 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
127fb 20 3d 20 70 72 6f 78 79 2d 3e 65 46 69 6c 65 4c   = proxy->eFileL
127fc 6f 63 6b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  ock;..    }else{
127fd 0d 0a 20 20 20 20 20 20 2f 2a 20 63 6f 6e 63 68  ..      /* conch
127fe 48 65 6c 64 20 3c 20 30 20 69 73 20 6c 6f 63 6b  Held < 0 is lock
127ff 6c 65 73 73 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a  less */..    }..
12800 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
12801 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
12802 6c 6f 73 65 20 61 20 66 69 6c 65 20 74 68 61 74  lose a file that
12803 20 75 73 65 73 20 70 72 6f 78 79 20 6c 6f 63 6b   uses proxy lock
12804 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
12805 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 73 71  nt proxyClose(sq
12806 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
12807 7b 0d 0a 20 20 69 66 28 20 69 64 20 29 7b 0d 0a  {..  if( id ){..
12808 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
12809 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
1280a 29 69 64 3b 0d 0a 20 20 20 20 70 72 6f 78 79 4c  )id;..    proxyL
1280b 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
1280c 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
1280d 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
1280e 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
1280f 78 74 3b 0d 0a 20 20 20 20 75 6e 69 78 46 69 6c  xt;..    unixFil
12810 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70  e *lockProxy = p
12811 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0d  Ctx->lockProxy;.
12812 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63  .    unixFile *c
12813 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d  onchFile = pCtx-
12814 3e 63 6f 6e 63 68 46 69 6c 65 3b 0d 0a 20 20 20  >conchFile;..   
12815 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
12816 5f 4f 4b 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20  _OK;..    ..    
12817 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b  if( lockProxy ){
12818 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63  ..      rc = loc
12819 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d  kProxy->pMethod-
1281a 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65  >xUnlock((sqlite
1281b 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78  3_file*)lockProx
1281c 79 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  y, NO_LOCK);..  
1281d 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1281e 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 72  urn rc;..      r
1281f 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70  c = lockProxy->p
12820 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28  Method->xClose((
12821 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f  sqlite3_file*)lo
12822 63 6b 50 72 6f 78 79 29 3b 0d 0a 20 20 20 20 20  ckProxy);..     
12823 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
12824 20 72 63 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69   rc;..      sqli
12825 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f  te3_free(lockPro
12826 78 79 29 3b 0d 0a 20 20 20 20 20 20 70 43 74 78  xy);..      pCtx
12827 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 30 3b  ->lockProxy = 0;
12828 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
12829 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0d 0a 20   conchFile ){.. 
1282a 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63       if( pCtx->c
1282b 6f 6e 63 68 48 65 6c 64 20 29 7b 0d 0a 20 20 20  onchHeld ){..   
1282c 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52       rc = proxyR
1282d 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c  eleaseConch(pFil
1282e 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  e);..        if(
1282f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
12830 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
12831 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d   rc = conchFile-
12832 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
12833 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
12834 63 6f 6e 63 68 46 69 6c 65 29 3b 0d 0a 20 20 20  conchFile);..   
12835 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
12836 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 73 71  rn rc;..      sq
12837 6c 69 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 68  lite3_free(conch
12838 46 69 6c 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  File);..    }.. 
12839 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1283a 28 30 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  (0, pCtx->lockPr
1283b 6f 78 79 50 61 74 68 29 3b 0d 0a 20 20 20 20 73  oxyPath);..    s
1283c 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
1283d 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29  ->conchFilePath)
1283e 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
1283f 46 72 65 65 28 30 2c 20 70 43 74 78 2d 3e 64 62  Free(0, pCtx->db
12840 50 61 74 68 29 3b 0d 0a 20 20 20 20 2f 2a 20 72  Path);..    /* r
12841 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69  estore the origi
12842 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  nal locking cont
12843 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20  ext and pMethod 
12844 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f  then close it */
12845 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ..    pFile->loc
12846 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43  kingContext = pC
12847 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f  tx->oldLockingCo
12848 6e 74 65 78 74 3b 0d 0a 20 20 20 20 70 46 69 6c  ntext;..    pFil
12849 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 74  e->pMethod = pCt
1284a 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0d 0a  x->pOldMethod;..
1284b 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1284c 28 70 43 74 78 29 3b 0d 0a 20 20 20 20 72 65 74  (pCtx);..    ret
1284d 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  urn pFile->pMeth
1284e 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0d  od->xClose(id);.
1284f 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
12850 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
12851 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  ....#endif /* de
12852 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
12853 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
12854 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
12855 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 70  */../*..** The p
12856 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74 79  roxy locking sty
12857 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 66  le is intended f
12858 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 50 20  or use with AFP 
12859 66 69 6c 65 73 79 73 74 65 6d 73 2e 0d 0a 2a 2a  filesystems...**
1285a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 69   And since AFP i
1285b 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
1285c 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 20   on MacOSX, the 
1285d 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  proxy locking is
1285e 20 61 6c 73 6f 0d 0a 2a 2a 20 72 65 73 74 72 69   also..** restri
1285f 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0d  cted to MacOSX..
12860 0a 2a 2a 20 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a  .** ..**..******
12861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
12862 64 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c  d of the proxy l
12863 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ock implementati
12864 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on *************
12865 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a  *********..*****
12866 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12867 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12869 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1286a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a  *********/..../*
1286b 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ..** Initialize 
1286c 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
1286d 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0d  stem interface..
1286e 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
1286f 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61  tine registers a
12870 6c 6c 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  ll VFS implement
12871 61 74 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d  ations for unix-
12872 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 0d 0a  like operating..
12873 2a 2a 20 73 79 73 74 65 6d 73 2e 20 20 54 68 69  ** systems.  Thi
12874 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74  s routine, and t
12875 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  he sqlite3_os_en
12876 64 28 29 20 72 6f 75 74 69 6e 65 20 74 68 61 74  d() routine that
12877 20 66 6f 6c 6c 6f 77 73 2c 0d 0a 2a 2a 20 73 68   follows,..** sh
12878 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
12879 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
1287a 73 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20  s file that are 
1287b 76 69 73 69 62 6c 65 20 66 72 6f 6d 20 6f 74 68  visible from oth
1287c 65 72 0d 0a 2a 2a 20 66 69 6c 65 73 2e 0d 0a 2a  er..** files...*
1287d 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
1287e 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63  ne is called onc
1287f 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 20  e during SQLite 
12880 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
12881 6e 64 20 62 79 20 61 0d 0a 2a 2a 20 73 69 6e 67  nd by a..** sing
12882 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
12883 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
12884 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73  n and mutex subs
12885 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 0d  ystems have not.
12886 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20  .** necessarily 
12887 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
12888 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
12889 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e  ne is called, an
1288a 64 20 73 6f 20 74 68 65 79 0d 0a 2a 2a 20 73 68  d so they..** sh
1288b 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
1288c 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
1288d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
1288e 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0d 0a 20  _init(void){ .. 
1288f 20 2f 2a 20 0d 0a 20 20 2a 2a 20 54 68 65 20 66   /* ..  ** The f
12890 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64  ollowing macro d
12891 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 69 61  efines an initia
12892 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 71 6c  lizer for an sql
12893 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e  ite3_vfs object.
12894 0d 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 20  ..  ** The name 
12895 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 4e 41  of the VFS is NA
12896 4d 45 2e 20 20 54 68 65 20 70 41 70 70 44 61 74  ME.  The pAppDat
12897 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  a is a pointer t
12898 6f 20 61 20 70 6f 69 6e 74 65 72 0d 0a 20 20 2a  o a pointer..  *
12899 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72  * to the "finder
1289a 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41  " function.  (pA
1289b 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e  ppData is a poin
1289c 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72  ter to a pointer
1289d 20 62 65 63 61 75 73 65 0d 0a 20 20 2a 2a 20 73   because..  ** s
1289e 69 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 70  illy C90 rules p
1289f 72 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a 20  rohibit a void* 
128a0 66 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 20  from being cast 
128a1 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f  to a function po
128a2 69 6e 74 65 72 0d 0a 20 20 2a 2a 20 61 6e 64 20  inter..  ** and 
128a3 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f  so we have to go
128a4 20 74 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74   through the int
128a5 65 72 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65  ermediate pointe
128a6 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c  r to avoid probl
128a7 65 6d 73 0d 0a 20 20 2a 2a 20 77 68 65 6e 20 63  ems..  ** when c
128a8 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 2d 70  ompiling with -p
128a9 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f  edantic-errors o
128aa 6e 20 47 43 43 2e 29 0d 0a 20 20 2a 2a 0d 0a 20  n GCC.)..  **.. 
128ab 20 2a 2a 20 54 68 65 20 46 49 4e 44 45 52 20 70   ** The FINDER p
128ac 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
128ad 20 6d 61 63 72 6f 20 69 73 20 74 68 65 20 6e 61   macro is the na
128ae 6d 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  me of the pointe
128af 72 20 74 6f 20 74 68 65 0d 0a 20 20 2a 2a 20 66  r to the..  ** f
128b0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 2e 20  inder-function. 
128b1 20 54 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63   The finder-func
128b2 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70  tion returns a p
128b3 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0d 0a 20  ointer to the.. 
128b4 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d 65   ** sqlite_io_me
128b5 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 68 61  thods object tha
128b6 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
128b7 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e 67   desired locking
128b8 0d 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 73  ..  ** behaviors
128b9 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 73  .  See the divis
128ba 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 63  ion above that c
128bb 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d 45  ontains the IOME
128bc 54 48 4f 44 53 0d 0a 20 20 2a 2a 20 6d 61 63 72  THODS..  ** macr
128bd 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69  o for addition i
128be 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69  nformation on fi
128bf 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0d  nder-functions..
128c0 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4d 6f 73 74  .  **..  ** Most
128c1 20 66 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20   finders simply 
128c2 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
128c3 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69   to a fixed sqli
128c4 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0d 0a  te3_io_methods..
128c5 20 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75    ** object.  Bu
128c6 74 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49  t the "autolockI
128c7 6f 46 69 6e 64 65 72 22 20 61 76 61 69 6c 61 62  oFinder" availab
128c8 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65  le on MacOSX doe
128c9 73 20 61 20 6c 69 74 74 6c 65 0d 0a 20 20 2a 2a  s a little..  **
128ca 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 61 74 3b   more than that;
128cb 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65   it looks at the
128cc 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65   filesystem type
128cd 20 74 68 61 74 20 68 6f 73 74 73 20 74 68 65 20   that hosts the 
128ce 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  ..  ** database 
128cf 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73 20 74  file and tries t
128d0 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b  o choose an lock
128d1 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 72 6f  ing method appro
128d2 70 72 69 61 74 65 20 66 6f 72 0d 0a 20 20 2a 2a  priate for..  **
128d3 20 74 68 61 74 20 66 69 6c 65 73 79 73 74 65 6d   that filesystem
128d4 20 74 69 6d 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   time...  */..  
128d5 23 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28  #define UNIXVFS(
128d6 56 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29  VFSNAME, FINDER)
128d7 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
128d8 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20            \..   
128d9 20 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   3,             
128da 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
128db 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  on */           
128dc 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20           \..    
128dd 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
128de 2c 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c  ,     /* szOsFil
128df 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
128e0 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 4d          \..    M
128e1 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20 20  AX_PATHNAME,    
128e2 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61       /* mxPathna
128e3 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  me */           
128e4 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 30 2c         \..    0,
128e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128e6 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20      /* pNext */ 
128e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128e8 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 56 46 53        \..    VFS
128e9 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 20 20  NAME,           
128ea 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20     /* zName */  
128eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128ec 20 20 20 20 20 5c 0d 0a 20 20 20 20 28 76 6f 69       \..    (voi
128ed 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 20 20  d*)&FINDER,     
128ee 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f    /* pAppData */
128ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128f0 20 20 20 20 5c 0d 0a 20 20 20 20 75 6e 69 78 4f      \..    unixO
128f1 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  pen,            
128f2 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20   /* xOpen */    
128f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128f4 20 20 20 5c 0d 0a 20 20 20 20 75 6e 69 78 44 65     \..    unixDe
128f5 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  lete,           
128f6 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20  /* xDelete */   
128f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128f8 20 20 5c 0d 0a 20 20 20 20 75 6e 69 78 41 63 63    \..    unixAcc
128f9 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ess,           /
128fa 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20  * xAccess */    
128fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128fc 20 5c 0d 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c   \..    unixFull
128fd 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a  Pathname,     /*
128fe 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a   xFullPathname *
128ff 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
12900 5c 0d 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 65  \..    unixDlOpe
12901 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n,           /* 
12902 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20  xDlOpen */      
12903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
12904 0d 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f  ..    unixDlErro
12905 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  r,          /* x
12906 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20  DlError */      
12907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d                \.
12908 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20  .    unixDlSym, 
12909 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
1290a 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 20  lSym */         
1290b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
1290c 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c      unixDlClose,
1290d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
1290e 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20  Close */        
1290f 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20              \.. 
12910 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73     unixRandomnes
12911 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e  s,       /* xRan
12912 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20  domness */      
12913 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
12914 20 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20    unixSleep,    
12915 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65          /* xSlee
12916 70 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  p */            
12917 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20            \..   
12918 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65   unixCurrentTime
12919 2c 20 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65  ,      /* xCurre
1291a 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20  ntTime */       
1291b 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20           \..    
1291c 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72  unixGetLastError
1291d 2c 20 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73  ,     /* xGetLas
1291e 74 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20  tError */       
1291f 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 75          \..    u
12920 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e  nixCurrentTimeIn
12921 74 36 34 2c 20 2f 2a 20 78 43 75 72 72 65 6e 74  t64, /* xCurrent
12922 54 69 6d 65 49 6e 74 36 34 20 2a 2f 20 20 20 20  TimeInt64 */    
12923 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 75 6e         \..    un
12924 69 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c 2c  ixSetSystemCall,
12925 20 20 20 20 2f 2a 20 78 53 65 74 53 79 73 74 65      /* xSetSyste
12926 6d 43 61 6c 6c 20 2a 2f 20 20 20 20 20 20 20 20  mCall */        
12927 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 75 6e 69        \..    uni
12928 78 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 2c 20  xGetSystemCall, 
12929 20 20 20 2f 2a 20 78 47 65 74 53 79 73 74 65 6d     /* xGetSystem
1292a 43 61 6c 6c 20 2a 2f 20 20 20 20 20 20 20 20 20  Call */         
1292b 20 20 20 20 20 5c 0d 0a 20 20 20 20 75 6e 69 78       \..    unix
1292c 4e 65 78 74 53 79 73 74 65 6d 43 61 6c 6c 2c 20  NextSystemCall, 
1292d 20 20 2f 2a 20 78 4e 65 78 74 53 79 73 74 65 6d    /* xNextSystem
1292e 43 61 6c 6c 20 2a 2f 20 20 20 20 20 20 20 20 20  Call */         
1292f 20 20 20 20 5c 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      \..  }....  
12930 2f 2a 0d 0a 20 20 2a 2a 20 41 6c 6c 20 64 65 66  /*..  ** All def
12931 61 75 6c 74 20 56 46 53 65 73 20 66 6f 72 20 75  ault VFSes for u
12932 6e 69 78 20 61 72 65 20 63 6f 6e 74 61 69 6e 65  nix are containe
12933 64 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d in the followi
12934 6e 67 20 61 72 72 61 79 2e 0d 0a 20 20 2a 2a 0d  ng array...  **.
12935 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
12936 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e  the sqlite3_vfs.
12937 70 4e 65 78 74 20 66 69 65 6c 64 20 6f 66 20 74  pNext field of t
12938 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 69 73  he VFS object is
12939 20 6d 6f 64 69 66 69 65 64 0d 0a 20 20 2a 2a 20   modified..  ** 
1293a 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  by the SQLite co
1293b 72 65 20 77 68 65 6e 20 74 68 65 20 56 46 53 20  re when the VFS 
1293c 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 20  is registered.  
1293d 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  So the following
1293e 0d 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61 6e  ..  ** array can
1293f 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0d 0a 20  not be const... 
12940 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 73 71   */..  static sq
12941 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d  lite3_vfs aVfs[]
12942 20 3d 20 7b 0d 0a 23 69 66 20 53 51 4c 49 54 45   = {..#if SQLITE
12943 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
12944 53 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57  STYLE && (OS_VXW
12945 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORKS || defined(
12946 5f 5f 41 50 50 4c 45 5f 5f 29 29 0d 0a 20 20 20  __APPLE__))..   
12947 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c   UNIXVFS("unix",
12948 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f            autolo
12949 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0d 0a 23  ckIoFinder ),..#
1294a 65 6c 73 65 0d 0a 20 20 20 20 55 4e 49 58 56 46  else..    UNIXVF
1294b 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20  S("unix",       
1294c 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72     posixIoFinder
1294d 20 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20   ),..#endif..   
1294e 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e   UNIXVFS("unix-n
1294f 6f 6e 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b  one",     nolock
12950 49 6f 46 69 6e 64 65 72 20 29 2c 0d 0a 20 20 20  IoFinder ),..   
12951 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 64   UNIXVFS("unix-d
12952 6f 74 66 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63  otfile",  dotloc
12953 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0d 0a 20 20  kIoFinder ),..  
12954 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d    UNIXVFS("unix-
12955 65 78 63 6c 22 2c 20 20 20 20 20 70 6f 73 69 78  excl",     posix
12956 49 6f 46 69 6e 64 65 72 20 29 2c 0d 0a 23 69 66  IoFinder ),..#if
12957 20 4f 53 5f 56 58 57 4f 52 4b 53 0d 0a 20 20 20   OS_VXWORKS..   
12958 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e   UNIXVFS("unix-n
12959 61 6d 65 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46  amedsem", semIoF
1295a 69 6e 64 65 72 20 29 2c 0d 0a 23 65 6e 64 69 66  inder ),..#endif
1295b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
1295c 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
1295d 45 0d 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  E..    UNIXVFS("
1295e 75 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 20 20  unix-posix",    
1295f 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c  posixIoFinder ),
12960 0d 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b  ..#if !OS_VXWORK
12961 53 0d 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  S..    UNIXVFS("
12962 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20  unix-flock",    
12963 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c  flockIoFinder ),
12964 0d 0a 23 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66  ..#endif..#endif
12965 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
12966 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
12967 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
12968 50 50 4c 45 5f 5f 29 0d 0a 20 20 20 20 55 4e 49  PPLE__)..    UNI
12969 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c  XVFS("unix-afp",
1296a 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65        afpIoFinde
1296b 72 20 29 2c 0d 0a 20 20 20 20 55 4e 49 58 56 46  r ),..    UNIXVF
1296c 53 28 22 75 6e 69 78 2d 6e 66 73 22 2c 20 20 20  S("unix-nfs",   
1296d 20 20 20 6e 66 73 49 6f 46 69 6e 64 65 72 20 29     nfsIoFinder )
1296e 2c 0d 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  ,..    UNIXVFS("
1296f 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 20 20  unix-proxy",    
12970 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 29 2c  proxyIoFinder ),
12971 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a  ..#endif..  };..
12972 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
12973 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ;          /* Lo
12974 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d  op counter */...
12975 0a 20 20 2f 2a 20 44 6f 75 62 6c 65 2d 63 68 65  .  /* Double-che
12976 63 6b 20 74 68 61 74 20 74 68 65 20 61 53 79 73  ck that the aSys
12977 63 61 6c 6c 5b 5d 20 61 72 72 61 79 20 68 61 73  call[] array has
12978 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 74 65   been constructe
12979 64 0d 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 6c  d..  ** correctl
1297a 79 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 5b  y.  See ticket [
1297b 62 62 33 61 38 36 65 38 39 30 63 38 65 39 36 61  bb3a86e890c8e96a
1297c 62 5d 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  b] */..  assert(
1297d 20 41 72 72 61 79 53 69 7a 65 28 61 53 79 73 63   ArraySize(aSysc
1297e 61 6c 6c 29 3d 3d 32 30 20 29 3b 0d 0a 0d 0a 20  all)==20 );.... 
1297f 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c   /* Register all
12980 20 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69   VFSes defined i
12981 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72  n the aVfs[] arr
12982 61 79 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30  ay */..  for(i=0
12983 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73  ; i<(sizeof(aVfs
12984 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  )/sizeof(sqlite3
12985 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0d 0a 20  _vfs)); i++){.. 
12986 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72     sqlite3_vfs_r
12987 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 5d  egister(&aVfs[i]
12988 2c 20 69 3d 3d 30 29 3b 0d 0a 20 20 7d 0d 0a 20  , i==0);..  }.. 
12989 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1298a 4b 3b 20 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  K; ..}..../*..**
1298b 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70   Shutdown the op
1298c 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
1298d 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a  nterface...**..*
1298e 2a 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67  * Some operating
1298f 20 73 79 73 74 65 6d 73 20 6d 69 67 68 74 20 6e   systems might n
12990 65 65 64 20 74 6f 20 64 6f 20 73 6f 6d 65 20 63  eed to do some c
12991 6c 65 61 6e 75 70 20 69 6e 20 74 68 69 73 20 72  leanup in this r
12992 6f 75 74 69 6e 65 2c 0d 0a 2a 2a 20 74 6f 20 72  outine,..** to r
12993 65 6c 65 61 73 65 20 64 79 6e 61 6d 69 63 61 6c  elease dynamical
12994 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6f 62 6a  ly allocated obj
12995 65 63 74 73 2e 20 20 42 75 74 20 6e 6f 74 20 6f  ects.  But not o
12996 6e 20 75 6e 69 78 2e 0d 0a 2a 2a 20 54 68 69 73  n unix...** This
12997 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
12998 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0d 0a 2a  -op for unix...*
12999 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
1299a 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  t sqlite3_os_end
1299b 28 76 6f 69 64 29 7b 20 0d 0a 20 20 72 65 74 75  (void){ ..  retu
1299c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0d 0a  rn SQLITE_OK; ..
1299d 7d 0d 0a 20 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.. ..#endif /* 
1299e 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a  SQLITE_OS_UNIX *
1299f 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
129a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75  **** End of os_u
129a1 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
129a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129a4 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
129a5 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
129a6 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a   os_win.c ******
129a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129a9 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
129aa 30 34 20 4d 61 79 20 32 32 0d 0a 2a 2a 0d 0a 2a  04 May 22..**..*
129ab 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
129ac 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
129ad 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
129ae 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
129af 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
129b0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
129b1 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
129b2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
129b3 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
129b4 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
129b5 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
129b6 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
129b7 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
129b8 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
129b9 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
129ba 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
129bb 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
129bc 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
129bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129c1 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ******..**..** T
129c2 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
129c3 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73  s code that is s
129c4 70 65 63 69 66 69 63 20 74 6f 20 57 69 6e 64 6f  pecific to Windo
129c5 77 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c  ws...*/..#if SQL
129c6 49 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20  ITE_OS_WIN      
129c7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
129c8 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 6f   file is used fo
129c9 72 20 57 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a  r Windows only *
129ca 2f 0d 0a 0d 0a 23 69 66 64 65 66 20 5f 5f 43 59  /....#ifdef __CY
129cb 47 57 49 4e 5f 5f 0d 0a 23 20 69 6e 63 6c 75 64  GWIN__..# includ
129cc 65 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e  e <sys/cygwin.h>
129cd 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
129ce 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20  ** Include code 
129cf 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
129d0 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c  o all os_*.c fil
129d1 65 73 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  es..*/../*******
129d2 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
129d3 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74  os_common.h in t
129d4 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
129d5 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
129d6 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
129d7 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
129d8 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ile os_common.h 
129d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129db 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  *******/../*..**
129dc 20 32 30 30 34 20 4d 61 79 20 32 32 0d 0a 2a 2a   2004 May 22..**
129dd 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
129de 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
129df 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
129e0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
129e1 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61  ce of..** a lega
129e2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
129e3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a  s a blessing:..*
129e4 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  *..**    May you
129e5 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
129e6 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61   evil...**    Ma
129e7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
129e8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
129e9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
129ea 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d  others...**    M
129eb 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
129ec 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
129ed 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
129ee 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a  give...**..*****
129ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
129f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  *********..**..*
129f4 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
129f5 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20  ains macros and 
129f6 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20  a little bit of 
129f7 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
129f8 6d 6f 6e 20 74 6f 0d 0a 2a 2a 20 61 6c 6c 20 6f  mon to..** all o
129f9 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
129fa 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
129fb 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
129fc 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
129fd 73 65 0d 0a 2a 2a 20 66 69 6c 65 73 2e 0d 0a 2a  se..** files...*
129fe 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
129ff 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75  should be #inclu
12a00 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e  ded by the os_*.
12a01 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49  c files only.  I
12a02 74 20 69 73 20 6e 6f 74 20 61 0d 0a 2a 2a 20 67  t is not a..** g
12a03 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68  eneral purpose h
12a04 65 61 64 65 72 20 66 69 6c 65 2e 0d 0a 2a 2f 0d  eader file...*/.
12a05 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d  .#ifndef _OS_COM
12a06 4d 4f 4e 5f 48 5f 0d 0a 23 64 65 66 69 6e 65 20  MON_H_..#define 
12a07 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0d 0a 0d  _OS_COMMON_H_...
12a08 0a 2f 2a 0d 0a 2a 2a 20 41 74 20 6c 65 61 73 74  ./*..** At least
12a09 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73   two bugs have s
12a0a 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73  lipped in becaus
12a0b 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65  e we changed the
12a0c 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0d 0a 2a   MEMORY_DEBUG..*
12a0d 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
12a0e 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
12a0f 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
12a10 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
12a11 64 65 20 74 68 65 0d 0a 2a 2a 20 73 77 69 74 63  de the..** switc
12a12 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  h.  The followin
12a13 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61  g code should ca
12a14 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d  tch this problem
12a15 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
12a16 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 4d 45  ...*/..#ifdef ME
12a17 4d 4f 52 59 5f 44 45 42 55 47 0d 0a 23 20 65 72  MORY_DEBUG..# er
12a18 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
12a19 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
12a1a 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
12a1b 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
12a1c 61 64 2e 22 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ad."..#endif....
12a1d 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
12a1e 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69  TE_TEST) && defi
12a1f 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
12a20 29 0d 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49  )..# ifndef SQLI
12a21 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43  TE_DEBUG_OS_TRAC
12a22 45 0d 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  E..#   define SQ
12a23 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52  LITE_DEBUG_OS_TR
12a24 41 43 45 20 30 0d 0a 23 20 65 6e 64 69 66 0d 0a  ACE 0..# endif..
12a25 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54    int sqlite3OST
12a26 72 61 63 65 20 3d 20 53 51 4c 49 54 45 5f 44 45  race = SQLITE_DE
12a27 42 55 47 5f 4f 53 5f 54 52 41 43 45 3b 0d 0a 23  BUG_OS_TRACE;..#
12a28 20 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 28   define OSTRACE(
12a29 58 29 20 20 20 20 20 20 20 20 20 20 69 66 28 20  X)          if( 
12a2a 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
12a2b 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
12a2c 6e 74 66 20 58 0d 0a 23 65 6c 73 65 0d 0a 23 20  ntf X..#else..# 
12a2d 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 28 58  define OSTRACE(X
12a2e 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  )..#endif..../*.
12a2f 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70  .** Macros for p
12a30 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69  erformance traci
12a31 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75  ng.  Normally tu
12a32 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20  rned off.  Only 
12a33 77 6f 72 6b 73 0d 0a 2a 2a 20 6f 6e 20 69 34 38  works..** on i48
12a34 36 20 68 61 72 64 77 61 72 65 2e 0d 0a 2a 2f 0d  6 hardware...*/.
12a35 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
12a36 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
12a37 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 68 77 74 69  ..../* ..** hwti
12a38 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e  me.h contains in
12a39 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63  line assembler c
12a3a 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
12a3b 74 69 6e 67 20 0d 0a 2a 2a 20 68 69 67 68 2d 70  ting ..** high-p
12a3c 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
12a3d 67 20 72 6f 75 74 69 6e 65 73 2e 0d 0a 2a 2f 0d  g routines...*/.
12a3e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
12a3f 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e   Include hwtime.
12a40 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
12a41 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  of os_common.h *
12a42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
12a43 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
12a44 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
12a45 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
12a46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a48 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20 4d  /../*..** 2008 M
12a49 61 79 20 32 37 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ay 27..**..** Th
12a4a 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
12a4b 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
12a4c 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
12a4d 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
12a4e 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
12a4f 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
12a50 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
12a51 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
12a52 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
12a53 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
12a54 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
12a55 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
12a56 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
12a57 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
12a58 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
12a59 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
12a5a 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
12a5b 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
12a5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a60 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  **..**..** This 
12a61 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
12a62 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
12a63 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
12a64 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0d  gh-performance".
12a65 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
12a66 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
12a67 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 48  ..*/..#ifndef _H
12a68 57 54 49 4d 45 5f 48 5f 0d 0a 23 64 65 66 69 6e  WTIME_H_..#defin
12a69 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0d 0a 0d 0a  e _HWTIME_H_....
12a6a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  /*..** The follo
12a6b 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  wing routine onl
12a6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69  y works on penti
12a6d 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77  um-class (or new
12a6e 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0d  er) processors..
12a6f 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20  .** It uses the 
12a70 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20  RDTSC opcode to 
12a71 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63  read the cycle c
12a72 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f  ount value out o
12a73 66 20 74 68 65 0d 0a 2a 2a 20 70 72 6f 63 65 73  f the..** proces
12a74 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20  sor and returns 
12a75 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69  that value.  Thi
12a76 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  s can be used fo
12a77 72 20 68 69 67 68 2d 72 65 73 0d 0a 2a 2a 20 70  r high-res..** p
12a78 72 6f 66 69 6c 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23  rofiling...*/..#
12a79 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
12a7a 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  UC__) || defined
12a7b 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c  (_MSC_VER)) && \
12a7c 0d 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  ..      (defined
12a7d 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
12a7e 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
12a7f 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
12a80 0d 0a 0d 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ....  #if define
12a81 64 28 5f 5f 47 4e 55 43 5f 5f 29 0d 0a 0d 0a 20  d(__GNUC__).... 
12a82 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
12a83 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
12a84 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0d 0a  3Hwtime(void){..
12a85 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
12a86 74 20 6c 6f 2c 20 68 69 3b 0d 0a 20 20 20 20 20  t lo, hi;..     
12a87 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
12a88 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20  le__ ("rdtsc" : 
12a89 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20  "=a" (lo), "=d" 
12a8a 28 68 69 29 29 3b 0d 0a 20 20 20 20 20 72 65 74  (hi));..     ret
12a8b 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74  urn (sqlite_uint
12a8c 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f  64)hi << 32 | lo
12a8d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23 65 6c 69  ;..  }....  #eli
12a8e 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  f defined(_MSC_V
12a8f 45 52 29 0d 0a 0d 0a 20 20 5f 5f 64 65 63 6c 73  ER)....  __decls
12a90 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c  pec(naked) __inl
12a91 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ine sqlite_uint6
12a92 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65  4 __cdecl sqlite
12a93 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0d 0a  3Hwtime(void){..
12a94 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0d 0a 20 20       __asm {..  
12a95 20 20 20 20 20 20 72 64 74 73 63 0d 0a 20 20 20        rdtsc..   
12a96 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b       ret       ;
12a97 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74   return value at
12a98 20 45 44 58 3a 45 41 58 0d 0a 20 20 20 20 20 7d   EDX:EAX..     }
12a99 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23 65 6e 64 69  ..  }....  #endi
12a9a 66 0d 0a 0d 0a 23 65 6c 69 66 20 28 64 65 66 69  f....#elif (defi
12a9b 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
12a9c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
12a9d 34 5f 5f 29 29 0d 0a 0d 0a 20 20 5f 5f 69 6e 6c  4__))....  __inl
12a9e 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
12a9f 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
12aa0 65 28 76 6f 69 64 29 7b 0d 0a 20 20 20 20 20 20  e(void){..      
12aa1 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61  unsigned long va
12aa2 6c 3b 0d 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  l;..      __asm_
12aa3 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
12aa4 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
12aa5 76 61 6c 29 29 3b 0d 0a 20 20 20 20 20 20 72 65  val));..      re
12aa6 74 75 72 6e 20 76 61 6c 3b 0d 0a 20 20 7d 0d 0a  turn val;..  }..
12aa7 20 0d 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65   ..#elif (define
12aa8 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
12aa9 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
12aaa 0d 0a 0d 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ....  __inline__
12aab 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
12aac 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
12aad 64 29 7b 0d 0a 20 20 20 20 20 20 75 6e 73 69 67  d){..      unsig
12aae 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
12aaf 74 76 61 6c 3b 0d 0a 20 20 20 20 20 20 75 6e 73  tval;..      uns
12ab0 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b  igned long junk;
12ab1 0d 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ..      __asm__ 
12ab2 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
12ab3 6e 5c 0d 0a 20 20 20 20 20 20 20 20 20 20 31 3a  n\..          1:
12ab4 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31        mftbu   %1
12ab5 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  \n\..           
12ab6 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
12ab7 4c 30 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20 20  L0\n\..         
12ab8 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20           mftbu  
12ab9 20 25 30 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20   %0\n\..        
12aba 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20            cmpw  
12abb 20 20 25 30 2c 25 31 5c 6e 5c 0d 0a 20 20 20 20    %0,%1\n\..    
12abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e                bn
12abd 65 20 20 20 20 20 31 62 22 0d 0a 20 20 20 20 20  e     1b"..     
12abe 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22               : "
12abf 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d  =r" (retval), "=
12ac0 72 22 20 28 6a 75 6e 6b 29 29 3b 0d 0a 20 20 20  r" (junk));..   
12ac1 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
12ac2 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 65 6c 73 65 0d  ;..  }....#else.
12ac3 0a 0d 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64  ...  #error Need
12ac4 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
12ac5 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  of sqlite3Hwtime
12ac6 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
12ac7 66 6f 72 6d 2e 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20  form.....  /*.. 
12ac8 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77   ** To compile w
12ac9 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74  ithout implement
12aca 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d  ing sqlite3Hwtim
12acb 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
12acc 74 66 6f 72 6d 2c 0d 0a 20 20 2a 2a 20 79 6f 75  tform,..  ** you
12acd 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
12ace 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
12acf 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
12ad0 6e 67 0d 0a 20 20 2a 2a 20 73 74 75 62 20 66 75  ng..  ** stub fu
12ad1 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c  nction.  You wil
12ad2 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75  l lose timing su
12ad3 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0d 0a  pport for many..
12ad4 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
12ad5 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
12ad6 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
12ad7 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0d 0a 20   it should at.. 
12ad8 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c   ** least compil
12ad9 65 20 61 6e 64 20 72 75 6e 2e 0d 0a 20 20 2a 2f  e and run...  */
12ada 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
12adb 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34     sqlite_uint64
12adc 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
12add 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73  oid){ return ((s
12ade 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b  qlite_uint64)0);
12adf 20 7d 0d 0a 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a   }....#endif....
12ae0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
12ae1 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a  ed(_HWTIME_H_) *
12ae2 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
12ae3 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
12ae4 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
12ae5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12ae6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12ae7 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
12ae8 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
12ae9 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
12aea 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  ff in os_common.
12aeb 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
12aec 2a 2a 2a 2a 2f 0d 0a 0d 0a 73 74 61 74 69 63 20  ****/....static 
12aed 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
12aee 73 74 61 72 74 3b 0d 0a 73 74 61 74 69 63 20 73  start;..static s
12aef 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65  qlite_uint64 g_e
12af0 6c 61 70 73 65 64 3b 0d 0a 23 64 65 66 69 6e 65  lapsed;..#define
12af1 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20   TIMER_START    
12af2 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74     g_start=sqlit
12af3 65 33 48 77 74 69 6d 65 28 29 0d 0a 23 64 65 66  e3Hwtime()..#def
12af4 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20  ine TIMER_END   
12af5 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d        g_elapsed=
12af6 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d  sqlite3Hwtime()-
12af7 67 5f 73 74 61 72 74 0d 0a 23 64 65 66 69 6e 65  g_start..#define
12af8 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
12af9 20 20 20 67 5f 65 6c 61 70 73 65 64 0d 0a 23 65     g_elapsed..#e
12afa 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20 54 49 4d  lse..#define TIM
12afb 45 52 5f 53 54 41 52 54 0d 0a 23 64 65 66 69 6e  ER_START..#defin
12afc 65 20 54 49 4d 45 52 5f 45 4e 44 0d 0a 23 64 65  e TIMER_END..#de
12afd 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
12afe 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f  ED     ((sqlite_
12aff 75 69 6e 74 36 34 29 30 29 0d 0a 23 65 6e 64 69  uint64)0)..#endi
12b00 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 77  f..../*..** If w
12b01 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74  e compile with t
12b02 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d  he SQLITE_TEST m
12b03 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74  acro set, then t
12b04 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
12b05 63 6b 0d 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77  ck..** of code w
12b06 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20  ill give us the 
12b07 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c  ability to simul
12b08 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65  ate a disk I/O e
12b09 72 72 6f 72 2e 20 20 54 68 69 73 0d 0a 2a 2a 20  rror.  This..** 
12b0a 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
12b0b 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f  ing the I/O reco
12b0c 76 65 72 79 20 6c 6f 67 69 63 2e 0d 0a 2a 2f 0d  very logic...*/.
12b0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
12b0e 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  EST..SQLITE_API 
12b0f 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
12b10 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20  rror_hit = 0;   
12b11 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
12b12 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20  l number of I/O 
12b13 45 72 72 6f 72 73 20 2a 2f 0d 0a 53 51 4c 49 54  Errors */..SQLIT
12b14 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12b15 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
12b16 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  it = 0;        /
12b17 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
12b18 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f  benign errors */
12b19 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
12b1a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
12b1b 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20  r_pending = 0;  
12b1c 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64        /* Count d
12b1d 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f  own to first I/O
12b1e 20 65 72 72 6f 72 20 2a 2f 0d 0a 53 51 4c 49 54   error */..SQLIT
12b1f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12b20 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
12b21 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  st = 0;        /
12b22 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72  * True if I/O er
12b23 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0d  rors persist */.
12b24 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12b25 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
12b26 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20  _benign = 0;    
12b27 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
12b28 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67  errors are benig
12b29 6e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  n */..SQLITE_API
12b2a 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
12b2b 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20  kfull_pending = 
12b2c 30 3b 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  0;..SQLITE_API i
12b2d 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
12b2e 75 6c 6c 20 3d 20 30 3b 0d 0a 23 64 65 66 69 6e  ull = 0;..#defin
12b2f 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
12b30 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74  rBenign(X) sqlit
12b31 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
12b32 67 6e 3d 28 58 29 0d 0a 23 64 65 66 69 6e 65 20  gn=(X)..#define 
12b33 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
12b34 43 4f 44 45 29 20 20 5c 0d 0a 20 20 69 66 28 20  CODE)  \..  if( 
12b35 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  (sqlite3_io_erro
12b36 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c  r_persist && sql
12b37 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
12b38 74 29 20 5c 0d 0a 20 20 20 20 20 20 20 7c 7c 20  t) \..       || 
12b39 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
12b3a 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20  _pending-- == 1 
12b3b 29 20 20 5c 0d 0a 20 20 20 20 20 20 20 20 20 20  )  \..          
12b3c 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72      { local_ioer
12b3d 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0d 0a 73 74  r(); CODE; }..st
12b3e 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
12b3f 69 6f 65 72 72 28 29 7b 0d 0a 20 20 49 4f 54 52  ioerr(){..  IOTR
12b40 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
12b41 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  ;..  sqlite3_io_
12b42 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0d 0a 20 20  error_hit++;..  
12b43 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
12b44 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
12b45 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
12b46 68 61 72 64 68 69 74 2b 2b 3b 0d 0a 7d 0d 0a 23  hardhit++;..}..#
12b47 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
12b48 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
12b49 45 29 20 5c 0d 0a 20 20 20 69 66 28 20 73 71 6c  E) \..   if( sql
12b4a 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
12b4b 6e 64 69 6e 67 20 29 7b 20 5c 0d 0a 20 20 20 20  nding ){ \..    
12b4c 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
12b4d 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d  kfull_pending ==
12b4e 20 31 20 29 7b 20 5c 0d 0a 20 20 20 20 20 20 20   1 ){ \..       
12b4f 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c  local_ioerr(); \
12b50 0d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ..       sqlite3
12b51 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c  _diskfull = 1; \
12b52 0d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ..       sqlite3
12b53 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
12b54 31 3b 20 5c 0d 0a 20 20 20 20 20 20 20 43 4f 44  1; \..       COD
12b55 45 3b 20 5c 0d 0a 20 20 20 20 20 7d 65 6c 73 65  E; \..     }else
12b56 7b 20 5c 0d 0a 20 20 20 20 20 20 20 73 71 6c 69  { \..       sqli
12b57 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
12b58 64 69 6e 67 2d 2d 3b 20 5c 0d 0a 20 20 20 20 20  ding--; \..     
12b59 7d 20 5c 0d 0a 20 20 20 7d 0d 0a 23 65 6c 73 65  } \..   }..#else
12b5a 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ..#define Simula
12b5b 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
12b5c 58 29 0d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  X)..#define Simu
12b5d 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0d 0a  lateIOError(A)..
12b5e 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
12b5f 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
12b60 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
12b61 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c  ** When testing,
12b62 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66   keep a count of
12b63 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f   the number of o
12b64 70 65 6e 20 66 69 6c 65 73 2e 0d 0a 2a 2f 0d 0a  pen files...*/..
12b65 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
12b66 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ST..SQLITE_API i
12b67 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
12b68 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d  file_count = 0;.
12b69 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
12b6a 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33  nter(X)  sqlite3
12b6b 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
12b6c 2b 3d 28 58 29 0d 0a 23 65 6c 73 65 0d 0a 23 64  +=(X)..#else..#d
12b6d 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
12b6e 72 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  r(X)..#endif....
12b6f 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
12b70 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ed(_OS_COMMON_H_
12b71 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ) */..../*******
12b72 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
12b73 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
12b74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12b75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12b76 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
12b77 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
12b78 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
12b79 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e  t off in os_win.
12b7a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
12b7b 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  *******/..../*..
12b7c 2a 2a 20 53 6f 6d 65 20 4d 69 63 72 6f 73 6f 66  ** Some Microsof
12b7d 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b  t compilers lack
12b7e 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   this definition
12b7f 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 49  ...*/..#ifndef I
12b80 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
12b81 49 42 55 54 45 53 0d 0a 23 20 64 65 66 69 6e 65  IBUTES..# define
12b82 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54   INVALID_FILE_AT
12b83 54 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 44  TRIBUTES ((DWORD
12b84 29 2d 31 29 20 0d 0a 23 65 6e 64 69 66 0d 0a 0d  )-1) ..#endif...
12b85 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
12b86 72 65 6e 63 65 73 20 2a 2f 0d 0a 74 79 70 65 64  rences */..typed
12b87 65 66 20 73 74 72 75 63 74 20 77 69 6e 53 68 6d  ef struct winShm
12b88 20 77 69 6e 53 68 6d 3b 20 20 20 20 20 20 20 20   winShm;        
12b89 20 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69     /* A connecti
12b8a 6f 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d 65 6d  on to shared-mem
12b8b 6f 72 79 20 2a 2f 0d 0a 74 79 70 65 64 65 66 20  ory */..typedef 
12b8c 73 74 72 75 63 74 20 77 69 6e 53 68 6d 4e 6f 64  struct winShmNod
12b8d 65 20 77 69 6e 53 68 6d 4e 6f 64 65 3b 20 20 20  e winShmNode;   
12b8e 2f 2a 20 41 20 72 65 67 69 6f 6e 20 6f 66 20 73  /* A region of s
12b8f 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 2a 2f 0d  hared-memory */.
12b90 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 69 6e 43 45 20  .../*..** WinCE 
12b91 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70  lacks native sup
12b92 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f  port for file lo
12b93 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65  cking so we have
12b94 20 74 6f 20 66 61 6b 65 20 69 74 0d 0a 2a 2a 20   to fake it..** 
12b95 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f  with some code o
12b96 66 20 6f 75 72 20 6f 77 6e 2e 0d 0a 2a 2f 0d 0a  f our own...*/..
12b97 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
12b98 4e 43 45 0d 0a 74 79 70 65 64 65 66 20 73 74 72  NCE..typedef str
12b99 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0d  uct winceLock {.
12b9a 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b  .  int nReaders;
12b9b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
12b9c 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73   of reader locks
12b9d 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0d 0a 20 20   obtained */..  
12b9e 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20  BOOL bPending;  
12b9f 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73      /* Indicates
12ba0 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20   a pending lock 
12ba1 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
12ba2 64 20 2a 2f 0d 0a 20 20 42 4f 4f 4c 20 62 52 65  d */..  BOOL bRe
12ba3 73 65 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49  served;     /* I
12ba4 6e 64 69 63 61 74 65 73 20 61 20 72 65 73 65 72  ndicates a reser
12ba5 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65  ved lock has bee
12ba6 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0d 0a 20  n obtained */.. 
12ba7 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76 65   BOOL bExclusive
12ba8 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65  ;    /* Indicate
12ba9 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
12baa 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74  ock has been obt
12bab 61 69 6e 65 64 20 2a 2f 0d 0a 7d 20 77 69 6e 63  ained */..} winc
12bac 65 4c 6f 63 6b 3b 0d 0a 23 65 6e 64 69 66 0d 0a  eLock;..#endif..
12bad 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 77 69 6e  ../*..** The win
12bae 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
12baf 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  s a subclass of 
12bb0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 20 73 70  sqlite3_file* sp
12bb1 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 77 69  ecific to the wi
12bb2 6e 33 32 0d 0a 2a 2a 20 70 6f 72 74 61 62 69 6c  n32..** portabil
12bb3 69 74 79 20 6c 61 79 65 72 2e 0d 0a 2a 2f 0d 0a  ity layer...*/..
12bb4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77  typedef struct w
12bb5 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0d  inFile winFile;.
12bb6 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20  .struct winFile 
12bb7 7b 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  {..  const sqlit
12bb8 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
12bb9 4d 65 74 68 6f 64 3b 20 2f 2a 2a 2a 20 4d 75 73  Method; /*** Mus
12bba 74 20 62 65 20 66 69 72 73 74 20 2a 2a 2a 2f 0d  t be first ***/.
12bbb 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
12bbc 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54 68  pVfs;      /* Th
12bbd 65 20 56 46 53 20 75 73 65 64 20 74 6f 20 6f 70  e VFS used to op
12bbe 65 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0d  en this file */.
12bbf 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 20 20  .  HANDLE h;    
12bc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
12bc1 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69  ndle for accessi
12bc2 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a  ng the file */..
12bc3 20 20 75 38 20 6c 6f 63 6b 74 79 70 65 3b 20 20    u8 locktype;  
12bc4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70            /* Typ
12bc5 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e  e of lock curren
12bc6 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73  tly held on this
12bc7 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 73 68 6f 72   file */..  shor
12bc8 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  t sharedLockByte
12bc9 3b 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20  ;   /* Randomly 
12bca 63 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64  chosen byte used
12bcb 20 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63   as a shared loc
12bcc 6b 20 2a 2f 0d 0a 20 20 75 38 20 63 74 72 6c 46  k */..  u8 ctrlF
12bcd 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
12bce 2f 2a 20 46 6c 61 67 73 2e 20 20 53 65 65 20 57  /* Flags.  See W
12bcf 49 4e 46 49 4c 45 5f 2a 20 62 65 6c 6f 77 20 2a  INFILE_* below *
12bd0 2f 0d 0a 20 20 44 57 4f 52 44 20 6c 61 73 74 45  /..  DWORD lastE
12bd1 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20  rrno;        /* 
12bd2 54 68 65 20 57 69 6e 64 6f 77 73 20 65 72 72 6e  The Windows errn
12bd3 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20  o from the last 
12bd4 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20  I/O error */..  
12bd5 77 69 6e 53 68 6d 20 2a 70 53 68 6d 3b 20 20 20  winShm *pShm;   
12bd6 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 61          /* Insta
12bd7 6e 63 65 20 6f 66 20 73 68 61 72 65 64 20 6d 65  nce of shared me
12bd8 6d 6f 72 79 20 6f 6e 20 74 68 69 73 20 66 69 6c  mory on this fil
12bd9 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  e */..  const ch
12bda 61 72 20 2a 7a 50 61 74 68 3b 20 20 20 20 20 20  ar *zPath;      
12bdb 2f 2a 20 46 75 6c 6c 20 70 61 74 68 6e 61 6d 65  /* Full pathname
12bdc 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   of this file */
12bdd 0d 0a 20 20 69 6e 74 20 73 7a 43 68 75 6e 6b 3b  ..  int szChunk;
12bde 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
12bdf 68 75 6e 6b 20 73 69 7a 65 20 63 6f 6e 66 69 67  hunk size config
12be0 75 72 65 64 20 62 79 20 46 43 4e 54 4c 5f 43 48  ured by FCNTL_CH
12be1 55 4e 4b 5f 53 49 5a 45 20 2a 2f 0d 0a 23 69 66  UNK_SIZE */..#if
12be2 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
12be3 0d 0a 20 20 4c 50 57 53 54 52 20 7a 44 65 6c 65  ..  LPWSTR zDele
12be4 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e  teOnClose;  /* N
12be5 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64  ame of file to d
12be6 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 69  elete when closi
12be7 6e 67 20 2a 2f 0d 0a 20 20 48 41 4e 44 4c 45 20  ng */..  HANDLE 
12be8 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
12be9 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
12bea 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
12beb 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
12bec 2a 2f 20 20 0d 0a 20 20 48 41 4e 44 4c 45 20 68  */  ..  HANDLE h
12bed 53 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20  Shared;         
12bee 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79  /* Shared memory
12bef 20 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f   segment used fo
12bf0 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0d 0a 20 20  r locking */..  
12bf1 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b  winceLock local;
12bf2 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73          /* Locks
12bf3 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69   obtained by thi
12bf4 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69  s instance of wi
12bf5 6e 46 69 6c 65 20 2a 2f 0d 0a 20 20 77 69 6e 63  nFile */..  winc
12bf6 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 20  eLock *shared;  
12bf7 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68      /* Global sh
12bf8 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79  ared lock memory
12bf9 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 20 2a   for the file  *
12bfa 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d  /..#endif..};...
12bfb 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ./*..** Allowed 
12bfc 76 61 6c 75 65 73 20 66 6f 72 20 77 69 6e 46 69  values for winFi
12bfd 6c 65 2e 63 74 72 6c 46 6c 61 67 73 0d 0a 2a 2f  le.ctrlFlags..*/
12bfe 0d 0a 23 64 65 66 69 6e 65 20 57 49 4e 46 49 4c  ..#define WINFIL
12bff 45 5f 50 45 52 53 49 53 54 5f 57 41 4c 20 20 20  E_PERSIST_WAL   
12c00 20 20 30 78 30 34 20 20 20 2f 2a 20 50 65 72 73    0x04   /* Pers
12c01 69 73 74 65 6e 74 20 57 41 4c 20 6d 6f 64 65 20  istent WAL mode 
12c02 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 49 4e 46  */..#define WINF
12c03 49 4c 45 5f 50 53 4f 57 20 20 20 20 20 20 20 20  ILE_PSOW        
12c04 20 20 20 20 30 78 31 30 20 20 20 2f 2a 20 53 51      0x10   /* SQ
12c05 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52  LITE_IOCAP_POWER
12c06 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 20 2a  SAFE_OVERWRITE *
12c07 2f 0d 0a 0d 0a 2f 2a 0d 0a 20 2a 20 49 66 20 63  /..../*.. * If c
12c08 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c  ompiled with SQL
12c09 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43  ITE_WIN32_MALLOC
12c0a 20 6f 6e 20 57 69 6e 64 6f 77 73 2c 20 77 65 20   on Windows, we 
12c0b 77 69 6c 6c 20 75 73 65 20 74 68 65 0d 0a 20 2a  will use the.. *
12c0c 20 76 61 72 69 6f 75 73 20 57 69 6e 33 32 20 41   various Win32 A
12c0d 50 49 20 68 65 61 70 20 66 75 6e 63 74 69 6f 6e  PI heap function
12c0e 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 75 72  s instead of our
12c0f 20 6f 77 6e 2e 0d 0a 20 2a 2f 0d 0a 23 69 66 64   own... */..#ifd
12c10 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  ef SQLITE_WIN32_
12c11 4d 41 4c 4c 4f 43 0d 0a 2f 2a 0d 0a 20 2a 20 54  MALLOC../*.. * T
12c12 68 65 20 69 6e 69 74 69 61 6c 20 73 69 7a 65 20  he initial size 
12c13 6f 66 20 74 68 65 20 57 69 6e 33 32 2d 73 70 65  of the Win32-spe
12c14 63 69 66 69 63 20 68 65 61 70 2e 20 20 54 68 69  cific heap.  Thi
12c15 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 7a  s value may be z
12c16 65 72 6f 2e 0d 0a 20 2a 2f 0d 0a 23 69 66 6e 64  ero... */..#ifnd
12c17 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  ef SQLITE_WIN32_
12c18 48 45 41 50 5f 49 4e 49 54 5f 53 49 5a 45 0d 0a  HEAP_INIT_SIZE..
12c19 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
12c1a 5f 57 49 4e 33 32 5f 48 45 41 50 5f 49 4e 49 54  _WIN32_HEAP_INIT
12c1b 5f 53 49 5a 45 20 28 28 53 51 4c 49 54 45 5f 44  _SIZE ((SQLITE_D
12c1c 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
12c1d 45 29 20 2a 20 5c 0d 0a 20 20 20 20 20 20 20 20  E) * \..        
12c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
12c20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
12c21 41 47 45 5f 53 49 5a 45 29 20 2b 20 34 31 39 34  AGE_SIZE) + 4194
12c22 33 30 34 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  304)..#endif....
12c23 2f 2a 0d 0a 20 2a 20 54 68 65 20 6d 61 78 69 6d  /*.. * The maxim
12c24 75 6d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 57  um size of the W
12c25 69 6e 33 32 2d 73 70 65 63 69 66 69 63 20 68 65  in32-specific he
12c26 61 70 2e 20 20 54 68 69 73 20 76 61 6c 75 65 20  ap.  This value 
12c27 6d 61 79 20 62 65 20 7a 65 72 6f 2e 0d 0a 20 2a  may be zero... *
12c28 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
12c29 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41 58  E_WIN32_HEAP_MAX
12c2a 5f 53 49 5a 45 0d 0a 23 20 20 64 65 66 69 6e 65  _SIZE..#  define
12c2b 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45   SQLITE_WIN32_HE
12c2c 41 50 5f 4d 41 58 5f 53 49 5a 45 20 20 28 30 29  AP_MAX_SIZE  (0)
12c2d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
12c2e 20 2a 20 54 68 65 20 65 78 74 72 61 20 66 6c 61   * The extra fla
12c2f 67 73 20 74 6f 20 75 73 65 20 69 6e 20 63 61 6c  gs to use in cal
12c30 6c 73 20 74 6f 20 74 68 65 20 57 69 6e 33 32 20  ls to the Win32 
12c31 68 65 61 70 20 41 50 49 73 2e 20 20 54 68 69 73  heap APIs.  This
12c32 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0d 0a 20   value may be.. 
12c33 2a 20 7a 65 72 6f 20 66 6f 72 20 74 68 65 20 64  * zero for the d
12c34 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 2e  efault behavior.
12c35 0d 0a 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  .. */..#ifndef S
12c36 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50  QLITE_WIN32_HEAP
12c37 5f 46 4c 41 47 53 0d 0a 23 20 20 64 65 66 69 6e  _FLAGS..#  defin
12c38 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  e SQLITE_WIN32_H
12c39 45 41 50 5f 46 4c 41 47 53 20 20 20 20 20 28 30  EAP_FLAGS     (0
12c3a 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  )..#endif..../*.
12c3b 0a 2a 2a 20 54 68 65 20 77 69 6e 4d 65 6d 44 61  .** The winMemDa
12c3c 74 61 20 73 74 72 75 63 74 75 72 65 20 73 74 6f  ta structure sto
12c3d 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  res information 
12c3e 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
12c3f 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 0d 0a  Win32-specific..
12c40 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ** sqlite3_mem_m
12c41 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74  ethods implement
12c42 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  ation...*/..type
12c43 64 65 66 20 73 74 72 75 63 74 20 77 69 6e 4d 65  def struct winMe
12c44 6d 44 61 74 61 20 77 69 6e 4d 65 6d 44 61 74 61  mData winMemData
12c45 3b 0d 0a 73 74 72 75 63 74 20 77 69 6e 4d 65 6d  ;..struct winMem
12c46 44 61 74 61 20 7b 0d 0a 23 69 66 6e 64 65 66 20  Data {..#ifndef 
12c47 4e 44 45 42 55 47 0d 0a 20 20 75 33 32 20 6d 61  NDEBUG..  u32 ma
12c48 67 69 63 3b 20 20 20 20 2f 2a 20 4d 61 67 69 63  gic;    /* Magic
12c49 20 6e 75 6d 62 65 72 20 74 6f 20 64 65 74 65 63   number to detec
12c4a 74 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72  t structure corr
12c4b 75 70 74 69 6f 6e 2e 20 2a 2f 0d 0a 23 65 6e 64  uption. */..#end
12c4c 69 66 0d 0a 20 20 48 41 4e 44 4c 45 20 68 48 65  if..  HANDLE hHe
12c4d 61 70 3b 20 2f 2a 20 54 68 65 20 68 61 6e 64 6c  ap; /* The handl
12c4e 65 20 74 6f 20 6f 75 72 20 68 65 61 70 2e 20 2a  e to our heap. *
12c4f 2f 0d 0a 20 20 42 4f 4f 4c 20 62 4f 77 6e 65 64  /..  BOOL bOwned
12c50 3b 20 20 2f 2a 20 44 6f 20 77 65 20 6f 77 6e 20  ;  /* Do we own 
12c51 74 68 65 20 68 65 61 70 20 28 69 2e 65 2e 20 64  the heap (i.e. d
12c52 65 73 74 72 6f 79 20 69 74 20 6f 6e 20 73 68 75  estroy it on shu
12c53 74 64 6f 77 6e 29 3f 20 2a 2f 0d 0a 7d 3b 0d 0a  tdown)? */..};..
12c54 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
12c55 0d 0a 23 64 65 66 69 6e 65 20 57 49 4e 4d 45 4d  ..#define WINMEM
12c56 5f 4d 41 47 49 43 20 20 20 20 20 30 78 34 32 62  _MAGIC     0x42b
12c57 32 38 33 30 62 0d 0a 23 65 6e 64 69 66 0d 0a 0d  2830b..#endif...
12c58 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 77  .static struct w
12c59 69 6e 4d 65 6d 44 61 74 61 20 77 69 6e 5f 6d 65  inMemData win_me
12c5a 6d 5f 64 61 74 61 20 3d 20 7b 0d 0a 23 69 66 6e  m_data = {..#ifn
12c5b 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20 57 49  def NDEBUG..  WI
12c5c 4e 4d 45 4d 5f 4d 41 47 49 43 2c 0d 0a 23 65 6e  NMEM_MAGIC,..#en
12c5d 64 69 66 0d 0a 20 20 4e 55 4c 4c 2c 20 46 41 4c  dif..  NULL, FAL
12c5e 53 45 0d 0a 7d 3b 0d 0a 0d 0a 23 69 66 6e 64 65  SE..};....#ifnde
12c5f 66 20 4e 44 45 42 55 47 0d 0a 23 64 65 66 69 6e  f NDEBUG..#defin
12c60 65 20 77 69 6e 4d 65 6d 41 73 73 65 72 74 4d 61  e winMemAssertMa
12c61 67 69 63 28 29 20 61 73 73 65 72 74 28 20 77 69  gic() assert( wi
12c62 6e 5f 6d 65 6d 5f 64 61 74 61 2e 6d 61 67 69 63  n_mem_data.magic
12c63 3d 3d 57 49 4e 4d 45 4d 5f 4d 41 47 49 43 20 29  ==WINMEM_MAGIC )
12c64 0d 0a 23 65 6c 73 65 0d 0a 23 64 65 66 69 6e 65  ..#else..#define
12c65 20 77 69 6e 4d 65 6d 41 73 73 65 72 74 4d 61 67   winMemAssertMag
12c66 69 63 28 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ic()..#endif....
12c67 23 64 65 66 69 6e 65 20 77 69 6e 4d 65 6d 47 65  #define winMemGe
12c68 74 48 65 61 70 28 29 20 77 69 6e 5f 6d 65 6d 5f  tHeap() win_mem_
12c69 64 61 74 61 2e 68 48 65 61 70 0d 0a 0d 0a 73 74  data.hHeap....st
12c6a 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 4d 65  atic void *winMe
12c6b 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  mMalloc(int nByt
12c6c 65 73 29 3b 0d 0a 73 74 61 74 69 63 20 76 6f 69  es);..static voi
12c6d 64 20 77 69 6e 4d 65 6d 46 72 65 65 28 76 6f 69  d winMemFree(voi
12c6e 64 20 2a 70 50 72 69 6f 72 29 3b 0d 0a 73 74 61  d *pPrior);..sta
12c6f 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 4d 65 6d  tic void *winMem
12c70 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
12c71 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73  rior, int nBytes
12c72 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  );..static int w
12c73 69 6e 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a  inMemSize(void *
12c74 70 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  p);..static int 
12c75 77 69 6e 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e  winMemRoundup(in
12c76 74 20 6e 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e  t n);..static in
12c77 74 20 77 69 6e 4d 65 6d 49 6e 69 74 28 76 6f 69  t winMemInit(voi
12c78 64 20 2a 70 41 70 70 44 61 74 61 29 3b 0d 0a 73  d *pAppData);..s
12c79 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 65  tatic void winMe
12c7a 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a  mShutdown(void *
12c7b 70 41 70 70 44 61 74 61 29 3b 0d 0a 0d 0a 53 51  pAppData);....SQ
12c7c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
12c7d 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
12c7e 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d  ethods *sqlite3M
12c7f 65 6d 47 65 74 57 69 6e 33 32 28 76 6f 69 64 29  emGetWin32(void)
12c80 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  ;..#endif /* SQL
12c81 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43  ITE_WIN32_MALLOC
12c82 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68   */..../*..** Th
12c83 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
12c84 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d 61 6c 6c  able is (normall
12c85 79 29 20 73 65 74 20 6f 6e 63 65 20 61 6e 64 20  y) set once and 
12c86 6e 65 76 65 72 20 63 68 61 6e 67 65 73 0d 0a 2a  never changes..*
12c87 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20 20 49  * thereafter.  I
12c88 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65  t records whethe
12c89 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  r the operating 
12c8a 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 78 0d  system is Win9x.
12c8b 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0d 0a 2a  .** or WinNT...*
12c8c 2a 0d 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61  *..** 0:   Opera
12c8d 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e  ting system unkn
12c8e 6f 77 6e 2e 0d 0a 2a 2a 20 31 3a 20 20 20 4f 70  own...** 1:   Op
12c8f 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
12c90 73 20 57 69 6e 39 78 2e 0d 0a 2a 2a 20 32 3a 20  s Win9x...** 2: 
12c91 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74    Operating syst
12c92 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0d 0a 2a 2a  em is WinNT...**
12c93 0d 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  ..** In order to
12c94 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
12c95 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
12c96 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
12c97 66 69 78 74 75 72 65 0d 0a 2a 2a 20 63 61 6e 20  fixture..** can 
12c98 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69  manually set thi
12c99 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20  s value to 1 to 
12c9a 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65  emulate Win98 be
12c9b 68 61 76 69 6f 72 2e 0d 0a 2a 2f 0d 0a 23 69 66  havior...*/..#if
12c9c 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d  def SQLITE_TEST.
12c9d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12c9e 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20  sqlite3_os_type 
12c9f 3d 20 30 3b 0d 0a 23 65 6c 73 65 0d 0a 73 74 61  = 0;..#else..sta
12ca0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  tic int sqlite3_
12ca1 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0d 0a 23 65  os_type = 0;..#e
12ca2 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  ndif..../*..** M
12ca3 61 6e 79 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  any system calls
12ca4 20 61 72 65 20 61 63 63 65 73 73 65 64 20 74 68   are accessed th
12ca5 72 6f 75 67 68 20 70 6f 69 6e 74 65 72 2d 74 6f  rough pointer-to
12ca6 2d 66 75 6e 63 74 69 6f 6e 73 20 73 6f 20 74 68  -functions so th
12ca7 61 74 0d 0a 2a 2a 20 74 68 65 79 20 6d 61 79 20  at..** they may 
12ca8 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74  be overridden at
12ca9 20 72 75 6e 74 69 6d 65 20 74 6f 20 66 61 63 69   runtime to faci
12caa 6c 69 74 61 74 65 20 66 61 75 6c 74 20 69 6e 6a  litate fault inj
12cab 65 63 74 69 6f 6e 20 64 75 72 69 6e 67 0d 0a 2a  ection during..*
12cac 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 73 61  * testing and sa
12cad 6e 64 62 6f 78 69 6e 67 2e 20 20 54 68 65 20 66  ndboxing.  The f
12cae 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68  ollowing array h
12caf 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61  olds the names a
12cb0 6e 64 20 70 6f 69 6e 74 65 72 73 0d 0a 2a 2a 20  nd pointers..** 
12cb1 74 6f 20 61 6c 6c 20 6f 76 65 72 72 69 64 65 61  to all overridea
12cb2 62 6c 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  ble system calls
12cb3 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 53 51 4c 49  ...*/..#if !SQLI
12cb4 54 45 5f 4f 53 5f 57 49 4e 43 45 0d 0a 23 20 20  TE_OS_WINCE..#  
12cb5 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 49  define SQLITE_WI
12cb6 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0d 0a 23 65  N32_HAS_ANSI..#e
12cb7 6e 64 69 66 0d 0a 0d 0a 23 69 66 20 53 51 4c 49  ndif....#if SQLI
12cb8 54 45 5f 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 53  TE_OS_WINCE || S
12cb9 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 0d 0a  QLITE_OS_WINNT..
12cba 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
12cbb 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 0d  _WIN32_HAS_WIDE.
12cbc 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64  .#endif....#ifnd
12cbd 65 66 20 53 59 53 43 41 4c 4c 0d 0a 23 20 20 64  ef SYSCALL..#  d
12cbe 65 66 69 6e 65 20 53 59 53 43 41 4c 4c 20 73 71  efine SYSCALL sq
12cbf 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
12cc0 72 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66  r..#endif....#if
12cc1 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
12cc2 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 6d  ../*..** These m
12cc3 61 63 72 6f 73 20 61 72 65 20 6e 65 63 65 73 73  acros are necess
12cc4 61 72 79 20 62 65 63 61 75 73 65 20 57 69 6e 64  ary because Wind
12cc5 6f 77 73 20 43 45 20 64 6f 65 73 20 6e 6f 74 20  ows CE does not 
12cc6 6e 61 74 69 76 65 6c 79 20 73 75 70 70 6f 72 74  natively support
12cc7 20 74 68 65 0d 0a 2a 2a 20 57 69 6e 33 32 20 41   the..** Win32 A
12cc8 50 49 73 20 4c 6f 63 6b 46 69 6c 65 2c 20 55 6e  PIs LockFile, Un
12cc9 6c 6f 63 6b 46 69 6c 65 2c 20 61 6e 64 20 4c 6f  lockFile, and Lo
12cca 63 6b 46 69 6c 65 45 78 2e 0d 0a 20 2a 2f 0d 0a  ckFileEx... */..
12ccb 0d 0a 23 20 20 64 65 66 69 6e 65 20 4c 6f 63 6b  ..#  define Lock
12ccc 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20  File(a,b,c,d,e) 
12ccd 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46        winceLockF
12cce 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c  ile(&a, b, c, d,
12ccf 20 65 29 0d 0a 23 20 20 64 65 66 69 6e 65 20 55   e)..#  define U
12cd0 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c  nlockFile(a,b,c,
12cd1 64 2c 65 29 20 20 20 20 20 77 69 6e 63 65 55 6e  d,e)     winceUn
12cd2 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20  lockFile(&a, b, 
12cd3 63 2c 20 64 2c 20 65 29 0d 0a 23 20 20 64 65 66  c, d, e)..#  def
12cd4 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61  ine LockFileEx(a
12cd5 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69  ,b,c,d,e,f)   wi
12cd6 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61  nceLockFileEx(&a
12cd7 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29  , b, c, d, e, f)
12cd8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 73 65  ..../*..** These
12cd9 20 61 72 65 20 74 68 65 20 73 70 65 63 69 61 6c   are the special
12cda 20 73 79 73 63 61 6c 6c 20 68 61 63 6b 73 20 66   syscall hacks f
12cdb 6f 72 20 57 69 6e 64 6f 77 73 20 43 45 2e 20 20  or Windows CE.  
12cdc 54 68 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 61  The locking rela
12cdd 74 65 64 0d 0a 2a 2a 20 64 65 66 69 6e 65 73 20  ted..** defines 
12cde 68 65 72 65 20 72 65 66 65 72 20 74 6f 20 74 68  here refer to th
12cdf 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64  e macros defined
12ce0 20 6a 75 73 74 20 61 62 6f 76 65 2e 0d 0a 20 2a   just above... *
12ce1 2f 0d 0a 0d 0a 23 20 20 64 65 66 69 6e 65 20 6f  /....#  define o
12ce2 73 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49  sAreFileApisANSI
12ce3 28 29 20 20 20 20 20 20 20 31 0d 0a 23 20 20 64  ()       1..#  d
12ce4 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 6c 65  efine osLockFile
12ce5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ce6 4c 6f 63 6b 46 69 6c 65 0d 0a 23 20 20 64 65 66  LockFile..#  def
12ce7 69 6e 65 20 6f 73 55 6e 6c 6f 63 6b 46 69 6c 65  ine osUnlockFile
12ce8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 55 6e                Un
12ce9 6c 6f 63 6b 46 69 6c 65 0d 0a 23 20 20 64 65 66  lockFile..#  def
12cea 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 78  ine osLockFileEx
12ceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 6f                Lo
12cec 63 6b 46 69 6c 65 45 78 0d 0a 23 65 6e 64 69 66  ckFileEx..#endif
12ced 0d 0a 0d 0a 73 74 61 74 69 63 20 73 74 72 75 63  ....static struc
12cee 74 20 77 69 6e 5f 73 79 73 63 61 6c 6c 20 7b 0d  t win_syscall {.
12cef 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
12cf0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
12cf1 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
12cf2 73 79 74 65 6d 20 63 61 6c 6c 20 2a 2f 0d 0a 20  sytem call */.. 
12cf3 20 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c   sqlite3_syscall
12cf4 5f 70 74 72 20 70 43 75 72 72 65 6e 74 3b 20 2f  _ptr pCurrent; /
12cf5 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75 65 20  * Current value 
12cf6 6f 66 20 74 68 65 20 73 79 73 74 65 6d 20 63 61  of the system ca
12cf7 6c 6c 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  ll */..  sqlite3
12cf8 5f 73 79 73 63 61 6c 6c 5f 70 74 72 20 70 44 65  _syscall_ptr pDe
12cf9 66 61 75 6c 74 3b 20 2f 2a 20 44 65 66 61 75 6c  fault; /* Defaul
12cfa 74 20 76 61 6c 75 65 20 2a 2f 0d 0a 7d 20 61 53  t value */..} aS
12cfb 79 73 63 61 6c 6c 5b 5d 20 3d 20 7b 0d 0a 23 69  yscall[] = {..#i
12cfc 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  f !SQLITE_OS_WIN
12cfd 43 45 0d 0a 20 20 7b 20 22 41 72 65 46 69 6c 65  CE..  { "AreFile
12cfe 41 70 69 73 41 4e 53 49 22 2c 20 20 20 20 20 20  ApisANSI",      
12cff 20 20 20 28 53 59 53 43 41 4c 4c 29 41 72 65 46     (SYSCALL)AreF
12d00 69 6c 65 41 70 69 73 41 4e 53 49 2c 20 20 20 20  ileApisANSI,    
12d01 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65       0 },....#de
12d02 66 69 6e 65 20 6f 73 41 72 65 46 69 6c 65 41 70  fine osAreFileAp
12d03 69 73 41 4e 53 49 20 28 28 42 4f 4f 4c 28 57 49  isANSI ((BOOL(WI
12d04 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 79  NAPI*)(VOID))aSy
12d05 73 63 61 6c 6c 5b 30 5d 2e 70 43 75 72 72 65 6e  scall[0].pCurren
12d06 74 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22  t)..#else..  { "
12d07 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 22  AreFileApisANSI"
12d08 2c 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41  ,         (SYSCA
12d09 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
12d0a 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
12d0b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20  ..#endif....#if 
12d0c 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
12d0d 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
12d0e 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45  E_WIN32_HAS_WIDE
12d0f 29 0d 0a 20 20 7b 20 22 43 68 61 72 4c 6f 77 65  )..  { "CharLowe
12d10 72 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  rW",            
12d11 20 20 28 53 59 53 43 41 4c 4c 29 43 68 61 72 4c    (SYSCALL)CharL
12d12 6f 77 65 72 57 2c 20 20 20 20 20 20 20 20 20 20  owerW,          
12d13 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d      0 },..#else.
12d14 0a 20 20 7b 20 22 43 68 61 72 4c 6f 77 65 72 57  .  { "CharLowerW
12d15 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
12d16 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20  (SYSCALL)0,     
12d17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12d18 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a    0 },..#endif..
12d19 0d 0a 23 64 65 66 69 6e 65 20 6f 73 43 68 61 72  ..#define osChar
12d1a 4c 6f 77 65 72 57 20 28 28 4c 50 57 53 54 52 28  LowerW ((LPWSTR(
12d1b 57 49 4e 41 50 49 2a 29 28 4c 50 57 53 54 52 29  WINAPI*)(LPWSTR)
12d1c 29 61 53 79 73 63 61 6c 6c 5b 31 5d 2e 70 43 75  )aSyscall[1].pCu
12d1d 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 53 51  rrent)....#if SQ
12d1e 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
12d1f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
12d20 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0d  WIN32_HAS_WIDE).
12d21 0a 20 20 7b 20 22 43 68 61 72 55 70 70 65 72 57  .  { "CharUpperW
12d22 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
12d23 28 53 59 53 43 41 4c 4c 29 43 68 61 72 55 70 70  (SYSCALL)CharUpp
12d24 65 72 57 2c 20 20 20 20 20 20 20 20 20 20 20 20  erW,            
12d25 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20    0 },..#else.. 
12d26 20 7b 20 22 43 68 61 72 55 70 70 65 72 57 22 2c   { "CharUpperW",
12d27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53                (S
12d28 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20  YSCALL)0,       
12d29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12d2a 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  0 },..#endif....
12d2b 23 64 65 66 69 6e 65 20 6f 73 43 68 61 72 55 70  #define osCharUp
12d2c 70 65 72 57 20 28 28 4c 50 57 53 54 52 28 57 49  perW ((LPWSTR(WI
12d2d 4e 41 50 49 2a 29 28 4c 50 57 53 54 52 29 29 61  NAPI*)(LPWSTR))a
12d2e 53 79 73 63 61 6c 6c 5b 32 5d 2e 70 43 75 72 72  Syscall[2].pCurr
12d2f 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 43 6c 6f  ent)....  { "Clo
12d30 73 65 48 61 6e 64 6c 65 22 2c 20 20 20 20 20 20  seHandle",      
12d31 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
12d32 43 6c 6f 73 65 48 61 6e 64 6c 65 2c 20 20 20 20  CloseHandle,    
12d33 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d           0 },...
12d34 0a 23 64 65 66 69 6e 65 20 6f 73 43 6c 6f 73 65  .#define osClose
12d35 48 61 6e 64 6c 65 20 28 28 42 4f 4f 4c 28 57 49  Handle ((BOOL(WI
12d36 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 29 29 61  NAPI*)(HANDLE))a
12d37 53 79 73 63 61 6c 6c 5b 33 5d 2e 70 43 75 72 72  Syscall[3].pCurr
12d38 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69  ent)....#if defi
12d39 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
12d3a 5f 48 41 53 5f 41 4e 53 49 29 0d 0a 20 20 7b 20  _HAS_ANSI)..  { 
12d3b 22 43 72 65 61 74 65 46 69 6c 65 41 22 2c 20 20  "CreateFileA",  
12d3c 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
12d3d 41 4c 4c 29 43 72 65 61 74 65 46 69 6c 65 41 2c  ALL)CreateFileA,
12d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
12d3f 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 43  ,..#else..  { "C
12d40 72 65 61 74 65 46 69 6c 65 41 22 2c 20 20 20 20  reateFileA",    
12d41 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
12d42 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  L)0,            
12d43 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d             0 },.
12d44 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69  .#endif....#defi
12d45 6e 65 20 6f 73 43 72 65 61 74 65 46 69 6c 65 41  ne osCreateFileA
12d46 20 28 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49   ((HANDLE(WINAPI
12d47 2a 29 28 4c 50 43 53 54 52 2c 44 57 4f 52 44 2c  *)(LPCSTR,DWORD,
12d48 44 57 4f 52 44 2c 20 5c 0d 0a 20 20 20 20 20 20  DWORD, \..      
12d49 20 20 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54    LPSECURITY_ATT
12d4a 52 49 42 55 54 45 53 2c 44 57 4f 52 44 2c 44 57  RIBUTES,DWORD,DW
12d4b 4f 52 44 2c 48 41 4e 44 4c 45 29 29 61 53 79 73  ORD,HANDLE))aSys
12d4c 63 61 6c 6c 5b 34 5d 2e 70 43 75 72 72 65 6e 74  call[4].pCurrent
12d4d 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64  )....#if defined
12d4e 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41  (SQLITE_WIN32_HA
12d4f 53 5f 57 49 44 45 29 0d 0a 20 20 7b 20 22 43 72  S_WIDE)..  { "Cr
12d50 65 61 74 65 46 69 6c 65 57 22 2c 20 20 20 20 20  eateFileW",     
12d51 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c          (SYSCALL
12d52 29 43 72 65 61 74 65 46 69 6c 65 57 2c 20 20 20  )CreateFileW,   
12d53 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a            0 },..
12d54 23 65 6c 73 65 0d 0a 20 20 7b 20 22 43 72 65 61  #else..  { "Crea
12d55 74 65 46 69 6c 65 57 22 2c 20 20 20 20 20 20 20  teFileW",       
12d56 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
12d57 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
12d58 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65          0 },..#e
12d59 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65 20  ndif....#define 
12d5a 6f 73 43 72 65 61 74 65 46 69 6c 65 57 20 28 28  osCreateFileW ((
12d5b 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 28  HANDLE(WINAPI*)(
12d5c 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 44 57  LPCWSTR,DWORD,DW
12d5d 4f 52 44 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20  ORD, \..        
12d5e 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 49  LPSECURITY_ATTRI
12d5f 42 55 54 45 53 2c 44 57 4f 52 44 2c 44 57 4f 52  BUTES,DWORD,DWOR
12d60 44 2c 48 41 4e 44 4c 45 29 29 61 53 79 73 63 61  D,HANDLE))aSysca
12d61 6c 6c 5b 35 5d 2e 70 43 75 72 72 65 6e 74 29 0d  ll[5].pCurrent).
12d62 0a 0d 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69  ...  { "CreateFi
12d63 6c 65 4d 61 70 70 69 6e 67 22 2c 20 20 20 20 20  leMapping",     
12d64 20 20 28 53 59 53 43 41 4c 4c 29 43 72 65 61 74    (SYSCALL)Creat
12d65 65 46 69 6c 65 4d 61 70 70 69 6e 67 2c 20 20 20  eFileMapping,   
12d66 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66      0 },....#def
12d67 69 6e 65 20 6f 73 43 72 65 61 74 65 46 69 6c 65  ine osCreateFile
12d68 4d 61 70 70 69 6e 67 20 28 28 48 41 4e 44 4c 45  Mapping ((HANDLE
12d69 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45  (WINAPI*)(HANDLE
12d6a 2c 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52  ,LPSECURITY_ATTR
12d6b 49 42 55 54 45 53 2c 20 5c 0d 0a 20 20 20 20 20  IBUTES, \..     
12d6c 20 20 20 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44     DWORD,DWORD,D
12d6d 57 4f 52 44 2c 4c 50 43 54 53 54 52 29 29 61 53  WORD,LPCTSTR))aS
12d6e 79 73 63 61 6c 6c 5b 36 5d 2e 70 43 75 72 72 65  yscall[6].pCurre
12d6f 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e  nt)....#if defin
12d70 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  ed(SQLITE_WIN32_
12d71 48 41 53 5f 57 49 44 45 29 0d 0a 20 20 7b 20 22  HAS_WIDE)..  { "
12d72 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
12d73 67 57 22 2c 20 20 20 20 20 20 28 53 59 53 43 41  gW",      (SYSCA
12d74 4c 4c 29 43 72 65 61 74 65 46 69 6c 65 4d 61 70  LL)CreateFileMap
12d75 70 69 6e 67 57 2c 20 20 20 20 20 20 30 20 7d 2c  pingW,      0 },
12d76 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 43 72  ..#else..  { "Cr
12d77 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57  eateFileMappingW
12d78 22 2c 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c  ",      (SYSCALL
12d79 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
12d7a 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a            0 },..
12d7b 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e  #endif....#defin
12d7c 65 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61  e osCreateFileMa
12d7d 70 70 69 6e 67 57 20 28 28 48 41 4e 44 4c 45 28  ppingW ((HANDLE(
12d7e 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c  WINAPI*)(HANDLE,
12d7f 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 49  LPSECURITY_ATTRI
12d80 42 55 54 45 53 2c 20 5c 0d 0a 20 20 20 20 20 20  BUTES, \..      
12d81 20 20 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57    DWORD,DWORD,DW
12d82 4f 52 44 2c 4c 50 43 57 53 54 52 29 29 61 53 79  ORD,LPCWSTR))aSy
12d83 73 63 61 6c 6c 5b 37 5d 2e 70 43 75 72 72 65 6e  scall[7].pCurren
12d84 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  t)....#if define
12d85 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
12d86 41 53 5f 57 49 44 45 29 0d 0a 20 20 7b 20 22 43  AS_WIDE)..  { "C
12d87 72 65 61 74 65 4d 75 74 65 78 57 22 2c 20 20 20  reateMutexW",   
12d88 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
12d89 4c 29 43 72 65 61 74 65 4d 75 74 65 78 57 2c 20  L)CreateMutexW, 
12d8a 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d             0 },.
12d8b 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 43 72 65  .#else..  { "Cre
12d8c 61 74 65 4d 75 74 65 78 57 22 2c 20 20 20 20 20  ateMutexW",     
12d8d 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
12d8e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
12d8f 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23           0 },..#
12d90 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65  endif....#define
12d91 20 6f 73 43 72 65 61 74 65 4d 75 74 65 78 57 20   osCreateMutexW 
12d92 28 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a  ((HANDLE(WINAPI*
12d93 29 28 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54  )(LPSECURITY_ATT
12d94 52 49 42 55 54 45 53 2c 42 4f 4f 4c 2c 20 5c 0d  RIBUTES,BOOL, \.
12d95 0a 20 20 20 20 20 20 20 20 4c 50 43 57 53 54 52  .        LPCWSTR
12d96 29 29 61 53 79 73 63 61 6c 6c 5b 38 5d 2e 70 43  ))aSyscall[8].pC
12d97 75 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64  urrent)....#if d
12d98 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49  efined(SQLITE_WI
12d99 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 0d 0a 20  N32_HAS_ANSI).. 
12d9a 20 7b 20 22 44 65 6c 65 74 65 46 69 6c 65 41 22   { "DeleteFileA"
12d9b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ,             (S
12d9c 59 53 43 41 4c 4c 29 44 65 6c 65 74 65 46 69 6c  YSCALL)DeleteFil
12d9d 65 41 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  eA,             
12d9e 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b  0 },..#else..  {
12d9f 20 22 44 65 6c 65 74 65 46 69 6c 65 41 22 2c 20   "DeleteFileA", 
12da0 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
12da1 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
12da2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
12da3 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64  },..#endif....#d
12da4 65 66 69 6e 65 20 6f 73 44 65 6c 65 74 65 46 69  efine osDeleteFi
12da5 6c 65 41 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50  leA ((BOOL(WINAP
12da6 49 2a 29 28 4c 50 43 53 54 52 29 29 61 53 79 73  I*)(LPCSTR))aSys
12da7 63 61 6c 6c 5b 39 5d 2e 70 43 75 72 72 65 6e 74  call[9].pCurrent
12da8 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64  )....#if defined
12da9 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41  (SQLITE_WIN32_HA
12daa 53 5f 57 49 44 45 29 0d 0a 20 20 7b 20 22 44 65  S_WIDE)..  { "De
12dab 6c 65 74 65 46 69 6c 65 57 22 2c 20 20 20 20 20  leteFileW",     
12dac 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c          (SYSCALL
12dad 29 44 65 6c 65 74 65 46 69 6c 65 57 2c 20 20 20  )DeleteFileW,   
12dae 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a            0 },..
12daf 23 65 6c 73 65 0d 0a 20 20 7b 20 22 44 65 6c 65  #else..  { "Dele
12db0 74 65 46 69 6c 65 57 22 2c 20 20 20 20 20 20 20  teFileW",       
12db1 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
12db2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
12db3 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65          0 },..#e
12db4 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65 20  ndif....#define 
12db5 6f 73 44 65 6c 65 74 65 46 69 6c 65 57 20 28 28  osDeleteFileW ((
12db6 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50  BOOL(WINAPI*)(LP
12db7 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b  CWSTR))aSyscall[
12db8 31 30 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  10].pCurrent)...
12db9 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
12dba 49 4e 43 45 0d 0a 20 20 7b 20 22 46 69 6c 65 54  INCE..  { "FileT
12dbb 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69  imeToLocalFileTi
12dbc 6d 65 22 2c 20 28 53 59 53 43 41 4c 4c 29 46 69  me", (SYSCALL)Fi
12dbd 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c  leTimeToLocalFil
12dbe 65 54 69 6d 65 2c 20 30 20 7d 2c 0d 0a 23 65 6c  eTime, 0 },..#el
12dbf 73 65 0d 0a 20 20 7b 20 22 46 69 6c 65 54 69 6d  se..  { "FileTim
12dc0 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65  eToLocalFileTime
12dc1 22 2c 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20  ", (SYSCALL)0,  
12dc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dc3 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69       0 },..#endi
12dc4 66 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 46  f....#define osF
12dc5 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69  ileTimeToLocalFi
12dc6 6c 65 54 69 6d 65 20 28 28 42 4f 4f 4c 28 57 49  leTime ((BOOL(WI
12dc7 4e 41 50 49 2a 29 28 43 4f 4e 53 54 20 46 49 4c  NAPI*)(CONST FIL
12dc8 45 54 49 4d 45 2a 2c 20 5c 0d 0a 20 20 20 20 20  ETIME*, \..     
12dc9 20 20 20 4c 50 46 49 4c 45 54 49 4d 45 29 29 61     LPFILETIME))a
12dca 53 79 73 63 61 6c 6c 5b 31 31 5d 2e 70 43 75 72  Syscall[11].pCur
12dcb 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 53 51 4c  rent)....#if SQL
12dcc 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0d 0a 20 20  ITE_OS_WINCE..  
12dcd 7b 20 22 46 69 6c 65 54 69 6d 65 54 6f 53 79 73  { "FileTimeToSys
12dce 74 65 6d 54 69 6d 65 22 2c 20 20 20 20 28 53 59  temTime",    (SY
12dcf 53 43 41 4c 4c 29 46 69 6c 65 54 69 6d 65 54 6f  SCALL)FileTimeTo
12dd0 53 79 73 74 65 6d 54 69 6d 65 2c 20 20 20 20 30  SystemTime,    0
12dd1 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20   },..#else..  { 
12dd2 22 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 74 65  "FileTimeToSyste
12dd3 6d 54 69 6d 65 22 2c 20 20 20 20 28 53 59 53 43  mTime",    (SYSC
12dd4 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
12dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
12dd6 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65  ,..#endif....#de
12dd7 66 69 6e 65 20 6f 73 46 69 6c 65 54 69 6d 65 54  fine osFileTimeT
12dd8 6f 53 79 73 74 65 6d 54 69 6d 65 20 28 28 42 4f  oSystemTime ((BO
12dd9 4f 4c 28 57 49 4e 41 50 49 2a 29 28 43 4f 4e 53  OL(WINAPI*)(CONS
12dda 54 20 46 49 4c 45 54 49 4d 45 2a 2c 20 5c 0d 0a  T FILETIME*, \..
12ddb 20 20 20 20 20 20 20 20 4c 50 53 59 53 54 45 4d          LPSYSTEM
12ddc 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 31  TIME))aSyscall[1
12ddd 32 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  2].pCurrent)....
12dde 20 20 7b 20 22 46 6c 75 73 68 46 69 6c 65 42 75    { "FlushFileBu
12ddf 66 66 65 72 73 22 2c 20 20 20 20 20 20 20 20 28  ffers",        (
12de0 53 59 53 43 41 4c 4c 29 46 6c 75 73 68 46 69 6c  SYSCALL)FlushFil
12de1 65 42 75 66 66 65 72 73 2c 20 20 20 20 20 20 20  eBuffers,       
12de2 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65   0 },....#define
12de3 20 6f 73 46 6c 75 73 68 46 69 6c 65 42 75 66 66   osFlushFileBuff
12de4 65 72 73 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50  ers ((BOOL(WINAP
12de5 49 2a 29 28 48 41 4e 44 4c 45 29 29 61 53 79 73  I*)(HANDLE))aSys
12de6 63 61 6c 6c 5b 31 33 5d 2e 70 43 75 72 72 65 6e  call[13].pCurren
12de7 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  t)....#if define
12de8 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
12de9 41 53 5f 41 4e 53 49 29 0d 0a 20 20 7b 20 22 46  AS_ANSI)..  { "F
12dea 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 22 2c 20  ormatMessageA", 
12deb 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
12dec 4c 29 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41  L)FormatMessageA
12ded 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d  ,          0 },.
12dee 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 46 6f 72  .#else..  { "For
12def 6d 61 74 4d 65 73 73 61 67 65 41 22 2c 20 20 20  matMessageA",   
12df0 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
12df1 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
12df2 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23           0 },..#
12df3 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65  endif....#define
12df4 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73 61 67 65   osFormatMessage
12df5 41 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49  A ((DWORD(WINAPI
12df6 2a 29 28 44 57 4f 52 44 2c 4c 50 43 56 4f 49 44  *)(DWORD,LPCVOID
12df7 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 4c 50 53  ,DWORD,DWORD,LPS
12df8 54 52 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 44  TR, \..        D
12df9 57 4f 52 44 2c 76 61 5f 6c 69 73 74 2a 29 29 61  WORD,va_list*))a
12dfa 53 79 73 63 61 6c 6c 5b 31 34 5d 2e 70 43 75 72  Syscall[14].pCur
12dfb 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65 66  rent)....#if def
12dfc 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33  ined(SQLITE_WIN3
12dfd 32 5f 48 41 53 5f 57 49 44 45 29 0d 0a 20 20 7b  2_HAS_WIDE)..  {
12dfe 20 22 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57   "FormatMessageW
12dff 22 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 53  ",          (SYS
12e00 43 41 4c 4c 29 46 6f 72 6d 61 74 4d 65 73 73 61  CALL)FormatMessa
12e01 67 65 57 2c 20 20 20 20 20 20 20 20 20 20 30 20  geW,          0 
12e02 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22  },..#else..  { "
12e03 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 22 2c  FormatMessageW",
12e04 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
12e05 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
12e06 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
12e07 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66  ..#endif....#def
12e08 69 6e 65 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73  ine osFormatMess
12e09 61 67 65 57 20 28 28 44 57 4f 52 44 28 57 49 4e  ageW ((DWORD(WIN
12e0a 41 50 49 2a 29 28 44 57 4f 52 44 2c 4c 50 43 56  API*)(DWORD,LPCV
12e0b 4f 49 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c  OID,DWORD,DWORD,
12e0c 4c 50 57 53 54 52 2c 20 5c 0d 0a 20 20 20 20 20  LPWSTR, \..     
12e0d 20 20 20 44 57 4f 52 44 2c 76 61 5f 6c 69 73 74     DWORD,va_list
12e0e 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 35 5d 2e  *))aSyscall[15].
12e0f 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b  pCurrent)....  {
12e10 20 22 46 72 65 65 4c 69 62 72 61 72 79 22 2c 20   "FreeLibrary", 
12e11 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
12e12 43 41 4c 4c 29 46 72 65 65 4c 69 62 72 61 72 79  CALL)FreeLibrary
12e13 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20  ,             0 
12e14 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73  },....#define os
12e15 46 72 65 65 4c 69 62 72 61 72 79 20 28 28 42 4f  FreeLibrary ((BO
12e16 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 4d 4f 44  OL(WINAPI*)(HMOD
12e17 55 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 36  ULE))aSyscall[16
12e18 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20  ].pCurrent).... 
12e19 20 7b 20 22 47 65 74 43 75 72 72 65 6e 74 50 72   { "GetCurrentPr
12e1a 6f 63 65 73 73 49 64 22 2c 20 20 20 20 20 28 53  ocessId",     (S
12e1b 59 53 43 41 4c 4c 29 47 65 74 43 75 72 72 65 6e  YSCALL)GetCurren
12e1c 74 50 72 6f 63 65 73 73 49 64 2c 20 20 20 20 20  tProcessId,     
12e1d 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20  0 },....#define 
12e1e 6f 73 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63  osGetCurrentProc
12e1f 65 73 73 49 64 20 28 28 44 57 4f 52 44 28 57 49  essId ((DWORD(WI
12e20 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 79  NAPI*)(VOID))aSy
12e21 73 63 61 6c 6c 5b 31 37 5d 2e 70 43 75 72 72 65  scall[17].pCurre
12e22 6e 74 29 0d 0a 0d 0a 23 69 66 20 21 53 51 4c 49  nt)....#if !SQLI
12e23 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 64  TE_OS_WINCE && d
12e24 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49  efined(SQLITE_WI
12e25 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 0d 0a 20  N32_HAS_ANSI).. 
12e26 20 7b 20 22 47 65 74 44 69 73 6b 46 72 65 65 53   { "GetDiskFreeS
12e27 70 61 63 65 41 22 2c 20 20 20 20 20 20 20 28 53  paceA",       (S
12e28 59 53 43 41 4c 4c 29 47 65 74 44 69 73 6b 46 72  YSCALL)GetDiskFr
12e29 65 65 53 70 61 63 65 41 2c 20 20 20 20 20 20 20  eeSpaceA,       
12e2a 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b  0 },..#else..  {
12e2b 20 22 47 65 74 44 69 73 6b 46 72 65 65 53 70 61   "GetDiskFreeSpa
12e2c 63 65 41 22 2c 20 20 20 20 20 20 20 28 53 59 53  ceA",       (SYS
12e2d 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
12e2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
12e2f 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64  },..#endif....#d
12e30 65 66 69 6e 65 20 6f 73 47 65 74 44 69 73 6b 46  efine osGetDiskF
12e31 72 65 65 53 70 61 63 65 41 20 28 28 42 4f 4f 4c  reeSpaceA ((BOOL
12e32 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 54 52  (WINAPI*)(LPCSTR
12e33 2c 4c 50 44 57 4f 52 44 2c 4c 50 44 57 4f 52 44  ,LPDWORD,LPDWORD
12e34 2c 4c 50 44 57 4f 52 44 2c 20 5c 0d 0a 20 20 20  ,LPDWORD, \..   
12e35 20 20 20 20 20 4c 50 44 57 4f 52 44 29 29 61 53       LPDWORD))aS
12e36 79 73 63 61 6c 6c 5b 31 38 5d 2e 70 43 75 72 72  yscall[18].pCurr
12e37 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 21 53 51 4c  ent)....#if !SQL
12e38 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20  ITE_OS_WINCE && 
12e39 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
12e3a 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0d 0a  IN32_HAS_WIDE)..
12e3b 20 20 7b 20 22 47 65 74 44 69 73 6b 46 72 65 65    { "GetDiskFree
12e3c 53 70 61 63 65 57 22 2c 20 20 20 20 20 20 20 28  SpaceW",       (
12e3d 53 59 53 43 41 4c 4c 29 47 65 74 44 69 73 6b 46  SYSCALL)GetDiskF
12e3e 72 65 65 53 70 61 63 65 57 2c 20 20 20 20 20 20  reeSpaceW,      
12e3f 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20   0 },..#else..  
12e40 7b 20 22 47 65 74 44 69 73 6b 46 72 65 65 53 70  { "GetDiskFreeSp
12e41 61 63 65 57 22 2c 20 20 20 20 20 20 20 28 53 59  aceW",       (SY
12e42 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
12e43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
12e44 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23   },..#endif....#
12e45 64 65 66 69 6e 65 20 6f 73 47 65 74 44 69 73 6b  define osGetDisk
12e46 46 72 65 65 53 70 61 63 65 57 20 28 28 42 4f 4f  FreeSpaceW ((BOO
12e47 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 57 53  L(WINAPI*)(LPCWS
12e48 54 52 2c 4c 50 44 57 4f 52 44 2c 4c 50 44 57 4f  TR,LPDWORD,LPDWO
12e49 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0d 0a 20  RD,LPDWORD, \.. 
12e4a 20 20 20 20 20 20 20 4c 50 44 57 4f 52 44 29 29         LPDWORD))
12e4b 61 53 79 73 63 61 6c 6c 5b 31 39 5d 2e 70 43 75  aSyscall[19].pCu
12e4c 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 64 65  rrent)....#if de
12e4d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e  fined(SQLITE_WIN
12e4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 0d 0a 20 20  32_HAS_ANSI)..  
12e4f 7b 20 22 47 65 74 46 69 6c 65 41 74 74 72 69 62  { "GetFileAttrib
12e50 75 74 65 73 41 22 2c 20 20 20 20 20 20 28 53 59  utesA",      (SY
12e51 53 43 41 4c 4c 29 47 65 74 46 69 6c 65 41 74 74  SCALL)GetFileAtt
12e52 72 69 62 75 74 65 73 41 2c 20 20 20 20 20 20 30  ributesA,      0
12e53 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20   },..#else..  { 
12e54 22 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74  "GetFileAttribut
12e55 65 73 41 22 2c 20 20 20 20 20 20 28 53 59 53 43  esA",      (SYSC
12e56 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
12e57 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
12e58 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65  ,..#endif....#de
12e59 66 69 6e 65 20 6f 73 47 65 74 46 69 6c 65 41 74  fine osGetFileAt
12e5a 74 72 69 62 75 74 65 73 41 20 28 28 44 57 4f 52  tributesA ((DWOR
12e5b 44 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 54  D(WINAPI*)(LPCST
12e5c 52 29 29 61 53 79 73 63 61 6c 6c 5b 32 30 5d 2e  R))aSyscall[20].
12e5d 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66  pCurrent)....#if
12e5e 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
12e5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0d  WIN32_HAS_WIDE).
12e60 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 41 74 74  .  { "GetFileAtt
12e61 72 69 62 75 74 65 73 57 22 2c 20 20 20 20 20 20  ributesW",      
12e62 28 53 59 53 43 41 4c 4c 29 47 65 74 46 69 6c 65  (SYSCALL)GetFile
12e63 41 74 74 72 69 62 75 74 65 73 57 2c 20 20 20 20  AttributesW,    
12e64 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a 20    0 },..#else.. 
12e65 20 7b 20 22 47 65 74 46 69 6c 65 41 74 74 72 69   { "GetFileAttri
12e66 62 75 74 65 73 57 22 2c 20 20 20 20 20 20 28 53  butesW",      (S
12e67 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20  YSCALL)0,       
12e68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e69 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  0 },..#endif....
12e6a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 46 69 6c  #define osGetFil
12e6b 65 41 74 74 72 69 62 75 74 65 73 57 20 28 28 44  eAttributesW ((D
12e6c 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 4c 50  WORD(WINAPI*)(LP
12e6d 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b  CWSTR))aSyscall[
12e6e 32 31 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  21].pCurrent)...
12e6f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
12e70 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49  ITE_WIN32_HAS_WI
12e71 44 45 29 0d 0a 20 20 7b 20 22 47 65 74 46 69 6c  DE)..  { "GetFil
12e72 65 41 74 74 72 69 62 75 74 65 73 45 78 57 22 2c  eAttributesExW",
12e73 20 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74      (SYSCALL)Get
12e74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 45 78  FileAttributesEx
12e75 57 2c 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73  W,    0 },..#els
12e76 65 0d 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 41  e..  { "GetFileA
12e77 74 74 72 69 62 75 74 65 73 45 78 57 22 2c 20 20  ttributesExW",  
12e78 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20    (SYSCALL)0,   
12e79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e7a 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66      0 },..#endif
12e7b 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 47 65  ....#define osGe
12e7c 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 45  tFileAttributesE
12e7d 78 57 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49  xW ((BOOL(WINAPI
12e7e 2a 29 28 4c 50 43 57 53 54 52 2c 47 45 54 5f 46  *)(LPCWSTR,GET_F
12e7f 49 4c 45 45 58 5f 49 4e 46 4f 5f 4c 45 56 45 4c  ILEEX_INFO_LEVEL
12e80 53 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50  S, \..        LP
12e81 56 4f 49 44 29 29 61 53 79 73 63 61 6c 6c 5b 32  VOID))aSyscall[2
12e82 32 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  2].pCurrent)....
12e83 20 20 7b 20 22 47 65 74 46 69 6c 65 53 69 7a 65    { "GetFileSize
12e84 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28  ",             (
12e85 53 59 53 43 41 4c 4c 29 47 65 74 46 69 6c 65 53  SYSCALL)GetFileS
12e86 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ize,            
12e87 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65   0 },....#define
12e88 20 6f 73 47 65 74 46 69 6c 65 53 69 7a 65 20 28   osGetFileSize (
12e89 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28  (DWORD(WINAPI*)(
12e8a 48 41 4e 44 4c 45 2c 4c 50 44 57 4f 52 44 29 29  HANDLE,LPDWORD))
12e8b 61 53 79 73 63 61 6c 6c 5b 32 33 5d 2e 70 43 75  aSyscall[23].pCu
12e8c 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 21 53  rrent)....#if !S
12e8d 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26  QLITE_OS_WINCE &
12e8e 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
12e8f 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29  _WIN32_HAS_ANSI)
12e90 0d 0a 20 20 7b 20 22 47 65 74 46 75 6c 6c 50 61  ..  { "GetFullPa
12e91 74 68 4e 61 6d 65 41 22 2c 20 20 20 20 20 20 20  thNameA",       
12e92 20 28 53 59 53 43 41 4c 4c 29 47 65 74 46 75 6c   (SYSCALL)GetFul
12e93 6c 50 61 74 68 4e 61 6d 65 41 2c 20 20 20 20 20  lPathNameA,     
12e94 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a     0 },..#else..
12e95 20 20 7b 20 22 47 65 74 46 75 6c 6c 50 61 74 68    { "GetFullPath
12e96 4e 61 6d 65 41 22 2c 20 20 20 20 20 20 20 20 28  NameA",        (
12e97 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20  SYSCALL)0,      
12e98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e99 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d   0 },..#endif...
12e9a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 46 75  .#define osGetFu
12e9b 6c 6c 50 61 74 68 4e 61 6d 65 41 20 28 28 44 57  llPathNameA ((DW
12e9c 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 4c 50 43  ORD(WINAPI*)(LPC
12e9d 53 54 52 2c 44 57 4f 52 44 2c 4c 50 53 54 52 2c  STR,DWORD,LPSTR,
12e9e 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50 53 54   \..        LPST
12e9f 52 2a 29 29 61 53 79 73 63 61 6c 6c 5b 32 34 5d  R*))aSyscall[24]
12ea0 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 23 69  .pCurrent)....#i
12ea1 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  f !SQLITE_OS_WIN
12ea2 43 45 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  CE && defined(SQ
12ea3 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57  LITE_WIN32_HAS_W
12ea4 49 44 45 29 0d 0a 20 20 7b 20 22 47 65 74 46 75  IDE)..  { "GetFu
12ea5 6c 6c 50 61 74 68 4e 61 6d 65 57 22 2c 20 20 20  llPathNameW",   
12ea6 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65       (SYSCALL)Ge
12ea7 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 2c 20  tFullPathNameW, 
12ea8 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c         0 },..#el
12ea9 73 65 0d 0a 20 20 7b 20 22 47 65 74 46 75 6c 6c  se..  { "GetFull
12eaa 50 61 74 68 4e 61 6d 65 57 22 2c 20 20 20 20 20  PathNameW",     
12eab 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
12eac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ead 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69       0 },..#endi
12eae 66 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 47  f....#define osG
12eaf 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 20  etFullPathNameW 
12eb0 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
12eb1 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 4c  (LPCWSTR,DWORD,L
12eb2 50 57 53 54 52 2c 20 5c 0d 0a 20 20 20 20 20 20  PWSTR, \..      
12eb3 20 20 4c 50 57 53 54 52 2a 29 29 61 53 79 73 63    LPWSTR*))aSysc
12eb4 61 6c 6c 5b 32 35 5d 2e 70 43 75 72 72 65 6e 74  all[25].pCurrent
12eb5 29 0d 0a 0d 0a 20 20 7b 20 22 47 65 74 4c 61 73  )....  { "GetLas
12eb6 74 45 72 72 6f 72 22 2c 20 20 20 20 20 20 20 20  tError",        
12eb7 20 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74      (SYSCALL)Get
12eb8 4c 61 73 74 45 72 72 6f 72 2c 20 20 20 20 20 20  LastError,      
12eb9 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64        0 },....#d
12eba 65 66 69 6e 65 20 6f 73 47 65 74 4c 61 73 74 45  efine osGetLastE
12ebb 72 72 6f 72 20 28 28 44 57 4f 52 44 28 57 49 4e  rror ((DWORD(WIN
12ebc 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 79 73  API*)(VOID))aSys
12ebd 63 61 6c 6c 5b 32 36 5d 2e 70 43 75 72 72 65 6e  call[26].pCurren
12ebe 74 29 0d 0a 0d 0a 23 69 66 20 53 51 4c 49 54 45  t)....#if SQLITE
12ebf 5f 4f 53 5f 57 49 4e 43 45 0d 0a 20 20 2f 2a 20  _OS_WINCE..  /* 
12ec0 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72 65  The GetProcAddre
12ec1 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ssA() routine is
12ec2 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
12ec3 6f 6e 20 57 69 6e 64 6f 77 73 20 43 45 2e 20 2a  on Windows CE. *
12ec4 2f 0d 0a 20 20 7b 20 22 47 65 74 50 72 6f 63 41  /..  { "GetProcA
12ec5 64 64 72 65 73 73 41 22 2c 20 20 20 20 20 20 20  ddressA",       
12ec6 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 50 72    (SYSCALL)GetPr
12ec7 6f 63 41 64 64 72 65 73 73 41 2c 20 20 20 20 20  ocAddressA,     
12ec8 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d      0 },..#else.
12ec9 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20  .  /* All other 
12eca 57 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d  Windows platform
12ecb 73 20 65 78 70 65 63 74 20 47 65 74 50 72 6f 63  s expect GetProc
12ecc 41 64 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b  Address() to tak
12ecd 65 0d 0a 20 20 2a 2a 20 61 6e 20 41 4e 53 49 20  e..  ** an ANSI 
12ece 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c 65 73  string regardles
12ecf 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f 44  s of the _UNICOD
12ed0 45 20 73 65 74 74 69 6e 67 20 2a 2f 0d 0a 20 20  E setting */..  
12ed1 7b 20 22 47 65 74 50 72 6f 63 41 64 64 72 65 73  { "GetProcAddres
12ed2 73 41 22 2c 20 20 20 20 20 20 20 20 20 28 53 59  sA",         (SY
12ed3 53 43 41 4c 4c 29 47 65 74 50 72 6f 63 41 64 64  SCALL)GetProcAdd
12ed4 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 30  ress,          0
12ed5 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23   },..#endif....#
12ed6 64 65 66 69 6e 65 20 6f 73 47 65 74 50 72 6f 63  define osGetProc
12ed7 41 64 64 72 65 73 73 41 20 28 28 46 41 52 50 52  AddressA ((FARPR
12ed8 4f 43 28 57 49 4e 41 50 49 2a 29 28 48 4d 4f 44  OC(WINAPI*)(HMOD
12ed9 55 4c 45 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20  ULE, \..        
12eda 4c 50 43 53 54 52 29 29 61 53 79 73 63 61 6c 6c  LPCSTR))aSyscall
12edb 5b 32 37 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a  [27].pCurrent)..
12edc 0d 0a 20 20 7b 20 22 47 65 74 53 79 73 74 65 6d  ..  { "GetSystem
12edd 49 6e 66 6f 22 2c 20 20 20 20 20 20 20 20 20 20  Info",          
12ede 20 28 53 59 53 43 41 4c 4c 29 47 65 74 53 79 73   (SYSCALL)GetSys
12edf 74 65 6d 49 6e 66 6f 2c 20 20 20 20 20 20 20 20  temInfo,        
12ee0 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69     0 },....#defi
12ee1 6e 65 20 6f 73 47 65 74 53 79 73 74 65 6d 49 6e  ne osGetSystemIn
12ee2 66 6f 20 28 28 56 4f 49 44 28 57 49 4e 41 50 49  fo ((VOID(WINAPI
12ee3 2a 29 28 4c 50 53 59 53 54 45 4d 5f 49 4e 46 4f  *)(LPSYSTEM_INFO
12ee4 29 29 61 53 79 73 63 61 6c 6c 5b 32 38 5d 2e 70  ))aSyscall[28].p
12ee5 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20  Current)....  { 
12ee6 22 47 65 74 53 79 73 74 65 6d 54 69 6d 65 22 2c  "GetSystemTime",
12ee7 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
12ee8 41 4c 4c 29 47 65 74 53 79 73 74 65 6d 54 69 6d  ALL)GetSystemTim
12ee9 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 20 7d  e,           0 }
12eea 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 47  ,....#define osG
12eeb 65 74 53 79 73 74 65 6d 54 69 6d 65 20 28 28 56  etSystemTime ((V
12eec 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 4c 50 53  OID(WINAPI*)(LPS
12eed 59 53 54 45 4d 54 49 4d 45 29 29 61 53 79 73 63  YSTEMTIME))aSysc
12eee 61 6c 6c 5b 32 39 5d 2e 70 43 75 72 72 65 6e 74  all[29].pCurrent
12eef 29 0d 0a 0d 0a 23 69 66 20 21 53 51 4c 49 54 45  )....#if !SQLITE
12ef0 5f 4f 53 5f 57 49 4e 43 45 0d 0a 20 20 7b 20 22  _OS_WINCE..  { "
12ef1 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 46  GetSystemTimeAsF
12ef2 69 6c 65 54 69 6d 65 22 2c 20 28 53 59 53 43 41  ileTime", (SYSCA
12ef3 4c 4c 29 47 65 74 53 79 73 74 65 6d 54 69 6d 65  LL)GetSystemTime
12ef4 41 73 46 69 6c 65 54 69 6d 65 2c 20 30 20 7d 2c  AsFileTime, 0 },
12ef5 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 47 65  ..#else..  { "Ge
12ef6 74 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c  tSystemTimeAsFil
12ef7 65 54 69 6d 65 22 2c 20 28 53 59 53 43 41 4c 4c  eTime", (SYSCALL
12ef8 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
12ef9 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a            0 },..
12efa 23 65 6e 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e  #endif....#defin
12efb 65 20 6f 73 47 65 74 53 79 73 74 65 6d 54 69 6d  e osGetSystemTim
12efc 65 41 73 46 69 6c 65 54 69 6d 65 20 28 28 56 4f  eAsFileTime ((VO
12efd 49 44 28 57 49 4e 41 50 49 2a 29 28 20 5c 0d 0a  ID(WINAPI*)( \..
12efe 20 20 20 20 20 20 20 20 4c 50 46 49 4c 45 54 49          LPFILETI
12eff 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 33 30 5d  ME))aSyscall[30]
12f00 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 23 69  .pCurrent)....#i
12f01 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
12f02 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29  _WIN32_HAS_ANSI)
12f03 0d 0a 20 20 7b 20 22 47 65 74 54 65 6d 70 50 61  ..  { "GetTempPa
12f04 74 68 41 22 2c 20 20 20 20 20 20 20 20 20 20 20  thA",           
12f05 20 28 53 59 53 43 41 4c 4c 29 47 65 74 54 65 6d   (SYSCALL)GetTem
12f06 70 50 61 74 68 41 2c 20 20 20 20 20 20 20 20 20  pPathA,         
12f07 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65 0d 0a     0 },..#else..
12f08 20 20 7b 20 22 47 65 74 54 65 6d 70 50 61 74 68    { "GetTempPath
12f09 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28  A",            (
12f0a 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20  SYSCALL)0,      
12f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f0c 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d   0 },..#endif...
12f0d 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 54 65  .#define osGetTe
12f0e 6d 70 50 61 74 68 41 20 28 28 44 57 4f 52 44 28  mpPathA ((DWORD(
12f0f 57 49 4e 41 50 49 2a 29 28 44 57 4f 52 44 2c 4c  WINAPI*)(DWORD,L
12f10 50 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 33  PSTR))aSyscall[3
12f11 31 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  1].pCurrent)....
12f12 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
12f13 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44  TE_WIN32_HAS_WID
12f14 45 29 0d 0a 20 20 7b 20 22 47 65 74 54 65 6d 70  E)..  { "GetTemp
12f15 50 61 74 68 57 22 2c 20 20 20 20 20 20 20 20 20  PathW",         
12f16 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 54     (SYSCALL)GetT
12f17 65 6d 70 50 61 74 68 57 2c 20 20 20 20 20 20 20  empPathW,       
12f18 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65       0 },..#else
12f19 0d 0a 20 20 7b 20 22 47 65 74 54 65 6d 70 50 61  ..  { "GetTempPa
12f1a 74 68 57 22 2c 20 20 20 20 20 20 20 20 20 20 20  thW",           
12f1b 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20   (SYSCALL)0,    
12f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f1d 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d     0 },..#endif.
12f1e 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74  ...#define osGet
12f1f 54 65 6d 70 50 61 74 68 57 20 28 28 44 57 4f 52  TempPathW ((DWOR
12f20 44 28 57 49 4e 41 50 49 2a 29 28 44 57 4f 52 44  D(WINAPI*)(DWORD
12f21 2c 4c 50 57 53 54 52 29 29 61 53 79 73 63 61 6c  ,LPWSTR))aSyscal
12f22 6c 5b 33 32 5d 2e 70 43 75 72 72 65 6e 74 29 0d  l[32].pCurrent).
12f23 0a 0d 0a 20 20 7b 20 22 47 65 74 54 69 63 6b 43  ...  { "GetTickC
12f24 6f 75 6e 74 22 2c 20 20 20 20 20 20 20 20 20 20  ount",          
12f25 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 54 69    (SYSCALL)GetTi
12f26 63 6b 43 6f 75 6e 74 2c 20 20 20 20 20 20 20 20  ckCount,        
12f27 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66      0 },....#def
12f28 69 6e 65 20 6f 73 47 65 74 54 69 63 6b 43 6f 75  ine osGetTickCou
12f29 6e 74 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50  nt ((DWORD(WINAP
12f2a 49 2a 29 28 56 4f 49 44 29 29 61 53 79 73 63 61  I*)(VOID))aSysca
12f2b 6c 6c 5b 33 33 5d 2e 70 43 75 72 72 65 6e 74 29  ll[33].pCurrent)
12f2c 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
12f2d 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53  SQLITE_WIN32_HAS
12f2e 5f 41 4e 53 49 29 0d 0a 20 20 7b 20 22 47 65 74  _ANSI)..  { "Get
12f2f 56 65 72 73 69 6f 6e 45 78 41 22 2c 20 20 20 20  VersionExA",    
12f30 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
12f31 47 65 74 56 65 72 73 69 6f 6e 45 78 41 2c 20 20  GetVersionExA,  
12f32 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23           0 },..#
12f33 65 6c 73 65 0d 0a 20 20 7b 20 22 47 65 74 56 65  else..  { "GetVe
12f34 72 73 69 6f 6e 45 78 41 22 2c 20 20 20 20 20 20  rsionExA",      
12f35 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c       (SYSCALL)0,
12f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f37 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e         0 },..#en
12f38 64 69 66 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f  dif....#define o
12f39 73 47 65 74 56 65 72 73 69 6f 6e 45 78 41 20 28  sGetVersionExA (
12f3a 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 20  (BOOL(WINAPI*)( 
12f3b 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50 4f 53 56  \..        LPOSV
12f3c 45 52 53 49 4f 4e 49 4e 46 4f 41 29 29 61 53 79  ERSIONINFOA))aSy
12f3d 73 63 61 6c 6c 5b 33 34 5d 2e 70 43 75 72 72 65  scall[34].pCurre
12f3e 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 48 65 61 70  nt)....  { "Heap
12f3f 41 6c 6c 6f 63 22 2c 20 20 20 20 20 20 20 20 20  Alloc",         
12f40 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48        (SYSCALL)H
12f41 65 61 70 41 6c 6c 6f 63 2c 20 20 20 20 20 20 20  eapAlloc,       
12f42 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a          0 },....
12f43 23 64 65 66 69 6e 65 20 6f 73 48 65 61 70 41 6c  #define osHeapAl
12f44 6c 6f 63 20 28 28 4c 50 56 4f 49 44 28 57 49 4e  loc ((LPVOID(WIN
12f45 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f  API*)(HANDLE,DWO
12f46 52 44 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 53  RD, \..        S
12f47 49 5a 45 5f 54 29 29 61 53 79 73 63 61 6c 6c 5b  IZE_T))aSyscall[
12f48 33 35 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  35].pCurrent)...
12f49 0a 20 20 7b 20 22 48 65 61 70 43 72 65 61 74 65  .  { "HeapCreate
12f4a 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
12f4b 28 53 59 53 43 41 4c 4c 29 48 65 61 70 43 72 65  (SYSCALL)HeapCre
12f4c 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
12f4d 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e    0 },....#defin
12f4e 65 20 6f 73 48 65 61 70 43 72 65 61 74 65 20 28  e osHeapCreate (
12f4f 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29  (HANDLE(WINAPI*)
12f50 28 44 57 4f 52 44 2c 53 49 5a 45 5f 54 2c 20 5c  (DWORD,SIZE_T, \
12f51 0d 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f 54  ..        SIZE_T
12f52 29 29 61 53 79 73 63 61 6c 6c 5b 33 36 5d 2e 70  ))aSyscall[36].p
12f53 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20  Current)....  { 
12f54 22 48 65 61 70 44 65 73 74 72 6f 79 22 2c 20 20  "HeapDestroy",  
12f55 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
12f56 41 4c 4c 29 48 65 61 70 44 65 73 74 72 6f 79 2c  ALL)HeapDestroy,
12f57 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
12f58 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 48  ,....#define osH
12f59 65 61 70 44 65 73 74 72 6f 79 20 28 28 42 4f 4f  eapDestroy ((BOO
12f5a 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c  L(WINAPI*)(HANDL
12f5b 45 29 29 61 53 79 73 63 61 6c 6c 5b 33 37 5d 2e  E))aSyscall[37].
12f5c 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b  pCurrent)....  {
12f5d 20 22 48 65 61 70 46 72 65 65 22 2c 20 20 20 20   "HeapFree",    
12f5e 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
12f5f 43 41 4c 4c 29 48 65 61 70 46 72 65 65 2c 20 20  CALL)HeapFree,  
12f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
12f61 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73  },....#define os
12f62 48 65 61 70 46 72 65 65 20 28 28 42 4f 4f 4c 28  HeapFree ((BOOL(
12f63 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c  WINAPI*)(HANDLE,
12f64 44 57 4f 52 44 2c 4c 50 56 4f 49 44 29 29 61 53  DWORD,LPVOID))aS
12f65 79 73 63 61 6c 6c 5b 33 38 5d 2e 70 43 75 72 72  yscall[38].pCurr
12f66 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 48 65 61  ent)....  { "Hea
12f67 70 52 65 41 6c 6c 6f 63 22 2c 20 20 20 20 20 20  pReAlloc",      
12f68 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
12f69 48 65 61 70 52 65 41 6c 6c 6f 63 2c 20 20 20 20  HeapReAlloc,    
12f6a 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d           0 },...
12f6b 0a 23 64 65 66 69 6e 65 20 6f 73 48 65 61 70 52  .#define osHeapR
12f6c 65 41 6c 6c 6f 63 20 28 28 4c 50 56 4f 49 44 28  eAlloc ((LPVOID(
12f6d 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c  WINAPI*)(HANDLE,
12f6e 44 57 4f 52 44 2c 4c 50 56 4f 49 44 2c 20 5c 0d  DWORD,LPVOID, \.
12f6f 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f 54 29  .        SIZE_T)
12f70 29 61 53 79 73 63 61 6c 6c 5b 33 39 5d 2e 70 43  )aSyscall[39].pC
12f71 75 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22  urrent)....  { "
12f72 48 65 61 70 53 69 7a 65 22 2c 20 20 20 20 20 20  HeapSize",      
12f73 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
12f74 4c 4c 29 48 65 61 70 53 69 7a 65 2c 20 20 20 20  LL)HeapSize,    
12f75 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
12f76 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 48 65  ....#define osHe
12f77 61 70 53 69 7a 65 20 28 28 53 49 5a 45 5f 54 28  apSize ((SIZE_T(
12f78 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c  WINAPI*)(HANDLE,
12f79 44 57 4f 52 44 2c 20 5c 0d 0a 20 20 20 20 20 20  DWORD, \..      
12f7a 20 20 4c 50 43 56 4f 49 44 29 29 61 53 79 73 63    LPCVOID))aSysc
12f7b 61 6c 6c 5b 34 30 5d 2e 70 43 75 72 72 65 6e 74  all[40].pCurrent
12f7c 29 0d 0a 0d 0a 20 20 7b 20 22 48 65 61 70 56 61  )....  { "HeapVa
12f7d 6c 69 64 61 74 65 22 2c 20 20 20 20 20 20 20 20  lidate",        
12f7e 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48 65 61      (SYSCALL)Hea
12f7f 70 56 61 6c 69 64 61 74 65 2c 20 20 20 20 20 20  pValidate,      
12f80 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64        0 },....#d
12f81 65 66 69 6e 65 20 6f 73 48 65 61 70 56 61 6c 69  efine osHeapVali
12f82 64 61 74 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41  date ((BOOL(WINA
12f83 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52  PI*)(HANDLE,DWOR
12f84 44 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50  D, \..        LP
12f85 43 56 4f 49 44 29 29 61 53 79 73 63 61 6c 6c 5b  CVOID))aSyscall[
12f86 34 31 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  41].pCurrent)...
12f87 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
12f88 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e  ITE_WIN32_HAS_AN
12f89 53 49 29 0d 0a 20 20 7b 20 22 4c 6f 61 64 4c 69  SI)..  { "LoadLi
12f8a 62 72 61 72 79 41 22 2c 20 20 20 20 20 20 20 20  braryA",        
12f8b 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4c 6f 61      (SYSCALL)Loa
12f8c 64 4c 69 62 72 61 72 79 41 2c 20 20 20 20 20 20  dLibraryA,      
12f8d 20 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73        0 },..#els
12f8e 65 0d 0a 20 20 7b 20 22 4c 6f 61 64 4c 69 62 72  e..  { "LoadLibr
12f8f 61 72 79 41 22 2c 20 20 20 20 20 20 20 20 20 20  aryA",          
12f90 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20    (SYSCALL)0,   
12f91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f92 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66      0 },..#endif
12f93 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 4c 6f  ....#define osLo
12f94 61 64 4c 69 62 72 61 72 79 41 20 28 28 48 4d 4f  adLibraryA ((HMO
12f95 44 55 4c 45 28 57 49 4e 41 50 49 2a 29 28 4c 50  DULE(WINAPI*)(LP
12f96 43 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 34  CSTR))aSyscall[4
12f97 32 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  2].pCurrent)....
12f98 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
12f99 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44  TE_WIN32_HAS_WID
12f9a 45 29 0d 0a 20 20 7b 20 22 4c 6f 61 64 4c 69 62  E)..  { "LoadLib
12f9b 72 61 72 79 57 22 2c 20 20 20 20 20 20 20 20 20  raryW",         
12f9c 20 20 20 28 53 59 53 43 41 4c 4c 29 4c 6f 61 64     (SYSCALL)Load
12f9d 4c 69 62 72 61 72 79 57 2c 20 20 20 20 20 20 20  LibraryW,       
12f9e 20 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6c 73 65       0 },..#else
12f9f 0d 0a 20 20 7b 20 22 4c 6f 61 64 4c 69 62 72 61  ..  { "LoadLibra
12fa0 72 79 57 22 2c 20 20 20 20 20 20 20 20 20 20 20  ryW",           
12fa1 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20   (SYSCALL)0,    
12fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fa3 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d     0 },..#endif.
12fa4 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 4c 6f 61  ...#define osLoa
12fa5 64 4c 69 62 72 61 72 79 57 20 28 28 48 4d 4f 44  dLibraryW ((HMOD
12fa6 55 4c 45 28 57 49 4e 41 50 49 2a 29 28 4c 50 43  ULE(WINAPI*)(LPC
12fa7 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 34  WSTR))aSyscall[4
12fa8 33 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a  3].pCurrent)....
12fa9 20 20 7b 20 22 4c 6f 63 61 6c 46 72 65 65 22 2c    { "LocalFree",
12faa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
12fab 53 59 53 43 41 4c 4c 29 4c 6f 63 61 6c 46 72 65  SYSCALL)LocalFre
12fac 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
12fad 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65   0 },....#define
12fae 20 6f 73 4c 6f 63 61 6c 46 72 65 65 20 28 28 48   osLocalFree ((H
12faf 4c 4f 43 41 4c 28 57 49 4e 41 50 49 2a 29 28 48  LOCAL(WINAPI*)(H
12fb0 4c 4f 43 41 4c 29 29 61 53 79 73 63 61 6c 6c 5b  LOCAL))aSyscall[
12fb1 34 34 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  44].pCurrent)...
12fb2 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
12fb3 57 49 4e 43 45 0d 0a 20 20 7b 20 22 4c 6f 63 6b  WINCE..  { "Lock
12fb4 46 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20  File",          
12fb5 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4c        (SYSCALL)L
12fb6 6f 63 6b 46 69 6c 65 2c 20 20 20 20 20 20 20 20  ockFile,        
12fb7 20 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a          0 },....
12fb8 23 64 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69  #define osLockFi
12fb9 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49  le ((BOOL(WINAPI
12fba 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c  *)(HANDLE,DWORD,
12fbb 44 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0d 0a  DWORD,DWORD, \..
12fbc 20 20 20 20 20 20 20 20 44 57 4f 52 44 29 29 61          DWORD))a
12fbd 53 79 73 63 61 6c 6c 5b 34 35 5d 2e 70 43 75 72  Syscall[45].pCur
12fbe 72 65 6e 74 29 0d 0a 23 65 6c 73 65 0d 0a 20 20  rent)..#else..  
12fbf 7b 20 22 4c 6f 63 6b 46 69 6c 65 22 2c 20 20 20  { "LockFile",   
12fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
12fc1 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
12fc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
12fc3 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23   },..#endif....#
12fc4 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49  if !SQLITE_OS_WI
12fc5 4e 43 45 0d 0a 20 20 7b 20 22 4c 6f 63 6b 46 69  NCE..  { "LockFi
12fc6 6c 65 45 78 22 2c 20 20 20 20 20 20 20 20 20 20  leEx",          
12fc7 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4c 6f 63      (SYSCALL)Loc
12fc8 6b 46 69 6c 65 45 78 2c 20 20 20 20 20 20 20 20  kFileEx,        
12fc9 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64        0 },....#d
12fca 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 6c 65  efine osLockFile
12fcb 45 78 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49  Ex ((BOOL(WINAPI
12fcc 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c  *)(HANDLE,DWORD,
12fcd 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57 4f 52  DWORD,DWORD,DWOR
12fce 44 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50  D, \..        LP
12fcf 4f 56 45 52 4c 41 50 50 45 44 29 29 61 53 79 73  OVERLAPPED))aSys
12fd0 63 61 6c 6c 5b 34 36 5d 2e 70 43 75 72 72 65 6e  call[46].pCurren
12fd1 74 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22  t)..#else..  { "
12fd2 4c 6f 63 6b 46 69 6c 65 45 78 22 2c 20 20 20 20  LockFileEx",    
12fd3 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
12fd4 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
12fd5 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
12fd6 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 7b 20  ..#endif....  { 
12fd7 22 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 22 2c  "MapViewOfFile",
12fd8 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
12fd9 41 4c 4c 29 4d 61 70 56 69 65 77 4f 66 46 69 6c  ALL)MapViewOfFil
12fda 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 20 7d  e,           0 }
12fdb 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 4d  ,....#define osM
12fdc 61 70 56 69 65 77 4f 66 46 69 6c 65 20 28 28 4c  apViewOfFile ((L
12fdd 50 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 48  PVOID(WINAPI*)(H
12fde 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52  ANDLE,DWORD,DWOR
12fdf 44 2c 44 57 4f 52 44 2c 20 5c 0d 0a 20 20 20 20  D,DWORD, \..    
12fe0 20 20 20 20 53 49 5a 45 5f 54 29 29 61 53 79 73      SIZE_T))aSys
12fe1 63 61 6c 6c 5b 34 37 5d 2e 70 43 75 72 72 65 6e  call[47].pCurren
12fe2 74 29 0d 0a 0d 0a 20 20 7b 20 22 4d 75 6c 74 69  t)....  { "Multi
12fe3 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 22 2c  ByteToWideChar",
12fe4 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4d 75       (SYSCALL)Mu
12fe5 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
12fe6 72 2c 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23  r,     0 },....#
12fe7 64 65 66 69 6e 65 20 6f 73 4d 75 6c 74 69 42 79  define osMultiBy
12fe8 74 65 54 6f 57 69 64 65 43 68 61 72 20 28 28 69  teToWideChar ((i
12fe9 6e 74 28 57 49 4e 41 50 49 2a 29 28 55 49 4e 54  nt(WINAPI*)(UINT
12fea 2c 44 57 4f 52 44 2c 4c 50 43 53 54 52 2c 69 6e  ,DWORD,LPCSTR,in
12feb 74 2c 4c 50 57 53 54 52 2c 20 5c 0d 0a 20 20 20  t,LPWSTR, \..   
12fec 20 20 20 20 20 69 6e 74 29 29 61 53 79 73 63 61       int))aSysca
12fed 6c 6c 5b 34 38 5d 2e 70 43 75 72 72 65 6e 74 29  ll[48].pCurrent)
12fee 0d 0a 0d 0a 20 20 7b 20 22 51 75 65 72 79 50 65  ....  { "QueryPe
12fef 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72  rformanceCounter
12ff0 22 2c 20 28 53 59 53 43 41 4c 4c 29 51 75 65 72  ", (SYSCALL)Quer
12ff1 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e  yPerformanceCoun
12ff2 74 65 72 2c 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65  ter, 0 },....#de
12ff3 66 69 6e 65 20 6f 73 51 75 65 72 79 50 65 72 66  fine osQueryPerf
12ff4 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 20 28  ormanceCounter (
12ff5 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 20  (BOOL(WINAPI*)( 
12ff6 5c 0d 0a 20 20 20 20 20 20 20 20 4c 41 52 47 45  \..        LARGE
12ff7 5f 49 4e 54 45 47 45 52 2a 29 29 61 53 79 73 63  _INTEGER*))aSysc
12ff8 61 6c 6c 5b 34 39 5d 2e 70 43 75 72 72 65 6e 74  all[49].pCurrent
12ff9 29 0d 0a 0d 0a 20 20 7b 20 22 52 65 61 64 46 69  )....  { "ReadFi
12ffa 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  le",            
12ffb 20 20 20 20 28 53 59 53 43 41 4c 4c 29 52 65 61      (SYSCALL)Rea
12ffc 64 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  dFile,          
12ffd 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64        0 },....#d
12ffe 65 66 69 6e 65 20 6f 73 52 65 61 64 46 69 6c 65  efine osReadFile
12fff 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29   ((BOOL(WINAPI*)
13000 28 48 41 4e 44 4c 45 2c 4c 50 56 4f 49 44 2c 44  (HANDLE,LPVOID,D
13001 57 4f 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0d  WORD,LPDWORD, \.
13002 0a 20 20 20 20 20 20 20 20 4c 50 4f 56 45 52 4c  .        LPOVERL
13003 41 50 50 45 44 29 29 61 53 79 73 63 61 6c 6c 5b  APPED))aSyscall[
13004 35 30 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d  50].pCurrent)...
13005 0a 20 20 7b 20 22 53 65 74 45 6e 64 4f 66 46 69  .  { "SetEndOfFi
13006 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  le",            
13007 28 53 59 53 43 41 4c 4c 29 53 65 74 45 6e 64 4f  (SYSCALL)SetEndO
13008 66 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  fFile,          
13009 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e    0 },....#defin
1300a 65 20 6f 73 53 65 74 45 6e 64 4f 66 46 69 6c 65  e osSetEndOfFile
1300b 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29   ((BOOL(WINAPI*)
1300c 28 48 41 4e 44 4c 45 29 29 61 53 79 73 63 61 6c  (HANDLE))aSyscal
1300d 6c 5b 35 31 5d 2e 70 43 75 72 72 65 6e 74 29 0d  l[51].pCurrent).
1300e 0a 0d 0a 20 20 7b 20 22 53 65 74 46 69 6c 65 50  ...  { "SetFileP
1300f 6f 69 6e 74 65 72 22 2c 20 20 20 20 20 20 20 20  ointer",        
13010 20 20 28 53 59 53 43 41 4c 4c 29 53 65 74 46 69    (SYSCALL)SetFi
13011 6c 65 50 6f 69 6e 74 65 72 2c 20 20 20 20 20 20  lePointer,      
13012 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66      0 },....#def
13013 69 6e 65 20 6f 73 53 65 74 46 69 6c 65 50 6f 69  ine osSetFilePoi
13014 6e 74 65 72 20 28 28 44 57 4f 52 44 28 57 49 4e  nter ((DWORD(WIN
13015 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 4c 4f 4e  API*)(HANDLE,LON
13016 47 2c 50 4c 4f 4e 47 2c 20 5c 0d 0a 20 20 20 20  G,PLONG, \..    
13017 20 20 20 20 44 57 4f 52 44 29 29 61 53 79 73 63      DWORD))aSysc
13018 61 6c 6c 5b 35 32 5d 2e 70 43 75 72 72 65 6e 74  all[52].pCurrent
13019 29 0d 0a 0d 0a 20 20 7b 20 22 53 6c 65 65 70 22  )....  { "Sleep"
1301a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1301b 20 20 20 20 28 53 59 53 43 41 4c 4c 29 53 6c 65      (SYSCALL)Sle
1301c 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ep,             
1301d 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64        0 },....#d
1301e 65 66 69 6e 65 20 6f 73 53 6c 65 65 70 20 28 28  efine osSleep ((
1301f 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 44 57  VOID(WINAPI*)(DW
13020 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 33  ORD))aSyscall[53
13021 5d 2e 70 43 75 72 72 65 6e 74 29 0d 0a 0d 0a 20  ].pCurrent).... 
13022 20 7b 20 22 53 79 73 74 65 6d 54 69 6d 65 54 6f   { "SystemTimeTo
13023 46 69 6c 65 54 69 6d 65 22 2c 20 20 20 20 28 53  FileTime",    (S
13024 59 53 43 41 4c 4c 29 53 79 73 74 65 6d 54 69 6d  YSCALL)SystemTim
13025 65 54 6f 46 69 6c 65 54 69 6d 65 2c 20 20 20 20  eToFileTime,    
13026 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e 65 20  0 },....#define 
13027 6f 73 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69  osSystemTimeToFi
13028 6c 65 54 69 6d 65 20 28 28 42 4f 4f 4c 28 57 49  leTime ((BOOL(WI
13029 4e 41 50 49 2a 29 28 43 4f 4e 53 54 20 53 59 53  NAPI*)(CONST SYS
1302a 54 45 4d 54 49 4d 45 2a 2c 20 5c 0d 0a 20 20 20  TEMTIME*, \..   
1302b 20 20 20 20 20 4c 50 46 49 4c 45 54 49 4d 45 29       LPFILETIME)
1302c 29 61 53 79 73 63 61 6c 6c 5b 35 34 5d 2e 70 43  )aSyscall[54].pC
1302d 75 72 72 65 6e 74 29 0d 0a 0d 0a 23 69 66 20 21  urrent)....#if !
1302e 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0d  SQLITE_OS_WINCE.
1302f 0a 20 20 7b 20 22 55 6e 6c 6f 63 6b 46 69 6c 65  .  { "UnlockFile
13030 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
13031 28 53 59 53 43 41 4c 4c 29 55 6e 6c 6f 63 6b 46  (SYSCALL)UnlockF
13032 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ile,            
13033 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e    0 },....#defin
13034 65 20 6f 73 55 6e 6c 6f 63 6b 46 69 6c 65 20 28  e osUnlockFile (
13035 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48  (BOOL(WINAPI*)(H
13036 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52  ANDLE,DWORD,DWOR
13037 44 2c 44 57 4f 52 44 2c 20 5c 0d 0a 20 20 20 20  D,DWORD, \..    
13038 20 20 20 20 44 57 4f 52 44 29 29 61 53 79 73 63      DWORD))aSysc
13039 61 6c 6c 5b 35 35 5d 2e 70 43 75 72 72 65 6e 74  all[55].pCurrent
1303a 29 0d 0a 23 65 6c 73 65 0d 0a 20 20 7b 20 22 55  )..#else..  { "U
1303b 6e 6c 6f 63 6b 46 69 6c 65 22 2c 20 20 20 20 20  nlockFile",     
1303c 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
1303d 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  L)0,            
1303e 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0d             0 },.
1303f 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20 21  .#endif....#if !
13040 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0d  SQLITE_OS_WINCE.
13041 0a 20 20 7b 20 22 55 6e 6c 6f 63 6b 46 69 6c 65  .  { "UnlockFile
13042 45 78 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  Ex",            
13043 28 53 59 53 43 41 4c 4c 29 55 6e 6c 6f 63 6b 46  (SYSCALL)UnlockF
13044 69 6c 65 45 78 2c 20 20 20 20 20 20 20 20 20 20  ileEx,          
13045 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65 66 69 6e    0 },....#defin
13046 65 20 6f 73 55 6e 6c 6f 63 6b 46 69 6c 65 45 78  e osUnlockFileEx
13047 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29   ((BOOL(WINAPI*)
13048 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 44 57  (HANDLE,DWORD,DW
13049 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0d 0a 20 20  ORD,DWORD, \..  
1304a 20 20 20 20 20 20 4c 50 4f 56 45 52 4c 41 50 50        LPOVERLAPP
1304b 45 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 36 5d  ED))aSyscall[56]
1304c 2e 70 43 75 72 72 65 6e 74 29 0d 0a 23 65 6c 73  .pCurrent)..#els
1304d 65 0d 0a 20 20 7b 20 22 55 6e 6c 6f 63 6b 46 69  e..  { "UnlockFi
1304e 6c 65 45 78 22 2c 20 20 20 20 20 20 20 20 20 20  leEx",          
1304f 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20    (SYSCALL)0,   
13050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13051 20 20 20 20 30 20 7d 2c 0d 0a 23 65 6e 64 69 66      0 },..#endif
13052 0d 0a 0d 0a 20 20 7b 20 22 55 6e 6d 61 70 56 69  ....  { "UnmapVi
13053 65 77 4f 66 46 69 6c 65 22 2c 20 20 20 20 20 20  ewOfFile",      
13054 20 20 20 28 53 59 53 43 41 4c 4c 29 55 6e 6d 61     (SYSCALL)Unma
13055 70 56 69 65 77 4f 66 46 69 6c 65 2c 20 20 20 20  pViewOfFile,    
13056 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23 64 65       0 },....#de
13057 66 69 6e 65 20 6f 73 55 6e 6d 61 70 56 69 65 77  fine osUnmapView
13058 4f 66 46 69 6c 65 20 28 28 42 4f 4f 4c 28 57 49  OfFile ((BOOL(WI
13059 4e 41 50 49 2a 29 28 4c 50 43 56 4f 49 44 29 29  NAPI*)(LPCVOID))
1305a 61 53 79 73 63 61 6c 6c 5b 35 37 5d 2e 70 43 75  aSyscall[57].pCu
1305b 72 72 65 6e 74 29 0d 0a 0d 0a 20 20 7b 20 22 57  rrent)....  { "W
1305c 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
1305d 74 65 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c  te",     (SYSCAL
1305e 4c 29 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74  L)WideCharToMult
1305f 69 42 79 74 65 2c 20 20 20 20 20 30 20 7d 2c 0d  iByte,     0 },.
13060 0a 0d 0a 23 64 65 66 69 6e 65 20 6f 73 57 69 64  ...#define osWid
13061 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
13062 20 28 28 69 6e 74 28 57 49 4e 41 50 49 2a 29 28   ((int(WINAPI*)(
13063 55 49 4e 54 2c 44 57 4f 52 44 2c 4c 50 43 57 53  UINT,DWORD,LPCWS
13064 54 52 2c 69 6e 74 2c 4c 50 53 54 52 2c 69 6e 74  TR,int,LPSTR,int
13065 2c 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50 43  , \..        LPC
13066 53 54 52 2c 4c 50 42 4f 4f 4c 29 29 61 53 79 73  STR,LPBOOL))aSys
13067 63 61 6c 6c 5b 35 38 5d 2e 70 43 75 72 72 65 6e  call[58].pCurren
13068 74 29 0d 0a 0d 0a 20 20 7b 20 22 57 72 69 74 65  t)....  { "Write
13069 46 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20  File",          
1306a 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 57 72       (SYSCALL)Wr
1306b 69 74 65 46 69 6c 65 2c 20 20 20 20 20 20 20 20  iteFile,        
1306c 20 20 20 20 20 20 20 30 20 7d 2c 0d 0a 0d 0a 23         0 },....#
1306d 64 65 66 69 6e 65 20 6f 73 57 72 69 74 65 46 69  define osWriteFi
1306e 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49  le ((BOOL(WINAPI
1306f 2a 29 28 48 41 4e 44 4c 45 2c 4c 50 43 56 4f 49  *)(HANDLE,LPCVOI
13070 44 2c 44 57 4f 52 44 2c 4c 50 44 57 4f 52 44 2c  D,DWORD,LPDWORD,
13071 20 5c 0d 0a 20 20 20 20 20 20 20 20 4c 50 4f 56   \..        LPOV
13072 45 52 4c 41 50 50 45 44 29 29 61 53 79 73 63 61  ERLAPPED))aSysca
13073 6c 6c 5b 35 39 5d 2e 70 43 75 72 72 65 6e 74 29  ll[59].pCurrent)
13074 0d 0a 0d 0a 7d 3b 20 2f 2a 20 45 6e 64 20 6f 66  ....}; /* End of
13075 20 74 68 65 20 6f 76 65 72 72 69 64 65 61 62 6c   the overrideabl
13076 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 2a  e system calls *
13077 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  /..../*..** This
13078 20 69 73 20 74 68 65 20 78 53 65 74 53 79 73 74   is the xSetSyst
13079 65 6d 43 61 6c 6c 28 29 20 6d 65 74 68 6f 64 20  emCall() method 
1307a 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 73 20 66  of sqlite3_vfs f
1307b 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 0d 0a 2a  or all of the..*
1307c 2a 20 22 77 69 6e 33 32 22 20 56 46 53 65 73 2e  * "win32" VFSes.
1307d 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
1307e 4f 4b 20 6f 70 6f 6e 20 73 75 63 63 65 73 73 66  OK opon successf
1307f 75 6c 6c 79 20 75 70 64 61 74 69 6e 67 20 74 68  ully updating th
13080 65 0d 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c  e..** system cal
13081 6c 20 70 6f 69 6e 74 65 72 2c 20 6f 72 20 53 51  l pointer, or SQ
13082 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 69 66  LITE_NOTFOUND if
13083 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e   there is no con
13084 66 69 67 75 72 61 62 6c 65 0d 0a 2a 2a 20 73 79  figurable..** sy
13085 73 74 65 6d 20 63 61 6c 6c 20 6e 61 6d 65 64 20  stem call named 
13086 7a 4e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  zName...*/..stat
13087 69 63 20 69 6e 74 20 77 69 6e 53 65 74 53 79 73  ic int winSetSys
13088 74 65 6d 43 61 6c 6c 28 0d 0a 20 20 73 71 6c 69  temCall(..  sqli
13089 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74 55 73 65  te3_vfs *pNotUse
1308a 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  d,        /* The
1308b 20 56 46 53 20 70 6f 69 6e 74 65 72 2e 20 20 4e   VFS pointer.  N
1308c 6f 74 20 75 73 65 64 20 2a 2f 0d 0a 20 20 63 6f  ot used */..  co
1308d 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1308e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1308f 61 6d 65 20 6f 66 20 73 79 73 74 65 6d 20 63 61  ame of system ca
13090 6c 6c 20 74 6f 20 6f 76 65 72 72 69 64 65 20 2a  ll to override *
13091 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 73  /..  sqlite3_sys
13092 63 61 6c 6c 5f 70 74 72 20 70 4e 65 77 46 75 6e  call_ptr pNewFun
13093 63 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f  c  /* Pointer to
13094 20 6e 65 77 20 73 79 73 74 65 6d 20 63 61 6c 6c   new system call
13095 20 76 61 6c 75 65 20 2a 2f 0d 0a 29 7b 0d 0a 20   value */..){.. 
13096 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b   unsigned int i;
13097 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
13098 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0d 0a 0d  ITE_NOTFOUND;...
13099 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1309a 54 45 52 28 70 4e 6f 74 55 73 65 64 29 3b 0d 0a  TER(pNotUsed);..
1309b 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
1309c 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20  {..    /* If no 
1309d 7a 4e 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20  zName is given, 
1309e 72 65 73 74 6f 72 65 20 61 6c 6c 20 73 79 73 74  restore all syst
1309f 65 6d 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 69  em calls to thei
130a0 72 20 64 65 66 61 75 6c 74 0d 0a 20 20 20 20 2a  r default..    *
130a1 2a 20 73 65 74 74 69 6e 67 73 20 61 6e 64 20 72  * settings and r
130a2 65 74 75 72 6e 20 4e 55 4c 4c 0d 0a 20 20 20 20  eturn NULL..    
130a3 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  */..    rc = SQL
130a4 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 66 6f 72  ITE_OK;..    for
130a5 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
130a6 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f 66 28  Syscall)/sizeof(
130a7 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b  aSyscall[0]); i+
130a8 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 61  +){..      if( a
130a9 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61  Syscall[i].pDefa
130aa 75 6c 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ult ){..        
130ab 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72  aSyscall[i].pCur
130ac 72 65 6e 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b  rent = aSyscall[
130ad 69 5d 2e 70 44 65 66 61 75 6c 74 3b 0d 0a 20 20  i].pDefault;..  
130ae 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
130af 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49  }else{..    /* I
130b0 66 20 7a 4e 61 6d 65 20 69 73 20 73 70 65 63 69  f zName is speci
130b1 66 69 65 64 2c 20 6f 70 65 72 61 74 65 20 6f 6e  fied, operate on
130b2 20 6f 6e 6c 79 20 74 68 65 20 6f 6e 65 20 73 79   only the one sy
130b3 73 74 65 6d 20 63 61 6c 6c 0d 0a 20 20 20 20 2a  stem call..    *
130b4 2a 20 73 70 65 63 69 66 69 65 64 2e 0d 0a 20 20  * specified...  
130b5 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69 3d    */..    for(i=
130b6 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 53 79 73  0; i<sizeof(aSys
130b7 63 61 6c 6c 29 2f 73 69 7a 65 6f 66 28 61 53 79  call)/sizeof(aSy
130b8 73 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b 2b 29 7b  scall[0]); i++){
130b9 0d 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63  ..      if( strc
130ba 6d 70 28 7a 4e 61 6d 65 2c 20 61 53 79 73 63 61  mp(zName, aSysca
130bb 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20  ll[i].zName)==0 
130bc 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
130bd 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66  aSyscall[i].pDef
130be 61 75 6c 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ault==0 ){..    
130bf 20 20 20 20 20 20 61 53 79 73 63 61 6c 6c 5b 69        aSyscall[i
130c0 5d 2e 70 44 65 66 61 75 6c 74 20 3d 20 61 53 79  ].pDefault = aSy
130c1 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e  scall[i].pCurren
130c2 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  t;..        }.. 
130c3 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
130c4 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20  TE_OK;..        
130c5 69 66 28 20 70 4e 65 77 46 75 6e 63 3d 3d 30 20  if( pNewFunc==0 
130c6 29 20 70 4e 65 77 46 75 6e 63 20 3d 20 61 53 79  ) pNewFunc = aSy
130c7 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c  scall[i].pDefaul
130c8 74 3b 0d 0a 20 20 20 20 20 20 20 20 61 53 79 73  t;..        aSys
130c9 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74  call[i].pCurrent
130ca 20 3d 20 70 4e 65 77 46 75 6e 63 3b 0d 0a 20 20   = pNewFunc;..  
130cb 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
130cc 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
130cd 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
130ce 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
130cf 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  urn the value of
130d0 20 61 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20   a system call. 
130d1 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
130d2 7a 4e 61 6d 65 20 69 73 20 6e 6f 74 20 61 0d 0a  zName is not a..
130d3 2a 2a 20 72 65 63 6f 67 6e 69 7a 65 64 20 73 79  ** recognized sy
130d4 73 74 65 6d 20 63 61 6c 6c 20 6e 61 6d 65 2e 20  stem call name. 
130d5 20 4e 55 4c 4c 20 69 73 20 61 6c 73 6f 20 72 65   NULL is also re
130d6 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 73 79  turned if the sy
130d7 73 74 65 6d 20 63 61 6c 6c 0d 0a 2a 2a 20 69 73  stem call..** is
130d8 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 66   currently undef
130d9 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ined...*/..stati
130da 63 20 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c  c sqlite3_syscal
130db 6c 5f 70 74 72 20 77 69 6e 47 65 74 53 79 73 74  l_ptr winGetSyst
130dc 65 6d 43 61 6c 6c 28 0d 0a 20 20 73 71 6c 69 74  emCall(..  sqlit
130dd 65 33 5f 76 66 73 20 2a 70 4e 6f 74 55 73 65 64  e3_vfs *pNotUsed
130de 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ,..  const char 
130df 2a 7a 4e 61 6d 65 0d 0a 29 7b 0d 0a 20 20 75 6e  *zName..){..  un
130e0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0d 0a 0d  signed int i;...
130e1 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
130e2 54 45 52 28 70 4e 6f 74 55 73 65 64 29 3b 0d 0a  TER(pNotUsed);..
130e3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a    for(i=0; i<siz
130e4 65 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73 69  eof(aSyscall)/si
130e5 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d  zeof(aSyscall[0]
130e6 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  ); i++){..    if
130e7 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20  ( strcmp(zName, 
130e8 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d  aSyscall[i].zNam
130e9 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 61  e)==0 ) return a
130ea 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72  Syscall[i].pCurr
130eb 65 6e 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ent;..  }..  ret
130ec 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  urn 0;..}..../*.
130ed 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
130ee 61 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74  ame of the first
130ef 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 61 66 74   system call aft
130f0 65 72 20 7a 4e 61 6d 65 2e 20 20 49 66 20 7a 4e  er zName.  If zN
130f1 61 6d 65 3d 3d 4e 55 4c 4c 0d 0a 2a 2a 20 74 68  ame==NULL..** th
130f2 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61  en return the na
130f3 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  me of the first 
130f4 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 52 65  system call.  Re
130f5 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 7a 4e 61  turn NULL if zNa
130f6 6d 65 0d 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61  me..** is the la
130f7 73 74 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 6f  st system call o
130f8 72 20 69 66 20 7a 4e 61 6d 65 20 69 73 20 6e 6f  r if zName is no
130f9 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  t the name of a 
130fa 76 61 6c 69 64 0d 0a 2a 2a 20 73 79 73 74 65 6d  valid..** system
130fb 20 63 61 6c 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   call...*/..stat
130fc 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 77  ic const char *w
130fd 69 6e 4e 65 78 74 53 79 73 74 65 6d 43 61 6c 6c  inNextSystemCall
130fe 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 2c  (sqlite3_vfs *p,
130ff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
13100 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 69 20 3d 20  me){..  int i = 
13101 2d 31 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f  -1;....  UNUSED_
13102 50 41 52 41 4d 45 54 45 52 28 70 29 3b 0d 0a 20  PARAMETER(p);.. 
13103 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0d 0a 20   if( zName ){.. 
13104 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72     for(i=0; i<Ar
13105 72 61 79 53 69 7a 65 28 61 53 79 73 63 61 6c 6c  raySize(aSyscall
13106 29 2d 31 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  )-1; i++){..    
13107 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61    if( strcmp(zNa
13108 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e  me, aSyscall[i].
13109 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
1310a 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  k;..    }..  }..
1310b 20 20 66 6f 72 28 69 2b 2b 3b 20 69 3c 41 72 72    for(i++; i<Arr
1310c 61 79 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29  aySize(aSyscall)
1310d 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28  ; i++){..    if(
1310e 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75   aSyscall[i].pCu
1310f 72 72 65 6e 74 21 3d 30 20 29 20 72 65 74 75 72  rrent!=0 ) retur
13110 6e 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e  n aSyscall[i].zN
13111 61 6d 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ame;..  }..  ret
13112 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  urn 0;..}..../*.
13113 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
13114 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65  (non-zero) if we
13115 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64   are running und
13116 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c  er WinNT, Win2K,
13117 20 57 69 6e 58 50 2c 0d 0a 2a 2a 20 6f 72 20 57   WinXP,..** or W
13118 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
13119 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
1311a 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
1311b 57 69 6e 4d 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 48  WinME...**..** H
1311c 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65  ere is an intere
1311d 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f  sting observatio
1311e 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38  n:  Win95, Win98
1311f 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b  , and WinME lack
13120 0d 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c  ..** the LockFil
13121 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 20  eEx() API.  But 
13122 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61  we can still sta
13123 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61  tically link aga
13124 69 6e 73 74 20 74 68 61 74 0d 0a 2a 2a 20 41 50  inst that..** AP
13125 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20  I as long as we 
13126 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 68  don't call it wh
13127 65 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35  en running Win95
13128 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20  /98/ME.  A call 
13129 74 6f 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  to..** this rout
1312a 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  ine is used to d
1312b 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20  etermine if the 
1312c 68 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38  host is Win95/98
1312d 2f 4d 45 20 6f 72 0d 0a 2a 2a 20 57 69 6e 4e 54  /ME or..** WinNT
1312e 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77  /2K/XP so that w
1312f 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74  e will know whet
13130 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61  her or not we ca
13131 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0d 0a 2a  n safely call..*
13132 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
13133 28 29 20 41 50 49 2e 0d 0a 2a 2f 0d 0a 23 69 66  () API...*/..#if
13134 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
13135 0d 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 28  ..# define isNT(
13136 29 20 20 28 31 29 0d 0a 23 65 6c 73 65 0d 0a 20  )  (1)..#else.. 
13137 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54   static int isNT
13138 28 76 6f 69 64 29 7b 0d 0a 20 20 20 20 69 66 28  (void){..    if(
13139 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65   sqlite3_os_type
1313a 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 4f 53  ==0 ){..      OS
1313b 56 45 52 53 49 4f 4e 49 4e 46 4f 41 20 73 49 6e  VERSIONINFOA sIn
1313c 66 6f 3b 0d 0a 20 20 20 20 20 20 73 49 6e 66 6f  fo;..      sInfo
1313d 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
1313e 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
1313f 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 6f 73 47  nfo);..      osG
13140 65 74 56 65 72 73 69 6f 6e 45 78 41 28 26 73 49  etVersionExA(&sI
13141 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  nfo);..      sql
13142 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 73  ite3_os_type = s
13143 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49  Info.dwPlatformI
13144 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f  d==VER_PLATFORM_
13145 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31  WIN32_NT ? 2 : 1
13146 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65  ;..    }..    re
13147 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f  turn sqlite3_os_
13148 74 79 70 65 3d 3d 32 3b 0d 0a 20 20 7d 0d 0a 23  type==2;..  }..#
13149 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1314a 4f 53 5f 57 49 4e 43 45 20 2a 2f 0d 0a 0d 0a 23  OS_WINCE */....#
1314b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e  ifdef SQLITE_WIN
1314c 33 32 5f 4d 41 4c 4c 4f 43 0d 0a 2f 2a 0d 0a 2a  32_MALLOC../*..*
1314d 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
1314e 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f  s of memory...*/
1314f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77  ..static void *w
13150 69 6e 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20  inMemMalloc(int 
13151 6e 42 79 74 65 73 29 7b 0d 0a 20 20 48 41 4e 44  nBytes){..  HAND
13152 4c 45 20 68 48 65 61 70 3b 0d 0a 20 20 76 6f 69  LE hHeap;..  voi
13153 64 20 2a 70 3b 0d 0a 0d 0a 20 20 77 69 6e 4d 65  d *p;....  winMe
13154 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29 3b 0d  mAssertMagic();.
13155 0a 20 20 68 48 65 61 70 20 3d 20 77 69 6e 4d 65  .  hHeap = winMe
13156 6d 47 65 74 48 65 61 70 28 29 3b 0d 0a 20 20 61  mGetHeap();..  a
13157 73 73 65 72 74 28 20 68 48 65 61 70 21 3d 30 20  ssert( hHeap!=0 
13158 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 68 48  );..  assert( hH
13159 65 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e  eap!=INVALID_HAN
1315a 44 4c 45 5f 56 41 4c 55 45 20 29 3b 0d 0a 23 69  DLE_VALUE );..#i
1315b 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33  fdef SQLITE_WIN3
1315c 32 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54  2_MALLOC_VALIDAT
1315d 45 0d 0a 20 20 61 73 73 65 72 74 20 28 20 6f 73  E..  assert ( os
1315e 48 65 61 70 56 61 6c 69 64 61 74 65 28 68 48 65  HeapValidate(hHe
1315f 61 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  ap, SQLITE_WIN32
13160 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20 4e 55 4c  _HEAP_FLAGS, NUL
13161 4c 29 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  L) );..#endif.. 
13162 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
13163 3d 30 20 29 3b 0d 0a 20 20 70 20 3d 20 6f 73 48  =0 );..  p = osH
13164 65 61 70 41 6c 6c 6f 63 28 68 48 65 61 70 2c 20  eapAlloc(hHeap, 
13165 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41  SQLITE_WIN32_HEA
13166 50 5f 46 4c 41 47 53 2c 20 28 53 49 5a 45 5f 54  P_FLAGS, (SIZE_T
13167 29 6e 42 79 74 65 73 29 3b 0d 0a 20 20 69 66 28  )nBytes);..  if(
13168 20 21 70 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69   !p ){..    sqli
13169 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e  te3_log(SQLITE_N
1316a 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20 74 6f  OMEM, "failed to
1316b 20 48 65 61 70 41 6c 6c 6f 63 20 25 75 20 62 79   HeapAlloc %u by
1316c 74 65 73 20 28 25 64 29 2c 20 68 65 61 70 3d 25  tes (%d), heap=%
1316d 70 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  p",..           
1316e 20 20 20 20 20 6e 42 79 74 65 73 2c 20 6f 73 47       nBytes, osG
1316f 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c 20 28  etLastError(), (
13170 76 6f 69 64 2a 29 68 48 65 61 70 29 3b 0d 0a 20  void*)hHeap);.. 
13171 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d   }..  return p;.
13172 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65  .}..../*..** Fre
13173 65 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 73  e memory...*/..s
13174 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 65  tatic void winMe
13175 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69  mFree(void *pPri
13176 6f 72 29 7b 0d 0a 20 20 48 41 4e 44 4c 45 20 68  or){..  HANDLE h
13177 48 65 61 70 3b 0d 0a 0d 0a 20 20 77 69 6e 4d 65  Heap;....  winMe
13178 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29 3b 0d  mAssertMagic();.
13179 0a 20 20 68 48 65 61 70 20 3d 20 77 69 6e 4d 65  .  hHeap = winMe
1317a 6d 47 65 74 48 65 61 70 28 29 3b 0d 0a 20 20 61  mGetHeap();..  a
1317b 73 73 65 72 74 28 20 68 48 65 61 70 21 3d 30 20  ssert( hHeap!=0 
1317c 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 68 48  );..  assert( hH
1317d 65 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e  eap!=INVALID_HAN
1317e 44 4c 45 5f 56 41 4c 55 45 20 29 3b 0d 0a 23 69  DLE_VALUE );..#i
1317f 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33  fdef SQLITE_WIN3
13180 32 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54  2_MALLOC_VALIDAT
13181 45 0d 0a 20 20 61 73 73 65 72 74 20 28 20 6f 73  E..  assert ( os
13182 48 65 61 70 56 61 6c 69 64 61 74 65 28 68 48 65  HeapValidate(hHe
13183 61 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  ap, SQLITE_WIN32
13184 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20 70 50 72  _HEAP_FLAGS, pPr
13185 69 6f 72 29 20 29 3b 0d 0a 23 65 6e 64 69 66 0d  ior) );..#endif.
13186 0a 20 20 69 66 28 20 21 70 50 72 69 6f 72 20 29  .  if( !pPrior )
13187 20 72 65 74 75 72 6e 3b 20 2f 2a 20 50 61 73 73   return; /* Pass
13188 69 6e 67 20 4e 55 4c 4c 20 74 6f 20 48 65 61 70  ing NULL to Heap
13189 46 72 65 65 20 69 73 20 75 6e 64 65 66 69 6e 65  Free is undefine
1318a 64 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 21 6f 73  d. */..  if( !os
1318b 48 65 61 70 46 72 65 65 28 68 48 65 61 70 2c 20  HeapFree(hHeap, 
1318c 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41  SQLITE_WIN32_HEA
1318d 50 5f 46 4c 41 47 53 2c 20 70 50 72 69 6f 72 29  P_FLAGS, pPrior)
1318e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
1318f 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  _log(SQLITE_NOME
13190 4d 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 48 65  M, "failed to He
13191 61 70 46 72 65 65 20 62 6c 6f 63 6b 20 25 70 20  apFree block %p 
13192 28 25 64 29 2c 20 68 65 61 70 3d 25 70 22 2c 0d  (%d), heap=%p",.
13193 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13194 20 70 50 72 69 6f 72 2c 20 6f 73 47 65 74 4c 61   pPrior, osGetLa
13195 73 74 45 72 72 6f 72 28 29 2c 20 28 76 6f 69 64  stError(), (void
13196 2a 29 68 48 65 61 70 29 3b 0d 0a 20 20 7d 0d 0a  *)hHeap);..  }..
13197 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e  }..../*..** Chan
13198 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
13199 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
1319a 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d 0a 2a 2f  y allocation..*/
1319b 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77  ..static void *w
1319c 69 6e 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69  inMemRealloc(voi
1319d 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
1319e 42 79 74 65 73 29 7b 0d 0a 20 20 48 41 4e 44 4c  Bytes){..  HANDL
1319f 45 20 68 48 65 61 70 3b 0d 0a 20 20 76 6f 69 64  E hHeap;..  void
131a0 20 2a 70 3b 0d 0a 0d 0a 20 20 77 69 6e 4d 65 6d   *p;....  winMem
131a1 41 73 73 65 72 74 4d 61 67 69 63 28 29 3b 0d 0a  AssertMagic();..
131a2 20 20 68 48 65 61 70 20 3d 20 77 69 6e 4d 65 6d    hHeap = winMem
131a3 47 65 74 48 65 61 70 28 29 3b 0d 0a 20 20 61 73  GetHeap();..  as
131a4 73 65 72 74 28 20 68 48 65 61 70 21 3d 30 20 29  sert( hHeap!=0 )
131a5 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 68 48 65  ;..  assert( hHe
131a6 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44  ap!=INVALID_HAND
131a7 4c 45 5f 56 41 4c 55 45 20 29 3b 0d 0a 23 69 66  LE_VALUE );..#if
131a8 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  def SQLITE_WIN32
131a9 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45  _MALLOC_VALIDATE
131aa 0d 0a 20 20 61 73 73 65 72 74 20 28 20 6f 73 48  ..  assert ( osH
131ab 65 61 70 56 61 6c 69 64 61 74 65 28 68 48 65 61  eapValidate(hHea
131ac 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  p, SQLITE_WIN32_
131ad 48 45 41 50 5f 46 4c 41 47 53 2c 20 70 50 72 69  HEAP_FLAGS, pPri
131ae 6f 72 29 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  or) );..#endif..
131af 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73    assert( nBytes
131b0 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 21 70  >=0 );..  if( !p
131b1 50 72 69 6f 72 20 29 7b 0d 0a 20 20 20 20 70 20  Prior ){..    p 
131b2 3d 20 6f 73 48 65 61 70 41 6c 6c 6f 63 28 68 48  = osHeapAlloc(hH
131b3 65 61 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33  eap, SQLITE_WIN3
131b4 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20 28 53  2_HEAP_FLAGS, (S
131b5 49 5a 45 5f 54 29 6e 42 79 74 65 73 29 3b 0d 0a  IZE_T)nBytes);..
131b6 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 20    }else{..    p 
131b7 3d 20 6f 73 48 65 61 70 52 65 41 6c 6c 6f 63 28  = osHeapReAlloc(
131b8 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57 49  hHeap, SQLITE_WI
131b9 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20  N32_HEAP_FLAGS, 
131ba 70 50 72 69 6f 72 2c 20 28 53 49 5a 45 5f 54 29  pPrior, (SIZE_T)
131bb 6e 42 79 74 65 73 29 3b 0d 0a 20 20 7d 0d 0a 20  nBytes);..  }.. 
131bc 20 69 66 28 20 21 70 20 29 7b 0d 0a 20 20 20 20   if( !p ){..    
131bd 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49  sqlite3_log(SQLI
131be 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65  TE_NOMEM, "faile
131bf 64 20 74 6f 20 25 73 20 25 75 20 62 79 74 65 73  d to %s %u bytes
131c0 20 28 25 64 29 2c 20 68 65 61 70 3d 25 70 22 2c   (%d), heap=%p",
131c1 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
131c2 20 20 70 50 72 69 6f 72 20 3f 20 22 48 65 61 70    pPrior ? "Heap
131c3 52 65 41 6c 6c 6f 63 22 20 3a 20 22 48 65 61 70  ReAlloc" : "Heap
131c4 41 6c 6c 6f 63 22 2c 20 6e 42 79 74 65 73 2c 20  Alloc", nBytes, 
131c5 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  osGetLastError()
131c6 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
131c7 20 20 20 28 76 6f 69 64 2a 29 68 48 65 61 70 29     (void*)hHeap)
131c8 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
131c9 20 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   p;..}..../*..**
131ca 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
131cb 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69   of an outstandi
131cc 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ng allocation, i
131cd 6e 20 62 79 74 65 73 2e 0d 0a 2a 2f 0d 0a 73 74  n bytes...*/..st
131ce 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 65 6d 53  atic int winMemS
131cf 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0d 0a 20  ize(void *p){.. 
131d0 20 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 0d 0a   HANDLE hHeap;..
131d1 20 20 53 49 5a 45 5f 54 20 6e 3b 0d 0a 0d 0a 20    SIZE_T n;.... 
131d2 20 77 69 6e 4d 65 6d 41 73 73 65 72 74 4d 61 67   winMemAssertMag
131d3 69 63 28 29 3b 0d 0a 20 20 68 48 65 61 70 20 3d  ic();..  hHeap =
131d4 20 77 69 6e 4d 65 6d 47 65 74 48 65 61 70 28 29   winMemGetHeap()
131d5 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 68 48 65  ;..  assert( hHe
131d6 61 70 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  ap!=0 );..  asse
131d7 72 74 28 20 68 48 65 61 70 21 3d 49 4e 56 41 4c  rt( hHeap!=INVAL
131d8 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20  ID_HANDLE_VALUE 
131d9 29 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
131da 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f 56  E_WIN32_MALLOC_V
131db 41 4c 49 44 41 54 45 0d 0a 20 20 61 73 73 65 72  ALIDATE..  asser
131dc 74 20 28 20 6f 73 48 65 61 70 56 61 6c 69 64 61  t ( osHeapValida
131dd 74 65 28 68 48 65 61 70 2c 20 53 51 4c 49 54 45  te(hHeap, SQLITE
131de 5f 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47  _WIN32_HEAP_FLAG
131df 53 2c 20 4e 55 4c 4c 29 20 29 3b 0d 0a 23 65 6e  S, NULL) );..#en
131e0 64 69 66 0d 0a 20 20 69 66 28 20 21 70 20 29 20  dif..  if( !p ) 
131e1 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 6e 20 3d  return 0;..  n =
131e2 20 6f 73 48 65 61 70 53 69 7a 65 28 68 48 65 61   osHeapSize(hHea
131e3 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  p, SQLITE_WIN32_
131e4 48 45 41 50 5f 46 4c 41 47 53 2c 20 70 29 3b 0d  HEAP_FLAGS, p);.
131e5 0a 20 20 69 66 28 20 6e 3d 3d 28 53 49 5a 45 5f  .  if( n==(SIZE_
131e6 54 29 2d 31 20 29 7b 0d 0a 20 20 20 20 73 71 6c  T)-1 ){..    sql
131e7 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f  ite3_log(SQLITE_
131e8 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20 74  NOMEM, "failed t
131e9 6f 20 48 65 61 70 53 69 7a 65 20 62 6c 6f 63 6b  o HeapSize block
131ea 20 25 70 20 28 25 64 29 2c 20 68 65 61 70 3d 25   %p (%d), heap=%
131eb 70 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  p",..           
131ec 20 20 20 20 20 70 2c 20 6f 73 47 65 74 4c 61 73       p, osGetLas
131ed 74 45 72 72 6f 72 28 29 2c 20 28 76 6f 69 64 2a  tError(), (void*
131ee 29 68 48 65 61 70 29 3b 0d 0a 20 20 20 20 72 65  )hHeap);..    re
131ef 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 0;..  }..  
131f0 72 65 74 75 72 6e 20 28 69 6e 74 29 6e 3b 0d 0a  return (int)n;..
131f1 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 75 6e  }..../*..** Roun
131f2 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73  d up a request s
131f3 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ize to the next 
131f4 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  valid allocation
131f5 20 73 69 7a 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   size...*/..stat
131f6 69 63 20 69 6e 74 20 77 69 6e 4d 65 6d 52 6f 75  ic int winMemRou
131f7 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0d 0a 20 20  ndup(int n){..  
131f8 72 65 74 75 72 6e 20 6e 3b 0d 0a 7d 0d 0a 0d 0a  return n;..}....
131f9 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  /*..** Initializ
131fa 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0d 0a  e this module...
131fb 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  */..static int w
131fc 69 6e 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a  inMemInit(void *
131fd 70 41 70 70 44 61 74 61 29 7b 0d 0a 20 20 77 69  pAppData){..  wi
131fe 6e 4d 65 6d 44 61 74 61 20 2a 70 57 69 6e 4d 65  nMemData *pWinMe
131ff 6d 44 61 74 61 20 3d 20 28 77 69 6e 4d 65 6d 44  mData = (winMemD
13200 61 74 61 20 2a 29 70 41 70 70 44 61 74 61 3b 0d  ata *)pAppData;.
13201 0a 0d 0a 20 20 69 66 28 20 21 70 57 69 6e 4d 65  ...  if( !pWinMe
13202 6d 44 61 74 61 20 29 20 72 65 74 75 72 6e 20 53  mData ) return S
13203 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
13204 61 73 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44  assert( pWinMemD
13205 61 74 61 2d 3e 6d 61 67 69 63 3d 3d 57 49 4e 4d  ata->magic==WINM
13206 45 4d 5f 4d 41 47 49 43 20 29 3b 0d 0a 20 20 69  EM_MAGIC );..  i
13207 66 28 20 21 70 57 69 6e 4d 65 6d 44 61 74 61 2d  f( !pWinMemData-
13208 3e 68 48 65 61 70 20 29 7b 0d 0a 20 20 20 20 70  >hHeap ){..    p
13209 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61  WinMemData->hHea
1320a 70 20 3d 20 6f 73 48 65 61 70 43 72 65 61 74 65  p = osHeapCreate
1320b 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45  (SQLITE_WIN32_HE
1320c 41 50 5f 46 4c 41 47 53 2c 0d 0a 20 20 20 20 20  AP_FLAGS,..     
1320d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1320e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1320f 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45   SQLITE_WIN32_HE
13210 41 50 5f 49 4e 49 54 5f 53 49 5a 45 2c 0d 0a 20  AP_INIT_SIZE,.. 
13211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13212 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13213 20 20 20 20 20 53 51 4c 49 54 45 5f 57 49 4e 33       SQLITE_WIN3
13214 32 5f 48 45 41 50 5f 4d 41 58 5f 53 49 5a 45 29  2_HEAP_MAX_SIZE)
13215 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 57 69 6e  ;..    if( !pWin
13216 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 20 29  MemData->hHeap )
13217 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
13218 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  _log(SQLITE_NOME
13219 4d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 22 66  M,..          "f
1321a 61 69 6c 65 64 20 74 6f 20 48 65 61 70 43 72 65  ailed to HeapCre
1321b 61 74 65 20 28 25 64 29 2c 20 66 6c 61 67 73 3d  ate (%d), flags=
1321c 25 75 2c 20 69 6e 69 74 53 69 7a 65 3d 25 75 2c  %u, initSize=%u,
1321d 20 6d 61 78 53 69 7a 65 3d 25 75 22 2c 0d 0a 20   maxSize=%u",.. 
1321e 20 20 20 20 20 20 20 20 20 6f 73 47 65 74 4c 61           osGetLa
1321f 73 74 45 72 72 6f 72 28 29 2c 20 53 51 4c 49 54  stError(), SQLIT
13220 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41  E_WIN32_HEAP_FLA
13221 47 53 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 53  GS,..          S
13222 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50  QLITE_WIN32_HEAP
13223 5f 49 4e 49 54 5f 53 49 5a 45 2c 20 53 51 4c 49  _INIT_SIZE, SQLI
13224 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41  TE_WIN32_HEAP_MA
13225 58 5f 53 49 5a 45 29 3b 0d 0a 20 20 20 20 20 20  X_SIZE);..      
13226 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
13227 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  MEM;..    }..   
13228 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62 4f   pWinMemData->bO
13229 77 6e 65 64 20 3d 20 54 52 55 45 3b 0d 0a 20 20  wned = TRUE;..  
1322a 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 57 69  }..  assert( pWi
1322b 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 21  nMemData->hHeap!
1322c 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
1322d 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68 48   pWinMemData->hH
1322e 65 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e  eap!=INVALID_HAN
1322f 44 4c 45 5f 56 41 4c 55 45 20 29 3b 0d 0a 23 69  DLE_VALUE );..#i
13230 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33  fdef SQLITE_WIN3
13231 32 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54  2_MALLOC_VALIDAT
13232 45 0d 0a 20 20 61 73 73 65 72 74 28 20 6f 73 48  E..  assert( osH
13233 65 61 70 56 61 6c 69 64 61 74 65 28 70 57 69 6e  eapValidate(pWin
13234 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 2c 20  MemData->hHeap, 
13235 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41  SQLITE_WIN32_HEA
13236 50 5f 46 4c 41 47 53 2c 20 4e 55 4c 4c 29 20 29  P_FLAGS, NULL) )
13237 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65 74  ;..#endif..  ret
13238 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
13239 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 69 6e  }..../*..** Dein
1323a 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
1323b 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  dule...*/..stati
1323c 63 20 76 6f 69 64 20 77 69 6e 4d 65 6d 53 68 75  c void winMemShu
1323d 74 64 6f 77 6e 28 76 6f 69 64 20 2a 70 41 70 70  tdown(void *pApp
1323e 44 61 74 61 29 7b 0d 0a 20 20 77 69 6e 4d 65 6d  Data){..  winMem
1323f 44 61 74 61 20 2a 70 57 69 6e 4d 65 6d 44 61 74  Data *pWinMemDat
13240 61 20 3d 20 28 77 69 6e 4d 65 6d 44 61 74 61 20  a = (winMemData 
13241 2a 29 70 41 70 70 44 61 74 61 3b 0d 0a 0d 0a 20  *)pAppData;.... 
13242 20 69 66 28 20 21 70 57 69 6e 4d 65 6d 44 61 74   if( !pWinMemDat
13243 61 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 69  a ) return;..  i
13244 66 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e  f( pWinMemData->
13245 68 48 65 61 70 20 29 7b 0d 0a 20 20 20 20 61 73  hHeap ){..    as
13246 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44 61 74  sert( pWinMemDat
13247 61 2d 3e 68 48 65 61 70 21 3d 49 4e 56 41 4c 49  a->hHeap!=INVALI
13248 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29  D_HANDLE_VALUE )
13249 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1324a 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f 56 41  _WIN32_MALLOC_VA
1324b 4c 49 44 41 54 45 0d 0a 20 20 20 20 61 73 73 65  LIDATE..    asse
1324c 72 74 28 20 6f 73 48 65 61 70 56 61 6c 69 64 61  rt( osHeapValida
1324d 74 65 28 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e  te(pWinMemData->
1324e 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57 49  hHeap, SQLITE_WI
1324f 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20  N32_HEAP_FLAGS, 
13250 4e 55 4c 4c 29 20 29 3b 0d 0a 23 65 6e 64 69 66  NULL) );..#endif
13251 0d 0a 20 20 20 20 69 66 28 20 70 57 69 6e 4d 65  ..    if( pWinMe
13252 6d 44 61 74 61 2d 3e 62 4f 77 6e 65 64 20 29 7b  mData->bOwned ){
13253 0d 0a 20 20 20 20 20 20 69 66 28 20 21 6f 73 48  ..      if( !osH
13254 65 61 70 44 65 73 74 72 6f 79 28 70 57 69 6e 4d  eapDestroy(pWinM
13255 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 29 20 29  emData->hHeap) )
13256 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
13257 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f  e3_log(SQLITE_NO
13258 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20 74 6f 20  MEM, "failed to 
13259 48 65 61 70 44 65 73 74 72 6f 79 20 28 25 64 29  HeapDestroy (%d)
1325a 2c 20 68 65 61 70 3d 25 70 22 2c 0d 0a 20 20 20  , heap=%p",..   
1325b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1325c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
1325d 29 2c 20 28 76 6f 69 64 2a 29 70 57 69 6e 4d 65  ), (void*)pWinMe
1325e 6d 44 61 74 61 2d 3e 68 48 65 61 70 29 3b 0d 0a  mData->hHeap);..
1325f 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70        }..      p
13260 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62 4f 77 6e  WinMemData->bOwn
13261 65 64 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20 20  ed = FALSE;..   
13262 20 7d 0d 0a 20 20 20 20 70 57 69 6e 4d 65 6d 44   }..    pWinMemD
13263 61 74 61 2d 3e 68 48 65 61 70 20 3d 20 4e 55 4c  ata->hHeap = NUL
13264 4c 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  L;..  }..}..../*
13265 0d 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68  ..** Populate th
13266 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  e low-level memo
13267 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75  ry allocation fu
13268 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20  nction pointers 
13269 69 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c  in..** sqlite3Gl
1326a 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74  obalConfig.m wit
1326b 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  h pointers to th
1326c 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
1326d 69 73 20 66 69 6c 65 2e 20 54 68 65 0d 0a 2a 2a  is file. The..**
1326e 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
1326f 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20  fy the block of 
13270 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65  memory to manage
13271 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
13272 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63  outine is only c
13273 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33  alled by sqlite3
13274 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74  _config(), and t
13275 68 65 72 65 66 6f 72 65 0d 0a 2a 2a 20 69 73 20  herefore..** is 
13276 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
13277 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 69  be threadsafe (i
13278 74 20 69 73 20 6e 6f 74 29 2e 0d 0a 2a 2f 0d 0a  t is not)...*/..
13279 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
1327a 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  onst sqlite3_mem
1327b 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
1327c 33 4d 65 6d 47 65 74 57 69 6e 33 32 28 76 6f 69  3MemGetWin32(voi
1327d 64 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  d){..  static co
1327e 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
1327f 6d 65 74 68 6f 64 73 20 77 69 6e 4d 65 6d 4d 65  methods winMemMe
13280 74 68 6f 64 73 20 3d 20 7b 0d 0a 20 20 20 20 77  thods = {..    w
13281 69 6e 4d 65 6d 4d 61 6c 6c 6f 63 2c 0d 0a 20 20  inMemMalloc,..  
13282 20 20 77 69 6e 4d 65 6d 46 72 65 65 2c 0d 0a 20    winMemFree,.. 
13283 20 20 20 77 69 6e 4d 65 6d 52 65 61 6c 6c 6f 63     winMemRealloc
13284 2c 0d 0a 20 20 20 20 77 69 6e 4d 65 6d 53 69 7a  ,..    winMemSiz
13285 65 2c 0d 0a 20 20 20 20 77 69 6e 4d 65 6d 52 6f  e,..    winMemRo
13286 75 6e 64 75 70 2c 0d 0a 20 20 20 20 77 69 6e 4d  undup,..    winM
13287 65 6d 49 6e 69 74 2c 0d 0a 20 20 20 20 77 69 6e  emInit,..    win
13288 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0d 0a 20 20  MemShutdown,..  
13289 20 20 26 77 69 6e 5f 6d 65 6d 5f 64 61 74 61 0d    &win_mem_data.
1328a 0a 20 20 7d 3b 0d 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
1328b 26 77 69 6e 4d 65 6d 4d 65 74 68 6f 64 73 3b 0d  &winMemMethods;.
1328c 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  .}....SQLITE_PRI
1328d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1328e 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76  3MemSetDefault(v
1328f 6f 69 64 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  oid){..  sqlite3
13290 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
13291 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 73 71  ONFIG_MALLOC, sq
13292 6c 69 74 65 33 4d 65 6d 47 65 74 57 69 6e 33 32  lite3MemGetWin32
13293 28 29 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20  ());..}..#endif 
13294 2f 2a 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  /* SQLITE_WIN32_
13295 4d 41 4c 4c 4f 43 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  MALLOC */..../*.
13296 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54  .** Convert a UT
13297 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 4d 69  F-8 string to Mi
13298 63 72 6f 73 6f 66 74 20 55 6e 69 63 6f 64 65 20  crosoft Unicode 
13299 28 55 54 46 2d 31 36 3f 29 2e 20 0d 0a 2a 2a 0d  (UTF-16?). ..**.
1329a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
1329b 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
1329c 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
1329d 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0d 0a  d from malloc...
1329e 2a 2f 0d 0a 73 74 61 74 69 63 20 4c 50 57 53 54  */..static LPWST
1329f 52 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28  R utf8ToUnicode(
132a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
132a1 65 6e 61 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 6e  ename){..  int n
132a2 43 68 61 72 3b 0d 0a 20 20 4c 50 57 53 54 52 20  Char;..  LPWSTR 
132a3 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0d 0a  zWideFilename;..
132a4 0d 0a 20 20 6e 43 68 61 72 20 3d 20 6f 73 4d 75  ..  nChar = osMu
132a5 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
132a6 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46  r(CP_UTF8, 0, zF
132a7 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c  ilename, -1, NUL
132a8 4c 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 6e 43  L, 0);..  if( nC
132a9 68 61 72 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  har==0 ){..    r
132aa 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
132ab 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d   zWideFilename =
132ac 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
132ad 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57   nChar*sizeof(zW
132ae 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20  ideFilename[0]) 
132af 29 3b 0d 0a 20 20 69 66 28 20 7a 57 69 64 65 46  );..  if( zWideF
132b0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20  ilename==0 ){.. 
132b1 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
132b2 7d 0d 0a 20 20 6e 43 68 61 72 20 3d 20 6f 73 4d  }..  nChar = osM
132b3 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
132b4 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  ar(CP_UTF8, 0, z
132b5 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 57  Filename, -1, zW
132b6 69 64 65 46 69 6c 65 6e 61 6d 65 2c 0d 0a 20 20  ideFilename,..  
132b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 43                nC
132b9 68 61 72 29 3b 0d 0a 20 20 69 66 28 20 6e 43 68  har);..  if( nCh
132ba 61 72 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  ar==0 ){..    sq
132bb 6c 69 74 65 33 5f 66 72 65 65 28 7a 57 69 64 65  lite3_free(zWide
132bc 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 20 20  Filename);..    
132bd 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20  zWideFilename = 
132be 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  0;..  }..  retur
132bf 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b  n zWideFilename;
132c0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f  ..}..../*..** Co
132c1 6e 76 65 72 74 20 4d 69 63 72 6f 73 6f 66 74 20  nvert Microsoft 
132c2 55 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38  Unicode to UTF-8
132c3 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
132c4 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
132c5 72 69 6e 67 20 69 73 0d 0a 2a 2a 20 6f 62 74 61  ring is..** obta
132c6 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
132c7 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2f 0d  3_malloc()...*/.
132c8 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e  .static char *un
132c9 69 63 6f 64 65 54 6f 55 74 66 38 28 4c 50 43 57  icodeToUtf8(LPCW
132ca 53 54 52 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  STR zWideFilenam
132cb 65 29 7b 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65  e){..  int nByte
132cc 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ;..  char *zFile
132cd 6e 61 6d 65 3b 0d 0a 0d 0a 20 20 6e 42 79 74 65  name;....  nByte
132ce 20 3d 20 6f 73 57 69 64 65 43 68 61 72 54 6f 4d   = osWideCharToM
132cf 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54 46 38  ultiByte(CP_UTF8
132d0 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61  , 0, zWideFilena
132d1 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c  me, -1, 0, 0, 0,
132d2 20 30 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74   0);..  if( nByt
132d3 65 20 3d 3d 20 30 20 29 7b 0d 0a 20 20 20 20 72  e == 0 ){..    r
132d4 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
132d5 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c   zFilename = sql
132d6 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 42 79  ite3_malloc( nBy
132d7 74 65 20 29 3b 0d 0a 20 20 69 66 28 20 7a 46 69  te );..  if( zFi
132d8 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20 20  lename==0 ){..  
132d9 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
132da 0d 0a 20 20 6e 42 79 74 65 20 3d 20 6f 73 57 69  ..  nByte = osWi
132db 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
132dc 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
132dd 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
132de 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
132df 65 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e,..            
132e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132e1 20 20 20 20 30 2c 20 30 29 3b 0d 0a 20 20 69 66      0, 0);..  if
132e2 28 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0d  ( nByte == 0 ){.
132e3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
132e4 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20  e(zFilename);.. 
132e5 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
132e6 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
132e7 20 7a 46 69 6c 65 6e 61 6d 65 3b 0d 0a 7d 0d 0a   zFilename;..}..
132e8 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74  ../*..** Convert
132e9 20 61 6e 20 41 4e 53 49 20 73 74 72 69 6e 67 20   an ANSI string 
132ea 74 6f 20 4d 69 63 72 6f 73 6f 66 74 20 55 6e 69  to Microsoft Uni
132eb 63 6f 64 65 2c 20 62 61 73 65 64 20 6f 6e 20 74  code, based on t
132ec 68 65 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63  he..** current c
132ed 6f 64 65 70 61 67 65 20 73 65 74 74 69 6e 67 73  odepage settings
132ee 20 66 6f 72 20 66 69 6c 65 20 61 70 69 73 2e 0d   for file apis..
132ef 0a 2a 2a 20 0d 0a 2a 2a 20 53 70 61 63 65 20 74  .** ..** Space t
132f0 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72  o hold the retur
132f1 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62  ned string is ob
132f2 74 61 69 6e 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20  tained..** from 
132f3 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 0d  sqlite3_malloc..
132f4 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 4c 50 57 53  .*/..static LPWS
132f5 54 52 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65  TR mbcsToUnicode
132f6 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
132f7 6c 65 6e 61 6d 65 29 7b 0d 0a 20 20 69 6e 74 20  lename){..  int 
132f8 6e 42 79 74 65 3b 0d 0a 20 20 4c 50 57 53 54 52  nByte;..  LPWSTR
132f9 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0d   zMbcsFilename;.
132fa 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20  .  int codepage 
132fb 3d 20 6f 73 41 72 65 46 69 6c 65 41 70 69 73 41  = osAreFileApisA
132fc 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a  NSI() ? CP_ACP :
132fd 20 43 50 5f 4f 45 4d 43 50 3b 0d 0a 0d 0a 20 20   CP_OEMCP;....  
132fe 6e 42 79 74 65 20 3d 20 6f 73 4d 75 6c 74 69 42  nByte = osMultiB
132ff 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f  yteToWideChar(co
13300 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65  depage, 0, zFile
13301 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 0d  name, -1, NULL,.
13302 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13303 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13304 20 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52   0)*sizeof(WCHAR
13305 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74 65 3d  );..  if( nByte=
13306 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
13307 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 4d 62  n 0;..  }..  zMb
13308 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c  csFilename = sql
13309 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 42 79  ite3_malloc( nBy
1330a 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46  te*sizeof(zMbcsF
1330b 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0d 0a  ilename[0]) );..
1330c 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e    if( zMbcsFilen
1330d 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ame==0 ){..    r
1330e 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
1330f 20 6e 42 79 74 65 20 3d 20 6f 73 4d 75 6c 74 69   nByte = osMulti
13310 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63  ByteToWideChar(c
13311 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c  odepage, 0, zFil
13312 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73  ename, -1, zMbcs
13313 46 69 6c 65 6e 61 6d 65 2c 0d 0a 20 20 20 20 20  Filename,..     
13314 20 20 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 6e 42 79 74 65             nByte
13316 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74 65 3d  );..  if( nByte=
13317 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  =0 ){..    sqlit
13318 65 33 5f 66 72 65 65 28 7a 4d 62 63 73 46 69 6c  e3_free(zMbcsFil
13319 65 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 7a 4d 62  ename);..    zMb
1331a 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0d  csFilename = 0;.
1331b 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 7a  .  }..  return z
1331c 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0d 0a 7d  MbcsFilename;..}
1331d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65  ..../*..** Conve
1331e 72 74 20 4d 69 63 72 6f 73 6f 66 74 20 55 6e 69  rt Microsoft Uni
1331f 63 6f 64 65 20 74 6f 20 6d 75 6c 74 69 2d 62 79  code to multi-by
13320 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72  te character str
13321 69 6e 67 2c 20 62 61 73 65 64 20 6f 6e 20 74 68  ing, based on th
13322 65 0d 0a 2a 2a 20 75 73 65 72 27 73 20 41 4e 53  e..** user's ANS
13323 49 20 63 6f 64 65 70 61 67 65 2e 0d 0a 2a 2a 0d  I codepage...**.
13324 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
13325 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
13326 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
13327 64 20 66 72 6f 6d 0d 0a 2a 2a 20 73 71 6c 69 74  d from..** sqlit
13328 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2f  e3_malloc()...*/
13329 0d 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75  ..static char *u
1332a 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 4c 50 43  nicodeToMbcs(LPC
1332b 57 53 54 52 20 7a 57 69 64 65 46 69 6c 65 6e 61  WSTR zWideFilena
1332c 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 6e 42 79 74  me){..  int nByt
1332d 65 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  e;..  char *zFil
1332e 65 6e 61 6d 65 3b 0d 0a 20 20 69 6e 74 20 63 6f  ename;..  int co
1332f 64 65 70 61 67 65 20 3d 20 6f 73 41 72 65 46 69  depage = osAreFi
13330 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43  leApisANSI() ? C
13331 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50  P_ACP : CP_OEMCP
13332 3b 0d 0a 0d 0a 20 20 6e 42 79 74 65 20 3d 20 6f  ;....  nByte = o
13333 73 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69  sWideCharToMulti
13334 42 79 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30  Byte(codepage, 0
13335 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c  , zWideFilename,
13336 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29   -1, 0, 0, 0, 0)
13337 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d  ;..  if( nByte =
13338 3d 20 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  = 0 ){..    retu
13339 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 46  rn 0;..  }..  zF
1333a 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ilename = sqlite
1333b 33 5f 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20  3_malloc( nByte 
1333c 29 3b 0d 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  );..  if( zFilen
1333d 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ame==0 ){..    r
1333e 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
1333f 20 6e 42 79 74 65 20 3d 20 6f 73 57 69 64 65 43   nByte = osWideC
13340 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
13341 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
13342 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
13343 46 69 6c 65 6e 61 6d 65 2c 0d 0a 20 20 20 20 20  Filename,..     
13344 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13345 20 20 20 20 20 20 20 20 20 20 20 6e 42 79 74 65             nByte
13346 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20  , 0, 0);..  if( 
13347 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0d 0a 20  nByte == 0 ){.. 
13348 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
13349 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 20  zFilename);..   
1334a 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0d   zFilename = 0;.
1334b 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 7a  .  }..  return z
1334c 46 69 6c 65 6e 61 6d 65 3b 0d 0a 7d 0d 0a 0d 0a  Filename;..}....
1334d 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d  /*..** Convert m
1334e 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74  ultibyte charact
1334f 65 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46  er string to UTF
13350 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f  -8.  Space to ho
13351 6c 64 20 74 68 65 0d 0a 2a 2a 20 72 65 74 75 72  ld the..** retur
13352 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62  ned string is ob
13353 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
13354 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a  te3_malloc()...*
13355 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  /..SQLITE_API ch
13356 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33  ar *sqlite3_win3
13357 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 63  2_mbcs_to_utf8(c
13358 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
13359 6e 61 6d 65 29 7b 0d 0a 20 20 63 68 61 72 20 2a  name){..  char *
1335a 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0d 0a  zFilenameUtf8;..
1335b 20 20 4c 50 57 53 54 52 20 7a 54 6d 70 57 69 64    LPWSTR zTmpWid
1335c 65 3b 0d 0a 0d 0a 20 20 7a 54 6d 70 57 69 64 65  e;....  zTmpWide
1335d 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65   = mbcsToUnicode
1335e 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20  (zFilename);..  
1335f 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
13360 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
13361 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 46 69 6c 65 6e  ;..  }..  zFilen
13362 61 6d 65 55 74 66 38 20 3d 20 75 6e 69 63 6f 64  ameUtf8 = unicod
13363 65 54 6f 55 74 66 38 28 7a 54 6d 70 57 69 64 65  eToUtf8(zTmpWide
13364 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
13365 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0d 0a 20  ee(zTmpWide);.. 
13366 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d   return zFilenam
13367 65 55 74 66 38 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  eUtf8;..}..../*.
13368 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d  .** Convert UTF-
13369 38 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63  8 to multibyte c
1336a 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e  haracter string.
1336b 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
1336c 74 68 65 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 65  the ..** returne
1336d 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
1336e 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1336f 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2f 0d  3_malloc()...*/.
13370 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
13371 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f   *sqlite3_win32_
13372 75 74 66 38 5f 74 6f 5f 6d 62 63 73 28 63 6f 6e  utf8_to_mbcs(con
13373 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
13374 6d 65 29 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 46  me){..  char *zF
13375 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0d 0a 20 20  ilenameMbcs;..  
13376 4c 50 57 53 54 52 20 7a 54 6d 70 57 69 64 65 3b  LPWSTR zTmpWide;
13377 0d 0a 0d 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d  ....  zTmpWide =
13378 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a   utf8ToUnicode(z
13379 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 69 66  Filename);..  if
1337a 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b  ( zTmpWide==0 ){
1337b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
1337c 0a 20 20 7d 0d 0a 20 20 7a 46 69 6c 65 6e 61 6d  .  }..  zFilenam
1337d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54  eMbcs = unicodeT
1337e 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b  oMbcs(zTmpWide);
1337f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
13380 28 7a 54 6d 70 57 69 64 65 29 3b 0d 0a 20 20 72  (zTmpWide);..  r
13381 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d  eturn zFilenameM
13382 62 63 73 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  bcs;..}....../*.
13383 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
13384 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45  alue of getLastE
13385 72 72 6f 72 4d 73 67 0d 0a 2a 2a 20 69 73 20 7a  rrorMsg..** is z
13386 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72  ero if the error
13387 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 6e   message fits in
13388 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20   the buffer, or 
13389 6e 6f 6e 2d 7a 65 72 6f 0d 0a 2a 2a 20 6f 74 68  non-zero..** oth
1338a 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d  erwise (if the m
1338b 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63  essage was trunc
1338c 61 74 65 64 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ated)...*/..stat
1338d 69 63 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72  ic int getLastEr
1338e 72 6f 72 4d 73 67 28 44 57 4f 52 44 20 6c 61 73  rorMsg(DWORD las
1338f 74 45 72 72 6e 6f 2c 20 69 6e 74 20 6e 42 75 66  tErrno, int nBuf
13390 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0d 0a  , char *zBuf){..
13391 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61    /* FormatMessa
13392 67 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e 20  ge returns 0 on 
13393 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 77  failure.  Otherw
13394 69 73 65 20 69 74 0d 0a 20 20 2a 2a 20 72 65 74  ise it..  ** ret
13395 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
13396 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 74 65  of TCHARs writte
13397 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0d  n to the output.
13398 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 78  .  ** buffer, ex
13399 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 6d  cluding the term
1339a 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61  inating null cha
1339b 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 44 57 4f 52  r...  */..  DWOR
1339c 44 20 64 77 4c 65 6e 20 3d 20 30 3b 0d 0a 20 20  D dwLen = 0;..  
1339d 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 30 3b 0d  char *zOut = 0;.
1339e 0a 0d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  ...  if( isNT() 
1339f 29 7b 0d 0a 20 20 20 20 4c 50 57 53 54 52 20 7a  ){..    LPWSTR z
133a0 54 65 6d 70 57 69 64 65 20 3d 20 4e 55 4c 4c 3b  TempWide = NULL;
133a1 0d 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 6f 73  ..    dwLen = os
133a2 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 28 46  FormatMessageW(F
133a3 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c  ORMAT_MESSAGE_AL
133a4 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 0d  LOCATE_BUFFER |.
133a5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
133a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 46 4f                FO
133a7 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f  RMAT_MESSAGE_FRO
133a8 4d 5f 53 59 53 54 45 4d 20 7c 0d 0a 20 20 20 20  M_SYSTEM |..    
133a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133aa 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f           FORMAT_
133ab 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49  MESSAGE_IGNORE_I
133ac 4e 53 45 52 54 53 2c 0d 0a 20 20 20 20 20 20 20  NSERTS,..       
133ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133ae 20 20 20 20 20 20 4e 55 4c 4c 2c 0d 0a 20 20 20        NULL,..   
133af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133b0 20 20 20 20 20 20 20 20 20 20 6c 61 73 74 45 72            lastEr
133b1 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  rno,..          
133b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133b3 20 20 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20     0,..         
133b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133b5 20 20 20 20 28 4c 50 57 53 54 52 29 20 26 7a 54      (LPWSTR) &zT
133b6 65 6d 70 57 69 64 65 2c 0d 0a 20 20 20 20 20 20  empWide,..      
133b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133b8 20 20 20 20 20 20 20 30 2c 0d 0a 20 20 20 20 20         0,..     
133b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133ba 20 20 20 20 20 20 20 20 30 29 3b 0d 0a 20 20 20          0);..   
133bb 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29   if( dwLen > 0 )
133bc 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f  {..      /* allo
133bd 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61 6e  cate a buffer an
133be 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 46  d convert to UTF
133bf 38 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69  8 */..      sqli
133c0 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
133c1 6c 6c 6f 63 28 29 3b 0d 0a 20 20 20 20 20 20 7a  lloc();..      z
133c2 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55  Out = unicodeToU
133c3 74 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0d  tf8(zTempWide);.
133c4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
133c5 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
133c6 0d 0a 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20  ..      /* free 
133c7 74 68 65 20 73 79 73 74 65 6d 20 62 75 66 66 65  the system buffe
133c8 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46  r allocated by F
133c9 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0d  ormatMessage */.
133ca 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61 6c 46 72  .      osLocalFr
133cb 65 65 28 7a 54 65 6d 70 57 69 64 65 29 3b 0d 0a  ee(zTempWide);..
133cc 20 20 20 20 7d 0d 0a 2f 2a 20 69 73 4e 54 28 29      }../* isNT()
133cd 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
133ce 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
133cf 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
133d0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0d 0a 2a  er executed. ..*
133d1 2a 20 53 69 6e 63 65 20 74 68 65 20 41 4e 53 49  * Since the ANSI
133d2 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
133d3 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
133d4 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
133d5 49 4e 43 45 2c 0d 0a 2a 2a 20 69 74 27 73 20 69  INCE,..** it's i
133d6 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
133d7 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
133d8 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
133d9 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45  ..*/..#if SQLITE
133da 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20 20  _OS_WINCE==0..  
133db 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 63 68 61 72  }else{..    char
133dc 20 2a 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b 0d   *zTemp = NULL;.
133dd 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 6f 73 46  .    dwLen = osF
133de 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46 4f  ormatMessageA(FO
133df 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c  RMAT_MESSAGE_ALL
133e0 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 0d 0a  OCATE_BUFFER |..
133e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133e2 20 20 20 20 20 20 20 20 20 20 20 20 20 46 4f 52               FOR
133e3 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d  MAT_MESSAGE_FROM
133e4 5f 53 59 53 54 45 4d 20 7c 0d 0a 20 20 20 20 20  _SYSTEM |..     
133e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133e6 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f 4d          FORMAT_M
133e7 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e  ESSAGE_IGNORE_IN
133e8 53 45 52 54 53 2c 0d 0a 20 20 20 20 20 20 20 20  SERTS,..        
133e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133ea 20 20 20 20 20 4e 55 4c 4c 2c 0d 0a 20 20 20 20       NULL,..    
133eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133ec 20 20 20 20 20 20 20 20 20 6c 61 73 74 45 72 72           lastErr
133ed 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  no,..           
133ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133ef 20 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20    0,..          
133f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133f1 20 20 20 28 4c 50 53 54 52 29 20 26 7a 54 65 6d     (LPSTR) &zTem
133f2 70 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  p,..            
133f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133f4 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20   0,..           
133f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133f6 20 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 64    0);..    if( d
133f7 77 4c 65 6e 20 3e 20 30 20 29 7b 0d 0a 20 20 20  wLen > 0 ){..   
133f8 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61     /* allocate a
133f9 20 62 75 66 66 65 72 20 61 6e 64 20 63 6f 6e 76   buffer and conv
133fa 65 72 74 20 74 6f 20 55 54 46 38 20 2a 2f 0d 0a  ert to UTF8 */..
133fb 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
133fc 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
133fd 3b 0d 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20  ;..      zOut = 
133fe 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
133ff 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70  cs_to_utf8(zTemp
13400 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
13401 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
13402 28 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 66 72  ();..      /* fr
13403 65 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 75  ee the system bu
13404 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62  ffer allocated b
13405 79 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20  y FormatMessage 
13406 2a 2f 0d 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61  */..      osLoca
13407 6c 46 72 65 65 28 7a 54 65 6d 70 29 3b 0d 0a 20  lFree(zTemp);.. 
13408 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20     }..#endif..  
13409 7d 0d 0a 20 20 69 66 28 20 30 20 3d 3d 20 64 77  }..  if( 0 == dw
1340a 4c 65 6e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  Len ){..    sqli
1340b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
1340c 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
1340d 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 6c 61  r 0x%x (%u)", la
1340e 73 74 45 72 72 6e 6f 2c 20 6c 61 73 74 45 72 72  stErrno, lastErr
1340f 6e 6f 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  no);..  }else{..
13410 20 20 20 20 2f 2a 20 63 6f 70 79 20 61 20 6d 61      /* copy a ma
13411 78 69 6d 75 6d 20 6f 66 20 6e 42 75 66 20 63 68  ximum of nBuf ch
13412 61 72 73 20 74 6f 20 6f 75 74 70 75 74 20 62 75  ars to output bu
13413 66 66 65 72 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  ffer */..    sql
13414 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
13415 75 66 2c 20 7a 42 75 66 2c 20 22 25 73 22 2c 20  uf, zBuf, "%s", 
13416 7a 4f 75 74 29 3b 0d 0a 20 20 20 20 2f 2a 20 66  zOut);..    /* f
13417 72 65 65 20 74 68 65 20 55 54 46 38 20 62 75 66  ree the UTF8 buf
13418 66 65 72 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  fer */..    sqli
13419 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 29 3b 0d  te3_free(zOut);.
1341a 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30  .  }..  return 0
1341b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 0d 0a  ;..}..../*..**..
1341c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1341d 20 2d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 41 74   - winLogErrorAt
1341e 4c 69 6e 65 28 29 20 2d 20 69 73 20 6f 6e 6c 79  Line() - is only
1341f 20 65 76 65 72 20 63 61 6c 6c 65 64 20 76 69 61   ever called via
13420 20 74 68 65 20 6d 61 63 72 6f 0d 0a 2a 2a 20 77   the macro..** w
13421 69 6e 4c 6f 67 45 72 72 6f 72 28 29 2e 0d 0a 2a  inLogError()...*
13422 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
13423 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 66  ne is invoked af
13424 74 65 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63  ter an error occ
13425 75 72 73 20 69 6e 20 61 6e 20 4f 53 20 66 75 6e  urs in an OS fun
13426 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 49 74 20 6c 6f  ction...** It lo
13427 67 73 20 61 20 6d 65 73 73 61 67 65 20 75 73 69  gs a message usi
13428 6e 67 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29  ng sqlite3_log()
13429 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1342a 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
1342b 0d 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  ..** error code 
1342c 61 6e 64 2c 20 69 66 20 70 6f 73 73 69 62 6c 65  and, if possible
1342d 2c 20 74 68 65 20 68 75 6d 61 6e 2d 72 65 61 64  , the human-read
1342e 61 62 6c 65 20 65 71 75 69 76 61 6c 65 6e 74 20  able equivalent 
1342f 66 72 6f 6d 20 0d 0a 2a 2a 20 46 6f 72 6d 61 74  from ..** Format
13430 4d 65 73 73 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  Message...**..**
13431 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
13432 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68  ent passed to th
13433 65 20 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62  e macro should b
13434 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  e the error code
13435 20 74 68 61 74 0d 0a 2a 2a 20 77 69 6c 6c 20 62   that..** will b
13436 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51  e returned to SQ
13437 4c 69 74 65 20 28 65 2e 67 2e 20 53 51 4c 49 54  Lite (e.g. SQLIT
13438 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20  E_IOERR_DELETE, 
13439 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29  SQLITE_CANTOPEN)
1343a 2e 20 0d 0a 2a 2a 20 54 68 65 20 74 77 6f 20 73  . ..** The two s
1343b 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65  ubsequent argume
1343c 6e 74 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68  nts should be th
1343d 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4f 53  e name of the OS
1343e 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 0d 0a   function that..
1343f 2a 2a 20 66 61 69 6c 65 64 20 61 6e 64 20 74 68  ** failed and th
13440 65 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  e the associated
13441 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 70 61 74   file-system pat
13442 68 2c 20 69 66 20 61 6e 79 2e 0d 0a 2a 2f 0d 0a  h, if any...*/..
13443 23 64 65 66 69 6e 65 20 77 69 6e 4c 6f 67 45 72  #define winLogEr
13444 72 6f 72 28 61 2c 62 2c 63 2c 64 29 20 20 20 77  ror(a,b,c,d)   w
13445 69 6e 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e 65  inLogErrorAtLine
13446 28 61 2c 62 2c 63 2c 64 2c 5f 5f 4c 49 4e 45 5f  (a,b,c,d,__LINE_
13447 5f 29 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  _)..static int w
13448 69 6e 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e 65  inLogErrorAtLine
13449 28 0d 0a 20 20 69 6e 74 20 65 72 72 63 6f 64 65  (..  int errcode
1344a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1344b 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 65       /* SQLite e
1344c 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  rror code */..  
1344d 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 2c  DWORD lastErrno,
1344e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1344f 2f 2a 20 57 69 6e 33 32 20 6c 61 73 74 20 65 72  /* Win32 last er
13450 72 6f 72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ror */..  const 
13451 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 20  char *zFunc,    
13452 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
13453 65 20 6f 66 20 4f 53 20 66 75 6e 63 74 69 6f 6e  e of OS function
13454 20 74 68 61 74 20 66 61 69 6c 65 64 20 2a 2f 0d   that failed */.
13455 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
13456 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20  Path,           
13457 20 20 20 2f 2a 20 46 69 6c 65 20 70 61 74 68 20     /* File path 
13458 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
13459 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  error */..  int 
1345a 69 4c 69 6e 65 20 20 20 20 20 20 20 20 20 20 20  iLine           
1345b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1345c 6f 75 72 63 65 20 6c 69 6e 65 20 6e 75 6d 62 65  ource line numbe
1345d 72 20 77 68 65 72 65 20 65 72 72 6f 72 20 6f 63  r where error oc
1345e 63 75 72 72 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20  curred */..){.. 
1345f 20 63 68 61 72 20 7a 4d 73 67 5b 35 30 30 5d 3b   char zMsg[500];
13460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13461 20 2f 2a 20 48 75 6d 61 6e 20 72 65 61 64 61 62   /* Human readab
13462 6c 65 20 65 72 72 6f 72 20 74 65 78 74 20 2a 2f  le error text */
13463 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  ..  int i;      
13464 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13465 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
13466 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 7a 4d 73 67  ter */....  zMsg
13467 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 67 65 74 4c  [0] = 0;..  getL
13468 61 73 74 45 72 72 6f 72 4d 73 67 28 6c 61 73 74  astErrorMsg(last
13469 45 72 72 6e 6f 2c 20 73 69 7a 65 6f 66 28 7a 4d  Errno, sizeof(zM
1346a 73 67 29 2c 20 7a 4d 73 67 29 3b 0d 0a 20 20 61  sg), zMsg);..  a
1346b 73 73 65 72 74 28 20 65 72 72 63 6f 64 65 21 3d  ssert( errcode!=
1346c 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20  SQLITE_OK );..  
1346d 69 66 28 20 7a 50 61 74 68 3d 3d 30 20 29 20 7a  if( zPath==0 ) z
1346e 50 61 74 68 20 3d 20 22 22 3b 0d 0a 20 20 66 6f  Path = "";..  fo
1346f 72 28 69 3d 30 3b 20 7a 4d 73 67 5b 69 5d 20 26  r(i=0; zMsg[i] &
13470 26 20 7a 4d 73 67 5b 69 5d 21 3d 27 5c 72 27 20  & zMsg[i]!='\r' 
13471 26 26 20 7a 4d 73 67 5b 69 5d 21 3d 27 5c 6e 27  && zMsg[i]!='\n'
13472 3b 20 69 2b 2b 29 7b 7d 0d 0a 20 20 7a 4d 73 67  ; i++){}..  zMsg
13473 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69  [i] = 0;..  sqli
13474 74 65 33 5f 6c 6f 67 28 65 72 72 63 6f 64 65 2c  te3_log(errcode,
13475 0d 0a 20 20 20 20 20 20 22 6f 73 5f 77 69 6e 2e  ..      "os_win.
13476 63 3a 25 64 3a 20 28 25 64 29 20 25 73 28 25 73  c:%d: (%d) %s(%s
13477 29 20 2d 20 25 73 22 2c 0d 0a 20 20 20 20 20 20  ) - %s",..      
13478 69 4c 69 6e 65 2c 20 6c 61 73 74 45 72 72 6e 6f  iLine, lastErrno
13479 2c 20 7a 46 75 6e 63 2c 20 7a 50 61 74 68 2c 20  , zFunc, zPath, 
1347a 7a 4d 73 67 0d 0a 20 20 29 3b 0d 0a 0d 0a 20 20  zMsg..  );....  
1347b 72 65 74 75 72 6e 20 65 72 72 63 6f 64 65 3b 0d  return errcode;.
1347c 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  .}..../*..** The
1347d 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
1347e 20 74 68 61 74 20 61 20 52 65 61 64 46 69 6c 65   that a ReadFile
1347f 28 29 2c 20 57 72 69 74 65 46 69 6c 65 28 29 2c  (), WriteFile(),
13480 20 61 6e 64 20 44 65 6c 65 74 65 46 69 6c 65 28   and DeleteFile(
13481 29 0d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65  )..** will be re
13482 74 72 69 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  tried following 
13483 61 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72 20  a locking error 
13484 2d 20 70 72 6f 62 61 62 6c 79 20 63 61 75 73 65  - probably cause
13485 64 20 62 79 20 0d 0a 2a 2a 20 61 6e 74 69 76 69  d by ..** antivi
13486 72 75 73 20 73 6f 66 74 77 61 72 65 2e 20 20 41  rus software.  A
13487 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 20  lso the initial 
13488 64 65 6c 61 79 20 62 65 66 6f 72 65 20 74 68 65  delay before the
13489 20 66 69 72 73 74 20 72 65 74 72 79 2e 0d 0a 2a   first retry...*
1348a 2a 20 54 68 65 20 64 65 6c 61 79 20 69 6e 63 72  * The delay incr
1348b 65 61 73 65 73 20 6c 69 6e 65 61 72 6c 79 20 77  eases linearly w
1348c 69 74 68 20 65 61 63 68 20 72 65 74 72 79 2e 0d  ith each retry..
1348d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
1348e 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f  ITE_WIN32_IOERR_
1348f 52 45 54 52 59 0d 0a 23 20 64 65 66 69 6e 65 20  RETRY..# define 
13490 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45  SQLITE_WIN32_IOE
13491 52 52 5f 52 45 54 52 59 20 31 30 0d 0a 23 65 6e  RR_RETRY 10..#en
13492 64 69 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
13493 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f  ITE_WIN32_IOERR_
13494 52 45 54 52 59 5f 44 45 4c 41 59 0d 0a 23 20 64  RETRY_DELAY..# d
13495 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 49 4e  efine SQLITE_WIN
13496 33 32 5f 49 4f 45 52 52 5f 52 45 54 52 59 5f 44  32_IOERR_RETRY_D
13497 45 4c 41 59 20 32 35 0d 0a 23 65 6e 64 69 66 0d  ELAY 25..#endif.
13498 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 33  .static int win3
13499 32 49 6f 65 72 72 52 65 74 72 79 20 3d 20 53 51  2IoerrRetry = SQ
1349a 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52  LITE_WIN32_IOERR
1349b 5f 52 45 54 52 59 3b 0d 0a 73 74 61 74 69 63 20  _RETRY;..static 
1349c 69 6e 74 20 77 69 6e 33 32 49 6f 65 72 72 52 65  int win32IoerrRe
1349d 74 72 79 44 65 6c 61 79 20 3d 20 53 51 4c 49 54  tryDelay = SQLIT
1349e 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f 52 45  E_WIN32_IOERR_RE
1349f 54 52 59 5f 44 45 4c 41 59 3b 0d 0a 0d 0a 2f 2a  TRY_DELAY;..../*
134a0 0d 0a 2a 2a 20 49 66 20 61 20 52 65 61 64 46 69  ..** If a ReadFi
134a1 6c 65 28 29 20 6f 72 20 57 72 69 74 65 46 69 6c  le() or WriteFil
134a2 65 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73  e() error occurs
134a3 2c 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 72 6f  , invoke this ro
134a4 75 74 69 6e 65 0d 0a 2a 2a 20 74 6f 20 73 65 65  utine..** to see
134a5 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 62 65   if it should be
134a6 20 72 65 74 72 69 65 64 2e 20 20 52 65 74 75 72   retried.  Retur
134a7 6e 20 54 52 55 45 20 74 6f 20 72 65 74 72 79 2e  n TRUE to retry.
134a8 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0d 0a    Return FALSE..
134a9 2a 2a 20 74 6f 20 67 69 76 65 20 75 70 20 77 69  ** to give up wi
134aa 74 68 20 61 6e 20 65 72 72 6f 72 2e 0d 0a 2a 2f  th an error...*/
134ab 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 74  ..static int ret
134ac 72 79 49 6f 65 72 72 28 69 6e 74 20 2a 70 6e 52  ryIoerr(int *pnR
134ad 65 74 72 79 2c 20 44 57 4f 52 44 20 2a 70 45 72  etry, DWORD *pEr
134ae 72 6f 72 29 7b 0d 0a 20 20 44 57 4f 52 44 20 65  ror){..  DWORD e
134af 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f   = osGetLastErro
134b0 72 28 29 3b 0d 0a 20 20 69 66 28 20 2a 70 6e 52  r();..  if( *pnR
134b1 65 74 72 79 3e 3d 77 69 6e 33 32 49 6f 65 72 72  etry>=win32Ioerr
134b2 52 65 74 72 79 20 29 7b 0d 0a 20 20 20 20 69 66  Retry ){..    if
134b3 28 20 70 45 72 72 6f 72 20 29 7b 0d 0a 20 20 20  ( pError ){..   
134b4 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 3b 0d     *pError = e;.
134b5 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75  .    }..    retu
134b6 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  rn 0;..  }..  if
134b7 28 20 65 3d 3d 45 52 52 4f 52 5f 41 43 43 45 53  ( e==ERROR_ACCES
134b8 53 5f 44 45 4e 49 45 44 20 7c 7c 0d 0a 20 20 20  S_DENIED ||..   
134b9 20 20 20 65 3d 3d 45 52 52 4f 52 5f 4c 4f 43 4b     e==ERROR_LOCK
134ba 5f 56 49 4f 4c 41 54 49 4f 4e 20 7c 7c 0d 0a 20  _VIOLATION ||.. 
134bb 20 20 20 20 20 65 3d 3d 45 52 52 4f 52 5f 53 48       e==ERROR_SH
134bc 41 52 49 4e 47 5f 56 49 4f 4c 41 54 49 4f 4e 20  ARING_VIOLATION 
134bd 29 7b 0d 0a 20 20 20 20 6f 73 53 6c 65 65 70 28  ){..    osSleep(
134be 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79 44  win32IoerrRetryD
134bf 65 6c 61 79 2a 28 31 2b 2a 70 6e 52 65 74 72 79  elay*(1+*pnRetry
134c0 29 29 3b 0d 0a 20 20 20 20 2b 2b 2a 70 6e 52 65  ));..    ++*pnRe
134c1 74 72 79 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  try;..    return
134c2 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   1;..  }..  if( 
134c3 70 45 72 72 6f 72 20 29 7b 0d 0a 20 20 20 20 2a  pError ){..    *
134c4 70 45 72 72 6f 72 20 3d 20 65 3b 0d 0a 20 20 7d  pError = e;..  }
134c5 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
134c6 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 67 20 61  ..../*..** Log a
134c7 20 49 2f 4f 20 65 72 72 6f 72 20 72 65 74 72 79   I/O error retry
134c8 20 65 70 69 73 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73   episode...*/..s
134c9 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 67 49 6f  tatic void logIo
134ca 65 72 72 28 69 6e 74 20 6e 52 65 74 72 79 29 7b  err(int nRetry){
134cb 0d 0a 20 20 69 66 28 20 6e 52 65 74 72 79 20 29  ..  if( nRetry )
134cc 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c  {..    sqlite3_l
134cd 6f 67 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c  og(SQLITE_IOERR,
134ce 20 0d 0a 20 20 20 20 20 20 22 64 65 6c 61 79 65   ..      "delaye
134cf 64 20 25 64 6d 73 20 66 6f 72 20 6c 6f 63 6b 2f  d %dms for lock/
134d0 73 68 61 72 69 6e 67 20 63 6f 6e 66 6c 69 63 74  sharing conflict
134d1 22 2c 0d 0a 20 20 20 20 20 20 77 69 6e 33 32 49  ",..      win32I
134d2 6f 65 72 72 52 65 74 72 79 44 65 6c 61 79 2a 6e  oerrRetryDelay*n
134d3 52 65 74 72 79 2a 28 6e 52 65 74 72 79 2b 31 29  Retry*(nRetry+1)
134d4 2f 32 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d 0d  /2..    );..  }.
134d5 0a 7d 0d 0a 0d 0a 23 69 66 20 53 51 4c 49 54 45  .}....#if SQLITE
134d6 5f 4f 53 5f 57 49 4e 43 45 0d 0a 2f 2a 2a 2a 2a  _OS_WINCE../****
134d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
134d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
134d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
134da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
134db 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 73  *****..** This s
134dc 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  ection contains 
134dd 63 6f 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f  code for WinCE o
134de 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  nly...*/../*..**
134df 20 57 69 6e 64 6f 77 73 20 43 45 20 64 6f 65 73   Windows CE does
134e0 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63 61   not have a loca
134e1 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e  ltime() function
134e2 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0d 0a  .  So create a..
134e3 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0d 0a  ** substitute...
134e4 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  */../* #include 
134e5 3c 74 69 6d 65 2e 68 3e 20 2a 2f 0d 0a 73 74 72  <time.h> */..str
134e6 75 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20  uct tm *__cdecl 
134e7 6c 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20  localtime(const 
134e8 74 69 6d 65 5f 74 20 2a 74 29 0d 0a 7b 0d 0a 20  time_t *t)..{.. 
134e9 20 73 74 61 74 69 63 20 73 74 72 75 63 74 20 74   static struct t
134ea 6d 20 79 3b 0d 0a 20 20 46 49 4c 45 54 49 4d 45  m y;..  FILETIME
134eb 20 75 54 6d 2c 20 6c 54 6d 3b 0d 0a 20 20 53 59   uTm, lTm;..  SY
134ec 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0d 0a 20  STEMTIME pTm;.. 
134ed 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
134ee 36 34 3b 0d 0a 20 20 74 36 34 20 3d 20 2a 74 3b  64;..  t64 = *t;
134ef 0d 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b  ..  t64 = (t64 +
134f0 20 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30   11644473600)*10
134f1 30 30 30 30 30 30 3b 0d 0a 20 20 75 54 6d 2e 64  000000;..  uTm.d
134f2 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 28  wLowDateTime = (
134f3 44 57 4f 52 44 29 28 74 36 34 20 26 20 30 78 46  DWORD)(t64 & 0xF
134f4 46 46 46 46 46 46 46 29 3b 0d 0a 20 20 75 54 6d  FFFFFFF);..  uTm
134f5 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 3d  .dwHighDateTime=
134f6 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e 3e 20   (DWORD)(t64 >> 
134f7 33 32 29 3b 0d 0a 20 20 6f 73 46 69 6c 65 54 69  32);..  osFileTi
134f8 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d  meToLocalFileTim
134f9 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0d 0a 20  e(&uTm,&lTm);.. 
134fa 20 6f 73 46 69 6c 65 54 69 6d 65 54 6f 53 79 73   osFileTimeToSys
134fb 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54  temTime(&lTm,&pT
134fc 6d 29 3b 0d 0a 20 20 79 2e 74 6d 5f 79 65 61 72  m);..  y.tm_year
134fd 20 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31   = pTm.wYear - 1
134fe 39 30 30 3b 0d 0a 20 20 79 2e 74 6d 5f 6d 6f 6e  900;..  y.tm_mon
134ff 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20   = pTm.wMonth - 
13500 31 3b 0d 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20  1;..  y.tm_wday 
13501 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b  = pTm.wDayOfWeek
13502 3b 0d 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20 3d  ;..  y.tm_mday =
13503 20 70 54 6d 2e 77 44 61 79 3b 0d 0a 20 20 79 2e   pTm.wDay;..  y.
13504 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48  tm_hour = pTm.wH
13505 6f 75 72 3b 0d 0a 20 20 79 2e 74 6d 5f 6d 69 6e  our;..  y.tm_min
13506 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0d   = pTm.wMinute;.
13507 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54  .  y.tm_sec = pT
13508 6d 2e 77 53 65 63 6f 6e 64 3b 0d 0a 20 20 72 65  m.wSecond;..  re
13509 74 75 72 6e 20 26 79 3b 0d 0a 7d 0d 0a 0d 0a 23  turn &y;..}....#
1350a 64 65 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f  define HANDLE_TO
1350b 5f 57 49 4e 46 49 4c 45 28 61 29 20 28 77 69 6e  _WINFILE(a) (win
1350c 46 69 6c 65 2a 29 26 28 28 63 68 61 72 2a 29 61  File*)&((char*)a
1350d 29 5b 2d 28 69 6e 74 29 6f 66 66 73 65 74 6f 66  )[-(int)offsetof
1350e 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0d 0a 0d 0a  (winFile,h)]....
1350f 2f 2a 0d 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  /*..** Acquire a
13510 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e   lock on the han
13511 64 6c 65 20 68 0d 0a 2a 2f 0d 0a 73 74 61 74 69  dle h..*/..stati
13512 63 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65  c void winceMute
13513 78 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20  xAcquire(HANDLE 
13514 68 29 7b 0d 0a 20 20 20 44 57 4f 52 44 20 64 77  h){..   DWORD dw
13515 45 72 72 3b 0d 0a 20 20 20 64 6f 20 7b 0d 0a 20  Err;..   do {.. 
13516 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69 74      dwErr = Wait
13517 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 28  ForSingleObject(
13518 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0d 0a 20  h, INFINITE);.. 
13519 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72    } while (dwErr
1351a 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f   != WAIT_OBJECT_
1351b 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41  0 && dwErr != WA
1351c 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0d 0a  IT_ABANDONED);..
1351d 7d 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73  }../*..** Releas
1351e 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65  e a lock acquire
1351f 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41  d by winceMutexA
13520 63 71 75 69 72 65 28 29 0d 0a 2a 2f 0d 0a 23 64  cquire()..*/..#d
13521 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78  efine winceMutex
13522 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61  Release(h) Relea
13523 73 65 4d 75 74 65 78 28 68 29 0d 0a 0d 0a 2f 2a  seMutex(h)..../*
13524 0d 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20  ..** Create the 
13525 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65 64  mutex and shared
13526 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72   memory used for
13527 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 20   locking in the 
13528 66 69 6c 65 0d 0a 2a 2a 20 64 65 73 63 72 69 70  file..** descrip
13529 74 6f 72 20 70 46 69 6c 65 0d 0a 2a 2f 0d 0a 73  tor pFile..*/..s
1352a 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65  tatic BOOL wince
1352b 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74  CreateLock(const
1352c 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
1352d 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65  , winFile *pFile
1352e 29 7b 0d 0a 20 20 4c 50 57 53 54 52 20 7a 54 6f  ){..  LPWSTR zTo
1352f 6b 3b 0d 0a 20 20 4c 50 57 53 54 52 20 7a 4e 61  k;..  LPWSTR zNa
13530 6d 65 3b 0d 0a 20 20 42 4f 4f 4c 20 62 49 6e 69  me;..  BOOL bIni
13531 74 20 3d 20 54 52 55 45 3b 0d 0a 0d 0a 20 20 7a  t = TRUE;....  z
13532 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e 69  Name = utf8ToUni
13533 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  code(zFilename);
13534 0d 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ..  if( zName==0
13535 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 6f 75 74 20   ){..    /* out 
13536 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20  of memory */..  
13537 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0d    return FALSE;.
13538 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 69  .  }....  /* Ini
13539 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61  tialize the loca
1353a 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0d 0a 20  l lockdata */.. 
1353b 20 6d 65 6d 73 65 74 28 26 70 46 69 6c 65 2d 3e   memset(&pFile->
1353c 6c 6f 63 61 6c 2c 20 30 2c 20 73 69 7a 65 6f 66  local, 0, sizeof
1353d 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b  (pFile->local));
1353e 0d 0a 0d 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65  ....  /* Replace
1353f 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73   the backslashes
13540 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61   from the filena
13541 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65  me and lowercase
13542 20 69 74 0d 0a 20 20 2a 2a 20 74 6f 20 64 65 72   it..  ** to der
13543 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65  ive a mutex name
13544 2e 20 2a 2f 0d 0a 20 20 7a 54 6f 6b 20 3d 20 6f  . */..  zTok = o
13545 73 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d  sCharLowerW(zNam
13546 65 29 3b 0d 0a 20 20 66 6f 72 20 28 3b 2a 7a 54  e);..  for (;*zT
13547 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0d 0a 20 20 20  ok;zTok++){..   
13548 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c   if (*zTok == '\
13549 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b  \') *zTok = '_';
1354a 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 72  ..  }....  /* Cr
1354b 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61  eate/open the na
1354c 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0d 0a 20 20  med mutex */..  
1354d 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
1354e 6f 73 43 72 65 61 74 65 4d 75 74 65 78 57 28 4e  osCreateMutexW(N
1354f 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d  ULL, FALSE, zNam
13550 65 29 3b 0d 0a 20 20 69 66 20 28 21 70 46 69 6c  e);..  if (!pFil
13551 65 2d 3e 68 4d 75 74 65 78 29 7b 0d 0a 20 20 20  e->hMutex){..   
13552 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
13553 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72  o = osGetLastErr
13554 6f 72 28 29 3b 0d 0a 20 20 20 20 77 69 6e 4c 6f  or();..    winLo
13555 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45 52  gError(SQLITE_ER
13556 52 4f 52 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74  ROR, pFile->last
13557 45 72 72 6e 6f 2c 20 22 77 69 6e 63 65 43 72 65  Errno, "winceCre
13558 61 74 65 4c 6f 63 6b 31 22 2c 20 7a 46 69 6c 65  ateLock1", zFile
13559 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69  name);..    sqli
1355a 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b  te3_free(zName);
1355b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c  ..    return FAL
1355c 53 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  SE;..  }....  /*
1355d 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
1355e 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e  ex before contin
1355f 75 69 6e 67 20 2a 2f 0d 0a 20 20 77 69 6e 63 65  uing */..  wince
13560 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
13561 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0d 0a 20 20  le->hMutex);..  
13562 0d 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65  ..  /* Since the
13563 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20   names of named 
13564 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70 68 6f  mutexes, semapho
13565 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e  res, file mappin
13566 67 73 20 65 74 63 20 61 72 65 20 0d 0a 20 20 2a  gs etc are ..  *
13567 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76 65  * case-sensitive
13568 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  , take advantage
13569 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70 65   of that by uppe
1356a 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65  rcasing the mute
1356b 78 20 6e 61 6d 65 0d 0a 20 20 2a 2a 20 61 6e 64  x name..  ** and
1356c 20 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74   using that as t
1356d 68 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61  he shared filema
1356e 70 70 69 6e 67 20 6e 61 6d 65 2e 0d 0a 20 20 2a  pping name...  *
1356f 2f 0d 0a 20 20 6f 73 43 68 61 72 55 70 70 65 72  /..  osCharUpper
13570 57 28 7a 4e 61 6d 65 29 3b 0d 0a 20 20 70 46 69  W(zName);..  pFi
13571 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 6f 73  le->hShared = os
13572 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
13573 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c  gW(INVALID_HANDL
13574 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0d 0a  E_VALUE, NULL,..
13575 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13577 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 45 41          PAGE_REA
13578 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f  DWRITE, 0, sizeo
13579 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0d 0a 20  f(winceLock),.. 
1357a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1357b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1357c 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20         zName);  
1357d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 74 20 61 20 66  ....  /* Set a f
1357e 6c 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74  lag that indicat
1357f 65 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72  es we're the fir
13580 73 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  st to create the
13581 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0d 0a   memory so it ..
13582 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72    ** must be zer
13583 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  o-initialized */
13584 0d 0a 20 20 69 66 20 28 6f 73 47 65 74 4c 61 73  ..  if (osGetLas
13585 74 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f  tError() == ERRO
13586 52 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53  R_ALREADY_EXISTS
13587 29 7b 0d 0a 20 20 20 20 62 49 6e 69 74 20 3d 20  ){..    bInit = 
13588 46 41 4c 53 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  FALSE;..  }.... 
13589 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e   sqlite3_free(zN
1358a 61 6d 65 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ame);....  /* If
1358b 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e   we succeeded in
1358c 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 72   making the shar
1358d 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65  ed memory handle
1358e 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0d 0a 20 20  , map it. */..  
1358f 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  if (pFile->hShar
13590 65 64 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  ed){..    pFile-
13591 3e 73 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65  >shared = (wince
13592 4c 6f 63 6b 2a 29 6f 73 4d 61 70 56 69 65 77 4f  Lock*)osMapViewO
13593 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68  fFile(pFile->hSh
13594 61 72 65 64 2c 20 0d 0a 20 20 20 20 20 20 20 20  ared, ..        
13595 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45       FILE_MAP_RE
13596 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54  AD|FILE_MAP_WRIT
13597 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  E, 0, 0, sizeof(
13598 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0d 0a 20 20  winceLock));..  
13599 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20    /* If mapping 
1359a 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68  failed, close th
1359b 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  e shared memory 
1359c 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65  handle and erase
1359d 20 69 74 20 2a 2f 0d 0a 20 20 20 20 69 66 20 28   it */..    if (
1359e 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b  !pFile->shared){
1359f 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ..      pFile->l
135a0 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74  astErrno = osGet
135a1 4c 61 73 74 45 72 72 6f 72 28 29 3b 0d 0a 20 20  LastError();..  
135a2 20 20 20 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28      winLogError(
135a3 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 70 46  SQLITE_ERROR, pF
135a4 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0d  ile->lastErrno,.
135a5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
135a6 22 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b  "winceCreateLock
135a7 32 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0d  2", zFilename);.
135a8 0a 20 20 20 20 20 20 6f 73 43 6c 6f 73 65 48 61  .      osCloseHa
135a9 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61  ndle(pFile->hSha
135aa 72 65 64 29 3b 0d 0a 20 20 20 20 20 20 70 46 69  red);..      pFi
135ab 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55  le->hShared = NU
135ac 4c 4c 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  LL;..    }..  }.
135ad 0a 0d 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65  ...  /* If share
135ae 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e  d memory could n
135af 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74  ot be created, t
135b0 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75  hen close the mu
135b1 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0d  tex and fail */.
135b2 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53  .  if (pFile->hS
135b3 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0d  hared == NULL){.
135b4 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
135b5 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
135b6 75 74 65 78 29 3b 0d 0a 20 20 20 20 6f 73 43 6c  utex);..    osCl
135b7 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
135b8 3e 68 4d 75 74 65 78 29 3b 0d 0a 20 20 20 20 70  >hMutex);..    p
135b9 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e  File->hMutex = N
135ba 55 4c 4c 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ULL;..    return
135bb 20 46 41 4c 53 45 3b 0d 0a 20 20 7d 0d 0a 20 20   FALSE;..  }..  
135bc 0d 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
135bd 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d  e the shared mem
135be 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 70  ory if we're sup
135bf 70 6f 73 65 64 20 74 6f 20 2a 2f 0d 0a 20 20 69  posed to */..  i
135c0 66 20 28 62 49 6e 69 74 29 20 7b 0d 0a 20 20 20  f (bInit) {..   
135c1 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2d 3e 73   memset(pFile->s
135c2 68 61 72 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66  hared, 0, sizeof
135c3 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0d 0a 20  (winceLock));.. 
135c4 20 7d 0d 0a 0d 0a 20 20 77 69 6e 63 65 4d 75 74   }....  winceMut
135c5 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
135c6 3e 68 4d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74  >hMutex);..  ret
135c7 75 72 6e 20 54 52 55 45 3b 0d 0a 7d 0d 0a 0d 0a  urn TRUE;..}....
135c8 2f 2a 0d 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74  /*..** Destroy t
135c9 68 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69  he part of winFi
135ca 6c 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69  le that deals wi
135cb 74 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0d 0a  th wince locks..
135cc 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
135cd 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b  winceDestroyLock
135ce 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29  (winFile *pFile)
135cf 7b 0d 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  {..  if (pFile->
135d0 68 4d 75 74 65 78 29 7b 0d 0a 20 20 20 20 2f 2a  hMutex){..    /*
135d1 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
135d2 65 78 20 2a 2f 0d 0a 20 20 20 20 77 69 6e 63 65  ex */..    wince
135d3 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
135d4 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0d 0a 0d 0a  le->hMutex);....
135d5 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
135d6 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75  wing blocks shou
135d7 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65  ld probably asse
135d8 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65  rt in debug mode
135d9 2c 20 62 75 74 20 74 68 65 79 0d 0a 20 20 20 20  , but they..    
135da 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75     are to cleanu
135db 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f  p in case any lo
135dc 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65  cks remained ope
135dd 6e 20 2a 2f 0d 0a 20 20 20 20 69 66 20 28 70 46  n */..    if (pF
135de 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
135df 65 72 73 29 7b 0d 0a 20 20 20 20 20 20 70 46 69  ers){..      pFi
135e0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61  le->shared->nRea
135e1 64 65 72 73 20 2d 2d 3b 0d 0a 20 20 20 20 7d 0d  ders --;..    }.
135e2 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
135e3 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29  local.bReserved)
135e4 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  {..      pFile->
135e5 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
135e6 64 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20 20 20  d = FALSE;..    
135e7 7d 0d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  }..    if (pFile
135e8 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67  ->local.bPending
135e9 29 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ){..      pFile-
135ea 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e  >shared->bPendin
135eb 67 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20 20 20  g = FALSE;..    
135ec 7d 0d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  }..    if (pFile
135ed 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69  ->local.bExclusi
135ee 76 65 29 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c  ve){..      pFil
135ef 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
135f0 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0d 0a  usive = FALSE;..
135f1 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
135f2 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64  De-reference and
135f3 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20   close our copy 
135f4 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d 65  of the shared me
135f5 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a  mory handle */..
135f6 20 20 20 20 6f 73 55 6e 6d 61 70 56 69 65 77 4f      osUnmapViewO
135f7 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61  fFile(pFile->sha
135f8 72 65 64 29 3b 0d 0a 20 20 20 20 6f 73 43 6c 6f  red);..    osClo
135f9 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e  seHandle(pFile->
135fa 68 53 68 61 72 65 64 29 3b 0d 0a 0d 0a 20 20 20  hShared);....   
135fb 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68   /* Done with th
135fc 65 20 6d 75 74 65 78 20 2a 2f 0d 0a 20 20 20 20  e mutex */..    
135fd 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73  winceMutexReleas
135fe 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
135ff 3b 20 20 20 20 0d 0a 20 20 20 20 6f 73 43 6c 6f  ;    ..    osClo
13600 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e  seHandle(pFile->
13601 68 4d 75 74 65 78 29 3b 0d 0a 20 20 20 20 70 46  hMutex);..    pF
13602 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55  ile->hMutex = NU
13603 4c 4c 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  LL;..  }..}..../
13604 2a 20 0d 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  * ..** An implem
13605 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
13606 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 6f  LockFile() API o
13607 66 20 57 69 6e 64 6f 77 73 20 66 6f 72 20 43 45  f Windows for CE
13608 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 42 4f 4f  ..*/..static BOO
13609 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28  L winceLockFile(
1360a 0d 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69  ..  HANDLE *phFi
1360b 6c 65 2c 0d 0a 20 20 44 57 4f 52 44 20 64 77 46  le,..  DWORD dwF
1360c 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0d 0a 20  ileOffsetLow,.. 
1360d 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
1360e 73 65 74 48 69 67 68 2c 0d 0a 20 20 44 57 4f 52  setHigh,..  DWOR
1360f 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
13610 54 6f 4c 6f 63 6b 4c 6f 77 2c 0d 0a 20 20 44 57  ToLockLow,..  DW
13611 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
13612 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0d 0a 29 7b  esToLockHigh..){
13613 0d 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ..  winFile *pFi
13614 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  le = HANDLE_TO_W
13615 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0d  INFILE(phFile);.
13616 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20  .  BOOL bReturn 
13617 3d 20 46 41 4c 53 45 3b 0d 0a 0d 0a 20 20 55 4e  = FALSE;....  UN
13618 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
13619 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29  wFileOffsetHigh)
1361a 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
1361b 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42  METER(nNumberOfB
1361c 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b  ytesToLockHigh);
1361d 0d 0a 0d 0a 20 20 69 66 20 28 21 70 46 69 6c 65  ....  if (!pFile
1361e 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e  ->hMutex) return
1361f 20 54 52 55 45 3b 0d 0a 20 20 77 69 6e 63 65 4d   TRUE;..  winceM
13620 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
13621 65 2d 3e 68 4d 75 74 65 78 29 3b 0d 0a 0d 0a 20  e->hMutex);.... 
13622 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65   /* Wanting an e
13623 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a  xclusive lock? *
13624 2f 0d 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  /..  if (dwFileO
13625 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
13626 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 0d  RD)SHARED_FIRST.
13627 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62  .       && nNumb
13628 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
13629 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41  ow == (DWORD)SHA
1362a 52 45 44 5f 53 49 5a 45 29 7b 0d 0a 20 20 20 20  RED_SIZE){..    
1362b 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
1362c 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  d->nReaders == 0
1362d 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65   && pFile->share
1362e 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d  d->bExclusive ==
1362f 20 30 29 7b 0d 0a 20 20 20 20 20 20 20 70 46 69   0){..       pFi
13630 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
13631 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0d 0a  lusive = TRUE;..
13632 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f         pFile->lo
13633 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d  cal.bExclusive =
13634 20 54 52 55 45 3b 0d 0a 20 20 20 20 20 20 20 62   TRUE;..       b
13635 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0d 0a  Return = TRUE;..
13636 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
13637 2f 2a 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f  /* Want a read-o
13638 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0d 0a 20 20  nly lock? */..  
13639 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f  else if (dwFileO
1363a 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
1363b 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 20  RD)SHARED_FIRST 
1363c 26 26 0d 0a 20 20 20 20 20 20 20 20 20 20 20 6e  &&..           n
1363d 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
1363e 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0d 0a 20  ockLow == 1){.. 
1363f 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68     if (pFile->sh
13640 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65  ared->bExclusive
13641 20 3d 3d 20 30 29 7b 0d 0a 20 20 20 20 20 20 70   == 0){..      p
13642 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
13643 64 65 72 73 20 2b 2b 3b 0d 0a 20 20 20 20 20 20  ders ++;..      
13644 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
13645 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 31 29 7b  .nReaders == 1){
13646 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ..        pFile-
13647 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
13648 73 20 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  s ++;..      }..
13649 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
1364a 54 52 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  TRUE;..    }..  
1364b 7d 0d 0a 0d 0a 20 20 2f 2a 20 57 61 6e 74 20 61  }....  /* Want a
1364c 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a   pending lock? *
1364d 2f 0d 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77  /..  else if (dw
1364e 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d  FileOffsetLow ==
1364f 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f   (DWORD)PENDING_
13650 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f  BYTE && nNumberO
13651 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20  fBytesToLockLow 
13652 3d 3d 20 31 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  == 1){..    /* I
13653 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f 63  f no pending loc
13654 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 69  k has been acqui
13655 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 72  red, then acquir
13656 65 20 69 74 20 2a 2f 0d 0a 20 20 20 20 69 66 20  e it */..    if 
13657 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
13658 62 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b  bPending == 0) {
13659 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  ..      pFile->s
1365a 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20  hared->bPending 
1365b 3d 20 54 52 55 45 3b 0d 0a 20 20 20 20 20 20 70  = TRUE;..      p
1365c 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
1365d 64 69 6e 67 20 3d 20 54 52 55 45 3b 0d 0a 20 20  ding = TRUE;..  
1365e 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
1365f 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  UE;..    }..  }.
13660 0a 0d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72  ...  /* Want a r
13661 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f  eserved lock? */
13662 0d 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  ..  else if (dwF
13663 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
13664 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44 5f  (DWORD)RESERVED_
13665 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f  BYTE && nNumberO
13666 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20  fBytesToLockLow 
13667 3d 3d 20 31 29 7b 0d 0a 20 20 20 20 69 66 20 28  == 1){..    if (
13668 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
13669 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20 7b  Reserved == 0) {
1366a 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  ..      pFile->s
1366b 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64  hared->bReserved
1366c 20 3d 20 54 52 55 45 3b 0d 0a 20 20 20 20 20 20   = TRUE;..      
1366d 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65  pFile->local.bRe
1366e 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0d 0a  served = TRUE;..
1366f 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
13670 54 52 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  TRUE;..    }..  
13671 7d 0d 0a 0d 0a 20 20 77 69 6e 63 65 4d 75 74 65  }....  winceMute
13672 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
13673 68 4d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75  hMutex);..  retu
13674 72 6e 20 62 52 65 74 75 72 6e 3b 0d 0a 7d 0d 0a  rn bReturn;..}..
13675 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6d 70 6c  ../*..** An impl
13676 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
13677 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49  e UnlockFile API
13678 20 6f 66 20 57 69 6e 64 6f 77 73 20 66 6f 72 20   of Windows for 
13679 43 45 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 42  CE..*/..static B
1367a 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46  OOL winceUnlockF
1367b 69 6c 65 28 0d 0a 20 20 48 41 4e 44 4c 45 20 2a  ile(..  HANDLE *
1367c 70 68 46 69 6c 65 2c 0d 0a 20 20 44 57 4f 52 44  phFile,..  DWORD
1367d 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
1367e 2c 0d 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c  ,..  DWORD dwFil
1367f 65 4f 66 66 73 65 74 48 69 67 68 2c 0d 0a 20 20  eOffsetHigh,..  
13680 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
13681 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c  ytesToUnlockLow,
13682 0d 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ..  DWORD nNumbe
13683 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
13684 48 69 67 68 0d 0a 29 7b 0d 0a 20 20 77 69 6e 46  High..){..  winF
13685 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e  ile *pFile = HAN
13686 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70  DLE_TO_WINFILE(p
13687 68 46 69 6c 65 29 3b 0d 0a 20 20 42 4f 4f 4c 20  hFile);..  BOOL 
13688 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b  bReturn = FALSE;
13689 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  ....  UNUSED_PAR
1368a 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 66  AMETER(dwFileOff
1368b 73 65 74 48 69 67 68 29 3b 0d 0a 20 20 55 4e 55  setHigh);..  UNU
1368c 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e  SED_PARAMETER(nN
1368d 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
1368e 6c 6f 63 6b 48 69 67 68 29 3b 0d 0a 0d 0a 20 20  lockHigh);....  
1368f 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74  if (!pFile->hMut
13690 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b  ex) return TRUE;
13691 0d 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63  ..  winceMutexAc
13692 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75  quire(pFile->hMu
13693 74 65 78 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65  tex);....  /* Re
13694 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 65 72  leasing a reader
13695 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c   lock or an excl
13696 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0d 0a 20  usive lock */.. 
13697 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
13698 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  tLow == (DWORD)S
13699 48 41 52 45 44 5f 46 49 52 53 54 29 7b 0d 0a 20  HARED_FIRST){.. 
1369a 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76     /* Did we hav
1369b 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  e an exclusive l
1369c 6f 63 6b 3f 20 2a 2f 0d 0a 20 20 20 20 69 66 20  ock? */..    if 
1369d 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45  (pFile->local.bE
1369e 78 63 6c 75 73 69 76 65 29 7b 0d 0a 20 20 20 20  xclusive){..    
1369f 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65 72    assert(nNumber
136a0 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c  OfBytesToUnlockL
136a1 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41  ow == (DWORD)SHA
136a2 52 45 44 5f 53 49 5a 45 29 3b 0d 0a 20 20 20 20  RED_SIZE);..    
136a3 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
136a4 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
136a5 45 3b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  E;..      pFile-
136a6 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73  >shared->bExclus
136a7 69 76 65 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20  ive = FALSE;..  
136a8 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
136a9 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  UE;..    }....  
136aa 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74    /* Did we just
136ab 20 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c   have a reader l
136ac 6f 63 6b 3f 20 2a 2f 0d 0a 20 20 20 20 65 6c 73  ock? */..    els
136ad 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63  e if (pFile->loc
136ae 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0d 0a 20  al.nReaders){.. 
136af 20 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d       assert(nNum
136b0 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f  berOfBytesToUnlo
136b1 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  ckLow == (DWORD)
136b2 53 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e  SHARED_SIZE || n
136b3 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
136b4 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0d  nlockLow == 1);.
136b5 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
136b6 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b  cal.nReaders --;
136b7 0d 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 6c  ..      if (pFil
136b8 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
136b9 73 20 3d 3d 20 30 29 0d 0a 20 20 20 20 20 20 7b  s == 0)..      {
136ba 0d 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ..        pFile-
136bb 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
136bc 73 20 2d 2d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  s --;..      }..
136bd 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
136be 54 52 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  TRUE;..    }..  
136bf 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6c 65 61 73  }....  /* Releas
136c0 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  ing a pending lo
136c1 63 6b 20 2a 2f 0d 0a 20 20 65 6c 73 65 20 69 66  ck */..  else if
136c2 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
136c3 77 20 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44  w == (DWORD)PEND
136c4 49 4e 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d  ING_BYTE && nNum
136c5 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f  berOfBytesToUnlo
136c6 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0d 0a 20 20  ckLow == 1){..  
136c7 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
136c8 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0d 0a 20  al.bPending){.. 
136c9 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
136ca 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c  l.bPending = FAL
136cb 53 45 3b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65  SE;..      pFile
136cc 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69  ->shared->bPendi
136cd 6e 67 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20 20  ng = FALSE;..   
136ce 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
136cf 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  E;..    }..  }..
136d0 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61    /* Releasing a
136d1 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a   reserved lock *
136d2 2f 0d 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77  /..  else if (dw
136d3 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d  FileOffsetLow ==
136d4 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44   (DWORD)RESERVED
136d5 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72  _BYTE && nNumber
136d6 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c  OfBytesToUnlockL
136d7 6f 77 20 3d 3d 20 31 29 7b 0d 0a 20 20 20 20 69  ow == 1){..    i
136d8 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
136d9 62 52 65 73 65 72 76 65 64 29 20 7b 0d 0a 20 20  bReserved) {..  
136da 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
136db 2e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c  .bReserved = FAL
136dc 53 45 3b 0d 0a 20 20 20 20 20 20 70 46 69 6c 65  SE;..      pFile
136dd 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72  ->shared->bReser
136de 76 65 64 20 3d 20 46 41 4c 53 45 3b 0d 0a 20 20  ved = FALSE;..  
136df 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
136e0 55 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  UE;..    }..  }.
136e1 0a 0d 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52  ...  winceMutexR
136e2 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
136e3 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
136e4 20 62 52 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 0d 0a   bReturn;..}....
136e5 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  /*..** An implem
136e6 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
136e7 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49  LockFileEx() API
136e8 20 6f 66 20 57 69 6e 64 6f 77 73 20 66 6f 72 20   of Windows for 
136e9 43 45 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 42  CE..*/..static B
136ea 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  OOL winceLockFil
136eb 65 45 78 28 0d 0a 20 20 48 41 4e 44 4c 45 20 2a  eEx(..  HANDLE *
136ec 70 68 46 69 6c 65 2c 0d 0a 20 20 44 57 4f 52 44  phFile,..  DWORD
136ed 20 64 77 46 6c 61 67 73 2c 0d 0a 20 20 44 57 4f   dwFlags,..  DWO
136ee 52 44 20 64 77 52 65 73 65 72 76 65 64 2c 0d 0a  RD dwReserved,..
136ef 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f    DWORD nNumberO
136f0 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c  fBytesToLockLow,
136f1 0d 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ..  DWORD nNumbe
136f2 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69  rOfBytesToLockHi
136f3 67 68 2c 0d 0a 20 20 4c 50 4f 56 45 52 4c 41 50  gh,..  LPOVERLAP
136f4 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64  PED lpOverlapped
136f5 0d 0a 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  ..){..  UNUSED_P
136f6 41 52 41 4d 45 54 45 52 28 64 77 52 65 73 65 72  ARAMETER(dwReser
136f7 76 65 64 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f  ved);..  UNUSED_
136f8 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65  PARAMETER(nNumbe
136f9 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69  rOfBytesToLockHi
136fa 67 68 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  gh);....  /* If 
136fb 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73  the caller wants
136fc 20 61 20 73 68 61 72 65 64 20 72 65 61 64 20 6c   a shared read l
136fd 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69  ock, forward thi
136fe 73 20 63 61 6c 6c 0d 0a 20 20 2a 2a 20 74 6f 20  s call..  ** to 
136ff 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f  winceLockFile */
13700 0d 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61  ..  if (lpOverla
13701 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20  pped->Offset == 
13702 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49  (DWORD)SHARED_FI
13703 52 53 54 20 26 26 0d 0a 20 20 20 20 20 20 64 77  RST &&..      dw
13704 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0d 0a 20  Flags == 1 &&.. 
13705 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79       nNumberOfBy
13706 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
13707 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53 49  (DWORD)SHARED_SI
13708 5a 45 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ZE){..    return
13709 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70   winceLockFile(p
1370a 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  hFile, SHARED_FI
1370b 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0d 0a  RST, 0, 1, 0);..
1370c 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 46 41    }..  return FA
1370d 4c 53 45 3b 0d 0a 7d 0d 0a 2f 2a 0d 0a 2a 2a 20  LSE;..}../*..** 
1370e 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69  End of the speci
1370f 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63  al code for winc
13710 65 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..*************
13711 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13712 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13713 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13714 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13715 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  /..#endif /* SQL
13716 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0d  ITE_OS_WINCE */.
13717 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
13718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13719 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1371a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1371b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1371c 2a 0d 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  *..** The next g
1371d 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73  roup of routines
1371e 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49   implement the I
1371f 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69  /O methods speci
13720 66 69 65 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20  fied..** by the 
13721 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
13722 64 73 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 2a 2a  ds object...****
13723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13724 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13726 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13727 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f  **********/..../
13728 2a 0d 0a 2a 2a 20 53 6f 6d 65 20 4d 69 63 72 6f  *..** Some Micro
13729 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c  soft compilers l
1372a 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74  ack this definit
1372b 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  ion...*/..#ifnde
1372c 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  f INVALID_SET_FI
1372d 4c 45 5f 50 4f 49 4e 54 45 52 0d 0a 23 20 64 65  LE_POINTER..# de
1372e 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54  fine INVALID_SET
1372f 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28  _FILE_POINTER ((
13730 44 57 4f 52 44 29 2d 31 29 0d 0a 23 65 6e 64 69  DWORD)-1)..#endi
13731 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 6f 76 65  f..../*..** Move
13732 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73   the current pos
13733 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 6c  ition of the fil
13734 65 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20  e handle passed 
13735 61 73 20 74 68 65 20 66 69 72 73 74 20 0d 0a 2a  as the first ..*
13736 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6f 66  * argument to of
13737 66 73 65 74 20 69 4f 66 66 73 65 74 20 77 69 74  fset iOffset wit
13738 68 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66  hin the file. If
13739 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74   successful, ret
1373a 75 72 6e 20 30 2e 20 0d 0a 2a 2a 20 4f 74 68 65  urn 0. ..** Othe
1373b 72 77 69 73 65 2c 20 73 65 74 20 70 46 69 6c 65  rwise, set pFile
1373c 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 61 6e 64 20  ->lastErrno and 
1373d 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
1373e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1373f 20 73 65 65 6b 57 69 6e 46 69 6c 65 28 77 69 6e   seekWinFile(win
13740 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 73 71 6c  File *pFile, sql
13741 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73  ite3_int64 iOffs
13742 65 74 29 7b 0d 0a 20 20 4c 4f 4e 47 20 75 70 70  et){..  LONG upp
13743 65 72 42 69 74 73 3b 20 20 20 20 20 20 20 20 20  erBits;         
13744 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20          /* Most 
13745 73 69 67 2e 20 33 32 20 62 69 74 73 20 6f 66 20  sig. 32 bits of 
13746 6e 65 77 20 6f 66 66 73 65 74 20 2a 2f 0d 0a 20  new offset */.. 
13747 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 3b   LONG lowerBits;
13748 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13749 20 2f 2a 20 4c 65 61 73 74 20 73 69 67 2e 20 33   /* Least sig. 3
1374a 32 20 62 69 74 73 20 6f 66 20 6e 65 77 20 6f 66  2 bits of new of
1374b 66 73 65 74 20 2a 2f 0d 0a 20 20 44 57 4f 52 44  fset */..  DWORD
1374c 20 64 77 52 65 74 3b 20 20 20 20 20 20 20 20 20   dwRet;         
1374d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
1374e 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
1374f 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 29  SetFilePointer()
13750 20 2a 2f 0d 0a 20 20 44 57 4f 52 44 20 6c 61 73   */..  DWORD las
13751 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20  tErrno;         
13752 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
13753 72 65 74 75 72 6e 65 64 20 62 79 20 47 65 74 4c  returned by GetL
13754 61 73 74 45 72 72 6f 72 28 29 20 2a 2f 0d 0a 0d  astError() */...
13755 0a 20 20 75 70 70 65 72 42 69 74 73 20 3d 20 28  .  upperBits = (
13756 4c 4f 4e 47 29 28 28 69 4f 66 66 73 65 74 3e 3e  LONG)((iOffset>>
13757 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
13758 29 3b 0d 0a 20 20 6c 6f 77 65 72 42 69 74 73 20  );..  lowerBits 
13759 3d 20 28 4c 4f 4e 47 29 28 69 4f 66 66 73 65 74  = (LONG)(iOffset
1375a 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0d   & 0xffffffff);.
1375b 0a 0d 0a 20 20 2f 2a 20 41 50 49 20 6f 64 64 69  ...  /* API oddi
1375c 74 79 3a 20 49 66 20 73 75 63 63 65 73 73 66 75  ty: If successfu
1375d 6c 2c 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65  l, SetFilePointe
1375e 72 28 29 20 72 65 74 75 72 6e 73 20 61 20 64 77  r() returns a dw
1375f 6f 72 64 20 0d 0a 20 20 2a 2a 20 63 6f 6e 74 61  ord ..  ** conta
13760 69 6e 69 6e 67 20 74 68 65 20 6c 6f 77 65 72 20  ining the lower 
13761 33 32 2d 62 69 74 73 20 6f 66 20 74 68 65 20 6e  32-bits of the n
13762 65 77 20 66 69 6c 65 2d 6f 66 66 73 65 74 2e 20  ew file-offset. 
13763 4f 72 2c 20 69 66 20 69 74 20 66 61 69 6c 73 2c  Or, if it fails,
13764 0d 0a 20 20 2a 2a 20 69 74 20 72 65 74 75 72 6e  ..  ** it return
13765 73 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  s INVALID_SET_FI
13766 4c 45 5f 50 4f 49 4e 54 45 52 2e 20 48 6f 77 65  LE_POINTER. Howe
13767 76 65 72 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ver according to
13768 20 4d 53 44 4e 2c 20 0d 0a 20 20 2a 2a 20 49 4e   MSDN, ..  ** IN
13769 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
1376a 4f 49 4e 54 45 52 20 6d 61 79 20 61 6c 73 6f 20  OINTER may also 
1376b 62 65 20 61 20 76 61 6c 69 64 20 6e 65 77 20 6f  be a valid new o
1376c 66 66 73 65 74 2e 20 53 6f 20 74 6f 20 64 65 74  ffset. So to det
1376d 65 72 6d 69 6e 65 20 0d 0a 20 20 2a 2a 20 77 68  ermine ..  ** wh
1376e 65 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 68  ether an error h
1376f 61 73 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75  as actually occu
13770 72 65 64 2c 20 69 74 20 69 73 20 61 6c 73 6f 20  red, it is also 
13771 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 61 6c  necessary to cal
13772 6c 20 0d 0a 20 20 2a 2a 20 47 65 74 4c 61 73 74  l ..  ** GetLast
13773 45 72 72 6f 72 28 29 2e 0d 0a 20 20 2a 2f 0d 0a  Error()...  */..
13774 20 20 64 77 52 65 74 20 3d 20 6f 73 53 65 74 46    dwRet = osSetF
13775 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65  ilePointer(pFile
13776 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20  ->h, lowerBits, 
13777 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45  &upperBits, FILE
13778 5f 42 45 47 49 4e 29 3b 0d 0a 0d 0a 20 20 69 66  _BEGIN);....  if
13779 28 20 28 64 77 52 65 74 3d 3d 49 4e 56 41 4c 49  ( (dwRet==INVALI
1377a 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54  D_SET_FILE_POINT
1377b 45 52 0d 0a 20 20 20 20 20 20 26 26 20 28 28 6c  ER..      && ((l
1377c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74  astErrno = osGet
1377d 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f  LastError())!=NO
1377e 5f 45 52 52 4f 52 29 29 20 29 7b 0d 0a 20 20 20  _ERROR)) ){..   
1377f 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
13780 6f 20 3d 20 6c 61 73 74 45 72 72 6e 6f 3b 0d 0a  o = lastErrno;..
13781 20 20 20 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28      winLogError(
13782 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 45 45  SQLITE_IOERR_SEE
13783 4b 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72  K, pFile->lastEr
13784 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  rno,..          
13785 20 20 20 22 73 65 65 6b 57 69 6e 46 69 6c 65 22     "seekWinFile"
13786 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b  , pFile->zPath);
13787 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d  ..    return 1;.
13788 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
13789 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
1378a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0d 0a   Close a file...
1378b 2a 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 72 65 70  **..** It is rep
1378c 6f 72 74 65 64 20 74 68 61 74 20 61 6e 20 61 74  orted that an at
1378d 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61  tempt to close a
1378e 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f   handle might so
1378f 6d 65 74 69 6d 65 73 0d 0a 2a 2a 20 66 61 69 6c  metimes..** fail
13790 2e 20 20 54 68 69 73 20 69 73 20 61 20 76 65 72  .  This is a ver
13791 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72  y unreasonable r
13792 65 73 75 6c 74 2c 20 62 75 74 20 57 69 6e 64 6f  esult, but Windo
13793 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0d  ws is notorious.
13794 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 6e  .** for being un
13795 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20  reasonable so I 
13796 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 61  do not doubt tha
13797 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 70 65  t it might happe
13798 6e 2e 20 20 49 66 0d 0a 2a 2a 20 74 68 65 20 63  n.  If..** the c
13799 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 70  lose fails, we p
1379a 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 6c  ause for 100 mil
1379b 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72  liseconds and tr
1379c 79 20 61 67 61 69 6e 2e 20 20 41 73 0d 0a 2a 2a  y again.  As..**
1379d 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53   many as MX_CLOS
1379e 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70  E_ATTEMPT attemp
1379f 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ts to close the 
137a0 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20  handle are made 
137a1 62 65 66 6f 72 65 0d 0a 2a 2a 20 67 69 76 69 6e  before..** givin
137a2 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69  g up and returni
137a3 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0d 0a 2a 2f  ng an error...*/
137a4 0d 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f  ..#define MX_CLO
137a5 53 45 5f 41 54 54 45 4d 50 54 20 33 0d 0a 73 74  SE_ATTEMPT 3..st
137a6 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73  atic int winClos
137a7 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
137a8 69 64 29 7b 0d 0a 20 20 69 6e 74 20 72 63 2c 20  id){..  int rc, 
137a9 63 6e 74 20 3d 20 30 3b 0d 0a 20 20 77 69 6e 46  cnt = 0;..  winF
137aa 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
137ab 6e 46 69 6c 65 2a 29 69 64 3b 0d 0a 0d 0a 20 20  nFile*)id;....  
137ac 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
137ad 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
137ae 65 2d 3e 70 53 68 6d 3d 3d 30 20 29 3b 0d 0a 20  e->pShm==0 );.. 
137af 20 4f 53 54 52 41 43 45 28 28 22 43 4c 4f 53 45   OSTRACE(("CLOSE
137b0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
137b1 29 29 3b 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20 20  ));..  do{..    
137b2 72 63 20 3d 20 6f 73 43 6c 6f 73 65 48 61 6e 64  rc = osCloseHand
137b3 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0d 0a 20  le(pFile->h);.. 
137b4 20 20 20 2f 2a 20 53 69 6d 75 6c 61 74 65 49 4f     /* SimulateIO
137b5 45 72 72 6f 72 28 20 72 63 3d 30 3b 20 63 6e 74  Error( rc=0; cnt
137b6 3d 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50  =MX_CLOSE_ATTEMP
137b7 54 3b 20 29 3b 20 2a 2f 0d 0a 20 20 7d 77 68 69  T; ); */..  }whi
137b8 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63  le( rc==0 && ++c
137b9 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54  nt < MX_CLOSE_AT
137ba 54 45 4d 50 54 20 26 26 20 28 6f 73 53 6c 65 65  TEMPT && (osSlee
137bb 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0d 0a 23  p(100), 1) );..#
137bc 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
137bd 43 45 0d 0a 23 64 65 66 69 6e 65 20 57 49 4e 43  CE..#define WINC
137be 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  E_DELETION_ATTEM
137bf 50 54 53 20 33 0d 0a 20 20 77 69 6e 63 65 44 65  PTS 3..  winceDe
137c0 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29  stroyLock(pFile)
137c1 3b 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  ;..  if( pFile->
137c2 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29  zDeleteOnClose )
137c3 7b 0d 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  {..    int cnt =
137c4 20 30 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 0d   0;..    while(.
137c5 0a 20 20 20 20 20 20 20 20 20 20 20 6f 73 44 65  .           osDe
137c6 6c 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d  leteFileW(pFile-
137c7 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29  >zDeleteOnClose)
137c8 3d 3d 30 0d 0a 20 20 20 20 20 20 20 20 26 26 20  ==0..        && 
137c9 6f 73 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  osGetFileAttribu
137ca 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c  tesW(pFile->zDel
137cb 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66  eteOnClose)!=0xf
137cc 66 66 66 66 66 66 66 20 0d 0a 20 20 20 20 20 20  fffffff ..      
137cd 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e    && cnt++ < WIN
137ce 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  CE_DELETION_ATTE
137cf 4d 50 54 53 0d 0a 20 20 20 20 29 7b 0d 0a 20 20  MPTS..    ){..  
137d0 20 20 20 20 20 6f 73 53 6c 65 65 70 28 31 30 30       osSleep(100
137d1 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69  );  /* Wait a li
137d2 74 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69  ttle before tryi
137d3 6e 67 20 61 67 61 69 6e 20 2a 2f 0d 0a 20 20 20  ng again */..   
137d4 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
137d5 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c  free(pFile->zDel
137d6 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0d 0a 20 20  eteOnClose);..  
137d7 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 4f 53 54  }..#endif..  OST
137d8 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 64 20  RACE(("CLOSE %d 
137d9 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
137da 20 72 63 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61   rc ? "ok" : "fa
137db 69 6c 65 64 22 29 29 3b 0d 0a 20 20 4f 70 65 6e  iled"));..  Open
137dc 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0d 0a 20 20  Counter(-1);..  
137dd 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49  return rc ? SQLI
137de 54 45 5f 4f 4b 0d 0a 20 20 20 20 20 20 20 20 20  TE_OK..         
137df 20 20 20 3a 20 77 69 6e 4c 6f 67 45 72 72 6f 72     : winLogError
137e0 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c  (SQLITE_IOERR_CL
137e1 4f 53 45 2c 20 6f 73 47 65 74 4c 61 73 74 45 72  OSE, osGetLastEr
137e2 72 6f 72 28 29 2c 0d 0a 20 20 20 20 20 20 20 20  ror(),..        
137e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
137e4 20 20 22 77 69 6e 43 6c 6f 73 65 22 2c 20 70 46    "winClose", pF
137e5 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0d 0a 7d 0d  ile->zPath);..}.
137e6 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 61 64 20 64  .../*..** Read d
137e7 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20  ata from a file 
137e8 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20  into a buffer.  
137e9 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
137ea 20 69 66 20 61 6c 6c 0d 0a 2a 2a 20 62 79 74 65   if all..** byte
137eb 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
137ec 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
137ed 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
137ee 74 68 69 6e 67 20 67 6f 65 73 0d 0a 2a 2a 20 77  thing goes..** w
137ef 72 6f 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rong...*/..stati
137f0 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0d 0a  c int winRead(..
137f1 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
137f2 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  id,          /* 
137f3 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f  File to read fro
137f4 6d 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 42  m */..  void *pB
137f5 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
137f6 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74     /* Write cont
137f7 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75  ent into this bu
137f8 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 61  ffer */..  int a
137f9 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
137fa 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
137fb 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64  of bytes to read
137fc 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69   */..  sqlite3_i
137fd 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
137fe 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69    /* Begin readi
137ff 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65  ng at this offse
13800 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 77 69 6e 46  t */..){..  winF
13801 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
13802 6e 46 69 6c 65 2a 29 69 64 3b 20 20 2f 2a 20 66  nFile*)id;  /* f
13803 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ile handle */.. 
13804 20 44 57 4f 52 44 20 6e 52 65 61 64 3b 20 20 20   DWORD nRead;   
13805 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13806 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
13807 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 61  tes actually rea
13808 64 20 66 72 6f 6d 20 66 69 6c 65 20 2a 2f 0d 0a  d from file */..
13809 20 20 69 6e 74 20 6e 52 65 74 72 79 20 3d 20 30    int nRetry = 0
1380a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1380b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
1380c 65 74 72 79 73 20 2a 2f 0d 0a 0d 0a 20 20 61 73  etrys */....  as
1380d 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0d 0a  sert( id!=0 );..
1380e 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
1380f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
13810 49 4f 45 52 52 5f 52 45 41 44 29 3b 0d 0a 20 20  IOERR_READ);..  
13811 4f 53 54 52 41 43 45 28 28 22 52 45 41 44 20 25  OSTRACE(("READ %
13812 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
13813 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
13814 6f 63 6b 74 79 70 65 29 29 3b 0d 0a 0d 0a 20 20  ocktype));....  
13815 69 66 28 20 73 65 65 6b 57 69 6e 46 69 6c 65 28  if( seekWinFile(
13816 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 29 20 29  pFile, offset) )
13817 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  {..    return SQ
13818 4c 49 54 45 5f 46 55 4c 4c 3b 0d 0a 20 20 7d 0d  LITE_FULL;..  }.
13819 0a 20 20 77 68 69 6c 65 28 20 21 6f 73 52 65 61  .  while( !osRea
1381a 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  dFile(pFile->h, 
1381b 70 42 75 66 2c 20 61 6d 74 2c 20 26 6e 52 65 61  pBuf, amt, &nRea
1381c 64 2c 20 30 29 20 29 7b 0d 0a 20 20 20 20 44 57  d, 0) ){..    DW
1381d 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0d 0a  ORD lastErrno;..
1381e 20 20 20 20 69 66 28 20 72 65 74 72 79 49 6f 65      if( retryIoe
1381f 72 72 28 26 6e 52 65 74 72 79 2c 20 26 6c 61 73  rr(&nRetry, &las
13820 74 45 72 72 6e 6f 29 20 29 20 63 6f 6e 74 69 6e  tErrno) ) contin
13821 75 65 3b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ue;..    pFile->
13822 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74  lastErrno = last
13823 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 72 65 74 75  Errno;..    retu
13824 72 6e 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53  rn winLogError(S
13825 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
13826 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  , pFile->lastErr
13827 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  no,..           
13828 20 20 22 77 69 6e 52 65 61 64 22 2c 20 70 46 69    "winRead", pFi
13829 6c 65 2d 3e 7a 50 61 74 68 29 3b 0d 0a 20 20 7d  le->zPath);..  }
1382a 0d 0a 20 20 6c 6f 67 49 6f 65 72 72 28 6e 52 65  ..  logIoerr(nRe
1382b 74 72 79 29 3b 0d 0a 20 20 69 66 28 20 6e 52 65  try);..  if( nRe
1382c 61 64 3c 28 44 57 4f 52 44 29 61 6d 74 20 29 7b  ad<(DWORD)amt ){
1382d 0d 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20  ..    /* Unread 
1382e 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66  parts of the buf
1382f 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f  fer must be zero
13830 2d 66 69 6c 6c 65 64 20 2a 2f 0d 0a 20 20 20 20  -filled */..    
13831 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29  memset(&((char*)
13832 70 42 75 66 29 5b 6e 52 65 61 64 5d 2c 20 30 2c  pBuf)[nRead], 0,
13833 20 61 6d 74 2d 6e 52 65 61 64 29 3b 0d 0a 20 20   amt-nRead);..  
13834 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13835 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
13836 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75  ;..  }....  retu
13837 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
13838 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65  ..../*..** Write
13839 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66   data from a buf
1383a 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e  fer into a file.
1383b 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
1383c 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0d 0a 2a  OK on success..*
1383d 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
1383e 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
1383f 69 6c 75 72 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ilure...*/..stat
13840 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 65 28  ic int winWrite(
13841 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ..  sqlite3_file
13842 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
13843 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77      /* File to w
13844 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0d 0a 20 20  rite into */..  
13845 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
13846 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
13847 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20  /* The bytes to 
13848 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0d 0a 20  be written */.. 
13849 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
1384a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1384b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1384c 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0d  tes to write */.
1384d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
1384e 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20   offset         
1384f 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
13850 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  o the file to be
13851 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a  gin writing at *
13852 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  /..){..  int rc;
13853 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13854 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
13855 20 69 66 20 65 72 72 6f 72 20 68 61 73 20 6f 63   if error has oc
13856 63 75 72 65 64 2c 20 65 6c 73 65 20 66 61 6c 73  cured, else fals
13857 65 20 2a 2f 0d 0a 20 20 77 69 6e 46 69 6c 65 20  e */..  winFile 
13858 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
13859 65 2a 29 69 64 3b 20 20 2f 2a 20 46 69 6c 65 20  e*)id;  /* File 
1385a 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74  handle */..  int
1385b 20 6e 52 65 74 72 79 20 3d 20 30 3b 20 20 20 20   nRetry = 0;    
1385c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1385d 4e 75 6d 62 65 72 20 6f 66 20 72 65 74 72 69 65  Number of retrie
1385e 73 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  s */....  assert
1385f 28 20 61 6d 74 3e 30 20 29 3b 0d 0a 20 20 61 73  ( amt>0 );..  as
13860 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0d 0a  sert( pFile );..
13861 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
13862 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
13863 49 4f 45 52 52 5f 57 52 49 54 45 29 3b 0d 0a 20  IOERR_WRITE);.. 
13864 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
13865 6c 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  lError(return SQ
13866 4c 49 54 45 5f 46 55 4c 4c 29 3b 0d 0a 0d 0a 20  LITE_FULL);.... 
13867 20 4f 53 54 52 41 43 45 28 28 22 57 52 49 54 45   OSTRACE(("WRITE
13868 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
13869 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
1386a 3e 6c 6f 63 6b 74 79 70 65 29 29 3b 0d 0a 0d 0a  >locktype));....
1386b 20 20 72 63 20 3d 20 73 65 65 6b 57 69 6e 46 69    rc = seekWinFi
1386c 6c 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 65 74  le(pFile, offset
1386d 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  );..  if( rc==0 
1386e 29 7b 0d 0a 20 20 20 20 75 38 20 2a 61 52 65 6d  ){..    u8 *aRem
1386f 20 3d 20 28 75 38 20 2a 29 70 42 75 66 3b 20 20   = (u8 *)pBuf;  
13870 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 79 65        /* Data ye
13871 74 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  t to be written 
13872 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 52 65 6d  */..    int nRem
13873 20 3d 20 61 6d 74 3b 20 20 20 20 20 20 20 20 20   = amt;         
13874 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
13875 6f 66 20 62 79 74 65 73 20 79 65 74 20 74 6f 20  of bytes yet to 
13876 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0d 0a 20  be written */.. 
13877 20 20 20 44 57 4f 52 44 20 6e 57 72 69 74 65 3b     DWORD nWrite;
13878 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13879 20 2f 2a 20 42 79 74 65 73 20 77 72 69 74 74 65   /* Bytes writte
1387a 6e 20 62 79 20 65 61 63 68 20 57 72 69 74 65 46  n by each WriteF
1387b 69 6c 65 28 29 20 63 61 6c 6c 20 2a 2f 0d 0a 20  ile() call */.. 
1387c 20 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72     DWORD lastErr
1387d 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20  no = NO_ERROR;  
1387e 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e   /* Value return
1387f 65 64 20 62 79 20 47 65 74 4c 61 73 74 45 72 72  ed by GetLastErr
13880 6f 72 28 29 20 2a 2f 0d 0a 0d 0a 20 20 20 20 77  or() */....    w
13881 68 69 6c 65 28 20 6e 52 65 6d 3e 30 20 29 7b 0d  hile( nRem>0 ){.
13882 0a 20 20 20 20 20 20 69 66 28 20 21 6f 73 57 72  .      if( !osWr
13883 69 74 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  iteFile(pFile->h
13884 2c 20 61 52 65 6d 2c 20 6e 52 65 6d 2c 20 26 6e  , aRem, nRem, &n
13885 57 72 69 74 65 2c 20 30 29 20 29 7b 0d 0a 20 20  Write, 0) ){..  
13886 20 20 20 20 20 20 69 66 28 20 72 65 74 72 79 49        if( retryI
13887 6f 65 72 72 28 26 6e 52 65 74 72 79 2c 20 26 6c  oerr(&nRetry, &l
13888 61 73 74 45 72 72 6e 6f 29 20 29 20 63 6f 6e 74  astErrno) ) cont
13889 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 20 20 62  inue;..        b
1388a 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
1388b 20 20 20 20 20 20 69 66 28 20 6e 57 72 69 74 65        if( nWrite
1388c 3c 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  <=0 ) break;..  
1388d 20 20 20 20 61 52 65 6d 20 2b 3d 20 6e 57 72 69      aRem += nWri
1388e 74 65 3b 0d 0a 20 20 20 20 20 20 6e 52 65 6d 20  te;..      nRem 
1388f 2d 3d 20 6e 57 72 69 74 65 3b 0d 0a 20 20 20 20  -= nWrite;..    
13890 7d 0d 0a 20 20 20 20 69 66 28 20 6e 52 65 6d 3e  }..    if( nRem>
13891 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 46 69 6c  0 ){..      pFil
13892 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c  e->lastErrno = l
13893 61 73 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 20  astErrno;..     
13894 20 72 63 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d   rc = 1;..    }.
13895 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63  .  }....  if( rc
13896 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 20 20 28   ){..    if(   (
13897 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
13898 6f 3d 3d 45 52 52 4f 52 5f 48 41 4e 44 4c 45 5f  o==ERROR_HANDLE_
13899 44 49 53 4b 5f 46 55 4c 4c 20 29 0d 0a 20 20 20  DISK_FULL )..   
1389a 20 20 20 20 7c 7c 20 28 20 70 46 69 6c 65 2d 3e      || ( pFile->
1389b 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52  lastErrno==ERROR
1389c 5f 44 49 53 4b 5f 46 55 4c 4c 20 29 29 7b 0d 0a  _DISK_FULL )){..
1389d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1389e 49 54 45 5f 46 55 4c 4c 3b 0d 0a 20 20 20 20 7d  ITE_FULL;..    }
1389f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e  ..    return win
138a0 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
138a1 49 4f 45 52 52 5f 57 52 49 54 45 2c 20 70 46 69  IOERR_WRITE, pFi
138a2 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0d 0a  le->lastErrno,..
138a3 20 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69               "wi
138a4 6e 57 72 69 74 65 22 2c 20 70 46 69 6c 65 2d 3e  nWrite", pFile->
138a5 7a 50 61 74 68 29 3b 0d 0a 20 20 7d 65 6c 73 65  zPath);..  }else
138a6 7b 0d 0a 20 20 20 20 6c 6f 67 49 6f 65 72 72 28  {..    logIoerr(
138a7 6e 52 65 74 72 79 29 3b 0d 0a 20 20 7d 0d 0a 20  nRetry);..  }.. 
138a8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
138a9 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
138aa 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
138ab 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
138ac 66 69 65 64 20 73 69 7a 65 0d 0a 2a 2f 0d 0a 73  fied size..*/..s
138ad 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75  tatic int winTru
138ae 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  ncate(sqlite3_fi
138af 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f  le *id, sqlite3_
138b0 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0d 0a 20  int64 nByte){.. 
138b1 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
138b2 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 20  = (winFile*)id; 
138b3 20 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65 20   /* File handle 
138b4 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74  object */..  int
138b5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
138b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
138b7 52 65 74 75 72 6e 20 63 6f 64 65 20 66 6f 72 20  Return code for 
138b8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  this function */
138b9 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46  ....  assert( pF
138ba 69 6c 65 20 29 3b 0d 0a 0d 0a 20 20 4f 53 54 52  ile );....  OSTR
138bb 41 43 45 28 28 22 54 52 55 4e 43 41 54 45 20 25  ACE(("TRUNCATE %
138bc 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65  d %lld\n", pFile
138bd 2d 3e 68 2c 20 6e 42 79 74 65 29 29 3b 0d 0a 20  ->h, nByte));.. 
138be 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
138bf 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
138c0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b 0d  OERR_TRUNCATE);.
138c1 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75  ...  /* If the u
138c2 73 65 72 20 68 61 73 20 63 6f 6e 66 69 67 75 72  ser has configur
138c3 65 64 20 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20  ed a chunk-size 
138c4 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 74  for this file, t
138c5 72 75 6e 63 61 74 65 20 74 68 65 0d 0a 20 20 2a  runcate the..  *
138c6 2a 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69  * file so that i
138c7 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6e  t consists of an
138c8 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20   integer number 
138c9 6f 66 20 63 68 75 6e 6b 73 20 28 69 2e 65 2e 20  of chunks (i.e. 
138ca 74 68 65 0d 0a 20 20 2a 2a 20 61 63 74 75 61 6c  the..  ** actual
138cb 20 66 69 6c 65 20 73 69 7a 65 20 61 66 74 65 72   file size after
138cc 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d   the operation m
138cd 61 79 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  ay be larger tha
138ce 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 0d  n the requested.
138cf 0a 20 20 2a 2a 20 73 69 7a 65 29 2e 0d 0a 20 20  .  ** size)...  
138d0 2a 2f 0d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  */..  if( pFile-
138d1 3e 73 7a 43 68 75 6e 6b 3e 30 20 29 7b 0d 0a 20  >szChunk>0 ){.. 
138d2 20 20 20 6e 42 79 74 65 20 3d 20 28 28 6e 42 79     nByte = ((nBy
138d3 74 65 20 2b 20 70 46 69 6c 65 2d 3e 73 7a 43 68  te + pFile->szCh
138d4 75 6e 6b 20 2d 20 31 29 2f 70 46 69 6c 65 2d 3e  unk - 1)/pFile->
138d5 73 7a 43 68 75 6e 6b 29 20 2a 20 70 46 69 6c 65  szChunk) * pFile
138d6 2d 3e 73 7a 43 68 75 6e 6b 3b 0d 0a 20 20 7d 0d  ->szChunk;..  }.
138d7 0a 0d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66  ...  /* SetEndOf
138d8 46 69 6c 65 28 29 20 72 65 74 75 72 6e 73 20 6e  File() returns n
138d9 6f 6e 2d 7a 65 72 6f 20 77 68 65 6e 20 73 75 63  on-zero when suc
138da 63 65 73 73 66 75 6c 2c 20 6f 72 20 7a 65 72 6f  cessful, or zero
138db 20 77 68 65 6e 20 69 74 20 66 61 69 6c 73 2e 20   when it fails. 
138dc 2a 2f 0d 0a 20 20 69 66 28 20 73 65 65 6b 57 69  */..  if( seekWi
138dd 6e 46 69 6c 65 28 70 46 69 6c 65 2c 20 6e 42 79  nFile(pFile, nBy
138de 74 65 29 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  te) ){..    rc =
138df 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
138e0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
138e1 54 45 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  TE, pFile->lastE
138e2 72 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20  rrno,..         
138e3 20 20 20 20 22 77 69 6e 54 72 75 6e 63 61 74 65      "winTruncate
138e4 31 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68  1", pFile->zPath
138e5 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  );..  }else if( 
138e6 30 3d 3d 6f 73 53 65 74 45 6e 64 4f 66 46 69 6c  0==osSetEndOfFil
138e7 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0d 0a  e(pFile->h) ){..
138e8 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
138e9 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74  rrno = osGetLast
138ea 45 72 72 6f 72 28 29 3b 0d 0a 20 20 20 20 72 63  Error();..    rc
138eb 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53   = winLogError(S
138ec 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
138ed 43 41 54 45 2c 20 70 46 69 6c 65 2d 3e 6c 61 73  CATE, pFile->las
138ee 74 45 72 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20  tErrno,..       
138ef 20 20 20 20 20 20 22 77 69 6e 54 72 75 6e 63 61        "winTrunca
138f0 74 65 32 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61  te2", pFile->zPa
138f1 74 68 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 4f  th);..  }....  O
138f2 53 54 52 41 43 45 28 28 22 54 52 55 4e 43 41 54  STRACE(("TRUNCAT
138f3 45 20 25 64 20 25 6c 6c 64 20 25 73 5c 6e 22 2c  E %d %lld %s\n",
138f4 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65   pFile->h, nByte
138f5 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20  , rc ? "failed" 
138f6 3a 20 22 6f 6b 22 29 29 3b 0d 0a 20 20 72 65 74  : "ok"));..  ret
138f7 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 69  urn rc;..}....#i
138f8 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
138f9 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 75 6e 74 20 74  ../*..** Count t
138fa 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c  he number of ful
138fb 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61  lsyncs and norma
138fc 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69  l syncs.  This i
138fd 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0d 0a  s used to test..
138fe 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
138ff 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
13900 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20  occuring at the 
13901 72 69 67 68 74 20 74 69 6d 65 73 2e 0d 0a 2a 2f  right times...*/
13902 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
13903 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
13904 75 6e 74 20 3d 20 30 3b 0d 0a 53 51 4c 49 54 45  unt = 0;..SQLITE
13905 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
13906 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20  _fullsync_count 
13907 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  = 0;..#endif....
13908 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  /*..** Make sure
13909 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61   all writes to a
1390a 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65   particular file
1390b 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74   are committed t
1390c 6f 20 64 69 73 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61  o disk...*/..sta
1390d 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28  tic int winSync(
1390e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
1390f 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 23  , int flags){..#
13910 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  ifndef SQLITE_NO
13911 5f 53 59 4e 43 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  _SYNC..  /*..  *
13912 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e  * Used only when
13913 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
13914 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0d  is not defined..
13915 0a 20 20 20 2a 2f 0d 0a 20 20 42 4f 4f 4c 20 72  .   */..  BOOL r
13916 63 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20  c;..#endif..#if 
13917 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
13918 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
13919 49 54 45 5f 4e 4f 5f 53 59 4e 43 29 20 7c 7c 20  ITE_NO_SYNC) || 
1391a 5c 0d 0a 20 20 20 20 28 64 65 66 69 6e 65 64 28  \..    (defined(
1391b 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20  SQLITE_TEST) && 
1391c 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
1391d 45 42 55 47 29 29 0d 0a 20 20 2f 2a 0d 0a 20 20  EBUG))..  /*..  
1391e 2a 2a 20 55 73 65 64 20 77 68 65 6e 20 53 51 4c  ** Used when SQL
1391f 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 69 73 20 6e  ITE_NO_SYNC is n
13920 6f 74 20 64 65 66 69 6e 65 64 20 61 6e 64 20 62  ot defined and b
13921 79 20 74 68 65 20 61 73 73 65 72 74 28 29 20 61  y the assert() a
13922 6e 64 2f 6f 72 0d 0a 20 20 2a 2a 20 4f 53 54 52  nd/or..  ** OSTR
13923 41 43 45 28 29 20 6d 61 63 72 6f 73 2e 0d 0a 20  ACE() macros... 
13924 20 20 2a 2f 0d 0a 20 20 77 69 6e 46 69 6c 65 20    */..  winFile 
13925 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
13926 65 2a 29 69 64 3b 0d 0a 23 65 6c 73 65 0d 0a 20  e*)id;..#else.. 
13927 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
13928 52 28 69 64 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  R(id);..#endif..
13929 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
1392a 65 20 29 3b 0d 0a 20 20 2f 2a 20 43 68 65 63 6b  e );..  /* Check
1392b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c   that one of SQL
1392c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20  ITE_SYNC_NORMAL 
1392d 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 73  or FULL was pass
1392e 65 64 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  ed */..  assert(
1392f 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
13930 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
13931 0d 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67  ..      || (flag
13932 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
13933 53 59 4e 43 5f 46 55 4c 4c 0d 0a 20 20 29 3b 0d  SYNC_FULL..  );.
13934 0a 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 53  ...  OSTRACE(("S
13935 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  YNC %d lock=%d\n
13936 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69  ", pFile->h, pFi
13937 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 29 3b 0d  le->locktype));.
13938 0a 0d 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e  ...  /* Unix can
13939 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79  not, but some sy
1393a 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e  stems may return
1393b 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f   SQLITE_FULL fro
1393c 6d 20 68 65 72 65 2e 20 54 68 69 73 0d 0a 20 20  m here. This..  
1393d 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65  ** line is to te
1393e 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f  st that doing so
1393f 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20   does not cause 
13940 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0d 0a 20  any problems... 
13941 20 2a 2f 0d 0a 20 20 53 69 6d 75 6c 61 74 65 44   */..  SimulateD
13942 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65  iskfullError( re
13943 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
13944 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   );....#ifndef S
13945 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 55 4e  QLITE_TEST..  UN
13946 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66  USED_PARAMETER(f
13947 6c 61 67 73 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20  lags);..#else.. 
13948 20 69 66 28 20 28 66 6c 61 67 73 26 30 78 30 46   if( (flags&0x0F
13949 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  )==SQLITE_SYNC_F
1394a 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ULL ){..    sqli
1394b 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
1394c 6e 74 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71  nt++;..  }..  sq
1394d 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
1394e 2b 2b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  ++;..#endif.... 
1394f 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
13950 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
13951 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
13952 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
13953 20 61 0d 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0d 0a   a..  ** no-op..
13954 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c    */..#ifdef SQL
13955 49 54 45 5f 4e 4f 5f 53 59 4e 43 0d 0a 20 20 72  ITE_NO_SYNC..  r
13956 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
13957 0d 0a 23 65 6c 73 65 0d 0a 20 20 72 63 20 3d 20  ..#else..  rc = 
13958 6f 73 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65  osFlushFileBuffe
13959 72 73 28 70 46 69 6c 65 2d 3e 68 29 3b 0d 0a 20  rs(pFile->h);.. 
1395a 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
1395b 28 20 72 63 3d 46 41 4c 53 45 20 29 3b 0d 0a 20  ( rc=FALSE );.. 
1395c 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
1395d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1395e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
1395f 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
13960 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72  o = osGetLastErr
13961 6f 72 28 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  or();..    retur
13962 6e 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51  n winLogError(SQ
13963 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
13964 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  , pFile->lastErr
13965 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  no,..           
13966 20 20 22 77 69 6e 53 79 6e 63 22 2c 20 70 46 69    "winSync", pFi
13967 6c 65 2d 3e 7a 50 61 74 68 29 3b 0d 0a 20 20 7d  le->zPath);..  }
13968 0d 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f  ..#endif..}..../
13969 2a 0d 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20  *..** Determine 
1396a 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
1396b 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79   of a file in by
1396c 74 65 73 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  tes..*/..static 
1396d 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  int winFileSize(
1396e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
1396f 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
13970 2a 70 53 69 7a 65 29 7b 0d 0a 20 20 44 57 4f 52  *pSize){..  DWOR
13971 44 20 75 70 70 65 72 42 69 74 73 3b 0d 0a 20 20  D upperBits;..  
13972 44 57 4f 52 44 20 6c 6f 77 65 72 42 69 74 73 3b  DWORD lowerBits;
13973 0d 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ..  winFile *pFi
13974 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
13975 64 3b 0d 0a 20 20 44 57 4f 52 44 20 6c 61 73 74  d;..  DWORD last
13976 45 72 72 6e 6f 3b 0d 0a 0d 0a 20 20 61 73 73 65  Errno;....  asse
13977 72 74 28 20 69 64 21 3d 30 20 29 3b 0d 0a 20 20  rt( id!=0 );..  
13978 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
13979 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
1397a 45 52 52 5f 46 53 54 41 54 29 3b 0d 0a 20 20 6c  ERR_FSTAT);..  l
1397b 6f 77 65 72 42 69 74 73 20 3d 20 6f 73 47 65 74  owerBits = osGet
1397c 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e  FileSize(pFile->
1397d 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b 0d  h, &upperBits);.
1397e 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 42  .  if(   (lowerB
1397f 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46  its == INVALID_F
13980 49 4c 45 5f 53 49 5a 45 29 0d 0a 20 20 20 20 20  ILE_SIZE)..     
13981 26 26 20 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d  && ((lastErrno =
13982 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
13983 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 29 20 29 0d  ))!=NO_ERROR) ).
13984 0a 20 20 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  .  {..    pFile-
13985 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73  >lastErrno = las
13986 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 72 65 74  tErrno;..    ret
13987 75 72 6e 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28  urn winLogError(
13988 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54  SQLITE_IOERR_FST
13989 41 54 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  AT, pFile->lastE
1398a 72 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20  rrno,..         
1398b 20 20 20 20 22 77 69 6e 46 69 6c 65 53 69 7a 65      "winFileSize
1398c 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29  ", pFile->zPath)
1398d 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 53 69 7a 65  ;..  }..  *pSize
1398e 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e   = (((sqlite3_in
1398f 74 36 34 29 75 70 70 65 72 42 69 74 73 29 3c 3c  t64)upperBits)<<
13990 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 74 73 3b  32) + lowerBits;
13991 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
13992 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
13993 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c  ** LOCKFILE_FAIL
13994 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20  _IMMEDIATELY is 
13995 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d  undefined on som
13996 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  e Windows system
13997 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20  s...*/..#ifndef 
13998 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d  LOCKFILE_FAIL_IM
13999 4d 45 44 49 41 54 45 4c 59 0d 0a 23 20 64 65 66  MEDIATELY..# def
1399a 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  ine LOCKFILE_FAI
1399b 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 0d  L_IMMEDIATELY 1.
1399c 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
1399d 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64  * Acquire a read
1399e 65 72 20 6c 6f 63 6b 2e 0d 0a 2a 2a 20 44 69 66  er lock...** Dif
1399f 66 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69  ferent API routi
139a0 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64  nes are called d
139a1 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
139a2 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0d  her or not this.
139a3 0a 2a 2a 20 69 73 20 57 69 6e 39 78 20 6f 72 20  .** is Win9x or 
139a4 57 69 6e 4e 54 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  WinNT...*/..stat
139a5 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f  ic int getReadLo
139a6 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  ck(winFile *pFil
139a7 65 29 7b 0d 0a 20 20 69 6e 74 20 72 65 73 3b 0d  e){..  int res;.
139a8 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
139a9 0d 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44  ..    OVERLAPPED
139aa 20 6f 76 6c 70 3b 0d 0a 20 20 20 20 6f 76 6c 70   ovlp;..    ovlp
139ab 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  .Offset = SHARED
139ac 5f 46 49 52 53 54 3b 0d 0a 20 20 20 20 6f 76 6c  _FIRST;..    ovl
139ad 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30  p.OffsetHigh = 0
139ae 3b 0d 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65  ;..    ovlp.hEve
139af 6e 74 20 3d 20 30 3b 0d 0a 20 20 20 20 72 65 73  nt = 0;..    res
139b0 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 78 28   = osLockFileEx(
139b1 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49  pFile->h, LOCKFI
139b2 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
139b3 45 4c 59 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  ELY,..          
139b4 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
139b5 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20  SHARED_SIZE, 0, 
139b6 26 6f 76 6c 70 29 3b 0d 0a 2f 2a 20 69 73 4e 54  &ovlp);../* isNT
139b7 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
139b8 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
139b9 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
139ba 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0d  ever executed. .
139bb 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .*/..#if SQLITE_
139bc 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20 20 7d  OS_WINCE==0..  }
139bd 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20 6c  else{..    int l
139be 6b 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  k;..    sqlite3_
139bf 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f  randomness(sizeo
139c0 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0d 0a 20 20  f(lk), &lk);..  
139c1 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c    pFile->sharedL
139c2 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74  ockByte = (short
139c3 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  )((lk & 0x7fffff
139c4 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
139c5 20 2d 20 31 29 29 3b 0d 0a 20 20 20 20 72 65 73   - 1));..    res
139c6 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65 28 70 46   = osLockFile(pF
139c7 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46  ile->h, SHARED_F
139c8 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72  IRST+pFile->shar
139c9 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31  edLockByte, 0, 1
139ca 2c 20 30 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  , 0);..#endif.. 
139cb 20 7d 0d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d   }..  if( res ==
139cc 20 30 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65   0 ){..    pFile
139cd 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73  ->lastErrno = os
139ce 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0d  GetLastError();.
139cf 0a 20 20 20 20 2f 2a 20 4e 6f 20 6e 65 65 64 20  .    /* No need 
139d0 74 6f 20 6c 6f 67 20 61 20 66 61 69 6c 75 72 65  to log a failure
139d1 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 7d   to lock */..  }
139d2 0d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0d  ..  return res;.
139d3 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 64  .}..../*..** Und
139d4 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0d 0a 2a 2f  o a readlock..*/
139d5 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c  ..static int unl
139d6 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46  ockReadLock(winF
139d7 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0d 0a 20 20  ile *pFile){..  
139d8 69 6e 74 20 72 65 73 3b 0d 0a 20 20 44 57 4f 52  int res;..  DWOR
139d9 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0d 0a 20 20  D lastErrno;..  
139da 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0d 0a 20  if( isNT() ){.. 
139db 20 20 20 72 65 73 20 3d 20 6f 73 55 6e 6c 6f 63     res = osUnloc
139dc 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
139dd 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
139de 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
139df 3b 0d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  ;../* isNT() is 
139e0 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
139e1 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
139e2 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
139e3 78 65 63 75 74 65 64 2e 20 0d 0a 2a 2f 0d 0a 23  xecuted. ..*/..#
139e4 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
139e5 43 45 3d 3d 30 0d 0a 20 20 7d 65 6c 73 65 7b 0d  CE==0..  }else{.
139e6 0a 20 20 20 20 72 65 73 20 3d 20 6f 73 55 6e 6c  .    res = osUnl
139e7 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
139e8 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b  , SHARED_FIRST +
139e9 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f   pFile->sharedLo
139ea 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29  ckByte, 0, 1, 0)
139eb 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a  ;..#endif..  }..
139ec 20 20 69 66 28 20 72 65 73 3d 3d 30 20 26 26 20    if( res==0 && 
139ed 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73  ((lastErrno = os
139ee 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21  GetLastError())!
139ef 3d 45 52 52 4f 52 5f 4e 4f 54 5f 4c 4f 43 4b 45  =ERROR_NOT_LOCKE
139f0 44 29 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65  D) ){..    pFile
139f1 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61  ->lastErrno = la
139f2 73 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 77 69  stErrno;..    wi
139f3 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  nLogError(SQLITE
139f4 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 2c 20 70  _IOERR_UNLOCK, p
139f5 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c  File->lastErrno,
139f6 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22  ..             "
139f7 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 22 2c  unlockReadLock",
139f8 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0d   pFile->zPath);.
139f9 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
139fa 65 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  es;..}..../*..**
139fb 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
139fc 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
139fd 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
139fe 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
139ff 6e 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  ne..** of the fo
13a00 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  llowing:..**..**
13a01 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
13a02 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20 28 32 29  LOCK..**     (2)
13a03 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0d 0a   RESERVED_LOCK..
13a04 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
13a05 4e 47 5f 4c 4f 43 4b 0d 0a 2a 2a 20 20 20 20 20  NG_LOCK..**     
13a06 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
13a07 43 4b 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d 65 74  CK..**..** Somet
13a08 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
13a09 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
13a0a 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
13a0b 6c 6f 63 6b 20 73 74 61 74 65 73 0d 0a 2a 2a 20  lock states..** 
13a0c 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
13a0d 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
13a0e 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
13a0f 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
13a10 61 74 65 72 0d 0a 2a 2a 20 74 72 61 6e 73 69 74  ater..** transit
13a11 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
13a12 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
13a13 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
13a14 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0d 0a  it started but..
13a15 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
13a16 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
13a17 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
13a18 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
13a19 65 64 0d 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ed..** transitio
13a1a 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
13a1b 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
13a1c 20 73 74 61 74 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a   states:..**..**
13a1d 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
13a1e 53 48 41 52 45 44 0d 0a 2a 2a 20 20 20 20 53 48  SHARED..**    SH
13a1f 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
13a20 0d 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  ..**    SHARED -
13a21 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
13a22 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 20 20 20  XCLUSIVE..**    
13a23 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
13a24 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
13a25 56 45 0d 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  VE..**    PENDIN
13a26 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0d 0a  G -> EXCLUSIVE..
13a27 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
13a28 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
13a29 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
13a2a 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20  The winUnlock() 
13a2b 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 65 72 61 73  routine..** eras
13a2c 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20  es all locks at 
13a2d 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73  once and returns
13a2e 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   us immediately 
13a2f 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
13a30 20 30 2e 0d 0a 2a 2a 20 49 74 20 69 73 20 6e 6f   0...** It is no
13a31 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f  t possible to lo
13a32 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
13a33 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61  level one step a
13a34 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0d 0a  t a time.  You..
13a35 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69  ** must go strai
13a36 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c  ght to locking l
13a37 65 76 65 6c 20 30 2e 0d 0a 2a 2f 0d 0a 73 74 61  evel 0...*/..sta
13a38 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28  tic int winLock(
13a39 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
13a3a 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
13a3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
13a3c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65  ITE_OK;    /* Re
13a3d 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73  turn code from s
13a3e 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0d 0a 20  ubroutines */.. 
13a3f 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20   int res = 1;   
13a40 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
13a41 74 20 6f 66 20 61 20 57 69 6e 64 6f 77 73 20 6c  t of a Windows l
13a42 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0d 0a 20 20 69  ock call */..  i
13a43 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20  nt newLocktype; 
13a44 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69        /* Set pFi
13a45 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20  le->locktype to 
13a46 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72  this value befor
13a47 65 20 65 78 69 74 69 6e 67 20 2a 2f 0d 0a 20 20  e exiting */..  
13a48 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f  int gotPendingLo
13a49 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69  ck = 0;/* True i
13a4a 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20  f we acquired a 
13a4b 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69  PENDING lock thi
13a4c 73 20 74 69 6d 65 20 2a 2f 0d 0a 20 20 77 69 6e  s time */..  win
13a4d 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
13a4e 69 6e 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 44  inFile*)id;..  D
13a4f 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 20 3d  WORD lastErrno =
13a50 20 4e 4f 5f 45 52 52 4f 52 3b 0d 0a 0d 0a 20 20   NO_ERROR;....  
13a51 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
13a52 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f  ..  OSTRACE(("LO
13a53 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28  CK %d %d was %d(
13a54 25 64 29 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20  %d)\n",..       
13a55 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f      pFile->h, lo
13a56 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c  cktype, pFile->l
13a57 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
13a58 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 29  sharedLockByte))
13a59 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65  ;....  /* If the
13a5a 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
13a5b 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
13a5c 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
13a5d 63 74 69 76 65 20 6f 6e 20 74 68 65 0d 0a 20 20  ctive on the..  
13a5e 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f  ** OsFile, do no
13a5f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
13a60 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
13a61 78 69 74 20 70 61 74 68 2c 20 61 73 0d 0a 20 20  xit path, as..  
13a62 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  ** sqlite3OsEnte
13a63 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20  rMutex() hasn't 
13a64 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
13a65 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 46  ..  */..  if( pF
13a66 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
13a67 6f 63 6b 74 79 70 65 20 29 7b 0d 0a 20 20 20 20  ocktype ){..    
13a68 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
13a69 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d  ;..  }....  /* M
13a6a 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
13a6b 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
13a6c 20 63 6f 72 72 65 63 74 0d 0a 20 20 2a 2f 0d 0a   correct..  */..
13a6d 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
13a6e 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
13a6f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK || locktype==
13a70 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a  SHARED_LOCK );..
13a71 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
13a72 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe!=PENDING_LOCK
13a73 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6c   );..  assert( l
13a74 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45  ocktype!=RESERVE
13a75 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d  D_LOCK || pFile-
13a76 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
13a77 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a 20 20 2f  D_LOCK );....  /
13a78 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49  * Lock the PENDI
13a79 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20  NG_LOCK byte if 
13a7a 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69  we need to acqui
13a7b 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
13a7c 6b 20 6f 72 0d 0a 20 20 2a 2a 20 61 20 53 48 41  k or..  ** a SHA
13a7d 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65  RED lock.  If we
13a7e 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61   are acquiring a
13a7f 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
13a80 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66  e acquisition of
13a81 0d 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49  ..  ** the PENDI
13a82 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20  NG_LOCK byte is 
13a83 74 65 6d 70 6f 72 61 72 79 2e 0d 0a 20 20 2a 2f  temporary...  */
13a84 0d 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20  ..  newLocktype 
13a85 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
13a86 65 3b 0d 0a 20 20 69 66 28 20 20 20 28 70 46 69  e;..  if(   (pFi
13a87 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
13a88 5f 4c 4f 43 4b 29 0d 0a 20 20 20 20 20 7c 7c 20  _LOCK)..     || 
13a89 28 20 20 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45  (   (locktype==E
13a8a 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0d 0a  XCLUSIVE_LOCK)..
13a8b 20 20 20 20 20 20 20 20 20 26 26 20 28 70 46 69           && (pFi
13a8c 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  le->locktype==RE
13a8d 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29 0d 0a 20  SERVED_LOCK)).. 
13a8e 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 63 6e 74   ){..    int cnt
13a8f 20 3d 20 33 3b 0d 0a 20 20 20 20 77 68 69 6c 65   = 3;..    while
13a90 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65  ( cnt-->0 && (re
13a91 73 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65 28 70  s = osLockFile(p
13a92 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
13a93 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29  _BYTE, 0, 1, 0))
13a94 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  ==0 ){..      /*
13a95 20 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20   Try 3 times to 
13a96 67 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20  get the pending 
13a97 6c 6f 63 6b 2e 20 20 54 68 69 73 20 69 73 20 6e  lock.  This is n
13a98 65 65 64 65 64 20 74 6f 20 77 6f 72 6b 0d 0a 20  eeded to work.. 
13a99 20 20 20 20 20 2a 2a 20 61 72 6f 75 6e 64 20 70       ** around p
13a9a 72 6f 62 6c 65 6d 73 20 63 61 75 73 65 64 20 62  roblems caused b
13a9b 79 20 69 6e 64 65 78 69 6e 67 20 61 6e 64 2f 6f  y indexing and/o
13a9c 72 20 61 6e 74 69 2d 76 69 72 75 73 20 73 6f 66  r anti-virus sof
13a9d 74 77 61 72 65 20 6f 6e 0d 0a 20 20 20 20 20 20  tware on..      
13a9e 2a 2a 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65  ** Windows syste
13a9f 6d 73 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 49 66  ms...      ** If
13aa0 20 79 6f 75 20 61 72 65 20 75 73 69 6e 67 20 74   you are using t
13aa1 68 69 73 20 63 6f 64 65 20 61 73 20 61 20 6d 6f  his code as a mo
13aa2 64 65 6c 20 66 6f 72 20 61 6c 74 65 72 6e 61 74  del for alternat
13aa3 69 76 65 20 56 46 53 65 73 2c 20 64 6f 20 6e 6f  ive VFSes, do no
13aa4 74 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79  t..      ** copy
13aa5 20 74 68 69 73 20 72 65 74 72 79 20 6c 6f 67 69   this retry logi
13aa6 63 2e 20 20 49 74 20 69 73 20 61 20 68 61 63 6b  c.  It is a hack
13aa7 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 57 69   intended for Wi
13aa8 6e 64 6f 77 73 20 6f 6e 6c 79 2e 0d 0a 20 20 20  ndows only...   
13aa9 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 4f 53 54     */..      OST
13aaa 52 41 43 45 28 28 22 63 6f 75 6c 64 20 6e 6f 74  RACE(("could not
13aab 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c   get a PENDING l
13aac 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20  ock. cnt=%d\n", 
13aad 63 6e 74 29 29 3b 0d 0a 20 20 20 20 20 20 69 66  cnt));..      if
13aae 28 20 63 6e 74 20 29 20 6f 73 53 6c 65 65 70 28  ( cnt ) osSleep(
13aaf 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  1);..    }..    
13ab0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
13ab1 20 72 65 73 3b 0d 0a 20 20 20 20 69 66 28 20 21   res;..    if( !
13ab2 72 65 73 20 29 7b 0d 0a 20 20 20 20 20 20 6c 61  res ){..      la
13ab3 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c  stErrno = osGetL
13ab4 61 73 74 45 72 72 6f 72 28 29 3b 0d 0a 20 20 20  astError();..   
13ab5 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20   }..  }....  /* 
13ab6 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64  Acquire a shared
13ab7 20 6c 6f 63 6b 0d 0a 20 20 2a 2f 0d 0a 20 20 69   lock..  */..  i
13ab8 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
13ab9 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  RED_LOCK && res 
13aba 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
13abb 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
13abc 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 20  =NO_LOCK );..   
13abd 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f   res = getReadLo
13abe 63 6b 28 70 46 69 6c 65 29 3b 0d 0a 20 20 20 20  ck(pFile);..    
13abf 69 66 28 20 72 65 73 20 29 7b 0d 0a 20 20 20 20  if( res ){..    
13ac0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
13ac1 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0d 0a 20 20  SHARED_LOCK;..  
13ac2 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
13ac3 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65  lastErrno = osGe
13ac4 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0d 0a 20  tLastError();.. 
13ac5 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
13ac6 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45  * Acquire a RESE
13ac7 52 56 45 44 20 6c 6f 63 6b 0d 0a 20 20 2a 2f 0d  RVED lock..  */.
13ac8 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
13ac9 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26  =RESERVED_LOCK &
13aca 26 20 72 65 73 20 29 7b 0d 0a 20 20 20 20 61 73  & res ){..    as
13acb 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
13acc 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
13acd 43 4b 20 29 3b 0d 0a 20 20 20 20 72 65 73 20 3d  CK );..    res =
13ace 20 6f 73 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   osLockFile(pFil
13acf 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
13ad0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0d 0a  YTE, 0, 1, 0);..
13ad1 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0d 0a      if( res ){..
13ad2 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70        newLocktyp
13ad3 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  e = RESERVED_LOC
13ad4 4b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  K;..    }else{..
13ad5 20 20 20 20 20 20 6c 61 73 74 45 72 72 6e 6f 20        lastErrno 
13ad6 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72  = osGetLastError
13ad7 28 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ();..    }..  }.
13ad8 0a 0d 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  ...  /* Acquire 
13ad9 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0d 0a  a PENDING lock..
13ada 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6c 6f 63 6b    */..  if( lock
13adb 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
13adc 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0d 0a  LOCK && res ){..
13add 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
13ade 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0d  = PENDING_LOCK;.
13adf 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c  .    gotPendingL
13ae0 6f 63 6b 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d  ock = 0;..  }...
13ae1 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e  .  /* Acquire an
13ae2 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0d   EXCLUSIVE lock.
13ae3 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6c 6f 63  .  */..  if( loc
13ae4 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
13ae5 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0d  _LOCK && res ){.
13ae6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
13ae7 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48  le->locktype>=SH
13ae8 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20  ARED_LOCK );..  
13ae9 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65    res = unlockRe
13aea 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0d 0a  adLock(pFile);..
13aeb 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 75 6e      OSTRACE(("un
13aec 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22  readlock = %d\n"
13aed 2c 20 72 65 73 29 29 3b 0d 0a 20 20 20 20 72 65  , res));..    re
13aee 73 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65 28 70  s = osLockFile(p
13aef 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f  File->h, SHARED_
13af0 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44  FIRST, 0, SHARED
13af1 5f 53 49 5a 45 2c 20 30 29 3b 0d 0a 20 20 20 20  _SIZE, 0);..    
13af2 69 66 28 20 72 65 73 20 29 7b 0d 0a 20 20 20 20  if( res ){..    
13af3 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
13af4 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0d  EXCLUSIVE_LOCK;.
13af5 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
13af6 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f     lastErrno = o
13af7 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b  sGetLastError();
13af8 0d 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28  ..      OSTRACE(
13af9 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25  ("error-code = %
13afa 64 5c 6e 22 2c 20 6c 61 73 74 45 72 72 6e 6f 29  d\n", lastErrno)
13afb 29 3b 0d 0a 20 20 20 20 20 20 67 65 74 52 65 61  );..      getRea
13afc 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0d 0a 20  dLock(pFile);.. 
13afd 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
13afe 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64  * If we are hold
13aff 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ing a PENDING lo
13b00 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f  ck that ought to
13b01 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68   be released, th
13b02 65 6e 0d 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65  en..  ** release
13b03 20 69 74 20 6e 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a   it now...  */..
13b04 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67    if( gotPending
13b05 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65  Lock && locktype
13b06 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
13b07 0d 0a 20 20 20 20 6f 73 55 6e 6c 6f 63 6b 46 69  ..    osUnlockFi
13b08 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e  le(pFile->h, PEN
13b09 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c  DING_BYTE, 0, 1,
13b0a 20 30 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f   0);..  }....  /
13b0b 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61  * Update the sta
13b0c 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68  te of the lock h
13b0d 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66  as held in the f
13b0e 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
13b0f 68 65 6e 0d 0a 20 20 2a 2a 20 72 65 74 75 72 6e  hen..  ** return
13b10 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
13b11 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0d 0a 20   result code... 
13b12 20 2a 2f 0d 0a 20 20 69 66 28 20 72 65 73 20 29   */..  if( res )
13b13 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  {..    rc = SQLI
13b14 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65 7b  TE_OK;..  }else{
13b15 0d 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 22  ..    OSTRACE(("
13b16 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74  LOCK FAILED %d t
13b17 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74  rying for %d but
13b18 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c   got %d\n", pFil
13b19 65 2d 3e 68 2c 0d 0a 20 20 20 20 20 20 20 20 20  e->h,..         
13b1a 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c    locktype, newL
13b1b 6f 63 6b 74 79 70 65 29 29 3b 0d 0a 20 20 20 20  ocktype));..    
13b1c 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
13b1d 20 3d 20 6c 61 73 74 45 72 72 6e 6f 3b 0d 0a 20   = lastErrno;.. 
13b1e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
13b1f 55 53 59 3b 0d 0a 20 20 7d 0d 0a 20 20 70 46 69  USY;..  }..  pFi
13b20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28  le->locktype = (
13b21 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0d  u8)newLocktype;.
13b22 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
13b23 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
13b24 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
13b25 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
13b26 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
13b27 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
13b28 0d 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  ..** file by thi
13b29 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
13b2a 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
13b2b 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
13b2c 72 65 74 75 72 6e 0d 0a 2a 2a 20 6e 6f 6e 2d 7a  return..** non-z
13b2d 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a  ero, otherwise z
13b2e 65 72 6f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ero...*/..static
13b2f 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73   int winCheckRes
13b30 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
13b31 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
13b32 2a 70 52 65 73 4f 75 74 29 7b 0d 0a 20 20 69 6e  *pResOut){..  in
13b33 74 20 72 63 3b 0d 0a 20 20 77 69 6e 46 69 6c 65  t rc;..  winFile
13b34 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
13b35 6c 65 2a 29 69 64 3b 0d 0a 0d 0a 20 20 53 69 6d  le*)id;....  Sim
13b36 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
13b37 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
13b38 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
13b39 4f 43 4b 3b 20 29 3b 0d 0a 0d 0a 20 20 61 73 73  OCK; );....  ass
13b3a 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0d 0a 20  ert( id!=0 );.. 
13b3b 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
13b3c 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c  type>=RESERVED_L
13b3d 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  OCK ){..    rc =
13b3e 20 31 3b 0d 0a 20 20 20 20 4f 53 54 52 41 43 45   1;..    OSTRACE
13b3f 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  (("TEST WR-LOCK 
13b40 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22  %d %d (local)\n"
13b41 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 29  , pFile->h, rc))
13b42 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
13b43 20 72 63 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65   rc = osLockFile
13b44 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52  (pFile->h, RESER
13b45 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  VED_BYTE, 0, 1, 
13b46 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20  0);..    if( rc 
13b47 29 7b 0d 0a 20 20 20 20 20 20 6f 73 55 6e 6c 6f  ){..      osUnlo
13b48 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
13b49 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
13b4a 30 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 20 20 7d  0, 1, 0);..    }
13b4b 0d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0d  ..    rc = !rc;.
13b4c 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 54  .    OSTRACE(("T
13b4d 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
13b4e 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70  d (remote)\n", p
13b4f 46 69 6c 65 2d 3e 68 2c 20 72 63 29 29 3b 0d 0a  File->h, rc));..
13b50 20 20 7d 0d 0a 20 20 2a 70 52 65 73 4f 75 74 20    }..  *pResOut 
13b51 3d 20 72 63 3b 0d 0a 20 20 72 65 74 75 72 6e 20  = rc;..  return 
13b52 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
13b53 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ./*..** Lower th
13b54 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
13b55 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
13b56 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70  or id to locktyp
13b57 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0d 0a 2a 2a  e.  locktype..**
13b58 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
13b59 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
13b5a 44 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  D_LOCK...**..** 
13b5b 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
13b5c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
13b5d 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
13b5e 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
13b5f 6f 77 0d 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  ow..** the reque
13b60 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
13b61 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
13b62 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a   is a no-op...**
13b63 0d 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  ..** It is not p
13b64 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73  ossible for this
13b65 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c   routine to fail
13b66 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   if the second a
13b67 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 69 73 20 4e  rgument..** is N
13b68 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20  O_LOCK.  If the 
13b69 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
13b6a 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74  is SHARED_LOCK t
13b6b 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
13b6c 0d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72  ..** might retur
13b6d 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0d  n SQLITE_IOERR;.
13b6e 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
13b6f 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  winUnlock(sqlite
13b70 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
13b71 6c 6f 63 6b 74 79 70 65 29 7b 0d 0a 20 20 69 6e  locktype){..  in
13b72 74 20 74 79 70 65 3b 0d 0a 20 20 77 69 6e 46 69  t type;..  winFi
13b73 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
13b74 46 69 6c 65 2a 29 69 64 3b 0d 0a 20 20 69 6e 74  File*)id;..  int
13b75 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13b76 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
13b77 65 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  e!=0 );..  asser
13b78 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
13b79 52 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 4f  RED_LOCK );..  O
13b7a 53 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20  STRACE(("UNLOCK 
13b7b 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28  %d to %d was %d(
13b7c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  %d)\n", pFile->h
13b7d 2c 20 6c 6f 63 6b 74 79 70 65 2c 0d 0a 20 20 20  , locktype,..   
13b7e 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f         pFile->lo
13b7f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73  cktype, pFile->s
13b80 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 29 3b  haredLockByte));
13b81 0d 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65  ..  type = pFile
13b82 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0d 0a 20 20 69  ->locktype;..  i
13b83 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49  f( type>=EXCLUSI
13b84 56 45 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20  VE_LOCK ){..    
13b85 6f 73 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69  osUnlockFile(pFi
13b86 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
13b87 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53  RST, 0, SHARED_S
13b88 49 5a 45 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66  IZE, 0);..    if
13b89 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
13b8a 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52  ED_LOCK && !getR
13b8b 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29  eadLock(pFile) )
13b8c 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  {..      /* This
13b8d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
13b8e 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64  ppen.  We should
13b8f 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20   always be able 
13b90 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 72 65 61  to..      ** rea
13b91 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
13b92 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 20 20 72  lock */..      r
13b93 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28  c = winLogError(
13b94 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
13b95 4f 43 4b 2c 20 6f 73 47 65 74 4c 61 73 74 45 72  OCK, osGetLastEr
13b96 72 6f 72 28 29 2c 0d 0a 20 20 20 20 20 20 20 20  ror(),..        
13b97 20 20 20 20 20 20 20 22 77 69 6e 55 6e 6c 6f 63         "winUnloc
13b98 6b 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68  k", pFile->zPath
13b99 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
13b9a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45    if( type>=RESE
13b9b 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0d 0a 20 20  RVED_LOCK ){..  
13b9c 20 20 6f 73 55 6e 6c 6f 63 6b 46 69 6c 65 28 70    osUnlockFile(p
13b9d 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
13b9e 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
13b9f 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6c 6f  ;..  }..  if( lo
13ba0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
13ba1 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f  && type>=SHARED_
13ba2 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 75 6e 6c  LOCK ){..    unl
13ba3 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  ockReadLock(pFil
13ba4 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  e);..  }..  if( 
13ba5 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
13ba6 43 4b 20 29 7b 0d 0a 20 20 20 20 6f 73 55 6e 6c  CK ){..    osUnl
13ba7 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
13ba8 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
13ba9 30 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a  0, 1, 0);..  }..
13baa 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
13bab 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65  e = (u8)locktype
13bac 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
13bad 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20  .}..../*..** If 
13bae 2a 70 41 72 67 20 69 73 20 69 6e 69 74 69 74 69  *pArg is inititi
13baf 61 6c 6c 79 20 6e 65 67 61 74 69 76 65 20 74 68  ally negative th
13bb0 65 6e 20 74 68 69 73 20 69 73 20 61 20 71 75 65  en this is a que
13bb1 72 79 2e 20 20 53 65 74 20 2a 70 41 72 67 20 74  ry.  Set *pArg t
13bb2 6f 0d 0a 2a 2a 20 31 20 6f 72 20 30 20 64 65 70  o..** 1 or 0 dep
13bb3 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
13bb4 72 20 6f 72 20 6e 6f 74 20 62 69 74 20 6d 61 73  r or not bit mas
13bb5 6b 20 6f 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c  k of pFile->ctrl
13bb6 46 6c 61 67 73 20 69 73 20 73 65 74 2e 0d 0a 2a  Flags is set...*
13bb7 2a 0d 0a 2a 2a 20 49 66 20 2a 70 41 72 67 20 69  *..** If *pArg i
13bb8 73 20 30 20 6f 72 20 31 2c 20 74 68 65 6e 20 63  s 0 or 1, then c
13bb9 6c 65 61 72 20 6f 72 20 73 65 74 20 74 68 65 20  lear or set the 
13bba 6d 61 73 6b 20 62 69 74 20 6f 66 20 70 46 69 6c  mask bit of pFil
13bbb 65 2d 3e 63 74 72 6c 46 6c 61 67 73 2e 0d 0a 2a  e->ctrlFlags...*
13bbc 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  /..static void w
13bbd 69 6e 4d 6f 64 65 42 69 74 28 77 69 6e 46 69 6c  inModeBit(winFil
13bbe 65 20 2a 70 46 69 6c 65 2c 20 75 6e 73 69 67 6e  e *pFile, unsign
13bbf 65 64 20 63 68 61 72 20 6d 61 73 6b 2c 20 69 6e  ed char mask, in
13bc0 74 20 2a 70 41 72 67 29 7b 0d 0a 20 20 69 66 28  t *pArg){..  if(
13bc1 20 2a 70 41 72 67 3c 30 20 29 7b 0d 0a 20 20 20   *pArg<0 ){..   
13bc2 20 2a 70 41 72 67 20 3d 20 28 70 46 69 6c 65 2d   *pArg = (pFile-
13bc3 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 6d 61 73  >ctrlFlags & mas
13bc4 6b 29 21 3d 30 3b 0d 0a 20 20 7d 65 6c 73 65 20  k)!=0;..  }else 
13bc5 69 66 28 20 28 2a 70 41 72 67 29 3d 3d 30 20 29  if( (*pArg)==0 )
13bc6 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74  {..    pFile->ct
13bc7 72 6c 46 6c 61 67 73 20 26 3d 20 7e 6d 61 73 6b  rlFlags &= ~mask
13bc8 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
13bc9 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67   pFile->ctrlFlag
13bca 73 20 7c 3d 20 6d 61 73 6b 3b 0d 0a 20 20 7d 0d  s |= mask;..  }.
13bcb 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e  .}..../*..** Con
13bcc 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f  trol and query o
13bcd 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  f the open file 
13bce 68 61 6e 64 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  handle...*/..sta
13bcf 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43  tic int winFileC
13bd0 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
13bd1 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c  ile *id, int op,
13bd2 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0d 0a 20   void *pArg){.. 
13bd3 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
13bd4 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0d  = (winFile*)id;.
13bd5 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
13bd6 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
13bd7 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
13bd8 45 3a 20 7b 0d 0a 20 20 20 20 20 20 2a 28 69 6e  E: {..      *(in
13bd9 74 2a 29 70 41 72 67 20 3d 20 70 46 69 6c 65 2d  t*)pArg = pFile-
13bda 3e 6c 6f 63 6b 74 79 70 65 3b 0d 0a 20 20 20 20  >locktype;..    
13bdb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13bdc 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  OK;..    }..    
13bdd 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54  case SQLITE_LAST
13bde 5f 45 52 52 4e 4f 3a 20 7b 0d 0a 20 20 20 20 20  _ERRNO: {..     
13bdf 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
13be0 69 6e 74 29 70 46 69 6c 65 2d 3e 6c 61 73 74 45  int)pFile->lastE
13be1 72 72 6e 6f 3b 0d 0a 20 20 20 20 20 20 72 65 74  rrno;..      ret
13be2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
13be3 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
13be4 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 43 48 55  SQLITE_FCNTL_CHU
13be5 4e 4b 5f 53 49 5a 45 3a 20 7b 0d 0a 20 20 20 20  NK_SIZE: {..    
13be6 20 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b    pFile->szChunk
13be7 20 3d 20 2a 28 69 6e 74 20 2a 29 70 41 72 67 3b   = *(int *)pArg;
13be8 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
13be9 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
13bea 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
13beb 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e  E_FCNTL_SIZE_HIN
13bec 54 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  T: {..      if( 
13bed 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 3e 30  pFile->szChunk>0
13bee 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
13bef 69 74 65 33 5f 69 6e 74 36 34 20 6f 6c 64 53 7a  ite3_int64 oldSz
13bf0 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ;..        int r
13bf1 63 20 3d 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  c = winFileSize(
13bf2 69 64 2c 20 26 6f 6c 64 53 7a 29 3b 0d 0a 20 20  id, &oldSz);..  
13bf3 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13bf4 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
13bf5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
13bf6 74 36 34 20 6e 65 77 53 7a 20 3d 20 2a 28 73 71  t64 newSz = *(sq
13bf7 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 41 72  lite3_int64*)pAr
13bf8 67 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  g;..          if
13bf9 28 20 6e 65 77 53 7a 3e 6f 6c 64 53 7a 20 29 7b  ( newSz>oldSz ){
13bfa 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 69  ..            Si
13bfb 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
13bfc 69 67 6e 28 31 29 3b 0d 0a 20 20 20 20 20 20 20  ign(1);..       
13bfd 20 20 20 20 20 72 63 20 3d 20 77 69 6e 54 72 75       rc = winTru
13bfe 6e 63 61 74 65 28 69 64 2c 20 6e 65 77 53 7a 29  ncate(id, newSz)
13bff 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 53  ;..            S
13c00 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
13c01 6e 69 67 6e 28 30 29 3b 0d 0a 20 20 20 20 20 20  nign(0);..      
13c02 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
13c03 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
13c04 20 72 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20   rc;..      }.. 
13c05 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
13c06 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
13c07 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
13c08 43 4e 54 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c  CNTL_PERSIST_WAL
13c09 3a 20 7b 0d 0a 20 20 20 20 20 20 77 69 6e 4d 6f  : {..      winMo
13c0a 64 65 42 69 74 28 70 46 69 6c 65 2c 20 57 49 4e  deBit(pFile, WIN
13c0b 46 49 4c 45 5f 50 45 52 53 49 53 54 5f 57 41 4c  FILE_PERSIST_WAL
13c0c 2c 20 28 69 6e 74 2a 29 70 41 72 67 29 3b 0d 0a  , (int*)pArg);..
13c0d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
13c0e 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a  ITE_OK;..    }..
13c0f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
13c10 46 43 4e 54 4c 5f 50 4f 57 45 52 53 41 46 45 5f  FCNTL_POWERSAFE_
13c11 4f 56 45 52 57 52 49 54 45 3a 20 7b 0d 0a 20 20  OVERWRITE: {..  
13c12 20 20 20 20 77 69 6e 4d 6f 64 65 42 69 74 28 70      winModeBit(p
13c13 46 69 6c 65 2c 20 57 49 4e 46 49 4c 45 5f 50 53  File, WINFILE_PS
13c14 4f 57 2c 20 28 69 6e 74 2a 29 70 41 72 67 29 3b  OW, (int*)pArg);
13c15 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
13c16 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
13c17 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
13c18 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 4d 45 3a  E_FCNTL_VFSNAME:
13c19 20 7b 0d 0a 20 20 20 20 20 20 2a 28 63 68 61 72   {..      *(char
13c1a 2a 2a 29 70 41 72 67 20 3d 20 73 71 6c 69 74 65  **)pArg = sqlite
13c1b 33 5f 6d 70 72 69 6e 74 66 28 22 77 69 6e 33 32  3_mprintf("win32
13c1c 22 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  ");..      retur
13c1d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
13c1e 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
13c1f 4c 49 54 45 5f 46 43 4e 54 4c 5f 57 49 4e 33 32  LITE_FCNTL_WIN32
13c20 5f 41 56 5f 52 45 54 52 59 3a 20 7b 0d 0a 20 20  _AV_RETRY: {..  
13c21 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e      int *a = (in
13c22 74 2a 29 70 41 72 67 3b 0d 0a 20 20 20 20 20 20  t*)pArg;..      
13c23 69 66 28 20 61 5b 30 5d 3e 30 20 29 7b 0d 0a 20  if( a[0]>0 ){.. 
13c24 20 20 20 20 20 20 20 77 69 6e 33 32 49 6f 65 72         win32Ioer
13c25 72 52 65 74 72 79 20 3d 20 61 5b 30 5d 3b 0d 0a  rRetry = a[0];..
13c26 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
13c27 20 20 20 20 20 20 61 5b 30 5d 20 3d 20 77 69 6e        a[0] = win
13c28 33 32 49 6f 65 72 72 52 65 74 72 79 3b 0d 0a 20  32IoerrRetry;.. 
13c29 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
13c2a 28 20 61 5b 31 5d 3e 30 20 29 7b 0d 0a 20 20 20  ( a[1]>0 ){..   
13c2b 20 20 20 20 20 77 69 6e 33 32 49 6f 65 72 72 52       win32IoerrR
13c2c 65 74 72 79 44 65 6c 61 79 20 3d 20 61 5b 31 5d  etryDelay = a[1]
13c2d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
13c2e 0a 20 20 20 20 20 20 20 20 61 5b 31 5d 20 3d 20  .        a[1] = 
13c2f 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79 44  win32IoerrRetryD
13c30 65 6c 61 79 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  elay;..      }..
13c31 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
13c32 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a  ITE_OK;..    }..
13c33 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
13c34 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0d 0a  LITE_NOTFOUND;..
13c35 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
13c36 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
13c37 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
13c38 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
13c39 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0d 0a  ock device for..
13c3a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
13c3b 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
13c3c 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
13c3d 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
13c3e 62 65 0d 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f  be..** larger fo
13c3f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0d  r some devices..
13c40 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 63  .**..** SQLite c
13c41 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
13c42 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
13c43 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
13c44 73 73 75 6d 65 73 20 74 68 61 74 0d 0a 2a 2a 20  ssumes that..** 
13c45 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65  if two files are
13c46 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
13c47 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d  same file-system
13c48 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e   directory (i.e.
13c49 0d 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20  ..** a database 
13c4a 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20  and its journal 
13c4b 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73  file) that the s
13c4c 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20  ector size will 
13c4d 62 65 20 74 68 65 0d 0a 2a 2a 20 73 61 6d 65 20  be the..** same 
13c4e 66 6f 72 20 62 6f 74 68 2e 0d 0a 2a 2f 0d 0a 73  for both...*/..s
13c4f 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63  tatic int winSec
13c50 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  torSize(sqlite3_
13c51 66 69 6c 65 20 2a 69 64 29 7b 0d 0a 20 20 28 76  file *id){..  (v
13c52 6f 69 64 29 69 64 3b 0d 0a 20 20 72 65 74 75 72  oid)id;..  retur
13c53 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  n SQLITE_DEFAULT
13c54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0d 0a 7d  _SECTOR_SIZE;..}
13c55 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
13c56 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65  n a vector of de
13c57 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73  vice characteris
13c58 74 69 63 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  tics...*/..stati
13c59 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 43  c int winDeviceC
13c5a 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
13c5b 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
13c5c 7b 0d 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 20  {..  winFile *p 
13c5d 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0d  = (winFile*)id;.
13c5e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
13c5f 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41 42  _IOCAP_UNDELETAB
13c60 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20 7c 0d 0a  LE_WHEN_OPEN |..
13c61 20 20 20 20 20 20 20 20 20 28 28 70 2d 3e 63 74           ((p->ct
13c62 72 6c 46 6c 61 67 73 20 26 20 57 49 4e 46 49 4c  rlFlags & WINFIL
13c63 45 5f 50 53 4f 57 29 3f 53 51 4c 49 54 45 5f 49  E_PSOW)?SQLITE_I
13c64 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f  OCAP_POWERSAFE_O
13c65 56 45 52 57 52 49 54 45 3a 30 29 3b 0d 0a 7d 0d  VERWRITE:0);..}.
13c66 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
13c67 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 0d 0a 2f 2a  E_OMIT_WAL..../*
13c68 20 0d 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 77 69   ..** Windows wi
13c69 6c 6c 20 6f 6e 6c 79 20 6c 65 74 20 79 6f 75 20  ll only let you 
13c6a 63 72 65 61 74 65 20 66 69 6c 65 20 76 69 65 77  create file view
13c6b 20 6d 61 70 70 69 6e 67 73 0d 0a 2a 2a 20 6f 6e   mappings..** on
13c6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
13c6d 20 67 72 61 6e 75 6c 61 72 69 74 79 20 62 6f 75   granularity bou
13c6e 6e 64 61 72 69 65 73 2e 0d 0a 2a 2a 20 44 75 72  ndaries...** Dur
13c6f 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  ing sqlite3_os_i
13c70 6e 69 74 28 29 20 77 65 20 64 6f 20 61 20 47 65  nit() we do a Ge
13c71 74 53 79 73 74 65 6d 49 6e 66 6f 28 29 0d 0a 2a  tSystemInfo()..*
13c72 2a 20 74 6f 20 67 65 74 20 74 68 65 20 67 72 61  * to get the gra
13c73 6e 75 6c 61 72 69 74 79 20 73 69 7a 65 2e 0d 0a  nularity size...
13c74 2a 2f 0d 0a 53 59 53 54 45 4d 5f 49 4e 46 4f 20  */..SYSTEM_INFO 
13c75 77 69 6e 53 79 73 49 6e 66 6f 3b 0d 0a 0d 0a 2f  winSysInfo;..../
13c76 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e  *..** Helper fun
13c77 63 74 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e  ctions to obtain
13c78 20 61 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20   and relinquish 
13c79 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  the global mutex
13c7a 2e 20 54 68 65 0d 0a 2a 2a 20 67 6c 6f 62 61 6c  . The..** global
13c7b 20 6d 75 74 65 78 20 69 73 20 75 73 65 64 20 74   mutex is used t
13c7c 6f 20 70 72 6f 74 65 63 74 20 74 68 65 20 77 69  o protect the wi
13c7d 6e 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74  nLockInfo object
13c7e 73 20 75 73 65 64 20 62 79 20 0d 0a 2a 2a 20 74  s used by ..** t
13c7f 68 69 73 20 66 69 6c 65 2c 20 61 6c 6c 20 6f 66  his file, all of
13c80 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 73 68   which may be sh
13c81 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65  ared by multiple
13c82 20 74 68 72 65 61 64 73 2e 0d 0a 2a 2a 0d 0a 2a   threads...**..*
13c83 2a 20 46 75 6e 63 74 69 6f 6e 20 77 69 6e 53 68  * Function winSh
13c84 6d 4d 75 74 65 78 48 65 6c 64 28 29 20 69 73 20  mMutexHeld() is 
13c85 75 73 65 64 20 74 6f 20 61 73 73 65 72 74 28 29  used to assert()
13c86 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c   that the global
13c87 20 6d 75 74 65 78 20 0d 0a 2a 2a 20 69 73 20 68   mutex ..** is h
13c88 65 6c 64 20 77 68 65 6e 20 72 65 71 75 69 72 65  eld when require
13c89 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  d. This function
13c8a 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73   is only used as
13c8b 20 70 61 72 74 20 6f 66 20 61 73 73 65 72 74 28   part of assert(
13c8c 29 20 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ) ..** statement
13c8d 73 2e 20 65 2e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  s. e.g...**..** 
13c8e 20 20 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74    winShmEnterMut
13c8f 65 78 28 29 0d 0a 2a 2a 20 20 20 20 20 61 73 73  ex()..**     ass
13c90 65 72 74 28 20 77 69 6e 53 68 6d 4d 75 74 65 78  ert( winShmMutex
13c91 48 65 6c 64 28 29 20 29 3b 0d 0a 2a 2a 20 20 20  Held() );..**   
13c92 77 69 6e 53 68 6d 4c 65 61 76 65 4d 75 74 65 78  winShmLeaveMutex
13c93 28 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  ()..*/..static v
13c94 6f 69 64 20 77 69 6e 53 68 6d 45 6e 74 65 72 4d  oid winShmEnterM
13c95 75 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20 20 73  utex(void){..  s
13c96 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
13c97 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  er(sqlite3MutexA
13c98 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
13c99 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
13c9a 29 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f  );..}..static vo
13c9b 69 64 20 77 69 6e 53 68 6d 4c 65 61 76 65 4d 75  id winShmLeaveMu
13c9c 74 65 78 28 76 6f 69 64 29 7b 0d 0a 20 20 73 71  tex(void){..  sq
13c9d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
13c9e 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
13c9f 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
13ca0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
13ca1 3b 0d 0a 7d 0d 0a 23 69 66 64 65 66 20 53 51 4c  ;..}..#ifdef SQL
13ca2 49 54 45 5f 44 45 42 55 47 0d 0a 73 74 61 74 69  ITE_DEBUG..stati
13ca3 63 20 69 6e 74 20 77 69 6e 53 68 6d 4d 75 74 65  c int winShmMute
13ca4 78 48 65 6c 64 28 76 6f 69 64 29 20 7b 0d 0a 20  xHeld(void) {.. 
13ca5 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
13ca6 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
13ca7 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
13ca8 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
13ca9 5f 4d 41 53 54 45 52 29 29 3b 0d 0a 7d 0d 0a 23  _MASTER));..}..#
13caa 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
13cab 4f 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20 72  Object used to r
13cac 65 70 72 65 73 65 6e 74 20 61 20 73 69 6e 67 6c  epresent a singl
13cad 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 6e  e file opened an
13cae 64 20 6d 6d 61 70 70 65 64 20 74 6f 20 70 72 6f  d mmapped to pro
13caf 76 69 64 65 0d 0a 2a 2a 20 73 68 61 72 65 64 20  vide..** shared 
13cb0 6d 65 6d 6f 72 79 2e 20 20 57 68 65 6e 20 6d 75  memory.  When mu
13cb1 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 61  ltiple threads a
13cb2 6c 6c 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ll reference the
13cb3 20 73 61 6d 65 0d 0a 2a 2a 20 6c 6f 67 2d 73 75   same..** log-su
13cb4 6d 6d 61 72 79 2c 20 65 61 63 68 20 74 68 72 65  mmary, each thre
13cb5 61 64 20 68 61 73 20 69 74 73 20 6f 77 6e 20 77  ad has its own w
13cb6 69 6e 46 69 6c 65 20 6f 62 6a 65 63 74 2c 20 62  inFile object, b
13cb7 75 74 20 74 68 65 79 20 61 6c 6c 0d 0a 2a 2a 20  ut they all..** 
13cb8 70 6f 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c  point to a singl
13cb9 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  e instance of th
13cba 69 73 20 6f 62 6a 65 63 74 2e 20 20 49 6e 20 6f  is object.  In o
13cbb 74 68 65 72 20 77 6f 72 64 73 2c 20 65 61 63 68  ther words, each
13cbc 0d 0a 2a 2a 20 6c 6f 67 2d 73 75 6d 6d 61 72 79  ..** log-summary
13cbd 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 6c 79 20   is opened only 
13cbe 6f 6e 63 65 20 70 65 72 20 70 72 6f 63 65 73 73  once per process
13cbf 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 77 69 6e 53 68 6d  ...**..** winShm
13cc0 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73 74  MutexHeld() must
13cc1 20 62 65 20 74 72 75 65 20 77 68 65 6e 20 63 72   be true when cr
13cc2 65 61 74 69 6e 67 20 6f 72 20 64 65 73 74 72 6f  eating or destro
13cc3 79 69 6e 67 0d 0a 2a 2a 20 74 68 69 73 20 6f 62  ying..** this ob
13cc4 6a 65 63 74 20 6f 72 20 77 68 69 6c 65 20 72 65  ject or while re
13cc5 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
13cc6 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
13cc7 69 65 6c 64 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ields:..**..**  
13cc8 20 20 20 20 6e 52 65 66 0d 0a 2a 2a 20 20 20 20      nRef..**    
13cc9 20 20 70 4e 65 78 74 20 0d 0a 2a 2a 0d 0a 2a 2a    pNext ..**..**
13cca 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
13ccb 69 65 6c 64 73 20 61 72 65 20 72 65 61 64 2d 6f  ields are read-o
13ccc 6e 6c 79 20 61 66 74 65 72 20 74 68 65 20 6f 62  nly after the ob
13ccd 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 3a  ject is created:
13cce 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20 20 20 20 20 66  ..** ..**      f
13ccf 69 64 0d 0a 2a 2a 20 20 20 20 20 20 7a 46 69 6c  id..**      zFil
13cd0 65 6e 61 6d 65 0d 0a 2a 2a 0d 0a 2a 2a 20 45 69  ename..**..** Ei
13cd1 74 68 65 72 20 77 69 6e 53 68 6d 4e 6f 64 65 2e  ther winShmNode.
13cd2 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
13cd3 6c 64 20 6f 72 20 77 69 6e 53 68 6d 4e 6f 64 65  ld or winShmNode
13cd4 2e 6e 52 65 66 3d 3d 30 20 61 6e 64 0d 0a 2a 2a  .nRef==0 and..**
13cd5 20 77 69 6e 53 68 6d 4d 75 74 65 78 48 65 6c 64   winShmMutexHeld
13cd6 28 29 20 69 73 20 74 72 75 65 20 77 68 65 6e 20  () is true when 
13cd7 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
13cd8 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 66 69 65  ng any other fie
13cd9 6c 64 0d 0a 2a 2a 20 69 6e 20 74 68 69 73 20 73  ld..** in this s
13cda 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a  tructure...**..*
13cdb 2f 0d 0a 73 74 72 75 63 74 20 77 69 6e 53 68 6d  /..struct winShm
13cdc 4e 6f 64 65 20 7b 0d 0a 20 20 73 71 6c 69 74 65  Node {..  sqlite
13cdd 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20  3_mutex *mutex; 
13cde 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f       /* Mutex to
13cdf 20 61 63 63 65 73 73 20 74 68 69 73 20 6f 62 6a   access this obj
13ce0 65 63 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  ect */..  char *
13ce1 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20  zFilename;      
13ce2 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
13ce3 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 77  the file */..  w
13ce4 69 6e 46 69 6c 65 20 68 46 69 6c 65 3b 20 20 20  inFile hFile;   
13ce5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
13ce6 65 20 68 61 6e 64 6c 65 20 66 72 6f 6d 20 77 69  e handle from wi
13ce7 6e 4f 70 65 6e 20 2a 2f 0d 0a 0d 0a 20 20 69 6e  nOpen */....  in
13ce8 74 20 73 7a 52 65 67 69 6f 6e 3b 20 20 20 20 20  t szRegion;     
13ce9 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
13cea 20 6f 66 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72   of shared-memor
13ceb 79 20 72 65 67 69 6f 6e 73 20 2a 2f 0d 0a 20 20  y regions */..  
13cec 69 6e 74 20 6e 52 65 67 69 6f 6e 3b 20 20 20 20  int nRegion;    
13ced 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
13cee 7a 65 20 6f 66 20 61 72 72 61 79 20 61 70 52 65  ze of array apRe
13cef 67 69 6f 6e 20 2a 2f 0d 0a 20 20 73 74 72 75 63  gion */..  struc
13cf0 74 20 53 68 6d 52 65 67 69 6f 6e 20 7b 0d 0a 20  t ShmRegion {.. 
13cf1 20 20 20 48 41 4e 44 4c 45 20 68 4d 61 70 3b 20     HANDLE hMap; 
13cf2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
13cf3 69 6c 65 20 68 61 6e 64 6c 65 20 66 72 6f 6d 20  ile handle from 
13cf4 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
13cf5 67 20 2a 2f 0d 0a 20 20 20 20 76 6f 69 64 20 2a  g */..    void *
13cf6 70 4d 61 70 3b 0d 0a 20 20 7d 20 2a 61 52 65 67  pMap;..  } *aReg
13cf7 69 6f 6e 3b 0d 0a 20 20 44 57 4f 52 44 20 6c 61  ion;..  DWORD la
13cf8 73 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20  stErrno;        
13cf9 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64 6f 77     /* The Window
13cfa 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65  s errno from the
13cfb 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20   last I/O error 
13cfc 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 6e 52 65 66  */....  int nRef
13cfd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13cfe 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
13cff 77 69 6e 53 68 6d 20 6f 62 6a 65 63 74 73 20 70  winShm objects p
13d00 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 69 73 20  ointing to this 
13d01 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 20 2a 70 46  */..  winShm *pF
13d02 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
13d03 20 2f 2a 20 41 6c 6c 20 77 69 6e 53 68 6d 20 6f   /* All winShm o
13d04 62 6a 65 63 74 73 20 70 6f 69 6e 74 69 6e 67 20  bjects pointing 
13d05 74 6f 20 74 68 69 73 20 2a 2f 0d 0a 20 20 77 69  to this */..  wi
13d06 6e 53 68 6d 4e 6f 64 65 20 2a 70 4e 65 78 74 3b  nShmNode *pNext;
13d07 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
13d08 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20   in list of all 
13d09 77 69 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63  winShmNode objec
13d0a 74 73 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ts */..#ifdef SQ
13d0b 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 75 38  LITE_DEBUG..  u8
13d0c 20 6e 65 78 74 53 68 6d 49 64 3b 20 20 20 20 20   nextShmId;     
13d0d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
13d0e 20 61 76 61 69 6c 61 62 6c 65 20 77 69 6e 53 68   available winSh
13d0f 6d 2e 69 64 20 76 61 6c 75 65 20 2a 2f 0d 0a 23  m.id value */..#
13d10 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  endif..};..../*.
13d11 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 61 72 72  .** A global arr
13d12 61 79 20 6f 66 20 61 6c 6c 20 77 69 6e 53 68 6d  ay of all winShm
13d13 4e 6f 64 65 20 6f 62 6a 65 63 74 73 2e 0d 0a 2a  Node objects...*
13d14 2a 0d 0a 2a 2a 20 54 68 65 20 77 69 6e 53 68 6d  *..** The winShm
13d15 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73 74  MutexHeld() must
13d16 20 62 65 20 74 72 75 65 20 77 68 69 6c 65 20 72   be true while r
13d17 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
13d18 67 20 74 68 69 73 20 6c 69 73 74 2e 0d 0a 2a 2f  g this list...*/
13d19 0d 0a 73 74 61 74 69 63 20 77 69 6e 53 68 6d 4e  ..static winShmN
13d1a 6f 64 65 20 2a 77 69 6e 53 68 6d 4e 6f 64 65 4c  ode *winShmNodeL
13d1b 69 73 74 20 3d 20 30 3b 0d 0a 0d 0a 2f 2a 0d 0a  ist = 0;..../*..
13d1c 2a 2a 20 53 74 72 75 63 74 75 72 65 20 75 73 65  ** Structure use
13d1d 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  d internally by 
13d1e 74 68 69 73 20 56 46 53 20 74 6f 20 72 65 63 6f  this VFS to reco
13d1f 72 64 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  rd the state of 
13d20 61 6e 0d 0a 2a 2a 20 6f 70 65 6e 20 73 68 61 72  an..** open shar
13d21 65 64 20 6d 65 6d 6f 72 79 20 63 6f 6e 6e 65 63  ed memory connec
13d22 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  tion...**..** Th
13d23 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c  e following fiel
13d24 64 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a  ds are initializ
13d25 65 64 20 77 68 65 6e 20 74 68 69 73 20 6f 62 6a  ed when this obj
13d26 65 63 74 20 69 73 20 63 72 65 61 74 65 64 20 61  ect is created a
13d27 6e 64 0d 0a 2a 2a 20 61 72 65 20 72 65 61 64 2d  nd..** are read-
13d28 6f 6e 6c 79 20 74 68 65 72 65 61 66 74 65 72 3a  only thereafter:
13d29 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 77 69 6e 53  ..**..**    winS
13d2a 68 6d 2e 70 53 68 6d 4e 6f 64 65 0d 0a 2a 2a 20  hm.pShmNode..** 
13d2b 20 20 20 77 69 6e 53 68 6d 2e 69 64 0d 0a 2a 2a     winShm.id..**
13d2c 0d 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 66  ..** All other f
13d2d 69 65 6c 64 73 20 61 72 65 20 72 65 61 64 2f 77  ields are read/w
13d2e 72 69 74 65 2e 20 20 54 68 65 20 77 69 6e 53 68  rite.  The winSh
13d2f 6d 2e 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65  m.pShmNode->mute
13d30 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0d 0a  x must be held..
13d31 2a 2a 20 77 68 69 6c 65 20 61 63 63 65 73 73 69  ** while accessi
13d32 6e 67 20 61 6e 79 20 72 65 61 64 2f 77 72 69 74  ng any read/writ
13d33 65 20 66 69 65 6c 64 73 2e 0d 0a 2a 2f 0d 0a 73  e fields...*/..s
13d34 74 72 75 63 74 20 77 69 6e 53 68 6d 20 7b 0d 0a  truct winShm {..
13d35 20 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53    winShmNode *pS
13d36 68 6d 4e 6f 64 65 3b 20 20 20 20 20 20 2f 2a 20  hmNode;      /* 
13d37 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 77  The underlying w
13d38 69 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74  inShmNode object
13d39 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 20 2a 70   */..  winShm *p
13d3a 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
13d3b 20 20 2f 2a 20 4e 65 78 74 20 77 69 6e 53 68 6d    /* Next winShm
13d3c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 77   with the same w
13d3d 69 6e 53 68 6d 4e 6f 64 65 20 2a 2f 0d 0a 20 20  inShmNode */..  
13d3e 75 38 20 68 61 73 4d 75 74 65 78 3b 20 20 20 20  u8 hasMutex;    
13d3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
13d40 75 65 20 69 66 20 68 6f 6c 64 69 6e 67 20 74 68  ue if holding th
13d41 65 20 77 69 6e 53 68 6d 4e 6f 64 65 20 6d 75 74  e winShmNode mut
13d42 65 78 20 2a 2f 0d 0a 20 20 75 31 36 20 73 68 61  ex */..  u16 sha
13d43 72 65 64 4d 61 73 6b 3b 20 20 20 20 20 20 20 20  redMask;        
13d44 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73      /* Mask of s
13d45 68 61 72 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64  hared locks held
13d46 20 2a 2f 0d 0a 20 20 75 31 36 20 65 78 63 6c 4d   */..  u16 exclM
13d47 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
13d48 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63    /* Mask of exc
13d49 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c  lusive locks hel
13d4a 64 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c  d */..#ifdef SQL
13d4b 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 75 38 20  ITE_DEBUG..  u8 
13d4c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  id;             
13d4d 20 20 20 20 20 20 20 20 2f 2a 20 49 64 20 6f 66          /* Id of
13d4e 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
13d4f 20 77 69 74 68 20 69 74 73 20 77 69 6e 53 68 6d   with its winShm
13d50 4e 6f 64 65 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  Node */..#endif.
13d51 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f  .};..../*..** Co
13d52 6e 73 74 61 6e 74 73 20 75 73 65 64 20 66 6f 72  nstants used for
13d53 20 6c 6f 63 6b 69 6e 67 0d 0a 2a 2f 0d 0a 23 64   locking..*/..#d
13d54 65 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f 42 41  efine WIN_SHM_BA
13d55 53 45 20 20 20 28 28 32 32 2b 53 51 4c 49 54 45  SE   ((22+SQLITE
13d56 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34 29 20 20  _SHM_NLOCK)*4)  
13d57 20 20 20 20 20 20 2f 2a 20 66 69 72 73 74 20 6c        /* first l
13d58 6f 63 6b 20 62 79 74 65 20 2a 2f 0d 0a 23 64 65  ock byte */..#de
13d59 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f 44 4d 53  fine WIN_SHM_DMS
13d5a 20 20 20 20 28 57 49 4e 5f 53 48 4d 5f 42 41 53      (WIN_SHM_BAS
13d5b 45 2b 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f  E+SQLITE_SHM_NLO
13d5c 43 4b 29 20 20 2f 2a 20 64 65 61 64 6d 61 6e 20  CK)  /* deadman 
13d5d 73 77 69 74 63 68 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  switch */..../*.
13d5e 0a 2a 2a 20 41 70 70 6c 79 20 61 64 76 69 73 6f  .** Apply adviso
13d5f 72 79 20 6c 6f 63 6b 73 20 66 6f 72 20 61 6c 6c  ry locks for all
13d60 20 6e 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69   n bytes beginni
13d61 6e 67 20 61 74 20 6f 66 73 74 2e 0d 0a 2a 2f 0d  ng at ofst...*/.
13d62 0a 23 64 65 66 69 6e 65 20 5f 53 48 4d 5f 55 4e  .#define _SHM_UN
13d63 4c 43 4b 20 20 31 0d 0a 23 64 65 66 69 6e 65 20  LCK  1..#define 
13d64 5f 53 48 4d 5f 52 44 4c 43 4b 20 20 32 0d 0a 23  _SHM_RDLCK  2..#
13d65 64 65 66 69 6e 65 20 5f 53 48 4d 5f 57 52 4c 43  define _SHM_WRLC
13d66 4b 20 20 33 0d 0a 73 74 61 74 69 63 20 69 6e 74  K  3..static int
13d67 20 77 69 6e 53 68 6d 53 79 73 74 65 6d 4c 6f 63   winShmSystemLoc
13d68 6b 28 0d 0a 20 20 77 69 6e 53 68 6d 4e 6f 64 65  k(..  winShmNode
13d69 20 2a 70 46 69 6c 65 2c 20 20 20 20 2f 2a 20 41   *pFile,    /* A
13d6a 70 70 6c 79 20 6c 6f 63 6b 73 20 74 6f 20 74 68  pply locks to th
13d6b 69 73 20 6f 70 65 6e 20 73 68 61 72 65 64 2d 6d  is open shared-m
13d6c 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 2a 2f  emory segment */
13d6d 0d 0a 20 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65  ..  int lockType
13d6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 5f 53 48  ,         /* _SH
13d6f 4d 5f 55 4e 4c 43 4b 2c 20 5f 53 48 4d 5f 52 44  M_UNLCK, _SHM_RD
13d70 4c 43 4b 2c 20 6f 72 20 5f 53 48 4d 5f 57 52 4c  LCK, or _SHM_WRL
13d71 43 4b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 66 73  CK */..  int ofs
13d72 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t,             /
13d73 2a 20 4f 66 66 73 65 74 20 74 6f 20 66 69 72 73  * Offset to firs
13d74 74 20 62 79 74 65 20 74 6f 20 62 65 20 6c 6f 63  t byte to be loc
13d75 6b 65 64 2f 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0d  ked/unlocked */.
13d76 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 20 20 20  .  int nByte    
13d77 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
13d78 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c  er of bytes to l
13d79 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f  ock or unlock */
13d7a 0d 0a 29 7b 0d 0a 20 20 4f 56 45 52 4c 41 50 50  ..){..  OVERLAPP
13d7b 45 44 20 6f 76 6c 70 3b 0d 0a 20 20 44 57 4f 52  ED ovlp;..  DWOR
13d7c 44 20 64 77 46 6c 61 67 73 3b 0d 0a 20 20 69 6e  D dwFlags;..  in
13d7d 74 20 72 63 20 3d 20 30 3b 20 20 20 20 20 20 20  t rc = 0;       
13d7e 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
13d7f 64 65 20 66 6f 72 6d 20 4c 6f 63 6b 2f 55 6e 6c  de form Lock/Unl
13d80 6f 63 6b 46 69 6c 65 45 78 28 29 20 2a 2f 0d 0a  ockFileEx() */..
13d81 0d 0a 20 20 2f 2a 20 41 63 63 65 73 73 20 74 6f  ..  /* Access to
13d82 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65 20   the winShmNode 
13d83 6f 62 6a 65 63 74 20 69 73 20 73 65 72 69 61 6c  object is serial
13d84 69 7a 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  ized by the call
13d85 65 72 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  er */..  assert(
13d86 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
13d87 65 6c 64 28 70 46 69 6c 65 2d 3e 6d 75 74 65 78  eld(pFile->mutex
13d88 29 20 7c 7c 20 70 46 69 6c 65 2d 3e 6e 52 65 66  ) || pFile->nRef
13d89 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  ==0 );....  /* I
13d8a 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f  nitialize the lo
13d8b 63 6b 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73  cking parameters
13d8c 20 2a 2f 0d 0a 20 20 64 77 46 6c 61 67 73 20 3d   */..  dwFlags =
13d8d 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
13d8e 4d 4d 45 44 49 41 54 45 4c 59 3b 0d 0a 20 20 69  MMEDIATELY;..  i
13d8f 66 28 20 6c 6f 63 6b 54 79 70 65 20 3d 3d 20 5f  f( lockType == _
13d90 53 48 4d 5f 57 52 4c 43 4b 20 29 20 64 77 46 6c  SHM_WRLCK ) dwFl
13d91 61 67 73 20 7c 3d 20 4c 4f 43 4b 46 49 4c 45 5f  ags |= LOCKFILE_
13d92 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0d  EXCLUSIVE_LOCK;.
13d93 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 26 6f 76 6c  ...  memset(&ovl
13d94 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4f 56 45  p, 0, sizeof(OVE
13d95 52 4c 41 50 50 45 44 29 29 3b 0d 0a 20 20 6f 76  RLAPPED));..  ov
13d96 6c 70 2e 4f 66 66 73 65 74 20 3d 20 6f 66 73 74  lp.Offset = ofst
13d97 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6c 65 61 73  ;....  /* Releas
13d98 65 2f 41 63 71 75 69 72 65 20 74 68 65 20 73 79  e/Acquire the sy
13d99 73 74 65 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 20  stem-level lock 
13d9a 2a 2f 0d 0a 20 20 69 66 28 20 6c 6f 63 6b 54 79  */..  if( lockTy
13d9b 70 65 3d 3d 5f 53 48 4d 5f 55 4e 4c 43 4b 20 29  pe==_SHM_UNLCK )
13d9c 7b 0d 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e  {..    rc = osUn
13d9d 6c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65  lockFileEx(pFile
13d9e 2d 3e 68 46 69 6c 65 2e 68 2c 20 30 2c 20 6e 42  ->hFile.h, 0, nB
13d9f 79 74 65 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0d  yte, 0, &ovlp);.
13da0 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72  .  }else{..    r
13da1 63 20 3d 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 78  c = osLockFileEx
13da2 28 70 46 69 6c 65 2d 3e 68 46 69 6c 65 2e 68 2c  (pFile->hFile.h,
13da3 20 64 77 46 6c 61 67 73 2c 20 30 2c 20 6e 42 79   dwFlags, 0, nBy
13da4 74 65 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0d 0a  te, 0, &ovlp);..
13da5 20 20 7d 0d 0a 20 20 0d 0a 20 20 69 66 28 20 72    }..  ..  if( r
13da6 63 21 3d 20 30 20 29 7b 0d 0a 20 20 20 20 72 63  c!= 0 ){..    rc
13da7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
13da8 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 46 69   }else{..    pFi
13da9 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
13daa 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
13dab 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  );..    rc = SQL
13dac 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20 7d 0d 0a  ITE_BUSY;..  }..
13dad 0d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 53 48  ..  OSTRACE(("SH
13dae 4d 2d 4c 4f 43 4b 20 25 64 20 25 73 20 25 73 20  M-LOCK %d %s %s 
13daf 30 78 25 30 38 6c 78 5c 6e 22 2c 20 0d 0a 20 20  0x%08lx\n", ..  
13db0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
13db1 68 46 69 6c 65 2e 68 2c 0d 0a 20 20 20 20 20 20  hFile.h,..      
13db2 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f       rc==SQLITE_
13db3 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69  OK ? "ok" : "fai
13db4 6c 65 64 22 2c 0d 0a 20 20 20 20 20 20 20 20 20  led",..         
13db5 20 20 6c 6f 63 6b 54 79 70 65 3d 3d 5f 53 48 4d    lockType==_SHM
13db6 5f 55 4e 4c 43 4b 20 3f 20 22 55 6e 6c 6f 63 6b  _UNLCK ? "Unlock
13db7 46 69 6c 65 45 78 22 20 3a 20 22 4c 6f 63 6b 46  FileEx" : "LockF
13db8 69 6c 65 45 78 22 2c 0d 0a 20 20 20 20 20 20 20  ileEx",..       
13db9 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
13dba 72 72 6e 6f 29 29 3b 0d 0a 0d 0a 20 20 72 65 74  rrno));....  ret
13dbb 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
13dbc 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
13dbd 63 65 73 20 74 6f 20 56 46 53 20 6d 65 74 68 6f  ces to VFS metho
13dbe 64 73 20 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ds */..static in
13dbf 74 20 77 69 6e 4f 70 65 6e 28 73 71 6c 69 74 65  t winOpen(sqlite
13dc0 33 5f 76 66 73 2a 2c 63 6f 6e 73 74 20 63 68 61  3_vfs*,const cha
13dc1 72 2a 2c 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  r*,sqlite3_file*
13dc2 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0d 0a 73 74 61  ,int,int*);..sta
13dc3 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74  tic int winDelet
13dc4 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  e(sqlite3_vfs *,
13dc5 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
13dc6 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 75 72 67  ;..../*..** Purg
13dc7 65 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65  e the winShmNode
13dc8 4c 69 73 74 20 6c 69 73 74 20 6f 66 20 61 6c 6c  List list of all
13dc9 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 77 69   entries with wi
13dca 6e 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30  nShmNode.nRef==0
13dcb 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  ...**..** This i
13dcc 73 20 6e 6f 74 20 61 20 56 46 53 20 73 68 61 72  s not a VFS shar
13dcd 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64  ed-memory method
13dce 3b 20 69 74 20 69 73 20 61 20 75 74 69 6c 69 74  ; it is a utilit
13dcf 79 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65  y function calle
13dd0 64 0d 0a 2a 2a 20 62 79 20 56 46 53 20 73 68 61  d..** by VFS sha
13dd1 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74 68 6f  red-memory metho
13dd2 64 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ds...*/..static 
13dd3 76 6f 69 64 20 77 69 6e 53 68 6d 50 75 72 67 65  void winShmPurge
13dd4 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
13dd5 66 73 2c 20 69 6e 74 20 64 65 6c 65 74 65 46 6c  fs, int deleteFl
13dd6 61 67 29 7b 0d 0a 20 20 77 69 6e 53 68 6d 4e 6f  ag){..  winShmNo
13dd7 64 65 20 2a 2a 70 70 3b 0d 0a 20 20 77 69 6e 53  de **pp;..  winS
13dd8 68 6d 4e 6f 64 65 20 2a 70 3b 0d 0a 20 20 42 4f  hmNode *p;..  BO
13dd9 4f 4c 20 62 52 63 3b 0d 0a 20 20 61 73 73 65 72  OL bRc;..  asser
13dda 74 28 20 77 69 6e 53 68 6d 4d 75 74 65 78 48 65  t( winShmMutexHe
13ddb 6c 64 28 29 20 29 3b 0d 0a 20 20 70 70 20 3d 20  ld() );..  pp = 
13ddc 26 77 69 6e 53 68 6d 4e 6f 64 65 4c 69 73 74 3b  &winShmNodeList;
13ddd 0d 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20  ..  while( (p = 
13dde 2a 70 70 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  *pp)!=0 ){..    
13ddf 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  if( p->nRef==0 )
13de0 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0d  {..      int i;.
13de1 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6d 75  .      if( p->mu
13de2 74 65 78 20 29 20 73 71 6c 69 74 65 33 5f 6d 75  tex ) sqlite3_mu
13de3 74 65 78 5f 66 72 65 65 28 70 2d 3e 6d 75 74 65  tex_free(p->mute
13de4 78 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69  x);..      for(i
13de5 3d 30 3b 20 69 3c 70 2d 3e 6e 52 65 67 69 6f 6e  =0; i<p->nRegion
13de6 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; i++){..       
13de7 20 62 52 63 20 3d 20 6f 73 55 6e 6d 61 70 56 69   bRc = osUnmapVi
13de8 65 77 4f 66 46 69 6c 65 28 70 2d 3e 61 52 65 67  ewOfFile(p->aReg
13de9 69 6f 6e 5b 69 5d 2e 70 4d 61 70 29 3b 0d 0a 20  ion[i].pMap);.. 
13dea 20 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28         OSTRACE((
13deb 22 53 48 4d 2d 50 55 52 47 45 20 70 69 64 2d 25  "SHM-PURGE pid-%
13dec 64 20 75 6e 6d 61 70 20 72 65 67 69 6f 6e 3d 25  d unmap region=%
13ded 64 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  d %s\n",..      
13dee 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
13def 6f 73 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63  osGetCurrentProc
13df0 65 73 73 49 64 28 29 2c 20 69 2c 0d 0a 20 20 20  essId(), i,..   
13df1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 52                bR
13df2 63 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  c ? "ok" : "fail
13df3 65 64 22 29 29 3b 0d 0a 20 20 20 20 20 20 20 20  ed"));..        
13df4 62 52 63 20 3d 20 6f 73 43 6c 6f 73 65 48 61 6e  bRc = osCloseHan
13df5 64 6c 65 28 70 2d 3e 61 52 65 67 69 6f 6e 5b 69  dle(p->aRegion[i
13df6 5d 2e 68 4d 61 70 29 3b 0d 0a 20 20 20 20 20 20  ].hMap);..      
13df7 20 20 4f 53 54 52 41 43 45 28 28 22 53 48 4d 2d    OSTRACE(("SHM-
13df8 50 55 52 47 45 20 70 69 64 2d 25 64 20 63 6c 6f  PURGE pid-%d clo
13df9 73 65 20 72 65 67 69 6f 6e 3d 25 64 20 25 73 5c  se region=%d %s\
13dfa 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  n",..           
13dfb 20 20 20 20 20 20 28 69 6e 74 29 6f 73 47 65 74        (int)osGet
13dfc 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64  CurrentProcessId
13dfd 28 29 2c 20 69 2c 0d 0a 20 20 20 20 20 20 20 20  (), i,..        
13dfe 20 20 20 20 20 20 20 20 20 62 52 63 20 3f 20 22           bRc ? "
13dff 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29  ok" : "failed"))
13e00 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
13e01 20 20 69 66 28 20 70 2d 3e 68 46 69 6c 65 2e 68    if( p->hFile.h
13e02 20 21 3d 20 49 4e 56 41 4c 49 44 5f 48 41 4e 44   != INVALID_HAND
13e03 4c 45 5f 56 41 4c 55 45 20 29 7b 0d 0a 20 20 20  LE_VALUE ){..   
13e04 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
13e05 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0d 0a  rrorBenign(1);..
13e06 20 20 20 20 20 20 20 20 77 69 6e 43 6c 6f 73 65          winClose
13e07 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
13e08 29 26 70 2d 3e 68 46 69 6c 65 29 3b 0d 0a 20 20  )&p->hFile);..  
13e09 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
13e0a 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0d  ErrorBenign(0);.
13e0b 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
13e0c 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67 20 29  if( deleteFlag )
13e0d 7b 0d 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c  {..        Simul
13e0e 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
13e0f 28 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  (1);..        sq
13e10 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
13e11 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 20 20 20  Malloc();..     
13e12 20 20 20 77 69 6e 44 65 6c 65 74 65 28 70 56 66     winDelete(pVf
13e13 73 2c 20 70 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  s, p->zFilename,
13e14 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71   0);..        sq
13e15 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
13e16 6c 6c 6f 63 28 29 3b 0d 0a 20 20 20 20 20 20 20  lloc();..       
13e17 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
13e18 42 65 6e 69 67 6e 28 30 29 3b 0d 0a 20 20 20 20  Benign(0);..    
13e19 20 20 7d 0d 0a 20 20 20 20 20 20 2a 70 70 20 3d    }..      *pp =
13e1a 20 70 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20   p->pNext;..    
13e1b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
13e1c 2d 3e 61 52 65 67 69 6f 6e 29 3b 0d 0a 20 20 20  ->aRegion);..   
13e1d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
13e1e 70 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  p);..    }else{.
13e1f 0a 20 20 20 20 20 20 70 70 20 3d 20 26 70 2d 3e  .      pp = &p->
13e20 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  pNext;..    }.. 
13e21 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
13e22 4f 70 65 6e 20 74 68 65 20 73 68 61 72 65 64 2d  Open the shared-
13e23 6d 65 6d 6f 72 79 20 61 72 65 61 20 61 73 73 6f  memory area asso
13e24 63 69 61 74 65 64 20 77 69 74 68 20 64 61 74 61  ciated with data
13e25 62 61 73 65 20 66 69 6c 65 20 70 44 62 46 64 2e  base file pDbFd.
13e26 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 6f 70  ..**..** When op
13e27 65 6e 69 6e 67 20 61 20 6e 65 77 20 73 68 61 72  ening a new shar
13e28 65 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2c 20  ed-memory file, 
13e29 69 66 20 6e 6f 20 6f 74 68 65 72 20 69 6e 73 74  if no other inst
13e2a 61 6e 63 65 73 20 6f 66 20 74 68 61 74 0d 0a 2a  ances of that..*
13e2b 2a 20 66 69 6c 65 20 61 72 65 20 63 75 72 72 65  * file are curre
13e2c 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 20 74 68  ntly open, in th
13e2d 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e  is process or in
13e2e 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
13e2f 2c 20 74 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 66  , then..** the f
13e30 69 6c 65 20 6d 75 73 74 20 62 65 20 74 72 75 6e  ile must be trun
13e31 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 6c 65  cated to zero le
13e32 6e 67 74 68 20 6f 72 20 68 61 76 65 20 69 74 73  ngth or have its
13e33 20 68 65 61 64 65 72 20 63 6c 65 61 72 65 64 2e   header cleared.
13e34 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
13e35 20 77 69 6e 4f 70 65 6e 53 68 61 72 65 64 4d 65   winOpenSharedMe
13e36 6d 6f 72 79 28 77 69 6e 46 69 6c 65 20 2a 70 44  mory(winFile *pD
13e37 62 46 64 29 7b 0d 0a 20 20 73 74 72 75 63 74 20  bFd){..  struct 
13e38 77 69 6e 53 68 6d 20 2a 70 3b 20 20 20 20 20 20  winShm *p;      
13e39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
13e3a 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  he connection to
13e3b 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0d 0a 20   be opened */.. 
13e3c 20 73 74 72 75 63 74 20 77 69 6e 53 68 6d 4e 6f   struct winShmNo
13e3d 64 65 20 2a 70 53 68 6d 4e 6f 64 65 20 3d 20 30  de *pShmNode = 0
13e3e 3b 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72  ;   /* The under
13e3f 6c 79 69 6e 67 20 6d 6d 61 70 70 65 64 20 66 69  lying mmapped fi
13e40 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b  le */..  int rc;
13e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e42 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
13e43 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0d 0a 20  esult code */.. 
13e44 20 73 74 72 75 63 74 20 77 69 6e 53 68 6d 4e 6f   struct winShmNo
13e45 64 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20  de *pNew;       
13e46 20 20 20 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c      /* Newly all
13e47 6f 63 61 74 65 64 20 77 69 6e 53 68 6d 4e 6f 64  ocated winShmNod
13e48 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e 61 6d  e */..  int nNam
13e49 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
13e4a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
13e4b 7a 65 20 6f 66 20 7a 4e 61 6d 65 20 69 6e 20 62  ze of zName in b
13e4c 79 74 65 73 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  ytes */....  ass
13e4d 65 72 74 28 20 70 44 62 46 64 2d 3e 70 53 68 6d  ert( pDbFd->pShm
13e4e 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 4e 6f 74  ==0 );    /* Not
13e4f 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
13e50 65 64 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 41 6c  ed */....  /* Al
13e51 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
13e52 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33   the new sqlite3
13e53 5f 73 68 6d 20 6f 62 6a 65 63 74 2e 20 20 41 6c  _shm object.  Al
13e54 73 6f 20 73 70 65 63 75 6c 61 74 69 76 65 6c 79  so speculatively
13e55 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20  ..  ** allocate 
13e56 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20  space for a new 
13e57 77 69 6e 53 68 6d 4e 6f 64 65 20 61 6e 64 20 66  winShmNode and f
13e58 69 6c 65 6e 61 6d 65 2e 0d 0a 20 20 2a 2f 0d 0a  ilename...  */..
13e59 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61    p = sqlite3_ma
13e5a 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 29  lloc( sizeof(*p)
13e5b 20 29 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20   );..  if( p==0 
13e5c 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
13e5d 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  IOERR_NOMEM;..  
13e5e 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
13e5f 65 6f 66 28 2a 70 29 29 3b 0d 0a 20 20 6e 4e 61  eof(*p));..  nNa
13e60 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
13e61 65 6e 33 30 28 70 44 62 46 64 2d 3e 7a 50 61 74  en30(pDbFd->zPat
13e62 68 29 3b 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71  h);..  pNew = sq
13e63 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
13e64 7a 65 6f 66 28 2a 70 53 68 6d 4e 6f 64 65 29 20  zeof(*pShmNode) 
13e65 2b 20 6e 4e 61 6d 65 20 2b 20 31 37 20 29 3b 0d  + nName + 17 );.
13e66 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
13e67 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  {..    sqlite3_f
13e68 72 65 65 28 70 29 3b 0d 0a 20 20 20 20 72 65 74  ree(p);..    ret
13e69 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
13e6a 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20  _NOMEM;..  }..  
13e6b 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20  memset(pNew, 0, 
13e6c 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20  sizeof(*pNew) + 
13e6d 6e 4e 61 6d 65 20 2b 20 31 37 29 3b 0d 0a 20 20  nName + 17);..  
13e6e 70 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20  pNew->zFilename 
13e6f 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31  = (char*)&pNew[1
13e70 5d 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  ];..  sqlite3_sn
13e71 70 72 69 6e 74 66 28 6e 4e 61 6d 65 2b 31 35 2c  printf(nName+15,
13e72 20 70 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65   pNew->zFilename
13e73 2c 20 22 25 73 2d 73 68 6d 22 2c 20 70 44 62 46  , "%s-shm", pDbF
13e74 64 2d 3e 7a 50 61 74 68 29 3b 0d 0a 20 20 73 71  d->zPath);..  sq
13e75 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78 33  lite3FileSuffix3
13e76 28 70 44 62 46 64 2d 3e 7a 50 61 74 68 2c 20 70  (pDbFd->zPath, p
13e77 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b  New->zFilename);
13e78 20 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 74   ....  /* Look t
13e79 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20 69  o see if there i
13e7a 73 20 61 6e 20 65 78 69 73 74 69 6e 67 20 77 69  s an existing wi
13e7b 6e 53 68 6d 4e 6f 64 65 20 74 68 61 74 20 63 61  nShmNode that ca
13e7c 6e 20 62 65 20 75 73 65 64 2e 0d 0a 20 20 2a 2a  n be used...  **
13e7d 20 49 66 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20   If no matching 
13e7e 77 69 6e 53 68 6d 4e 6f 64 65 20 63 75 72 72 65  winShmNode curre
13e7f 6e 74 6c 79 20 65 78 69 73 74 73 2c 20 63 72 65  ntly exists, cre
13e80 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e 0d 0a  ate a new one...
13e81 20 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 45 6e    */..  winShmEn
13e82 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 66  terMutex();..  f
13e83 6f 72 28 70 53 68 6d 4e 6f 64 65 20 3d 20 77 69  or(pShmNode = wi
13e84 6e 53 68 6d 4e 6f 64 65 4c 69 73 74 3b 20 70 53  nShmNodeList; pS
13e85 68 6d 4e 6f 64 65 3b 20 70 53 68 6d 4e 6f 64 65  hmNode; pShmNode
13e86 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 4e 65 78 74  =pShmNode->pNext
13e87 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 42 44 20 6e  ){..    /* TBD n
13e88 65 65 64 20 74 6f 20 63 6f 6d 65 20 75 70 20 77  eed to come up w
13e89 69 74 68 20 62 65 74 74 65 72 20 6d 61 74 63 68  ith better match
13e8a 20 68 65 72 65 2e 20 20 50 65 72 68 61 70 73 0d   here.  Perhaps.
13e8b 0a 20 20 20 20 2a 2a 20 75 73 65 20 46 49 4c 45  .    ** use FILE
13e8c 5f 49 44 5f 42 4f 54 48 5f 44 49 52 5f 49 4e 46  _ID_BOTH_DIR_INF
13e8d 4f 20 53 74 72 75 63 74 75 72 65 2e 0d 0a 20 20  O Structure...  
13e8e 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 71    */..    if( sq
13e8f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 53 68  lite3StrICmp(pSh
13e90 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65  mNode->zFilename
13e91 2c 20 70 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d  , pNew->zFilenam
13e92 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a  e)==0 ) break;..
13e93 20 20 7d 0d 0a 20 20 69 66 28 20 70 53 68 6d 4e    }..  if( pShmN
13e94 6f 64 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ode ){..    sqli
13e95 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0d  te3_free(pNew);.
13e96 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
13e97 53 68 6d 4e 6f 64 65 20 3d 20 70 4e 65 77 3b 0d  ShmNode = pNew;.
13e98 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0d 0a  .    pNew = 0;..
13e99 20 20 20 20 28 28 77 69 6e 46 69 6c 65 2a 29 28      ((winFile*)(
13e9a 26 70 53 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65  &pShmNode->hFile
13e9b 29 29 2d 3e 68 20 3d 20 49 4e 56 41 4c 49 44 5f  ))->h = INVALID_
13e9c 48 41 4e 44 4c 45 5f 56 41 4c 55 45 3b 0d 0a 20  HANDLE_VALUE;.. 
13e9d 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 4e 65     pShmNode->pNe
13e9e 78 74 20 3d 20 77 69 6e 53 68 6d 4e 6f 64 65 4c  xt = winShmNodeL
13e9f 69 73 74 3b 0d 0a 20 20 20 20 77 69 6e 53 68 6d  ist;..    winShm
13ea0 4e 6f 64 65 4c 69 73 74 20 3d 20 70 53 68 6d 4e  NodeList = pShmN
13ea1 6f 64 65 3b 0d 0a 0d 0a 20 20 20 20 70 53 68 6d  ode;....    pShm
13ea2 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 3d 20 73 71  Node->mutex = sq
13ea3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
13ea4 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  c(SQLITE_MUTEX_F
13ea5 41 53 54 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  AST);..    if( p
13ea6 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 3d 3d  ShmNode->mutex==
13ea7 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  0 ){..      rc =
13ea8 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
13ea9 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  MEM;..      goto
13eaa 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0d 0a   shm_open_err;..
13eab 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 72 63 20      }....    rc 
13eac 3d 20 77 69 6e 4f 70 65 6e 28 70 44 62 46 64 2d  = winOpen(pDbFd-
13ead 3e 70 56 66 73 2c 0d 0a 20 20 20 20 20 20 20 20  >pVfs,..        
13eae 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64           pShmNod
13eaf 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  e->zFilename,   
13eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
13eb1 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55  e of the file (U
13eb2 54 46 2d 38 29 20 2a 2f 0d 0a 20 20 20 20 20 20  TF-8) */..      
13eb3 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69             (sqli
13eb4 74 65 33 5f 66 69 6c 65 2a 29 26 70 53 68 6d 4e  te3_file*)&pShmN
13eb5 6f 64 65 2d 3e 68 46 69 6c 65 2c 20 20 2f 2a 20  ode->hFile,  /* 
13eb6 46 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65  File handle here
13eb7 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20   */..           
13eb8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
13eb9 4e 5f 57 41 4c 20 7c 20 53 51 4c 49 54 45 5f 4f  N_WAL | SQLITE_O
13eba 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20  PEN_READWRITE | 
13ebb 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
13ebc 54 45 2c 20 2f 2a 20 4d 6f 64 65 20 66 6c 61 67  TE, /* Mode flag
13ebd 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20  s */..          
13ebe 20 20 20 20 20 20 20 30 29 3b 0d 0a 20 20 20 20         0);..    
13ebf 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
13ec0 63 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  c ){..      goto
13ec1 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0d 0a   shm_open_err;..
13ec2 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
13ec3 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
13ec4 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  another process 
13ec5 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 64  is holding the d
13ec6 65 61 64 2d 6d 61 6e 20 73 77 69 74 63 68 2e 0d  ead-man switch..
13ec7 0a 20 20 20 20 2a 2a 20 49 66 20 6e 6f 74 2c 20  .    ** If not, 
13ec8 74 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c  truncate the fil
13ec9 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68  e to zero length
13eca 2e 20 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  . ..    */..    
13ecb 69 66 28 20 77 69 6e 53 68 6d 53 79 73 74 65 6d  if( winShmSystem
13ecc 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f  Lock(pShmNode, _
13ecd 53 48 4d 5f 57 52 4c 43 4b 2c 20 57 49 4e 5f 53  SHM_WRLCK, WIN_S
13ece 48 4d 5f 44 4d 53 2c 20 31 29 3d 3d 53 51 4c 49  HM_DMS, 1)==SQLI
13ecf 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
13ed0 72 63 20 3d 20 77 69 6e 54 72 75 6e 63 61 74 65  rc = winTruncate
13ed1 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
13ed2 29 26 70 53 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c  )&pShmNode->hFil
13ed3 65 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66  e, 0);..      if
13ed4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13ed5 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
13ed6 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
13ed7 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 4f 50 45  ITE_IOERR_SHMOPE
13ed8 4e 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f  N, osGetLastErro
13ed9 72 28 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  r(),..          
13eda 20 20 20 20 20 20 20 22 77 69 6e 4f 70 65 6e 53         "winOpenS
13edb 68 6d 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 74  hm", pDbFd->zPat
13edc 68 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  h);..      }..  
13edd 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
13ede 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
13edf 20 20 20 20 20 77 69 6e 53 68 6d 53 79 73 74 65       winShmSyste
13ee0 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20  mLock(pShmNode, 
13ee1 5f 53 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f  _SHM_UNLCK, WIN_
13ee2 53 48 4d 5f 44 4d 53 2c 20 31 29 3b 0d 0a 20 20  SHM_DMS, 1);..  
13ee3 20 20 20 20 72 63 20 3d 20 77 69 6e 53 68 6d 53      rc = winShmS
13ee4 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f  ystemLock(pShmNo
13ee5 64 65 2c 20 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20  de, _SHM_RDLCK, 
13ee6 57 49 4e 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3b  WIN_SHM_DMS, 1);
13ee7 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
13ee8 20 72 63 20 29 20 67 6f 74 6f 20 73 68 6d 5f 6f   rc ) goto shm_o
13ee9 70 65 6e 5f 65 72 72 3b 0d 0a 20 20 7d 0d 0a 0d  pen_err;..  }...
13eea 0a 20 20 2f 2a 20 4d 61 6b 65 20 74 68 65 20 6e  .  /* Make the n
13eeb 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 20  ew connection a 
13eec 63 68 69 6c 64 20 6f 66 20 74 68 65 20 77 69 6e  child of the win
13eed 53 68 6d 4e 6f 64 65 20 2a 2f 0d 0a 20 20 70 2d  ShmNode */..  p-
13eee 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d  >pShmNode = pShm
13eef 4e 6f 64 65 3b 0d 0a 23 69 66 64 65 66 20 53 51  Node;..#ifdef SQ
13ef0 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 70 2d  LITE_DEBUG..  p-
13ef1 3e 69 64 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e  >id = pShmNode->
13ef2 6e 65 78 74 53 68 6d 49 64 2b 2b 3b 0d 0a 23 65  nextShmId++;..#e
13ef3 6e 64 69 66 0d 0a 20 20 70 53 68 6d 4e 6f 64 65  ndif..  pShmNode
13ef4 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20 70 44 62  ->nRef++;..  pDb
13ef5 46 64 2d 3e 70 53 68 6d 20 3d 20 70 3b 0d 0a 20  Fd->pShm = p;.. 
13ef6 20 77 69 6e 53 68 6d 4c 65 61 76 65 4d 75 74 65   winShmLeaveMute
13ef7 78 28 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65  x();....  /* The
13ef8 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
13ef9 20 6f 6e 20 70 53 68 6d 4e 6f 64 65 20 68 61 73   on pShmNode has
13efa 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e   already been in
13efb 63 72 65 6d 65 6e 74 65 64 20 75 6e 64 65 72 0d  cremented under.
13efc 0a 20 20 2a 2a 20 74 68 65 20 63 6f 76 65 72 20  .  ** the cover 
13efd 6f 66 20 74 68 65 20 77 69 6e 53 68 6d 45 6e 74  of the winShmEnt
13efe 65 72 4d 75 74 65 78 28 29 20 6d 75 74 65 78 20  erMutex() mutex 
13eff 61 6e 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20  and the pointer 
13f00 66 72 6f 6d 20 74 68 65 0d 0a 20 20 2a 2a 20 6e  from the..  ** n
13f01 65 77 20 28 73 74 72 75 63 74 20 77 69 6e 53 68  ew (struct winSh
13f02 6d 29 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65  m) object to the
13f03 20 70 53 68 6d 4e 6f 64 65 20 68 61 73 20 62 65   pShmNode has be
13f04 65 6e 20 73 65 74 2e 20 41 6c 6c 20 74 68 61 74  en set. All that
13f05 20 69 73 0d 0a 20 20 2a 2a 20 6c 65 66 74 20 74   is..  ** left t
13f06 6f 20 64 6f 20 69 73 20 74 6f 20 6c 69 6e 6b 20  o do is to link 
13f07 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 20 69  the new object i
13f08 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  nto the linked l
13f09 69 73 74 20 73 74 61 72 74 69 6e 67 0d 0a 20 20  ist starting..  
13f0a 2a 2a 20 61 74 20 70 53 68 6d 4e 6f 64 65 2d 3e  ** at pShmNode->
13f0b 70 46 69 72 73 74 2e 20 54 68 69 73 20 6d 75 73  pFirst. This mus
13f0c 74 20 62 65 20 64 6f 6e 65 20 77 68 69 6c 65 20  t be done while 
13f0d 68 6f 6c 64 69 6e 67 20 74 68 65 20 70 53 68 6d  holding the pShm
13f0e 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 0d 0a 20 20  Node->mutex ..  
13f0f 2a 2a 20 6d 75 74 65 78 2e 0d 0a 20 20 2a 2f 0d  ** mutex...  */.
13f10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
13f11 5f 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d  _enter(pShmNode-
13f12 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 70 2d 3e 70  >mutex);..  p->p
13f13 4e 65 78 74 20 3d 20 70 53 68 6d 4e 6f 64 65 2d  Next = pShmNode-
13f14 3e 70 46 69 72 73 74 3b 0d 0a 20 20 70 53 68 6d  >pFirst;..  pShm
13f15 4e 6f 64 65 2d 3e 70 46 69 72 73 74 20 3d 20 70  Node->pFirst = p
13f16 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
13f17 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64  ex_leave(pShmNod
13f18 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  e->mutex);..  re
13f19 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
13f1a 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  ...  /* Jump her
13f1b 65 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 20 2a  e on any error *
13f1c 2f 0d 0a 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3a  /..shm_open_err:
13f1d 0d 0a 20 20 77 69 6e 53 68 6d 53 79 73 74 65 6d  ..  winShmSystem
13f1e 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f  Lock(pShmNode, _
13f1f 53 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f 53  SHM_UNLCK, WIN_S
13f20 48 4d 5f 44 4d 53 2c 20 31 29 3b 0d 0a 20 20 77  HM_DMS, 1);..  w
13f21 69 6e 53 68 6d 50 75 72 67 65 28 70 44 62 46 64  inShmPurge(pDbFd
13f22 2d 3e 70 56 66 73 2c 20 30 29 3b 20 20 20 20 20  ->pVfs, 0);     
13f23 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 66 72   /* This call fr
13f24 65 65 73 20 70 53 68 6d 4e 6f 64 65 20 69 66 20  ees pShmNode if 
13f25 72 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 73  required */..  s
13f26 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d  qlite3_free(p);.
13f27 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
13f28 70 4e 65 77 29 3b 0d 0a 20 20 77 69 6e 53 68 6d  pNew);..  winShm
13f29 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 20  LeaveMutex();.. 
13f2a 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
13f2b 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20 61  ../*..** Close a
13f2c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 73   connection to s
13f2d 68 61 72 65 64 2d 6d 65 6d 6f 72 79 2e 20 20 44  hared-memory.  D
13f2e 65 6c 65 74 65 20 74 68 65 20 75 6e 64 65 72 6c  elete the underl
13f2f 79 69 6e 67 20 0d 0a 2a 2a 20 73 74 6f 72 61 67  ying ..** storag
13f30 65 20 69 66 20 64 65 6c 65 74 65 46 6c 61 67 20  e if deleteFlag 
13f31 69 73 20 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 73 74  is true...*/..st
13f32 61 74 69 63 20 69 6e 74 20 77 69 6e 53 68 6d 55  atic int winShmU
13f33 6e 6d 61 70 28 0d 0a 20 20 73 71 6c 69 74 65 33  nmap(..  sqlite3
13f34 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 20 20 20  _file *fd,      
13f35 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
13f36 68 6f 6c 64 69 6e 67 20 73 68 61 72 65 64 20 6d  holding shared m
13f37 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20  emory */..  int 
13f38 64 65 6c 65 74 65 46 6c 61 67 20 20 20 20 20 20  deleteFlag      
13f39 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65         /* Delete
13f3a 20 61 66 74 65 72 20 63 6c 6f 73 69 6e 67 20 69   after closing i
13f3b 66 20 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  f true */..){.. 
13f3c 20 77 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 3b   winFile *pDbFd;
13f3d 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
13f3e 73 65 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65  se holding share
13f3f 64 2d 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 77  d-memory */..  w
13f40 69 6e 53 68 6d 20 2a 70 3b 20 20 20 20 20 20 20  inShm *p;       
13f41 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e       /* The conn
13f42 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6c 6f  ection to be clo
13f43 73 65 64 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d  sed */..  winShm
13f44 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 20  Node *pShmNode; 
13f45 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  /* The underlyin
13f46 67 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  g shared-memory 
13f47 66 69 6c 65 20 2a 2f 0d 0a 20 20 77 69 6e 53 68  file */..  winSh
13f48 6d 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20  m **pp;         
13f49 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
13f4a 6f 76 65 72 20 73 69 62 6c 69 6e 67 20 63 6f 6e  over sibling con
13f4b 6e 65 63 74 69 6f 6e 73 20 2a 2f 0d 0a 0d 0a 20  nections */.... 
13f4c 20 70 44 62 46 64 20 3d 20 28 77 69 6e 46 69 6c   pDbFd = (winFil
13f4d 65 2a 29 66 64 3b 0d 0a 20 20 70 20 3d 20 70 44  e*)fd;..  p = pD
13f4e 62 46 64 2d 3e 70 53 68 6d 3b 0d 0a 20 20 69 66  bFd->pShm;..  if
13f4f 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
13f50 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 70 53  SQLITE_OK;..  pS
13f51 68 6d 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d  hmNode = p->pShm
13f52 4e 6f 64 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65  Node;....  /* Re
13f53 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  move connection 
13f54 70 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20 6f  p from the set o
13f55 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 73  f connections as
13f56 73 6f 63 69 61 74 65 64 0d 0a 20 20 2a 2a 20 77  sociated..  ** w
13f57 69 74 68 20 70 53 68 6d 4e 6f 64 65 20 2a 2f 0d  ith pShmNode */.
13f58 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
13f59 5f 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d  _enter(pShmNode-
13f5a 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 66 6f 72 28  >mutex);..  for(
13f5b 70 70 3d 26 70 53 68 6d 4e 6f 64 65 2d 3e 70 46  pp=&pShmNode->pF
13f5c 69 72 73 74 3b 20 28 2a 70 70 29 21 3d 70 3b 20  irst; (*pp)!=p; 
13f5d 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
13f5e 78 74 29 7b 7d 0d 0a 20 20 2a 70 70 20 3d 20 70  xt){}..  *pp = p
13f5f 2d 3e 70 4e 65 78 74 3b 0d 0a 0d 0a 20 20 2f 2a  ->pNext;....  /*
13f60 20 46 72 65 65 20 74 68 65 20 63 6f 6e 6e 65 63   Free the connec
13f61 74 69 6f 6e 20 70 20 2a 2f 0d 0a 20 20 73 71 6c  tion p */..  sql
13f62 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
13f63 20 70 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 30   pDbFd->pShm = 0
13f64 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
13f65 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64  ex_leave(pShmNod
13f66 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 0d 0a 20 20  e->mutex);....  
13f67 2f 2a 20 49 66 20 70 53 68 6d 4e 6f 64 65 2d 3e  /* If pShmNode->
13f68 6e 52 65 66 20 68 61 73 20 72 65 61 63 68 65 64  nRef has reached
13f69 20 30 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74   0, then close t
13f6a 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0d 0a 20  he underlying.. 
13f6b 20 2a 2a 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72   ** shared-memor
13f6c 79 20 66 69 6c 65 2c 20 74 6f 6f 20 2a 2f 0d 0a  y file, too */..
13f6d 20 20 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74    winShmEnterMut
13f6e 65 78 28 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ex();..  assert(
13f6f 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 3e   pShmNode->nRef>
13f70 30 20 29 3b 0d 0a 20 20 70 53 68 6d 4e 6f 64 65  0 );..  pShmNode
13f71 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a 20 20 69 66 28  ->nRef--;..  if(
13f72 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 3d   pShmNode->nRef=
13f73 3d 30 20 29 7b 0d 0a 20 20 20 20 77 69 6e 53 68  =0 ){..    winSh
13f74 6d 50 75 72 67 65 28 70 44 62 46 64 2d 3e 70 56  mPurge(pDbFd->pV
13f75 66 73 2c 20 64 65 6c 65 74 65 46 6c 61 67 29 3b  fs, deleteFlag);
13f76 0d 0a 20 20 7d 0d 0a 20 20 77 69 6e 53 68 6d 4c  ..  }..  winShmL
13f77 65 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 0d 0a  eaveMutex();....
13f78 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13f79 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
13f7a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 6f 63 6b   Change the lock
13f7b 20 73 74 61 74 65 20 66 6f 72 20 61 20 73 68 61   state for a sha
13f7c 72 65 64 2d 6d 65 6d 6f 72 79 20 73 65 67 6d 65  red-memory segme
13f7d 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  nt...*/..static 
13f7e 69 6e 74 20 77 69 6e 53 68 6d 4c 6f 63 6b 28 0d  int winShmLock(.
13f7f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
13f80 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *fd,          /*
13f81 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   Database file h
13f82 6f 6c 64 69 6e 67 20 74 68 65 20 73 68 61 72 65  olding the share
13f83 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 69  d memory */..  i
13f84 6e 74 20 6f 66 73 74 2c 20 20 20 20 20 20 20 20  nt ofst,        
13f85 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
13f86 73 74 20 6c 6f 63 6b 20 74 6f 20 61 63 71 75 69  st lock to acqui
13f87 72 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f  re or release */
13f88 0d 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20  ..  int n,      
13f89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13f8a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b  * Number of lock
13f8b 73 20 74 6f 20 61 63 71 75 69 72 65 20 6f 72 20  s to acquire or 
13f8c 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 20 20 69 6e  release */..  in
13f8d 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20  t flags         
13f8e 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74           /* What
13f8f 20 74 6f 20 64 6f 20 77 69 74 68 20 74 68 65 20   to do with the 
13f90 6c 6f 63 6b 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 77  lock */..){..  w
13f91 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 20 3d 20  inFile *pDbFd = 
13f92 28 77 69 6e 46 69 6c 65 2a 29 66 64 3b 20 20 20  (winFile*)fd;   
13f93 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
13f94 6f 6e 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65  on holding share
13f95 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20 20 77  d memory */..  w
13f96 69 6e 53 68 6d 20 2a 70 20 3d 20 70 44 62 46 64  inShm *p = pDbFd
13f97 2d 3e 70 53 68 6d 3b 20 20 20 20 20 20 20 20 20  ->pShm;         
13f98 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72       /* The shar
13f99 65 64 20 6d 65 6d 6f 72 79 20 62 65 69 6e 67 20  ed memory being 
13f9a 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 20 20 77 69 6e  locked */..  win
13f9b 53 68 6d 20 2a 70 58 3b 20 20 20 20 20 20 20 20  Shm *pX;        
13f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13f9d 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
13f9e 67 20 6f 76 65 72 20 61 6c 6c 20 73 69 62 6c 69  g over all sibli
13f9f 6e 67 73 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d  ngs */..  winShm
13fa0 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 20 3d  Node *pShmNode =
13fa1 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0d 0a 20   p->pShmNode;.. 
13fa2 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13fa3 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
13fa4 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
13fa5 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 75 31 36 20   code */..  u16 
13fa6 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
13fa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fa8 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63    /* Mask of loc
13fa9 6b 73 20 74 6f 20 74 61 6b 65 20 6f 72 20 72 65  ks to take or re
13faa 6c 65 61 73 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73  lease */....  as
13fab 73 65 72 74 28 20 6f 66 73 74 3e 3d 30 20 26 26  sert( ofst>=0 &&
13fac 20 6f 66 73 74 2b 6e 3c 3d 53 51 4c 49 54 45 5f   ofst+n<=SQLITE_
13fad 53 48 4d 5f 4e 4c 4f 43 4b 20 29 3b 0d 0a 20 20  SHM_NLOCK );..  
13fae 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0d  assert( n>=1 );.
13faf 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 73  .  assert( flags
13fb0 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f  ==(SQLITE_SHM_LO
13fb1 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f  CK | SQLITE_SHM_
13fb2 53 48 41 52 45 44 29 0d 0a 20 20 20 20 20 20 20  SHARED)..       
13fb3 7c 7c 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54  || flags==(SQLIT
13fb4 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c  E_SHM_LOCK | SQL
13fb5 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56  ITE_SHM_EXCLUSIV
13fb6 45 29 0d 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c  E)..       || fl
13fb7 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d  ags==(SQLITE_SHM
13fb8 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45  _UNLOCK | SQLITE
13fb9 5f 53 48 4d 5f 53 48 41 52 45 44 29 0d 0a 20 20  _SHM_SHARED)..  
13fba 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d 28       || flags==(
13fbb 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43  SQLITE_SHM_UNLOC
13fbc 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45  K | SQLITE_SHM_E
13fbd 58 43 4c 55 53 49 56 45 29 20 29 3b 0d 0a 20 20  XCLUSIVE) );..  
13fbe 61 73 73 65 72 74 28 20 6e 3d 3d 31 20 7c 7c 20  assert( n==1 || 
13fbf 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
13fc0 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 21 3d  SHM_EXCLUSIVE)!=
13fc1 30 20 29 3b 0d 0a 0d 0a 20 20 6d 61 73 6b 20 3d  0 );....  mask =
13fc2 20 28 75 31 36 29 28 28 31 55 3c 3c 28 6f 66 73   (u16)((1U<<(ofs
13fc3 74 2b 6e 29 29 20 2d 20 28 31 55 3c 3c 6f 66 73  t+n)) - (1U<<ofs
13fc4 74 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  t));..  assert( 
13fc5 6e 3e 31 20 7c 7c 20 6d 61 73 6b 3d 3d 28 31 3c  n>1 || mask==(1<
13fc6 3c 6f 66 73 74 29 20 29 3b 0d 0a 20 20 73 71 6c  <ofst) );..  sql
13fc7 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
13fc8 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78  (pShmNode->mutex
13fc9 29 3b 0d 0a 20 20 69 66 28 20 66 6c 61 67 73 20  );..  if( flags 
13fca 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c  & SQLITE_SHM_UNL
13fcb 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 75 31 36 20  OCK ){..    u16 
13fcc 61 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f 2a 20  allMask = 0; /* 
13fcd 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20 68 65  Mask of locks he
13fce 6c 64 20 62 79 20 73 69 62 6c 69 6e 67 73 20 2a  ld by siblings *
13fcf 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 65 65 20  /....    /* See 
13fd0 69 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 73 20  if any siblings 
13fd1 68 6f 6c 64 20 74 68 69 73 20 73 61 6d 65 20 6c  hold this same l
13fd2 6f 63 6b 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28  ock */..    for(
13fd3 70 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69  pX=pShmNode->pFi
13fd4 72 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e  rst; pX; pX=pX->
13fd5 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 69  pNext){..      i
13fd6 66 28 20 70 58 3d 3d 70 20 29 20 63 6f 6e 74 69  f( pX==p ) conti
13fd7 6e 75 65 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  nue;..      asse
13fd8 72 74 28 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73  rt( (pX->exclMas
13fd9 6b 20 26 20 28 70 2d 3e 65 78 63 6c 4d 61 73 6b  k & (p->exclMask
13fda 7c 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b 29 29  |p->sharedMask))
13fdb 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 6c  ==0 );..      al
13fdc 6c 4d 61 73 6b 20 7c 3d 20 70 58 2d 3e 73 68 61  lMask |= pX->sha
13fdd 72 65 64 4d 61 73 6b 3b 0d 0a 20 20 20 20 7d 0d  redMask;..    }.
13fde 0a 0d 0a 20 20 20 20 2f 2a 20 55 6e 6c 6f 63 6b  ...    /* Unlock
13fdf 20 74 68 65 20 73 79 73 74 65 6d 2d 6c 65 76 65   the system-leve
13fe0 6c 20 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 20 20  l locks */..    
13fe1 69 66 28 20 28 6d 61 73 6b 20 26 20 61 6c 6c 4d  if( (mask & allM
13fe2 61 73 6b 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ask)==0 ){..    
13fe3 20 20 72 63 20 3d 20 77 69 6e 53 68 6d 53 79 73    rc = winShmSys
13fe4 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65  temLock(pShmNode
13fe5 2c 20 5f 53 48 4d 5f 55 4e 4c 43 4b 2c 20 6f 66  , _SHM_UNLCK, of
13fe6 73 74 2b 57 49 4e 5f 53 48 4d 5f 42 41 53 45 2c  st+WIN_SHM_BASE,
13fe7 20 6e 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b   n);..    }else{
13fe8 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
13fe9 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a  ITE_OK;..    }..
13fea 0d 0a 20 20 20 20 2f 2a 20 55 6e 64 6f 20 74 68  ..    /* Undo th
13feb 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 2a 2f  e local locks */
13fec 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
13fed 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
13fee 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d    p->exclMask &=
13fef 20 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 70   ~mask;..      p
13ff0 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 3d 20  ->sharedMask &= 
13ff1 7e 6d 61 73 6b 3b 0d 0a 20 20 20 20 7d 20 0d 0a  ~mask;..    } ..
13ff2 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67    }else if( flag
13ff3 73 20 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53  s & SQLITE_SHM_S
13ff4 48 41 52 45 44 20 29 7b 0d 0a 20 20 20 20 75 31  HARED ){..    u1
13ff5 36 20 61 6c 6c 53 68 61 72 65 64 20 3d 20 30 3b  6 allShared = 0;
13ff6 20 20 2f 2a 20 55 6e 69 6f 6e 20 6f 66 20 6c 6f    /* Union of lo
13ff7 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e  cks held by conn
13ff8 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 20 74 68  ections other th
13ff9 61 6e 20 22 70 22 20 2a 2f 0d 0a 0d 0a 20 20 20  an "p" */....   
13ffa 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 69   /* Find out whi
13ffb 63 68 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20  ch shared locks 
13ffc 61 72 65 20 61 6c 72 65 61 64 79 20 68 65 6c 64  are already held
13ffd 20 62 79 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e   by sibling conn
13ffe 65 63 74 69 6f 6e 73 2e 0d 0a 20 20 20 20 2a 2a  ections...    **
13fff 20 49 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 20   If any sibling 
14000 61 6c 72 65 61 64 79 20 68 6f 6c 64 73 20 61 6e  already holds an
14001 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c   exclusive lock,
14002 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65   go ahead and re
14003 74 75 72 6e 0d 0a 20 20 20 20 2a 2a 20 53 51 4c  turn..    ** SQL
14004 49 54 45 5f 42 55 53 59 2e 0d 0a 20 20 20 20 2a  ITE_BUSY...    *
14005 2f 0d 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 53  /..    for(pX=pS
14006 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20  hmNode->pFirst; 
14007 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 74  pX; pX=pX->pNext
14008 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70  ){..      if( (p
14009 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d 61  X->exclMask & ma
1400a 73 6b 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  sk)!=0 ){..     
1400b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1400c 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  USY;..        br
1400d 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
1400e 20 20 20 20 20 61 6c 6c 53 68 61 72 65 64 20 7c       allShared |
1400f 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b  = pX->sharedMask
14010 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
14011 2f 2a 20 47 65 74 20 73 68 61 72 65 64 20 6c 6f  /* Get shared lo
14012 63 6b 73 20 61 74 20 74 68 65 20 73 79 73 74 65  cks at the syste
14013 6d 20 6c 65 76 65 6c 2c 20 69 66 20 6e 65 63 65  m level, if nece
14014 73 73 61 72 79 20 2a 2f 0d 0a 20 20 20 20 69 66  ssary */..    if
14015 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
14016 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 61  ){..      if( (a
14017 6c 6c 53 68 61 72 65 64 20 26 20 6d 61 73 6b 29  llShared & mask)
14018 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
14019 72 63 20 3d 20 77 69 6e 53 68 6d 53 79 73 74 65  rc = winShmSyste
1401a 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20  mLock(pShmNode, 
1401b 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 6f 66 73 74  _SHM_RDLCK, ofst
1401c 2b 57 49 4e 5f 53 48 4d 5f 42 41 53 45 2c 20 6e  +WIN_SHM_BASE, n
1401d 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  );..      }else{
1401e 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ..        rc = S
1401f 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
14020 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
14021 20 2f 2a 20 47 65 74 20 74 68 65 20 6c 6f 63 61   /* Get the loca
14022 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 2a  l shared locks *
14023 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  /..    if( rc==S
14024 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
14025 20 20 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b     p->sharedMask
14026 20 7c 3d 20 6d 61 73 6b 3b 0d 0a 20 20 20 20 7d   |= mask;..    }
14027 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
14028 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20  /* Make sure no 
14029 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69  sibling connecti
1402a 6f 6e 73 20 68 6f 6c 64 20 6c 6f 63 6b 73 20 74  ons hold locks t
1402b 68 61 74 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 74  hat will block t
1402c 68 69 73 0d 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b  his..    ** lock
1402d 2e 20 20 49 66 20 61 6e 79 20 64 6f 2c 20 72 65  .  If any do, re
1402e 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
1402f 20 72 69 67 68 74 20 61 77 61 79 2e 0d 0a 20 20   right away...  
14030 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 70 58    */..    for(pX
14031 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73  =pShmNode->pFirs
14032 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e  t; pX; pX=pX->pN
14033 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  ext){..      if(
14034 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26   (pX->exclMask &
14035 20 6d 61 73 6b 29 21 3d 30 20 7c 7c 20 28 70 58   mask)!=0 || (pX
14036 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 20 6d  ->sharedMask & m
14037 61 73 6b 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  ask)!=0 ){..    
14038 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
14039 42 55 53 59 3b 0d 0a 20 20 20 20 20 20 20 20 62  BUSY;..        b
1403a 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
1403b 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f      }..  ..    /
1403c 2a 20 47 65 74 20 74 68 65 20 65 78 63 6c 75 73  * Get the exclus
1403d 69 76 65 20 6c 6f 63 6b 73 20 61 74 20 74 68 65  ive locks at the
1403e 20 73 79 73 74 65 6d 20 6c 65 76 65 6c 2e 20 20   system level.  
1403f 54 68 65 6e 20 69 66 20 73 75 63 63 65 73 73 66  Then if successf
14040 75 6c 0d 0a 20 20 20 20 2a 2a 20 61 6c 73 6f 20  ul..    ** also 
14041 6d 61 72 6b 20 74 68 65 20 6c 6f 63 61 6c 20 63  mark the local c
14042 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 62 65 69  onnection as bei
14043 6e 67 20 6c 6f 63 6b 65 64 2e 0d 0a 20 20 20 20  ng locked...    
14044 2a 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  */..    if( rc==
14045 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
14046 20 20 20 20 72 63 20 3d 20 77 69 6e 53 68 6d 53      rc = winShmS
14047 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f  ystemLock(pShmNo
14048 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b 2c 20  de, _SHM_WRLCK, 
14049 6f 66 73 74 2b 57 49 4e 5f 53 48 4d 5f 42 41 53  ofst+WIN_SHM_BAS
1404a 45 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20 69 66  E, n);..      if
1404b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1404c 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
1404d 72 74 28 20 28 70 2d 3e 73 68 61 72 65 64 4d 61  rt( (p->sharedMa
1404e 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 3b  sk & mask)==0 );
1404f 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 78 63  ..        p->exc
14050 6c 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0d 0a  lMask |= mask;..
14051 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
14052 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d    }..  sqlite3_m
14053 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e  utex_leave(pShmN
14054 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  ode->mutex);..  
14055 4f 53 54 52 41 43 45 28 28 22 53 48 4d 2d 4c 4f  OSTRACE(("SHM-LO
14056 43 4b 20 73 68 6d 69 64 2d 25 64 2c 20 70 69 64  CK shmid-%d, pid
14057 2d 25 64 20 67 6f 74 20 25 30 33 78 2c 25 30 33  -%d got %03x,%03
14058 78 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  x %s\n",..      
14059 20 20 20 20 20 70 2d 3e 69 64 2c 20 28 69 6e 74       p->id, (int
1405a 29 6f 73 47 65 74 43 75 72 72 65 6e 74 50 72 6f  )osGetCurrentPro
1405b 63 65 73 73 49 64 28 29 2c 20 70 2d 3e 73 68 61  cessId(), p->sha
1405c 72 65 64 4d 61 73 6b 2c 20 70 2d 3e 65 78 63 6c  redMask, p->excl
1405d 4d 61 73 6b 2c 0d 0a 20 20 20 20 20 20 20 20 20  Mask,..         
1405e 20 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20    rc ? "failed" 
1405f 3a 20 22 6f 6b 22 29 29 3b 0d 0a 20 20 72 65 74  : "ok"));..  ret
14060 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
14061 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 61  ..** Implement a
14062 20 6d 65 6d 6f 72 79 20 62 61 72 72 69 65 72 20   memory barrier 
14063 6f 72 20 6d 65 6d 6f 72 79 20 66 65 6e 63 65 20  or memory fence 
14064 6f 6e 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  on shared memory
14065 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20  .  ..**..** All 
14066 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f 72 65 73  loads and stores
14067 20 62 65 67 75 6e 20 62 65 66 6f 72 65 20 74 68   begun before th
14068 65 20 62 61 72 72 69 65 72 20 6d 75 73 74 20 63  e barrier must c
14069 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 65 0d 0a  omplete before..
1406a 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72 20 73  ** any load or s
1406b 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74 65 72  tore begun after
1406c 20 74 68 65 20 62 61 72 72 69 65 72 2e 0d 0a 2a   the barrier...*
1406d 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  /..static void w
1406e 69 6e 53 68 6d 42 61 72 72 69 65 72 28 0d 0a 20  inShmBarrier(.. 
1406f 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
14070 64 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  d          /* Da
14071 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
14072 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
14073 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 55 4e 55 53 45   */..){..  UNUSE
14074 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b  D_PARAMETER(fd);
14075 0d 0a 20 20 2f 2a 20 4d 65 6d 6f 72 79 42 61 72  ..  /* MemoryBar
14076 72 69 65 72 28 29 3b 20 2f 2f 20 64 6f 65 73 20  rier(); // does 
14077 6e 6f 74 20 77 6f 72 6b 20 2d 2d 20 64 6f 20 6e  not work -- do n
14078 6f 74 20 6b 6e 6f 77 20 77 68 79 20 6e 6f 74 20  ot know why not 
14079 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 45 6e 74 65  */..  winShmEnte
1407a 72 4d 75 74 65 78 28 29 3b 0d 0a 20 20 77 69 6e  rMutex();..  win
1407b 53 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  ShmLeaveMutex();
1407c 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
1407d 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1407e 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20  alled to obtain 
1407f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 67  a pointer to reg
14080 69 6f 6e 20 69 52 65 67 69 6f 6e 20 6f 66 20 74  ion iRegion of t
14081 68 65 20 0d 0a 2a 2a 20 73 68 61 72 65 64 2d 6d  he ..** shared-m
14082 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
14083 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
14084 73 65 20 66 69 6c 65 20 66 64 2e 20 53 68 61 72  se file fd. Shar
14085 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e  ed-memory region
14086 73 20 0d 0a 2a 2a 20 61 72 65 20 6e 75 6d 62 65  s ..** are numbe
14087 72 65 64 20 73 74 61 72 74 69 6e 67 20 66 72 6f  red starting fro
14088 6d 20 7a 65 72 6f 2e 20 45 61 63 68 20 73 68 61  m zero. Each sha
14089 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f  red-memory regio
1408a 6e 20 69 73 20 73 7a 52 65 67 69 6f 6e 20 0d 0a  n is szRegion ..
1408b 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
1408c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20  ...**..** If an 
1408d 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
1408e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
1408f 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20  eturned and *pp 
14090 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0d  is set to NULL..
14091 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  .**..** Otherwis
14092 65 2c 20 69 66 20 74 68 65 20 69 73 57 72 69 74  e, if the isWrit
14093 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  e parameter is 0
14094 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
14095 65 64 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  ed shared-memory
14096 0d 0a 2a 2a 20 72 65 67 69 6f 6e 20 68 61 73 20  ..** region has 
14097 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  not been allocat
14098 65 64 20 28 62 79 20 61 6e 79 20 63 6c 69 65 6e  ed (by any clien
14099 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f 6e 65  t, including one
1409a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 0d 0a 2a   running in a..*
1409b 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65  * separate proce
1409c 73 73 29 2c 20 74 68 65 6e 20 2a 70 70 20 69 73  ss), then *pp is
1409d 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64   set to NULL and
1409e 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
1409f 6e 65 64 2e 20 49 66 20 0d 0a 2a 2a 20 69 73 57  ned. If ..** isW
140a0 72 69 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  rite is non-zero
140a1 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
140a2 65 64 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  ed shared-memory
140a3 20 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20   region has not 
140a4 79 65 74 20 0d 0a 2a 2a 20 62 65 65 6e 20 61 6c  yet ..** been al
140a5 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 61  located, it is a
140a6 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
140a7 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a   function...**..
140a8 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 64  ** If the shared
140a9 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 68  -memory region h
140aa 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
140ab 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 69 73 20  allocated or is 
140ac 61 6c 6c 6f 63 61 74 65 64 20 62 79 0d 0a 2a 2a  allocated by..**
140ad 20 74 68 69 73 20 63 61 6c 6c 20 61 73 20 64 65   this call as de
140ae 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 74  scribed above, t
140af 68 65 6e 20 69 74 20 69 73 20 6d 61 70 70 65 64  hen it is mapped
140b0 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f 63 65   into this proce
140b1 73 73 65 73 20 0d 0a 2a 2a 20 61 64 64 72 65 73  sses ..** addres
140b2 73 20 73 70 61 63 65 20 28 69 66 20 69 74 20 69  s space (if it i
140b3 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 29 2c 20  s not already), 
140b4 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 70 6f  *pp is set to po
140b5 69 6e 74 20 74 6f 20 74 68 65 20 6d 61 70 70 65  int to the mappe
140b6 64 20 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e  d ..** memory an
140b7 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
140b8 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rned...*/..stati
140b9 63 20 69 6e 74 20 77 69 6e 53 68 6d 4d 61 70 28  c int winShmMap(
140ba 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ..  sqlite3_file
140bb 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20 20   *fd,           
140bc 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f 70      /* Handle op
140bd 65 6e 20 6f 6e 20 64 61 74 61 62 61 73 65 20 66  en on database f
140be 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 52  ile */..  int iR
140bf 65 67 69 6f 6e 2c 20 20 20 20 20 20 20 20 20 20  egion,          
140c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
140c1 69 6f 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20  ion to retrieve 
140c2 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 52 65 67 69  */..  int szRegi
140c3 6f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  on,             
140c4 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
140c5 20 72 65 67 69 6f 6e 73 20 2a 2f 0d 0a 20 20 69   regions */..  i
140c6 6e 74 20 69 73 57 72 69 74 65 2c 20 20 20 20 20  nt isWrite,     
140c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
140c8 2a 20 54 72 75 65 20 74 6f 20 65 78 74 65 6e 64  * True to extend
140c9 20 66 69 6c 65 20 69 66 20 6e 65 63 65 73 73 61   file if necessa
140ca 72 79 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 76 6f  ry */..  void vo
140cb 6c 61 74 69 6c 65 20 2a 2a 70 70 20 20 20 20 20  latile **pp     
140cc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
140cd 20 4d 61 70 70 65 64 20 6d 65 6d 6f 72 79 20 2a   Mapped memory *
140ce 2f 0d 0a 29 7b 0d 0a 20 20 77 69 6e 46 69 6c 65  /..){..  winFile
140cf 20 2a 70 44 62 46 64 20 3d 20 28 77 69 6e 46 69   *pDbFd = (winFi
140d0 6c 65 2a 29 66 64 3b 0d 0a 20 20 77 69 6e 53 68  le*)fd;..  winSh
140d1 6d 20 2a 70 20 3d 20 70 44 62 46 64 2d 3e 70 53  m *p = pDbFd->pS
140d2 68 6d 3b 0d 0a 20 20 77 69 6e 53 68 6d 4e 6f 64  hm;..  winShmNod
140d3 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 0d 0a 20 20  e *pShmNode;..  
140d4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
140d5 4f 4b 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 70 20  OK;....  if( !p 
140d6 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 77 69 6e  ){..    rc = win
140d7 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79  OpenSharedMemory
140d8 28 70 44 62 46 64 29 3b 0d 0a 20 20 20 20 69 66  (pDbFd);..    if
140d9 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
140da 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20  ) return rc;..  
140db 20 20 70 20 3d 20 70 44 62 46 64 2d 3e 70 53 68    p = pDbFd->pSh
140dc 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 53 68 6d 4e  m;..  }..  pShmN
140dd 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64  ode = p->pShmNod
140de 65 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f  e;....  sqlite3_
140df 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d  mutex_enter(pShm
140e0 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  Node->mutex);.. 
140e1 20 61 73 73 65 72 74 28 20 73 7a 52 65 67 69 6f   assert( szRegio
140e2 6e 3d 3d 70 53 68 6d 4e 6f 64 65 2d 3e 73 7a 52  n==pShmNode->szR
140e3 65 67 69 6f 6e 20 7c 7c 20 70 53 68 6d 4e 6f 64  egion || pShmNod
140e4 65 2d 3e 6e 52 65 67 69 6f 6e 3d 3d 30 20 29 3b  e->nRegion==0 );
140e5 0d 0a 0d 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f  ....  if( pShmNo
140e6 64 65 2d 3e 6e 52 65 67 69 6f 6e 3c 3d 69 52 65  de->nRegion<=iRe
140e7 67 69 6f 6e 20 29 7b 0d 0a 20 20 20 20 73 74 72  gion ){..    str
140e8 75 63 74 20 53 68 6d 52 65 67 69 6f 6e 20 2a 61  uct ShmRegion *a
140e9 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
140ea 2f 2a 20 4e 65 77 20 61 52 65 67 69 6f 6e 5b 5d  /* New aRegion[]
140eb 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 20 20 69   array */..    i
140ec 6e 74 20 6e 42 79 74 65 20 3d 20 28 69 52 65 67  nt nByte = (iReg
140ed 69 6f 6e 2b 31 29 2a 73 7a 52 65 67 69 6f 6e 3b  ion+1)*szRegion;
140ee 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 72 65 71    /* Minimum req
140ef 75 69 72 65 64 20 66 69 6c 65 20 73 69 7a 65 20  uired file size 
140f0 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  */..    sqlite3_
140f1 69 6e 74 36 34 20 73 7a 3b 20 20 20 20 20 20 20  int64 sz;       
140f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
140f3 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 77 61  rrent size of wa
140f4 6c 2d 69 6e 64 65 78 20 66 69 6c 65 20 2a 2f 0d  l-index file */.
140f5 0a 0d 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d  ...    pShmNode-
140f6 3e 73 7a 52 65 67 69 6f 6e 20 3d 20 73 7a 52 65  >szRegion = szRe
140f7 67 69 6f 6e 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  gion;....    /* 
140f8 54 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65  The requested re
140f9 67 69 6f 6e 20 69 73 20 6e 6f 74 20 6d 61 70 70  gion is not mapp
140fa 65 64 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f  ed into this pro
140fb 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73  cesses address s
140fc 70 61 63 65 2e 0d 0a 20 20 20 20 2a 2a 20 43 68  pace...    ** Ch
140fd 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74  eck to see if it
140fe 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61   has been alloca
140ff 74 65 64 20 28 69 2e 65 2e 20 69 66 20 74 68 65  ted (i.e. if the
14100 20 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 20   wal-index file 
14101 69 73 0d 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65  is..    ** large
14102 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61   enough to conta
14103 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64  in the requested
14104 20 72 65 67 69 6f 6e 29 2e 0d 0a 20 20 20 20 2a   region)...    *
14105 2f 0d 0a 20 20 20 20 72 63 20 3d 20 77 69 6e 46  /..    rc = winF
14106 69 6c 65 53 69 7a 65 28 28 73 71 6c 69 74 65 33  ileSize((sqlite3
14107 5f 66 69 6c 65 20 2a 29 26 70 53 68 6d 4e 6f 64  _file *)&pShmNod
14108 65 2d 3e 68 46 69 6c 65 2c 20 26 73 7a 29 3b 0d  e->hFile, &sz);.
14109 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1410a 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1410b 20 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f   rc = winLogErro
1410c 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  r(SQLITE_IOERR_S
1410d 48 4d 53 49 5a 45 2c 20 6f 73 47 65 74 4c 61 73  HMSIZE, osGetLas
1410e 74 45 72 72 6f 72 28 29 2c 0d 0a 20 20 20 20 20  tError(),..     
1410f 20 20 20 20 20 20 20 20 20 20 22 77 69 6e 53 68            "winSh
14110 6d 4d 61 70 31 22 2c 20 70 44 62 46 64 2d 3e 7a  mMap1", pDbFd->z
14111 50 61 74 68 29 3b 0d 0a 20 20 20 20 20 20 67 6f  Path);..      go
14112 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0d  to shmpage_out;.
14113 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66  .    }....    if
14114 28 20 73 7a 3c 6e 42 79 74 65 20 29 7b 0d 0a 20  ( sz<nByte ){.. 
14115 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75       /* The requ
14116 65 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67  ested memory reg
14117 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ion does not exi
14118 73 74 2e 20 49 66 20 69 73 57 72 69 74 65 20 69  st. If isWrite i
14119 73 20 73 65 74 20 74 6f 0d 0a 20 20 20 20 20 20  s set to..      
1411a 2a 2a 20 7a 65 72 6f 2c 20 65 78 69 74 20 65 61  ** zero, exit ea
1411b 72 6c 79 2e 20 2a 70 70 20 77 69 6c 6c 20 62 65  rly. *pp will be
1411c 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64   set to NULL and
1411d 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
1411e 6e 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a  ned...      **..
1411f 20 20 20 20 20 20 2a 2a 20 41 6c 74 65 72 6e 61        ** Alterna
14120 74 69 76 65 6c 79 2c 20 69 66 20 69 73 57 72 69  tively, if isWri
14121 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  te is non-zero, 
14122 75 73 65 20 66 74 72 75 6e 63 61 74 65 28 29 20  use ftruncate() 
14123 74 6f 20 61 6c 6c 6f 63 61 74 65 0d 0a 20 20 20  to allocate..   
14124 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73     ** the reques
14125 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 6f  ted memory regio
14126 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  n...      */..  
14127 20 20 20 20 69 66 28 20 21 69 73 57 72 69 74 65      if( !isWrite
14128 20 29 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f   ) goto shmpage_
14129 6f 75 74 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  out;..      rc =
1412a 20 77 69 6e 54 72 75 6e 63 61 74 65 28 28 73 71   winTruncate((sq
1412b 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 53  lite3_file *)&pS
1412c 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 2c 20 6e  hmNode->hFile, n
1412d 42 79 74 65 29 3b 0d 0a 20 20 20 20 20 20 69 66  Byte);..      if
1412e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1412f 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
14130 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
14131 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a  ITE_IOERR_SHMSIZ
14132 45 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f  E, osGetLastErro
14133 72 28 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  r(),..          
14134 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d 61         "winShmMa
14135 70 32 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 74  p2", pDbFd->zPat
14136 68 29 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f 74  h);..        got
14137 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0d 0a  o shmpage_out;..
14138 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
14139 0d 0a 20 20 20 20 2f 2a 20 4d 61 70 20 74 68 65  ..    /* Map the
1413a 20 72 65 71 75 65 73 74 65 64 20 6d 65 6d 6f 72   requested memor
1413b 79 20 72 65 67 69 6f 6e 20 69 6e 74 6f 20 74 68  y region into th
1413c 69 73 20 70 72 6f 63 65 73 73 65 73 20 61 64 64  is processes add
1413d 72 65 73 73 20 73 70 61 63 65 2e 20 2a 2f 0d 0a  ress space. */..
1413e 20 20 20 20 61 70 4e 65 77 20 3d 20 28 73 74 72      apNew = (str
1413f 75 63 74 20 53 68 6d 52 65 67 69 6f 6e 20 2a 29  uct ShmRegion *)
14140 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
14141 0d 0a 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f  ..        pShmNo
14142 64 65 2d 3e 61 52 65 67 69 6f 6e 2c 20 28 69 52  de->aRegion, (iR
14143 65 67 69 6f 6e 2b 31 29 2a 73 69 7a 65 6f 66 28  egion+1)*sizeof(
14144 61 70 4e 65 77 5b 30 5d 29 0d 0a 20 20 20 20 29  apNew[0])..    )
14145 3b 0d 0a 20 20 20 20 69 66 28 20 21 61 70 4e 65  ;..    if( !apNe
14146 77 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  w ){..      rc =
14147 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
14148 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  MEM;..      goto
14149 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0d 0a 20   shmpage_out;.. 
1414a 20 20 20 7d 0d 0a 20 20 20 20 70 53 68 6d 4e 6f     }..    pShmNo
1414b 64 65 2d 3e 61 52 65 67 69 6f 6e 20 3d 20 61 70  de->aRegion = ap
1414c 4e 65 77 3b 0d 0a 0d 0a 20 20 20 20 77 68 69 6c  New;....    whil
1414d 65 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65  e( pShmNode->nRe
1414e 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b  gion<=iRegion ){
1414f 0d 0a 20 20 20 20 20 20 48 41 4e 44 4c 45 20 68  ..      HANDLE h
14150 4d 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  Map;            
14151 20 20 20 20 2f 2a 20 66 69 6c 65 2d 6d 61 70 70      /* file-mapp
14152 69 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ing handle */.. 
14153 20 20 20 20 20 76 6f 69 64 20 2a 70 4d 61 70 20       void *pMap 
14154 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
14155 20 2f 2a 20 4d 61 70 70 65 64 20 6d 65 6d 6f 72   /* Mapped memor
14156 79 20 72 65 67 69 6f 6e 20 2a 2f 0d 0a 20 20 20  y region */..   
14157 20 20 0d 0a 20 20 20 20 20 20 68 4d 61 70 20 3d    ..      hMap =
14158 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61 70   osCreateFileMap
14159 70 69 6e 67 28 70 53 68 6d 4e 6f 64 65 2d 3e 68  ping(pShmNode->h
1415a 46 69 6c 65 2e 68 2c 20 0d 0a 20 20 20 20 20 20  File.h, ..      
1415b 20 20 20 20 4e 55 4c 4c 2c 20 50 41 47 45 5f 52      NULL, PAGE_R
1415c 45 41 44 57 52 49 54 45 2c 20 30 2c 20 6e 42 79  EADWRITE, 0, nBy
1415d 74 65 2c 20 4e 55 4c 4c 0d 0a 20 20 20 20 20 20  te, NULL..      
1415e 29 3b 0d 0a 20 20 20 20 20 20 4f 53 54 52 41 43  );..      OSTRAC
1415f 45 28 28 22 53 48 4d 2d 4d 41 50 20 70 69 64 2d  E(("SHM-MAP pid-
14160 25 64 20 63 72 65 61 74 65 20 72 65 67 69 6f 6e  %d create region
14161 3d 25 64 20 6e 62 79 74 65 3d 25 64 20 25 73 5c  =%d nbyte=%d %s\
14162 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  n",..           
14163 20 20 20 20 28 69 6e 74 29 6f 73 47 65 74 43 75      (int)osGetCu
14164 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29  rrentProcessId()
14165 2c 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67  , pShmNode->nReg
14166 69 6f 6e 2c 20 6e 42 79 74 65 2c 0d 0a 20 20 20  ion, nByte,..   
14167 20 20 20 20 20 20 20 20 20 20 20 20 68 4d 61 70              hMap
14168 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65   ? "ok" : "faile
14169 64 22 29 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  d"));..      if(
1416a 20 68 4d 61 70 20 29 7b 0d 0a 20 20 20 20 20 20   hMap ){..      
1416b 20 20 69 6e 74 20 69 4f 66 66 73 65 74 20 3d 20    int iOffset = 
1416c 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f  pShmNode->nRegio
1416d 6e 2a 73 7a 52 65 67 69 6f 6e 3b 0d 0a 20 20 20  n*szRegion;..   
1416e 20 20 20 20 20 69 6e 74 20 69 4f 66 66 73 65 74       int iOffset
1416f 53 68 69 66 74 20 3d 20 69 4f 66 66 73 65 74 20  Shift = iOffset 
14170 25 20 77 69 6e 53 79 73 49 6e 66 6f 2e 64 77 41  % winSysInfo.dwA
14171 6c 6c 6f 63 61 74 69 6f 6e 47 72 61 6e 75 6c 61  llocationGranula
14172 72 69 74 79 3b 0d 0a 20 20 20 20 20 20 20 20 70  rity;..        p
14173 4d 61 70 20 3d 20 6f 73 4d 61 70 56 69 65 77 4f  Map = osMapViewO
14174 66 46 69 6c 65 28 68 4d 61 70 2c 20 46 49 4c 45  fFile(hMap, FILE
14175 5f 4d 41 50 5f 57 52 49 54 45 20 7c 20 46 49 4c  _MAP_WRITE | FIL
14176 45 5f 4d 41 50 5f 52 45 41 44 2c 0d 0a 20 20 20  E_MAP_READ,..   
14177 20 20 20 20 20 20 20 20 20 30 2c 20 69 4f 66 66           0, iOff
14178 73 65 74 20 2d 20 69 4f 66 66 73 65 74 53 68 69  set - iOffsetShi
14179 66 74 2c 20 73 7a 52 65 67 69 6f 6e 20 2b 20 69  ft, szRegion + i
1417a 4f 66 66 73 65 74 53 68 69 66 74 0d 0a 20 20 20  OffsetShift..   
1417b 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 20       );..       
1417c 20 4f 53 54 52 41 43 45 28 28 22 53 48 4d 2d 4d   OSTRACE(("SHM-M
1417d 41 50 20 70 69 64 2d 25 64 20 6d 61 70 20 72 65  AP pid-%d map re
1417e 67 69 6f 6e 3d 25 64 20 6f 66 66 73 65 74 3d 25  gion=%d offset=%
1417f 64 20 73 69 7a 65 3d 25 64 20 25 73 5c 6e 22 2c  d size=%d %s\n",
14180 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
14181 20 20 20 28 69 6e 74 29 6f 73 47 65 74 43 75 72     (int)osGetCur
14182 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 2c  rentProcessId(),
14183 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69   pShmNode->nRegi
14184 6f 6e 2c 20 69 4f 66 66 73 65 74 2c 0d 0a 20 20  on, iOffset,..  
14185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
14186 7a 52 65 67 69 6f 6e 2c 20 70 4d 61 70 20 3f 20  zRegion, pMap ? 
14187 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
14188 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
14189 20 20 20 69 66 28 20 21 70 4d 61 70 20 29 7b 0d     if( !pMap ){.
1418a 0a 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64  .        pShmNod
1418b 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f  e->lastErrno = o
1418c 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b  sGetLastError();
1418d 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77  ..        rc = w
1418e 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54  inLogError(SQLIT
1418f 45 5f 49 4f 45 52 52 5f 53 48 4d 4d 41 50 2c 20  E_IOERR_SHMMAP, 
14190 70 53 68 6d 4e 6f 64 65 2d 3e 6c 61 73 74 45 72  pShmNode->lastEr
14191 72 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  rno,..          
14192 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d 61         "winShmMa
14193 70 33 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 74  p3", pDbFd->zPat
14194 68 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  h);..        if(
14195 20 68 4d 61 70 20 29 20 6f 73 43 6c 6f 73 65 48   hMap ) osCloseH
14196 61 6e 64 6c 65 28 68 4d 61 70 29 3b 0d 0a 20 20  andle(hMap);..  
14197 20 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61        goto shmpa
14198 67 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20 7d  ge_out;..      }
14199 0d 0a 0d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f  ....      pShmNo
1419a 64 65 2d 3e 61 52 65 67 69 6f 6e 5b 70 53 68 6d  de->aRegion[pShm
1419b 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 5d 2e 70  Node->nRegion].p
1419c 4d 61 70 20 3d 20 70 4d 61 70 3b 0d 0a 20 20 20  Map = pMap;..   
1419d 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 61 52 65     pShmNode->aRe
1419e 67 69 6f 6e 5b 70 53 68 6d 4e 6f 64 65 2d 3e 6e  gion[pShmNode->n
1419f 52 65 67 69 6f 6e 5d 2e 68 4d 61 70 20 3d 20 68  Region].hMap = h
141a0 4d 61 70 3b 0d 0a 20 20 20 20 20 20 70 53 68 6d  Map;..      pShm
141a1 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 2b 2b 3b  Node->nRegion++;
141a2 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
141a3 73 68 6d 70 61 67 65 5f 6f 75 74 3a 0d 0a 20 20  shmpage_out:..  
141a4 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52  if( pShmNode->nR
141a5 65 67 69 6f 6e 3e 69 52 65 67 69 6f 6e 20 29 7b  egion>iRegion ){
141a6 0d 0a 20 20 20 20 69 6e 74 20 69 4f 66 66 73 65  ..    int iOffse
141a7 74 20 3d 20 69 52 65 67 69 6f 6e 2a 73 7a 52 65  t = iRegion*szRe
141a8 67 69 6f 6e 3b 0d 0a 20 20 20 20 69 6e 74 20 69  gion;..    int i
141a9 4f 66 66 73 65 74 53 68 69 66 74 20 3d 20 69 4f  OffsetShift = iO
141aa 66 66 73 65 74 20 25 20 77 69 6e 53 79 73 49 6e  ffset % winSysIn
141ab 66 6f 2e 64 77 41 6c 6c 6f 63 61 74 69 6f 6e 47  fo.dwAllocationG
141ac 72 61 6e 75 6c 61 72 69 74 79 3b 0d 0a 20 20 20  ranularity;..   
141ad 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72   char *p = (char
141ae 20 2a 29 70 53 68 6d 4e 6f 64 65 2d 3e 61 52 65   *)pShmNode->aRe
141af 67 69 6f 6e 5b 69 52 65 67 69 6f 6e 5d 2e 70 4d  gion[iRegion].pM
141b0 61 70 3b 0d 0a 20 20 20 20 2a 70 70 20 3d 20 28  ap;..    *pp = (
141b1 76 6f 69 64 20 2a 29 26 70 5b 69 4f 66 66 73 65  void *)&p[iOffse
141b2 74 53 68 69 66 74 5d 3b 0d 0a 20 20 7d 65 6c 73  tShift];..  }els
141b3 65 7b 0d 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b  e{..    *pp = 0;
141b4 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
141b5 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68  _mutex_leave(pSh
141b6 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0d 0a  mNode->mutex);..
141b7 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
141b8 0a 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  ...#else..# defi
141b9 6e 65 20 77 69 6e 53 68 6d 4d 61 70 20 20 20 20  ne winShmMap    
141ba 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 77 69 6e   0..# define win
141bb 53 68 6d 4c 6f 63 6b 20 20 20 20 30 0d 0a 23 20  ShmLock    0..# 
141bc 64 65 66 69 6e 65 20 77 69 6e 53 68 6d 42 61 72  define winShmBar
141bd 72 69 65 72 20 30 0d 0a 23 20 64 65 66 69 6e 65  rier 0..# define
141be 20 77 69 6e 53 68 6d 55 6e 6d 61 70 20 20 20 30   winShmUnmap   0
141bf 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e  ..#endif /* #ifn
141c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
141c1 57 41 4c 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  WAL */..../*..**
141c2 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 69   Here ends the i
141c3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
141c4 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c   all sqlite3_fil
141c5 65 20 6d 65 74 68 6f 64 73 2e 0d 0a 2a 2a 0d 0a  e methods...**..
141c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141c7 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74  ****** End sqlit
141c8 65 33 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20  e3_file Methods 
141c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
141cb 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
141cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
141d0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
141d1 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61  vector defines a
141d2 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74  ll the methods t
141d3 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20  hat can operate 
141d4 6f 6e 20 61 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65  on an..** sqlite
141d5 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32  3_file for win32
141d6 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f  ...*/..static co
141d7 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  nst sqlite3_io_m
141d8 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68  ethods winIoMeth
141d9 6f 64 20 3d 20 7b 0d 0a 20 20 32 2c 20 20 20 20  od = {..  2,    
141da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
141dc 72 73 69 6f 6e 20 2a 2f 0d 0a 20 20 77 69 6e 43  rsion */..  winC
141dd 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
141de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
141df 43 6c 6f 73 65 20 2a 2f 0d 0a 20 20 77 69 6e 52  Close */..  winR
141e0 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  ead,            
141e1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
141e2 52 65 61 64 20 2a 2f 0d 0a 20 20 77 69 6e 57 72  Read */..  winWr
141e3 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ite,            
141e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57             /* xW
141e5 72 69 74 65 20 2a 2f 0d 0a 20 20 77 69 6e 54 72  rite */..  winTr
141e6 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20  uncate,         
141e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54             /* xT
141e8 72 75 6e 63 61 74 65 20 2a 2f 0d 0a 20 20 77 69  runcate */..  wi
141e9 6e 53 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20  nSync,          
141ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
141eb 20 78 53 79 6e 63 20 2a 2f 0d 0a 20 20 77 69 6e   xSync */..  win
141ec 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 20 20  FileSize,       
141ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
141ee 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0d 0a 20 20  xFileSize */..  
141ef 77 69 6e 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  winLock,        
141f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141f1 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0d 0a 20 20 77  /* xLock */..  w
141f2 69 6e 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20  inUnlock,       
141f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
141f4 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0d 0a 20 20  * xUnlock */..  
141f5 77 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64  winCheckReserved
141f6 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
141f7 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
141f8 64 4c 6f 63 6b 20 2a 2f 0d 0a 20 20 77 69 6e 46  dLock */..  winF
141f9 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20  ileControl,     
141fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
141fb 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0d 0a  FileControl */..
141fc 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c    winSectorSize,
141fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141fe 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65    /* xSectorSize
141ff 20 2a 2f 0d 0a 20 20 77 69 6e 44 65 76 69 63 65   */..  winDevice
14200 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2c  Characteristics,
14201 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63         /* xDevic
14202 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
14203 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 4d 61 70   */..  winShmMap
14204 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
14205 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61         /* xShmMa
14206 70 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d 4c 6f  p */..  winShmLo
14207 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
14208 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 4c          /* xShmL
14209 6f 63 6b 20 2a 2f 0d 0a 20 20 77 69 6e 53 68 6d  ock */..  winShm
1420a 42 61 72 72 69 65 72 2c 20 20 20 20 20 20 20 20  Barrier,        
1420b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68            /* xSh
1420c 6d 42 61 72 72 69 65 72 20 2a 2f 0d 0a 20 20 77  mBarrier */..  w
1420d 69 6e 53 68 6d 55 6e 6d 61 70 20 20 20 20 20 20  inShmUnmap      
1420e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1420f 2a 20 78 53 68 6d 55 6e 6d 61 70 20 2a 2f 0d 0a  * xShmUnmap */..
14210 7d 3b 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  };..../*********
14211 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14214 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14215 2a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***..***********
14216 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14217 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65  * sqlite3_vfs me
14218 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  thods **********
14219 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1421a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  **..**..** This 
1421b 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e  division contain
1421c 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
1421d 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73 20  tion of methods 
1421e 6f 6e 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74  on the..** sqlit
1421f 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0d 0a  e3_vfs object...
14220 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e  */..../*..** Con
14221 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c  vert a UTF-8 fil
14222 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65  ename into whate
14223 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64  ver form the und
14224 65 72 6c 79 69 6e 67 0d 0a 2a 2a 20 6f 70 65 72  erlying..** oper
14225 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e  ating system wan
14226 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e  ts filenames in.
14227 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
14228 74 68 65 20 72 65 73 75 6c 74 0d 0a 2a 2a 20 69  the result..** i
14229 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
1422a 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20  malloc and must 
1422b 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20  be freed by the 
1422c 63 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 66 75 6e 63  calling..** func
1422d 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  tion...*/..stati
1422e 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55  c void *convertU
1422f 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73  tf8Filename(cons
14230 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
14231 65 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  e){..  void *zCo
14232 6e 76 65 72 74 65 64 20 3d 20 30 3b 0d 0a 20 20  nverted = 0;..  
14233 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0d 0a 20  if( isNT() ){.. 
14234 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20     zConverted = 
14235 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46  utf8ToUnicode(zF
14236 69 6c 65 6e 61 6d 65 29 3b 0d 0a 2f 2a 20 69 73  ilename);../* is
14237 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
14238 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
14239 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
1423a 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
1423b 20 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54   ..*/..#if SQLIT
1423c 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20  E_OS_WINCE==0.. 
1423d 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 43 6f   }else{..    zCo
1423e 6e 76 65 72 74 65 64 20 3d 20 73 71 6c 69 74 65  nverted = sqlite
1423f 33 5f 77 69 6e 33 32 5f 75 74 66 38 5f 74 6f 5f  3_win32_utf8_to_
14240 6d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  mbcs(zFilename);
14241 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 20  ..#endif..  }.. 
14242 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20   /* caller will 
14243 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65  handle out of me
14244 6d 6f 72 79 20 2a 2f 0d 0a 20 20 72 65 74 75 72  mory */..  retur
14245 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0d 0a 7d  n zConverted;..}
14246 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74  ..../*..** Creat
14247 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
14248 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
14249 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
1424a 69 67 20 65 6e 6f 75 67 68 20 74 6f 0d 0a 2a 2a  ig enough to..**
1424b 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d   hold at pVfs->m
1424c 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63  xPathname charac
1424d 74 65 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ters...*/..stati
1424e 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d  c int getTempnam
1424f 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  e(int nBuf, char
14250 20 2a 7a 42 75 66 29 7b 0d 0a 20 20 73 74 61 74   *zBuf){..  stat
14251 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ic char zChars[]
14252 20 3d 0d 0a 20 20 20 20 22 61 62 63 64 65 66 67   =..    "abcdefg
14253 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77  hijklmnopqrstuvw
14254 78 79 7a 22 0d 0a 20 20 20 20 22 41 42 43 44 45  xyz"..    "ABCDE
14255 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55  FGHIJKLMNOPQRSTU
14256 56 57 58 59 5a 22 0d 0a 20 20 20 20 22 30 31 32  VWXYZ"..    "012
14257 33 34 35 36 37 38 39 22 3b 0d 0a 20 20 73 69 7a  3456789";..  siz
14258 65 5f 74 20 69 2c 20 6a 3b 0d 0a 20 20 63 68 61  e_t i, j;..  cha
14259 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f  r zTempPath[MAX_
1425a 50 41 54 48 2b 32 5d 3b 0d 0a 0d 0a 20 20 2f 2a  PATH+2];....  /*
1425b 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d   It's odd to sim
1425c 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f  ulate an io-erro
1425d 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c  r here, but real
1425e 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0d  ly this is just.
1425f 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
14260 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
14261 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
14262 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
14263 6c 65 73 20 74 68 69 73 0d 0a 20 20 2a 2a 20 66  les this..  ** f
14264 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e  unction failing.
14265 20 0d 0a 20 20 2a 2f 0d 0a 20 20 53 69 6d 75 6c   ..  */..  Simul
14266 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
14267 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  rn SQLITE_IOERR 
14268 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 73 71 6c 69  );....  if( sqli
14269 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
1426a 72 79 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ry ){..    sqlit
1426b 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f  e3_snprintf(MAX_
1426c 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61  PATH-30, zTempPa
1426d 74 68 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  th, "%s", sqlite
1426e 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
1426f 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  );..  }else if( 
14270 69 73 4e 54 28 29 20 29 7b 0d 0a 20 20 20 20 63  isNT() ){..    c
14271 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0d 0a 20 20  har *zMulti;..  
14272 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 74    WCHAR zWidePat
14273 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0d 0a 20 20  h[MAX_PATH];..  
14274 20 20 6f 73 47 65 74 54 65 6d 70 50 61 74 68 57    osGetTempPathW
14275 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57  (MAX_PATH-30, zW
14276 69 64 65 50 61 74 68 29 3b 0d 0a 20 20 20 20 7a  idePath);..    z
14277 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54  Multi = unicodeT
14278 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29  oUtf8(zWidePath)
14279 3b 0d 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74  ;..    if( zMult
1427a 69 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  i ){..      sqli
1427b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58  te3_snprintf(MAX
1427c 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50  _PATH-30, zTempP
1427d 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74  ath, "%s", zMult
1427e 69 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  i);..      sqlit
1427f 65 33 5f 66 72 65 65 28 7a 4d 75 6c 74 69 29 3b  e3_free(zMulti);
14280 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
14281 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14282 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0d 0a  E_IOERR_NOMEM;..
14283 20 20 20 20 7d 0d 0a 2f 2a 20 69 73 4e 54 28 29      }../* isNT()
14284 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
14285 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
14286 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
14287 65 72 20 65 78 65 63 75 74 65 64 2e 20 0d 0a 2a  er executed. ..*
14288 2a 20 53 69 6e 63 65 20 74 68 65 20 41 4e 53 49  * Since the ANSI
14289 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
1428a 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
1428b 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
1428c 49 4e 43 45 2c 0d 0a 2a 2a 20 69 74 27 73 20 69  INCE,..** it's i
1428d 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
1428e 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
1428f 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
14290 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45  ..*/..#if SQLITE
14291 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20 20  _OS_WINCE==0..  
14292 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 63 68 61 72  }else{..    char
14293 20 2a 7a 55 74 66 38 3b 0d 0a 20 20 20 20 63 68   *zUtf8;..    ch
14294 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d 41 58  ar zMbcsPath[MAX
14295 5f 50 41 54 48 5d 3b 0d 0a 20 20 20 20 6f 73 47  _PATH];..    osG
14296 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f  etTempPathA(MAX_
14297 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61  PATH-30, zMbcsPa
14298 74 68 29 3b 0d 0a 20 20 20 20 7a 55 74 66 38 20  th);..    zUtf8 
14299 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f  = sqlite3_win32_
1429a 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62  mbcs_to_utf8(zMb
1429b 63 73 50 61 74 68 29 3b 0d 0a 20 20 20 20 69 66  csPath);..    if
1429c 28 20 7a 55 74 66 38 20 29 7b 0d 0a 20 20 20 20  ( zUtf8 ){..    
1429d 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1429e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20  tf(MAX_PATH-30, 
1429f 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c  zTempPath, "%s",
142a0 20 7a 55 74 66 38 29 3b 0d 0a 20 20 20 20 20 20   zUtf8);..      
142a1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 55 74  sqlite3_free(zUt
142a2 66 38 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  f8);..    }else{
142a3 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
142a4 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
142a5 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69  M;..    }..#endi
142a6 66 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43  f..  }....  /* C
142a7 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f 75  heck that the ou
142a8 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20 6c  tput buffer is l
142a9 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20  arge enough for 
142aa 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69  the temporary fi
142ab 6c 65 20 0d 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20  le ..  ** name. 
142ac 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65  If it is not, re
142ad 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
142ae 52 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  R...  */..  if( 
142af 28 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  (sqlite3Strlen30
142b0 28 7a 54 65 6d 70 50 61 74 68 29 20 2b 20 73 71  (zTempPath) + sq
142b1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 53 51  lite3Strlen30(SQ
142b2 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
142b3 52 45 46 49 58 29 20 2b 20 31 38 29 20 3e 3d 20  REFIX) + 18) >= 
142b4 6e 42 75 66 20 29 7b 0d 0a 20 20 20 20 72 65 74  nBuf ){..    ret
142b5 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
142b6 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 66 6f 72 28  ;..  }....  for(
142b7 69 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  i=sqlite3Strlen3
142b8 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e  0(zTempPath); i>
142b9 30 20 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69  0 && zTempPath[i
142ba 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b  -1]=='\\'; i--){
142bb 7d 0d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 69  }..  zTempPath[i
142bc 5d 20 3d 20 30 3b 0d 0a 0d 0a 20 20 73 71 6c 69  ] = 0;....  sqli
142bd 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
142be 66 2d 31 38 2c 20 7a 42 75 66 2c 0d 0a 20 20 20  f-18, zBuf,..   
142bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
142c0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
142c1 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
142c2 54 65 6d 70 50 61 74 68 29 3b 0d 0a 20 20 6a 20  TempPath);..  j 
142c3 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
142c4 30 28 7a 42 75 66 29 3b 0d 0a 20 20 73 71 6c 69  0(zBuf);..  sqli
142c5 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31  te3_randomness(1
142c6 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0d 0a 20  5, &zBuf[j]);.. 
142c7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20   for(i=0; i<15; 
142c8 69 2b 2b 2c 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20  i++, j++){..    
142c9 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29  zBuf[j] = (char)
142ca 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e  zChars[ ((unsign
142cb 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29  ed char)zBuf[j])
142cc 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29  %(sizeof(zChars)
142cd 2d 31 29 20 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 7a  -1) ];..  }..  z
142ce 42 75 66 5b 6a 5d 20 3d 20 30 3b 0d 0a 20 20 7a  Buf[j] = 0;..  z
142cf 42 75 66 5b 6a 2b 31 5d 20 3d 20 30 3b 0d 0a 0d  Buf[j+1] = 0;...
142d0 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 4d  .  OSTRACE(("TEM
142d1 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e  P FILENAME: %s\n
142d2 22 2c 20 7a 42 75 66 29 29 3b 0d 0a 20 20 72 65  ", zBuf));..  re
142d3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
142d4 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70  ..}..../*..** Op
142d5 65 6e 20 61 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a  en a file...*/..
142d6 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70  static int winOp
142d7 65 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  en(..  sqlite3_v
142d8 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
142d9 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0d   /* Not used */.
142da 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
142db 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  Name,        /* 
142dc 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
142dd 20 28 55 54 46 2d 38 29 20 2a 2f 0d 0a 20 20 73   (UTF-8) */..  s
142de 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
142df 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
142e0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  e the SQLite fil
142e1 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f  e handle here */
142e2 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  ..  int flags,  
142e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
142e4 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73   Open mode flags
142e5 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 4f 75 74   */..  int *pOut
142e6 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
142e7 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 75 72   /* Status retur
142e8 6e 20 66 6c 61 67 73 20 2a 2f 0d 0a 29 7b 0d 0a  n flags */..){..
142e9 20 20 48 41 4e 44 4c 45 20 68 3b 0d 0a 20 20 44    HANDLE h;..  D
142ea 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0d  WORD lastErrno;.
142eb 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 72  .  DWORD dwDesir
142ec 65 64 41 63 63 65 73 73 3b 0d 0a 20 20 44 57 4f  edAccess;..  DWO
142ed 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0d  RD dwShareMode;.
142ee 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74  .  DWORD dwCreat
142ef 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0d  ionDisposition;.
142f0 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73  .  DWORD dwFlags
142f1 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20  AndAttributes = 
142f2 30 3b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  0;..#if SQLITE_O
142f3 53 5f 57 49 4e 43 45 0d 0a 20 20 69 6e 74 20 69  S_WINCE..  int i
142f4 73 54 65 6d 70 20 3d 20 30 3b 0d 0a 23 65 6e 64  sTemp = 0;..#end
142f5 69 66 0d 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  if..  winFile *p
142f6 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
142f7 29 69 64 3b 0d 0a 20 20 76 6f 69 64 20 2a 7a 43  )id;..  void *zC
142f8 6f 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20  onverted;       
142f9 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61         /* Filena
142fa 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e  me in OS encodin
142fb 67 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  g */..  const ch
142fc 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20  ar *zUtf8Name = 
142fd 7a 4e 61 6d 65 3b 20 2f 2a 20 46 69 6c 65 6e 61  zName; /* Filena
142fe 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f  me in UTF-8 enco
142ff 64 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 63  ding */..  int c
14300 6e 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20  nt = 0;....  /* 
14301 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 61 74  If argument zPat
14302 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  h is a NULL poin
14303 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ter, this functi
14304 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20 74  on is required t
14305 6f 20 6f 70 65 6e 0d 0a 20 20 2a 2a 20 61 20 74  o open..  ** a t
14306 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 55  emporary file. U
14307 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20 74  se this buffer t
14308 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65  o store the file
14309 20 6e 61 6d 65 20 69 6e 2e 0d 0a 20 20 2a 2f 0d   name in...  */.
1430a 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
1430b 5b 4d 41 58 5f 50 41 54 48 2b 32 5d 3b 20 20 20  [MAX_PATH+2];   
1430c 20 20 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64    /* Buffer used
1430d 20 74 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20   to create temp 
1430e 66 69 6c 65 6e 61 6d 65 20 2a 2f 0d 0a 0d 0a 20  filename */.... 
1430f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
14310 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
14311 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 52 65 74 75  /* Function Retu
14312 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a 23 69 66 20  rn Code */..#if 
14313 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
14314 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   || SQLITE_OS_WI
14315 4e 43 45 0d 0a 20 20 69 6e 74 20 65 54 79 70 65  NCE..  int eType
14316 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46   = flags&0xFFFFF
14317 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66  F00;  /* Type of
14318 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
14319 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 69 6e  ..#endif....  in
1431a 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d  t isExclusive  =
1431b 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
1431c 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29  _OPEN_EXCLUSIVE)
1431d 3b 0d 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74  ;..  int isDelet
1431e 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26  e     = (flags &
1431f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
14320 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0d 0a 20 20  ETEONCLOSE);..  
14321 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20  int isCreate    
14322 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
14323 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b  TE_OPEN_CREATE);
14324 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
14325 0d 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e  ..  int isReadon
14326 6c 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20  ly   = (flags & 
14327 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
14328 4f 4e 4c 59 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ONLY);..#endif..
14329 20 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 74    int isReadWrit
1432a 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51  e  = (flags & SQ
1432b 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
1432c 49 54 45 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66  ITE);....#ifndef
1432d 20 4e 44 45 42 55 47 0d 0a 20 20 69 6e 74 20 69   NDEBUG..  int i
1432e 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 20 3d 20 28  sOpenJournal = (
1432f 69 73 43 72 65 61 74 65 20 26 26 20 28 0d 0a 20  isCreate && (.. 
14330 20 20 20 20 20 20 20 65 54 79 70 65 3d 3d 53 51         eType==SQ
14331 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52  LITE_OPEN_MASTER
14332 5f 4a 4f 55 52 4e 41 4c 20 0d 0a 20 20 20 20 20  _JOURNAL ..     
14333 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
14334 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
14335 41 4c 20 0d 0a 20 20 20 20 20 7c 7c 20 65 54 79  AL ..     || eTy
14336 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
14337 57 41 4c 0d 0a 20 20 29 29 3b 0d 0a 23 65 6e 64  WAL..  ));..#end
14338 69 66 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b  if....  /* Check
14339 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1433a 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72  tatements are tr
1433b 75 65 3a 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ue: ..  **..  **
1433c 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 6f     (a) Exactly o
1433d 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 52  ne of the READWR
1433e 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59  ITE and READONLY
1433f 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73   flags must be s
14340 65 74 2c 20 61 6e 64 20 0d 0a 20 20 2a 2a 20 20  et, and ..  **  
14341 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69   (b) if CREATE i
14342 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44  s set, then READ
14343 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  WRITE must also 
14344 62 65 20 73 65 74 2c 20 61 6e 64 0d 0a 20 20 2a  be set, and..  *
14345 2a 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55  *   (c) if EXCLU
14346 53 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65  SIVE is set, the
14347 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c  n CREATE must al
14348 73 6f 20 62 65 20 73 65 74 2e 0d 0a 20 20 2a 2a  so be set...  **
14349 20 20 20 28 64 29 20 69 66 20 44 45 4c 45 54 45     (d) if DELETE
1434a 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20  ONCLOSE is set, 
1434b 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74  then CREATE must
1434c 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0d 0a 20   also be set... 
1434d 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 28 69   */..  assert((i
1434e 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20  sReadonly==0 || 
1434f 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 29 20  isReadWrite==0) 
14350 26 26 20 28 69 73 52 65 61 64 57 72 69 74 65 20  && (isReadWrite 
14351 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 29 3b  || isReadonly));
14352 0d 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 65  ..  assert(isCre
14353 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64  ate==0 || isRead
14354 57 72 69 74 65 29 3b 0d 0a 20 20 61 73 73 65 72  Write);..  asser
14355 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30  t(isExclusive==0
14356 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0d 0a   || isCreate);..
14357 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
14358 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65  e==0 || isCreate
14359 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 6d  );....  /* The m
1435a 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75  ain DB, main jou
1435b 72 6e 61 6c 2c 20 57 41 4c 20 66 69 6c 65 20 61  rnal, WAL file a
1435c 6e 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  nd master journa
1435d 6c 20 61 72 65 20 6e 65 76 65 72 20 0d 0a 20 20  l are never ..  
1435e 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
1435f 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 20 61 72   deleted. Nor ar
14360 65 20 74 68 65 79 20 65 76 65 72 20 74 65 6d 70  e they ever temp
14361 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 20 2a 2f  orary files.  */
14362 0d 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73  ..  assert( (!is
14363 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29  Delete && zName)
14364 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54   || eType!=SQLIT
14365 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29  E_OPEN_MAIN_DB )
14366 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  ;..  assert( (!i
14367 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
14368 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
14369 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
1436a 52 4e 41 4c 20 29 3b 0d 0a 20 20 61 73 73 65 72  RNAL );..  asser
1436b 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26  t( (!isDelete &&
1436c 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65   zName) || eType
1436d 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
1436e 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0d  STER_JOURNAL );.
1436f 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44  .  assert( (!isD
14370 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20  elete && zName) 
14371 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45  || eType!=SQLITE
14372 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0d 0a 0d 0a  _OPEN_WAL );....
14373 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
14374 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
14375 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20   has set one of 
14376 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20  the "file-type" 
14377 66 6c 61 67 73 2e 20 2a 2f 0d 0a 20 20 61 73 73  flags. */..  ass
14378 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49  ert( eType==SQLI
14379 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
1437a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
1437b 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
1437c 44 42 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 65  DB ..       || e
1437d 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
1437e 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c  N_MAIN_JOURNAL |
1437f 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
14380 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41  OPEN_TEMP_JOURNA
14381 4c 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 65 54  L ..       || eT
14382 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
14383 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c  _SUBJOURNAL   ||
14384 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
14385 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
14386 41 4c 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 65  AL ..       || e
14387 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
14388 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c  N_TRANSIENT_DB |
14389 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
1438a 4f 50 45 4e 5f 57 41 4c 0d 0a 20 20 29 3b 0d 0a  OPEN_WAL..  );..
1438b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
1438c 30 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  0 );..  UNUSED_P
1438d 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0d  ARAMETER(pVfs);.
1438e 0a 0d 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  ...  pFile->h = 
1438f 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
14390 41 4c 55 45 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ALUE;....  /* If
14391 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
14392 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
14393 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67  ction is NULL, g
14394 65 6e 65 72 61 74 65 20 61 20 0d 0a 20 20 2a 2a  enerate a ..  **
14395 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
14396 6e 61 6d 65 20 74 6f 20 75 73 65 20 0d 0a 20 20  name to use ..  
14397 2a 2f 0d 0a 20 20 69 66 28 20 21 7a 55 74 66 38  */..  if( !zUtf8
14398 4e 61 6d 65 20 29 7b 0d 0a 20 20 20 20 61 73 73  Name ){..    ass
14399 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26 20  ert(isDelete && 
1439a 21 69 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 29 3b  !isOpenJournal);
1439b 0d 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 65  ..    rc = getTe
1439c 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b  mpname(MAX_PATH+
1439d 32 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0d 0a 20  2, zTmpname);.. 
1439e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1439f 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72  E_OK ){..      r
143a0 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d  eturn rc;..    }
143a1 0d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20  ..    zUtf8Name 
143a2 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0d 0a 20 20 7d  = zTmpname;..  }
143a3 0d 0a 0d 0a 20 20 2f 2a 20 44 61 74 61 62 61 73  ....  /* Databas
143a4 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20  e filenames are 
143a5 64 6f 75 62 6c 65 2d 7a 65 72 6f 20 74 65 72 6d  double-zero term
143a6 69 6e 61 74 65 64 20 69 66 20 74 68 65 79 20 61  inated if they a
143a7 72 65 20 6e 6f 74 0d 0a 20 20 2a 2a 20 55 52 49  re not..  ** URI
143a8 73 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72  s with parameter
143a9 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 79 20  s.  Hence, they 
143aa 63 61 6e 20 61 6c 77 61 79 73 20 62 65 20 70 61  can always be pa
143ab 73 73 65 64 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20  ssed into..  ** 
143ac 73 71 6c 69 74 65 33 5f 75 72 69 5f 70 61 72 61  sqlite3_uri_para
143ad 6d 65 74 65 72 28 29 2e 0d 0a 20 20 2a 2f 0d 0a  meter()...  */..
143ae 20 20 61 73 73 65 72 74 28 20 28 65 54 79 70 65    assert( (eType
143af 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
143b0 49 4e 5f 44 42 29 20 7c 7c 20 28 66 6c 61 67 73  IN_DB) || (flags
143b1 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55   & SQLITE_OPEN_U
143b2 52 49 29 20 7c 7c 0d 0a 20 20 20 20 20 20 20 20  RI) ||..        
143b3 7a 55 74 66 38 4e 61 6d 65 5b 73 74 72 6c 65 6e  zUtf8Name[strlen
143b4 28 7a 55 74 66 38 4e 61 6d 65 29 2b 31 5d 3d 3d  (zUtf8Name)+1]==
143b5 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6e  0 );....  /* Con
143b6 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e 61 6d  vert the filenam
143b7 65 20 74 6f 20 74 68 65 20 73 79 73 74 65 6d 20  e to the system 
143b8 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0d 0a 20 20  encoding. */..  
143b9 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
143ba 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
143bb 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0d 0a 20 20  (zUtf8Name);..  
143bc 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d  if( zConverted==
143bd 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  0 ){..    return
143be 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
143bf 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  MEM;..  }....  i
143c0 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29  f( isReadWrite )
143c1 7b 0d 0a 20 20 20 20 64 77 44 65 73 69 72 65 64  {..    dwDesired
143c2 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43  Access = GENERIC
143c3 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f  _READ | GENERIC_
143c4 57 52 49 54 45 3b 0d 0a 20 20 7d 65 6c 73 65 7b  WRITE;..  }else{
143c5 0d 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41  ..    dwDesiredA
143c6 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f  ccess = GENERIC_
143c7 52 45 41 44 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  READ;..  }....  
143c8 2f 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45  /* SQLITE_OPEN_E
143c9 58 43 4c 55 53 49 56 45 20 69 73 20 75 73 65 64  XCLUSIVE is used
143ca 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
143cb 61 74 20 61 20 6e 65 77 20 66 69 6c 65 20 69 73  at a new file is
143cc 20 0d 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 2e   ..  ** created.
143cd 20 53 51 4c 69 74 65 20 64 6f 65 73 6e 27 74 20   SQLite doesn't 
143ce 75 73 65 20 69 74 20 74 6f 20 69 6e 64 69 63 61  use it to indica
143cf 74 65 20 22 65 78 63 6c 75 73 69 76 65 20 61 63  te "exclusive ac
143d0 63 65 73 73 22 20 0d 0a 20 20 2a 2a 20 61 73 20  cess" ..  ** as 
143d1 69 74 20 69 73 20 75 73 75 61 6c 6c 79 20 75 6e  it is usually un
143d2 64 65 72 73 74 6f 6f 64 2e 0d 0a 20 20 2a 2f 0d  derstood...  */.
143d3 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69  .  if( isExclusi
143d4 76 65 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 43 72  ve ){..    /* Cr
143d5 65 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65  eates a new file
143d6 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65  , only if it doe
143d7 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
143d8 69 73 74 2e 20 2a 2f 0d 0a 20 20 20 20 2f 2a 20  ist. */..    /* 
143d9 49 66 20 74 68 65 20 66 69 6c 65 20 65 78 69 73  If the file exis
143da 74 73 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f  ts, it fails. */
143db 0d 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e  ..    dwCreation
143dc 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52  Disposition = CR
143dd 45 41 54 45 5f 4e 45 57 3b 0d 0a 20 20 7d 65 6c  EATE_NEW;..  }el
143de 73 65 20 69 66 28 20 69 73 43 72 65 61 74 65 20  se if( isCreate 
143df 29 7b 0d 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20  ){..    /* Open 
143e0 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f  existing file, o
143e1 72 20 63 72 65 61 74 65 20 69 66 20 69 74 20 64  r create if it d
143e2 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f 0d  oesn't exist */.
143e3 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44  .    dwCreationD
143e4 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45  isposition = OPE
143e5 4e 5f 41 4c 57 41 59 53 3b 0d 0a 20 20 7d 65 6c  N_ALWAYS;..  }el
143e6 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 4f 70 65 6e  se{..    /* Open
143e7 73 20 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69  s a file, only i
143e8 66 20 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0d  f it exists. */.
143e9 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44  .    dwCreationD
143ea 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45  isposition = OPE
143eb 4e 5f 45 58 49 53 54 49 4e 47 3b 0d 0a 20 20 7d  N_EXISTING;..  }
143ec 0d 0a 0d 0a 20 20 64 77 53 68 61 72 65 4d 6f 64  ....  dwShareMod
143ed 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52  e = FILE_SHARE_R
143ee 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45  EAD | FILE_SHARE
143ef 5f 57 52 49 54 45 3b 0d 0a 0d 0a 20 20 69 66 28  _WRITE;....  if(
143f0 20 69 73 44 65 6c 65 74 65 20 29 7b 0d 0a 23 69   isDelete ){..#i
143f1 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
143f2 45 0d 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e  E..    dwFlagsAn
143f3 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49  dAttributes = FI
143f4 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44  LE_ATTRIBUTE_HID
143f5 44 45 4e 3b 0d 0a 20 20 20 20 69 73 54 65 6d 70  DEN;..    isTemp
143f6 20 3d 20 31 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20   = 1;..#else..  
143f7 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
143f8 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54  ibutes = FILE_AT
143f9 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 52  TRIBUTE_TEMPORAR
143fa 59 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  Y..             
143fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
143fc 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55    | FILE_ATTRIBU
143fd 54 45 5f 48 49 44 44 45 4e 0d 0a 20 20 20 20 20  TE_HIDDEN..     
143fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
143ff 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45            | FILE
14400 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f  _FLAG_DELETE_ON_
14401 43 4c 4f 53 45 3b 0d 0a 23 65 6e 64 69 66 0d 0a  CLOSE;..#endif..
14402 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 64 77    }else{..    dw
14403 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
14404 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42  es = FILE_ATTRIB
14405 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0d 0a 20 20 7d  UTE_NORMAL;..  }
14406 0d 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66  ..  /* Reports f
14407 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74  rom the internet
14408 20 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72   are that perfor
14409 6d 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0d  mance is always.
1440a 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20  .  ** better if 
1440b 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d  FILE_FLAG_RANDOM
1440c 5f 41 43 43 45 53 53 20 69 73 20 75 73 65 64 2e  _ACCESS is used.
1440d 20 20 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20    Ticket #2699. 
1440e 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  */..#if SQLITE_O
1440f 53 5f 57 49 4e 43 45 0d 0a 20 20 64 77 46 6c 61  S_WINCE..  dwFla
14410 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
14411 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e  |= FILE_FLAG_RAN
14412 44 4f 4d 5f 41 43 43 45 53 53 3b 0d 0a 23 65 6e  DOM_ACCESS;..#en
14413 64 69 66 0d 0a 0d 0a 20 20 69 66 28 20 69 73 4e  dif....  if( isN
14414 54 28 29 20 29 7b 0d 0a 20 20 20 20 77 68 69 6c  T() ){..    whil
14415 65 28 20 28 68 20 3d 20 6f 73 43 72 65 61 74 65  e( (h = osCreate
14416 46 69 6c 65 57 28 28 4c 50 43 57 53 54 52 29 7a  FileW((LPCWSTR)z
14417 43 6f 6e 76 65 72 74 65 64 2c 0d 0a 20 20 20 20  Converted,..    
14418 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14419 20 20 20 20 20 20 20 20 20 20 64 77 44 65 73 69            dwDesi
1441a 72 65 64 41 63 63 65 73 73 2c 0d 0a 20 20 20 20  redAccess,..    
1441b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1441c 20 20 20 20 20 20 20 20 20 20 64 77 53 68 61 72            dwShar
1441d 65 4d 6f 64 65 2c 20 4e 55 4c 4c 2c 0d 0a 20 20  eMode, NULL,..  
1441e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1441f 20 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72              dwCr
14420 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
14421 6e 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  n,..            
14422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14423 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
14424 69 62 75 74 65 73 2c 0d 0a 20 20 20 20 20 20 20  ibutes,..       
14425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14426 20 20 20 20 20 20 20 4e 55 4c 4c 29 29 3d 3d 49         NULL))==I
14427 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41  NVALID_HANDLE_VA
14428 4c 55 45 20 26 26 0d 0a 20 20 20 20 20 20 20 20  LUE &&..        
14429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1442a 20 20 20 20 20 20 72 65 74 72 79 49 6f 65 72 72        retryIoerr
1442b 28 26 63 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e  (&cnt, &lastErrn
1442c 6f 29 20 29 7b 7d 0d 0a 2f 2a 20 69 73 4e 54 28  o) ){}../* isNT(
1442d 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
1442e 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
1442f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
14430 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0d 0a  ver executed. ..
14431 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 4e 53  ** Since the ANS
14432 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
14433 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
14434 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
14435 57 49 4e 43 45 2c 0d 0a 2a 2a 20 69 74 27 73 20  WINCE,..** it's 
14436 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
14437 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
14438 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
14439 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54  ...*/..#if SQLIT
1443a 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20  E_OS_WINCE==0.. 
1443b 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 77 68 69   }else{..    whi
1443c 6c 65 28 20 28 68 20 3d 20 6f 73 43 72 65 61 74  le( (h = osCreat
1443d 65 46 69 6c 65 41 28 28 4c 50 43 53 54 52 29 7a  eFileA((LPCSTR)z
1443e 43 6f 6e 76 65 72 74 65 64 2c 0d 0a 20 20 20 20  Converted,..    
1443f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14440 20 20 20 20 20 20 20 20 20 20 64 77 44 65 73 69            dwDesi
14441 72 65 64 41 63 63 65 73 73 2c 0d 0a 20 20 20 20  redAccess,..    
14442 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14443 20 20 20 20 20 20 20 20 20 20 64 77 53 68 61 72            dwShar
14444 65 4d 6f 64 65 2c 20 4e 55 4c 4c 2c 0d 0a 20 20  eMode, NULL,..  
14445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14446 20 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72              dwCr
14447 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
14448 6e 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  n,..            
14449 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1444a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
1444b 69 62 75 74 65 73 2c 0d 0a 20 20 20 20 20 20 20  ibutes,..       
1444c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1444d 20 20 20 20 20 20 20 4e 55 4c 4c 29 29 3d 3d 49         NULL))==I
1444e 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41  NVALID_HANDLE_VA
1444f 4c 55 45 20 26 26 0d 0a 20 20 20 20 20 20 20 20  LUE &&..        
14450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14451 20 20 20 20 20 20 72 65 74 72 79 49 6f 65 72 72        retryIoerr
14452 28 26 63 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e  (&cnt, &lastErrn
14453 6f 29 20 29 7b 7d 0d 0a 23 65 6e 64 69 66 0d 0a  o) ){}..#endif..
14454 20 20 7d 0d 0a 0d 0a 20 20 6c 6f 67 49 6f 65 72    }....  logIoer
14455 72 28 63 6e 74 29 3b 0d 0a 0d 0a 20 20 4f 53 54  r(cnt);....  OST
14456 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25  RACE(("OPEN %d %
14457 73 20 30 78 25 6c 78 20 25 73 5c 6e 22 2c 20 0d  s 0x%lx %s\n", .
14458 0a 20 20 20 20 20 20 20 20 20 20 20 68 2c 20 7a  .           h, z
14459 4e 61 6d 65 2c 20 64 77 44 65 73 69 72 65 64 41  Name, dwDesiredA
1445a 63 63 65 73 73 2c 20 0d 0a 20 20 20 20 20 20 20  ccess, ..       
1445b 20 20 20 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48      h==INVALID_H
1445c 41 4e 44 4c 45 5f 56 41 4c 55 45 20 3f 20 22 66  ANDLE_VALUE ? "f
1445d 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b  ailed" : "ok"));
1445e 0d 0a 0d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56  ....  if( h==INV
1445f 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55  ALID_HANDLE_VALU
14460 45 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65 2d  E ){..    pFile-
14461 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73  >lastErrno = las
14462 74 45 72 72 6e 6f 3b 0d 0a 20 20 20 20 77 69 6e  tErrno;..    win
14463 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
14464 43 41 4e 54 4f 50 45 4e 2c 20 70 46 69 6c 65 2d  CANTOPEN, pFile-
14465 3e 6c 61 73 74 45 72 72 6e 6f 2c 20 22 77 69 6e  >lastErrno, "win
14466 4f 70 65 6e 22 2c 20 7a 55 74 66 38 4e 61 6d 65  Open", zUtf8Name
14467 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
14468 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
14469 3b 0d 0a 20 20 20 20 69 66 28 20 69 73 52 65 61  ;..    if( isRea
1446a 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78 63  dWrite && !isExc
1446b 6c 75 73 69 76 65 20 29 7b 0d 0a 20 20 20 20 20  lusive ){..     
1446c 20 72 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28   return winOpen(
1446d 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c  pVfs, zName, id,
1446e 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ..             
1446f 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f  ((flags|SQLITE_O
14470 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 28  PEN_READONLY)&~(
14471 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
14472 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  TE|SQLITE_OPEN_R
14473 45 41 44 57 52 49 54 45 29 29 2c 20 70 4f 75 74  EADWRITE)), pOut
14474 46 6c 61 67 73 29 3b 0d 0a 20 20 20 20 7d 65 6c  Flags);..    }el
14475 73 65 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  se{..      retur
14476 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
14477 4e 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a  N_BKPT;..    }..
14478 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 4f 75    }....  if( pOu
14479 74 46 6c 61 67 73 20 29 7b 0d 0a 20 20 20 20 69  tFlags ){..    i
1447a 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29  f( isReadWrite )
1447b 7b 0d 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c  {..      *pOutFl
1447c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  ags = SQLITE_OPE
1447d 4e 5f 52 45 41 44 57 52 49 54 45 3b 0d 0a 20 20  N_READWRITE;..  
1447e 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
1447f 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c  *pOutFlags = SQL
14480 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
14481 59 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  Y;..    }..  }..
14482 0d 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65  ..  memset(pFile
14483 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69  , 0, sizeof(*pFi
14484 6c 65 29 29 3b 0d 0a 20 20 70 46 69 6c 65 2d 3e  le));..  pFile->
14485 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f  pMethod = &winIo
14486 4d 65 74 68 6f 64 3b 0d 0a 20 20 70 46 69 6c 65  Method;..  pFile
14487 2d 3e 68 20 3d 20 68 3b 0d 0a 20 20 70 46 69 6c  ->h = h;..  pFil
14488 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e  e->lastErrno = N
14489 4f 5f 45 52 52 4f 52 3b 0d 0a 20 20 70 46 69 6c  O_ERROR;..  pFil
1448a 65 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0d  e->pVfs = pVfs;.
1448b 0a 20 20 70 46 69 6c 65 2d 3e 70 53 68 6d 20 3d  .  pFile->pShm =
1448c 20 30 3b 0d 0a 20 20 70 46 69 6c 65 2d 3e 7a 50   0;..  pFile->zP
1448d 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0d 0a 20 20  ath = zName;..  
1448e 69 66 28 20 73 71 6c 69 74 65 33 5f 75 72 69 5f  if( sqlite3_uri_
1448f 62 6f 6f 6c 65 61 6e 28 7a 4e 61 6d 65 2c 20 22  boolean(zName, "
14490 70 73 6f 77 22 2c 20 53 51 4c 49 54 45 5f 50 4f  psow", SQLITE_PO
14491 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54  WERSAFE_OVERWRIT
14492 45 29 20 29 7b 0d 0a 20 20 20 20 70 46 69 6c 65  E) ){..    pFile
14493 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 57  ->ctrlFlags |= W
14494 49 4e 46 49 4c 45 5f 50 53 4f 57 3b 0d 0a 20 20  INFILE_PSOW;..  
14495 7d 0d 0a 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  }....#if SQLITE_
14496 4f 53 5f 57 49 4e 43 45 0d 0a 20 20 69 66 28 20  OS_WINCE..  if( 
14497 69 73 52 65 61 64 57 72 69 74 65 20 26 26 20 65  isReadWrite && e
14498 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
14499 4e 5f 4d 41 49 4e 5f 44 42 0d 0a 20 20 20 20 20  N_MAIN_DB..     
1449a 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61 74    && !winceCreat
1449b 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69  eLock(zName, pFi
1449c 6c 65 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 6f  le)..  ){..    o
1449d 73 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b  sCloseHandle(h);
1449e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
1449f 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0d  ee(zConverted);.
144a0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
144a1 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54  TE_CANTOPEN_BKPT
144a2 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 69 73  ;..  }..  if( is
144a3 54 65 6d 70 20 29 7b 0d 0a 20 20 20 20 70 46 69  Temp ){..    pFi
144a4 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f  le->zDeleteOnClo
144a5 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b  se = zConverted;
144a6 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69  ..  }else..#endi
144a7 66 0d 0a 20 20 7b 0d 0a 20 20 20 20 73 71 6c 69  f..  {..    sqli
144a8 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72  te3_free(zConver
144a9 74 65 64 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ted);..  }....  
144aa 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b  OpenCounter(+1);
144ab 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
144ac 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65  }..../*..** Dele
144ad 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  te the named fil
144ae 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20  e...**..** Note 
144af 74 68 61 74 20 57 69 6e 64 6f 77 73 20 64 6f 65  that Windows doe
144b0 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69  s not allow a fi
144b1 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  le to be deleted
144b2 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 0d 0a   if some other..
144b3 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 69  ** process has i
144b4 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d  t open.  Sometim
144b5 65 73 20 61 20 76 69 72 75 73 20 73 63 61 6e 6e  es a virus scann
144b6 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67 20 70  er or indexing p
144b7 72 6f 67 72 61 6d 0d 0a 2a 2a 20 77 69 6c 6c 20  rogram..** will 
144b8 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66  open a journal f
144b9 69 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74 65  ile shortly afte
144ba 72 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20  r it is created 
144bb 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0d 0a  in order to do..
144bc 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74 20 64  ** whatever it d
144bd 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68 69 73  oes.  While this
144be 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69   other process i
144bf 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0d 0a 2a  s holding the..*
144c0 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20  * file open, we 
144c1 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20 74  will be unable t
144c2 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54 6f  o delete it.  To
144c3 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69   work around thi
144c4 73 0d 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77  s..** problem, w
144c5 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c  e delay 100 mill
144c6 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79  iseconds and try
144c7 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e   to delete again
144c8 2e 20 20 55 70 0d 0a 2a 2a 20 74 6f 20 4d 58 5f  .  Up..** to MX_
144c9 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54  DELETION_ATTEMPT
144ca 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d  s deletion attem
144cb 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f  pts are run befo
144cc 72 65 20 67 69 76 69 6e 67 0d 0a 2a 2a 20 75 70  re giving..** up
144cd 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61   and returning a
144ce 6e 20 65 72 72 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74  n error...*/..st
144cf 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65  atic int winDele
144d0 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  te(..  sqlite3_v
144d1 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
144d2 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
144d3 6e 20 77 69 6e 33 32 20 2a 2f 0d 0a 20 20 63 6f  n win32 */..  co
144d4 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
144d5 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ame,      /* Nam
144d6 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
144d7 65 74 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 79  ete */..  int sy
144d8 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20  ncDir           
144d9 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
144da 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0d 0a 29  d on win32 */..)
144db 7b 0d 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30  {..  int cnt = 0
144dc 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  ;..  int rc;..  
144dd 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b  DWORD lastErrno;
144de 0d 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65  ..  void *zConve
144df 72 74 65 64 3b 0d 0a 20 20 55 4e 55 53 45 44 5f  rted;..  UNUSED_
144e0 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
144e1 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
144e2 45 54 45 52 28 73 79 6e 63 44 69 72 29 3b 0d 0a  ETER(syncDir);..
144e3 0d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
144e4 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
144e5 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b  E_IOERR_DELETE);
144e6 0d 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d  ..  zConverted =
144e7 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
144e8 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  name(zFilename);
144e9 0d 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ..  if( zConvert
144ea 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65  ed==0 ){..    re
144eb 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
144ec 52 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20  R_NOMEM;..  }.. 
144ed 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0d 0a   if( isNT() ){..
144ee 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a 20 20 20      rc = 1;..   
144ef 20 77 68 69 6c 65 28 20 6f 73 47 65 74 46 69 6c   while( osGetFil
144f0 65 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f  eAttributesW(zCo
144f1 6e 76 65 72 74 65 64 29 21 3d 49 4e 56 41 4c 49  nverted)!=INVALI
144f2 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
144f3 53 20 26 26 0d 0a 20 20 20 20 20 20 20 20 20 28  S &&..         (
144f4 72 63 20 3d 20 6f 73 44 65 6c 65 74 65 46 69 6c  rc = osDeleteFil
144f5 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 3d  eW(zConverted))=
144f6 3d 30 20 26 26 20 72 65 74 72 79 49 6f 65 72 72  =0 && retryIoerr
144f7 28 26 63 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e  (&cnt, &lastErrn
144f8 6f 29 20 29 7b 7d 0d 0a 20 20 20 20 72 63 20 3d  o) ){}..    rc =
144f9 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20   rc ? SQLITE_OK 
144fa 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  : SQLITE_ERROR;.
144fb 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
144fc 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
144fd 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
144fe 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
144ff 63 75 74 65 64 2e 20 0d 0a 2a 2a 20 53 69 6e 63  cuted. ..** Sinc
14500 65 20 74 68 65 20 41 4e 53 49 20 76 65 72 73 69  e the ANSI versi
14501 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
14502 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
14503 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0d  xist for WINCE,.
14504 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61  .** it's importa
14505 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65  nt to not refere
14506 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e  nce them for WIN
14507 43 45 20 62 75 69 6c 64 73 2e 0d 0a 2a 2f 0d 0a  CE builds...*/..
14508 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
14509 4e 43 45 3d 3d 30 0d 0a 20 20 7d 65 6c 73 65 7b  NCE==0..  }else{
1450a 0d 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a 20  ..    rc = 1;.. 
1450b 20 20 20 77 68 69 6c 65 28 20 6f 73 47 65 74 46     while( osGetF
1450c 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28 7a  ileAttributesA(z
1450d 43 6f 6e 76 65 72 74 65 64 29 21 3d 49 4e 56 41  Converted)!=INVA
1450e 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
1450f 54 45 53 20 26 26 0d 0a 20 20 20 20 20 20 20 20  TES &&..        
14510 20 28 72 63 20 3d 20 6f 73 44 65 6c 65 74 65 46   (rc = osDeleteF
14511 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29  ileA(zConverted)
14512 29 3d 3d 30 20 26 26 20 72 65 74 72 79 49 6f 65  )==0 && retryIoe
14513 72 72 28 26 63 6e 74 2c 20 26 6c 61 73 74 45 72  rr(&cnt, &lastEr
14514 72 6e 6f 29 20 29 7b 7d 0d 0a 20 20 20 20 72 63  rno) ){}..    rc
14515 20 3d 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f   = rc ? SQLITE_O
14516 4b 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  K : SQLITE_ERROR
14517 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a  ;..#endif..  }..
14518 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20    if( rc ){..   
14519 20 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f   rc = winLogErro
1451a 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  r(SQLITE_IOERR_D
1451b 45 4c 45 54 45 2c 20 6c 61 73 74 45 72 72 6e 6f  ELETE, lastErrno
1451c 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
1451d 22 77 69 6e 44 65 6c 65 74 65 22 2c 20 7a 46 69  "winDelete", zFi
1451e 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 7d 65 6c 73  lename);..  }els
1451f 65 7b 0d 0a 20 20 20 20 6c 6f 67 49 6f 65 72 72  e{..    logIoerr
14520 28 63 6e 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73  (cnt);..  }..  s
14521 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e  qlite3_free(zCon
14522 76 65 72 74 65 64 29 3b 0d 0a 20 20 4f 53 54 52  verted);..  OSTR
14523 41 43 45 28 28 22 44 45 4c 45 54 45 20 5c 22 25  ACE(("DELETE \"%
14524 73 5c 22 20 25 73 5c 6e 22 2c 20 7a 46 69 6c 65  s\" %s\n", zFile
14525 6e 61 6d 65 2c 20 28 72 63 20 3f 20 22 66 61 69  name, (rc ? "fai
14526 6c 65 64 22 20 3a 20 22 6f 6b 22 20 29 29 29 3b  led" : "ok" )));
14527 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
14528 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63  }..../*..** Chec
14529 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20  k the existance 
1452a 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20  and status of a 
1452b 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  file...*/..stati
1452c 63 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28  c int winAccess(
1452d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ..  sqlite3_vfs 
1452e 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f  *pVfs,         /
1452f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69  * Not used on wi
14530 6e 33 32 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  n32 */..  const 
14531 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
14532 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
14533 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f  file to check */
14534 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  ..  int flags,  
14535 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14536 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74  * Type of test t
14537 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66  o make on this f
14538 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70  ile */..  int *p
14539 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 20  ResOut          
1453a 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 73       /* OUT: Res
1453b 75 6c 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 44 57  ult */..){..  DW
1453c 4f 52 44 20 61 74 74 72 3b 0d 0a 20 20 69 6e 74  ORD attr;..  int
1453d 20 72 63 20 3d 20 30 3b 0d 0a 20 20 44 57 4f 52   rc = 0;..  DWOR
1453e 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0d 0a 20 20  D lastErrno;..  
1453f 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
14540 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
14541 4d 45 54 45 52 28 70 56 66 73 29 3b 0d 0a 0d 0a  METER(pVfs);....
14542 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
14543 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
14544 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b 20 29  _IOERR_ACCESS; )
14545 3b 0d 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  ;..  zConverted 
14546 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
14547 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
14548 3b 0d 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  ;..  if( zConver
14549 74 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ted==0 ){..    r
1454a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
1454b 52 52 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a  RR_NOMEM;..  }..
1454c 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0d    if( isNT() ){.
1454d 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30  .    int cnt = 0
1454e 3b 0d 0a 20 20 20 20 57 49 4e 33 32 5f 46 49 4c  ;..    WIN32_FIL
1454f 45 5f 41 54 54 52 49 42 55 54 45 5f 44 41 54 41  E_ATTRIBUTE_DATA
14550 20 73 41 74 74 72 44 61 74 61 3b 0d 0a 20 20 20   sAttrData;..   
14551 20 6d 65 6d 73 65 74 28 26 73 41 74 74 72 44 61   memset(&sAttrDa
14552 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41  ta, 0, sizeof(sA
14553 74 74 72 44 61 74 61 29 29 3b 0d 0a 20 20 20 20  ttrData));..    
14554 77 68 69 6c 65 28 20 21 28 72 63 20 3d 20 6f 73  while( !(rc = os
14555 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65  GetFileAttribute
14556 73 45 78 57 28 28 4c 50 43 57 53 54 52 29 7a 43  sExW((LPCWSTR)zC
14557 6f 6e 76 65 72 74 65 64 2c 0d 0a 20 20 20 20 20  onverted,..     
14558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14559 20 20 20 20 20 20 20 20 47 65 74 46 69 6c 65 45          GetFileE
1455a 78 49 6e 66 6f 53 74 61 6e 64 61 72 64 2c 20 0d  xInfoStandard, .
1455b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1455c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 73                &s
1455d 41 74 74 72 44 61 74 61 29 29 20 26 26 20 72 65  AttrData)) && re
1455e 74 72 79 49 6f 65 72 72 28 26 63 6e 74 2c 20 26  tryIoerr(&cnt, &
1455f 6c 61 73 74 45 72 72 6e 6f 29 20 29 7b 7d 0d 0a  lastErrno) ){}..
14560 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20      if( rc ){.. 
14561 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 53       /* For an S
14562 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
14563 53 54 53 20 71 75 65 72 79 2c 20 74 72 65 61 74  STS query, treat
14564 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 66   a zero-length f
14565 69 6c 65 0d 0a 20 20 20 20 20 20 2a 2a 20 61 73  ile..      ** as
14566 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
14567 65 78 69 73 74 2e 0d 0a 20 20 20 20 20 20 2a 2f  exist...      */
14568 0d 0a 20 20 20 20 20 20 69 66 28 20 20 20 20 66  ..      if(    f
14569 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f 41 43 43  lags==SQLITE_ACC
1456a 45 53 53 5f 45 58 49 53 54 53 0d 0a 20 20 20 20  ESS_EXISTS..    
1456b 20 20 20 20 20 20 26 26 20 73 41 74 74 72 44 61        && sAttrDa
1456c 74 61 2e 6e 46 69 6c 65 53 69 7a 65 48 69 67 68  ta.nFileSizeHigh
1456d 3d 3d 30 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ==0 ..          
1456e 26 26 20 73 41 74 74 72 44 61 74 61 2e 6e 46 69  && sAttrData.nFi
1456f 6c 65 53 69 7a 65 4c 6f 77 3d 3d 30 20 29 7b 0d  leSizeLow==0 ){.
14570 0a 20 20 20 20 20 20 20 20 61 74 74 72 20 3d 20  .        attr = 
14571 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
14572 52 49 42 55 54 45 53 3b 0d 0a 20 20 20 20 20 20  RIBUTES;..      
14573 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
14574 61 74 74 72 20 3d 20 73 41 74 74 72 44 61 74 61  attr = sAttrData
14575 2e 64 77 46 69 6c 65 41 74 74 72 69 62 75 74 65  .dwFileAttribute
14576 73 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  s;..      }..   
14577 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6c   }else{..      l
14578 6f 67 49 6f 65 72 72 28 63 6e 74 29 3b 0d 0a 20  ogIoerr(cnt);.. 
14579 20 20 20 20 20 69 66 28 20 6c 61 73 74 45 72 72       if( lastErr
1457a 6e 6f 21 3d 45 52 52 4f 52 5f 46 49 4c 45 5f 4e  no!=ERROR_FILE_N
1457b 4f 54 5f 46 4f 55 4e 44 20 29 7b 0d 0a 20 20 20  OT_FOUND ){..   
1457c 20 20 20 20 20 77 69 6e 4c 6f 67 45 72 72 6f 72       winLogError
1457d 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43  (SQLITE_IOERR_AC
1457e 43 45 53 53 2c 20 6c 61 73 74 45 72 72 6e 6f 2c  CESS, lastErrno,
1457f 20 22 77 69 6e 41 63 63 65 73 73 22 2c 20 7a 46   "winAccess", zF
14580 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 20  ilename);..     
14581 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
14582 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0d 0a 20 20  zConverted);..  
14583 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
14584 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
14585 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
14586 0a 20 20 20 20 20 20 20 20 61 74 74 72 20 3d 20  .        attr = 
14587 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
14588 52 49 42 55 54 45 53 3b 0d 0a 20 20 20 20 20 20  RIBUTES;..      
14589 7d 0d 0a 20 20 20 20 7d 0d 0a 2f 2a 20 69 73 4e  }..    }../* isN
1458a 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
1458b 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
1458c 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
1458d 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
1458e 0d 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41  ..** Since the A
1458f 4e 53 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  NSI version of t
14590 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
14591 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
14592 72 20 57 49 4e 43 45 2c 0d 0a 2a 2a 20 69 74 27  r WINCE,..** it'
14593 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
14594 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
14595 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
14596 64 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c  ds...*/..#if SQL
14597 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d  ITE_OS_WINCE==0.
14598 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61  .  }else{..    a
14599 74 74 72 20 3d 20 6f 73 47 65 74 46 69 6c 65 41  ttr = osGetFileA
1459a 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72  ttributesA((char
1459b 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0d 0a  *)zConverted);..
1459c 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 20 20 73  #endif..  }..  s
1459d 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e  qlite3_free(zCon
1459e 76 65 72 74 65 64 29 3b 0d 0a 20 20 73 77 69 74  verted);..  swit
1459f 63 68 28 20 66 6c 61 67 73 20 29 7b 0d 0a 20 20  ch( flags ){..  
145a0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
145a1 43 45 53 53 5f 52 45 41 44 3a 0d 0a 20 20 20 20  CESS_READ:..    
145a2 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
145a3 53 53 5f 45 58 49 53 54 53 3a 0d 0a 20 20 20 20  SS_EXISTS:..    
145a4 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56    rc = attr!=INV
145a5 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
145a6 55 54 45 53 3b 0d 0a 20 20 20 20 20 20 62 72 65  UTES;..      bre
145a7 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65 20 53 51  ak;..    case SQ
145a8 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
145a9 57 52 49 54 45 3a 0d 0a 20 20 20 20 20 20 72 63  WRITE:..      rc
145aa 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 44   = attr!=INVALID
145ab 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
145ac 20 26 26 0d 0a 20 20 20 20 20 20 20 20 20 20 20   &&..           
145ad 20 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41    (attr & FILE_A
145ae 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c  TTRIBUTE_READONL
145af 59 29 3d 3d 30 3b 0d 0a 20 20 20 20 20 20 62 72  Y)==0;..      br
145b0 65 61 6b 3b 0d 0a 20 20 20 20 64 65 66 61 75 6c  eak;..    defaul
145b1 74 3a 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  t:..      assert
145b2 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73  (!"Invalid flags
145b3 20 61 72 67 75 6d 65 6e 74 22 29 3b 0d 0a 20 20   argument");..  
145b4 7d 0d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  }..  *pResOut = 
145b5 72 63 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  rc;..  return SQ
145b6 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d  LITE_OK;..}.....
145b7 0a 2f 2a 0d 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ./*..** Turn a r
145b8 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
145b9 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
145ba 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68  hname.  Write th
145bb 65 20 66 75 6c 6c 0d 0a 2a 2a 20 70 61 74 68 6e  e full..** pathn
145bc 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e  ame into zOut[].
145bd 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65    zOut[] will be
145be 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e   at least pVfs->
145bf 6d 78 50 61 74 68 6e 61 6d 65 0d 0a 2a 2a 20 62  mxPathname..** b
145c0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0d 0a 2a  ytes in size...*
145c1 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  /..static int wi
145c2 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0d 0a  nFullPathname(..
145c3 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
145c4 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Vfs,            
145c5 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
145c6 73 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 63  s object */..  c
145c7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61  onst char *zRela
145c8 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  tive,        /* 
145c9 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
145ca 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0d  e input path */.
145cb 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20  .  int nFull,   
145cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
145cd 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70   /* Size of outp
145ce 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  ut buffer in byt
145cf 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  es */..  char *z
145d0 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Full            
145d1 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
145d2 20 62 75 66 66 65 72 20 2a 2f 0d 0a 29 7b 0d 0a   buffer */..){..
145d3 20 20 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28    ..#if defined(
145d4 5f 5f 43 59 47 57 49 4e 5f 5f 29 0d 0a 20 20 53  __CYGWIN__)..  S
145d5 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
145d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
145d7 52 4f 52 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44  ROR );..  UNUSED
145d8 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
145d9 29 3b 0d 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e  );..  cygwin_con
145da 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f  v_to_full_win32_
145db 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20  path(zRelative, 
145dc 7a 46 75 6c 6c 29 3b 0d 0a 20 20 72 65 74 75 72  zFull);..  retur
145dd 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 23 65  n SQLITE_OK;..#e
145de 6e 64 69 66 0d 0a 0d 0a 23 69 66 20 53 51 4c 49  ndif....#if SQLI
145df 54 45 5f 4f 53 5f 57 49 4e 43 45 0d 0a 20 20 53  TE_OS_WINCE..  S
145e0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
145e1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
145e2 52 4f 52 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44  ROR );..  UNUSED
145e3 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
145e4 29 3b 0d 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68  );..  /* WinCE h
145e5 61 73 20 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66  as no concept of
145e6 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68   a relative path
145e7 6e 61 6d 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d  name, or so I am
145e8 20 74 6f 6c 64 2e 20 2a 2f 0d 0a 20 20 73 71 6c   told. */..  sql
145e9 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56  ite3_snprintf(pV
145ea 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20  fs->mxPathname, 
145eb 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 52 65  zFull, "%s", zRe
145ec 6c 61 74 69 76 65 29 3b 0d 0a 20 20 72 65 74 75  lative);..  retu
145ed 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 23  rn SQLITE_OK;..#
145ee 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20 21 53 51  endif....#if !SQ
145ef 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
145f0 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57   !defined(__CYGW
145f1 49 4e 5f 5f 29 0d 0a 20 20 69 6e 74 20 6e 42 79  IN__)..  int nBy
145f2 74 65 3b 0d 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  te;..  void *zCo
145f3 6e 76 65 72 74 65 64 3b 0d 0a 20 20 63 68 61 72  nverted;..  char
145f4 20 2a 7a 4f 75 74 3b 0d 0a 0d 0a 20 20 2f 2a 20   *zOut;....  /* 
145f5 49 66 20 74 68 69 73 20 70 61 74 68 20 6e 61 6d  If this path nam
145f6 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 22 2f  e begins with "/
145f7 58 3a 22 2c 20 77 68 65 72 65 20 22 58 22 20 69  X:", where "X" i
145f8 73 20 61 6e 79 20 61 6c 70 68 61 62 65 74 69 63  s any alphabetic
145f9 0d 0a 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72  ..  ** character
145fa 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 69 6e  , discard the in
145fb 69 74 69 61 6c 20 22 2f 22 20 66 72 6f 6d 20 74  itial "/" from t
145fc 68 65 20 70 61 74 68 6e 61 6d 65 2e 0d 0a 20 20  he pathname...  
145fd 2a 2f 0d 0a 20 20 69 66 28 20 7a 52 65 6c 61 74  */..  if( zRelat
145fe 69 76 65 5b 30 5d 3d 3d 27 2f 27 20 26 26 20 73  ive[0]=='/' && s
145ff 71 6c 69 74 65 33 49 73 61 6c 70 68 61 28 7a 52  qlite3Isalpha(zR
14600 65 6c 61 74 69 76 65 5b 31 5d 29 20 26 26 20 7a  elative[1]) && z
14601 52 65 6c 61 74 69 76 65 5b 32 5d 3d 3d 27 3a 27  Relative[2]==':'
14602 20 29 7b 0d 0a 20 20 20 20 7a 52 65 6c 61 74 69   ){..    zRelati
14603 76 65 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ve++;..  }....  
14604 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73  /* It's odd to s
14605 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72  imulate an io-er
14606 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65  ror here, but re
14607 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73  ally this is jus
14608 74 0d 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  t..  ** using th
14609 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61  e io-error infra
1460a 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73  structure to tes
1460b 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61  t that SQLite ha
1460c 6e 64 6c 65 73 20 74 68 69 73 0d 0a 20 20 2a 2a  ndles this..  **
1460d 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e   function failin
1460e 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  g. This function
1460f 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20   could fail if, 
14610 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  for example, the
14611 0d 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77  ..  ** current w
14612 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79  orking directory
14613 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b   has been unlink
14614 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 53 69 6d  ed...  */..  Sim
14615 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
14616 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
14617 52 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50  R );..  UNUSED_P
14618 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b  ARAMETER(nFull);
14619 0d 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d  ..  zConverted =
1461a 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
1461b 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 3b  name(zRelative);
1461c 0d 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ..  if( zConvert
1461d 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65  ed==0 ){..    re
1461e 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
1461f 52 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20  R_NOMEM;..  }.. 
14620 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0d 0a   if( isNT() ){..
14621 20 20 20 20 4c 50 57 53 54 52 20 7a 54 65 6d 70      LPWSTR zTemp
14622 3b 0d 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 6f  ;..    nByte = o
14623 73 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65  sGetFullPathName
14624 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e 76  W((LPCWSTR)zConv
14625 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20  erted, 0, 0, 0) 
14626 2b 20 33 3b 0d 0a 20 20 20 20 7a 54 65 6d 70 20  + 3;..    zTemp 
14627 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
14628 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a  ( nByte*sizeof(z
14629 54 65 6d 70 5b 30 5d 29 20 29 3b 0d 0a 20 20 20  Temp[0]) );..   
1462a 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b   if( zTemp==0 ){
1462b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
1462c 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
1462d 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
1462e 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
1462f 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  EM;..    }..    
14630 6f 73 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d  osGetFullPathNam
14631 65 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e  eW((LPCWSTR)zCon
14632 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a  verted, nByte, z
14633 54 65 6d 70 2c 20 30 29 3b 0d 0a 20 20 20 20 73  Temp, 0);..    s
14634 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e  qlite3_free(zCon
14635 76 65 72 74 65 64 29 3b 0d 0a 20 20 20 20 7a 4f  verted);..    zO
14636 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ut = unicodeToUt
14637 66 38 28 7a 54 65 6d 70 29 3b 0d 0a 20 20 20 20  f8(zTemp);..    
14638 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
14639 6d 70 29 3b 0d 0a 2f 2a 20 69 73 4e 54 28 29 20  mp);../* isNT() 
1463a 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
1463b 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
1463c 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
1463d 72 20 65 78 65 63 75 74 65 64 2e 20 0d 0a 2a 2a  r executed. ..**
1463e 20 53 69 6e 63 65 20 74 68 65 20 41 4e 53 49 20   Since the ANSI 
1463f 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65  version of these
14640 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20   Windows API do 
14641 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49  not exist for WI
14642 4e 43 45 2c 0d 0a 2a 2a 20 69 74 27 73 20 69 6d  NCE,..** it's im
14643 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
14644 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
14645 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0d  r WINCE builds..
14646 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .*/..#if SQLITE_
14647 4f 53 5f 57 49 4e 43 45 3d 3d 30 0d 0a 20 20 7d  OS_WINCE==0..  }
14648 65 6c 73 65 7b 0d 0a 20 20 20 20 63 68 61 72 20  else{..    char 
14649 2a 7a 54 65 6d 70 3b 0d 0a 20 20 20 20 6e 42 79  *zTemp;..    nBy
1464a 74 65 20 3d 20 6f 73 47 65 74 46 75 6c 6c 50 61  te = osGetFullPa
1464b 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a  thNameA((char*)z
1464c 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c  Converted, 0, 0,
1464d 20 30 29 20 2b 20 33 3b 0d 0a 20 20 20 20 7a 54   0) + 3;..    zT
1464e 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  emp = sqlite3_ma
1464f 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65  lloc( nByte*size
14650 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0d  of(zTemp[0]) );.
14651 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d  .    if( zTemp==
14652 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  0 ){..      sqli
14653 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72  te3_free(zConver
14654 74 65 64 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  ted);..      ret
14655 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
14656 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a  _NOMEM;..    }..
14657 20 20 20 20 6f 73 47 65 74 46 75 6c 6c 50 61 74      osGetFullPat
14658 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43  hNameA((char*)zC
14659 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c  onverted, nByte,
1465a 20 7a 54 65 6d 70 2c 20 30 29 3b 0d 0a 20 20 20   zTemp, 0);..   
1465b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
1465c 6f 6e 76 65 72 74 65 64 29 3b 0d 0a 20 20 20 20  onverted);..    
1465d 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77  zOut = sqlite3_w
1465e 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66  in32_mbcs_to_utf
1465f 38 28 7a 54 65 6d 70 29 3b 0d 0a 20 20 20 20 73  8(zTemp);..    s
14660 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d  qlite3_free(zTem
14661 70 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d  p);..#endif..  }
14662 0d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0d  ..  if( zOut ){.
14663 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
14664 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61  rintf(pVfs->mxPa
14665 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22  thname, zFull, "
14666 25 73 22 2c 20 7a 4f 75 74 29 3b 0d 0a 20 20 20  %s", zOut);..   
14667 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f   sqlite3_free(zO
14668 75 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ut);..    return
14669 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d   SQLITE_OK;..  }
1466a 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72  else{..    retur
1466b 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  n SQLITE_IOERR_N
1466c 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  OMEM;..  }..#end
1466d 69 66 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66  if..}....#ifndef
1466e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
1466f 44 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a 2f 2a 0d  D_EXTENSION../*.
14670 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
14671 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
14672 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
14673 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
14674 73 0d 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  s..** within the
14675 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
14676 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
14677 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
14678 0d 0a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74  ..*/../*..** Int
14679 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
1467a 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
1467b 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
1467c 74 72 79 20 70 6f 69 6e 74 73 0d 0a 2a 2a 20 77  try points..** w
1467d 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64  ithin the shared
1467e 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c   library, and cl
1467f 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64  osing the shared
14680 20 6c 69 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 73   library...*/..s
14681 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44  tatic void *winD
14682 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
14683 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63  s *pVfs, const c
14684 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
14685 0d 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0d 0a 20  ..  HANDLE h;.. 
14686 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
14687 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
14688 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d  ilename(zFilenam
14689 65 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  e);..  UNUSED_PA
1468a 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0d 0a  RAMETER(pVfs);..
1468b 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
1468c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  ==0 ){..    retu
1468d 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  rn 0;..  }..  if
1468e 28 20 69 73 4e 54 28 29 20 29 7b 0d 0a 20 20 20  ( isNT() ){..   
1468f 20 68 20 3d 20 6f 73 4c 6f 61 64 4c 69 62 72 61   h = osLoadLibra
14690 72 79 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f  ryW((LPCWSTR)zCo
14691 6e 76 65 72 74 65 64 29 3b 0d 0a 2f 2a 20 69 73  nverted);../* is
14692 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
14693 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
14694 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
14695 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
14696 20 0d 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20   ..** Since the 
14697 41 4e 53 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  ANSI version of 
14698 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
14699 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
1469a 6f 72 20 57 49 4e 43 45 2c 0d 0a 2a 2a 20 69 74  or WINCE,..** it
1469b 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  's important to 
1469c 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68  not reference th
1469d 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69  em for WINCE bui
1469e 6c 64 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51  lds...*/..#if SQ
1469f 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
146a0 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
146a1 68 20 3d 20 6f 73 4c 6f 61 64 4c 69 62 72 61 72  h = osLoadLibrar
146a2 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  yA((char*)zConve
146a3 72 74 65 64 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  rted);..#endif..
146a4 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66    }..  sqlite3_f
146a5 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
146a6 0d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ..  return (void
146a7 2a 29 68 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20  *)h;..}..static 
146a8 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72 28  void winDlError(
146a9 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
146aa 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
146ab 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0d 0a 20 20  r *zBufOut){..  
146ac 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
146ad 28 70 56 66 73 29 3b 0d 0a 20 20 67 65 74 4c 61  (pVfs);..  getLa
146ae 73 74 45 72 72 6f 72 4d 73 67 28 6f 73 47 65 74  stErrorMsg(osGet
146af 4c 61 73 74 45 72 72 6f 72 28 29 2c 20 6e 42 75  LastError(), nBu
146b0 66 2c 20 7a 42 75 66 4f 75 74 29 3b 0d 0a 7d 0d  f, zBufOut);..}.
146b1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a 77  .static void (*w
146b2 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  inDlSym(sqlite3_
146b3 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
146b4 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20  *pHandle, const 
146b5 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28  char *zSymbol))(
146b6 76 6f 69 64 29 7b 0d 0a 20 20 55 4e 55 53 45 44  void){..  UNUSED
146b7 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
146b8 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  ;..  return (voi
146b9 64 28 2a 29 28 76 6f 69 64 29 29 6f 73 47 65 74  d(*)(void))osGet
146ba 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41  ProcAddressA((HA
146bb 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53  NDLE)pHandle, zS
146bc 79 6d 62 6f 6c 29 3b 0d 0a 7d 0d 0a 73 74 61 74  ymbol);..}..stat
146bd 69 63 20 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f  ic void winDlClo
146be 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
146bf 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
146c0 64 6c 65 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f  dle){..  UNUSED_
146c1 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
146c2 0d 0a 20 20 6f 73 46 72 65 65 4c 69 62 72 61 72  ..  osFreeLibrar
146c3 79 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  y((HANDLE)pHandl
146c4 65 29 3b 0d 0a 7d 0d 0a 23 65 6c 73 65 20 2f 2a  e);..}..#else /*
146c5 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
146c6 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69  LOAD_EXTENSION i
146c7 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0d 0a 20  s defined: */.. 
146c8 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70   #define winDlOp
146c9 65 6e 20 20 30 0d 0a 20 20 23 64 65 66 69 6e 65  en  0..  #define
146ca 20 77 69 6e 44 6c 45 72 72 6f 72 20 30 0d 0a 20   winDlError 0.. 
146cb 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79   #define winDlSy
146cc 6d 20 20 20 30 0d 0a 20 20 23 64 65 66 69 6e 65  m   0..  #define
146cd 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30 0d 0a 23   winDlClose 0..#
146ce 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  endif....../*..*
146cf 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42  * Write up to nB
146d0 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64  uf bytes of rand
146d1 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66  omness into zBuf
146d2 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
146d3 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28  t winRandomness(
146d4 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
146d5 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
146d6 72 20 2a 7a 42 75 66 29 7b 0d 0a 20 20 69 6e 74  r *zBuf){..  int
146d7 20 6e 20 3d 20 30 3b 0d 0a 20 20 55 4e 55 53 45   n = 0;..  UNUSE
146d8 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
146d9 29 3b 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  );..#if defined(
146da 53 51 4c 49 54 45 5f 54 45 53 54 29 0d 0a 20 20  SQLITE_TEST)..  
146db 6e 20 3d 20 6e 42 75 66 3b 0d 0a 20 20 6d 65 6d  n = nBuf;..  mem
146dc 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75  set(zBuf, 0, nBu
146dd 66 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 69 66  f);..#else..  if
146de 28 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54  ( sizeof(SYSTEMT
146df 49 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0d  IME)<=nBuf-n ){.
146e0 0a 20 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20  .    SYSTEMTIME 
146e1 78 3b 0d 0a 20 20 20 20 6f 73 47 65 74 53 79 73  x;..    osGetSys
146e2 74 65 6d 54 69 6d 65 28 26 78 29 3b 0d 0a 20 20  temTime(&x);..  
146e3 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
146e4 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  ], &x, sizeof(x)
146e5 29 3b 0d 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  );..    n += siz
146e6 65 6f 66 28 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20  eof(x);..  }..  
146e7 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44  if( sizeof(DWORD
146e8 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0d 0a 20 20  )<=nBuf-n ){..  
146e9 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 6f 73    DWORD pid = os
146ea 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73  GetCurrentProces
146eb 73 49 64 28 29 3b 0d 0a 20 20 20 20 6d 65 6d 63  sId();..    memc
146ec 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69  py(&zBuf[n], &pi
146ed 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b  d, sizeof(pid));
146ee 0d 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ..    n += sizeo
146ef 66 28 70 69 64 29 3b 0d 0a 20 20 7d 0d 0a 20 20  f(pid);..  }..  
146f0 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44  if( sizeof(DWORD
146f1 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0d 0a 20 20  )<=nBuf-n ){..  
146f2 20 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 6f 73    DWORD cnt = os
146f3 47 65 74 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0d  GetTickCount();.
146f4 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
146f5 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65  f[n], &cnt, size
146f6 6f 66 28 63 6e 74 29 29 3b 0d 0a 20 20 20 20 6e  of(cnt));..    n
146f7 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b   += sizeof(cnt);
146f8 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 73 69 7a  ..  }..  if( siz
146f9 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45  eof(LARGE_INTEGE
146fa 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0d 0a 20  R)<=nBuf-n ){.. 
146fb 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52     LARGE_INTEGER
146fc 20 69 3b 0d 0a 20 20 20 20 6f 73 51 75 65 72 79   i;..    osQuery
146fd 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74  PerformanceCount
146fe 65 72 28 26 69 29 3b 0d 0a 20 20 20 20 6d 65 6d  er(&i);..    mem
146ff 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69  cpy(&zBuf[n], &i
14700 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0d 0a 20  , sizeof(i));.. 
14701 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69     n += sizeof(i
14702 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  );..  }..#endif.
14703 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0d 0a 7d 0d  .  return n;..}.
14704 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 6c 65 65  ...../*..** Slee
14705 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77  p for a little w
14706 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68  hile.  Return th
14707 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65  e amount of time
14708 20 73 6c 65 70 74 2e 0d 0a 2a 2f 0d 0a 73 74 61   slept...*/..sta
14709 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70  tic int winSleep
1470a 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
1470b 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  fs, int microsec
1470c 29 7b 0d 0a 20 20 6f 73 53 6c 65 65 70 28 28 6d  ){..  osSleep((m
1470d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30  icrosec+999)/100
1470e 30 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  0);..  UNUSED_PA
1470f 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0d 0a  RAMETER(pVfs);..
14710 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f    return ((micro
14711 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31  sec+999)/1000)*1
14712 30 30 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  000;..}..../*..*
14713 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
14714 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74  variable, if set
14715 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
14716 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70 72  alue, is interpr
14717 65 74 65 64 20 61 73 0d 0a 2a 2a 20 74 68 65 20  eted as..** the 
14718 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64  number of second
14719 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64  s since 1970 and
1471a 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20   is used to set 
1471b 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0d 0a 2a  the result of..*
1471c 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  * sqlite3OsCurre
1471d 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20  ntTime() during 
1471e 74 65 73 74 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23 69  testing...*/..#i
1471f 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
14720 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
14721 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
14722 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46  _time = 0;  /* F
14723 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20  ake system time 
14724 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65  in seconds since
14725 20 31 39 37 30 2e 20 2a 2f 0d 0a 23 65 6e 64 69   1970. */..#endi
14726 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64  f..../*..** Find
14727 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
14728 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
14729 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
1472a 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a  ).  Write into *
1472b 70 69 4e 6f 77 0d 0a 2a 2a 20 74 68 65 20 63 75  piNow..** the cu
1472c 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64  rrent time and d
1472d 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20  ate as a Julian 
1472e 44 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73  Day number times
1472f 20 38 36 5f 34 30 30 5f 30 30 30 2e 20 20 49 6e   86_400_000.  In
14730 0d 0a 2a 2a 20 6f 74 68 65 72 20 77 6f 72 64 73  ..** other words
14731 2c 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 70 69  , write into *pi
14732 4e 6f 77 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Now the number o
14733 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73  f milliseconds s
14734 69 6e 63 65 20 74 68 65 20 4a 75 6c 69 61 6e 0d  ince the Julian.
14735 0a 2a 2a 20 65 70 6f 63 68 20 6f 66 20 6e 6f 6f  .** epoch of noo
14736 6e 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f  n in Greenwich o
14737 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34  n November 24, 4
14738 37 31 34 20 42 2e 43 20 61 63 63 6f 72 64 69 6e  714 B.C accordin
14739 67 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 72 6f  g to the..** pro
1473a 6c 65 70 74 69 63 20 47 72 65 67 6f 72 69 61 6e  leptic Gregorian
1473b 20 63 61 6c 65 6e 64 61 72 2e 0d 0a 2a 2a 0d 0a   calendar...**..
1473c 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 72  ** On success, r
1473d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
1473e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
1473f 45 52 52 4f 52 20 69 66 20 74 68 65 20 74 69 6d  ERROR if the tim
14740 65 20 61 6e 64 20 64 61 74 65 20 0d 0a 2a 2a 20  e and date ..** 
14741 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e  cannot be found.
14742 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
14743 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 49   winCurrentTimeI
14744 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 66 73  nt64(sqlite3_vfs
14745 20 2a 70 56 66 73 2c 20 73 71 6c 69 74 65 33 5f   *pVfs, sqlite3_
14746 69 6e 74 36 34 20 2a 70 69 4e 6f 77 29 7b 0d 0a  int64 *piNow){..
14747 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73 74    /* FILETIME st
14748 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34 2d  ructure is a 64-
14749 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65 73  bit value repres
1474a 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 65  enting the numbe
1474b 72 20 6f 66 20 0d 0a 20 20 20 20 20 31 30 30 2d  r of ..     100-
1474c 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72  nanosecond inter
1474d 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61  vals since Janua
1474e 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44  ry 1, 1601 (= JD
1474f 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0d 0a 20   2305813.5). .. 
14750 20 2a 2f 0d 0a 20 20 46 49 4c 45 54 49 4d 45 20   */..  FILETIME 
14751 66 74 3b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  ft;..  static co
14752 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nst sqlite3_int6
14753 34 20 77 69 6e 46 69 6c 65 74 69 6d 65 45 70 6f  4 winFiletimeEpo
14754 63 68 20 3d 20 32 33 30 35 38 31 33 35 2a 28 73  ch = 23058135*(s
14755 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34  qlite3_int64)864
14756 30 30 30 30 3b 0d 0a 23 69 66 64 65 66 20 53 51  0000;..#ifdef SQ
14757 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 73 74 61  LITE_TEST..  sta
14758 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
14759 33 5f 69 6e 74 36 34 20 75 6e 69 78 45 70 6f 63  3_int64 unixEpoc
1475a 68 20 3d 20 32 34 34 30 35 38 37 35 2a 28 73 71  h = 24405875*(sq
1475b 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30  lite3_int64)8640
1475c 30 30 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  000;..#endif..  
1475d 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 6f  /* 2^32 - to avo
1475e 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e 64  id use of LL and
1475f 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 63   warnings in gcc
14760 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 63 6f   */..  static co
14761 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nst sqlite3_int6
14762 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20  4 max32BitValue 
14763 3d 20 0d 0a 20 20 20 20 20 20 28 73 71 6c 69 74  = ..      (sqlit
14764 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30  e3_int64)2000000
14765 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69  000 + (sqlite3_i
14766 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 30 20  nt64)2000000000 
14767 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  + (sqlite3_int64
14768 29 32 39 34 39 36 37 32 39 36 3b 0d 0a 0d 0a 23  )294967296;....#
14769 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
1476a 43 45 0d 0a 20 20 53 59 53 54 45 4d 54 49 4d 45  CE..  SYSTEMTIME
1476b 20 74 69 6d 65 3b 0d 0a 20 20 6f 73 47 65 74 53   time;..  osGetS
1476c 79 73 74 65 6d 54 69 6d 65 28 26 74 69 6d 65 29  ystemTime(&time)
1476d 3b 0d 0a 20 20 2f 2a 20 69 66 20 53 79 73 74 65  ;..  /* if Syste
1476e 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28  mTimeToFileTime(
1476f 29 20 66 61 69 6c 73 2c 20 69 74 20 72 65 74 75  ) fails, it retu
14770 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0d 0a 20 20  rns zero. */..  
14771 69 66 20 28 21 6f 73 53 79 73 74 65 6d 54 69 6d  if (!osSystemTim
14772 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d  eToFileTime(&tim
14773 65 2c 26 66 74 29 29 7b 0d 0a 20 20 20 20 72 65  e,&ft)){..    re
14774 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
14775 52 3b 0d 0a 20 20 7d 0d 0a 23 65 6c 73 65 0d 0a  R;..  }..#else..
14776 20 20 6f 73 47 65 74 53 79 73 74 65 6d 54 69 6d    osGetSystemTim
14777 65 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74  eAsFileTime( &ft
14778 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20   );..#endif.... 
14779 20 2a 70 69 4e 6f 77 20 3d 20 77 69 6e 46 69 6c   *piNow = winFil
1477a 65 74 69 6d 65 45 70 6f 63 68 20 2b 0d 0a 20 20  etimeEpoch +..  
1477b 20 20 20 20 20 20 20 20 20 20 28 28 28 28 73 71            ((((sq
1477c 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64  lite3_int64)ft.d
1477d 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 2a 6d  wHighDateTime)*m
1477e 61 78 33 32 42 69 74 56 61 6c 75 65 29 20 2b 20  ax32BitValue) + 
1477f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
14780 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
14781 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65  ft.dwLowDateTime
14782 29 2f 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  )/(sqlite3_int64
14783 29 31 30 30 30 30 3b 0d 0a 0d 0a 23 69 66 64 65  )10000;....#ifde
14784 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20  f SQLITE_TEST.. 
14785 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
14786 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0d 0a 20 20  rent_time ){..  
14787 20 20 2a 70 69 4e 6f 77 20 3d 20 31 30 30 30 2a    *piNow = 1000*
14788 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 73  (sqlite3_int64)s
14789 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
1478a 69 6d 65 20 2b 20 75 6e 69 78 45 70 6f 63 68 3b  ime + unixEpoch;
1478b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20  ..  }..#endif.. 
1478c 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1478d 52 28 70 56 66 73 29 3b 0d 0a 20 20 72 65 74 75  R(pVfs);..  retu
1478e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
1478f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20  ..../*..** Find 
14790 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65  the current time
14791 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43   (in Universal C
14792 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29  oordinated Time)
14793 2e 20 20 57 72 69 74 65 20 74 68 65 0d 0a 2a 2a  .  Write the..**
14794 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
14795 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
14796 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
14797 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0d 0a 2a  to *prNow and..*
14798 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74  * return 0.  Ret
14799 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d  urn 1 if the tim
1479a 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f  e and date canno
1479b 74 20 62 65 20 66 6f 75 6e 64 2e 0d 0a 2a 2f 0d  t be found...*/.
1479c 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43  .static int winC
1479d 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
1479e 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
1479f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0d 0a 20  uble *prNow){.. 
147a0 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73 71 6c 69   int rc;..  sqli
147a1 74 65 33 5f 69 6e 74 36 34 20 69 3b 0d 0a 20 20  te3_int64 i;..  
147a2 72 63 20 3d 20 77 69 6e 43 75 72 72 65 6e 74 54  rc = winCurrentT
147a3 69 6d 65 49 6e 74 36 34 28 70 56 66 73 2c 20 26  imeInt64(pVfs, &
147a4 69 29 3b 0d 0a 20 20 69 66 28 20 21 72 63 20 29  i);..  if( !rc )
147a5 7b 0d 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  {..    *prNow = 
147a6 69 2f 38 36 34 30 30 30 30 30 2e 30 3b 0d 0a 20  i/86400000.0;.. 
147a7 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
147a8 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
147a9 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20 74  e idea is that t
147aa 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72  his function wor
147ab 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e  ks like a combin
147ac 61 74 69 6f 6e 20 6f 66 0d 0a 2a 2a 20 47 65 74  ation of..** Get
147ad 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20  LastError() and 
147ae 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20  FormatMessage() 
147af 6f 6e 20 57 69 6e 64 6f 77 73 20 28 6f 72 20 65  on Windows (or e
147b0 72 72 6e 6f 20 61 6e 64 0d 0a 2a 2a 20 73 74 72  rrno and..** str
147b1 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 55 6e 69  error_r() on Uni
147b2 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72  x). After an err
147b3 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  or is returned b
147b4 79 20 61 6e 20 4f 53 0d 0a 2a 2a 20 66 75 6e 63  y an OS..** func
147b5 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c  tion, SQLite cal
147b6 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ls this function
147b7 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74   with zBuf point
147b8 69 6e 67 20 74 6f 0d 0a 2a 2a 20 61 20 62 75 66  ing to..** a buf
147b9 66 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65  fer of nBuf byte
147ba 73 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20  s. The OS layer 
147bb 73 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20  should populate 
147bc 74 68 65 0d 0a 2a 2a 20 62 75 66 66 65 72 20 77  the..** buffer w
147bd 69 74 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e  ith a nul-termin
147be 61 74 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64  ated UTF-8 encod
147bf 65 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ed error message
147c0 0d 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20  ..** describing 
147c1 74 68 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f  the last IO erro
147c2 72 20 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72  r to have occurr
147c3 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 61  ed within the ca
147c4 6c 6c 69 6e 67 0d 0a 2a 2a 20 74 68 72 65 61 64  lling..** thread
147c5 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
147c6 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
147c7 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20  s too large for 
147c8 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66  the supplied buf
147c9 66 65 72 2c 0d 0a 2a 2a 20 69 74 20 73 68 6f 75  fer,..** it shou
147ca 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e  ld be truncated.
147cb 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
147cc 65 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72  e of xGetLastErr
147cd 6f 72 0d 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69  or..** is zero i
147ce 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
147cf 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20  age fits in the 
147d0 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a  buffer, or non-z
147d1 65 72 6f 0d 0a 2a 2a 20 6f 74 68 65 72 77 69 73  ero..** otherwis
147d2 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 67  e (if the messag
147d3 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 29  e was truncated)
147d4 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73  . If non-zero is
147d5 20 72 65 74 75 72 6e 65 64 2c 0d 0a 2a 2a 20 74   returned,..** t
147d6 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  hen it is not ne
147d7 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75  cessary to inclu
147d8 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69  de the nul-termi
147d9 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72 0d  nator character.
147da 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75  .** in the outpu
147db 74 20 62 75 66 66 65 72 2e 0d 0a 2a 2a 0d 0a 2a  t buffer...**..*
147dc 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20  * Not supplying 
147dd 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
147de 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64   will have no ad
147df 76 65 72 73 65 20 65 66 66 65 63 74 0d 0a 2a 2a  verse effect..**
147e0 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69   on SQLite. It i
147e1 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61  s fine to have a
147e2 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
147e3 20 74 68 61 74 20 6e 65 76 65 72 0d 0a 2a 2a 20   that never..** 
147e4 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
147e5 20 6d 65 73 73 61 67 65 3a 0d 0a 2a 2a 0d 0a 2a   message:..**..*
147e6 2a 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74  *   int xGetLast
147e7 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
147e8 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
147e9 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0d  f, char *zBuf){.
147ea 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a  .**     assert(z
147eb 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0d 0a  Buf[0]=='\0');..
147ec 2a 2a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  **     return 0;
147ed 0d 0a 2a 2a 20 20 20 7d 0d 0a 2a 2a 0d 0a 2a 2a  ..**   }..**..**
147ee 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65   However if an e
147ef 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
147f0 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c  supplied, it wil
147f1 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65  l be incorporate
147f2 64 0d 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20  d..** by sqlite 
147f3 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d  into the error m
147f4 65 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65  essage available
147f5 20 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 69   to the user usi
147f6 6e 67 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65  ng..** sqlite3_e
147f7 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c  rrmsg(), possibl
147f8 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f  y making IO erro
147f9 72 73 20 65 61 73 69 65 72 20 74 6f 20 64 65 62  rs easier to deb
147fa 75 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ug...*/..static 
147fb 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45 72  int winGetLastEr
147fc 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
147fd 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
147fe 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0d 0a 20   char *zBuf){.. 
147ff 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
14800 52 28 70 56 66 73 29 3b 0d 0a 20 20 72 65 74 75  R(pVfs);..  retu
14801 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  rn getLastErrorM
14802 73 67 28 6f 73 47 65 74 4c 61 73 74 45 72 72 6f  sg(osGetLastErro
14803 72 28 29 2c 20 6e 42 75 66 2c 20 7a 42 75 66 29  r(), nBuf, zBuf)
14804 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
14805 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65  nitialize and de
14806 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f  initialize the o
14807 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
14808 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2f 0d 0a  interface...*/..
14809 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1480a 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76  qlite3_os_init(v
1480b 6f 69 64 29 7b 0d 0a 20 20 73 74 61 74 69 63 20  oid){..  static 
1480c 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69 6e 56  sqlite3_vfs winV
1480d 66 73 20 3d 20 7b 0d 0a 20 20 20 20 33 2c 20 20  fs = {..    3,  
1480e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1480f 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0d   /* iVersion */.
14810 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69 6e 46  .    sizeof(winF
14811 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a 4f  ile),     /* szO
14812 73 46 69 6c 65 20 2a 2f 0d 0a 20 20 20 20 4d 41  sFile */..    MA
14813 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20  X_PATH,         
14814 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65     /* mxPathname
14815 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20   */..    0,     
14816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14817 20 70 4e 65 78 74 20 2a 2f 0d 0a 20 20 20 20 22   pNext */..    "
14818 77 69 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20  win32",         
14819 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0d      /* zName */.
1481a 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
1481b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70            /* pAp
1481c 70 44 61 74 61 20 2a 2f 0d 0a 20 20 20 20 77 69  pData */..    wi
1481d 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  nOpen,          
1481e 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0d 0a     /* xOpen */..
1481f 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20      winDelete,  
14820 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
14821 65 74 65 20 2a 2f 0d 0a 20 20 20 20 77 69 6e 41  ete */..    winA
14822 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 20  ccess,          
14823 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0d 0a   /* xAccess */..
14824 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e      winFullPathn
14825 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c  ame,     /* xFul
14826 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0d 0a 20 20  lPathname */..  
14827 20 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20    winDlOpen,    
14828 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65         /* xDlOpe
14829 6e 20 2a 2f 0d 0a 20 20 20 20 77 69 6e 44 6c 45  n */..    winDlE
1482a 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f  rror,          /
1482b 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0d 0a 20  * xDlError */.. 
1482c 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20     winDlSym,    
1482d 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79          /* xDlSy
1482e 6d 20 2a 2f 0d 0a 20 20 20 20 77 69 6e 44 6c 43  m */..    winDlC
1482f 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  lose,          /
14830 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0d 0a 20  * xDlClose */.. 
14831 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73     winRandomness
14832 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ,       /* xRand
14833 6f 6d 6e 65 73 73 20 2a 2f 0d 0a 20 20 20 20 77  omness */..    w
14834 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20  inSleep,        
14835 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f      /* xSleep */
14836 0d 0a 20 20 20 20 77 69 6e 43 75 72 72 65 6e 74  ..    winCurrent
14837 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 78 43  Time,      /* xC
14838 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0d 0a 20  urrentTime */.. 
14839 20 20 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72     winGetLastErr
1483a 6f 72 2c 20 20 20 20 20 2f 2a 20 78 47 65 74 4c  or,     /* xGetL
1483b 61 73 74 45 72 72 6f 72 20 2a 2f 0d 0a 20 20 20  astError */..   
1483c 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 49   winCurrentTimeI
1483d 6e 74 36 34 2c 20 2f 2a 20 78 43 75 72 72 65 6e  nt64, /* xCurren
1483e 74 54 69 6d 65 49 6e 74 36 34 20 2a 2f 0d 0a 20  tTimeInt64 */.. 
1483f 20 20 20 77 69 6e 53 65 74 53 79 73 74 65 6d 43     winSetSystemC
14840 61 6c 6c 2c 20 20 20 20 2f 2a 20 78 53 65 74 53  all,    /* xSetS
14841 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0d 0a 20 20  ystemCall */..  
14842 20 20 77 69 6e 47 65 74 53 79 73 74 65 6d 43 61    winGetSystemCa
14843 6c 6c 2c 20 20 20 20 2f 2a 20 78 47 65 74 53 79  ll,    /* xGetSy
14844 73 74 65 6d 43 61 6c 6c 20 2a 2f 0d 0a 20 20 20  stemCall */..   
14845 20 77 69 6e 4e 65 78 74 53 79 73 74 65 6d 43 61   winNextSystemCa
14846 6c 6c 2c 20 20 20 2f 2a 20 78 4e 65 78 74 53 79  ll,   /* xNextSy
14847 73 74 65 6d 43 61 6c 6c 20 2a 2f 0d 0a 20 20 7d  stemCall */..  }
14848 3b 0d 0a 0d 0a 20 20 2f 2a 20 44 6f 75 62 6c 65  ;....  /* Double
14849 2d 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20  -check that the 
1484a 61 53 79 73 63 61 6c 6c 5b 5d 20 61 72 72 61 79  aSyscall[] array
1484b 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 74 72   has been constr
1484c 75 63 74 65 64 0d 0a 20 20 2a 2a 20 63 6f 72 72  ucted..  ** corr
1484d 65 63 74 6c 79 2e 20 20 53 65 65 20 74 69 63 6b  ectly.  See tick
1484e 65 74 20 5b 62 62 33 61 38 36 65 38 39 30 63 38  et [bb3a86e890c8
1484f 65 39 36 61 62 5d 20 2a 2f 0d 0a 20 20 61 73 73  e96ab] */..  ass
14850 65 72 74 28 20 41 72 72 61 79 53 69 7a 65 28 61  ert( ArraySize(a
14851 53 79 73 63 61 6c 6c 29 3d 3d 36 30 20 29 3b 0d  Syscall)==60 );.
14852 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
14853 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 2f 2a  E_OMIT_WAL..  /*
14854 20 67 65 74 20 6d 65 6d 6f 72 79 20 6d 61 70 20   get memory map 
14855 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 72 61 6e 75  allocation granu
14856 6c 61 72 69 74 79 20 2a 2f 0d 0a 20 20 6d 65 6d  larity */..  mem
14857 73 65 74 28 26 77 69 6e 53 79 73 49 6e 66 6f 2c  set(&winSysInfo,
14858 20 30 2c 20 73 69 7a 65 6f 66 28 53 59 53 54 45   0, sizeof(SYSTE
14859 4d 5f 49 4e 46 4f 29 29 3b 0d 0a 20 20 6f 73 47  M_INFO));..  osG
1485a 65 74 53 79 73 74 65 6d 49 6e 66 6f 28 26 77 69  etSystemInfo(&wi
1485b 6e 53 79 73 49 6e 66 6f 29 3b 0d 0a 20 20 61 73  nSysInfo);..  as
1485c 73 65 72 74 28 77 69 6e 53 79 73 49 6e 66 6f 2e  sert(winSysInfo.
1485d 64 77 41 6c 6c 6f 63 61 74 69 6f 6e 47 72 61 6e  dwAllocationGran
1485e 75 6c 61 72 69 74 79 20 3e 20 30 29 3b 0d 0a 23  ularity > 0);..#
1485f 65 6e 64 69 66 0d 0a 0d 0a 20 20 73 71 6c 69 74  endif....  sqlit
14860 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
14861 26 77 69 6e 56 66 73 2c 20 31 29 3b 0d 0a 20 20  &winVfs, 1);..  
14862 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
14863 3b 20 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f  ; ..}....SQLITE_
14864 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
14865 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0d 0a  os_end(void){ ..
14866 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14867 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66  OK;..}....#endif
14868 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   /* SQLITE_OS_WI
14869 4e 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  N */..../*******
1486a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
1486b 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
1486c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1486d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1486e 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
1486f 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
14870 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a  ile bitvec.c ***
14871 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14873 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  *******/../*..**
14874 20 32 30 30 38 20 46 65 62 72 75 61 72 79 20 31   2008 February 1
14875 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  6..**..** The au
14876 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
14877 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
14878 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
14879 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
1487a 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1487b 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1487c 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
1487d 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1487e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
1487f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
14880 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
14881 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
14882 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
14883 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
14884 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
14885 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
14886 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
14887 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14888 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14889 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1488a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1488b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
1488c 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
1488d 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 68 61  ts an object tha
1488e 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 66  t represents a f
1488f 69 78 65 64 2d 6c 65 6e 67 74 68 0d 0a 2a 2a 20  ixed-length..** 
14890 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 61 72  bitmap.  Bits ar
14891 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74  e numbered start
14892 69 6e 67 20 77 69 74 68 20 31 2e 0d 0a 2a 2a 0d  ing with 1...**.
14893 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20  .** A bitmap is 
14894 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
14895 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20  hich pages of a 
14896 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
14897 76 65 20 62 65 65 6e 0d 0a 2a 2a 20 6a 6f 75 72  ve been..** jour
14898 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20  nalled during a 
14899 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
1489a 77 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65  which pages have
1489b 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65   the "dont-write
1489c 22 0d 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20  "..** property. 
1489d 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20   Usually only a 
1489e 66 65 77 20 70 61 67 65 73 20 61 72 65 20 6d 65  few pages are me
1489f 65 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74  et either condit
148a0 69 6f 6e 2e 0d 0a 2a 2a 20 53 6f 20 74 68 65 20  ion...** So the 
148a1 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c  bitmap is usuall
148a2 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 61 73  y sparse and has
148a3 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79   low cardinality
148a4 2e 0d 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69  ...** But someti
148a5 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  mes (for example
148a6 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 44   when during a D
148a7 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 74  ROP of a large t
148a8 61 62 6c 65 29 20 6d 6f 73 74 0d 0a 2a 2a 20 6f  able) most..** o
148a9 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67  r all of the pag
148aa 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
148ab 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c   can get journal
148ac 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63  led.  In those c
148ad 61 73 65 73 2c 20 0d 0a 2a 2a 20 74 68 65 20 62  ases, ..** the b
148ae 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65  itmap becomes de
148af 6e 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61  nse with high ca
148b0 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20  rdinality.  The 
148b1 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20  algorithm needs 
148b2 0d 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62  ..** to handle b
148b3 6f 74 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0d  oth cases well..
148b4 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 69 7a 65  .**..** The size
148b5 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 69   of the bitmap i
148b6 73 20 66 69 78 65 64 20 77 68 65 6e 20 74 68 65  s fixed when the
148b7 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
148b8 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20  ed...**..** All 
148b9 62 69 74 73 20 61 72 65 20 63 6c 65 61 72 20 77  bits are clear w
148ba 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70 20 69  hen the bitmap i
148bb 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e 64 69  s created.  Indi
148bc 76 69 64 75 61 6c 20 62 69 74 73 0d 0a 2a 2a 20  vidual bits..** 
148bd 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63 6c  may be set or cl
148be 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20 74  eared one at a t
148bf 69 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 65 73  ime...**..** Tes
148c0 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  t operations are
148c1 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73   about 100 times
148c2 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61   more common tha
148c3 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73  t set operations
148c4 2e 0d 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72  ...** Clear oper
148c5 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65  ations are excee
148c6 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68  dingly rare.  Th
148c7 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20  ere are usually 
148c8 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 35 20 61 6e  between..** 5 an
148c9 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74  d 500 set operat
148ca 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20  ions per Bitvec 
148cb 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74  object, though t
148cc 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74  he number of set
148cd 73 20 63 61 6e 0d 0a 2a 2a 20 73 6f 6d 65 74 69  s can..** someti
148ce 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65  mes grow into te
148cf 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20  ns of thousands 
148d0 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20  or larger.  The 
148d1 73 69 7a 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20  size of the..** 
148d2 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73  Bitvec object is
148d3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
148d4 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
148d5 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65  base file at the
148d6 0d 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20  ..** start of a 
148d7 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
148d8 20 69 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79   is thus usually
148d9 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 65 77   less than a few
148da 20 74 68 6f 75 73 61 6e 64 2c 0d 0a 2a 2a 20 62   thousand,..** b
148db 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 72  ut can be as lar
148dc 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20  ge as 2 billion 
148dd 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 67  for a really big
148de 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a   database...*/..
148df 0d 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65  ../* Size of the
148e0 20 42 69 74 76 65 63 20 73 74 72 75 63 74 75 72   Bitvec structur
148e1 65 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0d 0a  e in bytes. */..
148e2 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53  #define BITVEC_S
148e3 5a 20 20 20 20 20 20 20 20 35 31 32 0d 0a 0d 0a  Z        512....
148e4 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69  /* Round the uni
148e5 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20  on size down to 
148e6 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e  the nearest poin
148e7 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69  ter boundary, si
148e8 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0d  nce that's how .
148e9 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61  .** it will be a
148ea 6c 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68  ligned within th
148eb 65 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e  e Bitvec struct.
148ec 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 49 54   */..#define BIT
148ed 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28  VEC_USIZE     ((
148ee 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69  (BITVEC_SZ-(3*si
148ef 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65  zeof(u32)))/size
148f0 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a  of(Bitvec*))*siz
148f1 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0d 0a 0d  eof(Bitvec*))...
148f2 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20  ./* Type of the 
148f3 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20  array "element" 
148f4 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72  for the bitmap r
148f5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0d  epresentation. .
148f6 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20  .** Should be a 
148f7 70 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20  power of 2, and 
148f8 69 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20  ideally, evenly 
148f9 64 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56  divide into BITV
148fa 45 43 5f 55 53 49 5a 45 2e 20 0d 0a 2a 2a 20 53  EC_USIZE. ..** S
148fb 65 74 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74  etting this to t
148fc 68 65 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64  he "natural word
148fd 22 20 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43  " size of your C
148fe 50 55 20 6d 61 79 20 69 6d 70 72 6f 76 65 0d 0a  PU may improve..
148ff 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  ** performance. 
14900 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 49 54 56  */..#define BITV
14901 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38 0d  EC_TELEM     u8.
14902 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74  ./* Size, in bit
14903 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70  s, of the bitmap
14904 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 23 64   element. */..#d
14905 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45  efine BITVEC_SZE
14906 4c 45 4d 20 20 20 20 38 0d 0a 2f 2a 20 4e 75 6d  LEM    8../* Num
14907 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
14908 69 6e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61  in a bitmap arra
14909 79 2e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42  y. */..#define B
1490a 49 54 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20  ITVEC_NELEM     
1490b 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69  (BITVEC_USIZE/si
1490c 7a 65 6f 66 28 42 49 54 56 45 43 5f 54 45 4c 45  zeof(BITVEC_TELE
1490d 4d 29 29 0d 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f  M))../* Number o
1490e 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69  f bits in the bi
1490f 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0d 0a  tmap array. */..
14910 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
14911 42 49 54 20 20 20 20 20 20 28 42 49 54 56 45 43  BIT      (BITVEC
14912 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f 53 5a  _NELEM*BITVEC_SZ
14913 45 4c 45 4d 29 0d 0a 0d 0a 2f 2a 20 4e 75 6d 62  ELEM)..../* Numb
14914 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73  er of u32 values
14915 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20   in hash table. 
14916 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 49 54 56  */..#define BITV
14917 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49  EC_NINT      (BI
14918 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
14919 66 28 75 33 32 29 29 0d 0a 2f 2a 20 4d 61 78 69  f(u32))../* Maxi
1491a 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  mum number of en
1491b 74 72 69 65 73 20 69 6e 20 68 61 73 68 20 74 61  tries in hash ta
1491c 62 6c 65 20 62 65 66 6f 72 65 20 0d 0a 2a 2a 20  ble before ..** 
1491d 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64  sub-dividing and
1491e 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0d   re-hashing. */.
1491f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
14920 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45  MXHASH    (BITVE
14921 43 5f 4e 49 4e 54 2f 32 29 0d 0a 2f 2a 20 48 61  C_NINT/2)../* Ha
14922 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66  shing function f
14923 6f 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70  or the aHash rep
14924 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0d 0a 2a 2a  resentation...**
14925 20 45 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69   Empirical testi
14926 6e 67 20 73 68 6f 77 65 64 20 74 68 61 74 20 74  ng showed that t
14927 68 65 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65  he *37 multiplie
14928 72 20 0d 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74  r ..** (an arbit
14929 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20 74 68  rary prime)in th
1492a 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
1492b 70 72 6f 76 69 64 65 64 20 0d 0a 2a 2a 20 6e 6f  provided ..** no
1492c 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f 6e   fewer collision
1492d 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f 70  s than the no-op
1492e 20 2a 31 2e 20 2a 2f 0d 0a 23 64 65 66 69 6e 65   *1. */..#define
1492f 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 20   BITVEC_HASH(X) 
14930 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 45    (((X)*1)%BITVE
14931 43 5f 4e 49 4e 54 29 0d 0a 0d 0a 23 64 65 66 69  C_NINT)....#defi
14932 6e 65 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20  ne BITVEC_NPTR  
14933 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a      (BITVEC_USIZ
14934 45 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20  E/sizeof(Bitvec 
14935 2a 29 29 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  *))....../*..** 
14936 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 69  A bitmap is an i
14937 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
14938 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
14939 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  re...**..** This
1493a 20 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20   bitmap records 
1493b 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
1493c 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69   zero or more bi
1493d 74 73 0d 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75  ts..** with valu
1493e 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  es between 1 and
1493f 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76   iSize, inclusiv
14940 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65  e...**..** There
14941 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69   are three possi
14942 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ble representati
14943 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ons of the bitma
14944 70 2e 0d 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c  p...** If iSize<
14945 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68  =BITVEC_NBIT, th
14946 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74  en Bitvec.u.aBit
14947 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69  map[] is a strai
14948 67 68 74 0d 0a 2a 2a 20 62 69 74 6d 61 70 2e 20  ght..** bitmap. 
14949 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69   The least signi
1494a 66 69 63 61 6e 74 20 62 69 74 20 69 73 20 62 69  ficant bit is bi
1494b 74 20 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  t 1...**..** If 
1494c 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49  iSize>BITVEC_NBI
1494d 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d  T and iDivisor==
1494e 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e  0 then Bitvec.u.
1494f 61 48 61 73 68 5b 5d 20 69 73 0d 0a 2a 2a 20 61  aHash[] is..** a
14950 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74   hash table that
14951 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f   will hold up to
14952 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64   BITVEC_MXHASH d
14953 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0d  istinct values..
14954 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  .**..** Otherwis
14955 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69 20 69  e, the value i i
14956 73 20 72 65 64 69 72 65 63 74 65 64 20 69 6e 74  s redirected int
14957 6f 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f  o one of BITVEC_
14958 4e 50 54 52 0d 0a 2a 2a 20 73 75 62 2d 62 69 74  NPTR..** sub-bit
14959 6d 61 70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  maps pointed to 
1495a 62 79 20 42 69 74 76 65 63 2e 75 2e 61 70 53 75  by Bitvec.u.apSu
1495b 62 5b 5d 2e 20 20 45 61 63 68 20 73 75 62 62 69  b[].  Each subbi
1495c 74 6d 61 70 0d 0a 2a 2a 20 68 61 6e 64 6c 65 73  tmap..** handles
1495d 20 75 70 20 74 6f 20 69 44 69 76 69 73 6f 72 20   up to iDivisor 
1495e 73 65 70 61 72 61 74 65 20 76 61 6c 75 65 73 20  separate values 
1495f 6f 66 20 69 2e 20 20 61 70 53 75 62 5b 30 5d 20  of i.  apSub[0] 
14960 68 6f 6c 64 73 0d 0a 2a 2a 20 76 61 6c 75 65 73  holds..** values
14961 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69   between 1 and i
14962 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b  Divisor.  apSub[
14963 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20  1] holds values 
14964 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 69 44 69 76  between..** iDiv
14965 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 69  isor+1 and 2*iDi
14966 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e 5d  visor.  apSub[N]
14967 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65   holds values be
14968 74 77 65 65 6e 0d 0a 2a 2a 20 4e 2a 69 44 69 76  tween..** N*iDiv
14969 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29  isor+1 and (N+1)
1496a 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68  *iDivisor.  Each
1496b 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f   subbitmap is no
1496c 72 6d 61 6c 69 7a 65 64 0d 0a 2a 2a 20 74 6f 20  rmalized..** to 
1496d 68 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76  hold deal with v
1496e 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20  alues between 1 
1496f 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 0d 0a 2a  and iDivisor...*
14970 2f 0d 0a 73 74 72 75 63 74 20 42 69 74 76 65 63  /..struct Bitvec
14971 20 7b 0d 0a 20 20 75 33 32 20 69 53 69 7a 65 3b   {..  u32 iSize;
14972 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
14973 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78   bit index.  Max
14974 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c   iSize is 4,294,
14975 39 36 37 2c 32 39 36 2e 20 2a 2f 0d 0a 20 20 75  967,296. */..  u
14976 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f  32 nSet;       /
14977 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73  * Number of bits
14978 20 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20   that are set - 
14979 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61  only valid for a
1497a 48 61 73 68 0d 0a 20 20 20 20 20 20 20 20 20 20  Hash..          
1497b 20 20 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65          ** eleme
1497c 6e 74 2e 20 20 4d 61 78 20 69 73 20 42 49 54 56  nt.  Max is BITV
1497d 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49  EC_NINT.  For BI
1497e 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 0d  TVEC_SZ of 512,.
1497f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
14980 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75 6c 64     ** this would
14981 20 62 65 20 31 32 35 2e 20 2a 2f 0d 0a 20 20 75   be 125. */..  u
14982 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20 2f  32 iDivisor;   /
14983 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73  * Number of bits
14984 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63 68   handled by each
14985 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20   apSub[] entry. 
14986 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
14987 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20        /* Should 
14988 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c  >=0 for apSub el
14989 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 20 20 20 20 20  ement. */..     
1498a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1498b 4d 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20  Max iDivisor is 
1498c 6d 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45  max(u32) / BITVE
1498d 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0d  C_NPTR + 1.  */.
1498e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1498f 20 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56     /* For a BITV
14990 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68  EC_SZ of 512, th
14991 69 73 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33  is would be 34,3
14992 35 39 2c 37 33 39 2e 20 2a 2f 0d 0a 20 20 75 6e  59,739. */..  un
14993 69 6f 6e 20 7b 0d 0a 20 20 20 20 42 49 54 56 45  ion {..    BITVE
14994 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b  C_TELEM aBitmap[
14995 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20  BITVEC_NELEM];  
14996 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72    /* Bitmap repr
14997 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0d 0a 20  esentation */.. 
14998 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 49 54     u32 aHash[BIT
14999 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20  VEC_NINT];      
1499a 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72 65  /* Hash table re
1499b 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0d  presentation */.
1499c 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 53  .    Bitvec *apS
1499d 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b  ub[BITVEC_NPTR];
1499e 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72    /* Recursive r
1499f 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
149a0 0d 0a 20 20 7d 20 75 3b 0d 0a 7d 3b 0d 0a 0d 0a  ..  } u;..};....
149a1 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  /*..** Create a 
149a2 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 63  new bitmap objec
149a3 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65  t able to handle
149a4 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 20   bits between 0 
149a5 61 6e 64 20 69 53 69 7a 65 2c 0d 0a 2a 2a 20 69  and iSize,..** i
149a6 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72  nclusive.  Retur
149a7 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
149a8 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20  he new object.  
149a9 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0d  Return NULL if .
149aa 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  .** malloc fails
149ab 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
149ac 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71  IVATE Bitvec *sq
149ad 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
149ae 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0d 0a 20  e(u32 iSize){.. 
149af 20 42 69 74 76 65 63 20 2a 70 3b 0d 0a 20 20 61   Bitvec *p;..  a
149b0 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70  ssert( sizeof(*p
149b1 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0d  )==BITVEC_SZ );.
149b2 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61  .  p = sqlite3Ma
149b3 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
149b4 28 2a 70 29 20 29 3b 0d 0a 20 20 69 66 28 20 70  (*p) );..  if( p
149b5 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 69 53 69 7a   ){..    p->iSiz
149b6 65 20 3d 20 69 53 69 7a 65 3b 0d 0a 20 20 7d 0d  e = iSize;..  }.
149b7 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d  .  return p;..}.
149b8 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20  .../*..** Check 
149b9 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 2d  to see if the i-
149ba 74 68 20 62 69 74 20 69 73 20 73 65 74 2e 20 20  th bit is set.  
149bb 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 20 66  Return true or f
149bc 61 6c 73 65 2e 0d 0a 2a 2a 20 49 66 20 70 20 69  alse...** If p i
149bd 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 62  s NULL (if the b
149be 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 65  itmap has not be
149bf 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 69  en created) or i
149c0 66 0d 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f  f..** i is out o
149c1 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65  f range, then re
149c2 74 75 72 6e 20 66 61 6c 73 65 2e 0d 0a 2a 2f 0d  turn false...*/.
149c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
149c4 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
149c5 63 54 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c  cTest(Bitvec *p,
149c6 20 75 33 32 20 69 29 7b 0d 0a 20 20 69 66 28 20   u32 i){..  if( 
149c7 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
149c8 0d 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69  ..  if( i>p->iSi
149c9 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74  ze || i==0 ) ret
149ca 75 72 6e 20 30 3b 0d 0a 20 20 69 2d 2d 3b 0d 0a  urn 0;..  i--;..
149cb 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76    while( p->iDiv
149cc 69 73 6f 72 20 29 7b 0d 0a 20 20 20 20 75 33 32  isor ){..    u32
149cd 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76   bin = i/p->iDiv
149ce 69 73 6f 72 3b 0d 0a 20 20 20 20 69 20 3d 20 69  isor;..    i = i
149cf 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0d 0a 20  %p->iDivisor;.. 
149d0 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75     p = p->u.apSu
149d1 62 5b 62 69 6e 5d 3b 0d 0a 20 20 20 20 69 66 20  b[bin];..    if 
149d2 28 21 70 29 20 7b 0d 0a 20 20 20 20 20 20 72 65  (!p) {..      re
149d3 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 0;..    }..
149d4 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e 69 53    }..  if( p->iS
149d5 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54  ize<=BITVEC_NBIT
149d6 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
149d7 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f  (p->u.aBitmap[i/
149d8 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26  BITVEC_SZELEM] &
149d9 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 5f   (1<<(i&(BITVEC_
149da 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b  SZELEM-1))))!=0;
149db 0d 0a 20 20 7d 20 65 6c 73 65 7b 0d 0a 20 20 20  ..  } else{..   
149dc 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f   u32 h = BITVEC_
149dd 48 41 53 48 28 69 2b 2b 29 3b 0d 0a 20 20 20 20  HASH(i++);..    
149de 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73  while( p->u.aHas
149df 68 5b 68 5d 20 29 7b 0d 0a 20 20 20 20 20 20 69  h[h] ){..      i
149e0 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  f( p->u.aHash[h]
149e1 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0d  ==i ) return 1;.
149e2 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 29  .      h = (h+1)
149e3 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b 0d   % BITVEC_NINT;.
149e4 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75  .    }..    retu
149e5 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  rn 0;..  }..}...
149e6 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
149e7 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72  i-th bit.  Retur
149e8 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61  n 0 on success a
149e9 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
149ea 20 69 66 0d 0a 2a 2a 20 61 6e 79 74 68 69 6e 67   if..** anything
149eb 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0d 0a 2a 2a   goes wrong...**
149ec 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
149ed 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73 75  e might cause su
149ee 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20  b-bitmaps to be 
149ef 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c  allocated.  Fail
149f0 69 6e 67 0d 0a 2a 2a 20 74 6f 20 67 65 74 20 74  ing..** to get t
149f1 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64  he memory needed
149f2 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62   to hold the sub
149f3 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f  -bitmap is the o
149f4 6e 6c 79 0d 0a 2a 2a 20 74 68 61 74 20 63 61 6e  nly..** that can
149f5 20 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61   go wrong with a
149f6 6e 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69  n insert, assumi
149f7 6e 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76  ng p and i are v
149f8 61 6c 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  alid...**..** Th
149f9 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
149fa 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  on must ensure t
149fb 68 61 74 20 70 20 69 73 20 61 20 76 61 6c 69 64  hat p is a valid
149fc 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 0d 0a   Bitvec object..
149fd 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20  ** and that the 
149fe 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 69 73  value for "i" is
149ff 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 6f 66   within range of
14a00 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65   the Bitvec obje
14a01 63 74 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ct...** Otherwis
14a02 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  e the behavior i
14a03 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f  s undefined...*/
14a04 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
14a05 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
14a06 65 63 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c  ecSet(Bitvec *p,
14a07 20 75 33 32 20 69 29 7b 0d 0a 20 20 75 33 32 20   u32 i){..  u32 
14a08 68 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  h;..  if( p==0 )
14a09 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14a0a 4b 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 3e  K;..  assert( i>
14a0b 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
14a0c 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0d 0a  i<=p->iSize );..
14a0d 20 20 69 2d 2d 3b 0d 0a 20 20 77 68 69 6c 65 28    i--;..  while(
14a0e 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56  (p->iSize > BITV
14a0f 45 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69  EC_NBIT) && p->i
14a10 44 69 76 69 73 6f 72 29 20 7b 0d 0a 20 20 20 20  Divisor) {..    
14a11 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69  u32 bin = i/p->i
14a12 44 69 76 69 73 6f 72 3b 0d 0a 20 20 20 20 69 20  Divisor;..    i 
14a13 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i%p->iDivisor;
14a14 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61  ..    if( p->u.a
14a15 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0d  pSub[bin]==0 ){.
14a16 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75  .      p->u.apSu
14a17 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33  b[bin] = sqlite3
14a18 42 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d  BitvecCreate( p-
14a19 3e 69 44 69 76 69 73 6f 72 20 29 3b 0d 0a 20 20  >iDivisor );..  
14a1a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53      if( p->u.apS
14a1b 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74  ub[bin]==0 ) ret
14a1c 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
14a1d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 20  ;..    }..    p 
14a1e 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
14a1f 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  ];..  }..  if( p
14a20 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  ->iSize<=BITVEC_
14a21 4e 42 49 54 20 29 7b 0d 0a 20 20 20 20 70 2d 3e  NBIT ){..    p->
14a22 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56  u.aBitmap[i/BITV
14a23 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20  EC_SZELEM] |= 1 
14a24 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a  << (i&(BITVEC_SZ
14a25 45 4c 45 4d 2d 31 29 29 3b 0d 0a 20 20 20 20 72  ELEM-1));..    r
14a26 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
14a27 0d 0a 20 20 7d 0d 0a 20 20 68 20 3d 20 42 49 54  ..  }..  h = BIT
14a28 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0d 0a  VEC_HASH(i++);..
14a29 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77 61    /* if there wa
14a2a 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c 6c  sn't a hash coll
14a2b 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20  ision, and this 
14a2c 64 6f 65 73 6e 27 74 20 2a 2f 0d 0a 20 20 2f 2a  doesn't */..  /*
14a2d 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c   completely fill
14a2e 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20   the hash, then 
14a2f 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68  just add it with
14a30 6f 75 74 20 2a 2f 0d 0a 20 20 2f 2a 20 77 6f 72  out */..  /* wor
14a31 72 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64  ring about sub-d
14a32 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68  ividing and re-h
14a33 61 73 68 69 6e 67 2e 20 2a 2f 0d 0a 20 20 69 66  ashing. */..  if
14a34 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  ( !p->u.aHash[h]
14a35 20 29 7b 0d 0a 20 20 20 20 69 66 20 28 70 2d 3e   ){..    if (p->
14a36 6e 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e  nSet<(BITVEC_NIN
14a37 54 2d 31 29 29 20 7b 0d 0a 20 20 20 20 20 20 67  T-1)) {..      g
14a38 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65  oto bitvec_set_e
14a39 6e 64 3b 0d 0a 20 20 20 20 7d 20 65 6c 73 65 20  nd;..    } else 
14a3a 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69  {..      goto bi
14a3b 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3b  tvec_set_rehash;
14a3c 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
14a3d 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63  /* there was a c
14a3e 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20  ollision, check 
14a3f 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61  to see if it's a
14a40 6c 72 65 61 64 79 20 2a 2f 0d 0a 20 20 2f 2a 20  lready */..  /* 
14a41 69 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c  in hash, if not,
14a42 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73   try to find a s
14a43 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0d 0a 20  pot for it */.. 
14a44 20 64 6f 20 7b 0d 0a 20 20 20 20 69 66 28 20 70   do {..    if( p
14a45 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20  ->u.aHash[h]==i 
14a46 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
14a47 4f 4b 3b 0d 0a 20 20 20 20 68 2b 2b 3b 0d 0a 20  OK;..    h++;.. 
14a48 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43     if( h>=BITVEC
14a49 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0d 0a  _NINT ) h = 0;..
14a4a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e    } while( p->u.
14a4b 61 48 61 73 68 5b 68 5d 20 29 3b 0d 0a 20 20 2f  aHash[h] );..  /
14a4c 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64  * we didn't find
14a4d 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e   it in the hash.
14a4e 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68    h points to th
14a4f 65 20 66 69 72 73 74 20 2a 2f 0d 0a 20 20 2f 2a  e first */..  /*
14a50 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20   available free 
14a51 73 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73  spot. check to s
14a52 65 65 20 69 66 20 74 68 69 73 20 69 73 20 67 6f  ee if this is go
14a53 69 6e 67 20 74 6f 20 2a 2f 0d 0a 20 20 2f 2a 20  ing to */..  /* 
14a54 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f  make our hash to
14a55 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0d 0a 62  o "full".  */..b
14a56 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68  itvec_set_rehash
14a57 3a 0d 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74  :..  if( p->nSet
14a58 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20  >=BITVEC_MXHASH 
14a59 29 7b 0d 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  ){..    unsigned
14a5a 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 69 6e 74   int j;..    int
14a5b 20 72 63 3b 0d 0a 20 20 20 20 75 33 32 20 2a 61   rc;..    u32 *a
14a5c 69 56 61 6c 75 65 73 20 3d 20 73 71 6c 69 74 65  iValues = sqlite
14a5d 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 30  3StackAllocRaw(0
14a5e 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48  , sizeof(p->u.aH
14a5f 61 73 68 29 29 3b 0d 0a 20 20 20 20 69 66 28 20  ash));..    if( 
14a60 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b 0d 0a  aiValues==0 ){..
14a61 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
14a62 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
14a63 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6d 65  }else{..      me
14a64 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70  mcpy(aiValues, p
14a65 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f  ->u.aHash, sizeo
14a66 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0d  f(p->u.aHash));.
14a67 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2d  .      memset(p-
14a68 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 7a  >u.apSub, 0, siz
14a69 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 29  eof(p->u.apSub))
14a6a 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 44 69 76  ;..      p->iDiv
14a6b 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65  isor = (p->iSize
14a6c 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d   + BITVEC_NPTR -
14a6d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b   1)/BITVEC_NPTR;
14a6e 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ..      rc = sql
14a6f 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c  ite3BitvecSet(p,
14a70 20 69 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28   i);..      for(
14a71 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49  j=0; j<BITVEC_NI
14a72 4e 54 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20  NT; j++){..     
14a73 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b     if( aiValues[
14a74 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74  j] ) rc |= sqlit
14a75 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61  e3BitvecSet(p, a
14a76 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0d 0a 20 20  iValues[j]);..  
14a77 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c      }..      sql
14a78 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 30 2c  ite3StackFree(0,
14a79 20 61 69 56 61 6c 75 65 73 29 3b 0d 0a 20 20 20   aiValues);..   
14a7a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
14a7b 20 20 20 7d 0d 0a 20 20 7d 0d 0a 62 69 74 76 65     }..  }..bitve
14a7c 63 5f 73 65 74 5f 65 6e 64 3a 0d 0a 20 20 70 2d  c_set_end:..  p-
14a7d 3e 6e 53 65 74 2b 2b 3b 0d 0a 20 20 70 2d 3e 75  >nSet++;..  p->u
14a7e 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0d 0a  .aHash[h] = i;..
14a7f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14a80 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
14a81 20 43 6c 65 61 72 20 74 68 65 20 69 2d 74 68 20   Clear the i-th 
14a82 62 69 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70 42 75  bit...**..** pBu
14a83 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e  f must be a poin
14a84 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74 20  ter to at least 
14a85 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73 20  BITVEC_SZ bytes 
14a86 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f  of temporary sto
14a87 72 61 67 65 0d 0a 2a 2a 20 74 68 61 74 20 42 69  rage..** that Bi
14a88 74 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73  tvecClear can us
14a89 65 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73  e to rebuilt its
14a8a 20 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a 2a 2f   hash table...*/
14a8b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
14a8c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
14a8d 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 20  vecClear(Bitvec 
14a8e 2a 70 2c 20 75 33 32 20 69 2c 20 76 6f 69 64 20  *p, u32 i, void 
14a8f 2a 70 42 75 66 29 7b 0d 0a 20 20 69 66 28 20 70  *pBuf){..  if( p
14a90 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  ==0 ) return;.. 
14a91 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0d   assert( i>0 );.
14a92 0a 20 20 69 2d 2d 3b 0d 0a 20 20 77 68 69 6c 65  .  i--;..  while
14a93 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b  ( p->iDivisor ){
14a94 0d 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20  ..    u32 bin = 
14a95 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0d 0a  i/p->iDivisor;..
14a96 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69      i = i%p->iDi
14a97 76 69 73 6f 72 3b 0d 0a 20 20 20 20 70 20 3d 20  visor;..    p = 
14a98 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b  p->u.apSub[bin];
14a99 0d 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0d  ..    if (!p) {.
14a9a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  .      return;..
14a9b 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
14a9c 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56  ( p->iSize<=BITV
14a9d 45 43 5f 4e 42 49 54 20 29 7b 0d 0a 20 20 20 20  EC_NBIT ){..    
14a9e 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42  p->u.aBitmap[i/B
14a9f 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d  ITVEC_SZELEM] &=
14aa0 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 56   ~(1 << (i&(BITV
14aa1 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b 0d  EC_SZELEM-1)));.
14aa2 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 75  .  }else{..    u
14aa3 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0d 0a  nsigned int j;..
14aa4 20 20 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65      u32 *aiValue
14aa5 73 20 3d 20 70 42 75 66 3b 0d 0a 20 20 20 20 6d  s = pBuf;..    m
14aa6 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20  emcpy(aiValues, 
14aa7 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65  p->u.aHash, size
14aa8 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b  of(p->u.aHash));
14aa9 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e  ..    memset(p->
14aaa 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65  u.aHash, 0, size
14aab 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b  of(p->u.aHash));
14aac 0d 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20  ..    p->nSet = 
14aad 30 3b 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  0;..    for(j=0;
14aae 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20   j<BITVEC_NINT; 
14aaf 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  j++){..      if(
14ab0 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20   aiValues[j] && 
14ab1 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b  aiValues[j]!=(i+
14ab2 31 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 75  1) ){..        u
14ab3 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41  32 h = BITVEC_HA
14ab4 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31  SH(aiValues[j]-1
14ab5 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e  );..        p->n
14ab6 53 65 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20  Set++;..        
14ab7 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73  while( p->u.aHas
14ab8 68 5b 68 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20  h[h] ){..       
14ab9 20 20 20 68 2b 2b 3b 0d 0a 20 20 20 20 20 20 20     h++;..       
14aba 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43     if( h>=BITVEC
14abb 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0d 0a  _NINT ) h = 0;..
14abc 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
14abd 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d     p->u.aHash[h]
14abe 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d 3b 0d   = aiValues[j];.
14abf 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
14ac0 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
14ac1 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 74 6d  * Destroy a bitm
14ac2 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 6c  ap object.  Recl
14ac3 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75  aim all memory u
14ac4 73 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  sed...*/..SQLITE
14ac5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14ac6 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
14ac7 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0d 0a  oy(Bitvec *p){..
14ac8 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
14ac9 75 72 6e 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 69  urn;..  if( p->i
14aca 44 69 76 69 73 6f 72 20 29 7b 0d 0a 20 20 20 20  Divisor ){..    
14acb 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0d  unsigned int i;.
14acc 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
14acd 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b  BITVEC_NPTR; i++
14ace 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
14acf 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
14ad0 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0d 0a  ->u.apSub[i]);..
14ad1 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71      }..  }..  sq
14ad2 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a  lite3_free(p);..
14ad3 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
14ad4 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  rn the value of 
14ad5 74 68 65 20 69 53 69 7a 65 20 70 61 72 61 6d 65  the iSize parame
14ad6 74 65 72 20 73 70 65 63 69 66 69 65 64 20 77 68  ter specified wh
14ad7 65 6e 20 42 69 74 76 65 63 20 2a 70 0d 0a 2a 2a  en Bitvec *p..**
14ad8 20 77 61 73 20 63 72 65 61 74 65 64 2e 0d 0a 2a   was created...*
14ad9 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
14ada 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74  E u32 sqlite3Bit
14adb 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 20 2a  vecSize(Bitvec *
14adc 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 2d  p){..  return p-
14add 3e 69 53 69 7a 65 3b 0d 0a 7d 0d 0a 0d 0a 23 69  >iSize;..}....#i
14ade 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14adf 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0d 0a  T_BUILTIN_TEST..
14ae0 2f 2a 0d 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62  /*..** Let V[] b
14ae1 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e  e an array of un
14ae2 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 72  signed character
14ae3 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20  s sufficient to 
14ae4 68 6f 6c 64 0d 0a 2a 2a 20 75 70 20 74 6f 20 4e  hold..** up to N
14ae5 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65   bits.  Let I be
14ae6 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
14ae7 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c  een 0 and N.  0<
14ae8 3d 49 3c 4e 2e 0d 0a 2a 2a 20 54 68 65 6e 20 74  =I<N...** Then t
14ae9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
14aea 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  ros can be used 
14aeb 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f  to set, clear, o
14aec 72 20 74 65 73 74 0d 0a 2a 2a 20 69 6e 64 69 76  r test..** indiv
14aed 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 69  idual bits withi
14aee 6e 20 56 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  n V...*/..#defin
14aef 65 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20  e SETBIT(V,I)   
14af0 20 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31     V[I>>3] |= (1
14af1 3c 3c 28 49 26 37 29 29 0d 0a 23 64 65 66 69 6e  <<(I&7))..#defin
14af2 65 20 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20  e CLEARBIT(V,I) 
14af3 20 20 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28     V[I>>3] &= ~(
14af4 31 3c 3c 28 49 26 37 29 29 0d 0a 23 64 65 66 69  1<<(I&7))..#defi
14af5 6e 65 20 54 45 53 54 42 49 54 28 56 2c 49 29 20  ne TESTBIT(V,I) 
14af6 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c      (V[I>>3]&(1<
14af7 3c 28 49 26 37 29 29 29 21 3d 30 0d 0a 0d 0a 2f  <(I&7)))!=0..../
14af8 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
14af9 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e  ne runs an exten
14afa 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65  sive test of the
14afb 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0d 0a 2a   Bitvec code...*
14afc 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  *..** The input 
14afd 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69  is an array of i
14afe 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 63 74  ntegers that act
14aff 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0d 0a  s as a program..
14b00 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 42  ** to test the B
14b01 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65  itvec.  The inte
14b02 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73  gers are opcodes
14b03 20 66 6f 6c 6c 6f 77 65 64 0d 0a 2a 2a 20 62 79   followed..** by
14b04 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72   0, 1, or 3 oper
14b05 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20  ands, depending 
14b06 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20  on the opcode.  
14b07 41 6e 6f 74 68 65 72 0d 0a 2a 2a 20 6f 70 63 6f  Another..** opco
14b08 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64  de follows immed
14b09 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65  iately after the
14b0a 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e 0d 0a   last operand...
14b0b 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  **..** There are
14b0c 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65   6 opcodes numbe
14b0d 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75  red from 0 throu
14b0e 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 65 0d  gh 5.  0 is the.
14b0f 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f 64  .** "halt" opcod
14b10 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65  e and causes the
14b11 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0d 0a 2a   test to end...*
14b12 2a 0d 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20  *..**    0      
14b13 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74      Halt and ret
14b14 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
14b15 66 20 65 72 72 6f 72 73 0d 0a 2a 2a 20 20 20 20  f errors..**    
14b16 31 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e  1 N S X    Set N
14b17 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20   bits beginning 
14b18 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65  with S and incre
14b19 6d 65 6e 74 69 6e 67 20 62 79 20 58 0d 0a 2a 2a  menting by X..**
14b1a 20 20 20 20 32 20 4e 20 53 20 58 20 20 20 20 43      2 N S X    C
14b1b 6c 65 61 72 20 4e 20 62 69 74 73 20 62 65 67 69  lear N bits begi
14b1c 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64  nning with S and
14b1d 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79   incrementing by
14b1e 20 58 0d 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20   X..**    3 N   
14b1f 20 20 20 20 20 53 65 74 20 4e 20 72 61 6e 64 6f       Set N rando
14b20 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0d  mly chosen bits.
14b21 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 20 20  .**    4 N      
14b22 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 6f 6d    Clear N random
14b23 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0d 0a  ly chosen bits..
14b24 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20 20  **    5 N S X   
14b25 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f 6d   Set N bits from
14b26 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 69   S increment X i
14b27 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f  n array only, no
14b28 74 20 69 6e 20 62 69 74 76 65 63 0d 0a 2a 2a 0d  t in bitvec..**.
14b29 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20  .** The opcodes 
14b2a 31 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66  1 through 4 perf
14b2b 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61  orm set and clea
14b2c 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  r operations are
14b2d 20 70 65 72 66 6f 72 6d 65 64 0d 0a 2a 2a 20 6f   performed..** o
14b2e 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 63 20  n both a Bitvec 
14b2f 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20  object and on a 
14b30 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20  linear array of 
14b31 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72  bits obtained fr
14b32 6f 6d 20 6d 61 6c 6c 6f 63 2e 0d 0a 2a 2a 20 4f  om malloc...** O
14b33 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e  pcode 5 works on
14b34 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61   the linear arra
14b35 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74  y only, not on t
14b36 68 65 20 42 69 74 76 65 63 2e 0d 0a 2a 2a 20 4f  he Bitvec...** O
14b37 70 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20  pcode 5 is used 
14b38 74 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20  to deliberately 
14b39 69 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69  induce a fault i
14b3a 6e 20 6f 72 64 65 72 20 74 6f 0d 0a 2a 2a 20 63  n order to..** c
14b3b 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f  onfirm that erro
14b3c 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b  r detection work
14b3d 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 74 20 74 68  s...**..** At th
14b3e 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
14b3f 74 68 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e  the test the lin
14b40 65 61 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d  ear array is com
14b41 70 61 72 65 64 0d 0a 2a 2a 20 61 67 61 69 6e 73  pared..** agains
14b42 74 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a  t the Bitvec obj
14b43 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 61  ect.  If there a
14b44 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63  re any differenc
14b45 65 73 2c 0d 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  es,..** an error
14b46 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
14b47 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 73  f they are the s
14b48 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74  ame, zero is ret
14b49 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  urned...**..** I
14b4a 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
14b4b 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75  ation error occu
14b4c 72 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0d 0a  rs, return -1...
14b4d 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
14b4e 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
14b4f 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
14b50 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70  int sz, int *aOp
14b51 29 7b 0d 0a 20 20 42 69 74 76 65 63 20 2a 70 42  ){..  Bitvec *pB
14b52 69 74 76 65 63 20 3d 20 30 3b 0d 0a 20 20 75 6e  itvec = 0;..  un
14b53 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56 20  signed char *pV 
14b54 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  = 0;..  int rc =
14b55 20 2d 31 3b 0d 0a 20 20 69 6e 74 20 69 2c 20 6e   -1;..  int i, n
14b56 78 2c 20 70 63 2c 20 6f 70 3b 0d 0a 20 20 76 6f  x, pc, op;..  vo
14b57 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b 0d 0a  id *pTmpSpace;..
14b58 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
14b59 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65  the Bitvec to be
14b5a 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69   tested and a li
14b5b 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0d 0a 20  near array of.. 
14b5c 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20   ** bits to act 
14b5d 61 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  as the reference
14b5e 20 2a 2f 0d 0a 20 20 70 42 69 74 76 65 63 20 3d   */..  pBitvec =
14b5f 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
14b60 65 61 74 65 28 20 73 7a 20 29 3b 0d 0a 20 20 70  eate( sz );..  p
14b61 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  V = sqlite3_mall
14b62 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31  oc( (sz+7)/8 + 1
14b63 20 29 3b 0d 0a 20 20 70 54 6d 70 53 70 61 63 65   );..  pTmpSpace
14b64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
14b65 63 28 42 49 54 56 45 43 5f 53 5a 29 3b 0d 0a 20  c(BITVEC_SZ);.. 
14b66 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d 30 20   if( pBitvec==0 
14b67 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d 70  || pV==0 || pTmp
14b68 53 70 61 63 65 3d 3d 30 20 20 29 20 67 6f 74 6f  Space==0  ) goto
14b69 20 62 69 74 76 65 63 5f 65 6e 64 3b 0d 0a 20 20   bitvec_end;..  
14b6a 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73  memset(pV, 0, (s
14b6b 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0d 0a 0d 0a  z+7)/8 + 1);....
14b6c 20 20 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 65    /* NULL pBitve
14b6d 63 20 74 65 73 74 73 20 2a 2f 0d 0a 20 20 73 71  c tests */..  sq
14b6e 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 30  lite3BitvecSet(0
14b6f 2c 20 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  , 1);..  sqlite3
14b70 42 69 74 76 65 63 43 6c 65 61 72 28 30 2c 20 31  BitvecClear(0, 1
14b71 2c 20 70 54 6d 70 53 70 61 63 65 29 3b 0d 0a 0d  , pTmpSpace);...
14b72 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 70 72  .  /* Run the pr
14b73 6f 67 72 61 6d 20 2a 2f 0d 0a 20 20 70 63 20 3d  ogram */..  pc =
14b74 20 30 3b 0d 0a 20 20 77 68 69 6c 65 28 20 28 6f   0;..  while( (o
14b75 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 20  p = aOp[pc])!=0 
14b76 29 7b 0d 0a 20 20 20 20 73 77 69 74 63 68 28 20  ){..    switch( 
14b77 6f 70 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 73  op ){..      cas
14b78 65 20 31 3a 0d 0a 20 20 20 20 20 20 63 61 73 65  e 1:..      case
14b79 20 32 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20   2:..      case 
14b7a 35 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 6e 78  5: {..        nx
14b7b 20 3d 20 34 3b 0d 0a 20 20 20 20 20 20 20 20 69   = 4;..        i
14b7c 20 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31   = aOp[pc+2] - 1
14b7d 3b 0d 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 70  ;..        aOp[p
14b7e 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33  c+2] += aOp[pc+3
14b7f 5d 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ];..        brea
14b80 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
14b81 20 20 20 63 61 73 65 20 33 3a 0d 0a 20 20 20 20     case 3:..    
14b82 20 20 63 61 73 65 20 34 3a 20 0d 0a 20 20 20 20    case 4: ..    
14b83 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20    default: {..  
14b84 20 20 20 20 20 20 6e 78 20 3d 20 32 3b 0d 0a 20        nx = 2;.. 
14b85 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
14b86 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
14b87 28 69 29 2c 20 26 69 29 3b 0d 0a 20 20 20 20 20  (i), &i);..     
14b88 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
14b89 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69   }..    }..    i
14b8a 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29  f( (--aOp[pc+1])
14b8b 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0d 0a   > 0 ) nx = 0;..
14b8c 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0d 0a 20      pc += nx;.. 
14b8d 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66     i = (i & 0x7f
14b8e 66 66 66 66 66 66 29 25 73 7a 3b 0d 0a 20 20 20  ffffff)%sz;..   
14b8f 20 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30   if( (op & 1)!=0
14b90 20 29 7b 0d 0a 20 20 20 20 20 20 53 45 54 42 49   ){..      SETBI
14b91 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0d 0a 20  T(pV, (i+1));.. 
14b92 20 20 20 20 20 69 66 28 20 6f 70 21 3d 35 20 29       if( op!=5 )
14b93 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  {..        if( s
14b94 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
14b95 70 42 69 74 76 65 63 2c 20 69 2b 31 29 20 29 20  pBitvec, i+1) ) 
14b96 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b  goto bitvec_end;
14b97 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
14b98 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 43 4c 45  else{..      CLE
14b99 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29  ARBIT(pV, (i+1))
14b9a 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
14b9b 42 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74  BitvecClear(pBit
14b9c 76 65 63 2c 20 69 2b 31 2c 20 70 54 6d 70 53 70  vec, i+1, pTmpSp
14b9d 61 63 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ace);..    }..  
14b9e 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 65 73 74 20 74  }....  /* Test t
14b9f 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
14ba0 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78 61  linear array exa
14ba1 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65  ctly matches the
14ba2 0d 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62  ..  ** Bitvec ob
14ba3 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74  ject.  Start wit
14ba4 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  h the assumption
14ba5 20 74 68 61 74 20 74 68 65 79 20 64 6f 0d 0a 20   that they do.. 
14ba6 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30   ** match (rc==0
14ba7 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f  ).  Change rc to
14ba8 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64   non-zero if a d
14ba9 69 73 63 72 65 70 61 6e 63 79 0d 0a 20 20 2a 2a  iscrepancy..  **
14baa 20 69 73 20 66 6f 75 6e 64 2e 0d 0a 20 20 2a 2f   is found...  */
14bab 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
14bac 42 69 74 76 65 63 54 65 73 74 28 30 2c 30 29 20  BitvecTest(0,0) 
14bad 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  + sqlite3BitvecT
14bae 65 73 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b  est(pBitvec, sz+
14baf 31 29 0d 0a 20 20 20 20 20 20 20 20 20 20 2b 20  1)..          + 
14bb0 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
14bb1 74 28 70 42 69 74 76 65 63 2c 20 30 29 0d 0a 20  t(pBitvec, 0).. 
14bb2 20 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c 69           + (sqli
14bb3 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42  te3BitvecSize(pB
14bb4 69 74 76 65 63 29 20 2d 20 73 7a 29 3b 0d 0a 20  itvec) - sz);.. 
14bb5 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b   for(i=1; i<=sz;
14bb6 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20   i++){..    if( 
14bb7 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 29   (TESTBIT(pV,i))
14bb8 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  !=sqlite3BitvecT
14bb9 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 29  est(pBitvec,i) )
14bba 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b  {..      rc = i;
14bbb 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
14bbc 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
14bbd 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 61 74 65  /* Free allocate
14bbe 64 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a  d structure */..
14bbf 62 69 74 76 65 63 5f 65 6e 64 3a 0d 0a 20 20 73  bitvec_end:..  s
14bc0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70  qlite3_free(pTmp
14bc1 53 70 61 63 65 29 3b 0d 0a 20 20 73 71 6c 69 74  Space);..  sqlit
14bc2 65 33 5f 66 72 65 65 28 70 56 29 3b 0d 0a 20 20  e3_free(pV);..  
14bc3 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
14bc4 74 72 6f 79 28 70 42 69 74 76 65 63 29 3b 0d 0a  troy(pBitvec);..
14bc5 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
14bc6 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
14bc7 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
14bc8 45 53 54 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  EST */..../*****
14bc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
14bca 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a   bitvec.c ******
14bcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bcd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
14bce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
14bcf 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a   file pcache.c *
14bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
14bd3 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30  ** 2008 August 0
14bd4 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  5..**..** The au
14bd5 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
14bd6 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
14bd7 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
14bd8 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
14bd9 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
14bda 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
14bdb 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
14bdc 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
14bdd 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
14bde 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
14bdf 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
14be0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
14be1 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
14be2 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
14be3 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
14be4 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
14be5 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
14be6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14be7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14be8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14be9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bea 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
14beb 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
14bec 74 73 20 74 68 61 74 20 70 61 67 65 20 63 61 63  ts that page cac
14bed 68 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  he...*/..../*..*
14bee 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67  * A complete pag
14bef 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e  e cache is an in
14bf0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
14bf1 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 73  tructure...*/..s
14bf2 74 72 75 63 74 20 50 43 61 63 68 65 20 7b 0d 0a  truct PCache {..
14bf3 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c    PgHdr *pDirty,
14bf4 20 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 20   *pDirtyTail;   
14bf5 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
14bf6 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20   dirty pages in 
14bf7 4c 52 55 20 6f 72 64 65 72 20 2a 2f 0d 0a 20 20  LRU order */..  
14bf8 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20  PgHdr *pSynced; 
14bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bfa 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63      /* Last sync
14bfb 65 64 20 70 61 67 65 20 69 6e 20 64 69 72 74 79  ed page in dirty
14bfc 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0d 0a 20   page list */.. 
14bfd 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
14bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bff 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14c00 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67  f referenced pag
14c01 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 43  es */..  int szC
14c02 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20  ache;           
14c03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14c04 43 6f 6e 66 69 67 75 72 65 64 20 63 61 63 68 65  Configured cache
14c05 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   size */..  int 
14c06 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  szPage;         
14c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c08 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79  /* Size of every
14c09 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61   page in this ca
14c0a 63 68 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a  che */..  int sz
14c0b 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20  Extra;          
14c0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14c0d 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 73   Size of extra s
14c0e 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 61  pace for each pa
14c0f 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 62 50 75  ge */..  int bPu
14c10 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
14c11 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14c12 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
14c13 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
14c14 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78  re */..  int (*x
14c15 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67  Stress)(void*,Pg
14c16 48 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20  Hdr*);       /* 
14c17 43 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65  Call to try make
14c18 20 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f   a page clean */
14c19 0d 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73  ..  void *pStres
14c1a 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
14c1b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
14c1c 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a  ent to xStress *
14c1d 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61  /..  sqlite3_pca
14c1e 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20 20  che *pCache;    
14c1f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6c 75 67           /* Plug
14c20 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f 64 75  gable cache modu
14c21 6c 65 20 2a 2f 0d 0a 20 20 50 67 48 64 72 20 2a  le */..  PgHdr *
14c22 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 20 20  pPage1;         
14c23 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14c24 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67  Reference to pag
14c25 65 20 31 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  e 1 */..};..../*
14c26 0d 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65  ..** Some of the
14c27 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73   assert() macros
14c28 20 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72   in this code ar
14c29 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20  e too expensive 
14c2a 74 6f 20 72 75 6e 0d 0a 2a 2a 20 65 76 65 6e 20  to run..** even 
14c2b 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65  during normal de
14c2c 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68  bugging.  Use th
14c2d 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f  em only rarely o
14c2e 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0d 0a  n long-running..
14c2f 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 6c  ** tests.  Enabl
14c30 65 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 20  e the expensive 
14c31 61 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 68  asserts using th
14c32 65 0d 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45  e..** -DSQLITE_E
14c33 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
14c34 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65  ASSERT=1 compile
14c35 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0d 0a 2a  -time option...*
14c36 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
14c37 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
14c38 45 5f 41 53 53 45 52 54 0d 0a 23 20 64 65 66 69  E_ASSERT..# defi
14c39 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  ne expensive_ass
14c3a 65 72 74 28 58 29 20 20 61 73 73 65 72 74 28 58  ert(X)  assert(X
14c3b 29 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  )..#else..# defi
14c3c 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  ne expensive_ass
14c3d 65 72 74 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a  ert(X)..#endif..
14c3e 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
14c3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c40 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73  ***** Linked Lis
14c41 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a  t Management ***
14c42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c43 2a 2f 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e  */....#if !defin
14c44 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 65  ed(NDEBUG) && de
14c45 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
14c46 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
14c47 53 45 52 54 29 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68  SERT)../*..** Ch
14c48 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61  eck that the pCa
14c49 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72  che->pSynced var
14c4a 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72  iable is set cor
14c4b 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0d 0a 2a  rectly. If it..*
14c4c 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72  * is not, either
14c4d 20 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20   fail an assert 
14c4e 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20  or return zero. 
14c4f 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
14c50 6e 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  n..** non-zero. 
14c51 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
14c52 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62  d in debugging b
14c53 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77  uilds, as follow
14c54 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 65 78 70  s:..**..**   exp
14c55 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70  ensive_assert( p
14c56 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
14c57 28 70 43 61 63 68 65 29 20 29 3b 0d 0a 2a 2f 0d  (pCache) );..*/.
14c58 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63  .static int pcac
14c59 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 50 43  heCheckSynced(PC
14c5a 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0d 0a  ache *pCache){..
14c5b 20 20 50 67 48 64 72 20 2a 70 3b 0d 0a 20 20 66    PgHdr *p;..  f
14c5c 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
14c5d 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61 63  rtyTail; p!=pCac
14c5e 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d 70  he->pSynced; p=p
14c5f 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0d 0a  ->pDirtyPrev){..
14c60 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
14c61 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73  Ref || (p->flags
14c62 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
14c63 29 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ) );..  }..  ret
14c64 75 72 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e  urn (p==0 || p->
14c65 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67  nRef || (p->flag
14c66 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
14c67 43 29 3d 3d 30 29 3b 0d 0a 7d 0d 0a 23 65 6e 64  C)==0);..}..#end
14c68 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 26 26  if /* !NDEBUG &&
14c69 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45   SQLITE_ENABLE_E
14c6a 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 20  XPENSIVE_ASSERT 
14c6b 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6d  */..../*..** Rem
14c6c 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66  ove page pPage f
14c6d 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20  rom the list of 
14c6e 64 69 72 74 79 20 70 61 67 65 73 2e 0d 0a 2a 2f  dirty pages...*/
14c6f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  ..static void pc
14c70 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
14c71 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70  rtyList(PgHdr *p
14c72 50 61 67 65 29 7b 0d 0a 20 20 50 43 61 63 68 65  Page){..  PCache
14c73 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61   *p = pPage->pCa
14c74 63 68 65 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  che;....  assert
14c75 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  ( pPage->pDirtyN
14c76 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d  ext || pPage==p-
14c77 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0d 0a  >pDirtyTail );..
14c78 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
14c79 3e 70 44 69 72 74 79 50 72 65 76 20 7c 7c 20 70  >pDirtyPrev || p
14c7a 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20  Page==p->pDirty 
14c7b 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 55 70 64 61 74  );....  /* Updat
14c7c 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70 53  e the PCache1.pS
14c7d 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69  ynced variable i
14c7e 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f 0d  f necessary. */.
14c7f 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65  .  if( p->pSynce
14c80 64 3d 3d 70 50 61 67 65 20 29 7b 0d 0a 20 20 20  d==pPage ){..   
14c81 20 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20   PgHdr *pSynced 
14c82 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  = pPage->pDirtyP
14c83 72 65 76 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  rev;..    while(
14c84 20 70 53 79 6e 63 65 64 20 26 26 20 28 70 53 79   pSynced && (pSy
14c85 6e 63 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44  nced->flags&PGHD
14c86 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0d  R_NEED_SYNC) ){.
14c87 0a 20 20 20 20 20 20 70 53 79 6e 63 65 64 20 3d  .      pSynced =
14c88 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 72 74 79   pSynced->pDirty
14c89 50 72 65 76 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Prev;..    }..  
14c8a 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70    p->pSynced = p
14c8b 53 79 6e 63 65 64 3b 0d 0a 20 20 7d 0d 0a 0d 0a  Synced;..  }....
14c8c 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69    if( pPage->pDi
14c8d 72 74 79 4e 65 78 74 20 29 7b 0d 0a 20 20 20 20  rtyNext ){..    
14c8e 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
14c8f 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20  t->pDirtyPrev = 
14c90 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
14c91 76 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  v;..  }else{..  
14c92 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d    assert( pPage=
14c93 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29  =p->pDirtyTail )
14c94 3b 0d 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  ;..    p->pDirty
14c95 54 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44  Tail = pPage->pD
14c96 69 72 74 79 50 72 65 76 3b 0d 0a 20 20 7d 0d 0a  irtyPrev;..  }..
14c97 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69    if( pPage->pDi
14c98 72 74 79 50 72 65 76 20 29 7b 0d 0a 20 20 20 20  rtyPrev ){..    
14c99 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
14c9a 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20  v->pDirtyNext = 
14c9b 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
14c9c 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  t;..  }else{..  
14c9d 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d    assert( pPage=
14c9e 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0d 0a 20  =p->pDirty );.. 
14c9f 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70     p->pDirty = p
14ca0 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
14ca1 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50 61 67 65 2d  ;..  }..  pPage-
14ca2 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 30 3b  >pDirtyNext = 0;
14ca3 0d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74  ..  pPage->pDirt
14ca4 79 50 72 65 76 20 3d 20 30 3b 0d 0a 0d 0a 20 20  yPrev = 0;....  
14ca5 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
14ca6 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e  ( pcacheCheckSyn
14ca7 63 65 64 28 70 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a  ced(p) );..}....
14ca8 2f 2a 0d 0a 2a 2a 20 41 64 64 20 70 61 67 65 20  /*..** Add page 
14ca9 70 50 61 67 65 20 74 6f 20 74 68 65 20 68 65 61  pPage to the hea
14caa 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c  d of the dirty l
14cab 69 73 74 20 28 50 43 61 63 68 65 31 2e 70 44 69  ist (PCache1.pDi
14cac 72 74 79 20 69 73 20 73 65 74 20 74 6f 0d 0a 2a  rty is set to..*
14cad 2a 20 70 50 61 67 65 29 2e 0d 0a 2a 2f 0d 0a 73  * pPage)...*/..s
14cae 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
14caf 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28  eAddToDirtyList(
14cb0 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0d 0a  PgHdr *pPage){..
14cb1 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50    PCache *p = pP
14cb2 61 67 65 2d 3e 70 43 61 63 68 65 3b 0d 0a 0d 0a  age->pCache;....
14cb3 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
14cb4 3e 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26  >pDirtyNext==0 &
14cb5 26 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  & pPage->pDirtyP
14cb6 72 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69  rev==0 && p->pDi
14cb7 72 74 79 21 3d 70 50 61 67 65 20 29 3b 0d 0a 0d  rty!=pPage );...
14cb8 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  .  pPage->pDirty
14cb9 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79  Next = p->pDirty
14cba 3b 0d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  ;..  if( pPage->
14cbb 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0d 0a 20  pDirtyNext ){.. 
14cbc 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
14cbd 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44  ->pDirtyNext->pD
14cbe 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0d 0a  irtyPrev==0 );..
14cbf 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74      pPage->pDirt
14cc0 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65  yNext->pDirtyPre
14cc1 76 20 3d 20 70 50 61 67 65 3b 0d 0a 20 20 7d 0d  v = pPage;..  }.
14cc2 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70  .  p->pDirty = p
14cc3 50 61 67 65 3b 0d 0a 20 20 69 66 28 20 21 70 2d  Page;..  if( !p-
14cc4 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b 0d 0a  >pDirtyTail ){..
14cc5 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69      p->pDirtyTai
14cc6 6c 20 3d 20 70 50 61 67 65 3b 0d 0a 20 20 7d 0d  l = pPage;..  }.
14cc7 0a 20 20 69 66 28 20 21 70 2d 3e 70 53 79 6e 63  .  if( !p->pSync
14cc8 65 64 20 26 26 20 30 3d 3d 28 70 50 61 67 65 2d  ed && 0==(pPage-
14cc9 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
14cca 44 5f 53 59 4e 43 29 20 29 7b 0d 0a 20 20 20 20  D_SYNC) ){..    
14ccb 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 61  p->pSynced = pPa
14ccc 67 65 3b 0d 0a 20 20 7d 0d 0a 20 20 65 78 70 65  ge;..  }..  expe
14ccd 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63  nsive_assert( pc
14cce 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28  acheCheckSynced(
14ccf 70 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  p) );..}..../*..
14cd0 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e  ** Wrapper aroun
14cd1 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20  d the pluggable 
14cd2 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20 6d 65  caches xUnpin me
14cd3 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63 61 63  thod. If the cac
14cd4 68 65 20 69 73 0d 0a 2a 2a 20 62 65 69 6e 67 20  he is..** being 
14cd5 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d  used for an in-m
14cd6 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20  emory database, 
14cd7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
14cd8 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 73   a no-op...*/..s
14cd9 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
14cda 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29  eUnpin(PgHdr *p)
14cdb 7b 0d 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  {..  PCache *pCa
14cdc 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
14cdd 0d 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ..  if( pCache->
14cde 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0d 0a 20  bPurgeable ){.. 
14cdf 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d     if( p->pgno==
14ce0 31 20 29 7b 0d 0a 20 20 20 20 20 20 70 43 61 63  1 ){..      pCac
14ce1 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0d  he->pPage1 = 0;.
14ce2 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
14ce3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
14ce4 70 63 61 63 68 65 32 2e 78 55 6e 70 69 6e 28 70  pcache2.xUnpin(p
14ce5 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
14ce6 2d 3e 70 50 61 67 65 2c 20 30 29 3b 0d 0a 20 20  ->pPage, 0);..  
14ce7 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  }..}..../*******
14ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ce9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e  ************ Gen
14ceb 65 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20  eral Interfaces 
14cec 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 49  ******..**..** I
14ced 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68  nitialize and sh
14cee 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20  utdown the page 
14cef 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 2e  cache subsystem.
14cf0 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73   Neither of thes
14cf1 65 20 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  e ..** functions
14cf2 20 61 72 65 20 74 68 72 65 61 64 73 61 66 65 2e   are threadsafe.
14cf3 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
14cf4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14cf5 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65  PcacheInitialize
14cf6 28 76 6f 69 64 29 7b 0d 0a 20 20 69 66 28 20 73  (void){..  if( s
14cf7 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
14cf8 69 67 2e 70 63 61 63 68 65 32 2e 78 49 6e 69 74  ig.pcache2.xInit
14cf9 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  ==0 ){..    /* I
14cfa 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46  MPLEMENTATION-OF
14cfb 3a 20 52 2d 32 36 38 30 31 2d 36 34 31 33 37 20  : R-26801-64137 
14cfc 49 66 20 74 68 65 20 78 49 6e 69 74 28 29 20 6d  If the xInit() m
14cfd 65 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c 20 74  ethod is NULL, t
14cfe 68 65 6e 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  hen the..    ** 
14cff 62 75 69 6c 74 2d 69 6e 20 64 65 66 61 75 6c 74  built-in default
14d00 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 75   page cache is u
14d01 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74  sed instead of t
14d02 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
14d03 65 66 69 6e 65 64 0d 0a 20 20 20 20 2a 2a 20 70  efined..    ** p
14d04 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0d 0a 20  age cache. */.. 
14d05 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
14d06 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0d 0a 20  SetDefault();.. 
14d07 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   }..  return sql
14d08 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
14d09 2e 70 63 61 63 68 65 32 2e 78 49 6e 69 74 28 73  .pcache2.xInit(s
14d0a 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
14d0b 69 67 2e 70 63 61 63 68 65 32 2e 70 41 72 67 29  ig.pcache2.pArg)
14d0c 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ;..}..SQLITE_PRI
14d0d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14d0e 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28  3PcacheShutdown(
14d0f 76 6f 69 64 29 7b 0d 0a 20 20 69 66 28 20 73 71  void){..  if( sq
14d10 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
14d11 67 2e 70 63 61 63 68 65 32 2e 78 53 68 75 74 64  g.pcache2.xShutd
14d12 6f 77 6e 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  own ){..    /* I
14d13 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46  MPLEMENTATION-OF
14d14 3a 20 52 2d 32 36 30 30 30 2d 35 36 35 38 39 20  : R-26000-56589 
14d15 54 68 65 20 78 53 68 75 74 64 6f 77 6e 28 29 20  The xShutdown() 
14d16 6d 65 74 68 6f 64 20 6d 61 79 20 62 65 20 4e 55  method may be NU
14d17 4c 4c 2e 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  LL. */..    sqli
14d18 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
14d19 70 63 61 63 68 65 32 2e 78 53 68 75 74 64 6f 77  pcache2.xShutdow
14d1a 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n(sqlite3GlobalC
14d1b 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 70 41  onfig.pcache2.pA
14d1c 72 67 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  rg);..  }..}....
14d1d 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
14d1e 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
14d1f 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
14d20 63 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ct...*/..SQLITE_
14d21 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14d22 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f  te3PcacheSize(vo
14d23 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65  id){ return size
14d24 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0d 0a 0d  of(PCache); }...
14d25 0a 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61  ./*..** Create a
14d26 20 6e 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65   new PCache obje
14d27 63 74 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63  ct. Storage spac
14d28 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62  e to hold the ob
14d29 6a 65 63 74 0d 0a 2a 2a 20 68 61 73 20 61 6c 72  ject..** has alr
14d2a 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
14d2b 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
14d2c 64 20 69 6e 20 61 73 20 74 68 65 20 70 20 70 6f  d in as the p po
14d2d 69 6e 74 65 72 2e 20 0d 0a 2a 2a 20 54 68 65 20  inter. ..** The 
14d2e 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65 72 73  caller discovers
14d2f 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20   how much space 
14d30 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c 6c 6f  needs to be allo
14d31 63 61 74 65 64 20 62 79 20 0d 0a 2a 2a 20 63 61  cated by ..** ca
14d32 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61  lling sqlite3Pca
14d33 63 68 65 53 69 7a 65 28 29 2e 0d 0a 2a 2f 0d 0a  cheSize()...*/..
14d34 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14d35 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
14d36 65 4f 70 65 6e 28 0d 0a 20 20 69 6e 74 20 73 7a  eOpen(..  int sz
14d37 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
14d38 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
14d39 66 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0d  f every page */.
14d3a 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20  .  int szExtra, 
14d3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d3c 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61  /* Extra space a
14d3d 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
14d3e 61 63 68 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69  ach page */..  i
14d3f 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20  nt bPurgeable,  
14d40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
14d41 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65  rue if pages are
14d42 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72   on backing stor
14d43 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 53  e */..  int (*xS
14d44 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
14d45 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20  dr*),/* Call to 
14d46 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
14d47 73 20 63 6c 65 61 6e 20 2a 2f 0d 0a 20 20 76 6f  s clean */..  vo
14d48 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20  id *pStress,    
14d49 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
14d4a 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
14d4b 73 20 2a 2f 0d 0a 20 20 50 43 61 63 68 65 20 2a  s */..  PCache *
14d4c 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
14d4d 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63       /* Prealloc
14d4e 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74  ated space for t
14d4f 68 65 20 50 43 61 63 68 65 20 2a 2f 0d 0a 29 7b  he PCache */..){
14d50 0d 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ..  memset(p, 0,
14d51 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29   sizeof(PCache))
14d52 3b 0d 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d  ;..  p->szPage =
14d53 20 73 7a 50 61 67 65 3b 0d 0a 20 20 70 2d 3e 73   szPage;..  p->s
14d54 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61  zExtra = szExtra
14d55 3b 0d 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62  ;..  p->bPurgeab
14d56 6c 65 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b  le = bPurgeable;
14d57 0d 0a 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d  ..  p->xStress =
14d58 20 78 53 74 72 65 73 73 3b 0d 0a 20 20 70 2d 3e   xStress;..  p->
14d59 70 53 74 72 65 73 73 20 3d 20 70 53 74 72 65 73  pStress = pStres
14d5a 73 3b 0d 0a 20 20 70 2d 3e 73 7a 43 61 63 68 65  s;..  p->szCache
14d5b 20 3d 20 31 30 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   = 100;..}..../*
14d5c 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  ..** Change the 
14d5d 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43  page size for PC
14d5e 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65  ache object. The
14d5f 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73   caller must ens
14d60 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0d 0a  ure that there..
14d61 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  ** are no outsta
14d62 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72  nding page refer
14d63 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20  ences when this 
14d64 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
14d65 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ed...*/..SQLITE_
14d66 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14d67 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67  ite3PcacheSetPag
14d68 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43  eSize(PCache *pC
14d69 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65  ache, int szPage
14d6a 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43  ){..  assert( pC
14d6b 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26  ache->nRef==0 &&
14d6c 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3d   pCache->pDirty=
14d6d 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 43 61  =0 );..  if( pCa
14d6e 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0d 0a  che->pCache ){..
14d6f 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
14d70 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e  lConfig.pcache2.
14d71 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d  xDestroy(pCache-
14d72 3e 70 43 61 63 68 65 29 3b 0d 0a 20 20 20 20 70  >pCache);..    p
14d73 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20  Cache->pCache = 
14d74 30 3b 0d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  0;..    pCache->
14d75 70 50 61 67 65 31 20 3d 20 30 3b 0d 0a 20 20 7d  pPage1 = 0;..  }
14d76 0d 0a 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61  ..  pCache->szPa
14d77 67 65 20 3d 20 73 7a 50 61 67 65 3b 0d 0a 7d 0d  ge = szPage;..}.
14d78 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74  .../*..** Comput
14d79 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
14d7a 70 61 67 65 73 20 6f 66 20 63 61 63 68 65 20 72  pages of cache r
14d7b 65 71 75 65 73 74 65 64 2e 0d 0a 2a 2f 0d 0a 73  equested...*/..s
14d7c 74 61 74 69 63 20 69 6e 74 20 6e 75 6d 62 65 72  tatic int number
14d7d 4f 66 43 61 63 68 65 50 61 67 65 73 28 50 43 61  OfCachePages(PCa
14d7e 63 68 65 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  che *p){..  if( 
14d7f 70 2d 3e 73 7a 43 61 63 68 65 3e 3d 30 20 29 7b  p->szCache>=0 ){
14d80 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e  ..    return p->
14d81 73 7a 43 61 63 68 65 3b 0d 0a 20 20 7d 65 6c 73  szCache;..  }els
14d82 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 28  e{..    return (
14d83 69 6e 74 29 28 28 2d 31 30 32 34 2a 28 69 36 34  int)((-1024*(i64
14d84 29 70 2d 3e 73 7a 43 61 63 68 65 29 2f 28 70 2d  )p->szCache)/(p-
14d85 3e 73 7a 50 61 67 65 2b 70 2d 3e 73 7a 45 78 74  >szPage+p->szExt
14d86 72 61 29 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ra));..  }..}...
14d87 0a 2f 2a 0d 0a 2a 2a 20 54 72 79 20 74 6f 20 6f  ./*..** Try to o
14d88 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f  btain a page fro
14d89 6d 20 74 68 65 20 63 61 63 68 65 2e 0d 0a 2a 2f  m the cache...*/
14d8a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
14d8b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
14d8c 68 65 46 65 74 63 68 28 0d 0a 20 20 50 43 61 63  heFetch(..  PCac
14d8d 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20 20 20  he *pCache,     
14d8e 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20    /* Obtain the 
14d8f 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 20 63  page from this c
14d90 61 63 68 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20  ache */..  Pgno 
14d91 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
14d92 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
14d93 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0d 0a 20 20  to obtain */..  
14d94 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20  int createFlag, 
14d95 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
14d96 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66  , create page if
14d97 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
14d98 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0d 0a 20  st already */.. 
14d99 20 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20   PgHdr **ppPage 
14d9a 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
14d9b 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f  the page here */
14d9c 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ..){..  sqlite3_
14d9d 70 63 61 63 68 65 5f 70 61 67 65 20 2a 70 50 61  pcache_page *pPa
14d9e 67 65 20 3d 20 30 3b 0d 0a 20 20 50 67 48 64 72  ge = 0;..  PgHdr
14d9f 20 2a 70 50 67 48 64 72 20 3d 20 30 3b 0d 0a 20   *pPgHdr = 0;.. 
14da0 20 69 6e 74 20 65 43 72 65 61 74 65 3b 0d 0a 0d   int eCreate;...
14da1 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
14da2 65 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  e!=0 );..  asser
14da3 74 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31  t( createFlag==1
14da4 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d   || createFlag==
14da5 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
14da6 70 67 6e 6f 3e 30 20 29 3b 0d 0a 0d 0a 20 20 2f  pgno>0 );....  /
14da7 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 61 62  * If the pluggab
14da8 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 74 65  le cache (sqlite
14da9 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 20 6e  3_pcache*) has n
14daa 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  ot been allocate
14dab 64 2c 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  d,..  ** allocat
14dac 65 20 69 74 20 6e 6f 77 2e 0d 0a 20 20 2a 2f 0d  e it now...  */.
14dad 0a 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e  .  if( !pCache->
14dae 70 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65  pCache && create
14daf 46 6c 61 67 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Flag ){..    sql
14db0 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0d  ite3_pcache *p;.
14db1 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
14db2 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
14db3 63 68 65 32 2e 78 43 72 65 61 74 65 28 0d 0a 20  che2.xCreate(.. 
14db4 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 73         pCache->s
14db5 7a 50 61 67 65 2c 20 70 43 61 63 68 65 2d 3e 73  zPage, pCache->s
14db6 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 6f 66 28  zExtra + sizeof(
14db7 50 67 48 64 72 29 2c 20 70 43 61 63 68 65 2d 3e  PgHdr), pCache->
14db8 62 50 75 72 67 65 61 62 6c 65 0d 0a 20 20 20 20  bPurgeable..    
14db9 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 20 29  );..    if( !p )
14dba 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
14dbb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
14dbc 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
14dbd 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
14dbe 61 63 68 65 32 2e 78 43 61 63 68 65 73 69 7a 65  ache2.xCachesize
14dbf 28 70 2c 20 6e 75 6d 62 65 72 4f 66 43 61 63 68  (p, numberOfCach
14dc0 65 50 61 67 65 73 28 70 43 61 63 68 65 29 29 3b  ePages(pCache));
14dc1 0d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ..    pCache->pC
14dc2 61 63 68 65 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a  ache = p;..  }..
14dc3 0d 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 72  ..  eCreate = cr
14dc4 65 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20  eateFlag * (1 + 
14dc5 28 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  (!pCache->bPurge
14dc6 61 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d  able || !pCache-
14dc7 3e 70 44 69 72 74 79 29 29 3b 0d 0a 20 20 69 66  >pDirty));..  if
14dc8 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
14dc9 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65 20 3d   ){..    pPage =
14dca 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
14dcb 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 46 65  nfig.pcache2.xFe
14dcc 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63  tch(pCache->pCac
14dcd 68 65 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 74  he, pgno, eCreat
14dce 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  e);..  }....  if
14dcf 28 20 21 70 50 61 67 65 20 26 26 20 65 43 72 65  ( !pPage && eCre
14dd0 61 74 65 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 50  ate==1 ){..    P
14dd1 67 48 64 72 20 2a 70 50 67 3b 0d 0a 0d 0a 20 20  gHdr *pPg;....  
14dd2 20 20 2f 2a 20 46 69 6e 64 20 61 20 64 69 72 74    /* Find a dirt
14dd3 79 20 70 61 67 65 20 74 6f 20 77 72 69 74 65 2d  y page to write-
14dd4 6f 75 74 20 61 6e 64 20 72 65 63 79 63 6c 65 2e  out and recycle.
14dd5 20 46 69 72 73 74 20 74 72 79 20 74 6f 20 66 69   First try to fi
14dd6 6e 64 20 61 20 0d 0a 20 20 20 20 2a 2a 20 70 61  nd a ..    ** pa
14dd7 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ge that does not
14dd8 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e   require a journ
14dd9 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20 77 69 74  al-sync (one wit
14dda 68 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  h PGHDR_NEED_SYN
14ddb 43 0d 0a 20 20 20 20 2a 2a 20 63 6c 65 61 72 65  C..    ** cleare
14ddc 64 29 2c 20 62 75 74 20 69 66 20 74 68 61 74 20  d), but if that 
14ddd 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
14dde 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79 20 6f  settle for any o
14ddf 74 68 65 72 20 0d 0a 20 20 20 20 2a 2a 20 75 6e  ther ..    ** un
14de0 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74 79  referenced dirty
14de1 20 70 61 67 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a   page...    */..
14de2 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73      expensive_as
14de3 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
14de4 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20  kSynced(pCache) 
14de5 29 3b 0d 0a 20 20 20 20 66 6f 72 28 70 50 67 3d  );..    for(pPg=
14de6 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b  pCache->pSynced;
14de7 20 0d 0a 20 20 20 20 20 20 20 20 70 50 67 20 26   ..        pPg &
14de8 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20  & (pPg->nRef || 
14de9 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
14dea 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0d  R_NEED_SYNC)); .
14deb 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67  .        pPg=pPg
14dec 2d 3e 70 44 69 72 74 79 50 72 65 76 0d 0a 20 20  ->pDirtyPrev..  
14ded 20 20 29 3b 0d 0a 20 20 20 20 70 43 61 63 68 65    );..    pCache
14dee 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 67 3b  ->pSynced = pPg;
14def 0d 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29  ..    if( !pPg )
14df0 7b 0d 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67  {..      for(pPg
14df1 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54  =pCache->pDirtyT
14df2 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d  ail; pPg && pPg-
14df3 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e  >nRef; pPg=pPg->
14df4 70 44 69 72 74 79 50 72 65 76 29 3b 0d 0a 20 20  pDirtyPrev);..  
14df5 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 70 50 67    }..    if( pPg
14df6 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72   ){..      int r
14df7 63 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  c;..#ifdef SQLIT
14df8 45 5f 4c 4f 47 5f 43 41 43 48 45 5f 53 50 49 4c  E_LOG_CACHE_SPIL
14df9 4c 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  L..      sqlite3
14dfa 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 46 55 4c 4c  _log(SQLITE_FULL
14dfb 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
14dfc 20 20 20 20 20 20 22 73 70 69 6c 6c 20 70 61 67        "spill pag
14dfd 65 20 25 64 20 6d 61 6b 69 6e 67 20 72 6f 6f 6d  e %d making room
14dfe 20 66 6f 72 20 25 64 20 2d 20 63 61 63 68 65 20   for %d - cache 
14dff 75 73 65 64 3a 20 25 64 2f 25 64 22 2c 0d 0a 20  used: %d/%d",.. 
14e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14e01 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f   pPg->pgno, pgno
14e02 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
14e03 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
14e04 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
14e05 78 50 61 67 65 63 6f 75 6e 74 28 70 43 61 63 68  xPagecount(pCach
14e06 65 2d 3e 70 43 61 63 68 65 29 2c 0d 0a 20 20 20  e->pCache),..   
14e07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
14e08 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61 67 65  umberOfCachePage
14e09 73 28 70 43 61 63 68 65 29 29 3b 0d 0a 23 65 6e  s(pCache));..#en
14e0a 64 69 66 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  dif..      rc = 
14e0b 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 28  pCache->xStress(
14e0c 70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 2c  pCache->pStress,
14e0d 20 70 50 67 29 3b 0d 0a 20 20 20 20 20 20 69 66   pPg);..      if
14e0e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
14e0f 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55  && rc!=SQLITE_BU
14e10 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  SY ){..        r
14e11 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20  eturn rc;..     
14e12 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
14e13 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33   pPage = sqlite3
14e14 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
14e15 63 68 65 32 2e 78 46 65 74 63 68 28 70 43 61 63  che2.xFetch(pCac
14e16 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f  he->pCache, pgno
14e17 2c 20 32 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  , 2);..  }....  
14e18 69 66 28 20 70 50 61 67 65 20 29 7b 0d 0a 20 20  if( pPage ){..  
14e19 20 20 70 50 67 48 64 72 20 3d 20 28 50 67 48 64    pPgHdr = (PgHd
14e1a 72 20 2a 29 70 50 61 67 65 2d 3e 70 45 78 74 72  r *)pPage->pExtr
14e1b 61 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 21 70  a;....    if( !p
14e1c 50 67 48 64 72 2d 3e 70 50 61 67 65 20 29 7b 0d  PgHdr->pPage ){.
14e1d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50  .      memset(pP
14e1e 67 48 64 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  gHdr, 0, sizeof(
14e1f 50 67 48 64 72 29 29 3b 0d 0a 20 20 20 20 20 20  PgHdr));..      
14e20 70 50 67 48 64 72 2d 3e 70 50 61 67 65 20 3d 20  pPgHdr->pPage = 
14e21 70 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 70 50  pPage;..      pP
14e22 67 48 64 72 2d 3e 70 44 61 74 61 20 3d 20 70 50  gHdr->pData = pP
14e23 61 67 65 2d 3e 70 42 75 66 3b 0d 0a 20 20 20 20  age->pBuf;..    
14e24 20 20 70 50 67 48 64 72 2d 3e 70 45 78 74 72 61    pPgHdr->pExtra
14e25 20 3d 20 28 76 6f 69 64 20 2a 29 26 70 50 67 48   = (void *)&pPgH
14e26 64 72 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 6d 65  dr[1];..      me
14e27 6d 73 65 74 28 70 50 67 48 64 72 2d 3e 70 45 78  mset(pPgHdr->pEx
14e28 74 72 61 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e  tra, 0, pCache->
14e29 73 7a 45 78 74 72 61 29 3b 0d 0a 20 20 20 20 20  szExtra);..     
14e2a 20 70 50 67 48 64 72 2d 3e 70 43 61 63 68 65 20   pPgHdr->pCache 
14e2b 3d 20 70 43 61 63 68 65 3b 0d 0a 20 20 20 20 20  = pCache;..     
14e2c 20 70 50 67 48 64 72 2d 3e 70 67 6e 6f 20 3d 20   pPgHdr->pgno = 
14e2d 70 67 6e 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  pgno;..    }..  
14e2e 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 72    assert( pPgHdr
14e2f 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65  ->pCache==pCache
14e30 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
14e31 20 70 50 67 48 64 72 2d 3e 70 67 6e 6f 3d 3d 70   pPgHdr->pgno==p
14e32 67 6e 6f 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  gno );..    asse
14e33 72 74 28 20 70 50 67 48 64 72 2d 3e 70 44 61 74  rt( pPgHdr->pDat
14e34 61 3d 3d 70 50 61 67 65 2d 3e 70 42 75 66 20 29  a==pPage->pBuf )
14e35 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
14e36 50 67 48 64 72 2d 3e 70 45 78 74 72 61 3d 3d 28  PgHdr->pExtra==(
14e37 76 6f 69 64 20 2a 29 26 70 50 67 48 64 72 5b 31  void *)&pPgHdr[1
14e38 5d 20 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20  ] );....    if( 
14e39 30 3d 3d 70 50 67 48 64 72 2d 3e 6e 52 65 66 20  0==pPgHdr->nRef 
14e3a 29 7b 0d 0a 20 20 20 20 20 20 70 43 61 63 68 65  ){..      pCache
14e3b 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20 20 20 7d  ->nRef++;..    }
14e3c 0d 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 6e 52  ..    pPgHdr->nR
14e3d 65 66 2b 2b 3b 0d 0a 20 20 20 20 69 66 28 20 70  ef++;..    if( p
14e3e 67 6e 6f 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20  gno==1 ){..     
14e3f 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20   pCache->pPage1 
14e40 3d 20 70 50 67 48 64 72 3b 0d 0a 20 20 20 20 7d  = pPgHdr;..    }
14e41 0d 0a 20 20 7d 0d 0a 20 20 2a 70 70 50 61 67 65  ..  }..  *ppPage
14e42 20 3d 20 70 50 67 48 64 72 3b 0d 0a 20 20 72 65   = pPgHdr;..  re
14e43 74 75 72 6e 20 28 70 50 67 48 64 72 3d 3d 30 20  turn (pPgHdr==0 
14e44 26 26 20 65 43 72 65 61 74 65 29 20 3f 20 53 51  && eCreate) ? SQ
14e45 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c  LITE_NOMEM : SQL
14e46 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
14e47 0d 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  ..** Decrement t
14e48 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
14e49 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66  nt on a page. If
14e4a 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65   the page is cle
14e4b 61 6e 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 72  an and the..** r
14e4c 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64  eference count d
14e4d 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20  rops to 0, then 
14e4e 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c  it is made elibl
14e4f 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e  e for recycling.
14e50 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
14e51 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14e52 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50  3PcacheRelease(P
14e53 67 48 64 72 20 2a 70 29 7b 0d 0a 20 20 61 73 73  gHdr *p){..  ass
14e54 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
14e55 3b 0d 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0d  ;..  p->nRef--;.
14e56 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d  .  if( p->nRef==
14e57 30 20 29 7b 0d 0a 20 20 20 20 50 43 61 63 68 65  0 ){..    PCache
14e58 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43   *pCache = p->pC
14e59 61 63 68 65 3b 0d 0a 20 20 20 20 70 43 61 63 68  ache;..    pCach
14e5a 65 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a 20 20 20 20  e->nRef--;..    
14e5b 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47  if( (p->flags&PG
14e5c 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b  HDR_DIRTY)==0 ){
14e5d 0d 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e  ..      pcacheUn
14e5e 70 69 6e 28 70 29 3b 0d 0a 20 20 20 20 7d 65 6c  pin(p);..    }el
14e5f 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4d 6f  se{..      /* Mo
14e60 76 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74  ve the page to t
14e61 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64  he head of the d
14e62 69 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0d 0a 20  irty list. */.. 
14e63 20 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76       pcacheRemov
14e64 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70  eFromDirtyList(p
14e65 29 3b 0d 0a 20 20 20 20 20 20 70 63 61 63 68 65  );..      pcache
14e66 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 70  AddToDirtyList(p
14e67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
14e68 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 63 72  }..../*..** Incr
14e69 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
14e6a 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75  ce count of a su
14e6b 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31  pplied page by 1
14e6c 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
14e6d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14e6e 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64  e3PcacheRef(PgHd
14e6f 72 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74  r *p){..  assert
14e70 28 70 2d 3e 6e 52 65 66 3e 30 29 3b 0d 0a 20 20  (p->nRef>0);..  
14e71 70 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 7d 0d 0a 0d  p->nRef++;..}...
14e72 0a 2f 2a 0d 0a 2a 2a 20 44 72 6f 70 20 61 20 70  ./*..** Drop a p
14e73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63  age from the cac
14e74 68 65 2e 20 54 68 65 72 65 20 6d 75 73 74 20 62  he. There must b
14e75 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 72 65  e exactly one re
14e76 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 0d 0a  ference to the..
14e77 2a 2a 20 70 61 67 65 2e 20 54 68 69 73 20 66 75  ** page. This fu
14e78 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74  nction deletes t
14e79 68 61 74 20 72 65 66 65 72 65 6e 63 65 2c 20 73  hat reference, s
14e7a 6f 20 61 66 74 65 72 20 69 74 20 72 65 74 75 72  o after it retur
14e7b 6e 73 20 74 68 65 0d 0a 2a 2a 20 70 61 67 65 20  ns the..** page 
14e7c 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 20  pointed to by p 
14e7d 69 73 20 69 6e 76 61 6c 69 64 2e 0d 0a 2a 2f 0d  is invalid...*/.
14e7e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14e7f 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
14e80 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29  heDrop(PgHdr *p)
14e81 7b 0d 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  {..  PCache *pCa
14e82 63 68 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  che;..  assert( 
14e83 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0d 0a 20  p->nRef==1 );.. 
14e84 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47   if( p->flags&PG
14e85 48 44 52 5f 44 49 52 54 59 20 29 7b 0d 0a 20 20  HDR_DIRTY ){..  
14e86 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72    pcacheRemoveFr
14e87 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0d  omDirtyList(p);.
14e88 0a 20 20 7d 0d 0a 20 20 70 43 61 63 68 65 20 3d  .  }..  pCache =
14e89 20 70 2d 3e 70 43 61 63 68 65 3b 0d 0a 20 20 70   p->pCache;..  p
14e8a 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0d 0a  Cache->nRef--;..
14e8b 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31    if( p->pgno==1
14e8c 20 29 7b 0d 0a 20 20 20 20 70 43 61 63 68 65 2d   ){..    pCache-
14e8d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0d 0a 20 20  >pPage1 = 0;..  
14e8e 7d 0d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  }..  sqlite3Glob
14e8f 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32  alConfig.pcache2
14e90 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e  .xUnpin(pCache->
14e91 70 43 61 63 68 65 2c 20 70 2d 3e 70 50 61 67 65  pCache, p->pPage
14e92 2c 20 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  , 1);..}..../*..
14e93 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
14e94 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20   page is marked 
14e95 61 73 20 64 69 72 74 79 2e 20 49 66 20 69 74 20  as dirty. If it 
14e96 69 73 6e 27 74 20 64 69 72 74 79 20 61 6c 72 65  isn't dirty alre
14e97 61 64 79 2c 0d 0a 2a 2a 20 6d 61 6b 65 20 69 74  ady,..** make it
14e98 20 73 6f 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45   so...*/..SQLITE
14e99 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14e9a 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
14e9b 69 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b 0d  irty(PgHdr *p){.
14e9c 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  .  p->flags &= ~
14e9d 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
14e9e 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
14e9f 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 69 66 28  nRef>0 );..  if(
14ea0 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 26 20   0==(p->flags & 
14ea1 50 47 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0d  PGHDR_DIRTY) ){.
14ea2 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d  .    p->flags |=
14ea3 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0d 0a 20   PGHDR_DIRTY;.. 
14ea4 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69     pcacheAddToDi
14ea5 72 74 79 4c 69 73 74 28 20 70 29 3b 0d 0a 20 20  rtyList( p);..  
14ea6 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d  }..}..../*..** M
14ea7 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67  ake sure the pag
14ea8 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63  e is marked as c
14ea9 6c 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27  lean. If it isn'
14eaa 74 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c  t clean already,
14eab 0d 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e  ..** make it so.
14eac 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
14ead 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14eae 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
14eaf 28 50 67 48 64 72 20 2a 70 29 7b 0d 0a 20 20 69  (PgHdr *p){..  i
14eb0 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 50  f( (p->flags & P
14eb1 47 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0d 0a  GHDR_DIRTY) ){..
14eb2 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65      pcacheRemove
14eb3 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29  FromDirtyList(p)
14eb4 3b 0d 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ;..    p->flags 
14eb5 26 3d 20 7e 28 50 47 48 44 52 5f 44 49 52 54 59  &= ~(PGHDR_DIRTY
14eb6 7c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  |PGHDR_NEED_SYNC
14eb7 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  );..    if( p->n
14eb8 52 65 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Ref==0 ){..     
14eb9 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b   pcacheUnpin(p);
14eba 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d  ..    }..  }..}.
14ebb 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65 20 65  .../*..** Make e
14ebc 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
14ebd 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0d 0a 2a   cache clean...*
14ebe 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
14ebf 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
14ec0 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61  acheCleanAll(PCa
14ec1 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0d 0a 20  che *pCache){.. 
14ec2 20 50 67 48 64 72 20 2a 70 3b 0d 0a 20 20 77 68   PgHdr *p;..  wh
14ec3 69 6c 65 28 20 28 70 20 3d 20 70 43 61 63 68 65  ile( (p = pCache
14ec4 2d 3e 70 44 69 72 74 79 29 21 3d 30 20 29 7b 0d  ->pDirty)!=0 ){.
14ec5 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
14ec6 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0d  heMakeClean(p);.
14ec7 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
14ec8 2a 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44  * Clear the PGHD
14ec9 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
14eca 20 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20   from all dirty 
14ecb 70 61 67 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  pages...*/..SQLI
14ecc 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14ecd 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
14ece 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63  arSyncFlags(PCac
14ecf 68 65 20 2a 70 43 61 63 68 65 29 7b 0d 0a 20 20  he *pCache){..  
14ed0 50 67 48 64 72 20 2a 70 3b 0d 0a 20 20 66 6f 72  PgHdr *p;..  for
14ed1 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
14ed2 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
14ed3 79 4e 65 78 74 29 7b 0d 0a 20 20 20 20 70 2d 3e  yNext){..    p->
14ed4 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f  flags &= ~PGHDR_
14ed5 4e 45 45 44 5f 53 59 4e 43 3b 0d 0a 20 20 7d 0d  NEED_SYNC;..  }.
14ed6 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63  .  pCache->pSync
14ed7 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69  ed = pCache->pDi
14ed8 72 74 79 54 61 69 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f  rtyTail;..}..../
14ed9 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  *..** Change the
14eda 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
14edb 70 61 67 65 20 70 20 74 6f 20 6e 65 77 50 67 6e  page p to newPgn
14edc 6f 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  o. ..*/..SQLITE_
14edd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14ede 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50  ite3PcacheMove(P
14edf 67 48 64 72 20 2a 70 2c 20 50 67 6e 6f 20 6e 65  gHdr *p, Pgno ne
14ee0 77 50 67 6e 6f 29 7b 0d 0a 20 20 50 43 61 63 68  wPgno){..  PCach
14ee1 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70  e *pCache = p->p
14ee2 43 61 63 68 65 3b 0d 0a 20 20 61 73 73 65 72 74  Cache;..  assert
14ee3 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0d 0a  ( p->nRef>0 );..
14ee4 20 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 6e    assert( newPgn
14ee5 6f 3e 30 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  o>0 );..  sqlite
14ee6 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
14ee7 61 63 68 65 32 2e 78 52 65 6b 65 79 28 70 43 61  ache2.xRekey(pCa
14ee8 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2d 3e  che->pCache, p->
14ee9 70 50 61 67 65 2c 20 70 2d 3e 70 67 6e 6f 2c 6e  pPage, p->pgno,n
14eea 65 77 50 67 6e 6f 29 3b 0d 0a 20 20 70 2d 3e 70  ewPgno);..  p->p
14eeb 67 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0d 0a  gno = newPgno;..
14eec 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26    if( (p->flags&
14eed 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 26 20  PGHDR_DIRTY) && 
14eee 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
14eef 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0d 0a 20  NEED_SYNC) ){.. 
14ef0 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
14ef1 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b  romDirtyList(p);
14ef2 0d 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54  ..    pcacheAddT
14ef3 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0d 0a  oDirtyList(p);..
14ef4 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
14ef5 20 44 72 6f 70 20 65 76 65 72 79 20 63 61 63 68   Drop every cach
14ef6 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20 70 61  e entry whose pa
14ef7 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65  ge number is gre
14ef8 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e 6f 22  ater than "pgno"
14ef9 2e 20 54 68 65 0d 0a 2a 2a 20 63 61 6c 6c 65 72  . The..** caller
14efa 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61   must ensure tha
14efb 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f  t there are no o
14efc 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
14efd 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67  ences to any pag
14efe 65 73 0d 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61  es..** other tha
14eff 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 20  n page 1 with a 
14f00 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61  page number grea
14f01 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0d 0a  ter than pgno...
14f02 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  **..** If there 
14f03 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  is a reference t
14f04 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65  o page 1 and the
14f05 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20   pgno parameter 
14f06 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0d 0a  passed to this..
14f07 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30  ** function is 0
14f08 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20  , then the data 
14f09 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20  area associated 
14f0a 77 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a  with page 1 is z
14f0b 65 72 6f 65 64 2c 20 62 75 74 0d 0a 2a 2a 20 74  eroed, but..** t
14f0c 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  he page object i
14f0d 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0d 0a  s not dropped...
14f0e 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
14f0f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
14f10 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43  cacheTruncate(PC
14f11 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67  ache *pCache, Pg
14f12 6e 6f 20 70 67 6e 6f 29 7b 0d 0a 20 20 69 66 28  no pgno){..  if(
14f13 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
14f14 29 7b 0d 0a 20 20 20 20 50 67 48 64 72 20 2a 70  ){..    PgHdr *p
14f15 3b 0d 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e  ;..    PgHdr *pN
14f16 65 78 74 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d  ext;..    for(p=
14f17 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
14f18 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0d 0a 20 20  p; p=pNext){..  
14f19 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
14f1a 44 69 72 74 79 4e 65 78 74 3b 0d 0a 20 20 20 20  DirtyNext;..    
14f1b 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
14f1c 65 20 6e 65 76 65 72 20 67 65 74 73 20 63 61 6c  e never gets cal
14f1d 6c 20 77 69 74 68 20 61 20 70 6f 73 69 74 69 76  l with a positiv
14f1e 65 20 70 67 6e 6f 20 65 78 63 65 70 74 20 72 69  e pgno except ri
14f1f 67 68 74 0d 0a 20 20 20 20 20 20 2a 2a 20 61 66  ght..      ** af
14f20 74 65 72 20 73 71 6c 69 74 65 33 50 63 61 63 68  ter sqlite3Pcach
14f21 65 43 6c 65 61 6e 41 6c 6c 28 29 2e 20 20 53 6f  eCleanAll().  So
14f22 20 69 66 20 74 68 65 72 65 20 61 72 65 20 64 69   if there are di
14f23 72 74 79 20 70 61 67 65 73 2c 0d 0a 20 20 20 20  rty pages,..    
14f24 20 20 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 20    ** it must be 
14f25 74 68 61 74 20 70 67 6e 6f 3d 3d 30 2e 0d 0a 20  that pgno==0... 
14f26 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 61       */..      a
14f27 73 73 65 72 74 28 20 70 2d 3e 70 67 6e 6f 3e 30  ssert( p->pgno>0
14f28 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 41   );..      if( A
14f29 4c 57 41 59 53 28 70 2d 3e 70 67 6e 6f 3e 70 67  LWAYS(p->pgno>pg
14f2a 6e 6f 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  no) ){..        
14f2b 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73  assert( p->flags
14f2c 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0d  &PGHDR_DIRTY );.
14f2d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
14f2e 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
14f2f 70 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  p);..      }..  
14f30 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 70 67 6e    }..    if( pgn
14f31 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e  o==0 && pCache->
14f32 70 50 61 67 65 31 20 29 7b 0d 0a 20 20 20 20 20  pPage1 ){..     
14f33 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e   memset(pCache->
14f34 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30  pPage1->pData, 0
14f35 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  , pCache->szPage
14f36 29 3b 0d 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d  );..      pgno =
14f37 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   1;..    }..    
14f38 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
14f39 66 69 67 2e 70 63 61 63 68 65 32 2e 78 54 72 75  fig.pcache2.xTru
14f3a 6e 63 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43  ncate(pCache->pC
14f3b 61 63 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0d 0a  ache, pgno+1);..
14f3c 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
14f3d 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0d   Close a cache..
14f3e 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
14f3f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14f40 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63  PcacheClose(PCac
14f41 68 65 20 2a 70 43 61 63 68 65 29 7b 0d 0a 20 20  he *pCache){..  
14f42 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63  if( pCache->pCac
14f43 68 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  he ){..    sqlit
14f44 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
14f45 63 61 63 68 65 32 2e 78 44 65 73 74 72 6f 79 28  cache2.xDestroy(
14f46 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
14f47 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  ..  }..}..../* .
14f48 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20  .** Discard the 
14f49 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
14f4a 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  cache...*/..SQLI
14f4b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14f4c 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
14f4d 61 72 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  ar(PCache *pCach
14f4e 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 50 63  e){..  sqlite3Pc
14f4f 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 43 61  acheTruncate(pCa
14f50 63 68 65 2c 20 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  che, 0);..}..../
14f51 2a 0d 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20  *..** Merge two 
14f52 6c 69 73 74 73 20 6f 66 20 70 61 67 65 73 20 63  lists of pages c
14f53 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 44 69 72  onnected by pDir
14f54 74 79 20 61 6e 64 20 69 6e 20 70 67 6e 6f 20 6f  ty and in pgno o
14f55 72 64 65 72 2e 0d 0a 2a 2a 20 44 6f 20 6e 6f 74  rder...** Do not
14f56 20 62 6f 74 68 20 66 69 78 69 6e 67 20 74 68 65   both fixing the
14f57 20 70 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e   pDirtyPrev poin
14f58 74 65 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ters...*/..stati
14f59 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 4d  c PgHdr *pcacheM
14f5a 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50 67  ergeDirtyList(Pg
14f5b 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 2a  Hdr *pA, PgHdr *
14f5c 70 42 29 7b 0d 0a 20 20 50 67 48 64 72 20 72 65  pB){..  PgHdr re
14f5d 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0d 0a 20  sult, *pTail;.. 
14f5e 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74   pTail = &result
14f5f 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26  ;..  while( pA &
14f60 26 20 70 42 20 29 7b 0d 0a 20 20 20 20 69 66 28  & pB ){..    if(
14f61 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67   pA->pgno<pB->pg
14f62 6e 6f 20 29 7b 0d 0a 20 20 20 20 20 20 70 54 61  no ){..      pTa
14f63 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b  il->pDirty = pA;
14f64 0d 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ..      pTail = 
14f65 70 41 3b 0d 0a 20 20 20 20 20 20 70 41 20 3d 20  pA;..      pA = 
14f66 70 41 2d 3e 70 44 69 72 74 79 3b 0d 0a 20 20 20  pA->pDirty;..   
14f67 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70   }else{..      p
14f68 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
14f69 42 3b 0d 0a 20 20 20 20 20 20 70 54 61 69 6c 20  B;..      pTail 
14f6a 3d 20 70 42 3b 0d 0a 20 20 20 20 20 20 70 42 20  = pB;..      pB 
14f6b 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0d 0a 20  = pB->pDirty;.. 
14f6c 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28     }..  }..  if(
14f6d 20 70 41 20 29 7b 0d 0a 20 20 20 20 70 54 61 69   pA ){..    pTai
14f6e 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0d  l->pDirty = pA;.
14f6f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 20  .  }else if( pB 
14f70 29 7b 0d 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70  ){..    pTail->p
14f71 44 69 72 74 79 20 3d 20 70 42 3b 0d 0a 20 20 7d  Dirty = pB;..  }
14f72 65 6c 73 65 7b 0d 0a 20 20 20 20 70 54 61 69 6c  else{..    pTail
14f73 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0d 0a 20  ->pDirty = 0;.. 
14f74 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 65 73   }..  return res
14f75 75 6c 74 2e 70 44 69 72 74 79 3b 0d 0a 7d 0d 0a  ult.pDirty;..}..
14f76 0d 0a 2f 2a 0d 0a 2a 2a 20 53 6f 72 74 20 74 68  ../*..** Sort th
14f77 65 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20  e list of pages 
14f78 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64  in accending ord
14f79 65 72 20 62 79 20 70 67 6e 6f 2e 20 20 50 61 67  er by pgno.  Pag
14f7a 65 73 20 61 72 65 0d 0a 2a 2a 20 63 6f 6e 6e 65  es are..** conne
14f7b 63 74 65 64 20 62 79 20 70 44 69 72 74 79 20 70  cted by pDirty p
14f7c 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20 70 44  ointers.  The pD
14f7d 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 65 72  irtyPrev pointer
14f7e 73 20 61 72 65 0d 0a 2a 2a 20 63 6f 72 72 75 70  s are..** corrup
14f7f 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72 74  ted by this sort
14f80 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 69 6e 63 65 20  ...**..** Since 
14f81 74 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20  there cannot be 
14f82 6d 6f 72 65 20 74 68 61 6e 20 32 5e 33 31 20 64  more than 2^31 d
14f83 69 73 74 69 6e 63 74 20 70 61 67 65 73 20 69 6e  istinct pages in
14f84 20 61 20 64 61 74 61 62 61 73 65 2c 0d 0a 2a 2a   a database,..**
14f85 20 74 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65   there cannot be
14f86 20 6d 6f 72 65 20 74 68 61 6e 20 33 31 20 62 75   more than 31 bu
14f87 63 6b 65 74 73 20 72 65 71 75 69 72 65 64 20 62  ckets required b
14f88 79 20 74 68 65 20 6d 65 72 67 65 20 73 6f 72 74  y the merge sort
14f89 65 72 2e 0d 0a 2a 2a 20 4f 6e 65 20 65 78 74 72  er...** One extr
14f8a 61 20 62 75 63 6b 65 74 20 69 73 20 61 64 64 65  a bucket is adde
14f8b 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65 72 66  d to catch overf
14f8c 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 6f 6d 65  low in case some
14f8d 74 68 69 6e 67 0d 0a 2a 2a 20 65 76 65 72 20 63  thing..** ever c
14f8e 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 20 74  hanges to make t
14f8f 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74  he previous sent
14f90 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 2e 0d  ence incorrect..
14f91 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4e 5f 53  .*/..#define N_S
14f92 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0d 0a  ORT_BUCKET  32..
14f93 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63  static PgHdr *pc
14f94 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73  acheSortDirtyLis
14f95 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0d 0a  t(PgHdr *pIn){..
14f96 20 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52    PgHdr *a[N_SOR
14f97 54 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0d 0a  T_BUCKET], *p;..
14f98 20 20 69 6e 74 20 69 3b 0d 0a 20 20 6d 65 6d 73    int i;..  mems
14f99 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(a, 0, sizeof(
14f9a 61 29 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70  a));..  while( p
14f9b 49 6e 20 29 7b 0d 0a 20 20 20 20 70 20 3d 20 70  In ){..    p = p
14f9c 49 6e 3b 0d 0a 20 20 20 20 70 49 6e 20 3d 20 70  In;..    pIn = p
14f9d 2d 3e 70 44 69 72 74 79 3b 0d 0a 20 20 20 20 70  ->pDirty;..    p
14f9e 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0d 0a 20  ->pDirty = 0;.. 
14f9f 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 41     for(i=0; ALWA
14fa0 59 53 28 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b  YS(i<N_SORT_BUCK
14fa1 45 54 2d 31 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20  ET-1); i++){..  
14fa2 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20      if( a[i]==0 
14fa3 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 5b 69 5d  ){..        a[i]
14fa4 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20 62   = p;..        b
14fa5 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  reak;..      }el
14fa6 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 20 3d  se{..        p =
14fa7 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
14fa8 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0d  yList(a[i], p);.
14fa9 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20  .        a[i] = 
14faa 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  0;..      }..   
14fab 20 7d 0d 0a 20 20 20 20 69 66 28 20 4e 45 56 45   }..    if( NEVE
14fac 52 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b  R(i==N_SORT_BUCK
14fad 45 54 2d 31 29 20 29 7b 0d 0a 20 20 20 20 20 20  ET-1) ){..      
14fae 2f 2a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20  /* To get here, 
14faf 74 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65  there need to be
14fb0 20 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45   2^(N_SORT_BUCKE
14fb1 54 29 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0d 0a  T) elements in..
14fb2 20 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 70        ** the inp
14fb3 75 74 20 6c 69 73 74 2e 20 20 42 75 74 20 74 68  ut list.  But th
14fb4 61 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65  at is impossible
14fb5 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
14fb6 20 20 20 61 5b 69 5d 20 3d 20 70 63 61 63 68 65     a[i] = pcache
14fb7 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 61  MergeDirtyList(a
14fb8 5b 69 5d 2c 20 70 29 3b 0d 0a 20 20 20 20 7d 0d  [i], p);..    }.
14fb9 0a 20 20 7d 0d 0a 20 20 70 20 3d 20 61 5b 30 5d  .  }..  p = a[0]
14fba 3b 0d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  ;..  for(i=1; i<
14fbb 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69  N_SORT_BUCKET; i
14fbc 2b 2b 29 7b 0d 0a 20 20 20 20 70 20 3d 20 70 63  ++){..    p = pc
14fbd 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
14fbe 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0d 0a 20 20  st(p, a[i]);..  
14fbf 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a  }..  return p;..
14fc0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
14fc1 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c  rn a list of all
14fc2 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20   dirty pages in 
14fc3 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65  the cache, sorte
14fc4 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72  d by page number
14fc5 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
14fc6 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c  IVATE PgHdr *sql
14fc7 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
14fc8 69 73 74 28 50 43 61 63 68 65 20 2a 70 43 61 63  ist(PCache *pCac
14fc9 68 65 29 7b 0d 0a 20 20 50 67 48 64 72 20 2a 70  he){..  PgHdr *p
14fca 3b 0d 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  ;..  for(p=pCach
14fcb 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  e->pDirty; p; p=
14fcc 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 29 7b 0d  p->pDirtyNext){.
14fcd 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d  .    p->pDirty =
14fce 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0d   p->pDirtyNext;.
14fcf 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
14fd0 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69  cacheSortDirtyLi
14fd1 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 72 74  st(pCache->pDirt
14fd2 79 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  y);..}..../* ..*
14fd3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
14fd4 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66  al number of ref
14fd5 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 68 65  erenced pages he
14fd6 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e  ld by the cache.
14fd7 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
14fd8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14fd9 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50  PcacheRefCount(P
14fda 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0d  Cache *pCache){.
14fdb 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65  .  return pCache
14fdc 2d 3e 6e 52 65 66 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ->nRef;..}..../*
14fdd 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
14fde 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  number of refere
14fdf 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
14fe0 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20   supplied as an 
14fe1 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53  argument...*/..S
14fe2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14fe3 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  t sqlite3PcacheP
14fe4 61 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64  ageRefcount(PgHd
14fe5 72 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e  r *p){..  return
14fe6 20 70 2d 3e 6e 52 65 66 3b 0d 0a 7d 0d 0a 0d 0a   p->nRef;..}....
14fe7 2f 2a 20 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  /* ..** Return t
14fe8 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
14fe9 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
14fea 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  cache...*/..SQLI
14feb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14fec 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
14fed 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43  count(PCache *pC
14fee 61 63 68 65 29 7b 0d 0a 20 20 69 6e 74 20 6e 50  ache){..  int nP
14fef 61 67 65 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20  age = 0;..  if( 
14ff0 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29  pCache->pCache )
14ff1 7b 0d 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73  {..    nPage = s
14ff2 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
14ff3 69 67 2e 70 63 61 63 68 65 32 2e 78 50 61 67 65  ig.pcache2.xPage
14ff4 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43  count(pCache->pC
14ff5 61 63 68 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  ache);..  }..  r
14ff6 65 74 75 72 6e 20 6e 50 61 67 65 3b 0d 0a 7d 0d  eturn nPage;..}.
14ff7 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
14ff8 5f 54 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  _TEST../*..** Ge
14ff9 74 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20  t the suggested 
14ffa 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65  cache-size value
14ffb 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
14ffc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14ffd 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73  3PcacheGetCaches
14ffe 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ize(PCache *pCac
14fff 68 65 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 6e  he){..  return n
15000 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61 67 65  umberOfCachePage
15001 73 28 70 43 61 63 68 65 29 3b 0d 0a 7d 0d 0a 23  s(pCache);..}..#
15002 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
15003 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65  Set the suggeste
15004 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c  d cache-size val
15005 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ue...*/..SQLITE_
15006 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
15007 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
15008 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70  hesize(PCache *p
15009 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67  Cache, int mxPag
1500a 65 29 7b 0d 0a 20 20 70 43 61 63 68 65 2d 3e 73  e){..  pCache->s
1500b 7a 43 61 63 68 65 20 3d 20 6d 78 50 61 67 65 3b  zCache = mxPage;
1500c 0d 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ..  if( pCache->
1500d 70 43 61 63 68 65 20 29 7b 0d 0a 20 20 20 20 73  pCache ){..    s
1500e 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
1500f 69 67 2e 70 63 61 63 68 65 32 2e 78 43 61 63 68  ig.pcache2.xCach
15010 65 73 69 7a 65 28 70 43 61 63 68 65 2d 3e 70 43  esize(pCache->pC
15011 61 63 68 65 2c 0d 0a 20 20 20 20 20 20 20 20 20  ache,..         
15012 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15013 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15014 20 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50    numberOfCacheP
15015 61 67 65 73 28 70 43 61 63 68 65 29 29 3b 0d 0a  ages(pCache));..
15016 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
15017 20 46 72 65 65 20 75 70 20 61 73 20 6d 75 63 68   Free up as much
15018 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69   memory as possi
15019 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  ble from the pag
1501a 65 20 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51  e cache...*/..SQ
1501b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1501c 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
1501d 68 72 69 6e 6b 28 50 43 61 63 68 65 20 2a 70 43  hrink(PCache *pC
1501e 61 63 68 65 29 7b 0d 0a 20 20 69 66 28 20 70 43  ache){..  if( pC
1501f 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0d  ache->pCache ){.
15020 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
15021 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32  alConfig.pcache2
15022 2e 78 53 68 72 69 6e 6b 28 70 43 61 63 68 65 2d  .xShrink(pCache-
15023 3e 70 43 61 63 68 65 29 3b 0d 0a 20 20 7d 0d 0a  >pCache);..  }..
15024 7d 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64  }....#if defined
15025 28 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41  (SQLITE_CHECK_PA
15026 47 45 53 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  GES) || defined(
15027 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0d 0a 2f  SQLITE_DEBUG)../
15028 2a 0d 0a 2a 2a 20 46 6f 72 20 61 6c 6c 20 64 69  *..** For all di
15029 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e  rty pages curren
1502a 74 6c 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  tly in the cache
1502b 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 70 65  , invoke the spe
1502c 63 69 66 69 65 64 0d 0a 2a 2a 20 63 61 6c 6c 62  cified..** callb
1502d 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c  ack. This is onl
1502e 79 20 75 73 65 64 20 69 66 20 74 68 65 20 53 51  y used if the SQ
1502f 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
15030 20 6d 61 63 72 6f 20 69 73 0d 0a 2a 2a 20 64 65   macro is..** de
15031 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  fined...*/..SQLI
15032 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15033 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
15034 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65  rateDirty(PCache
15035 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28   *pCache, void (
15036 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29  *xIter)(PgHdr *)
15037 29 7b 0d 0a 20 20 50 67 48 64 72 20 2a 70 44 69  ){..  PgHdr *pDi
15038 72 74 79 3b 0d 0a 20 20 66 6f 72 28 70 44 69 72  rty;..  for(pDir
15039 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  ty=pCache->pDirt
1503a 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72 74  y; pDirty; pDirt
1503b 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74 79  y=pDirty->pDirty
1503c 4e 65 78 74 29 7b 0d 0a 20 20 20 20 78 49 74 65  Next){..    xIte
1503d 72 28 70 44 69 72 74 79 29 3b 0d 0a 20 20 7d 0d  r(pDirty);..  }.
1503e 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  .}..#endif..../*
1503f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
15040 64 20 6f 66 20 70 63 61 63 68 65 2e 63 20 2a 2a  d of pcache.c **
15041 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15042 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15043 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
15044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
15045 65 67 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65  egin file pcache
15046 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  1.c ************
15047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
15049 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 65  /*..** 2008 Nove
1504a 6d 62 65 72 20 30 35 0d 0a 2a 2a 0d 0a 2a 2a 20  mber 05..**..** 
1504b 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1504c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1504d 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1504e 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1504f 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  ..** a legal not
15050 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
15051 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  lessing:..**..**
15052 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
15053 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
15054 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
15055 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
15056 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
15057 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
15058 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  s...**    May yo
15059 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1505a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1505b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1505c 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
1505d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1505e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1505f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
15061 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .**..** This fil
15062 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
15063 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61   default page ca
15064 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
15065 6f 6e 20 28 74 68 65 0d 0a 2a 2a 20 73 71 6c 69  on (the..** sqli
15066 74 65 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72  te3_pcache inter
15067 66 61 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63  face). It also c
15068 6f 6e 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20  ontains part of 
15069 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
1506a 6f 6e 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51  on..** of the SQ
1506b 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
1506c 43 41 43 48 45 20 61 6e 64 20 73 71 6c 69 74 65  CACHE and sqlite
1506d 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
1506e 28 29 20 66 65 61 74 75 72 65 73 2e 0d 0a 2a 2a  () features...**
1506f 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
15070 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
15071 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6f 76 65  mentation is ove
15072 72 72 69 64 65 6e 2c 20 74 68 65 6e 20 6e 65 69  rriden, then nei
15073 74 68 65 72 20 6f 66 0d 0a 2a 2a 20 74 68 65 73  ther of..** thes
15074 65 20 74 77 6f 20 66 65 61 74 75 72 65 73 20 61  e two features a
15075 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0d 0a 2a  re available...*
15076 2f 0d 0a 0d 0a 0d 0a 74 79 70 65 64 65 66 20 73  /......typedef s
15077 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 43  truct PCache1 PC
15078 61 63 68 65 31 3b 0d 0a 74 79 70 65 64 65 66 20  ache1;..typedef 
15079 73 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67  struct PgHdr1 Pg
1507a 48 64 72 31 3b 0d 0a 74 79 70 65 64 65 66 20 73  Hdr1;..typedef s
1507b 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74  truct PgFreeslot
1507c 20 50 67 46 72 65 65 73 6c 6f 74 3b 0d 0a 74 79   PgFreeslot;..ty
1507d 70 65 64 65 66 20 73 74 72 75 63 74 20 50 47 72  pedef struct PGr
1507e 6f 75 70 20 50 47 72 6f 75 70 3b 0d 0a 0d 0a 2f  oup PGroup;..../
1507f 2a 20 45 61 63 68 20 70 61 67 65 20 63 61 63 68  * Each page cach
15080 65 20 28 6f 72 20 50 43 61 63 68 65 29 20 62 65  e (or PCache) be
15081 6c 6f 6e 67 73 20 74 6f 20 61 20 50 47 72 6f 75  longs to a PGrou
15082 70 2e 20 20 41 20 50 47 72 6f 75 70 20 69 73 20  p.  A PGroup is 
15083 61 20 73 65 74 20 0d 0a 2a 2a 20 6f 66 20 6f 6e  a set ..** of on
15084 65 20 6f 72 20 6d 6f 72 65 20 50 43 61 63 68 65  e or more PCache
15085 73 20 74 68 61 74 20 61 72 65 20 61 62 6c 65 20  s that are able 
15086 74 6f 20 72 65 63 79 63 6c 65 20 65 61 63 68 20  to recycle each 
15087 6f 74 68 65 72 73 20 75 6e 70 69 6e 6e 65 64 0d  others unpinned.
15088 0a 2a 2a 20 70 61 67 65 73 20 77 68 65 6e 20 74  .** pages when t
15089 68 65 79 20 61 72 65 20 75 6e 64 65 72 20 6d 65  hey are under me
1508a 6d 6f 72 79 20 70 72 65 73 73 75 72 65 2e 20 20  mory pressure.  
1508b 41 20 50 47 72 6f 75 70 20 69 73 20 61 6e 20 69  A PGroup is an i
1508c 6e 73 74 61 6e 63 65 20 6f 66 0d 0a 2a 2a 20 74  nstance of..** t
1508d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a  he following obj
1508e 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ect...**..** Thi
1508f 73 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  s page cache imp
15090 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 6f 72 6b  lementation work
15091 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 77 6f 20  s in one of two 
15092 6d 6f 64 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  modes:..**..**  
15093 20 28 31 29 20 20 45 76 65 72 79 20 50 43 61 63   (1)  Every PCac
15094 68 65 20 69 73 20 74 68 65 20 73 6f 6c 65 20 6d  he is the sole m
15095 65 6d 62 65 72 20 6f 66 20 69 74 73 20 6f 77 6e  ember of its own
15096 20 50 47 72 6f 75 70 2e 20 20 54 68 65 72 65 20   PGroup.  There 
15097 69 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 6f 6e  is..**        on
15098 65 20 50 47 72 6f 75 70 20 70 65 72 20 50 43 61  e PGroup per PCa
15099 63 68 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28  che...**..**   (
1509a 32 29 20 20 54 68 65 72 65 20 69 73 20 61 20 73  2)  There is a s
1509b 69 6e 67 6c 65 20 67 6c 6f 62 61 6c 20 50 47 72  ingle global PGr
1509c 6f 75 70 20 74 68 61 74 20 61 6c 6c 20 50 43 61  oup that all PCa
1509d 63 68 65 73 20 61 72 65 20 61 20 6d 65 6d 62 65  ches are a membe
1509e 72 0d 0a 2a 2a 20 20 20 20 20 20 20 20 6f 66 2e  r..**        of.
1509f 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 64 65 20 31 20  ..**..** Mode 1 
150a0 75 73 65 73 20 6d 6f 72 65 20 6d 65 6d 6f 72 79  uses more memory
150a1 20 28 73 69 6e 63 65 20 50 43 61 63 68 65 20 69   (since PCache i
150a2 6e 73 74 61 6e 63 65 73 20 61 72 65 20 6e 6f 74  nstances are not
150a3 20 61 62 6c 65 20 74 6f 20 72 6f 62 0d 0a 2a 2a   able to rob..**
150a4 20 75 6e 75 73 65 64 20 70 61 67 65 73 20 66 72   unused pages fr
150a5 6f 6d 20 6f 74 68 65 72 20 50 43 61 63 68 65 73  om other PCaches
150a6 29 20 62 75 74 20 69 74 20 61 6c 73 6f 20 6f 70  ) but it also op
150a7 65 72 61 74 65 73 20 77 69 74 68 6f 75 74 20 61  erates without a
150a8 20 6d 75 74 65 78 2c 0d 0a 2a 2a 20 61 6e 64 20   mutex,..** and 
150a9 69 73 20 74 68 65 72 65 66 6f 72 65 20 6f 66 74  is therefore oft
150aa 65 6e 20 66 61 73 74 65 72 2e 20 20 4d 6f 64 65  en faster.  Mode
150ab 20 32 20 72 65 71 75 69 72 65 73 20 61 20 6d 75   2 requires a mu
150ac 74 65 78 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  tex in order to 
150ad 62 65 0d 0a 2a 2a 20 74 68 72 65 61 64 73 61 66  be..** threadsaf
150ae 65 2c 20 62 75 74 20 72 65 63 79 63 6c 65 73 20  e, but recycles 
150af 70 61 67 65 73 20 6d 6f 72 65 20 65 66 66 69 63  pages more effic
150b0 69 65 6e 74 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  iently...**..** 
150b1 46 6f 72 20 6d 6f 64 65 20 28 31 29 2c 20 50 47  For mode (1), PG
150b2 72 6f 75 70 2e 6d 75 74 65 78 20 69 73 20 4e 55  roup.mutex is NU
150b3 4c 4c 2e 20 20 46 6f 72 20 6d 6f 64 65 20 28 32  LL.  For mode (2
150b4 29 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  ) there is only 
150b5 61 20 73 69 6e 67 6c 65 0d 0a 2a 2a 20 50 47 72  a single..** PGr
150b6 6f 75 70 20 77 68 69 63 68 20 69 73 20 74 68 65  oup which is the
150b7 20 70 63 61 63 68 65 31 2e 67 72 70 20 67 6c 6f   pcache1.grp glo
150b8 62 61 6c 20 76 61 72 69 61 62 6c 65 20 61 6e 64  bal variable and
150b9 20 69 74 73 20 6d 75 74 65 78 20 69 73 0d 0a 2a   its mutex is..*
150ba 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  * SQLITE_MUTEX_S
150bb 54 41 54 49 43 5f 4c 52 55 2e 0d 0a 2a 2f 0d 0a  TATIC_LRU...*/..
150bc 73 74 72 75 63 74 20 50 47 72 6f 75 70 20 7b 0d  struct PGroup {.
150bd 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
150be 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20   *mutex;        
150bf 20 20 2f 2a 20 4d 55 54 45 58 5f 53 54 41 54 49    /* MUTEX_STATI
150c0 43 5f 4c 52 55 20 6f 72 20 4e 55 4c 4c 20 2a 2f  C_LRU or NULL */
150c1 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
150c2 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 20 20   nMaxPage;      
150c3 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61     /* Sum of nMa
150c4 78 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20  x for purgeable 
150c5 63 61 63 68 65 73 20 2a 2f 0d 0a 20 20 75 6e 73  caches */..  uns
150c6 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 50 61  igned int nMinPa
150c7 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ge;         /* S
150c8 75 6d 20 6f 66 20 6e 4d 69 6e 20 66 6f 72 20 70  um of nMin for p
150c9 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20  urgeable caches 
150ca 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  */..  unsigned i
150cb 6e 74 20 6d 78 50 69 6e 6e 65 64 3b 20 20 20 20  nt mxPinned;    
150cc 20 20 20 20 20 2f 2a 20 6e 4d 61 78 70 61 67 65       /* nMaxpage
150cd 20 2b 20 31 30 20 2d 20 6e 4d 69 6e 50 61 67 65   + 10 - nMinPage
150ce 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20   */..  unsigned 
150cf 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65  int nCurrentPage
150d0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
150d1 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  of purgeable pag
150d2 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0d  es allocated */.
150d3 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48  .  PgHdr1 *pLruH
150d4 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20  ead, *pLruTail; 
150d5 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66    /* LRU list of
150d6 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
150d7 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 45 61 63  */..};..../* Eac
150d8 68 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  h page cache is 
150d9 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
150da 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a  he following obj
150db 65 63 74 2e 20 20 45 76 65 72 79 0d 0a 2a 2a 20  ect.  Every..** 
150dc 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
150dd 6c 65 20 28 69 6e 63 6c 75 64 69 6e 67 20 65 61  le (including ea
150de 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  ch in-memory dat
150df 61 62 61 73 65 20 61 6e 64 20 65 61 63 68 0d 0a  abase and each..
150e0 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20  ** temporary or 
150e1 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 62 61  transient databa
150e2 73 65 29 20 68 61 73 20 61 20 73 69 6e 67 6c 65  se) has a single
150e3 20 70 61 67 65 20 63 61 63 68 65 20 77 68 69 63   page cache whic
150e4 68 0d 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74  h..** is an inst
150e5 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
150e6 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 6f 69  ect...**..** Poi
150e7 6e 74 65 72 73 20 74 6f 20 73 74 72 75 63 74 75  nters to structu
150e8 72 65 73 20 6f 66 20 74 68 69 73 20 74 79 70 65  res of this type
150e9 20 61 72 65 20 63 61 73 74 20 61 6e 64 20 72 65   are cast and re
150ea 74 75 72 6e 65 64 20 61 73 20 0d 0a 2a 2a 20 6f  turned as ..** o
150eb 70 61 71 75 65 20 73 71 6c 69 74 65 33 5f 70 63  paque sqlite3_pc
150ec 61 63 68 65 2a 20 68 61 6e 64 6c 65 73 2e 0d 0a  ache* handles...
150ed 2a 2f 0d 0a 73 74 72 75 63 74 20 50 43 61 63 68  */..struct PCach
150ee 65 31 20 7b 0d 0a 20 20 2f 2a 20 43 61 63 68 65  e1 {..  /* Cache
150ef 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70   configuration p
150f0 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67 65 20  arameters. Page 
150f1 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20 61 6e  size (szPage) an
150f2 64 20 74 68 65 20 70 75 72 67 65 61 62 6c 65 0d  d the purgeable.
150f3 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75 72  .  ** flag (bPur
150f4 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 74 20  geable) are set 
150f5 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 69  when the cache i
150f6 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78 20  s created. nMax 
150f7 6d 61 79 20 62 65 20 0d 0a 20 20 2a 2a 20 6d 6f  may be ..  ** mo
150f8 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 69  dified at any ti
150f9 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  me by a call to 
150fa 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 65  the pcache1Cache
150fb 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0d 0a  size() method...
150fc 20 20 2a 2a 20 54 68 65 20 50 47 72 6f 75 70 20    ** The PGroup 
150fd 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
150fe 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e  ld when accessin
150ff 67 20 6e 4d 61 78 2e 0d 0a 20 20 2a 2f 0d 0a 20  g nMax...  */.. 
15100 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 3b   PGroup *pGroup;
15101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15102 20 20 20 20 20 2f 2a 20 50 47 72 6f 75 70 20 74       /* PGroup t
15103 68 69 73 20 63 61 63 68 65 20 62 65 6c 6f 6e 67  his cache belong
15104 73 20 74 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 73  s to */..  int s
15105 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  zPage;          
15106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15107 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61  * Size of alloca
15108 74 65 64 20 70 61 67 65 73 20 69 6e 20 62 79 74  ted pages in byt
15109 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 45  es */..  int szE
1510a 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20  xtra;           
1510b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1510c 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 73 70  Size of extra sp
1510d 61 63 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d  ace in bytes */.
1510e 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c  .  int bPurgeabl
1510f 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
15110 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
15111 66 20 63 61 63 68 65 20 69 73 20 70 75 72 67 65  f cache is purge
15112 61 62 6c 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67  able */..  unsig
15113 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20  ned int nMin;   
15114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15115 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72  * Minimum number
15116 20 6f 66 20 70 61 67 65 73 20 72 65 73 65 72 76   of pages reserv
15117 65 64 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65  ed */..  unsigne
15118 64 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20  d int nMax;     
15119 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1511a 43 6f 6e 66 69 67 75 72 65 64 20 22 63 61 63 68  Configured "cach
1511b 65 5f 73 69 7a 65 22 20 76 61 6c 75 65 20 2a 2f  e_size" value */
1511c 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
1511d 20 6e 39 30 70 63 74 3b 20 20 20 20 20 20 20 20   n90pct;        
1511e 20 20 20 20 20 20 20 20 2f 2a 20 6e 4d 61 78 2a          /* nMax*
1511f 39 2f 31 30 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  9/10 */....  /* 
15120 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c  Hash table of al
15121 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c  l pages. The fol
15122 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
15123 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63   may only be acc
15124 65 73 73 65 64 0d 0a 20 20 2a 2a 20 77 68 65 6e  essed..  ** when
15125 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 73   the accessor is
15126 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 50 47 72   holding the PGr
15127 6f 75 70 20 6d 75 74 65 78 2e 0d 0a 20 20 2a 2f  oup mutex...  */
15128 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
15129 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 20 20   nRecyclable;   
1512a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1512b 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
1512c 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0d 0a 20  e LRU list */.. 
1512d 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50   unsigned int nP
1512e 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
1512f 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
15130 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
15131 20 61 70 48 61 73 68 20 2a 2f 0d 0a 20 20 75 6e   apHash */..  un
15132 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68  signed int nHash
15133 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15134 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
15135 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d  lots in apHash[]
15136 20 2a 2f 0d 0a 20 20 50 67 48 64 72 31 20 2a 2a   */..  PgHdr1 **
15137 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20  apHash;         
15138 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
15139 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73  sh table for fas
1513a 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20  t lookup by key 
1513b 2a 2f 0d 0a 0d 0a 20 20 75 6e 73 69 67 6e 65 64  */....  unsigned
1513c 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20   int iMaxKey;   
1513d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1513e 61 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20  argest key seen 
1513f 73 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28  since xTruncate(
15140 29 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  ) */..};..../*..
15141 2a 2a 20 45 61 63 68 20 63 61 63 68 65 20 65 6e  ** Each cache en
15142 74 72 79 20 69 73 20 72 65 70 72 65 73 65 6e 74  try is represent
15143 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
15144 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
15145 6e 67 20 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72  ng ..** structur
15146 65 2e 20 55 6e 6c 65 73 73 20 53 51 4c 49 54 45  e. Unless SQLITE
15147 5f 50 43 41 43 48 45 5f 53 45 50 41 52 41 54 45  _PCACHE_SEPARATE
15148 5f 48 45 41 44 45 52 20 69 73 20 64 65 66 69 6e  _HEADER is defin
15149 65 64 2c 20 61 20 62 75 66 66 65 72 20 6f 66 0d  ed, a buffer of.
1514a 0a 2a 2a 20 50 67 48 64 72 31 2e 70 43 61 63 68  .** PgHdr1.pCach
1514b 65 2d 3e 73 7a 50 61 67 65 20 62 79 74 65 73 20  e->szPage bytes 
1514c 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 72  is allocated dir
1514d 65 63 74 6c 79 20 62 65 66 6f 72 65 20 74 68 69  ectly before thi
1514e 73 20 73 74 72 75 63 74 75 72 65 20 0d 0a 2a 2a  s structure ..**
1514f 20 69 6e 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d   in memory...*/.
15150 0a 73 74 72 75 63 74 20 50 67 48 64 72 31 20 7b  .struct PgHdr1 {
15151 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  ..  sqlite3_pcac
15152 68 65 5f 70 61 67 65 20 70 61 67 65 3b 0d 0a 20  he_page page;.. 
15153 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b   unsigned int iK
15154 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
15155 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61  /* Key value (pa
15156 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0d 0a 20  ge number) */.. 
15157 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20   PgHdr1 *pNext; 
15158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15159 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20  /* Next in hash 
1515a 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0d 0a  table chain */..
1515b 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
1515c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1515d 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63   /* Cache that c
1515e 75 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68  urrently owns th
1515f 69 73 20 70 61 67 65 20 2a 2f 0d 0a 20 20 50 67  is page */..  Pg
15160 48 64 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20  Hdr1 *pLruNext; 
15161 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15162 4e 65 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74  Next in LRU list
15163 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67   of unpinned pag
15164 65 73 20 2a 2f 0d 0a 20 20 50 67 48 64 72 31 20  es */..  PgHdr1 
15165 2a 70 4c 72 75 50 72 65 76 3b 20 20 20 20 20 20  *pLruPrev;      
15166 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
15167 6f 75 73 20 69 6e 20 4c 52 55 20 6c 69 73 74 20  ous in LRU list 
15168 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  of unpinned page
15169 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  s */..};..../*..
1516a 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 6e  ** Free slots in
1516b 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20 75   the allocator u
1516c 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75 70  sed to divide up
1516d 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f 76   the buffer prov
1516e 69 64 65 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 74  ided using..** t
1516f 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
15170 5f 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61  _PAGECACHE mecha
15171 6e 69 73 6d 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63  nism...*/..struc
15172 74 20 50 67 46 72 65 65 73 6c 6f 74 20 7b 0d 0a  t PgFreeslot {..
15173 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 4e    PgFreeslot *pN
15174 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 66 72  ext;  /* Next fr
15175 65 65 20 73 6c 6f 74 20 2a 2f 0d 0a 7d 3b 0d 0a  ee slot */..};..
15176 0d 0a 2f 2a 0d 0a 2a 2a 20 47 6c 6f 62 61 6c 20  ../*..** Global 
15177 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69  data used by thi
15178 73 20 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 73 74  s cache...*/..st
15179 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
1517a 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f  struct PCacheGlo
1517b 62 61 6c 20 7b 0d 0a 20 20 50 47 72 6f 75 70 20  bal {..  PGroup 
1517c 67 72 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  grp;            
1517d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67          /* The g
1517e 6c 6f 62 61 6c 20 50 47 72 6f 75 70 20 66 6f 72  lobal PGroup for
1517f 20 6d 6f 64 65 20 28 32 29 20 2a 2f 0d 0a 0d 0a   mode (2) */....
15180 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 72    /* Variables r
15181 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 54 45  elated to SQLITE
15182 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
15183 45 20 73 65 74 74 69 6e 67 73 2e 20 20 54 68 65  E settings.  The
15184 0d 0a 20 20 2a 2a 20 73 7a 53 6c 6f 74 2c 20 6e  ..  ** szSlot, n
15185 53 6c 6f 74 2c 20 70 53 74 61 72 74 2c 20 70 45  Slot, pStart, pE
15186 6e 64 2c 20 6e 52 65 73 65 72 76 65 2c 20 61 6e  nd, nReserve, an
15187 64 20 69 73 49 6e 69 74 20 76 61 6c 75 65 73 20  d isInit values 
15188 61 72 65 20 61 6c 6c 0d 0a 20 20 2a 2a 20 66 69  are all..  ** fi
15189 78 65 64 20 61 74 20 73 71 6c 69 74 65 33 5f 69  xed at sqlite3_i
1518a 6e 69 74 69 61 6c 69 7a 65 28 29 20 74 69 6d 65  nitialize() time
1518b 20 61 6e 64 20 64 6f 20 6e 6f 74 20 72 65 71 75   and do not requ
1518c 69 72 65 20 6d 75 74 65 78 20 70 72 6f 74 65 63  ire mutex protec
1518d 74 69 6f 6e 2e 0d 0a 20 20 2a 2a 20 54 68 65 20  tion...  ** The 
1518e 6e 46 72 65 65 53 6c 6f 74 20 61 6e 64 20 70 46  nFreeSlot and pF
1518f 72 65 65 20 76 61 6c 75 65 73 20 64 6f 20 72 65  ree values do re
15190 71 75 69 72 65 20 6d 75 74 65 78 20 70 72 6f 74  quire mutex prot
15191 65 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20  ection...  */.. 
15192 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20   int isInit;    
15193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15194 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69  /* True if initi
15195 61 6c 69 7a 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  alized */..  int
15196 20 73 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20   szSlot;        
15197 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
15198 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65  ize of each free
15199 20 73 6c 6f 74 20 2a 2f 0d 0a 20 20 69 6e 74 20   slot */..  int 
1519a 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20  nSlot;          
1519b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1519c 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 63 61 63  e number of pcac
1519d 68 65 20 73 6c 6f 74 73 20 2a 2f 0d 0a 20 20 69  he slots */..  i
1519e 6e 74 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20  nt nReserve;    
1519f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
151a0 20 54 72 79 20 74 6f 20 6b 65 65 70 20 6e 46 72   Try to keep nFr
151a1 65 65 53 6c 6f 74 20 61 62 6f 76 65 20 74 68 69  eeSlot above thi
151a2 73 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 53  s */..  void *pS
151a3 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20  tart, *pEnd;    
151a4 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 73         /* Bounds
151a5 20 6f 66 20 70 61 67 65 63 61 63 68 65 20 6d 61   of pagecache ma
151a6 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f 0d 0a 20  lloc range */.. 
151a7 20 2f 2a 20 41 62 6f 76 65 20 72 65 71 75 69 72   /* Above requir
151a8 65 73 20 6e 6f 20 6d 75 74 65 78 2e 20 20 55 73  es no mutex.  Us
151a9 65 20 6d 75 74 65 78 20 62 65 6c 6f 77 20 66 6f  e mutex below fo
151aa 72 20 76 61 72 69 61 62 6c 65 20 74 68 61 74 20  r variable that 
151ab 66 6f 6c 6c 6f 77 2e 20 2a 2f 0d 0a 20 20 73 71  follow. */..  sq
151ac 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
151ad 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ex;          /* 
151ae 4d 75 74 65 78 20 66 6f 72 20 61 63 63 65 73 73  Mutex for access
151af 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
151b0 67 3a 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46 72  g: */..  int nFr
151b1 65 65 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20  eeSlot;         
151b2 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
151b3 72 20 6f 66 20 75 6e 75 73 65 64 20 70 63 61 63  r of unused pcac
151b4 68 65 20 73 6c 6f 74 73 20 2a 2f 0d 0a 20 20 50  he slots */..  P
151b5 67 46 72 65 65 73 6c 6f 74 20 2a 70 46 72 65 65  gFreeslot *pFree
151b6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
151b7 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b   Free page block
151b8 73 20 2a 2f 0d 0a 20 20 2f 2a 20 54 68 65 20 66  s */..  /* The f
151b9 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 72  ollowing value r
151ba 65 71 75 69 72 65 73 20 61 20 6d 75 74 65 78 20  equires a mutex 
151bb 74 6f 20 63 68 61 6e 67 65 2e 20 20 57 65 20 73  to change.  We s
151bc 6b 69 70 20 74 68 65 20 6d 75 74 65 78 20 6f 6e  kip the mutex on
151bd 0d 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 62  ..  ** reading b
151be 65 63 61 75 73 65 20 28 31 29 20 6d 6f 73 74 20  ecause (1) most 
151bf 70 6c 61 74 66 6f 72 6d 73 20 72 65 61 64 20 61  platforms read a
151c0 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
151c1 61 74 6f 6d 69 63 61 6c 6c 79 20 61 6e 64 0d 0a  atomically and..
151c2 20 20 2a 2a 20 28 32 29 20 65 76 65 6e 20 69 66    ** (2) even if
151c3 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 76 61   an incorrect va
151c4 6c 75 65 20 69 73 20 72 65 61 64 2c 20 6e 6f 20  lue is read, no 
151c5 67 72 65 61 74 20 68 61 72 6d 20 69 73 20 64 6f  great harm is do
151c6 6e 65 20 73 69 6e 63 65 20 74 68 69 73 0d 0a 20  ne since this.. 
151c7 20 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 6a 75   ** is really ju
151c8 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  st an optimizati
151c9 6f 6e 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 62 55  on. */..  int bU
151ca 6e 64 65 72 50 72 65 73 73 75 72 65 3b 20 20 20  nderPressure;   
151cb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
151cc 20 69 66 20 6c 6f 77 20 6f 6e 20 50 41 47 45 43   if low on PAGEC
151cd 41 43 48 45 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a  ACHE memory */..
151ce 7d 20 70 63 61 63 68 65 31 5f 67 3b 0d 0a 0d 0a  } pcache1_g;....
151cf 2f 2a 0d 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20  /*..** All code 
151d0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f  in this file sho
151d1 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67  uld access the g
151d2 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20  lobal structure 
151d3 61 62 6f 76 65 20 76 69 61 20 74 68 65 0d 0a 2a  above via the..*
151d4 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31  * alias "pcache1
151d5 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  ". This ensures 
151d6 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75  that the WSD emu
151d7 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77  lation is used w
151d8 68 65 6e 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e  hen..** compilin
151d9 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68  g for systems th
151da 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72  at do not suppor
151db 74 20 72 65 61 6c 20 57 53 44 2e 0d 0a 2a 2f 0d  t real WSD...*/.
151dc 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31  .#define pcache1
151dd 20 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   (GLOBAL(struct 
151de 50 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63  PCacheGlobal, pc
151df 61 63 68 65 31 5f 67 29 29 0d 0a 0d 0a 2f 2a 0d  ache1_g))..../*.
151e0 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 6e  .** Macros to en
151e1 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 68  ter and leave th
151e2 65 20 50 43 61 63 68 65 20 4c 52 55 20 6d 75 74  e PCache LRU mut
151e3 65 78 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  ex...*/..#define
151e4 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
151e5 65 78 28 58 29 20 73 71 6c 69 74 65 33 5f 6d 75  ex(X) sqlite3_mu
151e6 74 65 78 5f 65 6e 74 65 72 28 28 58 29 2d 3e 6d  tex_enter((X)->m
151e7 75 74 65 78 29 0d 0a 23 64 65 66 69 6e 65 20 70  utex)..#define p
151e8 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
151e9 28 58 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  (X) sqlite3_mute
151ea 78 5f 6c 65 61 76 65 28 28 58 29 2d 3e 6d 75 74  x_leave((X)->mut
151eb 65 78 29 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ex)..../********
151ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
151ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
151ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
151ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
151f0 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
151f1 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69  ** Page Allocati
151f2 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  on/SQLITE_CONFIG
151f3 5f 50 43 41 43 48 45 20 52 65 6c 61 74 65 64 20  _PCACHE Related 
151f4 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a  Functions ******
151f5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d  ********/..../*.
151f6 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
151f7 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69  n is called duri
151f8 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
151f9 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75  n if a static bu
151fa 66 66 65 72 20 69 73 20 0d 0a 2a 2a 20 73 75 70  ffer is ..** sup
151fb 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72  plied to use for
151fc 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20   the page-cache 
151fd 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 53  by passing the S
151fe 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
151ff 45 43 41 43 48 45 0d 0a 2a 2a 20 76 65 72 62 20  ECACHE..** verb 
15200 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  to sqlite3_confi
15201 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70  g(). Parameter p
15202 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  Buf points to an
15203 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
15204 65 0d 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20  e..** enough to 
15205 63 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66  contain 'n' buff
15206 65 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65  ers of 'sz' byte
15207 73 20 65 61 63 68 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  s each...**..** 
15208 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
15209 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69  called from sqli
1520a 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
1520b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 67 75   and so it is gu
1520c 61 72 61 6e 74 65 65 64 0d 0a 2a 2a 20 74 6f 20  aranteed..** to 
1520d 62 65 20 73 65 72 69 61 6c 69 7a 65 64 20 61 6c  be serialized al
1520e 72 65 61 64 79 2e 20 20 54 68 65 72 65 20 69 73  ready.  There is
1520f 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 66 75 72   no need for fur
15210 74 68 65 72 20 6d 75 74 65 78 69 6e 67 2e 0d 0a  ther mutexing...
15211 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
15212 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
15213 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70  CacheBufferSetup
15214 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74  (void *pBuf, int
15215 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20   sz, int n){..  
15216 69 66 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e  if( pcache1.isIn
15217 69 74 20 29 7b 0d 0a 20 20 20 20 50 67 46 72 65  it ){..    PgFre
15218 65 73 6c 6f 74 20 2a 70 3b 0d 0a 20 20 20 20 73  eslot *p;..    s
15219 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73  z = ROUNDDOWN8(s
1521a 7a 29 3b 0d 0a 20 20 20 20 70 63 61 63 68 65 31  z);..    pcache1
1521b 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0d 0a 20  .szSlot = sz;.. 
1521c 20 20 20 70 63 61 63 68 65 31 2e 6e 53 6c 6f 74     pcache1.nSlot
1521d 20 3d 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65   = pcache1.nFree
1521e 53 6c 6f 74 20 3d 20 6e 3b 0d 0a 20 20 20 20 70  Slot = n;..    p
1521f 63 61 63 68 65 31 2e 6e 52 65 73 65 72 76 65 20  cache1.nReserve 
15220 3d 20 6e 3e 39 30 20 3f 20 31 30 20 3a 20 28 6e  = n>90 ? 10 : (n
15221 2f 31 30 20 2b 20 31 29 3b 0d 0a 20 20 20 20 70  /10 + 1);..    p
15222 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20  cache1.pStart = 
15223 70 42 75 66 3b 0d 0a 20 20 20 20 70 63 61 63 68  pBuf;..    pcach
15224 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0d 0a 20  e1.pFree = 0;.. 
15225 20 20 20 70 63 61 63 68 65 31 2e 62 55 6e 64 65     pcache1.bUnde
15226 72 50 72 65 73 73 75 72 65 20 3d 20 30 3b 0d 0a  rPressure = 0;..
15227 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29      while( n-- )
15228 7b 0d 0a 20 20 20 20 20 20 70 20 3d 20 28 50 67  {..      p = (Pg
15229 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0d  Freeslot*)pBuf;.
1522a 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
1522b 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
1522c 0d 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  ..      pcache1.
1522d 70 46 72 65 65 20 3d 20 70 3b 0d 0a 20 20 20 20  pFree = p;..    
1522e 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29    pBuf = (void*)
1522f 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73  &((char*)pBuf)[s
15230 7a 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  z];..    }..    
15231 70 63 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70  pcache1.pEnd = p
15232 42 75 66 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  Buf;..  }..}....
15233 2f 2a 0d 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  /*..** Malloc fu
15234 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 69 74 68  nction used with
15235 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20  in this file to 
15236 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  allocate space f
15237 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0d 0a  rom the buffer..
15238 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  ** configured us
15239 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ing sqlite3_conf
1523a 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
1523b 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69  _PAGECACHE) opti
1523c 6f 6e 2e 20 49 66 20 6e 6f 20 0d 0a 2a 2a 20 73  on. If no ..** s
1523d 75 63 68 20 62 75 66 66 65 72 20 65 78 69 73 74  uch buffer exist
1523e 73 20 6f 72 20 74 68 65 72 65 20 69 73 20 6e 6f  s or there is no
1523f 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 69   space left in i
15240 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  t, this function
15241 20 66 61 6c 6c 73 20 0d 0a 2a 2a 20 62 61 63 6b   falls ..** back
15242 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   to sqlite3Mallo
15243 63 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 75 6c  c()...**..** Mul
15244 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 63 61  tiple threads ca
15245 6e 20 72 75 6e 20 74 68 69 73 20 72 6f 75 74 69  n run this routi
15246 6e 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ne at the same t
15247 69 6d 65 2e 20 20 47 6c 6f 62 61 6c 20 76 61 72  ime.  Global var
15248 69 61 62 6c 65 73 0d 0a 2a 2a 20 69 6e 20 70 63  iables..** in pc
15249 61 63 68 65 31 20 6e 65 65 64 20 74 6f 20 62 65  ache1 need to be
1524a 20 70 72 6f 74 65 63 74 65 64 20 76 69 61 20 6d   protected via m
1524b 75 74 65 78 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  utex...*/..stati
1524c 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41  c void *pcache1A
1524d 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
1524e 0d 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b  ..  void *p = 0;
1524f 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15250 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
15251 64 28 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 75  d(pcache1.grp.mu
15252 74 65 78 29 20 29 3b 0d 0a 20 20 73 71 6c 69 74  tex) );..  sqlit
15253 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
15254 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
15255 43 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29  CHE_SIZE, nByte)
15256 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  ;..  if( nByte<=
15257 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 29  pcache1.szSlot )
15258 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  {..    sqlite3_m
15259 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68  utex_enter(pcach
1525a 65 31 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20  e1.mutex);..    
1525b 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63  p = (PgHdr1 *)pc
1525c 61 63 68 65 31 2e 70 46 72 65 65 3b 0d 0a 20 20  ache1.pFree;..  
1525d 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20    if( p ){..    
1525e 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
1525f 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 2d  = pcache1.pFree-
15260 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 70  >pNext;..      p
15261 63 61 63 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74  cache1.nFreeSlot
15262 2d 2d 3b 0d 0a 20 20 20 20 20 20 70 63 61 63 68  --;..      pcach
15263 65 31 2e 62 55 6e 64 65 72 50 72 65 73 73 75 72  e1.bUnderPressur
15264 65 20 3d 20 70 63 61 63 68 65 31 2e 6e 46 72 65  e = pcache1.nFre
15265 65 53 6c 6f 74 3c 70 63 61 63 68 65 31 2e 6e 52  eSlot<pcache1.nR
15266 65 73 65 72 76 65 3b 0d 0a 20 20 20 20 20 20 61  eserve;..      a
15267 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 6e  ssert( pcache1.n
15268 46 72 65 65 53 6c 6f 74 3e 3d 30 20 29 3b 0d 0a  FreeSlot>=0 );..
15269 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
1526a 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
1526b 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
1526c 53 45 44 2c 20 31 29 3b 0d 0a 20 20 20 20 7d 0d  SED, 1);..    }.
1526d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1526e 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31  ex_leave(pcache1
1526f 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20  .mutex);..  }.. 
15270 20 69 66 28 20 70 3d 3d 30 20 29 7b 0d 0a 20 20   if( p==0 ){..  
15271 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 6e    /* Memory is n
15272 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
15273 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  the SQLITE_CONFI
15274 47 5f 50 41 47 45 43 41 43 48 45 20 70 6f 6f 6c  G_PAGECACHE pool
15275 2e 20 20 47 65 74 0d 0a 20 20 20 20 2a 2a 20 69  .  Get..    ** i
15276 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61  t from sqlite3Ma
15277 6c 6c 6f 63 20 69 6e 73 74 65 61 64 2e 0d 0a 20  lloc instead... 
15278 20 20 20 2a 2f 0d 0a 20 20 20 20 70 20 3d 20 73     */..    p = s
15279 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79  qlite3Malloc(nBy
1527a 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 20  te);..    if( p 
1527b 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  ){..      int sz
1527c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
1527d 53 69 7a 65 28 70 29 3b 0d 0a 20 20 20 20 20 20  Size(p);..      
1527e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1527f 74 65 72 28 70 63 61 63 68 65 31 2e 6d 75 74 65  ter(pcache1.mute
15280 78 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  x);..      sqlit
15281 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
15282 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
15283 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 73 7a  CHE_OVERFLOW, sz
15284 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
15285 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63  3_mutex_leave(pc
15286 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0d 0a 20  ache1.mutex);.. 
15287 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
15288 33 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65  3MemdebugSetType
15289 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50 43 41 43  (p, MEMTYPE_PCAC
1528a 48 45 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  HE);..  }..  ret
1528b 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  urn p;..}..../*.
1528c 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f  .** Free an allo
1528d 63 61 74 65 64 20 62 75 66 66 65 72 20 6f 62 74  cated buffer obt
1528e 61 69 6e 65 64 20 66 72 6f 6d 20 70 63 61 63 68  ained from pcach
1528f 65 31 41 6c 6c 6f 63 28 29 2e 0d 0a 2a 2f 0d 0a  e1Alloc()...*/..
15290 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68  static int pcach
15291 65 31 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  e1Free(void *p){
15292 0d 0a 20 20 69 6e 74 20 6e 46 72 65 65 64 20 3d  ..  int nFreed =
15293 20 30 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20   0;..  if( p==0 
15294 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 69  ) return 0;..  i
15295 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53  f( p>=pcache1.pS
15296 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65  tart && p<pcache
15297 31 2e 70 45 6e 64 20 29 7b 0d 0a 20 20 20 20 50  1.pEnd ){..    P
15298 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74  gFreeslot *pSlot
15299 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ;..    sqlite3_m
1529a 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68  utex_enter(pcach
1529b 65 31 2e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20  e1.mutex);..    
1529c 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
1529d 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
1529e 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d  AGECACHE_USED, -
1529f 31 29 3b 0d 0a 20 20 20 20 70 53 6c 6f 74 20 3d  1);..    pSlot =
152a0 20 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b   (PgFreeslot*)p;
152a1 0d 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65  ..    pSlot->pNe
152a2 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72  xt = pcache1.pFr
152a3 65 65 3b 0d 0a 20 20 20 20 70 63 61 63 68 65 31  ee;..    pcache1
152a4 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0d  .pFree = pSlot;.
152a5 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 46 72  .    pcache1.nFr
152a6 65 65 53 6c 6f 74 2b 2b 3b 0d 0a 20 20 20 20 70  eeSlot++;..    p
152a7 63 61 63 68 65 31 2e 62 55 6e 64 65 72 50 72 65  cache1.bUnderPre
152a8 73 73 75 72 65 20 3d 20 70 63 61 63 68 65 31 2e  ssure = pcache1.
152a9 6e 46 72 65 65 53 6c 6f 74 3c 70 63 61 63 68 65  nFreeSlot<pcache
152aa 31 2e 6e 52 65 73 65 72 76 65 3b 0d 0a 20 20 20  1.nReserve;..   
152ab 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31   assert( pcache1
152ac 2e 6e 46 72 65 65 53 6c 6f 74 3c 3d 70 63 61 63  .nFreeSlot<=pcac
152ad 68 65 31 2e 6e 53 6c 6f 74 20 29 3b 0d 0a 20 20  he1.nSlot );..  
152ae 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
152af 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d 75  leave(pcache1.mu
152b0 74 65 78 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  tex);..  }else{.
152b1 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
152b2 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
152b3 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50  ype(p, MEMTYPE_P
152b4 43 41 43 48 45 29 20 29 3b 0d 0a 20 20 20 20 73  CACHE) );..    s
152b5 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65  qlite3MemdebugSe
152b6 74 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  tType(p, MEMTYPE
152b7 5f 48 45 41 50 29 3b 0d 0a 20 20 20 20 6e 46 72  _HEAP);..    nFr
152b8 65 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  eed = sqlite3Mal
152b9 6c 6f 63 53 69 7a 65 28 70 29 3b 0d 0a 20 20 20  locSize(p);..   
152ba 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
152bb 6e 74 65 72 28 70 63 61 63 68 65 31 2e 6d 75 74  nter(pcache1.mut
152bc 65 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ex);..    sqlite
152bd 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
152be 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
152bf 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 6e 46  HE_OVERFLOW, -nF
152c0 72 65 65 64 29 3b 0d 0a 20 20 20 20 73 71 6c 69  reed);..    sqli
152c1 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
152c2 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0d  pcache1.mutex);.
152c3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
152c4 65 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  e(p);..  }..  re
152c5 74 75 72 6e 20 6e 46 72 65 65 64 3b 0d 0a 7d 0d  turn nFreed;..}.
152c6 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
152c7 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
152c8 41 4e 41 47 45 4d 45 4e 54 0d 0a 2f 2a 0d 0a 2a  ANAGEMENT../*..*
152c9 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
152ca 65 20 6f 66 20 61 20 70 63 61 63 68 65 20 61 6c  e of a pcache al
152cb 6c 6f 63 61 74 69 6f 6e 0d 0a 2a 2f 0d 0a 73 74  location..*/..st
152cc 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31  atic int pcache1
152cd 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  MemSize(void *p)
152ce 7b 0d 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 63  {..  if( p>=pcac
152cf 68 65 31 2e 70 53 74 61 72 74 20 26 26 20 70 3c  he1.pStart && p<
152d0 70 63 61 63 68 65 31 2e 70 45 6e 64 20 29 7b 0d  pcache1.pEnd ){.
152d1 0a 20 20 20 20 72 65 74 75 72 6e 20 70 63 61 63  .    return pcac
152d2 68 65 31 2e 73 7a 53 6c 6f 74 3b 0d 0a 20 20 7d  he1.szSlot;..  }
152d3 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20 69  else{..    int i
152d4 53 69 7a 65 3b 0d 0a 20 20 20 20 61 73 73 65 72  Size;..    asser
152d5 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  t( sqlite3Memdeb
152d6 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d  ugHasType(p, MEM
152d7 54 59 50 45 5f 50 43 41 43 48 45 29 20 29 3b 0d  TYPE_PCACHE) );.
152d8 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 64  .    sqlite3Memd
152d9 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d  ebugSetType(p, M
152da 45 4d 54 59 50 45 5f 48 45 41 50 29 3b 0d 0a 20  EMTYPE_HEAP);.. 
152db 20 20 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74     iSize = sqlit
152dc 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
152dd 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  ..    sqlite3Mem
152de 64 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20  debugSetType(p, 
152df 4d 45 4d 54 59 50 45 5f 50 43 41 43 48 45 29 3b  MEMTYPE_PCACHE);
152e0 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 69 53 69  ..    return iSi
152e1 7a 65 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e  ze;..  }..}..#en
152e2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
152e3 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
152e4 47 45 4d 45 4e 54 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  GEMENT */..../*.
152e5 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
152e6 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
152e7 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
152e8 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
152e9 43 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  Cache...*/..stat
152ea 69 63 20 50 67 48 64 72 31 20 2a 70 63 61 63 68  ic PgHdr1 *pcach
152eb 65 31 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63  e1AllocPage(PCac
152ec 68 65 31 20 2a 70 43 61 63 68 65 29 7b 0d 0a 20  he1 *pCache){.. 
152ed 20 50 67 48 64 72 31 20 2a 70 20 3d 20 30 3b 0d   PgHdr1 *p = 0;.
152ee 0a 20 20 76 6f 69 64 20 2a 70 50 67 3b 0d 0a 0d  .  void *pPg;...
152ef 0a 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20  .  /* The group 
152f0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 72 65  mutex must be re
152f1 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 70 63  leased before pc
152f2 61 63 68 65 31 41 6c 6c 6f 63 28 29 20 69 73 20  ache1Alloc() is 
152f3 63 61 6c 6c 65 64 2e 20 54 68 69 73 0d 0a 20 20  called. This..  
152f4 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 69 74  ** is because it
152f5 20 6d 61 79 20 63 61 6c 6c 20 73 71 6c 69 74 65   may call sqlite
152f6 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
152f7 28 29 2c 20 77 68 69 63 68 20 61 73 73 75 6d 65  (), which assume
152f8 73 20 74 68 61 74 20 0d 0a 20 20 2a 2a 20 74 68  s that ..  ** th
152f9 69 73 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20  is mutex is not 
152fa 68 65 6c 64 2e 20 2a 2f 0d 0a 20 20 61 73 73 65  held. */..  asse
152fb 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
152fc 78 5f 68 65 6c 64 28 70 43 61 63 68 65 2d 3e 70  x_held(pCache->p
152fd 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29 20 29 3b  Group->mutex) );
152fe 0d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ..  pcache1Leave
152ff 4d 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47  Mutex(pCache->pG
15300 72 6f 75 70 29 3b 0d 0a 23 69 66 64 65 66 20 53  roup);..#ifdef S
15301 51 4c 49 54 45 5f 50 43 41 43 48 45 5f 53 45 50  QLITE_PCACHE_SEP
15302 41 52 41 54 45 5f 48 45 41 44 45 52 0d 0a 20 20  ARATE_HEADER..  
15303 70 50 67 20 3d 20 70 63 61 63 68 65 31 41 6c 6c  pPg = pcache1All
15304 6f 63 28 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  oc(pCache->szPag
15305 65 29 3b 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74  e);..  p = sqlit
15306 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  e3Malloc(sizeof(
15307 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65  PgHdr1) + pCache
15308 2d 3e 73 7a 45 78 74 72 61 29 3b 0d 0a 20 20 69  ->szExtra);..  i
15309 66 28 20 21 70 50 67 20 7c 7c 20 21 70 20 29 7b  f( !pPg || !p ){
1530a 0d 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65  ..    pcache1Fre
1530b 65 28 70 50 67 29 3b 0d 0a 20 20 20 20 73 71 6c  e(pPg);..    sql
1530c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
1530d 20 20 20 70 50 67 20 3d 20 30 3b 0d 0a 20 20 7d     pPg = 0;..  }
1530e 0d 0a 23 65 6c 73 65 0d 0a 20 20 70 50 67 20 3d  ..#else..  pPg =
1530f 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 69   pcache1Alloc(si
15310 7a 65 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70  zeof(PgHdr1) + p
15311 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20  Cache->szPage + 
15312 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29  pCache->szExtra)
15313 3b 0d 0a 20 20 70 20 3d 20 28 50 67 48 64 72 31  ;..  p = (PgHdr1
15314 20 2a 29 26 28 28 75 38 20 2a 29 70 50 67 29 5b   *)&((u8 *)pPg)[
15315 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 5d 3b  pCache->szPage];
15316 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70 63 61 63  ..#endif..  pcac
15317 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 70 43  he1EnterMutex(pC
15318 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0d 0a  ache->pGroup);..
15319 0d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0d 0a  ..  if( pPg ){..
1531a 20 20 20 20 70 2d 3e 70 61 67 65 2e 70 42 75 66      p->page.pBuf
1531b 20 3d 20 70 50 67 3b 0d 0a 20 20 20 20 70 2d 3e   = pPg;..    p->
1531c 70 61 67 65 2e 70 45 78 74 72 61 20 3d 20 26 70  page.pExtra = &p
1531d 5b 31 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70 43  [1];..    if( pC
1531e 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
1531f 20 29 7b 0d 0a 20 20 20 20 20 20 70 43 61 63 68   ){..      pCach
15320 65 2d 3e 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72  e->pGroup->nCurr
15321 65 6e 74 50 61 67 65 2b 2b 3b 0d 0a 20 20 20 20  entPage++;..    
15322 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 3b  }..    return p;
15323 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
15324 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
15325 46 72 65 65 20 61 20 70 61 67 65 20 6f 62 6a 65  Free a page obje
15326 63 74 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ct allocated by 
15327 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65  pcache1AllocPage
15328 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ()...**..** The 
15329 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 6c 6f 77  pointer is allow
1532a 65 64 20 74 6f 20 62 65 20 4e 55 4c 4c 2c 20 77  ed to be NULL, w
1532b 68 69 63 68 20 69 73 20 70 72 75 64 65 6e 74 2e  hich is prudent.
1532c 20 20 42 75 74 20 69 74 20 74 75 72 6e 73 20 6f    But it turns o
1532d 75 74 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ut..** that the 
1532e 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
1532f 74 61 74 69 6f 6e 20 68 61 70 70 65 6e 73 20 74  tation happens t
15330 6f 20 6e 65 76 65 72 20 63 61 6c 6c 20 74 68 69  o never call thi
15331 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 77 69  s routine..** wi
15332 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  th a NULL pointe
15333 72 2c 20 73 6f 20 77 65 20 6d 61 72 6b 20 74 68  r, so we mark th
15334 65 20 4e 55 4c 4c 20 74 65 73 74 20 77 69 74 68  e NULL test with
15335 20 41 4c 57 41 59 53 28 29 2e 0d 0a 2a 2f 0d 0a   ALWAYS()...*/..
15336 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
15337 68 65 31 46 72 65 65 50 61 67 65 28 50 67 48 64  he1FreePage(PgHd
15338 72 31 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 41  r1 *p){..  if( A
15339 4c 57 41 59 53 28 70 29 20 29 7b 0d 0a 20 20 20  LWAYS(p) ){..   
1533a 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
1533b 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0d 0a 20   = p->pCache;.. 
1533c 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1533d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1533e 3e 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d  >pCache->pGroup-
1533f 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 20 20  >mutex) );..    
15340 70 63 61 63 68 65 31 46 72 65 65 28 70 2d 3e 70  pcache1Free(p->p
15341 61 67 65 2e 70 42 75 66 29 3b 0d 0a 23 69 66 64  age.pBuf);..#ifd
15342 65 66 20 53 51 4c 49 54 45 5f 50 43 41 43 48 45  ef SQLITE_PCACHE
15343 5f 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 52  _SEPARATE_HEADER
15344 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
15345 65 65 28 70 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ee(p);..#endif..
15346 20 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e      if( pCache->
15347 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0d 0a 20  bPurgeable ){.. 
15348 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 47 72       pCache->pGr
15349 6f 75 70 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67  oup->nCurrentPag
1534a 65 2d 2d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  e--;..    }..  }
1534b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61  ..}..../*..** Ma
1534c 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73  lloc function us
1534d 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20  ed by SQLite to 
1534e 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f  obtain space fro
1534f 6d 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e  m the buffer con
15350 66 69 67 75 72 65 64 0d 0a 2a 2a 20 75 73 69 6e  figured..** usin
15351 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  g sqlite3_config
15352 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  (SQLITE_CONFIG_P
15353 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f 6e  AGECACHE) option
15354 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62 75 66  . If no such buf
15355 66 65 72 0d 0a 2a 2a 20 65 78 69 73 74 73 2c 20  fer..** exists, 
15356 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 61  this function fa
15357 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c 69  lls back to sqli
15358 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2f  te3Malloc()...*/
15359 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1535a 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
1535b 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29  geMalloc(int sz)
1535c 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 63 61 63  {..  return pcac
1535d 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0d 0a 7d  he1Alloc(sz);..}
1535e 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20  ..../*..** Free 
1535f 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  an allocated buf
15360 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  fer obtained fro
15361 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  m sqlite3PageMal
15362 6c 6f 63 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  loc()...*/..SQLI
15363 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15364 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
15365 76 6f 69 64 20 2a 70 29 7b 0d 0a 20 20 70 63 61  void *p){..  pca
15366 63 68 65 31 46 72 65 65 28 70 29 3b 0d 0a 7d 0d  che1Free(p);..}.
15367 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  ...../*..** Retu
15368 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64 65  rn true if it de
15369 73 69 72 61 62 6c 65 20 74 6f 20 61 76 6f 69 64  sirable to avoid
1536a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65   allocating a ne
1536b 77 20 70 61 67 65 20 63 61 63 68 65 0d 0a 2a 2a  w page cache..**
1536c 20 65 6e 74 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   entry...**..** 
1536d 49 66 20 6d 65 6d 6f 72 79 20 77 61 73 20 61 6c  If memory was al
1536e 6c 6f 63 61 74 65 64 20 73 70 65 63 69 66 69 63  located specific
1536f 61 6c 6c 79 20 74 6f 20 74 68 65 20 70 61 67 65  ally to the page
15370 20 63 61 63 68 65 20 75 73 69 6e 67 0d 0a 2a 2a   cache using..**
15371 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
15372 41 47 45 43 41 43 48 45 20 62 75 74 20 74 68 61  AGECACHE but tha
15373 74 20 6d 65 6d 6f 72 79 20 68 61 73 20 61 6c 6c  t memory has all
15374 20 62 65 65 6e 20 75 73 65 64 2c 20 74 68 65 6e   been used, then
15375 0d 0a 2a 2a 20 69 74 20 69 73 20 64 65 73 69 72  ..** it is desir
15376 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 6c  able to avoid al
15377 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70  locating a new p
15378 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 20  age cache entry 
15379 62 65 63 61 75 73 65 0d 0a 2a 2a 20 70 72 65 73  because..** pres
1537a 75 6d 61 62 6c 79 20 53 51 4c 49 54 45 5f 43 4f  umably SQLITE_CO
1537b 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 77  NFIG_PAGECACHE w
1537c 61 73 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65  as suppose to be
1537d 20 73 75 66 66 69 63 69 65 6e 74 0d 0a 2a 2a 20   sufficient..** 
1537e 66 6f 72 20 61 6c 6c 20 70 61 67 65 20 63 61 63  for all page cac
1537f 68 65 20 6e 65 65 64 73 20 61 6e 64 20 77 65 20  he needs and we 
15380 73 68 6f 75 6c 64 20 6e 6f 74 20 6e 65 65 64 20  should not need 
15381 74 6f 20 73 70 69 6c 6c 20 74 68 65 0d 0a 2a 2a  to spill the..**
15382 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 6e 74 6f   allocation onto
15383 20 74 68 65 20 68 65 61 70 2e 0d 0a 2a 2a 0d 0a   the heap...**..
15384 2a 2a 20 4f 72 2c 20 74 68 65 20 68 65 61 70 20  ** Or, the heap 
15385 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20  is used for all 
15386 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
15387 79 20 62 75 74 20 74 68 65 20 68 65 61 70 20 69  y but the heap i
15388 73 0d 0a 2a 2a 20 75 6e 64 65 72 20 6d 65 6d 6f  s..** under memo
15389 72 79 20 70 72 65 73 73 75 72 65 2c 20 74 68 65  ry pressure, the
1538a 6e 20 61 67 61 69 6e 20 69 74 20 69 73 20 64 65  n again it is de
1538b 73 69 72 61 62 6c 65 20 74 6f 20 61 76 6f 69 64  sirable to avoid
1538c 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20  ..** allocating 
1538d 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68 65  a new page cache
1538e 20 65 6e 74 72 79 20 69 6e 20 6f 72 64 65 72 20   entry in order 
1538f 74 6f 20 61 76 6f 69 64 20 73 74 72 65 73 73 69  to avoid stressi
15390 6e 67 0d 0a 2a 2a 20 74 68 65 20 68 65 61 70 20  ng..** the heap 
15391 65 76 65 6e 20 66 75 72 74 68 65 72 2e 0d 0a 2a  even further...*
15392 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  /..static int pc
15393 61 63 68 65 31 55 6e 64 65 72 4d 65 6d 6f 72 79  ache1UnderMemory
15394 50 72 65 73 73 75 72 65 28 50 43 61 63 68 65 31  Pressure(PCache1
15395 20 2a 70 43 61 63 68 65 29 7b 0d 0a 20 20 69 66   *pCache){..  if
15396 28 20 70 63 61 63 68 65 31 2e 6e 53 6c 6f 74 20  ( pcache1.nSlot 
15397 26 26 20 28 70 43 61 63 68 65 2d 3e 73 7a 50 61  && (pCache->szPa
15398 67 65 2b 70 43 61 63 68 65 2d 3e 73 7a 45 78 74  ge+pCache->szExt
15399 72 61 29 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53  ra)<=pcache1.szS
1539a 6c 6f 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  lot ){..    retu
1539b 72 6e 20 70 63 61 63 68 65 31 2e 62 55 6e 64 65  rn pcache1.bUnde
1539c 72 50 72 65 73 73 75 72 65 3b 0d 0a 20 20 7d 65  rPressure;..  }e
1539d 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  lse{..    return
1539e 20 73 71 6c 69 74 65 33 48 65 61 70 4e 65 61 72   sqlite3HeapNear
1539f 6c 79 46 75 6c 6c 28 29 3b 0d 0a 20 20 7d 0d 0a  lyFull();..  }..
153a0 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }..../**********
153a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
153a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
153a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
153a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
153a5 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
153a6 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65   General Impleme
153a7 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e  ntation Function
153a8 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
153a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
153aa 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  ******/..../*..*
153ab 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
153ac 69 73 20 75 73 65 64 20 74 6f 20 72 65 73 69 7a  is used to resiz
153ad 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  e the hash table
153ae 20 75 73 65 64 20 62 79 20 74 68 65 20 63 61 63   used by the cac
153af 68 65 20 70 61 73 73 65 64 0d 0a 2a 2a 20 61 73  he passed..** as
153b0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
153b1 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ent...**..** The
153b2 20 50 43 61 63 68 65 20 6d 75 74 65 78 20 6d 75   PCache mutex mu
153b3 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20  st be held when 
153b4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
153b5 20 63 61 6c 6c 65 64 2e 0d 0a 2a 2f 0d 0a 73 74   called...*/..st
153b6 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31  atic int pcache1
153b7 52 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68  ResizeHash(PCach
153b8 65 31 20 2a 70 29 7b 0d 0a 20 20 50 67 48 64 72  e1 *p){..  PgHdr
153b9 31 20 2a 2a 61 70 4e 65 77 3b 0d 0a 20 20 75 6e  1 **apNew;..  un
153ba 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b  signed int nNew;
153bb 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
153bc 20 69 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   i;....  assert(
153bd 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
153be 65 6c 64 28 70 2d 3e 70 47 72 6f 75 70 2d 3e 6d  eld(p->pGroup->m
153bf 75 74 65 78 29 20 29 3b 0d 0a 0d 0a 20 20 6e 4e  utex) );....  nN
153c0 65 77 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b  ew = p->nHash*2;
153c1 0d 0a 20 20 69 66 28 20 6e 4e 65 77 3c 32 35 36  ..  if( nNew<256
153c2 20 29 7b 0d 0a 20 20 20 20 6e 4e 65 77 20 3d 20   ){..    nNew = 
153c3 32 35 36 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70  256;..  }....  p
153c4 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
153c5 28 70 2d 3e 70 47 72 6f 75 70 29 3b 0d 0a 20 20  (p->pGroup);..  
153c6 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20  if( p->nHash ){ 
153c7 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
153c8 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0d 0a 20  gnMalloc(); }.. 
153c9 20 61 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31   apNew = (PgHdr1
153ca 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   **)sqlite3_mall
153cb 6f 63 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31  oc(sizeof(PgHdr1
153cc 20 2a 29 2a 6e 4e 65 77 29 3b 0d 0a 20 20 69 66   *)*nNew);..  if
153cd 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71  ( p->nHash ){ sq
153ce 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
153cf 6c 6c 6f 63 28 29 3b 20 7d 0d 0a 20 20 70 63 61  lloc(); }..  pca
153d0 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 70  che1EnterMutex(p
153d1 2d 3e 70 47 72 6f 75 70 29 3b 0d 0a 20 20 69 66  ->pGroup);..  if
153d2 28 20 61 70 4e 65 77 20 29 7b 0d 0a 20 20 20 20  ( apNew ){..    
153d3 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20 30 2c  memset(apNew, 0,
153d4 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 2a   sizeof(PgHdr1 *
153d5 29 2a 6e 4e 65 77 29 3b 0d 0a 20 20 20 20 66 6f  )*nNew);..    fo
153d6 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 73  r(i=0; i<p->nHas
153d7 68 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  h; i++){..      
153d8 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0d 0a  PgHdr1 *pPage;..
153d9 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e        PgHdr1 *pN
153da 65 78 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b  ext = p->apHash[
153db 69 5d 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65  i];..      while
153dc 28 20 28 70 50 61 67 65 20 3d 20 70 4e 65 78 74  ( (pPage = pNext
153dd 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )!=0 ){..       
153de 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20   unsigned int h 
153df 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20  = pPage->iKey % 
153e0 6e 4e 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 70  nNew;..        p
153e1 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e  Next = pPage->pN
153e2 65 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 70 50  ext;..        pP
153e3 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e  age->pNext = apN
153e4 65 77 5b 68 5d 3b 0d 0a 20 20 20 20 20 20 20 20  ew[h];..        
153e5 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65  apNew[h] = pPage
153e6 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
153e7 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  }..    sqlite3_f
153e8 72 65 65 28 70 2d 3e 61 70 48 61 73 68 29 3b 0d  ree(p->apHash);.
153e9 0a 20 20 20 20 70 2d 3e 61 70 48 61 73 68 20 3d  .    p->apHash =
153ea 20 61 70 4e 65 77 3b 0d 0a 20 20 20 20 70 2d 3e   apNew;..    p->
153eb 6e 48 61 73 68 20 3d 20 6e 4e 65 77 3b 0d 0a 20  nHash = nNew;.. 
153ec 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 28   }....  return (
153ed 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51 4c 49  p->apHash ? SQLI
153ee 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e  TE_OK : SQLITE_N
153ef 4f 4d 45 4d 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  OMEM);..}..../*.
153f0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
153f1 6e 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e  n is used intern
153f2 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 65 20 74  ally to remove t
153f3 68 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72  he page pPage fr
153f4 6f 6d 20 74 68 65 20 0d 0a 2a 2a 20 50 47 72 6f  om the ..** PGro
153f5 75 70 20 4c 52 55 20 6c 69 73 74 2c 20 69 66 20  up LRU list, if 
153f6 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e 20 49  is part of it. I
153f7 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 70  f pPage is not p
153f8 61 72 74 20 6f 66 20 74 68 65 20 50 47 72 6f 75  art of the PGrou
153f9 70 0d 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c 20  p..** LRU list, 
153fa 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
153fb 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a  on is a no-op...
153fc 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 47 72 6f 75  **..** The PGrou
153fd 70 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  p mutex must be 
153fe 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
153ff 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
15400 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 50  d...**..** If pP
15401 61 67 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  age is NULL then
15402 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
15403 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 73   a no-op...*/..s
15404 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
15405 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72 31  e1PinPage(PgHdr1
15406 20 2a 70 50 61 67 65 29 7b 0d 0a 20 20 50 43 61   *pPage){..  PCa
15407 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0d 0a 20  che1 *pCache;.. 
15408 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 3b   PGroup *pGroup;
15409 0d 0a 0d 0a 20 20 69 66 28 20 70 50 61 67 65 3d  ....  if( pPage=
1540a 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  =0 ) return;..  
1540b 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e  pCache = pPage->
1540c 70 43 61 63 68 65 3b 0d 0a 20 20 70 47 72 6f 75  pCache;..  pGrou
1540d 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f  p = pCache->pGro
1540e 75 70 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  up;..  assert( s
1540f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15410 64 28 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29  d(pGroup->mutex)
15411 20 29 3b 0d 0a 20 20 69 66 28 20 70 50 61 67 65   );..  if( pPage
15412 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50  ->pLruNext || pP
15413 61 67 65 3d 3d 70 47 72 6f 75 70 2d 3e 70 4c 72  age==pGroup->pLr
15414 75 54 61 69 6c 20 29 7b 0d 0a 20 20 20 20 69 66  uTail ){..    if
15415 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65  ( pPage->pLruPre
15416 76 20 29 7b 0d 0a 20 20 20 20 20 20 70 50 61 67  v ){..      pPag
15417 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72  e->pLruPrev->pLr
15418 75 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70  uNext = pPage->p
15419 4c 72 75 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d  LruNext;..    }.
1541a 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
1541b 70 4c 72 75 4e 65 78 74 20 29 7b 0d 0a 20 20 20  pLruNext ){..   
1541c 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65     pPage->pLruNe
1541d 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70  xt->pLruPrev = p
1541e 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0d  Page->pLruPrev;.
1541f 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
15420 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64  pGroup->pLruHead
15421 3d 3d 70 50 61 67 65 20 29 7b 0d 0a 20 20 20 20  ==pPage ){..    
15422 20 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65    pGroup->pLruHe
15423 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  ad = pPage->pLru
15424 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Next;..    }..  
15425 20 20 69 66 28 20 70 47 72 6f 75 70 2d 3e 70 4c    if( pGroup->pL
15426 72 75 54 61 69 6c 3d 3d 70 50 61 67 65 20 29 7b  ruTail==pPage ){
15427 0d 0a 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e  ..      pGroup->
15428 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65  pLruTail = pPage
15429 2d 3e 70 4c 72 75 50 72 65 76 3b 0d 0a 20 20 20  ->pLruPrev;..   
1542a 20 7d 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   }..    pPage->p
1542b 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0d 0a 20 20  LruNext = 0;..  
1542c 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65    pPage->pLruPre
1542d 76 20 3d 20 30 3b 0d 0a 20 20 20 20 70 50 61 67  v = 0;..    pPag
1542e 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63 79  e->pCache->nRecy
1542f 63 6c 61 62 6c 65 2d 2d 3b 0d 0a 20 20 7d 0d 0a  clable--;..  }..
15430 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  }....../*..** Re
15431 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75  move the page su
15432 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
15433 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68  ument from the h
15434 61 73 68 20 74 61 62 6c 65 20 0d 0a 2a 2a 20 28  ash table ..** (
15435 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73  PCache1.apHash s
15436 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69  tructure) that i
15437 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73  t is currently s
15438 74 6f 72 65 64 20 69 6e 2e 0d 0a 2a 2a 0d 0a 2a  tored in...**..*
15439 2a 20 54 68 65 20 50 47 72 6f 75 70 20 6d 75 74  * The PGroup mut
1543a 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  ex must be held 
1543b 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
1543c 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0d 0a 2a  on is called...*
1543d 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  /..static void p
1543e 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
1543f 48 61 73 68 28 50 67 48 64 72 31 20 2a 70 50 61  Hash(PgHdr1 *pPa
15440 67 65 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  ge){..  unsigned
15441 20 69 6e 74 20 68 3b 0d 0a 20 20 50 43 61 63 68   int h;..  PCach
15442 65 31 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61  e1 *pCache = pPa
15443 67 65 2d 3e 70 43 61 63 68 65 3b 0d 0a 20 20 50  ge->pCache;..  P
15444 67 48 64 72 31 20 2a 2a 70 70 3b 0d 0a 0d 0a 20  gHdr1 **pp;.... 
15445 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15446 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 61 63  _mutex_held(pCac
15447 68 65 2d 3e 70 47 72 6f 75 70 2d 3e 6d 75 74 65  he->pGroup->mute
15448 78 29 20 29 3b 0d 0a 20 20 68 20 3d 20 70 50 61  x) );..  h = pPa
15449 67 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68  ge->iKey % pCach
1544a 65 2d 3e 6e 48 61 73 68 3b 0d 0a 20 20 66 6f 72  e->nHash;..  for
1544b 28 70 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48  (pp=&pCache->apH
1544c 61 73 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70  ash[h]; (*pp)!=p
1544d 50 61 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d  Page; pp=&(*pp)-
1544e 3e 70 4e 65 78 74 29 3b 0d 0a 20 20 2a 70 70 20  >pNext);..  *pp 
1544f 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0d  = (*pp)->pNext;.
15450 0a 0d 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 61  ...  pCache->nPa
15451 67 65 2d 2d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ge--;..}..../*..
15452 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
15453 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74  currently more t
15454 68 61 6e 20 6e 4d 61 78 50 61 67 65 20 70 61 67  han nMaxPage pag
15455 65 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72  es allocated, tr
15456 79 0d 0a 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65  y..** to recycle
15457 20 70 61 67 65 73 20 74 6f 20 72 65 64 75 63 65   pages to reduce
15458 20 74 68 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f   the number allo
15459 63 61 74 65 64 20 74 6f 20 6e 4d 61 78 50 61 67  cated to nMaxPag
1545a 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  e...*/..static v
1545b 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f 72  oid pcache1Enfor
1545c 63 65 4d 61 78 50 61 67 65 28 50 47 72 6f 75 70  ceMaxPage(PGroup
1545d 20 2a 70 47 72 6f 75 70 29 7b 0d 0a 20 20 61 73   *pGroup){..  as
1545e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1545f 74 65 78 5f 68 65 6c 64 28 70 47 72 6f 75 70 2d  tex_held(pGroup-
15460 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 77 68  >mutex) );..  wh
15461 69 6c 65 28 20 70 47 72 6f 75 70 2d 3e 6e 43 75  ile( pGroup->nCu
15462 72 72 65 6e 74 50 61 67 65 3e 70 47 72 6f 75 70  rrentPage>pGroup
15463 2d 3e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 47  ->nMaxPage && pG
15464 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69 6c 20 29  roup->pLruTail )
15465 7b 0d 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70  {..    PgHdr1 *p
15466 20 3d 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54   = pGroup->pLruT
15467 61 69 6c 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  ail;..    assert
15468 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 70 47 72  ( p->pCache->pGr
15469 6f 75 70 3d 3d 70 47 72 6f 75 70 20 29 3b 0d 0a  oup==pGroup );..
1546a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
1546b 67 65 28 70 29 3b 0d 0a 20 20 20 20 70 63 61 63  ge(p);..    pcac
1546c 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
1546d 68 28 70 29 3b 0d 0a 20 20 20 20 70 63 61 63 68  h(p);..    pcach
1546e 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0d 0a  e1FreePage(p);..
1546f 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
15470 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 61 67   Discard all pag
15471 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 70 43  es from cache pC
15472 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67 65  ache with a page
15473 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 61 6c   number (key val
15474 75 65 29 20 0d 0a 2a 2a 20 67 72 65 61 74 65 72  ue) ..** greater
15475 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
15476 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69  o iLimit. Any pi
15477 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 20  nned pages that 
15478 6d 65 65 74 20 74 68 69 73 20 0d 0a 2a 2a 20 63  meet this ..** c
15479 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69  riteria are unpi
1547a 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79  nned before they
1547b 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0d   are discarded..
1547c 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 43 61 63  .**..** The PCac
1547d 68 65 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  he mutex must be
1547e 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20   held when this 
1547f 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
15480 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ed...*/..static 
15481 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 75 6e  void pcache1Trun
15482 63 61 74 65 55 6e 73 61 66 65 28 0d 0a 20 20 50  cateUnsafe(..  P
15483 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 2c 20  Cache1 *pCache, 
15484 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
15485 68 65 20 63 61 63 68 65 20 74 6f 20 74 72 75 6e  he cache to trun
15486 63 61 74 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67  cate */..  unsig
15487 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 20 20  ned int iLimit  
15488 20 20 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20          /* Drop 
15489 70 61 67 65 73 20 77 69 74 68 20 74 68 69 73 20  pages with this 
1548a 70 67 6e 6f 20 6f 72 20 6c 61 72 67 65 72 20 2a  pgno or larger *
1548b 2f 0d 0a 29 7b 0d 0a 20 20 54 45 53 54 4f 4e 4c  /..){..  TESTONL
1548c 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  Y( unsigned int 
1548d 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 2f 2a  nPage = 0; )  /*
1548e 20 54 6f 20 61 73 73 65 72 74 20 70 43 61 63 68   To assert pCach
1548f 65 2d 3e 6e 50 61 67 65 20 69 73 20 63 6f 72 72  e->nPage is corr
15490 65 63 74 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e  ect */..  unsign
15491 65 64 20 69 6e 74 20 68 3b 0d 0a 20 20 61 73 73  ed int h;..  ass
15492 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15493 65 78 5f 68 65 6c 64 28 70 43 61 63 68 65 2d 3e  ex_held(pCache->
15494 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29 20 29  pGroup->mutex) )
15495 3b 0d 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c  ;..  for(h=0; h<
15496 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68  pCache->nHash; h
15497 2b 2b 29 7b 0d 0a 20 20 20 20 50 67 48 64 72 31  ++){..    PgHdr1
15498 20 2a 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d   **pp = &pCache-
15499 3e 61 70 48 61 73 68 5b 68 5d 3b 20 0d 0a 20 20  >apHash[h]; ..  
1549a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b    PgHdr1 *pPage;
1549b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 28 70 50  ..    while( (pP
1549c 61 67 65 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b  age = *pp)!=0 ){
1549d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  ..      if( pPag
1549e 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 20  e->iKey>=iLimit 
1549f 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 61 63  ){..        pCac
154a0 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0d 0a 20 20  he->nPage--;..  
154a1 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61 67        *pp = pPag
154a2 65 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20  e->pNext;..     
154a3 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
154a4 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  e(pPage);..     
154a5 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
154a6 67 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 20 20  ge(pPage);..    
154a7 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
154a8 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70    pp = &pPage->p
154a9 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 54  Next;..        T
154aa 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b  ESTONLY( nPage++
154ab 3b 20 29 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ; )..      }..  
154ac 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65    }..  }..  asse
154ad 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  rt( pCache->nPag
154ae 65 3d 3d 6e 50 61 67 65 20 29 3b 0d 0a 7d 0d 0a  e==nPage );..}..
154af 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
154b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b4 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  */../******** sq
154b5 6c 69 74 65 33 5f 70 63 61 63 68 65 20 4d 65 74  lite3_pcache Met
154b6 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods ***********
154b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154b9 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ***/..../*..** I
154ba 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
154bb 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
154bc 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 6f 64  che.xInit method
154bd 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
154be 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f  t pcache1Init(vo
154bf 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a 20  id *NotUsed){.. 
154c0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
154c1 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20 61  R(NotUsed);..  a
154c2 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69  ssert( pcache1.i
154c3 73 49 6e 69 74 3d 3d 30 20 29 3b 0d 0a 20 20 6d  sInit==0 );..  m
154c4 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20  emset(&pcache1, 
154c5 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65  0, sizeof(pcache
154c6 31 29 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  1));..  if( sqli
154c7 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
154c8 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0d 0a 20  bCoreMutex ){.. 
154c9 20 20 20 70 63 61 63 68 65 31 2e 67 72 70 2e 6d     pcache1.grp.m
154ca 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d  utex = sqlite3_m
154cb 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54  utex_alloc(SQLIT
154cc 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
154cd 52 55 29 3b 0d 0a 20 20 20 20 70 63 61 63 68 65  RU);..    pcache
154ce 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  1.mutex = sqlite
154cf 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
154d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
154d1 43 5f 50 4d 45 4d 29 3b 0d 0a 20 20 7d 0d 0a 20  C_PMEM);..  }.. 
154d2 20 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 78 50   pcache1.grp.mxP
154d3 69 6e 6e 65 64 20 3d 20 31 30 3b 0d 0a 20 20 70  inned = 10;..  p
154d4 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20 3d 20  cache1.isInit = 
154d5 31 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1;..  return SQL
154d6 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
154d7 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
154d8 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
154d9 65 33 5f 70 63 61 63 68 65 2e 78 53 68 75 74 64  e3_pcache.xShutd
154da 6f 77 6e 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2a 20  own method...** 
154db 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 74  Note that the st
154dc 61 74 69 63 20 6d 75 74 65 78 20 61 6c 6c 6f 63  atic mutex alloc
154dd 61 74 65 64 20 69 6e 20 78 49 6e 69 74 20 64 6f  ated in xInit do
154de 65 73 20 0d 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64  es ..** not need
154df 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0d 0a 2a   to be freed...*
154e0 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  /..static void p
154e1 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76  cache1Shutdown(v
154e2 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a  oid *NotUsed){..
154e3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
154e4 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20  ER(NotUsed);..  
154e5 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
154e6 69 73 49 6e 69 74 21 3d 30 20 29 3b 0d 0a 20 20  isInit!=0 );..  
154e7 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c  memset(&pcache1,
154e8 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68   0, sizeof(pcach
154e9 65 31 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  e1));..}..../*..
154ea 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
154eb 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
154ec 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 20  _pcache.xCreate 
154ed 6d 65 74 68 6f 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  method...**..** 
154ee 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63  Allocate a new c
154ef 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ache...*/..stati
154f0 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  c sqlite3_pcache
154f1 20 2a 70 63 61 63 68 65 31 43 72 65 61 74 65 28   *pcache1Create(
154f2 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20  int szPage, int 
154f3 73 7a 45 78 74 72 61 2c 20 69 6e 74 20 62 50 75  szExtra, int bPu
154f4 72 67 65 61 62 6c 65 29 7b 0d 0a 20 20 50 43 61  rgeable){..  PCa
154f5 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 20  che1 *pCache;   
154f6 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 6c 79 20     /* The newly 
154f7 63 72 65 61 74 65 64 20 70 61 67 65 20 63 61 63  created page cac
154f8 68 65 20 2a 2f 0d 0a 20 20 50 47 72 6f 75 70 20  he */..  PGroup 
154f9 2a 70 47 72 6f 75 70 3b 20 20 20 20 20 20 20 2f  *pGroup;       /
154fa 2a 20 54 68 65 20 67 72 6f 75 70 20 74 68 65 20  * The group the 
154fb 6e 65 77 20 70 61 67 65 20 63 61 63 68 65 20 77  new page cache w
154fc 69 6c 6c 20 62 65 6c 6f 6e 67 20 74 6f 20 2a 2f  ill belong to */
154fd 0d 0a 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20  ..  int sz;     
154fe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
154ff 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71  es of memory req
15500 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74  uired to allocat
15501 65 20 74 68 65 20 6e 65 77 20 63 61 63 68 65 20  e the new cache 
15502 2a 2f 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a  */....  /*..  **
15503 20 54 68 65 20 73 65 70 65 72 61 74 65 43 61 63   The seperateCac
15504 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 74  he variable is t
15505 72 75 65 20 69 66 20 65 61 63 68 20 50 43 61 63  rue if each PCac
15506 68 65 20 68 61 73 20 69 74 73 20 6f 77 6e 20 70  he has its own p
15507 72 69 76 61 74 65 0d 0a 20 20 2a 2a 20 50 47 72  rivate..  ** PGr
15508 6f 75 70 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  oup.  In other w
15509 6f 72 64 73 2c 20 73 65 70 61 72 61 74 65 43 61  ords, separateCa
1550a 63 68 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  che is true for 
1550b 6d 6f 64 65 20 28 31 29 20 77 68 65 72 65 20 6e  mode (1) where n
1550c 6f 0d 0a 20 20 2a 2a 20 6d 75 74 65 78 69 6e 67  o..  ** mutexing
1550d 20 69 73 20 72 65 71 75 69 72 65 64 2e 0d 0a 20   is required... 
1550e 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 2a 20 20 41   **..  **   *  A
1550f 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e 69 66  lways use a unif
15510 69 65 64 20 63 61 63 68 65 20 28 6d 6f 64 65 2d  ied cache (mode-
15511 32 29 20 69 66 20 45 4e 41 42 4c 45 5f 4d 45 4d  2) if ENABLE_MEM
15512 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0d 0a  ORY_MANAGEMENT..
15513 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 2a 20 20    **..  **   *  
15514 41 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e 69  Always use a uni
15515 66 69 65 64 20 63 61 63 68 65 20 69 6e 20 73 69  fied cache in si
15516 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
15517 70 6c 69 63 61 74 69 6f 6e 73 0d 0a 20 20 2a 2a  plications..  **
15518 0d 0a 20 20 2a 2a 20 20 20 2a 20 20 4f 74 68 65  ..  **   *  Othe
15519 72 77 69 73 65 20 28 69 66 20 6d 75 6c 74 69 2d  rwise (if multi-
1551a 74 68 72 65 61 64 65 64 20 61 6e 64 20 45 4e 41  threaded and ENA
1551b 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
1551c 45 4d 45 4e 54 20 69 73 20 6f 66 66 29 0d 0a 20  EMENT is off).. 
1551d 20 2a 2a 20 20 20 20 20 20 75 73 65 20 73 65 70   **      use sep
1551e 61 72 61 74 65 20 63 61 63 68 65 73 20 28 6d 6f  arate caches (mo
1551f 64 65 2d 31 29 0d 0a 20 20 2a 2f 0d 0a 23 69 66  de-1)..  */..#if
15520 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
15521 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
15522 4e 41 47 45 4d 45 4e 54 29 20 7c 7c 20 53 51 4c  NAGEMENT) || SQL
15523 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
15524 30 0d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 73  0..  const int s
15525 65 70 61 72 61 74 65 43 61 63 68 65 20 3d 20 30  eparateCache = 0
15526 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 69 6e 74 20  ;..#else..  int 
15527 73 65 70 61 72 61 74 65 43 61 63 68 65 20 3d 20  separateCache = 
15528 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
15529 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3e 30  fig.bCoreMutex>0
1552a 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 61  ;..#endif....  a
1552b 73 73 65 72 74 28 20 28 73 7a 50 61 67 65 20 26  ssert( (szPage &
1552c 20 28 73 7a 50 61 67 65 2d 31 29 29 3d 3d 30 20   (szPage-1))==0 
1552d 26 26 20 73 7a 50 61 67 65 3e 3d 35 31 32 20 26  && szPage>=512 &
1552e 26 20 73 7a 50 61 67 65 3c 3d 36 35 35 33 36 20  & szPage<=65536 
1552f 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 7a  );..  assert( sz
15530 45 78 74 72 61 20 3c 20 33 30 30 20 29 3b 0d 0a  Extra < 300 );..
15531 0d 0a 20 20 73 7a 20 3d 20 73 69 7a 65 6f 66 28  ..  sz = sizeof(
15532 50 43 61 63 68 65 31 29 20 2b 20 73 69 7a 65 6f  PCache1) + sizeo
15533 66 28 50 47 72 6f 75 70 29 2a 73 65 70 61 72 61  f(PGroup)*separa
15534 74 65 43 61 63 68 65 3b 0d 0a 20 20 70 43 61 63  teCache;..  pCac
15535 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29  he = (PCache1 *)
15536 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
15537 7a 29 3b 0d 0a 20 20 69 66 28 20 70 43 61 63 68  z);..  if( pCach
15538 65 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 73 65 74  e ){..    memset
15539 28 70 43 61 63 68 65 2c 20 30 2c 20 73 7a 29 3b  (pCache, 0, sz);
1553a 0d 0a 20 20 20 20 69 66 28 20 73 65 70 61 72 61  ..    if( separa
1553b 74 65 43 61 63 68 65 20 29 7b 0d 0a 20 20 20 20  teCache ){..    
1553c 20 20 70 47 72 6f 75 70 20 3d 20 28 50 47 72 6f    pGroup = (PGro
1553d 75 70 2a 29 26 70 43 61 63 68 65 5b 31 5d 3b 0d  up*)&pCache[1];.
1553e 0a 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 6d  .      pGroup->m
1553f 78 50 69 6e 6e 65 64 20 3d 20 31 30 3b 0d 0a 20  xPinned = 10;.. 
15540 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
15541 20 70 47 72 6f 75 70 20 3d 20 26 70 63 61 63 68   pGroup = &pcach
15542 65 31 2e 67 72 70 3b 0d 0a 20 20 20 20 7d 0d 0a  e1.grp;..    }..
15543 20 20 20 20 70 43 61 63 68 65 2d 3e 70 47 72 6f      pCache->pGro
15544 75 70 20 3d 20 70 47 72 6f 75 70 3b 0d 0a 20 20  up = pGroup;..  
15545 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65    pCache->szPage
15546 20 3d 20 73 7a 50 61 67 65 3b 0d 0a 20 20 20 20   = szPage;..    
15547 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20  pCache->szExtra 
15548 3d 20 73 7a 45 78 74 72 61 3b 0d 0a 20 20 20 20  = szExtra;..    
15549 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
1554a 6c 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c 65  le = (bPurgeable
1554b 20 3f 20 31 20 3a 20 30 29 3b 0d 0a 20 20 20 20   ? 1 : 0);..    
1554c 69 66 28 20 62 50 75 72 67 65 61 62 6c 65 20 29  if( bPurgeable )
1554d 7b 0d 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d  {..      pCache-
1554e 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0d 0a 20 20 20  >nMin = 10;..   
1554f 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
15550 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0d 0a 20  utex(pGroup);.. 
15551 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 69       pGroup->nMi
15552 6e 50 61 67 65 20 2b 3d 20 70 43 61 63 68 65 2d  nPage += pCache-
15553 3e 6e 4d 69 6e 3b 0d 0a 20 20 20 20 20 20 70 47  >nMin;..      pG
15554 72 6f 75 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d  roup->mxPinned =
15555 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67   pGroup->nMaxPag
15556 65 20 2b 20 31 30 20 2d 20 70 47 72 6f 75 70 2d  e + 10 - pGroup-
15557 3e 6e 4d 69 6e 50 61 67 65 3b 0d 0a 20 20 20 20  >nMinPage;..    
15558 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
15559 74 65 78 28 70 47 72 6f 75 70 29 3b 0d 0a 20 20  tex(pGroup);..  
1555a 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
1555b 72 6e 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63  rn (sqlite3_pcac
1555c 68 65 20 2a 29 70 43 61 63 68 65 3b 0d 0a 7d 0d  he *)pCache;..}.
1555d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d  .../*..** Implem
1555e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
1555f 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
15560 43 61 63 68 65 73 69 7a 65 20 6d 65 74 68 6f 64  Cachesize method
15561 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 43 6f 6e 66 69  . ..**..** Confi
15562 67 75 72 65 20 74 68 65 20 63 61 63 68 65 5f 73  gure the cache_s
15563 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 20  ize limit for a 
15564 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  cache...*/..stat
15565 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 43  ic void pcache1C
15566 61 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 33  achesize(sqlite3
15567 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 20  _pcache *p, int 
15568 6e 4d 61 78 29 7b 0d 0a 20 20 50 43 61 63 68 65  nMax){..  PCache
15569 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61  1 *pCache = (PCa
1556a 63 68 65 31 20 2a 29 70 3b 0d 0a 20 20 69 66 28  che1 *)p;..  if(
1556b 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
1556c 62 6c 65 20 29 7b 0d 0a 20 20 20 20 50 47 72 6f  ble ){..    PGro
1556d 75 70 20 2a 70 47 72 6f 75 70 20 3d 20 70 43 61  up *pGroup = pCa
1556e 63 68 65 2d 3e 70 47 72 6f 75 70 3b 0d 0a 20 20  che->pGroup;..  
1556f 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
15570 74 65 78 28 70 47 72 6f 75 70 29 3b 0d 0a 20 20  tex(pGroup);..  
15571 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61    pGroup->nMaxPa
15572 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43  ge += (nMax - pC
15573 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0d 0a 20 20  ache->nMax);..  
15574 20 20 70 47 72 6f 75 70 2d 3e 6d 78 50 69 6e 6e    pGroup->mxPinn
15575 65 64 20 3d 20 70 47 72 6f 75 70 2d 3e 6e 4d 61  ed = pGroup->nMa
15576 78 50 61 67 65 20 2b 20 31 30 20 2d 20 70 47 72  xPage + 10 - pGr
15577 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 3b 0d 0a  oup->nMinPage;..
15578 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78      pCache->nMax
15579 20 3d 20 6e 4d 61 78 3b 0d 0a 20 20 20 20 70 43   = nMax;..    pC
1557a 61 63 68 65 2d 3e 6e 39 30 70 63 74 20 3d 20 70  ache->n90pct = p
1557b 43 61 63 68 65 2d 3e 6e 4d 61 78 2a 39 2f 31 30  Cache->nMax*9/10
1557c 3b 0d 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e  ;..    pcache1En
1557d 66 6f 72 63 65 4d 61 78 50 61 67 65 28 70 47 72  forceMaxPage(pGr
1557e 6f 75 70 29 3b 0d 0a 20 20 20 20 70 63 61 63 68  oup);..    pcach
1557f 65 31 4c 65 61 76 65 4d 75 74 65 78 28 70 47 72  e1LeaveMutex(pGr
15580 6f 75 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  oup);..  }..}...
15581 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
15582 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
15583 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 68  lite3_pcache.xSh
15584 72 69 6e 6b 20 6d 65 74 68 6f 64 2e 20 0d 0a 2a  rink method. ..*
15585 2a 0d 0a 2a 2a 20 46 72 65 65 20 75 70 20 61 73  *..** Free up as
15586 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 73 20   much memory as 
15587 70 6f 73 73 69 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73  possible...*/..s
15588 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
15589 65 31 53 68 72 69 6e 6b 28 73 71 6c 69 74 65 33  e1Shrink(sqlite3
1558a 5f 70 63 61 63 68 65 20 2a 70 29 7b 0d 0a 20 20  _pcache *p){..  
1558b 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
1558c 3d 20 28 50 43 61 63 68 65 31 2a 29 70 3b 0d 0a  = (PCache1*)p;..
1558d 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50    if( pCache->bP
1558e 75 72 67 65 61 62 6c 65 20 29 7b 0d 0a 20 20 20  urgeable ){..   
1558f 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 20   PGroup *pGroup 
15590 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70  = pCache->pGroup
15591 3b 0d 0a 20 20 20 20 69 6e 74 20 73 61 76 65 64  ;..    int saved
15592 4d 61 78 50 61 67 65 3b 0d 0a 20 20 20 20 70 63  MaxPage;..    pc
15593 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
15594 70 47 72 6f 75 70 29 3b 0d 0a 20 20 20 20 73 61  pGroup);..    sa
15595 76 65 64 4d 61 78 50 61 67 65 20 3d 20 70 47 72  vedMaxPage = pGr
15596 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65 3b 0d 0a  oup->nMaxPage;..
15597 20 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78      pGroup->nMax
15598 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70  Page = 0;..    p
15599 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78  cache1EnforceMax
1559a 50 61 67 65 28 70 47 72 6f 75 70 29 3b 0d 0a 20  Page(pGroup);.. 
1559b 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50     pGroup->nMaxP
1559c 61 67 65 20 3d 20 73 61 76 65 64 4d 61 78 50 61  age = savedMaxPa
1559d 67 65 3b 0d 0a 20 20 20 20 70 63 61 63 68 65 31  ge;..    pcache1
1559e 4c 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75  LeaveMutex(pGrou
1559f 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  p);..  }..}..../
155a0 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  *..** Implementa
155a1 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
155a2 74 65 33 5f 70 63 61 63 68 65 2e 78 50 61 67 65  te3_pcache.xPage
155a3 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e 20 0d 0a  count method. ..
155a4 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  */..static int p
155a5 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 28  cache1Pagecount(
155a6 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
155a7 70 29 7b 0d 0a 20 20 69 6e 74 20 6e 3b 0d 0a 20  p){..  int n;.. 
155a8 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
155a9 20 3d 20 28 50 43 61 63 68 65 31 2a 29 70 3b 0d   = (PCache1*)p;.
155aa 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  .  pcache1EnterM
155ab 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72  utex(pCache->pGr
155ac 6f 75 70 29 3b 0d 0a 20 20 6e 20 3d 20 70 43 61  oup);..  n = pCa
155ad 63 68 65 2d 3e 6e 50 61 67 65 3b 0d 0a 20 20 70  che->nPage;..  p
155ae 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
155af 28 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29  (pCache->pGroup)
155b0 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0d 0a  ;..  return n;..
155b1 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c  }..../*..** Impl
155b2 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
155b3 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
155b4 2e 78 46 65 74 63 68 20 6d 65 74 68 6f 64 2e 20  .xFetch method. 
155b5 0d 0a 2a 2a 0d 0a 2a 2a 20 46 65 74 63 68 20 61  ..**..** Fetch a
155b6 20 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 6c   page by key val
155b7 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 74  ue...**..** Whet
155b8 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77  her or not a new
155b9 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 6c 6c   page may be all
155ba 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66  ocated by this f
155bb 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20  unction depends 
155bc 6f 6e 0d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  on..** the value
155bd 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 6c   of the createFl
155be 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 20 30 20  ag argument.  0 
155bf 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c  means do not all
155c0 6f 63 61 74 65 20 61 20 6e 65 77 0d 0a 2a 2a 20  ocate a new..** 
155c1 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 20 61  page.  1 means a
155c2 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
155c3 67 65 20 69 66 20 73 70 61 63 65 20 69 73 20 65  ge if space is e
155c4 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e  asily available.
155c5 20 20 32 20 0d 0a 2a 2a 20 6d 65 61 6e 73 20 74    2 ..** means t
155c6 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 72  o try really har
155c7 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  d to allocate a 
155c8 6e 65 77 20 70 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a  new page...**..*
155c9 2a 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67  * For a non-purg
155ca 65 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63  eable cache (a c
155cb 61 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65  ache used as the
155cc 20 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20   storage for an 
155cd 69 6e 2d 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 64 61  in-memory..** da
155ce 74 61 62 61 73 65 29 20 74 68 65 72 65 20 69 73  tabase) there is
155cf 20 72 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65   really no diffe
155d0 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72  rence between cr
155d1 65 61 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32  eateFlag 1 and 2
155d2 2e 20 20 53 6f 0d 0a 2a 2a 20 74 68 65 20 63 61  .  So..** the ca
155d3 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28  lling function (
155d4 70 63 61 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e  pcache.c) will n
155d5 65 76 65 72 20 68 61 76 65 20 61 20 63 72 65 61  ever have a crea
155d6 74 65 46 6c 61 67 20 6f 66 20 31 20 6f 6e 0d 0a  teFlag of 1 on..
155d7 2a 2a 20 61 20 6e 6f 6e 2d 70 75 72 67 65 61 62  ** a non-purgeab
155d8 6c 65 20 63 61 63 68 65 2e 0d 0a 2a 2a 0d 0a 2a  le cache...**..*
155d9 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  * There are thre
155da 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 72  e different appr
155db 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 6e  oaches to obtain
155dc 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 20  ing space for a 
155dd 70 61 67 65 2c 0d 0a 2a 2a 20 64 65 70 65 6e 64  page,..** depend
155de 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  ing on the value
155df 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 72   of parameter cr
155e0 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 20  eateFlag (which 
155e1 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32  may be 0, 1 or 2
155e2 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31 2e 20  )...**..**   1. 
155e3 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  Regardless of th
155e4 65 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74  e value of creat
155e5 65 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65  eFlag, the cache
155e6 20 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72   is searched for
155e7 20 61 20 0d 0a 2a 2a 20 20 20 20 20 20 63 6f 70   a ..**      cop
155e8 79 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  y of the request
155e9 65 64 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20  ed page. If one 
155ea 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20  is found, it is 
155eb 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a  returned...**..*
155ec 2a 20 20 20 32 2e 20 49 66 20 63 72 65 61 74 65  *   2. If create
155ed 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65 20  Flag==0 and the 
155ee 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  page is not alre
155ef 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  ady in the cache
155f0 2c 20 4e 55 4c 4c 20 69 73 0d 0a 2a 2a 20 20 20  , NULL is..**   
155f1 20 20 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a     returned...**
155f2 0d 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65  ..**   3. If cre
155f3 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e  ateFlag is 1, an
155f4 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
155f5 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
155f6 20 63 61 63 68 65 2c 20 74 68 65 6e 0d 0a 2a 2a   cache, then..**
155f7 20 20 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c        return NUL
155f8 4c 20 28 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61  L (do not alloca
155f9 74 65 20 61 20 6e 65 77 20 70 61 67 65 29 20 69  te a new page) i
155fa 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c  f any of the fol
155fb 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 20 20 20 20 20  lowing..**      
155fc 63 6f 6e 64 69 74 69 6f 6e 73 20 61 72 65 20 74  conditions are t
155fd 72 75 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  rue:..**..**    
155fe 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65     (a) the numbe
155ff 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65  r of pages pinne
15600 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69  d by the cache i
15601 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0d 0a  s greater than..
15602 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61  **           PCa
15603 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0d 0a 2a  che1.nMax, or..*
15604 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20  *..**       (b) 
15605 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
15606 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68  ges pinned by th
15607 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74  e cache is great
15608 65 72 20 74 68 61 6e 0d 0a 2a 2a 20 20 20 20 20  er than..**     
15609 20 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f 66        the sum of
1560a 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 75   nMax for all pu
1560b 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 20  rgeable caches, 
1560c 6c 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 20  less the sum of 
1560d 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e  ..**           n
1560e 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65  Min for all othe
1560f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  r purgeable cach
15610 65 73 2c 20 6f 72 0d 0a 2a 2a 0d 0a 2a 2a 20 20  es, or..**..**  
15611 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74   4. If none of t
15612 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 63  he first three c
15613 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79 20  onditions apply 
15614 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69 73  and the cache is
15615 20 6d 61 72 6b 65 64 0d 0a 2a 2a 20 20 20 20 20   marked..**     
15616 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61   as purgeable, a
15617 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  nd if one of the
15618 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72   following is tr
15619 75 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  ue:..**..**     
1561a 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72    (a) The number
1561b 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61   of pages alloca
1561c 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68  ted for the cach
1561d 65 20 69 73 20 61 6c 72 65 61 64 79 20 0d 0a 2a  e is already ..*
1561e 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63  *           PCac
1561f 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0d 0a 2a 2a  he1.nMax, or..**
15620 0d 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54  ..**       (b) T
15621 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
15622 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
15623 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63   all purgeable c
15624 61 63 68 65 73 20 69 73 0d 0a 2a 2a 20 20 20 20  aches is..**    
15625 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 65         already e
15626 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74  qual to or great
15627 65 72 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20  er than the sum 
15628 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0d  of nMax for all.
15629 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 70 75  .**           pu
1562a 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 0d  rgeable caches,.
1562b 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 28 63  .**..**       (c
1562c 29 20 54 68 65 20 73 79 73 74 65 6d 20 69 73 20  ) The system is 
1562d 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72 65  under memory pre
1562e 73 73 75 72 65 20 61 6e 64 20 77 61 6e 74 73 20  ssure and wants 
1562f 74 6f 20 61 76 6f 69 64 0d 0a 2a 2a 20 20 20 20  to avoid..**    
15630 20 20 20 20 20 20 20 75 6e 6e 65 63 65 73 73 61         unnecessa
15631 72 79 20 70 61 67 65 73 20 63 61 63 68 65 20 65  ry pages cache e
15632 6e 74 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  ntry allocations
15633 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 74 68  ..**..**      th
15634 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  en attempt to re
15635 63 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 6f  cycle a page fro
15636 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20  m the LRU list. 
15637 49 66 20 69 74 20 69 73 20 74 68 65 20 72 69 67  If it is the rig
15638 68 74 0d 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65  ht..**      size
15639 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 63  , return the rec
1563a 79 63 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74  ycled buffer. Ot
1563b 68 65 72 77 69 73 65 2c 20 66 72 65 65 20 74 68  herwise, free th
1563c 65 20 62 75 66 66 65 72 20 61 6e 64 0d 0a 2a 2a  e buffer and..**
1563d 20 20 20 20 20 20 70 72 6f 63 65 65 64 20 74 6f        proceed to
1563e 20 73 74 65 70 20 35 2e 20 0d 0a 2a 2a 0d 0a 2a   step 5. ..**..*
1563f 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69 73 65  *   5. Otherwise
15640 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72  , allocate and r
15641 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61 67 65  eturn a new page
15642 20 62 75 66 66 65 72 2e 0d 0a 2a 2f 0d 0a 73 74   buffer...*/..st
15643 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61  atic sqlite3_pca
15644 63 68 65 5f 70 61 67 65 20 2a 70 63 61 63 68 65  che_page *pcache
15645 31 46 65 74 63 68 28 0d 0a 20 20 73 71 6c 69 74  1Fetch(..  sqlit
15646 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 0d 0a  e3_pcache *p, ..
15647 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
15648 4b 65 79 2c 20 0d 0a 20 20 69 6e 74 20 63 72 65  Key, ..  int cre
15649 61 74 65 46 6c 61 67 0d 0a 29 7b 0d 0a 20 20 75  ateFlag..){..  u
1564a 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 69 6e  nsigned int nPin
1564b 6e 65 64 3b 0d 0a 20 20 50 43 61 63 68 65 31 20  ned;..  PCache1 
1564c 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
1564d 65 31 20 2a 29 70 3b 0d 0a 20 20 50 47 72 6f 75  e1 *)p;..  PGrou
1564e 70 20 2a 70 47 72 6f 75 70 3b 0d 0a 20 20 50 67  p *pGroup;..  Pg
1564f 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b  Hdr1 *pPage = 0;
15650 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43  ....  assert( pC
15651 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
15652 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 21 3d   || createFlag!=
15653 31 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  1 );..  assert( 
15654 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
15655 6c 65 20 7c 7c 20 70 43 61 63 68 65 2d 3e 6e 4d  le || pCache->nM
15656 69 6e 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  in==0 );..  asse
15657 72 74 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72  rt( pCache->bPur
15658 67 65 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 43 61  geable==0 || pCa
15659 63 68 65 2d 3e 6e 4d 69 6e 3d 3d 31 30 20 29 3b  che->nMin==10 );
1565a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63  ..  assert( pCac
1565b 68 65 2d 3e 6e 4d 69 6e 3d 3d 30 20 7c 7c 20 70  he->nMin==0 || p
1565c 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
1565d 65 20 29 3b 0d 0a 20 20 70 63 61 63 68 65 31 45  e );..  pcache1E
1565e 6e 74 65 72 4d 75 74 65 78 28 70 47 72 6f 75 70  nterMutex(pGroup
1565f 20 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75   = pCache->pGrou
15660 70 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 74 65 70  p);....  /* Step
15661 20 31 3a 20 53 65 61 72 63 68 20 74 68 65 20 68   1: Search the h
15662 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e  ash table for an
15663 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e   existing entry.
15664 20 2a 2f 0d 0a 20 20 69 66 28 20 70 43 61 63 68   */..  if( pCach
15665 65 2d 3e 6e 48 61 73 68 3e 30 20 29 7b 0d 0a 20  e->nHash>0 ){.. 
15666 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
15667 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68  h = iKey % pCach
15668 65 2d 3e 6e 48 61 73 68 3b 0d 0a 20 20 20 20 66  e->nHash;..    f
15669 6f 72 28 70 50 61 67 65 3d 70 43 61 63 68 65 2d  or(pPage=pCache-
1566a 3e 61 70 48 61 73 68 5b 68 5d 3b 20 70 50 61 67  >apHash[h]; pPag
1566b 65 26 26 70 50 61 67 65 2d 3e 69 4b 65 79 21 3d  e&&pPage->iKey!=
1566c 69 4b 65 79 3b 20 70 50 61 67 65 3d 70 50 61 67  iKey; pPage=pPag
1566d 65 2d 3e 70 4e 65 78 74 29 3b 0d 0a 20 20 7d 0d  e->pNext);..  }.
1566e 0a 0d 0a 20 20 2f 2a 20 53 74 65 70 20 32 3a 20  ...  /* Step 2: 
1566f 41 62 6f 72 74 20 69 66 20 6e 6f 20 65 78 69 73  Abort if no exis
15670 74 69 6e 67 20 70 61 67 65 20 69 73 20 66 6f 75  ting page is fou
15671 6e 64 20 61 6e 64 20 63 72 65 61 74 65 46 6c 61  nd and createFla
15672 67 20 69 73 20 30 20 2a 2f 0d 0a 20 20 69 66 28  g is 0 */..  if(
15673 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65   pPage || create
15674 46 6c 61 67 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Flag==0 ){..    
15675 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
15676 50 61 67 65 29 3b 0d 0a 20 20 20 20 67 6f 74 6f  Page);..    goto
15677 20 66 65 74 63 68 5f 6f 75 74 3b 0d 0a 20 20 7d   fetch_out;..  }
15678 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 70 47 72  ....  /* The pGr
15679 6f 75 70 20 6c 6f 63 61 6c 20 76 61 72 69 61 62  oup local variab
1567a 6c 65 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79  le will normally
1567b 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
1567c 62 79 20 74 68 65 0d 0a 20 20 2a 2a 20 70 63 61  by the..  ** pca
1567d 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
1567e 20 6d 61 63 72 6f 20 61 62 6f 76 65 2e 20 20 42   macro above.  B
1567f 75 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 55 54  ut if SQLITE_MUT
15680 45 58 5f 4f 4d 49 54 20 69 73 20 64 65 66 69 6e  EX_OMIT is defin
15681 65 64 2c 0d 0a 20 20 2a 2a 20 74 68 65 6e 20 70  ed,..  ** then p
15682 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
15683 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 73  () is a no-op, s
15684 6f 20 77 65 20 68 61 76 65 20 74 6f 20 69 6e 69  o we have to ini
15685 74 69 61 6c 69 7a 65 20 74 68 65 0d 0a 20 20 2a  tialize the..  *
15686 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
15687 20 68 65 72 65 2e 20 20 44 65 6c 61 79 69 6e 67   here.  Delaying
15688 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
15689 69 6f 6e 20 6f 66 20 70 47 72 6f 75 70 20 69 73  ion of pGroup is
1568a 20 61 6e 0d 0a 20 20 2a 2a 20 6f 70 74 69 6d 69   an..  ** optimi
1568b 7a 61 74 69 6f 6e 3a 20 20 54 68 65 20 63 6f 6d  zation:  The com
1568c 6d 6f 6e 20 63 61 73 65 20 69 73 20 74 6f 20 65  mon case is to e
1568d 78 69 74 20 74 68 65 20 6d 6f 64 75 6c 65 20 62  xit the module b
1568e 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 0d 0a  efore reaching..
1568f 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 2e    ** this point.
15690 0d 0a 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53  ..  */..#ifdef S
15691 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
15692 0d 0a 20 20 70 47 72 6f 75 70 20 3d 20 70 43 61  ..  pGroup = pCa
15693 63 68 65 2d 3e 70 47 72 6f 75 70 3b 0d 0a 23 65  che->pGroup;..#e
15694 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 53 74 65  ndif....  /* Ste
15695 70 20 33 3a 20 41 62 6f 72 74 20 69 66 20 63 72  p 3: Abort if cr
15696 65 61 74 65 46 6c 61 67 20 69 73 20 31 20 62 75  eateFlag is 1 bu
15697 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 6e  t the cache is n
15698 65 61 72 6c 79 20 66 75 6c 6c 20 2a 2f 0d 0a 20  early full */.. 
15699 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
1569a 3e 6e 50 61 67 65 20 3e 3d 20 70 43 61 63 68 65  >nPage >= pCache
1569b 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 20 29 3b  ->nRecyclable );
1569c 0d 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43  ..  nPinned = pC
1569d 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43  ache->nPage - pC
1569e 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c  ache->nRecyclabl
1569f 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 47  e;..  assert( pG
156a0 72 6f 75 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d  roup->mxPinned =
156a1 3d 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61  = pGroup->nMaxPa
156a2 67 65 20 2b 20 31 30 20 2d 20 70 47 72 6f 75 70  ge + 10 - pGroup
156a3 2d 3e 6e 4d 69 6e 50 61 67 65 20 29 3b 0d 0a 20  ->nMinPage );.. 
156a4 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
156a5 3e 6e 39 30 70 63 74 20 3d 3d 20 70 43 61 63 68  >n90pct == pCach
156a6 65 2d 3e 6e 4d 61 78 2a 39 2f 31 30 20 29 3b 0d  e->nMax*9/10 );.
156a7 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61  .  if( createFla
156a8 67 3d 3d 31 20 26 26 20 28 0d 0a 20 20 20 20 20  g==1 && (..     
156a9 20 20 20 6e 50 69 6e 6e 65 64 3e 3d 70 47 72 6f     nPinned>=pGro
156aa 75 70 2d 3e 6d 78 50 69 6e 6e 65 64 0d 0a 20 20  up->mxPinned..  
156ab 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 70     || nPinned>=p
156ac 43 61 63 68 65 2d 3e 6e 39 30 70 63 74 0d 0a 20  Cache->n90pct.. 
156ad 20 20 20 20 7c 7c 20 70 63 61 63 68 65 31 55 6e      || pcache1Un
156ae 64 65 72 4d 65 6d 6f 72 79 50 72 65 73 73 75 72  derMemoryPressur
156af 65 28 70 43 61 63 68 65 29 0d 0a 20 20 29 29 7b  e(pCache)..  )){
156b0 0d 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68  ..    goto fetch
156b1 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  _out;..  }....  
156b2 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  if( pCache->nPag
156b3 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68  e>=pCache->nHash
156b4 20 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a   && pcache1Resiz
156b5 65 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b  eHash(pCache) ){
156b6 0d 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68  ..    goto fetch
156b7 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  _out;..  }....  
156b8 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 74  /* Step 4. Try t
156b9 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 65  o recycle a page
156ba 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 70 43 61 63  . */..  if( pCac
156bb 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26  he->bPurgeable &
156bc 26 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61  & pGroup->pLruTa
156bd 69 6c 20 26 26 20 28 0d 0a 20 20 20 20 20 20 20  il && (..       
156be 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65    (pCache->nPage
156bf 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78  +1>=pCache->nMax
156c0 29 0d 0a 20 20 20 20 20 20 7c 7c 20 70 47 72 6f  )..      || pGro
156c1 75 70 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67 65  up->nCurrentPage
156c2 3e 3d 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61  >=pGroup->nMaxPa
156c3 67 65 0d 0a 20 20 20 20 20 20 7c 7c 20 70 63 61  ge..      || pca
156c4 63 68 65 31 55 6e 64 65 72 4d 65 6d 6f 72 79 50  che1UnderMemoryP
156c5 72 65 73 73 75 72 65 28 70 43 61 63 68 65 29 0d  ressure(pCache).
156c6 0a 20 20 29 29 7b 0d 0a 20 20 20 20 50 43 61 63  .  )){..    PCac
156c7 68 65 31 20 2a 70 4f 74 68 65 72 3b 0d 0a 20 20  he1 *pOther;..  
156c8 20 20 70 50 61 67 65 20 3d 20 70 47 72 6f 75 70    pPage = pGroup
156c9 2d 3e 70 4c 72 75 54 61 69 6c 3b 0d 0a 20 20 20  ->pLruTail;..   
156ca 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72   pcache1RemoveFr
156cb 6f 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0d 0a  omHash(pPage);..
156cc 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
156cd 67 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 20 20  ge(pPage);..    
156ce 70 4f 74 68 65 72 20 3d 20 70 50 61 67 65 2d 3e  pOther = pPage->
156cf 70 43 61 63 68 65 3b 0d 0a 0d 0a 20 20 20 20 2f  pCache;....    /
156d0 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 76 65 72  * We want to ver
156d1 69 66 79 20 74 68 61 74 20 73 7a 50 61 67 65 20  ify that szPage 
156d2 61 6e 64 20 73 7a 45 78 74 72 61 20 61 72 65 20  and szExtra are 
156d3 74 68 65 20 73 61 6d 65 20 66 6f 72 20 70 4f 74  the same for pOt
156d4 68 65 72 0d 0a 20 20 20 20 2a 2a 20 61 6e 64 20  her..    ** and 
156d5 70 43 61 63 68 65 2e 20 20 41 73 73 65 72 74 20  pCache.  Assert 
156d6 74 68 61 74 20 77 65 20 63 61 6e 20 76 65 72 69  that we can veri
156d7 66 79 20 74 68 69 73 20 62 79 20 63 6f 6d 70 61  fy this by compa
156d8 72 69 6e 67 20 73 75 6d 73 2e 20 2a 2f 0d 0a 20  ring sums. */.. 
156d9 20 20 20 61 73 73 65 72 74 28 20 28 70 43 61 63     assert( (pCac
156da 68 65 2d 3e 73 7a 50 61 67 65 20 26 20 28 70 43  he->szPage & (pC
156db 61 63 68 65 2d 3e 73 7a 50 61 67 65 2d 31 29 29  ache->szPage-1))
156dc 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 73  ==0 && pCache->s
156dd 7a 50 61 67 65 3e 3d 35 31 32 20 29 3b 0d 0a 20  zPage>=512 );.. 
156de 20 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68     assert( pCach
156df 65 2d 3e 73 7a 45 78 74 72 61 3c 35 31 32 20 29  e->szExtra<512 )
156e0 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ;..    assert( (
156e1 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 20 26  pOther->szPage &
156e2 20 28 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65   (pOther->szPage
156e3 2d 31 29 29 3d 3d 30 20 26 26 20 70 4f 74 68 65  -1))==0 && pOthe
156e4 72 2d 3e 73 7a 50 61 67 65 3e 3d 35 31 32 20 29  r->szPage>=512 )
156e5 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
156e6 4f 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 3c 35  Other->szExtra<5
156e7 31 32 20 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28  12 );....    if(
156e8 20 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 2b   pOther->szPage+
156e9 70 4f 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 20  pOther->szExtra 
156ea 21 3d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  != pCache->szPag
156eb 65 2b 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72  e+pCache->szExtr
156ec 61 20 29 7b 0d 0a 20 20 20 20 20 20 70 63 61 63  a ){..      pcac
156ed 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67  he1FreePage(pPag
156ee 65 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 67 65  e);..      pPage
156ef 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65   = 0;..    }else
156f0 7b 0d 0a 20 20 20 20 20 20 70 47 72 6f 75 70 2d  {..      pGroup-
156f1 3e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d  >nCurrentPage -=
156f2 20 28 70 4f 74 68 65 72 2d 3e 62 50 75 72 67 65   (pOther->bPurge
156f3 61 62 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62  able - pCache->b
156f4 50 75 72 67 65 61 62 6c 65 29 3b 0d 0a 20 20 20  Purgeable);..   
156f5 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20   }..  }....  /* 
156f6 53 74 65 70 20 35 2e 20 49 66 20 61 20 75 73 61  Step 5. If a usa
156f7 62 6c 65 20 70 61 67 65 20 62 75 66 66 65 72 20  ble page buffer 
156f8 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74 20 62 65  has still not be
156f9 65 6e 20 66 6f 75 6e 64 2c 20 0d 0a 20 20 2a 2a  en found, ..  **
156fa 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f   attempt to allo
156fb 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e 20  cate a new one. 
156fc 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 21 70  ..  */..  if( !p
156fd 50 61 67 65 20 29 7b 0d 0a 20 20 20 20 69 66 28  Page ){..    if(
156fe 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29   createFlag==1 )
156ff 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
15700 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20  ignMalloc();..  
15701 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65    pPage = pcache
15702 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68  1AllocPage(pCach
15703 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 63 72 65  e);..    if( cre
15704 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c  ateFlag==1 ) sql
15705 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
15706 6c 6f 63 28 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  loc();..  }.... 
15707 20 69 66 28 20 70 50 61 67 65 20 29 7b 0d 0a 20   if( pPage ){.. 
15708 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
15709 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68  h = iKey % pCach
1570a 65 2d 3e 6e 48 61 73 68 3b 0d 0a 20 20 20 20 70  e->nHash;..    p
1570b 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0d  Cache->nPage++;.
1570c 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 4b 65 79  .    pPage->iKey
1570d 20 3d 20 69 4b 65 79 3b 0d 0a 20 20 20 20 70 50   = iKey;..    pP
1570e 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61  age->pNext = pCa
1570f 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0d  che->apHash[h];.
15710 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63  .    pPage->pCac
15711 68 65 20 3d 20 70 43 61 63 68 65 3b 0d 0a 20 20  he = pCache;..  
15712 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65    pPage->pLruPre
15713 76 20 3d 20 30 3b 0d 0a 20 20 20 20 70 50 61 67  v = 0;..    pPag
15714 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b  e->pLruNext = 0;
15715 0d 0a 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a 29  ..    *(void **)
15716 70 50 61 67 65 2d 3e 70 61 67 65 2e 70 45 78 74  pPage->page.pExt
15717 72 61 20 3d 20 30 3b 0d 0a 20 20 20 20 70 43 61  ra = 0;..    pCa
15718 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d  che->apHash[h] =
15719 20 70 50 61 67 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a   pPage;..  }....
1571a 66 65 74 63 68 5f 6f 75 74 3a 0d 0a 20 20 69 66  fetch_out:..  if
1571b 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 3e  ( pPage && iKey>
1571c 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
1571d 29 7b 0d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  ){..    pCache->
1571e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0d  iMaxKey = iKey;.
1571f 0a 20 20 7d 0d 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
15720 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75 70  eaveMutex(pGroup
15721 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 26 70 50  );..  return &pP
15722 61 67 65 2d 3e 70 61 67 65 3b 0d 0a 7d 0d 0a 0d  age->page;..}...
15723 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d  .../*..** Implem
15724 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
15725 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
15726 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 0d 0a 2a  Unpin method...*
15727 2a 0d 0a 2a 2a 20 4d 61 72 6b 20 61 20 70 61 67  *..** Mark a pag
15728 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 20 28 65  e as unpinned (e
15729 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 73 79 6e  ligible for asyn
1572a 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 63 6c 69  chronous recycli
1572b 6e 67 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ng)...*/..static
1572c 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e 70   void pcache1Unp
1572d 69 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70  in(..  sqlite3_p
1572e 63 61 63 68 65 20 2a 70 2c 20 0d 0a 20 20 73 71  cache *p, ..  sq
1572f 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67  lite3_pcache_pag
15730 65 20 2a 70 50 67 2c 20 0d 0a 20 20 69 6e 74 20  e *pPg, ..  int 
15731 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 0d 0a 29  reuseUnlikely..)
15732 7b 0d 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  {..  PCache1 *pC
15733 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
15734 2a 29 70 3b 0d 0a 20 20 50 67 48 64 72 31 20 2a  *)p;..  PgHdr1 *
15735 70 50 61 67 65 20 3d 20 28 50 67 48 64 72 31 20  pPage = (PgHdr1 
15736 2a 29 70 50 67 3b 0d 0a 20 20 50 47 72 6f 75 70  *)pPg;..  PGroup
15737 20 2a 70 47 72 6f 75 70 20 3d 20 70 43 61 63 68   *pGroup = pCach
15738 65 2d 3e 70 47 72 6f 75 70 3b 0d 0a 20 0d 0a 20  e->pGroup;.. .. 
15739 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1573a 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29  pCache==pCache )
1573b 3b 0d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65  ;..  pcache1Ente
1573c 72 4d 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0d  rMutex(pGroup);.
1573d 0a 0d 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 6e  ...  /* It is an
1573e 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74   error to call t
1573f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  his function if 
15740 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65  the page is alre
15741 61 64 79 20 0d 0a 20 20 2a 2a 20 70 61 72 74 20  ady ..  ** part 
15742 6f 66 20 74 68 65 20 50 47 72 6f 75 70 20 4c 52  of the PGroup LR
15743 55 20 6c 69 73 74 2e 0d 0a 20 20 2a 2f 0d 0a 20  U list...  */.. 
15744 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15745 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26 20 70  pLruPrev==0 && p
15746 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d  Page->pLruNext==
15747 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
15748 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64  pGroup->pLruHead
15749 21 3d 70 50 61 67 65 20 26 26 20 70 47 72 6f 75  !=pPage && pGrou
1574a 70 2d 3e 70 4c 72 75 54 61 69 6c 21 3d 70 50 61  p->pLruTail!=pPa
1574b 67 65 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 72  ge );....  if( r
1574c 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20  euseUnlikely || 
1574d 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72 65 6e 74  pGroup->nCurrent
1574e 50 61 67 65 3e 70 47 72 6f 75 70 2d 3e 6e 4d 61  Page>pGroup->nMa
1574f 78 50 61 67 65 20 29 7b 0d 0a 20 20 20 20 70 63  xPage ){..    pc
15750 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
15751 61 73 68 28 70 50 61 67 65 29 3b 0d 0a 20 20 20  ash(pPage);..   
15752 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65   pcache1FreePage
15753 28 70 50 61 67 65 29 3b 0d 0a 20 20 7d 65 6c 73  (pPage);..  }els
15754 65 7b 0d 0a 20 20 20 20 2f 2a 20 41 64 64 20 74  e{..    /* Add t
15755 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 50  he page to the P
15756 47 72 6f 75 70 20 4c 52 55 20 6c 69 73 74 2e 20  Group LRU list. 
15757 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 47 72 6f  */..    if( pGro
15758 75 70 2d 3e 70 4c 72 75 48 65 61 64 20 29 7b 0d  up->pLruHead ){.
15759 0a 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 70  .      pGroup->p
1575a 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65  LruHead->pLruPre
1575b 76 20 3d 20 70 50 61 67 65 3b 0d 0a 20 20 20 20  v = pPage;..    
1575c 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78    pPage->pLruNex
1575d 74 20 3d 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75  t = pGroup->pLru
1575e 48 65 61 64 3b 0d 0a 20 20 20 20 20 20 70 47 72  Head;..      pGr
1575f 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 20 3d 20  oup->pLruHead = 
15760 70 50 61 67 65 3b 0d 0a 20 20 20 20 7d 65 6c 73  pPage;..    }els
15761 65 7b 0d 0a 20 20 20 20 20 20 70 47 72 6f 75 70  e{..      pGroup
15762 2d 3e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61  ->pLruTail = pPa
15763 67 65 3b 0d 0a 20 20 20 20 20 20 70 47 72 6f 75  ge;..      pGrou
15764 70 2d 3e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  p->pLruHead = pP
15765 61 67 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  age;..    }..   
15766 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c   pCache->nRecycl
15767 61 62 6c 65 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a  able++;..  }....
15768 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
15769 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 6f  tex(pCache->pGro
1576a 75 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  up);..}..../*..*
1576b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
1576c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
1576d 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20 6d 65  pcache.xRekey me
1576e 74 68 6f 64 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74  thod. ..*/..stat
1576f 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52  ic void pcache1R
15770 65 6b 65 79 28 0d 0a 20 20 73 71 6c 69 74 65 33  ekey(..  sqlite3
15771 5f 70 63 61 63 68 65 20 2a 70 2c 0d 0a 20 20 73  _pcache *p,..  s
15772 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61  qlite3_pcache_pa
15773 67 65 20 2a 70 50 67 2c 0d 0a 20 20 75 6e 73 69  ge *pPg,..  unsi
15774 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0d 0a  gned int iOld,..
15775 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
15776 4e 65 77 0d 0a 29 7b 0d 0a 20 20 50 43 61 63 68  New..){..  PCach
15777 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
15778 61 63 68 65 31 20 2a 29 70 3b 0d 0a 20 20 50 67  ache1 *)p;..  Pg
15779 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 28 50  Hdr1 *pPage = (P
1577a 67 48 64 72 31 20 2a 29 70 50 67 3b 0d 0a 20 20  gHdr1 *)pPg;..  
1577b 50 67 48 64 72 31 20 2a 2a 70 70 3b 0d 0a 20 20  PgHdr1 **pp;..  
1577c 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20  unsigned int h; 
1577d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
1577e 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b  e->iKey==iOld );
1577f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
15780 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68  e->pCache==pCach
15781 65 20 29 3b 0d 0a 0d 0a 20 20 70 63 61 63 68 65  e );....  pcache
15782 31 45 6e 74 65 72 4d 75 74 65 78 28 70 43 61 63  1EnterMutex(pCac
15783 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0d 0a 0d 0a  he->pGroup);....
15784 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68    h = iOld%pCach
15785 65 2d 3e 6e 48 61 73 68 3b 0d 0a 20 20 70 70 20  e->nHash;..  pp 
15786 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  = &pCache->apHas
15787 68 5b 68 5d 3b 0d 0a 20 20 77 68 69 6c 65 28 20  h[h];..  while( 
15788 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0d  (*pp)!=pPage ){.
15789 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29  .    pp = &(*pp)
1578a 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20  ->pNext;..  }.. 
1578b 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e   *pp = pPage->pN
1578c 65 78 74 3b 0d 0a 0d 0a 20 20 68 20 3d 20 69 4e  ext;....  h = iN
1578d 65 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68  ew%pCache->nHash
1578e 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 79  ;..  pPage->iKey
1578f 20 3d 20 69 4e 65 77 3b 0d 0a 20 20 70 50 61 67   = iNew;..  pPag
15790 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68  e->pNext = pCach
15791 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0d 0a 20  e->apHash[h];.. 
15792 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b   pCache->apHash[
15793 68 5d 20 3d 20 70 50 61 67 65 3b 0d 0a 20 20 69  h] = pPage;..  i
15794 66 28 20 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e  f( iNew>pCache->
15795 69 4d 61 78 4b 65 79 20 29 7b 0d 0a 20 20 20 20  iMaxKey ){..    
15796 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
15797 3d 20 69 4e 65 77 3b 0d 0a 20 20 7d 0d 0a 0d 0a  = iNew;..  }....
15798 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
15799 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 6f  tex(pCache->pGro
1579a 75 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  up);..}..../*..*
1579b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
1579c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
1579d 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65  pcache.xTruncate
1579e 20 6d 65 74 68 6f 64 2e 20 0d 0a 2a 2a 0d 0a 2a   method. ..**..*
1579f 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 6e  * Discard all un
157a0 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e 20  pinned pages in 
157a1 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 61  the cache with a
157a2 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 75   page number equ
157a3 61 6c 20 74 6f 0d 0a 2a 2a 20 6f 72 20 67 72 65  al to..** or gre
157a4 61 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65  ater than parame
157a5 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20  ter iLimit. Any 
157a6 70 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74  pinned pages wit
157a7 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0d  h a page number.
157a8 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  .** equal to or 
157a9 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69  greater than iLi
157aa 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74  mit are implicit
157ab 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0d 0a 2a 2f  ly unpinned...*/
157ac 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  ..static void pc
157ad 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71  ache1Truncate(sq
157ae 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c  lite3_pcache *p,
157af 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
157b0 69 6d 69 74 29 7b 0d 0a 20 20 50 43 61 63 68 65  imit){..  PCache
157b1 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61  1 *pCache = (PCa
157b2 63 68 65 31 20 2a 29 70 3b 0d 0a 20 20 70 63 61  che1 *)p;..  pca
157b3 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 70  che1EnterMutex(p
157b4 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0d  Cache->pGroup);.
157b5 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70  .  if( iLimit<=p
157b6 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29  Cache->iMaxKey )
157b7 7b 0d 0a 20 20 20 20 70 63 61 63 68 65 31 54 72  {..    pcache1Tr
157b8 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61  uncateUnsafe(pCa
157b9 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0d 0a 20  che, iLimit);.. 
157ba 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b     pCache->iMaxK
157bb 65 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0d 0a  ey = iLimit-1;..
157bc 20 20 7d 0d 0a 20 20 70 63 61 63 68 65 31 4c 65    }..  pcache1Le
157bd 61 76 65 4d 75 74 65 78 28 70 43 61 63 68 65 2d  aveMutex(pCache-
157be 3e 70 47 72 6f 75 70 29 3b 0d 0a 7d 0d 0a 0d 0a  >pGroup);..}....
157bf 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  /*..** Implement
157c0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
157c1 69 74 65 33 5f 70 63 61 63 68 65 2e 78 44 65 73  ite3_pcache.xDes
157c2 74 72 6f 79 20 6d 65 74 68 6f 64 2e 20 0d 0a 2a  troy method. ..*
157c3 2a 0d 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20  *..** Destroy a 
157c4 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  cache allocated 
157c5 75 73 69 6e 67 20 70 63 61 63 68 65 31 43 72 65  using pcache1Cre
157c6 61 74 65 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ate()...*/..stat
157c7 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 44  ic void pcache1D
157c8 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 70  estroy(sqlite3_p
157c9 63 61 63 68 65 20 2a 70 29 7b 0d 0a 20 20 50 43  cache *p){..  PC
157ca 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
157cb 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0d 0a 20  (PCache1 *)p;.. 
157cc 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 20   PGroup *pGroup 
157cd 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70  = pCache->pGroup
157ce 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ;..  assert( pCa
157cf 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
157d0 7c 7c 20 28 70 43 61 63 68 65 2d 3e 6e 4d 61 78  || (pCache->nMax
157d1 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 6e  ==0 && pCache->n
157d2 4d 69 6e 3d 3d 30 29 20 29 3b 0d 0a 20 20 70 63  Min==0) );..  pc
157d3 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
157d4 70 47 72 6f 75 70 29 3b 0d 0a 20 20 70 63 61 63  pGroup);..  pcac
157d5 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66  he1TruncateUnsaf
157d6 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0d 0a 20  e(pCache, 0);.. 
157d7 20 61 73 73 65 72 74 28 20 70 47 72 6f 75 70 2d   assert( pGroup-
157d8 3e 6e 4d 61 78 50 61 67 65 20 3e 3d 20 70 43 61  >nMaxPage >= pCa
157d9 63 68 65 2d 3e 6e 4d 61 78 20 29 3b 0d 0a 20 20  che->nMax );..  
157da 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65  pGroup->nMaxPage
157db 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78   -= pCache->nMax
157dc 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 47 72  ;..  assert( pGr
157dd 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 20 3e 3d  oup->nMinPage >=
157de 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 20 29 3b   pCache->nMin );
157df 0d 0a 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e  ..  pGroup->nMin
157e0 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e  Page -= pCache->
157e1 6e 4d 69 6e 3b 0d 0a 20 20 70 47 72 6f 75 70 2d  nMin;..  pGroup-
157e2 3e 6d 78 50 69 6e 6e 65 64 20 3d 20 70 47 72 6f  >mxPinned = pGro
157e3 75 70 2d 3e 6e 4d 61 78 50 61 67 65 20 2b 20 31  up->nMaxPage + 1
157e4 30 20 2d 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e  0 - pGroup->nMin
157e5 50 61 67 65 3b 0d 0a 20 20 70 63 61 63 68 65 31  Page;..  pcache1
157e6 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 70  EnforceMaxPage(p
157e7 47 72 6f 75 70 29 3b 0d 0a 20 20 70 63 61 63 68  Group);..  pcach
157e8 65 31 4c 65 61 76 65 4d 75 74 65 78 28 70 47 72  e1LeaveMutex(pGr
157e9 6f 75 70 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  oup);..  sqlite3
157ea 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70  _free(pCache->ap
157eb 48 61 73 68 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Hash);..  sqlite
157ec 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0d  3_free(pCache);.
157ed 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
157ee 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
157ef 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74  lled during init
157f0 69 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69  ialization (sqli
157f1 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
157f2 29 20 74 6f 0d 0a 2a 2a 20 69 6e 73 74 61 6c 6c  ) to..** install
157f3 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 6c 75   the default plu
157f4 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f 64  ggable cache mod
157f5 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ule, assuming th
157f6 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 0d 0a  e user has not..
157f7 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f 76 69  ** already provi
157f8 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ded an alternati
157f9 76 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ve...*/..SQLITE_
157fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
157fb 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
157fc 61 75 6c 74 28 76 6f 69 64 29 7b 0d 0a 20 20 73  ault(void){..  s
157fd 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
157fe 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
157ff 64 73 32 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ds2 defaultMetho
15800 64 73 20 3d 20 7b 0d 0a 20 20 20 20 31 2c 20 20  ds = {..    1,  
15801 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15802 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
15803 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20   */..    0,     
15804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15805 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0d 0a 20 20    /* pArg */..  
15806 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20    pcache1Init,  
15807 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49             /* xI
15808 6e 69 74 20 2a 2f 0d 0a 20 20 20 20 70 63 61 63  nit */..    pcac
15809 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20  he1Shutdown,    
1580a 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77       /* xShutdow
1580b 6e 20 2a 2f 0d 0a 20 20 20 20 70 63 61 63 68 65  n */..    pcache
1580c 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20  1Create,        
1580d 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f     /* xCreate */
1580e 0d 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63  ..    pcache1Cac
1580f 68 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f  hesize,        /
15810 2a 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0d  * xCachesize */.
15811 0a 20 20 20 20 70 63 61 63 68 65 31 50 61 67 65  .    pcache1Page
15812 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a  count,        /*
15813 20 78 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0d 0a   xPagecount */..
15814 20 20 20 20 70 63 61 63 68 65 31 46 65 74 63 68      pcache1Fetch
15815 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
15816 78 46 65 74 63 68 20 2a 2f 0d 0a 20 20 20 20 70  xFetch */..    p
15817 63 61 63 68 65 31 55 6e 70 69 6e 2c 20 20 20 20  cache1Unpin,    
15818 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 70 69          /* xUnpi
15819 6e 20 2a 2f 0d 0a 20 20 20 20 70 63 61 63 68 65  n */..    pcache
1581a 31 52 65 6b 65 79 2c 20 20 20 20 20 20 20 20 20  1Rekey,         
1581b 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 2a 2f 0d     /* xRekey */.
1581c 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e  .    pcache1Trun
1581d 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  cate,         /*
1581e 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0d 0a 20   xTruncate */.. 
1581f 20 20 20 70 63 61 63 68 65 31 44 65 73 74 72 6f     pcache1Destro
15820 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  y,          /* x
15821 44 65 73 74 72 6f 79 20 2a 2f 0d 0a 20 20 20 20  Destroy */..    
15822 70 63 61 63 68 65 31 53 68 72 69 6e 6b 20 20 20  pcache1Shrink   
15823 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 72           /* xShr
15824 69 6e 6b 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 20 20  ink */..  };..  
15825 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
15826 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
15827 43 48 45 32 2c 20 26 64 65 66 61 75 6c 74 4d 65  CHE2, &defaultMe
15828 74 68 6f 64 73 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69  thods);..}....#i
15829 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
1582a 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
1582b 4d 45 4e 54 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  MENT../*..** Thi
1582c 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1582d 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70  lled to free sup
1582e 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63  erfluous dynamic
1582f 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d  ally allocated m
15830 65 6d 6f 72 79 0d 0a 2a 2a 20 68 65 6c 64 20 62  emory..** held b
15831 79 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74  y the pager syst
15832 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73  em. Memory in us
15833 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20  e by any SQLite 
15834 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0d  pager allocated.
15835 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 65  .** by the curre
15836 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 65  nt thread may be
15837 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 65   sqlite3_free()e
15838 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 6e 52 65 71 20  d...**..** nReq 
15839 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1583a 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
1583b 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20   required. Once 
1583c 74 68 69 73 20 6d 75 63 68 20 68 61 73 0d 0a 2a  this much has..*
1583d 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c  * been released,
1583e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
1583f 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72  turns. The retur
15840 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74  n value is the t
15841 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0d 0a 2a 2a  otal number ..**
15842 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
15843 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0d 0a 2a  ory released...*
15844 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
15845 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
15846 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  cheReleaseMemory
15847 28 69 6e 74 20 6e 52 65 71 29 7b 0d 0a 20 20 69  (int nReq){..  i
15848 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0d 0a 20  nt nFree = 0;.. 
15849 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1584a 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70  _mutex_notheld(p
1584b 63 61 63 68 65 31 2e 67 72 70 2e 6d 75 74 65 78  cache1.grp.mutex
1584c 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1584d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
1584e 74 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75  theld(pcache1.mu
1584f 74 65 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 70  tex) );..  if( p
15850 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30  cache1.pStart==0
15851 20 29 7b 0d 0a 20 20 20 20 50 67 48 64 72 31 20   ){..    PgHdr1 
15852 2a 70 3b 0d 0a 20 20 20 20 70 63 61 63 68 65 31  *p;..    pcache1
15853 45 6e 74 65 72 4d 75 74 65 78 28 26 70 63 61 63  EnterMutex(&pcac
15854 68 65 31 2e 67 72 70 29 3b 0d 0a 20 20 20 20 77  he1.grp);..    w
15855 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c  hile( (nReq<0 ||
15856 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20   nFree<nReq) && 
15857 28 28 70 3d 70 63 61 63 68 65 31 2e 67 72 70 2e  ((p=pcache1.grp.
15858 70 4c 72 75 54 61 69 6c 29 21 3d 30 29 20 29 7b  pLruTail)!=0) ){
15859 0d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d  ..      nFree +=
1585a 20 70 63 61 63 68 65 31 4d 65 6d 53 69 7a 65 28   pcache1MemSize(
1585b 70 2d 3e 70 61 67 65 2e 70 42 75 66 29 3b 0d 0a  p->page.pBuf);..
1585c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 43  #ifdef SQLITE_PC
1585d 41 43 48 45 5f 53 45 50 41 52 41 54 45 5f 48 45  ACHE_SEPARATE_HE
1585e 41 44 45 52 0d 0a 20 20 20 20 20 20 6e 46 72 65  ADER..      nFre
1585f 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 65 6d 53  e += sqlite3MemS
15860 69 7a 65 28 70 29 3b 0d 0a 23 65 6e 64 69 66 0d  ize(p);..#endif.
15861 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69  .      pcache1Pi
15862 6e 50 61 67 65 28 70 29 3b 0d 0a 20 20 20 20 20  nPage(p);..     
15863 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72   pcache1RemoveFr
15864 6f 6d 48 61 73 68 28 70 29 3b 0d 0a 20 20 20 20  omHash(p);..    
15865 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67    pcache1FreePag
15866 65 28 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  e(p);..    }..  
15867 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
15868 74 65 78 28 26 70 63 61 63 68 65 31 2e 67 72 70  tex(&pcache1.grp
15869 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  );..  }..  retur
1586a 6e 20 6e 46 72 65 65 3b 0d 0a 7d 0d 0a 23 65 6e  n nFree;..}..#en
1586b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
1586c 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
1586d 47 45 4d 45 4e 54 20 2a 2f 0d 0a 0d 0a 23 69 66  GEMENT */....#if
1586e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d  def SQLITE_TEST.
1586f 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
15870 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79  ction is used by
15871 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
15872 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20   to inspect the 
15873 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0d 0a  internal state..
15874 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  ** of the global
15875 20 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c   cache...*/..SQL
15876 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
15877 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 74   sqlite3PcacheSt
15878 61 74 73 28 0d 0a 20 20 69 6e 74 20 2a 70 6e 43  ats(..  int *pnC
15879 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20  urrent,      /* 
1587a 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65  OUT: Total numbe
1587b 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65  r of pages cache
1587c 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 4d  d */..  int *pnM
1587d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ax,          /* 
1587e 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69  OUT: Global maxi
1587f 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a  mum cache size *
15880 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c  /..  int *pnMin,
15881 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
15882 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31  : Sum of PCache1
15883 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61  .nMin for purgea
15884 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0d 0a 20  ble caches */.. 
15885 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62   int *pnRecyclab
15886 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  le    /* OUT: To
15887 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
15888 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  ges available fo
15889 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0d 0a  r recycling */..
1588a 29 7b 0d 0a 20 20 50 67 48 64 72 31 20 2a 70 3b  ){..  PgHdr1 *p;
1588b 0d 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61  ..  int nRecycla
1588c 62 6c 65 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28  ble = 0;..  for(
1588d 70 3d 70 63 61 63 68 65 31 2e 67 72 70 2e 70 4c  p=pcache1.grp.pL
1588e 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e  ruHead; p; p=p->
1588f 70 4c 72 75 4e 65 78 74 29 7b 0d 0a 20 20 20 20  pLruNext){..    
15890 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0d 0a  nRecyclable++;..
15891 20 20 7d 0d 0a 20 20 2a 70 6e 43 75 72 72 65 6e    }..  *pnCurren
15892 74 20 3d 20 70 63 61 63 68 65 31 2e 67 72 70 2e  t = pcache1.grp.
15893 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0d 0a 20  nCurrentPage;.. 
15894 20 2a 70 6e 4d 61 78 20 3d 20 28 69 6e 74 29 70   *pnMax = (int)p
15895 63 61 63 68 65 31 2e 67 72 70 2e 6e 4d 61 78 50  cache1.grp.nMaxP
15896 61 67 65 3b 0d 0a 20 20 2a 70 6e 4d 69 6e 20 3d  age;..  *pnMin =
15897 20 28 69 6e 74 29 70 63 61 63 68 65 31 2e 67 72   (int)pcache1.gr
15898 70 2e 6e 4d 69 6e 50 61 67 65 3b 0d 0a 20 20 2a  p.nMinPage;..  *
15899 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 6e  pnRecyclable = n
1589a 52 65 63 79 63 6c 61 62 6c 65 3b 0d 0a 7d 0d 0a  Recyclable;..}..
1589b 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  #endif..../*****
1589c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1589d 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a   pcache1.c *****
1589e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1589f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
158a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
158a2 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a   file rowset.c *
158a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
158a6 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 72  ** 2008 December
158a7 20 33 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61   3..**..** The a
158a8 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
158a9 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
158aa 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
158ab 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
158ac 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
158ad 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
158ae 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
158af 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
158b0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
158b1 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
158b2 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
158b3 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
158b4 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
158b5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
158b6 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
158b7 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
158b8 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
158b9 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
158ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a  **********..**..
158be 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69  ** This module i
158bf 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a  mplements an obj
158c0 65 63 74 20 77 65 20 63 61 6c 6c 20 61 20 22 52  ect we call a "R
158c1 6f 77 53 65 74 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  owSet"...**..** 
158c2 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  The RowSet objec
158c3 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74 69 6f  t is a collectio
158c4 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20 52 6f  n of rowids.  Ro
158c5 77 69 64 73 0d 0a 2a 2a 20 61 72 65 20 69 6e 73  wids..** are ins
158c6 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52  erted into the R
158c7 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62 69  owSet in an arbi
158c8 74 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49 6e  trary order.  In
158c9 73 65 72 74 73 0d 0a 2a 2a 20 63 61 6e 20 62 65  serts..** can be
158ca 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68   intermixed with
158cb 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 69 66   tests to see if
158cc 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68   a given rowid h
158cd 61 73 20 62 65 65 6e 0d 0a 2a 2a 20 70 72 65 76  as been..** prev
158ce 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20  iously inserted 
158cf 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e  into the RowSet.
158d0 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 61  ..**..** After a
158d1 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66  ll inserts are f
158d2 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70  inished, it is p
158d3 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61  ossible to extra
158d4 63 74 20 74 68 65 0d 0a 2a 2a 20 65 6c 65 6d 65  ct the..** eleme
158d5 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65  nts of the RowSe
158d6 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  t in sorted orde
158d7 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78  r.  Once this ex
158d8 74 72 61 63 74 69 6f 6e 0d 0a 2a 2a 20 70 72 6f  traction..** pro
158d9 63 65 73 73 20 68 61 73 20 73 74 61 72 74 65 64  cess has started
158da 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74  , no new element
158db 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65  s may be inserte
158dc 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 48 65 6e 63 65  d...**..** Hence
158dd 2c 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  , the primitive 
158de 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61  operations for a
158df 20 52 6f 77 53 65 74 20 61 72 65 3a 0d 0a 2a 2a   RowSet are:..**
158e0 0d 0a 2a 2a 20 20 20 20 43 52 45 41 54 45 0d 0a  ..**    CREATE..
158e1 2a 2a 20 20 20 20 49 4e 53 45 52 54 0d 0a 2a 2a  **    INSERT..**
158e2 20 20 20 20 54 45 53 54 0d 0a 2a 2a 20 20 20 20      TEST..**    
158e3 53 4d 41 4c 4c 45 53 54 0d 0a 2a 2a 20 20 20 20  SMALLEST..**    
158e4 44 45 53 54 52 4f 59 0d 0a 2a 2a 0d 0a 2a 2a 20  DESTROY..**..** 
158e5 54 68 65 20 43 52 45 41 54 45 20 61 6e 64 20 44  The CREATE and D
158e6 45 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76 65  ESTROY primitive
158e7 73 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74 72  s are the constr
158e8 75 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72 75  uctor and destru
158e9 63 74 6f 72 2c 0d 0a 2a 2a 20 6f 62 76 69 6f 75  ctor,..** obviou
158ea 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54  sly.  The INSERT
158eb 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 73 20   primitive adds 
158ec 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
158ed 20 74 68 65 20 52 6f 77 53 65 74 2e 0d 0a 2a 2a   the RowSet...**
158ee 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f 20   TEST checks to 
158ef 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e  see if an elemen
158f0 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  t is already in 
158f1 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d 41  the RowSet.  SMA
158f2 4c 4c 45 53 54 0d 0a 2a 2a 20 65 78 74 72 61 63  LLEST..** extrac
158f3 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c  ts the least val
158f4 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53  ue from the RowS
158f5 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  et...**..** The 
158f6 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65  INSERT primitive
158f7 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20   might allocate 
158f8 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72  additional memor
158f9 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0d 0a 2a  y.  Memory is..*
158fa 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63  * allocated in c
158fb 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e  hunks so most IN
158fc 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f  SERTs do no allo
158fd 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69  cation.  There i
158fe 73 20 61 6e 20 0d 0a 2a 2a 20 75 70 70 65 72 20  s an ..** upper 
158ff 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a  bound on the siz
15900 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  e of allocated m
15901 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72  emory.  No memor
15902 79 20 69 73 20 66 72 65 65 64 0d 0a 2a 2a 20 75  y is freed..** u
15903 6e 74 69 6c 20 44 45 53 54 52 4f 59 2e 0d 0a 2a  ntil DESTROY...*
15904 2a 0d 0a 2a 2a 20 54 68 65 20 54 45 53 54 20 70  *..** The TEST p
15905 72 69 6d 69 74 69 76 65 20 69 6e 63 6c 75 64 65  rimitive include
15906 73 20 61 20 22 62 61 74 63 68 22 20 6e 75 6d 62  s a "batch" numb
15907 65 72 2e 20 20 54 68 65 20 54 45 53 54 20 70 72  er.  The TEST pr
15908 69 6d 69 74 69 76 65 0d 0a 2a 2a 20 77 69 6c 6c  imitive..** will
15909 20 6f 6e 6c 79 20 73 65 65 20 65 6c 65 6d 65 6e   only see elemen
1590a 74 73 20 74 68 61 74 20 77 65 72 65 20 69 6e 73  ts that were ins
1590b 65 72 74 65 64 20 62 65 66 6f 72 65 20 74 68 65  erted before the
1590c 20 6c 61 73 74 20 63 68 61 6e 67 65 0d 0a 2a 2a   last change..**
1590d 20 69 6e 20 74 68 65 20 62 61 74 63 68 20 6e 75   in the batch nu
1590e 6d 62 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20  mber.  In other 
1590f 77 6f 72 64 73 2c 20 69 66 20 61 6e 20 49 4e 53  words, if an INS
15910 45 52 54 20 6f 63 63 75 72 73 20 62 65 74 77 65  ERT occurs betwe
15911 65 6e 0d 0a 2a 2a 20 74 77 6f 20 54 45 53 54 73  en..** two TESTs
15912 20 77 68 65 72 65 20 74 68 65 20 54 45 53 54 73   where the TESTs
15913 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 62   have the same b
15914 61 74 63 68 20 6e 75 62 6d 65 72 2c 20 74 68 65  atch nubmer, the
15915 6e 20 74 68 65 0d 0a 2a 2a 20 76 61 6c 75 65 20  n the..** value 
15916 61 64 64 65 64 20 62 79 20 74 68 65 20 49 4e 53  added by the INS
15917 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  ERT will not be 
15918 76 69 73 69 62 6c 65 20 74 6f 20 74 68 65 20 73  visible to the s
15919 65 63 6f 6e 64 20 54 45 53 54 2e 0d 0a 2a 2a 20  econd TEST...** 
1591a 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61 74 63  The initial batc
1591b 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65 72 6f  h number is zero
1591c 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65 72 79  , so if the very
1591d 20 66 69 72 73 74 20 54 45 53 54 20 63 6f 6e 74   first TEST cont
1591e 61 69 6e 73 0d 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a  ains..** a non-z
1591f 65 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65 72  ero batch number
15920 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61 6c  , it will see al
15921 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73 2e  l prior INSERTs.
15922 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 20 49 4e 53 45  ..**..** No INSE
15923 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 61  RTs may occurs a
15924 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e  fter a SMALLEST.
15925 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 77    An assertion w
15926 69 6c 6c 20 66 61 69 6c 20 69 66 0d 0a 2a 2a 20  ill fail if..** 
15927 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 65  that is attempte
15928 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63  d...**..** The c
15929 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54  ost of an INSERT
1592a 20 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73   is roughly cons
1592b 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65  tant.  (Sometime
1592c 20 6e 65 77 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20   new memory..** 
1592d 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61  has to be alloca
1592e 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54  ted on an INSERT
1592f 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20  .)  The cost of 
15930 61 20 54 45 53 54 20 77 69 74 68 20 61 20 6e 65  a TEST with a ne
15931 77 0d 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62  w..** batch numb
15932 65 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77  er is O(NlogN) w
15933 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
15934 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
15935 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 0d   in the RowSet..
15936 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20  .** The cost of 
15937 61 20 54 45 53 54 20 75 73 69 6e 67 20 74 68 65  a TEST using the
15938 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62   same batch numb
15939 65 72 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20  er is O(logN).  
1593a 54 68 65 20 63 6f 73 74 0d 0a 2a 2a 20 6f 66 20  The cost..** of 
1593b 74 68 65 20 66 69 72 73 74 20 53 4d 41 4c 4c 45  the first SMALLE
1593c 53 54 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20  ST is O(NlogN). 
1593d 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73   Second and subs
1593e 65 71 75 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0d  equent SMALLEST.
1593f 0a 2a 2a 20 70 72 69 6d 69 74 69 76 65 73 20 61  .** primitives a
15940 72 65 20 63 6f 6e 73 74 61 6e 74 20 74 69 6d 65  re constant time
15941 2e 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 44  .  The cost of D
15942 45 53 54 52 4f 59 20 69 73 20 4f 28 4e 29 2e 0d  ESTROY is O(N)..
15943 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 69 73  .**..** There is
15944 20 61 6e 20 61 64 64 65 64 20 63 6f 73 74 20 6f   an added cost o
15945 66 20 4f 28 4e 29 20 77 68 65 6e 20 73 77 69 74  f O(N) when swit
15946 63 68 69 6e 67 20 62 65 74 77 65 65 6e 20 54 45  ching between TE
15947 53 54 20 61 6e 64 0d 0a 2a 2a 20 53 4d 41 4c 4c  ST and..** SMALL
15948 45 53 54 20 70 72 69 6d 69 74 69 76 65 73 2e 0d  EST primitives..
15949 0a 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  .*/....../*..** 
1594a 54 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20  Target size for 
1594b 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b  allocation chunk
1594c 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  s...*/..#define 
1594d 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f  ROWSET_ALLOCATIO
1594e 4e 5f 53 49 5a 45 20 31 30 32 34 0d 0a 0d 0a 2f  N_SIZE 1024..../
1594f 2a 0d 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  *..** The number
15950 20 6f 66 20 72 6f 77 73 65 74 20 65 6e 74 72 69   of rowset entri
15951 65 73 20 70 65 72 20 61 6c 6c 6f 63 61 74 69 6f  es per allocatio
15952 6e 20 63 68 75 6e 6b 2e 0d 0a 2a 2f 0d 0a 23 64  n chunk...*/..#d
15953 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 45 4e 54  efine ROWSET_ENT
15954 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 20 5c 0d  RY_PER_CHUNK  \.
15955 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15956 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 45 54          ((ROWSET
15957 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45  _ALLOCATION_SIZE
15958 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  -8)/sizeof(struc
15959 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 0d  t RowSetEntry)).
1595a 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 65  .../*..** Each e
1595b 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74  ntry in a RowSet
1595c 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
1595d 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1595e 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74   object...*/..st
1595f 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
15960 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 0d 0a   {            ..
15961 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20    i64 v;        
15962 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15963 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66  /* ROWID value f
15964 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f  or this entry */
15965 0d 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  ..  struct RowSe
15966 74 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20  tEntry *pRight; 
15967 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72    /* Right subtr
15968 65 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69  ee (larger entri
15969 65 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0d 0a  es) or list */..
1596a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1596b 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20  ntry *pLeft;    
1596c 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20  /* Left subtree 
1596d 28 73 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73  (smaller entries
1596e 29 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  ) */..};..../*..
1596f 2a 2a 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f  ** RowSetEntry o
15970 62 6a 65 63 74 73 20 61 72 65 20 61 6c 6c 6f 63  bjects are alloc
15971 61 74 65 64 20 69 6e 20 6c 61 72 67 65 20 63 68  ated in large ch
15972 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 65 73 20  unks (instances 
15973 6f 66 20 74 68 65 0d 0a 2a 2a 20 66 6f 6c 6c 6f  of the..** follo
15974 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20  wing structure) 
15975 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79  to reduce memory
15976 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72   allocation over
15977 68 65 61 64 2e 20 20 54 68 65 0d 0a 2a 2a 20 63  head.  The..** c
15978 68 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 6f  hunks are kept o
15979 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
1597a 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e  so that they can
1597b 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 0d   be deallocated.
1597c 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77  .** when the Row
1597d 53 65 74 20 69 73 20 64 65 73 74 72 6f 79 65 64  Set is destroyed
1597e 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 52 6f  ...*/..struct Ro
1597f 77 53 65 74 43 68 75 6e 6b 20 7b 0d 0a 20 20 73  wSetChunk {..  s
15980 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e  truct RowSetChun
15981 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20  k *pNextChunk;  
15982 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
15983 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74  unk on list of t
15984 68 65 6d 20 61 6c 6c 20 2a 2f 0d 0a 20 20 73 74  hem all */..  st
15985 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
15986 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45   aEntry[ROWSET_E
15987 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b  NTRY_PER_CHUNK];
15988 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e   /* Allocated en
15989 74 72 69 65 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  tries */..};....
1598a 2f 2a 0d 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20  /*..** A RowSet 
1598b 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  in an instance o
1598c 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1598d 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a  structure...**..
1598e 2a 2a 20 41 20 74 79 70 65 64 65 66 20 6f 66 20  ** A typedef of 
1598f 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
15990 66 20 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74  f found in sqlit
15991 65 49 6e 74 2e 68 2e 0d 0a 2a 2f 0d 0a 73 74 72  eInt.h...*/..str
15992 75 63 74 20 52 6f 77 53 65 74 20 7b 0d 0a 20 20  uct RowSet {..  
15993 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75  struct RowSetChu
15994 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 2f  nk *pChunk;    /
15995 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63 68  * List of all ch
15996 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  unk allocations 
15997 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */..  sqlite3 *d
15998 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
15999 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1599a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1599b 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 52 6f 77  */..  struct Row
1599c 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79  SetEntry *pEntry
1599d 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  ;    /* List of 
1599e 65 6e 74 72 69 65 73 20 75 73 69 6e 67 20 70 52  entries using pR
1599f 69 67 68 74 20 2a 2f 0d 0a 20 20 73 74 72 75 63  ight */..  struc
159a0 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
159a1 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73  Last;     /* Las
159a2 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70  t entry on the p
159a3 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0d 0a 20  Entry list */.. 
159a4 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
159a5 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 20 20  try *pFresh;    
159a6 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 77  /* Source of new
159a7 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 2a   entry objects *
159a8 2f 0d 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  /..  struct RowS
159a9 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b 20  etEntry *pTree; 
159aa 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 72      /* Binary tr
159ab 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a 2f  ee of entries */
159ac 0d 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20  ..  u16 nFresh; 
159ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
159ae 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
159af 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73  objects on pFres
159b0 68 20 2a 2f 0d 0a 20 20 75 38 20 69 73 53 6f 72  h */..  u8 isSor
159b1 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ted;            
159b2 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
159b3 66 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74  f pEntry is sort
159b4 65 64 20 2a 2f 0d 0a 20 20 75 38 20 69 42 61 74  ed */..  u8 iBat
159b5 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
159b6 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
159b7 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 68 20  nt insert batch 
159b8 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
159b9 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72   Turn bulk memor
159ba 79 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20  y into a RowSet 
159bb 6f 62 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73  object.  N bytes
159bc 20 6f 66 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 61   of memory..** a
159bd 72 65 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20  re available at 
159be 70 53 70 61 63 65 2e 20 20 54 68 65 20 64 62 20  pSpace.  The db 
159bf 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20  pointer is used 
159c0 61 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74  as a memory cont
159c1 65 78 74 0d 0a 2a 2a 20 66 6f 72 20 61 6e 79 20  ext..** for any 
159c2 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63  subsequent alloc
159c3 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64  ations that need
159c4 20 74 6f 20 6f 63 63 75 72 2e 0d 0a 2a 2a 20 52   to occur...** R
159c5 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
159c6 74 6f 20 74 68 65 20 6e 65 77 20 52 6f 77 53 65  to the new RowSe
159c7 74 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a  t object...**..*
159c8 2a 20 49 74 20 6d 75 73 74 20 62 65 20 74 68 65  * It must be the
159c9 20 63 61 73 65 20 74 68 61 74 20 4e 20 69 73 20   case that N is 
159ca 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 6d 61  sufficient to ma
159cb 6b 65 20 61 20 52 6f 77 73 65 74 2e 20 20 49 66  ke a Rowset.  If
159cc 20 6e 6f 74 0d 0a 2a 2a 20 61 6e 20 61 73 73 65   not..** an asse
159cd 72 74 69 6f 6e 20 66 61 75 6c 74 20 6f 63 63 75  rtion fault occu
159ce 72 73 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49 66 20  rs...** ..** If 
159cf 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  N is larger than
159d0 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 73   the minimum, us
159d1 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 61 73  e the surplus as
159d2 20 61 6e 20 69 6e 69 74 69 61 6c 0d 0a 2a 2a 20   an initial..** 
159d3 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 6e  allocation of en
159d4 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 20  tries available 
159d5 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0d 0a 2a  to be filled...*
159d6 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
159d7 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65  E RowSet *sqlite
159d8 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69  3RowSetInit(sqli
159d9 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
159da 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20  Space, unsigned 
159db 69 6e 74 20 4e 29 7b 0d 0a 20 20 52 6f 77 53 65  int N){..  RowSe
159dc 74 20 2a 70 3b 0d 0a 20 20 61 73 73 65 72 74 28  t *p;..  assert(
159dd 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a   N >= ROUND8(siz
159de 65 6f 66 28 2a 70 29 29 20 29 3b 0d 0a 20 20 70  eof(*p)) );..  p
159df 20 3d 20 70 53 70 61 63 65 3b 0d 0a 20 20 70 2d   = pSpace;..  p-
159e0 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0d 0a 20 20  >pChunk = 0;..  
159e1 70 2d 3e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 70  p->db = db;..  p
159e2 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0d 0a 20  ->pEntry = 0;.. 
159e3 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0d 0a   p->pLast = 0;..
159e4 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0d    p->pTree = 0;.
159e5 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 28  .  p->pFresh = (
159e6 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
159e7 72 79 2a 29 28 52 4f 55 4e 44 38 28 73 69 7a 65  ry*)(ROUND8(size
159e8 6f 66 28 2a 70 29 29 20 2b 20 28 63 68 61 72 2a  of(*p)) + (char*
159e9 29 70 29 3b 0d 0a 20 20 70 2d 3e 6e 46 72 65 73  )p);..  p->nFres
159ea 68 20 3d 20 28 75 31 36 29 28 28 4e 20 2d 20 52  h = (u16)((N - R
159eb 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29  OUND8(sizeof(*p)
159ec 29 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74  ))/sizeof(struct
159ed 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0d   RowSetEntry));.
159ee 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d  .  p->isSorted =
159ef 20 31 3b 0d 0a 20 20 70 2d 3e 69 42 61 74 63 68   1;..  p->iBatch
159f0 20 3d 20 30 3b 0d 0a 20 20 72 65 74 75 72 6e 20   = 0;..  return 
159f1 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
159f2 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63  Deallocate all c
159f3 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77  hunks from a Row
159f4 53 65 74 2e 20 20 54 68 69 73 20 66 72 65 65 73  Set.  This frees
159f5 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74   all memory that
159f6 0d 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 74 20  ..** the RowSet 
159f7 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76  has allocated ov
159f8 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d 65 2e  er its lifetime.
159f9 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
159fa 73 0d 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75  s..** the destru
159fb 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f 77  ctor for the Row
159fc 53 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  Set...*/..SQLITE
159fd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
159fe 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72  lite3RowSetClear
159ff 28 52 6f 77 53 65 74 20 2a 70 29 7b 0d 0a 20 20  (RowSet *p){..  
15a00 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75  struct RowSetChu
15a01 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65  nk *pChunk, *pNe
15a02 78 74 43 68 75 6e 6b 3b 0d 0a 20 20 66 6f 72 28  xtChunk;..  for(
15a03 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b  pChunk=p->pChunk
15a04 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b  ; pChunk; pChunk
15a05 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0d   = pNextChunk){.
15a06 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20  .    pNextChunk 
15a07 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43  = pChunk->pNextC
15a08 68 75 6e 6b 3b 0d 0a 20 20 20 20 73 71 6c 69 74  hunk;..    sqlit
15a09 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20  e3DbFree(p->db, 
15a0a 70 43 68 75 6e 6b 29 3b 0d 0a 20 20 7d 0d 0a 20  pChunk);..  }.. 
15a0b 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0d   p->pChunk = 0;.
15a0c 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 30  .  p->nFresh = 0
15a0d 3b 0d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d  ;..  p->pEntry =
15a0e 20 30 3b 0d 0a 20 20 70 2d 3e 70 4c 61 73 74 20   0;..  p->pLast 
15a0f 3d 20 30 3b 0d 0a 20 20 70 2d 3e 70 54 72 65 65  = 0;..  p->pTree
15a10 20 3d 20 30 3b 0d 0a 20 20 70 2d 3e 69 73 53 6f   = 0;..  p->isSo
15a11 72 74 65 64 20 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a  rted = 1;..}....
15a12 2f 2a 0d 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  /*..** Insert a 
15a13 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61  new value into a
15a14 20 52 6f 77 53 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a   RowSet...**..**
15a15 20 54 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   The mallocFaile
15a16 64 20 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61  d flag of the da
15a17 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15a18 6e 20 69 73 20 73 65 74 20 69 66 20 61 0d 0a 2a  n is set if a..*
15a19 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
15a1a 69 6f 6e 20 66 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a  ion fails...*/..
15a1b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
15a1c 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65  oid sqlite3RowSe
15a1d 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 20 2a  tInsert(RowSet *
15a1e 70 2c 20 69 36 34 20 72 6f 77 69 64 29 7b 0d 0a  p, i64 rowid){..
15a1f 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
15a20 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 2f  ntry *pEntry;  /
15a21 2a 20 54 68 65 20 6e 65 77 20 65 6e 74 72 79 20  * The new entry 
15a22 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 52 6f 77  */..  struct Row
15a23 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b  SetEntry *pLast;
15a24 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 70     /* The last p
15a25 72 69 6f 72 20 65 6e 74 72 79 20 2a 2f 0d 0a 20  rior entry */.. 
15a26 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
15a27 0d 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 65 73  ..  if( p->nFres
15a28 68 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 74 72  h==0 ){..    str
15a29 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20  uct RowSetChunk 
15a2a 2a 70 4e 65 77 3b 0d 0a 20 20 20 20 70 4e 65 77  *pNew;..    pNew
15a2b 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
15a2c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a  ocRaw(p->db, siz
15a2d 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0d 0a 20 20  eof(*pNew));..  
15a2e 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
15a2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d  ..      return;.
15a30 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4e 65 77  .    }..    pNew
15a31 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70  ->pNextChunk = p
15a32 2d 3e 70 43 68 75 6e 6b 3b 0d 0a 20 20 20 20 70  ->pChunk;..    p
15a33 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b  ->pChunk = pNew;
15a34 0d 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20  ..    p->pFresh 
15a35 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0d  = pNew->aEntry;.
15a36 0a 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d  .    p->nFresh =
15a37 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45   ROWSET_ENTRY_PE
15a38 52 5f 43 48 55 4e 4b 3b 0d 0a 20 20 7d 0d 0a 20  R_CHUNK;..  }.. 
15a39 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72   pEntry = p->pFr
15a3a 65 73 68 2b 2b 3b 0d 0a 20 20 70 2d 3e 6e 46 72  esh++;..  p->nFr
15a3b 65 73 68 2d 2d 3b 0d 0a 20 20 70 45 6e 74 72 79  esh--;..  pEntry
15a3c 2d 3e 76 20 3d 20 72 6f 77 69 64 3b 0d 0a 20 20  ->v = rowid;..  
15a3d 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d  pEntry->pRight =
15a3e 20 30 3b 0d 0a 20 20 70 4c 61 73 74 20 3d 20 70   0;..  pLast = p
15a3f 2d 3e 70 4c 61 73 74 3b 0d 0a 20 20 69 66 28 20  ->pLast;..  if( 
15a40 70 4c 61 73 74 20 29 7b 0d 0a 20 20 20 20 69 66  pLast ){..    if
15a41 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26 26  ( p->isSorted &&
15a42 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e 76   rowid<=pLast->v
15a43 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 73   ){..      p->is
15a44 53 6f 72 74 65 64 20 3d 20 30 3b 0d 0a 20 20 20  Sorted = 0;..   
15a45 20 7d 0d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70   }..    pLast->p
15a46 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0d  Right = pEntry;.
15a47 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61  .  }else{..    a
15a48 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79  ssert( p->pEntry
15a49 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 20  ==0 ); /* Fires 
15a4a 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72 20  if INSERT after 
15a4b 53 4d 41 4c 4c 45 53 54 20 2a 2f 0d 0a 20 20 20  SMALLEST */..   
15a4c 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e   p->pEntry = pEn
15a4d 74 72 79 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e  try;..  }..  p->
15a4e 70 4c 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0d  pLast = pEntry;.
15a4f 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 65 72  .}..../*..** Mer
15a50 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20  ge two lists of 
15a51 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65  RowSetEntry obje
15a52 63 74 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70  cts.  Remove dup
15a53 6c 69 63 61 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  licates...**..**
15a54 20 54 68 65 20 69 6e 70 75 74 20 6c 69 73 74 73   The input lists
15a55 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 64 20 76   are connected v
15a56 69 61 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65  ia pRight pointe
15a57 72 73 20 61 6e 64 20 61 72 65 20 0d 0a 2a 2a 20  rs and are ..** 
15a58 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63 68 20  assumed to each 
15a59 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20 73 6f  already be in so
15a5a 72 74 65 64 20 6f 72 64 65 72 2e 0d 0a 2a 2f 0d  rted order...*/.
15a5b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52  .static struct R
15a5c 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53  owSetEntry *rowS
15a5d 65 74 4d 65 72 67 65 28 0d 0a 20 20 73 74 72 75  etMerge(..  stru
15a5e 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
15a5f 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20  pA,    /* First 
15a60 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62  sorted list to b
15a61 65 20 6d 65 72 67 65 64 20 2a 2f 0d 0a 20 20 73  e merged */..  s
15a62 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
15a63 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63  y *pB     /* Sec
15a64 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20  ond sorted list 
15a65 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0d  to be merged */.
15a66 0a 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 52 6f  .){..  struct Ro
15a67 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0d  wSetEntry head;.
15a68 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
15a69 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0d 0a 0d  Entry *pTail;...
15a6a 0a 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64  .  pTail = &head
15a6b 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26  ;..  while( pA &
15a6c 26 20 70 42 20 29 7b 0d 0a 20 20 20 20 61 73 73  & pB ){..    ass
15a6d 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d  ert( pA->pRight=
15a6e 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d  =0 || pA->v<=pA-
15a6f 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0d 0a 20  >pRight->v );.. 
15a70 20 20 20 61 73 73 65 72 74 28 20 70 42 2d 3e 70     assert( pB->p
15a71 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e  Right==0 || pB->
15a72 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76  v<=pB->pRight->v
15a73 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 41 2d   );..    if( pA-
15a74 3e 76 3c 70 42 2d 3e 76 20 29 7b 0d 0a 20 20 20  >v<pB->v ){..   
15a75 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74     pTail->pRight
15a76 20 3d 20 70 41 3b 0d 0a 20 20 20 20 20 20 70 41   = pA;..      pA
15a77 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0d 0a   = pA->pRight;..
15a78 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54        pTail = pT
15a79 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20  ail->pRight;..  
15a7a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e    }else if( pB->
15a7b 76 3c 70 41 2d 3e 76 20 29 7b 0d 0a 20 20 20 20  v<pA->v ){..    
15a7c 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20    pTail->pRight 
15a7d 3d 20 70 42 3b 0d 0a 20 20 20 20 20 20 70 42 20  = pB;..      pB 
15a7e 3d 20 70 42 2d 3e 70 52 69 67 68 74 3b 0d 0a 20  = pB->pRight;.. 
15a7f 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61       pTail = pTa
15a80 69 6c 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20  il->pRight;..   
15a81 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70   }else{..      p
15a82 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0d  A = pA->pRight;.
15a83 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69  .    }..  }..  i
15a84 66 28 20 70 41 20 29 7b 0d 0a 20 20 20 20 61 73  f( pA ){..    as
15a85 73 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74  sert( pA->pRight
15a86 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41  ==0 || pA->v<=pA
15a87 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0d 0a  ->pRight->v );..
15a88 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68      pTail->pRigh
15a89 74 20 3d 20 70 41 3b 0d 0a 20 20 7d 65 6c 73 65  t = pA;..  }else
15a8a 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
15a8b 42 3d 3d 30 20 7c 7c 20 70 42 2d 3e 70 52 69 67  B==0 || pB->pRig
15a8c 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d  ht==0 || pB->v<=
15a8d 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b  pB->pRight->v );
15a8e 0d 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69  ..    pTail->pRi
15a8f 67 68 74 20 3d 20 70 42 3b 0d 0a 20 20 7d 0d 0a  ght = pB;..  }..
15a90 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e 70 52    return head.pR
15a91 69 67 68 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ight;..}..../*..
15a92 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d  ** Sort all elem
15a93 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74  ents on the pEnt
15a94 72 79 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52  ry list of the R
15a95 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e  owSet into ascen
15a96 64 69 6e 67 20 6f 72 64 65 72 2e 0d 0a 2a 2f 20  ding order...*/ 
15a97 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f  ..static void ro
15a98 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 20  wSetSort(RowSet 
15a99 2a 70 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  *p){..  unsigned
15a9a 20 69 6e 74 20 69 3b 0d 0a 20 20 73 74 72 75 63   int i;..  struc
15a9b 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
15a9c 45 6e 74 72 79 3b 0d 0a 20 20 73 74 72 75 63 74  Entry;..  struct
15a9d 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42   RowSetEntry *aB
15a9e 75 63 6b 65 74 5b 34 30 5d 3b 0d 0a 0d 0a 20 20  ucket[40];....  
15a9f 61 73 73 65 72 74 28 20 70 2d 3e 69 73 53 6f 72  assert( p->isSor
15aa0 74 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 6d 65 6d  ted==0 );..  mem
15aa1 73 65 74 28 61 42 75 63 6b 65 74 2c 20 30 2c 20  set(aBucket, 0, 
15aa2 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 29  sizeof(aBucket))
15aa3 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70  ;..  while( p->p
15aa4 45 6e 74 72 79 20 29 7b 0d 0a 20 20 20 20 70 45  Entry ){..    pE
15aa5 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79  ntry = p->pEntry
15aa6 3b 0d 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79  ;..    p->pEntry
15aa7 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68   = pEntry->pRigh
15aa8 74 3b 0d 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e  t;..    pEntry->
15aa9 70 52 69 67 68 74 20 3d 20 30 3b 0d 0a 20 20 20  pRight = 0;..   
15aaa 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b 65   for(i=0; aBucke
15aab 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  t[i]; i++){..   
15aac 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53     pEntry = rowS
15aad 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b  etMerge(aBucket[
15aae 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0d 0a 20 20  i], pEntry);..  
15aaf 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d      aBucket[i] =
15ab0 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   0;..    }..    
15ab1 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e  aBucket[i] = pEn
15ab2 74 72 79 3b 0d 0a 20 20 7d 0d 0a 20 20 70 45 6e  try;..  }..  pEn
15ab3 74 72 79 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28  try = 0;..  for(
15ab4 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42  i=0; i<sizeof(aB
15ab5 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42  ucket)/sizeof(aB
15ab6 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b  ucket[0]); i++){
15ab7 0d 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72  ..    pEntry = r
15ab8 6f 77 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72  owSetMerge(pEntr
15ab9 79 2c 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0d  y, aBucket[i]);.
15aba 0a 20 20 7d 0d 0a 20 20 70 2d 3e 70 45 6e 74 72  .  }..  p->pEntr
15abb 79 20 3d 20 70 45 6e 74 72 79 3b 0d 0a 20 20 70  y = pEntry;..  p
15abc 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0d 0a 20 20  ->pLast = 0;..  
15abd 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b  p->isSorted = 1;
15abe 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
15abf 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20  The input, pIn, 
15ac0 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65  is a binary tree
15ac1 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66   (or subtree) of
15ac2 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a   RowSetEntry obj
15ac3 65 63 74 73 2e 0d 0a 2a 2a 20 43 6f 6e 76 65 72  ects...** Conver
15ac4 74 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f  t this tree into
15ac5 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63   a linked list c
15ac6 6f 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20  onnected by the 
15ac7 70 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0d  pRight pointers.
15ac8 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70  .** and return p
15ac9 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66  ointers to the f
15aca 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c  irst and last el
15acb 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65  ements of the ne
15acc 77 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  w list...*/..sta
15acd 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54  tic void rowSetT
15ace 72 65 65 54 6f 4c 69 73 74 28 0d 0a 20 20 73 74  reeToList(..  st
15acf 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
15ad0 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f   *pIn,         /
15ad1 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e  * Root of the in
15ad2 70 75 74 20 74 72 65 65 20 2a 2f 0d 0a 20 20 73  put tree */..  s
15ad3 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
15ad4 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20  y **ppFirst,    
15ad5 2f 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66  /* Write head of
15ad6 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74   the output list
15ad7 20 68 65 72 65 20 2a 2f 0d 0a 20 20 73 74 72 75   here */..  stru
15ad8 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
15ad9 2a 70 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20  *ppLast      /* 
15ada 57 72 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68  Write tail of th
15adb 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65  e output list he
15adc 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 61 73 73  re */..){..  ass
15add 65 72 74 28 20 70 49 6e 21 3d 30 20 29 3b 0d 0a  ert( pIn!=0 );..
15ade 20 20 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 74    if( pIn->pLeft
15adf 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20   ){..    struct 
15ae0 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0d  RowSetEntry *p;.
15ae1 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54  .    rowSetTreeT
15ae2 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 4c 65 66 74  oList(pIn->pLeft
15ae3 2c 20 70 70 46 69 72 73 74 2c 20 26 70 29 3b 0d  , ppFirst, &p);.
15ae4 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d  .    p->pRight =
15ae5 20 70 49 6e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d   pIn;..  }else{.
15ae6 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d 20  .    *ppFirst = 
15ae7 70 49 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  pIn;..  }..  if(
15ae8 20 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0d   pIn->pRight ){.
15ae9 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54  .    rowSetTreeT
15aea 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68  oList(pIn->pRigh
15aeb 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c  t, &pIn->pRight,
15aec 20 70 70 4c 61 73 74 29 3b 0d 0a 20 20 7d 65 6c   ppLast);..  }el
15aed 73 65 7b 0d 0a 20 20 20 20 2a 70 70 4c 61 73 74  se{..    *ppLast
15aee 20 3d 20 70 49 6e 3b 0d 0a 20 20 7d 0d 0a 20 20   = pIn;..  }..  
15aef 61 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74  assert( (*ppLast
15af0 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0d  )->pRight==0 );.
15af1 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  .}....../*..** C
15af2 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20  onvert a sorted 
15af3 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73  list of elements
15af4 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70   (connected by p
15af5 52 69 67 68 74 29 20 69 6e 74 6f 20 61 20 62 69  Right) into a bi
15af6 6e 61 72 79 0d 0a 2a 2a 20 74 72 65 65 20 77 69  nary..** tree wi
15af7 74 68 20 64 65 70 74 68 20 6f 66 20 69 44 65 70  th depth of iDep
15af8 74 68 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20  th.  A depth of 
15af9 31 20 6d 65 61 6e 73 20 74 68 65 20 74 72 65 65  1 means the tree
15afa 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
15afb 6c 65 0d 0a 2a 2a 20 6e 6f 64 65 20 74 61 6b 65  le..** node take
15afc 6e 20 66 72 6f 6d 20 74 68 65 20 68 65 61 64 20  n from the head 
15afd 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20 41 20 64  of *ppList.  A d
15afe 65 70 74 68 20 6f 66 20 32 20 6d 65 61 6e 73 20  epth of 2 means 
15aff 61 20 74 72 65 65 20 77 69 74 68 0d 0a 2a 2a 20  a tree with..** 
15b00 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20 41 6e  three nodes.  An
15b01 64 20 73 6f 20 66 6f 72 74 68 2e 0d 0a 2a 2a 0d  d so forth...**.
15b02 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20  .** Use as many 
15b03 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65  entries from the
15b04 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72   input list as r
15b05 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61  equired and upda
15b06 74 65 20 74 68 65 0d 0a 2a 2a 20 2a 70 70 4c 69  te the..** *ppLi
15b07 73 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  st to point to t
15b08 68 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e  he unused elemen
15b09 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20  ts of the list. 
15b0a 20 49 66 20 74 68 65 20 69 6e 70 75 74 0d 0a 2a   If the input..*
15b0b 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20  * list contains 
15b0c 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73  too few elements
15b0d 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74  , then construct
15b0e 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74   an incomplete t
15b0f 72 65 65 0d 0a 2a 2a 20 61 6e 64 20 6c 65 61 76  ree..** and leav
15b10 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20 74 6f  e *ppList set to
15b11 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52   NULL...**..** R
15b12 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
15b13 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  to the root of t
15b14 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  he constructed b
15b15 69 6e 61 72 79 20 74 72 65 65 2e 0d 0a 2a 2f 0d  inary tree...*/.
15b16 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52  .static struct R
15b17 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53  owSetEntry *rowS
15b18 65 74 4e 44 65 65 70 54 72 65 65 28 0d 0a 20 20  etNDeepTree(..  
15b19 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
15b1a 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0d 0a 20 20  ry **ppList,..  
15b1b 69 6e 74 20 69 44 65 70 74 68 0d 0a 29 7b 0d 0a  int iDepth..){..
15b1c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
15b1d 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 20  ntry *p;        
15b1e 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20   /* Root of the 
15b1f 6e 65 77 20 74 72 65 65 20 2a 2f 0d 0a 20 20 73  new tree */..  s
15b20 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
15b21 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a  y *pLeft;     /*
15b22 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f   Left subtree */
15b23 0d 0a 20 20 69 66 28 20 2a 70 70 4c 69 73 74 3d  ..  if( *ppList=
15b24 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
15b25 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  n 0;..  }..  if(
15b26 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0d 0a 20   iDepth==1 ){.. 
15b27 20 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0d     p = *ppList;.
15b28 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70  .    *ppList = p
15b29 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 70  ->pRight;..    p
15b2a 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69  ->pLeft = p->pRi
15b2b 67 68 74 20 3d 20 30 3b 0d 0a 20 20 20 20 72 65  ght = 0;..    re
15b2c 74 75 72 6e 20 70 3b 0d 0a 20 20 7d 0d 0a 20 20  turn p;..  }..  
15b2d 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44  pLeft = rowSetND
15b2e 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20  eepTree(ppList, 
15b2f 69 44 65 70 74 68 2d 31 29 3b 0d 0a 20 20 70 20  iDepth-1);..  p 
15b30 3d 20 2a 70 70 4c 69 73 74 3b 0d 0a 20 20 69 66  = *ppList;..  if
15b31 28 20 70 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ( p==0 ){..    r
15b32 65 74 75 72 6e 20 70 4c 65 66 74 3b 0d 0a 20 20  eturn pLeft;..  
15b33 7d 0d 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20  }..  p->pLeft = 
15b34 70 4c 65 66 74 3b 0d 0a 20 20 2a 70 70 4c 69 73  pLeft;..  *ppLis
15b35 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0d 0a  t = p->pRight;..
15b36 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f    p->pRight = ro
15b37 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 70 70  wSetNDeepTree(pp
15b38 4c 69 73 74 2c 20 69 44 65 70 74 68 2d 31 29 3b  List, iDepth-1);
15b39 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 7d  ..  return p;..}
15b3a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65  ..../*..** Conve
15b3b 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74  rt a sorted list
15b3c 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74   of elements int
15b3d 6f 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e  o a binary tree.
15b3e 20 4d 61 6b 65 20 74 68 65 20 74 72 65 65 0d 0a   Make the tree..
15b3f 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20 69 74  ** as deep as it
15b40 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 20   needs to be in 
15b41 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e  order to contain
15b42 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74   the entire list
15b43 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 73 74  ...*/..static st
15b44 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
15b45 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72   *rowSetListToTr
15b46 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74  ee(struct RowSet
15b47 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0d 0a  Entry *pList){..
15b48 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20    int iDepth;   
15b49 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68          /* Depth
15b4a 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20   of the tree so 
15b4b 66 61 72 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  far */..  struct
15b4c 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b   RowSetEntry *p;
15b4d 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
15b4e 74 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0d 0a  t tree root */..
15b4f 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
15b50 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f  ntry *pLeft;   /
15b51 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a  * Left subtree *
15b52 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  /....  assert( p
15b53 4c 69 73 74 21 3d 30 20 29 3b 0d 0a 20 20 70 20  List!=0 );..  p 
15b54 3d 20 70 4c 69 73 74 3b 0d 0a 20 20 70 4c 69 73  = pList;..  pLis
15b55 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0d 0a  t = p->pRight;..
15b56 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e    p->pLeft = p->
15b57 70 52 69 67 68 74 20 3d 20 30 3b 0d 0a 20 20 66  pRight = 0;..  f
15b58 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70 4c 69  or(iDepth=1; pLi
15b59 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b 0d 0a  st; iDepth++){..
15b5a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0d 0a      pLeft = p;..
15b5b 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0d 0a      p = pList;..
15b5c 20 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70      pList = p->p
15b5d 52 69 67 68 74 3b 0d 0a 20 20 20 20 70 2d 3e 70  Right;..    p->p
15b5e 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0d 0a 20  Left = pLeft;.. 
15b5f 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 72     p->pRight = r
15b60 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 26  owSetNDeepTree(&
15b61 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29 3b 0d  pList, iDepth);.
15b62 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
15b63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
15b64 6f 6e 76 65 72 74 20 74 68 65 20 6c 69 73 74 20  onvert the list 
15b65 69 6e 20 70 2d 3e 70 45 6e 74 72 79 20 69 6e 74  in p->pEntry int
15b66 6f 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20  o a sorted list 
15b67 69 66 20 69 74 20 69 73 20 6e 6f 74 0d 0a 2a 2a  if it is not..**
15b68 20 73 6f 72 74 65 64 20 61 6c 72 65 61 64 79 2e   sorted already.
15b69 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20    If there is a 
15b6a 62 69 6e 61 72 79 20 74 72 65 65 20 6f 6e 20 70  binary tree on p
15b6b 2d 3e 70 54 72 65 65 2c 20 74 68 65 6e 0d 0a 2a  ->pTree, then..*
15b6c 2a 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74  * convert it int
15b6d 6f 20 61 20 6c 69 73 74 20 74 6f 6f 20 61 6e 64  o a list too and
15b6e 20 6d 65 72 67 65 20 69 74 20 69 6e 74 6f 20 74   merge it into t
15b6f 68 65 20 70 2d 3e 70 45 6e 74 72 79 20 6c 69 73  he p->pEntry lis
15b70 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  t...*/..static v
15b71 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c 69 73 74  oid rowSetToList
15b72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0d 0a 20 20  (RowSet *p){..  
15b73 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 65 64  if( !p->isSorted
15b74 20 29 7b 0d 0a 20 20 20 20 72 6f 77 53 65 74 53   ){..    rowSetS
15b75 6f 72 74 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20  ort(p);..  }..  
15b76 69 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0d  if( p->pTree ){.
15b77 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53  .    struct RowS
15b78 65 74 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20  etEntry *pHead, 
15b79 2a 70 54 61 69 6c 3b 0d 0a 20 20 20 20 72 6f 77  *pTail;..    row
15b7a 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 2d  SetTreeToList(p-
15b7b 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64 2c 20  >pTree, &pHead, 
15b7c 26 70 54 61 69 6c 29 3b 0d 0a 20 20 20 20 70 2d  &pTail);..    p-
15b7d 3e 70 54 72 65 65 20 3d 20 30 3b 0d 0a 20 20 20  >pTree = 0;..   
15b7e 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77   p->pEntry = row
15b7f 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74  SetMerge(p->pEnt
15b80 72 79 2c 20 70 48 65 61 64 29 3b 0d 0a 20 20 7d  ry, pHead);..  }
15b81 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78  ..}..../*..** Ex
15b82 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65  tract the smalle
15b83 73 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20  st element from 
15b84 74 68 65 20 52 6f 77 53 65 74 2e 0d 0a 2a 2a 20  the RowSet...** 
15b85 57 72 69 74 65 20 74 68 65 20 65 6c 65 6d 65 6e  Write the elemen
15b86 74 20 69 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20  t into *pRowid. 
15b87 20 52 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63   Return 1 on suc
15b88 63 65 73 73 2e 20 20 52 65 74 75 72 6e 0d 0a 2a  cess.  Return..*
15b89 2a 20 30 20 69 66 20 74 68 65 20 52 6f 77 53 65  * 0 if the RowSe
15b8a 74 20 69 73 20 61 6c 72 65 61 64 79 20 65 6d 70  t is already emp
15b8b 74 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65  ty...**..** Afte
15b8c 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68  r this routine h
15b8d 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  as been called, 
15b8e 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77 53 65  the sqlite3RowSe
15b8f 74 49 6e 73 65 72 74 28 29 0d 0a 2a 2a 20 72 6f  tInsert()..** ro
15b90 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62 65  utine may not be
15b91 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20 20   called again.  
15b92 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
15b93 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
15b94 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65  RowSetNext(RowSe
15b95 74 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69  t *p, i64 *pRowi
15b96 64 29 7b 0d 0a 20 20 72 6f 77 53 65 74 54 6f 4c  d){..  rowSetToL
15b97 69 73 74 28 70 29 3b 0d 0a 20 20 69 66 28 20 70  ist(p);..  if( p
15b98 2d 3e 70 45 6e 74 72 79 20 29 7b 0d 0a 20 20 20  ->pEntry ){..   
15b99 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 45   *pRowid = p->pE
15b9a 6e 74 72 79 2d 3e 76 3b 0d 0a 20 20 20 20 70 2d  ntry->v;..    p-
15b9b 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e  >pEntry = p->pEn
15b9c 74 72 79 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20  try->pRight;..  
15b9d 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d    if( p->pEntry=
15b9e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 ){..      sql
15b9f 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
15ba0 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  p);..    }..    
15ba1 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 65 6c  return 1;..  }el
15ba2 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  se{..    return 
15ba3 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  0;..  }..}..../*
15ba4 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  ..** Check to se
15ba5 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f  e if element iRo
15ba6 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64  wid was inserted
15ba7 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f   into the the ro
15ba8 77 73 65 74 20 61 73 0d 0a 2a 2a 20 70 61 72 74  wset as..** part
15ba9 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62   of any insert b
15baa 61 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42  atch prior to iB
15bab 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20  atch.  Return 1 
15bac 6f 72 20 30 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  or 0...*/..SQLIT
15bad 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15bae 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28  lite3RowSetTest(
15baf 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 2c  RowSet *pRowSet,
15bb0 20 75 38 20 69 42 61 74 63 68 2c 20 73 71 6c 69   u8 iBatch, sqli
15bb1 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64  te3_int64 iRowid
15bb2 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 52 6f 77  ){..  struct Row
15bb3 53 65 74 45 6e 74 72 79 20 2a 70 3b 0d 0a 20 20  SetEntry *p;..  
15bb4 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77  if( iBatch!=pRow
15bb5 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0d 0a  Set->iBatch ){..
15bb6 20 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d      if( pRowSet-
15bb7 3e 70 45 6e 74 72 79 20 29 7b 0d 0a 20 20 20 20  >pEntry ){..    
15bb8 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70    rowSetToList(p
15bb9 52 6f 77 53 65 74 29 3b 0d 0a 20 20 20 20 20 20  RowSet);..      
15bba 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 20 3d  pRowSet->pTree =
15bbb 20 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65   rowSetListToTre
15bbc 65 28 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72  e(pRowSet->pEntr
15bbd 79 29 3b 0d 0a 20 20 20 20 20 20 70 52 6f 77 53  y);..      pRowS
15bbe 65 74 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0d  et->pEntry = 0;.
15bbf 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e  .      pRowSet->
15bc0 70 4c 61 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20  pLast = 0;..    
15bc1 7d 0d 0a 20 20 20 20 70 52 6f 77 53 65 74 2d 3e  }..    pRowSet->
15bc2 69 42 61 74 63 68 20 3d 20 69 42 61 74 63 68 3b  iBatch = iBatch;
15bc3 0d 0a 20 20 7d 0d 0a 20 20 70 20 3d 20 70 52 6f  ..  }..  p = pRo
15bc4 77 53 65 74 2d 3e 70 54 72 65 65 3b 0d 0a 20 20  wSet->pTree;..  
15bc5 77 68 69 6c 65 28 20 70 20 29 7b 0d 0a 20 20 20  while( p ){..   
15bc6 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64   if( p->v<iRowid
15bc7 20 29 7b 0d 0a 20 20 20 20 20 20 70 20 3d 20 70   ){..      p = p
15bc8 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 7d  ->pRight;..    }
15bc9 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52  else if( p->v>iR
15bca 6f 77 69 64 20 29 7b 0d 0a 20 20 20 20 20 20 70  owid ){..      p
15bcb 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20   = p->pLeft;..  
15bcc 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
15bcd 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d  return 1;..    }
15bce 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
15bcf 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  0;..}..../******
15bd0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
15bd1 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  rowset.c *******
15bd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bd4 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
15bd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
15bd6 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a  file pager.c ***
15bd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bd9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
15bda 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
15bdb 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   15..**..** The 
15bdc 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
15bdd 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
15bde 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
15bdf 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
15be0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
15be1 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
15be2 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
15be3 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
15be4 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
15be5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
15be6 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
15be7 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
15be8 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
15be9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
15bea 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
15beb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
15bec 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
15bed 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
15bee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15bf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20  ***********..** 
15bf2 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c  This is the impl
15bf3 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
15bf4 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62  e page cache sub
15bf5 73 79 73 74 65 6d 20 6f 72 20 22 70 61 67 65 72  system or "pager
15bf6 22 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 54 68 65 20  "...** ..** The 
15bf7 70 61 67 65 72 20 69 73 20 75 73 65 64 20 74 6f  pager is used to
15bf8 20 61 63 63 65 73 73 20 61 20 64 61 74 61 62 61   access a databa
15bf9 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 20 49  se disk file.  I
15bfa 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0d 0a 2a 2a  t implements..**
15bfb 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 61   atomic commit a
15bfc 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 6f  nd rollback thro
15bfd 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20 61  ugh the use of a
15bfe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
15bff 61 74 0d 0a 2a 2a 20 69 73 20 73 65 70 61 72 61  at..** is separa
15c00 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  te from the data
15c01 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
15c02 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65  pager also imple
15c03 6d 65 6e 74 73 20 66 69 6c 65 0d 0a 2a 2a 20 6c  ments file..** l
15c04 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e  ocking to preven
15c05 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  t two processes 
15c06 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65  from writing the
15c07 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0d 0a   same database..
15c08 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e  ** file simultan
15c09 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70  eously, or one p
15c0a 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64  rocess from read
15c0b 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
15c0c 20 77 68 69 6c 65 0d 0a 2a 2a 20 61 6e 6f 74 68   while..** anoth
15c0d 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0d 0a  er is writing...
15c0e 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
15c0f 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0d 0a  TE_OMIT_DISKIO..
15c10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
15c11 49 6e 63 6c 75 64 65 20 77 61 6c 2e 68 20 69 6e  Include wal.h in
15c12 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 70   the middle of p
15c13 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.c *********
15c14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
15c15 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
15c16 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 2e   Begin file wal.
15c17 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
15c18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15c19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
15c1a 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 31 30 20 46 65  ../*..** 2010 Fe
15c1b 62 72 75 61 72 79 20 31 0d 0a 2a 2a 0d 0a 2a 2a  bruary 1..**..**
15c1c 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
15c1d 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
15c1e 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
15c1f 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
15c20 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
15c21 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
15c22 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
15c23 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
15c24 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
15c25 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
15c26 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
15c27 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
15c28 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
15c29 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
15c2a 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
15c2b 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
15c2c 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
15c2d 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
15c2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15c2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15c32 0d 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  ..** This header
15c33 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
15c34 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
15c35 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c  he write-ahead l
15c36 6f 67 67 69 6e 67 20 0d 0a 2a 2a 20 73 79 73 74  ogging ..** syst
15c37 65 6d 2e 20 52 65 66 65 72 20 74 6f 20 74 68 65  em. Refer to the
15c38 20 63 6f 6d 6d 65 6e 74 73 20 62 65 6c 6f 77 20   comments below 
15c39 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 63  and the header c
15c3a 6f 6d 6d 65 6e 74 20 61 74 74 61 63 68 65 64 20  omment attached 
15c3b 74 6f 20 0d 0a 2a 2a 20 74 68 65 20 69 6d 70 6c  to ..** the impl
15c3c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 65 61  ementation of ea
15c3d 63 68 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6c  ch function in l
15c3e 6f 67 2e 63 20 66 6f 72 20 66 75 72 74 68 65 72  og.c for further
15c3f 20 64 65 74 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 0d   details...*/...
15c40 0a 23 69 66 6e 64 65 66 20 5f 57 41 4c 5f 48 5f  .#ifndef _WAL_H_
15c41 0d 0a 23 64 65 66 69 6e 65 20 5f 57 41 4c 5f 48  ..#define _WAL_H
15c42 5f 0d 0a 0d 0a 0d 0a 2f 2a 20 41 64 64 69 74 69  _....../* Additi
15c43 6f 6e 61 6c 20 76 61 6c 75 65 73 20 74 68 61 74  onal values that
15c44 20 63 61 6e 20 62 65 20 61 64 64 65 64 20 74 6f   can be added to
15c45 20 74 68 65 20 73 79 6e 63 5f 66 6c 61 67 73 20   the sync_flags 
15c46 61 72 67 75 6d 65 6e 74 20 6f 66 0d 0a 2a 2a 20  argument of..** 
15c47 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 73  sqlite3WalFrames
15c48 28 29 3a 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  ():..*/..#define
15c49 20 57 41 4c 5f 53 59 4e 43 5f 54 52 41 4e 53 41   WAL_SYNC_TRANSA
15c4a 43 54 49 4f 4e 53 20 20 30 78 32 30 20 20 20 2f  CTIONS  0x20   /
15c4b 2a 20 53 79 6e 63 20 61 74 20 74 68 65 20 65 6e  * Sync at the en
15c4c 64 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61  d of each transa
15c4d 63 74 69 6f 6e 20 2a 2f 0d 0a 23 64 65 66 69 6e  ction */..#defin
15c4e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4d 41  e SQLITE_SYNC_MA
15c4f 53 4b 20 20 20 20 20 20 20 30 78 31 33 20 20 20  SK       0x13   
15c50 2f 2a 20 4d 61 73 6b 20 6f 66 66 20 74 68 65 20  /* Mask off the 
15c51 53 51 4c 49 54 45 5f 53 59 4e 43 5f 2a 20 76 61  SQLITE_SYNC_* va
15c52 6c 75 65 73 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65  lues */....#ifde
15c53 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
15c54 4c 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  L..# define sqli
15c55 74 65 33 57 61 6c 4f 70 65 6e 28 78 2c 79 2c 7a  te3WalOpen(x,y,z
15c56 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
15c57 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20      0..# define 
15c58 73 71 6c 69 74 65 33 57 61 6c 4c 69 6d 69 74 28  sqlite3WalLimit(
15c59 78 2c 79 29 0d 0a 23 20 64 65 66 69 6e 65 20 73  x,y)..# define s
15c5a 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 77  qlite3WalClose(w
15c5b 2c 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20  ,x,y,z)         
15c5c 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69         0..# defi
15c5d 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67  ne sqlite3WalBeg
15c5e 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f  inReadTransactio
15c5f 6e 28 79 2c 7a 29 20 20 20 20 20 30 0d 0a 23 20  n(y,z)     0..# 
15c60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61  define sqlite3Wa
15c61 6c 45 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74  lEndReadTransact
15c62 69 6f 6e 28 7a 29 0d 0a 23 20 64 65 66 69 6e 65  ion(z)..# define
15c63 20 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28   sqlite3WalRead(
15c64 76 2c 77 2c 78 2c 79 2c 7a 29 20 20 20 20 20 20  v,w,x,y,z)      
15c65 20 20 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65           0..# de
15c66 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 44  fine sqlite3WalD
15c67 62 73 69 7a 65 28 79 29 20 20 20 20 20 20 20 20  bsize(y)        
15c68 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0d 0a               0..
15c69 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
15c6a 57 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61  WalBeginWriteTra
15c6b 6e 73 61 63 74 69 6f 6e 28 79 29 20 20 20 20 20  nsaction(y)     
15c6c 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c   0..# define sql
15c6d 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54  ite3WalEndWriteT
15c6e 72 61 6e 73 61 63 74 69 6f 6e 28 78 29 20 20 20  ransaction(x)   
15c6f 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65       0..# define
15c70 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f 28   sqlite3WalUndo(
15c71 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20  x,y,z)          
15c72 20 20 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65           0..# de
15c73 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 53  fine sqlite3WalS
15c74 61 76 65 70 6f 69 6e 74 28 79 2c 7a 29 0d 0a 23  avepoint(y,z)..#
15c75 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57   define sqlite3W
15c76 61 6c 53 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28  alSavepointUndo(
15c77 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20 20 20  y,z)            
15c78 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  0..# define sqli
15c79 74 65 33 57 61 6c 46 72 61 6d 65 73 28 75 2c 76  te3WalFrames(u,v
15c7a 2c 77 2c 78 2c 79 2c 7a 29 20 20 20 20 20 20 20  ,w,x,y,z)       
15c7b 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20      0..# define 
15c7c 73 71 6c 69 74 65 33 57 61 6c 43 68 65 63 6b 70  sqlite3WalCheckp
15c7d 6f 69 6e 74 28 72 2c 73 2c 74 2c 75 2c 76 2c 77  oint(r,s,t,u,v,w
15c7e 2c 78 2c 79 2c 7a 29 20 30 0d 0a 23 20 64 65 66  ,x,y,z) 0..# def
15c7f 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 43 61  ine sqlite3WalCa
15c80 6c 6c 62 61 63 6b 28 7a 29 20 20 20 20 20 20 20  llback(z)       
15c81 20 20 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23              0..#
15c82 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57   define sqlite3W
15c83 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 28  alExclusiveMode(
15c84 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20 20 20  y,z)            
15c85 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  0..# define sqli
15c86 74 65 33 57 61 6c 48 65 61 70 4d 65 6d 6f 72 79  te3WalHeapMemory
15c87 28 7a 29 20 20 20 20 20 20 20 20 20 20 20 20 20  (z)             
15c88 20 20 20 20 30 0d 0a 23 65 6c 73 65 0d 0a 0d 0a      0..#else....
15c89 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 41 56 45  #define WAL_SAVE
15c8a 50 4f 49 4e 54 5f 4e 44 41 54 41 20 34 0d 0a 0d  POINT_NDATA 4...
15c8b 0a 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74  ./* Connection t
15c8c 6f 20 61 20 77 72 69 74 65 2d 61 68 65 61 64 20  o a write-ahead 
15c8d 6c 6f 67 20 28 57 41 4c 29 20 66 69 6c 65 2e 20  log (WAL) file. 
15c8e 0d 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e  ..** There is on
15c8f 65 20 6f 62 6a 65 63 74 20 6f 66 20 74 68 69 73  e object of this
15c90 20 74 79 70 65 20 66 6f 72 20 65 61 63 68 20 70   type for each p
15c91 61 67 65 72 2e 20 0d 0a 2a 2f 0d 0a 74 79 70 65  ager. ..*/..type
15c92 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 20 57  def struct Wal W
15c93 61 6c 3b 0d 0a 0d 0a 2f 2a 20 4f 70 65 6e 20 61  al;..../* Open a
15c94 6e 64 20 63 6c 6f 73 65 20 61 20 63 6f 6e 6e 65  nd close a conne
15c95 63 74 69 6f 6e 20 74 6f 20 61 20 77 72 69 74 65  ction to a write
15c96 2d 61 68 65 61 64 20 6c 6f 67 2e 20 2a 2f 0d 0a  -ahead log. */..
15c97 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15c98 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 4f 70 65  nt sqlite3WalOpe
15c99 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  n(sqlite3_vfs*, 
15c9a 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63  sqlite3_file*, c
15c9b 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
15c9c 2c 20 69 36 34 2c 20 57 61 6c 2a 2a 29 3b 0d 0a  , i64, Wal**);..
15c9d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15c9e 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f  nt sqlite3WalClo
15c9f 73 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e  se(Wal *pWal, in
15ca0 74 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20 69 6e  t sync_flags, in
15ca1 74 2c 20 75 38 20 2a 29 3b 0d 0a 0d 0a 2f 2a 20  t, u8 *);..../* 
15ca2 53 65 74 20 74 68 65 20 6c 69 6d 69 74 69 6e 67  Set the limiting
15ca3 20 73 69 7a 65 20 6f 66 20 61 20 57 41 4c 20 66   size of a WAL f
15ca4 69 6c 65 2e 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ile. */..SQLITE_
15ca5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
15ca6 69 74 65 33 57 61 6c 4c 69 6d 69 74 28 57 61 6c  ite3WalLimit(Wal
15ca7 2a 2c 20 69 36 34 29 3b 0d 0a 0d 0a 2f 2a 20 55  *, i64);..../* U
15ca8 73 65 64 20 62 79 20 72 65 61 64 65 72 73 20 74  sed by readers t
15ca9 6f 20 6f 70 65 6e 20 28 6c 6f 63 6b 29 20 61 6e  o open (lock) an
15caa 64 20 63 6c 6f 73 65 20 28 75 6e 6c 6f 63 6b 29  d close (unlock)
15cab 20 61 20 73 6e 61 70 73 68 6f 74 2e 20 20 41 20   a snapshot.  A 
15cac 0d 0a 2a 2a 20 73 6e 61 70 73 68 6f 74 20 69 73  ..** snapshot is
15cad 20 6c 69 6b 65 20 61 20 72 65 61 64 2d 74 72 61   like a read-tra
15cae 6e 73 61 63 74 69 6f 6e 2e 20 20 49 74 20 69 73  nsaction.  It is
15caf 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
15cb0 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 61  e database..** a
15cb1 74 20 61 6e 20 69 6e 73 74 61 6e 74 20 69 6e 20  t an instant in 
15cb2 74 69 6d 65 2e 20 20 73 71 6c 69 74 65 33 57 61  time.  sqlite3Wa
15cb3 6c 4f 70 65 6e 53 6e 61 70 73 68 6f 74 20 67 65  lOpenSnapshot ge
15cb4 74 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 61  ts a read lock a
15cb5 6e 64 0d 0a 2a 2a 20 70 72 65 73 65 72 76 65 73  nd..** preserves
15cb6 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
15cb7 74 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f  te even if the o
15cb8 74 68 65 72 20 74 68 72 65 61 64 73 20 6f 72 20  ther threads or 
15cb9 70 72 6f 63 65 73 73 65 73 0d 0a 2a 2a 20 77 72  processes..** wr
15cba 69 74 65 20 74 6f 20 6f 72 20 63 68 65 63 6b 70  ite to or checkp
15cbb 6f 69 6e 74 20 74 68 65 20 57 41 4c 2e 20 20 73  oint the WAL.  s
15cbc 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 53 6e  qlite3WalCloseSn
15cbd 61 70 73 68 6f 74 28 29 20 63 6c 6f 73 65 73 20  apshot() closes 
15cbe 74 68 65 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  the..** transact
15cbf 69 6f 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 73  ion and releases
15cc0 20 74 68 65 20 6c 6f 63 6b 2e 0d 0a 2a 2f 0d 0a   the lock...*/..
15cc1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15cc2 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67  nt sqlite3WalBeg
15cc3 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f  inReadTransactio
15cc4 6e 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74  n(Wal *pWal, int
15cc5 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49   *);..SQLITE_PRI
15cc6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
15cc7 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e 73  3WalEndReadTrans
15cc8 61 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c  action(Wal *pWal
15cc9 29 3b 0d 0a 0d 0a 2f 2a 20 52 65 61 64 20 61 20  );..../* Read a 
15cca 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 77 72  page from the wr
15ccb 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 2c 20 69  ite-ahead log, i
15ccc 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e  f it is present.
15ccd 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
15cce 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
15ccf 61 6c 52 65 61 64 28 57 61 6c 20 2a 70 57 61 6c  alRead(Wal *pWal
15cd0 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74  , Pgno pgno, int
15cd1 20 2a 70 49 6e 57 61 6c 2c 20 69 6e 74 20 6e 4f   *pInWal, int nO
15cd2 75 74 2c 20 75 38 20 2a 70 4f 75 74 29 3b 0d 0a  ut, u8 *pOut);..
15cd3 0d 0a 2f 2a 20 49 66 20 74 68 65 20 57 41 4c 20  ../* If the WAL 
15cd4 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 72 65  is not empty, re
15cd5 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
15cd6 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 2a   the database. *
15cd7 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
15cd8 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 57 61  E Pgno sqlite3Wa
15cd9 6c 44 62 73 69 7a 65 28 57 61 6c 20 2a 70 57 61  lDbsize(Wal *pWa
15cda 6c 29 3b 0d 0a 0d 0a 2f 2a 20 4f 62 74 61 69 6e  l);..../* Obtain
15cdb 20 6f 72 20 72 65 6c 65 61 73 65 20 74 68 65 20   or release the 
15cdc 57 52 49 54 45 52 20 6c 6f 63 6b 2e 20 2a 2f 0d  WRITER lock. */.
15cdd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15cde 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 42 65  int sqlite3WalBe
15cdf 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74  ginWriteTransact
15ce0 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0d  ion(Wal *pWal);.
15ce1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15ce2 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 45 6e  int sqlite3WalEn
15ce3 64 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f  dWriteTransactio
15ce4 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0d 0a 0d  n(Wal *pWal);...
15ce5 0a 2f 2a 20 55 6e 64 6f 20 61 6e 79 20 66 72 61  ./* Undo any fra
15ce6 6d 65 73 20 77 72 69 74 74 65 6e 20 28 62 75 74  mes written (but
15ce7 20 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29 20   not committed) 
15ce8 74 6f 20 74 68 65 20 6c 6f 67 20 2a 2f 0d 0a 53  to the log */..S
15ce9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15cea 74 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f  t sqlite3WalUndo
15ceb 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20  (Wal *pWal, int 
15cec 28 2a 78 55 6e 64 6f 29 28 76 6f 69 64 20 2a 2c  (*xUndo)(void *,
15ced 20 50 67 6e 6f 29 2c 20 76 6f 69 64 20 2a 70 55   Pgno), void *pU
15cee 6e 64 6f 43 74 78 29 3b 0d 0a 0d 0a 2f 2a 20 52  ndoCtx);..../* R
15cef 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
15cf0 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 74 68   that records th
15cf1 65 20 63 75 72 72 65 6e 74 20 28 75 6e 63 6f 6d  e current (uncom
15cf2 6d 69 74 74 65 64 29 20 77 72 69 74 65 0d 0a 2a  mitted) write..*
15cf3 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  * position in th
15cf4 65 20 57 41 4c 20 2a 2f 0d 0a 53 51 4c 49 54 45  e WAL */..SQLITE
15cf5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15cf6 6c 69 74 65 33 57 61 6c 53 61 76 65 70 6f 69 6e  lite3WalSavepoin
15cf7 74 28 57 61 6c 20 2a 70 57 61 6c 2c 20 75 33 32  t(Wal *pWal, u32
15cf8 20 2a 61 57 61 6c 44 61 74 61 29 3b 0d 0a 0d 0a   *aWalData);....
15cf9 2f 2a 20 4d 6f 76 65 20 74 68 65 20 77 72 69 74  /* Move the writ
15cfa 65 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68  e position of th
15cfb 65 20 57 41 4c 20 62 61 63 6b 20 74 6f 20 69 46  e WAL back to iF
15cfc 72 61 6d 65 2e 20 20 43 61 6c 6c 65 64 20 69 6e  rame.  Called in
15cfd 0d 0a 2a 2a 20 72 65 73 70 6f 6e 73 65 20 74 6f  ..** response to
15cfe 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63   a ROLLBACK TO c
15cff 6f 6d 6d 61 6e 64 2e 20 2a 2f 0d 0a 53 51 4c 49  ommand. */..SQLI
15d00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15d01 71 6c 69 74 65 33 57 61 6c 53 61 76 65 70 6f 69  qlite3WalSavepoi
15d02 6e 74 55 6e 64 6f 28 57 61 6c 20 2a 70 57 61 6c  ntUndo(Wal *pWal
15d03 2c 20 75 33 32 20 2a 61 57 61 6c 44 61 74 61 29  , u32 *aWalData)
15d04 3b 0d 0a 0d 0a 2f 2a 20 57 72 69 74 65 20 61 20  ;..../* Write a 
15d05 66 72 61 6d 65 20 6f 72 20 66 72 61 6d 65 73 20  frame or frames 
15d06 74 6f 20 74 68 65 20 6c 6f 67 2e 20 2a 2f 0d 0a  to the log. */..
15d07 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15d08 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61  nt sqlite3WalFra
15d09 6d 65 73 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69  mes(Wal *pWal, i
15d0a 6e 74 2c 20 50 67 48 64 72 20 2a 2c 20 50 67 6e  nt, PgHdr *, Pgn
15d0b 6f 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0d 0a 0d  o, int, int);...
15d0c 0a 2f 2a 20 43 6f 70 79 20 70 61 67 65 73 20 66  ./* Copy pages f
15d0d 72 6f 6d 20 74 68 65 20 6c 6f 67 20 74 6f 20 74  rom the log to t
15d0e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15d0f 20 2a 2f 20 0d 0a 53 51 4c 49 54 45 5f 50 52 49   */ ..SQLITE_PRI
15d10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
15d11 57 61 6c 43 68 65 63 6b 70 6f 69 6e 74 28 0d 0a  WalCheckpoint(..
15d12 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20    Wal *pWal,    
15d13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d14 20 20 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64    /* Write-ahead
15d15 20 6c 6f 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   log connection 
15d16 2a 2f 0d 0a 20 20 69 6e 74 20 65 4d 6f 64 65 2c  */..  int eMode,
15d17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d18 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
15d19 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20 61 6e  PASSIVE, FULL an
15d1a 64 20 52 45 53 54 41 52 54 20 2a 2f 0d 0a 20 20  d RESTART */..  
15d1b 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f 69  int (*xBusy)(voi
15d1c 64 2a 29 2c 20 20 20 20 20 20 20 20 20 20 20 20  d*),            
15d1d 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63  /* Function to c
15d1e 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f  all when busy */
15d1f 0d 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 41  ..  void *pBusyA
15d20 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rg,             
15d21 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 61      /* Context a
15d22 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 42 75 73  rgument for xBus
15d23 79 48 61 6e 64 6c 65 72 20 2a 2f 0d 0a 20 20 69  yHandler */..  i
15d24 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20 20  nt sync_flags,  
15d25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15d26 2a 20 46 6c 61 67 73 20 74 6f 20 73 79 6e 63 20  * Flags to sync 
15d27 64 62 20 66 69 6c 65 20 77 69 74 68 20 28 6f 72  db file with (or
15d28 20 30 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42   0) */..  int nB
15d29 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
15d2a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
15d2b 65 20 6f 66 20 62 75 66 66 65 72 20 6e 42 75 66  e of buffer nBuf
15d2c 20 2a 2f 0d 0a 20 20 75 38 20 2a 7a 42 75 66 2c   */..  u8 *zBuf,
15d2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d2e 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
15d2f 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73  ary buffer to us
15d30 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 4c  e */..  int *pnL
15d31 6f 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  og,             
15d32 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
15d33 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73  Number of frames
15d34 20 69 6e 20 57 41 4c 20 2a 2f 0d 0a 20 20 69 6e   in WAL */..  in
15d35 74 20 2a 70 6e 43 6b 70 74 20 20 20 20 20 20 20  t *pnCkpt       
15d36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15d37 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
15d38 62 61 63 6b 66 69 6c 6c 65 64 20 66 72 61 6d 65  backfilled frame
15d39 73 20 69 6e 20 57 41 4c 20 2a 2f 0d 0a 29 3b 0d  s in WAL */..);.
15d3a 0a 0d 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65  .../* Return the
15d3b 20 76 61 6c 75 65 20 74 6f 20 70 61 73 73 20 74   value to pass t
15d3c 6f 20 61 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f  o a sqlite3_wal_
15d3d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 2c 20 74  hook callback, t
15d3e 68 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  he..** number of
15d3f 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 57   frames in the W
15d40 41 4c 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20  AL at the point 
15d41 6f 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6d 6d  of the last comm
15d42 69 74 20 73 69 6e 63 65 0d 0a 2a 2a 20 73 71 6c  it since..** sql
15d43 69 74 65 33 57 61 6c 43 61 6c 6c 62 61 63 6b 28  ite3WalCallback(
15d44 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 20 49  ) was called.  I
15d45 66 20 6e 6f 20 63 6f 6d 6d 69 74 73 20 68 61 76  f no commits hav
15d46 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65  e occurred since
15d47 0d 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 63 61  ..** the last ca
15d48 6c 6c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ll, then return 
15d49 30 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  0...*/..SQLITE_P
15d4a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15d4b 65 33 57 61 6c 43 61 6c 6c 62 61 63 6b 28 57 61  e3WalCallback(Wa
15d4c 6c 20 2a 70 57 61 6c 29 3b 0d 0a 0d 0a 2f 2a 20  l *pWal);..../* 
15d4d 54 65 6c 6c 20 74 68 65 20 77 61 6c 20 6c 61 79  Tell the wal lay
15d4e 65 72 20 74 68 61 74 20 61 6e 20 45 58 43 4c 55  er that an EXCLU
15d4f 53 49 56 45 20 6c 6f 63 6b 20 68 61 73 20 62 65  SIVE lock has be
15d50 65 6e 20 6f 62 74 61 69 6e 65 64 20 28 6f 72 20  en obtained (or 
15d51 72 65 6c 65 61 73 65 64 29 0d 0a 2a 2a 20 62 79  released)..** by
15d52 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
15d53 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
15d54 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   file...*/..SQLI
15d55 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15d56 71 6c 69 74 65 33 57 61 6c 45 78 63 6c 75 73 69  qlite3WalExclusi
15d57 76 65 4d 6f 64 65 28 57 61 6c 20 2a 70 57 61 6c  veMode(Wal *pWal
15d58 2c 20 69 6e 74 20 6f 70 29 3b 0d 0a 0d 0a 2f 2a  , int op);..../*
15d59 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
15d5a 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
15d5b 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 74 68 65  non-NULL and the
15d5c 20 57 41 4c 20 6d 6f 64 75 6c 65 20 69 73 20 75   WAL module is u
15d5d 73 69 6e 67 0d 0a 2a 2a 20 68 65 61 70 2d 6d 65  sing..** heap-me
15d5e 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 77 61 6c  mory for the wal
15d5f 2d 69 6e 64 65 78 2e 20 4f 74 68 65 72 77 69 73  -index. Otherwis
15d60 65 2c 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  e, if the argume
15d61 6e 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 74 68  nt is NULL or th
15d62 65 0d 0a 2a 2a 20 57 41 4c 20 6d 6f 64 75 6c 65  e..** WAL module
15d63 20 69 73 20 75 73 69 6e 67 20 73 68 61 72 65 64   is using shared
15d64 2d 6d 65 6d 6f 72 79 2c 20 72 65 74 75 72 6e 20  -memory, return 
15d65 66 61 6c 73 65 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c  false. ..*/..SQL
15d66 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15d67 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70 4d 65  sqlite3WalHeapMe
15d68 6d 6f 72 79 28 57 61 6c 20 2a 70 57 61 6c 29 3b  mory(Wal *pWal);
15d69 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  ....#endif /* if
15d6a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15d6b 5f 57 41 4c 20 2a 2f 0d 0a 23 65 6e 64 69 66 20  _WAL */..#endif 
15d6c 2f 2a 20 5f 57 41 4c 5f 48 5f 20 2a 2f 0d 0a 0d  /* _WAL_H_ */...
15d6d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
15d6e 20 45 6e 64 20 6f 66 20 77 61 6c 2e 68 20 2a 2a   End of wal.h **
15d6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
15d72 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
15d73 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
15d74 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
15d75 6e 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a  n pager.c ******
15d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d77 2f 0d 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  /....../********
15d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 4f 54 45  *********** NOTE
15d79 53 20 4f 4e 20 54 48 45 20 44 45 53 49 47 4e 20  S ON THE DESIGN 
15d7a 4f 46 20 54 48 45 20 50 41 47 45 52 20 2a 2a 2a  OF THE PAGER ***
15d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d7c 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
15d7d 69 73 20 63 6f 6d 6d 65 6e 74 20 62 6c 6f 63 6b  is comment block
15d7e 20 64 65 73 63 72 69 62 65 73 20 69 6e 76 61 72   describes invar
15d7f 69 61 6e 74 73 20 74 68 61 74 20 68 6f 6c 64 20  iants that hold 
15d80 77 68 65 6e 20 75 73 69 6e 67 20 61 20 72 6f 6c  when using a rol
15d81 6c 62 61 63 6b 0d 0a 2a 2a 20 6a 6f 75 72 6e 61  lback..** journa
15d82 6c 2e 20 20 54 68 65 73 65 20 69 6e 76 61 72 69  l.  These invari
15d83 61 6e 74 73 20 64 6f 20 6e 6f 74 20 61 70 70 6c  ants do not appl
15d84 79 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  y for journal_mo
15d85 64 65 3d 57 41 4c 2c 0d 0a 2a 2a 20 6a 6f 75 72  de=WAL,..** jour
15d86 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 2c  nal_mode=MEMORY,
15d87 20 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65   or journal_mode
15d88 3d 4f 46 46 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 69  =OFF...**..** Wi
15d89 74 68 69 6e 20 74 68 69 73 20 63 6f 6d 6d 65 6e  thin this commen
15d8a 74 20 62 6c 6f 63 6b 2c 20 61 20 70 61 67 65 20  t block, a page 
15d8b 69 73 20 64 65 65 6d 65 64 20 74 6f 20 68 61 76  is deemed to hav
15d8c 65 20 62 65 65 6e 20 73 79 6e 63 65 64 0d 0a 2a  e been synced..*
15d8d 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
15d8e 61 73 20 73 6f 6f 6e 20 61 73 20 69 74 20 69 73  as soon as it is
15d8f 20 77 72 69 74 74 65 6e 20 77 68 65 6e 20 50 52   written when PR
15d90 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73  AGMA synchronous
15d91 3d 4f 46 46 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77  =OFF...** Otherw
15d92 69 73 65 2c 20 74 68 65 20 70 61 67 65 20 69 73  ise, the page is
15d93 20 6e 6f 74 20 73 79 6e 63 65 64 20 75 6e 74 69   not synced unti
15d94 6c 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68  l the xSync meth
15d95 6f 64 20 6f 66 20 74 68 65 20 56 46 53 0d 0a 2a  od of the VFS..*
15d96 2a 20 69 73 20 63 61 6c 6c 65 64 20 73 75 63 63  * is called succ
15d97 65 73 73 66 75 6c 6c 79 20 6f 6e 20 74 68 65 20  essfully on the 
15d98 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  file containing 
15d99 74 68 65 20 70 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a  the page...**..*
15d9a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 20 41  * Definition:  A
15d9b 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
15d9c 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 73 61  abase file is sa
15d9d 69 64 20 74 6f 20 62 65 20 22 6f 76 65 72 77 72  id to be "overwr
15d9e 69 74 65 61 62 6c 65 22 20 69 66 0d 0a 2a 2a 20  iteable" if..** 
15d9f 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
15da0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
15da1 20 74 72 75 65 20 61 62 6f 75 74 20 74 68 65 20   true about the 
15da2 70 61 67 65 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20  page:..** ..**  
15da3 20 20 20 28 61 29 20 20 54 68 65 20 6f 72 69 67     (a)  The orig
15da4 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20  inal content of 
15da5 74 68 65 20 70 61 67 65 20 61 73 20 69 74 20 77  the page as it w
15da6 61 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  as at the beginn
15da7 69 6e 67 20 6f 66 0d 0a 2a 2a 20 20 20 20 20 20  ing of..**      
15da8 20 20 20 20 74 68 65 20 74 72 61 6e 73 61 63 74      the transact
15da9 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 77 72 69  ion has been wri
15daa 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 72 6f  tten into the ro
15dab 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61  llback journal a
15dac 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nd..**          
15dad 73 79 6e 63 65 64 2e 0d 0a 2a 2a 20 0d 0a 2a 2a  synced...** ..**
15dae 20 20 20 20 20 28 62 29 20 20 54 68 65 20 70 61       (b)  The pa
15daf 67 65 20 77 61 73 20 61 20 66 72 65 65 6c 69 73  ge was a freelis
15db0 74 20 6c 65 61 66 20 70 61 67 65 20 61 74 20 74  t leaf page at t
15db1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
15db2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2a  transaction...**
15db3 20 0d 0a 2a 2a 20 20 20 20 20 28 63 29 20 20 54   ..**     (c)  T
15db4 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
15db5 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
15db6 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20  he largest page 
15db7 74 68 61 74 20 65 78 69 73 74 65 64 20 69 6e 0d  that existed in.
15db8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
15db9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
15dba 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
15dbb 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d  he transaction..
15dbc 0a 2a 2a 20 0d 0a 2a 2a 20 28 31 29 20 41 20 70  .** ..** (1) A p
15dbd 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
15dbe 61 73 65 20 66 69 6c 65 20 69 73 20 6e 65 76 65  ase file is neve
15dbf 72 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e  r overwritten un
15dc0 6c 65 73 73 20 6f 6e 65 20 6f 66 20 74 68 65 0d  less one of the.
15dc1 0a 2a 2a 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e  .**     followin
15dc2 67 20 61 72 65 20 74 72 75 65 3a 0d 0a 2a 2a 20  g are true:..** 
15dc3 0d 0a 2a 2a 20 20 20 20 20 28 61 29 20 54 68 65  ..**     (a) The
15dc4 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 6f 74   page and all ot
15dc5 68 65 72 20 70 61 67 65 73 20 6f 6e 20 74 68 65  her pages on the
15dc6 20 73 61 6d 65 20 73 65 63 74 6f 72 20 61 72 65   same sector are
15dc7 20 6f 76 65 72 77 72 69 74 65 61 62 6c 65 2e 0d   overwriteable..
15dc8 0a 2a 2a 20 0d 0a 2a 2a 20 20 20 20 20 28 62 29  .** ..**     (b)
15dc9 20 54 68 65 20 61 74 6f 6d 69 63 20 70 61 67 65   The atomic page
15dca 20 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74   write optimizat
15dcb 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ion is enabled, 
15dcc 61 6e 64 20 74 68 65 20 65 6e 74 69 72 65 0d 0a  and the entire..
15dcd 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e 73  **         trans
15dce 61 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  action other tha
15dcf 6e 20 74 68 65 20 75 70 64 61 74 65 20 6f 66 20  n the update of 
15dd0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
15dd1 73 65 71 75 65 6e 63 65 0d 0a 2a 2a 20 20 20 20  sequence..**    
15dd2 20 20 20 20 20 6e 75 6d 62 65 72 20 63 6f 6e 73       number cons
15dd3 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
15dd4 20 70 61 67 65 20 63 68 61 6e 67 65 2e 0d 0a 2a   page change...*
15dd5 2a 20 0d 0a 2a 2a 20 28 32 29 20 54 68 65 20 63  * ..** (2) The c
15dd6 6f 6e 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65  ontent of a page
15dd7 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
15dd8 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
15dd9 61 6c 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68  al exactly match
15dda 65 73 0d 0a 2a 2a 20 20 20 20 20 62 6f 74 68 20  es..**     both 
15ddb 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74  the content in t
15ddc 68 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e  he database when
15ddd 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
15dde 75 72 6e 61 6c 20 77 61 73 20 77 72 69 74 74 65  urnal was writte
15ddf 6e 0d 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74 68  n..**     and th
15de0 65 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65  e content in the
15de1 20 64 61 74 61 62 61 73 65 20 61 74 20 74 68 65   database at the
15de2 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
15de3 65 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 20 20  e current..**   
15de4 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a    transaction...
15de5 2a 2a 20 0d 0a 2a 2a 20 28 33 29 20 57 72 69 74  ** ..** (3) Writ
15de6 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
15de7 73 65 20 66 69 6c 65 20 61 72 65 20 61 6e 20 69  se file are an i
15de8 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  nteger multiple 
15de9 6f 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  of the page size
15dea 0d 0a 2a 2a 20 20 20 20 20 69 6e 20 6c 65 6e 67  ..**     in leng
15deb 74 68 20 61 6e 64 20 61 72 65 20 61 6c 69 67 6e  th and are align
15dec 65 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f 75  ed on a page bou
15ded 6e 64 61 72 79 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20  ndary...** ..** 
15dee 28 34 29 20 52 65 61 64 73 20 66 72 6f 6d 20 74  (4) Reads from t
15def 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15df0 20 61 72 65 20 65 69 74 68 65 72 20 61 6c 69 67   are either alig
15df1 6e 65 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f  ned on a page bo
15df2 75 6e 64 61 72 79 20 61 6e 64 0d 0a 2a 2a 20 20  undary and..**  
15df3 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75     an integer mu
15df4 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 20 70 61  ltiple of the pa
15df5 67 65 20 73 69 7a 65 20 69 6e 20 6c 65 6e 67 74  ge size in lengt
15df6 68 20 6f 72 20 61 72 65 20 74 61 6b 65 6e 20 66  h or are taken f
15df7 72 6f 6d 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20  rom the..**     
15df8 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20  first 100 bytes 
15df9 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
15dfa 66 69 6c 65 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 28  file...** ..** (
15dfb 35 29 20 41 6c 6c 20 77 72 69 74 65 73 20 74 6f  5) All writes to
15dfc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15dfd 6c 65 20 61 72 65 20 73 79 6e 63 65 64 20 70 72  le are synced pr
15dfe 69 6f 72 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62  ior to the rollb
15dff 61 63 6b 20 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20  ack journal..** 
15e00 20 20 20 20 62 65 69 6e 67 20 64 65 6c 65 74 65      being delete
15e01 64 2c 20 74 72 75 6e 63 61 74 65 64 2c 20 6f 72  d, truncated, or
15e02 20 7a 65 72 6f 65 64 2e 0d 0a 2a 2a 20 0d 0a 2a   zeroed...** ..*
15e03 2a 20 28 36 29 20 49 66 20 61 20 6d 61 73 74 65  * (6) If a maste
15e04 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  r journal file i
15e05 73 20 75 73 65 64 2c 20 74 68 65 6e 20 61 6c 6c  s used, then all
15e06 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 64   writes to the d
15e07 61 74 61 62 61 73 65 20 66 69 6c 65 0d 0a 2a 2a  atabase file..**
15e08 20 20 20 20 20 61 72 65 20 73 79 6e 63 65 64 20       are synced 
15e09 70 72 69 6f 72 20 74 6f 20 74 68 65 20 6d 61 73  prior to the mas
15e0a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 62 65 69 6e  ter journal bein
15e0b 67 20 64 65 6c 65 74 65 64 2e 0d 0a 2a 2a 20 0d  g deleted...** .
15e0c 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20  .** Definition: 
15e0d 54 77 6f 20 64 61 74 61 62 61 73 65 73 20 28 6f  Two databases (o
15e0e 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
15e0f 61 73 65 20 61 74 20 74 77 6f 20 70 6f 69 6e 74  ase at two point
15e10 73 20 69 74 20 74 69 6d 65 29 0d 0a 2a 2a 20 61  s it time)..** a
15e11 72 65 20 73 61 69 64 20 74 6f 20 62 65 20 22 6c  re said to be "l
15e12 6f 67 69 63 61 6c 6c 79 20 65 71 75 69 76 61 6c  ogically equival
15e13 65 6e 74 22 20 69 66 20 74 68 65 79 20 67 69 76  ent" if they giv
15e14 65 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65  e the same answe
15e15 72 20 74 6f 0d 0a 2a 2a 20 61 6c 6c 20 71 75 65  r to..** all que
15e16 72 69 65 73 2e 20 20 4e 6f 74 65 20 69 6e 20 70  ries.  Note in p
15e17 61 72 74 69 63 75 6c 61 72 20 74 68 65 20 74 68  articular the th
15e18 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 72 65  e content of fre
15e19 65 6c 69 73 74 20 6c 65 61 66 0d 0a 2a 2a 20 70  elist leaf..** p
15e1a 61 67 65 73 20 63 61 6e 20 62 65 20 63 68 61 6e  ages can be chan
15e1b 67 65 64 20 61 72 62 69 74 61 72 69 6c 79 20 77  ged arbitarily w
15e1c 69 74 68 6f 75 74 20 65 66 66 65 63 74 69 6e 67  ithout effecting
15e1d 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 65 71 75   the logical equ
15e1e 69 76 61 6c 65 6e 63 65 0d 0a 2a 2a 20 6f 66 20  ivalence..** of 
15e1f 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a  the database...*
15e20 2a 20 0d 0a 2a 2a 20 28 37 29 20 41 74 20 61 6e  * ..** (7) At an
15e21 79 20 74 69 6d 65 2c 20 69 66 20 61 6e 79 20 73  y time, if any s
15e22 75 62 73 65 74 2c 20 69 6e 63 6c 75 64 69 6e 67  ubset, including
15e23 20 74 68 65 20 65 6d 70 74 79 20 73 65 74 20 61   the empty set a
15e24 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 73 65 74  nd the total set
15e25 2c 0d 0a 2a 2a 20 20 20 20 20 6f 66 20 74 68 65  ,..**     of the
15e26 20 75 6e 73 79 6e 63 65 64 20 63 68 61 6e 67 65   unsynced change
15e27 73 20 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 20  s to a rollback 
15e28 6a 6f 75 72 6e 61 6c 20 61 72 65 20 72 65 6d 6f  journal are remo
15e29 76 65 64 20 61 6e 64 20 74 68 65 20 0d 0a 2a 2a  ved and the ..**
15e2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 69 73 20       journal is 
15e2b 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65  rolled back, the
15e2c 20 72 65 73 75 6c 74 69 6e 67 20 64 61 74 61 62   resulting datab
15e2d 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  ase file will be
15e2e 20 6c 6f 67 69 63 61 6c 0d 0a 2a 2a 20 20 20 20   logical..**    
15e2f 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74   equivalent to t
15e30 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15e31 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
15e32 67 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  g of the transac
15e33 74 69 6f 6e 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 28  tion...** ..** (
15e34 38 29 20 57 68 65 6e 20 61 20 74 72 61 6e 73 61  8) When a transa
15e35 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
15e36 62 61 63 6b 2c 20 74 68 65 20 78 54 72 75 6e 63  back, the xTrunc
15e37 61 74 65 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  ate method of th
15e38 65 20 56 46 53 0d 0a 2a 2a 20 20 20 20 20 69 73  e VFS..**     is
15e39 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 74 6f   called to resto
15e3a 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
15e3b 66 69 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65  file to the same
15e3c 20 73 69 7a 65 20 69 74 20 77 61 73 20 61 74 0d   size it was at.
15e3d 0a 2a 2a 20 20 20 20 20 74 68 65 20 62 65 67 69  .**     the begi
15e3e 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 74 72 61  nning of the tra
15e3f 6e 73 61 63 74 69 6f 6e 2e 20 20 28 49 6e 20 73  nsaction.  (In s
15e40 6f 6d 65 20 56 46 53 65 73 2c 20 74 68 65 20 78  ome VFSes, the x
15e41 54 72 75 6e 63 61 74 65 0d 0a 2a 2a 20 20 20 20  Truncate..**    
15e42 20 6d 65 74 68 6f 64 20 69 73 20 61 20 6e 6f 2d   method is a no-
15e43 6f 70 2c 20 62 75 74 20 74 68 61 74 20 64 6f 65  op, but that doe
15e44 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
15e45 20 66 61 63 74 20 74 68 65 20 53 51 4c 69 74 65   fact the SQLite
15e46 20 77 69 6c 6c 0d 0a 2a 2a 20 20 20 20 20 69 6e   will..**     in
15e47 76 6f 6b 65 20 69 74 2e 29 0d 0a 2a 2a 20 0d 0a  voke it.)..** ..
15e48 2a 2a 20 28 39 29 20 57 68 65 6e 65 76 65 72 20  ** (9) Whenever 
15e49 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
15e4a 65 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 61  e is modified, a
15e4b 74 20 6c 65 61 73 74 20 6f 6e 65 20 62 69 74 20  t least one bit 
15e4c 69 6e 20 74 68 65 20 72 61 6e 67 65 0d 0a 2a 2a  in the range..**
15e4d 20 20 20 20 20 6f 66 20 62 79 74 65 73 20 66 72       of bytes fr
15e4e 6f 6d 20 32 34 20 74 68 72 6f 75 67 68 20 33 39  om 24 through 39
15e4f 20 69 6e 63 6c 75 73 69 76 65 20 77 69 6c 6c 20   inclusive will 
15e50 62 65 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72  be changed prior
15e51 20 74 6f 20 72 65 6c 65 61 73 69 6e 67 0d 0a 2a   to releasing..*
15e52 2a 20 20 20 20 20 74 68 65 20 45 58 43 4c 55 53  *     the EXCLUS
15e53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 75 73 20 73  IVE lock, thus s
15e54 69 67 6e 61 6c 69 6e 67 20 6f 74 68 65 72 20 63  ignaling other c
15e55 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 6e 20 74 68  onnections on th
15e56 65 20 73 61 6d 65 0d 0a 2a 2a 20 20 20 20 20 64  e same..**     d
15e57 61 74 61 62 61 73 65 20 74 6f 20 66 6c 75 73 68  atabase to flush
15e58 20 74 68 65 69 72 20 63 61 63 68 65 73 2e 0d 0a   their caches...
15e59 2a 2a 0d 0a 2a 2a 20 28 31 30 29 20 54 68 65 20  **..** (10) The 
15e5a 70 61 74 74 65 72 6e 20 6f 66 20 62 69 74 73 20  pattern of bits 
15e5b 69 6e 20 62 79 74 65 73 20 32 34 20 74 68 72 6f  in bytes 24 thro
15e5c 75 67 68 20 33 39 20 73 68 61 6c 6c 20 6e 6f 74  ugh 39 shall not
15e5d 20 72 65 70 65 61 74 20 69 6e 20 6c 65 73 73 0d   repeat in less.
15e5e 0a 2a 2a 20 20 20 20 20 20 74 68 61 6e 20 6f 6e  .**      than on
15e5f 65 20 62 69 6c 6c 69 6f 6e 20 74 72 61 6e 73 61  e billion transa
15e60 63 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ctions...**..** 
15e61 28 31 31 29 20 41 20 64 61 74 61 62 61 73 65 20  (11) A database 
15e62 66 69 6c 65 20 69 73 20 77 65 6c 6c 2d 66 6f 72  file is well-for
15e63 6d 65 64 20 61 74 20 74 68 65 20 62 65 67 69 6e  med at the begin
15e64 6e 69 6e 67 20 61 6e 64 20 61 74 20 74 68 65 20  ning and at the 
15e65 63 6f 6e 63 6c 75 73 69 6f 6e 0d 0a 2a 2a 20 20  conclusion..**  
15e66 20 20 20 20 6f 66 20 65 76 65 72 79 20 74 72 61      of every tra
15e67 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  nsaction...**..*
15e68 2a 20 28 31 32 29 20 41 6e 20 45 58 43 4c 55 53  * (12) An EXCLUS
15e69 49 56 45 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  IVE lock is held
15e6a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
15e6b 20 66 69 6c 65 20 77 68 65 6e 20 77 72 69 74 69   file when writi
15e6c 6e 67 20 74 6f 0d 0a 2a 2a 20 20 20 20 20 20 74  ng to..**      t
15e6d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15e6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 28 31 33 29 20 41  ...**..** (13) A
15e6f 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20   SHARED lock is 
15e70 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
15e71 62 61 73 65 20 66 69 6c 65 20 77 68 69 6c 65 20  base file while 
15e72 72 65 61 64 69 6e 67 20 61 6e 79 0d 0a 2a 2a 20  reading any..** 
15e73 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 6f 75 74       content out
15e74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
15e75 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   file...**..****
15e76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f  **********/..../
15e7b 2a 0d 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  *..** Macros for
15e7c 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67   troubleshooting
15e7d 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
15e7e 65 64 20 6f 66 66 0d 0a 2a 2f 0d 0a 23 69 66 20  ed off..*/..#if 
15e7f 30 0d 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61  0..int sqlite3Pa
15e80 67 65 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20  gerTrace=1;  /* 
15e81 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74  True to enable t
15e82 72 61 63 69 6e 67 20 2a 2f 0d 0a 23 64 65 66 69  racing */..#defi
15e83 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  ne sqlite3DebugP
15e84 72 69 6e 74 66 20 70 72 69 6e 74 66 0d 0a 23 64  rintf printf..#d
15e85 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
15e86 28 58 29 20 20 20 20 20 69 66 28 20 73 71 6c 69  (X)     if( sqli
15e87 74 65 33 50 61 67 65 72 54 72 61 63 65 20 29 7b  te3PagerTrace ){
15e88 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
15e89 6e 74 66 20 58 3b 20 7d 0d 0a 23 65 6c 73 65 0d  ntf X; }..#else.
15e8a 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
15e8b 41 43 45 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a  ACE(X)..#endif..
15e8c 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  ../*..** The fol
15e8d 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f  lowing two macro
15e8e 73 20 61 72 65 20 75 73 65 64 20 77 69 74 68 69  s are used withi
15e8f 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 43 45  n the PAGERTRACE
15e90 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 0d  () macros above.
15e91 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 74  .** to print out
15e92 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
15e93 73 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 50 41 47 45  s. ..**..** PAGE
15e94 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f  RID() takes a po
15e95 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72  inter to a Pager
15e96 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61   struct as its a
15e97 72 67 75 6d 65 6e 74 2e 20 54 68 65 0d 0a 2a 2a  rgument. The..**
15e98 20 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65   associated file
15e99 2d 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72  -descriptor is r
15e9a 65 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e  eturned. FILEHAN
15e9b 44 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e  DLEID() takes an
15e9c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0d 0a 2a   sqlite3_file..*
15e9d 2a 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20  * struct as its 
15e9e 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 23  argument...*/..#
15e9f 64 65 66 69 6e 65 20 50 41 47 45 52 49 44 28 70  define PAGERID(p
15ea0 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 64 29 29  ) ((int)(p->fd))
15ea1 0d 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 48 41  ..#define FILEHA
15ea2 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 6e 74  NDLEID(fd) ((int
15ea3 29 66 64 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  )fd)..../*..** T
15ea4 68 65 20 50 61 67 65 72 2e 65 53 74 61 74 65 20  he Pager.eState 
15ea5 76 61 72 69 61 62 6c 65 20 73 74 6f 72 65 73 20  variable stores 
15ea6 74 68 65 20 63 75 72 72 65 6e 74 20 27 73 74 61  the current 'sta
15ea7 74 65 27 20 6f 66 20 61 20 70 61 67 65 72 2e 20  te' of a pager. 
15ea8 41 0d 0a 2a 2a 20 70 61 67 65 72 20 6d 61 79 20  A..** pager may 
15ea9 62 65 20 69 6e 20 61 6e 79 20 6f 6e 65 20 6f 66  be in any one of
15eaa 20 74 68 65 20 73 65 76 65 6e 20 73 74 61 74 65   the seven state
15eab 73 20 73 68 6f 77 6e 20 69 6e 20 74 68 65 20 66  s shown in the f
15eac 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 73 74 61  ollowing..** sta
15ead 74 65 20 64 69 61 67 72 61 6d 2e 0d 0a 2a 2a 0d  te diagram...**.
15eae 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
15eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f                 O
15eb0 50 45 4e 20 3c 2d 2d 2d 2d 2d 2d 2b 2d 2d 2d 2d  PEN <------+----
15eb1 2d 2d 2b 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  --+..**         
15eb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15eb3 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 7c       |         |
15eb4 20 20 20 20 20 20 7c 0d 0a 2a 2a 20 20 20 20 20        |..**     
15eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15eb6 20 20 20 20 20 20 20 20 20 56 20 20 20 20 20 20           V      
15eb7 20 20 20 7c 20 20 20 20 20 20 7c 0d 0a 2a 2a 20     |      |..** 
15eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 2d                +-
15eb9 2d 2d 2d 2d 2d 2d 2d 2d 3e 20 52 45 41 44 45 52  --------> READER
15eba 2d 2d 2d 2d 2d 2d 2d 2b 20 20 20 20 20 20 7c 0d  -------+      |.
15ebb 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
15ebc 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
15ebd 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20   |              
15ebe 20 20 7c 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20    |..**         
15ebf 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20        |         
15ec0 20 20 20 20 20 56 20 20 20 20 20 20 20 20 20 20       V          
15ec1 20 20 20 20 20 20 7c 0d 0a 2a 2a 20 20 20 20 20        |..**     
15ec2 20 20 20 20 20 20 20 20 20 20 7c 3c 2d 2d 2d 2d            |<----
15ec3 2d 2d 2d 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44  ---WRITER_LOCKED
15ec4 2d 2d 2d 2d 2d 2d 3e 20 45 52 52 4f 52 0d 0a 2a  ------> ERROR..*
15ec5 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
15ec6 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  |              |
15ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15ec8 5e 20 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ^  ..**         
15ec9 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20        |         
15eca 20 20 20 20 20 56 20 20 20 20 20 20 20 20 20 20       V          
15ecb 20 20 20 20 20 20 7c 0d 0a 2a 2a 20 20 20 20 20        |..**     
15ecc 20 20 20 20 20 20 20 20 20 20 7c 3c 2d 2d 2d 2d            |<----
15ecd 2d 2d 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f  --WRITER_CACHEMO
15ece 44 2d 2d 2d 2d 2d 2d 2d 2d 3e 7c 0d 0a 2a 2a 20  D-------->|..** 
15ecf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
15ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
15ed1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0d                |.
15ed2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
15ed3 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
15ed4 20 56 20 20 20 20 20 20 20 20 20 20 20 20 20 20   V              
15ed5 20 20 7c 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20    |..**         
15ed6 20 20 20 20 20 20 7c 3c 2d 2d 2d 2d 2d 2d 2d 57        |<-------W
15ed7 52 49 54 45 52 5f 44 42 4d 4f 44 2d 2d 2d 2d 2d  RITER_DBMOD-----
15ed8 2d 2d 2d 2d 2d 3e 7c 0d 0a 2a 2a 20 20 20 20 20  ----->|..**     
15ed9 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20            |     
15eda 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20           |      
15edb 20 20 20 20 20 20 20 20 20 20 7c 0d 0a 2a 2a 20            |..** 
15edc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
15edd 20 20 20 20 20 20 20 20 20 20 20 20 20 56 20 20               V  
15ede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0d                |.
15edf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
15ee0 20 20 2b 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 45 52    +<------WRITER
15ee1 5f 46 49 4e 49 53 48 45 44 2d 2d 2d 2d 2d 2d 2d  _FINISHED-------
15ee2 2d 3e 2b 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  ->+..**..**..** 
15ee3 4c 69 73 74 20 6f 66 20 73 74 61 74 65 20 74 72  List of state tr
15ee4 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
15ee5 65 20 43 20 5b 66 75 6e 63 74 69 6f 6e 5d 20 74  e C [function] t
15ee6 68 61 74 20 70 65 72 66 6f 72 6d 73 20 65 61 63  hat performs eac
15ee7 68 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20 20 4f 50  h:..** ..**   OP
15ee8 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EN              
15ee9 2d 3e 20 52 45 41 44 45 52 20 20 20 20 20 20 20  -> READER       
15eea 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 50         [sqlite3P
15eeb 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 5d 0d  agerSharedLock].
15eec 0a 2a 2a 20 20 20 52 45 41 44 45 52 20 20 20 20  .**   READER    
15eed 20 20 20 20 20 20 20 20 2d 3e 20 4f 50 45 4e 20          -> OPEN 
15eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b                 [
15eef 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 5d 0d 0a 2a  pager_unlock]..*
15ef0 2a 0d 0a 2a 2a 20 20 20 52 45 41 44 45 52 20 20  *..**   READER  
15ef1 20 20 20 20 20 20 20 20 20 20 2d 3e 20 57 52 49            -> WRI
15ef2 54 45 52 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20  TER_LOCKED      
15ef3 20 5b 73 71 6c 69 74 65 33 50 61 67 65 72 42 65   [sqlite3PagerBe
15ef4 67 69 6e 5d 0d 0a 2a 2a 20 20 20 57 52 49 54 45  gin]..**   WRITE
15ef5 52 5f 4c 4f 43 4b 45 44 20 20 20 20 20 2d 3e 20  R_LOCKED     -> 
15ef6 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20  WRITER_CACHEMOD 
15ef7 20 20 20 20 5b 70 61 67 65 72 5f 6f 70 65 6e 5f      [pager_open_
15ef8 6a 6f 75 72 6e 61 6c 5d 0d 0a 2a 2a 20 20 20 57  journal]..**   W
15ef9 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 20  RITER_CACHEMOD  
15efa 20 2d 3e 20 57 52 49 54 45 52 5f 44 42 4d 4f 44   -> WRITER_DBMOD
15efb 20 20 20 20 20 20 20 20 5b 73 79 6e 63 4a 6f 75          [syncJou
15efc 72 6e 61 6c 5d 0d 0a 2a 2a 20 20 20 57 52 49 54  rnal]..**   WRIT
15efd 45 52 5f 44 42 4d 4f 44 20 20 20 20 20 20 2d 3e  ER_DBMOD      ->
15efe 20 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44   WRITER_FINISHED
15eff 20 20 20 20 20 5b 73 71 6c 69 74 65 33 50 61 67       [sqlite3Pag
15f00 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
15f01 5d 0d 0a 2a 2a 20 20 20 57 52 49 54 45 52 5f 2a  ]..**   WRITER_*
15f02 2a 2a 20 20 20 20 20 20 20 20 2d 3e 20 52 45 41  **        -> REA
15f03 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  DER             
15f04 20 5b 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e   [pager_end_tran
15f05 73 61 63 74 69 6f 6e 5d 0d 0a 2a 2a 0d 0a 2a 2a  saction]..**..**
15f06 20 20 20 57 52 49 54 45 52 5f 2a 2a 2a 20 20 20     WRITER_***   
15f07 20 20 20 20 20 2d 3e 20 45 52 52 4f 52 20 20 20       -> ERROR   
15f08 20 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 67              [pag
15f09 65 72 5f 65 72 72 6f 72 5d 0d 0a 2a 2a 20 20 20  er_error]..**   
15f0a 45 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20  ERROR           
15f0b 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20 20 20 20    -> OPEN       
15f0c 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 72 5f           [pager_
15f0d 75 6e 6c 6f 63 6b 5d 0d 0a 2a 2a 20 0d 0a 2a 2a  unlock]..** ..**
15f0e 0d 0a 2a 2a 20 20 4f 50 45 4e 3a 0d 0a 2a 2a 0d  ..**  OPEN:..**.
15f0f 0a 2a 2a 20 20 20 20 54 68 65 20 70 61 67 65 72  .**    The pager
15f10 20 73 74 61 72 74 73 20 75 70 20 69 6e 20 74 68   starts up in th
15f11 69 73 20 73 74 61 74 65 2e 20 4e 6f 74 68 69 6e  is state. Nothin
15f12 67 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  g is guaranteed 
15f13 69 6e 20 74 68 69 73 0d 0a 2a 2a 20 20 20 20 73  in this..**    s
15f14 74 61 74 65 20 2d 20 74 68 65 20 66 69 6c 65 20  tate - the file 
15f15 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  may or may not b
15f16 65 20 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65  e locked and the
15f17 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 69   database size i
15f18 73 0d 0a 2a 2a 20 20 20 20 75 6e 6b 6e 6f 77 6e  s..**    unknown
15f19 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d  . The database m
15f1a 61 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f  ay not be read o
15f1b 72 20 77 72 69 74 74 65 6e 2e 0d 0a 2a 2a 0d 0a  r written...**..
15f1c 2a 2a 20 20 20 20 2a 20 4e 6f 20 72 65 61 64 20  **    * No read 
15f1d 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  or write transac
15f1e 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0d  tion is active..
15f1f 0a 2a 2a 20 20 20 20 2a 20 41 6e 79 20 6c 6f 63  .**    * Any loc
15f20 6b 2c 20 6f 72 20 6e 6f 20 6c 6f 63 6b 20 61 74  k, or no lock at
15f21 20 61 6c 6c 2c 20 6d 61 79 20 62 65 20 68 65 6c   all, may be hel
15f22 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
15f23 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 20 20 20 2a  e file...**    *
15f24 20 54 68 65 20 64 62 53 69 7a 65 2c 20 64 62 4f   The dbSize, dbO
15f25 72 69 67 53 69 7a 65 20 61 6e 64 20 64 62 46 69  rigSize and dbFi
15f26 6c 65 53 69 7a 65 20 76 61 72 69 61 62 6c 65 73  leSize variables
15f27 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 75 73   may not be trus
15f28 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 52 45  ted...**..**  RE
15f29 41 44 45 52 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ADER:..**..**   
15f2a 20 49 6e 20 74 68 69 73 20 73 74 61 74 65 20 61   In this state a
15f2b 6c 6c 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  ll the requireme
15f2c 6e 74 73 20 66 6f 72 20 72 65 61 64 69 6e 67 20  nts for reading 
15f2d 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  the database in 
15f2e 0d 0a 2a 2a 20 20 20 20 72 6f 6c 6c 62 61 63 6b  ..**    rollback
15f2f 20 28 6e 6f 6e 2d 57 41 4c 29 20 6d 6f 64 65 20   (non-WAL) mode 
15f30 61 72 65 20 6d 65 74 2e 20 55 6e 6c 65 73 73 20  are met. Unless 
15f31 74 68 65 20 70 61 67 65 72 20 69 73 20 28 6f 72  the pager is (or
15f32 20 72 65 63 65 6e 74 6c 79 0d 0a 2a 2a 20 20 20   recently..**   
15f33 20 77 61 73 29 20 69 6e 20 65 78 63 6c 75 73 69   was) in exclusi
15f34 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2c  ve-locking mode,
15f35 20 61 20 75 73 65 72 2d 6c 65 76 65 6c 20 72 65   a user-level re
15f36 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ad transaction i
15f37 73 20 0d 0a 2a 2a 20 20 20 20 6f 70 65 6e 2e 20  s ..**    open. 
15f38 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  The database siz
15f39 65 20 69 73 20 6b 6e 6f 77 6e 20 69 6e 20 74 68  e is known in th
15f3a 69 73 20 73 74 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a  is state...**..*
15f3b 2a 20 20 20 20 41 20 63 6f 6e 6e 65 63 74 69 6f  *    A connectio
15f3c 6e 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 6c  n running with l
15f3d 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 6e 6f 72 6d  ocking_mode=norm
15f3e 61 6c 20 65 6e 74 65 72 73 20 74 68 69 73 20 73  al enters this s
15f3f 74 61 74 65 20 77 68 65 6e 0d 0a 2a 2a 20 20 20  tate when..**   
15f40 20 69 74 20 6f 70 65 6e 73 20 61 20 72 65 61 64   it opens a read
15f41 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20  -transaction on 
15f42 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
15f43 20 72 65 74 75 72 6e 73 20 74 6f 20 73 74 61 74   returns to stat
15f44 65 0d 0a 2a 2a 20 20 20 20 4f 50 45 4e 20 61 66  e..**    OPEN af
15f45 74 65 72 20 74 68 65 20 72 65 61 64 2d 74 72 61  ter the read-tra
15f46 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70  nsaction is comp
15f47 6c 65 74 65 64 2e 20 48 6f 77 65 76 65 72 20 61  leted. However a
15f48 20 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20   connection..** 
15f49 20 20 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6c 6f     running in lo
15f4a 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75  cking_mode=exclu
15f4b 73 69 76 65 20 28 69 6e 63 6c 75 64 69 6e 67 20  sive (including 
15f4c 74 65 6d 70 20 64 61 74 61 62 61 73 65 73 29 20  temp databases) 
15f4d 72 65 6d 61 69 6e 73 20 69 6e 0d 0a 2a 2a 20 20  remains in..**  
15f4e 20 20 74 68 69 73 20 73 74 61 74 65 20 65 76 65    this state eve
15f4f 6e 20 61 66 74 65 72 20 74 68 65 20 72 65 61 64  n after the read
15f50 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
15f51 63 6c 6f 73 65 64 2e 20 54 68 65 20 6f 6e 6c 79  closed. The only
15f52 20 77 61 79 0d 0a 2a 2a 20 20 20 20 61 20 6c 6f   way..**    a lo
15f53 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75  cking_mode=exclu
15f54 73 69 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  sive connection 
15f55 63 61 6e 20 74 72 61 6e 73 69 74 69 6f 6e 20 66  can transition f
15f56 72 6f 6d 20 52 45 41 44 45 52 20 74 6f 20 4f 50  rom READER to OP
15f57 45 4e 0d 0a 2a 2a 20 20 20 20 69 73 20 76 69 61  EN..**    is via
15f58 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65   the ERROR state
15f59 20 28 73 65 65 20 62 65 6c 6f 77 29 2e 0d 0a 2a   (see below)...*
15f5a 2a 20 0d 0a 2a 2a 20 20 20 20 2a 20 41 20 72 65  * ..**    * A re
15f5b 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  ad transaction m
15f5c 61 79 20 62 65 20 61 63 74 69 76 65 20 28 62 75  ay be active (bu
15f5d 74 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  t a write-transa
15f5e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 29 2e 0d 0a  ction cannot)...
15f5f 2a 2a 20 20 20 20 2a 20 41 20 53 48 41 52 45 44  **    * A SHARED
15f60 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
15f61 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20   is held on the 
15f62 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
15f63 2a 2a 20 20 20 20 2a 20 54 68 65 20 64 62 53 69  **    * The dbSi
15f64 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  ze variable may 
15f65 62 65 20 74 72 75 73 74 65 64 20 28 65 76 65 6e  be trusted (even
15f66 20 69 66 20 61 20 75 73 65 72 2d 6c 65 76 65 6c   if a user-level
15f67 20 72 65 61 64 20 0d 0a 2a 2a 20 20 20 20 20 20   read ..**      
15f68 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e  transaction is n
15f69 6f 74 20 61 63 74 69 76 65 29 2e 20 54 68 65 20  ot active). The 
15f6a 64 62 4f 72 69 67 53 69 7a 65 20 61 6e 64 20 64  dbOrigSize and d
15f6b 62 46 69 6c 65 53 69 7a 65 20 76 61 72 69 61 62  bFileSize variab
15f6c 6c 65 73 0d 0a 2a 2a 20 20 20 20 20 20 6d 61 79  les..**      may
15f6d 20 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 20   not be trusted 
15f6e 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2e 0d 0a  at this point...
15f6f 2a 2a 20 20 20 20 2a 20 49 66 20 74 68 65 20 64  **    * If the d
15f70 61 74 61 62 61 73 65 20 69 73 20 61 20 57 41 4c  atabase is a WAL
15f71 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
15f72 74 68 65 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69  the WAL connecti
15f73 6f 6e 20 69 73 20 6f 70 65 6e 2e 0d 0a 2a 2a 20  on is open...** 
15f74 20 20 20 2a 20 45 76 65 6e 20 69 66 20 61 20 72     * Even if a r
15f75 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ead-transaction 
15f76 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 69 74 20  is not open, it 
15f77 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 68  is guaranteed th
15f78 61 74 20 0d 0a 2a 2a 20 20 20 20 20 20 74 68 65  at ..**      the
15f79 72 65 20 69 73 20 6e 6f 20 68 6f 74 2d 6a 6f 75  re is no hot-jou
15f7a 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65  rnal in the file
15f7b 2d 73 79 73 74 65 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a  -system...**..**
15f7c 20 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3a    WRITER_LOCKED:
15f7d 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 54 68 65 20  ..**..**    The 
15f7e 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74  pager moves to t
15f7f 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 52  his state from R
15f80 45 41 44 45 52 20 77 68 65 6e 20 61 20 77 72 69  EADER when a wri
15f81 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0d 0a  te-transaction..
15f82 2a 2a 20 20 20 20 69 73 20 66 69 72 73 74 20 6f  **    is first o
15f83 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74  pened on the dat
15f84 61 62 61 73 65 2e 20 49 6e 20 57 52 49 54 45 52  abase. In WRITER
15f85 5f 4c 4f 43 4b 45 44 20 73 74 61 74 65 2c 20 61  _LOCKED state, a
15f86 6c 6c 20 6c 6f 63 6b 73 20 0d 0a 2a 2a 20 20 20  ll locks ..**   
15f87 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 61   required to sta
15f88 72 74 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  rt a write-trans
15f89 61 63 74 69 6f 6e 20 61 72 65 20 68 65 6c 64 2c  action are held,
15f8a 20 62 75 74 20 6e 6f 20 61 63 74 75 61 6c 20 0d   but no actual .
15f8b 0a 2a 2a 20 20 20 20 6d 6f 64 69 66 69 63 61 74  .**    modificat
15f8c 69 6f 6e 73 20 74 6f 20 74 68 65 20 63 61 63 68  ions to the cach
15f8d 65 20 6f 72 20 64 61 74 61 62 61 73 65 20 68 61  e or database ha
15f8e 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 2e 0d  ve taken place..
15f8f 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 49 6e 20 72 6f  .**..**    In ro
15f90 6c 6c 62 61 63 6b 20 6d 6f 64 65 2c 20 61 20 52  llback mode, a R
15f91 45 53 45 52 56 45 44 20 6f 72 20 28 69 66 20 74  ESERVED or (if t
15f92 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
15f93 61 73 20 6f 70 65 6e 65 64 20 77 69 74 68 20 0d  as opened with .
15f94 0a 2a 2a 20 20 20 20 42 45 47 49 4e 20 45 58 43  .**    BEGIN EXC
15f95 4c 55 53 49 56 45 29 20 45 58 43 4c 55 53 49 56  LUSIVE) EXCLUSIV
15f96 45 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  E lock is obtain
15f97 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
15f98 73 65 20 66 69 6c 65 20 77 68 65 6e 0d 0a 2a 2a  se file when..**
15f99 20 20 20 20 6d 6f 76 69 6e 67 20 74 6f 20 74 68      moving to th
15f9a 69 73 20 73 74 61 74 65 2c 20 62 75 74 20 74 68  is state, but th
15f9b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
15f9c 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f  s not written to
15f9d 20 6f 72 20 6f 70 65 6e 65 64 20 0d 0a 2a 2a 20   or opened ..** 
15f9e 20 20 20 74 6f 20 69 6e 20 74 68 69 73 20 73 74     to in this st
15f9f 61 74 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e  ate. If the tran
15fa0 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
15fa1 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62  tted or rolled b
15fa2 61 63 6b 20 77 68 69 6c 65 20 0d 0a 2a 2a 20 20  ack while ..**  
15fa3 20 20 69 6e 20 57 52 49 54 45 52 5f 4c 4f 43 4b    in WRITER_LOCK
15fa4 45 44 20 73 74 61 74 65 2c 20 61 6c 6c 20 74 68  ED state, all th
15fa5 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69  at is required i
15fa6 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20  s to unlock the 
15fa7 64 61 74 61 62 61 73 65 20 0d 0a 2a 2a 20 20 20  database ..**   
15fa8 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20   file...**..**  
15fa9 20 20 49 4e 20 57 41 4c 20 6d 6f 64 65 2c 20 57    IN WAL mode, W
15faa 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e  alBeginWriteTran
15fab 73 61 63 74 69 6f 6e 28 29 20 69 73 20 63 61 6c  saction() is cal
15fac 6c 65 64 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20  led to lock the 
15fad 6c 6f 67 20 66 69 6c 65 2e 0d 0a 2a 2a 20 20 20  log file...**   
15fae 20 49 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   If the connecti
15faf 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 77 69  on is running wi
15fb0 74 68 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  th locking_mode=
15fb1 65 78 63 6c 75 73 69 76 65 2c 20 61 6e 20 61 74  exclusive, an at
15fb2 74 65 6d 70 74 0d 0a 2a 2a 20 20 20 20 69 73 20  tempt..**    is 
15fb3 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61  made to obtain a
15fb4 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
15fb5 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
15fb6 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20   file...**..**  
15fb7 20 20 2a 20 41 20 77 72 69 74 65 20 74 72 61 6e    * A write tran
15fb8 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
15fb9 65 2e 0d 0a 2a 2a 20 20 20 20 2a 20 49 66 20 74  e...**    * If t
15fba 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
15fbb 20 6f 70 65 6e 20 69 6e 20 72 6f 6c 6c 62 61 63   open in rollbac
15fbc 6b 2d 6d 6f 64 65 2c 20 61 20 52 45 53 45 52 56  k-mode, a RESERV
15fbd 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 0d 0a  ED or greater ..
15fbe 2a 2a 20 20 20 20 20 20 6c 6f 63 6b 20 69 73 20  **      lock is 
15fbf 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
15fc0 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 20  base file...**  
15fc1 20 20 2a 20 49 66 20 74 68 65 20 63 6f 6e 6e 65    * If the conne
15fc2 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 69 6e  ction is open in
15fc3 20 57 41 4c 2d 6d 6f 64 65 2c 20 61 20 57 41 4c   WAL-mode, a WAL
15fc4 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
15fc5 6f 6e 0d 0a 2a 2a 20 20 20 20 20 20 69 73 20 6f  on..**      is o
15fc6 70 65 6e 20 28 69 2e 65 2e 20 73 71 6c 69 74 65  pen (i.e. sqlite
15fc7 33 57 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72  3WalBeginWriteTr
15fc8 61 6e 73 61 63 74 69 6f 6e 28 29 20 68 61 73 20  ansaction() has 
15fc9 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c  been successfull
15fca 79 0d 0a 2a 2a 20 20 20 20 20 20 63 61 6c 6c 65  y..**      calle
15fcb 64 29 2e 0d 0a 2a 2a 20 20 20 20 2a 20 54 68 65  d)...**    * The
15fcc 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69 67 53   dbSize, dbOrigS
15fcd 69 7a 65 20 61 6e 64 20 64 62 46 69 6c 65 53 69  ize and dbFileSi
15fce 7a 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ze variables are
15fcf 20 61 6c 6c 20 76 61 6c 69 64 2e 0d 0a 2a 2a 20   all valid...** 
15fd0 20 20 20 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74     * The content
15fd1 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63  s of the pager c
15fd2 61 63 68 65 20 68 61 76 65 20 6e 6f 74 20 62 65  ache have not be
15fd3 65 6e 20 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2a  en modified...**
15fd4 20 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61      * The journa
15fd5 6c 20 66 69 6c 65 20 6d 61 79 20 6f 72 20 6d 61  l file may or ma
15fd6 79 20 6e 6f 74 20 62 65 20 6f 70 65 6e 2e 0d 0a  y not be open...
15fd7 2a 2a 20 20 20 20 2a 20 4e 6f 74 68 69 6e 67 20  **    * Nothing 
15fd8 28 6e 6f 74 20 65 76 65 6e 20 74 68 65 20 66 69  (not even the fi
15fd9 72 73 74 20 68 65 61 64 65 72 29 20 68 61 73 20  rst header) has 
15fda 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20  been written to 
15fdb 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a  the journal...**
15fdc 0d 0a 2a 2a 20 20 57 52 49 54 45 52 5f 43 41 43  ..**  WRITER_CAC
15fdd 48 45 4d 4f 44 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  HEMOD:..**..**  
15fde 20 20 41 20 70 61 67 65 72 20 6d 6f 76 65 73 20    A pager moves 
15fdf 66 72 6f 6d 20 57 52 49 54 45 52 5f 4c 4f 43 4b  from WRITER_LOCK
15fe0 45 44 20 73 74 61 74 65 20 74 6f 20 74 68 69 73  ED state to this
15fe1 20 73 74 61 74 65 20 77 68 65 6e 20 61 20 70 61   state when a pa
15fe2 67 65 20 69 73 0d 0a 2a 2a 20 20 20 20 66 69 72  ge is..**    fir
15fe3 73 74 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74  st modified by t
15fe4 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 2e 20  he upper layer. 
15fe5 49 6e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65  In rollback mode
15fe6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
15fe7 65 0d 0a 2a 2a 20 20 20 20 69 73 20 6f 70 65 6e  e..**    is open
15fe8 65 64 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74  ed (if it is not
15fe9 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 20 61   already open) a
15fea 6e 64 20 61 20 68 65 61 64 65 72 20 77 72 69 74  nd a header writ
15feb 74 65 6e 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 20  ten to the..**  
15fec 20 20 73 74 61 72 74 20 6f 66 20 69 74 2e 20 54    start of it. T
15fed 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15fee 20 6f 6e 20 64 69 73 6b 20 68 61 73 20 6e 6f 74   on disk has not
15fef 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 0d   been modified..
15ff0 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 2a 20 41 20 77  .**..**    * A w
15ff1 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
15ff2 20 69 73 20 61 63 74 69 76 65 2e 0d 0a 2a 2a 20   is active...** 
15ff3 20 20 20 2a 20 41 20 52 45 53 45 52 56 45 44 20     * A RESERVED 
15ff4 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20  or greater lock 
15ff5 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64  is held on the d
15ff6 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a  atabase file...*
15ff7 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e  *    * The journ
15ff8 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20  al file is open 
15ff9 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 68 65  and the first he
15ffa 61 64 65 72 20 68 61 73 20 62 65 65 6e 20 77 72  ader has been wr
15ffb 69 74 74 65 6e 20 0d 0a 2a 2a 20 20 20 20 20 20  itten ..**      
15ffc 74 6f 20 69 74 2c 20 62 75 74 20 74 68 65 20 68  to it, but the h
15ffd 65 61 64 65 72 20 68 61 73 20 6e 6f 74 20 62 65  eader has not be
15ffe 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73  en synced to dis
15fff 6b 2e 0d 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20  k...**    * The 
16000 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
16001 70 61 67 65 20 63 61 63 68 65 20 68 61 76 65 20  page cache have 
16002 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 0d 0a  been modified...
16003 2a 2a 0d 0a 2a 2a 20 20 57 52 49 54 45 52 5f 44  **..**  WRITER_D
16004 42 4d 4f 44 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  BMOD:..**..**   
16005 20 54 68 65 20 70 61 67 65 72 20 74 72 61 6e 73   The pager trans
16006 69 74 69 6f 6e 73 20 66 72 6f 6d 20 57 52 49 54  itions from WRIT
16007 45 52 5f 43 41 43 48 45 4d 4f 44 20 69 6e 74 6f  ER_CACHEMOD into
16008 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 73 74   WRITER_DBMOD st
16009 61 74 65 0d 0a 2a 2a 20 20 20 20 77 68 65 6e 20  ate..**    when 
1600a 69 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  it modifies the 
1600b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
1600c 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 57  database file. W
1600d 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0d 0a  AL connections..
1600e 2a 2a 20 20 20 20 6e 65 76 65 72 20 65 6e 74 65  **    never ente
1600f 72 20 74 68 69 73 20 73 74 61 74 65 20 28 73 69  r this state (si
16010 6e 63 65 20 74 68 65 79 20 64 6f 20 6e 6f 74 20  nce they do not 
16011 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
16012 61 73 65 20 66 69 6c 65 2c 0d 0a 2a 2a 20 20 20  ase file,..**   
16013 20 6a 75 73 74 20 74 68 65 20 6c 6f 67 20 66 69   just the log fi
16014 6c 65 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  le)...**..**    
16015 2a 20 41 20 77 72 69 74 65 20 74 72 61 6e 73 61  * A write transa
16016 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e  ction is active.
16017 0d 0a 2a 2a 20 20 20 20 2a 20 41 6e 20 45 58 43  ..**    * An EXC
16018 4c 55 53 49 56 45 20 6f 72 20 67 72 65 61 74 65  LUSIVE or greate
16019 72 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f  r lock is held o
1601a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1601b 69 6c 65 2e 0d 0a 2a 2a 20 20 20 20 2a 20 54 68  ile...**    * Th
1601c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
1601d 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 66  s open and the f
1601e 69 72 73 74 20 68 65 61 64 65 72 20 68 61 73 20  irst header has 
1601f 62 65 65 6e 20 77 72 69 74 74 65 6e 20 0d 0a 2a  been written ..*
16020 2a 20 20 20 20 20 20 61 6e 64 20 73 79 6e 63 65  *      and synce
16021 64 20 74 6f 20 64 69 73 6b 2e 0d 0a 2a 2a 20 20  d to disk...**  
16022 20 20 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73    * The contents
16023 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63   of the page cac
16024 68 65 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64  he have been mod
16025 69 66 69 65 64 20 28 61 6e 64 20 70 6f 73 73 69  ified (and possi
16026 62 6c 79 0d 0a 2a 2a 20 20 20 20 20 20 77 72 69  bly..**      wri
16027 74 74 65 6e 20 74 6f 20 64 69 73 6b 29 2e 0d 0a  tten to disk)...
16028 2a 2a 0d 0a 2a 2a 20 20 57 52 49 54 45 52 5f 46  **..**  WRITER_F
16029 49 4e 49 53 48 45 44 3a 0d 0a 2a 2a 0d 0a 2a 2a  INISHED:..**..**
1602a 20 20 20 20 49 74 20 69 73 20 6e 6f 74 20 70 6f      It is not po
1602b 73 73 69 62 6c 65 20 66 6f 72 20 61 20 57 41 4c  ssible for a WAL
1602c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 65   connection to e
1602d 6e 74 65 72 20 74 68 69 73 20 73 74 61 74 65 2e  nter this state.
1602e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 41 20 72 6f  ..**..**    A ro
1602f 6c 6c 62 61 63 6b 2d 6d 6f 64 65 20 70 61 67 65  llback-mode page
16030 72 20 63 68 61 6e 67 65 73 20 74 6f 20 57 52 49  r changes to WRI
16031 54 45 52 5f 46 49 4e 49 53 48 45 44 20 73 74 61  TER_FINISHED sta
16032 74 65 20 66 72 6f 6d 20 57 52 49 54 45 52 5f 44  te from WRITER_D
16033 42 4d 4f 44 0d 0a 2a 2a 20 20 20 20 73 74 61 74  BMOD..**    stat
16034 65 20 61 66 74 65 72 20 74 68 65 20 65 6e 74 69  e after the enti
16035 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  re transaction h
16036 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 66  as been successf
16037 75 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74  ully written int
16038 6f 20 74 68 65 0d 0a 2a 2a 20 20 20 20 64 61 74  o the..**    dat
16039 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 74  abase file. In t
1603a 68 69 73 20 73 74 61 74 65 20 74 68 65 20 74 72  his state the tr
1603b 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62 65  ansaction may be
1603c 20 63 6f 6d 6d 69 74 74 65 64 20 73 69 6d 70 6c   committed simpl
1603d 79 0d 0a 2a 2a 20 20 20 20 62 79 20 66 69 6e 61  y..**    by fina
1603e 6c 69 7a 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  lizing the journ
1603f 61 6c 20 66 69 6c 65 2e 20 4f 6e 63 65 20 69 6e  al file. Once in
16040 20 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44   WRITER_FINISHED
16041 20 73 74 61 74 65 2c 20 69 74 20 69 73 20 0d 0a   state, it is ..
16042 2a 2a 20 20 20 20 6e 6f 74 20 70 6f 73 73 69 62  **    not possib
16043 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65  le to modify the
16044 20 64 61 74 61 62 61 73 65 20 66 75 72 74 68 65   database furthe
16045 72 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  r. At this point
16046 2c 20 74 68 65 20 75 70 70 65 72 20 0d 0a 2a 2a  , the upper ..**
16047 20 20 20 20 6c 61 79 65 72 20 6d 75 73 74 20 65      layer must e
16048 69 74 68 65 72 20 63 6f 6d 6d 69 74 20 6f 72 20  ither commit or 
16049 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
1604a 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  nsaction...**..*
1604b 2a 20 20 20 20 2a 20 41 20 77 72 69 74 65 20 74  *    * A write t
1604c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
1604d 74 69 76 65 2e 0d 0a 2a 2a 20 20 20 20 2a 20 41  tive...**    * A
1604e 6e 20 45 58 43 4c 55 53 49 56 45 20 6f 72 20 67  n EXCLUSIVE or g
1604f 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 68  reater lock is h
16050 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  eld on the datab
16051 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 20 20  ase file...**   
16052 20 2a 20 41 6c 6c 20 77 72 69 74 69 6e 67 20 61   * All writing a
16053 6e 64 20 73 79 6e 63 69 6e 67 20 6f 66 20 6a 6f  nd syncing of jo
16054 75 72 6e 61 6c 20 61 6e 64 20 64 61 74 61 62 61  urnal and databa
16055 73 65 20 64 61 74 61 20 68 61 73 20 66 69 6e 69  se data has fini
16056 73 68 65 64 2e 0d 0a 2a 2a 20 20 20 20 20 20 49  shed...**      I
16057 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
16058 65 64 2c 20 61 6c 6c 20 74 68 61 74 20 72 65 6d  ed, all that rem
16059 61 69 6e 73 20 69 73 20 74 6f 20 66 69 6e 61 6c  ains is to final
1605a 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
1605b 74 6f 0d 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 6d  to..**      comm
1605c 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  it the transacti
1605d 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  on. If an error 
1605e 64 69 64 20 6f 63 63 75 72 2c 20 74 68 65 20 63  did occur, the c
1605f 61 6c 6c 65 72 20 77 69 6c 6c 20 6e 65 65 64 0d  aller will need.
16060 0a 2a 2a 20 20 20 20 20 20 74 6f 20 72 6f 6c 6c  .**      to roll
16061 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63  back the transac
16062 74 69 6f 6e 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20  tion. ..**..**  
16063 45 52 52 4f 52 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ERROR:..**..**  
16064 20 20 54 68 65 20 45 52 52 4f 52 20 73 74 61 74    The ERROR stat
16065 65 20 69 73 20 65 6e 74 65 72 65 64 20 77 68 65  e is entered whe
16066 6e 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 2d  n an IO or disk-
16067 66 75 6c 6c 20 65 72 72 6f 72 20 28 69 6e 63 6c  full error (incl
16068 75 64 69 6e 67 0d 0a 2a 2a 20 20 20 20 53 51 4c  uding..**    SQL
16069 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29  ITE_IOERR_NOMEM)
1606a 20 6f 63 63 75 72 73 20 61 74 20 61 20 70 6f 69   occurs at a poi
1606b 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 74  nt in the code t
1606c 68 61 74 20 6d 61 6b 65 73 20 69 74 20 0d 0a 2a  hat makes it ..*
1606d 2a 20 20 20 20 64 69 66 66 69 63 75 6c 74 20 74  *    difficult t
1606e 6f 20 62 65 20 73 75 72 65 20 74 68 61 74 20 74  o be sure that t
1606f 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  he in-memory pag
16070 65 72 20 73 74 61 74 65 20 28 63 61 63 68 65 20  er state (cache 
16071 63 6f 6e 74 65 6e 74 73 2c 20 0d 0a 2a 2a 20 20  contents, ..**  
16072 20 20 64 62 20 73 69 7a 65 20 65 74 63 2e 29 20    db size etc.) 
16073 61 72 65 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  are consistent w
16074 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ith the contents
16075 20 6f 66 20 74 68 65 20 66 69 6c 65 2d 73 79 73   of the file-sys
16076 74 65 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  tem...**..**    
16077 54 65 6d 70 6f 72 61 72 79 20 70 61 67 65 72 20  Temporary pager 
16078 66 69 6c 65 73 20 6d 61 79 20 65 6e 74 65 72 20  files may enter 
16079 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c  the ERROR state,
1607a 20 62 75 74 20 69 6e 2d 6d 65 6d 6f 72 79 20 70   but in-memory p
1607b 61 67 65 72 73 0d 0a 2a 2a 20 20 20 20 63 61 6e  agers..**    can
1607c 6e 6f 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  not...**..**    
1607d 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
1607e 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
1607f 72 73 20 77 68 69 6c 65 20 70 65 72 66 6f 72 6d  rs while perform
16080 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20  ing a rollback, 
16081 0d 0a 2a 2a 20 20 20 20 74 68 65 20 63 6f 6e 74  ..**    the cont
16082 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
16083 2d 63 61 63 68 65 20 6d 61 79 20 62 65 20 6c 65  -cache may be le
16084 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
16085 73 74 65 6e 74 20 73 74 61 74 65 2e 0d 0a 2a 2a  stent state...**
16086 20 20 20 20 41 74 20 74 68 69 73 20 70 6f 69 6e      At this poin
16087 74 20 69 74 20 77 6f 75 6c 64 20 62 65 20 64 61  t it would be da
16088 6e 67 65 72 6f 75 73 20 74 6f 20 63 68 61 6e 67  ngerous to chang
16089 65 20 62 61 63 6b 20 74 6f 20 52 45 41 44 45 52  e back to READER
1608a 20 73 74 61 74 65 0d 0a 2a 2a 20 20 20 20 28 61   state..**    (a
1608b 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e  s usually happen
1608c 73 20 61 66 74 65 72 20 61 20 72 6f 6c 6c 62 61  s after a rollba
1608d 63 6b 29 2e 20 41 6e 79 20 73 75 62 73 65 71 75  ck). Any subsequ
1608e 65 6e 74 20 72 65 61 64 65 72 73 20 6d 69 67 68  ent readers migh
1608f 74 0d 0a 2a 2a 20 20 20 20 72 65 70 6f 72 74 20  t..**    report 
16090 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
16091 69 6f 6e 20 28 64 75 65 20 74 6f 20 74 68 65 20  ion (due to the 
16092 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 63 61 63  inconsistent cac
16093 68 65 29 2c 20 61 6e 64 20 69 66 0d 0a 2a 2a 20  he), and if..** 
16094 20 20 20 74 68 65 79 20 75 70 67 72 61 64 65 20     they upgrade 
16095 74 6f 20 77 72 69 74 65 72 73 2c 20 74 68 65 79  to writers, they
16096 20 6d 61 79 20 69 6e 61 64 76 65 72 74 65 6e 74   may inadvertent
16097 6c 79 20 63 6f 72 72 75 70 74 20 74 68 65 20 64  ly corrupt the d
16098 61 74 61 62 61 73 65 0d 0a 2a 2a 20 20 20 20 66  atabase..**    f
16099 69 6c 65 2e 20 54 6f 20 61 76 6f 69 64 20 74 68  ile. To avoid th
1609a 69 73 20 68 61 7a 61 72 64 2c 20 74 68 65 20 70  is hazard, the p
1609b 61 67 65 72 20 73 77 69 74 63 68 65 73 20 69 6e  ager switches in
1609c 74 6f 20 74 68 65 20 45 52 52 4f 52 20 73 74 61  to the ERROR sta
1609d 74 65 0d 0a 2a 2a 20 20 20 20 69 6e 73 74 65 61  te..**    instea
1609e 64 20 6f 66 20 52 45 41 44 45 52 20 66 6f 6c 6c  d of READER foll
1609f 6f 77 69 6e 67 20 73 75 63 68 20 61 6e 20 65 72  owing such an er
160a0 72 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ror...**..**    
160a1 4f 6e 63 65 20 69 74 20 68 61 73 20 65 6e 74 65  Once it has ente
160a2 72 65 64 20 74 68 65 20 45 52 52 4f 52 20 73 74  red the ERROR st
160a3 61 74 65 2c 20 61 6e 79 20 61 74 74 65 6d 70 74  ate, any attempt
160a4 20 74 6f 20 75 73 65 20 74 68 65 20 70 61 67 65   to use the page
160a5 72 0d 0a 2a 2a 20 20 20 20 74 6f 20 72 65 61 64  r..**    to read
160a6 20 6f 72 20 77 72 69 74 65 20 64 61 74 61 20 72   or write data r
160a7 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e  eturns an error.
160a8 20 45 76 65 6e 74 75 61 6c 6c 79 2c 20 6f 6e 63   Eventually, onc
160a9 65 20 61 6c 6c 20 0d 0a 2a 2a 20 20 20 20 6f 75  e all ..**    ou
160aa 74 73 74 61 6e 64 69 6e 67 20 74 72 61 6e 73 61  tstanding transa
160ab 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e  ctions have been
160ac 20 61 62 61 6e 64 6f 6e 65 64 2c 20 74 68 65 20   abandoned, the 
160ad 70 61 67 65 72 20 69 73 20 61 62 6c 65 20 74 6f  pager is able to
160ae 0d 0a 2a 2a 20 20 20 20 74 72 61 6e 73 69 74 69  ..**    transiti
160af 6f 6e 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20  on back to OPEN 
160b0 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 69 6e  state, discardin
160b1 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
160b2 66 20 74 68 65 20 0d 0a 2a 2a 20 20 20 20 70 61  f the ..**    pa
160b3 67 65 2d 63 61 63 68 65 20 61 6e 64 20 61 6e 79  ge-cache and any
160b4 20 6f 74 68 65 72 20 69 6e 2d 6d 65 6d 6f 72 79   other in-memory
160b5 20 73 74 61 74 65 20 61 74 20 74 68 65 20 73 61   state at the sa
160b6 6d 65 20 74 69 6d 65 2e 20 45 76 65 72 79 74 68  me time. Everyth
160b7 69 6e 67 0d 0a 2a 2a 20 20 20 20 69 73 20 72 65  ing..**    is re
160b8 6c 6f 61 64 65 64 20 66 72 6f 6d 20 64 69 73 6b  loaded from disk
160b9 20 28 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73   (and, if necess
160ba 61 72 79 2c 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ary, hot-journal
160bb 20 72 6f 6c 6c 62 61 63 6b 20 70 65 66 6f 72 6d   rollback peform
160bc 65 64 29 0d 0a 2a 2a 20 20 20 20 77 68 65 6e 20  ed)..**    when 
160bd 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
160be 6f 6e 20 69 73 20 6e 65 78 74 20 6f 70 65 6e 65  on is next opene
160bf 64 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28  d on the pager (
160c0 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 0d 0a 2a  transitioning..*
160c1 2a 20 20 20 20 74 68 65 20 70 61 67 65 72 20 69  *    the pager i
160c2 6e 74 6f 20 52 45 41 44 45 52 20 73 74 61 74 65  nto READER state
160c3 29 2e 20 41 74 20 74 68 61 74 20 70 6f 69 6e 74  ). At that point
160c4 20 74 68 65 20 73 79 73 74 65 6d 20 68 61 73 20   the system has 
160c5 72 65 63 6f 76 65 72 65 64 20 0d 0a 2a 2a 20 20  recovered ..**  
160c6 20 20 66 72 6f 6d 20 74 68 65 20 65 72 72 6f 72    from the error
160c7 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 53 70 65  ...**..**    Spe
160c8 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 65 20 70  cifically, the p
160c9 61 67 65 72 20 6a 75 6d 70 73 20 69 6e 74 6f 20  ager jumps into 
160ca 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 20  the ERROR state 
160cb 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  if:..**..**     
160cc 20 31 2e 20 41 6e 20 65 72 72 6f 72 20 6f 63 63   1. An error occ
160cd 75 72 73 20 77 68 69 6c 65 20 61 74 74 65 6d 70  urs while attemp
160ce 74 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e  ting a rollback.
160cf 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e   This happens in
160d0 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 75 6e  ..**         fun
160d1 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 50 61 67  ction sqlite3Pag
160d2 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0d 0a 2a  erRollback()...*
160d3 2a 0d 0a 2a 2a 20 20 20 20 20 20 32 2e 20 41 6e  *..**      2. An
160d4 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
160d5 69 6c 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74  ile attempting t
160d6 6f 20 66 69 6e 61 6c 69 7a 65 20 61 20 6a 6f 75  o finalize a jou
160d7 72 6e 61 6c 20 66 69 6c 65 0d 0a 2a 2a 20 20 20  rnal file..**   
160d8 20 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 20        following 
160d9 61 20 63 6f 6d 6d 69 74 20 69 6e 20 66 75 6e 63  a commit in func
160da 74 69 6f 6e 20 73 71 6c 69 74 65 33 50 61 67 65  tion sqlite3Page
160db 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
160dc 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  )...**..**      
160dd 33 2e 20 41 6e 20 65 72 72 6f 72 20 6f 63 63 75  3. An error occu
160de 72 73 20 77 68 69 6c 65 20 61 74 74 65 6d 70 74  rs while attempt
160df 69 6e 67 20 74 6f 20 77 72 69 74 65 20 74 6f 20  ing to write to 
160e0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 0d 0a  the journal or..
160e1 2a 2a 20 20 20 20 20 20 20 20 20 64 61 74 61 62  **         datab
160e2 61 73 65 20 66 69 6c 65 20 69 6e 20 66 75 6e 63  ase file in func
160e3 74 69 6f 6e 20 70 61 67 65 72 53 74 72 65 73 73  tion pagerStress
160e4 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66  () in order to f
160e5 72 65 65 20 75 70 0d 0a 2a 2a 20 20 20 20 20 20  ree up..**      
160e6 20 20 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 0d 0a     memory...**..
160e7 2a 2a 20 20 20 20 49 6e 20 6f 74 68 65 72 20 63  **    In other c
160e8 61 73 65 73 2c 20 74 68 65 20 65 72 72 6f 72 20  ases, the error 
160e9 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
160ea 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2e  he b-tree layer.
160eb 20 54 68 65 20 62 2d 74 72 65 65 0d 0a 2a 2a 20   The b-tree..** 
160ec 20 20 20 6c 61 79 65 72 20 74 68 65 6e 20 61 74     layer then at
160ed 74 65 6d 70 74 73 20 61 20 72 6f 6c 6c 62 61 63  tempts a rollbac
160ee 6b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20  k operation. If 
160ef 74 68 65 20 65 72 72 6f 72 20 63 6f 6e 64 69 74  the error condit
160f0 69 6f 6e 20 0d 0a 2a 2a 20 20 20 20 70 65 72 73  ion ..**    pers
160f1 69 73 74 73 2c 20 74 68 65 20 70 61 67 65 72 20  ists, the pager 
160f2 65 6e 74 65 72 73 20 74 68 65 20 45 52 52 4f 52  enters the ERROR
160f3 20 73 74 61 74 65 20 76 69 61 20 63 6f 6e 64 69   state via condi
160f4 74 69 6f 6e 20 28 31 29 20 61 62 6f 76 65 2e 0d  tion (1) above..
160f5 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 43 6f 6e 64 69  .**..**    Condi
160f6 74 69 6f 6e 20 28 33 29 20 69 73 20 6e 65 63 65  tion (3) is nece
160f7 73 73 61 72 79 20 62 65 63 61 75 73 65 20 69 74  ssary because it
160f8 20 63 61 6e 20 62 65 20 74 72 69 67 67 65 72 65   can be triggere
160f9 64 20 62 79 20 61 20 72 65 61 64 2d 6f 6e 6c 79  d by a read-only
160fa 0d 0a 2a 2a 20 20 20 20 73 74 61 74 65 6d 65 6e  ..**    statemen
160fb 74 20 65 78 65 63 75 74 65 64 20 77 69 74 68 69  t executed withi
160fc 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  n a transaction.
160fd 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 69   In this case, i
160fe 66 20 74 68 65 20 65 72 72 6f 72 0d 0a 2a 2a 20  f the error..** 
160ff 20 20 20 63 6f 64 65 20 77 65 72 65 20 73 69 6d     code were sim
16100 70 6c 79 20 72 65 74 75 72 6e 65 64 20 74 6f 20  ply returned to 
16101 74 68 65 20 75 73 65 72 2c 20 74 68 65 20 62 2d  the user, the b-
16102 74 72 65 65 20 6c 61 79 65 72 20 77 6f 75 6c 64  tree layer would
16103 20 6e 6f 74 0d 0a 2a 2a 20 20 20 20 61 75 74 6f   not..**    auto
16104 6d 61 74 69 63 61 6c 6c 79 20 61 74 74 65 6d 70  matically attemp
16105 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73  t a rollback, as
16106 20 69 74 20 61 73 73 75 6d 65 73 20 74 68 61 74   it assumes that
16107 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 0d 0a   an error in a..
16108 2a 2a 20 20 20 20 72 65 61 64 2d 6f 6e 6c 79 20  **    read-only 
16109 73 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f 74  statement cannot
1610a 20 6c 65 61 76 65 20 74 68 65 20 70 61 67 65 72   leave the pager
1610b 20 69 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 6c   in an internall
1610c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 0d  y inconsistent .
1610d 0a 2a 2a 20 20 20 20 73 74 61 74 65 2e 0d 0a 2a  .**    state...*
1610e 2a 0d 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 50  *..**    * The P
1610f 61 67 65 72 2e 65 72 72 43 6f 64 65 20 76 61 72  ager.errCode var
16110 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
16111 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
16112 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d  than SQLITE_OK..
16113 0a 2a 2a 20 20 20 20 2a 20 54 68 65 72 65 20 61  .**    * There a
16114 72 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  re one or more o
16115 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
16116 65 6e 63 65 73 20 74 6f 20 70 61 67 65 73 20 28  ences to pages (
16117 61 66 74 65 72 20 74 68 65 0d 0a 2a 2a 20 20 20  after the..**   
16118 20 20 20 6c 61 73 74 20 72 65 66 65 72 65 6e 63     last referenc
16119 65 20 69 73 20 64 72 6f 70 70 65 64 20 74 68 65  e is dropped the
1611a 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20 6d 6f   pager should mo
1611b 76 65 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20  ve back to OPEN 
1611c 73 74 61 74 65 29 2e 0d 0a 2a 2a 20 20 20 20 2a  state)...**    *
1611d 20 54 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f   The pager is no
1611e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  t an in-memory p
1611f 61 67 65 72 2e 0d 0a 2a 2a 20 20 20 20 0d 0a 2a  ager...**    ..*
16120 2a 0d 0a 2a 2a 20 4e 6f 74 65 73 3a 0d 0a 2a 2a  *..** Notes:..**
16121 0d 0a 2a 2a 20 20 20 2a 20 41 20 70 61 67 65 72  ..**   * A pager
16122 20 69 73 20 6e 65 76 65 72 20 69 6e 20 57 52 49   is never in WRI
16123 54 45 52 5f 44 42 4d 4f 44 20 6f 72 20 57 52 49  TER_DBMOD or WRI
16124 54 45 52 5f 46 49 4e 49 53 48 45 44 20 73 74 61  TER_FINISHED sta
16125 74 65 20 69 66 20 74 68 65 0d 0a 2a 2a 20 20 20  te if the..**   
16126 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20    connection is 
16127 6f 70 65 6e 20 69 6e 20 57 41 4c 20 6d 6f 64 65  open in WAL mode
16128 2e 20 41 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69  . A WAL connecti
16129 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20  on is always in 
1612a 6f 6e 65 0d 0a 2a 2a 20 20 20 20 20 6f 66 20 74  one..**     of t
1612b 68 65 20 66 69 72 73 74 20 66 6f 75 72 20 73 74  he first four st
1612c 61 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ates...**..**   
1612d 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 61 20 63 6f  * Normally, a co
1612e 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 20 69 6e  nnection open in
1612f 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20   exclusive mode 
16130 69 73 20 6e 65 76 65 72 20 69 6e 20 50 41 47 45  is never in PAGE
16131 52 5f 4f 50 45 4e 0d 0a 2a 2a 20 20 20 20 20 73  R_OPEN..**     s
16132 74 61 74 65 2e 20 54 68 65 72 65 20 61 72 65 20  tate. There are 
16133 74 77 6f 20 65 78 63 65 70 74 69 6f 6e 73 3a 20  two exceptions: 
16134 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
16135 72 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65  r exclusive-mode
16136 20 68 61 73 0d 0a 2a 2a 20 20 20 20 20 62 65 65   has..**     bee
16137 6e 20 74 75 72 6e 65 64 20 6f 6e 20 28 61 6e 64  n turned on (and
16138 20 62 65 66 6f 72 65 20 61 6e 79 20 72 65 61 64   before any read
16139 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
1613a 63 74 69 6f 6e 73 20 61 72 65 20 0d 0a 2a 2a 20  ctions are ..** 
1613b 20 20 20 20 65 78 65 63 75 74 65 64 29 2c 20 61      executed), a
1613c 6e 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65  nd when the page
1613d 72 20 69 73 20 6c 65 61 76 69 6e 67 20 74 68 65  r is leaving the
1613e 20 22 65 72 72 6f 72 20 73 74 61 74 65 22 2e 0d   "error state"..
1613f 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 53 65 65 20  .**..**   * See 
16140 61 6c 73 6f 3a 20 61 73 73 65 72 74 5f 70 61 67  also: assert_pag
16141 65 72 5f 73 74 61 74 65 28 29 2e 0d 0a 2a 2f 0d  er_state()...*/.
16142 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4f  .#define PAGER_O
16143 50 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  PEN             
16144 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e 65 20       0..#define 
16145 50 41 47 45 52 5f 52 45 41 44 45 52 20 20 20 20  PAGER_READER    
16146 20 20 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23              1..#
16147 64 65 66 69 6e 65 20 50 41 47 45 52 5f 57 52 49  define PAGER_WRI
16148 54 45 52 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20  TER_LOCKED      
16149 20 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 50 41     2..#define PA
1614a 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45  GER_WRITER_CACHE
1614b 4d 4f 44 20 20 20 20 20 20 20 33 0d 0a 23 64 65  MOD       3..#de
1614c 66 69 6e 65 20 50 41 47 45 52 5f 57 52 49 54 45  fine PAGER_WRITE
1614d 52 5f 44 42 4d 4f 44 20 20 20 20 20 20 20 20 20  R_DBMOD         
1614e 20 34 0d 0a 23 64 65 66 69 6e 65 20 50 41 47 45   4..#define PAGE
1614f 52 5f 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45  R_WRITER_FINISHE
16150 44 20 20 20 20 20 20 20 35 0d 0a 23 64 65 66 69  D       5..#defi
16151 6e 65 20 50 41 47 45 52 5f 45 52 52 4f 52 20 20  ne PAGER_ERROR  
16152 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
16153 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 50  ..../*..** The P
16154 61 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 61  ager.eLock varia
16155 62 6c 65 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c  ble is almost al
16156 77 61 79 73 20 73 65 74 20 74 6f 20 6f 6e 65 20  ways set to one 
16157 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 66 6f 6c 6c  of the ..** foll
16158 6f 77 69 6e 67 20 6c 6f 63 6b 69 6e 67 2d 73 74  owing locking-st
16159 61 74 65 73 2c 20 61 63 63 6f 72 64 69 6e 67 20  ates, according 
1615a 74 6f 20 74 68 65 20 6c 6f 63 6b 20 63 75 72 72  to the lock curr
1615b 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 0d 0a 2a  ently held on..*
1615c 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
1615d 69 6c 65 3a 20 4e 4f 5f 4c 4f 43 4b 2c 20 53 48  ile: NO_LOCK, SH
1615e 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52  ARED_LOCK, RESER
1615f 56 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 43 4c  VED_LOCK or EXCL
16160 55 53 49 56 45 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 20  USIVE_LOCK...** 
16161 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73  This variable is
16162 20 6b 65 70 74 20 75 70 20 74 6f 20 64 61 74 65   kept up to date
16163 20 61 73 20 6c 6f 63 6b 73 20 61 72 65 20 74 61   as locks are ta
16164 6b 65 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 64  ken and released
16165 20 62 79 0d 0a 2a 2a 20 74 68 65 20 70 61 67 65   by..** the page
16166 72 4c 6f 63 6b 44 62 28 29 20 61 6e 64 20 70 61  rLockDb() and pa
16167 67 65 72 55 6e 6c 6f 63 6b 44 62 28 29 20 77 72  gerUnlockDb() wr
16168 61 70 70 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  appers...**..** 
16169 49 66 20 74 68 65 20 56 46 53 20 78 4c 6f 63 6b  If the VFS xLock
1616a 28 29 20 6f 72 20 78 55 6e 6c 6f 63 6b 28 29 20  () or xUnlock() 
1616b 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
1616c 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
1616d 54 45 5f 42 55 53 59 0d 0a 2a 2a 20 28 69 2e 65  TE_BUSY..** (i.e
1616e 2e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c  . one of the SQL
1616f 49 54 45 5f 49 4f 45 52 52 20 73 75 62 74 79 70  ITE_IOERR subtyp
16170 65 73 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 63  es), it is not c
16171 6c 65 61 72 20 77 68 65 74 68 65 72 20 6f 72 20  lear whether or 
16172 6e 6f 74 0d 0a 2a 2a 20 74 68 65 20 6f 70 65 72  not..** the oper
16173 61 74 69 6f 6e 20 77 61 73 20 73 75 63 63 65 73  ation was succes
16174 73 66 75 6c 2e 20 49 6e 20 74 68 65 73 65 20 63  sful. In these c
16175 69 72 63 75 6d 73 74 61 6e 63 65 73 20 70 61 67  ircumstances pag
16176 65 72 4c 6f 63 6b 44 62 28 29 20 61 6e 64 0d 0a  erLockDb() and..
16177 2a 2a 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62  ** pagerUnlockDb
16178 28 29 20 74 61 6b 65 20 61 20 63 6f 6e 73 65 72  () take a conser
16179 76 61 74 69 76 65 20 61 70 70 72 6f 61 63 68 20  vative approach 
1617a 2d 20 65 4c 6f 63 6b 20 69 73 20 61 6c 77 61 79  - eLock is alway
1617b 73 20 75 70 64 61 74 65 64 0d 0a 2a 2a 20 77 68  s updated..** wh
1617c 65 6e 20 75 6e 6c 6f 63 6b 69 6e 67 20 74 68 65  en unlocking the
1617d 20 66 69 6c 65 2c 20 61 6e 64 20 6f 6e 6c 79 20   file, and only 
1617e 75 70 64 61 74 65 64 20 77 68 65 6e 20 6c 6f 63  updated when loc
1617f 6b 69 6e 67 20 74 68 65 20 66 69 6c 65 20 69 66  king the file if
16180 20 74 68 65 0d 0a 2a 2a 20 56 46 53 20 63 61 6c   the..** VFS cal
16181 6c 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e  l is successful.
16182 20 54 68 69 73 20 77 61 79 2c 20 74 68 65 20 50   This way, the P
16183 61 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 61  ager.eLock varia
16184 62 6c 65 20 6d 61 79 20 62 65 20 73 65 74 0d 0a  ble may be set..
16185 2a 2a 20 74 6f 20 61 20 6c 65 73 73 20 65 78 63  ** to a less exc
16186 6c 75 73 69 76 65 20 28 6c 6f 77 65 72 29 20 76  lusive (lower) v
16187 61 6c 75 65 20 74 68 61 6e 20 74 68 65 20 6c 6f  alue than the lo
16188 63 6b 20 74 68 61 74 20 69 73 20 61 63 74 75 61  ck that is actua
16189 6c 6c 79 20 68 65 6c 64 0d 0a 2a 2a 20 61 74 20  lly held..** at 
1618a 74 68 65 20 73 79 73 74 65 6d 20 6c 65 76 65 6c  the system level
1618b 2c 20 62 75 74 20 69 74 20 69 73 20 6e 65 76 65  , but it is neve
1618c 72 20 73 65 74 20 74 6f 20 61 20 6d 6f 72 65 20  r set to a more 
1618d 65 78 63 6c 75 73 69 76 65 20 76 61 6c 75 65 2e  exclusive value.
1618e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73  ..**..** This is
1618f 20 75 73 75 61 6c 6c 79 20 73 61 66 65 2e 20 49   usually safe. I
16190 66 20 61 6e 20 78 55 6e 6c 6f 63 6b 20 66 61 69  f an xUnlock fai
16191 6c 73 20 6f 72 20 61 70 70 65 61 72 73 20 74 6f  ls or appears to
16192 20 66 61 69 6c 2c 20 74 68 65 72 65 20 6d 61 79   fail, there may
16193 20 0d 0a 2a 2a 20 62 65 20 61 20 66 65 77 20 72   ..** be a few r
16194 65 64 75 6e 64 61 6e 74 20 78 4c 6f 63 6b 28 29  edundant xLock()
16195 20 63 61 6c 6c 73 20 6f 72 20 61 20 6c 6f 63 6b   calls or a lock
16196 20 6d 61 79 20 62 65 20 68 65 6c 64 20 66 6f 72   may be held for
16197 20 6c 6f 6e 67 65 72 20 74 68 61 6e 0d 0a 2a 2a   longer than..**
16198 20 72 65 71 75 69 72 65 64 2c 20 62 75 74 20 6e   required, but n
16199 6f 74 68 69 6e 67 20 72 65 61 6c 6c 79 20 67 6f  othing really go
1619a 65 73 20 77 72 6f 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  es wrong...**..*
1619b 2a 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20  * The exception 
1619c 69 73 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  is when the data
1619d 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 6e 6c  base file is unl
1619e 6f 63 6b 65 64 20 61 73 20 74 68 65 20 70 61 67  ocked as the pag
1619f 65 72 20 6d 6f 76 65 73 0d 0a 2a 2a 20 66 72 6f  er moves..** fro
161a0 6d 20 45 52 52 4f 52 20 74 6f 20 4f 50 45 4e 20  m ERROR to OPEN 
161a1 73 74 61 74 65 2e 20 41 74 20 74 68 69 73 20 70  state. At this p
161a2 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62  oint there may b
161a3 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  e a hot-journal 
161a4 66 69 6c 65 20 0d 0a 2a 2a 20 69 6e 20 74 68 65  file ..** in the
161a5 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 74 68 61   file-system tha
161a6 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f  t needs to be ro
161a7 6c 6c 65 64 20 62 61 63 6b 20 28 61 73 20 70 61  lled back (as pa
161a8 72 74 20 6f 66 20 61 20 4f 50 45 4e 2d 3e 53 48  rt of a OPEN->SH
161a9 41 52 45 44 0d 0a 2a 2a 20 74 72 61 6e 73 69 74  ARED..** transit
161aa 69 6f 6e 2c 20 62 79 20 74 68 65 20 73 61 6d 65  ion, by the same
161ab 20 70 61 67 65 72 20 6f 72 20 61 6e 79 20 6f 74   pager or any ot
161ac 68 65 72 29 2e 20 49 66 20 74 68 65 20 63 61 6c  her). If the cal
161ad 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b 28 29 0d 0a  l to xUnlock()..
161ae 2a 2a 20 66 61 69 6c 73 20 61 74 20 74 68 69 73  ** fails at this
161af 20 70 6f 69 6e 74 20 61 6e 64 20 74 68 65 20 70   point and the p
161b0 61 67 65 72 20 69 73 20 6c 65 66 74 20 68 6f 6c  ager is left hol
161b1 64 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ding an EXCLUSIV
161b2 45 20 6c 6f 63 6b 2c 20 74 68 69 73 0d 0a 2a 2a  E lock, this..**
161b3 20 63 61 6e 20 63 6f 6e 66 75 73 65 20 74 68 65   can confuse the
161b4 20 63 61 6c 6c 20 74 6f 20 78 43 68 65 63 6b 52   call to xCheckR
161b5 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 63 61  eservedLock() ca
161b6 6c 6c 20 6d 61 64 65 20 6c 61 74 65 72 20 61 73  ll made later as
161b7 20 70 61 72 74 0d 0a 2a 2a 20 6f 66 20 68 6f 74   part..** of hot
161b8 2d 6a 6f 75 72 6e 61 6c 20 64 65 74 65 63 74 69  -journal detecti
161b9 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 78 43 68 65  on...**..** xChe
161ba 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29  ckReservedLock()
161bb 20 69 73 20 64 65 66 69 6e 65 64 20 61 73 20 72   is defined as r
161bc 65 74 75 72 6e 69 6e 67 20 74 72 75 65 20 22 69  eturning true "i
161bd 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
161be 45 52 56 45 44 20 0d 0a 2a 2a 20 6c 6f 63 6b 20  ERVED ..** lock 
161bf 68 65 6c 64 20 62 79 20 74 68 69 73 20 70 72 6f  held by this pro
161c0 63 65 73 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  cess or any othe
161c1 72 73 22 2e 20 53 6f 20 78 43 68 65 63 6b 52 65  rs". So xCheckRe
161c2 73 65 72 76 65 64 4c 6f 63 6b 20 6d 61 79 20 0d  servedLock may .
161c3 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20  .** return true 
161c4 62 65 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c  because the call
161c5 65 72 20 69 74 73 65 6c 66 20 69 73 20 68 6f 6c  er itself is hol
161c6 64 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ding an EXCLUSIV
161c7 45 20 6c 6f 63 6b 20 28 62 75 74 0d 0a 2a 2a 20  E lock (but..** 
161c8 64 6f 65 73 6e 27 74 20 6b 6e 6f 77 20 69 74 20  doesn't know it 
161c9 62 65 63 61 75 73 65 20 6f 66 20 61 20 70 72 65  because of a pre
161ca 76 69 6f 75 73 20 65 72 72 6f 72 20 69 6e 20 78  vious error in x
161cb 55 6e 6c 6f 63 6b 29 2e 20 49 66 20 74 68 69 73  Unlock). If this
161cc 20 68 61 70 70 65 6e 73 0d 0a 2a 2a 20 61 20 68   happens..** a h
161cd 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 62  ot-journal may b
161ce 65 20 6d 69 73 74 61 6b 65 6e 20 66 6f 72 20 61  e mistaken for a
161cf 20 6a 6f 75 72 6e 61 6c 20 62 65 69 6e 67 20 63   journal being c
161d0 72 65 61 74 65 64 20 62 79 20 61 6e 20 61 63 74  reated by an act
161d1 69 76 65 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ive..** transact
161d2 69 6f 6e 20 69 6e 20 61 6e 6f 74 68 65 72 20 70  ion in another p
161d3 72 6f 63 65 73 73 2c 20 63 61 75 73 69 6e 67 20  rocess, causing 
161d4 53 51 4c 69 74 65 20 74 6f 20 72 65 61 64 20 66  SQLite to read f
161d5 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
161d6 0d 0a 2a 2a 20 77 69 74 68 6f 75 74 20 72 6f 6c  ..** without rol
161d7 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e 0d 0a 2a  ling it back...*
161d8 2a 0d 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72  *..** To work ar
161d9 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 61 20  ound this, if a 
161da 63 61 6c 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b 28  call to xUnlock(
161db 29 20 66 61 69 6c 73 20 77 68 65 6e 20 75 6e 6c  ) fails when unl
161dc 6f 63 6b 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 64  ocking the..** d
161dd 61 74 61 62 61 73 65 20 69 6e 20 74 68 65 20 45  atabase in the E
161de 52 52 4f 52 20 73 74 61 74 65 2c 20 50 61 67 65  RROR state, Page
161df 72 2e 65 4c 6f 63 6b 20 69 73 20 73 65 74 20 74  r.eLock is set t
161e0 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20  o UNKNOWN_LOCK. 
161e1 49 74 0d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 63  It..** is only c
161e2 68 61 6e 67 65 64 20 62 61 63 6b 20 74 6f 20 61  hanged back to a
161e3 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 73 74   real locking st
161e4 61 74 65 20 61 66 74 65 72 20 61 20 73 75 63 63  ate after a succ
161e5 65 73 73 66 75 6c 20 63 61 6c 6c 0d 0a 2a 2a 20  essful call..** 
161e6 74 6f 20 78 4c 6f 63 6b 28 45 58 43 4c 55 53 49  to xLock(EXCLUSI
161e7 56 45 29 2e 20 41 6c 73 6f 2c 20 74 68 65 20 63  VE). Also, the c
161e8 6f 64 65 20 74 6f 20 64 6f 20 74 68 65 20 4f 50  ode to do the OP
161e9 45 4e 2d 3e 53 48 41 52 45 44 20 73 74 61 74 65  EN->SHARED state
161ea 20 74 72 61 6e 73 69 74 69 6f 6e 0d 0a 2a 2a 20   transition..** 
161eb 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63 6b 20  omits the check 
161ec 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  for a hot-journa
161ed 6c 20 69 66 20 50 61 67 65 72 2e 65 4c 6f 63 6b  l if Pager.eLock
161ee 20 69 73 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f   is set to UNKNO
161ef 57 4e 5f 4c 4f 43 4b 20 0d 0a 2a 2a 20 6c 6f 63  WN_LOCK ..** loc
161f0 6b 2e 20 49 6e 73 74 65 61 64 2c 20 69 74 20 61  k. Instead, it a
161f1 73 73 75 6d 65 73 20 61 20 68 6f 74 2d 6a 6f 75  ssumes a hot-jou
161f2 72 6e 61 6c 20 65 78 69 73 74 73 20 61 6e 64 20  rnal exists and 
161f3 6f 62 74 61 69 6e 73 20 61 6e 20 45 58 43 4c 55  obtains an EXCLU
161f4 53 49 56 45 0d 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e  SIVE..** lock on
161f5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
161f6 6c 65 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70  le before attemp
161f7 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 20  ting to roll it 
161f8 62 61 63 6b 2e 20 53 65 65 20 66 75 6e 63 74 69  back. See functi
161f9 6f 6e 0d 0a 2a 2a 20 50 61 67 65 72 53 68 61 72  on..** PagerShar
161fa 65 64 4c 6f 63 6b 28 29 20 66 6f 72 20 6d 6f 72  edLock() for mor
161fb 65 20 64 65 74 61 69 6c 2e 0d 0a 2a 2a 0d 0a 2a  e detail...**..*
161fc 2a 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 6d 61  * Pager.eLock ma
161fd 79 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 74 6f  y only be set to
161fe 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68   UNKNOWN_LOCK wh
161ff 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20  en the pager is 
16200 69 6e 20 0d 0a 2a 2a 20 50 41 47 45 52 5f 4f 50  in ..** PAGER_OP
16201 45 4e 20 73 74 61 74 65 2e 0d 0a 2a 2f 0d 0a 23  EN state...*/..#
16202 64 65 66 69 6e 65 20 55 4e 4b 4e 4f 57 4e 5f 4c  define UNKNOWN_L
16203 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20  OCK             
16204 20 20 20 28 45 58 43 4c 55 53 49 56 45 5f 4c 4f     (EXCLUSIVE_LO
16205 43 4b 2b 31 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  CK+1)..../*..** 
16206 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72  A macro used for
16207 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f   invoking the co
16208 64 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20  dec if there is 
16209 6f 6e 65 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  one..*/..#ifdef 
1620a 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
1620b 0d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  ..# define CODEC
1620c 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0d 0a  1(P,D,N,X,E) \..
1620d 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65      if( P->xCode
1620e 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50  c && P->xCodec(P
1620f 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d  ->pCodec,D,N,X)=
16210 3d 30 20 29 7b 20 45 3b 20 7d 0d 0a 23 20 64 65  =0 ){ E; }..# de
16211 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c  fine CODEC2(P,D,
16212 4e 2c 58 2c 45 2c 4f 29 20 5c 0d 0a 20 20 20 20  N,X,E,O) \..    
16213 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30  if( P->xCodec==0
16214 20 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20   ){ O=(char*)D; 
16215 7d 65 6c 73 65 20 5c 0d 0a 20 20 20 20 69 66 28  }else \..    if(
16216 20 28 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78   (O=(char*)(P->x
16217 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c  Codec(P->pCodec,
16218 44 2c 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45  D,N,X)))==0 ){ E
16219 3b 20 7d 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65  ; }..#else..# de
1621a 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c  fine CODEC1(P,D,
1621b 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f  N,X,E)   /* NO-O
1621c 50 20 2a 2f 0d 0a 23 20 64 65 66 69 6e 65 20 43  P */..# define C
1621d 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c  ODEC2(P,D,N,X,E,
1621e 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 0d 0a 23  O) O=(char*)D..#
1621f 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
16220 54 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  The maximum allo
16221 77 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 2e  wed sector size.
16222 20 36 34 4b 69 42 2e 20 49 66 20 74 68 65 20 78   64KiB. If the x
16223 53 65 63 74 6f 72 73 69 7a 65 28 29 20 6d 65 74  Sectorsize() met
16224 68 6f 64 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 73  hod ..** returns
16225 20 61 20 76 61 6c 75 65 20 6c 61 72 67 65 72 20   a value larger 
16226 74 68 61 6e 20 74 68 69 73 2c 20 74 68 65 6e 20  than this, then 
16227 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20  MAX_SECTOR_SIZE 
16228 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e  is used instead.
16229 0d 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20  ..** This could 
1622a 63 6f 6e 63 65 69 76 61 62 6c 79 20 63 61 75 73  conceivably caus
1622b 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c  e corruption fol
1622c 6c 6f 77 69 6e 67 20 61 20 70 6f 77 65 72 20 66  lowing a power f
1622d 61 69 6c 75 72 65 20 6f 6e 0d 0a 2a 2a 20 73 75  ailure on..** su
1622e 63 68 20 61 20 73 79 73 74 65 6d 2e 20 54 68 69  ch a system. Thi
1622f 73 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  s is currently a
16230 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 6c  n undocumented l
16231 69 6d 69 74 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  imit...*/..#defi
16232 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  ne MAX_SECTOR_SI
16233 5a 45 20 30 78 31 30 30 30 30 0d 0a 0d 0a 2f 2a  ZE 0x10000..../*
16234 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  ..** An instance
16235 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
16236 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
16237 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
16238 68 20 61 63 74 69 76 65 0d 0a 2a 2a 20 73 61 76  h active..** sav
16239 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65  epoint and state
1623a 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1623b 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20   in the system. 
1623c 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74 75  All such structu
1623d 72 65 73 0d 0a 2a 2a 20 61 72 65 20 73 74 6f 72  res..** are stor
1623e 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e  ed in the Pager.
1623f 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72  aSavepoint[] arr
16240 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c  ay, which is all
16241 6f 63 61 74 65 64 20 61 6e 64 0d 0a 2a 2a 20 72  ocated and..** r
16242 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c  esized using sql
16243 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0d 0a  ite3Realloc()...
16244 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61  **..** When a sa
16245 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74  vepoint is creat
16246 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 61 76  ed, the PagerSav
16247 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
16248 74 20 66 69 65 6c 64 20 69 73 0d 0a 2a 2a 20 73  t field is..** s
16249 65 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f  et to 0. If a jo
1624a 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20  urnal-header is 
1624b 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
1624c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68   main journal wh
1624d 69 6c 65 0d 0a 2a 2a 20 74 68 65 20 73 61 76 65  ile..** the save
1624e 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c  point is active,
1624f 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74   then iHdrOffset
16250 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62   is set to the b
16251 79 74 65 20 6f 66 66 73 65 74 20 0d 0a 2a 2a 20  yte offset ..** 
16252 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
16253 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 6a  owing the last j
16254 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 72  ournal record wr
16255 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d  itten into the m
16256 61 69 6e 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ain..** journal 
16257 62 65 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e  before the journ
16258 61 6c 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20  al-header. This 
16259 69 73 20 72 65 71 75 69 72 65 64 20 64 75 72 69  is required duri
1625a 6e 67 20 73 61 76 65 70 6f 69 6e 74 0d 0a 2a 2a  ng savepoint..**
1625b 20 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65 20 70   rollback (see p
1625c 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65  agerPlaybackSave
1625d 70 6f 69 6e 74 28 29 29 2e 0d 0a 2a 2f 0d 0a 74  point())...*/..t
1625e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
1625f 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50 61 67  gerSavepoint Pag
16260 65 72 53 61 76 65 70 6f 69 6e 74 3b 0d 0a 73 74  erSavepoint;..st
16261 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f  ruct PagerSavepo
16262 69 6e 74 20 7b 0d 0a 20 20 69 36 34 20 69 4f 66  int {..  i64 iOf
16263 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
16264 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e        /* Startin
16265 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e  g offset in main
16266 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20 20 69   journal */..  i
16267 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20  64 iHdrOffset;  
16268 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
16269 65 65 20 61 62 6f 76 65 20 2a 2f 0d 0a 20 20 42  ee above */..  B
1626a 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f  itvec *pInSavepo
1626b 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  int;        /* S
1626c 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  et of pages in t
1626d 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f  his savepoint */
1626e 0d 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20  ..  Pgno nOrig; 
1626f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16270 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d   /* Original num
16271 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
16272 66 69 6c 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20  file */..  Pgno 
16273 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20  iSubRec;        
16274 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
16275 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 64   of first record
16276 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   in sub-journal 
16277 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
16278 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 75  TE_OMIT_WAL..  u
16279 33 32 20 61 57 61 6c 44 61 74 61 5b 57 41 4c 5f  32 aWalData[WAL_
1627a 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54 41 5d  SAVEPOINT_NDATA]
1627b 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 41 4c 20  ;        /* WAL 
1627c 73 61 76 65 70 6f 69 6e 74 20 63 6f 6e 74 65 78  savepoint contex
1627d 74 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b  t */..#endif..};
1627e 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 6f 70 65  ..../*..** A ope
1627f 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  n page cache is 
16280 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73  an instance of s
16281 74 72 75 63 74 20 50 61 67 65 72 2e 20 41 20 64  truct Pager. A d
16282 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 0d 0a 2a  escription of..*
16283 2a 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 6d 6f  * some of the mo
16284 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 6d 65 6d  re important mem
16285 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 66 6f  ber variables fo
16286 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 65  llows:..**..** e
16287 53 74 61 74 65 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  State..**..**   
16288 54 68 65 20 63 75 72 72 65 6e 74 20 27 73 74 61  The current 'sta
16289 74 65 27 20 6f 66 20 74 68 65 20 70 61 67 65 72  te' of the pager
1628a 20 6f 62 6a 65 63 74 2e 20 53 65 65 20 74 68 65   object. See the
1628b 20 63 6f 6d 6d 65 6e 74 20 61 6e 64 20 73 74 61   comment and sta
1628c 74 65 0d 0a 2a 2a 20 20 20 64 69 61 67 72 61 6d  te..**   diagram
1628d 20 61 62 6f 76 65 20 66 6f 72 20 61 20 64 65 73   above for a des
1628e 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
1628f 70 61 67 65 72 20 73 74 61 74 65 2e 0d 0a 2a 2a  pager state...**
16290 0d 0a 2a 2a 20 65 4c 6f 63 6b 0d 0a 2a 2a 0d 0a  ..** eLock..**..
16291 2a 2a 20 20 20 46 6f 72 20 61 20 72 65 61 6c 20  **   For a real 
16292 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65  on-disk database
16293 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  , the current lo
16294 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64  ck held on the d
16295 61 74 61 62 61 73 65 20 66 69 6c 65 20 2d 0d 0a  atabase file -..
16296 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 2c 20 53 48  **   NO_LOCK, SH
16297 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52  ARED_LOCK, RESER
16298 56 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 43 4c  VED_LOCK or EXCL
16299 55 53 49 56 45 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d  USIVE_LOCK...**.
1629a 0a 2a 2a 20 20 20 46 6f 72 20 61 20 74 65 6d 70  .**   For a temp
1629b 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f  orary or in-memo
1629c 72 79 20 64 61 74 61 62 61 73 65 20 28 6e 65 69  ry database (nei
1629d 74 68 65 72 20 6f 66 20 77 68 69 63 68 20 72 65  ther of which re
1629e 71 75 69 72 65 20 61 6e 79 0d 0a 2a 2a 20 20 20  quire any..**   
1629f 6c 6f 63 6b 73 29 2c 20 74 68 69 73 20 76 61 72  locks), this var
162a0 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 20  iable is always 
162a1 73 65 74 20 74 6f 20 45 58 43 4c 55 53 49 56 45  set to EXCLUSIVE
162a2 5f 4c 4f 43 4b 2e 20 53 69 6e 63 65 20 73 75 63  _LOCK. Since suc
162a3 68 0d 0a 2a 2a 20 20 20 64 61 74 61 62 61 73 65  h..**   database
162a4 73 20 61 6c 77 61 79 73 20 68 61 76 65 20 50 61  s always have Pa
162a5 67 65 72 2e 65 78 63 6c 75 73 69 76 65 4d 6f 64  ger.exclusiveMod
162a6 65 3d 3d 31 2c 20 74 68 69 73 20 74 72 69 63 6b  e==1, this trick
162a7 73 20 74 68 65 20 70 61 67 65 72 0d 0a 2a 2a 20  s the pager..** 
162a8 20 20 6c 6f 67 69 63 20 69 6e 74 6f 20 74 68 69    logic into thi
162a9 6e 6b 69 6e 67 20 74 68 61 74 20 69 74 20 61 6c  nking that it al
162aa 72 65 61 64 79 20 68 61 73 20 61 6c 6c 20 74 68  ready has all th
162ab 65 20 6c 6f 63 6b 73 20 69 74 20 77 69 6c 6c 20  e locks it will 
162ac 65 76 65 72 0d 0a 2a 2a 20 20 20 6e 65 65 64 20  ever..**   need 
162ad 28 61 6e 64 20 6e 6f 20 72 65 61 73 6f 6e 20 74  (and no reason t
162ae 6f 20 72 65 6c 65 61 73 65 20 74 68 65 6d 29 2e  o release them).
162af 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 49 6e 20 73 6f  ..**..**   In so
162b0 6d 65 20 28 6f 62 73 63 75 72 65 29 20 63 69 72  me (obscure) cir
162b1 63 75 6d 73 74 61 6e 63 65 73 2c 20 74 68 69 73  cumstances, this
162b2 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 61 6c   variable may al
162b3 73 6f 20 62 65 20 73 65 74 20 74 6f 0d 0a 2a 2a  so be set to..**
162b4 20 20 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e     UNKNOWN_LOCK.
162b5 20 53 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74   See the comment
162b6 20 61 62 6f 76 65 20 74 68 65 20 23 64 65 66 69   above the #defi
162b7 6e 65 20 6f 66 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f  ne of UNKNOWN_LO
162b8 43 4b 20 66 6f 72 0d 0a 2a 2a 20 20 20 64 65 74  CK for..**   det
162b9 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 63 68  ails...**..** ch
162ba 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0d 0a 2a  angeCountDone..*
162bb 2a 0d 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f  *..**   This boo
162bc 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73  lean variable is
162bd 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
162be 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 6e  re that the chan
162bf 67 65 2d 63 6f 75 6e 74 65 72 20 0d 0a 2a 2a 20  ge-counter ..** 
162c0 20 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65    (the 4-byte he
162c1 61 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79  ader field at by
162c2 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20  te offset 24 of 
162c3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
162c4 65 29 20 69 73 20 0d 0a 2a 2a 20 20 20 6e 6f 74  e) is ..**   not
162c5 20 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66   updated more of
162c6 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61  ten than necessa
162c7 72 79 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 49  ry. ..**..**   I
162c8 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  t is set to true
162c9 20 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65   when the change
162ca 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 69  -counter field i
162cb 73 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 68  s updated, which
162cc 20 0d 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79   ..**   can only
162cd 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78   happen if an ex
162ce 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20  clusive lock is 
162cf 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
162d0 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 20  base file...**  
162d1 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 28   It is cleared (
162d2 73 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 68  set to false) wh
162d3 65 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 73  enever an exclus
162d4 69 76 65 20 6c 6f 63 6b 20 69 73 20 0d 0a 2a 2a  ive lock is ..**
162d5 20 20 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20     relinquished 
162d6 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
162d7 66 69 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20  file. Each time 
162d8 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
162d9 20 63 6f 6d 6d 69 74 74 65 64 2c 0d 0a 2a 2a 20   committed,..** 
162da 20 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e    The changeCoun
162db 74 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 69 6e  tDone flag is in
162dc 73 70 65 63 74 65 64 2e 20 49 66 20 69 74 20 69  spected. If it i
162dd 73 20 74 72 75 65 2c 20 74 68 65 20 77 6f 72 6b  s true, the work
162de 20 6f 66 0d 0a 2a 2a 20 20 20 75 70 64 61 74 69   of..**   updati
162df 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  ng the change-co
162e0 75 6e 74 65 72 20 69 73 20 6f 6d 69 74 74 65 64  unter is omitted
162e1 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
162e2 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a   transaction...*
162e3 2a 0d 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65 63  *..**   This mec
162e4 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68 61  hanism means tha
162e5 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69  t when running i
162e6 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
162e7 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0d  , a connection .
162e8 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20  .**   need only 
162e9 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
162ea 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20  e-counter once, 
162eb 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 72  for the first tr
162ec 61 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2a 20 20 20  ansaction..**   
162ed 63 6f 6d 6d 69 74 74 65 64 2e 0d 0a 2a 2a 0d 0a  committed...**..
162ee 2a 2a 20 73 65 74 4d 61 73 74 65 72 0d 0a 2a 2a  ** setMaster..**
162ef 0d 0a 2a 2a 20 20 20 57 68 65 6e 20 50 61 67 65  ..**   When Page
162f0 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
162f1 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63  ) is called to c
162f2 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74  ommit a transact
162f3 69 6f 6e 2c 20 69 74 20 6d 61 79 0d 0a 2a 2a 20  ion, it may..** 
162f4 20 20 28 6f 72 20 6d 61 79 20 6e 6f 74 29 20 73    (or may not) s
162f5 70 65 63 69 66 79 20 61 20 6d 61 73 74 65 72 2d  pecify a master-
162f6 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f 20  journal name to 
162f7 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
162f8 74 68 65 20 0d 0a 2a 2a 20 20 20 6a 6f 75 72 6e  the ..**   journ
162f9 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69  al file before i
162fa 74 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64  t is synced to d
162fb 69 73 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 57  isk...**..**   W
162fc 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20  hether or not a 
162fd 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e  journal file con
162fe 74 61 69 6e 73 20 61 20 6d 61 73 74 65 72 2d 6a  tains a master-j
162ff 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61  ournal pointer a
16300 66 66 65 63 74 73 20 0d 0a 2a 2a 20 20 20 74 68  ffects ..**   th
16301 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 74  e way in which t
16302 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
16303 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 61 66 74  is finalized aft
16304 65 72 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  er the transacti
16305 6f 6e 20 69 73 20 0d 0a 2a 2a 20 20 20 63 6f 6d  on is ..**   com
16306 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
16307 20 62 61 63 6b 20 77 68 65 6e 20 72 75 6e 6e 69   back when runni
16308 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61 6c 5f 6d  ng in "journal_m
16309 6f 64 65 3d 50 45 52 53 49 53 54 22 20 6d 6f 64  ode=PERSIST" mod
1630a 65 2e 0d 0a 2a 2a 20 20 20 49 66 20 61 20 6a 6f  e...**   If a jo
1630b 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20  urnal file does 
1630c 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 6d 61  not contain a ma
1630d 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster-journal poi
1630e 6e 74 65 72 2c 20 69 74 20 69 73 0d 0a 2a 2a 20  nter, it is..** 
1630f 20 20 66 69 6e 61 6c 69 7a 65 64 20 62 79 20 6f    finalized by o
16310 76 65 72 77 72 69 74 69 6e 67 20 74 68 65 20 66  verwriting the f
16311 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  irst journal hea
16312 64 65 72 20 77 69 74 68 20 7a 65 72 6f 65 73 2e  der with zeroes.
16313 20 49 66 0d 0a 2a 2a 20 20 20 69 74 20 64 6f 65   If..**   it doe
16314 73 20 63 6f 6e 74 61 69 6e 20 61 20 6d 61 73 74  s contain a mast
16315 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er-journal point
16316 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  er the journal f
16317 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  ile is finalized
16318 20 0d 0a 2a 2a 20 20 20 62 79 20 74 72 75 6e 63   ..**   by trunc
16319 61 74 69 6e 67 20 69 74 20 74 6f 20 7a 65 72 6f  ating it to zero
1631a 20 62 79 74 65 73 2c 20 6a 75 73 74 20 61 73 20   bytes, just as 
1631b 69 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  if the connectio
1631c 6e 20 77 65 72 65 20 0d 0a 2a 2a 20 20 20 72 75  n were ..**   ru
1631d 6e 6e 69 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61  nning in "journa
1631e 6c 5f 6d 6f 64 65 3d 74 72 75 6e 63 61 74 65 22  l_mode=truncate"
1631f 20 6d 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20   mode...**..**  
16320 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 74   Journal files t
16321 68 61 74 20 63 6f 6e 74 61 69 6e 20 6d 61 73 74  hat contain mast
16322 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er journal point
16323 65 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 66 69  ers cannot be fi
16324 6e 61 6c 69 7a 65 64 0d 0a 2a 2a 20 20 20 73 69  nalized..**   si
16325 6d 70 6c 79 20 62 79 20 6f 76 65 72 77 72 69 74  mply by overwrit
16326 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 6a 6f  ing the first jo
16327 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 77 69 74  urnal-header wit
16328 68 20 7a 65 72 6f 65 73 2c 20 61 73 20 74 68 65  h zeroes, as the
16329 0d 0a 2a 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f  ..**   master jo
1632a 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 63 6f  urnal pointer co
1632b 75 6c 64 20 69 6e 74 65 72 66 65 72 65 20 77 69  uld interfere wi
1632c 74 68 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72  th hot-journal r
1632d 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 6e 79 0d 0a  ollback of any..
1632e 2a 2a 20 20 20 73 75 62 73 65 71 75 65 6e 74 6c  **   subsequentl
1632f 79 20 69 6e 74 65 72 72 75 70 74 65 64 20 74 72  y interrupted tr
16330 61 6e 73 61 63 74 69 6f 6e 20 74 68 61 74 20 72  ansaction that r
16331 65 75 73 65 73 20 74 68 65 20 6a 6f 75 72 6e 61  euses the journa
16332 6c 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  l file...**..** 
16333 20 20 54 68 65 20 66 6c 61 67 20 69 73 20 63 6c    The flag is cl
16334 65 61 72 65 64 20 61 73 20 73 6f 6f 6e 20 61 73  eared as soon as
16335 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
16336 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 28  e is finalized (
16337 65 69 74 68 65 72 0d 0a 2a 2a 20 20 20 62 79 20  either..**   by 
16338 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
16339 54 77 6f 20 6f 72 20 50 61 67 65 72 52 6f 6c 6c  Two or PagerRoll
1633a 62 61 63 6b 29 2e 20 49 66 20 61 6e 20 49 4f 20  back). If an IO 
1633b 65 72 72 6f 72 20 70 72 65 76 65 6e 74 73 20 74  error prevents t
1633c 68 65 0d 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c  he..**   journal
1633d 20 66 69 6c 65 20 66 72 6f 6d 20 62 65 69 6e 67   file from being
1633e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 66 69   successfully fi
1633f 6e 61 6c 69 7a 65 64 2c 20 74 68 65 20 73 65 74  nalized, the set
16340 4d 61 73 74 65 72 20 66 6c 61 67 0d 0a 2a 2a 20  Master flag..** 
16341 20 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 79    is cleared any
16342 77 61 79 20 28 61 6e 64 20 74 68 65 20 70 61 67  way (and the pag
16343 65 72 20 77 69 6c 6c 20 6d 6f 76 65 20 74 6f 20  er will move to 
16344 45 52 52 4f 52 20 73 74 61 74 65 29 2e 0d 0a 2a  ERROR state)...*
16345 2a 0d 0a 2a 2a 20 64 6f 4e 6f 74 53 70 69 6c 6c  *..** doNotSpill
16346 2c 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c  , doNotSyncSpill
16347 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 54 68 65 73 65  ..**..**   These
16348 20 74 77 6f 20 62 6f 6f 6c 65 61 6e 20 76 61 72   two boolean var
16349 69 61 62 6c 65 73 20 63 6f 6e 74 72 6f 6c 20 74  iables control t
1634a 68 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20  he behaviour of 
1634b 63 61 63 68 65 2d 73 70 69 6c 6c 73 0d 0a 2a 2a  cache-spills..**
1634c 20 20 20 28 63 61 6c 6c 73 20 6d 61 64 65 20 62     (calls made b
1634d 79 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64  y the pcache mod
1634e 75 6c 65 20 74 6f 20 74 68 65 20 70 61 67 65 72  ule to the pager
1634f 53 74 72 65 73 73 28 29 20 72 6f 75 74 69 6e 65  Stress() routine
16350 20 74 6f 0d 0a 2a 2a 20 20 20 77 72 69 74 65 20   to..**   write 
16351 63 61 63 68 65 64 20 64 61 74 61 20 74 6f 20 74  cached data to t
16352 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69  he file-system i
16353 6e 20 6f 72 64 65 72 20 74 6f 20 66 72 65 65 20  n order to free 
16354 75 70 20 6d 65 6d 6f 72 79 29 2e 0d 0a 2a 2a 0d  up memory)...**.
16355 0a 2a 2a 20 20 20 57 68 65 6e 20 64 6f 4e 6f 74  .**   When doNot
16356 53 70 69 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72  Spill is non-zer
16357 6f 2c 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  o, writing to th
16358 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20  e database from 
16359 70 61 67 65 72 53 74 72 65 73 73 28 29 0d 0a 2a  pagerStress()..*
1635a 2a 20 20 20 69 73 20 64 69 73 61 62 6c 65 64 20  *   is disabled 
1635b 61 6c 74 6f 67 65 74 68 65 72 2e 20 54 68 69 73  altogether. This
1635c 20 69 73 20 64 6f 6e 65 20 69 6e 20 61 20 76 65   is done in a ve
1635d 72 79 20 6f 62 73 63 75 72 65 20 63 61 73 65 20  ry obscure case 
1635e 74 68 61 74 0d 0a 2a 2a 20 20 20 63 6f 6d 65 73  that..**   comes
1635f 20 75 70 20 64 75 72 69 6e 67 20 73 61 76 65 70   up during savep
16360 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68  oint rollback th
16361 61 74 20 72 65 71 75 69 72 65 73 20 74 68 65 20  at requires the 
16362 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 0d 0a 2a  pcache module..*
16363 2a 20 20 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  *   to allocate 
16364 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 70 72  a new page to pr
16365 65 76 65 6e 74 20 74 68 65 20 6a 6f 75 72 6e 61  event the journa
16366 6c 20 66 69 6c 65 20 66 72 6f 6d 20 62 65 69 6e  l file from bein
16367 67 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 20 20  g written..**   
16368 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e  while it is bein
16369 67 20 74 72 61 76 65 72 73 65 64 20 62 79 20 63  g traversed by c
1636a 6f 64 65 20 69 6e 20 70 61 67 65 72 5f 70 6c 61  ode in pager_pla
1636b 79 62 61 63 6b 28 29 2e 0d 0a 2a 2a 20 0d 0a 2a  yback()...** ..*
1636c 2a 20 20 20 49 66 20 64 6f 4e 6f 74 53 79 6e 63  *   If doNotSync
1636d 53 70 69 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72  Spill is non-zer
1636e 6f 2c 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  o, writing to th
1636f 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20  e database from 
16370 70 61 67 65 72 53 74 72 65 73 73 28 29 0d 0a 2a  pagerStress()..*
16371 2a 20 20 20 69 73 20 70 65 72 6d 69 74 74 65 64  *   is permitted
16372 2c 20 62 75 74 20 73 79 6e 63 69 6e 67 20 74 68  , but syncing th
16373 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
16374 73 20 6e 6f 74 2e 20 54 68 69 73 20 66 6c 61 67  s not. This flag
16375 20 69 73 20 73 65 74 0d 0a 2a 2a 20 20 20 62 79   is set..**   by
16376 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
16377 74 65 28 29 20 77 68 65 6e 20 74 68 65 20 66 69  te() when the fi
16378 6c 65 2d 73 79 73 74 65 6d 20 73 65 63 74 6f 72  le-system sector
16379 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65 72 20  -size is larger 
1637a 74 68 61 6e 0d 0a 2a 2a 20 20 20 74 68 65 20 64  than..**   the d
1637b 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a  atabase page-siz
1637c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  e in order to pr
1637d 65 76 65 6e 74 20 61 20 6a 6f 75 72 6e 61 6c 20  event a journal 
1637e 73 79 6e 63 20 66 72 6f 6d 20 68 61 70 70 65 6e  sync from happen
1637f 69 6e 67 20 0d 0a 2a 2a 20 20 20 69 6e 20 62 65  ing ..**   in be
16380 74 77 65 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61  tween the journa
16381 6c 6c 69 6e 67 20 6f 66 20 74 77 6f 20 70 61 67  lling of two pag
16382 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 73  es on the same s
16383 65 63 74 6f 72 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20  ector. ..**..** 
16384 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 0d 0a 2a 2a  subjInMemory..**
16385 0d 0a 2a 2a 20 20 20 54 68 69 73 20 69 73 20 61  ..**   This is a
16386 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c   boolean variabl
16387 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68 65 6e  e. If true, then
16388 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 73 75   any required su
16389 62 2d 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 20 20  b-journal..**   
1638a 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20  is opened as an 
1638b 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61  in-memory journa
1638c 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c 73 65  l file. If false
1638d 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79  , then in-memory
1638e 0d 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 6e  ..**   sub-journ
1638f 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  als are only use
16390 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  d for in-memory 
16391 70 61 67 65 72 20 66 69 6c 65 73 2e 0d 0a 2a 2a  pager files...**
16392 0d 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69  ..**   This vari
16393 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64 20  able is updated 
16394 62 79 20 74 68 65 20 75 70 70 65 72 20 6c 61 79  by the upper lay
16395 65 72 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e  er each time a n
16396 65 77 20 0d 0a 2a 2a 20 20 20 77 72 69 74 65 2d  ew ..**   write-
16397 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
16398 70 65 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 64  pened...**..** d
16399 62 53 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a  bSize, dbOrigSiz
1639a 65 2c 20 64 62 46 69 6c 65 53 69 7a 65 0d 0a 2a  e, dbFileSize..*
1639b 2a 0d 0a 2a 2a 20 20 20 56 61 72 69 61 62 6c 65  *..**   Variable
1639c 20 64 62 53 69 7a 65 20 69 73 20 73 65 74 20 74   dbSize is set t
1639d 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1639e 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
1639f 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20  abase file...** 
163a0 20 20 49 74 20 69 73 20 76 61 6c 69 64 20 69 6e    It is valid in
163a1 20 50 41 47 45 52 5f 52 45 41 44 45 52 20 61 6e   PAGER_READER an
163a2 64 20 68 69 67 68 65 72 20 73 74 61 74 65 73 20  d higher states 
163a3 28 61 6c 6c 20 73 74 61 74 65 73 20 65 78 63 65  (all states exce
163a4 70 74 20 66 6f 72 0d 0a 2a 2a 20 20 20 4f 50 45  pt for..**   OPE
163a5 4e 20 61 6e 64 20 45 52 52 4f 52 29 2e 20 0d 0a  N and ERROR). ..
163a6 2a 2a 0d 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20  **..**   dbSize 
163a7 69 73 20 73 65 74 20 62 61 73 65 64 20 6f 6e 20  is set based on 
163a8 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
163a9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77  database file, w
163aa 68 69 63 68 20 6d 61 79 20 62 65 20 0d 0a 2a 2a  hich may be ..**
163ab 20 20 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74     larger than t
163ac 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  he size of the d
163ad 61 74 61 62 61 73 65 20 28 74 68 65 20 76 61 6c  atabase (the val
163ae 75 65 20 73 74 6f 72 65 64 20 61 74 20 6f 66 66  ue stored at off
163af 73 65 74 0d 0a 2a 2a 20 20 20 32 38 20 6f 66 20  set..**   28 of 
163b0 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61  the database hea
163b1 64 65 72 20 62 79 20 74 68 65 20 62 74 72 65 65  der by the btree
163b2 29 2e 20 49 66 20 74 68 65 20 73 69 7a 65 20 6f  ). If the size o
163b3 66 20 74 68 65 20 66 69 6c 65 0d 0a 2a 2a 20 20  f the file..**  
163b4 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
163b5 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 74  er multiple of t
163b6 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68  he page-size, th
163b7 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
163b8 6e 0d 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 69  n..**   dbSize i
163b9 73 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 28  s rounded down (
163ba 69 2e 65 2e 20 61 20 35 4b 42 20 66 69 6c 65 20  i.e. a 5KB file 
163bb 77 69 74 68 20 32 4b 20 70 61 67 65 2d 73 69 7a  with 2K page-siz
163bc 65 20 68 61 73 20 64 62 53 69 7a 65 3d 3d 32 29  e has dbSize==2)
163bd 2e 0d 0a 2a 2a 20 20 20 45 78 63 65 70 74 2c 20  ...**   Except, 
163be 61 6e 79 20 66 69 6c 65 20 74 68 61 74 20 69 73  any file that is
163bf 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 20   greater than 0 
163c0 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 69 73  bytes in size is
163c1 20 63 6f 6e 73 69 64 65 72 65 64 0d 0a 2a 2a 20   considered..** 
163c2 20 20 74 6f 20 68 61 76 65 20 61 74 20 6c 65 61    to have at lea
163c3 73 74 20 6f 6e 65 20 70 61 67 65 2e 20 28 69 2e  st one page. (i.
163c4 65 2e 20 61 20 31 4b 42 20 66 69 6c 65 20 77 69  e. a 1KB file wi
163c5 74 68 20 32 4b 20 70 61 67 65 2d 73 69 7a 65 20  th 2K page-size 
163c6 6c 65 61 64 73 0d 0a 2a 2a 20 20 20 74 6f 20 64  leads..**   to d
163c7 62 53 69 7a 65 3d 3d 31 29 2e 0d 0a 2a 2a 0d 0a  bSize==1)...**..
163c8 2a 2a 20 20 20 44 75 72 69 6e 67 20 61 20 77 72  **   During a wr
163c9 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ite-transaction,
163ca 20 69 66 20 70 61 67 65 73 20 77 69 74 68 20 70   if pages with p
163cb 61 67 65 2d 6e 75 6d 62 65 72 73 20 67 72 65 61  age-numbers grea
163cc 74 65 72 20 74 68 61 6e 0d 0a 2a 2a 20 20 20 64  ter than..**   d
163cd 62 53 69 7a 65 20 61 72 65 20 6d 6f 64 69 66 69  bSize are modifi
163ce 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  ed in the cache,
163cf 20 64 62 53 69 7a 65 20 69 73 20 75 70 64 61 74   dbSize is updat
163d0 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0d  ed accordingly..
163d1 0a 2a 2a 20 20 20 53 69 6d 69 6c 61 72 6c 79 2c  .**   Similarly,
163d2 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
163d3 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 75 73   is truncated us
163d4 69 6e 67 20 50 61 67 65 72 54 72 75 6e 63 61 74  ing PagerTruncat
163d5 65 49 6d 61 67 65 28 29 2c 20 0d 0a 2a 2a 20 20  eImage(), ..**  
163d6 20 64 62 53 69 7a 65 20 69 73 20 75 70 64 61 74   dbSize is updat
163d7 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 56 61  ed...**..**   Va
163d8 72 69 61 62 6c 65 73 20 64 62 4f 72 69 67 53 69  riables dbOrigSi
163d9 7a 65 20 61 6e 64 20 64 62 46 69 6c 65 53 69 7a  ze and dbFileSiz
163da 65 20 61 72 65 20 76 61 6c 69 64 20 69 6e 20 73  e are valid in s
163db 74 61 74 65 73 20 0d 0a 2a 2a 20 20 20 50 41 47  tates ..**   PAG
163dc 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44  ER_WRITER_LOCKED
163dd 20 61 6e 64 20 68 69 67 68 65 72 2e 20 64 62 4f   and higher. dbO
163de 72 69 67 53 69 7a 65 20 69 73 20 61 20 63 6f 70  rigSize is a cop
163df 79 20 6f 66 20 74 68 65 20 64 62 53 69 7a 65 0d  y of the dbSize.
163e0 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 61  .**   variable a
163e1 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
163e2 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
163e3 49 74 20 69 73 20 75 73 65 64 20 64 75 72 69 6e  It is used durin
163e4 67 20 72 6f 6c 6c 62 61 63 6b 2c 0d 0a 2a 2a 20  g rollback,..** 
163e5 20 20 61 6e 64 20 74 6f 20 64 65 74 65 72 6d 69    and to determi
163e6 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ne whether or no
163e7 74 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20  t pages need to 
163e8 62 65 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 65  be journalled be
163e9 66 6f 72 65 0d 0a 2a 2a 20 20 20 62 65 69 6e 67  fore..**   being
163ea 20 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2a 0d 0a   modified...**..
163eb 2a 2a 20 20 20 54 68 72 6f 75 67 68 6f 75 74 20  **   Throughout 
163ec 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
163ed 69 6f 6e 2c 20 64 62 46 69 6c 65 53 69 7a 65 20  ion, dbFileSize 
163ee 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 69 7a  contains the siz
163ef 65 20 6f 66 0d 0a 2a 2a 20 20 20 74 68 65 20 66  e of..**   the f
163f0 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 6e 20 70  ile on disk in p
163f1 61 67 65 73 2e 20 49 74 20 69 73 20 73 65 74 20  ages. It is set 
163f2 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 64 62 53  to a copy of dbS
163f3 69 7a 65 20 77 68 65 6e 20 74 68 65 0d 0a 2a 2a  ize when the..**
163f4 20 20 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63     write-transac
163f5 74 69 6f 6e 20 69 73 20 66 69 72 73 74 20 6f 70  tion is first op
163f6 65 6e 65 64 2c 20 61 6e 64 20 75 70 64 61 74 65  ened, and update
163f7 64 20 77 68 65 6e 20 56 46 53 20 63 61 6c 6c 73  d when VFS calls
163f8 20 61 72 65 20 6d 61 64 65 0d 0a 2a 2a 20 20 20   are made..**   
163f9 74 6f 20 77 72 69 74 65 20 6f 72 20 74 72 75 6e  to write or trun
163fa 63 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73  cate the databas
163fb 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20  e file on disk. 
163fc 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 54 68 65 20 6f  ..**..**   The o
163fd 6e 6c 79 20 72 65 61 73 6f 6e 20 74 68 65 20 64  nly reason the d
163fe 62 46 69 6c 65 53 69 7a 65 20 76 61 72 69 61 62  bFileSize variab
163ff 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 69  le is required i
16400 73 20 74 6f 20 73 75 70 70 72 65 73 73 20 0d 0a  s to suppress ..
16401 2a 2a 20 20 20 75 6e 6e 65 63 65 73 73 61 72 79  **   unnecessary
16402 20 63 61 6c 6c 73 20 74 6f 20 78 54 72 75 6e 63   calls to xTrunc
16403 61 74 65 28 29 20 61 66 74 65 72 20 63 6f 6d 6d  ate() after comm
16404 69 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63  itting a transac
16405 74 69 6f 6e 2e 20 49 66 2c 20 0d 0a 2a 2a 20 20  tion. If, ..**  
16406 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
16407 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
16408 2c 20 74 68 65 20 64 62 46 69 6c 65 53 69 7a 65  , the dbFileSize
16409 20 76 61 72 69 61 62 6c 65 20 69 6e 64 69 63 61   variable indica
1640a 74 65 73 20 0d 0a 2a 2a 20 20 20 74 68 61 74 20  tes ..**   that 
1640b 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1640c 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
1640d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
1640e 61 67 65 20 28 50 61 67 65 72 2e 64 62 53 69 7a  age (Pager.dbSiz
1640f 65 29 2c 20 0d 0a 2a 2a 20 20 20 70 61 67 65 72  e), ..**   pager
16410 5f 74 72 75 6e 63 61 74 65 28 29 20 69 73 20 63  _truncate() is c
16411 61 6c 6c 65 64 2e 20 54 68 65 20 70 61 67 65 72  alled. The pager
16412 5f 74 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c  _truncate() call
16413 20 75 73 65 73 20 78 46 69 6c 65 73 69 7a 65 28   uses xFilesize(
16414 29 0d 0a 2a 2a 20 20 20 74 6f 20 6d 65 61 73 75  )..**   to measu
16415 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
16416 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 61 6e  file on disk, an
16417 64 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 73  d then truncates
16418 20 69 74 20 69 66 20 72 65 71 75 69 72 65 64 2e   it if required.
16419 0d 0a 2a 2a 20 20 20 64 62 46 69 6c 65 53 69 7a  ..**   dbFileSiz
1641a 65 20 69 73 20 6e 6f 74 20 75 73 65 64 20 77 68  e is not used wh
1641b 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  en rolling back 
1641c 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  a transaction. I
1641d 6e 20 74 68 69 73 20 63 61 73 65 0d 0a 2a 2a 20  n this case..** 
1641e 20 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65    pager_truncate
1641f 28 29 20 69 73 20 63 61 6c 6c 65 64 20 75 6e 63  () is called unc
16420 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 28 77 68  onditionally (wh
16421 69 63 68 20 6d 65 61 6e 73 20 74 68 65 72 65 20  ich means there 
16422 6d 61 79 20 62 65 0d 0a 2a 2a 20 20 20 61 20 63  may be..**   a c
16423 61 6c 6c 20 74 6f 20 78 46 69 6c 65 73 69 7a 65  all to xFilesize
16424 28 29 20 74 68 61 74 20 69 73 20 6e 6f 74 20 73  () that is not s
16425 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64  trictly required
16426 29 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  ). In either cas
16427 65 2c 0d 0a 2a 2a 20 20 20 70 61 67 65 72 5f 74  e,..**   pager_t
16428 72 75 6e 63 61 74 65 28 29 20 6d 61 79 20 63 61  runcate() may ca
16429 75 73 65 20 74 68 65 20 66 69 6c 65 20 74 6f 20  use the file to 
1642a 62 65 63 6f 6d 65 20 73 6d 61 6c 6c 65 72 20 6f  become smaller o
1642b 72 20 6c 61 72 67 65 72 2e 0d 0a 2a 2a 0d 0a 2a  r larger...**..*
1642c 2a 20 64 62 48 69 6e 74 53 69 7a 65 0d 0a 2a 2a  * dbHintSize..**
1642d 0d 0a 2a 2a 20 20 20 54 68 65 20 64 62 48 69 6e  ..**   The dbHin
1642e 74 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69  tSize variable i
1642f 73 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20  s used to limit 
16430 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  the number of ca
16431 6c 6c 73 20 6d 61 64 65 20 74 6f 0d 0a 2a 2a 20  lls made to..** 
16432 20 20 74 68 65 20 56 46 53 20 78 46 69 6c 65 43    the VFS xFileC
16433 6f 6e 74 72 6f 6c 28 46 43 4e 54 4c 5f 53 49 5a  ontrol(FCNTL_SIZ
16434 45 5f 48 49 4e 54 29 20 6d 65 74 68 6f 64 2e 20  E_HINT) method. 
16435 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 64 62 48 69 6e  ..**..**   dbHin
16436 74 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20  tSize is set to 
16437 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 62  a copy of the db
16438 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 77 68  Size variable wh
16439 65 6e 20 61 0d 0a 2a 2a 20 20 20 77 72 69 74 65  en a..**   write
1643a 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
1643b 6f 70 65 6e 65 64 20 28 61 74 20 74 68 65 20 73  opened (at the s
1643c 61 6d 65 20 74 69 6d 65 20 61 73 20 64 62 46 69  ame time as dbFi
1643d 6c 65 53 69 7a 65 20 61 6e 64 0d 0a 2a 2a 20 20  leSize and..**  
1643e 20 64 62 4f 72 69 67 53 69 7a 65 29 2e 20 49 66   dbOrigSize). If
1643f 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
16440 6c 28 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e  l(FCNTL_SIZE_HIN
16441 54 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c  T) method is cal
16442 6c 65 64 2c 0d 0a 2a 2a 20 20 20 64 62 48 69 6e  led,..**   dbHin
16443 74 53 69 7a 65 20 69 73 20 69 6e 63 72 65 61 73  tSize is increas
16444 65 64 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  ed to the number
16445 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 63   of pages that c
16446 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65  orrespond to the
16447 0d 0a 2a 2a 20 20 20 73 69 7a 65 2d 68 69 6e 74  ..**   size-hint
16448 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 6d   passed to the m
16449 65 74 68 6f 64 20 63 61 6c 6c 2e 20 53 65 65 20  ethod call. See 
1644a 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
1644b 6c 69 73 74 28 29 20 66 6f 72 20 0d 0a 2a 2a 20  list() for ..** 
1644c 20 20 64 65 74 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a    details...**..
1644d 2a 2a 20 65 72 72 43 6f 64 65 0d 0a 2a 2a 0d 0a  ** errCode..**..
1644e 2a 2a 20 20 20 54 68 65 20 50 61 67 65 72 2e 65  **   The Pager.e
1644f 72 72 43 6f 64 65 20 76 61 72 69 61 62 6c 65 20  rrCode variable 
16450 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65  is only ever use
16451 64 20 69 6e 20 50 41 47 45 52 5f 45 52 52 4f 52  d in PAGER_ERROR
16452 20 73 74 61 74 65 2e 20 49 74 0d 0a 2a 2a 20 20   state. It..**  
16453 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20   is set to zero 
16454 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 61  in all other sta
16455 74 65 73 2e 20 49 6e 20 50 41 47 45 52 5f 45 52  tes. In PAGER_ER
16456 52 4f 52 20 73 74 61 74 65 2c 20 50 61 67 65 72  ROR state, Pager
16457 2e 65 72 72 43 6f 64 65 20 0d 0a 2a 2a 20 20 20  .errCode ..**   
16458 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f  is always set to
16459 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 53 51   SQLITE_FULL, SQ
1645a 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 6f 6e  LITE_IOERR or on
1645b 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
1645c 49 4f 45 52 52 5f 58 58 58 20 0d 0a 2a 2a 20 20  IOERR_XXX ..**  
1645d 20 73 75 62 2d 63 6f 64 65 73 2e 0d 0a 2a 2f 0d   sub-codes...*/.
1645e 0a 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0d  .struct Pager {.
1645f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
16460 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f  pVfs;          /
16461 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74  * OS functions t
16462 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0d  o use for IO */.
16463 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d  .  u8 exclusiveM
16464 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ode;           /
16465 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20  * Boolean. True 
16466 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  if locking_mode=
16467 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0d 0a 20  =EXCLUSIVE */.. 
16468 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b   u8 journalMode;
16469 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1646a 4f 6e 65 20 6f 66 20 74 68 65 20 50 41 47 45 52  One of the PAGER
1646b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76  _JOURNALMODE_* v
1646c 61 6c 75 65 73 20 2a 2f 0d 0a 20 20 75 38 20 75  alues */..  u8 u
1646d 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  seJournal;      
1646e 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61          /* Use a
1646f 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
16470 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a  l on this file *
16471 2f 0d 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f  /..  u8 noReadlo
16472 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
16473 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65   /* Do not bothe
16474 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64  r to obtain read
16475 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20 75 38 20 6e  locks */..  u8 n
16476 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20  oSync;          
16477 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
16478 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  t sync the journ
16479 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0d 0a 20  al if true */.. 
1647a 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20   u8 fullSync;   
1647b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1647c 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f  Do extra syncs o
1647d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f  f the journal fo
1647e 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0d  r robustness */.
1647f 0a 20 20 75 38 20 63 6b 70 74 53 79 6e 63 46 6c  .  u8 ckptSyncFl
16480 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ags;           /
16481 2a 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72  * SYNC_NORMAL or
16482 20 53 59 4e 43 5f 46 55 4c 4c 20 66 6f 72 20 63   SYNC_FULL for c
16483 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0d 0a 20 20  heckpoint */..  
16484 75 38 20 77 61 6c 53 79 6e 63 46 6c 61 67 73 3b  u8 walSyncFlags;
16485 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
16486 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59  YNC_NORMAL or SY
16487 4e 43 5f 46 55 4c 4c 20 66 6f 72 20 77 61 6c 20  NC_FULL for wal 
16488 77 72 69 74 65 73 20 2a 2f 0d 0a 20 20 75 38 20  writes */..  u8 
16489 73 79 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20  syncFlags;      
1648a 20 20 20 20 20 20 20 20 20 2f 2a 20 53 59 4e 43           /* SYNC
1648b 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f  _NORMAL or SYNC_
1648c 46 55 4c 4c 20 6f 74 68 65 72 77 69 73 65 20 2a  FULL otherwise *
1648d 2f 0d 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65  /..  u8 tempFile
1648e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1648f 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   /* zFilename is
16490 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
16491 65 20 2a 2f 0d 0a 20 20 75 38 20 72 65 61 64 4f  e */..  u8 readO
16492 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  nly;            
16493 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
16494 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61  a read-only data
16495 62 61 73 65 20 2a 2f 0d 0a 20 20 75 38 20 6d 65  base */..  u8 me
16496 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  mDb;            
16497 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
16498 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69  o inhibit all fi
16499 6c 65 20 49 2f 4f 20 2a 2f 0d 0a 0d 0a 20 20 2f  le I/O */....  /
1649a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1649b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1649c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1649d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1649e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 20 20 2a 2a  **********..  **
1649f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
164a0 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68  lock contains th
164a1 6f 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72  ose class member
164a2 73 20 74 68 61 74 20 63 68 61 6e 67 65 20 64 75  s that change du
164a3 72 69 6e 67 0d 0a 20 20 2a 2a 20 72 6f 75 74 69  ring..  ** routi
164a4 6e 65 20 6f 70 65 72 74 69 6f 6e 2e 20 20 43 6c  ne opertion.  Cl
164a5 61 73 73 20 6d 65 6d 62 65 72 73 20 6e 6f 74 20  ass members not 
164a6 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 72  in this block ar
164a7 65 20 65 69 74 68 65 72 20 66 69 78 65 64 0d 0a  e either fixed..
164a8 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 70 61    ** when the pa
164a9 67 65 72 20 69 73 20 66 69 72 73 74 20 63 72 65  ger is first cre
164aa 61 74 65 64 20 6f 72 20 65 6c 73 65 20 6f 6e 6c  ated or else onl
164ab 79 20 63 68 61 6e 67 65 20 77 68 65 6e 20 74 68  y change when th
164ac 65 72 65 20 69 73 20 61 0d 0a 20 20 2a 2a 20 73  ere is a..  ** s
164ad 69 67 6e 69 66 69 63 61 6e 74 20 6d 6f 64 65 20  ignificant mode 
164ae 63 68 61 6e 67 65 20 28 73 75 63 68 20 61 73 20  change (such as 
164af 63 68 61 6e 67 69 6e 67 20 74 68 65 20 70 61 67  changing the pag
164b0 65 5f 73 69 7a 65 2c 20 6c 6f 63 6b 69 6e 67 5f  e_size, locking_
164b1 6d 6f 64 65 2c 0d 0a 20 20 2a 2a 20 6f 72 20 74  mode,..  ** or t
164b2 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 29  he journal_mode)
164b3 2e 20 20 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20  .  From another 
164b4 76 69 65 77 2c 20 74 68 65 73 65 20 63 6c 61 73  view, these clas
164b5 73 20 6d 65 6d 62 65 72 73 20 64 65 73 63 72 69  s members descri
164b6 62 65 0d 0a 20 20 2a 2a 20 74 68 65 20 22 73 74  be..  ** the "st
164b7 61 74 65 22 20 6f 66 20 74 68 65 20 70 61 67 65  ate" of the page
164b8 72 2c 20 77 68 69 6c 65 20 6f 74 68 65 72 20 63  r, while other c
164b9 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 64 65 73  lass members des
164ba 63 72 69 62 65 20 74 68 65 0d 0a 20 20 2a 2a 20  cribe the..  ** 
164bb 22 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 22 20  "configuration" 
164bc 6f 66 20 74 68 65 20 70 61 67 65 72 2e 0d 0a 20  of the pager... 
164bd 20 2a 2f 0d 0a 20 20 75 38 20 65 53 74 61 74 65   */..  u8 eState
164be 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
164bf 20 20 20 2f 2a 20 50 61 67 65 72 20 73 74 61 74     /* Pager stat
164c0 65 20 28 4f 50 45 4e 2c 20 52 45 41 44 45 52 2c  e (OPEN, READER,
164c1 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 2e 2e   WRITER_LOCKED..
164c2 29 20 2a 2f 0d 0a 20 20 75 38 20 65 4c 6f 63 6b  ) */..  u8 eLock
164c3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
164c4 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6c      /* Current l
164c5 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 64 61 74 61  ock held on data
164c6 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  base file */..  
164c7 75 38 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  u8 changeCountDo
164c8 6e 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ne;         /* S
164c9 65 74 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65  et after increme
164ca 6e 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65  nting the change
164cb 2d 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 75  -counter */..  u
164cc 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20 20 20  8 setMaster;    
164cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
164ce 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65  ue if a m-j name
164cf 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65   has been writte
164d0 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0d 0a 20 20  n to jrnl */..  
164d1 75 38 20 64 6f 4e 6f 74 53 70 69 6c 6c 3b 20 20  u8 doNotSpill;  
164d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
164d3 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 20  o not spill the 
164d4 63 61 63 68 65 20 77 68 65 6e 20 6e 6f 6e 2d 7a  cache when non-z
164d5 65 72 6f 20 2a 2f 0d 0a 20 20 75 38 20 64 6f 4e  ero */..  u8 doN
164d6 6f 74 53 79 6e 63 53 70 69 6c 6c 3b 20 20 20 20  otSyncSpill;    
164d7 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
164d8 64 6f 20 61 20 73 70 69 6c 6c 20 74 68 61 74 20  do a spill that 
164d9 72 65 71 75 69 72 65 73 20 6a 72 6e 6c 20 73 79  requires jrnl sy
164da 6e 63 20 2a 2f 0d 0a 20 20 75 38 20 73 75 62 6a  nc */..  u8 subj
164db 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20  InMemory;       
164dc 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
164dd 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75  use in-memory su
164de 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0d 0a 20  b-journals */.. 
164df 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20   Pgno dbSize;   
164e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
164e1 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
164e2 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
164e3 2a 2f 0d 0a 20 20 50 67 6e 6f 20 64 62 4f 72 69  */..  Pgno dbOri
164e4 67 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  gSize;          
164e5 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f    /* dbSize befo
164e6 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  re the current t
164e7 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a 20  ransaction */.. 
164e8 20 50 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65   Pgno dbFileSize
164e9 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
164ea 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
164eb 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
164ec 66 69 6c 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20  file */..  Pgno 
164ed 64 62 48 69 6e 74 53 69 7a 65 3b 20 20 20 20 20  dbHintSize;     
164ee 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
164ef 70 61 73 73 65 64 20 74 6f 20 46 43 4e 54 4c 5f  passed to FCNTL_
164f0 53 49 5a 45 5f 48 49 4e 54 20 63 61 6c 6c 20 2a  SIZE_HINT call *
164f1 2f 0d 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65  /..  int errCode
164f2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
164f3 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72   /* One of sever
164f4 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f  al kinds of erro
164f5 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65  rs */..  int nRe
164f6 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
164f7 20 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f       /* Pages jo
164f8 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c  urnalled since l
164f9 61 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69  ast j-header wri
164fa 74 74 65 6e 20 2a 2f 0d 0a 20 20 75 33 32 20 63  tten */..  u32 c
164fb 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20  ksumInit;       
164fc 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d         /* Quasi-
164fd 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64  random value add
164fe 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 65 63  ed to every chec
164ff 6b 73 75 6d 20 2a 2f 0d 0a 20 20 75 33 32 20 6e  ksum */..  u32 n
16500 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 20  SubRec;         
16501 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16502 20 6f 66 20 72 65 63 6f 72 64 73 20 77 72 69 74   of records writ
16503 74 65 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e  ten to sub-journ
16504 61 6c 20 2a 2f 0d 0a 20 20 42 69 74 76 65 63 20  al */..  Bitvec 
16505 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  *pInJournal;    
16506 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20       /* One bit 
16507 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e  for each page in
16508 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
16509 6c 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  le */..  sqlite3
1650a 5f 66 69 6c 65 20 2a 66 64 3b 20 20 20 20 20 20  _file *fd;      
1650b 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
1650c 63 72 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61  criptor for data
1650d 62 61 73 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  base */..  sqlit
1650e 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20  e3_file *jfd;   
1650f 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64         /* File d
16510 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 6d 61  escriptor for ma
16511 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20  in journal */.. 
16512 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73   sqlite3_file *s
16513 6a 66 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  jfd;         /* 
16514 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
16515 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  for sub-journal 
16516 2a 2f 0d 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61  */..  i64 journa
16517 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  lOff;           
16518 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 72 69    /* Current wri
16519 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65  te offset in the
1651a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
1651b 0d 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48  ..  i64 journalH
1651c 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
1651d 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74  /* Byte offset t
1651e 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e  o previous journ
1651f 61 6c 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20  al header */..  
16520 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
16521 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a 20 50  pBackup;    /* P
16522 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 20 6f  ointer to list o
16523 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b 75 70  f ongoing backup
16524 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0d 0a 20   processes */.. 
16525 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
16526 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20  *aSavepoint; /* 
16527 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 20  Array of active 
16528 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0d 0a 20  savepoints */.. 
16529 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b   int nSavepoint;
1652a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1652b 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
1652c 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74  ts in aSavepoint
1652d 5b 5d 20 2a 2f 0d 0a 20 20 63 68 61 72 20 64 62  [] */..  char db
1652e 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20  FileVers[16];   
1652f 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20       /* Changes 
16530 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73  whenever databas
16531 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a  e file changes *
16532 2f 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 45 6e  /..  /*..  ** En
16533 64 20 6f 66 20 74 68 65 20 72 6f 75 74 69 6e 65  d of the routine
16534 6c 79 2d 63 68 61 6e 67 69 6e 67 20 63 6c 61 73  ly-changing clas
16535 73 20 6d 65 6d 62 65 72 73 0d 0a 20 20 2a 2a 2a  s members..  ***
16536 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1653a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 20 20 75  ********/....  u
1653b 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20  16 nExtra;      
1653c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
1653d 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  d this many byte
1653e 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d  s to each in-mem
1653f 6f 72 79 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69  ory page */..  i
16540 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20  16 nReserve;    
16541 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
16542 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62  mber of unused b
16543 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65  ytes at end of e
16544 61 63 68 20 70 61 67 65 20 2a 2f 0d 0a 20 20 75  ach page */..  u
16545 33 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20  32 vfsFlags;    
16546 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
16547 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  ags for sqlite3_
16548 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0d 0a  vfs.xOpen() */..
16549 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65    u32 sectorSize
1654a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1654b 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20   Assumed sector 
1654c 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c  size during roll
1654d 62 61 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 70  back */..  int p
1654e 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
1654f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16550 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 70   of bytes in a p
16551 61 67 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 6d  age */..  Pgno m
16552 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20  xPgno;          
16553 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
16554 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66   allowed size of
16555 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
16556 0d 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53  ..  i64 journalS
16557 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  izeLimit;       
16558 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 6f  /* Size limit fo
16559 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  r persistent jou
1655a 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0d 0a 20  rnal files */.. 
1655b 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
1655c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1655d 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
1655e 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  base file */..  
1655f 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20  char *zJournal; 
16560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
16561 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ame of the journ
16562 61 6c 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e  al file */..  in
16563 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72  t (*xBusyHandler
16564 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e  )(void*); /* Fun
16565 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68  ction to call wh
16566 65 6e 20 62 75 73 79 20 2a 2f 0d 0a 20 20 76 6f  en busy */..  vo
16567 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
16568 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e  Arg;      /* Con
16569 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f  text argument fo
1656a 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  r xBusyHandler *
1656b 2f 0d 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e  /..  int nHit, n
1656c 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20  Miss;           
1656d 20 2f 2a 20 54 6f 74 61 6c 20 63 61 63 68 65 20   /* Total cache 
1656e 68 69 74 73 20 61 6e 64 20 6d 69 73 73 65 73 20  hits and misses 
1656f 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
16570 45 5f 54 45 53 54 0d 0a 20 20 69 6e 74 20 6e 52  E_TEST..  int nR
16571 65 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20  ead, nWrite;    
16572 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
16573 65 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 69  e pages read/wri
16574 74 74 65 6e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  tten */..#endif.
16575 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69  .  void (*xReini
16576 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b 20 2f  ter)(DbPage*); /
16577 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
16578 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 64 69  ine when reloadi
16579 6e 67 20 70 61 67 65 73 20 2a 2f 0d 0a 23 69 66  ng pages */..#if
1657a 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  def SQLITE_HAS_C
1657b 4f 44 45 43 0d 0a 20 20 76 6f 69 64 20 2a 28 2a  ODEC..  void *(*
1657c 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f  xCodec)(void*,vo
1657d 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f  id*,Pgno,int); /
1657e 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e  * Routine for en
1657f 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a  /decoding data *
16580 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64  /..  void (*xCod
16581 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64  ecSizeChng)(void
16582 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e  *,int,int); /* N
16583 6f 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69  otify of page si
16584 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f 0d 0a 20  ze changes */.. 
16585 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72   void (*xCodecFr
16586 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20  ee)(void*);     
16587 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72          /* Destr
16588 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f  uctor for the co
16589 64 65 63 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a  dec */..  void *
1658a 70 43 6f 64 65 63 3b 20 20 20 20 20 20 20 20 20  pCodec;         
1658b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
1658c 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65  rgument to xCode
1658d 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0d  c... methods */.
1658e 0a 23 65 6e 64 69 66 0d 0a 20 20 63 68 61 72 20  .#endif..  char 
1658f 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20  *pTmpSpace;     
16590 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 2e         /* Pager.
16591 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f  pageSize bytes o
16592 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20  f space for tmp 
16593 75 73 65 20 2a 2f 0d 0a 20 20 50 43 61 63 68 65  use */..  PCache
16594 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20   *pPCache;      
16595 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
16596 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f   to page cache o
16597 62 6a 65 63 74 20 2a 2f 0d 0a 23 69 66 6e 64 65  bject */..#ifnde
16598 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
16599 4c 0d 0a 20 20 57 61 6c 20 2a 70 57 61 6c 3b 20  L..  Wal *pWal; 
1659a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1659b 20 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64 20   /* Write-ahead 
1659c 6c 6f 67 20 75 73 65 64 20 62 79 20 22 6a 6f 75  log used by "jou
1659d 72 6e 61 6c 5f 6d 6f 64 65 3d 77 61 6c 22 20 2a  rnal_mode=wal" *
1659e 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 57 61 6c 3b  /..  char *zWal;
1659f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
165a0 20 2f 2a 20 46 69 6c 65 20 6e 61 6d 65 20 66 6f   /* File name fo
165a1 72 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f  r write-ahead lo
165a2 67 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b  g */..#endif..};
165a3 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66  ..../*..** The f
165a4 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
165a5 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63  variables hold c
165a6 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72  ounters used for
165a7 0d 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72  ..** testing pur
165a8 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65  poses only.  The
165a9 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20  se variables do 
165aa 6e 6f 74 20 65 78 69 73 74 20 69 6e 0d 0a 2a 2a  not exist in..**
165ab 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62   a non-testing b
165ac 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72  uild.  These var
165ad 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74  iables are not t
165ae 68 72 65 61 64 2d 73 61 66 65 2e 0d 0a 2a 2f 0d  hread-safe...*/.
165af 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
165b0 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  EST..SQLITE_API 
165b1 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65  int sqlite3_page
165b2 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d  r_readdb_count =
165b3 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   0;    /* Number
165b4 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72   of full pages r
165b5 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0d 0a  ead from DB */..
165b6 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
165b7 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
165b8 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20  tedb_count = 0; 
165b9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
165ba 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65  ull pages writte
165bb 6e 20 74 6f 20 44 42 20 2a 2f 0d 0a 53 51 4c 49  n to DB */..SQLI
165bc 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
165bd 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f  e3_pager_writej_
165be 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a  count = 0;    /*
165bf 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
165c0 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72   written to jour
165c1 6e 61 6c 20 2a 2f 0d 0a 23 20 64 65 66 69 6e 65  nal */..# define
165c2 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20   PAGER_INCR(v)  
165c3 76 2b 2b 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65  v++..#else..# de
165c4 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28  fine PAGER_INCR(
165c5 76 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a  v)..#endif......
165c6 0d 0a 2f 2a 0d 0a 2a 2a 20 4a 6f 75 72 6e 61 6c  ../*..** Journal
165c7 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74   files begin wit
165c8 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  h the following 
165c9 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54  magic string.  T
165ca 68 65 20 64 61 74 61 0d 0a 2a 2a 20 77 61 73 20  he data..** was 
165cb 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64  obtained from /d
165cc 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69  ev/random.  It i
165cd 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61  s used only as a
165ce 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0d 0a   sanity check...
165cf 2a 2a 0d 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72  **..** Since ver
165d0 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20  sion 2.8.0, the 
165d1 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63  journal format c
165d2 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e  ontains addition
165d3 61 6c 20 73 61 6e 69 74 79 0d 0a 2a 2a 20 63 68  al sanity..** ch
165d4 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  ecking informati
165d5 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65  on.  If the powe
165d6 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68  r fails while th
165d7 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69  e journal is bei
165d8 6e 67 0d 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20  ng..** written, 
165d9 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62  semi-random garb
165da 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 61  age data might a
165db 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75  ppear in the jou
165dc 72 6e 61 6c 0d 0a 2a 2a 20 66 69 6c 65 20 61 66  rnal..** file af
165dd 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73  ter power is res
165de 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74  tored.  If an at
165df 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61  tempt is then ma
165e0 64 65 0d 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74  de..** to roll t
165e1 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c  he journal back,
165e2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
165e3 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64  uld be corrupted
165e4 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61  .  The additiona
165e5 6c 0d 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65  l..** sanity che
165e6 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e  cking data is an
165e7 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63   attempt to disc
165e8 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65  over the garbage
165e9 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 6a 6f 75 72   in the..** jour
165ea 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69  nal and ignore i
165eb 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73  t...**..** The s
165ec 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69  anity checking i
165ed 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
165ee 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66  he new journal f
165ef 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74 73 0d 0a  ormat consists..
165f0 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74 20 63  ** of a 32-bit c
165f1 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20  hecksum on each 
165f2 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20 20 54  page of data.  T
165f3 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f 76 65  he checksum cove
165f4 72 73 20 62 6f 74 68 0d 0a 2a 2a 20 74 68 65 20  rs both..** the 
165f5 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  page number and 
165f6 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67 65  the pPager->page
165f7 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61  Size bytes of da
165f8 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65 2e  ta for the page.
165f9 0d 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20  ..** This cksum 
165fa 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
165fb 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f  o a 32-bit rando
165fc 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70  m value that app
165fd 65 61 72 73 20 69 6e 20 74 68 65 0d 0a 2a 2a 20  ears in the..** 
165fe 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67  journal file rig
165ff 68 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61  ht after the hea
16600 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d  der.  The random
16601 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20   initializer is 
16602 69 6d 70 6f 72 74 61 6e 74 2c 0d 0a 2a 2a 20 62  important,..** b
16603 65 63 61 75 73 65 20 67 61 72 62 61 67 65 20 64  ecause garbage d
16604 61 74 61 20 74 68 61 74 20 61 70 70 65 61 72 73  ata that appears
16605 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61   at the end of a
16606 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65   journal is like
16607 6c 79 0d 0a 2a 2a 20 64 61 74 61 20 74 68 61 74  ly..** data that
16608 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68   was once in oth
16609 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 61  er files that ha
1660a 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65  ve now been dele
1660b 74 65 64 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a  ted.  If the..**
1660c 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63 61   garbage data ca
1660d 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c  me from an obsol
1660e 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ete journal file
1660f 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20  , the checksums 
16610 6d 69 67 68 74 0d 0a 2a 2a 20 62 65 20 63 6f 72  might..** be cor
16611 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e  rect.  But by in
16612 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63  itializing the c
16613 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f  hecksum to rando
16614 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0d 0a 2a  m value which..*
16615 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66  * is different f
16616 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c  or every journal
16617 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68  , we minimize th
16618 61 74 20 72 69 73 6b 2e 0d 0a 2a 2f 0d 0a 73 74  at risk...*/..st
16619 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
1661a 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61  ned char aJourna
1661b 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0d 0a 20 20  lMagic[] = {..  
1661c 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35  0xd9, 0xd5, 0x05
1661d 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78  , 0xf9, 0x20, 0x
1661e 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0d  a1, 0x63, 0xd7,.
1661f 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  .};..../*..** Th
16620 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 66  e size of the of
16621 20 65 61 63 68 20 70 61 67 65 20 72 65 63 6f 72   each page recor
16622 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d in the journal
16623 20 69 73 20 67 69 76 65 6e 20 62 79 0d 0a 2a 2a   is given by..**
16624 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
16625 61 63 72 6f 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  acro...*/..#defi
16626 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a  ne JOURNAL_PG_SZ
16627 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67  (pPager)  ((pPag
16628 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20  er->pageSize) + 
16629 38 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  8)..../*..** The
1662a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
1662b 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61  size for this pa
1662c 67 65 72 2e 20 54 68 69 73 20 69 73 20 75 73 75  ger. This is usu
1662d 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 0d 0a  ally the same ..
1662e 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 73 69 6e  ** size as a sin
1662f 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e  gle disk sector.
16630 20 53 65 65 20 61 6c 73 6f 20 73 65 74 53 65 63   See also setSec
16631 74 6f 72 53 69 7a 65 28 29 2e 0d 0a 2a 2f 0d 0a  torSize()...*/..
16632 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f  #define JOURNAL_
16633 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28  HDR_SZ(pPager) (
16634 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
16635 7a 65 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ze)..../*..** Th
16636 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73  e macro MEMDB is
16637 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20   true if we are 
16638 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20  dealing with an 
16639 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
1663a 73 65 2e 0d 0a 2a 2a 20 57 65 20 64 6f 20 74 68  se...** We do th
1663b 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f  is as a macro so
1663c 20 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c   that if the SQL
1663d 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
1663e 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0d  B macro is set,.
1663f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
16640 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61   MEMDB will be a
16641 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68   constant and th
16642 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20  e compiler will 
16643 6f 70 74 69 6d 69 7a 65 0d 0a 2a 2a 20 6f 75 74  optimize..** out
16644 20 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64   code that would
16645 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e 0d   never execute..
16646 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
16647 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
16648 0d 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42  ..# define MEMDB
16649 20 30 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66   0..#else..# def
1664a 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72  ine MEMDB pPager
1664b 2d 3e 6d 65 6d 44 62 0d 0a 23 65 6e 64 69 66 0d  ->memDb..#endif.
1664c 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6d 61  .../*..** The ma
1664d 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65  ximum legal page
1664e 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31   number is (2^31
1664f 20 2d 20 31 29 2e 0d 0a 2a 2f 0d 0a 23 64 65 66   - 1)...*/..#def
16650 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47  ine PAGER_MAX_PG
16651 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0d 0a 0d  NO 2147483647...
16652 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 61 72 67 75  ./*..** The argu
16653 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63  ment to this mac
16654 72 6f 20 69 73 20 61 20 66 69 6c 65 20 64 65 73  ro is a file des
16655 63 72 69 70 74 6f 72 20 28 74 79 70 65 20 73 71  criptor (type sq
16656 6c 69 74 65 33 5f 66 69 6c 65 2a 29 2e 0d 0a 2a  lite3_file*)...*
16657 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74  * Return 0 if it
16658 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72   is not open, or
16659 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e   non-zero (but n
1665a 6f 74 20 31 29 20 69 66 20 69 74 20 69 73 2e 0d  ot 1) if it is..
1665b 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20  .**..** This is 
1665c 73 6f 20 74 68 61 74 20 65 78 70 72 65 73 73 69  so that expressi
1665d 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69 74 74  ons can be writt
1665e 65 6e 20 61 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  en as:..**..**  
1665f 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
16660 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0d  er->jfd) ){ ....
16661 0a 2a 2a 0d 0a 2a 2a 20 69 6e 73 74 65 61 64 20  .**..** instead 
16662 6f 66 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 69 66 28  of..**..**   if(
16663 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d   pPager->jfd->pM
16664 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e 0d 0a 2a  ethods ){ .....*
16665 2f 0d 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 65  /..#define isOpe
16666 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e 70  n(pFd) ((pFd)->p
16667 4d 65 74 68 6f 64 73 29 0d 0a 0d 0a 2f 2a 0d 0a  Methods)..../*..
16668 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
16669 66 20 74 68 69 73 20 70 61 67 65 72 20 75 73 65  f this pager use
1666a 73 20 61 20 77 72 69 74 65 2d 61 68 65 61 64 20  s a write-ahead 
1666b 6c 6f 67 20 69 6e 73 74 65 61 64 20 6f 66 20 74  log instead of t
1666c 68 65 20 75 73 75 61 6c 0d 0a 2a 2a 20 72 6f 6c  he usual..** rol
1666d 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 4f  lback journal. O
1666e 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0d  therwise false..
1666f 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
16670 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 73 74  ITE_OMIT_WAL..st
16671 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 55 73  atic int pagerUs
16672 65 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  eWal(Pager *pPag
16673 65 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 28  er){..  return (
16674 70 50 61 67 65 72 2d 3e 70 57 61 6c 21 3d 30 29  pPager->pWal!=0)
16675 3b 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 20 64  ;..}..#else..# d
16676 65 66 69 6e 65 20 70 61 67 65 72 55 73 65 57 61  efine pagerUseWa
16677 6c 28 78 29 20 30 0d 0a 23 20 64 65 66 69 6e 65  l(x) 0..# define
16678 20 70 61 67 65 72 52 6f 6c 6c 62 61 63 6b 57 61   pagerRollbackWa
16679 6c 28 78 29 20 30 0d 0a 23 20 64 65 66 69 6e 65  l(x) 0..# define
1667a 20 70 61 67 65 72 57 61 6c 46 72 61 6d 65 73 28   pagerWalFrames(
1667b 76 2c 77 2c 78 2c 79 29 20 30 0d 0a 23 20 64 65  v,w,x,y) 0..# de
1667c 66 69 6e 65 20 70 61 67 65 72 4f 70 65 6e 57 61  fine pagerOpenWa
1667d 6c 49 66 50 72 65 73 65 6e 74 28 7a 29 20 53 51  lIfPresent(z) SQ
1667e 4c 49 54 45 5f 4f 4b 0d 0a 23 20 64 65 66 69 6e  LITE_OK..# defin
1667f 65 20 70 61 67 65 72 42 65 67 69 6e 52 65 61 64  e pagerBeginRead
16680 54 72 61 6e 73 61 63 74 69 6f 6e 28 7a 29 20 53  Transaction(z) S
16681 51 4c 49 54 45 5f 4f 4b 0d 0a 23 65 6e 64 69 66  QLITE_OK..#endif
16682 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ....#ifndef NDEB
16683 55 47 20 0d 0a 2f 2a 0d 0a 2a 2a 20 55 73 61 67  UG ../*..** Usag
16684 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 61 73 73  e:..**..**   ass
16685 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65  ert( assert_page
16686 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20  r_state(pPager) 
16687 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  );..**..** This 
16688 66 75 6e 63 74 69 6f 6e 20 72 75 6e 73 20 6d 61  function runs ma
16689 6e 79 20 61 73 73 65 72 74 73 20 74 6f 20 74 72  ny asserts to tr
1668a 79 20 74 6f 20 66 69 6e 64 20 69 6e 63 6f 6e 73  y to find incons
1668b 69 73 74 65 6e 63 69 65 73 20 69 6e 0d 0a 2a 2a  istencies in..**
1668c 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74   the internal st
1668d 61 74 65 20 6f 66 20 74 68 65 20 50 61 67 65 72  ate of the Pager
1668e 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74   object...*/..st
1668f 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f  atic int assert_
16690 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65  pager_state(Page
16691 72 20 2a 70 29 7b 0d 0a 20 20 50 61 67 65 72 20  r *p){..  Pager 
16692 2a 70 50 61 67 65 72 20 3d 20 70 3b 0d 0a 0d 0a  *pPager = p;....
16693 20 20 2f 2a 20 53 74 61 74 65 20 6d 75 73 74 20    /* State must 
16694 62 65 20 76 61 6c 69 64 2e 20 2a 2f 0d 0a 20 20  be valid. */..  
16695 61 73 73 65 72 74 28 20 70 2d 3e 65 53 74 61 74  assert( p->eStat
16696 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 0d 0a 20  e==PAGER_OPEN.. 
16697 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61        || p->eSta
16698 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52  te==PAGER_READER
16699 0d 0a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65  ..       || p->e
1669a 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49  State==PAGER_WRI
1669b 54 45 52 5f 4c 4f 43 4b 45 44 0d 0a 20 20 20 20  TER_LOCKED..    
1669c 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d     || p->eState=
1669d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41  =PAGER_WRITER_CA
1669e 43 48 45 4d 4f 44 0d 0a 20 20 20 20 20 20 20 7c  CHEMOD..       |
1669f 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  | p->eState==PAG
166a0 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 0d  ER_WRITER_DBMOD.
166a1 0a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53  .       || p->eS
166a2 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
166a3 45 52 5f 46 49 4e 49 53 48 45 44 0d 0a 20 20 20  ER_FINISHED..   
166a4 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65      || p->eState
166a5 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 0d 0a 20  ==PAGER_ERROR.. 
166a6 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 67 61   );....  /* Rega
166a7 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 63 75  rdless of the cu
166a8 72 72 65 6e 74 20 73 74 61 74 65 2c 20 61 20 74  rrent state, a t
166a9 65 6d 70 2d 66 69 6c 65 20 63 6f 6e 6e 65 63 74  emp-file connect
166aa 69 6f 6e 20 61 6c 77 61 79 73 20 62 65 68 61 76  ion always behav
166ab 65 73 0d 0a 20 20 2a 2a 20 61 73 20 69 66 20 69  es..  ** as if i
166ac 74 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69  t has an exclusi
166ad 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ve lock on the d
166ae 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 74  atabase file. It
166af 20 6e 65 76 65 72 20 75 70 64 61 74 65 73 0d 0a   never updates..
166b0 20 20 2a 2a 20 74 68 65 20 63 68 61 6e 67 65 2d    ** the change-
166b1 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 2c 20 73  counter field, s
166b2 6f 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e  o the changeCoun
166b3 74 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c  tDone flag is al
166b4 77 61 79 73 20 73 65 74 2e 0d 0a 20 20 2a 2f 0d  ways set...  */.
166b5 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 74 65  .  assert( p->te
166b6 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e  mpFile==0 || p->
166b7 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
166b8 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 61 73 73 65  _LOCK );..  asse
166b9 72 74 28 20 70 2d 3e 74 65 6d 70 46 69 6c 65 3d  rt( p->tempFile=
166ba 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63 68  =0 || pPager->ch
166bb 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29 3b  angeCountDone );
166bc 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
166bd 75 73 65 4a 6f 75 72 6e 61 6c 20 66 6c 61 67 20  useJournal flag 
166be 69 73 20 63 6c 65 61 72 2c 20 74 68 65 20 6a 6f  is clear, the jo
166bf 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74 20  urnal-mode must 
166c0 62 65 20 22 4f 46 46 22 2e 20 0d 0a 20 20 2a 2a  be "OFF". ..  **
166c1 20 41 6e 64 20 69 66 20 74 68 65 20 6a 6f 75 72   And if the jour
166c2 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 22 4f 46 46  nal-mode is "OFF
166c3 22 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ", the journal f
166c4 69 6c 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  ile must not be 
166c5 6f 70 65 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61  open...  */..  a
166c6 73 73 65 72 74 28 20 70 2d 3e 6a 6f 75 72 6e 61  ssert( p->journa
166c7 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
166c8 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20  RNALMODE_OFF || 
166c9 70 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b  p->useJournal );
166ca 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6a  ..  assert( p->j
166cb 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
166cc 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
166cd 46 20 7c 7c 20 21 69 73 4f 70 65 6e 28 70 2d 3e  F || !isOpen(p->
166ce 6a 66 64 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  jfd) );....  /* 
166cf 43 68 65 63 6b 20 74 68 61 74 20 4d 45 4d 44 42  Check that MEMDB
166d0 20 69 6d 70 6c 69 65 73 20 6e 6f 53 79 6e 63 2e   implies noSync.
166d1 20 41 6e 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   And an in-memor
166d2 79 20 6a 6f 75 72 6e 61 6c 2e 20 53 69 6e 63 65  y journal. Since
166d3 20 0d 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 61   ..  ** this mea
166d4 6e 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  ns an in-memory 
166d5 70 61 67 65 72 20 70 65 72 66 6f 72 6d 73 20 6e  pager performs n
166d6 6f 20 49 4f 20 61 74 20 61 6c 6c 2c 20 69 74 20  o IO at all, it 
166d7 63 61 6e 6e 6f 74 20 65 6e 63 6f 75 6e 74 65 72  cannot encounter
166d8 20 0d 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 53   ..  ** either S
166d9 51 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53  QLITE_IOERR or S
166da 51 4c 49 54 45 5f 46 55 4c 4c 20 64 75 72 69 6e  QLITE_FULL durin
166db 67 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 77 68  g rollback or wh
166dc 69 6c 65 20 66 69 6e 61 6c 69 7a 69 6e 67 20 0d  ile finalizing .
166dd 0a 20 20 2a 2a 20 61 20 6a 6f 75 72 6e 61 6c 20  .  ** a journal 
166de 66 69 6c 65 2e 20 28 61 6c 74 68 6f 75 67 68 20  file. (although 
166df 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f  the in-memory jo
166e0 75 72 6e 61 6c 20 69 6d 70 6c 65 6d 65 6e 74 61  urnal implementa
166e1 74 69 6f 6e 20 6d 61 79 20 0d 0a 20 20 2a 2a 20  tion may ..  ** 
166e2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
166e3 45 52 52 5f 4e 4f 4d 45 4d 20 77 68 69 6c 65 20  ERR_NOMEM while 
166e4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
166e5 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65   is being writte
166e6 6e 29 2e 20 49 74 20 0d 0a 20 20 2a 2a 20 69 73  n). It ..  ** is
166e7 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 74 20 70   therefore not p
166e8 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 6e 20 69  ossible for an i
166e9 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 74  n-memory pager t
166ea 6f 20 65 6e 74 65 72 20 74 68 65 20 45 52 52 4f  o enter the ERRO
166eb 52 20 0d 0a 20 20 2a 2a 20 73 74 61 74 65 2e 0d  R ..  ** state..
166ec 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 4d 45 4d  .  */..  if( MEM
166ed 44 42 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  DB ){..    asser
166ee 74 28 20 70 2d 3e 6e 6f 53 79 6e 63 20 29 3b 0d  t( p->noSync );.
166ef 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
166f0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
166f1 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
166f2 46 46 20 0d 0a 20 20 20 20 20 20 20 20 20 7c 7c  FF ..         ||
166f3 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d   p->journalMode=
166f4 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
166f5 44 45 5f 4d 45 4d 4f 52 59 20 0d 0a 20 20 20 20  DE_MEMORY ..    
166f6 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
166f7 70 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52  p->eState!=PAGER
166f8 5f 45 52 52 4f 52 20 26 26 20 70 2d 3e 65 53 74  _ERROR && p->eSt
166f9 61 74 65 21 3d 50 41 47 45 52 5f 4f 50 45 4e 20  ate!=PAGER_OPEN 
166fa 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
166fb 70 61 67 65 72 55 73 65 57 61 6c 28 70 29 3d 3d  pagerUseWal(p)==
166fc 30 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  0 );..  }....  /
166fd 2a 20 49 66 20 63 68 61 6e 67 65 43 6f 75 6e 74  * If changeCount
166fe 44 6f 6e 65 20 69 73 20 73 65 74 2c 20 61 20 52  Done is set, a R
166ff 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 72 20  ESERVED lock or 
16700 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 65 20  greater must be 
16701 68 65 6c 64 0d 0a 20 20 2a 2a 20 6f 6e 20 74 68  held..  ** on th
16702 65 20 66 69 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20  e file...  */.. 
16703 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
16704 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
16705 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65  ==0 || pPager->e
16706 4c 6f 63 6b 3e 3d 52 45 53 45 52 56 45 44 5f 4c  Lock>=RESERVED_L
16707 4f 43 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  OCK );..  assert
16708 28 20 70 2d 3e 65 4c 6f 63 6b 21 3d 50 45 4e 44  ( p->eLock!=PEND
16709 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a 20  ING_LOCK );.... 
1670a 20 73 77 69 74 63 68 28 20 70 2d 3e 65 53 74 61   switch( p->eSta
1670b 74 65 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  te ){..    case 
1670c 50 41 47 45 52 5f 4f 50 45 4e 3a 0d 0a 20 20 20  PAGER_OPEN:..   
1670d 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44     assert( !MEMD
1670e 42 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  B );..      asse
1670f 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  rt( pPager->errC
16710 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  ode==SQLITE_OK )
16711 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
16712 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
16713 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
16714 50 43 61 63 68 65 29 3d 3d 30 20 7c 7c 20 70 50  PCache)==0 || pP
16715 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
16716 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
16717 0a 0d 0a 20 20 20 20 63 61 73 65 20 50 41 47 45  ...    case PAGE
16718 52 5f 52 45 41 44 45 52 3a 0d 0a 20 20 20 20 20  R_READER:..     
16719 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1671a 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45  >errCode==SQLITE
1671b 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  _OK );..      as
1671c 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 21 3d  sert( p->eLock!=
1671d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0d  UNKNOWN_LOCK );.
1671e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1671f 2d 3e 65 4c 6f 63 6b 3e 3d 53 48 41 52 45 44 5f  ->eLock>=SHARED_
16720 4c 4f 43 4b 20 7c 7c 20 70 2d 3e 6e 6f 52 65 61  LOCK || p->noRea
16721 64 6c 6f 63 6b 20 29 3b 0d 0a 20 20 20 20 20 20  dlock );..      
16722 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 63 61  break;....    ca
16723 73 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f  se PAGER_WRITER_
16724 4c 4f 43 4b 45 44 3a 0d 0a 20 20 20 20 20 20 61  LOCKED:..      a
16725 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 21  ssert( p->eLock!
16726 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b  =UNKNOWN_LOCK );
16727 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
16728 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
16729 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20  =SQLITE_OK );.. 
1672a 20 20 20 20 20 69 66 28 20 21 70 61 67 65 72 55       if( !pagerU
1672b 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b  seWal(pPager) ){
1672c 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
1672d 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 52 45 53 45  ( p->eLock>=RESE
1672e 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20  RVED_LOCK );..  
1672f 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73      }..      ass
16730 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
16731 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 4f  ize==pPager->dbO
16732 72 69 67 53 69 7a 65 20 29 3b 0d 0a 20 20 20 20  rigSize );..    
16733 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
16734 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50  ->dbOrigSize==pP
16735 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
16736 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
16737 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  t( pPager->dbOri
16738 67 53 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64  gSize==pPager->d
16739 62 48 69 6e 74 53 69 7a 65 20 29 3b 0d 0a 20 20  bHintSize );..  
1673a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1673b 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 3d 3d 30  er->setMaster==0
1673c 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b   );..      break
1673d 3b 0d 0a 0d 0a 20 20 20 20 63 61 73 65 20 50 41  ;....    case PA
1673e 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45  GER_WRITER_CACHE
1673f 4d 4f 44 3a 0d 0a 20 20 20 20 20 20 61 73 73 65  MOD:..      asse
16740 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 21 3d 55 4e  rt( p->eLock!=UN
16741 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0d 0a 20  KNOWN_LOCK );.. 
16742 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
16743 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
16744 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20  LITE_OK );..    
16745 20 20 69 66 28 20 21 70 61 67 65 72 55 73 65 57    if( !pagerUseW
16746 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0d 0a 20  al(pPager) ){.. 
16747 20 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20         /* It is 
16748 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 69 66  possible that if
16749 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 77 61   journal_mode=wa
1674a 6c 20 68 65 72 65 20 74 68 61 74 20 6e 65 69 74  l here that neit
1674b 68 65 72 20 74 68 65 0d 0a 20 20 20 20 20 20 20  her the..       
1674c 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   ** journal file
1674d 20 6e 6f 72 20 74 68 65 20 57 41 4c 20 66 69 6c   nor the WAL fil
1674e 65 20 61 72 65 20 6f 70 65 6e 2e 20 54 68 69 73  e are open. This
1674f 20 68 61 70 70 65 6e 73 20 64 75 72 69 6e 67 0d   happens during.
16750 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 72 6f  .        ** a ro
16751 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69  llback transacti
16752 6f 6e 20 74 68 61 74 20 73 77 69 74 63 68 65 73  on that switches
16753 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 5f 6d 6f   from journal_mo
16754 64 65 3d 6f 66 66 0d 0a 20 20 20 20 20 20 20 20  de=off..        
16755 2a 2a 20 74 6f 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  ** to journal_mo
16756 64 65 3d 77 61 6c 2e 0d 0a 20 20 20 20 20 20 20  de=wal...       
16757 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 61 73 73   */..        ass
16758 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 52  ert( p->eLock>=R
16759 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0d  ESERVED_LOCK );.
1675a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1675b 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20   isOpen(p->jfd) 
1675c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  ..             |
1675d 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  | p->journalMode
1675e 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
1675f 4f 44 45 5f 4f 46 46 20 0d 0a 20 20 20 20 20 20  ODE_OFF ..      
16760 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75         || p->jou
16761 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
16762 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20  JOURNALMODE_WAL 
16763 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d 0a 20 20  ..        );..  
16764 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73      }..      ass
16765 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f  ert( pPager->dbO
16766 72 69 67 53 69 7a 65 3d 3d 70 50 61 67 65 72 2d  rigSize==pPager-
16767 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 3b 0d 0a  >dbFileSize );..
16768 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
16769 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
1676a 3d 3d 70 50 61 67 65 72 2d 3e 64 62 48 69 6e 74  ==pPager->dbHint
1676b 53 69 7a 65 20 29 3b 0d 0a 20 20 20 20 20 20 62  Size );..      b
1676c 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 63 61 73  reak;....    cas
1676d 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44  e PAGER_WRITER_D
1676e 42 4d 4f 44 3a 0d 0a 20 20 20 20 20 20 61 73 73  BMOD:..      ass
1676f 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 45  ert( p->eLock==E
16770 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b  XCLUSIVE_LOCK );
16771 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
16772 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
16773 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20  =SQLITE_OK );.. 
16774 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 61       assert( !pa
16775 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72  gerUseWal(pPager
16776 29 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  ) );..      asse
16777 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 45 58  rt( p->eLock>=EX
16778 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0d  CLUSIVE_LOCK );.
16779 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
1677a 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20 0d 0a  sOpen(p->jfd) ..
1677b 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d             || p-
1677c 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
1677d 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
1677e 4f 46 46 20 0d 0a 20 20 20 20 20 20 20 20 20 20  OFF ..          
1677f 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f   || p->journalMo
16780 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
16781 4c 4d 4f 44 45 5f 57 41 4c 20 0d 0a 20 20 20 20  LMODE_WAL ..    
16782 20 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65    );..      asse
16783 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72  rt( pPager->dbOr
16784 69 67 53 69 7a 65 3c 3d 70 50 61 67 65 72 2d 3e  igSize<=pPager->
16785 64 62 48 69 6e 74 53 69 7a 65 20 29 3b 0d 0a 20  dbHintSize );.. 
16786 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20       break;.... 
16787 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57 52     case PAGER_WR
16788 49 54 45 52 5f 46 49 4e 49 53 48 45 44 3a 0d 0a  ITER_FINISHED:..
16789 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1678a 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56  >eLock==EXCLUSIV
1678b 45 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 20 20 20  E_LOCK );..     
1678c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1678d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45  >errCode==SQLITE
1678e 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  _OK );..      as
1678f 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57  sert( !pagerUseW
16790 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20  al(pPager) );.. 
16791 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f       assert( isO
16792 70 65 6e 28 70 2d 3e 6a 66 64 29 20 0d 0a 20 20  pen(p->jfd) ..  
16793 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a           || p->j
16794 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
16795 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
16796 46 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 7c  F ..           |
16797 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  | p->journalMode
16798 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
16799 4f 44 45 5f 57 41 4c 20 0d 0a 20 20 20 20 20 20  ODE_WAL ..      
1679a 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
1679b 0d 0a 0d 0a 20 20 20 20 63 61 73 65 20 50 41 47  ....    case PAG
1679c 45 52 5f 45 52 52 4f 52 3a 0d 0a 20 20 20 20 20  ER_ERROR:..     
1679d 20 2f 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62   /* There must b
1679e 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f  e at least one o
1679f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
167a0 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65  ence to the page
167a1 72 20 69 66 0d 0a 20 20 20 20 20 20 2a 2a 20 69  r if..      ** i
167a2 6e 20 45 52 52 4f 52 20 73 74 61 74 65 2e 20 4f  n ERROR state. O
167a3 74 68 65 72 77 69 73 65 20 74 68 65 20 70 61 67  therwise the pag
167a4 65 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61  er should have a
167a5 6c 72 65 61 64 79 20 64 72 6f 70 70 65 64 0d 0a  lready dropped..
167a6 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f        ** back to
167a7 20 4f 50 45 4e 20 73 74 61 74 65 2e 0d 0a 20 20   OPEN state...  
167a8 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73      */..      as
167a9 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72  sert( pPager->er
167aa 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 4f 4b  rCode!=SQLITE_OK
167ab 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
167ac 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  t( sqlite3Pcache
167ad 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
167ae 3e 70 50 43 61 63 68 65 29 3e 30 20 29 3b 0d 0a  >pPCache)>0 );..
167af 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
167b0 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b  }....  return 1;
167b1 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 69  ..}..#endif /* i
167b2 66 6e 64 65 66 20 4e 44 45 42 55 47 20 2a 2f 0d  fndef NDEBUG */.
167b3 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
167b4 5f 44 45 42 55 47 20 0d 0a 2f 2a 0d 0a 2a 2a 20  _DEBUG ../*..** 
167b5 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
167b6 20 74 6f 20 61 20 68 75 6d 61 6e 20 72 65 61 64   to a human read
167b7 61 62 6c 65 20 73 74 72 69 6e 67 20 69 6e 20 61  able string in a
167b8 20 73 74 61 74 69 63 20 62 75 66 66 65 72 0d 0a   static buffer..
167b9 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ** containing th
167ba 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50  e state of the P
167bb 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73  ager object pass
167bc 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
167bd 74 2e 20 54 68 69 73 0d 0a 2a 2a 20 69 73 20 69  t. This..** is i
167be 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73  ntended to be us
167bf 65 64 20 77 69 74 68 69 6e 20 64 65 62 75 67 67  ed within debugg
167c0 65 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ers. For example
167c1 2c 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74  , as an alternat
167c2 69 76 65 0d 0a 2a 2a 20 74 6f 20 22 70 72 69 6e  ive..** to "prin
167c3 74 20 2a 70 50 61 67 65 72 22 20 69 6e 20 67 64  t *pPager" in gd
167c4 62 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 28 67 64 62 29  b:..**..** (gdb)
167c5 20 70 72 69 6e 74 66 20 22 25 73 22 2c 20 70 72   printf "%s", pr
167c6 69 6e 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  int_pager_state(
167c7 70 50 61 67 65 72 29 0d 0a 2a 2f 0d 0a 73 74 61  pPager)..*/..sta
167c8 74 69 63 20 63 68 61 72 20 2a 70 72 69 6e 74 5f  tic char *print_
167c9 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65  pager_state(Page
167ca 72 20 2a 70 29 7b 0d 0a 20 20 73 74 61 74 69 63  r *p){..  static
167cb 20 63 68 61 72 20 7a 52 65 74 5b 31 30 32 34 5d   char zRet[1024]
167cc 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73  ;....  sqlite3_s
167cd 6e 70 72 69 6e 74 66 28 31 30 32 34 2c 20 7a 52  nprintf(1024, zR
167ce 65 74 2c 0d 0a 20 20 20 20 20 20 22 46 69 6c 65  et,..      "File
167cf 6e 61 6d 65 3a 20 20 20 20 20 20 25 73 5c 6e 22  name:      %s\n"
167d0 0d 0a 20 20 20 20 20 20 22 53 74 61 74 65 3a 20  ..      "State: 
167d1 20 20 20 20 20 20 20 20 25 73 20 65 72 72 43 6f          %s errCo
167d2 64 65 3d 25 64 5c 6e 22 0d 0a 20 20 20 20 20 20  de=%d\n"..      
167d3 22 4c 6f 63 6b 3a 20 20 20 20 20 20 20 20 20 20  "Lock:          
167d4 25 73 5c 6e 22 0d 0a 20 20 20 20 20 20 22 4c 6f  %s\n"..      "Lo
167d5 63 6b 69 6e 67 20 6d 6f 64 65 3a 20 20 6c 6f 63  cking mode:  loc
167d6 6b 69 6e 67 5f 6d 6f 64 65 3d 25 73 5c 6e 22 0d  king_mode=%s\n".
167d7 0a 20 20 20 20 20 20 22 4a 6f 75 72 6e 61 6c 20  .      "Journal 
167d8 6d 6f 64 65 3a 20 20 6a 6f 75 72 6e 61 6c 5f 6d  mode:  journal_m
167d9 6f 64 65 3d 25 73 5c 6e 22 0d 0a 20 20 20 20 20  ode=%s\n"..     
167da 20 22 42 61 63 6b 69 6e 67 20 73 74 6f 72 65 3a   "Backing store:
167db 20 74 65 6d 70 46 69 6c 65 3d 25 64 20 6d 65 6d   tempFile=%d mem
167dc 44 62 3d 25 64 20 75 73 65 4a 6f 75 72 6e 61 6c  Db=%d useJournal
167dd 3d 25 64 5c 6e 22 0d 0a 20 20 20 20 20 20 22 4a  =%d\n"..      "J
167de 6f 75 72 6e 61 6c 3a 20 20 20 20 20 20 20 6a 6f  ournal:       jo
167df 75 72 6e 61 6c 4f 66 66 3d 25 6c 6c 64 20 6a 6f  urnalOff=%lld jo
167e0 75 72 6e 61 6c 48 64 72 3d 25 6c 6c 64 5c 6e 22  urnalHdr=%lld\n"
167e1 0d 0a 20 20 20 20 20 20 22 53 69 7a 65 3a 20 20  ..      "Size:  
167e2 20 20 20 20 20 20 20 20 64 62 73 69 7a 65 3d 25          dbsize=%
167e3 64 20 64 62 4f 72 69 67 53 69 7a 65 3d 25 64 20  d dbOrigSize=%d 
167e4 64 62 46 69 6c 65 53 69 7a 65 3d 25 64 5c 6e 22  dbFileSize=%d\n"
167e5 0d 0a 20 20 20 20 20 20 2c 20 70 2d 3e 7a 46 69  ..      , p->zFi
167e6 6c 65 6e 61 6d 65 0d 0a 20 20 20 20 20 20 2c 20  lename..      , 
167e7 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  p->eState==PAGER
167e8 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 20 20 20  _OPEN           
167e9 20 3f 20 22 4f 50 45 4e 22 20 3a 0d 0a 20 20 20   ? "OPEN" :..   
167ea 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d       p->eState==
167eb 50 41 47 45 52 5f 52 45 41 44 45 52 20 20 20 20  PAGER_READER    
167ec 20 20 20 20 20 20 3f 20 22 52 45 41 44 45 52 22        ? "READER"
167ed 20 3a 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65   :..        p->e
167ee 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49  State==PAGER_WRI
167ef 54 45 52 5f 4c 4f 43 4b 45 44 20 20 20 3f 20 22  TER_LOCKED   ? "
167f0 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 22 20 3a  WRITER_LOCKED" :
167f1 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74  ..        p->eSt
167f2 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45  ate==PAGER_WRITE
167f3 52 5f 43 41 43 48 45 4d 4f 44 20 3f 20 22 57 52  R_CACHEMOD ? "WR
167f4 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 22 20 3a  ITER_CACHEMOD" :
167f5 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74  ..        p->eSt
167f6 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45  ate==PAGER_WRITE
167f7 52 5f 44 42 4d 4f 44 20 20 20 20 3f 20 22 57 52  R_DBMOD    ? "WR
167f8 49 54 45 52 5f 44 42 4d 4f 44 22 20 3a 0d 0a 20  ITER_DBMOD" :.. 
167f9 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65         p->eState
167fa 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46  ==PAGER_WRITER_F
167fb 49 4e 49 53 48 45 44 20 3f 20 22 57 52 49 54 45  INISHED ? "WRITE
167fc 52 5f 46 49 4e 49 53 48 45 44 22 20 3a 0d 0a 20  R_FINISHED" :.. 
167fd 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65         p->eState
167fe 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 20 20  ==PAGER_ERROR   
167ff 20 20 20 20 20 20 20 20 3f 20 22 45 52 52 4f 52          ? "ERROR
16800 22 20 3a 20 22 3f 65 72 72 6f 72 3f 22 0d 0a 20  " : "?error?".. 
16801 20 20 20 20 20 2c 20 28 69 6e 74 29 70 2d 3e 65       , (int)p->e
16802 72 72 43 6f 64 65 0d 0a 20 20 20 20 20 20 2c 20  rrCode..      , 
16803 70 2d 3e 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43  p->eLock==NO_LOC
16804 4b 20 20 20 20 20 20 20 20 20 3f 20 22 4e 4f 5f  K         ? "NO_
16805 4c 4f 43 4b 22 20 3a 0d 0a 20 20 20 20 20 20 20  LOCK" :..       
16806 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 53 45 52   p->eLock==RESER
16807 56 45 44 5f 4c 4f 43 4b 20 20 20 3f 20 22 52 45  VED_LOCK   ? "RE
16808 53 45 52 56 45 44 22 20 3a 0d 0a 20 20 20 20 20  SERVED" :..     
16809 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43     p->eLock==EXC
1680a 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 3f 20 22  LUSIVE_LOCK  ? "
1680b 45 58 43 4c 55 53 49 56 45 22 20 3a 0d 0a 20 20  EXCLUSIVE" :..  
1680c 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d        p->eLock==
1680d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
1680e 3f 20 22 53 48 41 52 45 44 22 20 3a 0d 0a 20 20  ? "SHARED" :..  
1680f 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d        p->eLock==
16810 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 20 20 20  UNKNOWN_LOCK    
16811 3f 20 22 55 4e 4b 4e 4f 57 4e 22 20 3a 20 22 3f  ? "UNKNOWN" : "?
16812 65 72 72 6f 72 3f 22 0d 0a 20 20 20 20 20 20 2c  error?"..      ,
16813 20 70 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64   p->exclusiveMod
16814 65 20 3f 20 22 65 78 63 6c 75 73 69 76 65 22 20  e ? "exclusive" 
16815 3a 20 22 6e 6f 72 6d 61 6c 22 0d 0a 20 20 20 20  : "normal"..    
16816 20 20 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f    , p->journalMo
16817 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
16818 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 3f  LMODE_MEMORY   ?
16819 20 22 6d 65 6d 6f 72 79 22 20 3a 0d 0a 20 20 20   "memory" :..   
1681a 20 20 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d       p->journalM
1681b 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
1681c 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20  ALMODE_OFF      
1681d 3f 20 22 6f 66 66 22 20 3a 0d 0a 20 20 20 20 20  ? "off" :..     
1681e 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64     p->journalMod
1681f 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
16820 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 3f 20  MODE_DELETE   ? 
16821 22 64 65 6c 65 74 65 22 20 3a 0d 0a 20 20 20 20  "delete" :..    
16822 20 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f      p->journalMo
16823 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
16824 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 3f  LMODE_PERSIST  ?
16825 20 22 70 65 72 73 69 73 74 22 20 3a 0d 0a 20 20   "persist" :..  
16826 20 20 20 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c        p->journal
16827 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
16828 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
16829 20 3f 20 22 74 72 75 6e 63 61 74 65 22 20 3a 0d   ? "truncate" :.
1682a 0a 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f 75 72  .        p->jour
1682b 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
1682c 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 20  OURNALMODE_WAL  
1682d 20 20 20 20 3f 20 22 77 61 6c 22 20 3a 20 22 3f      ? "wal" : "?
1682e 65 72 72 6f 72 3f 22 0d 0a 20 20 20 20 20 20 2c  error?"..      ,
1682f 20 28 69 6e 74 29 70 2d 3e 74 65 6d 70 46 69 6c   (int)p->tempFil
16830 65 2c 20 28 69 6e 74 29 70 2d 3e 6d 65 6d 44 62  e, (int)p->memDb
16831 2c 20 28 69 6e 74 29 70 2d 3e 75 73 65 4a 6f 75  , (int)p->useJou
16832 72 6e 61 6c 0d 0a 20 20 20 20 20 20 2c 20 70 2d  rnal..      , p-
16833 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 2d 3e  >journalOff, p->
16834 6a 6f 75 72 6e 61 6c 48 64 72 0d 0a 20 20 20 20  journalHdr..    
16835 20 20 2c 20 28 69 6e 74 29 70 2d 3e 64 62 53 69    , (int)p->dbSi
16836 7a 65 2c 20 28 69 6e 74 29 70 2d 3e 64 62 4f 72  ze, (int)p->dbOr
16837 69 67 53 69 7a 65 2c 20 28 69 6e 74 29 70 2d 3e  igSize, (int)p->
16838 64 62 46 69 6c 65 53 69 7a 65 0d 0a 20 20 29 3b  dbFileSize..  );
16839 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 7a 52 65  ....  return zRe
1683a 74 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  t;..}..#endif...
1683b 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./*..** Return t
1683c 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63  rue if it is nec
1683d 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
1683e 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74  page *pPg into t
1683f 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0d  he sub-journal..
16840 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73  .** A page needs
16841 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69   to be written i
16842 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  nto the sub-jour
16843 6e 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69  nal if there exi
16844 73 74 73 20 6f 6e 65 0d 0a 2a 2a 20 6f 72 20 6d  sts one..** or m
16845 6f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  ore open savepoi
16846 6e 74 73 20 66 6f 72 20 77 68 69 63 68 3a 0d 0a  nts for which:..
16847 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70  **..**   * The p
16848 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65  age-number is le
16849 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1684a 20 74 6f 20 50 61 67 65 72 53 61 76 65 70 6f 69   to PagerSavepoi
1684b 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 0d 0a 2a  nt.nOrig, and..*
1684c 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20 63 6f  *   * The bit co
1684d 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
1684e 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69  he page-number i
1684f 73 20 6e 6f 74 20 73 65 74 20 69 6e 0d 0a 2a 2a  s not set in..**
16850 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f       PagerSavepo
16851 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  int.pInSavepoint
16852 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
16853 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  t subjRequiresPa
16854 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0d  ge(PgHdr *pPg){.
16855 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70  .  Pgno pgno = p
16856 50 67 2d 3e 70 67 6e 6f 3b 0d 0a 20 20 50 61 67  Pg->pgno;..  Pag
16857 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
16858 2d 3e 70 50 61 67 65 72 3b 0d 0a 20 20 69 6e 74  ->pPager;..  int
16859 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20   i;..  for(i=0; 
1685a 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  i<pPager->nSavep
1685b 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  oint; i++){..   
1685c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
1685d 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53  *p = &pPager->aS
1685e 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0d 0a 20 20  avepoint[i];..  
1685f 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d    if( p->nOrig>=
16860 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c 69 74  pgno && 0==sqlit
16861 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2d 3e  e3BitvecTest(p->
16862 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67  pInSavepoint, pg
16863 6e 6f 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  no) ){..      re
16864 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 1;..    }..
16865 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
16866 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
16867 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
16868 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
16869 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
1686a 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  file...*/..stati
1686b 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72  c int pageInJour
1686c 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b  nal(PgHdr *pPg){
1686d 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
1686e 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67  e3BitvecTest(pPg
1686f 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ->pPager->pInJou
16870 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  rnal, pPg->pgno)
16871 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
16872 65 61 64 20 61 20 33 32 2d 62 69 74 20 69 6e 74  ead a 32-bit int
16873 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 67 69  eger from the gi
16874 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  ven file descrip
16875 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20  tor.  Store the 
16876 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 74 68 61 74  integer..** that
16877 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70 52 65   is read in *pRe
16878 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  s.  Return SQLIT
16879 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
1687a 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 61 6e  ng worked, or an
1687b 0d 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  ..** error code 
1687c 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  is something goe
1687d 73 20 77 72 6f 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a  s wrong...**..**
1687e 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20   All values are 
1687f 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61  stored on disk a
16880 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0d 0a 2a  s big-endian...*
16881 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  /..static int re
16882 61 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33  ad32bits(sqlite3
16883 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f  _file *fd, i64 o
16884 66 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73  ffset, u32 *pRes
16885 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ){..  unsigned c
16886 68 61 72 20 61 63 5b 34 5d 3b 0d 0a 20 20 69 6e  har ac[4];..  in
16887 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  t rc = sqlite3Os
16888 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73 69 7a  Read(fd, ac, siz
16889 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 74 29  eof(ac), offset)
1688a 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
1688b 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 2a  ITE_OK ){..    *
1688c 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65  pRes = sqlite3Ge
1688d 74 34 62 79 74 65 28 61 63 29 3b 0d 0a 20 20 7d  t4byte(ac);..  }
1688e 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
1688f 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74  }..../*..** Writ
16890 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
16891 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  er into a string
16892 20 62 75 66 66 65 72 20 69 6e 20 62 69 67 2d 65   buffer in big-e
16893 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64 65 72  ndian byte order
16894 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 70  ...*/..#define p
16895 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 73  ut32bits(A,B)  s
16896 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 28  qlite3Put4byte((
16897 75 38 2a 29 41 2c 42 29 0d 0a 0d 0a 0d 0a 2f 2a  u8*)A,B)....../*
16898 0d 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d  ..** Write a 32-
16899 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f  bit integer into
1689a 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20   the given file 
1689b 64 65 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74  descriptor.  Ret
1689c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 2a  urn SQLITE_OK..*
1689d 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  * on success or 
1689e 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
1689f 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20   something goes 
168a0 77 72 6f 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  wrong...*/..stat
168a1 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 69  ic int write32bi
168a2 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ts(sqlite3_file 
168a3 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c  *fd, i64 offset,
168a4 20 75 33 32 20 76 61 6c 29 7b 0d 0a 20 20 63 68   u32 val){..  ch
168a5 61 72 20 61 63 5b 34 5d 3b 0d 0a 20 20 70 75 74  ar ac[4];..  put
168a6 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b  32bits(ac, val);
168a7 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
168a8 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63  e3OsWrite(fd, ac
168a9 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0d 0a 7d  , 4, offset);..}
168aa 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 6c 6f 63  ..../*..** Unloc
168ab 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  k the database f
168ac 69 6c 65 20 74 6f 20 6c 65 76 65 6c 20 65 4c 6f  ile to level eLo
168ad 63 6b 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62  ck, which must b
168ae 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
168af 0d 0a 2a 2a 20 6f 72 20 53 48 41 52 45 44 5f 4c  ..** or SHARED_L
168b0 4f 43 4b 2e 20 52 65 67 61 72 64 6c 65 73 73 20  OCK. Regardless 
168b1 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
168b2 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 78 55  t the call to xU
168b3 6e 6c 6f 63 6b 28 29 0d 0a 2a 2a 20 73 75 63 63  nlock()..** succ
168b4 65 65 64 73 2c 20 73 65 74 20 74 68 65 20 50 61  eeds, set the Pa
168b5 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62  ger.eLock variab
168b6 6c 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  le to match the 
168b7 28 61 74 74 65 6d 70 74 65 64 29 20 6e 65 77 20  (attempted) new 
168b8 6c 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78  lock...**..** Ex
168b9 63 65 70 74 2c 20 69 66 20 50 61 67 65 72 2e 65  cept, if Pager.e
168ba 4c 6f 63 6b 20 69 73 20 73 65 74 20 74 6f 20 55  Lock is set to U
168bb 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68 65 6e  NKNOWN_LOCK when
168bc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
168bd 73 0d 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 64 6f  s..** called, do
168be 20 6e 6f 74 20 6d 6f 64 69 66 79 20 69 74 2e 20   not modify it. 
168bf 53 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  See the comment 
168c0 61 62 6f 76 65 20 74 68 65 20 23 64 65 66 69 6e  above the #defin
168c1 65 20 6f 66 20 0d 0a 2a 2a 20 55 4e 4b 4e 4f 57  e of ..** UNKNOW
168c2 4e 5f 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78  N_LOCK for an ex
168c3 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 69  planation of thi
168c4 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
168c5 6e 74 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62  nt pagerUnlockDb
168c6 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
168c7 69 6e 74 20 65 4c 6f 63 6b 29 7b 0d 0a 20 20 69  int eLock){..  i
168c8 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
168c9 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  K;....  assert( 
168ca 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  !pPager->exclusi
168cb 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72  veMode || pPager
168cc 2d 3e 65 4c 6f 63 6b 3d 3d 65 4c 6f 63 6b 20 29  ->eLock==eLock )
168cd 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f  ;..  assert( eLo
168ce 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 65  ck==NO_LOCK || e
168cf 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  Lock==SHARED_LOC
168d0 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  K );..  assert( 
168d1 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  eLock!=NO_LOCK |
168d2 7c 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  | pagerUseWal(pP
168d3 61 67 65 72 29 3d 3d 30 20 29 3b 0d 0a 20 20 69  ager)==0 );..  i
168d4 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
168d5 2d 3e 66 64 29 20 29 7b 0d 0a 20 20 20 20 61 73  ->fd) ){..    as
168d6 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c  sert( pPager->eL
168d7 6f 63 6b 3e 3d 65 4c 6f 63 6b 20 29 3b 0d 0a 20  ock>=eLock );.. 
168d8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
168d9 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e  sUnlock(pPager->
168da 66 64 2c 20 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20  fd, eLock);..   
168db 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f   if( pPager->eLo
168dc 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b  ck!=UNKNOWN_LOCK
168dd 20 29 7b 0d 0a 20 20 20 20 20 20 70 50 61 67 65   ){..      pPage
168de 72 2d 3e 65 4c 6f 63 6b 20 3d 20 28 75 38 29 65  r->eLock = (u8)e
168df 4c 6f 63 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Lock;..    }..  
168e0 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f    IOTRACE(("UNLO
168e1 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61  CK %p %d\n", pPa
168e2 67 65 72 2c 20 65 4c 6f 63 6b 29 29 0d 0a 20 20  ger, eLock))..  
168e3 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
168e4 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63  .}..../*..** Loc
168e5 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  k the database f
168e6 69 6c 65 20 74 6f 20 6c 65 76 65 6c 20 65 4c 6f  ile to level eLo
168e7 63 6b 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62  ck, which must b
168e8 65 20 65 69 74 68 65 72 20 53 48 41 52 45 44 5f  e either SHARED_
168e9 4c 4f 43 4b 2c 0d 0a 2a 2a 20 52 45 53 45 52 56  LOCK,..** RESERV
168ea 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 43 4c 55  ED_LOCK or EXCLU
168eb 53 49 56 45 5f 4c 4f 43 4b 2e 20 49 66 20 74 68  SIVE_LOCK. If th
168ec 65 20 63 61 6c 6c 65 72 20 69 73 20 73 75 63 63  e caller is succ
168ed 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 0d  essful, set the.
168ee 0a 2a 2a 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20  .** Pager.eLock 
168ef 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20  variable to the 
168f0 6e 65 77 20 6c 6f 63 6b 69 6e 67 20 73 74 61 74  new locking stat
168f1 65 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 63 65  e. ..**..** Exce
168f2 70 74 2c 20 69 66 20 50 61 67 65 72 2e 65 4c 6f  pt, if Pager.eLo
168f3 63 6b 20 69 73 20 73 65 74 20 74 6f 20 55 4e 4b  ck is set to UNK
168f4 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68 65 6e 20 74  NOWN_LOCK when t
168f5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
168f6 0d 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 64 6f 20  ..** called, do 
168f7 6e 6f 74 20 6d 6f 64 69 66 79 20 69 74 20 75 6e  not modify it un
168f8 6c 65 73 73 20 74 68 65 20 6e 65 77 20 6c 6f 63  less the new loc
168f9 6b 69 6e 67 20 73 74 61 74 65 20 69 73 20 45 58  king state is EX
168fa 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e 20 0d 0a  CLUSIVE_LOCK. ..
168fb 2a 2a 20 53 65 65 20 74 68 65 20 63 6f 6d 6d 65  ** See the comme
168fc 6e 74 20 61 62 6f 76 65 20 74 68 65 20 23 64 65  nt above the #de
168fd 66 69 6e 65 20 6f 66 20 55 4e 4b 4e 4f 57 4e 5f  fine of UNKNOWN_
168fe 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78 70 6c  LOCK for an expl
168ff 61 6e 61 74 69 6f 6e 20 0d 0a 2a 2a 20 6f 66 20  anation ..** of 
16900 74 68 69 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  this...*/..stati
16901 63 20 69 6e 74 20 70 61 67 65 72 4c 6f 63 6b 44  c int pagerLockD
16902 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  b(Pager *pPager,
16903 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0d 0a 20 20   int eLock){..  
16904 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
16905 4f 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  OK;....  assert(
16906 20 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c   eLock==SHARED_L
16907 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 52 45  OCK || eLock==RE
16908 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65  SERVED_LOCK || e
16909 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f  Lock==EXCLUSIVE_
1690a 4c 4f 43 4b 20 29 3b 0d 0a 20 20 69 66 28 20 70  LOCK );..  if( p
1690b 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3c 65 4c 6f  Pager->eLock<eLo
1690c 63 6b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 4c  ck || pPager->eL
1690d 6f 63 6b 3d 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43  ock==UNKNOWN_LOC
1690e 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73  K ){..    rc = s
1690f 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61  qlite3OsLock(pPa
16910 67 65 72 2d 3e 66 64 2c 20 65 4c 6f 63 6b 29 3b  ger->fd, eLock);
16911 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
16912 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50 61 67  LITE_OK && (pPag
16913 65 72 2d 3e 65 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f  er->eLock!=UNKNO
16914 57 4e 5f 4c 4f 43 4b 7c 7c 65 4c 6f 63 6b 3d 3d  WN_LOCK||eLock==
16915 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 20  EXCLUSIVE_LOCK) 
16916 29 7b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72  ){..      pPager
16917 2d 3e 65 4c 6f 63 6b 20 3d 20 28 75 38 29 65 4c  ->eLock = (u8)eL
16918 6f 63 6b 3b 0d 0a 20 20 20 20 20 20 49 4f 54 52  ock;..      IOTR
16919 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64  ACE(("LOCK %p %d
1691a 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 65 4c 6f  \n", pPager, eLo
1691b 63 6b 29 29 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ck))..    }..  }
1691c 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
1691d 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
1691e 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d   function determ
1691f 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ines whether or 
16920 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  not the atomic-w
16921 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
16922 6e 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  n..** can be use
16923 64 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65  d with this page
16924 72 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74  r. The optimizat
16925 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20  ion can be used 
16926 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 28 61 29  if:..**..**  (a)
16927 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
16928 6e 65 64 20 62 79 20 4f 73 44 65 76 69 63 65 43  ned by OsDeviceC
16929 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 29  haracteristics()
1692a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 0d   indicates that.
1692b 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74 61 62  .**      a datab
1692c 61 73 65 20 70 61 67 65 20 6d 61 79 20 62 65 20  ase page may be 
1692d 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c  written atomical
1692e 6c 79 2c 20 61 6e 64 0d 0a 2a 2a 20 20 28 62 29  ly, and..**  (b)
1692f 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
16930 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72 53  ned by OsSectorS
16931 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74 68  ize() is less th
16932 61 6e 20 6f 72 20 65 71 75 61 6c 0d 0a 2a 2a 20  an or equal..** 
16933 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65       to the page
16934 20 73 69 7a 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54   size...**..** T
16935 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
16936 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65  is also always e
16937 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f  nabled for tempo
16938 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69  rary files. It i
16939 73 0d 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74  s..** an error t
1693a 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  o call this func
1693b 74 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69  tion if pPager i
1693c 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69  s opened on an i
1693d 6e 2d 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 64 61 74  n-memory..** dat
1693e 61 62 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  abase...**..** I
1693f 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  f the optimizati
16940 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  on cannot be use
16941 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  d, 0 is returned
16942 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20 75  . If it can be u
16943 73 65 64 2c 0d 0a 2a 2a 20 74 68 65 6e 20 74 68  sed,..** then th
16944 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
16945 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
16946 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
16947 20 77 68 65 6e 20 69 74 0d 0a 2a 2a 20 63 6f 6e   when it..** con
16948 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64  tains rollback d
16949 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20  ata for exactly 
1694a 6f 6e 65 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 23  one page...*/..#
1694b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
1694c 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
1694d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  ..static int jrn
1694e 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65  lBufferSize(Page
1694f 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 61  r *pPager){..  a
16950 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b  ssert( !MEMDB );
16951 0d 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  ..  if( !pPager-
16952 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0d 0a 20 20  >tempFile ){..  
16953 20 20 69 6e 74 20 64 63 3b 20 20 20 20 20 20 20    int dc;       
16954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16955 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 63 68      /* Device ch
16956 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f  aracteristics */
16957 0d 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 74 6f  ..    int nSecto
16958 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
16959 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f          /* Secto
1695a 72 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 20 20 69  r size */..    i
1695b 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20  nt szPage;      
1695c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1695d 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f   /* Page size */
1695e 0d 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ....    assert( 
1695f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
16960 64 29 20 29 3b 0d 0a 20 20 20 20 64 63 20 3d 20  d) );..    dc = 
16961 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
16962 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70  haracteristics(p
16963 50 61 67 65 72 2d 3e 66 64 29 3b 0d 0a 20 20 20  Pager->fd);..   
16964 20 6e 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65   nSector = pPage
16965 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0d 0a  r->sectorSize;..
16966 20 20 20 20 73 7a 50 61 67 65 20 3d 20 70 50 61      szPage = pPa
16967 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0d 0a  ger->pageSize;..
16968 0d 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c  ..    assert(SQL
16969 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
1696a 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0d  512==(512>>8));.
1696b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49  .    assert(SQLI
1696c 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
1696d 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b  4K==(65536>>8));
1696e 0d 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63  ..    if( 0==(dc
1696f 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  &(SQLITE_IOCAP_A
16970 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38  TOMIC|(szPage>>8
16971 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a  )) || nSector>sz
16972 50 61 67 65 29 20 29 7b 0d 0a 20 20 20 20 20 20  Page) ){..      
16973 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d  return 0;..    }
16974 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
16975 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  n JOURNAL_HDR_SZ
16976 28 70 50 61 67 65 72 29 20 2b 20 4a 4f 55 52 4e  (pPager) + JOURN
16977 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
16978 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
16979 2f 2a 0d 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  /*..** If SQLITE
1697a 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20  _CHECK_PAGES is 
1697b 64 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20  defined then we 
1697c 64 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63  do some sanity c
1697d 68 65 63 6b 69 6e 67 0d 0a 2a 2a 20 6f 6e 20 74  hecking..** on t
1697e 68 65 20 63 61 63 68 65 20 75 73 69 6e 67 20 61  he cache using a
1697f 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20   hash function. 
16980 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f   This is used fo
16981 72 20 74 65 73 74 69 6e 67 0d 0a 2a 2a 20 61 6e  r testing..** an
16982 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
16983 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
16984 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
16985 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
16986 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66  a 32-bit hash of
16987 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66   the page data f
16988 6f 72 20 70 50 61 67 65 2e 0d 0a 2a 2f 0d 0a 73  or pPage...*/..s
16989 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f  tatic u32 pager_
1698a 64 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79  datahash(int nBy
1698b 74 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61  te, unsigned cha
1698c 72 20 2a 70 44 61 74 61 29 7b 0d 0a 20 20 75 33  r *pData){..  u3
1698d 32 20 68 61 73 68 20 3d 20 30 3b 0d 0a 20 20 69  2 hash = 0;..  i
1698e 6e 74 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d 30  nt i;..  for(i=0
1698f 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b 29 7b  ; i<nByte; i++){
16990 0d 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68 61  ..    hash = (ha
16991 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74 61  sh*1039) + pData
16992 5b 69 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  [i];..  }..  ret
16993 75 72 6e 20 68 61 73 68 3b 0d 0a 7d 0d 0a 73 74  urn hash;..}..st
16994 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70  atic u32 pager_p
16995 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70  agehash(PgHdr *p
16996 50 61 67 65 29 7b 0d 0a 20 20 72 65 74 75 72 6e  Page){..  return
16997 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
16998 70 50 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70  pPage->pPager->p
16999 61 67 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e  ageSize, (unsign
1699a 65 64 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d  ed char *)pPage-
1699b 3e 70 44 61 74 61 29 3b 0d 0a 7d 0d 0a 73 74 61  >pData);..}..sta
1699c 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 73  tic void pager_s
1699d 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 48 64  et_pagehash(PgHd
1699e 72 20 2a 70 50 61 67 65 29 7b 0d 0a 20 20 70 50  r *pPage){..  pP
1699f 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  age->pageHash = 
169a0 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
169a1 50 61 67 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  Page);..}..../*.
169a2 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41  .** The CHECK_PA
169a3 47 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61  GE macro takes a
169a4 20 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72   PgHdr* as an ar
169a5 67 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54  gument. If SQLIT
169a6 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0d 0a 2a  E_CHECK_PAGES..*
169a7 2a 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e  * is defined, an
169a8 64 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20  d NDEBUG is not 
169a9 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65  defined, an asse
169aa 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63  rt() statement c
169ab 68 65 63 6b 73 0d 0a 2a 2a 20 74 68 61 74 20 74  hecks..** that t
169ac 68 65 20 70 61 67 65 20 69 73 20 65 69 74 68 65  he page is eithe
169ad 72 20 64 69 72 74 79 20 6f 72 20 73 74 69 6c 6c  r dirty or still
169ae 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 61 6c   matches the cal
169af 63 75 6c 61 74 65 64 20 70 61 67 65 2d 68 61 73  culated page-has
169b0 68 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  h...*/..#define 
169b1 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 68  CHECK_PAGE(x) ch
169b2 65 63 6b 50 61 67 65 28 78 29 0d 0a 73 74 61 74  eckPage(x)..stat
169b3 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67  ic void checkPag
169b4 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0d 0a  e(PgHdr *pPg){..
169b5 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
169b6 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0d 0a  = pPg->pPager;..
169b7 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
169b8 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f  ->eState!=PAGER_
169b9 45 52 52 4f 52 20 29 3b 0d 0a 20 20 61 73 73 65  ERROR );..  asse
169ba 72 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26  rt( (pPg->flags&
169bb 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20  PGHDR_DIRTY) || 
169bc 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70  pPg->pageHash==p
169bd 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
169be 67 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6c 73  g) );..}....#els
169bf 65 0d 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72  e..#define pager
169c0 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29 20 20  _datahash(X,Y)  
169c1 30 0d 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72  0..#define pager
169c2 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 30 0d  _pagehash(X)  0.
169c3 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 73  .#define pager_s
169c4 65 74 5f 70 61 67 65 68 61 73 68 28 58 29 0d 0a  et_pagehash(X)..
169c5 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41  #define CHECK_PA
169c6 47 45 28 78 29 0d 0a 23 65 6e 64 69 66 20 20 2f  GE(x)..#endif  /
169c7 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  * SQLITE_CHECK_P
169c8 41 47 45 53 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  AGES */..../*..*
169c9 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
169ca 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61  alled the journa
169cb 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72  l file for pager
169cc 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20   pPager must be 
169cd 6f 70 65 6e 2e 0d 0a 2a 2a 20 54 68 69 73 20 66  open...** This f
169ce 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
169cf 20 74 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65   to read a maste
169d0 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
169d1 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 0d 0a 2a  ame from the ..*
169d2 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  * end of the fil
169d3 65 20 61 6e 64 2c 20 69 66 20 73 75 63 63 65 73  e and, if succes
169d4 73 66 75 6c 2c 20 63 6f 70 69 65 73 20 69 74 20  sful, copies it 
169d5 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70  into memory supp
169d6 6c 69 65 64 20 0d 0a 2a 2a 20 62 79 20 74 68 65  lied ..** by the
169d7 20 63 61 6c 6c 65 72 2e 20 53 65 65 20 63 6f 6d   caller. See com
169d8 6d 65 6e 74 73 20 61 62 6f 76 65 20 77 72 69 74  ments above writ
169d9 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29  eMasterJournal()
169da 20 66 6f 72 20 74 68 65 20 66 6f 72 6d 61 74 0d   for the format.
169db 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72  .** used to stor
169dc 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  e a master journ
169dd 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61 74 20  al file name at 
169de 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75  the end of a jou
169df 72 6e 61 6c 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a  rnal file...**..
169e0 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20  ** zMaster must 
169e1 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
169e2 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d  r of at least nM
169e3 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f  aster bytes allo
169e4 63 61 74 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65  cated by..** the
169e5 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68   caller. This sh
169e6 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f  ould be sqlite3_
169e7 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  vfs.mxPathname+1
169e8 20 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72   (to ensure ther
169e9 65 20 69 73 0d 0a 2a 2a 20 65 6e 6f 75 67 68 20  e is..** enough 
169ea 73 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 74  space to write t
169eb 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
169ec 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20  l name). If the 
169ed 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0d 0a  master journal..
169ee 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a  ** name in the j
169ef 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72  ournal is longer
169f0 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 79   than nMaster by
169f1 74 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 61  tes (including a
169f2 0d 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  ..** nul-termina
169f3 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20  tor), then this 
169f4 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66  is handled as if
169f5 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   no master journ
169f6 61 6c 20 6e 61 6d 65 0d 0a 2a 2a 20 77 65 72 65  al name..** were
169f7 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
169f8 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a  journal...**..**
169f9 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   If a master jou
169fa 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
169fb 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65  s present at the
169fc 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72   end of the jour
169fd 6e 61 6c 0d 0a 2a 2a 20 66 69 6c 65 2c 20 74 68  nal..** file, th
169fe 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  en it is copied 
169ff 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20  into the buffer 
16a00 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d  pointed to by zM
16a01 61 73 74 65 72 2e 20 41 0d 0a 2a 2a 20 6e 75 6c  aster. A..** nul
16a02 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65  -terminator byte
16a03 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
16a04 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f  the buffer follo
16a05 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0d  wing the master.
16a06 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
16a07 20 6e 61 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   name...**..** I
16a08 66 20 69 74 20 69 73 20 64 65 74 65 72 6d 69 6e  f it is determin
16a09 65 64 20 74 68 61 74 20 6e 6f 20 6d 61 73 74 65  ed that no maste
16a0a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
16a0b 61 6d 65 20 69 73 20 70 72 65 73 65 6e 74 20 0d  ame is present .
16a0c 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69  .** zMaster[0] i
16a0d 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  s set to 0 and S
16a0e 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
16a0f 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e  d...**..** If an
16a10 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
16a11 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d  ile reading from
16a12 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
16a13 65 2c 20 61 6e 20 53 51 4c 69 74 65 0d 0a 2a 2a  e, an SQLite..**
16a14 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
16a15 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74  eturned...*/..st
16a16 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73  atic int readMas
16a17 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74  terJournal(sqlit
16a18 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20  e3_file *pJrnl, 
16a19 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75  char *zMaster, u
16a1a 33 32 20 6e 4d 61 73 74 65 72 29 7b 0d 0a 20 20  32 nMaster){..  
16a1b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
16a1c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
16a1d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  turn code */..  
16a1e 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 20 20 20  u32 len;        
16a1f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
16a20 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66  ngth in bytes of
16a21 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
16a22 6e 61 6d 65 20 2a 2f 0d 0a 20 20 69 36 34 20 73  name */..  i64 s
16a23 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  zJ;             
16a24 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73        /* Total s
16a25 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
16a26 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 4a 72  journal file pJr
16a27 6e 6c 20 2a 2f 0d 0a 20 20 75 33 32 20 63 6b 73  nl */..  u32 cks
16a28 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
16a29 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b 73      /* MJ checks
16a2a 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  um value read fr
16a2b 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20  om journal */.. 
16a2c 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20   u32 u;         
16a2d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
16a2e 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75  nsigned loop cou
16a2f 6e 74 65 72 20 2a 2f 0d 0a 20 20 75 6e 73 69 67  nter */..  unsig
16a30 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b  ned char aMagic[
16a31 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65  8];   /* A buffe
16a32 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61  r to hold the ma
16a33 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20  gic header */.. 
16a34 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c   zMaster[0] = '\
16a35 30 27 3b 0d 0a 0d 0a 20 20 69 66 28 20 53 51 4c  0';....  if( SQL
16a36 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
16a37 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
16a38 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 0d 0a 20  pJrnl, &szJ)).. 
16a39 20 20 7c 7c 20 73 7a 4a 3c 31 36 0d 0a 20 20 20    || szJ<16..   
16a3a 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  || SQLITE_OK!=(r
16a3b 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
16a3c 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c  Jrnl, szJ-16, &l
16a3d 65 6e 29 29 0d 0a 20 20 20 7c 7c 20 6c 65 6e 3e  en))..   || len>
16a3e 3d 6e 4d 61 73 74 65 72 20 0d 0a 20 20 20 7c 7c  =nMaster ..   ||
16a3f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
16a40 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72  = read32bits(pJr
16a41 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73  nl, szJ-12, &cks
16a42 75 6d 29 29 0d 0a 20 20 20 7c 7c 20 53 51 4c 49  um))..   || SQLI
16a43 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
16a44 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c  ite3OsRead(pJrnl
16a45 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a  , aMagic, 8, szJ
16a46 2d 38 29 29 0d 0a 20 20 20 7c 7c 20 6d 65 6d 63  -8))..   || memc
16a47 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72  mp(aMagic, aJour
16a48 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0d 0a 20 20  nalMagic, 8)..  
16a49 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
16a4a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
16a4b 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65  ad(pJrnl, zMaste
16a4c 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c  r, len, szJ-16-l
16a4d 65 6e 29 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20  en))..  ){..    
16a4e 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d  return rc;..  }.
16a4f 0a 0d 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74  ...  /* See if t
16a50 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63  he checksum matc
16a51 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a  hes the master j
16a52 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0d 0a  ournal name */..
16a53 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e    for(u=0; u<len
16a54 3b 20 75 2b 2b 29 7b 0d 0a 20 20 20 20 63 6b 73  ; u++){..    cks
16a55 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d  um -= zMaster[u]
16a56 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 63 6b  ;..  }..  if( ck
16a57 73 75 6d 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  sum ){..    /* I
16a58 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64  f the checksum d
16a59 6f 65 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74  oesn't add up, t
16a5a 68 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  hen one or more 
16a5b 6f 66 20 74 68 65 20 64 69 73 6b 20 73 65 63 74  of the disk sect
16a5c 6f 72 73 0d 0a 20 20 20 20 2a 2a 20 63 6f 6e 74  ors..    ** cont
16a5d 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 73 74 65  aining the maste
16a5e 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61  r journal filena
16a5f 6d 65 20 69 73 20 63 6f 72 72 75 70 74 65 64 2e  me is corrupted.
16a60 20 54 68 69 73 20 6d 65 61 6e 73 0d 0a 20 20 20   This means..   
16a61 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79 20 72   ** definitely r
16a62 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a 75 73  oll back, so jus
16a63 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
16a64 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20 61 20  OK and report a 
16a65 28 6e 75 6c 29 0d 0a 20 20 20 20 2a 2a 20 6d 61  (nul)..    ** ma
16a66 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster-journal fil
16a67 65 6e 61 6d 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  ename...    */..
16a68 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0d 0a 20 20      len = 0;..  
16a69 7d 0d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e  }..  zMaster[len
16a6a 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 0d 0a  ] = '\0';..   ..
16a6b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16a6c 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
16a6d 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73   Return the offs
16a6e 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72  et of the sector
16a6f 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20   boundary at or 
16a70 69 6d 6d 65 64 69 61 74 65 6c 79 20 0d 0a 2a 2a  immediately ..**
16a71 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76   following the v
16a72 61 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e  alue in pPager->
16a73 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75  journalOff, assu
16a74 6d 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0d 0a  ming a sector ..
16a75 2a 2a 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65  ** size of pPage
16a76 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79  r->sectorSize by
16a77 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 69 2e 65  tes...**..** i.e
16a78 20 66 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69   for a sector si
16a79 7a 65 20 6f 66 20 35 31 32 3a 0d 0a 2a 2a 0d 0a  ze of 512:..**..
16a7a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72 6e  **   Pager.journ
16a7b 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20 52  alOff          R
16a7c 65 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20  eturn value..** 
16a7d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
16a7e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16a7f 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20 20 20  ---------..**   
16a80 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
16a81 20 20 20 20 20 20 20 20 20 20 30 0d 0a 2a 2a 20            0..** 
16a82 20 20 35 31 32 20 20 20 20 20 20 20 20 20 20 20    512           
16a83 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0d              512.
16a84 0a 2a 2a 20 20 20 31 30 30 20 20 20 20 20 20 20  .**   100       
16a85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a86 35 31 32 0d 0a 2a 2a 20 20 20 32 30 30 30 20 20  512..**   2000  
16a87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a88 20 20 20 20 32 30 34 38 0d 0a 2a 2a 20 0d 0a 2a      2048..** ..*
16a89 2f 0d 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f  /..static i64 jo
16a8a 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50  urnalHdrOffset(P
16a8b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a  ager *pPager){..
16a8c 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30    i64 offset = 0
16a8d 3b 0d 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61  ;..  i64 c = pPa
16a8e 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
16a8f 0d 0a 20 20 69 66 28 20 63 20 29 7b 0d 0a 20 20  ..  if( c ){..  
16a90 20 20 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31    offset = ((c-1
16a91 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  )/JOURNAL_HDR_SZ
16a92 28 70 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20  (pPager) + 1) * 
16a93 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
16a94 50 61 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20  Pager);..  }..  
16a95 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 25 4a  assert( offset%J
16a96 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
16a97 61 67 65 72 29 3d 3d 30 20 29 3b 0d 0a 20 20 61  ager)==0 );..  a
16a98 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e 3d 63  ssert( offset>=c
16a99 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28   );..  assert( (
16a9a 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e 41  offset-c)<JOURNA
16a9b 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
16a9c 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6f 66   );..  return of
16a9d 66 73 65 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  fset;..}..../*..
16a9e 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** The journal f
16a9f 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
16aa0 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
16aa1 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0d 0a  ion is called...
16aa2 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  **..** This func
16aa3 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tion is a no-op 
16aa4 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  if the journal f
16aa5 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ile has not been
16aa6 20 77 72 69 74 74 65 6e 20 74 6f 0d 0a 2a 2a 20   written to..** 
16aa7 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65  within the curre
16aa8 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28  nt transaction (
16aa9 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f  i.e. if Pager.jo
16aaa 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0d 0a 2a  urnalOff==0)...*
16aab 2a 0d 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63  *..** If doTrunc
16aac 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20  ate is non-zero 
16aad 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75  or the Pager.jou
16aae 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61  rnalSizeLimit va
16aaf 72 69 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 73 65  riable is..** se
16ab0 74 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75  t to 0, then tru
16ab1 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  ncate the journa
16ab2 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62  l file to zero b
16ab3 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74  ytes in size. Ot
16ab4 68 65 72 77 69 73 65 2c 0d 0a 2a 2a 20 7a 65 72  herwise,..** zer
16ab5 6f 20 74 68 65 20 32 38 2d 62 79 74 65 20 68 65  o the 28-byte he
16ab6 61 64 65 72 20 61 74 20 74 68 65 20 73 74 61 72  ader at the star
16ab7 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
16ab8 20 66 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72   file. In either
16ab9 20 63 61 73 65 2c 20 0d 0a 2a 2a 20 69 66 20 74   case, ..** if t
16aba 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20  he pager is not 
16abb 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c  in no-sync mode,
16abc 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   sync the journa
16abd 6c 20 66 69 6c 65 20 69 6d 6d 65 64 69 61 74 65  l file immediate
16abe 6c 79 20 0d 0a 2a 2a 20 61 66 74 65 72 20 77 72  ly ..** after wr
16abf 69 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74  iting or truncat
16ac0 69 6e 67 20 69 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ing it...**..** 
16ac1 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  If Pager.journal
16ac2 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65 74  SizeLimit is set
16ac3 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c 20   to a positive, 
16ac4 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20  non-zero value, 
16ac5 61 6e 64 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  and..** followin
16ac6 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e  g the truncation
16ac7 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63   or zeroing desc
16ac8 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 20  ribed above the 
16ac9 73 69 7a 65 20 6f 66 20 74 68 65 20 0d 0a 2a 2a  size of the ..**
16aca 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
16acb 20 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72   bytes is larger
16acc 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65   than this value
16acd 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
16ace 74 68 65 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  the..** journal 
16acf 66 69 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f  file to Pager.jo
16ad0 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62  urnalSizeLimit b
16ad1 79 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ytes. The journa
16ad2 6c 20 66 69 6c 65 20 64 6f 65 73 0d 0a 2a 2a 20  l file does..** 
16ad3 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 73  not need to be s
16ad4 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  ynced following 
16ad5 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0d  this operation..
16ad6 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 49 4f  .**..** If an IO
16ad7 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61   error occurs, a
16ad8 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e  bandon processin
16ad9 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  g and return the
16ada 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d   IO error code..
16adb 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72  .** Otherwise, r
16adc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
16add 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
16ade 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28   zeroJournalHdr(
16adf 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
16ae0 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0d  nt doTruncate){.
16ae1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
16ae2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
16ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ae4 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
16ae5 6f 64 65 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ode */..  assert
16ae6 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
16ae7 3e 6a 66 64 29 20 29 3b 0d 0a 20 20 69 66 28 20  >jfd) );..  if( 
16ae8 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
16ae9 66 66 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74  ff ){..    const
16aea 20 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 70 50   i64 iLimit = pP
16aeb 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a  ager->journalSiz
16aec 65 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f  eLimit;    /* Lo
16aed 63 61 6c 20 63 61 63 68 65 20 6f 66 20 6a 73 6c  cal cache of jsl
16aee 20 2a 2f 0d 0a 0d 0a 20 20 20 20 49 4f 54 52 41   */....    IOTRA
16aef 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20 25 70  CE(("JZEROHDR %p
16af0 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0d 0a 20  \n", pPager)).. 
16af1 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61 74     if( doTruncat
16af2 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 29  e || iLimit==0 )
16af3 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
16af4 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
16af5 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b  pPager->jfd, 0);
16af6 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
16af7 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
16af8 20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38   char zeroHdr[28
16af9 5d 20 3d 20 7b 30 7d 3b 0d 0a 20 20 20 20 20 20  ] = {0};..      
16afa 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
16afb 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
16afc 20 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66   zeroHdr, sizeof
16afd 28 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0d 0a  (zeroHdr), 0);..
16afe 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
16aff 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
16b00 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
16b01 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ){..      rc = s
16b02 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61  qlite3OsSync(pPa
16b03 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45  ger->jfd, SQLITE
16b04 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70  _SYNC_DATAONLY|p
16b05 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73  Pager->syncFlags
16b06 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  );..    }....   
16b07 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
16b08 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
16b09 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62  n is committed b
16b0a 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  ut the write loc
16b0b 6b 20 0d 0a 20 20 20 20 2a 2a 20 69 73 20 73 74  k ..    ** is st
16b0c 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ill held on the 
16b0d 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69  file. If there i
16b0e 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63  s a size limit c
16b0f 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0d 0a  onfigured for ..
16b10 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69      ** the persi
16b11 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e  stent journal an
16b12 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  d the journal fi
16b13 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  le currently con
16b14 73 75 6d 65 73 20 6d 6f 72 65 0d 0a 20 20 20 20  sumes more..    
16b15 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20 74 68  ** space than th
16b16 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20  at limit allows 
16b17 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74  for, truncate it
16b18 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e   now. There is n
16b19 6f 20 6e 65 65 64 0d 0a 20 20 20 20 2a 2a 20 74  o need..    ** t
16b1a 6f 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65 20  o sync the file 
16b1b 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f  following this o
16b1c 70 65 72 61 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a  peration...    *
16b1d 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  /..    if( rc==S
16b1e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d  QLITE_OK && iLim
16b1f 69 74 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 69  it>0 ){..      i
16b20 36 34 20 73 7a 3b 0d 0a 20 20 20 20 20 20 72 63  64 sz;..      rc
16b21 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
16b22 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
16b23 2c 20 26 73 7a 29 3b 0d 0a 20 20 20 20 20 20 69  , &sz);..      i
16b24 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16b25 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b   && sz>iLimit ){
16b26 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ..        rc = s
16b27 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
16b28 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c  (pPager->jfd, iL
16b29 69 6d 69 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  imit);..      }.
16b2a 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
16b2b 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
16b2c 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  /*..** The journ
16b2d 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
16b2e 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72  open when this r
16b2f 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
16b30 2e 20 41 20 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20  . A journal..** 
16b31 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f  header (JOURNAL_
16b32 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73  HDR_SZ bytes) is
16b33 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
16b34 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
16b35 74 20 74 68 65 0d 0a 2a 2a 20 63 75 72 72 65 6e  t the..** curren
16b36 74 20 6c 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d  t location...**.
16b37 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66  .** The format f
16b38 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  or the journal h
16b39 65 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c  eader is as foll
16b3a 6f 77 73 3a 0d 0a 2a 2a 20 2d 20 38 20 62 79 74  ows:..** - 8 byt
16b3b 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 69  es: Magic identi
16b3c 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 6f  fying journal fo
16b3d 72 6d 61 74 2e 0d 0a 2a 2a 20 2d 20 34 20 62 79  rmat...** - 4 by
16b3e 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72  tes: Number of r
16b3f 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61  ecords in journa
16b40 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63  l, or -1 no-sync
16b41 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0d 0a 2a 2a   mode is on...**
16b42 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64   - 4 bytes: Rand
16b43 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66  om number used f
16b44 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0d 0a 2a  or page hash...*
16b45 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69  * - 4 bytes: Ini
16b46 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61  tial database pa
16b47 67 65 20 63 6f 75 6e 74 2e 0d 0a 2a 2a 20 2d 20  ge count...** - 
16b48 34 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20  4 bytes: Sector 
16b49 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65  size used by the
16b4a 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72   process that wr
16b4b 6f 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c  ote this journal
16b4c 2e 0d 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a  ...** - 4 bytes:
16b4d 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20 73   Database page s
16b4e 69 7a 65 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 46 6f  ize...** ..** Fo
16b4f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 4e  llowed by (JOURN
16b50 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20  AL_HDR_SZ - 28) 
16b51 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20  bytes of unused 
16b52 73 70 61 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  space...*/..stat
16b53 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72  ic int writeJour
16b54 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50  nalHdr(Pager *pP
16b55 61 67 65 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63  ager){..  int rc
16b56 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
16b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16b58 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   Return code */.
16b59 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72  .  char *zHeader
16b5a 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53   = pPager->pTmpS
16b5b 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72  pace;  /* Tempor
16b5c 61 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74  ary space used t
16b5d 6f 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a  o build header *
16b5e 2f 0d 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72  /..  u32 nHeader
16b5f 20 3d 20 28 75 33 32 29 70 50 61 67 65 72 2d 3e   = (u32)pPager->
16b60 70 61 67 65 53 69 7a 65 3b 2f 2a 20 53 69 7a 65  pageSize;/* Size
16b61 20 6f 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74   of buffer point
16b62 65 64 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72  ed to by zHeader
16b63 20 2a 2f 0d 0a 20 20 75 33 32 20 6e 57 72 69 74   */..  u32 nWrit
16b64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
16b65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
16b66 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 65  tes of header se
16b67 63 74 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f 0d  ctor written */.
16b68 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20  .  int ii;      
16b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b6a 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
16b6b 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 61  ounter */....  a
16b6c 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
16b6d 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20  ager->jfd) );   
16b6e 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69     /* Journal fi
16b6f 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e  le must be open.
16b70 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 6e 48 65   */....  if( nHe
16b71 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52  ader>JOURNAL_HDR
16b72 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0d 0a  _SZ(pPager) ){..
16b73 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f      nHeader = JO
16b74 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
16b75 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ger);..  }....  
16b76 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
16b77 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
16b78 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 68 65  s and any of the
16b79 6d 20 77 65 72 65 20 63 72 65 61 74 65 64 20 0d  m were created .
16b7a 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 20  .  ** since the 
16b7b 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 72  most recent jour
16b7c 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 20 77  nal header was w
16b7d 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 74  ritten, update t
16b7e 68 65 20 0d 0a 20 20 2a 2a 20 50 61 67 65 72 53  he ..  ** PagerS
16b7f 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
16b80 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0d  set fields now..
16b81 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 69 3d  .  */..  for(ii=
16b82 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  0; ii<pPager->nS
16b83 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
16b84 0d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ..    if( pPager
16b85 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
16b86 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29  .iHdrOffset==0 )
16b87 7b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  {..      pPager-
16b88 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e  >aSavepoint[ii].
16b89 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61  iHdrOffset = pPa
16b8a 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
16b8b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
16b8c 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
16b8d 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e 6a  lHdr = pPager->j
16b8e 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72  ournalOff = jour
16b8f 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61  nalHdrOffset(pPa
16b90 67 65 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 0d 0a  ger);....  /* ..
16b91 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e    ** Write the n
16b92 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20  Rec Field - the 
16b93 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72  number of page r
16b94 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c  ecords that foll
16b95 6f 77 20 74 68 69 73 0d 0a 20 20 2a 2a 20 6a 6f  ow this..  ** jo
16b96 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f  urnal header. No
16b97 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20  rmally, zero is 
16b98 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20  written to this 
16b99 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69  value at this ti
16b9a 6d 65 2e 0d 0a 20 20 2a 2a 20 41 66 74 65 72 20  me...  ** After 
16b9b 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20  the records are 
16b9c 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75  added to the jou
16b9d 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f  rnal (and the jo
16b9e 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0d 0a  urnal synced, ..
16b9f 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d    ** if in full-
16ba0 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20  sync mode), the 
16ba1 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74  zero is overwrit
16ba2 74 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75  ten with the tru
16ba3 65 20 6e 75 6d 62 65 72 0d 0a 20 20 2a 2a 20 6f  e number..  ** o
16ba4 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20 73  f records (see s
16ba5 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0d 0a  yncJournal())...
16ba6 20 20 2a 2a 0d 0a 20 20 2a 2a 20 41 20 66 61 73    **..  ** A fas
16ba7 74 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20  ter alternative 
16ba8 69 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46  is to write 0xFF
16ba9 46 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52  FFFFFF to the nR
16baa 65 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0d 0a  ec field. When..
16bab 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65    ** reading the
16bac 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61   journal this va
16bad 6c 75 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65  lue tells SQLite
16bae 20 74 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20   to assume that 
16baf 74 68 65 0d 0a 20 20 2a 2a 20 72 65 73 74 20 6f  the..  ** rest o
16bb0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
16bb1 6c 65 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69  le contains vali
16bb2 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 2e 20  d page records. 
16bb3 54 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 0d  This assumption.
16bb4 0a 20 20 2a 2a 20 69 73 20 64 61 6e 67 65 72 6f  .  ** is dangero
16bb5 75 73 2c 20 61 73 20 69 66 20 61 20 66 61 69 6c  us, as if a fail
16bb6 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
16bb7 6c 73 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74  lst writing to t
16bb8 68 65 20 6a 6f 75 72 6e 61 6c 0d 0a 20 20 2a 2a  he journal..  **
16bb9 20 66 69 6c 65 20 69 74 20 6d 61 79 20 63 6f 6e   file it may con
16bba 74 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 61 67  tain some garbag
16bbb 65 20 64 61 74 61 2e 20 54 68 65 72 65 20 61 72  e data. There ar
16bbc 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 0d  e two scenarios.
16bbd 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 68 69 73  .  ** where this
16bbe 20 72 69 73 6b 20 63 61 6e 20 62 65 20 69 67 6e   risk can be ign
16bbf 6f 72 65 64 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  ored:..  **..  *
16bc0 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 70  *   * When the p
16bc1 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 79  ager is in no-sy
16bc2 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 74  nc mode. Corrupt
16bc3 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 61  ion can follow a
16bc4 0d 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72  ..  **     power
16bc5 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73   failure in this
16bc6 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0d 0a 20   case anyway... 
16bc7 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 2a 20 57 68   **..  **   * Wh
16bc8 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f  en the SQLITE_IO
16bc9 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
16bca 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69  flag is set. Thi
16bcb 73 20 67 75 61 72 61 6e 74 65 65 73 0d 0a 20 20  s guarantees..  
16bcc 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62  **     that garb
16bcd 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65  age data is neve
16bce 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  r appended to th
16bcf 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0d  e journal file..
16bd0 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
16bd1 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
16bd2 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e  fd) || pPager->n
16bd3 6f 53 79 6e 63 20 29 3b 0d 0a 20 20 69 66 28 20  oSync );..  if( 
16bd4 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 7c  pPager->noSync |
16bd5 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  | (pPager->journ
16bd6 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
16bd7 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
16bd8 29 0d 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65  )..   || (sqlite
16bd9 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
16bda 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d  eristics(pPager-
16bdb 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41  >fd)&SQLITE_IOCA
16bdc 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0d  P_SAFE_APPEND) .
16bdd 0a 20 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 63 70  .  ){..    memcp
16bde 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72  y(zHeader, aJour
16bdf 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  nalMagic, sizeof
16be0 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29  (aJournalMagic))
16be1 3b 0d 0a 20 20 20 20 70 75 74 33 32 62 69 74 73  ;..    put32bits
16be2 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
16be3 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d  (aJournalMagic)]
16be4 2c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0d 0a  , 0xffffffff);..
16be5 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6d 65    }else{..    me
16be6 6d 73 65 74 28 7a 48 65 61 64 65 72 2c 20 30 2c  mset(zHeader, 0,
16be7 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c   sizeof(aJournal
16be8 4d 61 67 69 63 29 2b 34 29 3b 0d 0a 20 20 7d 0d  Magic)+4);..  }.
16be9 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 72 61 6e 64  ...  /* The rand
16bea 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68 20 69 6e  om check-hash in
16beb 69 74 69 61 6c 69 73 65 72 20 2a 2f 20 0d 0a 20  itialiser */ .. 
16bec 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
16bed 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61 67 65  ess(sizeof(pPage
16bee 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c 20 26  r->cksumInit), &
16bef 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
16bf0 74 29 3b 0d 0a 20 20 70 75 74 33 32 62 69 74 73  t);..  put32bits
16bf1 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
16bf2 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
16bf3 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 75  4], pPager->cksu
16bf4 6d 49 6e 69 74 29 3b 0d 0a 20 20 2f 2a 20 54 68  mInit);..  /* Th
16bf5 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61  e initial databa
16bf6 73 65 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 70 75  se size */..  pu
16bf7 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
16bf8 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
16bf9 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65  Magic)+8], pPage
16bfa 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0d  r->dbOrigSize);.
16bfb 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65  .  /* The assume
16bfc 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f  d sector size fo
16bfd 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a  r this process *
16bfe 2f 0d 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  /..  put32bits(&
16bff 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
16c00 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32  JournalMagic)+12
16c01 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  ], pPager->secto
16c02 72 53 69 7a 65 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  rSize);....  /* 
16c03 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  The page size */
16c04 0d 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a  ..  put32bits(&z
16c05 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
16c06 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d  ournalMagic)+16]
16c07 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
16c08 7a 65 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 69  ze);....  /* Ini
16c09 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61  tializing the ta
16c0a 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  il of the buffer
16c0b 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
16c0c 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0d 0a  y.  Everything..
16c0d 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20    ** works find 
16c0e 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  if the following
16c0f 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69   memset() is omi
16c10 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69  tted.  But initi
16c11 61 6c 69 7a 69 6e 67 0d 0a 20 20 2a 2a 20 74 68  alizing..  ** th
16c12 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74  e memory prevent
16c13 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20  s valgrind from 
16c14 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20  complaining, so 
16c15 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74  we are willing t
16c16 6f 0d 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65  o..  ** take the
16c17 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74   performance hit
16c18 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6d 65 6d 73 65  ...  */..  memse
16c19 74 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f  t(&zHeader[sizeo
16c1a 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
16c1b 2b 32 30 5d 2c 20 30 2c 0d 0a 20 20 20 20 20 20  +20], 0,..      
16c1c 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69 7a 65     nHeader-(size
16c1d 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
16c1e 29 2b 32 30 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  )+20));....  /* 
16c1f 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 73  In theory, it is
16c20 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 20   only necessary 
16c21 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 20  to write the 28 
16c22 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 0d  bytes that the .
16c23 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65  .  ** journal he
16c24 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f  ader consumes to
16c25 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
16c26 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63  e here. Then inc
16c27 72 65 6d 65 6e 74 20 74 68 65 20 0d 0a 20 20 2a  rement the ..  *
16c28 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  * Pager.journalO
16c29 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a  ff variable by J
16c2a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f  OURNAL_HDR_SZ so
16c2b 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0d   that the next .
16c2c 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20  .  ** record is 
16c2d 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66  written to the f
16c2e 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20  ollowing sector 
16c2f 28 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69  (leaving a gap i
16c30 6e 20 74 68 65 20 66 69 6c 65 0d 0a 20 20 2a 2a  n the file..  **
16c31 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d   that will be im
16c32 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20  plicitly filled 
16c33 69 6e 20 62 79 20 74 68 65 20 4f 53 29 2e 0d 0a  in by the OS)...
16c34 20 20 2a 2a 0d 0a 20 20 2a 2a 20 48 6f 77 65 76    **..  ** Howev
16c35 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 64  er it has been d
16c36 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 6f  iscovered that o
16c37 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74  n some systems t
16c38 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 6e 20  his pattern can 
16c39 0d 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 66  ..  ** be signif
16c3a 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74  icantly slower t
16c3b 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79  han contiguously
16c3c 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f   writing data to
16c3d 20 74 68 65 20 66 69 6c 65 2c 0d 0a 20 20 2a 2a   the file,..  **
16c3e 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65   even if that me
16c3f 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77  ans explicitly w
16c40 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74  riting data to t
16c41 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0d 0a 20 20  he block of ..  
16c42 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ** (JOURNAL_HDR_
16c43 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74  SZ - 28) bytes t
16c44 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  hat will not be 
16c45 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73  used. So that is
16c46 20 77 68 61 74 0d 0a 20 20 2a 2a 20 69 73 20 64   what..  ** is d
16c47 6f 6e 65 2e 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  one. ..  **..  *
16c48 2a 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65  * The loop is re
16c49 71 75 69 72 65 64 20 68 65 72 65 20 69 6e 20 63  quired here in c
16c4a 61 73 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73  ase the sector-s
16c4b 69 7a 65 20 69 73 20 6c 61 72 67 65 72 20 74 68  ize is larger th
16c4c 61 6e 20 74 68 65 20 0d 0a 20 20 2a 2a 20 64 61  an the ..  ** da
16c4d 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
16c4e 2e 20 53 69 6e 63 65 20 74 68 65 20 7a 48 65 61  . Since the zHea
16c4f 64 65 72 20 62 75 66 66 65 72 20 69 73 20 6f 6e  der buffer is on
16c50 6c 79 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a  ly Pager.pageSiz
16c51 65 0d 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e  e..  ** bytes in
16c52 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 68 61 6e   size, more than
16c53 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   one call to sql
16c54 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 6d 61  ite3OsWrite() ma
16c55 79 20 62 65 20 72 65 71 75 69 72 65 64 0d 0a 20  y be required.. 
16c56 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 65 20   ** to populate 
16c57 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e  the entire journ
16c58 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 6f 72  al header sector
16c59 2e 0d 0a 20 20 2a 2f 20 0d 0a 20 20 66 6f 72 28  ...  */ ..  for(
16c5a 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51  nWrite=0; rc==SQ
16c5b 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c  LITE_OK&&nWrite<
16c5c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
16c5d 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d  Pager); nWrite+=
16c5e 6e 48 65 61 64 65 72 29 7b 0d 0a 20 20 20 20 49  nHeader){..    I
16c5f 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70  OTRACE(("JHDR %p
16c60 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61   %lld %d\n", pPa
16c61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ger, pPager->jou
16c62 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72  rnalHdr, nHeader
16c63 29 29 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ))..    rc = sql
16c64 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
16c65 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72  er->jfd, zHeader
16c66 2c 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65  , nHeader, pPage
16c67 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0d  r->journalOff);.
16c68 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
16c69 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
16c6a 3c 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  <= pPager->journ
16c6b 61 6c 4f 66 66 20 29 3b 0d 0a 20 20 20 20 70 50  alOff );..    pP
16c6c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
16c6d 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0d 0a 20 20   += nHeader;..  
16c6e 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  }....  return rc
16c6f 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
16c70 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
16c71 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
16c72 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  n this is called
16c73 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  . A journal head
16c74 65 72 20 66 69 6c 65 0d 0a 2a 2a 20 28 4a 4f 55  er file..** (JOU
16c75 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
16c76 73 29 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20  s) is read from 
16c77 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  the current loca
16c78 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72  tion in the jour
16c79 6e 61 6c 0d 0a 2a 2a 20 66 69 6c 65 2e 20 54 68  nal..** file. Th
16c7a 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69  e current locati
16c7b 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  on in the journa
16c7c 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20  l file is given 
16c7d 62 79 0d 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a  by..** pPager->j
16c7e 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63  ournalOff. See c
16c7f 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75  omments above fu
16c80 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72  nction writeJour
16c81 6e 61 6c 48 64 72 28 29 20 66 6f 72 0d 0a 2a 2a  nalHdr() for..**
16c82 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f   a description o
16c83 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
16c84 61 64 65 72 20 66 6f 72 6d 61 74 2e 0d 0a 2a 2a  ader format...**
16c85 0d 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64  ..** If the head
16c86 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65  er is read succe
16c87 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20  ssfully, *pNRec 
16c88 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
16c89 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 70 61 67 65  mber of..** page
16c8a 20 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69   records followi
16c8b 6e 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61  ng this header a
16c8c 6e 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73  nd *pDbSize is s
16c8d 65 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f  et to the size o
16c8e 66 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61  f the..** databa
16c8f 73 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  se before the tr
16c90 61 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c  ansaction began,
16c91 20 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c   in pages. Also,
16c92 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e   pPager->cksumIn
16c93 69 74 0d 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  it..** is set to
16c94 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
16c95 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
16c96 20 68 65 61 64 65 72 2e 20 53 51 4c 49 54 45 5f   header. SQLITE_
16c97 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0d 0a  OK is returned..
16c98 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ** in this case.
16c99 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
16c9a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
16c9b 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62  ile appears to b
16c9c 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c  e corrupted, SQL
16c9d 49 54 45 5f 44 4f 4e 45 20 69 73 0d 0a 2a 2a 20  ITE_DONE is..** 
16c9e 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e  returned and *pN
16c9f 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65  Rec and *PDbSize
16ca0 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20   are undefined. 
16ca1 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f   If JOURNAL_HDR_
16ca2 53 5a 20 62 79 74 65 73 0d 0a 2a 2a 20 63 61 6e  SZ bytes..** can
16ca3 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d  not be read from
16ca4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
16ca5 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  e an error code 
16ca6 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f  is returned...*/
16ca7 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  ..static int rea
16ca8 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0d 0a 20 20  dJournalHdr(..  
16ca9 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20  Pager *pPager,  
16caa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16cab 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0d  Pager object */.
16cac 0a 20 20 69 6e 74 20 69 73 48 6f 74 2c 0d 0a 20  .  int isHot,.. 
16cad 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65   i64 journalSize
16cae 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
16caf 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 70 65   Size of the ope
16cb0 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  n journal file i
16cb1 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20 75 33  n bytes */..  u3
16cb2 32 20 2a 70 4e 52 65 63 2c 20 20 20 20 20 20 20  2 *pNRec,       
16cb3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
16cb4 54 3a 20 56 61 6c 75 65 20 72 65 61 64 20 66 72  T: Value read fr
16cb5 6f 6d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  om the nRec fiel
16cb6 64 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 70 44 62  d */..  u32 *pDb
16cb7 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  Size            
16cb8 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c       /* OUT: Val
16cb9 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 64  ue of original d
16cba 61 74 61 62 61 73 65 20 73 69 7a 65 20 66 69 65  atabase size fie
16cbb 6c 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  ld */..){..  int
16cbc 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
16cbd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
16cbe 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 75  urn code */..  u
16cbf 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61  nsigned char aMa
16cc0 67 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41  gic[8];     /* A
16cc1 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
16cc2 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72  the magic header
16cc3 20 2a 2f 0d 0a 20 20 69 36 34 20 69 48 64 72 4f   */..  i64 iHdrO
16cc4 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
16cc5 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
16cc6 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
16cc7 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0d 0a 0d  being read */...
16cc8 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
16cc9 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
16cca 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61  ;      /* Journa
16ccb 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
16ccc 70 65 6e 2e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  pen. */....  /* 
16ccd 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f  Advance Pager.jo
16cce 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20  urnalOff to the 
16ccf 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78  start of the nex
16cd0 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65  t sector. If the
16cd1 0d 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  ..  ** journal f
16cd2 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c  ile is too small
16cd3 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65   for there to be
16cd4 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64   a header stored
16cd5 20 61 74 20 74 68 69 73 0d 0a 20 20 2a 2a 20 70   at this..  ** p
16cd6 6f 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c  oint, return SQL
16cd7 49 54 45 5f 44 4f 4e 45 2e 0d 0a 20 20 2a 2f 0d  ITE_DONE...  */.
16cd8 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
16cd9 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48  alOff = journalH
16cda 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29  drOffset(pPager)
16cdb 3b 0d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ;..  if( pPager-
16cdc 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52  >journalOff+JOUR
16cdd 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
16cde 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65  r) > journalSize
16cdf 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
16ce0 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a 20 20  SQLITE_DONE;..  
16ce1 7d 0d 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70  }..  iHdrOff = p
16ce2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
16ce3 66 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61 64 20  f;....  /* Read 
16ce4 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62  in the first 8 b
16ce5 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ytes of the jour
16ce6 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74  nal header. If t
16ce7 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68  hey do not match
16ce8 0d 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69  ..  ** the  magi
16ce9 63 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61  c string found a
16cea 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
16ceb 61 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ach journal head
16cec 65 72 2c 20 72 65 74 75 72 6e 0d 0a 20 20 2a 2a  er, return..  **
16ced 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66   SQLITE_DONE. If
16cee 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
16cef 75 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  urs, return an e
16cf0 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72  rror code. Other
16cf1 77 69 73 65 2c 0d 0a 20 20 2a 2a 20 70 72 6f 63  wise,..  ** proc
16cf2 65 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  eed...  */..  if
16cf3 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f  ( isHot || iHdrO
16cf4 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ff!=pPager->jour
16cf5 6e 61 6c 48 64 72 20 29 7b 0d 0a 20 20 20 20 72  nalHdr ){..    r
16cf6 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
16cf7 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61  d(pPager->jfd, a
16cf8 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d  Magic, sizeof(aM
16cf9 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b  agic), iHdrOff);
16cfa 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d  ..    if( rc ){.
16cfb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
16cfc 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
16cfd 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  ( memcmp(aMagic,
16cfe 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
16cff 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21  sizeof(aMagic))!
16d00 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  =0 ){..      ret
16d01 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
16d02 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
16d03 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66 69    /* Read the fi
16d04 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69 74  rst three 32-bit
16d05 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 6a   fields of the j
16d06 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20 54  ournal header: T
16d07 68 65 20 6e 52 65 63 0d 0a 20 20 2a 2a 20 66 69  he nRec..  ** fi
16d08 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75  eld, the checksu
16d09 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e  m-initializer an
16d0a 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
16d0b 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74  ize at the start
16d0c 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72  ..  ** of the tr
16d0d 61 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72  ansaction. Retur
16d0e 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
16d0f 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
16d10 20 77 72 6f 6e 67 2e 0d 0a 20 20 2a 2f 0d 0a 20   wrong...  */.. 
16d11 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
16d12 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73  (rc = read32bits
16d13 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
16d14 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 29 29  drOff+8, pNRec))
16d15 0d 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f  ..   || SQLITE_O
16d16 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
16d17 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
16d18 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 50   iHdrOff+12, &pP
16d19 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
16d1a 29 0d 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  )..   || SQLITE_
16d1b 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
16d1c 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
16d1d 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44  , iHdrOff+16, pD
16d1e 62 53 69 7a 65 29 29 0d 0a 20 20 29 7b 0d 0a 20  bSize))..  ){.. 
16d1f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
16d20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 50 61 67   }....  if( pPag
16d21 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
16d22 30 20 29 7b 0d 0a 20 20 20 20 75 33 32 20 69 50  0 ){..    u32 iP
16d23 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
16d24 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 2d 73         /* Page-s
16d25 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75  ize field of jou
16d26 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0d 0a  rnal header */..
16d27 20 20 20 20 75 33 32 20 69 53 65 63 74 6f 72 53      u32 iSectorS
16d28 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
16d29 20 2f 2a 20 53 65 63 74 6f 72 2d 73 69 7a 65 20   /* Sector-size 
16d2a 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  field of journal
16d2b 20 68 65 61 64 65 72 20 2a 2f 0d 0a 0d 0a 20 20   header */....  
16d2c 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61    /* Read the pa
16d2d 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74  ge-size and sect
16d2e 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20  or-size journal 
16d2f 68 65 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a  header fields. *
16d30 2f 0d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  /..    if( SQLIT
16d31 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
16d32 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
16d33 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c 20  fd, iHdrOff+20, 
16d34 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 0d 0a  &iSectorSize))..
16d35 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f       || SQLITE_O
16d36 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
16d37 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
16d38 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 50   iHdrOff+24, &iP
16d39 61 67 65 53 69 7a 65 29 29 0d 0a 20 20 20 20 29  ageSize))..    )
16d3a 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
16d3b 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  rc;..    }....  
16d3c 20 20 2f 2a 20 56 65 72 73 69 6f 6e 73 20 6f 66    /* Versions of
16d3d 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f   SQLite prior to
16d3e 20 33 2e 35 2e 38 20 73 65 74 20 74 68 65 20 70   3.5.8 set the p
16d3f 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  age-size field o
16d40 66 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 6a 6f  f the..    ** jo
16d41 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20  urnal header to 
16d42 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 61  zero. In this ca
16d43 73 65 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20  se, assume that 
16d44 74 68 65 20 50 61 67 65 72 2e 70 61 67 65 53 69  the Pager.pageSi
16d45 7a 65 0d 0a 20 20 20 20 2a 2a 20 76 61 72 69 61  ze..    ** varia
16d46 62 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 73  ble is already s
16d47 65 74 20 74 6f 20 74 68 65 20 63 6f 72 72 65 63  et to the correc
16d48 74 20 70 61 67 65 20 73 69 7a 65 2e 0d 0a 20 20  t page size...  
16d49 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 69 50    */..    if( iP
16d4a 61 67 65 53 69 7a 65 3d 3d 30 20 29 7b 0d 0a 20  ageSize==0 ){.. 
16d4b 20 20 20 20 20 69 50 61 67 65 53 69 7a 65 20 3d       iPageSize =
16d4c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
16d4d 65 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  e;..    }....   
16d4e 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
16d4f 68 65 20 76 61 6c 75 65 73 20 72 65 61 64 20 66  he values read f
16d50 72 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a  rom the page-siz
16d51 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a  e and sector-siz
16d52 65 20 66 69 65 6c 64 73 0d 0a 20 20 20 20 2a 2a  e fields..    **
16d53 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67   are within rang
16d54 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e  e. To be 'in ran
16d55 67 65 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73  ge', both values
16d56 20 6e 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f   need to be a po
16d57 77 65 72 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 74  wer..    ** of t
16d58 77 6f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  wo greater than 
16d59 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31 32 20  or equal to 512 
16d5a 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74 20 67  or 32, and not g
16d5b 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 69  reater than thei
16d5c 72 20 0d 0a 20 20 20 20 2a 2a 20 72 65 73 70 65  r ..    ** respe
16d5d 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69  ctive compile ti
16d5e 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74  me maximum limit
16d5f 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  s...    */..    
16d60 69 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31  if( iPageSize<51
16d61 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
16d62 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a     || iSectorSiz
16d63 65 3c 33 32 0d 0a 20 20 20 20 20 7c 7c 20 69 50  e<32..     || iP
16d64 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d  ageSize>SQLITE_M
16d65 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20  AX_PAGE_SIZE || 
16d66 69 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f  iSectorSize>MAX_
16d67 53 45 43 54 4f 52 5f 53 49 5a 45 0d 0a 20 20 20  SECTOR_SIZE..   
16d68 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a 65    || ((iPageSize
16d69 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21 3d  -1)&iPageSize)!=
16d6a 30 20 20 20 7c 7c 20 28 28 69 53 65 63 74 6f 72  0   || ((iSector
16d6b 53 69 7a 65 2d 31 29 26 69 53 65 63 74 6f 72 53  Size-1)&iSectorS
16d6c 69 7a 65 29 21 3d 30 20 0d 0a 20 20 20 20 29 7b  ize)!=0 ..    ){
16d6d 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
16d6e 65 20 65 69 74 68 65 72 20 74 68 65 20 70 61 67  e either the pag
16d6f 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f 72  e-size or sector
16d70 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75  -size in the jou
16d71 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 0d  rnal-header is .
16d72 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69  .      ** invali
16d73 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63  d, then the proc
16d74 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74  ess that wrote t
16d75 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65  he journal-heade
16d76 72 20 6d 75 73 74 20 68 61 76 65 20 0d 0a 20 20  r must have ..  
16d77 20 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 62      ** crashed b
16d78 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 72  efore the header
16d79 20 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e 20   was synced. In 
16d7a 74 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 72  this case stop r
16d7b 65 61 64 69 6e 67 20 0d 0a 20 20 20 20 20 20 2a  eading ..      *
16d7c 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * the journal fi
16d7d 6c 65 20 68 65 72 65 2e 0d 0a 20 20 20 20 20 20  le here...      
16d7e 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  */..      return
16d7f 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a 20   SQLITE_DONE;.. 
16d80 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 55     }....    /* U
16d81 70 64 61 74 65 20 74 68 65 20 70 61 67 65 2d 73  pdate the page-s
16d82 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ize to match the
16d83 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
16d84 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 0d 0a   the journal. ..
16d85 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65 73      ** Use a tes
16d86 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74 6f  tcase() macro to
16d87 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
16d88 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 77  malloc failure w
16d89 69 74 68 69 6e 20 0d 0a 20 20 20 20 2a 2a 20 50  ithin ..    ** P
16d8a 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
16d8b 29 20 69 73 20 74 65 73 74 65 64 2e 0d 0a 20 20  ) is tested...  
16d8c 20 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73    */..    rc = s
16d8d 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
16d8e 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26  gesize(pPager, &
16d8f 69 50 61 67 65 53 69 7a 65 2c 20 2d 31 29 3b 0d  iPageSize, -1);.
16d90 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72  .    testcase( r
16d91 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d  c!=SQLITE_OK );.
16d92 0a 0d 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65  ...    /* Update
16d93 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63   the assumed sec
16d94 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63  tor-size to matc
16d95 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64  h the value used
16d96 20 62 79 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65   by ..    ** the
16d97 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 72   process that cr
16d98 65 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 6e  eated this journ
16d99 61 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 72  al. If this jour
16d9a 6e 61 6c 20 77 61 73 0d 0a 20 20 20 20 2a 2a 20  nal was..    ** 
16d9b 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f  created by a pro
16d9c 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20  cess other than 
16d9d 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74  this one, then t
16d9e 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 20 20 20  his routine..   
16d9f 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c   ** is being cal
16da0 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
16da1 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29  pager_playback()
16da2 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75  . The local valu
16da3 65 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67  e..    ** of Pag
16da4 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73  er.sectorSize is
16da5 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 68 65   restored at the
16da6 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75   end of that rou
16da7 74 69 6e 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  tine...    */.. 
16da8 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
16da9 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53  rSize = iSectorS
16daa 69 7a 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70  ize;..  }....  p
16dab 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
16dac 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  f += JOURNAL_HDR
16dad 5f 53 5a 28 70 50 61 67 65 72 29 3b 0d 0a 20 20  _SZ(pPager);..  
16dae 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
16daf 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20  .../*..** Write 
16db0 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61 73  the supplied mas
16db1 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
16db2 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61   into the journa
16db3 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72  l file for pager
16db4 0d 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74  ..** pPager at t
16db5 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
16db6 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20  ion. The master 
16db7 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73  journal name mus
16db8 74 20 62 65 20 74 68 65 20 6c 61 73 74 0d 0a 2a  t be the last..*
16db9 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20  * thing written 
16dba 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  to a journal fil
16dbb 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20  e. If the pager 
16dbc 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20  is in full-sync 
16dbd 6d 6f 64 65 2c 20 74 68 65 0d 0a 2a 2a 20 6a 6f  mode, the..** jo
16dbe 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
16dbf 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65  iptor is advance
16dc0 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65  d to the next se
16dc1 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65  ctor boundary be
16dc2 66 6f 72 65 0d 0a 2a 2a 20 61 6e 79 74 68 69 6e  fore..** anythin
16dc3 67 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68  g is written. Th
16dc4 65 20 66 6f 72 6d 61 74 20 69 73 3a 0d 0a 2a 2a  e format is:..**
16dc5 0d 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73  ..**   + 4 bytes
16dc6 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e  : PAGER_MJ_PGNO.
16dc7 0d 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73  ..**   + N bytes
16dc8 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  : Master journal
16dc9 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66   filename in utf
16dca 2d 38 2e 0d 0a 2a 2a 20 20 20 2b 20 34 20 62 79  -8...**   + 4 by
16dcb 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f  tes: N (length o
16dcc 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
16dcd 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20   name in bytes, 
16dce 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  no nul-terminato
16dcf 72 29 2e 0d 0a 2a 2a 20 20 20 2b 20 34 20 62 79  r)...**   + 4 by
16dd0 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72  tes: Master jour
16dd1 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75  nal name checksu
16dd2 6d 2e 0d 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74  m...**   + 8 byt
16dd3 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  es: aJournalMagi
16dd4 63 5b 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  c[]...**..** The
16dd5 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
16dd6 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73  page checksum is
16dd7 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
16dd8 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73  bytes in the mas
16dd9 74 65 72 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ter..** journal 
16dda 6e 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68  name, where each
16ddb 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72   byte is interpr
16ddc 65 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64  eted as a signed
16ddd 20 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0d   8-bit integer..
16dde 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 7a 4d 61 73 74  .**..** If zMast
16ddf 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  er is a NULL poi
16de0 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72  nter (occurs for
16de1 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
16de2 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c  se transaction),
16de3 20 0d 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20   ..** this call 
16de4 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d  is a no-op...*/.
16de5 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74  .static int writ
16de6 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50  eMasterJournal(P
16de7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f  ager *pPager, co
16de8 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
16de9 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20  r){..  int rc;  
16dea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16deb 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
16dec 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74  n code */..  int
16ded 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20   nMaster;       
16dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16def 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e   Length of strin
16df0 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0d 0a 20 20  g zMaster */..  
16df1 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20  i64 iHdrOff;    
16df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16df3 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65   /* Offset of he
16df4 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20  ader in journal 
16df5 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 36 34 20 6a  file */..  i64 j
16df6 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 20  rnlSize;        
16df7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
16df8 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66  ize of journal f
16df9 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0d 0a  ile on disk */..
16dfa 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b    u32 cksum = 0;
16dfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16dfc 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f     /* Checksum o
16dfd 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72  f string zMaster
16dfe 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   */....  assert(
16dff 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
16e00 65 72 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  er==0 );..  asse
16e01 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c  rt( !pagerUseWal
16e02 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 0d 0a 20  (pPager) );.... 
16e03 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 0d 0a   if( !zMaster ..
16e04 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f     || pPager->jo
16e05 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
16e06 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
16e07 4f 52 59 20 0d 0a 20 20 20 7c 7c 20 70 50 61 67  ORY ..   || pPag
16e08 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
16e09 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
16e0a 44 45 5f 4f 46 46 20 0d 0a 20 20 29 7b 0d 0a 20  DE_OFF ..  ){.. 
16e0b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
16e0c 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50 61  _OK;..  }..  pPa
16e0d 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
16e0e 20 31 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69   1;..  assert( i
16e0f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
16e10 64 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  d) );..  assert(
16e11 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
16e12 48 64 72 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6a  Hdr <= pPager->j
16e13 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0d 0a 0d 0a  ournalOff );....
16e14 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74    /* Calculate t
16e15 68 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74  he length in byt
16e16 65 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b  es and the check
16e17 73 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a  sum of zMaster *
16e18 2f 0d 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72  /..  for(nMaster
16e19 3d 30 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73  =0; zMaster[nMas
16e1a 74 65 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29  ter]; nMaster++)
16e1b 7b 0d 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20  {..    cksum += 
16e1c 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d  zMaster[nMaster]
16e1d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49  ;..  }....  /* I
16e1e 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  f in full-sync m
16e1f 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20  ode, advance to 
16e20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65  the next disk se
16e21 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74  ctor before writ
16e22 69 6e 67 0d 0a 20 20 2a 2a 20 74 68 65 20 6d 61  ing..  ** the ma
16e23 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
16e24 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61  e. This is in ca
16e25 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  se the previous 
16e26 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0d  page written to.
16e27 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  .  ** the journa
16e28 6c 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  l has already be
16e29 65 6e 20 73 79 6e 63 65 64 2e 0d 0a 20 20 2a 2f  en synced...  */
16e2a 0d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
16e2b 66 75 6c 6c 53 79 6e 63 20 29 7b 0d 0a 20 20 20  fullSync ){..   
16e2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
16e2d 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72  Off = journalHdr
16e2e 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0d  Offset(pPager);.
16e2f 0a 20 20 7d 0d 0a 20 20 69 48 64 72 4f 66 66 20  .  }..  iHdrOff 
16e30 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
16e31 6c 4f 66 66 3b 0d 0a 0d 0a 20 20 2f 2a 20 57 72  lOff;....  /* Wr
16e32 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
16e33 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74  ournal data to t
16e34 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f  he end of the jo
16e35 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0d 0a  urnal file. If..
16e36 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63    ** an error oc
16e37 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65  curs, return the
16e38 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74   error code to t
16e39 68 65 20 63 61 6c 6c 65 72 2e 0d 0a 20 20 2a 2f  he caller...  */
16e3a 0d 0a 20 20 69 66 28 20 28 30 20 21 3d 20 28 72  ..  if( (0 != (r
16e3b 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28  c = write32bits(
16e3c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
16e3d 72 4f 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50  rOff, PAGER_MJ_P
16e3e 47 4e 4f 28 70 50 61 67 65 72 29 29 29 29 0d 0a  GNO(pPager))))..
16e3f 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20     || (0 != (rc 
16e40 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
16e41 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
16e42 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20  aster, nMaster, 
16e43 69 48 64 72 4f 66 66 2b 34 29 29 29 0d 0a 20 20  iHdrOff+4)))..  
16e44 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20   || (0 != (rc = 
16e45 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
16e46 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
16e47 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 73  +4+nMaster, nMas
16e48 74 65 72 29 29 29 0d 0a 20 20 20 7c 7c 20 28 30  ter)))..   || (0
16e49 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33   != (rc = write3
16e4a 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
16e4b 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  d, iHdrOff+4+nMa
16e4c 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 29 29  ster+4, cksum)))
16e4d 0d 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72  ..   || (0 != (r
16e4e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
16e4f 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
16e50 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38  aJournalMagic, 8
16e51 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73  , iHdrOff+4+nMas
16e52 74 65 72 2b 38 29 29 29 0d 0a 20 20 29 7b 0d 0a  ter+8)))..  ){..
16e53 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
16e54 20 20 7d 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6a    }..  pPager->j
16e55 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d  ournalOff += (nM
16e56 61 73 74 65 72 2b 32 30 29 3b 0d 0a 0d 0a 20 20  aster+20);....  
16e57 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  /* If the pager 
16e58 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 2d  is in peristent-
16e59 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 68  journal mode, th
16e5a 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c 20  en the physical 
16e5b 0d 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66  ..  ** journal-f
16e5c 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70  ile may extend p
16e5d 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
16e5e 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  he master-journa
16e5f 6c 20 6e 61 6d 65 0d 0a 20 20 2a 2a 20 61 6e 64  l name..  ** and
16e60 20 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69   8 bytes of magi
16e61 63 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74  c data just writ
16e62 74 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e  ten to the file.
16e63 20 54 68 69 73 20 69 73 20 0d 0a 20 20 2a 2a 20   This is ..  ** 
16e64 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73  dangerous becaus
16e65 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f  e the code to ro
16e66 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75  llback a hot-jou
16e67 72 6e 61 6c 20 66 69 6c 65 0d 0a 20 20 2a 2a 20  rnal file..  ** 
16e68 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65  will not be able
16e69 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61 73   to find the mas
16e6a 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter-journal name
16e6b 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0d 0a   to determine ..
16e6c 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20    ** whether or 
16e6d 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  not the journal 
16e6e 69 73 20 68 6f 74 2e 20 0d 0a 20 20 2a 2a 0d 0a  is hot. ..  **..
16e6f 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69    ** Easiest thi
16e70 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73  ng to do in this
16e71 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20   scenario is to 
16e72 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75  truncate the jou
16e73 72 6e 61 6c 20 0d 0a 20 20 2a 2a 20 66 69 6c 65  rnal ..  ** file
16e74 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64   to the required
16e75 20 73 69 7a 65 2e 0d 0a 20 20 2a 2f 20 0d 0a 20   size...  */ .. 
16e76 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
16e77 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46  (rc = sqlite3OsF
16e78 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e  ileSize(pPager->
16e79 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29  jfd, &jrnlSize))
16e7a 0d 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65  ..   && jrnlSize
16e7b 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  >pPager->journal
16e7c 4f 66 66 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 72  Off..  ){..    r
16e7d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
16e7e 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
16e7f 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
16e80 61 6c 4f 66 66 29 3b 0d 0a 20 20 7d 0d 0a 20 20  alOff);..  }..  
16e81 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
16e82 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 61 20 70  ./*..** Find a p
16e83 61 67 65 20 69 6e 20 74 68 65 20 68 61 73 68 20  age in the hash 
16e84 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20  table given its 
16e85 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74  page number. Ret
16e86 75 72 6e 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  urn..** a pointe
16e87 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72  r to the page or
16e88 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65 71   NULL if the req
16e89 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e  uested page is n
16e8a 6f 74 20 0d 0a 2a 2a 20 61 6c 72 65 61 64 79 20  ot ..** already 
16e8b 69 6e 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a  in memory...*/..
16e8c 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 61  static PgHdr *pa
16e8d 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72  ger_lookup(Pager
16e8e 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
16e8f 67 6e 6f 29 7b 0d 0a 20 20 50 67 48 64 72 20 2a  gno){..  PgHdr *
16e90 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
16e91 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
16e92 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0d 0a 0d  turn value */...
16e93 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20  .  /* It is not 
16e94 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63  possible for a c
16e95 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74  all to PcacheFet
16e96 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65  ch() with create
16e97 46 6c 61 67 3d 3d 30 20 74 6f 0d 0a 20 20 2a 2a  Flag==0 to..  **
16e98 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20   fail, since no 
16e99 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63  attempt to alloc
16e9a 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f  ate dynamic memo
16e9b 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e  ry will be made.
16e9c 0d 0a 20 20 2a 2f 0d 0a 20 20 28 76 6f 69 64 29  ..  */..  (void)
16e9d 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
16e9e 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
16e9f 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29  he, pgno, 0, &p)
16ea0 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a  ;..  return p;..
16ea1 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 69 73 63  }..../*..** Disc
16ea2 61 72 64 20 74 68 65 20 65 6e 74 69 72 65 20 63  ard the entire c
16ea3 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69  ontents of the i
16ea4 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 2d 63 61  n-memory page-ca
16ea5 63 68 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  che...*/..static
16ea6 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65   void pager_rese
16ea7 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
16ea8 7b 0d 0a 20 20 73 71 6c 69 74 65 33 42 61 63 6b  {..  sqlite3Back
16ea9 75 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72  upRestart(pPager
16eaa 2d 3e 70 42 61 63 6b 75 70 29 3b 0d 0a 20 20 73  ->pBackup);..  s
16eab 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
16eac 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  r(pPager->pPCach
16ead 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  e);..}..../*..**
16eae 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74   Free all struct
16eaf 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65  ures in the Page
16eb0 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61  r.aSavepoint[] a
16eb1 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74  rray and set bot
16eb2 68 0d 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76  h..** Pager.aSav
16eb3 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72  epoint and Pager
16eb4 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a  .nSavepoint to z
16eb5 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73  ero. Close the s
16eb6 75 62 2d 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 69  ub-journal..** i
16eb7 66 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64  f it is open and
16eb8 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f   the pager is no
16eb9 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  t in exclusive m
16eba 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ode...*/..static
16ebb 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c 6c   void releaseAll
16ebc 53 61 76 65 70 6f 69 6e 74 73 28 50 61 67 65 72  Savepoints(Pager
16ebd 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 69 6e   *pPager){..  in
16ebe 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
16ebf 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
16ec0 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  for looping thro
16ec1 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 65 70  ugh Pager.aSavep
16ec2 6f 69 6e 74 20 2a 2f 0d 0a 20 20 66 6f 72 28 69  oint */..  for(i
16ec3 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e  i=0; ii<pPager->
16ec4 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
16ec5 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ){..    sqlite3B
16ec6 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
16ec7 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b  ger->aSavepoint[
16ec8 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  ii].pInSavepoint
16ec9 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21  );..  }..  if( !
16eca 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
16ecb 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33  eMode || sqlite3
16ecc 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61  IsMemJournal(pPa
16ecd 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0d 0a 20  ger->sjfd) ){.. 
16ece 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
16ecf 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b  e(pPager->sjfd);
16ed0 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
16ed1 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53  _free(pPager->aS
16ed2 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 70 50  avepoint);..  pP
16ed3 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
16ed4 20 3d 20 30 3b 0d 0a 20 20 70 50 61 67 65 72 2d   = 0;..  pPager-
16ed5 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b  >nSavepoint = 0;
16ed6 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62  ..  pPager->nSub
16ed7 52 65 63 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  Rec = 0;..}..../
16ed8 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69  *..** Set the bi
16ed9 74 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e  t number pgno in
16eda 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
16edb 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  int.pInSavepoint
16edc 20 0d 0a 2a 2a 20 62 69 74 76 65 63 73 20 6f 66   ..** bitvecs of
16edd 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f   all open savepo
16ede 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 53 51 4c  ints. Return SQL
16edf 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
16ee0 73 66 75 6c 0d 0a 2a 2a 20 6f 72 20 53 51 4c 49  sful..** or SQLI
16ee1 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61  TE_NOMEM if a ma
16ee2 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63  lloc failure occ
16ee3 75 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  urs...*/..static
16ee4 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f   int addToSavepo
16ee5 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 65 72  intBitvecs(Pager
16ee6 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
16ee7 67 6e 6f 29 7b 0d 0a 20 20 69 6e 74 20 69 69 3b  gno){..  int ii;
16ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ee9 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
16eea 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20  er */..  int rc 
16eeb 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
16eec 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
16eed 65 20 2a 2f 0d 0a 0d 0a 20 20 66 6f 72 28 69 69  e */....  for(ii
16eee 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e  =0; ii<pPager->n
16eef 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29  Savepoint; ii++)
16ef0 7b 0d 0a 20 20 20 20 50 61 67 65 72 53 61 76 65  {..    PagerSave
16ef1 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67  point *p = &pPag
16ef2 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69  er->aSavepoint[i
16ef3 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70 67 6e  i];..    if( pgn
16ef4 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0d 0a  o<=p->nOrig ){..
16ef5 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69        rc |= sqli
16ef6 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e  te3BitvecSet(p->
16ef7 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67  pInSavepoint, pg
16ef8 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  no);..      test
16ef9 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  case( rc==SQLITE
16efa 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20 20 20 20  _NOMEM );..     
16efb 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
16efc 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51  ITE_OK || rc==SQ
16efd 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20  LITE_NOMEM );.. 
16efe 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
16eff 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
16f00 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
16f01 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  on is a no-op if
16f02 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
16f03 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20   exclusive mode 
16f04 61 6e 64 20 6e 6f 74 0d 0a 2a 2a 20 69 6e 20 74  and not..** in t
16f05 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2e 20  he ERROR state. 
16f06 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 73 77  Otherwise, it sw
16f07 69 74 63 68 65 73 20 74 68 65 20 70 61 67 65 72  itches the pager
16f08 20 74 6f 20 50 41 47 45 52 5f 4f 50 45 4e 0d 0a   to PAGER_OPEN..
16f09 2a 2a 20 73 74 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a  ** state...**..*
16f0a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
16f0b 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69  s not in exclusi
16f0c 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 2c 20  ve-access mode, 
16f0d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
16f0e 65 20 69 73 0d 0a 2a 2a 20 63 6f 6d 70 6c 65 74  e is..** complet
16f0f 65 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 20 49 66  ely unlocked. If
16f10 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c   the file is unl
16f11 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 66 69  ocked and the fi
16f12 6c 65 2d 73 79 73 74 65 6d 20 64 6f 65 73 0d 0a  le-system does..
16f13 2a 2a 20 6e 6f 74 20 65 78 68 69 62 69 74 20 74  ** not exhibit t
16f14 68 65 20 55 4e 44 45 4c 45 54 41 42 4c 45 5f 57  he UNDELETABLE_W
16f15 48 45 4e 5f 4f 50 45 4e 20 70 72 6f 70 65 72 74  HEN_OPEN propert
16f16 79 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  y, the journal f
16f17 69 6c 65 20 69 73 0d 0a 2a 2a 20 63 6c 6f 73 65  ile is..** close
16f18 64 20 28 69 66 20 69 74 20 69 73 20 6f 70 65 6e  d (if it is open
16f19 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  )...**..** If th
16f1a 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 45 52  e pager is in ER
16f1b 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20 74  ROR state when t
16f1c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
16f1d 63 61 6c 6c 65 64 2c 20 74 68 65 20 0d 0a 2a 2a  called, the ..**
16f1e 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
16f1f 20 70 61 67 65 72 20 63 61 63 68 65 20 61 72 65   pager cache are
16f20 20 64 69 73 63 61 72 64 65 64 20 62 65 66 6f 72   discarded befor
16f21 65 20 73 77 69 74 63 68 69 6e 67 20 62 61 63 6b  e switching back
16f22 20 74 6f 20 0d 0a 2a 2a 20 74 68 65 20 4f 50 45   to ..** the OPE
16f23 4e 20 73 74 61 74 65 2e 20 52 65 67 61 72 64 6c  N state. Regardl
16f24 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74  ess of whether t
16f25 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65  he pager is in e
16f26 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 0d 0a 2a  xclusive-mode..*
16f27 2a 20 6f 72 20 6e 6f 74 2c 20 61 6e 79 20 6a 6f  * or not, any jo
16f28 75 72 6e 61 6c 20 66 69 6c 65 20 6c 65 66 74 20  urnal file left 
16f29 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
16f2a 65 6d 20 77 69 6c 6c 20 62 65 20 74 72 65 61 74  em will be treat
16f2b 65 64 0d 0a 2a 2a 20 61 73 20 61 20 68 6f 74 2d  ed..** as a hot-
16f2c 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c  journal and roll
16f2d 65 64 20 62 61 63 6b 20 74 68 65 20 6e 65 78 74  ed back the next
16f2e 20 74 69 6d 65 20 61 20 72 65 61 64 2d 74 72 61   time a read-tra
16f2f 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20  nsaction..** is 
16f30 6f 70 65 6e 65 64 20 28 62 79 20 74 68 69 73 20  opened (by this 
16f31 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72 20  or by any other 
16f32 63 6f 6e 6e 65 63 74 69 6f 6e 29 2e 0d 0a 2a 2f  connection)...*/
16f33 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  ..static void pa
16f34 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72  ger_unlock(Pager
16f35 20 2a 70 50 61 67 65 72 29 7b 0d 0a 0d 0a 20 20   *pPager){....  
16f36 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
16f37 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  eState==PAGER_RE
16f38 41 44 45 52 20 0d 0a 20 20 20 20 20 20 20 7c 7c  ADER ..       ||
16f39 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
16f3a 3d 50 41 47 45 52 5f 4f 50 45 4e 20 0d 0a 20 20  =PAGER_OPEN ..  
16f3b 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
16f3c 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 45 52  eState==PAGER_ER
16f3d 52 4f 52 20 0d 0a 20 20 29 3b 0d 0a 0d 0a 20 20  ROR ..  );....  
16f3e 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
16f3f 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e  troy(pPager->pIn
16f40 4a 6f 75 72 6e 61 6c 29 3b 0d 0a 20 20 70 50 61  Journal);..  pPa
16f41 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
16f42 3d 20 30 3b 0d 0a 20 20 72 65 6c 65 61 73 65 41  = 0;..  releaseA
16f43 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61  llSavepoints(pPa
16f44 67 65 72 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70  ger);....  if( p
16f45 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
16f46 72 29 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  r) ){..    asser
16f47 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  t( !isOpen(pPage
16f48 72 2d 3e 6a 66 64 29 20 29 3b 0d 0a 20 20 20 20  r->jfd) );..    
16f49 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 52 65 61  sqlite3WalEndRea
16f4a 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  dTransaction(pPa
16f4b 67 65 72 2d 3e 70 57 61 6c 29 3b 0d 0a 20 20 20  ger->pWal);..   
16f4c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20   pPager->eState 
16f4d 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0d 0a 20  = PAGER_OPEN;.. 
16f4e 20 7d 65 6c 73 65 20 69 66 28 20 21 70 50 61 67   }else if( !pPag
16f4f 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
16f50 65 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63  e ){..    int rc
16f51 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16f52 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
16f53 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
16f54 79 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28  y pagerUnlockDb(
16f55 29 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 44  ) */..    int iD
16f56 63 20 3d 20 69 73 4f 70 65 6e 28 70 50 61 67 65  c = isOpen(pPage
16f57 72 2d 3e 66 64 29 3f 73 71 6c 69 74 65 33 4f 73  r->fd)?sqlite3Os
16f58 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
16f59 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
16f5a 29 3a 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49  ):0;....    /* I
16f5b 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
16f5c 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 64  system support d
16f5d 65 6c 65 74 69 6f 6e 20 6f 66 20 6f 70 65 6e 20  eletion of open 
16f5e 66 69 6c 65 73 2c 20 74 68 65 6e 0d 0a 20 20 20  files, then..   
16f5f 20 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f   ** close the jo
16f60 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20  urnal file when 
16f61 64 72 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74  dropping the dat
16f62 61 62 61 73 65 20 6c 6f 63 6b 2e 20 20 4f 74 68  abase lock.  Oth
16f63 65 72 77 69 73 65 0d 0a 20 20 20 20 2a 2a 20 61  erwise..    ** a
16f64 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f  nother connectio
16f65 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d  n with journal_m
16f66 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74  ode=delete might
16f67 20 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65   delete the file
16f68 0d 0a 20 20 20 20 2a 2a 20 6f 75 74 20 66 72 6f  ..    ** out fro
16f69 6d 20 75 6e 64 65 72 20 75 73 2e 0d 0a 20 20 20  m under us...   
16f6a 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
16f6b 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d   (PAGER_JOURNALM
16f6c 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 26 20 35  ODE_MEMORY   & 5
16f6d 29 21 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73 73  )!=1 );..    ass
16f6e 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52  ert( (PAGER_JOUR
16f6f 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20  NALMODE_OFF     
16f70 20 26 20 35 29 21 3d 31 20 29 3b 0d 0a 20 20 20   & 5)!=1 );..   
16f71 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f   assert( (PAGER_
16f72 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20  JOURNALMODE_WAL 
16f73 20 20 20 20 20 26 20 35 29 21 3d 31 20 29 3b 0d       & 5)!=1 );.
16f74 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41  .    assert( (PA
16f75 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
16f76 44 45 4c 45 54 45 20 20 20 26 20 35 29 21 3d 31  DELETE   & 5)!=1
16f77 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
16f78 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d   (PAGER_JOURNALM
16f79 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 26 20 35  ODE_TRUNCATE & 5
16f7a 29 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73 73  )==1 );..    ass
16f7b 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52  ert( (PAGER_JOUR
16f7c 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20  NALMODE_PERSIST 
16f7d 20 26 20 35 29 3d 3d 31 20 29 3b 0d 0a 20 20 20   & 5)==1 );..   
16f7e 20 69 66 28 20 30 3d 3d 28 69 44 63 20 26 20 53   if( 0==(iDc & S
16f7f 51 4c 49 54 45 5f 49 4f 43 41 50 5f 55 4e 44 45  QLITE_IOCAP_UNDE
16f80 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f 4f 50 45  LETABLE_WHEN_OPE
16f81 4e 29 0d 0a 20 20 20 20 20 7c 7c 20 31 21 3d 28  N)..     || 1!=(
16f82 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
16f83 6f 64 65 20 26 20 35 29 0d 0a 20 20 20 20 29 7b  ode & 5)..    ){
16f84 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ..      sqlite3O
16f85 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
16f86 66 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  fd);..    }.... 
16f87 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67     /* If the pag
16f88 65 72 20 69 73 20 69 6e 20 74 68 65 20 45 52 52  er is in the ERR
16f89 4f 52 20 73 74 61 74 65 20 61 6e 64 20 74 68 65  OR state and the
16f8a 20 63 61 6c 6c 20 74 6f 20 75 6e 6c 6f 63 6b 20   call to unlock 
16f8b 74 68 65 20 64 61 74 61 62 61 73 65 0d 0a 20 20  the database..  
16f8c 20 20 2a 2a 20 66 69 6c 65 20 66 61 69 6c 73 2c    ** file fails,
16f8d 20 73 65 74 20 74 68 65 20 63 75 72 72 65 6e 74   set the current
16f8e 20 6c 6f 63 6b 20 74 6f 20 55 4e 4b 4e 4f 57 4e   lock to UNKNOWN
16f8f 5f 4c 4f 43 4b 2e 20 53 65 65 20 74 68 65 20 63  _LOCK. See the c
16f90 6f 6d 6d 65 6e 74 0d 0a 20 20 20 20 2a 2a 20 61  omment..    ** a
16f91 62 6f 76 65 20 74 68 65 20 23 64 65 66 69 6e 65  bove the #define
16f92 20 66 6f 72 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43   for UNKNOWN_LOC
16f93 4b 20 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61  K for an explana
16f94 74 69 6f 6e 20 6f 66 20 77 68 79 20 74 68 69 73  tion of why this
16f95 0d 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 63 65  ..    ** is nece
16f96 73 73 61 72 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  ssary...    */..
16f97 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 55 6e      rc = pagerUn
16f98 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 4e  lockDb(pPager, N
16f99 4f 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 69 66  O_LOCK);..    if
16f9a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
16f9b 26 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  && pPager->eStat
16f9c 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29  e==PAGER_ERROR )
16f9d 7b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  {..      pPager-
16f9e 3e 65 4c 6f 63 6b 20 3d 20 55 4e 4b 4e 4f 57 4e  >eLock = UNKNOWN
16f9f 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  _LOCK;..    }...
16fa0 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65  .    /* The page
16fa1 72 20 73 74 61 74 65 20 6d 61 79 20 62 65 20 63  r state may be c
16fa2 68 61 6e 67 65 64 20 66 72 6f 6d 20 50 41 47 45  hanged from PAGE
16fa3 52 5f 45 52 52 4f 52 20 74 6f 20 50 41 47 45 52  R_ERROR to PAGER
16fa4 5f 4f 50 45 4e 20 68 65 72 65 0d 0a 20 20 20 20  _OPEN here..    
16fa5 2a 2a 20 77 69 74 68 6f 75 74 20 63 6c 65 61 72  ** without clear
16fa6 69 6e 67 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ing the error co
16fa7 64 65 2e 20 54 68 69 73 20 69 73 20 69 6e 74 65  de. This is inte
16fa8 6e 74 69 6f 6e 61 6c 20 2d 20 74 68 65 20 65 72  ntional - the er
16fa9 72 6f 72 0d 0a 20 20 20 20 2a 2a 20 63 6f 64 65  ror..    ** code
16faa 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20   is cleared and 
16fab 74 68 65 20 63 61 63 68 65 20 72 65 73 65 74 20  the cache reset 
16fac 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c  in the block bel
16fad 6f 77 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ow...    */..   
16fae 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
16faf 3e 65 72 72 43 6f 64 65 20 7c 7c 20 70 50 61 67  >errCode || pPag
16fb0 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45  er->eState!=PAGE
16fb1 52 5f 45 52 52 4f 52 20 29 3b 0d 0a 20 20 20 20  R_ERROR );..    
16fb2 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
16fb3 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0d 0a 20 20  untDone = 0;..  
16fb4 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65    pPager->eState
16fb5 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0d 0a   = PAGER_OPEN;..
16fb6 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 50    }....  /* If P
16fb7 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20  ager.errCode is 
16fb8 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74  set, the content
16fb9 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63  s of the pager c
16fba 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0d 0a  ache cannot be..
16fbb 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f    ** trusted. No
16fbc 77 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  w that there are
16fbd 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
16fbe 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
16fbf 65 20 70 61 67 65 72 2c 0d 0a 20 20 2a 2a 20 69  e pager,..  ** i
16fc0 74 20 63 61 6e 20 73 61 66 65 6c 79 20 6d 6f 76  t can safely mov
16fc1 65 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f  e back to PAGER_
16fc2 4f 50 45 4e 20 73 74 61 74 65 2e 20 54 68 69 73  OPEN state. This
16fc3 20 68 61 70 70 65 6e 73 20 69 6e 20 62 6f 74 68   happens in both
16fc4 0d 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 61 6e  ..  ** normal an
16fc5 64 20 65 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b  d exclusive-lock
16fc6 69 6e 67 20 6d 6f 64 65 2e 0d 0a 20 20 2a 2f 0d  ing mode...  */.
16fc7 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
16fc8 72 72 43 6f 64 65 20 29 7b 0d 0a 20 20 20 20 61  rrCode ){..    a
16fc9 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b  ssert( !MEMDB );
16fca 0d 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73 65  ..    pager_rese
16fcb 74 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20  t(pPager);..    
16fcc 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
16fcd 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72  untDone = pPager
16fce 2d 3e 74 65 6d 70 46 69 6c 65 3b 0d 0a 20 20 20  ->tempFile;..   
16fcf 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20   pPager->eState 
16fd0 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0d 0a 20  = PAGER_OPEN;.. 
16fd1 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
16fd2 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  de = SQLITE_OK;.
16fd3 0a 20 20 7d 0d 0a 0d 0a 20 20 70 50 61 67 65 72  .  }....  pPager
16fd4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
16fd5 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ;..  pPager->jou
16fd6 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0d 0a 20 20  rnalHdr = 0;..  
16fd7 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
16fd8 72 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  r = 0;..}..../*.
16fd9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
16fda 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
16fdb 65 76 65 72 20 61 6e 20 49 4f 45 52 52 20 6f 72  ever an IOERR or
16fdc 20 46 55 4c 4c 20 65 72 72 6f 72 20 74 68 61 74   FULL error that
16fdd 20 72 65 71 75 69 72 65 73 0d 0a 2a 2a 20 74 68   requires..** th
16fde 65 20 70 61 67 65 72 20 74 6f 20 74 72 61 6e 73  e pager to trans
16fdf 69 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 45  ition into the E
16fe0 52 52 4f 52 20 73 74 61 74 65 20 6d 61 79 20 61  RROR state may a
16fe1 68 76 65 20 6f 63 63 75 72 72 65 64 2e 0d 0a 2a  hve occurred...*
16fe2 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
16fe3 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
16fe4 72 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 73  r to the pager s
16fe5 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 73 65  tructure, the se
16fe6 63 6f 6e 64 20 0d 0a 2a 2a 20 74 68 65 20 65 72  cond ..** the er
16fe7 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74  ror-code about t
16fe8 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  o be returned by
16fe9 20 61 20 70 61 67 65 72 20 41 50 49 20 66 75 6e   a pager API fun
16fea 63 74 69 6f 6e 2e 20 54 68 65 20 0d 0a 2a 2a 20  ction. The ..** 
16feb 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
16fec 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
16fed 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
16fee 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
16fef 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  . ..**..** If th
16ff0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
16ff1 74 20 69 73 20 53 51 4c 49 54 45 5f 46 55 4c 4c  t is SQLITE_FULL
16ff2 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 6f  , SQLITE_IOERR o
16ff3 72 20 6f 6e 65 20 6f 66 20 74 68 65 0d 0a 2a 2a  r one of the..**
16ff4 20 49 4f 45 52 52 20 73 75 62 2d 63 6f 64 65 73   IOERR sub-codes
16ff5 2c 20 74 68 65 20 70 61 67 65 72 20 65 6e 74 65  , the pager ente
16ff6 72 73 20 74 68 65 20 45 52 52 4f 52 20 73 74 61  rs the ERROR sta
16ff7 74 65 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72  te and the error
16ff8 20 63 6f 64 65 0d 0a 2a 2a 20 69 73 20 73 74 6f   code..** is sto
16ff9 72 65 64 20 69 6e 20 50 61 67 65 72 2e 65 72 72  red in Pager.err
16ffa 43 6f 64 65 2e 20 57 68 69 6c 65 20 74 68 65 20  Code. While the 
16ffb 70 61 67 65 72 20 72 65 6d 61 69 6e 73 20 69 6e  pager remains in
16ffc 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65   the ERROR state
16ffd 2c 0d 0a 2a 2a 20 61 6c 6c 20 6d 61 6a 6f 72 20  ,..** all major 
16ffe 41 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 65  API calls on the
16fff 20 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65   Pager will imme
17000 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 50  diately return P
17001 61 67 65 72 2e 65 72 72 43 6f 64 65 2e 0d 0a 2a  ager.errCode...*
17002 2a 0d 0a 2a 2a 20 54 68 65 20 45 52 52 4f 52 20  *..** The ERROR 
17003 73 74 61 74 65 20 69 6e 64 69 63 61 74 65 73 20  state indicates 
17004 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
17005 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
17006 61 63 68 65 20 0d 0a 2a 2a 20 63 61 6e 6e 6f 74  ache ..** cannot
17007 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69   be trusted. Thi
17008 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63  s state can be c
17009 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65  leared by comple
1700a 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20  tely discarding 
1700b 0d 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  ..** the content
1700c 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
1700d 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73  ache. If a trans
1700e 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76  action was activ
1700f 65 20 77 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 70  e when..** the p
17010 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20  ersistent error 
17011 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74  occurred, then t
17012 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
17013 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0d 0a 2a 2a  nal may need..**
17014 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20   to be replayed 
17015 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63  to restore the c
17016 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64  ontents of the d
17017 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 73  atabase file (as
17018 20 69 66 0d 0a 2a 2a 20 69 74 20 77 65 72 65 20   if..** it were 
17019 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0d  a hot-journal)..
1701a 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
1701b 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65  pager_error(Page
1701c 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72  r *pPager, int r
1701d 63 29 7b 0d 0a 20 20 69 6e 74 20 72 63 32 20 3d  c){..  int rc2 =
1701e 20 72 63 20 26 20 30 78 66 66 3b 0d 0a 20 20 61   rc & 0xff;..  a
1701f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
17020 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42 20 29  E_OK || !MEMDB )
17021 3b 0d 0a 20 20 61 73 73 65 72 74 28 0d 0a 20 20  ;..  assert(..  
17022 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72       pPager->err
17023 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c  Code==SQLITE_FUL
17024 4c 20 7c 7c 0d 0a 20 20 20 20 20 20 20 70 50 61  L ||..       pPa
17025 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
17026 4c 49 54 45 5f 4f 4b 20 7c 7c 0d 0a 20 20 20 20  LITE_OK ||..    
17027 20 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43     (pPager->errC
17028 6f 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c  ode & 0xff)==SQL
17029 49 54 45 5f 49 4f 45 52 52 0d 0a 20 20 29 3b 0d  ITE_IOERR..  );.
1702a 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49  .  if( rc2==SQLI
1702b 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d  TE_FULL || rc2==
1702c 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0d  SQLITE_IOERR ){.
1702d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72  .    pPager->err
1702e 43 6f 64 65 20 3d 20 72 63 3b 0d 0a 20 20 20 20  Code = rc;..    
1702f 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d  pPager->eState =
17030 20 50 41 47 45 52 5f 45 52 52 4f 52 3b 0d 0a 20   PAGER_ERROR;.. 
17031 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
17032 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
17033 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20  is routine ends 
17034 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  a transaction. A
17035 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
17036 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79  usually ended by
17037 20 0d 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 43   ..** either a C
17038 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42  OMMIT or a ROLLB
17039 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54  ACK operation. T
1703a 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 20  his routine may 
1703b 62 65 20 63 61 6c 6c 65 64 20 0d 0a 2a 2a 20 61  be called ..** a
1703c 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  fter rollback of
1703d 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20   a hot-journal, 
1703e 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  or if an error o
1703f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e  ccurs while open
17040 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  ing..** the jour
17041 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74  nal file or writ
17042 69 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72  ing the very fir
17043 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65  st journal-heade
17044 72 20 6f 66 20 61 0d 0a 2a 2a 20 64 61 74 61 62  r of a..** datab
17045 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ase transaction.
17046 0d 0a 2a 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 72  ..** ..** This r
17047 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20  outine is never 
17048 63 61 6c 6c 65 64 20 69 6e 20 50 41 47 45 52 5f  called in PAGER_
17049 45 52 52 4f 52 20 73 74 61 74 65 2e 20 49 66 20  ERROR state. If 
1704a 69 74 20 69 73 20 63 61 6c 6c 65 64 0d 0a 2a 2a  it is called..**
1704b 20 69 6e 20 50 41 47 45 52 5f 4e 4f 4e 45 20 6f   in PAGER_NONE o
1704c 72 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73  r PAGER_SHARED s
1704d 74 61 74 65 20 61 6e 64 20 74 68 65 20 6c 6f 63  tate and the loc
1704e 6b 20 68 65 6c 64 20 69 73 20 6c 65 73 73 0d 0a  k held is less..
1704f 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 74 68 61  ** exclusive tha
17050 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  n a RESERVED loc
17051 6b 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70  k, it is a no-op
17052 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77  ...**..** Otherw
17053 69 73 65 2c 20 61 6e 79 20 61 63 74 69 76 65 20  ise, any active 
17054 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 72  savepoints are r
17055 65 6c 65 61 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  eleased...**..**
17056 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
17057 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 68  file is open, th
17058 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61 6c 69  en it is "finali
17059 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a 6f 75  zed". Once a jou
1705a 72 6e 61 6c 20 0d 0a 2a 2a 20 66 69 6c 65 20 68  rnal ..** file h
1705b 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65  as been finalize
1705c 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  d it is not poss
1705d 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20 74  ible to use it t
1705e 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0d 0a  o roll back a ..
1705f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
17060 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63  Nor will it be c
17061 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
17062 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79  a hot-journal by
17063 20 74 68 69 73 0d 0a 2a 2a 20 6f 72 20 61 6e 79   this..** or any
17064 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
17065 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63  connection. Exac
17066 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61  tly how a journa
17067 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0d 0a  l is finalized..
17068 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68  ** depends on wh
17069 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
1706a 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
1706b 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
1706c 6f 64 65 20 61 6e 64 0d 0a 2a 2a 20 74 68 65 20  ode and..** the 
1706d 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d  current journal-
1706e 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72  mode (Pager.jour
1706f 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20  nalMode value), 
17070 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d  as follows:..**.
17071 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64  .**   journalMod
17072 65 3d 3d 4d 45 4d 4f 52 59 0d 0a 2a 2a 20 20 20  e==MEMORY..**   
17073 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64    Journal file d
17074 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69 6d  escriptor is sim
17075 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 73  ply closed. This
17076 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0d 0a 2a   destroys an ..*
17077 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20  *     in-memory 
17078 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a  journal...**..**
17079 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d     journalMode==
1707a 54 52 55 4e 43 41 54 45 0d 0a 2a 2a 20 20 20 20  TRUNCATE..**    
1707b 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   Journal file is
1707c 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65   truncated to ze
1707d 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ro bytes in size
1707e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 6a 6f 75 72  ...**..**   jour
1707f 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54  nalMode==PERSIST
17080 0d 0a 2a 2a 20 20 20 20 20 54 68 65 20 66 69 72  ..**     The fir
17081 73 74 20 32 38 20 62 79 74 65 73 20 6f 66 20 74  st 28 bytes of t
17082 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
17083 61 72 65 20 7a 65 72 6f 65 64 2e 20 54 68 69 73  are zeroed. This
17084 20 69 6e 76 61 6c 69 64 61 74 65 73 0d 0a 2a 2a   invalidates..**
17085 20 20 20 20 20 74 68 65 20 66 69 72 73 74 20 6a       the first j
17086 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e  ournal header in
17087 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 20 68   the file, and h
17088 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72 65 20  ence the entire 
17089 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 20 20 20 20  journal..**     
1708a 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69 64  file. An invalid
1708b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 61   journal file ca
1708c 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62  nnot be rolled b
1708d 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 6a  ack...**..**   j
1708e 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45  ournalMode==DELE
1708f 54 45 0d 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a  TE..**     The j
17090 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63  ournal file is c
17091 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65  losed and delete
17092 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f  d using sqlite3O
17093 73 44 65 6c 65 74 65 28 29 2e 0d 0a 2a 2a 0d 0a  sDelete()...**..
17094 2a 2a 20 20 20 20 20 49 66 20 74 68 65 20 70 61  **     If the pa
17095 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69  ger is running i
17096 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
17097 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66  , this method of
17098 20 66 69 6e 61 6c 69 7a 69 6e 67 0d 0a 2a 2a 20   finalizing..** 
17099 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20      the journal 
1709a 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 75 73  file is never us
1709b 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20  ed. Instead, if 
1709c 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20  the journalMode 
1709d 69 73 0d 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54  is..**     DELET
1709e 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20  E and the pager 
1709f 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  is in exclusive 
170a0 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f 64  mode, the method
170a1 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65 72   described under
170a2 0d 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c  ..**     journal
170a3 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73  Mode==PERSIST is
170a4 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0d 0a   used instead...
170a5 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  **..** After the
170a6 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61   journal is fina
170a7 6c 69 7a 65 64 2c 20 74 68 65 20 70 61 67 65 72  lized, the pager
170a8 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f   moves to PAGER_
170a9 52 45 41 44 45 52 20 73 74 61 74 65 2e 0d 0a 2a  READER state...*
170aa 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20  * If running in 
170ab 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 72 6f  non-exclusive ro
170ac 6c 6c 62 61 63 6b 20 6d 6f 64 65 2c 20 74 68 65  llback mode, the
170ad 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c   lock on the fil
170ae 65 20 69 73 20 0d 0a 2a 2a 20 64 6f 77 6e 67 72  e is ..** downgr
170af 61 64 65 64 20 74 6f 20 61 20 53 48 41 52 45 44  aded to a SHARED
170b0 5f 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53  _LOCK...**..** S
170b1 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
170b2 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72  rned if no error
170b3 20 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65   occurs. If an e
170b4 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
170b5 6e 67 0d 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68  ng..** any of th
170b6 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20  e IO operations 
170b7 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20  to finalize the 
170b8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
170b9 75 6e 6c 6f 63 6b 20 74 68 65 0d 0a 2a 2a 20 64  unlock the..** d
170ba 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68 65  atabase then the
170bb 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
170bc 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  s returned to th
170bd 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20 0d  e user. If the .
170be 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  .** operation to
170bf 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f   finalize the jo
170c0 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73  urnal file fails
170c1 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20  , then the code 
170c2 73 74 69 6c 6c 0d 0a 2a 2a 20 74 72 69 65 73 20  still..** tries 
170c3 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  to unlock the da
170c4 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e  tabase file if n
170c5 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  ot in exclusive 
170c6 6d 6f 64 65 2e 20 49 66 20 74 68 65 0d 0a 2a 2a  mode. If the..**
170c7 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f   unlock operatio
170c8 6e 20 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c  n fails as well,
170c9 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
170ca 65 72 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74  error code relat
170cb 65 64 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69  ed..** to the fi
170cc 72 73 74 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e  rst error encoun
170cd 74 65 72 65 64 20 28 74 68 65 20 6a 6f 75 72 6e  tered (the journ
170ce 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20  al finalization 
170cf 6f 6e 65 29 20 69 73 0d 0a 2a 2a 20 72 65 74 75  one) is..** retu
170d0 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rned...*/..stati
170d1 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f  c int pager_end_
170d2 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65  transaction(Page
170d3 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68  r *pPager, int h
170d4 61 73 4d 61 73 74 65 72 29 7b 0d 0a 20 20 69 6e  asMaster){..  in
170d5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
170d6 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  ;      /* Error 
170d7 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61  code from journa
170d8 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f  l finalization o
170d9 70 65 72 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69  peration */..  i
170da 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f  nt rc2 = SQLITE_
170db 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  OK;     /* Error
170dc 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69   code from db fi
170dd 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74  le unlock operat
170de 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 44  ion */....  /* D
170df 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 68 65  o nothing if the
170e0 20 70 61 67 65 72 20 64 6f 65 73 20 6e 6f 74 20   pager does not 
170e1 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69  have an open wri
170e2 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0d 0a  te transaction..
170e3 20 20 2a 2a 20 6f 72 20 61 74 20 6c 65 61 73 74    ** or at least
170e4 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
170e5 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
170e6 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68  may be called wh
170e7 65 6e 20 74 68 65 72 65 0d 0a 20 20 2a 2a 20 69  en there..  ** i
170e8 73 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73  s no write-trans
170e9 61 63 74 69 6f 6e 20 61 63 74 69 76 65 20 62 75  action active bu
170ea 74 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  t a RESERVED or 
170eb 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 0d  greater lock is.
170ec 0a 20 20 2a 2a 20 68 65 6c 64 20 75 6e 64 65 72  .  ** held under
170ed 20 74 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63   two circumstanc
170ee 65 73 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  es:..  **..  ** 
170ef 20 20 31 2e 20 41 66 74 65 72 20 61 20 73 75 63    1. After a suc
170f0 63 65 73 73 66 75 6c 20 68 6f 74 2d 6a 6f 75 72  cessful hot-jour
170f1 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74  nal rollback, it
170f2 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0d   is called with.
170f3 0a 20 20 2a 2a 20 20 20 20 20 20 65 53 74 61 74  .  **      eStat
170f4 65 3d 3d 50 41 47 45 52 5f 4e 4f 4e 45 20 61 6e  e==PAGER_NONE an
170f5 64 20 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49  d eLock==EXCLUSI
170f6 56 45 5f 4c 4f 43 4b 2e 0d 0a 20 20 2a 2a 0d 0a  VE_LOCK...  **..
170f7 20 20 2a 2a 20 20 20 32 2e 20 49 66 20 61 20 63    **   2. If a c
170f8 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6c  onnection with l
170f9 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c  ocking_mode=excl
170fa 75 73 69 76 65 20 68 6f 6c 64 69 6e 67 20 61 6e  usive holding an
170fb 20 45 58 43 4c 55 53 49 56 45 20 0d 0a 20 20 2a   EXCLUSIVE ..  *
170fc 2a 20 20 20 20 20 20 6c 6f 63 6b 20 73 77 69 74  *      lock swit
170fd 63 68 65 73 20 62 61 63 6b 20 74 6f 20 6c 6f 63  ches back to loc
170fe 6b 69 6e 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61 6c  king_mode=normal
170ff 20 61 6e 64 20 74 68 65 6e 20 65 78 65 63 75 74   and then execut
17100 65 73 20 61 0d 0a 20 20 2a 2a 20 20 20 20 20 20  es a..  **      
17101 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
17102 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
17103 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 65  is called with e
17104 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41  State==PAGER_REA
17105 44 45 52 20 0d 0a 20 20 2a 2a 20 20 20 20 20 20  DER ..  **      
17106 61 6e 64 20 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55  and eLock==EXCLU
17107 53 49 56 45 5f 4c 4f 43 4b 20 77 68 65 6e 20 74  SIVE_LOCK when t
17108 68 65 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74  he read-transact
17109 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0d 0a  ion is closed...
1710a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20    */..  assert( 
1710b 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
1710c 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20  te(pPager) );.. 
1710d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1710e 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45  >eState!=PAGER_E
1710f 52 52 4f 52 20 29 3b 0d 0a 20 20 69 66 28 20 70  RROR );..  if( p
17110 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c 50 41  Pager->eState<PA
17111 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45  GER_WRITER_LOCKE
17112 44 20 26 26 20 70 50 61 67 65 72 2d 3e 65 4c 6f  D && pPager->eLo
17113 63 6b 3c 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  ck<RESERVED_LOCK
17114 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
17115 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d  SQLITE_OK;..  }.
17116 0a 0d 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53  ...  releaseAllS
17117 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72  avepoints(pPager
17118 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73  );..  assert( is
17119 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1711a 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e  ) || pPager->pIn
1711b 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0d 0a 20  Journal==0 );.. 
1711c 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
1711d 65 72 2d 3e 6a 66 64 29 20 29 7b 0d 0a 20 20 20  er->jfd) ){..   
1711e 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55   assert( !pagerU
1711f 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b  seWal(pPager) );
17120 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c  ....    /* Final
17121 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
17122 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20 20 20 69 66  file. */..    if
17123 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f  ( sqlite3IsMemJo
17124 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66  urnal(pPager->jf
17125 64 29 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  d) ){..      ass
17126 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
17127 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
17128 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
17129 52 59 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  RY );..      sql
1712a 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
1712b 65 72 2d 3e 6a 66 64 29 3b 0d 0a 20 20 20 20 7d  er->jfd);..    }
1712c 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d  else if( pPager-
1712d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
1712e 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
1712f 54 52 55 4e 43 41 54 45 20 29 7b 0d 0a 20 20 20  TRUNCATE ){..   
17130 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
17131 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0d  ournalOff==0 ){.
17132 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
17133 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20  LITE_OK;..      
17134 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
17135 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
17136 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
17137 66 64 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d  fd, 0);..      }
17138 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ..      pPager->
17139 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0d  journalOff = 0;.
1713a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1713b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1713c 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
1713d 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0d 0a 20  LMODE_PERSIST.. 
1713e 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d       || (pPager-
1713f 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26  >exclusiveMode &
17140 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
17141 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
17142 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 29 0d 0a 20  RNALMODE_WAL).. 
17143 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20     ){..      rc 
17144 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72  = zeroJournalHdr
17145 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74  (pPager, hasMast
17146 65 72 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 67  er);..      pPag
17147 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
17148 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   0;..    }else{.
17149 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
1714a 72 61 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65  ranch may be exe
1714b 63 75 74 65 64 20 77 69 74 68 20 50 61 67 65 72  cuted with Pager
1714c 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45  .journalMode==ME
1714d 4d 4f 52 59 20 69 66 0d 0a 20 20 20 20 20 20 2a  MORY if..      *
1714e 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  * a hot-journal 
1714f 77 61 73 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20  was just rolled 
17150 62 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 61  back. In this ca
17151 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0d 0a  se the journal..
17152 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 73 68        ** file sh
17153 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 61  ould be closed a
17154 6e 64 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74  nd deleted. If t
17155 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  his connection w
17156 72 69 74 65 73 20 74 6f 0d 0a 20 20 20 20 20 20  rites to..      
17157 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
17158 66 69 6c 65 2c 20 69 74 20 77 69 6c 6c 20 64 6f  file, it will do
17159 20 73 6f 20 75 73 69 6e 67 20 61 6e 20 69 6e 2d   so using an in-
1715a 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 20  memory journal. 
1715b 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
1715c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1715d 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
1715e 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
1715f 5f 44 45 4c 45 54 45 20 0d 0a 20 20 20 20 20 20  _DELETE ..      
17160 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
17161 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
17162 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
17163 45 4d 4f 52 59 20 0d 0a 20 20 20 20 20 20 20 20  EMORY ..        
17164 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f     || pPager->jo
17165 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
17166 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c  _JOURNALMODE_WAL
17167 20 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20 20   ..      );..   
17168 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
17169 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0d  e(pPager->jfd);.
1716a 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67  .      if( !pPag
1716b 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0d  er->tempFile ){.
1716c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1716d 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50  lite3OsDelete(pP
1716e 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67  ager->pVfs, pPag
1716f 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29  er->zJournal, 0)
17170 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
17171 7d 0d 0a 20 20 7d 0d 0a 0d 0a 23 69 66 64 65 66  }..  }....#ifdef
17172 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
17173 47 45 53 0d 0a 20 20 73 71 6c 69 74 65 33 50 63  GES..  sqlite3Pc
17174 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79  acheIterateDirty
17175 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
17176 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65  , pager_set_page
17177 68 61 73 68 29 3b 0d 0a 20 20 69 66 28 20 70 50  hash);..  if( pP
17178 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 20  ager->dbSize==0 
17179 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  && sqlite3Pcache
1717a 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
1717b 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0d 0a  >pPCache)>0 ){..
1717c 20 20 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70      PgHdr *p = p
1717d 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
1717e 65 72 2c 20 31 29 3b 0d 0a 20 20 20 20 69 66 28  er, 1);..    if(
1717f 20 70 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e   p ){..      p->
17180 70 61 67 65 48 61 73 68 20 3d 20 30 3b 0d 0a 20  pageHash = 0;.. 
17181 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
17182 72 55 6e 72 65 66 28 70 29 3b 0d 0a 20 20 20 20  rUnref(p);..    
17183 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  }..  }..#endif..
17184 0d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  ..  sqlite3Bitve
17185 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
17186 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0d 0a 20  >pInJournal);.. 
17187 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
17188 6e 61 6c 20 3d 20 30 3b 0d 0a 20 20 70 50 61 67  nal = 0;..  pPag
17189 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0d 0a 20  er->nRec = 0;.. 
1718a 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
1718b 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70  eanAll(pPager->p
1718c 50 43 61 63 68 65 29 3b 0d 0a 20 20 73 71 6c 69  PCache);..  sqli
1718d 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74  te3PcacheTruncat
1718e 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  e(pPager->pPCach
1718f 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  e, pPager->dbSiz
17190 65 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 61 67  e);....  if( pag
17191 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
17192 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 44 72 6f 70   ){..    /* Drop
17193 20 74 68 65 20 57 41 4c 20 77 72 69 74 65 2d 6c   the WAL write-l
17194 6f 63 6b 2c 20 69 66 20 61 6e 79 2e 20 41 6c 73  ock, if any. Als
17195 6f 2c 20 69 66 20 74 68 65 20 63 6f 6e 6e 65 63  o, if the connec
17196 74 69 6f 6e 20 77 61 73 20 69 6e 20 0d 0a 20 20  tion was in ..  
17197 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64    ** locking_mod
17198 65 3d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  e=exclusive mode
17199 20 62 75 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65   but is no longe
1719a 72 2c 20 64 72 6f 70 20 74 68 65 20 45 58 43 4c  r, drop the EXCL
1719b 55 53 49 56 45 20 0d 0a 20 20 20 20 2a 2a 20 6c  USIVE ..    ** l
1719c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
1719d 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
1719e 20 20 20 20 2a 2f 0d 0a 20 20 20 20 72 63 32 20      */..    rc2 
1719f 3d 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 57  = sqlite3WalEndW
171a0 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f 6e 28  riteTransaction(
171a1 70 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0d 0a  pPager->pWal);..
171a2 20 20 20 20 61 73 73 65 72 74 28 20 72 63 32 3d      assert( rc2=
171a3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20  =SQLITE_OK );.. 
171a4 20 7d 0d 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
171a5 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
171a6 20 0d 0a 20 20 20 26 26 20 28 21 70 61 67 65 72   ..   && (!pager
171a7 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 7c  UseWal(pPager) |
171a8 7c 20 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c  | sqlite3WalExcl
171a9 75 73 69 76 65 4d 6f 64 65 28 70 50 61 67 65 72  usiveMode(pPager
171aa 2d 3e 70 57 61 6c 2c 20 30 29 29 0d 0a 20 20 29  ->pWal, 0))..  )
171ab 7b 0d 0a 20 20 20 20 72 63 32 20 3d 20 70 61 67  {..    rc2 = pag
171ac 65 72 55 6e 6c 6f 63 6b 44 62 28 70 50 61 67 65  erUnlockDb(pPage
171ad 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  r, SHARED_LOCK);
171ae 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68  ..    pPager->ch
171af 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
171b0 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50 61 67 65  0;..  }..  pPage
171b1 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 45  r->eState = PAGE
171b2 52 5f 52 45 41 44 45 52 3b 0d 0a 20 20 70 50 61  R_READER;..  pPa
171b3 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
171b4 20 30 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20   0;....  return 
171b5 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72  (rc==SQLITE_OK?r
171b6 63 32 3a 72 63 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  c2:rc);..}..../*
171b7 0d 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 72  ..** Execute a r
171b8 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 61  ollback if a tra
171b9 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
171ba 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68  ve and unlock th
171bb 65 20 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  e ..** database 
171bc 66 69 6c 65 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49  file. ..**..** I
171bd 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  f the pager has 
171be 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20  already entered 
171bf 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c  the ERROR state,
171c0 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
171c1 0d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63  ..** the rollbac
171c2 6b 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20  k at this time. 
171c3 49 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75  Instead, pager_u
171c4 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65  nlock() is calle
171c5 64 2e 20 54 68 65 0d 0a 2a 2a 20 63 61 6c 6c 20  d. The..** call 
171c6 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28  to pager_unlock(
171c7 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61  ) will discard a
171c8 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  ll in-memory pag
171c9 65 73 2c 20 75 6e 6c 6f 63 6b 0d 0a 2a 2a 20 74  es, unlock..** t
171ca 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
171cb 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 70 61   and move the pa
171cc 67 65 72 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e  ger back to OPEN
171cd 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20   state. If this 
171ce 0d 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  ..** means that 
171cf 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a  there is a hot-j
171d0 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74  ournal left in t
171d1 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20  he file-system, 
171d2 74 68 65 20 6e 65 78 74 20 0d 0a 2a 2a 20 63 6f  the next ..** co
171d3 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6f 62 74 61  nnection to obta
171d4 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  in a shared lock
171d5 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28 77   on the pager (w
171d6 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69 73  hich may be this
171d7 20 6f 6e 65 29 20 0d 0a 2a 2a 20 77 69 6c 6c 20   one) ..** will 
171d8 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0d 0a 2a  roll it back...*
171d9 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  *..** If the pag
171da 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61  er has not alrea
171db 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 45  dy entered the E
171dc 52 52 4f 52 20 73 74 61 74 65 2c 20 62 75 74 20  RROR state, but 
171dd 61 6e 20 49 4f 20 6f 72 0d 0a 2a 2a 20 6d 61 6c  an IO or..** mal
171de 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73  loc error occurs
171df 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
171e0 63 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 69  ck, then this wi
171e1 6c 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 20  ll itself cause 
171e2 0d 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74  ..** the pager t
171e3 6f 20 65 6e 74 65 72 20 74 68 65 20 45 52 52 4f  o enter the ERRO
171e4 52 20 73 74 61 74 65 2e 20 57 68 69 63 68 20 77  R state. Which w
171e5 69 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62  ill be cleared b
171e6 79 20 74 68 65 0d 0a 2a 2a 20 63 61 6c 6c 20 74  y the..** call t
171e7 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29  o pager_unlock()
171e8 2c 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61  , as described a
171e9 62 6f 76 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  bove...*/..stati
171ea 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f  c void pagerUnlo
171eb 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61  ckAndRollback(Pa
171ec 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20  ger *pPager){.. 
171ed 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 74   if( pPager->eSt
171ee 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52  ate!=PAGER_ERROR
171ef 20 26 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61   && pPager->eSta
171f0 74 65 21 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29  te!=PAGER_OPEN )
171f1 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  {..    assert( a
171f2 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
171f3 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20  e(pPager) );..  
171f4 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53    if( pPager->eS
171f5 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 54  tate>=PAGER_WRIT
171f6 45 52 5f 4c 4f 43 4b 45 44 20 29 7b 0d 0a 20 20  ER_LOCKED ){..  
171f7 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
171f8 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d  BenignMalloc();.
171f9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
171fa 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67  gerRollback(pPag
171fb 65 72 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  er);..      sqli
171fc 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
171fd 6f 63 28 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  oc();..    }else
171fe 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78   if( !pPager->ex
171ff 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0d 0a  clusiveMode ){..
17200 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
17201 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
17202 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0d 0a 20  GER_READER );.. 
17203 20 20 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74       pager_end_t
17204 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
17205 72 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  r, 0);..    }.. 
17206 20 7d 0d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f   }..  pager_unlo
17207 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 7d 0d 0a  ck(pPager);..}..
17208 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74  ../*..** Paramet
17209 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 6f  er aData must po
1720a 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
1720b 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  of pPager->pageS
1720c 69 7a 65 20 62 79 74 65 73 0d 0a 2a 2a 20 6f 66  ize bytes..** of
1720d 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61   data. Compute a
1720e 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63  nd return a chec
1720f 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74  ksum based ont t
17210 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
17211 68 65 20 0d 0a 2a 2a 20 70 61 67 65 20 6f 66 20  he ..** page of 
17212 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72  data and the cur
17213 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50  rent value of pP
17214 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e  ager->cksumInit.
17215 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73  ..**..** This is
17216 20 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63   not a real chec
17217 6b 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c  ksum. It is real
17218 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20  ly just the sum 
17219 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 72 61 6e 64  of the ..** rand
1721a 6f 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  om initial value
1721b 20 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49   (pPager->cksumI
1721c 6e 69 74 29 20 61 6e 64 20 65 76 65 72 79 20 32  nit) and every 2
1721d 30 30 74 68 20 62 79 74 65 0d 0a 2a 2a 20 6f 66  00th byte..** of
1721e 20 74 68 65 20 70 61 67 65 20 64 61 74 61 2c 20   the page data, 
1721f 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 62 79  starting with by
17220 74 65 20 6f 66 66 73 65 74 20 28 70 50 61 67 65  te offset (pPage
17221 72 2d 3e 70 61 67 65 53 69 7a 65 25 32 30 30 29  r->pageSize%200)
17222 2e 0d 0a 2a 2a 20 45 61 63 68 20 62 79 74 65 20  ...** Each byte 
17223 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
17224 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67  s an 8-bit unsig
17225 6e 65 64 20 69 6e 74 65 67 65 72 2e 0d 0a 2a 2a  ned integer...**
17226 0d 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  ..** Changing th
17227 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64 20 74  e formula used t
17228 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73 20 63  o compute this c
17229 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74 73 20  hecksum results 
1722a 69 6e 20 61 6e 0d 0a 2a 2a 20 69 6e 63 6f 6d 70  in an..** incomp
1722b 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20 66  atible journal f
1722c 69 6c 65 20 66 6f 72 6d 61 74 2e 0d 0a 2a 2a 0d  ile format...**.
1722d 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63  .** If journal c
1722e 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73  orruption occurs
1722f 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20   due to a power 
17230 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73  failure, the mos
17231 74 20 6c 69 6b 65 6c 79 20 0d 0a 2a 2a 20 73 63  t likely ..** sc
17232 65 6e 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f  enario is that o
17233 6e 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74  ne end or the ot
17234 68 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72  her of the recor
17235 64 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65  d will be change
17236 64 2e 20 0d 0a 2a 2a 20 49 74 20 69 73 20 6d 75  d. ..** It is mu
17237 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74  ch less likely t
17238 68 61 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73  hat the two ends
17239 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
1723a 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 0d 0a  record will be..
1723b 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 20 74  ** correct and t
1723c 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 6f 72  he middle be cor
1723d 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 68 69  rupt.  Thus, thi
1723e 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 63 68  s "checksum" sch
1723f 65 6d 65 2c 0d 0a 2a 2a 20 74 68 6f 75 67 68 20  eme,..** though 
17240 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c  fast and simple,
17241 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f 73   catches the mos
17242 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20  tly likely kind 
17243 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0d 0a  of corruption...
17244 2a 2f 0d 0a 73 74 61 74 69 63 20 75 33 32 20 70  */..static u32 p
17245 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72  ager_cksum(Pager
17246 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20   *pPager, const 
17247 75 38 20 2a 61 44 61 74 61 29 7b 0d 0a 20 20 75  u8 *aData){..  u
17248 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65  32 cksum = pPage
17249 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20  r->cksumInit;   
1724a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75        /* Checksu
1724b 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72  m value to retur
1724c 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 20 3d 20  n */..  int i = 
1724d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1724e 2d 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f  -200;          /
1724f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
17250 2f 0d 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  /..  while( i>0 
17251 29 7b 0d 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d  ){..    cksum +=
17252 20 61 44 61 74 61 5b 69 5d 3b 0d 0a 20 20 20 20   aData[i];..    
17253 69 20 2d 3d 20 32 30 30 3b 0d 0a 20 20 7d 0d 0a  i -= 200;..  }..
17254 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d 3b 0d    return cksum;.
17255 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 70  .}..../*..** Rep
17256 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  ort the current 
17257 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75  page size and nu
17258 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64  mber of reserved
17259 20 62 79 74 65 73 20 62 61 63 6b 0d 0a 2a 2a 20   bytes back..** 
1725a 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0d 0a 2a  to the codec...*
1725b 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
1725c 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 73 74 61 74  _HAS_CODEC..stat
1725d 69 63 20 76 6f 69 64 20 70 61 67 65 72 52 65 70  ic void pagerRep
1725e 6f 72 74 53 69 7a 65 28 50 61 67 65 72 20 2a 70  ortSize(Pager *p
1725f 50 61 67 65 72 29 7b 0d 0a 20 20 69 66 28 20 70  Pager){..  if( p
17260 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a  Pager->xCodecSiz
17261 65 43 68 6e 67 20 29 7b 0d 0a 20 20 20 20 70 50  eChng ){..    pP
17262 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65  ager->xCodecSize
17263 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 43 6f  Chng(pPager->pCo
17264 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  dec, pPager->pag
17265 65 53 69 7a 65 2c 0d 0a 20 20 20 20 20 20 20 20  eSize,..        
17266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17267 20 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e     (int)pPager->
17268 6e 52 65 73 65 72 76 65 29 3b 0d 0a 20 20 7d 0d  nReserve);..  }.
17269 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66  .}..#else..# def
1726a 69 6e 65 20 70 61 67 65 72 52 65 70 6f 72 74 53  ine pagerReportS
1726b 69 7a 65 28 58 29 20 20 20 20 20 2f 2a 20 4e 6f  ize(X)     /* No
1726c 2d 6f 70 20 69 66 20 77 65 20 64 6f 20 6e 6f 74  -op if we do not
1726d 20 73 75 70 70 6f 72 74 20 61 20 63 6f 64 65 63   support a codec
1726e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f   */..#endif..../
1726f 2a 0d 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e  *..** Read a sin
17270 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69  gle page from ei
17271 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ther the journal
17272 20 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e   file (if isMain
17273 4a 72 6e 6c 3d 3d 31 29 20 6f 72 0d 0a 2a 2a 20  Jrnl==1) or..** 
17274 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75  from the sub-jou
17275 72 6e 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a  rnal (if isMainJ
17276 72 6e 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79  rnl==0) and play
17277 62 61 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0d  back that page..
17278 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 62 65 67  .** The page beg
17279 69 6e 73 20 61 74 20 6f 66 66 73 65 74 20 2a 70  ins at offset *p
1727a 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  Offset into the 
1727b 66 69 6c 65 2e 20 54 68 65 20 2a 70 4f 66 66 73  file. The *pOffs
1727c 65 74 0d 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20  et..** value is 
1727d 69 6e 63 72 65 61 73 65 64 20 74 6f 20 74 68 65  increased to the
1727e 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65   start of the ne
1727f 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a  xt page in the j
17280 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ournal...**..** 
17281 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63  The main rollbac
17282 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 20 63  k journal uses c
17283 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 65 20 73  hecksums - the s
17284 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
17285 20 64 6f 65 73 20 0d 0a 2a 2a 20 6e 6f 74 2e 0d   does ..** not..
17286 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70  .**..** If the p
17287 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
17288 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 72 65  e page record re
17289 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
1728a 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0d 0a  -)journal file..
1728b 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ** is greater th
1728c 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  an the current v
1728d 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62  alue of Pager.db
1728e 53 69 7a 65 2c 20 74 68 65 6e 20 70 6c 61 79 62  Size, then playb
1728f 61 63 6b 20 69 73 0d 0a 2a 2a 20 73 6b 69 70 70  ack is..** skipp
17290 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  ed and SQLITE_OK
17291 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   is returned...*
17292 2a 0d 0a 2a 2a 20 49 66 20 70 44 6f 6e 65 20 69  *..** If pDone i
17293 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
17294 20 69 74 20 69 73 20 61 20 72 65 63 6f 72 64 20   it is a record 
17295 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 68 61  of pages that ha
17296 76 65 20 61 6c 72 65 61 64 79 0d 0a 2a 2a 20 62  ve already..** b
17297 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 2e  een played back.
17298 20 20 49 66 20 74 68 65 20 70 61 67 65 20 61 74    If the page at
17299 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20 61 6c   *pOffset has al
1729a 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79 65  ready been playe
1729b 64 20 62 61 63 6b 0d 0a 2a 2a 20 28 69 66 20 74  d back..** (if t
1729c 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1729d 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73 65   pDone bit is se
1729e 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65  t) then skip the
1729f 20 70 6c 61 79 62 61 63 6b 2e 0d 0a 2a 2a 20 4d   playback...** M
172a0 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f  ake sure the pDo
172a1 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e  ne bit correspon
172a2 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66  ding to the *pOf
172a3 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74  fset page is set
172a4 0d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65  ..** prior to re
172a5 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a  turning...**..**
172a6 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 63   If the page rec
172a7 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 66 75  ord is successfu
172a8 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68  lly read from th
172a9 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
172aa 66 69 6c 65 0d 0a 2a 2a 20 61 6e 64 20 70 6c 61  file..** and pla
172ab 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 53  yed back, then S
172ac 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
172ad 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
172ae 72 72 6f 72 20 6f 63 63 75 72 73 0d 0a 2a 2a 20  rror occurs..** 
172af 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68  while reading th
172b0 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68  e record from th
172b1 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
172b2 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72  file or while wr
172b3 69 74 69 6e 67 0d 0a 2a 2a 20 74 6f 20 74 68 65  iting..** to the
172b4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
172b5 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f  then the IO erro
172b6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
172b7 65 64 2e 20 49 66 20 64 61 74 61 0d 0a 2a 2a 20  ed. If data..** 
172b8 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
172b9 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73  read from the (s
172ba 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ub-)journal file
172bb 20 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20   but appears to 
172bc 62 65 0d 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64  be..** corrupted
172bd 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73  , SQLITE_DONE is
172be 20 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20   returned. Data 
172bf 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f  is considered co
172c0 72 72 75 70 74 65 64 20 69 6e 0d 0a 2a 2a 20 74  rrupted in..** t
172c1 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73  wo circumstances
172c2 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20 20 2a 20 49  :..** ..**   * I
172c3 66 20 74 68 65 20 72 65 63 6f 72 64 20 70 61 67  f the record pag
172c4 65 2d 6e 75 6d 62 65 72 20 69 73 20 69 6c 6c 65  e-number is ille
172c5 67 61 6c 20 28 30 20 6f 72 20 50 41 47 45 52 5f  gal (0 or PAGER_
172c6 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72 0d 0a 2a 2a  MJ_PGNO), or..**
172c7 20 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f     * If the reco
172c8 72 64 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c  rd is being roll
172c9 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65  ed back from the
172ca 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   main journal fi
172cb 6c 65 0d 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74  le..**     and t
172cc 68 65 20 63 68 65 63 6b 73 75 6d 20 66 69 65 6c  he checksum fiel
172cd 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  d does not match
172ce 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74   the record cont
172cf 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 65 69  ent...**..** Nei
172d0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
172d1 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65 20  o scenarios are 
172d2 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67 20  possible during 
172d3 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c  a savepoint roll
172d4 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  back...**..** If
172d5 20 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70   this is a savep
172d6 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74  oint rollback, t
172d7 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68  hen memory may h
172d8 61 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69  ave to be dynami
172d9 63 61 6c 6c 79 0d 0a 2a 2a 20 61 6c 6c 6f 63 61  cally..** alloca
172da 74 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ted by this func
172db 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73  tion. If this is
172dc 20 74 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e   the case and an
172dd 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
172de 73 2c 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  s,..** SQLITE_NO
172df 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  MEM is returned.
172e0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
172e1 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
172e2 6f 6e 65 5f 70 61 67 65 28 0d 0a 20 20 50 61 67  one_page(..  Pag
172e3 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
172e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
172e5 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c  e pager being pl
172e6 61 79 65 64 20 62 61 63 6b 20 2a 2f 0d 0a 20 20  ayed back */..  
172e7 69 36 34 20 2a 70 4f 66 66 73 65 74 2c 20 20 20  i64 *pOffset,   
172e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
172e9 20 4f 66 66 73 65 74 20 6f 66 20 72 65 63 6f 72   Offset of recor
172ea 64 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f  d to playback */
172eb 0d 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e  ..  Bitvec *pDon
172ec 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
172ed 20 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70    /* Bitvec of p
172ee 61 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61  ages already pla
172ef 79 65 64 20 62 61 63 6b 20 2a 2f 0d 0a 20 20 69  yed back */..  i
172f0 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20  nt isMainJrnl,  
172f1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
172f2 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61  1 -> main journa
172f3 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72  l. 0 -> sub-jour
172f4 6e 61 6c 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  nal. */..  int i
172f5 73 53 61 76 65 70 6e 74 20 20 20 20 20 20 20 20  sSavepnt        
172f6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
172f7 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 6e 74   for a savepoint
172f8 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 29 7b   rollback */..){
172f9 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 50  ..  int rc;..  P
172fa 67 48 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20  gHdr *pPg;      
172fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
172fc 41 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 65  An existing page
172fd 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f   in the cache */
172fe 0d 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20  ..  Pgno pgno;  
172ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17300 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75    /* The page nu
17301 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65 20 69  mber of a page i
17302 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20 20  n journal */..  
17303 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20  u32 cksum;      
17304 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17305 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20 66   Checksum used f
17306 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  or sanity checki
17307 6e 67 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 61  ng */..  char *a
17308 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
17309 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
1730a 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20  ary storage for 
1730b 74 68 65 20 70 61 67 65 20 2a 2f 0d 0a 20 20 73  the page */..  s
1730c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64  qlite3_file *jfd
1730d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1730e 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  The file descrip
1730f 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72  tor for the jour
17310 6e 61 6c 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69  nal file */..  i
17311 6e 74 20 69 73 53 79 6e 63 65 64 3b 20 20 20 20  nt isSynced;    
17312 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17313 54 72 75 65 20 69 66 20 6a 6f 75 72 6e 61 6c 20  True if journal 
17314 70 61 67 65 20 69 73 20 73 79 6e 63 65 64 20 2a  page is synced *
17315 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 28  /....  assert( (
17316 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d  isMainJrnl&~1)==
17317 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d  0 );      /* isM
17318 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20  ainJrnl is 0 or 
17319 31 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  1 */..  assert( 
1731a 28 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d  (isSavepnt&~1)==
1731b 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73  0 );       /* is
1731c 53 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20  Savepnt is 0 or 
1731d 31 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  1 */..  assert( 
1731e 69 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44  isMainJrnl || pD
1731f 6f 6e 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44  one );     /* pD
17320 6f 6e 65 20 61 6c 77 61 79 73 20 75 73 65 64 20  one always used 
17321 6f 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20  on sub-journals 
17322 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73  */..  assert( is
17323 53 61 76 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65  Savepnt || pDone
17324 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e  ==0 );   /* pDon
17325 65 20 6e 65 76 65 72 20 75 73 65 64 20 6f 6e 20  e never used on 
17326 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f  non-savepoint */
17327 0d 0a 0d 0a 20 20 61 44 61 74 61 20 3d 20 70 50  ....  aData = pP
17328 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
17329 0d 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74  ..  assert( aDat
1732a 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  a );         /* 
1732b 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73  Temp storage mus
1732c 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
1732d 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  een allocated */
1732e 0d 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65  ..  assert( page
1732f 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 3d  rUseWal(pPager)=
17330 3d 30 20 7c 7c 20 28 21 69 73 4d 61 69 6e 4a 72  =0 || (!isMainJr
17331 6e 6c 20 26 26 20 69 73 53 61 76 65 70 6e 74 29  nl && isSavepnt)
17332 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 45 69 74 68   );....  /* Eith
17333 65 72 20 74 68 65 20 73 74 61 74 65 20 69 73 20  er the state is 
17334 67 72 65 61 74 65 72 20 74 68 61 6e 20 50 41 47  greater than PAG
17335 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d  ER_WRITER_CACHEM
17336 4f 44 20 28 61 20 74 72 61 6e 73 61 63 74 69 6f  OD (a transactio
17337 6e 20 0d 0a 20 20 2a 2a 20 6f 72 20 73 61 76 65  n ..  ** or save
17338 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 64  point rollback d
17339 6f 6e 65 20 61 74 20 74 68 65 20 72 65 71 75 65  one at the reque
1733a 73 74 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  st of the caller
1733b 29 20 6f 72 20 74 68 69 73 20 69 73 0d 0a 20 20  ) or this is..  
1733c 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ** a hot-journal
1733d 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 66 20 69 74   rollback. If it
1733e 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   is a hot-journa
1733f 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20  l rollback, the 
17340 70 61 67 65 72 0d 0a 20 20 2a 2a 20 69 73 20 69  pager..  ** is i
17341 6e 20 73 74 61 74 65 20 4f 50 45 4e 20 61 6e 64  n state OPEN and
17342 20 68 6f 6c 64 73 20 61 6e 20 45 58 43 4c 55 53   holds an EXCLUS
17343 49 56 45 20 6c 6f 63 6b 2e 20 48 6f 74 2d 6a 6f  IVE lock. Hot-jo
17344 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 0d 0a  urnal rollback..
17345 20 20 2a 2a 20 6f 6e 6c 79 20 72 65 61 64 73 20    ** only reads 
17346 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f  from the main jo
17347 75 72 6e 61 6c 2c 20 6e 6f 74 20 74 68 65 20 73  urnal, not the s
17348 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0d 0a 20 20 2a  ub-journal...  *
17349 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  /..  assert( pPa
1734a 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47  ger->eState>=PAG
1734b 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d  ER_WRITER_CACHEM
1734c 4f 44 0d 0a 20 20 20 20 20 20 20 7c 7c 20 28 70  OD..       || (p
1734d 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
1734e 41 47 45 52 5f 4f 50 45 4e 20 26 26 20 70 50 61  AGER_OPEN && pPa
1734f 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c  ger->eLock==EXCL
17350 55 53 49 56 45 5f 4c 4f 43 4b 29 0d 0a 20 20 29  USIVE_LOCK)..  )
17351 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
17352 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47  ger->eState>=PAG
17353 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d  ER_WRITER_CACHEM
17354 4f 44 20 7c 7c 20 69 73 4d 61 69 6e 4a 72 6e 6c  OD || isMainJrnl
17355 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61 64   );....  /* Read
17356 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
17357 20 61 6e 64 20 70 61 67 65 20 64 61 74 61 20 66   and page data f
17358 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
17359 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0d 0a  or sub-journal..
1735a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 75 72    ** file. Retur
1735b 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
1735c 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 66  to the caller if
1735d 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
1735e 75 72 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6a 66  urs...  */..  jf
1735f 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f  d = isMainJrnl ?
17360 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70   pPager->jfd : p
17361 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0d 0a 20 20  Pager->sjfd;..  
17362 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
17363 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26  jfd, *pOffset, &
17364 70 67 6e 6f 29 3b 0d 0a 20 20 69 66 28 20 72 63  pgno);..  if( rc
17365 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
17366 74 75 72 6e 20 72 63 3b 0d 0a 20 20 72 63 20 3d  turn rc;..  rc =
17367 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a   sqlite3OsRead(j
17368 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61 2c 20  fd, (u8*)aData, 
17369 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1736a 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b  , (*pOffset)+4);
1736b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
1736c 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
1736d 63 3b 0d 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b  c;..  *pOffset +
1736e 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
1736f 7a 65 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a  ze + 4 + isMainJ
17370 72 6e 6c 2a 34 3b 0d 0a 0d 0a 20 20 2f 2a 20 53  rnl*4;....  /* S
17371 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
17372 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69  n the page.  Thi
17373 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74  s is more import
17374 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 67 69  ant that I origi
17375 6e 61 6c 6c 79 0d 0a 20 20 2a 2a 20 74 68 6f 75  nally..  ** thou
17376 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 72  ght.  If a power
17377 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20   failure occurs 
17378 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61  while the journa
17379 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74  l is being writt
1737a 65 6e 2c 0d 0a 20 20 2a 2a 20 69 74 20 63 6f 75  en,..  ** it cou
1737b 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64  ld cause invalid
1737c 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74   data to be writ
1737d 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
1737e 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74  rnal.  We need t
1737f 6f 0d 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74  o..  ** detect t
17380 68 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61  his invalid data
17381 20 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62   (with high prob
17382 61 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e  ability) and ign
17383 6f 72 65 20 69 74 2e 0d 0a 20 20 2a 2f 0d 0a 20  ore it...  */.. 
17384 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20   if( pgno==0 || 
17385 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50  pgno==PAGER_MJ_P
17386 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0d 0a  GNO(pPager) ){..
17387 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 53      assert( !isS
17388 61 76 65 70 6e 74 20 29 3b 0d 0a 20 20 20 20 72  avepnt );..    r
17389 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
1738a 45 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  E;..  }..  if( p
1738b 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72  gno>(Pgno)pPager
1738c 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69  ->dbSize || sqli
1738d 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44  te3BitvecTest(pD
1738e 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0d 0a 20  one, pgno) ){.. 
1738f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17390 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  _OK;..  }..  if(
17391 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0d 0a   isMainJrnl ){..
17392 20 20 20 20 72 63 20 3d 20 72 65 61 64 33 32 62      rc = read32b
17393 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73  its(jfd, (*pOffs
17394 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0d  et)-4, &cksum);.
17395 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
17396 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 69 66  turn rc;..    if
17397 28 20 21 69 73 53 61 76 65 70 6e 74 20 26 26 20  ( !isSavepnt && 
17398 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67  pager_cksum(pPag
17399 65 72 2c 20 28 75 38 2a 29 61 44 61 74 61 29 21  er, (u8*)aData)!
1739a 3d 63 6b 73 75 6d 20 29 7b 0d 0a 20 20 20 20 20  =cksum ){..     
1739b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
1739c 4f 4e 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ONE;..    }..  }
1739d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  ....  /* If this
1739e 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64   page has alread
1739f 79 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 79  y been played by
173a0 20 62 65 66 6f 72 65 20 64 75 72 69 6e 67 20 74   before during t
173a1 68 65 20 63 75 72 72 65 6e 74 0d 0a 20 20 2a 2a  he current..  **
173a2 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20   rollback, then 
173a3 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 74 6f 20  don't bother to 
173a4 70 6c 61 79 20 69 74 20 62 61 63 6b 20 61 67 61  play it back aga
173a5 69 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  in...  */..  if(
173a6 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 3d 20   pDone && (rc = 
173a7 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
173a8 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 21 3d  (pDone, pgno))!=
173a9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
173aa 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20    return rc;..  
173ab 7d 0d 0a 0d 0a 20 20 2f 2a 20 57 68 65 6e 20 70  }....  /* When p
173ac 6c 61 79 69 6e 67 20 62 61 63 6b 20 70 61 67 65  laying back page
173ad 20 31 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20   1, restore the 
173ae 6e 52 65 73 65 72 76 65 20 73 65 74 74 69 6e 67  nReserve setting
173af 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 67  ..  */..  if( pg
173b0 6e 6f 3d 3d 31 20 26 26 20 70 50 61 67 65 72 2d  no==1 && pPager-
173b1 3e 6e 52 65 73 65 72 76 65 21 3d 28 28 75 38 2a  >nReserve!=((u8*
173b2 29 61 44 61 74 61 29 5b 32 30 5d 20 29 7b 0d 0a  )aData)[20] ){..
173b3 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 73      pPager->nRes
173b4 65 72 76 65 20 3d 20 28 28 75 38 2a 29 61 44 61  erve = ((u8*)aDa
173b5 74 61 29 5b 32 30 5d 3b 0d 0a 20 20 20 20 70 61  ta)[20];..    pa
173b6 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50  gerReportSize(pP
173b7 61 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ager);..  }.... 
173b8 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
173b9 20 69 73 20 69 6e 20 43 41 43 48 45 4d 4f 44 20   is in CACHEMOD 
173ba 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 65 72  state, then ther
173bb 65 20 6d 75 73 74 20 62 65 20 61 20 63 6f 70 79  e must be a copy
173bc 20 6f 66 20 74 68 69 73 0d 0a 20 20 2a 2a 20 70   of this..  ** p
173bd 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 65 72  age in the pager
173be 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20   cache. In this 
173bf 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 74 65  case just update
173c0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
173c1 2c 0d 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20  ,..  ** not the 
173c2 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
173c3 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 20  he page is left 
173c4 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e 20  marked dirty in 
173c5 74 68 69 73 20 63 61 73 65 2e 0d 0a 20 20 2a 2a  this case...  **
173c6 0d 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74  ..  ** An except
173c7 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65  ion to the above
173c8 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61   rule: If the da
173c9 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d  tabase is in no-
173ca 73 79 6e 63 20 6d 6f 64 65 0d 0a 20 20 2a 2a 20  sync mode..  ** 
173cb 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f  and a page is mo
173cc 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e  ved during an in
173cd 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
173ce 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
173cf 61 79 0d 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20  ay..  ** not be 
173d0 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63  in the pager cac
173d1 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20  he. Later: if a 
173d2 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65  malloc() or IO e
173d3 72 72 6f 72 20 6f 63 63 75 72 73 0d 0a 20 20 2a  rror occurs..  *
173d4 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70  * during a Movep
173d5 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e  age() call, then
173d6 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f   the page may no
173d7 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68  t be in the cach
173d8 65 0d 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20  e..  ** either. 
173d9 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  So the condition
173da 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68   described in th
173db 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70  e above paragrap
173dc 68 20 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 61  h is not..  ** a
173dd 73 73 65 72 74 28 29 61 62 6c 65 2e 0d 0a 20 20  ssert()able...  
173de 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20 69 6e 20 57  **..  ** If in W
173df 52 49 54 45 52 5f 44 42 4d 4f 44 2c 20 57 52 49  RITER_DBMOD, WRI
173e0 54 45 52 5f 46 49 4e 49 53 48 45 44 20 6f 72 20  TER_FINISHED or 
173e1 4f 50 45 4e 20 73 74 61 74 65 2c 20 74 68 65 6e  OPEN state, then
173e2 20 77 65 20 75 70 64 61 74 65 20 74 68 65 0d 0a   we update the..
173e3 20 20 2a 2a 20 70 61 67 65 72 20 63 61 63 68 65    ** pager cache
173e4 20 69 66 20 69 74 20 65 78 69 73 74 73 20 61 6e   if it exists an
173e5 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e  d the main file.
173e6 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65   The page is the
173e7 6e 20 6d 61 72 6b 65 64 20 0d 0a 20 20 2a 2a 20  n marked ..  ** 
173e8 6e 6f 74 20 64 69 72 74 79 2e 20 53 69 6e 63 65  not dirty. Since
173e9 20 74 68 69 73 20 63 6f 64 65 20 69 73 20 6f 6e   this code is on
173ea 6c 79 20 65 78 65 63 75 74 65 64 20 69 6e 20 50  ly executed in P
173eb 41 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 20  AGER_OPEN state 
173ec 66 6f 72 0d 0a 20 20 2a 2a 20 61 20 68 6f 74 2d  for..  ** a hot-
173ed 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b  journal rollback
173ee 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  , it is guarante
173ef 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ed that the page
173f0 2d 63 61 63 68 65 20 69 73 20 65 6d 70 74 79 0d  -cache is empty.
173f1 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67  .  ** if the pag
173f2 65 72 20 69 73 20 69 6e 20 4f 50 45 4e 20 73 74  er is in OPEN st
173f3 61 74 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ate...  **..  **
173f4 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20   Ticket #1171:  
173f5 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  The statement jo
173f6 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74  urnal might cont
173f7 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  ain page content
173f8 20 74 68 61 74 20 69 73 0d 0a 20 20 2a 2a 20 64   that is..  ** d
173f9 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
173fa 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61  e page content a
173fb 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
173fc 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d  he transaction..
173fd 0a 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72  .  ** This occur
173fe 73 20 77 68 65 6e 20 61 20 70 61 67 65 20 69 73  s when a page is
173ff 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74   changed prior t
17400 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61  o the start of a
17401 20 73 74 61 74 65 6d 65 6e 74 0d 0a 20 20 2a 2a   statement..  **
17402 20 74 68 65 6e 20 63 68 61 6e 67 65 64 20 61 67   then changed ag
17403 61 69 6e 20 77 69 74 68 69 6e 20 74 68 65 20 73  ain within the s
17404 74 61 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20  tatement.  When 
17405 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 63  rolling back suc
17406 68 20 61 0d 0a 20 20 2a 2a 20 73 74 61 74 65 6d  h a..  ** statem
17407 65 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f 74 20  ent we must not 
17408 77 72 69 74 65 20 74 6f 20 74 68 65 20 6f 72 69  write to the ori
17409 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 75  ginal database u
1740a 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0d 0a 20  nless we know.. 
1740b 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 6e 20   ** for certain 
1740c 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 70 61  that original pa
1740d 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 20  ge contents are 
1740e 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20  synced into the 
1740f 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0d 0a 20  main rollback.. 
17410 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74   ** journal.  Ot
17411 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65 72  herwise, a power
17412 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 76   loss might leav
17413 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61 20  e modified data 
17414 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 64 61 74  in the..  ** dat
17415 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f  abase file witho
17416 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  ut an entry in t
17417 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
17418 6e 61 6c 20 74 68 61 74 20 63 61 6e 0d 0a 20 20  nal that can..  
17419 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64  ** restore the d
1741a 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f  atabase to its o
1741b 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54  riginal form.  T
1741c 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75  wo conditions mu
1741d 73 74 20 62 65 0d 0a 20 20 2a 2a 20 6d 65 74 20  st be..  ** met 
1741e 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74  before writing t
1741f 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
17420 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64 61  iles. (1) the da
17421 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 0d 0a  tabase must be..
17422 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32    ** locked.  (2
17423 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
17424 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
17425 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c   content is full
17426 79 20 73 79 6e 63 65 64 0d 0a 20 20 2a 2a 20 69  y synced..  ** i
17427 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  n the main journ
17428 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73  al either becaus
17429 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  e the page is no
1742a 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c  t in cache or el
1742b 73 65 0d 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  se..  ** the pag
1742c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e  e is marked as n
1742d 65 65 64 53 79 6e 63 3d 3d 30 2e 0d 0a 20 20 2a  eedSync==0...  *
1742e 2a 0d 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d  *..  ** 2008-04-
1742f 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70  14:  When attemp
17430 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61  ting to vacuum a
17431 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
17432 65 20 66 69 6c 65 2c 20 69 74 0d 0a 20 20 2a 2a  e file, it..  **
17433 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   is possible to 
17434 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74  fail a statement
17435 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74   on a database t
17436 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74  hat does not yet
17437 20 65 78 69 73 74 2e 0d 0a 20 20 2a 2a 20 44 6f   exist...  ** Do
17438 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
17439 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61 73  write if databas
1743a 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72  e file has never
1743b 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0d 0a 20   been opened... 
1743c 20 2a 2f 0d 0a 20 20 69 66 28 20 70 61 67 65 72   */..  if( pager
1743d 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29  UseWal(pPager) )
1743e 7b 0d 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0d  {..    pPg = 0;.
1743f 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
17440 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  Pg = pager_looku
17441 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b  p(pPager, pgno);
17442 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
17443 20 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29   pPg || !MEMDB )
17444 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
17445 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47  ger->eState!=PAG
17446 45 52 5f 4f 50 45 4e 20 7c 7c 20 70 50 67 3d 3d  ER_OPEN || pPg==
17447 30 20 29 3b 0d 0a 20 20 50 41 47 45 52 54 52 41  0 );..  PAGERTRA
17448 43 45 28 28 22 50 4c 41 59 42 41 43 4b 20 25 64  CE(("PLAYBACK %d
17449 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
1744a 38 78 29 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20  8x) %s\n",..    
1744b 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
1744c 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
1744d 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61  ger_datahash(pPa
1744e 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28  ger->pageSize, (
1744f 75 38 2a 29 61 44 61 74 61 29 2c 0d 0a 20 20 20  u8*)aData),..   
17450 20 20 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a          (isMainJ
17451 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61  rnl?"main-journa
17452 6c 22 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22  l":"sub-journal"
17453 29 0d 0a 20 20 29 29 3b 0d 0a 20 20 69 66 28 20  )..  ));..  if( 
17454 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0d 0a 20  isMainJrnl ){.. 
17455 20 20 20 69 73 53 79 6e 63 65 64 20 3d 20 70 50     isSynced = pP
17456 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 7c 7c 20  ager->noSync || 
17457 28 2a 70 4f 66 66 73 65 74 20 3c 3d 20 70 50 61  (*pOffset <= pPa
17458 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29  ger->journalHdr)
17459 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
1745a 20 69 73 53 79 6e 63 65 64 20 3d 20 28 70 50 67   isSynced = (pPg
1745b 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e  ==0 || 0==(pPg->
1745c 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 4e 45  flags & PGHDR_NE
1745d 45 44 5f 53 59 4e 43 29 29 3b 0d 0a 20 20 7d 0d  ED_SYNC));..  }.
1745e 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50  .  if( isOpen(pP
1745f 61 67 65 72 2d 3e 66 64 29 0d 0a 20 20 20 26 26  ager->fd)..   &&
17460 20 28 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65   (pPager->eState
17461 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44  >=PAGER_WRITER_D
17462 42 4d 4f 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e  BMOD || pPager->
17463 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50  eState==PAGER_OP
17464 45 4e 29 0d 0a 20 20 20 26 26 20 69 73 53 79 6e  EN)..   && isSyn
17465 63 65 64 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 69  ced..  ){..    i
17466 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d  64 ofst = (pgno-
17467 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e  1)*(i64)pPager->
17468 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20 20 20 74  pageSize;..    t
17469 65 73 74 63 61 73 65 28 20 21 69 73 53 61 76 65  estcase( !isSave
1746a 70 6e 74 20 26 26 20 70 50 67 21 3d 30 20 26 26  pnt && pPg!=0 &&
1746b 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48   (pPg->flags&PGH
1746c 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 21 3d 30  DR_NEED_SYNC)!=0
1746d 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
1746e 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50   !pagerUseWal(pP
1746f 61 67 65 72 29 20 29 3b 0d 0a 20 20 20 20 72 63  ager) );..    rc
17470 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
17471 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 28 75  e(pPager->fd, (u
17472 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67 65 72  8*)aData, pPager
17473 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 73 74  ->pageSize, ofst
17474 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f  );..    if( pgno
17475 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53  >pPager->dbFileS
17476 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20 70 50  ize ){..      pP
17477 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
17478 20 3d 20 70 67 6e 6f 3b 0d 0a 20 20 20 20 7d 0d   = pgno;..    }.
17479 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
1747a 3e 70 42 61 63 6b 75 70 20 29 7b 0d 0a 20 20 20  >pBackup ){..   
1747b 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72     CODEC1(pPager
1747c 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33  , aData, pgno, 3
1747d 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45  , rc=SQLITE_NOME
1747e 4d 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  M);..      sqlit
1747f 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 70  e3BackupUpdate(p
17480 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20  Pager->pBackup, 
17481 70 67 6e 6f 2c 20 28 75 38 2a 29 61 44 61 74 61  pgno, (u8*)aData
17482 29 3b 0d 0a 20 20 20 20 20 20 43 4f 44 45 43 32  );..      CODEC2
17483 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20  (pPager, aData, 
17484 70 67 6e 6f 2c 20 37 2c 20 72 63 3d 53 51 4c 49  pgno, 7, rc=SQLI
17485 54 45 5f 4e 4f 4d 45 4d 2c 20 61 44 61 74 61 29  TE_NOMEM, aData)
17486 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
17487 65 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e  e if( !isMainJrn
17488 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0d 0a  l && pPg==0 ){..
17489 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1748a 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20  s a rollback of 
1748b 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20  a savepoint and 
1748c 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69  data was not wri
1748d 74 74 65 6e 20 74 6f 0d 0a 20 20 20 20 2a 2a 20  tten to..    ** 
1748e 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
1748f 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
17490 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72   in-memory, ther
17491 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 6c  e is a potential
17492 0d 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d  ..    ** problem
17493 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20  . When the page 
17494 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20  is next fetched 
17495 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  by the b-tree la
17496 79 65 72 2c 20 69 74 20 0d 0a 20 20 20 20 2a 2a  yer, it ..    **
17497 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72   will be read fr
17498 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
17499 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20  file, which may 
1749a 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0d 0a  or may not be ..
1749b 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20      ** current. 
1749c 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
1749d 20 54 68 65 72 65 20 61 72 65 20 61 20 63 6f 75   There are a cou
1749e 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ple of different
1749f 20 77 61 79 73 20 74 68 69 73 20 63 61 6e 20 68   ways this can h
174a0 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71  appen. All are q
174a1 75 69 74 65 0d 0a 20 20 20 20 2a 2a 20 6f 62 73  uite..    ** obs
174a2 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69  cure. When runni
174a3 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75  ng in synchronou
174a4 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 6e  s mode, this can
174a5 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0d 0a 20   only happen .. 
174a6 20 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67     ** if the pag
174a7 65 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65  e is on the free
174a8 2d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61  -list at the sta
174a9 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
174aa 63 74 69 6f 6e 2c 20 74 68 65 6e 0d 0a 20 20 20  ction, then..   
174ab 20 2a 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74   ** populated, t
174ac 68 65 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20  hen moved using 
174ad 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65  sqlite3PagerMove
174ae 70 61 67 65 28 29 2e 0d 0a 20 20 20 20 2a 2a 0d  page()...    **.
174af 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75  .    ** The solu
174b0 74 69 6f 6e 20 69 73 20 74 6f 20 61 64 64 20 61  tion is to add a
174b1 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  n in-memory page
174b2 20 74 6f 20 74 68 65 20 63 61 63 68 65 20 63 6f   to the cache co
174b3 6e 74 61 69 6e 69 6e 67 0d 0a 20 20 20 20 2a 2a  ntaining..    **
174b4 20 74 68 65 20 64 61 74 61 20 6a 75 73 74 20 72   the data just r
174b5 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 75 62  ead from the sub
174b6 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b 20 74  -journal. Mark t
174b7 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 79  he page as dirty
174b8 20 0d 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 66   ..    ** and if
174b9 20 74 68 65 20 70 61 67 65 72 20 72 65 71 75 69   the pager requi
174ba 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79  res a journal-sy
174bb 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68  nc, then mark th
174bc 65 20 70 61 67 65 20 61 73 20 0d 0a 20 20 20 20  e page as ..    
174bd 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a  ** requiring a j
174be 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f  ournal-sync befo
174bf 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e  re it is written
174c0 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61  ...    */..    a
174c1 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74  ssert( isSavepnt
174c2 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
174c3 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 70   pPager->doNotSp
174c4 69 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 70  ill==0 );..    p
174c5 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 70 69 6c  Pager->doNotSpil
174c6 6c 2b 2b 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  l++;..    rc = s
174c7 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
174c8 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c  re(pPager, pgno,
174c9 20 26 70 50 67 2c 20 31 29 3b 0d 0a 20 20 20 20   &pPg, 1);..    
174ca 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
174cb 64 6f 4e 6f 74 53 70 69 6c 6c 3d 3d 31 20 29 3b  doNotSpill==1 );
174cc 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f  ..    pPager->do
174cd 4e 6f 74 53 70 69 6c 6c 2d 2d 3b 0d 0a 20 20 20  NotSpill--;..   
174ce 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
174cf 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d  OK ) return rc;.
174d0 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20  .    pPg->flags 
174d1 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52  &= ~PGHDR_NEED_R
174d2 45 41 44 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  EAD;..    sqlite
174d3 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
174d4 28 70 50 67 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69  (pPg);..  }..  i
174d5 66 28 20 70 50 67 20 29 7b 0d 0a 20 20 20 20 2f  f( pPg ){..    /
174d6 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64  * No page should
174d7 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69   ever be explici
174d8 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  tly rolled back 
174d9 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20  that is in use, 
174da 65 78 63 65 70 74 0d 0a 20 20 20 20 2a 2a 20 66  except..    ** f
174db 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20  or page 1 which 
174dc 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69  is held in use i
174dd 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
174de 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0d  the lock on the.
174df 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
174e0 20 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72   active. However
174e1 20 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79   such a page may
174e2 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
174e3 61 73 20 61 20 72 65 73 75 6c 74 0d 0a 20 20 20  as a result..   
174e4 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e   ** of an intern
174e5 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69  al error resulti
174e6 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74  ng in an automat
174e7 69 63 20 63 61 6c 6c 20 74 6f 0d 0a 20 20 20 20  ic call to..    
174e8 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  ** sqlite3PagerR
174e9 6f 6c 6c 62 61 63 6b 28 29 2e 0d 0a 20 20 20 20  ollback()...    
174ea 2a 2f 0d 0a 20 20 20 20 76 6f 69 64 20 2a 70 44  */..    void *pD
174eb 61 74 61 3b 0d 0a 20 20 20 20 70 44 61 74 61 20  ata;..    pData 
174ec 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0d 0a 20  = pPg->pData;.. 
174ed 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c     memcpy(pData,
174ee 20 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61   (u8*)aData, pPa
174ef 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0d  ger->pageSize);.
174f0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65  .    pPager->xRe
174f1 69 6e 69 74 65 72 28 70 50 67 29 3b 0d 0a 20 20  initer(pPg);..  
174f2 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c    if( isMainJrnl
174f3 20 26 26 20 28 21 69 73 53 61 76 65 70 6e 74 20   && (!isSavepnt 
174f4 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61  || *pOffset<=pPa
174f5 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29  ger->journalHdr)
174f6 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66   ){..      /* If
174f7 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
174f8 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65 20   this page were 
174f9 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 72  just restored fr
174fa 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0d 0a 20 20  om the main ..  
174fb 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
174fc 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f  ile, then its co
174fd 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73  ntent must be as
174fe 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20   they were when 
174ff 74 68 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74  the ..      ** t
17500 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66  ransaction was f
17501 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20  irst opened. In 
17502 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e  this case we can
17503 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0d 0a   mark the page..
17504 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61        ** as clea
17505 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77  n, since there w
17506 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74  ill be no need t
17507 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74  o write it out t
17508 6f 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  o the..      ** 
17509 64 61 74 61 62 61 73 65 2e 0d 0a 20 20 20 20 20  database...     
1750a 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 54 68   **..      ** Th
1750b 65 72 65 20 69 73 20 6f 6e 65 20 65 78 63 65 70  ere is one excep
1750c 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 75 6c  tion to this rul
1750d 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69  e. If the page i
1750e 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 0d 0a  s being rolled..
1750f 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 61 73        ** back as
17510 20 70 61 72 74 20 6f 66 20 61 20 73 61 76 65 70   part of a savep
17511 6f 69 6e 74 20 28 6f 72 20 73 74 61 74 65 6d 65  oint (or stateme
17512 6e 74 29 20 72 6f 6c 6c 62 61 63 6b 20 66 72 6f  nt) rollback fro
17513 6d 20 61 6e 20 0d 0a 20 20 20 20 20 20 2a 2a 20  m an ..      ** 
17514 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e  unsynced portion
17515 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   of the main jou
17516 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20  rnal file, then 
17517 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0d 0a  it is not safe..
17518 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 72 6b        ** to mark
17519 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65   the page as cle
1751a 61 6e 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  an. This is beca
1751b 75 73 65 20 6d 61 72 6b 69 6e 67 20 74 68 65 20  use marking the 
1751c 70 61 67 65 20 61 73 0d 0a 20 20 20 20 20 20 2a  page as..      *
1751d 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20 63 6c 65  * clean will cle
1751e 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  ar the PGHDR_NEE
1751f 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20 53 69 6e  D_SYNC flag. Sin
17520 63 65 20 74 68 65 20 70 61 67 65 20 69 73 0d 0a  ce the page is..
17521 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79        ** already
17522 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
17523 66 69 6c 65 20 28 72 65 63 6f 72 64 65 64 20 69  file (recorded i
17524 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e  n Pager.pInJourn
17525 61 6c 29 20 61 6e 64 0d 0a 20 20 20 20 20 20 2a  al) and..      *
17526 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44  * the PGHDR_NEED
17527 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 63 6c  _SYNC flag is cl
17528 65 61 72 65 64 2c 20 69 66 20 74 68 65 20 70 61  eared, if the pa
17529 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ge is written to
1752a 0d 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e  ..      ** again
1752b 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61   within this tra
1752c 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c  nsaction, it wil
1752d 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64  l be marked as d
1752e 69 72 74 79 20 62 75 74 0d 0a 20 20 20 20 20 20  irty but..      
1752f 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  ** the PGHDR_NEE
17530 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c 6c  D_SYNC flag will
17531 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 20   not be set. It 
17532 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 6e  could then poten
17533 74 69 61 6c 6c 79 0d 0a 20 20 20 20 20 20 2a 2a  tially..      **
17534 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20   be written out 
17535 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
17536 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74  e file before it
17537 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0d 0a  s journal file..
17538 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74        ** segment
17539 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61   is synced. If a
1753a 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75   crash occurs du
1753b 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e  ring or followin
1753c 67 20 74 68 69 73 2c 0d 0a 20 20 20 20 20 20 2a  g this,..      *
1753d 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  * database corru
1753e 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e  ption may ensue.
1753f 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
17540 20 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72    assert( !pager
17541 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29  UseWal(pPager) )
17542 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
17543 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
17544 70 50 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  pPg);..    }..  
17545 20 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65    pager_set_page
17546 68 61 73 68 28 70 50 67 29 3b 0d 0a 0d 0a 20 20  hash(pPg);....  
17547 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
17548 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65   page 1, then re
17549 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  store the value 
1754a 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  of Pager.dbFileV
1754b 65 72 73 2e 0d 0a 20 20 20 20 2a 2a 20 44 6f 20  ers...    ** Do 
1754c 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20  this before any 
1754d 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0d 0a 20 20  decoding. */..  
1754e 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b    if( pgno==1 ){
1754f 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ..      memcpy(&
17550 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
17551 72 73 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61  rs, &((u8*)pData
17552 29 5b 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61  )[24],sizeof(pPa
17553 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
17554 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  );..    }....   
17555 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 70   /* Decode the p
17556 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 66 72  age just read fr
17557 6f 6d 20 64 69 73 6b 20 2a 2f 0d 0a 20 20 20 20  om disk */..    
17558 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70  CODEC1(pPager, p
17559 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  Data, pPg->pgno,
1755a 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f   3, rc=SQLITE_NO
1755b 4d 45 4d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  MEM);..    sqlit
1755c 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28  e3PcacheRelease(
1755d 70 50 67 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  pPg);..  }..  re
1755e 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
1755f 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  *..** Parameter 
17560 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e  zMaster is the n
17561 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20  ame of a master 
17562 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20  journal file. A 
17563 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0d 0a  single journal..
17564 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 65 66  ** file that ref
17565 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73  erred to the mas
17566 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
17567 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72   has just been r
17568 6f 6c 6c 65 64 20 62 61 63 6b 2e 0d 0a 2a 2a 20  olled back...** 
17569 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
1756a 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f 73  cks if it is pos
1756b 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  sible to delete 
1756c 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1756d 61 6c 20 66 69 6c 65 2c 0d 0a 2a 2a 20 61 6e 64  al file,..** and
1756e 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69   does so if it i
1756f 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 72 67 75 6d  s...**..** Argum
17570 65 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20  ent zMaster may 
17571 70 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70  point to Pager.p
17572 54 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61  TmpSpace. So tha
17573 74 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20  t buffer is not 
17574 0d 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66  ..** available f
17575 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20 74 68  or use within th
17576 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a  is function...**
17577 0d 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 73 74  ..** When a mast
17578 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
17579 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 69  is created, it i
1757a 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  s populated with
1757b 20 74 68 65 20 6e 61 6d 65 73 20 0d 0a 2a 2a 20   the names ..** 
1757c 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68  of all of its ch
1757d 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e  ild journals, on
1757e 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72 2c  e after another,
1757f 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75 74   formatted as ut
17580 66 2d 38 20 0d 0a 2a 2a 20 65 6e 63 6f 64 65 64  f-8 ..** encoded
17581 20 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f   text. The end o
17582 66 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75  f each child jou
17583 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72  rnal file is mar
17584 6b 65 64 20 77 69 74 68 20 61 20 0d 0a 2a 2a 20  ked with a ..** 
17585 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62  nul-terminator b
17586 79 74 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e  yte (0x00). i.e.
17587 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
17588 65 6e 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72  ents of a master
17589 20 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 66 69 6c   journal..** fil
1758a 65 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74  e for a transact
1758b 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77  ion involving tw
1758c 6f 20 64 61 74 61 62 61 73 65 73 20 6d 69 67 68  o databases migh
1758d 74 20 62 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  t be:..**..**   
1758e 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 62  "/home/bill/a.db
1758f 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f 6d  -journal\x00/hom
17590 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 72  e/bill/b.db-jour
17591 6e 61 6c 5c 78 30 30 22 0d 0a 2a 2a 0d 0a 2a 2a  nal\x00"..**..**
17592 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   A master journa
17593 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20  l file may only 
17594 62 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20  be deleted once 
17595 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64  all of its child
17596 20 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68   ..** journals h
17597 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20  ave been rolled 
17598 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  back...**..** Th
17599 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64  is function read
1759a 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
1759b 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75  f the master-jou
1759c 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0d  rnal file into .
1759d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c  .** memory and l
1759e 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63  oops through eac
1759f 68 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a  h of the child j
175a0 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f  ournal names. Fo
175a1 72 0d 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64  r..** each child
175a2 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65   journal, it che
175a3 63 6b 73 20 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  cks if:..**..** 
175a4 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64    * if the child
175a5 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2c   journal exists,
175a6 20 61 6e 64 20 69 66 20 73 6f 0d 0a 2a 2a 20 20   and if so..**  
175a7 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 20   * if the child 
175a8 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73  journal contains
175a9 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
175aa 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 0d  master journal .
175ab 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d 61  .**     file zMa
175ac 73 74 65 72 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ster..**..** If 
175ad 61 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20  a child journal 
175ae 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61  can be found tha
175af 74 20 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f  t matches both o
175b0 66 20 74 68 65 20 63 72 69 74 65 72 69 61 0d 0a  f the criteria..
175b1 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 73 20 66  ** above, this f
175b2 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
175b3 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
175b4 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 77 69 73  ything. Otherwis
175b5 65 2c 20 69 66 0d 0a 2a 2a 20 6e 6f 20 73 75 63  e, if..** no suc
175b6 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20  h child journal 
175b7 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 66 69  can be found, fi
175b8 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20 64 65  le zMaster is de
175b9 6c 65 74 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 74  leted from..** t
175ba 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 75  he file-system u
175bb 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 65  sing sqlite3OsDe
175bc 6c 65 74 65 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  lete()...**..** 
175bd 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77  If an IO error w
175be 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74  ithin this funct
175bf 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f  ion, an error co
175c0 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
175c1 54 68 69 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f  This..** functio
175c2 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f  n allocates memo
175c3 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71  ry by calling sq
175c4 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49  lite3Malloc(). I
175c5 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d  f an allocation.
175c6 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54  .** fails, SQLIT
175c7 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
175c8 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
175c9 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c  if no IO or mall
175ca 6f 63 20 65 72 72 6f 72 73 20 0d 0a 2a 2a 20 6f  oc errors ..** o
175cb 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ccur, SQLITE_OK 
175cc 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
175cd 0d 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20  ..** TODO: This 
175ce 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74  function allocat
175cf 65 73 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63  es a single bloc
175d0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c  k of memory to l
175d1 6f 61 64 0d 0a 2a 2a 20 74 68 65 20 65 6e 74 69  oad..** the enti
175d2 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  re contents of t
175d3 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
175d4 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 75  l file. This cou
175d5 6c 64 20 62 65 0d 0a 2a 2a 20 61 20 63 6f 75 70  ld be..** a coup
175d6 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20  le of kilobytes 
175d7 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61  or so - potentia
175d8 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20  lly larger than 
175d9 74 68 65 20 70 61 67 65 20 0d 0a 2a 2a 20 73 69  the page ..** si
175da 7a 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ze...*/..static 
175db 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73  int pager_delmas
175dc 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  ter(Pager *pPage
175dd 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
175de 4d 61 73 74 65 72 29 7b 0d 0a 20 20 73 71 6c 69  Master){..  sqli
175df 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20  te3_vfs *pVfs = 
175e0 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0d 0a 20  pPager->pVfs;.. 
175e1 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
175e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
175e3 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  turn code */..  
175e4 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d  sqlite3_file *pM
175e5 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d 61 6c  aster;    /* Mal
175e6 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a 6f 75  loc'd master-jou
175e7 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69  rnal file descri
175e8 70 74 6f 72 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ptor */..  sqlit
175e9 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61  e3_file *pJourna
175ea 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64  l;   /* Malloc'd
175eb 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 66   child-journal f
175ec 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
175ed 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  /..  char *zMast
175ee 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f  erJournal = 0; /
175ef 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61  * Contents of ma
175f0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
175f1 65 20 2a 2f 0d 0a 20 20 69 36 34 20 6e 4d 61 73  e */..  i64 nMas
175f2 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  terJournal;     
175f3 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73    /* Size of mas
175f4 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
175f5 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4a 6f   */..  char *zJo
175f6 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20  urnal;          
175f7 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6f   /* Pointer to o
175f8 6e 65 20 6a 6f 75 72 6e 61 6c 20 77 69 74 68 69  ne journal withi
175f9 6e 20 4d 4a 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  n MJ file */..  
175fa 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 50 74 72  char *zMasterPtr
175fb 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61  ;         /* Spa
175fc 63 65 20 74 6f 20 68 6f 6c 64 20 4d 4a 20 66 69  ce to hold MJ fi
175fd 6c 65 6e 61 6d 65 20 66 72 6f 6d 20 61 20 6a 6f  lename from a jo
175fe 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0d 0a 20  urnal file */.. 
175ff 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 3b   int nMasterPtr;
17600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d             /* Am
17601 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 61 6c  ount of space al
17602 6c 6f 63 61 74 65 64 20 74 6f 20 7a 4d 61 73 74  located to zMast
17603 65 72 50 74 72 5b 5d 20 2a 2f 0d 0a 0d 0a 20 20  erPtr[] */....  
17604 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
17605 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 70  e for both the p
17606 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61 73  Journal and pMas
17607 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70  ter file descrip
17608 74 6f 72 73 2e 0d 0a 20 20 2a 2a 20 49 66 20 73  tors...  ** If s
17609 75 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20  uccessful, open 
1760a 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1760b 61 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64  al file for read
1760c 69 6e 67 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 4d  ing...  */..  pM
1760d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33  aster = (sqlite3
1760e 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d  _file *)sqlite3M
1760f 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e  allocZero(pVfs->
17610 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 3b 0d 0a  szOsFile * 2);..
17611 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 73 71    pJournal = (sq
17612 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 28 28  lite3_file *)(((
17613 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 2b 20  u8 *)pMaster) + 
17614 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b  pVfs->szOsFile);
17615 0d 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 72  ..  if( !pMaster
17616 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51   ){..    rc = SQ
17617 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d  LITE_NOMEM;..  }
17618 65 6c 73 65 7b 0d 0a 20 20 20 20 63 6f 6e 73 74  else{..    const
17619 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51   int flags = (SQ
1761a 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
1761b 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  LY|SQLITE_OPEN_M
1761c 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0d  ASTER_JOURNAL);.
1761d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1761e 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d  3OsOpen(pVfs, zM
1761f 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20  aster, pMaster, 
17620 66 6c 61 67 73 2c 20 30 29 3b 0d 0a 20 20 7d 0d  flags, 0);..  }.
17621 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
17622 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d  E_OK ) goto delm
17623 61 73 74 65 72 5f 6f 75 74 3b 0d 0a 0d 0a 20 20  aster_out;....  
17624 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69  /* Load the enti
17625 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  re master journa
17626 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63  l file into spac
17627 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0d  e obtained from.
17628 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  .  ** sqlite3_ma
17629 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e 74  lloc() and point
1762a 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72  ed to by zMaster
1762b 4a 6f 75 72 6e 61 6c 2e 20 20 20 41 6c 73 6f 20  Journal.   Also 
1762c 6f 62 74 61 69 6e 0d 0a 20 20 2a 2a 20 73 75 66  obtain..  ** suf
1762d 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 28 69  ficient space (i
1762e 6e 20 7a 4d 61 73 74 65 72 50 74 72 29 20 74 6f  n zMasterPtr) to
1762f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d 65 73 20   hold the names 
17630 6f 66 20 6d 61 73 74 65 72 0d 0a 20 20 2a 2a 20  of master..  ** 
17631 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 65 78  journal files ex
17632 74 72 61 63 74 65 64 20 66 72 6f 6d 20 72 65 67  tracted from reg
17633 75 6c 61 72 20 72 6f 6c 6c 62 61 63 6b 2d 6a 6f  ular rollback-jo
17634 75 72 6e 61 6c 73 2e 0d 0a 20 20 2a 2f 0d 0a 20  urnals...  */.. 
17635 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
17636 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65 72 2c  ileSize(pMaster,
17637 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   &nMasterJournal
17638 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
17639 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64  LITE_OK ) goto d
1763a 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0d 0a 20  elmaster_out;.. 
1763b 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56   nMasterPtr = pV
1763c 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
1763d 3b 0d 0a 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72  ;..  zMasterJour
1763e 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  nal = sqlite3Mal
1763f 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 72  loc((int)nMaster
17640 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65  Journal + nMaste
17641 72 50 74 72 20 2b 20 31 29 3b 0d 0a 20 20 69 66  rPtr + 1);..  if
17642 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  ( !zMasterJourna
17643 6c 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  l ){..    rc = S
17644 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
17645 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
17646 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 4d  _out;..  }..  zM
17647 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d 61 73  asterPtr = &zMas
17648 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74  terJournal[nMast
17649 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0d 0a 20  erJournal+1];.. 
1764a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
1764b 65 61 64 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61  ead(pMaster, zMa
1764c 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e  sterJournal, (in
1764d 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  t)nMasterJournal
1764e 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  , 0);..  if( rc!
1764f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
17650 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
17651 0d 0a 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  ..  zMasterJourn
17652 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  al[nMasterJourna
17653 6c 5d 20 3d 20 30 3b 0d 0a 0d 0a 20 20 7a 4a 6f  l] = 0;....  zJo
17654 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a  urnal = zMasterJ
17655 6f 75 72 6e 61 6c 3b 0d 0a 20 20 77 68 69 6c 65  ournal;..  while
17656 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73  ( (zJournal-zMas
17657 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73  terJournal)<nMas
17658 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0d 0a 20  terJournal ){.. 
17659 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0d 0a     int exists;..
1765a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1765b 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a  OsAccess(pVfs, z
1765c 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f  Journal, SQLITE_
1765d 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26  ACCESS_EXISTS, &
1765e 65 78 69 73 74 73 29 3b 0d 0a 20 20 20 20 69 66  exists);..    if
1765f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17660 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 64  ){..      goto d
17661 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0d 0a 20  elmaster_out;.. 
17662 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 65 78     }..    if( ex
17663 69 73 74 73 20 29 7b 0d 0a 20 20 20 20 20 20 2f  ists ){..      /
17664 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75  * One of the jou
17665 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f  rnals pointed to
17666 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a   by the master j
17667 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0d 0a  ournal exists...
17668 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74        ** Open it
17669 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74   and check if it
1766a 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d   points at the m
1766b 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49  aster journal. I
1766c 66 0d 0a 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20  f..      ** so, 
1766d 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64  return without d
1766e 65 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74  eleting the mast
1766f 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
17670 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
17671 20 20 69 6e 74 20 63 3b 0d 0a 20 20 20 20 20 20    int c;..      
17672 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c  int flags = (SQL
17673 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
17674 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  Y|SQLITE_OPEN_MA
17675 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0d 0a 20 20  IN_JOURNAL);..  
17676 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17677 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f  OsOpen(pVfs, zJo
17678 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c  urnal, pJournal,
17679 20 66 6c 61 67 73 2c 20 30 29 3b 0d 0a 20 20 20   flags, 0);..   
1767a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1767b 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
1767c 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
1767d 6f 75 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d  out;..      }...
1767e 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64  .      rc = read
1767f 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a  MasterJournal(pJ
17680 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50  ournal, zMasterP
17681 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b  tr, nMasterPtr);
17682 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ..      sqlite3O
17683 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29  sClose(pJournal)
17684 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  ;..      if( rc!
17685 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
17686 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d         goto delm
17687 61 73 74 65 72 5f 6f 75 74 3b 0d 0a 20 20 20 20  aster_out;..    
17688 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 63 20 3d    }....      c =
17689 20 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d   zMasterPtr[0]!=
1768a 30 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73  0 && strcmp(zMas
1768b 74 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29  terPtr, zMaster)
1768c 3d 3d 30 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ==0;..      if( 
1768d 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  c ){..        /*
1768e 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 68   We have a match
1768f 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  . Do not delete 
17690 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
17691 61 6c 20 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20 20  al file. */..   
17692 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73       goto delmas
17693 74 65 72 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20  ter_out;..      
17694 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 7a 4a  }..    }..    zJ
17695 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74  ournal += (sqlit
17696 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72  e3Strlen30(zJour
17697 6e 61 6c 29 2b 31 29 3b 0d 0a 20 20 7d 0d 0a 20  nal)+1);..  }.. 
17698 0d 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  ..  sqlite3OsClo
17699 73 65 28 70 4d 61 73 74 65 72 29 3b 0d 0a 20 20  se(pMaster);..  
1769a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  rc = sqlite3OsDe
1769b 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74  lete(pVfs, zMast
1769c 65 72 2c 20 30 29 3b 0d 0a 0d 0a 64 65 6c 6d 61  er, 0);....delma
1769d 73 74 65 72 5f 6f 75 74 3a 0d 0a 20 20 73 71 6c  ster_out:..  sql
1769e 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65  ite3_free(zMaste
1769f 72 4a 6f 75 72 6e 61 6c 29 3b 0d 0a 20 20 69 66  rJournal);..  if
176a0 28 20 70 4d 61 73 74 65 72 20 29 7b 0d 0a 20 20  ( pMaster ){..  
176a1 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
176a2 28 70 4d 61 73 74 65 72 29 3b 0d 0a 20 20 20 20  (pMaster);..    
176a3 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28  assert( !isOpen(
176a4 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0d 0a 20 20  pJournal) );..  
176a5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
176a6 4d 61 73 74 65 72 29 3b 0d 0a 20 20 7d 0d 0a 20  Master);..  }.. 
176a7 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
176a8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
176a9 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
176aa 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 61   to change the a
176ab 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 74 68  ctual size of th
176ac 65 20 64 61 74 61 62 61 73 65 20 0d 0a 2a 2a 20  e database ..** 
176ad 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c 65  file in the file
176ae 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f 6e  -system. This on
176af 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  ly happens when 
176b0 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61  committing a tra
176b1 6e 73 61 63 74 69 6f 6e 2c 0d 0a 2a 2a 20 6f 72  nsaction,..** or
176b2 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
176b3 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63  transaction (inc
176b4 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62  luding rolling b
176b5 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  ack a hot-journa
176b6 6c 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  l)...**..** If t
176b7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
176b8 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65   file is not ope
176b9 6e 2c 20 6f 72 20 74 68 65 20 70 61 67 65 72 20  n, or the pager 
176ba 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72  is not in either
176bb 0d 0a 2a 2a 20 44 42 4d 4f 44 20 6f 72 20 4f 50  ..** DBMOD or OP
176bc 45 4e 20 73 74 61 74 65 2c 20 74 68 69 73 20 66  EN state, this f
176bd 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
176be 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  op. Otherwise, t
176bf 68 65 20 73 69 7a 65 20 0d 0a 2a 2a 20 6f 66 20  he size ..** of 
176c0 74 68 65 20 66 69 6c 65 20 69 73 20 63 68 61 6e  the file is chan
176c1 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67  ged to nPage pag
176c2 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72  es (nPage*pPager
176c3 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
176c4 29 2e 20 0d 0a 2a 2a 20 49 66 20 74 68 65 20 66  ). ..** If the f
176c5 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 63  ile on disk is c
176c6 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
176c7 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73  than nPage pages
176c8 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56  , then use the V
176c9 46 53 0d 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65  FS..** xTruncate
176ca 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75  () method to tru
176cb 6e 63 61 74 65 20 69 74 2e 0d 0a 2a 2a 0d 0a 2a  ncate it...**..*
176cc 2a 20 4f 72 2c 20 69 74 20 6d 69 67 68 74 20 6d  * Or, it might m
176cd 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65  ight be the case
176ce 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 6f   that the file o
176cf 6e 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65  n disk is smalle
176d0 72 20 74 68 61 6e 20 0d 0a 2a 2a 20 6e 50 61 67  r than ..** nPag
176d1 65 20 70 61 67 65 73 2e 20 53 6f 6d 65 20 6f 70  e pages. Some op
176d2 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
176d3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63  mplementations c
176d4 61 6e 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20  an get confused 
176d5 69 66 20 0d 0a 2a 2a 20 79 6f 75 20 74 72 79 20  if ..** you try 
176d6 74 6f 20 74 72 75 6e 63 61 74 65 20 61 20 66 69  to truncate a fi
176d7 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20  le to some size 
176d8 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
176d9 68 61 6e 20 69 74 20 0d 0a 2a 2a 20 63 75 72 72  han it ..** curr
176da 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64 65 74  ently is, so det
176db 65 63 74 20 74 68 69 73 20 63 61 73 65 20 61 6e  ect this case an
176dc 64 20 77 72 69 74 65 20 61 20 73 69 6e 67 6c 65  d write a single
176dd 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20 0d 0a   zero byte to ..
176de 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  ** the end of th
176df 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74 65  e new file inste
176e0 61 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73  ad...**..** If s
176e1 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72  uccessful, retur
176e2 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  n SQLITE_OK. If 
176e3 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
176e4 72 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69  rs while modifyi
176e5 6e 67 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ng..** the datab
176e6 61 73 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e  ase file, return
176e7 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
176e8 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a  to the caller...
176e9 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  */..static int p
176ea 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50 61  ager_truncate(Pa
176eb 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
176ec 6f 20 6e 50 61 67 65 29 7b 0d 0a 20 20 69 6e 74  o nPage){..  int
176ed 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
176ee 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
176ef 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45  er->eState!=PAGE
176f0 52 5f 45 52 52 4f 52 20 29 3b 0d 0a 20 20 61 73  R_ERROR );..  as
176f1 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53  sert( pPager->eS
176f2 74 61 74 65 21 3d 50 41 47 45 52 5f 52 45 41 44  tate!=PAGER_READ
176f3 45 52 20 29 3b 0d 0a 20 20 0d 0a 20 20 69 66 28  ER );..  ..  if(
176f4 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
176f5 66 64 29 20 0d 0a 20 20 20 26 26 20 28 70 50 61  fd) ..   && (pPa
176f6 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47  ger->eState>=PAG
176f7 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20  ER_WRITER_DBMOD 
176f8 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  || pPager->eStat
176f9 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 29 20 0d  e==PAGER_OPEN) .
176fa 0a 20 20 29 7b 0d 0a 20 20 20 20 69 36 34 20 63  .  ){..    i64 c
176fb 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53  urrentSize, newS
176fc 69 7a 65 3b 0d 0a 20 20 20 20 69 6e 74 20 73 7a  ize;..    int sz
176fd 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 70  Page = pPager->p
176fe 61 67 65 53 69 7a 65 3b 0d 0a 20 20 20 20 61 73  ageSize;..    as
176ff 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c  sert( pPager->eL
17700 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ock==EXCLUSIVE_L
17701 4f 43 4b 20 29 3b 0d 0a 20 20 20 20 2f 2a 20 54  OCK );..    /* T
17702 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20  ODO: Is it safe 
17703 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46  to use Pager.dbF
17704 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f  ileSize here? */
17705 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
17706 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
17707 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e  ger->fd, &curren
17708 74 53 69 7a 65 29 3b 0d 0a 20 20 20 20 6e 65 77  tSize);..    new
17709 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 2a 28 69  Size = szPage*(i
1770a 36 34 29 6e 50 61 67 65 3b 0d 0a 20 20 20 20 69  64)nPage;..    i
1770b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1770c 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21   && currentSize!
1770d 3d 6e 65 77 53 69 7a 65 20 29 7b 0d 0a 20 20 20  =newSize ){..   
1770e 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69     if( currentSi
1770f 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0d 0a 20  ze>newSize ){.. 
17710 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
17711 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50  te3OsTruncate(pP
17712 61 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a  ager->fd, newSiz
17713 65 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  e);..      }else
17714 20 69 66 28 20 28 63 75 72 72 65 6e 74 53 69 7a   if( (currentSiz
17715 65 2b 73 7a 50 61 67 65 29 3c 3d 6e 65 77 53 69  e+szPage)<=newSi
17716 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63  ze ){..        c
17717 68 61 72 20 2a 70 54 6d 70 20 3d 20 70 50 61 67  har *pTmp = pPag
17718 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0d 0a  er->pTmpSpace;..
17719 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
1771a 54 6d 70 2c 20 30 2c 20 73 7a 50 61 67 65 29 3b  Tmp, 0, szPage);
1771b 0d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ..        testca
1771c 73 65 28 20 28 6e 65 77 53 69 7a 65 2d 73 7a 50  se( (newSize-szP
1771d 61 67 65 29 20 3d 3d 20 63 75 72 72 65 6e 74 53  age) == currentS
1771e 69 7a 65 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ize );..        
1771f 74 65 73 74 63 61 73 65 28 20 28 6e 65 77 53 69  testcase( (newSi
17720 7a 65 2d 73 7a 50 61 67 65 29 20 3e 20 20 63 75  ze-szPage) >  cu
17721 72 72 65 6e 74 53 69 7a 65 20 29 3b 0d 0a 20 20  rrentSize );..  
17722 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17723 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
17724 2d 3e 66 64 2c 20 70 54 6d 70 2c 20 73 7a 50 61  ->fd, pTmp, szPa
17725 67 65 2c 20 6e 65 77 53 69 7a 65 2d 73 7a 50 61  ge, newSize-szPa
17726 67 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ge);..      }.. 
17727 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
17728 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
17729 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c     pPager->dbFil
1772a 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0d 0a  eSize = nPage;..
1772b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
1772c 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
1772d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53  ;..}..../*..** S
1772e 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
1772f 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72  the Pager.sector
17730 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66 6f  Size variable fo
17731 72 20 74 68 65 20 67 69 76 65 6e 0d 0a 2a 2a 20  r the given..** 
17732 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74  pager based on t
17733 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
17734 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72  d by the xSector
17735 53 69 7a 65 20 6d 65 74 68 6f 64 0d 0a 2a 2a 20  Size method..** 
17736 6f 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61  of the open data
17737 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73  base file. The s
17738 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20  ector size will 
17739 62 65 20 75 73 65 64 20 75 73 65 64 20 0d 0a 2a  be used used ..*
1773a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  * to determine t
1773b 68 65 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67  he size and alig
1773c 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c  nment of journal
1773d 20 68 65 61 64 65 72 20 61 6e 64 20 0d 0a 2a 2a   header and ..**
1773e 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1773f 70 6f 69 6e 74 65 72 73 20 77 69 74 68 69 6e 20  pointers within 
17740 63 72 65 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20  created journal 
17741 66 69 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46  files...**..** F
17742 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
17743 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65  es the effective
17744 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20   sector size is 
17745 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73  always 512 bytes
17746 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77  ...**..** Otherw
17747 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d  ise, for non-tem
17748 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68  porary files, th
17749 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74  e effective sect
1774a 6f 72 20 73 69 7a 65 20 69 73 0d 0a 2a 2a 20 74  or size is..** t
1774b 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1774c 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72  d by the xSector
1774d 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f  Size() method ro
1774e 75 6e 64 65 64 20 75 70 20 74 6f 20 33 32 20 69  unded up to 32 i
1774f 66 0d 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73  f..** it is less
17750 20 74 68 61 6e 20 33 32 2c 20 6f 72 20 72 6f 75   than 32, or rou
17751 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58  nded down to MAX
17752 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20  _SECTOR_SIZE if 
17753 69 74 0d 0a 2a 2a 20 69 73 20 67 72 65 61 74 65  it..** is greate
17754 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f  r than MAX_SECTO
17755 52 5f 53 49 5a 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  R_SIZE...**..** 
17756 49 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 20  If the file has 
17757 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  the SQLITE_IOCAP
17758 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
17759 52 49 54 45 20 70 72 6f 70 65 72 74 79 2c 20 74  RITE property, t
1775a 68 65 6e 20 73 65 74 0d 0a 2a 2a 20 74 68 65 20  hen set..** the 
1775b 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f 72  effective sector
1775c 20 73 69 7a 65 20 74 6f 20 69 74 73 20 6d 69 6e   size to its min
1775d 69 6d 75 6d 20 76 61 6c 75 65 20 28 35 31 32 29  imum value (512)
1775e 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f  .  The purpose o
1775f 66 0d 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 73 65  f..** pPager->se
17760 63 74 6f 72 53 69 7a 65 20 69 73 20 74 6f 20 64  ctorSize is to d
17761 65 66 69 6e 65 20 74 68 65 20 22 62 6c 61 73 74  efine the "blast
17762 20 72 61 64 69 75 73 22 20 6f 66 20 62 79 74 65   radius" of byte
17763 73 20 74 68 61 74 0d 0a 2a 2a 20 6d 69 67 68 74  s that..** might
17764 20 63 68 61 6e 67 65 20 69 66 20 61 20 63 72 61   change if a cra
17765 73 68 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  sh occurs while 
17766 77 72 69 74 69 6e 67 20 74 6f 20 61 20 73 69 6e  writing to a sin
17767 67 6c 65 20 62 79 74 65 20 69 6e 0d 0a 2a 2a 20  gle byte in..** 
17768 74 68 61 74 20 72 61 6e 67 65 2e 20 20 42 75 74  that range.  But
17769 20 77 69 74 68 20 50 4f 57 45 52 53 41 46 45 5f   with POWERSAFE_
1776a 4f 56 45 52 57 52 49 54 45 2c 20 74 68 65 20 62  OVERWRITE, the b
1776b 6c 61 73 74 20 72 61 64 69 75 73 20 69 73 20 7a  last radius is z
1776c 65 72 6f 0d 0a 2a 2a 20 28 74 68 61 74 20 69 73  ero..** (that is
1776d 20 77 68 61 74 20 50 4f 57 45 52 53 41 46 45 5f   what POWERSAFE_
1776e 4f 56 45 52 57 52 49 54 45 20 6d 65 61 6e 73 29  OVERWRITE means)
1776f 2c 20 73 6f 20 77 65 20 6d 69 6e 69 6d 69 7a 65  , so we minimize
17770 20 74 68 65 20 73 65 63 74 6f 72 0d 0a 2a 2a 20   the sector..** 
17771 73 69 7a 65 2e 20 20 46 6f 72 20 62 61 63 6b 77  size.  For backw
17772 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
17773 74 79 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61  ty of the rollba
17774 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ck journal file 
17775 66 6f 72 6d 61 74 2c 0d 0a 2a 2a 20 77 65 20 63  format,..** we c
17776 61 6e 6e 6f 74 20 72 65 64 75 63 65 20 74 68 65  annot reduce the
17777 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
17778 72 20 73 69 7a 65 20 62 65 6c 6f 77 20 35 31 32  r size below 512
17779 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
1777a 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65  id setSectorSize
1777b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
1777c 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ..  assert( isOp
1777d 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
1777e 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  | pPager->tempFi
1777f 6c 65 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70  le );....  if( p
17780 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 0d  Pager->tempFile.
17781 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f  .   || (sqlite3O
17782 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
17783 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
17784 64 29 20 26 20 0d 0a 20 20 20 20 20 20 20 20 20  d) & ..         
17785 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41       SQLITE_IOCA
17786 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52  P_POWERSAFE_OVER
17787 57 52 49 54 45 29 21 3d 30 0d 0a 20 20 29 7b 0d  WRITE)!=0..  ){.
17788 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73  .    /* Sector s
17789 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 74  ize doesn't matt
1778a 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  er for temporary
1778b 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 68   files. Also, th
1778c 65 20 66 69 6c 65 0d 0a 20 20 20 20 2a 2a 20 6d  e file..    ** m
1778d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ay not have been
1778e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20   opened yet, in 
1778f 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 4f  which case the O
17790 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0d 0a 20  sSectorSize().. 
17791 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20     ** call will 
17792 73 65 67 66 61 75 6c 74 2e 20 2a 2f 0d 0a 20 20  segfault. */..  
17793 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72    pPager->sector
17794 53 69 7a 65 20 3d 20 35 31 32 3b 0d 0a 20 20 7d  Size = 512;..  }
17795 65 6c 73 65 7b 0d 0a 20 20 20 20 70 50 61 67 65  else{..    pPage
17796 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20  r->sectorSize = 
17797 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
17798 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ize(pPager->fd);
17799 0d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ..    if( pPager
1779a 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c 33 32 20  ->sectorSize<32 
1779b 29 7b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72  ){..      pPager
1779c 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35  ->sectorSize = 5
1779d 31 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  12;..    }..    
1779e 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74  if( pPager->sect
1779f 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f  orSize>MAX_SECTO
177a0 52 5f 53 49 5a 45 20 29 7b 0d 0a 20 20 20 20 20  R_SIZE ){..     
177a1 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43   assert( MAX_SEC
177a2 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b  TOR_SIZE>=512 );
177a3 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ..      pPager->
177a4 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58  sectorSize = MAX
177a5 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0d 0a 20  _SECTOR_SIZE;.. 
177a6 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
177a7 2f 2a 0d 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20  /*..** Playback 
177a8 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  the journal and 
177a9 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 65  thus restore the
177aa 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
177ab 6f 0d 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  o..** the state 
177ac 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65  it was in before
177ad 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69   we started maki
177ae 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0d 0a 2a  ng changes.  ..*
177af 2a 0d 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  *..** The journa
177b0 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73  l file format is
177b1 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0d 0a 2a   as follows: ..*
177b2 2a 0d 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79  *..**  (1)  8 by
177b3 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f  te prefix.  A co
177b4 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61  py of aJournalMa
177b5 67 69 63 5b 5d 2e 0d 0a 2a 2a 20 20 28 32 29 20  gic[]...**  (2) 
177b6 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69   4 byte big-endi
177b7 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
177b8 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
177b9 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63  f valid page rec
177ba 6f 72 64 73 0d 0a 2a 2a 20 20 20 20 20 20 20 69  ords..**       i
177bb 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  n the journal.  
177bc 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  If this value is
177bd 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65   0xffffffff, the
177be 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0d 0a 2a  n compute the..*
177bf 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f  *       number o
177c0 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66  f page records f
177c1 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
177c2 73 69 7a 65 2e 0d 0a 2a 2a 20 20 28 33 29 20 20  size...**  (3)  
177c3 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  4 byte big-endia
177c4 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
177c5 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76  is the initial v
177c6 61 6c 75 65 20 66 6f 72 20 74 68 65 20 0d 0a 2a  alue for the ..*
177c7 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79 20 63  *       sanity c
177c8 68 65 63 6b 73 75 6d 2e 0d 0a 2a 2a 20 20 28 34  hecksum...**  (4
177c9 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65  )  4 byte intege
177ca 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e  r which is the n
177cb 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
177cc 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0d 0a  o truncate the..
177cd 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73  **       databas
177ce 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f  e to during a ro
177cf 6c 6c 62 61 63 6b 2e 0d 0a 2a 2a 20 20 28 35 29  llback...**  (5)
177d0 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64    4 byte big-end
177d1 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63  ian integer whic
177d2 68 20 69 73 20 74 68 65 20 73 65 63 74 6f 72 20  h is the sector 
177d3 73 69 7a 65 2e 20 20 54 68 65 20 68 65 61 64 65  size.  The heade
177d4 72 0d 0a 2a 2a 20 20 20 20 20 20 20 69 73 20 74  r..**       is t
177d5 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 69  his many bytes i
177d6 6e 20 73 69 7a 65 2e 0d 0a 2a 2a 20 20 28 36 29  n size...**  (6)
177d7 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64    4 byte big-end
177d8 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63  ian integer whic
177d9 68 20 69 73 20 74 68 65 20 70 61 67 65 20 73 69  h is the page si
177da 7a 65 2e 0d 0a 2a 2a 20 20 28 37 29 20 20 7a 65  ze...**  (7)  ze
177db 72 6f 20 70 61 64 64 69 6e 67 20 6f 75 74 20 74  ro padding out t
177dc 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f  o the next secto
177dd 72 20 73 69 7a 65 2e 0d 0a 2a 2a 20 20 28 38 29  r size...**  (8)
177de 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70    Zero or more p
177df 61 67 65 73 20 69 6e 73 74 61 6e 63 65 73 2c 20  ages instances, 
177e0 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  each as follows:
177e1 0d 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34  ..**        +  4
177e2 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65   byte page numbe
177e3 72 2e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20  r...**        + 
177e4 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
177e5 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e  e bytes of data.
177e6 0d 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34  ..**        +  4
177e7 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0d 0a   byte checksum..
177e8 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73  **..** When we s
177e9 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72  peak of the jour
177ea 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d  nal header, we m
177eb 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 37 20  ean the first 7 
177ec 69 74 65 6d 73 20 61 62 6f 76 65 2e 0d 0a 2a 2a  items above...**
177ed 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   Each entry in t
177ee 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e  he journal is an
177ef 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
177f0 20 38 74 68 20 69 74 65 6d 2e 0d 0a 2a 2a 0d 0a   8th item...**..
177f1 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75  ** Call the valu
177f2 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e  e from the secon
177f3 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e  d bullet "nRec".
177f4 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75    nRec is the nu
177f5 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 76 61 6c 69  mber of..** vali
177f6 64 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69  d page entries i
177f7 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  n the journal.  
177f8 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79  In most cases, y
177f9 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74  ou can compute t
177fa 68 65 0d 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  he..** value of 
177fb 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69  nRec from the si
177fc 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
177fd 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20  l file.  But if 
177fe 61 20 70 6f 77 65 72 0d 0a 2a 2a 20 66 61 69 6c  a power..** fail
177ff 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
17800 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  le the journal w
17801 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e  as being written
17802 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
17803 65 0d 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20  e..** case that 
17804 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
17805 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64  journal file had
17806 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e   already been in
17807 63 72 65 61 73 65 64 20 62 75 74 0d 0a 2a 2a 20  creased but..** 
17808 74 68 65 20 65 78 74 72 61 20 65 6e 74 72 69 65  the extra entrie
17809 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61  s had not yet ma
1780a 64 65 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20  de it safely to 
1780b 64 69 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61  disk.  In such a
1780c 20 63 61 73 65 2c 0d 0a 2a 2a 20 74 68 65 20 76   case,..** the v
1780d 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d  alue of nRec com
1780e 70 75 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66  puted from the f
1780f 69 6c 65 20 73 69 7a 65 20 77 6f 75 6c 64 20 62  ile size would b
17810 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f  e too large.  Fo
17811 72 0d 0a 2a 2a 20 74 68 61 74 20 72 65 61 73 6f  r..** that reaso
17812 6e 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65  n, we always use
17813 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20   the nRec value 
17814 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0d 0a  in the header...
17815 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 52  **..** If the nR
17816 65 63 20 76 61 6c 75 65 20 69 73 20 30 78 66 66  ec value is 0xff
17817 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e 73 20  ffffff it means 
17818 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75 6c 64  that nRec should
17819 20 62 65 20 63 6f 6d 70 75 74 65 64 0d 0a 2a 2a   be computed..**
1781a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73   from the file s
1781b 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75 65  ize.  This value
1781c 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 68   is used when th
1781d 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 20 74  e user selects t
1781e 68 65 0d 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f  he..** no-sync o
1781f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f  ption for the jo
17820 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20  urnal.  A power 
17821 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65  failure could le
17822 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e  ad to corruption
17823 0d 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  ..** in this cas
17824 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e  e.  But for thin
17825 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72  gs like temporar
17826 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77  y table (which w
17827 69 6c 6c 20 62 65 0d 0a 2a 2a 20 64 65 6c 65 74  ill be..** delet
17828 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65  ed when the powe
17829 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77  r is restored) w
1782a 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0d  e don't care.  .
1782b 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 66  .**..** If the f
1782c 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68  ile opened as th
1782d 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
1782e 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72  s not a well-for
1782f 6d 65 64 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  med..** journal 
17830 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61  file then all pa
17831 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  ges up to the fi
17832 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61  rst corrupted pa
17833 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0d 0a 2a  ge are rolled..*
17834 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61  * back (or no pa
17835 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e  ges if the journ
17836 61 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72  al header is cor
17837 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75  rupted). The jou
17838 72 6e 61 6c 20 66 69 6c 65 0d 0a 2a 2a 20 69 73  rnal file..** is
17839 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e   then deleted an
1783a 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
1783b 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66  rned, just as if
1783c 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68   no corruption h
1783d 61 64 0d 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f  ad..** been enco
1783e 75 6e 74 65 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  untered...**..**
1783f 20 49 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61   If an I/O or ma
17840 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63  lloc() error occ
17841 75 72 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c  urs, the journal
17842 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c  -file is not del
17843 65 74 65 64 0d 0a 2a 2a 20 61 6e 64 20 61 6e 20  eted..** and an 
17844 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
17845 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  turned...**..** 
17846 54 68 65 20 69 73 48 6f 74 20 70 61 72 61 6d 65  The isHot parame
17847 74 65 72 20 69 6e 64 69 63 61 74 65 73 20 74 68  ter indicates th
17848 61 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  at we are trying
17849 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 6a   to rollback a j
1784a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 74 68 61 74 20  ournal..** that 
1784b 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74 20 6a  might be a hot j
1784c 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 20  ournal.  Or, it 
1784d 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 68  could be that th
1784e 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0d 0a 2a  e journal is ..*
1784f 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 63 61  * preserved beca
17850 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f  use of JOURNALMO
17851 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a 4f  DE_PERSIST or JO
17852 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
17853 54 45 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6a  TE...** If the j
17854 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73  ournal really is
17855 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20   hot, reset the 
17856 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 6f  pager cache prio
17857 72 20 72 6f 6c 6c 69 6e 67 0d 0a 2a 2a 20 62 61  r rolling..** ba
17858 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20  ck any content. 
17859 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
1785a 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73  is merely persis
1785b 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69  tent, no reset i
1785c 73 0d 0a 2a 2a 20 6e 65 65 64 65 64 2e 0d 0a 2a  s..** needed...*
1785d 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  /..static int pa
1785e 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67  ger_playback(Pag
1785f 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
17860 69 73 48 6f 74 29 7b 0d 0a 20 20 73 71 6c 69 74  isHot){..  sqlit
17861 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70  e3_vfs *pVfs = p
17862 50 61 67 65 72 2d 3e 70 56 66 73 3b 0d 0a 20 20  Pager->pVfs;..  
17863 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20  i64 szJ;        
17864 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
17865 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
17866 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  file in bytes */
17867 0d 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20  ..  u32 nRec;   
17868 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17869 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64  Number of Record
1786a 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s in the journal
1786b 20 2a 2f 0d 0a 20 20 75 33 32 20 75 3b 20 20 20   */..  u32 u;   
1786c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1786d 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70  /* Unsigned loop
1786e 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 50   counter */..  P
1786f 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20  gno mxPg = 0;   
17870 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
17871 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
17872 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f  file in pages */
17873 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
17874 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17875 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61  Result code of a
17876 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a   subroutine */..
17877 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20    int res = 1;  
17878 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
17879 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
1787a 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
1787b 29 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4d  ) */..  char *zM
1787c 61 73 74 65 72 20 3d 20 30 3b 20 20 20 20 20 20  aster = 0;      
1787d 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74   /* Name of mast
1787e 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1787f 69 66 20 61 6e 79 20 2a 2f 0d 0a 20 20 69 6e 74  if any */..  int
17880 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 3b   needPagerReset;
17881 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
17882 20 72 65 73 65 74 20 70 61 67 65 20 70 72 69 6f   reset page prio
17883 72 20 74 6f 20 66 69 72 73 74 20 70 61 67 65 20  r to first page 
17884 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 0d 0a 20  rollback */.... 
17885 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
17886 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73 20  ow many records 
17887 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  are in the journ
17888 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c 79  al.  Abort early
17889 20 69 66 0d 0a 20 20 2a 2a 20 74 68 65 20 6a 6f   if..  ** the jo
1788a 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0d  urnal is empty..
1788b 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
1788c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
1788d 6a 66 64 29 20 29 3b 0d 0a 20 20 72 63 20 3d 20  jfd) );..  rc = 
1788e 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
1788f 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26  e(pPager->jfd, &
17890 73 7a 4a 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  szJ);..  if( rc!
17891 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
17892 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
17893 62 61 63 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  back;..  }....  
17894 2f 2a 20 52 65 61 64 20 74 68 65 20 6d 61 73 74  /* Read the mast
17895 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
17896 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
17897 2c 20 69 66 20 69 74 20 69 73 20 70 72 65 73 65  , if it is prese
17898 6e 74 2e 0d 0a 20 20 2a 2a 20 49 66 20 61 20 6d  nt...  ** If a m
17899 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1789a 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 63 69  le name is speci
1789b 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 66 69  fied, but the fi
1789c 6c 65 20 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20  le is not..  ** 
1789d 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b 2c  present on disk,
1789e 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
1789f 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e 64  l is not hot and
178a0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
178a1 6f 20 62 65 0d 0a 20 20 2a 2a 20 70 6c 61 79 65  o be..  ** playe
178a2 64 20 62 61 63 6b 2e 0d 0a 20 20 2a 2a 0d 0a 20  d back...  **.. 
178a3 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69   ** TODO: Techni
178a4 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77  cally the follow
178a5 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20  ing is an error 
178a6 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75 6d  because it assum
178a7 65 73 20 74 68 61 74 0d 0a 20 20 2a 2a 20 62 75  es that..  ** bu
178a8 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53  ffer Pager.pTmpS
178a9 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e  pace is (mxPathn
178aa 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20  ame+1) bytes or 
178ab 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61  larger. i.e. tha
178ac 74 0d 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d  t..  ** (pPager-
178ad 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61  >pageSize >= pPa
178ae 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74  ger->pVfs->mxPat
178af 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20  hname+1). Using 
178b0 6f 73 5f 75 6e 69 78 2e 63 2c 0d 0a 20 20 2a 2a  os_unix.c,..  **
178b1 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20    mxPathname is 
178b2 35 31 32 2c 20 77 68 69 63 68 20 69 73 20 74 68  512, which is th
178b3 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69  e same as the mi
178b4 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20  nimum allowable 
178b5 76 61 6c 75 65 0d 0a 20 20 2a 2a 20 66 6f 72 20  value..  ** for 
178b6 70 61 67 65 53 69 7a 65 2e 0d 0a 20 20 2a 2f 0d  pageSize...  */.
178b7 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61  .  zMaster = pPa
178b8 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0d  ger->pTmpSpace;.
178b9 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74  .  rc = readMast
178ba 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
178bb 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20  ->jfd, zMaster, 
178bc 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
178bd 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0d 0a 20 20  Pathname+1);..  
178be 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
178bf 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20  K && zMaster[0] 
178c0 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ){..    rc = sql
178c1 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
178c2 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49  s, zMaster, SQLI
178c3 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
178c4 2c 20 26 72 65 73 29 3b 0d 0a 20 20 7d 0d 0a 20  , &res);..  }.. 
178c5 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0d 0a 20   zMaster = 0;.. 
178c6 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
178c7 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0d 0a 20  OK || !res ){.. 
178c8 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
178c9 62 61 63 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50  back;..  }..  pP
178ca 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
178cb 20 3d 20 30 3b 0d 0a 20 20 6e 65 65 64 50 61 67   = 0;..  needPag
178cc 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b  erReset = isHot;
178cd 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f  ....  /* This lo
178ce 6f 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69  op terminates ei
178cf 74 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64  ther when a read
178d0 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20  JournalHdr() or 
178d1 0d 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61  ..  ** pager_pla
178d2 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29  yback_one_page()
178d3 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51   call returns SQ
178d4 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20  LITE_DONE or an 
178d5 49 4f 20 65 72 72 6f 72 20 0d 0a 20 20 2a 2a 20  IO error ..  ** 
178d6 6f 63 63 75 72 73 2e 20 0d 0a 20 20 2a 2f 0d 0a  occurs. ..  */..
178d7 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0d 0a 20    while( 1 ){.. 
178d8 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e     /* Read the n
178d9 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ext journal head
178da 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  er from the jour
178db 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68  nal file.  If th
178dc 65 72 65 20 61 72 65 0d 0a 20 20 20 20 2a 2a 20  ere are..    ** 
178dd 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73  not enough bytes
178de 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75   left in the jou
178df 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20  rnal file for a 
178e0 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c  complete header,
178e1 20 6f 72 0d 0a 20 20 20 20 2a 2a 20 69 74 20 69   or..    ** it i
178e2 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65  s corrupted, the
178e3 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74  n a process must
178e4 20 68 61 76 65 20 66 61 69 6c 65 64 20 77 68 69   have failed whi
178e5 6c 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0d 0a  le writing it...
178e6 20 20 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69      ** This indi
178e7 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f  cates nothing mo
178e8 72 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  re needs to be r
178e9 6f 6c 6c 65 64 20 62 61 63 6b 2e 0d 0a 20 20 20  olled back...   
178ea 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 72 65   */..    rc = re
178eb 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  adJournalHdr(pPa
178ec 67 65 72 2c 20 69 73 48 6f 74 2c 20 73 7a 4a 2c  ger, isHot, szJ,
178ed 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0d   &nRec, &mxPg);.
178ee 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
178ef 49 54 45 5f 4f 4b 20 29 7b 20 0d 0a 20 20 20 20  ITE_OK ){ ..    
178f0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
178f1 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20 20 20 20 20  _DONE ){..      
178f2 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
178f3 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
178f4 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
178f5 61 63 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  ack;..    }.... 
178f6 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73     /* If nRec is
178f7 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65   0xffffffff, the
178f8 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77  n this journal w
178f9 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20  as created by a 
178fa 70 72 6f 63 65 73 73 0d 0a 20 20 20 20 2a 2a 20  process..    ** 
178fb 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79  working in no-sy
178fc 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65  nc mode. This me
178fd 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 73  ans that the res
178fe 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
178ff 0d 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f  ..    ** file co
17900 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c  nsists of pages,
17901 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
17902 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
17903 72 73 2e 20 43 6f 6d 70 75 74 65 0d 0a 20 20 20  rs. Compute..   
17904 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66   ** the value of
17905 20 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74   nRec based on t
17906 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0d  his assumption..
17907 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28  .    */..    if(
17908 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 66 66 66   nRec==0xfffffff
17909 66 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  f ){..      asse
1790a 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
1790b 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f  nalOff==JOURNAL_
1790c 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29  HDR_SZ(pPager) )
1790d 3b 0d 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20  ;..      nRec = 
1790e 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f 55  (int)((szJ - JOU
1790f 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
17910 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f  er))/JOURNAL_PG_
17911 53 5a 28 70 50 61 67 65 72 29 29 3b 0d 0a 20 20  SZ(pPager));..  
17912 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66    }....    /* If
17913 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64 20 74   nRec is 0 and t
17914 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20  his rollback is 
17915 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  of a transaction
17916 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73   created by this
17917 0d 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73  ..    ** process
17918 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73 20   and if this is 
17919 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65 72  the final header
1791a 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c   in the journal,
1791b 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0d 0a   then it means..
1791c 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73      ** that this
1791d 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75   part of the jou
1791e 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66  rnal was being f
1791f 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f  illed but has no
17920 74 20 79 65 74 20 62 65 65 6e 0d 0a 20 20 20 20  t yet been..    
17921 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73  ** synced to dis
17922 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20  k.  Compute the 
17923 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
17924 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d  based on the rem
17925 61 69 6e 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 73  aining..    ** s
17926 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e  ize of the file.
17927 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
17928 20 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20   The third term 
17929 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20  of the test was 
1792a 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63  added to fix tic
1792b 6b 65 74 20 23 32 35 36 35 2e 0d 0a 20 20 20 20  ket #2565...    
1792c 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20  ** When rolling 
1792d 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e  back a hot journ
1792e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61  al, nRec==0 alwa
1792f 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ys means that th
17930 65 20 6e 65 78 74 0d 0a 20 20 20 20 2a 2a 20 63  e next..    ** c
17931 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72  hunk of the jour
17932 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72  nal contains zer
17933 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f  o pages to be ro
17934 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0d  lled back.  But.
17935 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69  .    ** when doi
17936 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e  ng a ROLLBACK an
17937 64 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68  d the nRec==0 ch
17938 75 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20  unk is the last 
17939 63 68 75 6e 6b 20 69 6e 0d 0a 20 20 20 20 2a 2a  chunk in..    **
1793a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74   the journal, it
1793b 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
1793c 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f  journal might co
1793d 6e 74 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c  ntain additional
1793e 0d 0a 20 20 20 20 2a 2a 20 70 61 67 65 73 20 74  ..    ** pages t
1793f 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72  hat need to be r
17940 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74  olled back and t
17941 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hat the number o
17942 66 20 70 61 67 65 73 20 0d 0a 20 20 20 20 2a 2a  f pages ..    **
17943 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75   should be compu
17944 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
17945 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 69   journal file si
17946 7a 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ze...    */..   
17947 20 69 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20   if( nRec==0 && 
17948 21 69 73 48 6f 74 20 26 26 0d 0a 20 20 20 20 20  !isHot &&..     
17949 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
1794a 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alHdr+JOURNAL_HD
1794b 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50  R_SZ(pPager)==pP
1794c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
1794d 20 29 7b 0d 0a 20 20 20 20 20 20 6e 52 65 63 20   ){..      nRec 
1794e 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 70  = (int)((szJ - p
1794f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
17950 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  f) / JOURNAL_PG_
17951 53 5a 28 70 50 61 67 65 72 29 29 3b 0d 0a 20 20  SZ(pPager));..  
17952 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66    }....    /* If
17953 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
17954 73 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66  st header read f
17955 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c  rom the journal,
17956 20 74 72 75 6e 63 61 74 65 20 74 68 65 0d 0a 20   truncate the.. 
17957 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66     ** database f
17958 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20  ile back to its 
17959 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0d 0a  original size...
1795a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
1795b 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
1795c 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
1795d 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0d 0a 20  SZ(pPager) ){.. 
1795e 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
1795f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c  truncate(pPager,
17960 20 6d 78 50 67 29 3b 0d 0a 20 20 20 20 20 20 69   mxPg);..      i
17961 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
17962 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67 6f 74   ){..        got
17963 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0d  o end_playback;.
17964 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
17965 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
17966 20 6d 78 50 67 3b 0d 0a 20 20 20 20 7d 0d 0a 0d   mxPg;..    }...
17967 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69  .    /* Copy ori
17968 67 69 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20  ginal pages out 
17969 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  of the journal a
1796a 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  nd back into the
1796b 20 0d 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61   ..    ** databa
1796c 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20 70  se file and/or p
1796d 61 67 65 20 63 61 63 68 65 2e 0d 0a 20 20 20 20  age cache...    
1796e 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b  */..    for(u=0;
1796f 20 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0d 0a   u<nRec; u++){..
17970 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 50 61        if( needPa
17971 67 65 72 52 65 73 65 74 20 29 7b 0d 0a 20 20 20  gerReset ){..   
17972 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74       pager_reset
17973 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 20  (pPager);..     
17974 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 73 65     needPagerRese
17975 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d  t = 0;..      }.
17976 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
17977 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
17978 61 67 65 28 70 50 61 67 65 72 2c 26 70 50 61 67  age(pPager,&pPag
17979 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30  er->journalOff,0
1797a 2c 31 2c 30 29 3b 0d 0a 20 20 20 20 20 20 69 66  ,1,0);..      if
1797b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1797c 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
1797d 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
1797e 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 50  ){..          pP
1797f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
17980 20 3d 20 73 7a 4a 3b 0d 0a 20 20 20 20 20 20 20   = szJ;..       
17981 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
17982 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d     }else if( rc=
17983 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
17984 4f 52 54 5f 52 45 41 44 20 29 7b 0d 0a 20 20 20  ORT_READ ){..   
17985 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
17986 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 62 65 65   journal has bee
17987 6e 20 74 72 75 6e 63 61 74 65 64 2c 20 73 69 6d  n truncated, sim
17988 70 6c 79 20 73 74 6f 70 20 72 65 61 64 69 6e 67  ply stop reading
17989 20 61 6e 64 0d 0a 20 20 20 20 20 20 20 20 20 20   and..          
1798a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68  ** processing th
1798b 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 68 69 73 20  e journal. This 
1798c 6d 69 67 68 74 20 68 61 70 70 65 6e 20 69 66 20  might happen if 
1798d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0d  the journal was.
1798e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f  .          ** no
1798f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 77 72 69  t completely wri
17990 74 74 65 6e 20 61 6e 64 20 73 79 6e 63 65 64 20  tten and synced 
17991 70 72 69 6f 72 20 74 6f 20 61 20 63 72 61 73 68  prior to a crash
17992 2e 20 20 49 6e 20 74 68 61 74 0d 0a 20 20 20 20  .  In that..    
17993 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2c 20 74        ** case, t
17994 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75  he database shou
17995 6c 64 20 68 61 76 65 20 6e 65 76 65 72 20 62 65  ld have never be
17996 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 20 74 68  en written in th
17997 65 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e..          ** 
17998 66 69 72 73 74 20 70 6c 61 63 65 20 73 6f 20 69  first place so i
17999 74 20 69 73 20 4f 4b 20 74 6f 20 73 69 6d 70 6c  t is OK to simpl
1799a 79 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 72 6f  y abandon the ro
1799b 6c 6c 62 61 63 6b 2e 20 2a 2f 0d 0a 20 20 20 20  llback. */..    
1799c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1799d 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 20  E_OK;..         
1799e 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
1799f 63 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  ck;..        }el
179a0 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f  se{..          /
179a1 2a 20 49 66 20 77 65 20 61 72 65 20 75 6e 61 62  * If we are unab
179a2 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20  le to rollback, 
179a3 71 75 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20  quit and return 
179a4 74 68 65 20 65 72 72 6f 72 0d 0a 20 20 20 20 20  the error..     
179a5 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20 54       ** code.  T
179a6 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74  his will cause t
179a7 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65  he pager to ente
179a8 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  r the error stat
179a9 65 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e..          ** 
179aa 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74 68  so that no furth
179ab 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 20  er harm will be 
179ac 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20 74  done.  Perhaps t
179ad 68 65 20 6e 65 78 74 0d 0a 20 20 20 20 20 20 20  he next..       
179ae 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f     ** process to
179af 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c   come along will
179b0 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c   be able to roll
179b1 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  back the databas
179b2 65 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  e...          */
179b3 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ..          goto
179b4 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0d 0a   end_playback;..
179b5 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
179b6 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
179b7 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f    /*NOTREACHED*/
179b8 0d 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b  ..  assert( 0 );
179b9 0d 0a 0d 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b  ....end_playback
179ba 3a 0d 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e  :..  /* Followin
179bb 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68  g a rollback, th
179bc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
179bd 73 68 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 69  should be back i
179be 6e 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0d 0a  n its original..
179bf 20 20 2a 2a 20 73 74 61 74 65 20 70 72 69 6f 72    ** state prior
179c0 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
179c1 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
179c2 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0d  , so invoke the.
179c3 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e  .  ** SQLITE_FCN
179c4 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20  TL_DB_UNCHANGED 
179c5 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74  file-control met
179c6 68 6f 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74  hod to disable t
179c7 68 65 0d 0a 20 20 2a 2a 20 61 73 73 65 72 74 69  he..  ** asserti
179c8 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72 61 6e  on that the tran
179c9 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20  saction counter 
179ca 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0d 0a 20  was modified... 
179cb 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
179cc 54 45 5f 44 45 42 55 47 0d 0a 20 20 69 66 28 20  TE_DEBUG..  if( 
179cd 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74  pPager->fd->pMet
179ce 68 6f 64 73 20 29 7b 0d 0a 20 20 20 20 73 71 6c  hods ){..    sql
179cf 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
179d0 6c 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64  lHint(pPager->fd
179d1 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42  ,SQLITE_FCNTL_DB
179d2 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3b 0d 0a  _UNCHANGED,0);..
179d3 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20    }..#endif.... 
179d4 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61 79   /* If this play
179d5 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69 6e  back is happenin
179d6 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  g automatically 
179d7 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  as a result of a
179d8 6e 20 49 4f 20 6f 72 20 0d 0a 20 20 2a 2a 20 6d  n IO or ..  ** m
179d9 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74  alloc error that
179da 20 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20   occurred after 
179db 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
179dc 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62  er was updated b
179dd 75 74 20 0d 0a 20 20 2a 2a 20 62 65 66 6f 72 65  ut ..  ** before
179de 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
179df 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20   was committed, 
179e0 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d  then the change-
179e1 63 6f 75 6e 74 65 72 20 0d 0a 20 20 2a 2a 20 6d  counter ..  ** m
179e2 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 20  odification may 
179e3 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 72  just have been r
179e4 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 69 73  everted. If this
179e5 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 63 6c   happens in excl
179e6 75 73 69 76 65 20 0d 0a 20 20 2a 2a 20 6d 6f 64  usive ..  ** mod
179e7 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 65  e, then subseque
179e8 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nt transactions 
179e9 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65  performed by the
179ea 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
179eb 20 6e 6f 74 0d 0a 20 20 2a 2a 20 75 70 64 61 74   not..  ** updat
179ec 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  e the change-cou
179ed 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69  nter at all. Thi
179ee 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61  s may lead to ca
179ef 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63  che inconsistenc
179f0 79 0d 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73  y..  ** problems
179f1 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65   for other proce
179f2 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69  sses at some poi
179f3 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  nt in the future
179f4 2e 20 53 6f 2c 20 6a 75 73 74 0d 0a 20 20 2a 2a  . So, just..  **
179f5 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61   in case this ha
179f6 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61  s happened, clea
179f7 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e  r the changeCoun
179f8 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0d  tDone flag now..
179f9 0a 20 20 2a 2f 0d 0a 20 20 70 50 61 67 65 72 2d  .  */..  pPager-
179fa 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
179fb 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46   = pPager->tempF
179fc 69 6c 65 3b 0d 0a 0d 0a 20 20 69 66 28 20 72 63  ile;....  if( rc
179fd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
179fe 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50      zMaster = pP
179ff 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
17a00 0d 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d  ..    rc = readM
17a01 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61  asterJournal(pPa
17a02 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65  ger->jfd, zMaste
17a03 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d  r, pPager->pVfs-
17a04 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0d  >mxPathname+1);.
17a05 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72  .    testcase( r
17a06 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d  c!=SQLITE_OK );.
17a07 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
17a08 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20 20 20 26 26  SQLITE_OK..   &&
17a09 20 28 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65   (pPager->eState
17a0a 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44  >=PAGER_WRITER_D
17a0b 42 4d 4f 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e  BMOD || pPager->
17a0c 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50  eState==PAGER_OP
17a0d 45 4e 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 72  EN)..  ){..    r
17a0e 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
17a0f 53 79 6e 63 28 70 50 61 67 65 72 29 3b 0d 0a 20  Sync(pPager);.. 
17a10 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
17a11 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
17a12 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74  rc = pager_end_t
17a13 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
17a14 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d 27  r, zMaster[0]!='
17a15 5c 30 27 29 3b 0d 0a 20 20 20 20 74 65 73 74 63  \0');..    testc
17a16 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
17a17 4f 4b 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  OK );..  }..  if
17a18 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17a19 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26  && zMaster[0] &&
17a1a 20 72 65 73 20 29 7b 0d 0a 20 20 20 20 2f 2a 20   res ){..    /* 
17a1b 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 6d  If there was a m
17a1c 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e  aster journal an
17a1d 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  d this routine w
17a1e 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65  ill return succe
17a1f 73 73 2c 0d 0a 20 20 20 20 2a 2a 20 73 65 65 20  ss,..    ** see 
17a20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  if it is possibl
17a21 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  e to delete the 
17a22 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0d  master journal..
17a23 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 72 63 20  .    */..    rc 
17a24 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65  = pager_delmaste
17a25 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  r(pPager, zMaste
17a26 72 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73  r);..    testcas
17a27 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc!=SQLITE_OK
17a28 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   );..  }....  /*
17a29 20 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f   The Pager.secto
17a2a 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d  rSize variable m
17a2b 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64  ay have been upd
17a2c 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69  ated while rolli
17a2d 6e 67 0d 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20  ng..  ** back a 
17a2e 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20  journal created 
17a2f 62 79 20 61 20 70 72 6f 63 65 73 73 20 77 69 74  by a process wit
17a30 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 65  h a different se
17a31 63 74 6f 72 20 73 69 7a 65 0d 0a 20 20 2a 2a 20  ctor size..  ** 
17a32 76 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20  value. Reset it 
17a33 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 76  to the correct v
17a34 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70 72  alue for this pr
17a35 6f 63 65 73 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ocess...  */..  
17a36 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50  setSectorSize(pP
17a37 61 67 65 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e  ager);..  return
17a38 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d   rc;..}....../*.
17a39 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f 6e  .** Read the con
17a3a 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50  tent for page pP
17a3b 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74  g out of the dat
17a3c 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69  abase file and i
17a3d 6e 74 6f 20 0d 0a 2a 2a 20 70 50 67 2d 3e 70 44  nto ..** pPg->pD
17a3e 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f  ata. A shared lo
17a3f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75  ck or greater mu
17a40 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68  st be held on th
17a41 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 66  e database..** f
17a42 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20  ile before this 
17a43 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
17a44 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70  ed...**..** If p
17a45 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74  age 1 is read, t
17a46 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  hen the value of
17a47 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
17a48 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0d 0a 2a  s[] is set to..*
17a49 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64  * the value read
17a4a 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
17a4b 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  se file...**..**
17a4c 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
17a4d 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65  occurs, then the
17a4e 20 49 4f 20 65 72 72 6f 72 20 69 73 20 72 65 74   IO error is ret
17a4f 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
17a50 6c 65 72 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77 69  ler...** Otherwi
17a51 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  se, SQLITE_OK is
17a52 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a   returned...*/..
17a53 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 44  static int readD
17a54 62 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67  bPage(PgHdr *pPg
17a55 29 7b 0d 0a 20 20 50 61 67 65 72 20 2a 70 50 61  ){..  Pager *pPa
17a56 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
17a57 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65  r; /* Pager obje
17a58 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ct associated wi
17a59 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f 0d 0a  th page pPg */..
17a5a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50    Pgno pgno = pP
17a5b 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 2f  g->pgno;       /
17a5c 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f  * Page number to
17a5d 20 72 65 61 64 20 2a 2f 0d 0a 20 20 69 6e 74 20   read */..  int 
17a5e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
17a5f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
17a60 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e  rn code */..  in
17a61 74 20 69 73 49 6e 57 61 6c 20 3d 20 30 3b 20 20  t isInWal = 0;  
17a62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
17a63 75 65 20 69 66 20 70 61 67 65 20 69 73 20 69 6e  ue if page is in
17a64 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0d 0a 20 20   log file */..  
17a65 69 6e 74 20 70 67 73 7a 20 3d 20 70 50 61 67 65  int pgsz = pPage
17a66 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 2f 2a 20  r->pageSize; /* 
17a67 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
17a68 74 6f 20 72 65 61 64 20 2a 2f 0d 0a 0d 0a 20 20  to read */....  
17a69 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
17a6a 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  eState>=PAGER_RE
17a6b 41 44 45 52 20 26 26 20 21 4d 45 4d 44 42 20 29  ADER && !MEMDB )
17a6c 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  ;..  assert( isO
17a6d 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
17a6e 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 4e 45 56 45  );....  if( NEVE
17a6f 52 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  R(!isOpen(pPager
17a70 2d 3e 66 64 29 29 20 29 7b 0d 0a 20 20 20 20 61  ->fd)) ){..    a
17a71 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74  ssert( pPager->t
17a72 65 6d 70 46 69 6c 65 20 29 3b 0d 0a 20 20 20 20  empFile );..    
17a73 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74  memset(pPg->pDat
17a74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61  a, 0, pPager->pa
17a75 67 65 53 69 7a 65 29 3b 0d 0a 20 20 20 20 72 65  geSize);..    re
17a76 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
17a77 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 61  .  }....  if( pa
17a78 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72  gerUseWal(pPager
17a79 29 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 72 79  ) ){..    /* Try
17a7a 20 74 6f 20 70 75 6c 6c 20 74 68 65 20 70 61 67   to pull the pag
17a7b 65 20 66 72 6f 6d 20 74 68 65 20 77 72 69 74 65  e from the write
17a7c 2d 61 68 65 61 64 20 6c 6f 67 2e 20 2a 2f 0d 0a  -ahead log. */..
17a7d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17a7e 57 61 6c 52 65 61 64 28 70 50 61 67 65 72 2d 3e  WalRead(pPager->
17a7f 70 57 61 6c 2c 20 70 67 6e 6f 2c 20 26 69 73 49  pWal, pgno, &isI
17a80 6e 57 61 6c 2c 20 70 67 73 7a 2c 20 70 50 67 2d  nWal, pgsz, pPg-
17a81 3e 70 44 61 74 61 29 3b 0d 0a 20 20 7d 0d 0a 20  >pData);..  }.. 
17a82 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17a83 4f 4b 20 26 26 20 21 69 73 49 6e 57 61 6c 20 29  OK && !isInWal )
17a84 7b 0d 0a 20 20 20 20 69 36 34 20 69 4f 66 66 73  {..    i64 iOffs
17a85 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69  et = (pgno-1)*(i
17a86 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53  64)pPager->pageS
17a87 69 7a 65 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  ize;..    rc = s
17a88 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
17a89 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44  ger->fd, pPg->pD
17a8a 61 74 61 2c 20 70 67 73 7a 2c 20 69 4f 66 66 73  ata, pgsz, iOffs
17a8b 65 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  et);..    if( rc
17a8c 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  ==SQLITE_IOERR_S
17a8d 48 4f 52 54 5f 52 45 41 44 20 29 7b 0d 0a 20 20  HORT_READ ){..  
17a8e 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
17a8f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  OK;..    }..  }.
17a90 0a 0d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31  ...  if( pgno==1
17a91 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 72 63 20   ){..    if( rc 
17a92 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  ){..      /* If 
17a93 74 68 65 20 72 65 61 64 20 69 73 20 75 6e 73 75  the read is unsu
17a94 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68  ccessful, set th
17a95 65 20 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 74  e dbFileVers[] t
17a96 6f 20 73 6f 6d 65 74 68 69 6e 67 0d 0a 20 20 20  o something..   
17a97 20 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20     ** that will 
17a98 6e 65 76 65 72 20 62 65 20 61 20 76 61 6c 69 64  never be a valid
17a99 20 66 69 6c 65 20 76 65 72 73 69 6f 6e 2e 20 20   file version.  
17a9a 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20  dbFileVers[] is 
17a9b 61 20 63 6f 70 79 0d 0a 20 20 20 20 20 20 2a 2a  a copy..      **
17a9c 20 6f 66 20 62 79 74 65 73 20 32 34 2e 2e 33 39   of bytes 24..39
17a9d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
17a9e 2e 20 20 42 79 74 65 73 20 32 38 2e 2e 33 31 20  .  Bytes 28..31 
17a9f 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
17aa0 0d 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20  ..      ** zero 
17aa1 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  or the size of t
17aa2 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 70  he database in p
17aa3 61 67 65 2e 20 42 79 74 65 73 20 33 32 2e 2e 33  age. Bytes 32..3
17aa4 35 20 61 6e 64 20 33 35 2e 2e 33 39 0d 0a 20 20  5 and 35..39..  
17aa5 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65      ** should be
17aa6 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 77 68   page numbers wh
17aa7 69 63 68 20 61 72 65 20 6e 65 76 65 72 20 30 78  ich are never 0x
17aa8 66 66 66 66 66 66 66 66 2e 20 20 53 6f 20 66 69  ffffffff.  So fi
17aa9 6c 6c 69 6e 67 0d 0a 20 20 20 20 20 20 2a 2a 20  lling..      ** 
17aaa 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
17aab 72 73 5b 5d 20 77 69 74 68 20 61 6c 6c 20 30 78  rs[] with all 0x
17aac 66 66 20 62 79 74 65 73 20 73 68 6f 75 6c 64 20  ff bytes should 
17aad 73 75 66 66 69 63 65 2e 0d 0a 20 20 20 20 20 20  suffice...      
17aae 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72  **..      ** For
17aaf 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61   an encrypted da
17ab0 74 61 62 61 73 65 2c 20 74 68 65 20 73 69 74 75  tabase, the situ
17ab1 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 63 6f  ation is more co
17ab2 6d 70 6c 65 78 3a 20 20 62 79 74 65 73 0d 0a 20  mplex:  bytes.. 
17ab3 20 20 20 20 20 2a 2a 20 32 34 2e 2e 33 39 20 6f       ** 24..39 o
17ab4 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  f the database a
17ab5 72 65 20 77 68 69 74 65 20 6e 6f 69 73 65 2e 20  re white noise. 
17ab6 20 42 75 74 20 74 68 65 20 70 72 6f 62 61 62 69   But the probabi
17ab7 6c 69 74 79 20 6f 66 0d 0a 20 20 20 20 20 20 2a  lity of..      *
17ab8 2a 20 77 68 69 74 65 20 6e 6f 69 73 69 6e 67 20  * white noising 
17ab9 65 71 75 61 6c 69 6e 67 20 31 36 20 62 79 74 65  equaling 16 byte
17aba 73 20 6f 66 20 30 78 66 66 20 69 73 20 76 61 6e  s of 0xff is van
17abb 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 73  ishingly small s
17abc 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 73  o..      ** we s
17abd 68 6f 75 6c 64 20 73 74 69 6c 6c 20 62 65 20 6f  hould still be o
17abe 6b 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  k...      */..  
17abf 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65      memset(pPage
17ac0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 30  r->dbFileVers, 0
17ac1 78 66 66 2c 20 73 69 7a 65 6f 66 28 70 50 61 67  xff, sizeof(pPag
17ac2 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29  er->dbFileVers))
17ac3 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
17ac4 20 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65 56       u8 *dbFileV
17ac5 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50 67  ers = &((u8*)pPg
17ac6 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0d 0a 20  ->pData)[24];.. 
17ac7 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
17ac8 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
17ac9 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a   dbFileVers, siz
17aca 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
17acb 6c 65 56 65 72 73 29 29 3b 0d 0a 20 20 20 20 7d  leVers));..    }
17acc 0d 0a 20 20 7d 0d 0a 20 20 43 4f 44 45 43 31 28  ..  }..  CODEC1(
17acd 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61  pPager, pPg->pDa
17ace 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20  ta, pgno, 3, rc 
17acf 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  = SQLITE_NOMEM);
17ad0 0d 0a 0d 0a 20 20 50 41 47 45 52 5f 49 4e 43 52  ....  PAGER_INCR
17ad1 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72  (sqlite3_pager_r
17ad2 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0d 0a 20  eaddb_count);.. 
17ad3 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67   PAGER_INCR(pPag
17ad4 65 72 2d 3e 6e 52 65 61 64 29 3b 0d 0a 20 20 49  er->nRead);..  I
17ad5 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 70  OTRACE(("PGIN %p
17ad6 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
17ad7 70 67 6e 6f 29 29 3b 0d 0a 20 20 50 41 47 45 52  pgno));..  PAGER
17ad8 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64  TRACE(("FETCH %d
17ad9 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
17ada 38 78 29 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20  8x)\n",..       
17adb 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
17adc 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70  pPager), pgno, p
17add 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
17ade 67 29 29 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72  g)));....  retur
17adf 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
17ae0 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 76 61  ** Update the va
17ae1 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67  lue of the chang
17ae2 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 6f 66 66  e-counter at off
17ae3 73 65 74 73 20 32 34 20 61 6e 64 20 39 32 20 69  sets 24 and 92 i
17ae4 6e 0d 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  n..** the header
17ae5 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 20   and the sqlite 
17ae6 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61  version number a
17ae7 74 20 6f 66 66 73 65 74 20 39 36 2e 0d 0a 2a 2a  t offset 96...**
17ae8 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  ..** This is an 
17ae9 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 75 70  unconditional up
17aea 64 61 74 65 2e 20 20 53 65 65 20 61 6c 73 6f 20  date.  See also 
17aeb 74 68 65 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  the pager_incr_c
17aec 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 0d 0a  hangecounter()..
17aed 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 69 63 68  ** routine which
17aee 20 6f 6e 6c 79 20 75 70 64 61 74 65 73 20 74 68   only updates th
17aef 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
17af0 20 69 66 20 74 68 65 20 75 70 64 61 74 65 20 69   if the update i
17af1 73 20 61 63 74 75 61 6c 6c 79 0d 0a 2a 2a 20 6e  s actually..** n
17af2 65 65 64 65 64 2c 20 61 73 20 64 65 74 65 72 6d  eeded, as determ
17af3 69 6e 65 64 20 62 79 20 74 68 65 20 70 50 61 67  ined by the pPag
17af4 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44  er->changeCountD
17af5 6f 6e 65 20 73 74 61 74 65 20 76 61 72 69 61 62  one state variab
17af6 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  le...*/..static 
17af7 76 6f 69 64 20 70 61 67 65 72 5f 77 72 69 74 65  void pager_write
17af8 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50  _changecounter(P
17af9 67 48 64 72 20 2a 70 50 67 29 7b 0d 0a 20 20 75  gHdr *pPg){..  u
17afa 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65  32 change_counte
17afb 72 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 63 72 65  r;....  /* Incre
17afc 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a  ment the value j
17afd 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69  ust read and wri
17afe 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79  te it back to by
17aff 74 65 20 32 34 2e 20 2a 2f 0d 0a 20 20 63 68 61  te 24. */..  cha
17b00 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71  nge_counter = sq
17b01 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75  lite3Get4byte((u
17b02 38 2a 29 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e  8*)pPg->pPager->
17b03 64 62 46 69 6c 65 56 65 72 73 29 2b 31 3b 0d 0a  dbFileVers)+1;..
17b04 20 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68    put32bits(((ch
17b05 61 72 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b  ar*)pPg->pData)+
17b06 32 34 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74  24, change_count
17b07 65 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 73  er);....  /* Als
17b08 6f 20 73 74 6f 72 65 20 74 68 65 20 53 51 4c 69  o store the SQLi
17b09 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  te version numbe
17b0a 72 20 69 6e 20 62 79 74 65 73 20 39 36 2e 2e 39  r in bytes 96..9
17b0b 39 20 61 6e 64 20 69 6e 0d 0a 20 20 2a 2a 20 62  9 and in..  ** b
17b0c 79 74 65 73 20 39 32 2e 2e 39 35 20 73 74 6f 72  ytes 92..95 stor
17b0d 65 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75  e the change cou
17b0e 6e 74 65 72 20 66 6f 72 20 77 68 69 63 68 20 74  nter for which t
17b0f 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  he version numbe
17b10 72 0d 0a 20 20 2a 2a 20 69 73 20 76 61 6c 69 64  r..  ** is valid
17b11 2e 20 2a 2f 0d 0a 20 20 70 75 74 33 32 62 69 74  . */..  put32bit
17b12 73 28 28 28 63 68 61 72 2a 29 70 50 67 2d 3e 70  s(((char*)pPg->p
17b13 44 61 74 61 29 2b 39 32 2c 20 63 68 61 6e 67 65  Data)+92, change
17b14 5f 63 6f 75 6e 74 65 72 29 3b 0d 0a 20 20 70 75  _counter);..  pu
17b15 74 33 32 62 69 74 73 28 28 28 63 68 61 72 2a 29  t32bits(((char*)
17b16 70 50 67 2d 3e 70 44 61 74 61 29 2b 39 36 2c 20  pPg->pData)+96, 
17b17 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
17b18 55 4d 42 45 52 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69  UMBER);..}....#i
17b19 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17b1a 54 5f 57 41 4c 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  T_WAL../*..** Th
17b1b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
17b1c 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20  nvoked once for 
17b1d 65 61 63 68 20 70 61 67 65 20 74 68 61 74 20 68  each page that h
17b1e 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
17b1f 0d 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 74  ..** written int
17b20 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 77  o the log file w
17b21 68 65 6e 20 61 20 57 41 4c 20 74 72 61 6e 73 61  hen a WAL transa
17b22 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
17b23 62 61 63 6b 2e 0d 0a 2a 2a 20 50 61 72 61 6d 65  back...** Parame
17b24 74 65 72 20 69 50 67 20 69 73 20 74 68 65 20 70  ter iPg is the p
17b25 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  age number of sa
17b26 69 64 20 70 61 67 65 2e 20 54 68 65 20 70 43 74  id page. The pCt
17b27 78 20 61 72 67 75 6d 65 6e 74 20 0d 0a 2a 2a 20  x argument ..** 
17b28 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 70 6f  is actually a po
17b29 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67  inter to the Pag
17b2a 65 72 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a  er structure...*
17b2b 2a 0d 0a 2a 2a 20 49 66 20 70 61 67 65 20 69 50  *..** If page iP
17b2c 67 20 69 73 20 70 72 65 73 65 6e 74 20 69 6e 20  g is present in 
17b2d 74 68 65 20 63 61 63 68 65 2c 20 61 6e 64 20 68  the cache, and h
17b2e 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e  as no outstandin
17b2f 67 20 72 65 66 65 72 65 6e 63 65 73 2c 0d 0a 2a  g references,..*
17b30 2a 20 69 74 20 69 73 20 64 69 73 63 61 72 64 65  * it is discarde
17b31 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  d. Otherwise, if
17b32 20 74 68 65 72 65 20 61 72 65 20 6f 6e 65 20 6f   there are one o
17b33 72 20 6d 6f 72 65 20 6f 75 74 73 74 61 6e 64 69  r more outstandi
17b34 6e 67 0d 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  ng..** reference
17b35 73 2c 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  s, the page cont
17b36 65 6e 74 20 69 73 20 72 65 6c 6f 61 64 65 64 20  ent is reloaded 
17b37 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
17b38 65 2e 20 49 66 20 74 68 65 0d 0a 2a 2a 20 61 74  e. If the..** at
17b39 74 65 6d 70 74 20 74 6f 20 72 65 6c 6f 61 64 20  tempt to reload 
17b3a 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 74 68 65  content from the
17b3b 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 71   database is req
17b3c 75 69 72 65 64 20 61 6e 64 20 66 61 69 6c 73 2c  uired and fails,
17b3d 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20   ..** return an 
17b3e 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
17b3f 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51  e. Otherwise, SQ
17b40 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 73 74  LITE_OK...*/..st
17b41 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 55 6e  atic int pagerUn
17b42 64 6f 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20  doCallback(void 
17b43 2a 70 43 74 78 2c 20 50 67 6e 6f 20 69 50 67 29  *pCtx, Pgno iPg)
17b44 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  {..  int rc = SQ
17b45 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 50 61 67 65  LITE_OK;..  Page
17b46 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67  r *pPager = (Pag
17b47 65 72 20 2a 29 70 43 74 78 3b 0d 0a 20 20 50 67  er *)pCtx;..  Pg
17b48 48 64 72 20 2a 70 50 67 3b 0d 0a 0d 0a 20 20 70  Hdr *pPg;....  p
17b49 50 67 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  Pg = sqlite3Page
17b4a 72 4c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  rLookup(pPager, 
17b4b 69 50 67 29 3b 0d 0a 20 20 69 66 28 20 70 50 67  iPg);..  if( pPg
17b4c 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   ){..    if( sql
17b4d 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
17b4e 66 63 6f 75 6e 74 28 70 50 67 29 3d 3d 31 20 29  fcount(pPg)==1 )
17b4f 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
17b50 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b  PcacheDrop(pPg);
17b51 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
17b52 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 50      rc = readDbP
17b53 61 67 65 28 70 50 67 29 3b 0d 0a 20 20 20 20 20  age(pPg);..     
17b54 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17b55 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  OK ){..        p
17b56 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72  Pager->xReiniter
17b57 28 70 50 67 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  (pPg);..      }.
17b58 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
17b59 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0d 0a  gerUnref(pPg);..
17b5a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
17b5b 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 20  /* Normally, if 
17b5c 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
17b5d 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 61 6e   rolled back, an
17b5e 79 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73  y backup process
17b5f 65 73 20 61 72 65 0d 0a 20 20 2a 2a 20 75 70 64  es are..  ** upd
17b60 61 74 65 64 20 61 73 20 64 61 74 61 20 69 73 20  ated as data is 
17b61 63 6f 70 69 65 64 20 6f 75 74 20 6f 66 20 74 68  copied out of th
17b62 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
17b63 61 6c 20 61 6e 64 20 69 6e 74 6f 20 74 68 65 0d  al and into the.
17b64 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
17b65 54 68 69 73 20 69 73 20 6e 6f 74 20 67 65 6e 65  This is not gene
17b66 72 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 77  rally possible w
17b67 69 74 68 20 61 20 57 41 4c 20 64 61 74 61 62 61  ith a WAL databa
17b68 73 65 2c 20 61 73 0d 0a 20 20 2a 2a 20 72 6f 6c  se, as..  ** rol
17b69 6c 62 61 63 6b 20 69 6e 76 6f 6c 76 65 73 20 73  lback involves s
17b6a 69 6d 70 6c 79 20 74 72 75 6e 63 61 74 69 6e 67  imply truncating
17b6b 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 54   the log file. T
17b6c 68 65 72 65 66 6f 72 65 2c 20 69 66 20 6f 6e 65  herefore, if one
17b6d 0d 0a 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 66  ..  ** or more f
17b6e 72 61 6d 65 73 20 68 61 76 65 20 61 6c 72 65 61  rames have alrea
17b6f 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  dy been written 
17b70 74 6f 20 74 68 65 20 6c 6f 67 20 28 61 6e 64 20  to the log (and 
17b71 74 68 65 72 65 66 6f 72 65 20 0d 0a 20 20 2a 2a  therefore ..  **
17b72 20 61 6c 73 6f 20 63 6f 70 69 65 64 20 69 6e 74   also copied int
17b73 6f 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74  o the backup dat
17b74 61 62 61 73 65 73 29 20 61 73 20 70 61 72 74 20  abases) as part 
17b75 6f 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  of this transact
17b76 69 6f 6e 2c 0d 0a 20 20 2a 2a 20 74 68 65 20 62  ion,..  ** the b
17b77 61 63 6b 75 70 73 20 6d 75 73 74 20 62 65 20 72  ackups must be r
17b78 65 73 74 61 72 74 65 64 2e 0d 0a 20 20 2a 2f 0d  estarted...  */.
17b79 0a 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  .  sqlite3Backup
17b7a 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e  Restart(pPager->
17b7b 70 42 61 63 6b 75 70 29 3b 0d 0a 0d 0a 20 20 72  pBackup);....  r
17b7c 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
17b7d 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
17b7e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
17b7f 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61  o rollback a tra
17b80 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 61 20 57 41  nsaction on a WA
17b81 4c 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d  L database...*/.
17b82 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
17b83 72 52 6f 6c 6c 62 61 63 6b 57 61 6c 28 50 61 67  rRollbackWal(Pag
17b84 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20  er *pPager){..  
17b85 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
17b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b87 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
17b88 2f 0d 0a 20 20 50 67 48 64 72 20 2a 70 4c 69 73  /..  PgHdr *pLis
17b89 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
17b8a 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
17b8b 64 69 72 74 79 20 70 61 67 65 73 20 74 6f 20 72  dirty pages to r
17b8c 65 76 65 72 74 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a  evert */....  /*
17b8d 20 46 6f 72 20 61 6c 6c 20 70 61 67 65 73 20 69   For all pages i
17b8e 6e 20 74 68 65 20 63 61 63 68 65 20 74 68 61 74  n the cache that
17b8f 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 64   are currently d
17b90 69 72 74 79 20 6f 72 20 68 61 76 65 20 61 6c 72  irty or have alr
17b91 65 61 64 79 0d 0a 20 20 2a 2a 20 62 65 65 6e 20  eady..  ** been 
17b92 77 72 69 74 74 65 6e 20 28 62 75 74 20 6e 6f 74  written (but not
17b93 20 63 6f 6d 6d 69 74 74 65 64 29 20 74 6f 20 74   committed) to t
17b94 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 64 6f 20  he log file, do 
17b95 6f 6e 65 20 6f 66 20 74 68 65 20 0d 0a 20 20 2a  one of the ..  *
17b96 2a 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 20 20  * following:..  
17b97 2a 2a 0d 0a 20 20 2a 2a 20 20 20 2b 20 44 69 73  **..  **   + Dis
17b98 63 61 72 64 20 74 68 65 20 63 61 63 68 65 64 20  card the cached 
17b99 70 61 67 65 20 28 69 66 20 72 65 66 63 6f 75 6e  page (if refcoun
17b9a 74 3d 3d 30 29 2c 20 6f 72 0d 0a 20 20 2a 2a 20  t==0), or..  ** 
17b9b 20 20 2b 20 52 65 6c 6f 61 64 20 70 61 67 65 20    + Reload page 
17b9c 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 74 68 65  content from the
17b9d 20 64 61 74 61 62 61 73 65 20 28 69 66 20 72 65   database (if re
17b9e 66 63 6f 75 6e 74 3e 30 29 2e 0d 0a 20 20 2a 2f  fcount>0)...  */
17b9f 0d 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69  ..  pPager->dbSi
17ba0 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f  ze = pPager->dbO
17ba1 72 69 67 53 69 7a 65 3b 0d 0a 20 20 72 63 20 3d  rigSize;..  rc =
17ba2 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f 28   sqlite3WalUndo(
17ba3 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 61  pPager->pWal, pa
17ba4 67 65 72 55 6e 64 6f 43 61 6c 6c 62 61 63 6b 2c  gerUndoCallback,
17ba5 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65 72 29   (void *)pPager)
17ba6 3b 0d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  ;..  pList = sql
17ba7 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
17ba8 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ist(pPager->pPCa
17ba9 63 68 65 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20  che);..  while( 
17baa 70 4c 69 73 74 20 26 26 20 72 63 3d 3d 53 51 4c  pList && rc==SQL
17bab 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 50  ITE_OK ){..    P
17bac 67 48 64 72 20 2a 70 4e 65 78 74 20 3d 20 70 4c  gHdr *pNext = pL
17bad 69 73 74 2d 3e 70 44 69 72 74 79 3b 0d 0a 20 20  ist->pDirty;..  
17bae 20 20 72 63 20 3d 20 70 61 67 65 72 55 6e 64 6f    rc = pagerUndo
17baf 43 61 6c 6c 62 61 63 6b 28 28 76 6f 69 64 20 2a  Callback((void *
17bb0 29 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e  )pPager, pList->
17bb1 70 67 6e 6f 29 3b 0d 0a 20 20 20 20 70 4c 69 73  pgno);..    pLis
17bb2 74 20 3d 20 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d  t = pNext;..  }.
17bb3 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
17bb4 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
17bb5 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
17bb6 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73  wrapper around s
17bb7 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 73 28  qlite3WalFrames(
17bb8 29 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 6c 6f  ). As well as lo
17bb9 67 67 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 63 6f  gging..** the co
17bba 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69  ntents of the li
17bbb 73 74 20 6f 66 20 70 61 67 65 73 20 68 65 61 64  st of pages head
17bbc 65 64 20 62 79 20 70 4c 69 73 74 20 28 63 6f 6e  ed by pList (con
17bbd 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79  nected by pDirty
17bbe 29 2c 0d 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  ),..** this func
17bbf 74 69 6f 6e 20 6e 6f 74 69 66 69 65 73 20 61 6e  tion notifies an
17bc0 79 20 61 63 74 69 76 65 20 62 61 63 6b 75 70 20  y active backup 
17bc1 70 72 6f 63 65 73 73 65 73 20 74 68 61 74 20 74  processes that t
17bc2 68 65 20 70 61 67 65 73 20 68 61 76 65 0d 0a 2a  he pages have..*
17bc3 2a 20 63 68 61 6e 67 65 64 2e 20 0d 0a 2a 2a 0d  * changed. ..**.
17bc4 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20  .** The list of 
17bc5 70 61 67 65 73 20 70 61 73 73 65 64 20 69 6e 74  pages passed int
17bc6 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
17bc7 73 20 61 6c 77 61 79 73 20 73 6f 72 74 65 64 20  s always sorted 
17bc8 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0d  by page number..
17bc9 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 20 70 61  .** Hence, if pa
17bca 67 65 20 31 20 61 70 70 65 61 72 73 20 61 6e 79  ge 1 appears any
17bcb 77 68 65 72 65 20 6f 6e 20 74 68 65 20 6c 69 73  where on the lis
17bcc 74 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 74 68  t, it will be th
17bcd 65 20 66 69 72 73 74 20 70 61 67 65 2e 0d 0a 2a  e first page...*
17bce 2f 20 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  / ..static int p
17bcf 61 67 65 72 57 61 6c 46 72 61 6d 65 73 28 0d 0a  agerWalFrames(..
17bd0 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
17bd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17bd2 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63    /* Pager objec
17bd3 74 20 2a 2f 0d 0a 20 20 50 67 48 64 72 20 2a 70  t */..  PgHdr *p
17bd4 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  List,           
17bd5 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
17bd6 6f 66 20 66 72 61 6d 65 73 20 74 6f 20 6c 6f 67  of frames to log
17bd7 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 6e 54 72 75   */..  Pgno nTru
17bd8 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  ncate,          
17bd9 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
17bda 73 65 20 73 69 7a 65 20 61 66 74 65 72 20 74 68  se size after th
17bdb 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0d 0a 20 20  is commit */..  
17bdc 69 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20 20 20  int isCommit    
17bdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17bde 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
17bdf 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f 0d 0a  is a commit */..
17be0 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  ){..  int rc;   
17be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17be2 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
17be3 63 6f 64 65 20 2a 2f 0d 0a 23 69 66 20 64 65 66  code */..#if def
17be4 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
17be5 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
17be6 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
17be7 29 0d 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20  )..  PgHdr *p;  
17be8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17be9 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
17bea 69 6e 67 20 6f 76 65 72 20 70 61 67 65 73 20 2a  ing over pages *
17beb 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 61  /..#endif....  a
17bec 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
17bed 57 61 6c 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  Wal );..  assert
17bee 28 20 70 4c 69 73 74 20 29 3b 0d 0a 23 69 66 64  ( pList );..#ifd
17bef 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d  ef SQLITE_DEBUG.
17bf0 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61  .  /* Verify tha
17bf1 74 20 74 68 65 20 70 61 67 65 20 6c 69 73 74 20  t the page list 
17bf2 69 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  is in accending 
17bf3 6f 72 64 65 72 20 2a 2f 0d 0a 20 20 66 6f 72 28  order */..  for(
17bf4 70 3d 70 4c 69 73 74 3b 20 70 20 26 26 20 70 2d  p=pList; p && p-
17bf5 3e 70 44 69 72 74 79 3b 20 70 3d 70 2d 3e 70 44  >pDirty; p=p->pD
17bf6 69 72 74 79 29 7b 0d 0a 20 20 20 20 61 73 73 65  irty){..    asse
17bf7 72 74 28 20 70 2d 3e 70 67 6e 6f 20 3c 20 70 2d  rt( p->pgno < p-
17bf8 3e 70 44 69 72 74 79 2d 3e 70 67 6e 6f 20 29 3b  >pDirty->pgno );
17bf9 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
17bfa 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 74 20  .  if( isCommit 
17bfb 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20  ){..    /* If a 
17bfc 57 41 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  WAL transaction 
17bfd 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74  is being committ
17bfe 65 64 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ed, there is no 
17bff 70 6f 69 6e 74 20 69 6e 20 77 72 69 74 69 6e 67  point in writing
17c00 0d 0a 20 20 20 20 2a 2a 20 61 6e 79 20 70 61 67  ..    ** any pag
17c01 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  es with page num
17c02 62 65 72 73 20 67 72 65 61 74 65 72 20 74 68 61  bers greater tha
17c03 6e 20 6e 54 72 75 6e 63 61 74 65 20 69 6e 74 6f  n nTruncate into
17c04 20 74 68 65 20 57 41 4c 20 66 69 6c 65 2e 0d 0a   the WAL file...
17c05 20 20 20 20 2a 2a 20 54 68 65 79 20 77 69 6c 6c      ** They will
17c06 20 6e 65 76 65 72 20 62 65 20 72 65 61 64 20 62   never be read b
17c07 79 20 61 6e 79 20 63 6c 69 65 6e 74 2e 20 53 6f  y any client. So
17c08 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72 6f   remove them fro
17c09 6d 20 74 68 65 20 70 44 69 72 74 79 0d 0a 20 20  m the pDirty..  
17c0a 20 20 2a 2a 20 6c 69 73 74 20 68 65 72 65 2e 20    ** list here. 
17c0b 2a 2f 0d 0a 20 20 20 20 50 67 48 64 72 20 2a 70  */..    PgHdr *p
17c0c 3b 0d 0a 20 20 20 20 50 67 48 64 72 20 2a 2a 70  ;..    PgHdr **p
17c0d 70 4e 65 78 74 20 3d 20 26 70 4c 69 73 74 3b 0d  pNext = &pList;.
17c0e 0a 20 20 20 20 66 6f 72 28 70 3d 70 4c 69 73 74  .    for(p=pList
17c0f 3b 20 28 2a 70 70 4e 65 78 74 20 3d 20 70 29 3b  ; (*ppNext = p);
17c10 20 70 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0d 0a   p=p->pDirty){..
17c11 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e        if( p->pgn
17c12 6f 3c 3d 6e 54 72 75 6e 63 61 74 65 20 29 20 70  o<=nTruncate ) p
17c13 70 4e 65 78 74 20 3d 20 26 70 2d 3e 70 44 69 72  pNext = &p->pDir
17c14 74 79 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ty;..    }..    
17c15 61 73 73 65 72 74 28 20 70 4c 69 73 74 20 29 3b  assert( pList );
17c16 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70  ..  }....  if( p
17c17 4c 69 73 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 20  List->pgno==1 ) 
17c18 70 61 67 65 72 5f 77 72 69 74 65 5f 63 68 61 6e  pager_write_chan
17c19 67 65 63 6f 75 6e 74 65 72 28 70 4c 69 73 74 29  gecounter(pList)
17c1a 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
17c1b 33 57 61 6c 46 72 61 6d 65 73 28 70 50 61 67 65  3WalFrames(pPage
17c1c 72 2d 3e 70 57 61 6c 2c 20 0d 0a 20 20 20 20 20  r->pWal, ..     
17c1d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
17c1e 65 2c 20 70 4c 69 73 74 2c 20 6e 54 72 75 6e 63  e, pList, nTrunc
17c1f 61 74 65 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 70  ate, isCommit, p
17c20 50 61 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c  Pager->walSyncFl
17c21 61 67 73 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28  ags..  );..  if(
17c22 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
17c23 26 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  & pPager->pBacku
17c24 70 20 29 7b 0d 0a 20 20 20 20 50 67 48 64 72 20  p ){..    PgHdr 
17c25 2a 70 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d 70  *p;..    for(p=p
17c26 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 44  List; p; p=p->pD
17c27 69 72 74 79 29 7b 0d 0a 20 20 20 20 20 20 73 71  irty){..      sq
17c28 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74  lite3BackupUpdat
17c29 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  e(pPager->pBacku
17c2a 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 28 75 38 20  p, p->pgno, (u8 
17c2b 2a 29 70 2d 3e 70 44 61 74 61 29 3b 0d 0a 20 20  *)p->pData);..  
17c2c 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 23 69 66 64    }..  }....#ifd
17c2d 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
17c2e 50 41 47 45 53 0d 0a 20 20 70 4c 69 73 74 20 3d  PAGES..  pList =
17c2f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69   sqlite3PcacheDi
17c30 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e  rtyList(pPager->
17c31 70 50 43 61 63 68 65 29 3b 0d 0a 20 20 66 6f 72  pPCache);..  for
17c32 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70  (p=pList; p; p=p
17c33 2d 3e 70 44 69 72 74 79 29 7b 0d 0a 20 20 20 20  ->pDirty){..    
17c34 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61  pager_set_pageha
17c35 73 68 28 70 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  sh(p);..  }..#en
17c36 64 69 66 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20  dif....  return 
17c37 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
17c38 20 42 65 67 69 6e 20 61 20 72 65 61 64 20 74 72   Begin a read tr
17c39 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65  ansaction on the
17c3a 20 57 41 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68   WAL...**..** Th
17c3b 69 73 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20  is routine used 
17c3c 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 22 70 61  to be called "pa
17c3d 67 65 72 4f 70 65 6e 53 6e 61 70 73 68 6f 74 28  gerOpenSnapshot(
17c3e 29 22 20 62 65 63 61 75 73 65 20 69 74 20 65 73  )" because it es
17c3f 73 65 6e 74 69 61 6c 6c 79 0d 0a 2a 2a 20 6d 61  sentially..** ma
17c40 6b 65 73 20 61 20 73 6e 61 70 73 68 6f 74 20 6f  kes a snapshot o
17c41 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  f the database a
17c42 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f  t the current po
17c43 69 6e 74 20 69 6e 20 74 69 6d 65 20 61 6e 64 20  int in time and 
17c44 70 72 65 73 65 72 76 65 73 0d 0a 2a 2a 20 74 68  preserves..** th
17c45 61 74 20 73 6e 61 70 73 68 6f 74 20 66 6f 72 20  at snapshot for 
17c46 75 73 65 20 62 79 20 74 68 65 20 72 65 61 64 65  use by the reade
17c47 72 20 69 6e 20 73 70 69 74 65 20 6f 66 20 63 6f  r in spite of co
17c48 6e 63 75 72 72 65 6e 74 6c 79 20 63 68 61 6e 67  ncurrently chang
17c49 65 73 20 62 79 0d 0a 2a 2a 20 6f 74 68 65 72 20  es by..** other 
17c4a 77 72 69 74 65 72 73 20 6f 72 20 63 68 65 63 6b  writers or check
17c4b 70 6f 69 6e 74 65 72 73 2e 0d 0a 2a 2f 0d 0a 73  pointers...*/..s
17c4c 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 42  tatic int pagerB
17c4d 65 67 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74  eginReadTransact
17c4e 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65  ion(Pager *pPage
17c4f 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20  r){..  int rc;  
17c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c51 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
17c52 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   code */..  int 
17c53 63 68 61 6e 67 65 64 20 3d 20 30 3b 20 20 20 20  changed = 0;    
17c54 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17c55 72 75 65 20 69 66 20 63 61 63 68 65 20 6d 75 73  rue if cache mus
17c56 74 20 62 65 20 72 65 73 65 74 20 2a 2f 0d 0a 0d  t be reset */...
17c57 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65 72  .  assert( pager
17c58 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29  UseWal(pPager) )
17c59 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
17c5a 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
17c5b 45 52 5f 4f 50 45 4e 20 7c 7c 20 70 50 61 67 65  ER_OPEN || pPage
17c5c 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
17c5d 5f 52 45 41 44 45 52 20 29 3b 0d 0a 0d 0a 20 20  _READER );....  
17c5e 2f 2a 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64  /* sqlite3WalEnd
17c5f 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28  ReadTransaction(
17c60 29 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64  ) was not called
17c61 20 66 6f 72 20 74 68 65 20 70 72 65 76 69 6f 75   for the previou
17c62 73 0d 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  s..  ** transact
17c63 69 6f 6e 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d  ion in locking_m
17c64 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 2e 20 20  ode=EXCLUSIVE.  
17c65 53 6f 20 63 61 6c 6c 20 69 74 20 6e 6f 77 2e 20  So call it now. 
17c66 20 49 66 20 77 65 0d 0a 20 20 2a 2a 20 61 72 65   If we..  ** are
17c67 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   in locking_mode
17c68 3d 4e 4f 52 4d 41 4c 20 61 6e 64 20 45 6e 64 52  =NORMAL and EndR
17c69 65 61 64 28 29 20 77 61 73 20 70 72 65 76 69 6f  ead() was previo
17c6a 75 73 6c 79 20 63 61 6c 6c 65 64 2c 0d 0a 20 20  usly called,..  
17c6b 2a 2a 20 74 68 65 20 64 75 70 6c 69 63 61 74 65  ** the duplicate
17c6c 20 63 61 6c 6c 20 69 73 20 68 61 72 6d 6c 65 73   call is harmles
17c6d 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69  s...  */..  sqli
17c6e 74 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61  te3WalEndReadTra
17c6f 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d  nsaction(pPager-
17c70 3e 70 57 61 6c 29 3b 0d 0a 0d 0a 20 20 72 63 20  >pWal);....  rc 
17c71 3d 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69  = sqlite3WalBegi
17c72 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e  nReadTransaction
17c73 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 26  (pPager->pWal, &
17c74 63 68 61 6e 67 65 64 29 3b 0d 0a 20 20 69 66 28  changed);..  if(
17c75 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
17c76 7c 20 63 68 61 6e 67 65 64 20 29 7b 0d 0a 20 20  | changed ){..  
17c77 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
17c78 61 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ager);..  }.... 
17c79 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
17c7a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
17c7b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
17c7c 73 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74  s called as part
17c7d 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69 74 69   of the transiti
17c7e 6f 6e 20 66 72 6f 6d 20 50 41 47 45 52 5f 4f 50  on from PAGER_OP
17c7f 45 4e 0d 0a 2a 2a 20 74 6f 20 50 41 47 45 52 5f  EN..** to PAGER_
17c80 52 45 41 44 45 52 20 73 74 61 74 65 20 74 6f 20  READER state to 
17c81 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 73 69  determine the si
17c82 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
17c83 73 65 20 66 69 6c 65 0d 0a 2a 2a 20 69 6e 20 70  se file..** in p
17c84 61 67 65 73 20 28 61 73 73 75 6d 69 6e 67 20 74  ages (assuming t
17c85 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 75 72  he page size cur
17c86 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
17c87 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 29   Pager.pageSize)
17c88 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 20  ...**..** If no 
17c89 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51  error occurs, SQ
17c8a 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
17c8b 6e 65 64 20 61 6e 64 20 74 68 65 20 73 69 7a 65  ned and the size
17c8c 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
17c8d 0d 0a 2a 2a 20 69 6e 20 70 61 67 65 73 20 69 73  ..** in pages is
17c8e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 6e 50 61   stored in *pnPa
17c8f 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61  ge. Otherwise, a
17c90 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 70 65  n error code (pe
17c91 72 68 61 70 73 0d 0a 2a 2a 20 53 51 4c 49 54 45  rhaps..** SQLITE
17c92 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 20 69 73  _IOERR_FSTAT) is
17c93 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
17c94 6e 50 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e  nPage is left un
17c95 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2f 0d 0a 73  modified...*/..s
17c96 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50  tatic int pagerP
17c97 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a  agecount(Pager *
17c98 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 2a 70 6e  pPager, Pgno *pn
17c99 50 61 67 65 29 7b 0d 0a 20 20 50 67 6e 6f 20 6e  Page){..  Pgno n
17c9a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
17c9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
17c9c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61  ue to return via
17c9d 20 2a 70 6e 50 61 67 65 20 2a 2f 0d 0a 0d 0a 20   *pnPage */.... 
17c9e 20 2f 2a 20 51 75 65 72 79 20 74 68 65 20 57 41   /* Query the WA
17c9f 4c 20 73 75 62 2d 73 79 73 74 65 6d 20 66 6f 72  L sub-system for
17ca0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69   the database si
17ca1 7a 65 2e 20 54 68 65 20 57 61 6c 44 62 73 69 7a  ze. The WalDbsiz
17ca2 65 28 29 0d 0a 20 20 2a 2a 20 66 75 6e 63 74 69  e()..  ** functi
17ca3 6f 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20  on returns zero 
17ca4 69 66 20 74 68 65 20 57 41 4c 20 69 73 20 6e 6f  if the WAL is no
17ca5 74 20 6f 70 65 6e 20 28 69 2e 65 2e 20 50 61 67  t open (i.e. Pag
17ca6 65 72 2e 70 57 61 6c 3d 3d 30 29 2c 20 6f 72 0d  er.pWal==0), or.
17ca7 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 64 61 74  .  ** if the dat
17ca8 61 62 61 73 65 20 73 69 7a 65 20 69 73 20 6e 6f  abase size is no
17ca9 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 54 68 65  t available. The
17caa 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 69   database size i
17cab 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 61 76 61 69  s not..  ** avai
17cac 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 57  lable from the W
17cad 41 4c 20 73 75 62 2d 73 79 73 74 65 6d 20 69 66  AL sub-system if
17cae 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73   the log file is
17caf 20 65 6d 70 74 79 20 6f 72 0d 0a 20 20 2a 2a 20   empty or..  ** 
17cb0 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 76 61 6c 69  contains no vali
17cb1 64 20 63 6f 6d 6d 69 74 74 65 64 20 74 72 61 6e  d committed tran
17cb2 73 61 63 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2f 0d  sactions...  */.
17cb3 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
17cb4 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
17cb5 5f 4f 50 45 4e 20 29 3b 0d 0a 20 20 61 73 73 65  _OPEN );..  asse
17cb6 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63  rt( pPager->eLoc
17cb7 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c  k>=SHARED_LOCK |
17cb8 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64  | pPager->noRead
17cb9 6c 6f 63 6b 20 29 3b 0d 0a 20 20 6e 50 61 67 65  lock );..  nPage
17cba 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 44 62 73   = sqlite3WalDbs
17cbb 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 57 61 6c  ize(pPager->pWal
17cbc 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68  );....  /* If th
17cbd 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
17cbe 77 61 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  was not availabl
17cbf 65 20 66 72 6f 6d 20 74 68 65 20 57 41 4c 20 73  e from the WAL s
17cc0 75 62 2d 73 79 73 74 65 6d 2c 0d 0a 20 20 2a 2a  ub-system,..  **
17cc1 20 64 65 74 65 72 6d 69 6e 65 20 69 74 20 62 61   determine it ba
17cc2 73 65 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20  sed on the size 
17cc3 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
17cc4 66 69 6c 65 2e 20 49 66 20 74 68 65 20 73 69 7a  file. If the siz
17cc5 65 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  e..  ** of the d
17cc6 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
17cc7 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 6d  not an integer m
17cc8 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 20 70  ultiple of the p
17cc9 61 67 65 2d 73 69 7a 65 2c 0d 0a 20 20 2a 2a 20  age-size,..  ** 
17cca 72 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74 68  round down to th
17ccb 65 20 6e 65 61 72 65 73 74 20 70 61 67 65 2e 20  e nearest page. 
17ccc 45 78 63 65 70 74 2c 20 61 6e 79 20 66 69 6c 65  Except, any file
17ccd 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30 0d 0a   larger than 0..
17cce 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69    ** bytes in si
17ccf 7a 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ze is considered
17cd0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 74 20 6c   to contain at l
17cd1 65 61 73 74 20 6f 6e 65 20 70 61 67 65 2e 0d 0a  east one page...
17cd2 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 50 61 67    */..  if( nPag
17cd3 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 36 34  e==0 ){..    i64
17cd4 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20   n = 0;         
17cd5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
17cd6 7a 65 20 6f 66 20 64 62 20 66 69 6c 65 20 69 6e  ze of db file in
17cd7 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20 20 20 61   bytes */..    a
17cd8 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
17cd9 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
17cda 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
17cdb 0d 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e  ..    if( isOpen
17cdc 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0d  (pPager->fd) ){.
17cdd 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20  .      int rc = 
17cde 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
17cdf 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e  e(pPager->fd, &n
17ce0 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
17ce1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
17ce2 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
17ce3 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  c;..      }..   
17ce4 20 7d 0d 0a 20 20 20 20 6e 50 61 67 65 20 3d 20   }..    nPage = 
17ce5 28 50 67 6e 6f 29 28 28 6e 2b 70 50 61 67 65 72  (Pgno)((n+pPager
17ce6 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 20 2f 20  ->pageSize-1) / 
17ce7 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
17ce8 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  );..  }....  /* 
17ce9 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e  If the current n
17cea 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
17ceb 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 72  n the file is gr
17cec 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 0d 0a  eater than the..
17ced 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20    ** configured 
17cee 6d 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e 75  maximum pager nu
17cef 6d 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 74  mber, increase t
17cf0 68 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74  he allowed limit
17cf1 20 73 6f 0d 0a 20 20 2a 2a 20 74 68 61 74 20 74   so..  ** that t
17cf2 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72  he file can be r
17cf3 65 61 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ead...  */..  if
17cf4 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d 3e  ( nPage>pPager->
17cf5 6d 78 50 67 6e 6f 20 29 7b 0d 0a 20 20 20 20 70  mxPgno ){..    p
17cf6 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
17cf7 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0d 0a 20 20  (Pgno)nPage;..  
17cf8 7d 0d 0a 0d 0a 20 20 2a 70 6e 50 61 67 65 20 3d  }....  *pnPage =
17cf9 20 6e 50 61 67 65 3b 0d 0a 20 20 72 65 74 75 72   nPage;..  retur
17cfa 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
17cfb 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
17cfc 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 2f 2a 0d 0a  E_OMIT_WAL../*..
17cfd 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  ** Check if the 
17cfe 2a 2d 77 61 6c 20 66 69 6c 65 20 74 68 61 74 20  *-wal file that 
17cff 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
17d00 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e  he database open
17d01 65 64 20 62 79 20 70 50 61 67 65 72 0d 0a 2a 2a  ed by pPager..**
17d02 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 64   exists if the d
17d03 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 65  atabase is not e
17d04 6d 70 79 2c 20 6f 72 20 76 65 72 69 66 79 20 74  mpy, or verify t
17d05 68 61 74 20 74 68 65 20 2a 2d 77 61 6c 20 66 69  hat the *-wal fi
17d06 6c 65 20 64 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20  le does..** not 
17d07 65 78 69 73 74 20 28 62 79 20 64 65 6c 65 74 69  exist (by deleti
17d08 6e 67 20 69 74 29 20 69 66 20 74 68 65 20 64 61  ng it) if the da
17d09 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 65  tabase file is e
17d0a 6d 70 74 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  mpty...**..** If
17d0b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
17d0c 20 6e 6f 74 20 65 6d 70 74 79 20 61 6e 64 20 74   not empty and t
17d0d 68 65 20 2a 2d 77 61 6c 20 66 69 6c 65 20 65 78  he *-wal file ex
17d0e 69 73 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 70  ists, open the p
17d0f 61 67 65 72 0d 0a 2a 2a 20 69 6e 20 57 41 4c 20  ager..** in WAL 
17d10 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61  mode.  If the da
17d11 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 79 20  tabase is empty 
17d12 6f 72 20 69 66 20 6e 6f 20 2a 2d 77 61 6c 20 66  or if no *-wal f
17d13 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 0d 0a  ile exists and..
17d14 2a 2a 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ** if no error o
17d15 63 63 75 72 73 2c 20 6d 61 6b 65 20 73 75 72 65  ccurs, make sure
17d16 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f   Pager.journalMo
17d17 64 65 20 69 73 20 6e 6f 74 20 73 65 74 20 74 6f  de is not set to
17d18 0d 0a 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e  ..** PAGER_JOURN
17d19 41 4c 4d 4f 44 45 5f 57 41 4c 2e 0d 0a 2a 2a 0d  ALMODE_WAL...**.
17d1a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
17d1b 45 5f 4f 4b 20 6f 72 20 61 6e 20 65 72 72 6f 72  E_OK or an error
17d1c 20 63 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54   code...**..** T
17d1d 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 68  he caller must h
17d1e 6f 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63  old a SHARED loc
17d1f 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
17d20 65 20 66 69 6c 65 20 74 6f 20 63 61 6c 6c 20 74  e file to call t
17d21 68 69 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his..** function
17d22 2e 20 42 65 63 61 75 73 65 20 61 6e 20 45 58 43  . Because an EXC
17d23 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74  LUSIVE lock on t
17d24 68 65 20 64 62 20 66 69 6c 65 20 69 73 20 72 65  he db file is re
17d25 71 75 69 72 65 64 20 74 6f 20 64 65 6c 65 74 65  quired to delete
17d26 20 0d 0a 2a 2a 20 61 20 57 41 4c 20 6f 6e 20 61   ..** a WAL on a
17d27 20 6e 6f 6e 65 2d 65 6d 70 74 79 20 64 61 74 61   none-empty data
17d28 62 61 73 65 2c 20 74 68 69 73 20 65 6e 73 75 72  base, this ensur
17d29 65 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 72  es there is no r
17d2a 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 0d 0a  ace condition ..
17d2b 2a 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20 78  ** between the x
17d2c 41 63 63 65 73 73 28 29 20 62 65 6c 6f 77 20 61  Access() below a
17d2d 6e 64 20 61 6e 20 78 44 65 6c 65 74 65 28 29 20  nd an xDelete() 
17d2e 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 62  being executed b
17d2f 79 20 73 6f 6d 65 20 0d 0a 2a 2a 20 6f 74 68 65  y some ..** othe
17d30 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a  r connection...*
17d31 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  /..static int pa
17d32 67 65 72 4f 70 65 6e 57 61 6c 49 66 50 72 65 73  gerOpenWalIfPres
17d33 65 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  ent(Pager *pPage
17d34 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  r){..  int rc = 
17d35 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 61 73  SQLITE_OK;..  as
17d36 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53  sert( pPager->eS
17d37 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e  tate==PAGER_OPEN
17d38 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
17d39 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 53 48  Pager->eLock>=SH
17d3a 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61  ARED_LOCK || pPa
17d3b 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20  ger->noReadlock 
17d3c 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 70 50 61  );....  if( !pPa
17d3d 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
17d3e 0d 0a 20 20 20 20 69 6e 74 20 69 73 57 61 6c 3b  ..    int isWal;
17d3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17d40 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 57      /* True if W
17d41 41 4c 20 66 69 6c 65 20 65 78 69 73 74 73 20 2a  AL file exists *
17d42 2f 0d 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67  /..    Pgno nPag
17d43 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
17d44 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
17d45 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
17d46 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 72 63 20 3d  e */....    rc =
17d47 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28   pagerPagecount(
17d48 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
17d49 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ..    if( rc ) r
17d4a 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 69  eturn rc;..    i
17d4b 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0d 0a  f( nPage==0 ){..
17d4c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17d4d 65 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65  e3OsDelete(pPage
17d4e 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d  r->pVfs, pPager-
17d4f 3e 7a 57 61 6c 2c 20 30 29 3b 0d 0a 20 20 20 20  >zWal, 0);..    
17d50 20 20 69 73 57 61 6c 20 3d 20 30 3b 0d 0a 20 20    isWal = 0;..  
17d51 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
17d52 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
17d53 63 65 73 73 28 0d 0a 20 20 20 20 20 20 20 20 20  cess(..         
17d54 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70   pPager->pVfs, p
17d55 50 61 67 65 72 2d 3e 7a 57 61 6c 2c 20 53 51 4c  Pager->zWal, SQL
17d56 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
17d57 53 2c 20 26 69 73 57 61 6c 0d 0a 20 20 20 20 20  S, &isWal..     
17d58 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   );..    }..    
17d59 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17d5a 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  K ){..      if( 
17d5b 69 73 57 61 6c 20 29 7b 0d 0a 20 20 20 20 20 20  isWal ){..      
17d5c 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c 69    testcase( sqli
17d5d 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
17d5e 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
17d5f 68 65 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20  he)==0 );..     
17d60 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17d61 61 67 65 72 4f 70 65 6e 57 61 6c 28 70 50 61 67  agerOpenWal(pPag
17d62 65 72 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d  er, 0);..      }
17d63 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d  else if( pPager-
17d64 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
17d65 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
17d66 57 41 4c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  WAL ){..        
17d67 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
17d68 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
17d69 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 3b 0d  NALMODE_DELETE;.
17d6a 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
17d6b 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
17d6c 63 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  c;..}..#endif...
17d6d 0a 2f 2a 0d 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ./*..** Playback
17d6e 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65   savepoint pSave
17d6f 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53  point. Or, if pS
17d70 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20  avepoint==NULL, 
17d71 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0d 0a 2a  then playback..*
17d72 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73  * the entire mas
17d73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
17d74 2e 20 54 68 65 20 63 61 73 65 20 70 53 61 76 65  . The case pSave
17d75 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75  point==NULL occu
17d76 72 73 20 77 68 65 6e 20 0d 0a 2a 2a 20 61 20 52  rs when ..** a R
17d77 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61  OLLBACK TO comma
17d78 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  nd is invoked on
17d79 20 61 20 53 41 56 45 50 4f 49 4e 54 20 74 68 61   a SAVEPOINT tha
17d7a 74 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 69  t is a transacti
17d7b 6f 6e 20 0d 0a 2a 2a 20 73 61 76 65 70 6f 69 6e  on ..** savepoin
17d7c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20  t...**..** When 
17d7d 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 6e 6f  pSavepoint is no
17d7e 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e 67 20  t NULL (meaning 
17d7f 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f  a non-transactio
17d80 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 0d  n savepoint is .
17d81 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64  .** being rolled
17d82 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 68 65   back), then the
17d83 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69 73   rollback consis
17d84 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 72 65  ts of up to thre
17d85 65 20 73 74 61 67 65 73 2c 0d 0a 2a 2a 20 70 65  e stages,..** pe
17d86 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 6f  rformed in the o
17d87 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a 0d  rder specified:.
17d88 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 50 61 67 65  .**..**   * Page
17d89 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63  s are played bac
17d8a 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  k from the main 
17d8b 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67  journal starting
17d8c 20 61 74 20 62 79 74 65 0d 0a 2a 2a 20 20 20 20   at byte..**    
17d8d 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 76   offset PagerSav
17d8e 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61  epoint.iOffset a
17d8f 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f  nd continuing to
17d90 20 0d 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53   ..**     PagerS
17d91 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
17d92 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65  set, or to the e
17d93 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  nd of the main j
17d94 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 20 20 20 20 66  ournal..**     f
17d95 69 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65  ile if PagerSave
17d96 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74  point.iHdrOffset
17d97 20 69 73 20 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a   is zero...**..*
17d98 2a 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 61  *   * If PagerSa
17d99 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73  vepoint.iHdrOffs
17d9a 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  et is not zero, 
17d9b 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 70  then pages are p
17d9c 6c 61 79 65 64 0d 0a 2a 2a 20 20 20 20 20 62 61  layed..**     ba
17d9d 63 6b 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d  ck starting from
17d9e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
17d9f 64 65 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  der immediately 
17da0 66 6f 6c 6c 6f 77 69 6e 67 20 0d 0a 2a 2a 20 20  following ..**  
17da1 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
17da2 74 2e 69 48 64 72 4f 66 66 73 65 74 20 74 6f 20  t.iHdrOffset to 
17da3 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d  the end of the m
17da4 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
17da5 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 50 61  ...**..**   * Pa
17da6 67 65 73 20 61 72 65 20 74 68 65 6e 20 70 6c 61  ges are then pla
17da7 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  yed back from th
17da8 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69  e sub-journal fi
17da9 6c 65 2c 20 73 74 61 72 74 69 6e 67 0d 0a 2a 2a  le, starting..**
17daa 20 20 20 20 20 77 69 74 68 20 74 68 65 20 50 61       with the Pa
17dab 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 75  gerSavepoint.iSu
17dac 62 52 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e 75  bRec and continu
17dad 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ing to the end o
17dae 66 0d 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f  f..**     the jo
17daf 75 72 6e 61 6c 20 66 69 6c 65 2e 0d 0a 2a 2a 0d  urnal file...**.
17db0 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 20 74  .** Throughout t
17db1 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f 63  he rollback proc
17db2 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 20 61  ess, each time a
17db3 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 20   page is rolled 
17db4 62 61 63 6b 2c 20 74 68 65 0d 0a 2a 2a 20 63 6f  back, the..** co
17db5 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20  rresponding bit 
17db6 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 76  is set in a bitv
17db7 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 61  ec structure (va
17db8 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20  riable pDone in 
17db9 74 68 65 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  the..** implemen
17dba 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54  tation below). T
17dbb 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65  his is used to e
17dbc 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67  nsure that a pag
17dbd 65 20 69 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 72 6f  e is only..** ro
17dbe 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69  lled back the fi
17dbf 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65  rst time it is e
17dc0 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69  ncountered in ei
17dc1 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a  ther journal...*
17dc2 2a 0d 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f  *..** If pSavepo
17dc3 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  int is NULL, the
17dc4 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79  n pages are only
17dc5 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f   played back fro
17dc6 6d 20 74 68 65 20 6d 61 69 6e 0d 0a 2a 2a 20 6a  m the main..** j
17dc7 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65  ournal file. The
17dc8 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f  re is no need fo
17dc9 72 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68  r a bitvec in th
17dca 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  is case...**..**
17dcb 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c   In either case,
17dcc 20 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b   before playback
17dcd 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50   commences the P
17dce 61 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69  ager.dbSize vari
17dcf 61 62 6c 65 0d 0a 2a 2a 20 69 73 20 72 65 73 65  able..** is rese
17dd0 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74  t to the value t
17dd1 68 61 74 20 69 74 20 68 65 6c 64 20 61 74 20 74  hat it held at t
17dd2 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
17dd3 73 61 76 65 70 6f 69 6e 74 20 0d 0a 2a 2a 20 28  savepoint ..** (
17dd4 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e  or transaction).
17dd5 20 4e 6f 20 70 61 67 65 20 77 69 74 68 20 61 20   No page with a 
17dd6 70 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 61  page-number grea
17dd7 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 61  ter than this va
17dd8 6c 75 65 0d 0a 2a 2a 20 69 73 20 70 6c 61 79 65  lue..** is playe
17dd9 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69  d back. If one i
17dda 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74  s encountered it
17ddb 20 69 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 70   is simply skipp
17ddc 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ed...*/..static 
17ddd 69 6e 74 20 70 61 67 65 72 50 6c 61 79 62 61 63  int pagerPlaybac
17dde 6b 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72  kSavepoint(Pager
17ddf 20 2a 70 50 61 67 65 72 2c 20 50 61 67 65 72 53   *pPager, PagerS
17de0 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
17de1 6f 69 6e 74 29 7b 0d 0a 20 20 69 36 34 20 73 7a  oint){..  i64 sz
17de2 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  J;              
17de3 20 20 20 2f 2a 20 45 66 66 65 63 74 69 76 65 20     /* Effective 
17de4 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e  size of the main
17de5 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20 20 69   journal */..  i
17de6 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20  64 iHdrOff;     
17de7 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f          /* End o
17de8 66 20 66 69 72 73 74 20 73 65 67 6d 65 6e 74 20  f first segment 
17de9 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20  of main-journal 
17dea 72 65 63 6f 72 64 73 20 2a 2f 0d 0a 20 20 69 6e  records */..  in
17deb 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
17dec 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ;      /* Return
17ded 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 42 69 74 76   code */..  Bitv
17dee 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20  ec *pDone = 0;  
17def 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 74       /* Bitvec t
17df0 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 70  o ensure pages p
17df1 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20  layed back only 
17df2 6f 6e 63 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  once */....  ass
17df3 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74  ert( pPager->eSt
17df4 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52  ate!=PAGER_ERROR
17df5 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
17df6 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50  Pager->eState>=P
17df7 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b  AGER_WRITER_LOCK
17df8 45 44 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c  ED );....  /* Al
17df9 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 63 20  locate a bitvec 
17dfa 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20  to use to store 
17dfb 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 65 73  the set of pages
17dfc 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0d   rolled back */.
17dfd 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e  .  if( pSavepoin
17dfe 74 20 29 7b 0d 0a 20 20 20 20 70 44 6f 6e 65 20  t ){..    pDone 
17dff 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43  = sqlite3BitvecC
17e00 72 65 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74  reate(pSavepoint
17e01 2d 3e 6e 4f 72 69 67 29 3b 0d 0a 20 20 20 20 69  ->nOrig);..    i
17e02 66 28 20 21 70 44 6f 6e 65 20 29 7b 0d 0a 20 20  f( !pDone ){..  
17e03 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
17e04 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d  E_NOMEM;..    }.
17e05 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 74  .  }....  /* Set
17e06 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69   the database si
17e07 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76  ze back to the v
17e08 61 6c 75 65 20 69 74 20 77 61 73 20 62 65 66 6f  alue it was befo
17e09 72 65 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  re the savepoint
17e0a 20 0d 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72 65   ..  ** being re
17e0b 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 6e 65  verted was opene
17e0c 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 50 61 67  d...  */..  pPag
17e0d 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61  er->dbSize = pSa
17e0e 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70  vepoint ? pSavep
17e0f 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50  oint->nOrig : pP
17e10 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
17e11 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61  ;..  pPager->cha
17e12 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70  ngeCountDone = p
17e13 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b  Pager->tempFile;
17e14 0d 0a 0d 0a 20 20 69 66 28 20 21 70 53 61 76 65  ....  if( !pSave
17e15 70 6f 69 6e 74 20 26 26 20 70 61 67 65 72 55 73  point && pagerUs
17e16 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0d  eWal(pPager) ){.
17e17 0a 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65  .    return page
17e18 72 52 6f 6c 6c 62 61 63 6b 57 61 6c 28 70 50 61  rRollbackWal(pPa
17e19 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ger);..  }....  
17e1a 2f 2a 20 55 73 65 20 70 50 61 67 65 72 2d 3e 6a  /* Use pPager->j
17e1b 6f 75 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 65  ournalOff as the
17e1c 20 65 66 66 65 63 74 69 76 65 20 73 69 7a 65 20   effective size 
17e1d 6f 66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c  of the main roll
17e1e 62 61 63 6b 0d 0a 20 20 2a 2a 20 6a 6f 75 72 6e  back..  ** journ
17e1f 61 6c 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20  al.  The actual 
17e20 66 69 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 61  file might be la
17e21 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 69  rger than this i
17e22 6e 0d 0a 20 20 2a 2a 20 50 41 47 45 52 5f 4a 4f  n..  ** PAGER_JO
17e23 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
17e24 54 45 20 6f 72 20 50 41 47 45 52 5f 4a 4f 55 52  TE or PAGER_JOUR
17e25 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e  NALMODE_PERSIST.
17e26 20 20 42 75 74 20 61 6e 79 74 68 69 6e 67 0d 0a    But anything..
17e27 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67 65 72    ** past pPager
17e28 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 20  ->journalOff is 
17e29 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20 75 73  off-limits to us
17e2a 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 7a 4a 20 3d  ...  */..  szJ =
17e2b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
17e2c 4f 66 66 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  Off;..  assert( 
17e2d 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67  pagerUseWal(pPag
17e2e 65 72 29 3d 3d 30 20 7c 7c 20 73 7a 4a 3d 3d 30  er)==0 || szJ==0
17e2f 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 42 65 67 69   );....  /* Begi
17e30 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63  n by rolling bac
17e31 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74  k records from t
17e32 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
17e33 73 74 61 72 74 69 6e 67 20 61 74 0d 0a 20 20 2a  starting at..  *
17e34 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  * PagerSavepoint
17e35 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e  .iOffset and con
17e36 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 6e  tinuing to the n
17e37 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ext journal head
17e38 65 72 2e 0d 0a 20 20 2a 2a 20 54 68 65 72 65 20  er...  ** There 
17e39 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73  might be records
17e3a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
17e3b 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61  rnal that have a
17e3c 20 70 61 67 65 20 6e 75 6d 62 65 72 0d 0a 20 20   page number..  
17e3d 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ** greater than 
17e3e 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61  the current data
17e3f 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65  base size (pPage
17e40 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74  r->dbSize) but t
17e41 68 6f 73 65 0d 0a 20 20 2a 2a 20 77 69 6c 6c 20  hose..  ** will 
17e42 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d  be skipped autom
17e43 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73  atically.  Pages
17e44 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44   are added to pD
17e45 6f 6e 65 20 61 73 20 74 68 65 79 0d 0a 20 20 2a  one as they..  *
17e46 2a 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63  * are played bac
17e47 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  k...  */..  if( 
17e48 70 53 61 76 65 70 6f 69 6e 74 20 26 26 20 21 70  pSavepoint && !p
17e49 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
17e4a 72 29 20 29 7b 0d 0a 20 20 20 20 69 48 64 72 4f  r) ){..    iHdrO
17e4b 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d  ff = pSavepoint-
17e4c 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53  >iHdrOffset ? pS
17e4d 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66  avepoint->iHdrOf
17e4e 66 73 65 74 20 3a 20 73 7a 4a 3b 0d 0a 20 20 20  fset : szJ;..   
17e4f 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
17e50 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74  Off = pSavepoint
17e51 2d 3e 69 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20  ->iOffset;..    
17e52 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
17e53 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
17e54 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f  journalOff<iHdrO
17e55 66 66 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  ff ){..      rc 
17e56 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
17e57 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72  _one_page(pPager
17e58 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  , &pPager->journ
17e59 61 6c 4f 66 66 2c 20 70 44 6f 6e 65 2c 20 31 2c  alOff, pDone, 1,
17e5a 20 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20   1);..    }..   
17e5b 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
17e5c 49 54 45 5f 44 4f 4e 45 20 29 3b 0d 0a 20 20 7d  ITE_DONE );..  }
17e5d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 50 61 67 65  else{..    pPage
17e5e 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
17e5f 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  0;..  }....  /* 
17e60 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67  Continue rolling
17e61 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75   back records ou
17e62 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  t of the main jo
17e63 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61  urnal starting a
17e64 74 0d 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  t..  ** the firs
17e65 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
17e66 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e   seen and contin
17e67 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65  uing until the e
17e68 66 66 65 63 74 69 76 65 20 65 6e 64 0d 0a 20 20  ffective end..  
17e69 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ** of the main j
17e6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f  ournal file.  Co
17e6b 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f  ntinue to skip o
17e6c 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65  ut-of-range page
17e6d 73 20 61 6e 64 0d 0a 20 20 2a 2a 20 63 6f 6e 74  s and..  ** cont
17e6e 69 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 65  inue adding page
17e6f 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f  s rolled back to
17e70 20 70 44 6f 6e 65 2e 0d 0a 20 20 2a 2f 0d 0a 20   pDone...  */.. 
17e71 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
17e72 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d  TE_OK && pPager-
17e73 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20  >journalOff<szJ 
17e74 29 7b 0d 0a 20 20 20 20 75 33 32 20 69 69 3b 20  ){..    u32 ii; 
17e75 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
17e76 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
17e77 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20 30     u32 nJRec = 0
17e78 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
17e79 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72  of Journal Recor
17e7a 64 73 20 2a 2f 0d 0a 20 20 20 20 75 33 32 20 64  ds */..    u32 d
17e7b 75 6d 6d 79 3b 0d 0a 20 20 20 20 72 63 20 3d 20  ummy;..    rc = 
17e7c 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70  readJournalHdr(p
17e7d 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c 20 26  Pager, 0, szJ, &
17e7e 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29 3b 0d  nJRec, &dummy);.
17e7f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21  .    assert( rc!
17e80 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0d  =SQLITE_DONE );.
17e81 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a  ...    /*..    *
17e82 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a  * The "pPager->j
17e83 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41  ournalHdr+JOURNA
17e84 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
17e85 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ==pPager->journa
17e86 6c 4f 66 66 22 0d 0a 20 20 20 20 2a 2a 20 74 65  lOff"..    ** te
17e87 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f  st is related to
17e88 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20   ticket #2565.  
17e89 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69  See the discussi
17e8a 6f 6e 20 69 6e 20 74 68 65 0d 0a 20 20 20 20 2a  on in the..    *
17e8b 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  * pager_playback
17e8c 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
17e8d 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
17e8e 6d 61 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a 2f 0d  mation...    */.
17e8f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d  .    if( nJRec==
17e90 30 20 0d 0a 20 20 20 20 20 26 26 20 70 50 61 67  0 ..     && pPag
17e91 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
17e92 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
17e93 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
17e94 6f 75 72 6e 61 6c 4f 66 66 0d 0a 20 20 20 20 29  ournalOff..    )
17e95 7b 0d 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d  {..      nJRec =
17e96 20 28 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50   (u32)((szJ - pP
17e97 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
17e98 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28  )/JOURNAL_PG_SZ(
17e99 70 50 61 67 65 72 29 29 3b 0d 0a 20 20 20 20 7d  pPager));..    }
17e9a 0d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ..    for(ii=0; 
17e9b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
17e9c 20 69 69 3c 6e 4a 52 65 63 20 26 26 20 70 50 61   ii<nJRec && pPa
17e9d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c  ger->journalOff<
17e9e 73 7a 4a 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20  szJ; ii++){..   
17e9f 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
17ea0 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
17ea1 70 50 61 67 65 72 2c 20 26 70 50 61 67 65 72 2d  pPager, &pPager-
17ea2 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 44 6f  >journalOff, pDo
17ea3 6e 65 2c 20 31 2c 20 31 29 3b 0d 0a 20 20 20 20  ne, 1, 1);..    
17ea4 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  }..    assert( r
17ea5 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
17ea6 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
17ea7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17ea8 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
17ea9 61 6c 4f 66 66 3e 3d 73 7a 4a 20 29 3b 0d 0a 0d  alOff>=szJ );...
17eaa 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 20  .  /* Finally,  
17eab 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 66  rollback pages f
17eac 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
17ead 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74 20  nal.  Page that 
17eae 77 65 72 65 0d 0a 20 20 2a 2a 20 70 72 65 76 69  were..  ** previ
17eaf 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  ously rolled bac
17eb0 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69  k out of the mai
17eb1 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61  n journal (and a
17eb2 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e  re hence in pDon
17eb3 65 29 0d 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  e)..  ** will be
17eb4 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f   skipped.  Out-o
17eb5 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72  f-range pages ar
17eb6 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0d  e also skipped..
17eb7 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 53 61  .  */..  if( pSa
17eb8 76 65 70 6f 69 6e 74 20 29 7b 0d 0a 20 20 20 20  vepoint ){..    
17eb9 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20  u32 ii;         
17eba 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
17ebb 65 72 20 2a 2f 0d 0a 20 20 20 20 69 36 34 20 6f  er */..    i64 o
17ebc 66 66 73 65 74 20 3d 20 28 69 36 34 29 70 53 61  ffset = (i64)pSa
17ebd 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63  vepoint->iSubRec
17ebe 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65  *(4+pPager->page
17ebf 53 69 7a 65 29 3b 0d 0a 0d 0a 20 20 20 20 69 66  Size);....    if
17ec0 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  ( pagerUseWal(pP
17ec1 61 67 65 72 29 20 29 7b 0d 0a 20 20 20 20 20 20  ager) ){..      
17ec2 72 63 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 53  rc = sqlite3WalS
17ec3 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28 70 50 61  avepointUndo(pPa
17ec4 67 65 72 2d 3e 70 57 61 6c 2c 20 70 53 61 76 65  ger->pWal, pSave
17ec5 70 6f 69 6e 74 2d 3e 61 57 61 6c 44 61 74 61 29  point->aWalData)
17ec6 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 6f  ;..    }..    fo
17ec7 72 28 69 69 3d 70 53 61 76 65 70 6f 69 6e 74 2d  r(ii=pSavepoint-
17ec8 3e 69 53 75 62 52 65 63 3b 20 72 63 3d 3d 53 51  >iSubRec; rc==SQ
17ec9 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 70 50  LITE_OK && ii<pP
17eca 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 20 69  ager->nSubRec; i
17ecb 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  i++){..      ass
17ecc 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 28 69 36  ert( offset==(i6
17ecd 34 29 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e  4)ii*(4+pPager->
17ece 70 61 67 65 53 69 7a 65 29 20 29 3b 0d 0a 20 20  pageSize) );..  
17ecf 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
17ed0 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65  layback_one_page
17ed1 28 70 50 61 67 65 72 2c 20 26 6f 66 66 73 65 74  (pPager, &offset
17ed2 2c 20 70 44 6f 6e 65 2c 20 30 2c 20 31 29 3b 0d  , pDone, 0, 1);.
17ed3 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65  .    }..    asse
17ed4 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
17ed5 4f 4e 45 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ONE );..  }.... 
17ed6 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
17ed7 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0d 0a 20  stroy(pDone);.. 
17ed8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17ed9 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65  OK ){..    pPage
17eda 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
17edb 73 7a 4a 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72  szJ;..  }....  r
17edc 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
17edd 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  /*..** Change th
17ede 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
17edf 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   of in-memory pa
17ee0 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c  ges that are all
17ee1 6f 77 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  owed...*/..SQLIT
17ee2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
17ee3 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61  qlite3PagerSetCa
17ee4 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70  chesize(Pager *p
17ee5 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67  Pager, int mxPag
17ee6 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 50 63  e){..  sqlite3Pc
17ee7 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65  acheSetCachesize
17ee8 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
17ee9 2c 20 6d 78 50 61 67 65 29 3b 0d 0a 7d 0d 0a 0d  , mxPage);..}...
17eea 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 73 20  ./*..** Free as 
17eeb 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 73 20 70  much memory as p
17eec 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20 74 68 65  ossible from the
17eed 20 70 61 67 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c   pager...*/..SQL
17eee 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17eef 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 72   sqlite3PagerShr
17ef0 69 6e 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ink(Pager *pPage
17ef1 72 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 50 63  r){..  sqlite3Pc
17ef2 61 63 68 65 53 68 72 69 6e 6b 28 70 50 61 67 65  acheShrink(pPage
17ef3 72 2d 3e 70 50 43 61 63 68 65 29 3b 0d 0a 7d 0d  r->pPCache);..}.
17ef4 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 6a 75 73 74  .../*..** Adjust
17ef5 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20   the robustness 
17ef6 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
17ef7 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f  to damage due to
17ef8 20 4f 53 20 63 72 61 73 68 65 73 0d 0a 2a 2a 20   OS crashes..** 
17ef9 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65  or power failure
17efa 73 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74 68  s by changing th
17efb 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e 63  e number of sync
17efc 73 28 29 73 20 77 68 65 6e 20 77 72 69 74 69 6e  s()s when writin
17efd 67 0d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61  g..** the rollba
17efe 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65  ck journal.  The
17eff 72 65 20 61 72 65 20 74 68 72 65 65 20 6c 65 76  re are three lev
17f00 65 6c 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  els:..**..**    
17f01 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74 65  OFF       sqlite
17f02 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65 76  3OsSync() is nev
17f03 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73  er called.  This
17f04 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 0d   is the default.
17f05 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
17f06 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61   for temporary a
17f07 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c  nd transient fil
17f08 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4e  es...**..**    N
17f09 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75  ORMAL    The jou
17f0a 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f  rnal is synced o
17f0b 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65  nce before write
17f0c 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0d 0a  s begin on the..
17f0d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
17f0e 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
17f0f 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71  is normally adeq
17f10 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c  uate protection,
17f11 20 62 75 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20   but..**        
17f12 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f        it is theo
17f13 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62  retically possib
17f14 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20  le, though very 
17f15 75 6e 6c 69 6b 65 6c 79 2c 0d 0a 2a 2a 20 20 20  unlikely,..**   
17f16 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20             that 
17f17 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f  an inopertune po
17f18 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c  wer failure coul
17f19 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72  d leave the jour
17f1a 6e 61 6c 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  nal..**         
17f1b 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 20       in a state 
17f1c 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73  which would caus
17f1d 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20  e damage to the 
17f1e 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 20 20 20  database..**    
17f1f 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69            when i
17f20 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t is rolled back
17f21 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 46 55 4c  ...**..**    FUL
17f22 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e  L      The journ
17f23 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69  al is synced twi
17f24 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73  ce before writes
17f25 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0d 0a 2a   begin on the..*
17f26 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  *              d
17f27 61 74 61 62 61 73 65 20 28 77 69 74 68 20 73 6f  atabase (with so
17f28 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  me additional in
17f29 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20  formation - the 
17f2a 6e 52 65 63 20 66 69 65 6c 64 0d 0a 2a 2a 20 20  nRec field..**  
17f2b 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
17f2c 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
17f2d 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65  r - being writte
17f2e 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  n in between the
17f2f 20 74 77 6f 0d 0a 2a 2a 20 20 20 20 20 20 20 20   two..**        
17f30 20 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20 49        syncs).  I
17f31 66 20 77 65 20 61 73 73 75 6d 65 20 74 68 61 74  f we assume that
17f32 20 77 72 69 74 69 6e 67 20 61 0d 0a 2a 2a 20 20   writing a..**  
17f33 20 20 20 20 20 20 20 20 20 20 20 20 73 69 6e 67              sing
17f34 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69  le disk sector i
17f35 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74  s atomic, then t
17f36 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65  his mode provide
17f37 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  s..**           
17f38 20 20 20 61 73 73 75 72 61 6e 63 65 20 74 68 61     assurance tha
17f39 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69  t the journal wi
17f3a 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 75 70  ll not be corrup
17f3b 74 65 64 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 20  ted to the..**  
17f3c 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e              poin
17f3d 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61 6d  t of causing dam
17f3e 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
17f3f 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  ase during rollb
17f40 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ack...**..** The
17f41 20 61 62 6f 76 65 20 69 73 20 66 6f 72 20 61 20   above is for a 
17f42 72 6f 6c 6c 62 61 63 6b 2d 6a 6f 75 72 6e 61 6c  rollback-journal
17f43 20 6d 6f 64 65 2e 20 20 46 6f 72 20 57 41 4c 20   mode.  For WAL 
17f44 6d 6f 64 65 2c 20 4f 46 46 20 63 6f 6e 74 69 6e  mode, OFF contin
17f45 75 65 73 0d 0a 2a 2a 20 74 6f 20 6d 65 61 6e 20  ues..** to mean 
17f46 74 68 61 74 20 6e 6f 20 73 79 6e 63 73 20 65 76  that no syncs ev
17f47 65 72 20 6f 63 63 75 72 2e 20 20 4e 4f 52 4d 41  er occur.  NORMA
17f48 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  L means that the
17f49 20 57 41 4c 20 69 73 20 73 79 6e 63 65 64 0d 0a   WAL is synced..
17f4a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ** prior to the 
17f4b 73 74 61 72 74 20 6f 66 20 63 68 65 63 6b 70 6f  start of checkpo
17f4c 69 6e 74 20 61 6e 64 20 74 68 61 74 20 74 68 65  int and that the
17f4d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
17f4e 73 20 73 79 6e 63 65 64 0d 0a 2a 2a 20 61 74 20  s synced..** at 
17f4f 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
17f50 66 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74  f the checkpoint
17f51 20 69 66 20 74 68 65 20 65 6e 74 69 72 65 20 63   if the entire c
17f52 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 57 41  ontent of the WA
17f53 4c 0d 0a 2a 2a 20 77 61 73 20 77 72 69 74 74 65  L..** was writte
17f54 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  n back into the 
17f55 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 6e  database.  But n
17f56 6f 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e  o sync operation
17f57 73 20 6f 63 63 75 72 20 66 6f 72 0d 0a 2a 2a 20  s occur for..** 
17f58 61 6e 20 6f 72 64 69 6e 61 72 79 20 63 6f 6d 6d  an ordinary comm
17f59 69 74 20 69 6e 20 4e 4f 52 4d 41 4c 20 6d 6f 64  it in NORMAL mod
17f5a 65 20 77 69 74 68 20 57 41 4c 2e 20 20 46 55 4c  e with WAL.  FUL
17f5b 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  L means that the
17f5c 20 57 41 4c 0d 0a 2a 2a 20 66 69 6c 65 20 69 73   WAL..** file is
17f5d 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e   synced followin
17f5e 67 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 6f 70  g each commit op
17f5f 65 72 61 74 69 6f 6e 2c 20 69 6e 20 61 64 64 69  eration, in addi
17f60 74 69 6f 6e 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  tion to the..** 
17f61 73 79 6e 63 73 20 61 73 73 6f 63 69 61 74 65 64  syncs associated
17f62 20 77 69 74 68 20 4e 4f 52 4d 41 4c 2e 0d 0a 2a   with NORMAL...*
17f63 2a 0d 0a 2a 2a 20 44 6f 20 6e 6f 74 20 63 6f 6e  *..** Do not con
17f64 66 75 73 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73  fuse synchronous
17f65 3d 46 55 4c 4c 20 77 69 74 68 20 53 51 4c 49 54  =FULL with SQLIT
17f66 45 5f 53 59 4e 43 5f 46 55 4c 4c 2e 20 20 54 68  E_SYNC_FULL.  Th
17f67 65 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 53 59 4e  e..** SQLITE_SYN
17f68 43 5f 46 55 4c 4c 20 6d 61 63 72 6f 20 6d 65 61  C_FULL macro mea
17f69 6e 73 20 74 6f 20 75 73 65 20 74 68 65 20 4d 61  ns to use the Ma
17f6a 63 4f 53 58 2d 73 74 79 6c 65 20 66 75 6c 6c 2d  cOSX-style full-
17f6b 66 73 79 6e 63 0d 0a 2a 2a 20 75 73 69 6e 67 20  fsync..** using 
17f6c 66 63 6e 74 6c 28 46 5f 46 55 4c 4c 46 53 59 4e  fcntl(F_FULLFSYN
17f6d 43 29 2e 20 20 53 51 4c 49 54 45 5f 53 59 4e 43  C).  SQLITE_SYNC
17f6e 5f 4e 4f 52 4d 41 4c 20 6d 65 61 6e 73 20 74 6f  _NORMAL means to
17f6f 20 64 6f 20 61 6e 0d 0a 2a 2a 20 6f 72 64 69 6e   do an..** ordin
17f70 61 72 79 20 66 73 79 6e 63 28 29 20 63 61 6c 6c  ary fsync() call
17f71 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 64  .  There is no d
17f72 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
17f73 6e 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  n SQLITE_SYNC_FU
17f74 4c 4c 0d 0a 2a 2a 20 61 6e 64 20 53 51 4c 49 54  LL..** and SQLIT
17f75 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 6e  E_SYNC_NORMAL on
17f76 20 70 6c 61 74 66 6f 72 6d 73 20 6f 74 68 65 72   platforms other
17f77 20 74 68 61 6e 20 4d 61 63 4f 53 58 2e 20 20 42   than MacOSX.  B
17f78 75 74 20 74 68 65 0d 0a 2a 2a 20 73 79 6e 63 68  ut the..** synch
17f79 72 6f 6e 6f 75 73 3d 46 55 4c 4c 20 76 65 72 73  ronous=FULL vers
17f7a 75 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4e  us synchronous=N
17f7b 4f 52 4d 41 4c 20 73 65 74 74 69 6e 67 20 64 65  ORMAL setting de
17f7c 74 65 72 6d 69 6e 65 73 20 77 68 65 6e 0d 0a 2a  termines when..*
17f7d 2a 20 74 68 65 20 78 53 79 6e 63 20 70 72 69 6d  * the xSync prim
17f7e 69 74 69 76 65 20 69 73 20 63 61 6c 6c 65 64 20  itive is called 
17f7f 61 6e 64 20 69 73 20 72 65 6c 65 76 61 6e 74 20  and is relevant 
17f80 74 6f 20 61 6c 6c 20 70 6c 61 74 66 6f 72 6d 73  to all platforms
17f81 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 75 6d 65 72 69  ...**..** Numeri
17f82 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69 61  c values associa
17f83 74 65 64 20 77 69 74 68 20 74 68 65 73 65 20 73  ted with these s
17f84 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d 3d 31  tates are OFF==1
17f85 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0d 0a 2a 2a 20  , NORMAL=2,..** 
17f86 61 6e 64 20 46 55 4c 4c 3d 33 2e 0d 0a 2a 2f 0d  and FULL=3...*/.
17f87 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17f88 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
17f89 41 53 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  AS..SQLITE_PRIVA
17f8a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
17f8b 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76  agerSetSafetyLev
17f8c 65 6c 28 0d 0a 20 20 50 61 67 65 72 20 2a 70 50  el(..  Pager *pP
17f8d 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  ager,        /* 
17f8e 54 68 65 20 70 61 67 65 72 20 74 6f 20 73 65 74  The pager to set
17f8f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 66 6f   safety level fo
17f90 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c 65 76 65  r */..  int leve
17f91 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
17f92 20 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e   PRAGMA synchron
17f93 6f 75 73 2e 20 20 31 3d 4f 46 46 2c 20 32 3d 4e  ous.  1=OFF, 2=N
17f94 4f 52 4d 41 4c 2c 20 33 3d 46 55 4c 4c 20 2a 2f  ORMAL, 3=FULL */
17f95 20 20 0d 0a 20 20 69 6e 74 20 62 46 75 6c 6c 46    ..  int bFullF
17f96 73 79 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 50  sync,       /* P
17f97 52 41 47 4d 41 20 66 75 6c 6c 66 73 79 6e 63 20  RAGMA fullfsync 
17f98 2a 2f 0d 0a 20 20 69 6e 74 20 62 43 6b 70 74 46  */..  int bCkptF
17f99 75 6c 6c 46 73 79 6e 63 20 20 20 20 2f 2a 20 50  ullFsync    /* P
17f9a 52 41 47 4d 41 20 63 68 65 63 6b 70 6f 69 6e 74  RAGMA checkpoint
17f9b 5f 66 75 6c 6c 66 73 79 6e 63 20 2a 2f 0d 0a 29  _fullfsync */..)
17f9c 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 6c 65 76  {..  assert( lev
17f9d 65 6c 3e 3d 31 20 26 26 20 6c 65 76 65 6c 3c 3d  el>=1 && level<=
17f9e 33 20 29 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e  3 );..  pPager->
17f9f 6e 6f 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c  noSync =  (level
17fa0 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74  ==1 || pPager->t
17fa1 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0d 0a  empFile) ?1:0;..
17fa2 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79    pPager->fullSy
17fa3 6e 63 20 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26  nc = (level==3 &
17fa4 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  & !pPager->tempF
17fa5 69 6c 65 29 20 3f 31 3a 30 3b 0d 0a 20 20 69 66  ile) ?1:0;..  if
17fa6 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  ( pPager->noSync
17fa7 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d   ){..    pPager-
17fa8 3e 73 79 6e 63 46 6c 61 67 73 20 3d 20 30 3b 0d  >syncFlags = 0;.
17fa9 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b 70  .    pPager->ckp
17faa 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 30 3b 0d  tSyncFlags = 0;.
17fab 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 62 46 75  .  }else if( bFu
17fac 6c 6c 46 73 79 6e 63 20 29 7b 0d 0a 20 20 20 20  llFsync ){..    
17fad 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67  pPager->syncFlag
17fae 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s = SQLITE_SYNC_
17faf 46 55 4c 4c 3b 0d 0a 20 20 20 20 70 50 61 67 65  FULL;..    pPage
17fb0 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73  r->ckptSyncFlags
17fb1 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46   = SQLITE_SYNC_F
17fb2 55 4c 4c 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  ULL;..  }else if
17fb3 28 20 62 43 6b 70 74 46 75 6c 6c 46 73 79 6e 63  ( bCkptFullFsync
17fb4 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d   ){..    pPager-
17fb5 3e 73 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c  >syncFlags = SQL
17fb6 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b  ITE_SYNC_NORMAL;
17fb7 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b  ..    pPager->ck
17fb8 70 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51  ptSyncFlags = SQ
17fb9 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0d  LITE_SYNC_FULL;.
17fba 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
17fbb 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73  Pager->syncFlags
17fbc 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e   = SQLITE_SYNC_N
17fbd 4f 52 4d 41 4c 3b 0d 0a 20 20 20 20 70 50 61 67  ORMAL;..    pPag
17fbe 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67  er->ckptSyncFlag
17fbf 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s = SQLITE_SYNC_
17fc0 4e 4f 52 4d 41 4c 3b 0d 0a 20 20 7d 0d 0a 20 20  NORMAL;..  }..  
17fc1 70 50 61 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46  pPager->walSyncF
17fc2 6c 61 67 73 20 3d 20 70 50 61 67 65 72 2d 3e 73  lags = pPager->s
17fc3 79 6e 63 46 6c 61 67 73 3b 0d 0a 20 20 69 66 28  yncFlags;..  if(
17fc4 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
17fc5 63 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65 72  c ){..    pPager
17fc6 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67 73 20 7c  ->walSyncFlags |
17fc7 3d 20 57 41 4c 5f 53 59 4e 43 5f 54 52 41 4e 53  = WAL_SYNC_TRANS
17fc8 41 43 54 49 4f 4e 53 3b 0d 0a 20 20 7d 0d 0a 7d  ACTIONS;..  }..}
17fc9 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
17fca 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
17fcb 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
17fcc 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
17fcd 77 68 65 6e 65 76 65 72 20 74 68 65 20 6c 69 62  whenever the lib
17fce 72 61 72 79 0d 0a 2a 2a 20 61 74 74 65 6d 70 74  rary..** attempt
17fcf 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70  s to open a temp
17fd0 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 54 68 69  orary file.  Thi
17fd1 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  s information is
17fd2 20 75 73 65 64 20 66 6f 72 0d 0a 2a 2a 20 74 65   used for..** te
17fd3 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
17fd4 69 73 20 6f 6e 6c 79 2e 20 20 0d 0a 2a 2f 0d 0a  is only.  ..*/..
17fd5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
17fd6 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ST..SQLITE_API i
17fd7 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74  nt sqlite3_opent
17fd8 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a  emp_count = 0;..
17fd9 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
17fda 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72   Open a temporar
17fdb 79 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  y file...**..** 
17fdc 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64  Write the file d
17fdd 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a  escriptor into *
17fde 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51  pFile. Return SQ
17fdf 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
17fe0 73 73 20 0d 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  ss ..** or some 
17fe1 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
17fe2 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65   if we fail. The
17fe3 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74   OS will automat
17fe4 69 63 61 6c 6c 79 20 0d 0a 2a 2a 20 64 65 6c 65  ically ..** dele
17fe5 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  te the temporary
17fe6 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73   file when it is
17fe7 20 63 6c 6f 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a   closed...**..**
17fe8 20 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 65   The flags passe
17fe9 64 20 74 6f 20 74 68 65 20 56 46 53 20 6c 61 79  d to the VFS lay
17fea 65 72 20 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20  er xOpen() call 
17feb 61 72 65 20 74 68 6f 73 65 20 73 70 65 63 69 66  are those specif
17fec 69 65 64 0d 0a 2a 2a 20 62 79 20 70 61 72 61 6d  ied..** by param
17fed 65 74 65 72 20 76 66 73 46 6c 61 67 73 20 4f 52  eter vfsFlags OR
17fee 65 64 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c  ed with the foll
17fef 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  owing:..**..**  
17ff0 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
17ff1 45 41 44 57 52 49 54 45 0d 0a 2a 2a 20 20 20 20  EADWRITE..**    
17ff2 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
17ff3 41 54 45 0d 0a 2a 2a 20 20 20 20 20 53 51 4c 49  ATE..**     SQLI
17ff4 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
17ff5 45 0d 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  E..**     SQLITE
17ff6 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
17ff7 4f 53 45 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  OSE..*/..static 
17ff8 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d  int pagerOpentem
17ff9 70 28 0d 0a 20 20 50 61 67 65 72 20 2a 70 50 61  p(..  Pager *pPa
17ffa 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  ger,        /* T
17ffb 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20  he pager object 
17ffc 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  */..  sqlite3_fi
17ffd 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57  le *pFile,  /* W
17ffe 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65  rite the file de
17fff 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f  scriptor here */
18000 0d 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  ..  int vfsFlags
18001 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
18002 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  gs passed throug
18003 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0d  h to the VFS */.
18004 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20  .){..  int rc;  
18005 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18006 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
18007 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
18008 54 45 53 54 0d 0a 20 20 73 71 6c 69 74 65 33 5f  TEST..  sqlite3_
18009 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b  opentemp_count++
1800a 3b 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 74  ;  /* Used for t
1800b 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79  esting and analy
1800c 73 69 73 20 6f 6e 6c 79 20 2a 2f 0d 0a 23 65 6e  sis only */..#en
1800d 64 69 66 0d 0a 0d 0a 20 20 76 66 73 46 6c 61 67  dif....  vfsFlag
1800e 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 45  s |=  SQLITE_OPE
1800f 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51  N_READWRITE | SQ
18010 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
18011 20 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   |..            
18012 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
18013 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f  USIVE | SQLITE_O
18014 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
18015 45 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  E;..  rc = sqlit
18016 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  e3OsOpen(pPager-
18017 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c  >pVfs, 0, pFile,
18018 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0d 0a   vfsFlags, 0);..
18019 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
1801a 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65  LITE_OK || isOpe
1801b 6e 28 70 46 69 6c 65 29 20 29 3b 0d 0a 20 20 72  n(pFile) );..  r
1801c 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1801d 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 62  /*..** Set the b
1801e 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63  usy handler func
1801f 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  tion...**..** Th
18020 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20  e pager invokes 
18021 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  the busy-handler
18022 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63   if sqlite3OsLoc
18023 6b 28 29 20 72 65 74 75 72 6e 73 20 0d 0a 2a 2a  k() returns ..**
18024 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65   SQLITE_BUSY whe
18025 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72  n trying to upgr
18026 61 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b  ade from no-lock
18027 20 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63   to a SHARED loc
18028 6b 2c 0d 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74  k,..** or when t
18029 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65  rying to upgrade
1802a 20 66 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44   from a RESERVED
1802b 20 6c 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c   lock to an EXCL
1802c 55 53 49 56 45 20 0d 0a 2a 2a 20 6c 6f 63 6b 2e  USIVE ..** lock.
1802d 20 49 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69   It does *not* i
1802e 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
1802f 61 6e 64 6c 65 72 20 77 68 65 6e 20 75 70 67 72  andler when upgr
18030 61 64 69 6e 67 20 66 72 6f 6d 0d 0a 2a 2a 20 53  ading from..** S
18031 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 45  HARED to RESERVE
18032 44 2c 20 6f 72 20 77 68 65 6e 20 75 70 67 72 61  D, or when upgra
18033 64 69 6e 67 20 66 72 6f 6d 20 53 48 41 52 45 44  ding from SHARED
18034 20 74 6f 20 45 58 43 4c 55 53 49 56 45 0d 0a 2a   to EXCLUSIVE..*
18035 2a 20 28 77 68 69 63 68 20 6f 63 63 75 72 73 20  * (which occurs 
18036 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e  during hot-journ
18037 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 53 75  al rollback). Su
18038 6d 6d 61 72 79 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  mmary:..**..**  
18039 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 20   Transition     
1803a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1803b 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 75     | Invokes xBu
1803c 73 79 48 61 6e 64 6c 65 72 0d 0a 2a 2a 20 20 20  syHandler..**   
1803d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1803e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1803f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18040 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20 20 20 4e  --------..**   N
18041 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20  O_LOCK       -> 
18042 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
18043 20 7c 20 59 65 73 0d 0a 2a 2a 20 20 20 53 48 41   | Yes..**   SHA
18044 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45  RED_LOCK   -> RE
18045 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c  SERVED_LOCK    |
18046 20 4e 6f 0d 0a 2a 2a 20 20 20 53 48 41 52 45 44   No..**   SHARED
18047 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55  _LOCK   -> EXCLU
18048 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f  SIVE_LOCK   | No
18049 0d 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 5f  ..**   RESERVED_
1804a 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56  LOCK -> EXCLUSIV
1804b 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0d 0a  E_LOCK   | Yes..
1804c 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 62 75  **..** If the bu
1804d 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  sy-handler callb
1804e 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
1804f 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69  zero, the lock i
18050 73 20 0d 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20  s ..** retried. 
18051 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65  If it returns ze
18052 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c  ro, then the SQL
18053 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69  ITE_BUSY error i
18054 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74  s..** returned t
18055 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20  o the caller of 
18056 74 68 65 20 70 61 67 65 72 20 41 50 49 20 66 75  the pager API fu
18057 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c  nction...*/..SQL
18058 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18059 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1805a 42 75 73 79 68 61 6e 64 6c 65 72 28 0d 0a 20 20  Busyhandler(..  
1805b 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20  Pager *pPager,  
1805c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1805d 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62       /* Pager ob
1805e 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 28  ject */..  int (
1805f 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76  *xBusyHandler)(v
18060 6f 69 64 20 2a 29 2c 20 20 20 20 20 20 20 20 20  oid *),         
18061 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75  /* Pointer to bu
18062 73 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74  sy-handler funct
18063 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a  ion */..  void *
18064 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20  pBusyHandlerArg 
18065 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18066 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 61  * Argument to pa
18067 73 73 20 74 6f 20 78 42 75 73 79 48 61 6e 64 6c  ss to xBusyHandl
18068 65 72 20 2a 2f 0d 0a 29 7b 20 20 0d 0a 20 20 70  er */..){  ..  p
18069 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
1806a 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64 6c  ler = xBusyHandl
1806b 65 72 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 70  er;..  pPager->p
1806c 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d  BusyHandlerArg =
1806d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67   pBusyHandlerArg
1806e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
1806f 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73  hange the page s
18070 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ize used by the 
18071 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68  Pager object. Th
18072 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20  e new page size 
18073 0d 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 69  ..** is passed i
18074 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0d 0a 2a  n *pPageSize...*
18075 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  *..** If the pag
18076 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72  er is in the err
18077 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  or state when th
18078 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
18079 61 6c 6c 65 64 2c 20 69 74 0d 0a 2a 2a 20 69 73  alled, it..** is
1807a 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61   a no-op. The va
1807b 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
1807c 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
1807d 65 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e  error code (i.e.
1807e 20 0d 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c   ..** one of SQL
1807f 49 54 45 5f 49 4f 45 52 52 2c 20 61 6e 20 53 51  ITE_IOERR, an SQ
18080 4c 49 54 45 5f 49 4f 45 52 52 5f 78 78 78 20 73  LITE_IOERR_xxx s
18081 75 62 2d 63 6f 64 65 20 6f 72 20 53 51 4c 49 54  ub-code or SQLIT
18082 45 5f 46 55 4c 4c 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  E_FULL)...**..**
18083 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
18084 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
18085 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0d 0a 2a  ing are true:..*
18086 2a 0d 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65  *..**   * the ne
18087 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c  w page size (val
18088 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65  ue of *pPageSize
18089 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f  ) is valid (a po
1808a 77 65 72 20 0d 0a 2a 2a 20 20 20 20 20 6f 66 20  wer ..**     of 
1808b 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20  two between 512 
1808c 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  and SQLITE_MAX_P
1808d 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73  AGE_SIZE, inclus
1808e 69 76 65 29 2c 20 61 6e 64 0d 0a 2a 2a 0d 0a 2a  ive), and..**..*
1808f 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 20  *   * there are 
18090 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  no outstanding p
18091 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20  age references, 
18092 61 6e 64 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20  and..**..**   * 
18093 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
18094 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e  either not an in
18095 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
18096 20 6f 72 20 69 74 20 69 73 0d 0a 2a 2a 20 20 20   or it is..**   
18097 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64    an in-memory d
18098 61 74 61 62 61 73 65 20 74 68 61 74 20 63 75 72  atabase that cur
18099 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20  rently consists 
1809a 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0d 0a  of zero pages...
1809b 2a 2a 0d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  **..** then the 
1809c 70 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 67  pager object pag
1809d 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 6f  e size is set to
1809e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0d 0a 2a 2a   *pPageSize...**
1809f 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  ..** If the page
180a0 20 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65 64   size is changed
180a1 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
180a2 74 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74 65  tion uses sqlite
180a3 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0d  3PagerMalloc() .
180a4 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  .** to obtain a 
180a5 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70  new Pager.pTmpSp
180a6 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 20 74  ace buffer. If t
180a7 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  his allocation a
180a8 74 74 65 6d 70 74 20 0d 0a 2a 2a 20 66 61 69 6c  ttempt ..** fail
180a9 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  s, SQLITE_NOMEM 
180aa 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
180ab 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 65  the page size re
180ac 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e  mains unchanged.
180ad 20 0d 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68   ..** In all oth
180ae 65 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45  er cases, SQLITE
180af 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
180b0 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
180b1 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74  page size is not
180b2 20 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65 72   changed, either
180b3 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 20   because one of 
180b4 74 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0d 0a  the enumerated..
180b5 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62  ** conditions ab
180b6 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c  ove is not true,
180b7 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69   the pager was i
180b8 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68  n error state wh
180b9 65 6e 20 74 68 69 73 0d 0a 2a 2a 20 66 75 6e 63  en this..** func
180ba 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c  tion was called,
180bb 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20   or because the 
180bc 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
180bd 6e 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 64  n attempt failed
180be 2c 20 0d 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61  , ..** then *pPa
180bf 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f  geSize is set to
180c0 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e   the old, retain
180c1 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 65 66  ed page size bef
180c2 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  ore returning...
180c3 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
180c4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
180c5 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50  gerSetPagesize(P
180c6 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 33  ager *pPager, u3
180c7 32 20 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e  2 *pPageSize, in
180c8 74 20 6e 52 65 73 65 72 76 65 29 7b 0d 0a 20 20  t nReserve){..  
180c9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
180ca 4f 4b 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 74 20 69  OK;....  /* It i
180cb 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
180cc 6f 20 64 6f 20 61 20 66 75 6c 6c 20 61 73 73 65  o do a full asse
180cd 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 29  rt_pager_state()
180ce 20 68 65 72 65 2c 20 61 73 20 74 68 69 73 0d 0a   here, as this..
180cf 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 61    ** function ma
180d0 79 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  y be called from
180d1 20 77 69 74 68 69 6e 20 50 61 67 65 72 4f 70 65   within PagerOpe
180d2 6e 28 29 2c 20 62 65 66 6f 72 65 20 74 68 65 20  n(), before the 
180d3 73 74 61 74 65 0d 0a 20 20 2a 2a 20 6f 66 20 74  state..  ** of t
180d4 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  he Pager object 
180d5 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f  is internally co
180d6 6e 73 69 73 74 65 6e 74 2e 0d 0a 20 20 2a 2a 0d  nsistent...  **.
180d7 0a 20 20 2a 2a 20 41 74 20 6f 6e 65 20 70 6f 69  .  ** At one poi
180d8 6e 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nt this function
180d9 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72   returned an err
180da 6f 72 20 69 66 20 74 68 65 20 70 61 67 65 72 20  or if the pager 
180db 77 61 73 20 69 6e 20 0d 0a 20 20 2a 2a 20 50 41  was in ..  ** PA
180dc 47 45 52 5f 45 52 52 4f 52 20 73 74 61 74 65 2e  GER_ERROR state.
180dd 20 42 75 74 20 73 69 6e 63 65 20 50 41 47 45 52   But since PAGER
180de 5f 45 52 52 4f 52 20 73 74 61 74 65 20 67 75 61  _ERROR state gua
180df 72 61 6e 74 65 65 73 20 74 68 61 74 0d 0a 20 20  rantees that..  
180e0 2a 2a 20 74 68 65 72 65 20 69 73 20 61 74 20 6c  ** there is at l
180e1 65 61 73 74 20 6f 6e 65 20 6f 75 74 73 74 61 6e  east one outstan
180e2 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65  ding page refere
180e3 6e 63 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69  nce, this functi
180e4 6f 6e 0d 0a 20 20 2a 2a 20 69 73 20 61 20 6e 6f  on..  ** is a no
180e5 2d 6f 70 20 66 6f 72 20 74 68 61 74 20 63 61 73  -op for that cas
180e6 65 20 61 6e 79 68 6f 77 2e 0d 0a 20 20 2a 2f 0d  e anyhow...  */.
180e7 0a 0d 0a 20 20 75 33 32 20 70 61 67 65 53 69 7a  ...  u32 pageSiz
180e8 65 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0d  e = *pPageSize;.
180e9 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65 53  .  assert( pageS
180ea 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53  ize==0 || (pageS
180eb 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65  ize>=512 && page
180ec 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58  Size<=SQLITE_MAX
180ed 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0d 0a  _PAGE_SIZE) );..
180ee 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d    if( (pPager->m
180ef 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65  emDb==0 || pPage
180f0 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0d 0a 20  r->dbSize==0).. 
180f1 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63    && sqlite3Pcac
180f2 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65  heRefCount(pPage
180f3 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0d  r->pPCache)==0 .
180f4 0a 20 20 20 26 26 20 70 61 67 65 53 69 7a 65 20  .   && pageSize 
180f5 26 26 20 70 61 67 65 53 69 7a 65 21 3d 28 75 33  && pageSize!=(u3
180f6 32 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  2)pPager->pageSi
180f7 7a 65 20 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 63  ze ..  ){..    c
180f8 68 61 72 20 2a 70 4e 65 77 20 3d 20 4e 55 4c 4c  har *pNew = NULL
180f9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
180fa 20 4e 65 77 20 74 65 6d 70 20 73 70 61 63 65 20   New temp space 
180fb 2a 2f 0d 0a 20 20 20 20 69 36 34 20 6e 42 79 74  */..    i64 nByt
180fc 65 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 69 66  e = 0;....    if
180fd 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
180fe 3e 50 41 47 45 52 5f 4f 50 45 4e 20 26 26 20 69  >PAGER_OPEN && i
180ff 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
18100 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  ) ){..      rc =
18101 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
18102 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26  ze(pPager->fd, &
18103 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a  nByte);..    }..
18104 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
18105 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
18106 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73  pNew = (char *)s
18107 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
18108 28 70 61 67 65 53 69 7a 65 29 3b 0d 0a 20 20 20  (pageSize);..   
18109 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 72     if( !pNew ) r
1810a 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1810b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
1810c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1810d 4b 20 29 7b 0d 0a 20 20 20 20 20 20 70 61 67 65  K ){..      page
1810e 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b  r_reset(pPager);
1810f 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ..      pPager->
18110 64 62 53 69 7a 65 20 3d 20 28 50 67 6e 6f 29 28  dbSize = (Pgno)(
18111 28 6e 42 79 74 65 2b 70 61 67 65 53 69 7a 65 2d  (nByte+pageSize-
18112 31 29 2f 70 61 67 65 53 69 7a 65 29 3b 0d 0a 20  1)/pageSize);.. 
18113 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67       pPager->pag
18114 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
18115 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
18116 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d  PageFree(pPager-
18117 3e 70 54 6d 70 53 70 61 63 65 29 3b 0d 0a 20 20  >pTmpSpace);..  
18118 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70      pPager->pTmp
18119 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0d 0a 20  Space = pNew;.. 
1811a 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
1811b 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50  heSetPageSize(pP
1811c 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
1811d 61 67 65 53 69 7a 65 29 3b 0d 0a 20 20 20 20 7d  ageSize);..    }
1811e 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 50 61 67  ..  }....  *pPag
1811f 65 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  eSize = pPager->
18120 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20 69 66 28  pageSize;..  if(
18121 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
18122 7b 0d 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65  {..    if( nRese
18123 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65  rve<0 ) nReserve
18124 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65   = pPager->nRese
18125 72 76 65 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  rve;..    assert
18126 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26  ( nReserve>=0 &&
18127 20 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29   nReserve<1000 )
18128 3b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  ;..    pPager->n
18129 52 65 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e  Reserve = (i16)n
1812a 52 65 73 65 72 76 65 3b 0d 0a 20 20 20 20 70 61  Reserve;..    pa
1812b 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50  gerReportSize(pP
1812c 61 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  ager);..  }..  r
1812d 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1812e 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /*..** Return a 
1812f 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22  pointer to the "
18130 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20  temporary page" 
18131 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65  buffer held inte
18132 72 6e 61 6c 6c 79 0d 0a 2a 2a 20 62 79 20 74 68  rnally..** by th
18133 65 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69  e pager.  This i
18134 73 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20  s a buffer that 
18135 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  is big enough to
18136 20 68 6f 6c 64 20 74 68 65 0d 0a 2a 2a 20 65 6e   hold the..** en
18137 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20  tire content of 
18138 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e  a database page.
18139 20 20 54 68 69 73 20 62 75 66 66 65 72 20 69 73    This buffer is
1813a 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   used internally
1813b 0d 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c  ..** during roll
1813c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65  back and will be
1813d 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65   overwritten whe
1813e 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b  never a rollback
1813f 0d 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75  ..** occurs.  Bu
18140 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20  t other modules 
18141 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 20  are free to use 
18142 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20  it too, as long 
18143 61 73 0d 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61  as..** no rollba
18144 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e  cks are happenin
18145 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  g...*/..SQLITE_P
18146 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
18147 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61  ite3PagerTempSpa
18148 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ce(Pager *pPager
18149 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61  ){..  return pPa
1814a 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0d  ger->pTmpSpace;.
1814b 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 74  .}..../*..** Att
1814c 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20  empt to set the 
1814d 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65  maximum database
1814e 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d   page count if m
1814f 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76  xPage is positiv
18150 65 2e 20 0d 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20  e. ..** Make no 
18151 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67  changes if mxPag
18152 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67  e is zero or neg
18153 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65  ative.  And neve
18154 72 20 72 65 64 75 63 65 20 74 68 65 0d 0a 2a 2a  r reduce the..**
18155 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f   maximum page co
18156 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75  unt below the cu
18157 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68  rrent size of th
18158 65 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2a 0d  e database...**.
18159 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
1815a 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e  f mxPage, return
1815b 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78   the current max
1815c 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e  imum page count.
1815d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1815e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1815f 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e  PagerMaxPageCoun
18160 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
18161 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0d 0a 20   int mxPage){.. 
18162 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b   if( mxPage>0 ){
18163 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78  ..    pPager->mx
18164 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0d 0a  Pgno = mxPage;..
18165 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
18166 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50  Pager->eState!=P
18167 41 47 45 52 5f 4f 50 45 4e 20 29 3b 20 20 20 20  AGER_OPEN );    
18168 20 20 2f 2a 20 43 61 6c 6c 65 64 20 6f 6e 6c 79    /* Called only
18169 20 62 79 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20   by OP_MaxPgcnt 
1816a 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
1816b 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3e 3d 70 50  ager->mxPgno>=pP
1816c 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 20  ager->dbSize ); 
1816d 20 2f 2a 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20   /* OP_MaxPgcnt 
1816e 65 6e 66 6f 72 63 65 73 20 74 68 69 73 20 2a 2f  enforces this */
1816f 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  ..  return pPage
18170 72 2d 3e 6d 78 50 67 6e 6f 3b 0d 0a 7d 0d 0a 0d  r->mxPgno;..}...
18171 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ./*..** The foll
18172 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75  owing set of rou
18173 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74  tines are used t
18174 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69  o disable the si
18175 6d 75 6c 61 74 65 64 0d 0a 2a 2a 20 49 2f 4f 20  mulated..** I/O 
18176 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e  error mechanism.
18177 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
18178 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f   are used to avo
18179 69 64 20 73 69 6d 75 6c 61 74 65 64 0d 0a 2a 2a  id simulated..**
1817a 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65   errors in place
1817b 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f  s where we do no
1817c 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72  t care about err
1817d 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 55 6e 6c  ors...**..** Unl
1817e 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53  ess -DSQLITE_TES
1817f 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74 68 65  T=1 is used, the
18180 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
18181 61 6c 6c 20 6e 6f 2d 6f 70 73 0d 0a 2a 2a 20 61  all no-ops..** a
18182 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 63  nd generate no c
18183 6f 64 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  ode...*/..#ifdef
18184 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 53 51   SQLITE_TEST..SQ
18185 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20  LITE_API extern 
18186 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
18187 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0d 0a 53  rror_pending;..S
18188 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e  QLITE_API extern
18189 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
1818a 65 72 72 6f 72 5f 68 69 74 3b 0d 0a 73 74 61 74  error_hit;..stat
1818b 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74  ic int saved_cnt
1818c 3b 0d 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f  ;..void disable_
1818d 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
1818e 6f 72 73 28 76 6f 69 64 29 7b 0d 0a 20 20 73 61  ors(void){..  sa
1818f 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65  ved_cnt = sqlite
18190 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
18191 6e 67 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69  ng;..  sqlite3_i
18192 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
18193 3d 20 2d 31 3b 0d 0a 7d 0d 0a 76 6f 69 64 20 65  = -1;..}..void e
18194 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
18195 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b  io_errors(void){
18196 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ..  sqlite3_io_e
18197 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73  rror_pending = s
18198 61 76 65 64 5f 63 6e 74 3b 0d 0a 7d 0d 0a 23 65  aved_cnt;..}..#e
18199 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 64 69  lse..# define di
1819a 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
1819b 69 6f 5f 65 72 72 6f 72 73 28 29 0d 0a 23 20 64  io_errors()..# d
1819c 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d  efine enable_sim
1819d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
1819e 28 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  ()..#endif..../*
1819f 0d 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69  ..** Read the fi
181a0 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d  rst N bytes from
181a1 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
181a2 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20  f the file into 
181a3 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 74 68 61 74 20  memory..** that 
181a4 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e  pDest points to.
181a5 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65   ..**..** If the
181a6 20 70 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65   pager was opene
181a7 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74  d on a transient
181a8 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65   file (zFilename
181a9 3d 3d 22 22 29 2c 20 6f 72 0d 0a 2a 2a 20 6f 70  ==""), or..** op
181aa 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c  ened on a file l
181ab 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73  ess than N bytes
181ac 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f 75   in size, the ou
181ad 74 70 75 74 20 62 75 66 66 65 72 20 69 73 0d 0a  tput buffer is..
181ae 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51  ** zeroed and SQ
181af 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
181b0 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20  . The rationale 
181b1 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74  for this is that
181b2 20 74 68 69 73 20 0d 0a 2a 2a 20 66 75 6e 63 74   this ..** funct
181b3 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
181b4 65 61 64 20 64 61 74 61 62 61 73 65 20 68 65 61  ead database hea
181b5 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 20  ders, and a new 
181b6 74 72 61 6e 73 69 65 6e 74 20 6f 72 0d 0a 2a 2a  transient or..**
181b7 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61   zero sized data
181b8 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64 65  base has a heade
181b9 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20  r than consists 
181ba 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f  entirely of zero
181bb 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  es...**..** If a
181bc 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72  ny IO error apar
181bd 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f  t from SQLITE_IO
181be 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69  ERR_SHORT_READ i
181bf 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0d 0a  s encountered,..
181c0 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ** the error cod
181c1 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f  e is returned to
181c2 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20   the caller and 
181c3 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
181c4 74 68 65 0d 0a 2a 2a 20 6f 75 74 70 75 74 20 62  the..** output b
181c5 75 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e  uffer undefined.
181c6 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
181c7 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
181c8 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61  PagerReadFilehea
181c9 64 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  der(Pager *pPage
181ca 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e  r, int N, unsign
181cb 65 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b  ed char *pDest){
181cc 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
181cd 49 54 45 5f 4f 4b 3b 0d 0a 20 20 6d 65 6d 73 65  ITE_OK;..  memse
181ce 74 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0d  t(pDest, 0, N);.
181cf 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
181d0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c  n(pPager->fd) ||
181d1 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
181d2 65 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69  e );....  /* Thi
181d3 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c  s routine is onl
181d4 79 20 63 61 6c 6c 65 64 20 62 79 20 62 74 72 65  y called by btre
181d5 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  e immediately af
181d6 74 65 72 20 63 72 65 61 74 69 6e 67 0d 0a 20 20  ter creating..  
181d7 2a 2a 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  ** the Pager obj
181d8 65 63 74 2e 20 20 54 68 65 72 65 20 68 61 73 20  ect.  There has 
181d9 6e 6f 74 20 62 65 65 6e 20 61 6e 20 6f 70 70 6f  not been an oppo
181da 72 74 75 6e 69 74 79 20 74 6f 20 74 72 61 6e 73  rtunity to trans
181db 69 74 69 6f 6e 0d 0a 20 20 2a 2a 20 74 6f 20 57  ition..  ** to W
181dc 41 4c 20 6d 6f 64 65 20 79 65 74 2e 0d 0a 20 20  AL mode yet...  
181dd 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 21 70  */..  assert( !p
181de 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
181df 72 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 69  r) );....  if( i
181e0 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
181e1 29 20 29 7b 0d 0a 20 20 20 20 49 4f 54 52 41 43  ) ){..    IOTRAC
181e2 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25  E(("DBHDR %p 0 %
181e3 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29  d\n", pPager, N)
181e4 29 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  )..    rc = sqli
181e5 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
181e6 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20  ->fd, pDest, N, 
181e7 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  0);..    if( rc=
181e8 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
181e9 4f 52 54 5f 52 45 41 44 20 29 7b 0d 0a 20 20 20  ORT_READ ){..   
181ea 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
181eb 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  K;..    }..  }..
181ec 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
181ed 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
181ee 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79  unction may only
181ef 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   be called when 
181f0 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
181f1 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 0d 0a 2a  on is open on..*
181f2 2a 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20  * the pager. It 
181f3 72 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61  returns the tota
181f4 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
181f5 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
181f6 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 48 6f 77 65 76  e...**..** Howev
181f7 65 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65 20  er, if the file 
181f8 69 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  is between 1 and
181f9 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79 74   <page-size> byt
181fa 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e  es in size, then
181fb 20 0d 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f   ..** this is co
181fc 6e 73 69 64 65 72 65 64 20 61 20 31 20 70 61 67  nsidered a 1 pag
181fd 65 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  e file...*/..SQL
181fe 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
181ff 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
18200 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50  ecount(Pager *pP
18201 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67  ager, int *pnPag
18202 65 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  e){..  assert( p
18203 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50  Pager->eState>=P
18204 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0d 0a  AGER_READER );..
18205 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
18206 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f  ->eState!=PAGER_
18207 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20  WRITER_FINISHED 
18208 29 3b 0d 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20  );..  *pnPage = 
18209 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 64 62 53  (int)pPager->dbS
1820a 69 7a 65 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  ize;..}....../*.
1820b 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69  .** Try to obtai
1820c 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65  n a lock of type
1820d 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 65   locktype on the
1820e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1820f 49 66 0d 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72  If..** a similar
18210 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
18211 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64   is already held
18212 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
18213 69 73 20 61 20 6e 6f 2d 6f 70 0d 0a 2a 2a 20 28  is a no-op..** (
18214 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45  returning SQLITE
18215 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29  _OK immediately)
18216 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77  ...**..** Otherw
18217 69 73 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20  ise, attempt to 
18218 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20  obtain the lock 
18219 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c  using sqlite3OsL
1821a 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0d 0a  ock(). Invoke ..
1821b 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ** the busy call
1821c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b  back if the lock
1821d 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f   is currently no
1821e 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 70  t available. Rep
1821f 65 61 74 20 0d 0a 2a 2a 20 75 6e 74 69 6c 20 74  eat ..** until t
18220 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
18221 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 6f   returns false o
18222 72 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 65  r until the atte
18223 6d 70 74 20 74 6f 20 0d 0a 2a 2a 20 6f 62 74 61  mpt to ..** obta
18224 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63  in the lock succ
18225 65 65 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65  eeds...**..** Re
18226 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
18227 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e  n success and an
18228 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77   error code if w
18229 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0d  e cannot obtain.
1822a 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66  .** the lock. If
1822b 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74   the lock is obt
1822c 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  ained successful
1822d 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65  ly, set the Page
1822e 72 2e 73 74 61 74 65 20 0d 0a 2a 2a 20 76 61 72  r.state ..** var
1822f 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70  iable to locktyp
18230 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
18231 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ng...*/..static 
18232 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f  int pager_wait_o
18233 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  n_lock(Pager *pP
18234 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79  ager, int lockty
18235 70 65 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20  pe){..  int rc; 
18236 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18237 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18238 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
18239 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
1823a 74 20 74 68 69 73 20 69 73 20 65 69 74 68 65 72  t this is either
1823b 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73   a no-op (becaus
1823c 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  e the requested 
1823d 6c 6f 63 6b 20 69 73 20 0d 0a 20 20 2a 2a 20 61  lock is ..  ** a
1823e 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 20  lready held, or 
1823f 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73  one of the trans
18240 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68 65  istions that the
18241 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0d 0a 20   busy-handler.. 
18242 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b   ** may be invok
18243 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72  ed during, accor
18244 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d  ding to the comm
18245 65 6e 74 20 61 62 6f 76 65 0d 0a 20 20 2a 2a 20  ent above..  ** 
18246 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42  sqlite3PagerSetB
18247 75 73 79 68 61 6e 64 6c 65 72 28 29 2e 0d 0a 20  usyhandler()... 
18248 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28   */..  assert( (
18249 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 6c  pPager->eLock>=l
1824a 6f 63 6b 74 79 70 65 29 0d 0a 20 20 20 20 20 20  ocktype)..      
1824b 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f   || (pPager->eLo
1824c 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 6c  ck==NO_LOCK && l
1824d 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
1824e 4c 4f 43 4b 29 0d 0a 20 20 20 20 20 20 20 7c 7c  LOCK)..       ||
1824f 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d   (pPager->eLock=
18250 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26  =RESERVED_LOCK &
18251 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  & locktype==EXCL
18252 55 53 49 56 45 5f 4c 4f 43 4b 29 0d 0a 20 20 29  USIVE_LOCK)..  )
18253 3b 0d 0a 0d 0a 20 20 64 6f 20 7b 0d 0a 20 20 20  ;....  do {..   
18254 20 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44   rc = pagerLockD
18255 62 28 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79  b(pPager, lockty
18256 70 65 29 3b 0d 0a 20 20 7d 77 68 69 6c 65 28 20  pe);..  }while( 
18257 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
18258 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79  && pPager->xBusy
18259 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d 3e  Handler(pPager->
1825a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 29  pBusyHandlerArg)
1825b 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63   );..  return rc
1825c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46  ;..}..../*..** F
1825d 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54 72  unction assertTr
1825e 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  uncateConstraint
1825f 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73 20  (pPager) checks 
18260 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  that one of the 
18261 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69  ..** following i
18262 73 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64  s true for all d
18263 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65  irty pages curre
18264 6e 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65  ntly in the page
18265 2d 63 61 63 68 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  -cache:..**..** 
18266 20 20 61 29 20 54 68 65 20 70 61 67 65 20 6e 75    a) The page nu
18267 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 68 61  mber is less tha
18268 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
18269 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0d 0a  e size of the ..
1826a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e 74 20  **      current 
1826b 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20  database image, 
1826c 69 6e 20 70 61 67 65 73 2c 20 4f 52 0d 0a 2a 2a  in pages, OR..**
1826d 0d 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65  ..**   b) if the
1826e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65   page content we
1826f 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68  re written at th
18270 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c  is time, it woul
18271 64 20 6e 6f 74 0d 0a 2a 2a 20 20 20 20 20 20 62  d not..**      b
18272 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77  e necessary to w
18273 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
18274 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20   content out to 
18275 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0d  the sub-journal.
18276 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65 74  .**      (as det
18277 65 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74  ermined by funct
18278 69 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73  ion subjRequires
18279 50 61 67 65 28 29 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  Page())...**..**
1827a 20 49 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f   If the conditio
1827b 6e 20 61 73 73 65 72 74 65 64 20 62 79 20 74 68  n asserted by th
1827c 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 65 72 65  is function were
1827d 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 74   not true, and t
1827e 68 65 0d 0a 2a 2a 20 64 69 72 74 79 20 70 61 67  he..** dirty pag
1827f 65 20 77 65 72 65 20 74 6f 20 62 65 20 64 69 73  e were to be dis
18280 63 61 72 64 65 64 20 66 72 6f 6d 20 74 68 65 20  carded from the 
18281 63 61 63 68 65 20 76 69 61 20 74 68 65 20 70 61  cache via the pa
18282 67 65 72 53 74 72 65 73 73 28 29 0d 0a 2a 2a 20  gerStress()..** 
18283 72 6f 75 74 69 6e 65 2c 20 70 61 67 65 72 53 74  routine, pagerSt
18284 72 65 73 73 28 29 20 77 6f 75 6c 64 20 6e 6f 74  ress() would not
18285 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65   write the curre
18286 6e 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  nt page content 
18287 74 6f 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  to..** the datab
18288 61 73 65 20 66 69 6c 65 2e 20 49 66 20 61 20 73  ase file. If a s
18289 61 76 65 70 6f 69 6e 74 20 74 72 61 6e 73 61 63  avepoint transac
1828a 74 69 6f 6e 20 77 65 72 65 20 72 6f 6c 6c 65 64  tion were rolled
1828b 20 62 61 63 6b 20 61 66 74 65 72 0d 0a 2a 2a 20   back after..** 
1828c 74 68 69 73 20 68 61 70 70 65 6e 65 64 2c 20 74  this happened, t
1828d 68 65 20 63 6f 72 72 65 63 74 20 62 65 68 61 76  he correct behav
1828e 69 6f 75 72 20 77 6f 75 6c 64 20 62 65 20 74 6f  iour would be to
1828f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 75 72   restore the cur
18290 72 65 6e 74 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74  rent..** content
18291 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 48 6f   of the page. Ho
18292 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68 69  wever, since thi
18293 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74  s content is not
18294 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69 74 68   present in eith
18295 65 72 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  er..** the datab
18296 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65 20  ase file or the 
18297 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  portion of the r
18298 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
18299 61 6e 64 20 0d 0a 2a 2a 20 73 75 62 2d 6a 6f 75  and ..** sub-jou
1829a 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b  rnal rolled back
1829b 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75   the content cou
1829c 6c 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72  ld not be restor
1829d 65 64 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 64  ed and the..** d
1829e 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f  atabase image wo
1829f 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75  uld become corru
182a0 70 74 2e 20 49 74 20 69 73 20 74 68 65 72 65 66  pt. It is theref
182a1 6f 72 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68  ore fortunate th
182a2 61 74 20 0d 0a 2a 2a 20 74 68 69 73 20 63 69 72  at ..** this cir
182a3 63 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74  cumstance cannot
182a4 20 61 72 69 73 65 2e 0d 0a 2a 2f 0d 0a 23 69 66   arise...*/..#if
182a5 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
182a6 44 45 42 55 47 29 0d 0a 73 74 61 74 69 63 20 76  DEBUG)..static v
182a7 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61  oid assertTrunca
182a8 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50  teConstraintCb(P
182a9 67 48 64 72 20 2a 70 50 67 29 7b 0d 0a 20 20 61  gHdr *pPg){..  a
182aa 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67  ssert( pPg->flag
182ab 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b  s&PGHDR_DIRTY );
182ac 0d 0a 20 20 61 73 73 65 72 74 28 20 21 73 75 62  ..  assert( !sub
182ad 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50  jRequiresPage(pP
182ae 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c  g) || pPg->pgno<
182af 3d 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62  =pPg->pPager->db
182b0 53 69 7a 65 20 29 3b 0d 0a 7d 0d 0a 73 74 61 74  Size );..}..stat
182b1 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 54 72  ic void assertTr
182b2 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  uncateConstraint
182b3 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
182b4 0d 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ..  sqlite3Pcach
182b5 65 49 74 65 72 61 74 65 44 69 72 74 79 28 70 50  eIterateDirty(pP
182b6 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 61  ager->pPCache, a
182b7 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e  ssertTruncateCon
182b8 73 74 72 61 69 6e 74 43 62 29 3b 0d 0a 7d 0d 0a  straintCb);..}..
182b9 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
182ba 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f  assertTruncateCo
182bb 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 29  nstraint(pPager)
182bc 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
182bd 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
182be 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
182bf 73 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f  se file image to
182c0 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 68   nPage pages. Th
182c1 69 73 20 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  is ..** function
182c2 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c   does not actual
182c3 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61  ly modify the da
182c4 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64  tabase file on d
182c5 69 73 6b 2e 20 49 74 20 0d 0a 2a 2a 20 6a 75 73  isk. It ..** jus
182c6 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 72  t sets the inter
182c7 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65  nal state of the
182c8 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73 6f   pager object so
182c9 20 74 68 61 74 20 74 68 65 20 0d 0a 2a 2a 20 74   that the ..** t
182ca 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62  runcation will b
182cb 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20  e done when the 
182cc 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
182cd 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
182ce 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
182cf 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
182d0 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
182d1 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67  mage(Pager *pPag
182d2 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b  er, Pgno nPage){
182d3 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
182d4 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67  er->dbSize>=nPag
182d5 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  e );..  assert( 
182d6 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d  pPager->eState>=
182d7 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43  PAGER_WRITER_CAC
182d8 48 45 4d 4f 44 20 29 3b 0d 0a 20 20 70 50 61 67  HEMOD );..  pPag
182d9 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61  er->dbSize = nPa
182da 67 65 3b 0d 0a 20 20 61 73 73 65 72 74 54 72 75  ge;..  assertTru
182db 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28  ncateConstraint(
182dc 70 50 61 67 65 72 29 3b 0d 0a 7d 0d 0a 0d 0a 0d  pPager);..}.....
182dd 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
182de 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
182df 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e  before attemptin
182e0 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  g a hot-journal 
182e1 72 6f 6c 6c 62 61 63 6b 2e 20 49 74 0d 0a 2a 2a  rollback. It..**
182e2 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75 72 6e   syncs the journ
182e3 61 6c 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2c  al file to disk,
182e4 20 74 68 65 6e 20 73 65 74 73 20 70 50 61 67 65   then sets pPage
182e5 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f  r->journalHdr to
182e6 20 74 68 65 0d 0a 2a 2a 20 73 69 7a 65 20 6f 66   the..** size of
182e7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
182e8 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61  e so that the pa
182e9 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72  ger_playback() r
182ea 6f 75 74 69 6e 65 20 6b 6e 6f 77 73 0d 0a 2a 2a  outine knows..**
182eb 20 74 68 61 74 20 74 68 65 20 65 6e 74 69 72 65   that the entire
182ec 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
182ed 73 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0d 0a  s been synced...
182ee 2a 2a 0d 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 61  **..** Syncing a
182ef 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20   hot-journal to 
182f0 64 69 73 6b 20 62 65 66 6f 72 65 20 61 74 74 65  disk before atte
182f1 6d 70 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 69  mpting to roll i
182f2 74 20 62 61 63 6b 20 65 6e 73 75 72 65 73 20 0d  t back ensures .
182f3 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20 70 6f  .** that if a po
182f4 77 65 72 2d 66 61 69 6c 75 72 65 20 6f 63 63 75  wer-failure occu
182f5 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f  rs during the ro
182f6 6c 6c 62 61 63 6b 2c 20 74 68 65 20 70 72 6f 63  llback, the proc
182f7 65 73 73 20 74 68 61 74 0d 0a 2a 2a 20 61 74 74  ess that..** att
182f8 65 6d 70 74 73 20 72 6f 6c 6c 62 61 63 6b 20 66  empts rollback f
182f9 6f 6c 6c 6f 77 69 6e 67 20 73 79 73 74 65 6d 20  ollowing system 
182fa 72 65 63 6f 76 65 72 79 20 73 65 65 73 20 74 68  recovery sees th
182fb 65 20 73 61 6d 65 20 6a 6f 75 72 6e 61 6c 0d 0a  e same journal..
182fc 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 73 20 74 68  ** content as th
182fd 69 73 20 70 72 6f 63 65 73 73 2e 0d 0a 2a 2a 0d  is process...**.
182fe 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e  .** If everythin
182ff 67 20 67 6f 65 73 20 61 73 20 70 6c 61 6e 6e 65  g goes as planne
18300 64 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  d, SQLITE_OK is 
18301 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
18302 69 73 65 2c 20 0d 0a 2a 2a 20 61 6e 20 53 51 4c  ise, ..** an SQL
18303 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d  ite error code..
18304 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
18305 70 61 67 65 72 53 79 6e 63 48 6f 74 4a 6f 75 72  pagerSyncHotJour
18306 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
18307 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  r){..  int rc = 
18308 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66  SQLITE_OK;..  if
18309 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  ( !pPager->noSyn
1830a 63 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73  c ){..    rc = s
1830b 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61  qlite3OsSync(pPa
1830c 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45  ger->jfd, SQLITE
1830d 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0d 0a  _SYNC_NORMAL);..
1830e 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
1830f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
18310 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
18311 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e  ileSize(pPager->
18312 6a 66 64 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f  jfd, &pPager->jo
18313 75 72 6e 61 6c 48 64 72 29 3b 0d 0a 20 20 7d 0d  urnalHdr);..  }.
18314 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
18315 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 68 75 74 64  ..../*..** Shutd
18316 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63  own the page cac
18317 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65  he.  Free all me
18318 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61  mory and close a
18319 6c 6c 20 66 69 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a  ll files...**..*
1831a 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  * If a transacti
1831b 6f 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65  on was in progre
1831c 73 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ss when this rou
1831d 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
1831e 74 68 61 74 0d 0a 2a 2a 20 74 72 61 6e 73 61 63  that..** transac
1831f 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62  tion is rolled b
18320 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61  ack.  All outsta
18321 6e 64 69 6e 67 20 70 61 67 65 73 20 61 72 65 20  nding pages are 
18322 69 6e 76 61 6c 69 64 61 74 65 64 0d 0a 2a 2a 20  invalidated..** 
18323 61 6e 64 20 74 68 65 69 72 20 6d 65 6d 6f 72 79  and their memory
18324 20 69 73 20 66 72 65 65 64 2e 20 20 41 6e 79 20   is freed.  Any 
18325 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61  attempt to use a
18326 20 70 61 67 65 20 61 73 73 6f 63 69 61 74 65 64   page associated
18327 0d 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 20 70  ..** with this p
18328 61 67 65 20 63 61 63 68 65 20 61 66 74 65 72 20  age cache after 
18329 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
1832a 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  turns will likel
1832b 79 0d 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20  y..** result in 
1832c 61 20 63 6f 72 65 64 75 6d 70 2e 0d 0a 2a 2a 0d  a coredump...**.
1832d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1832e 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64  n always succeed
1832f 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  s. If a transact
18330 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e  ion is active an
18331 20 61 74 74 65 6d 70 74 0d 0a 2a 2a 20 69 73 20   attempt..** is 
18332 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20  made to roll it 
18333 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f  back. If an erro
18334 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
18335 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0d 0a 2a  the rollback ..*
18336 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  * a hot journal 
18337 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74  may be left in t
18338 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75  he filesystem bu
18339 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65  t no error is re
1833a 74 75 72 6e 65 64 0d 0a 2a 2a 20 74 6f 20 74 68  turned..** to th
1833b 65 20 63 61 6c 6c 65 72 2e 0d 0a 2a 2f 0d 0a 53  e caller...*/..S
1833c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1833d 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c  t sqlite3PagerCl
1833e 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ose(Pager *pPage
1833f 72 29 7b 0d 0a 20 20 75 38 20 2a 70 54 6d 70 20  r){..  u8 *pTmp 
18340 3d 20 28 75 38 20 2a 29 70 50 61 67 65 72 2d 3e  = (u8 *)pPager->
18341 70 54 6d 70 53 70 61 63 65 3b 0d 0a 0d 0a 20 20  pTmpSpace;....  
18342 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70  assert( assert_p
18343 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65  ager_state(pPage
18344 72 29 20 29 3b 0d 0a 20 20 64 69 73 61 62 6c 65  r) );..  disable
18345 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
18346 72 6f 72 73 28 29 3b 0d 0a 20 20 73 71 6c 69 74  rors();..  sqlit
18347 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
18348 6c 6f 63 28 29 3b 0d 0a 20 20 2f 2a 20 70 50 61  loc();..  /* pPa
18349 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 30  ger->errCode = 0
1834a 3b 20 2a 2f 0d 0a 20 20 70 50 61 67 65 72 2d 3e  ; */..  pPager->
1834b 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20  exclusiveMode = 
1834c 30 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0;..#ifndef SQLI
1834d 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 73  TE_OMIT_WAL..  s
1834e 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 70  qlite3WalClose(p
1834f 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 50 61  Pager->pWal, pPa
18350 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61  ger->ckptSyncFla
18351 67 73 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  gs, pPager->page
18352 53 69 7a 65 2c 20 70 54 6d 70 29 3b 0d 0a 20 20  Size, pTmp);..  
18353 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20 30  pPager->pWal = 0
18354 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70 61 67  ;..#endif..  pag
18355 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
18356 3b 0d 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  ;..  if( MEMDB )
18357 7b 0d 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c  {..    pager_unl
18358 6f 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20  ock(pPager);..  
18359 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49  }else{..    /* I
1835a 66 20 69 74 20 69 73 20 6f 70 65 6e 2c 20 73 79  f it is open, sy
1835b 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
1835c 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ile before calli
1835d 6e 67 20 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  ng UnlockAndRoll
1835e 62 61 63 6b 2e 0d 0a 20 20 20 20 2a 2a 20 49 66  back...    ** If
1835f 20 74 68 69 73 20 69 73 20 6e 6f 74 20 64 6f 6e   this is not don
18360 65 2c 20 74 68 65 6e 20 61 6e 20 75 6e 73 79 6e  e, then an unsyn
18361 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  ced portion of t
18362 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20  he open journal 
18363 0d 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 6d 61  ..    ** file ma
18364 79 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  y be played back
18365 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
18366 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66  se. If a power f
18367 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 0d 0a  ailure occurs ..
18368 20 20 20 20 2a 2a 20 77 68 69 6c 65 20 74 68 69      ** while thi
18369 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20  s is happening, 
1836a 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75  the database cou
1836b 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70  ld become corrup
1836c 74 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  t...    **..    
1836d 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
1836e 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69  ccurs while tryi
1836f 6e 67 20 74 6f 20 73 79 6e 63 20 74 68 65 20 6a  ng to sync the j
18370 6f 75 72 6e 61 6c 2c 20 73 68 69 66 74 20 74 68  ournal, shift th
18371 65 20 70 61 67 65 72 0d 0a 20 20 20 20 2a 2a 20  e pager..    ** 
18372 69 6e 74 6f 20 74 68 65 20 45 52 52 4f 52 20 73  into the ERROR s
18373 74 61 74 65 2e 20 54 68 69 73 20 63 61 75 73 65  tate. This cause
18374 73 20 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  s UnlockAndRollb
18375 61 63 6b 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68  ack to unlock th
18376 65 0d 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  e..    ** databa
18377 73 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65  se and close the
18378 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 69   journal file wi
18379 74 68 6f 75 74 20 61 74 74 65 6d 70 74 69 6e 67  thout attempting
1837a 20 74 6f 20 72 6f 6c 6c 20 69 74 0d 0a 20 20 20   to roll it..   
1837b 20 2a 2a 20 62 61 63 6b 20 6f 72 20 66 69 6e 61   ** back or fina
1837c 6c 69 7a 65 20 69 74 2e 20 54 68 65 20 6e 65 78  lize it. The nex
1837d 74 20 64 61 74 61 62 61 73 65 20 75 73 65 72 20  t database user 
1837e 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20  will have to do 
1837f 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0d 0a 20 20 20  hot-journal..   
18380 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62 65 66   ** rollback bef
18381 6f 72 65 20 61 63 63 65 73 73 69 6e 67 20 74 68  ore accessing th
18382 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
18383 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
18384 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
18385 3e 6a 66 64 29 20 29 7b 0d 0a 20 20 20 20 20 20  >jfd) ){..      
18386 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
18387 65 72 2c 20 70 61 67 65 72 53 79 6e 63 48 6f 74  er, pagerSyncHot
18388 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 29  Journal(pPager))
18389 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 61  ;..    }..    pa
1838a 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
1838b 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20  back(pPager);.. 
1838c 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64   }..  sqlite3End
1838d 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d  BenignMalloc();.
1838e 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  .  enable_simula
1838f 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
18390 0d 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ..  PAGERTRACE((
18391 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41  "CLOSE %d\n", PA
18392 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
18393 0d 0a 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c  ..  IOTRACE(("CL
18394 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  OSE %p\n", pPage
18395 72 29 29 0d 0a 20 20 73 71 6c 69 74 65 33 4f 73  r))..  sqlite3Os
18396 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
18397 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 4f 73  d);..  sqlite3Os
18398 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64  Close(pPager->fd
18399 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 50 61 67  );..  sqlite3Pag
1839a 65 46 72 65 65 28 70 54 6d 70 29 3b 0d 0a 20 20  eFree(pTmp);..  
1839b 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f  sqlite3PcacheClo
1839c 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  se(pPager->pPCac
1839d 68 65 29 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53  he);....#ifdef S
1839e 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0d  QLITE_HAS_CODEC.
1839f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78  .  if( pPager->x
183a0 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61 67  CodecFree ) pPag
183a1 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70  er->xCodecFree(p
183a2 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0d  Pager->pCodec);.
183a3 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 61 73 73  .#endif....  ass
183a4 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53  ert( !pPager->aS
183a5 61 76 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61  avepoint && !pPa
183a6 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
183a7 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 21 69  );..  assert( !i
183a8 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
183a9 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50  d) && !isOpen(pP
183aa 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 3b 0d 0a  ager->sjfd) );..
183ab 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
183ac 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 72 65 74  (pPager);..  ret
183ad 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
183ae 7d 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65  }....#if !define
183af 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
183b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
183b1 29 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  )../*..** Return
183b2 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
183b3 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0d 0a   for page pPg...
183b4 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
183b5 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50  TE Pgno sqlite3P
183b6 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44  agerPagenumber(D
183b7 62 50 61 67 65 20 2a 70 50 67 29 7b 0d 0a 20 20  bPage *pPg){..  
183b8 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f  return pPg->pgno
183b9 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
183ba 2f 2a 0d 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74  /*..** Increment
183bb 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
183bc 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50  ount for page pP
183bd 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  g...*/..SQLITE_P
183be 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
183bf 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61  te3PagerRef(DbPa
183c0 67 65 20 2a 70 50 67 29 7b 0d 0a 20 20 73 71 6c  ge *pPg){..  sql
183c1 69 74 65 33 50 63 61 63 68 65 52 65 66 28 70 50  ite3PcacheRef(pP
183c2 67 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  g);..}..../*..**
183c3 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   Sync the journa
183c4 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  l. In other word
183c5 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c  s, make sure all
183c6 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20   the pages that 
183c7 68 61 76 65 0d 0a 2a 2a 20 62 65 65 6e 20 77 72  have..** been wr
183c8 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
183c9 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c  rnal have actual
183ca 6c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 73  ly reached the s
183cb 75 72 66 61 63 65 20 6f 66 20 74 68 65 0d 0a 2a  urface of the..*
183cc 2a 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62  * disk and can b
183cd 65 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68  e restored in th
183ce 65 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74  e event of a hot
183cf 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
183d0 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  k...**..** If th
183d1 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66  e Pager.noSync f
183d2 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
183d3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
183d4 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 20 4f  s a no-op...** O
183d5 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63  therwise, the ac
183d6 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64  tions required d
183d7 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75  epend on the jou
183d8 72 6e 61 6c 2d 6d 6f 64 65 20 61 6e 64 20 74 68  rnal-mode and th
183d9 65 20 0d 0a 2a 2a 20 64 65 76 69 63 65 20 63 68  e ..** device ch
183da 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66  aracteristics of
183db 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79   the the file-sy
183dc 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73  stem, as follows
183dd 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 49 66  :..**..**   * If
183de 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
183df 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  e is an in-memor
183e0 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  y journal file, 
183e1 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64 0d 0a  no action need..
183e2 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65 6e 2e  **     be taken.
183e3 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 4f 74 68  ..**..**   * Oth
183e4 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64  erwise, if the d
183e5 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73  evice does not s
183e6 75 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f  upport the SAFE_
183e7 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c  APPEND property,
183e8 0d 0a 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68  ..**     then th
183e9 65 20 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20  e nRec field of 
183ea 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
183eb 79 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61  y written journa
183ec 6c 20 68 65 61 64 65 72 0d 0a 2a 2a 20 20 20 20  l header..**    
183ed 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 63   is updated to c
183ee 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65  ontain the numbe
183ef 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63  r of journal rec
183f0 6f 72 64 73 20 74 68 61 74 20 68 61 76 65 0d 0a  ords that have..
183f1 2a 2a 20 20 20 20 20 62 65 65 6e 20 77 72 69 74  **     been writ
183f2 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 74  ten following it
183f3 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  . If the pager i
183f4 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 66  s operating in f
183f5 75 6c 6c 2d 73 79 6e 63 0d 0a 2a 2a 20 20 20 20  ull-sync..**    
183f6 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20   mode, then the 
183f7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
183f8 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20 74 68  synced before th
183f9 69 73 20 66 69 65 6c 64 20 69 73 20 75 70 64 61  is field is upda
183fa 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a  ted...**..**   *
183fb 20 49 66 20 74 68 65 20 64 65 76 69 63 65 20 64   If the device d
183fc 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
183fd 74 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70  the SEQUENTIAL p
183fe 72 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0d 0a  roperty, then ..
183ff 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66  **     journal f
18400 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e 0d 0a  ile is synced...
18401 2a 2a 0d 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73  **..** Or, in ps
18402 65 75 64 6f 2d 63 6f 64 65 3a 0d 0a 2a 2a 0d 0a  eudo-code:..**..
18403 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e  **   if( NOT <in
18404 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e  -memory journal>
18405 20 29 7b 0d 0a 2a 2a 20 20 20 20 20 69 66 28 20   ){..**     if( 
18406 4e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20  NOT SAFE_APPEND 
18407 29 7b 0d 0a 2a 2a 20 20 20 20 20 20 20 69 66 28  ){..**       if(
18408 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65   <full-sync mode
18409 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e  > ) xSync(<journ
1840a 61 6c 20 66 69 6c 65 3e 29 3b 0d 0a 2a 2a 20 20  al file>);..**  
1840b 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e 52 65       <update nRe
1840c 63 20 66 69 65 6c 64 3e 0d 0a 2a 2a 20 20 20 20  c field>..**    
1840d 20 7d 20 0d 0a 2a 2a 20 20 20 20 20 69 66 28 20   } ..**     if( 
1840e 4e 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29  NOT SEQUENTIAL )
1840f 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20   xSync(<journal 
18410 66 69 6c 65 3e 29 3b 0d 0a 2a 2a 20 20 20 7d 0d  file>);..**   }.
18411 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63 63 65  .**..** If succe
18412 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f 75 74  ssful, this rout
18413 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20 50  ine clears the P
18414 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
18415 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0d 0a 2a  lag of every ..*
18416 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79  * page currently
18417 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20   held in memory 
18418 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
18419 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61   SQLITE_OK. If a
1841a 6e 20 49 4f 0d 0a 2a 2a 20 65 72 72 6f 72 20 69  n IO..** error i
1841b 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  s encountered, t
1841c 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
1841d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
1841e 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
1841f 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
18420 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67   syncJournal(Pag
18421 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
18422 6e 65 77 48 64 72 29 7b 0d 0a 20 20 69 6e 74 20  newHdr){..  int 
18423 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
18424 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
18425 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d  eturn code */...
18426 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
18427 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
18428 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44  _WRITER_CACHEMOD
18429 0d 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67  ..       || pPag
1842a 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45  er->eState==PAGE
1842b 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 0d 0a  R_WRITER_DBMOD..
1842c 20 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20    );..  assert( 
1842d 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
1842e 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20  te(pPager) );.. 
1842f 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55   assert( !pagerU
18430 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b  seWal(pPager) );
18431 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ....  rc = sqlit
18432 65 33 50 61 67 65 72 45 78 63 6c 75 73 69 76 65  e3PagerExclusive
18433 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20  Lock(pPager);.. 
18434 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18435 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d  OK ) return rc;.
18436 0a 0d 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  ...  if( !pPager
18437 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0d 0a 20 20 20  ->noSync ){..   
18438 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
18439 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0d 0a 20  ->tempFile );.. 
1843a 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
1843b 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50  ager->jfd) && pP
1843c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
1843d 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
1843e 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0d 0a  MODE_MEMORY ){..
1843f 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
18440 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44  iDc = sqlite3OsD
18441 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
18442 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
18443 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
18444 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
18445 6a 66 64 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 20  jfd) );....     
18446 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
18447 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
18448 50 50 45 4e 44 29 20 29 7b 0d 0a 20 20 20 20 20  PPEND) ){..     
18449 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b     /* This block
1844a 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20 6f   deals with an o
1844b 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20  bscure problem. 
1844c 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e  If the last conn
1844d 65 63 74 69 6f 6e 0d 0a 20 20 20 20 20 20 20 20  ection..        
1844e 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f  ** that wrote to
1844f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 77   this database w
18450 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20  as operating in 
18451 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e  persistent-journ
18452 61 6c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d  al..        ** m
18453 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ode, then the jo
18454 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61  urnal file may a
18455 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74  t this point act
18456 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0d  ually be larger.
18457 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e  .        ** than
18458 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
18459 66 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 20  f bytes. If the 
1845a 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68  next thing in th
1845b 65 20 6a 6f 75 72 6e 61 6c 0d 0a 20 20 20 20 20  e journal..     
1845c 20 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65     ** file happe
1845d 6e 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e  ns to be a journ
1845e 61 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74 74  al-header (writt
1845f 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  en as part of th
18460 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72  e..        ** pr
18461 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f  evious connectio
18462 6e 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 29  n's transaction)
18463 2c 20 61 6e 64 20 61 20 63 72 61 73 68 20 6f 72  , and a crash or
18464 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 0d   power-failure .
18465 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75  .        ** occu
18466 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 69 73  rs after nRec is
18467 20 75 70 64 61 74 65 64 20 62 75 74 20 62 65 66   updated but bef
18468 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74  ore this connect
18469 69 6f 6e 20 77 72 69 74 65 73 20 0d 0a 20 20 20  ion writes ..   
1846a 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67       ** anything
1846b 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f 75   else to the jou
1846c 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f  rnal file (or co
1846d 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b  mmits/rolls back
1846e 20 69 74 73 20 0d 0a 20 20 20 20 20 20 20 20 2a   its ..        *
1846f 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20  * transaction), 
18470 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20  then SQLite may 
18471 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20  become confused 
18472 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0d  when doing the .
18473 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d  .        ** hot-
18474 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b  journal rollback
18475 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76   following recov
18476 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c  ery. It may roll
18477 20 62 61 63 6b 20 61 6c 6c 0d 0a 20 20 20 20 20   back all..     
18478 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f     ** of this co
18479 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20  nnections data, 
1847a 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20  then proceed to 
1847b 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65  rolling back the
1847c 20 6f 6c 64 2c 0d 0a 20 20 20 20 20 20 20 20 2a   old,..        *
1847d 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61  * out-of-date da
1847e 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  ta that follows 
1847f 69 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72  it. Database cor
18480 72 75 70 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20  ruption...      
18481 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a    **..        **
18482 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
18483 74 68 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75  this, if the jou
18484 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61  rnal file does a
18485 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e  ppear to contain
18486 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 76  ..        ** a v
18487 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f 6c 6c  alid header foll
18488 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72  owing Pager.jour
18489 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 72 69  nalOff, then wri
1848a 74 65 20 61 20 30 78 30 30 0d 0a 20 20 20 20 20  te a 0x00..     
1848b 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 68     ** byte to th
1848c 65 20 73 74 61 72 74 20 6f 66 20 69 74 20 74 6f  e start of it to
1848d 20 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d   prevent it from
1848e 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a 65   being recognize
1848f 64 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  d...        **..
18490 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61          ** Varia
18491 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73  ble iNextHdrOffs
18492 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  et is set to the
18493 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68   offset at which
18494 20 74 68 69 73 0d 0a 20 20 20 20 20 20 20 20 2a   this..        *
18495 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65  * problematic he
18496 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c  ader will occur,
18497 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 61   if it exists. a
18498 4d 61 67 69 63 20 69 73 20 75 73 65 64 20 0d 0a  Magic is used ..
18499 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20          ** as a 
1849a 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72  temporary buffer
1849b 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20   to inspect the 
1849c 66 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20  first couple of 
1849d 62 79 74 65 73 20 6f 66 0d 0a 20 20 20 20 20 20  bytes of..      
1849e 20 20 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69    ** the potenti
1849f 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  al journal heade
184a0 72 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a  r...        */..
184a1 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 78          i64 iNex
184a2 74 48 64 72 4f 66 66 73 65 74 3b 0d 0a 20 20 20  tHdrOffset;..   
184a3 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38       u8 aMagic[8
184a4 5d 3b 0d 0a 20 20 20 20 20 20 20 20 75 38 20 7a  ];..        u8 z
184a5 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
184a6 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 3b  ournalMagic)+4];
184a7 0d 0a 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  ....        memc
184a8 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75  py(zHeader, aJou
184a9 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f  rnalMagic, sizeo
184aa 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
184ab 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 75 74 33  );..        put3
184ac 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
184ad 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
184ae 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d 3e 6e  gic)], pPager->n
184af 52 65 63 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  Rec);....       
184b0 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20   iNextHdrOffset 
184b1 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
184b2 65 74 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 20  et(pPager);..   
184b3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
184b4 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e  3OsRead(pPager->
184b5 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20  jfd, aMagic, 8, 
184b6 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b  iNextHdrOffset);
184b7 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ..        if( rc
184b8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30  ==SQLITE_OK && 0
184b9 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  ==memcmp(aMagic,
184ba 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
184bb 38 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  8) ){..         
184bc 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
184bd 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b 0d 0a   zerobyte = 0;..
184be 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
184bf 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
184c0 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f  ager->jfd, &zero
184c1 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 64  byte, 1, iNextHd
184c2 72 4f 66 66 73 65 74 29 3b 0d 0a 20 20 20 20 20  rOffset);..     
184c3 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66     }..        if
184c4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
184c5 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  && rc!=SQLITE_IO
184c6 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
184c7 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  {..          ret
184c8 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 20  urn rc;..       
184c9 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a   }....        /*
184ca 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 20   Write the nRec 
184cb 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 6a  value into the j
184cc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61 64  ournal file head
184cd 65 72 2e 20 49 66 20 69 6e 0d 0a 20 20 20 20 20  er. If in..     
184ce 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 68     ** full-synch
184cf 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 6e  ronous mode, syn
184d0 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  c the journal fi
184d1 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72 65  rst. This ensure
184d2 73 20 74 68 61 74 0d 0a 20 20 20 20 20 20 20 20  s that..        
184d3 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20  ** all data has 
184d4 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64  really hit the d
184d5 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20  isk before nRec 
184d6 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61  is updated to ma
184d7 72 6b 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  rk..        ** i
184d8 74 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 65  t as a candidate
184d9 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0d 0a   for rollback...
184da 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20          **..    
184db 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e      ** This is n
184dc 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74  ot required if t
184dd 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65  he persistent me
184de 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65  dia supports the
184df 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46  ..        ** SAF
184e0 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74  E_APPEND propert
184e1 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68  y. Because in th
184e2 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f  is case it is no
184e3 74 20 70 6f 73 73 69 62 6c 65 20 0d 0a 20 20 20  t possible ..   
184e4 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62       ** for garb
184e5 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61  age data to be a
184e6 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66  ppended to the f
184e7 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69  ile, the nRec fi
184e8 65 6c 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  eld..        ** 
184e9 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
184ea 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 65  h 0xFFFFFFFF whe
184eb 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  n the journal he
184ec 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 0d  ader is written.
184ed 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  .        ** and 
184ee 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62  never needs to b
184ef 65 20 75 70 64 61 74 65 64 2e 0d 0a 20 20 20 20  e updated...    
184f0 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
184f1 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  if( pPager->full
184f2 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26  Sync && 0==(iDc&
184f3 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
184f4 55 45 4e 54 49 41 4c 29 20 29 7b 0d 0a 20 20 20  UENTIAL) ){..   
184f5 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
184f6 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c  E(("SYNC journal
184f7 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52   of %d\n", PAGER
184f8 49 44 28 70 50 61 67 65 72 29 29 29 3b 0d 0a 20  ID(pPager)));.. 
184f9 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45           IOTRACE
184fa 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20  (("JSYNC %p\n", 
184fb 70 50 61 67 65 72 29 29 0d 0a 20 20 20 20 20 20  pPager))..      
184fc 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
184fd 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a  OsSync(pPager->j
184fe 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  fd, pPager->sync
184ff 46 6c 61 67 73 29 3b 0d 0a 20 20 20 20 20 20 20  Flags);..       
18500 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18501 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
18502 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
18503 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
18504 4a 48 44 52 20 25 70 20 25 6c 6c 64 5c 6e 22 2c  JHDR %p %lld\n",
18505 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
18506 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 29 3b 0d 0a  >journalHdr));..
18507 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
18508 69 74 65 33 4f 73 57 72 69 74 65 28 0d 0a 20 20  ite3OsWrite(..  
18509 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
1850a 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20  ->jfd, zHeader, 
1850b 73 69 7a 65 6f 66 28 7a 48 65 61 64 65 72 29 2c  sizeof(zHeader),
1850c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1850d 48 64 72 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d  Hdr..        );.
1850e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1850f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
18510 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 7d  urn rc;..      }
18511 0d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28  ..      if( 0==(
18512 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50  iDc&SQLITE_IOCAP
18513 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0d  _SEQUENTIAL) ){.
18514 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52  .        PAGERTR
18515 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e  ACE(("SYNC journ
18516 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47  al of %d\n", PAG
18517 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0d  ERID(pPager)));.
18518 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45  .        IOTRACE
18519 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20  (("JSYNC %p\n", 
1851a 70 50 61 67 65 72 29 29 0d 0a 20 20 20 20 20 20  pPager))..      
1851b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1851c 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64  Sync(pPager->jfd
1851d 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c  , pPager->syncFl
1851e 61 67 73 7c 20 0d 0a 20 20 20 20 20 20 20 20 20  ags| ..         
1851f 20 28 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c   (pPager->syncFl
18520 61 67 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43  ags==SQLITE_SYNC
18521 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e  _FULL?SQLITE_SYN
18522 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 29 0d 0a 20  C_DATAONLY:0).. 
18523 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20         );..     
18524 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18525 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
18526 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  ;..      }....  
18527 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
18528 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d  nalHdr = pPager-
18529 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0d 0a 20 20  >journalOff;..  
1852a 20 20 20 20 69 66 28 20 6e 65 77 48 64 72 20 26      if( newHdr &
1852b 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45  & 0==(iDc&SQLITE
1852c 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
1852d 4e 44 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ND) ){..        
1852e 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30  pPager->nRec = 0
1852f 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ;..        rc = 
18530 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
18531 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 20 20  pPager);..      
18532 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18533 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
18534 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
18535 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 50 61  else{..      pPa
18536 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
18537 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
18538 6c 4f 66 66 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  lOff;..    }..  
18539 7d 0d 0a 0d 0a 20 20 2f 2a 20 55 6e 6c 65 73 73  }....  /* Unless
1853a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
1853b 20 6e 6f 53 79 6e 63 20 6d 6f 64 65 2c 20 74 68   noSync mode, th
1853c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
1853d 61 73 20 6a 75 73 74 20 0d 0a 20 20 2a 2a 20 73  as just ..  ** s
1853e 75 63 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63  uccessfully sync
1853f 65 64 2e 20 45 69 74 68 65 72 20 77 61 79 2c 20  ed. Either way, 
18540 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f  clear the PGHDR_
18541 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f  NEED_SYNC flag o
18542 6e 20 0d 0a 20 20 2a 2a 20 61 6c 6c 20 70 61 67  n ..  ** all pag
18543 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  es...  */..  sql
18544 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53  ite3PcacheClearS
18545 79 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d  yncFlags(pPager-
18546 3e 70 50 43 61 63 68 65 29 3b 0d 0a 20 20 70 50  >pPCache);..  pP
18547 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50  ager->eState = P
18548 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f  AGER_WRITER_DBMO
18549 44 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 73  D;..  assert( as
1854a 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
1854b 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20 72  (pPager) );..  r
1854c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1854d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
1854e 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
1854f 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e  e first in a lin
18550 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ked list of dirt
18551 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65  y pages connecte
18552 64 0d 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48  d..** by the PgH
18553 64 72 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65  dr.pDirty pointe
18554 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
18555 20 77 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65   writes each one
18556 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 69 6e 2d 6d   of the..** in-m
18557 65 6d 6f 72 79 20 70 61 67 65 73 20 69 6e 20 74  emory pages in t
18558 68 65 20 6c 69 73 74 20 74 6f 20 74 68 65 20 64  he list to the d
18559 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68  atabase file. Th
1855a 65 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0d 0a  e argument may..
1855b 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72  ** be NULL, repr
1855c 65 73 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74  esenting an empt
1855d 79 20 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20  y list. In this 
1855e 63 61 73 65 20 74 68 69 73 20 66 75 6e 63 74 69  case this functi
1855f 6f 6e 20 69 73 0d 0a 2a 2a 20 61 20 6e 6f 2d 6f  on is..** a no-o
18560 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70  p...**..** The p
18561 61 67 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 61  ager must hold a
18562 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 56  t least a RESERV
18563 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 74 68 69  ED lock when thi
18564 73 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 69  s function..** i
18565 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f 72 65  s called. Before
18566 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 69 6e   writing anythin
18567 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
18568 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c 6f 63  e file, this loc
18569 6b 0d 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 65  k..** is upgrade
1856a 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56  d to an EXCLUSIV
1856b 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c  E lock. If the l
1856c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62  ock cannot be ob
1856d 74 61 69 6e 65 64 2c 0d 0a 2a 2a 20 53 51 4c 49  tained,..** SQLI
1856e 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72  TE_BUSY is retur
1856f 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20  ned and no data 
18570 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  is written to th
18571 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
18572 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49 66 20 74 68 65  ..** ..** If the
18573 20 70 61 67 65 72 20 69 73 20 61 20 74 65 6d 70   pager is a temp
18574 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e 64 20  -file pager and 
18575 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 2d  the actual file-
18576 73 79 73 74 65 6d 20 66 69 6c 65 0d 0a 2a 2a 20  system file..** 
18577 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 2c  is not yet open,
18578 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 61   it is created a
18579 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65  nd opened before
1857a 20 61 6e 79 20 64 61 74 61 20 69 73 20 0d 0a 2a   any data is ..*
1857b 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0d 0a  * written out...
1857c 2a 2a 0d 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20  **..** Once the 
1857d 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70  lock has been up
1857e 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e  graded and, if n
1857f 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69  ecessary, the fi
18580 6c 65 20 6f 70 65 6e 65 64 2c 0d 0a 2a 2a 20 74  le opened,..** t
18581 68 65 20 70 61 67 65 73 20 61 72 65 20 77 72 69  he pages are wri
18582 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20  tten out to the 
18583 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
18584 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69   list order. Wri
18585 74 69 6e 67 0d 0a 2a 2a 20 61 20 70 61 67 65 20  ting..** a page 
18586 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 69 74  is skipped if it
18587 20 6d 65 65 74 73 20 65 69 74 68 65 72 20 6f 66   meets either of
18588 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
18589 72 69 74 65 72 69 61 3a 0d 0a 2a 2a 0d 0a 2a 2a  riteria:..**..**
1858a 20 20 20 2a 20 54 68 65 20 70 61 67 65 20 6e 75     * The page nu
1858b 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20  mber is greater 
1858c 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a  than Pager.dbSiz
1858d 65 2c 20 6f 72 0d 0a 2a 2a 20 20 20 2a 20 54 68  e, or..**   * Th
1858e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  e PGHDR_DONT_WRI
1858f 54 45 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f  TE flag is set o
18590 6e 20 74 68 65 20 70 61 67 65 2e 0d 0a 2a 2a 0d  n the page...**.
18591 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e 67 20 6f  .** If writing o
18592 75 74 20 61 20 70 61 67 65 20 63 61 75 73 65 73  ut a page causes
18593 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
18594 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 61 67 65  le to grow, Page
18595 72 2e 64 62 46 69 6c 65 53 69 7a 65 0d 0a 2a 2a  r.dbFileSize..**
18596 20 69 73 20 75 70 64 61 74 65 64 20 61 63 63 6f   is updated acco
18597 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 67 65  rdingly. If page
18598 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 6f 75   1 is written ou
18599 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  t, then the valu
1859a 65 20 63 61 63 68 65 64 0d 0a 2a 2a 20 69 6e 20  e cached..** in 
1859b 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
1859c 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 6f  [] is updated to
1859d 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 76   match the new v
1859e 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0d 0a  alue stored in..
1859f 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
185a0 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  file...**..** If
185a1 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73   everything is s
185a2 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
185a3 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
185a4 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
185a5 20 0d 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 6e   ..** occurs, an
185a6 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
185a7 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c 20  s returned. Or, 
185a8 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56 45  if the EXCLUSIVE
185a9 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0d 0a 2a 2a   lock cannot..**
185aa 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51   be obtained, SQ
185ab 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
185ac 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  urned...*/..stat
185ad 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69  ic int pager_wri
185ae 74 65 5f 70 61 67 65 6c 69 73 74 28 50 61 67 65  te_pagelist(Page
185af 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64 72  r *pPager, PgHdr
185b0 20 2a 70 4c 69 73 74 29 7b 0d 0a 20 20 69 6e 74   *pList){..  int
185b1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
185b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185b3 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
185b4 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73   */....  /* This
185b5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c   function is onl
185b6 79 20 63 61 6c 6c 65 64 20 66 6f 72 20 72 6f 6c  y called for rol
185b7 6c 62 61 63 6b 20 70 61 67 65 72 73 20 69 6e 20  lback pagers in 
185b8 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 73 74 61  WRITER_DBMOD sta
185b9 74 65 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  te. */..  assert
185ba 28 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70  ( !pagerUseWal(p
185bb 50 61 67 65 72 29 20 29 3b 0d 0a 20 20 61 73 73  Pager) );..  ass
185bc 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74  ert( pPager->eSt
185bd 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45  ate==PAGER_WRITE
185be 52 5f 44 42 4d 4f 44 20 29 3b 0d 0a 20 20 61 73  R_DBMOD );..  as
185bf 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c  sert( pPager->eL
185c0 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ock==EXCLUSIVE_L
185c1 4f 43 4b 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  OCK );....  /* I
185c2 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 20  f the file is a 
185c3 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f  temp-file has no
185c4 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65  t yet been opene
185c5 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20  d, open it now. 
185c6 49 74 0d 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  It..  ** is not 
185c7 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20  possible for rc 
185c8 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e  to be other than
185c9 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68   SQLITE_OK if th
185ca 69 73 20 62 72 61 6e 63 68 0d 0a 20 20 2a 2a 20  is branch..  ** 
185cb 69 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67  is taken, as pag
185cc 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
185cd 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72  ) is a no-op for
185ce 20 74 65 6d 70 2d 66 69 6c 65 73 2e 0d 0a 20 20   temp-files...  
185cf 2a 2f 0d 0a 20 20 69 66 28 20 21 69 73 4f 70 65  */..  if( !isOpe
185d0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b  n(pPager->fd) ){
185d1 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ..    assert( pP
185d2 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26  ager->tempFile &
185d3 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
185d4 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 70 61 67  );..    rc = pag
185d5 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
185d6 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  r, pPager->fd, p
185d7 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29  Pager->vfsFlags)
185d8 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 42  ;..  }....  /* B
185d9 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  efore the first 
185da 77 72 69 74 65 2c 20 67 69 76 65 20 74 68 65 20  write, give the 
185db 56 46 53 20 61 20 68 69 6e 74 20 6f 66 20 77 68  VFS a hint of wh
185dc 61 74 20 74 68 65 20 66 69 6e 61 6c 0d 0a 20 20  at the final..  
185dd 2a 2a 20 66 69 6c 65 20 73 69 7a 65 20 77 69 6c  ** file size wil
185de 6c 20 62 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61  l be...  */..  a
185df 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
185e0 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70  E_OK || isOpen(p
185e1 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0d 0a 20  Pager->fd) );.. 
185e2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
185e3 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62  OK && pPager->db
185e4 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 64 62 48  Size>pPager->dbH
185e5 69 6e 74 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20  intSize ){..    
185e6 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 7a  sqlite3_int64 sz
185e7 46 69 6c 65 20 3d 20 70 50 61 67 65 72 2d 3e 70  File = pPager->p
185e8 61 67 65 53 69 7a 65 20 2a 20 28 73 71 6c 69 74  ageSize * (sqlit
185e9 65 33 5f 69 6e 74 36 34 29 70 50 61 67 65 72 2d  e3_int64)pPager-
185ea 3e 64 62 53 69 7a 65 3b 0d 0a 20 20 20 20 73 71  >dbSize;..    sq
185eb 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
185ec 6f 6c 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66  olHint(pPager->f
185ed 64 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  d, SQLITE_FCNTL_
185ee 53 49 5a 45 5f 48 49 4e 54 2c 20 26 73 7a 46 69  SIZE_HINT, &szFi
185ef 6c 65 29 3b 0d 0a 20 20 20 20 70 50 61 67 65 72  le);..    pPager
185f0 2d 3e 64 62 48 69 6e 74 53 69 7a 65 20 3d 20 70  ->dbHintSize = p
185f1 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0d 0a  Pager->dbSize;..
185f2 20 20 7d 0d 0a 0d 0a 20 20 77 68 69 6c 65 28 20    }....  while( 
185f3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
185f4 20 70 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20 50   pList ){..    P
185f5 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74  gno pgno = pList
185f6 2d 3e 70 67 6e 6f 3b 0d 0a 0d 0a 20 20 20 20 2f  ->pgno;....    /
185f7 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64  * If there are d
185f8 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68  irty pages in th
185f9 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74  e page cache wit
185fa 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67  h page numbers g
185fb 72 65 61 74 65 72 0d 0a 20 20 20 20 2a 2a 20 74  reater..    ** t
185fc 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65  han Pager.dbSize
185fd 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c  , this means sql
185fe 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
185ff 65 49 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c  eImage() was cal
18600 6c 65 64 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 6d  led to..    ** m
18601 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d 61  ake the file sma
18602 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c 79  ller (presumably
18603 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   by auto-vacuum 
18604 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72  code). Do not wr
18605 69 74 65 0d 0a 20 20 20 20 2a 2a 20 61 6e 79 20  ite..    ** any 
18606 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 68  such pages to th
18607 65 20 66 69 6c 65 2e 0d 0a 20 20 20 20 2a 2a 0d  e file...    **.
18608 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f  .    ** Also, do
18609 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61   not write out a
1860a 6e 79 20 70 61 67 65 20 74 68 61 74 20 68 61 73  ny page that has
1860b 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f   the PGHDR_DONT_
1860c 57 52 49 54 45 20 66 6c 61 67 0d 0a 20 20 20 20  WRITE flag..    
1860d 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20 73  ** set (set by s
1860e 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57  qlite3PagerDontW
1860f 72 69 74 65 28 29 29 2e 0d 0a 20 20 20 20 2a 2f  rite())...    */
18610 0d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d  ..    if( pgno<=
18611 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26  pPager->dbSize &
18612 26 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61  & 0==(pList->fla
18613 67 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52  gs&PGHDR_DONT_WR
18614 49 54 45 29 20 29 7b 0d 0a 20 20 20 20 20 20 69  ITE) ){..      i
18615 36 34 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e  64 offset = (pgn
18616 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72  o-1)*(i64)pPager
18617 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 2f 2a  ->pageSize;   /*
18618 20 4f 66 66 73 65 74 20 74 6f 20 77 72 69 74 65   Offset to write
18619 20 2a 2f 0d 0a 20 20 20 20 20 20 63 68 61 72 20   */..      char 
1861a 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
1861b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1861c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1861d 61 20 74 6f 20 77 72 69 74 65 20 2a 2f 20 20 20  a to write */   
1861e 20 0d 0a 0d 0a 20 20 20 20 20 20 61 73 73 65 72   ....      asser
1861f 74 28 20 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73  t( (pList->flags
18620 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
18621 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 69  )==0 );..      i
18622 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3d 3d  f( pList->pgno==
18623 31 20 29 20 70 61 67 65 72 5f 77 72 69 74 65 5f  1 ) pager_write_
18624 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 4c  changecounter(pL
18625 69 73 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f  ist);....      /
18626 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 64 61 74  * Encode the dat
18627 61 62 61 73 65 20 2a 2f 0d 0a 20 20 20 20 20 20  abase */..      
18628 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70  CODEC2(pPager, p
18629 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 67 6e  List->pData, pgn
1862a 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 53 51 4c  o, 6, return SQL
1862b 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61  ITE_NOMEM, pData
1862c 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 57  );....      /* W
1862d 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 67  rite out the pag
1862e 65 20 64 61 74 61 2e 20 2a 2f 0d 0a 20 20 20 20  e data. */..    
1862f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
18630 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
18631 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
18632 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
18633 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20  t);....      /* 
18634 49 66 20 70 61 67 65 20 31 20 77 61 73 20 6a 75  If page 1 was ju
18635 73 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61  st written, upda
18636 74 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  te Pager.dbFileV
18637 65 72 73 20 74 6f 20 6d 61 74 63 68 0d 0a 20 20  ers to match..  
18638 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65      ** the value
18639 20 6e 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74   now stored in t
1863a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1863b 2e 20 49 66 20 77 72 69 74 69 6e 67 20 74 68 69  . If writing thi
1863c 73 20 0d 0a 20 20 20 20 20 20 2a 2a 20 70 61 67  s ..      ** pag
1863d 65 20 63 61 75 73 65 64 20 74 68 65 20 64 61 74  e caused the dat
1863e 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72  abase file to gr
1863f 6f 77 2c 20 75 70 64 61 74 65 20 64 62 46 69 6c  ow, update dbFil
18640 65 53 69 7a 65 2e 20 0d 0a 20 20 20 20 20 20 2a  eSize. ..      *
18641 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e  /..      if( pgn
18642 6f 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20  o==1 ){..       
18643 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d   memcpy(&pPager-
18644 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44  >dbFileVers, &pD
18645 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28  ata[24], sizeof(
18646 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
18647 72 73 29 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rs));..      }..
18648 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70        if( pgno>p
18649 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
1864a 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 50  e ){..        pP
1864b 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
1864c 20 3d 20 70 67 6e 6f 3b 0d 0a 20 20 20 20 20 20   = pgno;..      
1864d 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 55 70  }....      /* Up
1864e 64 61 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20  date any backup 
1864f 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20  objects copying 
18650 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
18651 74 68 69 73 20 70 61 67 65 72 2e 20 2a 2f 0d 0a  this pager. */..
18652 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 63        sqlite3Bac
18653 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65 72  kupUpdate(pPager
18654 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c  ->pBackup, pgno,
18655 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61   (u8*)pList->pDa
18656 74 61 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 50 41  ta);....      PA
18657 47 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45  GERTRACE(("STORE
18658 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68   %d page %d hash
18659 28 25 30 38 78 29 5c 6e 22 2c 0d 0a 20 20 20 20  (%08x)\n",..    
1865a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
1865b 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
1865c 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65  pgno, pager_page
1865d 68 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0d 0a  hash(pList)));..
1865e 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
1865f 50 47 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20  PGOUT %p %d\n", 
18660 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0d  pPager, pgno));.
18661 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43  .      PAGER_INC
18662 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  R(sqlite3_pager_
18663 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0d  writedb_count);.
18664 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43  .      PAGER_INC
18665 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65  R(pPager->nWrite
18666 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
18667 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
18668 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61  (("NOSTORE %d pa
18669 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  ge %d\n", PAGERI
1866a 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29  D(pPager), pgno)
1866b 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70  );..    }..    p
1866c 61 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73  ager_set_pagehas
1866d 68 28 70 4c 69 73 74 29 3b 0d 0a 20 20 20 20 70  h(pList);..    p
1866e 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44  List = pList->pD
1866f 69 72 74 79 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  irty;..  }....  
18670 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
18671 0a 2f 2a 0d 0a 2a 2a 20 45 6e 73 75 72 65 20 74  ./*..** Ensure t
18672 68 61 74 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  hat the sub-jour
18673 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  nal file is open
18674 2e 20 49 66 20 69 74 20 69 73 20 61 6c 72 65 61  . If it is alrea
18675 64 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 0d 0a  dy open, this ..
18676 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  ** function is a
18677 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   no-op...**..** 
18678 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
18679 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68  urned if everyth
1867a 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69  ing goes accordi
1867b 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 41 6e 20 0d  ng to plan. An .
1867c 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  .** SQLITE_IOERR
1867d 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20  _XXX error code 
1867e 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61  is returned if a
1867f 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
18680 4f 73 4f 70 65 6e 28 29 20 0d 0a 2a 2a 20 66 61  OsOpen() ..** fa
18681 69 6c 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ils...*/..static
18682 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72   int openSubJour
18683 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
18684 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  r){..  int rc = 
18685 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66  SQLITE_OK;..  if
18686 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
18687 2d 3e 73 6a 66 64 29 20 29 7b 0d 0a 20 20 20 20  ->sjfd) ){..    
18688 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
18689 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
1868a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
1868b 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62  Y || pPager->sub
1868c 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0d 0a 20 20  jInMemory ){..  
1868d 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f      sqlite3MemJo
1868e 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72  urnalOpen(pPager
1868f 2d 3e 73 6a 66 64 29 3b 0d 0a 20 20 20 20 7d 65  ->sjfd);..    }e
18690 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  lse{..      rc =
18691 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70   pagerOpentemp(p
18692 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73  Pager, pPager->s
18693 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  jfd, SQLITE_OPEN
18694 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0d 0a 20  _SUBJOURNAL);.. 
18695 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
18696 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
18697 0d 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 72 65  ..** Append a re
18698 63 6f 72 64 20 6f 66 20 74 68 65 20 63 75 72 72  cord of the curr
18699 65 6e 74 20 73 74 61 74 65 20 6f 66 20 70 61 67  ent state of pag
1869a 65 20 70 50 67 20 74 6f 20 74 68 65 20 73 75 62  e pPg to the sub
1869b 2d 6a 6f 75 72 6e 61 6c 2e 20 0d 0a 2a 2a 20 49  -journal. ..** I
1869c 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73  t is the callers
1869d 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
1869e 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 75 69  to use subjRequi
1869f 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 68 65  resPage() to che
186a0 63 6b 20 0d 0a 2a 2a 20 74 68 61 74 20 69 74 20  ck ..** that it 
186a1 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72  is really requir
186a2 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  ed before callin
186a3 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  g this function.
186a4 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63 63  ..**..** If succ
186a5 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20  essful, set the 
186a6 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  bit correspondin
186a7 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69  g to pPg->pgno i
186a8 6e 20 74 68 65 20 62 69 74 76 65 63 73 0d 0a 2a  n the bitvecs..*
186a9 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73  * for all open s
186aa 61 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 65  avepoints before
186ab 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d   returning...**.
186ac 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
186ad 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  n returns SQLITE
186ae 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e  _OK if everythin
186af 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
186b0 20 61 6e 20 49 4f 0d 0a 2a 2a 20 65 72 72 6f 72   an IO..** error
186b1 20 63 6f 64 65 20 69 66 20 74 68 65 20 61 74 74   code if the att
186b2 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f  empt to write to
186b3 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
186b4 20 66 61 69 6c 73 2c 20 6f 72 20 0d 0a 2a 2a 20   fails, or ..** 
186b5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
186b6 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77  a malloc fails w
186b7 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61 20 62  hile setting a b
186b8 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f 69 6e  it in a savepoin
186b9 74 0d 0a 2a 2a 20 62 69 74 76 65 63 2e 0d 0a 2a  t..** bitvec...*
186ba 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75  /..static int su
186bb 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48  bjournalPage(PgH
186bc 64 72 20 2a 70 50 67 29 7b 0d 0a 20 20 69 6e 74  dr *pPg){..  int
186bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
186be 0d 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ..  Pager *pPage
186bf 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
186c0 0d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
186c1 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47  journalMode!=PAG
186c2 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
186c3 46 46 20 29 7b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  FF ){....    /* 
186c4 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75  Open the sub-jou
186c5 72 6e 61 6c 2c 20 69 66 20 69 74 20 68 61 73 20  rnal, if it has 
186c6 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e  not already been
186c7 20 6f 70 65 6e 65 64 20 2a 2f 0d 0a 20 20 20 20   opened */..    
186c8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
186c9 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0d 0a 20  useJournal );.. 
186ca 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65     assert( isOpe
186cb 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
186cc 7c 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  | pagerUseWal(pP
186cd 61 67 65 72 29 20 29 3b 0d 0a 20 20 20 20 61 73  ager) );..    as
186ce 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
186cf 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 20 70 50  ger->sjfd) || pP
186d0 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3d 3d 30  ager->nSubRec==0
186d1 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
186d2 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61   pagerUseWal(pPa
186d3 67 65 72 29 20 0d 0a 20 20 20 20 20 20 20 20 20  ger) ..         
186d4 7c 7c 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c  || pageInJournal
186d5 28 70 50 67 29 20 0d 0a 20 20 20 20 20 20 20 20  (pPg) ..        
186d6 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50   || pPg->pgno>pP
186d7 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
186d8 20 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20 72   ..    );..    r
186d9 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e  c = openSubJourn
186da 61 6c 28 70 50 61 67 65 72 29 3b 0d 0a 0d 0a 20  al(pPager);.... 
186db 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 75 62     /* If the sub
186dc 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6f 70 65  -journal was ope
186dd 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ned successfully
186de 20 28 6f 72 20 77 61 73 20 61 6c 72 65 61 64 79   (or was already
186df 20 6f 70 65 6e 29 2c 0d 0a 20 20 20 20 2a 2a 20   open),..    ** 
186e0 77 72 69 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  write the journa
186e1 6c 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  l record into th
186e2 65 20 66 69 6c 65 2e 20 20 2a 2f 0d 0a 20 20 20  e file.  */..   
186e3 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
186e4 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 76 6f 69  OK ){..      voi
186e5 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e  d *pData = pPg->
186e6 70 44 61 74 61 3b 0d 0a 20 20 20 20 20 20 69 36  pData;..      i6
186e7 34 20 6f 66 66 73 65 74 20 3d 20 28 69 36 34 29  4 offset = (i64)
186e8 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a  pPager->nSubRec*
186e9 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53  (4+pPager->pageS
186ea 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 63 68 61  ize);..      cha
186eb 72 20 2a 70 44 61 74 61 32 3b 0d 0a 20 20 0d 0a  r *pData2;..  ..
186ec 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61        CODEC2(pPa
186ed 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
186ee 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e  >pgno, 7, return
186ef 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70   SQLITE_NOMEM, p
186f0 44 61 74 61 32 29 3b 0d 0a 20 20 20 20 20 20 50  Data2);..      P
186f1 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54  AGERTRACE(("STMT
186f2 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65  -JOURNAL %d page
186f3 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
186f4 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67  pPager), pPg->pg
186f5 6e 6f 29 29 3b 0d 0a 20 20 20 20 20 20 72 63 20  no));..      rc 
186f6 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
186f7 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73  ager->sjfd, offs
186f8 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0d  et, pPg->pgno);.
186f9 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
186fa 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
186fb 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
186fc 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
186fd 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70  >sjfd, pData2, p
186fe 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
186ff 20 6f 66 66 73 65 74 2b 34 29 3b 0d 0a 20 20 20   offset+4);..   
18700 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
18701 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
18702 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 50  TE_OK ){..    pP
18703 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b  ager->nSubRec++;
18704 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ..    assert( pP
18705 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
18706 3e 30 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  >0 );..    rc = 
18707 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69  addToSavepointBi
18708 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50  tvecs(pPager, pP
18709 67 2d 3e 70 67 6e 6f 29 3b 0d 0a 20 20 7d 0d 0a  g->pgno);..  }..
1870a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
1870b 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
1870c 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1870d 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20  d by the pcache 
1870e 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61  layer when it ha
1870f 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0d 0a  s reached some..
18710 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c  ** soft memory l
18711 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 20  imit. The first 
18712 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f  argument is a po
18713 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72  inter to a Pager
18714 20 6f 62 6a 65 63 74 0d 0a 2a 2a 20 28 63 61 73   object..** (cas
18715 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54  t as a void*). T
18716 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77 61  he pager is alwa
18717 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20 28  ys 'purgeable' (
18718 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  not an in-memory
18719 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20  ..** database). 
1871a 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
1871b 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e  ent is a referen
1871c 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61  ce to a page tha
1871d 74 20 69 73 20 0d 0a 2a 2a 20 63 75 72 72 65 6e  t is ..** curren
1871e 74 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61  tly dirty but ha
1871f 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  s no outstanding
18720 20 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65   references. The
18721 20 70 61 67 65 0d 0a 2a 2a 20 69 73 20 61 6c 77   page..** is alw
18722 61 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ays associated w
18723 69 74 68 20 74 68 65 20 50 61 67 65 72 20 6f 62  ith the Pager ob
18724 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74  ject passed as t
18725 68 65 20 66 69 72 73 74 20 0d 0a 2a 2a 20 61 72  he first ..** ar
18726 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  gument...**..** 
18727 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20  The job of this 
18728 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 6d  function is to m
18729 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 79  ake pPg clean by
1872a 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f 6e   writing its con
1872b 74 65 6e 74 73 0d 0a 2a 2a 20 6f 75 74 20 74 6f  tents..** out to
1872c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1872d 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e  le, if possible.
1872e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76   This may involv
1872f 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0d 0a 2a  e syncing the..*
18730 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  * journal file. 
18731 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63 63  ..**..** If succ
18732 65 73 73 66 75 6c 2c 20 73 71 6c 69 74 65 33 50  essful, sqlite3P
18733 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29  cacheMakeClean()
18734 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68   is called on th
18735 65 20 70 61 67 65 20 61 6e 64 0d 0a 2a 2a 20 53  e page and..** S
18736 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
18737 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  d. If an IO erro
18738 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  r occurs while t
18739 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68  rying to make th
1873a 65 0d 0a 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e  e..** page clean
1873b 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  , the IO error c
1873c 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1873d 20 49 66 20 74 68 65 20 70 61 67 65 20 63 61 6e   If the page can
1873e 6e 6f 74 20 62 65 0d 0a 2a 2a 20 6d 61 64 65 20  not be..** made 
1873f 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 6f  clean for some o
18740 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 74  ther reason, but
18741 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   no error occurs
18742 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b  , then SQLITE_OK
18743 0d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ..** is returned
18744 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 63 68   by sqlite3Pcach
18745 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20  eMakeClean() is 
18746 6e 6f 74 20 63 61 6c 6c 65 64 2e 0d 0a 2a 2f 0d  not called...*/.
18747 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
18748 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c  rStress(void *p,
18749 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0d 0a 20   PgHdr *pPg){.. 
1874a 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
1874b 20 28 50 61 67 65 72 20 2a 29 70 3b 0d 0a 20 20   (Pager *)p;..  
1874c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1874d 4f 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  OK;....  assert(
1874e 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50   pPg->pPager==pP
1874f 61 67 65 72 20 29 3b 0d 0a 20 20 61 73 73 65 72  ager );..  asser
18750 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  t( pPg->flags&PG
18751 48 44 52 5f 44 49 52 54 59 20 29 3b 0d 0a 0d 0a  HDR_DIRTY );....
18752 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79    /* The doNotSy
18753 6e 63 53 70 69 6c 6c 20 66 6c 61 67 20 69 73 20  ncSpill flag is 
18754 73 65 74 20 64 75 72 69 6e 67 20 74 69 6d 65 73  set during times
18755 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 73 79   when doing a sy
18756 6e 63 20 6f 66 0d 0a 20 20 2a 2a 20 6a 6f 75 72  nc of..  ** jour
18757 6e 61 6c 20 28 61 6e 64 20 61 64 64 69 6e 67 20  nal (and adding 
18758 61 20 6e 65 77 20 68 65 61 64 65 72 29 20 69 73  a new header) is
18759 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 2e 20 20 54   not allowed.  T
1875a 68 69 73 20 6f 63 63 75 72 73 0d 0a 20 20 2a 2a  his occurs..  **
1875b 20 64 75 72 69 6e 67 20 63 61 6c 6c 73 20 74 6f   during calls to
1875c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1875d 74 65 28 29 20 77 68 69 6c 65 20 74 72 79 69 6e  te() while tryin
1875e 67 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 6d 75 6c  g to journal mul
1875f 74 69 70 6c 65 0d 0a 20 20 2a 2a 20 70 61 67 65  tiple..  ** page
18760 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74  s belonging to t
18761 68 65 20 73 61 6d 65 20 73 65 63 74 6f 72 2e 0d  he same sector..
18762 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20  .  **..  ** The 
18763 64 6f 4e 6f 74 53 70 69 6c 6c 20 66 6c 61 67 20  doNotSpill flag 
18764 69 6e 68 69 62 69 74 73 20 61 6c 6c 20 63 61 63  inhibits all cac
18765 68 65 20 73 70 69 6c 6c 69 6e 67 20 72 65 67 61  he spilling rega
18766 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
18767 72 0d 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 61  r..  ** or not a
18768 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 65   sync is require
18769 64 2e 20 20 54 68 69 73 20 69 73 20 73 65 74 20  d.  This is set 
1876a 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
1876b 6b 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 53  k...  **..  ** S
1876c 70 69 6c 6c 69 6e 67 20 69 73 20 61 6c 73 6f 20  pilling is also 
1876d 70 72 6f 68 69 62 69 74 65 64 20 77 68 65 6e 20  prohibited when 
1876e 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74  in an error stat
1876f 65 20 73 69 6e 63 65 20 74 68 61 74 20 63 6f 75  e since that cou
18770 6c 64 0d 0a 20 20 2a 2a 20 6c 65 61 64 20 74 6f  ld..  ** lead to
18771 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
18772 74 69 6f 6e 2e 20 20 20 49 6e 20 74 68 65 20 63  tion.   In the c
18773 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74  urrent implement
18774 61 74 6f 6e 20 69 74 20 0d 0a 20 20 2a 2a 20 69  aton it ..  ** i
18775 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72  s impossible for
18776 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65   sqlite3PcacheFe
18777 74 63 68 28 29 20 74 6f 20 62 65 20 63 61 6c 6c  tch() to be call
18778 65 64 20 77 69 74 68 20 63 72 65 61 74 65 46 6c  ed with createFl
18779 61 67 3d 3d 31 0d 0a 20 20 2a 2a 20 77 68 69 6c  ag==1..  ** whil
1877a 65 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73  e in the error s
1877b 74 61 74 65 2c 20 68 65 6e 63 65 20 69 74 20 69  tate, hence it i
1877c 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72  s impossible for
1877d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
1877e 0d 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64  ..  ** be called
1877f 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74   in the error st
18780 61 74 65 2e 20 20 4e 65 76 65 72 74 68 65 6c 65  ate.  Neverthele
18781 73 73 2c 20 77 65 20 69 6e 63 6c 75 64 65 20 61  ss, we include a
18782 20 4e 45 56 45 52 28 29 0d 0a 20 20 2a 2a 20 74   NEVER()..  ** t
18783 65 73 74 20 66 6f 72 20 74 68 65 20 65 72 72 6f  est for the erro
18784 72 20 73 74 61 74 65 20 61 73 20 61 20 73 61 66  r state as a saf
18785 65 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 66  eguard against f
18786 75 74 75 72 65 20 63 68 61 6e 67 65 73 2e 0d 0a  uture changes...
18787 20 20 2a 2f 0d 0a 20 20 69 66 28 20 4e 45 56 45    */..  if( NEVE
18788 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  R(pPager->errCod
18789 65 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  e) ) return SQLI
1878a 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 50  TE_OK;..  if( pP
1878b 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 70 69 6c 6c  ager->doNotSpill
1878c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
1878d 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 50 61 67  _OK;..  if( pPag
1878e 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 53 70 69  er->doNotSyncSpi
1878f 6c 6c 20 26 26 20 28 70 50 67 2d 3e 66 6c 61 67  ll && (pPg->flag
18790 73 20 26 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  s & PGHDR_NEED_S
18791 59 4e 43 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  YNC)!=0 ){..    
18792 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18793 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 50 67 2d  ;..  }....  pPg-
18794 3e 70 44 69 72 74 79 20 3d 20 30 3b 0d 0a 20 20  >pDirty = 0;..  
18795 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28  if( pagerUseWal(
18796 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20 20 20  pPager) ){..    
18797 2f 2a 20 57 72 69 74 65 20 61 20 73 69 6e 67 6c  /* Write a singl
18798 65 20 66 72 61 6d 65 20 66 6f 72 20 74 68 69 73  e frame for this
18799 20 70 61 67 65 20 74 6f 20 74 68 65 20 6c 6f 67   page to the log
1879a 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 75  . */..    if( su
1879b 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70  bjRequiresPage(p
1879c 50 67 29 20 29 7b 20 0d 0a 20 20 20 20 20 20 72  Pg) ){ ..      r
1879d 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  c = subjournalPa
1879e 67 65 28 70 50 67 29 3b 20 0d 0a 20 20 20 20 7d  ge(pPg); ..    }
1879f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
187a0 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
187a1 20 20 72 63 20 3d 20 70 61 67 65 72 57 61 6c 46    rc = pagerWalF
187a2 72 61 6d 65 73 28 70 50 61 67 65 72 2c 20 70 50  rames(pPager, pP
187a3 67 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 7d  g, 0, 0);..    }
187a4 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 0d 0a  ..  }else{..  ..
187a5 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20      /* Sync the 
187a6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
187a7 72 65 71 75 69 72 65 64 2e 20 2a 2f 0d 0a 20 20  required. */..  
187a8 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73    if( pPg->flags
187a9 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
187aa 20 0d 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65   ..     || pPage
187ab 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
187ac 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44  _WRITER_CACHEMOD
187ad 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20  ..    ){..      
187ae 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c  rc = syncJournal
187af 28 70 50 61 67 65 72 2c 20 31 29 3b 0d 0a 20 20  (pPager, 1);..  
187b0 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20    }..  ..    /* 
187b1 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
187b2 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  er of this page 
187b3 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
187b4 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
187b5 6f 66 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  of..    ** the d
187b6 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69  atabase image, i
187b7 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65  t may need to be
187b8 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
187b9 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0d 0a 20 20  sub-journal...  
187ba 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63    ** This is bec
187bb 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  ause the call to
187bc 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67   pager_write_pag
187bd 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69  elist() below wi
187be 6c 6c 20 6e 6f 74 0d 0a 20 20 20 20 2a 2a 20 61  ll not..    ** a
187bf 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61  ctually write da
187c0 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69  ta to the file i
187c1 6e 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 20 20  n this case...  
187c2 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 43 6f 6e    **..    ** Con
187c3 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  sider the follow
187c4 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ing sequence of 
187c5 65 76 65 6e 74 73 3a 0d 0a 20 20 20 20 2a 2a 0d  events:..    **.
187c6 0a 20 20 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b  .    **   BEGIN;
187c7 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 3c 6a 6f  ..    **     <jo
187c8 75 72 6e 61 6c 20 70 61 67 65 20 58 3e 0d 0a 20  urnal page X>.. 
187c9 20 20 20 2a 2a 20 20 20 20 20 3c 6d 6f 64 69 66     **     <modif
187ca 79 20 70 61 67 65 20 58 3e 0d 0a 20 20 20 20 2a  y page X>..    *
187cb 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20  *     SAVEPOINT 
187cc 73 70 3b 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20  sp;..    **     
187cd 20 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61    <shrink databa
187ce 73 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67  se file to Y pag
187cf 65 73 3e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20  es>..    **     
187d0 20 20 70 61 67 65 72 53 74 72 65 73 73 28 70 61    pagerStress(pa
187d1 67 65 20 58 29 0d 0a 20 20 20 20 2a 2a 20 20 20  ge X)..    **   
187d2 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70    ROLLBACK TO sp
187d3 3b 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  ;..    **..    *
187d4 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65 6e  * If (X>Y), then
187d5 20 77 68 65 6e 20 70 61 67 65 72 53 74 72 65 73   when pagerStres
187d6 73 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65  s is called page
187d7 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77   X will not be w
187d8 72 69 74 74 65 6e 0d 0a 20 20 20 20 2a 2a 20 6f  ritten..    ** o
187d9 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
187da 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c  se file, but wil
187db 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f  l be dropped fro
187dc 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65  m the cache. The
187dd 6e 2c 0d 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  n,..    ** follo
187de 77 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 41  wing the "ROLLBA
187df 43 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 6d  CK TO sp" statem
187e0 65 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 67  ent, reading pag
187e1 65 20 58 20 77 69 6c 6c 20 72 65 61 64 0d 0a 20  e X will read.. 
187e2 20 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20     ** data from 
187e3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
187e4 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20  e. This will be 
187e5 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65  the copy of page
187e6 20 58 20 61 73 20 69 74 0d 0a 20 20 20 20 2a 2a   X as it..    **
187e7 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 74 72   was when the tr
187e8 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65  ansaction starte
187e9 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73  d, not as it was
187ea 20 77 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54   when "SAVEPOINT
187eb 20 73 70 22 0d 0a 20 20 20 20 2a 2a 20 77 61 73   sp"..    ** was
187ec 20 65 78 65 63 75 74 65 64 2e 0d 0a 20 20 20 20   executed...    
187ed 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 73  **..    ** The s
187ee 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72  olution is to wr
187ef 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
187f0 64 61 74 61 20 66 6f 72 20 70 61 67 65 20 58 20  data for page X 
187f1 69 6e 74 6f 20 74 68 65 20 0d 0a 20 20 20 20 2a  into the ..    *
187f2 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69  * sub-journal fi
187f3 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69 73  le now (if it is
187f4 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68 65   not already the
187f5 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74 20  re), so that it 
187f6 77 69 6c 6c 0d 0a 20 20 20 20 2a 2a 20 62 65 20  will..    ** be 
187f7 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20  restored to its 
187f8 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68  current value wh
187f9 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b  en the "ROLLBACK
187fa 20 54 4f 20 73 70 22 20 69 73 20 0d 0a 20 20 20   TO sp" is ..   
187fb 20 2a 2a 20 65 78 65 63 75 74 65 64 2e 0d 0a 20   ** executed... 
187fc 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 4e     */..    if( N
187fd 45 56 45 52 28 0d 0a 20 20 20 20 20 20 20 20 72  EVER(..        r
187fe 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
187ff 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72  pPg->pgno>pPager
18800 2d 3e 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a  ->dbSize && subj
18801 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67  RequiresPage(pPg
18802 29 0d 0a 20 20 20 20 29 20 29 7b 0d 0a 20 20 20  )..    ) ){..   
18803 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e     rc = subjourn
18804 61 6c 50 61 67 65 28 70 50 67 29 3b 0d 0a 20 20  alPage(pPg);..  
18805 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20    }..  ..    /* 
18806 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  Write the conten
18807 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f  ts of the page o
18808 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
18809 73 65 20 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20 20  se file. */..   
1880a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1880b 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  OK ){..      ass
1880c 65 72 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73  ert( (pPg->flags
1880d 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
1880e 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 72  )==0 );..      r
1880f 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f  c = pager_write_
18810 70 61 67 65 6c 69 73 74 28 70 50 61 67 65 72 2c  pagelist(pPager,
18811 20 70 50 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20   pPg);..    }.. 
18812 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d 61 72 6b 20   }....  /* Mark 
18813 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  the page as clea
18814 6e 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 3d  n. */..  if( rc=
18815 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
18816 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
18817 53 54 52 45 53 53 20 25 64 20 70 61 67 65 20 25  STRESS %d page %
18818 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
18819 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
1881a 29 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ));..    sqlite3
1881b 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
1881c 70 50 67 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  pPg);..  }....  
1881d 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72  return pager_err
1881e 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 20  or(pPager, rc); 
1881f 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
18820 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69  Allocate and ini
18821 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61  tialize a new Pa
18822 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70  ger object and p
18823 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ut a pointer to 
18824 69 74 0d 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67  it..** in *ppPag
18825 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68  er. The pager sh
18826 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20  ould eventually 
18827 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73  be freed by pass
18828 69 6e 67 20 69 74 0d 0a 2a 2a 20 74 6f 20 73 71  ing it..** to sq
18829 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28  lite3PagerClose(
1882a 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 7a  )...**..** The z
1882b 46 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e  Filename argumen
1882c 74 20 69 73 20 74 68 65 20 70 61 74 68 20 74 6f  t is the path to
1882d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1882e 6c 65 20 74 6f 20 6f 70 65 6e 2e 0d 0a 2a 2a 20  le to open...** 
1882f 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
18830 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64  NULL then a rand
18831 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f  omly-named tempo
18832 72 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65  rary file is cre
18833 61 74 65 64 0d 0a 2a 2a 20 61 6e 64 20 75 73 65  ated..** and use
18834 64 20 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f  d as the file to
18835 20 62 65 20 63 61 63 68 65 64 2e 20 54 65 6d 70   be cached. Temp
18836 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20  orary files are 
18837 62 65 20 64 65 6c 65 74 65 64 0d 0a 2a 2a 20 61  be deleted..** a
18838 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65  utomatically whe
18839 6e 20 74 68 65 79 20 61 72 65 20 63 6c 6f 73 65  n they are close
1883a 64 2e 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  d. If zFilename 
1883b 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68  is ":memory:" th
1883c 65 6e 20 0d 0a 2a 2a 20 61 6c 6c 20 69 6e 66 6f  en ..** all info
1883d 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 20  rmation is held 
1883e 69 6e 20 63 61 63 68 65 2e 20 49 74 20 69 73 20  in cache. It is 
1883f 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74 6f  never written to
18840 20 64 69 73 6b 2e 20 0d 0a 2a 2a 20 54 68 69 73   disk. ..** This
18841 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
18842 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d  implement an in-
18843 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e  memory database.
18844 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e 45 78  ..**..** The nEx
18845 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73 70  tra parameter sp
18846 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62  ecifies the numb
18847 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
18848 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0d 0a  pace allocated..
18849 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61  ** along with ea
1884a 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ch page referenc
1884b 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73  e. This space is
1884c 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
1884d 65 20 75 73 65 72 0d 0a 2a 2a 20 76 69 61 20 74  e user..** via t
1884e 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  he sqlite3PagerG
1884f 65 74 45 78 74 72 61 28 29 20 41 50 49 2e 0d 0a  etExtra() API...
18850 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  **..** The flags
18851 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73 65   argument is use
18852 64 20 74 6f 20 73 70 65 63 69 66 79 20 70 72 6f  d to specify pro
18853 70 65 72 74 69 65 73 20 74 68 61 74 20 61 66 66  perties that aff
18854 65 63 74 20 74 68 65 0d 0a 2a 2a 20 6f 70 65 72  ect the..** oper
18855 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67  ation of the pag
18856 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65  er. It should be
18857 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74   passed some bit
18858 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  wise combination
18859 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45  ..** of the PAGE
1885a 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61  R_OMIT_JOURNAL a
1885b 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44  nd PAGER_NO_READ
1885c 4c 4f 43 4b 20 66 6c 61 67 73 2e 0d 0a 2a 2a 0d  LOCK flags...**.
1885d 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73  .** The vfsFlags
1885e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20   parameter is a 
1885f 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20  bitmask to pass 
18860 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  to the flags par
18861 61 6d 65 74 65 72 0d 0a 2a 2a 20 6f 66 20 74 68  ameter..** of th
18862 65 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64  e xOpen() method
18863 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64   of the supplied
18864 20 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e   VFS when openin
18865 67 20 66 69 6c 65 73 2e 20 0d 0a 2a 2a 0d 0a 2a  g files. ..**..*
18866 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 6f  * If the pager o
18867 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74  bject is allocat
18868 65 64 20 61 6e 64 20 74 68 65 20 73 70 65 63 69  ed and the speci
18869 66 69 65 64 20 66 69 6c 65 20 6f 70 65 6e 65 64  fied file opened
1886a 20 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c   ..** successful
1886b 6c 79 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ly, SQLITE_OK is
1886c 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
1886d 70 50 61 67 65 72 20 73 65 74 20 74 6f 20 70 6f  pPager set to po
1886e 69 6e 74 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 6e  int to..** the n
1886f 65 77 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e  ew pager object.
18870 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
18871 75 72 73 2c 20 2a 70 70 50 61 67 65 72 20 69 73  urs, *ppPager is
18872 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0d 0a 2a 2a   set to NULL..**
18873 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 65 20   and error code 
18874 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 66  returned. This f
18875 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
18876 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0d  rn SQLITE_NOMEM.
18877 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c  .** (sqlite3Mall
18878 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 6f 20  oc() is used to 
18879 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 29  allocate memory)
1887a 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  , SQLITE_CANTOPE
1887b 4e 20 6f 72 20 0d 0a 2a 2a 20 76 61 72 69 6f 75  N or ..** variou
1887c 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20  s SQLITE_IO_XXX 
1887d 65 72 72 6f 72 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  errors...*/..SQL
1887e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1887f 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
18880 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  (..  sqlite3_vfs
18881 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a   *pVfs,       /*
18882 20 54 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c   The virtual fil
18883 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20  e system to use 
18884 2a 2f 0d 0a 20 20 50 61 67 65 72 20 2a 2a 70 70  */..  Pager **pp
18885 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 2f  Pager,         /
18886 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74 68  * OUT: Return th
18887 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
18888 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 63 6f 6e  e here */..  con
18889 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
1888a 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me,   /* Name of
1888b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1888c 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0d 0a 20  le to open */.. 
1888d 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20   int nExtra,    
1888e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
1888f 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20  ra bytes append 
18890 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72  to each in-memor
18891 79 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69 6e 74  y page */..  int
18892 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
18893 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63        /* flags c
18894 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20  ontrolling this 
18895 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 76  file */..  int v
18896 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20  fsFlags,        
18897 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73      /* flags pas
18898 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
18899 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
1889a 28 29 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a  () */..  void (*
1889b 78 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a  xReinit)(DbPage*
1889c 29 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  ) /* Function to
1889d 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61   reinitialize pa
1889e 67 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 38  ges */..){..  u8
1889f 20 2a 70 50 74 72 3b 0d 0a 20 20 50 61 67 65 72   *pPtr;..  Pager
188a0 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 20 20 20   *pPager = 0;   
188a1 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a      /* Pager obj
188a2 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ect to allocate 
188a3 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 20  and return */.. 
188a4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
188a5 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74  _OK;      /* Ret
188a6 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69  urn code */..  i
188a7 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b  nt tempFile = 0;
188a8 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
188a9 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 20 28  for temp files (
188aa 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20  incl. in-memory 
188ab 66 69 6c 65 73 29 20 2a 2f 0d 0a 20 20 69 6e 74  files) */..  int
188ac 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 20   memDb = 0;     
188ad 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
188ae 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d   this is an in-m
188af 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0d 0a 20  emory file */.. 
188b0 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20   int readOnly = 
188b1 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
188b2 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 72  e if this is a r
188b3 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f  ead-only file */
188b4 0d 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46  ..  int journalF
188b5 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  ileSize;     /* 
188b6 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  Bytes to allocat
188b7 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e  e for each journ
188b8 61 6c 20 66 64 20 2a 2f 0d 0a 20 20 63 68 61 72  al fd */..  char
188b9 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b   *zPathname = 0;
188ba 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74       /* Full pat
188bb 68 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69  h to database fi
188bc 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50 61  le */..  int nPa
188bd 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20  thname = 0;     
188be 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
188bf 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d  ytes in zPathnam
188c0 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 75 73 65 4a  e */..  int useJ
188c1 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20  ournal = (flags 
188c2 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55  & PAGER_OMIT_JOU
188c3 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c  RNAL)==0; /* Fal
188c4 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e  se to omit journ
188c5 61 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 6f 52  al */..  int noR
188c6 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73  eadlock = (flags
188c7 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44   & PAGER_NO_READ
188c8 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 72  LOCK)!=0;  /* Tr
188c9 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 2d  ue to omit read-
188ca 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 70  lock */..  int p
188cb 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69  cacheSize = sqli
188cc 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b  te3PcacheSize();
188cd 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
188ce 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20  to allocate for 
188cf 50 43 61 63 68 65 20 2a 2f 0d 0a 20 20 75 33 32  PCache */..  u32
188d0 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51   szPageDflt = SQ
188d1 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
188d2 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61  E_SIZE;  /* Defa
188d3 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  ult page size */
188d4 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
188d5 7a 55 72 69 20 3d 20 30 3b 20 20 20 20 2f 2a 20  zUri = 0;    /* 
188d6 55 52 49 20 61 72 67 73 20 74 6f 20 63 6f 70 79  URI args to copy
188d7 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 55 72 69 20   */..  int nUri 
188d8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
188d9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
188da 65 73 20 6f 66 20 55 52 49 20 61 72 67 73 20 61  es of URI args a
188db 74 20 2a 7a 55 72 69 20 2a 2f 0d 0a 0d 0a 20 20  t *zUri */....  
188dc 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
188dd 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20  w much space is 
188de 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 61 63  required for eac
188df 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68  h journal file-h
188e0 61 6e 64 6c 65 0d 0a 20 20 2a 2a 20 28 74 68 65  andle..  ** (the
188e1 72 65 20 61 72 65 20 74 77 6f 20 6f 66 20 74 68  re are two of th
188e2 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  em, the main jou
188e3 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 75 62  rnal and the sub
188e4 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 73 0d  -journal). This.
188e5 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 78  .  ** is the max
188e6 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 75 69  imum space requi
188e7 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  red for an in-me
188e8 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
188e9 65 20 68 61 6e 64 6c 65 20 0d 0a 20 20 2a 2a 20  e handle ..  ** 
188ea 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a 6f  and a regular jo
188eb 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c  urnal file-handl
188ec 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 22  e. Note that a "
188ed 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d  regular journal-
188ee 68 61 6e 64 6c 65 22 0d 0a 20 20 2a 2a 20 6d 61  handle"..  ** ma
188ef 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 63  y be a wrapper c
188f0 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e  apable of cachin
188f1 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 74  g the first port
188f2 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ion of the journ
188f3 61 6c 0d 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e  al..  ** file in
188f4 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65   memory to imple
188f5 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d  ment the atomic-
188f6 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
188f7 6f 6e 20 28 73 65 65 20 0d 0a 20 20 2a 2a 20 73  on (see ..  ** s
188f8 6f 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e  ource file journ
188f9 61 6c 2e 63 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  al.c)...  */..  
188fa 69 66 28 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  if( sqlite3Journ
188fb 61 6c 53 69 7a 65 28 70 56 66 73 29 3e 73 71 6c  alSize(pVfs)>sql
188fc 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69  ite3MemJournalSi
188fd 7a 65 28 29 20 29 7b 0d 0a 20 20 20 20 6a 6f 75  ze() ){..    jou
188fe 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52  rnalFileSize = R
188ff 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4a 6f 75  OUND8(sqlite3Jou
18900 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b  rnalSize(pVfs));
18901 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
18902 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20  journalFileSize 
18903 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33  = ROUND8(sqlite3
18904 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29  MemJournalSize()
18905 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  );..  }....  /* 
18906 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76  Set the output v
18907 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20  ariable to NULL 
18908 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72  in case an error
18909 20 6f 63 63 75 72 73 2e 20 2a 2f 0d 0a 20 20 2a   occurs. */..  *
1890a 70 70 50 61 67 65 72 20 3d 20 30 3b 0d 0a 0d 0a  ppPager = 0;....
1890b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1890c 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0d 0a 20 20  MIT_MEMORYDB..  
1890d 69 66 28 20 66 6c 61 67 73 20 26 20 50 41 47 45  if( flags & PAGE
1890e 52 5f 4d 45 4d 4f 52 59 20 29 7b 0d 0a 20 20 20  R_MEMORY ){..   
1890f 20 6d 65 6d 44 62 20 3d 20 31 3b 0d 0a 20 20 20   memDb = 1;..   
18910 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0d   zFilename = 0;.
18911 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
18912 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64    /* Compute and
18913 20 73 74 6f 72 65 20 74 68 65 20 66 75 6c 6c 20   store the full 
18914 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61  pathname in an a
18915 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
18916 70 6f 69 6e 74 65 64 0d 0a 20 20 2a 2a 20 74 6f  pointed..  ** to
18917 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6c   by zPathname, l
18918 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 2e  ength nPathname.
18919 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 73 20   Or, if this is 
1891a 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
1891b 2c 0d 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f  ,..  ** leave bo
1891c 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64  th nPathname and
1891d 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 74   zPathname set t
1891e 6f 20 30 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  o 0...  */..  if
1891f 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a  ( zFilename && z
18920 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0d 0a  Filename[0] ){..
18921 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
18922 7a 3b 0d 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d  z;..    nPathnam
18923 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  e = pVfs->mxPath
18924 6e 61 6d 65 2b 31 3b 0d 0a 20 20 20 20 7a 50 61  name+1;..    zPa
18925 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  thname = sqlite3
18926 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 65  Malloc(nPathname
18927 2a 32 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a 50  *2);..    if( zP
18928 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20  athname==0 ){.. 
18929 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1892a 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d  TE_NOMEM;..    }
1892b 0d 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b  ..    zPathname[
1892c 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20  0] = 0; /* Make 
1892d 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  sure initialized
1892e 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74   even if FullPat
1892f 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f  hname() fails */
18930 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
18931 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
18932 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65  (pVfs, zFilename
18933 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61  , nPathname, zPa
18934 74 68 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 6e 50  thname);..    nP
18935 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
18936 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e  3Strlen30(zPathn
18937 61 6d 65 29 3b 0d 0a 20 20 20 20 7a 20 3d 20 7a  ame);..    z = z
18938 55 72 69 20 3d 20 26 7a 46 69 6c 65 6e 61 6d 65  Uri = &zFilename
18939 5b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  [sqlite3Strlen30
1893a 28 7a 46 69 6c 65 6e 61 6d 65 29 2b 31 5d 3b 0d  (zFilename)+1];.
1893b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 29  .    while( *z )
1893c 7b 0d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 73 71  {..      z += sq
1893d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
1893e 2b 31 3b 0d 0a 20 20 20 20 20 20 7a 20 2b 3d 20  +1;..      z += 
1893f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
18940 7a 29 2b 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  z)+1;..    }..  
18941 20 20 6e 55 72 69 20 3d 20 28 69 6e 74 29 28 26    nUri = (int)(&
18942 7a 5b 31 5d 20 2d 20 7a 55 72 69 29 3b 0d 0a 20  z[1] - zUri);.. 
18943 20 20 20 61 73 73 65 72 74 28 20 6e 55 72 69 3e     assert( nUri>
18944 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  =0 );..    if( r
18945 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
18946 6e 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73  nPathname+8>pVfs
18947 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0d  ->mxPathname ){.
18948 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
18949 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77  ranch is taken w
1894a 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
1894b 70 61 74 68 20 72 65 71 75 69 72 65 64 20 62 79  path required by
1894c 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64  ..      ** the d
1894d 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 6f 70  atabase being op
1894e 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72  ened will be mor
1894f 65 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50  e than pVfs->mxP
18950 61 74 68 6e 61 6d 65 0d 0a 20 20 20 20 20 20 2a  athname..      *
18951 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74  * bytes in lengt
18952 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  h. This means th
18953 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f  e database canno
18954 74 20 62 65 20 6f 70 65 6e 65 64 2c 0d 0a 20 20  t be opened,..  
18955 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 6c      ** as it wil
18956 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  l not be possibl
18957 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f  e to open the jo
18958 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 76  urnal file or ev
18959 65 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 63 68 65  en..      ** che
1895a 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75  ck for a hot-jou
1895b 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64  rnal before read
1895c 69 6e 67 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ing...      */..
1895d 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1895e 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b  E_CANTOPEN_BKPT;
1895f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
18960 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
18961 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
18962 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
18963 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
18964 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  rc;..    }..  }.
18965 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ...  /* Allocate
18966 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20   memory for the 
18967 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c  Pager structure,
18968 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20   PCache object, 
18969 74 68 65 0d 0a 20 20 2a 2a 20 74 68 72 65 65 20  the..  ** three 
1896a 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
1896b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  , the database f
1896c 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65  ile name and the
1896d 20 6a 6f 75 72 6e 61 6c 20 0d 0a 20 20 2a 2a 20   journal ..  ** 
1896e 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c  file name. The l
1896f 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20  ayout in memory 
18970 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a  is as follows:..
18971 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 50    **..  **     P
18972 61 67 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20  ager object     
18973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
18974 73 69 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79  sizeof(Pager) by
18975 74 65 73 29 0d 0a 20 20 2a 2a 20 20 20 20 20 50  tes)..  **     P
18976 43 61 63 68 65 20 6f 62 6a 65 63 74 20 20 20 20  Cache object    
18977 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
18978 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a  sqlite3PcacheSiz
18979 65 28 29 20 62 79 74 65 73 29 0d 0a 20 20 2a 2a  e() bytes)..  **
1897a 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69       Database fi
1897b 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20  le handle       
1897c 20 20 20 20 20 28 70 56 66 73 2d 3e 73 7a 4f 73       (pVfs->szOs
1897d 46 69 6c 65 20 62 79 74 65 73 29 0d 0a 20 20 2a  File bytes)..  *
1897e 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75 72 6e 61  *     Sub-journa
1897f 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20  l file handle   
18980 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69        (journalFi
18981 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0d 0a 20  leSize bytes).. 
18982 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a 6f 75   **     Main jou
18983 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65  rnal file handle
18984 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c          (journal
18985 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0d  FileSize bytes).
18986 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61  .  **     Databa
18987 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20  se file name    
18988 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68            (nPath
18989 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0d 0a 20  name+1 bytes).. 
1898a 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20   **     Journal 
1898b 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20  file name       
1898c 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61          (nPathna
1898d 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0d 0a 20  me+8+1 bytes).. 
1898e 20 2a 2f 0d 0a 20 20 70 50 74 72 20 3d 20 28 75   */..  pPtr = (u
1898f 38 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  8 *)sqlite3Mallo
18990 63 5a 65 72 6f 28 0d 0a 20 20 20 20 52 4f 55 4e  cZero(..    ROUN
18991 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65  D8(sizeof(*pPage
18992 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61  r)) +      /* Pa
18993 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ger structure */
18994 0d 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63 61  ..    ROUND8(pca
18995 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20 20  cheSize) +      
18996 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20 6f       /* PCache o
18997 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 20 20 52 4f  bject */..    RO
18998 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46  UND8(pVfs->szOsF
18999 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20  ile) +       /* 
1899a 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65  The main db file
1899b 20 2a 2f 0d 0a 20 20 20 20 6a 6f 75 72 6e 61 6c   */..    journal
1899c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20  FileSize * 2 +  
1899d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1899e 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  wo journal files
1899f 20 2a 2f 20 0d 0a 20 20 20 20 6e 50 61 74 68 6e   */ ..    nPathn
189a0 61 6d 65 20 2b 20 31 20 2b 20 6e 55 72 69 20 2b  ame + 1 + nUri +
189a1 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
189a2 65 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 6e 50  ename */..    nP
189a3 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 32 20  athname + 8 + 2 
189a4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
189a5 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 23 69 66  zJournal */..#if
189a6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
189a7 5f 57 41 4c 0d 0a 20 20 20 20 2b 20 6e 50 61 74  _WAL..    + nPat
189a8 68 6e 61 6d 65 20 2b 20 34 20 2b 20 32 20 20 20  hname + 4 + 2   
189a9 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 57 61 6c           /* zWal
189aa 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 29   */..#endif..  )
189ab 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  ;..  assert( EIG
189ac 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
189ad 54 28 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  T(SQLITE_INT_TO_
189ae 50 54 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53  PTR(journalFileS
189af 69 7a 65 29 29 20 29 3b 0d 0a 20 20 69 66 28 20  ize)) );..  if( 
189b0 21 70 50 74 72 20 29 7b 0d 0a 20 20 20 20 73 71  !pPtr ){..    sq
189b1 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68  lite3_free(zPath
189b2 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 72 65 74 75  name);..    retu
189b3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
189b4 0d 0a 20 20 7d 0d 0a 20 20 70 50 61 67 65 72 20  ..  }..  pPager 
189b5 3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  =              (
189b6 50 61 67 65 72 2a 29 28 70 50 74 72 29 3b 0d 0a  Pager*)(pPtr);..
189b7 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68    pPager->pPCach
189b8 65 20 3d 20 20 20 20 28 50 43 61 63 68 65 2a 29  e =    (PCache*)
189b9 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28  (pPtr += ROUND8(
189ba 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29  sizeof(*pPager))
189bb 29 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 66 64  );..  pPager->fd
189bc 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69   =   (sqlite3_fi
189bd 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55  le*)(pPtr += ROU
189be 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 29  ND8(pcacheSize))
189bf 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a 66  ;..  pPager->sjf
189c0 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c  d = (sqlite3_fil
189c1 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e  e*)(pPtr += ROUN
189c2 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  D8(pVfs->szOsFil
189c3 65 29 29 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e  e));..  pPager->
189c4 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f  jfd =  (sqlite3_
189c5 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a  file*)(pPtr += j
189c6 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b  ournalFileSize);
189c7 0d 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c  ..  pPager->zFil
189c8 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61 72  ename =    (char
189c9 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e  *)(pPtr += journ
189ca 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0d 0a 20 20  alFileSize);..  
189cb 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
189cc 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61  TE_ALIGNMENT(pPa
189cd 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0d 0a 0d 0a  ger->jfd) );....
189ce 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
189cf 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65   Pager.zFilename
189d0 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72   and Pager.zJour
189d1 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20  nal buffers, if 
189d2 72 65 71 75 69 72 65 64 2e 20 2a 2f 0d 0a 20 20  required. */..  
189d3 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b  if( zPathname ){
189d4 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 50  ..    assert( nP
189d5 61 74 68 6e 61 6d 65 3e 30 20 29 3b 0d 0a 20 20  athname>0 );..  
189d6 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e    pPager->zJourn
189d7 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70  al =   (char*)(p
189d8 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65  Ptr += nPathname
189d9 20 2b 20 31 20 2b 20 6e 55 72 69 29 3b 0d 0a 20   + 1 + nUri);.. 
189da 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72     memcpy(pPager
189db 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 61  ->zFilename, zPa
189dc 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d  thname, nPathnam
189dd 65 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28  e);..    memcpy(
189de 26 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61  &pPager->zFilena
189df 6d 65 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 2c  me[nPathname+1],
189e0 20 7a 55 72 69 2c 20 6e 55 72 69 29 3b 0d 0a 20   zUri, nUri);.. 
189e1 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72     memcpy(pPager
189e2 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74  ->zJournal, zPat
189e3 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65  hname, nPathname
189e4 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  );..    memcpy(&
189e5 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
189e6 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a  [nPathname], "-j
189e7 6f 75 72 6e 61 6c 5c 30 30 30 22 2c 20 38 2b 31  ournal\000", 8+1
189e8 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46  );..    sqlite3F
189e9 69 6c 65 53 75 66 66 69 78 33 28 70 50 61 67 65  ileSuffix3(pPage
189ea 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 50  r->zFilename, pP
189eb 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 29 3b  ager->zJournal);
189ec 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
189ed 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20 20 20 70  _OMIT_WAL..    p
189ee 50 61 67 65 72 2d 3e 7a 57 61 6c 20 3d 20 26 70  Pager->zWal = &p
189ef 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b  Pager->zJournal[
189f0 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 5d 3b 0d  nPathname+8+1];.
189f1 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67  .    memcpy(pPag
189f2 65 72 2d 3e 7a 57 61 6c 2c 20 7a 50 61 74 68 6e  er->zWal, zPathn
189f3 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b  ame, nPathname);
189f4 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50  ..    memcpy(&pP
189f5 61 67 65 72 2d 3e 7a 57 61 6c 5b 6e 50 61 74 68  ager->zWal[nPath
189f6 6e 61 6d 65 5d 2c 20 22 2d 77 61 6c 5c 30 30 30  name], "-wal\000
189f7 22 2c 20 34 2b 31 29 3b 0d 0a 20 20 20 20 73 71  ", 4+1);..    sq
189f8 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78 33  lite3FileSuffix3
189f9 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61  (pPager->zFilena
189fa 6d 65 2c 20 70 50 61 67 65 72 2d 3e 7a 57 61 6c  me, pPager->zWal
189fb 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  );..#endif..    
189fc 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
189fd 74 68 6e 61 6d 65 29 3b 0d 0a 20 20 7d 0d 0a 20  thname);..  }.. 
189fe 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20   pPager->pVfs = 
189ff 70 56 66 73 3b 0d 0a 20 20 70 50 61 67 65 72 2d  pVfs;..  pPager-
18a00 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46  >vfsFlags = vfsF
18a01 6c 61 67 73 3b 0d 0a 0d 0a 20 20 2f 2a 20 4f 70  lags;....  /* Op
18a02 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  en the pager fil
18a03 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  e...  */..  if( 
18a04 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
18a05 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0d 0a 20 20  lename[0] ){..  
18a06 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20    int fout = 0; 
18a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a08 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20     /* VFS flags 
18a09 72 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 65  returned by xOpe
18a0a 6e 28 29 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d  n() */..    rc =
18a0b 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
18a0c 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69  Vfs, pPager->zFi
18a0d 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e  lename, pPager->
18a0e 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66  fd, vfsFlags, &f
18a0f 6f 75 74 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  out);..    asser
18a10 74 28 20 21 6d 65 6d 44 62 20 29 3b 0d 0a 20 20  t( !memDb );..  
18a11 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f    readOnly = (fo
18a12 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ut&SQLITE_OPEN_R
18a13 45 41 44 4f 4e 4c 59 29 3b 0d 0a 0d 0a 20 20 20  EADONLY);....   
18a14 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
18a15 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
18a16 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
18a17 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c 0d 0a  /write access,..
18a18 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20      ** choose a 
18a19 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a  default page siz
18a1a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 61 76  e in case we hav
18a1b 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0d  e to create the.
18a1c 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
18a1d 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75   file. The defau
18a1e 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  lt page size is 
18a1f 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0d  the maximum of:.
18a20 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
18a21 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41     + SQLITE_DEFA
18a22 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0d 0a  ULT_PAGE_SIZE,..
18a23 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20      **    + The 
18a24 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
18a25 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  y sqlite3OsSecto
18a26 72 53 69 7a 65 28 29 0d 0a 20 20 20 20 2a 2a 20  rSize()..    ** 
18a27 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74     + The largest
18a28 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20   page size that 
18a29 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61  can be written a
18a2a 74 6f 6d 69 63 61 6c 6c 79 2e 0d 0a 20 20 20 20  tomically...    
18a2b 2a 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  */..    if( rc==
18a2c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65  SQLITE_OK && !re
18a2d 61 64 4f 6e 6c 79 20 29 7b 0d 0a 20 20 20 20 20  adOnly ){..     
18a2e 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70   setSectorSize(p
18a2f 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 20 20 61  Pager);..      a
18a30 73 73 65 72 74 28 53 51 4c 49 54 45 5f 44 45 46  ssert(SQLITE_DEF
18a31 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d  AULT_PAGE_SIZE<=
18a32 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55  SQLITE_MAX_DEFAU
18a33 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0d 0a  LT_PAGE_SIZE);..
18a34 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 67 65        if( szPage
18a35 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 65 63  Dflt<pPager->sec
18a36 74 6f 72 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20  torSize ){..    
18a37 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
18a38 73 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 54  sectorSize>SQLIT
18a39 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
18a3a 47 45 5f 53 49 5a 45 20 29 7b 0d 0a 20 20 20 20  GE_SIZE ){..    
18a3b 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
18a3c 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45   = SQLITE_MAX_DE
18a3d 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b  FAULT_PAGE_SIZE;
18a3e 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ..        }else{
18a3f 0d 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61  ..          szPa
18a40 67 65 44 66 6c 74 20 3d 20 28 75 33 32 29 70 50  geDflt = (u32)pP
18a41 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
18a42 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
18a43 20 20 20 20 7d 0d 0a 23 69 66 64 65 66 20 53 51      }..#ifdef SQ
18a44 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
18a45 49 43 5f 57 52 49 54 45 0d 0a 20 20 20 20 20 20  IC_WRITE..      
18a46 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  {..        int i
18a47 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  Dc = sqlite3OsDe
18a48 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
18a49 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ics(pPager->fd);
18a4a 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69  ..        int ii
18a4b 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ;..        asser
18a4c 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  t(SQLITE_IOCAP_A
18a4d 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e  TOMIC512==(512>>
18a4e 38 29 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73  8));..        as
18a4f 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41  sert(SQLITE_IOCA
18a50 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35  P_ATOMIC64K==(65
18a51 35 33 36 3e 3e 38 29 29 3b 0d 0a 20 20 20 20 20  536>>8));..     
18a52 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45     assert(SQLITE
18a53 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
18a54 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0d  E_SIZE<=65536);.
18a55 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d  .        for(ii=
18a56 73 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d  szPageDflt; ii<=
18a57 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55  SQLITE_MAX_DEFAU
18a58 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 69 69  LT_PAGE_SIZE; ii
18a59 3d 69 69 2a 32 29 7b 0d 0a 20 20 20 20 20 20 20  =ii*2){..       
18a5a 20 20 20 69 66 28 20 69 44 63 26 28 53 51 4c 49     if( iDc&(SQLI
18a5b 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c  TE_IOCAP_ATOMIC|
18a5c 28 69 69 3e 3e 38 29 29 20 29 7b 0d 0a 20 20 20  (ii>>8)) ){..   
18a5d 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44           szPageD
18a5e 66 6c 74 20 3d 20 69 69 3b 0d 0a 20 20 20 20 20  flt = ii;..     
18a5f 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
18a60 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64  }..      }..#end
18a61 69 66 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c  if..    }..  }el
18a62 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61  se{..    /* If a
18a63 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
18a64 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74  is requested, it
18a65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69   is not opened i
18a66 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 20 20 20  mmediately...   
18a67 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
18a68 20 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64   we accept the d
18a69 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65  efault page size
18a6a 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61   and delay actua
18a6b 6c 6c 79 0d 0a 20 20 20 20 2a 2a 20 6f 70 65 6e  lly..    ** open
18a6c 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74  ing the file unt
18a6d 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  il the first cal
18a6e 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0d  l to OsWrite()..
18a6f 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
18a70 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 61  This branch is a
18a71 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69  lso run for an i
18a72 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
18a73 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0d  e. An in-memory.
18a74 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
18a75 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
18a76 61 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74  a temp-file that
18a77 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65   is never writte
18a78 6e 20 6f 75 74 20 74 6f 0d 0a 20 20 20 20 2a 2a  n out to..    **
18a79 20 64 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61   disk and uses a
18a7a 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c  n in-memory roll
18a7b 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0d 0a 20  back journal... 
18a7c 20 20 20 2a 2f 20 0d 0a 20 20 20 20 74 65 6d 70     */ ..    temp
18a7d 46 69 6c 65 20 3d 20 31 3b 0d 0a 20 20 20 20 70  File = 1;..    p
18a7e 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20  Pager->eState = 
18a7f 50 41 47 45 52 5f 52 45 41 44 45 52 3b 0d 0a 20  PAGER_READER;.. 
18a80 20 20 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b     pPager->eLock
18a81 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43   = EXCLUSIVE_LOC
18a82 4b 3b 0d 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79  K;..    readOnly
18a83 20 3d 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c   = (vfsFlags&SQL
18a84 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
18a85 59 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  Y);..  }....  /*
18a86 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
18a87 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50  all to PagerSetP
18a88 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 65 73  agesize() serves
18a89 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75   to set the valu
18a8a 65 20 6f 66 20 0d 0a 20 20 2a 2a 20 50 61 67 65  e of ..  ** Page
18a8b 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 74  r.pageSize and t
18a8c 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 50  o allocate the P
18a8d 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62  ager.pTmpSpace b
18a8e 75 66 66 65 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  uffer...  */..  
18a8f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18a90 4b 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  K ){..    assert
18a91 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d  ( pPager->memDb=
18a92 3d 30 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  =0 );..    rc = 
18a93 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
18a94 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20  agesize(pPager, 
18a95 26 73 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31 29  &szPageDflt, -1)
18a96 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  ;..    testcase(
18a97 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
18a98 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49  ;..  }....  /* I
18a99 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
18a9a 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66  red in either of
18a9b 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76   the blocks abov
18a9c 65 2c 20 66 72 65 65 20 74 68 65 20 0d 0a 20 20  e, free the ..  
18a9d 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75  ** Pager structu
18a9e 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65  re and close the
18a9f 20 66 69 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   file...  */..  
18aa0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18aa1 4b 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  K ){..    assert
18aa2 28 20 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53  ( !pPager->pTmpS
18aa3 70 61 63 65 20 29 3b 0d 0a 20 20 20 20 73 71 6c  pace );..    sql
18aa4 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
18aa5 65 72 2d 3e 66 64 29 3b 0d 0a 20 20 20 20 73 71  er->fd);..    sq
18aa6 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
18aa7 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  r);..    return 
18aa8 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  rc;..  }....  /*
18aa9 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
18aaa 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a  PCache object. *
18aab 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78  /..  assert( nEx
18aac 74 72 61 3c 31 30 30 30 20 29 3b 0d 0a 20 20 6e  tra<1000 );..  n
18aad 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 6e  Extra = ROUND8(n
18aae 45 78 74 72 61 29 3b 0d 0a 20 20 73 71 6c 69 74  Extra);..  sqlit
18aaf 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50  e3PcacheOpen(szP
18ab0 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c  ageDflt, nExtra,
18ab1 20 21 6d 65 6d 44 62 2c 0d 0a 20 20 20 20 20 20   !memDb,..      
18ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d                !m
18ab3 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73  emDb?pagerStress
18ab4 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67  :0, (void *)pPag
18ab5 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61  er, pPager->pPCa
18ab6 63 68 65 29 3b 0d 0a 0d 0a 20 20 50 41 47 45 52  che);....  PAGER
18ab7 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20  TRACE(("OPEN %d 
18ab8 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c  %s\n", FILEHANDL
18ab9 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c  EID(pPager->fd),
18aba 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
18abb 6d 65 29 29 3b 0d 0a 20 20 49 4f 54 52 41 43 45  me));..  IOTRACE
18abc 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22  (("OPEN %p %s\n"
18abd 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  , pPager, pPager
18abe 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0d 0a 0d  ->zFilename))...
18abf 0a 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  .  pPager->useJo
18ac0 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a  urnal = (u8)useJ
18ac1 6f 75 72 6e 61 6c 3b 0d 0a 20 20 70 50 61 67 65  ournal;..  pPage
18ac2 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20  r->noReadlock = 
18ac3 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72  (noReadlock && r
18ac4 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0d 0a  eadOnly) ?1:0;..
18ac5 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
18ac6 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0d 0a 20  tOpen = 0; */.. 
18ac7 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74   /* pPager->stmt
18ac8 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0d 0a 20  InUse = 0; */.. 
18ac9 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 66   /* pPager->nRef
18aca 20 3d 20 30 3b 20 2a 2f 0d 0a 20 20 2f 2a 20 70   = 0; */..  /* p
18acb 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20  Pager->stmtSize 
18acc 3d 20 30 3b 20 2a 2f 0d 0a 20 20 2f 2a 20 70 50  = 0; */..  /* pP
18acd 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20  ager->stmtJSize 
18ace 3d 20 30 3b 20 2a 2f 0d 0a 20 20 2f 2a 20 70 50  = 0; */..  /* pP
18acf 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b  ager->nPage = 0;
18ad0 20 2a 2f 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6d   */..  pPager->m
18ad1 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d  xPgno = SQLITE_M
18ad2 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0d 0a  AX_PAGE_COUNT;..
18ad3 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61    /* pPager->sta
18ad4 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  te = PAGER_UNLOC
18ad5 4b 3b 20 2a 2f 0d 0a 23 69 66 20 30 0d 0a 20 20  K; */..#if 0..  
18ad6 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
18ad7 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 69  state == (tempFi
18ad8 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c 55  le ? PAGER_EXCLU
18ad9 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c  SIVE : PAGER_UNL
18ada 4f 43 4b 29 20 29 3b 0d 0a 23 65 6e 64 69 66 0d  OCK) );..#endif.
18adb 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72  .  /* pPager->er
18adc 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f 0d 0a 20  rMask = 0; */.. 
18add 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
18ade 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65  e = (u8)tempFile
18adf 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 74 65 6d  ;..  assert( tem
18ae0 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  pFile==PAGER_LOC
18ae1 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
18ae2 0d 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74  ..          || t
18ae3 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c  empFile==PAGER_L
18ae4 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
18ae5 53 49 56 45 20 29 3b 0d 0a 20 20 61 73 73 65 72  SIVE );..  asser
18ae6 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
18ae7 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d  MODE_EXCLUSIVE==
18ae8 31 20 29 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e  1 );..  pPager->
18ae9 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20  exclusiveMode = 
18aea 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0d 0a  (u8)tempFile; ..
18aeb 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
18aec 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67  CountDone = pPag
18aed 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0d 0a 20  er->tempFile;.. 
18aee 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d   pPager->memDb =
18aef 20 28 75 38 29 6d 65 6d 44 62 3b 0d 0a 20 20 70   (u8)memDb;..  p
18af0 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
18af1 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0d  = (u8)readOnly;.
18af2 0a 20 20 61 73 73 65 72 74 28 20 75 73 65 4a 6f  .  assert( useJo
18af3 75 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d  urnal || pPager-
18af4 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0d 0a 20 20  >tempFile );..  
18af5 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d  pPager->noSync =
18af6 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
18af7 65 3b 0d 0a 20 20 69 66 28 20 70 50 61 67 65 72  e;..  if( pPager
18af8 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0d 0a 20 20 20  ->noSync ){..   
18af9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
18afa 3e 66 75 6c 6c 53 79 6e 63 3d 3d 30 20 29 3b 0d  >fullSync==0 );.
18afb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
18afc 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 3d 3d  ger->syncFlags==
18afd 30 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  0 );..    assert
18afe 28 20 70 50 61 67 65 72 2d 3e 77 61 6c 53 79 6e  ( pPager->walSyn
18aff 63 46 6c 61 67 73 3d 3d 30 20 29 3b 0d 0a 20 20  cFlags==0 );..  
18b00 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
18b01 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 3d  ->ckptSyncFlags=
18b02 3d 30 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  =0 );..  }else{.
18b03 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 75 6c  .    pPager->ful
18b04 6c 53 79 6e 63 20 3d 20 31 3b 0d 0a 20 20 20 20  lSync = 1;..    
18b05 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67  pPager->syncFlag
18b06 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s = SQLITE_SYNC_
18b07 4e 4f 52 4d 41 4c 3b 0d 0a 20 20 20 20 70 50 61  NORMAL;..    pPa
18b08 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67  ger->walSyncFlag
18b09 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s = SQLITE_SYNC_
18b0a 4e 4f 52 4d 41 4c 20 7c 20 57 41 4c 5f 53 59 4e  NORMAL | WAL_SYN
18b0b 43 5f 54 52 41 4e 53 41 43 54 49 4f 4e 53 3b 0d  C_TRANSACTIONS;.
18b0c 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b 70  .    pPager->ckp
18b0d 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c  tSyncFlags = SQL
18b0e 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b  ITE_SYNC_NORMAL;
18b0f 0d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 70 50 61 67  ..  }..  /* pPag
18b10 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20  er->pFirst = 0; 
18b11 2a 2f 0d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  */..  /* pPager-
18b12 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20  >pFirstSynced = 
18b13 30 3b 20 2a 2f 0d 0a 20 20 2f 2a 20 70 50 61 67  0; */..  /* pPag
18b14 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a  er->pLast = 0; *
18b15 2f 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78  /..  pPager->nEx
18b16 74 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72  tra = (u16)nExtr
18b17 61 3b 0d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f  a;..  pPager->jo
18b18 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d  urnalSizeLimit =
18b19 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
18b1a 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d  JOURNAL_SIZE_LIM
18b1b 49 54 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69  IT;..  assert( i
18b1c 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
18b1d 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b  ) || tempFile );
18b1e 0d 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a  ..  setSectorSiz
18b1f 65 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 69 66  e(pPager);..  if
18b20 28 20 21 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b  ( !useJournal ){
18b21 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ..    pPager->jo
18b22 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45  urnalMode = PAGE
18b23 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
18b24 46 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  F;..  }else if( 
18b25 6d 65 6d 44 62 20 29 7b 0d 0a 20 20 20 20 70 50  memDb ){..    pP
18b26 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
18b27 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  e = PAGER_JOURNA
18b28 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0d 0a 20  LMODE_MEMORY;.. 
18b29 20 7d 0d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   }..  /* pPager-
18b2a 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20  >xBusyHandler = 
18b2b 30 3b 20 2a 2f 0d 0a 20 20 2f 2a 20 70 50 61 67  0; */..  /* pPag
18b2c 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72  er->pBusyHandler
18b2d 41 72 67 20 3d 20 30 3b 20 2a 2f 0d 0a 20 20 70  Arg = 0; */..  p
18b2e 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72  Pager->xReiniter
18b2f 20 3d 20 78 52 65 69 6e 69 74 3b 0d 0a 20 20 2f   = xReinit;..  /
18b30 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d  * memset(pPager-
18b31 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f  >aHash, 0, sizeo
18b32 66 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29  f(pPager->aHash)
18b33 29 3b 20 2a 2f 0d 0a 0d 0a 20 20 2a 70 70 50 61  ); */....  *ppPa
18b34 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0d 0a 20  ger = pPager;.. 
18b35 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
18b36 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d  K;..}......../*.
18b37 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
18b38 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  n is called afte
18b39 72 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20  r transitioning 
18b3a 66 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  from PAGER_UNLOC
18b3b 4b 20 74 6f 0d 0a 2a 2a 20 50 41 47 45 52 5f 53  K to..** PAGER_S
18b3c 48 41 52 45 44 20 73 74 61 74 65 2e 20 49 74 20  HARED state. It 
18b3d 74 65 73 74 73 20 69 66 20 74 68 65 72 65 20 69  tests if there i
18b3e 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  s a hot journal 
18b3f 70 72 65 73 65 6e 74 20 69 6e 0d 0a 2a 2a 20 74  present in..** t
18b40 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66  he file-system f
18b41 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67  or the given pag
18b42 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61  er. A hot journa
18b43 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 0d 0a  l is one that ..
18b44 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70  ** needs to be p
18b45 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 6f  layed back. Acco
18b46 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 75  rding to this fu
18b47 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f  nction, a hot-jo
18b48 75 72 6e 61 6c 0d 0a 2a 2a 20 66 69 6c 65 20 65  urnal..** file e
18b49 78 69 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c  xists if the fol
18b4a 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 20  lowing criteria 
18b4b 61 72 65 20 6d 65 74 3a 0d 0a 2a 2a 0d 0a 2a 2a  are met:..**..**
18b4c 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c     * The journal
18b4d 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20   file exists in 
18b4e 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c  the file system,
18b4f 20 61 6e 64 0d 0a 2a 2a 20 20 20 2a 20 4e 6f 20   and..**   * No 
18b50 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20  process holds a 
18b51 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61  RESERVED or grea
18b52 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ter lock on the 
18b53 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61  database file, a
18b54 6e 64 0d 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64  nd..**   * The d
18b55 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 73  atabase file its
18b56 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20 74  elf is greater t
18b57 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73  han 0 bytes in s
18b58 69 7a 65 2c 20 61 6e 64 0d 0a 2a 2a 20 20 20 2a  ize, and..**   *
18b59 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
18b5a 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
18b5b 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69  ile exists and i
18b5c 73 20 6e 6f 74 20 30 78 30 30 2e 0d 0a 2a 2a 0d  s not 0x00...**.
18b5d 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65  .** If the curre
18b5e 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  nt size of the d
18b5f 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
18b60 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20  0 but a journal 
18b61 66 69 6c 65 0d 0a 2a 2a 20 65 78 69 73 74 73 2c  file..** exists,
18b62 20 74 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c   that is probabl
18b63 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c  y an old journal
18b64 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20   left over from 
18b65 61 20 70 72 69 6f 72 0d 0a 2a 2a 20 64 61 74 61  a prior..** data
18b66 62 61 73 65 20 77 69 74 68 20 74 68 65 20 73 61  base with the sa
18b67 6d 65 20 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73  me name. In this
18b68 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61   case the journa
18b69 6c 20 66 69 6c 65 20 69 73 0d 0a 2a 2a 20 6a 75  l file is..** ju
18b6a 73 74 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67  st deleted using
18b6b 20 4f 73 44 65 6c 65 74 65 2c 20 2a 70 45 78 69   OsDelete, *pExi
18b6c 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 30 20  sts is set to 0 
18b6d 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 2a  and SQLITE_OK..*
18b6e 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  * is returned...
18b6f 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
18b70 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 65  ine does not che
18b71 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ck if there is a
18b72 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
18b73 66 69 6c 65 6e 61 6d 65 0d 0a 2a 2a 20 61 74 20  filename..** at 
18b74 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
18b75 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
18b76 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 74 65  , and that maste
18b77 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0d 0a  r journal file..
18b78 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  ** does not exis
18b79 74 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  t, then the jour
18b7a 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  nal file is not 
18b7b 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 74  really hot. In t
18b7c 68 69 73 0d 0a 2a 2a 20 63 61 73 65 20 74 68 69  his..** case thi
18b7d 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72  s routine will r
18b7e 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f  eturn a false-po
18b7f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 67 65  sitive. The page
18b80 72 5f 70 6c 61 79 62 61 63 6b 28 29 0d 0a 2a 2a  r_playback()..**
18b81 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69   routine will di
18b82 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 20  scover that the 
18b83 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
18b84 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 61  not really hot a
18b85 6e 64 20 0d 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  nd ..** will not
18b86 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0d   roll it back. .
18b87 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20 68 6f 74  .**..** If a hot
18b88 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73  -journal file is
18b89 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c   found to exist,
18b8a 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 74   *pExists is set
18b8b 20 74 6f 20 31 20 61 6e 64 20 0d 0a 2a 2a 20 53   to 1 and ..** S
18b8c 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
18b8d 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75  d. If no hot-jou
18b8e 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
18b8f 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69  sent, *pExists i
18b90 73 0d 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61  s..** set to 0 a
18b91 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
18b92 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
18b93 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
18b94 6c 65 20 74 72 79 69 6e 67 0d 0a 2a 2a 20 74 6f  le trying..** to
18b95 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
18b96 65 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d  er or not a hot-
18b97 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
18b98 73 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f  sts, the IO erro
18b99 72 0d 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65  r..** code is re
18b9a 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76  turned and the v
18b9b 61 6c 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73  alue of *pExists
18b9c 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a   is undefined...
18b9d 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 68  */..static int h
18b9e 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67  asHotJournal(Pag
18b9f 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
18ba0 2a 70 45 78 69 73 74 73 29 7b 0d 0a 20 20 73 71  *pExists){..  sq
18ba1 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73  lite3_vfs * cons
18ba2 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d  t pVfs = pPager-
18ba3 3e 70 56 66 73 3b 0d 0a 20 20 69 6e 74 20 72 63  >pVfs;..  int rc
18ba4 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
18ba5 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
18ba6 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74  n code */..  int
18ba7 20 65 78 69 73 74 73 20 3d 20 31 3b 20 20 20 20   exists = 1;    
18ba8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
18ba9 75 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c 20  ue if a journal 
18baa 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 20  file is present 
18bab 2a 2f 0d 0a 20 20 69 6e 74 20 6a 72 6e 6c 4f 70  */..  int jrnlOp
18bac 65 6e 20 3d 20 21 21 69 73 4f 70 65 6e 28 70 50  en = !!isOpen(pP
18bad 61 67 65 72 2d 3e 6a 66 64 29 3b 0d 0a 0d 0a 20  ager->jfd);.... 
18bae 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
18baf 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0d 0a  >useJournal );..
18bb0 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
18bb1 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0d  (pPager->fd) );.
18bb2 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
18bb3 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
18bb4 5f 4f 50 45 4e 20 29 3b 0d 0a 0d 0a 20 20 61 73  _OPEN );....  as
18bb5 73 65 72 74 28 20 6a 72 6e 6c 4f 70 65 6e 3d 3d  sert( jrnlOpen==
18bb6 30 20 7c 7c 20 28 20 73 71 6c 69 74 65 33 4f 73  0 || ( sqlite3Os
18bb7 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
18bb8 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 6a 66  stics(pPager->jf
18bb9 64 29 20 26 0d 0a 20 20 20 20 53 51 4c 49 54 45  d) &..    SQLITE
18bba 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41 42  _IOCAP_UNDELETAB
18bbb 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 0d 0a 20 20  LE_WHEN_OPEN..  
18bbc 29 29 3b 0d 0a 0d 0a 20 20 2a 70 45 78 69 73 74  ));....  *pExist
18bbd 73 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 21 6a  s = 0;..  if( !j
18bbe 72 6e 6c 4f 70 65 6e 20 29 7b 0d 0a 20 20 20 20  rnlOpen ){..    
18bbf 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
18bc0 63 65 73 73 28 70 56 66 73 2c 20 70 50 61 67 65  cess(pVfs, pPage
18bc1 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c  r->zJournal, SQL
18bc2 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
18bc3 53 2c 20 26 65 78 69 73 74 73 29 3b 0d 0a 20 20  S, &exists);..  
18bc4 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
18bc5 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73  ITE_OK && exists
18bc6 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6c 6f 63   ){..    int loc
18bc7 6b 65 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ked = 0;        
18bc8 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
18bc9 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 6f 6c  some process hol
18bca 64 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ds a RESERVED lo
18bcb 63 6b 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20  ck */....    /* 
18bcc 52 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68  Race condition h
18bcd 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 20 70 72  ere:  Another pr
18bce 6f 63 65 73 73 20 6d 69 67 68 74 20 68 61 76 65  ocess might have
18bcf 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68   been holding th
18bd0 65 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 52 45  e..    ** the RE
18bd1 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e 64 20  SERVED lock and 
18bd2 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c 20 6f  have a journal o
18bd3 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c 69 74  pen at the sqlit
18bd4 65 33 4f 73 41 63 63 65 73 73 28 29 20 0d 0a 20  e3OsAccess() .. 
18bd5 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65     ** call above
18bd6 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65 74  , but then delet
18bd7 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e  e the journal an
18bd8 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b 20  d drop the lock 
18bd9 62 65 66 6f 72 65 0d 0a 20 20 20 20 2a 2a 20 77  before..    ** w
18bda 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c  e get to the fol
18bdb 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73  lowing sqlite3Os
18bdc 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
18bdd 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68  k() call.  If th
18bde 61 74 0d 0a 20 20 20 20 2a 2a 20 69 73 20 74 68  at..    ** is th
18bdf 65 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75  e case, this rou
18be0 74 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b  tine might think
18be1 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20   there is a hot 
18be2 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0d 0a 20 20  journal when..  
18be3 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65    ** in fact the
18be4 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69  re is none.  Thi
18be5 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66  s results in a f
18be6 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68  alse-positive wh
18be7 69 63 68 20 77 69 6c 6c 0d 0a 20 20 20 20 2a 2a  ich will..    **
18be8 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62   be dealt with b
18be9 79 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 72  y the playback r
18bea 6f 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74 20  outine.  Ticket 
18beb 23 33 38 38 33 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  #3883...    */..
18bec 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
18bed 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  OsCheckReservedL
18bee 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
18bef 26 6c 6f 63 6b 65 64 29 3b 0d 0a 20 20 20 20 69  &locked);..    i
18bf0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
18bf1 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0d 0a   && !locked ){..
18bf2 20 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65        Pgno nPage
18bf3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18bf4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
18bf5 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65  ages in database
18bf6 20 66 69 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20   file */....    
18bf7 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 73    /* Check the s
18bf8 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
18bf9 61 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20  ase file. If it 
18bfa 63 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 61  consists of 0 pa
18bfb 67 65 73 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 74  ges,..      ** t
18bfc 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a  hen delete the j
18bfd 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65  ournal file. See
18bfe 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d   the header comm
18bff 65 6e 74 20 61 62 6f 76 65 20 66 6f 72 20 0d 0a  ent above for ..
18c00 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 61        ** the rea
18c01 73 6f 6e 69 6e 67 20 68 65 72 65 2e 20 20 44 65  soning here.  De
18c02 6c 65 74 65 20 74 68 65 20 6f 62 73 6f 6c 65 74  lete the obsolet
18c03 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75  e journal file u
18c04 6e 64 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20 61  nder..      ** a
18c05 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74   RESERVED lock t
18c06 6f 20 61 76 6f 69 64 20 72 61 63 65 20 63 6f 6e  o avoid race con
18c07 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 6f 20 61  ditions and to a
18c08 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e 67 0d 0a  void violating..
18c09 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 30 32 30        ** [H33020
18c0a 5d 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  ]...      */..  
18c0b 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 61      rc = pagerPa
18c0c 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
18c0d 26 6e 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20  &nPage);..      
18c0e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18c0f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66  K ){..        if
18c10 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0d 0a 20  ( nPage==0 ){.. 
18c11 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
18c12 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
18c13 63 28 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  c();..          
18c14 69 66 28 20 70 61 67 65 72 4c 6f 63 6b 44 62 28  if( pagerLockDb(
18c15 70 50 61 67 65 72 2c 20 52 45 53 45 52 56 45 44  pPager, RESERVED
18c16 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f  _LOCK)==SQLITE_O
18c17 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  K ){..          
18c18 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74    sqlite3OsDelet
18c19 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  e(pVfs, pPager->
18c1a 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0d 0a 20  zJournal, 0);.. 
18c1b 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21             if( !
18c1c 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
18c1d 65 4d 6f 64 65 20 29 20 70 61 67 65 72 55 6e 6c  eMode ) pagerUnl
18c1e 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48  ockDb(pPager, SH
18c1f 41 52 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20  ARED_LOCK);..   
18c20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
18c21 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
18c22 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20  nignMalloc();.. 
18c23 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
18c24 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
18c25 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
18c26 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72  sts and no other
18c27 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
18c28 61 20 72 65 73 65 72 76 65 64 0d 0a 20 20 20 20  a reserved..    
18c29 20 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 61        ** or grea
18c2a 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ter lock on the 
18c2b 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 4e  database file. N
18c2c 6f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  ow check that th
18c2d 65 72 65 20 69 73 0d 0a 20 20 20 20 20 20 20 20  ere is..        
18c2e 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e    ** at least on
18c2f 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73  e non-zero bytes
18c30 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
18c31 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
18c32 65 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  e...          **
18c33 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 74 68   If there is, th
18c34 65 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 20 74  en we consider t
18c35 68 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62  his journal to b
18c36 65 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0d  e hot. If not, .
18c37 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74  .          ** it
18c38 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 2e   can be ignored.
18c39 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a  ..          */..
18c3a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 6a            if( !j
18c3b 72 6e 6c 4f 70 65 6e 20 29 7b 0d 0a 20 20 20 20  rnlOpen ){..    
18c3c 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20          int f = 
18c3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
18c3e 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  ONLY|SQLITE_OPEN
18c3f 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0d 0a  _MAIN_JOURNAL;..
18c40 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
18c41 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
18c42 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
18c43 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a  urnal, pPager->j
18c44 66 64 2c 20 66 2c 20 26 66 29 3b 0d 0a 20 20 20  fd, f, &f);..   
18c45 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
18c46 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
18c47 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
18c48 20 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d        u8 first =
18c49 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   0;..           
18c4a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
18c4b 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ead(pPager->jfd,
18c4c 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c   (void *)&first,
18c4d 20 31 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20   1, 0);..       
18c4e 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
18c4f 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
18c50 52 45 41 44 20 29 7b 0d 0a 20 20 20 20 20 20 20  READ ){..       
18c51 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
18c52 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20  TE_OK;..        
18c53 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
18c54 20 20 20 69 66 28 20 21 6a 72 6e 6c 4f 70 65 6e     if( !jrnlOpen
18c55 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
18c56 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
18c57 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0d  e(pPager->jfd);.
18c58 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  .            }..
18c59 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78              *pEx
18c5a 69 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d 30  ists = (first!=0
18c5b 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65  );..          }e
18c5c 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
18c5d 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0d 0a  TE_CANTOPEN ){..
18c5e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
18c5f 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 6e  f we cannot open
18c60 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
18c61 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72  urnal file in or
18c62 64 65 72 20 74 6f 20 73 65 65 20 69 66 0d 0a 20  der to see if.. 
18c63 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74             ** it
18c64 73 20 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61  s has a zero hea
18c65 64 65 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20  der, that might 
18c66 62 65 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f  be due to an I/O
18c67 20 65 72 72 6f 72 2c 20 6f 72 0d 0a 20 20 20 20   error, or..    
18c68 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69          ** it mi
18c69 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 74 68  ght be due to th
18c6a 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  e race condition
18c6b 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
18c6c 20 61 6e 64 20 69 6e 0d 0a 20 20 20 20 20 20 20   and in..       
18c6d 20 20 20 20 20 2a 2a 20 74 69 63 6b 65 74 20 23       ** ticket #
18c6e 33 38 38 33 2e 20 20 45 69 74 68 65 72 20 77 61  3883.  Either wa
18c6f 79 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74  y, assume that t
18c70 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f  he journal is ho
18c71 74 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  t...            
18c72 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 62 65  ** This might be
18c73 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76   a false positiv
18c74 65 2e 20 20 42 75 74 20 69 66 20 69 74 20 69 73  e.  But if it is
18c75 2c 20 74 68 65 6e 20 74 68 65 0d 0a 20 20 20 20  , then the..    
18c76 20 20 20 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d          ** autom
18c77 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61  atic journal pla
18c78 79 62 61 63 6b 20 61 6e 64 20 72 65 63 6f 76 65  yback and recove
18c79 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c  ry mechanism wil
18c7a 6c 20 64 65 61 6c 0d 0a 20 20 20 20 20 20 20 20  l deal..        
18c7b 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74 20 75      ** with it u
18c7c 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53 49 56  nder an EXCLUSIV
18c7d 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 65 20  E lock where we 
18c7e 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0d 0a  do not need to..
18c7f 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77              ** w
18c80 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69 74  orry so much wit
18c81 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  h race condition
18c82 73 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  s...            
18c83 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
18c84 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0d 0a 20  *pExists = 1;.. 
18c85 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
18c86 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20  SQLITE_OK;..    
18c87 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
18c88 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
18c89 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74   }..  }....  ret
18c8a 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
18c8b 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
18c8c 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
18c8d 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20  obtain a shared 
18c8e 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
18c8f 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 49  base file...** I
18c90 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
18c91 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
18c92 72 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c  rAcquire() until
18c93 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
18c94 74 69 6f 6e 0d 0a 2a 2a 20 68 61 73 20 62 65 65  tion..** has bee
18c95 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63  n successfully c
18c96 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72  alled. If a shar
18c97 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  ed-lock is alrea
18c98 64 79 20 68 65 6c 64 20 77 68 65 6e 0d 0a 2a 2a  dy held when..**
18c99 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
18c9a 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20  s called, it is 
18c9b 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a  a no-op...**..**
18c9c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   The following o
18c9d 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6c  perations are al
18c9e 73 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20  so performed by 
18c9f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  this function...
18ca0 2a 2a 0d 0a 2a 2a 20 20 20 31 29 20 49 66 20 74  **..**   1) If t
18ca1 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72  he pager is curr
18ca2 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 5f 4f  ently in PAGER_O
18ca3 50 45 4e 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f  PEN state (no lo
18ca4 63 6b 20 68 65 6c 64 0d 0a 2a 2a 20 20 20 20 20  ck held..**     
18ca5 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
18ca6 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e 20   file), then an 
18ca7 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
18ca8 74 6f 20 6f 62 74 61 69 6e 20 61 0d 0a 2a 2a 20  to obtain a..** 
18ca9 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b       SHARED lock
18caa 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
18cab 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65   file. Immediate
18cac 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69  ly after obtaini
18cad 6e 67 0d 0a 2a 2a 20 20 20 20 20 20 74 68 65 20  ng..**      the 
18cae 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65  SHARED lock, the
18caf 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 20   file-system is 
18cb0 63 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 6f  checked for a ho
18cb1 74 2d 6a 6f 75 72 6e 61 6c 2c 0d 0a 2a 2a 20 20  t-journal,..**  
18cb2 20 20 20 20 77 68 69 63 68 20 69 73 20 70 6c 61      which is pla
18cb3 79 65 64 20 62 61 63 6b 20 69 66 20 70 72 65 73  yed back if pres
18cb4 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61  ent. Following a
18cb5 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0d  ny hot-journal .
18cb6 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 63  .**      rollbac
18cb7 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  k, the contents 
18cb8 6f 66 20 74 68 65 20 63 61 63 68 65 20 61 72 65  of the cache are
18cb9 20 76 61 6c 69 64 61 74 65 64 20 62 79 20 63 68   validated by ch
18cba 65 63 6b 69 6e 67 0d 0a 2a 2a 20 20 20 20 20 20  ecking..**      
18cbb 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e  the 'change-coun
18cbc 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68  ter' field of th
18cbd 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
18cbe 68 65 61 64 65 72 20 61 6e 64 0d 0a 2a 2a 20 20  header and..**  
18cbf 20 20 20 20 64 69 73 63 61 72 64 65 64 20 69 66      discarded if
18cc0 20 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 20   they are found 
18cc1 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0d 0a  to be invalid...
18cc2 2a 2a 0d 0a 2a 2a 20 20 20 32 29 20 49 66 20 74  **..**   2) If t
18cc3 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e  he pager is runn
18cc4 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65  ing in exclusive
18cc5 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 65  -mode, and there
18cc6 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0d 0a   are currently..
18cc7 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 74  **      no outst
18cc8 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65  anding reference
18cc9 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c 20  s to any pages, 
18cca 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65 72  and is in the er
18ccb 72 6f 72 20 73 74 61 74 65 2c 0d 0a 2a 2a 20 20  ror state,..**  
18ccc 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65      then an atte
18ccd 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63  mpt is made to c
18cce 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
18ccf 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69  tate by discardi
18cd0 6e 67 0d 0a 2a 2a 20 20 20 20 20 20 74 68 65 20  ng..**      the 
18cd1 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
18cd2 70 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72  page cache and r
18cd3 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20  olling back any 
18cd4 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a  open journal..**
18cd5 20 20 20 20 20 20 66 69 6c 65 2e 0d 0a 2a 2a 0d        file...**.
18cd6 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e  .** If everythin
18cd7 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
18cd8 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
18cd9 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f  turned. If an IO
18cda 20 65 72 72 6f 72 20 0d 0a 2a 2a 20 6f 63 63 75   error ..** occu
18cdb 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67  rs while locking
18cdc 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 63   the database, c
18cdd 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f  hecking for a ho
18cde 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  t-journal file o
18cdf 72 20 0d 0a 2a 2a 20 72 6f 6c 6c 69 6e 67 20 62  r ..** rolling b
18ce0 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ack a journal fi
18ce1 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  le, the IO error
18ce2 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
18ce3 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
18ce4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18ce5 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63  e3PagerSharedLoc
18ce6 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  k(Pager *pPager)
18ce7 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  {..  int rc = SQ
18ce8 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
18ce9 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
18cea 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 2f  n code */....  /
18ceb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
18cec 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
18ced 6f 6d 20 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e  om b-tree and on
18cee 6c 79 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  ly when there ar
18cef 65 20 6e 6f 0d 0a 20 20 2a 2a 20 6f 75 74 73 74  e no..  ** outst
18cf0 61 6e 64 69 6e 67 20 70 61 67 65 73 2e 20 54 68  anding pages. Th
18cf1 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20  is implies that 
18cf2 74 68 65 20 70 61 67 65 72 20 73 74 61 74 65 20  the pager state 
18cf3 73 68 6f 75 6c 64 20 65 69 74 68 65 72 0d 0a 20  should either.. 
18cf4 20 2a 2a 20 62 65 20 4f 50 45 4e 20 6f 72 20 52   ** be OPEN or R
18cf5 45 41 44 45 52 2e 20 52 45 41 44 45 52 20 69 73  EADER. READER is
18cf6 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 69   only possible i
18cf7 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f  f the pager is o
18cf8 72 20 77 61 73 20 69 6e 20 0d 0a 20 20 2a 2a 20  r was in ..  ** 
18cf9 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
18cfa 20 6d 6f 64 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   mode...  */..  
18cfb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
18cfc 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
18cfd 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
18cfe 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
18cff 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
18d00 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20  te(pPager) );.. 
18d01 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
18d02 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f  >eState==PAGER_O
18d03 50 45 4e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65  PEN || pPager->e
18d04 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41  State==PAGER_REA
18d05 44 45 52 20 29 3b 0d 0a 20 20 69 66 28 20 4e 45  DER );..  if( NE
18d06 56 45 52 28 4d 45 4d 44 42 20 26 26 20 70 50 61  VER(MEMDB && pPa
18d07 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 7b  ger->errCode) ){
18d08 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
18d09 65 72 72 43 6f 64 65 3b 20 7d 0d 0a 0d 0a 20 20  errCode; }....  
18d0a 69 66 28 20 21 70 61 67 65 72 55 73 65 57 61 6c  if( !pagerUseWal
18d0b 28 70 50 61 67 65 72 29 20 26 26 20 70 50 61 67  (pPager) && pPag
18d0c 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45  er->eState==PAGE
18d0d 52 5f 4f 50 45 4e 20 29 7b 0d 0a 20 20 20 20 69  R_OPEN ){..    i
18d0e 6e 74 20 62 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d  nt bHotJournal =
18d0f 20 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   1;          /* 
18d10 54 72 75 65 20 69 66 20 74 68 65 72 65 20 65 78  True if there ex
18d11 69 73 74 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e  ists a hot journ
18d12 61 6c 2d 66 69 6c 65 20 2a 2f 0d 0a 0d 0a 20 20  al-file */....  
18d13 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42    assert( !MEMDB
18d14 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
18d15 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c   pPager->noReadl
18d16 6f 63 6b 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72  ock==0 || pPager
18d17 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0d 0a 0d  ->readOnly );...
18d18 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
18d19 3e 6e 6f 52 65 61 64 6c 6f 63 6b 3d 3d 30 20 29  >noReadlock==0 )
18d1a 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  {..      rc = pa
18d1b 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
18d1c 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f  (pPager, SHARED_
18d1d 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 20 20 69 66  LOCK);..      if
18d1e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18d1f 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
18d20 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63  rt( pPager->eLoc
18d21 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 70 50  k==NO_LOCK || pP
18d22 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 55 4e 4b  ager->eLock==UNK
18d23 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20  NOWN_LOCK );..  
18d24 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65        goto faile
18d25 64 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  d;..      }..   
18d26 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20   }....    /* If 
18d27 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  a journal file e
18d28 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65  xists, and there
18d29 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20   is no RESERVED 
18d2a 6c 6f 63 6b 20 6f 6e 20 74 68 65 0d 0a 20 20 20  lock on the..   
18d2b 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
18d2c 65 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65  e, then it eithe
18d2d 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c  r needs to be pl
18d2e 61 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c  ayed back or del
18d2f 65 74 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  eted...    */.. 
18d30 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
18d31 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  Lock<=SHARED_LOC
18d32 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  K ){..      rc =
18d33 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70   hasHotJournal(p
18d34 50 61 67 65 72 2c 20 26 62 48 6f 74 4a 6f 75 72  Pager, &bHotJour
18d35 6e 61 6c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  nal);..    }..  
18d36 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18d37 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f  _OK ){..      go
18d38 74 6f 20 66 61 69 6c 65 64 3b 0d 0a 20 20 20 20  to failed;..    
18d39 7d 0d 0a 20 20 20 20 69 66 28 20 62 48 6f 74 4a  }..    if( bHotJ
18d3a 6f 75 72 6e 61 6c 20 29 7b 0d 0a 20 20 20 20 20  ournal ){..     
18d3b 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c 55   /* Get an EXCLU
18d3c 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
18d3d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
18d3e 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74  At this point it
18d3f 20 69 73 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6d   is..      ** im
18d40 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52  portant that a R
18d41 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20  ESERVED lock is 
18d42 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  not obtained on 
18d43 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0d 0a  the way to the..
18d44 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49        ** EXCLUSI
18d45 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77  VE lock. If it w
18d46 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f  ere, another pro
18d47 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20  cess might open 
18d48 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20 64 61  the..      ** da
18d49 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74  tabase file, det
18d4a 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45 44  ect the RESERVED
18d4b 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c   lock, and concl
18d4c 75 64 65 20 74 68 61 74 20 74 68 65 0d 0a 20 20  ude that the..  
18d4d 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
18d4e 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  is safe to read 
18d4f 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65  while this proce
18d50 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c  ss is still roll
18d51 69 6e 67 20 74 68 65 20 0d 0a 20 20 20 20 20 20  ing the ..      
18d52 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62  ** hot-journal b
18d53 61 63 6b 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 0d  ack...      ** .
18d54 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73  .      ** Becaus
18d55 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61  e the intermedia
18d56 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b  te RESERVED lock
18d57 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65   is not requeste
18d58 64 2c 20 61 6e 79 0d 0a 20 20 20 20 20 20 2a 2a  d, any..      **
18d59 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61   other process a
18d5a 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63  ttempting to acc
18d5b 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
18d5c 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74   file will get t
18d5d 6f 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 69  o ..      ** thi
18d5e 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63  s point in the c
18d5f 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20  ode and fail to 
18d60 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45  obtain its own E
18d61 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0d 0a  XCLUSIVE lock ..
18d62 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20        ** on the 
18d63 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
18d64 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
18d65 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61  ** Unless the pa
18d66 67 65 72 20 69 73 20 69 6e 20 6c 6f 63 6b 69 6e  ger is in lockin
18d67 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65  g_mode=exclusive
18d68 20 6d 6f 64 65 2c 20 74 68 65 20 6c 6f 63 6b 20   mode, the lock 
18d69 69 73 0d 0a 20 20 20 20 20 20 2a 2a 20 64 6f 77  is..      ** dow
18d6a 6e 67 72 61 64 65 64 20 74 6f 20 53 48 41 52 45  ngraded to SHARE
18d6b 44 5f 4c 4f 43 4b 20 62 65 66 6f 72 65 20 74 68  D_LOCK before th
18d6c 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
18d6d 72 6e 73 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  rns...      */..
18d6e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
18d6f 4c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 45  LockDb(pPager, E
18d70 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0d  XCLUSIVE_LOCK);.
18d71 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
18d72 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
18d73 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
18d74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 0d 0a 20  ;..      }.. .. 
18d75 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73       /* If it is
18d76 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65   not already ope
18d77 6e 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 65  n and the file e
18d78 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 6f  xists on disk, o
18d79 70 65 6e 20 74 68 65 20 0d 0a 20 20 20 20 20 20  pen the ..      
18d7a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72  ** journal for r
18d7b 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
18d7c 2e 20 57 72 69 74 65 20 61 63 63 65 73 73 20 69  . Write access i
18d7d 73 20 72 65 71 75 69 72 65 64 20 62 65 63 61 75  s required becau
18d7e 73 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6e  se ..      ** in
18d7f 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73   exclusive-acces
18d80 73 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20  s mode the file 
18d81 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20  descriptor will 
18d82 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 0d 0a 20  be kept open .. 
18d83 20 20 20 20 20 2a 2a 20 61 6e 64 20 70 6f 73 73       ** and poss
18d84 69 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 20  ibly used for a 
18d85 74 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65  transaction late
18d86 72 20 6f 6e 2e 20 41 6c 73 6f 2c 20 77 72 69 74  r on. Also, writ
18d87 65 2d 61 63 63 65 73 73 20 0d 0a 20 20 20 20 20  e-access ..     
18d88 20 2a 2a 20 69 73 20 75 73 75 61 6c 6c 79 20 72   ** is usually r
18d89 65 71 75 69 72 65 64 20 74 6f 20 66 69 6e 61 6c  equired to final
18d8a 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
18d8b 69 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  in journal_mode=
18d8c 70 65 72 73 69 73 74 20 0d 0a 20 20 20 20 20 20  persist ..      
18d8d 2a 2a 20 6d 6f 64 65 20 28 61 6e 64 20 61 6c 73  ** mode (and als
18d8e 6f 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  o for journal_mo
18d8f 64 65 3d 74 72 75 6e 63 61 74 65 20 6f 6e 20 73  de=truncate on s
18d90 6f 6d 65 20 73 79 73 74 65 6d 73 29 2e 0d 0a 20  ome systems)... 
18d91 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a       **..      *
18d92 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
18d93 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
18d94 20 69 74 20 75 73 75 61 6c 6c 79 20 6d 65 61 6e   it usually mean
18d95 73 20 74 68 61 74 20 73 6f 6d 65 20 0d 0a 20 20  s that some ..  
18d96 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e      ** other con
18d97 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 20  nection managed 
18d98 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72 6f  to get in and ro
18d99 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f 72  ll it back befor
18d9a 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 69  e ..      ** thi
18d9b 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74  s connection obt
18d9c 61 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73  ained the exclus
18d9d 69 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20  ive lock above. 
18d9e 4f 72 2c 20 69 74 20 0d 0a 20 20 20 20 20 20 2a  Or, it ..      *
18d9f 2a 20 6d 61 79 20 6d 65 61 6e 20 74 68 61 74 20  * may mean that 
18da0 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e  the pager was in
18da1 20 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65   the error-state
18da2 20 77 68 65 6e 20 74 68 69 73 0d 0a 20 20 20 20   when this..    
18da3 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61    ** function wa
18da4 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 65  s called and the
18da5 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f   journal file do
18da6 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0d 0a 20  es not exist... 
18da7 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69       */..      i
18da8 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  f( !isOpen(pPage
18da9 72 2d 3e 6a 66 64 29 20 29 7b 0d 0a 20 20 20 20  r->jfd) ){..    
18daa 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20      sqlite3_vfs 
18dab 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70  * const pVfs = p
18dac 50 61 67 65 72 2d 3e 70 56 66 73 3b 0d 0a 20 20  Pager->pVfs;..  
18dad 20 20 20 20 20 20 69 6e 74 20 62 45 78 69 73 74        int bExist
18dae 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
18daf 2f 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72 6e  /* True if journ
18db0 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 2a  al file exists *
18db1 2f 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  /..        rc = 
18db2 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
18db3 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 56  ..            pV
18db4 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
18db5 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  rnal, SQLITE_ACC
18db6 45 53 53 5f 45 58 49 53 54 53 2c 20 26 62 45 78  ESS_EXISTS, &bEx
18db7 69 73 74 73 29 3b 0d 0a 20 20 20 20 20 20 20 20  ists);..        
18db8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18db9 4b 20 26 26 20 62 45 78 69 73 74 73 20 29 7b 0d  K && bExists ){.
18dba 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66  .          int f
18dbb 6f 75 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  out = 0;..      
18dbc 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49      int f = SQLI
18dbd 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
18dbe 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  E|SQLITE_OPEN_MA
18dbf 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0d 0a 20 20 20  IN_JOURNAL;..   
18dc0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
18dc1 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
18dc2 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   );..          r
18dc3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
18dc4 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
18dc5 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
18dc6 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29  ->jfd, f, &fout)
18dc7 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ;..          ass
18dc8 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
18dc9 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61  OK || isOpen(pPa
18dca 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0d 0a 20 20  ger->jfd) );..  
18dcb 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
18dcc 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75  SQLITE_OK && fou
18dcd 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  t&SQLITE_OPEN_RE
18dce 41 44 4f 4e 4c 59 20 29 7b 0d 0a 20 20 20 20 20  ADONLY ){..     
18dcf 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
18dd0 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54  TE_CANTOPEN_BKPT
18dd1 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ;..            s
18dd2 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
18dd3 61 67 65 72 2d 3e 6a 66 64 29 3b 0d 0a 20 20 20  ager->jfd);..   
18dd4 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
18dd5 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 0d    }..      }.. .
18dd6 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61  .      /* Playba
18dd7 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ck and delete th
18dd8 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70  e journal.  Drop
18dd9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72   the database wr
18dda 69 74 65 0d 0a 20 20 20 20 20 20 2a 2a 20 6c 6f  ite..      ** lo
18ddb 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65  ck and reacquire
18ddc 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20   the read lock. 
18ddd 50 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20  Purge the cache 
18dde 62 65 66 6f 72 65 0d 0a 20 20 20 20 20 20 2a 2a  before..      **
18ddf 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68   playing back th
18de0 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f  e hot-journal so
18de1 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65   that we don't e
18de2 6e 64 20 75 70 20 77 69 74 68 0d 0a 20 20 20 20  nd up with..    
18de3 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73    ** an inconsis
18de4 74 65 6e 74 20 63 61 63 68 65 2e 20 20 53 79 6e  tent cache.  Syn
18de5 63 20 74 68 65 20 68 6f 74 20 6a 6f 75 72 6e 61  c the hot journa
18de6 6c 20 62 65 66 6f 72 65 20 70 6c 61 79 69 6e 67  l before playing
18de7 0d 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 62 61  ..      ** it ba
18de8 63 6b 20 73 69 6e 63 65 20 74 68 65 20 70 72 6f  ck since the pro
18de9 63 65 73 73 20 74 68 61 74 20 63 72 61 73 68 65  cess that crashe
18dea 64 20 61 6e 64 20 6c 65 66 74 20 74 68 65 20 68  d and left the h
18deb 6f 74 20 6a 6f 75 72 6e 61 6c 0d 0a 20 20 20 20  ot journal..    
18dec 20 20 2a 2a 20 70 72 6f 62 61 62 6c 79 20 64 69    ** probably di
18ded 64 20 6e 6f 74 20 73 79 6e 63 20 69 74 20 61 6e  d not sync it an
18dee 64 20 77 65 20 61 72 65 20 72 65 71 75 69 72 65  d we are require
18def 64 20 74 6f 20 61 6c 77 61 79 73 20 73 79 6e 63  d to always sync
18df0 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a  ..      ** the j
18df1 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 70 6c  ournal before pl
18df2 61 79 69 6e 67 20 69 74 20 62 61 63 6b 2e 0d 0a  aying it back...
18df3 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
18df4 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
18df5 72 2d 3e 6a 66 64 29 20 29 7b 0d 0a 20 20 20 20  r->jfd) ){..    
18df6 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
18df7 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20  SQLITE_OK );..  
18df8 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
18df9 53 79 6e 63 48 6f 74 4a 6f 75 72 6e 61 6c 28 70  SyncHotJournal(p
18dfa 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 20 20 20  Pager);..       
18dfb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
18dfc 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
18dfd 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
18dfe 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 3b  back(pPager, 1);
18dff 0d 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ..          pPag
18e00 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47  er->eState = PAG
18e01 45 52 5f 4f 50 45 4e 3b 0d 0a 20 20 20 20 20 20  ER_OPEN;..      
18e02 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65    }..      }else
18e03 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78   if( !pPager->ex
18e04 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0d 0a  clusiveMode ){..
18e05 20 20 20 20 20 20 20 20 70 61 67 65 72 55 6e 6c          pagerUnl
18e06 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48  ockDb(pPager, SH
18e07 41 52 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20  ARED_LOCK);..   
18e08 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 69 66     }....      if
18e09 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18e0a 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  ){..        /* T
18e0b 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61  his branch is ta
18e0c 6b 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ken if an error 
18e0d 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79  occurs while try
18e0e 69 6e 67 20 74 6f 20 6f 70 65 6e 0d 0a 20 20 20  ing to open..   
18e0f 20 20 20 20 20 2a 2a 20 6f 72 20 72 6f 6c 6c 20       ** or roll 
18e10 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  back a hot-journ
18e11 61 6c 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67  al while holding
18e12 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
18e13 63 6b 2e 20 54 68 65 0d 0a 20 20 20 20 20 20 20  ck. The..       
18e14 20 2a 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b   ** pager_unlock
18e15 28 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  () routine will 
18e16 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
18e17 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 75 6e   returning to un
18e18 6c 6f 63 6b 0d 0a 20 20 20 20 20 20 20 20 2a 2a  lock..        **
18e19 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68   the file. If th
18e1a 65 20 75 6e 6c 6f 63 6b 20 61 74 74 65 6d 70 74  e unlock attempt
18e1b 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 50 61 67   fails, then Pag
18e1c 65 72 2e 65 4c 6f 63 6b 20 6d 75 73 74 20 62 65  er.eLock must be
18e1d 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74  ..        ** set
18e1e 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b   to UNKNOWN_LOCK
18e1f 20 28 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e   (see the commen
18e20 74 20 61 62 6f 76 65 20 74 68 65 20 23 64 65 66  t above the #def
18e21 69 6e 65 20 66 6f 72 20 0d 0a 20 20 20 20 20 20  ine for ..      
18e22 20 20 2a 2a 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43    ** UNKNOWN_LOC
18e23 4b 20 61 62 6f 76 65 20 66 6f 72 20 61 6e 20 65  K above for an e
18e24 78 70 6c 61 6e 61 74 69 6f 6e 29 2e 20 0d 0a 20  xplanation). .. 
18e25 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20         **..     
18e26 20 20 20 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74     ** In order t
18e27 6f 20 67 65 74 20 70 61 67 65 72 5f 75 6e 6c 6f  o get pager_unlo
18e28 63 6b 28 29 20 74 6f 20 64 6f 20 74 68 69 73 2c  ck() to do this,
18e29 20 73 65 74 20 50 61 67 65 72 2e 65 53 74 61 74   set Pager.eStat
18e2a 65 20 74 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a  e to..        **
18e2b 20 50 41 47 45 52 5f 45 52 52 4f 52 20 6e 6f 77   PAGER_ERROR now
18e2c 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 63  . This is not ac
18e2d 74 75 61 6c 6c 79 20 63 6f 75 6e 74 65 64 20 61  tually counted a
18e2e 73 20 61 20 74 72 61 6e 73 69 74 69 6f 6e 0d 0a  s a transition..
18e2f 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 45 52          ** to ER
18e30 52 4f 52 20 73 74 61 74 65 20 69 6e 20 74 68 65  ROR state in the
18e31 20 73 74 61 74 65 20 64 69 61 67 72 61 6d 20 61   state diagram a
18e32 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69  t the top of thi
18e33 73 20 66 69 6c 65 2c 0d 0a 20 20 20 20 20 20 20  s file,..       
18e34 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 6b 6e 6f   ** since we kno
18e35 77 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20  w that the same 
18e36 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e  call to pager_un
18e37 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 76 65 72 79  lock() will very
18e38 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 68 6f  ..        ** sho
18e39 72 74 6c 79 20 74 72 61 6e 73 69 74 69 6f 6e 20  rtly transition 
18e3a 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74  the pager object
18e3b 20 74 6f 20 74 68 65 20 4f 50 45 4e 20 73 74 61   to the OPEN sta
18e3c 74 65 2e 20 43 61 6c 6c 69 6e 67 0d 0a 20 20 20  te. Calling..   
18e3d 20 20 20 20 20 2a 2a 20 61 73 73 65 72 74 5f 70       ** assert_p
18e3e 61 67 65 72 5f 73 74 61 74 65 28 29 20 77 6f 75  ager_state() wou
18e3f 6c 64 20 66 61 69 6c 20 6e 6f 77 2c 20 61 73 20  ld fail now, as 
18e40 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  it should not be
18e41 20 70 6f 73 73 69 62 6c 65 0d 0a 20 20 20 20 20   possible..     
18e42 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 20 45     ** to be in E
18e43 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20  RROR state when 
18e44 74 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 6f  there are zero o
18e45 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
18e46 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 66  ..        ** ref
18e47 65 72 65 6e 63 65 73 2e 0d 0a 20 20 20 20 20 20  erences...      
18e48 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 70 61    */..        pa
18e49 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
18e4a 2c 20 72 63 29 3b 0d 0a 20 20 20 20 20 20 20 20  , rc);..        
18e4b 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0d 0a 20 20  goto failed;..  
18e4c 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 61      }....      a
18e4d 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
18e4e 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45  State==PAGER_OPE
18e4f 4e 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  N );..      asse
18e50 72 74 28 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f  rt( (pPager->eLo
18e51 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29  ck==SHARED_LOCK)
18e52 0d 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  ..           || 
18e53 28 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  (pPager->exclusi
18e54 76 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72  veMode && pPager
18e55 2d 3e 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c  ->eLock>SHARED_L
18e56 4f 43 4b 29 0d 0a 20 20 20 20 20 20 29 3b 0d 0a  OCK)..      );..
18e57 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28      }....    if(
18e58 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
18e59 6c 65 20 0d 0a 20 20 20 20 20 26 26 20 28 70 50  le ..     && (pP
18e5a 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 7c 7c  ager->pBackup ||
18e5b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
18e5c 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  gecount(pPager->
18e5d 70 50 43 61 63 68 65 29 3e 30 29 20 0d 0a 20 20  pPCache)>0) ..  
18e5e 20 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54    ){..      /* T
18e5f 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68  he shared-lock h
18e60 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71  as just been acq
18e61 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74  uired on the dat
18e62 61 62 61 73 65 20 66 69 6c 65 0d 0a 20 20 20 20  abase file..    
18e63 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
18e64 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 73  re already pages
18e65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 66   in the cache (f
18e66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0d 0a  rom a previous..
18e67 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72        ** read or
18e68 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
18e69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  on).  Check to s
18e6a 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ee if the databa
18e6b 73 65 0d 0a 20 20 20 20 20 20 2a 2a 20 68 61 73  se..      ** has
18e6c 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20   been modified. 
18e6d 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
18e6e 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c   has changed, fl
18e6f 75 73 68 20 74 68 65 0d 0a 20 20 20 20 20 20 2a  ush the..      *
18e70 2a 20 63 61 63 68 65 2e 0d 0a 20 20 20 20 20 20  * cache...      
18e71 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 44 61 74  **..      ** Dat
18e72 61 62 61 73 65 20 63 68 61 6e 67 65 73 20 69 73  abase changes is
18e73 20 64 65 74 65 63 74 65 64 20 62 79 20 6c 6f 6f   detected by loo
18e74 6b 69 6e 67 20 61 74 20 31 35 20 62 79 74 65 73  king at 15 bytes
18e75 20 62 65 67 69 6e 6e 69 6e 67 0d 0a 20 20 20 20   beginning..    
18e76 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 20 32    ** at offset 2
18e77 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e  4 into the file.
18e78 20 20 54 68 65 20 66 69 72 73 74 20 34 20 6f 66    The first 4 of
18e79 20 74 68 65 73 65 20 31 36 20 62 79 74 65 73 20   these 16 bytes 
18e7a 61 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 61 20  are..      ** a 
18e7b 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20 74  32-bit counter t
18e7c 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  hat is increment
18e7d 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68 61  ed with each cha
18e7e 6e 67 65 2e 20 20 54 68 65 0d 0a 20 20 20 20 20  nge.  The..     
18e7f 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20   ** other bytes 
18e80 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20  change randomly 
18e81 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63  with each file c
18e82 68 61 6e 67 65 20 77 68 65 6e 0d 0a 20 20 20 20  hange when..    
18e83 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20    ** a codec is 
18e84 69 6e 20 75 73 65 2e 0d 0a 20 20 20 20 20 20 2a  in use...      *
18e85 2a 20 0d 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  * ..      ** The
18e86 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e  re is a vanishin
18e87 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65  gly small chance
18e88 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77   that a change w
18e89 69 6c 6c 20 6e 6f 74 20 62 65 20 0d 0a 20 20 20  ill not be ..   
18e8a 20 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20     ** detected. 
18e8b 20 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61   The chance of a
18e8c 6e 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61  n undetected cha
18e8d 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20  nge is so small 
18e8e 74 68 61 74 0d 0a 20 20 20 20 20 20 2a 2a 20 69  that..      ** i
18e8f 74 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74  t can be neglect
18e90 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  ed...      */.. 
18e91 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 20       Pgno nPage 
18e92 3d 20 30 3b 0d 0a 20 20 20 20 20 20 63 68 61 72  = 0;..      char
18e93 20 64 62 46 69 6c 65 56 65 72 73 5b 73 69 7a 65   dbFileVers[size
18e94 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  of(pPager->dbFil
18e95 65 56 65 72 73 29 5d 3b 0d 0a 0d 0a 20 20 20 20  eVers)];....    
18e96 20 20 72 63 20 3d 20 70 61 67 65 72 50 61 67 65    rc = pagerPage
18e97 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e  count(pPager, &n
18e98 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 69 66  Page);..      if
18e99 28 20 72 63 20 29 20 67 6f 74 6f 20 66 61 69 6c  ( rc ) goto fail
18e9a 65 64 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28  ed;....      if(
18e9b 20 6e 50 61 67 65 3e 30 20 29 7b 0d 0a 20 20 20   nPage>0 ){..   
18e9c 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43       IOTRACE(("C
18e9d 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22 2c 20  KVERS %p %d\n", 
18e9e 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 64  pPager, sizeof(d
18e9f 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0d 0a 20  bFileVers)));.. 
18ea0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
18ea1 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
18ea2 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 72  ->fd, &dbFileVer
18ea3 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65  s, sizeof(dbFile
18ea4 56 65 72 73 29 2c 20 32 34 29 3b 0d 0a 20 20 20  Vers), 24);..   
18ea5 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18ea6 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
18ea7 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
18ea8 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
18ea9 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
18eaa 20 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c      memset(dbFil
18eab 65 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66  eVers, 0, sizeof
18eac 28 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0d 0a  (dbFileVers));..
18ead 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
18eae 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67   if( memcmp(pPag
18eaf 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
18eb0 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65  dbFileVers, size
18eb1 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21  of(dbFileVers))!
18eb2 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  =0 ){..        p
18eb3 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
18eb4 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  r);..      }..  
18eb5 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66    }....    /* If
18eb6 20 74 68 65 72 65 20 69 73 20 61 20 57 41 4c 20   there is a WAL 
18eb7 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c 65  file in the file
18eb8 2d 73 79 73 74 65 6d 2c 20 6f 70 65 6e 20 74 68  -system, open th
18eb9 69 73 20 64 61 74 61 62 61 73 65 20 69 6e 20 57  is database in W
18eba 41 4c 0d 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e  AL..    ** mode.
18ebb 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
18ebc 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
18ebd 6f 6e 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  on call is a no-
18ebe 6f 70 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  op...    */..   
18ebf 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 57   rc = pagerOpenW
18ec0 61 6c 49 66 50 72 65 73 65 6e 74 28 70 50 61 67  alIfPresent(pPag
18ec1 65 72 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  er);..#ifndef SQ
18ec2 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20  LITE_OMIT_WAL.. 
18ec3 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
18ec4 72 2d 3e 70 57 61 6c 3d 3d 30 20 7c 7c 20 72 63  r->pWal==0 || rc
18ec5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a  ==SQLITE_OK );..
18ec6 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20  #endif..  }.... 
18ec7 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c   if( pagerUseWal
18ec8 28 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20 20  (pPager) ){..   
18ec9 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
18eca 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 72  ITE_OK );..    r
18ecb 63 20 3d 20 70 61 67 65 72 42 65 67 69 6e 52 65  c = pagerBeginRe
18ecc 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  adTransaction(pP
18ecd 61 67 65 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ager);..  }.... 
18ece 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 74   if( pPager->eSt
18ecf 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20  ate==PAGER_OPEN 
18ed0 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
18ed1 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 70 61   ){..    rc = pa
18ed2 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
18ed3 67 65 72 2c 20 26 70 50 61 67 65 72 2d 3e 64 62  ger, &pPager->db
18ed4 53 69 7a 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Size);..  }.... 
18ed5 66 61 69 6c 65 64 3a 0d 0a 20 20 69 66 28 20 72  failed:..  if( r
18ed6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
18ed7 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45  .    assert( !ME
18ed8 4d 44 42 20 29 3b 0d 0a 20 20 20 20 70 61 67 65  MDB );..    page
18ed9 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29  r_unlock(pPager)
18eda 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
18edb 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
18edc 41 47 45 52 5f 4f 50 45 4e 20 29 3b 0d 0a 20 20  AGER_OPEN );..  
18edd 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 50 61 67  }else{..    pPag
18ede 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47  er->eState = PAG
18edf 45 52 5f 52 45 41 44 45 52 3b 0d 0a 20 20 7d 0d  ER_READER;..  }.
18ee0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
18ee1 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  ..../*..** If th
18ee2 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
18ee3 74 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65  t has reached ze
18ee4 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79  ro, rollback any
18ee5 20 61 63 74 69 76 65 0d 0a 2a 2a 20 74 72 61 6e   active..** tran
18ee6 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f  saction and unlo
18ee7 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0d 0a 2a  ck the pager...*
18ee8 2a 0d 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e  *..** Except, in
18ee9 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58   locking_mode=EX
18eea 43 4c 55 53 49 56 45 20 77 68 65 6e 20 74 68 65  CLUSIVE when the
18eeb 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f  re is nothing to
18eec 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c   in..** the roll
18eed 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68  back journal, th
18eee 65 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  e unlock is not 
18eef 70 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 74 68  performed and th
18ef0 65 72 65 20 69 73 0d 0a 2a 2a 20 6e 6f 74 68 69  ere is..** nothi
18ef1 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20  ng to rollback, 
18ef2 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  so this routine 
18ef3 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 20  is a no-op...*/ 
18ef4 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  ..static void pa
18ef5 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
18ef6 64 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  d(Pager *pPager)
18ef7 7b 0d 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65  {..  if( (sqlite
18ef8 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
18ef9 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
18efa 3d 3d 30 29 20 29 7b 0d 0a 20 20 20 20 70 61 67  ==0) ){..    pag
18efb 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
18efc 61 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20  ack(pPager);..  
18efd 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  }..}..../*..** A
18efe 63 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e  cquire a referen
18eff 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65  ce to page numbe
18f00 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20  r pgno in pager 
18f01 70 50 61 67 65 72 20 28 61 20 70 61 67 65 0d 0a  pPager (a page..
18f02 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61 73  ** reference has
18f03 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20   type DbPage*). 
18f04 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
18f05 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0d 0a   reference is ..
18f06 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ** successfully 
18f07 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20  obtained, it is 
18f08 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67  copied to *ppPag
18f09 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
18f0a 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a  returned...**..*
18f0b 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74  * If the request
18f0c 65 64 20 70 61 67 65 20 69 73 20 61 6c 72 65 61  ed page is alrea
18f0d 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  dy in the cache,
18f0e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
18f0f 20 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c   ..** Otherwise,
18f10 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65   a new page obje
18f11 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ct is allocated 
18f12 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69  and populated wi
18f13 74 68 20 64 61 74 61 0d 0a 2a 2a 20 72 65 61 64  th data..** read
18f14 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
18f15 73 65 20 66 69 6c 65 2e 20 49 6e 20 73 6f 6d 65  se file. In some
18f16 20 63 61 73 65 73 2c 20 74 68 65 20 70 63 61 63   cases, the pcac
18f17 68 65 20 6d 6f 64 75 6c 65 20 6d 61 79 0d 0a 2a  he module may..*
18f18 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 20 74 6f 20  * choose not to 
18f19 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
18f1a 61 67 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 6d  age object and m
18f1b 61 79 20 72 65 75 73 65 20 61 6e 20 65 78 69 73  ay reuse an exis
18f1c 74 69 6e 67 0d 0a 2a 2a 20 6f 62 6a 65 63 74 20  ting..** object 
18f1d 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 61 6e 64  with no outstand
18f1e 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 0d  ing references..
18f1f 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 65 78 74 72  .**..** The extr
18f20 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 20  a data appended 
18f21 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c 77  to a page is alw
18f22 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ays initialized 
18f23 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0d 0a 2a  to zeros the ..*
18f24 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70  * first time a p
18f25 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e  age is loaded in
18f26 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68  to memory. If th
18f27 65 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64  e page requested
18f28 20 69 73 20 0d 0a 2a 2a 20 61 6c 72 65 61 64 79   is ..** already
18f29 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 68   in the cache wh
18f2a 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
18f2b 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e   is called, then
18f2c 20 74 68 65 20 65 78 74 72 61 0d 0a 2a 2a 20 64   the extra..** d
18f2d 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 20 69  ata is left as i
18f2e 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 70  t was when the p
18f2f 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c  age object was l
18f30 61 73 74 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a  ast used...**..*
18f31 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
18f32 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c  e image is small
18f33 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75  er than the requ
18f34 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66  ested page or if
18f35 20 61 20 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f   a ..** non-zero
18f36 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64   value is passed
18f37 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e   as the noConten
18f38 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20  t parameter and 
18f39 74 68 65 20 0d 0a 2a 2a 20 72 65 71 75 65 73 74  the ..** request
18f3a 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  ed page is not a
18f3b 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e  lready stored in
18f3c 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e   the cache, then
18f3d 20 6e 6f 20 0d 0a 2a 2a 20 61 63 74 75 61 6c 20   no ..** actual 
18f3e 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73  disk read occurs
18f3f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
18f40 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20  he memory image 
18f41 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 70 61 67 65  of the ..** page
18f42 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
18f43 74 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0d 0a  to all zeros. ..
18f44 2a 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74  **..** If noCont
18f45 65 6e 74 20 69 73 20 74 72 75 65 2c 20 69 74 20  ent is true, it 
18f46 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f  means that we do
18f47 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20   not care about 
18f48 74 68 65 20 63 6f 6e 74 65 6e 74 73 0d 0a 2a 2a  the contents..**
18f49 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 54 68   of the page. Th
18f4a 69 73 20 6f 63 63 75 72 73 20 69 6e 20 74 77 6f  is occurs in two
18f4b 20 73 65 70 65 72 61 74 65 20 73 63 65 6e 61 72   seperate scenar
18f4c 69 6f 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 61  ios:..**..**   a
18f4d 29 20 57 68 65 6e 20 72 65 61 64 69 6e 67 20 61  ) When reading a
18f4e 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
18f4f 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61  page from the da
18f50 74 61 62 61 73 65 2c 20 61 6e 64 0d 0a 2a 2a 0d  tabase, and..**.
18f51 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61 20  .**   b) When a 
18f52 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65 69  savepoint is bei
18f53 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  ng rolled back a
18f54 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c 6f  nd we need to lo
18f55 61 64 0d 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65  ad..**      a ne
18f56 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  w page into the 
18f57 63 61 63 68 65 20 74 6f 20 62 65 20 66 69 6c 6c  cache to be fill
18f58 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
18f59 20 72 65 61 64 0d 0a 2a 2a 20 20 20 20 20 20 66   read..**      f
18f5a 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e  rom the savepoin
18f5b 74 20 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a 0d 0a  t journal...**..
18f5c 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20  ** If noContent 
18f5d 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
18f5e 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  e data returned 
18f5f 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61  is zeroed instea
18f60 64 20 6f 66 0d 0a 2a 2a 20 62 65 69 6e 67 20 72  d of..** being r
18f61 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ead from the dat
18f62 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61  abase. Additiona
18f63 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f  lly, the bits co
18f64 72 72 65 73 70 6f 6e 64 69 6e 67 0d 0a 2a 2a 20  rresponding..** 
18f65 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72  to pgno in Pager
18f66 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74  .pInJournal (bit
18f67 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72  vec of pages alr
18f68 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20  eady written to 
18f69 74 68 65 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  the..** journal 
18f6a 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61  file) and the Pa
18f6b 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e  gerSavepoint.pIn
18f6c 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  Savepoint bitvec
18f6d 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0d 0a 2a  s of any open..*
18f6e 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65  * savepoints are
18f6f 20 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73   set. This means
18f70 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
18f71 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74  made writable at
18f72 20 61 6e 79 0d 0a 2a 2a 20 70 6f 69 6e 74 20 69   any..** point i
18f73 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20 75 73  n the future, us
18f74 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
18f75 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
18f76 29 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0d  ), its contents.
18f77 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  .** will not be 
18f78 6a 6f 75 72 6e 61 6c 65 64 2e 20 54 68 69 73 20  journaled. This 
18f79 73 61 76 65 73 20 49 4f 2e 0d 0a 2a 2a 0d 0a 2a  saves IO...**..*
18f7a 2a 20 54 68 65 20 61 63 71 75 69 73 69 74 69 6f  * The acquisitio
18f7b 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 66 6f 72  n might fail for
18f7c 20 73 65 76 65 72 61 6c 20 72 65 61 73 6f 6e 73   several reasons
18f7d 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 2c  .  In all cases,
18f7e 0d 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69  ..** an appropri
18f7f 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ate error code i
18f80 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
18f81 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
18f82 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53   NULL...**..** S
18f83 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50  ee also sqlite3P
18f84 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42  agerLookup().  B
18f85 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65  oth this routine
18f86 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74   and Lookup() at
18f87 74 65 6d 70 74 0d 0a 2a 2a 20 74 6f 20 66 69 6e  tempt..** to fin
18f88 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  d a page in the 
18f89 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20  in-memory cache 
18f8a 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70  first.  If the p
18f8b 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
18f8c 64 79 0d 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79  dy..** in memory
18f8d 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67  , this routine g
18f8e 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72  oes to disk to r
18f8f 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61  ead it in wherea
18f90 73 20 4c 6f 6f 6b 75 70 28 29 0d 0a 2a 2a 20 6a  s Lookup()..** j
18f91 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20  ust returns 0.  
18f92 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71  This routine acq
18f93 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63  uires a read-loc
18f94 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  k the first time
18f95 20 69 74 0d 0a 2a 2a 20 68 61 73 20 74 6f 20 67   it..** has to g
18f96 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63  o to disk, and c
18f97 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61  ould also playba
18f98 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61  ck an old journa
18f99 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0d  l if necessary..
18f9a 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70  .** Since Lookup
18f9b 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f  () never goes to
18f9c 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20   disk, it never 
18f9d 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68  has to deal with
18f9e 20 6c 6f 63 6b 73 0d 0a 2a 2a 20 6f 72 20 6a 6f   locks..** or jo
18f9f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0d 0a 2a 2f  urnal files...*/
18fa0 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
18fa1 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
18fa2 72 41 63 71 75 69 72 65 28 0d 0a 20 20 50 61 67  rAcquire(..  Pag
18fa3 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
18fa4 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70   /* The pager op
18fa5 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  en on the databa
18fa6 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 50 67  se file */..  Pg
18fa7 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
18fa8 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
18fa9 20 74 6f 20 66 65 74 63 68 20 2a 2f 0d 0a 20 20   to fetch */..  
18faa 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  DbPage **ppPage,
18fab 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70      /* Write a p
18fac 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
18fad 67 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e  ge here */..  in
18fae 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20  t noContent     
18faf 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68    /* Do not both
18fb0 65 72 20 72 65 61 64 69 6e 67 20 63 6f 6e 74 65  er reading conte
18fb1 6e 74 20 66 72 6f 6d 20 64 69 73 6b 20 69 66 20  nt from disk if 
18fb2 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  true */..){..  i
18fb3 6e 74 20 72 63 3b 0d 0a 20 20 50 67 48 64 72 20  nt rc;..  PgHdr 
18fb4 2a 70 50 67 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  *pPg;....  asser
18fb5 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
18fb6 65 3e 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20  e>=PAGER_READER 
18fb7 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 73  );..  assert( as
18fb8 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
18fb9 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 0d 0a 20  (pPager) );.... 
18fba 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0d   if( pgno==0 ){.
18fbb 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
18fbc 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
18fbd 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ..  }....  /* If
18fbe 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
18fbf 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
18fc0 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
18fc1 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0d  r immediately. .
18fc2 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  .  ** Otherwise,
18fc3 20 72 65 71 75 65 73 74 20 74 68 65 20 70 61 67   request the pag
18fc4 65 20 66 72 6f 6d 20 74 68 65 20 50 43 61 63 68  e from the PCach
18fc5 65 20 6c 61 79 65 72 2e 20 2a 2f 0d 0a 20 20 69  e layer. */..  i
18fc6 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
18fc7 64 65 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  de!=SQLITE_OK ){
18fc8 0d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65  ..    rc = pPage
18fc9 72 2d 3e 65 72 72 43 6f 64 65 3b 0d 0a 20 20 7d  r->errCode;..  }
18fca 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20  else{..    rc = 
18fcb 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
18fcc 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
18fcd 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 70 50  he, pgno, 1, ppP
18fce 61 67 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  age);..  }....  
18fcf 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18fd0 4b 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 45 69 74  K ){..    /* Eit
18fd1 68 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  her the call to 
18fd2 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
18fd3 63 68 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e  ch() returned an
18fd4 20 65 72 72 6f 72 20 6f 72 20 74 68 65 0d 0a 20   error or the.. 
18fd5 20 20 20 2a 2a 20 70 61 67 65 72 20 77 61 73 20     ** pager was 
18fd6 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65  already in the e
18fd7 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e 20  rror-state when 
18fd8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61  this function wa
18fd9 73 20 63 61 6c 6c 65 64 2e 0d 0a 20 20 20 20 2a  s called...    *
18fda 2a 20 53 65 74 20 70 50 67 20 74 6f 20 30 20 61  * Set pPg to 0 a
18fdb 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65  nd jump to the e
18fdc 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 72  xception handler
18fdd 2e 20 20 2a 2f 0d 0a 20 20 20 20 70 50 67 20 3d  .  */..    pPg =
18fde 20 30 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 70 61   0;..    goto pa
18fdf 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b  ger_acquire_err;
18fe0 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
18fe1 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f   (*ppPage)->pgno
18fe2 3d 3d 70 67 6e 6f 20 29 3b 0d 0a 20 20 61 73 73  ==pgno );..  ass
18fe3 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e  ert( (*ppPage)->
18fe4 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c  pPager==pPager |
18fe5 7c 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61  | (*ppPage)->pPa
18fe6 67 65 72 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 69  ger==0 );....  i
18fe7 66 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50  f( (*ppPage)->pP
18fe8 61 67 65 72 20 26 26 20 21 6e 6f 43 6f 6e 74 65  ager && !noConte
18fe9 6e 74 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 6e  nt ){..    /* In
18fea 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 70   this case the p
18feb 63 61 63 68 65 20 61 6c 72 65 61 64 79 20 63 6f  cache already co
18fec 6e 74 61 69 6e 73 20 61 6e 20 69 6e 69 74 69 61  ntains an initia
18fed 6c 69 7a 65 64 20 63 6f 70 79 20 6f 66 0d 0a 20  lized copy of.. 
18fee 20 20 20 2a 2a 20 74 68 65 20 70 61 67 65 2e 20     ** the page. 
18fef 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 66  Return without f
18ff0 75 72 74 68 65 72 20 61 64 6f 2e 20 20 2a 2f 0d  urther ado.  */.
18ff1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
18ff2 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e  o<=PAGER_MAX_PGN
18ff3 4f 20 26 26 20 70 67 6e 6f 21 3d 50 41 47 45 52  O && pgno!=PAGER
18ff4 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
18ff5 20 29 3b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d   );..    pPager-
18ff6 3e 6e 48 69 74 2b 2b 3b 0d 0a 20 20 20 20 72 65  >nHit++;..    re
18ff7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
18ff8 0a 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ...  }else{..   
18ff9 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61   /* The pager ca
18ffa 63 68 65 20 68 61 73 20 63 72 65 61 74 65 64 20  che has created 
18ffb 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20  a new page. Its 
18ffc 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f  content needs to
18ffd 20 0d 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e 69   ..    ** be ini
18ffe 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0d 0a 0d  tialized.  */...
18fff 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70 50 61  .    pPg = *ppPa
19000 67 65 3b 0d 0a 20 20 20 20 70 50 67 2d 3e 70 50  ge;..    pPg->pP
19001 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0d 0a  ager = pPager;..
19002 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78  ..    /* The max
19003 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72  imum page number
19004 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e   is 2^31. Return
19005 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
19006 69 66 20 61 20 70 61 67 65 0d 0a 20 20 20 20 2a  if a page..    *
19007 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72  * number greater
19008 20 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74   than this, or t
19009 68 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e  he unused lockin
1900a 67 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65  g-page, is reque
1900b 73 74 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 69 66  sted. */..    if
1900c 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58  ( pgno>PAGER_MAX
1900d 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50  _PGNO || pgno==P
1900e 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
1900f 67 65 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 72  ger) ){..      r
19010 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
19011 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20  PT_BKPT;..      
19012 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
19013 72 65 5f 65 72 72 3b 0d 0a 20 20 20 20 7d 0d 0a  re_err;..    }..
19014 0d 0a 20 20 20 20 69 66 28 20 4d 45 4d 44 42 20  ..    if( MEMDB 
19015 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  || pPager->dbSiz
19016 65 3c 70 67 6e 6f 20 7c 7c 20 6e 6f 43 6f 6e 74  e<pgno || noCont
19017 65 6e 74 20 7c 7c 20 21 69 73 4f 70 65 6e 28 70  ent || !isOpen(p
19018 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0d 0a 20  Pager->fd) ){.. 
19019 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50       if( pgno>pP
1901a 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0d  ager->mxPgno ){.
1901b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1901c 4c 49 54 45 5f 46 55 4c 4c 3b 0d 0a 20 20 20 20  LITE_FULL;..    
1901d 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61      goto pager_a
1901e 63 71 75 69 72 65 5f 65 72 72 3b 0d 0a 20 20 20  cquire_err;..   
1901f 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
19020 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0d 0a 20 20  noContent ){..  
19021 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 72 65        /* Failure
19022 20 74 6f 20 73 65 74 20 74 68 65 20 62 69 74 73   to set the bits
19023 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61   in the InJourna
19024 6c 20 62 69 74 2d 76 65 63 74 6f 72 73 20 69 73  l bit-vectors is
19025 20 62 65 6e 69 67 6e 2e 0d 0a 20 20 20 20 20 20   benign...      
19026 20 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d    ** It merely m
19027 65 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67  eans that we mig
19028 68 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61  ht do some extra
19029 20 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c   work to journal
1902a 20 61 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20   a ..        ** 
1902b 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e  page that does n
1902c 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f  ot need to be jo
1902d 75 72 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72 74  urnaled.  Nevert
1902e 68 65 6c 65 73 73 2c 20 62 65 20 73 75 72 65 20  heless, be sure 
1902f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  ..        ** to 
19030 74 65 73 74 20 74 68 65 20 63 61 73 65 20 77 68  test the case wh
19031 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72  ere a malloc err
19032 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
19033 74 72 79 69 6e 67 20 74 6f 20 73 65 74 20 0d 0a  trying to set ..
19034 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62 69 74          ** a bit
19035 20 69 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72   in a bit vector
19036 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
19037 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65         sqlite3Be
19038 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
19039 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
1903a 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62  pgno<=pPager->db
1903b 4f 72 69 67 53 69 7a 65 20 29 7b 0d 0a 20 20 20  OrigSize ){..   
1903c 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28         TESTONLY(
1903d 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42   rc = ) sqlite3B
1903e 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
1903f 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e  >pInJournal, pgn
19040 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 74  o);..          t
19041 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
19042 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20  ITE_NOMEM );..  
19043 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
19044 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20   TESTONLY( rc = 
19045 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74  ) addToSavepoint
19046 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20  Bitvecs(pPager, 
19047 70 67 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20  pgno);..        
19048 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51  testcase( rc==SQ
19049 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20  LITE_NOMEM );.. 
1904a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e         sqlite3En
1904b 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1904c 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
1904d 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61   memset(pPg->pDa
1904e 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70  ta, 0, pPager->p
1904f 61 67 65 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20  ageSize);..     
19050 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52 4f 20   IOTRACE(("ZERO 
19051 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
19052 2c 20 70 67 6e 6f 29 29 3b 0d 0a 20 20 20 20 7d  , pgno));..    }
19053 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 73 73  else{..      ass
19054 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72  ert( pPg->pPager
19055 3d 3d 70 50 61 67 65 72 20 29 3b 0d 0a 20 20 20  ==pPager );..   
19056 20 20 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73     pPager->nMiss
19057 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  ++;..      rc = 
19058 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b  readDbPage(pPg);
19059 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
1905a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1905b 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72        goto pager
1905c 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0d 0a 20  _acquire_err;.. 
1905d 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
1905e 20 20 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67     pager_set_pag
1905f 65 68 61 73 68 28 70 50 67 29 3b 0d 0a 20 20 7d  ehash(pPg);..  }
19060 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ....  return SQL
19061 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 70 61 67 65 72  ITE_OK;....pager
19062 5f 61 63 71 75 69 72 65 5f 65 72 72 3a 0d 0a 20  _acquire_err:.. 
19063 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
19064 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 69 66 28  ITE_OK );..  if(
19065 20 70 50 67 20 29 7b 0d 0a 20 20 20 20 73 71 6c   pPg ){..    sql
19066 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70  ite3PcacheDrop(p
19067 50 67 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 61 67  Pg);..  }..  pag
19068 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64  erUnlockIfUnused
19069 28 70 50 61 67 65 72 29 3b 0d 0a 0d 0a 20 20 2a  (pPager);....  *
1906a 70 70 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 72  ppPage = 0;..  r
1906b 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1906c 2f 2a 0d 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  /*..** Acquire a
1906d 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20 61   page if it is a
1906e 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69 6e  lready in the in
1906f 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20  -memory cache.  
19070 44 6f 0d 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20  Do..** not read 
19071 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69  the page from di
19072 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  sk.  Return a po
19073 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
19074 65 2c 0d 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74  e,..** or 0 if t
19075 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69  he page is not i
19076 6e 20 63 61 63 68 65 2e 20 0d 0a 2a 2a 0d 0a 2a  n cache. ..**..*
19077 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74  * See also sqlit
19078 65 33 50 61 67 65 72 47 65 74 28 29 2e 20 20 54  e3PagerGet().  T
19079 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  he difference be
1907a 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69  tween this routi
1907b 6e 65 0d 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74  ne..** and sqlit
1907c 65 33 50 61 67 65 72 47 65 74 28 29 20 69 73 20  e3PagerGet() is 
1907d 74 68 61 74 20 5f 67 65 74 28 29 20 77 69 6c 6c  that _get() will
1907e 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 20   go to the disk 
1907f 61 6e 64 20 72 65 61 64 0d 0a 2a 2a 20 69 6e 20  and read..** in 
19080 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65 20  the page if the 
19081 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  page is not alre
19082 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20 54  ady in cache.  T
19083 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20  his routine..** 
19084 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20  returns NULL if 
19085 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
19086 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61  in cache or if a
19087 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20   disk I/O error 
19088 0d 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61  ..** has ever ha
19089 70 70 65 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ppened...*/..SQL
1908a 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61  ITE_PRIVATE DbPa
1908b 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ge *sqlite3Pager
1908c 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50  Lookup(Pager *pP
1908d 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  ager, Pgno pgno)
1908e 7b 0d 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20  {..  PgHdr *pPg 
1908f 3d 20 30 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
19090 70 50 61 67 65 72 21 3d 30 20 29 3b 0d 0a 20 20  pPager!=0 );..  
19091 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20  assert( pgno!=0 
19092 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  );..  assert( pP
19093 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 3d 30  ager->pPCache!=0
19094 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
19095 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50  Pager->eState>=P
19096 41 47 45 52 5f 52 45 41 44 45 52 20 26 26 20 70  AGER_READER && p
19097 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50  Pager->eState!=P
19098 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0d 0a 20  AGER_ERROR );.. 
19099 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65   sqlite3PcacheFe
1909a 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61  tch(pPager->pPCa
1909b 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70  che, pgno, 0, &p
1909c 50 67 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70  Pg);..  return p
1909d 50 67 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  Pg;..}..../*..**
1909e 20 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 20   Release a page 
1909f 72 65 66 65 72 65 6e 63 65 2e 0d 0a 2a 2a 0d 0a  reference...**..
190a0 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72  ** If the number
190a1 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
190a2 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 20  o the page drop 
190a3 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  to zero, then th
190a4 65 0d 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64  e..** page is ad
190a5 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c  ded to the LRU l
190a6 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72  ist.  When all r
190a7 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c  eferences to all
190a8 20 70 61 67 65 73 0d 0a 2a 2a 20 61 72 65 20 72   pages..** are r
190a9 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62  eleased, a rollb
190aa 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ack occurs and t
190ab 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  he lock on the d
190ac 61 74 61 62 61 73 65 20 69 73 0d 0a 2a 2a 20 72  atabase is..** r
190ad 65 6d 6f 76 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  emoved...*/..SQL
190ae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
190af 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
190b0 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ef(DbPage *pPg){
190b1 0d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0d 0a  ..  if( pPg ){..
190b2 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
190b3 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
190b4 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ..    sqlite3Pca
190b5 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b  cheRelease(pPg);
190b6 0d 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  ..    pagerUnloc
190b7 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72  kIfUnused(pPager
190b8 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  );..  }..}..../*
190b9 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
190ba 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20  on is called at 
190bb 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 76 65  the start of eve
190bc 72 79 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  ry write transac
190bd 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 65 72 65 20  tion...** There 
190be 6d 75 73 74 20 61 6c 72 65 61 64 79 20 62 65 20  must already be 
190bf 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58  a RESERVED or EX
190c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
190c1 74 68 65 20 64 61 74 61 62 61 73 65 20 0d 0a 2a  the database ..*
190c2 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73  * file when this
190c3 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
190c4 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 70 65 6e  ed...**..** Open
190c5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
190c6 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67  e for pager pPag
190c7 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a  er and write a j
190c8 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0d 0a 2a  ournal header..*
190c9 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  * to the start o
190ca 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61  f it. If there a
190cb 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f  re active savepo
190cc 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73  ints, open the s
190cd 75 62 2d 6a 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 61  ub-journal..** a
190ce 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e  s well. This fun
190cf 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
190d0 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72  ed when the jour
190d1 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e  nal file is bein
190d2 67 20 0d 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f  g ..** opened to
190d3 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63   write a rollbac
190d4 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e  k log for a tran
190d5 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e  saction. It is n
190d6 6f 74 20 75 73 65 64 20 0d 0a 2a 2a 20 77 68 65  ot used ..** whe
190d7 6e 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20  n opening a hot 
190d8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
190d9 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0d 0a 2a  roll it back...*
190da 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75  *..** If the jou
190db 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6c 72  rnal file is alr
190dc 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20 69 74  eady open (as it
190dd 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63 6c 75   may be in exclu
190de 73 69 76 65 20 6d 6f 64 65 29 2c 0d 0a 2a 2a 20  sive mode),..** 
190df 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
190e0 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20 61  on just writes a
190e1 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
190e2 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
190e3 74 68 65 0d 0a 2a 2a 20 61 6c 72 65 61 64 79 20  the..** already 
190e4 6f 70 65 6e 20 66 69 6c 65 2e 20 0d 0a 2a 2a 0d  open file. ..**.
190e5 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e  .** Whether or n
190e6 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ot the journal f
190e7 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79  ile is opened by
190e8 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
190e9 74 68 65 0d 0a 2a 2a 20 50 61 67 65 72 2e 70 49  the..** Pager.pI
190ea 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20  nJournal bitvec 
190eb 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
190ec 6f 63 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ocated...**..** 
190ed 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
190ee 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
190ef 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 4f 74  s successful. Ot
190f0 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
190f1 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ..** SQLITE_NOME
190f2 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74  M if the attempt
190f3 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50 61 67   to allocate Pag
190f4 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61  er.pInJournal fa
190f5 69 6c 73 2c 20 6f 72 20 0d 0a 2a 2a 20 61 6e 20  ils, or ..** an 
190f6 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  IO error code if
190f7 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69 74   opening or writ
190f8 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
190f9 66 69 6c 65 20 66 61 69 6c 73 2e 0d 0a 2a 2f 0d  file fails...*/.
190fa 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
190fb 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50  r_open_journal(P
190fc 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a  ager *pPager){..
190fd 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
190fe 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
190ff 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19100 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
19101 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20    sqlite3_vfs * 
19102 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61  const pVfs = pPa
19103 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20  ger->pVfs;   /* 
19104 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76  Local cache of v
19105 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 0d  fs pointer */...
19106 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
19107 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
19108 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29  _WRITER_LOCKED )
19109 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 73 73  ;..  assert( ass
1910a 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
1910b 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20 61 73  pPager) );..  as
1910c 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
1910d 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0d 0a  nJournal==0 );..
1910e 20 20 0d 0a 20 20 2f 2a 20 49 66 20 61 6c 72 65    ..  /* If alre
1910f 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72  ady in the error
19110 20 73 74 61 74 65 2c 20 74 68 69 73 20 66 75 6e   state, this fun
19111 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
19112 2e 20 20 42 75 74 20 6f 6e 0d 0a 20 20 2a 2a 20  .  But on..  ** 
19113 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
19114 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
19115 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 66 20  never called if 
19116 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 69  we are already i
19117 6e 0d 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  n..  ** an error
19118 20 73 74 61 74 65 2e 20 2a 2f 0d 0a 20 20 69 66   state. */..  if
19119 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e  ( NEVER(pPager->
1911a 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72  errCode) ) retur
1911b 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
1911c 65 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 70 61 67  e;....  if( !pag
1911d 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
1911e 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
1911f 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
19120 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29  OURNALMODE_OFF )
19121 7b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70  {..    pPager->p
19122 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69  InJournal = sqli
19123 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
19124 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b  pPager->dbSize);
19125 0d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ..    if( pPager
19126 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
19127 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
19128 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
19129 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f      }..  ..    /
1912a 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e  * Open the journ
1912b 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 69 73  al file if it is
1912c 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65   not already ope
1912d 6e 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 21  n. */..    if( !
1912e 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
1912f 66 64 29 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  fd) ){..      if
19130 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
19131 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
19132 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
19133 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
19134 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
19135 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0d  n(pPager->jfd);.
19136 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
19137 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74         const int
19138 20 66 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20   flags =        
19139 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46             /* VF
1913a 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20  S flags to open 
1913b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0d  journal file */.
1913c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
1913d 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
1913e 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
1913f 41 54 45 7c 0d 0a 20 20 20 20 20 20 20 20 20 20  ATE|..          
19140 28 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  (pPager->tempFil
19141 65 20 3f 20 0d 0a 20 20 20 20 20 20 20 20 20 20  e ? ..          
19142 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44    (SQLITE_OPEN_D
19143 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c  ELETEONCLOSE|SQL
19144 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
19145 55 52 4e 41 4c 29 3a 0d 0a 20 20 20 20 20 20 20  URNAL):..       
19146 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45       (SQLITE_OPE
19147 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0d  N_MAIN_JOURNAL).
19148 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0d 0a 20  .          );.. 
19149 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45   #ifdef SQLITE_E
1914a 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
1914b 54 45 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  TE..        rc =
1914c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f   sqlite3JournalO
1914d 70 65 6e 28 0d 0a 20 20 20 20 20 20 20 20 20 20  pen(..          
1914e 20 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e    pVfs, pPager->
1914f 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
19150 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72  ->jfd, flags, jr
19151 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61  nlBufferSize(pPa
19152 67 65 72 29 0d 0a 20 20 20 20 20 20 20 20 29 3b  ger)..        );
19153 0d 0a 20 20 23 65 6c 73 65 0d 0a 20 20 20 20 20  ..  #else..     
19154 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
19155 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
19156 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
19157 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73  ager->jfd, flags
19158 2c 20 30 29 3b 0d 0a 20 20 23 65 6e 64 69 66 0d  , 0);..  #endif.
19159 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1915a 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
1915b 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28  TE_OK || isOpen(
1915c 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0d  pPager->jfd) );.
1915d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 0d 0a  .    }..  ..  ..
1915e 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
1915f 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68   first journal h
19160 65 61 64 65 72 20 74 6f 20 74 68 65 20 6a 6f 75  eader to the jou
19161 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70  rnal file and op
19162 65 6e 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20  en ..    ** the 
19163 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 6e  sub-journal if n
19164 65 63 65 73 73 61 72 79 2e 0d 0a 20 20 20 20 2a  ecessary...    *
19165 2f 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  /..    if( rc==S
19166 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
19167 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 63     /* TODO: Chec
19168 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 73  k if all of thes
19169 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 71  e are really req
1916a 75 69 72 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 20  uired. */..     
1916b 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
1916c 30 3b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72  0;..      pPager
1916d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
1916e 3b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ;..      pPager-
1916f 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0d  >setMaster = 0;.
19170 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
19171 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0d 0a  ournalHdr = 0;..
19172 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
19173 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
19174 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  r);..    }..  }.
19175 0a 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ...  if( rc!=SQL
19176 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 73  ITE_OK ){..    s
19177 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
19178 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a  roy(pPager->pInJ
19179 6f 75 72 6e 61 6c 29 3b 0d 0a 20 20 20 20 70 50  ournal);..    pP
1917a 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
1917b 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d   = 0;..  }else{.
1917c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1917d 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
1917e 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44  ER_WRITER_LOCKED
1917f 20 29 3b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d   );..    pPager-
19180 3e 65 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f  >eState = PAGER_
19181 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 3b  WRITER_CACHEMOD;
19182 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
19183 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
19184 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 74 65  ** Begin a write
19185 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20  -transaction on 
19186 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61  the specified pa
19187 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61  ger object. If a
19188 20 0d 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e   ..** write-tran
19189 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65  saction has alre
1918a 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  ady been opened,
1918b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1918c 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a  s a no-op...**..
1918d 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67  ** If the exFlag
1918e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c   argument is fal
1918f 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65  se, then acquire
19190 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45   at least a RESE
19191 52 56 45 44 0d 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e  RVED..** lock on
19192 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
19193 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73  le. If exFlag is
19194 20 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75   true, then acqu
19195 69 72 65 20 61 74 20 6c 65 61 73 74 0d 0a 2a 2a  ire at least..**
19196 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
19197 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ck. If such a lo
19198 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
19199 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0d  ld, no locking .
1919a 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65  .** functions ne
1919b 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0d 0a 2a  ed be called...*
1919c 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 73 75 62  *..** If the sub
1919d 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65  jInMemory argume
1919e 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  nt is non-zero, 
1919f 74 68 65 6e 20 61 6e 79 20 73 75 62 2d 6a 6f 75  then any sub-jou
191a0 72 6e 61 6c 20 6f 70 65 6e 65 64 0d 0a 2a 2a 20  rnal opened..** 
191a1 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e  within this tran
191a2 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  saction will be 
191a3 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d  opened as an in-
191a4 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54 68 69  memory file. Thi
191a5 73 0d 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66 66  s..** has no eff
191a6 65 63 74 20 69 66 20 74 68 65 20 73 75 62 2d 6a  ect if the sub-j
191a7 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 61 64  ournal is alread
191a8 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 74 20  y opened (as it 
191a9 6d 61 79 20 62 65 20 77 68 65 6e 0d 0a 2a 2a 20  may be when..** 
191aa 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
191ab 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 66  sive mode) or if
191ac 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
191ad 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72   does not requir
191ae 65 20 61 0d 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72  e a..** sub-jour
191af 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a  nal. If the subj
191b0 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e  InMemory argumen
191b1 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  t is zero, then 
191b2 61 6e 79 20 72 65 71 75 69 72 65 64 0d 0a 2a 2a  any required..**
191b3 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
191b4 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d  implemented in-m
191b5 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20  emory if pPager 
191b6 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
191b7 64 61 74 61 62 61 73 65 2c 20 0d 0a 2a 2a 20 6f  database, ..** o
191b8 72 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72  r using a tempor
191b9 61 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69  ary file otherwi
191ba 73 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  se...*/..SQLITE_
191bb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
191bc 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61  te3PagerBegin(Pa
191bd 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
191be 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62   exFlag, int sub
191bf 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0d 0a 20 20 69  jInMemory){..  i
191c0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
191c1 4b 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 50 61 67  K;....  if( pPag
191c2 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 20 72 65  er->errCode ) re
191c3 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
191c4 43 6f 64 65 3b 0d 0a 20 20 61 73 73 65 72 74 28  Code;..  assert(
191c5 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e   pPager->eState>
191c6 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 26 26  =PAGER_READER &&
191c7 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c   pPager->eState<
191c8 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0d 0a  PAGER_ERROR );..
191c9 20 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e    pPager->subjIn
191ca 4d 65 6d 6f 72 79 20 3d 20 28 75 38 29 73 75 62  Memory = (u8)sub
191cb 6a 49 6e 4d 65 6d 6f 72 79 3b 0d 0a 0d 0a 20 20  jInMemory;....  
191cc 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 67 65  if( ALWAYS(pPage
191cd 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
191ce 5f 52 45 41 44 45 52 29 20 29 7b 0d 0a 20 20 20  _READER) ){..   
191cf 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
191d0 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
191d1 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 61 67  ;....    if( pag
191d2 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
191d3 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66   ){..      /* If
191d4 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 6f   the pager is co
191d5 6e 66 69 67 75 72 65 64 20 74 6f 20 75 73 65 20  nfigured to use 
191d6 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63  locking_mode=exc
191d7 6c 75 73 69 76 65 2c 20 61 6e 64 20 61 6e 0d 0a  lusive, and an..
191d8 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69        ** exclusi
191d9 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ve lock on the d
191da 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 61  atabase is not a
191db 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74  lready held, obt
191dc 61 69 6e 20 69 74 20 6e 6f 77 2e 0d 0a 20 20 20  ain it now...   
191dd 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28     */..      if(
191de 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
191df 76 65 4d 6f 64 65 20 26 26 20 73 71 6c 69 74 65  veMode && sqlite
191e0 33 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64  3WalExclusiveMod
191e1 65 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20  e(pPager->pWal, 
191e2 2d 31 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  -1) ){..        
191e3 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62  rc = pagerLockDb
191e4 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49  (pPager, EXCLUSI
191e5 56 45 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 20  VE_LOCK);..     
191e6 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
191e7 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
191e8 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
191e9 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
191ea 20 20 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c    sqlite3WalExcl
191eb 75 73 69 76 65 4d 6f 64 65 28 70 50 61 67 65 72  usiveMode(pPager
191ec 2d 3e 70 57 61 6c 2c 20 31 29 3b 0d 0a 20 20 20  ->pWal, 1);..   
191ed 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
191ee 20 47 72 61 62 20 74 68 65 20 77 72 69 74 65 20   Grab the write 
191ef 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6c 6f 67 20  lock on the log 
191f0 66 69 6c 65 2e 20 49 66 20 73 75 63 63 65 73 73  file. If success
191f1 66 75 6c 2c 20 75 70 67 72 61 64 65 20 74 6f 0d  ful, upgrade to.
191f2 0a 20 20 20 20 20 20 2a 2a 20 50 41 47 45 52 5f  .      ** PAGER_
191f3 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20  RESERVED state. 
191f4 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
191f5 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
191f6 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a  to the caller...
191f7 20 20 20 20 20 20 2a 2a 20 54 68 65 20 62 75 73        ** The bus
191f8 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 6e 6f 74  y-handler is not
191f9 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 6e 6f 74   invoked if anot
191fa 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  her connection a
191fb 6c 72 65 61 64 79 0d 0a 20 20 20 20 20 20 2a 2a  lready..      **
191fc 20 68 6f 6c 64 73 20 74 68 65 20 77 72 69 74 65   holds the write
191fd 2d 6c 6f 63 6b 2e 20 49 66 20 70 6f 73 73 69 62  -lock. If possib
191fe 6c 65 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61  le, the upper la
191ff 79 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 69 74  yer will call it
19200 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
19201 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 57     rc = sqlite3W
19202 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e  alBeginWriteTran
19203 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e  saction(pPager->
19204 70 57 61 6c 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  pWal);..    }els
19205 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74  e{..      /* Obt
19206 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
19207 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
19208 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  ase file. If the
19209 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74 65   exFlag paramete
1920a 72 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74  r..      ** is t
1920b 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69  rue, then immedi
1920c 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 74 68  ately upgrade th
1920d 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49  is to an EXCLUSI
1920e 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0d 0a 20 20  VE lock. The..  
1920f 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64      ** busy-hand
19210 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e  ler callback can
19211 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70   be used when up
19212 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45  grading to the E
19213 58 43 4c 55 53 49 56 45 0d 0a 20 20 20 20 20 20  XCLUSIVE..      
19214 2a 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74  ** lock, but not
19215 20 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20   when obtaining 
19216 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63  the RESERVED loc
19217 6b 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  k...      */..  
19218 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4c 6f      rc = pagerLo
19219 63 6b 44 62 28 70 50 61 67 65 72 2c 20 52 45 53  ckDb(pPager, RES
1921a 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  ERVED_LOCK);..  
1921b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1921c 54 45 5f 4f 4b 20 26 26 20 65 78 46 6c 61 67 20  TE_OK && exFlag 
1921d 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
1921e 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
1921f 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c  ock(pPager, EXCL
19220 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  USIVE_LOCK);..  
19221 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a      }..    }....
19222 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
19223 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
19224 2f 2a 20 43 68 61 6e 67 65 20 74 6f 20 57 52 49  /* Change to WRI
19225 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74 61 74 65  TER_LOCKED state
19226 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20  ...      **..   
19227 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 65 20 73     ** WAL mode s
19228 65 74 73 20 50 61 67 65 72 2e 65 53 74 61 74 65  ets Pager.eState
19229 20 74 6f 20 50 41 47 45 52 5f 57 52 49 54 45 52   to PAGER_WRITER
1922a 5f 4c 4f 43 4b 45 44 20 6f 72 20 43 41 43 48 45  _LOCKED or CACHE
1922b 4d 4f 44 0d 0a 20 20 20 20 20 20 2a 2a 20 77 68  MOD..      ** wh
1922c 65 6e 20 69 74 20 68 61 73 20 61 6e 20 6f 70 65  en it has an ope
1922d 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 62  n transaction, b
1922e 75 74 20 6e 65 76 65 72 20 74 6f 20 44 42 4d 4f  ut never to DBMO
1922f 44 20 6f 72 20 46 49 4e 49 53 48 45 44 2e 0d 0a  D or FINISHED...
19230 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73        ** This is
19231 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 6f 73   because in thos
19232 65 20 73 74 61 74 65 73 20 74 68 65 20 63 6f 64  e states the cod
19233 65 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 73  e to roll back s
19234 61 76 65 70 6f 69 6e 74 20 0d 0a 20 20 20 20 20  avepoint ..     
19235 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 73   ** transactions
19236 20 6d 61 79 20 63 6f 70 79 20 64 61 74 61 20 66   may copy data f
19237 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
19238 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 64 61 74  nal into the dat
19239 61 62 61 73 65 20 0d 0a 20 20 20 20 20 20 2a 2a  abase ..      **
1923a 20 66 69 6c 65 20 61 73 20 77 65 6c 6c 20 61 73   file as well as
1923b 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 20 63   into the page c
1923c 61 63 68 65 2e 20 57 68 69 63 68 20 77 6f 75 6c  ache. Which woul
1923d 64 20 62 65 20 69 6e 63 6f 72 72 65 63 74 20 69  d be incorrect i
1923e 6e 20 0d 0a 20 20 20 20 20 20 2a 2a 20 57 41 4c  n ..      ** WAL
1923f 20 6d 6f 64 65 2e 0d 0a 20 20 20 20 20 20 2a 2f   mode...      */
19240 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ..      pPager->
19241 65 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 57  eState = PAGER_W
19242 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3b 0d 0a 20  RITER_LOCKED;.. 
19243 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 48       pPager->dbH
19244 69 6e 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72  intSize = pPager
19245 2d 3e 64 62 53 69 7a 65 3b 0d 0a 20 20 20 20 20  ->dbSize;..     
19246 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53   pPager->dbFileS
19247 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  ize = pPager->db
19248 53 69 7a 65 3b 0d 0a 20 20 20 20 20 20 70 50 61  Size;..      pPa
19249 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
1924a 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
1924b 3b 0d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ;..      pPager-
1924c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
1924d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 61  ..    }....    a
1924e 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1924f 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e  E_OK || pPager->
19250 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  eState==PAGER_RE
19251 41 44 45 52 20 29 3b 0d 0a 20 20 20 20 61 73 73  ADER );..    ass
19252 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
19253 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53  OK || pPager->eS
19254 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
19255 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0d 0a 20 20  ER_LOCKED );..  
19256 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
19257 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
19258 67 65 72 29 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ger) );..  }....
19259 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 54    PAGERTRACE(("T
1925a 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22  RANSACTION %d\n"
1925b 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
1925c 29 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  )));..  return r
1925d 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1925e 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 61  Mark a single da
1925f 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65  ta page as write
19260 61 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69  able. The page i
19261 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  s written into t
19262 68 65 20 0d 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75  he ..** main jou
19263 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72  rnal or sub-jour
19264 6e 61 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e  nal as required.
19265 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20   If the page is 
19266 77 72 69 74 74 65 6e 20 69 6e 74 6f 0d 0a 2a 2a  written into..**
19267 20 6f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72   one of the jour
19268 6e 61 6c 73 2c 20 74 68 65 20 63 6f 72 72 65 73  nals, the corres
19269 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73  ponding bit is s
1926a 65 74 20 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 50  et in the ..** P
1926b 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ager.pInJournal 
1926c 62 69 74 76 65 63 20 61 6e 64 20 74 68 65 20 50  bitvec and the P
1926d 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
1926e 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65  nSavepoint bitve
1926f 63 73 0d 0a 2a 2a 20 6f 66 20 61 6e 79 20 6f 70  cs..** of any op
19270 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 61 73  en savepoints as
19271 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0d 0a 2a   appropriate...*
19272 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  /..static int pa
19273 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 72 20  ger_write(PgHdr 
19274 2a 70 50 67 29 7b 0d 0a 20 20 76 6f 69 64 20 2a  *pPg){..  void *
19275 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61  pData = pPg->pDa
19276 74 61 3b 0d 0a 20 20 50 61 67 65 72 20 2a 70 50  ta;..  Pager *pP
19277 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
19278 65 72 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  er;..  int rc = 
19279 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20  SQLITE_OK;....  
1927a 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
1927b 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e  is not called un
1927c 6c 65 73 73 20 61 20 77 72 69 74 65 2d 74 72 61  less a write-tra
1927d 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72  nsaction has alr
1927e 65 61 64 79 20 0d 0a 20 20 2a 2a 20 62 65 65 6e  eady ..  ** been
1927f 20 73 74 61 72 74 65 64 2e 20 54 68 65 20 6a 6f   started. The jo
19280 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f  urnal file may o
19281 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 70 65  r may not be ope
19282 6e 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2e  n at this point.
19283 0d 0a 20 20 2a 2a 20 49 74 20 69 73 20 6e 65 76  ..  ** It is nev
19284 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 74 68 65  er called in the
19285 20 45 52 52 4f 52 20 73 74 61 74 65 2e 0d 0a 20   ERROR state... 
19286 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
19287 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
19288 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b  AGER_WRITER_LOCK
19289 45 44 0d 0a 20 20 20 20 20 20 20 7c 7c 20 70 50  ED..       || pP
1928a 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
1928b 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45  GER_WRITER_CACHE
1928c 4d 4f 44 0d 0a 20 20 20 20 20 20 20 7c 7c 20 70  MOD..       || p
1928d 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
1928e 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f  AGER_WRITER_DBMO
1928f 44 0d 0a 20 20 29 3b 0d 0a 20 20 61 73 73 65 72  D..  );..  asser
19290 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  t( assert_pager_
19291 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b  state(pPager) );
19292 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65  ....  /* If an e
19293 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72  rror has been pr
19294 65 76 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65  eviously detecte
19295 64 2c 20 72 65 70 6f 72 74 20 74 68 65 20 73 61  d, report the sa
19296 6d 65 20 65 72 72 6f 72 0d 0a 20 20 2a 2a 20 61  me error..  ** a
19297 67 61 69 6e 2e 20 54 68 69 73 20 73 68 6f 75 6c  gain. This shoul
19298 64 20 6e 6f 74 20 68 61 70 70 65 6e 2c 20 62 75  d not happen, bu
19299 74 20 74 68 65 20 63 68 65 63 6b 20 70 72 6f 76  t the check prov
1929a 69 64 65 73 20 72 6f 62 75 73 74 6e 65 73 73 2e  ides robustness.
1929b 20 2a 2f 0d 0a 20 20 69 66 28 20 4e 45 56 45 52   */..  if( NEVER
1929c 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65  (pPager->errCode
1929d 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 67  ) )  return pPag
1929e 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0d 0a 0d 0a  er->errCode;....
1929f 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65    /* Higher-leve
192a0 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72  l routines never
192a1 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
192a2 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20  ion if database 
192a3 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 77 72 69  is not..  ** wri
192a4 74 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63  table.  But chec
192a5 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66  k anyway, just f
192a6 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a  or robustness. *
192a7 2f 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  /..  if( NEVER(p
192a8 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29  Pager->readOnly)
192a9 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
192aa 5f 50 45 52 4d 3b 0d 0a 0d 0a 20 20 43 48 45 43  _PERM;....  CHEC
192ab 4b 5f 50 41 47 45 28 70 50 67 29 3b 0d 0a 0d 0a  K_PAGE(pPg);....
192ac 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
192ad 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62   file needs to b
192ae 65 20 6f 70 65 6e 65 64 2e 20 48 69 67 68 65 72  e opened. Higher
192af 20 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 20   level routines 
192b0 68 61 76 65 20 61 6c 72 65 61 64 79 0d 0a 20 20  have already..  
192b1 2a 2a 20 6f 62 74 61 69 6e 65 64 20 74 68 65 20  ** obtained the 
192b2 6e 65 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20  necessary locks 
192b3 74 6f 20 62 65 67 69 6e 20 74 68 65 20 77 72 69  to begin the wri
192b4 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  te-transaction, 
192b5 62 75 74 20 74 68 65 0d 0a 20 20 2a 2a 20 72 6f  but the..  ** ro
192b6 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d  llback journal m
192b7 69 67 68 74 20 6e 6f 74 20 79 65 74 20 62 65 20  ight not yet be 
192b8 6f 70 65 6e 2e 20 4f 70 65 6e 20 69 74 20 6e 6f  open. Open it no
192b9 77 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  w if this is the
192ba 20 63 61 73 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20   case...  **..  
192bb 2a 2a 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  ** This is done 
192bc 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  before calling s
192bd 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
192be 44 69 72 74 79 28 29 20 6f 6e 20 74 68 65 20 70  Dirty() on the p
192bf 61 67 65 2e 20 0d 0a 20 20 2a 2a 20 4f 74 68 65  age. ..  ** Othe
192c0 72 77 69 73 65 2c 20 69 66 20 69 74 20 77 65 72  rwise, if it wer
192c1 65 20 64 6f 6e 65 20 61 66 74 65 72 20 63 61 6c  e done after cal
192c2 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63  ling sqlite3Pcac
192c3 68 65 4d 61 6b 65 44 69 72 74 79 28 29 2c 20 74  heMakeDirty(), t
192c4 68 65 6e 0d 0a 20 20 2a 2a 20 61 6e 20 65 72 72  hen..  ** an err
192c5 6f 72 20 6d 69 67 68 74 20 6f 63 63 75 72 20 61  or might occur a
192c6 6e 64 20 74 68 65 20 70 61 67 65 72 20 77 6f 75  nd the pager wou
192c7 6c 64 20 65 6e 64 20 75 70 20 69 6e 20 57 52 49  ld end up in WRI
192c8 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74 61 74 65  TER_LOCKED state
192c9 0d 0a 20 20 2a 2a 20 77 69 74 68 20 70 61 67 65  ..  ** with page
192ca 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  s marked as dirt
192cb 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0d  y in the cache..
192cc 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 50 61  .  */..  if( pPa
192cd 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
192ce 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44  ER_WRITER_LOCKED
192cf 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 70 61   ){..    rc = pa
192d0 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
192d1 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 69  (pPager);..    i
192d2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
192d3 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20   ) return rc;.. 
192d4 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50   }..  assert( pP
192d5 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41  ager->eState>=PA
192d6 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45  GER_WRITER_CACHE
192d7 4d 4f 44 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  MOD );..  assert
192d8 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73  ( assert_pager_s
192d9 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d  tate(pPager) );.
192da 0a 0d 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65  ...  /* Mark the
192db 20 70 61 67 65 20 61 73 20 64 69 72 74 79 2e 20   page as dirty. 
192dc 20 49 66 20 74 68 65 20 70 61 67 65 20 68 61 73   If the page has
192dd 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72   already been wr
192de 69 74 74 65 6e 0d 0a 20 20 2a 2a 20 74 6f 20 74  itten..  ** to t
192df 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20  he journal then 
192e0 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20 72 69  we can return ri
192e1 67 68 74 20 61 77 61 79 2e 0d 0a 20 20 2a 2f 0d  ght away...  */.
192e2 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
192e3 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0d  MakeDirty(pPg);.
192e4 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f 75  .  if( pageInJou
192e5 72 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 75  rnal(pPg) && !su
192e6 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70  bjRequiresPage(p
192e7 50 67 29 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  Pg) ){..    asse
192e8 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c  rt( !pagerUseWal
192e9 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20 7d  (pPager) );..  }
192ea 65 6c 73 65 7b 0d 0a 20 20 0d 0a 20 20 20 20 2f  else{..  ..    /
192eb 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f  * The transactio
192ec 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78  n journal now ex
192ed 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65  ists and we have
192ee 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61   a RESERVED or a
192ef 6e 0d 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53  n..    ** EXCLUS
192f0 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
192f1 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
192f2 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63  le.  Write the c
192f3 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0d 0a  urrent page to..
192f4 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73      ** the trans
192f5 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69  action journal i
192f6 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72  f it is not ther
192f7 65 20 61 6c 72 65 61 64 79 2e 0d 0a 20 20 20 20  e already...    
192f8 2a 2f 0d 0a 20 20 20 20 69 66 28 20 21 70 61 67  */..    if( !pag
192f9 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
192fa 26 26 20 21 70 61 67 65 72 55 73 65 57 61 6c 28  && !pagerUseWal(
192fb 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20 20 20  pPager) ){..    
192fc 20 20 61 73 73 65 72 74 28 20 70 61 67 65 72 55    assert( pagerU
192fd 73 65 57 61 6c 28 70 50 61 67 65 72 29 3d 3d 30  seWal(pPager)==0
192fe 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70   );..      if( p
192ff 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72  Pg->pgno<=pPager
19300 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 26 26 20  ->dbOrigSize && 
19301 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
19302 66 64 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  fd) ){..        
19303 75 33 32 20 63 6b 73 75 6d 3b 0d 0a 20 20 20 20  u32 cksum;..    
19304 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32      char *pData2
19305 3b 0d 0a 20 20 20 20 20 20 20 20 69 36 34 20 69  ;..        i64 i
19306 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
19307 75 72 6e 61 6c 4f 66 66 3b 0d 0a 0d 0a 20 20 20  urnalOff;....   
19308 20 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c       /* We shoul
19309 64 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f  d never write to
1930a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1930b 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0d  e the page that.
1930c 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  .        ** cont
1930d 61 69 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  ains the databas
1930e 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f  e locks.  The fo
1930f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76  llowing assert v
19310 65 72 69 66 69 65 73 0d 0a 20 20 20 20 20 20 20  erifies..       
19311 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e   ** that we do n
19312 6f 74 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ot. */..        
19313 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e  assert( pPg->pgn
19314 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o!=PAGER_MJ_PGNO
19315 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 0d 0a 20  (pPager) );.... 
19316 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
19317 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
19318 72 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  r<=pPager->journ
19319 61 6c 4f 66 66 20 29 3b 0d 0a 20 20 20 20 20 20  alOff );..      
1931a 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c    CODEC2(pPager,
1931b 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e   pData, pPg->pgn
1931c 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c  o, 7, return SQL
1931d 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61  ITE_NOMEM, pData
1931e 32 29 3b 0d 0a 20 20 20 20 20 20 20 20 63 6b 73  2);..        cks
1931f 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d  um = pager_cksum
19320 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44  (pPager, (u8*)pD
19321 61 74 61 32 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  ata2);....      
19322 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20    /* Even if an 
19323 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65  IO or diskfull e
19324 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
19325 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68  e journalling th
19326 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61  e..        ** pa
19327 67 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20  ge in the block 
19328 61 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e  above, set the n
19329 65 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f  eed-sync flag fo
1932a 72 20 74 68 65 20 70 61 67 65 2e 0d 0a 20 20 20  r the page...   
1932b 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73       ** Otherwis
1932c 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  e, when the tran
1932d 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
1932e 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69  d back, the logi
1932f 63 20 69 6e 0d 0a 20 20 20 20 20 20 20 20 2a 2a  c in..        **
19330 20 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61   playback_one_pa
19331 67 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20  ge() will think 
19332 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65  that the page ne
19333 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72  eds to be restor
19334 65 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  ed..        ** i
19335 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
19336 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49  ile. And if an I
19337 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  O error occurs w
19338 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0d 0a  hile doing so,..
19339 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
1933a 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 20 66  corruption may f
1933b 6f 6c 6c 6f 77 2e 0d 0a 20 20 20 20 20 20 20 20  ollow...        
1933c 2a 2f 0d 0a 20 20 20 20 20 20 20 20 70 50 67 2d  */..        pPg-
1933d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f  >flags |= PGHDR_
1933e 4e 45 45 44 5f 53 59 4e 43 3b 0d 0a 0d 0a 20 20  NEED_SYNC;....  
1933f 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
19340 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
19341 66 64 2c 20 69 4f 66 66 2c 20 70 50 67 2d 3e 70  fd, iOff, pPg->p
19342 67 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  gno);..        i
19343 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
19344 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20   ) return rc;.. 
19345 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
19346 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
19347 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20  r->jfd, pData2, 
19348 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
19349 2c 20 69 4f 66 66 2b 34 29 3b 0d 0a 20 20 20 20  , iOff+4);..    
1934a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1934b 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
1934c 63 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  c;..        rc =
1934d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
1934e 67 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2b 70  ger->jfd, iOff+p
1934f 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b  Pager->pageSize+
19350 34 2c 20 63 6b 73 75 6d 29 3b 0d 0a 20 20 20 20  4, cksum);..    
19351 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
19352 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
19353 63 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 49 4f  c;....        IO
19354 54 52 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20  TRACE(("JOUT %p 
19355 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70  %d %lld %d\n", p
19356 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
19357 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
19358 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
19359 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d  rnalOff, pPager-
1935a 3e 70 61 67 65 53 69 7a 65 29 29 3b 0d 0a 20 20  >pageSize));..  
1935b 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52        PAGER_INCR
1935c 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77  (sqlite3_pager_w
1935d 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0d 0a 20  ritej_count);.. 
1935e 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
1935f 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70  E(("JOURNAL %d p
19360 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d  age %d needSync=
19361 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22  %d hash(%08x)\n"
19362 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
19363 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
19364 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0d 0a 20 20   pPg->pgno, ..  
19365 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67             ((pPg
19366 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
19367 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70  ED_SYNC)?1:0), p
19368 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
19369 67 29 29 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  g)));....       
1936a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1936b 4f 66 66 20 2b 3d 20 38 20 2b 20 70 50 61 67 65  Off += 8 + pPage
1936c 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20  r->pageSize;..  
1936d 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52        pPager->nR
1936e 65 63 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 61  ec++;..        a
1936f 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
19370 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0d  InJournal!=0 );.
19371 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
19372 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
19373 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
19374 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0d 0a  l, pPg->pgno);..
19375 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
19376 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
19377 45 4d 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 61  EM );..        a
19378 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
19379 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
1937a 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20 20  TE_NOMEM );..   
1937b 20 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f       rc |= addTo
1937c 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73  SavepointBitvecs
1937d 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67  (pPager, pPg->pg
1937e 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  no);..        if
1937f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
19380 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73  ){..          as
19381 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
19382 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20 20 20 20  _NOMEM );..     
19383 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d       return rc;.
19384 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
19385 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
19386 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53    if( pPager->eS
19387 74 61 74 65 21 3d 50 41 47 45 52 5f 57 52 49 54  tate!=PAGER_WRIT
19388 45 52 5f 44 42 4d 4f 44 20 29 7b 0d 0a 20 20 20  ER_DBMOD ){..   
19389 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67         pPg->flag
1938a 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f  s |= PGHDR_NEED_
1938b 53 59 4e 43 3b 0d 0a 20 20 20 20 20 20 20 20 7d  SYNC;..        }
1938c 0d 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54  ..        PAGERT
1938d 52 41 43 45 28 28 22 41 50 50 45 4e 44 20 25 64  RACE(("APPEND %d
1938e 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e   page %d needSyn
1938f 63 3d 25 64 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  c=%d\n",..      
19390 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49            PAGERI
19391 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
19392 70 67 6e 6f 2c 0d 0a 20 20 20 20 20 20 20 20 20  pgno,..         
19393 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61        ((pPg->fla
19394 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
19395 4e 43 29 3f 31 3a 30 29 29 29 3b 0d 0a 20 20 20  NC)?1:0)));..   
19396 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d     }..    }..  .
19397 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
19398 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
19399 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65   is open and the
1939a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20   page is not in 
1939b 69 74 2c 0d 0a 20 20 20 20 2a 2a 20 74 68 65 6e  it,..    ** then
1939c 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65   write the curre
1939d 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73  nt page to the s
1939e 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
1939f 2e 20 20 4e 6f 74 65 20 74 68 61 74 0d 0a 20 20  .  Note that..  
193a0 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
193a1 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  nt journal forma
193a2 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74  t differs from t
193a3 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72  he standard jour
193a4 6e 61 6c 20 66 6f 72 6d 61 74 0d 0a 20 20 20 20  nal format..    
193a5 2a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d  ** in that it om
193a6 69 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d  its the checksum
193a7 73 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72  s and the header
193a8 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  ...    */..    i
193a9 66 28 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  f( subjRequiresP
193aa 61 67 65 28 70 50 67 29 20 29 7b 0d 0a 20 20 20  age(pPg) ){..   
193ab 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e     rc = subjourn
193ac 61 6c 50 61 67 65 28 70 50 67 29 3b 0d 0a 20 20  alPage(pPg);..  
193ad 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
193ae 20 55 70 64 61 74 65 20 74 68 65 20 64 61 74 61   Update the data
193af 62 61 73 65 20 73 69 7a 65 20 61 6e 64 20 72 65  base size and re
193b0 74 75 72 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  turn...  */..  i
193b1 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  f( pPager->dbSiz
193b2 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0d 0a  e<pPg->pgno ){..
193b3 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
193b4 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0d  ze = pPg->pgno;.
193b5 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
193b6 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
193b7 4d 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65  Mark a data page
193b8 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54   as writeable. T
193b9 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
193ba 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
193bb 65 20 0d 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 68  e ..** making ch
193bc 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e  anges to a page.
193bd 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
193be 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72   check the retur
193bf 6e 20 76 61 6c 75 65 20 0d 0a 2a 2a 20 6f 66 20  n value ..** of 
193c0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e  this function an
193c1 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f 74  d be careful not
193c2 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 20 70   to change any p
193c3 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 73 20  age data unless 
193c4 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  ..** this routin
193c5 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
193c6 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  _OK...**..** The
193c7 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
193c8 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  een this functio
193c9 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74  n and pager_writ
193ca 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73  e() is that this
193cb 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c  ..** function al
193cc 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  so deals with th
193cd 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77  e special case w
193ce 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70  here 2 or more p
193cf 61 67 65 73 0d 0a 2a 2a 20 66 69 74 20 6f 6e 20  ages..** fit on 
193d0 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65  a single disk se
193d1 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61  ctor. In this ca
193d2 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65  se all co-reside
193d3 6e 74 20 70 61 67 65 73 0d 0a 2a 2a 20 6d 75 73  nt pages..** mus
193d4 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  t have been writ
193d5 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
193d6 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72  al file before r
193d7 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  eturning...**..*
193d8 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
193d9 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  curs, SQLITE_NOM
193da 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f  EM or an IO erro
193db 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
193dc 65 64 0d 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70  ed..** as approp
193dd 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65  riate. Otherwise
193de 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f  , SQLITE_OK...*/
193df 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
193e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
193e1 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  rWrite(DbPage *p
193e2 44 62 50 61 67 65 29 7b 0d 0a 20 20 69 6e 74 20  DbPage){..  int 
193e3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
193e4 0a 0d 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20  ...  PgHdr *pPg 
193e5 3d 20 70 44 62 50 61 67 65 3b 0d 0a 20 20 50 61  = pDbPage;..  Pa
193e6 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
193e7 67 2d 3e 70 50 61 67 65 72 3b 0d 0a 20 20 50 67  g->pPager;..  Pg
193e8 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f  no nPagePerSecto
193e9 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63  r = (pPager->sec
193ea 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e  torSize/pPager->
193eb 70 61 67 65 53 69 7a 65 29 3b 0d 0a 0d 0a 20 20  pageSize);....  
193ec 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
193ed 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52  eState>=PAGER_WR
193ee 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0d 0a  ITER_LOCKED );..
193ef 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
193f0 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f  ->eState!=PAGER_
193f1 45 52 52 4f 52 20 29 3b 0d 0a 20 20 61 73 73 65  ERROR );..  asse
193f2 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72  rt( assert_pager
193f3 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29  _state(pPager) )
193f4 3b 0d 0a 0d 0a 20 20 69 66 28 20 6e 50 61 67 65  ;....  if( nPage
193f5 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b 0d 0a  PerSector>1 ){..
193f6 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 6f      Pgno nPageCo
193f7 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  unt;          /*
193f8 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
193f9 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61   pages in databa
193fa 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20  se file */..    
193fb 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20  Pgno pg1;       
193fc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
193fd 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
193fe 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
193ff 61 74 65 64 20 6f 6e 2e 20 2a 2f 0d 0a 20 20 20  ated on. */..   
19400 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b 20   int nPage = 0; 
19401 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
19402 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74  mber of pages st
19403 61 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f  arting at pg1 to
19404 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a 20 20 20   journal */..   
19405 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
19406 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
19407 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
19408 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20     int needSync 
19409 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
1940a 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 67 65  True if any page
1940b 20 68 61 73 20 50 47 48 44 52 5f 4e 45 45 44 5f   has PGHDR_NEED_
1940c 53 59 4e 43 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f  SYNC */....    /
1940d 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 53  * Set the doNotS
1940e 79 6e 63 53 70 69 6c 6c 20 66 6c 61 67 20 74 6f  yncSpill flag to
1940f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61   1. This is beca
19410 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c  use we cannot al
19411 6c 6f 77 0d 0a 20 20 20 20 2a 2a 20 61 20 6a 6f  low..    ** a jo
19412 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20  urnal header to 
19413 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65  be written betwe
19414 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75  en the pages jou
19415 72 6e 61 6c 65 64 20 62 79 0d 0a 20 20 20 20 2a  rnaled by..    *
19416 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  * this function.
19417 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73  ..    */..    as
19418 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0d  sert( !MEMDB );.
19419 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1941a 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 53 70  ger->doNotSyncSp
1941b 69 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 70  ill==0 );..    p
1941c 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63  Pager->doNotSync
1941d 53 70 69 6c 6c 2b 2b 3b 0d 0a 0d 0a 20 20 20 20  Spill++;....    
1941e 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73  /* This trick as
1941f 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20  sumes that both 
19420 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e  the page-size an
19421 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72  d sector-size ar
19422 65 0d 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74  e..    ** an int
19423 65 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e  eger power of 2.
19424 20 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c   It sets variabl
19425 65 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65  e pg1 to the ide
19426 6e 74 69 66 69 65 72 0d 0a 20 20 20 20 2a 2a 20  ntifier..    ** 
19427 6f 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67  of the first pag
19428 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20  e of the sector 
19429 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f  pPg is located o
1942a 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  n...    */..    
1942b 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e  pg1 = ((pPg->pgn
1942c 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50 65  o-1) & ~(nPagePe
1942d 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b  rSector-1)) + 1;
1942e 0d 0a 0d 0a 20 20 20 20 6e 50 61 67 65 43 6f 75  ....    nPageCou
1942f 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  nt = pPager->dbS
19430 69 7a 65 3b 0d 0a 20 20 20 20 69 66 28 20 70 50  ize;..    if( pP
19431 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75  g->pgno>nPageCou
19432 6e 74 20 29 7b 0d 0a 20 20 20 20 20 20 6e 50 61  nt ){..      nPa
19433 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20  ge = (pPg->pgno 
19434 2d 20 70 67 31 29 2b 31 3b 0d 0a 20 20 20 20 7d  - pg1)+1;..    }
19435 65 6c 73 65 20 69 66 28 20 28 70 67 31 2b 6e 50  else if( (pg1+nP
19436 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 3e  agePerSector-1)>
19437 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0d 0a 20  nPageCount ){.. 
19438 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61       nPage = nPa
19439 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0d 0a  geCount+1-pg1;..
1943a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1943b 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50    nPage = nPageP
1943c 65 72 53 65 63 74 6f 72 3b 0d 0a 20 20 20 20 7d  erSector;..    }
1943d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50 61  ..    assert(nPa
1943e 67 65 3e 30 29 3b 0d 0a 20 20 20 20 61 73 73 65  ge>0);..    asse
1943f 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e  rt(pg1<=pPg->pgn
19440 6f 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  o);..    assert(
19441 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d  (pg1+nPage)>pPg-
19442 3e 70 67 6e 6f 29 3b 0d 0a 0d 0a 20 20 20 20 66  >pgno);....    f
19443 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67  or(ii=0; ii<nPag
19444 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  e && rc==SQLITE_
19445 4f 4b 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20  OK; ii++){..    
19446 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b    Pgno pg = pg1+
19447 69 69 3b 0d 0a 20 20 20 20 20 20 50 67 48 64 72  ii;..      PgHdr
19448 20 2a 70 50 61 67 65 3b 0d 0a 20 20 20 20 20 20   *pPage;..      
19449 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e  if( pg==pPg->pgn
1944a 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74  o || !sqlite3Bit
1944b 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e  vecTest(pPager->
1944c 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20  pInJournal, pg) 
1944d 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
1944e 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  pg!=PAGER_MJ_PGN
1944f 4f 28 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20  O(pPager) ){..  
19450 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
19451 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
19452 67 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29  ger, pg, &pPage)
19453 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
19454 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
19455 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  {..            r
19456 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28  c = pager_write(
19457 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20  pPage);..       
19458 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
19459 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
1945a 5f 53 59 4e 43 20 29 7b 0d 0a 20 20 20 20 20 20  _SYNC ){..      
1945b 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63          needSync
1945c 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 1;..         
1945d 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
1945e 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
1945f 72 65 66 28 70 50 61 67 65 29 3b 0d 0a 20 20 20  ref(pPage);..   
19460 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
19461 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65    }..      }else
19462 20 69 66 28 20 28 70 50 61 67 65 20 3d 20 70 61   if( (pPage = pa
19463 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
19464 72 2c 20 70 67 29 29 21 3d 30 20 29 7b 0d 0a 20  r, pg))!=0 ){.. 
19465 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
19466 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
19467 45 44 5f 53 59 4e 43 20 29 7b 0d 0a 20 20 20 20  ED_SYNC ){..    
19468 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d        needSync =
19469 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a   1;..        }..
1946a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
1946b 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29  agerUnref(pPage)
1946c 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1946d 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74  }....    /* If t
1946e 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
1946f 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 20 66  NC flag is set f
19470 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50  or any of the nP
19471 61 67 65 20 70 61 67 65 73 20 0d 0a 20 20 20 20  age pages ..    
19472 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ** starting at p
19473 67 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64  g1, then it need
19474 73 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20  s to be set for 
19475 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63  all of them. Bec
19476 61 75 73 65 0d 0a 20 20 20 20 2a 2a 20 77 72 69  ause..    ** wri
19477 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74  ting to any of t
19478 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 65 73  hese nPage pages
19479 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 65 20   may damage the 
1947a 6f 74 68 65 72 73 2c 20 74 68 65 0d 0a 20 20 20  others, the..   
1947b 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   ** journal file
1947c 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 79   must contain sy
1947d 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f 66  nc()ed copies of
1947e 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0d 0a 20 20   all of them..  
1947f 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20    ** before any 
19480 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77  of them can be w
19481 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68  ritten out to th
19482 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
19483 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
19484 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19485 26 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0d 0a  && needSync ){..
19486 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d        assert( !M
19487 45 4d 44 42 20 29 3b 0d 0a 20 20 20 20 20 20 66  EMDB );..      f
19488 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67  or(ii=0; ii<nPag
19489 65 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  e; ii++){..     
1948a 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 20     PgHdr *pPage 
1948b 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70  = pager_lookup(p
1948c 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0d  Pager, pg1+ii);.
1948d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
1948e 67 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  ge ){..         
1948f 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d   pPage->flags |=
19490 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
19491 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
19492 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
19493 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Page);..        
19494 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
19495 7d 0d 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28  }....    assert(
19496 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
19497 6e 63 53 70 69 6c 6c 3d 3d 31 20 29 3b 0d 0a 20  ncSpill==1 );.. 
19498 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74     pPager->doNot
19499 53 79 6e 63 53 70 69 6c 6c 2d 2d 3b 0d 0a 20 20  SyncSpill--;..  
1949a 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d  }else{..    rc =
1949b 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62   pager_write(pDb
1949c 50 61 67 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  Page);..  }..  r
1949d 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1949e 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  /*..** Return TR
1949f 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20 67  UE if the page g
194a0 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75  iven in the argu
194a1 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75  ment was previou
194a2 73 6c 79 20 70 61 73 73 65 64 0d 0a 2a 2a 20 74  sly passed..** t
194a3 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  o sqlite3PagerWr
194a4 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72  ite().  In other
194a5 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54   words, return T
194a6 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0d  RUE if it is ok.
194a7 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68  .** to change th
194a8 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
194a9 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e   page...*/..#ifn
194aa 64 65 66 20 4e 44 45 42 55 47 0d 0a 53 51 4c 49  def NDEBUG..SQLI
194ab 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
194ac 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
194ad 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70  teable(DbPage *p
194ae 50 67 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70  Pg){..  return p
194af 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
194b0 44 49 52 54 59 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  DIRTY;..}..#endi
194b1 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 63 61  f..../*..** A ca
194b2 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ll to this routi
194b3 6e 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67  ne tells the pag
194b4 65 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  er that it is no
194b5 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0d 0a  t necessary to..
194b6 2a 2a 20 77 72 69 74 65 20 74 68 65 20 69 6e 66  ** write the inf
194b7 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65  ormation on page
194b8 20 70 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65   pPg back to the
194b9 20 64 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75   disk, even thou
194ba 67 68 0d 0a 2a 2a 20 74 68 61 74 20 70 61 67 65  gh..** that page
194bb 20 6d 69 67 68 74 20 62 65 20 6d 61 72 6b 65 64   might be marked
194bc 20 61 73 20 64 69 72 74 79 2e 20 20 54 68 69 73   as dirty.  This
194bd 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78   happens, for ex
194be 61 6d 70 6c 65 2c 20 77 68 65 6e 0d 0a 2a 2a 20  ample, when..** 
194bf 74 68 65 20 70 61 67 65 20 68 61 73 20 62 65 65  the page has bee
194c0 6e 20 61 64 64 65 64 20 61 73 20 61 20 6c 65 61  n added as a lea
194c1 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  f of the freelis
194c2 74 20 61 6e 64 20 73 6f 20 69 74 73 0d 0a 2a 2a  t and so its..**
194c3 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67   content no long
194c4 65 72 20 6d 61 74 74 65 72 73 2e 0d 0a 2a 2a 0d  er matters...**.
194c5 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e  .** The overlyin
194c6 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72  g software layer
194c7 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
194c8 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20  ine when all of 
194c9 74 68 65 20 64 61 74 61 0d 0a 2a 2a 20 6f 6e 20  the data..** on 
194ca 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69  the given page i
194cb 73 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61  s unused. The pa
194cc 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61  ger marks the pa
194cd 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0d 0a  ge as clean so..
194ce 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20  ** that it does 
194cf 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20  not get written 
194d0 74 6f 20 64 69 73 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  to disk...**..**
194d1 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74   Tests show that
194d2 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
194d3 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65  on can quadruple
194d4 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61   the speed of la
194d5 72 67 65 20 0d 0a 2a 2a 20 44 45 4c 45 54 45 20  rge ..** DELETE 
194d6 6f 70 65 72 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d  operations...*/.
194d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
194d8 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
194d9 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72  rDontWrite(PgHdr
194da 20 2a 70 50 67 29 7b 0d 0a 20 20 50 61 67 65 72   *pPg){..  Pager
194db 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
194dc 70 50 61 67 65 72 3b 0d 0a 20 20 69 66 28 20 28  pPager;..  if( (
194dd 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
194de 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 67 65  _DIRTY) && pPage
194df 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30  r->nSavepoint==0
194e0 20 29 7b 0d 0a 20 20 20 20 50 41 47 45 52 54 52   ){..    PAGERTR
194e1 41 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 45  ACE(("DONT_WRITE
194e2 20 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e   page %d of %d\n
194e3 22 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41  ", pPg->pgno, PA
194e4 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
194e5 0d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  ..    IOTRACE(("
194e6 43 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20  CLEAN %p %d\n", 
194e7 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e  pPager, pPg->pgn
194e8 6f 29 29 0d 0a 20 20 20 20 70 50 67 2d 3e 66 6c  o))..    pPg->fl
194e9 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e  ags |= PGHDR_DON
194ea 54 5f 57 52 49 54 45 3b 0d 0a 20 20 20 20 70 61  T_WRITE;..    pa
194eb 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68  ger_set_pagehash
194ec 28 70 50 67 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  (pPg);..  }..}..
194ed 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ../*..** This ro
194ee 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
194ef 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  to increment the
194f0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61   value of the da
194f1 74 61 62 61 73 65 20 66 69 6c 65 20 0d 0a 2a 2a  tabase file ..**
194f2 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c   change-counter,
194f3 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62   stored as a 4-b
194f4 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
194f5 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 20  nteger starting 
194f6 61 74 20 0d 0a 2a 2a 20 62 79 74 65 20 6f 66 66  at ..** byte off
194f7 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61  set 24 of the pa
194f8 67 65 72 20 66 69 6c 65 2e 20 20 54 68 65 20 73  ger file.  The s
194f9 65 63 6f 6e 64 61 72 79 20 63 68 61 6e 67 65 20  econdary change 
194fa 63 6f 75 6e 74 65 72 20 61 74 0d 0a 2a 2a 20 39  counter at..** 9
194fb 32 20 69 73 20 61 6c 73 6f 20 75 70 64 61 74 65  2 is also update
194fc 64 2c 20 61 73 20 69 73 20 74 68 65 20 53 51 4c  d, as is the SQL
194fd 69 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  ite version numb
194fe 65 72 20 61 74 20 6f 66 66 73 65 74 20 39 36 2e  er at offset 96.
194ff 0d 0a 2a 2a 0d 0a 2a 2a 20 42 75 74 20 74 68 69  ..**..** But thi
19500 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69  s only happens i
19501 66 20 74 68 65 20 70 50 61 67 65 72 2d 3e 63 68  f the pPager->ch
19502 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c  angeCountDone fl
19503 61 67 20 69 73 20 66 61 6c 73 65 2e 0d 0a 2a 2a  ag is false...**
19504 20 54 6f 20 61 76 6f 69 64 20 65 78 63 65 73 73   To avoid excess
19505 20 63 68 75 72 6e 69 6e 67 20 6f 66 20 70 61 67   churning of pag
19506 65 20 31 2c 20 74 68 65 20 75 70 64 61 74 65 20  e 1, the update 
19507 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 6f 6e 63  only happens onc
19508 65 2e 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  e...** See also 
19509 74 68 65 20 70 61 67 65 72 5f 77 72 69 74 65 5f  the pager_write_
1950a 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20  changecounter() 
1950b 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65  routine that doe
1950c 73 20 61 6e 20 0d 0a 2a 2a 20 75 6e 63 6f 6e 64  s an ..** uncond
1950d 69 74 69 6f 6e 61 6c 20 75 70 64 61 74 65 20 6f  itional update o
1950e 66 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75  f the change cou
1950f 6e 74 65 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  nters...**..** I
19510 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f  f the isDirectMo
19511 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c  de flag is zero,
19512 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f   then this is do
19513 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0d 0a  ne by calling ..
19514 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  ** sqlite3PagerW
19515 72 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31  rite() on page 1
19516 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67  , then modifying
19517 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
19518 20 74 68 65 0d 0a 2a 2a 20 70 61 67 65 20 64 61   the..** page da
19519 74 61 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ta. In this case
1951a 20 74 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62   the file will b
1951b 65 20 75 70 64 61 74 65 64 20 77 68 65 6e 20 74  e updated when t
1951c 68 65 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 74  he current..** t
1951d 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
1951e 6d 6d 69 74 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  mmitted...**..**
1951f 20 54 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64   The isDirectMod
19520 65 20 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79 20  e flag may only 
19521 62 65 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74  be non-zero if t
19522 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63  he library was c
19523 6f 6d 70 69 6c 65 64 0d 0a 2a 2a 20 77 69 74 68  ompiled..** with
19524 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42   the SQLITE_ENAB
19525 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 20  LE_ATOMIC_WRITE 
19526 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 2e 20 49  macro defined. I
19527 6e 20 74 68 69 73 20 63 61 73 65 2c 0d 0a 2a 2a  n this case,..**
19528 20 69 66 20 69 73 44 69 72 65 63 74 20 69 73 20   if isDirect is 
19529 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
1952a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1952b 20 69 73 20 75 70 64 61 74 65 64 20 64 69 72 65   is updated dire
1952c 63 74 6c 79 0d 0a 2a 2a 20 62 79 20 77 72 69 74  ctly..** by writ
1952d 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 20 76  ing an updated v
1952e 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 31  ersion of page 1
1952f 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f   using a call to
19530 20 74 68 65 20 0d 0a 2a 2a 20 73 71 6c 69 74 65   the ..** sqlite
19531 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 74  3OsWrite() funct
19532 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
19533 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f   int pager_incr_
19534 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61  changecounter(Pa
19535 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
19536 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0d   isDirectMode){.
19537 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
19538 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65  TE_OK;....  asse
19539 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61  rt( pPager->eSta
1953a 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52  te==PAGER_WRITER
1953b 5f 43 41 43 48 45 4d 4f 44 0d 0a 20 20 20 20 20  _CACHEMOD..     
1953c 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74    || pPager->eSt
1953d 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45  ate==PAGER_WRITE
1953e 52 5f 44 42 4d 4f 44 0d 0a 20 20 29 3b 0d 0a 20  R_DBMOD..  );.. 
1953f 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f   assert( assert_
19540 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67  pager_state(pPag
19541 65 72 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 44  er) );....  /* D
19542 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69  eclare and initi
19543 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69  alize constant i
19544 6e 74 65 67 65 72 20 27 69 73 44 69 72 65 63 74  nteger 'isDirect
19545 27 2e 20 49 66 20 74 68 65 0d 0a 20 20 2a 2a 20  '. If the..  ** 
19546 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74  atomic-write opt
19547 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61  imization is ena
19548 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69  bled in this bui
19549 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65 63  ld, then isDirec
1954a 74 0d 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69  t..  ** is initi
1954b 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61  alized to the va
1954c 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68  lue passed as th
1954d 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70  e isDirectMode p
1954e 61 72 61 6d 65 74 65 72 0d 0a 20 20 2a 2a 20 74  arameter..  ** t
1954f 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
19550 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
19551 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20  s always set to 
19552 7a 65 72 6f 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  zero...  **..  *
19553 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68  * The idea is th
19554 61 74 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63  at if the atomic
19555 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74  -write optimizat
19556 69 6f 6e 20 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a  ion is not..  **
19557 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
19558 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 20 63 6f  ile time, the co
19559 6d 70 69 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20  mpiler can omit 
1955a 74 68 65 20 74 65 73 74 73 20 6f 66 0d 0a 20 20  the tests of..  
1955b 2a 2a 20 27 69 73 44 69 72 65 63 74 27 20 62 65  ** 'isDirect' be
1955c 6c 6f 77 2c 20 61 73 20 77 65 6c 6c 20 61 73 20  low, as well as 
1955d 74 68 65 20 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73  the block enclos
1955e 65 64 20 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20  ed in the..  ** 
1955f 22 69 66 28 20 69 73 44 69 72 65 63 74 20 29 22  "if( isDirect )"
19560 20 63 6f 6e 64 69 74 69 6f 6e 2e 0d 0a 20 20 2a   condition...  *
19561 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
19562 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
19563 57 52 49 54 45 0d 0a 23 20 64 65 66 69 6e 65 20  WRITE..# define 
19564 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0d 0a 20  DIRECT_MODE 0.. 
19565 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63   assert( isDirec
19566 74 4d 6f 64 65 3d 3d 30 20 29 3b 0d 0a 20 20 55  tMode==0 );..  U
19567 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
19568 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0d 0a  isDirectMode);..
19569 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
1956a 44 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 69  DIRECT_MODE isDi
1956b 72 65 63 74 4d 6f 64 65 0d 0a 23 65 6e 64 69 66  rectMode..#endif
1956c 0d 0a 0d 0a 20 20 69 66 28 20 21 70 50 61 67 65  ....  if( !pPage
1956d 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
1956e 6e 65 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62  ne && pPager->db
1956f 53 69 7a 65 3e 30 20 29 7b 0d 0a 20 20 20 20 50  Size>0 ){..    P
19570 67 48 64 72 20 2a 70 50 67 48 64 72 3b 20 20 20  gHdr *pPgHdr;   
19571 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19572 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67  Reference to pag
19573 65 20 31 20 2a 2f 0d 0a 0d 0a 20 20 20 20 61 73  e 1 */....    as
19574 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74  sert( !pPager->t
19575 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65  empFile && isOpe
19576 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b  n(pPager->fd) );
19577 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20  ....    /* Open 
19578 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 66 69  page 1 of the fi
19579 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  le for writing. 
1957a 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  */..    rc = sql
1957b 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
1957c 67 65 72 2c 20 31 2c 20 26 70 50 67 48 64 72 29  ger, 1, &pPgHdr)
1957d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
1957e 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d  PgHdr==0 || rc==
1957f 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 0d 0a  SQLITE_OK );....
19580 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 6f      /* If page o
19581 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 20 73  ne was fetched s
19582 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e 64  uccessfully, and
19583 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
19584 73 20 6e 6f 74 0d 0a 20 20 20 20 2a 2a 20 6f 70  s not..    ** op
19585 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63  erating in direc
19586 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67  t-mode, make pag
19587 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20 57  e 1 writable.  W
19588 68 65 6e 20 6e 6f 74 20 69 6e 20 0d 0a 20 20 20  hen not in ..   
19589 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c   ** direct mode,
1958a 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 79   page 1 is alway
1958b 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20  s held in cache 
1958c 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 61  and hence the Pa
1958d 67 65 72 47 65 74 28 29 0d 0a 20 20 20 20 2a 2a  gerGet()..    **
1958e 20 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73   above is always
1958f 20 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65   successful - he
19590 6e 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f  nce the ALWAYS o
19591 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e  n rc==SQLITE_OK.
19592 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
19593 28 20 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 26  ( !DIRECT_MODE &
19594 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c  & ALWAYS(rc==SQL
19595 49 54 45 5f 4f 4b 29 20 29 7b 0d 0a 20 20 20 20  ITE_OK) ){..    
19596 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
19597 67 65 72 57 72 69 74 65 28 70 50 67 48 64 72 29  gerWrite(pPgHdr)
19598 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
19599 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1959a 4b 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 41  K ){..      /* A
1959b 63 74 75 61 6c 6c 79 20 64 6f 20 74 68 65 20 75  ctually do the u
1959c 70 64 61 74 65 20 6f 66 20 74 68 65 20 63 68 61  pdate of the cha
1959d 6e 67 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a  nge counter */..
1959e 20 20 20 20 20 20 70 61 67 65 72 5f 77 72 69 74        pager_writ
1959f 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28  e_changecounter(
195a0 70 50 67 48 64 72 29 3b 0d 0a 0d 0a 20 20 20 20  pPgHdr);....    
195a1 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20    /* If running 
195a2 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20  in direct mode, 
195a3 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
195a4 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20  ts of page 1 to 
195a5 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20  the file. */..  
195a6 20 20 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d      if( DIRECT_M
195a7 4f 44 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ODE ){..        
195a8 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66  const void *zBuf
195a9 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ;..        asser
195aa 74 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  t( pPager->dbFil
195ab 65 53 69 7a 65 3e 30 20 29 3b 0d 0a 20 20 20 20  eSize>0 );..    
195ac 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65      CODEC2(pPage
195ad 72 2c 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61  r, pPgHdr->pData
195ae 2c 20 31 2c 20 36 2c 20 72 63 3d 53 51 4c 49 54  , 1, 6, rc=SQLIT
195af 45 5f 4e 4f 4d 45 4d 2c 20 7a 42 75 66 29 3b 0d  E_NOMEM, zBuf);.
195b0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
195b1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
195b2 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
195b3 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
195b4 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70  ger->fd, zBuf, p
195b5 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
195b6 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   0);..        }.
195b7 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
195b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
195b9 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
195ba 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
195bb 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d   = 1;..        }
195bc 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
195bd 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
195be 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
195bf 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  = 1;..      }.. 
195c0 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52     }....    /* R
195c1 65 6c 65 61 73 65 20 74 68 65 20 70 61 67 65 20  elease the page 
195c2 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0d 0a 20  reference. */.. 
195c3 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
195c4 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0d 0a 20  nref(pPgHdr);.. 
195c5 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
195c6 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 79  ..}..../*..** Sy
195c7 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
195c8 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68  file to disk. Th
195c9 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  is is a no-op fo
195ca 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  r in-memory data
195cb 62 61 73 65 73 0d 0a 2a 2a 20 6f 72 20 70 61 67  bases..** or pag
195cc 65 73 20 77 69 74 68 20 74 68 65 20 50 61 67 65  es with the Page
195cd 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 67 20 73 65  r.noSync flag se
195ce 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75  t...**..** If su
195cf 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 69 66 20  ccessful, or if 
195d0 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65  called on a page
195d1 72 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 69  r for which it i
195d2 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0d  s a no-op, this.
195d3 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
195d4 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  urns SQLITE_OK. 
195d5 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f  Otherwise, an IO
195d6 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
195d7 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  eturned...*/..SQ
195d8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
195d9 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e   sqlite3PagerSyn
195da 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
195db 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  {..  int rc = SQ
195dc 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20  LITE_OK;..  if( 
195dd 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
195de 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
195df 21 4d 45 4d 44 42 20 29 3b 0d 0a 20 20 20 20 72  !MEMDB );..    r
195e0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
195e1 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50  c(pPager->fd, pP
195e2 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 29  ager->syncFlags)
195e3 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  ;..  }else if( i
195e4 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
195e5 29 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  ) ){..    assert
195e6 28 20 21 4d 45 4d 44 42 20 29 3b 0d 0a 20 20 20  ( !MEMDB );..   
195e7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
195e8 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65  ileControl(pPage
195e9 72 2d 3e 66 64 2c 20 53 51 4c 49 54 45 5f 46 43  r->fd, SQLITE_FC
195ea 4e 54 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44  NTL_SYNC_OMITTED
195eb 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  , 0);..    if( r
195ec 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55  c==SQLITE_NOTFOU
195ed 4e 44 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  ND ){..      rc 
195ee 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
195ef 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
195f0 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
195f1 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
195f2 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61  n may only be ca
195f3 6c 6c 65 64 20 77 68 69 6c 65 20 61 20 77 72 69  lled while a wri
195f4 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
195f5 73 20 61 63 74 69 76 65 20 69 6e 0d 0a 2a 2a 20  s active in..** 
195f6 72 6f 6c 6c 62 61 63 6b 2e 20 49 66 20 74 68 65  rollback. If the
195f7 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69   connection is i
195f8 6e 20 57 41 4c 20 6d 6f 64 65 2c 20 74 68 69 73  n WAL mode, this
195f9 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70   call is a no-op
195fa 2e 20 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  . ..** Otherwise
195fb 2c 20 69 66 20 74 68 65 20 63 6f 6e 6e 65 63 74  , if the connect
195fc 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72  ion does not alr
195fd 65 61 64 79 20 68 61 76 65 20 61 6e 20 45 58 43  eady have an EXC
195fe 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 0d  LUSIVE lock on .
195ff 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
19600 20 66 69 6c 65 2c 20 61 6e 20 61 74 74 65 6d 70   file, an attemp
19601 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74  t is made to obt
19602 61 69 6e 20 6f 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ain one...**..**
19603 20 49 66 20 74 68 65 20 45 58 43 4c 55 53 49 56   If the EXCLUSIV
19604 45 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64  E lock is alread
19605 79 20 68 65 6c 64 20 6f 72 20 74 68 65 20 61 74  y held or the at
19606 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20  tempt to obtain 
19607 69 74 20 69 73 0d 0a 2a 2a 20 73 75 63 63 65 73  it is..** succes
19608 73 66 75 6c 2c 20 6f 72 20 74 68 65 20 63 6f 6e  sful, or the con
19609 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 57 41  nection is in WA
1960a 4c 20 6d 6f 64 65 2c 20 53 51 4c 49 54 45 5f 4f  L mode, SQLITE_O
1960b 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  K is returned...
1960c 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 65 69  ** Otherwise, ei
1960d 74 68 65 72 20 53 51 4c 49 54 45 5f 42 55 53 59  ther SQLITE_BUSY
1960e 20 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f   or an SQLITE_IO
1960f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ERR_XXX error co
19610 64 65 20 69 73 20 0d 0a 2a 2a 20 72 65 74 75 72  de is ..** retur
19611 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ned...*/..SQLITE
19612 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19613 69 74 65 33 50 61 67 65 72 45 78 63 6c 75 73 69  ite3PagerExclusi
19614 76 65 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  veLock(Pager *pP
19615 61 67 65 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63  ager){..  int rc
19616 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
19617 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
19618 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57  >eState==PAGER_W
19619 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 0d  RITER_CACHEMOD .
1961a 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65  .       || pPage
1961b 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
1961c 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 0d 0a  _WRITER_DBMOD ..
1961d 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72         || pPager
1961e 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f  ->eState==PAGER_
1961f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 0d 0a  WRITER_LOCKED ..
19620 20 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20    );..  assert( 
19621 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
19622 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20  te(pPager) );.. 
19623 20 69 66 28 20 30 3d 3d 70 61 67 65 72 55 73 65   if( 0==pagerUse
19624 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0d 0a  Wal(pPager) ){..
19625 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
19626 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67  ait_on_lock(pPag
19627 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  er, EXCLUSIVE_LO
19628 43 4b 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  CK);..  }..  ret
19629 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1962a 0d 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 61  ..** Sync the da
1962b 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20  tabase file for 
1962c 74 68 65 20 70 61 67 65 72 20 70 50 61 67 65 72  the pager pPager
1962d 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73  . zMaster points
1962e 20 74 6f 20 74 68 65 20 6e 61 6d 65 0d 0a 2a 2a   to the name..**
1962f 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   of a master jou
19630 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73  rnal file that s
19631 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
19632 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69 76 69   into the indivi
19633 64 75 61 6c 0d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  dual..** journal
19634 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d   file. zMaster m
19635 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63  ay be NULL, whic
19636 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64  h is interpreted
19637 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0d 0a 2a   as no master..*
19638 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e  * journal (a sin
19639 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61  gle database tra
1963a 6e 73 61 63 74 69 6f 6e 29 2e 0d 0a 2a 2a 0d 0a  nsaction)...**..
1963b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1963c 65 6e 73 75 72 65 73 20 74 68 61 74 3a 0d 0a 2a  ensures that:..*
1963d 2a 0d 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61  *..**   * The da
1963e 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e  tabase file chan
1963f 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 75 70  ge-counter is up
19640 64 61 74 65 64 2c 0d 0a 2a 2a 20 20 20 2a 20 74  dated,..**   * t
19641 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  he journal is sy
19642 6e 63 65 64 20 28 75 6e 6c 65 73 73 20 74 68 65  nced (unless the
19643 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
19644 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 73  timization is us
19645 65 64 29 2c 0d 0a 2a 2a 20 20 20 2a 20 61 6c 6c  ed),..**   * all
19646 20 64 69 72 74 79 20 70 61 67 65 73 20 61 72 65   dirty pages are
19647 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
19648 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 0d  database file, .
19649 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61  .**   * the data
1964a 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 72 75  base file is tru
1964b 6e 63 61 74 65 64 20 28 69 66 20 72 65 71 75 69  ncated (if requi
1964c 72 65 64 29 2c 20 61 6e 64 0d 0a 2a 2a 20 20 20  red), and..**   
1964d 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
1964e 69 6c 65 20 73 79 6e 63 65 64 2e 20 0d 0a 2a 2a  ile synced. ..**
1964f 0d 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68  ..** The only th
19650 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73  ing that remains
19651 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74   to commit the t
19652 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f  ransaction is to
19653 20 66 69 6e 61 6c 69 7a 65 20 0d 0a 2a 2a 20 28   finalize ..** (
19654 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65  delete, truncate
19655 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72   or zero the fir
19656 73 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20  st part of) the 
19657 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72  journal file (or
19658 20 0d 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65   ..** delete the
19659 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1965a 66 69 6c 65 20 69 66 20 73 70 65 63 69 66 69 65  file if specifie
1965b 64 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65  d)...**..** Note
1965c 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74 65 72   that if zMaster
1965d 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65  ==NULL, this doe
1965e 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 20  s not overwrite 
1965f 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  a previous value
19660 0d 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 61  ..** passed to a
19661 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  n sqlite3PagerCo
19662 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 63  mmitPhaseOne() c
19663 61 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  all...**..** If 
19664 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65  the final parame
19665 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69  ter - noSync - i
19666 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
19667 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
19668 74 73 65 6c 66 0d 0a 2a 2a 20 69 73 20 6e 6f 74  tself..** is not
19669 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c   synced. The cal
1966a 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ler must call sq
1966b 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29  lite3PagerSync()
1966c 20 64 69 72 65 63 74 6c 79 20 74 6f 0d 0a 2a 2a   directly to..**
1966d 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   sync the databa
1966e 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63  se file before c
1966f 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61  alling CommitPha
19670 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74  seTwo() to delet
19671 65 20 74 68 65 0d 0a 2a 2a 20 6a 6f 75 72 6e 61  e the..** journa
19672 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63  l file in this c
19673 61 73 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ase...*/..SQLITE
19674 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19675 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
19676 68 61 73 65 4f 6e 65 28 0d 0a 20 20 50 61 67 65  haseOne(..  Page
19677 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
19678 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
19679 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a  ager object */..
1967a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
1967b 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 20 20  aster,          
1967c 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c    /* If not NULL
1967d 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  , the master jou
1967e 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20  rnal name */..  
1967f 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 20  int noSync      
19680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19681 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20  /* True to omit 
19682 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 65  the xSync on the
19683 20 64 62 20 66 69 6c 65 20 2a 2f 0d 0a 29 7b 0d   db file */..){.
19684 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
19685 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
19686 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
19687 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  e */....  assert
19688 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
19689 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c  ==PAGER_WRITER_L
1968a 4f 43 4b 45 44 0d 0a 20 20 20 20 20 20 20 7c 7c  OCKED..       ||
1968b 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
1968c 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41  =PAGER_WRITER_CA
1968d 43 48 45 4d 4f 44 0d 0a 20 20 20 20 20 20 20 7c  CHEMOD..       |
1968e 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  | pPager->eState
1968f 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44  ==PAGER_WRITER_D
19690 42 4d 4f 44 0d 0a 20 20 20 20 20 20 20 7c 7c 20  BMOD..       || 
19691 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
19692 50 41 47 45 52 5f 45 52 52 4f 52 0d 0a 20 20 29  PAGER_ERROR..  )
19693 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 73 73  ;..  assert( ass
19694 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
19695 70 50 61 67 65 72 29 20 29 3b 0d 0a 0d 0a 20 20  pPager) );....  
19696 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65 72  /* If a prior er
19697 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 72 65  ror occurred, re
19698 70 6f 72 74 20 74 68 61 74 20 65 72 72 6f 72 20  port that error 
19699 61 67 61 69 6e 2e 20 2a 2f 0d 0a 20 20 69 66 28  again. */..  if(
1969a 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
1969b 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
1969c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1969d 3b 0d 0a 0d 0a 20 20 50 41 47 45 52 54 52 41 43  ;....  PAGERTRAC
1969e 45 28 28 22 44 41 54 41 42 41 53 45 20 53 59 4e  E(("DATABASE SYN
1969f 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74  C: File=%s zMast
196a0 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e  er=%s nSize=%d\n
196a1 22 2c 20 0d 0a 20 20 20 20 20 20 70 50 61 67 65  ", ..      pPage
196a2 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d  r->zFilename, zM
196a3 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64  aster, pPager->d
196a4 62 53 69 7a 65 29 29 3b 0d 0a 0d 0a 20 20 2f 2a  bSize));....  /*
196a5 20 49 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20   If no database 
196a6 63 68 61 6e 67 65 73 20 68 61 76 65 20 62 65 65  changes have bee
196a7 6e 20 6d 61 64 65 2c 20 72 65 74 75 72 6e 20 65  n made, return e
196a8 61 72 6c 79 2e 20 2a 2f 0d 0a 20 20 69 66 28 20  arly. */..  if( 
196a9 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c 50  pPager->eState<P
196aa 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48  AGER_WRITER_CACH
196ab 45 4d 4f 44 20 29 20 72 65 74 75 72 6e 20 53 51  EMOD ) return SQ
196ac 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 69 66  LITE_OK;....  if
196ad 28 20 4d 45 4d 44 42 20 29 7b 0d 0a 20 20 20 20  ( MEMDB ){..    
196ae 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
196af 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f   in-memory db, o
196b0 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20  r no pages have 
196b1 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c  been written to,
196b2 20 6f 72 20 74 68 69 73 0d 0a 20 20 20 20 2a 2a   or this..    **
196b3 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c   function has al
196b4 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65  ready been calle
196b5 64 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79 20  d, it is mostly 
196b6 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65  a no-op.  Howeve
196b7 72 2c 20 61 6e 79 0d 0a 20 20 20 20 2a 2a 20 62  r, any..    ** b
196b8 61 63 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73  ackup in progres
196b9 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  s needs to be re
196ba 73 74 61 72 74 65 64 2e 0d 0a 20 20 20 20 2a 2f  started...    */
196bb 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 63  ..    sqlite3Bac
196bc 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67 65  kupRestart(pPage
196bd 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0d 0a 20 20  r->pBackup);..  
196be 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 66 28 20  }else{..    if( 
196bf 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67  pagerUseWal(pPag
196c0 65 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 50 67  er) ){..      Pg
196c1 48 64 72 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c  Hdr *pList = sql
196c2 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
196c3 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ist(pPager->pPCa
196c4 63 68 65 29 3b 0d 0a 20 20 20 20 20 20 50 67 48  che);..      PgH
196c5 64 72 20 2a 70 50 61 67 65 4f 6e 65 20 3d 20 30  dr *pPageOne = 0
196c6 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69  ;..      if( pLi
196c7 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  st==0 ){..      
196c8 20 20 2f 2a 20 4d 75 73 74 20 68 61 76 65 20 61    /* Must have a
196c9 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 61 67 65  t least one page
196ca 20 66 6f 72 20 74 68 65 20 57 41 4c 20 63 6f 6d   for the WAL com
196cb 6d 69 74 20 66 6c 61 67 2e 0d 0a 20 20 20 20 20  mit flag...     
196cc 20 20 20 2a 2a 20 54 69 63 6b 65 74 20 5b 32 64     ** Ticket [2d
196cd 31 61 35 63 36 37 64 66 63 32 33 36 33 65 34 34  1a5c67dfc2363e44
196ce 66 32 39 64 39 62 62 64 35 37 66 5d 20 32 30 31  f29d9bbd57f] 201
196cf 31 2d 30 35 2d 31 38 20 2a 2f 0d 0a 20 20 20 20  1-05-18 */..    
196d0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
196d1 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c  PagerGet(pPager,
196d2 20 31 2c 20 26 70 50 61 67 65 4f 6e 65 29 3b 0d   1, &pPageOne);.
196d3 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d  .        pList =
196d4 20 70 50 61 67 65 4f 6e 65 3b 0d 0a 20 20 20 20   pPageOne;..    
196d5 20 20 20 20 70 4c 69 73 74 2d 3e 70 44 69 72 74      pList->pDirt
196d6 79 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d  y = 0;..      }.
196d7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
196d8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d  c==SQLITE_OK );.
196d9 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
196da 53 28 70 4c 69 73 74 29 20 29 7b 0d 0a 20 20 20  S(pList) ){..   
196db 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 57       rc = pagerW
196dc 61 6c 46 72 61 6d 65 73 28 70 50 61 67 65 72 2c  alFrames(pPager,
196dd 20 70 4c 69 73 74 2c 20 70 50 61 67 65 72 2d 3e   pList, pPager->
196de 64 62 53 69 7a 65 2c 20 31 29 3b 0d 0a 20 20 20  dbSize, 1);..   
196df 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
196e0 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
196e1 61 67 65 4f 6e 65 29 3b 0d 0a 20 20 20 20 20 20  ageOne);..      
196e2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
196e3 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  K ){..        sq
196e4 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
196e5 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
196e6 63 68 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  che);..      }..
196e7 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
196e8 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
196e9 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61 74 65 73  ng block updates
196ea 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
196eb 74 65 72 2e 20 45 78 61 63 74 6c 79 20 68 6f 77  ter. Exactly how
196ec 20 69 74 0d 0a 20 20 20 20 20 20 2a 2a 20 64 6f   it..      ** do
196ed 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20  es this depends 
196ee 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
196ef 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64  t the atomic-upd
196f0 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ate optimization
196f1 0d 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 20 65  ..      ** was e
196f2 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
196f3 65 20 74 69 6d 65 2c 20 61 6e 64 20 69 66 20 74  e time, and if t
196f4 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  his transaction 
196f5 6d 65 65 74 73 20 74 68 65 20 0d 0a 20 20 20 20  meets the ..    
196f6 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 69    ** runtime cri
196f7 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 65  teria to use the
196f8 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0d 0a 20 20   operation: ..  
196f9 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a      **..      **
196fa 20 20 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73      * The file-s
196fb 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74  ystem supports t
196fc 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
196fd 70 72 6f 70 65 72 74 79 20 66 6f 72 0d 0a 20 20  property for..  
196fe 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63      **      bloc
196ff 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d  ks of size page-
19700 73 69 7a 65 2c 20 61 6e 64 20 0d 0a 20 20 20 20  size, and ..    
19701 20 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63    **    * This c
19702 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ommit is not par
19703 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c  t of a multi-fil
19704 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  e transaction, a
19705 6e 64 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  nd..      **    
19706 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 61  * Exactly one pa
19707 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  ge has been modi
19708 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 69  fied and store i
19709 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
1970a 6c 65 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20  le...      **.. 
1970b 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 6f       ** If the o
1970c 70 74 69 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20  ptimization was 
1970d 6e 6f 74 20 65 6e 61 62 6c 65 64 20 61 74 20 63  not enabled at c
1970e 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 68 65  ompile time, the
1970f 6e 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  n the..      ** 
19710 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
19711 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e 63 74  ecounter() funct
19712 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
19713 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
19714 67 65 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 75  ge..      ** cou
19715 6e 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63  nter in 'indirec
19716 74 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20  t-mode'. If the 
19717 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
19718 63 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0d  compiled in but.
19719 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74  .      ** is not
1971a 20 61 70 70 6c 69 63 61 62 6c 65 20 74 6f 20 74   applicable to t
1971b 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  his transaction,
1971c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4a 6f 75   call sqlite3Jou
1971d 72 6e 61 6c 43 72 65 61 74 65 28 29 0d 0a 20 20  rnalCreate()..  
1971e 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 73      ** to make s
1971f 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ure the journal 
19720 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c 6c  file has actuall
19721 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20  y been created, 
19722 74 68 65 6e 20 63 61 6c 6c 0d 0a 20 20 20 20 20  then call..     
19723 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63   ** pager_incr_c
19724 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74  hangecounter() t
19725 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  o update the cha
19726 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69  nge-counter in i
19727 6e 64 69 72 65 63 74 0d 0a 20 20 20 20 20 20 2a  ndirect..      *
19728 2a 20 6d 6f 64 65 2e 20 0d 0a 20 20 20 20 20 20  * mode. ..      
19729 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 4f 74 68  **..      ** Oth
1972a 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 6f  erwise, if the o
1972b 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62  ptimization is b
1972c 6f 74 68 20 65 6e 61 62 6c 65 64 20 61 6e 64 20  oth enabled and 
1972d 61 70 70 6c 69 63 61 62 6c 65 2c 0d 0a 20 20 20  applicable,..   
1972e 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20     ** then call 
1972f 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
19730 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70  ecounter() to up
19731 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
19732 63 6f 75 6e 74 65 72 0d 0a 20 20 20 20 20 20 2a  counter..      *
19733 2a 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f  * in 'direct' mo
19734 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  de. In this case
19735 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
19736 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0d  e will never be.
19737 0a 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65  .      ** create
19738 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73  d for this trans
19739 61 63 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a  action...      *
1973a 2f 0d 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49  /..  #ifdef SQLI
1973b 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
1973c 5f 57 52 49 54 45 0d 0a 20 20 20 20 20 20 50 67  _WRITE..      Pg
1973d 48 64 72 20 2a 70 50 67 3b 0d 0a 20 20 20 20 20  Hdr *pPg;..     
1973e 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
1973f 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0d 0a 20  pPager->jfd) .. 
19740 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61            || pPa
19741 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
19742 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
19743 4f 44 45 5f 4f 46 46 20 0d 0a 20 20 20 20 20 20  ODE_OFF ..      
19744 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
19745 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
19746 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57  ER_JOURNALMODE_W
19747 41 4c 20 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20  AL ..      );.. 
19748 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65       if( !zMaste
19749 72 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67  r && isOpen(pPag
1974a 65 72 2d 3e 6a 66 64 29 20 0d 0a 20 20 20 20 20  er->jfd) ..     
1974b 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
1974c 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66  rnalOff==jrnlBuf
1974d 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 20  ferSize(pPager) 
1974e 0d 0a 20 20 20 20 20 20 20 26 26 20 70 50 61 67  ..       && pPag
1974f 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67  er->dbSize>=pPag
19750 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 0d 0a  er->dbOrigSize..
19751 20 20 20 20 20 20 20 26 26 20 28 30 3d 3d 28 70         && (0==(p
19752 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  Pg = sqlite3Pcac
19753 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67  heDirtyList(pPag
19754 65 72 2d 3e 70 50 43 61 63 68 65 29 29 20 7c 7c  er->pPCache)) ||
19755 20 30 3d 3d 70 50 67 2d 3e 70 44 69 72 74 79 29   0==pPg->pDirty)
19756 0d 0a 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20  ..      ){..    
19757 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68      /* Update th
19758 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65  e db file change
19759 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65   counter via the
1975a 20 64 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65   direct-write me
1975b 74 68 6f 64 2e 20 54 68 65 20 0d 0a 20 20 20 20  thod. The ..    
1975c 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67      ** following
1975d 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66   call will modif
1975e 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  y the in-memory 
1975f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
19760 66 20 70 61 67 65 20 31 20 0d 0a 20 20 20 20 20  f page 1 ..     
19761 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65     ** to include
19762 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61   the updated cha
19763 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20  nge counter and 
19764 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20  then write page 
19765 31 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  1 ..        ** d
19766 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64  irectly to the d
19767 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65  atabase file. Be
19768 63 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f  cause of the ato
19769 6d 69 63 2d 77 72 69 74 65 20 0d 0a 20 20 20 20  mic-write ..    
1976a 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20      ** property 
1976b 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65  of the host file
1976c 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73  -system, this is
1976d 20 73 61 66 65 2e 0d 0a 20 20 20 20 20 20 20 20   safe...        
1976e 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  */..        rc =
1976f 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
19770 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
19771 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  , 1);..      }el
19772 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  se{..        rc 
19773 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  = sqlite3Journal
19774 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  Create(pPager->j
19775 66 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  fd);..        if
19776 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19777 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63  ){..          rc
19778 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
19779 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
1977a 65 72 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  er, 0);..       
1977b 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 23   }..      }..  #
1977c 65 6c 73 65 0d 0a 20 20 20 20 20 20 72 63 20 3d  else..      rc =
1977d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
1977e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
1977f 2c 20 30 29 3b 0d 0a 20 20 23 65 6e 64 69 66 0d  , 0);..  #endif.
19780 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
19781 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
19782 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
19783 5f 65 78 69 74 3b 0d 0a 20 20 0d 0a 20 20 20 20  _exit;..  ..    
19784 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61    /* If this tra
19785 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64  nsaction has mad
19786 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
19787 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c  maller, then all
19788 20 70 61 67 65 73 0d 0a 20 20 20 20 20 20 2a 2a   pages..      **
19789 20 62 65 69 6e 67 20 64 69 73 63 61 72 64 65 64   being discarded
1978a 20 62 79 20 74 68 65 20 74 72 75 6e 63 61 74 69   by the truncati
1978b 6f 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74 74  on must be writt
1978c 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  en to the journa
1978d 6c 0d 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65  l..      ** file
1978e 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20  . This can only 
1978f 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76  happen in auto-v
19790 61 63 75 75 6d 20 6d 6f 64 65 2e 0d 0a 20 20 20  acuum mode...   
19791 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20     **..      ** 
19792 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67 20 74  Before reading t
19793 68 65 20 70 61 67 65 73 20 77 69 74 68 20 70 61  he pages with pa
19794 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72 67 65  ge numbers large
19795 72 20 74 68 61 6e 20 74 68 65 20 0d 0a 20 20 20  r than the ..   
19796 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 61     ** current va
19797 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53  lue of Pager.dbS
19798 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 20  ize, set dbSize 
19799 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75  back to the valu
1979a 65 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74  e..      ** that
1979b 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20   it took at the 
1979c 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61  start of the tra
1979d 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  nsaction. Otherw
1979e 69 73 65 2c 20 74 68 65 0d 0a 20 20 20 20 20 20  ise, the..      
1979f 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
197a0 74 65 33 50 61 67 65 72 47 65 74 28 29 20 72 65  te3PagerGet() re
197a1 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 65  turn zeroed page
197a2 73 20 69 6e 73 74 65 61 64 20 6f 66 20 0d 0a 20  s instead of .. 
197a3 20 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20       ** reading 
197a4 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
197a5 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 20 20  tabase file...  
197a6 20 20 20 20 2a 2f 0d 0a 20 20 23 69 66 6e 64 65      */..  #ifnde
197a7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
197a8 54 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 20 20  TOVACUUM..      
197a9 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
197aa 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  ze<pPager->dbOri
197ab 67 53 69 7a 65 20 0d 0a 20 20 20 20 20 20 20 26  gSize ..       &
197ac 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
197ad 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
197ae 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0d 0a 20 20  RNALMODE_OFF..  
197af 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20      ){..        
197b0 50 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20  Pgno i;         
197b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
197b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
197b3 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
197b4 2f 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  /..        const
197b5 20 50 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41   Pgno iSkip = PA
197b6 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
197b7 65 72 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20  er); /* Pending 
197b8 6c 6f 63 6b 20 70 61 67 65 20 2a 2f 0d 0a 20 20  lock page */..  
197b9 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f        const Pgno
197ba 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72   dbSize = pPager
197bb 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20  ->dbSize;       
197bc 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 67  /* Database imag
197bd 65 20 73 69 7a 65 20 2a 2f 20 0d 0a 20 20 20 20  e size */ ..    
197be 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
197bf 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f  ze = pPager->dbO
197c0 72 69 67 53 69 7a 65 3b 0d 0a 20 20 20 20 20 20  rigSize;..      
197c1 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a 65 2b    for( i=dbSize+
197c2 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 64 62  1; i<=pPager->db
197c3 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 29 7b  OrigSize; i++ ){
197c4 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
197c5 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  !sqlite3BitvecTe
197c6 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  st(pPager->pInJo
197c7 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d  urnal, i) && i!=
197c8 69 53 6b 69 70 20 29 7b 0d 0a 20 20 20 20 20 20  iSkip ){..      
197c9 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61        PgHdr *pPa
197ca 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
197cb 2f 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e  /* Page to journ
197cc 61 6c 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  al */..         
197cd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
197ce 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20  agerGet(pPager, 
197cf 69 2c 20 26 70 50 61 67 65 29 3b 0d 0a 20 20 20  i, &pPage);..   
197d0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
197d1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
197d2 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
197d3 6e 65 5f 65 78 69 74 3b 0d 0a 20 20 20 20 20 20  ne_exit;..      
197d4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
197d5 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
197d6 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ge);..          
197d7 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
197d8 72 65 66 28 70 50 61 67 65 29 3b 0d 0a 20 20 20  ref(pPage);..   
197d9 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
197da 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
197db 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
197dc 6e 65 5f 65 78 69 74 3b 0d 0a 20 20 20 20 20 20  ne_exit;..      
197dd 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
197de 0d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ..        pPager
197df 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a  ->dbSize = dbSiz
197e0 65 3b 0d 0a 20 20 20 20 20 20 7d 20 0d 0a 20 20  e;..      } ..  
197e1 23 65 6e 64 69 66 0d 0a 20 20 0d 0a 20 20 20 20  #endif..  ..    
197e2 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d    /* Write the m
197e3 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
197e4 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  me into the jour
197e5 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 6d  nal file. If a m
197e6 61 73 74 65 72 20 0d 0a 20 20 20 20 20 20 2a 2a  aster ..      **
197e7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
197e8 6d 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  me has already b
197e9 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74  een written to t
197ea 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  he journal file,
197eb 20 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 69   ..      ** or i
197ec 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e 55 4c  f zMaster is NUL
197ed 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  L (no master jou
197ee 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 69 73  rnal), then this
197ef 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70   call is a no-op
197f0 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
197f1 20 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61 73     rc = writeMas
197f2 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  terJournal(pPage
197f3 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0d 0a 20 20  r, zMaster);..  
197f4 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
197f5 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d  TE_OK ) goto com
197f6 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
197f7 69 74 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 2f  it;..  ..      /
197f8 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  * Sync the journ
197f9 61 6c 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74  al file and writ
197fa 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  e all dirty page
197fb 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  s to the databas
197fc 65 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 49 66 20  e...      ** If 
197fd 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74  the atomic-updat
197fe 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
197ff 73 20 62 65 69 6e 67 20 75 73 65 64 2c 20 74 68  s being used, th
19800 69 73 20 73 79 6e 63 20 77 69 6c 6c 20 6e 6f 74  is sync will not
19801 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63 72 65 61   ..      ** crea
19802 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  te the journal f
19803 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61  ile or perform a
19804 6e 79 20 72 65 61 6c 20 49 4f 2e 0d 0a 20 20 20  ny real IO...   
19805 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20     **..      ** 
19806 42 65 63 61 75 73 65 20 74 68 65 20 63 68 61 6e  Because the chan
19807 67 65 2d 63 6f 75 6e 74 65 72 20 70 61 67 65 20  ge-counter page 
19808 77 61 73 20 6a 75 73 74 20 6d 6f 64 69 66 69 65  was just modifie
19809 64 2c 20 75 6e 6c 65 73 73 20 74 68 65 0d 0a 20  d, unless the.. 
1980a 20 20 20 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 75       ** atomic-u
1980b 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  pdate optimizati
1980c 6f 6e 20 69 73 20 75 73 65 64 20 69 74 20 69 73  on is used it is
1980d 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20   almost certain 
1980e 74 68 61 74 20 74 68 65 0d 0a 20 20 20 20 20 20  that the..      
1980f 2a 2a 20 6a 6f 75 72 6e 61 6c 20 72 65 71 75 69  ** journal requi
19810 72 65 73 20 61 20 73 79 6e 63 20 68 65 72 65 2e  res a sync here.
19811 20 48 6f 77 65 76 65 72 2c 20 69 6e 20 6c 6f 63   However, in loc
19812 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73  king_mode=exclus
19813 69 76 65 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 6e  ive..      ** on
19814 20 61 20 73 79 73 74 65 6d 20 75 6e 64 65 72 20   a system under 
19815 6d 65 6d 6f 72 79 20 70 72 65 73 73 75 72 65 20  memory pressure 
19816 69 74 20 69 73 20 6a 75 73 74 20 70 6f 73 73 69  it is just possi
19817 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 69 73  ble that this is
19818 20 0d 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20   ..      ** not 
19819 74 68 65 20 63 61 73 65 2e 20 49 6e 20 74 68 69  the case. In thi
1981a 73 20 63 61 73 65 20 69 74 20 69 73 20 6c 69 6b  s case it is lik
1981b 65 6c 79 20 65 6e 6f 75 67 68 20 74 68 61 74 20  ely enough that 
1981c 74 68 65 20 72 65 64 75 6e 64 61 6e 74 0d 0a 20  the redundant.. 
1981d 20 20 20 20 20 2a 2a 20 78 53 79 6e 63 28 29 20       ** xSync() 
1981e 63 61 6c 6c 20 77 69 6c 6c 20 62 65 20 63 68 61  call will be cha
1981f 6e 67 65 64 20 74 6f 20 61 20 6e 6f 2d 6f 70 20  nged to a no-op 
19820 62 79 20 74 68 65 20 4f 53 20 61 6e 79 68 6f 77  by the OS anyhow
19821 2e 20 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  . ..      */..  
19822 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75      rc = syncJou
19823 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 30 29 3b  rnal(pPager, 0);
19824 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
19825 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
19826 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
19827 65 5f 65 78 69 74 3b 0d 0a 20 20 0d 0a 20 20 20  e_exit;..  ..   
19828 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72     rc = pager_wr
19829 69 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 61  ite_pagelist(pPa
1982a 67 65 72 2c 73 71 6c 69 74 65 33 50 63 61 63 68  ger,sqlite3Pcach
1982b 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65  eDirtyList(pPage
1982c 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0d 0a 20  r->pPCache));.. 
1982d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1982e 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1982f 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
19830 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
19831 4b 45 44 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  KED );..        
19832 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
19833 65 5f 6f 6e 65 5f 65 78 69 74 3b 0d 0a 20 20 20  e_one_exit;..   
19834 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
19835 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c  te3PcacheCleanAl
19836 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  l(pPager->pPCach
19837 65 29 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 2f  e);..  ..      /
19838 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 6e  * If the file on
19839 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 65   disk is not the
1983a 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 74 68   same size as th
1983b 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
1983c 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  ,..      ** then
1983d 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63   use pager_trunc
1983e 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73  ate to grow or s
1983f 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68  hrink the file h
19840 65 72 65 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ere...      */..
19841 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72        if( pPager
19842 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 67 65 72  ->dbSize!=pPager
19843 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0d  ->dbFileSize ){.
19844 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e  .        Pgno nN
19845 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ew = pPager->dbS
19846 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64  ize - (pPager->d
19847 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f  bSize==PAGER_MJ_
19848 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0d 0a  PGNO(pPager));..
19849 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1984a 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
1984b 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d  PAGER_WRITER_DBM
1984c 4f 44 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  OD );..        r
1984d 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61  c = pager_trunca
1984e 74 65 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29  te(pPager, nNew)
1984f 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
19850 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
19851 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65  oto commit_phase
19852 5f 6f 6e 65 5f 65 78 69 74 3b 0d 0a 20 20 20 20  _one_exit;..    
19853 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 20 20 2f    }..  ..      /
19854 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 20  * Finally, sync 
19855 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
19856 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28  e. */..      if(
19857 20 21 6e 6f 53 79 6e 63 20 29 7b 0d 0a 20 20 20   !noSync ){..   
19858 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
19859 33 50 61 67 65 72 53 79 6e 63 28 70 50 61 67 65  3PagerSync(pPage
1985a 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  r);..      }..  
1985b 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42      IOTRACE(("DB
1985c 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67  SYNC %p\n", pPag
1985d 65 72 29 29 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  er))..    }..  }
1985e 0d 0a 0d 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65  ....commit_phase
1985f 5f 6f 6e 65 5f 65 78 69 74 3a 0d 0a 20 20 69 66  _one_exit:..  if
19860 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19861 26 26 20 21 70 61 67 65 72 55 73 65 57 61 6c 28  && !pagerUseWal(
19862 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20 20 20  pPager) ){..    
19863 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d  pPager->eState =
19864 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 49   PAGER_WRITER_FI
19865 4e 49 53 48 45 44 3b 0d 0a 20 20 7d 0d 0a 20 20  NISHED;..  }..  
19866 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
19867 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74  .../*..** When t
19868 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
19869 63 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 61  called, the data
1986a 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65  base file has be
1986b 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0d 0a 2a  en completely..*
1986c 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66  * updated to ref
1986d 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67 65 73  lect the changes
1986e 20 6d 61 64 65 20 62 79 20 74 68 65 20 63 75 72   made by the cur
1986f 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
19870 20 61 6e 64 0d 0a 2a 2a 20 73 79 6e 63 65 64 20   and..** synced 
19871 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75  to disk. The jou
19872 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20  rnal file still 
19873 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69  exists in the fi
19874 6c 65 2d 73 79 73 74 65 6d 20 0d 0a 2a 2a 20 74  le-system ..** t
19875 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20  hough, and if a 
19876 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61  failure occurs a
19877 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20  t this point it 
19878 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0d  will eventually.
19879 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 61  .** be used as a
1987a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64   hot-journal and
1987b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
1987c 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20  nsaction rolled 
1987d 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  back...**..** Th
1987e 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61  is function fina
1987f 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61  lizes the journa
19880 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62  l file, either b
19881 79 20 64 65 6c 65 74 69 6e 67 2c 20 0d 0a 2a 2a  y deleting, ..**
19882 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70   truncating or p
19883 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67  artially zeroing
19884 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20   it, so that it 
19885 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0d  cannot be used .
19886 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72  .** for hot-jour
19887 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e  nal rollback. On
19888 63 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20  ce this is done 
19889 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1988a 69 73 0d 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62  is..** irrevocab
1988b 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0d 0a 2a  ly committed...*
1988c 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  *..** If an erro
1988d 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20  r occurs, an IO 
1988e 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
1988f 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70  turned and the p
19890 61 67 65 72 0d 0a 2a 2a 20 6d 6f 76 65 73 20 69  ager..** moves i
19891 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 74  nto the error st
19892 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ate. Otherwise, 
19893 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
19894 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  urned...*/..SQLI
19895 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19896 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
19897 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 20  tPhaseTwo(Pager 
19898 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 69 6e 74  *pPager){..  int
19899 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1989a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1989b 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
1989c 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73   */....  /* This
1989d 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
1989e 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66  not be called if
1989f 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68   a prior error h
198a0 61 73 20 6f 63 63 75 72 72 65 64 2e 0d 0a 20 20  as occurred...  
198a1 2a 2a 20 42 75 74 20 69 66 20 28 64 75 65 20 74  ** But if (due t
198a2 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72  o a coding error
198a3 20 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74 68   elsewhere in th
198a4 65 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f 65  e system) it doe
198a5 73 20 67 65 74 0d 0a 20 20 2a 2a 20 63 61 6c 6c  s get..  ** call
198a6 65 64 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20  ed, just return 
198a7 74 68 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63  the same error c
198a8 6f 64 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e  ode without doin
198a9 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0d 0a  g anything. */..
198aa 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67    if( NEVER(pPag
198ab 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72  er->errCode) ) r
198ac 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
198ad 72 43 6f 64 65 3b 0d 0a 0d 0a 20 20 61 73 73 65  rCode;....  asse
198ae 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61  rt( pPager->eSta
198af 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52  te==PAGER_WRITER
198b0 5f 4c 4f 43 4b 45 44 0d 0a 20 20 20 20 20 20 20  _LOCKED..       
198b1 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  || pPager->eStat
198b2 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f  e==PAGER_WRITER_
198b3 46 49 4e 49 53 48 45 44 0d 0a 20 20 20 20 20 20  FINISHED..      
198b4 20 7c 7c 20 28 70 61 67 65 72 55 73 65 57 61 6c   || (pagerUseWal
198b5 28 70 50 61 67 65 72 29 20 26 26 20 70 50 61 67  (pPager) && pPag
198b6 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45  er->eState==PAGE
198b7 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f  R_WRITER_CACHEMO
198b8 44 29 0d 0a 20 20 29 3b 0d 0a 20 20 61 73 73 65  D)..  );..  asse
198b9 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72  rt( assert_pager
198ba 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29  _state(pPager) )
198bb 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6e 20 6f 70 74  ;....  /* An opt
198bc 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68  imization. If th
198bd 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6e  e database was n
198be 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69  ot actually modi
198bf 66 69 65 64 20 64 75 72 69 6e 67 0d 0a 20 20 2a  fied during..  *
198c0 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  * this transacti
198c1 6f 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69 73  on, the pager is
198c2 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c   running in excl
198c3 75 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69  usive-mode and i
198c4 73 0d 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65  s..  ** using pe
198c5 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
198c6 73 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  s, then this fun
198c7 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
198c8 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68  ...  **..  ** Th
198c9 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a  e start of the j
198ca 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72  ournal file curr
198cb 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61  ently contains a
198cc 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 20   single journal 
198cd 0d 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 69  ..  ** header wi
198ce 74 68 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  th the nRec fiel
198cf 64 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 73  d set to 0. If s
198d0 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73  uch a journal is
198d1 20 75 73 65 64 20 61 73 0d 0a 20 20 2a 2a 20 61   used as..  ** a
198d2 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 75 72   hot-journal dur
198d3 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  ing hot-journal 
198d4 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 61 6e  rollback, 0 chan
198d5 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 64 65  ges will be made
198d6 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61  ..  ** to the da
198d7 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20  tabase file. So 
198d8 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
198d9 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75   to zero the jou
198da 72 6e 61 6c 20 0d 0a 20 20 2a 2a 20 68 65 61 64  rnal ..  ** head
198db 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61  er. Since the pa
198dc 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73  ger is in exclus
198dd 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20  ive mode, there 
198de 69 73 20 6e 6f 20 6e 65 65 64 0d 0a 20 20 2a 2a  is no need..  **
198df 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63   to drop any loc
198e0 6b 73 20 65 69 74 68 65 72 2e 0d 0a 20 20 2a 2f  ks either...  */
198e1 0d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
198e2 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52  eState==PAGER_WR
198e3 49 54 45 52 5f 4c 4f 43 4b 45 44 20 0d 0a 20 20  ITER_LOCKED ..  
198e4 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c   && pPager->excl
198e5 75 73 69 76 65 4d 6f 64 65 20 0d 0a 20 20 20 26  usiveMode ..   &
198e6 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
198e7 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
198e8 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
198e9 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ..  ){..    asse
198ea 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
198eb 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f  nalOff==JOURNAL_
198ec 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 7c  HDR_SZ(pPager) |
198ed 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  | !pPager->journ
198ee 61 6c 4f 66 66 20 29 3b 0d 0a 20 20 20 20 70 50  alOff );..    pP
198ef 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50  ager->eState = P
198f0 41 47 45 52 5f 52 45 41 44 45 52 3b 0d 0a 20 20  AGER_READER;..  
198f1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
198f2 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 50 41  OK;..  }....  PA
198f3 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d 49  GERTRACE(("COMMI
198f4 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  T %d\n", PAGERID
198f5 28 70 50 61 67 65 72 29 29 29 3b 0d 0a 20 20 72  (pPager)));..  r
198f6 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
198f7 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
198f8 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
198f9 74 65 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ter);..  return 
198fa 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
198fb 65 72 2c 20 72 63 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  er, rc);..}..../
198fc 2a 0d 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65  *..** If a write
198fd 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
198fe 6f 70 65 6e 2c 20 74 68 65 6e 20 61 6c 6c 20 63  open, then all c
198ff 68 61 6e 67 65 73 20 6d 61 64 65 20 77 69 74 68  hanges made with
19900 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 74 72 61 6e  in the ..** tran
19901 73 61 63 74 69 6f 6e 20 61 72 65 20 72 65 76 65  saction are reve
19902 72 74 65 64 20 61 6e 64 20 74 68 65 20 63 75 72  rted and the cur
19903 72 65 6e 74 20 77 72 69 74 65 2d 74 72 61 6e 73  rent write-trans
19904 61 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64  action is closed
19905 2e 0d 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  ...** The pager 
19906 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 50 41  falls back to PA
19907 47 45 52 5f 52 45 41 44 45 52 20 73 74 61 74 65  GER_READER state
19908 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   if successful, 
19909 6f 72 20 50 41 47 45 52 5f 45 52 52 4f 52 0d 0a  or PAGER_ERROR..
1990a 2a 2a 20 73 74 61 74 65 20 69 66 20 61 6e 20 65  ** state if an e
1990b 72 72 6f 72 20 6f 63 63 75 72 73 2e 0d 0a 2a 2a  rror occurs...**
1990c 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  ..** If the page
1990d 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  r is already in 
1990e 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 61 74  PAGER_ERROR stat
1990f 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  e when this func
19910 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0d  tion is called,.
19911 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 50  .** it returns P
19912 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 6d 6d  ager.errCode imm
19913 65 64 69 61 74 65 6c 79 2e 20 4e 6f 20 77 6f 72  ediately. No wor
19914 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 69  k is performed i
19915 6e 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 2a 2a  n this case...**
19916 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
19917 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65  in rollback mode
19918 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
19919 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 66 75 6e  performs two fun
1991a 63 74 69 6f 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ctions:..**..** 
1991b 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 20 62 61    1) It rolls ba
1991c 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ck the journal f
1991d 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e 67 20 61  ile, restoring a
1991e 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
1991f 20 61 6e 64 20 0d 0a 2a 2a 20 20 20 20 20 20 69   and ..**      i
19920 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70  n-memory cache p
19921 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 61 74  ages to the stat
19922 65 20 74 68 65 79 20 77 65 72 65 20 69 6e 20 77  e they were in w
19923 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
19924 69 6f 6e 0d 0a 2a 2a 20 20 20 20 20 20 77 61 73  ion..**      was
19925 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0d 0a 2a 2a   opened, and..**
19926 0d 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e  ..**   2) It fin
19927 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e  alizes the journ
19928 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74  al file, so that
19929 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20   it is not used 
1992a 66 6f 72 20 68 6f 74 0d 0a 2a 2a 20 20 20 20 20  for hot..**     
1992b 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79   rollback at any
1992c 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75   point in the fu
1992d 74 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 69  ture...**..** Fi
1992e 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68  nalization of th
1992f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28  e journal file (
19930 74 61 73 6b 20 32 29 20 69 73 20 6f 6e 6c 79 20  task 2) is only 
19931 70 65 72 66 6f 72 6d 65 64 20 69 66 20 74 68 65  performed if the
19932 20 0d 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 69   ..** rollback i
19933 73 20 73 75 63 63 65 73 73 66 75 6c 2e 0d 0a 2a  s successful...*
19934 2a 0d 0a 2a 2a 20 49 6e 20 57 41 4c 20 6d 6f 64  *..** In WAL mod
19935 65 2c 20 61 6c 6c 20 63 61 63 68 65 2d 65 6e 74  e, all cache-ent
19936 72 69 65 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ries containing 
19937 64 61 74 61 20 6d 6f 64 69 66 69 65 64 20 77 69  data modified wi
19938 74 68 69 6e 20 74 68 65 0d 0a 2a 2a 20 63 75 72  thin the..** cur
19939 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
1993a 20 61 72 65 20 65 69 74 68 65 72 20 65 78 70 65   are either expe
1993b 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61  lled from the ca
1993c 63 68 65 20 6f 72 20 72 65 76 65 72 74 65 64 20  che or reverted 
1993d 74 6f 0d 0a 2a 2a 20 74 68 65 69 72 20 70 72 65  to..** their pre
1993e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61  -transaction sta
1993f 74 65 20 62 79 20 72 65 2d 72 65 61 64 69 6e 67  te by re-reading
19940 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64   data from the d
19941 61 74 61 62 61 73 65 20 6f 72 0d 0a 2a 2a 20 57  atabase or..** W
19942 41 4c 20 66 69 6c 65 73 2e 20 54 68 65 20 57 41  AL files. The WA
19943 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  L transaction is
19944 20 74 68 65 6e 20 63 6c 6f 73 65 64 2e 0d 0a 2a   then closed...*
19945 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
19946 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
19947 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72  erRollback(Pager
19948 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 69 6e   *pPager){..  in
19949 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1994a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1994b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
1994c 65 20 2a 2f 0d 0a 20 20 50 41 47 45 52 54 52 41  e */..  PAGERTRA
1994d 43 45 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64  CE(("ROLLBACK %d
1994e 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
1994f 67 65 72 29 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  ger)));....  /* 
19950 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20  PagerRollback() 
19951 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 63 61  is a no-op if ca
19952 6c 6c 65 64 20 69 6e 20 52 45 41 44 45 52 20 6f  lled in READER o
19953 72 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 49 66  r OPEN state. If
19954 0d 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72  ..  ** the pager
19955 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
19956 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c 20  he ERROR state, 
19957 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20  the rollback is 
19958 6e 6f 74 20 0d 0a 20 20 2a 2a 20 61 74 74 65 6d  not ..  ** attem
19959 70 74 65 64 20 68 65 72 65 2e 20 49 6e 73 74 65  pted here. Inste
1995a 61 64 2c 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ad, the error co
1995b 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  de is returned t
1995c 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a 20  o the caller... 
1995d 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 61   */..  assert( a
1995e 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
1995f 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20  e(pPager) );..  
19960 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61  if( pPager->eSta
19961 74 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 20  te==PAGER_ERROR 
19962 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d  ) return pPager-
19963 3e 65 72 72 43 6f 64 65 3b 0d 0a 20 20 69 66 28  >errCode;..  if(
19964 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c   pPager->eState<
19965 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 20  =PAGER_READER ) 
19966 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
19967 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 61 67 65 72  ;....  if( pager
19968 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29  UseWal(pPager) )
19969 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0d  {..    int rc2;.
1996a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1996b 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28  3PagerSavepoint(
1996c 70 50 61 67 65 72 2c 20 53 41 56 45 50 4f 49 4e  pPager, SAVEPOIN
1996d 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 2d 31 29 3b  T_ROLLBACK, -1);
1996e 0d 0a 20 20 20 20 72 63 32 20 3d 20 70 61 67 65  ..    rc2 = page
1996f 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
19970 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  n(pPager, pPager
19971 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0d 0a 20  ->setMaster);.. 
19972 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
19973 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 3b  E_OK ) rc = rc2;
19974 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 69  ..  }else if( !i
19975 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
19976 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53  d) || pPager->eS
19977 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
19978 45 52 5f 4c 4f 43 4b 45 44 20 29 7b 0d 0a 20 20  ER_LOCKED ){..  
19979 20 20 69 6e 74 20 65 53 74 61 74 65 20 3d 20 70    int eState = p
1997a 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3b 0d 0a  Pager->eState;..
1997b 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
1997c 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
1997d 50 61 67 65 72 2c 20 30 29 3b 0d 0a 20 20 20 20  Pager, 0);..    
1997e 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20 65 53  if( !MEMDB && eS
1997f 74 61 74 65 3e 50 41 47 45 52 5f 57 52 49 54 45  tate>PAGER_WRITE
19980 52 5f 4c 4f 43 4b 45 44 20 29 7b 0d 0a 20 20 20  R_LOCKED ){..   
19981 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 68     /* This can h
19982 61 70 70 65 6e 20 75 73 69 6e 67 20 6a 6f 75 72  appen using jour
19983 6e 61 6c 5f 6d 6f 64 65 3d 6f 66 66 2e 20 4d 6f  nal_mode=off. Mo
19984 76 65 20 74 68 65 20 70 61 67 65 72 20 74 6f 20  ve the pager to 
19985 74 68 65 20 65 72 72 6f 72 20 0d 0a 20 20 20 20  the error ..    
19986 20 20 2a 2a 20 73 74 61 74 65 20 74 6f 20 69 6e    ** state to in
19987 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
19988 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
19989 63 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65  cache may not be
1998a 20 74 72 75 73 74 65 64 2e 0d 0a 20 20 20 20 20   trusted...     
1998b 20 2a 2a 20 41 6e 79 20 61 63 74 69 76 65 20 72   ** Any active r
1998c 65 61 64 65 72 73 20 77 69 6c 6c 20 67 65 74 20  eaders will get 
1998d 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0d 0a 20  SQLITE_ABORT... 
1998e 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 70       */..      p
1998f 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d  Pager->errCode =
19990 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0d 0a   SQLITE_ABORT;..
19991 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 53        pPager->eS
19992 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 52 52  tate = PAGER_ERR
19993 4f 52 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  OR;..      retur
19994 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  n rc;..    }..  
19995 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d  }else{..    rc =
19996 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
19997 70 50 61 67 65 72 2c 20 30 29 3b 0d 0a 20 20 7d  pPager, 0);..  }
19998 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  ....  assert( pP
19999 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
1999a 47 45 52 5f 52 45 41 44 45 52 20 7c 7c 20 72 63  GER_READER || rc
1999b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a  !=SQLITE_OK );..
1999c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1999d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
1999e 51 4c 49 54 45 5f 46 55 4c 4c 0d 0a 20 20 20 20  QLITE_FULL..    
1999f 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c        || rc==SQL
199a0 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 28 72 63  ITE_NOMEM || (rc
199a1 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 49  &0xFF)==SQLITE_I
199a2 4f 45 52 52 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  OERR );....  /* 
199a3 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
199a4 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c  rs during a ROLL
199a5 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20  BACK, we can no 
199a6 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 65  longer trust the
199a7 20 70 61 67 65 72 0d 0a 20 20 2a 2a 20 63 61 63   pager..  ** cac
199a8 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65  he. So call page
199a9 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65  r_error() on the
199aa 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65   way out to make
199ab 20 61 6e 79 20 65 72 72 6f 72 20 70 65 72 73 69   any error persi
199ac 73 74 65 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  stent...  */..  
199ad 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72  return pager_err
199ae 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0d  or(pPager, rc);.
199af 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
199b0 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
199b1 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
199b2 20 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c   opened read-onl
199b3 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45  y.  Return FALSE
199b4 0d 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61  ..** if the data
199b5 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f  base is (in theo
199b6 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0d 0a 2a  ry) writable...*
199b7 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
199b8 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65  E u8 sqlite3Page
199b9 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65  rIsreadonly(Page
199ba 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 72  r *pPager){..  r
199bb 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65  eturn pPager->re
199bc 61 64 4f 6e 6c 79 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  adOnly;..}..../*
199bd 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
199be 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  number of refere
199bf 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
199c0 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  r...*/..SQLITE_P
199c1 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
199c2 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28  e3PagerRefcount(
199c3 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d  Pager *pPager){.
199c4 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
199c5 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
199c6 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
199c7 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
199c8 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 78  eturn the approx
199c9 69 6d 61 74 65 20 6e 75 6d 62 65 72 20 6f 66 20  imate number of 
199ca 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
199cb 63 75 72 72 65 6e 74 6c 79 0d 0a 2a 2a 20 75 73  currently..** us
199cc 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20  ed by the pager 
199cd 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61 74  and its associat
199ce 65 64 20 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53  ed cache...*/..S
199cf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
199d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 65  t sqlite3PagerMe
199d1 6d 55 73 65 64 28 50 61 67 65 72 20 2a 70 50 61  mUsed(Pager *pPa
199d2 67 65 72 29 7b 0d 0a 20 20 69 6e 74 20 70 65 72  ger){..  int per
199d3 50 61 67 65 53 69 7a 65 20 3d 20 70 50 61 67 65  PageSize = pPage
199d4 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 70 50  r->pageSize + pP
199d5 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 2b 20 73  ager->nExtra + s
199d6 69 7a 65 6f 66 28 50 67 48 64 72 29 0d 0a 20 20  izeof(PgHdr)..  
199d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
199d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
199d9 20 20 20 2b 20 35 2a 73 69 7a 65 6f 66 28 76 6f     + 5*sizeof(vo
199da 69 64 2a 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  id*);..  return 
199db 70 65 72 50 61 67 65 53 69 7a 65 2a 73 71 6c 69  perPageSize*sqli
199dc 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
199dd 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
199de 68 65 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20  he)..           
199df 2b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  + sqlite3MallocS
199e0 69 7a 65 28 70 50 61 67 65 72 29 0d 0a 20 20 20  ize(pPager)..   
199e1 20 20 20 20 20 20 20 20 2b 20 70 50 61 67 65 72          + pPager
199e2 2d 3e 70 61 67 65 53 69 7a 65 3b 0d 0a 7d 0d 0a  ->pageSize;..}..
199e3 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
199e4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
199e5 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
199e6 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0d  specified page..
199e7 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
199e8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
199e9 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74  agerPageRefcount
199ea 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 29 7b  (DbPage *pPage){
199eb 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
199ec 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63  e3PcachePageRefc
199ed 6f 75 6e 74 28 70 50 61 67 65 29 3b 0d 0a 7d 0d  ount(pPage);..}.
199ee 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
199ef 5f 54 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  _TEST../*..** Th
199f0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
199f1 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61  ed for testing a
199f2 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79  nd analysis only
199f3 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
199f4 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74  IVATE int *sqlit
199f5 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67  e3PagerStats(Pag
199f6 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20  er *pPager){..  
199f7 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d  static int a[11]
199f8 3b 0d 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69  ;..  a[0] = sqli
199f9 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
199fa 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
199fb 65 29 3b 0d 0a 20 20 61 5b 31 5d 20 3d 20 73 71  e);..  a[1] = sq
199fc 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63  lite3PcachePagec
199fd 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
199fe 61 63 68 65 29 3b 0d 0a 20 20 61 5b 32 5d 20 3d  ache);..  a[2] =
199ff 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65   sqlite3PcacheGe
19a00 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65  tCachesize(pPage
19a01 72 2d 3e 70 50 43 61 63 68 65 29 3b 0d 0a 20 20  r->pPCache);..  
19a02 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65  a[3] = pPager->e
19a03 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45  State==PAGER_OPE
19a04 4e 20 3f 20 2d 31 20 3a 20 28 69 6e 74 29 20 70  N ? -1 : (int) p
19a05 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0d 0a  Pager->dbSize;..
19a06 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d    a[4] = pPager-
19a07 3e 65 53 74 61 74 65 3b 0d 0a 20 20 61 5b 35 5d  >eState;..  a[5]
19a08 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
19a09 64 65 3b 0d 0a 20 20 61 5b 36 5d 20 3d 20 70 50  de;..  a[6] = pP
19a0a 61 67 65 72 2d 3e 6e 48 69 74 3b 0d 0a 20 20 61  ager->nHit;..  a
19a0b 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d  [7] = pPager->nM
19a0c 69 73 73 3b 0d 0a 20 20 61 5b 38 5d 20 3d 20 30  iss;..  a[8] = 0
19a0d 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65  ;  /* Used to be
19a0e 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a   pPager->nOvfl *
19a0f 2f 0d 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 67  /..  a[9] = pPag
19a10 65 72 2d 3e 6e 52 65 61 64 3b 0d 0a 20 20 61 5b  er->nRead;..  a[
19a11 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57  10] = pPager->nW
19a12 72 69 74 65 3b 0d 0a 20 20 72 65 74 75 72 6e 20  rite;..  return 
19a13 61 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  a;..}..#endif...
19a14 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ./*..** Paramete
19a15 72 20 65 53 74 61 74 20 6d 75 73 74 20 62 65 20  r eStat must be 
19a16 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 42  either SQLITE_DB
19a17 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54  STATUS_CACHE_HIT
19a18 20 6f 72 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 44   or..** SQLITE_D
19a19 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 4d 49  BSTATUS_CACHE_MI
19a1a 53 53 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72  SS. Before retur
19a1b 6e 69 6e 67 2c 20 2a 70 6e 56 61 6c 20 69 73 20  ning, *pnVal is 
19a1c 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
19a1d 68 65 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63  he..** current c
19a1e 61 63 68 65 20 68 69 74 20 6f 72 20 6d 69 73 73  ache hit or miss
19a1f 20 63 6f 75 6e 74 2c 20 61 63 63 6f 72 64 69 6e   count, accordin
19a20 67 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f  g to the value o
19a21 66 20 65 53 74 61 74 2e 20 49 66 20 74 68 65 20  f eStat. If the 
19a22 0d 0a 2a 2a 20 72 65 73 65 74 20 70 61 72 61 6d  ..** reset param
19a23 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
19a24 2c 20 74 68 65 20 63 61 63 68 65 20 68 69 74 20  , the cache hit 
19a25 6f 72 20 6d 69 73 73 20 63 6f 75 6e 74 20 69 73  or miss count is
19a26 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 20 0d   zeroed before .
19a27 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  .** returning...
19a28 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
19a29 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
19a2a 61 67 65 72 43 61 63 68 65 53 74 61 74 28 50 61  agerCacheStat(Pa
19a2b 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
19a2c 20 65 53 74 61 74 2c 20 69 6e 74 20 72 65 73 65   eStat, int rese
19a2d 74 2c 20 69 6e 74 20 2a 70 6e 56 61 6c 29 7b 0d  t, int *pnVal){.
19a2e 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 74 3b 0d  .  int *piStat;.
19a2f 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 65 53 74  ...  assert( eSt
19a30 61 74 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41  at==SQLITE_DBSTA
19a31 54 55 53 5f 43 41 43 48 45 5f 48 49 54 0d 0a 20  TUS_CACHE_HIT.. 
19a32 20 20 20 20 20 20 7c 7c 20 65 53 74 61 74 3d 3d        || eStat==
19a33 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
19a34 43 41 43 48 45 5f 4d 49 53 53 0d 0a 20 20 29 3b  CACHE_MISS..  );
19a35 0d 0a 20 20 69 66 28 20 65 53 74 61 74 3d 3d 53  ..  if( eStat==S
19a36 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43  QLITE_DBSTATUS_C
19a37 41 43 48 45 5f 48 49 54 20 29 7b 0d 0a 20 20 20  ACHE_HIT ){..   
19a38 20 70 69 53 74 61 74 20 3d 20 26 70 50 61 67 65   piStat = &pPage
19a39 72 2d 3e 6e 48 69 74 3b 0d 0a 20 20 7d 65 6c 73  r->nHit;..  }els
19a3a 65 7b 0d 0a 20 20 20 20 70 69 53 74 61 74 20 3d  e{..    piStat =
19a3b 20 26 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b   &pPager->nMiss;
19a3c 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 6e 56 61  ..  }....  *pnVa
19a3d 6c 20 2b 3d 20 2a 70 69 53 74 61 74 3b 0d 0a 20  l += *piStat;.. 
19a3e 20 69 66 28 20 72 65 73 65 74 20 29 7b 0d 0a 20   if( reset ){.. 
19a3f 20 20 20 2a 70 69 53 74 61 74 20 3d 20 30 3b 0d     *piStat = 0;.
19a40 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
19a41 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
19a42 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d   this is an in-m
19a43 65 6d 6f 72 79 20 70 61 67 65 72 2e 0d 0a 2a 2f  emory pager...*/
19a44 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
19a45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
19a46 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a  rIsMemdb(Pager *
19a47 70 50 61 67 65 72 29 7b 0d 0a 20 20 72 65 74 75  pPager){..  retu
19a48 72 6e 20 4d 45 4d 44 42 3b 0d 0a 7d 0d 0a 0d 0a  rn MEMDB;..}....
19a49 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  /*..** Check tha
19a4a 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c  t there are at l
19a4b 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20  east nSavepoint 
19a4c 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e  savepoints open.
19a4d 20 49 66 20 74 68 65 72 65 20 61 72 65 0d 0a 2a   If there are..*
19a4e 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73  * currently less
19a4f 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74   than nSavepoint
19a50 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65  s open, then ope
19a51 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61  n one or more sa
19a52 76 65 70 6f 69 6e 74 73 0d 0a 2a 2a 20 74 6f 20  vepoints..** to 
19a53 6d 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 66  make up the diff
19a54 65 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e  erence. If the n
19a55 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69  umber of savepoi
19a56 6e 74 73 20 69 73 20 61 6c 72 65 61 64 79 0d 0a  nts is already..
19a57 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76  ** equal to nSav
19a58 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69  epoint, then thi
19a59 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
19a5a 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  no-op...**..** I
19a5b 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
19a5c 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c  ation fails, SQL
19a5d 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
19a5e 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72  urned. If an err
19a5f 6f 72 20 0d 0a 2a 2a 20 6f 63 63 75 72 73 20 77  or ..** occurs w
19a60 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65  hile opening the
19a61 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   sub-journal fil
19a62 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72  e, then an IO er
19a63 72 6f 72 20 63 6f 64 65 20 69 73 0d 0a 2a 2a 20  ror code is..** 
19a64 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
19a65 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d  ise, SQLITE_OK..
19a66 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
19a67 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
19a68 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
19a69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
19a6a 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 29   int nSavepoint)
19a6b 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  {..  int rc = SQ
19a6c 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
19a6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19a6e 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
19a6f 0d 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74  ..  int nCurrent
19a70 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65   = pPager->nSave
19a71 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  point;        /*
19a72 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20   Current number 
19a73 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f  of savepoints */
19a74 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  ....  assert( pP
19a75 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41  ager->eState>=PA
19a76 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45  GER_WRITER_LOCKE
19a77 44 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  D );..  assert( 
19a78 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
19a79 74 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 0d  te(pPager) );...
19a7a 0a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e  .  if( nSavepoin
19a7b 74 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50  t>nCurrent && pP
19a7c 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
19a7d 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 69 3b   ){..    int ii;
19a7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a80 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
19a81 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 50 61  iable */..    Pa
19a82 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e  gerSavepoint *aN
19a83 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
19a84 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 50 61 67        /* New Pag
19a85 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72  er.aSavepoint ar
19a86 72 61 79 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a  ray */....    /*
19a87 20 47 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e   Grow the Pager.
19a88 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79  aSavepoint array
19a89 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29   using realloc()
19a8a 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
19a8b 4e 4f 4d 45 4d 0d 0a 20 20 20 20 2a 2a 20 69 66  NOMEM..    ** if
19a8c 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
19a8d 66 61 69 6c 73 2e 20 4f 74 68 65 72 77 69 73 65  fails. Otherwise
19a8e 2c 20 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 70  , zero the new p
19a8f 6f 72 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 61  ortion in case a
19a90 20 0d 0a 20 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63   ..    ** malloc
19a91 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20   failure occurs 
19a92 77 68 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67  while populating
19a93 20 69 74 20 69 6e 20 74 68 65 20 66 6f 72 28 2e   it in the for(.
19a94 2e 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0d  ..) loop below..
19a95 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 4e 65  .    */..    aNe
19a96 77 20 3d 20 28 50 61 67 65 72 53 61 76 65 70 6f  w = (PagerSavepo
19a97 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 52 65 61  int *)sqlite3Rea
19a98 6c 6c 6f 63 28 0d 0a 20 20 20 20 20 20 20 20 70  lloc(..        p
19a99 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
19a9a 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53  t, sizeof(PagerS
19a9b 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 65 70  avepoint)*nSavep
19a9c 6f 69 6e 74 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  oint..    );..  
19a9d 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0d 0a    if( !aNew ){..
19a9e 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
19a9f 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
19aa0 7d 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61  }..    memset(&a
19aa1 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30  New[nCurrent], 0
19aa2 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43  , (nSavepoint-nC
19aa3 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66  urrent) * sizeof
19aa4 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29  (PagerSavepoint)
19aa5 29 3b 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  );..    pPager->
19aa6 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65  aSavepoint = aNe
19aa7 77 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 50 6f 70  w;....    /* Pop
19aa8 75 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53  ulate the PagerS
19aa9 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75  avepoint structu
19aaa 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74  res just allocat
19aab 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28  ed. */..    for(
19aac 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c  ii=nCurrent; ii<
19aad 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
19aae 29 7b 0d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69  ){..      aNew[i
19aaf 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 67 65  i].nOrig = pPage
19ab0 72 2d 3e 64 62 53 69 7a 65 3b 0d 0a 20 20 20 20  r->dbSize;..    
19ab1 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
19ab2 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61  ger->jfd) && pPa
19ab3 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
19ab4 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 4e  0 ){..        aN
19ab5 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d  ew[ii].iOffset =
19ab6 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
19ab7 4f 66 66 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  Off;..      }els
19ab8 65 7b 0d 0a 20 20 20 20 20 20 20 20 61 4e 65 77  e{..        aNew
19ab9 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a  [ii].iOffset = J
19aba 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
19abb 61 67 65 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  ager);..      }.
19abc 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e  .      aNew[ii].
19abd 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 65 72  iSubRec = pPager
19abe 2d 3e 6e 53 75 62 52 65 63 3b 0d 0a 20 20 20 20  ->nSubRec;..    
19abf 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61    aNew[ii].pInSa
19ac0 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65  vepoint = sqlite
19ac1 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50  3BitvecCreate(pP
19ac2 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0d 0a  ager->dbSize);..
19ac3 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 5b        if( !aNew[
19ac4 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  ii].pInSavepoint
19ac5 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74   ){..        ret
19ac6 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
19ac7 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
19ac8 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61    if( pagerUseWa
19ac9 6c 28 70 50 61 67 65 72 29 20 29 7b 0d 0a 20 20  l(pPager) ){..  
19aca 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c        sqlite3Wal
19acb 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72  Savepoint(pPager
19acc 2d 3e 70 57 61 6c 2c 20 61 4e 65 77 5b 69 69 5d  ->pWal, aNew[ii]
19acd 2e 61 57 61 6c 44 61 74 61 29 3b 0d 0a 20 20 20  .aWalData);..   
19ace 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 50 61 67     }..      pPag
19acf 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
19ad0 20 69 69 2b 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20   ii+1;..    }.. 
19ad1 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
19ad2 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 6e  r->nSavepoint==n
19ad3 53 61 76 65 70 6f 69 6e 74 20 29 3b 0d 0a 20 20  Savepoint );..  
19ad4 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65    assertTruncate
19ad5 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65  Constraint(pPage
19ad6 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65  r);..  }....  re
19ad7 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
19ad8 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
19ad9 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
19ada 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c   rollback or rel
19adb 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20  ease (commit) a 
19adc 73 61 76 65 70 6f 69 6e 74 2e 0d 0a 2a 2a 20 54  savepoint...** T
19add 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20  he savepoint to 
19ade 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62  release or rollb
19adf 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  ack need not be 
19ae0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
19ae1 79 20 0d 0a 2a 2a 20 63 72 65 61 74 65 64 20 73  y ..** created s
19ae2 61 76 65 70 6f 69 6e 74 2e 0d 0a 2a 2a 0d 0a 2a  avepoint...**..*
19ae3 2a 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69  * Parameter op i
19ae4 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 20  s always either 
19ae5 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
19ae6 43 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f  CK or SAVEPOINT_
19ae7 52 45 4c 45 41 53 45 2e 0d 0a 2a 2a 20 49 66 20  RELEASE...** If 
19ae8 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f  it is SAVEPOINT_
19ae9 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65  RELEASE, then re
19aea 6c 65 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f  lease and destro
19aeb 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  y the savepoint 
19aec 77 69 74 68 0d 0a 2a 2a 20 69 6e 64 65 78 20 69  with..** index i
19aed 53 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 74  Savepoint. If it
19aee 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f   is SAVEPOINT_RO
19aef 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c  LLBACK, then rol
19af0 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65  lback all change
19af1 73 0d 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20  s..** that have 
19af2 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20 74  occurred since t
19af3 68 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76  he specified sav
19af4 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74  epoint was creat
19af5 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
19af6 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 6f 6c  savepoint to rol
19af7 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65  lback or release
19af8 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
19af9 79 20 70 61 72 61 6d 65 74 65 72 20 0d 0a 2a 2a  y parameter ..**
19afa 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 76   iSavepoint. A v
19afb 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20  alue of 0 means 
19afc 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68  to operate on th
19afd 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65  e outermost save
19afe 70 6f 69 6e 74 0d 0a 2a 2a 20 28 74 68 65 20 66  point..** (the f
19aff 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41  irst created). A
19b00 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72   value of (Pager
19b01 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d  .nSavepoint-1) m
19b02 65 61 6e 73 20 6f 70 65 72 61 74 65 0d 0a 2a 2a  eans operate..**
19b03 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   on the most rec
19b04 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61  ently created sa
19b05 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76  vepoint. If iSav
19b06 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65  epoint is greate
19b07 72 20 74 68 61 6e 0d 0a 2a 2a 20 28 50 61 67 65  r than..** (Page
19b08 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c  r.nSavepoint-1),
19b09 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
19b0a 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d  ion is a no-op..
19b0b 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20 6e 65 67  .**..** If a neg
19b0c 61 74 69 76 65 20 76 61 6c 75 65 20 69 73 20 70  ative value is p
19b0d 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
19b0e 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  nction, then the
19b0f 20 63 75 72 72 65 6e 74 0d 0a 2a 2a 20 74 72 61   current..** tra
19b10 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
19b11 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73  ed back. This is
19b12 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 63 61   different to ca
19b13 6c 6c 69 6e 67 20 0d 0a 2a 2a 20 73 71 6c 69 74  lling ..** sqlit
19b14 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
19b15 29 20 62 65 63 61 75 73 65 20 74 68 69 73 20 66  ) because this f
19b16 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
19b17 20 74 65 72 6d 69 6e 61 74 65 0d 0a 2a 2a 20 74   terminate..** t
19b18 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
19b19 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  r unlock the dat
19b1a 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72  abase, it just r
19b1b 65 73 74 6f 72 65 73 20 74 68 65 20 0d 0a 2a 2a  estores the ..**
19b1c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
19b1d 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
19b1e 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e   original state.
19b1f 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 61 6e 79   ..**..** In any
19b20 20 63 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70   case, all savep
19b21 6f 69 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e  oints with an in
19b22 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e  dex greater than
19b23 20 69 53 61 76 65 70 6f 69 6e 74 20 0d 0a 2a 2a   iSavepoint ..**
19b24 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 2e 20   are destroyed. 
19b25 49 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c  If this is a rel
19b26 65 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 28  ease operation (
19b27 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
19b28 4c 45 41 53 45 29 2c 0d 0a 2a 2a 20 74 68 65 6e  LEASE),..** then
19b29 20 73 61 76 65 70 6f 69 6e 74 20 69 53 61 76 65   savepoint iSave
19b2a 70 6f 69 6e 74 20 69 73 20 61 6c 73 6f 20 64 65  point is also de
19b2b 73 74 72 6f 79 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  stroyed...**..**
19b2c 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
19b2d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
19b2e 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 6d 6f  _NOMEM if a memo
19b2f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
19b30 69 6c 73 2c 0d 0a 2a 2a 20 6f 72 20 61 6e 20 49  ils,..** or an I
19b31 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  O error code if 
19b32 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
19b33 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67  rs while rolling
19b34 20 62 61 63 6b 20 61 20 0d 0a 2a 2a 20 73 61 76   back a ..** sav
19b35 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72  epoint. If no er
19b36 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49  rors occur, SQLI
19b37 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
19b38 64 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c 49 54 45 5f  d...*/ ..SQLITE_
19b39 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19b3a 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e  te3PagerSavepoin
19b3b 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
19b3c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61   int op, int iSa
19b3d 76 65 70 6f 69 6e 74 29 7b 0d 0a 20 20 69 6e 74  vepoint){..  int
19b3e 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
19b3f 72 43 6f 64 65 3b 20 20 20 20 20 20 20 2f 2a 20  rCode;       /* 
19b40 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
19b41 0d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d  ..  assert( op==
19b42 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
19b43 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49  E || op==SAVEPOI
19b44 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0d 0a  NT_ROLLBACK );..
19b45 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70    assert( iSavep
19b46 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53  oint>=0 || op==S
19b47 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
19b48 4b 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 72 63  K );....  if( rc
19b49 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
19b4a 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72  Savepoint<pPager
19b4b 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0d  ->nSavepoint ){.
19b4c 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20  .    int ii;    
19b4d 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
19b4e 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d  tor variable */.
19b4f 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20  .    int nNew;  
19b50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
19b51 72 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73  r of remaining s
19b52 61 76 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20  avepoints after 
19b53 74 68 69 73 20 6f 70 2e 20 2a 2f 0d 0a 0d 0a 20  this op. */.... 
19b54 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
19b55 20 68 6f 77 20 6d 61 6e 79 20 73 61 76 65 70 6f   how many savepo
19b56 69 6e 74 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20  ints will still 
19b57 62 65 20 61 63 74 69 76 65 20 61 66 74 65 72 20  be active after 
19b58 74 68 69 73 0d 0a 20 20 20 20 2a 2a 20 6f 70 65  this..    ** ope
19b59 72 61 74 69 6f 6e 2e 20 53 74 6f 72 65 20 74 68  ration. Store th
19b5a 69 73 20 76 61 6c 75 65 20 69 6e 20 6e 4e 65 77  is value in nNew
19b5b 2e 20 54 68 65 6e 20 66 72 65 65 20 72 65 73 6f  . Then free reso
19b5c 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  urces associated
19b5d 20 0d 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61   ..    ** with a
19b5e 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74 68  ny savepoints th
19b5f 61 74 20 61 72 65 20 64 65 73 74 72 6f 79 65 64  at are destroyed
19b60 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
19b61 6f 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  on...    */..   
19b62 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f 69   nNew = iSavepoi
19b63 6e 74 20 2b 20 28 28 20 6f 70 3d 3d 53 41 56 45  nt + (( op==SAVE
19b64 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 20  POINT_RELEASE ) 
19b65 3f 20 30 20 3a 20 31 29 3b 0d 0a 20 20 20 20 66  ? 0 : 1);..    f
19b66 6f 72 28 69 69 3d 6e 4e 65 77 3b 20 69 69 3c 70  or(ii=nNew; ii<p
19b67 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
19b68 74 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  t; ii++){..     
19b69 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
19b6a 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53  stroy(pPager->aS
19b6b 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e  avepoint[ii].pIn
19b6c 53 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 20  Savepoint);..   
19b6d 20 7d 0d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   }..    pPager->
19b6e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 4e 65  nSavepoint = nNe
19b6f 77 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20  w;....    /* If 
19b70 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73  this is a releas
19b71 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f  e of the outermo
19b72 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72  st savepoint, tr
19b73 75 6e 63 61 74 65 20 0d 0a 20 20 20 20 2a 2a 20  uncate ..    ** 
19b74 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
19b75 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e  to zero bytes in
19b76 20 73 69 7a 65 2e 20 2a 2f 0d 0a 20 20 20 20 69   size. */..    i
19b77 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  f( op==SAVEPOINT
19b78 5f 52 45 4c 45 41 53 45 20 29 7b 0d 0a 20 20 20  _RELEASE ){..   
19b79 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26     if( nNew==0 &
19b7a 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
19b7b 3e 73 6a 66 64 29 20 29 7b 0d 0a 20 20 20 20 20  >sjfd) ){..     
19b7c 20 20 20 2f 2a 20 4f 6e 6c 79 20 74 72 75 6e 63     /* Only trunc
19b7d 61 74 65 20 69 66 20 69 74 20 69 73 20 61 6e 20  ate if it is an 
19b7e 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f  in-memory sub-jo
19b7f 75 72 6e 61 6c 2e 20 2a 2f 0d 0a 20 20 20 20 20  urnal. */..     
19b80 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
19b81 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  MemJournal(pPage
19b82 72 2d 3e 73 6a 66 64 29 20 29 7b 0d 0a 20 20 20  r->sjfd) ){..   
19b83 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
19b84 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50  te3OsTruncate(pP
19b85 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b 0d  ager->sjfd, 0);.
19b86 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
19b87 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
19b88 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a   );..        }..
19b89 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
19b8a 6e 53 75 62 52 65 63 20 3d 20 30 3b 0d 0a 20 20  nSubRec = 0;..  
19b8b 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
19b8c 20 20 2f 2a 20 45 6c 73 65 20 74 68 69 73 20 69    /* Else this i
19b8d 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65  s a rollback ope
19b8e 72 61 74 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b  ration, playback
19b8f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 73   the specified s
19b90 61 76 65 70 6f 69 6e 74 2e 0d 0a 20 20 20 20 2a  avepoint...    *
19b91 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 74  * If this is a t
19b92 65 6d 70 2d 66 69 6c 65 2c 20 69 74 20 69 73 20  emp-file, it is 
19b93 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68  possible that th
19b94 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
19b95 61 73 0d 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 79  as..    ** not y
19b96 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20  et been opened. 
19b97 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
19b98 72 65 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20  re have been no 
19b99 63 68 61 6e 67 65 73 20 74 6f 0d 0a 20 20 20 20  changes to..    
19b9a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
19b9b 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 70 6c 61  file, so the pla
19b9c 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20  yback operation 
19b9d 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e 0d  can be skipped..
19b9e 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 65 6c 73  .    */..    els
19b9f 65 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61  e if( pagerUseWa
19ba0 6c 28 70 50 61 67 65 72 29 20 7c 7c 20 69 73 4f  l(pPager) || isO
19ba1 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
19ba2 20 29 7b 0d 0a 20 20 20 20 20 20 50 61 67 65 72   ){..      Pager
19ba3 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
19ba4 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30  point = (nNew==0
19ba5 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61  )?0:&pPager->aSa
19ba6 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b  vepoint[nNew-1];
19ba7 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  ..      rc = pag
19ba8 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f  erPlaybackSavepo
19ba9 69 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76  int(pPager, pSav
19baa 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20  epoint);..      
19bab 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54  assert(rc!=SQLIT
19bac 45 5f 44 4f 4e 45 29 3b 0d 0a 20 20 20 20 7d 0d  E_DONE);..    }.
19bad 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
19bae 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
19baf 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c  * Return the ful
19bb0 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  l pathname of th
19bb1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
19bb2 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
19bb3 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
19bb4 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c  *sqlite3PagerFil
19bb5 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61  ename(Pager *pPa
19bb6 67 65 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20  ger){..  return 
19bb7 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
19bb8 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  e;..}..../*..** 
19bb9 52 65 74 75 72 6e 20 74 68 65 20 56 46 53 20 73  Return the VFS s
19bba 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65  tructure for the
19bbb 20 70 61 67 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c   pager...*/..SQL
19bbc 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
19bbd 74 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73  t sqlite3_vfs *s
19bbe 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 50  qlite3PagerVfs(P
19bbf 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a  ager *pPager){..
19bc0 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
19bc1 3e 70 56 66 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  >pVfs;..}..../*.
19bc2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66  .** Return the f
19bc3 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74  ile handle for t
19bc4 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
19bc5 20 61 73 73 6f 63 69 61 74 65 64 0d 0a 2a 2a 20   associated..** 
19bc6 77 69 74 68 20 74 68 65 20 70 61 67 65 72 2e 20  with the pager. 
19bc7 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 75   This might retu
19bc8 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66  rn NULL if the f
19bc9 69 6c 65 20 68 61 73 0d 0a 2a 2a 20 6e 6f 74 20  ile has..** not 
19bca 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e  yet been opened.
19bcb 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
19bcc 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c  VATE sqlite3_fil
19bcd 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46  e *sqlite3PagerF
19bce 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ile(Pager *pPage
19bcf 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 50  r){..  return pP
19bd0 61 67 65 72 2d 3e 66 64 3b 0d 0a 7d 0d 0a 0d 0a  ager->fd;..}....
19bd1 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
19bd2 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20  e full pathname 
19bd3 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
19bd4 69 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ile...*/..SQLITE
19bd5 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
19bd6 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
19bd7 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67  rJournalname(Pag
19bd8 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20  er *pPager){..  
19bd9 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a  return pPager->z
19bda 4a 6f 75 72 6e 61 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f  Journal;..}..../
19bdb 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  *..** Return tru
19bdc 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c  e if fsync() cal
19bdd 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20  ls are disabled 
19bde 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20  for this pager. 
19bdf 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0d 0a 2a   Return FALSE..*
19be0 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 72  * if fsync()s ar
19be1 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d 61  e executed norma
19be2 6c 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  lly...*/..SQLITE
19be3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19be4 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28  ite3PagerNosync(
19be5 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d  Pager *pPager){.
19be6 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
19be7 2d 3e 6e 6f 53 79 6e 63 3b 0d 0a 7d 0d 0a 0d 0a  ->noSync;..}....
19be8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
19be9 53 5f 43 4f 44 45 43 0d 0a 2f 2a 0d 0a 2a 2a 20  S_CODEC../*..** 
19bea 53 65 74 20 6f 72 20 72 65 74 72 69 65 76 65 20  Set or retrieve 
19beb 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68  the codec for th
19bec 69 73 20 70 61 67 65 72 0d 0a 2a 2f 0d 0a 53 51  is pager..*/..SQ
19bed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19bee 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  d sqlite3PagerSe
19bef 74 43 6f 64 65 63 28 0d 0a 20 20 50 61 67 65 72  tCodec(..  Pager
19bf0 20 2a 70 50 61 67 65 72 2c 0d 0a 20 20 76 6f 69   *pPager,..  voi
19bf1 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69  d *(*xCodec)(voi
19bf2 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e  d*,void*,Pgno,in
19bf3 74 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 43  t),..  void (*xC
19bf4 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f  odecSizeChng)(vo
19bf5 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0d 0a 20  id*,int,int),.. 
19bf6 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72   void (*xCodecFr
19bf7 65 65 29 28 76 6f 69 64 2a 29 2c 0d 0a 20 20 76  ee)(void*),..  v
19bf8 6f 69 64 20 2a 70 43 6f 64 65 63 0d 0a 29 7b 0d  oid *pCodec..){.
19bf9 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78  .  if( pPager->x
19bfa 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61 67  CodecFree ) pPag
19bfb 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70  er->xCodecFree(p
19bfc 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0d  Pager->pCodec);.
19bfd 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
19bfe 63 20 3d 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44  c = pPager->memD
19bff 62 20 3f 20 30 20 3a 20 78 43 6f 64 65 63 3b 0d  b ? 0 : xCodec;.
19c00 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
19c01 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64  cSizeChng = xCod
19c02 65 63 53 69 7a 65 43 68 6e 67 3b 0d 0a 20 20 70  ecSizeChng;..  p
19c03 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65  Pager->xCodecFre
19c04 65 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0d  e = xCodecFree;.
19c05 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65  .  pPager->pCode
19c06 63 20 3d 20 70 43 6f 64 65 63 3b 0d 0a 20 20 70  c = pCodec;..  p
19c07 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70  agerReportSize(p
19c08 50 61 67 65 72 29 3b 0d 0a 7d 0d 0a 53 51 4c 49  Pager);..}..SQLI
19c09 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19c0a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
19c0b 43 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50 61  Codec(Pager *pPa
19c0c 67 65 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20  ger){..  return 
19c0d 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0d  pPager->pCodec;.
19c0e 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  .}..#endif....#i
19c0f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
19c10 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 2f 2a  T_AUTOVACUUM../*
19c11 0d 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61  ..** Move the pa
19c12 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 69  ge pPg to locati
19c13 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 66  on pgno in the f
19c14 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ile...**..** The
19c15 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65  re must be no re
19c16 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
19c17 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20  page previously 
19c18 6c 6f 63 61 74 65 64 20 61 74 0d 0a 2a 2a 20 70  located at..** p
19c19 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63 61  gno (which we ca
19c1a 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67  ll pPgOld) thoug
19c1b 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20 61  h that page is a
19c1c 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0d 0a 2a 2a  llowed to be..**
19c1d 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74   in cache.  If t
19c1e 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73  he page previous
19c1f 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67  ly located at pg
19c20 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  no is not alread
19c21 79 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c  y..** in the rol
19c22 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69  lback journal, i
19c23 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65  t is not put the
19c24 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72 6f  re by by this ro
19c25 75 74 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52  utine...**..** R
19c26 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
19c27 20 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e   page pPg remain
19c28 20 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67   valid. Updating
19c29 20 61 6e 79 0d 0a 2a 2a 20 6d 65 74 61 2d 64 61   any..** meta-da
19c2a 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
19c2b 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74  th pPg (i.e. dat
19c2c 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  a stored in the 
19c2d 6e 45 78 74 72 61 20 62 79 74 65 73 0d 0a 2a 2a  nExtra bytes..**
19c2e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67   allocated along
19c2f 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29 20   with the page) 
19c30 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62  is the responsib
19c31 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
19c32 6c 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 74  ler...**..** A t
19c33 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
19c34 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74  be active when t
19c35 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
19c36 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74  alled. It used t
19c37 6f 20 62 65 0d 0a 2a 2a 20 72 65 71 75 69 72 65  o be..** require
19c38 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65  d that a stateme
19c39 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
19c3a 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62  as not active, b
19c3b 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74  ut this restrict
19c3c 69 6f 6e 0d 0a 2a 2a 20 68 61 73 20 62 65 65 6e  ion..** has been
19c3d 20 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45   removed (CREATE
19c3e 20 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20   INDEX needs to 
19c3f 6d 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e  move a page when
19c40 20 61 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a   a statement..**
19c41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
19c42 61 63 74 69 76 65 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  active)...**..**
19c43 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 61   If the fourth a
19c44 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d 69  rgument, isCommi
19c45 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  t, is non-zero, 
19c46 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69  then this page i
19c47 73 20 62 65 69 6e 67 0d 0a 2a 2a 20 6d 6f 76 65  s being..** move
19c48 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64  d as part of a d
19c49 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69  atabase reorgani
19c4a 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f  zation just befo
19c4b 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
19c4c 6f 6e 20 0d 0a 2a 2a 20 69 73 20 62 65 69 6e 67  on ..** is being
19c4d 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74   committed. In t
19c4e 68 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 20  his case, it is 
19c4f 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20  guaranteed that 
19c50 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  the database pag
19c51 65 20 0d 0a 2a 2a 20 70 50 67 20 72 65 66 65 72  e ..** pPg refer
19c52 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s to will not be
19c53 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69   written to agai
19c54 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72  n within this tr
19c55 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  ansaction...**..
19c56 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
19c57 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
19c58 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49  TE_NOMEM or an I
19c59 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  O error code if 
19c5a 61 6e 20 65 72 72 6f 72 0d 0a 2a 2a 20 6f 63 63  an error..** occ
19c5b 75 72 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  urs. Otherwise, 
19c5c 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
19c5d 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  E_OK...*/..SQLIT
19c5e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19c5f 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
19c60 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
19c61 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50  , DbPage *pPg, P
19c62 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 73  gno pgno, int is
19c63 43 6f 6d 6d 69 74 29 7b 0d 0a 20 20 50 67 48 64  Commit){..  PgHd
19c64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 20  r *pPgOld;      
19c65 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19c66 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77  page being overw
19c67 72 69 74 74 65 6e 2e 20 2a 2f 0d 0a 20 20 50 67  ritten. */..  Pg
19c68 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20  no needSyncPgno 
19c69 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c  = 0;       /* Ol
19c6a 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e  d value of pPg->
19c6b 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73  pgno, if sync is
19c6c 20 72 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20   required */..  
19c6d 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
19c6e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19c6f 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
19c70 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b    Pgno origPgno;
19c71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19c72 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  * The original p
19c73 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 0d  age number */...
19c74 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
19c75 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 61 73 73  nRef>0 );..  ass
19c76 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74  ert( pPager->eSt
19c77 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45  ate==PAGER_WRITE
19c78 52 5f 43 41 43 48 45 4d 4f 44 0d 0a 20 20 20 20  R_CACHEMOD..    
19c79 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53     || pPager->eS
19c7a 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
19c7b 45 52 5f 44 42 4d 4f 44 0d 0a 20 20 29 3b 0d 0a  ER_DBMOD..  );..
19c7c 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
19c7d 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
19c7e 67 65 72 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  ger) );....  /* 
19c7f 49 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 61  In order to be a
19c80 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c  ble to rollback,
19c81 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
19c82 74 61 62 61 73 65 20 6d 75 73 74 20 6a 6f 75 72  tabase must jour
19c83 6e 61 6c 0d 0a 20 20 2a 2a 20 74 68 65 20 70 61  nal..  ** the pa
19c84 67 65 20 77 65 20 61 72 65 20 6d 6f 76 69 6e 67  ge we are moving
19c85 20 66 72 6f 6d 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   from...  */..  
19c86 69 66 28 20 4d 45 4d 44 42 20 29 7b 0d 0a 20 20  if( MEMDB ){..  
19c87 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
19c88 67 65 72 57 72 69 74 65 28 70 50 67 29 3b 0d 0a  gerWrite(pPg);..
19c89 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
19c8a 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a  urn rc;..  }....
19c8b 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
19c8c 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20   being moved is 
19c8d 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f  dirty and has no
19c8e 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79 20  t been saved by 
19c8f 74 68 65 20 6c 61 74 65 73 74 0d 0a 20 20 2a 2a  the latest..  **
19c90 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e   savepoint, then
19c91 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   save the curren
19c92 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  t contents of th
19c93 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
19c94 0d 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e  ..  ** sub-journ
19c95 61 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20  al now. This is 
19c96 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64  required to hand
19c97 6c 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  le the following
19c98 20 73 63 65 6e 61 72 69 6f 3a 0d 0a 20 20 2a 2a   scenario:..  **
19c99 0d 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0d  ..  **   BEGIN;.
19c9a 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e  .  **     <journ
19c9b 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 6e 20  al page X, then 
19c9c 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d 65 6d  modify it in mem
19c9d 6f 72 79 3e 0d 0a 20 20 2a 2a 20 20 20 20 20 53  ory>..  **     S
19c9e 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0d 0a 20  AVEPOINT one;.. 
19c9f 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20   **       <Move 
19ca0 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69  page X to locati
19ca1 6f 6e 20 59 3e 0d 0a 20 20 2a 2a 20 20 20 20 20  on Y>..  **     
19ca2 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b  ROLLBACK TO one;
19ca3 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20  ..  **..  ** If 
19ca4 70 61 67 65 20 58 20 77 65 72 65 20 6e 6f 74 20  page X were not 
19ca5 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73  written to the s
19ca6 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c  ub-journal here,
19ca7 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0d 0a 20   it would not.. 
19ca8 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 6c 65 20   ** be possible 
19ca9 74 6f 20 72 65 73 74 6f 72 65 20 69 74 73 20 63  to restore its c
19caa 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 74 68 65  ontents when the
19cab 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e   "ROLLBACK TO on
19cac 65 22 0d 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  e"..  ** stateme
19cad 6e 74 20 77 65 72 65 20 69 73 20 70 72 6f 63 65  nt were is proce
19cae 73 73 65 64 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  ssed...  **..  *
19caf 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  * subjournalPage
19cb0 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 61  () may need to a
19cb1 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f  llocate space to
19cb2 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f   store pPg->pgno
19cb3 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20 6f 6e 65 20   into..  ** one 
19cb4 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e  or more savepoin
19cb5 74 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 20  t bitvecs. This 
19cb6 69 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68  is the reason th
19cb7 69 73 20 66 75 6e 63 74 69 6f 6e 0d 0a 20 20 2a  is function..  *
19cb8 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  * may return SQL
19cb9 49 54 45 5f 4e 4f 4d 45 4d 2e 0d 0a 20 20 2a 2f  ITE_NOMEM...  */
19cba 0d 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61  ..  if( pPg->fla
19cbb 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 0d 0a  gs&PGHDR_DIRTY..
19cbc 20 20 20 26 26 20 73 75 62 6a 52 65 71 75 69 72     && subjRequir
19cbd 65 73 50 61 67 65 28 70 50 67 29 0d 0a 20 20 20  esPage(pPg)..   
19cbe 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  && SQLITE_OK!=(r
19cbf 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  c = subjournalPa
19cc0 67 65 28 70 50 67 29 29 0d 0a 20 20 29 7b 0d 0a  ge(pPg))..  ){..
19cc1 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
19cc2 20 20 7d 0d 0a 0d 0a 20 20 50 41 47 45 52 54 52    }....  PAGERTR
19cc3 41 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61  ACE(("MOVE %d pa
19cc4 67 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d  ge %d (needSync=
19cc5 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c  %d) moves to %d\
19cc6 6e 22 2c 20 0d 0a 20 20 20 20 20 20 50 41 47 45  n", ..      PAGE
19cc7 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
19cc8 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c  ->pgno, (pPg->fl
19cc9 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
19cca 59 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29  YNC)?1:0, pgno))
19ccb 3b 0d 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d  ;..  IOTRACE(("M
19ccc 4f 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c  OVE %p %d %d\n",
19ccd 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67   pPager, pPg->pg
19cce 6e 6f 2c 20 70 67 6e 6f 29 29 0d 0a 0d 0a 20 20  no, pgno))....  
19ccf 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  /* If the journa
19cd0 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79  l needs to be sy
19cd1 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 70 61  nc()ed before pa
19cd2 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61 6e  ge pPg->pgno can
19cd3 0d 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65  ..  ** be writte
19cd4 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d  n to, store pPg-
19cd5 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76  >pgno in local v
19cd6 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63  ariable needSync
19cd7 50 67 6e 6f 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  Pgno...  **..  *
19cd8 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69  * If the isCommi
19cd9 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  t flag is set, t
19cda 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
19cdb 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74  to remember that
19cdc 0d 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  ..  ** the journ
19cdd 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  al needs to be s
19cde 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64  ync()ed before d
19cdf 61 74 61 62 61 73 65 20 70 61 67 65 20 70 50 67  atabase page pPg
19ce0 2d 3e 70 67 6e 6f 20 0d 0a 20 20 2a 2a 20 63 61  ->pgno ..  ** ca
19ce1 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e  n be written to.
19ce2 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   The caller has 
19ce3 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64  already promised
19ce4 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f   not to write to
19ce5 20 69 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66   it...  */..  if
19ce6 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  ( (pPg->flags&PG
19ce7 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 26  HDR_NEED_SYNC) &
19ce8 26 20 21 69 73 43 6f 6d 6d 69 74 20 29 7b 0d 0a  & !isCommit ){..
19ce9 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f      needSyncPgno
19cea 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0d 0a 20   = pPg->pgno;.. 
19ceb 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49     assert( pageI
19cec 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c  nJournal(pPg) ||
19ced 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
19cee 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b  r->dbOrigSize );
19cef 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ..    assert( pP
19cf0 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
19cf1 49 52 54 59 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  IRTY );..  }....
19cf2 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68    /* If the cach
19cf3 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67  e contains a pag
19cf4 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62  e with page-numb
19cf5 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20  er pgno, remove 
19cf6 69 74 0d 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74  it..  ** from it
19cf7 73 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c  s hash chain. Al
19cf8 73 6f 2c 20 69 66 20 74 68 65 20 50 47 48 44 52  so, if the PGHDR
19cf9 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
19cfa 77 61 73 20 73 65 74 20 66 6f 72 20 0d 0a 20 20  was set for ..  
19cfb 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66  ** page pgno bef
19cfc 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f  ore the 'move' o
19cfd 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65  peration, it nee
19cfe 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65  ds to be retaine
19cff 64 20 0d 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  d ..  ** for the
19d00 20 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 72   page moved ther
19d01 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 50 67 2d  e...  */..  pPg-
19d02 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52  >flags &= ~PGHDR
19d03 5f 4e 45 45 44 5f 53 59 4e 43 3b 0d 0a 20 20 70  _NEED_SYNC;..  p
19d04 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c 6f  PgOld = pager_lo
19d05 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e  okup(pPager, pgn
19d06 6f 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 21  o);..  assert( !
19d07 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64  pPgOld || pPgOld
19d08 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0d 0a 20 20  ->nRef==1 );..  
19d09 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0d 0a 20  if( pPgOld ){.. 
19d0a 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
19d0b 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26   (pPgOld->flags&
19d0c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
19d0d 3b 0d 0a 20 20 20 20 69 66 28 20 4d 45 4d 44 42  ;..    if( MEMDB
19d0e 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 44 6f   ){..      /* Do
19d0f 20 6e 6f 74 20 64 69 73 63 61 72 64 20 70 61 67   not discard pag
19d10 65 73 20 66 72 6f 6d 20 61 6e 20 69 6e 2d 6d 65  es from an in-me
19d11 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 73 69  mory database si
19d12 6e 63 65 20 77 65 20 6d 69 67 68 74 0d 0a 20 20  nce we might..  
19d13 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 72      ** need to r
19d14 6f 6c 6c 62 61 63 6b 20 6c 61 74 65 72 2e 20 20  ollback later.  
19d15 4a 75 73 74 20 6d 6f 76 65 20 74 68 65 20 70 61  Just move the pa
19d16 67 65 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61  ge out of the wa
19d17 79 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c  y. */..      sql
19d18 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70  ite3PcacheMove(p
19d19 50 67 4f 6c 64 2c 20 70 50 61 67 65 72 2d 3e 64  PgOld, pPager->d
19d1a 62 53 69 7a 65 2b 31 29 3b 0d 0a 20 20 20 20 7d  bSize+1);..    }
19d1b 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c  else{..      sql
19d1c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70  ite3PcacheDrop(p
19d1d 50 67 4f 6c 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a  PgOld);..    }..
19d1e 20 20 7d 0d 0a 0d 0a 20 20 6f 72 69 67 50 67 6e    }....  origPgn
19d1f 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0d 0a  o = pPg->pgno;..
19d20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
19d21 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0d  ove(pPg, pgno);.
19d22 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
19d23 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0d  MakeDirty(pPg);.
19d24 0a 0d 0a 20 20 2f 2a 20 46 6f 72 20 61 6e 20 69  ...  /* For an i
19d25 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
19d26 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  e, make sure the
19d27 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63   original page c
19d28 6f 6e 74 69 6e 75 65 73 0d 0a 20 20 2a 2a 20 74  ontinues..  ** t
19d29 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 61 73 65  o exist, in case
19d2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
19d2b 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c 6c 20 62   needs to roll b
19d2c 61 63 6b 2e 20 20 55 73 65 20 70 50 67 4f 6c 64  ack.  Use pPgOld
19d2d 0d 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 6f 72  ..  ** as the or
19d2e 69 67 69 6e 61 6c 20 70 61 67 65 20 73 69 6e 63  iginal page sinc
19d2f 65 20 69 74 20 68 61 73 20 61 6c 72 65 61 64 79  e it has already
19d30 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e   been allocated.
19d31 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 4d 45  ..  */..  if( ME
19d32 4d 44 42 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  MDB ){..    asse
19d33 72 74 28 20 70 50 67 4f 6c 64 20 29 3b 0d 0a 20  rt( pPgOld );.. 
19d34 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
19d35 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 6f 72 69  Move(pPgOld, ori
19d36 67 50 67 6e 6f 29 3b 0d 0a 20 20 20 20 73 71 6c  gPgno);..    sql
19d37 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
19d38 50 67 4f 6c 64 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  PgOld);..  }....
19d39 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67    if( needSyncPg
19d3a 6e 6f 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66  no ){..    /* If
19d3b 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 69 73   needSyncPgno is
19d3c 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
19d3d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
19d3e 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0d 0a 20   needs to be .. 
19d3f 20 20 20 2a 2a 20 73 79 6e 63 28 29 65 64 20 62     ** sync()ed b
19d40 65 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69  efore any data i
19d41 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 61 74  s written to dat
19d42 61 62 61 73 65 20 66 69 6c 65 20 70 61 67 65 20  abase file page 
19d43 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0d 0a 20  needSyncPgno... 
19d44 20 20 20 2a 2a 20 43 75 72 72 65 6e 74 6c 79 2c     ** Currently,
19d45 20 6e 6f 20 73 75 63 68 20 70 61 67 65 20 65 78   no such page ex
19d46 69 73 74 73 20 69 6e 20 74 68 65 20 70 61 67 65  ists in the page
19d47 2d 63 61 63 68 65 20 61 6e 64 20 74 68 65 20 0d  -cache and the .
19d48 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a 6f 75 72  .    ** "is jour
19d49 6e 61 6c 65 64 22 20 62 69 74 76 65 63 20 66 6c  naled" bitvec fl
19d4a 61 67 20 68 61 73 20 62 65 65 6e 20 73 65 74 2e  ag has been set.
19d4b 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f 20 62   This needs to b
19d4c 65 20 72 65 6d 65 64 69 65 64 20 62 79 0d 0a 20  e remedied by.. 
19d4d 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 74 68     ** loading th
19d4e 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
19d4f 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20  pager-cache and 
19d50 73 65 74 74 69 6e 67 20 74 68 65 20 50 47 48 44  setting the PGHD
19d51 52 5f 4e 45 45 44 5f 53 59 4e 43 0d 0a 20 20 20  R_NEED_SYNC..   
19d52 20 2a 2a 20 66 6c 61 67 2e 0d 0a 20 20 20 20 2a   ** flag...    *
19d53 2a 0d 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  *..    ** If the
19d54 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64   attempt to load
19d55 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74   the page into t
19d56 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61  he page-cache fa
19d57 69 6c 73 2c 20 28 64 75 65 0d 0a 20 20 20 20 2a  ils, (due..    *
19d58 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20  * to a malloc() 
19d59 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20  or IO failure), 
19d5a 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e  clear the bit in
19d5b 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b   the pInJournal[
19d5c 5d 0d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e  ]..    ** array.
19d5d 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74   Otherwise, if t
19d5e 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65  he page is loade
19d5f 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67  d and written ag
19d60 61 69 6e 20 69 6e 0d 0a 20 20 20 20 2a 2a 20 74  ain in..    ** t
19d61 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  his transaction,
19d62 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 74   it may be writt
19d63 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
19d64 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0d 0a  se file before..
19d65 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e      ** it is syn
19d66 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ced into the jou
19d67 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
19d68 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20  way, it may end 
19d69 75 70 20 69 6e 0d 0a 20 20 20 20 2a 2a 20 74 68  up in..    ** th
19d6a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  e journal file t
19d6b 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69  wice, but that i
19d6c 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e  s not a problem.
19d6d 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 50 67  ..    */..    Pg
19d6e 48 64 72 20 2a 70 50 67 48 64 72 3b 0d 0a 20 20  Hdr *pPgHdr;..  
19d6f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
19d70 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e  gerGet(pPager, n
19d71 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50  eedSyncPgno, &pP
19d72 67 48 64 72 29 3b 0d 0a 20 20 20 20 69 66 28 20  gHdr);..    if( 
19d73 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
19d74 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65 64  ..      if( need
19d75 53 79 6e 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72  SyncPgno<=pPager
19d76 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0d  ->dbOrigSize ){.
19d77 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
19d78 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
19d79 63 65 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ce!=0 );..      
19d7a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
19d7b 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 49 6e  lear(pPager->pIn
19d7c 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e  Journal, needSyn
19d7d 63 50 67 6e 6f 2c 20 70 50 61 67 65 72 2d 3e 70  cPgno, pPager->p
19d7e 54 6d 70 53 70 61 63 65 29 3b 0d 0a 20 20 20 20  TmpSpace);..    
19d7f 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74 75 72    }..      retur
19d80 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  n rc;..    }..  
19d81 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20    pPgHdr->flags 
19d82 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  |= PGHDR_NEED_SY
19d83 4e 43 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  NC;..    sqlite3
19d84 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
19d85 70 50 67 48 64 72 29 3b 0d 0a 20 20 20 20 73 71  pPgHdr);..    sq
19d86 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
19d87 70 50 67 48 64 72 29 3b 0d 0a 20 20 7d 0d 0a 0d  pPgHdr);..  }...
19d88 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
19d89 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  _OK;..}..#endif.
19d8a 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
19d8b 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
19d8c 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 73  e data for the s
19d8d 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0d 0a  pecified page...
19d8e 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
19d8f 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
19d90 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50  PagerGetData(DbP
19d91 61 67 65 20 2a 70 50 67 29 7b 0d 0a 20 20 61 73  age *pPg){..  as
19d92 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e  sert( pPg->nRef>
19d93 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72  0 || pPg->pPager
19d94 2d 3e 6d 65 6d 44 62 20 29 3b 0d 0a 20 20 72 65  ->memDb );..  re
19d95 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b  turn pPg->pData;
19d96 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
19d97 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
19d98 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74  o the Pager.nExt
19d99 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74  ra bytes of "ext
19d9a 72 61 22 20 73 70 61 63 65 20 0d 0a 2a 2a 20 61  ra" space ..** a
19d9b 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
19d9c 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65  ith the specifie
19d9d 64 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  d page...*/..SQL
19d9e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19d9f 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
19da0 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70  tExtra(DbPage *p
19da1 50 67 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70  Pg){..  return p
19da2 50 67 2d 3e 70 45 78 74 72 61 3b 0d 0a 7d 0d 0a  Pg->pExtra;..}..
19da3 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 74 2f 73 65 74  ../*..** Get/set
19da4 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
19da5 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  e for this pager
19da6 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64  . Parameter eMod
19da7 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0d 0a 2a  e must be one..*
19da8 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49  * of PAGER_LOCKI
19da9 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41  NGMODE_QUERY, PA
19daa 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
19dab 4e 4f 52 4d 41 4c 20 6f 72 20 0d 0a 2a 2a 20 50  NORMAL or ..** P
19dac 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
19dad 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74  _EXCLUSIVE. If t
19dae 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
19daf 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e  not _QUERY, then
19db0 0d 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ..** the locking
19db1 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  -mode is set to 
19db2 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66  the value specif
19db3 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ied...**..** The
19db4 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20   returned value 
19db5 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52 5f  is either PAGER_
19db6 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
19db7 41 4c 20 6f 72 0d 0a 2a 2a 20 50 41 47 45 52 5f  AL or..** PAGER_
19db8 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
19db9 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e  USIVE, indicatin
19dba 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70  g the current (p
19dbb 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29  ossibly updated)
19dbc 0d 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64  ..** locking-mod
19dbd 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
19dbe 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19dbf 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
19dc0 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
19dc1 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0d 0a 20  , int eMode){.. 
19dc2 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d   assert( eMode==
19dc3 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
19dc4 45 5f 51 55 45 52 59 0d 0a 20 20 20 20 20 20 20  E_QUERY..       
19dc5 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
19dc6 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
19dc7 5f 4e 4f 52 4d 41 4c 0d 0a 20 20 20 20 20 20 20  _NORMAL..       
19dc8 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
19dc9 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
19dca 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0d 0a 20  _EXCLUSIVE );.. 
19dcb 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c   assert( PAGER_L
19dcc 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
19dcd 3c 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  <0 );..  assert(
19dce 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
19dcf 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 26 26 20  DE_NORMAL>=0 && 
19dd0 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
19dd1 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d 30 20 29  E_EXCLUSIVE>=0 )
19dd2 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
19dd3 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
19dd4 64 65 20 7c 7c 20 30 3d 3d 73 71 6c 69 74 65 33  de || 0==sqlite3
19dd5 57 61 6c 48 65 61 70 4d 65 6d 6f 72 79 28 70 50  WalHeapMemory(pP
19dd6 61 67 65 72 2d 3e 70 57 61 6c 29 20 29 3b 0d 0a  ager->pWal) );..
19dd7 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26    if( eMode>=0 &
19dd8 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  & !pPager->tempF
19dd9 69 6c 65 20 26 26 20 21 73 71 6c 69 74 65 33 57  ile && !sqlite3W
19dda 61 6c 48 65 61 70 4d 65 6d 6f 72 79 28 70 50 61  alHeapMemory(pPa
19ddb 67 65 72 2d 3e 70 57 61 6c 29 20 29 7b 0d 0a 20  ger->pWal) ){.. 
19ddc 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75     pPager->exclu
19ddd 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65  siveMode = (u8)e
19dde 4d 6f 64 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  Mode;..  }..  re
19ddf 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72  turn (int)pPager
19de0 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b  ->exclusiveMode;
19de1 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65  ..}..../*..** Se
19de2 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  t the journal-mo
19de3 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
19de4 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
19de5 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  de must be one o
19de6 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 50 41  f:..**..**    PA
19de7 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
19de8 44 45 4c 45 54 45 0d 0a 2a 2a 20 20 20 20 50 41  DELETE..**    PA
19de9 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
19dea 54 52 55 4e 43 41 54 45 0d 0a 2a 2a 20 20 20 20  TRUNCATE..**    
19deb 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
19dec 45 5f 50 45 52 53 49 53 54 0d 0a 2a 2a 20 20 20  E_PERSIST..**   
19ded 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
19dee 44 45 5f 4f 46 46 0d 0a 2a 2a 20 20 20 20 50 41  DE_OFF..**    PA
19def 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
19df0 4d 45 4d 4f 52 59 0d 0a 2a 2a 20 20 20 20 50 41  MEMORY..**    PA
19df1 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
19df2 57 41 4c 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  WAL..**..** The 
19df3 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 69 73 20 73  journalmode is s
19df4 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
19df5 73 70 65 63 69 66 69 65 64 20 69 66 20 74 68 65  specified if the
19df6 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77   change is allow
19df7 65 64 2e 0d 0a 2a 2a 20 54 68 65 20 63 68 61 6e  ed...** The chan
19df8 67 65 20 6d 61 79 20 62 65 20 64 69 73 61 6c 6c  ge may be disall
19df9 6f 77 65 64 20 66 6f 72 20 74 68 65 20 66 6f 6c  owed for the fol
19dfa 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0d  lowing reasons:.
19dfb 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20 41 6e 20  .**..**   *  An 
19dfc 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
19dfd 73 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65  se can only have
19dfe 20 69 74 73 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64   its journal_mod
19dff 65 20 73 65 74 20 74 6f 20 5f 4f 46 46 0d 0a 2a  e set to _OFF..*
19e00 2a 20 20 20 20 20 20 6f 72 20 5f 4d 45 4d 4f 52  *      or _MEMOR
19e01 59 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 20  Y...**..**   *  
19e02 54 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  Temporary databa
19e03 73 65 73 20 63 61 6e 6e 6f 74 20 68 61 76 65 20  ses cannot have 
19e04 5f 57 41 4c 20 6a 6f 75 72 6e 61 6c 6d 6f 64 65  _WAL journalmode
19e05 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65  ...**..** The re
19e06 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74 65 20  turned indicate 
19e07 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73  the current (pos
19e08 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 20 6a  sibly updated) j
19e09 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0d 0a 2a 2f  ournal-mode...*/
19e0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
19e0b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
19e0c 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  rSetJournalMode(
19e0d 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
19e0e 6e 74 20 65 4d 6f 64 65 29 7b 0d 0a 20 20 75 38  nt eMode){..  u8
19e0f 20 65 4f 6c 64 20 3d 20 70 50 61 67 65 72 2d 3e   eOld = pPager->
19e10 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20  journalMode;    
19e11 2f 2a 20 50 72 69 6f 72 20 6a 6f 75 72 6e 61 6c  /* Prior journal
19e12 6d 6f 64 65 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65  mode */....#ifde
19e13 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a  f SQLITE_DEBUG..
19e14 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 74 5f 70    /* The print_p
19e15 61 67 65 72 5f 73 74 61 74 65 28 29 20 72 6f 75  ager_state() rou
19e16 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tine is intended
19e17 20 74 6f 20 62 65 20 75 73 65 64 20 62 79 20 74   to be used by t
19e18 68 65 20 64 65 62 75 67 67 65 72 0d 0a 20 20 2a  he debugger..  *
19e19 2a 20 6f 6e 6c 79 2e 20 20 57 65 20 69 6e 76 6f  * only.  We invo
19e1a 6b 65 20 69 74 20 6f 6e 63 65 20 68 65 72 65 20  ke it once here 
19e1b 74 6f 20 73 75 70 70 72 65 73 73 20 61 20 63 6f  to suppress a co
19e1c 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 2e 20  mpiler warning. 
19e1d 2a 2f 0d 0a 20 20 70 72 69 6e 74 5f 70 61 67 65  */..  print_page
19e1e 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 3b  r_state(pPager);
19e1f 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 20 20  ..#endif......  
19e20 2f 2a 20 54 68 65 20 65 4d 6f 64 65 20 70 61 72  /* The eMode par
19e21 61 6d 65 74 65 72 20 69 73 20 61 6c 77 61 79 73  ameter is always
19e22 20 76 61 6c 69 64 20 2a 2f 0d 0a 20 20 61 73 73   valid */..  ass
19e23 65 72 74 28 20 20 20 20 20 20 65 4d 6f 64 65 3d  ert(      eMode=
19e24 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
19e25 44 45 5f 44 45 4c 45 54 45 0d 0a 20 20 20 20 20  DE_DELETE..     
19e26 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
19e27 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
19e28 44 45 5f 54 52 55 4e 43 41 54 45 0d 0a 20 20 20  DE_TRUNCATE..   
19e29 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
19e2a 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
19e2b 4d 4f 44 45 5f 50 45 52 53 49 53 54 0d 0a 20 20  MODE_PERSIST..  
19e2c 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
19e2d 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
19e2e 4c 4d 4f 44 45 5f 4f 46 46 20 0d 0a 20 20 20 20  LMODE_OFF ..    
19e2f 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
19e30 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
19e31 4f 44 45 5f 57 41 4c 20 0d 0a 20 20 20 20 20 20  ODE_WAL ..      
19e32 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
19e33 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
19e34 45 5f 4d 45 4d 4f 52 59 20 29 3b 0d 0a 0d 0a 20  E_MEMORY );.... 
19e35 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
19e36 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
19e37 66 72 6f 6d 20 74 68 65 20 4f 50 5f 4a 6f 75 72  from the OP_Jour
19e38 6e 61 6c 4d 6f 64 65 20 6f 70 63 6f 64 65 2c 20  nalMode opcode, 
19e39 61 6e 64 0d 0a 20 20 2a 2a 20 74 68 65 20 6c 6f  and..  ** the lo
19e3a 67 69 63 20 74 68 65 72 65 20 77 69 6c 6c 20 6e  gic there will n
19e3b 65 76 65 72 20 61 6c 6c 6f 77 20 61 20 74 65 6d  ever allow a tem
19e3c 70 6f 72 61 72 79 20 66 69 6c 65 20 74 6f 20 62  porary file to b
19e3d 65 20 63 68 61 6e 67 65 64 0d 0a 20 20 2a 2a 20  e changed..  ** 
19e3e 74 6f 20 57 41 4c 20 6d 6f 64 65 2e 0d 0a 20 20  to WAL mode...  
19e3f 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
19e40 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d  ager->tempFile==
19e41 30 20 7c 7c 20 65 4d 6f 64 65 21 3d 50 41 47 45  0 || eMode!=PAGE
19e42 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41  R_JOURNALMODE_WA
19e43 4c 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 44 6f 20  L );....  /* Do 
19e44 61 6c 6c 6f 77 20 74 68 65 20 6a 6f 75 72 6e 61  allow the journa
19e45 6c 6d 6f 64 65 20 6f 66 20 61 6e 20 69 6e 2d 6d  lmode of an in-m
19e46 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 74  emory database t
19e47 6f 20 62 65 20 73 65 74 20 74 6f 0d 0a 20 20 2a  o be set to..  *
19e48 2a 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72  * anything other
19e49 20 74 68 61 6e 20 4d 45 4d 4f 52 59 20 6f 72 20   than MEMORY or 
19e4a 4f 46 46 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  OFF..  */..  if(
19e4b 20 4d 45 4d 44 42 20 29 7b 0d 0a 20 20 20 20 61   MEMDB ){..    a
19e4c 73 73 65 72 74 28 20 65 4f 6c 64 3d 3d 50 41 47  ssert( eOld==PAG
19e4d 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
19e4e 45 4d 4f 52 59 20 7c 7c 20 65 4f 6c 64 3d 3d 50  EMORY || eOld==P
19e4f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
19e50 5f 4f 46 46 20 29 3b 0d 0a 20 20 20 20 69 66 28  _OFF );..    if(
19e51 20 65 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f   eMode!=PAGER_JO
19e52 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
19e53 20 26 26 20 65 4d 6f 64 65 21 3d 50 41 47 45 52   && eMode!=PAGER
19e54 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
19e55 20 29 7b 0d 0a 20 20 20 20 20 20 65 4d 6f 64 65   ){..      eMode
19e56 20 3d 20 65 4f 6c 64 3b 0d 0a 20 20 20 20 7d 0d   = eOld;..    }.
19e57 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 65 4d  .  }....  if( eM
19e58 6f 64 65 21 3d 65 4f 6c 64 20 29 7b 0d 0a 0d 0a  ode!=eOld ){....
19e59 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 20 74 68      /* Change th
19e5a 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 20  e journal mode. 
19e5b 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
19e5c 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d  pPager->eState!=
19e5d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0d 0a  PAGER_ERROR );..
19e5e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
19e5f 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d  nalMode = (u8)eM
19e60 6f 64 65 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 57  ode;....    /* W
19e61 68 65 6e 20 74 72 61 6e 73 69 73 74 69 6f 6e 69  hen transistioni
19e62 6e 67 20 66 72 6f 6d 20 54 52 55 4e 43 41 54 45  ng from TRUNCATE
19e63 20 6f 72 20 50 45 52 53 49 53 54 20 74 6f 20 61   or PERSIST to a
19e64 6e 79 20 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c  ny other journal
19e65 0d 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 20 65 78  ..    ** mode ex
19e66 63 65 70 74 20 57 41 4c 2c 20 75 6e 6c 65 73 73  cept WAL, unless
19e67 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
19e68 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78   locking_mode=ex
19e69 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 0d 0a 20  clusive mode,.. 
19e6a 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65     ** delete the
19e6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0d 0a   journal file...
19e6c 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65      */..    asse
19e6d 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e  rt( (PAGER_JOURN
19e6e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20  ALMODE_TRUNCATE 
19e6f 26 20 35 29 3d 3d 31 20 29 3b 0d 0a 20 20 20 20  & 5)==1 );..    
19e70 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a  assert( (PAGER_J
19e71 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
19e72 53 54 20 26 20 35 29 3d 3d 31 20 29 3b 0d 0a 20  ST & 5)==1 );.. 
19e73 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45     assert( (PAGE
19e74 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
19e75 4c 45 54 45 20 26 20 35 29 3d 3d 30 20 29 3b 0d  LETE & 5)==0 );.
19e76 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41  .    assert( (PA
19e77 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
19e78 4d 45 4d 4f 52 59 20 26 20 35 29 3d 3d 34 20 29  MEMORY & 5)==4 )
19e79 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ;..    assert( (
19e7a 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
19e7b 45 5f 4f 46 46 20 26 20 35 29 3d 3d 30 20 29 3b  E_OFF & 5)==0 );
19e7c 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50  ..    assert( (P
19e7d 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
19e7e 5f 57 41 4c 20 26 20 35 29 3d 3d 35 20 29 3b 0d  _WAL & 5)==5 );.
19e7f 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ...    assert( i
19e80 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
19e81 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 78 63  ) || pPager->exc
19e82 6c 75 73 69 76 65 4d 6f 64 65 20 29 3b 0d 0a 20  lusiveMode );.. 
19e83 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
19e84 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26  exclusiveMode &&
19e85 20 28 65 4f 6c 64 20 26 20 35 29 3d 3d 31 20 26   (eOld & 5)==1 &
19e86 26 20 28 65 4d 6f 64 65 20 26 20 31 29 3d 3d 30  & (eMode & 1)==0
19e87 20 29 7b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20   ){....      /* 
19e88 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
19e89 77 6f 75 6c 64 20 6c 69 6b 65 20 74 6f 20 64 65  would like to de
19e8a 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
19e8b 20 66 69 6c 65 2e 20 49 66 20 69 74 20 69 73 0d   file. If it is.
19e8c 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 70 6f  .      ** not po
19e8d 73 73 69 62 6c 65 2c 20 74 68 65 6e 20 74 68 61  ssible, then tha
19e8e 74 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c  t is not a probl
19e8f 65 6d 2e 20 44 65 6c 65 74 69 6e 67 20 74 68 65  em. Deleting the
19e90 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0d 0a 20   journal file.. 
19e91 20 20 20 20 20 2a 2a 20 68 65 72 65 20 69 73 20       ** here is 
19e92 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  an optimization 
19e93 6f 6e 6c 79 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d  only...      **.
19e94 0a 20 20 20 20 20 20 2a 2a 20 42 65 66 6f 72 65  .      ** Before
19e95 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f   deleting the jo
19e96 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 62 74 61  urnal file, obta
19e97 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  in a RESERVED lo
19e98 63 6b 20 6f 6e 20 74 68 65 0d 0a 20 20 20 20 20  ck on the..     
19e99 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
19e9a 65 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  e. This ensures 
19e9b 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
19e9c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c   file is not del
19e9d 65 74 65 64 0d 0a 20 20 20 20 20 20 2a 2a 20 77  eted..      ** w
19e9e 68 69 6c 65 20 69 74 20 69 73 20 69 6e 20 75 73  hile it is in us
19e9f 65 20 62 79 20 73 6f 6d 65 20 6f 74 68 65 72 20  e by some other 
19ea0 63 6c 69 65 6e 74 2e 0d 0a 20 20 20 20 20 20 2a  client...      *
19ea1 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
19ea2 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
19ea3 6a 66 64 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  jfd);..      if(
19ea4 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d   pPager->eLock>=
19ea5 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
19ea6 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
19ea7 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72  3OsDelete(pPager
19ea8 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  ->pVfs, pPager->
19ea9 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0d 0a 20  zJournal, 0);.. 
19eaa 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
19eab 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51       int rc = SQ
19eac 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20  LITE_OK;..      
19ead 20 20 69 6e 74 20 73 74 61 74 65 20 3d 20 70 50    int state = pP
19eae 61 67 65 72 2d 3e 65 53 74 61 74 65 3b 0d 0a 20  ager->eState;.. 
19eaf 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
19eb0 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e  tate==PAGER_OPEN
19eb1 20 7c 7c 20 73 74 61 74 65 3d 3d 50 41 47 45 52   || state==PAGER
19eb2 5f 52 45 41 44 45 52 20 29 3b 0d 0a 20 20 20 20  _READER );..    
19eb3 20 20 20 20 69 66 28 20 73 74 61 74 65 3d 3d 50      if( state==P
19eb4 41 47 45 52 5f 4f 50 45 4e 20 29 7b 0d 0a 20 20  AGER_OPEN ){..  
19eb5 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
19eb6 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c  ite3PagerSharedL
19eb7 6f 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20  ock(pPager);..  
19eb8 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
19eb9 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 74   if( pPager->eSt
19eba 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45  ate==PAGER_READE
19ebb 52 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  R ){..          
19ebc 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
19ebd 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 20 20  TE_OK );..      
19ebe 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4c 6f      rc = pagerLo
19ebf 63 6b 44 62 28 70 50 61 67 65 72 2c 20 52 45 53  ckDb(pPager, RES
19ec0 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20  ERVED_LOCK);..  
19ec1 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
19ec2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19ec3 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
19ec4 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
19ec5 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70  (pPager->pVfs, p
19ec6 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
19ec7 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   0);..        }.
19ec8 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
19ec9 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 74  =SQLITE_OK && st
19eca 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45  ate==PAGER_READE
19ecb 52 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  R ){..          
19ecc 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 70 50  pagerUnlockDb(pP
19ecd 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43  ager, SHARED_LOC
19ece 4b 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  K);..        }el
19ecf 73 65 20 69 66 28 20 73 74 61 74 65 3d 3d 50 41  se if( state==PA
19ed0 47 45 52 5f 4f 50 45 4e 20 29 7b 0d 0a 20 20 20  GER_OPEN ){..   
19ed1 20 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c         pager_unl
19ed2 6f 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20  ock(pPager);..  
19ed3 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
19ed4 20 61 73 73 65 72 74 28 20 73 74 61 74 65 3d 3d   assert( state==
19ed5 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 29  pPager->eState )
19ed6 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
19ed7 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52  }..  }....  /* R
19ed8 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6a 6f  eturn the new jo
19ed9 75 72 6e 61 6c 20 6d 6f 64 65 20 2a 2f 0d 0a 20  urnal mode */.. 
19eda 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61   return (int)pPa
19edb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
19edc 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
19edd 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
19ede 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 0d  t journal mode..
19edf 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
19ee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
19ee1 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f  agerGetJournalMo
19ee2 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
19ee3 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e  ){..  return (in
19ee4 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  t)pPager->journa
19ee5 6c 4d 6f 64 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  lMode;..}..../*.
19ee6 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
19ee7 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  if the pager is 
19ee8 69 6e 20 61 20 73 74 61 74 65 20 77 68 65 72 65  in a state where
19ee9 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 63 68 61   it is OK to cha
19eea 6e 67 65 20 74 68 65 0d 0a 2a 2a 20 6a 6f 75 72  nge the..** jour
19eeb 6e 61 6c 6d 6f 64 65 2e 20 20 4a 6f 75 72 6e 61  nalmode.  Journa
19eec 6c 6d 6f 64 65 20 63 68 61 6e 67 65 73 20 63 61  lmode changes ca
19eed 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 68  n only happen wh
19eee 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0d  en the database.
19eef 0a 2a 2a 20 69 73 20 75 6e 6d 6f 64 69 66 69 65  .** is unmodifie
19ef0 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
19ef1 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19ef2 65 33 50 61 67 65 72 4f 6b 54 6f 43 68 61 6e 67  e3PagerOkToChang
19ef3 65 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67  eJournalMode(Pag
19ef4 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20  er *pPager){..  
19ef5 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70  assert( assert_p
19ef6 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65  ager_state(pPage
19ef7 72 29 20 29 3b 0d 0a 20 20 69 66 28 20 70 50 61  r) );..  if( pPa
19ef8 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47  ger->eState>=PAG
19ef9 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d  ER_WRITER_CACHEM
19efa 4f 44 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a  OD ) return 0;..
19efb 20 20 69 66 28 20 4e 45 56 45 52 28 69 73 4f 70    if( NEVER(isOp
19efc 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
19efd 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
19efe 61 6c 4f 66 66 3e 30 29 20 29 20 72 65 74 75 72  alOff>0) ) retur
19eff 6e 20 30 3b 0d 0a 20 20 72 65 74 75 72 6e 20 31  n 0;..  return 1
19f00 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  ;..}..../*..** G
19f01 65 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d  et/set the size-
19f02 6c 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70  limit used for p
19f03 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61  ersistent journa
19f04 6c 20 66 69 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  l files...**..**
19f05 20 53 65 74 74 69 6e 67 20 74 68 65 20 73 69 7a   Setting the siz
19f06 65 20 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65  e limit to -1 me
19f07 61 6e 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20  ans no limit is 
19f08 65 6e 66 6f 72 63 65 64 2e 0d 0a 2a 2a 20 41 6e  enforced...** An
19f09 20 61 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20   attempt to set 
19f0a 61 20 6c 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20  a limit smaller 
19f0b 74 68 61 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d  than -1 is a no-
19f0c 6f 70 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  op...*/..SQLITE_
19f0d 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69  PRIVATE i64 sqli
19f0e 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53  te3PagerJournalS
19f0f 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20 2a  izeLimit(Pager *
19f10 70 50 61 67 65 72 2c 20 69 36 34 20 69 4c 69 6d  pPager, i64 iLim
19f11 69 74 29 7b 0d 0a 20 20 69 66 28 20 69 4c 69 6d  it){..  if( iLim
19f12 69 74 3e 3d 2d 31 20 29 7b 0d 0a 20 20 20 20 70  it>=-1 ){..    p
19f13 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69  Pager->journalSi
19f14 7a 65 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74  zeLimit = iLimit
19f15 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61  ;..    sqlite3Wa
19f16 6c 4c 69 6d 69 74 28 70 50 61 67 65 72 2d 3e 70  lLimit(pPager->p
19f17 57 61 6c 2c 20 69 4c 69 6d 69 74 29 3b 0d 0a 20  Wal, iLimit);.. 
19f18 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61   }..  return pPa
19f19 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ger->journalSize
19f1a 4c 69 6d 69 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  Limit;..}..../*.
19f1b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
19f1c 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 61 67  nter to the pPag
19f1d 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72 69  er->pBackup vari
19f1e 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75 70  able. The backup
19f1f 20 6d 6f 64 75 6c 65 0d 0a 2a 2a 20 69 6e 20 62   module..** in b
19f20 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e  ackup.c maintain
19f21 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  s the content of
19f22 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20   this variable. 
19f23 54 68 69 73 20 6d 6f 64 75 6c 65 0d 0a 2a 2a 20  This module..** 
19f24 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79  uses it opaquely
19f25 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
19f26 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  to sqlite3Backup
19f27 52 65 73 74 61 72 74 28 29 20 61 6e 64 0d 0a 2a  Restart() and..*
19f28 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55  * sqlite3BackupU
19f29 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0d 0a 2a  pdate() only...*
19f2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
19f2b 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  E sqlite3_backup
19f2c 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42   **sqlite3PagerB
19f2d 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a  ackupPtr(Pager *
19f2e 70 50 61 67 65 72 29 7b 0d 0a 20 20 72 65 74 75  pPager){..  retu
19f2f 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61 63  rn &pPager->pBac
19f30 6b 75 70 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64  kup;..}....#ifnd
19f31 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
19f32 41 43 55 55 4d 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e  ACUUM../*..** Un
19f33 6c 65 73 73 20 74 68 69 73 20 69 73 20 61 6e 20  less this is an 
19f34 69 6e 2d 6d 65 6d 6f 72 79 20 6f 72 20 74 65 6d  in-memory or tem
19f35 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2c  porary database,
19f36 20 63 6c 65 61 72 20 74 68 65 20 70 61 67 65 72   clear the pager
19f37 20 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c   cache...*/..SQL
19f38 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19f39 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 65   sqlite3PagerCle
19f3a 61 72 43 61 63 68 65 28 50 61 67 65 72 20 2a 70  arCache(Pager *p
19f3b 50 61 67 65 72 29 7b 0d 0a 20 20 69 66 28 20 21  Pager){..  if( !
19f3c 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d  MEMDB && pPager-
19f3d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 29 20 70  >tempFile==0 ) p
19f3e 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
19f3f 72 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a  r);..}..#endif..
19f40 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
19f41 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 2f 2a 0d 0a 2a  _OMIT_WAL../*..*
19f42 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
19f43 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
19f44 68 65 20 75 73 65 72 20 69 6e 76 6f 6b 65 73 20  he user invokes 
19f45 22 50 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63  "PRAGMA wal_chec
19f46 6b 70 6f 69 6e 74 22 2c 0d 0a 2a 2a 20 22 50 52  kpoint",..** "PR
19f47 41 47 4d 41 20 77 61 6c 5f 62 6c 6f 63 6b 69 6e  AGMA wal_blockin
19f48 67 5f 63 68 65 63 6b 70 6f 69 6e 74 22 20 6f 72  g_checkpoint" or
19f49 20 63 61 6c 6c 73 20 74 68 65 20 73 71 6c 69 74   calls the sqlit
19f4a 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
19f4b 74 28 29 0d 0a 2a 2a 20 6f 72 20 77 61 6c 5f 62  t()..** or wal_b
19f4c 6c 6f 63 6b 69 6e 67 5f 63 68 65 63 6b 70 6f 69  locking_checkpoi
19f4d 6e 74 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  nt() API functio
19f4e 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 61 72 61  ns...**..** Para
19f4f 6d 65 74 65 72 20 65 4d 6f 64 65 20 69 73 20 6f  meter eMode is o
19f50 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 48 45  ne of SQLITE_CHE
19f51 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 2c  CKPOINT_PASSIVE,
19f52 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52 54   FULL or RESTART
19f53 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
19f54 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19f55 33 50 61 67 65 72 43 68 65 63 6b 70 6f 69 6e 74  3PagerCheckpoint
19f56 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
19f57 69 6e 74 20 65 4d 6f 64 65 2c 20 69 6e 74 20 2a  int eMode, int *
19f58 70 6e 4c 6f 67 2c 20 69 6e 74 20 2a 70 6e 43 6b  pnLog, int *pnCk
19f59 70 74 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  pt){..  int rc =
19f5a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69   SQLITE_OK;..  i
19f5b 66 28 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20  f( pPager->pWal 
19f5c 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ){..    rc = sql
19f5d 69 74 65 33 57 61 6c 43 68 65 63 6b 70 6f 69 6e  ite3WalCheckpoin
19f5e 74 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20  t(pPager->pWal, 
19f5f 65 4d 6f 64 65 2c 0d 0a 20 20 20 20 20 20 20 20  eMode,..        
19f60 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e  pPager->xBusyHan
19f61 64 6c 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 42  dler, pPager->pB
19f62 75 73 79 48 61 6e 64 6c 65 72 41 72 67 2c 0d 0a  usyHandlerArg,..
19f63 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
19f64 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 2c 20 70  ckptSyncFlags, p
19f65 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
19f66 20 28 75 38 20 2a 29 70 50 61 67 65 72 2d 3e 70   (u8 *)pPager->p
19f67 54 6d 70 53 70 61 63 65 2c 0d 0a 20 20 20 20 20  TmpSpace,..     
19f68 20 20 20 70 6e 4c 6f 67 2c 20 70 6e 43 6b 70 74     pnLog, pnCkpt
19f69 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 20  ..    );..  }.. 
19f6a 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
19f6b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
19f6c 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
19f6d 72 57 61 6c 43 61 6c 6c 62 61 63 6b 28 50 61 67  rWalCallback(Pag
19f6e 65 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20  er *pPager){..  
19f6f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 57 61  return sqlite3Wa
19f70 6c 43 61 6c 6c 62 61 63 6b 28 70 50 61 67 65 72  lCallback(pPager
19f71 2d 3e 70 57 61 6c 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  ->pWal);..}..../
19f72 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  *..** Return tru
19f73 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79  e if the underly
19f74 69 6e 67 20 56 46 53 20 66 6f 72 20 74 68 65 20  ing VFS for the 
19f75 67 69 76 65 6e 20 70 61 67 65 72 20 73 75 70 70  given pager supp
19f76 6f 72 74 73 20 74 68 65 0d 0a 2a 2a 20 70 72 69  orts the..** pri
19f77 6d 69 74 69 76 65 73 20 6e 65 63 65 73 73 61 72  mitives necessar
19f78 79 20 66 6f 72 20 77 72 69 74 65 2d 61 68 65 61  y for write-ahea
19f79 64 20 6c 6f 67 67 69 6e 67 2e 0d 0a 2a 2f 0d 0a  d logging...*/..
19f7a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19f7b 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  nt sqlite3PagerW
19f7c 61 6c 53 75 70 70 6f 72 74 65 64 28 50 61 67 65  alSupported(Page
19f7d 72 20 2a 70 50 61 67 65 72 29 7b 0d 0a 20 20 63  r *pPager){..  c
19f7e 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
19f7f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64  methods *pMethod
19f80 73 20 3d 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e  s = pPager->fd->
19f81 70 4d 65 74 68 6f 64 73 3b 0d 0a 20 20 72 65 74  pMethods;..  ret
19f82 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 78 63 6c  urn pPager->excl
19f83 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 28 70 4d  usiveMode || (pM
19f84 65 74 68 6f 64 73 2d 3e 69 56 65 72 73 69 6f 6e  ethods->iVersion
19f85 3e 3d 32 20 26 26 20 70 4d 65 74 68 6f 64 73 2d  >=2 && pMethods-
19f86 3e 78 53 68 6d 4d 61 70 29 3b 0d 0a 7d 0d 0a 0d  >xShmMap);..}...
19f87 0a 2f 2a 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ./*..** Attempt 
19f88 74 6f 20 74 61 6b 65 20 61 6e 20 65 78 63 6c 75  to take an exclu
19f89 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  sive lock on the
19f8a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
19f8b 49 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  If a PENDING loc
19f8c 6b 0d 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65  k..** is obtaine
19f8d 64 20 69 6e 73 74 65 61 64 2c 20 69 6d 6d 65 64  d instead, immed
19f8e 69 61 74 65 6c 79 20 72 65 6c 65 61 73 65 20 69  iately release i
19f8f 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  t...*/..static i
19f90 6e 74 20 70 61 67 65 72 45 78 63 6c 75 73 69 76  nt pagerExclusiv
19f91 65 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61  eLock(Pager *pPa
19f92 67 65 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  ger){..  int rc;
19f93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f94 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
19f95 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20  rn code */....  
19f96 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
19f97 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f  eLock==SHARED_LO
19f98 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 4c  CK || pPager->eL
19f99 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ock==EXCLUSIVE_L
19f9a 4f 43 4b 20 29 3b 0d 0a 20 20 72 63 20 3d 20 70  OCK );..  rc = p
19f9b 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 65  agerLockDb(pPage
19f9c 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  r, EXCLUSIVE_LOC
19f9d 4b 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53  K);..  if( rc!=S
19f9e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
19f9f 20 2f 2a 20 49 66 20 74 68 65 20 61 74 74 65 6d   /* If the attem
19fa0 70 74 20 74 6f 20 67 72 61 62 20 74 68 65 20 65  pt to grab the e
19fa1 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 66 61  xclusive lock fa
19fa2 69 6c 65 64 2c 20 72 65 6c 65 61 73 65 20 74 68  iled, release th
19fa3 65 20 0d 0a 20 20 20 20 2a 2a 20 70 65 6e 64 69  e ..    ** pendi
19fa4 6e 67 20 6c 6f 63 6b 20 74 68 61 74 20 6d 61 79  ng lock that may
19fa5 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69   have been obtai
19fa6 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 20 2a 2f  ned instead.  */
19fa7 0d 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  ..    pagerUnloc
19fa8 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48 41 52  kDb(pPager, SHAR
19fa9 45 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 7d 0d 0a  ED_LOCK);..  }..
19faa 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
19fab 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c  }..../*..** Call
19fac 20 73 71 6c 69 74 65 33 57 61 6c 4f 70 65 6e 28   sqlite3WalOpen(
19fad 29 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 57 41  ) to open the WA
19fae 4c 20 68 61 6e 64 6c 65 2e 20 49 66 20 74 68 65  L handle. If the
19faf 20 70 61 67 65 72 20 69 73 20 69 6e 20 0d 0a 2a   pager is in ..*
19fb0 2a 20 65 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b  * exclusive-lock
19fb1 69 6e 67 20 6d 6f 64 65 20 77 68 65 6e 20 74 68  ing mode when th
19fb2 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
19fb3 61 6c 6c 65 64 2c 20 74 61 6b 65 20 61 6e 20 45  alled, take an E
19fb4 58 43 4c 55 53 49 56 45 0d 0a 2a 2a 20 6c 6f 63  XCLUSIVE..** loc
19fb5 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
19fb6 65 20 66 69 6c 65 20 61 6e 64 20 75 73 65 20 68  e file and use h
19fb7 65 61 70 2d 6d 65 6d 6f 72 79 20 74 6f 20 73 74  eap-memory to st
19fb8 6f 72 65 20 74 68 65 20 77 61 6c 2d 69 6e 64 65  ore the wal-inde
19fb9 78 0d 0a 2a 2a 20 69 6e 2e 20 4f 74 68 65 72 77  x..** in. Otherw
19fba 69 73 65 2c 20 75 73 65 20 74 68 65 20 6e 6f 72  ise, use the nor
19fbb 6d 61 6c 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72  mal shared-memor
19fbc 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  y...*/..static i
19fbd 6e 74 20 70 61 67 65 72 4f 70 65 6e 57 61 6c 28  nt pagerOpenWal(
19fbe 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0d  Pager *pPager){.
19fbf 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
19fc0 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65  TE_OK;....  asse
19fc1 72 74 28 20 70 50 61 67 65 72 2d 3e 70 57 61 6c  rt( pPager->pWal
19fc2 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 74  ==0 && pPager->t
19fc3 65 6d 70 46 69 6c 65 3d 3d 30 20 29 3b 0d 0a 20  empFile==0 );.. 
19fc4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
19fc5 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  >eLock==SHARED_L
19fc6 4f 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65  OCK || pPager->e
19fc7 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f  Lock==EXCLUSIVE_
19fc8 4c 4f 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e  LOCK || pPager->
19fc9 6e 6f 52 65 61 64 6c 6f 63 6b 29 3b 0d 0a 0d 0a  noReadlock);....
19fca 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
19fcb 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  r is already in 
19fcc 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20  exclusive-mode, 
19fcd 74 68 65 20 57 41 4c 20 6d 6f 64 75 6c 65 20 77  the WAL module w
19fce 69 6c 6c 20 75 73 65 20 0d 0a 20 20 2a 2a 20 68  ill use ..  ** h
19fcf 65 61 70 2d 6d 65 6d 6f 72 79 20 66 6f 72 20 74  eap-memory for t
19fd0 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 69 6e 73  he wal-index ins
19fd1 74 65 61 64 20 6f 66 20 74 68 65 20 56 46 53 20  tead of the VFS 
19fd2 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 0d 0a  shared-memory ..
19fd3 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74    ** implementat
19fd4 69 6f 6e 2e 20 54 61 6b 65 20 74 68 65 20 65 78  ion. Take the ex
19fd5 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6e 6f 77  clusive lock now
19fd6 2c 20 62 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67  , before opening
19fd7 20 74 68 65 20 57 41 4c 0d 0a 20 20 2a 2a 20 66   the WAL..  ** f
19fd8 69 6c 65 2c 20 74 6f 20 6d 61 6b 65 20 73 75 72  ile, to make sur
19fd9 65 20 74 68 69 73 20 69 73 20 73 61 66 65 2e 0d  e this is safe..
19fda 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 50 61  .  */..  if( pPa
19fdb 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
19fdc 64 65 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  de ){..    rc = 
19fdd 70 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f  pagerExclusiveLo
19fde 63 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 7d  ck(pPager);..  }
19fdf 0d 0a 0d 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68  ....  /* Open th
19fe0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
19fe1 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 49 66  the log file. If
19fe2 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
19fe3 66 61 69 6c 73 2c 20 0d 0a 20 20 2a 2a 20 28 65  fails, ..  ** (e
19fe4 2e 67 2e 20 64 75 65 20 74 6f 20 6d 61 6c 6c 6f  .g. due to mallo
19fe5 63 28 29 20 66 61 69 6c 75 72 65 29 2c 20 72 65  c() failure), re
19fe6 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
19fe7 64 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  de...  */..  if(
19fe8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
19fe9 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  {..    rc = sqli
19fea 74 65 33 57 61 6c 4f 70 65 6e 28 70 50 61 67 65  te3WalOpen(pPage
19feb 72 2d 3e 70 56 66 73 2c 20 0d 0a 20 20 20 20 20  r->pVfs, ..     
19fec 20 20 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70     pPager->fd, p
19fed 50 61 67 65 72 2d 3e 7a 57 61 6c 2c 20 70 50 61  Pager->zWal, pPa
19fee 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
19fef 64 65 2c 0d 0a 20 20 20 20 20 20 20 20 70 50 61  de,..        pPa
19ff0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ger->journalSize
19ff1 4c 69 6d 69 74 2c 20 26 70 50 61 67 65 72 2d 3e  Limit, &pPager->
19ff2 70 57 61 6c 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  pWal..    );..  
19ff3 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  }....  return rc
19ff4 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ;..}....../*..**
19ff5 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
19ff6 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 53 48   be holding a SH
19ff7 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ARED lock on the
19ff8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
19ff9 6f 20 63 61 6c 6c 0d 0a 2a 2a 20 74 68 69 73 20  o call..** this 
19ffa 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  function...**..*
19ffb 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 70  * If the pager p
19ffc 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
19ffd 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  st argument is o
19ffe 70 65 6e 20 6f 6e 20 61 20 72 65 61 6c 20 64 61  pen on a real da
19fff 74 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 20  tabase..** file 
1a000 28 6e 6f 74 20 61 20 74 65 6d 70 20 66 69 6c 65  (not a temp file
1a001 20 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79   or an in-memory
1a002 20 64 61 74 61 62 61 73 65 29 2c 20 61 6e 64 20   database), and 
1a003 74 68 65 20 57 41 4c 20 66 69 6c 65 0d 0a 2a 2a  the WAL file..**
1a004 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
1a005 6f 70 65 6e 2c 20 6d 61 6b 65 20 61 6e 20 61 74  open, make an at
1a006 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 69 74  tempt to open it
1a007 20 6e 6f 77 2e 20 49 66 20 73 75 63 63 65 73 73   now. If success
1a008 66 75 6c 2c 0d 0a 2a 2a 20 72 65 74 75 72 6e 20  ful,..** return 
1a009 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e  SQLITE_OK. If an
1a00a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 6f 72   error occurs or
1a00b 20 74 68 65 20 56 46 53 20 75 73 65 64 20 62 79   the VFS used by
1a00c 20 74 68 65 20 70 61 67 65 72 20 64 6f 65 73 20   the pager does 
1a00d 0d 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74  ..** not support
1a00e 20 74 68 65 20 78 53 68 6d 58 58 58 28 29 20 6d   the xShmXXX() m
1a00f 65 74 68 6f 64 73 2c 20 72 65 74 75 72 6e 20 61  ethods, return a
1a010 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 70  n error code. *p
1a011 62 4f 70 65 6e 20 69 73 0d 0a 2a 2a 20 6e 6f 74  bOpen is..** not
1a012 20 6d 6f 64 69 66 69 65 64 20 69 6e 20 65 69 74   modified in eit
1a013 68 65 72 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a  her case...**..*
1a014 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
1a015 73 20 6f 70 65 6e 20 6f 6e 20 61 20 74 65 6d 70  s open on a temp
1a016 2d 66 69 6c 65 20 28 6f 72 20 69 6e 2d 6d 65 6d  -file (or in-mem
1a017 6f 72 79 20 64 61 74 61 62 61 73 65 29 2c 20 6f  ory database), o
1a018 72 20 69 66 0d 0a 2a 2a 20 74 68 65 20 57 41 4c  r if..** the WAL
1a019 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79   file is already
1a01a 20 6f 70 65 6e 2c 20 73 65 74 20 2a 70 62 4f 70   open, set *pbOp
1a01b 65 6e 20 74 6f 20 31 20 61 6e 64 20 72 65 74 75  en to 1 and retu
1a01c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 2a 2a  rn SQLITE_OK..**
1a01d 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61   without doing a
1a01e 6e 79 74 68 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53 51  nything...*/..SQ
1a01f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a020 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
1a021 6e 57 61 6c 28 0d 0a 20 20 50 61 67 65 72 20 2a  nWal(..  Pager *
1a022 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20  pPager,         
1a023 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1a024 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69  r object */..  i
1a025 6e 74 20 2a 70 62 4f 70 65 6e 20 20 20 20 20 20  nt *pbOpen      
1a026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a027 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72  * OUT: Set to tr
1a028 75 65 20 69 66 20 63 61 6c 6c 20 69 73 20 61 20  ue if call is a 
1a029 6e 6f 2d 6f 70 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  no-op */..){..  
1a02a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1a02b 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
1a02c 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
1a02d 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 61  /....  assert( a
1a02e 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
1a02f 65 28 70 50 61 67 65 72 29 20 29 3b 0d 0a 20 20  e(pPager) );..  
1a030 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1a031 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50  eState==PAGER_OP
1a032 45 4e 20 20 20 7c 7c 20 70 62 4f 70 65 6e 20 29  EN   || pbOpen )
1a033 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
1a034 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
1a035 45 52 5f 52 45 41 44 45 52 20 7c 7c 20 21 70 62  ER_READER || !pb
1a036 4f 70 65 6e 20 29 3b 0d 0a 20 20 61 73 73 65 72  Open );..  asser
1a037 74 28 20 70 62 4f 70 65 6e 3d 3d 30 20 7c 7c 20  t( pbOpen==0 || 
1a038 2a 70 62 4f 70 65 6e 3d 3d 30 20 29 3b 0d 0a 20  *pbOpen==0 );.. 
1a039 20 61 73 73 65 72 74 28 20 70 62 4f 70 65 6e 21   assert( pbOpen!
1a03a 3d 30 20 7c 7c 20 28 21 70 50 61 67 65 72 2d 3e  =0 || (!pPager->
1a03b 74 65 6d 70 46 69 6c 65 20 26 26 20 21 70 50 61  tempFile && !pPa
1a03c 67 65 72 2d 3e 70 57 61 6c 29 20 29 3b 0d 0a 0d  ger->pWal) );...
1a03d 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
1a03e 74 65 6d 70 46 69 6c 65 20 26 26 20 21 70 50 61  tempFile && !pPa
1a03f 67 65 72 2d 3e 70 57 61 6c 20 29 7b 0d 0a 20 20  ger->pWal ){..  
1a040 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 50 61    if( !sqlite3Pa
1a041 67 65 72 57 61 6c 53 75 70 70 6f 72 74 65 64 28  gerWalSupported(
1a042 70 50 61 67 65 72 29 20 29 20 72 65 74 75 72 6e  pPager) ) return
1a043 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
1a044 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6c 6f 73  ;....    /* Clos
1a045 65 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  e any rollback j
1a046 6f 75 72 6e 61 6c 20 70 72 65 76 69 6f 75 73 6c  ournal previousl
1a047 79 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20 20 20 73  y open */..    s
1a048 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1a049 61 67 65 72 2d 3e 6a 66 64 29 3b 0d 0a 0d 0a 20  ager->jfd);.... 
1a04a 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65     rc = pagerOpe
1a04b 6e 57 61 6c 28 70 50 61 67 65 72 29 3b 0d 0a 20  nWal(pPager);.. 
1a04c 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1a04d 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 70  E_OK ){..      p
1a04e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1a04f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e  de = PAGER_JOURN
1a050 41 4c 4d 4f 44 45 5f 57 41 4c 3b 0d 0a 20 20 20  ALMODE_WAL;..   
1a051 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74     pPager->eStat
1a052 65 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0d  e = PAGER_OPEN;.
1a053 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
1a054 0d 0a 20 20 20 20 2a 70 62 4f 70 65 6e 20 3d 20  ..    *pbOpen = 
1a055 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  1;..  }....  ret
1a056 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1a057 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
1a058 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
1a059 63 6c 6f 73 65 20 74 68 65 20 63 6f 6e 6e 65 63  close the connec
1a05a 74 69 6f 6e 20 74 6f 20 74 68 65 20 6c 6f 67 20  tion to the log 
1a05b 66 69 6c 65 20 70 72 69 6f 72 0d 0a 2a 2a 20 74  file prior..** t
1a05c 6f 20 73 77 69 74 63 68 69 6e 67 20 66 72 6f 6d  o switching from
1a05d 20 57 41 4c 20 74 6f 20 72 6f 6c 6c 62 61 63 6b   WAL to rollback
1a05e 20 6d 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 42   mode...**..** B
1a05f 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 20 74 68  efore closing th
1a060 65 20 6c 6f 67 20 66 69 6c 65 2c 20 74 68 69 73  e log file, this
1a061 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
1a062 74 73 20 74 6f 20 74 61 6b 65 20 61 6e 20 0d 0a  ts to take an ..
1a063 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ** EXCLUSIVE loc
1a064 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1a065 65 20 66 69 6c 65 2e 20 49 66 20 74 68 69 73 20  e file. If this 
1a066 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
1a067 65 64 2c 20 61 6e 0d 0a 2a 2a 20 65 72 72 6f 72  ed, an..** error
1a068 20 28 53 51 4c 49 54 45 5f 42 55 53 59 29 20 69   (SQLITE_BUSY) i
1a069 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
1a06a 68 65 20 6c 6f 67 20 63 6f 6e 6e 65 63 74 69 6f  he log connectio
1a06b 6e 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 2e  n is not closed.
1a06c 0d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  ..** If successf
1a06d 75 6c 2c 20 74 68 65 20 45 58 43 4c 55 53 49 56  ul, the EXCLUSIV
1a06e 45 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  E lock is not re
1a06f 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 72 65  leased before re
1a070 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53 51  turning...*/..SQ
1a071 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a072 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
1a073 73 65 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61  seWal(Pager *pPa
1a074 67 65 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20  ger){..  int rc 
1a075 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a  = SQLITE_OK;....
1a076 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1a077 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
1a078 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
1a079 5f 57 41 4c 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  _WAL );....  /* 
1a07a 49 66 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20  If the log file 
1a07b 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
1a07c 70 65 6e 2c 20 62 75 74 20 64 6f 65 73 20 65 78  pen, but does ex
1a07d 69 73 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d  ist in the file-
1a07e 73 79 73 74 65 6d 2c 0d 0a 20 20 2a 2a 20 69 74  system,..  ** it
1a07f 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20   may need to be 
1a080 63 68 65 63 6b 70 6f 69 6e 74 65 64 20 62 65 66  checkpointed bef
1a081 6f 72 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  ore the connecti
1a082 6f 6e 20 63 61 6e 20 73 77 69 74 63 68 20 74 6f  on can switch to
1a083 0d 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20  ..  ** rollback 
1a084 6d 6f 64 65 2e 20 4f 70 65 6e 20 69 74 20 6e 6f  mode. Open it no
1a085 77 20 73 6f 20 74 68 69 73 20 63 61 6e 20 68 61  w so this can ha
1a086 70 70 65 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ppen...  */..  i
1a087 66 28 20 21 70 50 61 67 65 72 2d 3e 70 57 61 6c  f( !pPager->pWal
1a088 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6c 6f 67   ){..    int log
1a089 65 78 69 73 74 73 20 3d 20 30 3b 0d 0a 20 20 20  exists = 0;..   
1a08a 20 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44   rc = pagerLockD
1a08b 62 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44  b(pPager, SHARED
1a08c 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 69 66 28  _LOCK);..    if(
1a08d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1a08e 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
1a08f 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0d 0a  lite3OsAccess(..
1a090 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
1a091 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  ->pVfs, pPager->
1a092 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  zWal, SQLITE_ACC
1a093 45 53 53 5f 45 58 49 53 54 53 2c 20 26 6c 6f 67  ESS_EXISTS, &log
1a094 65 78 69 73 74 73 0d 0a 20 20 20 20 20 20 29 3b  exists..      );
1a095 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
1a096 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1a097 26 20 6c 6f 67 65 78 69 73 74 73 20 29 7b 0d 0a  & logexists ){..
1a098 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
1a099 4f 70 65 6e 57 61 6c 28 70 50 61 67 65 72 29 3b  OpenWal(pPager);
1a09a 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
1a09b 20 20 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 70 6f    ..  /* Checkpo
1a09c 69 6e 74 20 61 6e 64 20 63 6c 6f 73 65 20 74 68  int and close th
1a09d 65 20 6c 6f 67 2e 20 42 65 63 61 75 73 65 20 61  e log. Because a
1a09e 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
1a09f 20 69 73 20 68 65 6c 64 20 6f 6e 0d 0a 20 20 2a   is held on..  *
1a0a0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
1a0a1 69 6c 65 2c 20 74 68 65 20 6c 6f 67 20 61 6e 64  ile, the log and
1a0a2 20 6c 6f 67 2d 73 75 6d 6d 61 72 79 20 66 69 6c   log-summary fil
1a0a3 65 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74  es will be delet
1a0a4 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ed...  */..  if(
1a0a5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1a0a6 26 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 29  & pPager->pWal )
1a0a7 7b 0d 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  {..    rc = page
1a0a8 72 45 78 63 6c 75 73 69 76 65 4c 6f 63 6b 28 70  rExclusiveLock(p
1a0a9 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 69 66 28  Pager);..    if(
1a0aa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1a0ab 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
1a0ac 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 70 50  lite3WalClose(pP
1a0ad 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 50 61 67  ager->pWal, pPag
1a0ae 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67  er->ckptSyncFlag
1a0af 73 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  s,..            
1a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1a0b1 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
1a0b2 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54   (u8*)pPager->pT
1a0b3 6d 70 53 70 61 63 65 29 3b 0d 0a 20 20 20 20 20  mpSpace);..     
1a0b4 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20   pPager->pWal = 
1a0b5 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  0;..    }..  }..
1a0b6 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
1a0b7 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
1a0b8 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 2f 2a 0d 0a  _HAS_CODEC../*..
1a0b9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1a0ba 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1a0bb 65 20 77 61 6c 20 6d 6f 64 75 6c 65 20 77 68 65  e wal module whe
1a0bc 6e 20 77 72 69 74 69 6e 67 20 70 61 67 65 20 63  n writing page c
1a0bd 6f 6e 74 65 6e 74 0d 0a 2a 2a 20 69 6e 74 6f 20  ontent..** into 
1a0be 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 0d 0a 2a  the log file...*
1a0bf 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
1a0c0 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ion returns a po
1a0c1 69 6e 74 65 72 20 74 6f 20 61 20 62 75 66 66 65  inter to a buffe
1a0c2 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
1a0c3 20 65 6e 63 72 79 70 74 65 64 0d 0a 2a 2a 20 70   encrypted..** p
1a0c4 61 67 65 20 63 6f 6e 74 65 6e 74 2e 20 49 66 20  age content. If 
1a0c5 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  a malloc fails, 
1a0c6 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  this function ma
1a0c7 79 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0d 0a  y return NULL...
1a0c8 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1a0c9 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
1a0ca 50 61 67 65 72 43 6f 64 65 63 28 50 67 48 64 72  PagerCodec(PgHdr
1a0cb 20 2a 70 50 67 29 7b 0d 0a 20 20 76 6f 69 64 20   *pPg){..  void 
1a0cc 2a 61 44 61 74 61 20 3d 20 30 3b 0d 0a 20 20 43  *aData = 0;..  C
1a0cd 4f 44 45 43 32 28 70 50 67 2d 3e 70 50 61 67 65  ODEC2(pPg->pPage
1a0ce 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70  r, pPg->pData, p
1a0cf 50 67 2d 3e 70 67 6e 6f 2c 20 36 2c 20 72 65 74  Pg->pgno, 6, ret
1a0d0 75 72 6e 20 30 2c 20 61 44 61 74 61 29 3b 0d 0a  urn 0, aData);..
1a0d1 20 20 72 65 74 75 72 6e 20 61 44 61 74 61 3b 0d    return aData;.
1a0d2 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
1a0d3 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a  LITE_HAS_CODEC *
1a0d4 2f 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  /....#endif /* !
1a0d5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 20  SQLITE_OMIT_WAL 
1a0d6 2a 2f 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  */....#endif /* 
1a0d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
1a0d8 49 4f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  IO */..../******
1a0d9 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1a0da 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  pager.c ********
1a0db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0dd 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
1a0de 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1a0df 66 69 6c 65 20 77 61 6c 2e 63 20 2a 2a 2a 2a 2a  file wal.c *****
1a0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0e2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
1a0e3 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 79 20  * 2010 February 
1a0e4 31 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  1..**..** The au
1a0e5 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1a0e6 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1a0e7 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1a0e8 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
1a0e9 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1a0ea 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1a0eb 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
1a0ec 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1a0ed 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
1a0ee 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1a0ef 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1a0f0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1a0f1 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
1a0f2 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1a0f3 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1a0f4 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1a0f5 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
1a0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  *********..**..*
1a0fb 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1a0fc 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
1a0fd 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 77 72 69  ntation of a wri
1a0fe 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 28 57 41  te-ahead log (WA
1a0ff 4c 29 20 75 73 65 64 20 69 6e 20 0d 0a 2a 2a 20  L) used in ..** 
1a100 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41  "journal_mode=WA
1a101 4c 22 20 6d 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  L" mode...**..**
1a102 20 57 52 49 54 45 2d 41 48 45 41 44 20 4c 4f 47   WRITE-AHEAD LOG
1a103 20 28 57 41 4c 29 20 46 49 4c 45 20 46 4f 52 4d   (WAL) FILE FORM
1a104 41 54 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 57 41 4c  AT..**..** A WAL
1a105 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f   file consists o
1a106 66 20 61 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f  f a header follo
1a107 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 20 6d  wed by zero or m
1a108 6f 72 65 20 22 66 72 61 6d 65 73 22 2e 0d 0a 2a  ore "frames"...*
1a109 2a 20 45 61 63 68 20 66 72 61 6d 65 20 72 65 63  * Each frame rec
1a10a 6f 72 64 73 20 74 68 65 20 72 65 76 69 73 65 64  ords the revised
1a10b 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 73 69   content of a si
1a10c 6e 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 74  ngle page from t
1a10d 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  he..** database 
1a10e 66 69 6c 65 2e 20 20 41 6c 6c 20 63 68 61 6e 67  file.  All chang
1a10f 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
1a110 73 65 20 61 72 65 20 72 65 63 6f 72 64 65 64 20  se are recorded 
1a111 62 79 20 77 72 69 74 69 6e 67 0d 0a 2a 2a 20 66  by writing..** f
1a112 72 61 6d 65 73 20 69 6e 74 6f 20 74 68 65 20 57  rames into the W
1a113 41 4c 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e  AL.  Transaction
1a114 73 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20  s commit when a 
1a115 66 72 61 6d 65 20 69 73 20 77 72 69 74 74 65 6e  frame is written
1a116 20 74 68 61 74 0d 0a 2a 2a 20 63 6f 6e 74 61 69   that..** contai
1a117 6e 73 20 61 20 63 6f 6d 6d 69 74 20 6d 61 72 6b  ns a commit mark
1a118 65 72 2e 20 20 41 20 73 69 6e 67 6c 65 20 57 41  er.  A single WA
1a119 4c 20 63 61 6e 20 61 6e 64 20 75 73 75 61 6c 6c  L can and usuall
1a11a 79 20 64 6f 65 73 20 72 65 63 6f 72 64 20 0d 0a  y does record ..
1a11b 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 72 61 6e  ** multiple tran
1a11c 73 61 63 74 69 6f 6e 73 2e 20 20 50 65 72 69 6f  sactions.  Perio
1a11d 64 69 63 61 6c 6c 79 2c 20 74 68 65 20 63 6f 6e  dically, the con
1a11e 74 65 6e 74 20 6f 66 20 74 68 65 20 57 41 4c 20  tent of the WAL 
1a11f 69 73 0d 0a 2a 2a 20 74 72 61 6e 73 66 65 72 72  is..** transferr
1a120 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  ed back into the
1a121 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1a122 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63  n an operation c
1a123 61 6c 6c 65 64 20 61 0d 0a 2a 2a 20 22 63 68 65  alled a..** "che
1a124 63 6b 70 6f 69 6e 74 22 2e 0d 0a 2a 2a 0d 0a 2a  ckpoint"...**..*
1a125 2a 20 41 20 73 69 6e 67 6c 65 20 57 41 4c 20 66  * A single WAL f
1a126 69 6c 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ile can be used 
1a127 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20  multiple times. 
1a128 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1a129 20 74 68 65 0d 0a 2a 2a 20 57 41 4c 20 63 61 6e   the..** WAL can
1a12a 20 66 69 6c 6c 20 75 70 20 77 69 74 68 20 66 72   fill up with fr
1a12b 61 6d 65 73 20 61 6e 64 20 74 68 65 6e 20 62 65  ames and then be
1a12c 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 20 61 6e   checkpointed an
1a12d 64 20 74 68 65 6e 20 6e 65 77 0d 0a 2a 2a 20 66  d then new..** f
1a12e 72 61 6d 65 73 20 63 61 6e 20 6f 76 65 72 77 72  rames can overwr
1a12f 69 74 65 20 74 68 65 20 6f 6c 64 20 6f 6e 65 73  ite the old ones
1a130 2e 20 20 41 20 57 41 4c 20 61 6c 77 61 79 73 20  .  A WAL always 
1a131 67 72 6f 77 73 20 66 72 6f 6d 20 62 65 67 69 6e  grows from begin
1a132 6e 69 6e 67 0d 0a 2a 2a 20 74 6f 77 61 72 64 20  ning..** toward 
1a133 74 68 65 20 65 6e 64 2e 20 20 43 68 65 63 6b 73  the end.  Checks
1a134 75 6d 73 20 61 6e 64 20 63 6f 75 6e 74 65 72 73  ums and counters
1a135 20 61 74 74 61 63 68 65 64 20 74 6f 20 65 61 63   attached to eac
1a136 68 20 66 72 61 6d 65 20 61 72 65 0d 0a 2a 2a 20  h frame are..** 
1a137 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1a138 65 20 77 68 69 63 68 20 66 72 61 6d 65 73 20 77  e which frames w
1a139 69 74 68 69 6e 20 74 68 65 20 57 41 4c 20 61 72  ithin the WAL ar
1a13a 65 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69 63  e valid and whic
1a13b 68 0d 0a 2a 2a 20 61 72 65 20 6c 65 66 74 6f 76  h..** are leftov
1a13c 65 72 73 20 66 72 6f 6d 20 70 72 69 6f 72 20 63  ers from prior c
1a13d 68 65 63 6b 70 6f 69 6e 74 73 2e 0d 0a 2a 2a 0d  heckpoints...**.
1a13e 0a 2a 2a 20 54 68 65 20 57 41 4c 20 68 65 61 64  .** The WAL head
1a13f 65 72 20 69 73 20 33 32 20 62 79 74 65 73 20 69  er is 32 bytes i
1a140 6e 20 73 69 7a 65 20 61 6e 64 20 63 6f 6e 73 69  n size and consi
1a141 73 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  sts of the follo
1a142 77 69 6e 67 20 65 69 67 68 74 0d 0a 2a 2a 20 62  wing eight..** b
1a143 69 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69 74  ig-endian 32-bit
1a144 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
1a145 72 20 76 61 6c 75 65 73 3a 0d 0a 2a 2a 0d 0a 2a  r values:..**..*
1a146 2a 20 20 20 20 20 30 3a 20 4d 61 67 69 63 20 6e  *     0: Magic n
1a147 75 6d 62 65 72 2e 20 20 30 78 33 37 37 66 30 36  umber.  0x377f06
1a148 38 32 20 6f 72 20 30 78 33 37 37 66 30 36 38 33  82 or 0x377f0683
1a149 0d 0a 2a 2a 20 20 20 20 20 34 3a 20 46 69 6c 65  ..**     4: File
1a14a 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 2e   format version.
1a14b 20 20 43 75 72 72 65 6e 74 6c 79 20 33 30 30 37    Currently 3007
1a14c 30 30 30 0d 0a 2a 2a 20 20 20 20 20 38 3a 20 44  000..**     8: D
1a14d 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
1a14e 65 2e 20 20 45 78 61 6d 70 6c 65 3a 20 31 30 32  e.  Example: 102
1a14f 34 0d 0a 2a 2a 20 20 20 20 31 32 3a 20 43 68 65  4..**    12: Che
1a150 63 6b 70 6f 69 6e 74 20 73 65 71 75 65 6e 63 65  ckpoint sequence
1a151 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 20 20 20 31   number..**    1
1a152 36 3a 20 53 61 6c 74 2d 31 2c 20 72 61 6e 64 6f  6: Salt-1, rando
1a153 6d 20 69 6e 74 65 67 65 72 20 69 6e 63 72 65 6d  m integer increm
1a154 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20  ented with each 
1a155 63 68 65 63 6b 70 6f 69 6e 74 0d 0a 2a 2a 20 20  checkpoint..**  
1a156 20 20 32 30 3a 20 53 61 6c 74 2d 32 2c 20 61 20    20: Salt-2, a 
1a157 64 69 66 66 65 72 65 6e 74 20 72 61 6e 64 6f 6d  different random
1a158 20 69 6e 74 65 67 65 72 20 63 68 61 6e 67 69 6e   integer changin
1a159 67 20 77 69 74 68 20 65 61 63 68 20 63 6b 70 74  g with each ckpt
1a15a 0d 0a 2a 2a 20 20 20 20 32 34 3a 20 43 68 65 63  ..**    24: Chec
1a15b 6b 73 75 6d 2d 31 20 28 66 69 72 73 74 20 70 61  ksum-1 (first pa
1a15c 72 74 20 6f 66 20 63 68 65 63 6b 73 75 6d 20 66  rt of checksum f
1a15d 6f 72 20 66 69 72 73 74 20 32 34 20 62 79 74 65  or first 24 byte
1a15e 73 20 6f 66 20 68 65 61 64 65 72 29 2e 0d 0a 2a  s of header)...*
1a15f 2a 20 20 20 20 32 38 3a 20 43 68 65 63 6b 73 75  *    28: Checksu
1a160 6d 2d 32 20 28 73 65 63 6f 6e 64 20 70 61 72 74  m-2 (second part
1a161 20 6f 66 20 63 68 65 63 6b 73 75 6d 20 66 6f 72   of checksum for
1a162 20 66 69 72 73 74 20 32 34 20 62 79 74 65 73 20   first 24 bytes 
1a163 6f 66 20 68 65 61 64 65 72 29 2e 0d 0a 2a 2a 0d  of header)...**.
1a164 0a 2a 2a 20 49 6d 6d 65 64 69 61 74 65 6c 79 20  .** Immediately 
1a165 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 77 61  following the wa
1a166 6c 2d 68 65 61 64 65 72 20 61 72 65 20 7a 65 72  l-header are zer
1a167 6f 20 6f 72 20 6d 6f 72 65 20 66 72 61 6d 65 73  o or more frames
1a168 2e 20 45 61 63 68 0d 0a 2a 2a 20 66 72 61 6d 65  . Each..** frame
1a169 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32   consists of a 2
1a16a 34 2d 62 79 74 65 20 66 72 61 6d 65 2d 68 65 61  4-byte frame-hea
1a16b 64 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  der followed by 
1a16c 61 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79  a <page-size> by
1a16d 74 65 73 0d 0a 2a 2a 20 6f 66 20 70 61 67 65 20  tes..** of page 
1a16e 64 61 74 61 2e 20 54 68 65 20 66 72 61 6d 65 2d  data. The frame-
1a16f 68 65 61 64 65 72 20 69 73 20 73 69 78 20 62 69  header is six bi
1a170 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69 74 20  g-endian 32-bit 
1a171 75 6e 73 69 67 6e 65 64 20 0d 0a 2a 2a 20 69 6e  unsigned ..** in
1a172 74 65 67 65 72 20 76 61 6c 75 65 73 2c 20 61 73  teger values, as
1a173 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a   follows:..**..*
1a174 2a 20 20 20 20 20 30 3a 20 50 61 67 65 20 6e 75  *     0: Page nu
1a175 6d 62 65 72 2e 0d 0a 2a 2a 20 20 20 20 20 34 3a  mber...**     4:
1a176 20 46 6f 72 20 63 6f 6d 6d 69 74 20 72 65 63 6f   For commit reco
1a177 72 64 73 2c 20 74 68 65 20 73 69 7a 65 20 6f 66  rds, the size of
1a178 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
1a179 61 67 65 20 69 6e 20 70 61 67 65 73 20 0d 0a 2a  age in pages ..*
1a17a 2a 20 20 20 20 20 20 20 20 61 66 74 65 72 20 74  *        after t
1a17b 68 65 20 63 6f 6d 6d 69 74 2e 20 46 6f 72 20 61  he commit. For a
1a17c 6c 6c 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73  ll other records
1a17d 2c 20 7a 65 72 6f 2e 0d 0a 2a 2a 20 20 20 20 20  , zero...**     
1a17e 38 3a 20 53 61 6c 74 2d 31 20 28 63 6f 70 69 65  8: Salt-1 (copie
1a17f 64 20 66 72 6f 6d 20 74 68 65 20 68 65 61 64 65  d from the heade
1a180 72 29 0d 0a 2a 2a 20 20 20 20 31 32 3a 20 53 61  r)..**    12: Sa
1a181 6c 74 2d 32 20 28 63 6f 70 69 65 64 20 66 72 6f  lt-2 (copied fro
1a182 6d 20 74 68 65 20 68 65 61 64 65 72 29 0d 0a 2a  m the header)..*
1a183 2a 20 20 20 20 31 36 3a 20 43 68 65 63 6b 73 75  *    16: Checksu
1a184 6d 2d 31 2e 0d 0a 2a 2a 20 20 20 20 32 30 3a 20  m-1...**    20: 
1a185 43 68 65 63 6b 73 75 6d 2d 32 2e 0d 0a 2a 2a 0d  Checksum-2...**.
1a186 0a 2a 2a 20 41 20 66 72 61 6d 65 20 69 73 20 63  .** A frame is c
1a187 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20  onsidered valid 
1a188 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74  if and only if t
1a189 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
1a18a 64 69 74 69 6f 6e 73 20 61 72 65 0d 0a 2a 2a 20  ditions are..** 
1a18b 74 72 75 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  true:..**..**   
1a18c 20 28 31 29 20 54 68 65 20 73 61 6c 74 2d 31 20   (1) The salt-1 
1a18d 61 6e 64 20 73 61 6c 74 2d 32 20 76 61 6c 75 65  and salt-2 value
1a18e 73 20 69 6e 20 74 68 65 20 66 72 61 6d 65 2d 68  s in the frame-h
1a18f 65 61 64 65 72 20 6d 61 74 63 68 0d 0a 2a 2a 20  eader match..** 
1a190 20 20 20 20 20 20 20 73 61 6c 74 20 76 61 6c 75         salt valu
1a191 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d 68 65  es in the wal-he
1a192 61 64 65 72 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ader..**..**    
1a193 28 32 29 20 54 68 65 20 63 68 65 63 6b 73 75 6d  (2) The checksum
1a194 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66   values in the f
1a195 69 6e 61 6c 20 38 20 62 79 74 65 73 20 6f 66 20  inal 8 bytes of 
1a196 74 68 65 20 66 72 61 6d 65 2d 68 65 61 64 65 72  the frame-header
1a197 0d 0a 2a 2a 20 20 20 20 20 20 20 20 65 78 61 63  ..**        exac
1a198 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 63 68  tly match the ch
1a199 65 63 6b 73 75 6d 20 63 6f 6d 70 75 74 65 64 20  ecksum computed 
1a19a 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 20 6f 6e  consecutively on
1a19b 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20 20 20 20   the..**        
1a19c 57 41 4c 20 68 65 61 64 65 72 20 61 6e 64 20 74  WAL header and t
1a19d 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73  he first 8 bytes
1a19e 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74   and the content
1a19f 20 6f 66 20 61 6c 6c 20 66 72 61 6d 65 73 0d 0a   of all frames..
1a1a0 2a 2a 20 20 20 20 20 20 20 20 75 70 20 74 6f 20  **        up to 
1a1a1 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  and including th
1a1a2 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 2e  e current frame.
1a1a3 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 68 65  ..**..** The che
1a1a4 63 6b 73 75 6d 20 69 73 20 63 6f 6d 70 75 74 65  cksum is compute
1a1a5 64 20 75 73 69 6e 67 20 33 32 2d 62 69 74 20 62  d using 32-bit b
1a1a6 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
1a1a7 72 73 20 69 66 20 74 68 65 0d 0a 2a 2a 20 6d 61  rs if the..** ma
1a1a8 67 69 63 20 6e 75 6d 62 65 72 20 69 6e 20 74 68  gic number in th
1a1a9 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20  e first 4 bytes 
1a1aa 6f 66 20 74 68 65 20 57 41 4c 20 69 73 20 30 78  of the WAL is 0x
1a1ab 33 37 37 66 30 36 38 33 20 61 6e 64 20 69 74 0d  377f0683 and it.
1a1ac 0a 2a 2a 20 69 73 20 63 6f 6d 70 75 74 65 64 20  .** is computed 
1a1ad 75 73 69 6e 67 20 6c 69 74 74 6c 65 2d 65 6e 64  using little-end
1a1ae 69 61 6e 20 69 66 20 74 68 65 20 6d 61 67 69 63  ian if the magic
1a1af 20 6e 75 6d 62 65 72 20 69 73 20 30 78 33 37 37   number is 0x377
1a1b0 66 30 36 38 32 2e 0d 0a 2a 2a 20 54 68 65 20 63  f0682...** The c
1a1b1 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 73 20 61  hecksum values a
1a1b2 72 65 20 61 6c 77 61 79 73 20 73 74 6f 72 65 64  re always stored
1a1b3 20 69 6e 20 74 68 65 20 66 72 61 6d 65 20 68 65   in the frame he
1a1b4 61 64 65 72 20 69 6e 20 61 0d 0a 2a 2a 20 62 69  ader in a..** bi
1a1b5 67 2d 65 6e 64 69 61 6e 20 66 6f 72 6d 61 74 20  g-endian format 
1a1b6 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
1a1b7 69 63 68 20 62 79 74 65 20 6f 72 64 65 72 20 69  ich byte order i
1a1b8 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  s used to comput
1a1b9 65 0d 0a 2a 2a 20 74 68 65 20 63 68 65 63 6b 73  e..** the checks
1a1ba 75 6d 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75  um.  The checksu
1a1bb 6d 20 69 73 20 63 6f 6d 70 75 74 65 64 20 62 79  m is computed by
1a1bc 20 69 6e 74 65 72 70 72 65 74 69 6e 67 20 74 68   interpreting th
1a1bd 65 20 69 6e 70 75 74 20 61 73 0d 0a 2a 2a 20 61  e input as..** a
1a1be 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 20 6f 66  n even number of
1a1bf 20 75 6e 73 69 67 6e 65 64 20 33 32 2d 62 69 74   unsigned 32-bit
1a1c0 20 69 6e 74 65 67 65 72 73 3a 20 78 5b 30 5d 20   integers: x[0] 
1a1c1 74 68 72 6f 75 67 68 20 78 5b 4e 5d 2e 20 20 54  through x[N].  T
1a1c2 68 65 0d 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d  he..** algorithm
1a1c3 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 68   used for the ch
1a1c4 65 63 6b 73 75 6d 20 69 73 20 61 73 20 66 6f 6c  ecksum is as fol
1a1c5 6c 6f 77 73 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20  lows:..** ..**  
1a1c6 20 66 6f 72 20 69 20 66 72 6f 6d 20 30 20 74 6f   for i from 0 to
1a1c7 20 6e 2d 31 20 73 74 65 70 20 32 3a 0d 0a 2a 2a   n-1 step 2:..**
1a1c8 20 20 20 20 20 73 30 20 2b 3d 20 78 5b 69 5d 20       s0 += x[i] 
1a1c9 2b 20 73 31 3b 0d 0a 2a 2a 20 20 20 20 20 73 31  + s1;..**     s1
1a1ca 20 2b 3d 20 78 5b 69 2b 31 5d 20 2b 20 73 30 3b   += x[i+1] + s0;
1a1cb 0d 0a 2a 2a 20 20 20 65 6e 64 66 6f 72 0d 0a 2a  ..**   endfor..*
1a1cc 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  *..** Note that 
1a1cd 73 30 20 61 6e 64 20 73 31 20 61 72 65 20 62 6f  s0 and s1 are bo
1a1ce 74 68 20 77 65 69 67 68 74 65 64 20 63 68 65 63  th weighted chec
1a1cf 6b 73 75 6d 73 20 75 73 69 6e 67 20 66 69 62 6f  ksums using fibo
1a1d0 6e 61 63 63 69 20 77 65 69 67 68 74 73 0d 0a 2a  nacci weights..*
1a1d1 2a 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  * in reverse ord
1a1d2 65 72 20 28 74 68 65 20 6c 61 72 67 65 73 74 20  er (the largest 
1a1d3 66 69 62 6f 6e 61 63 63 69 20 77 65 69 67 68 74  fibonacci weight
1a1d4 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 66   occurs on the f
1a1d5 69 72 73 74 20 65 6c 65 6d 65 6e 74 0d 0a 2a 2a  irst element..**
1a1d6 20 6f 66 20 74 68 65 20 73 65 71 75 65 6e 63 65   of the sequence
1a1d7 20 62 65 69 6e 67 20 73 75 6d 6d 65 64 2e 29 20   being summed.) 
1a1d8 20 54 68 65 20 73 31 20 76 61 6c 75 65 20 73 70   The s1 value sp
1a1d9 61 6e 73 20 61 6c 6c 20 33 32 2d 62 69 74 20 0d  ans all 32-bit .
1a1da 0a 2a 2a 20 74 65 72 6d 73 20 6f 66 20 74 68 65  .** terms of the
1a1db 20 73 65 71 75 65 6e 63 65 20 77 68 65 72 65 61   sequence wherea
1a1dc 73 20 73 30 20 6f 6d 69 74 73 20 74 68 65 20 66  s s0 omits the f
1a1dd 69 6e 61 6c 20 74 65 72 6d 2e 0d 0a 2a 2a 0d 0a  inal term...**..
1a1de 2a 2a 20 4f 6e 20 61 20 63 68 65 63 6b 70 6f 69  ** On a checkpoi
1a1df 6e 74 2c 20 74 68 65 20 57 41 4c 20 69 73 20 66  nt, the WAL is f
1a1e0 69 72 73 74 20 56 46 53 2e 78 53 79 6e 63 2d 65  irst VFS.xSync-e
1a1e1 64 2c 20 74 68 65 6e 20 76 61 6c 69 64 20 63 6f  d, then valid co
1a1e2 6e 74 65 6e 74 20 6f 66 20 74 68 65 0d 0a 2a 2a  ntent of the..**
1a1e3 20 57 41 4c 20 69 73 20 74 72 61 6e 73 66 65 72   WAL is transfer
1a1e4 72 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61 74  red into the dat
1a1e5 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20  abase, then the 
1a1e6 64 61 74 61 62 61 73 65 20 69 73 20 56 46 53 2e  database is VFS.
1a1e7 78 53 79 6e 63 2d 65 64 2e 0d 0a 2a 2a 20 54 68  xSync-ed...** Th
1a1e8 65 20 56 46 53 2e 78 53 79 6e 63 20 6f 70 65 72  e VFS.xSync oper
1a1e9 61 74 69 6f 6e 73 20 73 65 72 76 65 20 61 73 20  ations serve as 
1a1ea 77 72 69 74 65 20 62 61 72 72 69 65 72 73 20 2d  write barriers -
1a1eb 20 61 6c 6c 20 77 72 69 74 65 73 20 6c 61 75 6e   all writes laun
1a1ec 63 68 65 64 0d 0a 2a 2a 20 62 65 66 6f 72 65 20  ched..** before 
1a1ed 74 68 65 20 78 53 79 6e 63 20 6d 75 73 74 20 63  the xSync must c
1a1ee 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 65 20 61  omplete before a
1a1ef 6e 79 20 77 72 69 74 65 20 74 68 61 74 20 6c 61  ny write that la
1a1f0 75 6e 63 68 65 73 20 61 66 74 65 72 20 74 68 65  unches after the
1a1f1 0d 0a 2a 2a 20 78 53 79 6e 63 20 62 65 67 69 6e  ..** xSync begin
1a1f2 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72  s...**..** After
1a1f3 20 65 61 63 68 20 63 68 65 63 6b 70 6f 69 6e 74   each checkpoint
1a1f4 2c 20 74 68 65 20 73 61 6c 74 2d 31 20 76 61 6c  , the salt-1 val
1a1f5 75 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ue is incremente
1a1f6 64 20 61 6e 64 20 74 68 65 20 73 61 6c 74 2d 32  d and the salt-2
1a1f7 0d 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 72 61  ..** value is ra
1a1f8 6e 64 6f 6d 69 7a 65 64 2e 20 20 54 68 69 73 20  ndomized.  This 
1a1f9 70 72 65 76 65 6e 74 73 20 6f 6c 64 20 61 6e 64  prevents old and
1a1fa 20 6e 65 77 20 66 72 61 6d 65 73 20 69 6e 20 74   new frames in t
1a1fb 68 65 20 57 41 4c 20 66 72 6f 6d 0d 0a 2a 2a 20  he WAL from..** 
1a1fc 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64  being considered
1a1fd 20 76 61 6c 69 64 20 61 74 20 74 68 65 20 73 61   valid at the sa
1a1fe 6d 65 20 74 69 6d 65 20 61 6e 64 20 62 65 69 6e  me time and bein
1a1ff 67 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20  g checkpointing 
1a200 74 6f 67 65 74 68 65 72 0d 0a 2a 2a 20 66 6f 6c  together..** fol
1a201 6c 6f 77 69 6e 67 20 61 20 63 72 61 73 68 2e 0d  lowing a crash..
1a202 0a 2a 2a 0d 0a 2a 2a 20 52 45 41 44 45 52 20 41  .**..** READER A
1a203 4c 47 4f 52 49 54 48 4d 0d 0a 2a 2a 0d 0a 2a 2a  LGORITHM..**..**
1a204 20 54 6f 20 72 65 61 64 20 61 20 70 61 67 65 20   To read a page 
1a205 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1a206 65 20 28 63 61 6c 6c 20 69 74 20 70 61 67 65 20  e (call it page 
1a207 6e 75 6d 62 65 72 20 50 29 2c 20 61 20 72 65 61  number P), a rea
1a208 64 65 72 0d 0a 2a 2a 20 66 69 72 73 74 20 63 68  der..** first ch
1a209 65 63 6b 73 20 74 68 65 20 57 41 4c 20 74 6f 20  ecks the WAL to 
1a20a 73 65 65 20 69 66 20 69 74 20 63 6f 6e 74 61 69  see if it contai
1a20b 6e 73 20 70 61 67 65 20 50 2e 20 20 49 66 20 73  ns page P.  If s
1a20c 6f 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20  o, then the..** 
1a20d 6c 61 73 74 20 76 61 6c 69 64 20 69 6e 73 74 61  last valid insta
1a20e 6e 63 65 20 6f 66 20 70 61 67 65 20 50 20 74 68  nce of page P th
1a20f 61 74 20 69 73 20 61 20 66 6f 6c 6c 6f 77 65 64  at is a followed
1a210 20 62 79 20 61 20 63 6f 6d 6d 69 74 20 66 72 61   by a commit fra
1a211 6d 65 0d 0a 2a 2a 20 6f 72 20 69 73 20 61 20 63  me..** or is a c
1a212 6f 6d 6d 69 74 20 66 72 61 6d 65 20 69 74 73 65  ommit frame itse
1a213 6c 66 20 62 65 63 6f 6d 65 73 20 74 68 65 20 76  lf becomes the v
1a214 61 6c 75 65 20 72 65 61 64 2e 20 20 49 66 20 74  alue read.  If t
1a215 68 65 20 57 41 4c 0d 0a 2a 2a 20 63 6f 6e 74 61  he WAL..** conta
1a216 69 6e 73 20 6e 6f 20 63 6f 70 69 65 73 20 6f 66  ins no copies of
1a217 20 70 61 67 65 20 50 20 74 68 61 74 20 61 72 65   page P that are
1a218 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68   valid and which
1a219 20 61 72 65 20 61 20 63 6f 6d 6d 69 74 0d 0a 2a   are a commit..*
1a21a 2a 20 66 72 61 6d 65 20 6f 72 20 61 72 65 20 66  * frame or are f
1a21b 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 6f 6d  ollowed by a com
1a21c 6d 69 74 20 66 72 61 6d 65 2c 20 74 68 65 6e 20  mit frame, then 
1a21d 70 61 67 65 20 50 20 69 73 20 72 65 61 64 20 66  page P is read f
1a21e 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61  rom..** the data
1a21f 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a  base file...**..
1a220 2a 2a 20 54 6f 20 73 74 61 72 74 20 61 20 72 65  ** To start a re
1a221 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ad transaction, 
1a222 74 68 65 20 72 65 61 64 65 72 20 72 65 63 6f 72  the reader recor
1a223 64 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  ds the index of 
1a224 74 68 65 20 6c 61 73 74 0d 0a 2a 2a 20 76 61 6c  the last..** val
1a225 69 64 20 66 72 61 6d 65 20 69 6e 20 74 68 65 20  id frame in the 
1a226 57 41 4c 2e 20 20 54 68 65 20 72 65 61 64 65 72  WAL.  The reader
1a227 20 75 73 65 73 20 74 68 69 73 20 72 65 63 6f 72   uses this recor
1a228 64 65 64 20 22 6d 78 46 72 61 6d 65 22 20 76 61  ded "mxFrame" va
1a229 6c 75 65 0d 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20  lue..** for all 
1a22a 73 75 62 73 65 71 75 65 6e 74 20 72 65 61 64 20  subsequent read 
1a22b 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 4e 65 77  operations.  New
1a22c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 61   transactions ca
1a22d 6e 20 62 65 20 61 70 70 65 6e 64 65 64 0d 0a 2a  n be appended..*
1a22e 2a 20 74 6f 20 74 68 65 20 57 41 4c 2c 20 62 75  * to the WAL, bu
1a22f 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  t as long as the
1a230 20 72 65 61 64 65 72 20 75 73 65 73 20 69 74 73   reader uses its
1a231 20 6f 72 69 67 69 6e 61 6c 20 6d 78 46 72 61 6d   original mxFram
1a232 65 20 76 61 6c 75 65 0d 0a 2a 2a 20 61 6e 64 20  e value..** and 
1a233 69 67 6e 6f 72 65 73 20 74 68 65 20 6e 65 77 6c  ignores the newl
1a234 79 20 61 70 70 65 6e 64 65 64 20 63 6f 6e 74 65  y appended conte
1a235 6e 74 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20  nt, it will see 
1a236 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 73 6e 61  a consistent sna
1a237 70 73 68 6f 74 0d 0a 2a 2a 20 6f 66 20 74 68 65  pshot..** of the
1a238 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 61   database from a
1a239 20 73 69 6e 67 6c 65 20 70 6f 69 6e 74 20 69 6e   single point in
1a23a 20 74 69 6d 65 2e 20 20 54 68 69 73 20 74 65 63   time.  This tec
1a23b 68 6e 69 71 75 65 20 61 6c 6c 6f 77 73 0d 0a 2a  hnique allows..*
1a23c 2a 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75  * multiple concu
1a23d 72 72 65 6e 74 20 72 65 61 64 65 72 73 20 74 6f  rrent readers to
1a23e 20 76 69 65 77 20 64 69 66 66 65 72 65 6e 74 20   view different 
1a23f 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
1a240 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63 6f 6e  database..** con
1a241 74 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75  tent simultaneou
1a242 73 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  sly...**..** The
1a243 20 72 65 61 64 65 72 20 61 6c 67 6f 72 69 74 68   reader algorith
1a244 6d 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  m in the previou
1a245 73 20 70 61 72 61 67 72 61 70 68 73 20 77 6f 72  s paragraphs wor
1a246 6b 73 20 63 6f 72 72 65 63 74 6c 79 2c 20 62 75  ks correctly, bu
1a247 74 20 0d 0a 2a 2a 20 62 65 63 61 75 73 65 20 66  t ..** because f
1a248 72 61 6d 65 73 20 66 6f 72 20 70 61 67 65 20 50  rames for page P
1a249 20 63 61 6e 20 61 70 70 65 61 72 20 61 6e 79 77   can appear anyw
1a24a 68 65 72 65 20 77 69 74 68 69 6e 20 74 68 65 20  here within the 
1a24b 57 41 4c 2c 20 74 68 65 0d 0a 2a 2a 20 72 65 61  WAL, the..** rea
1a24c 64 65 72 20 68 61 73 20 74 6f 20 73 63 61 6e 20  der has to scan 
1a24d 74 68 65 20 65 6e 74 69 72 65 20 57 41 4c 20 6c  the entire WAL l
1a24e 6f 6f 6b 69 6e 67 20 66 6f 72 20 70 61 67 65 20  ooking for page 
1a24f 50 20 66 72 61 6d 65 73 2e 20 20 49 66 20 74 68  P frames.  If th
1a250 65 0d 0a 2a 2a 20 57 41 4c 20 69 73 20 6c 61 72  e..** WAL is lar
1a251 67 65 20 28 6d 75 6c 74 69 70 6c 65 20 6d 65 67  ge (multiple meg
1a252 61 62 79 74 65 73 20 69 73 20 74 79 70 69 63 61  abytes is typica
1a253 6c 29 20 74 68 61 74 20 73 63 61 6e 20 63 61 6e  l) that scan can
1a254 20 62 65 20 73 6c 6f 77 2c 0d 0a 2a 2a 20 61 6e   be slow,..** an
1a255 64 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e  d read performan
1a256 63 65 20 73 75 66 66 65 72 73 2e 20 20 54 6f 20  ce suffers.  To 
1a257 6f 76 65 72 63 6f 6d 65 20 74 68 69 73 20 70 72  overcome this pr
1a258 6f 62 6c 65 6d 2c 20 61 20 73 65 70 61 72 61 74  oblem, a separat
1a259 65 0d 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63  e..** data struc
1a25a 74 75 72 65 20 63 61 6c 6c 65 64 20 74 68 65 20  ture called the 
1a25b 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 6d 61 69  wal-index is mai
1a25c 6e 74 61 69 6e 65 64 20 74 6f 20 65 78 70 65 64  ntained to exped
1a25d 69 74 65 20 74 68 65 0d 0a 2a 2a 20 73 65 61 72  ite the..** sear
1a25e 63 68 20 66 6f 72 20 66 72 61 6d 65 73 20 6f 66  ch for frames of
1a25f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 70 61   a particular pa
1a260 67 65 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 57 41 4c  ge...** ..** WAL
1a261 2d 49 4e 44 45 58 20 46 4f 52 4d 41 54 0d 0a 2a  -INDEX FORMAT..*
1a262 2a 0d 0a 2a 2a 20 43 6f 6e 63 65 70 74 75 61 6c  *..** Conceptual
1a263 6c 79 2c 20 74 68 65 20 77 61 6c 2d 69 6e 64 65  ly, the wal-inde
1a264 78 20 69 73 20 73 68 61 72 65 64 20 6d 65 6d 6f  x is shared memo
1a265 72 79 2c 20 74 68 6f 75 67 68 20 56 46 53 20 69  ry, though VFS i
1a266 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0d 0a  mplementations..
1a267 2a 2a 20 6d 69 67 68 74 20 63 68 6f 6f 73 65 20  ** might choose 
1a268 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1a269 20 77 61 6c 2d 69 6e 64 65 78 20 75 73 69 6e 67   wal-index using
1a26a 20 61 20 6d 6d 61 70 70 65 64 20 66 69 6c 65 2e   a mmapped file.
1a26b 20 20 42 65 63 61 75 73 65 0d 0a 2a 2a 20 74 68    Because..** th
1a26c 65 20 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73  e wal-index is s
1a26d 68 61 72 65 64 20 6d 65 6d 6f 72 79 2c 20 53 51  hared memory, SQ
1a26e 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 73 75  Lite does not su
1a26f 70 70 6f 72 74 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  pport journal_mo
1a270 64 65 3d 57 41 4c 20 0d 0a 2a 2a 20 6f 6e 20 61  de=WAL ..** on a
1a271 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73   network filesys
1a272 74 65 6d 2e 20 20 41 6c 6c 20 75 73 65 72 73 20  tem.  All users 
1a273 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1a274 6d 75 73 74 20 62 65 20 61 62 6c 65 20 74 6f 0d  must be able to.
1a275 0a 2a 2a 20 73 68 61 72 65 20 6d 65 6d 6f 72 79  .** share memory
1a276 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 77 61  ...**..** The wa
1a277 6c 2d 69 6e 64 65 78 20 69 73 20 74 72 61 6e 73  l-index is trans
1a278 69 65 6e 74 2e 20 20 41 66 74 65 72 20 61 20 63  ient.  After a c
1a279 72 61 73 68 2c 20 74 68 65 20 77 61 6c 2d 69 6e  rash, the wal-in
1a27a 64 65 78 20 63 61 6e 20 28 61 6e 64 20 73 68 6f  dex can (and sho
1a27b 75 6c 64 0d 0a 2a 2a 20 62 65 29 20 72 65 63 6f  uld..** be) reco
1a27c 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 74  nstructed from t
1a27d 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 41 4c 20  he original WAL 
1a27e 66 69 6c 65 2e 20 20 49 6e 20 66 61 63 74 2c 20  file.  In fact, 
1a27f 74 68 65 20 56 46 53 20 69 73 20 72 65 71 75 69  the VFS is requi
1a280 72 65 64 0d 0a 2a 2a 20 74 6f 20 65 69 74 68 65  red..** to eithe
1a281 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65  r truncate or ze
1a282 72 6f 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  ro the header of
1a283 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 77   the wal-index w
1a284 68 65 6e 20 74 68 65 20 6c 61 73 74 0d 0a 2a 2a  hen the last..**
1a285 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 69   connection to i
1a286 74 20 63 6c 6f 73 65 73 2e 20 20 42 65 63 61 75  t closes.  Becau
1a287 73 65 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78  se the wal-index
1a288 20 69 73 20 74 72 61 6e 73 69 65 6e 74 2c 20 69   is transient, i
1a289 74 20 63 61 6e 0d 0a 2a 2a 20 75 73 65 20 61 6e  t can..** use an
1a28a 20 61 72 63 68 69 74 65 63 74 75 72 65 2d 73 70   architecture-sp
1a28b 65 63 69 66 69 63 20 66 6f 72 6d 61 74 3b 20 69  ecific format; i
1a28c 74 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  t does not have 
1a28d 74 6f 20 62 65 20 63 72 6f 73 73 2d 70 6c 61 74  to be cross-plat
1a28e 66 6f 72 6d 2e 0d 0a 2a 2a 20 48 65 6e 63 65 2c  form...** Hence,
1a28f 20 75 6e 6c 69 6b 65 20 74 68 65 20 64 61 74 61   unlike the data
1a290 62 61 73 65 20 61 6e 64 20 57 41 4c 20 66 69 6c  base and WAL fil
1a291 65 20 66 6f 72 6d 61 74 73 20 77 68 69 63 68 20  e formats which 
1a292 73 74 6f 72 65 20 61 6c 6c 20 76 61 6c 75 65 73  store all values
1a293 0d 0a 2a 2a 20 61 73 20 62 69 67 20 65 6e 64 69  ..** as big endi
1a294 61 6e 2c 20 74 68 65 20 77 61 6c 2d 69 6e 64 65  an, the wal-inde
1a295 78 20 63 61 6e 20 73 74 6f 72 65 20 6d 75 6c 74  x can store mult
1a296 69 2d 62 79 74 65 20 76 61 6c 75 65 73 20 69 6e  i-byte values in
1a297 20 74 68 65 20 6e 61 74 69 76 65 0d 0a 2a 2a 20   the native..** 
1a298 62 79 74 65 20 6f 72 64 65 72 20 6f 66 20 74 68  byte order of th
1a299 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e  e host computer.
1a29a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70 75 72  ..**..** The pur
1a29b 70 6f 73 65 20 6f 66 20 74 68 65 20 77 61 6c 2d  pose of the wal-
1a29c 69 6e 64 65 78 20 69 73 20 74 6f 20 61 6e 73 77  index is to answ
1a29d 65 72 20 74 68 69 73 20 71 75 65 73 74 69 6f 6e  er this question
1a29e 20 71 75 69 63 6b 6c 79 3a 20 20 47 69 76 65 6e   quickly:  Given
1a29f 0d 0a 2a 2a 20 61 20 70 61 67 65 20 6e 75 6d 62  ..** a page numb
1a2a0 65 72 20 50 2c 20 72 65 74 75 72 6e 20 74 68 65  er P, return the
1a2a1 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6c 61   index of the la
1a2a2 73 74 20 66 72 61 6d 65 20 66 6f 72 20 70 61 67  st frame for pag
1a2a3 65 20 50 20 69 6e 20 74 68 65 20 57 41 4c 2c 0d  e P in the WAL,.
1a2a4 0a 2a 2a 20 6f 72 20 72 65 74 75 72 6e 20 4e 55  .** or return NU
1a2a5 4c 4c 20 69 66 20 74 68 65 72 65 20 61 72 65 20  LL if there are 
1a2a6 6e 6f 20 66 72 61 6d 65 73 20 66 6f 72 20 70 61  no frames for pa
1a2a7 67 65 20 50 20 69 6e 20 74 68 65 20 57 41 4c 2e  ge P in the WAL.
1a2a8 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 77 61 6c  ..**..** The wal
1a2a9 2d 69 6e 64 65 78 20 63 6f 6e 73 69 73 74 73 20  -index consists 
1a2aa 6f 66 20 61 20 68 65 61 64 65 72 20 72 65 67 69  of a header regi
1a2ab 6f 6e 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  on, followed by 
1a2ac 61 6e 20 6f 6e 65 20 6f 72 0d 0a 2a 2a 20 6d 6f  an one or..** mo
1a2ad 72 65 20 69 6e 64 65 78 20 62 6c 6f 63 6b 73 2e  re index blocks.
1a2ae 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 77    ..**..** The w
1a2af 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20  al-index header 
1a2b0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 6f 74  contains the tot
1a2b1 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61  al number of fra
1a2b2 6d 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 57  mes within the W
1a2b3 41 4c 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 74 68  AL..** in the th
1a2b4 65 20 6d 78 46 72 61 6d 65 20 66 69 65 6c 64 2e  e mxFrame field.
1a2b5 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61 63 68 20    ..**..** Each 
1a2b6 69 6e 64 65 78 20 62 6c 6f 63 6b 20 65 78 63 65  index block exce
1a2b7 70 74 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  pt for the first
1a2b8 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
1a2b9 61 74 69 6f 6e 20 6f 6e 20 0d 0a 2a 2a 20 48 41  ation on ..** HA
1a2ba 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20 66 72  SHTABLE_NPAGE fr
1a2bb 61 6d 65 73 2e 20 54 68 65 20 66 69 72 73 74 20  ames. The first 
1a2bc 69 6e 64 65 78 20 62 6c 6f 63 6b 20 63 6f 6e 74  index block cont
1a2bd 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ains information
1a2be 20 6f 6e 0d 0a 2a 2a 20 48 41 53 48 54 41 42 4c   on..** HASHTABL
1a2bf 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 66 72 61 6d  E_NPAGE_ONE fram
1a2c0 65 73 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6f  es. The values o
1a2c1 66 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47  f HASHTABLE_NPAG
1a2c2 45 5f 4f 4e 45 20 61 6e 64 20 0d 0a 2a 2a 20 48  E_ONE and ..** H
1a2c3 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20 61  ASHTABLE_NPAGE a
1a2c4 72 65 20 73 65 6c 65 63 74 65 64 20 73 6f 20 74  re selected so t
1a2c5 68 61 74 20 74 6f 67 65 74 68 65 72 20 74 68 65  hat together the
1a2c6 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65   wal-index heade
1a2c7 72 20 61 6e 64 0d 0a 2a 2a 20 66 69 72 73 74 20  r and..** first 
1a2c8 69 6e 64 65 78 20 62 6c 6f 63 6b 20 61 72 65 20  index block are 
1a2c9 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73  the same size as
1a2ca 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 64 65 78   all other index
1a2cb 20 62 6c 6f 63 6b 73 20 69 6e 20 74 68 65 0d 0a   blocks in the..
1a2cc 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 2e 0d 0a 2a  ** wal-index...*
1a2cd 2a 0d 0a 2a 2a 20 45 61 63 68 20 69 6e 64 65 78  *..** Each index
1a2ce 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20   block contains 
1a2cf 74 77 6f 20 73 65 63 74 69 6f 6e 73 2c 20 61 20  two sections, a 
1a2d0 70 61 67 65 2d 6d 61 70 70 69 6e 67 20 74 68 61  page-mapping tha
1a2d1 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0d 0a  t contains the..
1a2d2 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ** database page
1a2d3 20 6e 75 6d 62 65 72 20 61 73 73 6f 63 69 61 74   number associat
1a2d4 65 64 20 77 69 74 68 20 65 61 63 68 20 77 61 6c  ed with each wal
1a2d5 20 66 72 61 6d 65 2c 20 61 6e 64 20 61 20 68 61   frame, and a ha
1a2d6 73 68 2d 74 61 62 6c 65 20 0d 0a 2a 2a 20 74 68  sh-table ..** th
1a2d7 61 74 20 61 6c 6c 6f 77 73 20 72 65 61 64 65 72  at allows reader
1a2d8 73 20 74 6f 20 71 75 65 72 79 20 61 6e 20 69 6e  s to query an in
1a2d9 64 65 78 20 62 6c 6f 63 6b 20 66 6f 72 20 61 20  dex block for a 
1a2da 73 70 65 63 69 66 69 63 20 70 61 67 65 20 6e 75  specific page nu
1a2db 6d 62 65 72 2e 0d 0a 2a 2a 20 54 68 65 20 70 61  mber...** The pa
1a2dc 67 65 2d 6d 61 70 70 69 6e 67 20 69 73 20 61 6e  ge-mapping is an
1a2dd 20 61 72 72 61 79 20 6f 66 20 48 41 53 48 54 41   array of HASHTA
1a2de 42 4c 45 5f 4e 50 41 47 45 20 28 6f 72 20 48 41  BLE_NPAGE (or HA
1a2df 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e  SHTABLE_NPAGE_ON
1a2e0 45 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 66 69  E..** for the fi
1a2e1 72 73 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 29  rst index block)
1a2e2 20 33 32 2d 62 69 74 20 70 61 67 65 20 6e 75 6d   32-bit page num
1a2e3 62 65 72 73 2e 20 54 68 65 20 66 69 72 73 74 20  bers. The first 
1a2e4 65 6e 74 72 79 20 69 6e 20 74 68 65 20 0d 0a 2a  entry in the ..*
1a2e5 2a 20 66 69 72 73 74 20 69 6e 64 65 78 2d 62 6c  * first index-bl
1a2e6 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ock contains the
1a2e7 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6e   database page n
1a2e8 75 6d 62 65 72 20 63 6f 72 72 65 73 70 6f 6e 64  umber correspond
1a2e9 69 6e 67 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 66  ing to the..** f
1a2ea 69 72 73 74 20 66 72 61 6d 65 20 69 6e 20 74 68  irst frame in th
1a2eb 65 20 57 41 4c 20 66 69 6c 65 2e 20 54 68 65 20  e WAL file. The 
1a2ec 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74  first entry in t
1a2ed 68 65 20 73 65 63 6f 6e 64 20 69 6e 64 65 78 20  he second index 
1a2ee 62 6c 6f 63 6b 0d 0a 2a 2a 20 69 6e 20 74 68 65  block..** in the
1a2ef 20 57 41 4c 20 66 69 6c 65 20 63 6f 72 72 65 73   WAL file corres
1a2f0 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 28 48 41  ponds to the (HA
1a2f1 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e  SHTABLE_NPAGE_ON
1a2f2 45 2b 31 29 74 68 20 66 72 61 6d 65 20 69 6e 0d  E+1)th frame in.
1a2f3 0a 2a 2a 20 74 68 65 20 6c 6f 67 2c 20 61 6e 64  .** the log, and
1a2f4 20 73 6f 20 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   so on...**..** 
1a2f5 54 68 65 20 6c 61 73 74 20 69 6e 64 65 78 20 62  The last index b
1a2f6 6c 6f 63 6b 20 69 6e 20 61 20 77 61 6c 2d 69 6e  lock in a wal-in
1a2f7 64 65 78 20 75 73 75 61 6c 6c 79 20 63 6f 6e 74  dex usually cont
1a2f8 61 69 6e 73 20 6c 65 73 73 20 74 68 61 6e 20 74  ains less than t
1a2f9 68 65 20 66 75 6c 6c 0d 0a 2a 2a 20 63 6f 6d 70  he full..** comp
1a2fa 6c 65 6d 65 6e 74 20 6f 66 20 48 41 53 48 54 41  lement of HASHTA
1a2fb 42 4c 45 5f 4e 50 41 47 45 20 28 6f 72 20 48 41  BLE_NPAGE (or HA
1a2fc 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e  SHTABLE_NPAGE_ON
1a2fd 45 29 20 70 61 67 65 2d 6e 75 6d 62 65 72 73 2c  E) page-numbers,
1a2fe 0d 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f  ..** depending o
1a2ff 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  n the contents o
1a300 66 20 74 68 65 20 57 41 4c 20 66 69 6c 65 2e 20  f the WAL file. 
1a301 54 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 68  This does not ch
1a302 61 6e 67 65 20 74 68 65 0d 0a 2a 2a 20 61 6c 6c  ange the..** all
1a303 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 74  ocated size of t
1a304 68 65 20 70 61 67 65 2d 6d 61 70 70 69 6e 67 20  he page-mapping 
1a305 61 72 72 61 79 20 2d 20 74 68 65 20 70 61 67 65  array - the page
1a306 2d 6d 61 70 70 69 6e 67 20 61 72 72 61 79 20 6d  -mapping array m
1a307 65 72 65 6c 79 0d 0a 2a 2a 20 63 6f 6e 74 61 69  erely..** contai
1a308 6e 73 20 75 6e 75 73 65 64 20 65 6e 74 72 69 65  ns unused entrie
1a309 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 76 65 6e 20  s...**..** Even 
1a30a 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 68  without using th
1a30b 65 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68  e hash table, th
1a30c 65 20 6c 61 73 74 20 66 72 61 6d 65 20 66 6f 72  e last frame for
1a30d 20 70 61 67 65 20 50 0d 0a 2a 2a 20 63 61 6e 20   page P..** can 
1a30e 62 65 20 66 6f 75 6e 64 20 62 79 20 73 63 61 6e  be found by scan
1a30f 6e 69 6e 67 20 74 68 65 20 70 61 67 65 2d 6d 61  ning the page-ma
1a310 70 70 69 6e 67 20 73 65 63 74 69 6f 6e 73 20 6f  pping sections o
1a311 66 20 65 61 63 68 20 69 6e 64 65 78 20 62 6c 6f  f each index blo
1a312 63 6b 0d 0a 2a 2a 20 73 74 61 72 74 69 6e 67 20  ck..** starting 
1a313 77 69 74 68 20 74 68 65 20 6c 61 73 74 20 69 6e  with the last in
1a314 64 65 78 20 62 6c 6f 63 6b 20 61 6e 64 20 6d 6f  dex block and mo
1a315 76 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20  ving toward the 
1a316 66 69 72 73 74 2c 20 61 6e 64 0d 0a 2a 2a 20 77  first, and..** w
1a317 69 74 68 69 6e 20 65 61 63 68 20 69 6e 64 65 78  ithin each index
1a318 20 62 6c 6f 63 6b 2c 20 73 74 61 72 74 69 6e 67   block, starting
1a319 20 61 74 20 74 68 65 20 65 6e 64 20 61 6e 64 20   at the end and 
1a31a 6d 6f 76 69 6e 67 20 74 6f 77 61 72 64 20 74 68  moving toward th
1a31b 65 0d 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 2e  e..** beginning.
1a31c 20 20 54 68 65 20 66 69 72 73 74 20 65 6e 74 72    The first entr
1a31d 79 20 74 68 61 74 20 65 71 75 61 6c 73 20 50 20  y that equals P 
1a31e 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
1a31f 68 65 20 66 72 61 6d 65 0d 0a 2a 2a 20 68 6f 6c  he frame..** hol
1a320 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ding the content
1a321 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0d   for that page..
1a322 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 68 61 73 68  .**..** The hash
1a323 20 74 61 62 6c 65 20 63 6f 6e 73 69 73 74 73 20   table consists 
1a324 6f 66 20 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c  of HASHTABLE_NSL
1a325 4f 54 20 31 36 2d 62 69 74 20 75 6e 73 69 67 6e  OT 16-bit unsign
1a326 65 64 20 69 6e 74 65 67 65 72 73 2e 0d 0a 2a 2a  ed integers...**
1a327 20 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54   HASHTABLE_NSLOT
1a328 20 3d 20 32 2a 48 41 53 48 54 41 42 4c 45 5f 4e   = 2*HASHTABLE_N
1a329 50 41 47 45 2c 20 61 6e 64 20 74 68 65 72 65 20  PAGE, and there 
1a32a 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20  is one entry in 
1a32b 74 68 65 0d 0a 2a 2a 20 68 61 73 68 20 74 61 62  the..** hash tab
1a32c 6c 65 20 66 6f 72 20 65 61 63 68 20 70 61 67 65  le for each page
1a32d 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 6d   number in the m
1a32e 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e 2c 20  apping section, 
1a32f 73 6f 20 74 68 65 20 68 61 73 68 20 0d 0a 2a 2a  so the hash ..**
1a330 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72 20   table is never 
1a331 6d 6f 72 65 20 74 68 61 6e 20 68 61 6c 66 20 66  more than half f
1a332 75 6c 6c 2e 20 20 54 68 65 20 65 78 70 65 63 74  ull.  The expect
1a333 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  ed number of col
1a334 6c 69 73 69 6f 6e 73 20 0d 0a 2a 2a 20 70 72 69  lisions ..** pri
1a335 6f 72 20 74 6f 20 66 69 6e 64 69 6e 67 20 61 20  or to finding a 
1a336 6d 61 74 63 68 20 69 73 20 31 2e 20 20 45 61 63  match is 1.  Eac
1a337 68 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 68  h entry of the h
1a338 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 0d  ash table is an.
1a339 0a 2a 2a 20 31 2d 62 61 73 65 64 20 69 6e 64 65  .** 1-based inde
1a33a 78 20 6f 66 20 61 6e 20 65 6e 74 72 79 20 69 6e  x of an entry in
1a33b 20 74 68 65 20 6d 61 70 70 69 6e 67 20 73 65 63   the mapping sec
1a33c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 61 6d 65  tion of the same
1a33d 0d 0a 2a 2a 20 69 6e 64 65 78 20 62 6c 6f 63 6b  ..** index block
1a33e 2e 20 20 20 4c 65 74 20 4b 20 62 65 20 74 68 65  .   Let K be the
1a33f 20 31 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f   1-based index o
1a340 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  f the largest en
1a341 74 72 79 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 6d  try in..** the m
1a342 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e 2e 20  apping section. 
1a343 20 28 46 6f 72 20 69 6e 64 65 78 20 62 6c 6f 63   (For index bloc
1a344 6b 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ks other than th
1a345 65 20 6c 61 73 74 2c 20 4b 20 77 69 6c 6c 0d 0a  e last, K will..
1a346 2a 2a 20 61 6c 77 61 79 73 20 62 65 20 65 78 61  ** always be exa
1a347 63 74 6c 79 20 48 41 53 48 54 41 42 4c 45 5f 4e  ctly HASHTABLE_N
1a348 50 41 47 45 20 28 34 30 39 36 29 20 61 6e 64 20  PAGE (4096) and 
1a349 66 6f 72 20 74 68 65 20 6c 61 73 74 20 69 6e 64  for the last ind
1a34a 65 78 20 62 6c 6f 63 6b 0d 0a 2a 2a 20 4b 20 77  ex block..** K w
1a34b 69 6c 6c 20 62 65 20 28 6d 78 46 72 61 6d 65 25  ill be (mxFrame%
1a34c 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 29  HASHTABLE_NPAGE)
1a34d 2e 29 20 20 55 6e 75 73 65 64 20 73 6c 6f 74 73  .)  Unused slots
1a34e 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62   of the hash tab
1a34f 6c 65 0d 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61  le..** contain a
1a350 20 76 61 6c 75 65 20 6f 66 20 30 2e 0d 0a 2a 2a   value of 0...**
1a351 0d 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 66 6f 72  ..** To look for
1a352 20 70 61 67 65 20 50 20 69 6e 20 74 68 65 20 68   page P in the h
1a353 61 73 68 20 74 61 62 6c 65 2c 20 66 69 72 73 74  ash table, first
1a354 20 63 6f 6d 70 75 74 65 20 61 20 68 61 73 68 20   compute a hash 
1a355 69 4b 65 79 20 6f 6e 0d 0a 2a 2a 20 50 20 61 73  iKey on..** P as
1a356 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a   follows:..**..*
1a357 2a 20 20 20 20 20 20 69 4b 65 79 20 3d 20 28 50  *      iKey = (P
1a358 20 2a 20 33 38 33 29 20 25 20 48 41 53 48 54 41   * 383) % HASHTA
1a359 42 4c 45 5f 4e 53 4c 4f 54 0d 0a 2a 2a 0d 0a 2a  BLE_NSLOT..**..*
1a35a 2a 20 54 68 65 6e 20 73 74 61 72 74 20 73 63 61  * Then start sca
1a35b 6e 6e 69 6e 67 20 65 6e 74 72 69 65 73 20 6f 66  nning entries of
1a35c 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2c   the hash table,
1a35d 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69   starting with i
1a35e 4b 65 79 0d 0a 2a 2a 20 28 77 72 61 70 70 69 6e  Key..** (wrappin
1a35f 67 20 61 72 6f 75 6e 64 20 74 6f 20 74 68 65 20  g around to the 
1a360 62 65 67 69 6e 6e 69 6e 67 20 77 68 65 6e 20 74  beginning when t
1a361 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 68 61  he end of the ha
1a362 73 68 20 74 61 62 6c 65 20 69 73 0d 0a 2a 2a 20  sh table is..** 
1a363 72 65 61 63 68 65 64 29 20 75 6e 74 69 6c 20 61  reached) until a
1a364 6e 20 75 6e 75 73 65 64 20 68 61 73 68 20 73 6c  n unused hash sl
1a365 6f 74 20 69 73 20 66 6f 75 6e 64 2e 20 4c 65 74  ot is found. Let
1a366 20 74 68 65 20 66 69 72 73 74 20 75 6e 75 73 65   the first unuse
1a367 64 20 73 6c 6f 74 0d 0a 2a 2a 20 62 65 20 61 74  d slot..** be at
1a368 20 69 6e 64 65 78 20 69 55 6e 75 73 65 64 2e 20   index iUnused. 
1a369 20 28 69 55 6e 75 73 65 64 20 6d 69 67 68 74 20   (iUnused might 
1a36a 62 65 20 6c 65 73 73 20 74 68 61 6e 20 69 4b 65  be less than iKe
1a36b 79 20 69 66 20 74 68 65 72 65 20 77 61 73 0d 0a  y if there was..
1a36c 2a 2a 20 77 72 61 70 2d 61 72 6f 75 6e 64 2e 29  ** wrap-around.)
1a36d 20 42 65 63 61 75 73 65 20 74 68 65 20 68 61 73   Because the has
1a36e 68 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72  h table is never
1a36f 20 6d 6f 72 65 20 74 68 61 6e 20 68 61 6c 66 20   more than half 
1a370 66 75 6c 6c 2c 0d 0a 2a 2a 20 74 68 65 20 73 65  full,..** the se
1a371 61 72 63 68 20 69 73 20 67 75 61 72 61 6e 74 65  arch is guarante
1a372 65 64 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79  ed to eventually
1a373 20 68 69 74 20 61 6e 20 75 6e 75 73 65 64 20 65   hit an unused e
1a374 6e 74 72 79 2e 20 20 4c 65 74 20 0d 0a 2a 2a 20  ntry.  Let ..** 
1a375 69 4d 61 78 20 62 65 20 74 68 65 20 76 61 6c 75  iMax be the valu
1a376 65 20 62 65 74 77 65 65 6e 20 69 4b 65 79 20 61  e between iKey a
1a377 6e 64 20 69 55 6e 75 73 65 64 2c 20 63 6c 6f 73  nd iUnused, clos
1a378 65 73 74 20 74 6f 20 69 55 6e 75 73 65 64 2c 0d  est to iUnused,.
1a379 0a 2a 2a 20 77 68 65 72 65 20 61 48 61 73 68 5b  .** where aHash[
1a37a 69 4d 61 78 5d 3d 3d 50 2e 20 20 49 66 20 74 68  iMax]==P.  If th
1a37b 65 72 65 20 69 73 20 6e 6f 20 69 4d 61 78 20 65  ere is no iMax e
1a37c 6e 74 72 79 20 28 69 66 20 74 68 65 72 65 20 65  ntry (if there e
1a37d 78 69 73 74 73 0d 0a 2a 2a 20 6e 6f 20 68 61 73  xists..** no has
1a37e 68 20 73 6c 6f 74 20 73 75 63 68 20 74 68 61 74  h slot such that
1a37f 20 61 48 61 73 68 5b 69 5d 3d 3d 70 29 20 74 68   aHash[i]==p) th
1a380 65 6e 20 70 61 67 65 20 50 20 69 73 20 6e 6f 74  en page P is not
1a381 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 63 75 72 72   in the..** curr
1a382 65 6e 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 2e  ent index block.
1a383 20 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20    Otherwise the 
1a384 69 4d 61 78 2d 74 68 20 6d 61 70 70 69 6e 67 20  iMax-th mapping 
1a385 65 6e 74 72 79 20 6f 66 20 74 68 65 0d 0a 2a 2a  entry of the..**
1a386 20 63 75 72 72 65 6e 74 20 69 6e 64 65 78 20 62   current index b
1a387 6c 6f 63 6b 20 63 6f 72 72 65 73 70 6f 6e 64 73  lock corresponds
1a388 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
1a389 72 79 20 74 68 61 74 20 72 65 66 65 72 65 6e 63  ry that referenc
1a38a 65 73 20 0d 0a 2a 2a 20 70 61 67 65 20 50 2e 0d  es ..** page P..
1a38b 0a 2a 2a 0d 0a 2a 2a 20 41 20 68 61 73 68 20 73  .**..** A hash s
1a38c 65 61 72 63 68 20 62 65 67 69 6e 73 20 77 69 74  earch begins wit
1a38d 68 20 74 68 65 20 6c 61 73 74 20 69 6e 64 65 78  h the last index
1a38e 20 62 6c 6f 63 6b 20 61 6e 64 20 6d 6f 76 65 73   block and moves
1a38f 20 74 6f 77 61 72 64 20 74 68 65 0d 0a 2a 2a 20   toward the..** 
1a390 66 69 72 73 74 20 69 6e 64 65 78 20 62 6c 6f 63  first index bloc
1a391 6b 2c 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 65  k, looking for e
1a392 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e  ntries correspon
1a393 64 69 6e 67 20 74 6f 20 70 61 67 65 20 50 2e 20  ding to page P. 
1a394 20 4f 6e 0d 0a 2a 2a 20 61 76 65 72 61 67 65 2c   On..** average,
1a395 20 6f 6e 6c 79 20 74 77 6f 20 6f 72 20 74 68 72   only two or thr
1a396 65 65 20 73 6c 6f 74 73 20 69 6e 20 65 61 63 68  ee slots in each
1a397 20 69 6e 64 65 78 20 62 6c 6f 63 6b 20 6e 65 65   index block nee
1a398 64 20 74 6f 20 62 65 0d 0a 2a 2a 20 65 78 61 6d  d to be..** exam
1a399 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ined in order to
1a39a 20 65 69 74 68 65 72 20 66 69 6e 64 20 74 68 65   either find the
1a39b 20 6c 61 73 74 20 65 6e 74 72 79 20 66 6f 72 20   last entry for 
1a39c 70 61 67 65 20 50 2c 20 6f 72 20 74 6f 0d 0a 2a  page P, or to..*
1a39d 2a 20 65 73 74 61 62 6c 69 73 68 20 74 68 61 74  * establish that
1a39e 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 20 65   no such entry e
1a39f 78 69 73 74 73 20 69 6e 20 74 68 65 20 62 6c 6f  xists in the blo
1a3a0 63 6b 2e 20 20 45 61 63 68 20 69 6e 64 65 78 20  ck.  Each index 
1a3a1 62 6c 6f 63 6b 0d 0a 2a 2a 20 68 6f 6c 64 73 20  block..** holds 
1a3a2 6f 76 65 72 20 34 30 30 30 20 65 6e 74 72 69 65  over 4000 entrie
1a3a3 73 2e 20 20 53 6f 20 74 77 6f 20 6f 72 20 74 68  s.  So two or th
1a3a4 72 65 65 20 69 6e 64 65 78 20 62 6c 6f 63 6b 73  ree index blocks
1a3a5 20 61 72 65 20 73 75 66 66 69 63 69 65 6e 74 0d   are sufficient.
1a3a6 0a 2a 2a 20 74 6f 20 63 6f 76 65 72 20 61 20 74  .** to cover a t
1a3a7 79 70 69 63 61 6c 20 31 30 20 6d 65 67 61 62 79  ypical 10 megaby
1a3a8 74 65 20 57 41 4c 20 66 69 6c 65 2c 20 61 73 73  te WAL file, ass
1a3a9 75 6d 69 6e 67 20 31 4b 20 70 61 67 65 73 2e 20  uming 1K pages. 
1a3aa 20 38 20 6f 72 20 31 30 0d 0a 2a 2a 20 63 6f 6d   8 or 10..** com
1a3ab 70 61 72 69 73 6f 6e 73 20 28 6f 6e 20 61 76 65  parisons (on ave
1a3ac 72 61 67 65 29 20 73 75 66 66 69 63 65 20 74 6f  rage) suffice to
1a3ad 20 65 69 74 68 65 72 20 6c 6f 63 61 74 65 20 61   either locate a
1a3ae 20 66 72 61 6d 65 20 69 6e 20 74 68 65 0d 0a 2a   frame in the..*
1a3af 2a 20 57 41 4c 20 6f 72 20 74 6f 20 65 73 74 61  * WAL or to esta
1a3b0 62 6c 69 73 68 20 74 68 61 74 20 74 68 65 20 66  blish that the f
1a3b1 72 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20 65 78  rame does not ex
1a3b2 69 73 74 20 69 6e 20 74 68 65 20 57 41 4c 2e 20  ist in the WAL. 
1a3b3 20 54 68 69 73 0d 0a 2a 2a 20 69 73 20 6d 75 63   This..** is muc
1a3b4 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 73 63  h faster than sc
1a3b5 61 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 72  anning the entir
1a3b6 65 20 31 30 4d 42 20 57 41 4c 2e 0d 0a 2a 2a 0d  e 10MB WAL...**.
1a3b7 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 65 6e  .** Note that en
1a3b8 74 72 69 65 73 20 61 72 65 20 61 64 64 65 64 20  tries are added 
1a3b9 69 6e 20 6f 72 64 65 72 20 6f 66 20 69 6e 63 72  in order of incr
1a3ba 65 61 73 69 6e 67 20 4b 2e 20 20 48 65 6e 63 65  easing K.  Hence
1a3bb 2c 20 6f 6e 65 0d 0a 2a 2a 20 72 65 61 64 65 72  , one..** reader
1a3bc 20 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67 20   might be using 
1a3bd 73 6f 6d 65 20 76 61 6c 75 65 20 4b 30 20 61 6e  some value K0 an
1a3be 64 20 61 20 73 65 63 6f 6e 64 20 72 65 61 64 65  d a second reade
1a3bf 72 20 74 68 61 74 20 73 74 61 72 74 65 64 0d 0a  r that started..
1a3c0 2a 2a 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  ** at a later ti
1a3c1 6d 65 20 28 61 66 74 65 72 20 61 64 64 69 74 69  me (after additi
1a3c2 6f 6e 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e  onal transaction
1a3c3 73 20 77 65 72 65 20 61 64 64 65 64 20 74 6f 20  s were added to 
1a3c4 74 68 65 20 57 41 4c 0d 0a 2a 2a 20 61 6e 64 20  the WAL..** and 
1a3c5 74 6f 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78  to the wal-index
1a3c6 29 20 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67  ) might be using
1a3c7 20 61 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c   a different val
1a3c8 75 65 20 4b 31 2c 20 77 68 65 72 65 20 4b 31 3e  ue K1, where K1>
1a3c9 4b 30 2e 0d 0a 2a 2a 20 42 6f 74 68 20 72 65 61  K0...** Both rea
1a3ca 64 65 72 73 20 63 61 6e 20 75 73 65 20 74 68 65  ders can use the
1a3cb 20 73 61 6d 65 20 68 61 73 68 20 74 61 62 6c 65   same hash table
1a3cc 20 61 6e 64 20 6d 61 70 70 69 6e 67 20 73 65 63   and mapping sec
1a3cd 74 69 6f 6e 20 74 6f 20 67 65 74 0d 0a 2a 2a 20  tion to get..** 
1a3ce 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 73 75  the correct resu
1a3cf 6c 74 2e 20 20 54 68 65 72 65 20 6d 61 79 20 62  lt.  There may b
1a3d0 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
1a3d1 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 74 68   hash table with
1a3d2 0d 0a 2a 2a 20 4b 3e 4b 30 20 62 75 74 20 74 6f  ..** K>K0 but to
1a3d3 20 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65   the first reade
1a3d4 72 2c 20 74 68 6f 73 65 20 65 6e 74 72 69 65 73  r, those entries
1a3d5 20 77 69 6c 6c 20 61 70 70 65 61 72 20 74 6f 20   will appear to 
1a3d6 62 65 20 75 6e 75 73 65 64 0d 0a 2a 2a 20 73 6c  be unused..** sl
1a3d7 6f 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 20  ots in the hash 
1a3d8 74 61 62 6c 65 20 61 6e 64 20 73 6f 20 74 68 65  table and so the
1a3d9 20 66 69 72 73 74 20 72 65 61 64 65 72 20 77 69   first reader wi
1a3da 6c 6c 20 67 65 74 20 61 6e 20 61 6e 73 77 65 72  ll get an answer
1a3db 20 61 73 0d 0a 2a 2a 20 69 66 20 6e 6f 20 76 61   as..** if no va
1a3dc 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 68 61  lues greater tha
1a3dd 6e 20 4b 30 20 68 61 64 20 65 76 65 72 20 62 65  n K0 had ever be
1a3de 65 6e 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  en inserted into
1a3df 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0d   the hash table.
1a3e0 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74  .** in the first
1a3e1 20 70 6c 61 63 65 20 2d 20 77 68 69 63 68 20 69   place - which i
1a3e2 73 20 77 68 61 74 20 72 65 61 64 65 72 20 6f 6e  s what reader on
1a3e3 65 20 77 61 6e 74 73 2e 20 20 4d 65 61 6e 77 68  e wants.  Meanwh
1a3e4 69 6c 65 2c 20 74 68 65 0d 0a 2a 2a 20 73 65 63  ile, the..** sec
1a3e5 6f 6e 64 20 72 65 61 64 65 72 20 75 73 69 6e 67  ond reader using
1a3e6 20 4b 31 20 77 69 6c 6c 20 73 65 65 20 61 64 64   K1 will see add
1a3e7 69 74 69 6f 6e 61 6c 20 76 61 6c 75 65 73 20 74  itional values t
1a3e8 68 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 65  hat were inserte
1a3e9 64 0d 0a 2a 2a 20 6c 61 74 65 72 2c 20 77 68 69  d..** later, whi
1a3ea 63 68 20 69 73 20 65 78 61 63 74 6c 79 20 77 68  ch is exactly wh
1a3eb 61 74 20 72 65 61 64 65 72 20 74 77 6f 20 77 61  at reader two wa
1a3ec 6e 74 73 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 57  nts.  ..**..** W
1a3ed 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f  hen a rollback o
1a3ee 63 63 75 72 73 2c 20 74 68 65 20 76 61 6c 75 65  ccurs, the value
1a3ef 20 6f 66 20 4b 20 69 73 20 64 65 63 72 65 61 73   of K is decreas
1a3f0 65 64 2e 20 48 61 73 68 20 74 61 62 6c 65 20 65  ed. Hash table e
1a3f1 6e 74 72 69 65 73 0d 0a 2a 2a 20 74 68 61 74 20  ntries..** that 
1a3f2 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 66 72  correspond to fr
1a3f3 61 6d 65 73 20 67 72 65 61 74 65 72 20 74 68 61  ames greater tha
1a3f4 6e 20 74 68 65 20 6e 65 77 20 4b 20 76 61 6c 75  n the new K valu
1a3f5 65 20 61 72 65 20 72 65 6d 6f 76 65 64 0d 0a 2a  e are removed..*
1a3f6 2a 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  * from the hash 
1a3f7 74 61 62 6c 65 20 61 74 20 74 68 69 73 20 70 6f  table at this po
1a3f8 69 6e 74 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  int...*/..#ifnde
1a3f9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
1a3fa 4c 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72  L....../*..** Tr
1a3fb 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63 72 6f  ace output macro
1a3fc 73 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66 69 6e  s..*/..#if defin
1a3fd 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
1a3fe 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
1a3ff 45 5f 44 45 42 55 47 29 0d 0a 53 51 4c 49 54 45  E_DEBUG)..SQLITE
1a400 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a401 69 74 65 33 57 61 6c 54 72 61 63 65 20 3d 20 30  ite3WalTrace = 0
1a402 3b 0d 0a 23 20 64 65 66 69 6e 65 20 57 41 4c 54  ;..# define WALT
1a403 52 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69  RACE(X)  if(sqli
1a404 74 65 33 57 61 6c 54 72 61 63 65 29 20 73 71 6c  te3WalTrace) sql
1a405 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20  ite3DebugPrintf 
1a406 58 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  X..#else..# defi
1a407 6e 65 20 57 41 4c 54 52 41 43 45 28 58 29 0d 0a  ne WALTRACE(X)..
1a408 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
1a409 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 28 61 6e   The maximum (an
1a40a 64 20 6f 6e 6c 79 29 20 76 65 72 73 69 6f 6e 73  d only) versions
1a40b 20 6f 66 20 74 68 65 20 77 61 6c 20 61 6e 64 20   of the wal and 
1a40c 77 61 6c 2d 69 6e 64 65 78 20 66 6f 72 6d 61 74  wal-index format
1a40d 73 0d 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62  s..** that may b
1a40e 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 62 79  e interpreted by
1a40f 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   this version of
1a410 20 53 51 4c 69 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a   SQLite...**..**
1a411 20 49 66 20 61 20 63 6c 69 65 6e 74 20 62 65 67   If a client beg
1a412 69 6e 73 20 72 65 63 6f 76 65 72 69 6e 67 20 61  ins recovering a
1a413 20 57 41 4c 20 66 69 6c 65 20 61 6e 64 20 66 69   WAL file and fi
1a414 6e 64 73 20 74 68 61 74 20 28 61 29 20 74 68 65  nds that (a) the
1a415 20 63 68 65 63 6b 73 75 6d 0d 0a 2a 2a 20 76 61   checksum..** va
1a416 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d  lues in the wal-
1a417 68 65 61 64 65 72 20 61 72 65 20 63 6f 72 72 65  header are corre
1a418 63 74 20 61 6e 64 20 28 62 29 20 74 68 65 20 76  ct and (b) the v
1a419 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20  ersion field is 
1a41a 6e 6f 74 0d 0a 2a 2a 20 57 41 4c 5f 4d 41 58 5f  not..** WAL_MAX_
1a41b 56 45 52 53 49 4f 4e 2c 20 72 65 63 6f 76 65 72  VERSION, recover
1a41c 79 20 66 61 69 6c 73 20 61 6e 64 20 53 51 4c 69  y fails and SQLi
1a41d 74 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  te returns SQLIT
1a41e 45 5f 43 41 4e 54 4f 50 45 4e 2e 0d 0a 2a 2a 0d  E_CANTOPEN...**.
1a41f 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69  .** Similarly, i
1a420 66 20 61 20 63 6c 69 65 6e 74 20 73 75 63 63 65  f a client succe
1a421 73 73 66 75 6c 6c 79 20 72 65 61 64 73 20 61 20  ssfully reads a 
1a422 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72  wal-index header
1a423 20 28 69 2e 65 2e 20 74 68 65 20 0d 0a 2a 2a 20   (i.e. the ..** 
1a424 63 68 65 63 6b 73 75 6d 20 74 65 73 74 20 69 73  checksum test is
1a425 20 73 75 63 63 65 73 73 66 75 6c 29 20 61 6e 64   successful) and
1a426 20 66 69 6e 64 73 20 74 68 61 74 20 74 68 65 20   finds that the 
1a427 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73  version field is
1a428 20 6e 6f 74 0d 0a 2a 2a 20 57 41 4c 49 4e 44 45   not..** WALINDE
1a429 58 5f 4d 41 58 5f 56 45 52 53 49 4f 4e 2c 20 74  X_MAX_VERSION, t
1a42a 68 65 6e 20 6e 6f 20 72 65 61 64 2d 74 72 61 6e  hen no read-tran
1a42b 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65  saction is opene
1a42c 64 20 61 6e 64 20 53 51 4c 69 74 65 0d 0a 2a 2a  d and SQLite..**
1a42d 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1a42e 43 41 4e 54 4f 50 45 4e 2e 0d 0a 2a 2f 0d 0a 23  CANTOPEN...*/..#
1a42f 64 65 66 69 6e 65 20 57 41 4c 5f 4d 41 58 5f 56  define WAL_MAX_V
1a430 45 52 53 49 4f 4e 20 20 20 20 20 20 33 30 30 37  ERSION      3007
1a431 30 30 30 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c  000..#define WAL
1a432 49 4e 44 45 58 5f 4d 41 58 5f 56 45 52 53 49 4f  INDEX_MAX_VERSIO
1a433 4e 20 33 30 30 37 30 30 30 0d 0a 0d 0a 2f 2a 0d  N 3007000..../*.
1a434 0a 2a 2a 20 49 6e 64 69 63 65 73 20 6f 66 20 76  .** Indices of v
1a435 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 62  arious locking b
1a436 79 74 65 73 2e 20 20 20 57 41 4c 5f 4e 52 45 41  ytes.   WAL_NREA
1a437 44 45 52 20 69 73 20 74 68 65 20 6e 75 6d 62 65  DER is the numbe
1a438 72 0d 0a 2a 2a 20 6f 66 20 61 76 61 69 6c 61 62  r..** of availab
1a439 6c 65 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 20  le reader locks 
1a43a 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 61 74  and should be at
1a43b 20 6c 65 61 73 74 20 33 2e 0d 0a 2a 2f 0d 0a 23   least 3...*/..#
1a43c 64 65 66 69 6e 65 20 57 41 4c 5f 57 52 49 54 45  define WAL_WRITE
1a43d 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0d  _LOCK         0.
1a43e 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 41 4c 4c  .#define WAL_ALL
1a43f 5f 42 55 54 5f 57 52 49 54 45 20 20 20 20 20 20  _BUT_WRITE      
1a440 31 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 43  1..#define WAL_C
1a441 4b 50 54 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20  KPT_LOCK        
1a442 20 20 31 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c    1..#define WAL
1a443 5f 52 45 43 4f 56 45 52 5f 4c 4f 43 4b 20 20 20  _RECOVER_LOCK   
1a444 20 20 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 57      2..#define W
1a445 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 49 29 20  AL_READ_LOCK(I) 
1a446 20 20 20 20 20 20 28 33 2b 28 49 29 29 0d 0a 23        (3+(I))..#
1a447 64 65 66 69 6e 65 20 57 41 4c 5f 4e 52 45 41 44  define WAL_NREAD
1a448 45 52 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ER            (S
1a449 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 2d  QLITE_SHM_NLOCK-
1a44a 33 29 0d 0a 0d 0a 0d 0a 2f 2a 20 4f 62 6a 65 63  3)....../* Objec
1a44b 74 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a  t declarations *
1a44c 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
1a44d 74 20 57 61 6c 49 6e 64 65 78 48 64 72 20 57 61  t WalIndexHdr Wa
1a44e 6c 49 6e 64 65 78 48 64 72 3b 0d 0a 74 79 70 65  lIndexHdr;..type
1a44f 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 49 74  def struct WalIt
1a450 65 72 61 74 6f 72 20 57 61 6c 49 74 65 72 61 74  erator WalIterat
1a451 6f 72 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72  or;..typedef str
1a452 75 63 74 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20  uct WalCkptInfo 
1a453 57 61 6c 43 6b 70 74 49 6e 66 6f 3b 0d 0a 0d 0a  WalCkptInfo;....
1a454 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  ../*..** The fol
1a455 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f  lowing object ho
1a456 6c 64 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  lds a copy of th
1a457 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  e wal-index head
1a458 65 72 20 63 6f 6e 74 65 6e 74 2e 0d 0a 2a 2a 0d  er content...**.
1a459 0a 2a 2a 20 54 68 65 20 61 63 74 75 61 6c 20 68  .** The actual h
1a45a 65 61 64 65 72 20 69 6e 20 74 68 65 20 77 61 6c  eader in the wal
1a45b 2d 69 6e 64 65 78 20 63 6f 6e 73 69 73 74 73 20  -index consists 
1a45c 6f 66 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66  of two copies of
1a45d 20 74 68 69 73 0d 0a 2a 2a 20 6f 62 6a 65 63 74   this..** object
1a45e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 7a  ...**..** The sz
1a45f 50 61 67 65 20 76 61 6c 75 65 20 63 61 6e 20 62  Page value can b
1a460 65 20 61 6e 79 20 70 6f 77 65 72 20 6f 66 20 32  e any power of 2
1a461 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
1a462 20 33 32 37 36 38 2c 20 69 6e 63 6c 75 73 69 76   32768, inclusiv
1a463 65 2e 0d 0a 2a 2a 20 4f 72 20 69 74 20 63 61 6e  e...** Or it can
1a464 20 62 65 20 31 20 74 6f 20 72 65 70 72 65 73 65   be 1 to represe
1a465 6e 74 20 61 20 36 35 35 33 36 2d 62 79 74 65 20  nt a 65536-byte 
1a466 70 61 67 65 2e 20 20 54 68 65 20 6c 61 74 74 65  page.  The latte
1a467 72 20 63 61 73 65 20 77 61 73 0d 0a 2a 2a 20 61  r case was..** a
1a468 64 64 65 64 20 69 6e 20 33 2e 37 2e 31 20 77 68  dded in 3.7.1 wh
1a469 65 6e 20 73 75 70 70 6f 72 74 20 66 6f 72 20 36  en support for 6
1a46a 34 4b 20 70 61 67 65 73 20 77 61 73 20 61 64 64  4K pages was add
1a46b 65 64 2e 20 20 0d 0a 2a 2f 0d 0a 73 74 72 75 63  ed.  ..*/..struc
1a46c 74 20 57 61 6c 49 6e 64 65 78 48 64 72 20 7b 0d  t WalIndexHdr {.
1a46d 0a 20 20 75 33 32 20 69 56 65 72 73 69 6f 6e 3b  .  u32 iVersion;
1a46e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a46f 20 20 20 2f 2a 20 57 61 6c 2d 69 6e 64 65 78 20     /* Wal-index 
1a470 76 65 72 73 69 6f 6e 20 2a 2f 0d 0a 20 20 75 33  version */..  u3
1a471 32 20 75 6e 75 73 65 64 3b 20 20 20 20 20 20 20  2 unused;       
1a472 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a473 20 55 6e 75 73 65 64 20 28 70 61 64 64 69 6e 67   Unused (padding
1a474 29 20 66 69 65 6c 64 20 2a 2f 0d 0a 20 20 75 33  ) field */..  u3
1a475 32 20 69 43 68 61 6e 67 65 3b 20 20 20 20 20 20  2 iChange;      
1a476 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a477 20 43 6f 75 6e 74 65 72 20 69 6e 63 72 65 6d 65   Counter increme
1a478 6e 74 65 64 20 65 61 63 68 20 74 72 61 6e 73 61  nted each transa
1a479 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 75 38 20 69  ction */..  u8 i
1a47a 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20  sInit;          
1a47b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
1a47c 20 77 68 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   when initialize
1a47d 64 20 2a 2f 0d 0a 20 20 75 38 20 62 69 67 45 6e  d */..  u8 bigEn
1a47e 64 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20  dCksum;         
1a47f 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1a480 69 66 20 63 68 65 63 6b 73 75 6d 73 20 69 6e 20  if checksums in 
1a481 57 41 4c 20 61 72 65 20 62 69 67 2d 65 6e 64 69  WAL are big-endi
1a482 61 6e 20 2a 2f 0d 0a 20 20 75 31 36 20 73 7a 50  an */..  u16 szP
1a483 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
1a484 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1a485 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69  base page size i
1a486 6e 20 62 79 74 65 73 2e 20 31 3d 3d 36 34 4b 20  n bytes. 1==64K 
1a487 2a 2f 0d 0a 20 20 75 33 32 20 6d 78 46 72 61 6d  */..  u32 mxFram
1a488 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1a489 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1a48a 66 20 6c 61 73 74 20 76 61 6c 69 64 20 66 72 61  f last valid fra
1a48b 6d 65 20 69 6e 20 74 68 65 20 57 41 4c 20 2a 2f  me in the WAL */
1a48c 0d 0a 20 20 75 33 32 20 6e 50 61 67 65 3b 20 20  ..  u32 nPage;  
1a48d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a48e 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64      /* Size of d
1a48f 61 74 61 62 61 73 65 20 69 6e 20 70 61 67 65 73  atabase in pages
1a490 20 2a 2f 0d 0a 20 20 75 33 32 20 61 46 72 61 6d   */..  u32 aFram
1a491 65 43 6b 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20  eCksum[2];      
1a492 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73         /* Checks
1a493 75 6d 20 6f 66 20 6c 61 73 74 20 66 72 61 6d 65  um of last frame
1a494 20 69 6e 20 6c 6f 67 20 2a 2f 0d 0a 20 20 75 33   in log */..  u3
1a495 32 20 61 53 61 6c 74 5b 32 5d 3b 20 20 20 20 20  2 aSalt[2];     
1a496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a497 20 54 77 6f 20 73 61 6c 74 20 76 61 6c 75 65 73   Two salt values
1a498 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 57 41 4c   copied from WAL
1a499 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20 75 33   header */..  u3
1a49a 32 20 61 43 6b 73 75 6d 5b 32 5d 3b 20 20 20 20  2 aCksum[2];    
1a49b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a49c 20 43 68 65 63 6b 73 75 6d 20 6f 76 65 72 20 61   Checksum over a
1a49d 6c 6c 20 70 72 69 6f 72 20 66 69 65 6c 64 73 20  ll prior fields 
1a49e 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
1a49f 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66   A copy of the f
1a4a0 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20  ollowing object 
1a4a1 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 77 61  occurs in the wa
1a4a2 6c 2d 69 6e 64 65 78 20 69 6d 6d 65 64 69 61 74  l-index immediat
1a4a3 65 6c 79 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  ely..** followin
1a4a4 67 20 74 68 65 20 73 65 63 6f 6e 64 20 63 6f 70  g the second cop
1a4a5 79 20 6f 66 20 74 68 65 20 57 61 6c 49 6e 64 65  y of the WalInde
1a4a6 78 48 64 72 2e 20 20 54 68 69 73 20 6f 62 6a 65  xHdr.  This obje
1a4a7 63 74 20 73 74 6f 72 65 73 0d 0a 2a 2a 20 69 6e  ct stores..** in
1a4a8 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62  formation used b
1a4a9 79 20 63 68 65 63 6b 70 6f 69 6e 74 2e 0d 0a 2a  y checkpoint...*
1a4aa 2a 0d 0a 2a 2a 20 6e 42 61 63 6b 66 69 6c 6c 20  *..** nBackfill 
1a4ab 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1a4ac 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 57   frames in the W
1a4ad 41 4c 20 74 68 61 74 20 68 61 76 65 20 62 65 65  AL that have bee
1a4ae 6e 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 62 61  n written..** ba
1a4af 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ck into the data
1a4b0 62 61 73 65 2e 20 28 57 65 20 63 61 6c 6c 20 74  base. (We call t
1a4b1 68 65 20 61 63 74 20 6f 66 20 6d 6f 76 69 6e 67  he act of moving
1a4b2 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 57 41   content from WA
1a4b3 4c 20 74 6f 0d 0a 2a 2a 20 64 61 74 61 62 61 73  L to..** databas
1a4b4 65 20 22 62 61 63 6b 66 69 6c 6c 69 6e 67 22 2e  e "backfilling".
1a4b5 29 20 20 54 68 65 20 6e 42 61 63 6b 66 69 6c 6c  )  The nBackfill
1a4b6 20 6e 75 6d 62 65 72 20 69 73 20 6e 65 76 65 72   number is never
1a4b7 20 67 72 65 61 74 65 72 20 74 68 61 6e 0d 0a 2a   greater than..*
1a4b8 2a 20 57 61 6c 49 6e 64 65 78 48 64 72 2e 6d 78  * WalIndexHdr.mx
1a4b9 46 72 61 6d 65 2e 20 20 6e 42 61 63 6b 66 69 6c  Frame.  nBackfil
1a4ba 6c 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 69 6e  l can only be in
1a4bb 63 72 65 61 73 65 64 20 62 79 20 74 68 72 65 61  creased by threa
1a4bc 64 73 0d 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 74  ds..** holding t
1a4bd 68 65 20 57 41 4c 5f 43 4b 50 54 5f 4c 4f 43 4b  he WAL_CKPT_LOCK
1a4be 20 6c 6f 63 6b 20 28 77 68 69 63 68 20 69 6e 63   lock (which inc
1a4bf 6c 75 64 65 73 20 61 20 72 65 63 6f 76 65 72 79  ludes a recovery
1a4c0 20 74 68 72 65 61 64 29 2e 0d 0a 2a 2a 20 48 6f   thread)...** Ho
1a4c1 77 65 76 65 72 2c 20 61 20 57 41 4c 5f 57 52 49  wever, a WAL_WRI
1a4c2 54 45 5f 4c 4f 43 4b 20 74 68 72 65 61 64 20 63  TE_LOCK thread c
1a4c3 61 6e 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75  an move the valu
1a4c4 65 20 6f 66 20 6e 42 61 63 6b 66 69 6c 6c 20 66  e of nBackfill f
1a4c5 72 6f 6d 0d 0a 2a 2a 20 6d 78 46 72 61 6d 65 20  rom..** mxFrame 
1a4c6 62 61 63 6b 20 74 6f 20 7a 65 72 6f 20 77 68 65  back to zero whe
1a4c7 6e 20 74 68 65 20 57 41 4c 20 69 73 20 72 65 73  n the WAL is res
1a4c8 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72  et...**..** Ther
1a4c9 65 20 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69  e is one entry i
1a4ca 6e 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 66 6f  n aReadMark[] fo
1a4cb 72 20 65 61 63 68 20 72 65 61 64 65 72 20 6c 6f  r each reader lo
1a4cc 63 6b 2e 20 20 49 66 20 61 20 72 65 61 64 65 72  ck.  If a reader
1a4cd 0d 0a 2a 2a 20 68 6f 6c 64 73 20 72 65 61 64 2d  ..** holds read-
1a4ce 6c 6f 63 6b 20 4b 2c 20 74 68 65 6e 20 74 68 65  lock K, then the
1a4cf 20 76 61 6c 75 65 20 69 6e 20 61 52 65 61 64 4d   value in aReadM
1a4d0 61 72 6b 5b 4b 5d 20 69 73 20 6e 6f 20 67 72 65  ark[K] is no gre
1a4d1 61 74 65 72 20 74 68 61 6e 0d 0a 2a 2a 20 74 68  ater than..** th
1a4d2 65 20 6d 78 46 72 61 6d 65 20 66 6f 72 20 74 68  e mxFrame for th
1a4d3 61 74 20 72 65 61 64 65 72 2e 20 20 54 68 65 20  at reader.  The 
1a4d4 76 61 6c 75 65 20 52 45 41 44 4d 41 52 4b 5f 4e  value READMARK_N
1a4d5 4f 54 5f 55 53 45 44 20 28 30 78 66 66 66 66 66  OT_USED (0xfffff
1a4d6 66 66 66 29 0d 0a 2a 2a 20 66 6f 72 20 61 6e 79  fff)..** for any
1a4d7 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 6d 65 61   aReadMark[] mea
1a4d8 6e 73 20 74 68 61 74 20 65 6e 74 72 79 20 69 73  ns that entry is
1a4d9 20 75 6e 75 73 65 64 2e 20 20 61 52 65 61 64 4d   unused.  aReadM
1a4da 61 72 6b 5b 30 5d 20 69 73 20 0d 0a 2a 2a 20 61  ark[0] is ..** a
1a4db 20 73 70 65 63 69 61 6c 20 63 61 73 65 3b 20 69   special case; i
1a4dc 74 73 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65  ts value is neve
1a4dd 72 20 75 73 65 64 20 61 6e 64 20 69 74 20 65 78  r used and it ex
1a4de 69 73 74 73 20 61 73 20 61 20 70 6c 61 63 65 2d  ists as a place-
1a4df 68 6f 6c 64 65 72 0d 0a 2a 2a 20 74 6f 20 61 76  holder..** to av
1a4e0 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 6f 66  oid having to of
1a4e1 66 73 65 74 20 61 52 65 61 64 4d 61 72 6b 5b 5d  fset aReadMark[]
1a4e2 20 69 6e 64 65 78 73 20 62 79 20 6f 6e 65 2e 20   indexs by one. 
1a4e3 20 52 65 61 64 65 72 73 20 68 6f 6c 64 69 6e 67   Readers holding
1a4e4 0d 0a 2a 2a 20 57 41 4c 5f 52 45 41 44 5f 4c 4f  ..** WAL_READ_LO
1a4e5 43 4b 28 30 29 20 61 6c 77 61 79 73 20 69 67 6e  CK(0) always ign
1a4e6 6f 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 57  ore the entire W
1a4e7 41 4c 20 61 6e 64 20 72 65 61 64 20 61 6c 6c 20  AL and read all 
1a4e8 63 6f 6e 74 65 6e 74 0d 0a 2a 2a 20 64 69 72 65  content..** dire
1a4e9 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61  ctly from the da
1a4ea 74 61 62 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tabase...**..** 
1a4eb 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 52 65  The value of aRe
1a4ec 61 64 4d 61 72 6b 5b 4b 5d 20 6d 61 79 20 6f 6e  adMark[K] may on
1a4ed 6c 79 20 62 65 20 63 68 61 6e 67 65 64 20 62 79  ly be changed by
1a4ee 20 61 20 74 68 72 65 61 64 20 74 68 61 74 0d 0a   a thread that..
1a4ef 2a 2a 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e  ** is holding an
1a4f0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
1a4f1 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b  on WAL_READ_LOCK
1a4f2 28 4b 29 2e 20 20 54 68 75 73 2c 20 74 68 65 20  (K).  Thus, the 
1a4f3 76 61 6c 75 65 20 6f 66 0d 0a 2a 2a 20 61 52 65  value of..** aRe
1a4f4 61 64 4d 61 72 6b 5b 4b 5d 20 63 61 6e 6e 6f 74  adMark[K] cannot
1a4f5 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74   changed while t
1a4f6 68 65 72 65 20 69 73 20 61 20 72 65 61 64 65 72  here is a reader
1a4f7 20 69 73 20 75 73 69 6e 67 20 74 68 61 74 20 6d   is using that m
1a4f8 61 72 6b 0d 0a 2a 2a 20 73 69 6e 63 65 20 74 68  ark..** since th
1a4f9 65 20 72 65 61 64 65 72 20 77 69 6c 6c 20 62 65  e reader will be
1a4fa 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65   holding a share
1a4fb 64 20 6c 6f 63 6b 20 6f 6e 20 57 41 4c 5f 52 45  d lock on WAL_RE
1a4fc 41 44 5f 4c 4f 43 4b 28 4b 29 2e 0d 0a 2a 2a 0d  AD_LOCK(K)...**.
1a4fd 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 70 6f 69  .** The checkpoi
1a4fe 6e 74 65 72 20 6d 61 79 20 6f 6e 6c 79 20 74 72  nter may only tr
1a4ff 61 6e 73 66 65 72 20 66 72 61 6d 65 73 20 66 72  ansfer frames fr
1a500 6f 6d 20 57 41 4c 20 74 6f 20 64 61 74 61 62 61  om WAL to databa
1a501 73 65 20 77 68 65 72 65 0d 0a 2a 2a 20 74 68 65  se where..** the
1a502 20 66 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 61   frame numbers a
1a503 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  re less than or 
1a504 65 71 75 61 6c 20 74 6f 20 65 76 65 72 79 20 61  equal to every a
1a505 52 65 61 64 4d 61 72 6b 5b 5d 20 74 68 61 74 20  ReadMark[] that 
1a506 69 73 0d 0a 2a 2a 20 69 6e 20 75 73 65 20 28 74  is..** in use (t
1a507 68 61 74 20 69 73 2c 20 65 76 65 72 79 20 61 52  hat is, every aR
1a508 65 61 64 4d 61 72 6b 5b 6a 5d 20 66 6f 72 20 77  eadMark[j] for w
1a509 68 69 63 68 20 74 68 65 72 65 20 69 73 20 61 20  hich there is a 
1a50a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0d 0a 2a  corresponding..*
1a50b 2a 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28  * WAL_READ_LOCK(
1a50c 6a 29 29 2e 20 20 4e 65 77 20 72 65 61 64 65 72  j)).  New reader
1a50d 73 20 28 75 73 75 61 6c 6c 79 29 20 70 69 63 6b  s (usually) pick
1a50e 20 74 68 65 20 61 52 65 61 64 4d 61 72 6b 5b 5d   the aReadMark[]
1a50f 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20 6c 61   with the..** la
1a510 72 67 65 73 74 20 76 61 6c 75 65 20 61 6e 64 20  rgest value and 
1a511 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 61 6e  will increase an
1a512 20 75 6e 75 73 65 64 20 61 52 65 61 64 4d 61 72   unused aReadMar
1a513 6b 5b 5d 20 74 6f 20 6d 78 46 72 61 6d 65 20 69  k[] to mxFrame i
1a514 66 20 74 68 65 72 65 0d 0a 2a 2a 20 69 73 20 6e  f there..** is n
1a515 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 20 61 52  ot already an aR
1a516 65 61 64 4d 61 72 6b 5b 5d 20 65 71 75 61 6c 20  eadMark[] equal 
1a517 74 6f 20 6d 78 46 72 61 6d 65 2e 20 20 54 68 65  to mxFrame.  The
1a518 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68   exception to th
1a519 65 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 73  e..** previous s
1a51a 65 6e 74 65 6e 63 65 20 69 73 20 77 68 65 6e 20  entence is when 
1a51b 6e 42 61 63 6b 66 69 6c 6c 20 65 71 75 61 6c 73  nBackfill equals
1a51c 20 6d 78 46 72 61 6d 65 20 28 6d 65 61 6e 69 6e   mxFrame (meanin
1a51d 67 20 74 68 61 74 20 65 76 65 72 79 74 68 69 6e  g that everythin
1a51e 67 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c  g..** in the WAL
1a51f 20 68 61 73 20 62 65 65 6e 20 62 61 63 6b 66 69   has been backfi
1a520 6c 6c 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61  lled into the da
1a521 74 61 62 61 73 65 29 20 74 68 65 6e 20 6e 65 77  tabase) then new
1a522 20 72 65 61 64 65 72 73 0d 0a 2a 2a 20 77 69 6c   readers..** wil
1a523 6c 20 63 68 6f 6f 73 65 20 61 52 65 61 64 4d 61  l choose aReadMa
1a524 72 6b 5b 30 5d 20 77 68 69 63 68 20 68 61 73 20  rk[0] which has 
1a525 76 61 6c 75 65 20 30 20 61 6e 64 20 68 65 6e 63  value 0 and henc
1a526 65 20 73 75 63 68 20 72 65 61 64 65 72 20 77 69  e such reader wi
1a527 6c 6c 0d 0a 2a 2a 20 67 65 74 20 61 6c 6c 20 74  ll..** get all t
1a528 68 65 69 72 20 61 6c 6c 20 63 6f 6e 74 65 6e 74  heir all content
1a529 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74   directly from t
1a52a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1a52b 20 61 6e 64 20 69 67 6e 6f 72 65 20 0d 0a 2a 2a   and ignore ..**
1a52c 20 74 68 65 20 57 41 4c 2e 0d 0a 2a 2a 0d 0a 2a   the WAL...**..*
1a52d 2a 20 57 72 69 74 65 72 73 20 6e 6f 72 6d 61 6c  * Writers normal
1a52e 6c 79 20 61 70 70 65 6e 64 20 6e 65 77 20 66 72  ly append new fr
1a52f 61 6d 65 73 20 74 6f 20 74 68 65 20 65 6e 64 20  ames to the end 
1a530 6f 66 20 74 68 65 20 57 41 4c 2e 20 20 48 6f 77  of the WAL.  How
1a531 65 76 65 72 2c 0d 0a 2a 2a 20 69 66 20 6e 42 61  ever,..** if nBa
1a532 63 6b 66 69 6c 6c 20 65 71 75 61 6c 73 20 6d 78  ckfill equals mx
1a533 46 72 61 6d 65 20 28 6d 65 61 6e 69 6e 67 20 74  Frame (meaning t
1a534 68 61 74 20 61 6c 6c 20 57 41 4c 20 63 6f 6e 74  hat all WAL cont
1a535 65 6e 74 20 68 61 73 20 62 65 65 6e 0d 0a 2a 2a  ent has been..**
1a536 20 77 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e   written back in
1a537 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 29  to the database)
1a538 20 61 6e 64 20 69 66 20 6e 6f 20 72 65 61 64 65   and if no reade
1a539 72 73 20 61 72 65 20 75 73 69 6e 67 20 74 68 65  rs are using the
1a53a 20 57 41 4c 0d 0a 2a 2a 20 28 69 6e 20 6f 74 68   WAL..** (in oth
1a53b 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65  er words, if the
1a53c 72 65 20 61 72 65 20 6e 6f 20 57 41 4c 5f 52 45  re are no WAL_RE
1a53d 41 44 5f 4c 4f 43 4b 28 69 29 20 77 68 65 72 65  AD_LOCK(i) where
1a53e 20 69 3e 30 29 20 74 68 65 6e 0d 0a 2a 2a 20 74   i>0) then..** t
1a53f 68 65 20 77 72 69 74 65 72 20 77 69 6c 6c 20 66  he writer will f
1a540 69 72 73 74 20 22 72 65 73 65 74 22 20 74 68 65  irst "reset" the
1a541 20 57 41 4c 20 62 61 63 6b 20 74 6f 20 74 68 65   WAL back to the
1a542 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 73   beginning and s
1a543 74 61 72 74 0d 0a 2a 2a 20 77 72 69 74 69 6e 67  tart..** writing
1a544 20 6e 65 77 20 63 6f 6e 74 65 6e 74 20 62 65 67   new content beg
1a545 69 6e 6e 69 6e 67 20 61 74 20 66 72 61 6d 65 20  inning at frame 
1a546 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 65 20 61 73  1...**..** We as
1a547 73 75 6d 65 20 74 68 61 74 20 33 32 2d 62 69 74  sume that 32-bit
1a548 20 6c 6f 61 64 73 20 61 72 65 20 61 74 6f 6d 69   loads are atomi
1a549 63 20 61 6e 64 20 73 6f 20 6e 6f 20 6c 6f 63 6b  c and so no lock
1a54a 73 20 61 72 65 20 6e 65 65 64 65 64 20 69 6e 0d  s are needed in.
1a54b 0a 2a 2a 20 6f 72 64 65 72 20 74 6f 20 72 65 61  .** order to rea
1a54c 64 20 66 72 6f 6d 20 61 6e 79 20 61 52 65 61 64  d from any aRead
1a54d 4d 61 72 6b 5b 5d 20 65 6e 74 72 69 65 73 2e 0d  Mark[] entries..
1a54e 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 57 61 6c 43  .*/..struct WalC
1a54f 6b 70 74 49 6e 66 6f 20 7b 0d 0a 20 20 75 33 32  kptInfo {..  u32
1a550 20 6e 42 61 63 6b 66 69 6c 6c 3b 20 20 20 20 20   nBackfill;     
1a551 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a552 4e 75 6d 62 65 72 20 6f 66 20 57 41 4c 20 66 72  Number of WAL fr
1a553 61 6d 65 73 20 62 61 63 6b 66 69 6c 6c 65 64 20  ames backfilled 
1a554 69 6e 74 6f 20 44 42 20 2a 2f 0d 0a 20 20 75 33  into DB */..  u3
1a555 32 20 61 52 65 61 64 4d 61 72 6b 5b 57 41 4c 5f  2 aReadMark[WAL_
1a556 4e 52 45 41 44 45 52 5d 3b 20 20 20 20 20 2f 2a  NREADER];     /*
1a557 20 52 65 61 64 65 72 20 6d 61 72 6b 73 20 2a 2f   Reader marks */
1a558 0d 0a 7d 3b 0d 0a 23 64 65 66 69 6e 65 20 52 45  ..};..#define RE
1a559 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 20  ADMARK_NOT_USED 
1a55a 20 30 78 66 66 66 66 66 66 66 66 0d 0a 0d 0a 0d   0xffffffff.....
1a55b 0a 2f 2a 20 41 20 62 6c 6f 63 6b 20 6f 66 20 57  ./* A block of W
1a55c 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 45 53  ALINDEX_LOCK_RES
1a55d 45 52 56 45 44 20 62 79 74 65 73 20 62 65 67 69  ERVED bytes begi
1a55e 6e 6e 69 6e 67 20 61 74 0d 0a 2a 2a 20 57 41 4c  nning at..** WAL
1a55f 49 4e 44 45 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45  INDEX_LOCK_OFFSE
1a560 54 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f  T is reserved fo
1a561 72 20 6c 6f 63 6b 73 2e 20 53 69 6e 63 65 20 73  r locks. Since s
1a562 6f 6d 65 20 73 79 73 74 65 6d 73 0d 0a 2a 2a 20  ome systems..** 
1a563 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 6d 61 6e  only support man
1a564 64 61 74 6f 72 79 20 66 69 6c 65 2d 6c 6f 63 6b  datory file-lock
1a565 73 2c 20 77 65 20 64 6f 20 6e 6f 74 20 72 65 61  s, we do not rea
1a566 64 20 6f 72 20 77 72 69 74 65 20 64 61 74 61 0d  d or write data.
1a567 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 67  .** from the reg
1a568 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ion of the file 
1a569 6f 6e 20 77 68 69 63 68 20 6c 6f 63 6b 73 20 61  on which locks a
1a56a 72 65 20 61 70 70 6c 69 65 64 2e 0d 0a 2a 2f 0d  re applied...*/.
1a56b 0a 23 64 65 66 69 6e 65 20 57 41 4c 49 4e 44 45  .#define WALINDE
1a56c 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45 54 20 20 20  X_LOCK_OFFSET   
1a56d 28 73 69 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78  (sizeof(WalIndex
1a56e 48 64 72 29 2a 32 20 2b 20 73 69 7a 65 6f 66 28  Hdr)*2 + sizeof(
1a56f 57 61 6c 43 6b 70 74 49 6e 66 6f 29 29 0d 0a 23  WalCkptInfo))..#
1a570 64 65 66 69 6e 65 20 57 41 4c 49 4e 44 45 58 5f  define WALINDEX_
1a571 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 20 31 36  LOCK_RESERVED 16
1a572 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 49 4e 44  ..#define WALIND
1a573 45 58 5f 48 44 52 5f 53 49 5a 45 20 20 20 20 20  EX_HDR_SIZE     
1a574 20 28 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f   (WALINDEX_LOCK_
1a575 4f 46 46 53 45 54 2b 57 41 4c 49 4e 44 45 58 5f  OFFSET+WALINDEX_
1a576 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 29 0d 0a  LOCK_RESERVED)..
1a577 0d 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 68 65 61  ../* Size of hea
1a578 64 65 72 20 62 65 66 6f 72 65 20 65 61 63 68 20  der before each 
1a579 66 72 61 6d 65 20 69 6e 20 77 61 6c 20 2a 2f 0d  frame in wal */.
1a57a 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 46 52 41  .#define WAL_FRA
1a57b 4d 45 5f 48 44 52 53 49 5a 45 20 32 34 0d 0a 0d  ME_HDRSIZE 24...
1a57c 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 77 72 69 74  ./* Size of writ
1a57d 65 20 61 68 65 61 64 20 6c 6f 67 20 68 65 61 64  e ahead log head
1a57e 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 68  er, including ch
1a57f 65 63 6b 73 75 6d 2e 20 2a 2f 0d 0a 2f 2a 20 23  ecksum. */../* #
1a580 64 65 66 69 6e 65 20 57 41 4c 5f 48 44 52 53 49  define WAL_HDRSI
1a581 5a 45 20 32 34 20 2a 2f 0d 0a 23 64 65 66 69 6e  ZE 24 */..#defin
1a582 65 20 57 41 4c 5f 48 44 52 53 49 5a 45 20 33 32  e WAL_HDRSIZE 32
1a583 0d 0a 0d 0a 2f 2a 20 57 41 4c 20 6d 61 67 69 63  ..../* WAL magic
1a584 20 76 61 6c 75 65 2e 20 45 69 74 68 65 72 20 74   value. Either t
1a585 68 69 73 20 76 61 6c 75 65 2c 20 6f 72 20 74 68  his value, or th
1a586 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 69 74  e same value wit
1a587 68 20 74 68 65 20 6c 65 61 73 74 0d 0a 2a 2a 20  h the least..** 
1a588 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20  significant bit 
1a589 61 6c 73 6f 20 73 65 74 20 28 57 41 4c 5f 4d 41  also set (WAL_MA
1a58a 47 49 43 20 7c 20 30 78 30 30 30 30 30 30 30 31  GIC | 0x00000001
1a58b 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 33  ) is stored in 3
1a58c 32 2d 62 69 74 0d 0a 2a 2a 20 62 69 67 2d 65 6e  2-bit..** big-en
1a58d 64 69 61 6e 20 66 6f 72 6d 61 74 20 69 6e 20 74  dian format in t
1a58e 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73  he first 4 bytes
1a58f 20 6f 66 20 61 20 57 41 4c 20 66 69 6c 65 2e 0d   of a WAL file..
1a590 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 4c  .**..** If the L
1a591 53 42 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  SB is set, then 
1a592 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 66 6f  the checksums fo
1a593 72 20 65 61 63 68 20 66 72 61 6d 65 20 77 69 74  r each frame wit
1a594 68 69 6e 20 74 68 65 20 57 41 4c 0d 0a 2a 2a 20  hin the WAL..** 
1a595 66 69 6c 65 20 61 72 65 20 63 61 6c 63 75 6c 61  file are calcula
1a596 74 65 64 20 62 79 20 74 72 65 61 74 69 6e 67 20  ted by treating 
1a597 61 6c 6c 20 64 61 74 61 20 61 73 20 61 6e 20 61  all data as an a
1a598 72 72 61 79 20 6f 66 20 33 32 2d 62 69 74 20 0d  rray of 32-bit .
1a599 0a 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e 20 77  .** big-endian w
1a59a 6f 72 64 73 2e 20 4f 74 68 65 72 77 69 73 65 2c  ords. Otherwise,
1a59b 20 74 68 65 79 20 61 72 65 20 63 61 6c 63 75 6c   they are calcul
1a59c 61 74 65 64 20 62 79 20 69 6e 74 65 72 70 72 65  ated by interpre
1a59d 74 69 6e 67 20 0d 0a 2a 2a 20 61 6c 6c 20 64 61  ting ..** all da
1a59e 74 61 20 61 73 20 33 32 2d 62 69 74 20 6c 69 74  ta as 32-bit lit
1a59f 74 6c 65 2d 65 6e 64 69 61 6e 20 77 6f 72 64 73  tle-endian words
1a5a0 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57  ...*/..#define W
1a5a1 41 4c 5f 4d 41 47 49 43 20 30 78 33 37 37 66 30  AL_MAGIC 0x377f0
1a5a2 36 38 32 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  682..../*..** Re
1a5a3 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74 20  turn the offset 
1a5a4 6f 66 20 66 72 61 6d 65 20 69 46 72 61 6d 65 20  of frame iFrame 
1a5a5 69 6e 20 74 68 65 20 77 72 69 74 65 2d 61 68 65  in the write-ahe
1a5a6 61 64 20 6c 6f 67 20 66 69 6c 65 2c 20 0d 0a 2a  ad log file, ..*
1a5a7 2a 20 61 73 73 75 6d 69 6e 67 20 61 20 64 61 74  * assuming a dat
1a5a8 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20  abase page size 
1a5a9 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 2e  of szPage bytes.
1a5aa 20 54 68 65 20 6f 66 66 73 65 74 20 72 65 74 75   The offset retu
1a5ab 72 6e 65 64 0d 0a 2a 2a 20 69 73 20 74 6f 20 74  rned..** is to t
1a5ac 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
1a5ad 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20  write-ahead log 
1a5ae 66 72 61 6d 65 2d 68 65 61 64 65 72 2e 0d 0a 2a  frame-header...*
1a5af 2f 0d 0a 23 64 65 66 69 6e 65 20 77 61 6c 46 72  /..#define walFr
1a5b0 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65  ameOffset(iFrame
1a5b1 2c 20 73 7a 50 61 67 65 29 20 28 20 20 20 20 20  , szPage) (     
1a5b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5b3 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 57            \..  W
1a5b4 41 4c 5f 48 44 52 53 49 5a 45 20 2b 20 28 28 69  AL_HDRSIZE + ((i
1a5b5 46 72 61 6d 65 29 2d 31 29 2a 28 69 36 34 29 28  Frame)-1)*(i64)(
1a5b6 28 73 7a 50 61 67 65 29 2b 57 41 4c 5f 46 52 41  (szPage)+WAL_FRA
1a5b7 4d 45 5f 48 44 52 53 49 5a 45 29 20 20 20 20 20  ME_HDRSIZE)     
1a5b8 20 20 20 20 5c 0d 0a 29 0d 0a 0d 0a 2f 2a 0d 0a      \..)..../*..
1a5b9 2a 2a 20 41 6e 20 6f 70 65 6e 20 77 72 69 74 65  ** An open write
1a5ba 2d 61 68 65 61 64 20 6c 6f 67 20 66 69 6c 65 20  -ahead log file 
1a5bb 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  is represented b
1a5bc 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
1a5bd 20 74 68 65 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69   the..** followi
1a5be 6e 67 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a  ng object...*/..
1a5bf 73 74 72 75 63 74 20 57 61 6c 20 7b 0d 0a 20 20  struct Wal {..  
1a5c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1a5c1 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  s;         /* Th
1a5c2 65 20 56 46 53 20 75 73 65 64 20 74 6f 20 63 72  e VFS used to cr
1a5c3 65 61 74 65 20 70 44 62 46 64 20 2a 2f 0d 0a 20  eate pDbFd */.. 
1a5c4 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1a5c5 44 62 46 64 3b 20 20 20 20 20 20 20 2f 2a 20 46  DbFd;       /* F
1a5c6 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74  ile handle for t
1a5c7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1a5c8 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66   */..  sqlite3_f
1a5c9 69 6c 65 20 2a 70 57 61 6c 46 64 3b 20 20 20 20  ile *pWalFd;    
1a5ca 20 20 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65    /* File handle
1a5cb 20 66 6f 72 20 57 41 4c 20 66 69 6c 65 20 2a 2f   for WAL file */
1a5cc 0d 0a 20 20 75 33 32 20 69 43 61 6c 6c 62 61 63  ..  u32 iCallbac
1a5cd 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
1a5ce 2a 20 56 61 6c 75 65 20 74 6f 20 70 61 73 73 20  * Value to pass 
1a5cf 74 6f 20 6c 6f 67 20 63 61 6c 6c 62 61 63 6b 20  to log callback 
1a5d0 28 6f 72 20 30 29 20 2a 2f 0d 0a 20 20 69 36 34  (or 0) */..  i64
1a5d1 20 6d 78 57 61 6c 53 69 7a 65 3b 20 20 20 20 20   mxWalSize;     
1a5d2 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 6e 63          /* Trunc
1a5d3 61 74 65 20 57 41 4c 20 74 6f 20 74 68 69 73 20  ate WAL to this 
1a5d4 73 69 7a 65 20 75 70 6f 6e 20 72 65 73 65 74 20  size upon reset 
1a5d5 2a 2f 0d 0a 20 20 69 6e 74 20 6e 57 69 44 61 74  */..  int nWiDat
1a5d6 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
1a5d7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61   /* Size of arra
1a5d8 79 20 61 70 57 69 44 61 74 61 20 2a 2f 0d 0a 20  y apWiData */.. 
1a5d9 20 69 6e 74 20 73 7a 46 69 72 73 74 42 6c 6f 63   int szFirstBloc
1a5da 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  k;          /* S
1a5db 69 7a 65 20 6f 66 20 66 69 72 73 74 20 62 6c 6f  ize of first blo
1a5dc 63 6b 20 77 72 69 74 74 65 6e 20 74 6f 20 57 41  ck written to WA
1a5dd 4c 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 76 6f 6c  L file */..  vol
1a5de 61 74 69 6c 65 20 75 33 32 20 2a 2a 61 70 57 69  atile u32 **apWi
1a5df 44 61 74 61 3b 20 20 20 2f 2a 20 50 6f 69 6e 74  Data;   /* Point
1a5e0 65 72 20 74 6f 20 77 61 6c 2d 69 6e 64 65 78 20  er to wal-index 
1a5e1 63 6f 6e 74 65 6e 74 20 69 6e 20 6d 65 6d 6f 72  content in memor
1a5e2 79 20 2a 2f 0d 0a 20 20 75 33 32 20 73 7a 50 61  y */..  u32 szPa
1a5e3 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
1a5e4 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70     /* Database p
1a5e5 61 67 65 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 69  age size */..  i
1a5e6 31 36 20 72 65 61 64 4c 6f 63 6b 3b 20 20 20 20  16 readLock;    
1a5e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69            /* Whi
1a5e8 63 68 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20  ch read lock is 
1a5e9 62 65 69 6e 67 20 68 65 6c 64 2e 20 20 2d 31 20  being held.  -1 
1a5ea 66 6f 72 20 6e 6f 6e 65 20 2a 2f 0d 0a 20 20 75  for none */..  u
1a5eb 38 20 73 79 6e 63 46 6c 61 67 73 3b 20 20 20 20  8 syncFlags;    
1a5ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1a5ed 67 73 20 74 6f 20 75 73 65 20 74 6f 20 73 79 6e  gs to use to syn
1a5ee 63 20 68 65 61 64 65 72 20 77 72 69 74 65 73 20  c header writes 
1a5ef 2a 2f 0d 0a 20 20 75 38 20 65 78 63 6c 75 73 69  */..  u8 exclusi
1a5f0 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  veMode;         
1a5f1 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20   /* Non-zero if 
1a5f2 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e  connection is in
1a5f3 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20   exclusive mode 
1a5f4 2a 2f 0d 0a 20 20 75 38 20 77 72 69 74 65 4c 6f  */..  u8 writeLo
1a5f5 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
1a5f6 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61   /* True if in a
1a5f7 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1a5f8 6f 6e 20 2a 2f 0d 0a 20 20 75 38 20 63 6b 70 74  on */..  u8 ckpt
1a5f9 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Lock;           
1a5fa 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68      /* True if h
1a5fb 6f 6c 64 69 6e 67 20 61 20 63 68 65 63 6b 70 6f  olding a checkpo
1a5fc 69 6e 74 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 75  int lock */..  u
1a5fd 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
1a5fe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 41 4c            /* WAL
1a5ff 5f 52 44 57 52 2c 20 57 41 4c 5f 52 44 4f 4e 4c  _RDWR, WAL_RDONL
1a600 59 2c 20 6f 72 20 57 41 4c 5f 53 48 4d 5f 52 44  Y, or WAL_SHM_RD
1a601 4f 4e 4c 59 20 2a 2f 0d 0a 20 20 75 38 20 74 72  ONLY */..  u8 tr
1a602 75 6e 63 61 74 65 4f 6e 43 6f 6d 6d 69 74 3b 20  uncateOnCommit; 
1a603 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
1a604 20 74 72 75 6e 63 61 74 65 20 57 41 4c 20 66 69   truncate WAL fi
1a605 6c 65 20 6f 6e 20 63 6f 6d 6d 69 74 20 2a 2f 0d  le on commit */.
1a606 0a 20 20 75 38 20 73 79 6e 63 48 65 61 64 65 72  .  u8 syncHeader
1a607 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1a608 20 46 73 79 6e 63 20 74 68 65 20 57 41 4c 20 68   Fsync the WAL h
1a609 65 61 64 65 72 20 69 66 20 74 72 75 65 20 2a 2f  eader if true */
1a60a 0d 0a 20 20 75 38 20 70 61 64 54 6f 53 65 63 74  ..  u8 padToSect
1a60b 6f 72 42 6f 75 6e 64 61 72 79 3b 20 20 20 20 2f  orBoundary;    /
1a60c 2a 20 50 61 64 20 74 72 61 6e 73 61 63 74 69 6f  * Pad transactio
1a60d 6e 73 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65  ns out to the ne
1a60e 78 74 20 73 65 63 74 6f 72 20 2a 2f 0d 0a 20 20  xt sector */..  
1a60f 57 61 6c 49 6e 64 65 78 48 64 72 20 68 64 72 3b  WalIndexHdr hdr;
1a610 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61             /* Wa
1a611 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 66  l-index header f
1a612 6f 72 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  or current trans
1a613 61 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e  action */..  con
1a614 73 74 20 63 68 61 72 20 2a 7a 57 61 6c 4e 61 6d  st char *zWalNam
1a615 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  e;      /* Name 
1a616 6f 66 20 57 41 4c 20 66 69 6c 65 20 2a 2f 0d 0a  of WAL file */..
1a617 20 20 75 33 32 20 6e 43 6b 70 74 3b 20 20 20 20    u32 nCkpt;    
1a618 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a619 43 68 65 63 6b 70 6f 69 6e 74 20 73 65 71 75 65  Checkpoint seque
1a61a 6e 63 65 20 63 6f 75 6e 74 65 72 20 69 6e 20 74  nce counter in t
1a61b 68 65 20 77 61 6c 2d 68 65 61 64 65 72 20 2a 2f  he wal-header */
1a61c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1a61d 44 45 42 55 47 0d 0a 20 20 75 38 20 6c 6f 63 6b  DEBUG..  u8 lock
1a61e 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20  Error;          
1a61f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
1a620 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72 20 68   locking error h
1a621 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0d 0a  as occurred */..
1a622 23 65 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  #endif..};..../*
1a623 0d 0a 2a 2a 20 43 61 6e 64 69 64 61 74 65 20 76  ..** Candidate v
1a624 61 6c 75 65 73 20 66 6f 72 20 57 61 6c 2e 65 78  alues for Wal.ex
1a625 63 6c 75 73 69 76 65 4d 6f 64 65 2e 0d 0a 2a 2f  clusiveMode...*/
1a626 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 4e 4f  ..#define WAL_NO
1a627 52 4d 41 4c 5f 4d 4f 44 45 20 20 20 20 20 30 0d  RMAL_MODE     0.
1a628 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 45 58 43  .#define WAL_EXC
1a629 4c 55 53 49 56 45 5f 4d 4f 44 45 20 20 31 20 20  LUSIVE_MODE  1  
1a62a 20 20 20 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c     ..#define WAL
1a62b 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45  _HEAPMEMORY_MODE
1a62c 20 32 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f 73   2..../*..** Pos
1a62d 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
1a62e 20 57 41 4c 2e 72 65 61 64 4f 6e 6c 79 0d 0a 2a   WAL.readOnly..*
1a62f 2f 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 52  /..#define WAL_R
1a630 44 57 52 20 20 20 20 20 20 20 20 30 20 20 20 20  DWR        0    
1a631 2f 2a 20 4e 6f 72 6d 61 6c 20 72 65 61 64 2f 77  /* Normal read/w
1a632 72 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  rite connection 
1a633 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f  */..#define WAL_
1a634 52 44 4f 4e 4c 59 20 20 20 20 20 20 31 20 20 20  RDONLY      1   
1a635 20 2f 2a 20 54 68 65 20 57 41 4c 20 66 69 6c 65   /* The WAL file
1a636 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0d   is readonly */.
1a637 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 48 4d  .#define WAL_SHM
1a638 5f 52 44 4f 4e 4c 59 20 20 32 20 20 20 20 2f 2a  _RDONLY  2    /*
1a639 20 54 68 65 20 53 48 4d 20 66 69 6c 65 20 69 73   The SHM file is
1a63a 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0d 0a 0d 0a   readonly */....
1a63b 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20 70 61 67 65  /*..** Each page
1a63c 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65   of the wal-inde
1a63d 78 20 6d 61 70 70 69 6e 67 20 63 6f 6e 74 61 69  x mapping contai
1a63e 6e 73 20 61 20 68 61 73 68 2d 74 61 62 6c 65 20  ns a hash-table 
1a63f 6d 61 64 65 20 75 70 20 6f 66 0d 0a 2a 2a 20 61  made up of..** a
1a640 6e 20 61 72 72 61 79 20 6f 66 20 48 41 53 48 54  n array of HASHT
1a641 41 42 4c 45 5f 4e 53 4c 4f 54 20 65 6c 65 6d 65  ABLE_NSLOT eleme
1a642 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nts of the follo
1a643 77 69 6e 67 20 74 79 70 65 2e 0d 0a 2a 2f 0d 0a  wing type...*/..
1a644 74 79 70 65 64 65 66 20 75 31 36 20 68 74 5f 73  typedef u16 ht_s
1a645 6c 6f 74 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  lot;..../*..** T
1a646 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
1a647 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1a648 6e 74 20 61 6e 20 69 74 65 72 61 74 6f 72 20 74  nt an iterator t
1a649 68 61 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  hat loops throug
1a64a 68 0d 0a 2a 2a 20 61 6c 6c 20 66 72 61 6d 65 73  h..** all frames
1a64b 20 69 6e 20 74 68 65 20 57 41 4c 20 69 6e 20 64   in the WAL in d
1a64c 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 72 64  atabase page ord
1a64d 65 72 2e 20 57 68 65 72 65 20 74 77 6f 20 6f 72  er. Where two or
1a64e 20 6d 6f 72 65 20 66 72 61 6d 65 73 0d 0a 2a 2a   more frames..**
1a64f 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74   correspond to t
1a650 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
1a651 20 70 61 67 65 2c 20 74 68 65 20 69 74 65 72 61   page, the itera
1a652 74 6f 72 20 76 69 73 69 74 73 20 6f 6e 6c 79 20  tor visits only 
1a653 74 68 65 20 0d 0a 2a 2a 20 66 72 61 6d 65 20 6d  the ..** frame m
1a654 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69  ost recently wri
1a655 74 74 65 6e 20 74 6f 20 74 68 65 20 57 41 4c 20  tten to the WAL 
1a656 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (in other words,
1a657 20 74 68 65 20 66 72 61 6d 65 20 77 69 74 68 0d   the frame with.
1a658 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20  .** the largest 
1a659 69 6e 64 65 78 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  index)...**..** 
1a65a 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  The internals of
1a65b 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
1a65c 61 72 65 20 6f 6e 6c 79 20 61 63 63 65 73 73 65  are only accesse
1a65d 64 20 62 79 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  d by:..**..**   
1a65e 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69 74 28  walIteratorInit(
1a65f 29 20 2d 20 43 72 65 61 74 65 20 61 20 6e 65 77  ) - Create a new
1a660 20 69 74 65 72 61 74 6f 72 2c 0d 0a 2a 2a 20 20   iterator,..**  
1a661 20 77 61 6c 49 74 65 72 61 74 6f 72 4e 65 78 74   walIteratorNext
1a662 28 29 20 2d 20 53 74 65 70 20 61 6e 20 69 74 65  () - Step an ite
1a663 72 61 74 6f 72 2c 0d 0a 2a 2a 20 20 20 77 61 6c  rator,..**   wal
1a664 49 74 65 72 61 74 6f 72 46 72 65 65 28 29 20 2d  IteratorFree() -
1a665 20 46 72 65 65 20 61 6e 20 69 74 65 72 61 74 6f   Free an iterato
1a666 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  r...**..** This 
1a667 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73  functionality is
1a668 20 75 73 65 64 20 62 79 20 74 68 65 20 63 68 65   used by the che
1a669 63 6b 70 6f 69 6e 74 20 63 6f 64 65 20 28 73 65  ckpoint code (se
1a66a 65 20 77 61 6c 43 68 65 63 6b 70 6f 69 6e 74 28  e walCheckpoint(
1a66b 29 29 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  ))...*/..struct 
1a66c 57 61 6c 49 74 65 72 61 74 6f 72 20 7b 0d 0a 20  WalIterator {.. 
1a66d 20 69 6e 74 20 69 50 72 69 6f 72 3b 20 20 20 20   int iPrior;    
1a66e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a66f 20 2f 2a 20 4c 61 73 74 20 72 65 73 75 6c 74 20   /* Last result 
1a670 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 74 68  returned from th
1a671 65 20 69 74 65 72 61 74 6f 72 20 2a 2f 0d 0a 20  e iterator */.. 
1a672 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 3b 20 20   int nSegment;  
1a673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a674 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
1a675 74 72 69 65 73 20 69 6e 20 61 53 65 67 6d 65 6e  tries in aSegmen
1a676 74 5b 5d 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  t[] */..  struct
1a677 20 57 61 6c 53 65 67 6d 65 6e 74 20 7b 0d 0a 20   WalSegment {.. 
1a678 20 20 20 69 6e 74 20 69 4e 65 78 74 3b 20 20 20     int iNext;   
1a679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a67a 20 2f 2a 20 4e 65 78 74 20 73 6c 6f 74 20 69 6e   /* Next slot in
1a67b 20 61 49 6e 64 65 78 5b 5d 20 6e 6f 74 20 79 65   aIndex[] not ye
1a67c 74 20 72 65 74 75 72 6e 65 64 20 2a 2f 0d 0a 20  t returned */.. 
1a67d 20 20 20 68 74 5f 73 6c 6f 74 20 2a 61 49 6e 64     ht_slot *aInd
1a67e 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
1a67f 20 2f 2a 20 69 30 2c 20 69 31 2c 20 69 32 2e 2e   /* i0, i1, i2..
1a680 2e 20 73 75 63 68 20 74 68 61 74 20 61 50 67 6e  . such that aPgn
1a681 6f 5b 69 4e 5d 20 61 73 63 65 6e 64 20 2a 2f 0d  o[iN] ascend */.
1a682 0a 20 20 20 20 75 33 32 20 2a 61 50 67 6e 6f 3b  .    u32 *aPgno;
1a683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a684 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 70     /* Array of p
1a685 61 67 65 20 6e 75 6d 62 65 72 73 2e 20 2a 2f 0d  age numbers. */.
1a686 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b  .    int nEntry;
1a687 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a688 20 20 20 2f 2a 20 4e 72 2e 20 6f 66 20 65 6e 74     /* Nr. of ent
1a689 72 69 65 73 20 69 6e 20 61 50 67 6e 6f 5b 5d 20  ries in aPgno[] 
1a68a 61 6e 64 20 61 49 6e 64 65 78 5b 5d 20 2a 2f 0d  and aIndex[] */.
1a68b 0a 20 20 20 20 69 6e 74 20 69 5a 65 72 6f 3b 20  .    int iZero; 
1a68c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a68d 20 20 20 2f 2a 20 46 72 61 6d 65 20 6e 75 6d 62     /* Frame numb
1a68e 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  er associated wi
1a68f 74 68 20 61 50 67 6e 6f 5b 30 5d 20 2a 2f 0d 0a  th aPgno[0] */..
1a690 20 20 7d 20 61 53 65 67 6d 65 6e 74 5b 31 5d 3b    } aSegment[1];
1a691 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a692 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 76 65    /* One for eve
1a693 72 79 20 33 32 4b 42 20 70 61 67 65 20 69 6e 20  ry 32KB page in 
1a694 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 2a 2f  the wal-index */
1a695 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44  ..};..../*..** D
1a696 65 66 69 6e 65 20 74 68 65 20 70 61 72 61 6d 65  efine the parame
1a697 74 65 72 73 20 6f 66 20 74 68 65 20 68 61 73 68  ters of the hash
1a698 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 77   tables in the w
1a699 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 2e 20 54  al-index file. T
1a69a 68 65 72 65 0d 0a 2a 2a 20 69 73 20 61 20 68 61  here..** is a ha
1a69b 73 68 2d 74 61 62 6c 65 20 66 6f 6c 6c 6f 77 69  sh-table followi
1a69c 6e 67 20 65 76 65 72 79 20 48 41 53 48 54 41 42  ng every HASHTAB
1a69d 4c 45 5f 4e 50 41 47 45 20 70 61 67 65 20 6e 75  LE_NPAGE page nu
1a69e 6d 62 65 72 73 20 69 6e 20 74 68 65 0d 0a 2a 2a  mbers in the..**
1a69f 20 77 61 6c 2d 69 6e 64 65 78 2e 0d 0a 2a 2a 0d   wal-index...**.
1a6a0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 61 6e 79  .** Changing any
1a6a1 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61   of these consta
1a6a2 6e 74 73 20 77 69 6c 6c 20 61 6c 74 65 72 20 74  nts will alter t
1a6a3 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 66 6f 72  he wal-index for
1a6a4 6d 61 74 20 61 6e 64 0d 0a 2a 2a 20 63 72 65 61  mat and..** crea
1a6a5 74 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69  te incompatibili
1a6a6 74 69 65 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ties...*/..#defi
1a6a7 6e 65 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41  ne HASHTABLE_NPA
1a6a8 47 45 20 20 20 20 20 20 34 30 39 36 20 20 20 20  GE      4096    
1a6a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a6aa 4d 75 73 74 20 62 65 20 70 6f 77 65 72 20 6f 66  Must be power of
1a6ab 20 32 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 48   2 */..#define H
1a6ac 41 53 48 54 41 42 4c 45 5f 48 41 53 48 5f 31 20  ASHTABLE_HASH_1 
1a6ad 20 20 20 20 33 38 33 20 20 20 20 20 20 20 20 20      383         
1a6ae 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75           /* Shou
1a6af 6c 64 20 62 65 20 70 72 69 6d 65 20 2a 2f 0d 0a  ld be prime */..
1a6b0 23 64 65 66 69 6e 65 20 48 41 53 48 54 41 42 4c  #define HASHTABL
1a6b1 45 5f 4e 53 4c 4f 54 20 20 20 20 20 20 28 48 41  E_NSLOT      (HA
1a6b2 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 2a 32 29  SHTABLE_NPAGE*2)
1a6b3 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 61 20 70    /* Must be a p
1a6b4 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0d 0a 0d 0a  ower of 2 */....
1a6b5 2f 2a 20 0d 0a 2a 2a 20 54 68 65 20 62 6c 6f 63  /* ..** The bloc
1a6b6 6b 20 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 72  k of page number
1a6b7 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
1a6b8 68 20 74 68 65 20 66 69 72 73 74 20 68 61 73 68  h the first hash
1a6b9 2d 74 61 62 6c 65 20 69 6e 20 61 0d 0a 2a 2a 20  -table in a..** 
1a6ba 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73 6d 61  wal-index is sma
1a6bb 6c 6c 65 72 20 74 68 61 6e 20 75 73 75 61 6c 2e  ller than usual.
1a6bc 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74   This is so that
1a6bd 20 74 68 65 72 65 20 69 73 20 61 20 63 6f 6d 70   there is a comp
1a6be 6c 65 74 65 0d 0a 2a 2a 20 68 61 73 68 2d 74 61  lete..** hash-ta
1a6bf 62 6c 65 20 6f 6e 20 65 61 63 68 20 61 6c 69 67  ble on each alig
1a6c0 6e 65 64 20 33 32 4b 42 20 70 61 67 65 20 6f 66  ned 32KB page of
1a6c1 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0d   the wal-index..
1a6c2 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 48 41 53  .*/..#define HAS
1a6c3 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45  HTABLE_NPAGE_ONE
1a6c4 20 20 28 48 41 53 48 54 41 42 4c 45 5f 4e 50 41    (HASHTABLE_NPA
1a6c5 47 45 20 2d 20 28 57 41 4c 49 4e 44 45 58 5f 48  GE - (WALINDEX_H
1a6c6 44 52 5f 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75  DR_SIZE/sizeof(u
1a6c7 33 32 29 29 29 0d 0a 0d 0a 2f 2a 20 54 68 65 20  32)))..../* The 
1a6c8 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 64 69 76  wal-index is div
1a6c9 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 20  ided into pages 
1a6ca 6f 66 20 57 41 4c 49 4e 44 45 58 5f 50 47 53 5a  of WALINDEX_PGSZ
1a6cb 20 62 79 74 65 73 20 65 61 63 68 2e 20 2a 2f 0d   bytes each. */.
1a6cc 0a 23 64 65 66 69 6e 65 20 57 41 4c 49 4e 44 45  .#define WALINDE
1a6cd 58 5f 50 47 53 5a 20 20 20 28 20 20 20 20 20 20  X_PGSZ   (      
1a6ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6d0 20 20 20 5c 0d 0a 20 20 20 20 73 69 7a 65 6f 66     \..    sizeof
1a6d1 28 68 74 5f 73 6c 6f 74 29 2a 48 41 53 48 54 41  (ht_slot)*HASHTA
1a6d2 42 4c 45 5f 4e 53 4c 4f 54 20 2b 20 48 41 53 48  BLE_NSLOT + HASH
1a6d3 54 41 42 4c 45 5f 4e 50 41 47 45 2a 73 69 7a 65  TABLE_NPAGE*size
1a6d4 6f 66 28 75 33 32 29 20 5c 0d 0a 29 0d 0a 0d 0a  of(u32) \..)....
1a6d5 2f 2a 0d 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20  /*..** Obtain a 
1a6d6 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69  pointer to the i
1a6d7 50 61 67 65 27 74 68 20 70 61 67 65 20 6f 66 20  Page'th page of 
1a6d8 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 20 54  the wal-index. T
1a6d9 68 65 20 77 61 6c 2d 69 6e 64 65 78 0d 0a 2a 2a  he wal-index..**
1a6da 20 69 73 20 62 72 6f 6b 65 6e 20 69 6e 74 6f 20   is broken into 
1a6db 70 61 67 65 73 20 6f 66 20 57 41 4c 49 4e 44 45  pages of WALINDE
1a6dc 58 5f 50 47 53 5a 20 62 79 74 65 73 2e 20 57 61  X_PGSZ bytes. Wa
1a6dd 6c 2d 69 6e 64 65 78 20 70 61 67 65 73 20 61 72  l-index pages ar
1a6de 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 65 64 20 66  e..** numbered f
1a6df 72 6f 6d 20 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a  rom zero...**..*
1a6e0 2a 20 49 66 20 74 68 69 73 20 63 61 6c 6c 20 69  * If this call i
1a6e1 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70  s successful, *p
1a6e2 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20  pPage is set to 
1a6e3 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 77 61 6c  point to the wal
1a6e4 2d 69 6e 64 65 78 0d 0a 2a 2a 20 70 61 67 65 20  -index..** page 
1a6e5 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  and SQLITE_OK is
1a6e6 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
1a6e7 20 65 72 72 6f 72 20 28 61 6e 20 4f 4f 4d 20 6f   error (an OOM o
1a6e8 72 20 56 46 53 20 65 72 72 6f 72 29 20 6f 63 63  r VFS error) occ
1a6e9 75 72 73 2c 0d 0a 2a 2a 20 74 68 65 6e 20 61 6e  urs,..** then an
1a6ea 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
1a6eb 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
1a6ec 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  nd *ppPage is se
1a6ed 74 20 74 6f 20 30 2e 0d 0a 2a 2f 0d 0a 73 74 61  t to 0...*/..sta
1a6ee 74 69 63 20 69 6e 74 20 77 61 6c 49 6e 64 65 78  tic int walIndex
1a6ef 50 61 67 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20  Page(Wal *pWal, 
1a6f0 69 6e 74 20 69 50 61 67 65 2c 20 76 6f 6c 61 74  int iPage, volat
1a6f1 69 6c 65 20 75 33 32 20 2a 2a 70 70 50 61 67 65  ile u32 **ppPage
1a6f2 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
1a6f3 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 2f  QLITE_OK;....  /
1a6f4 2a 20 45 6e 6c 61 72 67 65 20 74 68 65 20 70 57  * Enlarge the pW
1a6f5 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 5d 20 61  al->apWiData[] a
1a6f6 72 72 61 79 20 69 66 20 72 65 71 75 69 72 65 64  rray if required
1a6f7 20 2a 2f 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d   */..  if( pWal-
1a6f8 3e 6e 57 69 44 61 74 61 3c 3d 69 50 61 67 65 20  >nWiData<=iPage 
1a6f9 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  ){..    int nByt
1a6fa 65 20 3d 20 73 69 7a 65 6f 66 28 75 33 32 2a 29  e = sizeof(u32*)
1a6fb 2a 28 69 50 61 67 65 2b 31 29 3b 0d 0a 20 20 20  *(iPage+1);..   
1a6fc 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a 2a   volatile u32 **
1a6fd 61 70 4e 65 77 3b 0d 0a 20 20 20 20 61 70 4e 65  apNew;..    apNe
1a6fe 77 20 3d 20 28 76 6f 6c 61 74 69 6c 65 20 75 33  w = (volatile u3
1a6ff 32 20 2a 2a 29 73 71 6c 69 74 65 33 5f 72 65 61  2 **)sqlite3_rea
1a700 6c 6c 6f 63 28 28 76 6f 69 64 20 2a 29 70 57 61  lloc((void *)pWa
1a701 6c 2d 3e 61 70 57 69 44 61 74 61 2c 20 6e 42 79  l->apWiData, nBy
1a702 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 61  te);..    if( !a
1a703 70 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20 2a  pNew ){..      *
1a704 70 70 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 20  ppPage = 0;..   
1a705 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1a706 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a  _NOMEM;..    }..
1a707 20 20 20 20 6d 65 6d 73 65 74 28 28 76 6f 69 64      memset((void
1a708 2a 29 26 61 70 4e 65 77 5b 70 57 61 6c 2d 3e 6e  *)&apNew[pWal->n
1a709 57 69 44 61 74 61 5d 2c 20 30 2c 0d 0a 20 20 20  WiData], 0,..   
1a70a 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75          sizeof(u
1a70b 33 32 2a 29 2a 28 69 50 61 67 65 2b 31 2d 70 57  32*)*(iPage+1-pW
1a70c 61 6c 2d 3e 6e 57 69 44 61 74 61 29 29 3b 0d 0a  al->nWiData));..
1a70d 20 20 20 20 70 57 61 6c 2d 3e 61 70 57 69 44 61      pWal->apWiDa
1a70e 74 61 20 3d 20 61 70 4e 65 77 3b 0d 0a 20 20 20  ta = apNew;..   
1a70f 20 70 57 61 6c 2d 3e 6e 57 69 44 61 74 61 20 3d   pWal->nWiData =
1a710 20 69 50 61 67 65 2b 31 3b 0d 0a 20 20 7d 0d 0a   iPage+1;..  }..
1a711 0d 0a 20 20 2f 2a 20 52 65 71 75 65 73 74 20 61  ..  /* Request a
1a712 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1a713 72 65 71 75 69 72 65 64 20 70 61 67 65 20 66 72  required page fr
1a714 6f 6d 20 74 68 65 20 56 46 53 20 2a 2f 0d 0a 20  om the VFS */.. 
1a715 20 69 66 28 20 70 57 61 6c 2d 3e 61 70 57 69 44   if( pWal->apWiD
1a716 61 74 61 5b 69 50 61 67 65 5d 3d 3d 30 20 29 7b  ata[iPage]==0 ){
1a717 0d 0a 20 20 20 20 69 66 28 20 70 57 61 6c 2d 3e  ..    if( pWal->
1a718 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 57  exclusiveMode==W
1a719 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d 4f  AL_HEAPMEMORY_MO
1a71a 44 45 20 29 7b 0d 0a 20 20 20 20 20 20 70 57 61  DE ){..      pWa
1a71b 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 61 67  l->apWiData[iPag
1a71c 65 5d 20 3d 20 28 75 33 32 20 76 6f 6c 61 74 69  e] = (u32 volati
1a71d 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  le *)sqlite3Mall
1a71e 6f 63 5a 65 72 6f 28 57 41 4c 49 4e 44 45 58 5f  ocZero(WALINDEX_
1a71f 50 47 53 5a 29 3b 0d 0a 20 20 20 20 20 20 69 66  PGSZ);..      if
1a720 28 20 21 70 57 61 6c 2d 3e 61 70 57 69 44 61 74  ( !pWal->apWiDat
1a721 61 5b 69 50 61 67 65 5d 20 29 20 72 63 20 3d 20  a[iPage] ) rc = 
1a722 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
1a723 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1a724 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
1a725 68 6d 4d 61 70 28 70 57 61 6c 2d 3e 70 44 62 46  hmMap(pWal->pDbF
1a726 64 2c 20 69 50 61 67 65 2c 20 57 41 4c 49 4e 44  d, iPage, WALIND
1a727 45 58 5f 50 47 53 5a 2c 20 0d 0a 20 20 20 20 20  EX_PGSZ, ..     
1a728 20 20 20 20 20 70 57 61 6c 2d 3e 77 72 69 74 65       pWal->write
1a729 4c 6f 63 6b 2c 20 28 76 6f 69 64 20 76 6f 6c 61  Lock, (void vola
1a72a 74 69 6c 65 20 2a 2a 29 26 70 57 61 6c 2d 3e 61  tile **)&pWal->a
1a72b 70 57 69 44 61 74 61 5b 69 50 61 67 65 5d 0d 0a  pWiData[iPage]..
1a72c 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
1a72d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
1a72e 45 41 44 4f 4e 4c 59 20 29 7b 0d 0a 20 20 20 20  EADONLY ){..    
1a72f 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4f 6e      pWal->readOn
1a730 6c 79 20 7c 3d 20 57 41 4c 5f 53 48 4d 5f 52 44  ly |= WAL_SHM_RD
1a731 4f 4e 4c 59 3b 0d 0a 20 20 20 20 20 20 20 20 72  ONLY;..        r
1a732 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
1a733 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
1a734 20 20 7d 0d 0a 0d 0a 20 20 2a 70 70 50 61 67 65    }....  *ppPage
1a735 20 3d 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74   = pWal->apWiDat
1a736 61 5b 69 50 61 67 65 5d 3b 0d 0a 20 20 61 73 73  a[iPage];..  ass
1a737 65 72 74 28 20 69 50 61 67 65 3d 3d 30 20 7c 7c  ert( iPage==0 ||
1a738 20 2a 70 70 50 61 67 65 20 7c 7c 20 72 63 21 3d   *ppPage || rc!=
1a739 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20  SQLITE_OK );..  
1a73a 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
1a73b 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ./*..** Return a
1a73c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1a73d 57 61 6c 43 6b 70 74 49 6e 66 6f 20 73 74 72 75  WalCkptInfo stru
1a73e 63 74 75 72 65 20 69 6e 20 74 68 65 20 77 61 6c  cture in the wal
1a73f 2d 69 6e 64 65 78 2e 0d 0a 2a 2f 0d 0a 73 74 61  -index...*/..sta
1a740 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 57 61 6c  tic volatile Wal
1a741 43 6b 70 74 49 6e 66 6f 20 2a 77 61 6c 43 6b 70  CkptInfo *walCkp
1a742 74 49 6e 66 6f 28 57 61 6c 20 2a 70 57 61 6c 29  tInfo(Wal *pWal)
1a743 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  {..  assert( pWa
1a744 6c 2d 3e 6e 57 69 44 61 74 61 3e 30 20 26 26 20  l->nWiData>0 && 
1a745 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 30  pWal->apWiData[0
1a746 5d 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28  ] );..  return (
1a747 76 6f 6c 61 74 69 6c 65 20 57 61 6c 43 6b 70 74  volatile WalCkpt
1a748 49 6e 66 6f 2a 29 26 28 70 57 61 6c 2d 3e 61 70  Info*)&(pWal->ap
1a749 57 69 44 61 74 61 5b 30 5d 5b 73 69 7a 65 6f 66  WiData[0][sizeof
1a74a 28 57 61 6c 49 6e 64 65 78 48 64 72 29 2f 32 5d  (WalIndexHdr)/2]
1a74b 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
1a74c 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1a74d 20 74 6f 20 74 68 65 20 57 61 6c 49 6e 64 65 78   to the WalIndex
1a74e 48 64 72 20 73 74 72 75 63 74 75 72 65 20 69 6e  Hdr structure in
1a74f 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0d   the wal-index..
1a750 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 6c 61  .*/..static vola
1a751 74 69 6c 65 20 57 61 6c 49 6e 64 65 78 48 64 72  tile WalIndexHdr
1a752 20 2a 77 61 6c 49 6e 64 65 78 48 64 72 28 57 61   *walIndexHdr(Wa
1a753 6c 20 2a 70 57 61 6c 29 7b 0d 0a 20 20 61 73 73  l *pWal){..  ass
1a754 65 72 74 28 20 70 57 61 6c 2d 3e 6e 57 69 44 61  ert( pWal->nWiDa
1a755 74 61 3e 30 20 26 26 20 70 57 61 6c 2d 3e 61 70  ta>0 && pWal->ap
1a756 57 69 44 61 74 61 5b 30 5d 20 29 3b 0d 0a 20 20  WiData[0] );..  
1a757 72 65 74 75 72 6e 20 28 76 6f 6c 61 74 69 6c 65  return (volatile
1a758 20 57 61 6c 49 6e 64 65 78 48 64 72 2a 29 70 57   WalIndexHdr*)pW
1a759 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 30 5d 3b  al->apWiData[0];
1a75a 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
1a75b 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  e argument to th
1a75c 69 73 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 65  is macro must be
1a75d 20 6f 66 20 74 79 70 65 20 75 33 32 2e 20 4f 6e   of type u32. On
1a75e 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e   a little-endian
1a75f 0d 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72  ..** architectur
1a760 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 74 68  e, it returns th
1a761 65 20 75 33 32 20 76 61 6c 75 65 20 74 68 61 74  e u32 value that
1a762 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 69 6e   results from in
1a763 74 65 72 70 72 65 74 69 6e 67 0d 0a 2a 2a 20 74  terpreting..** t
1a764 68 65 20 34 20 62 79 74 65 73 20 61 73 20 61 20  he 4 bytes as a 
1a765 62 69 67 2d 65 6e 64 69 61 6e 20 76 61 6c 75 65  big-endian value
1a766 2e 20 4f 6e 20 61 20 62 69 67 2d 65 6e 64 69 61  . On a big-endia
1a767 6e 20 61 72 63 68 69 74 65 63 74 75 72 65 2c 20  n architecture, 
1a768 69 74 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74  it..** returns t
1a769 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f  he value that wo
1a76a 75 6c 64 20 62 65 20 70 72 6f 64 75 63 65 64 20  uld be produced 
1a76b 62 79 20 69 6e 74 65 70 72 65 74 69 6e 67 20 74  by intepreting t
1a76c 68 65 20 34 20 62 79 74 65 73 0d 0a 2a 2a 20 6f  he 4 bytes..** o
1a76d 66 20 74 68 65 20 69 6e 70 75 74 20 76 61 6c 75  f the input valu
1a76e 65 20 61 73 20 61 20 6c 69 74 74 6c 65 2d 65 6e  e as a little-en
1a76f 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a 2a  dian integer...*
1a770 2f 0d 0a 23 64 65 66 69 6e 65 20 42 59 54 45 53  /..#define BYTES
1a771 57 41 50 33 32 28 78 29 20 28 20 5c 0d 0a 20 20  WAP32(x) ( \..  
1a772 20 20 28 28 28 78 29 26 30 78 30 30 30 30 30 30    (((x)&0x000000
1a773 46 46 29 3c 3c 32 34 29 20 2b 20 28 28 28 78 29  FF)<<24) + (((x)
1a774 26 30 78 30 30 30 30 46 46 30 30 29 3c 3c 38 29  &0x0000FF00)<<8)
1a775 20 20 5c 0d 0a 20 20 2b 20 28 28 28 78 29 26 30    \..  + (((x)&0
1a776 78 30 30 46 46 30 30 30 30 29 3e 3e 38 29 20 20  x00FF0000)>>8)  
1a777 2b 20 28 28 28 78 29 26 30 78 46 46 30 30 30 30  + (((x)&0xFF0000
1a778 30 30 29 3e 3e 32 34 29 20 5c 0d 0a 29 0d 0a 0d  00)>>24) \..)...
1a779 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ./*..** Generate
1a77a 20 6f 72 20 65 78 74 65 6e 64 20 61 6e 20 38 20   or extend an 8 
1a77b 62 79 74 65 20 63 68 65 63 6b 73 75 6d 20 62 61  byte checksum ba
1a77c 73 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 20  sed on the data 
1a77d 69 6e 20 0d 0a 2a 2a 20 61 72 72 61 79 20 61 42  in ..** array aB
1a77e 79 74 65 5b 5d 20 61 6e 64 20 74 68 65 20 69 6e  yte[] and the in
1a77f 69 74 69 61 6c 20 76 61 6c 75 65 73 20 6f 66 20  itial values of 
1a780 61 49 6e 5b 30 5d 20 61 6e 64 20 61 49 6e 5b 31  aIn[0] and aIn[1
1a781 5d 20 28 6f 72 0d 0a 2a 2a 20 69 6e 69 74 69 61  ] (or..** initia
1a782 6c 20 76 61 6c 75 65 73 20 6f 66 20 30 20 61 6e  l values of 0 an
1a783 64 20 30 20 69 66 20 61 49 6e 3d 3d 4e 55 4c 4c  d 0 if aIn==NULL
1a784 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63  )...**..** The c
1a785 68 65 63 6b 73 75 6d 20 69 73 20 77 72 69 74 74  hecksum is writt
1a786 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 61 4f 75  en back into aOu
1a787 74 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72  t[] before retur
1a788 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 6e 42  ning...**..** nB
1a789 79 74 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f  yte must be a po
1a78a 73 69 74 69 76 65 20 6d 75 6c 74 69 70 6c 65 20  sitive multiple 
1a78b 6f 66 20 38 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  of 8...*/..stati
1a78c 63 20 76 6f 69 64 20 77 61 6c 43 68 65 63 6b 73  c void walChecks
1a78d 75 6d 42 79 74 65 73 28 0d 0a 20 20 69 6e 74 20  umBytes(..  int 
1a78e 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 2f 2a 20  nativeCksum, /* 
1a78f 54 72 75 65 20 66 6f 72 20 6e 61 74 69 76 65 20  True for native 
1a790 62 79 74 65 2d 6f 72 64 65 72 2c 20 66 61 6c 73  byte-order, fals
1a791 65 20 66 6f 72 20 6e 6f 6e 2d 6e 61 74 69 76 65  e for non-native
1a792 20 2a 2f 0d 0a 20 20 75 38 20 2a 61 2c 20 20 20   */..  u8 *a,   
1a793 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
1a794 6e 74 20 74 6f 20 62 65 20 63 68 65 63 6b 73 75  nt to be checksu
1a795 6d 6d 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  mmed */..  int n
1a796 42 79 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 42  Byte,       /* B
1a797 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20  ytes of content 
1a798 69 6e 20 61 5b 5d 2e 20 20 4d 75 73 74 20 62 65  in a[].  Must be
1a799 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38   a multiple of 8
1a79a 2e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 33  . */..  const u3
1a79b 32 20 2a 61 49 6e 2c 20 20 2f 2a 20 49 6e 69 74  2 *aIn,  /* Init
1a79c 69 61 6c 20 63 68 65 63 6b 73 75 6d 20 76 61 6c  ial checksum val
1a79d 75 65 20 69 6e 70 75 74 20 2a 2f 0d 0a 20 20 75  ue input */..  u
1a79e 33 32 20 2a 61 4f 75 74 20 20 20 20 20 20 20 20  32 *aOut        
1a79f 2f 2a 20 4f 55 54 3a 20 46 69 6e 61 6c 20 63 68  /* OUT: Final ch
1a7a0 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 6f 75 74  ecksum value out
1a7a1 70 75 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 33  put */..){..  u3
1a7a2 32 20 73 31 2c 20 73 32 3b 0d 0a 20 20 75 33 32  2 s1, s2;..  u32
1a7a3 20 2a 61 44 61 74 61 20 3d 20 28 75 33 32 20 2a   *aData = (u32 *
1a7a4 29 61 3b 0d 0a 20 20 75 33 32 20 2a 61 45 6e 64  )a;..  u32 *aEnd
1a7a5 20 3d 20 28 75 33 32 20 2a 29 26 61 5b 6e 42 79   = (u32 *)&a[nBy
1a7a6 74 65 5d 3b 0d 0a 0d 0a 20 20 69 66 28 20 61 49  te];....  if( aI
1a7a7 6e 20 29 7b 0d 0a 20 20 20 20 73 31 20 3d 20 61  n ){..    s1 = a
1a7a8 49 6e 5b 30 5d 3b 0d 0a 20 20 20 20 73 32 20 3d  In[0];..    s2 =
1a7a9 20 61 49 6e 5b 31 5d 3b 0d 0a 20 20 7d 65 6c 73   aIn[1];..  }els
1a7aa 65 7b 0d 0a 20 20 20 20 73 31 20 3d 20 73 32 20  e{..    s1 = s2 
1a7ab 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61  = 0;..  }....  a
1a7ac 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 38 20  ssert( nByte>=8 
1a7ad 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 6e  );..  assert( (n
1a7ae 42 79 74 65 26 30 78 30 30 30 30 30 30 30 37 29  Byte&0x00000007)
1a7af 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20  ==0 );....  if( 
1a7b0 6e 61 74 69 76 65 43 6b 73 75 6d 20 29 7b 0d 0a  nativeCksum ){..
1a7b1 20 20 20 20 64 6f 20 7b 0d 0a 20 20 20 20 20 20      do {..      
1a7b2 73 31 20 2b 3d 20 2a 61 44 61 74 61 2b 2b 20 2b  s1 += *aData++ +
1a7b3 20 73 32 3b 0d 0a 20 20 20 20 20 20 73 32 20 2b   s2;..      s2 +
1a7b4 3d 20 2a 61 44 61 74 61 2b 2b 20 2b 20 73 31 3b  = *aData++ + s1;
1a7b5 0d 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 61 44  ..    }while( aD
1a7b6 61 74 61 3c 61 45 6e 64 20 29 3b 0d 0a 20 20 7d  ata<aEnd );..  }
1a7b7 65 6c 73 65 7b 0d 0a 20 20 20 20 64 6f 20 7b 0d  else{..    do {.
1a7b8 0a 20 20 20 20 20 20 73 31 20 2b 3d 20 42 59 54  .      s1 += BYT
1a7b9 45 53 57 41 50 33 32 28 61 44 61 74 61 5b 30 5d  ESWAP32(aData[0]
1a7ba 29 20 2b 20 73 32 3b 0d 0a 20 20 20 20 20 20 73  ) + s2;..      s
1a7bb 32 20 2b 3d 20 42 59 54 45 53 57 41 50 33 32 28  2 += BYTESWAP32(
1a7bc 61 44 61 74 61 5b 31 5d 29 20 2b 20 73 31 3b 0d  aData[1]) + s1;.
1a7bd 0a 20 20 20 20 20 20 61 44 61 74 61 20 2b 3d 20  .      aData += 
1a7be 32 3b 0d 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  2;..    }while( 
1a7bf 61 44 61 74 61 3c 61 45 6e 64 20 29 3b 0d 0a 20  aData<aEnd );.. 
1a7c0 20 7d 0d 0a 0d 0a 20 20 61 4f 75 74 5b 30 5d 20   }....  aOut[0] 
1a7c1 3d 20 73 31 3b 0d 0a 20 20 61 4f 75 74 5b 31 5d  = s1;..  aOut[1]
1a7c2 20 3d 20 73 32 3b 0d 0a 7d 0d 0a 0d 0a 73 74 61   = s2;..}....sta
1a7c3 74 69 63 20 76 6f 69 64 20 77 61 6c 53 68 6d 42  tic void walShmB
1a7c4 61 72 72 69 65 72 28 57 61 6c 20 2a 70 57 61 6c  arrier(Wal *pWal
1a7c5 29 7b 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e  ){..  if( pWal->
1a7c6 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 57  exclusiveMode!=W
1a7c7 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d 4f  AL_HEAPMEMORY_MO
1a7c8 44 45 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  DE ){..    sqlit
1a7c9 65 33 4f 73 53 68 6d 42 61 72 72 69 65 72 28 70  e3OsShmBarrier(p
1a7ca 57 61 6c 2d 3e 70 44 62 46 64 29 3b 0d 0a 20 20  Wal->pDbFd);..  
1a7cb 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57  }..}..../*..** W
1a7cc 72 69 74 65 20 74 68 65 20 68 65 61 64 65 72 20  rite the header 
1a7cd 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 70  information in p
1a7ce 57 61 6c 2d 3e 68 64 72 20 69 6e 74 6f 20 74 68  Wal->hdr into th
1a7cf 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0d 0a 2a 2a  e wal-index...**
1a7d0 0d 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 73 75  ..** The checksu
1a7d1 6d 20 6f 6e 20 70 57 61 6c 2d 3e 68 64 72 20 69  m on pWal->hdr i
1a7d2 73 20 75 70 64 61 74 65 64 20 62 65 66 6f 72 65  s updated before
1a7d3 20 69 74 20 69 73 20 77 72 69 74 74 65 6e 2e 0d   it is written..
1a7d4 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
1a7d5 20 77 61 6c 49 6e 64 65 78 57 72 69 74 65 48 64   walIndexWriteHd
1a7d6 72 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0d 0a 20  r(Wal *pWal){.. 
1a7d7 20 76 6f 6c 61 74 69 6c 65 20 57 61 6c 49 6e 64   volatile WalInd
1a7d8 65 78 48 64 72 20 2a 61 48 64 72 20 3d 20 77 61  exHdr *aHdr = wa
1a7d9 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29 3b  lIndexHdr(pWal);
1a7da 0d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43  ..  const int nC
1a7db 6b 73 75 6d 20 3d 20 6f 66 66 73 65 74 6f 66 28  ksum = offsetof(
1a7dc 57 61 6c 49 6e 64 65 78 48 64 72 2c 20 61 43 6b  WalIndexHdr, aCk
1a7dd 73 75 6d 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  sum);....  asser
1a7de 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f  t( pWal->writeLo
1a7df 63 6b 20 29 3b 0d 0a 20 20 70 57 61 6c 2d 3e 68  ck );..  pWal->h
1a7e0 64 72 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0d 0a  dr.isInit = 1;..
1a7e1 20 20 70 57 61 6c 2d 3e 68 64 72 2e 69 56 65 72    pWal->hdr.iVer
1a7e2 73 69 6f 6e 20 3d 20 57 41 4c 49 4e 44 45 58 5f  sion = WALINDEX_
1a7e3 4d 41 58 5f 56 45 52 53 49 4f 4e 3b 0d 0a 20 20  MAX_VERSION;..  
1a7e4 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65 73  walChecksumBytes
1a7e5 28 31 2c 20 28 75 38 2a 29 26 70 57 61 6c 2d 3e  (1, (u8*)&pWal->
1a7e6 68 64 72 2c 20 6e 43 6b 73 75 6d 2c 20 30 2c 20  hdr, nCksum, 0, 
1a7e7 70 57 61 6c 2d 3e 68 64 72 2e 61 43 6b 73 75 6d  pWal->hdr.aCksum
1a7e8 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 28 76 6f  );..  memcpy((vo
1a7e9 69 64 20 2a 29 26 61 48 64 72 5b 31 5d 2c 20 28  id *)&aHdr[1], (
1a7ea 76 6f 69 64 20 2a 29 26 70 57 61 6c 2d 3e 68 64  void *)&pWal->hd
1a7eb 72 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e 64  r, sizeof(WalInd
1a7ec 65 78 48 64 72 29 29 3b 0d 0a 20 20 77 61 6c 53  exHdr));..  walS
1a7ed 68 6d 42 61 72 72 69 65 72 28 70 57 61 6c 29 3b  hmBarrier(pWal);
1a7ee 0d 0a 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64  ..  memcpy((void
1a7ef 20 2a 29 26 61 48 64 72 5b 30 5d 2c 20 28 76 6f   *)&aHdr[0], (vo
1a7f0 69 64 20 2a 29 26 70 57 61 6c 2d 3e 68 64 72 2c  id *)&pWal->hdr,
1a7f1 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78   sizeof(WalIndex
1a7f2 48 64 72 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  Hdr));..}..../*.
1a7f3 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1a7f4 6e 20 65 6e 63 6f 64 65 73 20 61 20 73 69 6e 67  n encodes a sing
1a7f5 6c 65 20 66 72 61 6d 65 20 68 65 61 64 65 72 20  le frame header 
1a7f6 61 6e 64 20 77 72 69 74 65 73 20 69 74 20 74 6f  and writes it to
1a7f7 20 61 20 62 75 66 66 65 72 0d 0a 2a 2a 20 73 75   a buffer..** su
1a7f8 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 63 61  pplied by the ca
1a7f9 6c 6c 65 72 2e 20 41 20 66 72 61 6d 65 2d 68 65  ller. A frame-he
1a7fa 61 64 65 72 20 69 73 20 6d 61 64 65 20 75 70 20  ader is made up 
1a7fb 6f 66 20 61 20 73 65 72 69 65 73 20 6f 66 20 0d  of a series of .
1a7fc 0a 2a 2a 20 34 2d 62 79 74 65 20 62 69 67 2d 65  .** 4-byte big-e
1a7fd 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 73 2c 20  ndian integers, 
1a7fe 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d  as follows:..**.
1a7ff 0a 2a 2a 20 20 20 20 20 30 3a 20 50 61 67 65 20  .**     0: Page 
1a800 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 20 20 20 20 20  number...**     
1a801 34 3a 20 46 6f 72 20 63 6f 6d 6d 69 74 20 72 65  4: For commit re
1a802 63 6f 72 64 73 2c 20 74 68 65 20 73 69 7a 65 20  cords, the size 
1a803 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1a804 69 6d 61 67 65 20 69 6e 20 70 61 67 65 73 20 0d  image in pages .
1a805 0a 2a 2a 20 20 20 20 20 20 20 20 61 66 74 65 72  .**        after
1a806 20 74 68 65 20 63 6f 6d 6d 69 74 2e 20 46 6f 72   the commit. For
1a807 20 61 6c 6c 20 6f 74 68 65 72 20 72 65 63 6f 72   all other recor
1a808 64 73 2c 20 7a 65 72 6f 2e 0d 0a 2a 2a 20 20 20  ds, zero...**   
1a809 20 20 38 3a 20 53 61 6c 74 2d 31 20 28 63 6f 70    8: Salt-1 (cop
1a80a 69 65 64 20 66 72 6f 6d 20 74 68 65 20 77 61 6c  ied from the wal
1a80b 2d 68 65 61 64 65 72 29 0d 0a 2a 2a 20 20 20 20  -header)..**    
1a80c 31 32 3a 20 53 61 6c 74 2d 32 20 28 63 6f 70 69  12: Salt-2 (copi
1a80d 65 64 20 66 72 6f 6d 20 74 68 65 20 77 61 6c 2d  ed from the wal-
1a80e 68 65 61 64 65 72 29 0d 0a 2a 2a 20 20 20 20 31  header)..**    1
1a80f 36 3a 20 43 68 65 63 6b 73 75 6d 2d 31 2e 0d 0a  6: Checksum-1...
1a810 2a 2a 20 20 20 20 32 30 3a 20 43 68 65 63 6b 73  **    20: Checks
1a811 75 6d 2d 32 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  um-2...*/..stati
1a812 63 20 76 6f 69 64 20 77 61 6c 45 6e 63 6f 64 65  c void walEncode
1a813 46 72 61 6d 65 28 0d 0a 20 20 57 61 6c 20 2a 70  Frame(..  Wal *p
1a814 57 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Wal,            
1a815 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1a816 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67   write-ahead log
1a817 20 2a 2f 0d 0a 20 20 75 33 32 20 69 50 61 67 65   */..  u32 iPage
1a818 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a819 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1a81a 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  se page number f
1a81b 6f 72 20 66 72 61 6d 65 20 2a 2f 0d 0a 20 20 75  or frame */..  u
1a81c 33 32 20 6e 54 72 75 6e 63 61 74 65 2c 20 20 20  32 nTruncate,   
1a81d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a81e 2a 20 4e 65 77 20 64 62 20 73 69 7a 65 20 28 6f  * New db size (o
1a81f 72 20 30 20 66 6f 72 20 6e 6f 6e 2d 63 6f 6d 6d  r 0 for non-comm
1a820 69 74 20 66 72 61 6d 65 73 29 20 2a 2f 0d 0a 20  it frames) */.. 
1a821 20 75 38 20 2a 61 44 61 74 61 2c 20 20 20 20 20   u8 *aData,     
1a822 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a823 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70   /* Pointer to p
1a824 61 67 65 20 64 61 74 61 20 2a 2f 0d 0a 20 20 75  age data */..  u
1a825 38 20 2a 61 46 72 61 6d 65 20 20 20 20 20 20 20  8 *aFrame       
1a826 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a827 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 65 6e 63  * OUT: Write enc
1a828 6f 64 65 64 20 66 72 61 6d 65 20 68 65 72 65 20  oded frame here 
1a829 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 6e 61  */..){..  int na
1a82a 74 69 76 65 43 6b 73 75 6d 3b 20 20 20 20 20 20  tiveCksum;      
1a82b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1a82c 65 20 66 6f 72 20 6e 61 74 69 76 65 20 62 79 74  e for native byt
1a82d 65 2d 6f 72 64 65 72 20 63 68 65 63 6b 73 75 6d  e-order checksum
1a82e 73 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 61 43 6b  s */..  u32 *aCk
1a82f 73 75 6d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e  sum = pWal->hdr.
1a830 61 46 72 61 6d 65 43 6b 73 75 6d 3b 0d 0a 20 20  aFrameCksum;..  
1a831 61 73 73 65 72 74 28 20 57 41 4c 5f 46 52 41 4d  assert( WAL_FRAM
1a832 45 5f 48 44 52 53 49 5a 45 3d 3d 32 34 20 29 3b  E_HDRSIZE==24 );
1a833 0d 0a 20 20 73 71 6c 69 74 65 33 50 75 74 34 62  ..  sqlite3Put4b
1a834 79 74 65 28 26 61 46 72 61 6d 65 5b 30 5d 2c 20  yte(&aFrame[0], 
1a835 69 50 61 67 65 29 3b 0d 0a 20 20 73 71 6c 69 74  iPage);..  sqlit
1a836 65 33 50 75 74 34 62 79 74 65 28 26 61 46 72 61  e3Put4byte(&aFra
1a837 6d 65 5b 34 5d 2c 20 6e 54 72 75 6e 63 61 74 65  me[4], nTruncate
1a838 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 26 61 46  );..  memcpy(&aF
1a839 72 61 6d 65 5b 38 5d 2c 20 70 57 61 6c 2d 3e 68  rame[8], pWal->h
1a83a 64 72 2e 61 53 61 6c 74 2c 20 38 29 3b 0d 0a 0d  dr.aSalt, 8);...
1a83b 0a 20 20 6e 61 74 69 76 65 43 6b 73 75 6d 20 3d  .  nativeCksum =
1a83c 20 28 70 57 61 6c 2d 3e 68 64 72 2e 62 69 67 45   (pWal->hdr.bigE
1a83d 6e 64 43 6b 73 75 6d 3d 3d 53 51 4c 49 54 45 5f  ndCksum==SQLITE_
1a83e 42 49 47 45 4e 44 49 41 4e 29 3b 0d 0a 20 20 77  BIGENDIAN);..  w
1a83f 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65 73 28  alChecksumBytes(
1a840 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 61 46 72  nativeCksum, aFr
1a841 61 6d 65 2c 20 38 2c 20 61 43 6b 73 75 6d 2c 20  ame, 8, aCksum, 
1a842 61 43 6b 73 75 6d 29 3b 0d 0a 20 20 77 61 6c 43  aCksum);..  walC
1a843 68 65 63 6b 73 75 6d 42 79 74 65 73 28 6e 61 74  hecksumBytes(nat
1a844 69 76 65 43 6b 73 75 6d 2c 20 61 44 61 74 61 2c  iveCksum, aData,
1a845 20 70 57 61 6c 2d 3e 73 7a 50 61 67 65 2c 20 61   pWal->szPage, a
1a846 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d 29 3b 0d  Cksum, aCksum);.
1a847 0a 0d 0a 20 20 73 71 6c 69 74 65 33 50 75 74 34  ...  sqlite3Put4
1a848 62 79 74 65 28 26 61 46 72 61 6d 65 5b 31 36 5d  byte(&aFrame[16]
1a849 2c 20 61 43 6b 73 75 6d 5b 30 5d 29 3b 0d 0a 20  , aCksum[0]);.. 
1a84a 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65   sqlite3Put4byte
1a84b 28 26 61 46 72 61 6d 65 5b 32 30 5d 2c 20 61 43  (&aFrame[20], aC
1a84c 6b 73 75 6d 5b 31 5d 29 3b 0d 0a 7d 0d 0a 0d 0a  ksum[1]);..}....
1a84d 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  /*..** Check to 
1a84e 73 65 65 20 69 66 20 74 68 65 20 66 72 61 6d 65  see if the frame
1a84f 20 77 69 74 68 20 68 65 61 64 65 72 20 69 6e 20   with header in 
1a850 61 46 72 61 6d 65 5b 5d 20 61 6e 64 20 63 6f 6e  aFrame[] and con
1a851 74 65 6e 74 0d 0a 2a 2a 20 69 6e 20 61 44 61 74  tent..** in aDat
1a852 61 5b 5d 20 69 73 20 76 61 6c 69 64 2e 20 20 49  a[] is valid.  I
1a853 66 20 69 74 20 69 73 20 61 20 76 61 6c 69 64 20  f it is a valid 
1a854 66 72 61 6d 65 2c 20 66 69 6c 6c 20 2a 70 69 50  frame, fill *piP
1a855 61 67 65 20 61 6e 64 0d 0a 2a 2a 20 2a 70 6e 54  age and..** *pnT
1a856 72 75 6e 63 61 74 65 20 61 6e 64 20 72 65 74 75  runcate and retu
1a857 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e  rn true.  Return
1a858 20 69 66 20 74 68 65 20 66 72 61 6d 65 20 69 73   if the frame is
1a859 20 6e 6f 74 20 76 61 6c 69 64 2e 0d 0a 2a 2f 0d   not valid...*/.
1a85a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c 44  .static int walD
1a85b 65 63 6f 64 65 46 72 61 6d 65 28 0d 0a 20 20 57  ecodeFrame(..  W
1a85c 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20 20  al *pWal,       
1a85d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a85e 2a 20 54 68 65 20 77 72 69 74 65 2d 61 68 65 61  * The write-ahea
1a85f 64 20 6c 6f 67 20 2a 2f 0d 0a 20 20 75 33 32 20  d log */..  u32 
1a860 2a 70 69 50 61 67 65 2c 20 20 20 20 20 20 20 20  *piPage,        
1a861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1a862 55 54 3a 20 44 61 74 61 62 61 73 65 20 70 61 67  UT: Database pag
1a863 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 66 72 61  e number for fra
1a864 6d 65 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 70 6e  me */..  u32 *pn
1a865 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20  Truncate,       
1a866 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1a867 20 4e 65 77 20 64 62 20 73 69 7a 65 20 28 6f 72   New db size (or
1a868 20 30 20 69 66 20 6e 6f 74 20 63 6f 6d 6d 69 74   0 if not commit
1a869 29 20 2a 2f 0d 0a 20 20 75 38 20 2a 61 44 61 74  ) */..  u8 *aDat
1a86a 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a,              
1a86b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1a86c 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20  er to page data 
1a86d 28 66 6f 72 20 63 68 65 63 6b 73 75 6d 29 20 2a  (for checksum) *
1a86e 2f 0d 0a 20 20 75 38 20 2a 61 46 72 61 6d 65 20  /..  u8 *aFrame 
1a86f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a870 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20 64 61       /* Frame da
1a871 74 61 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  ta */..){..  int
1a872 20 6e 61 74 69 76 65 43 6b 73 75 6d 3b 20 20 20   nativeCksum;   
1a873 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a874 54 72 75 65 20 66 6f 72 20 6e 61 74 69 76 65 20  True for native 
1a875 62 79 74 65 2d 6f 72 64 65 72 20 63 68 65 63 6b  byte-order check
1a876 73 75 6d 73 20 2a 2f 0d 0a 20 20 75 33 32 20 2a  sums */..  u32 *
1a877 61 43 6b 73 75 6d 20 3d 20 70 57 61 6c 2d 3e 68  aCksum = pWal->h
1a878 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 3b 0d  dr.aFrameCksum;.
1a879 0a 20 20 75 33 32 20 70 67 6e 6f 3b 20 20 20 20  .  u32 pgno;    
1a87a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a87b 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
1a87c 72 20 6f 66 20 74 68 65 20 66 72 61 6d 65 20 2a  r of the frame *
1a87d 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 57 41 4c  /..  assert( WAL
1a87e 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 3d 3d  _FRAME_HDRSIZE==
1a87f 32 34 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 20  24 );....  /* A 
1a880 66 72 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61  frame is only va
1a881 6c 69 64 20 69 66 20 74 68 65 20 73 61 6c 74 20  lid if the salt 
1a882 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 72  values in the fr
1a883 61 6d 65 2d 68 65 61 64 65 72 0d 0a 20 20 2a 2a  ame-header..  **
1a884 20 6d 61 74 63 68 20 74 68 65 20 73 61 6c 74 20   match the salt 
1a885 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61  values in the wa
1a886 6c 2d 68 65 61 64 65 72 2e 20 0d 0a 20 20 2a 2f  l-header. ..  */
1a887 0d 0a 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26  ..  if( memcmp(&
1a888 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61 6c 74 2c  pWal->hdr.aSalt,
1a889 20 26 61 46 72 61 6d 65 5b 38 5d 2c 20 38 29 21   &aFrame[8], 8)!
1a88a 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
1a88b 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  n 0;..  }....  /
1a88c 2a 20 41 20 66 72 61 6d 65 20 69 73 20 6f 6e 6c  * A frame is onl
1a88d 79 20 76 61 6c 69 64 20 69 66 20 74 68 65 20 70  y valid if the p
1a88e 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 63 72  age number is cr
1a88f 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e  eater than zero.
1a890 0d 0a 20 20 2a 2f 0d 0a 20 20 70 67 6e 6f 20 3d  ..  */..  pgno =
1a891 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
1a892 28 26 61 46 72 61 6d 65 5b 30 5d 29 3b 0d 0a 20  (&aFrame[0]);.. 
1a893 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0d   if( pgno==0 ){.
1a894 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  .    return 0;..
1a895 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 20 66 72    }....  /* A fr
1a896 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  ame is only vali
1a897 64 20 69 66 20 61 20 63 68 65 63 6b 73 75 6d 20  d if a checksum 
1a898 6f 66 20 74 68 65 20 57 41 4c 20 68 65 61 64 65  of the WAL heade
1a899 72 2c 0d 0a 20 20 2a 2a 20 61 6c 6c 20 70 72 69  r,..  ** all pri
1a89a 6f 72 20 66 72 61 6d 73 2c 20 74 68 65 20 66 69  or frams, the fi
1a89b 72 73 74 20 31 36 20 62 79 74 65 73 20 6f 66 20  rst 16 bytes of 
1a89c 74 68 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65  this frame-heade
1a89d 72 2c 20 0d 0a 20 20 2a 2a 20 61 6e 64 20 74 68  r, ..  ** and th
1a89e 65 20 66 72 61 6d 65 2d 64 61 74 61 20 6d 61 74  e frame-data mat
1a89f 63 68 65 73 20 74 68 65 20 63 68 65 63 6b 73 75  ches the checksu
1a8a0 6d 20 69 6e 20 74 68 65 20 6c 61 73 74 20 38 20  m in the last 8 
1a8a1 0d 0a 20 20 2a 2a 20 62 79 74 65 73 20 6f 66 20  ..  ** bytes of 
1a8a2 74 68 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65  this frame-heade
1a8a3 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6e 61 74 69  r...  */..  nati
1a8a4 76 65 43 6b 73 75 6d 20 3d 20 28 70 57 61 6c 2d  veCksum = (pWal-
1a8a5 3e 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d  >hdr.bigEndCksum
1a8a6 3d 3d 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49  ==SQLITE_BIGENDI
1a8a7 41 4e 29 3b 0d 0a 20 20 77 61 6c 43 68 65 63 6b  AN);..  walCheck
1a8a8 73 75 6d 42 79 74 65 73 28 6e 61 74 69 76 65 43  sumBytes(nativeC
1a8a9 6b 73 75 6d 2c 20 61 46 72 61 6d 65 2c 20 38 2c  ksum, aFrame, 8,
1a8aa 20 61 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d 29   aCksum, aCksum)
1a8ab 3b 0d 0a 20 20 77 61 6c 43 68 65 63 6b 73 75 6d  ;..  walChecksum
1a8ac 42 79 74 65 73 28 6e 61 74 69 76 65 43 6b 73 75  Bytes(nativeCksu
1a8ad 6d 2c 20 61 44 61 74 61 2c 20 70 57 61 6c 2d 3e  m, aData, pWal->
1a8ae 73 7a 50 61 67 65 2c 20 61 43 6b 73 75 6d 2c 20  szPage, aCksum, 
1a8af 61 43 6b 73 75 6d 29 3b 0d 0a 20 20 69 66 28 20  aCksum);..  if( 
1a8b0 61 43 6b 73 75 6d 5b 30 5d 21 3d 73 71 6c 69 74  aCksum[0]!=sqlit
1a8b1 65 33 47 65 74 34 62 79 74 65 28 26 61 46 72 61  e3Get4byte(&aFra
1a8b2 6d 65 5b 31 36 5d 29 20 0d 0a 20 20 20 7c 7c 20  me[16]) ..   || 
1a8b3 61 43 6b 73 75 6d 5b 31 5d 21 3d 73 71 6c 69 74  aCksum[1]!=sqlit
1a8b4 65 33 47 65 74 34 62 79 74 65 28 26 61 46 72 61  e3Get4byte(&aFra
1a8b5 6d 65 5b 32 30 5d 29 20 0d 0a 20 20 29 7b 0d 0a  me[20]) ..  ){..
1a8b6 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
1a8b7 66 61 69 6c 65 64 2e 20 2a 2f 0d 0a 20 20 20 20  failed. */..    
1a8b8 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a  return 0;..  }..
1a8b9 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61  ..  /* If we rea
1a8ba 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  ch this point, t
1a8bb 68 65 20 66 72 61 6d 65 20 69 73 20 76 61 6c 69  he frame is vali
1a8bc 64 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 70  d.  Return the p
1a8bd 61 67 65 20 6e 75 6d 62 65 72 0d 0a 20 20 2a 2a  age number..  **
1a8be 20 61 6e 64 20 74 68 65 20 6e 65 77 20 64 61 74   and the new dat
1a8bf 61 62 61 73 65 20 73 69 7a 65 2e 0d 0a 20 20 2a  abase size...  *
1a8c0 2f 0d 0a 20 20 2a 70 69 50 61 67 65 20 3d 20 70  /..  *piPage = p
1a8c1 67 6e 6f 3b 0d 0a 20 20 2a 70 6e 54 72 75 6e 63  gno;..  *pnTrunc
1a8c2 61 74 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74  ate = sqlite3Get
1a8c3 34 62 79 74 65 28 26 61 46 72 61 6d 65 5b 34 5d  4byte(&aFrame[4]
1a8c4 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d  );..  return 1;.
1a8c5 0a 7d 0d 0a 0d 0a 0d 0a 23 69 66 20 64 65 66 69  .}......#if defi
1a8c6 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
1a8c7 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
1a8c8 54 45 5f 44 45 42 55 47 29 0d 0a 2f 2a 0d 0a 2a  TE_DEBUG)../*..*
1a8c9 2a 20 4e 61 6d 65 73 20 6f 66 20 6c 6f 63 6b 73  * Names of locks
1a8ca 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1a8cb 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69  is used to provi
1a8cc 64 65 20 64 65 62 75 67 67 69 6e 67 20 6f 75 74  de debugging out
1a8cd 70 75 74 20 61 6e 64 20 69 73 20 6e 6f 74 0d 0a  put and is not..
1a8ce 2a 2a 20 61 20 70 61 72 74 20 6f 66 20 61 6e 20  ** a part of an 
1a8cf 6f 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0d  ordinary build..
1a8d0 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73  .*/..static cons
1a8d1 74 20 63 68 61 72 20 2a 77 61 6c 4c 6f 63 6b 4e  t char *walLockN
1a8d2 61 6d 65 28 69 6e 74 20 6c 6f 63 6b 49 64 78 29  ame(int lockIdx)
1a8d3 7b 0d 0a 20 20 69 66 28 20 6c 6f 63 6b 49 64 78  {..  if( lockIdx
1a8d4 3d 3d 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b  ==WAL_WRITE_LOCK
1a8d5 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
1a8d6 22 57 52 49 54 45 2d 4c 4f 43 4b 22 3b 0d 0a 20  "WRITE-LOCK";.. 
1a8d7 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 49   }else if( lockI
1a8d8 64 78 3d 3d 57 41 4c 5f 43 4b 50 54 5f 4c 4f 43  dx==WAL_CKPT_LOC
1a8d9 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  K ){..    return
1a8da 20 22 43 4b 50 54 2d 4c 4f 43 4b 22 3b 0d 0a 20   "CKPT-LOCK";.. 
1a8db 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 49   }else if( lockI
1a8dc 64 78 3d 3d 57 41 4c 5f 52 45 43 4f 56 45 52 5f  dx==WAL_RECOVER_
1a8dd 4c 4f 43 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74  LOCK ){..    ret
1a8de 75 72 6e 20 22 52 45 43 4f 56 45 52 2d 4c 4f 43  urn "RECOVER-LOC
1a8df 4b 22 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  K";..  }else{.. 
1a8e0 20 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a     static char z
1a8e1 4e 61 6d 65 5b 31 35 5d 3b 0d 0a 20 20 20 20 73  Name[15];..    s
1a8e2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a8e3 73 69 7a 65 6f 66 28 7a 4e 61 6d 65 29 2c 20 7a  sizeof(zName), z
1a8e4 4e 61 6d 65 2c 20 22 52 45 41 44 2d 4c 4f 43 4b  Name, "READ-LOCK
1a8e5 5b 25 64 5d 22 2c 0d 0a 20 20 20 20 20 20 20 20  [%d]",..        
1a8e6 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63               loc
1a8e7 6b 49 64 78 2d 57 41 4c 5f 52 45 41 44 5f 4c 4f  kIdx-WAL_READ_LO
1a8e8 43 4b 28 30 29 29 3b 0d 0a 20 20 20 20 72 65 74  CK(0));..    ret
1a8e9 75 72 6e 20 7a 4e 61 6d 65 3b 0d 0a 20 20 7d 0d  urn zName;..  }.
1a8ea 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 64 65 66  .}..#endif /*def
1a8eb 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
1a8ec 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
1a8ed 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0d 0a 20  ITE_DEBUG) */.. 
1a8ee 20 20 20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65     ..../*..** Se
1a8ef 74 20 6f 72 20 72 65 6c 65 61 73 65 20 6c 6f 63  t or release loc
1a8f0 6b 73 20 6f 6e 20 74 68 65 20 57 41 4c 2e 20 20  ks on the WAL.  
1a8f1 4c 6f 63 6b 73 20 61 72 65 20 65 69 74 68 65 72  Locks are either
1a8f2 20 73 68 61 72 65 64 20 6f 72 20 65 78 63 6c 75   shared or exclu
1a8f3 73 69 76 65 2e 0d 0a 2a 2a 20 41 20 6c 6f 63 6b  sive...** A lock
1a8f4 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64   cannot be moved
1a8f5 20 64 69 72 65 63 74 6c 79 20 62 65 74 77 65 65   directly betwee
1a8f6 6e 20 73 68 61 72 65 64 20 61 6e 64 20 65 78 63  n shared and exc
1a8f7 6c 75 73 69 76 65 20 2d 20 69 74 20 6d 75 73 74  lusive - it must
1a8f8 20 67 6f 0d 0a 2a 2a 20 74 68 72 6f 75 67 68 20   go..** through 
1a8f9 74 68 65 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61  the unlocked sta
1a8fa 74 65 20 66 69 72 73 74 2e 0d 0a 2a 2a 0d 0a 2a  te first...**..*
1a8fb 2a 20 49 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  * In locking_mod
1a8fc 65 3d 45 58 43 4c 55 53 49 56 45 2c 20 61 6c 6c  e=EXCLUSIVE, all
1a8fd 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
1a8fe 65 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73  es become no-ops
1a8ff 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
1a900 74 20 77 61 6c 4c 6f 63 6b 53 68 61 72 65 64 28  t walLockShared(
1a901 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 6c  Wal *pWal, int l
1a902 6f 63 6b 49 64 78 29 7b 0d 0a 20 20 69 6e 74 20  ockIdx){..  int 
1a903 72 63 3b 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d  rc;..  if( pWal-
1a904 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29  >exclusiveMode )
1a905 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1a906 4b 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  K;..  rc = sqlit
1a907 65 33 4f 73 53 68 6d 4c 6f 63 6b 28 70 57 61 6c  e3OsShmLock(pWal
1a908 2d 3e 70 44 62 46 64 2c 20 6c 6f 63 6b 49 64 78  ->pDbFd, lockIdx
1a909 2c 20 31 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  , 1,..          
1a90a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
1a90b 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20  LITE_SHM_LOCK | 
1a90c 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45  SQLITE_SHM_SHARE
1a90d 44 29 3b 0d 0a 20 20 57 41 4c 54 52 41 43 45 28  D);..  WALTRACE(
1a90e 28 22 57 41 4c 25 70 3a 20 61 63 71 75 69 72 65  ("WAL%p: acquire
1a90f 20 53 48 41 52 45 44 2d 25 73 20 25 73 5c 6e 22   SHARED-%s %s\n"
1a910 2c 20 70 57 61 6c 2c 0d 0a 20 20 20 20 20 20 20  , pWal,..       
1a911 20 20 20 20 20 77 61 6c 4c 6f 63 6b 4e 61 6d 65       walLockName
1a912 28 6c 6f 63 6b 49 64 78 29 2c 20 72 63 20 3f 20  (lockIdx), rc ? 
1a913 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29  "failed" : "ok")
1a914 29 3b 0d 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20  );..  VVA_ONLY( 
1a915 70 57 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20  pWal->lockError 
1a916 3d 20 28 75 38 29 28 72 63 21 3d 53 51 4c 49 54  = (u8)(rc!=SQLIT
1a917 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49  E_OK && rc!=SQLI
1a918 54 45 5f 42 55 53 59 29 3b 20 29 0d 0a 20 20 72  TE_BUSY); )..  r
1a919 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 73 74  eturn rc;..}..st
1a91a 61 74 69 63 20 76 6f 69 64 20 77 61 6c 55 6e 6c  atic void walUnl
1a91b 6f 63 6b 53 68 61 72 65 64 28 57 61 6c 20 2a 70  ockShared(Wal *p
1a91c 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49 64 78  Wal, int lockIdx
1a91d 29 7b 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e  ){..  if( pWal->
1a91e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 20  exclusiveMode ) 
1a91f 72 65 74 75 72 6e 3b 0d 0a 20 20 28 76 6f 69 64  return;..  (void
1a920 29 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63  )sqlite3OsShmLoc
1a921 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 6c  k(pWal->pDbFd, l
1a922 6f 63 6b 49 64 78 2c 20 31 2c 0d 0a 20 20 20 20  ockIdx, 1,..    
1a923 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a924 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f       SQLITE_SHM_
1a925 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f  UNLOCK | SQLITE_
1a926 53 48 4d 5f 53 48 41 52 45 44 29 3b 0d 0a 20 20  SHM_SHARED);..  
1a927 57 41 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70  WALTRACE(("WAL%p
1a928 3a 20 72 65 6c 65 61 73 65 20 53 48 41 52 45 44  : release SHARED
1a929 2d 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 77 61  -%s\n", pWal, wa
1a92a 6c 4c 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64  lLockName(lockId
1a92b 78 29 29 29 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63  x)));..}..static
1a92c 20 69 6e 74 20 77 61 6c 4c 6f 63 6b 45 78 63 6c   int walLockExcl
1a92d 75 73 69 76 65 28 57 61 6c 20 2a 70 57 61 6c 2c  usive(Wal *pWal,
1a92e 20 69 6e 74 20 6c 6f 63 6b 49 64 78 2c 20 69 6e   int lockIdx, in
1a92f 74 20 6e 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  t n){..  int rc;
1a930 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e 65 78  ..  if( pWal->ex
1a931 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 20 72 65  clusiveMode ) re
1a932 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
1a933 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1a934 73 53 68 6d 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70  sShmLock(pWal->p
1a935 44 62 46 64 2c 20 6c 6f 63 6b 49 64 78 2c 20 6e  DbFd, lockIdx, n
1a936 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
1a937 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
1a938 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c  E_SHM_LOCK | SQL
1a939 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56  ITE_SHM_EXCLUSIV
1a93a 45 29 3b 0d 0a 20 20 57 41 4c 54 52 41 43 45 28  E);..  WALTRACE(
1a93b 28 22 57 41 4c 25 70 3a 20 61 63 71 75 69 72 65  ("WAL%p: acquire
1a93c 20 45 58 43 4c 55 53 49 56 45 2d 25 73 20 63 6e   EXCLUSIVE-%s cn
1a93d 74 3d 25 64 20 25 73 5c 6e 22 2c 20 70 57 61 6c  t=%d %s\n", pWal
1a93e 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 77  ,..            w
1a93f 61 6c 4c 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49  alLockName(lockI
1a940 64 78 29 2c 20 6e 2c 20 72 63 20 3f 20 22 66 61  dx), n, rc ? "fa
1a941 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0d  iled" : "ok"));.
1a942 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 57 61  .  VVA_ONLY( pWa
1a943 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 3d 20 28  l->lockError = (
1a944 75 38 29 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f  u8)(rc!=SQLITE_O
1a945 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
1a946 42 55 53 59 29 3b 20 29 0d 0a 20 20 72 65 74 75  BUSY); )..  retu
1a947 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 73 74 61 74 69  rn rc;..}..stati
1a948 63 20 76 6f 69 64 20 77 61 6c 55 6e 6c 6f 63 6b  c void walUnlock
1a949 45 78 63 6c 75 73 69 76 65 28 57 61 6c 20 2a 70  Exclusive(Wal *p
1a94a 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49 64 78  Wal, int lockIdx
1a94b 2c 20 69 6e 74 20 6e 29 7b 0d 0a 20 20 69 66 28  , int n){..  if(
1a94c 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65   pWal->exclusive
1a94d 4d 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0d 0a  Mode ) return;..
1a94e 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 4f    (void)sqlite3O
1a94f 73 53 68 6d 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70  sShmLock(pWal->p
1a950 44 62 46 64 2c 20 6c 6f 63 6b 49 64 78 2c 20 6e  DbFd, lockIdx, n
1a951 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
1a952 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
1a953 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20  TE_SHM_UNLOCK | 
1a954 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55  SQLITE_SHM_EXCLU
1a955 53 49 56 45 29 3b 0d 0a 20 20 57 41 4c 54 52 41  SIVE);..  WALTRA
1a956 43 45 28 28 22 57 41 4c 25 70 3a 20 72 65 6c 65  CE(("WAL%p: rele
1a957 61 73 65 20 45 58 43 4c 55 53 49 56 45 2d 25 73  ase EXCLUSIVE-%s
1a958 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 70 57 61 6c   cnt=%d\n", pWal
1a959 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
1a95a 77 61 6c 4c 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b  walLockName(lock
1a95b 49 64 78 29 2c 20 6e 29 29 3b 0d 0a 7d 0d 0a 0d  Idx), n));..}...
1a95c 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ./*..** Compute 
1a95d 61 20 68 61 73 68 20 6f 6e 20 61 20 70 61 67 65  a hash on a page
1a95e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 72 65   number.  The re
1a95f 73 75 6c 74 69 6e 67 20 68 61 73 68 20 76 61 6c  sulting hash val
1a960 75 65 20 6d 75 73 74 20 6c 61 6e 64 0d 0a 2a 2a  ue must land..**
1a961 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 28   between 0 and (
1a962 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 2d  HASHTABLE_NSLOT-
1a963 31 29 2e 20 20 54 68 65 20 77 61 6c 48 61 73 68  1).  The walHash
1a964 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
1a965 61 64 76 61 6e 63 65 73 0d 0a 2a 2a 20 74 68 65  advances..** the
1a966 20 68 61 73 68 20 74 6f 20 74 68 65 20 6e 65 78   hash to the nex
1a967 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 65  t value in the e
1a968 76 65 6e 74 20 6f 66 20 61 20 63 6f 6c 6c 69 73  vent of a collis
1a969 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
1a96a 20 69 6e 74 20 77 61 6c 48 61 73 68 28 75 33 32   int walHash(u32
1a96b 20 69 50 61 67 65 29 7b 0d 0a 20 20 61 73 73 65   iPage){..  asse
1a96c 72 74 28 20 69 50 61 67 65 3e 30 20 29 3b 0d 0a  rt( iPage>0 );..
1a96d 20 20 61 73 73 65 72 74 28 20 28 48 41 53 48 54    assert( (HASHT
1a96e 41 42 4c 45 5f 4e 53 4c 4f 54 20 26 20 28 48 41  ABLE_NSLOT & (HA
1a96f 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 2d 31 29  SHTABLE_NSLOT-1)
1a970 29 3d 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72  )==0 );..  retur
1a971 6e 20 28 69 50 61 67 65 2a 48 41 53 48 54 41 42  n (iPage*HASHTAB
1a972 4c 45 5f 48 41 53 48 5f 31 29 20 26 20 28 48 41  LE_HASH_1) & (HA
1a973 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 2d 31 29  SHTABLE_NSLOT-1)
1a974 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74  ;..}..static int
1a975 20 77 61 6c 4e 65 78 74 48 61 73 68 28 69 6e 74   walNextHash(int
1a976 20 69 50 72 69 6f 72 48 61 73 68 29 7b 0d 0a 20   iPriorHash){.. 
1a977 20 72 65 74 75 72 6e 20 28 69 50 72 69 6f 72 48   return (iPriorH
1a978 61 73 68 2b 31 29 26 28 48 41 53 48 54 41 42 4c  ash+1)&(HASHTABL
1a979 45 5f 4e 53 4c 4f 54 2d 31 29 3b 0d 0a 7d 0d 0a  E_NSLOT-1);..}..
1a97a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 52 65 74 75 72 6e  ../* ..** Return
1a97b 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
1a97c 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
1a97d 70 61 67 65 20 6e 75 6d 62 65 72 20 61 72 72 61  page number arra
1a97e 79 20 73 74 6f 72 65 64 20 6f 6e 0d 0a 2a 2a 20  y stored on..** 
1a97f 70 61 67 65 20 69 48 61 73 68 20 6f 66 20 74 68  page iHash of th
1a980 65 20 77 61 6c 2d 69 6e 64 65 78 2e 20 54 68 65  e wal-index. The
1a981 20 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 62 72   wal-index is br
1a982 6f 6b 65 6e 20 69 6e 74 6f 20 33 32 4b 42 20 70  oken into 32KB p
1a983 61 67 65 73 0d 0a 2a 2a 20 6e 75 6d 62 65 72 65  ages..** numbere
1a984 64 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20  d starting from 
1a985 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 74 20 6f  0...**..** Set o
1a986 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a  utput variable *
1a987 70 61 48 61 73 68 20 74 6f 20 70 6f 69 6e 74 20  paHash to point 
1a988 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
1a989 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0d 0a  the hash table..
1a98a 2a 2a 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e  ** in the wal-in
1a98b 64 65 78 20 66 69 6c 65 2e 20 53 65 74 20 2a 70  dex file. Set *p
1a98c 69 5a 65 72 6f 20 74 6f 20 6f 6e 65 20 6c 65 73  iZero to one les
1a98d 73 20 74 68 61 6e 20 74 68 65 20 66 72 61 6d 65  s than the frame
1a98e 20 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20   ..** number of 
1a98f 74 68 65 20 66 69 72 73 74 20 66 72 61 6d 65 20  the first frame 
1a990 69 6e 64 65 78 65 64 20 62 79 20 74 68 69 73 20  indexed by this 
1a991 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 61  hash table. If a
1a992 0d 0a 2a 2a 20 73 6c 6f 74 20 69 6e 20 74 68 65  ..** slot in the
1a993 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 73   hash table is s
1a994 65 74 20 74 6f 20 4e 2c 20 69 74 20 72 65 66 65  et to N, it refe
1a995 72 73 20 74 6f 20 66 72 61 6d 65 20 6e 75 6d 62  rs to frame numb
1a996 65 72 20 0d 0a 2a 2a 20 28 2a 70 69 5a 65 72 6f  er ..** (*piZero
1a997 2b 4e 29 20 69 6e 20 74 68 65 20 6c 6f 67 2e 0d  +N) in the log..
1a998 0a 2a 2a 0d 0a 2a 2a 20 46 69 6e 61 6c 6c 79 2c  .**..** Finally,
1a999 20 73 65 74 20 2a 70 61 50 67 6e 6f 20 73 6f 20   set *paPgno so 
1a99a 74 68 61 74 20 2a 70 61 50 67 6e 6f 5b 31 5d 20  that *paPgno[1] 
1a99b 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  is the page numb
1a99c 65 72 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 66 69  er of the..** fi
1a99d 72 73 74 20 66 72 61 6d 65 20 69 6e 64 65 78 65  rst frame indexe
1a99e 64 20 62 79 20 74 68 65 20 68 61 73 68 20 74 61  d by the hash ta
1a99f 62 6c 65 2c 20 66 72 61 6d 65 20 28 2a 70 69 5a  ble, frame (*piZ
1a9a0 65 72 6f 2b 31 29 2e 0d 0a 2a 2f 0d 0a 73 74 61  ero+1)...*/..sta
1a9a1 74 69 63 20 69 6e 74 20 77 61 6c 48 61 73 68 47  tic int walHashG
1a9a2 65 74 28 0d 0a 20 20 57 61 6c 20 2a 70 57 61 6c  et(..  Wal *pWal
1a9a3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a9a4 20 20 20 20 20 20 20 2f 2a 20 57 41 4c 20 68 61         /* WAL ha
1a9a5 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ndle */..  int i
1a9a6 48 61 73 68 2c 20 20 20 20 20 20 20 20 20 20 20  Hash,           
1a9a7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
1a9a8 6e 64 20 74 68 65 20 69 48 61 73 68 27 74 68 20  nd the iHash'th 
1a9a9 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 76 6f 6c 61  table */..  vola
1a9aa 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 2a 70  tile ht_slot **p
1a9ab 61 48 61 73 68 2c 20 20 20 20 20 20 2f 2a 20 4f  aHash,      /* O
1a9ac 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 68  UT: Pointer to h
1a9ad 61 73 68 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  ash index */..  
1a9ae 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a 2a 70  volatile u32 **p
1a9af 61 50 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20  aPgno,          
1a9b0 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
1a9b1 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61  to page number a
1a9b2 72 72 61 79 20 2a 2f 0d 0a 20 20 75 33 32 20 2a  rray */..  u32 *
1a9b3 70 69 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20  piZero          
1a9b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
1a9b5 54 3a 20 46 72 61 6d 65 20 61 73 73 6f 63 69 61  T: Frame associa
1a9b6 74 65 64 20 77 69 74 68 20 2a 70 61 50 67 6e 6f  ted with *paPgno
1a9b7 5b 30 5d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  [0] */..){..  in
1a9b8 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
1a9b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a9ba 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   Return code */.
1a9bb 0a 20 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20  .  volatile u32 
1a9bc 2a 61 50 67 6e 6f 3b 0d 0a 0d 0a 20 20 72 63 20  *aPgno;....  rc 
1a9bd 3d 20 77 61 6c 49 6e 64 65 78 50 61 67 65 28 70  = walIndexPage(p
1a9be 57 61 6c 2c 20 69 48 61 73 68 2c 20 26 61 50 67  Wal, iHash, &aPg
1a9bf 6e 6f 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  no);..  assert( 
1a9c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1a9c1 20 69 48 61 73 68 3e 30 20 29 3b 0d 0a 0d 0a 20   iHash>0 );.... 
1a9c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a9c3 4f 4b 20 29 7b 0d 0a 20 20 20 20 75 33 32 20 69  OK ){..    u32 i
1a9c4 5a 65 72 6f 3b 0d 0a 20 20 20 20 76 6f 6c 61 74  Zero;..    volat
1a9c5 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48 61  ile ht_slot *aHa
1a9c6 73 68 3b 0d 0a 0d 0a 20 20 20 20 61 48 61 73 68  sh;....    aHash
1a9c7 20 3d 20 28 76 6f 6c 61 74 69 6c 65 20 68 74 5f   = (volatile ht_
1a9c8 73 6c 6f 74 20 2a 29 26 61 50 67 6e 6f 5b 48 41  slot *)&aPgno[HA
1a9c9 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5d 3b 0d  SHTABLE_NPAGE];.
1a9ca 0a 20 20 20 20 69 66 28 20 69 48 61 73 68 3d 3d  .    if( iHash==
1a9cb 30 20 29 7b 0d 0a 20 20 20 20 20 20 61 50 67 6e  0 ){..      aPgn
1a9cc 6f 20 3d 20 26 61 50 67 6e 6f 5b 57 41 4c 49 4e  o = &aPgno[WALIN
1a9cd 44 45 58 5f 48 44 52 5f 53 49 5a 45 2f 73 69 7a  DEX_HDR_SIZE/siz
1a9ce 65 6f 66 28 75 33 32 29 5d 3b 0d 0a 20 20 20 20  eof(u32)];..    
1a9cf 20 20 69 5a 65 72 6f 20 3d 20 30 3b 0d 0a 20 20    iZero = 0;..  
1a9d0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
1a9d1 69 5a 65 72 6f 20 3d 20 48 41 53 48 54 41 42 4c  iZero = HASHTABL
1a9d2 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 2b 20 28 69  E_NPAGE_ONE + (i
1a9d3 48 61 73 68 2d 31 29 2a 48 41 53 48 54 41 42 4c  Hash-1)*HASHTABL
1a9d4 45 5f 4e 50 41 47 45 3b 0d 0a 20 20 20 20 7d 0d  E_NPAGE;..    }.
1a9d5 0a 20 20 0d 0a 20 20 20 20 2a 70 61 50 67 6e 6f  .  ..    *paPgno
1a9d6 20 3d 20 26 61 50 67 6e 6f 5b 2d 31 5d 3b 0d 0a   = &aPgno[-1];..
1a9d7 20 20 20 20 2a 70 61 48 61 73 68 20 3d 20 61 48      *paHash = aH
1a9d8 61 73 68 3b 0d 0a 20 20 20 20 2a 70 69 5a 65 72  ash;..    *piZer
1a9d9 6f 20 3d 20 69 5a 65 72 6f 3b 0d 0a 20 20 7d 0d  o = iZero;..  }.
1a9da 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
1a9db 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
1a9dc 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1a9dd 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 70 61  the wal-index pa
1a9de 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
1a9df 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 0d   the hash-table.
1a9e0 0a 2a 2a 20 61 6e 64 20 70 61 67 65 2d 6e 75 6d  .** and page-num
1a9e1 62 65 72 20 61 72 72 61 79 20 74 68 61 74 20 63  ber array that c
1a9e2 6f 6e 74 61 69 6e 20 65 6e 74 72 69 65 73 20 63  ontain entries c
1a9e3 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1a9e4 57 41 4c 20 66 72 61 6d 65 0d 0a 2a 2a 20 69 46  WAL frame..** iF
1a9e5 72 61 6d 65 2e 20 54 68 65 20 77 61 6c 2d 69 6e  rame. The wal-in
1a9e6 64 65 78 20 69 73 20 62 72 6f 6b 65 6e 20 75 70  dex is broken up
1a9e7 20 69 6e 74 6f 20 33 32 4b 42 20 70 61 67 65 73   into 32KB pages
1a9e8 2e 20 57 61 6c 2d 69 6e 64 65 78 20 70 61 67 65  . Wal-index page
1a9e9 73 20 0d 0a 2a 2a 20 61 72 65 20 6e 75 6d 62 65  s ..** are numbe
1a9ea 72 65 64 20 73 74 61 72 74 69 6e 67 20 66 72 6f  red starting fro
1a9eb 6d 20 30 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  m 0...*/..static
1a9ec 20 69 6e 74 20 77 61 6c 46 72 61 6d 65 50 61 67   int walFramePag
1a9ed 65 28 75 33 32 20 69 46 72 61 6d 65 29 7b 0d 0a  e(u32 iFrame){..
1a9ee 20 20 69 6e 74 20 69 48 61 73 68 20 3d 20 28 69    int iHash = (i
1a9ef 46 72 61 6d 65 2b 48 41 53 48 54 41 42 4c 45 5f  Frame+HASHTABLE_
1a9f0 4e 50 41 47 45 2d 48 41 53 48 54 41 42 4c 45 5f  NPAGE-HASHTABLE_
1a9f1 4e 50 41 47 45 5f 4f 4e 45 2d 31 29 20 2f 20 48  NPAGE_ONE-1) / H
1a9f2 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 3b 0d  ASHTABLE_NPAGE;.
1a9f3 0a 20 20 61 73 73 65 72 74 28 20 28 69 48 61 73  .  assert( (iHas
1a9f4 68 3d 3d 30 20 7c 7c 20 69 46 72 61 6d 65 3e 48  h==0 || iFrame>H
1a9f5 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f  ASHTABLE_NPAGE_O
1a9f6 4e 45 29 0d 0a 20 20 20 20 20 20 20 26 26 20 28  NE)..       && (
1a9f7 69 48 61 73 68 3e 3d 31 20 7c 7c 20 69 46 72 61  iHash>=1 || iFra
1a9f8 6d 65 3c 3d 48 41 53 48 54 41 42 4c 45 5f 4e 50  me<=HASHTABLE_NP
1a9f9 41 47 45 5f 4f 4e 45 29 0d 0a 20 20 20 20 20 20  AGE_ONE)..      
1a9fa 20 26 26 20 28 69 48 61 73 68 3c 3d 31 20 7c 7c   && (iHash<=1 ||
1a9fb 20 69 46 72 61 6d 65 3e 28 48 41 53 48 54 41 42   iFrame>(HASHTAB
1a9fc 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 2b 48 41 53  LE_NPAGE_ONE+HAS
1a9fd 48 54 41 42 4c 45 5f 4e 50 41 47 45 29 29 0d 0a  HTABLE_NPAGE))..
1a9fe 20 20 20 20 20 20 20 26 26 20 28 69 48 61 73 68         && (iHash
1a9ff 3e 3d 32 20 7c 7c 20 69 46 72 61 6d 65 3c 3d 48  >=2 || iFrame<=H
1aa00 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f  ASHTABLE_NPAGE_O
1aa01 4e 45 2b 48 41 53 48 54 41 42 4c 45 5f 4e 50 41  NE+HASHTABLE_NPA
1aa02 47 45 29 0d 0a 20 20 20 20 20 20 20 26 26 20 28  GE)..       && (
1aa03 69 48 61 73 68 3c 3d 32 20 7c 7c 20 69 46 72 61  iHash<=2 || iFra
1aa04 6d 65 3e 28 48 41 53 48 54 41 42 4c 45 5f 4e 50  me>(HASHTABLE_NP
1aa05 41 47 45 5f 4f 4e 45 2b 32 2a 48 41 53 48 54 41  AGE_ONE+2*HASHTA
1aa06 42 4c 45 5f 4e 50 41 47 45 29 29 0d 0a 20 20 29  BLE_NPAGE))..  )
1aa07 3b 0d 0a 20 20 72 65 74 75 72 6e 20 69 48 61 73  ;..  return iHas
1aa08 68 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  h;..}..../*..** 
1aa09 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20  Return the page 
1aa0a 6e 75 6d 62 65 72 20 61 73 73 6f 63 69 61 74 65  number associate
1aa0b 64 20 77 69 74 68 20 66 72 61 6d 65 20 69 46 72  d with frame iFr
1aa0c 61 6d 65 20 69 6e 20 74 68 69 73 20 57 41 4c 2e  ame in this WAL.
1aa0d 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 75 33 32  ..*/..static u32
1aa0e 20 77 61 6c 46 72 61 6d 65 50 67 6e 6f 28 57 61   walFramePgno(Wa
1aa0f 6c 20 2a 70 57 61 6c 2c 20 75 33 32 20 69 46 72  l *pWal, u32 iFr
1aa10 61 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 69 48 61  ame){..  int iHa
1aa11 73 68 20 3d 20 77 61 6c 46 72 61 6d 65 50 61 67  sh = walFramePag
1aa12 65 28 69 46 72 61 6d 65 29 3b 0d 0a 20 20 69 66  e(iFrame);..  if
1aa13 28 20 69 48 61 73 68 3d 3d 30 20 29 7b 0d 0a 20  ( iHash==0 ){.. 
1aa14 20 20 20 72 65 74 75 72 6e 20 70 57 61 6c 2d 3e     return pWal->
1aa15 61 70 57 69 44 61 74 61 5b 30 5d 5b 57 41 4c 49  apWiData[0][WALI
1aa16 4e 44 45 58 5f 48 44 52 5f 53 49 5a 45 2f 73 69  NDEX_HDR_SIZE/si
1aa17 7a 65 6f 66 28 75 33 32 29 20 2b 20 69 46 72 61  zeof(u32) + iFra
1aa18 6d 65 20 2d 20 31 5d 3b 0d 0a 20 20 7d 0d 0a 20  me - 1];..  }.. 
1aa19 20 72 65 74 75 72 6e 20 70 57 61 6c 2d 3e 61 70   return pWal->ap
1aa1a 57 69 44 61 74 61 5b 69 48 61 73 68 5d 5b 28 69  WiData[iHash][(i
1aa1b 46 72 61 6d 65 2d 31 2d 48 41 53 48 54 41 42 4c  Frame-1-HASHTABL
1aa1c 45 5f 4e 50 41 47 45 5f 4f 4e 45 29 25 48 41 53  E_NPAGE_ONE)%HAS
1aa1d 48 54 41 42 4c 45 5f 4e 50 41 47 45 5d 3b 0d 0a  HTABLE_NPAGE];..
1aa1e 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6d 6f  }..../*..** Remo
1aa1f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ve entries from 
1aa20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
1aa21 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 57 41 4c  hat point to WAL
1aa22 20 73 6c 6f 74 73 20 67 72 65 61 74 65 72 0d 0a   slots greater..
1aa23 2a 2a 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64  ** than pWal->hd
1aa24 72 2e 6d 78 46 72 61 6d 65 2e 0d 0a 2a 2a 0d 0a  r.mxFrame...**..
1aa25 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1aa26 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 65   is called whene
1aa27 76 65 72 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78  ver pWal->hdr.mx
1aa28 46 72 61 6d 65 20 69 73 20 64 65 63 72 65 61 73  Frame is decreas
1aa29 65 64 20 64 75 65 0d 0a 2a 2a 20 74 6f 20 61 20  ed due..** to a 
1aa2a 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 73 61 76 65  rollback or save
1aa2b 70 6f 69 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41  point...**..** A
1aa2c 74 20 6d 6f 73 74 20 6f 6e 6c 79 20 74 68 65 20  t most only the 
1aa2d 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61  hash table conta
1aa2e 69 6e 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e  ining pWal->hdr.
1aa2f 6d 78 46 72 61 6d 65 20 6e 65 65 64 73 20 74 6f  mxFrame needs to
1aa30 20 62 65 0d 0a 2a 2a 20 75 70 64 61 74 65 64 2e   be..** updated.
1aa31 20 20 41 6e 79 20 6c 61 74 65 72 20 68 61 73 68    Any later hash
1aa32 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65 20   tables will be 
1aa33 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c  automatically cl
1aa34 65 61 72 65 64 20 77 68 65 6e 0d 0a 2a 2a 20 70  eared when..** p
1aa35 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1aa36 20 61 64 76 61 6e 63 65 73 20 74 6f 20 74 68 65   advances to the
1aa37 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 6f   point where tho
1aa38 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61  se hash tables a
1aa39 72 65 0d 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20  re..** actually 
1aa3a 6e 65 65 64 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61  needed...*/..sta
1aa3b 74 69 63 20 76 6f 69 64 20 77 61 6c 43 6c 65 61  tic void walClea
1aa3c 6e 75 70 48 61 73 68 28 57 61 6c 20 2a 70 57 61  nupHash(Wal *pWa
1aa3d 6c 29 7b 0d 0a 20 20 76 6f 6c 61 74 69 6c 65 20  l){..  volatile 
1aa3e 68 74 5f 73 6c 6f 74 20 2a 61 48 61 73 68 20 3d  ht_slot *aHash =
1aa3f 20 30 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65   0;    /* Pointe
1aa40 72 20 74 6f 20 68 61 73 68 20 74 61 62 6c 65 20  r to hash table 
1aa41 74 6f 20 63 6c 65 61 72 20 2a 2f 0d 0a 20 20 76  to clear */..  v
1aa42 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a 61 50 67  olatile u32 *aPg
1aa43 6e 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  no = 0;        /
1aa44 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 61 72  * Page number ar
1aa45 72 61 79 20 66 6f 72 20 68 61 73 68 20 74 61 62  ray for hash tab
1aa46 6c 65 20 2a 2f 0d 0a 20 20 75 33 32 20 69 5a 65  le */..  u32 iZe
1aa47 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ro = 0;         
1aa48 20 20 20 20 20 20 20 20 20 2f 2a 20 66 72 61 6d           /* fram
1aa49 65 20 3d 3d 20 28 61 48 61 73 68 5b 78 5d 2b 69  e == (aHash[x]+i
1aa4a 5a 65 72 6f 29 20 2a 2f 0d 0a 20 20 69 6e 74 20  Zero) */..  int 
1aa4b 69 4c 69 6d 69 74 20 3d 20 30 3b 20 20 20 20 20  iLimit = 0;     
1aa4c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a              /* Z
1aa4d 65 72 6f 20 76 61 6c 75 65 73 20 67 72 65 61 74  ero values great
1aa4e 65 72 20 74 68 61 6e 20 74 68 69 73 20 2a 2f 0d  er than this */.
1aa4f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20  .  int nByte;   
1aa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa51 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1aa52 62 79 74 65 73 20 74 6f 20 7a 65 72 6f 20 69 6e  bytes to zero in
1aa53 20 61 50 67 6e 6f 5b 5d 20 2a 2f 0d 0a 20 20 69   aPgno[] */..  i
1aa54 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1aa55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1aa56 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74  * Used to iterat
1aa57 65 20 74 68 72 6f 75 67 68 20 61 48 61 73 68 5b  e through aHash[
1aa58 5d 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  ] */....  assert
1aa59 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63  ( pWal->writeLoc
1aa5a 6b 20 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65  k );..  testcase
1aa5b 28 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  ( pWal->hdr.mxFr
1aa5c 61 6d 65 3d 3d 48 41 53 48 54 41 42 4c 45 5f 4e  ame==HASHTABLE_N
1aa5d 50 41 47 45 5f 4f 4e 45 2d 31 20 29 3b 0d 0a 20  PAGE_ONE-1 );.. 
1aa5e 20 74 65 73 74 63 61 73 65 28 20 70 57 61 6c 2d   testcase( pWal-
1aa5f 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3d 3d 48 41  >hdr.mxFrame==HA
1aa60 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e  SHTABLE_NPAGE_ON
1aa61 45 20 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65  E );..  testcase
1aa62 28 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  ( pWal->hdr.mxFr
1aa63 61 6d 65 3d 3d 48 41 53 48 54 41 42 4c 45 5f 4e  ame==HASHTABLE_N
1aa64 50 41 47 45 5f 4f 4e 45 2b 31 20 29 3b 0d 0a 0d  PAGE_ONE+1 );...
1aa65 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e 68 64 72  .  if( pWal->hdr
1aa66 2e 6d 78 46 72 61 6d 65 3d 3d 30 20 29 20 72 65  .mxFrame==0 ) re
1aa67 74 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 4f 62  turn;....  /* Ob
1aa68 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f  tain pointers to
1aa69 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20   the hash-table 
1aa6a 61 6e 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  and page-number 
1aa6b 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67  array containing
1aa6c 20 0d 0a 20 20 2a 2a 20 74 68 65 20 65 6e 74 72   ..  ** the entr
1aa6d 79 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e  y that correspon
1aa6e 64 73 20 74 6f 20 66 72 61 6d 65 20 70 57 61 6c  ds to frame pWal
1aa6f 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 2e 20 49  ->hdr.mxFrame. I
1aa70 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0d  t is guaranteed.
1aa71 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 70  .  ** that the p
1aa72 61 67 65 20 73 61 69 64 20 68 61 73 68 2d 74 61  age said hash-ta
1aa73 62 6c 65 20 61 6e 64 20 61 72 72 61 79 20 72 65  ble and array re
1aa74 73 69 64 65 20 6f 6e 20 69 73 20 61 6c 72 65 61  side on is alrea
1aa75 64 79 20 6d 61 70 70 65 64 2e 0d 0a 20 20 2a 2f  dy mapped...  */
1aa76 0d 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 6c  ..  assert( pWal
1aa77 2d 3e 6e 57 69 44 61 74 61 3e 77 61 6c 46 72 61  ->nWiData>walFra
1aa78 6d 65 50 61 67 65 28 70 57 61 6c 2d 3e 68 64 72  mePage(pWal->hdr
1aa79 2e 6d 78 46 72 61 6d 65 29 20 29 3b 0d 0a 20 20  .mxFrame) );..  
1aa7a 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 61 70  assert( pWal->ap
1aa7b 57 69 44 61 74 61 5b 77 61 6c 46 72 61 6d 65 50  WiData[walFrameP
1aa7c 61 67 65 28 70 57 61 6c 2d 3e 68 64 72 2e 6d 78  age(pWal->hdr.mx
1aa7d 46 72 61 6d 65 29 5d 20 29 3b 0d 0a 20 20 77 61  Frame)] );..  wa
1aa7e 6c 48 61 73 68 47 65 74 28 70 57 61 6c 2c 20 77  lHashGet(pWal, w
1aa7f 61 6c 46 72 61 6d 65 50 61 67 65 28 70 57 61 6c  alFramePage(pWal
1aa80 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 29 2c 20  ->hdr.mxFrame), 
1aa81 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20  &aHash, &aPgno, 
1aa82 26 69 5a 65 72 6f 29 3b 0d 0a 0d 0a 20 20 2f 2a  &iZero);....  /*
1aa83 20 5a 65 72 6f 20 61 6c 6c 20 68 61 73 68 2d 74   Zero all hash-t
1aa84 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61  able entries tha
1aa85 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  t correspond to 
1aa86 66 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 67 72  frame numbers gr
1aa87 65 61 74 65 72 0d 0a 20 20 2a 2a 20 74 68 61 6e  eater..  ** than
1aa88 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61   pWal->hdr.mxFra
1aa89 6d 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 4c 69  me...  */..  iLi
1aa8a 6d 69 74 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e  mit = pWal->hdr.
1aa8b 6d 78 46 72 61 6d 65 20 2d 20 69 5a 65 72 6f 3b  mxFrame - iZero;
1aa8c 0d 0a 20 20 61 73 73 65 72 74 28 20 69 4c 69 6d  ..  assert( iLim
1aa8d 69 74 3e 30 20 29 3b 0d 0a 20 20 66 6f 72 28 69  it>0 );..  for(i
1aa8e 3d 30 3b 20 69 3c 48 41 53 48 54 41 42 4c 45 5f  =0; i<HASHTABLE_
1aa8f 4e 53 4c 4f 54 3b 20 69 2b 2b 29 7b 0d 0a 20 20  NSLOT; i++){..  
1aa90 20 20 69 66 28 20 61 48 61 73 68 5b 69 5d 3e 69    if( aHash[i]>i
1aa91 4c 69 6d 69 74 20 29 7b 0d 0a 20 20 20 20 20 20  Limit ){..      
1aa92 61 48 61 73 68 5b 69 5d 20 3d 20 30 3b 0d 0a 20  aHash[i] = 0;.. 
1aa93 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d 0a 20     }..  }..  .. 
1aa94 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 65 6e 74   /* Zero the ent
1aa95 72 69 65 73 20 69 6e 20 74 68 65 20 61 50 67 6e  ries in the aPgn
1aa96 6f 20 61 72 72 61 79 20 74 68 61 74 20 63 6f 72  o array that cor
1aa97 72 65 73 70 6f 6e 64 20 74 6f 20 66 72 61 6d 65  respond to frame
1aa98 73 20 77 69 74 68 0d 0a 20 20 2a 2a 20 66 72 61  s with..  ** fra
1aa99 6d 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61 74  me numbers great
1aa9a 65 72 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64  er than pWal->hd
1aa9b 72 2e 6d 78 46 72 61 6d 65 2e 20 0d 0a 20 20 2a  r.mxFrame. ..  *
1aa9c 2f 0d 0a 20 20 6e 42 79 74 65 20 3d 20 28 69 6e  /..  nByte = (in
1aa9d 74 29 28 28 63 68 61 72 20 2a 29 61 48 61 73 68  t)((char *)aHash
1aa9e 20 2d 20 28 63 68 61 72 20 2a 29 26 61 50 67 6e   - (char *)&aPgn
1aa9f 6f 5b 69 4c 69 6d 69 74 2b 31 5d 29 3b 0d 0a 20  o[iLimit+1]);.. 
1aaa0 20 6d 65 6d 73 65 74 28 28 76 6f 69 64 20 2a 29   memset((void *)
1aaa1 26 61 50 67 6e 6f 5b 69 4c 69 6d 69 74 2b 31 5d  &aPgno[iLimit+1]
1aaa2 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0d 0a 0d 0a  , 0, nByte);....
1aaa3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
1aaa4 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41  ABLE_EXPENSIVE_A
1aaa5 53 53 45 52 54 0d 0a 20 20 2f 2a 20 56 65 72 69  SSERT..  /* Veri
1aaa6 66 79 20 74 68 61 74 20 74 68 65 20 65 76 65 72  fy that the ever
1aaa7 79 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6d  y entry in the m
1aaa8 61 70 70 69 6e 67 20 72 65 67 69 6f 6e 20 69 73  apping region is
1aaa9 20 73 74 69 6c 6c 20 72 65 61 63 68 61 62 6c 65   still reachable
1aaaa 0d 0a 20 20 2a 2a 20 76 69 61 20 74 68 65 20 68  ..  ** via the h
1aaab 61 73 68 20 74 61 62 6c 65 20 65 76 65 6e 20 61  ash table even a
1aaac 66 74 65 72 20 74 68 65 20 63 6c 65 61 6e 75 70  fter the cleanup
1aaad 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69  ...  */..  if( i
1aaae 4c 69 6d 69 74 20 29 7b 0d 0a 20 20 20 20 69 6e  Limit ){..    in
1aaaf 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
1aab0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1aab1 2f 0d 0a 20 20 20 20 69 6e 74 20 69 4b 65 79 3b  /..    int iKey;
1aab2 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
1aab3 6b 65 79 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28  key */..    for(
1aab4 69 3d 31 3b 20 69 3c 3d 69 4c 69 6d 69 74 3b 20  i=1; i<=iLimit; 
1aab5 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 66 6f 72  i++){..      for
1aab6 28 69 4b 65 79 3d 77 61 6c 48 61 73 68 28 61 50  (iKey=walHash(aP
1aab7 67 6e 6f 5b 69 5d 29 3b 20 61 48 61 73 68 5b 69  gno[i]); aHash[i
1aab8 4b 65 79 5d 3b 20 69 4b 65 79 3d 77 61 6c 4e 65  Key]; iKey=walNe
1aab9 78 74 48 61 73 68 28 69 4b 65 79 29 29 7b 0d 0a  xtHash(iKey)){..
1aaba 20 20 20 20 20 20 20 20 69 66 28 20 61 48 61 73          if( aHas
1aabb 68 5b 69 4b 65 79 5d 3d 3d 69 20 29 20 62 72 65  h[iKey]==i ) bre
1aabc 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
1aabd 20 20 20 20 61 73 73 65 72 74 28 20 61 48 61 73      assert( aHas
1aabe 68 5b 69 4b 65 79 5d 3d 3d 69 20 29 3b 0d 0a 20  h[iKey]==i );.. 
1aabf 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69     }..  }..#endi
1aac0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
1aac1 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53  LE_EXPENSIVE_ASS
1aac2 45 52 54 20 2a 2f 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  ERT */..}....../
1aac3 2a 0d 0a 2a 2a 20 53 65 74 20 61 6e 20 65 6e 74  *..** Set an ent
1aac4 72 79 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e  ry in the wal-in
1aac5 64 65 78 20 74 68 61 74 20 77 69 6c 6c 20 6d 61  dex that will ma
1aac6 70 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  p database page 
1aac7 6e 75 6d 62 65 72 0d 0a 2a 2a 20 70 50 61 67 65  number..** pPage
1aac8 20 69 6e 74 6f 20 57 41 4c 20 66 72 61 6d 65 20   into WAL frame 
1aac9 69 46 72 61 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  iFrame...*/..sta
1aaca 74 69 63 20 69 6e 74 20 77 61 6c 49 6e 64 65 78  tic int walIndex
1aacb 41 70 70 65 6e 64 28 57 61 6c 20 2a 70 57 61 6c  Append(Wal *pWal
1aacc 2c 20 75 33 32 20 69 46 72 61 6d 65 2c 20 75 33  , u32 iFrame, u3
1aacd 32 20 69 50 61 67 65 29 7b 0d 0a 20 20 69 6e 74  2 iPage){..  int
1aace 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1aacf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1aad0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
1aad1 20 20 75 33 32 20 69 5a 65 72 6f 20 3d 20 30 3b    u32 iZero = 0;
1aad2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aad3 20 20 2f 2a 20 4f 6e 65 20 6c 65 73 73 20 74 68    /* One less th
1aad4 61 6e 20 66 72 61 6d 65 20 6e 75 6d 62 65 72 20  an frame number 
1aad5 6f 66 20 61 50 67 6e 6f 5b 31 5d 20 2a 2f 0d 0a  of aPgno[1] */..
1aad6 20 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a    volatile u32 *
1aad7 61 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 20  aPgno = 0;      
1aad8 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
1aad9 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 76 6f 6c   array */..  vol
1aada 61 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61  atile ht_slot *a
1aadb 48 61 73 68 20 3d 20 30 3b 20 20 20 20 2f 2a 20  Hash = 0;    /* 
1aadc 48 61 73 68 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d  Hash table */...
1aadd 0a 20 20 72 63 20 3d 20 77 61 6c 48 61 73 68 47  .  rc = walHashG
1aade 65 74 28 70 57 61 6c 2c 20 77 61 6c 46 72 61 6d  et(pWal, walFram
1aadf 65 50 61 67 65 28 69 46 72 61 6d 65 29 2c 20 26  ePage(iFrame), &
1aae0 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26  aHash, &aPgno, &
1aae1 69 5a 65 72 6f 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  iZero);....  /* 
1aae2 41 73 73 75 6d 69 6e 67 20 74 68 65 20 77 61 6c  Assuming the wal
1aae3 2d 69 6e 64 65 78 20 66 69 6c 65 20 77 61 73 20  -index file was 
1aae4 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6d 61 70  successfully map
1aae5 70 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  ped, populate th
1aae6 65 0d 0a 20 20 2a 2a 20 70 61 67 65 20 6e 75 6d  e..  ** page num
1aae7 62 65 72 20 61 72 72 61 79 20 61 6e 64 20 68 61  ber array and ha
1aae8 73 68 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 0d  sh table entry..
1aae9 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 3d  .  */..  if( rc=
1aaea 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
1aaeb 20 20 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20     int iKey;    
1aaec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aaed 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6b   /* Hash table k
1aaee 65 79 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69  ey */..    int i
1aaef 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
1aaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1aaf1 65 20 74 6f 20 77 72 69 74 65 20 74 6f 20 68 61  e to write to ha
1aaf2 73 68 2d 74 61 62 6c 65 20 73 6c 6f 74 20 2a 2f  sh-table slot */
1aaf3 0d 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 6c 69  ..    int nColli
1aaf4 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
1aaf5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1aaf6 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 73   hash collisions
1aaf7 20 2a 2f 0d 0a 0d 0a 20 20 20 20 69 64 78 20 3d   */....    idx =
1aaf8 20 69 46 72 61 6d 65 20 2d 20 69 5a 65 72 6f 3b   iFrame - iZero;
1aaf9 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64  ..    assert( id
1aafa 78 20 3c 3d 20 48 41 53 48 54 41 42 4c 45 5f 4e  x <= HASHTABLE_N
1aafb 53 4c 4f 54 2f 32 20 2b 20 31 20 29 3b 0d 0a 20  SLOT/2 + 1 );.. 
1aafc 20 20 20 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74     ..    /* If t
1aafd 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74  his is the first
1aafe 20 65 6e 74 72 79 20 74 6f 20 62 65 20 61 64 64   entry to be add
1aaff 65 64 20 74 6f 20 74 68 69 73 20 68 61 73 68 2d  ed to this hash-
1ab00 74 61 62 6c 65 2c 20 7a 65 72 6f 20 74 68 65 0d  table, zero the.
1ab01 0a 20 20 20 20 2a 2a 20 65 6e 74 69 72 65 20 68  .    ** entire h
1ab02 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 61 50  ash table and aP
1ab03 67 6e 6f 5b 5d 20 61 72 72 61 79 20 62 65 66 6f  gno[] array befo
1ab04 72 65 20 70 72 6f 63 65 64 69 6e 67 2e 20 0d 0a  re proceding. ..
1ab05 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
1ab06 69 64 78 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20  idx==1 ){..     
1ab07 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 69 6e   int nByte = (in
1ab08 74 29 28 28 75 38 20 2a 29 26 61 48 61 73 68 5b  t)((u8 *)&aHash[
1ab09 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 5d  HASHTABLE_NSLOT]
1ab0a 20 2d 20 28 75 38 20 2a 29 26 61 50 67 6e 6f 5b   - (u8 *)&aPgno[
1ab0b 31 5d 29 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 73  1]);..      mems
1ab0c 65 74 28 28 76 6f 69 64 2a 29 26 61 50 67 6e 6f  et((void*)&aPgno
1ab0d 5b 31 5d 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0d  [1], 0, nByte);.
1ab0e 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
1ab0f 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 69 6e   If the entry in
1ab10 20 61 50 67 6e 6f 5b 5d 20 69 73 20 61 6c 72 65   aPgno[] is alre
1ab11 61 64 79 20 73 65 74 2c 20 74 68 65 6e 20 74 68  ady set, then th
1ab12 65 20 70 72 65 76 69 6f 75 73 20 77 72 69 74 65  e previous write
1ab13 72 0d 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 68  r..    ** must h
1ab14 61 76 65 20 65 78 69 74 65 64 20 75 6e 65 78 70  ave exited unexp
1ab15 65 63 74 65 64 6c 79 20 69 6e 20 74 68 65 20 6d  ectedly in the m
1ab16 69 64 64 6c 65 20 6f 66 20 61 20 74 72 61 6e 73  iddle of a trans
1ab17 61 63 74 69 6f 6e 20 28 61 66 74 65 72 0d 0a 20  action (after.. 
1ab18 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 6f 6e     ** writing on
1ab19 65 20 6f 72 20 6d 6f 72 65 20 64 69 72 74 79 20  e or more dirty 
1ab1a 70 61 67 65 73 20 74 6f 20 74 68 65 20 57 41 4c  pages to the WAL
1ab1b 20 74 6f 20 66 72 65 65 20 75 70 20 6d 65 6d 6f   to free up memo
1ab1c 72 79 29 2e 20 0d 0a 20 20 20 20 2a 2a 20 52 65  ry). ..    ** Re
1ab1d 6d 6f 76 65 20 74 68 65 20 72 65 6d 6e 61 6e 74  move the remnant
1ab1e 73 20 6f 66 20 74 68 61 74 20 77 72 69 74 65 72  s of that writer
1ab1f 73 20 75 6e 63 6f 6d 6d 69 74 74 65 64 20 74 72  s uncommitted tr
1ab20 61 6e 73 61 63 74 69 6f 6e 20 66 72 6f 6d 20 0d  ansaction from .
1ab21 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 61 73 68  .    ** the hash
1ab22 2d 74 61 62 6c 65 20 62 65 66 6f 72 65 20 77 72  -table before wr
1ab23 69 74 69 6e 67 20 61 6e 79 20 6e 65 77 20 65 6e  iting any new en
1ab24 74 72 69 65 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  tries...    */..
1ab25 20 20 20 20 69 66 28 20 61 50 67 6e 6f 5b 69 64      if( aPgno[id
1ab26 78 5d 20 29 7b 0d 0a 20 20 20 20 20 20 77 61 6c  x] ){..      wal
1ab27 43 6c 65 61 6e 75 70 48 61 73 68 28 70 57 61 6c  CleanupHash(pWal
1ab28 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
1ab29 28 20 21 61 50 67 6e 6f 5b 69 64 78 5d 20 29 3b  ( !aPgno[idx] );
1ab2a 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
1ab2b 2a 20 57 72 69 74 65 20 74 68 65 20 61 50 67 6e  * Write the aPgn
1ab2c 6f 5b 5d 20 61 72 72 61 79 20 65 6e 74 72 79 20  o[] array entry 
1ab2d 61 6e 64 20 74 68 65 20 68 61 73 68 2d 74 61 62  and the hash-tab
1ab2e 6c 65 20 73 6c 6f 74 2e 20 2a 2f 0d 0a 20 20 20  le slot. */..   
1ab2f 20 6e 43 6f 6c 6c 69 64 65 20 3d 20 69 64 78 3b   nCollide = idx;
1ab30 0d 0a 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 77  ..    for(iKey=w
1ab31 61 6c 48 61 73 68 28 69 50 61 67 65 29 3b 20 61  alHash(iPage); a
1ab32 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 4b 65 79  Hash[iKey]; iKey
1ab33 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 69 4b 65  =walNextHash(iKe
1ab34 79 29 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  y)){..      if( 
1ab35 28 6e 43 6f 6c 6c 69 64 65 2d 2d 29 3d 3d 30 20  (nCollide--)==0 
1ab36 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1ab37 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20  CORRUPT_BKPT;.. 
1ab38 20 20 20 7d 0d 0a 20 20 20 20 61 50 67 6e 6f 5b     }..    aPgno[
1ab39 69 64 78 5d 20 3d 20 69 50 61 67 65 3b 0d 0a 20  idx] = iPage;.. 
1ab3a 20 20 20 61 48 61 73 68 5b 69 4b 65 79 5d 20 3d     aHash[iKey] =
1ab3b 20 28 68 74 5f 73 6c 6f 74 29 69 64 78 3b 0d 0a   (ht_slot)idx;..
1ab3c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1ab3d 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45  ENABLE_EXPENSIVE
1ab3e 5f 41 53 53 45 52 54 0d 0a 20 20 20 20 2f 2a 20  _ASSERT..    /* 
1ab3f 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
1ab40 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
1ab41 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  s in the hash ta
1ab42 62 6c 65 20 65 78 61 63 74 6c 79 20 65 71 75 61  ble exactly equa
1ab43 6c 73 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e  ls..    ** the n
1ab44 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
1ab45 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e 67 20   in the mapping 
1ab46 72 65 67 69 6f 6e 2e 0d 0a 20 20 20 20 2a 2f 0d  region...    */.
1ab47 0a 20 20 20 20 7b 0d 0a 20 20 20 20 20 20 69 6e  .    {..      in
1ab48 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
1ab49 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1ab4a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 6e  /..      int nEn
1ab4b 74 72 79 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d  try = 0;  /* Num
1ab4c 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
1ab4d 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
1ab4e 20 2a 2f 0d 0a 20 20 20 20 20 20 66 6f 72 28 69   */..      for(i
1ab4f 3d 30 3b 20 69 3c 48 41 53 48 54 41 42 4c 45 5f  =0; i<HASHTABLE_
1ab50 4e 53 4c 4f 54 3b 20 69 2b 2b 29 7b 20 69 66 28  NSLOT; i++){ if(
1ab51 20 61 48 61 73 68 5b 69 5d 20 29 20 6e 45 6e 74   aHash[i] ) nEnt
1ab52 72 79 2b 2b 3b 20 7d 0d 0a 20 20 20 20 20 20 61  ry++; }..      a
1ab53 73 73 65 72 74 28 20 6e 45 6e 74 72 79 3d 3d 69  ssert( nEntry==i
1ab54 64 78 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  dx );..    }....
1ab55 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68      /* Verify th
1ab56 61 74 20 74 68 65 20 65 76 65 72 79 20 65 6e 74  at the every ent
1ab57 72 79 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e  ry in the mappin
1ab58 67 20 72 65 67 69 6f 6e 20 69 73 20 72 65 61 63  g region is reac
1ab59 68 61 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 76 69  hable..    ** vi
1ab5a 61 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  a the hash table
1ab5b 2e 20 20 54 68 69 73 20 74 75 72 6e 73 20 6f 75  .  This turns ou
1ab5c 74 20 74 6f 20 62 65 20 61 20 72 65 61 6c 6c 79  t to be a really
1ab5d 2c 20 72 65 61 6c 6c 79 20 65 78 70 65 6e 73 69  , really expensi
1ab5e 76 65 0d 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67  ve..    ** thing
1ab5f 20 74 6f 20 63 68 65 63 6b 2c 20 73 6f 20 6f 6e   to check, so on
1ab60 6c 79 20 64 6f 20 74 68 69 73 20 6f 63 63 61 73  ly do this occas
1ab61 69 6f 6e 61 6c 6c 79 20 2d 20 6e 6f 74 20 6f 6e  ionally - not on
1ab62 20 65 76 65 72 79 0d 0a 20 20 20 20 2a 2a 20 69   every..    ** i
1ab63 74 65 72 61 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a  teration...    *
1ab64 2f 0d 0a 20 20 20 20 69 66 28 20 28 69 64 78 26  /..    if( (idx&
1ab65 30 78 33 66 66 29 3d 3d 30 20 29 7b 0d 0a 20 20  0x3ff)==0 ){..  
1ab66 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
1ab67 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1ab68 6e 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20 66  nter */..      f
1ab69 6f 72 28 69 3d 31 3b 20 69 3c 3d 69 64 78 3b 20  or(i=1; i<=idx; 
1ab6a 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 66  i++){..        f
1ab6b 6f 72 28 69 4b 65 79 3d 77 61 6c 48 61 73 68 28  or(iKey=walHash(
1ab6c 61 50 67 6e 6f 5b 69 5d 29 3b 20 61 48 61 73 68  aPgno[i]); aHash
1ab6d 5b 69 4b 65 79 5d 3b 20 69 4b 65 79 3d 77 61 6c  [iKey]; iKey=wal
1ab6e 4e 65 78 74 48 61 73 68 28 69 4b 65 79 29 29 7b  NextHash(iKey)){
1ab6f 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
1ab70 61 48 61 73 68 5b 69 4b 65 79 5d 3d 3d 69 20 29  aHash[iKey]==i )
1ab71 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
1ab72 20 7d 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65   }..        asse
1ab73 72 74 28 20 61 48 61 73 68 5b 69 4b 65 79 5d 3d  rt( aHash[iKey]=
1ab74 3d 69 20 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  =i );..      }..
1ab75 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a      }..#endif /*
1ab76 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45   SQLITE_ENABLE_E
1ab77 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 20  XPENSIVE_ASSERT 
1ab78 2a 2f 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20 20 72  */..  }......  r
1ab79 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1ab7a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 63 6f 76 65 72  ../*..** Recover
1ab7b 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 62   the wal-index b
1ab7c 79 20 72 65 61 64 69 6e 67 20 74 68 65 20 77 72  y reading the wr
1ab7d 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 66 69  ite-ahead log fi
1ab7e 6c 65 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  le. ..**..** Thi
1ab7f 73 20 72 6f 75 74 69 6e 65 20 66 69 72 73 74 20  s routine first 
1ab80 74 72 69 65 73 20 74 6f 20 65 73 74 61 62 6c 69  tries to establi
1ab81 73 68 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  sh an exclusive 
1ab82 6c 6f 63 6b 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20  lock on the..** 
1ab83 77 61 6c 2d 69 6e 64 65 78 20 74 6f 20 70 72 65  wal-index to pre
1ab84 76 65 6e 74 20 6f 74 68 65 72 20 74 68 72 65 61  vent other threa
1ab85 64 73 2f 70 72 6f 63 65 73 73 65 73 20 66 72 6f  ds/processes fro
1ab86 6d 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  m doing anything
1ab87 0d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 57 41  ..** with the WA
1ab88 4c 20 6f 72 20 77 61 6c 2d 69 6e 64 65 78 20 77  L or wal-index w
1ab89 68 69 6c 65 20 72 65 63 6f 76 65 72 79 20 69 73  hile recovery is
1ab8a 20 72 75 6e 6e 69 6e 67 2e 20 20 54 68 65 0d 0a   running.  The..
1ab8b 2a 2a 20 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c  ** WAL_RECOVER_L
1ab8c 4f 43 4b 20 69 73 20 61 6c 73 6f 20 68 65 6c 64  OCK is also held
1ab8d 20 73 6f 20 74 68 61 74 20 6f 74 68 65 72 20 74   so that other t
1ab8e 68 72 65 61 64 73 20 77 69 6c 6c 20 6b 6e 6f 77  hreads will know
1ab8f 0d 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 74  ..** that this t
1ab90 68 72 65 61 64 20 69 73 20 72 75 6e 6e 69 6e 67  hread is running
1ab91 20 72 65 63 6f 76 65 72 79 2e 20 20 49 66 20 75   recovery.  If u
1ab92 6e 61 62 6c 65 20 74 6f 20 65 73 74 61 62 6c 69  nable to establi
1ab93 73 68 0d 0a 2a 2a 20 74 68 65 20 6e 65 63 65 73  sh..** the neces
1ab94 73 61 72 79 20 6c 6f 63 6b 73 2c 20 74 68 69 73  sary locks, this
1ab95 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
1ab96 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0d 0a 2a   SQLITE_BUSY...*
1ab97 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61  /..static int wa
1ab98 6c 49 6e 64 65 78 52 65 63 6f 76 65 72 28 57 61  lIndexRecover(Wa
1ab99 6c 20 2a 70 57 61 6c 29 7b 0d 0a 20 20 69 6e 74  l *pWal){..  int
1ab9a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1ab9b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ab9c 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a  Return Code */..
1ab9d 20 20 69 36 34 20 6e 53 69 7a 65 3b 20 20 20 20    i64 nSize;    
1ab9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab9f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6c 6f 67    /* Size of log
1aba0 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 75 33 32 20   file */..  u32 
1aba1 61 46 72 61 6d 65 43 6b 73 75 6d 5b 32 5d 20 3d  aFrameCksum[2] =
1aba2 20 7b 30 2c 20 30 7d 3b 0d 0a 20 20 69 6e 74 20   {0, 0};..  int 
1aba3 69 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  iLock;          
1aba4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1aba5 6f 63 6b 20 6f 66 66 73 65 74 20 74 6f 20 6c 6f  ock offset to lo
1aba6 63 6b 20 66 6f 72 20 63 68 65 63 6b 70 6f 69 6e  ck for checkpoin
1aba7 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4c 6f 63  t */..  int nLoc
1aba8 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
1aba9 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1abaa 72 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 68 6f  r of locks to ho
1abab 6c 64 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 4f 62  ld */....  /* Ob
1abac 74 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76  tain an exclusiv
1abad 65 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 20 62 79  e lock on all by
1abae 74 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e  te in the lockin
1abaf 67 20 72 61 6e 67 65 20 6e 6f 74 20 61 6c 72 65  g range not alre
1abb0 61 64 79 0d 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64  ady..  ** locked
1abb1 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
1abb2 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 67 75  The caller is gu
1abb3 61 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65  aranteed to have
1abb4 20 6c 6f 63 6b 65 64 20 74 68 65 0d 0a 20 20 2a   locked the..  *
1abb5 2a 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b  * WAL_WRITE_LOCK
1abb6 20 62 79 74 65 2c 20 61 6e 64 20 6d 61 79 20 68   byte, and may h
1abb7 61 76 65 20 61 6c 73 6f 20 6c 6f 63 6b 65 64 20  ave also locked 
1abb8 74 68 65 20 57 41 4c 5f 43 4b 50 54 5f 4c 4f 43  the WAL_CKPT_LOC
1abb9 4b 20 62 79 74 65 2e 0d 0a 20 20 2a 2a 20 49 66  K byte...  ** If
1abba 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
1abbb 20 73 61 6d 65 20 62 79 74 65 73 20 74 68 61 74   same bytes that
1abbc 20 61 72 65 20 6c 6f 63 6b 65 64 20 68 65 72 65   are locked here
1abbd 20 61 72 65 20 75 6e 6c 6f 63 6b 65 64 20 62 65   are unlocked be
1abbe 66 6f 72 65 0d 0a 20 20 2a 2a 20 74 68 69 73 20  fore..  ** this 
1abbf 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1abc0 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72  ...  */..  asser
1abc1 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c 6f 63  t( pWal->ckptLoc
1abc2 6b 3d 3d 31 20 7c 7c 20 70 57 61 6c 2d 3e 63 6b  k==1 || pWal->ck
1abc3 70 74 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a 20 20  ptLock==0 );..  
1abc4 61 73 73 65 72 74 28 20 57 41 4c 5f 41 4c 4c 5f  assert( WAL_ALL_
1abc5 42 55 54 5f 57 52 49 54 45 3d 3d 57 41 4c 5f 57  BUT_WRITE==WAL_W
1abc6 52 49 54 45 5f 4c 4f 43 4b 2b 31 20 29 3b 0d 0a  RITE_LOCK+1 );..
1abc7 20 20 61 73 73 65 72 74 28 20 57 41 4c 5f 43 4b    assert( WAL_CK
1abc8 50 54 5f 4c 4f 43 4b 3d 3d 57 41 4c 5f 41 4c 4c  PT_LOCK==WAL_ALL
1abc9 5f 42 55 54 5f 57 52 49 54 45 20 29 3b 0d 0a 20  _BUT_WRITE );.. 
1abca 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77   assert( pWal->w
1abcb 72 69 74 65 4c 6f 63 6b 20 29 3b 0d 0a 20 20 69  riteLock );..  i
1abcc 4c 6f 63 6b 20 3d 20 57 41 4c 5f 41 4c 4c 5f 42  Lock = WAL_ALL_B
1abcd 55 54 5f 57 52 49 54 45 20 2b 20 70 57 61 6c 2d  UT_WRITE + pWal-
1abce 3e 63 6b 70 74 4c 6f 63 6b 3b 0d 0a 20 20 6e 4c  >ckptLock;..  nL
1abcf 6f 63 6b 20 3d 20 53 51 4c 49 54 45 5f 53 48 4d  ock = SQLITE_SHM
1abd0 5f 4e 4c 4f 43 4b 20 2d 20 69 4c 6f 63 6b 3b 0d  _NLOCK - iLock;.
1abd1 0a 20 20 72 63 20 3d 20 77 61 6c 4c 6f 63 6b 45  .  rc = walLockE
1abd2 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 69  xclusive(pWal, i
1abd3 4c 6f 63 6b 2c 20 6e 4c 6f 63 6b 29 3b 0d 0a 20  Lock, nLock);.. 
1abd4 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
1abd5 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d  return rc;..  }.
1abd6 0a 20 20 57 41 4c 54 52 41 43 45 28 28 22 57 41  .  WALTRACE(("WA
1abd7 4c 25 70 3a 20 72 65 63 6f 76 65 72 79 20 62 65  L%p: recovery be
1abd8 67 69 6e 2e 2e 2e 5c 6e 22 2c 20 70 57 61 6c 29  gin...\n", pWal)
1abd9 29 3b 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 26  );....  memset(&
1abda 70 57 61 6c 2d 3e 68 64 72 2c 20 30 2c 20 73 69  pWal->hdr, 0, si
1abdb 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72  zeof(WalIndexHdr
1abdc 29 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71  ));....  rc = sq
1abdd 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
1abde 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20 26 6e  pWal->pWalFd, &n
1abdf 53 69 7a 65 29 3b 0d 0a 20 20 69 66 28 20 72 63  Size);..  if( rc
1abe0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
1abe1 20 20 20 20 67 6f 74 6f 20 72 65 63 6f 76 65 72      goto recover
1abe2 79 5f 65 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 0d  y_error;..  }...
1abe3 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 57 41 4c  .  if( nSize>WAL
1abe4 5f 48 44 52 53 49 5a 45 20 29 7b 0d 0a 20 20 20  _HDRSIZE ){..   
1abe5 20 75 38 20 61 42 75 66 5b 57 41 4c 5f 48 44 52   u8 aBuf[WAL_HDR
1abe6 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 20 20 2f  SIZE];         /
1abe7 2a 20 42 75 66 66 65 72 20 74 6f 20 6c 6f 61 64  * Buffer to load
1abe8 20 57 41 4c 20 68 65 61 64 65 72 20 69 6e 74 6f   WAL header into
1abe9 20 2a 2f 0d 0a 20 20 20 20 75 38 20 2a 61 46 72   */..    u8 *aFr
1abea 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ame = 0;        
1abeb 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63         /* Malloc
1abec 27 64 20 62 75 66 66 65 72 20 74 6f 20 6c 6f 61  'd buffer to loa
1abed 64 20 65 6e 74 69 72 65 20 66 72 61 6d 65 20 2a  d entire frame *
1abee 2f 0d 0a 20 20 20 20 69 6e 74 20 73 7a 46 72 61  /..    int szFra
1abef 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
1abf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1abf1 66 20 62 79 74 65 73 20 69 6e 20 62 75 66 66 65  f bytes in buffe
1abf2 72 20 61 46 72 61 6d 65 5b 5d 20 2a 2f 0d 0a 20  r aFrame[] */.. 
1abf3 20 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20     u8 *aData;   
1abf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abf5 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64   /* Pointer to d
1abf6 61 74 61 20 70 61 72 74 20 6f 66 20 61 46 72 61  ata part of aFra
1abf7 6d 65 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20  me buffer */..  
1abf8 20 20 69 6e 74 20 69 46 72 61 6d 65 3b 20 20 20    int iFrame;   
1abf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abfa 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 61 73 74  /* Index of last
1abfb 20 66 72 61 6d 65 20 72 65 61 64 20 2a 2f 0d 0a   frame read */..
1abfc 20 20 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b      i64 iOffset;
1abfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abfe 20 20 2f 2a 20 4e 65 78 74 20 6f 66 66 73 65 74    /* Next offset
1abff 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 6c 6f   to read from lo
1ac00 67 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20 69  g file */..    i
1ac01 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20  nt szPage;      
1ac02 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ac03 50 61 67 65 20 73 69 7a 65 20 61 63 63 6f 72 64  Page size accord
1ac04 69 6e 67 20 74 6f 20 74 68 65 20 6c 6f 67 20 2a  ing to the log *
1ac05 2f 0d 0a 20 20 20 20 75 33 32 20 6d 61 67 69 63  /..    u32 magic
1ac06 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1ac07 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 76 61       /* Magic va
1ac08 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 57 41  lue read from WA
1ac09 4c 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20 20  L header */..   
1ac0a 20 75 33 32 20 76 65 72 73 69 6f 6e 3b 20 20 20   u32 version;   
1ac0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ac0c 2a 20 4d 61 67 69 63 20 76 61 6c 75 65 20 72 65  * Magic value re
1ac0d 61 64 20 66 72 6f 6d 20 57 41 4c 20 68 65 61 64  ad from WAL head
1ac0e 65 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69  er */..    int i
1ac0f 73 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20  sValid;         
1ac10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1ac11 20 69 66 20 74 68 69 73 20 66 72 61 6d 65 20 69   if this frame i
1ac12 73 20 76 61 6c 69 64 20 2a 2f 0d 0a 0d 0a 20 20  s valid */....  
1ac13 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65    /* Read in the
1ac14 20 57 41 4c 20 68 65 61 64 65 72 2e 20 2a 2f 0d   WAL header. */.
1ac15 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1ac16 33 4f 73 52 65 61 64 28 70 57 61 6c 2d 3e 70 57  3OsRead(pWal->pW
1ac17 61 6c 46 64 2c 20 61 42 75 66 2c 20 57 41 4c 5f  alFd, aBuf, WAL_
1ac18 48 44 52 53 49 5a 45 2c 20 30 29 3b 0d 0a 20 20  HDRSIZE, 0);..  
1ac19 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1ac1a 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f  _OK ){..      go
1ac1b 74 6f 20 72 65 63 6f 76 65 72 79 5f 65 72 72 6f  to recovery_erro
1ac1c 72 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  r;..    }....   
1ac1d 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
1ac1e 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  ase page size is
1ac1f 20 6e 6f 74 20 61 20 70 6f 77 65 72 20 6f 66 20   not a power of 
1ac20 74 77 6f 2c 20 6f 72 20 69 73 20 67 72 65 61 74  two, or is great
1ac21 65 72 20 74 68 61 6e 0d 0a 20 20 20 20 2a 2a 20  er than..    ** 
1ac22 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1ac23 53 49 5a 45 2c 20 63 6f 6e 63 6c 75 64 65 20 74  SIZE, conclude t
1ac24 68 61 74 20 74 68 65 20 57 41 4c 20 66 69 6c 65  hat the WAL file
1ac25 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 76 61 6c   contains no val
1ac26 69 64 20 0d 0a 20 20 20 20 2a 2a 20 64 61 74 61  id ..    ** data
1ac27 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20  . Similarly, if 
1ac28 74 68 65 20 27 6d 61 67 69 63 27 20 76 61 6c 75  the 'magic' valu
1ac29 65 20 69 73 20 69 6e 76 61 6c 69 64 2c 20 69 67  e is invalid, ig
1ac2a 6e 6f 72 65 20 74 68 65 20 77 68 6f 6c 65 0d 0a  nore the whole..
1ac2b 20 20 20 20 2a 2a 20 57 41 4c 20 66 69 6c 65 2e      ** WAL file.
1ac2c 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 6d 61  ..    */..    ma
1ac2d 67 69 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74  gic = sqlite3Get
1ac2e 34 62 79 74 65 28 26 61 42 75 66 5b 30 5d 29 3b  4byte(&aBuf[0]);
1ac2f 0d 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 73  ..    szPage = s
1ac30 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26  qlite3Get4byte(&
1ac31 61 42 75 66 5b 38 5d 29 3b 0d 0a 20 20 20 20 69  aBuf[8]);..    i
1ac32 66 28 20 28 6d 61 67 69 63 26 30 78 46 46 46 46  f( (magic&0xFFFF
1ac33 46 46 46 45 29 21 3d 57 41 4c 5f 4d 41 47 49 43  FFFE)!=WAL_MAGIC
1ac34 20 0d 0a 20 20 20 20 20 7c 7c 20 73 7a 50 61 67   ..     || szPag
1ac35 65 26 28 73 7a 50 61 67 65 2d 31 29 20 0d 0a 20  e&(szPage-1) .. 
1ac36 20 20 20 20 7c 7c 20 73 7a 50 61 67 65 3e 53 51      || szPage>SQ
1ac37 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1ac38 5a 45 20 0d 0a 20 20 20 20 20 7c 7c 20 73 7a 50  ZE ..     || szP
1ac39 61 67 65 3c 35 31 32 20 0d 0a 20 20 20 20 29 7b  age<512 ..    ){
1ac3a 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e  ..      goto fin
1ac3b 69 73 68 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ished;..    }.. 
1ac3c 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 62 69 67     pWal->hdr.big
1ac3d 45 6e 64 43 6b 73 75 6d 20 3d 20 28 75 38 29 28  EndCksum = (u8)(
1ac3e 6d 61 67 69 63 26 30 78 30 30 30 30 30 30 30 31  magic&0x00000001
1ac3f 29 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e 73 7a  );..    pWal->sz
1ac40 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0d 0a  Page = szPage;..
1ac41 20 20 20 20 70 57 61 6c 2d 3e 6e 43 6b 70 74 20      pWal->nCkpt 
1ac42 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  = sqlite3Get4byt
1ac43 65 28 26 61 42 75 66 5b 31 32 5d 29 3b 0d 0a 20  e(&aBuf[12]);.. 
1ac44 20 20 20 6d 65 6d 63 70 79 28 26 70 57 61 6c 2d     memcpy(&pWal-
1ac45 3e 68 64 72 2e 61 53 61 6c 74 2c 20 26 61 42 75  >hdr.aSalt, &aBu
1ac46 66 5b 31 36 5d 2c 20 38 29 3b 0d 0a 0d 0a 20 20  f[16], 8);....  
1ac47 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
1ac48 20 74 68 65 20 57 41 4c 20 68 65 61 64 65 72 20   the WAL header 
1ac49 63 68 65 63 6b 73 75 6d 20 69 73 20 63 6f 72 72  checksum is corr
1ac4a 65 63 74 20 2a 2f 0d 0a 20 20 20 20 77 61 6c 43  ect */..    walC
1ac4b 68 65 63 6b 73 75 6d 42 79 74 65 73 28 70 57 61  hecksumBytes(pWa
1ac4c 6c 2d 3e 68 64 72 2e 62 69 67 45 6e 64 43 6b 73  l->hdr.bigEndCks
1ac4d 75 6d 3d 3d 53 51 4c 49 54 45 5f 42 49 47 45 4e  um==SQLITE_BIGEN
1ac4e 44 49 41 4e 2c 20 0d 0a 20 20 20 20 20 20 20 20  DIAN, ..        
1ac4f 61 42 75 66 2c 20 57 41 4c 5f 48 44 52 53 49 5a  aBuf, WAL_HDRSIZ
1ac50 45 2d 32 2a 34 2c 20 30 2c 20 70 57 61 6c 2d 3e  E-2*4, 0, pWal->
1ac51 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 0d  hdr.aFrameCksum.
1ac52 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20 69 66 28  .    );..    if(
1ac53 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d   pWal->hdr.aFram
1ac54 65 43 6b 73 75 6d 5b 30 5d 21 3d 73 71 6c 69 74  eCksum[0]!=sqlit
1ac55 65 33 47 65 74 34 62 79 74 65 28 26 61 42 75 66  e3Get4byte(&aBuf
1ac56 5b 32 34 5d 29 0d 0a 20 20 20 20 20 7c 7c 20 70  [24])..     || p
1ac57 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43  Wal->hdr.aFrameC
1ac58 6b 73 75 6d 5b 31 5d 21 3d 73 71 6c 69 74 65 33  ksum[1]!=sqlite3
1ac59 47 65 74 34 62 79 74 65 28 26 61 42 75 66 5b 32  Get4byte(&aBuf[2
1ac5a 38 5d 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20  8])..    ){..   
1ac5b 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64     goto finished
1ac5c 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
1ac5d 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
1ac5e 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  he version numbe
1ac5f 72 20 6f 6e 20 74 68 65 20 57 41 4c 20 66 6f 72  r on the WAL for
1ac60 6d 61 74 20 69 73 20 6f 6e 65 20 74 68 61 74 0d  mat is one that.
1ac61 0a 20 20 20 20 2a 2a 20 61 72 65 20 61 62 6c 65  .    ** are able
1ac62 20 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 2a   to understand *
1ac63 2f 0d 0a 20 20 20 20 76 65 72 73 69 6f 6e 20 3d  /..    version =
1ac64 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
1ac65 28 26 61 42 75 66 5b 34 5d 29 3b 0d 0a 20 20 20  (&aBuf[4]);..   
1ac66 20 69 66 28 20 76 65 72 73 69 6f 6e 21 3d 57 41   if( version!=WA
1ac67 4c 5f 4d 41 58 5f 56 45 52 53 49 4f 4e 20 29 7b  L_MAX_VERSION ){
1ac68 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
1ac69 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50  ITE_CANTOPEN_BKP
1ac6a 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 66  T;..      goto f
1ac6b 69 6e 69 73 68 65 64 3b 0d 0a 20 20 20 20 7d 0d  inished;..    }.
1ac6c 0a 0d 0a 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63  ...    /* Malloc
1ac6d 20 61 20 62 75 66 66 65 72 20 74 6f 20 72 65 61   a buffer to rea
1ac6e 64 20 66 72 61 6d 65 73 20 69 6e 74 6f 2e 20 2a  d frames into. *
1ac6f 2f 0d 0a 20 20 20 20 73 7a 46 72 61 6d 65 20 3d  /..    szFrame =
1ac70 20 73 7a 50 61 67 65 20 2b 20 57 41 4c 5f 46 52   szPage + WAL_FR
1ac71 41 4d 45 5f 48 44 52 53 49 5a 45 3b 0d 0a 20 20  AME_HDRSIZE;..  
1ac72 20 20 61 46 72 61 6d 65 20 3d 20 28 75 38 20 2a    aFrame = (u8 *
1ac73 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
1ac74 73 7a 46 72 61 6d 65 29 3b 0d 0a 20 20 20 20 69  szFrame);..    i
1ac75 66 28 20 21 61 46 72 61 6d 65 20 29 7b 0d 0a 20  f( !aFrame ){.. 
1ac76 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1ac77 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 67  _NOMEM;..      g
1ac78 6f 74 6f 20 72 65 63 6f 76 65 72 79 5f 65 72 72  oto recovery_err
1ac79 6f 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  or;..    }..    
1ac7a 61 44 61 74 61 20 3d 20 26 61 46 72 61 6d 65 5b  aData = &aFrame[
1ac7b 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a  WAL_FRAME_HDRSIZ
1ac7c 45 5d 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65  E];....    /* Re
1ac7d 61 64 20 61 6c 6c 20 66 72 61 6d 65 73 20 66 72  ad all frames fr
1ac7e 6f 6d 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e  om the log file.
1ac7f 20 2a 2f 0d 0a 20 20 20 20 69 46 72 61 6d 65 20   */..    iFrame 
1ac80 3d 20 30 3b 0d 0a 20 20 20 20 66 6f 72 28 69 4f  = 0;..    for(iO
1ac81 66 66 73 65 74 3d 57 41 4c 5f 48 44 52 53 49 5a  ffset=WAL_HDRSIZ
1ac82 45 3b 20 28 69 4f 66 66 73 65 74 2b 73 7a 46 72  E; (iOffset+szFr
1ac83 61 6d 65 29 3c 3d 6e 53 69 7a 65 3b 20 69 4f 66  ame)<=nSize; iOf
1ac84 66 73 65 74 2b 3d 73 7a 46 72 61 6d 65 29 7b 0d  fset+=szFrame){.
1ac85 0a 20 20 20 20 20 20 75 33 32 20 70 67 6e 6f 3b  .      u32 pgno;
1ac86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac87 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70     /* Database p
1ac88 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 66  age number for f
1ac89 72 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 20 20 75  rame */..      u
1ac8a 33 32 20 6e 54 72 75 6e 63 61 74 65 3b 20 20 20  32 nTruncate;   
1ac8b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62             /* db
1ac8c 73 69 7a 65 20 66 69 65 6c 64 20 66 72 6f 6d 20  size field from 
1ac8d 66 72 61 6d 65 20 68 65 61 64 65 72 20 2a 2f 0d  frame header */.
1ac8e 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 52 65 61 64  ...      /* Read
1ac8f 20 61 6e 64 20 64 65 63 6f 64 65 20 74 68 65 20   and decode the 
1ac90 6e 65 78 74 20 6c 6f 67 20 66 72 61 6d 65 2e 20  next log frame. 
1ac91 2a 2f 0d 0a 20 20 20 20 20 20 69 46 72 61 6d 65  */..      iFrame
1ac92 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  ++;..      rc = 
1ac93 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 57  sqlite3OsRead(pW
1ac94 61 6c 2d 3e 70 57 61 6c 46 64 2c 20 61 46 72 61  al->pWalFd, aFra
1ac95 6d 65 2c 20 73 7a 46 72 61 6d 65 2c 20 69 4f 66  me, szFrame, iOf
1ac96 66 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 69 66  fset);..      if
1ac97 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1ac98 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
1ac99 69 73 56 61 6c 69 64 20 3d 20 77 61 6c 44 65 63  isValid = walDec
1ac9a 6f 64 65 46 72 61 6d 65 28 70 57 61 6c 2c 20 26  odeFrame(pWal, &
1ac9b 70 67 6e 6f 2c 20 26 6e 54 72 75 6e 63 61 74 65  pgno, &nTruncate
1ac9c 2c 20 61 44 61 74 61 2c 20 61 46 72 61 6d 65 29  , aData, aFrame)
1ac9d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 69 73  ;..      if( !is
1ac9e 56 61 6c 69 64 20 29 20 62 72 65 61 6b 3b 0d 0a  Valid ) break;..
1ac9f 20 20 20 20 20 20 72 63 20 3d 20 77 61 6c 49 6e        rc = walIn
1aca0 64 65 78 41 70 70 65 6e 64 28 70 57 61 6c 2c 20  dexAppend(pWal, 
1aca1 69 46 72 61 6d 65 2c 20 70 67 6e 6f 29 3b 0d 0a  iFrame, pgno);..
1aca2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1aca3 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
1aca4 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  ....      /* If 
1aca5 6e 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f 6e  nTruncate is non
1aca6 2d 7a 65 72 6f 2c 20 74 68 69 73 20 69 73 20 61  -zero, this is a
1aca7 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 64 2e 20   commit record. 
1aca8 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 54  */..      if( nT
1aca9 72 75 6e 63 61 74 65 20 29 7b 0d 0a 20 20 20 20  runcate ){..    
1acaa 20 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78      pWal->hdr.mx
1acab 46 72 61 6d 65 20 3d 20 69 46 72 61 6d 65 3b 0d  Frame = iFrame;.
1acac 0a 20 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 68  .        pWal->h
1acad 64 72 2e 6e 50 61 67 65 20 3d 20 6e 54 72 75 6e  dr.nPage = nTrun
1acae 63 61 74 65 3b 0d 0a 20 20 20 20 20 20 20 20 70  cate;..        p
1acaf 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 20  Wal->hdr.szPage 
1acb0 3d 20 28 75 31 36 29 28 28 73 7a 50 61 67 65 26  = (u16)((szPage&
1acb1 30 78 66 66 30 30 29 20 7c 20 28 73 7a 50 61 67  0xff00) | (szPag
1acb2 65 3e 3e 31 36 29 29 3b 0d 0a 20 20 20 20 20 20  e>>16));..      
1acb3 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 50 61    testcase( szPa
1acb4 67 65 3c 3d 33 32 37 36 38 20 29 3b 0d 0a 20 20  ge<=32768 );..  
1acb5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1acb6 73 7a 50 61 67 65 3e 3d 36 35 35 33 36 20 29 3b  szPage>=65536 );
1acb7 0d 0a 20 20 20 20 20 20 20 20 61 46 72 61 6d 65  ..        aFrame
1acb8 43 6b 73 75 6d 5b 30 5d 20 3d 20 70 57 61 6c 2d  Cksum[0] = pWal-
1acb9 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d  >hdr.aFrameCksum
1acba 5b 30 5d 3b 0d 0a 20 20 20 20 20 20 20 20 61 46  [0];..        aF
1acbb 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 20 3d 20 70  rameCksum[1] = p
1acbc 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43  Wal->hdr.aFrameC
1acbd 6b 73 75 6d 5b 31 5d 3b 0d 0a 20 20 20 20 20 20  ksum[1];..      
1acbe 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  }..    }....    
1acbf 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 46 72  sqlite3_free(aFr
1acc0 61 6d 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 66 69  ame);..  }....fi
1acc1 6e 69 73 68 65 64 3a 0d 0a 20 20 69 66 28 20 72  nished:..  if( r
1acc2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
1acc3 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 57 61  .    volatile Wa
1acc4 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e 66 6f  lCkptInfo *pInfo
1acc5 3b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20  ;..    int i;.. 
1acc6 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72     pWal->hdr.aFr
1acc7 61 6d 65 43 6b 73 75 6d 5b 30 5d 20 3d 20 61 46  ameCksum[0] = aF
1acc8 72 61 6d 65 43 6b 73 75 6d 5b 30 5d 3b 0d 0a 20  rameCksum[0];.. 
1acc9 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72     pWal->hdr.aFr
1acca 61 6d 65 43 6b 73 75 6d 5b 31 5d 20 3d 20 61 46  ameCksum[1] = aF
1accb 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 3b 0d 0a 20  rameCksum[1];.. 
1accc 20 20 20 77 61 6c 49 6e 64 65 78 57 72 69 74 65     walIndexWrite
1accd 48 64 72 28 70 57 61 6c 29 3b 0d 0a 0d 0a 20 20  Hdr(pWal);....  
1acce 20 20 2f 2a 20 52 65 73 65 74 20 74 68 65 20 63    /* Reset the c
1accf 68 65 63 6b 70 6f 69 6e 74 2d 68 65 61 64 65 72  heckpoint-header
1acd0 2e 20 54 68 69 73 20 69 73 20 73 61 66 65 20 62  . This is safe b
1acd1 65 63 61 75 73 65 20 74 68 69 73 20 74 68 72 65  ecause this thre
1acd2 61 64 20 69 73 20 0d 0a 20 20 20 20 2a 2a 20 63  ad is ..    ** c
1acd3 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67  urrently holding
1acd4 20 6c 6f 63 6b 73 20 74 68 61 74 20 65 78 63 6c   locks that excl
1acd5 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 72 65  ude all other re
1acd6 61 64 65 72 73 2c 20 77 72 69 74 65 72 73 20 61  aders, writers a
1acd7 6e 64 0d 0a 20 20 20 20 2a 2a 20 63 68 65 63 6b  nd..    ** check
1acd8 70 6f 69 6e 74 65 72 73 2e 0d 0a 20 20 20 20 2a  pointers...    *
1acd9 2f 0d 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 77  /..    pInfo = w
1acda 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57 61 6c 29  alCkptInfo(pWal)
1acdb 3b 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 42  ;..    pInfo->nB
1acdc 61 63 6b 66 69 6c 6c 20 3d 20 30 3b 0d 0a 20 20  ackfill = 0;..  
1acdd 20 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61    pInfo->aReadMa
1acde 72 6b 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 20 20  rk[0] = 0;..    
1acdf 66 6f 72 28 69 3d 31 3b 20 69 3c 57 41 4c 5f 4e  for(i=1; i<WAL_N
1ace0 52 45 41 44 45 52 3b 20 69 2b 2b 29 20 70 49 6e  READER; i++) pIn
1ace1 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b 69 5d  fo->aReadMark[i]
1ace2 20 3d 20 52 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f   = READMARK_NOT_
1ace3 55 53 45 44 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  USED;....    /* 
1ace4 49 66 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  If more than one
1ace5 20 66 72 61 6d 65 20 77 61 73 20 72 65 63 6f 76   frame was recov
1ace6 65 72 65 64 20 66 72 6f 6d 20 74 68 65 20 6c 6f  ered from the lo
1ace7 67 20 66 69 6c 65 2c 20 72 65 70 6f 72 74 20 61  g file, report a
1ace8 6e 0d 0a 20 20 20 20 2a 2a 20 65 76 65 6e 74 20  n..    ** event 
1ace9 76 69 61 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28  via sqlite3_log(
1acea 29 2e 20 54 68 69 73 20 69 73 20 74 6f 20 68 65  ). This is to he
1aceb 6c 70 20 77 69 74 68 20 69 64 65 6e 74 69 66 79  lp with identify
1acec 69 6e 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 0d  ing performance.
1aced 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73  .    ** problems
1acee 20 63 61 75 73 65 64 20 62 79 20 61 70 70 6c 69   caused by appli
1acef 63 61 74 69 6f 6e 73 20 72 6f 75 74 69 6e 65 6c  cations routinel
1acf0 79 20 73 68 75 74 74 69 6e 67 20 64 6f 77 6e 20  y shutting down 
1acf1 77 69 74 68 6f 75 74 0d 0a 20 20 20 20 2a 2a 20  without..    ** 
1acf2 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20 74 68  checkpointing th
1acf3 65 20 6c 6f 67 20 66 69 6c 65 2e 0d 0a 20 20 20  e log file...   
1acf4 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 57 61   */..    if( pWa
1acf5 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 20 29 7b 0d  l->hdr.nPage ){.
1acf6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6c  .      sqlite3_l
1acf7 6f 67 28 53 51 4c 49 54 45 5f 4f 4b 2c 20 22 52  og(SQLITE_OK, "R
1acf8 65 63 6f 76 65 72 65 64 20 25 64 20 66 72 61 6d  ecovered %d fram
1acf9 65 73 20 66 72 6f 6d 20 57 41 4c 20 66 69 6c 65  es from WAL file
1acfa 20 25 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 20   %s",..         
1acfb 20 70 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65   pWal->hdr.nPage
1acfc 2c 20 70 57 61 6c 2d 3e 7a 57 61 6c 4e 61 6d 65  , pWal->zWalName
1acfd 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20  ..      );..    
1acfe 7d 0d 0a 20 20 7d 0d 0a 0d 0a 72 65 63 6f 76 65  }..  }....recove
1acff 72 79 5f 65 72 72 6f 72 3a 0d 0a 20 20 57 41 4c  ry_error:..  WAL
1ad00 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 72  TRACE(("WAL%p: r
1ad01 65 63 6f 76 65 72 79 20 25 73 5c 6e 22 2c 20 70  ecovery %s\n", p
1ad02 57 61 6c 2c 20 72 63 20 3f 20 22 66 61 69 6c 65  Wal, rc ? "faile
1ad03 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0d 0a 20 20  d" : "ok"));..  
1ad04 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69  walUnlockExclusi
1ad05 76 65 28 70 57 61 6c 2c 20 69 4c 6f 63 6b 2c 20  ve(pWal, iLock, 
1ad06 6e 4c 6f 63 6b 29 3b 0d 0a 20 20 72 65 74 75 72  nLock);..  retur
1ad07 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
1ad08 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e  ** Close an open
1ad09 20 77 61 6c 2d 69 6e 64 65 78 2e 0d 0a 2a 2f 0d   wal-index...*/.
1ad0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c  .static void wal
1ad0b 49 6e 64 65 78 43 6c 6f 73 65 28 57 61 6c 20 2a  IndexClose(Wal *
1ad0c 70 57 61 6c 2c 20 69 6e 74 20 69 73 44 65 6c 65  pWal, int isDele
1ad0d 74 65 29 7b 0d 0a 20 20 69 66 28 20 70 57 61 6c  te){..  if( pWal
1ad0e 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d  ->exclusiveMode=
1ad0f 3d 57 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 5f  =WAL_HEAPMEMORY_
1ad10 4d 4f 44 45 20 29 7b 0d 0a 20 20 20 20 69 6e 74  MODE ){..    int
1ad11 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   i;..    for(i=0
1ad12 3b 20 69 3c 70 57 61 6c 2d 3e 6e 57 69 44 61 74  ; i<pWal->nWiDat
1ad13 61 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  a; i++){..      
1ad14 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f  sqlite3_free((vo
1ad15 69 64 20 2a 29 70 57 61 6c 2d 3e 61 70 57 69 44  id *)pWal->apWiD
1ad16 61 74 61 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20  ata[i]);..      
1ad17 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69  pWal->apWiData[i
1ad18 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ] = 0;..    }.. 
1ad19 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
1ad1a 69 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 70  ite3OsShmUnmap(p
1ad1b 57 61 6c 2d 3e 70 44 62 46 64 2c 20 69 73 44 65  Wal->pDbFd, isDe
1ad1c 6c 65 74 65 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  lete);..  }..}..
1ad1d 0d 0a 2f 2a 20 0d 0a 2a 2a 20 4f 70 65 6e 20 61  ../* ..** Open a
1ad1e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74   connection to t
1ad1f 68 65 20 57 41 4c 20 66 69 6c 65 20 7a 57 61 6c  he WAL file zWal
1ad20 4e 61 6d 65 2e 20 54 68 65 20 64 61 74 61 62 61  Name. The databa
1ad21 73 65 20 66 69 6c 65 20 6d 75 73 74 20 0d 0a 2a  se file must ..*
1ad22 2a 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70 65  * already be ope
1ad23 6e 65 64 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f  ned on connectio
1ad24 6e 20 70 44 62 46 64 2e 20 54 68 65 20 62 75 66  n pDbFd. The buf
1ad25 66 65 72 20 74 68 61 74 20 7a 57 61 6c 4e 61 6d  fer that zWalNam
1ad26 65 20 70 6f 69 6e 74 73 0d 0a 2a 2a 20 74 6f 20  e points..** to 
1ad27 6d 75 73 74 20 72 65 6d 61 69 6e 20 76 61 6c 69  must remain vali
1ad28 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69  d for the lifeti
1ad29 6d 65 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e  me of the return
1ad2a 65 64 20 57 61 6c 2a 20 68 61 6e 64 6c 65 2e 0d  ed Wal* handle..
1ad2b 0a 2a 2a 0d 0a 2a 2a 20 41 20 53 48 41 52 45 44  .**..** A SHARED
1ad2c 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20   lock should be 
1ad2d 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
1ad2e 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 74  base file when t
1ad2f 68 69 73 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a  his function..**
1ad30 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20   is called. The 
1ad31 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
1ad32 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 74  SHARED lock is t
1ad33 6f 20 70 72 65 76 65 6e 74 20 61 6e 79 20 6f 74  o prevent any ot
1ad34 68 65 72 0d 0a 2a 2a 20 63 6c 69 65 6e 74 20 66  her..** client f
1ad35 72 6f 6d 20 75 6e 6c 69 6e 6b 69 6e 67 20 74 68  rom unlinking th
1ad36 65 20 57 41 4c 20 6f 72 20 77 61 6c 2d 69 6e 64  e WAL or wal-ind
1ad37 65 78 20 66 69 6c 65 2e 20 49 66 20 61 6e 6f 74  ex file. If anot
1ad38 68 65 72 20 70 72 6f 63 65 73 73 0d 0a 2a 2a 20  her process..** 
1ad39 77 65 72 65 20 74 6f 20 64 6f 20 74 68 69 73 20  were to do this 
1ad3a 6a 75 73 74 20 61 66 74 65 72 20 74 68 69 73 20  just after this 
1ad3b 63 6c 69 65 6e 74 20 6f 70 65 6e 65 64 20 6f 6e  client opened on
1ad3c 65 20 6f 66 20 74 68 65 73 65 20 66 69 6c 65 73  e of these files
1ad3d 2c 20 74 68 65 0d 0a 2a 2a 20 73 79 73 74 65 6d  , the..** system
1ad3e 20 77 6f 75 6c 64 20 62 65 20 62 61 64 6c 79 20   would be badly 
1ad3f 62 72 6f 6b 65 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  broken...**..** 
1ad40 49 66 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20  If the log file 
1ad41 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
1ad42 6f 70 65 6e 65 64 2c 20 53 51 4c 49 54 45 5f 4f  opened, SQLITE_O
1ad43 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
1ad44 64 20 0d 0a 2a 2a 20 2a 70 70 57 61 6c 20 69 73  d ..** *ppWal is
1ad45 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
1ad46 20 61 20 6e 65 77 20 57 41 4c 20 68 61 6e 64 6c   a new WAL handl
1ad47 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  e. If an error o
1ad48 63 63 75 72 73 2c 0d 0a 2a 2a 20 61 6e 20 53 51  ccurs,..** an SQ
1ad49 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
1ad4a 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
1ad4b 2a 70 70 57 61 6c 20 69 73 20 6c 65 66 74 20 75  *ppWal is left u
1ad4c 6e 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2f 0d 0a  nmodified...*/..
1ad4d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1ad4e 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 4f 70 65  nt sqlite3WalOpe
1ad4f 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  n(..  sqlite3_vf
1ad50 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
1ad51 20 20 20 20 20 20 2f 2a 20 76 66 73 20 6d 6f 64        /* vfs mod
1ad52 75 6c 65 20 74 6f 20 6f 70 65 6e 20 77 61 6c 20  ule to open wal 
1ad53 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 20 2a 2f  and wal-index */
1ad54 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ..  sqlite3_file
1ad55 20 2a 70 44 62 46 64 2c 20 20 20 20 20 20 20 20   *pDbFd,        
1ad56 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 6e 20      /* The open 
1ad57 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
1ad58 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
1ad59 7a 57 61 6c 4e 61 6d 65 2c 20 20 20 20 20 20 20  zWalName,       
1ad5a 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1ad5b 68 65 20 57 41 4c 20 66 69 6c 65 20 2a 2f 0d 0a  he WAL file */..
1ad5c 20 20 69 6e 74 20 62 4e 6f 53 68 6d 2c 20 20 20    int bNoShm,   
1ad5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad5e 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 75 6e    /* True to run
1ad5f 20 69 6e 20 68 65 61 70 2d 6d 65 6d 6f 72 79 20   in heap-memory 
1ad60 6d 6f 64 65 20 2a 2f 0d 0a 20 20 69 36 34 20 6d  mode */..  i64 m
1ad61 78 57 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 20  xWalSize,       
1ad62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1ad63 75 6e 63 61 74 65 20 57 41 4c 20 74 6f 20 74 68  uncate WAL to th
1ad64 69 73 20 73 69 7a 65 20 6f 6e 20 72 65 73 65 74  is size on reset
1ad65 20 2a 2f 0d 0a 20 20 57 61 6c 20 2a 2a 70 70 57   */..  Wal **ppW
1ad66 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  al              
1ad67 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41         /* OUT: A
1ad68 6c 6c 6f 63 61 74 65 64 20 57 61 6c 20 68 61 6e  llocated Wal han
1ad69 64 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  dle */..){..  in
1ad6a 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
1ad6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ad6c 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d   Return Code */.
1ad6d 0a 20 20 57 61 6c 20 2a 70 52 65 74 3b 20 20 20  .  Wal *pRet;   
1ad6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad6f 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 74 6f 20     /* Object to 
1ad70 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74  allocate and ret
1ad71 75 72 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c  urn */..  int fl
1ad72 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
1ad73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1ad74 67 73 20 70 61 73 73 65 64 20 74 6f 20 4f 73 4f  gs passed to OsO
1ad75 70 65 6e 28 29 20 2a 2f 0d 0a 0d 0a 20 20 61 73  pen() */....  as
1ad76 73 65 72 74 28 20 7a 57 61 6c 4e 61 6d 65 20 26  sert( zWalName &
1ad77 26 20 7a 57 61 6c 4e 61 6d 65 5b 30 5d 20 29 3b  & zWalName[0] );
1ad78 0d 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 46  ..  assert( pDbF
1ad79 64 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 20  d );....  /* In 
1ad7a 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
1ad7b 2c 20 74 68 65 20 6f 73 5f 75 6e 69 78 2e 63 20  , the os_unix.c 
1ad7c 61 6e 64 20 6f 73 5f 77 69 6e 2e 63 20 73 6f 75  and os_win.c sou
1ad7d 72 63 65 20 66 69 6c 65 73 20 63 6f 6d 65 20 62  rce files come b
1ad7e 65 66 6f 72 65 0d 0a 20 20 2a 2a 20 74 68 69 73  efore..  ** this
1ad7f 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 20 20 56   source file.  V
1ad80 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 23  erify that the #
1ad81 64 65 66 69 6e 65 73 20 6f 66 20 74 68 65 20 6c  defines of the l
1ad82 6f 63 6b 69 6e 67 20 62 79 74 65 20 6f 66 66 73  ocking byte offs
1ad83 65 74 73 0d 0a 20 20 2a 2a 20 69 6e 20 6f 73 5f  ets..  ** in os_
1ad84 75 6e 69 78 2e 63 20 61 6e 64 20 6f 73 5f 77 69  unix.c and os_wi
1ad85 6e 2e 63 20 61 67 72 65 65 20 77 69 74 68 20 74  n.c agree with t
1ad86 68 65 20 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b  he WALINDEX_LOCK
1ad87 5f 4f 46 46 53 45 54 20 76 61 6c 75 65 2e 0d 0a  _OFFSET value...
1ad88 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 57 49 4e    */..#ifdef WIN
1ad89 5f 53 48 4d 5f 42 41 53 45 0d 0a 20 20 61 73 73  _SHM_BASE..  ass
1ad8a 65 72 74 28 20 57 49 4e 5f 53 48 4d 5f 42 41 53  ert( WIN_SHM_BAS
1ad8b 45 3d 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b  E==WALINDEX_LOCK
1ad8c 5f 4f 46 46 53 45 54 20 29 3b 0d 0a 23 65 6e 64  _OFFSET );..#end
1ad8d 69 66 0d 0a 23 69 66 64 65 66 20 55 4e 49 58 5f  if..#ifdef UNIX_
1ad8e 53 48 4d 5f 42 41 53 45 0d 0a 20 20 61 73 73 65  SHM_BASE..  asse
1ad8f 72 74 28 20 55 4e 49 58 5f 53 48 4d 5f 42 41 53  rt( UNIX_SHM_BAS
1ad90 45 3d 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b  E==WALINDEX_LOCK
1ad91 5f 4f 46 46 53 45 54 20 29 3b 0d 0a 23 65 6e 64  _OFFSET );..#end
1ad92 69 66 0d 0a 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c  if......  /* All
1ad93 6f 63 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63  ocate an instanc
1ad94 65 20 6f 66 20 73 74 72 75 63 74 20 57 61 6c 20  e of struct Wal 
1ad95 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f 0d 0a 20  to return. */.. 
1ad96 20 2a 70 70 57 61 6c 20 3d 20 30 3b 0d 0a 20 20   *ppWal = 0;..  
1ad97 70 52 65 74 20 3d 20 28 57 61 6c 2a 29 73 71 6c  pRet = (Wal*)sql
1ad98 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
1ad99 69 7a 65 6f 66 28 57 61 6c 29 20 2b 20 70 56 66  izeof(Wal) + pVf
1ad9a 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0d 0a 20  s->szOsFile);.. 
1ad9b 20 69 66 28 20 21 70 52 65 74 20 29 7b 0d 0a 20   if( !pRet ){.. 
1ad9c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1ad9d 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a  _NOMEM;..  }....
1ad9e 20 20 70 52 65 74 2d 3e 70 56 66 73 20 3d 20 70    pRet->pVfs = p
1ad9f 56 66 73 3b 0d 0a 20 20 70 52 65 74 2d 3e 70 57  Vfs;..  pRet->pW
1ada0 61 6c 46 64 20 3d 20 28 73 71 6c 69 74 65 33 5f  alFd = (sqlite3_
1ada1 66 69 6c 65 20 2a 29 26 70 52 65 74 5b 31 5d 3b  file *)&pRet[1];
1ada2 0d 0a 20 20 70 52 65 74 2d 3e 70 44 62 46 64 20  ..  pRet->pDbFd 
1ada3 3d 20 70 44 62 46 64 3b 0d 0a 20 20 70 52 65 74  = pDbFd;..  pRet
1ada4 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b  ->readLock = -1;
1ada5 0d 0a 20 20 70 52 65 74 2d 3e 6d 78 57 61 6c 53  ..  pRet->mxWalS
1ada6 69 7a 65 20 3d 20 6d 78 57 61 6c 53 69 7a 65 3b  ize = mxWalSize;
1ada7 0d 0a 20 20 70 52 65 74 2d 3e 7a 57 61 6c 4e 61  ..  pRet->zWalNa
1ada8 6d 65 20 3d 20 7a 57 61 6c 4e 61 6d 65 3b 0d 0a  me = zWalName;..
1ada9 20 20 70 52 65 74 2d 3e 73 79 6e 63 48 65 61 64    pRet->syncHead
1adaa 65 72 20 3d 20 31 3b 0d 0a 20 20 70 52 65 74 2d  er = 1;..  pRet-
1adab 3e 70 61 64 54 6f 53 65 63 74 6f 72 42 6f 75 6e  >padToSectorBoun
1adac 64 61 72 79 20 3d 20 31 3b 0d 0a 20 20 70 52 65  dary = 1;..  pRe
1adad 74 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  t->exclusiveMode
1adae 20 3d 20 28 62 4e 6f 53 68 6d 20 3f 20 57 41 4c   = (bNoShm ? WAL
1adaf 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45  _HEAPMEMORY_MODE
1adb0 3a 20 57 41 4c 5f 4e 4f 52 4d 41 4c 5f 4d 4f 44  : WAL_NORMAL_MOD
1adb1 45 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4f 70 65 6e  E);....  /* Open
1adb2 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 6f 6e 20   file handle on 
1adb3 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 20  the write-ahead 
1adb4 6c 6f 67 20 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20  log file. */..  
1adb5 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f  flags = (SQLITE_
1adb6 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53  OPEN_READWRITE|S
1adb7 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
1adb8 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41  E|SQLITE_OPEN_WA
1adb9 4c 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  L);..  rc = sqli
1adba 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
1adbb 7a 57 61 6c 4e 61 6d 65 2c 20 70 52 65 74 2d 3e  zWalName, pRet->
1adbc 70 57 61 6c 46 64 2c 20 66 6c 61 67 73 2c 20 26  pWalFd, flags, &
1adbd 66 6c 61 67 73 29 3b 0d 0a 20 20 69 66 28 20 72  flags);..  if( r
1adbe 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1adbf 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45  flags&SQLITE_OPE
1adc0 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0d 0a 20  N_READONLY ){.. 
1adc1 20 20 20 70 52 65 74 2d 3e 72 65 61 64 4f 6e 6c     pRet->readOnl
1adc2 79 20 3d 20 57 41 4c 5f 52 44 4f 4e 4c 59 3b 0d  y = WAL_RDONLY;.
1adc3 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63  .  }....  if( rc
1adc4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
1adc5 20 20 20 20 77 61 6c 49 6e 64 65 78 43 6c 6f 73      walIndexClos
1adc6 65 28 70 52 65 74 2c 20 30 29 3b 0d 0a 20 20 20  e(pRet, 0);..   
1adc7 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
1adc8 70 52 65 74 2d 3e 70 57 61 6c 46 64 29 3b 0d 0a  pRet->pWalFd);..
1adc9 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1adca 28 70 52 65 74 29 3b 0d 0a 20 20 7d 65 6c 73 65  (pRet);..  }else
1adcb 7b 0d 0a 20 20 20 20 69 6e 74 20 69 44 43 20 3d  {..    int iDC =
1adcc 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
1adcd 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
1adce 70 52 65 74 2d 3e 70 57 61 6c 46 64 29 3b 0d 0a  pRet->pWalFd);..
1adcf 20 20 20 20 69 66 28 20 69 44 43 20 26 20 53 51      if( iDC & SQ
1add0 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
1add1 4e 54 49 41 4c 20 29 7b 20 70 52 65 74 2d 3e 73  NTIAL ){ pRet->s
1add2 79 6e 63 48 65 61 64 65 72 20 3d 20 30 3b 20 7d  yncHeader = 0; }
1add3 0d 0a 20 20 20 20 69 66 28 20 69 44 43 20 26 20  ..    if( iDC & 
1add4 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57  SQLITE_IOCAP_POW
1add5 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45  ERSAFE_OVERWRITE
1add6 20 29 7b 0d 0a 20 20 20 20 20 20 70 52 65 74 2d   ){..      pRet-
1add7 3e 70 61 64 54 6f 53 65 63 74 6f 72 42 6f 75 6e  >padToSectorBoun
1add8 64 61 72 79 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  dary = 0;..    }
1add9 0d 0a 20 20 20 20 2a 70 70 57 61 6c 20 3d 20 70  ..    *ppWal = p
1adda 52 65 74 3b 0d 0a 20 20 20 20 57 41 4c 54 52 41  Ret;..    WALTRA
1addb 43 45 28 28 22 57 41 4c 25 64 3a 20 6f 70 65 6e  CE(("WAL%d: open
1addc 65 64 5c 6e 22 2c 20 70 52 65 74 29 29 3b 0d 0a  ed\n", pRet));..
1addd 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
1adde 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
1addf 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 74  hange the size t
1ade0 6f 20 77 68 69 63 68 20 74 68 65 20 57 41 4c 20  o which the WAL 
1ade1 66 69 6c 65 20 69 73 20 74 72 75 63 61 74 65 64  file is trucated
1ade2 20 6f 6e 20 65 61 63 68 20 72 65 73 65 74 2e 0d   on each reset..
1ade3 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1ade4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1ade5 57 61 6c 4c 69 6d 69 74 28 57 61 6c 20 2a 70 57  WalLimit(Wal *pW
1ade6 61 6c 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b  al, i64 iLimit){
1ade7 0d 0a 20 20 69 66 28 20 70 57 61 6c 20 29 20 70  ..  if( pWal ) p
1ade8 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 20 3d  Wal->mxWalSize =
1ade9 20 69 4c 69 6d 69 74 3b 0d 0a 7d 0d 0a 0d 0a 2f   iLimit;..}..../
1adea 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 73  *..** Find the s
1adeb 6d 61 6c 6c 65 73 74 20 70 61 67 65 20 6e 75 6d  mallest page num
1adec 62 65 72 20 6f 75 74 20 6f 66 20 61 6c 6c 20 70  ber out of all p
1aded 61 67 65 73 20 68 65 6c 64 20 69 6e 20 74 68 65  ages held in the
1adee 20 57 41 4c 20 74 68 61 74 0d 0a 2a 2a 20 68 61   WAL that..** ha
1adef 73 20 6e 6f 74 20 62 65 65 6e 20 72 65 74 75 72  s not been retur
1adf0 6e 65 64 20 62 79 20 61 6e 79 20 70 72 69 6f 72  ned by any prior
1adf1 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
1adf2 68 69 73 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68  his method on th
1adf3 65 0d 0a 2a 2a 20 73 61 6d 65 20 57 61 6c 49 74  e..** same WalIt
1adf4 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 2e 20 20  erator object.  
1adf5 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 46   Write into *piF
1adf6 72 61 6d 65 20 74 68 65 20 66 72 61 6d 65 20 69  rame the frame i
1adf7 6e 64 65 78 20 77 68 65 72 65 0d 0a 2a 2a 20 74  ndex where..** t
1adf8 68 61 74 20 70 61 67 65 20 77 61 73 20 6c 61 73  hat page was las
1adf9 74 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  t written into t
1adfa 68 65 20 57 41 4c 2e 20 20 57 72 69 74 65 20 69  he WAL.  Write i
1adfb 6e 74 6f 20 2a 70 69 50 61 67 65 20 74 68 65 20  nto *piPage the 
1adfc 70 61 67 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 2e  page..** number.
1adfd 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ..**..** Return 
1adfe 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49  0 on success.  I
1adff 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 70  f there are no p
1ae00 61 67 65 73 20 69 6e 20 74 68 65 20 57 41 4c 20  ages in the WAL 
1ae01 77 69 74 68 20 61 20 70 61 67 65 0d 0a 2a 2a 20  with a page..** 
1ae02 6e 75 6d 62 65 72 20 6c 61 72 67 65 72 20 74 68  number larger th
1ae03 61 6e 20 2a 70 69 50 61 67 65 2c 20 74 68 65 6e  an *piPage, then
1ae04 20 72 65 74 75 72 6e 20 31 2e 0d 0a 2a 2f 0d 0a   return 1...*/..
1ae05 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c 49 74  static int walIt
1ae06 65 72 61 74 6f 72 4e 65 78 74 28 0d 0a 20 20 57  eratorNext(..  W
1ae07 61 6c 49 74 65 72 61 74 6f 72 20 2a 70 2c 20 20  alIterator *p,  
1ae08 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ae09 49 74 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 75  Iterator */..  u
1ae0a 33 32 20 2a 70 69 50 61 67 65 2c 20 20 20 20 20  32 *piPage,     
1ae0b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ae0c 4f 55 54 3a 20 54 68 65 20 70 61 67 65 20 6e 75  OUT: The page nu
1ae0d 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
1ae0e 20 70 61 67 65 20 2a 2f 0d 0a 20 20 75 33 32 20   page */..  u32 
1ae0f 2a 70 69 46 72 61 6d 65 20 20 20 20 20 20 20 20  *piFrame        
1ae10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
1ae11 3a 20 57 61 6c 20 66 72 61 6d 65 20 69 6e 64 65  : Wal frame inde
1ae12 78 20 6f 66 20 6e 65 78 74 20 70 61 67 65 20 2a  x of next page *
1ae13 2f 0d 0a 29 7b 0d 0a 20 20 75 33 32 20 69 4d 69  /..){..  u32 iMi
1ae14 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
1ae15 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
1ae16 20 70 67 6e 6f 20 6d 75 73 74 20 62 65 20 67 72   pgno must be gr
1ae17 65 61 74 65 72 20 74 68 61 6e 20 69 4d 69 6e 20  eater than iMin 
1ae18 2a 2f 0d 0a 20 20 75 33 32 20 69 52 65 74 20 3d  */..  u32 iRet =
1ae19 20 30 78 46 46 46 46 46 46 46 46 3b 20 20 20 20   0xFFFFFFFF;    
1ae1a 20 20 20 20 2f 2a 20 30 78 66 66 66 66 66 66 66      /* 0xfffffff
1ae1b 66 20 69 73 20 6e 65 76 65 72 20 61 20 76 61 6c  f is never a val
1ae1c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a  id page number *
1ae1d 2f 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  /..  int i;     
1ae1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae1f 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
1ae20 67 20 74 68 72 6f 75 67 68 20 73 65 67 6d 65 6e  g through segmen
1ae21 74 73 20 2a 2f 0d 0a 0d 0a 20 20 69 4d 69 6e 20  ts */....  iMin 
1ae22 3d 20 70 2d 3e 69 50 72 69 6f 72 3b 0d 0a 20 20  = p->iPrior;..  
1ae23 61 73 73 65 72 74 28 20 69 4d 69 6e 3c 30 78 66  assert( iMin<0xf
1ae24 66 66 66 66 66 66 66 20 29 3b 0d 0a 20 20 66 6f  fffffff );..  fo
1ae25 72 28 69 3d 70 2d 3e 6e 53 65 67 6d 65 6e 74 2d  r(i=p->nSegment-
1ae26 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0d 0a  1; i>=0; i--){..
1ae27 20 20 20 20 73 74 72 75 63 74 20 57 61 6c 53 65      struct WalSe
1ae28 67 6d 65 6e 74 20 2a 70 53 65 67 6d 65 6e 74 20  gment *pSegment 
1ae29 3d 20 26 70 2d 3e 61 53 65 67 6d 65 6e 74 5b 69  = &p->aSegment[i
1ae2a 5d 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 70  ];..    while( p
1ae2b 53 65 67 6d 65 6e 74 2d 3e 69 4e 65 78 74 3c 70  Segment->iNext<p
1ae2c 53 65 67 6d 65 6e 74 2d 3e 6e 45 6e 74 72 79 20  Segment->nEntry 
1ae2d 29 7b 0d 0a 20 20 20 20 20 20 75 33 32 20 69 50  ){..      u32 iP
1ae2e 67 20 3d 20 70 53 65 67 6d 65 6e 74 2d 3e 61 50  g = pSegment->aP
1ae2f 67 6e 6f 5b 70 53 65 67 6d 65 6e 74 2d 3e 61 49  gno[pSegment->aI
1ae30 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74 2d 3e 69  ndex[pSegment->i
1ae31 4e 65 78 74 5d 5d 3b 0d 0a 20 20 20 20 20 20 69  Next]];..      i
1ae32 66 28 20 69 50 67 3e 69 4d 69 6e 20 29 7b 0d 0a  f( iPg>iMin ){..
1ae33 20 20 20 20 20 20 20 20 69 66 28 20 69 50 67 3c          if( iPg<
1ae34 69 52 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  iRet ){..       
1ae35 20 20 20 69 52 65 74 20 3d 20 69 50 67 3b 0d 0a     iRet = iPg;..
1ae36 20 20 20 20 20 20 20 20 20 20 2a 70 69 46 72 61            *piFra
1ae37 6d 65 20 3d 20 70 53 65 67 6d 65 6e 74 2d 3e 69  me = pSegment->i
1ae38 5a 65 72 6f 20 2b 20 70 53 65 67 6d 65 6e 74 2d  Zero + pSegment-
1ae39 3e 61 49 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74  >aIndex[pSegment
1ae3a 2d 3e 69 4e 65 78 74 5d 3b 0d 0a 20 20 20 20 20  ->iNext];..     
1ae3b 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 72     }..        br
1ae3c 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
1ae3d 20 20 20 20 20 70 53 65 67 6d 65 6e 74 2d 3e 69       pSegment->i
1ae3e 4e 65 78 74 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a  Next++;..    }..
1ae3f 20 20 7d 0d 0a 0d 0a 20 20 2a 70 69 50 61 67 65    }....  *piPage
1ae40 20 3d 20 70 2d 3e 69 50 72 69 6f 72 20 3d 20 69   = p->iPrior = i
1ae41 52 65 74 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28  Ret;..  return (
1ae42 69 52 65 74 3d 3d 30 78 46 46 46 46 46 46 46 46  iRet==0xFFFFFFFF
1ae43 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
1ae44 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 65  This function me
1ae45 72 67 65 73 20 74 77 6f 20 73 6f 72 74 65 64 20  rges two sorted 
1ae46 6c 69 73 74 73 20 69 6e 74 6f 20 61 20 73 69 6e  lists into a sin
1ae47 67 6c 65 20 73 6f 72 74 65 64 20 6c 69 73 74 2e  gle sorted list.
1ae48 0d 0a 2a 2a 0d 0a 2a 2a 20 61 4c 65 66 74 5b 5d  ..**..** aLeft[]
1ae49 20 61 6e 64 20 61 52 69 67 68 74 5b 5d 20 61 72   and aRight[] ar
1ae4a 65 20 61 72 72 61 79 73 20 6f 66 20 69 6e 64 69  e arrays of indi
1ae4b 63 65 73 2e 20 20 54 68 65 20 73 6f 72 74 20 6b  ces.  The sort k
1ae4c 65 79 20 69 73 0d 0a 2a 2a 20 61 43 6f 6e 74 65  ey is..** aConte
1ae4d 6e 74 5b 61 4c 65 66 74 5b 5d 5d 20 61 6e 64 20  nt[aLeft[]] and 
1ae4e 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 5b  aContent[aRight[
1ae4f 5d 5d 2e 20 20 55 70 6f 6e 20 65 6e 74 72 79 2c  ]].  Upon entry,
1ae50 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a   the following..
1ae51 2a 2a 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  ** is guaranteed
1ae52 20 66 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0d 0a 2a   for all J<K:..*
1ae53 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 61 43 6f  *..**        aCo
1ae54 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 4a 5d 5d 20  ntent[aLeft[J]] 
1ae55 3c 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74  < aContent[aLeft
1ae56 5b 4b 5d 5d 0d 0a 2a 2a 20 20 20 20 20 20 20 20  [K]]..**        
1ae57 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 5b  aContent[aRight[
1ae58 4a 5d 5d 20 3c 20 61 43 6f 6e 74 65 6e 74 5b 61  J]] < aContent[a
1ae59 52 69 67 68 74 5b 4b 5d 5d 0d 0a 2a 2a 0d 0a 2a  Right[K]]..**..*
1ae5a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
1ae5b 76 65 72 77 72 69 74 65 73 20 61 52 69 67 68 74  verwrites aRight
1ae5c 5b 5d 20 77 69 74 68 20 61 20 6e 65 77 20 28 70  [] with a new (p
1ae5d 72 6f 62 61 62 6c 79 20 6c 6f 6e 67 65 72 29 20  robably longer) 
1ae5e 73 65 71 75 65 6e 63 65 0d 0a 2a 2a 20 6f 66 20  sequence..** of 
1ae5f 69 6e 64 69 63 65 73 20 73 75 63 68 20 74 68 61  indices such tha
1ae60 74 20 74 68 65 20 61 52 69 67 68 74 5b 5d 20 63  t the aRight[] c
1ae61 6f 6e 74 61 69 6e 73 20 65 76 65 72 79 20 69 6e  ontains every in
1ae62 64 65 78 20 74 68 61 74 20 61 70 70 65 61 72 73  dex that appears
1ae63 20 69 6e 0d 0a 2a 2a 20 65 69 74 68 65 72 20 61   in..** either a
1ae64 4c 65 66 74 5b 5d 20 6f 72 20 74 68 65 20 6f 6c  Left[] or the ol
1ae65 64 20 61 52 69 67 68 74 5b 5d 20 61 6e 64 20 73  d aRight[] and s
1ae66 75 63 68 20 74 68 61 74 20 74 68 65 20 73 65 63  uch that the sec
1ae67 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 0d 0a 2a  ond condition..*
1ae68 2a 20 61 62 6f 76 65 20 69 73 20 73 74 69 6c 6c  * above is still
1ae69 20 6d 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68   met...**..** Th
1ae6a 65 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74  e aContent[aLeft
1ae6b 5b 58 5d 5d 20 76 61 6c 75 65 73 20 77 69 6c 6c  [X]] values will
1ae6c 20 62 65 20 75 6e 69 71 75 65 20 66 6f 72 20 61   be unique for a
1ae6d 6c 6c 20 58 2e 20 20 41 6e 64 20 74 68 65 0d 0a  ll X.  And the..
1ae6e 2a 2a 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67  ** aContent[aRig
1ae6f 68 74 5b 58 5d 5d 20 76 61 6c 75 65 73 20 77 69  ht[X]] values wi
1ae70 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 74 6f 6f  ll be unique too
1ae71 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d 69 67  .  But there mig
1ae72 68 74 20 62 65 0d 0a 2a 2a 20 6f 6e 65 20 6f 72  ht be..** one or
1ae73 20 6d 6f 72 65 20 63 6f 6d 62 69 6e 61 74 69 6f   more combinatio
1ae74 6e 73 20 6f 66 20 58 20 61 6e 64 20 59 20 73 75  ns of X and Y su
1ae75 63 68 20 74 68 61 74 0d 0a 2a 2a 0d 0a 2a 2a 20  ch that..**..** 
1ae76 20 20 20 20 20 61 4c 65 66 74 5b 58 5d 21 3d 61       aLeft[X]!=a
1ae77 52 69 67 68 74 5b 59 5d 20 20 26 26 20 20 61 43  Right[Y]  &&  aC
1ae78 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 58 5d 5d  ontent[aLeft[X]]
1ae79 20 3d 3d 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69   == aContent[aRi
1ae7a 67 68 74 5b 59 5d 5d 0d 0a 2a 2a 0d 0a 2a 2a 20  ght[Y]]..**..** 
1ae7b 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e  When that happen
1ae7c 73 2c 20 6f 6d 69 74 20 74 68 65 20 61 4c 65 66  s, omit the aLef
1ae7d 74 5b 58 5d 20 61 6e 64 20 75 73 65 20 74 68 65  t[X] and use the
1ae7e 20 61 52 69 67 68 74 5b 59 5d 20 69 6e 64 65 78   aRight[Y] index
1ae7f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
1ae80 69 64 20 77 61 6c 4d 65 72 67 65 28 0d 0a 20 20  id walMerge(..  
1ae81 63 6f 6e 73 74 20 75 33 32 20 2a 61 43 6f 6e 74  const u32 *aCont
1ae82 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ent,            
1ae83 2f 2a 20 50 61 67 65 73 20 69 6e 20 77 61 6c 20  /* Pages in wal 
1ae84 2d 20 6b 65 79 73 20 66 6f 72 20 74 68 65 20 73  - keys for the s
1ae85 6f 72 74 20 2a 2f 0d 0a 20 20 68 74 5f 73 6c 6f  ort */..  ht_slo
1ae86 74 20 2a 61 4c 65 66 74 2c 20 20 20 20 20 20 20  t *aLeft,       
1ae87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 3a            /* IN:
1ae88 20 4c 65 66 74 20 68 61 6e 64 20 69 6e 70 75 74   Left hand input
1ae89 20 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20   list */..  int 
1ae8a 6e 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20  nLeft,          
1ae8b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1ae8c 4e 3a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61  N: Elements in a
1ae8d 72 72 61 79 20 2a 70 61 4c 65 66 74 20 2a 2f 0d  rray *paLeft */.
1ae8e 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 2a 70 61 52  .  ht_slot **paR
1ae8f 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
1ae90 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 52 69     /* IN/OUT: Ri
1ae91 67 68 74 20 68 61 6e 64 20 69 6e 70 75 74 20 6c  ght hand input l
1ae92 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70  ist */..  int *p
1ae93 6e 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  nRight,         
1ae94 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
1ae95 4f 55 54 3a 20 45 6c 65 6d 65 6e 74 73 20 69 6e  OUT: Elements in
1ae96 20 2a 70 61 52 69 67 68 74 20 2a 2f 0d 0a 20 20   *paRight */..  
1ae97 68 74 5f 73 6c 6f 74 20 2a 61 54 6d 70 20 20 20  ht_slot *aTmp   
1ae98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae99 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 62 75 66  /* Temporary buf
1ae9a 66 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  fer */..){..  in
1ae9b 74 20 69 4c 65 66 74 20 3d 20 30 3b 20 20 20 20  t iLeft = 0;    
1ae9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ae9d 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69   Current index i
1ae9e 6e 20 61 4c 65 66 74 20 2a 2f 0d 0a 20 20 69 6e  n aLeft */..  in
1ae9f 74 20 69 52 69 67 68 74 20 3d 20 30 3b 20 20 20  t iRight = 0;   
1aea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1aea1 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69   Current index i
1aea2 6e 20 61 52 69 67 68 74 20 2a 2f 0d 0a 20 20 69  n aRight */..  i
1aea3 6e 74 20 69 4f 75 74 20 3d 20 30 3b 20 20 20 20  nt iOut = 0;    
1aea4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1aea5 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20  * Current index 
1aea6 69 6e 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  in output buffer
1aea7 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 69 67 68   */..  int nRigh
1aea8 74 20 3d 20 2a 70 6e 52 69 67 68 74 3b 0d 0a 20  t = *pnRight;.. 
1aea9 20 68 74 5f 73 6c 6f 74 20 2a 61 52 69 67 68 74   ht_slot *aRight
1aeaa 20 3d 20 2a 70 61 52 69 67 68 74 3b 0d 0a 0d 0a   = *paRight;....
1aeab 20 20 61 73 73 65 72 74 28 20 6e 4c 65 66 74 3e    assert( nLeft>
1aeac 30 20 26 26 20 6e 52 69 67 68 74 3e 30 20 29 3b  0 && nRight>0 );
1aead 0d 0a 20 20 77 68 69 6c 65 28 20 69 52 69 67 68  ..  while( iRigh
1aeae 74 3c 6e 52 69 67 68 74 20 7c 7c 20 69 4c 65 66  t<nRight || iLef
1aeaf 74 3c 6e 4c 65 66 74 20 29 7b 0d 0a 20 20 20 20  t<nLeft ){..    
1aeb0 68 74 5f 73 6c 6f 74 20 6c 6f 67 70 61 67 65 3b  ht_slot logpage;
1aeb1 0d 0a 20 20 20 20 50 67 6e 6f 20 64 62 70 61 67  ..    Pgno dbpag
1aeb2 65 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 28 69  e;....    if( (i
1aeb3 4c 65 66 74 3c 6e 4c 65 66 74 29 20 0d 0a 20 20  Left<nLeft) ..  
1aeb4 20 20 20 26 26 20 28 69 52 69 67 68 74 3e 3d 6e     && (iRight>=n
1aeb5 52 69 67 68 74 20 7c 7c 20 61 43 6f 6e 74 65 6e  Right || aConten
1aeb6 74 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 3c  t[aLeft[iLeft]]<
1aeb7 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 5b  aContent[aRight[
1aeb8 69 52 69 67 68 74 5d 5d 29 0d 0a 20 20 20 20 29  iRight]])..    )
1aeb9 7b 0d 0a 20 20 20 20 20 20 6c 6f 67 70 61 67 65  {..      logpage
1aeba 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 2b 2b   = aLeft[iLeft++
1aebb 5d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ];..    }else{..
1aebc 20 20 20 20 20 20 6c 6f 67 70 61 67 65 20 3d 20        logpage = 
1aebd 61 52 69 67 68 74 5b 69 52 69 67 68 74 2b 2b 5d  aRight[iRight++]
1aebe 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64 62  ;..    }..    db
1aebf 70 61 67 65 20 3d 20 61 43 6f 6e 74 65 6e 74 5b  page = aContent[
1aec0 6c 6f 67 70 61 67 65 5d 3b 0d 0a 0d 0a 20 20 20  logpage];....   
1aec1 20 61 54 6d 70 5b 69 4f 75 74 2b 2b 5d 20 3d 20   aTmp[iOut++] = 
1aec2 6c 6f 67 70 61 67 65 3b 0d 0a 20 20 20 20 69 66  logpage;..    if
1aec3 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 26 26  ( iLeft<nLeft &&
1aec4 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b   aContent[aLeft[
1aec5 69 4c 65 66 74 5d 5d 3d 3d 64 62 70 61 67 65 20  iLeft]]==dbpage 
1aec6 29 20 69 4c 65 66 74 2b 2b 3b 0d 0a 0d 0a 20 20  ) iLeft++;....  
1aec7 20 20 61 73 73 65 72 74 28 20 69 4c 65 66 74 3e    assert( iLeft>
1aec8 3d 6e 4c 65 66 74 20 7c 7c 20 61 43 6f 6e 74 65  =nLeft || aConte
1aec9 6e 74 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d  nt[aLeft[iLeft]]
1aeca 3e 64 62 70 61 67 65 20 29 3b 0d 0a 20 20 20 20  >dbpage );..    
1aecb 61 73 73 65 72 74 28 20 69 52 69 67 68 74 3e 3d  assert( iRight>=
1aecc 6e 52 69 67 68 74 20 7c 7c 20 61 43 6f 6e 74 65  nRight || aConte
1aecd 6e 74 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  nt[aRight[iRight
1aece 5d 5d 3e 64 62 70 61 67 65 20 29 3b 0d 0a 20 20  ]]>dbpage );..  
1aecf 7d 0d 0a 0d 0a 20 20 2a 70 61 52 69 67 68 74 20  }....  *paRight 
1aed0 3d 20 61 4c 65 66 74 3b 0d 0a 20 20 2a 70 6e 52  = aLeft;..  *pnR
1aed1 69 67 68 74 20 3d 20 69 4f 75 74 3b 0d 0a 20 20  ight = iOut;..  
1aed2 6d 65 6d 63 70 79 28 61 4c 65 66 74 2c 20 61 54  memcpy(aLeft, aT
1aed3 6d 70 2c 20 73 69 7a 65 6f 66 28 61 54 6d 70 5b  mp, sizeof(aTmp[
1aed4 30 5d 29 2a 69 4f 75 74 29 3b 0d 0a 7d 0d 0a 0d  0])*iOut);..}...
1aed5 0a 2f 2a 0d 0a 2a 2a 20 53 6f 72 74 20 74 68 65  ./*..** Sort the
1aed6 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 6c 69 73   elements in lis
1aed7 74 20 61 4c 69 73 74 20 75 73 69 6e 67 20 61 43  t aList using aC
1aed8 6f 6e 74 65 6e 74 5b 5d 20 61 73 20 74 68 65 20  ontent[] as the 
1aed9 73 6f 72 74 20 6b 65 79 2e 0d 0a 2a 2a 20 52 65  sort key...** Re
1aeda 6d 6f 76 65 20 65 6c 65 6d 65 6e 74 73 20 77 69  move elements wi
1aedb 74 68 20 64 75 70 6c 69 63 61 74 65 20 6b 65 79  th duplicate key
1aedc 73 2c 20 70 72 65 66 65 72 72 69 6e 67 20 74 6f  s, preferring to
1aedd 20 6b 65 65 70 20 74 68 65 0d 0a 2a 2a 20 6c 61   keep the..** la
1aede 72 67 65 72 20 61 4c 69 73 74 5b 5d 20 76 61 6c  rger aList[] val
1aedf 75 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ues...**..** The
1aee0 20 61 4c 69 73 74 5b 5d 20 65 6e 74 72 69 65 73   aList[] entries
1aee1 20 61 72 65 20 69 6e 64 69 63 65 73 20 69 6e 74   are indices int
1aee2 6f 20 61 43 6f 6e 74 65 6e 74 5b 5d 2e 20 20 54  o aContent[].  T
1aee3 68 65 20 76 61 6c 75 65 73 20 69 6e 0d 0a 2a 2a  he values in..**
1aee4 20 61 4c 69 73 74 5b 5d 20 61 72 65 20 74 6f 20   aList[] are to 
1aee5 62 65 20 73 6f 72 74 65 64 20 73 6f 20 74 68 61  be sorted so tha
1aee6 74 20 66 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0d 0a  t for all J<K:..
1aee7 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 61 43 6f 6e  **..**      aCon
1aee8 74 65 6e 74 5b 61 4c 69 73 74 5b 4a 5d 5d 20 3c  tent[aList[J]] <
1aee9 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73 74 5b   aContent[aList[
1aeea 4b 5d 5d 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20  K]]..**..** For 
1aeeb 61 6e 79 20 58 20 61 6e 64 20 59 20 73 75 63 68  any X and Y such
1aeec 20 74 68 61 74 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20   that..**..**   
1aeed 20 20 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73     aContent[aLis
1aeee 74 5b 58 5d 5d 20 3d 3d 20 61 43 6f 6e 74 65 6e  t[X]] == aConten
1aeef 74 5b 61 4c 69 73 74 5b 59 5d 5d 0d 0a 2a 2a 0d  t[aList[Y]]..**.
1aef0 0a 2a 2a 20 4b 65 65 70 20 74 68 65 20 6c 61 72  .** Keep the lar
1aef1 67 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 76  ger of the two v
1aef2 61 6c 75 65 73 20 61 4c 69 73 74 5b 58 5d 20 61  alues aList[X] a
1aef3 6e 64 20 61 4c 69 73 74 5b 59 5d 20 61 6e 64 20  nd aList[Y] and 
1aef4 64 69 73 63 61 72 64 0d 0a 2a 2a 20 74 68 65 20  discard..** the 
1aef5 73 6d 61 6c 6c 65 72 2e 0d 0a 2a 2f 0d 0a 73 74  smaller...*/..st
1aef6 61 74 69 63 20 76 6f 69 64 20 77 61 6c 4d 65 72  atic void walMer
1aef7 67 65 73 6f 72 74 28 0d 0a 20 20 63 6f 6e 73 74  gesort(..  const
1aef8 20 75 33 32 20 2a 61 43 6f 6e 74 65 6e 74 2c 20   u32 *aContent, 
1aef9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1aefa 67 65 73 20 69 6e 20 77 61 6c 20 2a 2f 0d 0a 20  ges in wal */.. 
1aefb 20 68 74 5f 73 6c 6f 74 20 2a 61 42 75 66 66 65   ht_slot *aBuffe
1aefc 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
1aefd 20 2f 2a 20 42 75 66 66 65 72 20 6f 66 20 61 74   /* Buffer of at
1aefe 20 6c 65 61 73 74 20 2a 70 6e 4c 69 73 74 20 69   least *pnList i
1aeff 74 65 6d 73 20 74 6f 20 75 73 65 20 2a 2f 0d 0a  tems to use */..
1af00 20 20 68 74 5f 73 6c 6f 74 20 2a 61 4c 69 73 74    ht_slot *aList
1af01 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1af02 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4c 69 73    /* IN/OUT: Lis
1af03 74 20 74 6f 20 73 6f 72 74 20 2a 2f 0d 0a 20 20  t to sort */..  
1af04 69 6e 74 20 2a 70 6e 4c 69 73 74 20 20 20 20 20  int *pnList     
1af05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1af06 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4e 75 6d 62 65  /* IN/OUT: Numbe
1af07 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
1af08 20 61 4c 69 73 74 5b 5d 20 2a 2f 0d 0a 29 7b 0d   aList[] */..){.
1af09 0a 20 20 73 74 72 75 63 74 20 53 75 62 6c 69 73  .  struct Sublis
1af0a 74 20 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 4c 69  t {..    int nLi
1af0b 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
1af0c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1af0d 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
1af0e 61 4c 69 73 74 20 2a 2f 0d 0a 20 20 20 20 68 74  aList */..    ht
1af0f 5f 73 6c 6f 74 20 2a 61 4c 69 73 74 3b 20 20 20  _slot *aList;   
1af10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1af11 6f 69 6e 74 65 72 20 74 6f 20 73 75 62 2d 6c 69  ointer to sub-li
1af12 73 74 20 63 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 20  st content */.. 
1af13 20 7d 3b 0d 0a 0d 0a 20 20 63 6f 6e 73 74 20 69   };....  const i
1af14 6e 74 20 6e 4c 69 73 74 20 3d 20 2a 70 6e 4c 69  nt nList = *pnLi
1af15 73 74 3b 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  st;      /* Size
1af16 20 6f 66 20 69 6e 70 75 74 20 6c 69 73 74 20 2a   of input list *
1af17 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65 72 67 65 20  /..  int nMerge 
1af18 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1af19 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1af1a 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 6c 69  f elements in li
1af1b 73 74 20 61 4d 65 72 67 65 20 2a 2f 0d 0a 20 20  st aMerge */..  
1af1c 68 74 5f 73 6c 6f 74 20 2a 61 4d 65 72 67 65 20  ht_slot *aMerge 
1af1d 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1af1e 2f 2a 20 4c 69 73 74 20 74 6f 20 62 65 20 6d 65  /* List to be me
1af1f 72 67 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  rged */..  int i
1af20 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  List;           
1af21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1af22 64 65 78 20 69 6e 74 6f 20 69 6e 70 75 74 20 6c  dex into input l
1af23 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 53  ist */..  int iS
1af24 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ub = 0;         
1af25 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
1af26 65 78 20 69 6e 74 6f 20 61 53 75 62 20 61 72 72  ex into aSub arr
1af27 61 79 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  ay */..  struct 
1af28 53 75 62 6c 69 73 74 20 61 53 75 62 5b 31 33 5d  Sublist aSub[13]
1af29 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61  ;        /* Arra
1af2a 79 20 6f 66 20 73 75 62 2d 6c 69 73 74 73 20 2a  y of sub-lists *
1af2b 2f 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 61 53  /....  memset(aS
1af2c 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 53  ub, 0, sizeof(aS
1af2d 75 62 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ub));..  assert(
1af2e 20 6e 4c 69 73 74 3c 3d 48 41 53 48 54 41 42 4c   nList<=HASHTABL
1af2f 45 5f 4e 50 41 47 45 20 26 26 20 6e 4c 69 73 74  E_NPAGE && nList
1af30 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  >0 );..  assert(
1af31 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45   HASHTABLE_NPAGE
1af32 3d 3d 28 31 3c 3c 28 41 72 72 61 79 53 69 7a 65  ==(1<<(ArraySize
1af33 28 61 53 75 62 29 2d 31 29 29 20 29 3b 0d 0a 0d  (aSub)-1)) );...
1af34 0a 20 20 66 6f 72 28 69 4c 69 73 74 3d 30 3b 20  .  for(iList=0; 
1af35 69 4c 69 73 74 3c 6e 4c 69 73 74 3b 20 69 4c 69  iList<nList; iLi
1af36 73 74 2b 2b 29 7b 0d 0a 20 20 20 20 6e 4d 65 72  st++){..    nMer
1af37 67 65 20 3d 20 31 3b 0d 0a 20 20 20 20 61 4d 65  ge = 1;..    aMe
1af38 72 67 65 20 3d 20 26 61 4c 69 73 74 5b 69 4c 69  rge = &aList[iLi
1af39 73 74 5d 3b 0d 0a 20 20 20 20 66 6f 72 28 69 53  st];..    for(iS
1af3a 75 62 3d 30 3b 20 69 4c 69 73 74 20 26 20 28 31  ub=0; iList & (1
1af3b 3c 3c 69 53 75 62 29 3b 20 69 53 75 62 2b 2b 29  <<iSub); iSub++)
1af3c 7b 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  {..      struct 
1af3d 53 75 62 6c 69 73 74 20 2a 70 20 3d 20 26 61 53  Sublist *p = &aS
1af3e 75 62 5b 69 53 75 62 5d 3b 0d 0a 20 20 20 20 20  ub[iSub];..     
1af3f 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4c 69 73   assert( p->aLis
1af40 74 20 26 26 20 70 2d 3e 6e 4c 69 73 74 3c 3d 28  t && p->nList<=(
1af41 31 3c 3c 69 53 75 62 29 20 29 3b 0d 0a 20 20 20  1<<iSub) );..   
1af42 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4c     assert( p->aL
1af43 69 73 74 3d 3d 26 61 4c 69 73 74 5b 69 4c 69 73  ist==&aList[iLis
1af44 74 26 7e 28 28 32 3c 3c 69 53 75 62 29 2d 31 29  t&~((2<<iSub)-1)
1af45 5d 20 29 3b 0d 0a 20 20 20 20 20 20 77 61 6c 4d  ] );..      walM
1af46 65 72 67 65 28 61 43 6f 6e 74 65 6e 74 2c 20 70  erge(aContent, p
1af47 2d 3e 61 4c 69 73 74 2c 20 70 2d 3e 6e 4c 69 73  ->aList, p->nLis
1af48 74 2c 20 26 61 4d 65 72 67 65 2c 20 26 6e 4d 65  t, &aMerge, &nMe
1af49 72 67 65 2c 20 61 42 75 66 66 65 72 29 3b 0d 0a  rge, aBuffer);..
1af4a 20 20 20 20 7d 0d 0a 20 20 20 20 61 53 75 62 5b      }..    aSub[
1af4b 69 53 75 62 5d 2e 61 4c 69 73 74 20 3d 20 61 4d  iSub].aList = aM
1af4c 65 72 67 65 3b 0d 0a 20 20 20 20 61 53 75 62 5b  erge;..    aSub[
1af4d 69 53 75 62 5d 2e 6e 4c 69 73 74 20 3d 20 6e 4d  iSub].nList = nM
1af4e 65 72 67 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  erge;..  }....  
1af4f 66 6f 72 28 69 53 75 62 2b 2b 3b 20 69 53 75 62  for(iSub++; iSub
1af50 3c 41 72 72 61 79 53 69 7a 65 28 61 53 75 62 29  <ArraySize(aSub)
1af51 3b 20 69 53 75 62 2b 2b 29 7b 0d 0a 20 20 20 20  ; iSub++){..    
1af52 69 66 28 20 6e 4c 69 73 74 20 26 20 28 31 3c 3c  if( nList & (1<<
1af53 69 53 75 62 29 20 29 7b 0d 0a 20 20 20 20 20 20  iSub) ){..      
1af54 73 74 72 75 63 74 20 53 75 62 6c 69 73 74 20 2a  struct Sublist *
1af55 70 20 3d 20 26 61 53 75 62 5b 69 53 75 62 5d 3b  p = &aSub[iSub];
1af56 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
1af57 70 2d 3e 6e 4c 69 73 74 3c 3d 28 31 3c 3c 69 53  p->nList<=(1<<iS
1af58 75 62 29 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  ub) );..      as
1af59 73 65 72 74 28 20 70 2d 3e 61 4c 69 73 74 3d 3d  sert( p->aList==
1af5a 26 61 4c 69 73 74 5b 6e 4c 69 73 74 26 7e 28 28  &aList[nList&~((
1af5b 32 3c 3c 69 53 75 62 29 2d 31 29 5d 20 29 3b 0d  2<<iSub)-1)] );.
1af5c 0a 20 20 20 20 20 20 77 61 6c 4d 65 72 67 65 28  .      walMerge(
1af5d 61 43 6f 6e 74 65 6e 74 2c 20 70 2d 3e 61 4c 69  aContent, p->aLi
1af5e 73 74 2c 20 70 2d 3e 6e 4c 69 73 74 2c 20 26 61  st, p->nList, &a
1af5f 4d 65 72 67 65 2c 20 26 6e 4d 65 72 67 65 2c 20  Merge, &nMerge, 
1af60 61 42 75 66 66 65 72 29 3b 0d 0a 20 20 20 20 7d  aBuffer);..    }
1af61 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
1af62 20 61 4d 65 72 67 65 3d 3d 61 4c 69 73 74 20 29   aMerge==aList )
1af63 3b 0d 0a 20 20 2a 70 6e 4c 69 73 74 20 3d 20 6e  ;..  *pnList = n
1af64 4d 65 72 67 65 3b 0d 0a 0d 0a 23 69 66 64 65 66  Merge;....#ifdef
1af65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
1af66 20 7b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a   {..    int i;..
1af67 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 2a      for(i=1; i<*
1af68 70 6e 4c 69 73 74 3b 20 69 2b 2b 29 7b 0d 0a 20  pnList; i++){.. 
1af69 20 20 20 20 20 61 73 73 65 72 74 28 20 61 43 6f       assert( aCo
1af6a 6e 74 65 6e 74 5b 61 4c 69 73 74 5b 69 5d 5d 20  ntent[aList[i]] 
1af6b 3e 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73 74  > aContent[aList
1af6c 5b 69 2d 31 5d 5d 20 29 3b 0d 0a 20 20 20 20 7d  [i-1]] );..    }
1af6d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 7d  ..  }..#endif..}
1af6e 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 46 72 65 65  ..../* ..** Free
1af6f 20 61 6e 20 69 74 65 72 61 74 6f 72 20 61 6c 6c   an iterator all
1af70 6f 63 61 74 65 64 20 62 79 20 77 61 6c 49 74 65  ocated by walIte
1af71 72 61 74 6f 72 49 6e 69 74 28 29 2e 0d 0a 2a 2f  ratorInit()...*/
1af72 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61  ..static void wa
1af73 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28 57 61  lIteratorFree(Wa
1af74 6c 49 74 65 72 61 74 6f 72 20 2a 70 29 7b 0d 0a  lIterator *p){..
1af75 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68    sqlite3Scratch
1af76 46 72 65 65 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  Free(p);..}..../
1af77 2a 0d 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  *..** Construct 
1af78 61 20 57 61 6c 49 6e 74 65 72 61 74 6f 72 20 6f  a WalInterator o
1af79 62 6a 65 63 74 20 74 68 61 74 20 63 61 6e 20 62  bject that can b
1af7a 65 20 75 73 65 64 20 74 6f 20 6c 6f 6f 70 20 6f  e used to loop o
1af7b 76 65 72 20 61 6c 6c 20 0d 0a 2a 2a 20 70 61 67  ver all ..** pag
1af7c 65 73 20 69 6e 20 74 68 65 20 57 41 4c 20 69 6e  es in the WAL in
1af7d 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
1af7e 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  . The caller mus
1af7f 74 20 68 6f 6c 64 20 74 68 65 20 63 68 65 63 6b  t hold the check
1af80 70 6f 69 6e 74 0d 0a 2a 2a 20 6c 6f 63 6b 2e 0d  point..** lock..
1af81 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  .**..** On succe
1af82 73 73 2c 20 6d 61 6b 65 20 2a 70 70 20 70 6f 69  ss, make *pp poi
1af83 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 6c 79 20  nt to the newly 
1af84 61 6c 6c 6f 63 61 74 65 64 20 57 61 6c 49 6e 74  allocated WalInt
1af85 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 0d 0a 2a  erator object..*
1af86 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * return SQLITE_
1af87 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  OK. Otherwise, r
1af88 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
1af89 6f 64 65 2e 20 49 66 20 74 68 69 73 20 72 6f 75  ode. If this rou
1af8a 74 69 6e 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 73  tine..** returns
1af8b 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 20 76   an error, the v
1af8c 61 6c 75 65 20 6f 66 20 2a 70 70 20 69 73 20 75  alue of *pp is u
1af8d 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a  ndefined...**..*
1af8e 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f  * The calling ro
1af8f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 69 6e 76  utine should inv
1af90 6f 6b 65 20 77 61 6c 49 74 65 72 61 74 6f 72 46  oke walIteratorF
1af91 72 65 65 28 29 20 74 6f 20 64 65 73 74 72 6f 79  ree() to destroy
1af92 20 74 68 65 0d 0a 2a 2a 20 57 61 6c 49 74 65 72   the..** WalIter
1af93 61 74 6f 72 20 6f 62 6a 65 63 74 20 77 68 65 6e  ator object when
1af94 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64   it has finished
1af95 20 77 69 74 68 20 69 74 2e 0d 0a 2a 2f 0d 0a 73   with it...*/..s
1af96 74 61 74 69 63 20 69 6e 74 20 77 61 6c 49 74 65  tatic int walIte
1af97 72 61 74 6f 72 49 6e 69 74 28 57 61 6c 20 2a 70  ratorInit(Wal *p
1af98 57 61 6c 2c 20 57 61 6c 49 74 65 72 61 74 6f 72  Wal, WalIterator
1af99 20 2a 2a 70 70 29 7b 0d 0a 20 20 57 61 6c 49 74   **pp){..  WalIt
1af9a 65 72 61 74 6f 72 20 2a 70 3b 20 20 20 20 20 20  erator *p;      
1af9b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1af9c 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0d 0a 20  turn value */.. 
1af9d 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 3b 20 20   int nSegment;  
1af9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1af9f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65   /* Number of se
1afa0 67 6d 65 6e 74 73 20 74 6f 20 6d 65 72 67 65 20  gments to merge 
1afa1 2a 2f 0d 0a 20 20 75 33 32 20 69 4c 61 73 74 3b  */..  u32 iLast;
1afa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1afa3 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 72        /* Last fr
1afa4 61 6d 65 20 69 6e 20 6c 6f 67 20 2a 2f 0d 0a 20  ame in log */.. 
1afa5 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20   int nByte;     
1afa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1afa7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1afa8 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  tes to allocate 
1afa9 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  */..  int i;    
1afaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1afab 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
1afac 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20  r variable */.. 
1afad 20 68 74 5f 73 6c 6f 74 20 2a 61 54 6d 70 3b 20   ht_slot *aTmp; 
1afae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1afaf 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 75   /* Temp space u
1afb0 73 65 64 20 62 79 20 6d 65 72 67 65 2d 73 6f 72  sed by merge-sor
1afb1 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20 3d  t */..  int rc =
1afb2 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
1afb3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1afb4 6e 20 43 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 2f  n Code */....  /
1afb5 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
1afb6 6e 6c 79 20 72 75 6e 73 20 77 68 69 6c 65 20 68  nly runs while h
1afb7 6f 6c 64 69 6e 67 20 74 68 65 20 63 68 65 63 6b  olding the check
1afb8 70 6f 69 6e 74 20 6c 6f 63 6b 2e 20 41 6e 64 0d  point lock. And.
1afb9 0a 20 20 2a 2a 20 69 74 20 6f 6e 6c 79 20 72 75  .  ** it only ru
1afba 6e 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ns if there is a
1afbb 63 74 75 61 6c 6c 79 20 63 6f 6e 74 65 6e 74 20  ctually content 
1afbc 69 6e 20 74 68 65 20 6c 6f 67 20 28 6d 78 46 72  in the log (mxFr
1afbd 61 6d 65 3e 30 29 2e 0d 0a 20 20 2a 2f 0d 0a 20  ame>0)...  */.. 
1afbe 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 63   assert( pWal->c
1afbf 6b 70 74 4c 6f 63 6b 20 26 26 20 70 57 61 6c 2d  kptLock && pWal-
1afc0 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3e 30 20 29  >hdr.mxFrame>0 )
1afc1 3b 0d 0a 20 20 69 4c 61 73 74 20 3d 20 70 57 61  ;..  iLast = pWa
1afc2 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0d  l->hdr.mxFrame;.
1afc3 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ...  /* Allocate
1afc4 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 57   space for the W
1afc5 61 6c 49 74 65 72 61 74 6f 72 20 6f 62 6a 65 63  alIterator objec
1afc6 74 2e 20 2a 2f 0d 0a 20 20 6e 53 65 67 6d 65 6e  t. */..  nSegmen
1afc7 74 20 3d 20 77 61 6c 46 72 61 6d 65 50 61 67 65  t = walFramePage
1afc8 28 69 4c 61 73 74 29 20 2b 20 31 3b 0d 0a 20 20  (iLast) + 1;..  
1afc9 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 57  nByte = sizeof(W
1afca 61 6c 49 74 65 72 61 74 6f 72 29 20 0d 0a 20 20  alIterator) ..  
1afcb 20 20 20 20 20 20 2b 20 28 6e 53 65 67 6d 65 6e        + (nSegmen
1afcc 74 2d 31 29 2a 73 69 7a 65 6f 66 28 73 74 72 75  t-1)*sizeof(stru
1afcd 63 74 20 57 61 6c 53 65 67 6d 65 6e 74 29 0d 0a  ct WalSegment)..
1afce 20 20 20 20 20 20 20 20 2b 20 69 4c 61 73 74 2a          + iLast*
1afcf 73 69 7a 65 6f 66 28 68 74 5f 73 6c 6f 74 29 3b  sizeof(ht_slot);
1afd0 0d 0a 20 20 70 20 3d 20 28 57 61 6c 49 74 65 72  ..  p = (WalIter
1afd1 61 74 6f 72 20 2a 29 73 71 6c 69 74 65 33 53 63  ator *)sqlite3Sc
1afd2 72 61 74 63 68 4d 61 6c 6c 6f 63 28 6e 42 79 74  ratchMalloc(nByt
1afd3 65 29 3b 0d 0a 20 20 69 66 28 20 21 70 20 29 7b  e);..  if( !p ){
1afd4 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
1afd5 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d  ITE_NOMEM;..  }.
1afd6 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
1afd7 6e 42 79 74 65 29 3b 0d 0a 20 20 70 2d 3e 6e 53  nByte);..  p->nS
1afd8 65 67 6d 65 6e 74 20 3d 20 6e 53 65 67 6d 65 6e  egment = nSegmen
1afd9 74 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63  t;....  /* Alloc
1afda 61 74 65 20 74 65 6d 70 6f 72 61 72 79 20 73 70  ate temporary sp
1afdb 61 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ace used by the 
1afdc 6d 65 72 67 65 2d 73 6f 72 74 20 72 6f 75 74 69  merge-sort routi
1afdd 6e 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0d 0a  ne. This block..
1afde 20 20 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 77    ** of memory w
1afdf 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 65 66  ill be freed bef
1afe0 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
1afe1 6e 20 72 65 74 75 72 6e 73 2e 0d 0a 20 20 2a 2f  n returns...  */
1afe2 0d 0a 20 20 61 54 6d 70 20 3d 20 28 68 74 5f 73  ..  aTmp = (ht_s
1afe3 6c 6f 74 20 2a 29 73 71 6c 69 74 65 33 53 63 72  lot *)sqlite3Scr
1afe4 61 74 63 68 4d 61 6c 6c 6f 63 28 0d 0a 20 20 20  atchMalloc(..   
1afe5 20 20 20 73 69 7a 65 6f 66 28 68 74 5f 73 6c 6f     sizeof(ht_slo
1afe6 74 29 20 2a 20 28 69 4c 61 73 74 3e 48 41 53 48  t) * (iLast>HASH
1afe7 54 41 42 4c 45 5f 4e 50 41 47 45 3f 48 41 53 48  TABLE_NPAGE?HASH
1afe8 54 41 42 4c 45 5f 4e 50 41 47 45 3a 69 4c 61 73  TABLE_NPAGE:iLas
1afe9 74 29 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20  t)..  );..  if( 
1afea 21 61 54 6d 70 20 29 7b 0d 0a 20 20 20 20 72 63  !aTmp ){..    rc
1afeb 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1afec 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 66 6f 72 28 69  ..  }....  for(i
1afed 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
1afee 4b 20 26 26 20 69 3c 6e 53 65 67 6d 65 6e 74 3b  K && i<nSegment;
1afef 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 76 6f 6c 61   i++){..    vola
1aff0 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48  tile ht_slot *aH
1aff1 61 73 68 3b 0d 0a 20 20 20 20 75 33 32 20 69 5a  ash;..    u32 iZ
1aff2 65 72 6f 3b 0d 0a 20 20 20 20 76 6f 6c 61 74 69  ero;..    volati
1aff3 6c 65 20 75 33 32 20 2a 61 50 67 6e 6f 3b 0d 0a  le u32 *aPgno;..
1aff4 0d 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 48 61  ..    rc = walHa
1aff5 73 68 47 65 74 28 70 57 61 6c 2c 20 69 2c 20 26  shGet(pWal, i, &
1aff6 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26  aHash, &aPgno, &
1aff7 69 5a 65 72 6f 29 3b 0d 0a 20 20 20 20 69 66 28  iZero);..    if(
1aff8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1aff9 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20  {..      int j; 
1affa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1affb 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20       /* Counter 
1affc 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  variable */..   
1affd 20 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20     int nEntry;  
1affe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1afff 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
1b000 69 65 73 20 69 6e 20 74 68 69 73 20 73 65 67 6d  ies in this segm
1b001 65 6e 74 20 2a 2f 0d 0a 20 20 20 20 20 20 68 74  ent */..      ht
1b002 5f 73 6c 6f 74 20 2a 61 49 6e 64 65 78 3b 20 20  _slot *aIndex;  
1b003 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 72            /* Sor
1b004 74 65 64 20 69 6e 64 65 78 20 66 6f 72 20 74 68  ted index for th
1b005 69 73 20 73 65 67 6d 65 6e 74 20 2a 2f 0d 0a 0d  is segment */...
1b006 0a 20 20 20 20 20 20 61 50 67 6e 6f 2b 2b 3b 0d  .      aPgno++;.
1b007 0a 20 20 20 20 20 20 69 66 28 20 28 69 2b 31 29  .      if( (i+1)
1b008 3d 3d 6e 53 65 67 6d 65 6e 74 20 29 7b 0d 0a 20  ==nSegment ){.. 
1b009 20 20 20 20 20 20 20 6e 45 6e 74 72 79 20 3d 20         nEntry = 
1b00a 28 69 6e 74 29 28 69 4c 61 73 74 20 2d 20 69 5a  (int)(iLast - iZ
1b00b 65 72 6f 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ero);..      }el
1b00c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 6e 45 6e  se{..        nEn
1b00d 74 72 79 20 3d 20 28 69 6e 74 29 28 28 75 33 32  try = (int)((u32
1b00e 2a 29 61 48 61 73 68 20 2d 20 28 75 33 32 2a 29  *)aHash - (u32*)
1b00f 61 50 67 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 7d  aPgno);..      }
1b010 0d 0a 20 20 20 20 20 20 61 49 6e 64 65 78 20 3d  ..      aIndex =
1b011 20 26 28 28 68 74 5f 73 6c 6f 74 20 2a 29 26 70   &((ht_slot *)&p
1b012 2d 3e 61 53 65 67 6d 65 6e 74 5b 70 2d 3e 6e 53  ->aSegment[p->nS
1b013 65 67 6d 65 6e 74 5d 29 5b 69 5a 65 72 6f 5d 3b  egment])[iZero];
1b014 0d 0a 20 20 20 20 20 20 69 5a 65 72 6f 2b 2b 3b  ..      iZero++;
1b015 0d 0a 20 20 0d 0a 20 20 20 20 20 20 66 6f 72 28  ..  ..      for(
1b016 6a 3d 30 3b 20 6a 3c 6e 45 6e 74 72 79 3b 20 6a  j=0; j<nEntry; j
1b017 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 49  ++){..        aI
1b018 6e 64 65 78 5b 6a 5d 20 3d 20 28 68 74 5f 73 6c  ndex[j] = (ht_sl
1b019 6f 74 29 6a 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ot)j;..      }..
1b01a 20 20 20 20 20 20 77 61 6c 4d 65 72 67 65 73 6f        walMergeso
1b01b 72 74 28 28 75 33 32 20 2a 29 61 50 67 6e 6f 2c  rt((u32 *)aPgno,
1b01c 20 61 54 6d 70 2c 20 61 49 6e 64 65 78 2c 20 26   aTmp, aIndex, &
1b01d 6e 45 6e 74 72 79 29 3b 0d 0a 20 20 20 20 20 20  nEntry);..      
1b01e 70 2d 3e 61 53 65 67 6d 65 6e 74 5b 69 5d 2e 69  p->aSegment[i].i
1b01f 5a 65 72 6f 20 3d 20 69 5a 65 72 6f 3b 0d 0a 20  Zero = iZero;.. 
1b020 20 20 20 20 20 70 2d 3e 61 53 65 67 6d 65 6e 74       p->aSegment
1b021 5b 69 5d 2e 6e 45 6e 74 72 79 20 3d 20 6e 45 6e  [i].nEntry = nEn
1b022 74 72 79 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 61  try;..      p->a
1b023 53 65 67 6d 65 6e 74 5b 69 5d 2e 61 49 6e 64 65  Segment[i].aInde
1b024 78 20 3d 20 61 49 6e 64 65 78 3b 0d 0a 20 20 20  x = aIndex;..   
1b025 20 20 20 70 2d 3e 61 53 65 67 6d 65 6e 74 5b 69     p->aSegment[i
1b026 5d 2e 61 50 67 6e 6f 20 3d 20 28 75 33 32 20 2a  ].aPgno = (u32 *
1b027 29 61 50 67 6e 6f 3b 0d 0a 20 20 20 20 7d 0d 0a  )aPgno;..    }..
1b028 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 53 63    }..  sqlite3Sc
1b029 72 61 74 63 68 46 72 65 65 28 61 54 6d 70 29 3b  ratchFree(aTmp);
1b02a 0d 0a 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ....  if( rc!=SQ
1b02b 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1b02c 77 61 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28  walIteratorFree(
1b02d 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 70 20  p);..  }..  *pp 
1b02e 3d 20 70 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  = p;..  return r
1b02f 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1b030 41 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69  Attempt to obtai
1b031 6e 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20  n the exclusive 
1b032 57 41 4c 20 6c 6f 63 6b 20 64 65 66 69 6e 65 64  WAL lock defined
1b033 20 62 79 20 70 61 72 61 6d 65 74 65 72 73 20 6c   by parameters l
1b034 6f 63 6b 49 64 78 20 61 6e 64 0d 0a 2a 2a 20 6e  ockIdx and..** n
1b035 2e 20 49 66 20 74 68 65 20 61 74 74 65 6d 70 74  . If the attempt
1b036 20 66 61 69 6c 73 20 61 6e 64 20 70 61 72 61 6d   fails and param
1b037 65 74 65 72 20 78 42 75 73 79 20 69 73 20 6e 6f  eter xBusy is no
1b038 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  t NULL, then it 
1b039 69 73 20 61 0d 0a 2a 2a 20 62 75 73 79 2d 68 61  is a..** busy-ha
1b03a 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  ndler function. 
1b03b 49 6e 76 6f 6b 65 20 69 74 20 61 6e 64 20 72 65  Invoke it and re
1b03c 74 72 79 20 74 68 65 20 6c 6f 63 6b 20 75 6e 74  try the lock unt
1b03d 69 6c 20 65 69 74 68 65 72 20 74 68 65 0d 0a 2a  il either the..*
1b03e 2a 20 6c 6f 63 6b 20 69 73 20 73 75 63 63 65 73  * lock is succes
1b03f 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 20  sfully obtained 
1b040 6f 72 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64  or the busy-hand
1b041 6c 65 72 20 72 65 74 75 72 6e 73 20 30 2e 0d 0a  ler returns 0...
1b042 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  */..static int w
1b043 61 6c 42 75 73 79 4c 6f 63 6b 28 0d 0a 20 20 57  alBusyLock(..  W
1b044 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20 20  al *pWal,       
1b045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b046 2a 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e  * WAL connection
1b047 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 42 75   */..  int (*xBu
1b048 73 79 29 28 76 6f 69 64 2a 29 2c 20 20 20 20 20  sy)(void*),     
1b049 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69         /* Functi
1b04a 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20  on to call when 
1b04b 62 75 73 79 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  busy */..  void 
1b04c 2a 70 42 75 73 79 41 72 67 2c 20 20 20 20 20 20  *pBusyArg,      
1b04d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1b04e 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66  ntext argument f
1b04f 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20  or xBusyHandler 
1b050 2a 2f 0d 0a 20 20 69 6e 74 20 6c 6f 63 6b 49 64  */..  int lockId
1b051 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
1b052 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1b053 6f 66 20 66 69 72 73 74 20 62 79 74 65 20 74 6f  of first byte to
1b054 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20   lock */..  int 
1b055 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
1b056 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1b057 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1b058 6f 20 6c 6f 63 6b 20 2a 2f 0d 0a 29 7b 0d 0a 20  o lock */..){.. 
1b059 20 69 6e 74 20 72 63 3b 0d 0a 20 20 64 6f 20 7b   int rc;..  do {
1b05a 0d 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 4c 6f  ..    rc = walLo
1b05b 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c  ckExclusive(pWal
1b05c 2c 20 6c 6f 63 6b 49 64 78 2c 20 6e 29 3b 0d 0a  , lockIdx, n);..
1b05d 20 20 7d 77 68 69 6c 65 28 20 78 42 75 73 79 20    }while( xBusy 
1b05e 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  && rc==SQLITE_BU
1b05f 53 59 20 26 26 20 78 42 75 73 79 28 70 42 75 73  SY && xBusy(pBus
1b060 79 41 72 67 29 20 29 3b 0d 0a 20 20 72 65 74 75  yArg) );..  retu
1b061 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
1b062 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6f 66  .** The cache of
1b063 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68   the wal-index h
1b064 65 61 64 65 72 20 6d 75 73 74 20 62 65 20 76 61  eader must be va
1b065 6c 69 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  lid to call this
1b066 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 52   function...** R
1b067 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 2d 73  eturn the page-s
1b068 69 7a 65 20 69 6e 20 62 79 74 65 73 20 75 73 65  ize in bytes use
1b069 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73  d by the databas
1b06a 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
1b06b 6e 74 20 77 61 6c 50 61 67 65 73 69 7a 65 28 57  nt walPagesize(W
1b06c 61 6c 20 2a 70 57 61 6c 29 7b 0d 0a 20 20 72 65  al *pWal){..  re
1b06d 74 75 72 6e 20 28 70 57 61 6c 2d 3e 68 64 72 2e  turn (pWal->hdr.
1b06e 73 7a 50 61 67 65 26 30 78 66 65 30 30 29 20 2b  szPage&0xfe00) +
1b06f 20 28 28 70 57 61 6c 2d 3e 68 64 72 2e 73 7a 50   ((pWal->hdr.szP
1b070 61 67 65 26 30 78 30 30 30 31 29 3c 3c 31 36 29  age&0x0001)<<16)
1b071 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
1b072 6f 70 79 20 61 73 20 6d 75 63 68 20 63 6f 6e 74  opy as much cont
1b073 65 6e 74 20 61 73 20 77 65 20 63 61 6e 20 66 72  ent as we can fr
1b074 6f 6d 20 74 68 65 20 57 41 4c 20 62 61 63 6b 20  om the WAL back 
1b075 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
1b076 65 20 66 69 6c 65 0d 0a 2a 2a 20 69 6e 20 72 65  e file..** in re
1b077 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 73 71 6c  sponse to an sql
1b078 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f  ite3_wal_checkpo
1b079 69 6e 74 28 29 20 72 65 71 75 65 73 74 20 6f 72  int() request or
1b07a 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 2e   the equivalent.
1b07b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 6d 6f  ..**..** The amo
1b07c 75 6e 74 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69  unt of informati
1b07d 6f 6e 20 63 6f 70 69 65 73 20 66 72 6f 6d 20 57  on copies from W
1b07e 41 4c 20 74 6f 20 64 61 74 61 62 61 73 65 20 6d  AL to database m
1b07f 69 67 68 74 20 62 65 20 6c 69 6d 69 74 65 64 0d  ight be limited.
1b080 0a 2a 2a 20 62 79 20 61 63 74 69 76 65 20 72 65  .** by active re
1b081 61 64 65 72 73 2e 20 20 54 68 69 73 20 72 6f 75  aders.  This rou
1b082 74 69 6e 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  tine will never 
1b083 6f 76 65 72 77 72 69 74 65 20 61 20 64 61 74 61  overwrite a data
1b084 62 61 73 65 20 70 61 67 65 0d 0a 2a 2a 20 74 68  base page..** th
1b085 61 74 20 61 20 63 6f 6e 63 75 72 72 65 6e 74 20  at a concurrent 
1b086 72 65 61 64 65 72 20 6d 69 67 68 74 20 62 65 20  reader might be 
1b087 75 73 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41  using...**..** A
1b088 6c 6c 20 49 2f 4f 20 62 61 72 72 69 65 72 20 6f  ll I/O barrier o
1b089 70 65 72 61 74 69 6f 6e 73 20 28 61 2e 6b 2e 61  perations (a.k.a
1b08a 20 66 73 79 6e 63 73 29 20 6f 63 63 75 72 20 69   fsyncs) occur i
1b08b 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
1b08c 68 65 6e 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 69  hen..** SQLite i
1b08d 73 20 69 6e 20 57 41 4c 2d 6d 6f 64 65 20 69 6e  s in WAL-mode in
1b08e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4e 4f 52   synchronous=NOR
1b08f 4d 41 4c 2e 20 20 54 68 61 74 20 6d 65 61 6e 73  MAL.  That means
1b090 20 74 68 61 74 20 69 66 20 0d 0a 2a 2a 20 63 68   that if ..** ch
1b091 65 63 6b 70 6f 69 6e 74 73 20 61 72 65 20 61 6c  eckpoints are al
1b092 77 61 79 73 20 72 75 6e 20 62 79 20 61 20 62 61  ways run by a ba
1b093 63 6b 67 72 6f 75 6e 64 20 74 68 72 65 61 64 20  ckground thread 
1b094 6f 72 20 62 61 63 6b 67 72 6f 75 6e 64 20 0d 0a  or background ..
1b095 2a 2a 20 70 72 6f 63 65 73 73 2c 20 66 6f 72 65  ** process, fore
1b096 67 72 6f 75 6e 64 20 74 68 72 65 61 64 73 20 77  ground threads w
1b097 69 6c 6c 20 6e 65 76 65 72 20 62 6c 6f 63 6b 20  ill never block 
1b098 6f 6e 20 61 20 6c 65 6e 67 74 68 79 20 66 73 79  on a lengthy fsy
1b099 6e 63 20 63 61 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a  nc call...**..**
1b09a 20 46 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64   Fsync is called
1b09b 20 6f 6e 20 74 68 65 20 57 41 4c 20 62 65 66 6f   on the WAL befo
1b09c 72 65 20 77 72 69 74 69 6e 67 20 63 6f 6e 74 65  re writing conte
1b09d 6e 74 20 6f 75 74 20 6f 66 20 74 68 65 20 57 41  nt out of the WA
1b09e 4c 20 61 6e 64 0d 0a 2a 2a 20 69 6e 74 6f 20 74  L and..** into t
1b09f 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
1b0a0 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  is ensures that 
1b0a1 69 66 20 74 68 65 20 6e 65 77 20 63 6f 6e 74 65  if the new conte
1b0a2 6e 74 20 69 73 20 70 65 72 73 69 73 74 65 6e 74  nt is persistent
1b0a3 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c 20  ..** in the WAL 
1b0a4 61 6e 64 20 63 61 6e 20 62 65 20 72 65 63 6f 76  and can be recov
1b0a5 65 72 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  ered following a
1b0a6 20 70 6f 77 65 72 2d 6c 6f 73 73 20 6f 72 20 68   power-loss or h
1b0a7 61 72 64 20 72 65 73 65 74 2e 0d 0a 2a 2a 0d 0a  ard reset...**..
1b0a8 2a 2a 20 46 73 79 6e 63 20 69 73 20 61 6c 73 6f  ** Fsync is also
1b0a9 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 64   called on the d
1b0aa 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1b0ab 28 61 6e 64 20 6f 6e 6c 79 20 69 66 29 20 74 68  (and only if) th
1b0ac 65 20 65 6e 74 69 72 65 0d 0a 2a 2a 20 57 41 4c  e entire..** WAL
1b0ad 20 63 6f 6e 74 65 6e 74 20 69 73 20 63 6f 70 69   content is copi
1b0ae 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ed into the data
1b0af 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73  base file.  This
1b0b0 20 73 65 63 6f 6e 64 20 66 73 79 6e 63 20 6d 61   second fsync ma
1b0b1 6b 65 73 0d 0a 2a 2a 20 69 74 20 73 61 66 65 20  kes..** it safe 
1b0b2 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 57 41  to delete the WA
1b0b3 4c 20 73 69 6e 63 65 20 74 68 65 20 6e 65 77 20  L since the new 
1b0b4 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 70 65 72  content will per
1b0b5 73 69 73 74 20 69 6e 20 74 68 65 0d 0a 2a 2a 20  sist in the..** 
1b0b6 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
1b0b7 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
1b0b8 69 6e 65 20 75 73 65 73 20 61 6e 64 20 75 70 64  ine uses and upd
1b0b9 61 74 65 73 20 74 68 65 20 6e 42 61 63 6b 66 69  ates the nBackfi
1b0ba 6c 6c 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ll field of the 
1b0bb 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72  wal-index header
1b0bc 2e 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  ...** This is th
1b0bd 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 74  e only routine t
1b0be 68 61 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65  ha will increase
1b0bf 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 42   the value of nB
1b0c0 61 63 6b 66 69 6c 6c 2e 20 20 0d 0a 2a 2a 20 28  ackfill.  ..** (
1b0c1 41 20 57 41 4c 20 72 65 73 65 74 20 6f 72 20 72  A WAL reset or r
1b0c2 65 63 6f 76 65 72 79 20 77 69 6c 6c 20 72 65 76  ecovery will rev
1b0c3 65 72 74 20 6e 42 61 63 6b 66 69 6c 6c 20 74 6f  ert nBackfill to
1b0c4 20 7a 65 72 6f 2c 20 62 75 74 20 6e 6f 74 20 69   zero, but not i
1b0c5 6e 63 72 65 61 73 65 0d 0a 2a 2a 20 69 74 73 20  ncrease..** its 
1b0c6 76 61 6c 75 65 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20  value.)..**..** 
1b0c7 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
1b0c8 62 65 20 68 6f 6c 64 69 6e 67 20 73 75 66 66 69  be holding suffi
1b0c9 63 69 65 6e 74 20 6c 6f 63 6b 73 20 74 6f 20 65  cient locks to e
1b0ca 6e 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74  nsure that no ot
1b0cb 68 65 72 0d 0a 2a 2a 20 63 68 65 63 6b 70 6f 69  her..** checkpoi
1b0cc 6e 74 20 69 73 20 72 75 6e 6e 69 6e 67 20 28 69  nt is running (i
1b0cd 6e 20 61 6e 79 20 6f 74 68 65 72 20 74 68 72 65  n any other thre
1b0ce 61 64 20 6f 72 20 70 72 6f 63 65 73 73 29 20 61  ad or process) a
1b0cf 74 20 74 68 65 20 73 61 6d 65 0d 0a 2a 2a 20 74  t the same..** t
1b0d0 69 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ime...*/..static
1b0d1 20 69 6e 74 20 77 61 6c 43 68 65 63 6b 70 6f 69   int walCheckpoi
1b0d2 6e 74 28 0d 0a 20 20 57 61 6c 20 2a 70 57 61 6c  nt(..  Wal *pWal
1b0d3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1b0d4 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 63 6f         /* Wal co
1b0d5 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69  nnection */..  i
1b0d6 6e 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20  nt eMode,       
1b0d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b0d8 2a 20 4f 6e 65 20 6f 66 20 50 41 53 53 49 56 45  * One of PASSIVE
1b0d9 2c 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52  , FULL or RESTAR
1b0da 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 42  T */..  int (*xB
1b0db 75 73 79 43 61 6c 6c 29 28 76 6f 69 64 2a 29 2c  usyCall)(void*),
1b0dc 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74          /* Funct
1b0dd 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e  ion to call when
1b0de 20 62 75 73 79 20 2a 2f 0d 0a 20 20 76 6f 69 64   busy */..  void
1b0df 20 2a 70 42 75 73 79 41 72 67 2c 20 20 20 20 20   *pBusyArg,     
1b0e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1b0e1 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20  ontext argument 
1b0e2 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72  for xBusyHandler
1b0e3 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 79 6e 63 5f   */..  int sync_
1b0e4 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
1b0e5 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1b0e6 66 6f 72 20 4f 73 53 79 6e 63 28 29 20 28 6f 72  for OsSync() (or
1b0e7 20 30 29 20 2a 2f 0d 0a 20 20 75 38 20 2a 7a 42   0) */..  u8 *zB
1b0e8 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  uf              
1b0e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
1b0ea 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74 6f  porary buffer to
1b0eb 20 75 73 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69   use */..){..  i
1b0ec 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1b0ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b0ee 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
1b0ef 0d 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20  ..  int szPage; 
1b0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0f1 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1b0f2 70 61 67 65 2d 73 69 7a 65 20 2a 2f 0d 0a 20 20  page-size */..  
1b0f3 57 61 6c 49 74 65 72 61 74 6f 72 20 2a 70 49 74  WalIterator *pIt
1b0f4 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  er = 0;         
1b0f5 2f 2a 20 57 61 6c 20 69 74 65 72 61 74 6f 72 20  /* Wal iterator 
1b0f6 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 75 33  context */..  u3
1b0f7 32 20 69 44 62 70 61 67 65 20 3d 20 30 3b 20 20  2 iDbpage = 0;  
1b0f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b0f9 20 4e 65 78 74 20 64 61 74 61 62 61 73 65 20 70   Next database p
1b0fa 61 67 65 20 74 6f 20 77 72 69 74 65 20 2a 2f 0d  age to write */.
1b0fb 0a 20 20 75 33 32 20 69 46 72 61 6d 65 20 3d 20  .  u32 iFrame = 
1b0fc 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1b0fd 20 20 20 2f 2a 20 57 61 6c 20 66 72 61 6d 65 20     /* Wal frame 
1b0fe 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 20  containing data 
1b0ff 66 6f 72 20 69 44 62 70 61 67 65 20 2a 2f 0d 0a  for iDbpage */..
1b100 20 20 75 33 32 20 6d 78 53 61 66 65 46 72 61 6d    u32 mxSafeFram
1b101 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1b102 20 20 2f 2a 20 4d 61 78 20 66 72 61 6d 65 20 74    /* Max frame t
1b103 68 61 74 20 63 61 6e 20 62 65 20 62 61 63 6b 66  hat can be backf
1b104 69 6c 6c 65 64 20 2a 2f 0d 0a 20 20 75 33 32 20  illed */..  u32 
1b105 6d 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  mxPage;         
1b106 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
1b107 61 78 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ax database page
1b108 20 74 6f 20 77 72 69 74 65 20 2a 2f 0d 0a 20 20   to write */..  
1b109 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1b10a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b10b 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1b10c 2a 2f 0d 0a 20 20 76 6f 6c 61 74 69 6c 65 20 57  */..  volatile W
1b10d 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e 66  alCkptInfo *pInf
1b10e 6f 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 68 65  o;    /* The che
1b10f 63 6b 70 6f 69 6e 74 20 73 74 61 74 75 73 20 69  ckpoint status i
1b110 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0d 0a 20  nformation */.. 
1b111 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f   int (*xBusy)(vo
1b112 69 64 2a 29 20 3d 20 30 3b 20 20 20 20 20 20 20  id*) = 0;       
1b113 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20   /* Function to 
1b114 63 61 6c 6c 20 77 68 65 6e 20 77 61 69 74 69 6e  call when waitin
1b115 67 20 66 6f 72 20 6c 6f 63 6b 73 20 2a 2f 0d 0a  g for locks */..
1b116 0d 0a 20 20 73 7a 50 61 67 65 20 3d 20 77 61 6c  ..  szPage = wal
1b117 50 61 67 65 73 69 7a 65 28 70 57 61 6c 29 3b 0d  Pagesize(pWal);.
1b118 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 50  .  testcase( szP
1b119 61 67 65 3c 3d 33 32 37 36 38 20 29 3b 0d 0a 20  age<=32768 );.. 
1b11a 20 74 65 73 74 63 61 73 65 28 20 73 7a 50 61 67   testcase( szPag
1b11b 65 3e 3d 36 35 35 33 36 20 29 3b 0d 0a 20 20 70  e>=65536 );..  p
1b11c 49 6e 66 6f 20 3d 20 77 61 6c 43 6b 70 74 49 6e  Info = walCkptIn
1b11d 66 6f 28 70 57 61 6c 29 3b 0d 0a 20 20 69 66 28  fo(pWal);..  if(
1b11e 20 70 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c   pInfo->nBackfil
1b11f 6c 3e 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46  l>=pWal->hdr.mxF
1b120 72 61 6d 65 20 29 20 72 65 74 75 72 6e 20 53 51  rame ) return SQ
1b121 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 2f 2a  LITE_OK;....  /*
1b122 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 74   Allocate the it
1b123 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 72 63 20  erator */..  rc 
1b124 3d 20 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69  = walIteratorIni
1b125 74 28 70 57 61 6c 2c 20 26 70 49 74 65 72 29 3b  t(pWal, &pIter);
1b126 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
1b127 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 65  TE_OK ){..    re
1b128 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20  turn rc;..  }.. 
1b129 20 61 73 73 65 72 74 28 20 70 49 74 65 72 20 29   assert( pIter )
1b12a 3b 0d 0a 0d 0a 20 20 69 66 28 20 65 4d 6f 64 65  ;....  if( eMode
1b12b 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f  !=SQLITE_CHECKPO
1b12c 49 4e 54 5f 50 41 53 53 49 56 45 20 29 20 78 42  INT_PASSIVE ) xB
1b12d 75 73 79 20 3d 20 78 42 75 73 79 43 61 6c 6c 3b  usy = xBusyCall;
1b12e 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65  ....  /* Compute
1b12f 20 69 6e 20 6d 78 53 61 66 65 46 72 61 6d 65 20   in mxSafeFrame 
1b130 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
1b131 20 6c 61 73 74 20 66 72 61 6d 65 20 6f 66 20 74   last frame of t
1b132 68 65 20 57 41 4c 20 74 68 61 74 20 69 73 0d 0a  he WAL that is..
1b133 20 20 2a 2a 20 73 61 66 65 20 74 6f 20 77 72 69    ** safe to wri
1b134 74 65 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  te into the data
1b135 62 61 73 65 2e 20 20 46 72 61 6d 65 73 20 62 65  base.  Frames be
1b136 79 6f 6e 64 20 6d 78 53 61 66 65 46 72 61 6d 65  yond mxSafeFrame
1b137 20 6d 69 67 68 74 0d 0a 20 20 2a 2a 20 6f 76 65   might..  ** ove
1b138 72 77 72 69 74 65 20 64 61 74 61 62 61 73 65 20  rwrite database 
1b139 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 69  pages that are i
1b13a 6e 20 75 73 65 20 62 79 20 61 63 74 69 76 65 20  n use by active 
1b13b 72 65 61 64 65 72 73 20 61 6e 64 20 74 68 75 73  readers and thus
1b13c 0d 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 62 65  ..  ** cannot be
1b13d 20 62 61 63 6b 66 69 6c 6c 65 64 20 66 72 6f 6d   backfilled from
1b13e 20 74 68 65 20 57 41 4c 2e 0d 0a 20 20 2a 2f 0d   the WAL...  */.
1b13f 0a 20 20 6d 78 53 61 66 65 46 72 61 6d 65 20 3d  .  mxSafeFrame =
1b140 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61   pWal->hdr.mxFra
1b141 6d 65 3b 0d 0a 20 20 6d 78 50 61 67 65 20 3d 20  me;..  mxPage = 
1b142 70 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 3b  pWal->hdr.nPage;
1b143 0d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 57  ..  for(i=1; i<W
1b144 41 4c 5f 4e 52 45 41 44 45 52 3b 20 69 2b 2b 29  AL_NREADER; i++)
1b145 7b 0d 0a 20 20 20 20 75 33 32 20 79 20 3d 20 70  {..    u32 y = p
1b146 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b  Info->aReadMark[
1b147 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 6d 78 53  i];..    if( mxS
1b148 61 66 65 46 72 61 6d 65 3e 79 20 29 7b 0d 0a 20  afeFrame>y ){.. 
1b149 20 20 20 20 20 61 73 73 65 72 74 28 20 79 3c 3d       assert( y<=
1b14a 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1b14b 65 20 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  e );..      rc =
1b14c 20 77 61 6c 42 75 73 79 4c 6f 63 6b 28 70 57 61   walBusyLock(pWa
1b14d 6c 2c 20 78 42 75 73 79 2c 20 70 42 75 73 79 41  l, xBusy, pBusyA
1b14e 72 67 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43  rg, WAL_READ_LOC
1b14f 4b 28 69 29 2c 20 31 29 3b 0d 0a 20 20 20 20 20  K(i), 1);..     
1b150 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1b151 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  OK ){..        p
1b152 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b  Info->aReadMark[
1b153 69 5d 20 3d 20 52 45 41 44 4d 41 52 4b 5f 4e 4f  i] = READMARK_NO
1b154 54 5f 55 53 45 44 3b 0d 0a 20 20 20 20 20 20 20  T_USED;..       
1b155 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73   walUnlockExclus
1b156 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45  ive(pWal, WAL_RE
1b157 41 44 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0d  AD_LOCK(i), 1);.
1b158 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1b159 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1b15a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 6d 78 53   ){..        mxS
1b15b 61 66 65 46 72 61 6d 65 20 3d 20 79 3b 0d 0a 20  afeFrame = y;.. 
1b15c 20 20 20 20 20 20 20 78 42 75 73 79 20 3d 20 30         xBusy = 0
1b15d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
1b15e 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 77 61  .        goto wa
1b15f 6c 63 68 65 63 6b 70 6f 69 6e 74 5f 6f 75 74 3b  lcheckpoint_out;
1b160 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
1b161 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70  ..  }....  if( p
1b162 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3c  Info->nBackfill<
1b163 6d 78 53 61 66 65 46 72 61 6d 65 0d 0a 20 20 20  mxSafeFrame..   
1b164 26 26 20 28 72 63 20 3d 20 77 61 6c 42 75 73 79  && (rc = walBusy
1b165 4c 6f 63 6b 28 70 57 61 6c 2c 20 78 42 75 73 79  Lock(pWal, xBusy
1b166 2c 20 70 42 75 73 79 41 72 67 2c 20 57 41 4c 5f  , pBusyArg, WAL_
1b167 52 45 41 44 5f 4c 4f 43 4b 28 30 29 2c 20 31 29  READ_LOCK(0), 1)
1b168 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20 20  )==SQLITE_OK..  
1b169 29 7b 0d 0a 20 20 20 20 69 36 34 20 6e 53 69 7a  ){..    i64 nSiz
1b16a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1b16b 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1b16c 20 73 69 7a 65 20 6f 66 20 64 61 74 61 62 61 73   size of databas
1b16d 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20 75  e file */..    u
1b16e 33 32 20 6e 42 61 63 6b 66 69 6c 6c 20 3d 20 70  32 nBackfill = p
1b16f 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3b  Info->nBackfill;
1b170 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20  ....    /* Sync 
1b171 74 68 65 20 57 41 4c 20 74 6f 20 64 69 73 6b 20  the WAL to disk 
1b172 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 79 6e 63  */..    if( sync
1b173 5f 66 6c 61 67 73 20 29 7b 0d 0a 20 20 20 20 20  _flags ){..     
1b174 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
1b175 79 6e 63 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64  ync(pWal->pWalFd
1b176 2c 20 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0d 0a  , sync_flags);..
1b177 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
1b178 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1b179 66 69 6c 65 20 6d 61 79 20 67 72 6f 77 20 61 73  file may grow as
1b17a 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 69   a result of thi
1b17b 73 20 63 68 65 63 6b 70 6f 69 6e 74 2c 20 68 69  s checkpoint, hi
1b17c 6e 74 0d 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74  nt..    ** about
1b17d 20 74 68 65 20 65 76 65 6e 74 75 61 6c 20 73 69   the eventual si
1b17e 7a 65 20 6f 66 20 74 68 65 20 64 62 20 66 69 6c  ze of the db fil
1b17f 65 20 74 6f 20 74 68 65 20 56 46 53 20 6c 61 79  e to the VFS lay
1b180 65 72 2e 20 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  er. ..    */..  
1b181 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1b182 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69 36  _OK ){..      i6
1b183 34 20 6e 52 65 71 20 3d 20 28 28 69 36 34 29 6d  4 nReq = ((i64)m
1b184 78 50 61 67 65 20 2a 20 73 7a 50 61 67 65 29 3b  xPage * szPage);
1b185 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ..      rc = sql
1b186 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
1b187 57 61 6c 2d 3e 70 44 62 46 64 2c 20 26 6e 53 69  Wal->pDbFd, &nSi
1b188 7a 65 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ze);..      if( 
1b189 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1b18a 20 6e 53 69 7a 65 3c 6e 52 65 71 20 29 7b 0d 0a   nSize<nReq ){..
1b18b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
1b18c 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74  sFileControlHint
1b18d 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 53 51  (pWal->pDbFd, SQ
1b18e 4c 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f  LITE_FCNTL_SIZE_
1b18f 48 49 4e 54 2c 20 26 6e 52 65 71 29 3b 0d 0a 20  HINT, &nReq);.. 
1b190 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d       }..    }...
1b191 0a 20 20 20 20 2f 2a 20 49 74 65 72 61 74 65 20  .    /* Iterate 
1b192 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e 74  through the cont
1b193 65 6e 74 73 20 6f 66 20 74 68 65 20 57 41 4c 2c  ents of the WAL,
1b194 20 63 6f 70 79 69 6e 67 20 64 61 74 61 20 74 6f   copying data to
1b195 20 74 68 65 20 64 62 20 66 69 6c 65 2e 20 2a 2f   the db file. */
1b196 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d  ..    while( rc=
1b197 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d  =SQLITE_OK && 0=
1b198 3d 77 61 6c 49 74 65 72 61 74 6f 72 4e 65 78 74  =walIteratorNext
1b199 28 70 49 74 65 72 2c 20 26 69 44 62 70 61 67 65  (pIter, &iDbpage
1b19a 2c 20 26 69 46 72 61 6d 65 29 20 29 7b 0d 0a 20  , &iFrame) ){.. 
1b19b 20 20 20 20 20 69 36 34 20 69 4f 66 66 73 65 74       i64 iOffset
1b19c 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
1b19d 20 77 61 6c 46 72 61 6d 65 50 67 6e 6f 28 70 57   walFramePgno(pW
1b19e 61 6c 2c 20 69 46 72 61 6d 65 29 3d 3d 69 44 62  al, iFrame)==iDb
1b19f 70 61 67 65 20 29 3b 0d 0a 20 20 20 20 20 20 69  page );..      i
1b1a0 66 28 20 69 46 72 61 6d 65 3c 3d 6e 42 61 63 6b  f( iFrame<=nBack
1b1a1 66 69 6c 6c 20 7c 7c 20 69 46 72 61 6d 65 3e 6d  fill || iFrame>m
1b1a2 78 53 61 66 65 46 72 61 6d 65 20 7c 7c 20 69 44  xSafeFrame || iD
1b1a3 62 70 61 67 65 3e 6d 78 50 61 67 65 20 29 20 63  bpage>mxPage ) c
1b1a4 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20  ontinue;..      
1b1a5 69 4f 66 66 73 65 74 20 3d 20 77 61 6c 46 72 61  iOffset = walFra
1b1a6 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65 2c  meOffset(iFrame,
1b1a7 20 73 7a 50 61 67 65 29 20 2b 20 57 41 4c 5f 46   szPage) + WAL_F
1b1a8 52 41 4d 45 5f 48 44 52 53 49 5a 45 3b 0d 0a 20  RAME_HDRSIZE;.. 
1b1a9 20 20 20 20 20 2f 2a 20 74 65 73 74 63 61 73 65       /* testcase
1b1aa 28 20 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f 66  ( IS_BIG_INT(iOf
1b1ab 66 73 65 74 29 20 29 3b 20 2f 2f 20 72 65 71 75  fset) ); // requ
1b1ac 69 72 65 73 20 61 20 34 47 69 42 20 57 41 4c 20  ires a 4GiB WAL 
1b1ad 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20 20 20 72  file */..      r
1b1ae 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
1b1af 64 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20  d(pWal->pWalFd, 
1b1b0 7a 42 75 66 2c 20 73 7a 50 61 67 65 2c 20 69 4f  zBuf, szPage, iO
1b1b1 66 66 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 69  ffset);..      i
1b1b2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1b1b3 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
1b1b4 20 69 4f 66 66 73 65 74 20 3d 20 28 69 44 62 70   iOffset = (iDbp
1b1b5 61 67 65 2d 31 29 2a 28 69 36 34 29 73 7a 50 61  age-1)*(i64)szPa
1b1b6 67 65 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63  ge;..      testc
1b1b7 61 73 65 28 20 49 53 5f 42 49 47 5f 49 4e 54 28  ase( IS_BIG_INT(
1b1b8 69 4f 66 66 73 65 74 29 20 29 3b 0d 0a 20 20 20  iOffset) );..   
1b1b9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1b1ba 73 57 72 69 74 65 28 70 57 61 6c 2d 3e 70 44 62  sWrite(pWal->pDb
1b1bb 46 64 2c 20 7a 42 75 66 2c 20 73 7a 50 61 67 65  Fd, zBuf, szPage
1b1bc 2c 20 69 4f 66 66 73 65 74 29 3b 0d 0a 20 20 20  , iOffset);..   
1b1bd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1b1be 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0d 0a 20  E_OK ) break;.. 
1b1bf 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49     }....    /* I
1b1c0 66 20 77 6f 72 6b 20 77 61 73 20 61 63 74 75 61  f work was actua
1b1c1 6c 6c 79 20 61 63 63 6f 6d 70 6c 69 73 68 65 64  lly accomplished
1b1c2 2e 2e 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  ... */..    if( 
1b1c3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1b1c4 0d 0a 20 20 20 20 20 20 69 66 28 20 6d 78 53 61  ..      if( mxSa
1b1c5 66 65 46 72 61 6d 65 3d 3d 77 61 6c 49 6e 64 65  feFrame==walInde
1b1c6 78 48 64 72 28 70 57 61 6c 29 2d 3e 6d 78 46 72  xHdr(pWal)->mxFr
1b1c7 61 6d 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ame ){..        
1b1c8 69 36 34 20 73 7a 44 62 20 3d 20 70 57 61 6c 2d  i64 szDb = pWal-
1b1c9 3e 68 64 72 2e 6e 50 61 67 65 2a 28 69 36 34 29  >hdr.nPage*(i64)
1b1ca 73 7a 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 20  szPage;..       
1b1cb 20 74 65 73 74 63 61 73 65 28 20 49 53 5f 42 49   testcase( IS_BI
1b1cc 47 5f 49 4e 54 28 73 7a 44 62 29 20 29 3b 0d 0a  G_INT(szDb) );..
1b1cd 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1b1ce 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
1b1cf 57 61 6c 2d 3e 70 44 62 46 64 2c 20 73 7a 44 62  Wal->pDbFd, szDb
1b1d0 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
1b1d1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1b1d2 20 73 79 6e 63 5f 66 6c 61 67 73 20 29 7b 0d 0a   sync_flags ){..
1b1d3 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
1b1d4 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 57 61  qlite3OsSync(pWa
1b1d5 6c 2d 3e 70 44 62 46 64 2c 20 73 79 6e 63 5f 66  l->pDbFd, sync_f
1b1d6 6c 61 67 73 29 3b 0d 0a 20 20 20 20 20 20 20 20  lags);..        
1b1d7 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
1b1d8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1b1d9 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
1b1da 70 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c  pInfo->nBackfill
1b1db 20 3d 20 6d 78 53 61 66 65 46 72 61 6d 65 3b 0d   = mxSafeFrame;.
1b1dc 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
1b1dd 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73  ...    /* Releas
1b1de 65 20 74 68 65 20 72 65 61 64 65 72 20 6c 6f 63  e the reader loc
1b1df 6b 20 68 65 6c 64 20 77 68 69 6c 65 20 62 61 63  k held while bac
1b1e0 6b 66 69 6c 6c 69 6e 67 20 2a 2f 0d 0a 20 20 20  kfilling */..   
1b1e1 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73   walUnlockExclus
1b1e2 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45  ive(pWal, WAL_RE
1b1e3 41 44 5f 4c 4f 43 4b 28 30 29 2c 20 31 29 3b 0d  AD_LOCK(0), 1);.
1b1e4 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63  .  }....  if( rc
1b1e5 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  ==SQLITE_BUSY ){
1b1e6 0d 0a 20 20 20 20 2f 2a 20 52 65 73 65 74 20 74  ..    /* Reset t
1b1e7 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 73  he return code s
1b1e8 6f 20 61 73 20 6e 6f 74 20 74 6f 20 72 65 70 6f  o as not to repo
1b1e9 72 74 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20  rt a checkpoint 
1b1ea 66 61 69 6c 75 72 65 0d 0a 20 20 20 20 2a 2a 20  failure..    ** 
1b1eb 6a 75 73 74 20 62 65 63 61 75 73 65 20 74 68 65  just because the
1b1ec 72 65 20 61 72 65 20 61 63 74 69 76 65 20 72 65  re are active re
1b1ed 61 64 65 72 73 2e 20 20 2a 2f 0d 0a 20 20 20 20  aders.  */..    
1b1ee 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
1b1ef 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
1b1f0 74 68 69 73 20 69 73 20 61 6e 20 53 51 4c 49 54  this is an SQLIT
1b1f1 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53  E_CHECKPOINT_RES
1b1f2 54 41 52 54 20 6f 70 65 72 61 74 69 6f 6e 2c 20  TART operation, 
1b1f3 61 6e 64 20 74 68 65 20 65 6e 74 69 72 65 20 77  and the entire w
1b1f4 61 6c 0d 0a 20 20 2a 2a 20 66 69 6c 65 20 68 61  al..  ** file ha
1b1f5 73 20 62 65 65 6e 20 63 6f 70 69 65 64 20 69 6e  s been copied in
1b1f6 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
1b1f7 66 69 6c 65 2c 20 74 68 65 6e 20 62 6c 6f 63 6b  file, then block
1b1f8 20 75 6e 74 69 6c 20 61 6c 6c 0d 0a 20 20 2a 2a   until all..  **
1b1f9 20 72 65 61 64 65 72 73 20 68 61 76 65 20 66 69   readers have fi
1b1fa 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65  nished using the
1b1fb 20 77 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20   wal file. This 
1b1fc 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65  ensures that the
1b1fd 20 6e 65 78 74 0d 0a 20 20 2a 2a 20 70 72 6f 63   next..  ** proc
1b1fe 65 73 73 20 74 6f 20 77 72 69 74 65 20 74 6f 20  ess to write to 
1b1ff 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 73  the database res
1b200 74 61 72 74 73 20 74 68 65 20 77 61 6c 20 66 69  tarts the wal fi
1b201 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  le...  */..  if(
1b202 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1b203 26 20 65 4d 6f 64 65 21 3d 53 51 4c 49 54 45 5f  & eMode!=SQLITE_
1b204 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49  CHECKPOINT_PASSI
1b205 56 45 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  VE ){..    asser
1b206 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f  t( pWal->writeLo
1b207 63 6b 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  ck );..    if( p
1b208 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3c  Info->nBackfill<
1b209 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1b20a 65 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  e ){..      rc =
1b20b 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
1b20c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 4d 6f     }else if( eMo
1b20d 64 65 3d 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b  de==SQLITE_CHECK
1b20e 50 4f 49 4e 54 5f 52 45 53 54 41 52 54 20 29 7b  POINT_RESTART ){
1b20f 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
1b210 6d 78 53 61 66 65 46 72 61 6d 65 3d 3d 70 57 61  mxSafeFrame==pWa
1b211 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 29  l->hdr.mxFrame )
1b212 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61  ;..      rc = wa
1b213 6c 42 75 73 79 4c 6f 63 6b 28 70 57 61 6c 2c 20  lBusyLock(pWal, 
1b214 78 42 75 73 79 2c 20 70 42 75 73 79 41 72 67 2c  xBusy, pBusyArg,
1b215 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 31   WAL_READ_LOCK(1
1b216 29 2c 20 57 41 4c 5f 4e 52 45 41 44 45 52 2d 31  ), WAL_NREADER-1
1b217 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
1b218 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1b219 20 20 20 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63          walUnloc
1b21a 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c  kExclusive(pWal,
1b21b 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 31   WAL_READ_LOCK(1
1b21c 29 2c 20 57 41 4c 5f 4e 52 45 41 44 45 52 2d 31  ), WAL_NREADER-1
1b21d 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
1b21e 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 77 61 6c 63   }..  }.... walc
1b21f 68 65 63 6b 70 6f 69 6e 74 5f 6f 75 74 3a 0d 0a  heckpoint_out:..
1b220 20 20 77 61 6c 49 74 65 72 61 74 6f 72 46 72 65    walIteratorFre
1b221 65 28 70 49 74 65 72 29 3b 0d 0a 20 20 72 65 74  e(pIter);..  ret
1b222 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1b223 0d 0a 2a 2a 20 49 66 20 74 68 65 20 57 41 4c 20  ..** If the WAL 
1b224 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c  file is currentl
1b225 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 4d  y larger than nM
1b226 61 78 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ax bytes in size
1b227 2c 20 74 72 75 6e 63 61 74 65 0d 0a 2a 2a 20 69  , truncate..** i
1b228 74 20 74 6f 20 65 78 61 63 74 6c 79 20 6e 4d 61  t to exactly nMa
1b229 78 20 62 79 74 65 73 2e 20 49 66 20 61 6e 20 65  x bytes. If an e
1b22a 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1b22b 65 20 64 6f 69 6e 67 20 73 6f 2c 20 69 67 6e 6f  e doing so, igno
1b22c 72 65 20 69 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  re it...*/..stat
1b22d 69 63 20 76 6f 69 64 20 77 61 6c 4c 69 6d 69 74  ic void walLimit
1b22e 53 69 7a 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20  Size(Wal *pWal, 
1b22f 69 36 34 20 6e 4d 61 78 29 7b 0d 0a 20 20 69 36  i64 nMax){..  i6
1b230 34 20 73 7a 3b 0d 0a 20 20 69 6e 74 20 72 78 3b  4 sz;..  int rx;
1b231 0d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ..  sqlite3Begin
1b232 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d  BenignMalloc();.
1b233 0a 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 4f  .  rx = sqlite3O
1b234 73 46 69 6c 65 53 69 7a 65 28 70 57 61 6c 2d 3e  sFileSize(pWal->
1b235 70 57 61 6c 46 64 2c 20 26 73 7a 29 3b 0d 0a 20  pWalFd, &sz);.. 
1b236 20 69 66 28 20 72 78 3d 3d 53 51 4c 49 54 45 5f   if( rx==SQLITE_
1b237 4f 4b 20 26 26 20 28 73 7a 20 3e 20 6e 4d 61 78  OK && (sz > nMax
1b238 20 29 20 29 7b 0d 0a 20 20 20 20 72 78 20 3d 20   ) ){..    rx = 
1b239 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
1b23a 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20  e(pWal->pWalFd, 
1b23b 6e 4d 61 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73  nMax);..  }..  s
1b23c 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1b23d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 69 66 28 20  alloc();..  if( 
1b23e 72 78 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  rx ){..    sqlit
1b23f 65 33 5f 6c 6f 67 28 72 78 2c 20 22 63 61 6e 6e  e3_log(rx, "cann
1b240 6f 74 20 6c 69 6d 69 74 20 57 41 4c 20 73 69 7a  ot limit WAL siz
1b241 65 3a 20 25 73 22 2c 20 70 57 61 6c 2d 3e 7a 57  e: %s", pWal->zW
1b242 61 6c 4e 61 6d 65 29 3b 0d 0a 20 20 7d 0d 0a 7d  alName);..  }..}
1b243 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65  ..../*..** Close
1b244 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f   a connection to
1b245 20 61 20 6c 6f 67 20 66 69 6c 65 2e 0d 0a 2a 2f   a log file...*/
1b246 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1b247 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 43   int sqlite3WalC
1b248 6c 6f 73 65 28 0d 0a 20 20 57 61 6c 20 2a 70 57  lose(..  Wal *pW
1b249 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  al,             
1b24a 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20           /* Wal 
1b24b 74 6f 20 63 6c 6f 73 65 20 2a 2f 0d 0a 20 20 69  to close */..  i
1b24c 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20 20  nt sync_flags,  
1b24d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b24e 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20  * Flags to pass 
1b24f 74 6f 20 4f 73 53 79 6e 63 28 29 20 28 6f 72 20  to OsSync() (or 
1b250 30 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 75  0) */..  int nBu
1b251 66 2c 0d 0a 20 20 75 38 20 2a 7a 42 75 66 20 20  f,..  u8 *zBuf  
1b252 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b253 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
1b254 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 75 66  of at least nBuf
1b255 20 62 79 74 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20   bytes */..){.. 
1b256 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1b257 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 57 61 6c  _OK;..  if( pWal
1b258 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 73 44   ){..    int isD
1b259 65 6c 65 74 65 20 3d 20 30 3b 20 20 20 20 20 20  elete = 0;      
1b25a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
1b25b 6f 20 75 6e 6c 69 6e 6b 20 77 61 6c 20 61 6e 64  o unlink wal and
1b25c 20 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 73   wal-index files
1b25d 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66   */....    /* If
1b25e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
1b25f 63 6b 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e  ck can be obtain
1b260 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
1b261 73 65 20 66 69 6c 65 20 28 75 73 69 6e 67 20 74  se file (using t
1b262 68 65 0d 0a 20 20 20 20 2a 2a 20 6f 72 64 69 6e  he..    ** ordin
1b263 61 72 79 2c 20 72 6f 6c 6c 62 61 63 6b 2d 6d 6f  ary, rollback-mo
1b264 64 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  de locking metho
1b265 64 73 2c 20 74 68 69 73 20 67 75 61 72 61 6e 74  ds, this guarant
1b266 65 65 73 20 74 68 61 74 20 74 68 65 0d 0a 20 20  ees that the..  
1b267 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20    ** connection 
1b268 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1b269 74 68 69 73 20 6c 6f 67 20 66 69 6c 65 20 69 73  this log file is
1b26a 20 74 68 65 20 6f 6e 6c 79 20 63 6f 6e 6e 65 63   the only connec
1b26b 74 69 6f 6e 20 74 6f 0d 0a 20 20 20 20 2a 2a 20  tion to..    ** 
1b26c 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 6e  the database. In
1b26d 20 74 68 69 73 20 63 61 73 65 20 63 68 65 63 6b   this case check
1b26e 70 6f 69 6e 74 20 74 68 65 20 64 61 74 61 62 61  point the databa
1b26f 73 65 20 61 6e 64 20 75 6e 6c 69 6e 6b 20 62 6f  se and unlink bo
1b270 74 68 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 77  th..    ** the w
1b271 61 6c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78  al and wal-index
1b272 20 66 69 6c 65 73 2e 0d 0a 20 20 20 20 2a 2a 0d   files...    **.
1b273 0a 20 20 20 20 2a 2a 20 54 68 65 20 45 58 43 4c  .    ** The EXCL
1b274 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 6e 6f  USIVE lock is no
1b275 74 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72  t released befor
1b276 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 20 20  e returning...  
1b277 20 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73    */..    rc = s
1b278 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 57 61  qlite3OsLock(pWa
1b279 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49 54 45  l->pDbFd, SQLITE
1b27a 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 29  _LOCK_EXCLUSIVE)
1b27b 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
1b27c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1b27d 20 20 20 69 66 28 20 70 57 61 6c 2d 3e 65 78 63     if( pWal->exc
1b27e 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f  lusiveMode==WAL_
1b27f 4e 4f 52 4d 41 4c 5f 4d 4f 44 45 20 29 7b 0d 0a  NORMAL_MODE ){..
1b280 20 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 65 78          pWal->ex
1b281 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 57 41  clusiveMode = WA
1b282 4c 5f 45 58 43 4c 55 53 49 56 45 5f 4d 4f 44 45  L_EXCLUSIVE_MODE
1b283 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1b284 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 57 61    rc = sqlite3Wa
1b285 6c 43 68 65 63 6b 70 6f 69 6e 74 28 0d 0a 20 20  lCheckpoint(..  
1b286 20 20 20 20 20 20 20 20 70 57 61 6c 2c 20 53 51          pWal, SQ
1b287 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f  LITE_CHECKPOINT_
1b288 50 41 53 53 49 56 45 2c 20 30 2c 20 30 2c 20 73  PASSIVE, 0, 0, s
1b289 79 6e 63 5f 66 6c 61 67 73 2c 20 6e 42 75 66 2c  ync_flags, nBuf,
1b28a 20 7a 42 75 66 2c 20 30 2c 20 30 0d 0a 20 20 20   zBuf, 0, 0..   
1b28b 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28     );..      if(
1b28c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1b28d 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 62  {..        int b
1b28e 50 65 72 73 69 73 74 20 3d 20 2d 31 3b 0d 0a 20  Persist = -1;.. 
1b28f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
1b290 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28  FileControlHint(
1b291 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ..            pW
1b292 61 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49 54  al->pDbFd, SQLIT
1b293 45 5f 46 43 4e 54 4c 5f 50 45 52 53 49 53 54 5f  E_FCNTL_PERSIST_
1b294 57 41 4c 2c 20 26 62 50 65 72 73 69 73 74 0d 0a  WAL, &bPersist..
1b295 20 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20          );..    
1b296 20 20 20 20 69 66 28 20 62 50 65 72 73 69 73 74      if( bPersist
1b297 21 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  !=1 ){..        
1b298 20 20 2f 2a 20 54 72 79 20 74 6f 20 64 65 6c 65    /* Try to dele
1b299 74 65 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20  te the WAL file 
1b29a 69 66 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e  if the checkpoin
1b29b 74 20 63 6f 6d 70 6c 65 74 65 64 20 61 6e 64 0d  t completed and.
1b29c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 73  .          ** fs
1b29d 79 6e 65 64 20 28 72 63 3d 3d 53 51 4c 49 54 45  yned (rc==SQLITE
1b29e 5f 4f 4b 29 20 61 6e 64 20 69 66 20 77 65 20 61  _OK) and if we a
1b29f 72 65 20 6e 6f 74 20 69 6e 20 70 65 72 73 69 73  re not in persis
1b2a0 74 65 6e 74 2d 77 61 6c 0d 0a 20 20 20 20 20 20  tent-wal..      
1b2a1 20 20 20 20 2a 2a 20 6d 6f 64 65 20 28 21 62 50      ** mode (!bP
1b2a2 65 72 73 69 73 74 29 20 2a 2f 0d 0a 20 20 20 20  ersist) */..    
1b2a3 20 20 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d        isDelete =
1b2a4 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c   1;..        }el
1b2a5 73 65 20 69 66 28 20 70 57 61 6c 2d 3e 6d 78 57  se if( pWal->mxW
1b2a6 61 6c 53 69 7a 65 3e 3d 30 20 29 7b 0d 0a 20 20  alSize>=0 ){..  
1b2a7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 79 20 74          /* Try t
1b2a8 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 57  o truncate the W
1b2a9 41 4c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20  AL file to zero 
1b2aa 62 79 74 65 73 20 69 66 20 74 68 65 20 63 68 65  bytes if the che
1b2ab 63 6b 70 6f 69 6e 74 0d 0a 20 20 20 20 20 20 20  ckpoint..       
1b2ac 20 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20     ** completed 
1b2ad 61 6e 64 20 66 73 79 6e 63 65 64 20 28 72 63 3d  and fsynced (rc=
1b2ae 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 61 6e 64 20  =SQLITE_OK) and 
1b2af 77 65 20 61 72 65 20 69 6e 20 70 65 72 73 69 73  we are in persis
1b2b0 74 65 6e 74 0d 0a 20 20 20 20 20 20 20 20 20 20  tent..          
1b2b1 2a 2a 20 57 41 4c 20 6d 6f 64 65 20 28 62 50 65  ** WAL mode (bPe
1b2b2 72 73 69 73 74 29 20 61 6e 64 20 69 66 20 74 68  rsist) and if th
1b2b3 65 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c  e PRAGMA journal
1b2b4 5f 73 69 7a 65 5f 6c 69 6d 69 74 20 69 73 20 61  _size_limit is a
1b2b5 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e  ..          ** n
1b2b6 6f 6e 2d 6e 65 67 61 74 69 76 65 20 76 61 6c 75  on-negative valu
1b2b7 65 20 28 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69  e (pWal->mxWalSi
1b2b8 7a 65 3e 3d 30 29 2e 20 20 4e 6f 74 65 20 74 68  ze>=0).  Note th
1b2b9 61 74 20 77 65 20 74 72 75 6e 63 61 74 65 0d 0a  at we truncate..
1b2ba 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20            ** to 
1b2bb 7a 65 72 6f 20 62 79 74 65 73 20 61 73 20 74 72  zero bytes as tr
1b2bc 75 6e 63 61 74 69 6e 67 20 74 6f 20 74 68 65 20  uncating to the 
1b2bd 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
1b2be 69 74 20 6d 69 67 68 74 0d 0a 20 20 20 20 20 20  it might..      
1b2bf 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 61 20 63      ** leave a c
1b2c0 6f 72 72 75 70 74 20 57 41 4c 20 66 69 6c 65 20  orrupt WAL file 
1b2c1 6f 6e 20 64 69 73 6b 2e 20 2a 2f 0d 0a 20 20 20  on disk. */..   
1b2c2 20 20 20 20 20 20 20 77 61 6c 4c 69 6d 69 74 53         walLimitS
1b2c3 69 7a 65 28 70 57 61 6c 2c 20 30 29 3b 0d 0a 20  ize(pWal, 0);.. 
1b2c4 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
1b2c5 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  }..    }....    
1b2c6 77 61 6c 49 6e 64 65 78 43 6c 6f 73 65 28 70 57  walIndexClose(pW
1b2c7 61 6c 2c 20 69 73 44 65 6c 65 74 65 29 3b 0d 0a  al, isDelete);..
1b2c8 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
1b2c9 73 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 29  se(pWal->pWalFd)
1b2ca 3b 0d 0a 20 20 20 20 69 66 28 20 69 73 44 65 6c  ;..    if( isDel
1b2cb 65 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  ete ){..      sq
1b2cc 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
1b2cd 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 20 20 20  Malloc();..     
1b2ce 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1b2cf 28 70 57 61 6c 2d 3e 70 56 66 73 2c 20 70 57 61  (pWal->pVfs, pWa
1b2d0 6c 2d 3e 7a 57 61 6c 4e 61 6d 65 2c 20 30 29 3b  l->zWalName, 0);
1b2d1 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
1b2d2 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
1b2d3 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 57 41  ;..    }..    WA
1b2d4 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20  LTRACE(("WAL%p: 
1b2d5 63 6c 6f 73 65 64 5c 6e 22 2c 20 70 57 61 6c 29  closed\n", pWal)
1b2d6 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
1b2d7 66 72 65 65 28 28 76 6f 69 64 20 2a 29 70 57 61  free((void *)pWa
1b2d8 6c 2d 3e 61 70 57 69 44 61 74 61 29 3b 0d 0a 20  l->apWiData);.. 
1b2d9 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1b2da 70 57 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  pWal);..  }..  r
1b2db 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1b2dc 2f 2a 0d 0a 2a 2a 20 54 72 79 20 74 6f 20 72 65  /*..** Try to re
1b2dd 61 64 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78  ad the wal-index
1b2de 20 68 65 61 64 65 72 2e 20 20 52 65 74 75 72 6e   header.  Return
1b2df 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
1b2e0 64 20 31 20 69 66 0d 0a 2a 2a 20 74 68 65 72 65  d 1 if..** there
1b2e1 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 2e 0d 0a   is a problem...
1b2e2 2a 2a 0d 0a 2a 2a 20 54 68 65 20 77 61 6c 2d 69  **..** The wal-i
1b2e3 6e 64 65 78 20 69 73 20 69 6e 20 73 68 61 72 65  ndex is in share
1b2e4 64 20 6d 65 6d 6f 72 79 2e 20 20 41 6e 6f 74 68  d memory.  Anoth
1b2e5 65 72 20 74 68 72 65 61 64 20 6f 72 20 70 72 6f  er thread or pro
1b2e6 63 65 73 73 20 6d 69 67 68 74 0d 0a 2a 2a 20 62  cess might..** b
1b2e7 65 20 77 72 69 74 69 6e 67 20 74 68 65 20 68 65  e writing the he
1b2e8 61 64 65 72 20 61 74 20 74 68 65 20 73 61 6d 65  ader at the same
1b2e9 20 74 69 6d 65 20 74 68 69 73 20 70 72 6f 63 65   time this proce
1b2ea 64 75 72 65 20 69 73 20 74 72 79 69 6e 67 20 74  dure is trying t
1b2eb 6f 0d 0a 2a 2a 20 72 65 61 64 20 69 74 2c 20 77  o..** read it, w
1b2ec 68 69 63 68 20 6d 69 67 68 74 20 72 65 73 75 6c  hich might resul
1b2ed 74 20 69 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  t in inconsisten
1b2ee 63 79 2e 20 20 41 20 64 69 72 74 79 20 72 65 61  cy.  A dirty rea
1b2ef 64 20 69 73 20 64 65 74 65 63 74 65 64 0d 0a 2a  d is detected..*
1b2f0 2a 20 62 79 20 76 65 72 69 66 79 69 6e 67 20 74  * by verifying t
1b2f1 68 61 74 20 62 6f 74 68 20 63 6f 70 69 65 73 20  hat both copies 
1b2f2 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 61 72  of the header ar
1b2f3 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 61  e the same and a
1b2f4 6c 73 6f 20 62 79 0d 0a 2a 2a 20 61 20 63 68 65  lso by..** a che
1b2f5 63 6b 73 75 6d 20 6f 6e 20 74 68 65 20 68 65 61  cksum on the hea
1b2f6 64 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  der...**..** If 
1b2f7 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  and only if the 
1b2f8 72 65 61 64 20 69 73 20 63 6f 6e 73 69 73 74 65  read is consiste
1b2f9 6e 74 20 61 6e 64 20 74 68 65 20 68 65 61 64 65  nt and the heade
1b2fa 72 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66  r is different f
1b2fb 72 6f 6d 0d 0a 2a 2a 20 70 57 61 6c 2d 3e 68 64  rom..** pWal->hd
1b2fc 72 2c 20 74 68 65 6e 20 70 57 61 6c 2d 3e 68 64  r, then pWal->hd
1b2fd 72 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20  r is updated to 
1b2fe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
1b2ff 68 65 20 6e 65 77 20 68 65 61 64 65 72 0d 0a 2a  he new header..*
1b300 2a 20 61 6e 64 20 2a 70 43 68 61 6e 67 65 64 20  * and *pChanged 
1b301 69 73 20 73 65 74 20 74 6f 20 31 2e 0d 0a 2a 2a  is set to 1...**
1b302 0d 0a 2a 2a 20 49 66 20 74 68 65 20 63 68 65 63  ..** If the chec
1b303 6b 73 75 6d 20 63 61 6e 6e 6f 74 20 62 65 20 76  ksum cannot be v
1b304 65 72 69 66 69 65 64 20 72 65 74 75 72 6e 20 6e  erified return n
1b305 6f 6e 2d 7a 65 72 6f 2e 20 49 66 20 74 68 65 20  on-zero. If the 
1b306 68 65 61 64 65 72 0d 0a 2a 2a 20 69 73 20 72 65  header..** is re
1b307 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ad successfully 
1b308 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d  and the checksum
1b309 20 76 65 72 69 66 69 65 64 2c 20 72 65 74 75 72   verified, retur
1b30a 6e 20 7a 65 72 6f 2e 0d 0a 2a 2f 0d 0a 73 74 61  n zero...*/..sta
1b30b 74 69 63 20 69 6e 74 20 77 61 6c 49 6e 64 65 78  tic int walIndex
1b30c 54 72 79 48 64 72 28 57 61 6c 20 2a 70 57 61 6c  TryHdr(Wal *pWal
1b30d 2c 20 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 29  , int *pChanged)
1b30e 7b 0d 0a 20 20 75 33 32 20 61 43 6b 73 75 6d 5b  {..  u32 aCksum[
1b30f 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  2];             
1b310 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d       /* Checksum
1b311 20 6f 6e 20 74 68 65 20 68 65 61 64 65 72 20 63   on the header c
1b312 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 20 20 57 61 6c  ontent */..  Wal
1b313 49 6e 64 65 78 48 64 72 20 68 31 2c 20 68 32 3b  IndexHdr h1, h2;
1b314 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b315 54 77 6f 20 63 6f 70 69 65 73 20 6f 66 20 74 68  Two copies of th
1b316 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74  e header content
1b317 20 2a 2f 0d 0a 20 20 57 61 6c 49 6e 64 65 78 48   */..  WalIndexH
1b318 64 72 20 76 6f 6c 61 74 69 6c 65 20 2a 61 48 64  dr volatile *aHd
1b319 72 3b 20 20 20 20 20 2f 2a 20 48 65 61 64 65 72  r;     /* Header
1b31a 20 69 6e 20 73 68 61 72 65 64 20 6d 65 6d 6f 72   in shared memor
1b31b 79 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65  y */....  /* The
1b31c 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   first page of t
1b31d 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 6d 75 73  he wal-index mus
1b31e 74 20 62 65 20 6d 61 70 70 65 64 20 61 74 20 74  t be mapped at t
1b31f 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0d 0a 20  his point. */.. 
1b320 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 6e   assert( pWal->n
1b321 57 69 44 61 74 61 3e 30 20 26 26 20 70 57 61 6c  WiData>0 && pWal
1b322 2d 3e 61 70 57 69 44 61 74 61 5b 30 5d 20 29 3b  ->apWiData[0] );
1b323 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  ....  /* Read th
1b324 65 20 68 65 61 64 65 72 2e 20 54 68 69 73 20 6d  e header. This m
1b325 69 67 68 74 20 68 61 70 70 65 6e 20 63 6f 6e 63  ight happen conc
1b326 75 72 72 65 6e 74 6c 79 20 77 69 74 68 20 61 20  urrently with a 
1b327 77 72 69 74 65 20 74 6f 20 74 68 65 0d 0a 20 20  write to the..  
1b328 2a 2a 20 73 61 6d 65 20 61 72 65 61 20 6f 66 20  ** same area of 
1b329 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 6f 6e  shared memory on
1b32a 20 61 20 64 69 66 66 65 72 65 6e 74 20 43 50 55   a different CPU
1b32b 20 69 6e 20 61 20 53 4d 50 2c 0d 0a 20 20 2a 2a   in a SMP,..  **
1b32c 20 6d 65 61 6e 69 6e 67 20 69 74 20 69 73 20 70   meaning it is p
1b32d 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 20  ossible that an 
1b32e 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 6e 61  inconsistent sna
1b32f 70 73 68 6f 74 20 69 73 20 72 65 61 64 0d 0a 20  pshot is read.. 
1b330 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c   ** from the fil
1b331 65 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65  e. If this happe
1b332 6e 73 2c 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  ns, return non-z
1b333 65 72 6f 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ero...  **..  **
1b334 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 63   There are two c
1b335 6f 70 69 65 73 20 6f 66 20 74 68 65 20 68 65 61  opies of the hea
1b336 64 65 72 20 61 74 20 74 68 65 20 62 65 67 69 6e  der at the begin
1b337 6e 69 6e 67 20 6f 66 20 74 68 65 20 77 61 6c 2d  ning of the wal-
1b338 69 6e 64 65 78 2e 0d 0a 20 20 2a 2a 20 57 68 65  index...  ** Whe
1b339 6e 20 72 65 61 64 69 6e 67 2c 20 72 65 61 64 20  n reading, read 
1b33a 5b 30 5d 20 66 69 72 73 74 20 74 68 65 6e 20 5b  [0] first then [
1b33b 31 5d 2e 20 20 57 72 69 74 65 73 20 61 72 65 20  1].  Writes are 
1b33c 69 6e 20 74 68 65 20 72 65 76 65 72 73 65 20 6f  in the reverse o
1b33d 72 64 65 72 2e 0d 0a 20 20 2a 2a 20 4d 65 6d 6f  rder...  ** Memo
1b33e 72 79 20 62 61 72 72 69 65 72 73 20 61 72 65 20  ry barriers are 
1b33f 75 73 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20  used to prevent 
1b340 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6f 72 20  the compiler or 
1b341 74 68 65 20 68 61 72 64 77 61 72 65 20 66 72 6f  the hardware fro
1b342 6d 0d 0a 20 20 2a 2a 20 72 65 6f 72 64 65 72 69  m..  ** reorderi
1b343 6e 67 20 74 68 65 20 72 65 61 64 73 20 61 6e 64  ng the reads and
1b344 20 77 72 69 74 65 73 2e 0d 0a 20 20 2a 2f 0d 0a   writes...  */..
1b345 20 20 61 48 64 72 20 3d 20 77 61 6c 49 6e 64 65    aHdr = walInde
1b346 78 48 64 72 28 70 57 61 6c 29 3b 0d 0a 20 20 6d  xHdr(pWal);..  m
1b347 65 6d 63 70 79 28 26 68 31 2c 20 28 76 6f 69 64  emcpy(&h1, (void
1b348 20 2a 29 26 61 48 64 72 5b 30 5d 2c 20 73 69 7a   *)&aHdr[0], siz
1b349 65 6f 66 28 68 31 29 29 3b 0d 0a 20 20 77 61 6c  eof(h1));..  wal
1b34a 53 68 6d 42 61 72 72 69 65 72 28 70 57 61 6c 29  ShmBarrier(pWal)
1b34b 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 26 68 32 2c  ;..  memcpy(&h2,
1b34c 20 28 76 6f 69 64 20 2a 29 26 61 48 64 72 5b 31   (void *)&aHdr[1
1b34d 5d 2c 20 73 69 7a 65 6f 66 28 68 32 29 29 3b 0d  ], sizeof(h2));.
1b34e 0a 0d 0a 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  ...  if( memcmp(
1b34f 26 68 31 2c 20 26 68 32 2c 20 73 69 7a 65 6f 66  &h1, &h2, sizeof
1b350 28 68 31 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20  (h1))!=0 ){..   
1b351 20 72 65 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20   return 1;   /* 
1b352 44 69 72 74 79 20 72 65 61 64 20 2a 2f 0d 0a 20  Dirty read */.. 
1b353 20 7d 20 20 0d 0a 20 20 69 66 28 20 68 31 2e 69   }  ..  if( h1.i
1b354 73 49 6e 69 74 3d 3d 30 20 29 7b 0d 0a 20 20 20  sInit==0 ){..   
1b355 20 72 65 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20   return 1;   /* 
1b356 4d 61 6c 66 6f 72 6d 65 64 20 68 65 61 64 65 72  Malformed header
1b357 20 2d 20 70 72 6f 62 61 62 6c 79 20 61 6c 6c 20   - probably all 
1b358 7a 65 72 6f 73 20 2a 2f 0d 0a 20 20 7d 0d 0a 20  zeros */..  }.. 
1b359 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65   walChecksumByte
1b35a 73 28 31 2c 20 28 75 38 2a 29 26 68 31 2c 20 73  s(1, (u8*)&h1, s
1b35b 69 7a 65 6f 66 28 68 31 29 2d 73 69 7a 65 6f 66  izeof(h1)-sizeof
1b35c 28 68 31 2e 61 43 6b 73 75 6d 29 2c 20 30 2c 20  (h1.aCksum), 0, 
1b35d 61 43 6b 73 75 6d 29 3b 0d 0a 20 20 69 66 28 20  aCksum);..  if( 
1b35e 61 43 6b 73 75 6d 5b 30 5d 21 3d 68 31 2e 61 43  aCksum[0]!=h1.aC
1b35f 6b 73 75 6d 5b 30 5d 20 7c 7c 20 61 43 6b 73 75  ksum[0] || aCksu
1b360 6d 5b 31 5d 21 3d 68 31 2e 61 43 6b 73 75 6d 5b  m[1]!=h1.aCksum[
1b361 31 5d 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  1] ){..    retur
1b362 6e 20 31 3b 20 20 20 2f 2a 20 43 68 65 63 6b 73  n 1;   /* Checks
1b363 75 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  um does not matc
1b364 68 20 2a 2f 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  h */..  }....  i
1b365 66 28 20 6d 65 6d 63 6d 70 28 26 70 57 61 6c 2d  f( memcmp(&pWal-
1b366 3e 68 64 72 2c 20 26 68 31 2c 20 73 69 7a 65 6f  >hdr, &h1, sizeo
1b367 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 29 20  f(WalIndexHdr)) 
1b368 29 7b 0d 0a 20 20 20 20 2a 70 43 68 61 6e 67 65  ){..    *pChange
1b369 64 20 3d 20 31 3b 0d 0a 20 20 20 20 6d 65 6d 63  d = 1;..    memc
1b36a 70 79 28 26 70 57 61 6c 2d 3e 68 64 72 2c 20 26  py(&pWal->hdr, &
1b36b 68 31 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e  h1, sizeof(WalIn
1b36c 64 65 78 48 64 72 29 29 3b 0d 0a 20 20 20 20 70  dexHdr));..    p
1b36d 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 28 70  Wal->szPage = (p
1b36e 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26  Wal->hdr.szPage&
1b36f 30 78 66 65 30 30 29 20 2b 20 28 28 70 57 61 6c  0xfe00) + ((pWal
1b370 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26 30 78 30  ->hdr.szPage&0x0
1b371 30 30 31 29 3c 3c 31 36 29 3b 0d 0a 20 20 20 20  001)<<16);..    
1b372 74 65 73 74 63 61 73 65 28 20 70 57 61 6c 2d 3e  testcase( pWal->
1b373 73 7a 50 61 67 65 3c 3d 33 32 37 36 38 20 29 3b  szPage<=32768 );
1b374 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
1b375 70 57 61 6c 2d 3e 73 7a 50 61 67 65 3e 3d 36 35  pWal->szPage>=65
1b376 35 33 36 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  536 );..  }.... 
1b377 20 2f 2a 20 54 68 65 20 68 65 61 64 65 72 20 77   /* The header w
1b378 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  as successfully 
1b379 72 65 61 64 2e 20 52 65 74 75 72 6e 20 7a 65 72  read. Return zer
1b37a 6f 2e 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20  o. */..  return 
1b37b 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
1b37c 52 65 61 64 20 74 68 65 20 77 61 6c 2d 69 6e 64  Read the wal-ind
1b37d 65 78 20 68 65 61 64 65 72 20 66 72 6f 6d 20 74  ex header from t
1b37e 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 61 6e 64  he wal-index and
1b37f 20 69 6e 74 6f 20 70 57 61 6c 2d 3e 68 64 72 2e   into pWal->hdr.
1b380 0d 0a 2a 2a 20 49 66 20 74 68 65 20 77 61 6c 2d  ..** If the wal-
1b381 68 65 61 64 65 72 20 61 70 70 65 61 72 73 20 74  header appears t
1b382 6f 20 62 65 20 63 6f 72 72 75 70 74 2c 20 74 72  o be corrupt, tr
1b383 79 20 74 6f 20 72 65 63 6f 6e 73 74 72 75 63 74  y to reconstruct
1b384 20 74 68 65 0d 0a 2a 2a 20 77 61 6c 2d 69 6e 64   the..** wal-ind
1b385 65 78 20 66 72 6f 6d 20 74 68 65 20 57 41 4c 20  ex from the WAL 
1b386 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
1b387 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 74 20 2a 70  ...**..** Set *p
1b388 43 68 61 6e 67 65 64 20 74 6f 20 31 20 69 66 20  Changed to 1 if 
1b389 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  the wal-index he
1b38a 61 64 65 72 20 76 61 6c 75 65 20 69 6e 20 70 57  ader value in pW
1b38b 61 6c 2d 3e 68 64 72 20 69 73 0d 0a 2a 2a 20 63  al->hdr is..** c
1b38c 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 6f  hanged by this o
1b38d 70 65 72 74 69 6f 6e 2e 20 20 49 66 20 70 57 61  pertion.  If pWa
1b38e 6c 2d 3e 68 64 72 20 69 73 20 75 6e 63 68 61 6e  l->hdr is unchan
1b38f 67 65 64 2c 20 73 65 74 20 2a 70 43 68 61 6e 67  ged, set *pChang
1b390 65 64 0d 0a 2a 2a 20 74 6f 20 30 2e 0d 0a 2a 2a  ed..** to 0...**
1b391 0d 0a 2a 2a 20 49 66 20 74 68 65 20 77 61 6c 2d  ..** If the wal-
1b392 69 6e 64 65 78 20 68 65 61 64 65 72 20 69 73 20  index header is 
1b393 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61  successfully rea
1b394 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d, return SQLITE
1b395 5f 4f 4b 2e 20 0d 0a 2a 2a 20 4f 74 68 65 72 77  _OK. ..** Otherw
1b396 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72  ise an SQLite er
1b397 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73  ror code...*/..s
1b398 74 61 74 69 63 20 69 6e 74 20 77 61 6c 49 6e 64  tatic int walInd
1b399 65 78 52 65 61 64 48 64 72 28 57 61 6c 20 2a 70  exReadHdr(Wal *p
1b39a 57 61 6c 2c 20 69 6e 74 20 2a 70 43 68 61 6e 67  Wal, int *pChang
1b39b 65 64 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20  ed){..  int rc; 
1b39c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b39d 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1b39e 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74  n code */..  int
1b39f 20 62 61 64 48 64 72 3b 20 20 20 20 20 20 20 20   badHdr;        
1b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b3a1 54 72 75 65 20 69 66 20 61 20 68 65 61 64 65 72  True if a header
1b3a2 20 72 65 61 64 20 66 61 69 6c 65 64 20 2a 2f 0d   read failed */.
1b3a3 0a 20 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20  .  volatile u32 
1b3a4 2a 70 61 67 65 30 3b 20 20 20 20 20 20 20 20 20  *page0;         
1b3a5 20 20 20 2f 2a 20 43 68 75 6e 6b 20 6f 66 20 77     /* Chunk of w
1b3a6 61 6c 2d 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e  al-index contain
1b3a7 69 6e 67 20 68 65 61 64 65 72 20 2a 2f 0d 0a 0d  ing header */...
1b3a8 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 61  .  /* Ensure tha
1b3a9 74 20 70 61 67 65 20 30 20 6f 66 20 74 68 65 20  t page 0 of the 
1b3aa 77 61 6c 2d 69 6e 64 65 78 20 28 74 68 65 20 70  wal-index (the p
1b3ab 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
1b3ac 73 20 74 68 65 20 0d 0a 20 20 2a 2a 20 77 61 6c  s the ..  ** wal
1b3ad 2d 69 6e 64 65 78 20 68 65 61 64 65 72 29 20 69  -index header) i
1b3ae 73 20 6d 61 70 70 65 64 2e 20 52 65 74 75 72 6e  s mapped. Return
1b3af 20 65 61 72 6c 79 20 69 66 20 61 6e 20 65 72 72   early if an err
1b3b0 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2e 0d  or occurs here..
1b3b1 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
1b3b2 20 70 43 68 61 6e 67 65 64 20 29 3b 0d 0a 20 20   pChanged );..  
1b3b3 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 61 67  rc = walIndexPag
1b3b4 65 28 70 57 61 6c 2c 20 30 2c 20 26 70 61 67 65  e(pWal, 0, &page
1b3b5 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53  0);..  if( rc!=S
1b3b6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1b3b7 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d   return rc;..  }
1b3b8 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 61 67  ;..  assert( pag
1b3b9 65 30 20 7c 7c 20 70 57 61 6c 2d 3e 77 72 69 74  e0 || pWal->writ
1b3ba 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0d 0a 0d 0a 20  eLock==0 );.... 
1b3bb 20 2f 2a 20 49 66 20 74 68 65 20 66 69 72 73 74   /* If the first
1b3bc 20 70 61 67 65 20 6f 66 20 74 68 65 20 77 61 6c   page of the wal
1b3bd 2d 69 6e 64 65 78 20 68 61 73 20 62 65 65 6e 20  -index has been 
1b3be 6d 61 70 70 65 64 2c 20 74 72 79 20 74 6f 20 72  mapped, try to r
1b3bf 65 61 64 20 74 68 65 0d 0a 20 20 2a 2a 20 77 61  ead the..  ** wa
1b3c0 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 69  l-index header i
1b3c1 6d 6d 65 64 69 61 74 65 6c 79 2c 20 77 69 74 68  mmediately, with
1b3c2 6f 75 74 20 68 6f 6c 64 69 6e 67 20 61 6e 79 20  out holding any 
1b3c3 6c 6f 63 6b 2e 20 54 68 69 73 20 75 73 75 61 6c  lock. This usual
1b3c4 6c 79 0d 0a 20 20 2a 2a 20 77 6f 72 6b 73 2c 20  ly..  ** works, 
1b3c5 62 75 74 20 6d 61 79 20 66 61 69 6c 20 69 66 20  but may fail if 
1b3c6 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  the wal-index he
1b3c7 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 20  ader is corrupt 
1b3c8 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 0d 0a 20  or currently .. 
1b3c9 20 2a 2a 20 62 65 69 6e 67 20 6d 6f 64 69 66 69   ** being modifi
1b3ca 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68  ed by another th
1b3cb 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 73 2e  read or process.
1b3cc 0d 0a 20 20 2a 2f 0d 0a 20 20 62 61 64 48 64 72  ..  */..  badHdr
1b3cd 20 3d 20 28 70 61 67 65 30 20 3f 20 77 61 6c 49   = (page0 ? walI
1b3ce 6e 64 65 78 54 72 79 48 64 72 28 70 57 61 6c 2c  ndexTryHdr(pWal,
1b3cf 20 70 43 68 61 6e 67 65 64 29 20 3a 20 31 29 3b   pChanged) : 1);
1b3d0 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
1b3d1 66 69 72 73 74 20 61 74 74 65 6d 70 74 20 66 61  first attempt fa
1b3d2 69 6c 65 64 2c 20 69 74 20 6d 69 67 68 74 20 68  iled, it might h
1b3d3 61 76 65 20 62 65 65 6e 20 64 75 65 20 74 6f 20  ave been due to 
1b3d4 61 20 72 61 63 65 0d 0a 20 20 2a 2a 20 77 69 74  a race..  ** wit
1b3d5 68 20 61 20 77 72 69 74 65 72 2e 20 20 53 6f 20  h a writer.  So 
1b3d6 67 65 74 20 61 20 57 52 49 54 45 20 6c 6f 63 6b  get a WRITE lock
1b3d7 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0d   and try again..
1b3d8 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
1b3d9 20 62 61 64 48 64 72 3d 3d 30 20 7c 7c 20 70 57   badHdr==0 || pW
1b3da 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d 3d 30  al->writeLock==0
1b3db 20 29 3b 0d 0a 20 20 69 66 28 20 62 61 64 48 64   );..  if( badHd
1b3dc 72 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 57  r ){..    if( pW
1b3dd 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57  al->readOnly & W
1b3de 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 20 29 7b  AL_SHM_RDONLY ){
1b3df 0d 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  ..      if( SQLI
1b3e0 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c  TE_OK==(rc = wal
1b3e1 4c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c  LockShared(pWal,
1b3e2 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b 29   WAL_WRITE_LOCK)
1b3e3 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 77 61  ) ){..        wa
1b3e4 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57  lUnlockShared(pW
1b3e5 61 6c 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f  al, WAL_WRITE_LO
1b3e6 43 4b 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63  CK);..        rc
1b3e7 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e   = SQLITE_READON
1b3e8 4c 59 5f 52 45 43 4f 56 45 52 59 3b 0d 0a 20 20  LY_RECOVERY;..  
1b3e9 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65      }..    }else
1b3ea 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
1b3eb 28 72 63 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63  (rc = walLockExc
1b3ec 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c  lusive(pWal, WAL
1b3ed 5f 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 29  _WRITE_LOCK, 1))
1b3ee 20 29 7b 0d 0a 20 20 20 20 20 20 70 57 61 6c 2d   ){..      pWal-
1b3ef 3e 77 72 69 74 65 4c 6f 63 6b 20 3d 20 31 3b 0d  >writeLock = 1;.
1b3f0 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
1b3f1 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 49  E_OK==(rc = walI
1b3f2 6e 64 65 78 50 61 67 65 28 70 57 61 6c 2c 20 30  ndexPage(pWal, 0
1b3f3 2c 20 26 70 61 67 65 30 29 29 20 29 7b 0d 0a 20  , &page0)) ){.. 
1b3f4 20 20 20 20 20 20 20 62 61 64 48 64 72 20 3d 20         badHdr = 
1b3f5 77 61 6c 49 6e 64 65 78 54 72 79 48 64 72 28 70  walIndexTryHdr(p
1b3f6 57 61 6c 2c 20 70 43 68 61 6e 67 65 64 29 3b 0d  Wal, pChanged);.
1b3f7 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 61 64  .        if( bad
1b3f8 48 64 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Hdr ){..        
1b3f9 20 20 2f 2a 20 49 66 20 74 68 65 20 77 61 6c 2d    /* If the wal-
1b3fa 69 6e 64 65 78 20 68 65 61 64 65 72 20 69 73 20  index header is 
1b3fb 73 74 69 6c 6c 20 6d 61 6c 66 6f 72 6d 65 64 20  still malformed 
1b3fc 65 76 65 6e 20 77 68 69 6c 65 20 68 6f 6c 64 69  even while holdi
1b3fd 6e 67 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ng..          **
1b3fe 20 61 20 57 52 49 54 45 20 6c 6f 63 6b 2c 20 69   a WRITE lock, i
1b3ff 74 20 63 61 6e 20 6f 6e 6c 79 20 6d 65 61 6e 20  t can only mean 
1b400 74 68 61 74 20 74 68 65 20 68 65 61 64 65 72 20  that the header 
1b401 69 73 20 63 6f 72 72 75 70 74 65 64 20 61 6e 64  is corrupted and
1b402 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e  ..          ** n
1b403 65 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6e  eeds to be recon
1b404 73 74 72 75 63 74 65 64 2e 20 20 53 6f 20 72 75  structed.  So ru
1b405 6e 20 72 65 63 6f 76 65 72 79 20 74 6f 20 64 6f  n recovery to do
1b406 20 65 78 61 63 74 6c 79 20 74 68 61 74 2e 0d 0a   exactly that...
1b407 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20            */..  
1b408 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 61 6c          rc = wal
1b409 49 6e 64 65 78 52 65 63 6f 76 65 72 28 70 57 61  IndexRecover(pWa
1b40a 6c 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  l);..          *
1b40b 70 43 68 61 6e 67 65 64 20 3d 20 31 3b 0d 0a 20  pChanged = 1;.. 
1b40c 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
1b40d 7d 0d 0a 20 20 20 20 20 20 70 57 61 6c 2d 3e 77  }..      pWal->w
1b40e 72 69 74 65 4c 6f 63 6b 20 3d 20 30 3b 0d 0a 20  riteLock = 0;.. 
1b40f 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78       walUnlockEx
1b410 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41  clusive(pWal, WA
1b411 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29  L_WRITE_LOCK, 1)
1b412 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
1b413 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 65 61  .  /* If the hea
1b414 64 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63  der is read succ
1b415 65 73 73 66 75 6c 6c 79 2c 20 63 68 65 63 6b 20  essfully, check 
1b416 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  the version numb
1b417 65 72 20 74 6f 20 6d 61 6b 65 0d 0a 20 20 2a 2a  er to make..  **
1b418 20 73 75 72 65 20 74 68 65 20 77 61 6c 2d 69 6e   sure the wal-in
1b419 64 65 78 20 77 61 73 20 6e 6f 74 20 63 6f 6e 73  dex was not cons
1b41a 74 72 75 63 74 65 64 20 77 69 74 68 20 73 6f 6d  tructed with som
1b41b 65 20 66 75 74 75 72 65 20 66 6f 72 6d 61 74 20  e future format 
1b41c 74 68 61 74 0d 0a 20 20 2a 2a 20 74 68 69 73 20  that..  ** this 
1b41d 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74  version of SQLit
1b41e 65 20 63 61 6e 6e 6f 74 20 75 6e 64 65 72 73 74  e cannot underst
1b41f 61 6e 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  and...  */..  if
1b420 28 20 62 61 64 48 64 72 3d 3d 30 20 26 26 20 70  ( badHdr==0 && p
1b421 57 61 6c 2d 3e 68 64 72 2e 69 56 65 72 73 69 6f  Wal->hdr.iVersio
1b422 6e 21 3d 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f  n!=WALINDEX_MAX_
1b423 56 45 52 53 49 4f 4e 20 29 7b 0d 0a 20 20 20 20  VERSION ){..    
1b424 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54  rc = SQLITE_CANT
1b425 4f 50 45 4e 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d  OPEN_BKPT;..  }.
1b426 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
1b427 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
1b428 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74  s is the value t
1b429 68 61 74 20 77 61 6c 54 72 79 42 65 67 69 6e 52  hat walTryBeginR
1b42a 65 61 64 20 72 65 74 75 72 6e 73 20 77 68 65 6e  ead returns when
1b42b 20 69 74 20 6e 65 65 64 73 20 74 6f 0d 0a 2a 2a   it needs to..**
1b42c 20 62 65 20 72 65 74 72 69 65 64 2e 0d 0a 2a 2f   be retried...*/
1b42d 0d 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 52 45  ..#define WAL_RE
1b42e 54 52 59 20 20 28 2d 31 29 0d 0a 0d 0a 2f 2a 0d  TRY  (-1)..../*.
1b42f 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73  .** Attempt to s
1b430 74 61 72 74 20 61 20 72 65 61 64 20 74 72 61 6e  tart a read tran
1b431 73 61 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d  saction.  This m
1b432 69 67 68 74 20 66 61 69 6c 20 64 75 65 20 74 6f  ight fail due to
1b433 20 61 20 72 61 63 65 20 6f 72 0d 0a 2a 2a 20 6f   a race or..** o
1b434 74 68 65 72 20 74 72 61 6e 73 69 65 6e 74 20 63  ther transient c
1b435 6f 6e 64 69 74 69 6f 6e 2e 20 20 57 68 65 6e 20  ondition.  When 
1b436 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 69 74  that happens, it
1b437 20 72 65 74 75 72 6e 73 20 57 41 4c 5f 52 45 54   returns WAL_RET
1b438 52 59 20 74 6f 0d 0a 2a 2a 20 69 6e 64 69 63 61  RY to..** indica
1b439 74 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  te to the caller
1b43a 20 74 68 61 74 20 69 74 20 69 73 20 73 61 66 65   that it is safe
1b43b 20 74 6f 20 72 65 74 72 79 20 69 6d 6d 65 64 69   to retry immedi
1b43c 61 74 65 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f  ately...**..** O
1b43d 6e 20 73 75 63 63 65 73 73 20 72 65 74 75 72 6e  n success return
1b43e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 4f 6e 20   SQLITE_OK.  On 
1b43f 61 20 70 65 72 6d 61 6e 65 6e 74 20 66 61 69 6c  a permanent fail
1b440 75 72 65 20 28 73 75 63 68 20 61 6e 0d 0a 2a 2a  ure (such an..**
1b441 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 6e   I/O error or an
1b442 20 53 51 4c 49 54 45 5f 42 55 53 59 20 62 65 63   SQLITE_BUSY bec
1b443 61 75 73 65 20 61 6e 6f 74 68 65 72 20 70 72 6f  ause another pro
1b444 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e 67 0d  cess is running.
1b445 0a 2a 2a 20 72 65 63 6f 76 65 72 79 29 20 72 65  .** recovery) re
1b446 74 75 72 6e 20 61 20 70 6f 73 69 74 69 76 65 20  turn a positive 
1b447 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2a 0d  error code...**.
1b448 0a 2a 2a 20 54 68 65 20 75 73 65 57 61 6c 20 70  .** The useWal p
1b449 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65  arameter is true
1b44a 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 75 73   to force the us
1b44b 65 20 6f 66 20 74 68 65 20 57 41 4c 20 61 6e 64  e of the WAL and
1b44c 20 64 69 73 61 62 6c 65 0d 0a 2a 2a 20 74 68 65   disable..** the
1b44d 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
1b44e 57 41 4c 20 69 73 20 62 79 70 61 73 73 65 64 20  WAL is bypassed 
1b44f 62 65 63 61 75 73 65 20 69 74 20 68 61 73 20 62  because it has b
1b450 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0d 0a  een completely..
1b451 2a 2a 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 2e  ** checkpointed.
1b452 20 20 49 66 20 75 73 65 57 61 6c 3d 3d 30 20 74    If useWal==0 t
1b453 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1b454 20 63 61 6c 6c 73 20 77 61 6c 49 6e 64 65 78 52   calls walIndexR
1b455 65 61 64 48 64 72 28 29 20 0d 0a 2a 2a 20 74 6f  eadHdr() ..** to
1b456 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   make a copy of 
1b457 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  the wal-index he
1b458 61 64 65 72 20 69 6e 74 6f 20 70 57 61 6c 2d 3e  ader into pWal->
1b459 68 64 72 2e 20 20 49 66 20 74 68 65 20 0d 0a 2a  hdr.  If the ..*
1b45a 2a 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  * wal-index head
1b45b 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20  er has changed, 
1b45c 2a 70 43 68 61 6e 67 65 64 20 69 73 20 73 65 74  *pChanged is set
1b45d 20 74 6f 20 31 20 28 61 73 20 61 6e 20 69 6e 64   to 1 (as an ind
1b45e 69 63 61 74 69 6f 6e 20 0d 0a 2a 2a 20 74 6f 20  ication ..** to 
1b45f 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20  the caller that 
1b460 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 74 20  the local paget 
1b461 63 61 63 68 65 20 69 73 20 6f 62 73 6f 6c 65 74  cache is obsolet
1b462 65 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62  e and needs to b
1b463 65 20 0d 0a 2a 2a 20 66 6c 75 73 68 65 64 2e 29  e ..** flushed.)
1b464 20 20 57 68 65 6e 20 75 73 65 57 61 6c 3d 3d 31    When useWal==1
1b465 2c 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  , the wal-index 
1b466 68 65 61 64 65 72 20 69 73 20 61 73 73 75 6d 65  header is assume
1b467 64 20 74 6f 20 61 6c 72 65 61 64 79 0d 0a 2a 2a  d to already..**
1b468 20 62 65 20 6c 6f 61 64 65 64 20 61 6e 64 20 74   be loaded and t
1b469 68 65 20 70 43 68 61 6e 67 65 64 20 70 61 72 61  he pChanged para
1b46a 6d 65 74 65 72 20 69 73 20 75 6e 75 73 65 64 2e  meter is unused.
1b46b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 61 6c  ..**..** The cal
1b46c 6c 65 72 20 6d 75 73 74 20 73 65 74 20 74 68 65  ler must set the
1b46d 20 63 6e 74 20 70 61 72 61 6d 65 74 65 72 20 74   cnt parameter t
1b46e 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1b46f 70 72 69 6f 72 20 63 61 6c 6c 73 20 74 6f 0d 0a  prior calls to..
1b470 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
1b471 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72 65  during the curre
1b472 6e 74 20 72 65 61 64 20 61 74 74 65 6d 70 74 20  nt read attempt 
1b473 74 68 61 74 20 72 65 74 75 72 6e 65 64 20 57 41  that returned WA
1b474 4c 5f 52 45 54 52 59 2e 0d 0a 2a 2a 20 54 68 69  L_RETRY...** Thi
1b475 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 73  s routine will s
1b476 74 61 72 74 20 74 61 6b 69 6e 67 20 6d 6f 72 65  tart taking more
1b477 20 61 67 67 72 65 73 73 69 76 65 20 6d 65 61 73   aggressive meas
1b478 75 72 65 73 20 74 6f 20 63 6c 65 61 72 20 74 68  ures to clear th
1b479 65 0d 0a 2a 2a 20 72 61 63 65 20 63 6f 6e 64 69  e..** race condi
1b47a 74 69 6f 6e 73 20 61 66 74 65 72 20 6d 75 6c 74  tions after mult
1b47b 69 70 6c 65 20 57 41 4c 5f 52 45 54 52 59 20 72  iple WAL_RETRY r
1b47c 65 74 75 72 6e 73 2c 20 61 6e 64 20 61 66 74 65  eturns, and afte
1b47d 72 20 61 6e 20 65 78 63 65 73 73 69 76 65 0d 0a  r an excessive..
1b47e 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  ** number of err
1b47f 6f 72 73 20 77 69 6c 6c 20 75 6c 74 69 6d 61 74  ors will ultimat
1b480 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ely return SQLIT
1b481 45 5f 50 52 4f 54 4f 43 4f 4c 2e 20 20 54 68 65  E_PROTOCOL.  The
1b482 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 4f 54  ..** SQLITE_PROT
1b483 4f 43 4f 4c 20 72 65 74 75 72 6e 20 69 6e 64 69  OCOL return indi
1b484 63 61 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20  cates that some 
1b485 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 61  other process ha
1b486 73 20 67 6f 6e 65 20 72 6f 67 75 65 0d 0a 2a 2a  s gone rogue..**
1b487 20 61 6e 64 20 69 73 20 6e 6f 74 20 68 6f 6e 6f   and is not hono
1b488 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ring the locking
1b489 20 70 72 6f 74 6f 63 6f 6c 2e 20 20 54 68 65 72   protocol.  Ther
1b48a 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67  e is a vanishing
1b48b 6c 79 20 73 6d 61 6c 6c 0d 0a 2a 2a 20 63 68 61  ly small..** cha
1b48c 6e 63 65 20 74 68 61 74 20 53 51 4c 49 54 45 5f  nce that SQLITE_
1b48d 50 52 4f 54 4f 43 4f 4c 20 63 6f 75 6c 64 20 62  PROTOCOL could b
1b48e 65 20 72 65 74 75 72 6e 65 64 20 62 65 63 61 75  e returned becau
1b48f 73 65 20 6f 66 20 61 20 72 75 6e 20 6f 66 20 72  se of a run of r
1b490 65 61 6c 6c 79 0d 0a 2a 2a 20 62 61 64 20 6c 75  eally..** bad lu
1b491 63 6b 20 77 68 65 6e 20 74 68 65 72 65 20 69 73  ck when there is
1b492 20 6c 6f 74 73 20 6f 66 20 63 6f 6e 74 65 6e 74   lots of content
1b493 69 6f 6e 20 66 6f 72 20 74 68 65 20 77 61 6c 2d  ion for the wal-
1b494 69 6e 64 65 78 2c 20 62 75 74 20 74 68 61 74 0d  index, but that.
1b495 0a 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 79 20  .** possibility 
1b496 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74  is so small that
1b497 20 69 74 20 63 61 6e 20 62 65 20 73 61 66 65 6c   it can be safel
1b498 79 20 6e 65 67 6c 65 63 74 65 64 2c 20 77 65 20  y neglected, we 
1b499 62 65 6c 69 65 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  believe...**..**
1b49a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 74 68 69   On success, thi
1b49b 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 69 6e  s routine obtain
1b49c 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  s a read lock on
1b49d 20 0d 0a 2a 2a 20 57 41 4c 5f 52 45 41 44 5f 4c   ..** WAL_READ_L
1b49e 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f  OCK(pWal->readLo
1b49f 63 6b 29 2e 20 20 54 68 65 20 70 57 61 6c 2d 3e  ck).  The pWal->
1b4a0 72 65 61 64 4c 6f 63 6b 20 69 6e 74 65 67 65 72  readLock integer
1b4a1 20 69 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 72   is..** in the r
1b4a2 61 6e 67 65 20 30 20 3c 3d 20 70 57 61 6c 2d 3e  ange 0 <= pWal->
1b4a3 72 65 61 64 4c 6f 63 6b 20 3c 20 57 41 4c 5f 4e  readLock < WAL_N
1b4a4 52 45 41 44 45 52 2e 20 20 49 66 20 70 57 61 6c  READER.  If pWal
1b4a5 2d 3e 72 65 61 64 4c 6f 63 6b 3d 3d 28 2d 31 29  ->readLock==(-1)
1b4a6 0d 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  ..** that means 
1b4a7 74 68 65 20 57 61 6c 20 64 6f 65 73 20 6e 6f 74  the Wal does not
1b4a8 20 68 6f 6c 64 20 61 6e 79 20 72 65 61 64 20 6c   hold any read l
1b4a9 6f 63 6b 2e 20 20 54 68 65 20 72 65 61 64 65 72  ock.  The reader
1b4aa 20 6d 75 73 74 20 6e 6f 74 0d 0a 2a 2a 20 61 63   must not..** ac
1b4ab 63 65 73 73 20 61 6e 79 20 64 61 74 61 62 61 73  cess any databas
1b4ac 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20 6d  e page that is m
1b4ad 6f 64 69 66 69 65 64 20 62 79 20 61 20 57 41 4c  odified by a WAL
1b4ae 20 66 72 61 6d 65 20 75 70 20 74 6f 20 61 6e 64   frame up to and
1b4af 0d 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 66  ..** including f
1b4b0 72 61 6d 65 20 6e 75 6d 62 65 72 20 61 52 65 61  rame number aRea
1b4b1 64 4d 61 72 6b 5b 70 57 61 6c 2d 3e 72 65 61 64  dMark[pWal->read
1b4b2 4c 6f 63 6b 5d 2e 20 20 54 68 65 20 72 65 61 64  Lock].  The read
1b4b3 65 72 20 77 69 6c 6c 0d 0a 2a 2a 20 75 73 65 20  er will..** use 
1b4b4 57 41 4c 20 66 72 61 6d 65 73 20 75 70 20 74 6f  WAL frames up to
1b4b5 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 70   and including p
1b4b6 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1b4b7 20 69 66 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f   if pWal->readLo
1b4b8 63 6b 3e 30 0d 0a 2a 2a 20 4f 72 20 69 66 20 70  ck>0..** Or if p
1b4b9 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3d 3d 30  Wal->readLock==0
1b4ba 2c 20 74 68 65 6e 20 74 68 65 20 72 65 61 64 65  , then the reade
1b4bb 72 20 77 69 6c 6c 20 69 67 6e 6f 72 65 20 74 68  r will ignore th
1b4bc 65 20 57 41 4c 0d 0a 2a 2a 20 63 6f 6d 70 6c 65  e WAL..** comple
1b4bd 74 65 6c 79 20 61 6e 64 20 67 65 74 20 61 6c 6c  tely and get all
1b4be 20 63 6f 6e 74 65 6e 74 20 64 69 72 65 63 74 6c   content directl
1b4bf 79 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  y from the datab
1b4c0 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 49 66  ase file...** If
1b4c1 20 74 68 65 20 75 73 65 57 61 6c 20 70 61 72 61   the useWal para
1b4c2 6d 65 74 65 72 20 69 73 20 31 20 74 68 65 6e 20  meter is 1 then 
1b4c3 74 68 65 20 57 41 4c 20 77 69 6c 6c 20 6e 65 76  the WAL will nev
1b4c4 65 72 20 62 65 20 69 67 6e 6f 72 65 64 20 61 6e  er be ignored an
1b4c5 64 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  d..** this routi
1b4c6 6e 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73  ne will always s
1b4c7 65 74 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  et pWal->readLoc
1b4c8 6b 3e 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0d  k>0 on success..
1b4c9 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 72 65 61  .** When the rea
1b4ca 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  d transaction is
1b4cb 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 20   completed, the 
1b4cc 63 61 6c 6c 65 72 20 6d 75 73 74 20 72 65 6c 65  caller must rele
1b4cd 61 73 65 20 74 68 65 0d 0a 2a 2a 20 6c 6f 63 6b  ase the..** lock
1b4ce 20 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43   on WAL_READ_LOC
1b4cf 4b 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b  K(pWal->readLock
1b4d0 29 20 61 6e 64 20 73 65 74 20 70 57 61 6c 2d 3e  ) and set pWal->
1b4d1 72 65 61 64 4c 6f 63 6b 20 74 6f 20 2d 31 2e 0d  readLock to -1..
1b4d2 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
1b4d3 74 69 6e 65 20 75 73 65 73 20 74 68 65 20 6e 42  tine uses the nB
1b4d4 61 63 6b 66 69 6c 6c 20 61 6e 64 20 61 52 65 61  ackfill and aRea
1b4d5 64 4d 61 72 6b 5b 5d 20 66 69 65 6c 64 73 20 6f  dMark[] fields o
1b4d6 66 20 74 68 65 20 68 65 61 64 65 72 0d 0a 2a 2a  f the header..**
1b4d7 20 74 6f 20 73 65 6c 65 63 74 20 61 20 70 61 72   to select a par
1b4d8 74 69 63 75 6c 61 72 20 57 41 4c 5f 52 45 41 44  ticular WAL_READ
1b4d9 5f 4c 4f 43 4b 28 29 20 74 68 61 74 20 73 74 72  _LOCK() that str
1b4da 69 76 65 73 20 74 6f 20 6c 65 74 20 74 68 65 0d  ives to let the.
1b4db 0a 2a 2a 20 63 68 65 63 6b 70 6f 69 6e 74 20 70  .** checkpoint p
1b4dc 72 6f 63 65 73 73 20 64 6f 20 61 73 20 6d 75 63  rocess do as muc
1b4dd 68 20 77 6f 72 6b 20 61 73 20 70 6f 73 73 69 62  h work as possib
1b4de 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  le.  This routin
1b4df 65 20 6d 69 67 68 74 0d 0a 2a 2a 20 75 70 64 61  e might..** upda
1b4e0 74 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  te values of the
1b4e1 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 61 72 72   aReadMark[] arr
1b4e2 61 79 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ay in the header
1b4e3 2c 20 62 75 74 20 69 66 20 69 74 20 64 6f 65 73  , but if it does
1b4e4 0d 0a 2a 2a 20 73 6f 20 69 74 20 74 61 6b 65 73  ..** so it takes
1b4e5 20 63 61 72 65 20 74 6f 20 68 6f 6c 64 20 61 6e   care to hold an
1b4e6 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
1b4e7 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
1b4e8 64 69 6e 67 0d 0a 2a 2a 20 57 41 4c 5f 52 45 41  ding..** WAL_REA
1b4e9 44 5f 4c 4f 43 4b 28 29 20 77 68 69 6c 65 20 63  D_LOCK() while c
1b4ea 68 61 6e 67 69 6e 67 20 76 61 6c 75 65 73 2e 0d  hanging values..
1b4eb 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
1b4ec 77 61 6c 54 72 79 42 65 67 69 6e 52 65 61 64 28  walTryBeginRead(
1b4ed 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 2a  Wal *pWal, int *
1b4ee 70 43 68 61 6e 67 65 64 2c 20 69 6e 74 20 75 73  pChanged, int us
1b4ef 65 57 61 6c 2c 20 69 6e 74 20 63 6e 74 29 7b 0d  eWal, int cnt){.
1b4f0 0a 20 20 76 6f 6c 61 74 69 6c 65 20 57 61 6c 43  .  volatile WalC
1b4f1 6b 70 74 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 20  kptInfo *pInfo; 
1b4f2 20 20 20 2f 2a 20 43 68 65 63 6b 70 6f 69 6e 74     /* Checkpoint
1b4f3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
1b4f4 77 61 6c 2d 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  wal-index */..  
1b4f5 75 33 32 20 6d 78 52 65 61 64 4d 61 72 6b 3b 20  u32 mxReadMark; 
1b4f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b4f7 2f 2a 20 4c 61 72 67 65 73 74 20 61 52 65 61 64  /* Largest aRead
1b4f8 4d 61 72 6b 5b 5d 20 76 61 6c 75 65 20 2a 2f 0d  Mark[] value */.
1b4f9 0a 20 20 69 6e 74 20 6d 78 49 3b 20 20 20 20 20  .  int mxI;     
1b4fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b4fb 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c     /* Index of l
1b4fc 61 72 67 65 73 74 20 61 52 65 61 64 4d 61 72 6b  argest aReadMark
1b4fd 5b 5d 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 69  [] value */..  i
1b4fe 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1b4ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b500 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1b501 2f 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  /..  int rc = SQ
1b502 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
1b503 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
1b504 6f 64 65 20 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  ode  */....  ass
1b505 65 72 74 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c  ert( pWal->readL
1b506 6f 63 6b 3c 30 20 29 3b 20 20 20 20 20 2f 2a 20  ock<0 );     /* 
1b507 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f  Not currently lo
1b508 63 6b 65 64 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  cked */....  /* 
1b509 54 61 6b 65 20 73 74 65 70 73 20 74 6f 20 61 76  Take steps to av
1b50a 6f 69 64 20 73 70 69 6e 6e 69 6e 67 20 66 6f 72  oid spinning for
1b50b 65 76 65 72 20 69 66 20 74 68 65 72 65 20 69 73  ever if there is
1b50c 20 61 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f   a protocol erro
1b50d 72 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 43  r...  **..  ** C
1b50e 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 61  ircumstances tha
1b50f 74 20 63 61 75 73 65 20 61 20 52 45 54 52 59 20  t cause a RETRY 
1b510 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 6c 61 73 74  should only last
1b511 20 66 6f 72 20 74 68 65 20 62 72 69 65 66 65 73   for the briefes
1b512 74 0d 0a 20 20 2a 2a 20 69 6e 73 74 61 6e 63 65  t..  ** instance
1b513 73 20 6f 66 20 74 69 6d 65 2e 20 20 4e 6f 20 49  s of time.  No I
1b514 2f 4f 20 6f 72 20 6f 74 68 65 72 20 73 79 73 74  /O or other syst
1b515 65 6d 20 63 61 6c 6c 73 20 61 72 65 20 64 6f 6e  em calls are don
1b516 65 20 77 68 69 6c 65 20 74 68 65 0d 0a 20 20 2a  e while the..  *
1b517 2a 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64  * locks are held
1b518 2c 20 73 6f 20 74 68 65 20 6c 6f 63 6b 73 20 73  , so the locks s
1b519 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 68 65 6c  hould not be hel
1b51a 64 20 66 6f 72 20 76 65 72 79 20 6c 6f 6e 67 2e  d for very long.
1b51b 20 42 75 74 20 0d 0a 20 20 2a 2a 20 69 66 20 77   But ..  ** if w
1b51c 65 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 61  e are unlucky, a
1b51d 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 74  nother process t
1b51e 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  hat is holding a
1b51f 20 6c 6f 63 6b 20 6d 69 67 68 74 20 67 65 74 0d   lock might get.
1b520 0a 20 20 2a 2a 20 70 61 67 65 64 20 6f 75 74 20  .  ** paged out 
1b521 6f 72 20 74 61 6b 65 20 61 20 70 61 67 65 2d 66  or take a page-f
1b522 61 75 6c 74 20 74 68 61 74 20 69 73 20 74 69 6d  ault that is tim
1b523 65 2d 63 6f 6e 73 75 6d 69 6e 67 20 74 6f 20 72  e-consuming to r
1b524 65 73 6f 6c 76 65 2c 20 0d 0a 20 20 2a 2a 20 64  esolve, ..  ** d
1b525 75 72 69 6e 67 20 74 68 65 20 66 65 77 20 6e 61  uring the few na
1b526 6e 6f 73 65 63 6f 6e 64 73 20 74 68 61 74 20 69  noseconds that i
1b527 74 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  t is holding the
1b528 20 6c 6f 63 6b 2e 20 20 49 6e 20 74 68 61 74 20   lock.  In that 
1b529 63 61 73 65 2c 0d 0a 20 20 2a 2a 20 69 74 20 6d  case,..  ** it m
1b52a 69 67 68 74 20 74 61 6b 65 20 6c 6f 6e 67 65 72  ight take longer
1b52b 20 74 68 61 6e 20 6e 6f 72 6d 61 6c 20 66 6f 72   than normal for
1b52c 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 66 72 65   the lock to fre
1b52d 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 41  e...  **..  ** A
1b52e 66 74 65 72 20 35 20 52 45 54 52 59 73 2c 20 77  fter 5 RETRYs, w
1b52f 65 20 62 65 67 69 6e 20 63 61 6c 6c 69 6e 67 20  e begin calling 
1b530 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 29  sqlite3OsSleep()
1b531 2e 20 20 54 68 65 20 66 69 72 73 74 20 66 65 77  .  The first few
1b532 0d 0a 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ..  ** calls to 
1b533 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 29  sqlite3OsSleep()
1b534 20 68 61 76 65 20 61 20 64 65 6c 61 79 20 6f 66   have a delay of
1b535 20 31 20 6d 69 63 72 6f 73 65 63 6f 6e 64 2e 20   1 microsecond. 
1b536 20 52 65 61 6c 6c 79 20 74 68 69 73 0d 0a 20 20   Really this..  
1b537 2a 2a 20 69 73 20 6d 6f 72 65 20 6f 66 20 61 20  ** is more of a 
1b538 73 63 68 65 64 75 6c 65 72 20 79 69 65 6c 64 20  scheduler yield 
1b539 74 68 61 6e 20 61 6e 20 61 63 74 75 61 6c 20 64  than an actual d
1b53a 65 6c 61 79 2e 20 20 42 75 74 20 6f 6e 20 74 68  elay.  But on th
1b53b 65 20 31 30 74 68 0d 0a 20 20 2a 2a 20 61 6e 20  e 10th..  ** an 
1b53c 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 72 69  subsequent retri
1b53d 65 73 2c 20 74 68 65 20 64 65 6c 61 79 73 20 73  es, the delays s
1b53e 74 61 72 74 20 62 65 63 6f 6d 69 6e 67 20 6c 6f  tart becoming lo
1b53f 6e 67 65 72 20 61 6e 64 20 6c 6f 6e 67 65 72 2c  nger and longer,
1b540 20 0d 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20   ..  ** so that 
1b541 6f 6e 20 74 68 65 20 31 30 30 74 68 20 28 61 6e  on the 100th (an
1b542 64 20 6c 61 73 74 29 20 52 45 54 52 59 20 77 65  d last) RETRY we
1b543 20 64 65 6c 61 79 20 66 6f 72 20 32 31 20 6d 69   delay for 21 mi
1b544 6c 6c 69 73 65 63 6f 6e 64 73 2e 0d 0a 20 20 2a  lliseconds...  *
1b545 2a 20 54 68 65 20 74 6f 74 61 6c 20 64 65 6c 61  * The total dela
1b546 79 20 74 69 6d 65 20 62 65 66 6f 72 65 20 67 69  y time before gi
1b547 76 69 6e 67 20 75 70 20 69 73 20 6c 65 73 73 20  ving up is less 
1b548 74 68 61 6e 20 31 20 73 65 63 6f 6e 64 2e 0d 0a  than 1 second...
1b549 20 20 2a 2f 0d 0a 20 20 69 66 28 20 63 6e 74 3e    */..  if( cnt>
1b54a 35 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 44  5 ){..    int nD
1b54b 65 6c 61 79 20 3d 20 31 3b 20 20 20 20 20 20 20  elay = 1;       
1b54c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b54d 2a 20 50 61 75 73 65 20 74 69 6d 65 20 69 6e 20  * Pause time in 
1b54e 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 2a 2f 0d  microseconds */.
1b54f 0a 20 20 20 20 69 66 28 20 63 6e 74 3e 31 30 30  .    if( cnt>100
1b550 20 29 7b 0d 0a 20 20 20 20 20 20 56 56 41 5f 4f   ){..      VVA_O
1b551 4e 4c 59 28 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45  NLY( pWal->lockE
1b552 72 72 6f 72 20 3d 20 31 3b 20 29 0d 0a 20 20 20  rror = 1; )..   
1b553 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b554 5f 50 52 4f 54 4f 43 4f 4c 3b 0d 0a 20 20 20 20  _PROTOCOL;..    
1b555 7d 0d 0a 20 20 20 20 69 66 28 20 63 6e 74 3e 3d  }..    if( cnt>=
1b556 31 30 20 29 20 6e 44 65 6c 61 79 20 3d 20 28 63  10 ) nDelay = (c
1b557 6e 74 2d 39 29 2a 32 33 38 3b 20 20 2f 2a 20 4d  nt-9)*238;  /* M
1b558 61 78 20 64 65 6c 61 79 20 32 31 6d 73 2e 20 54  ax delay 21ms. T
1b559 6f 74 61 6c 20 64 65 6c 61 79 20 39 39 36 6d 73  otal delay 996ms
1b55a 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   */..    sqlite3
1b55b 4f 73 53 6c 65 65 70 28 70 57 61 6c 2d 3e 70 56  OsSleep(pWal->pV
1b55c 66 73 2c 20 6e 44 65 6c 61 79 29 3b 0d 0a 20 20  fs, nDelay);..  
1b55d 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 75 73 65 57  }....  if( !useW
1b55e 61 6c 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  al ){..    rc = 
1b55f 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64 72 28  walIndexReadHdr(
1b560 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64 29 3b  pWal, pChanged);
1b561 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
1b562 4c 49 54 45 5f 42 55 53 59 20 29 7b 0d 0a 20 20  LITE_BUSY ){..  
1b563 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1b564 69 73 20 6e 6f 74 20 61 20 72 65 63 6f 76 65 72  is not a recover
1b565 79 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 6e 6f  y running in ano
1b566 74 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70  ther thread or p
1b567 72 6f 63 65 73 73 0d 0a 20 20 20 20 20 20 2a 2a  rocess..      **
1b568 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 42 55   then convert BU
1b569 53 59 20 65 72 72 6f 72 73 20 74 6f 20 57 41 4c  SY errors to WAL
1b56a 5f 52 45 54 52 59 2e 20 20 49 66 20 72 65 63 6f  _RETRY.  If reco
1b56b 76 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 74 6f  very is known to
1b56c 0d 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 72 75  ..      ** be ru
1b56d 6e 6e 69 6e 67 2c 20 63 6f 6e 76 65 72 74 20 42  nning, convert B
1b56e 55 53 59 20 74 6f 20 42 55 53 59 5f 52 45 43 4f  USY to BUSY_RECO
1b56f 56 45 52 59 2e 20 20 54 68 65 72 65 20 69 73 20  VERY.  There is 
1b570 61 20 72 61 63 65 20 68 65 72 65 0d 0a 20 20 20  a race here..   
1b571 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 69 67 68     ** which migh
1b572 74 20 63 61 75 73 65 20 57 41 4c 5f 52 45 54 52  t cause WAL_RETR
1b573 59 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  Y to be returned
1b574 20 65 76 65 6e 20 69 66 20 42 55 53 59 5f 52 45   even if BUSY_RE
1b575 43 4f 56 45 52 59 0d 0a 20 20 20 20 20 20 2a 2a  COVERY..      **
1b576 20 77 6f 75 6c 64 20 62 65 20 74 65 63 68 6e 69   would be techni
1b577 63 61 6c 6c 79 20 63 6f 72 72 65 63 74 2e 20 20  cally correct.  
1b578 42 75 74 20 74 68 65 20 72 61 63 65 20 69 73 20  But the race is 
1b579 62 65 6e 69 67 6e 20 73 69 6e 63 65 20 77 69 74  benign since wit
1b57a 68 0d 0a 20 20 20 20 20 20 2a 2a 20 57 41 4c 5f  h..      ** WAL_
1b57b 52 45 54 52 59 20 74 68 69 73 20 72 6f 75 74 69  RETRY this routi
1b57c 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  ne will be calle
1b57d 64 20 61 67 61 69 6e 20 61 6e 64 20 77 69 6c 6c  d again and will
1b57e 20 70 72 6f 62 61 62 6c 79 20 62 65 0d 0a 20 20   probably be..  
1b57f 20 20 20 20 2a 2a 20 72 69 67 68 74 20 6f 6e 20      ** right on 
1b580 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61  the second itera
1b581 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  tion...      */.
1b582 0a 20 20 20 20 20 20 69 66 28 20 70 57 61 6c 2d  .      if( pWal-
1b583 3e 61 70 57 69 44 61 74 61 5b 30 5d 3d 3d 30 20  >apWiData[0]==0 
1b584 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  ){..        /* T
1b585 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61  his branch is ta
1b586 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 78 53 68  ken when the xSh
1b587 6d 4d 61 70 28 29 20 6d 65 74 68 6f 64 20 72 65  mMap() method re
1b588 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 42 55 53  turns SQLITE_BUS
1b589 59 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 57  Y...        ** W
1b58a 65 20 61 73 73 75 6d 65 20 74 68 69 73 20 69 73  e assume this is
1b58b 20 61 20 74 72 61 6e 73 69 65 6e 74 20 63 6f 6e   a transient con
1b58c 64 69 74 69 6f 6e 2c 20 73 6f 20 72 65 74 75 72  dition, so retur
1b58d 6e 20 57 41 4c 5f 52 45 54 52 59 2e 20 54 68 65  n WAL_RETRY. The
1b58e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 78 53 68  ..        ** xSh
1b58f 6d 4d 61 70 28 29 20 69 6d 70 6c 65 6d 65 6e 74  mMap() implement
1b590 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68  ation used by th
1b591 65 20 64 65 66 61 75 6c 74 20 75 6e 69 78 20 61  e default unix a
1b592 6e 64 20 77 69 6e 33 32 20 56 46 53 20 0d 0a 20  nd win32 VFS .. 
1b593 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 75 6c 65         ** module
1b594 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  s may return SQL
1b595 49 54 45 5f 42 55 53 59 20 64 75 65 20 74 6f 20  ITE_BUSY due to 
1b596 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
1b597 20 69 6e 20 74 68 65 20 0d 0a 20 20 20 20 20 20   in the ..      
1b598 20 20 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64    ** code that d
1b599 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
1b59a 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 68 61  r or not the sha
1b59b 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f  red-memory regio
1b59c 6e 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d  n ..        ** m
1b59d 75 73 74 20 62 65 20 7a 65 72 6f 65 64 20 62 65  ust be zeroed be
1b59e 66 6f 72 65 20 74 68 65 20 72 65 71 75 65 73 74  fore the request
1b59f 65 64 20 70 61 67 65 20 69 73 20 72 65 74 75 72  ed page is retur
1b5a0 6e 65 64 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f  ned...        */
1b5a1 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 57  ..        rc = W
1b5a2 41 4c 5f 52 45 54 52 59 3b 0d 0a 20 20 20 20 20  AL_RETRY;..     
1b5a3 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
1b5a4 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 4c  E_OK==(rc = walL
1b5a5 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20  ockShared(pWal, 
1b5a6 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c 4f 43 4b  WAL_RECOVER_LOCK
1b5a7 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 77  )) ){..        w
1b5a8 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70  alUnlockShared(p
1b5a9 57 61 6c 2c 20 57 41 4c 5f 52 45 43 4f 56 45 52  Wal, WAL_RECOVER
1b5aa 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 20 20 20  _LOCK);..       
1b5ab 20 72 63 20 3d 20 57 41 4c 5f 52 45 54 52 59 3b   rc = WAL_RETRY;
1b5ac 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ..      }else if
1b5ad 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
1b5ae 59 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63  Y ){..        rc
1b5af 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52   = SQLITE_BUSY_R
1b5b0 45 43 4f 56 45 52 59 3b 0d 0a 20 20 20 20 20 20  ECOVERY;..      
1b5b1 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  }..    }..    if
1b5b2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1b5b3 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
1b5b4 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d   rc;..    }..  }
1b5b5 0d 0a 0d 0a 20 20 70 49 6e 66 6f 20 3d 20 77 61  ....  pInfo = wa
1b5b6 6c 43 6b 70 74 49 6e 66 6f 28 70 57 61 6c 29 3b  lCkptInfo(pWal);
1b5b7 0d 0a 20 20 69 66 28 20 21 75 73 65 57 61 6c 20  ..  if( !useWal 
1b5b8 26 26 20 70 49 6e 66 6f 2d 3e 6e 42 61 63 6b 66  && pInfo->nBackf
1b5b9 69 6c 6c 3d 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d  ill==pWal->hdr.m
1b5ba 78 46 72 61 6d 65 20 29 7b 0d 0a 20 20 20 20 2f  xFrame ){..    /
1b5bb 2a 20 54 68 65 20 57 41 4c 20 68 61 73 20 62 65  * The WAL has be
1b5bc 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 61  en completely ba
1b5bd 63 6b 66 69 6c 6c 65 64 20 28 6f 72 20 69 74 20  ckfilled (or it 
1b5be 69 73 20 65 6d 70 74 79 29 2e 0d 0a 20 20 20 20  is empty)...    
1b5bf 2a 2a 20 61 6e 64 20 63 61 6e 20 62 65 20 73 61  ** and can be sa
1b5c0 66 65 6c 79 20 69 67 6e 6f 72 65 64 2e 0d 0a 20  fely ignored... 
1b5c1 20 20 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20     */..    rc = 
1b5c2 77 61 6c 4c 6f 63 6b 53 68 61 72 65 64 28 70 57  walLockShared(pW
1b5c3 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43  al, WAL_READ_LOC
1b5c4 4b 28 30 29 29 3b 0d 0a 20 20 20 20 77 61 6c 53  K(0));..    walS
1b5c5 68 6d 42 61 72 72 69 65 72 28 70 57 61 6c 29 3b  hmBarrier(pWal);
1b5c6 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
1b5c7 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1b5c8 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 28 76 6f    if( memcmp((vo
1b5c9 69 64 20 2a 29 77 61 6c 49 6e 64 65 78 48 64 72  id *)walIndexHdr
1b5ca 28 70 57 61 6c 29 2c 20 26 70 57 61 6c 2d 3e 68  (pWal), &pWal->h
1b5cb 64 72 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e  dr, sizeof(WalIn
1b5cc 64 65 78 48 64 72 29 29 20 29 7b 0d 0a 20 20 20  dexHdr)) ){..   
1b5cd 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f       /* It is no
1b5ce 74 20 73 61 66 65 20 74 6f 20 61 6c 6c 6f 77 20  t safe to allow 
1b5cf 74 68 65 20 72 65 61 64 65 72 20 74 6f 20 63 6f  the reader to co
1b5d0 6e 74 69 6e 75 65 20 68 65 72 65 20 69 66 20 66  ntinue here if f
1b5d1 72 61 6d 65 73 0d 0a 20 20 20 20 20 20 20 20 2a  rames..        *
1b5d2 2a 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20  * may have been 
1b5d3 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
1b5d4 6c 6f 67 20 62 65 66 6f 72 65 20 52 45 41 44 5f  log before READ_
1b5d5 4c 4f 43 4b 28 30 29 20 77 61 73 20 6f 62 74 61  LOCK(0) was obta
1b5d6 69 6e 65 64 2e 0d 0a 20 20 20 20 20 20 20 20 2a  ined...        *
1b5d7 2a 20 57 68 65 6e 20 68 6f 6c 64 69 6e 67 20 52  * When holding R
1b5d8 45 41 44 5f 4c 4f 43 4b 28 30 29 2c 20 74 68 65  EAD_LOCK(0), the
1b5d9 20 72 65 61 64 65 72 20 69 67 6e 6f 72 65 73 20   reader ignores 
1b5da 74 68 65 20 65 6e 74 69 72 65 20 6c 6f 67 20 66  the entire log f
1b5db 69 6c 65 2c 0d 0a 20 20 20 20 20 20 20 20 2a 2a  ile,..        **
1b5dc 20 77 68 69 63 68 20 69 6d 70 6c 69 65 73 20 74   which implies t
1b5dd 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
1b5de 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61   file contains a
1b5df 20 74 72 75 73 74 77 6f 72 74 68 79 0d 0a 20 20   trustworthy..  
1b5e0 20 20 20 20 20 20 2a 2a 20 73 6e 61 70 73 68 6f        ** snapsho
1b5e1 54 2e 20 53 69 6e 63 65 20 68 6f 6c 64 69 6e 67  T. Since holding
1b5e2 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 20 70 72   READ_LOCK(0) pr
1b5e3 65 76 65 6e 74 73 20 61 20 63 68 65 63 6b 70 6f  events a checkpo
1b5e4 69 6e 74 20 66 72 6f 6d 0d 0a 20 20 20 20 20 20  int from..      
1b5e5 20 20 2a 2a 20 68 61 70 70 65 6e 69 6e 67 2c 20    ** happening, 
1b5e6 74 68 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20  this is usually 
1b5e7 63 6f 72 72 65 63 74 2e 0d 0a 20 20 20 20 20 20  correct...      
1b5e8 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a    **..        **
1b5e9 20 48 6f 77 65 76 65 72 2c 20 69 66 20 66 72 61   However, if fra
1b5ea 6d 65 73 20 68 61 76 65 20 62 65 65 6e 20 61 70  mes have been ap
1b5eb 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6c 6f  pended to the lo
1b5ec 67 20 28 6f 72 20 69 66 20 74 68 65 20 6c 6f 67  g (or if the log
1b5ed 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73   ..        ** is
1b5ee 20 77 72 61 70 70 65 64 20 61 6e 64 20 77 72 69   wrapped and wri
1b5ef 74 74 65 6e 20 66 6f 72 20 74 68 61 74 20 6d 61  tten for that ma
1b5f0 74 74 65 72 29 20 62 65 66 6f 72 65 20 74 68 65  tter) before the
1b5f1 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 0d 0a 20   READ_LOCK(0).. 
1b5f2 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 62 74         ** is obt
1b5f3 61 69 6e 65 64 2c 20 74 68 61 74 20 69 73 20 6e  ained, that is n
1b5f4 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74  ot necessarily t
1b5f5 72 75 65 2e 20 41 20 63 68 65 63 6b 70 6f 69 6e  rue. A checkpoin
1b5f6 74 65 72 20 6d 61 79 0d 0a 20 20 20 20 20 20 20  ter may..       
1b5f7 20 2a 2a 20 68 61 76 65 20 73 74 61 72 74 65 64   ** have started
1b5f8 20 74 6f 20 62 61 63 6b 66 69 6c 6c 20 74 68 65   to backfill the
1b5f9 20 61 70 70 65 6e 64 65 64 20 66 72 61 6d 65 73   appended frames
1b5fa 20 62 75 74 20 63 72 61 73 68 65 64 20 62 65 66   but crashed bef
1b5fb 6f 72 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ore..        ** 
1b5fc 69 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 65 61  it finished. Lea
1b5fd 76 69 6e 67 20 61 20 63 6f 72 72 75 70 74 20 69  ving a corrupt i
1b5fe 6d 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  mage in the data
1b5ff 62 61 73 65 20 66 69 6c 65 2e 0d 0a 20 20 20 20  base file...    
1b600 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
1b601 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 64 28  walUnlockShared(
1b602 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c  pWal, WAL_READ_L
1b603 4f 43 4b 28 30 29 29 3b 0d 0a 20 20 20 20 20 20  OCK(0));..      
1b604 20 20 72 65 74 75 72 6e 20 57 41 4c 5f 52 45 54    return WAL_RET
1b605 52 59 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  RY;..      }..  
1b606 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f      pWal->readLo
1b607 63 6b 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72  ck = 0;..      r
1b608 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1b609 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
1b60a 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc!=SQLITE_BUSY 
1b60b 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
1b60c 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d   rc;..    }..  }
1b60d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 67  ....  /* If we g
1b60e 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20  et this far, it 
1b60f 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72  means that the r
1b610 65 61 64 65 72 20 77 69 6c 6c 20 77 61 6e 74 20  eader will want 
1b611 74 6f 20 75 73 65 0d 0a 20 20 2a 2a 20 74 68 65  to use..  ** the
1b612 20 57 41 4c 20 74 6f 20 67 65 74 20 61 74 20 63   WAL to get at c
1b613 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 63 65  ontent from rece
1b614 6e 74 20 63 6f 6d 6d 69 74 73 2e 20 20 54 68 65  nt commits.  The
1b615 20 6a 6f 62 20 6e 6f 77 20 69 73 0d 0a 20 20 2a   job now is..  *
1b616 2a 20 74 6f 20 73 65 6c 65 63 74 20 6f 6e 65 20  * to select one 
1b617 6f 66 20 74 68 65 20 61 52 65 61 64 4d 61 72 6b  of the aReadMark
1b618 5b 5d 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  [] entries that 
1b619 69 73 20 63 6c 6f 73 65 73 74 20 74 6f 0d 0a 20  is closest to.. 
1b61a 20 2a 2a 20 62 75 74 20 6e 6f 74 20 65 78 63 65   ** but not exce
1b61b 65 64 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e  eding pWal->hdr.
1b61c 6d 78 46 72 61 6d 65 20 61 6e 64 20 6c 6f 63 6b  mxFrame and lock
1b61d 20 74 68 61 74 20 65 6e 74 72 79 2e 0d 0a 20 20   that entry...  
1b61e 2a 2f 0d 0a 20 20 6d 78 52 65 61 64 4d 61 72 6b  */..  mxReadMark
1b61f 20 3d 20 30 3b 0d 0a 20 20 6d 78 49 20 3d 20 30   = 0;..  mxI = 0
1b620 3b 0d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  ;..  for(i=1; i<
1b621 57 41 4c 5f 4e 52 45 41 44 45 52 3b 20 69 2b 2b  WAL_NREADER; i++
1b622 29 7b 0d 0a 20 20 20 20 75 33 32 20 74 68 69 73  ){..    u32 this
1b623 4d 61 72 6b 20 3d 20 70 49 6e 66 6f 2d 3e 61 52  Mark = pInfo->aR
1b624 65 61 64 4d 61 72 6b 5b 69 5d 3b 0d 0a 20 20 20  eadMark[i];..   
1b625 20 69 66 28 20 6d 78 52 65 61 64 4d 61 72 6b 3c   if( mxReadMark<
1b626 3d 74 68 69 73 4d 61 72 6b 20 26 26 20 74 68 69  =thisMark && thi
1b627 73 4d 61 72 6b 3c 3d 70 57 61 6c 2d 3e 68 64 72  sMark<=pWal->hdr
1b628 2e 6d 78 46 72 61 6d 65 20 29 7b 0d 0a 20 20 20  .mxFrame ){..   
1b629 20 20 20 61 73 73 65 72 74 28 20 74 68 69 73 4d     assert( thisM
1b62a 61 72 6b 21 3d 52 45 41 44 4d 41 52 4b 5f 4e 4f  ark!=READMARK_NO
1b62b 54 5f 55 53 45 44 20 29 3b 0d 0a 20 20 20 20 20  T_USED );..     
1b62c 20 6d 78 52 65 61 64 4d 61 72 6b 20 3d 20 74 68   mxReadMark = th
1b62d 69 73 4d 61 72 6b 3b 0d 0a 20 20 20 20 20 20 6d  isMark;..      m
1b62e 78 49 20 3d 20 69 3b 0d 0a 20 20 20 20 7d 0d 0a  xI = i;..    }..
1b62f 20 20 7d 0d 0a 20 20 2f 2a 20 54 68 65 72 65 20    }..  /* There 
1b630 77 61 73 20 6f 6e 63 65 20 61 6e 20 22 69 66 22  was once an "if"
1b631 20 68 65 72 65 2e 20 54 68 65 20 65 78 74 72 61   here. The extra
1b632 20 22 7b 22 20 69 73 20 74 6f 20 70 72 65 73 65   "{" is to prese
1b633 72 76 65 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e  rve indentation.
1b634 20 2a 2f 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 66   */..  {..    if
1b635 28 20 28 70 57 61 6c 2d 3e 72 65 61 64 4f 6e 6c  ( (pWal->readOnl
1b636 79 20 26 20 57 41 4c 5f 53 48 4d 5f 52 44 4f 4e  y & WAL_SHM_RDON
1b637 4c 59 29 3d 3d 30 0d 0a 20 20 20 20 20 26 26 20  LY)==0..     && 
1b638 28 6d 78 52 65 61 64 4d 61 72 6b 3c 70 57 61 6c  (mxReadMark<pWal
1b639 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 7c 7c  ->hdr.mxFrame ||
1b63a 20 6d 78 49 3d 3d 30 29 0d 0a 20 20 20 20 29 7b   mxI==0)..    ){
1b63b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ..      for(i=1;
1b63c 20 69 3c 57 41 4c 5f 4e 52 45 41 44 45 52 3b 20   i<WAL_NREADER; 
1b63d 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  i++){..        r
1b63e 63 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75  c = walLockExclu
1b63f 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52  sive(pWal, WAL_R
1b640 45 41 44 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b  EAD_LOCK(i), 1);
1b641 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ..        if( rc
1b642 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1b643 20 20 20 20 20 20 20 20 20 20 6d 78 52 65 61 64            mxRead
1b644 4d 61 72 6b 20 3d 20 70 49 6e 66 6f 2d 3e 61 52  Mark = pInfo->aR
1b645 65 61 64 4d 61 72 6b 5b 69 5d 20 3d 20 70 57 61  eadMark[i] = pWa
1b646 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0d  l->hdr.mxFrame;.
1b647 0a 20 20 20 20 20 20 20 20 20 20 6d 78 49 20 3d  .          mxI =
1b648 20 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 77   i;..          w
1b649 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69 76  alUnlockExclusiv
1b64a 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44  e(pWal, WAL_READ
1b64b 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0d 0a 20  _LOCK(i), 1);.. 
1b64c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d           break;.
1b64d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
1b64e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55  f( rc!=SQLITE_BU
1b64f 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  SY ){..         
1b650 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1b651 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
1b652 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1b653 6d 78 49 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  mxI==0 ){..     
1b654 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1b655 49 54 45 5f 42 55 53 59 20 7c 7c 20 28 70 57 61  ITE_BUSY || (pWa
1b656 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57 41  l->readOnly & WA
1b657 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 29 21 3d 30  L_SHM_RDONLY)!=0
1b658 20 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72   );..      retur
1b659 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  n rc==SQLITE_BUS
1b65a 59 20 3f 20 57 41 4c 5f 52 45 54 52 59 20 3a 20  Y ? WAL_RETRY : 
1b65b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f  SQLITE_READONLY_
1b65c 43 41 4e 54 4c 4f 43 4b 3b 0d 0a 20 20 20 20 7d  CANTLOCK;..    }
1b65d 0d 0a 0d 0a 20 20 20 20 72 63 20 3d 20 77 61 6c  ....    rc = wal
1b65e 4c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c  LockShared(pWal,
1b65f 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d   WAL_READ_LOCK(m
1b660 78 49 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  xI));..    if( r
1b661 63 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  c ){..      retu
1b662 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  rn rc==SQLITE_BU
1b663 53 59 20 3f 20 57 41 4c 5f 52 45 54 52 59 20 3a  SY ? WAL_RETRY :
1b664 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20   rc;..    }..   
1b665 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65   /* Now that the
1b666 20 72 65 61 64 2d 6c 6f 63 6b 20 68 61 73 20 62   read-lock has b
1b667 65 65 6e 20 6f 62 74 61 69 6e 65 64 2c 20 63 68  een obtained, ch
1b668 65 63 6b 20 74 68 61 74 20 6e 65 69 74 68 65 72  eck that neither
1b669 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 76 61 6c   the..    ** val
1b66a 75 65 20 69 6e 20 74 68 65 20 61 52 65 61 64 4d  ue in the aReadM
1b66b 61 72 6b 5b 5d 20 61 72 72 61 79 20 6f 72 20 74  ark[] array or t
1b66c 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
1b66d 68 65 20 77 61 6c 2d 69 6e 64 65 78 0d 0a 20 20  he wal-index..  
1b66e 20 20 2a 2a 20 68 65 61 64 65 72 20 68 61 76 65    ** header have
1b66f 20 63 68 61 6e 67 65 64 2e 0d 0a 20 20 20 20 2a   changed...    *
1b670 2a 0d 0a 20 20 20 20 2a 2a 20 49 74 20 69 73 20  *..    ** It is 
1b671 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 68 65  necessary to che
1b672 63 6b 20 74 68 61 74 20 74 68 65 20 77 61 6c 2d  ck that the wal-
1b673 69 6e 64 65 78 20 68 65 61 64 65 72 20 64 69 64  index header did
1b674 20 6e 6f 74 20 63 68 61 6e 67 65 0d 0a 20 20 20   not change..   
1b675 20 2a 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20   ** between the 
1b676 74 69 6d 65 20 69 74 20 77 61 73 20 72 65 61 64  time it was read
1b677 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 73 68   and when the sh
1b678 61 72 65 64 2d 6c 6f 63 6b 20 77 61 73 20 6f 62  ared-lock was ob
1b679 74 61 69 6e 65 64 0d 0a 20 20 20 20 2a 2a 20 6f  tained..    ** o
1b67a 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28  n WAL_READ_LOCK(
1b67b 6d 78 49 29 20 77 61 73 20 6f 62 74 61 69 6e 65  mxI) was obtaine
1b67c 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  d to account for
1b67d 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79   the possibility
1b67e 0d 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68  ..    ** that th
1b67f 65 20 6c 6f 67 20 66 69 6c 65 20 6d 61 79 20 68  e log file may h
1b680 61 76 65 20 62 65 65 6e 20 77 72 61 70 70 65 64  ave been wrapped
1b681 20 62 79 20 61 20 77 72 69 74 65 72 2c 20 6f 72   by a writer, or
1b682 20 74 68 61 74 20 66 72 61 6d 65 73 0d 0a 20 20   that frames..  
1b683 20 20 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20    ** that occur 
1b684 6c 61 74 65 72 20 69 6e 20 74 68 65 20 6c 6f 67  later in the log
1b685 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64 72 2e   than pWal->hdr.
1b686 6d 78 46 72 61 6d 65 20 6d 61 79 20 68 61 76 65  mxFrame may have
1b687 20 62 65 65 6e 0d 0a 20 20 20 20 2a 2a 20 63 6f   been..    ** co
1b688 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61  pied into the da
1b689 74 61 62 61 73 65 20 62 79 20 61 20 63 68 65 63  tabase by a chec
1b68a 6b 70 6f 69 6e 74 65 72 2e 20 49 66 20 65 69 74  kpointer. If eit
1b68b 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 68 69  her of these thi
1b68c 6e 67 73 0d 0a 20 20 20 20 2a 2a 20 68 61 70 70  ngs..    ** happ
1b68d 65 6e 65 64 2c 20 74 68 65 6e 20 72 65 61 64 69  ened, then readi
1b68e 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
1b68f 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
1b690 20 76 61 6c 75 65 20 6f 66 0d 0a 20 20 20 20 2a   value of..    *
1b691 2a 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  * pWal->hdr.mxFr
1b692 61 6d 65 20 72 69 73 6b 73 20 72 65 61 64 69 6e  ame risks readin
1b693 67 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 6e  g a corrupted sn
1b694 61 70 73 68 6f 74 2e 20 53 6f 2c 20 72 65 74 72  apshot. So, retr
1b695 79 0d 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 61  y..    ** instea
1b696 64 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  d...    **..    
1b697 2a 2a 20 54 68 69 73 20 64 6f 65 73 20 6e 6f 74  ** This does not
1b698 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
1b699 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  the copy of the 
1b69a 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72  wal-index header
1b69b 20 69 73 20 75 70 20 74 6f 0d 0a 20 20 20 20 2a   is up to..    *
1b69c 2a 20 64 61 74 65 20 62 65 66 6f 72 65 20 70 72  * date before pr
1b69d 6f 63 65 65 64 69 6e 67 2e 20 54 68 61 74 20 77  oceeding. That w
1b69e 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 6f 73 73  ould not be poss
1b69f 69 62 6c 65 20 77 69 74 68 6f 75 74 20 73 6f 6d  ible without som
1b6a0 65 68 6f 77 0d 0a 20 20 20 20 2a 2a 20 62 6c 6f  ehow..    ** blo
1b6a1 63 6b 69 6e 67 20 77 72 69 74 65 72 73 2e 20 49  cking writers. I
1b6a2 74 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65  t only guarantee
1b6a3 73 20 74 68 61 74 20 61 20 64 61 6e 67 65 72 6f  s that a dangero
1b6a4 75 73 20 63 68 65 63 6b 70 6f 69 6e 74 20 6f 72  us checkpoint or
1b6a5 20 0d 0a 20 20 20 20 2a 2a 20 6c 6f 67 2d 77 72   ..    ** log-wr
1b6a6 61 70 20 28 65 69 74 68 65 72 20 6f 66 20 77 68  ap (either of wh
1b6a7 69 63 68 20 77 6f 75 6c 64 20 72 65 71 75 69 72  ich would requir
1b6a8 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  e an exclusive l
1b6a9 6f 63 6b 20 6f 6e 0d 0a 20 20 20 20 2a 2a 20 57  ock on..    ** W
1b6aa 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d 78 49  AL_READ_LOCK(mxI
1b6ab 29 29 20 68 61 73 20 6e 6f 74 20 6f 63 63 75 72  )) has not occur
1b6ac 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 6e  red since the sn
1b6ad 61 70 73 68 6f 74 20 77 61 73 20 76 61 6c 69 64  apshot was valid
1b6ae 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 77  ...    */..    w
1b6af 61 6c 53 68 6d 42 61 72 72 69 65 72 28 70 57 61  alShmBarrier(pWa
1b6b0 6c 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 49 6e  l);..    if( pIn
1b6b1 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b 6d 78  fo->aReadMark[mx
1b6b2 49 5d 21 3d 6d 78 52 65 61 64 4d 61 72 6b 0d 0a  I]!=mxReadMark..
1b6b3 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 28       || memcmp((
1b6b4 76 6f 69 64 20 2a 29 77 61 6c 49 6e 64 65 78 48  void *)walIndexH
1b6b5 64 72 28 70 57 61 6c 29 2c 20 26 70 57 61 6c 2d  dr(pWal), &pWal-
1b6b6 3e 68 64 72 2c 20 73 69 7a 65 6f 66 28 57 61 6c  >hdr, sizeof(Wal
1b6b7 49 6e 64 65 78 48 64 72 29 29 0d 0a 20 20 20 20  IndexHdr))..    
1b6b8 29 7b 0d 0a 20 20 20 20 20 20 77 61 6c 55 6e 6c  ){..      walUnl
1b6b9 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20  ockShared(pWal, 
1b6ba 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d 78  WAL_READ_LOCK(mx
1b6bb 49 29 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  I));..      retu
1b6bc 72 6e 20 57 41 4c 5f 52 45 54 52 59 3b 0d 0a 20  rn WAL_RETRY;.. 
1b6bd 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1b6be 20 61 73 73 65 72 74 28 20 6d 78 52 65 61 64 4d   assert( mxReadM
1b6bf 61 72 6b 3c 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d  ark<=pWal->hdr.m
1b6c0 78 46 72 61 6d 65 20 29 3b 0d 0a 20 20 20 20 20  xFrame );..     
1b6c1 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20   pWal->readLock 
1b6c2 3d 20 28 69 31 36 29 6d 78 49 3b 0d 0a 20 20 20  = (i16)mxI;..   
1b6c3 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
1b6c4 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
1b6c5 2a 2a 20 42 65 67 69 6e 20 61 20 72 65 61 64 20  ** Begin a read 
1b6c6 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
1b6c7 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2a  he database...**
1b6c8 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1b6c9 65 20 75 73 65 64 20 74 6f 20 62 65 20 63 61 6c  e used to be cal
1b6ca 6c 65 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 53  led sqlite3OpenS
1b6cb 6e 61 70 73 68 6f 74 28 29 20 61 6e 64 20 77 69  napshot() and wi
1b6cc 74 68 20 67 6f 6f 64 20 72 65 61 73 6f 6e 3a 0d  th good reason:.
1b6cd 0a 2a 2a 20 69 74 20 74 61 6b 65 73 20 61 20 73  .** it takes a s
1b6ce 6e 61 70 73 68 6f 74 20 6f 66 20 74 68 65 20 73  napshot of the s
1b6cf 74 61 74 65 20 6f 66 20 74 68 65 20 57 41 4c 20  tate of the WAL 
1b6d0 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 20 66 6f  and wal-index fo
1b6d1 72 20 74 68 65 20 63 75 72 72 65 6e 74 0d 0a 2a  r the current..*
1b6d2 2a 20 69 6e 73 74 61 6e 74 20 69 6e 20 74 69 6d  * instant in tim
1b6d3 65 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74 20  e.  The current 
1b6d4 74 68 72 65 61 64 20 77 69 6c 6c 20 63 6f 6e 74  thread will cont
1b6d5 69 6e 75 65 20 74 6f 20 75 73 65 20 74 68 69 73  inue to use this
1b6d6 20 73 6e 61 70 73 68 6f 74 2e 0d 0a 2a 2a 20 4f   snapshot...** O
1b6d7 74 68 65 72 20 74 68 72 65 61 64 73 20 6d 69 67  ther threads mig
1b6d8 68 74 20 61 70 70 65 6e 64 20 6e 65 77 20 63 6f  ht append new co
1b6d9 6e 74 65 6e 74 20 74 6f 20 74 68 65 20 57 41 4c  ntent to the WAL
1b6da 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 20 62   and wal-index b
1b6db 75 74 0d 0a 2a 2a 20 74 68 61 74 20 65 78 74 72  ut..** that extr
1b6dc 61 20 63 6f 6e 74 65 6e 74 20 69 73 20 69 67 6e  a content is ign
1b6dd 6f 72 65 64 20 62 79 20 74 68 65 20 63 75 72 72  ored by the curr
1b6de 65 6e 74 20 74 68 72 65 61 64 2e 0d 0a 2a 2a 0d  ent thread...**.
1b6df 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
1b6e0 61 73 65 20 63 6f 6e 74 65 6e 74 73 20 68 61 76  ase contents hav
1b6e1 65 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20  e changes since 
1b6e2 74 68 65 20 70 72 65 76 69 6f 75 73 20 72 65 61  the previous rea
1b6e3 64 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  d..** transactio
1b6e4 6e 2c 20 74 68 65 6e 20 2a 70 43 68 61 6e 67 65  n, then *pChange
1b6e5 64 20 69 73 20 73 65 74 20 74 6f 20 31 20 62 65  d is set to 1 be
1b6e6 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20  fore returning. 
1b6e7 20 54 68 65 0d 0a 2a 2a 20 50 61 67 65 72 20 6c   The..** Pager l
1b6e8 61 79 65 72 20 77 69 6c 6c 20 75 73 65 20 74 68  ayer will use th
1b6e9 69 73 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20  is to know that 
1b6ea 69 73 20 63 61 63 68 65 20 69 73 20 73 74 61 6c  is cache is stal
1b6eb 65 20 61 6e 64 0d 0a 2a 2a 20 6e 65 65 64 73 20  e and..** needs 
1b6ec 74 6f 20 62 65 20 66 6c 75 73 68 65 64 2e 0d 0a  to be flushed...
1b6ed 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1b6ee 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1b6ef 6c 42 65 67 69 6e 52 65 61 64 54 72 61 6e 73 61  lBeginReadTransa
1b6f0 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 2c  ction(Wal *pWal,
1b6f1 20 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 29 7b   int *pChanged){
1b6f2 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
1b6f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b6f4 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1b6f5 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 6e 74  de */..  int cnt
1b6f6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1b6f7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1b6f8 65 72 20 6f 66 20 54 72 79 42 65 67 69 6e 52 65  er of TryBeginRe
1b6f9 61 64 20 61 74 74 65 6d 70 74 73 20 2a 2f 0d 0a  ad attempts */..
1b6fa 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20 20 72 63 20  ..  do{..    rc 
1b6fb 3d 20 77 61 6c 54 72 79 42 65 67 69 6e 52 65 61  = walTryBeginRea
1b6fc 64 28 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64  d(pWal, pChanged
1b6fd 2c 20 30 2c 20 2b 2b 63 6e 74 29 3b 0d 0a 20 20  , 0, ++cnt);..  
1b6fe 7d 77 68 69 6c 65 28 20 72 63 3d 3d 57 41 4c 5f  }while( rc==WAL_
1b6ff 52 45 54 52 59 20 29 3b 0d 0a 20 20 74 65 73 74  RETRY );..  test
1b700 63 61 73 65 28 20 28 72 63 26 30 78 66 66 29 3d  case( (rc&0xff)=
1b701 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0d  =SQLITE_BUSY );.
1b702 0a 20 20 74 65 73 74 63 61 73 65 28 20 28 72 63  .  testcase( (rc
1b703 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49  &0xff)==SQLITE_I
1b704 4f 45 52 52 20 29 3b 0d 0a 20 20 74 65 73 74 63  OERR );..  testc
1b705 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ase( rc==SQLITE_
1b706 50 52 4f 54 4f 43 4f 4c 20 29 3b 0d 0a 20 20 74  PROTOCOL );..  t
1b707 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
1b708 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 72 65 74  ITE_OK );..  ret
1b709 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1b70a 0d 0a 2a 2a 20 46 69 6e 69 73 68 20 77 69 74 68  ..** Finish with
1b70b 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74   a read transact
1b70c 69 6f 6e 2e 20 20 41 6c 6c 20 74 68 69 73 20 64  ion.  All this d
1b70d 6f 65 73 20 69 73 20 72 65 6c 65 61 73 65 20 74  oes is release t
1b70e 68 65 0d 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b  he..** read-lock
1b70f 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
1b710 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1b711 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e  e3WalEndReadTran
1b712 73 61 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61  saction(Wal *pWa
1b713 6c 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 57 61  l){..  sqlite3Wa
1b714 6c 45 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63  lEndWriteTransac
1b715 74 69 6f 6e 28 70 57 61 6c 29 3b 0d 0a 20 20 69  tion(pWal);..  i
1b716 66 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  f( pWal->readLoc
1b717 6b 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 77 61 6c  k>=0 ){..    wal
1b718 55 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61  UnlockShared(pWa
1b719 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b  l, WAL_READ_LOCK
1b71a 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29  (pWal->readLock)
1b71b 29 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e 72 65  );..    pWal->re
1b71c 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b 0d 0a 20 20  adLock = -1;..  
1b71d 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  }..}..../*..** R
1b71e 65 61 64 20 61 20 70 61 67 65 20 66 72 6f 6d 20  ead a page from 
1b71f 74 68 65 20 57 41 4c 2c 20 69 66 20 69 74 20 69  the WAL, if it i
1b720 73 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  s present in the
1b721 20 57 41 4c 20 61 6e 64 20 69 66 20 74 68 65 20   WAL and if the 
1b722 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 20 72 65 61  ..** current rea
1b723 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  d transaction is
1b724 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 75   configured to u
1b725 73 65 20 74 68 65 20 57 41 4c 2e 20 20 0d 0a 2a  se the WAL.  ..*
1b726 2a 0d 0a 2a 2a 20 54 68 65 20 2a 70 49 6e 57 61  *..** The *pInWa
1b727 6c 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66  l is set to 1 if
1b728 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
1b729 61 67 65 20 69 73 20 69 6e 20 74 68 65 20 57 41  age is in the WA
1b72a 4c 20 61 6e 64 0d 0a 2a 2a 20 68 61 73 20 62 65  L and..** has be
1b72b 65 6e 20 6c 6f 61 64 65 64 2e 20 20 4f 72 20 2a  en loaded.  Or *
1b72c 70 49 6e 57 61 6c 20 69 73 20 73 65 74 20 74 6f  pInWal is set to
1b72d 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 77   0 if the page w
1b72e 61 73 20 6e 6f 74 20 69 6e 20 0d 0a 2a 2a 20 74  as not in ..** t
1b72f 68 65 20 57 41 4c 20 61 6e 64 20 6e 65 65 64 73  he WAL and needs
1b730 20 74 6f 20 62 65 20 72 65 61 64 20 6f 75 74 20   to be read out 
1b731 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
1b732 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1b733 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b734 57 61 6c 52 65 61 64 28 0d 0a 20 20 57 61 6c 20  WalRead(..  Wal 
1b735 2a 70 57 61 6c 2c 20 20 20 20 20 20 20 20 20 20  *pWal,          
1b736 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
1b737 41 4c 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  AL handle */..  
1b738 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20  Pgno pgno,      
1b739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b73a 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65  /* Database page
1b73b 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20   number to read 
1b73c 64 61 74 61 20 66 6f 72 20 2a 2f 0d 0a 20 20 69  data for */..  i
1b73d 6e 74 20 2a 70 49 6e 57 61 6c 2c 20 20 20 20 20  nt *pInWal,     
1b73e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b73f 2a 20 4f 55 54 3a 20 54 72 75 65 20 69 66 20 64  * OUT: True if d
1b740 61 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d  ata is read from
1b741 20 57 41 4c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e   WAL */..  int n
1b742 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  Out,            
1b743 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1b744 7a 65 20 6f 66 20 62 75 66 66 65 72 20 70 4f 75  ze of buffer pOu
1b745 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20  t in bytes */.. 
1b746 20 75 38 20 2a 70 4f 75 74 20 20 20 20 20 20 20   u8 *pOut       
1b747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b748 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 77 72   /* Buffer to wr
1b749 69 74 65 20 70 61 67 65 20 64 61 74 61 20 74 6f  ite page data to
1b74a 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 33 32 20 69   */..){..  u32 i
1b74b 52 65 61 64 20 3d 20 30 3b 20 20 20 20 20 20 20  Read = 0;       
1b74c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1b74d 20 21 3d 30 2c 20 57 41 4c 20 66 72 61 6d 65 20   !=0, WAL frame 
1b74e 74 6f 20 72 65 74 75 72 6e 20 64 61 74 61 20 66  to return data f
1b74f 72 6f 6d 20 2a 2f 0d 0a 20 20 75 33 32 20 69 4c  rom */..  u32 iL
1b750 61 73 74 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e  ast = pWal->hdr.
1b751 6d 78 46 72 61 6d 65 3b 20 20 2f 2a 20 4c 61 73  mxFrame;  /* Las
1b752 74 20 70 61 67 65 20 69 6e 20 57 41 4c 20 66 6f  t page in WAL fo
1b753 72 20 74 68 69 73 20 72 65 61 64 65 72 20 2a 2f  r this reader */
1b754 0d 0a 20 20 69 6e 74 20 69 48 61 73 68 3b 20 20  ..  int iHash;  
1b755 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b756 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
1b757 6f 6f 70 20 74 68 72 6f 75 67 68 20 4e 20 68 61  oop through N ha
1b758 73 68 20 74 61 62 6c 65 73 20 2a 2f 0d 0a 0d 0a  sh tables */....
1b759 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
1b75a 65 20 69 73 20 6f 6e 6c 79 20 62 65 20 63 61 6c  e is only be cal
1b75b 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
1b75c 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
1b75d 6f 6e 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  on. */..  assert
1b75e 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b  ( pWal->readLock
1b75f 3e 3d 30 20 7c 7c 20 70 57 61 6c 2d 3e 6c 6f 63  >=0 || pWal->loc
1b760 6b 45 72 72 6f 72 20 29 3b 0d 0a 0d 0a 20 20 2f  kError );....  /
1b761 2a 20 49 66 20 74 68 65 20 22 6c 61 73 74 20 70  * If the "last p
1b762 61 67 65 22 20 66 69 65 6c 64 20 6f 66 20 74 68  age" field of th
1b763 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  e wal-index head
1b764 65 72 20 73 6e 61 70 73 68 6f 74 20 69 73 20 30  er snapshot is 0
1b765 2c 20 74 68 65 6e 0d 0a 20 20 2a 2a 20 6e 6f 20  , then..  ** no 
1b766 64 61 74 61 20 77 69 6c 6c 20 62 65 20 72 65 61  data will be rea
1b767 64 20 66 72 6f 6d 20 74 68 65 20 77 61 6c 20 75  d from the wal u
1b768 6e 64 65 72 20 61 6e 79 20 63 69 72 63 75 6d 73  nder any circums
1b769 74 61 6e 63 65 73 2e 20 52 65 74 75 72 6e 20 65  tances. Return e
1b76a 61 72 6c 79 0d 0a 20 20 2a 2a 20 69 6e 20 74 68  arly..  ** in th
1b76b 69 73 20 63 61 73 65 20 61 73 20 61 6e 20 6f 70  is case as an op
1b76c 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4c 69 6b  timization.  Lik
1b76d 65 77 69 73 65 2c 20 69 66 20 70 57 61 6c 2d 3e  ewise, if pWal->
1b76e 72 65 61 64 4c 6f 63 6b 3d 3d 30 2c 20 0d 0a 20  readLock==0, .. 
1b76f 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 57 41 4c   ** then the WAL
1b770 20 69 73 20 69 67 6e 6f 72 65 64 20 62 79 20 74   is ignored by t
1b771 68 65 20 72 65 61 64 65 72 20 73 6f 20 72 65 74  he reader so ret
1b772 75 72 6e 20 65 61 72 6c 79 2c 20 61 73 20 69 66  urn early, as if
1b773 20 74 68 65 20 0d 0a 20 20 2a 2a 20 57 41 4c 20   the ..  ** WAL 
1b774 77 65 72 65 20 65 6d 70 74 79 2e 0d 0a 20 20 2a  were empty...  *
1b775 2f 0d 0a 20 20 69 66 28 20 69 4c 61 73 74 3d 3d  /..  if( iLast==
1b776 30 20 7c 7c 20 70 57 61 6c 2d 3e 72 65 61 64 4c  0 || pWal->readL
1b777 6f 63 6b 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a  ock==0 ){..    *
1b778 70 49 6e 57 61 6c 20 3d 20 30 3b 0d 0a 20 20 20  pInWal = 0;..   
1b779 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1b77a 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  K;..  }....  /* 
1b77b 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68 20  Search the hash 
1b77c 74 61 62 6c 65 20 6f 72 20 74 61 62 6c 65 73 20  table or tables 
1b77d 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6d 61 74  for an entry mat
1b77e 63 68 69 6e 67 20 70 61 67 65 20 6e 75 6d 62 65  ching page numbe
1b77f 72 0d 0a 20 20 2a 2a 20 70 67 6e 6f 2e 20 45 61  r..  ** pgno. Ea
1b780 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  ch iteration of 
1b781 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
1b782 72 28 29 20 6c 6f 6f 70 20 73 65 61 72 63 68 65  r() loop searche
1b783 73 20 6f 6e 65 0d 0a 20 20 2a 2a 20 68 61 73 68  s one..  ** hash
1b784 20 74 61 62 6c 65 20 28 65 61 63 68 20 68 61 73   table (each has
1b785 68 20 74 61 62 6c 65 20 69 6e 64 65 78 65 73 20  h table indexes 
1b786 75 70 20 74 6f 20 48 41 53 48 54 41 42 4c 45 5f  up to HASHTABLE_
1b787 4e 50 41 47 45 20 66 72 61 6d 65 73 29 2e 0d 0a  NPAGE frames)...
1b788 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69 73 20    **..  ** This 
1b789 63 6f 64 65 20 6d 69 67 68 74 20 72 75 6e 20 63  code might run c
1b78a 6f 6e 63 75 72 72 65 6e 74 6c 79 20 74 6f 20 74  oncurrently to t
1b78b 68 65 20 63 6f 64 65 20 69 6e 20 77 61 6c 49 6e  he code in walIn
1b78c 64 65 78 41 70 70 65 6e 64 28 29 0d 0a 20 20 2a  dexAppend()..  *
1b78d 2a 20 74 68 61 74 20 61 64 64 73 20 65 6e 74 72  * that adds entr
1b78e 69 65 73 20 74 6f 20 74 68 65 20 77 61 6c 2d 69  ies to the wal-i
1b78f 6e 64 65 78 20 28 61 6e 64 20 70 6f 73 73 69 62  ndex (and possib
1b790 6c 79 20 74 6f 20 74 68 69 73 20 68 61 73 68 20  ly to this hash 
1b791 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 29 2e 20 54  ..  ** table). T
1b792 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 76 61  his means the va
1b793 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 66 72  lue just read fr
1b794 6f 6d 20 74 68 65 20 68 61 73 68 20 0d 0a 20 20  om the hash ..  
1b795 2a 2a 20 73 6c 6f 74 20 28 61 48 61 73 68 5b 69  ** slot (aHash[i
1b796 4b 65 79 5d 29 20 6d 61 79 20 68 61 76 65 20 62  Key]) may have b
1b797 65 65 6e 20 61 64 64 65 64 20 62 65 66 6f 72 65  een added before
1b798 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 0d 0a   or after the ..
1b799 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 72 65 61    ** current rea
1b79a 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  d transaction wa
1b79b 73 20 6f 70 65 6e 65 64 2e 20 56 61 6c 75 65 73  s opened. Values
1b79c 20 61 64 64 65 64 20 61 66 74 65 72 20 74 68 65   added after the
1b79d 0d 0a 20 20 2a 2a 20 72 65 61 64 20 74 72 61 6e  ..  ** read tran
1b79e 73 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e  saction was open
1b79f 65 64 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e  ed may have been
1b7a0 20 77 72 69 74 74 65 6e 20 69 6e 63 6f 72 72 65   written incorre
1b7a1 63 74 6c 79 20 2d 0d 0a 20 20 2a 2a 20 69 2e 65  ctly -..  ** i.e
1b7a2 2e 20 74 68 65 73 65 20 73 6c 6f 74 73 20 6d 61  . these slots ma
1b7a3 79 20 63 6f 6e 74 61 69 6e 20 67 61 72 62 61 67  y contain garbag
1b7a4 65 20 64 61 74 61 2e 20 48 6f 77 65 76 65 72 2c  e data. However,
1b7a5 20 77 65 20 61 73 73 75 6d 65 0d 0a 20 20 2a 2a   we assume..  **
1b7a6 20 74 68 61 74 20 61 6e 79 20 73 6c 6f 74 73 20   that any slots 
1b7a7 77 72 69 74 74 65 6e 20 62 65 66 6f 72 65 20 74  written before t
1b7a8 68 65 20 63 75 72 72 65 6e 74 20 72 65 61 64 20  he current read 
1b7a9 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 0d  transaction was.
1b7aa 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 72 65 6d  .  ** opened rem
1b7ab 61 69 6e 20 75 6e 6d 6f 64 69 66 69 65 64 2e 0d  ain unmodified..
1b7ac 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 46 6f 72 20  .  **..  ** For 
1b7ad 74 68 65 20 72 65 61 73 6f 6e 73 20 61 62 6f 76  the reasons abov
1b7ae 65 2c 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 63  e, the if(...) c
1b7af 6f 6e 64 69 74 69 6f 6e 20 66 65 61 74 75 72 65  ondition feature
1b7b0 64 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0d 0a  d in the inner..
1b7b1 20 20 2a 2a 20 6c 6f 6f 70 20 6f 66 20 74 68 65    ** loop of the
1b7b2 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
1b7b3 20 69 73 20 6d 6f 72 65 20 73 74 72 69 6e 67 65   is more stringe
1b7b4 6e 74 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65  nt that would be
1b7b5 20 72 65 71 75 69 72 65 64 20 0d 0a 20 20 2a 2a   required ..  **
1b7b6 20 69 66 20 77 65 20 68 61 64 20 65 78 63 6c 75   if we had exclu
1b7b7 73 69 76 65 20 61 63 63 65 73 73 20 74 6f 20 74  sive access to t
1b7b8 68 65 20 68 61 73 68 2d 74 61 62 6c 65 3a 0d 0a  he hash-table:..
1b7b9 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 28 61 50    **..  **   (aP
1b7ba 67 6e 6f 5b 69 46 72 61 6d 65 5d 3d 3d 70 67 6e  gno[iFrame]==pgn
1b7bb 6f 29 3a 20 0d 0a 20 20 2a 2a 20 20 20 20 20 54  o): ..  **     T
1b7bc 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 66 69  his condition fi
1b7bd 6c 74 65 72 73 20 6f 75 74 20 6e 6f 72 6d 61 6c  lters out normal
1b7be 20 68 61 73 68 2d 74 61 62 6c 65 20 63 6f 6c 6c   hash-table coll
1b7bf 69 73 69 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20  isions...  **.. 
1b7c0 20 2a 2a 20 20 20 28 69 46 72 61 6d 65 3c 3d 69   **   (iFrame<=i
1b7c1 4c 61 73 74 29 3a 20 0d 0a 20 20 2a 2a 20 20 20  Last): ..  **   
1b7c2 20 20 54 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e    This condition
1b7c3 20 66 69 6c 74 65 72 73 20 6f 75 74 20 65 6e 74   filters out ent
1b7c4 72 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61  ries that were a
1b7c5 64 64 65 64 20 74 6f 20 74 68 65 20 68 61 73 68  dded to the hash
1b7c6 0d 0a 20 20 2a 2a 20 20 20 20 20 74 61 62 6c 65  ..  **     table
1b7c7 20 61 66 74 65 72 20 74 68 65 20 63 75 72 72 65   after the curre
1b7c8 6e 74 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74  nt read-transact
1b7c9 69 6f 6e 20 68 61 64 20 73 74 61 72 74 65 64 2e  ion had started.
1b7ca 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 48  ..  */..  for(iH
1b7cb 61 73 68 3d 77 61 6c 46 72 61 6d 65 50 61 67 65  ash=walFramePage
1b7cc 28 69 4c 61 73 74 29 3b 20 69 48 61 73 68 3e 3d  (iLast); iHash>=
1b7cd 30 20 26 26 20 69 52 65 61 64 3d 3d 30 3b 20 69  0 && iRead==0; i
1b7ce 48 61 73 68 2d 2d 29 7b 0d 0a 20 20 20 20 76 6f  Hash--){..    vo
1b7cf 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a  latile ht_slot *
1b7d0 61 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 50  aHash;      /* P
1b7d1 6f 69 6e 74 65 72 20 74 6f 20 68 61 73 68 20 74  ointer to hash t
1b7d2 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 76 6f 6c  able */..    vol
1b7d3 61 74 69 6c 65 20 75 33 32 20 2a 61 50 67 6e 6f  atile u32 *aPgno
1b7d4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ;          /* Po
1b7d5 69 6e 74 65 72 20 74 6f 20 61 72 72 61 79 20 6f  inter to array o
1b7d6 66 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 2a  f page numbers *
1b7d7 2f 0d 0a 20 20 20 20 75 33 32 20 69 5a 65 72 6f  /..    u32 iZero
1b7d8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1b7d9 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20 6e 75       /* Frame nu
1b7da 6d 62 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69  mber correspondi
1b7db 6e 67 20 74 6f 20 61 50 67 6e 6f 5b 30 5d 20 2a  ng to aPgno[0] *
1b7dc 2f 0d 0a 20 20 20 20 69 6e 74 20 69 4b 65 79 3b  /..    int iKey;
1b7dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b7de 20 20 20 20 20 2f 2a 20 48 61 73 68 20 73 6c 6f       /* Hash slo
1b7df 74 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 20 20  t index */..    
1b7e0 69 6e 74 20 6e 43 6f 6c 6c 69 64 65 3b 20 20 20  int nCollide;   
1b7e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b7e2 20 4e 75 6d 62 65 72 20 6f 66 20 68 61 73 68 20   Number of hash 
1b7e3 63 6f 6c 6c 69 73 69 6f 6e 73 20 72 65 6d 61 69  collisions remai
1b7e4 6e 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  ning */..    int
1b7e5 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1b7e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72             /* Er
1b7e7 72 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20  ror code */.... 
1b7e8 20 20 20 72 63 20 3d 20 77 61 6c 48 61 73 68 47     rc = walHashG
1b7e9 65 74 28 70 57 61 6c 2c 20 69 48 61 73 68 2c 20  et(pWal, iHash, 
1b7ea 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20  &aHash, &aPgno, 
1b7eb 26 69 5a 65 72 6f 29 3b 0d 0a 20 20 20 20 69 66  &iZero);..    if
1b7ec 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1b7ed 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
1b7ee 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20   rc;..    }..   
1b7ef 20 6e 43 6f 6c 6c 69 64 65 20 3d 20 48 41 53 48   nCollide = HASH
1b7f0 54 41 42 4c 45 5f 4e 53 4c 4f 54 3b 0d 0a 20 20  TABLE_NSLOT;..  
1b7f1 20 20 66 6f 72 28 69 4b 65 79 3d 77 61 6c 48 61    for(iKey=walHa
1b7f2 73 68 28 70 67 6e 6f 29 3b 20 61 48 61 73 68 5b  sh(pgno); aHash[
1b7f3 69 4b 65 79 5d 3b 20 69 4b 65 79 3d 77 61 6c 4e  iKey]; iKey=walN
1b7f4 65 78 74 48 61 73 68 28 69 4b 65 79 29 29 7b 0d  extHash(iKey)){.
1b7f5 0a 20 20 20 20 20 20 75 33 32 20 69 46 72 61 6d  .      u32 iFram
1b7f6 65 20 3d 20 61 48 61 73 68 5b 69 4b 65 79 5d 20  e = aHash[iKey] 
1b7f7 2b 20 69 5a 65 72 6f 3b 0d 0a 20 20 20 20 20 20  + iZero;..      
1b7f8 69 66 28 20 69 46 72 61 6d 65 3c 3d 69 4c 61 73  if( iFrame<=iLas
1b7f9 74 20 26 26 20 61 50 67 6e 6f 5b 61 48 61 73 68  t && aPgno[aHash
1b7fa 5b 69 4b 65 79 5d 5d 3d 3d 70 67 6e 6f 20 29 7b  [iKey]]==pgno ){
1b7fb 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 61 73 73  ..        /* ass
1b7fc 65 72 74 28 20 69 46 72 61 6d 65 3e 69 52 65 61  ert( iFrame>iRea
1b7fd 64 20 29 3b 20 2d 2d 20 6e 6f 74 20 74 72 75 65  d ); -- not true
1b7fe 20 69 66 20 74 68 65 72 65 20 69 73 20 63 6f 72   if there is cor
1b7ff 72 75 70 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20  ruption */..    
1b800 20 20 20 20 69 52 65 61 64 20 3d 20 69 46 72 61      iRead = iFra
1b801 6d 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  me;..      }..  
1b802 20 20 20 20 69 66 28 20 28 6e 43 6f 6c 6c 69 64      if( (nCollid
1b803 65 2d 2d 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  e--)==0 ){..    
1b804 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1b805 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d  E_CORRUPT_BKPT;.
1b806 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
1b807 0a 20 20 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53  .  }....#ifdef S
1b808 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50  QLITE_ENABLE_EXP
1b809 45 4e 53 49 56 45 5f 41 53 53 45 52 54 0d 0a 20  ENSIVE_ASSERT.. 
1b80a 20 2f 2a 20 49 66 20 65 78 70 65 6e 73 69 76 65   /* If expensive
1b80b 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
1b80c 65 6e 74 73 20 61 72 65 20 61 76 61 69 6c 61 62  ents are availab
1b80d 6c 65 2c 20 64 6f 20 61 20 6c 69 6e 65 61 72 20  le, do a linear 
1b80e 73 65 61 72 63 68 0d 0a 20 20 2a 2a 20 6f 66 20  search..  ** of 
1b80f 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 66 69  the wal-index fi
1b810 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 4d 61 6b 65  le content. Make
1b811 20 73 75 72 65 20 74 68 65 20 72 65 73 75 6c 74   sure the result
1b812 73 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65  s agree with the
1b813 0d 0a 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f 62  ..  ** result ob
1b814 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65  tained using the
1b815 20 68 61 73 68 20 69 6e 64 65 78 65 73 20 61 62   hash indexes ab
1b816 6f 76 65 2e 20 20 2a 2f 0d 0a 20 20 7b 0d 0a 20  ove.  */..  {.. 
1b817 20 20 20 75 33 32 20 69 52 65 61 64 32 20 3d 20     u32 iRead2 = 
1b818 30 3b 0d 0a 20 20 20 20 75 33 32 20 69 54 65 73  0;..    u32 iTes
1b819 74 3b 0d 0a 20 20 20 20 66 6f 72 28 69 54 65 73  t;..    for(iTes
1b81a 74 3d 69 4c 61 73 74 3b 20 69 54 65 73 74 3e 30  t=iLast; iTest>0
1b81b 3b 20 69 54 65 73 74 2d 2d 29 7b 0d 0a 20 20 20  ; iTest--){..   
1b81c 20 20 20 69 66 28 20 77 61 6c 46 72 61 6d 65 50     if( walFrameP
1b81d 67 6e 6f 28 70 57 61 6c 2c 20 69 54 65 73 74 29  gno(pWal, iTest)
1b81e 3d 3d 70 67 6e 6f 20 29 7b 0d 0a 20 20 20 20 20  ==pgno ){..     
1b81f 20 20 20 69 52 65 61 64 32 20 3d 20 69 54 65 73     iRead2 = iTes
1b820 74 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  t;..        brea
1b821 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
1b822 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
1b823 69 52 65 61 64 3d 3d 69 52 65 61 64 32 20 29 3b  iRead==iRead2 );
1b824 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
1b825 0a 20 20 2f 2a 20 49 66 20 69 52 65 61 64 20 69  .  /* If iRead i
1b826 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1b827 20 69 74 20 69 73 20 74 68 65 20 6c 6f 67 20 66   it is the log f
1b828 72 61 6d 65 20 6e 75 6d 62 65 72 20 74 68 61 74  rame number that
1b829 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0d 0a 20   contains the.. 
1b82a 20 2a 2a 20 72 65 71 75 69 72 65 64 20 70 61 67   ** required pag
1b82b 65 2e 20 52 65 61 64 20 61 6e 64 20 72 65 74 75  e. Read and retu
1b82c 72 6e 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  rn data from the
1b82d 20 6c 6f 67 20 66 69 6c 65 2e 0d 0a 20 20 2a 2f   log file...  */
1b82e 0d 0a 20 20 69 66 28 20 69 52 65 61 64 20 29 7b  ..  if( iRead ){
1b82f 0d 0a 20 20 20 20 69 6e 74 20 73 7a 3b 0d 0a 20  ..    int sz;.. 
1b830 20 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 0d     i64 iOffset;.
1b831 0a 20 20 20 20 73 7a 20 3d 20 70 57 61 6c 2d 3e  .    sz = pWal->
1b832 68 64 72 2e 73 7a 50 61 67 65 3b 0d 0a 20 20 20  hdr.szPage;..   
1b833 20 73 7a 20 3d 20 28 73 7a 26 30 78 66 65 30 30   sz = (sz&0xfe00
1b834 29 20 2b 20 28 28 73 7a 26 30 78 30 30 30 31 29  ) + ((sz&0x0001)
1b835 3c 3c 31 36 29 3b 0d 0a 20 20 20 20 74 65 73 74  <<16);..    test
1b836 63 61 73 65 28 20 73 7a 3c 3d 33 32 37 36 38 20  case( sz<=32768 
1b837 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65  );..    testcase
1b838 28 20 73 7a 3e 3d 36 35 35 33 36 20 29 3b 0d 0a  ( sz>=65536 );..
1b839 20 20 20 20 69 4f 66 66 73 65 74 20 3d 20 77 61      iOffset = wa
1b83a 6c 46 72 61 6d 65 4f 66 66 73 65 74 28 69 52 65  lFrameOffset(iRe
1b83b 61 64 2c 20 73 7a 29 20 2b 20 57 41 4c 5f 46 52  ad, sz) + WAL_FR
1b83c 41 4d 45 5f 48 44 52 53 49 5a 45 3b 0d 0a 20 20  AME_HDRSIZE;..  
1b83d 20 20 2a 70 49 6e 57 61 6c 20 3d 20 31 3b 0d 0a    *pInWal = 1;..
1b83e 20 20 20 20 2f 2a 20 74 65 73 74 63 61 73 65 28      /* testcase(
1b83f 20 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f 66 66   IS_BIG_INT(iOff
1b840 73 65 74 29 20 29 3b 20 2f 2f 20 72 65 71 75 69  set) ); // requi
1b841 72 65 73 20 61 20 34 47 69 42 20 57 41 4c 20 2a  res a 4GiB WAL *
1b842 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  /..    return sq
1b843 6c 69 74 65 33 4f 73 52 65 61 64 28 70 57 61 6c  lite3OsRead(pWal
1b844 2d 3e 70 57 61 6c 46 64 2c 20 70 4f 75 74 2c 20  ->pWalFd, pOut, 
1b845 6e 4f 75 74 2c 20 69 4f 66 66 73 65 74 29 3b 0d  nOut, iOffset);.
1b846 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 49 6e 57 61  .  }....  *pInWa
1b847 6c 20 3d 20 30 3b 0d 0a 20 20 72 65 74 75 72 6e  l = 0;..  return
1b848 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
1b849 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 52 65 74 75  ..../* ..** Retu
1b84a 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  rn the size of t
1b84b 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 70  he database in p
1b84c 61 67 65 73 20 28 6f 72 20 7a 65 72 6f 2c 20 69  ages (or zero, i
1b84d 66 20 75 6e 6b 6e 6f 77 6e 29 2e 0d 0a 2a 2f 0d  f unknown)...*/.
1b84e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b84f 50 67 6e 6f 20 73 71 6c 69 74 65 33 57 61 6c 44  Pgno sqlite3WalD
1b850 62 73 69 7a 65 28 57 61 6c 20 2a 70 57 61 6c 29  bsize(Wal *pWal)
1b851 7b 0d 0a 20 20 69 66 28 20 70 57 61 6c 20 26 26  {..  if( pWal &&
1b852 20 41 4c 57 41 59 53 28 70 57 61 6c 2d 3e 72 65   ALWAYS(pWal->re
1b853 61 64 4c 6f 63 6b 3e 3d 30 29 20 29 7b 0d 0a 20  adLock>=0) ){.. 
1b854 20 20 20 72 65 74 75 72 6e 20 70 57 61 6c 2d 3e     return pWal->
1b855 68 64 72 2e 6e 50 61 67 65 3b 0d 0a 20 20 7d 0d  hdr.nPage;..  }.
1b856 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d  .  return 0;..}.
1b857 0a 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 69  ...../* ..** Thi
1b858 73 20 66 75 6e 63 74 69 6f 6e 20 73 74 61 72 74  s function start
1b859 73 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61  s a write transa
1b85a 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 57 41 4c  ction on the WAL
1b85b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 72 65 61 64  ...**..** A read
1b85c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
1b85d 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
1b85e 65 65 6e 20 73 74 61 72 74 65 64 20 62 79 20 61  een started by a
1b85f 20 70 72 69 6f 72 20 63 61 6c 6c 0d 0a 2a 2a 20   prior call..** 
1b860 74 6f 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67  to sqlite3WalBeg
1b861 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f  inReadTransactio
1b862 6e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  n()...**..** If 
1b863 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 6f  another thread o
1b864 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 77 72  r process has wr
1b865 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64  itten into the d
1b866 61 74 61 62 61 73 65 20 73 69 6e 63 65 0d 0a 2a  atabase since..*
1b867 2a 20 74 68 65 20 72 65 61 64 20 74 72 61 6e 73  * the read trans
1b868 61 63 74 69 6f 6e 20 77 61 73 20 73 74 61 72 74  action was start
1b869 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e  ed, then it is n
1b86a 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
1b86b 74 68 69 73 0d 0a 2a 2a 20 74 68 72 65 61 64 20  this..** thread 
1b86c 74 6f 20 77 72 69 74 65 20 61 73 20 64 6f 69 6e  to write as doin
1b86d 67 20 73 6f 20 77 6f 75 6c 64 20 63 61 75 73 65  g so would cause
1b86e 20 61 20 66 6f 72 6b 2e 20 20 53 6f 20 74 68 69   a fork.  So thi
1b86f 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 72 65  s routine..** re
1b870 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 42 55 53  turns SQLITE_BUS
1b871 59 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 61  Y in that case a
1b872 6e 64 20 6e 6f 20 77 72 69 74 65 20 74 72 61 6e  nd no write tran
1b873 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1b874 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72  ed...**..** Ther
1b875 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20  e can only be a 
1b876 73 69 6e 67 6c 65 20 77 72 69 74 65 72 20 61 63  single writer ac
1b877 74 69 76 65 20 61 74 20 61 20 74 69 6d 65 2e 0d  tive at a time..
1b878 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1b879 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1b87a 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e  alBeginWriteTran
1b87b 73 61 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61  saction(Wal *pWa
1b87c 6c 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  l){..  int rc;..
1b87d 0d 0a 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 73 74  ..  /* Cannot st
1b87e 61 72 74 20 61 20 77 72 69 74 65 20 74 72 61 6e  art a write tran
1b87f 73 61 63 74 69 6f 6e 20 77 69 74 68 6f 75 74 20  saction without 
1b880 66 69 72 73 74 20 68 6f 6c 64 69 6e 67 20 61 20  first holding a 
1b881 72 65 61 64 0d 0a 20 20 2a 2a 20 74 72 61 6e 73  read..  ** trans
1b882 61 63 74 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 61 73  action. */..  as
1b883 73 65 72 74 28 20 70 57 61 6c 2d 3e 72 65 61 64  sert( pWal->read
1b884 4c 6f 63 6b 3e 3d 30 20 29 3b 0d 0a 0d 0a 20 20  Lock>=0 );....  
1b885 69 66 28 20 70 57 61 6c 2d 3e 72 65 61 64 4f 6e  if( pWal->readOn
1b886 6c 79 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ly ){..    retur
1b887 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
1b888 59 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  Y;..  }....  /* 
1b889 4f 6e 6c 79 20 6f 6e 65 20 77 72 69 74 65 72 20  Only one writer 
1b88a 61 6c 6c 6f 77 65 64 20 61 74 20 61 20 74 69 6d  allowed at a tim
1b88b 65 2e 20 20 47 65 74 20 74 68 65 20 77 72 69 74  e.  Get the writ
1b88c 65 20 6c 6f 63 6b 2e 20 20 52 65 74 75 72 6e 0d  e lock.  Return.
1b88d 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  .  ** SQLITE_BUS
1b88e 59 20 69 66 20 75 6e 61 62 6c 65 2e 0d 0a 20 20  Y if unable...  
1b88f 2a 2f 0d 0a 20 20 72 63 20 3d 20 77 61 6c 4c 6f  */..  rc = walLo
1b890 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c  ckExclusive(pWal
1b891 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b  , WAL_WRITE_LOCK
1b892 2c 20 31 29 3b 0d 0a 20 20 69 66 28 20 72 63 20  , 1);..  if( rc 
1b893 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ){..    return r
1b894 63 3b 0d 0a 20 20 7d 0d 0a 20 20 70 57 61 6c 2d  c;..  }..  pWal-
1b895 3e 77 72 69 74 65 4c 6f 63 6b 20 3d 20 31 3b 0d  >writeLock = 1;.
1b896 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68  ...  /* If anoth
1b897 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  er connection ha
1b898 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  s written to the
1b899 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73   database file s
1b89a 69 6e 63 65 20 74 68 65 0d 0a 20 20 2a 2a 20 74  ince the..  ** t
1b89b 69 6d 65 20 74 68 65 20 72 65 61 64 20 74 72 61  ime the read tra
1b89c 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73  nsaction on this
1b89d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20   connection was 
1b89e 73 74 61 72 74 65 64 2c 20 74 68 65 6e 0d 0a 20  started, then.. 
1b89f 20 2a 2a 20 74 68 65 20 77 72 69 74 65 20 69 73   ** the write is
1b8a0 20 64 69 73 61 6c 6c 6f 77 65 64 2e 0d 0a 20 20   disallowed...  
1b8a1 2a 2f 0d 0a 20 20 69 66 28 20 6d 65 6d 63 6d 70  */..  if( memcmp
1b8a2 28 26 70 57 61 6c 2d 3e 68 64 72 2c 20 28 76 6f  (&pWal->hdr, (vo
1b8a3 69 64 20 2a 29 77 61 6c 49 6e 64 65 78 48 64 72  id *)walIndexHdr
1b8a4 28 70 57 61 6c 29 2c 20 73 69 7a 65 6f 66 28 57  (pWal), sizeof(W
1b8a5 61 6c 49 6e 64 65 78 48 64 72 29 29 21 3d 30 20  alIndexHdr))!=0 
1b8a6 29 7b 0d 0a 20 20 20 20 77 61 6c 55 6e 6c 6f 63  ){..    walUnloc
1b8a7 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c  kExclusive(pWal,
1b8a8 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b 2c   WAL_WRITE_LOCK,
1b8a9 20 31 29 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e   1);..    pWal->
1b8aa 77 72 69 74 65 4c 6f 63 6b 20 3d 20 30 3b 0d 0a  writeLock = 0;..
1b8ab 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1b8ac 42 55 53 59 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  BUSY;..  }....  
1b8ad 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
1b8ae 0a 2f 2a 0d 0a 2a 2a 20 45 6e 64 20 61 20 77 72  ./*..** End a wr
1b8af 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ite transaction.
1b8b0 20 20 54 68 65 20 63 6f 6d 6d 69 74 20 68 61 73    The commit has
1b8b1 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 6f   already been do
1b8b2 6e 65 2e 20 20 54 68 69 73 0d 0a 2a 2a 20 72 6f  ne.  This..** ro
1b8b3 75 74 69 6e 65 20 6d 65 72 65 6c 79 20 72 65 6c  utine merely rel
1b8b4 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0d  eases the lock..
1b8b5 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1b8b6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1b8b7 61 6c 45 6e 64 57 72 69 74 65 54 72 61 6e 73 61  alEndWriteTransa
1b8b8 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29  ction(Wal *pWal)
1b8b9 7b 0d 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e 77  {..  if( pWal->w
1b8ba 72 69 74 65 4c 6f 63 6b 20 29 7b 0d 0a 20 20 20  riteLock ){..   
1b8bb 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73   walUnlockExclus
1b8bc 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 57 52  ive(pWal, WAL_WR
1b8bd 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0d 0a 20  ITE_LOCK, 1);.. 
1b8be 20 20 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f     pWal->writeLo
1b8bf 63 6b 20 3d 20 30 3b 0d 0a 20 20 20 20 70 57 61  ck = 0;..    pWa
1b8c0 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e 43 6f 6d  l->truncateOnCom
1b8c1 6d 69 74 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  mit = 0;..  }.. 
1b8c2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1b8c3 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
1b8c4 49 66 20 61 6e 79 20 64 61 74 61 20 68 61 73 20  If any data has 
1b8c5 62 65 65 6e 20 77 72 69 74 74 65 6e 20 28 62 75  been written (bu
1b8c6 74 20 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29  t not committed)
1b8c7 20 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65   to the log file
1b8c8 2c 20 74 68 69 73 0d 0a 2a 2a 20 66 75 6e 63 74  , this..** funct
1b8c9 69 6f 6e 20 6d 6f 76 65 73 20 74 68 65 20 77 72  ion moves the wr
1b8ca 69 74 65 2d 70 6f 69 6e 74 65 72 20 62 61 63 6b  ite-pointer back
1b8cb 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1b8cc 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1b8cd 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 64 64 69 74 69  ...**..** Additi
1b8ce 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 63 61 6c 6c  onally, the call
1b8cf 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73  back function is
1b8d0 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 65 61 63   invoked for eac
1b8d1 68 20 66 72 61 6d 65 20 77 72 69 74 74 65 6e 0d  h frame written.
1b8d2 0a 2a 2a 20 74 6f 20 74 68 65 20 57 41 4c 20 73  .** to the WAL s
1b8d3 69 6e 63 65 20 74 68 65 20 73 74 61 72 74 20 6f  ince the start o
1b8d4 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
1b8d5 6e 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61  n. If the callba
1b8d6 63 6b 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 6f  ck returns..** o
1b8d7 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
1b8d8 5f 4f 4b 2c 20 69 74 20 69 73 20 6e 6f 74 20 69  _OK, it is not i
1b8d9 6e 76 6f 6b 65 64 20 61 67 61 69 6e 20 61 6e 64  nvoked again and
1b8da 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1b8db 69 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  is..** returned 
1b8dc 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a  to the caller...
1b8dd 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  **..** Otherwise
1b8de 2c 20 69 66 20 74 68 65 20 63 61 6c 6c 62 61 63  , if the callbac
1b8df 6b 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  k function does 
1b8e0 6e 6f 74 20 72 65 74 75 72 6e 20 61 6e 20 65 72  not return an er
1b8e1 72 6f 72 2c 20 74 68 69 73 0d 0a 2a 2a 20 66 75  ror, this..** fu
1b8e2 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
1b8e3 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 53  QLITE_OK...*/..S
1b8e4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1b8e5 74 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f  t sqlite3WalUndo
1b8e6 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20  (Wal *pWal, int 
1b8e7 28 2a 78 55 6e 64 6f 29 28 76 6f 69 64 20 2a 2c  (*xUndo)(void *,
1b8e8 20 50 67 6e 6f 29 2c 20 76 6f 69 64 20 2a 70 55   Pgno), void *pU
1b8e9 6e 64 6f 43 74 78 29 7b 0d 0a 20 20 69 6e 74 20  ndoCtx){..  int 
1b8ea 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
1b8eb 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 57  .  if( ALWAYS(pW
1b8ec 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 29 20 29  al->writeLock) )
1b8ed 7b 0d 0a 20 20 20 20 50 67 6e 6f 20 69 4d 61 78  {..    Pgno iMax
1b8ee 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46   = pWal->hdr.mxF
1b8ef 72 61 6d 65 3b 0d 0a 20 20 20 20 50 67 6e 6f 20  rame;..    Pgno 
1b8f0 69 46 72 61 6d 65 3b 0d 0a 20 20 0d 0a 20 20 20  iFrame;..  ..   
1b8f1 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20   /* Restore the 
1b8f2 63 6c 69 65 6e 74 73 20 63 61 63 68 65 20 6f 66  clients cache of
1b8f3 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68   the wal-index h
1b8f4 65 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 61  eader to the sta
1b8f5 74 65 20 69 74 0d 0a 20 20 20 20 2a 2a 20 77 61  te it..    ** wa
1b8f6 73 20 69 6e 20 62 65 66 6f 72 65 20 74 68 65 20  s in before the 
1b8f7 63 6c 69 65 6e 74 20 62 65 67 61 6e 20 77 72 69  client began wri
1b8f8 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  ting to the data
1b8f9 62 61 73 65 2e 20 0d 0a 20 20 20 20 2a 2f 0d 0a  base. ..    */..
1b8fa 20 20 20 20 6d 65 6d 63 70 79 28 26 70 57 61 6c      memcpy(&pWal
1b8fb 2d 3e 68 64 72 2c 20 28 76 6f 69 64 20 2a 29 77  ->hdr, (void *)w
1b8fc 61 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29  alIndexHdr(pWal)
1b8fd 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e 64 65  , sizeof(WalInde
1b8fe 78 48 64 72 29 29 3b 0d 0a 0d 0a 20 20 20 20 66  xHdr));....    f
1b8ff 6f 72 28 69 46 72 61 6d 65 3d 70 57 61 6c 2d 3e  or(iFrame=pWal->
1b900 68 64 72 2e 6d 78 46 72 61 6d 65 2b 31 3b 20 0d  hdr.mxFrame+1; .
1b901 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 28  .        ALWAYS(
1b902 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26  rc==SQLITE_OK) &
1b903 26 20 69 46 72 61 6d 65 3c 3d 69 4d 61 78 3b 20  & iFrame<=iMax; 
1b904 0d 0a 20 20 20 20 20 20 20 20 69 46 72 61 6d 65  ..        iFrame
1b905 2b 2b 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20  ++..    ){..    
1b906 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 63    /* This call c
1b907 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 55 6e 6c 65  annot fail. Unle
1b908 73 73 20 74 68 65 20 70 61 67 65 20 66 6f 72 20  ss the page for 
1b909 77 68 69 63 68 20 74 68 65 20 70 61 67 65 20 6e  which the page n
1b90a 75 6d 62 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20  umber..      ** 
1b90b 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
1b90c 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1b90d 20 69 73 20 28 61 29 20 69 6e 20 74 68 65 20 63   is (a) in the c
1b90e 61 63 68 65 20 61 6e 64 20 0d 0a 20 20 20 20 20  ache and ..     
1b90f 20 2a 2a 20 28 62 29 20 68 61 73 20 61 6e 20 6f   ** (b) has an o
1b910 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
1b911 65 6e 63 65 2c 20 74 68 65 6e 20 78 55 6e 64 6f  ence, then xUndo
1b912 20 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f 2d   is either a no-
1b913 6f 70 0d 0a 20 20 20 20 20 20 2a 2a 20 28 69 66  op..      ** (if
1b914 20 28 61 29 20 69 73 20 66 61 6c 73 65 29 20 6f   (a) is false) o
1b915 72 20 73 69 6d 70 6c 79 20 65 78 70 65 6c 73 20  r simply expels 
1b916 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  the page from th
1b917 65 20 63 61 63 68 65 20 28 69 66 20 28 62 29 0d  e cache (if (b).
1b918 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 66 61 6c  .      ** is fal
1b919 73 65 29 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a  se)...      **..
1b91a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
1b91b 75 70 70 65 72 20 6c 61 79 65 72 20 69 73 20 64  upper layer is d
1b91c 6f 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c  oing a rollback,
1b91d 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
1b91e 64 20 74 68 61 74 20 74 68 65 72 65 0d 0a 20 20  d that there..  
1b91f 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 20 6f 75      ** are no ou
1b920 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65  tstanding refere
1b921 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65  nces to any page
1b922 20 6f 74 68 65 72 20 74 68 61 6e 20 70 61 67 65   other than page
1b923 20 31 2e 20 41 6e 64 0d 0a 20 20 20 20 20 20 2a   1. And..      *
1b924 2a 20 70 61 67 65 20 31 20 69 73 20 6e 65 76 65  * page 1 is neve
1b925 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  r written to the
1b926 20 6c 6f 67 20 75 6e 74 69 6c 20 74 68 65 20 74   log until the t
1b927 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0d 0a 20  ransaction is.. 
1b928 20 20 20 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65       ** committe
1b929 64 2e 20 41 73 20 61 20 72 65 73 75 6c 74 2c 20  d. As a result, 
1b92a 74 68 65 20 63 61 6c 6c 20 74 6f 20 78 55 6e 64  the call to xUnd
1b92b 6f 20 6d 61 79 20 6e 6f 74 20 66 61 69 6c 2e 0d  o may not fail..
1b92c 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
1b92d 20 61 73 73 65 72 74 28 20 77 61 6c 46 72 61 6d   assert( walFram
1b92e 65 50 67 6e 6f 28 70 57 61 6c 2c 20 69 46 72 61  ePgno(pWal, iFra
1b92f 6d 65 29 21 3d 31 20 29 3b 0d 0a 20 20 20 20 20  me)!=1 );..     
1b930 20 72 63 20 3d 20 78 55 6e 64 6f 28 70 55 6e 64   rc = xUndo(pUnd
1b931 6f 43 74 78 2c 20 77 61 6c 46 72 61 6d 65 50 67  oCtx, walFramePg
1b932 6e 6f 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 29  no(pWal, iFrame)
1b933 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 77  );..    }..    w
1b934 61 6c 43 6c 65 61 6e 75 70 48 61 73 68 28 70 57  alCleanupHash(pW
1b935 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73  al);..  }..  ass
1b936 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1b937 4f 4b 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  OK );..  return 
1b938 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  rc;..}..../* ..*
1b939 2a 20 41 72 67 75 6d 65 6e 74 20 61 57 61 6c 44  * Argument aWalD
1b93a 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  ata must point t
1b93b 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 57 41  o an array of WA
1b93c 4c 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54  L_SAVEPOINT_NDAT
1b93d 41 20 75 33 32 20 0d 0a 2a 2a 20 76 61 6c 75 65  A u32 ..** value
1b93e 73 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  s. This function
1b93f 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65 20 61   populates the a
1b940 72 72 61 79 20 77 69 74 68 20 76 61 6c 75 65 73  rray with values
1b941 20 72 65 71 75 69 72 65 64 20 74 6f 20 0d 0a 2a   required to ..*
1b942 2a 20 22 72 6f 6c 6c 62 61 63 6b 22 20 74 68 65  * "rollback" the
1b943 20 77 72 69 74 65 20 70 6f 73 69 74 69 6f 6e 20   write position 
1b944 6f 66 20 74 68 65 20 57 41 4c 20 68 61 6e 64 6c  of the WAL handl
1b945 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 63 75  e back to the cu
1b946 72 72 65 6e 74 20 0d 0a 2a 2a 20 70 6f 69 6e 74  rrent ..** point
1b947 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66   in the event of
1b948 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c   a savepoint rol
1b949 6c 62 61 63 6b 20 28 76 69 61 20 57 61 6c 53 61  lback (via WalSa
1b94a 76 65 70 6f 69 6e 74 55 6e 64 6f 28 29 29 2e 0d  vepointUndo())..
1b94b 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1b94c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1b94d 57 61 6c 53 61 76 65 70 6f 69 6e 74 28 57 61 6c  WalSavepoint(Wal
1b94e 20 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61   *pWal, u32 *aWa
1b94f 6c 44 61 74 61 29 7b 0d 0a 20 20 61 73 73 65 72  lData){..  asser
1b950 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f  t( pWal->writeLo
1b951 63 6b 20 29 3b 0d 0a 20 20 61 57 61 6c 44 61 74  ck );..  aWalDat
1b952 61 5b 30 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72  a[0] = pWal->hdr
1b953 2e 6d 78 46 72 61 6d 65 3b 0d 0a 20 20 61 57 61  .mxFrame;..  aWa
1b954 6c 44 61 74 61 5b 31 5d 20 3d 20 70 57 61 6c 2d  lData[1] = pWal-
1b955 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d  >hdr.aFrameCksum
1b956 5b 30 5d 3b 0d 0a 20 20 61 57 61 6c 44 61 74 61  [0];..  aWalData
1b957 5b 32 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e  [2] = pWal->hdr.
1b958 61 46 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 3b 0d  aFrameCksum[1];.
1b959 0a 20 20 61 57 61 6c 44 61 74 61 5b 33 5d 20 3d  .  aWalData[3] =
1b95a 20 70 57 61 6c 2d 3e 6e 43 6b 70 74 3b 0d 0a 7d   pWal->nCkpt;..}
1b95b 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 4d 6f 76 65  ..../* ..** Move
1b95c 20 74 68 65 20 77 72 69 74 65 20 70 6f 73 69 74   the write posit
1b95d 69 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 62  ion of the WAL b
1b95e 61 63 6b 20 74 6f 20 74 68 65 20 70 6f 69 6e 74  ack to the point
1b95f 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 0d 0a   identified by..
1b960 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  ** the values in
1b961 20 74 68 65 20 61 57 61 6c 44 61 74 61 5b 5d 20   the aWalData[] 
1b962 61 72 72 61 79 2e 20 61 57 61 6c 44 61 74 61 20  array. aWalData 
1b963 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e  must point to an
1b964 20 61 72 72 61 79 0d 0a 2a 2a 20 6f 66 20 57 41   array..** of WA
1b965 4c 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54  L_SAVEPOINT_NDAT
1b966 41 20 75 33 32 20 76 61 6c 75 65 73 20 74 68 61  A u32 values tha
1b967 74 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69  t has been previ
1b968 6f 75 73 6c 79 20 70 6f 70 75 6c 61 74 65 64 0d  ously populated.
1b969 0a 2a 2a 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  .** by a call to
1b96a 20 57 61 6c 53 61 76 65 70 6f 69 6e 74 28 29 2e   WalSavepoint().
1b96b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1b96c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b96d 57 61 6c 53 61 76 65 70 6f 69 6e 74 55 6e 64 6f  WalSavepointUndo
1b96e 28 57 61 6c 20 2a 70 57 61 6c 2c 20 75 33 32 20  (Wal *pWal, u32 
1b96f 2a 61 57 61 6c 44 61 74 61 29 7b 0d 0a 20 20 69  *aWalData){..  i
1b970 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1b971 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  K;....  assert( 
1b972 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20  pWal->writeLock 
1b973 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 57  );..  assert( aW
1b974 61 6c 44 61 74 61 5b 33 5d 21 3d 70 57 61 6c 2d  alData[3]!=pWal-
1b975 3e 6e 43 6b 70 74 20 7c 7c 20 61 57 61 6c 44 61  >nCkpt || aWalDa
1b976 74 61 5b 30 5d 3c 3d 70 57 61 6c 2d 3e 68 64 72  ta[0]<=pWal->hdr
1b977 2e 6d 78 46 72 61 6d 65 20 29 3b 0d 0a 0d 0a 20  .mxFrame );.... 
1b978 20 69 66 28 20 61 57 61 6c 44 61 74 61 5b 33 5d   if( aWalData[3]
1b979 21 3d 70 57 61 6c 2d 3e 6e 43 6b 70 74 20 29 7b  !=pWal->nCkpt ){
1b97a 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73 61  ..    /* This sa
1b97b 76 65 70 6f 69 6e 74 20 77 61 73 20 6f 70 65 6e  vepoint was open
1b97c 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ed immediately a
1b97d 66 74 65 72 20 74 68 65 20 77 72 69 74 65 2d 74  fter the write-t
1b97e 72 61 6e 73 61 63 74 69 6f 6e 0d 0a 20 20 20 20  ransaction..    
1b97f 2a 2a 20 77 61 73 20 73 74 61 72 74 65 64 2e 20  ** was started. 
1b980 52 69 67 68 74 20 61 66 74 65 72 20 74 68 61 74  Right after that
1b981 2c 20 74 68 65 20 77 72 69 74 65 72 20 64 65 63  , the writer dec
1b982 69 64 65 64 20 74 6f 20 77 72 61 70 20 61 72 6f  ided to wrap aro
1b983 75 6e 64 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 74  und..    ** to t
1b984 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
1b985 6c 6f 67 2e 20 55 70 64 61 74 65 20 74 68 65 20  log. Update the 
1b986 73 61 76 65 70 6f 69 6e 74 20 76 61 6c 75 65 73  savepoint values
1b987 20 74 6f 20 6d 61 74 63 68 2e 0d 0a 20 20 20 20   to match...    
1b988 2a 2f 0d 0a 20 20 20 20 61 57 61 6c 44 61 74 61  */..    aWalData
1b989 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 61 57  [0] = 0;..    aW
1b98a 61 6c 44 61 74 61 5b 33 5d 20 3d 20 70 57 61 6c  alData[3] = pWal
1b98b 2d 3e 6e 43 6b 70 74 3b 0d 0a 20 20 7d 0d 0a 0d  ->nCkpt;..  }...
1b98c 0a 20 20 69 66 28 20 61 57 61 6c 44 61 74 61 5b  .  if( aWalData[
1b98d 30 5d 3c 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46  0]<pWal->hdr.mxF
1b98e 72 61 6d 65 20 29 7b 0d 0a 20 20 20 20 70 57 61  rame ){..    pWa
1b98f 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 3d  l->hdr.mxFrame =
1b990 20 61 57 61 6c 44 61 74 61 5b 30 5d 3b 0d 0a 20   aWalData[0];.. 
1b991 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72     pWal->hdr.aFr
1b992 61 6d 65 43 6b 73 75 6d 5b 30 5d 20 3d 20 61 57  ameCksum[0] = aW
1b993 61 6c 44 61 74 61 5b 31 5d 3b 0d 0a 20 20 20 20  alData[1];..    
1b994 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65  pWal->hdr.aFrame
1b995 43 6b 73 75 6d 5b 31 5d 20 3d 20 61 57 61 6c 44  Cksum[1] = aWalD
1b996 61 74 61 5b 32 5d 3b 0d 0a 20 20 20 20 77 61 6c  ata[2];..    wal
1b997 43 6c 65 61 6e 75 70 48 61 73 68 28 70 57 61 6c  CleanupHash(pWal
1b998 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  );..  }....  ret
1b999 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  urn rc;..}......
1b99a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
1b99b 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 6a  tion is called j
1b99c 75 73 74 20 62 65 66 6f 72 65 20 77 72 69 74 69  ust before writi
1b99d 6e 67 20 61 20 73 65 74 20 6f 66 20 66 72 61 6d  ng a set of fram
1b99e 65 73 20 74 6f 20 74 68 65 20 6c 6f 67 0d 0a 2a  es to the log..*
1b99f 2a 20 66 69 6c 65 20 28 73 65 65 20 73 71 6c 69  * file (see sqli
1b9a0 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 29 2e  te3WalFrames()).
1b9a1 20 49 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65   It checks to se
1b9a2 65 20 69 66 2c 20 69 6e 73 74 65 61 64 20 6f 66  e if, instead of
1b9a3 20 61 70 70 65 6e 64 69 6e 67 0d 0a 2a 2a 20 74   appending..** t
1b9a4 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  o the current lo
1b9a5 67 20 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f  g file, it is po
1b9a6 73 73 69 62 6c 65 20 74 6f 20 6f 76 65 72 77 72  ssible to overwr
1b9a7 69 74 65 20 74 68 65 20 73 74 61 72 74 20 6f 66  ite the start of
1b9a8 20 74 68 65 0d 0a 2a 2a 20 65 78 69 73 74 69 6e   the..** existin
1b9a9 67 20 6c 6f 67 20 66 69 6c 65 20 77 69 74 68 20  g log file with 
1b9aa 74 68 65 20 6e 65 77 20 66 72 61 6d 65 73 20 28  the new frames (
1b9ab 69 2e 65 2e 20 22 72 65 73 65 74 22 20 74 68 65  i.e. "reset" the
1b9ac 20 6c 6f 67 29 2e 20 49 66 20 73 6f 2c 0d 0a 2a   log). If so,..*
1b9ad 2a 20 69 74 20 73 65 74 73 20 70 57 61 6c 2d 3e  * it sets pWal->
1b9ae 68 64 72 2e 6d 78 46 72 61 6d 65 20 74 6f 20 30  hdr.mxFrame to 0
1b9af 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 70 57 61  . Otherwise, pWa
1b9b0 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 69  l->hdr.mxFrame i
1b9b1 73 20 6c 65 66 74 0d 0a 2a 2a 20 75 6e 63 68 61  s left..** uncha
1b9b2 6e 67 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51  nged...**..** SQ
1b9b3 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
1b9b4 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20  ned if no error 
1b9b5 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28  is encountered (
1b9b6 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
1b9b7 65 74 68 65 72 0d 0a 2a 2a 20 6f 72 20 6e 6f 74  ether..** or not
1b9b8 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61   pWal->hdr.mxFra
1b9b9 6d 65 20 69 73 20 6d 6f 64 69 66 69 65 64 29 2e  me is modified).
1b9ba 20 41 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   An SQLite error
1b9bb 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
1b9bc 64 0d 0a 2a 2a 20 69 66 20 61 6e 20 65 72 72 6f  d..** if an erro
1b9bd 72 20 6f 63 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 73  r occurs...*/..s
1b9be 74 61 74 69 63 20 69 6e 74 20 77 61 6c 52 65 73  tatic int walRes
1b9bf 74 61 72 74 4c 6f 67 28 57 61 6c 20 2a 70 57 61  tartLog(Wal *pWa
1b9c0 6c 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  l){..  int rc = 
1b9c1 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e  SQLITE_OK;..  in
1b9c2 74 20 63 6e 74 3b 0d 0a 0d 0a 20 20 69 66 28 20  t cnt;....  if( 
1b9c3 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3d 3d  pWal->readLock==
1b9c4 30 20 29 7b 0d 0a 20 20 20 20 76 6f 6c 61 74 69  0 ){..    volati
1b9c5 6c 65 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a  le WalCkptInfo *
1b9c6 70 49 6e 66 6f 20 3d 20 77 61 6c 43 6b 70 74 49  pInfo = walCkptI
1b9c7 6e 66 6f 28 70 57 61 6c 29 3b 0d 0a 20 20 20 20  nfo(pWal);..    
1b9c8 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e  assert( pInfo->n
1b9c9 42 61 63 6b 66 69 6c 6c 3d 3d 70 57 61 6c 2d 3e  Backfill==pWal->
1b9ca 68 64 72 2e 6d 78 46 72 61 6d 65 20 29 3b 0d 0a  hdr.mxFrame );..
1b9cb 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e      if( pInfo->n
1b9cc 42 61 63 6b 66 69 6c 6c 3e 30 20 29 7b 0d 0a 20  Backfill>0 ){.. 
1b9cd 20 20 20 20 20 75 33 32 20 73 61 6c 74 31 3b 0d       u32 salt1;.
1b9ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
1b9cf 61 6e 64 6f 6d 6e 65 73 73 28 34 2c 20 26 73 61  andomness(4, &sa
1b9d0 6c 74 31 29 3b 0d 0a 20 20 20 20 20 20 72 63 20  lt1);..      rc 
1b9d1 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69  = walLockExclusi
1b9d2 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41  ve(pWal, WAL_REA
1b9d3 44 5f 4c 4f 43 4b 28 31 29 2c 20 57 41 4c 5f 4e  D_LOCK(1), WAL_N
1b9d4 52 45 41 44 45 52 2d 31 29 3b 0d 0a 20 20 20 20  READER-1);..    
1b9d5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1b9d6 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
1b9d7 2f 2a 20 49 66 20 61 6c 6c 20 72 65 61 64 65 72  /* If all reader
1b9d8 73 20 61 72 65 20 75 73 69 6e 67 20 57 41 4c 5f  s are using WAL_
1b9d9 52 45 41 44 5f 4c 4f 43 4b 28 30 29 20 28 69 6e  READ_LOCK(0) (in
1b9da 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20   other words if 
1b9db 6e 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  no..        ** r
1b9dc 65 61 64 65 72 73 20 61 72 65 20 63 75 72 72 65  eaders are curre
1b9dd 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 65 20 57  ntly using the W
1b9de 41 4c 29 2c 20 74 68 65 6e 20 74 68 65 20 74 72  AL), then the tr
1b9df 61 6e 73 61 63 74 69 6f 6e 73 0d 0a 20 20 20 20  ansactions..    
1b9e0 20 20 20 20 2a 2a 20 66 72 61 6d 65 73 20 77 69      ** frames wi
1b9e1 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65  ll overwrite the
1b9e2 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 65 78   start of the ex
1b9e3 69 73 74 69 6e 67 20 6c 6f 67 2e 20 55 70 64 61  isting log. Upda
1b9e4 74 65 20 74 68 65 0d 0a 20 20 20 20 20 20 20 20  te the..        
1b9e5 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61  ** wal-index hea
1b9e6 64 65 72 20 74 6f 20 72 65 66 6c 65 63 74 20 74  der to reflect t
1b9e7 68 69 73 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a  his...        **
1b9e8 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20  ..        ** In 
1b9e9 74 68 65 6f 72 79 20 69 74 20 77 6f 75 6c 64 20  theory it would 
1b9ea 62 65 20 4f 6b 20 74 6f 20 75 70 64 61 74 65 20  be Ok to update 
1b9eb 74 68 65 20 63 61 63 68 65 20 6f 66 20 74 68 65  the cache of the
1b9ec 20 68 65 61 64 65 72 20 6f 6e 6c 79 0d 0a 20 20   header only..  
1b9ed 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68 69 73        ** at this
1b9ee 20 70 6f 69 6e 74 2e 20 42 75 74 20 75 70 64 61   point. But upda
1b9ef 74 69 6e 67 20 74 68 65 20 61 63 74 75 61 6c 20  ting the actual 
1b9f0 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72  wal-index header
1b9f1 20 69 73 20 61 6c 73 6f 0d 0a 20 20 20 20 20 20   is also..      
1b9f2 20 20 2a 2a 20 73 61 66 65 20 61 6e 64 20 6d 65    ** safe and me
1b9f3 61 6e 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ans there is no 
1b9f4 73 70 65 63 69 61 6c 20 63 61 73 65 20 66 6f 72  special case for
1b9f5 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f 28   sqlite3WalUndo(
1b9f6 29 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  )..        ** to
1b9f7 20 68 61 6e 64 6c 65 20 69 66 20 74 68 69 73 20   handle if this 
1b9f8 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
1b9f9 6f 6c 6c 65 64 20 62 61 63 6b 2e 0d 0a 20 20 20  olled back...   
1b9fa 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20       */..       
1b9fb 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1b9fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1b9fd 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
1b9fe 20 20 20 20 20 20 20 75 33 32 20 2a 61 53 61 6c         u32 *aSal
1b9ff 74 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53  t = pWal->hdr.aS
1ba00 61 6c 74 3b 20 20 20 20 20 20 20 2f 2a 20 42 69  alt;       /* Bi
1ba01 67 2d 65 6e 64 69 61 6e 20 73 61 6c 74 20 76 61  g-endian salt va
1ba02 6c 75 65 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20  lues */....     
1ba03 20 20 20 70 57 61 6c 2d 3e 6e 43 6b 70 74 2b 2b     pWal->nCkpt++
1ba04 3b 0d 0a 20 20 20 20 20 20 20 20 70 57 61 6c 2d  ;..        pWal-
1ba05 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 30  >hdr.mxFrame = 0
1ba06 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
1ba07 65 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29  e3Put4byte((u8*)
1ba08 26 61 53 61 6c 74 5b 30 5d 2c 20 31 20 2b 20 73  &aSalt[0], 1 + s
1ba09 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28  qlite3Get4byte((
1ba0a 75 38 2a 29 26 61 53 61 6c 74 5b 30 5d 29 29 3b  u8*)&aSalt[0]));
1ba0b 0d 0a 20 20 20 20 20 20 20 20 61 53 61 6c 74 5b  ..        aSalt[
1ba0c 31 5d 20 3d 20 73 61 6c 74 31 3b 0d 0a 20 20 20  1] = salt1;..   
1ba0d 20 20 20 20 20 77 61 6c 49 6e 64 65 78 57 72 69       walIndexWri
1ba0e 74 65 48 64 72 28 70 57 61 6c 29 3b 0d 0a 20 20  teHdr(pWal);..  
1ba0f 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 42 61        pInfo->nBa
1ba10 63 6b 66 69 6c 6c 20 3d 20 30 3b 0d 0a 20 20 20  ckfill = 0;..   
1ba11 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c       for(i=1; i<
1ba12 57 41 4c 5f 4e 52 45 41 44 45 52 3b 20 69 2b 2b  WAL_NREADER; i++
1ba13 29 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61  ) pInfo->aReadMa
1ba14 72 6b 5b 69 5d 20 3d 20 52 45 41 44 4d 41 52 4b  rk[i] = READMARK
1ba15 5f 4e 4f 54 5f 55 53 45 44 3b 0d 0a 20 20 20 20  _NOT_USED;..    
1ba16 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 66      assert( pInf
1ba17 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b 30 5d 3d  o->aReadMark[0]=
1ba18 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 77  =0 );..        w
1ba19 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69 76  alUnlockExclusiv
1ba1a 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44  e(pWal, WAL_READ
1ba1b 5f 4c 4f 43 4b 28 31 29 2c 20 57 41 4c 5f 4e 52  _LOCK(1), WAL_NR
1ba1c 45 41 44 45 52 2d 31 29 3b 0d 0a 20 20 20 20 20  EADER-1);..     
1ba1d 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53   }else if( rc!=S
1ba1e 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0d 0a 20  QLITE_BUSY ){.. 
1ba1f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1ba20 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1ba21 7d 0d 0a 20 20 20 20 77 61 6c 55 6e 6c 6f 63 6b  }..    walUnlock
1ba22 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41 4c  Shared(pWal, WAL
1ba23 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 29 29 3b 0d  _READ_LOCK(0));.
1ba24 0a 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4c  .    pWal->readL
1ba25 6f 63 6b 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 63  ock = -1;..    c
1ba26 6e 74 20 3d 20 30 3b 0d 0a 20 20 20 20 64 6f 7b  nt = 0;..    do{
1ba27 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f 74 55  ..      int notU
1ba28 73 65 64 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  sed;..      rc =
1ba29 20 77 61 6c 54 72 79 42 65 67 69 6e 52 65 61 64   walTryBeginRead
1ba2a 28 70 57 61 6c 2c 20 26 6e 6f 74 55 73 65 64 2c  (pWal, &notUsed,
1ba2b 20 31 2c 20 2b 2b 63 6e 74 29 3b 0d 0a 20 20 20   1, ++cnt);..   
1ba2c 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 57 41 4c   }while( rc==WAL
1ba2d 5f 52 45 54 52 59 20 29 3b 0d 0a 20 20 20 20 61  _RETRY );..    a
1ba2e 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
1ba2f 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b  !=SQLITE_BUSY );
1ba30 20 2f 2a 20 42 55 53 59 20 6e 6f 74 20 70 6f 73   /* BUSY not pos
1ba31 73 69 62 6c 65 20 77 68 65 6e 20 75 73 65 57 61  sible when useWa
1ba32 6c 3d 3d 31 20 2a 2f 0d 0a 20 20 20 20 74 65 73  l==1 */..    tes
1ba33 74 63 61 73 65 28 20 28 72 63 26 30 78 66 66 29  tcase( (rc&0xff)
1ba34 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  ==SQLITE_IOERR )
1ba35 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  ;..    testcase(
1ba36 20 72 63 3d 3d 53 51 4c 49 54 45 5f 50 52 4f 54   rc==SQLITE_PROT
1ba37 4f 43 4f 4c 20 29 3b 0d 0a 20 20 20 20 74 65 73  OCOL );..    tes
1ba38 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54  tcase( rc==SQLIT
1ba39 45 5f 4f 4b 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20  E_OK );..  }..  
1ba3a 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
1ba3b 0a 2f 2a 0d 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74  ./*..** Informat
1ba3c 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 75  ion about the cu
1ba3d 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74  rrent state of t
1ba3e 68 65 20 57 41 4c 20 66 69 6c 65 20 61 6e 64 20  he WAL file and 
1ba3f 77 68 65 72 65 0d 0a 2a 2a 20 74 68 65 20 6e 65  where..** the ne
1ba40 78 74 20 66 73 79 6e 63 20 73 68 6f 75 6c 64 20  xt fsync should 
1ba41 6f 63 63 75 72 20 2d 20 70 61 73 73 65 64 20 66  occur - passed f
1ba42 72 6f 6d 20 73 71 6c 69 74 65 33 57 61 6c 46 72  rom sqlite3WalFr
1ba43 61 6d 65 73 28 29 20 69 6e 74 6f 0d 0a 2a 2a 20  ames() into..** 
1ba44 77 61 6c 57 72 69 74 65 54 6f 4c 6f 67 28 29 2e  walWriteToLog().
1ba45 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74  ..*/..typedef st
1ba46 72 75 63 74 20 57 61 6c 57 72 69 74 65 72 20 7b  ruct WalWriter {
1ba47 0d 0a 20 20 57 61 6c 20 2a 70 57 61 6c 3b 20 20  ..  Wal *pWal;  
1ba48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ba49 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
1ba4a 20 57 41 4c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   WAL information
1ba4b 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66   */..  sqlite3_f
1ba4c 69 6c 65 20 2a 70 46 64 3b 20 20 20 20 20 20 20  ile *pFd;       
1ba4d 20 20 20 20 2f 2a 20 54 68 65 20 57 41 4c 20 66      /* The WAL f
1ba4e 69 6c 65 20 74 6f 20 77 68 69 63 68 20 77 65 20  ile to which we 
1ba4f 77 72 69 74 65 20 2a 2f 0d 0a 20 20 73 71 6c 69  write */..  sqli
1ba50 74 65 33 5f 69 6e 74 36 34 20 69 53 79 6e 63 50  te3_int64 iSyncP
1ba51 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 46 73 79 6e  oint;    /* Fsyn
1ba52 63 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74  c at this offset
1ba53 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 79 6e 63 46   */..  int syncF
1ba54 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
1ba55 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72      /* Flags for
1ba56 20 74 68 65 20 66 73 79 6e 63 20 2a 2f 0d 0a 20   the fsync */.. 
1ba57 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20   int szPage;    
1ba58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ba59 20 53 69 7a 65 20 6f 66 20 6f 6e 65 20 70 61 67   Size of one pag
1ba5a 65 20 2a 2f 0d 0a 7d 20 57 61 6c 57 72 69 74 65  e */..} WalWrite
1ba5b 72 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69  r;..../*..** Wri
1ba5c 74 65 20 69 41 6d 74 20 62 79 74 65 73 20 6f 66  te iAmt bytes of
1ba5d 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68   content into th
1ba5e 65 20 57 41 4c 20 66 69 6c 65 20 62 65 67 69 6e  e WAL file begin
1ba5f 6e 69 6e 67 20 61 74 20 69 4f 66 66 73 65 74 2e  ning at iOffset.
1ba60 0d 0a 2a 2a 20 44 6f 20 61 20 73 79 6e 63 20 77  ..** Do a sync w
1ba61 68 65 6e 20 63 72 6f 73 73 69 6e 67 20 74 68 65  hen crossing the
1ba62 20 70 2d 3e 69 53 79 6e 63 50 6f 69 6e 74 20 62   p->iSyncPoint b
1ba63 6f 75 6e 64 61 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a  oundary...**..**
1ba64 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1ba65 20 69 66 20 69 53 79 6e 63 50 6f 69 6e 74 20 69   if iSyncPoint i
1ba66 73 20 69 6e 20 62 65 74 77 65 65 6e 20 69 4f 66  s in between iOf
1ba67 66 73 65 74 20 61 6e 64 20 69 4f 66 66 73 65 74  fset and iOffset
1ba68 2b 69 41 6d 74 2c 0d 0a 2a 2a 20 66 69 72 73 74  +iAmt,..** first
1ba69 20 77 72 69 74 65 20 74 68 65 20 70 61 72 74 20   write the part 
1ba6a 62 65 66 6f 72 65 20 69 53 79 6e 63 50 6f 69 6e  before iSyncPoin
1ba6b 74 2c 20 74 68 65 6e 20 73 79 6e 63 2c 20 74 68  t, then sync, th
1ba6c 65 6e 20 77 72 69 74 65 20 74 68 65 0d 0a 2a 2a  en write the..**
1ba6d 20 72 65 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   rest...*/..stat
1ba6e 69 63 20 69 6e 74 20 77 61 6c 57 72 69 74 65 54  ic int walWriteT
1ba6f 6f 4c 6f 67 28 0d 0a 20 20 57 61 6c 57 72 69 74  oLog(..  WalWrit
1ba70 65 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  er *p,          
1ba71 20 20 20 20 2f 2a 20 57 41 4c 20 74 6f 20 77 72      /* WAL to wr
1ba72 69 74 65 20 74 6f 20 2a 2f 0d 0a 20 20 76 6f 69  ite to */..  voi
1ba73 64 20 2a 70 43 6f 6e 74 65 6e 74 2c 20 20 20 20  d *pContent,    
1ba74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
1ba75 6e 74 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  nt to be written
1ba76 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 41 6d 74 2c   */..  int iAmt,
1ba77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ba78 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1ba79 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f  ytes to write */
1ba7a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
1ba7b 34 20 69 4f 66 66 73 65 74 20 20 20 20 20 20 2f  4 iOffset      /
1ba7c 2a 20 53 74 61 72 74 20 77 72 69 74 69 6e 67 20  * Start writing 
1ba7d 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a  at this offset *
1ba7e 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  /..){..  int rc;
1ba7f 0d 0a 20 20 69 66 28 20 69 4f 66 66 73 65 74 3c  ..  if( iOffset<
1ba80 70 2d 3e 69 53 79 6e 63 50 6f 69 6e 74 20 26 26  p->iSyncPoint &&
1ba81 20 69 4f 66 66 73 65 74 2b 69 41 6d 74 3e 3d 70   iOffset+iAmt>=p
1ba82 2d 3e 69 53 79 6e 63 50 6f 69 6e 74 20 29 7b 0d  ->iSyncPoint ){.
1ba83 0a 20 20 20 20 69 6e 74 20 69 46 69 72 73 74 41  .    int iFirstA
1ba84 6d 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e 69 53  mt = (int)(p->iS
1ba85 79 6e 63 50 6f 69 6e 74 20 2d 20 69 4f 66 66 73  yncPoint - iOffs
1ba86 65 74 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  et);..    rc = s
1ba87 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
1ba88 3e 70 46 64 2c 20 70 43 6f 6e 74 65 6e 74 2c 20  >pFd, pContent, 
1ba89 69 46 69 72 73 74 41 6d 74 2c 20 69 4f 66 66 73  iFirstAmt, iOffs
1ba8a 65 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  et);..    if( rc
1ba8b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20   ) return rc;.. 
1ba8c 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 69 46     iOffset += iF
1ba8d 69 72 73 74 41 6d 74 3b 0d 0a 20 20 20 20 69 41  irstAmt;..    iA
1ba8e 6d 74 20 2d 3d 20 69 46 69 72 73 74 41 6d 74 3b  mt -= iFirstAmt;
1ba8f 0d 0a 20 20 20 20 70 43 6f 6e 74 65 6e 74 20 3d  ..    pContent =
1ba90 20 28 76 6f 69 64 2a 29 28 69 46 69 72 73 74 41   (void*)(iFirstA
1ba91 6d 74 20 2b 20 28 63 68 61 72 2a 29 70 43 6f 6e  mt + (char*)pCon
1ba92 74 65 6e 74 29 3b 0d 0a 20 20 20 20 61 73 73 65  tent);..    asse
1ba93 72 74 28 20 70 2d 3e 73 79 6e 63 46 6c 61 67 73  rt( p->syncFlags
1ba94 20 26 20 28 53 51 4c 49 54 45 5f 53 59 4e 43 5f   & (SQLITE_SYNC_
1ba95 4e 4f 52 4d 41 4c 7c 53 51 4c 49 54 45 5f 53 59  NORMAL|SQLITE_SY
1ba96 4e 43 5f 46 55 4c 4c 29 20 29 3b 0d 0a 20 20 20  NC_FULL) );..   
1ba97 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
1ba98 79 6e 63 28 70 2d 3e 70 46 64 2c 20 70 2d 3e 73  ync(p->pFd, p->s
1ba99 79 6e 63 46 6c 61 67 73 29 3b 0d 0a 20 20 20 20  yncFlags);..    
1ba9a 69 66 28 20 69 41 6d 74 3d 3d 30 20 7c 7c 20 72  if( iAmt==0 || r
1ba9b 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  c ) return rc;..
1ba9c 20 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69    }..  rc = sqli
1ba9d 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 46  te3OsWrite(p->pF
1ba9e 64 2c 20 70 43 6f 6e 74 65 6e 74 2c 20 69 41 6d  d, pContent, iAm
1ba9f 74 2c 20 69 4f 66 66 73 65 74 29 3b 0d 0a 20 20  t, iOffset);..  
1baa0 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
1baa1 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 6f 75  ./*..** Write ou
1baa2 74 20 61 20 73 69 6e 67 6c 65 20 66 72 61 6d 65  t a single frame
1baa3 20 6f 66 20 74 68 65 20 57 41 4c 0d 0a 2a 2f 0d   of the WAL..*/.
1baa4 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c 57  .static int walW
1baa5 72 69 74 65 4f 6e 65 46 72 61 6d 65 28 0d 0a 20  riteOneFrame(.. 
1baa6 20 57 61 6c 57 72 69 74 65 72 20 2a 70 2c 20 20   WalWriter *p,  
1baa7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1baa8 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74  Where to write t
1baa9 68 65 20 66 72 61 6d 65 20 2a 2f 0d 0a 20 20 50  he frame */..  P
1baaa 67 48 64 72 20 2a 70 50 61 67 65 2c 20 20 20 20  gHdr *pPage,    
1baab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1baac 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 72  e page of the fr
1baad 61 6d 65 20 74 6f 20 62 65 20 77 72 69 74 74 65  ame to be writte
1baae 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 72 75  n */..  int nTru
1baaf 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  ncate,          
1bab0 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d 69      /* The commi
1bab1 74 20 66 6c 61 67 2e 20 20 55 73 75 61 6c 6c 79  t flag.  Usually
1bab2 20 30 2e 20 20 3e 30 20 66 6f 72 20 63 6f 6d 6d   0.  >0 for comm
1bab3 69 74 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  it */..  sqlite3
1bab4 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 20 20  _int64 iOffset  
1bab5 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66       /* Byte off
1bab6 73 65 74 20 61 74 20 77 68 69 63 68 20 74 6f 20  set at which to 
1bab7 77 72 69 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  write */..){..  
1bab8 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1bab9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1baba 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66  /* Result code f
1babb 72 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73  rom subfunctions
1babc 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 44 61   */..  void *pDa
1babd 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
1babe 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61         /* Data a
1babf 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ctually written 
1bac0 2a 2f 0d 0a 20 20 75 38 20 61 46 72 61 6d 65 5b  */..  u8 aFrame[
1bac1 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a  WAL_FRAME_HDRSIZ
1bac2 45 5d 3b 20 20 20 2f 2a 20 42 75 66 66 65 72 20  E];   /* Buffer 
1bac3 74 6f 20 61 73 73 65 6d 62 6c 65 20 66 72 61 6d  to assemble fram
1bac4 65 2d 68 65 61 64 65 72 20 69 6e 20 2a 2f 0d 0a  e-header in */..
1bac5 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1bac6 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0d 0a 20  TE_HAS_CODEC).. 
1bac7 20 69 66 28 20 28 70 44 61 74 61 20 3d 20 73 71   if( (pData = sq
1bac8 6c 69 74 65 33 50 61 67 65 72 43 6f 64 65 63 28  lite3PagerCodec(
1bac9 70 50 61 67 65 29 29 3d 3d 30 20 29 20 72 65 74  pPage))==0 ) ret
1baca 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1bacb 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 70 44 61 74  ;..#else..  pDat
1bacc 61 20 3d 20 70 50 61 67 65 2d 3e 70 44 61 74 61  a = pPage->pData
1bacd 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 77 61 6c  ;..#endif..  wal
1bace 45 6e 63 6f 64 65 46 72 61 6d 65 28 70 2d 3e 70  EncodeFrame(p->p
1bacf 57 61 6c 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  Wal, pPage->pgno
1bad0 2c 20 6e 54 72 75 6e 63 61 74 65 2c 20 70 44 61  , nTruncate, pDa
1bad1 74 61 2c 20 61 46 72 61 6d 65 29 3b 0d 0a 20 20  ta, aFrame);..  
1bad2 72 63 20 3d 20 77 61 6c 57 72 69 74 65 54 6f 4c  rc = walWriteToL
1bad3 6f 67 28 70 2c 20 61 46 72 61 6d 65 2c 20 73 69  og(p, aFrame, si
1bad4 7a 65 6f 66 28 61 46 72 61 6d 65 29 2c 20 69 4f  zeof(aFrame), iO
1bad5 66 66 73 65 74 29 3b 0d 0a 20 20 69 66 28 20 72  ffset);..  if( r
1bad6 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  c ) return rc;..
1bad7 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70    /* Write the p
1bad8 61 67 65 20 64 61 74 61 20 2a 2f 0d 0a 20 20 72  age data */..  r
1bad9 63 20 3d 20 77 61 6c 57 72 69 74 65 54 6f 4c 6f  c = walWriteToLo
1bada 67 28 70 2c 20 70 44 61 74 61 2c 20 70 2d 3e 73  g(p, pData, p->s
1badb 7a 50 61 67 65 2c 20 69 4f 66 66 73 65 74 2b 73  zPage, iOffset+s
1badc 69 7a 65 6f 66 28 61 46 72 61 6d 65 29 29 3b 0d  izeof(aFrame));.
1badd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
1bade 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 57 72 69 74  ..../* ..** Writ
1badf 65 20 61 20 73 65 74 20 6f 66 20 66 72 61 6d 65  e a set of frame
1bae0 73 20 74 6f 20 74 68 65 20 6c 6f 67 2e 20 54 68  s to the log. Th
1bae1 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f  e caller must ho
1bae2 6c 64 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63  ld the write-loc
1bae3 6b 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 67  k..** on the log
1bae4 20 66 69 6c 65 20 28 6f 62 74 61 69 6e 65 64 20   file (obtained 
1bae5 75 73 69 6e 67 20 73 71 6c 69 74 65 33 57 61 6c  using sqlite3Wal
1bae6 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61  BeginWriteTransa
1bae7 63 74 69 6f 6e 28 29 29 2e 0d 0a 2a 2f 0d 0a 53  ction())...*/..S
1bae8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1bae9 74 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d  t sqlite3WalFram
1baea 65 73 28 0d 0a 20 20 57 61 6c 20 2a 70 57 61 6c  es(..  Wal *pWal
1baeb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1baec 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 68 61         /* Wal ha
1baed 6e 64 6c 65 20 74 6f 20 77 72 69 74 65 20 74 6f  ndle to write to
1baee 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 50 61 67   */..  int szPag
1baef 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
1baf0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1baf1 73 65 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20  se page-size in 
1baf2 62 79 74 65 73 20 2a 2f 0d 0a 20 20 50 67 48 64  bytes */..  PgHd
1baf3 72 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20  r *pList,       
1baf4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1baf5 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67  ist of dirty pag
1baf6 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0d 0a  es to write */..
1baf7 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 61 74 65    Pgno nTruncate
1baf8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1baf9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 69    /* Database si
1bafa 7a 65 20 61 66 74 65 72 20 74 68 69 73 20 63 6f  ze after this co
1bafb 6d 6d 69 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  mmit */..  int i
1bafc 73 43 6f 6d 6d 69 74 2c 20 20 20 20 20 20 20 20  sCommit,        
1bafd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1bafe 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
1baff 63 6f 6d 6d 69 74 20 2a 2f 0d 0a 20 20 69 6e 74  commit */..  int
1bb00 20 73 79 6e 63 5f 66 6c 61 67 73 20 20 20 20 20   sync_flags     
1bb01 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1bb02 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f  Flags to pass to
1bb03 20 4f 73 53 79 6e 63 28 29 20 28 6f 72 20 30 29   OsSync() (or 0)
1bb04 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
1bb05 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1bb06 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
1bb07 65 64 20 74 6f 20 63 61 74 63 68 20 72 65 74 75  ed to catch retu
1bb08 72 6e 20 63 6f 64 65 73 20 2a 2f 0d 0a 20 20 75  rn codes */..  u
1bb09 33 32 20 69 46 72 61 6d 65 3b 20 20 20 20 20 20  32 iFrame;      
1bb0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1bb0b 2a 20 4e 65 78 74 20 66 72 61 6d 65 20 61 64 64  * Next frame add
1bb0c 72 65 73 73 20 2a 2f 0d 0a 20 20 50 67 48 64 72  ress */..  PgHdr
1bb0d 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
1bb0e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
1bb0f 65 72 61 74 6f 72 20 74 6f 20 72 75 6e 20 74 68  erator to run th
1bb10 72 6f 75 67 68 20 70 4c 69 73 74 20 77 69 74 68  rough pList with
1bb11 2e 20 2a 2f 0d 0a 20 20 50 67 48 64 72 20 2a 70  . */..  PgHdr *p
1bb12 4c 61 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Last = 0;       
1bb13 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
1bb14 66 72 61 6d 65 20 69 6e 20 6c 69 73 74 20 2a 2f  frame in list */
1bb15 0d 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d  ..  int nExtra =
1bb16 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1bb17 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1bb18 20 65 78 74 72 61 20 63 6f 70 69 65 73 20 6f 66   extra copies of
1bb19 20 6c 61 73 74 20 70 61 67 65 20 2a 2f 0d 0a 20   last page */.. 
1bb1a 20 69 6e 74 20 73 7a 46 72 61 6d 65 3b 20 20 20   int szFrame;   
1bb1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bb1c 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20   /* The size of 
1bb1d 61 20 73 69 6e 67 6c 65 20 66 72 61 6d 65 20 2a  a single frame *
1bb1e 2f 0d 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74  /..  i64 iOffset
1bb1f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1bb20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 79 74       /* Next byt
1bb21 65 20 74 6f 20 77 72 69 74 65 20 69 6e 20 57 41  e to write in WA
1bb22 4c 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 57 61 6c  L file */..  Wal
1bb23 57 72 69 74 65 72 20 77 3b 20 20 20 20 20 20 20  Writer w;       
1bb24 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1bb25 54 68 65 20 77 72 69 74 65 72 20 2a 2f 0d 0a 0d  The writer */...
1bb26 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
1bb27 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
1bb28 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29  Wal->writeLock )
1bb29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69  ;....  /* If thi
1bb2a 73 20 66 72 61 6d 65 20 73 65 74 20 63 6f 6d 70  s frame set comp
1bb2b 6c 65 74 65 73 20 61 20 74 72 61 6e 73 61 63 74  letes a transact
1bb2c 69 6f 6e 2c 20 74 68 65 6e 20 6e 54 72 75 6e 63  ion, then nTrunc
1bb2d 61 74 65 3e 30 2e 20 20 49 66 0d 0a 20 20 2a 2a  ate>0.  If..  **
1bb2e 20 6e 54 72 75 6e 63 61 74 65 3d 3d 30 20 74 68   nTruncate==0 th
1bb2f 65 6e 20 74 68 69 73 20 66 72 61 6d 65 20 73 65  en this frame se
1bb30 74 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 70 6c  t does not compl
1bb31 65 74 65 20 74 68 65 20 74 72 61 6e 73 61 63 74  ete the transact
1bb32 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72  ion. */..  asser
1bb33 74 28 20 28 69 73 43 6f 6d 6d 69 74 21 3d 30 29  t( (isCommit!=0)
1bb34 3d 3d 28 6e 54 72 75 6e 63 61 74 65 21 3d 30 29  ==(nTruncate!=0)
1bb35 20 29 3b 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e   );....#if defin
1bb36 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
1bb37 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
1bb38 45 5f 44 45 42 55 47 29 0d 0a 20 20 7b 20 69 6e  E_DEBUG)..  { in
1bb39 74 20 63 6e 74 3b 20 66 6f 72 28 63 6e 74 3d 30  t cnt; for(cnt=0
1bb3a 2c 20 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d  , p=pList; p; p=
1bb3b 70 2d 3e 70 44 69 72 74 79 2c 20 63 6e 74 2b 2b  p->pDirty, cnt++
1bb3c 29 7b 7d 0d 0a 20 20 20 20 57 41 4c 54 52 41 43  ){}..    WALTRAC
1bb3d 45 28 28 22 57 41 4c 25 70 3a 20 66 72 61 6d 65  E(("WAL%p: frame
1bb3e 20 77 72 69 74 65 20 62 65 67 69 6e 2e 20 25 64   write begin. %d
1bb3f 20 66 72 61 6d 65 73 2e 20 6d 78 46 72 61 6d 65   frames. mxFrame
1bb40 3d 25 64 2e 20 25 73 5c 6e 22 2c 0d 0a 20 20 20  =%d. %s\n",..   
1bb41 20 20 20 20 20 20 20 20 20 20 20 70 57 61 6c 2c             pWal,
1bb42 20 63 6e 74 2c 20 70 57 61 6c 2d 3e 68 64 72 2e   cnt, pWal->hdr.
1bb43 6d 78 46 72 61 6d 65 2c 20 69 73 43 6f 6d 6d 69  mxFrame, isCommi
1bb44 74 20 3f 20 22 43 6f 6d 6d 69 74 22 20 3a 20 22  t ? "Commit" : "
1bb45 53 70 69 6c 6c 22 29 29 3b 0d 0a 20 20 7d 0d 0a  Spill"));..  }..
1bb46 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 53  #endif....  /* S
1bb47 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73  ee if it is poss
1bb48 69 62 6c 65 20 74 6f 20 77 72 69 74 65 20 74 68  ible to write th
1bb49 65 73 65 20 66 72 61 6d 65 73 20 69 6e 74 6f 20  ese frames into 
1bb4a 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
1bb4b 0d 0a 20 20 2a 2a 20 6c 6f 67 20 66 69 6c 65 2c  ..  ** log file,
1bb4c 20 69 6e 73 74 65 61 64 20 6f 66 20 61 70 70 65   instead of appe
1bb4d 6e 64 69 6e 67 20 74 6f 20 69 74 20 61 74 20 70  nding to it at p
1bb4e 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1bb4f 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 53  ...  */..  if( S
1bb50 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
1bb51 77 61 6c 52 65 73 74 61 72 74 4c 6f 67 28 70 57  walRestartLog(pW
1bb52 61 6c 29 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  al)) ){..    ret
1bb53 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a  urn rc;..  }....
1bb54 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1bb55 74 68 65 20 66 69 72 73 74 20 66 72 61 6d 65 20  the first frame 
1bb56 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
1bb57 20 6c 6f 67 2c 20 77 72 69 74 65 20 74 68 65 20   log, write the 
1bb58 57 41 4c 0d 0a 20 20 2a 2a 20 68 65 61 64 65 72  WAL..  ** header
1bb59 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1bb5a 20 74 68 65 20 57 41 4c 20 66 69 6c 65 2e 20 53   the WAL file. S
1bb5b 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 74 20 74  ee comments at t
1bb5c 68 65 20 74 6f 70 20 6f 66 0d 0a 20 20 2a 2a 20  he top of..  ** 
1bb5d 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  this source file
1bb5e 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69   for a descripti
1bb5f 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 68 65  on of the WAL he
1bb60 61 64 65 72 20 66 6f 72 6d 61 74 2e 0d 0a 20 20  ader format...  
1bb61 2a 2f 0d 0a 20 20 69 46 72 61 6d 65 20 3d 20 70  */..  iFrame = p
1bb62 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1bb63 3b 0d 0a 20 20 69 66 28 20 69 46 72 61 6d 65 3d  ;..  if( iFrame=
1bb64 3d 30 20 29 7b 0d 0a 20 20 20 20 75 38 20 61 57  =0 ){..    u8 aW
1bb65 61 6c 48 64 72 5b 57 41 4c 5f 48 44 52 53 49 5a  alHdr[WAL_HDRSIZ
1bb66 45 5d 3b 20 20 20 20 20 20 2f 2a 20 42 75 66 66  E];      /* Buff
1bb67 65 72 20 74 6f 20 61 73 73 65 6d 62 6c 65 20 77  er to assemble w
1bb68 61 6c 2d 68 65 61 64 65 72 20 69 6e 20 2a 2f 0d  al-header in */.
1bb69 0a 20 20 20 20 75 33 32 20 61 43 6b 73 75 6d 5b  .    u32 aCksum[
1bb6a 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  2];             
1bb6b 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 66     /* Checksum f
1bb6c 6f 72 20 77 61 6c 2d 68 65 61 64 65 72 20 2a 2f  or wal-header */
1bb6d 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ....    sqlite3P
1bb6e 75 74 34 62 79 74 65 28 26 61 57 61 6c 48 64 72  ut4byte(&aWalHdr
1bb6f 5b 30 5d 2c 20 28 57 41 4c 5f 4d 41 47 49 43 20  [0], (WAL_MAGIC 
1bb70 7c 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49  | SQLITE_BIGENDI
1bb71 41 4e 29 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  AN));..    sqlit
1bb72 65 33 50 75 74 34 62 79 74 65 28 26 61 57 61 6c  e3Put4byte(&aWal
1bb73 48 64 72 5b 34 5d 2c 20 57 41 4c 5f 4d 41 58 5f  Hdr[4], WAL_MAX_
1bb74 56 45 52 53 49 4f 4e 29 3b 0d 0a 20 20 20 20 73  VERSION);..    s
1bb75 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 26  qlite3Put4byte(&
1bb76 61 57 61 6c 48 64 72 5b 38 5d 2c 20 73 7a 50 61  aWalHdr[8], szPa
1bb77 67 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ge);..    sqlite
1bb78 33 50 75 74 34 62 79 74 65 28 26 61 57 61 6c 48  3Put4byte(&aWalH
1bb79 64 72 5b 31 32 5d 2c 20 70 57 61 6c 2d 3e 6e 43  dr[12], pWal->nC
1bb7a 6b 70 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  kpt);..    if( p
1bb7b 57 61 6c 2d 3e 6e 43 6b 70 74 3d 3d 30 20 29 20  Wal->nCkpt==0 ) 
1bb7c 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
1bb7d 73 73 28 38 2c 20 70 57 61 6c 2d 3e 68 64 72 2e  ss(8, pWal->hdr.
1bb7e 61 53 61 6c 74 29 3b 0d 0a 20 20 20 20 6d 65 6d  aSalt);..    mem
1bb7f 63 70 79 28 26 61 57 61 6c 48 64 72 5b 31 36 5d  cpy(&aWalHdr[16]
1bb80 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61 6c  , pWal->hdr.aSal
1bb81 74 2c 20 38 29 3b 0d 0a 20 20 20 20 77 61 6c 43  t, 8);..    walC
1bb82 68 65 63 6b 73 75 6d 42 79 74 65 73 28 31 2c 20  hecksumBytes(1, 
1bb83 61 57 61 6c 48 64 72 2c 20 57 41 4c 5f 48 44 52  aWalHdr, WAL_HDR
1bb84 53 49 5a 45 2d 32 2a 34 2c 20 30 2c 20 61 43 6b  SIZE-2*4, 0, aCk
1bb85 73 75 6d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  sum);..    sqlit
1bb86 65 33 50 75 74 34 62 79 74 65 28 26 61 57 61 6c  e3Put4byte(&aWal
1bb87 48 64 72 5b 32 34 5d 2c 20 61 43 6b 73 75 6d 5b  Hdr[24], aCksum[
1bb88 30 5d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0]);..    sqlite
1bb89 33 50 75 74 34 62 79 74 65 28 26 61 57 61 6c 48  3Put4byte(&aWalH
1bb8a 64 72 5b 32 38 5d 2c 20 61 43 6b 73 75 6d 5b 31  dr[28], aCksum[1
1bb8b 5d 29 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20 70  ]);..    ..    p
1bb8c 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a  Wal->szPage = sz
1bb8d 50 61 67 65 3b 0d 0a 20 20 20 20 70 57 61 6c 2d  Page;..    pWal-
1bb8e 3e 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d  >hdr.bigEndCksum
1bb8f 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44   = SQLITE_BIGEND
1bb90 49 41 4e 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e  IAN;..    pWal->
1bb91 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b  hdr.aFrameCksum[
1bb92 30 5d 20 3d 20 61 43 6b 73 75 6d 5b 30 5d 3b 0d  0] = aCksum[0];.
1bb93 0a 20 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61  .    pWal->hdr.a
1bb94 46 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 20 3d 20  FrameCksum[1] = 
1bb95 61 43 6b 73 75 6d 5b 31 5d 3b 0d 0a 20 20 20 20  aCksum[1];..    
1bb96 70 57 61 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e  pWal->truncateOn
1bb97 43 6f 6d 6d 69 74 20 3d 20 31 3b 0d 0a 0d 0a 20  Commit = 1;.... 
1bb98 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1bb99 73 57 72 69 74 65 28 70 57 61 6c 2d 3e 70 57 61  sWrite(pWal->pWa
1bb9a 6c 46 64 2c 20 61 57 61 6c 48 64 72 2c 20 73 69  lFd, aWalHdr, si
1bb9b 7a 65 6f 66 28 61 57 61 6c 48 64 72 29 2c 20 30  zeof(aWalHdr), 0
1bb9c 29 3b 0d 0a 20 20 20 20 57 41 4c 54 52 41 43 45  );..    WALTRACE
1bb9d 28 28 22 57 41 4c 25 70 3a 20 77 61 6c 2d 68 65  (("WAL%p: wal-he
1bb9e 61 64 65 72 20 77 72 69 74 65 20 25 73 5c 6e 22  ader write %s\n"
1bb9f 2c 20 70 57 61 6c 2c 20 72 63 20 3f 20 22 66 61  , pWal, rc ? "fa
1bba0 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0d  iled" : "ok"));.
1bba1 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1bba2 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1bba3 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1bba4 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 79 6e   }....    /* Syn
1bba5 63 20 74 68 65 20 68 65 61 64 65 72 20 28 75 6e  c the header (un
1bba6 6c 65 73 73 20 53 51 4c 49 54 45 5f 49 4f 43 41  less SQLITE_IOCA
1bba7 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 69 73 20  P_SEQUENTIAL is 
1bba8 74 72 75 65 20 6f 72 20 75 6e 6c 65 73 73 0d 0a  true or unless..
1bba9 20 20 20 20 2a 2a 20 61 6c 6c 20 73 79 6e 63 69      ** all synci
1bbaa 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66  ng is turned off
1bbab 20 62 79 20 50 52 41 47 4d 41 20 73 79 6e 63 68   by PRAGMA synch
1bbac 72 6f 6e 6f 75 73 3d 4f 46 46 29 2e 20 20 4f 74  ronous=OFF).  Ot
1bbad 68 65 72 77 69 73 65 0d 0a 20 20 20 20 2a 2a 20  herwise..    ** 
1bbae 61 6e 20 6f 75 74 2d 6f 66 2d 6f 72 64 65 72 20  an out-of-order 
1bbaf 77 72 69 74 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  write following 
1bbb0 61 20 57 41 4c 20 72 65 73 74 61 72 74 20 63 6f  a WAL restart co
1bbb1 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 0d 0a 20  uld result in.. 
1bbb2 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63     ** database c
1bbb3 6f 72 72 75 70 74 69 6f 6e 2e 20 20 53 65 65 20  orruption.  See 
1bbb4 74 68 65 20 74 69 63 6b 65 74 3a 0d 0a 20 20 20  the ticket:..   
1bbb5 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20   **..    **     
1bbb6 68 74 74 70 3a 2f 2f 6c 6f 63 61 6c 68 6f 73 74  http://localhost
1bbb7 3a 35 39 31 2f 73 71 6c 69 74 65 2f 69 6e 66 6f  :591/sqlite/info
1bbb8 2f 66 66 35 62 65 37 33 64 65 65 0d 0a 20 20 20  /ff5be73dee..   
1bbb9 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 57 61   */..    if( pWa
1bbba 6c 2d 3e 73 79 6e 63 48 65 61 64 65 72 20 26 26  l->syncHeader &&
1bbbb 20 73 79 6e 63 5f 66 6c 61 67 73 20 29 7b 0d 0a   sync_flags ){..
1bbbc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bbbd 65 33 4f 73 53 79 6e 63 28 70 57 61 6c 2d 3e 70  e3OsSync(pWal->p
1bbbe 57 61 6c 46 64 2c 20 73 79 6e 63 5f 66 6c 61 67  WalFd, sync_flag
1bbbf 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s & SQLITE_SYNC_
1bbc0 4d 41 53 4b 29 3b 0d 0a 20 20 20 20 20 20 69 66  MASK);..      if
1bbc1 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1bbc2 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
1bbc3 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 70 57   assert( (int)pW
1bbc4 61 6c 2d 3e 73 7a 50 61 67 65 3d 3d 73 7a 50 61  al->szPage==szPa
1bbc5 67 65 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 65  ge );....  /* Se
1bbc6 74 75 70 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tup information 
1bbc7 6e 65 65 64 65 64 20 74 6f 20 77 72 69 74 65 20  needed to write 
1bbc8 66 72 61 6d 65 73 20 69 6e 74 6f 20 74 68 65 20  frames into the 
1bbc9 57 41 4c 20 2a 2f 0d 0a 20 20 77 2e 70 57 61 6c  WAL */..  w.pWal
1bbca 20 3d 20 70 57 61 6c 3b 0d 0a 20 20 77 2e 70 46   = pWal;..  w.pF
1bbcb 64 20 3d 20 70 57 61 6c 2d 3e 70 57 61 6c 46 64  d = pWal->pWalFd
1bbcc 3b 0d 0a 20 20 77 2e 69 53 79 6e 63 50 6f 69 6e  ;..  w.iSyncPoin
1bbcd 74 20 3d 20 30 3b 0d 0a 20 20 77 2e 73 79 6e 63  t = 0;..  w.sync
1bbce 46 6c 61 67 73 20 3d 20 73 79 6e 63 5f 66 6c 61  Flags = sync_fla
1bbcf 67 73 3b 0d 0a 20 20 77 2e 73 7a 50 61 67 65 20  gs;..  w.szPage 
1bbd0 3d 20 73 7a 50 61 67 65 3b 0d 0a 20 20 69 4f 66  = szPage;..  iOf
1bbd1 66 73 65 74 20 3d 20 77 61 6c 46 72 61 6d 65 4f  fset = walFrameO
1bbd2 66 66 73 65 74 28 69 46 72 61 6d 65 2b 31 2c 20  ffset(iFrame+1, 
1bbd3 73 7a 50 61 67 65 29 3b 0d 0a 20 20 73 7a 46 72  szPage);..  szFr
1bbd4 61 6d 65 20 3d 20 73 7a 50 61 67 65 20 2b 20 57  ame = szPage + W
1bbd5 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45  AL_FRAME_HDRSIZE
1bbd6 3b 0d 0a 0d 0a 20 20 2f 2a 20 57 72 69 74 65 20  ;....  /* Write 
1bbd7 61 6c 6c 20 66 72 61 6d 65 73 20 69 6e 74 6f 20  all frames into 
1bbd8 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 65 78 61  the log file exa
1bbd9 63 74 6c 79 20 6f 6e 63 65 20 2a 2f 0d 0a 20 20  ctly once */..  
1bbda 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20  for(p=pList; p; 
1bbdb 70 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0d 0a 20  p=p->pDirty){.. 
1bbdc 20 20 20 69 6e 74 20 6e 44 62 53 69 7a 65 3b 20     int nDbSize; 
1bbdd 20 20 2f 2a 20 30 20 6e 6f 72 6d 61 6c 6c 79 2e    /* 0 normally.
1bbde 20 20 50 6f 73 69 74 69 76 65 20 3d 3d 20 63 6f    Positive == co
1bbdf 6d 6d 69 74 20 66 6c 61 67 20 2a 2f 0d 0a 20 20  mmit flag */..  
1bbe0 20 20 69 46 72 61 6d 65 2b 2b 3b 0d 0a 20 20 20    iFrame++;..   
1bbe1 20 61 73 73 65 72 74 28 20 69 4f 66 66 73 65 74   assert( iOffset
1bbe2 3d 3d 77 61 6c 46 72 61 6d 65 4f 66 66 73 65 74  ==walFrameOffset
1bbe3 28 69 46 72 61 6d 65 2c 20 73 7a 50 61 67 65 29  (iFrame, szPage)
1bbe4 20 29 3b 0d 0a 20 20 20 20 6e 44 62 53 69 7a 65   );..    nDbSize
1bbe5 20 3d 20 28 69 73 43 6f 6d 6d 69 74 20 26 26 20   = (isCommit && 
1bbe6 70 2d 3e 70 44 69 72 74 79 3d 3d 30 29 20 3f 20  p->pDirty==0) ? 
1bbe7 6e 54 72 75 6e 63 61 74 65 20 3a 20 30 3b 0d 0a  nTruncate : 0;..
1bbe8 20 20 20 20 72 63 20 3d 20 77 61 6c 57 72 69 74      rc = walWrit
1bbe9 65 4f 6e 65 46 72 61 6d 65 28 26 77 2c 20 70 2c  eOneFrame(&w, p,
1bbea 20 6e 44 62 53 69 7a 65 2c 20 69 4f 66 66 73 65   nDbSize, iOffse
1bbeb 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20  t);..    if( rc 
1bbec 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20  ) return rc;..  
1bbed 20 20 70 4c 61 73 74 20 3d 20 70 3b 0d 0a 20 20    pLast = p;..  
1bbee 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 73 7a 46    iOffset += szF
1bbef 72 61 6d 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rame;..  }....  
1bbf0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
1bbf1 65 20 65 6e 64 20 6f 66 20 61 20 74 72 61 6e 73  e end of a trans
1bbf2 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20  action, then we 
1bbf3 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 70 61  might need to pa
1bbf4 64 0d 0a 20 20 2a 2a 20 74 68 65 20 74 72 61 6e  d..  ** the tran
1bbf5 73 61 63 74 69 6f 6e 20 61 6e 64 2f 6f 72 20 73  saction and/or s
1bbf6 79 6e 63 20 74 68 65 20 57 41 4c 20 66 69 6c 65  ync the WAL file
1bbf7 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 50 61  ...  **..  ** Pa
1bbf8 64 64 69 6e 67 20 61 6e 64 20 73 79 6e 63 69 6e  dding and syncin
1bbf9 67 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20  g only occur if 
1bbfa 74 68 69 73 20 73 65 74 20 6f 66 20 66 72 61 6d  this set of fram
1bbfb 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 0d 0a 20  es complete a.. 
1bbfc 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
1bbfd 61 6e 64 20 69 66 20 50 52 41 47 4d 41 20 73 79  and if PRAGMA sy
1bbfe 6e 63 68 72 6f 6e 6f 75 73 3d 46 55 4c 4c 2e 20  nchronous=FULL. 
1bbff 20 49 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d   If synchronous=
1bc00 3d 4e 4f 52 4d 41 4c 0d 0a 20 20 2a 2a 20 6f 72  =NORMAL..  ** or
1bc01 20 73 79 6e 63 68 6f 6e 6f 75 73 3d 3d 4f 46 46   synchonous==OFF
1bc02 2c 20 74 68 65 6e 20 6e 6f 20 70 61 64 64 69 6e  , then no paddin
1bc03 67 20 6f 72 20 73 79 6e 63 69 6e 67 20 61 72 65  g or syncing are
1bc04 20 6e 65 65 64 65 64 2e 0d 0a 20 20 2a 2a 0d 0a   needed...  **..
1bc05 20 20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 49    ** If SQLITE_I
1bc06 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f  OCAP_POWERSAFE_O
1bc07 56 45 52 57 52 49 54 45 20 69 73 20 64 65 66 69  VERWRITE is defi
1bc08 6e 65 64 2c 20 74 68 65 6e 20 70 61 64 64 69 6e  ned, then paddin
1bc09 67 20 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 6e  g is not..  ** n
1bc0a 65 65 64 65 64 20 61 6e 64 20 6f 6e 6c 79 20 74  eeded and only t
1bc0b 68 65 20 73 79 6e 63 20 69 73 20 64 6f 6e 65 2e  he sync is done.
1bc0c 20 20 49 66 20 70 61 64 64 69 6e 67 20 69 73 20    If padding is 
1bc0d 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 74 68 65  needed, then the
1bc0e 0d 0a 20 20 2a 2a 20 66 69 6e 61 6c 20 66 72 61  ..  ** final fra
1bc0f 6d 65 20 69 73 20 72 65 70 65 61 74 65 64 20 28  me is repeated (
1bc10 77 69 74 68 20 69 74 73 20 63 6f 6d 6d 69 74 20  with its commit 
1bc11 6d 61 72 6b 29 20 75 6e 74 69 6c 20 74 68 65 20  mark) until the 
1bc12 6e 65 78 74 20 73 65 63 74 6f 72 0d 0a 20 20 2a  next sector..  *
1bc13 2a 20 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72  * boundary is cr
1bc14 6f 73 73 65 64 2e 20 20 4f 6e 6c 79 20 74 68 65  ossed.  Only the
1bc15 20 70 61 72 74 20 6f 66 20 74 68 65 20 57 41 4c   part of the WAL
1bc16 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 6c 61   prior to the la
1bc17 73 74 0d 0a 20 20 2a 2a 20 73 65 63 74 6f 72 20  st..  ** sector 
1bc18 62 6f 75 6e 64 61 72 79 20 69 73 20 73 79 6e 63  boundary is sync
1bc19 65 64 3b 20 74 68 65 20 70 61 72 74 20 6f 66 20  ed; the part of 
1bc1a 74 68 65 20 6c 61 73 74 20 66 72 61 6d 65 20 74  the last frame t
1bc1b 68 61 74 20 65 78 74 65 6e 64 73 0d 0a 20 20 2a  hat extends..  *
1bc1c 2a 20 70 61 73 74 20 74 68 65 20 73 65 63 74 6f  * past the secto
1bc1d 72 20 62 6f 75 6e 64 61 72 79 20 69 73 20 77 72  r boundary is wr
1bc1e 69 74 74 65 6e 20 61 66 74 65 72 20 74 68 65 20  itten after the 
1bc1f 73 79 6e 63 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  sync...  */..  i
1bc20 66 28 20 69 73 43 6f 6d 6d 69 74 20 26 26 20 28  f( isCommit && (
1bc21 73 79 6e 63 5f 66 6c 61 67 73 20 26 20 57 41 4c  sync_flags & WAL
1bc22 5f 53 59 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f  _SYNC_TRANSACTIO
1bc23 4e 53 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 69  NS)!=0 ){..    i
1bc24 66 28 20 70 57 61 6c 2d 3e 70 61 64 54 6f 53 65  f( pWal->padToSe
1bc25 63 74 6f 72 42 6f 75 6e 64 61 72 79 20 29 7b 0d  ctorBoundary ){.
1bc26 0a 20 20 20 20 20 20 69 6e 74 20 73 65 63 74 6f  .      int secto
1bc27 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f  rSize = sqlite3O
1bc28 73 53 65 63 74 6f 72 53 69 7a 65 28 70 57 61 6c  sSectorSize(pWal
1bc29 2d 3e 70 57 61 6c 46 64 29 3b 0d 0a 20 20 20 20  ->pWalFd);..    
1bc2a 20 20 77 2e 69 53 79 6e 63 50 6f 69 6e 74 20 3d    w.iSyncPoint =
1bc2b 20 28 28 69 4f 66 66 73 65 74 2b 73 65 63 74 6f   ((iOffset+secto
1bc2c 72 53 69 7a 65 2d 31 29 2f 73 65 63 74 6f 72 53  rSize-1)/sectorS
1bc2d 69 7a 65 29 2a 73 65 63 74 6f 72 53 69 7a 65 3b  ize)*sectorSize;
1bc2e 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69  ..      while( i
1bc2f 4f 66 66 73 65 74 3c 77 2e 69 53 79 6e 63 50 6f  Offset<w.iSyncPo
1bc30 69 6e 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  int ){..        
1bc31 72 63 20 3d 20 77 61 6c 57 72 69 74 65 4f 6e 65  rc = walWriteOne
1bc32 46 72 61 6d 65 28 26 77 2c 20 70 4c 61 73 74 2c  Frame(&w, pLast,
1bc33 20 6e 54 72 75 6e 63 61 74 65 2c 20 69 4f 66 66   nTruncate, iOff
1bc34 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  set);..        i
1bc35 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1bc36 63 3b 0d 0a 20 20 20 20 20 20 20 20 69 4f 66 66  c;..        iOff
1bc37 73 65 74 20 2b 3d 20 73 7a 46 72 61 6d 65 3b 0d  set += szFrame;.
1bc38 0a 20 20 20 20 20 20 20 20 6e 45 78 74 72 61 2b  .        nExtra+
1bc39 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  +;..      }..   
1bc3a 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72   }else{..      r
1bc3b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
1bc3c 63 28 77 2e 70 46 64 2c 20 73 79 6e 63 5f 66 6c  c(w.pFd, sync_fl
1bc3d 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
1bc3e 43 5f 4d 41 53 4b 29 3b 0d 0a 20 20 20 20 7d 0d  C_MASK);..    }.
1bc3f 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
1bc40 74 68 69 73 20 66 72 61 6d 65 20 73 65 74 20 63  this frame set c
1bc41 6f 6d 70 6c 65 74 65 73 20 74 68 65 20 66 69 72  ompletes the fir
1bc42 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  st transaction i
1bc43 6e 20 74 68 65 20 57 41 4c 20 61 6e 64 0d 0a 20  n the WAL and.. 
1bc44 20 2a 2a 20 69 66 20 50 52 41 47 4d 41 20 6a 6f   ** if PRAGMA jo
1bc45 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
1bc46 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 72   is set, then tr
1bc47 75 6e 63 61 74 65 20 74 68 65 20 57 41 4c 20 74  uncate the WAL t
1bc48 6f 20 74 68 65 0d 0a 20 20 2a 2a 20 6a 6f 75 72  o the..  ** jour
1bc49 6e 61 6c 20 73 69 7a 65 20 6c 69 6d 69 74 2c 20  nal size limit, 
1bc4a 69 66 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 20 20  if possible...  
1bc4b 2a 2f 0d 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d  */..  if( isComm
1bc4c 69 74 20 26 26 20 70 57 61 6c 2d 3e 74 72 75 6e  it && pWal->trun
1bc4d 63 61 74 65 4f 6e 43 6f 6d 6d 69 74 20 26 26 20  cateOnCommit && 
1bc4e 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 3e  pWal->mxWalSize>
1bc4f 3d 30 20 29 7b 0d 0a 20 20 20 20 69 36 34 20 73  =0 ){..    i64 s
1bc50 7a 20 3d 20 70 57 61 6c 2d 3e 6d 78 57 61 6c 53  z = pWal->mxWalS
1bc51 69 7a 65 3b 0d 0a 20 20 20 20 69 66 28 20 77 61  ize;..    if( wa
1bc52 6c 46 72 61 6d 65 4f 66 66 73 65 74 28 69 46 72  lFrameOffset(iFr
1bc53 61 6d 65 2b 6e 45 78 74 72 61 2b 31 2c 20 73 7a  ame+nExtra+1, sz
1bc54 50 61 67 65 29 3e 70 57 61 6c 2d 3e 6d 78 57 61  Page)>pWal->mxWa
1bc55 6c 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20  lSize ){..      
1bc56 73 7a 20 3d 20 77 61 6c 46 72 61 6d 65 4f 66 66  sz = walFrameOff
1bc57 73 65 74 28 69 46 72 61 6d 65 2b 6e 45 78 74 72  set(iFrame+nExtr
1bc58 61 2b 31 2c 20 73 7a 50 61 67 65 29 3b 0d 0a 20  a+1, szPage);.. 
1bc59 20 20 20 7d 0d 0a 20 20 20 20 77 61 6c 4c 69 6d     }..    walLim
1bc5a 69 74 53 69 7a 65 28 70 57 61 6c 2c 20 73 7a 29  itSize(pWal, sz)
1bc5b 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e 74 72 75  ;..    pWal->tru
1bc5c 6e 63 61 74 65 4f 6e 43 6f 6d 6d 69 74 20 3d 20  ncateOnCommit = 
1bc5d 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  0;..  }....  /* 
1bc5e 41 70 70 65 6e 64 20 64 61 74 61 20 74 6f 20 74  Append data to t
1bc5f 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 20 49 74  he wal-index. It
1bc60 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
1bc61 79 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 0d 0a  y to lock the ..
1bc62 20 20 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 20 74    ** wal-index t
1bc63 6f 20 64 6f 20 74 68 69 73 20 61 73 20 74 68 65  o do this as the
1bc64 20 53 51 4c 49 54 45 5f 53 48 4d 5f 57 52 49 54   SQLITE_SHM_WRIT
1bc65 45 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  E lock held on t
1bc66 68 65 20 77 61 6c 2d 69 6e 64 65 78 0d 0a 20 20  he wal-index..  
1bc67 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  ** guarantees th
1bc68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  at there are no 
1bc69 6f 74 68 65 72 20 77 72 69 74 65 72 73 2c 20 61  other writers, a
1bc6a 6e 64 20 6e 6f 20 64 61 74 61 20 74 68 61 74 20  nd no data that 
1bc6b 6d 61 79 0d 0a 20 20 2a 2a 20 62 65 20 69 6e 20  may..  ** be in 
1bc6c 75 73 65 20 62 79 20 65 78 69 73 74 69 6e 67 20  use by existing 
1bc6d 72 65 61 64 65 72 73 20 69 73 20 62 65 69 6e 67  readers is being
1bc6e 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0d 0a 20   overwritten... 
1bc6f 20 2a 2f 0d 0a 20 20 69 46 72 61 6d 65 20 3d 20   */..  iFrame = 
1bc70 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1bc71 65 3b 0d 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73  e;..  for(p=pLis
1bc72 74 3b 20 70 20 26 26 20 72 63 3d 3d 53 51 4c 49  t; p && rc==SQLI
1bc73 54 45 5f 4f 4b 3b 20 70 3d 70 2d 3e 70 44 69 72  TE_OK; p=p->pDir
1bc74 74 79 29 7b 0d 0a 20 20 20 20 69 46 72 61 6d 65  ty){..    iFrame
1bc75 2b 2b 3b 0d 0a 20 20 20 20 72 63 20 3d 20 77 61  ++;..    rc = wa
1bc76 6c 49 6e 64 65 78 41 70 70 65 6e 64 28 70 57 61  lIndexAppend(pWa
1bc77 6c 2c 20 69 46 72 61 6d 65 2c 20 70 2d 3e 70 67  l, iFrame, p->pg
1bc78 6e 6f 29 3b 0d 0a 20 20 7d 0d 0a 20 20 77 68 69  no);..  }..  whi
1bc79 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
1bc7a 4b 20 26 26 20 6e 45 78 74 72 61 3e 30 20 29 7b  K && nExtra>0 ){
1bc7b 0d 0a 20 20 20 20 69 46 72 61 6d 65 2b 2b 3b 0d  ..    iFrame++;.
1bc7c 0a 20 20 20 20 6e 45 78 74 72 61 2d 2d 3b 0d 0a  .    nExtra--;..
1bc7d 20 20 20 20 72 63 20 3d 20 77 61 6c 49 6e 64 65      rc = walInde
1bc7e 78 41 70 70 65 6e 64 28 70 57 61 6c 2c 20 69 46  xAppend(pWal, iF
1bc7f 72 61 6d 65 2c 20 70 4c 61 73 74 2d 3e 70 67 6e  rame, pLast->pgn
1bc80 6f 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  o);..  }....  if
1bc81 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1bc82 29 7b 0d 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  ){..    /* Updat
1bc83 65 20 74 68 65 20 70 72 69 76 61 74 65 20 63 6f  e the private co
1bc84 70 79 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  py of the header
1bc85 2e 20 2a 2f 0d 0a 20 20 20 20 70 57 61 6c 2d 3e  . */..    pWal->
1bc86 68 64 72 2e 73 7a 50 61 67 65 20 3d 20 28 75 31  hdr.szPage = (u1
1bc87 36 29 28 28 73 7a 50 61 67 65 26 30 78 66 66 30  6)((szPage&0xff0
1bc88 30 29 20 7c 20 28 73 7a 50 61 67 65 3e 3e 31 36  0) | (szPage>>16
1bc89 29 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73  ));..    testcas
1bc8a 65 28 20 73 7a 50 61 67 65 3c 3d 33 32 37 36 38  e( szPage<=32768
1bc8b 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73   );..    testcas
1bc8c 65 28 20 73 7a 50 61 67 65 3e 3d 36 35 35 33 36  e( szPage>=65536
1bc8d 20 29 3b 0d 0a 20 20 20 20 70 57 61 6c 2d 3e 68   );..    pWal->h
1bc8e 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 69 46 72  dr.mxFrame = iFr
1bc8f 61 6d 65 3b 0d 0a 20 20 20 20 69 66 28 20 69 73  ame;..    if( is
1bc90 43 6f 6d 6d 69 74 20 29 7b 0d 0a 20 20 20 20 20  Commit ){..     
1bc91 20 70 57 61 6c 2d 3e 68 64 72 2e 69 43 68 61 6e   pWal->hdr.iChan
1bc92 67 65 2b 2b 3b 0d 0a 20 20 20 20 20 20 70 57 61  ge++;..      pWa
1bc93 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 20 3d 20 6e  l->hdr.nPage = n
1bc94 54 72 75 6e 63 61 74 65 3b 0d 0a 20 20 20 20 7d  Truncate;..    }
1bc95 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
1bc96 20 69 73 20 61 20 63 6f 6d 6d 69 74 2c 20 75 70   is a commit, up
1bc97 64 61 74 65 20 74 68 65 20 77 61 6c 2d 69 6e 64  date the wal-ind
1bc98 65 78 20 68 65 61 64 65 72 20 74 6f 6f 2e 20 2a  ex header too. *
1bc99 2f 0d 0a 20 20 20 20 69 66 28 20 69 73 43 6f 6d  /..    if( isCom
1bc9a 6d 69 74 20 29 7b 0d 0a 20 20 20 20 20 20 77 61  mit ){..      wa
1bc9b 6c 49 6e 64 65 78 57 72 69 74 65 48 64 72 28 70  lIndexWriteHdr(p
1bc9c 57 61 6c 29 3b 0d 0a 20 20 20 20 20 20 70 57 61  Wal);..      pWa
1bc9d 6c 2d 3e 69 43 61 6c 6c 62 61 63 6b 20 3d 20 69  l->iCallback = i
1bc9e 46 72 61 6d 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Frame;..    }.. 
1bc9f 20 7d 0d 0a 0d 0a 20 20 57 41 4c 54 52 41 43 45   }....  WALTRACE
1bca0 28 28 22 57 41 4c 25 70 3a 20 66 72 61 6d 65 20  (("WAL%p: frame 
1bca1 77 72 69 74 65 20 25 73 5c 6e 22 2c 20 70 57 61  write %s\n", pWa
1bca2 6c 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22  l, rc ? "failed"
1bca3 20 3a 20 22 6f 6b 22 29 29 3b 0d 0a 20 20 72 65   : "ok"));..  re
1bca4 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
1bca5 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  * ..** This rout
1bca6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
1bca7 20 69 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74   implement sqlit
1bca8 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
1bca9 74 28 29 20 61 6e 64 0d 0a 2a 2a 20 72 65 6c 61  t() and..** rela
1bcaa 74 65 64 20 69 6e 74 65 72 66 61 63 65 73 2e 0d  ted interfaces..
1bcab 0a 2a 2a 0d 0a 2a 2a 20 4f 62 74 61 69 6e 20 61  .**..** Obtain a
1bcac 20 43 48 45 43 4b 50 4f 49 4e 54 20 6c 6f 63 6b   CHECKPOINT lock
1bcad 20 61 6e 64 20 74 68 65 6e 20 62 61 63 6b 66 69   and then backfi
1bcae 6c 6c 20 61 73 20 6d 75 63 68 20 69 6e 66 6f 72  ll as much infor
1bcaf 6d 61 74 69 6f 6e 20 61 73 0d 0a 2a 2a 20 77 65  mation as..** we
1bcb0 20 63 61 6e 20 66 72 6f 6d 20 57 41 4c 20 69 6e   can from WAL in
1bcb1 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
1bcb2 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 61 72 61  ..**..** If para
1bcb3 6d 65 74 65 72 20 78 42 75 73 79 20 69 73 20 6e  meter xBusy is n
1bcb4 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 69 73 20 61  ot NULL, it is a
1bcb5 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 75   pointer to a bu
1bcb6 73 79 2d 68 61 6e 64 6c 65 72 0d 0a 2a 2a 20 63  sy-handler..** c
1bcb7 61 6c 6c 62 61 63 6b 2e 20 49 6e 20 74 68 69 73  allback. In this
1bcb8 20 63 61 73 65 20 74 68 69 73 20 66 75 6e 63 74   case this funct
1bcb9 69 6f 6e 20 72 75 6e 73 20 61 20 62 6c 6f 63 6b  ion runs a block
1bcba 69 6e 67 20 63 68 65 63 6b 70 6f 69 6e 74 2e 0d  ing checkpoint..
1bcbb 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1bcbc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1bcbd 61 6c 43 68 65 63 6b 70 6f 69 6e 74 28 0d 0a 20  alCheckpoint(.. 
1bcbe 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20   Wal *pWal,     
1bcbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcc0 20 2f 2a 20 57 61 6c 20 63 6f 6e 6e 65 63 74 69   /* Wal connecti
1bcc1 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 4d 6f  on */..  int eMo
1bcc2 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  de,             
1bcc3 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 53 53           /* PASS
1bcc4 49 56 45 2c 20 46 55 4c 4c 20 6f 72 20 52 45 53  IVE, FULL or RES
1bcc5 54 41 52 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 28  TART */..  int (
1bcc6 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 29 2c 20  *xBusy)(void*), 
1bcc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75             /* Fu
1bcc8 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77  nction to call w
1bcc9 68 65 6e 20 62 75 73 79 20 2a 2f 0d 0a 20 20 76  hen busy */..  v
1bcca 6f 69 64 20 2a 70 42 75 73 79 41 72 67 2c 20 20  oid *pBusyArg,  
1bccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1bccc 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65  * Context argume
1bccd 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64  nt for xBusyHand
1bcce 6c 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 79  ler */..  int sy
1bccf 6e 63 5f 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nc_flags,       
1bcd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1bcd1 67 73 20 74 6f 20 73 79 6e 63 20 64 62 20 66 69  gs to sync db fi
1bcd2 6c 65 20 77 69 74 68 20 28 6f 72 20 30 29 20 2a  le with (or 0) *
1bcd3 2f 0d 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20 20  /..  int nBuf,  
1bcd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcd5 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1bcd6 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72  temporary buffer
1bcd7 20 2a 2f 0d 0a 20 20 75 38 20 2a 7a 42 75 66 2c   */..  u8 *zBuf,
1bcd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcd9 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
1bcda 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73  ary buffer to us
1bcdb 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 4c  e */..  int *pnL
1bcdc 6f 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  og,             
1bcdd 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
1bcde 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73  Number of frames
1bcdf 20 69 6e 20 57 41 4c 20 2a 2f 0d 0a 20 20 69 6e   in WAL */..  in
1bce0 74 20 2a 70 6e 43 6b 70 74 20 20 20 20 20 20 20  t *pnCkpt       
1bce1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bce2 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
1bce3 62 61 63 6b 66 69 6c 6c 65 64 20 66 72 61 6d 65  backfilled frame
1bce4 73 20 69 6e 20 57 41 4c 20 2a 2f 0d 0a 29 7b 0d  s in WAL */..){.
1bce5 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1bce6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bce7 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
1bce8 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 43 68  e */..  int isCh
1bce9 61 6e 67 65 64 20 3d 20 30 3b 20 20 20 20 20 20  anged = 0;      
1bcea 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1bceb 69 66 20 61 20 6e 65 77 20 77 61 6c 2d 69 6e 64  if a new wal-ind
1bcec 65 78 20 68 65 61 64 65 72 20 69 73 20 6c 6f 61  ex header is loa
1bced 64 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 4d  ded */..  int eM
1bcee 6f 64 65 32 20 3d 20 65 4d 6f 64 65 3b 20 20 20  ode2 = eMode;   
1bcef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64            /* Mod
1bcf0 65 20 74 6f 20 70 61 73 73 20 74 6f 20 77 61 6c  e to pass to wal
1bcf1 43 68 65 63 6b 70 6f 69 6e 74 28 29 20 2a 2f 0d  Checkpoint() */.
1bcf2 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  ...  assert( pWa
1bcf3 6c 2d 3e 63 6b 70 74 4c 6f 63 6b 3d 3d 30 20 29  l->ckptLock==0 )
1bcf4 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  ;..  assert( pWa
1bcf5 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d 3d 30 20  l->writeLock==0 
1bcf6 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 57 61 6c  );....  if( pWal
1bcf7 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 20 72 65 74  ->readOnly ) ret
1bcf8 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
1bcf9 4e 4c 59 3b 0d 0a 20 20 57 41 4c 54 52 41 43 45  NLY;..  WALTRACE
1bcfa 28 28 22 57 41 4c 25 70 3a 20 63 68 65 63 6b 70  (("WAL%p: checkp
1bcfb 6f 69 6e 74 20 62 65 67 69 6e 73 5c 6e 22 2c 20  oint begins\n", 
1bcfc 70 57 61 6c 29 29 3b 0d 0a 20 20 72 63 20 3d 20  pWal));..  rc = 
1bcfd 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 65  walLockExclusive
1bcfe 28 70 57 61 6c 2c 20 57 41 4c 5f 43 4b 50 54 5f  (pWal, WAL_CKPT_
1bcff 4c 4f 43 4b 2c 20 31 29 3b 0d 0a 20 20 69 66 28  LOCK, 1);..  if(
1bd00 20 72 63 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 55   rc ){..    /* U
1bd01 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 20 53  sually this is S
1bd02 51 4c 49 54 45 5f 42 55 53 59 20 6d 65 61 6e 69  QLITE_BUSY meani
1bd03 6e 67 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  ng that another 
1bd04 74 68 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73  thread or proces
1bd05 73 0d 0a 20 20 20 20 2a 2a 20 69 73 20 61 6c 72  s..    ** is alr
1bd06 65 61 64 79 20 72 75 6e 6e 69 6e 67 20 61 20 63  eady running a c
1bd07 68 65 63 6b 70 6f 69 6e 74 2c 20 6f 72 20 6d 61  heckpoint, or ma
1bd08 79 62 65 20 61 20 72 65 63 6f 76 65 72 79 2e 20  ybe a recovery. 
1bd09 20 42 75 74 20 69 74 20 6d 69 67 68 74 0d 0a 20   But it might.. 
1bd0a 20 20 20 2a 2a 20 61 6c 73 6f 20 62 65 20 53 51     ** also be SQ
1bd0b 4c 49 54 45 5f 49 4f 45 52 52 2e 20 2a 2f 0d 0a  LITE_IOERR. */..
1bd0c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
1bd0d 20 20 7d 0d 0a 20 20 70 57 61 6c 2d 3e 63 6b 70    }..  pWal->ckp
1bd0e 74 4c 6f 63 6b 20 3d 20 31 3b 0d 0a 0d 0a 20 20  tLock = 1;....  
1bd0f 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
1bd10 62 6c 6f 63 6b 69 6e 67 2d 63 68 65 63 6b 70 6f  blocking-checkpo
1bd11 69 6e 74 2c 20 74 68 65 6e 20 6f 62 74 61 69 6e  int, then obtain
1bd12 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20   the write-lock 
1bd13 61 73 20 77 65 6c 6c 0d 0a 20 20 2a 2a 20 74 6f  as well..  ** to
1bd14 20 70 72 65 76 65 6e 74 20 61 6e 79 20 77 72 69   prevent any wri
1bd15 74 65 72 73 20 66 72 6f 6d 20 72 75 6e 6e 69 6e  ters from runnin
1bd16 67 20 77 68 69 6c 65 20 74 68 65 20 63 68 65 63  g while the chec
1bd17 6b 70 6f 69 6e 74 20 69 73 20 75 6e 64 65 72 77  kpoint is underw
1bd18 61 79 2e 0d 0a 20 20 2a 2a 20 54 68 69 73 20 68  ay...  ** This h
1bd19 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 62 65  as to be done be
1bd1a 66 6f 72 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  fore the call to
1bd1b 20 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64 72   walIndexReadHdr
1bd1c 28 29 20 62 65 6c 6f 77 2e 0d 0a 20 20 2a 2a 0d  () below...  **.
1bd1d 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 77 72 69  .  ** If the wri
1bd1e 74 65 72 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  ter lock cannot 
1bd1f 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 65  be obtained, the
1bd20 6e 20 61 20 70 61 73 73 69 76 65 20 63 68 65 63  n a passive chec
1bd21 6b 70 6f 69 6e 74 20 69 73 0d 0a 20 20 2a 2a 20  kpoint is..  ** 
1bd22 72 75 6e 20 69 6e 73 74 65 61 64 2e 20 53 69 6e  run instead. Sin
1bd23 63 65 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e  ce the checkpoin
1bd24 74 65 72 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69  ter is not holdi
1bd25 6e 67 20 74 68 65 20 77 72 69 74 65 72 20 6c 6f  ng the writer lo
1bd26 63 6b 2c 0d 0a 20 20 2a 2a 20 74 68 65 72 65 20  ck,..  ** there 
1bd27 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 62  is no point in b
1bd28 6c 6f 63 6b 69 6e 67 20 77 61 69 74 69 6e 67 20  locking waiting 
1bd29 66 6f 72 20 61 6e 79 20 72 65 61 64 65 72 73 2e  for any readers.
1bd2a 20 41 73 73 75 6d 69 6e 67 20 6e 6f 20 0d 0a 20   Assuming no .. 
1bd2b 20 2a 2a 20 6f 74 68 65 72 20 65 72 72 6f 72 20   ** other error 
1bd2c 6f 63 63 75 72 73 2c 20 74 68 69 73 20 66 75 6e  occurs, this fun
1bd2d 63 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72  ction will retur
1bd2e 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 74 6f  n SQLITE_BUSY to
1bd2f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0d 0a 20 20   the caller...  
1bd30 2a 2f 0d 0a 20 20 69 66 28 20 65 4d 6f 64 65 21  */..  if( eMode!
1bd31 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49  =SQLITE_CHECKPOI
1bd32 4e 54 5f 50 41 53 53 49 56 45 20 29 7b 0d 0a 20  NT_PASSIVE ){.. 
1bd33 20 20 20 72 63 20 3d 20 77 61 6c 42 75 73 79 4c     rc = walBusyL
1bd34 6f 63 6b 28 70 57 61 6c 2c 20 78 42 75 73 79 2c  ock(pWal, xBusy,
1bd35 20 70 42 75 73 79 41 72 67 2c 20 57 41 4c 5f 57   pBusyArg, WAL_W
1bd36 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0d 0a  RITE_LOCK, 1);..
1bd37 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1bd38 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
1bd39 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20  pWal->writeLock 
1bd3a 3d 20 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20  = 1;..    }else 
1bd3b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
1bd3c 55 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 65 4d  USY ){..      eM
1bd3d 6f 64 65 32 20 3d 20 53 51 4c 49 54 45 5f 43 48  ode2 = SQLITE_CH
1bd3e 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45  ECKPOINT_PASSIVE
1bd3f 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ;..      rc = SQ
1bd40 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d  LITE_OK;..    }.
1bd41 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61  .  }....  /* Rea
1bd42 64 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  d the wal-index 
1bd43 68 65 61 64 65 72 2e 20 2a 2f 0d 0a 20 20 69 66  header. */..  if
1bd44 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1bd45 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 77 61 6c  ){..    rc = wal
1bd46 49 6e 64 65 78 52 65 61 64 48 64 72 28 70 57 61  IndexReadHdr(pWa
1bd47 6c 2c 20 26 69 73 43 68 61 6e 67 65 64 29 3b 0d  l, &isChanged);.
1bd48 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 70  .  }....  /* Cop
1bd49 79 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  y data from the 
1bd4a 6c 6f 67 20 74 6f 20 74 68 65 20 64 61 74 61 62  log to the datab
1bd4b 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20  ase file. */..  
1bd4c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bd4d 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 57  K ){..    if( pW
1bd4e 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20  al->hdr.mxFrame 
1bd4f 26 26 20 77 61 6c 50 61 67 65 73 69 7a 65 28 70  && walPagesize(p
1bd50 57 61 6c 29 21 3d 6e 42 75 66 20 29 7b 0d 0a 20  Wal)!=nBuf ){.. 
1bd51 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1bd52 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a  _CORRUPT_BKPT;..
1bd53 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1bd54 20 20 72 63 20 3d 20 77 61 6c 43 68 65 63 6b 70    rc = walCheckp
1bd55 6f 69 6e 74 28 70 57 61 6c 2c 20 65 4d 6f 64 65  oint(pWal, eMode
1bd56 32 2c 20 78 42 75 73 79 2c 20 70 42 75 73 79 41  2, xBusy, pBusyA
1bd57 72 67 2c 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20  rg, sync_flags, 
1bd58 7a 42 75 66 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  zBuf);..    }...
1bd59 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 65 72  .    /* If no er
1bd5a 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 73 65  ror occurred, se
1bd5b 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72  t the output var
1bd5c 69 61 62 6c 65 73 2e 20 2a 2f 0d 0a 20 20 20 20  iables. */..    
1bd5d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bd5e 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  K || rc==SQLITE_
1bd5f 42 55 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 69  BUSY ){..      i
1bd60 66 28 20 70 6e 4c 6f 67 20 29 20 2a 70 6e 4c 6f  f( pnLog ) *pnLo
1bd61 67 20 3d 20 28 69 6e 74 29 70 57 61 6c 2d 3e 68  g = (int)pWal->h
1bd62 64 72 2e 6d 78 46 72 61 6d 65 3b 0d 0a 20 20 20  dr.mxFrame;..   
1bd63 20 20 20 69 66 28 20 70 6e 43 6b 70 74 20 29 20     if( pnCkpt ) 
1bd64 2a 70 6e 43 6b 70 74 20 3d 20 28 69 6e 74 29 28  *pnCkpt = (int)(
1bd65 77 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57 61 6c  walCkptInfo(pWal
1bd66 29 2d 3e 6e 42 61 63 6b 66 69 6c 6c 29 3b 0d 0a  )->nBackfill);..
1bd67 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
1bd68 69 66 28 20 69 73 43 68 61 6e 67 65 64 20 29 7b  if( isChanged ){
1bd69 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6e 65  ..    /* If a ne
1bd6a 77 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  w wal-index head
1bd6b 65 72 20 77 61 73 20 6c 6f 61 64 65 64 20 62 65  er was loaded be
1bd6c 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b 70 6f  fore the checkpo
1bd6d 69 6e 74 20 77 61 73 20 0d 0a 20 20 20 20 2a 2a  int was ..    **
1bd6e 20 70 65 72 66 6f 72 6d 65 64 2c 20 74 68 65 6e   performed, then
1bd6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
1bd70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1bd71 20 70 57 61 6c 20 69 73 20 6e 6f 77 0d 0a 20 20   pWal is now..  
1bd72 20 20 2a 2a 20 6f 75 74 20 6f 66 20 64 61 74 65    ** out of date
1bd73 2e 20 53 6f 20 7a 65 72 6f 20 74 68 65 20 63 61  . So zero the ca
1bd74 63 68 65 64 20 77 61 6c 2d 69 6e 64 65 78 20 68  ched wal-index h
1bd75 65 61 64 65 72 20 74 6f 20 65 6e 73 75 72 65 20  eader to ensure 
1bd76 74 68 61 74 0d 0a 20 20 20 20 2a 2a 20 6e 65 78  that..    ** nex
1bd77 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 72  t time the pager
1bd78 20 6f 70 65 6e 73 20 61 20 73 6e 61 70 73 68 6f   opens a snapsho
1bd79 74 20 6f 6e 20 74 68 69 73 20 64 61 74 61 62 61  t on this databa
1bd7a 73 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74  se it knows that
1bd7b 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63  ..    ** the cac
1bd7c 68 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  he needs to be r
1bd7d 65 73 65 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  eset...    */.. 
1bd7e 20 20 20 6d 65 6d 73 65 74 28 26 70 57 61 6c 2d     memset(&pWal-
1bd7f 3e 68 64 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  >hdr, 0, sizeof(
1bd80 57 61 6c 49 6e 64 65 78 48 64 72 29 29 3b 0d 0a  WalIndexHdr));..
1bd81 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6c 65    }....  /* Rele
1bd82 61 73 65 20 74 68 65 20 6c 6f 63 6b 73 2e 20 2a  ase the locks. *
1bd83 2f 0d 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 45  /..  sqlite3WalE
1bd84 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63 74 69  ndWriteTransacti
1bd85 6f 6e 28 70 57 61 6c 29 3b 0d 0a 20 20 77 61 6c  on(pWal);..  wal
1bd86 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69 76 65 28  UnlockExclusive(
1bd87 70 57 61 6c 2c 20 57 41 4c 5f 43 4b 50 54 5f 4c  pWal, WAL_CKPT_L
1bd88 4f 43 4b 2c 20 31 29 3b 0d 0a 20 20 70 57 61 6c  OCK, 1);..  pWal
1bd89 2d 3e 63 6b 70 74 4c 6f 63 6b 20 3d 20 30 3b 0d  ->ckptLock = 0;.
1bd8a 0a 20 20 57 41 4c 54 52 41 43 45 28 28 22 57 41  .  WALTRACE(("WA
1bd8b 4c 25 70 3a 20 63 68 65 63 6b 70 6f 69 6e 74 20  L%p: checkpoint 
1bd8c 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 72 63 20  %s\n", pWal, rc 
1bd8d 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b  ? "failed" : "ok
1bd8e 22 29 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28  "));..  return (
1bd8f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1bd90 20 65 4d 6f 64 65 21 3d 65 4d 6f 64 65 32 20 3f   eMode!=eMode2 ?
1bd91 20 53 51 4c 49 54 45 5f 42 55 53 59 20 3a 20 72   SQLITE_BUSY : r
1bd92 63 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 52 65 74  c);..}..../* Ret
1bd93 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 74 6f  urn the value to
1bd94 20 70 61 73 73 20 74 6f 20 61 20 73 71 6c 69 74   pass to a sqlit
1bd95 65 33 5f 77 61 6c 5f 68 6f 6f 6b 20 63 61 6c 6c  e3_wal_hook call
1bd96 62 61 63 6b 2c 20 74 68 65 0d 0a 2a 2a 20 6e 75  back, the..** nu
1bd97 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69  mber of frames i
1bd98 6e 20 74 68 65 20 57 41 4c 20 61 74 20 74 68 65  n the WAL at the
1bd99 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 6c 61   point of the la
1bd9a 73 74 20 63 6f 6d 6d 69 74 20 73 69 6e 63 65 0d  st commit since.
1bd9b 0a 2a 2a 20 73 71 6c 69 74 65 33 57 61 6c 43 61  .** sqlite3WalCa
1bd9c 6c 6c 62 61 63 6b 28 29 20 77 61 73 20 63 61 6c  llback() was cal
1bd9d 6c 65 64 2e 20 20 49 66 20 6e 6f 20 63 6f 6d 6d  led.  If no comm
1bd9e 69 74 73 20 68 61 76 65 20 6f 63 63 75 72 72 65  its have occurre
1bd9f 64 20 73 69 6e 63 65 0d 0a 2a 2a 20 74 68 65 20  d since..** the 
1bda0 6c 61 73 74 20 63 61 6c 6c 2c 20 74 68 65 6e 20  last call, then 
1bda1 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f 0d 0a 53  return 0...*/..S
1bda2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1bda3 74 20 73 71 6c 69 74 65 33 57 61 6c 43 61 6c 6c  t sqlite3WalCall
1bda4 62 61 63 6b 28 57 61 6c 20 2a 70 57 61 6c 29 7b  back(Wal *pWal){
1bda5 0d 0a 20 20 75 33 32 20 72 65 74 20 3d 20 30 3b  ..  u32 ret = 0;
1bda6 0d 0a 20 20 69 66 28 20 70 57 61 6c 20 29 7b 0d  ..  if( pWal ){.
1bda7 0a 20 20 20 20 72 65 74 20 3d 20 70 57 61 6c 2d  .    ret = pWal-
1bda8 3e 69 43 61 6c 6c 62 61 63 6b 3b 0d 0a 20 20 20  >iCallback;..   
1bda9 20 70 57 61 6c 2d 3e 69 43 61 6c 6c 62 61 63 6b   pWal->iCallback
1bdaa 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65   = 0;..  }..  re
1bdab 74 75 72 6e 20 28 69 6e 74 29 72 65 74 3b 0d 0a  turn (int)ret;..
1bdac 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
1bdad 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
1bdae 6c 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68  led to change th
1bdaf 65 20 57 41 4c 20 73 75 62 73 79 73 74 65 6d 20  e WAL subsystem 
1bdb0 69 6e 74 6f 20 6f 72 20 6f 75 74 0d 0a 2a 2a 20  into or out..** 
1bdb1 6f 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  of locking_mode=
1bdb2 45 58 43 4c 55 53 49 56 45 2e 0d 0a 2a 2a 0d 0a  EXCLUSIVE...**..
1bdb3 2a 2a 20 49 66 20 6f 70 20 69 73 20 7a 65 72 6f  ** If op is zero
1bdb4 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74  , then attempt t
1bdb5 6f 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6c 6f  o change from lo
1bdb6 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55  cking_mode=EXCLU
1bdb7 53 49 56 45 0d 0a 2a 2a 20 69 6e 74 6f 20 6c 6f  SIVE..** into lo
1bdb8 63 6b 69 6e 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41  cking_mode=NORMA
1bdb9 4c 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74  L.  This means t
1bdba 68 61 74 20 77 65 20 6d 75 73 74 20 61 63 71 75  hat we must acqu
1bdbb 69 72 65 20 61 20 6c 6f 63 6b 0d 0a 2a 2a 20 6f  ire a lock..** o
1bdbc 6e 20 74 68 65 20 70 57 61 6c 2d 3e 72 65 61 64  n the pWal->read
1bdbd 4c 6f 63 6b 20 62 79 74 65 2e 20 20 49 66 20 74  Lock byte.  If t
1bdbe 68 65 20 57 41 4c 20 69 73 20 61 6c 72 65 61 64  he WAL is alread
1bdbf 79 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  y in locking_mod
1bdc0 65 3d 4e 4f 52 4d 41 4c 0d 0a 2a 2a 20 6f 72 20  e=NORMAL..** or 
1bdc1 69 66 20 74 68 65 20 61 63 71 75 69 73 69 74 69  if the acquisiti
1bdc2 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 66  on of the lock f
1bdc3 61 69 6c 73 2c 20 74 68 65 6e 20 72 65 74 75 72  ails, then retur
1bdc4 6e 20 30 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a  n 0.  If the..**
1bdc5 20 74 72 61 6e 73 69 74 69 6f 6e 20 6f 75 74 20   transition out 
1bdc6 6f 66 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64  of exclusive-mod
1bdc7 65 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  e is successful,
1bdc8 20 72 65 74 75 72 6e 20 31 2e 20 20 54 68 69 73   return 1.  This
1bdc9 0d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6d  ..** operation m
1bdca 75 73 74 20 6f 63 63 75 72 20 77 68 69 6c 65 20  ust occur while 
1bdcb 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69  the pager is sti
1bdcc 6c 6c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 65  ll holding the e
1bdcd 78 63 6c 75 73 69 76 65 0d 0a 2a 2a 20 6c 6f 63  xclusive..** loc
1bdce 6b 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  k on the main da
1bdcf 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a  tabase file...**
1bdd0 0d 0a 2a 2a 20 49 66 20 6f 70 20 69 73 20 6f 6e  ..** If op is on
1bdd1 65 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 66  e, then change f
1bdd2 72 6f 6d 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  rom locking_mode
1bdd3 3d 4e 4f 52 4d 41 4c 20 69 6e 74 6f 20 0d 0a 2a  =NORMAL into ..*
1bdd4 2a 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45  * locking_mode=E
1bdd5 58 43 4c 55 53 49 56 45 2e 20 20 54 68 69 73 20  XCLUSIVE.  This 
1bdd6 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70  means that the p
1bdd7 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20 6d 75  Wal->readLock mu
1bdd8 73 74 0d 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73  st..** be releas
1bdd9 65 64 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  ed.  Return 1 if
1bdda 20 74 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20   the transition 
1bddb 69 73 20 6d 61 64 65 20 61 6e 64 20 30 20 69 66  is made and 0 if
1bddc 20 74 68 65 0d 0a 2a 2a 20 57 41 4c 20 69 73 20   the..** WAL is 
1bddd 61 6c 72 65 61 64 79 20 69 6e 20 65 78 63 6c 75  already in exclu
1bdde 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64  sive-locking mod
1bddf 65 20 2d 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  e - meaning that
1bde0 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e   this..** routin
1bde1 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54  e is a no-op.  T
1bde2 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 61 6c  he pager must al
1bde3 72 65 61 64 79 20 68 6f 6c 64 20 74 68 65 20 65  ready hold the e
1bde4 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0d 0a 2a  xclusive lock..*
1bde5 2a 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  * on the main da
1bde6 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f  tabase file befo
1bde7 72 65 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  re invoking this
1bde8 20 6f 70 65 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d   operation...**.
1bde9 0a 2a 2a 20 49 66 20 6f 70 20 69 73 20 6e 65 67  .** If op is neg
1bdea 61 74 69 76 65 2c 20 74 68 65 6e 20 64 6f 20 61  ative, then do a
1bdeb 20 64 72 79 2d 72 75 6e 20 6f 66 20 74 68 65 20   dry-run of the 
1bdec 6f 70 3d 3d 31 20 63 61 73 65 20 62 75 74 20 64  op==1 case but d
1bded 6f 0d 0a 2a 2a 20 6e 6f 74 20 61 63 74 75 61 6c  o..** not actual
1bdee 6c 79 20 63 68 61 6e 67 65 20 61 6e 79 74 68 69  ly change anythi
1bdef 6e 67 2e 20 54 68 65 20 70 61 67 65 72 20 75 73  ng. The pager us
1bdf0 65 73 20 74 68 69 73 20 74 6f 20 73 65 65 20 69  es this to see i
1bdf1 66 20 69 74 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20  f it..** should 
1bdf2 61 63 71 75 69 72 65 20 74 68 65 20 64 61 74 61  acquire the data
1bdf3 62 61 73 65 20 65 78 63 6c 75 73 69 76 65 20 6c  base exclusive l
1bdf4 6f 63 6b 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  ock prior to inv
1bdf5 6f 6b 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 6f 70  oking..** the op
1bdf6 3d 3d 31 20 63 61 73 65 2e 0d 0a 2a 2f 0d 0a 53  ==1 case...*/..S
1bdf7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1bdf8 74 20 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c  t sqlite3WalExcl
1bdf9 75 73 69 76 65 4d 6f 64 65 28 57 61 6c 20 2a 70  usiveMode(Wal *p
1bdfa 57 61 6c 2c 20 69 6e 74 20 6f 70 29 7b 0d 0a 20  Wal, int op){.. 
1bdfb 20 69 6e 74 20 72 63 3b 0d 0a 20 20 61 73 73 65   int rc;..  asse
1bdfc 72 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c  rt( pWal->writeL
1bdfd 6f 63 6b 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73  ock==0 );..  ass
1bdfe 65 72 74 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75  ert( pWal->exclu
1bdff 73 69 76 65 4d 6f 64 65 21 3d 57 41 4c 5f 48 45  siveMode!=WAL_HE
1be00 41 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 7c 7c  APMEMORY_MODE ||
1be01 20 6f 70 3d 3d 2d 31 20 29 3b 0d 0a 0d 0a 20 20   op==-1 );....  
1be02 2f 2a 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  /* pWal->readLoc
1be03 6b 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65 74  k is usually set
1be04 2c 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 2d  , but might be -
1be05 31 20 69 66 20 74 68 65 72 65 20 77 61 73 20 61  1 if there was a
1be06 20 0d 0a 20 20 2a 2a 20 70 72 69 6f 72 20 65 72   ..  ** prior er
1be07 72 6f 72 20 77 68 69 6c 65 20 61 74 74 65 6d 70  ror while attemp
1be08 74 69 6e 67 20 74 6f 20 61 63 71 75 69 72 65 20  ting to acquire 
1be09 61 72 65 20 72 65 61 64 2d 6c 6f 63 6b 2e 20 54  are read-lock. T
1be0a 68 69 73 20 63 61 6e 6e 6f 74 20 0d 0a 20 20 2a  his cannot ..  *
1be0b 2a 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20  * happen if the 
1be0c 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 63  connection is ac
1be0d 74 75 61 6c 6c 79 20 69 6e 20 65 78 63 6c 75 73  tually in exclus
1be0e 69 76 65 20 6d 6f 64 65 20 28 61 73 20 6e 6f 20  ive mode (as no 
1be0f 78 53 68 6d 4c 6f 63 6b 0d 0a 20 20 2a 2a 20 6c  xShmLock..  ** l
1be10 6f 63 6b 73 20 61 72 65 20 74 61 6b 65 6e 20 69  ocks are taken i
1be11 6e 20 74 68 69 73 20 63 61 73 65 29 2e 20 4e 6f  n this case). No
1be12 72 20 73 68 6f 75 6c 64 20 74 68 65 20 70 61 67  r should the pag
1be13 65 72 20 61 74 74 65 6d 70 74 20 74 6f 0d 0a 20  er attempt to.. 
1be14 20 2a 2a 20 75 70 67 72 61 64 65 20 74 6f 20 65   ** upgrade to e
1be15 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 66 6f  xclusive-mode fo
1be16 6c 6c 6f 77 69 6e 67 20 73 75 63 68 20 61 6e 20  llowing such an 
1be17 65 72 72 6f 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  error...  */..  
1be18 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72 65  assert( pWal->re
1be19 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20 70 57 61  adLock>=0 || pWa
1be1a 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 29 3b 0d  l->lockError );.
1be1b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d  .  assert( pWal-
1be1c 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20  >readLock>=0 || 
1be1d 28 6f 70 3c 3d 30 20 26 26 20 70 57 61 6c 2d 3e  (op<=0 && pWal->
1be1e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 30  exclusiveMode==0
1be1f 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 6f 70  ) );....  if( op
1be20 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  ==0 ){..    if( 
1be21 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65 4d  pWal->exclusiveM
1be22 6f 64 65 20 29 7b 0d 0a 20 20 20 20 20 20 70 57  ode ){..      pW
1be23 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  al->exclusiveMod
1be24 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66  e = 0;..      if
1be25 28 20 77 61 6c 4c 6f 63 6b 53 68 61 72 65 64 28  ( walLockShared(
1be26 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c  pWal, WAL_READ_L
1be27 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f  OCK(pWal->readLo
1be28 63 6b 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ck))!=SQLITE_OK 
1be29 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 57 61 6c  ){..        pWal
1be2a 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
1be2b 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  = 1;..      }.. 
1be2c 20 20 20 20 20 72 63 20 3d 20 70 57 61 6c 2d 3e       rc = pWal->
1be2d 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 30  exclusiveMode==0
1be2e 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
1be2f 20 20 20 20 20 2f 2a 20 41 6c 72 65 61 64 79 20       /* Already 
1be30 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  in locking_mode=
1be31 4e 4f 52 4d 41 4c 20 2a 2f 0d 0a 20 20 20 20 20  NORMAL */..     
1be32 20 72 63 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d   rc = 0;..    }.
1be33 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3e  .  }else if( op>
1be34 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  0 ){..    assert
1be35 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76  ( pWal->exclusiv
1be36 65 4d 6f 64 65 3d 3d 30 20 29 3b 0d 0a 20 20 20  eMode==0 );..   
1be37 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72   assert( pWal->r
1be38 65 61 64 4c 6f 63 6b 3e 3d 30 20 29 3b 0d 0a 20  eadLock>=0 );.. 
1be39 20 20 20 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72     walUnlockShar
1be3a 65 64 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41  ed(pWal, WAL_REA
1be3b 44 5f 4c 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61  D_LOCK(pWal->rea
1be3c 64 4c 6f 63 6b 29 29 3b 0d 0a 20 20 20 20 70 57  dLock));..    pW
1be3d 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  al->exclusiveMod
1be3e 65 20 3d 20 31 3b 0d 0a 20 20 20 20 72 63 20 3d  e = 1;..    rc =
1be3f 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   1;..  }else{.. 
1be40 20 20 20 72 63 20 3d 20 70 57 61 6c 2d 3e 65 78     rc = pWal->ex
1be41 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 30 3b 0d  clusiveMode==0;.
1be42 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
1be43 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a  c;..}..../* ..**
1be44 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1be45 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
1be46 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 74 68 65  non-NULL and the
1be47 20 57 41 4c 20 6d 6f 64 75 6c 65 20 69 73 20 75   WAL module is u
1be48 73 69 6e 67 0d 0a 2a 2a 20 68 65 61 70 2d 6d 65  sing..** heap-me
1be49 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 77 61 6c  mory for the wal
1be4a 2d 69 6e 64 65 78 2e 20 4f 74 68 65 72 77 69 73  -index. Otherwis
1be4b 65 2c 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  e, if the argume
1be4c 6e 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 74 68  nt is NULL or th
1be4d 65 0d 0a 2a 2a 20 57 41 4c 20 6d 6f 64 75 6c 65  e..** WAL module
1be4e 20 69 73 20 75 73 69 6e 67 20 73 68 61 72 65 64   is using shared
1be4f 2d 6d 65 6d 6f 72 79 2c 20 72 65 74 75 72 6e 20  -memory, return 
1be50 66 61 6c 73 65 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c  false. ..*/..SQL
1be51 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1be52 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70 4d 65  sqlite3WalHeapMe
1be53 6d 6f 72 79 28 57 61 6c 20 2a 70 57 61 6c 29 7b  mory(Wal *pWal){
1be54 0d 0a 20 20 72 65 74 75 72 6e 20 28 70 57 61 6c  ..  return (pWal
1be55 20 26 26 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73   && pWal->exclus
1be56 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f 48 45 41  iveMode==WAL_HEA
1be57 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 3b 0d  PMEMORY_MODE );.
1be58 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  .}....#endif /* 
1be59 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1be5a 4d 49 54 5f 57 41 4c 20 2a 2f 0d 0a 0d 0a 2f 2a  MIT_WAL */..../*
1be5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1be5c 64 20 6f 66 20 77 61 6c 2e 63 20 2a 2a 2a 2a 2a  d of wal.c *****
1be5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
1be60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1be61 65 67 69 6e 20 66 69 6c 65 20 62 74 6d 75 74 65  egin file btmute
1be62 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
1be63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
1be65 2f 2a 0d 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  /*..** 2007 Augu
1be66 73 74 20 32 37 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  st 27..**..** Th
1be67 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1be68 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1be69 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1be6a 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
1be6b 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1be6c 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1be6d 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
1be6e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1be6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
1be70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1be71 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1be72 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1be73 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1be74 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1be75 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1be76 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1be77 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
1be78 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
1be79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
1be7d 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
1be7e 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1be7f 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1be80 6d 75 74 65 78 65 73 20 6f 6e 20 42 74 72 65 65  mutexes on Btree
1be81 20 6f 62 6a 65 63 74 73 2e 0d 0a 2a 2a 20 54 68   objects...** Th
1be82 69 73 20 63 6f 64 65 20 72 65 61 6c 6c 79 20 62  is code really b
1be83 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 65 65 2e  elongs in btree.
1be84 63 2e 20 20 42 75 74 20 62 74 72 65 65 2e 63 20  c.  But btree.c 
1be85 69 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 0d 0a  is getting too..
1be86 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 77 61  ** big and we wa
1be87 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 20 64  nt to break it d
1be88 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 73 20  own some.  This 
1be89 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 64 20  packaged seemed 
1be8a 6c 69 6b 65 0d 0a 2a 2a 20 61 20 67 6f 6f 64 20  like..** a good 
1be8b 62 72 65 61 6b 6f 75 74 2e 0d 0a 2a 2f 0d 0a 2f  breakout...*/../
1be8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
1be8d 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e  nclude btreeInt.
1be8e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
1be8f 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a  of btmutex.c ***
1be90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
1be91 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1be92 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65  Begin file btree
1be93 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
1be94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
1be96 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 34 20 41 70 72  ./*..** 2004 Apr
1be97 69 6c 20 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  il 6..**..** The
1be98 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1be99 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1be9a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1be9b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
1be9c 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1be9d 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1be9e 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
1be9f 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1bea0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
1bea1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1bea2 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1bea3 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1bea4 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
1bea5 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1bea6 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1bea7 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1bea8 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
1bea9 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
1beaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1beab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1beac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
1beae 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
1beaf 6d 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c  ments a external
1beb0 20 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61   (disk-based) da
1beb1 74 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 72  tabase using BTr
1beb2 65 65 73 2e 0d 0a 2a 2a 20 46 6f 72 20 61 20 64  ees...** For a d
1beb3 65 74 61 69 6c 65 64 20 64 69 73 63 75 73 73 69  etailed discussi
1beb4 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c 20 72 65  on of BTrees, re
1beb5 66 65 72 20 74 6f 0d 0a 2a 2a 0d 0a 2a 2a 20 20  fer to..**..**  
1beb6 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75     Donald E. Knu
1beb7 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 20 43  th, THE ART OF C
1beb8 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 4d 4d  OMPUTER PROGRAMM
1beb9 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0d 0a  ING, Volume 3:..
1beba 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 20  **     "Sorting 
1bebb 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c 20  And Searching", 
1bebc 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 41  pages 473-480. A
1bebd 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0d 0a 2a  ddison-Wesley..*
1bebe 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67  *     Publishing
1bebf 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e   Company, Readin
1bec0 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74 73  g, Massachusetts
1bec1 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 62 61  ...**..** The ba
1bec2 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 74  sic idea is that
1bec3 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
1bec4 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  e file contains 
1bec5 4e 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 65  N database..** e
1bec6 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70  ntries and N+1 p
1bec7 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61  ointers to subpa
1bec8 67 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2d  ges...**..**   -
1bec9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1beca 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1becb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1becc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d  ---------------.
1becd 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 29 20  .**   |  Ptr(0) 
1bece 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 28 31  | Key(0) | Ptr(1
1becf 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e  ) | Key(1) | ...
1bed0 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74   | Key(N-1) | Pt
1bed1 72 28 4e 29 20 7c 0d 0a 2a 2a 20 20 20 2d 2d 2d  r(N) |..**   ---
1bed2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bed3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bed4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bed5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a  -------------..*
1bed6 2a 0d 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  *..** All of the
1bed7 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67   keys on the pag
1bed8 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f  e that Ptr(0) po
1bed9 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c  ints to have val
1beda 75 65 73 20 6c 65 73 73 0d 0a 2a 2a 20 74 68 61  ues less..** tha
1bedb 6e 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f  n Key(0).  All o
1bedc 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61  f the keys on pa
1bedd 67 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74  ge Ptr(1) and it
1bede 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0d  s subpages have.
1bedf 0a 2a 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74  .** values great
1bee0 65 72 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61  er than Key(0) a
1bee1 6e 64 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79  nd less than Key
1bee2 28 31 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65  (1).  All of the
1bee3 20 6b 65 79 73 0d 0a 2a 2a 20 6f 6e 20 50 74 72   keys..** on Ptr
1bee4 28 4e 29 20 61 6e 64 20 69 74 73 20 73 75 62 70  (N) and its subp
1bee5 61 67 65 73 20 68 61 76 65 20 76 61 6c 75 65 73  ages have values
1bee6 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65   greater than Ke
1bee7 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0d 0a 2a 2a  y(N-1).  And..**
1bee8 20 73 6f 20 66 6f 72 74 68 2e 0d 0a 2a 2a 0d 0a   so forth...**..
1bee9 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 72  ** Finding a par
1beea 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 75  ticular key requ
1beeb 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 6c  ires reading O(l
1beec 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 6f  og(M)) pages fro
1beed 6d 20 74 68 65 20 0d 0a 2a 2a 20 64 69 73 6b 20  m the ..** disk 
1beee 77 68 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e  where M is the n
1beef 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
1bef0 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0d 0a 2a   in the tree...*
1bef1 2a 0d 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d  *..** In this im
1bef2 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20  plementation, a 
1bef3 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20  single file can 
1bef4 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  hold one or more
1bef5 20 73 65 70 61 72 61 74 65 20 0d 0a 2a 2a 20 42   separate ..** B
1bef6 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72  Trees.  Each BTr
1bef7 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ee is identified
1bef8 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66   by the index of
1bef9 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20   its root page. 
1befa 20 54 68 65 0d 0a 2a 2a 20 6b 65 79 20 61 6e 64   The..** key and
1befb 20 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e   data for any en
1befc 74 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64  try are combined
1befd 20 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61   to form the "pa
1befe 79 6c 6f 61 64 22 2e 20 20 41 0d 0a 2a 2a 20 66  yload".  A..** f
1beff 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70  ixed amount of p
1bf00 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61  ayload can be ca
1bf01 72 72 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f  rried directly o
1bf02 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0d 0a  n the database..
1bf03 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 68 65  ** page.  If the
1bf04 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 72 67   payload is larg
1bf05 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 73  er than the pres
1bf06 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73  et amount then s
1bf07 75 72 70 6c 75 73 0d 0a 2a 2a 20 62 79 74 65 73  urplus..** bytes
1bf08 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f   are stored on o
1bf09 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20  verflow pages.  
1bf0a 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20  The payload for 
1bf0b 61 6e 20 65 6e 74 72 79 0d 0a 2a 2a 20 61 6e 64  an entry..** and
1bf0c 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70   the preceding p
1bf0d 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69  ointer are combi
1bf0e 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43  ned to form a "C
1bf0f 65 6c 6c 22 2e 20 20 45 61 63 68 20 0d 0a 2a 2a  ell".  Each ..**
1bf10 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c   page has a smal
1bf11 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63  l header which c
1bf12 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28  ontains the Ptr(
1bf13 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f  N) pointer and o
1bf14 74 68 65 72 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ther..** informa
1bf15 74 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65  tion such as the
1bf16 20 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64   size of key and
1bf17 20 64 61 74 61 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46   data...**..** F
1bf18 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 0d 0a 2a  ORMAT DETAILS..*
1bf19 2a 0d 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 69  *..** The file i
1bf1a 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 70  s divided into p
1bf1b 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 73 74  ages.  The first
1bf1c 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 64 20   page is called 
1bf1d 70 61 67 65 20 31 2c 0d 0a 2a 2a 20 74 68 65 20  page 1,..** the 
1bf1e 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 32  second is page 2
1bf1f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1bf20 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f   A page number o
1bf21 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 73  f zero indicates
1bf22 0d 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61  ..** "no such pa
1bf23 67 65 22 2e 20 20 54 68 65 20 70 61 67 65 20 73  ge".  The page s
1bf24 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20 70  ize can be any p
1bf25 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65  ower of 2 betwee
1bf26 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e  n 512 and 65536.
1bf27 0d 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63  ..** Each page c
1bf28 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62  an be either a b
1bf29 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65  tree page, a fre
1bf2a 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f  elist page, an o
1bf2b 76 65 72 66 6c 6f 77 0d 0a 2a 2a 20 70 61 67 65  verflow..** page
1bf2c 2c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d  , or a pointer-m
1bf2d 61 70 20 70 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ap page...**..**
1bf2e 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
1bf2f 69 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65  is always a btre
1bf30 65 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72  e page.  The fir
1bf31 73 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20  st 100 bytes of 
1bf32 74 68 65 20 66 69 72 73 74 0d 0a 2a 2a 20 70 61  the first..** pa
1bf33 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65  ge contain a spe
1bf34 63 69 61 6c 20 68 65 61 64 65 72 20 28 74 68 65  cial header (the
1bf35 20 22 66 69 6c 65 20 68 65 61 64 65 72 22 29 20   "file header") 
1bf36 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
1bf37 68 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 54 68 65  he file...** The
1bf38 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 66   format of the f
1bf39 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 61 73  ile header is as
1bf3a 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a   follows:..**..*
1bf3b 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a  *   OFFSET   SIZ
1bf3c 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
1bf3d 0d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20  ..**      0     
1bf3e 20 31 36 20 20 20 20 20 48 65 61 64 65 72 20 73   16     Header s
1bf3f 74 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66  tring: "SQLite f
1bf40 6f 72 6d 61 74 20 33 5c 30 30 30 22 0d 0a 2a 2a  ormat 3\000"..**
1bf41 20 20 20 20 20 31 36 20 20 20 20 20 20 20 32 20       16       2 
1bf42 20 20 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e      Page size in
1bf43 20 62 79 74 65 73 2e 20 20 0d 0a 2a 2a 20 20 20   bytes.  ..**   
1bf44 20 20 31 38 20 20 20 20 20 20 20 31 20 20 20 20    18       1    
1bf45 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 72 69   File format wri
1bf46 74 65 20 76 65 72 73 69 6f 6e 0d 0a 2a 2a 20 20  te version..**  
1bf47 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20 20     19       1   
1bf48 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 65    File format re
1bf49 61 64 20 76 65 72 73 69 6f 6e 0d 0a 2a 2a 20 20  ad version..**  
1bf4a 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20     20       1   
1bf4b 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65    Bytes of unuse
1bf4c 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  d space at the e
1bf4d 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0d  nd of each page.
1bf4e 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20  .**     21      
1bf4f 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64   1     Max embed
1bf50 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
1bf51 74 69 6f 6e 0d 0a 2a 2a 20 20 20 20 20 32 32 20  tion..**     22 
1bf52 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20        1     Min 
1bf53 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
1bf54 20 66 72 61 63 74 69 6f 6e 0d 0a 2a 2a 20 20 20   fraction..**   
1bf55 20 20 32 33 20 20 20 20 20 20 20 31 20 20 20 20    23       1    
1bf56 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61   Min leaf payloa
1bf57 64 20 66 72 61 63 74 69 6f 6e 0d 0a 2a 2a 20 20  d fraction..**  
1bf58 20 20 20 32 34 20 20 20 20 20 20 20 34 20 20 20     24       4   
1bf59 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f    File change co
1bf5a 75 6e 74 65 72 0d 0a 2a 2a 20 20 20 20 20 32 38  unter..**     28
1bf5b 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65 73         4     Res
1bf5c 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65  erved for future
1bf5d 20 75 73 65 0d 0a 2a 2a 20 20 20 20 20 33 32 20   use..**     32 
1bf5e 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
1bf5f 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0d  t freelist page.
1bf60 0a 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20  .**     36      
1bf61 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66   4     Number of
1bf62 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20   freelist pages 
1bf63 69 6e 20 74 68 65 20 66 69 6c 65 0d 0a 2a 2a 20  in the file..** 
1bf64 20 20 20 20 34 30 20 20 20 20 20 20 36 30 20 20      40      60  
1bf65 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74     15 4-byte met
1bf66 61 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20  a values passed 
1bf67 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65 72 73  to higher layers
1bf68 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 34 30 20  ..**..**     40 
1bf69 20 20 20 20 20 20 34 20 20 20 20 20 53 63 68 65        4     Sche
1bf6a 6d 61 20 63 6f 6f 6b 69 65 0d 0a 2a 2a 20 20 20  ma cookie..**   
1bf6b 20 20 34 34 20 20 20 20 20 20 20 34 20 20 20 20    44       4    
1bf6c 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20   File format of 
1bf6d 73 63 68 65 6d 61 20 6c 61 79 65 72 0d 0a 2a 2a  schema layer..**
1bf6e 20 20 20 20 20 34 38 20 20 20 20 20 20 20 34 20       48       4 
1bf6f 20 20 20 20 53 69 7a 65 20 6f 66 20 70 61 67 65      Size of page
1bf70 20 63 61 63 68 65 0d 0a 2a 2a 20 20 20 20 20 35   cache..**     5
1bf71 32 20 20 20 20 20 20 20 34 20 20 20 20 20 4c 61  2       4     La
1bf72 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20  rgest root-page 
1bf73 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75  (auto/incr_vacuu
1bf74 6d 29 0d 0a 2a 2a 20 20 20 20 20 35 36 20 20 20  m)..**     56   
1bf75 20 20 20 20 34 20 20 20 20 20 31 3d 55 54 46 2d      4     1=UTF-
1bf76 38 20 32 3d 55 54 46 31 36 6c 65 20 33 3d 55 54  8 2=UTF16le 3=UT
1bf77 46 31 36 62 65 0d 0a 2a 2a 20 20 20 20 20 36 30  F16be..**     60
1bf78 20 20 20 20 20 20 20 34 20 20 20 20 20 55 73 65         4     Use
1bf79 72 20 76 65 72 73 69 6f 6e 0d 0a 2a 2a 20 20 20  r version..**   
1bf7a 20 20 36 34 20 20 20 20 20 20 20 34 20 20 20 20    64       4    
1bf7b 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   Incremental vac
1bf7c 75 75 6d 20 6d 6f 64 65 0d 0a 2a 2a 20 20 20 20  uum mode..**    
1bf7d 20 36 38 20 20 20 20 20 20 20 34 20 20 20 20 20   68       4     
1bf7e 75 6e 75 73 65 64 0d 0a 2a 2a 20 20 20 20 20 37  unused..**     7
1bf7f 32 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e  2       4     un
1bf80 75 73 65 64 0d 0a 2a 2a 20 20 20 20 20 37 36 20  used..**     76 
1bf81 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73        4     unus
1bf82 65 64 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 6f  ed..**..** All o
1bf83 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  f the integer va
1bf84 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64  lues are big-end
1bf85 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66  ian (most signif
1bf86 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74  icant byte first
1bf87 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66  )...**..** The f
1bf88 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  ile change count
1bf89 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  er is incremente
1bf8a 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62  d when the datab
1bf8b 61 73 65 20 69 73 20 63 68 61 6e 67 65 64 0d 0a  ase is changed..
1bf8c 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20  ** This counter 
1bf8d 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f  allows other pro
1bf8e 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77  cesses to know w
1bf8f 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73  hen the file has
1bf90 20 63 68 61 6e 67 65 64 0d 0a 2a 2a 20 61 6e 64   changed..** and
1bf91 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 20   thus when they 
1bf92 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 68  need to flush th
1bf93 65 69 72 20 63 61 63 68 65 2e 0d 0a 2a 2a 0d 0a  eir cache...**..
1bf94 2a 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64  ** The max embed
1bf95 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
1bf96 74 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75  tion is the amou
1bf97 6e 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20  nt of the total 
1bf98 75 73 61 62 6c 65 0d 0a 2a 2a 20 73 70 61 63 65  usable..** space
1bf99 20 69 6e 20 61 20 70 61 67 65 20 74 68 61 74 20   in a page that 
1bf9a 63 61 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20  can be consumed 
1bf9b 62 79 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c  by a single cell
1bf9c 20 66 6f 72 20 73 74 61 6e 64 61 72 64 0d 0a 2a   for standard..*
1bf9d 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45  * B-tree (non-LE
1bf9e 41 46 44 41 54 41 29 20 74 61 62 6c 65 73 2e 20  AFDATA) tables. 
1bf9f 20 41 20 76 61 6c 75 65 20 6f 66 20 32 35 35 20   A value of 255 
1bfa0 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 54 68 65  means 100%.  The
1bfa1 20 64 65 66 61 75 6c 74 0d 0a 2a 2a 20 69 73 20   default..** is 
1bfa2 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78  to limit the max
1bfa3 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 73  imum cell size s
1bfa4 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20  o that at least 
1bfa5 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 69 74  4 cells will fit
1bfa6 0d 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 65  ..** on one page
1bfa7 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66 61  .  Thus the defa
1bfa8 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65 64  ult max embedded
1bfa9 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1bfaa 6e 20 69 73 20 36 34 2e 0d 0a 2a 2a 0d 0a 2a 2a  n is 64...**..**
1bfab 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20   If the payload 
1bfac 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61  for a cell is la
1bfad 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61  rger than the ma
1bfae 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20  x payload, then 
1bfaf 65 78 74 72 61 0d 0a 2a 2a 20 70 61 79 6c 6f 61  extra..** payloa
1bfb0 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20  d is spilled to 
1bfb1 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
1bfb2 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f   Once an overflo
1bfb3 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61  w page is alloca
1bfb4 74 65 64 2c 0d 0a 2a 2a 20 61 73 20 6d 61 6e 79  ted,..** as many
1bfb5 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62   bytes as possib
1bfb6 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74  le are moved int
1bfb7 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  o the overflow p
1bfb8 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74  ages without let
1bfb9 74 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 63 65 6c  ting..** the cel
1bfba 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f  l size drop belo
1bfbb 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  w the min embedd
1bfbc 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
1bfbd 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ion...**..** The
1bfbe 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61   min leaf payloa
1bfbf 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69  d fraction is li
1bfc0 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64  ke the min embed
1bfc1 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
1bfc2 74 69 6f 6e 0d 0a 2a 2a 20 65 78 63 65 70 74 20  tion..** except 
1bfc3 74 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 20  that it applies 
1bfc4 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e  to leaf nodes in
1bfc5 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65   a LEAFDATA tree
1bfc6 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0d 0a  .  The maximum..
1bfc7 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ** payload fract
1bfc8 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41  ion for a LEAFDA
1bfc9 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 61 79  TA tree is alway
1bfca 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 29 20  s 100% (or 255) 
1bfcb 61 6e 64 20 69 74 0d 0a 2a 2a 20 6e 6f 74 20 73  and it..** not s
1bfcc 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
1bfcd 68 65 61 64 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  header...**..** 
1bfce 45 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73  Each btree pages
1bfcf 20 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f   is divided into
1bfd0 20 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a   three sections:
1bfd1 20 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68    The header, th
1bfd2 65 0d 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74  e..** cell point
1bfd3 65 72 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68  er array, and th
1bfd4 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
1bfd5 72 65 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73  rea.  Page 1 als
1bfd6 6f 20 68 61 73 20 61 20 31 30 30 2d 62 79 74 65  o has a 100-byte
1bfd7 0d 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72  ..** file header
1bfd8 20 74 68 61 74 20 6f 63 63 75 72 73 20 62 65 66   that occurs bef
1bfd9 6f 72 65 20 74 68 65 20 70 61 67 65 20 68 65 61  ore the page hea
1bfda 64 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  der...**..**    
1bfdb 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
1bfdc 2d 2d 2d 7c 0d 0a 2a 2a 20 20 20 20 20 20 7c 20  ---|..**      | 
1bfdd 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c  file header    |
1bfde 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50     100 bytes.  P
1bfdf 61 67 65 20 31 20 6f 6e 6c 79 2e 0d 0a 2a 2a 20  age 1 only...** 
1bfe0 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       |----------
1bfe1 2d 2d 2d 2d 2d 2d 7c 0d 0a 2a 2a 20 20 20 20 20  ------|..**     
1bfe2 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20 20   | page header  
1bfe3 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f    |   8 bytes fo
1bfe4 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79  r leaves.  12 by
1bfe5 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72  tes for interior
1bfe6 20 6e 6f 64 65 73 0d 0a 2a 2a 20 20 20 20 20 20   nodes..**      
1bfe7 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
1bfe8 2d 7c 0d 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  -|..**      | ce
1bfe9 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20  ll pointer   |  
1bfea 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20   |  2 bytes per 
1bfeb 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72  cell.  Sorted or
1bfec 64 65 72 2e 0d 0a 2a 2a 20 20 20 20 20 20 7c 20  der...**      | 
1bfed 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c  array          |
1bfee 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e     |  Grows down
1bfef 77 61 72 64 0d 0a 2a 2a 20 20 20 20 20 20 7c 20  ward..**      | 
1bff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
1bff1 20 20 20 76 0d 0a 2a 2a 20 20 20 20 20 20 7c 2d     v..**      |-
1bff2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
1bff3 0d 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c  ..**      | unal
1bff4 6c 6f 63 61 74 65 64 20 20 20 20 7c 0d 0a 2a 2a  located    |..**
1bff5 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 20        | space   
1bff6 20 20 20 20 20 20 20 7c 0d 0a 2a 2a 20 20 20 20         |..**    
1bff7 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
1bff8 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20  ---|   ^  Grows 
1bff9 75 70 77 61 72 64 73 0d 0a 2a 2a 20 20 20 20 20  upwards..**     
1bffa 20 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20   | cell content 
1bffb 20 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61    |   |  Arbitra
1bffc 72 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70  ry order intersp
1bffd 65 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62  ersed with freeb
1bffe 6c 6f 63 6b 73 2e 0d 0a 2a 2a 20 20 20 20 20 20  locks...**      
1bfff 7c 20 61 72 65 61 20 20 20 20 20 20 20 20 20 20  | area          
1c000 20 7c 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65   |   |  and free
1c001 20 73 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73   space fragments
1c002 2e 0d 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d  ...**      |----
1c003 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0d 0a 2a  ------------|..*
1c004 2a 0d 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 68  *..** The page h
1c005 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 6b  eaders looks lik
1c006 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  e this:..**..** 
1c007 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20    OFFSET   SIZE 
1c008 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0d      DESCRIPTION.
1c009 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
1c00a 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31   1      Flags. 1
1c00b 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72  : intkey, 2: zer
1c00c 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61  odata, 4: leafda
1c00d 74 61 2c 20 38 3a 20 6c 65 61 66 0d 0a 2a 2a 20  ta, 8: leaf..** 
1c00e 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20       1       2  
1c00f 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20      byte offset 
1c010 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65  to the first fre
1c011 65 62 6c 6f 63 6b 0d 0a 2a 2a 20 20 20 20 20 20  eblock..**      
1c012 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e  3       2      n
1c013 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
1c014 6e 20 74 68 69 73 20 70 61 67 65 0d 0a 2a 2a 20  n this page..** 
1c015 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20       5       2  
1c016 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f      first byte o
1c017 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
1c018 6e 74 20 61 72 65 61 0d 0a 2a 2a 20 20 20 20 20  nt area..**     
1c019 20 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20   7       1      
1c01a 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65  number of fragme
1c01b 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0d  nted free bytes.
1c01c 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20  .**      8      
1c01d 20 34 20 20 20 20 20 20 52 69 67 68 74 20 63 68   4      Right ch
1c01e 69 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 20  ild (the Ptr(N) 
1c01f 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64  value).  Omitted
1c020 20 6f 6e 20 6c 65 61 76 65 73 2e 0d 0a 2a 2a 0d   on leaves...**.
1c021 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 65  .** The flags de
1c022 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 20  fine the format 
1c023 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70 61  of this btree pa
1c024 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 6c  ge.  The leaf fl
1c025 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0d 0a 2a  ag means that..*
1c026 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20  * this page has 
1c027 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68  no children.  Th
1c028 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20  e zerodata flag 
1c029 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
1c02a 70 61 67 65 20 63 61 72 72 69 65 73 0d 0a 2a 2a  page carries..**
1c02b 20 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e   only keys and n
1c02c 6f 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74  o data.  The int
1c02d 6b 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  key flag means t
1c02e 68 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61  hat the key is a
1c02f 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 77 68 69   integer..** whi
1c030 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ch is stored in 
1c031 74 68 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74  the key size ent
1c032 72 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68  ry of the cell h
1c033 65 61 64 65 72 20 72 61 74 68 65 72 20 74 68 61  eader rather tha
1c034 6e 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 70 61 79  n in..** the pay
1c035 6c 6f 61 64 20 61 72 65 61 2e 0d 0a 2a 2a 0d 0a  load area...**..
1c036 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e  ** The cell poin
1c037 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e 73  ter array begins
1c038 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62 79   on the first by
1c039 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61 67  te after the pag
1c03a 65 20 68 65 61 64 65 72 2e 0d 0a 2a 2a 20 54 68  e header...** Th
1c03b 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
1c03c 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65  rray contains ze
1c03d 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74  ro or more 2-byt
1c03e 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20  e numbers which 
1c03f 61 72 65 0d 0a 2a 2a 20 6f 66 66 73 65 74 73 20  are..** offsets 
1c040 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
1c041 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 74  ng of the page t
1c042 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
1c043 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0d 0a  nt in the cell..
1c044 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e  ** content area.
1c045 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74    The cell point
1c046 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72  ers occur in sor
1c047 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20  ted order.  The 
1c048 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 0d 0a  system strives..
1c049 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 20  ** to keep free 
1c04a 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 20  space after the 
1c04b 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  last cell pointe
1c04c 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 65  r so that new ce
1c04d 6c 6c 73 20 63 61 6e 0d 0a 2a 2a 20 62 65 20 65  lls can..** be e
1c04e 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68  asily added with
1c04f 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65  out having to de
1c050 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
1c051 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43 65 6c 6c 20  e...**..** Cell 
1c052 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65  content is store
1c053 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e  d at the very en
1c054 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e  d of the page an
1c055 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74  d grows toward t
1c056 68 65 0d 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67  he..** beginning
1c057 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0d 0a 2a   of the page...*
1c058 2a 0d 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 61  *..** Unused spa
1c059 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 65  ce within the ce
1c05a 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
1c05b 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74  is collected int
1c05c 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  o a linked list 
1c05d 6f 66 0d 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b  of..** freeblock
1c05e 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f  s.  Each freeblo
1c05f 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34  ck is at least 4
1c060 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
1c061 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   The byte offset
1c062 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73  ..** to the firs
1c063 74 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67  t freeblock is g
1c064 69 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64  iven in the head
1c065 65 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20  er.  Freeblocks 
1c066 6f 63 63 75 72 20 69 6e 0d 0a 2a 2a 20 69 6e 63  occur in..** inc
1c067 72 65 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20  reasing order.  
1c068 42 65 63 61 75 73 65 20 61 20 66 72 65 65 62 6c  Because a freebl
1c069 6f 63 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c  ock must be at l
1c06a 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20  east 4 bytes in 
1c06b 73 69 7a 65 2c 0d 0a 2a 2a 20 61 6e 79 20 67 72  size,..** any gr
1c06c 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65  oup of 3 or fewe
1c06d 72 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 69  r unused bytes i
1c06e 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  n the cell conte
1c06f 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f 74 0d 0a  nt area cannot..
1c070 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20  ** exist on the 
1c071 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 6e 2e  freeblock chain.
1c072 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33 20 6f    A group of 3 o
1c073 72 20 66 65 77 65 72 20 66 72 65 65 20 62 79 74  r fewer free byt
1c074 65 73 20 69 73 20 63 61 6c 6c 65 64 0d 0a 2a 2a  es is called..**
1c075 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 68   a fragment.  Th
1c076 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1c077 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 66  f bytes in all f
1c078 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63 6f  ragments is reco
1c079 72 64 65 64 2e 0d 0a 2a 2a 20 69 6e 20 74 68 65  rded...** in the
1c07a 20 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20   page header at 
1c07b 6f 66 66 73 65 74 20 37 2e 0d 0a 2a 2a 0d 0a 2a  offset 7...**..*
1c07c 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
1c07d 43 52 49 50 54 49 4f 4e 0d 0a 2a 2a 20 20 20 20  CRIPTION..**    
1c07e 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 66    2     Byte off
1c07f 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20  set of the next 
1c080 66 72 65 65 62 6c 6f 63 6b 0d 0a 2a 2a 20 20 20  freeblock..**   
1c081 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69     2     Bytes i
1c082 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b  n this freeblock
1c083 0d 0a 2a 2a 0d 0a 2a 2a 20 43 65 6c 6c 73 20 61  ..**..** Cells a
1c084 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c  re of variable l
1c085 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72  ength.  Cells ar
1c086 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
1c087 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
1c088 61 20 61 74 0d 0a 2a 2a 20 74 68 65 20 65 6e 64  a at..** the end
1c089 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 50   of the page.  P
1c08a 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63  ointers to the c
1c08b 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20  ells are in the 
1c08c 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
1c08d 61 79 0d 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65  ay..** that imme
1c08e 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20  diately follows 
1c08f 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  the page header.
1c090 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e    Cells is not n
1c091 65 63 65 73 73 61 72 69 6c 79 0d 0a 2a 2a 20 63  ecessarily..** c
1c092 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e 20  ontiguous or in 
1c093 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c 20  order, but cell 
1c094 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f 6e  pointers are con
1c095 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f  tiguous and in o
1c096 72 64 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43 65  rder...**..** Ce
1c097 6c 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73  ll content makes
1c098 20 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65   use of variable
1c099 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73   length integers
1c09a 2e 20 20 41 20 76 61 72 69 61 62 6c 65 0d 0a 2a  .  A variable..*
1c09b 2a 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  * length integer
1c09c 20 69 73 20 31 20 74 6f 20 39 20 62 79 74 65 73   is 1 to 9 bytes
1c09d 20 77 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72   where the lower
1c09e 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68 20   7 bits of each 
1c09f 0d 0a 2a 2a 20 62 79 74 65 20 61 72 65 20 75 73  ..** byte are us
1c0a0 65 64 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  ed.  The integer
1c0a1 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c   consists of all
1c0a2 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65   bytes that have
1c0a3 20 62 69 74 20 38 20 73 65 74 20 61 6e 64 0d 0a   bit 8 set and..
1c0a4 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74  ** the first byt
1c0a5 65 20 77 69 74 68 20 62 69 74 20 38 20 63 6c 65  e with bit 8 cle
1c0a6 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 73 69  ar.  The most si
1c0a7 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 6f  gnificant byte o
1c0a8 66 20 74 68 65 20 69 6e 74 65 67 65 72 0d 0a 2a  f the integer..*
1c0a9 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74 2e  * appears first.
1c0aa 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e    A variable-len
1c0ab 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79 20  gth integer may 
1c0ac 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e  not be more than
1c0ad 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0d 0a   9 bytes long...
1c0ae 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20  ** As a special 
1c0af 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65  case, all 8 byte
1c0b0 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74  s of the 9th byt
1c0b1 65 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61  e are used as da
1c0b2 74 61 2e 20 20 54 68 69 73 0d 0a 2a 2a 20 61 6c  ta.  This..** al
1c0b3 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e  lows a 64-bit in
1c0b4 74 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f  teger to be enco
1c0b5 64 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0d  ded in 9 bytes..
1c0b6 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 30 78 30 30 20  .**..**    0x00 
1c0b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0b8 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78       becomes  0x
1c0b9 30 30 30 30 30 30 30 30 0d 0a 2a 2a 20 20 20 20  00000000..**    
1c0ba 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
1c0bb 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
1c0bc 73 20 20 30 78 30 30 30 30 30 30 37 66 0d 0a 2a  s  0x0000007f..*
1c0bd 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20  *    0x81 0x00  
1c0be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
1c0bf 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30  ecomes  0x000000
1c0c0 38 30 0d 0a 2a 2a 20 20 20 20 30 78 38 32 20 30  80..**    0x82 0
1c0c1 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
1c0c2 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
1c0c3 30 30 30 30 31 30 30 0d 0a 2a 2a 20 20 20 20 30  0000100..**    0
1c0c4 78 38 30 20 30 78 37 66 20 20 20 20 20 20 20 20  x80 0x7f        
1c0c5 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73           becomes
1c0c6 20 20 30 78 30 30 30 30 30 30 37 66 0d 0a 2a 2a    0x0000007f..**
1c0c7 20 20 20 20 30 78 38 61 20 30 78 39 31 20 30 78      0x8a 0x91 0x
1c0c8 64 31 20 30 78 61 63 20 30 78 37 38 20 20 62 65  d1 0xac 0x78  be
1c0c9 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 36 37  comes  0x1234567
1c0ca 38 0d 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78  8..**    0x81 0x
1c0cb 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 30  81 0x81 0x81 0x0
1c0cc 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 30  1  becomes  0x10
1c0cd 32 30 34 30 38 31 0d 0a 2a 2a 0d 0a 2a 2a 20 56  204081..**..** V
1c0ce 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69  ariable length i
1c0cf 6e 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64  ntegers are used
1c0d0 20 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20   for rowids and 
1c0d1 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62  to hold the numb
1c0d2 65 72 20 6f 66 0d 0a 2a 2a 20 62 79 74 65 73 20  er of..** bytes 
1c0d3 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20  of key and data 
1c0d4 69 6e 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e  in a btree cell.
1c0d5 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 6e  ..**..** The con
1c0d6 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c 20 6c  tent of a cell l
1c0d7 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0d  ooks like this:.
1c0d8 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 53 49 5a 45 20  .**..**    SIZE 
1c0d9 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0d 0a     DESCRIPTION..
1c0da 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61  **      4     Pa
1c0db 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
1c0dc 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69   left child. Omi
1c0dd 74 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61  tted if leaf fla
1c0de 67 20 69 73 20 73 65 74 2e 0d 0a 2a 2a 20 20 20  g is set...**   
1c0df 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20    var    Number 
1c0e0 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1c0e1 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65  . Omitted if the
1c0e2 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69   zerodata flag i
1c0e3 73 20 73 65 74 2e 0d 0a 2a 2a 20 20 20 20 20 76  s set...**     v
1c0e4 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  ar    Number of 
1c0e5 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72  bytes of key. Or
1c0e6 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 20   the key itself 
1c0e7 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69  if intkey flag i
1c0e8 73 20 73 65 74 2e 0d 0a 2a 2a 20 20 20 20 20 20  s set...**      
1c0e9 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0d 0a 2a  *     Payload..*
1c0ea 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72  *      4     Fir
1c0eb 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f  st page of the o
1c0ec 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20  verflow chain.  
1c0ed 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76  Omitted if no ov
1c0ee 65 72 66 6c 6f 77 0d 0a 2a 2a 0d 0a 2a 2a 20 4f  erflow..**..** O
1c0ef 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f  verflow pages fo
1c0f0 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  rm a linked list
1c0f1 2e 20 20 45 61 63 68 20 70 61 67 65 20 65 78 63  .  Each page exc
1c0f2 65 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 20  ept the last is 
1c0f3 63 6f 6d 70 6c 65 74 65 6c 79 0d 0a 2a 2a 20 66  completely..** f
1c0f4 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 61 20  illed with data 
1c0f5 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79  (pagesize - 4 by
1c0f6 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20  tes).  The last 
1c0f7 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 61 73  page can have as
1c0f8 20 6c 69 74 74 6c 65 0d 0a 2a 2a 20 61 73 20 31   little..** as 1
1c0f9 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0d 0a   byte of data...
1c0fa 2a 2a 0d 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20  **..**    SIZE  
1c0fb 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0d 0a 2a    DESCRIPTION..*
1c0fc 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67  *      4     Pag
1c0fd 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74  e number of next
1c0fe 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0d 0a   overflow page..
1c0ff 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 44 61  **      *     Da
1c100 74 61 0d 0a 2a 2a 0d 0a 2a 2a 20 46 72 65 65 6c  ta..**..** Freel
1c101 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20 69  ist pages come i
1c102 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a 20  n two subtypes: 
1c103 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 20  trunk pages and 
1c104 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
1c105 0d 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72  ..** file header
1c106 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
1c107 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
1c108 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70   list of trunk p
1c109 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b  age.  Each trunk
1c10a 0d 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73  ..** page points
1c10b 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61   to multiple lea
1c10c 66 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f  f pages.  The co
1c10d 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20  ntent of a leaf 
1c10e 70 61 67 65 20 69 73 0d 0a 2a 2a 20 75 6e 73 70  page is..** unsp
1c10f 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e  ecified.  A trun
1c110 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b  k page looks lik
1c111 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  e this:..**..** 
1c112 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
1c113 49 50 54 49 4f 4e 0d 0a 2a 2a 20 20 20 20 20 20  IPTION..**      
1c114 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65  4     Page numbe
1c115 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20  r of next trunk 
1c116 70 61 67 65 0d 0a 2a 2a 20 20 20 20 20 20 34 20  page..**      4 
1c117 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65      Number of le
1c118 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74  af pointers on t
1c119 68 69 73 20 70 61 67 65 0d 0a 2a 2a 20 20 20 20  his page..**    
1c11a 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20    *     zero or 
1c11b 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65  more pages numbe
1c11c 72 73 20 6f 66 20 6c 65 61 76 65 73 0d 0a 2a 2f  rs of leaves..*/
1c11d 0d 0a 0d 0a 0d 0a 2f 2a 20 54 68 65 20 66 6f 6c  ....../* The fol
1c11e 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20  lowing value is 
1c11f 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c  the maximum cell
1c120 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61   size assuming a
1c121 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0d 0a 2a   maximum page..*
1c122 2a 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76  * size give abov
1c123 65 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  e...*/..#define 
1c124 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74  MX_CELL_SIZE(pBt
1c125 29 20 20 28 28 69 6e 74 29 28 70 42 74 2d 3e 70  )  ((int)(pBt->p
1c126 61 67 65 53 69 7a 65 2d 38 29 29 0d 0a 0d 0a 2f  ageSize-8))..../
1c127 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  * The maximum nu
1c128 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e  mber of cells on
1c129 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f   a single page o
1c12a 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
1c12b 20 54 68 69 73 0d 0a 2a 2a 20 61 73 73 75 6d 65   This..** assume
1c12c 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c  s a minimum cell
1c12d 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73   size of 6 bytes
1c12e 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74    (4 bytes for t
1c12f 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0d 0a  he cell itself..
1c130 2a 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 20  ** plus 2 bytes 
1c131 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f  for the index to
1c132 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65   the cell in the
1c133 20 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 20   page header).  
1c134 53 75 63 68 0d 0a 2a 2a 20 73 6d 61 6c 6c 20 63  Such..** small c
1c135 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72  ells will be rar
1c136 65 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 20  e, but they are 
1c137 70 6f 73 73 69 62 6c 65 2e 0d 0a 2a 2f 0d 0a 23  possible...*/..#
1c138 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 70  define MX_CELL(p
1c139 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65 53  Bt) ((pBt->pageS
1c13a 69 7a 65 2d 38 29 2f 36 29 0d 0a 0d 0a 2f 2a 20  ize-8)/6)..../* 
1c13b 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
1c13c 69 6f 6e 73 20 2a 2f 0d 0a 74 79 70 65 64 65 66  ions */..typedef
1c13d 20 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20   struct MemPage 
1c13e 4d 65 6d 50 61 67 65 3b 0d 0a 74 79 70 65 64 65  MemPage;..typede
1c13f 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20  f struct BtLock 
1c140 42 74 4c 6f 63 6b 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a  BtLock;..../*..*
1c141 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69  * This is a magi
1c142 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70  c string that ap
1c143 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67  pears at the beg
1c144 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0d  inning of every.
1c145 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62  .** SQLite datab
1c146 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ase in order to 
1c147 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c  identify the fil
1c148 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61  e as a real data
1c149 62 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 59 6f  base...**..** Yo
1c14a 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69  u can change thi
1c14b 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69  s value at compi
1c14c 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69  le-time by speci
1c14d 66 79 69 6e 67 20 61 0d 0a 2a 2a 20 2d 44 53 51  fying a..** -DSQ
1c14e 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
1c14f 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f  ="..." on the co
1c150 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c  mpiler command-l
1c151 69 6e 65 2e 20 20 54 68 65 0d 0a 2a 2a 20 68 65  ine.  The..** he
1c152 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61  ader must be exa
1c153 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e  ctly 16 bytes in
1c154 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f  cluding the zero
1c155 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0d 0a  -terminator so..
1c156 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 74  ** the string it
1c157 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31  self should be 1
1c158 35 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e  5 characters lon
1c159 67 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67  g.  If you chang
1c15a 65 0d 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  e..** the header
1c15b 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74  , then your cust
1c15c 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20  om library will 
1c15d 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72  not be able to r
1c15e 65 61 64 20 0d 0a 2a 2a 20 64 61 74 61 62 61 73  ead ..** databas
1c15f 65 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  es generated by 
1c160 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f  the standard too
1c161 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e 64  ls and the stand
1c162 61 72 64 20 74 6f 6f 6c 73 0d 0a 2a 2a 20 77 69  ard tools..** wi
1c163 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
1c164 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 73  o read databases
1c165 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75 72   created by your
1c166 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e   custom library.
1c167 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
1c168 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
1c169 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32   /* 123456789 12
1c16a 33 34 35 36 20 2a 2f 0d 0a 23 20 20 64 65 66 69  3456 */..#  defi
1c16b 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48  ne SQLITE_FILE_H
1c16c 45 41 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f  EADER "SQLite fo
1c16d 72 6d 61 74 20 33 22 0d 0a 23 65 6e 64 69 66 0d  rmat 3"..#endif.
1c16e 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 67 65 20 74  .../*..** Page t
1c16f 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f  ype flags.  An O
1c170 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  Red combination 
1c171 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61  of these flags a
1c172 70 70 65 61 72 20 61 73 20 74 68 65 0d 0a 2a 2a  ppear as the..**
1c173 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f   first byte of o
1c174 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20  n-disk image of 
1c175 65 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65  every BTree page
1c176 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50  ...*/..#define P
1c177 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30  TF_INTKEY    0x0
1c178 31 0d 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 5a  1..#define PTF_Z
1c179 45 52 4f 44 41 54 41 20 20 30 78 30 32 0d 0a 23  ERODATA  0x02..#
1c17a 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44  define PTF_LEAFD
1c17b 41 54 41 20 20 30 78 30 34 0d 0a 23 64 65 66 69  ATA  0x04..#defi
1c17c 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20  ne PTF_LEAF     
1c17d 20 30 78 30 38 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   0x08..../*..** 
1c17e 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66 20  As each page of 
1c17f 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 64  the file is load
1c180 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20  ed into memory, 
1c181 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1c182 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a  he following..**
1c183 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
1c184 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69  pended and initi
1c185 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20  alized to zero. 
1c186 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
1c187 73 74 6f 72 65 73 0d 0a 2a 2a 20 69 6e 66 6f 72  stores..** infor
1c188 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
1c189 20 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65   page that is de
1c18a 63 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72  coded from the r
1c18b 61 77 20 66 69 6c 65 20 70 61 67 65 2e 0d 0a 2a  aw file page...*
1c18c 2a 0d 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e  *..** The pParen
1c18d 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 62  t field points b
1c18e 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ack to the paren
1c18f 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61 6c  t page.  This al
1c190 6c 6f 77 73 20 75 73 20 74 6f 0d 0a 2a 2a 20 77  lows us to..** w
1c191 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65  alk up the BTree
1c192 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74   from any leaf t
1c193 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72  o the root.  Car
1c194 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20  e must be taken 
1c195 74 6f 0d 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74  to..** unref() t
1c196 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70  he parent page p
1c197 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73  ointer when this
1c198 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67   page is no long
1c199 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0d 0a  er referenced...
1c19a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72  ** The pageDestr
1c19b 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20  uctor() routine 
1c19c 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f  handles that cho
1c19d 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 63 63 65  re...**..** Acce
1c19e 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73  ss to all fields
1c19f 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
1c1a0 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64  re is controlled
1c1a1 20 62 79 20 74 68 65 20 6d 75 74 65 78 0d 0a 2a   by the mutex..*
1c1a2 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50  * stored in MemP
1c1a3 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0d  age.pBt->mutex..
1c1a4 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 4d 65 6d 50  .*/..struct MemP
1c1a5 61 67 65 20 7b 0d 0a 20 20 75 38 20 69 73 49 6e  age {..  u8 isIn
1c1a6 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  it;           /*
1c1a7 20 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75   True if previou
1c1a8 73 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  sly initialized.
1c1a9 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20   MUST BE FIRST! 
1c1aa 2a 2f 0d 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c  */..  u8 nOverfl
1c1ab 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ow;        /* Nu
1c1ac 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77  mber of overflow
1c1ad 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20   cell bodies in 
1c1ae 61 43 65 6c 6c 5b 5d 20 2a 2f 0d 0a 20 20 75 38  aCell[] */..  u8
1c1af 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20   intKey;        
1c1b0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
1c1b1 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74  tkey flag is set
1c1b2 20 2a 2f 0d 0a 20 20 75 38 20 6c 65 61 66 3b 20   */..  u8 leaf; 
1c1b3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1c1b4 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61 67  rue if leaf flag
1c1b5 20 69 73 20 73 65 74 20 2a 2f 0d 0a 20 20 75 38   is set */..  u8
1c1b6 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20   hasData;       
1c1b7 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1c1b8 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64  is page stores d
1c1b9 61 74 61 20 2a 2f 0d 0a 20 20 75 38 20 68 64 72  ata */..  u8 hdr
1c1ba 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f  Offset;        /
1c1bb 2a 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31  * 100 for page 1
1c1bc 2e 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a  .  0 otherwise *
1c1bd 2f 0d 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72  /..  u8 childPtr
1c1be 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69  Size;     /* 0 i
1c1bf 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66  f leaf==1.  4 if
1c1c0 20 6c 65 61 66 3d 3d 30 20 2a 2f 0d 0a 20 20 75   leaf==0 */..  u
1c1c1 38 20 6d 61 78 31 62 79 74 65 50 61 79 6c 6f 61  8 max1bytePayloa
1c1c2 64 3b 20 20 2f 2a 20 6d 69 6e 28 6d 61 78 4c 6f  d;  /* min(maxLo
1c1c3 63 61 6c 2c 31 32 37 29 20 2a 2f 0d 0a 20 20 75  cal,127) */..  u
1c1c4 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20  16 maxLocal;    
1c1c5 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42      /* Copy of B
1c1c6 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c  tShared.maxLocal
1c1c7 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 61 78   or BtShared.max
1c1c8 4c 65 61 66 20 2a 2f 0d 0a 20 20 75 31 36 20 6d  Leaf */..  u16 m
1c1c9 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20  inLocal;        
1c1ca 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61  /* Copy of BtSha
1c1cb 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20  red.minLocal or 
1c1cc 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 66  BtShared.minLeaf
1c1cd 20 2a 2f 0d 0a 20 20 75 31 36 20 63 65 6c 6c 4f   */..  u16 cellO
1c1ce 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49  ffset;      /* I
1c1cf 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66  ndex in aData of
1c1d0 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e   first cell poin
1c1d1 74 65 72 20 2a 2f 0d 0a 20 20 75 31 36 20 6e 46  ter */..  u16 nF
1c1d2 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ree;           /
1c1d3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
1c1d4 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61   bytes on the pa
1c1d5 67 65 20 2a 2f 0d 0a 20 20 75 31 36 20 6e 43 65  ge */..  u16 nCe
1c1d6 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ll;           /*
1c1d7 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
1c1d8 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c   on this page, l
1c1d9 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f  ocal and ovfl */
1c1da 0d 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65  ..  u16 maskPage
1c1db 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b  ;        /* Mask
1c1dc 20 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74   for page offset
1c1dd 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 5f 4f   */..  struct _O
1c1de 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43  vflCell {   /* C
1c1df 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e  ells that will n
1c1e0 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b  ot fit on aData[
1c1e1 5d 20 2a 2f 0d 0a 20 20 20 20 75 38 20 2a 70 43  ] */..    u8 *pC
1c1e2 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ell;          /*
1c1e3 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
1c1e4 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 65   body of the ove
1c1e5 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0d 0a 20  rflow cell */.. 
1c1e6 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 20 20     u16 idx;     
1c1e7 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
1c1e8 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72   this cell befor
1c1e9 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65  e idx-th non-ove
1c1ea 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0d 0a 20  rflow cell */.. 
1c1eb 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0d 0a 20 20   } aOvfl[5];..  
1c1ec 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
1c1ed 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1c1ee 74 6f 20 42 74 53 68 61 72 65 64 20 74 68 61 74  to BtShared that
1c1ef 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61   this page is pa
1c1f0 72 74 20 6f 66 20 2a 2f 0d 0a 20 20 75 38 20 2a  rt of */..  u8 *
1c1f1 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  aData;          
1c1f2 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64   /* Pointer to d
1c1f3 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 68 65  isk image of the
1c1f4 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0d 0a 20   page data */.. 
1c1f5 20 75 38 20 2a 61 44 61 74 61 45 6e 64 3b 20 20   u8 *aDataEnd;  
1c1f6 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 79 74        /* One byt
1c1f7 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1c1f8 66 20 75 73 61 62 6c 65 20 64 61 74 61 20 2a 2f  f usable data */
1c1f9 0d 0a 20 20 75 38 20 2a 61 43 65 6c 6c 49 64 78  ..  u8 *aCellIdx
1c1fa 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
1c1fb 63 65 6c 6c 20 69 6e 64 65 78 20 61 72 65 61 20  cell index area 
1c1fc 2a 2f 0d 0a 20 20 44 62 50 61 67 65 20 2a 70 44  */..  DbPage *pD
1c1fd 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61  bPage;     /* Pa
1c1fe 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20  ger page handle 
1c1ff 2a 2f 0d 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b  */..  Pgno pgno;
1c200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1c201 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
1c202 69 73 20 70 61 67 65 20 2a 2f 0d 0a 7d 3b 0d 0a  is page */..};..
1c203 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 2d  ../*..** The in-
1c204 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
1c205 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 73 20  a disk page has 
1c206 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e  the auxiliary in
1c207 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64  formation append
1c208 65 64 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e  ed..** to the en
1c209 64 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 69  d.  EXTRA_SIZE i
1c20a 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
1c20b 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e  bytes of space n
1c20c 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 0d 0a 2a  eeded to hold..*
1c20d 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  * that extra inf
1c20e 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 23  ormation...*/..#
1c20f 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a  define EXTRA_SIZ
1c210 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  E sizeof(MemPage
1c211 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 6c 69  )..../*..** A li
1c212 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65  nked list of the
1c213 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
1c214 74 75 72 65 73 20 69 73 20 73 74 6f 72 65 64 20  tures is stored 
1c215 61 74 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63  at BtShared.pLoc
1c216 6b 2e 0d 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65  k...** Locks are
1c217 20 61 64 64 65 64 20 28 6f 72 20 75 70 67 72 61   added (or upgra
1c218 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f  ded from READ_LO
1c219 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b  CK to WRITE_LOCK
1c21a 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f 72 20  ) when a cursor 
1c21b 0d 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 6f  ..** is opened o
1c21c 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  n the table with
1c21d 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68 61   root page BtSha
1c21e 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 6b  red.iTable. Lock
1c21f 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0d 0a 2a  s are removed..*
1c220 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74  * from this list
1c221 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
1c222 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
1c223 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   or rolled back,
1c224 20 6f 72 20 77 68 65 6e 0d 0a 2a 2a 20 61 20 62   or when..** a b
1c225 74 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63  tree handle is c
1c226 6c 6f 73 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  losed...*/..stru
1c227 63 74 20 42 74 4c 6f 63 6b 20 7b 0d 0a 20 20 42  ct BtLock {..  B
1c228 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20  tree *pBtree;   
1c229 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61       /* Btree ha
1c22a 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69  ndle holding thi
1c22b 73 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 50 67 6e  s lock */..  Pgn
1c22c 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20  o iTable;       
1c22d 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
1c22e 6f 66 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 75  of table */..  u
1c22f 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  8 eLock;        
1c230 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43       /* READ_LOC
1c231 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20  K or WRITE_LOCK 
1c232 2a 2f 0d 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e  */..  BtLock *pN
1c233 65 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ext;        /* N
1c234 65 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e  ext in BtShared.
1c235 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0d 0a 7d  pLock list */..}
1c236 3b 0d 0a 0d 0a 2f 2a 20 43 61 6e 64 69 64 61 74  ;..../* Candidat
1c237 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c  e values for BtL
1c238 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0d 0a 23 64  ock.eLock */..#d
1c239 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20  efine READ_LOCK 
1c23a 20 20 20 20 31 0d 0a 23 64 65 66 69 6e 65 20 57      1..#define W
1c23b 52 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0d 0a  RITE_LOCK    2..
1c23c 0d 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e  ../* A Btree han
1c23d 64 6c 65 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 64 61  dle..**..** A da
1c23e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c23f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  n contains a poi
1c240 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
1c241 6e 63 65 20 6f 66 0d 0a 2a 2a 20 74 68 69 73 20  nce of..** this 
1c242 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79  object for every
1c243 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
1c244 68 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e  hat it has open.
1c245 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
1c246 0d 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74  ..** is opaque t
1c247 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  o the database c
1c248 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
1c249 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c24a 69 6f 6e 20 63 61 6e 6e 6f 74 0d 0a 2a 2a 20 73  ion cannot..** s
1c24b 65 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 73  ee the internals
1c24c 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
1c24d 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c  re and only deal
1c24e 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  s with pointers 
1c24f 74 6f 0d 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  to..** this stru
1c250 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46  cture...**..** F
1c251 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65  or some database
1c252 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65   files, the same
1c253 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61   underlying data
1c254 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68 74  base cache might
1c255 20 62 65 20 0d 0a 2a 2a 20 73 68 61 72 65 64 20   be ..** shared 
1c256 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65  between multiple
1c257 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49   connections.  I
1c258 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63  n that case, eac
1c259 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a  h connection..**
1c25a 20 68 61 73 20 69 74 20 6f 77 6e 20 69 6e 73 74   has it own inst
1c25b 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
1c25c 65 63 74 2e 20 20 42 75 74 20 65 61 63 68 20 69  ect.  But each i
1c25d 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
1c25e 6f 62 6a 65 63 74 0d 0a 2a 2a 20 70 6f 69 6e 74  object..** point
1c25f 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42 74  s to the same Bt
1c260 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20  Shared object.  
1c261 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63  The database cac
1c262 68 65 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 73  he and the..** s
1c263 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64  chema associated
1c264 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
1c265 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20  se file are all 
1c266 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
1c267 0d 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65  ..** the BtShare
1c268 64 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a  d object...**..*
1c269 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20  * All fields in 
1c26a 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
1c26b 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
1c26c 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e  r sqlite3.mutex.
1c26d 0d 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69  ..** The pBt poi
1c26e 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20  nter itself may 
1c26f 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77  not be changed w
1c270 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74  hile there exist
1c271 73 20 63 75 72 73 6f 72 73 20 0d 0a 2a 2a 20 69  s cursors ..** i
1c272 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  n the referenced
1c273 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70   BtShared that p
1c274 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69  oint back to thi
1c275 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68  s Btree since th
1c276 6f 73 65 0d 0a 2a 2a 20 63 75 72 73 6f 72 73 20  ose..** cursors 
1c277 68 61 76 65 20 74 6f 20 67 6f 20 74 68 72 6f 75  have to go throu
1c278 67 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f  gh this Btree to
1c279 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68   find their BtSh
1c27a 61 72 65 64 20 61 6e 64 0d 0a 2a 2a 20 74 68 65  ared and..** the
1c27b 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69  y often do so wi
1c27c 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71  thout holding sq
1c27d 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0d 0a 2a 2f  lite3.mutex...*/
1c27e 0d 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 7b  ..struct Btree {
1c27f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ..  sqlite3 *db;
1c280 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1c281 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c282 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62  n holding this b
1c283 74 72 65 65 20 2a 2f 0d 0a 20 20 42 74 53 68 61  tree */..  BtSha
1c284 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a  red *pBt;     /*
1c285 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e   Sharable conten
1c286 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20  t of this btree 
1c287 2a 2f 0d 0a 20 20 75 38 20 69 6e 54 72 61 6e 73  */..  u8 inTrans
1c288 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e  ;        /* TRAN
1c289 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45  S_NONE, TRANS_RE
1c28a 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54  AD or TRANS_WRIT
1c28b 45 20 2a 2f 0d 0a 20 20 75 38 20 73 68 61 72 61  E */..  u8 shara
1c28c 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  ble;       /* Tr
1c28d 75 65 20 69 66 20 77 65 20 63 61 6e 20 73 68 61  ue if we can sha
1c28e 72 65 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74  re pBt with anot
1c28f 68 65 72 20 64 62 20 2a 2f 0d 0a 20 20 75 38 20  her db */..  u8 
1c290 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20  locked;         
1c291 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 63 75  /* True if db cu
1c292 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74 20  rrently has pBt 
1c293 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  locked */..  int
1c294 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20   wantToLock;    
1c295 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
1c296 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ted calls to sql
1c297 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29  ite3BtreeEnter()
1c298 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 61 63 6b   */..  int nBack
1c299 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  up;       /* Num
1c29a 62 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70  ber of backup op
1c29b 65 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67  erations reading
1c29c 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0d 0a   this btree */..
1c29d 20 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20    Btree *pNext; 
1c29e 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
1c29f 6f 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42  other sharable B
1c2a0 74 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73  trees from the s
1c2a1 61 6d 65 20 64 62 20 2a 2f 0d 0a 20 20 42 74 72  ame db */..  Btr
1c2a2 65 65 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20  ee *pPrev;      
1c2a3 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20  /* Back pointer 
1c2a4 6f 66 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74  of the same list
1c2a5 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c2a6 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1c2a7 43 41 43 48 45 0d 0a 20 20 42 74 4c 6f 63 6b 20  CACHE..  BtLock 
1c2a8 6c 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a 20 4f  lock;       /* O
1c2a9 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20 6c 6f  bject used to lo
1c2aa 63 6b 20 70 61 67 65 20 31 20 2a 2f 0d 0a 23 65  ck page 1 */..#e
1c2ab 6e 64 69 66 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  ndif..};..../*..
1c2ac 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73  ** Btree.inTrans
1c2ad 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66   may take one of
1c2ae 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   the following v
1c2af 61 6c 75 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  alues...**..** I
1c2b0 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74  f the shared-dat
1c2b1 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65  a extension is e
1c2b2 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61  nabled, there ma
1c2b3 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73  y be multiple us
1c2b4 65 72 73 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 42  ers..** of the B
1c2b5 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20  tree structure. 
1c2b6 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74  At most one of t
1c2b7 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20  hese may open a 
1c2b8 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1c2b9 6e 2c 0d 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e  n,..** but any n
1c2ba 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61  umber may have a
1c2bb 63 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73  ctive read trans
1c2bc 61 63 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 23 64  actions...*/..#d
1c2bd 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45  efine TRANS_NONE
1c2be 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 54 52 41    0..#define TRA
1c2bf 4e 53 5f 52 45 41 44 20 20 31 0d 0a 23 64 65 66  NS_READ  1..#def
1c2c0 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20  ine TRANS_WRITE 
1c2c1 32 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69  2..../*..** An i
1c2c2 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
1c2c3 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
1c2c4 73 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  s a single datab
1c2c5 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20 0d 0a  ase file...** ..
1c2c6 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61  ** A single data
1c2c7 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62 65  base file can be
1c2c8 20 69 6e 20 75 73 65 20 61 74 20 74 68 65 20 73   in use at the s
1c2c9 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f 0d  ame time by two.
1c2ca 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61  .** or more data
1c2cb 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
1c2cc 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d  .  When two or m
1c2cd 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ore connections 
1c2ce 61 72 65 0d 0a 2a 2a 20 73 68 61 72 69 6e 67 20  are..** sharing 
1c2cf 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1c2d0 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e  e file, each con
1c2d1 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f  nection has it o
1c2d2 77 6e 0d 0a 2a 2a 20 70 72 69 76 61 74 65 20 42  wn..** private B
1c2d3 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20  tree object for 
1c2d4 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63  the file and eac
1c2d5 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65  h of those Btree
1c2d6 73 20 70 6f 69 6e 74 73 0d 0a 2a 2a 20 74 6f 20  s points..** to 
1c2d7 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65  this one BtShare
1c2d8 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61  d object.  BtSha
1c2d9 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20  red.nRef is the 
1c2da 6e 75 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 63 6f  number of..** co
1c2db 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  nnections curren
1c2dc 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69 73  tly sharing this
1c2dd 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d   database file..
1c2de 0a 2a 2a 0d 0a 2a 2a 20 46 69 65 6c 64 73 20 69  .**..** Fields i
1c2df 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
1c2e0 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
1c2e1 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  der the BtShared
1c2e2 2e 6d 75 74 65 78 0d 0a 2a 2a 20 6d 75 74 65 78  .mutex..** mutex
1c2e3 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65  , except for nRe
1c2e4 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63  f and pNext whic
1c2e5 68 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75  h are accessed u
1c2e6 6e 64 65 72 20 74 68 65 0d 0a 2a 2a 20 67 6c 6f  nder the..** glo
1c2e7 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  bal SQLITE_MUTEX
1c2e8 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d  _STATIC_MASTER m
1c2e9 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67 65  utex.  The pPage
1c2ea 72 20 66 69 65 6c 64 0d 0a 2a 2a 20 6d 61 79 20  r field..** may 
1c2eb 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20  not be modified 
1c2ec 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69  once it is initi
1c2ed 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67  ally set as long
1c2ee 20 61 73 20 6e 52 65 66 3e 30 2e 0d 0a 2a 2a 20   as nRef>0...** 
1c2ef 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c  The pSchema fiel
1c2f0 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63  d may be set onc
1c2f1 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 64  e under BtShared
1c2f2 2e 6d 75 74 65 78 20 61 6e 64 0d 0a 2a 2a 20 74  .mutex and..** t
1c2f3 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e 63  hereafter is unc
1c2f4 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61  hanged as long a
1c2f5 73 20 6e 52 65 66 3e 30 2e 0d 0a 2a 2a 0d 0a 2a  s nRef>0...**..*
1c2f6 2a 20 69 73 50 65 6e 64 69 6e 67 3a 0d 0a 2a 2a  * isPending:..**
1c2f7 0d 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 53 68  ..**   If a BtSh
1c2f8 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 69 6c  ared client fail
1c2f9 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72  s to obtain a wr
1c2fa 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 61  ite-lock on a da
1c2fb 74 61 62 61 73 65 0d 0a 2a 2a 20 20 20 74 61 62  tabase..**   tab
1c2fc 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 72  le (because ther
1c2fd 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20  e exists one or 
1c2fe 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 20  more read-locks 
1c2ff 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0d 0a  on the table),..
1c300 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d  **   the shared-
1c301 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65  cache enters 'pe
1c302 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74  nding-lock' stat
1c303 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20  e and isPending 
1c304 69 73 0d 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20  is..**   set to 
1c305 74 72 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  true...**..**   
1c306 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  The shared-cache
1c307 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 65 6e   leaves the 'pen
1c308 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 65  ding lock' state
1c309 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 0d   when either of.
1c30a 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77  .**   the follow
1c30b 69 6e 67 20 6f 63 63 75 72 3a 0d 0a 2a 2a 0d 0a  ing occur:..**..
1c30c 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20 63 75  **     1) The cu
1c30d 72 72 65 6e 74 20 77 72 69 74 65 72 20 28 42 74  rrent writer (Bt
1c30e 53 68 61 72 65 64 2e 70 57 72 69 74 65 72 29 20  Shared.pWriter) 
1c30f 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 74 72  concludes its tr
1c310 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0d 0a 2a  ansaction, OR..*
1c311 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75 6d  *     2) The num
1c312 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c  ber of locks hel
1c313 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65  d by other conne
1c314 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20  ctions drops to 
1c315 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  zero...**..**   
1c316 77 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65  while in the 'pe
1c317 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74  nding-lock' stat
1c318 65 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e  e, no connection
1c319 20 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65 77   may start a new
1c31a 0d 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69  ..**   transacti
1c31b 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 54 68  on...**..**   Th
1c31c 69 73 20 66 65 61 74 75 72 65 20 69 73 20 69 6e  is feature is in
1c31d 63 6c 75 64 65 64 20 74 6f 20 68 65 6c 70 20 70  cluded to help p
1c31e 72 65 76 65 6e 74 20 77 72 69 74 65 72 2d 73 74  revent writer-st
1c31f 61 72 76 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73  arvation...*/..s
1c320 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 7b  truct BtShared {
1c321 0d 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ..  Pager *pPage
1c322 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
1c323 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0d 0a   page cache */..
1c324 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1c325 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1c326 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
1c327 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74  urrently using t
1c328 68 69 73 20 42 74 72 65 65 20 2a 2f 0d 0a 20 20  his Btree */..  
1c329 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  BtCursor *pCurso
1c32a 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  r;    /* A list 
1c32b 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73  of all open curs
1c32c 6f 72 73 20 2a 2f 0d 0a 20 20 4d 65 6d 50 61 67  ors */..  MemPag
1c32d 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20  e *pPage1;      
1c32e 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66  /* First page of
1c32f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
1c330 0d 0a 20 20 75 38 20 6f 70 65 6e 46 6c 61 67 73  ..  u8 openFlags
1c331 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61  ;         /* Fla
1c332 67 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  gs to sqlite3Btr
1c333 65 65 4f 70 65 6e 28 29 20 2a 2f 0d 0a 23 69 66  eeOpen() */..#if
1c334 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c335 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 75  _AUTOVACUUM..  u
1c336 38 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20  8 autoVacuum;   
1c337 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1c338 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65  auto-vacuum is e
1c339 6e 61 62 6c 65 64 20 2a 2f 0d 0a 20 20 75 38 20  nabled */..  u8 
1c33a 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 20 20  incrVacuum;     
1c33b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
1c33c 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61  cr-vacuum is ena
1c33d 62 6c 65 64 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  bled */..#endif.
1c33e 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74  .  u8 inTransact
1c33f 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e  ion;     /* Tran
1c340 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f  saction state */
1c341 0d 0a 20 20 75 38 20 6d 61 78 31 62 79 74 65 50  ..  u8 max1byteP
1c342 61 79 6c 6f 61 64 3b 20 20 20 2f 2a 20 4d 61 78  ayload;   /* Max
1c343 69 6d 75 6d 20 66 69 72 73 74 20 62 79 74 65 20  imum first byte 
1c344 6f 66 20 63 65 6c 6c 20 66 6f 72 20 61 20 31 2d  of cell for a 1-
1c345 62 79 74 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0d  byte payload */.
1c346 0a 20 20 75 31 36 20 62 74 73 46 6c 61 67 73 3b  .  u16 btsFlags;
1c347 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c           /* Bool
1c348 65 61 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20  ean parameters. 
1c349 20 53 65 65 20 42 54 53 5f 2a 20 6d 61 63 72 6f   See BTS_* macro
1c34a 73 20 62 65 6c 6f 77 20 2a 2f 0d 0a 20 20 75 31  s below */..  u1
1c34b 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20  6 maxLocal;     
1c34c 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
1c34d 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20  ocal payload in 
1c34e 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62  non-LEAFDATA tab
1c34f 6c 65 73 20 2a 2f 0d 0a 20 20 75 31 36 20 6d 69  les */..  u16 mi
1c350 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20  nLocal;         
1c351 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c  /* Minimum local
1c352 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d   payload in non-
1c353 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20  LEAFDATA tables 
1c354 2a 2f 0d 0a 20 20 75 31 36 20 6d 61 78 4c 65 61  */..  u16 maxLea
1c355 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  f;          /* M
1c356 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  aximum local pay
1c357 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41  load in a LEAFDA
1c358 54 41 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 75  TA table */..  u
1c359 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20  16 minLeaf;     
1c35a 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
1c35b 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
1c35c 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c   a LEAFDATA tabl
1c35d 65 20 2a 2f 0d 0a 20 20 75 33 32 20 70 61 67 65  e */..  u32 page
1c35e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Size;         /*
1c35f 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
1c360 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65   bytes on a page
1c361 20 2a 2f 0d 0a 20 20 75 33 32 20 75 73 61 62 6c   */..  u32 usabl
1c362 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20  eSize;       /* 
1c363 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  Number of usable
1c364 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70   bytes on each p
1c365 61 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54  age */..  int nT
1c366 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
1c367 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65  /* Number of ope
1c368 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28  n transactions (
1c369 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f  read + write) */
1c36a 0d 0a 20 20 75 33 32 20 6e 50 61 67 65 3b 20 20  ..  u32 nPage;  
1c36b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1c36c 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
1c36d 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0d  the database */.
1c36e 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61  .  void *pSchema
1c36f 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
1c370 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c  ter to space all
1c371 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
1c372 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a  3BtreeSchema() *
1c373 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65  /..  void (*xFre
1c374 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b  eSchema)(void*);
1c375 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
1c376 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63  for BtShared.pSc
1c377 68 65 6d 61 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  hema */..  sqlit
1c378 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
1c379 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76   /* Non-recursiv
1c37a 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 65 64  e mutex required
1c37b 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20   to access this 
1c37c 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 42 69 74  object */..  Bit
1c37d 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74  vec *pHasContent
1c37e 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67  ;  /* Set of pag
1c37f 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 65  es moved to free
1c380 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 6e 73  -list this trans
1c381 61 63 74 69 6f 6e 20 2a 2f 0d 0a 23 69 66 6e 64  action */..#ifnd
1c382 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1c383 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 20 20 69  HARED_CACHE..  i
1c384 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
1c385 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c386 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
1c387 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
1c388 2f 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  /..  BtShared *p
1c389 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Next;      /* Ne
1c38a 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20  xt on a list of 
1c38b 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65  sharable BtShare
1c38c 64 20 73 74 72 75 63 74 73 20 2a 2f 0d 0a 20 20  d structs */..  
1c38d 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20  BtLock *pLock;  
1c38e 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
1c38f 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74   locks held on t
1c390 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65  his shared-btree
1c391 20 73 74 72 75 63 74 20 2a 2f 0d 0a 20 20 42 74   struct */..  Bt
1c392 72 65 65 20 2a 70 57 72 69 74 65 72 3b 20 20 20  ree *pWriter;   
1c393 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74      /* Btree wit
1c394 68 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e  h currently open
1c395 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1c396 6f 6e 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20  on */..#endif.. 
1c397 20 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20   u8 *pTmpSpace; 
1c398 20 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72         /* BtShar
1c399 65 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65  ed.pageSize byte
1c39a 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74  s of space for t
1c39b 6d 70 20 75 73 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  mp use */..};...
1c39c 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ./*..** Allowed 
1c39d 76 61 6c 75 65 73 20 66 6f 72 20 42 74 53 68 61  values for BtSha
1c39e 72 65 64 2e 62 74 73 46 6c 61 67 73 0d 0a 2a 2f  red.btsFlags..*/
1c39f 0d 0a 23 64 65 66 69 6e 65 20 42 54 53 5f 52 45  ..#define BTS_RE
1c3a0 41 44 5f 4f 4e 4c 59 20 20 20 20 20 20 20 20 30  AD_ONLY        0
1c3a1 78 30 30 30 31 20 20 20 2f 2a 20 55 6e 64 65 72  x0001   /* Under
1c3a2 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65  lying file is re
1c3a3 61 64 6f 6e 6c 79 20 2a 2f 0d 0a 23 64 65 66 69  adonly */..#defi
1c3a4 6e 65 20 42 54 53 5f 50 41 47 45 53 49 5a 45 5f  ne BTS_PAGESIZE_
1c3a5 46 49 58 45 44 20 20 20 30 78 30 30 30 32 20 20  FIXED   0x0002  
1c3a6 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 63 61   /* Page size ca
1c3a7 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63  n no longer be c
1c3a8 68 61 6e 67 65 64 20 2a 2f 0d 0a 23 64 65 66 69  hanged */..#defi
1c3a9 6e 65 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45  ne BTS_SECURE_DE
1c3aa 4c 45 54 45 20 20 20 20 30 78 30 30 30 34 20 20  LETE    0x0004  
1c3ab 20 2f 2a 20 50 52 41 47 4d 41 20 73 65 63 75 72   /* PRAGMA secur
1c3ac 65 5f 64 65 6c 65 74 65 20 69 73 20 65 6e 61 62  e_delete is enab
1c3ad 6c 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  led */..#define 
1c3ae 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d  BTS_INITIALLY_EM
1c3af 50 54 59 20 20 30 78 30 30 30 38 20 20 20 2f 2a  PTY  0x0008   /*
1c3b0 20 44 61 74 61 62 61 73 65 20 77 61 73 20 65 6d   Database was em
1c3b1 70 74 79 20 61 74 20 74 72 61 6e 73 20 73 74 61  pty at trans sta
1c3b2 72 74 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42  rt */..#define B
1c3b3 54 53 5f 4e 4f 5f 57 41 4c 20 20 20 20 20 20 20  TS_NO_WAL       
1c3b4 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 2a 20      0x0010   /* 
1c3b5 44 6f 20 6e 6f 74 20 6f 70 65 6e 20 77 72 69 74  Do not open writ
1c3b6 65 2d 61 68 65 61 64 2d 6c 6f 67 20 66 69 6c 65  e-ahead-log file
1c3b7 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 54  s */..#define BT
1c3b8 53 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  S_EXCLUSIVE     
1c3b9 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 70     0x0020   /* p
1c3ba 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 65 78  Writer has an ex
1c3bb 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0d  clusive lock */.
1c3bc 0a 23 64 65 66 69 6e 65 20 42 54 53 5f 50 45 4e  .#define BTS_PEN
1c3bd 44 49 4e 47 20 20 20 20 20 20 20 20 20 20 30 78  DING          0x
1c3be 30 30 34 30 20 20 20 2f 2a 20 57 61 69 74 69 6e  0040   /* Waitin
1c3bf 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73  g for read-locks
1c3c0 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0d 0a 0d 0a   to clear */....
1c3c1 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  /*..** An instan
1c3c2 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
1c3c3 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
1c3c4 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e   used to hold in
1c3c5 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 61 62  formation..** ab
1c3c6 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65  out a cell.  The
1c3c7 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20   parseCellPtr() 
1c3c8 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69  function fills i
1c3c9 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
1c3ca 0d 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e  ..** based on in
1c3cb 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63  formation extrac
1c3cc 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64  t from the raw d
1c3cd 69 73 6b 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 74  isk page...*/..t
1c3ce 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43 65  ypedef struct Ce
1c3cf 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b  llInfo CellInfo;
1c3d0 0d 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66  ..struct CellInf
1c3d1 6f 20 7b 0d 0a 20 20 69 36 34 20 6e 4b 65 79 3b  o {..  i64 nKey;
1c3d2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79        /* The key
1c3d3 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c   for INTKEY tabl
1c3d4 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 6f 66  es, or number of
1c3d5 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f   bytes in key */
1c3d6 0d 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20  ..  u8 *pCell;  
1c3d7 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1c3d8 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65   the start of ce
1c3d9 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 20  ll content */.. 
1c3da 20 75 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20   u32 nData;     
1c3db 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c3dc 65 73 20 6f 66 20 64 61 74 61 20 2a 2f 0d 0a 20  es of data */.. 
1c3dd 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20   u32 nPayload;  
1c3de 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20  /* Total amount 
1c3df 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f 0d 0a 20  of payload */.. 
1c3e0 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20 20 20   u16 nHeader;   
1c3e1 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63  /* Size of the c
1c3e2 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64  ell content head
1c3e3 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  er in bytes */..
1c3e4 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 20    u16 nLocal;   
1c3e5 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 61   /* Amount of pa
1c3e6 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
1c3e7 6c 79 20 2a 2f 0d 0a 20 20 75 31 36 20 69 4f 76  ly */..  u16 iOv
1c3e8 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65  erflow; /* Offse
1c3e9 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
1c3ea 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f  ge number.  Zero
1c3eb 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20   if no overflow 
1c3ec 2a 2f 0d 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b  */..  u16 nSize;
1c3ed 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1c3ee 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
1c3ef 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74   on the main b-t
1c3f0 72 65 65 20 70 61 67 65 20 2a 2f 0d 0a 7d 3b 0d  ree page */..};.
1c3f1 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 78 69 6d 75  .../*..** Maximu
1c3f2 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 51  m depth of an SQ
1c3f3 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 75  Lite B-Tree stru
1c3f4 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 65  cture. Any B-Tre
1c3f5 65 20 64 65 65 70 65 72 20 74 68 61 6e 0d 0a 2a  e deeper than..*
1c3f6 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64  * this will be d
1c3f7 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e  eclared corrupt.
1c3f8 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63   This value is c
1c3f9 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20  alculated based 
1c3fa 6f 6e 20 61 0d 0a 2a 2a 20 6d 61 78 69 6d 75 6d  on a..** maximum
1c3fb 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f   database size o
1c3fc 66 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d  f 2^31 pages a m
1c3fd 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66  inimum fanout of
1c3fe 20 32 20 66 6f 72 20 61 0d 0a 2a 2a 20 72 6f 6f   2 for a..** roo
1c3ff 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72  t-node and 3 for
1c400 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72   all other inter
1c401 6e 61 6c 20 6e 6f 64 65 73 2e 0d 0a 2a 2a 0d 0a  nal nodes...**..
1c402 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61  ** If a tree tha
1c403 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  t appears to be 
1c404 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73  taller than this
1c405 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
1c406 20 69 74 20 69 73 0d 0a 2a 2a 20 61 73 73 75 6d   it is..** assum
1c407 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ed that the data
1c408 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e  base is corrupt.
1c409 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 42 54  ..*/..#define BT
1c40a 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48  CURSOR_MAX_DEPTH
1c40b 20 32 30 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20   20..../*..** A 
1c40c 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e  cursor is a poin
1c40d 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75  ter to a particu
1c40e 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e  lar entry within
1c40f 20 61 20 70 61 72 74 69 63 75 6c 61 72 0d 0a 2a   a particular..*
1c410 2a 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20  * b-tree within 
1c411 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
1c412 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 65 6e 74  ..**..** The ent
1c413 72 79 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ry is identified
1c414 20 62 79 20 69 74 73 20 4d 65 6d 50 61 67 65 20   by its MemPage 
1c415 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e  and the index in
1c416 0d 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65  ..** MemPage.aCe
1c417 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e 74 72  ll[] of the entr
1c418 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 73 69 6e  y...**..** A sin
1c419 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
1c41a 65 20 63 61 6e 20 62 65 20 73 68 61 72 65 64 20  e can be shared 
1c41b 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 74 61  by two more data
1c41c 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
1c41d 2c 0d 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f 72  ,..** but cursor
1c41e 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 72  s cannot be shar
1c41f 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f 72  ed.  Each cursor
1c420 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77   is associated w
1c421 69 74 68 20 61 0d 0a 2a 2a 20 70 61 72 74 69 63  ith a..** partic
1c422 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
1c423 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
1c424 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
1c425 72 65 65 2e 64 62 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ree.db...**..** 
1c426 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73  Fields in this s
1c427 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63  tructure are acc
1c428 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20  essed under the 
1c429 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0d 0a  BtShared.mutex..
1c42a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c 66  ** found at self
1c42b 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0d 0a  ->pBt->mutex. ..
1c42c 2a 2f 0d 0a 73 74 72 75 63 74 20 42 74 43 75 72  */..struct BtCur
1c42d 73 6f 72 20 7b 0d 0a 20 20 42 74 72 65 65 20 2a  sor {..  Btree *
1c42e 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 20  pBtree;         
1c42f 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20     /* The Btree 
1c430 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 63 75  to which this cu
1c431 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0d  rsor belongs */.
1c432 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1c433 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1c434 54 68 65 20 42 74 53 68 61 72 65 64 20 74 68 69  The BtShared thi
1c435 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20  s cursor points 
1c436 74 6f 20 2a 2f 0d 0a 20 20 42 74 43 75 72 73 6f  to */..  BtCurso
1c437 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76  r *pNext, *pPrev
1c438 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69  ;  /* Forms a li
1c439 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c  nked list of all
1c43a 20 63 75 72 73 6f 72 73 20 2a 2f 0d 0a 20 20 73   cursors */..  s
1c43b 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70  truct KeyInfo *p
1c43c 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75  KeyInfo; /* Argu
1c43d 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 63  ment passed to c
1c43e 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
1c43f 6f 6e 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 70 67  on */..  Pgno pg
1c440 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20  noRoot;         
1c441 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
1c442 61 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65  age of this tree
1c443 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69   */..  sqlite3_i
1c444 6e 74 36 34 20 63 61 63 68 65 64 52 6f 77 69 64  nt64 cachedRowid
1c445 3b 20 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20  ; /* Next rowid 
1c446 63 61 63 68 65 2e 20 20 30 20 6d 65 61 6e 73 20  cache.  0 means 
1c447 6e 6f 74 20 76 61 6c 69 64 20 2a 2f 0d 0a 20 20  not valid */..  
1c448 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20  CellInfo info;  
1c449 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70            /* A p
1c44a 61 72 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  arse of the cell
1c44b 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67   we are pointing
1c44c 20 61 74 20 2a 2f 0d 0a 20 20 69 36 34 20 6e 4b   at */..  i64 nK
1c44d 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ey;        /* Si
1c44e 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c  ze of pKey, or l
1c44f 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20  ast integer key 
1c450 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79  */..  void *pKey
1c451 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20  ;      /* Saved 
1c452 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75 72  key that was cur
1c453 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77 6e  sor's last known
1c454 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0d 0a 20 20   position */..  
1c455 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20 20  int skipNext;   
1c456 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e 6f   /* Prev() is no
1c457 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20  op if negative. 
1c458 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20 69  Next() is noop i
1c459 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0d 0a 20  f positive */.. 
1c45a 20 75 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20   u8 wrFlag;     
1c45b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1c45c 75 65 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a  ue if writable *
1c45d 2f 0d 0a 20 20 75 38 20 61 74 4c 61 73 74 3b 20  /..  u8 atLast; 
1c45e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c45f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
1c460 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  g to the last en
1c461 74 72 79 20 2a 2f 0d 0a 20 20 75 38 20 76 61 6c  try */..  u8 val
1c462 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 20 20 20  idNKey;         
1c463 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
1c464 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69  nfo.nKey is vali
1c465 64 20 2a 2f 0d 0a 20 20 75 38 20 65 53 74 61 74  d */..  u8 eStat
1c466 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1c467 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
1c468 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e 73 74  CURSOR_XXX const
1c469 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f 77 29  ants (see below)
1c46a 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c46b 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1c46c 42 0d 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72  B..  Pgno *aOver
1c46d 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f  flow;          /
1c46e 2a 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66  * Cache of overf
1c46f 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f  low page locatio
1c470 6e 73 20 2a 2f 0d 0a 20 20 75 38 20 69 73 49 6e  ns */..  u8 isIn
1c471 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20  crblobHandle;   
1c472 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1c473 69 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20  is cursor is an 
1c474 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20  incr. io handle 
1c475 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 31  */..#endif..  i1
1c476 36 20 69 50 61 67 65 3b 20 20 20 20 20 20 20 20  6 iPage;        
1c477 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c478 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1c479 63 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20  current page in 
1c47a 61 70 50 61 67 65 20 2a 2f 0d 0a 20 20 75 31 36  apPage */..  u16
1c47b 20 61 69 49 64 78 5b 42 54 43 55 52 53 4f 52 5f   aiIdx[BTCURSOR_
1c47c 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 20 20 20  MAX_DEPTH];     
1c47d 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e     /* Current in
1c47e 64 65 78 20 69 6e 20 61 70 50 61 67 65 5b 69 5d  dex in apPage[i]
1c47f 20 2a 2f 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a   */..  MemPage *
1c480 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52 5f  apPage[BTCURSOR_
1c481 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20  MAX_DEPTH];  /* 
1c482 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20  Pages from root 
1c483 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  to current page 
1c484 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
1c485 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65   Potential value
1c486 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65  s for BtCursor.e
1c487 53 74 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43  State...**..** C
1c488 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0d 0a 2a 2a  URSOR_VALID:..**
1c489 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73     Cursor points
1c48a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72   to a valid entr
1c48b 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20  y. getPayload() 
1c48c 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c  etc. may be call
1c48d 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43 55 52 53  ed...**..** CURS
1c48e 4f 52 5f 49 4e 56 41 4c 49 44 3a 0d 0a 2a 2a 20  OR_INVALID:..** 
1c48f 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f    Cursor does no
1c490 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  t point to a val
1c491 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63  id entry. This c
1c492 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65  an happen (for e
1c493 78 61 6d 70 6c 65 29 20 0d 0a 2a 2a 20 20 20 62  xample) ..**   b
1c494 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65  ecause the table
1c495 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63   is empty or bec
1c496 61 75 73 65 20 42 74 72 65 65 43 75 72 73 6f 72  ause BtreeCursor
1c497 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20  First() has not 
1c498 62 65 65 6e 0d 0a 2a 2a 20 20 20 63 61 6c 6c 65  been..**   calle
1c499 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43 55 52 53 4f  d...**..** CURSO
1c49a 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0d 0a  R_REQUIRESEEK:..
1c49b 2a 2a 20 20 20 54 68 65 20 74 61 62 6c 65 20 74  **   The table t
1c49c 68 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20  hat this cursor 
1c49d 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74  was opened on st
1c49e 69 6c 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20  ill exists, but 
1c49f 68 61 73 20 62 65 65 6e 20 0d 0a 2a 2a 20 20 20  has been ..**   
1c4a0 6d 6f 64 69 66 69 65 64 20 73 69 6e 63 65 20 74  modified since t
1c4a1 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6c 61  he cursor was la
1c4a2 73 74 20 75 73 65 64 2e 20 54 68 65 20 63 75 72  st used. The cur
1c4a3 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 73 20  sor position is 
1c4a4 73 61 76 65 64 0d 0a 2a 2a 20 20 20 69 6e 20 76  saved..**   in v
1c4a5 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f  ariables BtCurso
1c4a6 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 43 75 72  r.pKey and BtCur
1c4a7 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 6e 20 61  sor.nKey. When a
1c4a8 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 0d 0a   cursor is in ..
1c4a9 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 65 2c  **   this state,
1c4aa 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1c4ab 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 65 20  sition() can be 
1c4ac 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d 70  called to attemp
1c4ad 74 20 74 6f 0d 0a 2a 2a 20 20 20 73 65 65 6b 20  t to..**   seek 
1c4ae 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
1c4af 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e  e saved position
1c4b0 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43 55 52 53 4f 52  ...**..** CURSOR
1c4b1 5f 46 41 55 4c 54 3a 0d 0a 2a 2a 20 20 20 41 20  _FAULT:..**   A 
1c4b2 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72  unrecoverable er
1c4b3 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f  ror (an I/O erro
1c4b4 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61  r or a malloc fa
1c4b5 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72  ilure) has occur
1c4b6 72 65 64 0d 0a 2a 2a 20 20 20 6f 6e 20 61 20 64  red..**   on a d
1c4b7 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74  ifferent connect
1c4b8 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20  ion that shares 
1c4b9 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63  the BtShared cac
1c4ba 68 65 20 77 69 74 68 20 74 68 69 73 0d 0a 2a 2a  he with this..**
1c4bb 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20     cursor.  The 
1c4bc 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74  error has left t
1c4bd 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69  he cache in an i
1c4be 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74  nconsistent stat
1c4bf 65 2e 0d 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68  e...**   Do noth
1c4c0 69 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74 68  ing else with th
1c4c1 69 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20  is cursor.  Any 
1c4c2 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74  attempt to use t
1c4c3 68 65 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 20 20  he cursor..**   
1c4c4 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68  should return th
1c4c5 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
1c4c6 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
1c4c7 73 6b 69 70 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  skip..*/..#defin
1c4c8 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  e CURSOR_INVALID
1c4c9 20 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23 64             0..#d
1c4ca 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c  efine CURSOR_VAL
1c4cb 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ID             1
1c4cc 0d 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52  ..#define CURSOR
1c4cd 5f 52 45 51 55 49 52 45 53 45 45 4b 20 20 20 20  _REQUIRESEEK    
1c4ce 20 20 20 32 0d 0a 23 64 65 66 69 6e 65 20 43 55     2..#define CU
1c4cf 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20  RSOR_FAULT      
1c4d0 20 20 20 20 20 20 20 33 0d 0a 0d 0a 2f 2a 20 0d         3..../* .
1c4d1 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 73 65  .** The database
1c4d2 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 4e   page the PENDIN
1c4d3 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 2e  G_BYTE occupies.
1c4d4 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e 65   This page is ne
1c4d5 76 65 72 20 75 73 65 64 2e 0d 0a 2a 2f 0d 0a 23  ver used...*/..#
1c4d6 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f   define PENDING_
1c4d7 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 50  BYTE_PAGE(pBt) P
1c4d8 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 74  AGER_MJ_PGNO(pBt
1c4d9 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 73  )..../*..** Thes
1c4da 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20  e macros define 
1c4db 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  the location of 
1c4dc 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
1c4dd 65 6e 74 72 79 20 66 6f 72 20 61 20 0d 0a 2a 2a  entry for a ..**
1c4de 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20   database page. 
1c4df 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
1c4e0 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20 74 68  nt to each is th
1c4e1 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62  e number of usab
1c4e2 6c 65 0d 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20  le..** bytes on 
1c4e3 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65  each page of the
1c4e4 20 64 61 74 61 62 61 73 65 20 28 6f 66 74 65 6e   database (often
1c4e5 20 31 30 32 34 29 2e 20 54 68 65 20 73 65 63 6f   1024). The seco
1c4e6 6e 64 20 69 73 20 74 68 65 0d 0a 2a 2a 20 70 61  nd is the..** pa
1c4e7 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f  ge number to loo
1c4e8 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e  k up in the poin
1c4e9 74 65 72 20 6d 61 70 2e 0d 0a 2a 2a 0d 0a 2a 2a  ter map...**..**
1c4ea 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72   PTRMAP_PAGENO r
1c4eb 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62  eturns the datab
1c4ec 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ase page number 
1c4ed 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
1c4ee 61 70 0d 0a 2a 2a 20 70 61 67 65 20 74 68 61 74  ap..** page that
1c4ef 20 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75   stores the requ
1c4f0 69 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54  ired pointer. PT
1c4f1 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72  RMAP_PTROFFSET r
1c4f2 65 74 75 72 6e 73 0d 0a 2a 2a 20 74 68 65 20 6f  eturns..** the o
1c4f3 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 65 71  ffset of the req
1c4f4 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79  uested map entry
1c4f5 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
1c4f6 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70   pgno argument p
1c4f7 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f  assed to PTRMAP_
1c4f8 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e  PAGENO is a poin
1c4f9 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0d 0a 2a  ter-map page,..*
1c4fa 2a 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72  * then pgno is r
1c4fb 65 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e  eturned. So (pgn
1c4fc 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  o==PTRMAP_PAGENO
1c4fd 28 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61  (pgsz, pgno)) ca
1c4fe 6e 20 62 65 0d 0a 2a 2a 20 75 73 65 64 20 74 6f  n be..** used to
1c4ff 20 74 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73   test if pgno is
1c500 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
1c501 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41  age. PTRMAP_ISPA
1c502 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 0d 0a 2a  GE implements..*
1c503 2a 20 74 68 69 73 20 74 65 73 74 2e 0d 0a 2a 2f  * this test...*/
1c504 0d 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50  ..#define PTRMAP
1c505 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e  _PAGENO(pBt, pgn
1c506 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28  o) ptrmapPageno(
1c507 70 42 74 2c 20 70 67 6e 6f 29 0d 0a 23 64 65 66  pBt, pgno)..#def
1c508 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  ine PTRMAP_PTROF
1c509 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70  FSET(pgptrmap, p
1c50a 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67  gno) (5*(pgno-pg
1c50b 70 74 72 6d 61 70 2d 31 29 29 0d 0a 23 64 65 66  ptrmap-1))..#def
1c50c 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47  ine PTRMAP_ISPAG
1c50d 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54  E(pBt, pgno) (PT
1c50e 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74  RMAP_PAGENO((pBt
1c50f 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f  ),(pgno))==(pgno
1c510 29 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  ))..../*..** The
1c511 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20   pointer map is 
1c512 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74  a lookup table t
1c513 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74  hat identifies t
1c514 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 66  he parent page f
1c515 6f 72 0d 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c  or..** each chil
1c516 64 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  d page in the da
1c517 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
1c518 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73  e parent page is
1c519 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0d 0a   the page that..
1c51a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  ** contains a po
1c51b 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 68 69  inter to the chi
1c51c 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 65 20  ld.  Every page 
1c51d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1c51e 63 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 30 20 6f  contains..** 0 o
1c51f 72 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 73  r 1 parent pages
1c520 2e 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e 74  .  (In this cont
1c521 65 78 74 20 27 64 61 74 61 62 61 73 65 20 70 61  ext 'database pa
1c522 67 65 27 20 72 65 66 65 72 73 0d 0a 2a 2a 20 74  ge' refers..** t
1c523 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20  o any page that 
1c524 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  is not part of t
1c525 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
1c526 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f  tself.)  Each po
1c527 69 6e 74 65 72 20 6d 61 70 0d 0a 2a 2a 20 65 6e  inter map..** en
1c528 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  try consists of 
1c529 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 74  a single byte 't
1c52a 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 74  ype' and a 4 byt
1c52b 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  e parent page nu
1c52c 6d 62 65 72 2e 0d 0a 2a 2a 20 54 68 65 20 50 54  mber...** The PT
1c52d 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66  RMAP_XXX identif
1c52e 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74  iers below are t
1c52f 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0d  he valid types..
1c530 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70 75 72 70  .**..** The purp
1c531 6f 73 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  ose of the point
1c532 65 72 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63  er map is to fac
1c533 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67  ility moving pag
1c534 65 73 20 66 72 6f 6d 20 6f 6e 65 0d 0a 2a 2a 20  es from one..** 
1c535 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20  position in the 
1c536 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 72 20  file to another 
1c537 61 73 20 70 61 72 74 20 6f 66 20 61 75 74 6f 76  as part of autov
1c538 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 20 70  acuum.  When a p
1c539 61 67 65 0d 0a 2a 2a 20 69 73 20 6d 6f 76 65 64  age..** is moved
1c53a 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e  , the pointer in
1c53b 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73 74   its parent must
1c53c 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20 70   be updated to p
1c53d 6f 69 6e 74 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  oint to the..** 
1c53e 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  new location.  T
1c53f 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
1c540 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
1c541 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1c542 20 71 75 69 63 6b 6c 79 2e 0d 0a 2a 2a 0d 0a 2a   quickly...**..*
1c543 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  * PTRMAP_ROOTPAG
1c544 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
1c545 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70  page is a root-p
1c546 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
1c547 6d 62 65 72 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20  mber is not..** 
1c548 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c549 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61   used in this ca
1c54a 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 54 52 4d  se...**..** PTRM
1c54b 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65  AP_FREEPAGE: The
1c54c 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
1c54d 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65  s an unused (fre
1c54e 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 61 67  e) page. The pag
1c54f 65 2d 6e 75 6d 62 65 72 20 0d 0a 2a 2a 20 20 20  e-number ..**   
1c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
1c551 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 68  s not used in th
1c552 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  is case...**..**
1c553 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1c554 31 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  1: The database 
1c555 70 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73  page is the firs
1c556 74 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74  t page in a list
1c557 20 6f 66 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20   of ..**        
1c558 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66             overf
1c559 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70  low pages. The p
1c55a 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74  age number ident
1c55b 69 66 69 65 73 20 74 68 65 20 70 61 67 65 20 74  ifies the page t
1c55c 68 61 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  hat..**         
1c55d 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 69            contai
1c55e 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  ns the cell with
1c55f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1c560 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  is overflow page
1c561 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 54 52 4d 41 50  ...**..** PTRMAP
1c562 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20  _OVERFLOW2: The 
1c563 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
1c564 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c   the second or l
1c565 61 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c  ater page in a l
1c566 69 73 74 20 6f 66 0d 0a 2a 2a 20 20 20 20 20 20  ist of..**      
1c567 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65               ove
1c568 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
1c569 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65   page-number ide
1c56a 6e 74 69 66 69 65 73 20 74 68 65 20 70 72 65 76  ntifies the prev
1c56b 69 6f 75 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ious..**        
1c56c 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20             page 
1c56d 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
1c56e 70 61 67 65 20 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a  page list...**..
1c56f 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a  ** PTRMAP_BTREE:
1c570 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
1c571 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74  ge is a non-root
1c572 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65   btree page. The
1c573 20 70 61 67 65 20 6e 75 6d 62 65 72 0d 0a 2a 2a   page number..**
1c574 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
1c575 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
1c576 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65  rent page in the
1c577 20 62 74 72 65 65 2e 0d 0a 2a 2f 0d 0a 23 64 65   btree...*/..#de
1c578 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54  fine PTRMAP_ROOT
1c579 50 41 47 45 20 31 0d 0a 23 64 65 66 69 6e 65 20  PAGE 1..#define 
1c57a 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
1c57b 32 0d 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41  2..#define PTRMA
1c57c 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0d 0a 23  P_OVERFLOW1 3..#
1c57d 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56  define PTRMAP_OV
1c57e 45 52 46 4c 4f 57 32 20 34 0d 0a 23 64 65 66 69  ERFLOW2 4..#defi
1c57f 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20  ne PTRMAP_BTREE 
1c580 35 0d 0a 0d 0a 2f 2a 20 41 20 62 75 6e 63 68 20  5..../* A bunch 
1c581 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74  of assert() stat
1c582 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20  ements to check 
1c583 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1c584 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 0d  state variables.
1c585 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 20  .** of handle p 
1c586 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 72  (type Btree*) ar
1c587 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e  e internally con
1c588 73 69 73 74 65 6e 74 2e 0d 0a 2a 2f 0d 0a 23 64  sistent...*/..#d
1c589 65 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67  efine btreeInteg
1c58a 72 69 74 79 28 70 29 20 5c 0d 0a 20 20 61 73 73  rity(p) \..  ass
1c58b 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54  ert( p->pBt->inT
1c58c 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e  ransaction!=TRAN
1c58d 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74  S_NONE || p->pBt
1c58e 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  ->nTransaction==
1c58f 30 20 29 3b 20 5c 0d 0a 20 20 61 73 73 65 72 74  0 ); \..  assert
1c590 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e  ( p->pBt->inTran
1c591 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72  saction>=p->inTr
1c592 61 6e 73 20 29 3b 20 0d 0a 0d 0a 0d 0a 2f 2a 0d  ans ); ....../*.
1c593 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41  .** The ISAUTOVA
1c594 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73  CUUM macro is us
1c595 65 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63  ed within balanc
1c596 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64  e_nonroot() to d
1c597 65 74 65 72 6d 69 6e 65 0d 0a 2a 2a 20 69 66 20  etermine..** if 
1c598 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70  the database sup
1c599 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
1c59a 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73  m or not. Becaus
1c59b 65 20 69 74 20 69 73 20 75 73 65 64 0d 0a 2a 2a  e it is used..**
1c59c 20 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72 65   within an expre
1c59d 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6e  ssion that is an
1c59e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f   argument to ano
1c59f 74 68 65 72 20 6d 61 63 72 6f 20 0d 0a 2a 2a 20  ther macro ..** 
1c5a0 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77  (sqliteMallocRaw
1c5a1 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ), it is not pos
1c5a2 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e  sible to use con
1c5a3 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61  ditional compila
1c5a4 74 69 6f 6e 2e 0d 0a 2a 2a 20 53 6f 2c 20 74 68  tion...** So, th
1c5a5 69 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  is macro is defi
1c5a6 6e 65 64 20 69 6e 73 74 65 61 64 2e 0d 0a 2a 2f  ned instead...*/
1c5a7 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1c5a8 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1c5a9 0d 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f  ..#define ISAUTO
1c5aa 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 74  VACUUM (pBt->aut
1c5ab 6f 56 61 63 75 75 6d 29 0d 0a 23 65 6c 73 65 0d  oVacuum)..#else.
1c5ac 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56  .#define ISAUTOV
1c5ad 41 43 55 55 4d 20 30 0d 0a 23 65 6e 64 69 66 0d  ACUUM 0..#endif.
1c5ae 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  ...../*..** This
1c5af 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
1c5b0 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f  ssed around thro
1c5b1 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69  ugh all the sani
1c5b2 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74  ty checking rout
1c5b3 69 6e 65 73 0d 0a 2a 2a 20 69 6e 20 6f 72 64 65  ines..** in orde
1c5b4 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20  r to keep track 
1c5b5 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73  of some global s
1c5b6 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
1c5b7 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73  ...*/..typedef s
1c5b8 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43  truct IntegrityC
1c5b9 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b 0d 0a  k IntegrityCk;..
1c5ba 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79  struct Integrity
1c5bb 43 6b 20 7b 0d 0a 20 20 42 74 53 68 61 72 65 64  Ck {..  BtShared
1c5bc 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65   *pBt;    /* The
1c5bd 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 65 63   tree being chec
1c5be 6b 65 64 20 6f 75 74 20 2a 2f 0d 0a 20 20 50 61  ked out */..  Pa
1c5bf 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20  ger *pPager;    
1c5c0 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
1c5c1 64 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 61  d pager.  Also a
1c5c2 63 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 74  ccessible by pBt
1c5c3 2d 3e 70 50 61 67 65 72 20 2a 2f 0d 0a 20 20 50  ->pPager */..  P
1c5c4 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20  gno nPage;      
1c5c5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1c5c6 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
1c5c7 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61  ase */..  int *a
1c5c8 6e 52 65 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e  nRef;       /* N
1c5c9 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 65  umber of times e
1c5ca 61 63 68 20 70 61 67 65 20 69 73 20 72 65 66 65  ach page is refe
1c5cb 72 65 6e 63 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  renced */..  int
1c5cc 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20 20 2f   mxErr;        /
1c5cd 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 74  * Stop accumulat
1c5ce 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65 6e 20  ing errors when 
1c5cf 74 68 69 73 20 72 65 61 63 68 65 73 20 7a 65 72  this reaches zer
1c5d0 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 45 72 72  o */..  int nErr
1c5d1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
1c5d2 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73 20  ber of messages 
1c5d3 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d  written to zErrM
1c5d4 73 67 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20 20  sg so far */..  
1c5d5 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  int mallocFailed
1c5d6 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c  ; /* A memory al
1c5d7 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68  location error h
1c5d8 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0d 0a  as occurred */..
1c5d9 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73    StrAccum errMs
1c5da 67 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74  g;  /* Accumulat
1c5db 65 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  e the error mess
1c5dc 61 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f  age text here */
1c5dd 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ..};..../*..** R
1c5de 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61 64 20  outines to read 
1c5df 6f 72 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20  or write a two- 
1c5e0 61 6e 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69  and four-byte bi
1c5e1 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
1c5e2 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f 0d 0a 23 64   values...*/..#d
1c5e3 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 28 78  efine get2byte(x
1c5e4 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c 38 20 7c  )   ((x)[0]<<8 |
1c5e5 20 28 78 29 5b 31 5d 29 0d 0a 23 64 65 66 69 6e   (x)[1])..#defin
1c5e6 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 29 20  e put2byte(p,v) 
1c5e7 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 28 28  ((p)[0] = (u8)((
1c5e8 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d 20 3d  v)>>8), (p)[1] =
1c5e9 20 28 75 38 29 28 76 29 29 0d 0a 23 64 65 66 69   (u8)(v))..#defi
1c5ea 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c 69  ne get4byte sqli
1c5eb 74 65 33 47 65 74 34 62 79 74 65 0d 0a 23 64 65  te3Get4byte..#de
1c5ec 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71  fine put4byte sq
1c5ed 6c 69 74 65 33 50 75 74 34 62 79 74 65 0d 0a 0d  lite3Put4byte...
1c5ee 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1c5ef 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74   End of btreeInt
1c5f0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
1c5f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c5f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1c5f3 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1c5f4 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
1c5f5 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
1c5f6 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  n btmutex.c ****
1c5f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c5f8 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1c5f9 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1c5fa 43 48 45 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  CHE..#if SQLITE_
1c5fb 54 48 52 45 41 44 53 41 46 45 0d 0a 0d 0a 2f 2a  THREADSAFE..../*
1c5fc 0d 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20  ..** Obtain the 
1c5fd 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61  BtShared mutex a
1c5fe 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42  ssociated with B
1c5ff 2d 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20  -Tree handle p. 
1c600 41 6c 73 6f 2c 0d 0a 2a 2a 20 73 65 74 20 42 74  Also,..** set Bt
1c601 53 68 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65  Shared.db to the
1c602 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1c603 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1c604 20 70 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 70   p and the..** p
1c605 2d 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e  ->locked boolean
1c606 20 74 6f 20 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 73   to true...*/..s
1c607 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 6b 42  tatic void lockB
1c608 74 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20  treeMutex(Btree 
1c609 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  *p){..  assert( 
1c60a 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0d  p->locked==0 );.
1c60b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c60c 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
1c60d 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  (p->pBt->mutex) 
1c60e 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
1c60f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1c610 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
1c611 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ;....  sqlite3_m
1c612 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42  utex_enter(p->pB
1c613 74 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 70 2d  t->mutex);..  p-
1c614 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  >pBt->db = p->db
1c615 3b 0d 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d  ;..  p->locked =
1c616 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   1;..}..../*..**
1c617 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53   Release the BtS
1c618 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f  hared mutex asso
1c619 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72  ciated with B-Tr
1c61a 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0d  ee handle p and.
1c61b 0a 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d  .** clear the p-
1c61c 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e  >locked boolean.
1c61d 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
1c61e 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74  d unlockBtreeMut
1c61f 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20  ex(Btree *p){.. 
1c620 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1c621 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 61 73 73 65   p->pBt;..  asse
1c622 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31  rt( p->locked==1
1c623 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
1c624 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1c625 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1c626 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1c627 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1c628 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  ->db->mutex) );.
1c629 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
1c62a 3d 3d 70 42 74 2d 3e 64 62 20 29 3b 0d 0a 0d 0a  ==pBt->db );....
1c62b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1c62c 6c 65 61 76 65 28 70 42 74 2d 3e 6d 75 74 65 78  leave(pBt->mutex
1c62d 29 3b 0d 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20  );..  p->locked 
1c62e 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  = 0;..}..../*..*
1c62f 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20  * Enter a mutex 
1c630 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 72  on the given BTr
1c631 65 65 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a  ee object...**..
1c632 2a 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74  ** If the object
1c633 20 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65   is not sharable
1c634 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20  , then no mutex 
1c635 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64  is ever required
1c636 0d 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f  ..** and this ro
1c637 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
1c638 2e 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  .  The underlyin
1c639 67 20 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72  g mutex is non-r
1c63a 65 63 75 72 73 69 76 65 2e 0d 0a 2a 2a 20 42 75  ecursive...** Bu
1c63b 74 20 77 65 20 6b 65 65 70 20 61 20 72 65 66 65  t we keep a refe
1c63c 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 6e 20 42  rence count in B
1c63d 74 72 65 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20  tree.wantToLock 
1c63e 73 6f 20 74 68 65 20 62 65 68 61 76 69 6f 72 0d  so the behavior.
1c63f 0a 2a 2a 20 6f 66 20 74 68 69 73 20 69 6e 74 65  .** of this inte
1c640 72 66 61 63 65 20 69 73 20 72 65 63 75 72 73 69  rface is recursi
1c641 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 61  ve...**..** To a
1c642 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 2c 20  void deadlocks, 
1c643 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 73 20  multiple Btrees 
1c644 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68  are locked in th
1c645 65 20 73 61 6d 65 20 6f 72 64 65 72 0d 0a 2a 2a  e same order..**
1c646 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 65   by all database
1c647 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54   connections.  T
1c648 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 61  he p->pNext is a
1c649 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0d 0a   list of other..
1c64a 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67  ** Btrees belong
1c64b 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
1c64c 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c64d 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72  ion as the p Btr
1c64e 65 65 0d 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65  ee..** which nee
1c64f 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61  d to be locked a
1c650 66 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63  fter p.  If we c
1c651 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b  annot get a lock
1c652 20 6f 6e 0d 0a 2a 2a 20 70 2c 20 74 68 65 6e 20   on..** p, then 
1c653 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c  first unlock all
1c654 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f   of the others o
1c655 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e  n p->pNext, then
1c656 20 77 61 69 74 0d 0a 2a 2a 20 66 6f 72 20 74 68   wait..** for th
1c657 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65  e lock to become
1c658 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c   available on p,
1c659 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c   then relock all
1c65a 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 73 75 62 73   of the..** subs
1c65b 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 74 68  equent Btrees th
1c65c 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b  at desire a lock
1c65d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
1c65e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1c65f 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72  e3BtreeEnter(Btr
1c660 65 65 20 2a 70 29 7b 0d 0a 20 20 42 74 72 65 65  ee *p){..  Btree
1c661 20 2a 70 4c 61 74 65 72 3b 0d 0a 0d 0a 20 20 2f   *pLater;....  /
1c662 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
1c663 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ity checking on 
1c664 74 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20  the Btree.  The 
1c665 6c 69 73 74 20 6f 66 20 42 74 72 65 65 73 0d 0a  list of Btrees..
1c666 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62    ** connected b
1c667 79 20 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65  y pNext and pPre
1c668 76 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73  v should be in s
1c669 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 0d 0a  orted order by..
1c66a 20 20 2a 2a 20 42 74 72 65 65 2e 70 42 74 20 76    ** Btree.pBt v
1c66b 61 6c 75 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e  alue. All elemen
1c66c 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20 73  ts of the list s
1c66d 68 6f 75 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0d  hould belong to.
1c66e 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 63  .  ** the same c
1c66f 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20  onnection. Only 
1c670 73 68 61 72 65 64 20 42 74 72 65 65 73 20 61 72  shared Btrees ar
1c671 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 2a  e on the list. *
1c672 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  /..  assert( p->
1c673 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70  pNext==0 || p->p
1c674 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e 70 42 74  Next->pBt>p->pBt
1c675 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
1c676 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d  ->pPrev==0 || p-
1c677 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d 3e 70  >pPrev->pBt<p->p
1c678 42 74 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  Bt );..  assert(
1c679 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20   p->pNext==0 || 
1c67a 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 2d  p->pNext->db==p-
1c67b 3e 64 62 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  >db );..  assert
1c67c 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c  ( p->pPrev==0 ||
1c67d 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70   p->pPrev->db==p
1c67e 2d 3e 64 62 20 29 3b 0d 0a 20 20 61 73 73 65 72  ->db );..  asser
1c67f 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c  t( p->sharable |
1c680 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26  | (p->pNext==0 &
1c681 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29  & p->pPrev==0) )
1c682 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;....  /* Check 
1c683 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73  for locking cons
1c684 69 73 74 65 6e 63 79 20 2a 2f 0d 0a 20 20 61 73  istency */..  as
1c685 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64  sert( !p->locked
1c686 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   || p->wantToLoc
1c687 6b 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  k>0 );..  assert
1c688 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
1c689 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
1c68a 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 57 65 20  0 );....  /* We 
1c68b 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68  should already h
1c68c 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  old a lock on th
1c68d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1c68e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 61 73 73 65  ction */..  asse
1c68f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1c690 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
1c691 74 65 78 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  tex) );....  /* 
1c692 55 6e 6c 65 73 73 20 74 68 65 20 64 61 74 61 62  Unless the datab
1c693 61 73 65 20 69 73 20 73 68 61 72 61 62 6c 65 20  ase is sharable 
1c694 61 6e 64 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68  and unlocked, th
1c695 65 6e 20 42 74 53 68 61 72 65 64 2e 64 62 0d 0a  en BtShared.db..
1c696 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61 6c 72 65    ** should alre
1c697 61 64 79 20 62 65 20 73 65 74 20 63 6f 72 72 65  ady be set corre
1c698 63 74 6c 79 2e 20 2a 2f 0d 0a 20 20 61 73 73 65  ctly. */..  asse
1c699 72 74 28 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  rt( (p->locked==
1c69a 30 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65  0 && p->sharable
1c69b 29 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d  ) || p->pBt->db=
1c69c 3d 70 2d 3e 64 62 20 29 3b 0d 0a 0d 0a 20 20 69  =p->db );....  i
1c69d 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20  f( !p->sharable 
1c69e 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 70 2d 3e  ) return;..  p->
1c69f 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0d 0a 20  wantToLock++;.. 
1c6a0 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29   if( p->locked )
1c6a1 20 72 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a   return;....  /*
1c6a2 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20   In most cases, 
1c6a3 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c  we should be abl
1c6a4 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65  e to acquire the
1c6a5 20 6c 6f 63 6b 20 77 65 0d 0a 20 20 2a 2a 20 77   lock we..  ** w
1c6a6 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 69  ant without havi
1c6a7 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68  ng to go through
1c6a8 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20  t the ascending 
1c6a9 6c 6f 63 6b 0d 0a 20 20 2a 2a 20 70 72 6f 63 65  lock..  ** proce
1c6aa 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77  dure that follow
1c6ab 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65  s.  Just be sure
1c6ac 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0d 0a   not to block...
1c6ad 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69    */..  if( sqli
1c6ae 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d  te3_mutex_try(p-
1c6af 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51  >pBt->mutex)==SQ
1c6b0 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1c6b1 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  p->pBt->db = p->
1c6b2 64 62 3b 0d 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b  db;..    p->lock
1c6b3 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 72 65 74  ed = 1;..    ret
1c6b4 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  urn;..  }....  /
1c6b5 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  * To avoid deadl
1c6b6 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c 65 61  ock, first relea
1c6b7 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74  se all locks wit
1c6b8 68 20 61 20 6c 61 72 67 65 72 0d 0a 20 20 2a 2a  h a larger..  **
1c6b9 20 42 74 53 68 61 72 65 64 20 61 64 64 72 65 73   BtShared addres
1c6ba 73 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72 65  s.  Then acquire
1c6bb 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 6e   our lock.  Then
1c6bc 20 72 65 61 63 71 75 69 72 65 0d 0a 20 20 2a 2a   reacquire..  **
1c6bd 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 68 61   the other BtSha
1c6be 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77  red locks that w
1c6bf 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69  e used to hold i
1c6c0 6e 20 61 73 63 65 6e 64 69 6e 67 0d 0a 20 20 2a  n ascending..  *
1c6c1 2a 20 6f 72 64 65 72 2e 0d 0a 20 20 2a 2f 0d 0a  * order...  */..
1c6c2 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e    for(pLater=p->
1c6c3 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70  pNext; pLater; p
1c6c4 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e  Later=pLater->pN
1c6c5 65 78 74 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  ext){..    asser
1c6c6 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61  t( pLater->shara
1c6c7 62 6c 65 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ble );..    asse
1c6c8 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78  rt( pLater->pNex
1c6c9 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e  t==0 || pLater->
1c6ca 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65  pNext->pBt>pLate
1c6cb 72 2d 3e 70 42 74 20 29 3b 0d 0a 20 20 20 20 61  r->pBt );..    a
1c6cc 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e  ssert( !pLater->
1c6cd 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72  locked || pLater
1c6ce 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29  ->wantToLock>0 )
1c6cf 3b 0d 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65  ;..    if( pLate
1c6d0 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0d 0a 20 20  r->locked ){..  
1c6d1 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d      unlockBtreeM
1c6d2 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0d 0a 20  utex(pLater);.. 
1c6d3 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 6c 6f 63     }..  }..  loc
1c6d4 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0d  kBtreeMutex(p);.
1c6d5 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d  .  for(pLater=p-
1c6d6 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20  >pNext; pLater; 
1c6d7 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70  pLater=pLater->p
1c6d8 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20  Next){..    if( 
1c6d9 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f  pLater->wantToLo
1c6da 63 6b 20 29 7b 0d 0a 20 20 20 20 20 20 6c 6f 63  ck ){..      loc
1c6db 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74  kBtreeMutex(pLat
1c6dc 65 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  er);..    }..  }
1c6dd 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78  ..}..../*..** Ex
1c6de 69 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65  it the recursive
1c6df 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
1c6e0 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
1c6e1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1c6e2 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74  te3BtreeLeave(Bt
1c6e3 72 65 65 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  ree *p){..  if( 
1c6e4 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0d 0a  p->sharable ){..
1c6e5 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
1c6e6 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0d 0a  antToLock>0 );..
1c6e7 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
1c6e8 6b 2d 2d 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d  k--;..    if( p-
1c6e9 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
1c6ea 7b 0d 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  {..      unlockB
1c6eb 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0d 0a 20  treeMutex(p);.. 
1c6ec 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
1c6ed 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
1c6ee 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  /*..** Return tr
1c6ef 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
1c6f0 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
1c6f1 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f   on the btree, o
1c6f2 72 20 69 66 20 74 68 65 0d 0a 2a 2a 20 42 2d 54  r if the..** B-T
1c6f3 72 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65  ree is not marke
1c6f4 64 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0d 0a  d as sharable...
1c6f5 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
1c6f6 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  ine is used only
1c6f7 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73 73   from within ass
1c6f8 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
1c6f9 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
1c6fa 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c6fb 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
1c6fc 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20 61  (Btree *p){..  a
1c6fd 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
1c6fe 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
1c6ff 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74  ed==0 || p->want
1c700 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0d 0a 20 20 61  ToLock>0 );..  a
1c701 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
1c702 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
1c703 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d  ed==0 || p->db==
1c704 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0d 0a 20  p->pBt->db );.. 
1c705 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
1c706 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
1c707 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  cked==0 || sqlit
1c708 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1c709 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  >pBt->mutex) );.
1c70a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
1c70b 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e  arable==0 || p->
1c70c 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c  locked==0 || sql
1c70d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1c70e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
1c70f 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  ....  return (p-
1c710 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20  >sharable==0 || 
1c711 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0d 0a 7d 0d 0a  p->locked);..}..
1c712 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 23 69 66 6e  #endif......#ifn
1c713 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c714 49 4e 43 52 42 4c 4f 42 0d 0a 2f 2a 0d 0a 2a 2a  INCRBLOB../*..**
1c715 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65   Enter and leave
1c716 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74   a mutex on a Bt
1c717 72 65 65 20 67 69 76 65 6e 20 61 20 63 75 72 73  ree given a curs
1c718 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74  or owned by that
1c719 0d 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 65  ..** Btree.  The
1c71a 73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 20  se entry points 
1c71b 61 72 65 20 75 73 65 64 20 62 79 20 69 6e 63 72  are used by incr
1c71c 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20  emental I/O and 
1c71d 63 61 6e 20 62 65 0d 0a 2a 2a 20 6f 6d 69 74 74  can be..** omitt
1c71e 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 75 6c  ed if that modul
1c71f 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0d 0a  e is not used...
1c720 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1c721 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1c722 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28  treeEnterCursor(
1c723 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1c724 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1c725 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72  Enter(pCur->pBtr
1c726 65 65 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f  ee);..}..SQLITE_
1c727 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1c728 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75  ite3BtreeLeaveCu
1c729 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70  rsor(BtCursor *p
1c72a 43 75 72 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  Cur){..  sqlite3
1c72b 42 74 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d  BtreeLeave(pCur-
1c72c 3e 70 42 74 72 65 65 29 3b 0d 0a 7d 0d 0a 23 65  >pBtree);..}..#e
1c72d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1c72e 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0d  MIT_INCRBLOB */.
1c72f 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 6e 74 65  ...../*..** Ente
1c730 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
1c731 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
1c732 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
1c733 61 62 61 73 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63  abase..** connec
1c734 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e  tion.  This is n
1c735 65 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70  eeded (for examp
1c736 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72  le) prior to par
1c737 73 69 6e 67 0d 0a 2a 2a 20 61 20 73 74 61 74 65  sing..** a state
1c738 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69  ment since we wi
1c739 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20  ll be comparing 
1c73a 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
1c73b 20 6e 61 6d 65 73 0d 0a 2a 2a 20 61 67 61 69 6e   names..** again
1c73c 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 61  st all schemas a
1c73d 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e  nd we do not wan
1c73e 74 20 74 68 6f 73 65 20 73 63 68 65 6d 61 73 20  t those schemas 
1c73f 62 65 69 6e 67 0d 0a 2a 2a 20 72 65 73 65 74 20  being..** reset 
1c740 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75  out from under u
1c741 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65  s...**..** There
1c742 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64   is a correspond
1c743 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72  ing leave-all pr
1c744 6f 63 65 64 75 72 65 73 2e 0d 0a 2a 2a 0d 0a 2a  ocedures...**..*
1c745 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65  * Enter the mute
1c746 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67  xes in accending
1c747 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72   order by BtShar
1c748 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65  ed pointer addre
1c749 73 73 0d 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20  ss..** to avoid 
1c74a 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20  the possibility 
1c74b 6f 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e  of deadlock when
1c74c 20 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74   two threads wit
1c74d 68 0d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72  h..** two or mor
1c74e 65 20 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d  e btrees in comm
1c74f 6f 6e 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c  on both try to l
1c750 6f 63 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74  ock all their bt
1c751 72 65 65 73 0d 0a 2a 2a 20 61 74 20 74 68 65 20  rees..** at the 
1c752 73 61 6d 65 20 69 6e 73 74 61 6e 74 2e 0d 0a 2a  same instant...*
1c753 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
1c754 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
1c755 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69  reeEnterAll(sqli
1c756 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 69 6e 74  te3 *db){..  int
1c757 20 69 3b 0d 0a 20 20 42 74 72 65 65 20 2a 70 3b   i;..  Btree *p;
1c758 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1c759 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
1c75a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  b->mutex) );..  
1c75b 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
1c75c 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 70  Db; i++){..    p
1c75d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1c75e 74 3b 0d 0a 20 20 20 20 69 66 28 20 70 20 29 20  t;..    if( p ) 
1c75f 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1c760 72 28 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 53  r(p);..  }..}..S
1c761 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1c762 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
1c763 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 20  eaveAll(sqlite3 
1c764 2a 64 62 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d  *db){..  int i;.
1c765 0a 20 20 42 74 72 65 65 20 2a 70 3b 0d 0a 20 20  .  Btree *p;..  
1c766 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1c767 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
1c768 75 74 65 78 29 20 29 3b 0d 0a 20 20 66 6f 72 28  utex) );..  for(
1c769 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
1c76a 69 2b 2b 29 7b 0d 0a 20 20 20 20 70 20 3d 20 64  i++){..    p = d
1c76b 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a  b->aDb[i].pBt;..
1c76c 20 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69      if( p ) sqli
1c76d 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1c76e 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
1c76f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1c770 69 66 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  if a particular 
1c771 42 74 72 65 65 20 72 65 71 75 69 72 65 73 20 61  Btree requires a
1c772 20 6c 6f 63 6b 2e 20 20 52 65 74 75 72 6e 20 46   lock.  Return F
1c773 41 4c 53 45 20 69 66 0d 0a 2a 2a 20 6e 6f 20 6c  ALSE if..** no l
1c774 6f 63 6b 20 69 73 20 65 76 65 72 20 72 65 71 75  ock is ever requ
1c775 69 72 65 64 20 73 69 6e 63 65 20 69 74 20 69 73  ired since it is
1c776 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2e 0d 0a   not sharable...
1c777 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1c778 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1c779 72 65 65 53 68 61 72 61 62 6c 65 28 42 74 72 65  reeSharable(Btre
1c77a 65 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e  e *p){..  return
1c77b 20 70 2d 3e 73 68 61 72 61 62 6c 65 3b 0d 0a 7d   p->sharable;..}
1c77c 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ....#ifndef NDEB
1c77d 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  UG../*..** Retur
1c77e 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75  n true if the cu
1c77f 72 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c  rrent thread hol
1c780 64 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ds the database 
1c781 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 6d  connection..** m
1c782 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71  utex and all req
1c783 75 69 72 65 64 20 42 74 53 68 61 72 65 64 20 6d  uired BtShared m
1c784 75 74 65 78 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  utexes...**..** 
1c785 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1c786 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65  used inside asse
1c787 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
1c788 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  only...*/..SQLIT
1c789 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1c78a 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
1c78b 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65  llMutexes(sqlite
1c78c 33 20 2a 64 62 29 7b 0d 0a 20 20 69 6e 74 20 69  3 *db){..  int i
1c78d 3b 0d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ;..  if( !sqlite
1c78e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
1c78f 3e 6d 75 74 65 78 29 20 29 7b 0d 0a 20 20 20 20  >mutex) ){..    
1c790 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a  return 0;..  }..
1c791 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1c792 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
1c793 20 42 74 72 65 65 20 2a 70 3b 0d 0a 20 20 20 20   Btree *p;..    
1c794 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  p = db->aDb[i].p
1c795 42 74 3b 0d 0a 20 20 20 20 69 66 28 20 70 20 26  Bt;..    if( p &
1c796 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26  & p->sharable &&
1c797 0d 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77  ..         (p->w
1c798 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  antToLock==0 || 
1c799 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68  !sqlite3_mutex_h
1c79a 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
1c79b 78 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  x)) ){..      re
1c79c 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 0;..    }..
1c79d 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b    }..  return 1;
1c79e 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  ..}..#endif /* N
1c79f 44 45 42 55 47 20 2a 2f 0d 0a 0d 0a 23 69 66 6e  DEBUG */....#ifn
1c7a0 64 65 66 20 4e 44 45 42 55 47 0d 0a 2f 2a 0d 0a  def NDEBUG../*..
1c7a1 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1c7a2 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 6d 75  f the correct mu
1c7a3 74 65 78 65 73 20 61 72 65 20 68 65 6c 64 20 66  texes are held f
1c7a4 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
1c7a5 0d 0a 2a 2a 20 64 62 2d 3e 61 44 62 5b 69 44 62  ..** db->aDb[iDb
1c7a6 5d 2e 70 53 63 68 65 6d 61 20 73 74 72 75 63 74  ].pSchema struct
1c7a7 75 72 65 2e 20 20 54 68 65 20 6d 75 74 65 78 65  ure.  The mutexe
1c7a8 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73  s required for s
1c7a9 63 68 65 6d 61 0d 0a 2a 2a 20 61 63 63 65 73 73  chema..** access
1c7aa 20 61 72 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20   are:..**..**   
1c7ab 28 31 29 20 54 68 65 20 6d 75 74 65 78 20 6f 6e  (1) The mutex on
1c7ac 20 64 62 0d 0a 2a 2a 20 20 20 28 32 29 20 69 66   db..**   (2) if
1c7ad 20 69 44 62 21 3d 31 2c 20 74 68 65 6e 20 74 68   iDb!=1, then th
1c7ae 65 20 6d 75 74 65 78 20 6f 6e 20 64 62 2d 3e 61  e mutex on db->a
1c7af 44 62 5b 69 44 62 5d 2e 70 42 74 2e 0d 0a 2a 2a  Db[iDb].pBt...**
1c7b0 0d 0a 2a 2a 20 49 66 20 70 53 63 68 65 6d 61 20  ..** If pSchema 
1c7b1 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
1c7b2 6e 20 69 44 62 20 69 73 20 63 6f 6d 70 75 74 65  n iDb is compute
1c7b3 64 20 66 72 6f 6d 20 70 53 63 68 65 6d 61 20 61  d from pSchema a
1c7b4 6e 64 0d 0a 2a 2a 20 64 62 20 75 73 69 6e 67 20  nd..** db using 
1c7b5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1c7b6 6e 64 65 78 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ndex()...*/..SQL
1c7b7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1c7b8 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74  sqlite3SchemaMut
1c7b9 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 20 2a  exHeld(sqlite3 *
1c7ba 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 53 63 68  db, int iDb, Sch
1c7bb 65 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0d 0a  ema *pSchema){..
1c7bc 20 20 42 74 72 65 65 20 2a 70 3b 0d 0a 20 20 61    Btree *p;..  a
1c7bd 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0d  ssert( db!=0 );.
1c7be 0a 20 20 69 66 28 20 70 53 63 68 65 6d 61 20 29  .  if( pSchema )
1c7bf 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1c7c0 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1c7c1 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 61 73 73  pSchema);..  ass
1c7c2 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
1c7c3 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20  Db<db->nDb );.. 
1c7c4 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75   if( !sqlite3_mu
1c7c5 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
1c7c6 65 78 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ex) ) return 0;.
1c7c7 0a 20 20 69 66 28 20 69 44 62 3d 3d 31 20 29 20  .  if( iDb==1 ) 
1c7c8 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 70 20 3d  return 1;..  p =
1c7c9 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42   db->aDb[iDb].pB
1c7ca 74 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 21  t;..  assert( p!
1c7cb 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  =0 );..  return 
1c7cc 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c  p->sharable==0 |
1c7cd 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 3b 0d  | p->locked==1;.
1c7ce 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  .}..#endif /* ND
1c7cf 45 42 55 47 20 2a 2f 0d 0a 0d 0a 23 65 6c 73 65  EBUG */....#else
1c7d0 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41   /* SQLITE_THREA
1c7d1 44 53 41 46 45 3e 30 20 61 62 6f 76 65 2e 20 20  DSAFE>0 above.  
1c7d2 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
1c7d3 45 3d 3d 30 20 62 65 6c 6f 77 20 2a 2f 0d 0a 2f  E==0 below */../
1c7d4 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
1c7d5 69 6e 67 20 61 72 65 20 73 70 65 63 69 61 6c 20  ing are special 
1c7d6 63 61 73 65 73 20 66 6f 72 20 6d 75 74 65 78 20  cases for mutex 
1c7d7 65 6e 74 65 72 20 72 6f 75 74 69 6e 65 73 20 66  enter routines f
1c7d8 6f 72 20 75 73 65 0d 0a 2a 2a 20 69 6e 20 73 69  or use..** in si
1c7d9 6e 67 6c 65 20 74 68 72 65 61 64 65 64 20 61 70  ngle threaded ap
1c7da 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
1c7db 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65  use shared cache
1c7dc 2e 20 20 45 78 63 65 70 74 20 66 6f 72 0d 0a 2a  .  Except for..*
1c7dd 2a 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74  * these two rout
1c7de 69 6e 65 73 2c 20 61 6c 6c 20 6d 75 74 65 78 20  ines, all mutex 
1c7df 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 6e  operations are n
1c7e0 6f 2d 6f 70 73 20 69 6e 20 74 68 61 74 20 63 61  o-ops in that ca
1c7e1 73 65 20 61 6e 64 0d 0a 2a 2a 20 61 72 65 20 6e  se and..** are n
1c7e2 75 6c 6c 20 23 64 65 66 69 6e 65 73 20 69 6e 20  ull #defines in 
1c7e3 62 74 72 65 65 2e 68 2e 0d 0a 2a 2a 0d 0a 2a 2a  btree.h...**..**
1c7e4 20 49 66 20 73 68 61 72 65 64 20 63 61 63 68 65   If shared cache
1c7e5 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68   is disabled, th
1c7e6 65 6e 20 61 6c 6c 20 62 74 72 65 65 20 6d 75 74  en all btree mut
1c7e7 65 78 20 72 6f 75 74 69 6e 65 73 2c 20 69 6e 63  ex routines, inc
1c7e8 6c 75 64 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 6f  luding..** the o
1c7e9 6e 65 73 20 62 65 6c 6f 77 2c 20 61 72 65 20 6e  nes below, are n
1c7ea 6f 2d 6f 70 73 20 61 6e 64 20 61 72 65 20 6e 75  o-ops and are nu
1c7eb 6c 6c 20 23 64 65 66 69 6e 65 73 20 69 6e 20 62  ll #defines in b
1c7ec 74 72 65 65 2e 68 2e 0d 0a 2a 2f 0d 0a 0d 0a 53  tree.h...*/....S
1c7ed 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1c7ee 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
1c7ef 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0d  nter(Btree *p){.
1c7f0 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
1c7f1 70 2d 3e 64 62 3b 0d 0a 7d 0d 0a 53 51 4c 49 54  p->db;..}..SQLIT
1c7f2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1c7f3 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1c7f4 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29  All(sqlite3 *db)
1c7f5 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 66  {..  int i;..  f
1c7f6 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
1c7f7 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 42 74  b; i++){..    Bt
1c7f8 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62  ree *p = db->aDb
1c7f9 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20 20 69 66  [i].pBt;..    if
1c7fa 28 20 70 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d  ( p ){..      p-
1c7fb 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  >pBt->db = p->db
1c7fc 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d  ;..    }..  }..}
1c7fd 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 20 53  ..#endif /* if S
1c7fe 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
1c7ff 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 69   */..#endif /* i
1c800 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1c801 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a  T_SHARED_CACHE *
1c802 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
1c803 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75  **** End of btmu
1c804 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.c **********
1c805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c807 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
1c808 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1c809 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   btree.c *******
1c80a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c80c 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
1c80d 30 34 20 41 70 72 69 6c 20 36 0d 0a 2a 2a 0d 0a  04 April 6..**..
1c80e 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1c80f 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1c810 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1c811 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1c812 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
1c813 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1c814 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
1c815 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1c816 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1c817 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
1c818 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1c819 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1c81a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1c81b 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
1c81c 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1c81d 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1c81e 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1c81f 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
1c820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c821 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c822 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c823 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c824 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
1c825 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78   implements a ex
1c826 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73  ternal (disk-bas
1c827 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69  ed) database usi
1c828 6e 67 20 42 54 72 65 65 73 2e 0d 0a 2a 2a 20 53  ng BTrees...** S
1c829 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f  ee the header co
1c82a 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49  mment on "btreeI
1c82b 6e 74 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69  nt.h" for additi
1c82c 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1c82d 2e 0d 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20  ...** Including 
1c82e 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
1c82f 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64   file format and
1c830 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f 66 20   an overview of 
1c831 6f 70 65 72 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  operation...*/..
1c832 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 68 65 61  ../*..** The hea
1c833 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20  der string that 
1c834 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62  appears at the b
1c835 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
1c836 79 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74  y..** SQLite dat
1c837 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  abase...*/..stat
1c838 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d  ic const char zM
1c839 61 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53  agicHeader[] = S
1c83a 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45  QLITE_FILE_HEADE
1c83b 52 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74  R;..../*..** Set
1c83c 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
1c83d 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20 65 6e  iable to 1 to en
1c83e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75 73 69  able tracing usi
1c83f 6e 67 20 74 68 65 20 54 52 41 43 45 0d 0a 2a 2a  ng the TRACE..**
1c840 20 6d 61 63 72 6f 2e 0d 0a 2a 2f 0d 0a 23 69 66   macro...*/..#if
1c841 20 30 0d 0a 69 6e 74 20 73 71 6c 69 74 65 33 42   0..int sqlite3B
1c842 74 72 65 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a  treeTrace=1;  /*
1c843 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
1c844 74 72 61 63 69 6e 67 20 2a 2f 0d 0a 23 20 64 65  tracing */..# de
1c845 66 69 6e 65 20 54 52 41 43 45 28 58 29 20 20 69  fine TRACE(X)  i
1c846 66 28 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  f(sqlite3BtreeTr
1c847 61 63 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66  ace){printf X;ff
1c848 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 7d 0d 0a  lush(stdout);}..
1c849 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
1c84a 54 52 41 43 45 28 58 29 0d 0a 23 65 6e 64 69 66  TRACE(X)..#endif
1c84b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 74 72 61  ..../*..** Extra
1c84c 63 74 20 61 20 32 2d 62 79 74 65 20 62 69 67 2d  ct a 2-byte big-
1c84d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 66  endian integer f
1c84e 72 6f 6d 20 61 6e 20 61 72 72 61 79 20 6f 66 20  rom an array of 
1c84f 75 6e 73 69 67 6e 65 64 20 62 79 74 65 73 2e 0d  unsigned bytes..
1c850 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 76  .** But if the v
1c851 61 6c 75 65 20 69 73 20 7a 65 72 6f 2c 20 6d 61  alue is zero, ma
1c852 6b 65 20 69 74 20 36 35 35 33 36 2e 0d 0a 2a 2a  ke it 65536...**
1c853 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1c854 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 78 74  e is used to ext
1c855 72 61 63 74 20 74 68 65 20 22 6f 66 66 73 65 74  ract the "offset
1c856 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   to cell content
1c857 20 61 72 65 61 22 20 76 61 6c 75 65 0d 0a 2a 2a   area" value..**
1c858 20 66 72 6f 6d 20 74 68 65 20 68 65 61 64 65 72   from the header
1c859 20 6f 66 20 61 20 62 74 72 65 65 20 70 61 67 65   of a btree page
1c85a 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 73  .  If the page s
1c85b 69 7a 65 20 69 73 20 36 35 35 33 36 20 61 6e 64  ize is 65536 and
1c85c 20 74 68 65 20 70 61 67 65 0d 0a 2a 2a 20 69 73   the page..** is
1c85d 20 65 6d 70 74 79 2c 20 74 68 65 20 6f 66 66 73   empty, the offs
1c85e 65 74 20 73 68 6f 75 6c 64 20 62 65 20 36 35 35  et should be 655
1c85f 33 36 2c 20 62 75 74 20 74 68 65 20 32 2d 62 79  36, but the 2-by
1c860 74 65 20 76 61 6c 75 65 20 73 74 6f 72 65 73 20  te value stores 
1c861 7a 65 72 6f 2e 0d 0a 2a 2a 20 54 68 69 73 20 72  zero...** This r
1c862 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 74 68 65  outine makes the
1c863 20 6e 65 63 65 73 73 61 72 79 20 61 64 6a 75 73   necessary adjus
1c864 74 6d 65 6e 74 20 74 6f 20 36 35 35 33 36 2e 0d  tment to 65536..
1c865 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 67 65 74  .*/..#define get
1c866 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 58 29 20  2byteNotZero(X) 
1c867 20 28 28 28 28 28 69 6e 74 29 67 65 74 32 62 79   (((((int)get2by
1c868 74 65 28 58 29 29 2d 31 29 26 30 78 66 66 66 66  te(X))-1)&0xffff
1c869 29 2b 31 29 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  )+1)....#ifndef 
1c86a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1c86b 45 44 5f 43 41 43 48 45 0d 0a 2f 2a 0d 0a 2a 2a  ED_CACHE../*..**
1c86c 20 41 20 6c 69 73 74 20 6f 66 20 42 74 53 68 61   A list of BtSha
1c86d 72 65 64 20 6f 62 6a 65 63 74 73 20 74 68 61 74  red objects that
1c86e 20 61 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f   are eligible fo
1c86f 72 20 70 61 72 74 69 63 69 70 61 74 69 6f 6e 0d  r participation.
1c870 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64 20 63 61  .** in shared ca
1c871 63 68 65 2e 20 20 54 68 69 73 20 76 61 72 69 61  che.  This varia
1c872 62 6c 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f  ble has file sco
1c873 70 65 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c  pe during normal
1c874 20 62 75 69 6c 64 73 2c 0d 0a 2a 2a 20 62 75 74   builds,..** but
1c875 20 74 68 65 20 74 65 73 74 20 68 61 72 6e 65 73   the test harnes
1c876 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63 65 73  s needs to acces
1c877 73 20 69 74 20 73 6f 20 77 65 20 6d 61 6b 65 20  s it so we make 
1c878 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 20 0d 0a  it global for ..
1c879 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 2e 0d  ** test builds..
1c87a 0a 2a 2a 0d 0a 2a 2a 20 41 63 63 65 73 73 20 74  .**..** Access t
1c87b 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  o this variable 
1c87c 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  is protected by 
1c87d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
1c87e 54 49 43 5f 4d 41 53 54 45 52 2e 0d 0a 2a 2f 0d  TIC_MASTER...*/.
1c87f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1c880 45 53 54 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST..SQLITE_PRIV
1c881 41 54 45 20 42 74 53 68 61 72 65 64 20 2a 53 51  ATE BtShared *SQ
1c882 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
1c883 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20  SharedCacheList 
1c884 3d 20 30 3b 0d 0a 23 65 6c 73 65 0d 0a 73 74 61  = 0;..#else..sta
1c885 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51  tic BtShared *SQ
1c886 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
1c887 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20  SharedCacheList 
1c888 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 65  = 0;..#endif..#e
1c889 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1c88a 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1c88b 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   */....#ifndef S
1c88c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1c88d 44 5f 43 41 43 48 45 0d 0a 2f 2a 0d 0a 2a 2a 20  D_CACHE../*..** 
1c88e 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
1c88f 65 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67  e the shared pag
1c890 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65  er and schema fe
1c891 61 74 75 72 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  atures...**..** 
1c892 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  This routine has
1c893 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78   no effect on ex
1c894 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20  isting database 
1c895 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0d 0a 2a 2a  connections...**
1c896 20 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68   The shared cach
1c897 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74  e setting effect
1c898 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61  s only future ca
1c899 6c 6c 73 20 74 6f 0d 0a 2a 2a 20 73 71 6c 69 74  lls to..** sqlit
1c89a 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74  e3_open(), sqlit
1c89b 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20  e3_open16(), or 
1c89c 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
1c89d 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  )...*/..SQLITE_A
1c89e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1c89f 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
1c8a0 68 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0d  he(int enable){.
1c8a1 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  .  sqlite3Global
1c8a2 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63  Config.sharedCac
1c8a3 68 65 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62  heEnabled = enab
1c8a4 6c 65 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  le;..  return SQ
1c8a5 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e  LITE_OK;..}..#en
1c8a6 64 69 66 0d 0a 0d 0a 0d 0a 0d 0a 23 69 66 64 65  dif........#ifde
1c8a7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1c8a8 41 52 45 44 5f 43 41 43 48 45 0d 0a 20 20 2f 2a  ARED_CACHE..  /*
1c8a9 0d 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74  ..  ** The funct
1c8aa 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72 65 64  ions queryShared
1c8ab 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29  CacheTableLock()
1c8ac 2c 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65  , setSharedCache
1c8ad 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0d 0a 20 20  TableLock(),..  
1c8ae 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53  ** and clearAllS
1c8af 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1c8b0 6f 63 6b 73 28 29 0d 0a 20 20 2a 2a 20 6d 61 6e  ocks()..  ** man
1c8b1 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20  ipulate entries 
1c8b2 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  in the BtShared.
1c8b3 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73  pLock linked lis
1c8b4 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0d  t used to store.
1c8b5 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63  .  ** shared-cac
1c8b6 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c  he table level l
1c8b7 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 62  ocks. If the lib
1c8b8 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64  rary is compiled
1c8b9 20 77 69 74 68 20 74 68 65 0d 0a 20 20 2a 2a 20   with the..  ** 
1c8ba 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61  shared-cache fea
1c8bb 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74  ture disabled, t
1c8bc 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  hen there is onl
1c8bd 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0d  y ever one user.
1c8be 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74  .  ** of each Bt
1c8bf 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
1c8c0 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63   and so this loc
1c8c1 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65  king is not nece
1c8c2 73 73 61 72 79 2e 20 0d 0a 20 20 2a 2a 20 53 6f  ssary. ..  ** So
1c8c3 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b   define the lock
1c8c4 20 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f   related functio
1c8c5 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0d 0a 20  ns as no-ops... 
1c8c6 20 2a 2f 0d 0a 20 20 23 64 65 66 69 6e 65 20 71   */..  #define q
1c8c7 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
1c8c8 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20  ableLock(a,b,c) 
1c8c9 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20 20 23 64 65  SQLITE_OK..  #de
1c8ca 66 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 61  fine setSharedCa
1c8cb 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62  cheTableLock(a,b
1c8cc 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20  ,c) SQLITE_OK.. 
1c8cd 20 23 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c   #define clearAl
1c8ce 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
1c8cf 65 4c 6f 63 6b 73 28 61 29 0d 0a 20 20 23 64 65  eLocks(a)..  #de
1c8d0 66 69 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 6c  fine downgradeAl
1c8d1 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
1c8d2 65 4c 6f 63 6b 73 28 61 29 0d 0a 20 20 23 64 65  eLocks(a)..  #de
1c8d3 66 69 6e 65 20 68 61 73 53 68 61 72 65 64 43 61  fine hasSharedCa
1c8d4 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62  cheTableLock(a,b
1c8d5 2c 63 2c 64 29 20 31 0d 0a 20 20 23 64 65 66 69  ,c,d) 1..  #defi
1c8d6 6e 65 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69  ne hasReadConfli
1c8d7 63 74 73 28 61 2c 20 62 29 20 30 0d 0a 23 65 6e  cts(a, b) 0..#en
1c8d8 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  dif....#ifndef S
1c8d9 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1c8da 44 5f 43 41 43 48 45 0d 0a 0d 0a 23 69 66 64 65  D_CACHE....#ifde
1c8db 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a  f SQLITE_DEBUG..
1c8dc 2f 2a 0d 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75  /*..**** This fu
1c8dd 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75  nction is only u
1c8de 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
1c8df 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
1c8e0 6d 65 6e 74 2e 20 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  ment. ***..**..*
1c8e1 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c8e2 66 20 70 42 74 72 65 65 20 68 6f 6c 64 73 20 74  f pBtree holds t
1c8e3 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b  he required lock
1c8e4 73 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  s to read or wri
1c8e5 74 65 20 74 6f 20 74 68 65 20 0d 0a 2a 2a 20 74  te to the ..** t
1c8e6 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70  able with root p
1c8e7 61 67 65 20 69 52 6f 6f 74 2e 20 20 20 52 65 74  age iRoot.   Ret
1c8e8 75 72 6e 20 31 20 69 66 20 69 74 20 64 6f 65 73  urn 1 if it does
1c8e9 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0d 0a   and 0 if not...
1c8ea 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  **..** For examp
1c8eb 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67  le, when writing
1c8ec 20 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68   to a table with
1c8ed 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74   root-page iRoot
1c8ee 20 76 69 61 20 0d 0a 2a 2a 20 42 74 72 65 65 20   via ..** Btree 
1c8ef 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 74 72 65  connection pBtre
1c8f0 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 61 73  e:..**..**    as
1c8f1 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43  sert( hasSharedC
1c8f2 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 42  acheTableLock(pB
1c8f3 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 2c 20  tree, iRoot, 0, 
1c8f4 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b 0d 0a  WRITE_LOCK) );..
1c8f5 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74  **..** When writ
1c8f6 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20  ing to an index 
1c8f7 74 68 61 74 20 72 65 73 69 64 65 73 20 69 6e 20  that resides in 
1c8f8 61 20 73 68 61 72 61 62 6c 65 20 64 61 74 61 62  a sharable datab
1c8f9 61 73 65 2c 20 74 68 65 20 0d 0a 2a 2a 20 63 61  ase, the ..** ca
1c8fa 6c 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76 65  ller should have
1c8fb 20 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64 20   first obtained 
1c8fc 61 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69 6e  a lock specifyin
1c8fd 67 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  g the root page 
1c8fe 6f 66 0d 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65  of..** the corre
1c8ff 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20  sponding table. 
1c900 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67  This makes thing
1c901 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d  s a bit more com
1c902 70 6c 69 63 61 74 65 64 2c 0d 0a 2a 2a 20 61 73  plicated,..** as
1c903 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65   this module tre
1c904 61 74 73 20 65 61 63 68 20 74 61 62 6c 65 20 61  ats each table a
1c905 73 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72  s a separate str
1c906 75 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72  ucture. To deter
1c907 6d 69 6e 65 0d 0a 2a 2a 20 74 68 65 20 74 61 62  mine..** the tab
1c908 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  le corresponding
1c909 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 62 65   to the index be
1c90a 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 74 68 69  ing written, thi
1c90b 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68  s..** function h
1c90c 61 73 20 74 6f 20 73 65 61 72 63 68 20 74 68 72  as to search thr
1c90d 6f 75 67 68 20 74 68 65 20 64 61 74 61 62 61 73  ough the databas
1c90e 65 20 73 63 68 65 6d 61 2e 0d 0a 2a 2a 0d 0a 2a  e schema...**..*
1c90f 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c  * Instead of a l
1c910 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
1c911 2f 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74  /index rooted at
1c912 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65   page iRoot, the
1c913 20 63 61 6c 6c 65 72 20 6d 61 79 0d 0a 2a 2a 20   caller may..** 
1c914 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63  hold a write-loc
1c915 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20  k on the schema 
1c916 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65  table (root page
1c917 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73   1). This is als
1c918 6f 0d 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65  o..** acceptable
1c919 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
1c91a 74 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65  t hasSharedCache
1c91b 54 61 62 6c 65 4c 6f 63 6b 28 0d 0a 20 20 42 74  TableLock(..  Bt
1c91c 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
1c91d 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74       /* Handle t
1c91e 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f  hat must hold lo
1c91f 63 6b 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 69 52  ck */..  Pgno iR
1c920 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  oot,            
1c921 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20  /* Root page of 
1c922 62 2d 74 72 65 65 20 2a 2f 0d 0a 20 20 69 6e 74  b-tree */..  int
1c923 20 69 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20   isIndex,       
1c924 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
1c925 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74  Root is the root
1c926 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74   of an index b-t
1c927 72 65 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 4c  ree */..  int eL
1c928 6f 63 6b 54 79 70 65 20 20 20 20 20 20 20 20 20  ockType         
1c929 20 2f 2a 20 52 65 71 75 69 72 65 64 20 6c 6f 63   /* Required loc
1c92a 6b 20 74 79 70 65 20 28 52 45 41 44 5f 4c 4f 43  k type (READ_LOC
1c92b 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29  K or WRITE_LOCK)
1c92c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 53 63 68 65 6d   */..){..  Schem
1c92d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63  a *pSchema = (Sc
1c92e 68 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e 70  hema *)pBtree->p
1c92f 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0d 0a 20 20  Bt->pSchema;..  
1c930 50 67 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0d 0a  Pgno iTab = 0;..
1c931 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b    BtLock *pLock;
1c932 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  ....  /* If this
1c933 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
1c934 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72 20 69   shareable, or i
1c935 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
1c936 72 65 61 64 69 6e 67 0d 0a 20 20 2a 2a 20 61 6e  reading..  ** an
1c937 64 20 68 61 73 20 74 68 65 20 72 65 61 64 2d 75  d has the read-u
1c938 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
1c939 73 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f 63  set, then no loc
1c93a 6b 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 0d  k is required. .
1c93b 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  .  ** Return tru
1c93c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a  e immediately...
1c93d 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 70 42 74    */..  if( (pBt
1c93e 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ree->sharable==0
1c93f 29 0d 0a 20 20 20 7c 7c 20 28 65 4c 6f 63 6b 54  )..   || (eLockT
1c940 79 70 65 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 26  ype==READ_LOCK &
1c941 26 20 28 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66  & (pBtree->db->f
1c942 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65  lags & SQLITE_Re
1c943 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 29 0d  adUncommitted)).
1c944 0a 20 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  .  ){..    retur
1c945 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  n 1;..  }....  /
1c946 2a 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74 20  * If the client 
1c947 69 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20 77  is reading  or w
1c948 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20  riting an index 
1c949 61 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20 69  and the schema i
1c94a 73 0d 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64  s..  ** not load
1c94b 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74  ed, then it is t
1c94c 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20  oo difficult to 
1c94d 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74  actually check t
1c94e 6f 20 73 65 65 20 69 66 0d 0a 20 20 2a 2a 20 74  o see if..  ** t
1c94f 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73  he correct locks
1c950 20 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 64   are held.  So d
1c951 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a  o not bother - j
1c952 75 73 74 20 72 65 74 75 72 6e 20 74 72 75 65 2e  ust return true.
1c953 0d 0a 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65  ..  ** This case
1c954 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75   does not come u
1c955 70 20 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79  p very often any
1c956 68 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  how...  */..  if
1c957 28 20 69 73 49 6e 64 65 78 20 26 26 20 28 21 70  ( isIndex && (!p
1c958 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 65  Schema || (pSche
1c959 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63 68  ma->flags&DB_Sch
1c95a 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20 29  emaLoaded)==0) )
1c95b 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  {..    return 1;
1c95c 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 46 69  ..  }....  /* Fi
1c95d 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f 6f  gure out the roo
1c95e 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 20  t-page that the 
1c95f 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68  lock should be h
1c960 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c  eld on. For tabl
1c961 65 0d 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c  e..  ** b-trees,
1c962 20 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 68   this is just th
1c963 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
1c964 68 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20  he b-tree being 
1c965 72 65 61 64 20 6f 72 0d 0a 20 20 2a 2a 20 77 72  read or..  ** wr
1c966 69 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78  itten. For index
1c967 20 62 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20   b-trees, it is 
1c968 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  the root page of
1c969 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0d   the associated.
1c96a 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f  .  ** table.  */
1c96b 0d 0a 20 20 69 66 28 20 69 73 49 6e 64 65 78 20  ..  if( isIndex 
1c96c 29 7b 0d 0a 20 20 20 20 48 61 73 68 45 6c 65 6d  ){..    HashElem
1c96d 20 2a 70 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d   *p;..    for(p=
1c96e 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
1c96f 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  &pSchema->idxHas
1c970 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  h); p; p=sqliteH
1c971 61 73 68 4e 65 78 74 28 70 29 29 7b 0d 0a 20 20  ashNext(p)){..  
1c972 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
1c973 3d 20 28 49 6e 64 65 78 20 2a 29 73 71 6c 69 74  = (Index *)sqlit
1c974 65 48 61 73 68 44 61 74 61 28 70 29 3b 0d 0a 20  eHashData(p);.. 
1c975 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74       if( pIdx->t
1c976 6e 75 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f 74 20  num==(int)iRoot 
1c977 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 54 61 62  ){..        iTab
1c978 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d   = pIdx->pTable-
1c979 3e 74 6e 75 6d 3b 0d 0a 20 20 20 20 20 20 7d 0d  >tnum;..      }.
1c97a 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
1c97b 0d 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f  ..    iTab = iRo
1c97c 6f 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ot;..  }....  /*
1c97d 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20   Search for the 
1c97e 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45  required lock. E
1c97f 69 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c 6f  ither a write-lo
1c980 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20  ck on root-page 
1c981 69 54 61 62 2c 20 61 20 0d 0a 20 20 2a 2a 20 77  iTab, a ..  ** w
1c982 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
1c983 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f   schema table, o
1c984 72 20 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74  r (if the client
1c985 20 69 73 20 72 65 61 64 69 6e 67 29 20 61 0d 0a   is reading) a..
1c986 20 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f    ** read-lock o
1c987 6e 20 69 54 61 62 20 77 69 6c 6c 20 73 75 66 66  n iTab will suff
1c988 69 63 65 2e 20 52 65 74 75 72 6e 20 31 20 69 66  ice. Return 1 if
1c989 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72   any of these ar
1c98a 65 20 66 6f 75 6e 64 2e 20 20 2a 2f 0d 0a 20 20  e found.  */..  
1c98b 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65 65  for(pLock=pBtree
1c98c 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c  ->pBt->pLock; pL
1c98d 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b  ock; pLock=pLock
1c98e 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69  ->pNext){..    i
1c98f 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65  f( pLock->pBtree
1c990 3d 3d 70 42 74 72 65 65 20 0d 0a 20 20 20 20 20  ==pBtree ..     
1c991 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c  && (pLock->iTabl
1c992 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f 63  e==iTab || (pLoc
1c993 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  k->eLock==WRITE_
1c994 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 69  LOCK && pLock->i
1c995 54 61 62 6c 65 3d 3d 31 29 29 0d 0a 20 20 20 20  Table==1))..    
1c996 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b   && pLock->eLock
1c997 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0d 0a 20 20  >=eLockType ..  
1c998 20 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75    ){..      retu
1c999 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
1c99a 7d 0d 0a 0d 0a 20 20 2f 2a 20 46 61 69 6c 65 64  }....  /* Failed
1c99b 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65 71   to find the req
1c99c 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0d 0a  uired lock. */..
1c99d 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a    return 0;..}..
1c99e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1c99f 5f 44 45 42 55 47 20 2a 2f 0d 0a 0d 0a 23 69 66  _DEBUG */....#if
1c9a0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1c9a1 0d 0a 2f 2a 0d 0a 2a 2a 2a 2a 20 54 68 69 73 20  ../*..**** This 
1c9a2 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
1c9a3 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
1c9a4 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
1c9a5 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0d 0a  nts only. ****..
1c9a6 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  **..** Return tr
1c9a7 75 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20 62  ue if it would b
1c9a8 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42  e illegal for pB
1c9a9 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 6e  tree to write in
1c9aa 74 6f 20 74 68 65 0d 0a 2a 2a 20 74 61 62 6c 65  to the..** table
1c9ab 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64   or index rooted
1c9ac 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73   at iRoot becaus
1c9ad 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 20 63  e other shared c
1c9ae 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0d 0a  onnections are..
1c9af 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c  ** simultaneousl
1c9b0 79 20 72 65 61 64 69 6e 67 20 74 68 61 74 20 73  y reading that s
1c9b1 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ame table or ind
1c9b2 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74 20 69  ex...**..** It i
1c9b3 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42  s illegal for pB
1c9b4 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 66  tree to write if
1c9b5 20 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 65   some other Btre
1c9b6 65 20 6f 62 6a 65 63 74 20 74 68 61 74 0d 0a 2a  e object that..*
1c9b7 2a 20 73 68 61 72 65 73 20 74 68 65 20 73 61 6d  * shares the sam
1c9b8 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
1c9b9 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72  t is currently r
1c9ba 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
1c9bb 67 0d 0a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20  g..** the iRoot 
1c9bc 74 61 62 6c 65 2e 20 20 45 78 63 65 70 74 2c 20  table.  Except, 
1c9bd 69 66 20 74 68 65 20 6f 74 68 65 72 20 42 74 72  if the other Btr
1c9be 65 65 20 6f 62 6a 65 63 74 20 68 61 73 20 74 68  ee object has th
1c9bf 65 0d 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f 6d  e..** read-uncom
1c9c0 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c  mitted flag set,
1c9c1 20 74 68 65 6e 20 69 74 20 69 73 20 4f 4b 20 66   then it is OK f
1c9c2 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 62 6a  or the other obj
1c9c3 65 63 74 20 74 6f 0d 0a 2a 2a 20 68 61 76 65 20  ect to..** have 
1c9c4 61 20 72 65 61 64 20 63 75 72 73 6f 72 2e 0d 0a  a read cursor...
1c9c5 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  **..** For examp
1c9c6 6c 65 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69  le, before writi
1c9c7 6e 67 20 74 6f 20 61 6e 79 20 70 61 72 74 20 6f  ng to any part o
1c9c8 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  f the table or i
1c9c9 6e 64 65 78 0d 0a 2a 2a 20 72 6f 6f 74 65 64 20  ndex..** rooted 
1c9ca 61 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f  at page iRoot, o
1c9cb 6e 65 20 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0d  ne should call:.
1c9cc 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 61 73 73 65 72  .**..**    asser
1c9cd 74 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c  t( !hasReadConfl
1c9ce 69 63 74 73 28 70 42 74 72 65 65 2c 20 69 52 6f  icts(pBtree, iRo
1c9cf 6f 74 29 20 29 3b 0d 0a 2a 2f 0d 0a 73 74 61 74  ot) );..*/..stat
1c9d0 69 63 20 69 6e 74 20 68 61 73 52 65 61 64 43 6f  ic int hasReadCo
1c9d1 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20 2a 70  nflicts(Btree *p
1c9d2 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52 6f 6f  Btree, Pgno iRoo
1c9d3 74 29 7b 0d 0a 20 20 42 74 43 75 72 73 6f 72 20  t){..  BtCursor 
1c9d4 2a 70 3b 0d 0a 20 20 66 6f 72 28 70 3d 70 42 74  *p;..  for(p=pBt
1c9d5 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f  ree->pBt->pCurso
1c9d6 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
1c9d7 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70  ){..    if( p->p
1c9d8 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0d  gnoRoot==iRoot .
1c9d9 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72  .     && p->pBtr
1c9da 65 65 21 3d 70 42 74 72 65 65 0d 0a 20 20 20 20  ee!=pBtree..    
1c9db 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65   && 0==(p->pBtre
1c9dc 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
1c9dd 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
1c9de 69 74 74 65 64 29 0d 0a 20 20 20 20 29 7b 0d 0a  itted)..    ){..
1c9df 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d        return 1;.
1c9e0 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
1c9e1 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 23 65 6e  eturn 0;..}..#en
1c9e2 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66  dif    /* #ifdef
1c9e3 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f   SQLITE_DEBUG */
1c9e4 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 51 75 65 72 79  ..../*..** Query
1c9e5 20 74 6f 20 73 65 65 20 69 66 20 42 74 72 65 65   to see if Btree
1c9e6 20 68 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62   handle p may ob
1c9e7 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74  tain a lock of t
1c9e8 79 70 65 20 65 4c 6f 63 6b 20 0d 0a 2a 2a 20 28  ype eLock ..** (
1c9e9 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49  READ_LOCK or WRI
1c9ea 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20  TE_LOCK) on the 
1c9eb 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
1c9ec 70 61 67 65 20 69 54 61 62 2e 20 52 65 74 75 72  page iTab. Retur
1c9ed 6e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  n..** SQLITE_OK 
1c9ee 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79 20  if the lock may 
1c9ef 62 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79 20  be obtained (by 
1c9f0 63 61 6c 6c 69 6e 67 0d 0a 2a 2a 20 73 65 74 53  calling..** setS
1c9f1 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1c9f2 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 54  ock()), or SQLIT
1c9f3 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e  E_LOCKED if not.
1c9f4 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1c9f5 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
1c9f6 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65  eTableLock(Btree
1c9f7 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20   *p, Pgno iTab, 
1c9f8 75 38 20 65 4c 6f 63 6b 29 7b 0d 0a 20 20 42 74  u8 eLock){..  Bt
1c9f9 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1c9fa 3e 70 42 74 3b 0d 0a 20 20 42 74 4c 6f 63 6b 20  >pBt;..  BtLock 
1c9fb 2a 70 49 74 65 72 3b 0d 0a 0d 0a 20 20 61 73 73  *pIter;....  ass
1c9fc 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1c9fd 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
1c9fe 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f  ;..  assert( eLo
1c9ff 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c  ck==READ_LOCK ||
1ca00 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f   eLock==WRITE_LO
1ca01 43 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  CK );..  assert(
1ca02 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0d 0a 20 20   p->db!=0 );..  
1ca03 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62 2d  assert( !(p->db-
1ca04 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
1ca05 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c  adUncommitted)||
1ca06 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
1ca07 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0d 0a 20  K||iTab==1 );.. 
1ca08 20 0d 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65   ..  /* If reque
1ca09 73 74 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f  sting a write-lo
1ca0a 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72  ck, then the Btr
1ca0b 65 65 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20  ee must have an 
1ca0c 6f 70 65 6e 20 77 72 69 74 65 0d 0a 20 20 2a 2a  open write..  **
1ca0d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
1ca0e 74 68 69 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20  this file. And, 
1ca0f 6f 62 76 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74  obviously, for t
1ca10 68 69 73 20 74 6f 20 62 65 20 73 6f 20 74 68 65  his to be so the
1ca11 72 65 20 0d 0a 20 20 2a 2a 20 6d 75 73 74 20 62  re ..  ** must b
1ca12 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20  e an open write 
1ca13 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
1ca14 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0d  he file itself..
1ca15 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
1ca16 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43   eLock==READ_LOC
1ca17 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70 57  K || (p==pBt->pW
1ca18 72 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54 72  riter && p->inTr
1ca19 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1ca1a 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1ca1b 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
1ca1c 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   || pBt->inTrans
1ca1d 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
1ca1e 49 54 45 20 29 3b 0d 0a 20 20 0d 0a 20 20 2f 2a  ITE );..  ..  /*
1ca1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1ca20 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20   a no-op if the 
1ca21 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20  shared-cache is 
1ca22 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0d 0a  not enabled */..
1ca23 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62    if( !p->sharab
1ca24 6c 65 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  le ){..    retur
1ca25 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
1ca26 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 73 6f 6d  }....  /* If som
1ca27 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
1ca28 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e  on is holding an
1ca29 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c   exclusive lock,
1ca2a 20 74 68 65 0d 0a 20 20 2a 2a 20 72 65 71 75 65   the..  ** reque
1ca2b 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f  sted lock may no
1ca2c 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0d 0a  t be obtained...
1ca2d 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 42 74 2d    */..  if( pBt-
1ca2e 3e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 28  >pWriter!=p && (
1ca2f 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20  pBt->btsFlags & 
1ca30 42 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 3d  BTS_EXCLUSIVE)!=
1ca31 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0 ){..    sqlite
1ca32 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
1ca33 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70  ed(p->db, pBt->p
1ca34 57 72 69 74 65 72 2d 3e 64 62 29 3b 0d 0a 20 20  Writer->db);..  
1ca35 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1ca36 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
1ca37 48 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 66 6f  HE;..  }....  fo
1ca38 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f  r(pIter=pBt->pLo
1ca39 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72  ck; pIter; pIter
1ca3a 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0d  =pIter->pNext){.
1ca3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64  .    /* The cond
1ca3c 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c  ition (pIter->eL
1ca3d 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74  ock!=eLock) in t
1ca3e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28  he following if(
1ca3f 2e 2e 2e 29 20 0d 0a 20 20 20 20 2a 2a 20 73 74  ...) ..    ** st
1ca40 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d  atement is a sim
1ca41 70 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0d  plification of:.
1ca42 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
1ca43 20 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f    (eLock==WRITE_
1ca44 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65  LOCK || pIter->e
1ca45 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1ca46 29 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  )..    **..    *
1ca47 2a 20 73 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20  * since we know 
1ca48 74 68 61 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57  that if eLock==W
1ca49 52 49 54 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20  RITE_LOCK, then 
1ca4a 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  no other connect
1ca4b 69 6f 6e 0d 0a 20 20 20 20 2a 2a 20 6d 61 79 20  ion..    ** may 
1ca4c 68 6f 6c 64 20 61 20 57 52 49 54 45 5f 4c 4f 43  hold a WRITE_LOC
1ca4d 4b 20 6f 6e 20 61 6e 79 20 74 61 62 6c 65 20 69  K on any table i
1ca4e 6e 20 74 68 69 73 20 66 69 6c 65 20 28 73 69 6e  n this file (sin
1ca4f 63 65 20 74 68 65 72 65 20 63 61 6e 0d 0a 20 20  ce there can..  
1ca50 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73    ** only be a s
1ca51 69 6e 67 6c 65 20 77 72 69 74 65 72 29 2e 0d 0a  ingle writer)...
1ca52 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65      */..    asse
1ca53 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b  rt( pIter->eLock
1ca54 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70  ==READ_LOCK || p
1ca55 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49  Iter->eLock==WRI
1ca56 54 45 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 20 20  TE_LOCK );..    
1ca57 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
1ca58 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65  EAD_LOCK || pIte
1ca59 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20  r->pBtree==p || 
1ca5a 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45  pIter->eLock==RE
1ca5b 41 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 20 20 69  AD_LOCK);..    i
1ca5c 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65  f( pIter->pBtree
1ca5d 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54  !=p && pIter->iT
1ca5e 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49  able==iTab && pI
1ca5f 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63  ter->eLock!=eLoc
1ca60 6b 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  k ){..      sqli
1ca61 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
1ca62 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65  cked(p->db, pIte
1ca63 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0d  r->pBtree->db);.
1ca64 0a 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b  .      if( eLock
1ca65 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0d  ==WRITE_LOCK ){.
1ca66 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1ca67 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72   p==pBt->pWriter
1ca68 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 42 74   );..        pBt
1ca69 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54  ->btsFlags |= BT
1ca6a 53 5f 50 45 4e 44 49 4e 47 3b 0d 0a 20 20 20 20  S_PENDING;..    
1ca6b 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74 75 72    }..      retur
1ca6c 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  n SQLITE_LOCKED_
1ca6d 53 48 41 52 45 44 43 41 43 48 45 3b 0d 0a 20 20  SHAREDCACHE;..  
1ca6e 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
1ca6f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
1ca70 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  ..#endif /* !SQL
1ca71 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1ca72 43 41 43 48 45 20 2a 2f 0d 0a 0d 0a 23 69 66 6e  CACHE */....#ifn
1ca73 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ca74 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 2f 2a  SHARED_CACHE../*
1ca75 0d 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20  ..** Add a lock 
1ca76 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
1ca77 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
1ca78 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64  le to the shared
1ca79 2d 62 74 72 65 65 20 75 73 65 64 0d 0a 2a 2a 20  -btree used..** 
1ca7a 62 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20  by Btree handle 
1ca7b 70 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f  p. Parameter eLo
1ca7c 63 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ck must be eithe
1ca7d 72 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0d  r READ_LOCK or .
1ca7e 0a 2a 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0d  .** WRITE_LOCK..
1ca7f 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  .**..** This fun
1ca80 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
1ca81 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a  e following:..**
1ca82 0d 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 20 73  ..**   (a) The s
1ca83 70 65 63 69 66 69 65 64 20 42 74 72 65 65 20 6f  pecified Btree o
1ca84 62 6a 65 63 74 20 70 20 69 73 20 63 6f 6e 6e 65  bject p is conne
1ca85 63 74 65 64 20 74 6f 20 61 20 73 68 61 72 61 62  cted to a sharab
1ca86 6c 65 0d 0a 2a 2a 20 20 20 20 20 20 20 64 61 74  le..**       dat
1ca87 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 68 20  abase (one with 
1ca88 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 68 61  the BtShared.sha
1ca89 72 61 62 6c 65 20 66 6c 61 67 20 73 65 74 29 2c  rable flag set),
1ca8a 20 61 6e 64 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28   and..**..**   (
1ca8b 62 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72 65  b) No other Btre
1ca8c 65 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20 61  e objects hold a
1ca8d 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c   lock that confl
1ca8e 69 63 74 73 0d 0a 2a 2a 20 20 20 20 20 20 20 77  icts..**       w
1ca8f 69 74 68 20 74 68 65 20 72 65 71 75 65 73 74 65  ith the requeste
1ca90 64 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65  d lock (i.e. que
1ca91 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
1ca92 6c 65 4c 6f 63 6b 28 29 20 68 61 73 0d 0a 2a 2a  leLock() has..**
1ca93 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 62         already b
1ca94 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72  een called and r
1ca95 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f  eturned SQLITE_O
1ca96 4b 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49  K)...**..** SQLI
1ca97 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
1ca98 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73  d if the lock is
1ca99 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66 75   added successfu
1ca9a 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  lly. SQLITE_NOME
1ca9b 4d 20 0d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  M ..** is return
1ca9c 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 61  ed if a malloc a
1ca9d 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0d 0a 2a  ttempt fails...*
1ca9e 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  /..static int se
1ca9f 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
1caa0 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20  eLock(Btree *p, 
1caa1 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20  Pgno iTable, u8 
1caa2 65 4c 6f 63 6b 29 7b 0d 0a 20 20 42 74 53 68 61  eLock){..  BtSha
1caa3 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1caa4 74 3b 0d 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c  t;..  BtLock *pL
1caa5 6f 63 6b 20 3d 20 30 3b 0d 0a 20 20 42 74 4c 6f  ock = 0;..  BtLo
1caa6 63 6b 20 2a 70 49 74 65 72 3b 0d 0a 0d 0a 20 20  ck *pIter;....  
1caa7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1caa8 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
1caa9 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1caaa 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
1caab 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45   || eLock==WRITE
1caac 5f 4c 4f 43 4b 20 29 3b 0d 0a 20 20 61 73 73 65  _LOCK );..  asse
1caad 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0d  rt( p->db!=0 );.
1caae 0a 0d 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63  ...  /* A connec
1caaf 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65  tion with the re
1cab0 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66  ad-uncommitted f
1cab1 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76  lag set will nev
1cab2 65 72 20 74 72 79 20 74 6f 0d 0a 20 20 2a 2a 20  er try to..  ** 
1cab3 6f 62 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f  obtain a read-lo
1cab4 63 6b 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  ck using this fu
1cab5 6e 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79  nction. The only
1cab6 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69   read-lock obtai
1cab7 6e 65 64 0d 0a 20 20 2a 2a 20 62 79 20 61 20 63  ned..  ** by a c
1cab8 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 61  onnection in rea
1cab9 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f  d-uncommitted mo
1caba 64 65 20 69 73 20 6f 6e 20 74 68 65 20 73 71 6c  de is on the sql
1cabb 69 74 65 5f 6d 61 73 74 65 72 20 0d 0a 20 20 2a  ite_master ..  *
1cabc 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 61  * table, and tha
1cabd 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  t lock is obtain
1cabe 65 64 20 69 6e 20 42 74 72 65 65 42 65 67 69 6e  ed in BtreeBegin
1cabf 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0d 0a 20 20  Trans().  */..  
1cac0 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d 3e 64  assert( 0==(p->d
1cac1 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
1cac2 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29  ReadUncommitted)
1cac3 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45   || eLock==WRITE
1cac4 5f 4c 4f 43 4b 20 29 3b 0d 0a 0d 0a 20 20 2f 2a  _LOCK );....  /*
1cac5 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
1cac6 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61  hould only be ca
1cac7 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 62  lled on a sharab
1cac8 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 20  le b-tree after 
1cac9 69 74 20 0d 0a 20 20 2a 2a 20 68 61 73 20 62 65  it ..  ** has be
1caca 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68  en determined th
1cacb 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72  at no other b-tr
1cacc 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c  ee holds a confl
1cacd 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f  icting lock.  */
1cace 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  ..  assert( p->s
1cacf 68 61 72 61 62 6c 65 20 29 3b 0d 0a 20 20 61 73  harable );..  as
1cad0 73 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d  sert( SQLITE_OK=
1cad1 3d 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68  =querySharedCach
1cad2 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54  eTableLock(p, iT
1cad3 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0d  able, eLock) );.
1cad4 0a 0d 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65  ...  /* First se
1cad5 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f  arch the list fo
1cad6 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f  r an existing lo
1cad7 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  ck on this table
1cad8 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 70 49 74 65  . */..  for(pIte
1cad9 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
1cada 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
1cadb 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69  ->pNext){..    i
1cadc 66 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65  f( pIter->iTable
1cadd 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65  ==iTable && pIte
1cade 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0d  r->pBtree==p ){.
1cadf 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70  .      pLock = p
1cae0 49 74 65 72 3b 0d 0a 20 20 20 20 20 20 62 72 65  Iter;..      bre
1cae1 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ak;..    }..  }.
1cae2 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  ...  /* If the a
1cae3 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 64 20  bove search did 
1cae4 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63  not find a BtLoc
1cae5 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 69 61  k struct associa
1cae6 74 69 6e 67 20 42 74 72 65 65 20 70 0d 0a 20 20  ting Btree p..  
1cae7 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 54  ** with table iT
1cae8 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f  able, allocate o
1cae9 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69  ne and link it i
1caea 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e 0d 0a 20  nto the list... 
1caeb 20 2a 2f 0d 0a 20 20 69 66 28 20 21 70 4c 6f 63   */..  if( !pLoc
1caec 6b 20 29 7b 0d 0a 20 20 20 20 70 4c 6f 63 6b 20  k ){..    pLock 
1caed 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69  = (BtLock *)sqli
1caee 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
1caef 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0d 0a  zeof(BtLock));..
1caf0 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29      if( !pLock )
1caf1 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
1caf2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
1caf3 20 20 20 7d 0d 0a 20 20 20 20 70 4c 6f 63 6b 2d     }..    pLock-
1caf4 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65  >iTable = iTable
1caf5 3b 0d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42  ;..    pLock->pB
1caf6 74 72 65 65 20 3d 20 70 3b 0d 0a 20 20 20 20 70  tree = p;..    p
1caf7 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42  Lock->pNext = pB
1caf8 74 2d 3e 70 4c 6f 63 6b 3b 0d 0a 20 20 20 20 70  t->pLock;..    p
1caf9 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63  Bt->pLock = pLoc
1cafa 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  k;..  }....  /* 
1cafb 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65  Set the BtLock.e
1cafc 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f  Lock variable to
1cafd 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20   the maximum of 
1cafe 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b  the current lock
1caff 0d 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72  ..  ** and the r
1cb00 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54  equested lock. T
1cb01 68 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20 77  his means if a w
1cb02 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c  rite-lock was al
1cb03 72 65 61 64 79 20 68 65 6c 64 0d 0a 20 20 2a 2a  ready held..  **
1cb04 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b   and a read-lock
1cb05 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 20 64   requested, we d
1cb06 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79  on't incorrectly
1cb07 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c   downgrade the l
1cb08 6f 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73  ock...  */..  as
1cb09 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b  sert( WRITE_LOCK
1cb0a 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0d 0a 20  >READ_LOCK );.. 
1cb0b 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b   if( eLock>pLock
1cb0c 2d 3e 65 4c 6f 63 6b 20 29 7b 0d 0a 20 20 20 20  ->eLock ){..    
1cb0d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65  pLock->eLock = e
1cb0e 4c 6f 63 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  Lock;..  }....  
1cb0f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1cb10 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
1cb11 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
1cb12 52 45 44 5f 43 41 43 48 45 20 2a 2f 0d 0a 0d 0a  RED_CACHE */....
1cb13 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1cb14 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1cb15 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65  ../*..** Release
1cb16 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c   all the table l
1cb17 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61  ocks (locks obta
1cb18 69 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74  ined via calls t
1cb19 6f 0d 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61  o..** the setSha
1cb1a 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
1cb1b 6b 28 29 20 70 72 6f 63 65 64 75 72 65 29 20 68  k() procedure) h
1cb1c 65 6c 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a  eld by Btree obj
1cb1d 65 63 74 20 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ect p...**..** T
1cb1e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  his function ass
1cb1f 75 6d 65 73 20 74 68 61 74 20 42 74 72 65 65 20  umes that Btree 
1cb20 70 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65  p has an open re
1cb21 61 64 20 6f 72 20 77 72 69 74 65 20 0d 0a 2a 2a  ad or write ..**
1cb22 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66   transaction. If
1cb23 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68   it does not, th
1cb24 65 6e 20 74 68 65 20 42 54 53 5f 50 45 4e 44 49  en the BTS_PENDI
1cb25 4e 47 20 66 6c 61 67 0d 0a 2a 2a 20 6d 61 79 20  NG flag..** may 
1cb26 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 63  be incorrectly c
1cb27 6c 65 61 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61  leared...*/..sta
1cb28 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c  tic void clearAl
1cb29 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
1cb2a 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29  eLocks(Btree *p)
1cb2b 7b 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  {..  BtShared *p
1cb2c 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20  Bt = p->pBt;..  
1cb2d 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20  BtLock **ppIter 
1cb2e 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0d 0a  = &pBt->pLock;..
1cb2f 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1cb30 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
1cb31 65 78 28 70 29 20 29 3b 0d 0a 20 20 61 73 73 65  ex(p) );..  asse
1cb32 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
1cb33 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b  || 0==*ppIter );
1cb34 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ..  assert( p->i
1cb35 6e 54 72 61 6e 73 3e 30 20 29 3b 0d 0a 0d 0a 20  nTrans>0 );.... 
1cb36 20 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20   while( *ppIter 
1cb37 29 7b 0d 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a  ){..    BtLock *
1cb38 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b  pLock = *ppIter;
1cb39 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ..    assert( (p
1cb3a 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42  Bt->btsFlags & B
1cb3b 54 53 5f 45 58 43 4c 55 53 49 56 45 29 3d 3d 30  TS_EXCLUSIVE)==0
1cb3c 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72   || pBt->pWriter
1cb3d 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20  ==pLock->pBtree 
1cb3e 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
1cb3f 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69  pLock->pBtree->i
1cb40 6e 54 72 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65  nTrans>=pLock->e
1cb41 4c 6f 63 6b 20 29 3b 0d 0a 20 20 20 20 69 66 28  Lock );..    if(
1cb42 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d   pLock->pBtree==
1cb43 70 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 70 49  p ){..      *ppI
1cb44 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65  ter = pLock->pNe
1cb45 78 74 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  xt;..      asser
1cb46 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65  t( pLock->iTable
1cb47 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 70  !=1 || pLock==&p
1cb48 2d 3e 6c 6f 63 6b 20 29 3b 0d 0a 20 20 20 20 20  ->lock );..     
1cb49 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62   if( pLock->iTab
1cb4a 6c 65 21 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20  le!=1 ){..      
1cb4b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1cb4c 4c 6f 63 6b 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  Lock);..      }.
1cb4d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
1cb4e 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f     ppIter = &pLo
1cb4f 63 6b 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  ck->pNext;..    
1cb50 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65  }..  }....  asse
1cb51 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61  rt( (pBt->btsFla
1cb52 67 73 20 26 20 42 54 53 5f 50 45 4e 44 49 4e 47  gs & BTS_PENDING
1cb53 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72  )==0 || pBt->pWr
1cb54 69 74 65 72 20 29 3b 0d 0a 20 20 69 66 28 20 70  iter );..  if( p
1cb55 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29  Bt->pWriter==p )
1cb56 7b 0d 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69  {..    pBt->pWri
1cb57 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 70 42  ter = 0;..    pB
1cb58 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e  t->btsFlags &= ~
1cb59 28 42 54 53 5f 45 58 43 4c 55 53 49 56 45 7c 42  (BTS_EXCLUSIVE|B
1cb5a 54 53 5f 50 45 4e 44 49 4e 47 29 3b 0d 0a 20 20  TS_PENDING);..  
1cb5b 7d 65 6c 73 65 20 69 66 28 20 70 42 74 2d 3e 6e  }else if( pBt->n
1cb5c 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29  Transaction==2 )
1cb5d 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66  {..    /* This f
1cb5e 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1cb5f 64 20 77 68 65 6e 20 42 74 72 65 65 20 70 20 69  d when Btree p i
1cb60 73 20 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73  s concluding its
1cb61 20 0d 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61   ..    ** transa
1cb62 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20  ction. If there 
1cb63 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73  currently exists
1cb64 20 61 20 77 72 69 74 65 72 2c 20 61 6e 64 20 70   a writer, and p
1cb65 20 69 73 20 6e 6f 74 0d 0a 20 20 20 20 2a 2a 20   is not..    ** 
1cb66 74 68 61 74 20 77 72 69 74 65 72 2c 20 74 68 65  that writer, the
1cb67 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1cb68 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f  locks held by co
1cb69 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 0d  nnections other.
1cb6a 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65  .    ** than the
1cb6b 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 65 20   writer must be 
1cb6c 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 6f  about to drop to
1cb6d 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63   zero. In this c
1cb6e 61 73 65 0d 0a 20 20 20 20 2a 2a 20 73 65 74 20  ase..    ** set 
1cb6f 74 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 20  the BTS_PENDING 
1cb70 66 6c 61 67 20 74 6f 20 30 2e 0d 0a 20 20 20 20  flag to 0...    
1cb71 2a 2a 0d 0a 20 20 20 20 2a 2a 20 49 66 20 74 68  **..    ** If th
1cb72 65 72 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ere is not curre
1cb73 6e 74 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74  ntly a writer, t
1cb74 68 65 6e 20 42 54 53 5f 50 45 4e 44 49 4e 47 20  hen BTS_PENDING 
1cb75 6d 75 73 74 0d 0a 20 20 20 20 2a 2a 20 62 65 20  must..    ** be 
1cb76 7a 65 72 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f  zero already. So
1cb77 20 74 68 69 73 20 6e 65 78 74 20 6c 69 6e 65 20   this next line 
1cb78 69 73 20 68 61 72 6d 6c 65 73 73 20 69 6e 20 74  is harmless in t
1cb79 68 61 74 20 63 61 73 65 2e 0d 0a 20 20 20 20 2a  hat case...    *
1cb7a 2f 0d 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46  /..    pBt->btsF
1cb7b 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 50 45 4e  lags &= ~BTS_PEN
1cb7c 44 49 4e 47 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  DING;..  }..}...
1cb7d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
1cb7e 63 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 61 6c  ction changes al
1cb7f 6c 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 68 65  l write-locks he
1cb80 6c 64 20 62 79 20 42 74 72 65 65 20 70 20 69 6e  ld by Btree p in
1cb81 74 6f 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 0d 0a  to read-locks...
1cb82 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
1cb83 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72  downgradeAllShar
1cb84 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1cb85 73 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20  s(Btree *p){..  
1cb86 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1cb87 70 2d 3e 70 42 74 3b 0d 0a 20 20 69 66 28 20 70  p->pBt;..  if( p
1cb88 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29  Bt->pWriter==p )
1cb89 7b 0d 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70  {..    BtLock *p
1cb8a 4c 6f 63 6b 3b 0d 0a 20 20 20 20 70 42 74 2d 3e  Lock;..    pBt->
1cb8b 70 57 72 69 74 65 72 20 3d 20 30 3b 0d 0a 20 20  pWriter = 0;..  
1cb8c 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20    pBt->btsFlags 
1cb8d 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49  &= ~(BTS_EXCLUSI
1cb8e 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b  VE|BTS_PENDING);
1cb8f 0d 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d  ..    for(pLock=
1cb90 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63  pBt->pLock; pLoc
1cb91 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e  k; pLock=pLock->
1cb92 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 61  pNext){..      a
1cb93 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c  ssert( pLock->eL
1cb94 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
1cb95 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  | pLock->pBtree=
1cb96 3d 70 20 29 3b 0d 0a 20 20 20 20 20 20 70 4c 6f  =p );..      pLo
1cb97 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44  ck->eLock = READ
1cb98 5f 4c 4f 43 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _LOCK;..    }.. 
1cb99 20 7d 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20   }..}....#endif 
1cb9a 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
1cb9b 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0d 0a  HARED_CACHE */..
1cb9c 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  ..static void re
1cb9d 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67  leasePage(MemPag
1cb9e 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46  e *pPage);  /* F
1cb9f 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
1cba0 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 2a 2a 2a   */..../*..*****
1cba1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1cba2 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20   used inside of 
1cba3 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a 2a  assert() only **
1cba4 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 56 65 72 69 66  **..**..** Verif
1cba5 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
1cba6 72 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65  r holds the mute
1cba7 78 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65  x on its BtShare
1cba8 64 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  d..*/..#ifdef SQ
1cba9 4c 49 54 45 5f 44 45 42 55 47 0d 0a 73 74 61 74  LITE_DEBUG..stat
1cbaa 69 63 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c  ic int cursorHol
1cbab 64 73 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72  dsMutex(BtCursor
1cbac 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20   *p){..  return 
1cbad 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1cbae 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
1cbaf 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  );..}..#endif...
1cbb0 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1cbb1 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0d  E_OMIT_INCRBLOB.
1cbb2 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61  ./*..** Invalida
1cbb3 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  te the overflow 
1cbb4 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20  page-list cache 
1cbb5 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 72 2c  for cursor pCur,
1cbb6 20 69 66 20 61 6e 79 2e 0d 0a 2a 2f 0d 0a 73 74   if any...*/..st
1cbb7 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69  atic void invali
1cbb8 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
1cbb9 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
1cbba 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 63 75  ){..  assert( cu
1cbbb 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1cbbc 43 75 72 29 20 29 3b 0d 0a 20 20 73 71 6c 69 74  Cur) );..  sqlit
1cbbd 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f  e3_free(pCur->aO
1cbbe 76 65 72 66 6c 6f 77 29 3b 0d 0a 20 20 70 43 75  verflow);..  pCu
1cbbf 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30  r->aOverflow = 0
1cbc0 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
1cbc1 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76  nvalidate the ov
1cbc2 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74  erflow page-list
1cbc3 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c 20 63   cache for all c
1cbc4 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0d 0a 2a  ursors opened..*
1cbc5 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20  * on the shared 
1cbc6 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
1cbc7 70 42 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  pBt...*/..static
1cbc8 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
1cbc9 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
1cbca 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
1cbcb 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  ..  BtCursor *p;
1cbcc 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1cbcd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1cbce 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  Bt->mutex) );.. 
1cbcf 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72   for(p=pBt->pCur
1cbd0 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
1cbd1 78 74 29 7b 0d 0a 20 20 20 20 69 6e 76 61 6c 69  xt){..    invali
1cbd2 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
1cbd3 65 28 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  e(p);..  }..}...
1cbd4 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
1cbd5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1cbd6 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67  before modifying
1cbd7 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1cbd8 20 61 20 74 61 62 6c 65 0d 0a 2a 2a 20 74 6f 20   a table..** to 
1cbd9 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69  invalidate any i
1cbda 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20  ncrblob cursors 
1cbdb 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e  that are open on
1cbdc 20 74 68 65 0d 0a 2a 2a 20 72 6f 77 20 6f 72 20   the..** row or 
1cbdd 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 73 20  one of the rows 
1cbde 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0d  being modified..
1cbdf 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 72 67 75 6d  .**..** If argum
1cbe0 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65  ent isClearTable
1cbe1 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
1cbe2 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e  he entire conten
1cbe3 74 73 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 74 61  ts of the..** ta
1cbe4 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f 20  ble is about to 
1cbe5 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74  be deleted. In t
1cbe6 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64  his case invalid
1cbe7 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62  ate all incrblob
1cbe8 0d 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65  ..** cursors ope
1cbe9 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74  n on any row wit
1cbea 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69  hin the table wi
1cbeb 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e  th root-page pgn
1cbec 6f 52 6f 6f 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f  oRoot...**..** O
1cbed 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 72 67  therwise, if arg
1cbee 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62  ument isClearTab
1cbef 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  le is false, the
1cbf0 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 0d 0a  n the row with..
1cbf1 2a 2a 20 72 6f 77 69 64 20 69 52 6f 77 20 69 73  ** rowid iRow is
1cbf2 20 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20   being replaced 
1cbf3 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74  or deleted. In t
1cbf4 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64  his case invalid
1cbf5 61 74 65 0d 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f  ate..** only tho
1cbf6 73 65 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73  se incrblob curs
1cbf7 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 61 74  ors open on that
1cbf8 20 73 70 65 63 69 66 69 63 20 72 6f 77 2e 0d 0a   specific row...
1cbf9 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
1cbfa 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c  invalidateIncrbl
1cbfb 6f 62 43 75 72 73 6f 72 73 28 0d 0a 20 20 42 74  obCursors(..  Bt
1cbfc 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
1cbfd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1cbfe 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68  abase file to ch
1cbff 65 63 6b 20 2a 2f 0d 0a 20 20 69 36 34 20 69 52  eck */..  i64 iR
1cc00 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ow,             
1cc01 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74    /* The rowid t
1cc02 68 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 61  hat might be cha
1cc03 6e 67 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20  nging */..  int 
1cc04 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20  isClearTable    
1cc05 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
1cc06 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e  ll rows are bein
1cc07 67 20 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 29 7b  g deleted */..){
1cc08 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  ..  BtCursor *p;
1cc09 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ..  BtShared *pB
1cc0a 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b  t = pBtree->pBt;
1cc0b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1cc0c 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
1cc0d 65 78 28 70 42 74 72 65 65 29 20 29 3b 0d 0a 20  ex(pBtree) );.. 
1cc0e 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72   for(p=pBt->pCur
1cc0f 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
1cc10 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d  xt){..    if( p-
1cc11 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
1cc12 65 20 26 26 20 28 69 73 43 6c 65 61 72 54 61 62  e && (isClearTab
1cc13 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b  le || p->info.nK
1cc14 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0d 0a 20 20  ey==iRow) ){..  
1cc15 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20      p->eState = 
1cc16 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0d  CURSOR_INVALID;.
1cc17 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a  .    }..  }..}..
1cc18 0d 0a 23 65 6c 73 65 0d 0a 20 20 2f 2a 20 53 74  ..#else..  /* St
1cc19 75 62 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65  ub functions whe
1cc1a 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73 20 6f 6d  n INCRBLOB is om
1cc1b 69 74 74 65 64 20 2a 2f 0d 0a 20 20 23 64 65 66  itted */..  #def
1cc1c 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76  ine invalidateOv
1cc1d 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0d 0a  erflowCache(x)..
1cc1e 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69    #define invali
1cc1f 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
1cc20 61 63 68 65 28 78 29 0d 0a 20 20 23 64 65 66 69  ache(x)..  #defi
1cc21 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  ne invalidateInc
1cc22 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79  rblobCursors(x,y
1cc23 2c 7a 29 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ,z)..#endif /* S
1cc24 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1cc25 4c 4f 42 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  LOB */..../*..**
1cc26 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66   Set bit pgno of
1cc27 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   the BtShared.pH
1cc28 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63  asContent bitvec
1cc29 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64  . This is called
1cc2a 20 0d 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 67   ..** when a pag
1cc2b 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c  e that previousl
1cc2c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 61  y contained data
1cc2d 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 2d   becomes a free-
1cc2e 6c 69 73 74 20 6c 65 61 66 20 0d 0a 2a 2a 20 70  list leaf ..** p
1cc2f 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  age...**..** The
1cc30 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f   BtShared.pHasCo
1cc31 6e 74 65 6e 74 20 62 69 74 76 65 63 20 65 78 69  ntent bitvec exi
1cc32 73 74 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  sts to work arou
1cc33 6e 64 20 61 6e 20 6f 62 73 63 75 72 65 0d 0a 2a  nd an obscure..*
1cc34 2a 20 62 75 67 20 63 61 75 73 65 64 20 62 79 20  * bug caused by 
1cc35 74 68 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 20  the interaction 
1cc36 6f 66 20 74 77 6f 20 75 73 65 66 75 6c 20 49 4f  of two useful IO
1cc37 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73   optimizations s
1cc38 75 72 72 6f 75 6e 64 69 6e 67 0d 0a 2a 2a 20 66  urrounding..** f
1cc39 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61  ree-list leaf pa
1cc3a 67 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31  ges:..**..**   1
1cc3b 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 61 20  ) When all data 
1cc3c 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  is deleted from 
1cc3d 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 70  a page and the p
1cc3e 61 67 65 20 62 65 63 6f 6d 65 73 0d 0a 2a 2a 20  age becomes..** 
1cc3f 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 74       a free-list
1cc40 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 20   leaf page, the 
1cc41 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 74  page is not writ
1cc42 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62  ten to the datab
1cc43 61 73 65 0d 0a 2a 2a 20 20 20 20 20 20 28 61 73  ase..**      (as
1cc44 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
1cc45 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f  pages contain no
1cc46 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61   meaningful data
1cc47 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0d 0a 2a 2a  ). Sometimes..**
1cc48 20 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67        such a pag
1cc49 65 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f  e is not even jo
1cc4a 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20  urnalled (as it 
1cc4b 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69  will not be modi
1cc4c 66 69 65 64 2c 0d 0a 2a 2a 20 20 20 20 20 20 77  fied,..**      w
1cc4d 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61  hy bother journa
1cc4e 6c 6c 69 6e 67 20 69 74 3f 29 2e 0d 0a 2a 2a 0d  lling it?)...**.
1cc4f 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20  .**   2) When a 
1cc50 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70  free-list leaf p
1cc51 61 67 65 20 69 73 20 72 65 75 73 65 64 2c 20 69  age is reused, i
1cc52 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  ts content is no
1cc53 74 20 72 65 61 64 0d 0a 2a 2a 20 20 20 20 20 20  t read..**      
1cc54 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1cc55 65 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f 20  e or written to 
1cc56 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1cc57 20 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 0d   (why should it.
1cc58 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20  .**      be, if 
1cc59 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c  it is not at all
1cc5a 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0d 0a   meaningful?)...
1cc5b 2a 2a 0d 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65  **..** By themse
1cc5c 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69  lves, these opti
1cc5d 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66  mizations work f
1cc5e 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20  ine and provide 
1cc5f 61 20 68 61 6e 64 79 0d 0a 2a 2a 20 70 65 72 66  a handy..** perf
1cc60 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f  ormance boost to
1cc61 20 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20   bulk delete or 
1cc62 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e  insert operation
1cc63 73 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0d 0a  s. However, if..
1cc64 2a 2a 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76  ** a page is mov
1cc65 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c  ed to the free-l
1cc66 69 73 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75  ist and then reu
1cc67 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73  sed within the s
1cc68 61 6d 65 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ame..** transact
1cc69 69 6f 6e 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63  ion, a problem c
1cc6a 6f 6d 65 73 20 75 70 2e 20 49 66 20 74 68 65 20  omes up. If the 
1cc6b 70 61 67 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72  page is not jour
1cc6c 6e 61 6c 6c 65 64 20 77 68 65 6e 0d 0a 2a 2a 20  nalled when..** 
1cc6d 69 74 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74  it is moved to t
1cc6e 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64  he free-list and
1cc6f 20 69 74 20 69 73 20 61 6c 73 6f 20 6e 6f 74 20   it is also not 
1cc70 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e 20  journalled when 
1cc71 69 74 0d 0a 2a 2a 20 69 73 20 65 78 74 72 61 63  it..** is extrac
1cc72 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65  ted from the fre
1cc73 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 75 73 65  e-list and reuse
1cc74 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67  d, then the orig
1cc75 69 6e 61 6c 20 64 61 74 61 0d 0a 2a 2a 20 6d 61  inal data..** ma
1cc76 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 74 68  y be lost. In th
1cc77 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c  e event of a rol
1cc78 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 6e 6f  lback, it may no
1cc79 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0d 0a 2a  t be possible..*
1cc7a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65  * to restore the
1cc7b 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
1cc7c 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 67   original config
1cc7d 75 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a  uration...**..**
1cc7e 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73   The solution is
1cc7f 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   the BtShared.pH
1cc80 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63  asContent bitvec
1cc81 2e 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67  . Whenever a pag
1cc82 65 20 69 73 20 0d 0a 2a 2a 20 6d 6f 76 65 64 20  e is ..** moved 
1cc83 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65  to become a free
1cc84 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c  -list leaf page,
1cc85 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1cc86 6e 67 20 62 69 74 20 69 73 0d 0a 2a 2a 20 73 65  ng bit is..** se
1cc87 74 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 2e  t in the bitvec.
1cc88 20 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 66   Whenever a leaf
1cc89 20 70 61 67 65 20 69 73 20 65 78 74 72 61 63 74   page is extract
1cc8a 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65  ed from the free
1cc8b 2d 6c 69 73 74 2c 0d 0a 2a 2a 20 6f 70 74 69 6d  -list,..** optim
1cc8c 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20  ization 2 above 
1cc8d 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68  is omitted if th
1cc8e 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1cc8f 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 0d 0a  bit is already..
1cc90 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 72  ** set in BtShar
1cc91 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20  ed.pHasContent. 
1cc92 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
1cc93 74 68 65 20 62 69 74 76 65 63 20 61 72 65 20 63  the bitvec are c
1cc94 6c 65 61 72 65 64 0d 0a 2a 2a 20 61 74 20 74 68  leared..** at th
1cc95 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74  e end of every t
1cc96 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d  ransaction...*/.
1cc97 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
1cc98 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42  eSetHasContent(B
1cc99 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
1cc9a 6e 6f 20 70 67 6e 6f 29 7b 0d 0a 20 20 69 6e 74  no pgno){..  int
1cc9b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1cc9c 0d 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48  ..  if( !pBt->pH
1cc9d 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0d 0a 20 20  asContent ){..  
1cc9e 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d    assert( pgno<=
1cc9f 70 42 74 2d 3e 6e 50 61 67 65 20 29 3b 0d 0a 20  pBt->nPage );.. 
1cca0 20 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74     pBt->pHasCont
1cca1 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74  ent = sqlite3Bit
1cca2 76 65 63 43 72 65 61 74 65 28 70 42 74 2d 3e 6e  vecCreate(pBt->n
1cca3 50 61 67 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  Page);..    if( 
1cca4 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e  !pBt->pHasConten
1cca5 74 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  t ){..      rc =
1cca6 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
1cca7 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
1cca8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1cca9 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33  && pgno<=sqlite3
1ccaa 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e  BitvecSize(pBt->
1ccab 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0d  pHasContent) ){.
1ccac 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1ccad 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e  3BitvecSet(pBt->
1ccae 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e  pHasContent, pgn
1ccaf 6f 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  o);..  }..  retu
1ccb0 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
1ccb1 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 42 74  .** Query the Bt
1ccb2 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65  Shared.pHasConte
1ccb3 6e 74 20 76 65 63 74 6f 72 2e 0d 0a 2a 2a 0d 0a  nt vector...**..
1ccb4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1ccb5 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
1ccb6 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66  a free-list leaf
1ccb7 20 70 61 67 65 20 69 73 20 72 65 6d 6f 76 65 64   page is removed
1ccb8 20 66 72 6f 6d 20 74 68 65 0d 0a 2a 2a 20 66 72   from the..** fr
1ccb9 65 65 2d 6c 69 73 74 20 66 6f 72 20 72 65 75 73  ee-list for reus
1ccba 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 66 61  e. It returns fa
1ccbb 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 61 66  lse if it is saf
1ccbc 65 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  e to retrieve th
1ccbd 65 0d 0a 2a 2a 20 70 61 67 65 20 66 72 6f 6d 20  e..** page from 
1ccbe 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20  the pager layer 
1ccbf 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e  with the 'no-con
1ccc0 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20  tent' flag set. 
1ccc1 54 72 75 65 20 6f 74 68 65 72 77 69 73 65 2e 0d  True otherwise..
1ccc2 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
1ccc3 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65  btreeGetHasConte
1ccc4 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  nt(BtShared *pBt
1ccc5 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0d 0a 20  , Pgno pgno){.. 
1ccc6 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74   Bitvec *p = pBt
1ccc7 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0d 0a  ->pHasContent;..
1ccc8 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28    return (p && (
1ccc9 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76  pgno>sqlite3Bitv
1ccca 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c  ecSize(p) || sql
1cccb 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
1cccc 2c 20 70 67 6e 6f 29 29 29 3b 0d 0a 7d 0d 0a 0d  , pgno)));..}...
1cccd 0a 2f 2a 0d 0a 2a 2a 20 43 6c 65 61 72 20 28 64  ./*..** Clear (d
1ccce 65 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68  estroy) the BtSh
1cccf 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74  ared.pHasContent
1ccd0 20 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68   bitvec. This sh
1ccd1 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20 69 6e 76 6f  ould be..** invo
1ccd2 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c  ked at the concl
1ccd3 75 73 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72  usion of each wr
1ccd4 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ite-transaction.
1ccd5 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
1ccd6 64 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43  d btreeClearHasC
1ccd7 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20  ontent(BtShared 
1ccd8 2a 70 42 74 29 7b 0d 0a 20 20 73 71 6c 69 74 65  *pBt){..  sqlite
1ccd9 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
1ccda 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29  Bt->pHasContent)
1ccdb 3b 0d 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 6f  ;..  pBt->pHasCo
1ccdc 6e 74 65 6e 74 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d  ntent = 0;..}...
1ccdd 0a 2f 2a 0d 0a 2a 2a 20 53 61 76 65 20 74 68 65  ./*..** Save the
1ccde 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
1ccdf 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20  position in the 
1cce0 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73  variables BtCurs
1cce1 6f 72 2e 6e 4b 65 79 20 0d 0a 2a 2a 20 61 6e 64  or.nKey ..** and
1cce2 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20   BtCursor.pKey. 
1cce3 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 61  The cursor's sta
1cce4 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 52  te is set to CUR
1cce5 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e  SOR_REQUIRESEEK.
1cce6 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 61 6c  ..**..** The cal
1cce7 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20  ler must ensure 
1cce8 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
1cce9 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53  is valid (has eS
1ccea 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1cceb 49 44 29 0d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f  ID)..** prior to
1ccec 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
1cced 75 74 69 6e 65 2e 20 20 0d 0a 2a 2f 0d 0a 73 74  utine.  ..*/..st
1ccee 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72  atic int saveCur
1ccef 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75  sorPosition(BtCu
1ccf0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d 0a 20 20  rsor *pCur){..  
1ccf1 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 61 73 73  int rc;....  ass
1ccf2 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49  ert( CURSOR_VALI
1ccf3 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
1ccf4 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 30 3d  );..  assert( 0=
1ccf5 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0d 0a  =pCur->pKey );..
1ccf6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1ccf7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1ccf8 20 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71   );....  rc = sq
1ccf9 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
1ccfa 65 28 70 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e  e(pCur, &pCur->n
1ccfb 4b 65 79 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  Key);..  assert(
1ccfc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ccfd 3b 20 20 2f 2a 20 4b 65 79 53 69 7a 65 28 29 20  ;  /* KeySize() 
1ccfe 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0d 0a  cannot fail */..
1ccff 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
1cd00 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c  s an intKey tabl
1cd01 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 6f 76  e, then the abov
1cd02 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4b  e call to BtreeK
1cd03 65 79 53 69 7a 65 28 29 0d 0a 20 20 2a 2a 20 73  eySize()..  ** s
1cd04 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 65  tores the intege
1cd05 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e 6e  r key in pCur->n
1cd06 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73  Key. In this cas
1cd07 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 0d  e this value is.
1cd08 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
1cd09 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65  s required. Othe
1cd0a 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69  rwise, if pCur i
1cd0b 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e  s not open on an
1cd0c 20 69 6e 74 4b 65 79 0d 0a 20 20 2a 2a 20 74 61   intKey..  ** ta
1cd0d 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63  ble, then malloc
1cd0e 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73   space for and s
1cd0f 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e  tore the pCur->n
1cd10 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79  Key bytes of key
1cd11 20 0d 0a 20 20 2a 2a 20 64 61 74 61 2e 0d 0a 20   ..  ** data... 
1cd12 20 2a 2f 0d 0a 20 20 69 66 28 20 30 3d 3d 70 43   */..  if( 0==pC
1cd13 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
1cd14 6e 74 4b 65 79 20 29 7b 0d 0a 20 20 20 20 76 6f  ntKey ){..    vo
1cd15 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74  id *pKey = sqlit
1cd16 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70  e3Malloc( (int)p
1cd17 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0d 0a 20 20  Cur->nKey );..  
1cd18 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0d 0a 20    if( pKey ){.. 
1cd19 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1cd1a 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20  3BtreeKey(pCur, 
1cd1b 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b  0, (int)pCur->nK
1cd1c 65 79 2c 20 70 4b 65 79 29 3b 0d 0a 20 20 20 20  ey, pKey);..    
1cd1d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1cd1e 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
1cd1f 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65  pCur->pKey = pKe
1cd20 79 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  y;..      }else{
1cd21 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
1cd22 33 5f 66 72 65 65 28 70 4b 65 79 29 3b 0d 0a 20  3_free(pKey);.. 
1cd23 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73       }..    }els
1cd24 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  e{..      rc = S
1cd25 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
1cd26 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65    }..  }..  asse
1cd27 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67  rt( !pCur->apPag
1cd28 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20  e[0]->intKey || 
1cd29 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0d 0a  !pCur->pKey );..
1cd2a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1cd2b 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 6e  TE_OK ){..    in
1cd2c 74 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  t i;..    for(i=
1cd2d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67  0; i<=pCur->iPag
1cd2e 65 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  e; i++){..      
1cd2f 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
1cd30 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0d 0a 20  ->apPage[i]);.. 
1cd31 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67       pCur->apPag
1cd32 65 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  e[i] = 0;..    }
1cd33 0d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  ..    pCur->iPag
1cd34 65 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 70 43 75  e = -1;..    pCu
1cd35 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
1cd36 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0d  OR_REQUIRESEEK;.
1cd37 0a 20 20 7d 0d 0a 0d 0a 20 20 69 6e 76 61 6c 69  .  }....  invali
1cd38 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
1cd39 65 28 70 43 75 72 29 3b 0d 0a 20 20 72 65 74 75  e(pCur);..  retu
1cd3a 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
1cd3b 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73  .** Save the pos
1cd3c 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75  itions of all cu
1cd3d 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 45  rsors (except pE
1cd3e 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65 20  xcept) that are 
1cd3f 6f 70 65 6e 20 6f 6e 0d 0a 2a 2a 20 74 68 65 20  open on..** the 
1cd40 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74  table  with root
1cd41 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75  -page iRoot. Usu
1cd42 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
1cd43 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65  lled just before
1cd44 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 70 45 78 63   cursor..** pExc
1cd45 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d  ept is used to m
1cd46 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20  odify the table 
1cd47 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f  (BtreeDelete() o
1cd48 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29  r BtreeInsert())
1cd49 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
1cd4a 74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73  t saveAllCursors
1cd4b 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
1cd4c 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75  Pgno iRoot, BtCu
1cd4d 72 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0d  rsor *pExcept){.
1cd4e 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0d  .  BtCursor *p;.
1cd4f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1cd50 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1cd51 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  t->mutex) );..  
1cd52 61 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d  assert( pExcept=
1cd53 3d 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70  =0 || pExcept->p
1cd54 42 74 3d 3d 70 42 74 20 29 3b 0d 0a 20 20 66 6f  Bt==pBt );..  fo
1cd55 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72  r(p=pBt->pCursor
1cd56 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
1cd57 7b 0d 0a 20 20 20 20 69 66 28 20 70 21 3d 70 45  {..    if( p!=pE
1cd58 78 63 65 70 74 20 26 26 20 28 30 3d 3d 69 52 6f  xcept && (0==iRo
1cd59 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f  ot || p->pgnoRoo
1cd5a 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20 0d 0a 20  t==iRoot) && .. 
1cd5b 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65         p->eState
1cd5c 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
1cd5d 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20  {..      int rc 
1cd5e 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  = saveCursorPosi
1cd5f 74 69 6f 6e 28 70 29 3b 0d 0a 20 20 20 20 20 20  tion(p);..      
1cd60 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
1cd61 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65  c ){..        re
1cd62 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20  turn rc;..      
1cd63 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  }..    }..  }.. 
1cd64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1cd65 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
1cd66 43 6c 65 61 72 20 74 68 65 20 63 75 72 72 65 6e  Clear the curren
1cd67 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f  t cursor positio
1cd68 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  n...*/..SQLITE_P
1cd69 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1cd6a 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
1cd6b 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
1cd6c 75 72 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  ur){..  assert( 
1cd6d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1cd6e 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 73 71 6c  (pCur) );..  sql
1cd6f 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e  ite3_free(pCur->
1cd70 70 4b 65 79 29 3b 0d 0a 20 20 70 43 75 72 2d 3e  pKey);..  pCur->
1cd71 70 4b 65 79 20 3d 20 30 3b 0d 0a 20 20 70 43 75  pKey = 0;..  pCu
1cd72 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
1cd73 4f 52 5f 49 4e 56 41 4c 49 44 3b 0d 0a 7d 0d 0a  OR_INVALID;..}..
1cd74 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 20 74 68 69 73  ../*..** In this
1cd75 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65   version of Btre
1cd76 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73  eMoveto, pKey is
1cd77 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20   a packed index 
1cd78 72 65 63 6f 72 64 0d 0a 2a 2a 20 73 75 63 68 20  record..** such 
1cd79 61 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  as is generated 
1cd7a 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65  by the OP_MakeRe
1cd7b 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e  cord opcode.  Un
1cd7c 70 61 63 6b 20 74 68 65 0d 0a 2a 2a 20 72 65 63  pack the..** rec
1cd7d 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c  ord and then cal
1cd7e 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  l BtreeMovetoUnp
1cd7f 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68  acked() to do th
1cd80 65 20 77 6f 72 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61  e work...*/..sta
1cd81 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76  tic int btreeMov
1cd82 65 74 6f 28 0d 0a 20 20 42 74 43 75 72 73 6f 72  eto(..  BtCursor
1cd83 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43   *pCur,     /* C
1cd84 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68  ursor open on th
1cd85 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65  e btree to be se
1cd86 61 72 63 68 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e  arched */..  con
1cd87 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20  st void *pKey,  
1cd88 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69   /* Packed key i
1cd89 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61  f the btree is a
1cd8a 6e 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 69 36  n index */..  i6
1cd8b 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20  4 nKey,         
1cd8c 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79    /* Integer key
1cd8d 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69   for tables.  Si
1cd8e 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69  ze of pKey for i
1cd8f 6e 64 69 63 65 73 20 2a 2f 0d 0a 20 20 69 6e 74  ndices */..  int
1cd90 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20   bias,          
1cd91 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20   /* Bias search 
1cd92 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20  to the high end 
1cd93 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 52 65 73 20  */..  int *pRes 
1cd94 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
1cd95 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74  te search result
1cd96 73 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  s here */..){.. 
1cd97 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
1cd98 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1cd99 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0d 0a 20  tatus code */.. 
1cd9a 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1cd9b 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55  *pIdxKey;   /* U
1cd9c 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1cd9d 79 20 2a 2f 0d 0a 20 20 63 68 61 72 20 61 53 70  y */..  char aSp
1cd9e 61 63 65 5b 31 35 30 5d 3b 20 20 20 20 20 20 20  ace[150];       
1cd9f 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65     /* Temp space
1cda0 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74   for pIdxKey - t
1cda1 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63  o avoid a malloc
1cda2 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70 46 72   */..  char *pFr
1cda3 65 65 20 3d 20 30 3b 0d 0a 0d 0a 20 20 69 66 28  ee = 0;....  if(
1cda4 20 70 4b 65 79 20 29 7b 0d 0a 20 20 20 20 61 73   pKey ){..    as
1cda5 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
1cda6 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0d 0a 20  )(int)nKey );.. 
1cda7 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c     pIdxKey = sql
1cda8 69 74 65 33 56 64 62 65 41 6c 6c 6f 63 55 6e 70  ite3VdbeAllocUnp
1cda9 61 63 6b 65 64 52 65 63 6f 72 64 28 0d 0a 20 20  ackedRecord(..  
1cdaa 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79        pCur->pKey
1cdab 49 6e 66 6f 2c 20 61 53 70 61 63 65 2c 20 73 69  Info, aSpace, si
1cdac 7a 65 6f 66 28 61 53 70 61 63 65 29 2c 20 26 70  zeof(aSpace), &p
1cdad 46 72 65 65 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  Free..    );..  
1cdae 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30    if( pIdxKey==0
1cdaf 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
1cdb0 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 73 71 6c  _NOMEM;..    sql
1cdb1 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
1cdb2 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49  pack(pCur->pKeyI
1cdb3 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20  nfo, (int)nKey, 
1cdb4 70 4b 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0d  pKey, pIdxKey);.
1cdb5 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
1cdb6 49 64 78 4b 65 79 20 3d 20 30 3b 0d 0a 20 20 7d  IdxKey = 0;..  }
1cdb7 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
1cdb8 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1cdb9 6b 65 64 28 70 43 75 72 2c 20 70 49 64 78 4b 65  ked(pCur, pIdxKe
1cdba 79 2c 20 6e 4b 65 79 2c 20 62 69 61 73 2c 20 70  y, nKey, bias, p
1cdbb 52 65 73 29 3b 0d 0a 20 20 69 66 28 20 70 46 72  Res);..  if( pFr
1cdbc 65 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ee ){..    sqlit
1cdbd 65 33 44 62 46 72 65 65 28 70 43 75 72 2d 3e 70  e3DbFree(pCur->p
1cdbe 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 46 72  KeyInfo->db, pFr
1cdbf 65 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ee);..  }..  ret
1cdc0 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1cdc1 0d 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65  ..** Restore the
1cdc2 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70   cursor to the p
1cdc3 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69  osition it was i
1cdc4 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74  n (or as close t
1cdc5 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0d 0a  o as possible)..
1cdc6 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73  ** when saveCurs
1cdc7 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73  orPosition() was
1cdc8 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68   called. Note th
1cdc9 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c  at this call del
1cdca 65 74 65 73 20 74 68 65 20 0d 0a 2a 2a 20 73 61  etes the ..** sa
1cdcb 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66  ved position inf
1cdcc 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65  o stored by save
1cdcd 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
1cdce 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62  , so there can b
1cdcf 65 0d 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e  e..** at most on
1cdd0 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74  e effective rest
1cdd1 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
1cdd2 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65  n() call after e
1cdd3 61 63 68 20 0d 0a 2a 2a 20 73 61 76 65 43 75 72  ach ..** saveCur
1cdd4 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0d 0a  sorPosition()...
1cdd5 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 62  */..static int b
1cdd6 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f  treeRestoreCurso
1cdd7 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73  rPosition(BtCurs
1cdd8 6f 72 20 2a 70 43 75 72 29 7b 0d 0a 20 20 69 6e  or *pCur){..  in
1cdd9 74 20 72 63 3b 0d 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1cdda 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1cddb 78 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 61 73  x(pCur) );..  as
1cddc 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
1cddd 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
1cdde 52 45 53 45 45 4b 20 29 3b 0d 0a 20 20 69 66 28  RESEEK );..  if(
1cddf 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
1cde0 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0d 0a  URSOR_FAULT ){..
1cde1 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d      return pCur-
1cde2 3e 73 6b 69 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d  >skipNext;..  }.
1cde3 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
1cde4 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1cde5 3b 0d 0a 20 20 72 63 20 3d 20 62 74 72 65 65 4d  ;..  rc = btreeM
1cde6 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72  oveto(pCur, pCur
1cde7 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b  ->pKey, pCur->nK
1cde8 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b  ey, 0, &pCur->sk
1cde9 69 70 4e 65 78 74 29 3b 0d 0a 20 20 69 66 28 20  ipNext);..  if( 
1cdea 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1cdeb 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
1cdec 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0d  ee(pCur->pKey);.
1cded 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20  .    pCur->pKey 
1cdee 3d 20 30 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  = 0;..    assert
1cdef 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
1cdf0 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20  CURSOR_VALID || 
1cdf1 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1cdf2 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0d  RSOR_INVALID );.
1cdf3 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
1cdf4 63 3b 0d 0a 7d 0d 0a 0d 0a 23 64 65 66 69 6e 65  c;..}....#define
1cdf5 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1cdf6 73 69 74 69 6f 6e 28 70 29 20 5c 0d 0a 20 20 28  sition(p) \..  (
1cdf7 70 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f  p->eState>=CURSO
1cdf8 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 3f 20  R_REQUIRESEEK ? 
1cdf9 5c 0d 0a 20 20 20 20 20 20 20 20 20 62 74 72 65  \..         btre
1cdfa 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f  eRestoreCursorPo
1cdfb 73 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0d 0a 20  sition(p) : \.. 
1cdfc 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
1cdfd 4b 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 74  K)..../*..** Det
1cdfe 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
1cdff 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68  r not a cursor h
1ce00 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68  as moved from th
1ce01 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0d 0a 2a  e position it..*
1ce02 2a 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 65  * was last place
1ce03 64 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 63  d at.  Cursors c
1ce04 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 65  an move when the
1ce05 20 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 6f   row they are po
1ce06 69 6e 74 69 6e 67 0d 0a 2a 2a 20 61 74 20 69 73  inting..** at is
1ce07 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f   deleted out fro
1ce08 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0d 0a 2a  m under them...*
1ce09 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
1ce0a 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  ne returns an er
1ce0b 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65  ror code if some
1ce0c 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
1ce0d 2e 20 20 54 68 65 0d 0a 2a 2a 20 69 6e 74 65 67  .  The..** integ
1ce0e 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73  er *pHasMoved is
1ce0f 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74   set to one if t
1ce10 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f  he cursor has mo
1ce11 76 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74  ved and 0 if not
1ce12 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
1ce13 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ce14 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
1ce15 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70  oved(BtCursor *p
1ce16 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f  Cur, int *pHasMo
1ce17 76 65 64 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  ved){..  int rc;
1ce18 0d 0a 0d 0a 20 20 72 63 20 3d 20 72 65 73 74 6f  ....  rc = resto
1ce19 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
1ce1a 28 70 43 75 72 29 3b 0d 0a 20 20 69 66 28 20 72  (pCur);..  if( r
1ce1b 63 20 29 7b 0d 0a 20 20 20 20 2a 70 48 61 73 4d  c ){..    *pHasM
1ce1c 6f 76 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 72  oved = 1;..    r
1ce1d 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a  eturn rc;..  }..
1ce1e 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
1ce1f 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te!=CURSOR_VALID
1ce20 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65   || pCur->skipNe
1ce21 78 74 21 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70  xt!=0 ){..    *p
1ce22 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0d 0a 20  HasMoved = 1;.. 
1ce23 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2a 70 48   }else{..    *pH
1ce24 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0d 0a 20 20  asMoved = 0;..  
1ce25 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
1ce26 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66  TE_OK;..}....#if
1ce27 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ce28 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 2f 2a 0d  _AUTOVACUUM../*.
1ce29 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65  .** Given a page
1ce2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67   number of a reg
1ce2b 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 70 61  ular database pa
1ce2c 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70  ge, return the p
1ce2d 61 67 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 66  age..** number f
1ce2e 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  or the pointer-m
1ce2f 61 70 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  ap page that con
1ce30 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20  tains the entry 
1ce31 66 6f 72 20 74 68 65 0d 0a 2a 2a 20 69 6e 70 75  for the..** inpu
1ce32 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0d 0a  t page number...
1ce33 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20  **..** Return 0 
1ce34 28 6e 6f 74 20 61 20 76 61 6c 69 64 20 70 61 67  (not a valid pag
1ce35 65 29 20 66 6f 72 20 70 67 6e 6f 3d 3d 31 20 73  e) for pgno==1 s
1ce36 69 6e 63 65 20 74 68 65 72 65 20 69 73 0d 0a 2a  ince there is..*
1ce37 2a 20 6e 6f 20 70 6f 69 6e 74 65 72 20 6d 61 70  * no pointer map
1ce38 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1ce39 20 70 61 67 65 20 31 2e 20 20 54 68 65 20 69 6e   page 1.  The in
1ce3a 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 6c 6f  tegrity_check lo
1ce3b 67 69 63 0d 0a 2a 2a 20 72 65 71 75 69 72 65 73  gic..** requires
1ce3c 20 74 68 61 74 20 70 74 72 6d 61 70 50 61 67 65   that ptrmapPage
1ce3d 6e 6f 28 2a 2c 31 29 21 3d 31 2e 0d 0a 2a 2f 0d  no(*,1)!=1...*/.
1ce3e 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72  .static Pgno ptr
1ce3f 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72  mapPageno(BtShar
1ce40 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67  ed *pBt, Pgno pg
1ce41 6e 6f 29 7b 0d 0a 20 20 69 6e 74 20 6e 50 61 67  no){..  int nPag
1ce42 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0d 0a 20  esPerMapPage;.. 
1ce43 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72   Pgno iPtrMap, r
1ce44 65 74 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  et;..  assert( s
1ce45 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1ce46 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1ce47 0d 0a 20 20 69 66 28 20 70 67 6e 6f 3c 32 20 29  ..  if( pgno<2 )
1ce48 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 6e 50   return 0;..  nP
1ce49 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d  agesPerMapPage =
1ce4a 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a   (pBt->usableSiz
1ce4b 65 2f 35 29 2b 31 3b 0d 0a 20 20 69 50 74 72 4d  e/5)+1;..  iPtrM
1ce4c 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50  ap = (pgno-2)/nP
1ce4d 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0d  agesPerMapPage;.
1ce4e 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61  .  ret = (iPtrMa
1ce4f 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61  p*nPagesPerMapPa
1ce50 67 65 29 20 2b 20 32 3b 20 0d 0a 20 20 69 66 28  ge) + 2; ..  if(
1ce51 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59   ret==PENDING_BY
1ce52 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0d  TE_PAGE(pBt) ){.
1ce53 0a 20 20 20 20 72 65 74 2b 2b 3b 0d 0a 20 20 7d  .    ret++;..  }
1ce54 0d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0d  ..  return ret;.
1ce55 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69  .}..../*..** Wri
1ce56 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f  te an entry into
1ce57 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1ce58 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
1ce59 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74  outine updates t
1ce5a 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
1ce5b 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75  ntry for page nu
1ce5c 6d 62 65 72 20 27 6b 65 79 27 0d 0a 2a 2a 20 73  mber 'key'..** s
1ce5d 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74  o that it maps t
1ce5e 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61  o type 'eType' a
1ce5f 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  nd parent page n
1ce60 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0d 0a 2a  umber 'pgno'...*
1ce61 2a 0d 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 73  *..** If *pRC is
1ce62 20 69 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a   initially non-z
1ce63 65 72 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f  ero (non-SQLITE_
1ce64 4f 4b 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f  OK) then this ro
1ce65 75 74 69 6e 65 20 69 73 0d 0a 2a 2a 20 61 20 6e  utine is..** a n
1ce66 6f 2d 6f 70 2e 20 20 49 66 20 61 6e 20 65 72 72  o-op.  If an err
1ce67 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 61  or occurs, the a
1ce68 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
1ce69 20 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e   code is written
1ce6a 0d 0a 2a 2a 20 69 6e 74 6f 20 2a 70 52 43 2e 0d  ..** into *pRC..
1ce6b 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
1ce6c 20 70 74 72 6d 61 70 50 75 74 28 42 74 53 68 61   ptrmapPut(BtSha
1ce6d 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b  red *pBt, Pgno k
1ce6e 65 79 2c 20 75 38 20 65 54 79 70 65 2c 20 50 67  ey, u8 eType, Pg
1ce6f 6e 6f 20 70 61 72 65 6e 74 2c 20 69 6e 74 20 2a  no parent, int *
1ce70 70 52 43 29 7b 0d 0a 20 20 44 62 50 61 67 65 20  pRC){..  DbPage 
1ce71 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 54 68  *pDbPage;  /* Th
1ce72 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
1ce73 67 65 20 2a 2f 0d 0a 20 20 75 38 20 2a 70 50 74  ge */..  u8 *pPt
1ce74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 68  rmap;      /* Th
1ce75 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 61  e pointer map da
1ce76 74 61 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 69 50  ta */..  Pgno iP
1ce77 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68  trmap;     /* Th
1ce78 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
1ce79 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20  ge number */..  
1ce7a 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20  int offset;     
1ce7b 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70    /* Offset in p
1ce7c 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
1ce7d 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  */..  int rc;   
1ce7e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1ce7f 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66  n code from subf
1ce80 75 6e 63 74 69 6f 6e 73 20 2a 2f 0d 0a 0d 0a 20  unctions */.... 
1ce81 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75   if( *pRC ) retu
1ce82 72 6e 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  rn;....  assert(
1ce83 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1ce84 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
1ce85 29 3b 0d 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73  );..  /* The mas
1ce86 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65  ter-journal page
1ce87 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76   number must nev
1ce88 65 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20  er be used as a 
1ce89 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
1ce8a 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 30   */..  assert( 0
1ce8b 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  ==PTRMAP_ISPAGE(
1ce8c 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54  pBt, PENDING_BYT
1ce8d 45 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b 0d  E_PAGE(pBt)) );.
1ce8e 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  ...  assert( pBt
1ce8f 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0d  ->autoVacuum );.
1ce90 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b  .  if( key==0 ){
1ce91 0d 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c  ..    *pRC = SQL
1ce92 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1ce93 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  ;..    return;..
1ce94 20 20 7d 0d 0a 20 20 69 50 74 72 6d 61 70 20 3d    }..  iPtrmap =
1ce95 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70   PTRMAP_PAGENO(p
1ce96 42 74 2c 20 6b 65 79 29 3b 0d 0a 20 20 72 63 20  Bt, key);..  rc 
1ce97 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1ce98 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
1ce99 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
1ce9a 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
1ce9b 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1ce9c 2a 70 52 43 20 3d 20 72 63 3b 0d 0a 20 20 20 20  *pRC = rc;..    
1ce9d 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20  return;..  }..  
1ce9e 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f  offset = PTRMAP_
1ce9f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61  PTROFFSET(iPtrma
1cea0 70 2c 20 6b 65 79 29 3b 0d 0a 20 20 69 66 28 20  p, key);..  if( 
1cea1 6f 66 66 73 65 74 3c 30 20 29 7b 0d 0a 20 20 20  offset<0 ){..   
1cea2 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43   *pRC = SQLITE_C
1cea3 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1cea4 20 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78    goto ptrmap_ex
1cea5 69 74 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65  it;..  }..  asse
1cea6 72 74 28 20 6f 66 66 73 65 74 20 3c 3d 20 28 69  rt( offset <= (i
1cea7 6e 74 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  nt)pBt->usableSi
1cea8 7a 65 2d 35 20 29 3b 0d 0a 20 20 70 50 74 72 6d  ze-5 );..  pPtrm
1cea9 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ap = (u8 *)sqlit
1ceaa 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
1ceab 44 62 50 61 67 65 29 3b 0d 0a 0d 0a 20 20 69 66  DbPage);....  if
1ceac 28 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70  ( eType!=pPtrmap
1cead 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34  [offset] || get4
1ceae 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
1ceaf 66 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74  fset+1])!=parent
1ceb0 20 29 7b 0d 0a 20 20 20 20 54 52 41 43 45 28 28   ){..    TRACE((
1ceb1 22 50 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20  "PTRMAP_UPDATE: 
1ceb2 25 64 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20  %d->(%d,%d)\n", 
1ceb3 6b 65 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65  key, eType, pare
1ceb4 6e 74 29 29 3b 0d 0a 20 20 20 20 2a 70 52 43 3d  nt));..    *pRC=
1ceb5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1ceb6 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29  erWrite(pDbPage)
1ceb7 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
1ceb8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1ceb9 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65     pPtrmap[offse
1ceba 74 5d 20 3d 20 65 54 79 70 65 3b 0d 0a 20 20 20  t] = eType;..   
1cebb 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74     put4byte(&pPt
1cebc 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20  rmap[offset+1], 
1cebd 70 61 72 65 6e 74 29 3b 0d 0a 20 20 20 20 7d 0d  parent);..    }.
1cebe 0a 20 20 7d 0d 0a 0d 0a 70 74 72 6d 61 70 5f 65  .  }....ptrmap_e
1cebf 78 69 74 3a 0d 0a 20 20 73 71 6c 69 74 65 33 50  xit:..  sqlite3P
1cec0 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67  agerUnref(pDbPag
1cec1 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  e);..}..../*..**
1cec2 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 66   Read an entry f
1cec3 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 20  rom the pointer 
1cec4 6d 61 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  map...**..** Thi
1cec5 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65  s routine retrie
1cec6 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ves the pointer 
1cec7 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61  map entry for pa
1cec8 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e  ge 'key', writin
1cec9 67 0d 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61  g..** the type a
1ceca 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  nd parent page n
1cecb 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65  umber to *pEType
1cecc 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70   and *pPgno resp
1cecd 65 63 74 69 76 65 6c 79 2e 0d 0a 2a 2a 20 41 6e  ectively...** An
1cece 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
1cecf 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74  eturned if somet
1ced0 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c  hing goes wrong,
1ced1 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54   otherwise SQLIT
1ced2 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  E_OK...*/..stati
1ced3 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28  c int ptrmapGet(
1ced4 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
1ced5 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54  gno key, u8 *pET
1ced6 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f  ype, Pgno *pPgno
1ced7 29 7b 0d 0a 20 20 44 62 50 61 67 65 20 2a 70 44  ){..  DbPage *pD
1ced8 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20  bPage;   /* The 
1ced9 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
1ceda 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 74 72 6d   */..  int iPtrm
1cedb 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  ap;       /* Poi
1cedc 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e  nter map page in
1cedd 64 65 78 20 2a 2f 0d 0a 20 20 75 38 20 2a 70 50  dex */..  u8 *pP
1cede 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20  trmap;       /* 
1cedf 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  Pointer map page
1cee0 20 64 61 74 61 20 2a 2f 0d 0a 20 20 69 6e 74 20   data */..  int 
1cee1 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f  offset;        /
1cee2 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72  * Offset of entr
1cee3 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70  y in pointer map
1cee4 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a   */..  int rc;..
1cee5 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1cee6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1cee7 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 0d  Bt->mutex) );...
1cee8 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52  .  iPtrmap = PTR
1cee9 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
1ceea 6b 65 79 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71  key);..  rc = sq
1ceeb 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
1ceec 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d  t->pPager, iPtrm
1ceed 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0d 0a  ap, &pDbPage);..
1ceee 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0d 0a    if( rc!=0 ){..
1ceef 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
1cef0 20 20 7d 0d 0a 20 20 70 50 74 72 6d 61 70 20 3d    }..  pPtrmap =
1cef1 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61   (u8 *)sqlite3Pa
1cef2 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61  gerGetData(pDbPa
1cef3 67 65 29 3b 0d 0a 0d 0a 20 20 6f 66 66 73 65 74  ge);....  offset
1cef4 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46   = PTRMAP_PTROFF
1cef5 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79  SET(iPtrmap, key
1cef6 29 3b 0d 0a 20 20 69 66 28 20 6f 66 66 73 65 74  );..  if( offset
1cef7 3c 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  <0 ){..    sqlit
1cef8 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62  e3PagerUnref(pDb
1cef9 50 61 67 65 29 3b 0d 0a 20 20 20 20 72 65 74 75  Page);..    retu
1cefa 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1cefb 54 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20  T_BKPT;..  }..  
1cefc 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 3c  assert( offset <
1cefd 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 62  = (int)pBt->usab
1cefe 6c 65 53 69 7a 65 2d 35 20 29 3b 0d 0a 20 20 61  leSize-5 );..  a
1ceff 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30  ssert( pEType!=0
1cf00 20 29 3b 0d 0a 20 20 2a 70 45 54 79 70 65 20 3d   );..  *pEType =
1cf01 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d   pPtrmap[offset]
1cf02 3b 0d 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29  ;..  if( pPgno )
1cf03 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79   *pPgno = get4by
1cf04 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73  te(&pPtrmap[offs
1cf05 65 74 2b 31 5d 29 3b 0d 0a 0d 0a 20 20 73 71 6c  et+1]);....  sql
1cf06 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
1cf07 44 62 50 61 67 65 29 3b 0d 0a 20 20 69 66 28 20  DbPage);..  if( 
1cf08 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45  *pEType<1 || *pE
1cf09 54 79 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20  Type>5 ) return 
1cf0a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1cf0b 4b 50 54 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  KPT;..  return S
1cf0c 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
1cf0d 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69  #else /* if defi
1cf0e 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned SQLITE_OMIT_
1cf0f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0d 0a 20  AUTOVACUUM */.. 
1cf10 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50   #define ptrmapP
1cf11 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0d 0a  ut(w,x,y,z,rc)..
1cf12 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70    #define ptrmap
1cf13 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c  Get(w,x,y,z) SQL
1cf14 49 54 45 5f 4f 4b 0d 0a 20 20 23 64 65 66 69 6e  ITE_OK..  #defin
1cf15 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50  e ptrmapPutOvflP
1cf16 74 72 28 78 2c 20 79 2c 20 72 63 29 0d 0a 23 65  tr(x, y, rc)..#e
1cf17 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  ndif..../*..** G
1cf18 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67  iven a btree pag
1cf19 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64  e and a cell ind
1cf1a 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20  ex (0 means the 
1cf1b 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0d 0a 2a  first cell on..*
1cf1c 2a 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65  * the page, 1 me
1cf1d 61 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63  ans the second c
1cf1e 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  ell, and so fort
1cf1f 68 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  h) return a poin
1cf20 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 63  ter..** to the c
1cf21 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0d 0a 2a 2a  ell content...**
1cf22 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1cf23 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72  e works only for
1cf24 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 6e   pages that do n
1cf25 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66  ot contain overf
1cf26 6c 6f 77 20 63 65 6c 6c 73 2e 0d 0a 2a 2f 0d 0a  low cells...*/..
1cf27 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c  #define findCell
1cf28 28 50 2c 49 29 20 5c 0d 0a 20 20 28 28 50 29 2d  (P,I) \..  ((P)-
1cf29 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d  >aData + ((P)->m
1cf2a 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 62 79  askPage & get2by
1cf2b 74 65 28 26 28 50 29 2d 3e 61 43 65 6c 6c 49 64  te(&(P)->aCellId
1cf2c 78 5b 32 2a 28 49 29 5d 29 29 29 0d 0a 23 64 65  x[2*(I)])))..#de
1cf2d 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c 76 32 28  fine findCellv2(
1cf2e 44 2c 4d 2c 4f 2c 49 29 20 28 44 2b 28 4d 26 67  D,M,O,I) (D+(M&g
1cf2f 65 74 32 62 79 74 65 28 44 2b 28 4f 2b 32 2a 28  et2byte(D+(O+2*(
1cf30 49 29 29 29 29 29 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  I)))))....../*..
1cf31 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63  ** This a more c
1cf32 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f  omplex version o
1cf33 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61  f findCell() tha
1cf34 74 20 77 6f 72 6b 73 20 66 6f 72 0d 0a 2a 2a 20  t works for..** 
1cf35 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f  pages that do co
1cf36 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63  ntain overflow c
1cf37 65 6c 6c 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ells...*/..stati
1cf38 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c  c u8 *findOverfl
1cf39 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a  owCell(MemPage *
1cf3a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c  pPage, int iCell
1cf3b 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
1cf3c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1cf3d 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1cf3e 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1cf3f 0d 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d  ..  for(i=pPage-
1cf40 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e  >nOverflow-1; i>
1cf41 3d 30 3b 20 69 2d 2d 29 7b 0d 0a 20 20 20 20 69  =0; i--){..    i
1cf42 6e 74 20 6b 3b 0d 0a 20 20 20 20 73 74 72 75 63  nt k;..    struc
1cf43 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76  t _OvflCell *pOv
1cf44 66 6c 3b 0d 0a 20 20 20 20 70 4f 76 66 6c 20 3d  fl;..    pOvfl =
1cf45 20 26 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69   &pPage->aOvfl[i
1cf46 5d 3b 0d 0a 20 20 20 20 6b 20 3d 20 70 4f 76 66  ];..    k = pOvf
1cf47 6c 2d 3e 69 64 78 3b 0d 0a 20 20 20 20 69 66 28  l->idx;..    if(
1cf48 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0d 0a 20 20   k<=iCell ){..  
1cf49 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c 6c      if( k==iCell
1cf4a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74   ){..        ret
1cf4b 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c  urn pOvfl->pCell
1cf4c 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1cf4d 20 20 69 43 65 6c 6c 2d 2d 3b 0d 0a 20 20 20 20    iCell--;..    
1cf4e 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
1cf4f 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
1cf50 20 69 43 65 6c 6c 29 3b 0d 0a 7d 0d 0a 0d 0a 2f   iCell);..}..../
1cf51 2a 0d 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65  *..** Parse a ce
1cf52 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b  ll content block
1cf53 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65   and fill in the
1cf54 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74   CellInfo struct
1cf55 75 72 65 2e 20 20 54 68 65 72 65 0d 0a 2a 2a 20  ure.  There..** 
1cf56 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73  are two versions
1cf57 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
1cf58 6e 2e 20 20 62 74 72 65 65 50 61 72 73 65 43 65  n.  btreeParseCe
1cf59 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0d 0a 2a  ll() takes a ..*
1cf5a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20  * cell index as 
1cf5b 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
1cf5c 65 6e 74 20 61 6e 64 20 62 74 72 65 65 50 61 72  ent and btreePar
1cf5d 73 65 43 65 6c 6c 50 74 72 28 29 20 0d 0a 2a 2a  seCellPtr() ..**
1cf5e 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72   takes a pointer
1cf5f 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20   to the body of 
1cf60 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74 73 20  the cell as its 
1cf61 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e  second argument.
1cf62 0d 0a 2a 2a 0d 0a 2a 2a 20 57 69 74 68 69 6e 20  ..**..** Within 
1cf63 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70  this file, the p
1cf64 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f  arseCell() macro
1cf65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69   can be called i
1cf66 6e 73 74 65 61 64 20 6f 66 0d 0a 2a 2a 20 62 74  nstead of..** bt
1cf67 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1cf68 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f  ). Using some co
1cf69 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 20 77 69  mpilers, this wi
1cf6a 6c 6c 20 62 65 20 66 61 73 74 65 72 2e 0d 0a 2a  ll be faster...*
1cf6b 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  /..static void b
1cf6c 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
1cf6d 28 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  (..  MemPage *pP
1cf6e 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  age,         /* 
1cf6f 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
1cf70 74 68 65 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20 75  the cell */..  u
1cf71 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20  8 *pCell,       
1cf72 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1cf73 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65  r to the cell te
1cf74 78 74 2e 20 2a 2f 0d 0a 20 20 43 65 6c 6c 49 6e  xt. */..  CellIn
1cf75 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20  fo *pInfo       
1cf76 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69    /* Fill in thi
1cf77 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a  s structure */..
1cf78 29 7b 0d 0a 20 20 75 31 36 20 6e 3b 20 20 20 20  ){..  u16 n;    
1cf79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1cf7a 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
1cf7b 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
1cf7c 61 64 65 72 20 2a 2f 0d 0a 20 20 75 33 32 20 6e  ader */..  u32 n
1cf7d 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20  Payload;        
1cf7e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1cf7f 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61  bytes of cell pa
1cf80 79 6c 6f 61 64 20 2a 2f 0d 0a 0d 0a 20 20 61 73  yload */....  as
1cf81 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1cf82 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1cf83 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  pBt->mutex) );..
1cf84 0d 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c  ..  pInfo->pCell
1cf85 20 3d 20 70 43 65 6c 6c 3b 0d 0a 20 20 61 73 73   = pCell;..  ass
1cf86 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66  ert( pPage->leaf
1cf87 3d 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65  ==0 || pPage->le
1cf88 61 66 3d 3d 31 20 29 3b 0d 0a 20 20 6e 20 3d 20  af==1 );..  n = 
1cf89 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
1cf8a 69 7a 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ize;..  assert( 
1cf8b 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65  n==4-4*pPage->le
1cf8c 61 66 20 29 3b 0d 0a 20 20 69 66 28 20 70 50 61  af );..  if( pPa
1cf8d 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0d 0a 20  ge->intKey ){.. 
1cf8e 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61     if( pPage->ha
1cf8f 73 44 61 74 61 20 29 7b 0d 0a 20 20 20 20 20 20  sData ){..      
1cf90 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  n += getVarint32
1cf91 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79  (&pCell[n], nPay
1cf92 6c 6f 61 64 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  load);..    }els
1cf93 65 7b 0d 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f  e{..      nPaylo
1cf94 61 64 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  ad = 0;..    }..
1cf95 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
1cf96 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75  nt(&pCell[n], (u
1cf97 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79  64*)&pInfo->nKey
1cf98 29 3b 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  );..    pInfo->n
1cf99 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
1cf9a 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
1cf9b 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30  pInfo->nData = 0
1cf9c 3b 0d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56  ;..    n += getV
1cf9d 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e  arint32(&pCell[n
1cf9e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0d 0a 20  ], nPayload);.. 
1cf9f 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d     pInfo->nKey =
1cfa0 20 6e 50 61 79 6c 6f 61 64 3b 0d 0a 20 20 7d 0d   nPayload;..  }.
1cfa1 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f  .  pInfo->nPaylo
1cfa2 61 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0d 0a  ad = nPayload;..
1cfa3 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 72    pInfo->nHeader
1cfa4 20 3d 20 6e 3b 0d 0a 20 20 74 65 73 74 63 61 73   = n;..  testcas
1cfa5 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61  e( nPayload==pPa
1cfa6 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0d  ge->maxLocal );.
1cfa7 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61  .  testcase( nPa
1cfa8 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61  yload==pPage->ma
1cfa9 78 4c 6f 63 61 6c 2b 31 20 29 3b 0d 0a 20 20 69  xLocal+1 );..  i
1cfaa 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f  f( likely(nPaylo
1cfab 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ad<=pPage->maxLo
1cfac 63 61 6c 29 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  cal) ){..    /* 
1cfad 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73  This is the (eas
1cfae 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77  y) common case w
1cfaf 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20  here the entire 
1cfb0 70 61 79 6c 6f 61 64 20 66 69 74 73 0d 0a 20 20  payload fits..  
1cfb1 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61    ** on the loca
1cfb2 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72  l page.  No over
1cfb3 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64  flow is required
1cfb4 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  ...    */..    i
1cfb5 66 28 20 28 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65  f( (pInfo->nSize
1cfb6 20 3d 20 28 75 31 36 29 28 6e 2b 6e 50 61 79 6c   = (u16)(n+nPayl
1cfb7 6f 61 64 29 29 3c 34 20 29 20 70 49 6e 66 6f 2d  oad))<4 ) pInfo-
1cfb8 3e 6e 53 69 7a 65 20 3d 20 34 3b 0d 0a 20 20 20  >nSize = 4;..   
1cfb9 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d   pInfo->nLocal =
1cfba 20 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0d   (u16)nPayload;.
1cfbb 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
1cfbc 72 66 6c 6f 77 20 3d 20 30 3b 0d 0a 20 20 7d 65  rflow = 0;..  }e
1cfbd 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20  lse{..    /* If 
1cfbe 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c  the payload will
1cfbf 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74   not fit complet
1cfc0 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  ely on the local
1cfc1 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0d 0a   page, we have..
1cfc2 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65      ** to decide
1cfc3 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f   how much to sto
1cfc4 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68  re locally and h
1cfc5 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c  ow much to spill
1cfc6 20 6f 6e 74 6f 0d 0a 20 20 20 20 2a 2a 20 6f 76   onto..    ** ov
1cfc7 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54  erflow pages.  T
1cfc8 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74  he strategy is t
1cfc9 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61  o minimize the a
1cfca 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0d  mount of unused.
1cfcb 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e  .    ** space on
1cfcc 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
1cfcd 77 68 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68  while keeping th
1cfce 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61  e amount of loca
1cfcf 6c 20 73 74 6f 72 61 67 65 0d 0a 20 20 20 20 2a  l storage..    *
1cfd0 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e  * in between min
1cfd1 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63  Local and maxLoc
1cfd2 61 6c 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  al...    **..   
1cfd3 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 63 68   ** Warning:  ch
1cfd4 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 20 6f  anging the way o
1cfd5 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20  verflow payload 
1cfd6 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69  is distributed i
1cfd7 6e 20 61 6e 79 0d 0a 20 20 20 20 2a 2a 20 77 61  n any..    ** wa
1cfd8 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e  y will result in
1cfd9 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   an incompatible
1cfda 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0d 0a 20   file format... 
1cfdb 20 20 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6d     */..    int m
1cfdc 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e  inLocal;  /* Min
1cfdd 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70  imum amount of p
1cfde 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61  ayload held loca
1cfdf 6c 6c 79 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  lly */..    int 
1cfe0 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61  maxLocal;  /* Ma
1cfe1 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  ximum amount of 
1cfe2 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63  payload held loc
1cfe3 61 6c 6c 79 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  ally */..    int
1cfe4 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f   surplus;   /* O
1cfe5 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20  verflow payload 
1cfe6 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f  available for lo
1cfe7 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0d 0a  cal storage */..
1cfe8 0d 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d  ..    minLocal =
1cfe9 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c   pPage->minLocal
1cfea 3b 0d 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20  ;..    maxLocal 
1cfeb 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  = pPage->maxLoca
1cfec 6c 3b 0d 0a 20 20 20 20 73 75 72 70 6c 75 73 20  l;..    surplus 
1cfed 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50  = minLocal + (nP
1cfee 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61  ayload - minLoca
1cfef 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  l)%(pPage->pBt->
1cff0 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b  usableSize - 4);
1cff1 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
1cff2 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61  surplus==maxLoca
1cff3 6c 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61  l );..    testca
1cff4 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78  se( surplus==max
1cff5 4c 6f 63 61 6c 2b 31 20 29 3b 0d 0a 20 20 20 20  Local+1 );..    
1cff6 69 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d  if( surplus <= m
1cff7 61 78 4c 6f 63 61 6c 20 29 7b 0d 0a 20 20 20 20  axLocal ){..    
1cff8 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20    pInfo->nLocal 
1cff9 3d 20 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0d  = (u16)surplus;.
1cffa 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
1cffb 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c     pInfo->nLocal
1cffc 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c   = (u16)minLocal
1cffd 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 49  ;..    }..    pI
1cffe 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d  nfo->iOverflow =
1cfff 20 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c   (u16)(pInfo->nL
1d000 6f 63 61 6c 20 2b 20 6e 29 3b 0d 0a 20 20 20 20  ocal + n);..    
1d001 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70  pInfo->nSize = p
1d002 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
1d003 2b 20 34 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 64  + 4;..  }..}..#d
1d004 65 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c 28  efine parseCell(
1d005 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49  pPage, iCell, pI
1d006 6e 66 6f 29 20 5c 0d 0a 20 20 62 74 72 65 65 50  nfo) \..  btreeP
1d007 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61  arseCellPtr((pPa
1d008 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70  ge), findCell((p
1d009 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c  Page), (iCell)),
1d00a 20 28 70 49 6e 66 6f 29 29 0d 0a 73 74 61 74 69   (pInfo))..stati
1d00b 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73  c void btreePars
1d00c 65 43 65 6c 6c 28 0d 0a 20 20 4d 65 6d 50 61 67  eCell(..  MemPag
1d00d 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
1d00e 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
1d00f 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
1d010 0d 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20  ..  int iCell,  
1d011 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1d012 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20  he cell index.  
1d013 46 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20  First cell is 0 
1d014 2a 2f 0d 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a  */..  CellInfo *
1d015 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a  pInfo         /*
1d016 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74   Fill in this st
1d017 72 75 63 74 75 72 65 20 2a 2f 0d 0a 29 7b 0d 0a  ructure */..){..
1d018 20 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67    parseCell(pPag
1d019 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29  e, iCell, pInfo)
1d01a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
1d01b 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c  ompute the total
1d01c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1d01d 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65   that a Cell nee
1d01e 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0d 0a  ds in the cell..
1d01f 2a 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66 20  ** data area of 
1d020 74 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20  the btree-page. 
1d021 20 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62   The return numb
1d022 65 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  er includes the 
1d023 63 65 6c 6c 0d 0a 2a 2a 20 64 61 74 61 20 68 65  cell..** data he
1d024 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63  ader and the loc
1d025 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20  al payload, but 
1d026 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  not any overflow
1d027 20 70 61 67 65 20 6f 72 0d 0a 2a 2a 20 74 68 65   page or..** the
1d028 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20 74   space used by t
1d029 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e  he cell pointer.
1d02a 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 75 31 36  ..*/..static u16
1d02b 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d   cellSizePtr(Mem
1d02c 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20  Page *pPage, u8 
1d02d 2a 70 43 65 6c 6c 29 7b 0d 0a 20 20 75 38 20 2a  *pCell){..  u8 *
1d02e 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70  pIter = &pCell[p
1d02f 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
1d030 7a 65 5d 3b 0d 0a 20 20 75 33 32 20 6e 53 69 7a  ze];..  u32 nSiz
1d031 65 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  e;....#ifdef SQL
1d032 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 2f 2a 20  ITE_DEBUG..  /* 
1d033 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
1d034 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
1d035 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ion should alway
1d036 73 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  s be the same as
1d037 0d 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c  ..  ** the (Cell
1d038 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75  Info.nSize) valu
1d039 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67  e found by doing
1d03a 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 6f 66   a full parse of
1d03b 20 74 68 65 0d 0a 20 20 2a 2a 20 63 65 6c 6c 2e   the..  ** cell.
1d03c 20 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   If SQLITE_DEBUG
1d03d 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 20   is defined, an 
1d03e 61 73 73 65 72 74 28 29 20 61 74 20 74 68 65 20  assert() at the 
1d03f 62 6f 74 74 6f 6d 20 6f 66 0d 0a 20 20 2a 2a 20  bottom of..  ** 
1d040 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65  this function ve
1d041 72 69 66 69 65 73 20 74 68 61 74 20 74 68 69 73  rifies that this
1d042 20 69 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f   invariant is no
1d043 74 20 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0d 0a  t violated. */..
1d044 20 20 43 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67    CellInfo debug
1d045 69 6e 66 6f 3b 0d 0a 20 20 62 74 72 65 65 50 61  info;..  btreePa
1d046 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
1d047 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 69  , pCell, &debugi
1d048 6e 66 6f 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  nfo);..#endif...
1d049 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e  .  if( pPage->in
1d04a 74 4b 65 79 20 29 7b 0d 0a 20 20 20 20 75 38 20  tKey ){..    u8 
1d04b 2a 70 45 6e 64 3b 0d 0a 20 20 20 20 69 66 28 20  *pEnd;..    if( 
1d04c 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
1d04d 7b 0d 0a 20 20 20 20 20 20 70 49 74 65 72 20 2b  {..      pIter +
1d04e 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 49  = getVarint32(pI
1d04f 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0d 0a 20 20  ter, nSize);..  
1d050 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
1d051 6e 53 69 7a 65 20 3d 20 30 3b 0d 0a 20 20 20 20  nSize = 0;..    
1d052 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 70 49 74 65  }....    /* pIte
1d053 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20  r now points at 
1d054 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67  the 64-bit integ
1d055 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20  er key value, a 
1d056 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20  variable length 
1d057 0d 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72  ..    ** integer
1d058 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
1d059 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74 65  block moves pIte
1d05a 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  r to point at th
1d05b 65 20 66 69 72 73 74 20 62 79 74 65 0d 0a 20 20  e first byte..  
1d05c 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e    ** past the en
1d05d 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c  d of the key val
1d05e 75 65 2e 20 2a 2f 0d 0a 20 20 20 20 70 45 6e 64  ue. */..    pEnd
1d05f 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0d 0a 20   = &pIter[9];.. 
1d060 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65     while( (*pIte
1d061 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 49 74  r++)&0x80 && pIt
1d062 65 72 3c 70 45 6e 64 20 29 3b 0d 0a 20 20 7d 65  er<pEnd );..  }e
1d063 6c 73 65 7b 0d 0a 20 20 20 20 70 49 74 65 72 20  lse{..    pIter 
1d064 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70  += getVarint32(p
1d065 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0d 0a 20  Iter, nSize);.. 
1d066 20 7d 0d 0a 0d 0a 20 20 74 65 73 74 63 61 73 65   }....  testcase
1d067 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e  ( nSize==pPage->
1d068 6d 61 78 4c 6f 63 61 6c 20 29 3b 0d 0a 20 20 74  maxLocal );..  t
1d069 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d  estcase( nSize==
1d06a 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b  pPage->maxLocal+
1d06b 31 20 29 3b 0d 0a 20 20 69 66 28 20 6e 53 69 7a  1 );..  if( nSiz
1d06c 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  e>pPage->maxLoca
1d06d 6c 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6d 69  l ){..    int mi
1d06e 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e  nLocal = pPage->
1d06f 6d 69 6e 4c 6f 63 61 6c 3b 0d 0a 20 20 20 20 6e  minLocal;..    n
1d070 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20  Size = minLocal 
1d071 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c 6f  + (nSize - minLo
1d072 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e 70  cal) % (pPage->p
1d073 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
1d074 20 34 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61   4);..    testca
1d075 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65  se( nSize==pPage
1d076 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0d 0a 20  ->maxLocal );.. 
1d077 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69     testcase( nSi
1d078 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ze==pPage->maxLo
1d079 63 61 6c 2b 31 20 29 3b 0d 0a 20 20 20 20 69 66  cal+1 );..    if
1d07a 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d  ( nSize>pPage->m
1d07b 61 78 4c 6f 63 61 6c 20 29 7b 0d 0a 20 20 20 20  axLocal ){..    
1d07c 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63    nSize = minLoc
1d07d 61 6c 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  al;..    }..    
1d07e 6e 53 69 7a 65 20 2b 3d 20 34 3b 0d 0a 20 20 7d  nSize += 4;..  }
1d07f 0d 0a 20 20 6e 53 69 7a 65 20 2b 3d 20 28 75 33  ..  nSize += (u3
1d080 32 29 28 70 49 74 65 72 20 2d 20 70 43 65 6c 6c  2)(pIter - pCell
1d081 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 6d  );....  /* The m
1d082 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61  inimum size of a
1d083 6e 79 20 63 65 6c 6c 20 69 73 20 34 20 62 79 74  ny cell is 4 byt
1d084 65 73 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 53  es. */..  if( nS
1d085 69 7a 65 3c 34 20 29 7b 0d 0a 20 20 20 20 6e 53  ize<4 ){..    nS
1d086 69 7a 65 20 3d 20 34 3b 0d 0a 20 20 7d 0d 0a 0d  ize = 4;..  }...
1d087 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a 65  .  assert( nSize
1d088 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a  ==debuginfo.nSiz
1d089 65 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28  e );..  return (
1d08a 75 31 36 29 6e 53 69 7a 65 3b 0d 0a 7d 0d 0a 0d  u16)nSize;..}...
1d08b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1d08c 45 42 55 47 0d 0a 2f 2a 20 54 68 69 73 20 76 61  EBUG../* This va
1d08d 72 69 61 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53  riation on cellS
1d08e 69 7a 65 50 74 72 28 29 20 69 73 20 75 73 65 64  izePtr() is used
1d08f 20 69 6e 73 69 64 65 20 6f 66 20 61 73 73 65 72   inside of asser
1d090 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 0d 0a  t() statements..
1d091 2a 2a 20 6f 6e 6c 79 2e 20 2a 2f 0d 0a 73 74 61  ** only. */..sta
1d092 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65  tic u16 cellSize
1d093 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1d094 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0d 0a 20 20   int iCell){..  
1d095 72 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50  return cellSizeP
1d096 74 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65  tr(pPage, findCe
1d097 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29  ll(pPage, iCell)
1d098 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  );..}..#endif...
1d099 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d09a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d  OMIT_AUTOVACUUM.
1d09b 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 63  ./*..** If the c
1d09c 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20  ell pCell, part 
1d09d 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63 6f  of page pPage co
1d09e 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
1d09f 0d 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66  ..** to an overf
1d0a0 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74  low page, insert
1d0a1 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74   an entry into t
1d0a2 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0d 0a  he pointer-map..
1d0a3 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66  ** for the overf
1d0a4 6c 6f 77 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 73  low page...*/..s
1d0a5 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61  tatic void ptrma
1d0a6 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50  pPutOvflPtr(MemP
1d0a7 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
1d0a8 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29  pCell, int *pRC)
1d0a9 7b 0d 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  {..  CellInfo in
1d0aa 66 6f 3b 0d 0a 20 20 69 66 28 20 2a 70 52 43 20  fo;..  if( *pRC 
1d0ab 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73  ) return;..  ass
1d0ac 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b  ert( pCell!=0 );
1d0ad 0d 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65  ..  btreeParseCe
1d0ae 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
1d0af 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0d 0a 20 20 61  ll, &info);..  a
1d0b0 73 73 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61  ssert( (info.nDa
1d0b1 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65  ta+(pPage->intKe
1d0b2 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d  y?0:info.nKey))=
1d0b3 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29  =info.nPayload )
1d0b4 3b 0d 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f  ;..  if( info.iO
1d0b5 76 65 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20 20  verflow ){..    
1d0b6 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34  Pgno ovfl = get4
1d0b7 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f  byte(&pCell[info
1d0b8 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0d 0a 20  .iOverflow]);.. 
1d0b9 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50 61     ptrmapPut(pPa
1d0ba 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50  ge->pBt, ovfl, P
1d0bb 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c  TRMAP_OVERFLOW1,
1d0bc 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52   pPage->pgno, pR
1d0bd 43 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e  C);..  }..}..#en
1d0be 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  dif....../*..** 
1d0bf 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70  Defragment the p
1d0c0 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20  age given.  All 
1d0c1 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20  Cells are moved 
1d0c2 74 6f 20 74 68 65 0d 0a 2a 2a 20 65 6e 64 20 6f  to the..** end o
1d0c3 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61  f the page and a
1d0c4 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73  ll free space is
1d0c5 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
1d0c6 6f 6e 65 0d 0a 2a 2a 20 62 69 67 20 46 72 65 65  one..** big Free
1d0c7 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20  Blk that occurs 
1d0c8 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68  in between the h
1d0c9 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0d 0a  eader and cell..
1d0ca 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ** pointer array
1d0cb 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f   and the cell co
1d0cc 6e 74 65 6e 74 20 61 72 65 61 2e 0d 0a 2a 2f 0d  ntent area...*/.
1d0cd 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 72  .static int defr
1d0ce 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61  agmentPage(MemPa
1d0cf 67 65 20 2a 70 50 61 67 65 29 7b 0d 0a 20 20 69  ge *pPage){..  i
1d0d0 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1d0d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1d0d2 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20  p counter */..  
1d0d3 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
1d0d4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
1d0d5 64 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20  dress of a i-th 
1d0d6 63 65 6c 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 68  cell */..  int h
1d0d7 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
1d0d8 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1d0d9 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64  to the page head
1d0da 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 69 7a  er */..  int siz
1d0db 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1d0dc 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
1d0dd 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20   cell */..  int 
1d0de 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20  usableSize;     
1d0df 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1d0e0 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73   of usable bytes
1d0e1 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0d 0a 20   on a page */.. 
1d0e2 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b   int cellOffset;
1d0e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1d0e4 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c  ffset to the cel
1d0e5 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  l pointer array 
1d0e6 2a 2f 0d 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20  */..  int cbrk; 
1d0e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d0e8 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68   /* Offset to th
1d0e9 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
1d0ea 72 65 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43  rea */..  int nC
1d0eb 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
1d0ec 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d0ed 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70  f cells on the p
1d0ee 61 67 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e  age */..  unsign
1d0ef 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 20  ed char *data;  
1d0f0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
1d0f1 20 64 61 74 61 20 2a 2f 0d 0a 20 20 75 6e 73 69   data */..  unsi
1d0f2 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b  gned char *temp;
1d0f3 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61         /* Temp a
1d0f4 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e  rea for cell con
1d0f5 74 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  tent */..  int i
1d0f6 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 20  CellFirst;      
1d0f7 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
1d0f8 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e  llowable cell in
1d0f9 64 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43  dex */..  int iC
1d0fa 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20  ellLast;        
1d0fb 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73       /* Last pos
1d0fc 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78  sible cell index
1d0fd 20 2a 2f 0d 0a 0d 0a 0d 0a 20 20 61 73 73 65 72   */......  asser
1d0fe 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1d0ff 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
1d100 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0d 0a 20  ->pDbPage) );.. 
1d101 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1d102 70 42 74 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  pBt!=0 );..  ass
1d103 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d  ert( pPage->pBt-
1d104 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53  >usableSize <= S
1d105 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
1d106 49 5a 45 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  IZE );..  assert
1d107 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
1d108 6f 77 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  ow==0 );..  asse
1d109 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1d10a 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
1d10b 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  t->mutex) );..  
1d10c 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61  temp = sqlite3Pa
1d10d 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 61  gerTempSpace(pPa
1d10e 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29  ge->pBt->pPager)
1d10f 3b 0d 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  ;..  data = pPag
1d110 65 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 68 64 72  e->aData;..  hdr
1d111 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
1d112 73 65 74 3b 0d 0a 20 20 63 65 6c 6c 4f 66 66 73  set;..  cellOffs
1d113 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c  et = pPage->cell
1d114 4f 66 66 73 65 74 3b 0d 0a 20 20 6e 43 65 6c 6c  Offset;..  nCell
1d115 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
1d116 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c  ..  assert( nCel
1d117 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74  l==get2byte(&dat
1d118 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0d 0a 20 20  a[hdr+3]) );..  
1d119 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61  usableSize = pPa
1d11a 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
1d11b 69 7a 65 3b 0d 0a 20 20 63 62 72 6b 20 3d 20 67  ize;..  cbrk = g
1d11c 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
1d11d 72 2b 35 5d 29 3b 0d 0a 20 20 6d 65 6d 63 70 79  r+5]);..  memcpy
1d11e 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64  (&temp[cbrk], &d
1d11f 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c  ata[cbrk], usabl
1d120 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0d 0a  eSize - cbrk);..
1d121 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53    cbrk = usableS
1d122 69 7a 65 3b 0d 0a 20 20 69 43 65 6c 6c 46 69 72  ize;..  iCellFir
1d123 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20  st = cellOffset 
1d124 2b 20 32 2a 6e 43 65 6c 6c 3b 0d 0a 20 20 69 43  + 2*nCell;..  iC
1d125 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65  ellLast = usable
1d126 53 69 7a 65 20 2d 20 34 3b 0d 0a 20 20 66 6f 72  Size - 4;..  for
1d127 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
1d128 2b 2b 29 7b 0d 0a 20 20 20 20 75 38 20 2a 70 41  ++){..    u8 *pA
1d129 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ddr;     /* The 
1d12a 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65  i-th cell pointe
1d12b 72 20 2a 2f 0d 0a 20 20 20 20 70 41 64 64 72 20  r */..    pAddr 
1d12c 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73  = &data[cellOffs
1d12d 65 74 20 2b 20 69 2a 32 5d 3b 0d 0a 20 20 20 20  et + i*2];..    
1d12e 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 41  pc = get2byte(pA
1d12f 64 64 72 29 3b 0d 0a 20 20 20 20 74 65 73 74 63  ddr);..    testc
1d130 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69  ase( pc==iCellFi
1d131 72 73 74 20 29 3b 0d 0a 20 20 20 20 74 65 73 74  rst );..    test
1d132 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c  case( pc==iCellL
1d133 61 73 74 20 29 3b 0d 0a 23 69 66 20 21 64 65 66  ast );..#if !def
1d134 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
1d135 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c  LE_OVERSIZE_CELL
1d136 5f 43 48 45 43 4b 29 0d 0a 20 20 20 20 2f 2a 20  _CHECK)..    /* 
1d137 54 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73  These conditions
1d138 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
1d139 65 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20 62  en verified in b
1d13a 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 0d 0a  treeInitPage()..
1d13b 20 20 20 20 2a 2a 20 69 66 20 53 51 4c 49 54 45      ** if SQLITE
1d13c 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45  _ENABLE_OVERSIZE
1d13d 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64  _CELL_CHECK is d
1d13e 65 66 69 6e 65 64 20 0d 0a 20 20 20 20 2a 2f 0d  efined ..    */.
1d13f 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c  .    if( pc<iCel
1d140 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65  lFirst || pc>iCe
1d141 6c 6c 4c 61 73 74 20 29 7b 0d 0a 20 20 20 20 20  llLast ){..     
1d142 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1d143 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1d144 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20    }..#endif..   
1d145 20 61 73 73 65 72 74 28 20 70 63 3e 3d 69 43 65   assert( pc>=iCe
1d146 6c 6c 46 69 72 73 74 20 26 26 20 70 63 3c 3d 69  llFirst && pc<=i
1d147 43 65 6c 6c 4c 61 73 74 20 29 3b 0d 0a 20 20 20  CellLast );..   
1d148 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65   size = cellSize
1d149 50 74 72 28 70 50 61 67 65 2c 20 26 74 65 6d 70  Ptr(pPage, &temp
1d14a 5b 70 63 5d 29 3b 0d 0a 20 20 20 20 63 62 72 6b  [pc]);..    cbrk
1d14b 20 2d 3d 20 73 69 7a 65 3b 0d 0a 23 69 66 20 64   -= size;..#if d
1d14c 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
1d14d 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
1d14e 4c 4c 5f 43 48 45 43 4b 29 0d 0a 20 20 20 20 69  LL_CHECK)..    i
1d14f 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72  f( cbrk<iCellFir
1d150 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  st ){..      ret
1d151 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1d152 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d  PT_BKPT;..    }.
1d153 0a 23 65 6c 73 65 0d 0a 20 20 20 20 69 66 28 20  .#else..    if( 
1d154 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20  cbrk<iCellFirst 
1d155 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c  || pc+size>usabl
1d156 65 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20  eSize ){..      
1d157 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1d158 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20  RRUPT_BKPT;..   
1d159 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
1d15a 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 69 7a  assert( cbrk+siz
1d15b 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 26  e<=usableSize &&
1d15c 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73   cbrk>=iCellFirs
1d15d 74 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61  t );..    testca
1d15e 73 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d 75  se( cbrk+size==u
1d15f 73 61 62 6c 65 53 69 7a 65 20 29 3b 0d 0a 20 20  sableSize );..  
1d160 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73    testcase( pc+s
1d161 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize==usableSize 
1d162 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  );..    memcpy(&
1d163 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d  data[cbrk], &tem
1d164 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0d 0a 20  p[pc], size);.. 
1d165 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64 64     put2byte(pAdd
1d166 72 2c 20 63 62 72 6b 29 3b 0d 0a 20 20 7d 0d 0a  r, cbrk);..  }..
1d167 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d    assert( cbrk>=
1d168 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0d 0a 20  iCellFirst );.. 
1d169 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
1d16a 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0d 0a  hdr+5], cbrk);..
1d16b 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20    data[hdr+1] = 
1d16c 30 3b 0d 0a 20 20 64 61 74 61 5b 68 64 72 2b 32  0;..  data[hdr+2
1d16d 5d 20 3d 20 30 3b 0d 0a 20 20 64 61 74 61 5b 68  ] = 0;..  data[h
1d16e 64 72 2b 37 5d 20 3d 20 30 3b 0d 0a 20 20 6d 65  dr+7] = 0;..  me
1d16f 6d 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c 6c  mset(&data[iCell
1d170 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d  First], 0, cbrk-
1d171 69 43 65 6c 6c 46 69 72 73 74 29 3b 0d 0a 20 20  iCellFirst);..  
1d172 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1d173 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1d174 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
1d175 29 3b 0d 0a 20 20 69 66 28 20 63 62 72 6b 2d 69  );..  if( cbrk-i
1d176 43 65 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65  CellFirst!=pPage
1d177 2d 3e 6e 46 72 65 65 20 29 7b 0d 0a 20 20 20 20  ->nFree ){..    
1d178 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1d179 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 7d  RRUPT_BKPT;..  }
1d17a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1d17b 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
1d17c 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
1d17d 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
1d17e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
1d17f 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 73   B-Tree page pas
1d180 73 65 64 0d 0a 2a 2a 20 61 73 20 74 68 65 20 66  sed..** as the f
1d181 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 57  irst argument. W
1d182 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 20  rite into *pIdx 
1d183 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70  the index into p
1d184 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0d 0a 2a  Page->aData[]..*
1d185 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62  * of the first b
1d186 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64  yte of allocated
1d187 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 65   space. Return e
1d188 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20  ither SQLITE_OK 
1d189 6f 72 0d 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20  or..** an error 
1d18a 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51  code (usually SQ
1d18b 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0d 0a  LITE_CORRUPT)...
1d18c 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  **..** The calle
1d18d 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  r guarantees tha
1d18e 74 20 74 68 65 72 65 20 69 73 20 73 75 66 66 69  t there is suffi
1d18f 63 69 65 6e 74 20 73 70 61 63 65 20 74 6f 20 6d  cient space to m
1d190 61 6b 65 20 74 68 65 0d 0a 2a 2a 20 61 6c 6c 6f  ake the..** allo
1d191 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f  cation.  This ro
1d192 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 65 64  utine might need
1d193 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 69   to defragment i
1d194 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 6e 67  n order to bring
1d195 0d 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 61  ..** all the spa
1d196 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f 77  ce together, how
1d197 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 74  ever.  This rout
1d198 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 75  ine will avoid u
1d199 73 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 66 69 72  sing..** the fir
1d19a 73 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73  st two bytes pas
1d19b 74 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  t the cell point
1d19c 65 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72  er area since pr
1d19d 65 73 75 6d 61 62 6c 79 20 74 68 69 73 0d 0a 2a  esumably this..*
1d19e 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  * allocation is 
1d19f 62 65 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72  being made in or
1d1a0 64 65 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20  der to insert a 
1d1a1 6e 65 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20  new cell, so we 
1d1a2 77 69 6c 6c 0d 0a 2a 2a 20 61 6c 73 6f 20 65 6e  will..** also en
1d1a3 64 20 75 70 20 6e 65 65 64 69 6e 67 20 61 20 6e  d up needing a n
1d1a4 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e  ew cell pointer.
1d1a5 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1d1a6 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d   allocateSpace(M
1d1a7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
1d1a8 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70  nt nByte, int *p
1d1a9 49 64 78 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 69  Idx){..  const i
1d1aa 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  nt hdr = pPage->
1d1ab 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a  hdrOffset;    /*
1d1ac 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20   Local cache of 
1d1ad 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
1d1ae 20 2a 2f 0d 0a 20 20 75 38 20 2a 20 63 6f 6e 73   */..  u8 * cons
1d1af 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  t data = pPage->
1d1b0 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c  aData;      /* L
1d1b1 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50  ocal cache of pP
1d1b2 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0d 0a 20  age->aData */.. 
1d1b3 20 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20   int nFrag;     
1d1b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d1b5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d1b6 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79  of fragmented by
1d1b7 74 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0d  tes on pPage */.
1d1b8 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20  .  int top;     
1d1b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d1ba 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
1d1bb 20 62 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f   byte of cell co
1d1bc 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0d 0a 20  ntent area */.. 
1d1bd 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20   int gap;       
1d1be 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f   /* First byte o
1d1bf 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 63 65  f gap between ce
1d1c0 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20  ll pointers and 
1d1c1 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0d  cell content */.
1d1c2 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1d1c3 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65     /* Integer re
1d1c4 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  turn code */..  
1d1c5 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20  int usableSize; 
1d1c6 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f  /* Usable size o
1d1c7 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0d 0a 20  f the page */.. 
1d1c8 20 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c   ..  assert( sql
1d1c9 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1d1ca 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
1d1cb 61 67 65 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  age) );..  asser
1d1cc 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b  t( pPage->pBt );
1d1cd 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1d1ce 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1d1cf 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1d1d0 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1d1d1 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20  nByte>=0 );  /* 
1d1d2 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  Minimum cell siz
1d1d3 65 20 69 73 20 34 20 2a 2f 0d 0a 20 20 61 73 73  e is 4 */..  ass
1d1d4 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65  ert( pPage->nFre
1d1d5 65 3e 3d 6e 42 79 74 65 20 29 3b 0d 0a 20 20 61  e>=nByte );..  a
1d1d6 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
1d1d7 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0d 0a 20  verflow==0 );.. 
1d1d8 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 50   usableSize = pP
1d1d9 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1d1da 53 69 7a 65 3b 0d 0a 20 20 61 73 73 65 72 74 28  Size;..  assert(
1d1db 20 6e 42 79 74 65 20 3c 20 75 73 61 62 6c 65 53   nByte < usableS
1d1dc 69 7a 65 2d 38 20 29 3b 0d 0a 0d 0a 20 20 6e 46  ize-8 );....  nF
1d1dd 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37  rag = data[hdr+7
1d1de 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  ];..  assert( pP
1d1df 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
1d1e0 3d 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a  == hdr + 12 - 4*
1d1e1 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0d 0a  pPage->leaf );..
1d1e2 20 20 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63    gap = pPage->c
1d1e3 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50  ellOffset + 2*pP
1d1e4 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 20 20 74  age->nCell;..  t
1d1e5 6f 70 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74  op = get2byteNot
1d1e6 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35  Zero(&data[hdr+5
1d1e7 5d 29 3b 0d 0a 20 20 69 66 28 20 67 61 70 3e 74  ]);..  if( gap>t
1d1e8 6f 70 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  op ) return SQLI
1d1e9 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1d1ea 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  ..  testcase( ga
1d1eb 70 2b 32 3d 3d 74 6f 70 20 29 3b 0d 0a 20 20 74  p+2==top );..  t
1d1ec 65 73 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d  estcase( gap+1==
1d1ed 74 6f 70 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  top );..  testca
1d1ee 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0d  se( gap==top );.
1d1ef 0a 0d 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d  ...  if( nFrag>=
1d1f0 36 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41 6c  60 ){..    /* Al
1d1f1 77 61 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20  ways defragment 
1d1f2 68 69 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65  highly fragmente
1d1f3 64 20 70 61 67 65 73 20 2a 2f 0d 0a 20 20 20 20  d pages */..    
1d1f4 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50  rc = defragmentP
1d1f5 61 67 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 20  age(pPage);..   
1d1f6 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1d1f7 20 72 63 3b 0d 0a 20 20 20 20 74 6f 70 20 3d 20   rc;..    top = 
1d1f8 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28  get2byteNotZero(
1d1f9 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0d 0a  &data[hdr+5]);..
1d1fa 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 70 2b    }else if( gap+
1d1fb 32 3c 3d 74 6f 70 20 29 7b 0d 0a 20 20 20 20 2f  2<=top ){..    /
1d1fc 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72 65  * Search the fre
1d1fd 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f  elist looking fo
1d1fe 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69  r a free slot bi
1d1ff 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69  g enough to sati
1d200 73 66 79 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65  sfy ..    ** the
1d201 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c   request. The al
1d202 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65  location is made
1d203 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20   from the first 
1d204 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0d 0a 20  free slot in .. 
1d205 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74     ** the list t
1d206 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f  hat is large eno
1d207 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74  ugh to accomadat
1d208 65 20 69 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  e it...    */.. 
1d209 20 20 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b     int pc, addr;
1d20a 0d 0a 20 20 20 20 66 6f 72 28 61 64 64 72 3d 68  ..    for(addr=h
1d20b 64 72 2b 31 3b 20 28 70 63 20 3d 20 67 65 74 32  dr+1; (pc = get2
1d20c 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
1d20d 29 29 3e 30 3b 20 61 64 64 72 3d 70 63 29 7b 0d  ))>0; addr=pc){.
1d20e 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 3b  .      int size;
1d20f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1d210 69 7a 65 20 6f 66 20 74 68 65 20 66 72 65 65 20  ize of the free 
1d211 73 6c 6f 74 20 2a 2f 0d 0a 20 20 20 20 20 20 69  slot */..      i
1d212 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 7a 65  f( pc>usableSize
1d213 2d 34 20 7c 7c 20 70 63 3c 61 64 64 72 2b 34 20  -4 || pc<addr+4 
1d214 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
1d215 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1d216 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 7d  T_BKPT;..      }
1d217 0d 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  ..      size = g
1d218 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
1d219 2b 32 5d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  +2]);..      if(
1d21a 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0d   size>=nByte ){.
1d21b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d  .        int x =
1d21c 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0d 0a   size - nByte;..
1d21d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1d21e 28 20 78 3d 3d 34 20 29 3b 0d 0a 20 20 20 20 20  ( x==4 );..     
1d21f 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d     testcase( x==
1d220 33 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  3 );..        if
1d221 28 20 78 3c 34 20 29 7b 0d 0a 20 20 20 20 20 20  ( x<4 ){..      
1d222 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68      /* Remove th
1d223 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20  e slot from the 
1d224 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74  free-list. Updat
1d225 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d  e the number of.
1d226 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72  .          ** fr
1d227 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 77  agmented bytes w
1d228 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20  ithin the page. 
1d229 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 6d 65  */..          me
1d22a 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 5d  mcpy(&data[addr]
1d22b 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b  , &data[pc], 2);
1d22c 0d 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  ..          data
1d22d 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e  [hdr+7] = (u8)(n
1d22e 46 72 61 67 20 2b 20 78 29 3b 0d 0a 20 20 20 20  Frag + x);..    
1d22f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69      }else if( si
1d230 7a 65 2b 70 63 20 3e 20 75 73 61 62 6c 65 53 69  ze+pc > usableSi
1d231 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  ze ){..         
1d232 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1d233 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1d234 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
1d235 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
1d236 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f 6e 20 74  lot remains on t
1d237 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 52 65  he free-list. Re
1d238 64 75 63 65 20 69 74 73 20 73 69 7a 65 20 74 6f  duce its size to
1d239 20 61 63 63 6f 75 6e 74 0d 0a 20 20 20 20 20 20   account..      
1d23a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70      ** for the p
1d23b 6f 72 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74  ortion used by t
1d23c 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f  he new allocatio
1d23d 6e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  n. */..         
1d23e 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
1d23f 70 63 2b 32 5d 2c 20 78 29 3b 0d 0a 20 20 20 20  pc+2], x);..    
1d240 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 2a      }..        *
1d241 70 49 64 78 20 3d 20 70 63 20 2b 20 78 3b 0d 0a  pIdx = pc + x;..
1d242 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1d243 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
1d244 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
1d245 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20  ..  /* Check to 
1d246 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20  make sure there 
1d247 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20  is enough space 
1d248 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 73 61  in the gap to sa
1d249 74 69 73 66 79 0d 0a 20 20 2a 2a 20 74 68 65 20  tisfy..  ** the 
1d24a 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20  allocation.  If 
1d24b 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 2e  not, defragment.
1d24c 0d 0a 20 20 2a 2f 0d 0a 20 20 74 65 73 74 63 61  ..  */..  testca
1d24d 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d  se( gap+2+nByte=
1d24e 3d 74 6f 70 20 29 3b 0d 0a 20 20 69 66 28 20 67  =top );..  if( g
1d24f 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29  ap+2+nByte>top )
1d250 7b 0d 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72  {..    rc = defr
1d251 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65  agmentPage(pPage
1d252 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29  );..    if( rc )
1d253 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1d254 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 4e   top = get2byteN
1d255 6f 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72  otZero(&data[hdr
1d256 2b 35 5d 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  +5]);..    asser
1d257 74 28 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f  t( gap+nByte<=to
1d258 70 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20  p );..  }...... 
1d259 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d   /* Allocate mem
1d25a 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70  ory from the gap
1d25b 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20   in between the 
1d25c 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
1d25d 61 79 0d 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ay..  ** and the
1d25e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
1d25f 65 61 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e  ea.  The btreeIn
1d260 69 74 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61  itPage() call ha
1d261 73 20 61 6c 72 65 61 64 79 0d 0a 20 20 2a 2a 20  s already..  ** 
1d262 76 61 6c 69 64 61 74 65 64 20 74 68 65 20 66 72  validated the fr
1d263 65 65 6c 69 73 74 2e 20 20 47 69 76 65 6e 20 74  eelist.  Given t
1d264 68 61 74 20 74 68 65 20 66 72 65 65 6c 69 73 74  hat the freelist
1d265 20 69 73 20 76 61 6c 69 64 2c 20 74 68 65 72 65   is valid, there
1d266 0d 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 79  ..  ** is no way
1d267 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61   that the alloca
1d268 74 69 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 20  tion can extend 
1d269 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74  off the end of t
1d26a 68 65 20 70 61 67 65 2e 0d 0a 20 20 2a 2a 20 54  he page...  ** T
1d26b 68 65 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f  he assert() belo
1d26c 77 20 76 65 72 69 66 69 65 73 20 74 68 65 20 70  w verifies the p
1d26d 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
1d26e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 74 6f 70 20 2d  ...  */..  top -
1d26f 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 70 75 74 32  = nByte;..  put2
1d270 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1d271 5d 2c 20 74 6f 70 29 3b 0d 0a 20 20 61 73 73 65  ], top);..  asse
1d272 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 3c 3d  rt( top+nByte <=
1d273 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74   (int)pPage->pBt
1d274 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0d  ->usableSize );.
1d275 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b 0d  .  *pIdx = top;.
1d276 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1d277 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
1d278 2a 20 52 65 74 75 72 6e 20 61 20 73 65 63 74 69  * Return a secti
1d279 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d  on of the pPage-
1d27a 3e 61 44 61 74 61 20 74 6f 20 74 68 65 20 66 72  >aData to the fr
1d27b 65 65 6c 69 73 74 2e 0d 0a 2a 2a 20 54 68 65 20  eelist...** The 
1d27c 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
1d27d 65 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b  e new free block
1d27e 20 69 73 20 70 50 61 67 65 2d 3e 61 44 69 73 6b   is pPage->aDisk
1d27f 5b 73 74 61 72 74 5d 0d 0a 2a 2a 20 61 6e 64 20  [start]..** and 
1d280 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
1d281 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a 65 22 20  block is "size" 
1d282 62 79 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4d  bytes...**..** M
1d283 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f 72  ost of the effor
1d284 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c 76  t here is involv
1d285 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67 20  ed in coalesing 
1d286 61 64 6a 61 63 65 6e 74 0d 0a 2a 2a 20 66 72 65  adjacent..** fre
1d287 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20  e blocks into a 
1d288 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20  single big free 
1d289 62 6c 6f 63 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  block...*/..stat
1d28a 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65  ic int freeSpace
1d28b 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1d28c 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
1d28d 73 69 7a 65 29 7b 0d 0a 20 20 69 6e 74 20 61 64  size){..  int ad
1d28e 64 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b  dr, pbegin, hdr;
1d28f 0d 0a 20 20 69 6e 74 20 69 4c 61 73 74 3b 20 20  ..  int iLast;  
1d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d291 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74        /* Largest
1d292 20 70 6f 73 73 69 62 6c 65 20 66 72 65 65 62 6c   possible freebl
1d293 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0d 0a 20  ock offset */.. 
1d294 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1d295 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
1d296 61 74 61 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  ata;....  assert
1d297 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20  ( pPage->pBt!=0 
1d298 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
1d299 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1d29a 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1d29b 50 61 67 65 29 20 29 3b 0d 0a 20 20 61 73 73 65  Page) );..  asse
1d29c 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65  rt( start>=pPage
1d29d 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50  ->hdrOffset+6+pP
1d29e 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
1d29f 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  e );..  assert( 
1d2a0 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 20 3c  (start + size) <
1d2a1 3d 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42  = (int)pPage->pB
1d2a2 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  t->usableSize );
1d2a3 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1d2a4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1d2a5 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1d2a6 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1d2a7 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20  size>=0 );   /* 
1d2a8 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  Minimum cell siz
1d2a9 65 20 69 73 20 34 20 2a 2f 0d 0a 0d 0a 20 20 69  e is 4 */....  i
1d2aa 66 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 62  f( pPage->pBt->b
1d2ab 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 53 45  tsFlags & BTS_SE
1d2ac 43 55 52 45 5f 44 45 4c 45 54 45 20 29 7b 0d 0a  CURE_DELETE ){..
1d2ad 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65      /* Overwrite
1d2ae 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61   deleted informa
1d2af 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 20  tion with zeros 
1d2b0 77 68 65 6e 20 74 68 65 20 73 65 63 75 72 65 5f  when the secure_
1d2b1 64 65 6c 65 74 65 0d 0a 20 20 20 20 2a 2a 20 6f  delete..    ** o
1d2b2 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ption is enabled
1d2b3 20 2a 2f 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28   */..    memset(
1d2b4 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c  &data[start], 0,
1d2b5 20 73 69 7a 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a   size);..  }....
1d2b6 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61    /* Add the spa
1d2b7 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  ce back into the
1d2b8 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
1d2b9 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74  freeblocks.  Not
1d2ba 65 20 74 68 61 74 0d 0a 20 20 2a 2a 20 65 76 65  e that..  ** eve
1d2bb 6e 20 74 68 6f 75 67 68 20 74 68 65 20 66 72 65  n though the fre
1d2bc 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20  eblock list was 
1d2bd 63 68 65 63 6b 65 64 20 62 79 20 62 74 72 65 65  checked by btree
1d2be 49 6e 69 74 50 61 67 65 28 29 2c 0d 0a 20 20 2a  InitPage(),..  *
1d2bf 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  * btreeInitPage(
1d2c0 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65 63 74  ) did not detect
1d2c1 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63 65 6c   overlapping cel
1d2c2 6c 73 20 6f 72 0d 0a 20 20 2a 2a 20 66 72 65 65  ls or..  ** free
1d2c3 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65 72  blocks that over
1d2c4 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20 20  lapped cells.   
1d2c5 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74 65  Nor does it dete
1d2c6 63 74 20 77 68 65 6e 20 74 68 65 0d 0a 20 20 2a  ct when the..  *
1d2c7 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  * cell content a
1d2c8 72 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20  rea exceeds the 
1d2c9 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67  value in the pag
1d2ca 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68  e header.  If th
1d2cb 65 73 65 0d 0a 20 20 2a 2a 20 73 69 74 75 61 74  ese..  ** situat
1d2cc 69 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e  ions arise, then
1d2cd 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65   subsequent inse
1d2ce 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69  rt operations mi
1d2cf 67 68 74 20 63 6f 72 72 75 70 74 0d 0a 20 20 2a  ght corrupt..  *
1d2d0 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  * the freelist. 
1d2d1 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74   So we do need t
1d2d2 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72  o check for corr
1d2d3 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61  uption while sca
1d2d4 6e 6e 69 6e 67 0d 0a 20 20 2a 2a 20 74 68 65 20  nning..  ** the 
1d2d5 66 72 65 65 6c 69 73 74 2e 0d 0a 20 20 2a 2f 0d  freelist...  */.
1d2d6 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
1d2d7 68 64 72 4f 66 66 73 65 74 3b 0d 0a 20 20 61 64  hdrOffset;..  ad
1d2d8 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0d 0a 20  dr = hdr + 1;.. 
1d2d9 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65 2d 3e   iLast = pPage->
1d2da 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1d2db 2d 20 34 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  - 4;..  assert( 
1d2dc 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b 0d  start<=iLast );.
1d2dd 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
1d2de 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
1d2df 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74  ta[addr]))<start
1d2e0 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0d   && pbegin>0 ){.
1d2e1 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c  .    if( pbegin<
1d2e2 61 64 64 72 2b 34 20 29 7b 0d 0a 20 20 20 20 20  addr+4 ){..     
1d2e3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1d2e4 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1d2e5 20 20 7d 0d 0a 20 20 20 20 61 64 64 72 20 3d 20    }..    addr = 
1d2e6 70 62 65 67 69 6e 3b 0d 0a 20 20 7d 0d 0a 20 20  pbegin;..  }..  
1d2e7 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73 74  if( pbegin>iLast
1d2e8 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
1d2e9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1d2ea 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73  KPT;..  }..  ass
1d2eb 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72  ert( pbegin>addr
1d2ec 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b   || pbegin==0 );
1d2ed 0d 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ..  put2byte(&da
1d2ee 74 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29  ta[addr], start)
1d2ef 3b 0d 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  ;..  put2byte(&d
1d2f0 61 74 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67  ata[start], pbeg
1d2f1 69 6e 29 3b 0d 0a 20 20 70 75 74 32 62 79 74 65  in);..  put2byte
1d2f2 28 26 64 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c  (&data[start+2],
1d2f3 20 73 69 7a 65 29 3b 0d 0a 20 20 70 50 61 67 65   size);..  pPage
1d2f4 2d 3e 6e 46 72 65 65 20 3d 20 70 50 61 67 65 2d  ->nFree = pPage-
1d2f5 3e 6e 46 72 65 65 20 2b 20 28 75 31 36 29 73 69  >nFree + (u16)si
1d2f6 7a 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 61 6c  ze;....  /* Coal
1d2f7 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72  esce adjacent fr
1d2f8 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0d 0a 20 20  ee blocks */..  
1d2f9 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0d  addr = hdr + 1;.
1d2fa 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
1d2fb 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
1d2fc 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0d  ta[addr]))>0 ){.
1d2fd 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20  .    int pnext, 
1d2fe 70 73 69 7a 65 2c 20 78 3b 0d 0a 20 20 20 20 61  psize, x;..    a
1d2ff 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64  ssert( pbegin>ad
1d300 64 72 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  dr );..    asser
1d301 74 28 20 70 62 65 67 69 6e 20 3c 3d 20 28 69 6e  t( pbegin <= (in
1d302 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  t)pPage->pBt->us
1d303 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0d 0a 20  ableSize-4 );.. 
1d304 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62     pnext = get2b
1d305 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
1d306 5d 29 3b 0d 0a 20 20 20 20 70 73 69 7a 65 20 3d  ]);..    psize =
1d307 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
1d308 70 62 65 67 69 6e 2b 32 5d 29 3b 0d 0a 20 20 20  pbegin+2]);..   
1d309 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73   if( pbegin + ps
1d30a 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74  ize + 3 >= pnext
1d30b 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0d 0a   && pnext>0 ){..
1d30c 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d        int frag =
1d30d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e   pnext - (pbegin
1d30e 2b 70 73 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20  +psize);..      
1d30f 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20  if( (frag<0) || 
1d310 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b  (frag>(int)data[
1d311 68 64 72 2b 37 5d 29 20 29 7b 0d 0a 20 20 20 20  hdr+7]) ){..    
1d312 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1d313 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d  E_CORRUPT_BKPT;.
1d314 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1d315 64 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28  data[hdr+7] -= (
1d316 75 38 29 66 72 61 67 3b 0d 0a 20 20 20 20 20 20  u8)frag;..      
1d317 78 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  x = get2byte(&da
1d318 74 61 5b 70 6e 65 78 74 5d 29 3b 0d 0a 20 20 20  ta[pnext]);..   
1d319 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
1d31a 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b 0d 0a  a[pbegin], x);..
1d31b 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 20        x = pnext 
1d31c 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  + get2byte(&data
1d31d 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 65  [pnext+2]) - pbe
1d31e 67 69 6e 3b 0d 0a 20 20 20 20 20 20 70 75 74 32  gin;..      put2
1d31f 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
1d320 6e 2b 32 5d 2c 20 78 29 3b 0d 0a 20 20 20 20 7d  n+2], x);..    }
1d321 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 64 64  else{..      add
1d322 72 20 3d 20 70 62 65 67 69 6e 3b 0d 0a 20 20 20  r = pbegin;..   
1d323 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20   }..  }....  /* 
1d324 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  If the cell cont
1d325 65 6e 74 20 61 72 65 61 20 62 65 67 69 6e 73 20  ent area begins 
1d326 77 69 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b  with a freeblock
1d327 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0d  , remove it. */.
1d328 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 72 2b  .  if( data[hdr+
1d329 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20  1]==data[hdr+5] 
1d32a 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d  && data[hdr+2]==
1d32b 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b 0d 0a  data[hdr+6] ){..
1d32c 20 20 20 20 69 6e 74 20 74 6f 70 3b 0d 0a 20 20      int top;..  
1d32d 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62    pbegin = get2b
1d32e 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
1d32f 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  );..    memcpy(&
1d330 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61  data[hdr+1], &da
1d331 74 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0d  ta[pbegin], 2);.
1d332 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
1d333 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
1d334 29 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61  ) + get2byte(&da
1d335 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0d 0a  ta[pbegin+2]);..
1d336 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
1d337 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b  ta[hdr+5], top);
1d338 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
1d339 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
1d33a 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
1d33b 70 44 62 50 61 67 65 29 20 29 3b 0d 0a 20 20 72  pDbPage) );..  r
1d33c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1d33d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65  ..}..../*..** De
1d33e 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 62  code the flags b
1d33f 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20 62  yte (the first b
1d340 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65  yte of the heade
1d341 72 29 20 66 6f 72 20 61 20 70 61 67 65 0d 0a 2a  r) for a page..*
1d342 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  * and initialize
1d343 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d   fields of the M
1d344 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65  emPage structure
1d345 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0d 0a 2a   accordingly...*
1d346 2a 0d 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66  *..** Only the f
1d347 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61  ollowing combina
1d348 74 69 6f 6e 73 20 61 72 65 20 73 75 70 70 6f 72  tions are suppor
1d349 74 65 64 2e 20 20 41 6e 79 74 68 69 6e 67 20 64  ted.  Anything d
1d34a 69 66 66 65 72 65 6e 74 0d 0a 2a 2a 20 69 6e 64  ifferent..** ind
1d34b 69 63 61 74 65 73 20 61 20 63 6f 72 72 75 70 74  icates a corrupt
1d34c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 3a   database files:
1d34d 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ..**..**        
1d34e 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0d 0a 2a   PTF_ZERODATA..*
1d34f 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45  *         PTF_ZE
1d350 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41  RODATA | PTF_LEA
1d351 46 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54  F..**         PT
1d352 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46  F_LEAFDATA | PTF
1d353 5f 49 4e 54 4b 45 59 0d 0a 2a 2a 20 20 20 20 20  _INTKEY..**     
1d354 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41      PTF_LEAFDATA
1d355 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20   | PTF_INTKEY | 
1d356 50 54 46 5f 4c 45 41 46 0d 0a 2a 2f 0d 0a 73 74  PTF_LEAF..*/..st
1d357 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65 46  atic int decodeF
1d358 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70 50  lags(MemPage *pP
1d359 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79 74  age, int flagByt
1d35a 65 29 7b 0d 0a 20 20 42 74 53 68 61 72 65 64 20  e){..  BtShared 
1d35b 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63  *pBt;     /* A c
1d35c 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42  opy of pPage->pB
1d35d 74 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  t */....  assert
1d35e 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  ( pPage->hdrOffs
1d35f 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f  et==(pPage->pgno
1d360 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29  ==1 ? 100 : 0) )
1d361 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1d362 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1d363 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1d364 78 29 20 29 3b 0d 0a 20 20 70 50 61 67 65 2d 3e  x) );..  pPage->
1d365 6c 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67  leaf = (u8)(flag
1d366 42 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72  Byte>>3);  asser
1d367 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31  t( PTF_LEAF == 1
1d368 3c 3c 33 20 29 3b 0d 0a 20 20 66 6c 61 67 42 79  <<3 );..  flagBy
1d369 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b  te &= ~PTF_LEAF;
1d36a 0d 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64  ..  pPage->child
1d36b 50 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50  PtrSize = 4-4*pP
1d36c 61 67 65 2d 3e 6c 65 61 66 3b 0d 0a 20 20 70 42  age->leaf;..  pB
1d36d 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0d  t = pPage->pBt;.
1d36e 0a 20 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d  .  if( flagByte=
1d36f 3d 28 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c  =(PTF_LEAFDATA |
1d370 20 50 54 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0d   PTF_INTKEY) ){.
1d371 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b  .    pPage->intK
1d372 65 79 20 3d 20 31 3b 0d 0a 20 20 20 20 70 50 61  ey = 1;..    pPa
1d373 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 70 50  ge->hasData = pP
1d374 61 67 65 2d 3e 6c 65 61 66 3b 0d 0a 20 20 20 20  age->leaf;..    
1d375 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
1d376 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0d  = pBt->maxLeaf;.
1d377 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  .    pPage->minL
1d378 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  ocal = pBt->minL
1d379 65 61 66 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  eaf;..  }else if
1d37a 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f  ( flagByte==PTF_
1d37b 5a 45 52 4f 44 41 54 41 20 29 7b 0d 0a 20 20 20  ZERODATA ){..   
1d37c 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
1d37d 20 30 3b 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e   0;..    pPage->
1d37e 68 61 73 44 61 74 61 20 3d 20 30 3b 0d 0a 20 20  hasData = 0;..  
1d37f 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61    pPage->maxLoca
1d380 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61  l = pBt->maxLoca
1d381 6c 3b 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d  l;..    pPage->m
1d382 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d  inLocal = pBt->m
1d383 69 6e 4c 6f 63 61 6c 3b 0d 0a 20 20 7d 65 6c 73  inLocal;..  }els
1d384 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e{..    return S
1d385 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1d386 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50 61 67  PT;..  }..  pPag
1d387 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f  e->max1bytePaylo
1d388 61 64 20 3d 20 70 42 74 2d 3e 6d 61 78 31 62 79  ad = pBt->max1by
1d389 74 65 50 61 79 6c 6f 61 64 3b 0d 0a 20 20 72 65  tePayload;..  re
1d38a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
1d38b 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69  .}..../*..** Ini
1d38c 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 69  tialize the auxi
1d38d 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f  liary informatio
1d38e 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c 6f  n for a disk blo
1d38f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75  ck...**..** Retu
1d390 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
1d391 73 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 20  success.  If we 
1d392 73 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67  see that the pag
1d393 65 20 64 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20 63  e does..** not c
1d394 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f  ontain a well-fo
1d395 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 70 61  rmed database pa
1d396 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ge, then return 
1d397 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52  ..** SQLITE_CORR
1d398 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  UPT.  Note that 
1d399 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c 49  a return of SQLI
1d39a 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0d 0a  TE_OK does not..
1d39b 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  ** guarantee tha
1d39c 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 65  t the page is we
1d39d 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f  ll-formed.  It o
1d39e 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0d 0a  nly shows that..
1d39f 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  ** we failed to 
1d3a0 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75  detect any corru
1d3a1 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ption...*/..stat
1d3a2 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74  ic int btreeInit
1d3a3 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
1d3a4 61 67 65 29 7b 0d 0a 0d 0a 20 20 61 73 73 65 72  age){....  asser
1d3a5 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30  t( pPage->pBt!=0
1d3a6 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
1d3a7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1d3a8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
1d3a9 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
1d3aa 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d  t( pPage->pgno==
1d3ab 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
1d3ac 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44  number(pPage->pD
1d3ad 62 50 61 67 65 29 20 29 3b 0d 0a 20 20 61 73 73  bPage) );..  ass
1d3ae 65 72 74 28 20 70 50 61 67 65 20 3d 3d 20 73 71  ert( pPage == sq
1d3af 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
1d3b0 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ra(pPage->pDbPag
1d3b1 65 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  e) );..  assert(
1d3b2 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 3d   pPage->aData ==
1d3b3 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1d3b4 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
1d3b5 61 67 65 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  age) );....  if(
1d3b6 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20   !pPage->isInit 
1d3b7 29 7b 0d 0a 20 20 20 20 75 31 36 20 70 63 3b 20  ){..    u16 pc; 
1d3b8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
1d3b9 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62  dress of a freeb
1d3ba 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67  lock within pPag
1d3bb 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0d 0a 20  e->aData[] */.. 
1d3bc 20 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 20     u8 hdr;      
1d3bd 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1d3be 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  to beginning of 
1d3bf 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0d 0a  page header */..
1d3c0 20 20 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20      u8 *data;   
1d3c1 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20         /* Equal 
1d3c2 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  to pPage->aData 
1d3c3 2a 2f 0d 0a 20 20 20 20 42 74 53 68 61 72 65 64  */..    BtShared
1d3c4 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a   *pBt;        /*
1d3c5 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20   The main btree 
1d3c6 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20  structure */..  
1d3c7 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65    int usableSize
1d3c8 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f  ;    /* Amount o
1d3c9 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f  f usable space o
1d3ca 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0d 0a  n each page */..
1d3cb 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73      u16 cellOffs
1d3cc 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
1d3cd 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70   from start of p
1d3ce 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c  age to first cel
1d3cf 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20 20  l pointer */..  
1d3d0 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20    int nFree;    
1d3d1 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d3d2 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f  f unused bytes o
1d3d3 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0d 0a 20  n the page */.. 
1d3d4 20 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20     int top;     
1d3d5 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
1d3d6 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  yte of the cell 
1d3d7 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0d  content area */.
1d3d8 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69  .    int iCellFi
1d3d9 72 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74  rst;    /* First
1d3da 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20   allowable cell 
1d3db 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66  or freeblock off
1d3dc 73 65 74 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  set */..    int 
1d3dd 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f  iCellLast;     /
1d3de 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20  * Last possible 
1d3df 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63  cell or freebloc
1d3e0 6b 20 6f 66 66 73 65 74 20 2a 2f 0d 0a 0d 0a 20  k offset */.... 
1d3e1 20 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e     pBt = pPage->
1d3e2 70 42 74 3b 0d 0a 0d 0a 20 20 20 20 68 64 72 20  pBt;....    hdr 
1d3e3 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
1d3e4 65 74 3b 0d 0a 20 20 20 20 64 61 74 61 20 3d 20  et;..    data = 
1d3e5 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0d 0a 20  pPage->aData;.. 
1d3e6 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61     if( decodeFla
1d3e7 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68  gs(pPage, data[h
1d3e8 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51  dr]) ) return SQ
1d3e9 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1d3ea 54 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  T;..    assert( 
1d3eb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35  pBt->pageSize>=5
1d3ec 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53  12 && pBt->pageS
1d3ed 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0d 0a 20  ize<=65536 );.. 
1d3ee 20 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61     pPage->maskPa
1d3ef 67 65 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e  ge = (u16)(pBt->
1d3f0 70 61 67 65 53 69 7a 65 20 2d 20 31 29 3b 0d 0a  pageSize - 1);..
1d3f1 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72      pPage->nOver
1d3f2 66 6c 6f 77 20 3d 20 30 3b 0d 0a 20 20 20 20 75  flow = 0;..    u
1d3f3 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d  sableSize = pBt-
1d3f4 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0d 0a 20 20  >usableSize;..  
1d3f5 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
1d3f6 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  set = cellOffset
1d3f7 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a   = hdr + 12 - 4*
1d3f8 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0d 0a 20 20  pPage->leaf;..  
1d3f9 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e    pPage->aDataEn
1d3fa 64 20 3d 20 26 64 61 74 61 5b 75 73 61 62 6c 65  d = &data[usable
1d3fb 53 69 7a 65 5d 3b 0d 0a 20 20 20 20 70 50 61 67  Size];..    pPag
1d3fc 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64  e->aCellIdx = &d
1d3fd 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b  ata[cellOffset];
1d3fe 0d 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32  ..    top = get2
1d3ff 62 79 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74  byteNotZero(&dat
1d400 61 5b 68 64 72 2b 35 5d 29 3b 0d 0a 20 20 20 20  a[hdr+5]);..    
1d401 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67  pPage->nCell = g
1d402 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
1d403 72 2b 33 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20  r+3]);..    if( 
1d404 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f  pPage->nCell>MX_
1d405 43 45 4c 4c 28 70 42 74 29 20 29 7b 0d 0a 20 20  CELL(pBt) ){..  
1d406 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63      /* To many c
1d407 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c  ells for a singl
1d408 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67  e page.  The pag
1d409 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70  e must be corrup
1d40a 74 20 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75  t */..      retu
1d40b 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1d40c 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a  T_BKPT;..    }..
1d40d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50      testcase( pP
1d40e 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f 43  age->nCell==MX_C
1d40f 45 4c 4c 28 70 42 74 29 20 29 3b 0d 0a 0d 0a 20  ELL(pBt) );.... 
1d410 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d 65     /* A malforme
1d411 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  d database page 
1d412 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20 74  might cause us t
1d413 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20  o read past the 
1d414 65 6e 64 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 70  end..    ** of p
1d415 61 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67  age when parsing
1d416 20 61 20 63 65 6c 6c 2e 20 20 0d 0a 20 20 20 20   a cell.  ..    
1d417 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 66  **..    ** The f
1d418 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f  ollowing block o
1d419 66 20 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61  f code checks ea
1d41a 72 6c 79 20 74 6f 20 73 65 65 20 69 66 20 61 20  rly to see if a 
1d41b 63 65 6c 6c 20 65 78 74 65 6e 64 73 0d 0a 20 20  cell extends..  
1d41c 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e    ** past the en
1d41d 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f 75 6e  d of a page boun
1d41e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 73 20  dary and causes 
1d41f 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 74  SQLITE_CORRUPT t
1d420 6f 20 62 65 20 0d 0a 20 20 20 20 2a 2a 20 72 65  o be ..    ** re
1d421 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f 65  turned if it doe
1d422 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  s...    */..    
1d423 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c  iCellFirst = cel
1d424 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
1d425 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 20 20 20 20 69  e->nCell;..    i
1d426 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c  CellLast = usabl
1d427 65 53 69 7a 65 20 2d 20 34 3b 0d 0a 23 69 66 20  eSize - 4;..#if 
1d428 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
1d429 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43  NABLE_OVERSIZE_C
1d42a 45 4c 4c 5f 43 48 45 43 4b 29 0d 0a 20 20 20 20  ELL_CHECK)..    
1d42b 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20  {..      int i; 
1d42c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1d42d 64 65 78 20 69 6e 74 6f 20 74 68 65 20 63 65 6c  dex into the cel
1d42e 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  l pointer array 
1d42f 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  */..      int sz
1d430 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
1d431 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f  ize of a cell */
1d432 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 21 70  ....      if( !p
1d433 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65  Page->leaf ) iCe
1d434 6c 6c 4c 61 73 74 2d 2d 3b 0d 0a 20 20 20 20 20  llLast--;..     
1d435 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
1d436 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0d  e->nCell; i++){.
1d437 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 65  .        pc = ge
1d438 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c  t2byte(&data[cel
1d439 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0d 0a  lOffset+i*2]);..
1d43a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1d43b 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74  ( pc==iCellFirst
1d43c 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 65 73   );..        tes
1d43d 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c  tcase( pc==iCell
1d43e 4c 61 73 74 20 29 3b 0d 0a 20 20 20 20 20 20 20  Last );..       
1d43f 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72   if( pc<iCellFir
1d440 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61  st || pc>iCellLa
1d441 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  st ){..         
1d442 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1d443 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1d444 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
1d445 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74   sz = cellSizePt
1d446 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
1d447 63 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 65  c]);..        te
1d448 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75  stcase( pc+sz==u
1d449 73 61 62 6c 65 53 69 7a 65 20 29 3b 0d 0a 20 20  sableSize );..  
1d44a 20 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e        if( pc+sz>
1d44b 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0d 0a 20  usableSize ){.. 
1d44c 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1d44d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1d44e 4b 50 54 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  KPT;..        }.
1d44f 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1d450 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
1d451 20 29 20 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0d   ) iCellLast++;.
1d452 0a 20 20 20 20 7d 20 20 0d 0a 23 65 6e 64 69 66  .    }  ..#endif
1d453 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75  ....    /* Compu
1d454 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65  te the total fre
1d455 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70  e space on the p
1d456 61 67 65 20 2a 2f 0d 0a 20 20 20 20 70 63 20 3d  age */..    pc =
1d457 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
1d458 68 64 72 2b 31 5d 29 3b 0d 0a 20 20 20 20 6e 46  hdr+1]);..    nF
1d459 72 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37  ree = data[hdr+7
1d45a 5d 20 2b 20 74 6f 70 3b 0d 0a 20 20 20 20 77 68  ] + top;..    wh
1d45b 69 6c 65 28 20 70 63 3e 30 20 29 7b 0d 0a 20 20  ile( pc>0 ){..  
1d45c 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20 73 69      u16 next, si
1d45d 7a 65 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  ze;..      if( p
1d45e 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20  c<iCellFirst || 
1d45f 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0d  pc>iCellLast ){.
1d460 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72  .        /* Star
1d461 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20  t of free block 
1d462 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
1d463 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  */..        retu
1d464 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1d465 54 5f 42 4b 50 54 3b 20 0d 0a 20 20 20 20 20 20  T_BKPT; ..      
1d466 7d 0d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20  }..      next = 
1d467 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
1d468 63 5d 29 3b 0d 0a 20 20 20 20 20 20 73 69 7a 65  c]);..      size
1d469 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
1d46a 61 5b 70 63 2b 32 5d 29 3b 0d 0a 20 20 20 20 20  a[pc+2]);..     
1d46b 20 69 66 28 20 28 6e 65 78 74 3e 30 20 26 26 20   if( (next>0 && 
1d46c 6e 65 78 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29  next<=pc+size+3)
1d46d 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62   || pc+size>usab
1d46e 6c 65 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20  leSize ){..     
1d46f 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b     /* Free block
1d470 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61 73 63  s must be in asc
1d471 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 41 6e  ending order. An
1d472 64 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20  d the last byte 
1d473 6f 66 0d 0a 09 2a 2a 20 74 68 65 20 66 72 65 65  of...** the free
1d474 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 6c 69 65 20  -block must lie 
1d475 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1d476 70 61 67 65 2e 20 20 2a 2f 0d 0a 20 20 20 20 20  page.  */..     
1d477 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1d478 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0d  _CORRUPT_BKPT; .
1d479 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1d47a 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b 20  nFree = nFree + 
1d47b 73 69 7a 65 3b 0d 0a 20 20 20 20 20 20 70 63 20  size;..      pc 
1d47c 3d 20 6e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a  = next;..    }..
1d47d 0d 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73  ..    /* At this
1d47e 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f   point, nFree co
1d47f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d 20 6f  ntains the sum o
1d480 66 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20  f the offset to 
1d481 74 68 65 20 73 74 61 72 74 0d 0a 20 20 20 20 2a  the start..    *
1d482 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f  * of the cell-co
1d483 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 20  ntent area plus 
1d484 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  the number of fr
1d485 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e 0d  ee bytes within.
1d486 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c  .    ** the cell
1d487 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49  -content area. I
1d488 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65  f this is greate
1d489 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c  r than the usabl
1d48a 65 2d 73 69 7a 65 0d 0a 20 20 20 20 2a 2a 20 6f  e-size..    ** o
1d48b 66 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e  f the page, then
1d48c 20 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62   the page must b
1d48d 65 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69  e corrupted. Thi
1d48e 73 20 63 68 65 63 6b 20 61 6c 73 6f 0d 0a 20 20  s check also..  
1d48f 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76    ** serves to v
1d490 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f  erify that the o
1d491 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61  ffset to the sta
1d492 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63  rt of the cell-c
1d493 6f 6e 74 65 6e 74 0d 0a 20 20 20 20 2a 2a 20 61  ontent..    ** a
1d494 72 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 74  rea, according t
1d495 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  o the page heade
1d496 72 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20 74  r, lies within t
1d497 68 65 20 70 61 67 65 2e 0d 0a 20 20 20 20 2a 2f  he page...    */
1d498 0d 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 3e  ..    if( nFree>
1d499 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0d 0a 20  usableSize ){.. 
1d49a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1d49b 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1d49c 20 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50   ..    }..    pP
1d49d 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31  age->nFree = (u1
1d49e 36 29 28 6e 46 72 65 65 20 2d 20 69 43 65 6c 6c  6)(nFree - iCell
1d49f 46 69 72 73 74 29 3b 0d 0a 20 20 20 20 70 50 61  First);..    pPa
1d4a0 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0d  ge->isInit = 1;.
1d4a1 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
1d4a2 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
1d4a3 2f 2a 0d 0a 2a 2a 20 53 65 74 20 75 70 20 61 20  /*..** Set up a 
1d4a4 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74  raw page so that
1d4a5 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61   it looks like a
1d4a6 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68   database page h
1d4a7 6f 6c 64 69 6e 67 0d 0a 2a 2a 20 6e 6f 20 65 6e  olding..** no en
1d4a8 74 72 69 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  tries...*/..stat
1d4a9 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65  ic void zeroPage
1d4aa 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1d4ab 20 69 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 20 20   int flags){..  
1d4ac 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64  unsigned char *d
1d4ad 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
1d4ae 74 61 3b 0d 0a 20 20 42 74 53 68 61 72 65 64 20  ta;..  BtShared 
1d4af 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
1d4b0 74 3b 0d 0a 20 20 75 38 20 68 64 72 20 3d 20 70  t;..  u8 hdr = p
1d4b1 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
1d4b2 0d 0a 20 20 75 31 36 20 66 69 72 73 74 3b 0d 0a  ..  u16 first;..
1d4b3 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1d4b4 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
1d4b5 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  er(pPage->pDbPag
1d4b6 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20  e)==pPage->pgno 
1d4b7 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
1d4b8 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
1d4b9 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ra(pPage->pDbPag
1d4ba 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61  e) == (void*)pPa
1d4bb 67 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ge );..  assert(
1d4bc 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1d4bd 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
1d4be 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0d  age) == data );.
1d4bf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1d4c0 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1d4c1 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
1d4c2 65 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  e) );..  assert(
1d4c3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1d4c4 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
1d4c5 29 3b 0d 0a 20 20 69 66 28 20 70 42 74 2d 3e 62  );..  if( pBt->b
1d4c6 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 53 45  tsFlags & BTS_SE
1d4c7 43 55 52 45 5f 44 45 4c 45 54 45 20 29 7b 0d 0a  CURE_DELETE ){..
1d4c8 20 20 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61      memset(&data
1d4c9 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75  [hdr], 0, pBt->u
1d4ca 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 29  sableSize - hdr)
1d4cb 3b 0d 0a 20 20 7d 0d 0a 20 20 64 61 74 61 5b 68  ;..  }..  data[h
1d4cc 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67  dr] = (char)flag
1d4cd 73 3b 0d 0a 20 20 66 69 72 73 74 20 3d 20 68 64  s;..  first = hd
1d4ce 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67  r + 8 + 4*((flag
1d4cf 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f  s&PTF_LEAF)==0 ?
1d4d0 31 3a 30 29 3b 0d 0a 20 20 6d 65 6d 73 65 74 28  1:0);..  memset(
1d4d1 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c  &data[hdr+1], 0,
1d4d2 20 34 29 3b 0d 0a 20 20 64 61 74 61 5b 68 64 72   4);..  data[hdr
1d4d3 2b 37 5d 20 3d 20 30 3b 0d 0a 20 20 70 75 74 32  +7] = 0;..  put2
1d4d4 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1d4d5 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  ], pBt->usableSi
1d4d6 7a 65 29 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 6e  ze);..  pPage->n
1d4d7 46 72 65 65 20 3d 20 28 75 31 36 29 28 70 42 74  Free = (u16)(pBt
1d4d8 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 66  ->usableSize - f
1d4d9 69 72 73 74 29 3b 0d 0a 20 20 64 65 63 6f 64 65  irst);..  decode
1d4da 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c 61  Flags(pPage, fla
1d4db 67 73 29 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 68  gs);..  pPage->h
1d4dc 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0d  drOffset = hdr;.
1d4dd 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  .  pPage->cellOf
1d4de 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0d 0a 20  fset = first;.. 
1d4df 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64   pPage->aDataEnd
1d4e0 20 3d 20 26 64 61 74 61 5b 70 42 74 2d 3e 75 73   = &data[pBt->us
1d4e1 61 62 6c 65 53 69 7a 65 5d 3b 0d 0a 20 20 70 50  ableSize];..  pP
1d4e2 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20  age->aCellIdx = 
1d4e3 26 64 61 74 61 5b 66 69 72 73 74 5d 3b 0d 0a 20  &data[first];.. 
1d4e4 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
1d4e5 77 20 3d 20 30 3b 0d 0a 20 20 61 73 73 65 72 74  w = 0;..  assert
1d4e6 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e  ( pBt->pageSize>
1d4e7 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67  =512 && pBt->pag
1d4e8 65 53 69 7a 65 3c 3d 36 35 35 33 36 20 29 3b 0d  eSize<=65536 );.
1d4e9 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61  .  pPage->maskPa
1d4ea 67 65 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e  ge = (u16)(pBt->
1d4eb 70 61 67 65 53 69 7a 65 20 2d 20 31 29 3b 0d 0a  pageSize - 1);..
1d4ec 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
1d4ed 20 30 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 69 73   0;..  pPage->is
1d4ee 49 6e 69 74 20 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a  Init = 1;..}....
1d4ef 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74  ../*..** Convert
1d4f0 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e   a DbPage obtain
1d4f1 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ed from the page
1d4f2 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65  r into a MemPage
1d4f3 20 75 73 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65   used by..** the
1d4f4 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0d 0a 2a   btree layer...*
1d4f5 2f 0d 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67  /..static MemPag
1d4f6 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d  e *btreePageFrom
1d4f7 44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70  DbPage(DbPage *p
1d4f8 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e  DbPage, Pgno pgn
1d4f9 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  o, BtShared *pBt
1d4fa 29 7b 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ){..  MemPage *p
1d4fb 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a  Page = (MemPage*
1d4fc 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
1d4fd 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b 0d  Extra(pDbPage);.
1d4fe 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  .  pPage->aData 
1d4ff 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1d500 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0d  tData(pDbPage);.
1d501 0a 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67  .  pPage->pDbPag
1d502 65 20 3d 20 70 44 62 50 61 67 65 3b 0d 0a 20 20  e = pDbPage;..  
1d503 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74  pPage->pBt = pBt
1d504 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ;..  pPage->pgno
1d505 20 3d 20 70 67 6e 6f 3b 0d 0a 20 20 70 50 61 67   = pgno;..  pPag
1d506 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 70  e->hdrOffset = p
1d507 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20  Page->pgno==1 ? 
1d508 31 30 30 20 3a 20 30 3b 0d 0a 20 20 72 65 74 75  100 : 0;..  retu
1d509 72 6e 20 70 50 61 67 65 3b 20 0d 0a 7d 0d 0a 0d  rn pPage; ..}...
1d50a 0a 2f 2a 0d 0a 2a 2a 20 47 65 74 20 61 20 70 61  ./*..** Get a pa
1d50b 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
1d50c 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  r.  Initialize t
1d50d 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61  he MemPage.pBt a
1d50e 6e 64 0d 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61  nd..** MemPage.a
1d50f 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66  Data elements if
1d510 20 6e 65 65 64 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a   needed...**..**
1d511 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e   If the noConten
1d512 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69  t flag is set, i
1d513 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  t means that we 
1d514 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  do not care abou
1d515 74 0d 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  t..** the conten
1d516 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  t of the page at
1d517 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20   this time.  So 
1d518 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65  do not go to the
1d519 20 64 69 73 6b 0d 0a 2a 2a 20 74 6f 20 66 65 74   disk..** to fet
1d51a 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20  ch the content. 
1d51b 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68   Just fill in th
1d51c 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a  e content with z
1d51d 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0d 0a 2a  eros for now...*
1d51e 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 75  * If in the futu
1d51f 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74  re we call sqlit
1d520 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f  e3PagerWrite() o
1d521 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 61  n this page, tha
1d522 74 0d 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68  t..** means we h
1d523 61 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62  ave started to b
1d524 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75  e concerned abou
1d525 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68  t content and th
1d526 65 20 64 69 73 6b 0d 0a 2a 2a 20 72 65 61 64 20  e disk..** read 
1d527 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20  should occur at 
1d528 74 68 61 74 20 70 6f 69 6e 74 2e 0d 0a 2a 2f 0d  that point...*/.
1d529 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
1d52a 65 47 65 74 50 61 67 65 28 0d 0a 20 20 42 74 53  eGetPage(..  BtS
1d52b 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
1d52c 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a    /* The btree *
1d52d 2f 0d 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  /..  Pgno pgno, 
1d52e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1d52f 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20  ber of the page 
1d530 74 6f 20 66 65 74 63 68 20 2a 2f 0d 0a 20 20 4d  to fetch */..  M
1d531 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
1d532 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68      /* Return th
1d533 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 70  e page in this p
1d534 61 72 61 6d 65 74 65 72 20 2a 2f 0d 0a 20 20 69  arameter */..  i
1d535 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20  nt noContent    
1d536 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c 6f      /* Do not lo
1d537 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  ad page content 
1d538 69 66 20 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a  if true */..){..
1d539 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 44 62 50    int rc;..  DbP
1d53a 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0d 0a 0d  age *pDbPage;...
1d53b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1d53c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1d53d 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  t->mutex) );..  
1d53e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1d53f 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50  rAcquire(pBt->pP
1d540 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50  ager, pgno, (DbP
1d541 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20  age**)&pDbPage, 
1d542 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0d 0a 20 20 69  noContent);..  i
1d543 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1d544 63 3b 0d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20  c;..  *ppPage = 
1d545 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50  btreePageFromDbP
1d546 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e  age(pDbPage, pgn
1d547 6f 2c 20 70 42 74 29 3b 0d 0a 20 20 72 65 74 75  o, pBt);..  retu
1d548 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
1d549 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 72 69  ..../*..** Retri
1d54a 65 76 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20  eve a page from 
1d54b 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
1d54c 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65   If the requeste
1d54d 64 20 70 61 67 65 20 69 73 20 6e 6f 74 0d 0a 2a  d page is not..*
1d54e 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  * already in the
1d54f 20 70 61 67 65 72 20 63 61 63 68 65 20 72 65 74   pager cache ret
1d550 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e 69 74 69 61  urn NULL. Initia
1d551 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65  lize the MemPage
1d552 2e 70 42 74 20 61 6e 64 0d 0a 2a 2a 20 4d 65 6d  .pBt and..** Mem
1d553 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65  Page.aData eleme
1d554 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0d 0a  nts if needed...
1d555 2a 2f 0d 0a 73 74 61 74 69 63 20 4d 65 6d 50 61  */..static MemPa
1d556 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f 6f  ge *btreePageLoo
1d557 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 42  kup(BtShared *pB
1d558 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0d 0a  t, Pgno pgno){..
1d559 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
1d55a 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
1d55b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1d55c 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  (pBt->mutex) );.
1d55d 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c  .  pDbPage = sql
1d55e 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
1d55f 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e  pBt->pPager, pgn
1d560 6f 29 3b 0d 0a 20 20 69 66 28 20 70 44 62 50 61  o);..  if( pDbPa
1d561 67 65 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ge ){..    retur
1d562 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44  n btreePageFromD
1d563 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70  bPage(pDbPage, p
1d564 67 6e 6f 2c 20 70 42 74 29 3b 0d 0a 20 20 7d 0d  gno, pBt);..  }.
1d565 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d  .  return 0;..}.
1d566 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
1d567 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1d568 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1d569 6e 20 70 61 67 65 73 2e 20 49 66 20 74 68 65 72  n pages. If ther
1d56a 65 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66  e is any kind of
1d56b 0d 0a 2a 2a 20 65 72 72 6f 72 2c 20 72 65 74 75  ..** error, retu
1d56c 72 6e 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e  rn ((unsigned in
1d56d 74 29 2d 31 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  t)-1)...*/..stat
1d56e 69 63 20 50 67 6e 6f 20 62 74 72 65 65 50 61 67  ic Pgno btreePag
1d56f 65 63 6f 75 6e 74 28 42 74 53 68 61 72 65 64 20  ecount(BtShared 
1d570 2a 70 42 74 29 7b 0d 0a 20 20 72 65 74 75 72 6e  *pBt){..  return
1d571 20 70 42 74 2d 3e 6e 50 61 67 65 3b 0d 0a 7d 0d   pBt->nPage;..}.
1d572 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1d573 75 33 32 20 73 71 6c 69 74 65 33 42 74 72 65 65  u32 sqlite3Btree
1d574 4c 61 73 74 50 61 67 65 28 42 74 72 65 65 20 2a  LastPage(Btree *
1d575 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  p){..  assert( s
1d576 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1d577 4d 75 74 65 78 28 70 29 20 29 3b 0d 0a 20 20 61  Mutex(p) );..  a
1d578 73 73 65 72 74 28 20 28 28 70 2d 3e 70 42 74 2d  ssert( ((p->pBt-
1d579 3e 6e 50 61 67 65 29 26 30 78 38 30 30 30 30 30  >nPage)&0x800000
1d57a 30 29 3d 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75  0)==0 );..  retu
1d57b 72 6e 20 28 69 6e 74 29 62 74 72 65 65 50 61 67  rn (int)btreePag
1d57c 65 63 6f 75 6e 74 28 70 2d 3e 70 42 74 29 3b 0d  ecount(p->pBt);.
1d57d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 74  .}..../*..** Get
1d57e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
1d57f 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69   pager and initi
1d580 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20  alize it.  This 
1d581 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20  routine is just 
1d582 61 0d 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63  a..** convenienc
1d583 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  e wrapper around
1d584 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20   separate calls 
1d585 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28  to btreeGetPage(
1d586 29 20 61 6e 64 20 0d 0a 2a 2a 20 62 74 72 65 65  ) and ..** btree
1d587 49 6e 69 74 50 61 67 65 28 29 2e 0d 0a 2a 2a 0d  InitPage()...**.
1d588 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
1d589 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65  occurs, then the
1d58a 20 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20 69   value *ppPage i
1d58b 73 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64 65  s set to is unde
1d58c 66 69 6e 65 64 2e 20 49 74 0d 0a 2a 2a 20 6d 61  fined. It..** ma
1d58d 79 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67  y remain unchang
1d58e 65 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65  ed, or it may be
1d58f 20 73 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c   set to an inval
1d590 69 64 20 76 61 6c 75 65 2e 0d 0a 2a 2f 0d 0a 73  id value...*/..s
1d591 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 6e 64  tatic int getAnd
1d592 49 6e 69 74 50 61 67 65 28 0d 0a 20 20 42 74 53  InitPage(..  BtS
1d593 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
1d594 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1d595 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  base file */..  
1d596 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20  Pgno pgno,      
1d597 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d598 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 67 65  f the page to ge
1d599 74 20 2a 2f 0d 0a 20 20 4d 65 6d 50 61 67 65 20  t */..  MemPage 
1d59a 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f 2a 20  **ppPage     /* 
1d59b 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 70  Write the page p
1d59c 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f 0d 0a  ointer here */..
1d59d 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  ){..  int rc;.. 
1d59e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1d59f 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1d5a0 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 0d 0a 20 20  >mutex) );....  
1d5a1 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 65 50 61  if( pgno>btreePa
1d5a2 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0d  gecount(pBt) ){.
1d5a3 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1d5a4 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a  _CORRUPT_BKPT;..
1d5a5 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63    }else{..    rc
1d5a6 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1d5a7 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67  pBt, pgno, ppPag
1d5a8 65 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20  e, 0);..    if( 
1d5a9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1d5aa 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72  ..      rc = btr
1d5ab 65 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61  eeInitPage(*ppPa
1d5ac 67 65 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ge);..      if( 
1d5ad 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1d5ae 0d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ..        releas
1d5af 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0d  ePage(*ppPage);.
1d5b0 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
1d5b1 0a 20 20 7d 0d 0a 0d 0a 20 20 74 65 73 74 63 61  .  }....  testca
1d5b2 73 65 28 20 70 67 6e 6f 3d 3d 30 20 29 3b 0d 0a  se( pgno==0 );..
1d5b3 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d    assert( pgno!=
1d5b4 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  0 || rc==SQLITE_
1d5b5 43 4f 52 52 55 50 54 20 29 3b 0d 0a 20 20 72 65  CORRUPT );..  re
1d5b6 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
1d5b7 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  *..** Release a 
1d5b8 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73  MemPage.  This s
1d5b9 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
1d5ba 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 72  once for each pr
1d5bb 69 6f 72 0d 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  ior..** call to 
1d5bc 62 74 72 65 65 47 65 74 50 61 67 65 2e 0d 0a 2a  btreeGetPage...*
1d5bd 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  /..static void r
1d5be 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61  eleasePage(MemPa
1d5bf 67 65 20 2a 70 50 61 67 65 29 7b 0d 0a 20 20 69  ge *pPage){..  i
1d5c0 66 28 20 70 50 61 67 65 20 29 7b 0d 0a 20 20 20  f( pPage ){..   
1d5c1 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1d5c2 61 44 61 74 61 20 29 3b 0d 0a 20 20 20 20 61 73  aData );..    as
1d5c3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
1d5c4 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
1d5c5 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1d5c6 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62  Extra(pPage->pDb
1d5c7 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29  Page) == (void*)
1d5c8 70 50 61 67 65 20 29 3b 0d 0a 20 20 20 20 61 73  pPage );..    as
1d5c9 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1d5ca 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d  erGetData(pPage-
1d5cb 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65  >pDbPage)==pPage
1d5cc 2d 3e 61 44 61 74 61 20 29 3b 0d 0a 20 20 20 20  ->aData );..    
1d5cd 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1d5ce 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1d5cf 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1d5d0 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ..    sqlite3Pag
1d5d1 65 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70  erUnref(pPage->p
1d5d2 44 62 50 61 67 65 29 3b 0d 0a 20 20 7d 0d 0a 7d  DbPage);..  }..}
1d5d3 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 75 72 69 6e  ..../*..** Durin
1d5d4 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68  g a rollback, wh
1d5d5 65 6e 20 74 68 65 20 70 61 67 65 72 20 72 65 6c  en the pager rel
1d5d6 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  oads information
1d5d7 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0d   into the cache.
1d5d8 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20  .** so that the 
1d5d9 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72 65  cache is restore
1d5da 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  d to its origina
1d5db 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20 73  l state at the s
1d5dc 74 61 72 74 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  tart of..** the 
1d5dd 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72  transaction, for
1d5de 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f   each page resto
1d5df 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  red this routine
1d5e0 20 69 73 20 63 61 6c 6c 65 64 2e 0d 0a 2a 2a 0d   is called...**.
1d5e1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1d5e2 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20   needs to reset 
1d5e3 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73  the extra data s
1d5e4 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e  ection at the en
1d5e5 64 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 70 61 67  d of the..** pag
1d5e6 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20  e to agree with 
1d5e7 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74  the restored dat
1d5e8 61 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  a...*/..static v
1d5e9 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44  oid pageReinit(D
1d5ea 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0d 0a  bPage *pData){..
1d5eb 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1d5ec 3b 0d 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65  ;..  pPage = (Me
1d5ed 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50  mPage *)sqlite3P
1d5ee 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 61  agerGetExtra(pDa
1d5ef 74 61 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ta);..  assert( 
1d5f0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
1d5f1 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e  Refcount(pData)>
1d5f2 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 50 61 67  0 );..  if( pPag
1d5f3 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0d 0a 20 20  e->isInit ){..  
1d5f4 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1d5f5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1d5f6 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
1d5f7 29 3b 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e 69  );..    pPage->i
1d5f8 73 49 6e 69 74 20 3d 20 30 3b 0d 0a 20 20 20 20  sInit = 0;..    
1d5f9 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  if( sqlite3Pager
1d5fa 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61  PageRefcount(pDa
1d5fb 74 61 29 3e 31 20 29 7b 0d 0a 20 20 20 20 20 20  ta)>1 ){..      
1d5fc 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74 20 6e  /* pPage might n
1d5fd 6f 74 20 62 65 20 61 20 62 74 72 65 65 20 70 61  ot be a btree pa
1d5fe 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20 62 65  ge;  it might be
1d5ff 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
1d600 65 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 70  e..      ** or p
1d601 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61 20  trmap page or a 
1d602 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20 74  free page.  In t
1d603 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65 20  hose cases, the 
1d604 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 20 20 20 20 20  following..     
1d605 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65   ** call to btre
1d606 65 49 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c  eInitPage() will
1d607 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53   likely return S
1d608 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 0d 0a  QLITE_CORRUPT...
1d609 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20        ** But no 
1d60a 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 20  harm is done by 
1d60b 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69 73  this.  And it is
1d60c 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 20   very important 
1d60d 74 68 61 74 0d 0a 20 20 20 20 20 20 2a 2a 20 62  that..      ** b
1d60e 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62  treeInitPage() b
1d60f 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72  e called on ever
1d610 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20  y btree page so 
1d611 77 65 20 6d 61 6b 65 0d 0a 20 20 20 20 20 20 2a  we make..      *
1d612 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65  * the call for e
1d613 76 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63  very page that c
1d614 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69  omes in for re-i
1d615 6e 69 74 69 6e 67 2e 20 2a 2f 0d 0a 20 20 20 20  niting. */..    
1d616 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28    btreeInitPage(
1d617 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a  pPage);..    }..
1d618 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
1d619 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   Invoke the busy
1d61a 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62   handler for a b
1d61b 74 72 65 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  tree...*/..stati
1d61c 63 20 69 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b  c int btreeInvok
1d61d 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69  eBusyHandler(voi
1d61e 64 20 2a 70 41 72 67 29 7b 0d 0a 20 20 42 74 53  d *pArg){..  BtS
1d61f 68 61 72 65 64 20 2a 70 42 74 20 3d 20 28 42 74  hared *pBt = (Bt
1d620 53 68 61 72 65 64 2a 29 70 41 72 67 3b 0d 0a 20  Shared*)pArg;.. 
1d621 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 64 62   assert( pBt->db
1d622 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
1d623 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1d624 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 78  d(pBt->db->mutex
1d625 29 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 73  ) );..  return s
1d626 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79  qlite3InvokeBusy
1d627 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62  Handler(&pBt->db
1d628 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0d  ->busyHandler);.
1d629 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65  .}..../*..** Ope
1d62a 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
1d62b 65 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 7a 46 69 6c  e...** ..** zFil
1d62c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  ename is the nam
1d62d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1d62e 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c  e file.  If zFil
1d62f 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0d 0a 2a  ename is NULL..*
1d630 2a 20 74 68 65 6e 20 61 6e 20 65 70 68 65 6d 65  * then an epheme
1d631 72 61 6c 20 64 61 74 61 62 61 73 65 20 69 73 20  ral database is 
1d632 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 65 70  created.  The ep
1d633 68 65 6d 65 72 61 6c 20 64 61 74 61 62 61 73 65  hemeral database
1d634 20 6d 69 67 68 74 0d 0a 2a 2a 20 62 65 20 65 78   might..** be ex
1d635 63 6c 75 73 69 76 65 6c 79 20 69 6e 20 6d 65 6d  clusively in mem
1d636 6f 72 79 2c 20 6f 72 20 69 74 20 6d 69 67 68 74  ory, or it might
1d637 20 75 73 65 20 61 20 64 69 73 6b 2d 62 61 73 65   use a disk-base
1d638 64 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 0d  d memory cache..
1d639 0a 2a 2a 20 45 69 74 68 65 72 20 77 61 79 2c 20  .** Either way, 
1d63a 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64 61  the ephemeral da
1d63b 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 61  tabase will be a
1d63c 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c  utomatically del
1d63d 65 74 65 64 20 0d 0a 2a 2a 20 77 68 65 6e 20 73  eted ..** when s
1d63e 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
1d63f 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0d 0a 2a  () is called...*
1d640 2a 0d 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61  *..** If zFilena
1d641 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22  me is ":memory:"
1d642 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f   then an in-memo
1d643 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 63  ry database is c
1d644 72 65 61 74 65 64 0d 0a 2a 2a 20 74 68 61 74 20  reated..** that 
1d645 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
1d646 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20   destroyed when 
1d647 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0d 0a 2a  it is closed...*
1d648 2a 0d 0a 2a 2a 20 54 68 65 20 22 66 6c 61 67 73  *..** The "flags
1d649 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  " parameter is a
1d64a 20 62 69 74 6d 61 73 6b 20 74 68 61 74 20 6d 69   bitmask that mi
1d64b 67 68 74 20 63 6f 6e 74 61 69 6e 20 62 69 74 73  ght contain bits
1d64c 0d 0a 2a 2a 20 42 54 52 45 45 5f 4f 4d 49 54 5f  ..** BTREE_OMIT_
1d64d 4a 4f 55 52 4e 41 4c 20 61 6e 64 2f 6f 72 20 42  JOURNAL and/or B
1d64e 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  TREE_NO_READLOCK
1d64f 2e 20 20 54 68 65 20 42 54 52 45 45 5f 4e 4f 5f  .  The BTREE_NO_
1d650 52 45 41 44 4c 4f 43 4b 0d 0a 2a 2a 20 62 69 74  READLOCK..** bit
1d651 20 69 73 20 61 6c 73 6f 20 73 65 74 20 69 66 20   is also set if 
1d652 74 68 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61  the SQLITE_NoRea
1d653 64 6c 6f 63 6b 20 66 6c 61 67 73 20 69 73 20 73  dlock flags is s
1d654 65 74 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73 2e  et in db->flags.
1d655 0d 0a 2a 2a 20 54 68 65 73 65 20 66 6c 61 67 73  ..** These flags
1d656 20 61 72 65 20 70 61 73 73 65 64 20 74 68 72 6f   are passed thro
1d657 75 67 68 20 69 6e 74 6f 20 73 71 6c 69 74 65 33  ugh into sqlite3
1d658 50 61 67 65 72 4f 70 65 6e 28 29 20 61 6e 64 20  PagerOpen() and 
1d659 6d 75 73 74 0d 0a 2a 2a 20 62 65 20 74 68 65 20  must..** be the 
1d65a 73 61 6d 65 20 76 61 6c 75 65 73 20 61 73 20 50  same values as P
1d65b 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
1d65c 4c 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52  L and PAGER_NO_R
1d65d 45 41 44 4c 4f 43 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a  EADLOCK...**..**
1d65e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
1d65f 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e   is already open
1d660 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64  ed in the same d
1d661 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d662 6f 6e 0d 0a 2a 2a 20 61 6e 64 20 77 65 20 61 72  on..** and we ar
1d663 65 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68  e in shared cach
1d664 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65  e mode, then the
1d665 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 69 6c 20   open will fail 
1d666 77 69 74 68 20 61 6e 0d 0a 2a 2a 20 53 51 4c 49  with an..** SQLI
1d667 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 65 72  TE_CONSTRAINT er
1d668 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f 74 20  ror.  We cannot 
1d669 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d 6f 72  allow two or mor
1d66a 65 20 42 74 53 68 61 72 65 64 0d 0a 2a 2a 20 6f  e BtShared..** o
1d66b 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 61  bjects in the sa
1d66c 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
1d66d 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f 69  ection since doi
1d66e 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 0d  ng so will lead.
1d66f 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20  .** to problems 
1d670 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0d 0a 2a  with locking...*
1d671 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
1d672 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1d673 65 65 4f 70 65 6e 28 0d 0a 20 20 73 71 6c 69 74  eeOpen(..  sqlit
1d674 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
1d675 20 20 20 2f 2a 20 56 46 53 20 74 6f 20 75 73 65     /* VFS to use
1d676 20 66 6f 72 20 74 68 69 73 20 62 2d 74 72 65 65   for this b-tree
1d677 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
1d678 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f  r *zFilename,  /
1d679 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
1d67a 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
1d67b 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73 65  e BTree database
1d67c 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a   */..  sqlite3 *
1d67d 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
1d67e 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74  * Associated dat
1d67f 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  abase handle */.
1d680 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72  .  Btree **ppBtr
1d681 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ee,        /* Po
1d682 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72  inter to new Btr
1d683 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65  ee object writte
1d684 6e 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74  n here */..  int
1d685 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1d686 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20       /* Options 
1d687 2a 2f 0d 0a 20 20 69 6e 74 20 76 66 73 46 6c 61  */..  int vfsFla
1d688 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs            /*
1d689 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68   Flags passed th
1d68a 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33  rough to sqlite3
1d68b 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0d  _vfs.xOpen() */.
1d68c 0a 29 7b 0d 0a 20 20 42 74 53 68 61 72 65 64 20  .){..  BtShared 
1d68d 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20  *pBt = 0;       
1d68e 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
1d68f 70 61 72 74 20 6f 66 20 62 74 72 65 65 20 73 74  part of btree st
1d690 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20 42 74  ructure */..  Bt
1d691 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20  ree *p;         
1d692 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d693 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 72 6e  Handle to return
1d694 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d   */..  sqlite3_m
1d695 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e 20  utex *mutexOpen 
1d696 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74  = 0;  /* Prevent
1d697 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69  s a race conditi
1d698 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 33 37  on. Ticket #3537
1d699 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20   */..  int rc = 
1d69a 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1d69b 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
1d69c 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 66  code from this f
1d69d 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 75 38  unction */..  u8
1d69e 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20   nReserve;      
1d69f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d6a0 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73  Byte of unused s
1d6a1 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67  pace on each pag
1d6a2 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64  e */..  unsigned
1d6a3 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b   char zDbHeader[
1d6a4 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61  100];  /* Databa
1d6a5 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e  se header conten
1d6a6 74 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 72 75  t */....  /* Tru
1d6a7 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20  e if opening an 
1d6a8 65 70 68 65 6d 65 72 61 6c 2c 20 74 65 6d 70 6f  ephemeral, tempo
1d6a9 72 61 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f  rary database */
1d6aa 0d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73  ..  const int is
1d6ab 54 65 6d 70 44 62 20 3d 20 7a 46 69 6c 65 6e 61  TempDb = zFilena
1d6ac 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61  me==0 || zFilena
1d6ad 6d 65 5b 30 5d 3d 3d 30 3b 0d 0a 0d 0a 20 20 2f  me[0]==0;....  /
1d6ae 2a 20 53 65 74 20 74 68 65 20 76 61 72 69 61 62  * Set the variab
1d6af 6c 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 74 72  le isMemdb to tr
1d6b0 75 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d  ue for an in-mem
1d6b1 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72  ory database, or
1d6b2 20 0d 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 6f   ..  ** false fo
1d6b3 72 20 61 20 66 69 6c 65 2d 62 61 73 65 64 20 64  r a file-based d
1d6b4 61 74 61 62 61 73 65 2e 0d 0a 20 20 2a 2f 0d 0a  atabase...  */..
1d6b5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1d6b6 49 54 5f 4d 45 4d 4f 52 59 44 42 0d 0a 20 20 63  IT_MEMORYDB..  c
1d6b7 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62  onst int isMemdb
1d6b8 20 3d 20 30 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20   = 0;..#else..  
1d6b9 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64  const int isMemd
1d6ba 62 20 3d 20 28 7a 46 69 6c 65 6e 61 6d 65 20 26  b = (zFilename &
1d6bb 26 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61  & strcmp(zFilena
1d6bc 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d  me, ":memory:")=
1d6bd 3d 30 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20  =0)..           
1d6be 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28              || (
1d6bf 69 73 54 65 6d 70 44 62 20 26 26 20 73 71 6c 69  isTempDb && sqli
1d6c0 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28  te3TempInMemory(
1d6c1 64 62 29 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  db));..#endif...
1d6c2 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
1d6c3 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
1d6c4 56 66 73 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  Vfs!=0 );..  ass
1d6c5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1d6c6 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
1d6c7 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  x) );..  assert(
1d6c8 20 28 66 6c 61 67 73 26 30 78 66 66 29 3d 3d 66   (flags&0xff)==f
1d6c9 6c 61 67 73 20 29 3b 20 20 20 2f 2a 20 66 6c 61  lags );   /* fla
1d6ca 67 73 20 66 69 74 20 69 6e 20 38 20 62 69 74 73  gs fit in 8 bits
1d6cb 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 4f 6e 6c 79   */....  /* Only
1d6cc 20 61 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 20   a BTREE_SINGLE 
1d6cd 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 20  database can be 
1d6ce 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 20  BTREE_UNORDERED 
1d6cf 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28 66  */..  assert( (f
1d6d0 6c 61 67 73 20 26 20 42 54 52 45 45 5f 55 4e 4f  lags & BTREE_UNO
1d6d1 52 44 45 52 45 44 29 3d 3d 30 20 7c 7c 20 28 66  RDERED)==0 || (f
1d6d2 6c 61 67 73 20 26 20 42 54 52 45 45 5f 53 49 4e  lags & BTREE_SIN
1d6d3 47 4c 45 29 21 3d 30 20 29 3b 0d 0a 0d 0a 20 20  GLE)!=0 );....  
1d6d4 2f 2a 20 41 20 42 54 52 45 45 5f 53 49 4e 47 4c  /* A BTREE_SINGL
1d6d5 45 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c  E database is al
1d6d6 77 61 79 73 20 61 20 74 65 6d 70 6f 72 61 72 79  ways a temporary
1d6d7 20 61 6e 64 2f 6f 72 20 65 70 68 65 6d 65 72 61   and/or ephemera
1d6d8 6c 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  l */..  assert( 
1d6d9 28 66 6c 61 67 73 20 26 20 42 54 52 45 45 5f 53  (flags & BTREE_S
1d6da 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20 69 73 54  INGLE)==0 || isT
1d6db 65 6d 70 44 62 20 29 3b 0d 0a 0d 0a 20 20 69 66  empDb );....  if
1d6dc 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
1d6dd 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20  LITE_NoReadlock 
1d6de 29 7b 0d 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d  ){..    flags |=
1d6df 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f   BTREE_NO_READLO
1d6e0 43 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  CK;..  }..  if( 
1d6e1 69 73 4d 65 6d 64 62 20 29 7b 0d 0a 20 20 20 20  isMemdb ){..    
1d6e2 66 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4d  flags |= BTREE_M
1d6e3 45 4d 4f 52 59 3b 0d 0a 20 20 7d 0d 0a 20 20 69  EMORY;..  }..  i
1d6e4 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20 53  f( (vfsFlags & S
1d6e5 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
1d6e6 44 42 29 21 3d 30 20 26 26 20 28 69 73 4d 65 6d  DB)!=0 && (isMem
1d6e7 64 62 20 7c 7c 20 69 73 54 65 6d 70 44 62 29 20  db || isTempDb) 
1d6e8 29 7b 0d 0a 20 20 20 20 76 66 73 46 6c 61 67 73  ){..    vfsFlags
1d6e9 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e   = (vfsFlags & ~
1d6ea 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
1d6eb 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50  _DB) | SQLITE_OP
1d6ec 45 4e 5f 54 45 4d 50 5f 44 42 3b 0d 0a 20 20 7d  EN_TEMP_DB;..  }
1d6ed 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d  ..  p = sqlite3M
1d6ee 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66  allocZero(sizeof
1d6ef 28 42 74 72 65 65 29 29 3b 0d 0a 20 20 69 66 28  (Btree));..  if(
1d6f0 20 21 70 20 29 7b 0d 0a 20 20 20 20 72 65 74 75   !p ){..    retu
1d6f1 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1d6f2 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e 69 6e 54 72  ..  }..  p->inTr
1d6f3 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45  ans = TRANS_NONE
1d6f4 3b 0d 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  ;..  p->db = db;
1d6f5 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1d6f6 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1d6f7 48 45 0d 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42  HE..  p->lock.pB
1d6f8 74 72 65 65 20 3d 20 70 3b 0d 0a 20 20 70 2d 3e  tree = p;..  p->
1d6f9 6c 6f 63 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b  lock.iTable = 1;
1d6fa 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 20  ..#endif....#if 
1d6fb 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1d6fc 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1d6fd 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
1d6fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
1d6ff 4f 29 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 49  O)..  /*..  ** I
1d700 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73 20  f this Btree is 
1d701 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20  a candidate for 
1d702 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 72  shared cache, tr
1d703 79 20 74 6f 20 66 69 6e 64 20 61 6e 0d 0a 20 20  y to find an..  
1d704 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68  ** existing BtSh
1d705 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74  ared object that
1d706 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69   we can share wi
1d707 74 68 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  th..  */..  if( 
1d708 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 69 73  isMemdb==0 && is
1d709 54 65 6d 70 44 62 3d 3d 30 20 29 7b 0d 0a 20 20  TempDb==0 ){..  
1d70a 20 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26    if( vfsFlags &
1d70b 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41   SQLITE_OPEN_SHA
1d70c 52 45 44 43 41 43 48 45 20 29 7b 0d 0a 20 20 20  REDCACHE ){..   
1d70d 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68     int nFullPath
1d70e 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50  name = pVfs->mxP
1d70f 61 74 68 6e 61 6d 65 2b 31 3b 0d 0a 20 20 20 20  athname+1;..    
1d710 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74    char *zFullPat
1d711 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  hname = sqlite3M
1d712 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e  alloc(nFullPathn
1d713 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 4d 55 54  ame);..      MUT
1d714 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65  EX_LOGIC( sqlite
1d715 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68  3_mutex *mutexSh
1d716 61 72 65 64 3b 20 29 0d 0a 20 20 20 20 20 20 70  ared; )..      p
1d717 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0d  ->sharable = 1;.
1d718 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c  .      if( !zFul
1d719 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0d 0a 20 20  lPathname ){..  
1d71a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1d71b 65 65 28 70 29 3b 0d 0a 20 20 20 20 20 20 20 20  ee(p);..        
1d71c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1d71d 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  MEM;..      }.. 
1d71e 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1d71f 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  3OsFullPathname(
1d720 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  pVfs, zFilename,
1d721 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20   nFullPathname, 
1d722 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0d  zFullPathname);.
1d723 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
1d724 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
1d725 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68  3_free(zFullPath
1d726 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  name);..        
1d727 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
1d728 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
1d729 20 72 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23   rc;..      }..#
1d72a 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
1d72b 53 41 46 45 0d 0a 20 20 20 20 20 20 6d 75 74 65  SAFE..      mute
1d72c 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d  xOpen = sqlite3M
1d72d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
1d72e 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50  _MUTEX_STATIC_OP
1d72f 45 4e 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  EN);..      sqli
1d730 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1d731 6d 75 74 65 78 4f 70 65 6e 29 3b 0d 0a 20 20 20  mutexOpen);..   
1d732 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d     mutexShared =
1d733 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
1d734 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
1d735 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0d  STATIC_MASTER);.
1d736 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1d737 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
1d738 53 68 61 72 65 64 29 3b 0d 0a 23 65 6e 64 69 66  Shared);..#endif
1d739 0d 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d  ..      for(pBt=
1d73a 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
1d73b 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
1d73c 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70  cheList); pBt; p
1d73d 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0d  Bt=pBt->pNext){.
1d73e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1d73f 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0d   pBt->nRef>0 );.
1d740 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
1d741 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68  strcmp(zFullPath
1d742 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67  name, sqlite3Pag
1d743 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e  erFilename(pBt->
1d744 70 50 61 67 65 72 29 29 0d 0a 20 20 20 20 20 20  pPager))..      
1d745 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71             && sq
1d746 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42  lite3PagerVfs(pB
1d747 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73  t->pPager)==pVfs
1d748 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69   ){..          i
1d749 6e 74 20 69 44 62 3b 0d 0a 20 20 20 20 20 20 20  nt iDb;..       
1d74a 20 20 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e     for(iDb=db->n
1d74b 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44  Db-1; iDb>=0; iD
1d74c 62 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  b--){..         
1d74d 20 20 20 42 74 72 65 65 20 2a 70 45 78 69 73 74     Btree *pExist
1d74e 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ing = db->aDb[iD
1d74f 62 5d 2e 70 42 74 3b 0d 0a 20 20 20 20 20 20 20  b].pBt;..       
1d750 20 20 20 20 20 69 66 28 20 70 45 78 69 73 74 69       if( pExisti
1d751 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e 67 2d  ng && pExisting-
1d752 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0d 0a 20 20  >pBt==pBt ){..  
1d753 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1d754 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1d755 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0d 0a 20  mutexShared);.. 
1d756 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1d757 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1d758 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0d 0a 20 20  (mutexOpen);..  
1d759 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1d75a 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61  te3_free(zFullPa
1d75b 74 68 6e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20  thname);..      
1d75c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1d75d 66 72 65 65 28 70 29 3b 0d 0a 20 20 20 20 20 20  free(p);..      
1d75e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1d75f 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1d760 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
1d761 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
1d762 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20           p->pBt 
1d763 3d 20 70 42 74 3b 0d 0a 20 20 20 20 20 20 20 20  = pBt;..        
1d764 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a    pBt->nRef++;..
1d765 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1d766 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
1d767 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
1d768 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1d769 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0d 0a 20  mutexShared);.. 
1d76a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
1d76b 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  e(zFullPathname)
1d76c 3b 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 64 65 66  ;..    }..#ifdef
1d76d 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
1d76e 20 20 20 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20     else{..      
1d76f 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65  /* In debug mode
1d770 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65  , we mark all pe
1d771 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 73  rsistent databas
1d772 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0d 0a  es as sharable..
1d773 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68        ** even wh
1d774 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e  en they are not.
1d775 20 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73    This exercises
1d776 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64   the locking cod
1d777 65 20 61 6e 64 0d 0a 20 20 20 20 20 20 2a 2a 20  e and..      ** 
1d778 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72  gives more oppor
1d779 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72  tunity for asser
1d77a 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ts(sqlite3_mutex
1d77b 5f 68 65 6c 64 28 29 29 0d 0a 20 20 20 20 20 20  _held())..      
1d77c 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
1d77d 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72   find locking pr
1d77e 6f 62 6c 65 6d 73 2e 0d 0a 20 20 20 20 20 20 2a  oblems...      *
1d77f 2f 0d 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72  /..      p->shar
1d780 61 62 6c 65 20 3d 20 31 3b 0d 0a 20 20 20 20 7d  able = 1;..    }
1d781 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 23  ..#endif..  }..#
1d782 65 6e 64 69 66 0d 0a 20 20 69 66 28 20 70 42 74  endif..  if( pBt
1d783 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 0d 0a  ==0 ){..    /*..
1d784 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f      ** The follo
1d785 77 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b  wing asserts mak
1d786 65 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75  e sure that stru
1d787 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74  ctures used by t
1d788 68 65 20 62 74 72 65 65 20 61 72 65 0d 0a 20 20  he btree are..  
1d789 20 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73    ** the right s
1d78a 69 7a 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f  ize.  This is to
1d78b 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 73   guard against s
1d78c 69 7a 65 20 63 68 61 6e 67 65 73 20 74 68 61 74  ize changes that
1d78d 20 72 65 73 75 6c 74 0d 0a 20 20 20 20 2a 2a 20   result..    ** 
1d78e 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f  when compiling o
1d78f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 72  n a different ar
1d790 63 68 69 74 65 63 74 75 72 65 2e 0d 0a 20 20 20  chitecture...   
1d791 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
1d792 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20   sizeof(i64)==8 
1d793 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d  || sizeof(i64)==
1d794 34 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  4 );..    assert
1d795 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38  ( sizeof(u64)==8
1d796 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d   || sizeof(u64)=
1d797 3d 34 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  =4 );..    asser
1d798 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d  t( sizeof(u32)==
1d799 34 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  4 );..    assert
1d79a 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32  ( sizeof(u16)==2
1d79b 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
1d79c 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34   sizeof(Pgno)==4
1d79d 20 29 3b 0d 0a 20 20 0d 0a 20 20 20 20 70 42 74   );..  ..    pBt
1d79e 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
1d79f 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42  Zero( sizeof(*pB
1d7a0 74 29 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  t) );..    if( p
1d7a1 42 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  Bt==0 ){..      
1d7a2 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1d7a3 4d 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  M;..      goto b
1d7a4 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a  tree_open_out;..
1d7a5 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
1d7a6 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
1d7a7 28 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61  (pVfs, &pBt->pPa
1d7a8 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0d  ger, zFilename,.
1d7a9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d7aa 20 20 20 20 20 20 20 20 20 20 20 45 58 54 52 41             EXTRA
1d7ab 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66  _SIZE, flags, vf
1d7ac 73 46 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e  sFlags, pageRein
1d7ad 69 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  it);..    if( rc
1d7ae 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1d7af 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1d7b0 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68  e3PagerReadFileh
1d7b1 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65  eader(pBt->pPage
1d7b2 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64  r,sizeof(zDbHead
1d7b3 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0d  er),zDbHeader);.
1d7b4 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1d7b5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1d7b6 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72  ..      goto btr
1d7b7 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a 20 20  ee_open_out;..  
1d7b8 20 20 7d 0d 0a 20 20 20 20 70 42 74 2d 3e 6f 70    }..    pBt->op
1d7b9 65 6e 46 6c 61 67 73 20 3d 20 28 75 38 29 66 6c  enFlags = (u8)fl
1d7ba 61 67 73 3b 0d 0a 20 20 20 20 70 42 74 2d 3e 64  ags;..    pBt->d
1d7bb 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20 73 71 6c  b = db;..    sql
1d7bc 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
1d7bd 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61  handler(pBt->pPa
1d7be 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65  ger, btreeInvoke
1d7bf 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74  BusyHandler, pBt
1d7c0 29 3b 0d 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d  );..    p->pBt =
1d7c1 20 70 42 74 3b 0d 0a 20 20 0d 0a 20 20 20 20 70   pBt;..  ..    p
1d7c2 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b  Bt->pCursor = 0;
1d7c3 0d 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65  ..    pBt->pPage
1d7c4 31 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20  1 = 0;..    if( 
1d7c5 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65  sqlite3PagerIsre
1d7c6 61 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67  adonly(pBt->pPag
1d7c7 65 72 29 20 29 20 70 42 74 2d 3e 62 74 73 46 6c  er) ) pBt->btsFl
1d7c8 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f  ags |= BTS_READ_
1d7c9 4f 4e 4c 59 3b 0d 0a 23 69 66 64 65 66 20 53 51  ONLY;..#ifdef SQ
1d7ca 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
1d7cb 54 45 0d 0a 20 20 20 20 70 42 74 2d 3e 62 74 73  TE..    pBt->bts
1d7cc 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53 45 43  Flags |= BTS_SEC
1d7cd 55 52 45 5f 44 45 4c 45 54 45 3b 0d 0a 23 65 6e  URE_DELETE;..#en
1d7ce 64 69 66 0d 0a 20 20 20 20 70 42 74 2d 3e 70 61  dif..    pBt->pa
1d7cf 67 65 53 69 7a 65 20 3d 20 28 7a 44 62 48 65 61  geSize = (zDbHea
1d7d0 64 65 72 5b 31 36 5d 3c 3c 38 29 20 7c 20 28 7a  der[16]<<8) | (z
1d7d1 44 62 48 65 61 64 65 72 5b 31 37 5d 3c 3c 31 36  DbHeader[17]<<16
1d7d2 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 42 74 2d  );..    if( pBt-
1d7d3 3e 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c  >pageSize<512 ||
1d7d4 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 53   pBt->pageSize>S
1d7d5 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
1d7d6 49 5a 45 0d 0a 20 20 20 20 20 20 20 20 20 7c 7c  IZE..         ||
1d7d7 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   ((pBt->pageSize
1d7d8 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53 69 7a  -1)&pBt->pageSiz
1d7d9 65 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  e)!=0 ){..      
1d7da 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20  pBt->pageSize = 
1d7db 30 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0;..#ifndef SQLI
1d7dc 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1d7dd 55 4d 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  UM..      /* If 
1d7de 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22  the magic name "
1d7df 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63  :memory:" will c
1d7e0 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f  reate an in-memo
1d7e1 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ry database, the
1d7e2 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76  n..      ** leav
1d7e3 65 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d  e the autoVacuum
1d7e4 20 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e   mode at 0 (do n
1d7e5 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c  ot auto-vacuum),
1d7e6 20 65 76 65 6e 20 69 66 0d 0a 20 20 20 20 20 20   even if..      
1d7e7 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ** SQLITE_DEFAUL
1d7e8 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20  T_AUTOVACUUM is 
1d7e9 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68  true. On the oth
1d7ea 65 72 20 68 61 6e 64 2c 20 69 66 0d 0a 20 20 20  er hand, if..   
1d7eb 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49     ** SQLITE_OMI
1d7ec 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 62  T_MEMORYDB has b
1d7ed 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 65  een defined, the
1d7ee 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20  n ":memory:" is 
1d7ef 6a 75 73 74 20 61 0d 0a 20 20 20 20 20 20 2a 2a  just a..      **
1d7f0 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61   regular file-na
1d7f1 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  me. In this case
1d7f2 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
1d7f3 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20   applies as per 
1d7f4 6e 6f 72 6d 61 6c 2e 0d 0a 20 20 20 20 20 20 2a  normal...      *
1d7f5 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69  /..      if( zFi
1d7f6 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d  lename && !isMem
1d7f7 64 62 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  db ){..        p
1d7f8 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d  Bt->autoVacuum =
1d7f9 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   (SQLITE_DEFAULT
1d7fa 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20  _AUTOVACUUM ? 1 
1d7fb 3a 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  : 0);..        p
1d7fc 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d  Bt->incrVacuum =
1d7fd 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   (SQLITE_DEFAULT
1d7fe 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f  _AUTOVACUUM==2 ?
1d7ff 20 31 20 3a 20 30 29 3b 0d 0a 20 20 20 20 20 20   1 : 0);..      
1d800 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20  }..#endif..     
1d801 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0d 0a   nReserve = 0;..
1d802 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1d803 20 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62    nReserve = zDb
1d804 48 65 61 64 65 72 5b 32 30 5d 3b 0d 0a 20 20 20  Header[20];..   
1d805 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73     pBt->btsFlags
1d806 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 5a 45   |= BTS_PAGESIZE
1d807 5f 46 49 58 45 44 3b 0d 0a 23 69 66 6e 64 65 66  _FIXED;..#ifndef
1d808 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1d809 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 20 20 70  OVACUUM..      p
1d80a 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d  Bt->autoVacuum =
1d80b 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48   (get4byte(&zDbH
1d80c 65 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29  eader[36 + 4*4])
1d80d 3f 31 3a 30 29 3b 0d 0a 20 20 20 20 20 20 70 42  ?1:0);..      pB
1d80e 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
1d80f 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65  (get4byte(&zDbHe
1d810 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f  ader[36 + 7*4])?
1d811 31 3a 30 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  1:0);..#endif.. 
1d812 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73     }..    rc = s
1d813 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
1d814 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67  gesize(pBt->pPag
1d815 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69  er, &pBt->pageSi
1d816 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0d 0a  ze, nReserve);..
1d817 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1d818 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  o btree_open_out
1d819 3b 0d 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62  ;..    pBt->usab
1d81a 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61  leSize = pBt->pa
1d81b 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76  geSize - nReserv
1d81c 65 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  e;..    assert( 
1d81d 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26  (pBt->pageSize &
1d81e 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d   7)==0 );  /* 8-
1d81f 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
1d820 66 20 70 61 67 65 53 69 7a 65 20 2a 2f 0d 0a 20  f pageSize */.. 
1d821 20 20 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64    ..#if !defined
1d822 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
1d823 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64  RED_CACHE) && !d
1d824 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1d825 49 54 5f 44 49 53 4b 49 4f 29 0d 0a 20 20 20 20  IT_DISKIO)..    
1d826 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 42  /* Add the new B
1d827 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
1d828 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  o the linked lis
1d829 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61  t sharable BtSha
1d82a 72 65 64 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  reds...    */.. 
1d82b 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62     if( p->sharab
1d82c 6c 65 20 29 7b 0d 0a 20 20 20 20 20 20 4d 55 54  le ){..      MUT
1d82d 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65  EX_LOGIC( sqlite
1d82e 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68  3_mutex *mutexSh
1d82f 61 72 65 64 3b 20 29 0d 0a 20 20 20 20 20 20 70  ared; )..      p
1d830 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0d 0a 20  Bt->nRef = 1;.. 
1d831 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 43       MUTEX_LOGIC
1d832 28 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20  ( mutexShared = 
1d833 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
1d834 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1d835 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 29 0d  TATIC_MASTER);).
1d836 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
1d837 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
1d838 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
1d839 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
1d83a 7b 0d 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  {..        pBt->
1d83b 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
1d83c 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
1d83d 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0d 0a 20  _MUTEX_FAST);.. 
1d83e 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e         if( pBt->
1d83f 6d 75 74 65 78 3d 3d 30 20 29 7b 0d 0a 20 20 20  mutex==0 ){..   
1d840 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1d841 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20  TE_NOMEM;..     
1d842 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
1d843 61 69 6c 65 64 20 3d 20 30 3b 0d 0a 20 20 20 20  ailed = 0;..    
1d844 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65        goto btree
1d845 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a 20 20 20 20  _open_out;..    
1d846 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
1d847 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
1d848 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53  tex_enter(mutexS
1d849 68 61 72 65 64 29 3b 0d 0a 20 20 20 20 20 20 70  hared);..      p
1d84a 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42  Bt->pNext = GLOB
1d84b 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
1d84c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
1d84d 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 47 4c 4f  ist);..      GLO
1d84e 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
1d84f 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
1d850 4c 69 73 74 29 20 3d 20 70 42 74 3b 0d 0a 20 20  List) = pBt;..  
1d851 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1d852 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
1d853 72 65 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65  red);..    }..#e
1d854 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 23 69 66  ndif..  }....#if
1d855 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1d856 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1d857 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  HE) && !defined(
1d858 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
1d859 49 4f 29 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65  IO)..  /* If the
1d85a 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20   new Btree uses 
1d85b 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68  a sharable pBtSh
1d85c 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20  ared, then link 
1d85d 74 68 65 20 6e 65 77 0d 0a 20 20 2a 2a 20 42 74  the new..  ** Bt
1d85e 72 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73  ree into the lis
1d85f 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c  t of all sharabl
1d860 65 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65  e Btrees for the
1d861 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e   same connection
1d862 2e 0d 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74  ...  ** The list
1d863 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65   is kept in asce
1d864 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
1d865 42 74 20 61 64 64 72 65 73 73 2e 0d 0a 20 20 2a  Bt address...  *
1d866 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  /..  if( p->shar
1d867 61 62 6c 65 20 29 7b 0d 0a 20 20 20 20 69 6e 74  able ){..    int
1d868 20 69 3b 0d 0a 20 20 20 20 42 74 72 65 65 20 2a   i;..    Btree *
1d869 70 53 69 62 3b 0d 0a 20 20 20 20 66 6f 72 28 69  pSib;..    for(i
1d86a 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1d86b 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  ++){..      if( 
1d86c 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b  (pSib = db->aDb[
1d86d 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53  i].pBt)!=0 && pS
1d86e 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0d  ib->sharable ){.
1d86f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
1d870 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70  pSib->pPrev ){ p
1d871 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65  Sib = pSib->pPre
1d872 76 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66  v; }..        if
1d873 28 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70  ( p->pBt<pSib->p
1d874 42 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  Bt ){..         
1d875 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62   p->pNext = pSib
1d876 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ;..          p->
1d877 70 50 72 65 76 20 3d 20 30 3b 0d 0a 20 20 20 20  pPrev = 0;..    
1d878 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72 65        pSib->pPre
1d879 76 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20  v = p;..        
1d87a 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
1d87b 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70    while( pSib->p
1d87c 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e  Next && pSib->pN
1d87d 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20  ext->pBt<p->pBt 
1d87e 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
1d87f 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65  pSib = pSib->pNe
1d880 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  xt;..          }
1d881 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ..          p->p
1d882 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65  Next = pSib->pNe
1d883 78 74 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  xt;..          p
1d884 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b 0d  ->pPrev = pSib;.
1d885 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1d886 2d 3e 70 4e 65 78 74 20 29 7b 0d 0a 20 20 20 20  ->pNext ){..    
1d887 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
1d888 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0d 0a 20 20  ->pPrev = p;..  
1d889 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
1d88a 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74       pSib->pNext
1d88b 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20 7d   = p;..        }
1d88c 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
1d88d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
1d88e 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20  ..  }..#endif.. 
1d88f 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0d 0a   *ppBtree = p;..
1d890 0d 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  ..btree_open_out
1d891 3a 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  :..  if( rc!=SQL
1d892 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69  ITE_OK ){..    i
1d893 66 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70  f( pBt && pBt->p
1d894 50 61 67 65 72 20 29 7b 0d 0a 20 20 20 20 20 20  Pager ){..      
1d895 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73  sqlite3PagerClos
1d896 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0d  e(pBt->pPager);.
1d897 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1d898 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0d 0a  te3_free(pBt);..
1d899 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1d89a 28 70 29 3b 0d 0a 20 20 20 20 2a 70 70 42 74 72  (p);..    *ppBtr
1d89b 65 65 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65  ee = 0;..  }else
1d89c 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  {..    /* If the
1d89d 20 42 2d 54 72 65 65 20 77 61 73 20 73 75 63 63   B-Tree was succ
1d89e 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c  essfully opened,
1d89f 20 73 65 74 20 74 68 65 20 70 61 67 65 72 2d 63   set the pager-c
1d8a0 61 63 68 65 20 73 69 7a 65 20 74 6f 20 74 68 65  ache size to the
1d8a1 0d 0a 20 20 20 20 2a 2a 20 64 65 66 61 75 6c 74  ..    ** default
1d8a2 20 76 61 6c 75 65 2e 20 45 78 63 65 70 74 2c 20   value. Except, 
1d8a3 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 6f 6e 20  when opening on 
1d8a4 61 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61 72  an existing shar
1d8a5 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c 0d  ed pager-cache,.
1d8a6 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 63  .    ** do not c
1d8a7 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 72 2d  hange the pager-
1d8a8 63 61 63 68 65 20 73 69 7a 65 2e 0d 0a 20 20 20  cache size...   
1d8a9 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   */..    if( sql
1d8aa 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
1d8ab 70 2c 20 30 2c 20 30 29 3d 3d 30 20 29 7b 0d 0a  p, 0, 0)==0 ){..
1d8ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
1d8ad 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 70  erSetCachesize(p
1d8ae 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 53  ->pBt->pPager, S
1d8af 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
1d8b0 43 48 45 5f 53 49 5a 45 29 3b 0d 0a 20 20 20 20  CHE_SIZE);..    
1d8b1 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6d 75  }..  }..  if( mu
1d8b2 74 65 78 4f 70 65 6e 20 29 7b 0d 0a 20 20 20 20  texOpen ){..    
1d8b3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1d8b4 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78  mutex_held(mutex
1d8b5 4f 70 65 6e 29 20 29 3b 0d 0a 20 20 20 20 73 71  Open) );..    sq
1d8b6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1d8b7 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0d 0a 20  e(mutexOpen);.. 
1d8b8 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
1d8b9 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65  ..}..../*..** De
1d8ba 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53 68  crement the BtSh
1d8bb 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65  ared.nRef counte
1d8bc 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61 63  r.  When it reac
1d8bd 68 65 73 20 7a 65 72 6f 2c 0d 0a 2a 2a 20 72 65  hes zero,..** re
1d8be 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65  move the BtShare
1d8bf 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d  d structure from
1d8c0 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73   the sharing lis
1d8c1 74 2e 20 20 52 65 74 75 72 6e 0d 0a 2a 2a 20 74  t.  Return..** t
1d8c2 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61  rue if the BtSha
1d8c3 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72  red.nRef counter
1d8c4 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e   reaches zero an
1d8c5 64 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 66 61 6c  d return..** fal
1d8c6 73 65 20 69 66 20 69 74 20 69 73 20 73 74 69 6c  se if it is stil
1d8c7 6c 20 70 6f 73 69 74 69 76 65 2e 0d 0a 2a 2f 0d  l positive...*/.
1d8c8 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f  .static int remo
1d8c9 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
1d8ca 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
1d8cb 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  {..#ifndef SQLIT
1d8cc 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1d8cd 43 48 45 0d 0a 20 20 4d 55 54 45 58 5f 4c 4f 47  CHE..  MUTEX_LOG
1d8ce 49 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  IC( sqlite3_mute
1d8cf 78 20 2a 70 4d 61 73 74 65 72 3b 20 29 0d 0a 20  x *pMaster; ).. 
1d8d0 20 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74   BtShared *pList
1d8d1 3b 0d 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64  ;..  int removed
1d8d2 20 3d 20 30 3b 0d 0a 0d 0a 20 20 61 73 73 65 72   = 0;....  asser
1d8d3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1d8d4 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75  _notheld(pBt->mu
1d8d5 74 65 78 29 20 29 3b 0d 0a 20 20 4d 55 54 45 58  tex) );..  MUTEX
1d8d6 5f 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20  _LOGIC( pMaster 
1d8d7 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
1d8d8 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
1d8d9 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
1d8da 20 29 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   )..  sqlite3_mu
1d8db 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65  tex_enter(pMaste
1d8dc 72 29 3b 0d 0a 20 20 70 42 74 2d 3e 6e 52 65 66  r);..  pBt->nRef
1d8dd 2d 2d 3b 0d 0a 20 20 69 66 28 20 70 42 74 2d 3e  --;..  if( pBt->
1d8de 6e 52 65 66 3c 3d 30 20 29 7b 0d 0a 20 20 20 20  nRef<=0 ){..    
1d8df 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  if( GLOBAL(BtSha
1d8e0 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
1d8e1 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42  edCacheList)==pB
1d8e2 74 20 29 7b 0d 0a 20 20 20 20 20 20 47 4c 4f 42  t ){..      GLOB
1d8e3 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
1d8e4 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
1d8e5 69 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78  ist) = pBt->pNex
1d8e6 74 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  t;..    }else{..
1d8e7 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 47 4c        pList = GL
1d8e8 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73  OBAL(BtShared*,s
1d8e9 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
1d8ea 65 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 77  eList);..      w
1d8eb 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 4c 69  hile( ALWAYS(pLi
1d8ec 73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e 70 4e  st) && pList->pN
1d8ed 65 78 74 21 3d 70 42 74 20 29 7b 0d 0a 20 20 20  ext!=pBt ){..   
1d8ee 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 74       pList=pList
1d8ef 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20  ->pNext;..      
1d8f0 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  }..      if( ALW
1d8f1 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0d 0a 20  AYS(pList) ){.. 
1d8f2 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e         pList->pN
1d8f3 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74  ext = pBt->pNext
1d8f4 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1d8f5 7d 0d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  }..    if( SQLIT
1d8f6 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b 0d  E_THREADSAFE ){.
1d8f7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1d8f8 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d  utex_free(pBt->m
1d8f9 75 74 65 78 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  utex);..    }.. 
1d8fa 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0d     removed = 1;.
1d8fb 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
1d8fc 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73  mutex_leave(pMas
1d8fd 74 65 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ter);..  return 
1d8fe 72 65 6d 6f 76 65 64 3b 0d 0a 23 65 6c 73 65 0d  removed;..#else.
1d8ff 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 23 65  .  return 1;..#e
1d900 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ndif..}..../*..*
1d901 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d  * Make sure pBt-
1d902 3e 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74  >pTmpSpace point
1d903 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69  s to an allocati
1d904 6f 6e 20 6f 66 20 0d 0a 2a 2a 20 4d 58 5f 43 45  on of ..** MX_CE
1d905 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74  LL_SIZE(pBt) byt
1d906 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
1d907 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d  void allocateTem
1d908 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20  pSpace(BtShared 
1d909 2a 70 42 74 29 7b 0d 0a 20 20 69 66 28 20 21 70  *pBt){..  if( !p
1d90a 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b  Bt->pTmpSpace ){
1d90b 0d 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53  ..    pBt->pTmpS
1d90c 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61  pace = sqlite3Pa
1d90d 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70  geMalloc( pBt->p
1d90e 61 67 65 53 69 7a 65 20 29 3b 0d 0a 20 20 7d 0d  ageSize );..  }.
1d90f 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65  .}..../*..** Fre
1d910 65 20 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53  e the pBt->pTmpS
1d911 70 61 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0d  pace allocation.
1d912 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
1d913 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 42   freeTempSpace(B
1d914 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0d 0a  tShared *pBt){..
1d915 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65    sqlite3PageFre
1d916 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  e( pBt->pTmpSpac
1d917 65 29 3b 0d 0a 20 20 70 42 74 2d 3e 70 54 6d 70  e);..  pBt->pTmp
1d918 53 70 61 63 65 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d  Space = 0;..}...
1d919 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e  ./*..** Close an
1d91a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61   open database a
1d91b 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  nd invalidate al
1d91c 6c 20 63 75 72 73 6f 72 73 2e 0d 0a 2a 2f 0d 0a  l cursors...*/..
1d91d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1d91e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
1d91f 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0d  lose(Btree *p){.
1d920 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1d921 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 42 74   = p->pBt;..  Bt
1d922 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0d 0a 0d  Cursor *pCur;...
1d923 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  .  /* Close all 
1d924 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76  cursors opened v
1d925 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20  ia this handle. 
1d926 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
1d927 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1d928 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
1d929 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  );..  sqlite3Btr
1d92a 65 65 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20 70  eeEnter(p);..  p
1d92b 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73  Cur = pBt->pCurs
1d92c 6f 72 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 43  or;..  while( pC
1d92d 75 72 20 29 7b 0d 0a 20 20 20 20 42 74 43 75 72  ur ){..    BtCur
1d92e 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72  sor *pTmp = pCur
1d92f 3b 0d 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43  ;..    pCur = pC
1d930 75 72 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  ur->pNext;..    
1d931 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65  if( pTmp->pBtree
1d932 3d 3d 70 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  ==p ){..      sq
1d933 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
1d934 75 72 73 6f 72 28 70 54 6d 70 29 3b 0d 0a 20 20  ursor(pTmp);..  
1d935 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
1d936 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   Rollback any ac
1d937 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tive transaction
1d938 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61   and free the ha
1d939 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0d  ndle structure..
1d93a 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74  .  ** The call t
1d93b 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f  o sqlite3BtreeRo
1d93c 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61  llback() drops a
1d93d 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68  ny table-locks h
1d93e 65 6c 64 20 62 79 0d 0a 20 20 2a 2a 20 74 68 69  eld by..  ** thi
1d93f 73 20 68 61 6e 64 6c 65 2e 0d 0a 20 20 2a 2f 0d  s handle...  */.
1d940 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  .  sqlite3BtreeR
1d941 6f 6c 6c 62 61 63 6b 28 70 29 3b 0d 0a 20 20 73  ollback(p);..  s
1d942 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1d943 28 70 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  (p);....  /* If 
1d944 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20  there are still 
1d945 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e  other outstandin
1d946 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  g references to 
1d947 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
1d948 0d 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65  ..  ** structure
1d949 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68  , return now. Th
1d94a 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
1d94b 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c  his procedure cl
1d94c 65 61 6e 73 20 0d 0a 20 20 2a 2a 20 75 70 20 74  eans ..  ** up t
1d94d 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e  he shared-btree.
1d94e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ..  */..  assert
1d94f 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
1d950 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d  =0 && p->locked=
1d951 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 21 70 2d  =0 );..  if( !p-
1d952 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d  >sharable || rem
1d953 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69  oveFromSharingLi
1d954 73 74 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20  st(pBt) ){..    
1d955 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f  /* The pBt is no
1d956 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73   longer on the s
1d957 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20  haring list, so 
1d958 77 65 20 63 61 6e 20 61 63 63 65 73 73 0d 0a 20  we can access.. 
1d959 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74     ** it without
1d95a 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20   having to hold 
1d95b 74 68 65 20 6d 75 74 65 78 2e 0d 0a 20 20 20 20  the mutex...    
1d95c 2a 2a 0d 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e  **..    ** Clean
1d95d 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20   out and delete 
1d95e 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
1d95f 65 63 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ect...    */..  
1d960 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
1d961 70 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20 20 20  pCursor );..    
1d962 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73  sqlite3PagerClos
1d963 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0d  e(pBt->pPager);.
1d964 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46  .    if( pBt->xF
1d965 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74  reeSchema && pBt
1d966 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0d 0a 20 20  ->pSchema ){..  
1d967 20 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63      pBt->xFreeSc
1d968 68 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d  hema(pBt->pSchem
1d969 61 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  a);..    }..    
1d96a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 30 2c  sqlite3DbFree(0,
1d96b 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0d   pBt->pSchema);.
1d96c 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61  .    freeTempSpa
1d96d 63 65 28 70 42 74 29 3b 0d 0a 20 20 20 20 73 71  ce(pBt);..    sq
1d96e 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b  lite3_free(pBt);
1d96f 0d 0a 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66  ..  }....#ifndef
1d970 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1d971 52 45 44 5f 43 41 43 48 45 0d 0a 20 20 61 73 73  RED_CACHE..  ass
1d972 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
1d973 63 6b 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  ck==0 );..  asse
1d974 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30  rt( p->locked==0
1d975 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 50   );..  if( p->pP
1d976 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e  rev ) p->pPrev->
1d977 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
1d978 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78  ;..  if( p->pNex
1d979 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50  t ) p->pNext->pP
1d97a 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0d  rev = p->pPrev;.
1d97b 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 73 71 6c  .#endif....  sql
1d97c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
1d97d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1d97e 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
1d97f 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69 74  Change the limit
1d980 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   on the number o
1d981 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64 20  f pages allowed 
1d982 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0d 0a 2a  in the cache...*
1d983 2a 0d 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  *..** The maximu
1d984 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68  m number of cach
1d985 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74  e pages is set t
1d986 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0d 0a  o the absolute..
1d987 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61  ** value of mxPa
1d988 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69  ge.  If mxPage i
1d989 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  s negative, the 
1d98a 70 61 67 65 72 20 77 69 6c 6c 0d 0a 2a 2a 20 6f  pager will..** o
1d98b 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e  perate asynchron
1d98c 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20  ously - it will 
1d98d 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66  not stop to do f
1d98e 73 79 6e 63 28 29 73 0d 0a 2a 2a 20 74 6f 20 69  sync()s..** to i
1d98f 6e 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72  nsure data is wr
1d990 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73  itten to the dis
1d991 6b 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65  k surface before
1d992 0d 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e  ..** continuing.
1d993 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73    Transactions s
1d994 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e  till work if syn
1d995 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c  chronous is off,
1d996 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ..** and the dat
1d997 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20  abase cannot be 
1d998 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 69  corrupted if thi
1d999 73 20 70 72 6f 67 72 61 6d 0d 0a 2a 2a 20 63 72  s program..** cr
1d99a 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74  ashes.  But if t
1d99b 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
1d99c 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74  tem crashes or t
1d99d 68 65 72 65 20 69 73 0d 0a 2a 2a 20 61 6e 20 61  here is..** an a
1d99e 62 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c  brupt power fail
1d99f 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f  ure when synchro
1d9a0 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65  nous is off, the
1d9a1 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63 6f   database..** co
1d9a2 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61  uld be left in a
1d9a3 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61  n inconsistent a
1d9a4 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65  nd unrecoverable
1d9a5 20 73 74 61 74 65 2e 0d 0a 2a 2a 20 53 79 6e 63   state...** Sync
1d9a6 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79  hronous is on by
1d9a7 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61   default so data
1d9a8 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
1d9a9 69 73 20 6e 6f 74 0d 0a 2a 2a 20 6e 6f 72 6d 61  is not..** norma
1d9aa 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0d 0a 2a 2f  lly a worry...*/
1d9ab 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1d9ac 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1d9ad 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74  eSetCacheSize(Bt
1d9ae 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61  ree *p, int mxPa
1d9af 67 65 29 7b 0d 0a 20 20 42 74 53 68 61 72 65 64  ge){..  BtShared
1d9b0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0d   *pBt = p->pBt;.
1d9b1 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1d9b2 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1d9b3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  >db->mutex) );..
1d9b4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
1d9b5 74 65 72 28 70 29 3b 0d 0a 20 20 73 71 6c 69 74  ter(p);..  sqlit
1d9b6 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73  e3PagerSetCaches
1d9b7 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
1d9b8 20 6d 78 50 61 67 65 29 3b 0d 0a 20 20 73 71 6c   mxPage);..  sql
1d9b9 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1d9ba 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
1d9bb 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
1d9bc 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  ..** Change the 
1d9bd 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63  way data is sync
1d9be 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72  ed to disk in or
1d9bf 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20  der to increase 
1d9c0 6f 72 20 64 65 63 72 65 61 73 65 0d 0a 2a 2a 20  or decrease..** 
1d9c1 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74  how well the dat
1d9c2 61 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61  abase resists da
1d9c3 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63  mage due to OS c
1d9c4 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72  rashes and power
1d9c5 0d 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20  ..** failures.  
1d9c6 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 73  Level 1 is the s
1d9c7 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e  ame as asynchron
1d9c8 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20  ous (no syncs() 
1d9c9 6f 63 63 75 72 20 61 6e 64 0d 0a 2a 2a 20 74 68  occur and..** th
1d9ca 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72  ere is a high pr
1d9cb 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d  obability of dam
1d9cc 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73  age)  Level 2 is
1d9cd 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54   the default.  T
1d9ce 68 65 72 65 0d 0a 2a 2a 20 69 73 20 61 20 76 65  here..** is a ve
1d9cf 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a  ry low but non-z
1d9d0 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20  ero probability 
1d9d1 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65  of damage.  Leve
1d9d2 6c 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0d  l 3 reduces the.
1d9d3 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20  .** probability 
1d9d4 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61  of damage to nea
1d9d5 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20  r zero but with 
1d9d6 61 20 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61  a write performa
1d9d7 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e 0d 0a  nce reduction...
1d9d8 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1d9d9 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
1d9da 41 47 4d 41 53 0d 0a 53 51 4c 49 54 45 5f 50 52  AGMAS..SQLITE_PR
1d9db 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1d9dc 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c  3BtreeSetSafetyL
1d9dd 65 76 65 6c 28 0d 0a 20 20 42 74 72 65 65 20 2a  evel(..  Btree *
1d9de 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
1d9df 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20  /* The btree to 
1d9e0 73 65 74 20 74 68 65 20 73 61 66 65 74 79 20 6c  set the safety l
1d9e1 65 76 65 6c 20 6f 6e 20 2a 2f 0d 0a 20 20 69 6e  evel on */..  in
1d9e2 74 20 6c 65 76 65 6c 2c 20 20 20 20 20 20 20 20  t level,        
1d9e3 20 20 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 73       /* PRAGMA s
1d9e4 79 6e 63 68 72 6f 6e 6f 75 73 2e 20 20 31 3d 4f  ynchronous.  1=O
1d9e5 46 46 2c 20 32 3d 4e 4f 52 4d 41 4c 2c 20 33 3d  FF, 2=NORMAL, 3=
1d9e6 46 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e 74 20 66  FULL */..  int f
1d9e7 75 6c 6c 53 79 6e 63 2c 20 20 20 20 20 20 20 20  ullSync,        
1d9e8 20 20 2f 2a 20 50 52 41 47 4d 41 20 66 75 6c 6c    /* PRAGMA full
1d9e9 66 73 79 6e 63 2e 20 2a 2f 0d 0a 20 20 69 6e 74  fsync. */..  int
1d9ea 20 63 6b 70 74 46 75 6c 6c 53 79 6e 63 20 20 20   ckptFullSync   
1d9eb 20 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 63 68      /* PRAGMA ch
1d9ec 65 63 6b 70 6f 69 6e 74 5f 66 75 6c 6c 66 79 6e  eckpoint_fullfyn
1d9ed 63 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 42 74 53 68  c */..){..  BtSh
1d9ee 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1d9ef 42 74 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  Bt;..  assert( s
1d9f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1d9f1 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
1d9f2 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6c 65  );..  assert( le
1d9f3 76 65 6c 3e 3d 31 20 26 26 20 6c 65 76 65 6c 3c  vel>=1 && level<
1d9f4 3d 33 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  =3 );..  sqlite3
1d9f5 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0d 0a  BtreeEnter(p);..
1d9f6 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65    sqlite3PagerSe
1d9f7 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 74  tSafetyLevel(pBt
1d9f8 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c  ->pPager, level,
1d9f9 20 66 75 6c 6c 53 79 6e 63 2c 20 63 6b 70 74 46   fullSync, ckptF
1d9fa 75 6c 6c 53 79 6e 63 29 3b 0d 0a 20 20 73 71 6c  ullSync);..  sql
1d9fb 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1d9fc 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
1d9fd 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e 64  ITE_OK;..}..#end
1d9fe 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  if..../*..** Ret
1d9ff 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
1da00 67 69 76 65 6e 20 62 74 72 65 65 20 69 73 20 73  given btree is s
1da01 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76  et to safety lev
1da02 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0d  el 1.  In other.
1da03 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72  .** words, retur
1da04 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e  n TRUE if no syn
1da05 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68  c() occurs on th
1da06 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0d 0a 2a  e disk files...*
1da07 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
1da08 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1da09 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42  eeSyncDisabled(B
1da0a 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20 42 74 53  tree *p){..  BtS
1da0b 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1da0c 70 42 74 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  pBt;..  int rc;.
1da0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1da0e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1da0f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20  >db->mutex) );  
1da10 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1da11 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20 61 73 73  Enter(p);..  ass
1da12 65 72 74 28 20 70 42 74 20 26 26 20 70 42 74 2d  ert( pBt && pBt-
1da13 3e 70 50 61 67 65 72 20 29 3b 0d 0a 20 20 72 63  >pPager );..  rc
1da14 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e   = sqlite3PagerN
1da15 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 67 65  osync(pBt->pPage
1da16 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74  r);..  sqlite3Bt
1da17 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20  reeLeave(p);..  
1da18 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
1da19 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ./*..** Change t
1da1a 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73  he default pages
1da1b 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75   size and the nu
1da1c 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64  mber of reserved
1da1d 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e   bytes per page.
1da1e 0d 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20  ..** Or, if the 
1da1f 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c  page size has al
1da20 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64  ready been fixed
1da21 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
1da22 52 45 41 44 4f 4e 4c 59 20 0d 0a 2a 2a 20 77 69  READONLY ..** wi
1da23 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 61  thout changing a
1da24 6e 79 74 68 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a  nything...**..**
1da25 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d   The page size m
1da26 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
1da27 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20  f 2 between 512 
1da28 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 74  and 65536.  If t
1da29 68 65 20 70 61 67 65 0d 0a 2a 2a 20 73 69 7a 65  he page..** size
1da2a 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e   supplied does n
1da2b 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e  ot meet this con
1da2c 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65  straint then the
1da2d 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
1da2e 74 0d 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0d 0a  t..** changed...
1da2f 2a 2a 0d 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65  **..** Page size
1da30 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65  s are constraine
1da31 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20  d to be a power 
1da32 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74  of two so that t
1da33 68 65 20 72 65 67 69 6f 6e 0d 0a 2a 2a 20 6f 66  he region..** of
1da34 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1da35 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  le used for lock
1da36 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61  ing (beginning a
1da37 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0d  t PENDING_BYTE,.
1da38 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79  .** the first by
1da39 74 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20  te past the 1GB 
1da3a 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30  boundary, 0x4000
1da3b 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f  0000) needs to o
1da3c 63 63 75 72 0d 0a 2a 2a 20 61 74 20 74 68 65 20  ccur..** at the 
1da3d 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70  beginning of a p
1da3e 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  age...**..** If 
1da3f 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 72  parameter nReser
1da40 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  ve is less than 
1da41 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e  zero, then the n
1da42 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65  umber of reserve
1da43 64 0d 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20  d..** bytes per 
1da44 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63  page is left unc
1da45 68 61 6e 67 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  hanged...**..** 
1da46 49 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 74  If the iFix!=0 t
1da47 68 65 6e 20 74 68 65 20 42 54 53 5f 50 41 47 45  hen the BTS_PAGE
1da48 53 49 5a 45 5f 46 49 58 45 44 20 66 6c 61 67 20  SIZE_FIXED flag 
1da49 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74  is set so that t
1da4a 68 65 20 70 61 67 65 20 73 69 7a 65 0d 0a 2a 2a  he page size..**
1da4b 20 61 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20   and autovacuum 
1da4c 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67  mode can no long
1da4d 65 72 20 62 65 20 63 68 61 6e 67 65 64 2e 0d 0a  er be changed...
1da4e 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1da4f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1da50 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
1da51 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67  tree *p, int pag
1da52 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  eSize, int nRese
1da53 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 7b 0d  rve, int iFix){.
1da54 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1da55 54 45 5f 4f 4b 3b 0d 0a 20 20 42 74 53 68 61 72  TE_OK;..  BtShar
1da56 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1da57 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65  ;..  assert( nRe
1da58 73 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65  serve>=-1 && nRe
1da59 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0d 0a 20  serve<=255 );.. 
1da5a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1da5b 65 72 28 70 29 3b 0d 0a 20 20 69 66 28 20 70 42  er(p);..  if( pB
1da5c 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54  t->btsFlags & BT
1da5d 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44  S_PAGESIZE_FIXED
1da5e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
1da5f 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a  BtreeLeave(p);..
1da60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1da61 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d 0a 20 20 7d  E_READONLY;..  }
1da62 0d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65  ..  if( nReserve
1da63 3c 30 20 29 7b 0d 0a 20 20 20 20 6e 52 65 73 65  <0 ){..    nRese
1da64 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  rve = pBt->pageS
1da65 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c  ize - pBt->usabl
1da66 65 53 69 7a 65 3b 0d 0a 20 20 7d 0d 0a 20 20 61  eSize;..  }..  a
1da67 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e  ssert( nReserve>
1da68 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d  =0 && nReserve<=
1da69 32 35 35 20 29 3b 0d 0a 20 20 69 66 28 20 70 61  255 );..  if( pa
1da6a 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
1da6b 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f  ageSize<=SQLITE_
1da6c 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26  MAX_PAGE_SIZE &&
1da6d 0d 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65  ..        ((page
1da6e 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65  Size-1)&pageSize
1da6f 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73 73  )==0 ){..    ass
1da70 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26  ert( (pageSize &
1da71 20 37 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 61   7)==0 );..    a
1da72 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61  ssert( !pBt->pPa
1da73 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75  ge1 && !pBt->pCu
1da74 72 73 6f 72 20 29 3b 0d 0a 20 20 20 20 70 42 74  rsor );..    pBt
1da75 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 33  ->pageSize = (u3
1da76 32 29 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20 20  2)pageSize;..   
1da77 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70   freeTempSpace(p
1da78 42 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20  Bt);..  }..  rc 
1da79 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
1da7a 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
1da7b 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
1da7c 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29  eSize, nReserve)
1da7d 3b 0d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c 65  ;..  pBt->usable
1da7e 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65  Size = pBt->page
1da7f 53 69 7a 65 20 2d 20 28 75 31 36 29 6e 52 65 73  Size - (u16)nRes
1da80 65 72 76 65 3b 0d 0a 20 20 69 66 28 20 69 46 69  erve;..  if( iFi
1da81 78 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 67  x ) pBt->btsFlag
1da82 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 5a  s |= BTS_PAGESIZ
1da83 45 5f 46 49 58 45 44 3b 0d 0a 20 20 73 71 6c 69  E_FIXED;..  sqli
1da84 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1da85 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
1da86 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
1da87 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c  urn the currentl
1da88 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73  y defined page s
1da89 69 7a 65 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ize..*/..SQLITE_
1da8a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1da8b 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
1da8c 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a  ize(Btree *p){..
1da8d 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
1da8e 3e 70 61 67 65 53 69 7a 65 3b 0d 0a 7d 0d 0a 0d  >pageSize;..}...
1da8f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1da90 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
1da91 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
1da92 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1da93 5f 56 41 43 55 55 4d 29 0d 0a 2f 2a 0d 0a 2a 2a  _VACUUM)../*..**
1da94 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1da95 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
1da96 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20  pace at the end 
1da97 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68  of every page th
1da98 61 74 0d 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e  at..** are inten
1da99 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73  tually left unus
1da9a 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ed.  This is the
1da9b 20 22 72 65 73 65 72 76 65 64 22 20 73 70 61 63   "reserved" spac
1da9c 65 20 74 68 61 74 20 69 73 0d 0a 2a 2a 20 73 6f  e that is..** so
1da9d 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79 20  metimes used by 
1da9e 65 78 74 65 6e 73 69 6f 6e 73 2e 0d 0a 2a 2f 0d  extensions...*/.
1da9f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1daa0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1daa1 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65  GetReserve(Btree
1daa2 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 6e 3b 0d   *p){..  int n;.
1daa3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1daa4 6e 74 65 72 28 70 29 3b 0d 0a 20 20 6e 20 3d 20  nter(p);..  n = 
1daa5 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  p->pBt->pageSize
1daa6 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c   - p->pBt->usabl
1daa7 65 53 69 7a 65 3b 0d 0a 20 20 73 71 6c 69 74 65  eSize;..  sqlite
1daa8 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d  3BtreeLeave(p);.
1daa9 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0d 0a 7d 0d  .  return n;..}.
1daaa 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68  .../*..** Set th
1daab 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
1daac 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 74 61 62  ount for a datab
1daad 61 73 65 20 69 66 20 6d 78 50 61 67 65 20 69 73  ase if mxPage is
1daae 20 70 6f 73 69 74 69 76 65 2e 0d 0a 2a 2a 20 4e   positive...** N
1daaf 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  o changes are ma
1dab0 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 73 20  de if mxPage is 
1dab1 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 0d 0a  0 or negative...
1dab2 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66  ** Regardless of
1dab3 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 78   the value of mx
1dab4 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65  Page, return the
1dab5 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f   maximum page co
1dab6 75 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  unt...*/..SQLITE
1dab7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1dab8 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65  ite3BtreeMaxPage
1dab9 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20  Count(Btree *p, 
1daba 69 6e 74 20 6d 78 50 61 67 65 29 7b 0d 0a 20 20  int mxPage){..  
1dabb 69 6e 74 20 6e 3b 0d 0a 20 20 73 71 6c 69 74 65  int n;..  sqlite
1dabc 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0d  3BtreeEnter(p);.
1dabd 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61  .  n = sqlite3Pa
1dabe 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
1dabf 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  p->pBt->pPager, 
1dac0 6d 78 50 61 67 65 29 3b 0d 0a 20 20 73 71 6c 69  mxPage);..  sqli
1dac1 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1dac2 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0d 0a  ;..  return n;..
1dac3 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20  }..../*..** Set 
1dac4 74 68 65 20 42 54 53 5f 53 45 43 55 52 45 5f 44  the BTS_SECURE_D
1dac5 45 4c 45 54 45 20 66 6c 61 67 20 69 66 20 6e 65  ELETE flag if ne
1dac6 77 46 6c 61 67 20 69 73 20 30 20 6f 72 20 31 2e  wFlag is 0 or 1.
1dac7 20 20 49 66 20 6e 65 77 46 6c 61 67 20 69 73 20    If newFlag is 
1dac8 2d 31 2c 0d 0a 2a 2a 20 74 68 65 6e 20 6d 61 6b  -1,..** then mak
1dac9 65 20 6e 6f 20 63 68 61 6e 67 65 73 2e 20 20 41  e no changes.  A
1daca 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74 68 65  lways return the
1dacb 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 42 54   value of the BT
1dacc 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0d  S_SECURE_DELETE.
1dacd 0a 2a 2a 20 73 65 74 74 69 6e 67 20 61 66 74 65  .** setting afte
1dace 72 20 74 68 65 20 63 68 61 6e 67 65 2e 0d 0a 2a  r the change...*
1dacf 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
1dad0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1dad1 65 65 53 65 63 75 72 65 44 65 6c 65 74 65 28 42  eeSecureDelete(B
1dad2 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 65 77  tree *p, int new
1dad3 46 6c 61 67 29 7b 0d 0a 20 20 69 6e 74 20 62 3b  Flag){..  int b;
1dad4 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
1dad5 65 74 75 72 6e 20 30 3b 0d 0a 20 20 73 71 6c 69  eturn 0;..  sqli
1dad6 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
1dad7 3b 0d 0a 20 20 69 66 28 20 6e 65 77 46 6c 61 67  ;..  if( newFlag
1dad8 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 70  >=0 ){..    p->p
1dad9 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20  Bt->btsFlags &= 
1dada 7e 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45  ~BTS_SECURE_DELE
1dadb 54 45 3b 0d 0a 20 20 20 20 69 66 28 20 6e 65 77  TE;..    if( new
1dadc 46 6c 61 67 20 29 20 70 2d 3e 70 42 74 2d 3e 62  Flag ) p->pBt->b
1dadd 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53  tsFlags |= BTS_S
1dade 45 43 55 52 45 5f 44 45 4c 45 54 45 3b 0d 0a 20  ECURE_DELETE;.. 
1dadf 20 7d 20 0d 0a 20 20 62 20 3d 20 28 70 2d 3e 70   } ..  b = (p->p
1dae0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42  Bt->btsFlags & B
1dae1 54 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TS_SECURE_DELETE
1dae2 29 21 3d 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33  )!=0;..  sqlite3
1dae3 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a  BtreeLeave(p);..
1dae4 20 20 72 65 74 75 72 6e 20 62 3b 0d 0a 7d 0d 0a    return b;..}..
1dae5 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
1dae6 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
1dae7 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c  AGER_PRAGMAS) ||
1dae8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1dae9 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f  _OMIT_VACUUM) */
1daea 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67  ..../*..** Chang
1daeb 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75  e the 'auto-vacu
1daec 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20  um' property of 
1daed 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66  the database. If
1daee 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d   the 'autoVacuum
1daef 27 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  '..** parameter 
1daf0 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1daf1 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
1daf2 64 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49  de is enabled. I
1daf3 66 20 7a 65 72 6f 2c 20 69 74 0d 0a 2a 2a 20 69  f zero, it..** i
1daf4 73 20 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20  s disabled. The 
1daf5 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
1daf6 72 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75  r the auto-vacuu
1daf7 6d 20 70 72 6f 70 65 72 74 79 20 69 73 20 0d 0a  m property is ..
1daf8 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  ** determined by
1daf9 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41   the SQLITE_DEFA
1dafa 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d  ULT_AUTOVACUUM m
1dafb 61 63 72 6f 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  acro...*/..SQLIT
1dafc 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1dafd 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
1dafe 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  oVacuum(Btree *p
1daff 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d  , int autoVacuum
1db00 29 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ){..#ifdef SQLIT
1db01 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1db02 4d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  M..  return SQLI
1db03 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d 0a 23 65  TE_READONLY;..#e
1db04 6c 73 65 0d 0a 20 20 42 74 53 68 61 72 65 64 20  lse..  BtShared 
1db05 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a  *pBt = p->pBt;..
1db06 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1db07 45 5f 4f 4b 3b 0d 0a 20 20 75 38 20 61 76 20 3d  E_OK;..  u8 av =
1db08 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b   (u8)autoVacuum;
1db09 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ....  sqlite3Btr
1db0a 65 65 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20 69  eeEnter(p);..  i
1db0b 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67  f( (pBt->btsFlag
1db0c 73 20 26 20 42 54 53 5f 50 41 47 45 53 49 5a 45  s & BTS_PAGESIZE
1db0d 5f 46 49 58 45 44 29 21 3d 30 20 26 26 20 28 61  _FIXED)!=0 && (a
1db0e 76 20 3f 31 3a 30 29 21 3d 70 42 74 2d 3e 61 75  v ?1:0)!=pBt->au
1db0f 74 6f 56 61 63 75 75 6d 20 29 7b 0d 0a 20 20 20  toVacuum ){..   
1db10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
1db11 44 4f 4e 4c 59 3b 0d 0a 20 20 7d 65 6c 73 65 7b  DONLY;..  }else{
1db12 0d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56  ..    pBt->autoV
1db13 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a 30 3b  acuum = av ?1:0;
1db14 0d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56  ..    pBt->incrV
1db15 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f 31  acuum = av==2 ?1
1db16 3a 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  :0;..  }..  sqli
1db17 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1db18 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
1db19 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a  .#endif..}..../*
1db1a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
1db1b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75  value of the 'au
1db1c 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65  to-vacuum' prope
1db1d 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63  rty. If auto-vac
1db1e 75 75 6d 20 69 73 20 0d 0a 2a 2a 20 65 6e 61 62  uum is ..** enab
1db1f 6c 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65  led 1 is returne
1db20 64 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0d  d. Otherwise 0..
1db21 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
1db22 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1db23 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75  treeGetAutoVacuu
1db24 6d 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 23 69  m(Btree *p){..#i
1db25 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1db26 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 72  _AUTOVACUUM..  r
1db27 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f  eturn BTREE_AUTO
1db28 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0d 0a 23 65  VACUUM_NONE;..#e
1db29 6c 73 65 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  lse..  int rc;..
1db2a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
1db2b 74 65 72 28 70 29 3b 0d 0a 20 20 72 63 20 3d 20  ter(p);..  rc = 
1db2c 28 0d 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d  (..    (!p->pBt-
1db2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 52  >autoVacuum)?BTR
1db2e 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
1db2f 4e 45 3a 0d 0a 20 20 20 20 28 21 70 2d 3e 70 42  NE:..    (!p->pB
1db30 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42  t->incrVacuum)?B
1db31 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
1db32 46 55 4c 4c 3a 0d 0a 20 20 20 20 42 54 52 45 45  FULL:..    BTREE
1db33 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52  _AUTOVACUUM_INCR
1db34 0d 0a 20 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  ..  );..  sqlite
1db35 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d  3BtreeLeave(p);.
1db36 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 23  .  return rc;..#
1db37 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a  endif..}....../*
1db38 0d 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 65 72  ..** Get a refer
1db39 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 20 6f  ence to pPage1 o
1db3a 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1db3b 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 0d  ile.  This will.
1db3c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 65  .** also acquire
1db3d 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 74   a readlock on t
1db3e 68 61 74 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a  hat file...**..*
1db3f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
1db40 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65  eturned on succe
1db41 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65  ss.  If the file
1db42 20 69 73 20 6e 6f 74 20 61 0d 0a 2a 2a 20 77 65   is not a..** we
1db43 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61  ll-formed databa
1db44 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
1db45 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20  LITE_CORRUPT is 
1db46 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 53 51  returned...** SQ
1db47 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
1db48 75 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74  urned if the dat
1db49 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e  abase is locked.
1db4a 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0d 0a    SQLITE_NOMEM..
1db4b 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
1db4c 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20  f we run out of 
1db4d 6d 65 6d 6f 72 79 2e 20 0d 0a 2a 2f 0d 0a 73 74  memory. ..*/..st
1db4e 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72  atic int lockBtr
1db4f 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ee(BtShared *pBt
1db50 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  ){..  int rc;   
1db51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1db52 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73  sult code from s
1db53 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0d 0a  ubfunctions */..
1db54 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1db55 31 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 31  1;     /* Page 1
1db56 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1db57 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   file */..  int 
1db58 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  nPage;          
1db59 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1db5a 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
1db5b 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50  ase */..  int nP
1db5c 61 67 65 46 69 6c 65 20 3d 20 30 3b 20 20 20 2f  ageFile = 0;   /
1db5d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
1db5e 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
1db5f 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74  e file */..  int
1db60 20 6e 50 61 67 65 48 65 61 64 65 72 3b 20 20 20   nPageHeader;   
1db61 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
1db62 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
1db63 62 61 73 65 20 61 63 63 6f 72 64 69 6e 67 20 74  base according t
1db64 6f 20 68 64 72 20 2a 2f 0d 0a 0d 0a 20 20 61 73  o hdr */....  as
1db65 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1db66 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1db67 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
1db68 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d  t( pBt->pPage1==
1db69 30 20 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c  0 );..  rc = sql
1db6a 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c  ite3PagerSharedL
1db6b 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ock(pBt->pPager)
1db6c 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
1db6d 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
1db6e 72 63 3b 0d 0a 20 20 72 63 20 3d 20 62 74 72 65  rc;..  rc = btre
1db6f 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c  eGetPage(pBt, 1,
1db70 20 26 70 50 61 67 65 31 2c 20 30 29 3b 0d 0a 20   &pPage1, 0);.. 
1db71 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1db72 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d  OK ) return rc;.
1db73 0a 0d 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20  ...  /* Do some 
1db74 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70  checking to help
1db75 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65   insure the file
1db76 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c   we opened reall
1db77 79 20 69 73 0d 0a 20 20 2a 2a 20 61 20 76 61 6c  y is..  ** a val
1db78 69 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  id database file
1db79 2e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 6e 50 61 67  . ..  */..  nPag
1db7a 65 20 3d 20 6e 50 61 67 65 48 65 61 64 65 72 20  e = nPageHeader 
1db7b 3d 20 67 65 74 34 62 79 74 65 28 32 38 2b 28 75  = get4byte(28+(u
1db7c 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61  8*)pPage1->aData
1db7d 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 50 61 67  );..  sqlite3Pag
1db7e 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
1db7f 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 46  >pPager, &nPageF
1db80 69 6c 65 29 3b 0d 0a 20 20 69 66 28 20 6e 50 61  ile);..  if( nPa
1db81 67 65 3d 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 28  ge==0 || memcmp(
1db82 32 34 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e  24+(u8*)pPage1->
1db83 61 44 61 74 61 2c 20 39 32 2b 28 75 38 2a 29 70  aData, 92+(u8*)p
1db84 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 34 29 21  Page1->aData,4)!
1db85 3d 30 20 29 7b 0d 0a 20 20 20 20 6e 50 61 67 65  =0 ){..    nPage
1db86 20 3d 20 6e 50 61 67 65 46 69 6c 65 3b 0d 0a 20   = nPageFile;.. 
1db87 20 7d 0d 0a 20 20 69 66 28 20 6e 50 61 67 65 3e   }..  if( nPage>
1db88 30 20 29 7b 0d 0a 20 20 20 20 75 33 32 20 70 61  0 ){..    u32 pa
1db89 67 65 53 69 7a 65 3b 0d 0a 20 20 20 20 75 33 32  geSize;..    u32
1db8a 20 75 73 61 62 6c 65 53 69 7a 65 3b 0d 0a 20 20   usableSize;..  
1db8b 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50    u8 *page1 = pP
1db8c 61 67 65 31 2d 3e 61 44 61 74 61 3b 0d 0a 20 20  age1->aData;..  
1db8d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1db8e 54 41 44 42 3b 0d 0a 20 20 20 20 69 66 28 20 6d  TADB;..    if( m
1db8f 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61  emcmp(page1, zMa
1db90 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d  gicHeader, 16)!=
1db91 30 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  0 ){..      goto
1db92 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
1db93 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69  ed;..    }....#i
1db94 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1db95 5f 57 41 4c 0d 0a 20 20 20 20 69 66 28 20 70 61  _WAL..    if( pa
1db96 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0d 0a 20 20  ge1[18]>1 ){..  
1db97 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67      pBt->btsFlag
1db98 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e  s |= BTS_READ_ON
1db99 4c 59 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  LY;..    }..    
1db9a 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31 20  if( page1[19]>1 
1db9b 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 70  ){..      goto p
1db9c 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
1db9d 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6c 73 65 0d  ;..    }..#else.
1db9e 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31  .    if( page1[1
1db9f 38 5d 3e 32 20 29 7b 0d 0a 20 20 20 20 20 20 70  8]>2 ){..      p
1dba0 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20  Bt->btsFlags |= 
1dba1 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0d 0a  BTS_READ_ONLY;..
1dba2 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 70      }..    if( p
1dba3 61 67 65 31 5b 31 39 5d 3e 32 20 29 7b 0d 0a 20  age1[19]>2 ){.. 
1dba4 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
1dba5 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0d 0a 20 20  init_failed;..  
1dba6 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66    }....    /* If
1dba7 20 74 68 65 20 77 72 69 74 65 20 76 65 72 73 69   the write versi
1dba8 6f 6e 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20  on is set to 2, 
1dba9 74 68 69 73 20 64 61 74 61 62 61 73 65 20 73 68  this database sh
1dbaa 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73 65 64  ould be accessed
1dbab 0d 0a 20 20 20 20 2a 2a 20 69 6e 20 57 41 4c 20  ..    ** in WAL 
1dbac 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 6c 6f 67  mode. If the log
1dbad 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
1dbae 6f 70 65 6e 2c 20 6f 70 65 6e 20 69 74 20 6e 6f  open, open it no
1dbaf 77 2e 20 54 68 65 6e 20 0d 0a 20 20 20 20 2a 2a  w. Then ..    **
1dbb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1dbb1 4b 20 61 6e 64 20 72 65 74 75 72 6e 20 77 69 74  K and return wit
1dbb2 68 6f 75 74 20 70 6f 70 75 6c 61 74 69 6e 67 20  hout populating 
1dbb3 42 74 53 68 61 72 65 64 2e 70 50 61 67 65 31 2e  BtShared.pPage1.
1dbb4 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c  ..    ** The cal
1dbb5 6c 65 72 20 64 65 74 65 63 74 73 20 74 68 69 73  ler detects this
1dbb6 20 61 6e 64 20 63 61 6c 6c 73 20 74 68 69 73 20   and calls this 
1dbb7 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 2e 20  function again. 
1dbb8 54 68 69 73 20 69 73 0d 0a 20 20 20 20 2a 2a 20  This is..    ** 
1dbb9 72 65 71 75 69 72 65 64 20 61 73 20 74 68 65 20  required as the 
1dbba 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20  version of page 
1dbbb 31 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  1 currently in t
1dbbc 68 65 20 70 61 67 65 31 20 62 75 66 66 65 72 0d  he page1 buffer.
1dbbd 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20  .    ** may not 
1dbbe 62 65 20 74 68 65 20 6c 61 74 65 73 74 20 76 65  be the latest ve
1dbbf 72 73 69 6f 6e 20 2d 20 74 68 65 72 65 20 6d 61  rsion - there ma
1dbc0 79 20 62 65 20 61 20 6e 65 77 65 72 20 6f 6e 65  y be a newer one
1dbc1 20 69 6e 20 74 68 65 20 6c 6f 67 0d 0a 20 20 20   in the log..   
1dbc2 20 2a 2a 20 66 69 6c 65 2e 0d 0a 20 20 20 20 2a   ** file...    *
1dbc3 2f 0d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31  /..    if( page1
1dbc4 5b 31 39 5d 3d 3d 32 20 26 26 20 28 70 42 74 2d  [19]==2 && (pBt-
1dbc5 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f  >btsFlags & BTS_
1dbc6 4e 4f 5f 57 41 4c 29 3d 3d 30 20 29 7b 0d 0a 20  NO_WAL)==0 ){.. 
1dbc7 20 20 20 20 20 69 6e 74 20 69 73 4f 70 65 6e 20       int isOpen 
1dbc8 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  = 0;..      rc =
1dbc9 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
1dbca 6e 57 61 6c 28 70 42 74 2d 3e 70 50 61 67 65 72  nWal(pBt->pPager
1dbcb 2c 20 26 69 73 4f 70 65 6e 29 3b 0d 0a 20 20 20  , &isOpen);..   
1dbcc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1dbcd 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
1dbce 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74   goto page1_init
1dbcf 5f 66 61 69 6c 65 64 3b 0d 0a 20 20 20 20 20 20  _failed;..      
1dbd0 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 6e  }else if( isOpen
1dbd1 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
1dbd2 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
1dbd3 65 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 65  e1);..        re
1dbd4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
1dbd5 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1dbd6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41  rc = SQLITE_NOTA
1dbd7 44 42 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  DB;..    }..#end
1dbd8 69 66 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 65  if....    /* The
1dbd9 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65   maximum embedde
1dbda 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20  d fraction must 
1dbdb 62 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20  be exactly 25%. 
1dbdc 20 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d   And the minimum
1dbdd 0d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65  ..    ** embedde
1dbde 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20  d fraction must 
1dbdf 62 65 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74  be 12.5% for bot
1dbe0 68 20 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20  h leaf-data and 
1dbe1 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0d 0a  non-leaf-data...
1dbe2 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69      ** The origi
1dbe3 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77  nal design allow
1dbe4 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e 74 73  ed these amounts
1dbe5 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 61 73   to vary, but as
1dbe6 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 76 65 72 73   of..    ** vers
1dbe7 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 65  ion 3.6.0, we re
1dbe8 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 65  quire them to be
1dbe9 20 66 69 78 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d   fixed...    */.
1dbea 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  .    if( memcmp(
1dbeb 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30  &page1[21], "\10
1dbec 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30  0\040\040",3)!=0
1dbed 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20   ){..      goto 
1dbee 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
1dbef 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70  d;..    }..    p
1dbf0 61 67 65 53 69 7a 65 20 3d 20 28 70 61 67 65 31  ageSize = (page1
1dbf1 5b 31 36 5d 3c 3c 38 29 20 7c 20 28 70 61 67 65  [16]<<8) | (page
1dbf2 31 5b 31 37 5d 3c 3c 31 36 29 3b 0d 0a 20 20 20  1[17]<<16);..   
1dbf3 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d   if( ((pageSize-
1dbf4 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 0d  1)&pageSize)!=0.
1dbf5 0a 20 20 20 20 20 7c 7c 20 70 61 67 65 53 69 7a  .     || pageSiz
1dbf6 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  e>SQLITE_MAX_PAG
1dbf7 45 5f 53 49 5a 45 20 0d 0a 20 20 20 20 20 7c 7c  E_SIZE ..     ||
1dbf8 20 70 61 67 65 53 69 7a 65 3c 3d 32 35 36 20 0d   pageSize<=256 .
1dbf9 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 67  .    ){..      g
1dbfa 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
1dbfb 61 69 6c 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ailed;..    }.. 
1dbfc 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65     assert( (page
1dbfd 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0d  Size & 7)==0 );.
1dbfe 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20  .    usableSize 
1dbff 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67  = pageSize - pag
1dc00 65 31 5b 32 30 5d 3b 0d 0a 20 20 20 20 69 66 28  e1[20];..    if(
1dc01 20 28 75 33 32 29 70 61 67 65 53 69 7a 65 21 3d   (u32)pageSize!=
1dc02 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b  pBt->pageSize ){
1dc03 0d 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72  ..      /* After
1dc04 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 69 72   reading the fir
1dc05 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64  st page of the d
1dc06 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67  atabase assuming
1dc07 20 61 20 70 61 67 65 20 73 69 7a 65 0d 0a 20 20   a page size..  
1dc08 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72      ** of BtShar
1dc09 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20  ed.pageSize, we 
1dc0a 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 20  have discovered 
1dc0b 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69  that the page-si
1dc0c 7a 65 20 69 73 0d 0a 20 20 20 20 20 20 2a 2a 20  ze is..      ** 
1dc0d 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a  actually pageSiz
1dc0e 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  e. Unlock the da
1dc0f 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42  tabase, leave pB
1dc10 74 2d 3e 70 50 61 67 65 31 20 61 74 0d 0a 20 20  t->pPage1 at..  
1dc11 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20      ** zero and 
1dc12 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1dc13 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  . The caller wil
1dc14 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  l call this func
1dc15 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 61  tion..      ** a
1dc16 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f  gain with the co
1dc17 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e  rrect page-size.
1dc18 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
1dc19 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
1dc1a 61 67 65 31 29 3b 0d 0a 20 20 20 20 20 20 70 42  age1);..      pB
1dc1b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  t->usableSize = 
1dc1c 75 73 61 62 6c 65 53 69 7a 65 3b 0d 0a 20 20 20  usableSize;..   
1dc1d 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65     pBt->pageSize
1dc1e 20 3d 20 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20   = pageSize;..  
1dc1f 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63      freeTempSpac
1dc20 65 28 70 42 74 29 3b 0d 0a 20 20 20 20 20 20 72  e(pBt);..      r
1dc21 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1dc22 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d  SetPagesize(pBt-
1dc23 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70  >pPager, &pBt->p
1dc24 61 67 65 53 69 7a 65 2c 0d 0a 20 20 20 20 20 20  ageSize,..      
1dc25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc26 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
1dc27 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65  eSize-usableSize
1dc28 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  );..      return
1dc29 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20   rc;..    }..   
1dc2a 20 69 66 28 20 28 70 42 74 2d 3e 64 62 2d 3e 66   if( (pBt->db->f
1dc2b 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65  lags & SQLITE_Re
1dc2c 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 20 26  coveryMode)==0 &
1dc2d 26 20 6e 50 61 67 65 3e 6e 50 61 67 65 46 69 6c  & nPage>nPageFil
1dc2e 65 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  e ){..      rc =
1dc2f 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1dc30 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74  BKPT;..      got
1dc31 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
1dc32 6c 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  led;..    }..   
1dc33 20 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c   if( usableSize<
1dc34 34 38 30 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f  480 ){..      go
1dc35 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  to page1_init_fa
1dc36 69 6c 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  iled;..    }..  
1dc37 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
1dc38 3d 20 70 61 67 65 53 69 7a 65 3b 0d 0a 20 20 20  = pageSize;..   
1dc39 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
1dc3a 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0d 0a   = usableSize;..
1dc3b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1dc3c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a  MIT_AUTOVACUUM..
1dc3d 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
1dc3e 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
1dc3f 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d  &page1[36 + 4*4]
1dc40 29 3f 31 3a 30 29 3b 0d 0a 20 20 20 20 70 42 74  )?1:0);..    pBt
1dc41 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
1dc42 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b  get4byte(&page1[
1dc43 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 7*4])?1:0);
1dc44 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d  ..#endif..  }...
1dc45 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69  .  /* maxLocal i
1dc46 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  s the maximum am
1dc47 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
1dc48 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79  to store locally
1dc49 20 66 6f 72 0d 0a 20 20 2a 2a 20 61 20 63 65 6c   for..  ** a cel
1dc4a 6c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74  l.  Make sure it
1dc4b 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68   is small enough
1dc4c 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73   so that at leas
1dc4d 74 20 6d 69 6e 46 61 6e 6f 75 74 0d 0a 20 20 2a  t minFanout..  *
1dc4e 2a 20 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c  * cells can will
1dc4f 20 66 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65   fit on one page
1dc50 2e 20 20 57 65 20 61 73 73 75 6d 65 20 61 20 31  .  We assume a 1
1dc51 30 2d 62 79 74 65 20 70 61 67 65 20 68 65 61 64  0-byte page head
1dc52 65 72 2e 0d 0a 20 20 2a 2a 20 42 65 73 69 64 65  er...  ** Beside
1dc53 73 20 74 68 65 20 70 61 79 6c 6f 61 64 2c 20 74  s the payload, t
1dc54 68 65 20 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f  he cell must sto
1dc55 72 65 3a 0d 0a 20 20 2a 2a 20 20 20 20 20 32 2d  re:..  **     2-
1dc56 62 79 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20  byte pointer to 
1dc57 74 68 65 20 63 65 6c 6c 0d 0a 20 20 2a 2a 20 20  the cell..  **  
1dc58 20 20 20 34 2d 62 79 74 65 20 63 68 69 6c 64 20     4-byte child 
1dc59 70 6f 69 6e 74 65 72 0d 0a 20 20 2a 2a 20 20 20  pointer..  **   
1dc5a 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 76 61    9-byte nKey va
1dc5b 6c 75 65 0d 0a 20 20 2a 2a 20 20 20 20 20 34 2d  lue..  **     4-
1dc5c 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 65  byte nData value
1dc5d 0d 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74  ..  **     4-byt
1dc5e 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  e overflow page 
1dc5f 70 6f 69 6e 74 65 72 0d 0a 20 20 2a 2a 20 53 6f  pointer..  ** So
1dc60 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73   a cell consists
1dc61 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69   of a 2-byte poi
1dc62 6e 74 65 72 2c 20 61 20 68 65 61 64 65 72 20 77  nter, a header w
1dc63 68 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20  hich is as much 
1dc64 61 73 0d 0a 20 20 2a 2a 20 31 37 20 62 79 74 65  as..  ** 17 byte
1dc65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62  s long, 0 to N b
1dc66 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c  ytes of payload,
1dc67 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c   and an optional
1dc68 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77   4 byte overflow
1dc69 0d 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e  ..  ** page poin
1dc6a 74 65 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 42  ter...  */..  pB
1dc6b 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 75  t->maxLocal = (u
1dc6c 31 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c 65  16)((pBt->usable
1dc6d 53 69 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35 20  Size-12)*64/255 
1dc6e 2d 20 32 33 29 3b 0d 0a 20 20 70 42 74 2d 3e 6d  - 23);..  pBt->m
1dc6f 69 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 28  inLocal = (u16)(
1dc70 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
1dc71 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33  -12)*32/255 - 23
1dc72 29 3b 0d 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65  );..  pBt->maxLe
1dc73 61 66 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e  af = (u16)(pBt->
1dc74 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 33 35 29  usableSize - 35)
1dc75 3b 0d 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 61  ;..  pBt->minLea
1dc76 66 20 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e  f = (u16)((pBt->
1dc77 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33  usableSize-12)*3
1dc78 32 2f 32 35 35 20 2d 20 32 33 29 3b 0d 0a 20 20  2/255 - 23);..  
1dc79 69 66 28 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61  if( pBt->maxLoca
1dc7a 6c 3e 31 32 37 20 29 7b 0d 0a 20 20 20 20 70 42  l>127 ){..    pB
1dc7b 74 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c 6f  t->max1bytePaylo
1dc7c 61 64 20 3d 20 31 32 37 3b 0d 0a 20 20 7d 65 6c  ad = 127;..  }el
1dc7d 73 65 7b 0d 0a 20 20 20 20 70 42 74 2d 3e 6d 61  se{..    pBt->ma
1dc7e 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d 20  x1bytePayload = 
1dc7f 28 75 38 29 70 42 74 2d 3e 6d 61 78 4c 6f 63 61  (u8)pBt->maxLoca
1dc80 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  l;..  }..  asser
1dc81 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20  t( pBt->maxLeaf 
1dc82 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f  + 23 <= MX_CELL_
1dc83 53 49 5a 45 28 70 42 74 29 20 29 3b 0d 0a 20 20  SIZE(pBt) );..  
1dc84 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50  pBt->pPage1 = pP
1dc85 61 67 65 31 3b 0d 0a 20 20 70 42 74 2d 3e 6e 50  age1;..  pBt->nP
1dc86 61 67 65 20 3d 20 6e 50 61 67 65 3b 0d 0a 20 20  age = nPage;..  
1dc87 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1dc88 3b 0d 0a 0d 0a 70 61 67 65 31 5f 69 6e 69 74 5f  ;....page1_init_
1dc89 66 61 69 6c 65 64 3a 0d 0a 20 20 72 65 6c 65 61  failed:..  relea
1dc8a 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0d  sePage(pPage1);.
1dc8b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d  .  pBt->pPage1 =
1dc8c 20 30 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63   0;..  return rc
1dc8d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
1dc8e 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f  f there are no o
1dc8f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f  utstanding curso
1dc90 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f  rs and we are no
1dc91 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 0d  t in the middle.
1dc92 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63  .** of a transac
1dc93 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69  tion but there i
1dc94 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  s a read lock on
1dc95 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
1dc96 68 65 6e 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75  hen..** this rou
1dc97 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20  tine unrefs the 
1dc98 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
1dc99 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1dc9a 77 68 69 63 68 20 0d 0a 2a 2a 20 68 61 73 20 74  which ..** has t
1dc9b 68 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c  he effect of rel
1dc9c 65 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20  easing the read 
1dc9d 6c 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  lock...**..** If
1dc9e 20 74 68 65 72 65 20 69 73 20 61 20 74 72 61 6e   there is a tran
1dc9f 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72  saction in progr
1dca0 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ess, this routin
1dca1 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  e is a no-op...*
1dca2 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  /..static void u
1dca3 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
1dca4 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ed(BtShared *pBt
1dca5 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ){..  assert( sq
1dca6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1dca7 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  (pBt->mutex) );.
1dca8 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
1dca9 70 43 75 72 73 6f 72 3d 3d 30 20 7c 7c 20 70 42  pCursor==0 || pB
1dcaa 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1dcab 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0d 0a  >TRANS_NONE );..
1dcac 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61    if( pBt->inTra
1dcad 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
1dcae 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 50 61  NONE && pBt->pPa
1dcaf 67 65 31 21 3d 30 20 29 7b 0d 0a 20 20 20 20 61  ge1!=0 ){..    a
1dcb0 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
1dcb1 65 31 2d 3e 61 44 61 74 61 20 29 3b 0d 0a 20 20  e1->aData );..  
1dcb2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1dcb3 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
1dcb4 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 29  Bt->pPager)==1 )
1dcb5 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
1dcb6 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1dcb7 61 20 29 3b 0d 0a 20 20 20 20 72 65 6c 65 61 73  a );..    releas
1dcb8 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65  ePage(pBt->pPage
1dcb9 31 29 3b 0d 0a 20 20 20 20 70 42 74 2d 3e 70 50  1);..    pBt->pP
1dcba 61 67 65 31 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  age1 = 0;..  }..
1dcbb 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 70  }..../*..** If p
1dcbc 42 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  Bt points to an 
1dcbd 65 6d 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20  empty file then 
1dcbe 63 6f 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70  convert that emp
1dcbf 74 79 20 66 69 6c 65 0d 0a 2a 2a 20 69 6e 74 6f  ty file..** into
1dcc0 20 61 20 6e 65 77 20 65 6d 70 74 79 20 64 61 74   a new empty dat
1dcc1 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c  abase by initial
1dcc2 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  izing the first 
1dcc3 70 61 67 65 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  page of..** the 
1dcc4 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 73  database...*/..s
1dcc5 74 61 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74  tatic int newDat
1dcc6 61 62 61 73 65 28 42 74 53 68 61 72 65 64 20 2a  abase(BtShared *
1dcc7 70 42 74 29 7b 0d 0a 20 20 4d 65 6d 50 61 67 65  pBt){..  MemPage
1dcc8 20 2a 70 50 31 3b 0d 0a 20 20 75 6e 73 69 67 6e   *pP1;..  unsign
1dcc9 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 0d 0a  ed char *data;..
1dcca 20 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 61    int rc;....  a
1dccb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1dccc 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1dccd 75 74 65 78 29 20 29 3b 0d 0a 20 20 69 66 28 20  utex) );..  if( 
1dcce 70 42 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0d  pBt->nPage>0 ){.
1dccf 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1dcd0 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 70  TE_OK;..  }..  p
1dcd1 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31  P1 = pBt->pPage1
1dcd2 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 31  ;..  assert( pP1
1dcd3 21 3d 30 20 29 3b 0d 0a 20 20 64 61 74 61 20 3d  !=0 );..  data =
1dcd4 20 70 50 31 2d 3e 61 44 61 74 61 3b 0d 0a 20 20   pP1->aData;..  
1dcd5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1dcd6 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50  rWrite(pP1->pDbP
1dcd7 61 67 65 29 3b 0d 0a 20 20 69 66 28 20 72 63 20  age);..  if( rc 
1dcd8 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20  ) return rc;..  
1dcd9 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61  memcpy(data, zMa
1dcda 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f  gicHeader, sizeo
1dcdb 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29  f(zMagicHeader))
1dcdc 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  ;..  assert( siz
1dcdd 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72  eof(zMagicHeader
1dcde 29 3d 3d 31 36 20 29 3b 0d 0a 20 20 64 61 74 61  )==16 );..  data
1dcdf 5b 31 36 5d 20 3d 20 28 75 38 29 28 28 70 42 74  [16] = (u8)((pBt
1dce0 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 38 29 26 30  ->pageSize>>8)&0
1dce1 78 66 66 29 3b 0d 0a 20 20 64 61 74 61 5b 31 37  xff);..  data[17
1dce2 5d 20 3d 20 28 75 38 29 28 28 70 42 74 2d 3e 70  ] = (u8)((pBt->p
1dce3 61 67 65 53 69 7a 65 3e 3e 31 36 29 26 30 78 66  ageSize>>16)&0xf
1dce4 66 29 3b 0d 0a 20 20 64 61 74 61 5b 31 38 5d 20  f);..  data[18] 
1dce5 3d 20 31 3b 0d 0a 20 20 64 61 74 61 5b 31 39 5d  = 1;..  data[19]
1dce6 20 3d 20 31 3b 0d 0a 20 20 61 73 73 65 72 74 28   = 1;..  assert(
1dce7 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
1dce8 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  <=pBt->pageSize 
1dce9 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  && pBt->usableSi
1dcea 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 67  ze+255>=pBt->pag
1dceb 65 53 69 7a 65 29 3b 0d 0a 20 20 64 61 74 61 5b  eSize);..  data[
1dcec 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e  20] = (u8)(pBt->
1dced 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e  pageSize - pBt->
1dcee 75 73 61 62 6c 65 53 69 7a 65 29 3b 0d 0a 20 20  usableSize);..  
1dcef 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0d 0a  data[21] = 64;..
1dcf0 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b    data[22] = 32;
1dcf1 0d 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33  ..  data[23] = 3
1dcf2 32 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 64 61  2;..  memset(&da
1dcf3 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32  ta[24], 0, 100-2
1dcf4 34 29 3b 0d 0a 20 20 7a 65 72 6f 50 61 67 65 28  4);..  zeroPage(
1dcf5 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c  pP1, PTF_INTKEY|
1dcf6 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41  PTF_LEAF|PTF_LEA
1dcf7 46 44 41 54 41 20 29 3b 0d 0a 20 20 70 42 74 2d  FDATA );..  pBt-
1dcf8 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53  >btsFlags |= BTS
1dcf9 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b  _PAGESIZE_FIXED;
1dcfa 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1dcfb 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1dcfc 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ..  assert( pBt-
1dcfd 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c  >autoVacuum==1 |
1dcfe 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  | pBt->autoVacuu
1dcff 6d 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  m==0 );..  asser
1dd00 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  t( pBt->incrVacu
1dd01 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e  um==1 || pBt->in
1dd02 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0d 0a  crVacuum==0 );..
1dd03 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61    put4byte(&data
1dd04 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d  [36 + 4*4], pBt-
1dd05 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0d 0a 20  >autoVacuum);.. 
1dd06 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
1dd07 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e  36 + 7*4], pBt->
1dd08 69 6e 63 72 56 61 63 75 75 6d 29 3b 0d 0a 23 65  incrVacuum);..#e
1dd09 6e 64 69 66 0d 0a 20 20 70 42 74 2d 3e 6e 50 61  ndif..  pBt->nPa
1dd0a 67 65 20 3d 20 31 3b 0d 0a 20 20 64 61 74 61 5b  ge = 1;..  data[
1dd0b 33 31 5d 20 3d 20 31 3b 0d 0a 20 20 72 65 74 75  31] = 1;..  retu
1dd0c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
1dd0d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 74 65 6d  ..../*..** Attem
1dd0e 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65  pt to start a ne
1dd0f 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  w transaction. A
1dd10 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
1dd11 6f 6e 0d 0a 2a 2a 20 69 73 20 73 74 61 72 74 65  on..** is starte
1dd12 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  d if the second 
1dd13 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a  argument is nonz
1dd14 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ero, otherwise a
1dd15 20 72 65 61 64 2d 0d 0a 2a 2a 20 74 72 61 6e 73   read-..** trans
1dd16 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20  action.  If the 
1dd17 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
1dd18 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64  is 2 or more and
1dd19 20 65 78 63 6c 75 73 69 76 65 0d 0a 2a 2a 20 74   exclusive..** t
1dd1a 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
1dd1b 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74  arted, meaning t
1dd1c 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f  hat no other pro
1dd1d 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0d  cess is allowed.
1dd1e 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68  .** to access th
1dd1f 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70  e database.  A p
1dd20 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73  reexisting trans
1dd21 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62  action may not b
1dd22 65 0d 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74  e..** upgraded t
1dd23 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63  o exclusive by c
1dd24 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
1dd25 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d  ine a second tim
1dd26 65 20 2d 20 74 68 65 0d 0a 2a 2a 20 65 78 63 6c  e - the..** excl
1dd27 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c  usivity flag onl
1dd28 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65  y works for a ne
1dd29 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a  w transaction...
1dd2a 2a 2a 0d 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74  **..** A write-t
1dd2b 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1dd2c 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72  be started befor
1dd2d 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79  e attempting any
1dd2e 20 0d 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f   ..** changes to
1dd2f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
1dd30 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  None of the foll
1dd31 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0d  owing routines .
1dd32 0a 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e  .** will work un
1dd33 6c 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69  less a transacti
1dd34 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69  on is started fi
1dd35 72 73 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  rst:..**..**    
1dd36 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72    sqlite3BtreeCr
1dd37 65 61 74 65 54 61 62 6c 65 28 29 0d 0a 2a 2a 20  eateTable()..** 
1dd38 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1dd39 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 0d 0a  eCreateIndex()..
1dd3a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
1dd3b 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29  treeClearTable()
1dd3c 0d 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ..**      sqlite
1dd3d 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
1dd3e 29 0d 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  )..**      sqlit
1dd3f 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0d  e3BtreeInsert().
1dd40 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
1dd41 42 74 72 65 65 44 65 6c 65 74 65 28 29 0d 0a 2a  BtreeDelete()..*
1dd42 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  *      sqlite3Bt
1dd43 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0d  reeUpdateMeta().
1dd44 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  .**..** If an in
1dd45 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f  itial attempt to
1dd46 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63   acquire the loc
1dd47 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20  k fails because 
1dd48 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  of lock contenti
1dd49 6f 6e 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  on..** and the d
1dd4a 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76  atabase was prev
1dd4b 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c  iously unlocked,
1dd4c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65   then invoke the
1dd4d 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0d 0a 2a   busy handler..*
1dd4e 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e  * if there is on
1dd4f 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65  e.  But if there
1dd50 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
1dd51 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20  a read-lock, do 
1dd52 6e 6f 74 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  not..** invoke t
1dd53 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
1dd54 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51  - just return SQ
1dd55 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49  LITE_BUSY.  SQLI
1dd56 54 45 5f 42 55 53 59 20 69 73 20 0d 0a 2a 2a 20  TE_BUSY is ..** 
1dd57 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68  returned when th
1dd58 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
1dd59 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72   read-lock in or
1dd5a 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64  der to avoid a d
1dd5b 65 61 64 6c 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  eadlock...**..**
1dd5c 20 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61   Suppose there a
1dd5d 72 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73  re two processes
1dd5e 20 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73   A and B.  A has
1dd5f 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64   a read lock and
1dd60 20 42 20 68 61 73 0d 0a 2a 2a 20 61 20 72 65 73   B has..** a res
1dd61 65 72 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74  erved lock.  B t
1dd62 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20  ries to promote 
1dd63 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 75 74  to exclusive but
1dd64 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61   is blocked beca
1dd65 75 73 65 0d 0a 2a 2a 20 6f 66 20 41 27 73 20 72  use..** of A's r
1dd66 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69  ead lock.  A tri
1dd67 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  es to promote to
1dd68 20 72 65 73 65 72 76 65 64 20 62 75 74 20 69 73   reserved but is
1dd69 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 2e 0d 0a   blocked by B...
1dd6a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 6f 74  ** One or the ot
1dd6b 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 70  her of the two p
1dd6c 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 67 69  rocesses must gi
1dd6d 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 65 20  ve way or there 
1dd6e 63 61 6e 20 62 65 0d 0a 2a 2a 20 6e 6f 20 70 72  can be..** no pr
1dd6f 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 75  ogress.  By retu
1dd70 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 53  rning SQLITE_BUS
1dd71 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b 69  Y and not invoki
1dd72 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ng the busy call
1dd73 62 61 63 6b 0d 0a 2a 2a 20 77 68 65 6e 20 41 20  back..** when A 
1dd74 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65  already has a re
1dd75 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f  ad lock, we enco
1dd76 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20  urage A to give 
1dd77 75 70 20 61 6e 64 20 6c 65 74 20 42 0d 0a 2a 2a  up and let B..**
1dd78 20 70 72 6f 63 65 65 64 2e 0d 0a 2a 2f 0d 0a 53   proceed...*/..S
1dd79 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1dd7a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  t sqlite3BtreeBe
1dd7b 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a  ginTrans(Btree *
1dd7c 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0d  p, int wrflag){.
1dd7d 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f  .  sqlite3 *pBlo
1dd7e 63 6b 20 3d 20 30 3b 0d 0a 20 20 42 74 53 68 61  ck = 0;..  BtSha
1dd7f 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1dd80 74 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t;..  int rc = S
1dd81 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 73  QLITE_OK;....  s
1dd82 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1dd83 28 70 29 3b 0d 0a 20 20 62 74 72 65 65 49 6e 74  (p);..  btreeInt
1dd84 65 67 72 69 74 79 28 70 29 3b 0d 0a 0d 0a 20 20  egrity(p);....  
1dd85 2f 2a 20 49 66 20 74 68 65 20 62 74 72 65 65 20  /* If the btree 
1dd86 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20  is already in a 
1dd87 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1dd88 6e 2c 20 6f 72 20 69 74 0d 0a 20 20 2a 2a 20 69  n, or it..  ** i
1dd89 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 72  s already in a r
1dd8a 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ead-transaction 
1dd8b 61 6e 64 20 61 20 72 65 61 64 2d 74 72 61 6e 73  and a read-trans
1dd8c 61 63 74 69 6f 6e 0d 0a 20 20 2a 2a 20 69 73 20  action..  ** is 
1dd8d 72 65 71 75 65 73 74 65 64 2c 20 74 68 69 73 20  requested, this 
1dd8e 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 20 20 2a  is a no-op...  *
1dd8f 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  /..  if( p->inTr
1dd90 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1dd91 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d   || (p->inTrans=
1dd92 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21  =TRANS_READ && !
1dd93 77 72 66 6c 61 67 29 20 29 7b 0d 0a 20 20 20 20  wrflag) ){..    
1dd94 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
1dd95 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 57  ;..  }....  /* W
1dd96 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1dd97 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62  s are not possib
1dd98 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c  le on a read-onl
1dd99 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20  y database */.. 
1dd9a 20 69 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c   if( (pBt->btsFl
1dd9b 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f 4f  ags & BTS_READ_O
1dd9c 4e 4c 59 29 21 3d 30 20 26 26 20 77 72 66 6c 61  NLY)!=0 && wrfla
1dd9d 67 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  g ){..    rc = S
1dd9e 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d  QLITE_READONLY;.
1dd9f 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f  .    goto trans_
1dda0 62 65 67 75 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23  begun;..  }....#
1dda1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1dda2 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d  IT_SHARED_CACHE.
1dda3 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72  .  /* If another
1dda4 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1dda5 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65   has already ope
1dda6 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61 6e  ned a write tran
1dda7 73 61 63 74 69 6f 6e 20 0d 0a 20 20 2a 2a 20 6f  saction ..  ** o
1dda8 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74  n this shared-bt
1dda9 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
1ddaa 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65  d a second write
1ddab 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0d   transaction is.
1ddac 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c  .  ** requested,
1ddad 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
1ddae 4f 43 4b 45 44 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  OCKED...  */..  
1ddaf 69 66 28 20 28 77 72 66 6c 61 67 20 26 26 20 70  if( (wrflag && p
1ddb0 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1ddb1 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 0d  n==TRANS_WRITE).
1ddb2 0a 20 20 20 7c 7c 20 28 70 42 74 2d 3e 62 74 73  .   || (pBt->bts
1ddb3 46 6c 61 67 73 20 26 20 42 54 53 5f 50 45 4e 44  Flags & BTS_PEND
1ddb4 49 4e 47 29 21 3d 30 0d 0a 20 20 29 7b 0d 0a 20  ING)!=0..  ){.. 
1ddb5 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 74 2d     pBlock = pBt-
1ddb6 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0d 0a 20  >pWriter->db;.. 
1ddb7 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c 61   }else if( wrfla
1ddb8 67 3e 31 20 29 7b 0d 0a 20 20 20 20 42 74 4c 6f  g>1 ){..    BtLo
1ddb9 63 6b 20 2a 70 49 74 65 72 3b 0d 0a 20 20 20 20  ck *pIter;..    
1ddba 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70  for(pIter=pBt->p
1ddbb 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74  Lock; pIter; pIt
1ddbc 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29  er=pIter->pNext)
1ddbd 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  {..      if( pIt
1ddbe 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b  er->pBtree!=p ){
1ddbf 0d 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b  ..        pBlock
1ddc0 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65   = pIter->pBtree
1ddc1 2d 3e 64 62 3b 0d 0a 20 20 20 20 20 20 20 20 62  ->db;..        b
1ddc2 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
1ddc3 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
1ddc4 28 20 70 42 6c 6f 63 6b 20 29 7b 0d 0a 20 20 20  ( pBlock ){..   
1ddc5 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
1ddc6 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c  onBlocked(p->db,
1ddc7 20 70 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20 20 72   pBlock);..    r
1ddc8 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  c = SQLITE_LOCKE
1ddc9 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0d 0a  D_SHAREDCACHE;..
1ddca 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62      goto trans_b
1ddcb 65 67 75 6e 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  egun;..  }..#end
1ddcc 69 66 0d 0a 0d 0a 20 20 2f 2a 20 41 6e 79 20 72  if....  /* Any r
1ddcd 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64  ead-only or read
1ddce 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69  -write transacti
1ddcf 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 72 65 61  on implies a rea
1ddd0 64 2d 6c 6f 63 6b 20 6f 6e 20 0d 0a 20 20 2a 2a  d-lock on ..  **
1ddd1 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 20 73   page 1. So if s
1ddd2 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 65 64  ome other shared
1ddd3 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 61 6c  -cache client al
1ddd4 72 65 61 64 79 20 68 61 73 20 61 20 77 72 69 74  ready has a writ
1ddd5 65 2d 6c 6f 63 6b 20 0d 0a 20 20 2a 2a 20 6f 6e  e-lock ..  ** on
1ddd6 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 61   page 1, the tra
1ddd7 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  nsaction cannot 
1ddd8 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0d 0a 20  be opened. */.. 
1ddd9 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65   rc = queryShare
1ddda 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
1dddb 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  p, MASTER_ROOT, 
1dddc 52 45 41 44 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 69  READ_LOCK);..  i
1dddd 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63  f( SQLITE_OK!=rc
1ddde 20 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65   ) goto trans_be
1dddf 67 75 6e 3b 0d 0a 0d 0a 20 20 70 42 74 2d 3e 62  gun;....  pBt->b
1dde0 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f  tsFlags &= ~BTS_
1dde1 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b  INITIALLY_EMPTY;
1dde2 0d 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 50 61  ..  if( pBt->nPa
1dde3 67 65 3d 3d 30 20 29 20 70 42 74 2d 3e 62 74 73  ge==0 ) pBt->bts
1dde4 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 49 4e 49  Flags |= BTS_INI
1dde5 54 49 41 4c 4c 59 5f 45 4d 50 54 59 3b 0d 0a 20  TIALLY_EMPTY;.. 
1dde6 20 64 6f 20 7b 0d 0a 20 20 20 20 2f 2a 20 43 61   do {..    /* Ca
1dde7 6c 6c 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 75  ll lockBtree() u
1dde8 6e 74 69 6c 20 65 69 74 68 65 72 20 70 42 74 2d  ntil either pBt-
1dde9 3e 70 50 61 67 65 31 20 69 73 20 70 6f 70 75 6c  >pPage1 is popul
1ddea 61 74 65 64 20 6f 72 0d 0a 20 20 20 20 2a 2a 20  ated or..    ** 
1ddeb 6c 6f 63 6b 42 74 72 65 65 28 29 20 72 65 74 75  lockBtree() retu
1ddec 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74  rns something ot
1dded 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
1ddee 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 65 28 29 0d  OK. lockBtree().
1ddef 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 65 74 75  .    ** may retu
1ddf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 62 75 74  rn SQLITE_OK but
1ddf1 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67   leave pBt->pPag
1ddf2 65 31 20 73 65 74 20 74 6f 20 30 20 69 66 20 61  e1 set to 0 if a
1ddf3 66 74 65 72 0d 0a 20 20 20 20 2a 2a 20 72 65 61  fter..    ** rea
1ddf4 64 69 6e 67 20 70 61 67 65 20 31 20 69 74 20 64  ding page 1 it d
1ddf5 69 73 63 6f 76 65 72 73 20 74 68 61 74 20 74 68  iscovers that th
1ddf6 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74  e page-size of t
1ddf7 68 65 20 64 61 74 61 62 61 73 65 20 0d 0a 20 20  he database ..  
1ddf8 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f 74    ** file is not
1ddf9 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e 20   pBt->pageSize. 
1ddfa 49 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f 63  In this case loc
1ddfb 6b 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75 70  kBtree() will up
1ddfc 64 61 74 65 0d 0a 20 20 20 20 2a 2a 20 70 42 74  date..    ** pBt
1ddfd 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68  ->pageSize to th
1ddfe 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74  e page-size of t
1ddff 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
1de00 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 77 68  ..    */..    wh
1de01 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ile( pBt->pPage1
1de02 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  ==0 && SQLITE_OK
1de03 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65  ==(rc = lockBtre
1de04 65 28 70 42 74 29 29 20 29 3b 0d 0a 0d 0a 20 20  e(pBt)) );....  
1de05 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1de06 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
1de07 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70 42 74  ..      if( (pBt
1de08 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1de09 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 29  _READ_ONLY)!=0 )
1de0a 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
1de0b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
1de0c 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
1de0d 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1de0e 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70  ite3PagerBegin(p
1de0f 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 66 6c 61  Bt->pPager,wrfla
1de10 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 6d 70 49  g>1,sqlite3TempI
1de11 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 29 29 3b  nMemory(p->db));
1de12 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ..        if( rc
1de13 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1de14 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e            rc = n
1de15 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 3b  ewDatabase(pBt);
1de16 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
1de17 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d     }..    }..  .
1de18 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1de19 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1de1a 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
1de1b 75 73 65 64 28 70 42 74 29 3b 0d 0a 20 20 20 20  used(pBt);..    
1de1c 7d 0d 0a 20 20 7d 77 68 69 6c 65 28 20 28 72 63  }..  }while( (rc
1de1d 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 42  &0xFF)==SQLITE_B
1de1e 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72  USY && pBt->inTr
1de1f 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1de20 5f 4e 4f 4e 45 20 26 26 0d 0a 20 20 20 20 20 20  _NONE &&..      
1de21 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42      btreeInvokeB
1de22 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20  usyHandler(pBt) 
1de23 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d  );....  if( rc==
1de24 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1de25 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
1de26 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0d  ==TRANS_NONE ){.
1de27 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61  .      pBt->nTra
1de28 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0d 0a 23 69 66  nsaction++;..#if
1de29 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1de2a 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 20  _SHARED_CACHE.. 
1de2b 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72       if( p->shar
1de2c 61 62 6c 65 20 29 7b 0d 0a 09 61 73 73 65 72 74  able ){...assert
1de2d 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65  ( p->lock.pBtree
1de2e 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69  ==p && p->lock.i
1de2f 54 61 62 6c 65 3d 3d 31 20 29 3b 0d 0a 20 20 20  Table==1 );..   
1de30 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f       p->lock.eLo
1de31 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0d  ck = READ_LOCK;.
1de32 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b  .        p->lock
1de33 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c  .pNext = pBt->pL
1de34 6f 63 6b 3b 0d 0a 20 20 20 20 20 20 20 20 70 42  ock;..        pB
1de35 74 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c  t->pLock = &p->l
1de36 6f 63 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23  ock;..      }..#
1de37 65 6e 64 69 66 0d 0a 20 20 20 20 7d 0d 0a 20 20  endif..    }..  
1de38 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 28    p->inTrans = (
1de39 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 49  wrflag?TRANS_WRI
1de3a 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b 0d  TE:TRANS_READ);.
1de3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  .    if( p->inTr
1de3c 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73  ans>pBt->inTrans
1de3d 61 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20 20 20  action ){..     
1de3e 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1de3f 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73  ion = p->inTrans
1de40 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
1de41 28 20 77 72 66 6c 61 67 20 29 7b 0d 0a 20 20 20  ( wrflag ){..   
1de42 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67     MemPage *pPag
1de43 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31  e1 = pBt->pPage1
1de44 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1de45 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1de46 43 48 45 0d 0a 20 20 20 20 20 20 61 73 73 65 72  CHE..      asser
1de47 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72  t( !pBt->pWriter
1de48 20 29 3b 0d 0a 20 20 20 20 20 20 70 42 74 2d 3e   );..      pBt->
1de49 70 57 72 69 74 65 72 20 3d 20 70 3b 0d 0a 20 20  pWriter = p;..  
1de4a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67      pBt->btsFlag
1de4b 73 20 26 3d 20 7e 42 54 53 5f 45 58 43 4c 55 53  s &= ~BTS_EXCLUS
1de4c 49 56 45 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  IVE;..      if( 
1de4d 77 72 66 6c 61 67 3e 31 20 29 20 70 42 74 2d 3e  wrflag>1 ) pBt->
1de4e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f  btsFlags |= BTS_
1de4f 45 58 43 4c 55 53 49 56 45 3b 0d 0a 23 65 6e 64  EXCLUSIVE;..#end
1de50 69 66 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49  if....      /* I
1de51 66 20 74 68 65 20 64 62 2d 73 69 7a 65 20 68 65  f the db-size he
1de52 61 64 65 72 20 66 69 65 6c 64 20 69 73 20 69 6e  ader field is in
1de53 63 6f 72 72 65 63 74 20 28 61 73 20 69 74 20 6d  correct (as it m
1de54 61 79 20 62 65 20 69 66 20 61 6e 20 6f 6c 64 0d  ay be if an old.
1de55 0a 20 20 20 20 20 20 2a 2a 20 63 6c 69 65 6e 74  .      ** client
1de56 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 69 6e   has been writin
1de57 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  g the database f
1de58 69 6c 65 29 2c 20 75 70 64 61 74 65 20 69 74 20  ile), update it 
1de59 6e 6f 77 2e 20 44 6f 69 6e 67 0d 0a 20 20 20 20  now. Doing..    
1de5a 20 20 2a 2a 20 74 68 69 73 20 73 6f 6f 6e 65 72    ** this sooner
1de5b 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74   rather than lat
1de5c 65 72 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74  er means the dat
1de5d 61 62 61 73 65 20 73 69 7a 65 20 63 61 6e 20 73  abase size can s
1de5e 61 66 65 6c 79 20 0d 0a 20 20 20 20 20 20 2a 2a  afely ..      **
1de5f 20 72 65 2d 72 65 61 64 20 74 68 65 20 64 61 74   re-read the dat
1de60 61 62 61 73 65 20 73 69 7a 65 20 66 72 6f 6d 20  abase size from 
1de61 70 61 67 65 20 31 20 69 66 20 61 20 73 61 76 65  page 1 if a save
1de62 70 6f 69 6e 74 20 6f 72 20 74 72 61 6e 73 61 63  point or transac
1de63 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 72  tion..      ** r
1de64 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 77  ollback occurs w
1de65 69 74 68 69 6e 20 74 68 65 20 74 72 61 6e 73 61  ithin the transa
1de66 63 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f  ction...      */
1de67 0d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  ..      if( pBt-
1de68 3e 6e 50 61 67 65 21 3d 67 65 74 34 62 79 74 65  >nPage!=get4byte
1de69 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
1de6a 32 38 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  28]) ){..       
1de6b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1de6c 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e  erWrite(pPage1->
1de6d 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  pDbPage);..     
1de6e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1de6f 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
1de70 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
1de71 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d 2c 20  ge1->aData[28], 
1de72 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0d 0a 20 20  pBt->nPage);..  
1de73 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
1de74 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
1de75 0d 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0d 0a  ..trans_begun:..
1de76 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1de77 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
1de78 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61  ..    /* This ca
1de79 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68  ll makes sure th
1de7a 61 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73  at the pager has
1de7b 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d   the correct num
1de7c 62 65 72 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 6f  ber of..    ** o
1de7d 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20  pen savepoints. 
1de7e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  If the second pa
1de7f 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74  rameter is great
1de80 65 72 20 74 68 61 6e 20 30 20 61 6e 64 0d 0a 20  er than 0 and.. 
1de81 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f     ** the sub-jo
1de82 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72  urnal is not alr
1de83 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20  eady open, then 
1de84 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65  it will be opene
1de85 64 20 68 65 72 65 2e 0d 0a 20 20 20 20 2a 2f 0d  d here...    */.
1de86 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1de87 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
1de88 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  int(pBt->pPager,
1de89 20 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69   p->db->nSavepoi
1de8a 6e 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 62  nt);..  }....  b
1de8b 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
1de8c 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
1de8d 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20 72 65  eLeave(p);..  re
1de8e 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23  turn rc;..}....#
1de8f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1de90 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 0d  IT_AUTOVACUUM...
1de91 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
1de92 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
1de93 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c  ies for all chil
1de94 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61  dren of page pPa
1de95 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0d 0a 2a 2a  ge. Also, if..**
1de96 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20   pPage contains 
1de97 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74  cells that point
1de98 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
1de99 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e  es, set the poin
1de9a 74 65 72 0d 0a 2a 2a 20 6d 61 70 20 65 6e 74 72  ter..** map entr
1de9b 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72  ies for the over
1de9c 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65  flow pages as we
1de9d 6c 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ll...*/..static 
1de9e 69 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d  int setChildPtrm
1de9f 61 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  aps(MemPage *pPa
1dea0 67 65 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20  ge){..  int i;  
1dea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dea2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1dea3 75 6e 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a  unter variable *
1dea4 2f 0d 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20  /..  int nCell; 
1dea5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dea6 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1dea7 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 61  r of cells in pa
1dea8 67 65 20 70 50 61 67 65 20 2a 2f 0d 0a 20 20 69  ge pPage */..  i
1dea9 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1deaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1deab 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
1deac 20 2a 2f 0d 0a 20 20 42 74 53 68 61 72 65 64 20   */..  BtShared 
1dead 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
1deae 74 3b 0d 0a 20 20 75 38 20 69 73 49 6e 69 74 4f  t;..  u8 isInitO
1deaf 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49  rig = pPage->isI
1deb0 6e 69 74 3b 0d 0a 20 20 50 67 6e 6f 20 70 67 6e  nit;..  Pgno pgn
1deb1 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
1deb2 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ....  assert( sq
1deb3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1deb4 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1deb5 65 78 29 20 29 3b 0d 0a 20 20 72 63 20 3d 20 62  ex) );..  rc = b
1deb6 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61  treeInitPage(pPa
1deb7 67 65 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d  ge);..  if( rc!=
1deb8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1deb9 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64    goto set_child
1deba 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0d 0a 20  _ptrmaps_out;.. 
1debb 20 7d 0d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50   }..  nCell = pP
1debc 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 0d 0a 20  age->nCell;.... 
1debd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
1debe 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 75 38  l; i++){..    u8
1debf 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65   *pCell = findCe
1dec0 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0d 0a 0d  ll(pPage, i);...
1dec1 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76  .    ptrmapPutOv
1dec2 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  flPtr(pPage, pCe
1dec3 6c 6c 2c 20 26 72 63 29 3b 0d 0a 0d 0a 20 20 20  ll, &rc);....   
1dec4 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
1dec5 66 20 29 7b 0d 0a 20 20 20 20 20 20 50 67 6e 6f  f ){..      Pgno
1dec6 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74   childPgno = get
1dec7 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0d 0a 20  4byte(pCell);.. 
1dec8 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1dec9 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50  Bt, childPgno, P
1deca 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e  TRMAP_BTREE, pgn
1decb 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20 7d 0d  o, &rc);..    }.
1decc 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 70  .  }....  if( !p
1decd 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20  Page->leaf ){.. 
1dece 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e     Pgno childPgn
1decf 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  o = get4byte(&pP
1ded0 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
1ded1 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
1ded2 0d 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  ..    ptrmapPut(
1ded3 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20  pBt, childPgno, 
1ded4 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67  PTRMAP_BTREE, pg
1ded5 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 7d 0d 0a  no, &rc);..  }..
1ded6 0d 0a 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  ..set_child_ptrm
1ded7 61 70 73 5f 6f 75 74 3a 0d 0a 20 20 70 50 61 67  aps_out:..  pPag
1ded8 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e  e->isInit = isIn
1ded9 69 74 4f 72 69 67 3b 0d 0a 20 20 72 65 74 75 72  itOrig;..  retur
1deda 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
1dedb 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e 20  ** Somewhere on 
1dedc 70 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e 74  pPage is a point
1dedd 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f 6d  er to page iFrom
1dede 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20 70  .  Modify this p
1dedf 6f 69 6e 74 65 72 20 73 6f 0d 0a 2a 2a 20 74 68  ointer so..** th
1dee0 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1dee1 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65  iTo. Parameter e
1dee2 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74  Type describes t
1dee3 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74  he type of point
1dee4 65 72 20 74 6f 0d 0a 2a 2a 20 62 65 20 6d 6f 64  er to..** be mod
1dee5 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f  ified, as  follo
1dee6 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 54 52 4d  ws:..**..** PTRM
1dee7 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50  AP_BTREE:     pP
1dee8 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70  age is a btree-p
1dee9 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72  age. The pointer
1deea 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69   points at a chi
1deeb 6c 64 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld ..**         
1deec 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 6f            page o
1deed 66 20 70 50 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  f pPage...**..**
1deee 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1deef 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74  1: pPage is a bt
1def0 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f  ree-page. The po
1def1 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20  inter points at 
1def2 61 6e 20 6f 76 65 72 66 6c 6f 77 0d 0a 2a 2a 20  an overflow..** 
1def3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1def4 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74    page pointed t
1def5 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20  o by one of the 
1def6 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0d  cells on pPage..
1def7 0a 2a 2a 0d 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f  .**..** PTRMAP_O
1def8 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20  VERFLOW2: pPage 
1def9 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70  is an overflow-p
1defa 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72  age. The pointer
1defb 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e   points at the n
1defc 65 78 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ext..**         
1defd 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c            overfl
1defe 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c  ow page in the l
1deff 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ist...*/..static
1df00 20 69 6e 74 20 6d 6f 64 69 66 79 50 61 67 65 50   int modifyPageP
1df01 6f 69 6e 74 65 72 28 4d 65 6d 50 61 67 65 20 2a  ointer(MemPage *
1df02 70 50 61 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f  pPage, Pgno iFro
1df03 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20  m, Pgno iTo, u8 
1df04 65 54 79 70 65 29 7b 0d 0a 20 20 61 73 73 65 72  eType){..  asser
1df05 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1df06 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
1df07 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 61  ->mutex) );..  a
1df08 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
1df09 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1df0a 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
1df0b 3b 0d 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d  ;..  if( eType==
1df0c 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
1df0d 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20   ){..    /* The 
1df0e 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79  pointer is alway
1df0f 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  s the first 4 by
1df10 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  tes of the page 
1df11 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
1df12 2f 0d 0a 20 20 20 20 69 66 28 20 67 65 74 34 62  /..    if( get4b
1df13 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61  yte(pPage->aData
1df14 29 21 3d 69 46 72 6f 6d 20 29 7b 0d 0a 20 20 20  )!=iFrom ){..   
1df15 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1df16 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a  _CORRUPT_BKPT;..
1df17 20 20 20 20 7d 0d 0a 20 20 20 20 70 75 74 34 62      }..    put4b
1df18 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61  yte(pPage->aData
1df19 2c 20 69 54 6f 29 3b 0d 0a 20 20 7d 65 6c 73 65  , iTo);..  }else
1df1a 7b 0d 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74  {..    u8 isInit
1df1b 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
1df1c 49 6e 69 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69  Init;..    int i
1df1d 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c  ;..    int nCell
1df1e 3b 0d 0a 0d 0a 20 20 20 20 62 74 72 65 65 49 6e  ;....    btreeIn
1df1f 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0d 0a  itPage(pPage);..
1df20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67      nCell = pPag
1df21 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 0d 0a 20 20 20  e->nCell;....   
1df22 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
1df23 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  l; i++){..      
1df24 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64  u8 *pCell = find
1df25 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0d  Cell(pPage, i);.
1df26 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65  .      if( eType
1df27 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  ==PTRMAP_OVERFLO
1df28 57 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 43  W1 ){..        C
1df29 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0d 0a 20  ellInfo info;.. 
1df2a 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73         btreePars
1df2b 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
1df2c 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0d 0a  pCell, &info);..
1df2d 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
1df2e 2e 69 4f 76 65 72 66 6c 6f 77 0d 0a 20 20 20 20  .iOverflow..    
1df2f 20 20 20 20 20 26 26 20 70 43 65 6c 6c 2b 69 6e       && pCell+in
1df30 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b 33 3c 3d  fo.iOverflow+3<=
1df31 70 50 61 67 65 2d 3e 61 44 61 74 61 2b 70 50 61  pPage->aData+pPa
1df32 67 65 2d 3e 6d 61 73 6b 50 61 67 65 0d 0a 20 20  ge->maskPage..  
1df33 20 20 20 20 20 20 20 26 26 20 69 46 72 6f 6d 3d         && iFrom=
1df34 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c  =get4byte(&pCell
1df35 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
1df36 29 0d 0a 20 20 20 20 20 20 20 20 29 7b 0d 0a 20  )..        ){.. 
1df37 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
1df38 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f  e(&pCell[info.iO
1df39 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0d  verflow], iTo);.
1df3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
1df3b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
1df3c 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1df3d 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65      if( get4byte
1df3e 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29  (pCell)==iFrom )
1df3f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 75 74  {..          put
1df40 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f  4byte(pCell, iTo
1df41 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72  );..          br
1df42 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  eak;..        }.
1df43 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
1df44 0a 20 20 0d 0a 20 20 20 20 69 66 28 20 69 3d 3d  .  ..    if( i==
1df45 6e 43 65 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20  nCell ){..      
1df46 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  if( eType!=PTRMA
1df47 50 5f 42 54 52 45 45 20 7c 7c 20 0d 0a 20 20 20  P_BTREE || ..   
1df48 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28         get4byte(
1df49 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
1df4a 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
1df4b 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0d 0a 20 20  ])!=iFrom ){..  
1df4c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1df4d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1df4e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
1df4f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
1df50 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
1df51 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54  hdrOffset+8], iT
1df52 6f 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  o);..    }....  
1df53 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
1df54 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0d 0a 20  = isInitOrig;.. 
1df55 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
1df56 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  ITE_OK;..}......
1df57 2f 2a 0d 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  /*..** Move the 
1df58 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 70 61  open database pa
1df59 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f  ge pDbPage to lo
1df5a 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65  cation iFreePage
1df5b 20 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 64 61 74   in the ..** dat
1df5c 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 61  abase. The pDbPa
1df5d 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d  ge reference rem
1df5e 61 69 6e 73 20 76 61 6c 69 64 2e 0d 0a 2a 2a 0d  ains valid...**.
1df5f 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74  .** The isCommit
1df60 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20   flag indicates 
1df61 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f  that there is no
1df62 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
1df63 72 20 74 68 61 74 0d 0a 2a 2a 20 74 68 65 20 6a  r that..** the j
1df64 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20  ournal needs to 
1df65 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f  be sync()ed befo
1df66 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  re database page
1df67 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0d   pDbPage->pgno .
1df68 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74  .** can be writt
1df69 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65  en to. The calle
1df6a 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72  r has already pr
1df6b 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72  omised not to wr
1df6c 69 74 65 20 74 6f 20 74 68 61 74 0d 0a 2a 2a 20  ite to that..** 
1df6d 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  page...*/..stati
1df6e 63 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61  c int relocatePa
1df6f 67 65 28 0d 0a 20 20 42 74 53 68 61 72 65 64 20  ge(..  BtShared 
1df70 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20  *pBt,           
1df71 2f 2a 20 42 74 72 65 65 20 2a 2f 0d 0a 20 20 4d  /* Btree */..  M
1df72 65 6d 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c  emPage *pDbPage,
1df73 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
1df74 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0d  page to move */.
1df75 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20  .  u8 eType,    
1df76 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1df77 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 79 70 65  ointer map 'type
1df78 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50  ' entry for pDbP
1df79 61 67 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 69  age */..  Pgno i
1df7a 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 20 20  PtrPage,        
1df7b 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
1df7c 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72  p 'page-no' entr
1df7d 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
1df7e 0d 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61  ..  Pgno iFreePa
1df7f 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ge,          /* 
1df80 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20  The location to 
1df81 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f 20  move pDbPage to 
1df82 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d  */..  int isComm
1df83 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  it             /
1df84 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  * isCommit flag 
1df85 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
1df86 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a  3PagerMovepage *
1df87 2f 0d 0a 29 7b 0d 0a 20 20 4d 65 6d 50 61 67 65  /..){..  MemPage
1df88 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a   *pPtrPage;   /*
1df89 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63   The page that c
1df8a 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
1df8b 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0d  r to pDbPage */.
1df8c 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65 20  .  Pgno iDbPage 
1df8d 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b  = pDbPage->pgno;
1df8e 0d 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ..  Pager *pPage
1df8f 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b  r = pBt->pPager;
1df90 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20  ..  int rc;.... 
1df91 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d   assert( eType==
1df92 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
1df93 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41   || eType==PTRMA
1df94 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0d  P_OVERFLOW1 || .
1df95 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54  .      eType==PT
1df96 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54  RMAP_BTREE || eT
1df97 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype==PTRMAP_ROOT
1df98 50 41 47 45 20 29 3b 0d 0a 20 20 61 73 73 65 72  PAGE );..  asser
1df99 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1df9a 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
1df9b 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1df9c 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42  pDbPage->pBt==pB
1df9d 74 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4d 6f 76  t );....  /* Mov
1df9e 65 20 70 61 67 65 20 69 44 62 50 61 67 65 20 66  e page iDbPage f
1df9f 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20  rom its current 
1dfa0 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65  location to page
1dfa1 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 61 67   number iFreePag
1dfa2 65 20 2a 2f 0d 0a 20 20 54 52 41 43 45 28 28 22  e */..  TRACE(("
1dfa3 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69  AUTOVACUUM: Movi
1dfa4 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 61  ng %d to free pa
1dfa5 67 65 20 25 64 20 28 70 74 72 20 70 61 67 65 20  ge %d (ptr page 
1dfa6 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c 20  %d type %d)\n", 
1dfa7 0d 0a 20 20 20 20 20 20 69 44 62 50 61 67 65 2c  ..      iDbPage,
1dfa8 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74 72   iFreePage, iPtr
1dfa9 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0d 0a  Page, eType));..
1dfaa 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1dfab 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67  gerMovepage(pPag
1dfac 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62  er, pDbPage->pDb
1dfad 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c  Page, iFreePage,
1dfae 20 69 73 43 6f 6d 6d 69 74 29 3b 0d 0a 20 20 69   isCommit);..  i
1dfaf 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1dfb0 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
1dfb1 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20 70 44 62 50  rc;..  }..  pDbP
1dfb2 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65  age->pgno = iFre
1dfb3 65 50 61 67 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  ePage;....  /* I
1dfb4 66 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20  f pDbPage was a 
1dfb5 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e  btree-page, then
1dfb6 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69   it may have chi
1dfb7 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20  ld pages and/or 
1dfb8 63 65 6c 6c 73 0d 0a 20 20 2a 2a 20 74 68 61 74  cells..  ** that
1dfb9 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c   point to overfl
1dfba 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f  ow pages. The po
1dfbb 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65  inter map entrie
1dfbc 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0d  s for all these.
1dfbd 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64  .  ** pages need
1dfbe 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0d   to be changed..
1dfbf 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20 70  .  **..  ** If p
1dfc0 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65  DbPage is an ove
1dfc1 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
1dfc2 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74   the first 4 byt
1dfc3 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0d 0a  es may store a..
1dfc4 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20    ** pointer to 
1dfc5 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65  a subsequent ove
1dfc6 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74  rflow page. If t
1dfc7 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c  his is the case,
1dfc8 20 74 68 65 6e 0d 0a 20 20 2a 2a 20 74 68 65 20   then..  ** the 
1dfc9 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64  pointer map need
1dfca 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  s to be updated 
1dfcb 66 6f 72 20 74 68 65 20 73 75 62 73 65 71 75 65  for the subseque
1dfcc 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  nt overflow page
1dfcd 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 65  ...  */..  if( e
1dfce 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52  Type==PTRMAP_BTR
1dfcf 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  EE || eType==PTR
1dfd0 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0d  MAP_ROOTPAGE ){.
1dfd1 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 69  .    rc = setChi
1dfd2 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 67  ldPtrmaps(pDbPag
1dfd3 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21  e);..    if( rc!
1dfd4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
1dfd5 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d       return rc;.
1dfd6 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
1dfd7 0d 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f  ..    Pgno nextO
1dfd8 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 70  vfl = get4byte(p
1dfd9 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0d  DbPage->aData);.
1dfda 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66  .    if( nextOvf
1dfdb 6c 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70  l!=0 ){..      p
1dfdc 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65  trmapPut(pBt, ne
1dfdd 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f  xtOvfl, PTRMAP_O
1dfde 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50  VERFLOW2, iFreeP
1dfdf 61 67 65 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  age, &rc);..    
1dfe0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1dfe1 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
1dfe2 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20  return rc;..    
1dfe3 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
1dfe4 0a 0d 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20  ...  /* Fix the 
1dfe5 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72  database pointer
1dfe6 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 61 67   on page iPtrPag
1dfe7 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61  e that pointed a
1dfe8 74 20 69 44 62 50 61 67 65 20 73 6f 0d 0a 20 20  t iDbPage so..  
1dfe9 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  ** that it point
1dfea 73 20 61 74 20 69 46 72 65 65 50 61 67 65 2e 20  s at iFreePage. 
1dfeb 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f 69  Also fix the poi
1dfec 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66  nter map entry f
1dfed 6f 72 0d 0a 20 20 2a 2a 20 69 50 74 72 50 61 67  or..  ** iPtrPag
1dfee 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  e...  */..  if( 
1dfef 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f  eType!=PTRMAP_RO
1dff0 4f 54 50 41 47 45 20 29 7b 0d 0a 20 20 20 20 72  OTPAGE ){..    r
1dff1 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
1dff2 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20  (pBt, iPtrPage, 
1dff3 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0d 0a  &pPtrPage, 0);..
1dff4 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1dff5 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
1dff6 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20  return rc;..    
1dff7 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  }..    rc = sqli
1dff8 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
1dff9 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  trPage->pDbPage)
1dffa 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  ;..    if( rc!=S
1dffb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1dffc 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1dffd 50 74 72 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  PtrPage);..     
1dffe 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1dfff 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64   }..    rc = mod
1e000 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 70  ifyPagePointer(p
1e001 50 74 72 50 61 67 65 2c 20 69 44 62 50 61 67 65  PtrPage, iDbPage
1e002 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79  , iFreePage, eTy
1e003 70 65 29 3b 0d 0a 20 20 20 20 72 65 6c 65 61 73  pe);..    releas
1e004 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 3b  ePage(pPtrPage);
1e005 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
1e006 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1e007 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
1e008 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79 70   iFreePage, eTyp
1e009 65 2c 20 69 50 74 72 50 61 67 65 2c 20 26 72 63  e, iPtrPage, &rc
1e00a 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
1e00b 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
1e00c 0a 0d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
1e00d 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72  claration requir
1e00e 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d  ed by incrVacuum
1e00f 53 74 65 70 28 29 2e 20 2a 2f 0d 0a 73 74 61 74  Step(). */..stat
1e010 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42  ic int allocateB
1e011 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65  treePage(BtShare
1e012 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c  d *, MemPage **,
1e013 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75   Pgno *, Pgno, u
1e014 38 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 65  8);..../*..** Pe
1e015 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73  rform a single s
1e016 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d  tep of an increm
1e017 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66  ental-vacuum. If
1e018 20 73 75 63 63 65 73 73 66 75 6c 2c 0d 0a 2a 2a   successful,..**
1e019 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1e01a 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  K. If there is n
1e01b 6f 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e  o work to do (an
1e01c 64 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 0d 0a  d therefore no..
1e01d 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c  ** point in call
1e01e 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
1e01f 6e 20 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e  n again), return
1e020 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0d 0a 2a   SQLITE_DONE...*
1e021 2a 0d 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69  *..** More speci
1e022 66 69 63 6c 79 2c 20 74 68 69 73 20 66 75 6e 63  ficly, this func
1e023 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f  tion attempts to
1e024 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20 74 68 65   re-organize the
1e025 20 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 73   ..** database s
1e026 6f 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 20  o that the last 
1e027 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65  page of the file
1e028 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73   currently in us
1e029 65 0d 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67  e..** is no long
1e02a 65 72 20 69 6e 20 75 73 65 2e 0d 0a 2a 2a 0d 0a  er in use...**..
1e02b 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70  ** If the nFin p
1e02c 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d  arameter is non-
1e02d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 74  zero, this funct
1e02e 69 6f 6e 20 61 73 73 75 6d 65 73 0d 0a 2a 2a 20  ion assumes..** 
1e02f 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
1e030 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e  will keep callin
1e031 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  g incrVacuumStep
1e032 28 29 20 75 6e 74 69 6c 0d 0a 2a 2a 20 69 74 20  () until..** it 
1e033 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  returns SQLITE_D
1e034 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c  ONE or an error,
1e035 20 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 69   and that nFin i
1e036 73 20 74 68 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72  s the..** number
1e037 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 64 61   of pages the da
1e038 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c  tabase file will
1e039 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74   contain after t
1e03a 68 69 73 20 0d 0a 2a 2a 20 70 72 6f 63 65 73 73  his ..** process
1e03b 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49   is complete.  I
1e03c 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20  f nFin is zero, 
1e03d 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  it is assumed th
1e03e 61 74 0d 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75  at..** incrVacuu
1e03f 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20  mStep() will be 
1e040 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20  called a finite 
1e041 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0d  amount of times.
1e042 0a 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72  .** which may or
1e043 20 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74   may not empty t
1e044 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20  he freelist.  A 
1e045 66 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0d  full autovacuum.
1e046 0a 2a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20  .** has nFin>0. 
1e047 20 41 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65   A "PRAGMA incre
1e048 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68  mental_vacuum" h
1e049 61 73 20 6e 46 69 6e 3d 3d 30 2e 0d 0a 2a 2f 0d  as nFin==0...*/.
1e04a 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72  .static int incr
1e04b 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61  VacuumStep(BtSha
1e04c 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e  red *pBt, Pgno n
1e04d 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50  Fin, Pgno iLastP
1e04e 67 29 7b 0d 0a 20 20 50 67 6e 6f 20 6e 46 72 65  g){..  Pgno nFre
1e04f 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  eList;          
1e050 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1e051 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65  ges still on the
1e052 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0d 0a 20   free-list */.. 
1e053 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 61 73   int rc;....  as
1e054 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1e055 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1e056 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
1e057 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20  t( iLastPg>nFin 
1e058 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 50 54 52  );....  if( !PTR
1e059 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
1e05a 69 4c 61 73 74 50 67 29 20 26 26 20 69 4c 61 73  iLastPg) && iLas
1e05b 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  tPg!=PENDING_BYT
1e05c 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0d 0a  E_PAGE(pBt) ){..
1e05d 20 20 20 20 75 38 20 65 54 79 70 65 3b 0d 0a 20      u8 eType;.. 
1e05e 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65     Pgno iPtrPage
1e05f 3b 0d 0a 0d 0a 20 20 20 20 6e 46 72 65 65 4c 69  ;....    nFreeLi
1e060 73 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  st = get4byte(&p
1e061 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1e062 61 5b 33 36 5d 29 3b 0d 0a 20 20 20 20 69 66 28  a[36]);..    if(
1e063 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b   nFreeList==0 ){
1e064 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
1e065 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a 20 20 20  QLITE_DONE;..   
1e066 20 7d 0d 0a 0d 0a 20 20 20 20 72 63 20 3d 20 70   }....    rc = p
1e067 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c  trmapGet(pBt, iL
1e068 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26  astPg, &eType, &
1e069 69 50 74 72 50 61 67 65 29 3b 0d 0a 20 20 20 20  iPtrPage);..    
1e06a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1e06b 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  K ){..      retu
1e06c 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20  rn rc;..    }.. 
1e06d 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
1e06e 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b  RMAP_ROOTPAGE ){
1e06f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
1e070 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1e071 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  PT;..    }....  
1e072 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
1e073 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0d  MAP_FREEPAGE ){.
1e074 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d  .      if( nFin=
1e075 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f  =0 ){..        /
1e076 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67  * Remove the pag
1e077 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73  e from the files
1e078 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
1e079 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
1e07a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20  ..        ** if 
1e07b 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  nFin is non-zero
1e07c 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20  . In that case, 
1e07d 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69  the free-list wi
1e07e 6c 6c 20 62 65 0d 0a 20 20 20 20 20 20 20 20 2a  ll be..        *
1e07f 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a  * truncated to z
1e080 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 20 66  ero after this f
1e081 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c  unction returns,
1e082 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0d   so it doesn't .
1e083 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 74  .        ** matt
1e084 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20 63  er if it still c
1e085 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 72  ontains some gar
1e086 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0d 0a 20  bage entries... 
1e087 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
1e088 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b     Pgno iFreePg;
1e089 0d 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67  ..        MemPag
1e08a 65 20 2a 70 46 72 65 65 50 67 3b 0d 0a 20 20 20  e *pFreePg;..   
1e08b 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
1e08c 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1e08d 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
1e08e 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29  ePg, iLastPg, 1)
1e08f 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
1e090 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
1e091 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1e092 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d  n rc;..        }
1e093 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
1e094 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74  ( iFreePg==iLast
1e095 50 67 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  Pg );..        r
1e096 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65  eleasePage(pFree
1e097 50 67 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  Pg);..      }.. 
1e098 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20     } else {..   
1e099 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b     Pgno iFreePg;
1e09a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e09b 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 61  Index of free pa
1e09c 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74  ge to move pLast
1e09d 50 67 20 74 6f 20 2a 2f 0d 0a 20 20 20 20 20 20  Pg to */..      
1e09e 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67  MemPage *pLastPg
1e09f 3b 0d 0a 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  ;....      rc = 
1e0a0 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
1e0a1 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73  , iLastPg, &pLas
1e0a2 74 50 67 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  tPg, 0);..      
1e0a3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1e0a4 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65  K ){..        re
1e0a5 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20  turn rc;..      
1e0a6 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66  }....      /* If
1e0a7 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74   nFin is zero, t
1e0a8 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78  his loop runs ex
1e0a9 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70  actly once and p
1e0aa 61 67 65 20 70 4c 61 73 74 50 67 0d 0a 20 20 20  age pLastPg..   
1e0ab 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 64     ** is swapped
1e0ac 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
1e0ad 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 64  free page pulled
1e0ae 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c 69   off the free li
1e0af 73 74 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20  st...      **.. 
1e0b0 20 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f       ** On the o
1e0b1 74 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e 46  ther hand, if nF
1e0b2 69 6e 20 69 73 20 67 72 65 61 74 65 72 20 74 68  in is greater th
1e0b3 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65  an zero, then ke
1e0b4 65 70 0d 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f  ep..      ** loo
1e0b5 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65  ping until a fre
1e0b6 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77  e-page located w
1e0b7 69 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 20  ithin the first 
1e0b8 6e 46 69 6e 20 70 61 67 65 73 0d 0a 20 20 20 20  nFin pages..    
1e0b9 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65    ** of the file
1e0ba 20 69 73 20 66 6f 75 6e 64 2e 0d 0a 20 20 20 20   is found...    
1e0bb 20 20 2a 2f 0d 0a 20 20 20 20 20 20 64 6f 20 7b    */..      do {
1e0bc 0d 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67  ..        MemPag
1e0bd 65 20 2a 70 46 72 65 65 50 67 3b 0d 0a 20 20 20  e *pFreePg;..   
1e0be 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
1e0bf 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1e0c0 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
1e0c1 65 50 67 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  ePg, 0, 0);..   
1e0c2 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1e0c3 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1e0c4 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1e0c5 28 70 4c 61 73 74 50 67 29 3b 0d 0a 20 20 20 20  (pLastPg);..    
1e0c6 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1e0c7 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
1e0c8 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1e0c9 28 70 46 72 65 65 50 67 29 3b 0d 0a 20 20 20 20  (pFreePg);..    
1e0ca 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d    }while( nFin!=
1e0cb 30 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69  0 && iFreePg>nFi
1e0cc 6e 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  n );..      asse
1e0cd 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73  rt( iFreePg<iLas
1e0ce 74 50 67 20 29 3b 0d 0a 20 20 20 20 20 20 0d 0a  tPg );..      ..
1e0cf 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1e0d0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 61  e3PagerWrite(pLa
1e0d1 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0d  stPg->pDbPage);.
1e0d2 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1e0d3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1e0d4 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61       rc = reloca
1e0d5 74 65 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73  tePage(pBt, pLas
1e0d6 74 50 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72  tPg, eType, iPtr
1e0d7 50 61 67 65 2c 20 69 46 72 65 65 50 67 2c 20 6e  Page, iFreePg, n
1e0d8 46 69 6e 21 3d 30 29 3b 0d 0a 20 20 20 20 20 20  Fin!=0);..      
1e0d9 7d 0d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  }..      release
1e0da 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0d 0a  Page(pLastPg);..
1e0db 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1e0dc 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1e0dd 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
1e0de 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
1e0df 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 6e 46 69    }....  if( nFi
1e0e0 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 4c 61  n==0 ){..    iLa
1e0e1 73 74 50 67 2d 2d 3b 0d 0a 20 20 20 20 77 68 69  stPg--;..    whi
1e0e2 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e  le( iLastPg==PEN
1e0e3 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1e0e4 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41  Bt)||PTRMAP_ISPA
1e0e5 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29  GE(pBt, iLastPg)
1e0e6 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 50   ){..      if( P
1e0e7 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1e0e8 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0d 0a 20  , iLastPg) ){.. 
1e0e9 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
1e0ea 70 50 67 3b 0d 0a 20 20 20 20 20 20 20 20 72 63  pPg;..        rc
1e0eb 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1e0ec 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70  pBt, iLastPg, &p
1e0ed 50 67 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  Pg, 0);..       
1e0ee 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e0ef 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
1e0f0 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1e0f1 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
1e0f2 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1e0f3 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50  rWrite(pPg->pDbP
1e0f4 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  age);..        r
1e0f5 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b  eleasePage(pPg);
1e0f6 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ..        if( rc
1e0f7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
1e0f8 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1e0f9 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   rc;..        }.
1e0fa 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1e0fb 69 4c 61 73 74 50 67 2d 2d 3b 0d 0a 20 20 20 20  iLastPg--;..    
1e0fc 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  }..    sqlite3Pa
1e0fd 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
1e0fe 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c  (pBt->pPager, iL
1e0ff 61 73 74 50 67 29 3b 0d 0a 20 20 20 20 70 42 74  astPg);..    pBt
1e100 2d 3e 6e 50 61 67 65 20 3d 20 69 4c 61 73 74 50  ->nPage = iLastP
1e101 67 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  g;..  }..  retur
1e102 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
1e103 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 77 72 69 74  .../*..** A writ
1e104 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  e-transaction mu
1e105 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66  st be opened bef
1e106 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  ore calling this
1e107 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 49   function...** I
1e108 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e  t performs a sin
1e109 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b  gle unit of work
1e10a 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72   towards an incr
1e10b 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0d  emental vacuum..
1e10c 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 69  .**..** If the i
1e10d 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
1e10e 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66  m is finished af
1e10f 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
1e110 6e 20 68 61 73 20 72 75 6e 2c 0d 0a 2a 2a 20 53  n has run,..** S
1e111 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
1e112 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73  turned. If it is
1e113 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62   not finished, b
1e114 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  ut no error occu
1e115 72 72 65 64 2c 0d 0a 2a 2a 20 53 51 4c 49 54 45  rred,..** SQLITE
1e116 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
1e117 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51   Otherwise an SQ
1e118 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
1e119 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52   ..*/..SQLITE_PR
1e11a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1e11b 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
1e11c 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20 69  (Btree *p){..  i
1e11d 6e 74 20 72 63 3b 0d 0a 20 20 42 74 53 68 61 72  nt rc;..  BtShar
1e11e 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1e11f 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 42 74  ;....  sqlite3Bt
1e120 72 65 65 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20  reeEnter(p);..  
1e121 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54  assert( pBt->inT
1e122 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
1e123 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e 69 6e  S_WRITE && p->in
1e124 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
1e125 54 45 20 29 3b 0d 0a 20 20 69 66 28 20 21 70 42  TE );..  if( !pB
1e126 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
1e127 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
1e128 45 5f 44 4f 4e 45 3b 0d 0a 20 20 7d 65 6c 73 65  E_DONE;..  }else
1e129 7b 0d 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74  {..    invalidat
1e12a 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68  eAllOverflowCach
1e12b 65 28 70 42 74 29 3b 0d 0a 20 20 20 20 72 63 20  e(pBt);..    rc 
1e12c 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  = incrVacuumStep
1e12d 28 70 42 74 2c 20 30 2c 20 62 74 72 65 65 50 61  (pBt, 0, btreePa
1e12e 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b 0d 0a  gecount(pBt));..
1e12f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1e130 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
1e131 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1e132 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67  rWrite(pBt->pPag
1e133 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20  e1->pDbPage);.. 
1e134 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1e135 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1e136 61 5b 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67  a[28], pBt->nPag
1e137 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  e);..    }..  }.
1e138 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
1e139 65 61 76 65 28 70 29 3b 0d 0a 20 20 72 65 74 75  eave(p);..  retu
1e13a 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
1e13b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1e13c 20 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72   is called prior
1e13d 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
1e13e 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72  Commit when a tr
1e13f 61 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2a 20 69 73  ansaction..** is
1e140 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e   commited for an
1e141 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
1e142 61 62 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  abase...**..** I
1e143 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
1e144 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70  eturned, then *p
1e145 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f  nTrunc is set to
1e146 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1e147 61 67 65 73 0d 0a 2a 2a 20 74 68 65 20 64 61 74  ages..** the dat
1e148 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c  abase file shoul
1e149 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74  d be truncated t
1e14a 6f 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d  o during the com
1e14b 6d 69 74 20 70 72 6f 63 65 73 73 2e 20 0d 0a 2a  mit process. ..*
1e14c 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62  * i.e. the datab
1e14d 61 73 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f  ase has been reo
1e14e 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74  rganized so that
1e14f 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20   only the first 
1e150 2a 70 6e 54 72 75 6e 63 0d 0a 2a 2a 20 70 61 67  *pnTrunc..** pag
1e151 65 73 20 61 72 65 20 69 6e 20 75 73 65 2e 0d 0a  es are in use...
1e152 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 61  */..static int a
1e153 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
1e154 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0d  BtShared *pBt){.
1e155 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1e156 54 45 5f 4f 4b 3b 0d 0a 20 20 50 61 67 65 72 20  TE_OK;..  Pager 
1e157 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  *pPager = pBt->p
1e158 50 61 67 65 72 3b 0d 0a 20 20 56 56 41 5f 4f 4e  Pager;..  VVA_ON
1e159 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73  LY( int nRef = s
1e15a 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
1e15b 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0d 0a  unt(pPager) );..
1e15c 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1e15d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1e15e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  Bt->mutex) );.. 
1e15f 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
1e160 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
1e161 3b 0d 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d  ;..  assert(pBt-
1e162 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0d 0a 20  >autoVacuum);.. 
1e163 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56   if( !pBt->incrV
1e164 61 63 75 75 6d 20 29 7b 0d 0a 20 20 20 20 50 67  acuum ){..    Pg
1e165 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20  no nFin;        
1e166 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1e167 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  ges in database 
1e168 61 66 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d  after autovacuum
1e169 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 50 67 6e 6f  ing */..    Pgno
1e16a 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f   nFree;        /
1e16b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
1e16c 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  s on the freelis
1e16d 74 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0d 0a  t initially */..
1e16e 20 20 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70      Pgno nPtrmap
1e16f 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
1e170 20 6f 66 20 50 74 72 4d 61 70 20 70 61 67 65 73   of PtrMap pages
1e171 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0d   to be freed */.
1e172 0a 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b  .    Pgno iFree;
1e173 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
1e174 65 78 74 20 70 61 67 65 20 74 6f 20 62 65 20 66  ext page to be f
1e175 72 65 65 64 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  reed */..    int
1e176 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20   nEntry;        
1e177 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
1e178 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d  ries on one ptrm
1e179 61 70 20 70 61 67 65 20 2a 2f 0d 0a 20 20 20 20  ap page */..    
1e17a 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 20  Pgno nOrig;     
1e17b 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73     /* Database s
1e17c 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65 65 69  ize before freei
1e17d 6e 67 20 2a 2f 0d 0a 0d 0a 20 20 20 20 6e 4f 72  ng */....    nOr
1e17e 69 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f  ig = btreePageco
1e17f 75 6e 74 28 70 42 74 29 3b 0d 0a 20 20 20 20 69  unt(pBt);..    i
1e180 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  f( PTRMAP_ISPAGE
1e181 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20  (pBt, nOrig) || 
1e182 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42  nOrig==PENDING_B
1e183 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
1e184 0d 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73  ..      /* It is
1e185 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1e186 20 63 72 65 61 74 65 20 61 20 64 61 74 61 62 61   create a databa
1e187 73 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  se for which the
1e188 20 66 69 6e 61 6c 20 70 61 67 65 0d 0a 20 20 20   final page..   
1e189 20 20 20 2a 2a 20 69 73 20 65 69 74 68 65 72 20     ** is either 
1e18a 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
1e18b 67 65 20 6f 72 20 74 68 65 20 70 65 6e 64 69 6e  ge or the pendin
1e18c 67 2d 62 79 74 65 20 70 61 67 65 2e 20 49 66 20  g-byte page. If 
1e18d 6f 6e 65 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73  one..      ** is
1e18e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68   encountered, th
1e18f 69 73 20 69 6e 64 69 63 61 74 65 73 20 63 6f 72  is indicates cor
1e190 72 75 70 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20  ruption...      
1e191 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  */..      return
1e192 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1e193 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  BKPT;..    }....
1e194 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34      nFree = get4
1e195 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
1e196 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0d 0a  1->aData[36]);..
1e197 20 20 20 20 6e 45 6e 74 72 79 20 3d 20 70 42 74      nEntry = pBt
1e198 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b 0d  ->usableSize/5;.
1e199 0a 20 20 20 20 6e 50 74 72 6d 61 70 20 3d 20 28  .    nPtrmap = (
1e19a 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d  nFree-nOrig+PTRM
1e19b 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e  AP_PAGENO(pBt, n
1e19c 4f 72 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45  Orig)+nEntry)/nE
1e19d 6e 74 72 79 3b 0d 0a 20 20 20 20 6e 46 69 6e 20  ntry;..    nFin 
1e19e 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 72 65 65 20  = nOrig - nFree 
1e19f 2d 20 6e 50 74 72 6d 61 70 3b 0d 0a 20 20 20 20  - nPtrmap;..    
1e1a0 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 4e  if( nOrig>PENDIN
1e1a1 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
1e1a2 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e 47   && nFin<PENDING
1e1a3 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
1e1a4 29 7b 0d 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d  ){..      nFin--
1e1a5 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 77 68  ;..    }..    wh
1e1a6 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
1e1a7 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c  GE(pBt, nFin) ||
1e1a8 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42   nFin==PENDING_B
1e1a9 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
1e1aa 0d 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0d  ..      nFin--;.
1e1ab 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1e1ac 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74  nFin>nOrig ) ret
1e1ad 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1e1ae 50 54 5f 42 4b 50 54 3b 0d 0a 0d 0a 20 20 20 20  PT_BKPT;....    
1e1af 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b  for(iFree=nOrig;
1e1b0 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20 72   iFree>nFin && r
1e1b1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46  c==SQLITE_OK; iF
1e1b2 72 65 65 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 72  ree--){..      r
1e1b3 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
1e1b4 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46  ep(pBt, nFin, iF
1e1b5 72 65 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ree);..    }..  
1e1b6 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54    if( (rc==SQLIT
1e1b7 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51  E_DONE || rc==SQ
1e1b8 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65  LITE_OK) && nFre
1e1b9 65 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  e>0 ){..      rc
1e1ba 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1e1bb 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31  rite(pBt->pPage1
1e1bc 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 20  ->pDbPage);..   
1e1bd 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74     put4byte(&pBt
1e1be 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
1e1bf 33 32 5d 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  32], 0);..      
1e1c0 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  put4byte(&pBt->p
1e1c1 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
1e1c2 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 70 75 74  , 0);..      put
1e1c3 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
1e1c4 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d 2c 20 6e  e1->aData[28], n
1e1c5 46 69 6e 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  Fin);..      sql
1e1c6 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
1e1c7 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67  eImage(pBt->pPag
1e1c8 65 72 2c 20 6e 46 69 6e 29 3b 0d 0a 20 20 20 20  er, nFin);..    
1e1c9 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e    pBt->nPage = n
1e1ca 46 69 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  Fin;..    }..   
1e1cb 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e1cc 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  OK ){..      sql
1e1cd 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
1e1ce 6b 28 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20  k(pPager);..    
1e1cf 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65  }..  }....  asse
1e1d0 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65  rt( nRef==sqlite
1e1d1 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
1e1d2 50 61 67 65 72 29 20 29 3b 0d 0a 20 20 72 65 74  Pager) );..  ret
1e1d3 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 65  urn rc;..}....#e
1e1d4 6c 73 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  lse /* ifndef SQ
1e1d5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1e1d6 43 55 55 4d 20 2a 2f 0d 0a 23 20 64 65 66 69 6e  CUUM */..# defin
1e1d7 65 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  e setChildPtrmap
1e1d8 73 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a  s(x) SQLITE_OK..
1e1d9 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
1e1da 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
1e1db 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61  es the first pha
1e1dc 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 73  se of a two-phas
1e1dd 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 20  e commit.  This 
1e1de 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 63 61 75 73  routine..** caus
1e1df 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  es a rollback jo
1e1e0 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61  urnal to be crea
1e1e1 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20  ted (if it does 
1e1e2 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
1e1e3 74 29 0d 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c  t)..** and popul
1e1e4 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68  ated with enough
1e1e5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20   information so 
1e1e6 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20  that if a power 
1e1e7 6c 6f 73 73 20 6f 63 63 75 72 73 0d 0a 2a 2a 20  loss occurs..** 
1e1e8 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e  the database can
1e1e9 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20   be restored to 
1e1ea 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  its original sta
1e1eb 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61  te by playing ba
1e1ec 63 6b 0d 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  ck..** the journ
1e1ed 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f  al.  Then the co
1e1ee 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f  ntents of the jo
1e1ef 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65  urnal are flushe
1e1f0 64 20 6f 75 74 20 74 6f 0d 0a 2a 2a 20 74 68 65  d out to..** the
1e1f1 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68   disk.  After th
1e1f2 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66  e journal is saf
1e1f3 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68  ely on oxide, th
1e1f4 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  e changes to the
1e1f5 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72  ..** database ar
1e1f6 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
1e1f7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1e1f8 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20   and flushed to 
1e1f9 6f 78 69 64 65 2e 0d 0a 2a 2a 20 41 74 20 74 68  oxide...** At th
1e1fa 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61  e end of this ca
1e1fb 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  ll, the rollback
1e1fc 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65   journal still e
1e1fd 78 69 73 74 73 20 6f 6e 20 74 68 65 0d 0a 2a 2a  xists on the..**
1e1fe 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65   disk and we are
1e1ff 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61   still holding a
1e200 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65  ll locks, so the
1e201 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73   transaction has
1e202 20 6e 6f 74 0d 0a 2a 2a 20 63 6f 6d 6d 69 74 74   not..** committ
1e203 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  ed.  See sqlite3
1e204 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1e205 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65  Two() for the se
1e206 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68  cond phase of th
1e207 65 0d 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f  e..** commit pro
1e208 63 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  cess...**..** Th
1e209 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
1e20a 6f 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74  op if no write-t
1e20b 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75  ransaction is cu
1e20c 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f  rrently active o
1e20d 6e 20 70 42 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f  n pBt...**..** O
1e20e 74 68 65 72 77 69 73 65 2c 20 73 79 6e 63 20 74  therwise, sync t
1e20f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1e210 20 66 6f 72 20 74 68 65 20 62 74 72 65 65 20 70   for the btree p
1e211 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e  Bt. zMaster poin
1e212 74 73 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 6e 61  ts to..** the na
1e213 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  me of a master j
1e214 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74  ournal file that
1e215 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74   should be writt
1e216 65 6e 20 69 6e 74 6f 20 74 68 65 0d 0a 2a 2a 20  en into the..** 
1e217 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e  individual journ
1e218 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 4e  al file, or is N
1e219 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  ULL, indicating 
1e21a 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  no master journa
1e21b 6c 20 66 69 6c 65 20 0d 0a 2a 2a 20 28 73 69 6e  l file ..** (sin
1e21c 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61  gle database tra
1e21d 6e 73 61 63 74 69 6f 6e 29 2e 0d 0a 2a 2a 0d 0a  nsaction)...**..
1e21e 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
1e21f 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74  called, the mast
1e220 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c  er journal shoul
1e221 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62  d already have b
1e222 65 65 6e 0d 0a 2a 2a 20 63 72 65 61 74 65 64 2c  een..** created,
1e223 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
1e224 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  this journal poi
1e225 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20  nter and synced 
1e226 74 6f 20 64 69 73 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  to disk...**..**
1e227 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f   Once this is ro
1e228 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e  utine has return
1e229 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69  ed, the only thi
1e22a 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63  ng required to c
1e22b 6f 6d 6d 69 74 0d 0a 2a 2a 20 74 68 65 20 77 72  ommit..** the wr
1e22c 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1e22d 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73  for this databas
1e22e 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c  e file is to del
1e22f 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  ete the journal.
1e230 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1e231 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e232 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1e233 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f  One(Btree *p, co
1e234 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
1e235 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  r){..  int rc = 
1e236 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66  SQLITE_OK;..  if
1e237 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1e238 41 4e 53 5f 57 52 49 54 45 20 29 7b 0d 0a 20 20  ANS_WRITE ){..  
1e239 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1e23a 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 20 20 73  = p->pBt;..    s
1e23b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1e23c 28 70 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  (p);..#ifndef SQ
1e23d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1e23e 43 55 55 4d 0d 0a 20 20 20 20 69 66 28 20 70 42  CUUM..    if( pB
1e23f 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
1e240 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 61 75 74  ..      rc = aut
1e241 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42  oVacuumCommit(pB
1e242 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72  t);..      if( r
1e243 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
1e244 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e245 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a  BtreeLeave(p);..
1e246 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
1e247 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  c;..      }..   
1e248 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
1e249 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1e24a 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
1e24b 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61  pBt->pPager, zMa
1e24c 73 74 65 72 2c 20 30 29 3b 0d 0a 20 20 20 20 73  ster, 0);..    s
1e24d 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1e24e 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  (p);..  }..  ret
1e24f 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1e250 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
1e251 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  on is called fro
1e252 6d 20 62 6f 74 68 20 42 74 72 65 65 43 6f 6d 6d  m both BtreeComm
1e253 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 6e 64  itPhaseTwo() and
1e254 20 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29   BtreeRollback()
1e255 0d 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63  ..** at the conc
1e256 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 72 61 6e  lusion of a tran
1e257 73 61 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74  saction...*/..st
1e258 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 45  atic void btreeE
1e259 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 74  ndTransaction(Bt
1e25a 72 65 65 20 2a 70 29 7b 0d 0a 20 20 42 74 53 68  ree *p){..  BtSh
1e25b 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1e25c 42 74 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  Bt;..  assert( s
1e25d 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1e25e 4d 75 74 65 78 28 70 29 20 29 3b 0d 0a 0d 0a 20  Mutex(p) );.... 
1e25f 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f   btreeClearHasCo
1e260 6e 74 65 6e 74 28 70 42 74 29 3b 0d 0a 20 20 69  ntent(pBt);..  i
1e261 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52  f( p->inTrans>TR
1e262 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 2d 3e 64  ANS_NONE && p->d
1e263 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1e264 3e 31 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66  >1 ){..    /* If
1e265 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72   there are other
1e266 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
1e267 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74  ts that belong t
1e268 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0d  o this database.
1e269 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20  .    ** handle, 
1e26a 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72  downgrade to a r
1e26b 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63  ead-only transac
1e26c 74 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20  tion. The other 
1e26d 73 74 61 74 65 6d 65 6e 74 73 0d 0a 20 20 20 20  statements..    
1e26e 2a 2a 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20  ** may still be 
1e26f 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65  reading from the
1e270 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0d 0a   database.  */..
1e271 20 20 20 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c      downgradeAll
1e272 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1e273 4c 6f 63 6b 73 28 70 29 3b 0d 0a 20 20 20 20 70  Locks(p);..    p
1e274 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
1e275 53 5f 52 45 41 44 3b 0d 0a 20 20 7d 65 6c 73 65  S_READ;..  }else
1e276 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  {..    /* If the
1e277 20 68 61 6e 64 6c 65 20 68 61 64 20 61 6e 79 20   handle had any 
1e278 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74  kind of transact
1e279 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d  ion open, decrem
1e27a 65 6e 74 20 74 68 65 20 0d 0a 20 20 20 20 2a 2a  ent the ..    **
1e27b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
1e27c 6e 74 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  nt of the shared
1e27d 20 62 74 72 65 65 2e 20 49 66 20 74 68 65 20 74   btree. If the t
1e27e 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
1e27f 20 0d 0a 20 20 20 20 2a 2a 20 72 65 61 63 68 65   ..    ** reache
1e280 73 20 30 2c 20 73 65 74 20 74 68 65 20 73 68 61  s 0, set the sha
1e281 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41  red state to TRA
1e282 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c  NS_NONE. The unl
1e283 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
1e284 28 29 0d 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  ()..    ** call 
1e285 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63  below will unloc
1e286 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 2a 2f  k the pager.  */
1e287 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54  ..    if( p->inT
1e288 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
1e289 20 29 7b 0d 0a 20 20 20 20 20 20 63 6c 65 61 72   ){..      clear
1e28a 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
1e28b 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0d 0a 20 20  bleLocks(p);..  
1e28c 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
1e28d 63 74 69 6f 6e 2d 2d 3b 0d 0a 20 20 20 20 20 20  ction--;..      
1e28e 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61  if( 0==pBt->nTra
1e28f 6e 73 61 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20  nsaction ){..   
1e290 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e       pBt->inTran
1e291 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f  saction = TRANS_
1e292 4e 4f 4e 45 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  NONE;..      }..
1e293 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
1e294 53 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  Set the current 
1e295 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74  transaction stat
1e296 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20  e to TRANS_NONE 
1e297 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0d  and unlock the .
1e298 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 69 66  .    ** pager if
1e299 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65   this call close
1e29a 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64 20  d the only read 
1e29b 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  or write transac
1e29c 74 69 6f 6e 2e 20 20 2a 2f 0d 0a 20 20 20 20 70  tion.  */..    p
1e29d 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
1e29e 53 5f 4e 4f 4e 45 3b 0d 0a 20 20 20 20 75 6e 6c  S_NONE;..    unl
1e29f 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
1e2a0 28 70 42 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  (pBt);..  }.... 
1e2a1 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
1e2a2 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  p);..}..../*..**
1e2a3 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e   Commit the tran
1e2a4 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  saction currentl
1e2a5 79 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0d 0a  y in progress...
1e2a6 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
1e2a7 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ine implements t
1e2a8 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20  he second phase 
1e2a9 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 6f 6d  of a 2-phase com
1e2aa 6d 69 74 2e 20 20 54 68 65 0d 0a 2a 2a 20 73 71  mit.  The..** sq
1e2ab 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1e2ac 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 69  PhaseOne() routi
1e2ad 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
1e2ae 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75  t phase and shou
1e2af 6c 64 0d 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65  ld..** be invoke
1e2b0 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  d prior to calli
1e2b1 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
1e2b2 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72    The sqlite3Btr
1e2b3 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
1e2b4 28 29 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64  ()..** routine d
1e2b5 69 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20  id all the work 
1e2b6 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72  of writing infor
1e2b7 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69  mation out to di
1e2b8 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20  sk and flushing 
1e2b9 74 68 65 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73  the..** contents
1e2ba 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 72   so that they ar
1e2bb 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74  e written onto t
1e2bc 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e  he disk platter.
1e2bd 20 20 41 6c 6c 20 74 68 69 73 0d 0a 2a 2a 20 72    All this..** r
1e2be 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f  outine has to do
1e2bf 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72   is delete or tr
1e2c0 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74  uncate or zero t
1e2c1 68 65 20 68 65 61 64 65 72 20 69 6e 20 74 68 65  he header in the
1e2c2 0d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63  ..** the rollbac
1e2c3 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 68  k journal (which
1e2c4 20 63 61 75 73 65 73 20 74 68 65 20 74 72 61 6e   causes the tran
1e2c5 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69  saction to commi
1e2c6 74 29 20 61 6e 64 0d 0a 2a 2a 20 64 72 6f 70 20  t) and..** drop 
1e2c7 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e  locks...**..** N
1e2c8 6f 72 6d 61 6c 6c 79 2c 20 69 66 20 61 6e 20 65  ormally, if an e
1e2c9 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1e2ca 65 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  e the pager laye
1e2cb 72 20 69 73 20 61 74 74 65 6d 70 74 69 6e 67 20  r is attempting 
1e2cc 74 6f 20 0d 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65  to ..** finalize
1e2cd 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1e2ce 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
1e2cf 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
1e2d0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 61 6e 64  rns an error and
1e2d1 0d 0a 2a 2a 20 74 68 65 20 75 70 70 65 72 20 6c  ..** the upper l
1e2d2 61 79 65 72 20 77 69 6c 6c 20 61 74 74 65 6d 70  ayer will attemp
1e2d3 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 48 6f  t a rollback. Ho
1e2d4 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 73 65  wever, if the se
1e2d5 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0d 0a 2a  cond argument..*
1e2d6 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  * is non-zero th
1e2d7 65 6e 20 74 68 69 73 20 62 2d 74 72 65 65 20 74  en this b-tree t
1e2d8 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 70 61  ransaction is pa
1e2d9 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69  rt of a multi-fi
1e2da 6c 65 20 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  le ..** transact
1e2db 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
1e2dc 65 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  e, the transacti
1e2dd 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  on has already b
1e2de 65 65 6e 20 63 6f 6d 6d 69 74 74 65 64 20 0d 0a  een committed ..
1e2df 2a 2a 20 28 62 79 20 64 65 6c 65 74 69 6e 67 20  ** (by deleting 
1e2e0 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
1e2e1 20 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 63   file) and the c
1e2e2 61 6c 6c 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72  aller will ignor
1e2e3 65 20 74 68 69 73 20 0d 0a 2a 2a 20 66 75 6e 63  e this ..** func
1e2e4 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 63 6f 64  tions return cod
1e2e5 65 2e 20 53 6f 2c 20 65 76 65 6e 20 69 66 20 61  e. So, even if a
1e2e6 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69  n error occurs i
1e2e7 6e 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  n the pager laye
1e2e8 72 2c 0d 0a 2a 2a 20 72 65 73 65 74 20 74 68 65  r,..** reset the
1e2e9 20 62 2d 74 72 65 65 20 6f 62 6a 65 63 74 73 20   b-tree objects 
1e2ea 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 74  internal state t
1e2eb 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
1e2ec 74 68 65 20 77 72 69 74 65 0d 0a 2a 2a 20 74 72  the write..** tr
1e2ed 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65  ansaction has be
1e2ee 65 6e 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 20  en closed. This 
1e2ef 69 73 20 71 75 69 74 65 20 73 61 66 65 2c 20 61  is quite safe, a
1e2f0 73 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c  s the pager will
1e2f1 20 68 61 76 65 0d 0a 2a 2a 20 74 72 61 6e 73 69   have..** transi
1e2f2 74 69 6f 6e 65 64 20 74 6f 20 74 68 65 20 65 72  tioned to the er
1e2f3 72 6f 72 20 73 74 61 74 65 2e 0d 0a 2a 2a 0d 0a  ror state...**..
1e2f4 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c  ** This will rel
1e2f5 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c  ease the write l
1e2f6 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
1e2f7 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68  ase file.  If th
1e2f8 65 72 65 0d 0a 2a 2a 20 61 72 65 20 6e 6f 20 61  ere..** are no a
1e2f9 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69  ctive cursors, i
1e2fa 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20  t also releases 
1e2fb 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0d 0a  the read lock...
1e2fc 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1e2fd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1e2fe 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
1e2ff 6f 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  o(Btree *p, int 
1e300 62 43 6c 65 61 6e 75 70 29 7b 0d 0a 0d 0a 20 20  bCleanup){....  
1e301 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
1e302 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 20 72 65 74  TRANS_NONE ) ret
1e303 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
1e304 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
1e305 74 65 72 28 70 29 3b 0d 0a 20 20 62 74 72 65 65  ter(p);..  btree
1e306 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0d 0a 0d  Integrity(p);...
1e307 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e  .  /* If the han
1e308 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d  dle has a write-
1e309 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
1e30a 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61  , commit the sha
1e30b 72 65 64 2d 62 74 72 65 65 73 20 0d 0a 20 20 2a  red-btrees ..  *
1e30c 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  * transaction an
1e30d 64 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64  d set the shared
1e30e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f   state to TRANS_
1e30f 52 45 41 44 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  READ...  */..  i
1e310 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
1e311 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0d 0a 20  RANS_WRITE ){.. 
1e312 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20 20     int rc;..    
1e313 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1e314 70 2d 3e 70 42 74 3b 0d 0a 20 20 20 20 61 73 73  p->pBt;..    ass
1e315 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
1e316 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1e317 52 49 54 45 20 29 3b 0d 0a 20 20 20 20 61 73 73  RITE );..    ass
1e318 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
1e319 61 63 74 69 6f 6e 3e 30 20 29 3b 0d 0a 20 20 20  action>0 );..   
1e31a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1e31b 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
1e31c 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0d 0a  (pBt->pPager);..
1e31d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1e31e 54 45 5f 4f 4b 20 26 26 20 62 43 6c 65 61 6e 75  TE_OK && bCleanu
1e31f 70 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73  p==0 ){..      s
1e320 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1e321 28 70 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  (p);..      retu
1e322 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20  rn rc;..    }.. 
1e323 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
1e324 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45  ction = TRANS_RE
1e325 41 44 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 62 74  AD;..  }....  bt
1e326 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f  reeEndTransactio
1e327 6e 28 70 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  n(p);..  sqlite3
1e328 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a  BtreeLeave(p);..
1e329 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1e32a 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
1e32b 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20   Do both phases 
1e32c 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0d 0a 2a 2f  of a commit...*/
1e32d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1e32e 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1e32f 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70  eCommit(Btree *p
1e330 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  ){..  int rc;.. 
1e331 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1e332 65 72 28 70 29 3b 0d 0a 20 20 72 63 20 3d 20 73  er(p);..  rc = s
1e333 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1e334 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b  tPhaseOne(p, 0);
1e335 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1e336 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 63  TE_OK ){..    rc
1e337 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1e338 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2c  ommitPhaseTwo(p,
1e339 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c   0);..  }..  sql
1e33a 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1e33b 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
1e33c 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e  ..}....#ifndef N
1e33d 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  DEBUG../*..** Re
1e33e 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1e33f 6f 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73  of write-cursors
1e340 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61   open on this ha
1e341 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f  ndle. This is fo
1e342 72 20 75 73 65 0d 0a 2a 2a 20 69 6e 20 61 73 73  r use..** in ass
1e343 65 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e  ert() expression
1e344 73 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79  s, so it is only
1e345 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45   compiled if NDE
1e346 42 55 47 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 64  BUG is not..** d
1e347 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  efined...**..** 
1e348 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
1e349 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
1e34a 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72  , a write-cursor
1e34b 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74   is any cursor t
1e34c 68 61 74 0d 0a 2a 2a 20 69 73 20 63 61 70 61 62  hat..** is capab
1e34d 6c 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f  le of writing to
1e34e 20 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54   the databse.  T
1e34f 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75  hat means the cu
1e350 72 73 6f 72 20 77 61 73 0d 0a 2a 2a 20 6f 72 69  rsor was..** ori
1e351 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66  ginally opened f
1e352 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74  or writing and t
1e353 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f  he cursor has no
1e354 74 20 62 65 20 64 69 73 61 62 6c 65 64 0d 0a 2a  t be disabled..*
1e355 2a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73 20  * by having its 
1e356 73 74 61 74 65 20 63 68 61 6e 67 65 64 20 74 6f  state changed to
1e357 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0d 0a   CURSOR_FAULT...
1e358 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */..static int c
1e359 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73  ountWriteCursors
1e35a 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
1e35b 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ..  BtCursor *pC
1e35c 75 72 3b 0d 0a 20 20 69 6e 74 20 72 20 3d 20 30  ur;..  int r = 0
1e35d 3b 0d 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42  ;..  for(pCur=pB
1e35e 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72  t->pCursor; pCur
1e35f 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65  ; pCur=pCur->pNe
1e360 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 43  xt){..    if( pC
1e361 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43  ur->wrFlag && pC
1e362 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  ur->eState!=CURS
1e363 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20  OR_FAULT ) r++; 
1e364 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
1e365 72 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  r;..}..#endif...
1e366 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./*..** This rou
1e367 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74  tine sets the st
1e368 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41  ate to CURSOR_FA
1e369 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ULT and the erro
1e36a 72 0d 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72  r..** code to er
1e36b 72 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20  rCode for every 
1e36c 63 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72  cursor on BtShar
1e36d 65 64 20 74 68 61 74 20 70 42 74 72 65 65 0d 0a  ed that pBtree..
1e36e 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0d 0a  ** references...
1e36f 2a 2a 0d 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  **..** Every cur
1e370 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 2c 20  sor is tripped, 
1e371 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72  including cursor
1e372 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0d 0a 2a  s that belong..*
1e373 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62  * to other datab
1e374 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
1e375 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62  that happen to b
1e376 65 20 73 68 61 72 69 6e 67 0d 0a 2a 2a 20 74 68  e sharing..** th
1e377 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42 74  e cache with pBt
1e378 72 65 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ree...**..** Thi
1e379 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63  s routine gets c
1e37a 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c  alled when a rol
1e37b 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0d 0a 2a  lback occurs...*
1e37c 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73  * All cursors us
1e37d 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 63  ing the same cac
1e37e 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 70  he must be tripp
1e37f 65 64 0d 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e  ed..** to preven
1e380 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69  t them from tryi
1e381 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 74  ng to use the bt
1e382 72 65 65 20 61 66 74 65 72 0d 0a 2a 2a 20 74 68  ree after..** th
1e383 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65  e rollback.  The
1e384 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61   rollback may ha
1e385 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65  ve deleted table
1e386 73 0d 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72  s..** or moved r
1e387 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74  oot pages, so it
1e388 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65   is not sufficie
1e389 6e 74 20 74 6f 0d 0a 2a 2a 20 73 61 76 65 20 74  nt to..** save t
1e38a 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
1e38b 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72  cursor.  The cur
1e38c 73 6f 72 20 6d 75 73 74 20 62 65 0d 0a 2a 2a 20  sor must be..** 
1e38d 69 6e 76 61 6c 69 64 61 74 65 64 2e 0d 0a 2a 2f  invalidated...*/
1e38e 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1e38f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
1e390 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
1e391 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1e392 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0d 0a 20  int errCode){.. 
1e393 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0d 0a 20   BtCursor *p;.. 
1e394 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1e395 65 72 28 70 42 74 72 65 65 29 3b 0d 0a 20 20 66  er(pBtree);..  f
1e396 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74  or(p=pBtree->pBt
1e397 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
1e398 70 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20  p->pNext){..    
1e399 69 6e 74 20 69 3b 0d 0a 20 20 20 20 73 71 6c 69  int i;..    sqli
1e39a 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
1e39b 73 6f 72 28 70 29 3b 0d 0a 20 20 20 20 70 2d 3e  sor(p);..    p->
1e39c 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
1e39d 46 41 55 4c 54 3b 0d 0a 20 20 20 20 70 2d 3e 73  FAULT;..    p->s
1e39e 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43 6f 64  kipNext = errCod
1e39f 65 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  e;..    for(i=0;
1e3a0 20 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 2b   i<=p->iPage; i+
1e3a1 2b 29 7b 0d 0a 20 20 20 20 20 20 72 65 6c 65 61  +){..      relea
1e3a2 73 65 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65  sePage(p->apPage
1e3a3 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20 70 2d 3e  [i]);..      p->
1e3a4 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0d 0a  apPage[i] = 0;..
1e3a5 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71      }..  }..  sq
1e3a6 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1e3a7 70 42 74 72 65 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  pBtree);..}..../
1e3a8 2a 0d 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74  *..** Rollback t
1e3a9 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
1e3aa 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c  n progress.  All
1e3ab 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65   cursors will be
1e3ac 0d 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62  ..** invalided b
1e3ad 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  y this operation
1e3ae 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
1e3af 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 0d 0a  o use a cursor..
1e3b0 2a 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e  ** that was open
1e3b1 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
1e3b2 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74  g of this operat
1e3b3 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0d  ion will result.
1e3b4 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e  .** in an error.
1e3b5 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 77 69  ..**..** This wi
1e3b6 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77  ll release the w
1e3b7 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite lock on the
1e3b8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1e3b9 20 49 66 20 74 68 65 72 65 0d 0a 2a 2a 20 61 72   If there..** ar
1e3ba 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73  e no active curs
1e3bb 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c  ors, it also rel
1e3bc 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c  eases the read l
1e3bd 6f 63 6b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ock...*/..SQLITE
1e3be 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1e3bf 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
1e3c0 6b 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20  k(Btree *p){..  
1e3c1 69 6e 74 20 72 63 3b 0d 0a 20 20 42 74 53 68 61  int rc;..  BtSha
1e3c2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1e3c3 74 3b 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  t;..  MemPage *p
1e3c4 50 61 67 65 31 3b 0d 0a 0d 0a 20 20 73 71 6c 69  Page1;....  sqli
1e3c5 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
1e3c6 3b 0d 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  ;..  rc = saveAl
1e3c7 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c  lCursors(pBt, 0,
1e3c8 20 30 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51   0);..#ifndef SQ
1e3c9 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1e3ca 5f 43 41 43 48 45 0d 0a 20 20 69 66 28 20 72 63  _CACHE..  if( rc
1e3cb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
1e3cc 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61      /* This is a
1e3cd 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74   horrible situat
1e3ce 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61  ion. An IO or ma
1e3cf 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63  lloc() error occ
1e3d0 75 72 72 65 64 20 77 68 69 6c 73 74 0d 0a 20 20  urred whilst..  
1e3d1 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73    ** trying to s
1e3d2 61 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ave cursor posit
1e3d3 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73  ions. If this is
1e3d4 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
1e3d5 6c 6c 62 61 63 6b 20 28 61 73 0d 0a 20 20 20 20  llback (as..    
1e3d6 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ** the result of
1e3d7 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d   a constraint, m
1e3d8 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
1e3d9 6f 72 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65  or IO error) the
1e3da 6e 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 63  n ..    ** the c
1e3db 61 63 68 65 20 6d 61 79 20 62 65 20 69 6e 74 65  ache may be inte
1e3dc 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74  rnally inconsist
1e3dd 65 6e 74 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e  ent (not contain
1e3de 20 76 61 6c 69 64 20 74 72 65 65 73 29 20 73 6f   valid trees) so
1e3df 0d 0a 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 6e  ..    ** we cann
1e3e0 6f 74 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e  ot simply return
1e3e1 20 74 68 65 20 65 72 72 6f 72 20 74 6f 20 74 68   the error to th
1e3e2 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73 74 65 61  e caller. Instea
1e3e3 64 2c 20 61 62 6f 72 74 20 0d 0a 20 20 20 20 2a  d, abort ..    *
1e3e4 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20 74 68  * all queries th
1e3e5 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e 67 20  at may be using 
1e3e6 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72 73 6f  any of the curso
1e3e7 72 73 20 74 68 61 74 20 66 61 69 6c 65 64 20 74  rs that failed t
1e3e8 6f 20 73 61 76 65 2e 0d 0a 20 20 20 20 2a 2f 0d  o save...    */.
1e3e9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1e3ea 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28  eTripAllCursors(
1e3eb 70 2c 20 72 63 29 3b 0d 0a 20 20 7d 0d 0a 23 65  p, rc);..  }..#e
1e3ec 6e 64 69 66 0d 0a 20 20 62 74 72 65 65 49 6e 74  ndif..  btreeInt
1e3ed 65 67 72 69 74 79 28 70 29 3b 0d 0a 0d 0a 20 20  egrity(p);....  
1e3ee 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
1e3ef 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0d 0a  TRANS_WRITE ){..
1e3f0 20 20 20 20 69 6e 74 20 72 63 32 3b 0d 0a 0d 0a      int rc2;....
1e3f1 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e      assert( TRAN
1e3f2 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e  S_WRITE==pBt->in
1e3f3 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0d 0a  Transaction );..
1e3f4 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65      rc2 = sqlite
1e3f5 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
1e3f6 42 74 2d 3e 70 50 61 67 65 72 29 3b 0d 0a 20 20  Bt->pPager);..  
1e3f7 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54    if( rc2!=SQLIT
1e3f8 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72  E_OK ){..      r
1e3f9 63 20 3d 20 72 63 32 3b 0d 0a 20 20 20 20 7d 0d  c = rc2;..    }.
1e3fa 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ...    /* The ro
1e3fb 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20  llback may have 
1e3fc 64 65 73 74 72 6f 79 65 64 20 74 68 65 20 70 50  destroyed the pP
1e3fd 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61 6c 75  age1->aData valu
1e3fe 65 2e 20 20 53 6f 0d 0a 20 20 20 20 2a 2a 20 63  e.  So..    ** c
1e3ff 61 6c 6c 20 62 74 72 65 65 47 65 74 50 61 67 65  all btreeGetPage
1e400 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 61  () on page 1 aga
1e401 69 6e 20 74 6f 20 6d 61 6b 65 0d 0a 20 20 20 20  in to make..    
1e402 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e  ** sure pPage1->
1e403 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72  aData is set cor
1e404 72 65 63 74 6c 79 2e 20 2a 2f 0d 0a 20 20 20 20  rectly. */..    
1e405 69 66 28 20 62 74 72 65 65 47 65 74 50 61 67 65  if( btreeGetPage
1e406 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31  (pBt, 1, &pPage1
1e407 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  , 0)==SQLITE_OK 
1e408 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 50  ){..      int nP
1e409 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 32  age = get4byte(2
1e40a 38 2b 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61  8+(u8*)pPage1->a
1e40b 44 61 74 61 29 3b 0d 0a 20 20 20 20 20 20 74 65  Data);..      te
1e40c 73 74 63 61 73 65 28 20 6e 50 61 67 65 3d 3d 30  stcase( nPage==0
1e40d 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e   );..      if( n
1e40e 50 61 67 65 3d 3d 30 20 29 20 73 71 6c 69 74 65  Page==0 ) sqlite
1e40f 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
1e410 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
1e411 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  age);..      tes
1e412 74 63 61 73 65 28 20 70 42 74 2d 3e 6e 50 61 67  tcase( pBt->nPag
1e413 65 21 3d 6e 50 61 67 65 20 29 3b 0d 0a 20 20 20  e!=nPage );..   
1e414 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20     pBt->nPage = 
1e415 6e 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 72 65  nPage;..      re
1e416 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31  leasePage(pPage1
1e417 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61  );..    }..    a
1e418 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74  ssert( countWrit
1e419 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30  eCursors(pBt)==0
1e41a 20 29 3b 0d 0a 20 20 20 20 70 42 74 2d 3e 69 6e   );..    pBt->in
1e41b 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52  Transaction = TR
1e41c 41 4e 53 5f 52 45 41 44 3b 0d 0a 20 20 7d 0d 0a  ANS_READ;..  }..
1e41d 0d 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e  ..  btreeEndTran
1e41e 73 61 63 74 69 6f 6e 28 70 29 3b 0d 0a 20 20 73  saction(p);..  s
1e41f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1e420 28 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  (p);..  return r
1e421 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1e422 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65 6e  Start a statemen
1e423 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e  t subtransaction
1e424 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63  . The subtransac
1e425 74 69 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65 20  tion can can be 
1e426 72 6f 6c 6c 65 64 0d 0a 2a 2a 20 62 61 63 6b 20  rolled..** back 
1e427 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66  independently of
1e428 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61   the main transa
1e429 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20  ction. You must 
1e42a 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
1e42b 69 6f 6e 20 0d 0a 2a 2a 20 62 65 66 6f 72 65 20  ion ..** before 
1e42c 73 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72  starting a subtr
1e42d 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73  ansaction. The s
1e42e 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ubtransaction is
1e42f 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63   ended automatic
1e430 61 6c 6c 79 20 0d 0a 2a 2a 20 69 66 20 74 68 65  ally ..** if the
1e431 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f   main transactio
1e432 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c  n commits or rol
1e433 6c 73 20 62 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  ls back...**..**
1e434 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   Statement subtr
1e435 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75  ansactions are u
1e436 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76  sed around indiv
1e437 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  idual SQL statem
1e438 65 6e 74 73 0d 0a 2a 2a 20 74 68 61 74 20 61 72  ents..** that ar
1e439 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  e contained with
1e43a 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d  in a BEGIN...COM
1e43b 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61  MIT block.  If a
1e43c 20 63 6f 6e 73 74 72 61 69 6e 74 0d 0a 2a 2a 20   constraint..** 
1e43d 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74  error occurs wit
1e43e 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hin the statemen
1e43f 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  t, the effect of
1e440 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d   that one statem
1e441 65 6e 74 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 72  ent..** can be r
1e442 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f  olled back witho
1e443 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c  ut having to rol
1e444 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65  lback the entire
1e445 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a   transaction...*
1e446 2a 0d 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e  *..** A statemen
1e447 74 20 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f  t sub-transactio
1e448 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n is implemented
1e449 20 61 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73   as an anonymous
1e44a 20 73 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0d   savepoint. The.
1e44b 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73 73 65 64  .** value passed
1e44c 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
1e44d 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
1e44e 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1e44f 73 61 76 65 70 6f 69 6e 74 73 2c 0d 0a 2a 2a 20  savepoints,..** 
1e450 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e 65  including the ne
1e451 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65  w anonymous save
1e452 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 74  point, open on t
1e453 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65 2e 20  he B-Tree. i.e. 
1e454 69 66 20 74 68 65 72 65 0d 0a 2a 2a 20 61 72 65  if there..** are
1e455 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65 70   no active savep
1e456 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68  oints and no oth
1e457 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  er statement-tra
1e458 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c 0d  nsactions open,.
1e459 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69  .** iStatement i
1e45a 73 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d  s 1. This anonym
1e45b 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61  ous savepoint ca
1e45c 6e 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72  n be released or
1e45d 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0d 0a 2a 2a   rolled back..**
1e45e 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
1e45f 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
1e460 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f  () function...*/
1e461 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1e462 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1e463 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65  eBeginStmt(Btree
1e464 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d   *p, int iStatem
1e465 65 6e 74 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  ent){..  int rc;
1e466 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ..  BtShared *pB
1e467 74 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 73  t = p->pBt;..  s
1e468 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1e469 28 70 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  (p);..  assert( 
1e46a 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1e46b 53 5f 57 52 49 54 45 20 29 3b 0d 0a 20 20 61 73  S_WRITE );..  as
1e46c 73 65 72 74 28 20 28 70 42 74 2d 3e 62 74 73 46  sert( (pBt->btsF
1e46d 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 5f  lags & BTS_READ_
1e46e 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0d 0a 20 20 61  ONLY)==0 );..  a
1e46f 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e  ssert( iStatemen
1e470 74 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  t>0 );..  assert
1e471 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e  ( iStatement>p->
1e472 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29  db->nSavepoint )
1e473 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  ;..  assert( pBt
1e474 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1e475 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0d  =TRANS_WRITE );.
1e476 0a 20 20 2f 2a 20 41 74 20 74 68 65 20 70 61 67  .  /* At the pag
1e477 65 72 20 6c 65 76 65 6c 2c 20 61 20 73 74 61 74  er level, a stat
1e478 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1e479 6e 20 69 73 20 61 20 73 61 76 65 70 6f 69 6e 74  n is a savepoint
1e47a 20 77 69 74 68 0d 0a 20 20 2a 2a 20 61 6e 20 69   with..  ** an i
1e47b 6e 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61  ndex greater tha
1e47c 6e 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73  n all savepoints
1e47d 20 63 72 65 61 74 65 64 20 65 78 70 6c 69 63 69   created explici
1e47e 74 6c 79 20 75 73 69 6e 67 0d 0a 20 20 2a 2a 20  tly using..  ** 
1e47f 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  SQL statements. 
1e480 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
1e481 20 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f   open, release o
1e482 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0d 0a  r rollback any..
1e483 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f    ** such savepo
1e484 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73  ints while the s
1e485 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1e486 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
1e487 73 20 61 63 74 69 76 65 2e 0d 0a 20 20 2a 2f 0d  s active...  */.
1e488 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1e489 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
1e48a 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
1e48b 53 74 61 74 65 6d 65 6e 74 29 3b 0d 0a 20 20 73  Statement);..  s
1e48c 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1e48d 28 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  (p);..  return r
1e48e 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1e48f 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
1e490 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
1e491 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77  tion, op, is alw
1e492 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  ays SAVEPOINT_RO
1e493 4c 4c 42 41 43 4b 0d 0a 2a 2a 20 6f 72 20 53 41  LLBACK..** or SA
1e494 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e  VEPOINT_RELEASE.
1e495 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65   This function e
1e496 69 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f  ither releases o
1e497 72 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65  r rolls back the
1e498 0d 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69  ..** savepoint i
1e499 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72  dentified by par
1e49a 61 6d 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e  ameter iSavepoin
1e49b 74 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  t, depending on 
1e49c 74 68 65 20 76 61 6c 75 65 20 0d 0a 2a 2a 20 6f  the value ..** o
1e49d 66 20 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f  f op...**..** No
1e49e 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 70 6f 69  rmally, iSavepoi
1e49f 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68  nt is greater th
1e4a0 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a  an or equal to z
1e4a1 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c 20 69 66  ero. However, if
1e4a2 20 6f 70 20 69 73 0d 0a 2a 2a 20 53 41 56 45 50   op is..** SAVEP
1e4a3 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74  OINT_ROLLBACK, t
1e4a4 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 6d  hen iSavepoint m
1e4a5 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e 20 49  ay also be -1. I
1e4a6 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1e4a7 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  ..** contents of
1e4a8 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e   the entire tran
1e4a9 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c 6c  saction are roll
1e4aa 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73  ed back. This is
1e4ab 20 64 69 66 66 65 72 65 6e 74 0d 0a 2a 2a 20 66   different..** f
1e4ac 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61  rom a normal tra
1e4ad 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63  nsaction rollbac
1e4ae 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61  k, as no locks a
1e4af 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20  re released and 
1e4b0 74 68 65 0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  the..** transact
1e4b1 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e  ion remains open
1e4b2 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
1e4b3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1e4b4 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
1e4b5 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70  Btree *p, int op
1e4b6 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  , int iSavepoint
1e4b7 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
1e4b8 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28  QLITE_OK;..  if(
1e4b9 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73   p && p->inTrans
1e4ba 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  ==TRANS_WRITE ){
1e4bb 0d 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  ..    BtShared *
1e4bc 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20  pBt = p->pBt;.. 
1e4bd 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53     assert( op==S
1e4be 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1e4bf 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   || op==SAVEPOIN
1e4c0 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0d 0a 20  T_ROLLBACK );.. 
1e4c1 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65     assert( iSave
1e4c2 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61  point>=0 || (iSa
1e4c3 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f  vepoint==-1 && o
1e4c4 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
1e4c5 4c 42 41 43 4b 29 20 29 3b 0d 0a 20 20 20 20 73  LBACK) );..    s
1e4c6 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1e4c7 28 70 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  (p);..    rc = s
1e4c8 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70  qlite3PagerSavep
1e4c9 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  oint(pBt->pPager
1e4ca 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74  , op, iSavepoint
1e4cb 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  );..    if( rc==
1e4cc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1e4cd 20 20 20 20 69 66 28 20 69 53 61 76 65 70 6f 69      if( iSavepoi
1e4ce 6e 74 3c 30 20 26 26 20 28 70 42 74 2d 3e 62 74  nt<0 && (pBt->bt
1e4cf 73 46 6c 61 67 73 20 26 20 42 54 53 5f 49 4e 49  sFlags & BTS_INI
1e4d0 54 49 41 4c 4c 59 5f 45 4d 50 54 59 29 21 3d 30  TIALLY_EMPTY)!=0
1e4d1 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 42 74   ){..        pBt
1e4d2 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20  ->nPage = 0;..  
1e4d3 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 63 20      }..      rc 
1e4d4 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42  = newDatabase(pB
1e4d5 74 29 3b 0d 0a 20 20 20 20 20 20 70 42 74 2d 3e  t);..      pBt->
1e4d6 6e 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  nPage = get4byte
1e4d7 28 32 38 20 2b 20 70 42 74 2d 3e 70 50 61 67 65  (28 + pBt->pPage
1e4d8 31 2d 3e 61 44 61 74 61 29 3b 0d 0a 0d 0a 20 20  1->aData);....  
1e4d9 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1e4da 61 73 65 20 73 69 7a 65 20 77 61 73 20 77 72 69  ase size was wri
1e4db 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6f 66  tten into the of
1e4dc 66 73 65 74 20 32 38 20 6f 66 20 74 68 65 20 68  fset 28 of the h
1e4dd 65 61 64 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20  eader..      ** 
1e4de 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  when the transac
1e4df 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 73 6f  tion started, so
1e4e0 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68   we know that th
1e4e1 65 20 76 61 6c 75 65 20 61 74 20 6f 66 66 73 65  e value at offse
1e4e2 74 0d 0a 20 20 20 20 20 20 2a 2a 20 32 38 20 69  t..      ** 28 i
1e4e3 73 20 6e 6f 6e 7a 65 72 6f 2e 20 2a 2f 0d 0a 20  s nonzero. */.. 
1e4e4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74       assert( pBt
1e4e5 2d 3e 6e 50 61 67 65 3e 30 20 29 3b 0d 0a 20 20  ->nPage>0 );..  
1e4e6 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1e4e7 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a  BtreeLeave(p);..
1e4e8 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
1e4e9 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
1e4ea 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73  reate a new curs
1e4eb 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65  or for the BTree
1e4ec 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f   whose root is o
1e4ed 6e 20 74 68 65 20 70 61 67 65 0d 0a 2a 2a 20 69  n the page..** i
1e4ee 54 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61 64  Table. If a read
1e4ef 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73 20  -only cursor is 
1e4f0 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73  requested, it is
1e4f1 20 61 73 73 75 6d 65 64 20 74 68 61 74 0d 0a 2a   assumed that..*
1e4f2 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72  * the caller alr
1e4f3 65 61 64 79 20 68 61 73 20 61 74 20 6c 65 61 73  eady has at leas
1e4f4 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72  t a read-only tr
1e4f5 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0d 0a  ansaction open..
1e4f6 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
1e4f7 73 65 20 61 6c 72 65 61 64 79 2e 20 49 66 20 61  se already. If a
1e4f8 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73   write-cursor is
1e4f9 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e   requested, then
1e4fa 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20  ..** the caller 
1e4fb 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 61  is assumed to ha
1e4fc 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65  ve an open write
1e4fd 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 2a   transaction...*
1e4fe 2a 0d 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d  *..** If wrFlag=
1e4ff 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  =0, then the cur
1e500 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  sor can only be 
1e501 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67  used for reading
1e502 2e 0d 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d  ...** If wrFlag=
1e503 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  =1, then the cur
1e504 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20  sor can be used 
1e505 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66  for reading or f
1e506 6f 72 0d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69  or..** writing i
1e507 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f  f other conditio
1e508 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61  ns for writing a
1e509 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68  re also met.  Th
1e50a 65 73 65 0d 0a 2a 2a 20 61 72 65 20 74 68 65 20  ese..** are the 
1e50b 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20  conditions that 
1e50c 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f  must be met in o
1e50d 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67  rder for writing
1e50e 20 74 6f 0d 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77   to..** be allow
1e50f 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 31 3a 20 20  ed:..**..** 1:  
1e510 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20  The cursor must 
1e511 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64  have been opened
1e512 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0d   with wrFlag==1.
1e513 0a 2a 2a 0d 0a 2a 2a 20 32 3a 20 20 4f 74 68 65  .**..** 2:  Othe
1e514 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
1e515 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72  ctions that shar
1e516 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72  e the same pager
1e517 20 63 61 63 68 65 0d 0a 2a 2a 20 20 20 20 20 62   cache..**     b
1e518 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74  ut which are not
1e519 20 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43   in the READ_UNC
1e51a 4f 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d  OMMITTED state m
1e51b 61 79 20 6e 6f 74 20 68 61 76 65 0d 0a 2a 2a 20  ay not have..** 
1e51c 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e      cursors open
1e51d 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20   with wrFlag==0 
1e51e 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c  on the same tabl
1e51f 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0d 0a 2a  e.  Otherwise..*
1e520 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65  *     the change
1e521 73 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 77  s made by this w
1e522 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75 6c  rite cursor woul
1e523 64 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 0d  d be visible to.
1e524 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61 64  .**     the read
1e525 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20   cursors in the 
1e526 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
1e527 6f 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  onnection...**..
1e528 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74 61 62  ** 3:  The datab
1e529 61 73 65 20 6d 75 73 74 20 62 65 20 77 72 69 74  ase must be writ
1e52a 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61  able (not on rea
1e52b 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0d 0a 2a  d-only media)..*
1e52c 2a 0d 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20  *..** 4:  There 
1e52d 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 76  must be an activ
1e52e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a  e transaction...
1e52f 2a 2a 0d 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69  **..** No checki
1e530 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61  ng is done to ma
1e531 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67  ke sure that pag
1e532 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20  e iTable really 
1e533 69 73 20 74 68 65 0d 0a 2a 2a 20 72 6f 6f 74 20  is the..** root 
1e534 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65  page of a b-tree
1e535 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c  .  If it is not,
1e536 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
1e537 20 61 63 71 75 69 72 65 64 0d 0a 2a 2a 20 77 69   acquired..** wi
1e538 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72  ll not work corr
1e539 65 63 74 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  ectly...**..** I
1e53a 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1e53b 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72  t the sqlite3Btr
1e53c 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 20 68  eeCursorZero() h
1e53d 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 0d 0a  as been called..
1e53e 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 6e  ** on pCur to in
1e53f 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
1e540 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 20  ory space prior 
1e541 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
1e542 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d 0a 73   routine...*/..s
1e543 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43  tatic int btreeC
1e544 75 72 73 6f 72 28 0d 0a 20 20 42 74 72 65 65 20  ursor(..  Btree 
1e545 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1e546 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e547 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f   /* The btree */
1e548 0d 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  ..  int iTable, 
1e549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e54a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
1e54b 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
1e54c 20 74 6f 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20 69   to open */..  i
1e54d 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20  nt wrFlag,      
1e54e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e54f 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72        /* 1 to wr
1e550 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79  ite. 0 read-only
1e551 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 4b 65   */..  struct Ke
1e552 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
1e553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e554 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f   First arg to co
1e555 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
1e556 6e 20 2a 2f 0d 0a 20 20 42 74 43 75 72 73 6f 72  n */..  BtCursor
1e557 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20   *pCur          
1e558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e559 2a 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20  * Space for new 
1e55a 63 75 72 73 6f 72 20 2a 2f 0d 0a 29 7b 0d 0a 20  cursor */..){.. 
1e55b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1e55c 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20   p->pBt;        
1e55d 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65          /* Share
1e55e 64 20 62 2d 74 72 65 65 20 68 61 6e 64 6c 65 20  d b-tree handle 
1e55f 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
1e560 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
1e561 73 4d 75 74 65 78 28 70 29 20 29 3b 0d 0a 20 20  sMutex(p) );..  
1e562 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d  assert( wrFlag==
1e563 30 20 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29  0 || wrFlag==1 )
1e564 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 66 6f  ;....  /* The fo
1e565 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 73  llowing assert s
1e566 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69 66 79  tatements verify
1e567 20 74 68 61 74 20 69 66 20 74 68 69 73 20 69 73   that if this is
1e568 20 61 20 73 68 61 72 61 62 6c 65 20 0d 0a 20 20   a sharable ..  
1e569 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62 61  ** b-tree databa
1e56a 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  se, the connecti
1e56b 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68  on is holding th
1e56c 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65  e required table
1e56d 20 6c 6f 63 6b 73 2c 20 0d 0a 20 20 2a 2a 20 61   locks, ..  ** a
1e56e 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  nd that no other
1e56f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
1e570 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20  any open cursor 
1e571 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77  that conflicts w
1e572 69 74 68 20 0d 0a 20 20 2a 2a 20 74 68 69 73 20  ith ..  ** this 
1e573 6c 6f 63 6b 2e 20 20 2a 2f 0d 0a 20 20 61 73 73  lock.  */..  ass
1e574 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61  ert( hasSharedCa
1e575 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
1e576 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f  iTable, pKeyInfo
1e577 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29  !=0, wrFlag+1) )
1e578 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 77 72 46  ;..  assert( wrF
1e579 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65  lag==0 || !hasRe
1e57a 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69  adConflicts(p, i
1e57b 54 61 62 6c 65 29 20 29 3b 0d 0a 0d 0a 20 20 2f  Table) );....  /
1e57c 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
1e57d 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 70 65  e caller has ope
1e57e 6e 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64  ned the required
1e57f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f   transaction. */
1e580 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ..  assert( p->i
1e581 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e  nTrans>TRANS_NON
1e582 45 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  E );..  assert( 
1e583 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e  wrFlag==0 || p->
1e584 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
1e585 52 49 54 45 20 29 3b 0d 0a 20 20 61 73 73 65 72  RITE );..  asser
1e586 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26  t( pBt->pPage1 &
1e587 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  & pBt->pPage1->a
1e588 44 61 74 61 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  Data );....  if(
1e589 20 4e 45 56 45 52 28 77 72 46 6c 61 67 20 26 26   NEVER(wrFlag &&
1e58a 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20   (pBt->btsFlags 
1e58b 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29  & BTS_READ_ONLY)
1e58c 21 3d 30 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  !=0) ){..    ret
1e58d 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
1e58e 4e 4c 59 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  NLY;..  }..  if(
1e58f 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 62 74   iTable==1 && bt
1e590 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74  reePagecount(pBt
1e591 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73 73  )==0 ){..    ass
1e592 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 29  ert( wrFlag==0 )
1e593 3b 0d 0a 20 20 20 20 69 54 61 62 6c 65 20 3d 20  ;..    iTable = 
1e594 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  0;..  }....  /* 
1e595 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  Now that no othe
1e596 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63  r errors can occ
1e597 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69  ur, finish filli
1e598 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73  ng in the BtCurs
1e599 6f 72 0d 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  or..  ** variabl
1e59a 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20  es and link the 
1e59b 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20  cursor into the 
1e59c 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20  BtShared list.  
1e59d 2a 2f 0d 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f  */..  pCur->pgno
1e59e 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61  Root = (Pgno)iTa
1e59f 62 6c 65 3b 0d 0a 20 20 70 43 75 72 2d 3e 69 50  ble;..  pCur->iP
1e5a0 61 67 65 20 3d 20 2d 31 3b 0d 0a 20 20 70 43 75  age = -1;..  pCu
1e5a1 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b  r->pKeyInfo = pK
1e5a2 65 79 49 6e 66 6f 3b 0d 0a 20 20 70 43 75 72 2d  eyInfo;..  pCur-
1e5a3 3e 70 42 74 72 65 65 20 3d 20 70 3b 0d 0a 20 20  >pBtree = p;..  
1e5a4 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b  pCur->pBt = pBt;
1e5a5 0d 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  ..  pCur->wrFlag
1e5a6 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b 0d 0a   = (u8)wrFlag;..
1e5a7 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20    pCur->pNext = 
1e5a8 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0d 0a 20  pBt->pCursor;.. 
1e5a9 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74   if( pCur->pNext
1e5aa 20 29 7b 0d 0a 20 20 20 20 70 43 75 72 2d 3e 70   ){..    pCur->p
1e5ab 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43  Next->pPrev = pC
1e5ac 75 72 3b 0d 0a 20 20 7d 0d 0a 20 20 70 42 74 2d  ur;..  }..  pBt-
1e5ad 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b  >pCursor = pCur;
1e5ae 0d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65  ..  pCur->eState
1e5af 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
1e5b0 44 3b 0d 0a 20 20 70 43 75 72 2d 3e 63 61 63 68  D;..  pCur->cach
1e5b1 65 64 52 6f 77 69 64 20 3d 20 30 3b 0d 0a 20 20  edRowid = 0;..  
1e5b2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1e5b3 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ;..}..SQLITE_PRI
1e5b4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e5b5 42 74 72 65 65 43 75 72 73 6f 72 28 0d 0a 20 20  BtreeCursor(..  
1e5b6 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20  Btree *p,       
1e5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5b8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e5b9 68 65 20 62 74 72 65 65 20 2a 2f 0d 0a 20 20 69  he btree */..  i
1e5ba 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
1e5bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5bc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
1e5bd 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
1e5be 20 74 6f 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20 69   to open */..  i
1e5bf 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20  nt wrFlag,      
1e5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5c1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1e5c2 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64  to write. 0 read
1e5c3 2d 6f 6e 6c 79 20 2a 2f 0d 0a 20 20 73 74 72 75  -only */..  stru
1e5c4 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  ct KeyInfo *pKey
1e5c5 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Info,           
1e5c6 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
1e5c7 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65   arg to xCompare
1e5c8 28 29 20 2a 2f 0d 0a 20 20 42 74 43 75 72 73 6f  () */..  BtCurso
1e5c9 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20  r *pCur         
1e5ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5cb 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65       /* Write ne
1e5cc 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f  w cursor here */
1e5cd 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  ..){..  int rc;.
1e5ce 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1e5cf 6e 74 65 72 28 70 29 3b 0d 0a 20 20 72 63 20 3d  nter(p);..  rc =
1e5d0 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20   btreeCursor(p, 
1e5d1 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20  iTable, wrFlag, 
1e5d2 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b  pKeyInfo, pCur);
1e5d3 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1e5d4 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20 72 65 74  Leave(p);..  ret
1e5d5 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1e5d6 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
1e5d7 73 69 7a 65 20 6f 66 20 61 20 42 74 43 75 72 73  size of a BtCurs
1e5d8 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 62 79 74  or object in byt
1e5d9 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  es...**..** This
1e5da 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 6e   interfaces is n
1e5db 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75 73  eeded so that us
1e5dc 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20 63  ers of cursors c
1e5dd 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0d 0a  an preallocate..
1e5de 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74  ** sufficient st
1e5df 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20  orage to hold a 
1e5e0 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43  cursor.  The BtC
1e5e1 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20  ursor object is 
1e5e2 6f 70 61 71 75 65 0d 0a 2a 2a 20 74 6f 20 75 73  opaque..** to us
1e5e3 65 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e  ers so they cann
1e5e4 6f 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66  ot do the sizeof
1e5e5 28 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20  () themselves - 
1e5e6 74 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0d 0a  they must call..
1e5e7 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ** this routine.
1e5e8 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1e5e9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e5ea 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28  BtreeCursorSize(
1e5eb 76 6f 69 64 29 7b 0d 0a 20 20 72 65 74 75 72 6e  void){..  return
1e5ec 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 42   ROUND8(sizeof(B
1e5ed 74 43 75 72 73 6f 72 29 29 3b 0d 0a 7d 0d 0a 0d  tCursor));..}...
1e5ee 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ./*..** Initiali
1e5ef 7a 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77  ze memory that w
1e5f0 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ill be converted
1e5f1 20 69 6e 74 6f 20 61 20 42 74 43 75 72 73 6f 72   into a BtCursor
1e5f2 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a   object...**..**
1e5f3 20 54 68 65 20 73 69 6d 70 6c 65 20 61 70 70 72   The simple appr
1e5f4 6f 61 63 68 20 68 65 72 65 20 77 6f 75 6c 64 20  oach here would 
1e5f5 62 65 20 74 6f 20 6d 65 6d 73 65 74 28 29 20 74  be to memset() t
1e5f6 68 65 20 65 6e 74 69 72 65 20 6f 62 6a 65 63 74  he entire object
1e5f7 0d 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2e 20 20 42  ..** to zero.  B
1e5f8 75 74 20 69 74 20 74 75 72 6e 73 20 6f 75 74 20  ut it turns out 
1e5f9 74 68 61 74 20 74 68 65 20 61 70 50 61 67 65 5b  that the apPage[
1e5fa 5d 20 61 6e 64 20 61 69 49 64 78 5b 5d 20 61 72  ] and aiIdx[] ar
1e5fb 72 61 79 73 0d 0a 2a 2a 20 64 6f 20 6e 6f 74 20  rays..** do not 
1e5fc 6e 65 65 64 20 74 6f 20 62 65 20 7a 65 72 6f 65  need to be zeroe
1e5fd 64 20 61 6e 64 20 74 68 65 79 20 61 72 65 20 6c  d and they are l
1e5fe 61 72 67 65 2c 20 73 6f 20 77 65 20 63 61 6e 20  arge, so we can 
1e5ff 73 61 76 65 20 61 20 6c 6f 74 0d 0a 2a 2a 20 6f  save a lot..** o
1e600 66 20 72 75 6e 2d 74 69 6d 65 20 62 79 20 73 6b  f run-time by sk
1e601 69 70 70 69 6e 67 20 74 68 65 20 69 6e 69 74 69  ipping the initi
1e602 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 6f  alization of tho
1e603 73 65 20 65 6c 65 6d 65 6e 74 73 2e 0d 0a 2a 2f  se elements...*/
1e604 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
1e605 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
1e606 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74 43  eeCursorZero(BtC
1e607 75 72 73 6f 72 20 2a 70 29 7b 0d 0a 20 20 6d 65  ursor *p){..  me
1e608 6d 73 65 74 28 70 2c 20 30 2c 20 6f 66 66 73 65  mset(p, 0, offse
1e609 74 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 69 50  tof(BtCursor, iP
1e60a 61 67 65 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  age));..}..../*.
1e60b 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 63 68  .** Set the cach
1e60c 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6f  ed rowid value o
1e60d 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 69  f every cursor i
1e60e 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
1e60f 61 73 65 20 66 69 6c 65 0d 0a 2a 2a 20 61 73 20  ase file..** as 
1e610 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 20  pCur and having 
1e611 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 61  the same root pa
1e612 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 75  ge number as pCu
1e613 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  r.  The value is
1e614 0d 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77  ..** set to iRow
1e615 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 6c 79  id...**..** Only
1e616 20 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20   positive rowid 
1e617 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69  values are consi
1e618 64 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20  dered valid for 
1e619 74 68 69 73 20 63 61 63 68 65 2e 0d 0a 2a 2a 20  this cache...** 
1e61a 54 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 69  The cache is ini
1e61b 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f  tialized to zero
1e61c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20  , indicating an 
1e61d 69 6e 76 61 6c 69 64 20 63 61 63 68 65 2e 0d 0a  invalid cache...
1e61e 2a 2a 20 41 20 62 74 72 65 65 20 77 69 6c 6c 20  ** A btree will 
1e61f 77 6f 72 6b 20 66 69 6e 65 20 77 69 74 68 20 7a  work fine with z
1e620 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20  ero or negative 
1e621 72 6f 77 69 64 73 2e 20 20 57 65 20 6a 75 73 74  rowids.  We just
1e622 20 63 61 6e 6e 6f 74 0d 0a 2a 2a 20 63 61 63 68   cannot..** cach
1e623 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69  e zero or negati
1e624 76 65 20 72 6f 77 69 64 73 2c 20 77 68 69 63 68  ve rowids, which
1e625 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 20 74 68   means tables th
1e626 61 74 20 75 73 65 20 7a 65 72 6f 20 6f 72 0d 0a  at use zero or..
1e627 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69  ** negative rowi
1e628 64 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c  ds might run a l
1e629 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 42  ittle slower.  B
1e62a 75 74 20 69 6e 20 70 72 61 63 74 69 63 65 2c 20  ut in practice, 
1e62b 7a 65 72 6f 0d 0a 2a 2a 20 6f 72 20 6e 65 67 61  zero..** or nega
1e62c 74 69 76 65 20 72 6f 77 69 64 73 20 61 72 65 20  tive rowids are 
1e62d 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 6f  very uncommon so
1e62e 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f 74   this should not
1e62f 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0d 0a   be a problem...
1e630 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1e631 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1e632 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
1e633 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
1e634 72 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  r, sqlite3_int64
1e635 20 69 52 6f 77 69 64 29 7b 0d 0a 20 20 42 74 43   iRowid){..  BtC
1e636 75 72 73 6f 72 20 2a 70 3b 0d 0a 20 20 66 6f 72  ursor *p;..  for
1e637 28 70 3d 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43  (p=pCur->pBt->pC
1e638 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
1e639 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20  Next){..    if( 
1e63a 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75  p->pgnoRoot==pCu
1e63b 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 20 70 2d  r->pgnoRoot ) p-
1e63c 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 69  >cachedRowid = i
1e63d 52 6f 77 69 64 3b 0d 0a 20 20 7d 0d 0a 20 20 61  Rowid;..  }..  a
1e63e 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 61 63  ssert( pCur->cac
1e63f 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 69 64  hedRowid==iRowid
1e640 20 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   );..}..../*..**
1e641 20 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68   Return the cach
1e642 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  ed rowid for the
1e643 20 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20   given cursor.  
1e644 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65  A negative or ze
1e645 72 6f 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61  ro..** return va
1e646 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68  lue indicates th
1e647 61 74 20 74 68 65 20 72 6f 77 69 64 20 63 61 63  at the rowid cac
1e648 68 65 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e  he is invalid an
1e649 64 20 73 68 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20  d should be..** 
1e64a 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 74 68 65  ignored.  If the
1e64b 20 72 6f 77 69 64 20 63 61 63 68 65 20 68 61 73   rowid cache has
1e64c 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65   never before be
1e64d 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 61 0d 0a  en set, then a..
1e64e 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72  ** zero is retur
1e64f 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ned...*/..SQLITE
1e650 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
1e651 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74  _int64 sqlite3Bt
1e652 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69  reeGetCachedRowi
1e653 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
1e654 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 43 75  ){..  return pCu
1e655 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3b 0d  r->cachedRowid;.
1e656 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f  .}..../*..** Clo
1e657 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68  se a cursor.  Th
1e658 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74  e read lock on t
1e659 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1e65a 20 69 73 20 72 65 6c 65 61 73 65 64 0d 0a 2a 2a   is released..**
1e65b 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 63   when the last c
1e65c 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e  ursor is closed.
1e65d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1e65e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e65f 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72  BtreeCloseCursor
1e660 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1e661 7b 0d 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72  {..  Btree *pBtr
1e662 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  ee = pCur->pBtre
1e663 65 3b 0d 0a 20 20 69 66 28 20 70 42 74 72 65 65  e;..  if( pBtree
1e664 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d   ){..    int i;.
1e665 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70  .    BtShared *p
1e666 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0d  Bt = pCur->pBt;.
1e667 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1e668 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0d  eEnter(pBtree);.
1e669 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1e66a 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75  eClearCursor(pCu
1e66b 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 75  r);..    if( pCu
1e66c 72 2d 3e 70 50 72 65 76 20 29 7b 0d 0a 20 20 20  r->pPrev ){..   
1e66d 20 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e     pCur->pPrev->
1e66e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e  pNext = pCur->pN
1e66f 65 78 74 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  ext;..    }else{
1e670 0d 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 75  ..      pBt->pCu
1e671 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65  rsor = pCur->pNe
1e672 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  xt;..    }..    
1e673 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20  if( pCur->pNext 
1e674 29 7b 0d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  ){..      pCur->
1e675 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
1e676 43 75 72 2d 3e 70 50 72 65 76 3b 0d 0a 20 20 20  Cur->pPrev;..   
1e677 20 7d 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
1e678 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b   i<=pCur->iPage;
1e679 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 72 65   i++){..      re
1e67a 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
1e67b 61 70 50 61 67 65 5b 69 5d 29 3b 0d 0a 20 20 20  apPage[i]);..   
1e67c 20 7d 0d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74   }..    unlockBt
1e67d 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
1e67e 3b 0d 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74  ;..    invalidat
1e67f 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  eOverflowCache(p
1e680 43 75 72 29 3b 0d 0a 20 20 20 20 2f 2a 20 73 71  Cur);..    /* sq
1e681 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 29  lite3_free(pCur)
1e682 3b 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65  ; */..    sqlite
1e683 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72  3BtreeLeave(pBtr
1e684 65 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ee);..  }..  ret
1e685 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
1e686 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65  }..../*..** Make
1e687 20 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73   sure the BtCurs
1e688 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65  or* given in the
1e689 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20   argument has a 
1e68a 76 61 6c 69 64 0d 0a 2a 2a 20 42 74 43 75 72 73  valid..** BtCurs
1e68b 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72  or.info structur
1e68c 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74  e.  If it is not
1e68d 20 61 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20   already valid, 
1e68e 63 61 6c 6c 0d 0a 2a 2a 20 62 74 72 65 65 50 61  call..** btreePa
1e68f 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c  rseCell() to fil
1e690 6c 20 69 74 20 69 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a  l it in...**..**
1e691 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69   BtCursor.info i
1e692 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 65  s a cache of the
1e693 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
1e694 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c  the current cell
1e695 2e 0d 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73  ...** Using this
1e696 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74   cache reduces t
1e697 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c  he number of cal
1e698 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 65  ls to btreeParse
1e699 43 65 6c 6c 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  Cell()...**..** 
1e69a 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65  2007-06-25:  The
1e69b 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73  re is a bug in s
1e69c 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ome versions of 
1e69d 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20  MSVC that cause 
1e69e 74 68 65 0d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  the..** compiler
1e69f 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67   to crash when g
1e6a0 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20  etCellInfo() is 
1e6a1 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
1e6a2 20 6d 61 63 72 6f 2e 0d 0a 2a 2a 20 42 75 74 20   macro...** But 
1e6a3 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75  there is a measu
1e6a4 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76  reable speed adv
1e6a5 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20  antage to using 
1e6a6 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63  the macro on gcc
1e6a7 0d 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20  ..** (when less 
1e6a8 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a  compiler optimiz
1e6a9 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20  ations like -Os 
1e6aa 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20  or -O0 are used 
1e6ab 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 63 6f 6d 70  and the..** comp
1e6ac 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e  iler is not doin
1e6ad 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69  g agressive inli
1e6ae 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73  ning.)  So we us
1e6af 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f  e a real functio
1e6b0 6e 0d 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61  n..** for MSVC a
1e6b1 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65  nd a macro for e
1e6b2 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20  verything else. 
1e6b3 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0d 0a   Ticket #2457...
1e6b4 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
1e6b5 55 47 0d 0a 20 20 73 74 61 74 69 63 20 76 6f 69  UG..  static voi
1e6b6 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f  d assertCellInfo
1e6b7 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1e6b8 7b 0d 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20  {..    CellInfo 
1e6b9 69 6e 66 6f 3b 0d 0a 20 20 20 20 69 6e 74 20 69  info;..    int i
1e6ba 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61  Page = pCur->iPa
1e6bb 67 65 3b 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28  ge;..    memset(
1e6bc 26 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66  &info, 0, sizeof
1e6bd 28 69 6e 66 6f 29 29 3b 0d 0a 20 20 20 20 62 74  (info));..    bt
1e6be 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75  reeParseCell(pCu
1e6bf 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d  r->apPage[iPage]
1e6c0 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50  , pCur->aiIdx[iP
1e6c1 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0d 0a 20  age], &info);.. 
1e6c2 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 6d     assert( memcm
1e6c3 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e  p(&info, &pCur->
1e6c4 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66  info, sizeof(inf
1e6c5 6f 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 7d 0d 0a  o))==0 );..  }..
1e6c6 23 65 6c 73 65 0d 0a 20 20 23 64 65 66 69 6e 65  #else..  #define
1e6c7 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
1e6c8 78 29 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64  x)..#endif..#ifd
1e6c9 65 66 20 5f 4d 53 43 5f 56 45 52 0d 0a 20 20 2f  ef _MSC_VER..  /
1e6ca 2a 20 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e  * Use a real fun
1e6cb 63 74 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f  ction in MSVC to
1e6cc 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67   work around bug
1e6cd 73 20 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c  s in that compil
1e6ce 65 72 2e 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63  er. */..  static
1e6cf 20 76 6f 69 64 20 67 65 74 43 65 6c 6c 49 6e 66   void getCellInf
1e6d0 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  o(BtCursor *pCur
1e6d1 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 43 75 72  ){..    if( pCur
1e6d2 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20  ->info.nSize==0 
1e6d3 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 50  ){..      int iP
1e6d4 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67  age = pCur->iPag
1e6d5 65 3b 0d 0a 20 20 20 20 20 20 62 74 72 65 65 50  e;..      btreeP
1e6d6 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61  arseCell(pCur->a
1e6d7 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75  pPage[iPage],pCu
1e6d8 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c  r->aiIdx[iPage],
1e6d9 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0d 0a 20  &pCur->info);.. 
1e6da 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64       pCur->valid
1e6db 4e 4b 65 79 20 3d 20 31 3b 0d 0a 20 20 20 20 7d  NKey = 1;..    }
1e6dc 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 73 73  else{..      ass
1e6dd 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  ertCellInfo(pCur
1e6de 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
1e6df 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20  #else /* if not 
1e6e0 5f 4d 53 43 5f 56 45 52 20 2a 2f 0d 0a 20 20 2f  _MSC_VER */..  /
1e6e1 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e  * Use a macro in
1e6e2 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69   all other compi
1e6e3 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65  lers so that the
1e6e4 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c   function is inl
1e6e5 69 6e 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ined */..#define
1e6e6 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
1e6e7 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r)              
1e6e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6ea 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 69 66 28          \..  if(
1e6eb 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
1e6ec 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
1e6ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6ef 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20            \..   
1e6f0 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
1e6f1 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20  r->iPage;       
1e6f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6f4 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20              \.. 
1e6f5 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
1e6f6 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
1e6f7 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64  Page],pCur->aiId
1e6f8 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e  x[iPage],&pCur->
1e6f9 69 6e 66 6f 29 3b 20 5c 0d 0a 20 20 20 20 70 43  info); \..    pC
1e6fa 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
1e6fb 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1e6fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6fe 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 7d 65           \..  }e
1e6ff 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
1e700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e703 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a 20 20             \..  
1e704 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f    assertCellInfo
1e705 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20  (pCur);         
1e706 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e707 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e708 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
1e709 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f    }..#endif /* _
1e70a 4d 53 43 5f 56 45 52 20 2a 2f 0d 0a 0d 0a 23 69  MSC_VER */....#i
1e70b 66 6e 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a  fndef NDEBUG  /*
1e70c 20 54 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e   The next routin
1e70d 65 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68  e used only with
1e70e 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
1e70f 65 6d 65 6e 74 73 20 2a 2f 0d 0a 2f 2a 0d 0a 2a  ements */../*..*
1e710 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
1e711 20 74 68 65 20 67 69 76 65 6e 20 42 74 43 75 72   the given BtCur
1e712 73 6f 72 20 69 73 20 76 61 6c 69 64 2e 20 20 41  sor is valid.  A
1e713 20 76 61 6c 69 64 20 63 75 72 73 6f 72 20 69 73   valid cursor is
1e714 20 6f 6e 65 0d 0a 2a 2a 20 74 68 61 74 20 69 73   one..** that is
1e715 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1e716 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 69 6e 20  ing to a row in 
1e717 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 20 74 61  a (non-empty) ta
1e718 62 6c 65 2e 0d 0a 2a 2a 20 54 68 69 73 20 69 73  ble...** This is
1e719 20 61 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20   a verification 
1e71a 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1e71b 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65  only within asse
1e71c 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
1e71d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
1e71e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e71f 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
1e720 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
1e721 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70 43  r){..  return pC
1e722 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61  ur && pCur->eSta
1e723 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1e724 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
1e725 4e 44 45 42 55 47 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  NDEBUG */..../*.
1e726 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74  .** Set *pSize t
1e727 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  o the size of th
1e728 65 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20  e buffer needed 
1e729 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75  to hold the valu
1e72a 65 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 6b 65 79  e of..** the key
1e72b 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
1e72c 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20   entry.  If the 
1e72d 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f  cursor is not po
1e72e 69 6e 74 69 6e 67 0d 0a 2a 2a 20 74 6f 20 61 20  inting..** to a 
1e72f 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 53  valid entry, *pS
1e730 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e  ize is set to 0.
1e731 20 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 61 20   ..**..** For a 
1e732 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49  table with the I
1e733 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20  NTKEY flag set, 
1e734 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1e735 75 72 6e 73 20 74 68 65 20 6b 65 79 0d 0a 2a 2a  urns the key..**
1e736 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65   itself, not the
1e737 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1e738 20 69 6e 20 74 68 65 20 6b 65 79 2e 0d 0a 2a 2a   in the key...**
1e739 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  ..** The caller 
1e73a 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e 20 74 68  must position th
1e73b 65 20 63 75 72 73 6f 72 20 70 72 69 6f 72 20 74  e cursor prior t
1e73c 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20  o invoking this 
1e73d 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 20 0d 0a 2a  routine...** ..*
1e73e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1e73f 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 20  annot fail.  It 
1e740 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53  always returns S
1e741 51 4c 49 54 45 5f 4f 4b 2e 20 20 0d 0a 2a 2f 0d  QLITE_OK.  ..*/.
1e742 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e743 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1e744 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72  KeySize(BtCursor
1e745 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69   *pCur, i64 *pSi
1e746 7a 65 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  ze){..  assert( 
1e747 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1e748 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 61 73 73  (pCur) );..  ass
1e749 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
1e74a 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
1e74b 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74  D || pCur->eStat
1e74c 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
1e74d 29 3b 0d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e  );..  if( pCur->
1e74e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
1e74f 41 4c 49 44 20 29 7b 0d 0a 20 20 20 20 2a 70 53  ALID ){..    *pS
1e750 69 7a 65 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73  ize = 0;..  }els
1e751 65 7b 0d 0a 20 20 20 20 67 65 74 43 65 6c 6c 49  e{..    getCellI
1e752 6e 66 6f 28 70 43 75 72 29 3b 0d 0a 20 20 20 20  nfo(pCur);..    
1e753 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69  *pSize = pCur->i
1e754 6e 66 6f 2e 6e 4b 65 79 3b 0d 0a 20 20 7d 0d 0a  nfo.nKey;..  }..
1e755 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1e756 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
1e757 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74   Set *pSize to t
1e758 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1e759 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68  es of data in th
1e75a 65 20 65 6e 74 72 79 20 74 68 65 0d 0a 2a 2a 20  e entry the..** 
1e75b 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79  cursor currently
1e75c 20 70 6f 69 6e 74 73 20 74 6f 2e 0d 0a 2a 2a 0d   points to...**.
1e75d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  .** The caller m
1e75e 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68  ust guarantee th
1e75f 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
1e760 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e   pointing to a n
1e761 6f 6e 2d 4e 55 4c 4c 0d 0a 2a 2a 20 76 61 6c 69  on-NULL..** vali
1e762 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68  d entry.  In oth
1e763 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61  er words, the ca
1e764 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
1e765 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 0d 0a  must guarantee..
1e766 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ** that the curs
1e767 6f 72 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53  or has Cursor.eS
1e768 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1e769 49 44 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 61 69 6c  ID...**..** Fail
1e76a 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  ure is not possi
1e76b 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74  ble.  This funct
1e76c 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72  ion always retur
1e76d 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a  ns SQLITE_OK...*
1e76e 2a 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 20  * It might just 
1e76f 61 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 6f  as well be a pro
1e770 63 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 6e  cedure (returnin
1e771 67 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 63  g void) but we c
1e772 6f 6e 74 69 6e 75 65 0d 0a 2a 2a 20 74 6f 20 72  ontinue..** to r
1e773 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
1e774 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72   result code for
1e775 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73   historical reas
1e776 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ons...*/..SQLITE
1e777 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1e778 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a  ite3BtreeDataSiz
1e779 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
1e77a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0d 0a  , u32 *pSize){..
1e77b 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1e77c 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1e77d 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
1e77e 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
1e77f 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20  SOR_VALID );..  
1e780 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
1e781 29 3b 0d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 70  );..  *pSize = p
1e782 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b  Cur->info.nData;
1e783 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1e784 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
1e785 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67  ** Given the pag
1e786 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f  e number of an o
1e787 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20  verflow page in 
1e788 74 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61  the database (pa
1e789 72 61 6d 65 74 65 72 0d 0a 2a 2a 20 6f 76 66 6c  rameter..** ovfl
1e78a 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ), this function
1e78b 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 20   finds the page 
1e78c 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
1e78d 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 0d  xt page in the .
1e78e 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  .** linked list 
1e78f 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  of overflow page
1e790 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20  s. If possible, 
1e791 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f  it uses the auto
1e792 2d 76 61 63 75 75 6d 0d 0a 2a 2a 20 70 6f 69 6e  -vacuum..** poin
1e793 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73  ter-map data ins
1e794 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20  tead of reading 
1e795 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70  the content of p
1e796 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73  age ovfl to do s
1e797 6f 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  o. ..**..** If a
1e798 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
1e799 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
1e79a 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1e79b 20 4f 74 68 65 72 77 69 73 65 3a 0d 0a 2a 2a 0d   Otherwise:..**.
1e79c 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d  .** The page num
1e79d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ber of the next 
1e79e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
1e79f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
1e7a0 20 69 73 20 0d 0a 2a 2a 20 77 72 69 74 74 65 6e   is ..** written
1e7a1 20 74 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20   to *pPgnoNext. 
1e7a2 49 66 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20  If page ovfl is 
1e7a3 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e  the last page in
1e7a4 20 69 74 73 20 6c 69 6e 6b 65 64 20 0d 0a 2a 2a   its linked ..**
1e7a5 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78   list, *pPgnoNex
1e7a6 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
1e7a7 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 70  . ..**..** If pp
1e7a8 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Page is not NULL
1e7a9 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65 6e 63  , and a referenc
1e7aa 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65  e to the MemPage
1e7ab 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73 70 6f   object correspo
1e7ac 6e 64 69 6e 67 0d 0a 2a 2a 20 74 6f 20 70 61 67  nding..** to pag
1e7ad 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20 77  e number pOvfl w
1e7ae 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 65  as obtained, the
1e7af 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  n *ppPage is set
1e7b0 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
1e7b1 74 0d 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e  t..** reference.
1e7b2 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f   It is the respo
1e7b3 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
1e7b4 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20   caller to call 
1e7b5 72 65 6c 65 61 73 65 50 61 67 65 28 29 0d 0a 2a  releasePage()..*
1e7b6 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20  * on *ppPage to 
1e7b7 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e  free the referen
1e7b8 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65  ce. In no refere
1e7b9 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64  nce was obtained
1e7ba 20 28 62 65 63 61 75 73 65 0d 0a 2a 2a 20 74 68   (because..** th
1e7bb 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61  e pointer-map wa
1e7bc 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  s used to obtain
1e7bd 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a   the value for *
1e7be 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e  pPgnoNext), then
1e7bf 0d 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20  ..** *ppPage is 
1e7c0 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0d 0a 2a 2f  set to zero...*/
1e7c1 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  ..static int get
1e7c2 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0d 0a 20  OverflowPage(.. 
1e7c3 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
1e7c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e7c5 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
1e7c6 6c 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 6f 76  le */..  Pgno ov
1e7c7 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fl,             
1e7c8 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1e7c9 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e   overflow page n
1e7ca 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 4d 65 6d 50  umber */..  MemP
1e7cb 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20  age **ppPage,   
1e7cc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1e7cd 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 20   MemPage handle 
1e7ce 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f  (may be NULL) */
1e7cf 0d 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e  ..  Pgno *pPgnoN
1e7d0 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ext             
1e7d1 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76   /* OUT: Next ov
1e7d2 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62  erflow page numb
1e7d3 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 50 67 6e  er */..){..  Pgn
1e7d4 6f 20 6e 65 78 74 20 3d 20 30 3b 0d 0a 20 20 4d  o next = 0;..  M
1e7d5 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
1e7d6 30 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  0;..  int rc = S
1e7d7 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 61  QLITE_OK;....  a
1e7d8 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1e7d9 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1e7da 75 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65  utex) );..  asse
1e7db 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0d 0a  rt(pPgnoNext);..
1e7dc 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e7dd 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1e7de 0d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69  ..  /* Try to fi
1e7df 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  nd the next page
1e7e0 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
1e7e1 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0d   list using the.
1e7e2 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d  .  ** autovacuum
1e7e3 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
1e7e4 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74  es. Guess that t
1e7e5 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
1e7e6 0d 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66  ..  ** the overf
1e7e7 6c 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 65  low list is page
1e7e8 20 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29   number (ovfl+1)
1e7e9 2e 20 49 66 20 74 68 61 74 20 67 75 65 73 73 20  . If that guess 
1e7ea 74 75 72 6e 73 20 0d 0a 20 20 2a 2a 20 6f 75 74  turns ..  ** out
1e7eb 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61   to be wrong, fa
1e7ec 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69  ll back to loadi
1e7ed 6e 67 20 74 68 65 20 64 61 74 61 20 6f 66 20 70  ng the data of p
1e7ee 61 67 65 20 0d 0a 20 20 2a 2a 20 6e 75 6d 62 65  age ..  ** numbe
1e7ef 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 72 6d  r ovfl to determ
1e7f0 69 6e 65 20 74 68 65 20 6e 65 78 74 20 70 61 67  ine the next pag
1e7f1 65 20 6e 75 6d 62 65 72 2e 0d 0a 20 20 2a 2f 0d  e number...  */.
1e7f2 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f  .  if( pBt->auto
1e7f3 56 61 63 75 75 6d 20 29 7b 0d 0a 20 20 20 20 50  Vacuum ){..    P
1e7f4 67 6e 6f 20 70 67 6e 6f 3b 0d 0a 20 20 20 20 50  gno pgno;..    P
1e7f5 67 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66  gno iGuess = ovf
1e7f6 6c 2b 31 3b 0d 0a 20 20 20 20 75 38 20 65 54 79  l+1;..    u8 eTy
1e7f7 70 65 3b 0d 0a 0d 0a 20 20 20 20 77 68 69 6c 65  pe;....    while
1e7f8 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  ( PTRMAP_ISPAGE(
1e7f9 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c 20  pBt, iGuess) || 
1e7fa 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47 5f  iGuess==PENDING_
1e7fb 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1e7fc 7b 0d 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b  {..      iGuess+
1e7fd 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  +;..    }....   
1e7fe 20 69 66 28 20 69 47 75 65 73 73 3c 3d 62 74 72   if( iGuess<=btr
1e7ff 65 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  eePagecount(pBt)
1e800 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   ){..      rc = 
1e801 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69  ptrmapGet(pBt, i
1e802 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26  Guess, &eType, &
1e803 70 67 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 69 66  pgno);..      if
1e804 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e805 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  && eType==PTRMAP
1e806 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67  _OVERFLOW2 && pg
1e807 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0d 0a 20 20 20  no==ovfl ){..   
1e808 20 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65       next = iGue
1e809 73 73 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  ss;..        rc 
1e80a 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a  = SQLITE_DONE;..
1e80b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
1e80c 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20    }..#endif.... 
1e80d 20 61 73 73 65 72 74 28 20 6e 65 78 74 3d 3d 30   assert( next==0
1e80e 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44   || rc==SQLITE_D
1e80f 4f 4e 45 20 29 3b 0d 0a 20 20 69 66 28 20 72 63  ONE );..  if( rc
1e810 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1e811 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
1e812 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c  tPage(pBt, ovfl,
1e813 20 26 70 50 61 67 65 2c 20 30 29 3b 0d 0a 20 20   &pPage, 0);..  
1e814 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1e815 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
1e816 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20  ==0 );..    if( 
1e817 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1e818 0d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67  ..      next = g
1e819 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61  et4byte(pPage->a
1e81a 44 61 74 61 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Data);..    }.. 
1e81b 20 7d 0d 0a 0d 0a 20 20 2a 70 50 67 6e 6f 4e 65   }....  *pPgnoNe
1e81c 78 74 20 3d 20 6e 65 78 74 3b 0d 0a 20 20 69 66  xt = next;..  if
1e81d 28 20 70 70 50 61 67 65 20 29 7b 0d 0a 20 20 20  ( ppPage ){..   
1e81e 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65   *ppPage = pPage
1e81f 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
1e820 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
1e821 67 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ge);..  }..  ret
1e822 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
1e823 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
1e824 20 3a 20 72 63 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   : rc);..}..../*
1e825 0d 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20 66  ..** Copy data f
1e826 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f 20  rom a buffer to 
1e827 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20  a page, or from 
1e828 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66 66  a page to a buff
1e829 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70 50 61 79  er...**..** pPay
1e82a 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65  load is a pointe
1e82b 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64  r to data stored
1e82c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67   on database pag
1e82d 65 20 70 44 62 50 61 67 65 2e 0d 0a 2a 2a 20 49  e pDbPage...** I
1e82e 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69  f argument eOp i
1e82f 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42  s false, then nB
1e830 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  yte bytes of dat
1e831 61 20 61 72 65 20 63 6f 70 69 65 64 0d 0a 2a 2a  a are copied..**
1e832 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74   from pPayload t
1e833 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69  o the buffer poi
1e834 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e  nted at by pBuf.
1e835 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c   If eOp is true,
1e836 0d 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65  ..** then sqlite
1e837 33 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73  3PagerWrite() is
1e838 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61   called on pDbPa
1e839 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74  ge and nByte byt
1e83a 65 73 0d 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61  es..** of data a
1e83b 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74  re copied from t
1e83c 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20 74  he buffer pBuf t
1e83d 6f 20 70 50 61 79 6c 6f 61 64 2e 0d 0a 2a 2a 0d  o pPayload...**.
1e83e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
1e83f 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63   returned on suc
1e840 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20  cess, otherwise 
1e841 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a  an error code...
1e842 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */..static int c
1e843 6f 70 79 50 61 79 6c 6f 61 64 28 0d 0a 20 20 76  opyPayload(..  v
1e844 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20  oid *pPayload,  
1e845 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1e846 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61  ter to page data
1e847 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 42 75   */..  void *pBu
1e848 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
1e849 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62   /* Pointer to b
1e84a 75 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  uffer */..  int 
1e84b 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
1e84c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1e84d 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79  of bytes to copy
1e84e 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 4f 70 2c 20   */..  int eOp, 
1e84f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e850 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72   /* 0 -> copy fr
1e851 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f  om page, 1 -> co
1e852 70 79 20 74 6f 20 70 61 67 65 20 2a 2f 0d 0a 20  py to page */.. 
1e853 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
1e854 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1e855 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50  ge containing pP
1e856 61 79 6c 6f 61 64 20 2a 2f 0d 0a 29 7b 0d 0a 20  ayload */..){.. 
1e857 20 69 66 28 20 65 4f 70 20 29 7b 0d 0a 20 20 20   if( eOp ){..   
1e858 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72   /* Copy data fr
1e859 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67  om buffer to pag
1e85a 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61  e (a write opera
1e85b 74 69 6f 6e 29 20 2a 2f 0d 0a 20 20 20 20 69 6e  tion) */..    in
1e85c 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
1e85d 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
1e85e 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d  );..    if( rc!=
1e85f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1e860 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
1e861 20 20 20 20 7d 0d 0a 20 20 20 20 6d 65 6d 63 70      }..    memcp
1e862 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66  y(pPayload, pBuf
1e863 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 7d 65 6c  , nByte);..  }el
1e864 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 43 6f 70 79  se{..    /* Copy
1e865 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 20   data from page 
1e866 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 61  to buffer (a rea
1e867 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0d  d operation) */.
1e868 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66  .    memcpy(pBuf
1e869 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74  , pPayload, nByt
1e86a 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  e);..  }..  retu
1e86b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
1e86c 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
1e86d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
1e86e 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65 72   to read or over
1e86f 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69 6e  write payload in
1e870 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 66 6f  formation..** fo
1e871 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
1e872 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72   the pCur cursor
1e873 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e   is pointing to.
1e874 20 49 66 20 74 68 65 20 65 4f 70 0d 0a 2a 2a 20   If the eOp..** 
1e875 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20  parameter is 0, 
1e876 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f  this is a read o
1e877 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63  peration (data c
1e878 6f 70 69 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 62  opied into..** b
1e879 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20  uffer pBuf). If 
1e87a 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  it is non-zero, 
1e87b 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f  a write (data co
1e87c 70 69 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 62 75  pied from..** bu
1e87d 66 66 65 72 20 70 42 75 66 29 2e 0d 0a 2a 2a 0d  ffer pBuf)...**.
1e87e 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 22  .** A total of "
1e87f 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 20 72  amt" bytes are r
1e880 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 62  ead or written b
1e881 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66  eginning at "off
1e882 73 65 74 22 2e 0d 0a 2a 2a 20 44 61 74 61 20 69  set"...** Data i
1e883 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 6f  s read to or fro
1e884 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75  m the buffer pBu
1e885 66 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63  f...**..** The c
1e886 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 72 65 61  ontent being rea
1e887 64 20 6f 72 20 77 72 69 74 74 65 6e 20 6d 69 67  d or written mig
1e888 68 74 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65  ht appear on the
1e889 20 6d 61 69 6e 20 70 61 67 65 0d 0a 2a 2a 20 6f   main page..** o
1e88a 72 20 62 65 20 73 63 61 74 74 65 72 65 64 20 6f  r be scattered o
1e88b 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f  ut on multiple o
1e88c 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0d 0a  verflow pages...
1e88d 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 42 74  **..** If the Bt
1e88e 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f  Cursor.isIncrblo
1e88f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20  bHandle flag is 
1e890 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72  set, and the cur
1e891 72 65 6e 74 0d 0a 2a 2a 20 63 75 72 73 6f 72 20  rent..** cursor 
1e892 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f  entry uses one o
1e893 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20  r more overflow 
1e894 70 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63  pages, this func
1e895 74 69 6f 6e 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74  tion..** allocat
1e896 65 73 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64  es space for and
1e897 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65   lazily popluate
1e898 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  s the overflow p
1e899 61 67 65 2d 6c 69 73 74 20 0d 0a 2a 2a 20 63 61  age-list ..** ca
1e89a 63 68 65 20 61 72 72 61 79 20 28 42 74 43 75 72  che array (BtCur
1e89b 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20  sor.aOverflow). 
1e89c 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  Subsequent calls
1e89d 20 75 73 65 20 74 68 69 73 0d 0a 2a 2a 20 63 61   use this..** ca
1e89e 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b  che to make seek
1e89f 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c  ing to the suppl
1e8a0 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65 20  ied offset more 
1e8a1 65 66 66 69 63 69 65 6e 74 2e 0d 0a 2a 2a 0d 0a  efficient...**..
1e8a2 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  ** Once an overf
1e8a3 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
1e8a4 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c  che has been all
1e8a5 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62  ocated, it may b
1e8a6 65 0d 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  e..** invalidate
1e8a7 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  d if some other 
1e8a8 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f  cursor writes to
1e8a9 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c   the same table,
1e8aa 20 6f 72 20 69 66 0d 0a 2a 2a 20 74 68 65 20 63   or if..** the c
1e8ab 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74  ursor is moved t
1e8ac 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f  o a different ro
1e8ad 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  w. Additionally,
1e8ae 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0d   in auto-vacuum.
1e8af 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f  .** mode, the fo
1e8b0 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d  llowing events m
1e8b1 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  ay invalidate an
1e8b2 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
1e8b3 69 73 74 20 63 61 63 68 65 2e 0d 0a 2a 2a 0d 0a  ist cache...**..
1e8b4 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d  **   * An increm
1e8b5 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0d 0a 2a  ental vacuum,..*
1e8b6 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69  *   * A commit i
1e8b7 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66  n auto_vacuum="f
1e8b8 75 6c 6c 22 20 6d 6f 64 65 2c 0d 0a 2a 2a 20 20  ull" mode,..**  
1e8b9 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74 61   * Creating a ta
1e8ba 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72 65  ble (may require
1e8bb 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 66   moving an overf
1e8bc 6c 6f 77 20 70 61 67 65 29 2e 0d 0a 2a 2f 0d 0a  low page)...*/..
1e8bd 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73  static int acces
1e8be 73 50 61 79 6c 6f 61 64 28 0d 0a 20 20 42 74 43  sPayload(..  BtC
1e8bf 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20  ursor *pCur,    
1e8c0 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
1e8c1 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f  ting to entry to
1e8c2 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20   read from */.. 
1e8c3 20 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20   u32 offset,    
1e8c4 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72        /* Begin r
1e8c5 65 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20  eading this far 
1e8c6 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0d  into payload */.
1e8c7 0a 20 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20  .  u32 amt,     
1e8c8 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20          /* Read 
1e8c9 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20  this many bytes 
1e8ca 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  */..  unsigned c
1e8cb 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72  har *pBuf, /* Wr
1e8cc 69 74 65 20 74 68 65 20 62 79 74 65 73 20 69 6e  ite the bytes in
1e8cd 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a  to this buffer *
1e8ce 2f 20 0d 0a 20 20 69 6e 74 20 65 4f 70 20 20 20  / ..  int eOp   
1e8cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65             /* ze
1e8d0 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d  ro to read. non-
1e8d1 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a  zero to write. *
1e8d2 2f 0d 0a 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65  /..){..  unsigne
1e8d3 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64  d char *aPayload
1e8d4 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
1e8d5 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 75 33 32 20  LITE_OK;..  u32 
1e8d6 6e 4b 65 79 3b 0d 0a 20 20 69 6e 74 20 69 49 64  nKey;..  int iId
1e8d7 78 20 3d 20 30 3b 0d 0a 20 20 4d 65 6d 50 61 67  x = 0;..  MemPag
1e8d8 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
1e8d9 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1e8da 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70  age]; /* Btree p
1e8db 61 67 65 20 6f 66 20 63 75 72 72 65 6e 74 20 65  age of current e
1e8dc 6e 74 72 79 20 2a 2f 0d 0a 20 20 42 74 53 68 61  ntry */..  BtSha
1e8dd 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d  red *pBt = pCur-
1e8de 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  >pBt;           
1e8df 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
1e8e0 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f  this cursor belo
1e8e1 6e 67 73 20 74 6f 20 2a 2f 0d 0a 0d 0a 20 20 61  ngs to */....  a
1e8e2 73 73 65 72 74 28 20 70 50 61 67 65 20 29 3b 0d  ssert( pPage );.
1e8e3 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1e8e4 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1e8e5 56 41 4c 49 44 20 29 3b 0d 0a 20 20 61 73 73 65  VALID );..  asse
1e8e6 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
1e8e7 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61  pCur->iPage]<pPa
1e8e8 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 20 20  ge->nCell );..  
1e8e9 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1e8ea 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1e8eb 3b 0d 0a 0d 0a 20 20 67 65 74 43 65 6c 6c 49 6e  ;....  getCellIn
1e8ec 66 6f 28 70 43 75 72 29 3b 0d 0a 20 20 61 50 61  fo(pCur);..  aPa
1e8ed 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e  yload = pCur->in
1e8ee 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d  fo.pCell + pCur-
1e8ef 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0d 0a  >info.nHeader;..
1e8f0 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d    nKey = (pPage-
1e8f1 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 69  >intKey ? 0 : (i
1e8f2 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
1e8f3 65 79 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 4e 45  ey);....  if( NE
1e8f4 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e  VER(offset+amt >
1e8f5 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f   nKey+pCur->info
1e8f6 2e 6e 44 61 74 61 29 20 0d 0a 20 20 20 7c 7c 20  .nData) ..   || 
1e8f7 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e  &aPayload[pCur->
1e8f8 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26  info.nLocal] > &
1e8f9 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
1e8fa 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0d 0a 20  ->usableSize].. 
1e8fb 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 72 79 69   ){..    /* Tryi
1e8fc 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72  ng to read or wr
1e8fd 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  ite past the end
1e8fe 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20   of the data is 
1e8ff 61 6e 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 20  an error */..   
1e900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1e901 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20  ORRUPT_BKPT;..  
1e902 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  }....  /* Check 
1e903 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 65 20  if data must be 
1e904 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 6f 2f  read/written to/
1e905 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 70  from the btree p
1e906 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0d 0a  age itself. */..
1e907 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 75    if( offset<pCu
1e908 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29  r->info.nLocal )
1e909 7b 0d 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61  {..    int a = a
1e90a 6d 74 3b 0d 0a 20 20 20 20 69 66 28 20 61 2b 6f  mt;..    if( a+o
1e90b 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f  ffset>pCur->info
1e90c 2e 6e 4c 6f 63 61 6c 20 29 7b 0d 0a 20 20 20 20  .nLocal ){..    
1e90d 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f    a = pCur->info
1e90e 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74  .nLocal - offset
1e90f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 63  ;..    }..    rc
1e910 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26   = copyPayload(&
1e911 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d  aPayload[offset]
1e912 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20  , pBuf, a, eOp, 
1e913 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
1e914 0d 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30  ..    offset = 0
1e915 3b 0d 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61  ;..    pBuf += a
1e916 3b 0d 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b  ;..    amt -= a;
1e917 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
1e918 6f 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e  offset -= pCur->
1e919 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0d 0a 20 20  info.nLocal;..  
1e91a 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  }....  if( rc==S
1e91b 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e  QLITE_OK && amt>
1e91c 30 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  0 ){..    const 
1e91d 75 33 32 20 6f 76 66 6c 53 69 7a 65 20 3d 20 70  u32 ovflSize = p
1e91e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
1e91f 20 34 3b 20 20 2f 2a 20 42 79 74 65 73 20 63 6f   4;  /* Bytes co
1e920 6e 74 65 6e 74 20 70 65 72 20 6f 76 66 6c 20 70  ntent per ovfl p
1e921 61 67 65 20 2a 2f 0d 0a 20 20 20 20 50 67 6e 6f  age */..    Pgno
1e922 20 6e 65 78 74 50 61 67 65 3b 0d 0a 0d 0a 20 20   nextPage;....  
1e923 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74    nextPage = get
1e924 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b  4byte(&aPayload[
1e925 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
1e926 6c 5d 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  l]);....#ifndef 
1e927 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
1e928 42 4c 4f 42 0d 0a 20 20 20 20 2f 2a 20 49 66 20  BLOB..    /* If 
1e929 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61  the isIncrblobHa
1e92a 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74  ndle flag is set
1e92b 20 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f   and the BtCurso
1e92c 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0d 0a 20  r.aOverflow[].. 
1e92d 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65     ** has not be
1e92e 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c  en allocated, al
1e92f 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54  locate it now. T
1e930 68 65 20 61 72 72 61 79 20 69 73 20 73 69 7a 65  he array is size
1e931 64 20 61 74 0d 0a 20 20 20 20 2a 2a 20 6f 6e 65  d at..    ** one
1e932 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20   entry for each 
1e933 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
1e934 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68   the overflow ch
1e935 61 69 6e 2e 20 54 68 65 0d 0a 20 20 20 20 2a 2a  ain. The..    **
1e936 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
1e937 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c  the first overfl
1e938 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 65  ow page is store
1e939 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30  d in aOverflow[0
1e93a 5d 2c 0d 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20  ],..    ** etc. 
1e93b 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20  A value of 0 in 
1e93c 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20  the aOverflow[] 
1e93d 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74  array means "not
1e93e 20 79 65 74 20 6b 6e 6f 77 6e 22 0d 0a 20 20 20   yet known"..   
1e93f 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69   ** (the cache i
1e940 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74  s lazily populat
1e941 65 64 29 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ed)...    */..  
1e942 20 20 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e    if( pCur->isIn
1e943 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20  crblobHandle && 
1e944 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77  !pCur->aOverflow
1e945 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   ){..      int n
1e946 4f 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e  Ovfl = (pCur->in
1e947 66 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72  fo.nPayload-pCur
1e948 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76  ->info.nLocal+ov
1e949 66 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69  flSize-1)/ovflSi
1e94a 7a 65 3b 0d 0a 20 20 20 20 20 20 70 43 75 72 2d  ze;..      pCur-
1e94b 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 28 50 67  >aOverflow = (Pg
1e94c 6e 6f 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  no *)sqlite3Mall
1e94d 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 50 67  ocZero(sizeof(Pg
1e94e 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0d 0a 20 20 20  no)*nOvfl);..   
1e94f 20 20 20 2f 2a 20 6e 4f 76 66 6c 20 69 73 20 61     /* nOvfl is a
1e950 6c 77 61 79 73 20 70 6f 73 69 74 69 76 65 2e 20  lways positive. 
1e951 20 49 66 20 69 74 20 77 65 72 65 20 7a 65 72 6f   If it were zero
1e952 2c 20 66 65 74 63 68 50 61 79 6c 6f 61 64 20 77  , fetchPayload w
1e953 6f 75 6c 64 20 68 61 76 65 0d 0a 20 20 20 20 20  ould have..     
1e954 20 2a 2a 20 62 65 65 6e 20 75 73 65 64 20 69 6e   ** been used in
1e955 73 74 65 61 64 20 6f 66 20 74 68 69 73 20 72 6f  stead of this ro
1e956 75 74 69 6e 65 2e 20 2a 2f 0d 0a 20 20 20 20 20  utine. */..     
1e957 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 76 66   if( ALWAYS(nOvf
1e958 6c 29 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76  l) && !pCur->aOv
1e959 65 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20 20 20  erflow ){..     
1e95a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
1e95b 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  OMEM;..      }..
1e95c 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
1e95d 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  If the overflow 
1e95e 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20  page-list cache 
1e95f 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  has been allocat
1e960 65 64 20 61 6e 64 20 74 68 65 0d 0a 20 20 20 20  ed and the..    
1e961 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ** entry for the
1e962 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64 20   first required 
1e963 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73  overflow page is
1e964 20 76 61 6c 69 64 2c 20 73 6b 69 70 0d 0a 20 20   valid, skip..  
1e965 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f    ** directly to
1e966 20 69 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20   it...    */..  
1e967 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65    if( pCur->aOve
1e968 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61  rflow && pCur->a
1e969 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f  Overflow[offset/
1e96a 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0d 0a 20 20  ovflSize] ){..  
1e96b 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73      iIdx = (offs
1e96c 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0d 0a 20  et/ovflSize);.. 
1e96d 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20       nextPage = 
1e96e 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
1e96f 69 49 64 78 5d 3b 0d 0a 20 20 20 20 20 20 6f 66  iIdx];..      of
1e970 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f  fset = (offset%o
1e971 76 66 6c 53 69 7a 65 29 3b 0d 0a 20 20 20 20 7d  vflSize);..    }
1e972 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20  ..#endif....    
1e973 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54  for( ; rc==SQLIT
1e974 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26  E_OK && amt>0 &&
1e975 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b   nextPage; iIdx+
1e976 2b 29 7b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  +){....#ifndef S
1e977 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1e978 4c 4f 42 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66  LOB..      /* If
1e979 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c   required, popul
1e97a 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  ate the overflow
1e97b 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
1e97c 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20  . */..      if( 
1e97d 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
1e97e 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
1e97f 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 66  rt(!pCur->aOverf
1e980 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75  low[iIdx] || pCu
1e981 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64  r->aOverflow[iId
1e982 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0d 0a  x]==nextPage);..
1e983 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f          pCur->aO
1e984 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20  verflow[iIdx] = 
1e985 6e 65 78 74 50 61 67 65 3b 0d 0a 20 20 20 20 20  nextPage;..     
1e986 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
1e987 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d      if( offset>=
1e988 6f 76 66 6c 53 69 7a 65 20 29 7b 0d 0a 20 20 20  ovflSize ){..   
1e989 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79       /* The only
1e98a 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20   reason to read 
1e98b 74 68 69 73 20 70 61 67 65 20 69 73 20 74 6f 20  this page is to 
1e98c 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 0d  obtain the page.
1e98d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  .        ** numb
1e98e 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  er for the next 
1e98f 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72  page in the over
1e990 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20  flow chain. The 
1e991 70 61 67 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a  page..        **
1e992 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71   data is not req
1e993 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20  uired. So first 
1e994 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68  try to lookup th
1e995 65 20 6f 76 65 72 66 6c 6f 77 0d 0a 20 20 20 20  e overflow..    
1e996 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74      ** page-list
1e997 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20   cache, if any, 
1e998 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74  then fall back t
1e999 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f  o the getOverflo
1e99a 77 50 61 67 65 28 29 0d 0a 20 20 20 20 20 20 20  wPage()..       
1e99b 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 20   ** function... 
1e99c 20 20 20 20 20 20 20 2a 2f 0d 0a 23 69 66 6e 64         */..#ifnd
1e99d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1e99e 4e 43 52 42 4c 4f 42 0d 0a 20 20 20 20 20 20 20  NCRBLOB..       
1e99f 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72   if( pCur->aOver
1e9a0 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f  flow && pCur->aO
1e9a1 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20  verflow[iIdx+1] 
1e9a2 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 65  ){..          ne
1e9a3 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  xtPage = pCur->a
1e9a4 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d  Overflow[iIdx+1]
1e9a5 3b 0d 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  ;..        } els
1e9a6 65 20 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  e ..#endif..    
1e9a7 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76        rc = getOv
1e9a8 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20  erflowPage(pBt, 
1e9a9 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65  nextPage, 0, &ne
1e9aa 78 74 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20  xtPage);..      
1e9ab 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c    offset -= ovfl
1e9ac 53 69 7a 65 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  Size;..      }el
1e9ad 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20  se{..        /* 
1e9ae 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69  Need to read thi
1e9af 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e  s page properly.
1e9b0 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d   It contains som
1e9b1 65 20 6f 66 20 74 68 65 0d 0a 20 20 20 20 20 20  e of the..      
1e9b2 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61    ** range of da
1e9b3 74 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67  ta that is being
1e9b4 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f   read (eOp==0) o
1e9b5 72 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d  r written (eOp!=
1e9b6 30 29 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d  0)...        */.
1e9b7 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1e9b8 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f 52  IRECT_OVERFLOW_R
1e9b9 45 41 44 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  EAD..        sql
1e9ba 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0d 0a  ite3_file *fd;..
1e9bb 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 20 20  #endif..        
1e9bc 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0d 0a 20 20  int a = amt;..  
1e9bd 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66        if( a + of
1e9be 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20  fset > ovflSize 
1e9bf 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 20  ){..          a 
1e9c0 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66  = ovflSize - off
1e9c1 73 65 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  set;..        }.
1e9c2 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
1e9c3 5f 44 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57  _DIRECT_OVERFLOW
1e9c4 5f 52 45 41 44 0d 0a 20 20 20 20 20 20 20 20 2f  _READ..        /
1e9c5 2a 20 49 66 20 61 6c 6c 20 74 68 65 20 66 6f 6c  * If all the fol
1e9c6 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a  lowing are true:
1e9c7 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ..        **..  
1e9c8 20 20 20 20 20 20 2a 2a 20 20 20 31 29 20 74 68        **   1) th
1e9c9 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 65  is is a read ope
1e9ca 72 61 74 69 6f 6e 2c 20 61 6e 64 20 0d 0a 20 20  ration, and ..  
1e9cb 20 20 20 20 20 20 2a 2a 20 20 20 32 29 20 64 61        **   2) da
1e9cc 74 61 20 69 73 20 72 65 71 75 69 72 65 64 20 66  ta is required f
1e9cd 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66  rom the start of
1e9ce 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70   this overflow p
1e9cf 61 67 65 2c 20 61 6e 64 0d 0a 20 20 20 20 20 20  age, and..      
1e9d0 20 20 2a 2a 20 20 20 33 29 20 74 68 65 20 64 61    **   3) the da
1e9d1 74 61 62 61 73 65 20 69 73 20 66 69 6c 65 2d 62  tabase is file-b
1e9d2 61 63 6b 65 64 2c 20 61 6e 64 0d 0a 20 20 20 20  acked, and..    
1e9d3 20 20 20 20 2a 2a 20 20 20 34 29 20 74 68 65 72      **   4) ther
1e9d4 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 77 72 69  e is no open wri
1e9d5 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  te-transaction, 
1e9d6 61 6e 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  and..        ** 
1e9d7 20 20 35 29 20 74 68 65 20 64 61 74 61 62 61 73    5) the databas
1e9d8 65 20 69 73 20 6e 6f 74 20 61 20 57 41 4c 20 64  e is not a WAL d
1e9d9 61 74 61 62 61 73 65 2c 0d 0a 20 20 20 20 20 20  atabase,..      
1e9da 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a    **..        **
1e9db 20 74 68 65 6e 20 64 61 74 61 20 63 61 6e 20 62   then data can b
1e9dc 65 20 72 65 61 64 20 64 69 72 65 63 74 6c 79 20  e read directly 
1e9dd 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1e9de 65 20 66 69 6c 65 20 69 6e 74 6f 20 74 68 65 0d  e file into the.
1e9df 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 70  .        ** outp
1e9e0 75 74 20 62 75 66 66 65 72 2c 20 62 79 70 61 73  ut buffer, bypas
1e9e1 73 69 6e 67 20 74 68 65 20 70 61 67 65 2d 63 61  sing the page-ca
1e9e2 63 68 65 20 61 6c 74 6f 67 65 74 68 65 72 2e 20  che altogether. 
1e9e3 54 68 69 73 20 73 70 65 65 64 73 0d 0a 20 20 20  This speeds..   
1e9e4 20 20 20 20 20 2a 2a 20 75 70 20 6c 6f 61 64 69       ** up loadi
1e9e5 6e 67 20 6c 61 72 67 65 20 72 65 63 6f 72 64 73  ng large records
1e9e6 20 74 68 61 74 20 73 70 61 6e 20 6d 61 6e 79 20   that span many 
1e9e7 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0d  overflow pages..
1e9e8 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  .        */..   
1e9e9 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 30 20       if( eOp==0 
1e9ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9ec 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 28              /* (
1e9ed 31 29 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  1) */..         
1e9ee 26 26 20 6f 66 66 73 65 74 3d 3d 30 20 20 20 20  && offset==0    
1e9ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9f1 20 20 20 20 20 20 2f 2a 20 28 32 29 20 2a 2f 0d        /* (2) */.
1e9f2 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42 74  .         && pBt
1e9f3 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1e9f4 3d 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 20  =TRANS_READ     
1e9f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9f6 2f 2a 20 28 34 29 20 2a 2f 0d 0a 20 20 20 20 20  /* (4) */..     
1e9f7 20 20 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c      && (fd = sql
1e9f8 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 42  ite3PagerFile(pB
1e9f9 74 2d 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65  t->pPager))->pMe
1e9fa 74 68 6f 64 73 20 20 20 20 20 2f 2a 20 28 33 29  thods     /* (3)
1e9fb 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 26 26   */..         &&
1e9fc 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
1e9fd 61 74 61 5b 31 39 5d 3d 3d 30 78 30 31 20 20 20  ata[19]==0x01   
1e9fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9ff 20 20 20 20 2f 2a 20 28 35 29 20 2a 2f 0d 0a 20      /* (5) */.. 
1ea00 20 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20         ){..     
1ea01 20 20 20 20 20 75 38 20 61 53 61 76 65 5b 34 5d       u8 aSave[4]
1ea02 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 75 38 20  ;..          u8 
1ea03 2a 61 57 72 69 74 65 20 3d 20 26 70 42 75 66 5b  *aWrite = &pBuf[
1ea04 2d 34 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  -4];..          
1ea05 6d 65 6d 63 70 79 28 61 53 61 76 65 2c 20 61 57  memcpy(aSave, aW
1ea06 72 69 74 65 2c 20 34 29 3b 0d 0a 20 20 20 20 20  rite, 4);..     
1ea07 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1ea08 33 4f 73 52 65 61 64 28 66 64 2c 20 61 57 72 69  3OsRead(fd, aWri
1ea09 74 65 2c 20 61 2b 34 2c 20 28 69 36 34 29 70 42  te, a+4, (i64)pB
1ea0a 74 2d 3e 70 61 67 65 53 69 7a 65 2a 28 6e 65 78  t->pageSize*(nex
1ea0b 74 50 61 67 65 2d 31 29 29 3b 0d 0a 20 20 20 20  tPage-1));..    
1ea0c 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d        nextPage =
1ea0d 20 67 65 74 34 62 79 74 65 28 61 57 72 69 74 65   get4byte(aWrite
1ea0e 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 6d 65  );..          me
1ea0f 6d 63 70 79 28 61 57 72 69 74 65 2c 20 61 53 61  mcpy(aWrite, aSa
1ea10 76 65 2c 20 34 29 3b 0d 0a 20 20 20 20 20 20 20  ve, 4);..       
1ea11 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a   }else..#endif..
1ea12 0d 0a 20 20 20 20 20 20 20 20 7b 0d 0a 20 20 20  ..        {..   
1ea13 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70         DbPage *p
1ea14 44 62 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 20  DbPage;..       
1ea15 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1ea16 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
1ea17 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26  ger, nextPage, &
1ea18 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  pDbPage);..     
1ea19 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1ea1a 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
1ea1b 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20         aPayload 
1ea1c 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1ea1d 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0d  tData(pDbPage);.
1ea1e 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78  .            nex
1ea1f 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  tPage = get4byte
1ea20 28 61 50 61 79 6c 6f 61 64 29 3b 0d 0a 20 20 20  (aPayload);..   
1ea21 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f           rc = co
1ea22 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c  pyPayload(&aPayl
1ea23 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70  oad[offset+4], p
1ea24 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62  Buf, a, eOp, pDb
1ea25 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Page);..        
1ea26 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
1ea27 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0d  Unref(pDbPage);.
1ea28 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 66  .            off
1ea29 73 65 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  set = 0;..      
1ea2a 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
1ea2b 0d 0a 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d  ..        amt -=
1ea2c 20 61 3b 0d 0a 20 20 20 20 20 20 20 20 70 42 75   a;..        pBu
1ea2d 66 20 2b 3d 20 61 3b 0d 0a 20 20 20 20 20 20 7d  f += a;..      }
1ea2e 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
1ea2f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ea30 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0d  _OK && amt>0 ){.
1ea31 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1ea32 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1ea33 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
1ea34 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
1ea35 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68   Read part of th
1ea36 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 64  e key associated
1ea37 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75   with cursor pCu
1ea38 72 2e 20 20 45 78 61 63 74 6c 79 0d 0a 2a 2a 20  r.  Exactly..** 
1ea39 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c  "amt" bytes will
1ea3a 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69   be transfered i
1ea3b 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65  nto pBuf[].  The
1ea3c 20 74 72 61 6e 73 66 65 72 0d 0a 2a 2a 20 62 65   transfer..** be
1ea3d 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22  gins at "offset"
1ea3e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 61  ...**..** The ca
1ea3f 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65  ller must ensure
1ea40 20 74 68 61 74 20 70 43 75 72 20 69 73 20 70 6f   that pCur is po
1ea41 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69  inting to a vali
1ea42 64 20 72 6f 77 0d 0a 2a 2a 20 69 6e 20 74 68 65  d row..** in the
1ea43 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   table...**..** 
1ea44 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1ea45 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61   on success or a
1ea46 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
1ea47 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0d 0a 2a  anything goes..*
1ea48 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72  * wrong.  An err
1ea49 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  or is returned i
1ea4a 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69  f "offset+amt" i
1ea4b 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0d 0a 2a  s larger than..*
1ea4c 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  * the available 
1ea4d 70 61 79 6c 6f 61 64 2e 0d 0a 2a 2f 0d 0a 53 51  payload...*/..SQ
1ea4e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1ea4f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
1ea50 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1ea51 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
1ea52 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66   amt, void *pBuf
1ea53 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 63 75  ){..  assert( cu
1ea54 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1ea55 43 75 72 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  Cur) );..  asser
1ea56 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
1ea57 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
1ea58 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ..  assert( pCur
1ea59 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43  ->iPage>=0 && pC
1ea5a 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1ea5b 3e 69 50 61 67 65 5d 20 29 3b 0d 0a 20 20 61 73  >iPage] );..  as
1ea5c 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
1ea5d 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
1ea5e 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1ea5f 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
1ea60 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 61 63 63  );..  return acc
1ea61 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c  essPayload(pCur,
1ea62 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75   offset, amt, (u
1ea63 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42  nsigned char*)pB
1ea64 75 66 2c 20 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  uf, 0);..}..../*
1ea65 0d 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f  ..** Read part o
1ea66 66 20 74 68 65 20 64 61 74 61 20 61 73 73 6f 63  f the data assoc
1ea67 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f  iated with curso
1ea68 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79  r pCur.  Exactly
1ea69 0d 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73  ..** "amt" bytes
1ea6a 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65   will be transfe
1ea6b 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e  red into pBuf[].
1ea6c 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0d 0a    The transfer..
1ea6d 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66  ** begins at "of
1ea6e 66 73 65 74 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52  fset"...**..** R
1ea6f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
1ea70 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e  on success or an
1ea71 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61   error code if a
1ea72 6e 79 74 68 69 6e 67 20 67 6f 65 73 0d 0a 2a 2a  nything goes..**
1ea73 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f   wrong.  An erro
1ea74 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  r is returned if
1ea75 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73   "offset+amt" is
1ea76 20 6c 61 72 67 65 72 20 74 68 61 6e 0d 0a 2a 2a   larger than..**
1ea77 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70   the available p
1ea78 61 79 6c 6f 61 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ayload...*/..SQL
1ea79 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1ea7a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1ea7b 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1ea7c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
1ea7d 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66   amt, void *pBuf
1ea7e 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 0d  ){..  int rc;...
1ea7f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ea80 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0d 0a 20  OMIT_INCRBLOB.. 
1ea81 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61   if ( pCur->eSta
1ea82 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
1ea83 49 44 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ID ){..    retur
1ea84 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0d  n SQLITE_ABORT;.
1ea85 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
1ea86 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1ea87 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1ea88 20 29 3b 0d 0a 20 20 72 63 20 3d 20 72 65 73 74   );..  rc = rest
1ea89 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
1ea8a 6e 28 70 43 75 72 29 3b 0d 0a 20 20 69 66 28 20  n(pCur);..  if( 
1ea8b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1ea8c 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ..    assert( pC
1ea8d 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1ea8e 4f 52 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20 20  OR_VALID );..   
1ea8f 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
1ea90 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d  Page>=0 && pCur-
1ea91 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1ea92 61 67 65 5d 20 29 3b 0d 0a 20 20 20 20 61 73 73  age] );..    ass
1ea93 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1ea94 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
1ea95 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1ea96 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
1ea97 3b 0d 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65  ;..    rc = acce
1ea98 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
1ea99 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75  offset, amt, pBu
1ea9a 66 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  f, 0);..  }..  r
1ea9b 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
1ea9c 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /*..** Return a 
1ea9d 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f  pointer to paylo
1ea9e 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ad information f
1ea9f 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68  rom the entry th
1eaa0 61 74 20 74 68 65 20 0d 0a 2a 2a 20 70 43 75 72  at the ..** pCur
1eaa1 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
1eaa2 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69  ing to.  The poi
1eaa3 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 62  nter is to the b
1eaa4 65 67 69 6e 6e 69 6e 67 20 6f 66 0d 0a 2a 2a 20  eginning of..** 
1eaa5 74 68 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b  the key if skipK
1eaa6 65 79 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69  ey==0 and it poi
1eaa7 6e 74 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e  nts to the begin
1eaa8 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0d  ning of data if.
1eaa9 0a 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20  .** skipKey==1. 
1eaaa 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   The number of b
1eaab 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c  ytes of availabl
1eaac 65 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77 72  e key/data is wr
1eaad 69 74 74 65 6e 0d 0a 2a 2a 20 69 6e 74 6f 20 2a  itten..** into *
1eaae 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d  pAmt.  If *pAmt=
1eaaf 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  =0, then the val
1eab0 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c  ue returned will
1eab1 20 6e 6f 74 20 62 65 0d 0a 2a 2a 20 61 20 76 61   not be..** a va
1eab2 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a  lid pointer...**
1eab3 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1eab4 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  e is an optimiza
1eab5 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d  tion.  It is com
1eab6 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69  mon for the enti
1eab7 72 65 20 6b 65 79 0d 0a 2a 2a 20 61 6e 64 20 64  re key..** and d
1eab8 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 68  ata to fit on th
1eab9 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e 64  e local page and
1eaba 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65   for there to be
1eabb 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0d 0a 2a 2a   no overflow..**
1eabc 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68   pages.  When th
1eabd 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72  at is so, this r
1eabe 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73  outine can be us
1eabf 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
1eac0 0d 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74  ..** key and dat
1eac1 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67  a without making
1eac2 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65   a copy.  If the
1eac3 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61   key and/or data
1eac4 20 73 70 69 6c 6c 73 0d 0a 2a 2a 20 6f 6e 74 6f   spills..** onto
1eac5 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c   overflow pages,
1eac6 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c   then accessPayl
1eac7 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73  oad() must be us
1eac8 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65  ed to reassemble
1eac9 0d 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74  ..** the key/dat
1eaca 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e  a and copy it in
1eacb 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65  to a preallocate
1eacc 64 20 62 75 66 66 65 72 2e 0d 0a 2a 2a 0d 0a 2a  d buffer...**..*
1eacd 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65  * The pointer re
1eace 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72  turned by this r
1eacf 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72  outine looks dir
1ead0 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63  ectly into the c
1ead1 61 63 68 65 64 0d 0a 2a 2a 20 70 61 67 65 20 6f  ached..** page o
1ead2 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
1ead3 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 74 20   The data might 
1ead4 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74  change or move t
1ead5 68 65 20 6e 65 78 74 20 74 69 6d 65 0d 0a 2a 2a  he next time..**
1ead6 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 69   any btree routi
1ead7 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0d 0a 2a  ne is called...*
1ead8 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  /..static const 
1ead9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66  unsigned char *f
1eada 65 74 63 68 50 61 79 6c 6f 61 64 28 0d 0a 20 20  etchPayload(..  
1eadb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
1eadc 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70       /* Cursor p
1eadd 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79  ointing to entry
1eade 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
1eadf 0d 0a 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20  ..  int *pAmt,  
1eae0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
1eae1 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
1eae2 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20  available bytes 
1eae3 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73  here */..  int s
1eae4 6b 69 70 4b 65 79 20 20 20 20 20 20 20 20 20 20  kipKey          
1eae5 2f 2a 20 72 65 61 64 20 62 65 67 69 6e 6e 69 6e  /* read beginnin
1eae6 67 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69  g at data if thi
1eae7 73 20 69 73 20 74 72 75 65 20 2a 2f 0d 0a 29 7b  s is true */..){
1eae8 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ..  unsigned cha
1eae9 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0d 0a 20 20  r *aPayload;..  
1eaea 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0d  MemPage *pPage;.
1eaeb 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0d 0a 20 20  .  u32 nKey;..  
1eaec 75 33 32 20 6e 4c 6f 63 61 6c 3b 0d 0a 0d 0a 20  u32 nLocal;.... 
1eaed 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30   assert( pCur!=0
1eaee 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e   && pCur->iPage>
1eaef 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61  =0 && pCur->apPa
1eaf0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
1eaf1 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ;..  assert( pCu
1eaf2 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1eaf3 52 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20 61 73  R_VALID );..  as
1eaf4 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1eaf5 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0d  sMutex(pCur) );.
1eaf6 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
1eaf7 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1eaf8 61 67 65 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  age];..  assert(
1eaf9 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1eafa 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d  r->iPage]<pPage-
1eafb 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 20 20 69 66 28  >nCell );..  if(
1eafc 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 69 6e 66   NEVER(pCur->inf
1eafd 6f 2e 6e 53 69 7a 65 3d 3d 30 29 20 29 7b 0d 0a  o.nSize==0) ){..
1eafe 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65      btreeParseCe
1eaff 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ll(pCur->apPage[
1eb00 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 20 70 43  pCur->iPage], pC
1eb01 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1eb02 69 50 61 67 65 5d 2c 0d 0a 20 20 20 20 20 20 20  iPage],..       
1eb03 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 75              &pCu
1eb04 72 2d 3e 69 6e 66 6f 29 3b 0d 0a 20 20 7d 0d 0a  r->info);..  }..
1eb05 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75    aPayload = pCu
1eb06 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0d 0a  r->info.pCell;..
1eb07 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43    aPayload += pC
1eb08 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72  ur->info.nHeader
1eb09 3b 0d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  ;..  if( pPage->
1eb0a 69 6e 74 4b 65 79 20 29 7b 0d 0a 20 20 20 20 6e  intKey ){..    n
1eb0b 4b 65 79 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73  Key = 0;..  }els
1eb0c 65 7b 0d 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28  e{..    nKey = (
1eb0d 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  int)pCur->info.n
1eb0e 4b 65 79 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  Key;..  }..  if(
1eb0f 20 73 6b 69 70 4b 65 79 20 29 7b 0d 0a 20 20 20   skipKey ){..   
1eb10 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65   aPayload += nKe
1eb11 79 3b 0d 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d  y;..    nLocal =
1eb12 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63   pCur->info.nLoc
1eb13 61 6c 20 2d 20 6e 4b 65 79 3b 0d 0a 20 20 7d 65  al - nKey;..  }e
1eb14 6c 73 65 7b 0d 0a 20 20 20 20 6e 4c 6f 63 61 6c  lse{..    nLocal
1eb15 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c   = pCur->info.nL
1eb16 6f 63 61 6c 3b 0d 0a 20 20 20 20 61 73 73 65 72  ocal;..    asser
1eb17 74 28 20 6e 4c 6f 63 61 6c 3c 3d 6e 4b 65 79 20  t( nLocal<=nKey 
1eb18 29 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 41 6d 74  );..  }..  *pAmt
1eb19 20 3d 20 6e 4c 6f 63 61 6c 3b 0d 0a 20 20 72 65   = nLocal;..  re
1eb1a 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0d 0a  turn aPayload;..
1eb1b 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 6f  }....../*..** Fo
1eb1c 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
1eb1d 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20   cursor pCur is 
1eb1e 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e  point to, return
1eb1f 20 61 73 0d 0a 2a 2a 20 6d 61 6e 79 20 62 79 74   as..** many byt
1eb20 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72  es of the key or
1eb21 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61   data as are ava
1eb22 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  ilable on the lo
1eb23 63 61 6c 0d 0a 2a 2a 20 62 2d 74 72 65 65 20 70  cal..** b-tree p
1eb24 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20  age.  Write the 
1eb25 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61  number of availa
1eb26 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a  ble bytes into *
1eb27 70 41 6d 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  pAmt...**..** Th
1eb28 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
1eb29 65 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e  ed is ephemeral.
1eb2a 20 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d    The key/data m
1eb2b 61 79 20 6d 6f 76 65 0d 0a 2a 2a 20 6f 72 20 62  ay move..** or b
1eb2c 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74  e destroyed on t
1eb2d 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
1eb2e 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e  any Btree routin
1eb2f 65 2c 0d 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67  e,..** including
1eb30 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65   calls from othe
1eb31 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e 73  r threads agains
1eb32 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65  t the same cache
1eb33 2e 0d 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d  ...** Hence, a m
1eb34 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68  utex on the BtSh
1eb35 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68  ared should be h
1eb36 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  eld prior to cal
1eb37 6c 69 6e 67 0d 0a 2a 2a 20 74 68 69 73 20 72 6f  ling..** this ro
1eb38 75 74 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  utine...**..** T
1eb39 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 73  hese routines is
1eb3a 20 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 69   used to get qui
1eb3b 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 79  ck access to key
1eb3c 20 61 6e 64 20 64 61 74 61 0d 0a 2a 2a 20 69 6e   and data..** in
1eb3d 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65   the common case
1eb3e 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c   where no overfl
1eb3f 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73 65  ow pages are use
1eb40 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
1eb41 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
1eb42 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b  d *sqlite3BtreeK
1eb43 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  eyFetch(BtCursor
1eb44 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d   *pCur, int *pAm
1eb45 74 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t){..  const voi
1eb46 64 20 2a 70 20 3d 20 30 3b 0d 0a 20 20 61 73 73  d *p = 0;..  ass
1eb47 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1eb48 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1eb49 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1eb4a 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 63   );..  assert( c
1eb4b 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1eb4c 70 43 75 72 29 20 29 3b 0d 0a 20 20 69 66 28 20  pCur) );..  if( 
1eb4d 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74  ALWAYS(pCur->eSt
1eb4e 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
1eb4f 44 29 20 29 7b 0d 0a 20 20 20 20 70 20 3d 20 28  D) ){..    p = (
1eb50 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63  const void*)fetc
1eb51 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70  hPayload(pCur, p
1eb52 41 6d 74 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20  Amt, 0);..  }.. 
1eb53 20 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 53   return p;..}..S
1eb54 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
1eb55 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1eb56 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28  3BtreeDataFetch(
1eb57 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
1eb58 69 6e 74 20 2a 70 41 6d 74 29 7b 0d 0a 20 20 63  int *pAmt){..  c
1eb59 6f 6e 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 30  onst void *p = 0
1eb5a 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1eb5b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1eb5c 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
1eb5d 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 61  ->mutex) );..  a
1eb5e 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1eb5f 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1eb60 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ..  if( ALWAYS(p
1eb61 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
1eb62 53 4f 52 5f 56 41 4c 49 44 29 20 29 7b 0d 0a 20  SOR_VALID) ){.. 
1eb63 20 20 20 70 20 3d 20 28 63 6f 6e 73 74 20 76 6f     p = (const vo
1eb64 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64  id*)fetchPayload
1eb65 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b  (pCur, pAmt, 1);
1eb66 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
1eb67 70 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  p;..}....../*..*
1eb68 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1eb69 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20  r down to a new 
1eb6a 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65  child page.  The
1eb6b 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e   newPgno argumen
1eb6c 74 20 69 73 20 74 68 65 0d 0a 2a 2a 20 70 61 67  t is the..** pag
1eb6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1eb6e 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f  child page to mo
1eb6f 76 65 20 74 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ve to...**..** T
1eb70 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
1eb71 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52  urns SQLITE_CORR
1eb72 55 50 54 20 69 66 20 74 68 65 20 70 61 67 65 2d  UPT if the page-
1eb73 68 65 61 64 65 72 20 66 6c 61 67 73 20 66 69 65  header flags fie
1eb74 6c 64 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 6e 65  ld of..** the ne
1eb75 77 20 63 68 69 6c 64 20 70 61 67 65 20 64 6f 65  w child page doe
1eb76 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20  s not match the 
1eb77 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74  flags field of t
1eb78 68 65 20 70 61 72 65 6e 74 20 28 69 2e 65 2e 0d  he parent (i.e..
1eb79 0a 2a 2a 20 69 66 20 61 6e 20 69 6e 74 6b 65 79  .** if an intkey
1eb7a 20 70 61 67 65 20 61 70 70 65 61 72 73 20 74 6f   page appears to
1eb7b 20 62 65 20 74 68 65 20 70 61 72 65 6e 74 20 6f   be the parent o
1eb7c 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70  f a non-intkey p
1eb7d 61 67 65 2c 20 6f 72 0d 0a 2a 2a 20 76 69 63 65  age, or..** vice
1eb7e 2d 76 65 72 73 61 29 2e 0d 0a 2a 2f 0d 0a 73 74  -versa)...*/..st
1eb7f 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43  atic int moveToC
1eb80 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70  hild(BtCursor *p
1eb81 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f  Cur, u32 newPgno
1eb82 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  ){..  int rc;.. 
1eb83 20 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69   int i = pCur->i
1eb84 50 61 67 65 3b 0d 0a 20 20 4d 65 6d 50 61 67 65  Page;..  MemPage
1eb85 20 2a 70 4e 65 77 50 61 67 65 3b 0d 0a 20 20 42   *pNewPage;..  B
1eb86 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1eb87 43 75 72 2d 3e 70 42 74 3b 0d 0a 0d 0a 20 20 61  Cur->pBt;....  a
1eb88 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1eb89 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1eb8a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ..  assert( pCur
1eb8b 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1eb8c 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20 61 73 73  _VALID );..  ass
1eb8d 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65  ert( pCur->iPage
1eb8e 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45  <BTCURSOR_MAX_DE
1eb8f 50 54 48 20 29 3b 0d 0a 20 20 69 66 28 20 70 43  PTH );..  if( pC
1eb90 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55  ur->iPage>=(BTCU
1eb91 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31  RSOR_MAX_DEPTH-1
1eb92 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ) ){..    return
1eb93 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1eb94 42 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63  BKPT;..  }..  rc
1eb95 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
1eb96 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20  e(pBt, newPgno, 
1eb97 26 70 4e 65 77 50 61 67 65 29 3b 0d 0a 20 20 69  &pNewPage);..  i
1eb98 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1eb99 63 3b 0d 0a 20 20 70 43 75 72 2d 3e 61 70 50 61  c;..  pCur->apPa
1eb9a 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 61  ge[i+1] = pNewPa
1eb9b 67 65 3b 0d 0a 20 20 70 43 75 72 2d 3e 61 69 49  ge;..  pCur->aiI
1eb9c 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0d 0a 20 20  dx[i+1] = 0;..  
1eb9d 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0d 0a  pCur->iPage++;..
1eb9e 0d 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ..  pCur->info.n
1eb9f 53 69 7a 65 20 3d 20 30 3b 0d 0a 20 20 70 43 75  Size = 0;..  pCu
1eba0 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
1eba1 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 50 61 67  ;..  if( pNewPag
1eba2 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c 20 70 4e  e->nCell<1 || pN
1eba3 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 79 21 3d  ewPage->intKey!=
1eba4 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d  pCur->apPage[i]-
1eba5 3e 69 6e 74 4b 65 79 20 29 7b 0d 0a 20 20 20 20  >intKey ){..    
1eba6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1eba7 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 7d  RRUPT_BKPT;..  }
1eba8 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1eba9 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20  E_OK;..}....#if 
1ebaa 30 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 67 65 20 70  0../*..** Page p
1ebab 50 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74  Parent is an int
1ebac 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29  ernal (non-leaf)
1ebad 20 74 72 65 65 20 70 61 67 65 2e 20 54 68 69 73   tree page. This
1ebae 20 66 75 6e 63 74 69 6f 6e 20 0d 0a 2a 2a 20 61   function ..** a
1ebaf 73 73 65 72 74 73 20 74 68 61 74 20 70 61 67 65  sserts that page
1ebb0 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69   number iChild i
1ebb1 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64  s the left-child
1ebb2 20 69 66 20 74 68 65 20 69 49 64 78 27 74 68 0d   if the iIdx'th.
1ebb3 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 65  .** cell in page
1ebb4 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66   pParent. Or, if
1ebb5 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 74   iIdx is equal t
1ebb6 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  o the total numb
1ebb7 65 72 20 6f 66 0d 0a 2a 2a 20 63 65 6c 6c 73 20  er of..** cells 
1ebb8 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74  in pParent, that
1ebb9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
1ebba 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74  ild is the right
1ebbb 2d 63 68 69 6c 64 20 6f 66 0d 0a 2a 2a 20 74 68  -child of..** th
1ebbc 65 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  e page...*/..sta
1ebbd 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50  tic void assertP
1ebbe 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61  arentIndex(MemPa
1ebbf 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  ge *pParent, int
1ebc0 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69   iIdx, Pgno iChi
1ebc1 6c 64 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  ld){..  assert( 
1ebc2 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e  iIdx<=pParent->n
1ebc3 43 65 6c 6c 20 29 3b 0d 0a 20 20 69 66 28 20 69  Cell );..  if( i
1ebc4 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43  Idx==pParent->nC
1ebc5 65 6c 6c 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ell ){..    asse
1ebc6 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70 50  rt( get4byte(&pP
1ebc7 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61  arent->aData[pPa
1ebc8 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  rent->hdrOffset+
1ebc9 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0d 0a  8])==iChild );..
1ebca 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73    }else{..    as
1ebcb 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66  sert( get4byte(f
1ebcc 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  indCell(pParent,
1ebcd 20 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20   iIdx))==iChild 
1ebce 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73  );..  }..}..#els
1ebcf 65 0d 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73  e..#  define ass
1ebd0 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78  ertParentIndex(x
1ebd1 2c 79 2c 7a 29 20 0d 0a 23 65 6e 64 69 66 0d 0a  ,y,z) ..#endif..
1ebd2 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 6f 76 65 20 74 68  ../*..** Move th
1ebd3 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74  e cursor up to t
1ebd4 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0d  he parent page..
1ebd5 0a 2a 2a 0d 0a 2a 2a 20 70 43 75 72 2d 3e 69 64  .**..** pCur->id
1ebd6 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  x is set to the 
1ebd7 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20  cell index that 
1ebd8 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69  contains the poi
1ebd9 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68 65 20  nter..** to the 
1ebda 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69  page we are comi
1ebdb 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20  ng from.  If we 
1ebdc 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20  are coming from 
1ebdd 74 68 65 0d 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f  the..** right-mo
1ebde 73 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68  st child page th
1ebdf 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20  en pCur->idx is 
1ebe0 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20  set to one more 
1ebe1 74 68 61 6e 0d 0a 2a 2a 20 74 68 65 20 6c 61 72  than..** the lar
1ebe2 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e  gest cell index.
1ebe3 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
1ebe4 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42  d moveToParent(B
1ebe5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d  tCursor *pCur){.
1ebe6 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
1ebe7 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
1ebe8 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
1ebe9 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1ebea 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0d 0a 20  RSOR_VALID );.. 
1ebeb 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
1ebec 50 61 67 65 3e 30 20 29 3b 0d 0a 20 20 61 73 73  Page>0 );..  ass
1ebed 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
1ebee 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29  e[pCur->iPage] )
1ebef 3b 0d 0a 0d 0a 20 20 2f 2a 20 55 50 44 41 54 45  ;....  /* UPDATE
1ebf0 3a 20 49 74 20 69 73 20 61 63 74 75 61 6c 6c 79  : It is actually
1ebf1 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68   possible for th
1ebf2 65 20 63 6f 6e 64 69 74 69 6f 6e 20 74 65 73 74  e condition test
1ebf3 65 64 20 62 79 20 74 68 65 20 61 73 73 65 72 74  ed by the assert
1ebf4 0d 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 74 6f 20  ..  ** below to 
1ebf5 62 65 20 75 6e 74 72 75 65 20 69 66 20 74 68 65  be untrue if the
1ebf6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1ebf7 73 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20  s corrupt. This 
1ebf8 63 61 6e 20 6f 63 63 75 72 20 69 66 0d 0a 20 20  can occur if..  
1ebf9 2a 2a 20 6f 6e 65 20 63 75 72 73 6f 72 20 68 61  ** one cursor ha
1ebfa 73 20 6d 6f 64 69 66 69 65 64 20 70 61 67 65 20  s modified page 
1ebfb 70 50 61 72 65 6e 74 20 77 68 69 6c 65 20 61 20  pParent while a 
1ebfc 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 20  reference to it 
1ebfd 69 73 20 68 65 6c 64 20 0d 0a 20 20 2a 2a 20 62  is held ..  ** b
1ebfe 79 20 61 20 73 65 63 6f 6e 64 20 63 75 72 73 6f  y a second curso
1ebff 72 2e 20 57 68 69 63 68 20 63 61 6e 20 6f 6e 6c  r. Which can onl
1ec00 79 20 68 61 70 70 65 6e 20 69 66 20 61 20 73 69  y happen if a si
1ec01 6e 67 6c 65 20 70 61 67 65 20 69 73 20 6c 69 6e  ngle page is lin
1ec02 6b 65 64 0d 0a 20 20 2a 2a 20 69 6e 74 6f 20 6d  ked..  ** into m
1ec03 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 2d 74  ore than one b-t
1ec04 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69 6e  ree structure in
1ec05 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
1ec06 61 73 65 2e 20 20 2a 2f 0d 0a 23 69 66 20 30 0d  ase.  */..#if 0.
1ec07 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 49  .  assertParentI
1ec08 6e 64 65 78 28 0d 0a 20 20 20 20 70 43 75 72 2d  ndex(..    pCur-
1ec09 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1ec0a 61 67 65 2d 31 5d 2c 20 0d 0a 20 20 20 20 70 43  age-1], ..    pC
1ec0b 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1ec0c 69 50 61 67 65 2d 31 5d 2c 20 0d 0a 20 20 20 20  iPage-1], ..    
1ec0d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1ec0e 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0d  r->iPage]->pgno.
1ec0f 0a 20 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  .  );..#endif.. 
1ec10 20 74 65 73 74 63 61 73 65 28 20 70 43 75 72 2d   testcase( pCur-
1ec11 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1ec12 67 65 2d 31 5d 20 3e 20 70 43 75 72 2d 3e 61 70  ge-1] > pCur->ap
1ec13 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1ec14 2d 31 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 0d  -1]->nCell );...
1ec15 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
1ec16 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1ec17 2d 3e 69 50 61 67 65 5d 29 3b 0d 0a 20 20 70 43  ->iPage]);..  pC
1ec18 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0d 0a 20 20  ur->iPage--;..  
1ec19 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1ec1a 20 3d 20 30 3b 0d 0a 20 20 70 43 75 72 2d 3e 76   = 0;..  pCur->v
1ec1b 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0d 0a 7d  alidNKey = 0;..}
1ec1c 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 6f 76 65 20  ..../*..** Move 
1ec1d 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 70 6f  the cursor to po
1ec1e 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  int to the root 
1ec1f 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d 74 72  page of its b-tr
1ec20 65 65 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a  ee structure...*
1ec21 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62  *..** If the tab
1ec22 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61 6c  le has a virtual
1ec23 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e   root page, then
1ec24 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d   the cursor is m
1ec25 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0d 0a 2a  oved to point..*
1ec26 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  * to the virtual
1ec27 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65   root page inste
1ec28 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c  ad of the actual
1ec29 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61   root page. A ta
1ec2a 62 6c 65 20 68 61 73 20 61 0d 0a 2a 2a 20 76 69  ble has a..** vi
1ec2b 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20  rtual root page 
1ec2c 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20  when the actual 
1ec2d 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69  root page contai
1ec2e 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20  ns no cells and 
1ec2f 61 20 0d 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68  a ..** single ch
1ec30 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63  ild page. This c
1ec31 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77  an only happen w
1ec32 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f  ith the table ro
1ec33 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0d  oted at page 1..
1ec34 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 62  .**..** If the b
1ec35 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
1ec36 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75  is empty, the cu
1ec37 72 73 6f 72 20 73 74 61 74 65 20 69 73 20 73 65  rsor state is se
1ec38 74 20 74 6f 20 0d 0a 2a 2a 20 43 55 52 53 4f 52  t to ..** CURSOR
1ec39 5f 49 4e 56 41 4c 49 44 2e 20 4f 74 68 65 72 77  _INVALID. Otherw
1ec3a 69 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20  ise, the cursor 
1ec3b 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
1ec3c 74 6f 20 74 68 65 20 66 69 72 73 74 0d 0a 2a 2a  to the first..**
1ec3d 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 20 6f 6e   cell located on
1ec3e 20 74 68 65 20 72 6f 6f 74 20 28 6f 72 20 76 69   the root (or vi
1ec3f 72 74 75 61 6c 20 72 6f 6f 74 29 20 70 61 67 65  rtual root) page
1ec40 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20   and the cursor 
1ec41 73 74 61 74 65 0d 0a 2a 2a 20 69 73 20 73 65 74  state..** is set
1ec42 20 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44   to CURSOR_VALID
1ec43 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 69  ...**..** If thi
1ec44 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
1ec45 6e 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ns successfully,
1ec46 20 69 74 20 6d 61 79 20 62 65 20 61 73 73 75 6d   it may be assum
1ec47 65 64 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a 20  ed that the..** 
1ec48 70 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67  page-header flag
1ec49 73 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  s indicate that 
1ec4a 74 68 65 20 5b 76 69 72 74 75 61 6c 5d 20 72 6f  the [virtual] ro
1ec4b 6f 74 2d 70 61 67 65 20 69 73 20 74 68 65 20 65  ot-page is the e
1ec4c 78 70 65 63 74 65 64 20 0d 0a 2a 2a 20 6b 69 6e  xpected ..** kin
1ec4d 64 20 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65  d of b-tree page
1ec4e 20 28 69 2e 65 2e 20 69 66 20 77 68 65 6e 20 6f   (i.e. if when o
1ec4f 70 65 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f  pening the curso
1ec50 72 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64  r the caller did
1ec51 20 6e 6f 74 0d 0a 2a 2a 20 73 70 65 63 69 66 79   not..** specify
1ec52 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1ec53 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62  ture the flags b
1ec54 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78  yte is set to 0x
1ec55 30 35 20 6f 72 20 30 78 30 44 2c 0d 0a 2a 2a 20  05 or 0x0D,..** 
1ec56 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61 62  indicating a tab
1ec57 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69 66  le b-tree, or if
1ec58 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20   the caller did 
1ec59 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66  specify a KeyInf
1ec5a 6f 20 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  o ..** structure
1ec5b 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
1ec5c 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f  is set to 0x02 o
1ec5d 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69  r 0x0A, indicati
1ec5e 6e 67 20 61 6e 20 69 6e 64 65 78 0d 0a 2a 2a 20  ng an index..** 
1ec5f 62 2d 74 72 65 65 29 2e 0d 0a 2a 2f 0d 0a 73 74  b-tree)...*/..st
1ec60 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52  atic int moveToR
1ec61 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  oot(BtCursor *pC
1ec62 75 72 29 7b 0d 0a 20 20 4d 65 6d 50 61 67 65 20  ur){..  MemPage 
1ec63 2a 70 52 6f 6f 74 3b 0d 0a 20 20 69 6e 74 20 72  *pRoot;..  int r
1ec64 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
1ec65 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75    Btree *p = pCu
1ec66 72 2d 3e 70 42 74 72 65 65 3b 0d 0a 20 20 42 74  r->pBtree;..  Bt
1ec67 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1ec68 3e 70 42 74 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  >pBt;....  asser
1ec69 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
1ec6a 74 65 78 28 70 43 75 72 29 20 29 3b 0d 0a 20 20  tex(pCur) );..  
1ec6b 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49  assert( CURSOR_I
1ec6c 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f  NVALID < CURSOR_
1ec6d 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0d 0a  REQUIRESEEK );..
1ec6e 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52    assert( CURSOR
1ec6f 5f 56 41 4c 49 44 20 20 20 3c 20 43 55 52 53 4f  _VALID   < CURSO
1ec70 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
1ec71 0d 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53  ..  assert( CURS
1ec72 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 52  OR_FAULT   > CUR
1ec73 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
1ec74 29 3b 0d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e  );..  if( pCur->
1ec75 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52  eState>=CURSOR_R
1ec76 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0d 0a 20  EQUIRESEEK ){.. 
1ec77 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74     if( pCur->eSt
1ec78 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c  ate==CURSOR_FAUL
1ec79 54 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  T ){..      asse
1ec7a 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  rt( pCur->skipNe
1ec7b 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  xt!=SQLITE_OK );
1ec7c 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ..      return p
1ec7d 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0d 0a  Cur->skipNext;..
1ec7e 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1ec7f 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
1ec80 6f 72 28 70 43 75 72 29 3b 0d 0a 20 20 7d 0d 0a  or(pCur);..  }..
1ec81 0d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50  ..  if( pCur->iP
1ec82 61 67 65 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 69  age>=0 ){..    i
1ec83 6e 74 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69  nt i;..    for(i
1ec84 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61  =1; i<=pCur->iPa
1ec85 67 65 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  ge; i++){..     
1ec86 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
1ec87 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0d 0a  r->apPage[i]);..
1ec88 20 20 20 20 7d 0d 0a 20 20 20 20 70 43 75 72 2d      }..    pCur-
1ec89 3e 69 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 7d  >iPage = 0;..  }
1ec8a 65 6c 73 65 20 69 66 28 20 70 43 75 72 2d 3e 70  else if( pCur->p
1ec8b 67 6e 6f 52 6f 6f 74 3d 3d 30 20 29 7b 0d 0a 20  gnoRoot==0 ){.. 
1ec8c 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
1ec8d 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1ec8e 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ;..    return SQ
1ec8f 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73  LITE_OK;..  }els
1ec90 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20 67 65 74  e{..    rc = get
1ec91 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
1ec92 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
1ec93 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30   &pCur->apPage[0
1ec94 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21  ]);..    if( rc!
1ec95 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
1ec96 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74       pCur->eStat
1ec97 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
1ec98 49 44 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  ID;..      retur
1ec99 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  n rc;..    }..  
1ec9a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
1ec9b 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20  0;....    /* If 
1ec9c 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69  pCur->pKeyInfo i
1ec9d 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
1ec9e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74   the caller that
1ec9f 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 72   opened this cur
1eca0 73 6f 72 0d 0a 20 20 20 20 2a 2a 20 65 78 70 65  sor..    ** expe
1eca1 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20  cted to open it 
1eca2 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72  on an index b-tr
1eca3 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ee. Otherwise, i
1eca4 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0d 0a 20  f pKeyInfo is.. 
1eca5 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20     ** NULL, the 
1eca6 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61  caller expects a
1eca7 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49   table b-tree. I
1eca8 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68  f this is not th
1eca9 65 20 63 61 73 65 2c 0d 0a 20 20 20 20 2a 2a 20  e case,..    ** 
1ecaa 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45  return an SQLITE
1ecab 5f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20  _CORRUPT error. 
1ecac 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
1ecad 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1ecae 2d 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70  ->intKey==1 || p
1ecaf 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
1ecb0 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0d 0a 20 20  intKey==0 );..  
1ecb1 20 20 69 66 28 20 28 70 43 75 72 2d 3e 70 4b 65    if( (pCur->pKe
1ecb2 79 49 6e 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d  yInfo==0)!=pCur-
1ecb3 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
1ecb4 65 79 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  ey ){..      ret
1ecb5 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1ecb6 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d  PT_BKPT;..    }.
1ecb7 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 73 73  .  }....  /* Ass
1ecb8 65 72 74 20 74 68 61 74 20 74 68 65 20 72 6f 6f  ert that the roo
1ecb9 74 20 70 61 67 65 20 69 73 20 6f 66 20 74 68 65  t page is of the
1ecba 20 63 6f 72 72 65 63 74 20 74 79 70 65 2e 20 54   correct type. T
1ecbb 68 69 73 20 6d 75 73 74 20 62 65 20 74 68 65 0d  his must be the.
1ecbc 0a 20 20 2a 2a 20 63 61 73 65 20 61 73 20 74 68  .  ** case as th
1ecbd 65 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66  e call to this f
1ecbe 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61  unction that loa
1ecbf 64 65 64 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  ded the root-pag
1ecc0 65 20 28 65 69 74 68 65 72 0d 0a 20 20 2a 2a 20  e (either..  ** 
1ecc1 74 68 69 73 20 63 61 6c 6c 20 6f 72 20 61 20 70  this call or a p
1ecc2 72 65 76 69 6f 75 73 20 69 6e 76 6f 63 61 74 69  revious invocati
1ecc3 6f 6e 29 20 77 6f 75 6c 64 20 68 61 76 65 20 64  on) would have d
1ecc4 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 69  etected corrupti
1ecc5 6f 6e 20 0d 0a 20 20 2a 2a 20 69 66 20 74 68 65  on ..  ** if the
1ecc6 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 65 72 65   assumption were
1ecc7 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 69   not true, and i
1ecc8 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
1ecc9 65 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20  e for the flags 
1ecca 0d 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 68  ..  ** byte to h
1eccb 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
1eccc 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 75 72  d while this cur
1eccd 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  sor is holding a
1ecce 20 72 65 66 65 72 65 6e 63 65 0d 0a 20 20 2a 2a   reference..  **
1eccf 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 2a   to the page.  *
1ecd0 2f 0d 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75  /..  pRoot = pCu
1ecd1 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0d 0a 20  r->apPage[0];.. 
1ecd2 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e   assert( pRoot->
1ecd3 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f  pgno==pCur->pgno
1ecd4 52 6f 6f 74 20 29 3b 0d 0a 20 20 61 73 73 65 72  Root );..  asser
1ecd5 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74  t( pRoot->isInit
1ecd6 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49   && (pCur->pKeyI
1ecd7 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e  nfo==0)==pRoot->
1ecd8 69 6e 74 4b 65 79 20 29 3b 0d 0a 0d 0a 20 20 70  intKey );....  p
1ecd9 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20  Cur->aiIdx[0] = 
1ecda 30 3b 0d 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f  0;..  pCur->info
1ecdb 2e 6e 53 69 7a 65 20 3d 20 30 3b 0d 0a 20 20 70  .nSize = 0;..  p
1ecdc 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b  Cur->atLast = 0;
1ecdd 0d 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ..  pCur->validN
1ecde 4b 65 79 20 3d 20 30 3b 0d 0a 0d 0a 20 20 69 66  Key = 0;....  if
1ecdf 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d  ( pRoot->nCell==
1ece0 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61  0 && !pRoot->lea
1ece1 66 20 29 7b 0d 0a 20 20 20 20 50 67 6e 6f 20 73  f ){..    Pgno s
1ece2 75 62 70 61 67 65 3b 0d 0a 20 20 20 20 69 66 28  ubpage;..    if(
1ece3 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20   pRoot->pgno!=1 
1ece4 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1ece5 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20  CORRUPT_BKPT;.. 
1ece6 20 20 20 73 75 62 70 61 67 65 20 3d 20 67 65 74     subpage = get
1ece7 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44  4byte(&pRoot->aD
1ece8 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66  ata[pRoot->hdrOf
1ece9 66 73 65 74 2b 38 5d 29 3b 0d 0a 20 20 20 20 70  fset+8]);..    p
1ecea 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
1eceb 52 53 4f 52 5f 56 41 4c 49 44 3b 0d 0a 20 20 20  RSOR_VALID;..   
1ecec 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
1eced 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 29  d(pCur, subpage)
1ecee 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
1ecef 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
1ecf0 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30  ((pRoot->nCell>0
1ecf1 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43  )?CURSOR_VALID:C
1ecf2 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0d  URSOR_INVALID);.
1ecf3 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
1ecf4 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
1ecf5 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1ecf6 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74  down to the left
1ecf7 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79  -most leaf entry
1ecf8 20 62 65 6e 65 61 74 68 20 74 68 65 0d 0a 2a 2a   beneath the..**
1ecf9 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20   entry to which 
1ecfa 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  it is currently 
1ecfb 70 6f 69 6e 74 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  pointing...**..*
1ecfc 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  * The left-most 
1ecfd 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65 20  leaf is the one 
1ecfe 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 73  with the smalles
1ecff 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72 73  t key - the firs
1ed00 74 0d 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69  t..** in ascendi
1ed01 6e 67 20 6f 72 64 65 72 2e 0d 0a 2a 2f 0d 0a 73  ng order...*/..s
1ed02 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
1ed03 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f  Leftmost(BtCurso
1ed04 72 20 2a 70 43 75 72 29 7b 0d 0a 20 20 50 67 6e  r *pCur){..  Pgn
1ed05 6f 20 70 67 6e 6f 3b 0d 0a 20 20 69 6e 74 20 72  o pgno;..  int r
1ed06 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
1ed07 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1ed08 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 63  ;....  assert( c
1ed09 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1ed0a 70 43 75 72 29 20 29 3b 0d 0a 20 20 61 73 73 65  pCur) );..  asse
1ed0b 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
1ed0c 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
1ed0d 3b 0d 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  ;..  while( rc==
1ed0e 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70  SQLITE_OK && !(p
1ed0f 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
1ed10 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1ed11 29 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20 20 20 20  )->leaf ){..    
1ed12 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
1ed13 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
1ed14 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  <pPage->nCell );
1ed15 0d 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74  ..    pgno = get
1ed16 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
1ed17 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64  Page, pCur->aiId
1ed18 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29  x[pCur->iPage]))
1ed19 3b 0d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  ;..    rc = move
1ed1a 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67  ToChild(pCur, pg
1ed1b 6e 6f 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  no);..  }..  ret
1ed1c 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
1ed1d 0d 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ..** Move the cu
1ed1e 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65  rsor down to the
1ed1f 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66   right-most leaf
1ed20 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
1ed21 68 65 0d 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77  he..** page to w
1ed22 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72 65  hich it is curre
1ed23 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20  ntly pointing.  
1ed24 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 66 65  Notice the diffe
1ed25 72 65 6e 63 65 0d 0a 2a 2a 20 62 65 74 77 65 65  rence..** betwee
1ed26 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  n moveToLeftmost
1ed27 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67  () and moveToRig
1ed28 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54  htmost().  moveT
1ed29 6f 4c 65 66 74 6d 6f 73 74 28 29 0d 0a 2a 2a 20  oLeftmost()..** 
1ed2a 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d  finds the left-m
1ed2b 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74  ost entry beneat
1ed2c 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68  h the *entry* wh
1ed2d 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68  ereas moveToRigh
1ed2e 74 6d 6f 73 74 28 29 0d 0a 2a 2a 20 66 69 6e 64  tmost()..** find
1ed2f 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  s the right-most
1ed30 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
1ed31 68 65 20 2a 70 61 67 65 2a 2e 0d 0a 2a 2a 0d 0a  he *page*...**..
1ed32 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  ** The right-mos
1ed33 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f  t entry is the o
1ed34 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67  ne with the larg
1ed35 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61  est key - the la
1ed36 73 74 0d 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73  st..** key in as
1ed37 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0d 0a  cending order...
1ed38 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  */..static int m
1ed39 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42  oveToRightmost(B
1ed3a 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d  tCursor *pCur){.
1ed3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0d 0a 20  .  Pgno pgno;.. 
1ed3c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1ed3d 5f 4f 4b 3b 0d 0a 20 20 4d 65 6d 50 61 67 65 20  _OK;..  MemPage 
1ed3e 2a 70 50 61 67 65 20 3d 20 30 3b 0d 0a 0d 0a 20  *pPage = 0;.... 
1ed3f 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1ed40 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1ed41 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43  );..  assert( pC
1ed42 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1ed43 4f 52 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20 77  OR_VALID );..  w
1ed44 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1ed45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d  _OK && !(pPage =
1ed46 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1ed47 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61  ur->iPage])->lea
1ed48 66 20 29 7b 0d 0a 20 20 20 20 70 67 6e 6f 20 3d  f ){..    pgno =
1ed49 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
1ed4a 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
1ed4b 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0d 0a 20  drOffset+8]);.. 
1ed4c 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
1ed4d 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50  Cur->iPage] = pP
1ed4e 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 20 20 20  age->nCell;..   
1ed4f 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
1ed50 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0d 0a  d(pCur, pgno);..
1ed51 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
1ed52 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1ed53 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1ed54 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67  r->iPage] = pPag
1ed55 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0d 0a 20 20 20  e->nCell-1;..   
1ed56 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
1ed57 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70 43 75 72  e = 0;..    pCur
1ed58 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
1ed59 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
1ed5a 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4d 6f 76  rc;..}..../* Mov
1ed5b 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
1ed5c 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
1ed5d 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  in the table.  R
1ed5e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0d  eturn SQLITE_OK.
1ed5f 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  .** on success. 
1ed60 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20   Set *pRes to 0 
1ed61 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  if the cursor ac
1ed62 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f  tually points to
1ed63 20 73 6f 6d 65 74 68 69 6e 67 0d 0a 2a 2a 20 6f   something..** o
1ed64 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31  r set *pRes to 1
1ed65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
1ed66 20 65 6d 70 74 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c   empty...*/..SQL
1ed67 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1ed68 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
1ed69 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
1ed6a 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0d 0a 20  , int *pRes){.. 
1ed6b 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 61 73   int rc;....  as
1ed6c 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1ed6d 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0d  sMutex(pCur) );.
1ed6e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1ed6f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1ed70 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  ur->pBtree->db->
1ed71 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 72 63 20  mutex) );..  rc 
1ed72 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75  = moveToRoot(pCu
1ed73 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  r);..  if( rc==S
1ed74 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1ed75 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
1ed76 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
1ed77 44 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  D ){..      asse
1ed78 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  rt( pCur->pgnoRo
1ed79 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61  ot==0 || pCur->a
1ed7a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1ed7b 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0d  e]->nCell==0 );.
1ed7c 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31  .      *pRes = 1
1ed7d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
1ed7e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
1ed7f 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1ed80 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20  iPage]->nCell>0 
1ed81 29 3b 0d 0a 20 20 20 20 20 20 2a 70 52 65 73 20  );..      *pRes 
1ed82 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  = 0;..      rc =
1ed83 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28   moveToLeftmost(
1ed84 70 43 75 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  pCur);..    }.. 
1ed85 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
1ed86 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4d 6f 76 65 20 74  ..}..../* Move t
1ed87 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
1ed88 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74   last entry in t
1ed89 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
1ed8a 6e 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a 2a 2a 20  n SQLITE_OK..** 
1ed8b 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74  on success.  Set
1ed8c 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74   *pRes to 0 if t
1ed8d 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c  he cursor actual
1ed8e 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d  ly points to som
1ed8f 65 74 68 69 6e 67 0d 0a 2a 2a 20 6f 72 20 73 65  ething..** or se
1ed90 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20  t *pRes to 1 if 
1ed91 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1ed92 74 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ty...*/..SQLITE_
1ed93 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1ed94 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43  te3BtreeLast(BtC
1ed95 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
1ed96 20 2a 70 52 65 73 29 7b 0d 0a 20 20 69 6e 74 20   *pRes){..  int 
1ed97 72 63 3b 0d 0a 20 0d 0a 20 20 61 73 73 65 72 74  rc;.. ..  assert
1ed98 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1ed99 65 78 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 61  ex(pCur) );..  a
1ed9a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1ed9b 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e  utex_held(pCur->
1ed9c 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65  pBtree->db->mute
1ed9d 78 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  x) );....  /* If
1ed9e 20 74 68 65 20 63 75 72 73 6f 72 20 61 6c 72 65   the cursor alre
1ed9f 61 64 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ady points to th
1eda0 65 20 6c 61 73 74 20 65 6e 74 72 79 2c 20 74 68  e last entry, th
1eda1 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a  is is a no-op. *
1eda2 2f 0d 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f  /..  if( CURSOR_
1eda3 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74  VALID==pCur->eSt
1eda4 61 74 65 20 26 26 20 70 43 75 72 2d 3e 61 74 4c  ate && pCur->atL
1eda5 61 73 74 20 29 7b 0d 0a 23 69 66 64 65 66 20 53  ast ){..#ifdef S
1eda6 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 20  QLITE_DEBUG..   
1eda7 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73   /* This block s
1eda8 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 74 28  erves to assert(
1eda9 29 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  ) that the curso
1edaa 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f  r really does po
1edab 69 6e 74 20 0d 0a 20 20 20 20 2a 2a 20 74 6f 20  int ..    ** to 
1edac 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1edad 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f  n the b-tree. */
1edae 0d 0a 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20  ..    int ii;.. 
1edaf 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
1edb0 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b  pCur->iPage; ii+
1edb1 2b 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  +){..      asser
1edb2 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  t( pCur->aiIdx[i
1edb3 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65  i]==pCur->apPage
1edb4 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0d 0a  [ii]->nCell );..
1edb5 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65 72      }..    asser
1edb6 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
1edb7 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75  Cur->iPage]==pCu
1edb8 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1edb9 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20  iPage]->nCell-1 
1edba 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
1edbb 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1edbc 72 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20  r->iPage]->leaf 
1edbd 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  );..#endif..    
1edbe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1edbf 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20 3d  ;..  }....  rc =
1edc0 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
1edc1 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
1edc2 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1edc3 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
1edc4 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
1edc5 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   ){..      asser
1edc6 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t( pCur->pgnoRoo
1edc7 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70  t==0 || pCur->ap
1edc8 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1edc9 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0d 0a  ]->nCell==0 );..
1edca 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
1edcb 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
1edcc 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
1edcd 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1edce 5f 56 41 4c 49 44 20 29 3b 0d 0a 20 20 20 20 20  _VALID );..     
1edcf 20 2a 70 52 65 73 20 3d 20 30 3b 0d 0a 20 20 20   *pRes = 0;..   
1edd0 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69     rc = moveToRi
1edd1 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0d 0a  ghtmost(pCur);..
1edd2 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c 61        pCur->atLa
1edd3 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f  st = rc==SQLITE_
1edd4 4f 4b 20 3f 31 3a 30 3b 0d 0a 20 20 20 20 7d 0d  OK ?1:0;..    }.
1edd5 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
1edd6 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4d 6f 76 65  c;..}..../* Move
1edd7 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74   the cursor so t
1edd8 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
1edd9 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 74   an entry near t
1edda 68 65 20 6b 65 79 20 0d 0a 2a 2a 20 73 70 65 63  he key ..** spec
1eddb 69 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 79  ified by pIdxKey
1eddc 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 65   or intKey.   Re
1eddd 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63  turn a success c
1edde 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72  ode...**..** For
1eddf 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20   INTKEY tables, 
1ede0 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d  the intKey param
1ede1 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 70  eter is used.  p
1ede2 49 64 78 4b 65 79 20 0d 0a 2a 2a 20 6d 75 73 74  IdxKey ..** must
1ede3 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69   be NULL.  For i
1ede4 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64  ndex tables, pId
1ede5 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64  xKey is used and
1ede6 20 69 6e 74 4b 65 79 0d 0a 2a 2a 20 69 73 20 69   intKey..** is i
1ede7 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  gnored...**..** 
1ede8 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63  If an exact matc
1ede9 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20  h is not found, 
1edea 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
1edeb 69 73 20 61 6c 77 61 79 73 0d 0a 2a 2a 20 6c 65  is always..** le
1edec 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
1eded 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
1edee 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20   would hold the 
1edef 65 6e 74 72 79 20 69 66 20 69 74 0d 0a 2a 2a 20  entry if it..** 
1edf0 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54  were present.  T
1edf1 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20  he cursor might 
1edf2 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72  point to an entr
1edf3 79 20 74 68 61 74 20 63 6f 6d 65 73 0d 0a 2a 2a  y that comes..**
1edf4 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72   before or after
1edf5 20 74 68 65 20 6b 65 79 2e 0d 0a 2a 2a 0d 0a 2a   the key...**..*
1edf6 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20  * An integer is 
1edf7 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 52  written into *pR
1edf8 65 73 20 77 68 69 63 68 20 69 73 20 74 68 65 20  es which is the 
1edf9 72 65 73 75 6c 74 20 6f 66 0d 0a 2a 2a 20 63 6f  result of..** co
1edfa 6d 70 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20  mparing the key 
1edfb 77 69 74 68 20 74 68 65 20 65 6e 74 72 79 20 74  with the entry t
1edfc 6f 20 77 68 69 63 68 20 74 68 65 20 63 75 72 73  o which the curs
1edfd 6f 72 20 69 73 20 0d 0a 2a 2a 20 70 6f 69 6e 74  or is ..** point
1edfe 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e  ing.  The meanin
1edff 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  g of the integer
1ee00 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0d 0a 2a   written into..*
1ee01 2a 20 2a 70 52 65 73 20 69 73 20 61 73 20 66 6f  * *pRes is as fo
1ee02 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  llows:..**..**  
1ee03 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20     *pRes<0      
1ee04 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
1ee05 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
1ee06 6e 20 65 6e 74 72 79 20 74 68 61 74 0d 0a 2a 2a  n entry that..**
1ee07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee08 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61    is smaller tha
1ee09 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79  n intKey/pIdxKey
1ee0a 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65   or if the table
1ee0b 20 69 73 20 65 6d 70 74 79 0d 0a 2a 2a 20 20 20   is empty..**   
1ee0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
1ee0d 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  nd the cursor is
1ee0e 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20   therefore left 
1ee0f 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67  point to nothing
1ee10 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 70  ...**..**     *p
1ee11 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63  Res==0     The c
1ee12 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
1ee13 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74  inting at an ent
1ee14 72 79 20 74 68 61 74 0d 0a 2a 2a 20 20 20 20 20  ry that..**     
1ee15 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61               exa
1ee16 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74  ctly matches int
1ee17 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0d 0a 2a 2a  Key/pIdxKey...**
1ee18 0d 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30  ..**     *pRes>0
1ee19 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72        The cursor
1ee1a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1ee1b 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
1ee1c 61 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  at..**          
1ee1d 20 20 20 20 20 20 20 20 69 73 20 6c 61 72 67 65          is large
1ee1e 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49  r than intKey/pI
1ee1f 64 78 4b 65 79 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a  dxKey...**..*/..
1ee20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1ee21 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
1ee22 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0d 0a  ovetoUnpacked(..
1ee23 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
1ee24 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
1ee25 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d  e cursor to be m
1ee26 6f 76 65 64 20 2a 2f 0d 0a 20 20 55 6e 70 61 63  oved */..  Unpac
1ee27 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b  kedRecord *pIdxK
1ee28 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ey, /* Unpacked 
1ee29 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0d 0a 20 20  index key */..  
1ee2a 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20  i64 intKey,     
1ee2b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1ee2c 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0d 0a 20 20  table key */..  
1ee2d 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c 20 20  int biasRight,  
1ee2e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
1ee2f 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 73 65  rue, bias the se
1ee30 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68  arch to the high
1ee31 20 65 6e 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a   end */..  int *
1ee32 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 20  pRes            
1ee33 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61      /* Write sea
1ee34 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65  rch results here
1ee35 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
1ee36 63 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  c;....  assert( 
1ee37 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1ee38 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 61 73 73  (pCur) );..  ass
1ee39 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1ee3a 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1ee3b 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1ee3c 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
1ee3d 52 65 73 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  Res );..  assert
1ee3e 28 20 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d  ( (pIdxKey==0)==
1ee3f 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d  (pCur->pKeyInfo=
1ee40 3d 30 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  =0) );....  /* I
1ee41 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
1ee42 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e  already position
1ee43 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20  ed at the point 
1ee44 77 65 20 61 72 65 20 74 72 79 69 6e 67 0d 0a 20  we are trying.. 
1ee45 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20   ** to move to, 
1ee46 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e  then just return
1ee47 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61   without doing a
1ee48 6e 79 20 77 6f 72 6b 20 2a 2f 0d 0a 20 20 69 66  ny work */..  if
1ee49 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
1ee4a 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20  CURSOR_VALID && 
1ee4b 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1ee4c 0d 0a 20 20 20 26 26 20 70 43 75 72 2d 3e 61 70  ..   && pCur->ap
1ee4d 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
1ee4e 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  ..  ){..    if( 
1ee4f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d  pCur->info.nKey=
1ee50 3d 69 6e 74 4b 65 79 20 29 7b 0d 0a 20 20 20 20  =intKey ){..    
1ee51 20 20 2a 70 52 65 73 20 3d 20 30 3b 0d 0a 20 20    *pRes = 0;..  
1ee52 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ee53 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  E_OK;..    }..  
1ee54 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61    if( pCur->atLa
1ee55 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f  st && pCur->info
1ee56 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0d  .nKey<intKey ){.
1ee57 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d  .      *pRes = -
1ee58 31 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  1;..      return
1ee59 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20   SQLITE_OK;..   
1ee5a 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20   }..  }....  rc 
1ee5b 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75  = moveToRoot(pCu
1ee5c 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 7b  r);..  if( rc ){
1ee5d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ..    return rc;
1ee5e 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
1ee5f 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d   pCur->pgnoRoot=
1ee60 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61  =0 || pCur->apPa
1ee61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  ge[pCur->iPage] 
1ee62 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43  );..  assert( pC
1ee63 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20  ur->pgnoRoot==0 
1ee64 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  || pCur->apPage[
1ee65 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73  pCur->iPage]->is
1ee66 49 6e 69 74 20 29 3b 0d 0a 20 20 61 73 73 65 72  Init );..  asser
1ee67 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
1ee68 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
1ee69 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  || pCur->apPage[
1ee6a 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43  pCur->iPage]->nC
1ee6b 65 6c 6c 3e 30 20 29 3b 0d 0a 20 20 69 66 28 20  ell>0 );..  if( 
1ee6c 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1ee6d 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0d  RSOR_INVALID ){.
1ee6e 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b  .    *pRes = -1;
1ee6f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ..    assert( pC
1ee70 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20  ur->pgnoRoot==0 
1ee71 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  || pCur->apPage[
1ee72 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43  pCur->iPage]->nC
1ee73 65 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 72  ell==0 );..    r
1ee74 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1ee75 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
1ee76 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1ee77 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78  ->intKey || pIdx
1ee78 4b 65 79 20 29 3b 0d 0a 20 20 66 6f 72 28 3b 3b  Key );..  for(;;
1ee79 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c  ){..    int lwr,
1ee7a 20 75 70 72 2c 20 69 64 78 3b 0d 0a 20 20 20 20   upr, idx;..    
1ee7b 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0d 0a 20 20  Pgno chldPg;..  
1ee7c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1ee7d 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1ee7e 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20  pCur->iPage];.. 
1ee7f 20 20 20 69 6e 74 20 63 3b 0d 0a 0d 0a 20 20 20     int c;....   
1ee80 20 2f 2a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c   /* pPage->nCell
1ee81 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72   must be greater
1ee82 20 74 68 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74   than zero. If t
1ee83 68 69 73 20 69 73 20 74 68 65 20 72 6f 6f 74 2d  his is the root-
1ee84 70 61 67 65 0d 0a 20 20 20 20 2a 2a 20 74 68 65  page..    ** the
1ee85 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 68 61   cursor would ha
1ee86 76 65 20 62 65 65 6e 20 49 4e 56 41 4c 49 44 20  ve been INVALID 
1ee87 61 62 6f 76 65 20 61 6e 64 20 74 68 69 73 20 66  above and this f
1ee88 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0d 0a 20 20 20  or(;;) loop..   
1ee89 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 49 66 20   ** not run. If 
1ee8a 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20  this is not the 
1ee8b 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 65 6e 20  root-page, then 
1ee8c 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  the moveToChild(
1ee8d 29 20 72 6f 75 74 69 6e 65 0d 0a 20 20 20 20 2a  ) routine..    *
1ee8e 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 6c 72  * would have alr
1ee8f 65 61 64 79 20 64 65 74 65 63 74 65 64 20 64 62  eady detected db
1ee90 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 69 6d   corruption. Sim
1ee91 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 6d 75  ilarly, pPage mu
1ee92 73 74 0d 0a 20 20 20 20 2a 2a 20 62 65 20 74 68  st..    ** be th
1ee93 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 6e  e right kind (in
1ee94 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f 66  dex or table) of
1ee95 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f 74   b-tree page. Ot
1ee96 68 65 72 77 69 73 65 0d 0a 20 20 20 20 2a 2a 20  herwise..    ** 
1ee97 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20  a moveToChild() 
1ee98 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20  or moveToRoot() 
1ee99 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20  call would have 
1ee9a 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74  detected corrupt
1ee9b 69 6f 6e 2e 20 20 2a 2f 0d 0a 20 20 20 20 61 73  ion.  */..    as
1ee9c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65  sert( pPage->nCe
1ee9d 6c 6c 3e 30 20 29 3b 0d 0a 20 20 20 20 61 73 73  ll>0 );..    ass
1ee9e 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b  ert( pPage->intK
1ee9f 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29  ey==(pIdxKey==0)
1eea0 20 29 3b 0d 0a 20 20 20 20 6c 77 72 20 3d 20 30   );..    lwr = 0
1eea1 3b 0d 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61  ;..    upr = pPa
1eea2 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0d 0a 20 20  ge->nCell-1;..  
1eea3 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20    if( biasRight 
1eea4 29 7b 0d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  ){..      pCur->
1eea5 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1eea6 65 5d 20 3d 20 28 75 31 36 29 28 69 64 78 20 3d  e] = (u16)(idx =
1eea7 20 75 70 72 29 3b 0d 0a 20 20 20 20 7d 65 6c 73   upr);..    }els
1eea8 65 7b 0d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  e{..      pCur->
1eea9 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1eeaa 65 5d 20 3d 20 28 75 31 36 29 28 69 64 78 20 3d  e] = (u16)(idx =
1eeab 20 28 75 70 72 2b 6c 77 72 29 2f 32 29 3b 0d 0a   (upr+lwr)/2);..
1eeac 20 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72 28 3b      }..    for(;
1eead 3b 29 7b 0d 0a 20 20 20 20 20 20 75 38 20 2a 70  ;){..      u8 *p
1eeae 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
1eeaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1eeb0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72  * Pointer to cur
1eeb1 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61  rent cell in pPa
1eeb2 67 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 61  ge */....      a
1eeb3 73 73 65 72 74 28 20 69 64 78 3d 3d 70 43 75 72  ssert( idx==pCur
1eeb4 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
1eeb5 61 67 65 5d 20 29 3b 0d 0a 20 20 20 20 20 20 70  age] );..      p
1eeb6 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
1eeb7 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 43 65 6c  = 0;..      pCel
1eeb8 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
1eeb9 67 65 2c 20 69 64 78 29 20 2b 20 70 50 61 67 65  ge, idx) + pPage
1eeba 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0d  ->childPtrSize;.
1eebb 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
1eebc 2d 3e 69 6e 74 4b 65 79 20 29 7b 0d 0a 20 20 20  ->intKey ){..   
1eebd 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65       i64 nCellKe
1eebe 79 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  y;..        if( 
1eebf 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
1eec0 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  {..          u32
1eec1 20 64 75 6d 6d 79 3b 0d 0a 20 20 20 20 20 20 20   dummy;..       
1eec2 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56     pCell += getV
1eec3 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64  arint32(pCell, d
1eec4 75 6d 6d 79 29 3b 0d 0a 20 20 20 20 20 20 20 20  ummy);..        
1eec5 7d 0d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61  }..        getVa
1eec6 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34  rint(pCell, (u64
1eec7 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0d 0a 20  *)&nCellKey);.. 
1eec8 20 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c         if( nCell
1eec9 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0d 0a  Key==intKey ){..
1eeca 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b            c = 0;
1eecb 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ..        }else 
1eecc 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74  if( nCellKey<int
1eecd 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Key ){..        
1eece 20 20 63 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 20    c = -1;..     
1eecf 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1eed0 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65       assert( nCe
1eed1 6c 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 3b 0d  llKey>intKey );.
1eed2 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2b  .          c = +
1eed3 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  1;..        }.. 
1eed4 20 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c         pCur->val
1eed5 69 64 4e 4b 65 79 20 3d 20 31 3b 0d 0a 20 20 20  idNKey = 1;..   
1eed6 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e       pCur->info.
1eed7 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b  nKey = nCellKey;
1eed8 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
1eed9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
1eeda 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64  aximum supported
1eedb 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 36 35   page-size is 65
1eedc 35 33 36 20 62 79 74 65 73 2e 20 54 68 69 73 20  536 bytes. This 
1eedd 6d 65 61 6e 73 20 74 68 61 74 0d 0a 20 20 20 20  means that..    
1eede 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d      ** the maxim
1eedf 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63  um number of rec
1eee0 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64  ord bytes stored
1eee1 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54   on an index B-T
1eee2 72 65 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ree..        ** 
1eee3 70 61 67 65 20 69 73 20 6c 65 73 73 20 74 68 61  page is less tha
1eee4 6e 20 31 36 33 38 34 20 62 79 74 65 73 20 61 6e  n 16384 bytes an
1eee5 64 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20  d may be stored 
1eee6 61 73 20 61 20 32 2d 62 79 74 65 0d 0a 20 20 20  as a 2-byte..   
1eee7 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20       ** varint. 
1eee8 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
1eee9 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65   is used to atte
1eeea 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72  mpt to avoid par
1eeeb 73 69 6e 67 20 0d 0a 20 20 20 20 20 20 20 20 2a  sing ..        *
1eeec 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c  * the entire cel
1eeed 6c 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f  l by checking fo
1eeee 72 20 74 68 65 20 63 61 73 65 73 20 77 68 65 72  r the cases wher
1eeef 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20  e the record is 
1eef0 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f  ..        ** sto
1eef1 72 65 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74  red entirely wit
1eef2 68 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70  hin the b-tree p
1eef3 61 67 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e  age by inspectin
1eef4 67 20 74 68 65 20 66 69 72 73 74 20 0d 0a 20 20  g the first ..  
1eef5 20 20 20 20 20 20 2a 2a 20 32 20 62 79 74 65 73        ** 2 bytes
1eef6 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0d 0a 20   of the cell... 
1eef7 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
1eef8 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 70     int nCell = p
1eef9 43 65 6c 6c 5b 30 5d 3b 0d 0a 20 20 20 20 20 20  Cell[0];..      
1eefa 20 20 69 66 28 20 6e 43 65 6c 6c 3c 3d 70 50 61    if( nCell<=pPa
1eefb 67 65 2d 3e 6d 61 78 31 62 79 74 65 50 61 79 6c  ge->max1bytePayl
1eefc 6f 61 64 0d 0a 20 20 20 20 20 20 20 20 20 2f 2a  oad..         /*
1eefd 20 26 26 20 28 70 43 65 6c 6c 2b 6e 43 65 6c 6c   && (pCell+nCell
1eefe 29 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e  )<pPage->aDataEn
1eeff 64 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 29 7b  d */..        ){
1ef00 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ..          /* T
1ef01 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20  his branch runs 
1ef02 69 66 20 74 68 65 20 72 65 63 6f 72 64 2d 73 69  if the record-si
1ef03 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ze field of the 
1ef04 63 65 6c 6c 20 69 73 20 61 0d 0a 20 20 20 20 20  cell is a..     
1ef05 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 62       ** single b
1ef06 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 74  yte varint and t
1ef07 68 65 20 72 65 63 6f 72 64 20 66 69 74 73 20 65  he record fits e
1ef08 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d  ntirely on the m
1ef09 61 69 6e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  ain..          *
1ef0a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20  * b-tree page.  
1ef0b 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 74 65  */..          te
1ef0c 73 74 63 61 73 65 28 20 70 43 65 6c 6c 2b 6e 43  stcase( pCell+nC
1ef0d 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 2d 3e 61 44  ell+1==pPage->aD
1ef0e 61 74 61 45 6e 64 20 29 3b 0d 0a 20 20 20 20 20  ataEnd );..     
1ef0f 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
1ef10 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
1ef11 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29  e(nCell, (void*)
1ef12 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b  &pCell[1], pIdxK
1ef13 65 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  ey);..        }e
1ef14 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b  lse if( !(pCell[
1ef15 31 5d 20 26 20 30 78 38 30 29 20 0d 0a 20 20 20  1] & 0x80) ..   
1ef16 20 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c 6c         && (nCell
1ef17 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 29   = ((nCell&0x7f)
1ef18 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29  <<7) + pCell[1])
1ef19 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  <=pPage->maxLoca
1ef1a 6c 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l..          /* 
1ef1b 26 26 20 28 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b  && (pCell+nCell+
1ef1c 32 29 3c 3d 70 50 61 67 65 2d 3e 61 44 61 74 61  2)<=pPage->aData
1ef1d 45 6e 64 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  End */..        
1ef1e 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ){..          /*
1ef1f 20 54 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65   The record-size
1ef20 20 66 69 65 6c 64 20 69 73 20 61 20 32 20 62 79   field is a 2 by
1ef21 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68  te varint and th
1ef22 65 20 72 65 63 6f 72 64 20 0d 0a 20 20 20 20 20  e record ..     
1ef23 20 20 20 20 20 2a 2a 20 66 69 74 73 20 65 6e 74       ** fits ent
1ef24 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69  irely on the mai
1ef25 6e 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20  n b-tree page.  
1ef26 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 74 65  */..          te
1ef27 73 74 63 61 73 65 28 20 70 43 65 6c 6c 2b 6e 43  stcase( pCell+nC
1ef28 65 6c 6c 2b 32 3d 3d 70 50 61 67 65 2d 3e 61 44  ell+2==pPage->aD
1ef29 61 74 61 45 6e 64 20 29 3b 0d 0a 20 20 20 20 20  ataEnd );..     
1ef2a 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
1ef2b 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
1ef2c 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29  e(nCell, (void*)
1ef2d 26 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b  &pCell[2], pIdxK
1ef2e 65 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  ey);..        }e
1ef2f 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
1ef30 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 66 6c  /* The record fl
1ef31 6f 77 73 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e  ows over onto on
1ef32 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c  e or more overfl
1ef33 6f 77 20 70 61 67 65 73 2e 20 49 6e 0d 0a 20 20  ow pages. In..  
1ef34 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20          ** this 
1ef35 63 61 73 65 20 74 68 65 20 77 68 6f 6c 65 20 63  case the whole c
1ef36 65 6c 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ell needs to be 
1ef37 70 61 72 73 65 64 2c 20 61 20 62 75 66 66 65 72  parsed, a buffer
1ef38 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 20 20 20 20   allocated..    
1ef39 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 63 63        ** and acc
1ef3a 65 73 73 50 61 79 6c 6f 61 64 28 29 20 75 73 65  essPayload() use
1ef3b 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  d to retrieve th
1ef3c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  e record into th
1ef3d 65 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e..          ** 
1ef3e 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 56 64  buffer before Vd
1ef3f 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
1ef40 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e  ) can be called.
1ef41 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 76   */..          v
1ef42 6f 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0d 0a  oid *pCellKey;..
1ef43 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 20 63            u8 * c
1ef44 6f 6e 73 74 20 70 43 65 6c 6c 42 6f 64 79 20 3d  onst pCellBody =
1ef45 20 70 43 65 6c 6c 20 2d 20 70 50 61 67 65 2d 3e   pCell - pPage->
1ef46 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0d 0a 20  childPtrSize;.. 
1ef47 20 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61           btreePa
1ef48 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
1ef49 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43  , pCellBody, &pC
1ef4a 75 72 2d 3e 69 6e 66 6f 29 3b 0d 0a 20 20 20 20  ur->info);..    
1ef4b 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 28 69        nCell = (i
1ef4c 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
1ef4d 65 79 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  ey;..          p
1ef4e 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65  CellKey = sqlite
1ef4f 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 29  3Malloc( nCell )
1ef50 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
1ef51 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0d   pCellKey==0 ){.
1ef52 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
1ef53 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
1ef54 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
1ef55 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1ef56 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
1ef57 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 63           rc = ac
1ef58 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72  cessPayload(pCur
1ef59 2c 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73  , 0, nCell, (uns
1ef5a 69 67 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c  igned char*)pCel
1ef5b 6c 4b 65 79 2c 20 30 29 3b 0d 0a 20 20 20 20 20  lKey, 0);..     
1ef5c 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d 0a       if( rc ){..
1ef5d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1ef5e 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65  te3_free(pCellKe
1ef5f 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  y);..           
1ef60 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
1ef61 69 73 68 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ish;..          
1ef62 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  }..          c =
1ef63 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
1ef64 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c  rdCompare(nCell,
1ef65 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b   pCellKey, pIdxK
1ef66 65 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ey);..          
1ef67 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65  sqlite3_free(pCe
1ef68 6c 6c 4b 65 79 29 3b 0d 0a 20 20 20 20 20 20 20  llKey);..       
1ef69 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
1ef6a 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0d 0a     if( c==0 ){..
1ef6b 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
1ef6c 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50  e->intKey && !pP
1ef6d 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20 20  age->leaf ){..  
1ef6e 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64          lwr = id
1ef6f 78 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72  x;..          br
1ef70 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  eak;..        }e
1ef71 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
1ef72 2a 70 52 65 73 20 3d 20 30 3b 0d 0a 20 20 20 20  *pRes = 0;..    
1ef73 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1ef74 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 20  E_OK;..         
1ef75 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
1ef76 69 73 68 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  ish;..        }.
1ef77 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1ef78 69 66 28 20 63 3c 30 20 29 7b 0d 0a 20 20 20 20  if( c<0 ){..    
1ef79 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b      lwr = idx+1;
1ef7a 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
1ef7b 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64          upr = id
1ef7c 78 2d 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  x-1;..      }.. 
1ef7d 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72       if( lwr>upr
1ef7e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 62 72 65   ){..        bre
1ef7f 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
1ef80 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
1ef81 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28  pCur->iPage] = (
1ef82 75 31 36 29 28 69 64 78 20 3d 20 28 6c 77 72 2b  u16)(idx = (lwr+
1ef83 75 70 72 29 2f 32 29 3b 0d 0a 20 20 20 20 7d 0d  upr)/2);..    }.
1ef84 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72  .    assert( lwr
1ef85 3d 3d 75 70 72 2b 31 20 7c 7c 20 28 70 50 61 67  ==upr+1 || (pPag
1ef86 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50  e->intKey && !pP
1ef87 61 67 65 2d 3e 6c 65 61 66 29 20 29 3b 0d 0a 20  age->leaf) );.. 
1ef88 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
1ef89 2d 3e 69 73 49 6e 69 74 20 29 3b 0d 0a 20 20 20  ->isInit );..   
1ef8a 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
1ef8b 20 29 7b 0d 0a 20 20 20 20 20 20 63 68 6c 64 50   ){..      chldP
1ef8c 67 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73  g = 0;..    }els
1ef8d 65 20 69 66 28 20 6c 77 72 3e 3d 70 50 61 67 65  e if( lwr>=pPage
1ef8e 2d 3e 6e 43 65 6c 6c 20 29 7b 0d 0a 20 20 20 20  ->nCell ){..    
1ef8f 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62    chldPg = get4b
1ef90 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
1ef91 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
1ef92 65 74 2b 38 5d 29 3b 0d 0a 20 20 20 20 7d 65 6c  et+8]);..    }el
1ef93 73 65 7b 0d 0a 20 20 20 20 20 20 63 68 6c 64 50  se{..      chldP
1ef94 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e  g = get4byte(fin
1ef95 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72  dCell(pPage, lwr
1ef96 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ));..    }..    
1ef97 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b  if( chldPg==0 ){
1ef98 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
1ef99 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
1ef9a 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61  ->iPage]<pCur->a
1ef9b 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1ef9c 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 20 20  e]->nCell );..  
1ef9d 20 20 20 20 2a 70 52 65 73 20 3d 20 63 3b 0d 0a      *pRes = c;..
1ef9e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1ef9f 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 67 6f 74  E_OK;..      got
1efa0 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1efa1 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 43 75  ..    }..    pCu
1efa2 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1efa3 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72  Page] = (u16)lwr
1efa4 3b 0d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  ;..    pCur->inf
1efa5 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0d 0a 20 20  o.nSize = 0;..  
1efa6 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
1efa7 79 20 3d 20 30 3b 0d 0a 20 20 20 20 72 63 20 3d  y = 0;..    rc =
1efa8 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
1efa9 72 2c 20 63 68 6c 64 50 67 29 3b 0d 0a 20 20 20  r, chldPg);..   
1efaa 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d   if( rc ) goto m
1efab 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0d 0a 20  oveto_finish;.. 
1efac 20 7d 0d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73   }..moveto_finis
1efad 68 3a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  h:..  return rc;
1efae 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
1efaf 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
1efb0 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
1efb1 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20   pointing at an 
1efb2 65 6e 74 72 79 20 6f 66 20 74 68 65 20 74 61 62  entry of the tab
1efb3 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 52 55 45  le...**..** TRUE
1efb4 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
1efb5 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74  d after a call t
1efb6 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  o sqlite3BtreeNe
1efb7 78 74 28 29 20 6d 6f 76 65 73 0d 0a 2a 2a 20 70  xt() moves..** p
1efb8 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74  ast the last ent
1efb9 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ry in the table 
1efba 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  or sqlite3BtreeP
1efbb 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74  rev() moves past
1efbc 0d 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65  ..** the first e
1efbd 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61  ntry.  TRUE is a
1efbe 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20  lso returned if 
1efbf 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1efc0 74 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ty...*/..SQLITE_
1efc1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1efc2 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75  te3BtreeEof(BtCu
1efc3 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d 0a 20 20  rsor *pCur){..  
1efc4 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66  /* TODO: What if
1efc5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69   the cursor is i
1efc6 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  n CURSOR_REQUIRE
1efc7 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62  SEEK but all tab
1efc8 6c 65 20 65 6e 74 72 69 65 73 0d 0a 20 20 2a 2a  le entries..  **
1efc9 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74   have been delet
1efca 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c  ed? This API wil
1efcb 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65  l need to change
1efcc 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72   to return an er
1efcd 72 6f 72 20 63 6f 64 65 0d 0a 20 20 2a 2a 20 61  ror code..  ** a
1efce 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f  s well as the bo
1efcf 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c  olean result val
1efd0 75 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 72 65 74  ue...  */..  ret
1efd1 75 72 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49  urn (CURSOR_VALI
1efd2 44 21 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29  D!=pCur->eState)
1efd3 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ;..}..../*..** A
1efd4 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f  dvance the curso
1efd5 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  r to the next en
1efd6 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1efd7 61 73 65 2e 20 20 49 66 0d 0a 2a 2a 20 73 75 63  ase.  If..** suc
1efd8 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74  cessful then set
1efd9 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68   *pRes=0.  If th
1efda 65 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 77 61 73  e cursor..** was
1efdb 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e   already pointin
1efdc 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  g to the last en
1efdd 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1efde 61 73 65 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 74  ase before..** t
1efdf 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
1efe0 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74  called, then set
1efe1 20 2a 70 52 65 73 3d 31 2e 0d 0a 2a 2f 0d 0a 53   *pRes=1...*/..S
1efe2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1efe3 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  t sqlite3BtreeNe
1efe4 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  xt(BtCursor *pCu
1efe5 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0d 0a  r, int *pRes){..
1efe6 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74    int rc;..  int
1efe7 20 69 64 78 3b 0d 0a 20 20 4d 65 6d 50 61 67 65   idx;..  MemPage
1efe8 20 2a 70 50 61 67 65 3b 0d 0a 0d 0a 20 20 61 73   *pPage;....  as
1efe9 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1efea 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0d  sMutex(pCur) );.
1efeb 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43  .  rc = restoreC
1efec 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
1efed 75 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d  ur);..  if( rc!=
1efee 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
1efef 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20    return rc;..  
1eff0 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 52 65  }..  assert( pRe
1eff1 73 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 43  s!=0 );..  if( C
1eff2 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
1eff3 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0d 0a  Cur->eState ){..
1eff4 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0d 0a      *pRes = 1;..
1eff5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1eff6 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  E_OK;..  }..  if
1eff7 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74  ( pCur->skipNext
1eff8 3e 30 20 29 7b 0d 0a 20 20 20 20 70 43 75 72 2d  >0 ){..    pCur-
1eff9 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0d 0a  >skipNext = 0;..
1effa 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0d 0a      *pRes = 0;..
1effb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1effc 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 70 43  E_OK;..  }..  pC
1effd 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30  ur->skipNext = 0
1effe 3b 0d 0a 0d 0a 20 20 70 50 61 67 65 20 3d 20 70  ;....  pPage = p
1efff 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1f000 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20 20 69 64 78  ->iPage];..  idx
1f001 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78   = ++pCur->aiIdx
1f002 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a  [pCur->iPage];..
1f003 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1f004 3e 69 73 49 6e 69 74 20 29 3b 0d 0a 0d 0a 20 20  >isInit );....  
1f005 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
1f006 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72 75  se file is corru
1f007 70 74 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62  pt, it is possib
1f008 6c 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65  le for the value
1f009 20 6f 66 20 69 64 78 20 0d 0a 20 20 2a 2a 20 74   of idx ..  ** t
1f00a 6f 20 62 65 20 69 6e 76 61 6c 69 64 20 68 65 72  o be invalid her
1f00b 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79  e. This can only
1f00c 20 6f 63 63 75 72 20 69 66 20 61 20 73 65 63 6f   occur if a seco
1f00d 6e 64 20 63 75 72 73 6f 72 20 6d 6f 64 69 66 69  nd cursor modifi
1f00e 65 73 0d 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  es..  ** the pag
1f00f 65 20 77 68 69 6c 65 20 63 75 72 73 6f 72 20 70  e while cursor p
1f010 43 75 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  Cur is holding a
1f011 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74   reference to it
1f012 2e 20 57 68 69 63 68 20 63 61 6e 0d 0a 20 20 2a  . Which can..  *
1f013 2a 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66  * only happen if
1f014 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
1f015 20 63 6f 72 72 75 70 74 20 69 6e 20 73 75 63 68   corrupt in such
1f016 20 61 20 77 61 79 20 61 73 20 74 6f 20 6c 69 6e   a way as to lin
1f017 6b 20 74 68 65 0d 0a 20 20 2a 2a 20 70 61 67 65  k the..  ** page
1f018 20 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20   into more than 
1f019 6f 6e 65 20 62 2d 74 72 65 65 20 73 74 72 75 63  one b-tree struc
1f01a 74 75 72 65 2e 20 2a 2f 0d 0a 20 20 74 65 73 74  ture. */..  test
1f01b 63 61 73 65 28 20 69 64 78 3e 70 50 61 67 65 2d  case( idx>pPage-
1f01c 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 0d 0a 20 20 70  >nCell );....  p
1f01d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
1f01e 3d 20 30 3b 0d 0a 20 20 70 43 75 72 2d 3e 76 61  = 0;..  pCur->va
1f01f 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0d 0a 20 20  lidNKey = 0;..  
1f020 69 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e  if( idx>=pPage->
1f021 6e 43 65 6c 6c 20 29 7b 0d 0a 20 20 20 20 69 66  nCell ){..    if
1f022 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1f023 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f  {..      rc = mo
1f024 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
1f025 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
1f026 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
1f027 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0d 0a 20  rOffset+8]));.. 
1f028 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
1f029 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20  turn rc;..      
1f02a 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  rc = moveToLeftm
1f02b 6f 73 74 28 70 43 75 72 29 3b 0d 0a 20 20 20 20  ost(pCur);..    
1f02c 20 20 2a 70 52 65 73 20 3d 20 30 3b 0d 0a 20 20    *pRes = 0;..  
1f02d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
1f02e 20 20 20 20 7d 0d 0a 20 20 20 20 64 6f 7b 0d 0a      }..    do{..
1f02f 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e        if( pCur->
1f030 69 50 61 67 65 3d 3d 30 20 29 7b 0d 0a 20 20 20  iPage==0 ){..   
1f031 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0d       *pRes = 1;.
1f032 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65  .        pCur->e
1f033 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
1f034 4e 56 41 4c 49 44 3b 0d 0a 20 20 20 20 20 20 20  NVALID;..       
1f035 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1f036 4b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  K;..      }..   
1f037 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28     moveToParent(
1f038 70 43 75 72 29 3b 0d 0a 20 20 20 20 20 20 70 50  pCur);..      pP
1f039 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
1f03a 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
1f03b 0d 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43  ..    }while( pC
1f03c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1f03d 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e  iPage]>=pPage->n
1f03e 43 65 6c 6c 20 29 3b 0d 0a 20 20 20 20 2a 70 52  Cell );..    *pR
1f03f 65 73 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28  es = 0;..    if(
1f040 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
1f041 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
1f042 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70  lite3BtreeNext(p
1f043 43 75 72 2c 20 70 52 65 73 29 3b 0d 0a 20 20 20  Cur, pRes);..   
1f044 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72   }else{..      r
1f045 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
1f046 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75 72      }..    retur
1f047 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70  n rc;..  }..  *p
1f048 52 65 73 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20  Res = 0;..  if( 
1f049 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a  pPage->leaf ){..
1f04a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1f04b 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63  E_OK;..  }..  rc
1f04c 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73   = moveToLeftmos
1f04d 74 28 70 43 75 72 29 3b 0d 0a 20 20 72 65 74 75  t(pCur);..  retu
1f04e 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  rn rc;..}....../
1f04f 2a 0d 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 63  *..** Step the c
1f050 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61 63  ursor to the bac
1f051 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75  k to the previou
1f052 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  s entry in the d
1f053 61 74 61 62 61 73 65 2e 20 20 49 66 0d 0a 2a 2a  atabase.  If..**
1f054 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e   successful then
1f055 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49   set *pRes=0.  I
1f056 66 20 74 68 65 20 63 75 72 73 6f 72 0d 0a 2a 2a  f the cursor..**
1f057 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69   was already poi
1f058 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72  nting to the fir
1f059 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
1f05a 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0d  database before.
1f05b 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
1f05c 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65   was called, the
1f05d 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0d 0a  n set *pRes=1...
1f05e 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
1f05f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1f060 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75  reePrevious(BtCu
1f061 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
1f062 2a 70 52 65 73 29 7b 0d 0a 20 20 69 6e 74 20 72  *pRes){..  int r
1f063 63 3b 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  c;..  MemPage *p
1f064 50 61 67 65 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  Page;....  asser
1f065 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
1f066 74 65 78 28 70 43 75 72 29 20 29 3b 0d 0a 20 20  tex(pCur) );..  
1f067 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
1f068 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
1f069 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
1f06a 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72  ITE_OK ){..    r
1f06b 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a  eturn rc;..  }..
1f06c 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d    pCur->atLast =
1f06d 20 30 3b 0d 0a 20 20 69 66 28 20 43 55 52 53 4f   0;..  if( CURSO
1f06e 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d  R_INVALID==pCur-
1f06f 3e 65 53 74 61 74 65 20 29 7b 0d 0a 20 20 20 20  >eState ){..    
1f070 2a 70 52 65 73 20 3d 20 31 3b 0d 0a 20 20 20 20  *pRes = 1;..    
1f071 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1f072 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 43  ;..  }..  if( pC
1f073 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29  ur->skipNext<0 )
1f074 7b 0d 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69  {..    pCur->ski
1f075 70 4e 65 78 74 20 3d 20 30 3b 0d 0a 20 20 20 20  pNext = 0;..    
1f076 2a 70 52 65 73 20 3d 20 30 3b 0d 0a 20 20 20 20  *pRes = 0;..    
1f077 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1f078 3b 0d 0a 20 20 7d 0d 0a 20 20 70 43 75 72 2d 3e  ;..  }..  pCur->
1f079 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0d 0a 0d  skipNext = 0;...
1f07a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
1f07b 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1f07c 61 67 65 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  age];..  assert(
1f07d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
1f07e 3b 0d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  ;..  if( !pPage-
1f07f 3e 6c 65 61 66 20 29 7b 0d 0a 20 20 20 20 69 6e  >leaf ){..    in
1f080 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69  t idx = pCur->ai
1f081 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
1f082 3b 0d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  ;..    rc = move
1f083 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65  ToChild(pCur, ge
1f084 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
1f085 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b 0d 0a  pPage, idx)));..
1f086 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20      if( rc ){.. 
1f087 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d       return rc;.
1f088 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
1f089 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
1f08a 28 70 43 75 72 29 3b 0d 0a 20 20 7d 65 6c 73 65  (pCur);..  }else
1f08b 7b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43  {..    while( pC
1f08c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1f08d 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0d 0a 20 20  iPage]==0 ){..  
1f08e 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50      if( pCur->iP
1f08f 61 67 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  age==0 ){..     
1f090 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
1f091 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1f092 3b 0d 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73  ;..        *pRes
1f093 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 72   = 1;..        r
1f094 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1f095 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
1f096 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43   moveToParent(pC
1f097 75 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ur);..    }..   
1f098 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
1f099 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70 43 75 72  e = 0;..    pCur
1f09a 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
1f09b 0d 0a 0d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69  ....    pCur->ai
1f09c 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
1f09d 2d 2d 3b 0d 0a 20 20 20 20 70 50 61 67 65 20 3d  --;..    pPage =
1f09e 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1f09f 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20 20 20  ur->iPage];..   
1f0a0 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
1f0a1 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65  ey && !pPage->le
1f0a2 61 66 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  af ){..      rc 
1f0a3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  = sqlite3BtreePr
1f0a4 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65  evious(pCur, pRe
1f0a5 73 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  s);..    }else{.
1f0a6 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1f0a7 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
1f0a8 20 7d 0d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b   }..  *pRes = 0;
1f0a9 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
1f0aa 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f  }..../*..** Allo
1f0ab 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
1f0ac 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1f0ad 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  e file...**..** 
1f0ae 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 73 20  The new page is 
1f0af 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e  marked as dirty.
1f0b0 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64    (In other word
1f0b1 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  s, sqlite3PagerW
1f0b2 72 69 74 65 28 29 0d 0a 2a 2a 20 68 61 73 20 61  rite()..** has a
1f0b3 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c  lready been call
1f0b4 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61  ed on the new pa
1f0b5 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 61  ge.)  The new pa
1f0b6 67 65 20 68 61 73 20 61 6c 73 6f 0d 0a 2a 2a 20  ge has also..** 
1f0b7 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20  been referenced 
1f0b8 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  and the calling 
1f0b9 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f  routine is respo
1f0ba 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69  nsible for calli
1f0bb 6e 67 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  ng..** sqlite3Pa
1f0bc 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68  gerUnref() on th
1f0bd 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20  e new page when 
1f0be 69 74 20 69 73 20 64 6f 6e 65 2e 0d 0a 2a 2a 0d  it is done...**.
1f0bf 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
1f0c0 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63   returned on suc
1f0c1 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72  cess.  Any other
1f0c2 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e   return value in
1f0c3 64 69 63 61 74 65 73 0d 0a 2a 2a 20 61 6e 20 65  dicates..** an e
1f0c4 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61  rror.  *ppPage a
1f0c5 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e  nd *pPgno are un
1f0c6 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65  defined in the e
1f0c7 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  vent of an error
1f0c8 2e 0d 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76  ...** Do not inv
1f0c9 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  oke sqlite3Pager
1f0ca 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61  Unref() on *ppPa
1f0cb 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69  ge if an error i
1f0cc 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d  s returned...**.
1f0cd 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72  .** If the "near
1f0ce 62 79 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  by" parameter is
1f0cf 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 28   not 0, then a (
1f0d0 66 65 65 62 6c 65 29 20 65 66 66 6f 72 74 20 69  feeble) effort i
1f0d1 73 20 6d 61 64 65 20 74 6f 20 0d 0a 2a 2a 20 6c  s made to ..** l
1f0d2 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f  ocate a page clo
1f0d3 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e  se to the page n
1f0d4 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20  umber "nearby". 
1f0d5 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
1f0d6 64 20 69 6e 20 61 6e 0d 0a 2a 2a 20 61 74 74 65  d in an..** atte
1f0d7 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c 61  mpt to keep rela
1f0d8 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 20  ted pages close 
1f0d9 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 6e  to each other in
1f0da 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1f0db 6c 65 2c 0d 0a 2a 2a 20 77 68 69 63 68 20 69 6e  le,..** which in
1f0dc 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64   turn can make d
1f0dd 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66  atabase access f
1f0de 61 73 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  aster...**..** I
1f0df 66 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61  f the "exact" pa
1f0e0 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30  rameter is not 0
1f0e1 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e  , and the page-n
1f0e2 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69  umber nearby exi
1f0e3 73 74 73 20 0d 0a 2a 2a 20 61 6e 79 77 68 65 72  sts ..** anywher
1f0e4 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  e on the free-li
1f0e5 73 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67  st, then it is g
1f0e6 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20  uarenteed to be 
1f0e7 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0d 0a  returned. This..
1f0e8 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  ** is only used 
1f0e9 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  by auto-vacuum d
1f0ea 61 74 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c  atabases when al
1f0eb 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74  locating a new t
1f0ec 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  able...*/..stati
1f0ed 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74  c int allocateBt
1f0ee 72 65 65 50 61 67 65 28 0d 0a 20 20 42 74 53 68  reePage(..  BtSh
1f0ef 61 72 65 64 20 2a 70 42 74 2c 20 0d 0a 20 20 4d  ared *pBt, ..  M
1f0f0 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
1f0f1 20 0d 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f   ..  Pgno *pPgno
1f0f2 2c 20 0d 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62  , ..  Pgno nearb
1f0f3 79 2c 0d 0a 20 20 75 38 20 65 78 61 63 74 0d 0a  y,..  u8 exact..
1f0f4 29 7b 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ){..  MemPage *p
1f0f5 50 61 67 65 31 3b 0d 0a 20 20 69 6e 74 20 72 63  Page1;..  int rc
1f0f6 3b 0d 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 20  ;..  u32 n;     
1f0f7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1f0f8 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
1f0f9 73 74 20 2a 2f 0d 0a 20 20 75 33 32 20 6b 3b 20  st */..  u32 k; 
1f0fa 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1f0fb 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74   leaves on the t
1f0fc 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65  runk of the free
1f0fd 6c 69 73 74 20 2a 2f 0d 0a 20 20 4d 65 6d 50 61  list */..  MemPa
1f0fe 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0d  ge *pTrunk = 0;.
1f0ff 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65  .  MemPage *pPre
1f100 76 54 72 75 6e 6b 20 3d 20 30 3b 0d 0a 20 20 50  vTrunk = 0;..  P
1f101 67 6e 6f 20 6d 78 50 61 67 65 3b 20 20 20 20 20  gno mxPage;     
1f102 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66  /* Total size of
1f103 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1f104 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72  le */....  asser
1f105 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1f106 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
1f107 29 20 29 3b 0d 0a 20 20 70 50 61 67 65 31 20 3d  ) );..  pPage1 =
1f108 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0d 0a 20   pBt->pPage1;.. 
1f109 20 6d 78 50 61 67 65 20 3d 20 62 74 72 65 65 50   mxPage = btreeP
1f10a 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0d 0a  agecount(pBt);..
1f10b 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26    n = get4byte(&
1f10c 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
1f10d 5d 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28  ]);..  testcase(
1f10e 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0d   n==mxPage-1 );.
1f10f 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65  .  if( n>=mxPage
1f110 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
1f111 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1f112 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  KPT;..  }..  if(
1f113 20 6e 3e 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20   n>0 ){..    /* 
1f114 54 68 65 72 65 20 61 72 65 20 70 61 67 65 73 20  There are pages 
1f115 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e  on the freelist.
1f116 20 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 20 74    Reuse one of t
1f117 68 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f 0d 0a  hose pages. */..
1f118 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 3b      Pgno iTrunk;
1f119 0d 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 4c  ..    u8 searchL
1f11a 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74  ist = 0; /* If t
1f11b 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 73  he free-list mus
1f11c 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f  t be searched fo
1f11d 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0d 0a 20  r 'nearby' */.. 
1f11e 20 20 20 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74     ..    /* If t
1f11f 68 65 20 27 65 78 61 63 74 27 20 70 61 72 61 6d  he 'exact' param
1f120 65 74 65 72 20 77 61 73 20 74 72 75 65 20 61 6e  eter was true an
1f121 64 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65  d a query of the
1f122 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0d 0a 20 20   pointer-map..  
1f123 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20    ** shows that 
1f124 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79  the page 'nearby
1f125 27 20 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f  ' is somewhere o
1f126 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c  n the free-list,
1f127 20 74 68 65 6e 0d 0a 20 20 20 20 2a 2a 20 74 68   then..    ** th
1f128 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 77 69  e entire-list wi
1f129 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64 20 66  ll be searched f
1f12a 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0d 0a 20  or that page... 
1f12b 20 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53     */..#ifndef S
1f12c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1f12d 41 43 55 55 4d 0d 0a 20 20 20 20 69 66 28 20 65  ACUUM..    if( e
1f12e 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d  xact && nearby<=
1f12f 6d 78 50 61 67 65 20 29 7b 0d 0a 20 20 20 20 20  mxPage ){..     
1f130 20 75 38 20 65 54 79 70 65 3b 0d 0a 20 20 20 20   u8 eType;..    
1f131 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79    assert( nearby
1f132 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73  >0 );..      ass
1f133 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61  ert( pBt->autoVa
1f134 63 75 75 6d 20 29 3b 0d 0a 20 20 20 20 20 20 72  cuum );..      r
1f135 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42  c = ptrmapGet(pB
1f136 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70  t, nearby, &eTyp
1f137 65 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66  e, 0);..      if
1f138 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1f139 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79  ;..      if( eTy
1f13a 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
1f13b 41 47 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  AGE ){..        
1f13c 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0d  searchList = 1;.
1f13d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
1f13e 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b  *pPgno = nearby;
1f13f 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
1f140 0a 0d 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  ...    /* Decrem
1f141 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73  ent the free-lis
1f142 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65  t count by 1. Se
1f143 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20  t iTrunk to the 
1f144 69 6e 64 65 78 20 6f 66 20 74 68 65 0d 0a 20 20  index of the..  
1f145 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d    ** first free-
1f146 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e  list trunk page.
1f147 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69   iPrevTrunk is i
1f148 6e 69 74 69 61 6c 6c 79 20 31 2e 0d 0a 20 20 20  nitially 1...   
1f149 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   */..    rc = sq
1f14a 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1f14b 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
1f14c 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  ;..    if( rc ) 
1f14d 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20  return rc;..    
1f14e 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
1f14f 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31  ->aData[36], n-1
1f150 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 65  );....    /* The
1f151 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69   code within thi
1f152 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e  s loop is run on
1f153 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27  ly once if the '
1f154 73 65 61 72 63 68 4c 69 73 74 27 20 76 61 72 69  searchList' vari
1f155 61 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 69 73 20  able..    ** is 
1f156 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72 77  not true. Otherw
1f157 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63  ise, it runs onc
1f158 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e 6b  e for each trunk
1f159 2d 70 61 67 65 20 6f 6e 20 74 68 65 0d 0a 20 20  -page on the..  
1f15a 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75    ** free-list u
1f15b 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e  ntil the page 'n
1f15c 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65  earby' is locate
1f15d 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  d...    */..    
1f15e 64 6f 20 7b 0d 0a 20 20 20 20 20 20 70 50 72 65  do {..      pPre
1f15f 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b  vTrunk = pTrunk;
1f160 0d 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65  ..      if( pPre
1f161 76 54 72 75 6e 6b 20 29 7b 0d 0a 20 20 20 20 20  vTrunk ){..     
1f162 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34     iTrunk = get4
1f163 62 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b  byte(&pPrevTrunk
1f164 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0d 0a 20 20  ->aData[0]);..  
1f165 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1f166 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74      iTrunk = get
1f167 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1f168 44 61 74 61 5b 33 32 5d 29 3b 0d 0a 20 20 20 20  Data[32]);..    
1f169 20 20 7d 0d 0a 20 20 20 20 20 20 74 65 73 74 63    }..      testc
1f16a 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50  ase( iTrunk==mxP
1f16b 61 67 65 20 29 3b 0d 0a 20 20 20 20 20 20 69 66  age );..      if
1f16c 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20  ( iTrunk>mxPage 
1f16d 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
1f16e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1f16f 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  BKPT;..      }el
1f170 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  se{..        rc 
1f171 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
1f172 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72  Bt, iTrunk, &pTr
1f173 75 6e 6b 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  unk, 0);..      
1f174 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  }..      if( rc 
1f175 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 54 72 75  ){..        pTru
1f176 6e 6b 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  nk = 0;..       
1f177 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
1f178 74 65 5f 70 61 67 65 3b 0d 0a 20 20 20 20 20 20  te_page;..      
1f179 7d 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
1f17a 20 70 54 72 75 6e 6b 21 3d 30 20 29 3b 0d 0a 20   pTrunk!=0 );.. 
1f17b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72       assert( pTr
1f17c 75 6e 6b 2d 3e 61 44 61 74 61 21 3d 30 20 29 3b  unk->aData!=0 );
1f17d 0d 0a 0d 0a 20 20 20 20 20 20 6b 20 3d 20 67 65  ....      k = ge
1f17e 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e  t4byte(&pTrunk->
1f17f 61 44 61 74 61 5b 34 5d 29 3b 20 2f 2a 20 23 20  aData[4]); /* # 
1f180 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 69  of leaves on thi
1f181 73 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0d  s trunk page */.
1f182 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20  .      if( k==0 
1f183 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29  && !searchList )
1f184 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  {..        /* Th
1f185 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c  e trunk has no l
1f186 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69  eaves and the li
1f187 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  st is not being 
1f188 73 65 61 72 63 68 65 64 2e 20 0d 0a 20 20 20 20  searched. ..    
1f189 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63      ** So extrac
1f18a 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65  t the trunk page
1f18b 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20   itself and use 
1f18c 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20  it as the newly 
1f18d 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c  ..        ** all
1f18e 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0d 0a  ocated page */..
1f18f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1f190 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b  pPrevTrunk==0 );
1f191 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ..        rc = s
1f192 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1f193 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  (pTrunk->pDbPage
1f194 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
1f195 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  rc ){..         
1f196 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
1f197 74 65 5f 70 61 67 65 3b 0d 0a 20 20 20 20 20 20  te_page;..      
1f198 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 2a 70 50    }..        *pP
1f199 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0d 0a 20  gno = iTrunk;.. 
1f19a 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
1f19b 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
1f19c 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
1f19d 5b 30 5d 2c 20 34 29 3b 0d 0a 20 20 20 20 20 20  [0], 4);..      
1f19e 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75    *ppPage = pTru
1f19f 6e 6b 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 72  nk;..        pTr
1f1a0 75 6e 6b 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  unk = 0;..      
1f1a1 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
1f1a2 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25  TE: %d trunk - %
1f1a3 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66  d free pages lef
1f1a4 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d  t\n", *pPgno, n-
1f1a5 31 29 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  1));..      }els
1f1a6 65 20 69 66 28 20 6b 3e 28 75 33 32 29 28 70 42  e if( k>(u32)(pB
1f1a7 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  t->usableSize/4 
1f1a8 2d 20 32 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  - 2) ){..       
1f1a9 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69   /* Value of k i
1f1aa 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20  s out of range. 
1f1ab 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   Database corrup
1f1ac 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 20  tion */..       
1f1ad 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1f1ae 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20  RUPT_BKPT;..    
1f1af 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
1f1b0 6f 63 61 74 65 5f 70 61 67 65 3b 0d 0a 23 69 66  ocate_page;..#if
1f1b1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f1b2 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 20  _AUTOVACUUM..   
1f1b3 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61     }else if( sea
1f1b4 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62  rchList && nearb
1f1b5 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0d 0a 20 20  y==iTrunk ){..  
1f1b6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
1f1b7 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63  t is being searc
1f1b8 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75  hed and this tru
1f1b9 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nk page is the p
1f1ba 61 67 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  age..        ** 
1f1bb 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67  to allocate, reg
1f1bc 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
1f1bd 65 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73  er it has leaves
1f1be 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
1f1bf 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a         assert( *
1f1c0 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b  pPgno==iTrunk );
1f1c1 0d 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67  ..        *ppPag
1f1c2 65 20 3d 20 70 54 72 75 6e 6b 3b 0d 0a 20 20 20  e = pTrunk;..   
1f1c3 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20       searchList 
1f1c4 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 72 63  = 0;..        rc
1f1c5 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1f1c6 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62  rite(pTrunk->pDb
1f1c7 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Page);..        
1f1c8 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20 20  if( rc ){..     
1f1c9 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
1f1ca 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0d 0a 20 20  locate_page;..  
1f1cb 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
1f1cc 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0d 0a 20 20   if( k==0 ){..  
1f1cd 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 72          if( !pPr
1f1ce 65 76 54 72 75 6e 6b 20 29 7b 0d 0a 20 20 20 20  evTrunk ){..    
1f1cf 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1f1d0 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
1f1d1 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
1f1d2 61 5b 30 5d 2c 20 34 29 3b 0d 0a 20 20 20 20 20  a[0], 4);..     
1f1d3 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
1f1d4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1f1d5 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1f1d6 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50  pPrevTrunk->pDbP
1f1d7 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  age);..         
1f1d8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1f1d9 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
1f1da 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1f1db 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0d 0a  allocate_page;..
1f1dc 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20              }.. 
1f1dd 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
1f1de 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61  y(&pPrevTrunk->a
1f1df 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b  Data[0], &pTrunk
1f1e0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0d  ->aData[0], 4);.
1f1e1 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
1f1e2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
1f1e3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1f1e4 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 71  runk page is req
1f1e5 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 6c  uired by the cal
1f1e6 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 61  ler but it conta
1f1e7 69 6e 73 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ins ..          
1f1e8 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66  ** pointers to f
1f1e9 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e  ree-list leaves.
1f1ea 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20   The first leaf 
1f1eb 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0d  becomes a trunk.
1f1ec 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61  .          ** pa
1f1ed 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ge in this case.
1f1ee 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a  ..          */..
1f1ef 20 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67            MemPag
1f1f0 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0d 0a 20  e *pNewTrunk;.. 
1f1f1 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e           Pgno iN
1f1f2 65 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79  ewTrunk = get4by
1f1f3 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
1f1f4 61 5b 38 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20  a[8]);..        
1f1f5 20 20 69 66 28 20 69 4e 65 77 54 72 75 6e 6b 3e    if( iNewTrunk>
1f1f6 6d 78 50 61 67 65 20 29 7b 20 0d 0a 20 20 20 20  mxPage ){ ..    
1f1f7 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1f1f8 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1f1f9 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ;..            g
1f1fa 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1f1fb 5f 70 61 67 65 3b 0d 0a 20 20 20 20 20 20 20 20  _page;..        
1f1fc 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 74    }..          t
1f1fd 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75  estcase( iNewTru
1f1fe 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0d 0a 20  nk==mxPage );.. 
1f1ff 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74           rc = bt
1f200 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1f201 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77  iNewTrunk, &pNew
1f202 54 72 75 6e 6b 2c 20 30 29 3b 0d 0a 20 20 20 20  Trunk, 0);..    
1f203 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1f204 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1f205 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
1f206 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0d  _allocate_page;.
1f207 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
1f208 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1f209 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1f20a 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  NewTrunk->pDbPag
1f20b 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  e);..          i
1f20c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1f20d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
1f20e 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65   releasePage(pNe
1f20f 77 54 72 75 6e 6b 29 3b 0d 0a 20 20 20 20 20 20  wTrunk);..      
1f210 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
1f211 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0d 0a 20  llocate_page;.. 
1f212 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
1f213 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e        memcpy(&pN
1f214 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  ewTrunk->aData[0
1f215 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
1f216 61 5b 30 5d 2c 20 34 29 3b 0d 0a 20 20 20 20 20  a[0], 4);..     
1f217 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1f218 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  NewTrunk->aData[
1f219 34 5d 2c 20 6b 2d 31 29 3b 0d 0a 20 20 20 20 20  4], k-1);..     
1f21a 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
1f21b 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d  wTrunk->aData[8]
1f21c 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
1f21d 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0d  [12], (k-1)*4);.
1f21e 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61  .          relea
1f21f 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b  sePage(pNewTrunk
1f220 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  );..          if
1f221 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b  ( !pPrevTrunk ){
1f222 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ..            as
1f223 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1f224 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1f225 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29  age1->pDbPage) )
1f226 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ;..            p
1f227 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  ut4byte(&pPage1-
1f228 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77  >aData[32], iNew
1f229 54 72 75 6e 6b 29 3b 0d 0a 20 20 20 20 20 20 20  Trunk);..       
1f22a 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1f22b 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1f22c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
1f22d 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  revTrunk->pDbPag
1f22e 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  e);..           
1f22f 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
1f230 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
1f231 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
1f232 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
1f233 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75  ..            pu
1f234 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75  t4byte(&pPrevTru
1f235 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e  nk->aData[0], iN
1f236 65 77 54 72 75 6e 6b 29 3b 0d 0a 20 20 20 20 20  ewTrunk);..     
1f237 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
1f238 7d 0d 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e  }..        pTrun
1f239 6b 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  k = 0;..        
1f23a 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
1f23b 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20  : %d trunk - %d 
1f23c 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c  free pages left\
1f23d 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29  n", *pPgno, n-1)
1f23e 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  );..#endif..    
1f23f 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 30 20    }else if( k>0 
1f240 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 45  ){..        /* E
1f241 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 66 72  xtract a leaf fr
1f242 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0d  om the trunk */.
1f243 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6c 6f  .        u32 clo
1f244 73 65 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 50  sest;..        P
1f245 67 6e 6f 20 69 50 61 67 65 3b 0d 0a 20 20 20 20  gno iPage;..    
1f246 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
1f247 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e  r *aData = pTrun
1f248 6b 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 20 20 20  k->aData;..     
1f249 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20     if( nearby>0 
1f24a 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 75 33  ){..          u3
1f24b 32 20 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  2 i;..          
1f24c 69 6e 74 20 64 69 73 74 3b 0d 0a 20 20 20 20 20  int dist;..     
1f24d 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30       closest = 0
1f24e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 64 69 73  ;..          dis
1f24f 74 20 3d 20 73 71 6c 69 74 65 33 41 62 73 49 6e  t = sqlite3AbsIn
1f250 74 33 32 28 67 65 74 34 62 79 74 65 28 26 61 44  t32(get4byte(&aD
1f251 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79  ata[8]) - nearby
1f252 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 66 6f  );..          fo
1f253 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29  r(i=1; i<k; i++)
1f254 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  {..            i
1f255 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 41  nt d2 = sqlite3A
1f256 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65  bsInt32(get4byte
1f257 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20  (&aData[8+i*4]) 
1f258 2d 20 6e 65 61 72 62 79 29 3b 0d 0a 20 20 20 20  - nearby);..    
1f259 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64          if( d2<d
1f25a 69 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ist ){..        
1f25b 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
1f25c 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  i;..            
1f25d 20 20 64 69 73 74 20 3d 20 64 32 3b 0d 0a 20 20    dist = d2;..  
1f25e 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
1f25f 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
1f260 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
1f261 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b      closest = 0;
1f262 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  ..        }.... 
1f263 20 20 20 20 20 20 20 69 50 61 67 65 20 3d 20 67         iPage = g
1f264 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38  et4byte(&aData[8
1f265 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0d 0a 20  +closest*4]);.. 
1f266 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
1f267 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29   iPage==mxPage )
1f268 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ;..        if( i
1f269 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0d 0a  Page>mxPage ){..
1f26a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1f26b 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1f26c 50 54 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67  PT;..          g
1f26d 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1f26e 5f 70 61 67 65 3b 0d 0a 20 20 20 20 20 20 20 20  _page;..        
1f26f 7d 0d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  }..        testc
1f270 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61  ase( iPage==mxPa
1f271 67 65 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  ge );..        i
1f272 66 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c  f( !searchList |
1f273 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20  | iPage==nearby 
1f274 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e  ){..          in
1f275 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0d 0a 20 20  t noContent;..  
1f276 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d          *pPgno =
1f277 20 69 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 20   iPage;..       
1f278 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43     TRACE(("ALLOC
1f279 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66  ATE: %d was leaf
1f27a 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75   %d of %d on tru
1f27b 6e 6b 20 25 64 22 0d 0a 20 20 20 20 20 20 20 20  nk %d"..        
1f27c 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d           ": %d m
1f27d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e  ore free pages\n
1f27e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ",..            
1f27f 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f       *pPgno, clo
1f280 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e  sest+1, k, pTrun
1f281 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0d  k->pgno, n-1));.
1f282 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1f283 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1f284 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  e(pTrunk->pDbPag
1f285 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  e);..          i
1f286 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
1f287 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0d  _allocate_page;.
1f288 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
1f289 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0d 0a 20  losest<k-1 ){.. 
1f28a 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
1f28b 79 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65  y(&aData[8+close
1f28c 73 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b  st*4], &aData[4+
1f28d 6b 2a 34 5d 2c 20 34 29 3b 0d 0a 20 20 20 20 20  k*4], 4);..     
1f28e 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
1f28f 20 20 70 75 74 34 62 79 74 65 28 26 61 44 61 74    put4byte(&aDat
1f290 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0d 0a 20 20 20  a[4], k-1);..   
1f291 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74         noContent
1f292 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43   = !btreeGetHasC
1f293 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67  ontent(pBt, *pPg
1f294 6e 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  no);..          
1f295 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
1f296 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70  e(pBt, *pPgno, p
1f297 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74  pPage, noContent
1f298 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  );..          if
1f299 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1f29a 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
1f29b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1f29c 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29  rWrite((*ppPage)
1f29d 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 20  ->pDbPage);..   
1f29e 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
1f29f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
1f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c               rel
1f2a1 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
1f2a2 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
1f2a3 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  }..          }..
1f2a4 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68            search
1f2a5 4c 69 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20  List = 0;..     
1f2a6 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
1f2a7 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1f2a8 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0d 0a 20  (pPrevTrunk);.. 
1f2a9 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20       pPrevTrunk 
1f2aa 3d 20 30 3b 0d 0a 20 20 20 20 7d 77 68 69 6c 65  = 0;..    }while
1f2ab 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0d  ( searchList );.
1f2ac 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f  .  }else{..    /
1f2ad 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70  * There are no p
1f2ae 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
1f2af 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20  list, so create 
1f2b0 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68  a new page at th
1f2b1 65 0d 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66  e..    ** end of
1f2b2 20 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20   the file */..  
1f2b3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1f2b4 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50  gerWrite(pBt->pP
1f2b5 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0d  age1->pDbPage);.
1f2b6 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
1f2b7 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 70 42  turn rc;..    pB
1f2b8 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0d 0a 20 20 20  t->nPage++;..   
1f2b9 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 3d   if( pBt->nPage=
1f2ba 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
1f2bb 47 45 28 70 42 74 29 20 29 20 70 42 74 2d 3e 6e  GE(pBt) ) pBt->n
1f2bc 50 61 67 65 2b 2b 3b 0d 0a 0d 0a 23 69 66 6e 64  Page++;....#ifnd
1f2bd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f2be 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 69  UTOVACUUM..    i
1f2bf 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
1f2c0 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50  um && PTRMAP_ISP
1f2c1 41 47 45 28 70 42 74 2c 20 70 42 74 2d 3e 6e 50  AGE(pBt, pBt->nP
1f2c2 61 67 65 29 20 29 7b 0d 0a 20 20 20 20 20 20 2f  age) ){..      /
1f2c3 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65  * If *pPgno refe
1f2c4 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d  rs to a pointer-
1f2c5 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61  map page, alloca
1f2c6 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73  te two new pages
1f2c7 0d 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68  ..      ** at th
1f2c8 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
1f2c9 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65  e instead of one
1f2ca 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c 6f  . The first allo
1f2cb 63 61 74 65 64 20 70 61 67 65 0d 0a 20 20 20 20  cated page..    
1f2cc 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e    ** becomes a n
1f2cd 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  ew pointer-map p
1f2ce 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  age, the second 
1f2cf 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63  is used by the c
1f2d0 61 6c 6c 65 72 2e 0d 0a 20 20 20 20 20 20 2a 2f  aller...      */
1f2d1 0d 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  ..      MemPage 
1f2d2 2a 70 50 67 20 3d 20 30 3b 0d 0a 20 20 20 20 20  *pPg = 0;..     
1f2d3 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
1f2d4 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f  E: %d from end o
1f2d5 66 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d  f file (pointer-
1f2d6 6d 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 70 42  map page)\n", pB
1f2d7 74 2d 3e 6e 50 61 67 65 29 29 3b 0d 0a 20 20 20  t->nPage));..   
1f2d8 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
1f2d9 6e 50 61 67 65 21 3d 50 45 4e 44 49 4e 47 5f 42  nPage!=PENDING_B
1f2da 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
1f2db 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72  ..      rc = btr
1f2dc 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70  eeGetPage(pBt, p
1f2dd 42 74 2d 3e 6e 50 61 67 65 2c 20 26 70 50 67 2c  Bt->nPage, &pPg,
1f2de 20 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20   1);..      if( 
1f2df 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1f2e0 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ..        rc = s
1f2e1 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1f2e2 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0d  (pPg->pDbPage);.
1f2e3 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1f2e4 50 61 67 65 28 70 50 67 29 3b 0d 0a 20 20 20 20  Page(pPg);..    
1f2e5 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
1f2e6 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  c ) return rc;..
1f2e7 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65        pBt->nPage
1f2e8 2b 2b 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  ++;..      if( p
1f2e9 42 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49  Bt->nPage==PENDI
1f2ea 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
1f2eb 29 20 29 7b 20 70 42 74 2d 3e 6e 50 61 67 65 2b  ) ){ pBt->nPage+
1f2ec 2b 3b 20 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  +; }..    }..#en
1f2ed 64 69 66 0d 0a 20 20 20 20 70 75 74 34 62 79 74  dif..    put4byt
1f2ee 65 28 32 38 20 2b 20 28 75 38 2a 29 70 42 74 2d  e(28 + (u8*)pBt-
1f2ef 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 2c 20  >pPage1->aData, 
1f2f0 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0d 0a 20 20  pBt->nPage);..  
1f2f1 20 20 2a 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e    *pPgno = pBt->
1f2f2 6e 50 61 67 65 3b 0d 0a 0d 0a 20 20 20 20 61 73  nPage;....    as
1f2f3 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45  sert( *pPgno!=PE
1f2f4 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1f2f5 70 42 74 29 20 29 3b 0d 0a 20 20 20 20 72 63 20  pBt) );..    rc 
1f2f6 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
1f2f7 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61  Bt, *pPgno, ppPa
1f2f8 67 65 2c 20 31 29 3b 0d 0a 20 20 20 20 69 66 28  ge, 1);..    if(
1f2f9 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1f2fa 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
1f2fb 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70  e3PagerWrite((*p
1f2fc 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29  pPage)->pDbPage)
1f2fd 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  ;..    if( rc!=S
1f2fe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1f2ff 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
1f300 70 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 7d 0d  ppPage);..    }.
1f301 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c  .    TRACE(("ALL
1f302 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65  OCATE: %d from e
1f303 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a  nd of file\n", *
1f304 70 50 67 6e 6f 29 29 3b 0d 0a 20 20 7d 0d 0a 0d  pPgno));..  }...
1f305 0a 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e  .  assert( *pPgn
1f306 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
1f307 50 41 47 45 28 70 42 74 29 20 29 3b 0d 0a 0d 0a  PAGE(pBt) );....
1f308 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
1f309 65 3a 0d 0a 20 20 72 65 6c 65 61 73 65 50 61 67  e:..  releasePag
1f30a 65 28 70 54 72 75 6e 6b 29 3b 0d 0a 20 20 72 65  e(pTrunk);..  re
1f30b 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 54  leasePage(pPrevT
1f30c 72 75 6e 6b 29 3b 0d 0a 20 20 69 66 28 20 72 63  runk);..  if( rc
1f30d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
1f30e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50      if( sqlite3P
1f30f 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74  agerPageRefcount
1f310 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50  ((*ppPage)->pDbP
1f311 61 67 65 29 3e 31 20 29 7b 0d 0a 20 20 20 20 20  age)>1 ){..     
1f312 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70   releasePage(*pp
1f313 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 72 65  Page);..      re
1f314 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1f315 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d  UPT_BKPT;..    }
1f316 0d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d  ..    (*ppPage)-
1f317 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0d 0a 20 20  >isInit = 0;..  
1f318 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2a 70 70 50  }else{..    *ppP
1f319 61 67 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  age = 0;..  }.. 
1f31a 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
1f31b 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 65  ITE_OK || sqlite
1f31c 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
1f31d 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62  e((*ppPage)->pDb
1f31e 50 61 67 65 29 20 29 3b 0d 0a 20 20 72 65 74 75  Page) );..  retu
1f31f 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
1f320 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1f321 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64  n is used to add
1f322 20 70 61 67 65 20 69 50 61 67 65 20 74 6f 20 74   page iPage to t
1f323 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1f324 20 66 72 65 65 2d 6c 69 73 74 2e 20 0d 0a 2a 2a   free-list. ..**
1f325 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
1f326 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
1f327 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 61  not already a pa
1f328 72 74 20 6f 66 20 74 68 65 20 66 72 65 65 2d 6c  rt of the free-l
1f329 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ist...**..** The
1f32a 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
1f32b 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
1f32c 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
1f32d 63 74 69 6f 6e 20 69 73 20 6f 70 74 69 6f 6e 61  ction is optiona
1f32e 6c 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20 63 61  l...** If the ca
1f32f 6c 6c 65 72 20 68 61 70 70 65 6e 73 20 74 6f 20  ller happens to 
1f330 68 61 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74  have a pointer t
1f331 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62  o the MemPage ob
1f332 6a 65 63 74 20 0d 0a 2a 2a 20 63 6f 72 72 65 73  ject ..** corres
1f333 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65 20  ponding to page 
1f334 69 50 61 67 65 20 68 61 6e 64 79 2c 20 69 74 20  iPage handy, it 
1f335 6d 61 79 20 70 61 73 73 20 69 74 20 61 73 20 74  may pass it as t
1f336 68 65 20 73 65 63 6f 6e 64 20 76 61 6c 75 65 2e  he second value.
1f337 20 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c   ..** Otherwise,
1f338 20 69 74 20 6d 61 79 20 70 61 73 73 20 4e 55 4c   it may pass NUL
1f339 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20  L...**..** If a 
1f33a 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d  pointer to a Mem
1f33b 50 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 70  Page object is p
1f33c 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
1f33d 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 0d 0a 2a  ond argument,..*
1f33e 2a 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20  * its reference 
1f33f 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74  count is not alt
1f340 65 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ered by this fun
1f341 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ction...*/..stat
1f342 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 32  ic int freePage2
1f343 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
1f344 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 67  MemPage *pMemPag
1f345 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b 0d  e, Pgno iPage){.
1f346 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75  .  MemPage *pTru
1f347 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nk = 0;         
1f348 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c         /* Free-l
1f349 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a  ist trunk page *
1f34a 2f 0d 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b  /..  Pgno iTrunk
1f34b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1f34c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1f34d 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d   number of free-
1f34e 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20  list trunk page 
1f34f 2a 2f 20 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a  */ ..  MemPage *
1f350 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
1f351 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  age1;      /* Lo
1f352 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f  cal reference to
1f353 20 70 61 67 65 20 31 20 2a 2f 0d 0a 20 20 4d 65   page 1 */..  Me
1f354 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20  mPage *pPage;   
1f355 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f356 20 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20    /* Page being 
1f357 66 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55  freed. May be NU
1f358 4c 4c 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63  LL. */..  int rc
1f359 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f35b 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d   Return Code */.
1f35c 0a 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20  .  int nFree;   
1f35d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f35e 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61         /* Initia
1f35f 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
1f360 73 20 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a  s on free-list *
1f361 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73  /....  assert( s
1f362 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1f363 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1f364 0d 0a 20 20 61 73 73 65 72 74 28 20 69 50 61 67  ..  assert( iPag
1f365 65 3e 31 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  e>1 );..  assert
1f366 28 20 21 70 4d 65 6d 50 61 67 65 20 7c 7c 20 70  ( !pMemPage || p
1f367 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 69  MemPage->pgno==i
1f368 50 61 67 65 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  Page );....  if(
1f369 20 70 4d 65 6d 50 61 67 65 20 29 7b 0d 0a 20 20   pMemPage ){..  
1f36a 20 20 70 50 61 67 65 20 3d 20 70 4d 65 6d 50 61    pPage = pMemPa
1f36b 67 65 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ge;..    sqlite3
1f36c 50 61 67 65 72 52 65 66 28 70 50 61 67 65 2d 3e  PagerRef(pPage->
1f36d 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 7d 65 6c  pDbPage);..  }el
1f36e 73 65 7b 0d 0a 20 20 20 20 70 50 61 67 65 20 3d  se{..    pPage =
1f36f 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70   btreePageLookup
1f370 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0d 0a 20  (pBt, iPage);.. 
1f371 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 63 72 65   }....  /* Incre
1f372 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61  ment the free pa
1f373 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67  ge count on pPag
1f374 65 31 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 73 71  e1 */..  rc = sq
1f375 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1f376 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
1f377 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  ;..  if( rc ) go
1f378 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
1f379 0d 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34  ..  nFree = get4
1f37a 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44  byte(&pPage1->aD
1f37b 61 74 61 5b 33 36 5d 29 3b 0d 0a 20 20 70 75 74  ata[36]);..  put
1f37c 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1f37d 44 61 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b  Data[36], nFree+
1f37e 31 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 42 74  1);....  if( pBt
1f37f 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1f380 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 29  _SECURE_DELETE )
1f381 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  {..    /* If the
1f382 20 73 65 63 75 72 65 5f 64 65 6c 65 74 65 20 6f   secure_delete o
1f383 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ption is enabled
1f384 2c 20 74 68 65 6e 0d 0a 20 20 20 20 2a 2a 20 61  , then..    ** a
1f385 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 72  lways fully over
1f386 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e  write deleted in
1f387 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a  formation with z
1f388 65 72 6f 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  eros...    */.. 
1f389 20 20 20 69 66 28 20 28 21 70 50 61 67 65 20 26     if( (!pPage &
1f38a 26 20 28 28 72 63 20 3d 20 62 74 72 65 65 47 65  & ((rc = btreeGe
1f38b 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65  tPage(pBt, iPage
1f38c 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30  , &pPage, 0))!=0
1f38d 29 20 29 0d 0a 20 20 20 20 20 7c 7c 20 20 20 20  ) )..     ||    
1f38e 20 20 20 20 20 20 20 20 28 28 72 63 20 3d 20 73          ((rc = s
1f38f 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1f390 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1f391 29 21 3d 30 29 0d 0a 20 20 20 20 29 7b 0d 0a 20  )!=0)..    ){.. 
1f392 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61       goto freepa
1f393 67 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a  ge_out;..    }..
1f394 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65      memset(pPage
1f395 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->aData, 0, pPag
1f396 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e->pBt->pageSize
1f397 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  );..  }....  /* 
1f398 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1f399 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
1f39a 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65  cuum, write an e
1f39b 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e  ntry in the poin
1f39c 74 65 72 2d 6d 61 70 0d 0a 20 20 2a 2a 20 74 6f  ter-map..  ** to
1f39d 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74   indicate that t
1f39e 68 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e  he page is free.
1f39f 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 49 53  ..  */..  if( IS
1f3a0 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0d 0a 20  AUTOVACUUM ){.. 
1f3a1 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
1f3a2 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f  , iPage, PTRMAP_
1f3a3 46 52 45 45 50 41 47 45 2c 20 30 2c 20 26 72 63  FREEPAGE, 0, &rc
1f3a4 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29  );..    if( rc )
1f3a5 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
1f3a6 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ut;..  }....  /*
1f3a7 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20   Now manipulate 
1f3a8 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62  the actual datab
1f3a9 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74  ase free-list st
1f3aa 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61  ructure. There a
1f3ab 72 65 20 74 77 6f 0d 0a 20 20 2a 2a 20 70 6f 73  re two..  ** pos
1f3ac 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74  sibilities. If t
1f3ad 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20  he free-list is 
1f3ae 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c  currently empty,
1f3af 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74   or if the first
1f3b0 0d 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67  ..  ** trunk pag
1f3b1 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  e in the free-li
1f3b2 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e  st is full, then
1f3b3 20 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20   this page will 
1f3b4 62 65 63 6f 6d 65 20 61 0d 0a 20 20 2a 2a 20 6e  become a..  ** n
1f3b5 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75  ew free-list tru
1f3b6 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69  nk page. Otherwi
1f3b7 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f  se, it will beco
1f3b8 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65  me a leaf of the
1f3b9 0d 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75  ..  ** first tru
1f3ba 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  nk page in the c
1f3bb 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 74  urrent free-list
1f3bc 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 73  . This block tes
1f3bd 74 73 20 69 66 20 69 74 0d 0a 20 20 2a 2a 20 69  ts if it..  ** i
1f3be 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64  s possible to ad
1f3bf 64 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20  d the page as a 
1f3c0 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  new free-list le
1f3c1 61 66 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  af...  */..  if(
1f3c2 20 6e 46 72 65 65 21 3d 30 20 29 7b 0d 0a 20 20   nFree!=0 ){..  
1f3c3 20 20 75 33 32 20 6e 4c 65 61 66 3b 20 20 20 20    u32 nLeaf;    
1f3c4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1f3c5 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66  nitial number of
1f3c6 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74   leaf cells on t
1f3c7 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0d 0a 0d 0a  runk page */....
1f3c8 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74      iTrunk = get
1f3c9 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1f3ca 44 61 74 61 5b 33 32 5d 29 3b 0d 0a 20 20 20 20  Data[32]);..    
1f3cb 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
1f3cc 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26  e(pBt, iTrunk, &
1f3cd 70 54 72 75 6e 6b 2c 20 30 29 3b 0d 0a 20 20 20  pTrunk, 0);..   
1f3ce 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1f3cf 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74  OK ){..      got
1f3d0 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0d  o freepage_out;.
1f3d1 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 6e 4c  .    }....    nL
1f3d2 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26  eaf = get4byte(&
1f3d3 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d  pTrunk->aData[4]
1f3d4 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
1f3d5 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e  pBt->usableSize>
1f3d6 33 32 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 6e  32 );..    if( n
1f3d7 4c 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d  Leaf > (u32)pBt-
1f3d8 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20  >usableSize/4 - 
1f3d9 32 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  2 ){..      rc =
1f3da 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1f3db 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74  BKPT;..      got
1f3dc 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0d  o freepage_out;.
1f3dd 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1f3de 6e 4c 65 61 66 20 3c 20 28 75 33 32 29 70 42 74  nLeaf < (u32)pBt
1f3df 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  ->usableSize/4 -
1f3e0 20 38 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20   8 ){..      /* 
1f3e1 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
1f3e2 72 65 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68  re is room on th
1f3e3 65 20 74 72 75 6e 6b 20 70 61 67 65 20 74 6f 20  e trunk page to 
1f3e4 69 6e 73 65 72 74 20 74 68 65 20 70 61 67 65 0d  insert the page.
1f3e5 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20  .      ** being 
1f3e6 66 72 65 65 64 20 61 73 20 61 20 6e 65 77 20 6c  freed as a new l
1f3e7 65 61 66 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a  eaf...      **..
1f3e8 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68        ** Note th
1f3e9 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67  at the trunk pag
1f3ea 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20  e is not really 
1f3eb 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 6f  full until it co
1f3ec 6e 74 61 69 6e 73 0d 0a 20 20 20 20 20 20 2a 2a  ntains..      **
1f3ed 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
1f3ee 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75  2 entries, not u
1f3ef 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
1f3f0 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61  entries as we ha
1f3f1 76 65 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64  ve..      ** cod
1f3f2 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20  ed.  But due to 
1f3f3 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69  a coding error i
1f3f4 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  n versions of SQ
1f3f5 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0d 0a 20  Lite prior to.. 
1f3f6 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64       ** 3.6.0, d
1f3f7 61 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72  atabases with fr
1f3f8 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67  eelist trunk pag
1f3f9 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20  es holding more 
1f3fa 74 68 61 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 75  than..      ** u
1f3fb 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
1f3fc 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20  entries will be 
1f3fd 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72  reported as corr
1f3fe 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0d 0a  upt.  In order..
1f3ff 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e        ** to main
1f400 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63  tain backwards c
1f401 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74  ompatibility wit
1f402 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73  h older versions
1f403 20 6f 66 20 53 51 4c 69 74 65 2c 0d 0a 20 20 20   of SQLite,..   
1f404 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f     ** we will co
1f405 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69  ntinue to restri
1f406 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ct the number of
1f407 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62   entries to usab
1f408 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0d 0a 20 20  leSize/4 - 8..  
1f409 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20      ** for now. 
1f40a 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69   At some point i
1f40b 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e  n the future (on
1f40c 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20  ce everyone has 
1f40d 75 70 67 72 61 64 65 64 0d 0a 20 20 20 20 20 20  upgraded..      
1f40e 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c  ** to 3.6.0 or l
1f40f 61 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20  ater) we should 
1f410 63 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20  consider fixing 
1f411 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20  the conditional 
1f412 61 62 6f 76 65 0d 0a 20 20 20 20 20 20 2a 2a 20  above..      ** 
1f413 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c 65 53  to read "usableS
1f414 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64  ize/4-2" instead
1f415 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f   of "usableSize/
1f416 34 2d 38 22 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  4-8"...      */.
1f417 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1f418 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54  te3PagerWrite(pT
1f419 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0d  runk->pDbPage);.
1f41a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1f41b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1f41c 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1f41d 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c  Trunk->aData[4],
1f41e 20 6e 4c 65 61 66 2b 31 29 3b 0d 0a 20 20 20 20   nLeaf+1);..    
1f41f 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54      put4byte(&pT
1f420 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c  runk->aData[8+nL
1f421 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 3b 0d  eaf*4], iPage);.
1f422 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
1f423 67 65 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46  ge && (pBt->btsF
1f424 6c 61 67 73 20 26 20 42 54 53 5f 53 45 43 55 52  lags & BTS_SECUR
1f425 45 5f 44 45 4c 45 54 45 29 3d 3d 30 20 29 7b 0d  E_DELETE)==0 ){.
1f426 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1f427 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65  e3PagerDontWrite
1f428 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1f429 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
1f42a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65        rc = btree
1f42b 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42  SetHasContent(pB
1f42c 74 2c 20 69 50 61 67 65 29 3b 0d 0a 20 20 20 20  t, iPage);..    
1f42d 20 20 7d 0d 0a 20 20 20 20 20 20 54 52 41 43 45    }..      TRACE
1f42e 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64  (("FREE-PAGE: %d
1f42f 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70   leaf on trunk p
1f430 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d  age %d\n",pPage-
1f431 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67  >pgno,pTrunk->pg
1f432 6e 6f 29 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74  no));..      got
1f433 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0d  o freepage_out;.
1f434 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
1f435 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66   /* If control f
1f436 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69  lows to this poi
1f437 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 20  nt, then it was 
1f438 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
1f439 61 64 64 20 74 68 65 0d 0a 20 20 2a 2a 20 74 68  add the..  ** th
1f43a 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65  e page being fre
1f43b 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67  ed as a leaf pag
1f43c 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
1f43d 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65  runk in the free
1f43e 2d 6c 69 73 74 2e 0d 0a 20 20 2a 2a 20 50 6f 73  -list...  ** Pos
1f43f 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68  sibly because th
1f440 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65  e free-list is e
1f441 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c  mpty, or possibl
1f442 79 20 62 65 63 61 75 73 65 20 74 68 65 20 0d 0a  y because the ..
1f443 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b    ** first trunk
1f444 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   in the free-lis
1f445 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65  t is full. Eithe
1f446 72 20 77 61 79 2c 20 74 68 65 20 70 61 67 65 20  r way, the page 
1f447 62 65 69 6e 67 20 66 72 65 65 64 0d 0a 20 20 2a  being freed..  *
1f448 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  * will become th
1f449 65 20 6e 65 77 20 66 69 72 73 74 20 74 72 75 6e  e new first trun
1f44a 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 72  k page in the fr
1f44b 65 65 2d 6c 69 73 74 2e 0d 0a 20 20 2a 2f 0d 0a  ee-list...  */..
1f44c 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26    if( pPage==0 &
1f44d 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
1f44e 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1f44f 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61  pBt, iPage, &pPa
1f450 67 65 2c 20 30 29 29 20 29 7b 0d 0a 20 20 20 20  ge, 0)) ){..    
1f451 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75  goto freepage_ou
1f452 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20  t;..  }..  rc = 
1f453 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1f454 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1f455 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
1f456 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
1f457 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75  goto freepage_ou
1f458 74 3b 0d 0a 20 20 7d 0d 0a 20 20 70 75 74 34 62  t;..  }..  put4b
1f459 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61  yte(pPage->aData
1f45a 2c 20 69 54 72 75 6e 6b 29 3b 0d 0a 20 20 70 75  , iTrunk);..  pu
1f45b 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
1f45c 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0d 0a 20 20  Data[4], 0);..  
1f45d 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
1f45e 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 50 61  ->aData[32], iPa
1f45f 67 65 29 3b 0d 0a 20 20 54 52 41 43 45 28 28 22  ge);..  TRACE(("
1f460 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e 65  FREE-PAGE: %d ne
1f461 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 70  w trunk page rep
1f462 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50  lacing %d\n", pP
1f463 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e  age->pgno, iTrun
1f464 6b 29 29 3b 0d 0a 0d 0a 66 72 65 65 70 61 67 65  k));....freepage
1f465 5f 6f 75 74 3a 0d 0a 20 20 69 66 28 20 70 50 61  _out:..  if( pPa
1f466 67 65 20 29 7b 0d 0a 20 20 20 20 70 50 61 67 65  ge ){..    pPage
1f467 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0d 0a 20  ->isInit = 0;.. 
1f468 20 7d 0d 0a 20 20 72 65 6c 65 61 73 65 50 61 67   }..  releasePag
1f469 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 72 65 6c  e(pPage);..  rel
1f46a 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29  easePage(pTrunk)
1f46b 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
1f46c 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
1f46d 66 72 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65  freePage(MemPage
1f46e 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52   *pPage, int *pR
1f46f 43 29 7b 0d 0a 20 20 69 66 28 20 28 2a 70 52 43  C){..  if( (*pRC
1f470 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  )==SQLITE_OK ){.
1f471 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 65 65  .    *pRC = free
1f472 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 42 74  Page2(pPage->pBt
1f473 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  , pPage, pPage->
1f474 70 67 6e 6f 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  pgno);..  }..}..
1f475 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 6e  ../*..** Free an
1f476 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  y overflow pages
1f477 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1f478 20 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e   the given Cell.
1f479 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
1f47a 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61   clearCell(MemPa
1f47b 67 65 20 2a 70 50 61 67 65 2c 20 75 6e 73 69 67  ge *pPage, unsig
1f47c 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 29  ned char *pCell)
1f47d 7b 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  {..  BtShared *p
1f47e 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
1f47f 0d 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  ..  CellInfo inf
1f480 6f 3b 0d 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50  o;..  Pgno ovflP
1f481 67 6e 6f 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  gno;..  int rc;.
1f482 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0d 0a 20  .  int nOvfl;.. 
1f483 20 75 33 32 20 6f 76 66 6c 50 61 67 65 53 69 7a   u32 ovflPageSiz
1f484 65 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  e;....  assert( 
1f485 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1f486 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
1f487 75 74 65 78 29 20 29 3b 0d 0a 20 20 62 74 72 65  utex) );..  btre
1f488 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
1f489 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
1f48a 6f 29 3b 0d 0a 20 20 69 66 28 20 69 6e 66 6f 2e  o);..  if( info.
1f48b 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0d  iOverflow==0 ){.
1f48c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1f48d 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76  TE_OK;  /* No ov
1f48e 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65  erflow pages. Re
1f48f 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69  turn without doi
1f490 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0d 0a  ng anything */..
1f491 20 20 7d 0d 0a 20 20 69 66 28 20 70 43 65 6c 6c    }..  if( pCell
1f492 2b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 2b  +info.iOverflow+
1f493 33 20 3e 20 70 50 61 67 65 2d 3e 61 44 61 74 61  3 > pPage->aData
1f494 2b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65  +pPage->maskPage
1f495 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
1f496 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 20  SQLITE_CORRUPT; 
1f497 20 2f 2a 20 43 65 6c 6c 20 65 78 74 65 6e 64 73   /* Cell extends
1f498 20 70 61 73 74 20 65 6e 64 20 6f 66 20 70 61 67   past end of pag
1f499 65 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 6f 76 66  e */..  }..  ovf
1f49a 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  lPgno = get4byte
1f49b 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
1f49c 65 72 66 6c 6f 77 5d 29 3b 0d 0a 20 20 61 73 73  erflow]);..  ass
1f49d 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65  ert( pBt->usable
1f49e 53 69 7a 65 20 3e 20 34 20 29 3b 0d 0a 20 20 6f  Size > 4 );..  o
1f49f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42  vflPageSize = pB
1f4a0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
1f4a1 34 3b 0d 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69  4;..  nOvfl = (i
1f4a2 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69  nfo.nPayload - i
1f4a3 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66  nfo.nLocal + ovf
1f4a4 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f  lPageSize - 1)/o
1f4a5 76 66 6c 50 61 67 65 53 69 7a 65 3b 0d 0a 20 20  vflPageSize;..  
1f4a6 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f  assert( ovflPgno
1f4a7 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29  ==0 || nOvfl>0 )
1f4a8 3b 0d 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66  ;..  while( nOvf
1f4a9 6c 2d 2d 20 29 7b 0d 0a 20 20 20 20 50 67 6e 6f  l-- ){..    Pgno
1f4aa 20 69 4e 65 78 74 20 3d 20 30 3b 0d 0a 20 20 20   iNext = 0;..   
1f4ab 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20   MemPage *pOvfl 
1f4ac 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 6f 76  = 0;..    if( ov
1f4ad 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76 66 6c  flPgno<2 || ovfl
1f4ae 50 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 63 6f  Pgno>btreePageco
1f4af 75 6e 74 28 70 42 74 29 20 29 7b 0d 0a 20 20 20  unt(pBt) ){..   
1f4b0 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74 20 61     /* 0 is not a
1f4b1 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62   legal page numb
1f4b2 65 72 20 61 6e 64 20 70 61 67 65 20 31 20 63 61  er and page 1 ca
1f4b3 6e 6e 6f 74 20 62 65 20 61 6e 20 0d 0a 20 20 20  nnot be an ..   
1f4b4 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70     ** overflow p
1f4b5 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20 69  age. Therefore i
1f4b6 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 20  f ovflPgno<2 or 
1f4b7 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
1f4b8 74 68 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 66  the ..      ** f
1f4b9 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ile the database
1f4ba 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
1f4bb 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75  . */..      retu
1f4bc 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1f4bd 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a  T_BKPT;..    }..
1f4be 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b      if( nOvfl ){
1f4bf 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 74  ..      rc = get
1f4c0 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74  OverflowPage(pBt
1f4c1 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76  , ovflPgno, &pOv
1f4c2 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0d 0a 20 20  fl, &iNext);..  
1f4c3 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1f4c4 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a  urn rc;..    }..
1f4c5 0d 0a 20 20 20 20 69 66 28 20 28 20 70 4f 76 66  ..    if( ( pOvf
1f4c6 6c 20 7c 7c 20 28 28 70 4f 76 66 6c 20 3d 20 62  l || ((pOvfl = b
1f4c7 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70  treePageLookup(p
1f4c8 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 29 29 21 3d  Bt, ovflPgno))!=
1f4c9 30 29 20 29 0d 0a 20 20 20 20 20 26 26 20 73 71  0) )..     && sq
1f4ca 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65  lite3PagerPageRe
1f4cb 66 63 6f 75 6e 74 28 70 4f 76 66 6c 2d 3e 70 44  fcount(pOvfl->pD
1f4cc 62 50 61 67 65 29 21 3d 31 0d 0a 20 20 20 20 29  bPage)!=1..    )
1f4cd 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 72  {..      /* Ther
1f4ce 65 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e 20 61  e is no reason a
1f4cf 6e 79 20 63 75 72 73 6f 72 20 73 68 6f 75 6c 64  ny cursor should
1f4d0 20 68 61 76 65 20 61 6e 20 6f 75 74 73 74 61 6e   have an outstan
1f4d1 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 20 0d  ding reference .
1f4d2 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6e 20  .      ** to an 
1f4d3 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 62 65  overflow page be
1f4d4 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 20 63 65 6c  longing to a cel
1f4d5 6c 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  l that is being 
1f4d6 64 65 6c 65 74 65 64 2f 75 70 64 61 74 65 64 2e  deleted/updated.
1f4d7 0d 0a 20 20 20 20 20 20 2a 2a 20 53 6f 20 69 66  ..      ** So if
1f4d8 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6d 6f   there exists mo
1f4d9 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 65 66 65  re than one refe
1f4da 72 65 6e 63 65 20 74 6f 20 74 68 69 73 20 70 61  rence to this pa
1f4db 67 65 2c 20 74 68 65 6e 20 69 74 20 0d 0a 20 20  ge, then it ..  
1f4dc 20 20 20 20 2a 2a 20 6d 75 73 74 20 6e 6f 74 20      ** must not 
1f4dd 72 65 61 6c 6c 79 20 62 65 20 61 6e 20 6f 76 65  really be an ove
1f4de 72 66 6c 6f 77 20 70 61 67 65 20 61 6e 64 20 74  rflow page and t
1f4df 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74  he database must
1f4e0 20 62 65 20 63 6f 72 72 75 70 74 2e 20 0d 0a 20   be corrupt. .. 
1f4e1 20 20 20 20 20 2a 2a 20 49 74 20 69 73 20 68 65       ** It is he
1f4e2 6c 70 66 75 6c 20 74 6f 20 64 65 74 65 63 74 20  lpful to detect 
1f4e3 74 68 69 73 20 62 65 66 6f 72 65 20 63 61 6c 6c  this before call
1f4e4 69 6e 67 20 66 72 65 65 50 61 67 65 32 28 29 2c  ing freePage2(),
1f4e5 20 61 73 20 0d 0a 20 20 20 20 20 20 2a 2a 20 66   as ..      ** f
1f4e6 72 65 65 50 61 67 65 32 28 29 20 6d 61 79 20 7a  reePage2() may z
1f4e7 65 72 6f 20 74 68 65 20 70 61 67 65 20 63 6f 6e  ero the page con
1f4e8 74 65 6e 74 73 20 69 66 20 73 65 63 75 72 65 2d  tents if secure-
1f4e9 64 65 6c 65 74 65 20 6d 6f 64 65 20 69 73 0d 0a  delete mode is..
1f4ea 20 20 20 20 20 20 2a 2a 20 65 6e 61 62 6c 65 64        ** enabled
1f4eb 2e 20 49 66 20 74 68 69 73 20 27 6f 76 65 72 66  . If this 'overf
1f4ec 6c 6f 77 27 20 70 61 67 65 20 68 61 70 70 65 6e  low' page happen
1f4ed 73 20 74 6f 20 62 65 20 61 20 70 61 67 65 20 74  s to be a page t
1f4ee 68 61 74 20 74 68 65 0d 0a 20 20 20 20 20 20 2a  hat the..      *
1f4ef 2a 20 63 61 6c 6c 65 72 20 69 73 20 69 74 65 72  * caller is iter
1f4f0 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20 6f 72  ating through or
1f4f1 20 75 73 69 6e 67 20 69 6e 20 73 6f 6d 65 20 6f   using in some o
1f4f2 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 0d 0a  ther way, this..
1f4f3 20 20 20 20 20 20 2a 2a 20 63 61 6e 20 62 65 20        ** can be 
1f4f4 70 72 6f 62 6c 65 6d 61 74 69 63 2e 0d 0a 20 20  problematic...  
1f4f5 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 72 63      */..      rc
1f4f6 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1f4f7 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 65 6c  T_BKPT;..    }el
1f4f8 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  se{..      rc = 
1f4f9 66 72 65 65 50 61 67 65 32 28 70 42 74 2c 20 70  freePage2(pBt, p
1f4fa 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b  Ovfl, ovflPgno);
1f4fb 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69  ..    }....    i
1f4fc 66 28 20 70 4f 76 66 6c 20 29 7b 0d 0a 20 20 20  f( pOvfl ){..   
1f4fd 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
1f4fe 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50  nref(pOvfl->pDbP
1f4ff 61 67 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  age);..    }..  
1f500 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1f501 6e 20 72 63 3b 0d 0a 20 20 20 20 6f 76 66 6c 50  n rc;..    ovflP
1f502 67 6e 6f 20 3d 20 69 4e 65 78 74 3b 0d 0a 20 20  gno = iNext;..  
1f503 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
1f504 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  TE_OK;..}..../*.
1f505 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 62  .** Create the b
1f506 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 73 65  yte sequence use
1f507 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61  d to represent a
1f508 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50   cell on page pP
1f509 61 67 65 0d 0a 2a 2a 20 61 6e 64 20 77 72 69 74  age..** and writ
1f50a 65 20 74 68 61 74 20 62 79 74 65 20 73 65 71 75  e that byte sequ
1f50b 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b  ence into pCell[
1f50c 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 67  ].  Overflow pag
1f50d 65 73 20 61 72 65 0d 0a 2a 2a 20 61 6c 6c 6f 63  es are..** alloc
1f50e 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20  ated and filled 
1f50f 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  in as necessary.
1f510 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72    The calling pr
1f511 6f 63 65 64 75 72 65 0d 0a 2a 2a 20 69 73 20 72  ocedure..** is r
1f512 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d  esponsible for m
1f513 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69  aking sure suffi
1f514 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20  cient space has 
1f515 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0d 0a  been allocated..
1f516 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0d  ** for pCell[]..
1f517 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  .**..** Note tha
1f518 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74  t pCell does not
1f519 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20   necessary need 
1f51a 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
1f51b 70 50 61 67 65 2d 3e 61 44 61 74 61 0d 0a 2a 2a  pPage->aData..**
1f51c 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69   area.  pCell mi
1f51d 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d  ght point to som
1f51e 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72  e temporary stor
1f51f 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77  age.  The cell w
1f520 69 6c 6c 0d 0a 2a 2a 20 62 65 20 63 6f 6e 73 74  ill..** be const
1f521 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 74  ructed in this t
1f522 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 68  emporary area th
1f523 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70  en copied into p
1f524 50 61 67 65 2d 3e 61 44 61 74 61 0d 0a 2a 2a 20  Page->aData..** 
1f525 6c 61 74 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  later...*/..stat
1f526 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c  ic int fillInCel
1f527 6c 28 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  l(..  MemPage *p
1f528 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
1f529 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
1f52a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
1f52b 68 65 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20 75 6e  he cell */..  un
1f52c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
1f52d 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll,          /* 
1f52e 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  Complete text of
1f52f 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20   the cell */..  
1f530 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
1f531 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f  , i64 nKey,    /
1f532 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0d 0a 20 20  * The key */..  
1f533 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74  const void *pDat
1f534 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f  a,int nData,   /
1f535 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0d 0a 20  * The data */.. 
1f536 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20   int nZero,     
1f537 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f538 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79  /* Extra zero by
1f539 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f  tes to append to
1f53a 20 70 44 61 74 61 20 2a 2f 0d 0a 20 20 69 6e 74   pData */..  int
1f53b 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 20   *pnSize        
1f53c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
1f53d 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20 68  rite cell size h
1f53e 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ere */..){..  in
1f53f 74 20 6e 50 61 79 6c 6f 61 64 3b 0d 0a 20 20 63  t nPayload;..  c
1f540 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0d 0a  onst u8 *pSrc;..
1f541 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72    int nSrc, n, r
1f542 63 3b 0d 0a 20 20 69 6e 74 20 73 70 61 63 65 4c  c;..  int spaceL
1f543 65 66 74 3b 0d 0a 20 20 4d 65 6d 50 61 67 65 20  eft;..  MemPage 
1f544 2a 70 4f 76 66 6c 20 3d 20 30 3b 0d 0a 20 20 4d  *pOvfl = 0;..  M
1f545 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 61  emPage *pToRelea
1f546 73 65 20 3d 20 30 3b 0d 0a 20 20 75 6e 73 69 67  se = 0;..  unsig
1f547 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72  ned char *pPrior
1f548 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
1f549 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0d 0a 20  ar *pPayload;.. 
1f54a 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1f54b 20 70 50 61 67 65 2d 3e 70 42 74 3b 0d 0a 20 20   pPage->pBt;..  
1f54c 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20  Pgno pgnoOvfl = 
1f54d 30 3b 0d 0a 20 20 69 6e 74 20 6e 48 65 61 64 65  0;..  int nHeade
1f54e 72 3b 0d 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69  r;..  CellInfo i
1f54f 6e 66 6f 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  nfo;....  assert
1f550 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1f551 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
1f552 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 0d 0a 20 20  >mutex) );....  
1f553 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20  /* pPage is not 
1f554 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69 74  necessarily writ
1f555 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c  eable since pCel
1f556 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c  l might be auxil
1f557 69 61 72 79 0d 0a 20 20 2a 2a 20 62 75 66 66 65  iary..  ** buffe
1f558 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20  r space that is 
1f559 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68  separate from th
1f55a 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61  e pPage buffer a
1f55b 72 65 61 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  rea */..  assert
1f55c 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61  ( pCell<pPage->a
1f55d 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26  Data || pCell>=&
1f55e 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
1f55f 2d 3e 70 61 67 65 53 69 7a 65 5d 0d 0a 20 20 20  ->pageSize]..   
1f560 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69           || sqli
1f561 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1f562 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
1f563 67 65 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 46  ge) );....  /* F
1f564 69 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65  ill in the heade
1f565 72 2e 20 2a 2f 0d 0a 20 20 6e 48 65 61 64 65 72  r. */..  nHeader
1f566 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 21 70 50   = 0;..  if( !pP
1f567 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20 20  age->leaf ){..  
1f568 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 34 3b 0d    nHeader += 4;.
1f569 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 50 61 67  .  }..  if( pPag
1f56a 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0d 0a 20  e->hasData ){.. 
1f56b 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75     nHeader += pu
1f56c 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
1f56d 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b 6e  Header], nData+n
1f56e 5a 65 72 6f 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b  Zero);..  }else{
1f56f 0d 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a  ..    nData = nZ
1f570 65 72 6f 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  ero = 0;..  }.. 
1f571 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56   nHeader += putV
1f572 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65  arint(&pCell[nHe
1f573 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e  ader], *(u64*)&n
1f574 4b 65 79 29 3b 0d 0a 20 20 62 74 72 65 65 50 61  Key);..  btreePa
1f575 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
1f576 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
1f577 0d 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f  ..  assert( info
1f578 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65  .nHeader==nHeade
1f579 72 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  r );..  assert( 
1f57a 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20  info.nKey==nKey 
1f57b 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 6e  );..  assert( in
1f57c 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28  fo.nData==(u32)(
1f57d 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0d  nData+nZero) );.
1f57e 0a 20 20 0d 0a 20 20 2f 2a 20 46 69 6c 6c 20 69  .  ..  /* Fill i
1f57f 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f  n the payload */
1f580 0d 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  ..  nPayload = n
1f581 44 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0d 0a 20  Data + nZero;.. 
1f582 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
1f583 65 79 20 29 7b 0d 0a 20 20 20 20 70 53 72 63 20  ey ){..    pSrc 
1f584 3d 20 70 44 61 74 61 3b 0d 0a 20 20 20 20 6e 53  = pData;..    nS
1f585 72 63 20 3d 20 6e 44 61 74 61 3b 0d 0a 20 20 20  rc = nData;..   
1f586 20 6e 44 61 74 61 20 3d 20 30 3b 0d 0a 20 20 7d   nData = 0;..  }
1f587 65 6c 73 65 7b 20 0d 0a 20 20 20 20 69 66 28 20  else{ ..    if( 
1f588 4e 45 56 45 52 28 6e 4b 65 79 3e 30 78 37 66 66  NEVER(nKey>0x7ff
1f589 66 66 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d 30  fffff || pKey==0
1f58a 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  ) ){..      retu
1f58b 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1f58c 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a  T_BKPT;..    }..
1f58d 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d 20      nPayload += 
1f58e 28 69 6e 74 29 6e 4b 65 79 3b 0d 0a 20 20 20 20  (int)nKey;..    
1f58f 70 53 72 63 20 3d 20 70 4b 65 79 3b 0d 0a 20 20  pSrc = pKey;..  
1f590 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b    nSrc = (int)nK
1f591 65 79 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 6e 53  ey;..  }..  *pnS
1f592 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65  ize = info.nSize
1f593 3b 0d 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d  ;..  spaceLeft =
1f594 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0d 0a 20   info.nLocal;.. 
1f595 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65   pPayload = &pCe
1f596 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0d 0a 20 20  ll[nHeader];..  
1f597 70 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b  pPrior = &pCell[
1f598 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b  info.iOverflow];
1f599 0d 0a 0d 0a 20 20 77 68 69 6c 65 28 20 6e 50 61  ....  while( nPa
1f59a 79 6c 6f 61 64 3e 30 20 29 7b 0d 0a 20 20 20 20  yload>0 ){..    
1f59b 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30  if( spaceLeft==0
1f59c 20 29 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   ){..#ifndef SQL
1f59d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1f59e 55 55 4d 0d 0a 20 20 20 20 20 20 50 67 6e 6f 20  UUM..      Pgno 
1f59f 70 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e  pgnoPtrmap = pgn
1f5a0 6f 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c  oOvfl; /* Overfl
1f5a1 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d  ow page pointer-
1f5a2 6d 61 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a  map entry page *
1f5a3 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  /..      if( pBt
1f5a4 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0d  ->autoVacuum ){.
1f5a5 0a 20 20 20 20 20 20 20 20 64 6f 7b 0d 0a 20 20  .        do{..  
1f5a6 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76 66 6c          pgnoOvfl
1f5a7 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 20 77  ++;..        } w
1f5a8 68 69 6c 65 28 20 0d 0a 20 20 20 20 20 20 20 20  hile( ..        
1f5a9 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28    PTRMAP_ISPAGE(
1f5aa 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c  pBt, pgnoOvfl) |
1f5ab 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44  | pgnoOvfl==PEND
1f5ac 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1f5ad 74 29 20 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d  t) ..        );.
1f5ae 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  .      }..#endif
1f5af 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c  ..      rc = all
1f5b0 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
1f5b1 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e  Bt, &pOvfl, &pgn
1f5b2 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c  oOvfl, pgnoOvfl,
1f5b3 20 30 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51   0);..#ifndef SQ
1f5b4 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1f5b5 43 55 55 4d 0d 0a 20 20 20 20 20 20 2f 2a 20 49  CUUM..      /* I
1f5b6 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
1f5b7 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63  upports auto-vac
1f5b8 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 63  uum, and the sec
1f5b9 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65 6e  ond or subsequen
1f5ba 74 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  t..      ** over
1f5bb 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69  flow page is bei
1f5bc 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64  ng allocated, ad
1f5bd 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68  d an entry to th
1f5be 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0d 0a 20  e pointer-map.. 
1f5bf 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74       ** for that
1f5c0 20 70 61 67 65 20 6e 6f 77 2e 20 0d 0a 20 20 20   page now. ..   
1f5c1 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20     **..      ** 
1f5c2 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  If this is the f
1f5c3 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  irst overflow pa
1f5c4 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  ge, then write a
1f5c5 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0d   partial entry .
1f5c6 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
1f5c7 20 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66   pointer-map. If
1f5c8 20 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e   we write nothin
1f5c9 67 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65  g to this pointe
1f5ca 72 2d 6d 61 70 20 73 6c 6f 74 2c 0d 0a 20 20 20  r-map slot,..   
1f5cb 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f     ** then the o
1f5cc 70 74 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c  ptimistic overfl
1f5cd 6f 77 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73  ow chain process
1f5ce 69 6e 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c  ing in clearCell
1f5cf 28 29 0d 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79  ()..      ** may
1f5d0 20 6d 69 73 69 6e 74 65 72 70 72 65 74 20 74 68   misinterpret th
1f5d1 65 20 75 6e 69 6e 69 74 69 61 6c 69 73 65 64 20  e uninitialised 
1f5d2 76 61 6c 75 65 73 20 61 6e 64 20 64 65 6c 65 74  values and delet
1f5d3 65 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  e the..      ** 
1f5d4 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 6f 6d  wrong pages from
1f5d5 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a   the database...
1f5d6 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
1f5d7 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
1f5d8 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  uum && rc==SQLIT
1f5d9 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
1f5da 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e   u8 eType = (pgn
1f5db 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f  oPtrmap?PTRMAP_O
1f5dc 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f  VERFLOW2:PTRMAP_
1f5dd 4f 56 45 52 46 4c 4f 57 31 29 3b 0d 0a 20 20 20  OVERFLOW1);..   
1f5de 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1f5df 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54  Bt, pgnoOvfl, eT
1f5e0 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c  ype, pgnoPtrmap,
1f5e1 20 26 72 63 29 3b 0d 0a 20 20 20 20 20 20 20 20   &rc);..        
1f5e2 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20 20  if( rc ){..     
1f5e3 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1f5e4 28 70 4f 76 66 6c 29 3b 0d 0a 20 20 20 20 20 20  (pOvfl);..      
1f5e5 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65    }..      }..#e
1f5e6 6e 64 69 66 0d 0a 20 20 20 20 20 20 69 66 28 20  ndif..      if( 
1f5e7 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  rc ){..        r
1f5e8 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 65  eleasePage(pToRe
1f5e9 6c 65 61 73 65 29 3b 0d 0a 20 20 20 20 20 20 20  lease);..       
1f5ea 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
1f5eb 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
1f5ec 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69   If pToRelease i
1f5ed 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20  s not zero than 
1f5ee 70 50 72 69 6f 72 20 70 6f 69 6e 74 73 20 69 6e  pPrior points in
1f5ef 74 6f 20 74 68 65 20 64 61 74 61 20 61 72 65 61  to the data area
1f5f0 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 70 54  ..      ** of pT
1f5f1 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20  oRelease.  Make 
1f5f2 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 20  sure pToRelease 
1f5f3 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62  is still writeab
1f5f4 6c 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73  le. */..      as
1f5f5 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65  sert( pToRelease
1f5f6 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61  ==0 || sqlite3Pa
1f5f7 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1f5f8 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61  ToRelease->pDbPa
1f5f9 67 65 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  ge) );....      
1f5fa 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20  /* If pPrior is 
1f5fb 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
1f5fc 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20   area of pPage, 
1f5fd 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70  then make sure p
1f5fe 50 61 67 65 0d 0a 20 20 20 20 20 20 2a 2a 20 69  Page..      ** i
1f5ff 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
1f600 65 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73 73 65  e */..      asse
1f601 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65  rt( pPrior<pPage
1f602 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f  ->aData || pPrio
1f603 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61  r>=&pPage->aData
1f604 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0d  [pBt->pageSize].
1f605 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1f606 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1f607 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
1f608 44 62 50 61 67 65 29 20 29 3b 0d 0a 0d 0a 20 20  DbPage) );....  
1f609 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50 72      put4byte(pPr
1f60a 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0d  ior, pgnoOvfl);.
1f60b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1f60c 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0d  ge(pToRelease);.
1f60d 0a 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73  .      pToReleas
1f60e 65 20 3d 20 70 4f 76 66 6c 3b 0d 0a 20 20 20 20  e = pOvfl;..    
1f60f 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c    pPrior = pOvfl
1f610 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 20 20 20 20  ->aData;..      
1f611 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c  put4byte(pPrior,
1f612 20 30 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 79   0);..      pPay
1f613 6c 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61  load = &pOvfl->a
1f614 44 61 74 61 5b 34 5d 3b 0d 0a 20 20 20 20 20 20  Data[4];..      
1f615 73 70 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d  spaceLeft = pBt-
1f616 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b  >usableSize - 4;
1f617 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 6e 20 3d  ..    }..    n =
1f618 20 6e 50 61 79 6c 6f 61 64 3b 0d 0a 20 20 20 20   nPayload;..    
1f619 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74 20  if( n>spaceLeft 
1f61a 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74 3b  ) n = spaceLeft;
1f61b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 70 54  ....    /* If pT
1f61c 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20  oRelease is not 
1f61d 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f  zero than pPaylo
1f61e 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74  ad points into t
1f61f 68 65 20 64 61 74 61 20 61 72 65 61 0d 0a 20 20  he data area..  
1f620 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61    ** of pToRelea
1f621 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70  se.  Make sure p
1f622 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69  ToRelease is sti
1f623 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f  ll writeable. */
1f624 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ..    assert( pT
1f625 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73  oRelease==0 || s
1f626 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1f627 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73  teable(pToReleas
1f628 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0d 0a  e->pDbPage) );..
1f629 0d 0a 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79  ..    /* If pPay
1f62a 6c 6f 61 64 20 69 73 20 70 61 72 74 20 6f 66 20  load is part of 
1f62b 74 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66  the data area of
1f62c 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b   pPage, then mak
1f62d 65 20 73 75 72 65 20 70 50 61 67 65 0d 0a 20 20  e sure pPage..  
1f62e 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72    ** is still wr
1f62f 69 74 65 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20  iteable */..    
1f630 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f 61 64  assert( pPayload
1f631 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c  <pPage->aData ||
1f632 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67   pPayload>=&pPag
1f633 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61  e->aData[pBt->pa
1f634 67 65 53 69 7a 65 5d 0d 0a 20 20 20 20 20 20 20  geSize]..       
1f635 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50       || sqlite3P
1f636 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1f637 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
1f638 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 6e 53  );....    if( nS
1f639 72 63 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 69  rc>0 ){..      i
1f63a 66 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20  f( n>nSrc ) n = 
1f63b 6e 53 72 63 3b 0d 0a 20 20 20 20 20 20 61 73 73  nSrc;..      ass
1f63c 65 72 74 28 20 70 53 72 63 20 29 3b 0d 0a 20 20  ert( pSrc );..  
1f63d 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c      memcpy(pPayl
1f63e 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29 3b 0d 0a  oad, pSrc, n);..
1f63f 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
1f640 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f 61    memset(pPayloa
1f641 64 2c 20 30 2c 20 6e 29 3b 0d 0a 20 20 20 20 7d  d, 0, n);..    }
1f642 0d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d  ..    nPayload -
1f643 3d 20 6e 3b 0d 0a 20 20 20 20 70 50 61 79 6c 6f  = n;..    pPaylo
1f644 61 64 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20 70 53  ad += n;..    pS
1f645 72 63 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20 6e 53  rc += n;..    nS
1f646 72 63 20 2d 3d 20 6e 3b 0d 0a 20 20 20 20 73 70  rc -= n;..    sp
1f647 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0d 0a 20  aceLeft -= n;.. 
1f648 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20 29     if( nSrc==0 )
1f649 7b 0d 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20  {..      nSrc = 
1f64a 6e 44 61 74 61 3b 0d 0a 20 20 20 20 20 20 70 53  nData;..      pS
1f64b 72 63 20 3d 20 70 44 61 74 61 3b 0d 0a 20 20 20  rc = pData;..   
1f64c 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 6c 65 61   }..  }..  relea
1f64d 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73  sePage(pToReleas
1f64e 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  e);..  return SQ
1f64f 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
1f650 2a 0d 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  *..** Remove the
1f651 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20   i-th cell from 
1f652 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75  pPage.  This rou
1f653 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61  tine effects pPa
1f654 67 65 20 6f 6e 6c 79 2e 0d 0a 2a 2a 20 54 68 65  ge only...** The
1f655 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73   cell content is
1f656 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65   not freed or de
1f657 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69  allocated.  It i
1f658 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0d 0a  s assumed that..
1f659 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** the cell cont
1f65a 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70  ent has been cop
1f65b 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c  ied someplace el
1f65c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
1f65d 65 20 6a 75 73 74 0d 0a 2a 2a 20 72 65 6d 6f 76  e just..** remov
1f65e 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  es the reference
1f65f 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f   to the cell fro
1f660 6d 20 70 50 61 67 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  m pPage...**..**
1f661 20 22 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68   "sz" must be th
1f662 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1f663 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0d 0a  s in the cell...
1f664 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
1f665 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65  dropCell(MemPage
1f666 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 78   *pPage, int idx
1f667 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 70  , int sz, int *p
1f668 52 43 29 7b 0d 0a 20 20 75 33 32 20 70 63 3b 20  RC){..  u32 pc; 
1f669 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1f66a 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  t to cell conten
1f66b 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20  t of cell being 
1f66c 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 75 38  deleted */..  u8
1f66d 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a   *data;       /*
1f66e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f   pPage->aData */
1f66f 0d 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20  ..  u8 *ptr;    
1f670 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d      /* Used to m
1f671 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64  ove bytes around
1f672 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a   within data[] *
1f673 2f 0d 0a 20 20 75 38 20 2a 65 6e 64 50 74 72 3b  /..  u8 *endPtr;
1f674 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 6c       /* End of l
1f675 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63  oop */..  int rc
1f676 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
1f677 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   return code */.
1f678 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
1f679 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20     /* Beginning 
1f67a 6f 66 20 74 68 65 20 68 65 61 64 65 72 2e 20 20  of the header.  
1f67b 30 20 6d 6f 73 74 20 70 61 67 65 73 2e 20 20 31  0 most pages.  1
1f67c 30 30 20 70 61 67 65 20 31 20 2a 2f 0d 0a 0d 0a  00 page 1 */....
1f67d 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74    if( *pRC ) ret
1f67e 75 72 6e 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  urn;....  assert
1f67f 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c  ( idx>=0 && idx<
1f680 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0d  pPage->nCell );.
1f681 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63  .  assert( sz==c
1f682 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69  ellSize(pPage, i
1f683 64 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  dx) );..  assert
1f684 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1f685 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
1f686 3e 70 44 62 50 61 67 65 29 20 29 3b 0d 0a 20 20  >pDbPage) );..  
1f687 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1f688 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1f689 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1f68a 0d 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ..  data = pPage
1f68b 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 70 74 72 20  ->aData;..  ptr 
1f68c 3d 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49  = &pPage->aCellI
1f68d 64 78 5b 32 2a 69 64 78 5d 3b 0d 0a 20 20 70 63  dx[2*idx];..  pc
1f68e 20 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 29   = get2byte(ptr)
1f68f 3b 0d 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65  ;..  hdr = pPage
1f690 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0d 0a 20 20  ->hdrOffset;..  
1f691 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 67 65  testcase( pc==ge
1f692 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
1f693 2b 35 5d 29 20 29 3b 0d 0a 20 20 74 65 73 74 63  +5]) );..  testc
1f694 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 61 67  ase( pc+sz==pPag
1f695 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
1f696 7a 65 20 29 3b 0d 0a 20 20 69 66 28 20 70 63 20  ze );..  if( pc 
1f697 3c 20 28 75 33 32 29 67 65 74 32 62 79 74 65 28  < (u32)get2byte(
1f698 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c  &data[hdr+5]) ||
1f699 20 70 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e   pc+sz > pPage->
1f69a 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1f69b 29 7b 0d 0a 20 20 20 20 2a 70 52 43 20 3d 20 53  ){..    *pRC = S
1f69c 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1f69d 50 54 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b  PT;..    return;
1f69e 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 66 72  ..  }..  rc = fr
1f69f 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 70  eeSpace(pPage, p
1f6a0 63 2c 20 73 7a 29 3b 0d 0a 20 20 69 66 28 20 72  c, sz);..  if( r
1f6a1 63 20 29 7b 0d 0a 20 20 20 20 2a 70 52 43 20 3d  c ){..    *pRC =
1f6a2 20 72 63 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e   rc;..    return
1f6a3 3b 0d 0a 20 20 7d 0d 0a 20 20 65 6e 64 50 74 72  ;..  }..  endPtr
1f6a4 20 3d 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c   = &pPage->aCell
1f6a5 49 64 78 5b 32 2a 70 50 61 67 65 2d 3e 6e 43 65  Idx[2*pPage->nCe
1f6a6 6c 6c 20 2d 20 32 5d 3b 0d 0a 20 20 61 73 73 65  ll - 2];..  asse
1f6a7 72 74 28 20 28 53 51 4c 49 54 45 5f 50 54 52 5f  rt( (SQLITE_PTR_
1f6a8 54 4f 5f 49 4e 54 28 70 74 72 29 26 31 29 3d 3d  TO_INT(ptr)&1)==
1f6a9 30 20 29 3b 20 20 2f 2a 20 70 74 72 20 69 73 20  0 );  /* ptr is 
1f6aa 61 6c 77 61 79 73 20 32 2d 62 79 74 65 20 61 6c  always 2-byte al
1f6ab 69 67 6e 65 64 20 2a 2f 0d 0a 20 20 77 68 69 6c  igned */..  whil
1f6ac 65 28 20 70 74 72 3c 65 6e 64 50 74 72 20 29 7b  e( ptr<endPtr ){
1f6ad 0d 0a 20 20 20 20 2a 28 75 31 36 2a 29 70 74 72  ..    *(u16*)ptr
1f6ae 20 3d 20 2a 28 75 31 36 2a 29 26 70 74 72 5b 32   = *(u16*)&ptr[2
1f6af 5d 3b 0d 0a 20 20 20 20 70 74 72 20 2b 3d 20 32  ];..    ptr += 2
1f6b0 3b 0d 0a 20 20 7d 0d 0a 20 20 70 50 61 67 65 2d  ;..  }..  pPage-
1f6b1 3e 6e 43 65 6c 6c 2d 2d 3b 0d 0a 20 20 70 75 74  >nCell--;..  put
1f6b2 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
1f6b3 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  3], pPage->nCell
1f6b4 29 3b 0d 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72  );..  pPage->nFr
1f6b5 65 65 20 2b 3d 20 32 3b 0d 0a 7d 0d 0a 0d 0a 2f  ee += 2;..}..../
1f6b6 2a 0d 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e  *..** Insert a n
1f6b7 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65  ew cell on pPage
1f6b8 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 22   at cell index "
1f6b9 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74  i".  pCell point
1f6ba 73 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 63 6f 6e  s to the..** con
1f6bb 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
1f6bc 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
1f6bd 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69   cell content wi
1f6be 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61  ll fit on the pa
1f6bf 67 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20  ge, then put it 
1f6c0 74 68 65 72 65 2e 20 20 49 66 20 69 74 0d 0a 2a  there.  If it..*
1f6c1 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20  * will not fit, 
1f6c2 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79  then make a copy
1f6c3 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   of the cell con
1f6c4 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20  tent into pTemp 
1f6c5 69 66 0d 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20  if..** pTemp is 
1f6c6 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72  not null.  Regar
1f6c7 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 2c 20  dless of pTemp, 
1f6c8 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65  allocate a new e
1f6c9 6e 74 72 79 0d 0a 2a 2a 20 69 6e 20 70 50 61 67  ntry..** in pPag
1f6ca 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d  e->aOvfl[] and m
1f6cb 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f 20  ake it point to 
1f6cc 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
1f6cd 20 28 65 69 74 68 65 72 0d 0a 2a 2a 20 69 6e 20   (either..** in 
1f6ce 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69  pTemp or the ori
1f6cf 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64  ginal pCell) and
1f6d0 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73   also record its
1f6d1 20 69 6e 64 65 78 2e 20 0d 0a 2a 2a 20 41 6c 6c   index. ..** All
1f6d2 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e  ocating a new en
1f6d3 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43  try in pPage->aC
1f6d4 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68  ell[] implies th
1f6d5 61 74 20 0d 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e  at ..** pPage->n
1f6d6 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72  Overflow is incr
1f6d7 65 6d 65 6e 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  emented...**..**
1f6d8 20 49 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e   If nSkip is non
1f6d9 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e  -zero, then do n
1f6da 6f 74 20 63 6f 70 79 20 74 68 65 20 66 69 72 73  ot copy the firs
1f6db 74 20 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66  t nSkip bytes of
1f6dc 20 74 68 65 0d 0a 2a 2a 20 63 65 6c 6c 2e 20 54   the..** cell. T
1f6dd 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f  he caller will o
1f6de 76 65 72 77 72 69 74 65 20 74 68 65 6d 20 61 66  verwrite them af
1f6df 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
1f6e0 6e 20 72 65 74 75 72 6e 73 2e 20 49 66 0d 0a 2a  n returns. If..*
1f6e1 2a 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a  * nSkip is non-z
1f6e2 65 72 6f 2c 20 74 68 65 6e 20 70 43 65 6c 6c 20  ero, then pCell 
1f6e3 6d 61 79 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f  may not point to
1f6e4 20 61 6e 20 69 6e 76 61 6c 69 64 20 6d 65 6d 6f   an invalid memo
1f6e5 72 79 20 6c 6f 63 61 74 69 6f 6e 20 0d 0a 2a 2a  ry location ..**
1f6e6 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e 53 6b 69   (but pCell+nSki
1f6e7 70 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69  p is always vali
1f6e8 64 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  d)...*/..static 
1f6e9 76 6f 69 64 20 69 6e 73 65 72 74 43 65 6c 6c 28  void insertCell(
1f6ea 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ..  MemPage *pPa
1f6eb 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e  ge,   /* Page in
1f6ec 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
1f6ed 63 6f 70 79 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e  copying */..  in
1f6ee 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20  t i,            
1f6ef 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f  /* New cell beco
1f6f0 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c  mes the i-th cel
1f6f1 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f  l of the page */
1f6f2 0d 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20  ..  u8 *pCell,  
1f6f3 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
1f6f4 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c   of the new cell
1f6f5 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 2c 20 20   */..  int sz,  
1f6f6 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
1f6f7 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20  s of content in 
1f6f8 70 43 65 6c 6c 20 2a 2f 0d 0a 20 20 75 38 20 2a  pCell */..  u8 *
1f6f9 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a  pTemp,        /*
1f6fa 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70   Temp storage sp
1f6fb 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69  ace for pCell, i
1f6fc 66 20 6e 65 65 64 65 64 20 2a 2f 0d 0a 20 20 50  f needed */..  P
1f6fd 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20  gno iChild,     
1f6fe 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c   /* If non-zero,
1f6ff 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 20 34   replace first 4
1f700 20 62 79 74 65 73 20 77 69 74 68 20 74 68 69 73   bytes with this
1f701 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 69 6e 74   value */..  int
1f702 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 2f   *pRC          /
1f703 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65  * Read and write
1f704 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   return code fro
1f705 6d 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  m here */..){.. 
1f706 20 69 6e 74 20 69 64 78 20 3d 20 30 3b 20 20 20   int idx = 0;   
1f707 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77     /* Where to w
1f708 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f  rite new cell co
1f709 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20  ntent in data[] 
1f70a 2a 2f 0d 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20  */..  int j;    
1f70b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1f70c 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  counter */..  in
1f70d 74 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20  t end;          
1f70e 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61  /* First byte pa
1f70f 73 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  st the last cell
1f710 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61   pointer in data
1f711 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 6e 73  [] */..  int ins
1f712 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
1f713 64 65 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68  dex in data[] wh
1f714 65 72 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69  ere new cell poi
1f715 6e 74 65 72 20 69 73 20 69 6e 73 65 72 74 65 64  nter is inserted
1f716 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 65 6c 6c 4f   */..  int cellO
1f717 66 66 73 65 74 3b 20 20 20 2f 2a 20 41 64 64 72  ffset;   /* Addr
1f718 65 73 73 20 6f 66 20 66 69 72 73 74 20 63 65 6c  ess of first cel
1f719 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74  l pointer in dat
1f71a 61 5b 5d 20 2a 2f 0d 0a 20 20 75 38 20 2a 64 61  a[] */..  u8 *da
1f71b 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ta;         /* T
1f71c 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
1f71d 65 20 77 68 6f 6c 65 20 70 61 67 65 20 2a 2f 0d  e whole page */.
1f71e 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20  .  u8 *ptr;     
1f71f 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72       /* Used for
1f720 20 6d 6f 76 69 6e 67 20 69 6e 66 6f 72 6d 61 74   moving informat
1f721 69 6f 6e 20 61 72 6f 75 6e 64 20 69 6e 20 64 61  ion around in da
1f722 74 61 5b 5d 20 2a 2f 0d 0a 20 20 75 38 20 2a 65  ta[] */..  u8 *e
1f723 6e 64 50 74 72 3b 20 20 20 20 20 20 20 2f 2a 20  ndPtr;       /* 
1f724 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  End of the loop 
1f725 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 6e 53 6b 69  */....  int nSki
1f726 70 20 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 20  p = (iChild ? 4 
1f727 3a 20 30 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 2a  : 0);....  if( *
1f728 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0d 0a 0d  pRC ) return;...
1f729 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1f72a 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65  && i<=pPage->nCe
1f72b 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  ll+pPage->nOverf
1f72c 6c 6f 77 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  low );..  assert
1f72d 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d  ( pPage->nCell<=
1f72e 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
1f72f 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70  Bt) && MX_CELL(p
1f730 50 61 67 65 2d 3e 70 42 74 29 3c 3d 31 30 39 32  Page->pBt)<=1092
1f731 31 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  1 );..  assert( 
1f732 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
1f733 3c 3d 41 72 72 61 79 53 69 7a 65 28 70 50 61 67  <=ArraySize(pPag
1f734 65 2d 3e 61 4f 76 66 6c 29 20 29 3b 0d 0a 20 20  e->aOvfl) );..  
1f735 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1f736 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1f737 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1f738 0d 0a 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20  ..  /* The cell 
1f739 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c 79 20  should normally 
1f73a 62 65 20 73 69 7a 65 64 20 63 6f 72 72 65 63 74  be sized correct
1f73b 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 77 68  ly.  However, wh
1f73c 65 6e 20 6d 6f 76 69 6e 67 20 61 0d 0a 20 20 2a  en moving a..  *
1f73d 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65 6c 6c  * malformed cell
1f73e 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70 61 67   from a leaf pag
1f73f 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f 72  e to an interior
1f740 20 70 61 67 65 2c 20 69 66 20 74 68 65 20 63 65   page, if the ce
1f741 6c 6c 20 73 69 7a 65 0d 0a 20 20 2a 2a 20 77 61  ll size..  ** wa
1f742 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20  nted to be less 
1f743 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20 72  than 4 but got r
1f744 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 6f  ounded up to 4 o
1f745 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65 6e  n the leaf, then
1f746 20 73 69 7a 65 0d 0a 20 20 2a 2a 20 6d 69 67 68   size..  ** migh
1f747 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38  t be less than 8
1f748 20 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f   (leaf-size + po
1f749 69 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e  inter) on the in
1f74a 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65  terior node.  He
1f74b 6e 63 65 0d 0a 20 20 2a 2a 20 74 68 65 20 74 65  nce..  ** the te
1f74c 72 6d 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20  rm after the || 
1f74d 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
1f74e 20 61 73 73 65 72 74 28 29 2e 20 2a 2f 0d 0a 20   assert(). */.. 
1f74f 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c   assert( sz==cel
1f750 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
1f751 70 43 65 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38  pCell) || (sz==8
1f752 20 26 26 20 69 43 68 69 6c 64 3e 30 29 20 29 3b   && iChild>0) );
1f753 0d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e  ..  if( pPage->n
1f754 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32  Overflow || sz+2
1f755 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b  >pPage->nFree ){
1f756 0d 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20  ..    if( pTemp 
1f757 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  ){..      memcpy
1f758 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43  (pTemp+nSkip, pC
1f759 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
1f75a 6b 69 70 29 3b 0d 0a 20 20 20 20 20 20 70 43 65  kip);..      pCe
1f75b 6c 6c 20 3d 20 70 54 65 6d 70 3b 0d 0a 20 20 20  ll = pTemp;..   
1f75c 20 7d 0d 0a 20 20 20 20 69 66 28 20 69 43 68 69   }..    if( iChi
1f75d 6c 64 20 29 7b 0d 0a 20 20 20 20 20 20 70 75 74  ld ){..      put
1f75e 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 43 68  4byte(pCell, iCh
1f75f 69 6c 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ild);..    }..  
1f760 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76    j = pPage->nOv
1f761 65 72 66 6c 6f 77 2b 2b 3b 0d 0a 20 20 20 20 61  erflow++;..    a
1f762 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 29 28 73  ssert( j<(int)(s
1f763 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76  izeof(pPage->aOv
1f764 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 61 67 65  fl)/sizeof(pPage
1f765 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 29 3b 0d  ->aOvfl[0])) );.
1f766 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66  .    pPage->aOvf
1f767 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 43 65  l[j].pCell = pCe
1f768 6c 6c 3b 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e  ll;..    pPage->
1f769 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 28  aOvfl[j].idx = (
1f76a 75 31 36 29 69 3b 0d 0a 20 20 7d 65 6c 73 65 7b  u16)i;..  }else{
1f76b 0d 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  ..    int rc = s
1f76c 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1f76d 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1f76e 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  ;..    if( rc!=S
1f76f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
1f770 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0d 0a 20     *pRC = rc;.. 
1f771 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20       return;..  
1f772 20 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28    }..    assert(
1f773 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
1f774 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
1f775 70 44 62 50 61 67 65 29 20 29 3b 0d 0a 20 20 20  pDbPage) );..   
1f776 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1f777 44 61 74 61 3b 0d 0a 20 20 20 20 63 65 6c 6c 4f  Data;..    cellO
1f778 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63  ffset = pPage->c
1f779 65 6c 6c 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20  ellOffset;..    
1f77a 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  end = cellOffset
1f77b 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c   + 2*pPage->nCel
1f77c 6c 3b 0d 0a 20 20 20 20 69 6e 73 20 3d 20 63 65  l;..    ins = ce
1f77d 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0d  llOffset + 2*i;.
1f77e 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  .    rc = alloca
1f77f 74 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 73  teSpace(pPage, s
1f780 7a 2c 20 26 69 64 78 29 3b 0d 0a 20 20 20 20 69  z, &idx);..    i
1f781 66 28 20 72 63 20 29 7b 20 2a 70 52 43 20 3d 20  f( rc ){ *pRC = 
1f782 72 63 3b 20 72 65 74 75 72 6e 3b 20 7d 0d 0a 20  rc; return; }.. 
1f783 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c 6f 63 61     /* The alloca
1f784 74 65 53 70 61 63 65 28 29 20 72 6f 75 74 69 6e  teSpace() routin
1f785 65 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 65  e guarantees the
1f786 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 70   following two p
1f787 72 6f 70 65 72 74 69 65 73 0d 0a 20 20 20 20 2a  roperties..    *
1f788 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 20  * if it returns 
1f789 73 75 63 63 65 73 73 20 2a 2f 0d 0a 20 20 20 20  success */..    
1f78a 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20 65  assert( idx >= e
1f78b 6e 64 2b 32 20 29 3b 0d 0a 20 20 20 20 61 73 73  nd+2 );..    ass
1f78c 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 28  ert( idx+sz <= (
1f78d 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e  int)pPage->pBt->
1f78e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0d 0a 20  usableSize );.. 
1f78f 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b     pPage->nCell+
1f790 2b 3b 0d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  +;..    pPage->n
1f791 46 72 65 65 20 2d 3d 20 28 75 31 36 29 28 32 20  Free -= (u16)(2 
1f792 2b 20 73 7a 29 3b 0d 0a 20 20 20 20 6d 65 6d 63  + sz);..    memc
1f793 70 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b  py(&data[idx+nSk
1f794 69 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70  ip], pCell+nSkip
1f795 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0d 0a 20 20  , sz-nSkip);..  
1f796 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0d    if( iChild ){.
1f797 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
1f798 26 64 61 74 61 5b 69 64 78 5d 2c 20 69 43 68 69  &data[idx], iChi
1f799 6c 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ld);..    }..   
1f79a 20 70 74 72 20 3d 20 26 64 61 74 61 5b 65 6e 64   ptr = &data[end
1f79b 5d 3b 0d 0a 20 20 20 20 65 6e 64 50 74 72 20 3d  ];..    endPtr =
1f79c 20 26 64 61 74 61 5b 69 6e 73 5d 3b 0d 0a 20 20   &data[ins];..  
1f79d 20 20 61 73 73 65 72 74 28 20 28 53 51 4c 49 54    assert( (SQLIT
1f79e 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 74 72  E_PTR_TO_INT(ptr
1f79f 29 26 31 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 70  )&1)==0 );  /* p
1f7a0 74 72 20 69 73 20 61 6c 77 61 79 73 20 32 2d 62  tr is always 2-b
1f7a1 79 74 65 20 61 6c 69 67 6e 65 64 20 2a 2f 0d 0a  yte aligned */..
1f7a2 20 20 20 20 77 68 69 6c 65 28 20 70 74 72 3e 65      while( ptr>e
1f7a3 6e 64 50 74 72 20 29 7b 0d 0a 20 20 20 20 20 20  ndPtr ){..      
1f7a4 2a 28 75 31 36 2a 29 70 74 72 20 3d 20 2a 28 75  *(u16*)ptr = *(u
1f7a5 31 36 2a 29 26 70 74 72 5b 2d 32 5d 3b 0d 0a 20  16*)&ptr[-2];.. 
1f7a6 20 20 20 20 20 70 74 72 20 2d 3d 20 32 3b 0d 0a       ptr -= 2;..
1f7a7 20 20 20 20 7d 0d 0a 20 20 20 20 70 75 74 32 62      }..    put2b
1f7a8 79 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20  yte(&data[ins], 
1f7a9 69 64 78 29 3b 0d 0a 20 20 20 20 70 75 74 32 62  idx);..    put2b
1f7aa 79 74 65 28 26 64 61 74 61 5b 70 50 61 67 65 2d  yte(&data[pPage-
1f7ab 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70  >hdrOffset+3], p
1f7ac 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0d 0a 23  Page->nCell);..#
1f7ad 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f7ae 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20  IT_AUTOVACUUM.. 
1f7af 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42     if( pPage->pB
1f7b0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
1f7b1 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ..      /* The c
1f7b2 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ell may contain 
1f7b3 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
1f7b4 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49  overflow page. I
1f7b5 66 20 73 6f 2c 20 77 72 69 74 65 0d 0a 20 20 20  f so, write..   
1f7b6 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20     ** the entry 
1f7b7 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  for the overflow
1f7b8 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70   page into the p
1f7b9 6f 69 6e 74 65 72 20 6d 61 70 2e 0d 0a 20 20 20  ointer map...   
1f7ba 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 70 74 72     */..      ptr
1f7bb 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50  mapPutOvflPtr(pP
1f7bc 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29  age, pCell, pRC)
1f7bd 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  ;..    }..#endif
1f7be 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
1f7bf 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 6f 66  ** Add a list of
1f7c0 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 67 65   cells to a page
1f7c1 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 6f 75  .  The page shou
1f7c2 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c 79 20  ld be initially 
1f7c3 65 6d 70 74 79 2e 0d 0a 2a 2a 20 54 68 65 20 63  empty...** The c
1f7c4 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e 74  ells are guarant
1f7c5 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 68  eed to fit on th
1f7c6 65 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  e page...*/..sta
1f7c7 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c  tic void assembl
1f7c8 65 50 61 67 65 28 0d 0a 20 20 4d 65 6d 50 61 67  ePage(..  MemPag
1f7c9 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54  e *pPage,   /* T
1f7ca 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 73  he page to be as
1f7cb 73 65 6d 62 6c 69 65 64 20 2a 2f 0d 0a 20 20 69  semblied */..  i
1f7cc 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20  nt nCell,       
1f7cd 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
1f7ce 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74  f cells to add t
1f7cf 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0d 0a  o this page */..
1f7d0 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20    u8 **apCell,  
1f7d1 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
1f7d2 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a  to cell bodies *
1f7d3 2f 0d 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20  /..  u16 *aSize 
1f7d4 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20         /* Sizes 
1f7d5 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0d  of the cells */.
1f7d6 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20  .){..  int i;   
1f7d7 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1f7d8 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 75   counter */..  u
1f7d9 38 20 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20  8 *pCellptr;    
1f7da 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
1f7db 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  ext cell pointer
1f7dc 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 65 6c 6c 62   */..  int cellb
1f7dd 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72  ody;     /* Addr
1f7de 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c  ess of next cell
1f7df 20 62 6f 64 79 20 2a 2f 0d 0a 20 20 75 38 20 2a   body */..  u8 *
1f7e0 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50   const data = pP
1f7e1 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20  age->aData;     
1f7e2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1f7e3 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 70  er to data for p
1f7e4 50 61 67 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  Page */..  const
1f7e5 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65   int hdr = pPage
1f7e6 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20  ->hdrOffset;    
1f7e7 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1f7e8 20 6f 66 20 68 65 61 64 65 72 20 6f 6e 20 70 50   of header on pP
1f7e9 61 67 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  age */..  const 
1f7ea 69 6e 74 20 6e 55 73 61 62 6c 65 20 3d 20 70 50  int nUsable = pP
1f7eb 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1f7ec 53 69 7a 65 3b 20 2f 2a 20 55 73 61 62 6c 65 20  Size; /* Usable 
1f7ed 73 69 7a 65 20 6f 66 20 70 61 67 65 20 2a 2f 0d  size of page */.
1f7ee 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ...  assert( pPa
1f7ef 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ge->nOverflow==0
1f7f0 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
1f7f1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1f7f2 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
1f7f3 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
1f7f4 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20 6e  t( nCell>=0 && n
1f7f5 43 65 6c 6c 3c 3d 28 69 6e 74 29 4d 58 5f 43 45  Cell<=(int)MX_CE
1f7f6 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 0d 0a  LL(pPage->pBt)..
1f7f7 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
1f7f8 69 6e 74 29 4d 58 5f 43 45 4c 4c 28 70 50 61 67  int)MX_CELL(pPag
1f7f9 65 2d 3e 70 42 74 29 3c 3d 31 30 39 32 31 29 3b  e->pBt)<=10921);
1f7fa 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1f7fb 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1f7fc 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
1f7fd 67 65 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43  ge) );....  /* C
1f7fe 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61  heck that the pa
1f7ff 67 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ge has just been
1f800 20 7a 65 72 6f 65 64 20 62 79 20 7a 65 72 6f 50   zeroed by zeroP
1f801 61 67 65 28 29 20 2a 2f 0d 0a 20 20 61 73 73 65  age() */..  asse
1f802 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  rt( pPage->nCell
1f803 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ==0 );..  assert
1f804 28 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72  ( get2byteNotZer
1f805 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d  o(&data[hdr+5])=
1f806 3d 6e 55 73 61 62 6c 65 20 29 3b 0d 0a 0d 0a 20  =nUsable );.... 
1f807 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 70 50 61   pCellptr = &pPa
1f808 67 65 2d 3e 61 43 65 6c 6c 49 64 78 5b 6e 43 65  ge->aCellIdx[nCe
1f809 6c 6c 2a 32 5d 3b 0d 0a 20 20 63 65 6c 6c 62 6f  ll*2];..  cellbo
1f80a 64 79 20 3d 20 6e 55 73 61 62 6c 65 3b 0d 0a 20  dy = nUsable;.. 
1f80b 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 31 3b 20   for(i=nCell-1; 
1f80c 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0d 0a 20 20 20  i>=0; i--){..   
1f80d 20 75 31 36 20 73 7a 20 3d 20 61 53 69 7a 65 5b   u16 sz = aSize[
1f80e 69 5d 3b 0d 0a 20 20 20 20 70 43 65 6c 6c 70 74  i];..    pCellpt
1f80f 72 20 2d 3d 20 32 3b 0d 0a 20 20 20 20 63 65 6c  r -= 2;..    cel
1f810 6c 62 6f 64 79 20 2d 3d 20 73 7a 3b 0d 0a 20 20  lbody -= sz;..  
1f811 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c    put2byte(pCell
1f812 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0d  ptr, cellbody);.
1f813 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74  .    memcpy(&dat
1f814 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43  a[cellbody], apC
1f815 65 6c 6c 5b 69 5d 2c 20 73 7a 29 3b 0d 0a 20 20  ell[i], sz);..  
1f816 7d 0d 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  }..  put2byte(&d
1f817 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 6c  ata[hdr+3], nCel
1f818 6c 29 3b 0d 0a 20 20 70 75 74 32 62 79 74 65 28  l);..  put2byte(
1f819 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 65  &data[hdr+5], ce
1f81a 6c 6c 62 6f 64 79 29 3b 0d 0a 20 20 70 50 61 67  llbody);..  pPag
1f81b 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 65  e->nFree -= (nCe
1f81c 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 2d  ll*2 + nUsable -
1f81d 20 63 65 6c 6c 62 6f 64 79 29 3b 0d 0a 20 20 70   cellbody);..  p
1f81e 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75  Page->nCell = (u
1f81f 31 36 29 6e 43 65 6c 6c 3b 0d 0a 7d 0d 0a 0d 0a  16)nCell;..}....
1f820 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  /*..** The follo
1f821 77 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20  wing parameters 
1f822 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61  determine how ma
1f823 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65  ny adjacent page
1f824 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0d 0a  s get involved..
1f825 2a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e  ** in a balancin
1f826 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e  g operation.  NN
1f827 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
1f828 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65  f neighbors on e
1f829 69 74 68 65 72 20 73 69 64 65 0d 0a 2a 2a 20 6f  ither side..** o
1f82a 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  f the page that 
1f82b 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74  participate in t
1f82c 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65  he balancing ope
1f82d 72 61 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74  ration.  NB is t
1f82e 68 65 0d 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d  he..** total num
1f82f 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61  ber of pages tha
1f830 74 20 70 61 72 74 69 63 69 70 61 74 65 2c 20 69  t participate, i
1f831 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 61 72  ncluding the tar
1f832 67 65 74 20 70 61 67 65 20 61 6e 64 0d 0a 2a 2a  get page and..**
1f833 20 4e 4e 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e   NN neighbors on
1f834 20 65 69 74 68 65 72 20 73 69 64 65 2e 0d 0a 2a   either side...*
1f835 2a 0d 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75  *..** The minimu
1f836 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 69 73  m value of NN is
1f837 20 31 20 28 6f 66 20 63 6f 75 72 73 65 29 2e 20   1 (of course). 
1f838 20 49 6e 63 72 65 61 73 69 6e 67 20 4e 4e 20 61   Increasing NN a
1f839 62 6f 76 65 20 31 0d 0a 2a 2a 20 28 74 6f 20 32  bove 1..** (to 2
1f83a 20 6f 72 20 33 29 20 67 69 76 65 73 20 61 20 6d   or 3) gives a m
1f83b 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 6d 65 6e  odest improvemen
1f83c 74 20 69 6e 20 53 45 4c 45 43 54 20 61 6e 64 20  t in SELECT and 
1f83d 44 45 4c 45 54 45 20 70 65 72 66 6f 72 6d 61 6e  DELETE performan
1f83e 63 65 0d 0a 2a 2a 20 69 6e 20 65 78 63 68 61 6e  ce..** in exchan
1f83f 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20  ge for a larger 
1f840 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e 20 49  degradation in I
1f841 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41 54 45  NSERT and UPDATE
1f842 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0d 0a 2a   performance...*
1f843 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e  * The value of N
1f844 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69 76  N appears to giv
1f845 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75 6c  e the best resul
1f846 74 73 20 6f 76 65 72 61 6c 6c 2e 0d 0a 2a 2f 0d  ts overall...*/.
1f847 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20  .#define NN 1   
1f848 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1f849 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73  ber of neighbors
1f84a 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20   on either side 
1f84b 6f 66 20 70 50 61 67 65 20 2a 2f 0d 0a 23 64 65  of pPage */..#de
1f84c 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
1f84d 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
1f84e 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
1f84f 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0d   the balance */.
1f850 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .....#ifndef SQL
1f851 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41  ITE_OMIT_QUICKBA
1f852 4c 41 4e 43 45 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  LANCE../*..** Th
1f853 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61  is version of ba
1f854 6c 61 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20  lance() handles 
1f855 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69  the common speci
1f856 61 6c 20 63 61 73 65 20 77 68 65 72 65 0d 0a 2a  al case where..*
1f857 2a 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 73  * a new entry is
1f858 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
1f859 6f 6e 20 74 68 65 20 65 78 74 72 65 6d 65 20 72  on the extreme r
1f85a 69 67 68 74 2d 65 6e 64 20 6f 66 20 74 68 65 0d  ight-end of the.
1f85b 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f 74 68  .** tree, in oth
1f85c 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e 20 74  er words, when t
1f85d 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 69 6c  he new entry wil
1f85e 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c 61 72  l become the lar
1f85f 67 65 73 74 0d 0a 2a 2a 20 65 6e 74 72 79 20 69  gest..** entry i
1f860 6e 20 74 68 65 20 74 72 65 65 2e 0d 0a 2a 2a 0d  n the tree...**.
1f861 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74  .** Instead of t
1f862 72 79 69 6e 67 20 74 6f 20 62 61 6c 61 6e 63 65  rying to balance
1f863 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73   the 3 right-mos
1f864 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75  t leaf pages, ju
1f865 73 74 20 61 64 64 0d 0a 2a 2a 20 61 20 6e 65 77  st add..** a new
1f866 20 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67   page to the rig
1f867 68 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64  ht-hand side and
1f868 20 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77   put the one new
1f869 20 65 6e 74 72 79 20 69 6e 0d 0a 2a 2a 20 74 68   entry in..** th
1f86a 61 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c  at page.  This l
1f86b 65 61 76 65 73 20 74 68 65 20 72 69 67 68 74 20  eaves the right 
1f86c 73 69 64 65 20 6f 66 20 74 68 65 20 74 72 65 65  side of the tree
1f86d 20 73 6f 6d 65 77 68 61 74 0d 0a 2a 2a 20 75 6e   somewhat..** un
1f86e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 74 20 6f  balanced.  But o
1f86f 64 64 73 20 61 72 65 20 74 68 61 74 20 77 65 20  dds are that we 
1f870 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e  will be insertin
1f871 67 20 6e 65 77 20 65 6e 74 72 69 65 73 0d 0a 2a  g new entries..*
1f872 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 6f 6f  * at the end soo
1f873 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 6f 20  n afterwards so 
1f874 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79  the nearly empty
1f875 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 63 6b   page will quick
1f876 6c 79 0d 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20  ly..** fill up. 
1f877 20 4f 6e 20 61 76 65 72 61 67 65 2e 0d 0a 2a 2a   On average...**
1f878 0d 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68  ..** pPage is th
1f879 65 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63  e leaf page whic
1f87a 68 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d  h is the right-m
1f87b 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ost page in the 
1f87c 74 72 65 65 2e 0d 0a 2a 2a 20 70 50 61 72 65 6e  tree...** pParen
1f87d 74 20 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e  t is its parent.
1f87e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 61 76    pPage must hav
1f87f 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66  e a single overf
1f880 6c 6f 77 20 65 6e 74 72 79 0d 0a 2a 2a 20 77 68  low entry..** wh
1f881 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20  ich is also the 
1f882 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
1f883 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0d 0a 2a   on the page...*
1f884 2a 0d 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65  *..** The pSpace
1f885 20 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20   buffer is used 
1f886 74 6f 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f  to store a tempo
1f887 72 61 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65  rary copy of the
1f888 20 64 69 76 69 64 65 72 0d 0a 2a 2a 20 63 65 6c   divider..** cel
1f889 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69  l that will be i
1f88a 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 70 50 61  nserted into pPa
1f88b 72 65 6e 74 2e 20 53 75 63 68 20 61 20 63 65 6c  rent. Such a cel
1f88c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  l consists of a 
1f88d 34 0d 0a 2a 2a 20 62 79 74 65 20 70 61 67 65 20  4..** byte page 
1f88e 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f 77 65 64 20  number followed 
1f88f 62 79 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65  by a variable le
1f890 6e 67 74 68 20 69 6e 74 65 67 65 72 2e 20 49 6e  ngth integer. In
1f891 20 6f 74 68 65 72 0d 0a 2a 2a 20 77 6f 72 64 73   other..** words
1f892 2c 20 61 74 20 6d 6f 73 74 20 31 33 20 62 79 74  , at most 13 byt
1f893 65 73 2e 20 48 65 6e 63 65 20 74 68 65 20 70 53  es. Hence the pS
1f894 70 61 63 65 20 62 75 66 66 65 72 20 6d 75 73 74  pace buffer must
1f895 20 62 65 20 61 74 0d 0a 2a 2a 20 6c 65 61 73 74   be at..** least
1f896 20 31 33 20 62 79 74 65 73 20 69 6e 20 73 69 7a   13 bytes in siz
1f897 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
1f898 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  nt balance_quick
1f899 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e  (MemPage *pParen
1f89a 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  t, MemPage *pPag
1f89b 65 2c 20 75 38 20 2a 70 53 70 61 63 65 29 7b 0d  e, u8 *pSpace){.
1f89c 0a 20 20 42 74 53 68 61 72 65 64 20 2a 63 6f 6e  .  BtShared *con
1f89d 73 74 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  st pBt = pPage->
1f89e 70 42 74 3b 20 20 20 20 2f 2a 20 42 2d 54 72 65  pBt;    /* B-Tre
1f89f 65 20 44 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20  e Database */.. 
1f8a0 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 20   MemPage *pNew; 
1f8a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8a2 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c 79 20 61        /* Newly a
1f8a3 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f  llocated page */
1f8a4 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
1f8a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8a6 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
1f8a7 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a 20 20 50 67  rn Code */..  Pg
1f8a8 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 20  no pgnoNew;     
1f8a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8aa 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
1f8ab 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0d 0a 0d 0a  r of pNew */....
1f8ac 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1f8ad 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1f8ae 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
1f8af 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
1f8b0 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1f8b1 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70  eable(pParent->p
1f8b2 44 62 50 61 67 65 29 20 29 3b 0d 0a 20 20 61 73  DbPage) );..  as
1f8b3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
1f8b4 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0d 0a 0d 0a  erflow==1 );....
1f8b5 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20    /* This error 
1f8b6 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 6e 6f 77  condition is now
1f8b7 20 63 61 75 67 68 74 20 70 72 69 6f 72 20 74 6f   caught prior to
1f8b8 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 66   reaching this f
1f8b9 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 66  unction */..  if
1f8ba 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d  ( pPage->nCell<=
1f8bb 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
1f8bc 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d  E_CORRUPT_BKPT;.
1f8bd 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ...  /* Allocate
1f8be 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69   a new page. Thi
1f8bf 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f  s page will beco
1f8c0 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62  me the right-sib
1f8c1 6c 69 6e 67 20 6f 66 20 0d 0a 20 20 2a 2a 20 70  ling of ..  ** p
1f8c2 50 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70  Page. Make the p
1f8c3 61 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61  arent page writa
1f8c4 62 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65  ble, so that the
1f8c5 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c   new divider cel
1f8c6 6c 0d 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69  l..  ** may be i
1f8c7 6e 73 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68  nserted. If both
1f8c8 20 74 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e   these operation
1f8c9 73 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c  s are successful
1f8ca 2c 20 70 72 6f 63 65 65 64 2e 0d 0a 20 20 2a 2f  , proceed...  */
1f8cb 0d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  ..  rc = allocat
1f8cc 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
1f8cd 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c  &pNew, &pgnoNew,
1f8ce 20 30 2c 20 30 29 3b 0d 0a 0d 0a 20 20 69 66 28   0, 0);....  if(
1f8cf 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1f8d0 7b 0d 0a 0d 0a 20 20 20 20 75 38 20 2a 70 4f 75  {....    u8 *pOu
1f8d1 74 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0d  t = &pSpace[4];.
1f8d2 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d  .    u8 *pCell =
1f8d3 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d   pPage->aOvfl[0]
1f8d4 2e 70 43 65 6c 6c 3b 0d 0a 20 20 20 20 75 31 36  .pCell;..    u16
1f8d5 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69   szCell = cellSi
1f8d6 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  zePtr(pPage, pCe
1f8d7 6c 6c 29 3b 0d 0a 20 20 20 20 75 38 20 2a 70 53  ll);..    u8 *pS
1f8d8 74 6f 70 3b 0d 0a 0d 0a 20 20 20 20 61 73 73 65  top;....    asse
1f8d9 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
1f8da 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77  Iswriteable(pNew
1f8db 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0d 0a 20  ->pDbPage) );.. 
1f8dc 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
1f8dd 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46  ->aData[0]==(PTF
1f8de 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46  _INTKEY|PTF_LEAF
1f8df 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29  DATA|PTF_LEAF) )
1f8e0 3b 0d 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  ;..    zeroPage(
1f8e1 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59  pNew, PTF_INTKEY
1f8e2 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54  |PTF_LEAFDATA|PT
1f8e3 46 5f 4c 45 41 46 29 3b 0d 0a 20 20 20 20 61 73  F_LEAF);..    as
1f8e4 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c  semblePage(pNew,
1f8e5 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43   1, &pCell, &szC
1f8e6 65 6c 6c 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  ell);....    /* 
1f8e7 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  If this is an au
1f8e8 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
1f8e9 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70  se, update the p
1f8ea 6f 69 6e 74 65 72 20 6d 61 70 0d 0a 20 20 20 20  ointer map..    
1f8eb 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 20  ** with entries 
1f8ec 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65  for the new page
1f8ed 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65  , and any pointe
1f8ee 72 20 66 72 6f 6d 20 74 68 65 20 0d 0a 20 20 20  r from the ..   
1f8ef 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20   ** cell on the 
1f8f0 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66  page to an overf
1f8f1 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74  low page. If eit
1f8f2 68 65 72 20 6f 66 20 74 68 65 73 65 0d 0a 20 20  her of these..  
1f8f3 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20    ** operations 
1f8f4 66 61 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72  fails, the retur
1f8f5 6e 20 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62  n code is set, b
1f8f6 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0d  ut the contents.
1f8f7 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70  .    ** of the p
1f8f8 61 72 65 6e 74 20 70 61 67 65 20 61 72 65 20 73  arent page are s
1f8f9 74 69 6c 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64  till manipulated
1f8fa 20 62 79 20 74 68 68 20 63 6f 64 65 20 62 65 6c   by thh code bel
1f8fb 6f 77 2e 0d 0a 20 20 20 20 2a 2a 20 54 68 61 74  ow...    ** That
1f8fc 20 69 73 20 4f 6b 2c 20 61 74 20 74 68 69 73 20   is Ok, at this 
1f8fd 70 6f 69 6e 74 20 74 68 65 20 70 61 72 65 6e 74  point the parent
1f8fe 20 70 61 67 65 20 69 73 20 67 75 61 72 61 6e 74   page is guarant
1f8ff 65 65 64 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 62  eed to..    ** b
1f900 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
1f901 79 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20  y. Returning an 
1f902 65 72 72 6f 72 20 63 6f 64 65 20 77 69 6c 6c 20  error code will 
1f903 63 61 75 73 65 20 61 0d 0a 20 20 20 20 2a 2a 20  cause a..    ** 
1f904 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e 64 6f 69 6e  rollback, undoin
1f905 67 20 61 6e 79 20 63 68 61 6e 67 65 73 20 6d 61  g any changes ma
1f906 64 65 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  de to the parent
1f907 20 70 61 67 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a   page...    */..
1f908 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
1f909 43 55 55 4d 20 29 7b 0d 0a 20 20 20 20 20 20 70  CUUM ){..      p
1f90a 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67  trmapPut(pBt, pg
1f90b 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 54  noNew, PTRMAP_BT
1f90c 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67  REE, pParent->pg
1f90d 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20 20  no, &rc);..     
1f90e 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77   if( szCell>pNew
1f90f 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0d 0a 20  ->minLocal ){.. 
1f910 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74         ptrmapPut
1f911 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43  OvflPtr(pNew, pC
1f912 65 6c 6c 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  ell, &rc);..    
1f913 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a    }..    }..  ..
1f914 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
1f915 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20  divider cell to 
1f916 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72  insert into pPar
1f917 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64 65 72  ent. The divider
1f918 20 63 65 6c 6c 0d 0a 20 20 20 20 2a 2a 20 63 6f   cell..    ** co
1f919 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62 79  nsists of a 4-by
1f91a 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28  te page number (
1f91b 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
1f91c 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0d 0a 20  of pPage) and.. 
1f91d 20 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65     ** a variable
1f91e 20 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75   length key valu
1f91f 65 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65  e (which must be
1f920 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
1f921 61 73 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 6c  as the..    ** l
1f922 61 72 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50  argest key on pP
1f923 61 67 65 29 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  age)...    **.. 
1f924 20 20 20 2a 2a 20 54 6f 20 66 69 6e 64 20 74 68     ** To find th
1f925 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 76 61  e largest key va
1f926 6c 75 65 20 6f 6e 20 70 50 61 67 65 2c 20 66 69  lue on pPage, fi
1f927 72 73 74 20 66 69 6e 64 20 74 68 65 20 72 69 67  rst find the rig
1f928 68 74 2d 6d 6f 73 74 20 0d 0a 20 20 20 20 2a 2a  ht-most ..    **
1f929 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 2e 20   cell on pPage. 
1f92a 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 66 69  The first two fi
1f92b 65 6c 64 73 20 6f 66 20 74 68 69 73 20 63 65 6c  elds of this cel
1f92c 6c 20 61 72 65 20 74 68 65 20 0d 0a 20 20 20 20  l are the ..    
1f92d 2a 2a 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68  ** record-length
1f92e 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e   (a variable len
1f92f 67 74 68 20 69 6e 74 65 67 65 72 20 61 74 20 6d  gth integer at m
1f930 6f 73 74 20 33 32 2d 62 69 74 73 20 69 6e 20 73  ost 32-bits in s
1f931 69 7a 65 29 0d 0a 20 20 20 20 2a 2a 20 61 6e 64  ize)..    ** and
1f932 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 28   the key value (
1f933 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  a variable lengt
1f934 68 20 69 6e 74 65 67 65 72 2c 20 6d 61 79 20 68  h integer, may h
1f935 61 76 65 20 61 6e 79 20 76 61 6c 75 65 29 2e 0d  ave any value)..
1f936 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73  .    ** The firs
1f937 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65 28 2e  t of the while(.
1f938 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f 77 20  ..) loops below 
1f939 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65 20 72  skips over the r
1f93a 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0d 0a 20 20  ecord-length..  
1f93b 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65 20    ** field. The 
1f93c 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e 2e  second while(...
1f93d 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74 68  ) loop copies th
1f93e 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f 6d  e key value from
1f93f 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 63 65 6c   the..    ** cel
1f940 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20  l on pPage into 
1f941 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
1f942 72 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  r...    */..    
1f943 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
1f944 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e  (pPage, pPage->n
1f945 43 65 6c 6c 2d 31 29 3b 0d 0a 20 20 20 20 70 53  Cell-1);..    pS
1f946 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b  top = &pCell[9];
1f947 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28  ..    while( (*(
1f948 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26  pCell++)&0x80) &
1f949 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b  & pCell<pStop );
1f94a 0d 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70  ..    pStop = &p
1f94b 43 65 6c 6c 5b 39 5d 3b 0d 0a 20 20 20 20 77 68  Cell[9];..    wh
1f94c 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29  ile( ((*(pOut++)
1f94d 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30   = *(pCell++))&0
1f94e 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 53  x80) && pCell<pS
1f94f 74 6f 70 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  top );....    /*
1f950 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20   Insert the new 
1f951 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74  divider cell int
1f952 6f 20 70 50 61 72 65 6e 74 2e 20 2a 2f 0d 0a 20  o pParent. */.. 
1f953 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50     insertCell(pP
1f954 61 72 65 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e  arent, pParent->
1f955 6e 43 65 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28  nCell, pSpace, (
1f956 69 6e 74 29 28 70 4f 75 74 2d 70 53 70 61 63 65  int)(pOut-pSpace
1f957 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ),..            
1f958 20 20 20 30 2c 20 70 50 61 67 65 2d 3e 70 67 6e     0, pPage->pgn
1f959 6f 2c 20 26 72 63 29 3b 0d 0a 0d 0a 20 20 20 20  o, &rc);....    
1f95a 2f 2a 20 53 65 74 20 74 68 65 20 72 69 67 68 74  /* Set the right
1f95b 2d 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 6f  -child pointer o
1f95c 66 20 70 50 61 72 65 6e 74 20 74 6f 20 70 6f 69  f pParent to poi
1f95d 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61  nt to the new pa
1f95e 67 65 2e 20 2a 2f 0d 0a 20 20 20 20 70 75 74 34  ge. */..    put4
1f95f 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61  byte(&pParent->a
1f960 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64  Data[pParent->hd
1f961 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f  rOffset+8], pgno
1f962 4e 65 77 29 3b 0d 0a 20 20 0d 0a 20 20 20 20 2f  New);..  ..    /
1f963 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 65  * Release the re
1f964 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6e  ference to the n
1f965 65 77 20 70 61 67 65 2e 20 2a 2f 0d 0a 20 20 20  ew page. */..   
1f966 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65   releasePage(pNe
1f967 77 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65  w);..  }....  re
1f968 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 23 65 6e  turn rc;..}..#en
1f969 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1f96a 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20  IT_QUICKBALANCE 
1f96b 2a 2f 0d 0a 0d 0a 23 69 66 20 30 0d 0a 2f 2a 0d  */....#if 0../*.
1f96c 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1f96d 6e 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72  n does not contr
1f96e 69 62 75 74 65 20 61 6e 79 74 68 69 6e 67 20 74  ibute anything t
1f96f 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  o the operation 
1f970 6f 66 20 53 51 4c 69 74 65 2e 0d 0a 2a 2a 20 69  of SQLite...** i
1f971 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20 61  t is sometimes a
1f972 63 74 69 76 61 74 65 64 20 74 65 6d 70 6f 72 61  ctivated tempora
1f973 72 69 6c 79 20 77 68 69 6c 65 20 64 65 62 75 67  rily while debug
1f974 67 69 6e 67 20 63 6f 64 65 20 72 65 73 70 6f 6e  ging code respon
1f975 73 69 62 6c 65 20 0d 0a 2a 2a 20 66 6f 72 20 73  sible ..** for s
1f976 65 74 74 69 6e 67 20 70 6f 69 6e 74 65 72 2d 6d  etting pointer-m
1f977 61 70 20 65 6e 74 72 69 65 73 2e 0d 0a 2a 2f 0d  ap entries...*/.
1f978 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d  .static int ptrm
1f979 61 70 43 68 65 63 6b 50 61 67 65 73 28 4d 65 6d  apCheckPages(Mem
1f97a 50 61 67 65 20 2a 2a 61 70 50 61 67 65 2c 20 69  Page **apPage, i
1f97b 6e 74 20 6e 50 61 67 65 29 7b 0d 0a 20 20 69 6e  nt nPage){..  in
1f97c 74 20 69 2c 20 6a 3b 0d 0a 20 20 66 6f 72 28 69  t i, j;..  for(i
1f97d 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b 2b  =0; i<nPage; i++
1f97e 29 7b 0d 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0d  ){..    Pgno n;.
1f97f 0a 20 20 20 20 75 38 20 65 3b 0d 0a 20 20 20 20  .    u8 e;..    
1f980 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
1f981 20 61 70 50 61 67 65 5b 69 5d 3b 0d 0a 20 20 20   apPage[i];..   
1f982 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1f983 20 70 50 61 67 65 2d 3e 70 42 74 3b 0d 0a 20 20   pPage->pBt;..  
1f984 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1f985 3e 69 73 49 6e 69 74 20 29 3b 0d 0a 0d 0a 20 20  >isInit );....  
1f986 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61    for(j=0; j<pPa
1f987 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b  ge->nCell; j++){
1f988 0d 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f  ..      CellInfo
1f989 20 69 6e 66 6f 3b 0d 0a 20 20 20 20 20 20 75 38   info;..      u8
1f98a 20 2a 7a 3b 0d 0a 20 20 20 20 20 0d 0a 20 20 20   *z;..     ..   
1f98b 20 20 20 7a 20 3d 20 66 69 6e 64 43 65 6c 6c 28     z = findCell(
1f98c 70 50 61 67 65 2c 20 6a 29 3b 0d 0a 20 20 20 20  pPage, j);..    
1f98d 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
1f98e 50 74 72 28 70 50 61 67 65 2c 20 7a 2c 20 26 69  Ptr(pPage, z, &i
1f98f 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  nfo);..      if(
1f990 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20   info.iOverflow 
1f991 29 7b 0d 0a 20 20 20 20 20 20 20 20 50 67 6e 6f  ){..        Pgno
1f992 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65   ovfl = get4byte
1f993 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c  (&z[info.iOverfl
1f994 6f 77 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  ow]);..        p
1f995 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6f 76  trmapGet(pBt, ov
1f996 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0d 0a 20 20  fl, &e, &n);..  
1f997 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d        assert( n=
1f998 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20  =pPage->pgno && 
1f999 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
1f99a 4f 57 31 20 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  OW1 );..      }.
1f99b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67  .      if( !pPag
1f99c 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20 20 20 20  e->leaf ){..    
1f99d 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d      Pgno child =
1f99e 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0d 0a 20   get4byte(z);.. 
1f99f 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74         ptrmapGet
1f9a0 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c  (pBt, child, &e,
1f9a1 20 26 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 61   &n);..        a
1f9a2 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d  ssert( n==pPage-
1f9a3 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d  >pgno && e==PTRM
1f9a4 41 50 5f 42 54 52 45 45 20 29 3b 0d 0a 20 20 20  AP_BTREE );..   
1f9a5 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
1f9a6 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
1f9a7 66 20 29 7b 0d 0a 20 20 20 20 20 20 50 67 6e 6f  f ){..      Pgno
1f9a8 20 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74   child = get4byt
1f9a9 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
1f9aa 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
1f9ab 2b 38 5d 29 3b 0d 0a 20 20 20 20 20 20 70 74 72  +8]);..      ptr
1f9ac 6d 61 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c  mapGet(pBt, chil
1f9ad 64 2c 20 26 65 2c 20 26 6e 29 3b 0d 0a 20 20 20  d, &e, &n);..   
1f9ae 20 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50     assert( n==pP
1f9af 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d  age->pgno && e==
1f9b0 50 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0d  PTRMAP_BTREE );.
1f9b1 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
1f9b2 65 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 23 65 6e  eturn 1;..}..#en
1f9b3 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
1f9b4 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
1f9b5 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20  sed to copy the 
1f9b6 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
1f9b7 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72  b-tree node stor
1f9b8 65 64 20 0d 0a 2a 2a 20 6f 6e 20 70 61 67 65 20  ed ..** on page 
1f9b9 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54  pFrom to page pT
1f9ba 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d  o. If page pFrom
1f9bb 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20   was not a leaf 
1f9bc 70 61 67 65 2c 20 74 68 65 6e 0d 0a 2a 2a 20 74  page, then..** t
1f9bd 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
1f9be 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20  ntries for each 
1f9bf 63 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75  child page are u
1f9c0 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74  pdated so that t
1f9c1 68 65 0d 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61  he..** parent pa
1f9c2 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ge stored in the
1f9c3 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20   pointer map is 
1f9c4 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72  page pTo. If pFr
1f9c5 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0d 0a 2a 2a  om contained..**
1f9c6 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 20   any cells with 
1f9c7 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f  overflow page po
1f9c8 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 65  inters, then the
1f9c9 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70   corresponding p
1f9ca 6f 69 6e 74 65 72 0d 0a 2a 2a 20 6d 61 70 20 65  ointer..** map e
1f9cb 6e 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20  ntries are also 
1f9cc 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20  updated so that 
1f9cd 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
1f9ce 69 73 20 70 61 67 65 20 70 54 6f 2e 0d 0a 2a 2a  is page pTo...**
1f9cf 0d 0a 2a 2a 20 49 66 20 70 46 72 6f 6d 20 69 73  ..** If pFrom is
1f9d0 20 63 75 72 72 65 6e 74 6c 79 20 63 61 72 72 79   currently carry
1f9d1 69 6e 67 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  ing any overflow
1f9d2 20 63 65 6c 6c 73 20 28 65 6e 74 72 69 65 73 20   cells (entries 
1f9d3 69 6e 20 74 68 65 0d 0a 2a 2a 20 4d 65 6d 50 61  in the..** MemPa
1f9d4 67 65 2e 61 4f 76 66 6c 5b 5d 20 61 72 72 61 79  ge.aOvfl[] array
1f9d5 29 2c 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  ), they are not 
1f9d6 63 6f 70 69 65 64 20 74 6f 20 70 54 6f 2e 20 0d  copied to pTo. .
1f9d7 0a 2a 2a 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 72  .**..** Before r
1f9d8 65 74 75 72 6e 69 6e 67 2c 20 70 61 67 65 20 70  eturning, page p
1f9d9 54 6f 20 69 73 20 72 65 69 6e 69 74 69 61 6c 69  To is reinitiali
1f9da 7a 65 64 20 75 73 69 6e 67 20 62 74 72 65 65 49  zed using btreeI
1f9db 6e 69 74 50 61 67 65 28 29 2e 0d 0a 2a 2a 0d 0a  nitPage()...**..
1f9dc 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d 61 6e  ** The performan
1f9dd 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ce of this funct
1f9de 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69 74 69  ion is not criti
1f9df 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20  cal. It is only 
1f9e0 75 73 65 64 20 62 79 20 0d 0a 2a 2a 20 74 68 65  used by ..** the
1f9e1 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77   balance_shallow
1f9e2 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63 65  er() and balance
1f9e3 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65 64  _deeper() proced
1f9e4 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f 66  ures, neither of
1f9e5 0d 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63  ..** which are c
1f9e6 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65  alled often unde
1f9e7 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73  r normal circums
1f9e8 74 61 6e 63 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  tances...*/..sta
1f9e9 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64  tic void copyNod
1f9ea 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65  eContent(MemPage
1f9eb 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65   *pFrom, MemPage
1f9ec 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29   *pTo, int *pRC)
1f9ed 7b 0d 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d  {..  if( (*pRC)=
1f9ee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
1f9ef 20 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f     BtShared * co
1f9f0 6e 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d  nst pBt = pFrom-
1f9f1 3e 70 42 74 3b 0d 0a 20 20 20 20 75 38 20 2a 20  >pBt;..    u8 * 
1f9f2 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46  const aFrom = pF
1f9f3 72 6f 6d 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 20  rom->aData;..   
1f9f4 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20   u8 * const aTo 
1f9f5 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0d 0a 20  = pTo->aData;.. 
1f9f6 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 72     int const iFr
1f9f7 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68  omHdr = pFrom->h
1f9f8 64 72 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 69  drOffset;..    i
1f9f9 6e 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20  nt const iToHdr 
1f9fa 3d 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31  = ((pTo->pgno==1
1f9fb 29 20 3f 20 31 30 30 20 3a 20 30 29 3b 0d 0a 20  ) ? 100 : 0);.. 
1f9fc 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20 20     int rc;..    
1f9fd 69 6e 74 20 69 44 61 74 61 3b 0d 0a 20 20 0d 0a  int iData;..  ..
1f9fe 20 20 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20    ..    assert( 
1f9ff 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b  pFrom->isInit );
1fa00 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  ..    assert( pF
1fa01 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48  rom->nFree>=iToH
1fa02 64 72 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  dr );..    asser
1fa03 74 28 20 67 65 74 32 62 79 74 65 28 26 61 46 72  t( get2byte(&aFr
1fa04 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 20  om[iFromHdr+5]) 
1fa05 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61  <= (int)pBt->usa
1fa06 62 6c 65 53 69 7a 65 20 29 3b 0d 0a 20 20 0d 0a  bleSize );..  ..
1fa07 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20      /* Copy the 
1fa08 62 2d 74 72 65 65 20 6e 6f 64 65 20 63 6f 6e 74  b-tree node cont
1fa09 65 6e 74 20 66 72 6f 6d 20 70 61 67 65 20 70 46  ent from page pF
1fa0a 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e  rom to page pTo.
1fa0b 20 2a 2f 0d 0a 20 20 20 20 69 44 61 74 61 20 3d   */..    iData =
1fa0c 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d   get2byte(&aFrom
1fa0d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b 0d 0a  [iFromHdr+5]);..
1fa0e 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b      memcpy(&aTo[
1fa0f 69 44 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b 69  iData], &aFrom[i
1fa10 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61 62  Data], pBt->usab
1fa11 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 3b 0d 0a  leSize-iData);..
1fa12 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b      memcpy(&aTo[
1fa13 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b  iToHdr], &aFrom[
1fa14 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d  iFromHdr], pFrom
1fa15 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32  ->cellOffset + 2
1fa16 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0d  *pFrom->nCell);.
1fa17 0a 20 20 0d 0a 20 20 20 20 2f 2a 20 52 65 69 6e  .  ..    /* Rein
1fa18 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54  itialize page pT
1fa19 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  o so that the co
1fa1a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65  ntents of the Me
1fa1b 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 0d  mPage structure.
1fa1c 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68  .    ** match th
1fa1d 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20  e new data. The 
1fa1e 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f  initialization o
1fa1f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75 61 6c  f pTo can actual
1fa20 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0d 0a 20  ly fail under.. 
1fa21 20 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62 73     ** fairly obs
1fa22 63 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e 63  cure circumstanc
1fa23 65 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20  es, even though 
1fa24 69 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  it is a copy of 
1fa25 69 6e 69 74 69 61 6c 69 7a 65 64 20 0d 0a 20 20  initialized ..  
1fa26 20 20 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e    ** page pFrom.
1fa27 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 70 54  ..    */..    pT
1fa28 6f 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0d 0a  o->isInit = 0;..
1fa29 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e      rc = btreeIn
1fa2a 69 74 50 61 67 65 28 70 54 6f 29 3b 0d 0a 20 20  itPage(pTo);..  
1fa2b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1fa2c 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70  _OK ){..      *p
1fa2d 52 43 20 3d 20 72 63 3b 0d 0a 20 20 20 20 20 20  RC = rc;..      
1fa2e 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a  return;..    }..
1fa2f 20 20 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68    ..    /* If th
1fa30 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  is is an auto-va
1fa31 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75  cuum database, u
1fa32 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
1fa33 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 0d 0a 20  r-map entries.. 
1fa34 20 20 20 2a 2a 20 66 6f 72 20 61 6e 79 20 62 2d     ** for any b-
1fa35 74 72 65 65 20 6f 72 20 6f 76 65 72 66 6c 6f 77  tree or overflow
1fa36 20 70 61 67 65 73 20 74 68 61 74 20 70 54 6f 20   pages that pTo 
1fa37 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  now contains the
1fa38 20 70 6f 69 6e 74 65 72 73 20 74 6f 2e 0d 0a 20   pointers to... 
1fa39 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 49     */..    if( I
1fa3a 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0d 0a  SAUTOVACUUM ){..
1fa3b 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 65 74        *pRC = set
1fa3c 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 54 6f  ChildPtrmaps(pTo
1fa3d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
1fa3e 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
1fa3f 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72   routine redistr
1fa40 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20  ibutes cells on 
1fa41 74 68 65 20 69 50 61 72 65 6e 74 49 64 78 27 74  the iParentIdx't
1fa42 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65  h child of pPare
1fa43 6e 74 0d 0a 2a 2a 20 28 68 65 72 65 61 66 74 65  nt..** (hereafte
1fa44 72 20 22 74 68 65 20 70 61 67 65 22 29 20 61 6e  r "the page") an
1fa45 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69 6e  d up to 2 siblin
1fa46 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70  gs so that all p
1fa47 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20  ages have about 
1fa48 74 68 65 0d 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f  the..** same amo
1fa49 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63  unt of free spac
1fa4a 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e  e. Usually a sin
1fa4b 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65  gle sibling on e
1fa4c 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68  ither side of th
1fa4d 65 0d 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75  e..** page are u
1fa4e 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  sed in the balan
1fa4f 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74  cing, though bot
1fa50 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74  h siblings might
1fa51 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0d 0a   come from one..
1fa52 2a 2a 20 73 69 64 65 20 69 66 20 74 68 65 20 70  ** side if the p
1fa53 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74  age is the first
1fa54 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f   or last child o
1fa55 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66  f its parent. If
1fa56 20 74 68 65 20 70 61 67 65 20 0d 0a 2a 2a 20 68   the page ..** h
1fa57 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 20  as fewer than 2 
1fa58 73 69 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68  siblings (someth
1fa59 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e  ing which can on
1fa5a 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 65  ly happen if the
1fa5b 20 70 61 67 65 0d 0a 2a 2a 20 69 73 20 61 20 72   page..** is a r
1fa5c 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20 63 68  oot page or a ch
1fa5d 69 6c 64 20 6f 66 20 61 20 72 6f 6f 74 20 70 61  ild of a root pa
1fa5e 67 65 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61  ge) then all ava
1fa5f 69 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 0d  ilable siblings.
1fa60 0a 2a 2a 20 70 61 72 74 69 63 69 70 61 74 65 20  .** participate 
1fa61 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67  in the balancing
1fa62 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e 75  ...**..** The nu
1fa63 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 73  mber of siblings
1fa64 20 6f 66 20 74 68 65 20 70 61 67 65 20 6d 69 67   of the page mig
1fa65 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 20  ht be increased 
1fa66 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79 20  or decreased by 
1fa67 0d 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f 20  ..** one or two 
1fa68 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20  in an effort to 
1fa69 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c  keep pages nearl
1fa6a 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f  y full but not o
1fa6b 76 65 72 20 66 75 6c 6c 2e 20 0d 0a 2a 2a 0d 0a  ver full. ..**..
1fa6c 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65  ** Note that whe
1fa6d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
1fa6e 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f  s called, some o
1fa6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74  f the cells on t
1fa70 68 65 20 70 61 67 65 0d 0a 2a 2a 20 6d 69 67 68  he page..** migh
1fa71 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62  t not actually b
1fa72 65 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50  e stored in MemP
1fa73 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69  age.aData[]. Thi
1fa74 73 20 63 61 6e 20 68 61 70 70 65 6e 0d 0a 2a 2a  s can happen..**
1fa75 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
1fa76 6f 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 20 72  overfull. This r
1fa77 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74  outine ensures t
1fa78 68 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c  hat all cells al
1fa79 6c 6f 63 61 74 65 64 0d 0a 2a 2a 20 74 6f 20 74  located..** to t
1fa7a 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73 20  he page and its 
1fa7b 73 69 62 6c 69 6e 67 73 20 66 69 74 20 69 6e 74  siblings fit int
1fa7c 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b  o MemPage.aData[
1fa7d 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  ] before returni
1fa7e 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74  ng...**..** In t
1fa7f 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c  he course of bal
1fa80 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65 20  ancing the page 
1fa81 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73  and its siblings
1fa82 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0d 0a  , cells may be..
1fa83 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ** inserted into
1fa84 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   or removed from
1fa85 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1fa86 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e   (pParent). Doin
1fa87 67 20 73 6f 0d 0a 2a 2a 20 6d 61 79 20 63 61 75  g so..** may cau
1fa88 73 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  se the parent pa
1fa89 67 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65  ge to become ove
1fa8a 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75  rfull or underfu
1fa8b 6c 6c 2e 20 49 66 20 74 68 69 73 0d 0a 2a 2a 20  ll. If this..** 
1fa8c 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74  happens, it is t
1fa8d 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  he responsibilit
1fa8e 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
1fa8f 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f  to invoke the co
1fa90 72 72 65 63 74 0d 0a 2a 2a 20 62 61 6c 61 6e 63  rrect..** balanc
1fa91 69 6e 67 20 72 6f 75 74 69 6e 65 20 74 6f 20 66  ing routine to f
1fa92 69 78 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ix this problem 
1fa93 28 73 65 65 20 74 68 65 20 62 61 6c 61 6e 63 65  (see the balance
1fa94 28 29 20 72 6f 75 74 69 6e 65 29 2e 20 0d 0a 2a  () routine). ..*
1fa95 2a 0d 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f  *..** If this ro
1fa96 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20  utine fails for 
1fa97 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d  any reason, it m
1fa98 69 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64  ight leave the d
1fa99 61 74 61 62 61 73 65 0d 0a 2a 2a 20 69 6e 20 61  atabase..** in a
1fa9a 20 63 6f 72 72 75 70 74 65 64 20 73 74 61 74 65   corrupted state
1fa9b 2e 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75  . So if this rou
1fa9c 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20  tine fails, the 
1fa9d 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0d  database should.
1fa9e 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  .** be rolled ba
1fa9f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ck...**..** The 
1faa0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74  third argument t
1faa1 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  o this function,
1faa2 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 20   aOvflSpace, is 
1faa3 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0d 0a  a pointer to a..
1faa4 2a 2a 20 62 75 66 66 65 72 20 62 69 67 20 65 6e  ** buffer big en
1faa5 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65  ough to hold one
1faa6 20 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20   page. If while 
1faa7 69 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20  inserting cells 
1faa8 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0d  into the parent.
1faa9 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e  .** page (pParen
1faaa 74 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  t) the parent pa
1faab 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66  ge becomes overf
1faac 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72  ull, this buffer
1faad 20 69 73 0d 0a 2a 2a 20 75 73 65 64 20 74 6f 20   is..** used to 
1faae 73 74 6f 72 65 20 74 68 65 20 70 61 72 65 6e 74  store the parent
1faaf 27 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  's overflow cell
1fab0 73 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 20  s. Because this 
1fab1 66 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74 73  function inserts
1fab2 0d 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f  ..** a maximum o
1fab3 66 20 66 6f 75 72 20 64 69 76 69 64 65 72 20 63  f four divider c
1fab4 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61  ells into the pa
1fab5 72 65 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 74  rent page, and t
1fab6 68 65 20 6d 61 78 69 6d 75 6d 0d 0a 2a 2a 20 73  he maximum..** s
1fab7 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 73 74  ize of a cell st
1fab8 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e 20 69  ored within an i
1fab9 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20  nternal node is 
1faba 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68 61 6e  always less than
1fabb 20 31 2f 34 0d 0a 2a 2a 20 6f 66 20 74 68 65 20   1/4..** of the 
1fabc 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 61  page-size, the a
1fabd 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 66  OvflSpace[] buff
1fabe 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  er is guaranteed
1fabf 20 74 6f 20 62 65 20 6c 61 72 67 65 0d 0a 2a 2a   to be large..**
1fac0 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20   enough for all 
1fac1 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0d  overflow cells..
1fac2 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 4f 76 66 6c  .**..** If aOvfl
1fac3 53 70 61 63 65 20 69 73 20 73 65 74 20 74 6f 20  Space is set to 
1fac4 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20  a null pointer, 
1fac5 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
1fac6 74 75 72 6e 73 20 0d 0a 2a 2a 20 53 51 4c 49 54  turns ..** SQLIT
1fac7 45 5f 4e 4f 4d 45 4d 2e 0d 0a 2a 2f 0d 0a 73 74  E_NOMEM...*/..st
1fac8 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65  atic int balance
1fac9 5f 6e 6f 6e 72 6f 6f 74 28 0d 0a 20 20 4d 65 6d  _nonroot(..  Mem
1faca 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Page *pParent,  
1facb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1facc 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66 20 73  Parent page of s
1facd 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20 62 61  iblings being ba
1face 6c 61 6e 63 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  lanced */..  int
1facf 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20 20   iParentIdx,    
1fad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fad1 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70 61  Index of "the pa
1fad2 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20 2a  ge" in pParent *
1fad3 2f 0d 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70  /..  u8 *aOvflSp
1fad4 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ace,            
1fad5 20 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a       /* page-siz
1fad6 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
1fad7 20 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c   for parent ovfl
1fad8 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 52 6f 6f   */..  int isRoo
1fad9 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
1fada 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1fadb 66 20 70 50 61 72 65 6e 74 20 69 73 20 61 20 72  f pParent is a r
1fadc 6f 6f 74 2d 70 61 67 65 20 2a 2f 0d 0a 29 7b 0d  oot-page */..){.
1fadd 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1fade 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fadf 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74  /* The whole dat
1fae0 61 62 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  abase */..  int 
1fae1 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20  nCell = 0;      
1fae2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1fae3 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61  er of cells in a
1fae4 70 43 65 6c 6c 5b 5d 20 2a 2f 0d 0a 20 20 69 6e  pCell[] */..  in
1fae5 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b  t nMaxCells = 0;
1fae6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
1fae7 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
1fae8 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20  apCell, szCell, 
1fae9 61 46 72 6f 6d 2e 20 2a 2f 0d 0a 20 20 69 6e 74  aFrom. */..  int
1faea 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20   nNew = 0;      
1faeb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1faec 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
1faed 61 70 4e 65 77 5b 5d 20 2a 2f 0d 0a 20 20 69 6e  apNew[] */..  in
1faee 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20  t nOld;         
1faef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1faf0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1faf1 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0d 0a 20 20 69   apOld[] */..  i
1faf2 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20  nt i, j, k;     
1faf3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1faf4 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d  oop counters */.
1faf5 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20  .  int nxDiv;   
1faf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faf7 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20  /* Next divider 
1faf8 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d  slot in pParent-
1faf9 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0d 0a 20 20 69  >aCell[] */..  i
1fafa 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1fafb 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  K;          /* T
1fafc 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a  he return code *
1fafd 2f 0d 0a 20 20 75 31 36 20 6c 65 61 66 43 6f 72  /..  u16 leafCor
1fafe 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20  rection;        
1faff 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20    /* 4 if pPage 
1fb00 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66  is a leaf.  0 if
1fb01 20 6e 6f 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c   not */..  int l
1fb02 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20  eafData;        
1fb03 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1fb04 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  if pPage is a le
1fb05 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41  af of a LEAFDATA
1fb06 20 74 72 65 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   tree */..  int 
1fb07 75 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20  usableSpace;    
1fb08 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
1fb09 73 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e  s in pPage beyon
1fb0a 64 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0d  d the header */.
1fb0b 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73  .  int pageFlags
1fb0c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fb0d 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67  /* Value of pPag
1fb0e 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0d 0a  e->aData[0] */..
1fb0f 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20    int subtotal; 
1fb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fb11 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 79  * Subtotal of by
1fb12 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20  tes in cells on 
1fb13 6f 6e 65 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69  one page */..  i
1fb14 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20  nt iSpace1 = 0; 
1fb15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1fb16 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
1fb17 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f   of aSpace1[] */
1fb18 0d 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61  ..  int iOvflSpa
1fb19 63 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ce = 0;         
1fb1a 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64   /* First unused
1fb1b 20 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70   byte of aOvflSp
1fb1c 61 63 65 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20  ace[] */..  int 
1fb1d 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20 20  szScratch;      
1fb1e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
1fb1f 20 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f   of scratch memo
1fb20 72 79 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0d  ry requested */.
1fb21 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c  .  MemPage *apOl
1fb22 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20  d[NB];          
1fb23 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20  /* pPage and up 
1fb24 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67 73 20  to two siblings 
1fb25 2a 2f 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61  */..  MemPage *a
1fb26 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20  pCopy[NB];      
1fb27 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f     /* Private co
1fb28 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20  pies of apOld[] 
1fb29 70 61 67 65 73 20 2a 2f 0d 0a 20 20 4d 65 6d 50  pages */..  MemP
1fb2a 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d  age *apNew[NB+2]
1fb2b 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67  ;        /* pPag
1fb2c 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73  e and up to NB s
1fb2d 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61  iblings after ba
1fb2e 6c 61 6e 63 69 6e 67 20 2a 2f 0d 0a 20 20 75 38  lancing */..  u8
1fb2f 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20   *pRight;       
1fb30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1fb31 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74  cation in parent
1fb32 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c 69 6e   of right-siblin
1fb33 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20 20  g pointer */..  
1fb34 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d 3b  u8 *apDiv[NB-1];
1fb35 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fb36 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e  Divider cells in
1fb37 20 70 50 61 72 65 6e 74 20 2a 2f 0d 0a 20 20 69   pParent */..  i
1fb38 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b  nt cntNew[NB+2];
1fb39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1fb3a 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  ndex in aCell[] 
1fb3b 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d  of cell after i-
1fb3c 74 68 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69 6e  th page */..  in
1fb3d 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20  t szNew[NB+2];  
1fb3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1fb3f 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63  mbined size of c
1fb40 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d  ells place on i-
1fb41 74 68 20 70 61 67 65 20 2a 2f 0d 0a 20 20 75 38  th page */..  u8
1fb42 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20   **apCell = 0;  
1fb43 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
1fb44 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61  l cells begin ba
1fb45 6c 61 6e 63 65 64 20 2a 2f 0d 0a 20 20 75 31 36  lanced */..  u16
1fb46 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20   *szCell;       
1fb47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63            /* Loc
1fb48 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63  al size of all c
1fb49 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
1fb4a 20 2a 2f 0d 0a 20 20 75 38 20 2a 61 53 70 61 63   */..  u8 *aSpac
1fb4b 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  e1;             
1fb4c 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
1fb4d 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64   copies of divid
1fb4e 65 72 73 20 63 65 6c 6c 73 20 2a 2f 0d 0a 20 20  ers cells */..  
1fb4f 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20  Pgno pgno;      
1fb50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fb51 54 65 6d 70 20 76 61 72 20 74 6f 20 73 74 6f 72  Temp var to stor
1fb52 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20  e a page number 
1fb53 69 6e 20 2a 2f 0d 0a 0d 0a 20 20 70 42 74 20 3d  in */....  pBt =
1fb54 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3b 0d 0a   pParent->pBt;..
1fb55 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1fb56 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1fb57 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 61  ->mutex) );..  a
1fb58 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
1fb59 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1fb5a 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
1fb5b 20 29 3b 0d 0a 0d 0a 23 69 66 20 30 0d 0a 20 20   );....#if 0..  
1fb5c 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
1fb5d 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 63   begin page %d c
1fb5e 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70  hild of %d\n", p
1fb5f 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 72  Page->pgno, pPar
1fb60 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0d 0a 23 65  ent->pgno));..#e
1fb61 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 41 74 20  ndif....  /* At 
1fb62 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65  this point pPare
1fb63 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d  nt may have at m
1fb64 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77  ost one overflow
1fb65 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0d 0a 20   cell. And if.. 
1fb66 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   ** this overflo
1fb67 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e  w cell is presen
1fb68 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68  t, it must be th
1fb69 65 20 63 65 6c 6c 20 77 69 74 68 20 0d 0a 20 20  e cell with ..  
1fb6a 2a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74  ** index iParent
1fb6b 49 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72  Idx. This scenar
1fb6c 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77  io comes about w
1fb6d 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
1fb6e 6e 0d 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65  n..  ** is calle
1fb6f 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66  d (indirectly) f
1fb70 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65  rom sqlite3Btree
1fb71 44 65 6c 65 74 65 28 29 2e 0d 0a 20 20 2a 2f 0d  Delete()...  */.
1fb72 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
1fb73 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  nt->nOverflow==0
1fb74 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76   || pParent->nOv
1fb75 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0d 0a 20 20  erflow==1 );..  
1fb76 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
1fb77 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c  >nOverflow==0 ||
1fb78 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b   pParent->aOvfl[
1fb79 30 5d 2e 69 64 78 3d 3d 69 50 61 72 65 6e 74 49  0].idx==iParentI
1fb7a 64 78 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 21  dx );....  if( !
1fb7b 61 4f 76 66 6c 53 70 61 63 65 20 29 7b 0d 0a 20  aOvflSpace ){.. 
1fb7c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1fb7d 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a  _NOMEM;..  }....
1fb7e 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 69    /* Find the si
1fb7f 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 62  bling pages to b
1fb80 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63  alance. Also loc
1fb81 61 74 65 20 74 68 65 20 63 65 6c 6c 73 20 69 6e  ate the cells in
1fb82 20 70 50 61 72 65 6e 74 20 0d 0a 20 20 2a 2a 20   pParent ..  ** 
1fb83 74 68 61 74 20 64 69 76 69 64 65 20 74 68 65 20  that divide the 
1fb84 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74 74  siblings. An att
1fb85 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
1fb86 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73  find NN siblings
1fb87 20 6f 6e 20 0d 0a 20 20 2a 2a 20 65 69 74 68 65   on ..  ** eithe
1fb88 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e  r side of pPage.
1fb89 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61   More siblings a
1fb8a 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e  re taken from on
1fb8b 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c  e side, however,
1fb8c 20 0d 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65   ..  ** if there
1fb8d 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20   are fewer than 
1fb8e 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74  NN siblings on t
1fb8f 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 49  he other side. I
1fb90 66 20 70 50 61 72 65 6e 74 0d 0a 20 20 2a 2a 20  f pParent..  ** 
1fb91 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20  has NB or fewer 
1fb92 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c  children then al
1fb93 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50  l children of pP
1fb94 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e  arent are taken.
1fb95 20 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54    ..  **..  ** T
1fb96 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72  his loop also dr
1fb97 6f 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20  ops the divider 
1fb98 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70  cells from the p
1fb99 61 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73  arent page. This
1fb9a 0d 0a 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20  ..  ** way, the 
1fb9b 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65  remainder of the
1fb9c 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
1fb9d 6f 74 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20  ot have to deal 
1fb9e 77 69 74 68 20 61 6e 79 0d 0a 20 20 2a 2a 20 6f  with any..  ** o
1fb9f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e  verflow cells in
1fba0 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1fba1 2c 20 73 69 6e 63 65 20 69 66 20 61 6e 79 20 65  , since if any e
1fba2 78 69 73 74 65 64 20 74 68 65 79 20 77 69 6c 6c  xisted they will
1fba3 0d 0a 20 20 2a 2a 20 68 61 76 65 20 61 6c 72 65  ..  ** have alre
1fba4 61 64 79 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ady been removed
1fba5 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 20 3d 20 70  ...  */..  i = p
1fba6 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
1fba7 77 20 2b 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65  w + pParent->nCe
1fba8 6c 6c 3b 0d 0a 20 20 69 66 28 20 69 3c 32 20 29  ll;..  if( i<2 )
1fba9 7b 0d 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 30  {..    nxDiv = 0
1fbaa 3b 0d 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 69 2b  ;..    nOld = i+
1fbab 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  1;..  }else{..  
1fbac 20 20 6e 4f 6c 64 20 3d 20 33 3b 0d 0a 20 20 20    nOld = 3;..   
1fbad 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d   if( iParentIdx=
1fbae 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
1fbaf 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20 6e 78        ..      nx
1fbb0 44 69 76 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65  Div = 0;..    }e
1fbb1 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e 74 49  lse if( iParentI
1fbb2 64 78 3d 3d 69 20 29 7b 0d 0a 20 20 20 20 20 20  dx==i ){..      
1fbb3 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0d 0a 20 20  nxDiv = i-2;..  
1fbb4 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
1fbb5 6e 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49  nxDiv = iParentI
1fbb6 64 78 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  dx-1;..    }..  
1fbb7 20 20 69 20 3d 20 32 3b 0d 0a 20 20 7d 0d 0a 20    i = 2;..  }.. 
1fbb8 20 69 66 28 20 28 69 2b 6e 78 44 69 76 2d 70 50   if( (i+nxDiv-pP
1fbb9 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
1fbba 29 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  )==pParent->nCel
1fbbb 6c 20 29 7b 0d 0a 20 20 20 20 70 52 69 67 68 74  l ){..    pRight
1fbbc 20 3d 20 26 70 50 61 72 65 6e 74 2d 3e 61 44 61   = &pParent->aDa
1fbbd 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
1fbbe 66 66 73 65 74 2b 38 5d 3b 0d 0a 20 20 7d 65 6c  ffset+8];..  }el
1fbbf 73 65 7b 0d 0a 20 20 20 20 70 52 69 67 68 74 20  se{..    pRight 
1fbc0 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65  = findCell(pPare
1fbc1 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72  nt, i+nxDiv-pPar
1fbc2 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b  ent->nOverflow);
1fbc3 0d 0a 20 20 7d 0d 0a 20 20 70 67 6e 6f 20 3d 20  ..  }..  pgno = 
1fbc4 67 65 74 34 62 79 74 65 28 70 52 69 67 68 74 29  get4byte(pRight)
1fbc5 3b 0d 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  ;..  while( 1 ){
1fbc6 0d 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e  ..    rc = getAn
1fbc7 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70  dInitPage(pBt, p
1fbc8 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d 29 3b  gno, &apOld[i]);
1fbc9 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d  ..    if( rc ){.
1fbca 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70  .      memset(ap
1fbcb 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69  Old, 0, (i+1)*si
1fbcc 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b  zeof(MemPage*));
1fbcd 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c  ..      goto bal
1fbce 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  ance_cleanup;.. 
1fbcf 20 20 20 7d 0d 0a 20 20 20 20 6e 4d 61 78 43 65     }..    nMaxCe
1fbd0 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69  lls += 1+apOld[i
1fbd1 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69  ]->nCell+apOld[i
1fbd2 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0d 0a 20  ]->nOverflow;.. 
1fbd3 20 20 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20     if( (i--)==0 
1fbd4 29 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20  ) break;....    
1fbd5 69 66 28 20 69 2b 6e 78 44 69 76 3d 3d 70 50 61  if( i+nxDiv==pPa
1fbd6 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  rent->aOvfl[0].i
1fbd7 64 78 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e  dx && pParent->n
1fbd8 4f 76 65 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20  Overflow ){..   
1fbd9 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 70 50     apDiv[i] = pP
1fbda 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e  arent->aOvfl[0].
1fbdb 70 43 65 6c 6c 3b 0d 0a 20 20 20 20 20 20 70 67  pCell;..      pg
1fbdc 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61 70  no = get4byte(ap
1fbdd 44 69 76 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20  Div[i]);..      
1fbde 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53  szNew[i] = cellS
1fbdf 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20  izePtr(pParent, 
1fbe0 61 70 44 69 76 5b 69 5d 29 3b 0d 0a 20 20 20 20  apDiv[i]);..    
1fbe1 20 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72    pParent->nOver
1fbe2 66 6c 6f 77 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  flow = 0;..    }
1fbe3 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 70 44  else{..      apD
1fbe4 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c  iv[i] = findCell
1fbe5 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69  (pParent, i+nxDi
1fbe6 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  v-pParent->nOver
1fbe7 66 6c 6f 77 29 3b 0d 0a 20 20 20 20 20 20 70 67  flow);..      pg
1fbe8 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61 70  no = get4byte(ap
1fbe9 44 69 76 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20  Div[i]);..      
1fbea 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53  szNew[i] = cellS
1fbeb 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20  izePtr(pParent, 
1fbec 61 70 44 69 76 5b 69 5d 29 3b 0d 0a 0d 0a 20 20  apDiv[i]);....  
1fbed 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20      /* Drop the 
1fbee 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 61  cell from the pa
1fbef 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 76  rent page. apDiv
1fbf0 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73  [i] still points
1fbf1 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68   to..      ** th
1fbf2 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68  e cell within th
1fbf3 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74  e parent, even t
1fbf4 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 65  hough it has bee
1fbf5 6e 20 64 72 6f 70 70 65 64 2e 0d 0a 20 20 20 20  n dropped...    
1fbf6 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66    ** This is saf
1fbf7 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69  e because droppi
1fbf8 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f  ng a cell only o
1fbf9 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69  verwrites the fi
1fbfa 72 73 74 0d 0a 20 20 20 20 20 20 2a 2a 20 66 6f  rst..      ** fo
1fbfb 75 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20  ur bytes of it, 
1fbfc 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  and this functio
1fbfd 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
1fbfe 74 68 65 20 66 69 72 73 74 0d 0a 20 20 20 20 20  the first..     
1fbff 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f   ** four bytes o
1fc00 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  f the divider ce
1fc01 6c 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74  ll. So the point
1fc02 65 72 20 69 73 20 73 61 66 65 20 74 6f 20 75 73  er is safe to us
1fc03 65 0d 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74 65  e..      ** late
1fc04 72 20 6f 6e 2e 20 20 0d 0a 20 20 20 20 20 20 2a  r on.  ..      *
1fc05 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20  *..      ** But 
1fc06 6e 6f 74 20 69 66 20 77 65 20 61 72 65 20 69 6e  not if we are in
1fc07 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d   secure-delete m
1fc08 6f 64 65 2e 20 49 6e 20 73 65 63 75 72 65 2d 64  ode. In secure-d
1fc09 65 6c 65 74 65 20 6d 6f 64 65 2c 0d 0a 20 20 20  elete mode,..   
1fc0a 20 20 20 2a 2a 20 74 68 65 20 64 72 6f 70 43 65     ** the dropCe
1fc0b 6c 6c 28 29 20 72 6f 75 74 69 6e 65 20 77 69 6c  ll() routine wil
1fc0c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 20  l overwrite the 
1fc0d 65 6e 74 69 72 65 20 63 65 6c 6c 20 77 69 74 68  entire cell with
1fc0e 20 7a 65 72 6f 65 73 2e 0d 0a 20 20 20 20 20 20   zeroes...      
1fc0f 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  ** In this case,
1fc10 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70   temporarily cop
1fc11 79 20 74 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20  y the cell into 
1fc12 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d  the aOvflSpace[]
1fc13 0d 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65  ..      ** buffe
1fc14 72 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 63 6f  r. It will be co
1fc15 70 69 65 64 20 6f 75 74 20 61 67 61 69 6e 20 61  pied out again a
1fc16 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 61 53  s soon as the aS
1fc17 70 61 63 65 5b 5d 20 62 75 66 66 65 72 0d 0a 20  pace[] buffer.. 
1fc18 20 20 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f 63       ** is alloc
1fc19 61 74 65 64 2e 20 20 2a 2f 0d 0a 20 20 20 20 20  ated.  */..     
1fc1a 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61   if( pBt->btsFla
1fc1b 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f  gs & BTS_SECURE_
1fc1c 44 45 4c 45 54 45 20 29 7b 0d 0a 20 20 20 20 20  DELETE ){..     
1fc1d 20 20 20 69 6e 74 20 69 4f 66 66 3b 0d 0a 0d 0a     int iOff;....
1fc1e 20 20 20 20 20 20 20 20 69 4f 66 66 20 3d 20 53          iOff = S
1fc1f 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
1fc20 28 61 70 44 69 76 5b 69 5d 29 20 2d 20 53 51 4c  (apDiv[i]) - SQL
1fc21 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
1fc22 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 29 3b 0d  Parent->aData);.
1fc23 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 69 4f  .        if( (iO
1fc24 66 66 2b 73 7a 4e 65 77 5b 69 5d 29 3e 28 69 6e  ff+szNew[i])>(in
1fc25 74 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  t)pBt->usableSiz
1fc26 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  e ){..          
1fc27 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1fc28 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 20  UPT_BKPT;..     
1fc29 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f 6c       memset(apOl
1fc2a 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a 65  d, 0, (i+1)*size
1fc2b 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0d 0a  of(MemPage*));..
1fc2c 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
1fc2d 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0d  alance_cleanup;.
1fc2e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
1fc2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
1fc30 79 28 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f  y(&aOvflSpace[iO
1fc31 66 66 5d 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73  ff], apDiv[i], s
1fc32 7a 4e 65 77 5b 69 5d 29 3b 0d 0a 20 20 20 20 20  zNew[i]);..     
1fc33 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20       apDiv[i] = 
1fc34 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44 69  &aOvflSpace[apDi
1fc35 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44  v[i]-pParent->aD
1fc36 61 74 61 5d 3b 0d 0a 20 20 20 20 20 20 20 20 7d  ata];..        }
1fc37 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
1fc38 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e   dropCell(pParen
1fc39 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65  t, i+nxDiv-pPare
1fc3a 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73  nt->nOverflow, s
1fc3b 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b 0d 0a  zNew[i], &rc);..
1fc3c 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
1fc3d 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c  /* Make nMaxCell
1fc3e 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  s a multiple of 
1fc3f 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  4 in order to pr
1fc40 65 73 65 72 76 65 20 38 2d 62 79 74 65 0d 0a 20  eserve 8-byte.. 
1fc41 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f   ** alignment */
1fc42 0d 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20  ..  nMaxCells = 
1fc43 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26  (nMaxCells + 3)&
1fc44 7e 33 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  ~3;....  /*..  *
1fc45 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  * Allocate space
1fc46 20 66 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75   for memory stru
1fc47 63 74 75 72 65 73 0d 0a 20 20 2a 2f 0d 0a 20 20  ctures..  */..  
1fc48 6b 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  k = pBt->pageSiz
1fc49 65 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  e + ROUND8(sizeo
1fc4a 66 28 4d 65 6d 50 61 67 65 29 29 3b 0d 0a 20 20  f(MemPage));..  
1fc4b 73 7a 53 63 72 61 74 63 68 20 3d 0d 0a 20 20 20  szScratch =..   
1fc4c 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69      nMaxCells*si
1fc4d 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20  zeof(u8*)       
1fc4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc4f 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0d 0a 20 20  /* apCell */..  
1fc50 20 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73     + nMaxCells*s
1fc51 69 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20 20  izeof(u16)      
1fc52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc53 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0d 0a 20   /* szCell */.. 
1fc54 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67 65 53      + pBt->pageS
1fc55 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ize             
1fc56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc57 20 20 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f 0d    /* aSpace1 */.
1fc58 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20  .     + k*nOld; 
1fc59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc5b 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 70 69      /* Page copi
1fc5c 65 73 20 28 61 70 43 6f 70 79 29 20 2a 2f 0d 0a  es (apCopy) */..
1fc5d 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74    apCell = sqlit
1fc5e 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28  e3ScratchMalloc(
1fc5f 20 73 7a 53 63 72 61 74 63 68 20 29 3b 20 0d 0a   szScratch ); ..
1fc60 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20    if( apCell==0 
1fc61 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
1fc62 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
1fc63 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
1fc64 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 20 20 73 7a  anup;..  }..  sz
1fc65 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70  Cell = (u16*)&ap
1fc66 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b  Cell[nMaxCells];
1fc67 0d 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 75  ..  aSpace1 = (u
1fc68 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43  8*)&szCell[nMaxC
1fc69 65 6c 6c 73 5d 3b 0d 0a 20 20 61 73 73 65 72 74  ells];..  assert
1fc6a 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
1fc6b 47 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20  GNMENT(aSpace1) 
1fc6c 29 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a  );....  /*..  **
1fc6d 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74   Load pointers t
1fc6e 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73  o all cells on s
1fc6f 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64  ibling pages and
1fc70 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
1fc71 6c 73 0d 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  ls..  ** into th
1fc72 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d  e local apCell[]
1fc73 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f   array.  Make co
1fc74 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69  pies of the divi
1fc75 64 65 72 20 63 65 6c 6c 73 0d 0a 20 20 2a 2a 20  der cells..  ** 
1fc76 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69  into space obtai
1fc77 6e 65 64 20 66 72 6f 6d 20 61 53 70 61 63 65 31  ned from aSpace1
1fc78 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  [] and remove th
1fc79 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65  e the divider Ce
1fc7a 6c 6c 73 0d 0a 20 20 2a 2a 20 66 72 6f 6d 20 70  lls..  ** from p
1fc7b 50 61 72 65 6e 74 2e 0d 0a 20 20 2a 2a 0d 0a 20  Parent...  **.. 
1fc7c 20 2a 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69   ** If the sibli
1fc7d 6e 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 20  ngs are on leaf 
1fc7e 70 61 67 65 73 2c 20 74 68 65 6e 20 74 68 65 20  pages, then the 
1fc7f 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f  child pointers o
1fc80 66 20 74 68 65 0d 0a 20 20 2a 2a 20 64 69 76 69  f the..  ** divi
1fc81 64 65 72 20 63 65 6c 6c 73 20 61 72 65 20 73 74  der cells are st
1fc82 72 69 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20  ripped from the 
1fc83 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 65  cells before the
1fc84 79 20 61 72 65 20 63 6f 70 69 65 64 0d 0a 20 20  y are copied..  
1fc85 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 31 5b  ** into aSpace1[
1fc86 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  ].  In this way,
1fc87 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70   all cells in ap
1fc88 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f  Cell[] are witho
1fc89 75 74 0d 0a 20 20 2a 2a 20 63 68 69 6c 64 20 70  ut..  ** child p
1fc8a 6f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 69 62  ointers.  If sib
1fc8b 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65  lings are not le
1fc8c 61 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 63  aves, then all c
1fc8d 65 6c 6c 20 69 6e 0d 0a 20 20 2a 2a 20 61 70 43  ell in..  ** apC
1fc8e 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68  ell[] include ch
1fc8f 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 45  ild pointers.  E
1fc90 69 74 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 63  ither way, all c
1fc91 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
1fc92 0d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65  ..  ** are alike
1fc93 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 6c 65  ...  **..  ** le
1fc94 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34  afCorrection:  4
1fc95 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c   if pPage is a l
1fc96 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 65  eaf.  0 if pPage
1fc97 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0d   is not a leaf..
1fc98 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66  .  **       leaf
1fc99 44 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67  Data:  1 if pPag
1fc9a 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61  e holds key+data
1fc9b 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c   and pParent hol
1fc9c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0d 0a 20  ds only keys... 
1fc9d 20 2a 2f 0d 0a 20 20 6c 65 61 66 43 6f 72 72 65   */..  leafCorre
1fc9e 63 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d  ction = apOld[0]
1fc9f 2d 3e 6c 65 61 66 2a 34 3b 0d 0a 20 20 6c 65 61  ->leaf*4;..  lea
1fca0 66 44 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d  fData = apOld[0]
1fca1 2d 3e 68 61 73 44 61 74 61 3b 0d 0a 20 20 66 6f  ->hasData;..  fo
1fca2 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
1fca3 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6c 69  ++){..    int li
1fca4 6d 69 74 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20  mit;..    ..    
1fca5 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  /* Before doing 
1fca6 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 74  anything else, t
1fca7 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
1fca8 65 20 69 27 74 68 20 6f 72 69 67 69 6e 61 6c 20  e i'th original 
1fca9 73 69 62 6c 69 6e 67 0d 0a 20 20 20 20 2a 2a 20  sibling..    ** 
1fcaa 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73  The rest of this
1fcab 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75   function will u
1fcac 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  se data from the
1fcad 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0d 0a   copies rather..
1fcae 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
1fcaf 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73  original pages s
1fcb0 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61  ince the origina
1fcb1 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20  l pages will be 
1fcb2 69 6e 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 70  in the..    ** p
1fcb3 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20  rocess of being 
1fcb4 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 2a 2f  overwritten.  */
1fcb5 0d 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  ..    MemPage *p
1fcb6 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20  Old = apCopy[i] 
1fcb7 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 53 70  = (MemPage*)&aSp
1fcb8 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 69  ace1[pBt->pageSi
1fcb9 7a 65 20 2b 20 6b 2a 69 5d 3b 0d 0a 20 20 20 20  ze + k*i];..    
1fcba 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 4f  memcpy(pOld, apO
1fcbb 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d 65  ld[i], sizeof(Me
1fcbc 6d 50 61 67 65 29 29 3b 0d 0a 20 20 20 20 70 4f  mPage));..    pO
1fcbd 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f 69  ld->aData = (voi
1fcbe 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0d 0a 20 20  d*)&pOld[1];..  
1fcbf 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e 61    memcpy(pOld->a
1fcc0 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e  Data, apOld[i]->
1fcc1 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65  aData, pBt->page
1fcc2 53 69 7a 65 29 3b 0d 0a 0d 0a 20 20 20 20 6c 69  Size);....    li
1fcc3 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c  mit = pOld->nCel
1fcc4 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  l+pOld->nOverflo
1fcc5 77 3b 0d 0a 20 20 20 20 69 66 28 20 70 4f 6c 64  w;..    if( pOld
1fcc6 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 7b  ->nOverflow>0 ){
1fcc7 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ..      for(j=0;
1fcc8 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0d   j<limit; j++){.
1fcc9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1fcca 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73   nCell<nMaxCells
1fccb 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 70 43   );..        apC
1fccc 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e  ell[nCell] = fin
1fccd 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f  dOverflowCell(pO
1fcce 6c 64 2c 20 6a 29 3b 0d 0a 20 20 20 20 20 20 20  ld, j);..       
1fccf 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   szCell[nCell] =
1fcd0 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c   cellSizePtr(pOl
1fcd1 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  d, apCell[nCell]
1fcd2 29 3b 0d 0a 20 20 20 20 20 20 20 20 6e 43 65 6c  );..        nCel
1fcd3 6c 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  l++;..      }.. 
1fcd4 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
1fcd5 20 75 38 20 2a 61 44 61 74 61 20 3d 20 70 4f 6c   u8 *aData = pOl
1fcd6 64 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 20 20 20  d->aData;..     
1fcd7 20 75 31 36 20 6d 61 73 6b 50 61 67 65 20 3d 20   u16 maskPage = 
1fcd8 70 4f 6c 64 2d 3e 6d 61 73 6b 50 61 67 65 3b 0d  pOld->maskPage;.
1fcd9 0a 20 20 20 20 20 20 75 31 36 20 63 65 6c 6c 4f  .      u16 cellO
1fcda 66 66 73 65 74 20 3d 20 70 4f 6c 64 2d 3e 63 65  ffset = pOld->ce
1fcdb 6c 6c 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 20  llOffset;..     
1fcdc 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69   for(j=0; j<limi
1fcdd 74 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  t; j++){..      
1fcde 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c    assert( nCell<
1fcdf 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0d 0a 20 20  nMaxCells );..  
1fce0 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65        apCell[nCe
1fce1 6c 6c 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 76 32  ll] = findCellv2
1fce2 28 61 44 61 74 61 2c 20 6d 61 73 6b 50 61 67 65  (aData, maskPage
1fce3 2c 20 63 65 6c 6c 4f 66 66 73 65 74 2c 20 6a 29  , cellOffset, j)
1fce4 3b 0d 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c  ;..        szCel
1fce5 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53  l[nCell] = cellS
1fce6 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43  izePtr(pOld, apC
1fce7 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0d 0a 20 20  ell[nCell]);..  
1fce8 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0d 0a        nCell++;..
1fce9 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 20 20        }..    }  
1fcea 20 20 20 20 20 0d 0a 20 20 20 20 69 66 28 20 69       ..    if( i
1fceb 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 66  <nOld-1 && !leaf
1fcec 44 61 74 61 29 7b 0d 0a 20 20 20 20 20 20 75 31  Data){..      u1
1fced 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65  6 sz = (u16)szNe
1fcee 77 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 75 38 20  w[i];..      u8 
1fcef 2a 70 54 65 6d 70 3b 0d 0a 20 20 20 20 20 20 61  *pTemp;..      a
1fcf0 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61  ssert( nCell<nMa
1fcf1 78 43 65 6c 6c 73 20 29 3b 0d 0a 20 20 20 20 20  xCells );..     
1fcf2 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   szCell[nCell] =
1fcf3 20 73 7a 3b 0d 0a 20 20 20 20 20 20 70 54 65 6d   sz;..      pTem
1fcf4 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70  p = &aSpace1[iSp
1fcf5 61 63 65 31 5d 3b 0d 0a 20 20 20 20 20 20 69 53  ace1];..      iS
1fcf6 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0d 0a 20 20  pace1 += sz;..  
1fcf7 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
1fcf8 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 32 33  pBt->maxLocal+23
1fcf9 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
1fcfa 74 28 20 69 53 70 61 63 65 31 20 3c 3d 20 28 69  t( iSpace1 <= (i
1fcfb 6e 74 29 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  nt)pBt->pageSize
1fcfc 20 29 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70   );..      memcp
1fcfd 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 5b 69  y(pTemp, apDiv[i
1fcfe 5d 2c 20 73 7a 29 3b 0d 0a 20 20 20 20 20 20 61  ], sz);..      a
1fcff 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70  pCell[nCell] = p
1fd00 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 63 74  Temp+leafCorrect
1fd01 69 6f 6e 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  ion;..      asse
1fd02 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69  rt( leafCorrecti
1fd03 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f 72  on==0 || leafCor
1fd04 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0d 0a 20  rection==4 );.. 
1fd05 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c       szCell[nCel
1fd06 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c  l] = szCell[nCel
1fd07 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 63 74  l] - leafCorrect
1fd08 69 6f 6e 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ion;..      if( 
1fd09 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0d 0a  !pOld->leaf ){..
1fd0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1fd0b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
1fd0c 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73  0 );..        as
1fd0d 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f  sert( pOld->hdrO
1fd0e 66 66 73 65 74 3d 3d 30 20 29 3b 0d 0a 20 20 20  ffset==0 );..   
1fd0f 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
1fd10 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  t pointer of the
1fd11 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64   child page pOld
1fd12 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66   becomes the lef
1fd13 74 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f  t..        ** po
1fd14 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 69 76  inter of the div
1fd15 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20  ider cell */..  
1fd16 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 70 43        memcpy(apC
1fd17 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c  ell[nCell], &pOl
1fd18 64 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b  d->aData[8], 4);
1fd19 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
1fd1a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1fd1b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
1fd1c 34 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  4 );..        if
1fd1d 28 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c  ( szCell[nCell]<
1fd1e 34 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  4 ){..          
1fd1f 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  /* Do not allow 
1fd20 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65  any cells smalle
1fd21 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20  r than 4 bytes. 
1fd22 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 73 7a  */..          sz
1fd23 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b  Cell[nCell] = 4;
1fd24 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
1fd25 20 20 20 7d 0d 0a 20 20 20 20 20 20 6e 43 65 6c     }..      nCel
1fd26 6c 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  l++;..    }..  }
1fd27 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 46  ....  /*..  ** F
1fd28 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75  igure out the nu
1fd29 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65  mber of pages ne
1fd2a 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  eded to hold all
1fd2b 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0d 0a 20   nCell cells... 
1fd2c 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e   ** Store this n
1fd2d 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41  umber in "k".  A
1fd2e 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65  lso compute szNe
1fd2f 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65  w[] which is the
1fd30 20 74 6f 74 61 6c 0d 0a 20 20 2a 2a 20 73 69 7a   total..  ** siz
1fd31 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f  e of all cells o
1fd32 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65 20  n the i-th page 
1fd33 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69  and cntNew[] whi
1fd34 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78 0d  ch is the index.
1fd35 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b  .  ** in apCell[
1fd36 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68  ] of the cell th
1fd37 61 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20  at divides page 
1fd38 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e  i from page i+1.
1fd39 20 20 0d 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b    ..  ** cntNew[
1fd3a 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20  k] should equal 
1fd3b 6e 43 65 6c 6c 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  nCell...  **..  
1fd3c 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74  ** Values comput
1fd3d 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b  ed by this block
1fd3e 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20  :..  **..  **   
1fd3f 20 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74          k: The t
1fd40 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73  otal number of s
1fd41 69 62 6c 69 6e 67 20 70 61 67 65 73 0d 0a 20 20  ibling pages..  
1fd42 2a 2a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20  **    szNew[i]: 
1fd43 53 70 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74  Spaced used on t
1fd44 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20  he i-th sibling 
1fd45 70 61 67 65 2e 0d 0a 20 20 2a 2a 20 20 20 63 6e  page...  **   cn
1fd46 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69  tNew[i]: Index i
1fd47 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73  n apCell[] and s
1fd48 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20  zCell[] for the 
1fd49 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 0d 0a 20  first cell to.. 
1fd4a 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
1fd4b 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68   the right of th
1fd4c 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70  e i-th sibling p
1fd4d 61 67 65 2e 0d 0a 20 20 2a 2a 20 75 73 61 62 6c  age...  ** usabl
1fd4e 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 6f  eSpace: Number o
1fd4f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  f bytes of space
1fd50 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 61   available on ea
1fd51 63 68 20 73 69 62 6c 69 6e 67 2e 0d 0a 20 20 2a  ch sibling...  *
1fd52 2a 20 0d 0a 20 20 2a 2f 0d 0a 20 20 75 73 61 62  * ..  */..  usab
1fd53 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75  leSpace = pBt->u
1fd54 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b  sableSize - 12 +
1fd55 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
1fd56 0d 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c  ..  for(subtotal
1fd57 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  =k=i=0; i<nCell;
1fd58 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 61 73 73 65   i++){..    asse
1fd59 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20  rt( i<nMaxCells 
1fd5a 29 3b 0d 0a 20 20 20 20 73 75 62 74 6f 74 61 6c  );..    subtotal
1fd5b 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20   += szCell[i] + 
1fd5c 32 3b 0d 0a 20 20 20 20 69 66 28 20 73 75 62 74  2;..    if( subt
1fd5d 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61  otal > usableSpa
1fd5e 63 65 20 29 7b 0d 0a 20 20 20 20 20 20 73 7a 4e  ce ){..      szN
1fd5f 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c  ew[k] = subtotal
1fd60 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0d 0a 20   - szCell[i];.. 
1fd61 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d       cntNew[k] =
1fd62 20 69 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6c   i;..      if( l
1fd63 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20  eafData ){ i--; 
1fd64 7d 0d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61  }..      subtota
1fd65 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 6b 2b  l = 0;..      k+
1fd66 2b 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6b 3e  +;..      if( k>
1fd67 4e 42 2b 31 20 29 7b 20 72 63 20 3d 20 53 51 4c  NB+1 ){ rc = SQL
1fd68 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1fd69 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63  ; goto balance_c
1fd6a 6c 65 61 6e 75 70 3b 20 7d 0d 0a 20 20 20 20 7d  leanup; }..    }
1fd6b 0d 0a 20 20 7d 0d 0a 20 20 73 7a 4e 65 77 5b 6b  ..  }..  szNew[k
1fd6c 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0d 0a 20  ] = subtotal;.. 
1fd6d 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65   cntNew[k] = nCe
1fd6e 6c 6c 3b 0d 0a 20 20 6b 2b 2b 3b 0d 0a 0d 0a 20  ll;..  k++;.... 
1fd6f 20 2f 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 70 61   /*..  ** The pa
1fd70 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62  cking computed b
1fd71 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  y the previous b
1fd72 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74  lock is biased t
1fd73 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e  oward the siblin
1fd74 67 73 0d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  gs..  ** on the 
1fd75 6c 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20  left side.  The 
1fd76 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72  left siblings ar
1fd77 65 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20  e always nearly 
1fd78 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0d  full, while the.
1fd79 0a 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74  .  ** right-most
1fd7a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62   sibling might b
1fd7b 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20  e nearly empty. 
1fd7c 20 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   This block of c
1fd7d 6f 64 65 20 61 74 74 65 6d 70 74 73 0d 0a 20 20  ode attempts..  
1fd7e 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65  ** to adjust the
1fd7f 20 70 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c   packing of sibl
1fd80 69 6e 67 73 20 74 6f 20 67 65 74 20 61 20 62 65  ings to get a be
1fd81 74 74 65 72 20 62 61 6c 61 6e 63 65 2e 0d 0a 20  tter balance... 
1fd82 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69 73 20 61   **..  ** This a
1fd83 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d 6f 72  djustment is mor
1fd84 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 6d 69  e than an optimi
1fd85 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 61 63  zation.  The pac
1fd86 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 68 74  king above might
1fd87 0d 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 74  ..  ** be so out
1fd88 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20 74   of balance as t
1fd89 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 46  o be illegal.  F
1fd8a 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
1fd8b 72 69 67 68 74 2d 6d 6f 73 74 0d 0a 20 20 2a 2a  right-most..  **
1fd8c 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62   sibling might b
1fd8d 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70  e completely emp
1fd8e 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74  ty.  This adjust
1fd8f 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69  ment is not opti
1fd90 6f 6e 61 6c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66  onal...  */..  f
1fd91 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69  or(i=k-1; i>0; i
1fd92 2d 2d 29 7b 0d 0a 20 20 20 20 69 6e 74 20 73 7a  --){..    int sz
1fd93 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d  Right = szNew[i]
1fd94 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69  ;  /* Size of si
1fd95 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67  bling on the rig
1fd96 68 74 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 73  ht */..    int s
1fd97 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d  zLeft = szNew[i-
1fd98 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73  1]; /* Size of s
1fd99 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65  ibling on the le
1fd9a 66 74 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 72  ft */..    int r
1fd9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1fd9c 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68 74  * Index of right
1fd9d 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65  -most cell in le
1fd9e 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0d 0a 20  ft sibling */.. 
1fd9f 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20     int d;       
1fda0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1fda1 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f  of first cell to
1fda2 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67   the left of rig
1fda3 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0d 0a 0d  ht sibling */...
1fda4 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b  .    r = cntNew[
1fda5 69 2d 31 5d 20 2d 20 31 3b 0d 0a 20 20 20 20 64  i-1] - 1;..    d
1fda6 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
1fda7 61 74 61 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  ata;..    assert
1fda8 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( d<nMaxCells );
1fda9 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 3c  ..    assert( r<
1fdaa 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0d 0a 20 20  nMaxCells );..  
1fdab 20 20 77 68 69 6c 65 28 20 73 7a 52 69 67 68 74    while( szRight
1fdac 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73  ==0 || szRight+s
1fdad 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65  zCell[d]+2<=szLe
1fdae 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29  ft-(szCell[r]+2)
1fdaf 20 29 7b 0d 0a 20 20 20 20 20 20 73 7a 52 69 67   ){..      szRig
1fdb0 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20  ht += szCell[d] 
1fdb1 2b 20 32 3b 0d 0a 20 20 20 20 20 20 73 7a 4c 65  + 2;..      szLe
1fdb2 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 5d 20  ft -= szCell[r] 
1fdb3 2b 20 32 3b 0d 0a 20 20 20 20 20 20 63 6e 74 4e  + 2;..      cntN
1fdb4 65 77 5b 69 2d 31 5d 2d 2d 3b 0d 0a 20 20 20 20  ew[i-1]--;..    
1fdb5 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31    r = cntNew[i-1
1fdb6 5d 20 2d 20 31 3b 0d 0a 20 20 20 20 20 20 64 20  ] - 1;..      d 
1fdb7 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61  = r + 1 - leafDa
1fdb8 74 61 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ta;..    }..    
1fdb9 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67  szNew[i] = szRig
1fdba 68 74 3b 0d 0a 20 20 20 20 73 7a 4e 65 77 5b 69  ht;..    szNew[i
1fdbb 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0d 0a 20  -1] = szLeft;.. 
1fdbc 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 45 69 74 68 65   }....  /* Eithe
1fdbd 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f  r we found one o
1fdbe 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e  r more cells (cn
1fdbf 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 70  tnew[0])>0) or p
1fdc0 50 61 67 65 20 69 73 0d 0a 20 20 2a 2a 20 61 20  Page is..  ** a 
1fdc1 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67  virtual root pag
1fdc2 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f  e.  A virtual ro
1fdc3 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 6e 20  ot page is when 
1fdc4 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0d 0a 20  the real root.. 
1fdc5 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 65   ** page is page
1fdc6 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 68   1 and we are th
1fdc7 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20  e only child of 
1fdc8 74 68 61 74 20 70 61 67 65 2e 0d 0a 20 20 2a 2a  that page...  **
1fdc9 0d 0a 20 20 2a 2a 20 55 50 44 41 54 45 3a 20 20  ..  ** UPDATE:  
1fdca 54 68 65 20 61 73 73 65 72 74 28 29 20 62 65 6c  The assert() bel
1fdcb 6f 77 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  ow is not necess
1fdcc 61 72 69 6c 79 20 74 72 75 65 20 69 66 20 74 68  arily true if th
1fdcd 65 20 64 61 74 61 62 61 73 65 0d 0a 20 20 2a 2a  e database..  **
1fdce 20 66 69 6c 65 20 69 73 20 63 6f 72 72 75 70 74   file is corrupt
1fdcf 2e 20 20 54 68 65 20 63 6f 72 72 75 70 74 69 6f  .  The corruptio
1fdd0 6e 20 77 69 6c 6c 20 62 65 20 64 65 74 65 63 74  n will be detect
1fdd1 65 64 20 61 6e 64 20 72 65 70 6f 72 74 65 64 20  ed and reported 
1fdd2 6c 61 74 65 72 0d 0a 20 20 2a 2a 20 69 6e 20 74  later..  ** in t
1fdd3 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 73 6f  his procedure so
1fdd4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
1fdd5 64 20 74 6f 20 61 63 74 20 75 70 6f 6e 20 69 74  d to act upon it
1fdd6 20 6e 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a 23 69 66   now...  */..#if
1fdd7 20 30 0d 0a 20 20 61 73 73 65 72 74 28 20 63 6e   0..  assert( cn
1fdd8 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50  tNew[0]>0 || (pP
1fdd9 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26  arent->pgno==1 &
1fdda 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  & pParent->nCell
1fddb 3d 3d 30 29 20 29 3b 0d 0a 23 65 6e 64 69 66 0d  ==0) );..#endif.
1fddc 0a 0d 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c  ...  TRACE(("BAL
1fddd 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64  ANCE: old: %d %d
1fdde 20 25 64 20 20 22 2c 0d 0a 20 20 20 20 61 70 4f   %d  ",..    apO
1fddf 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0d 0a 20  ld[0]->pgno, .. 
1fde0 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 4f     nOld>=2 ? apO
1fde1 6c 64 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c  ld[1]->pgno : 0,
1fde2 0d 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20  ..    nOld>=3 ? 
1fde3 61 70 4f 6c 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a  apOld[2]->pgno :
1fde4 20 30 0d 0a 20 20 29 29 3b 0d 0a 0d 0a 20 20 2f   0..  ));....  /
1fde5 2a 0d 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65  *..  ** Allocate
1fde6 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 52   k new pages.  R
1fde7 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 77  euse old pages w
1fde8 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0d 0a  here possible...
1fde9 20 20 2a 2f 0d 0a 20 20 69 66 28 20 61 70 4f 6c    */..  if( apOl
1fdea 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b  d[0]->pgno<=1 ){
1fdeb 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
1fdec 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0d  E_CORRUPT_BKPT;.
1fded 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63  .    goto balanc
1fdee 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d  e_cleanup;..  }.
1fdef 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20 61  .  pageFlags = a
1fdf0 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74 61 5b 30  pOld[0]->aData[0
1fdf1 5d 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ];..  for(i=0; i
1fdf2 3c 6b 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 4d  <k; i++){..    M
1fdf3 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0d 0a 20  emPage *pNew;.. 
1fdf4 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b     if( i<nOld ){
1fdf5 0d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61  ..      pNew = a
1fdf6 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b  pNew[i] = apOld[
1fdf7 69 5d 3b 0d 0a 20 20 20 20 20 20 61 70 4f 6c 64  i];..      apOld
1fdf8 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  [i] = 0;..      
1fdf9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1fdfa 72 57 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 62  rWrite(pNew->pDb
1fdfb 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 6e 4e  Page);..      nN
1fdfc 65 77 2b 2b 3b 0d 0a 20 20 20 20 20 20 69 66 28  ew++;..      if(
1fdfd 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e   rc ) goto balan
1fdfe 63 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20  ce_cleanup;..   
1fdff 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61   }else{..      a
1fe00 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0d 0a 20  ssert( i>0 );.. 
1fe01 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
1fe02 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1fe03 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70   &pNew, &pgno, p
1fe04 67 6e 6f 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  gno, 0);..      
1fe05 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
1fe06 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a  lance_cleanup;..
1fe07 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d        apNew[i] =
1fe08 20 70 4e 65 77 3b 0d 0a 20 20 20 20 20 20 6e 4e   pNew;..      nN
1fe09 65 77 2b 2b 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f  ew++;....      /
1fe0a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65  * Set the pointe
1fe0b 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20  r-map entry for 
1fe0c 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20  the new sibling 
1fe0d 70 61 67 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  page. */..      
1fe0e 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
1fe0f 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 74 72   ){..        ptr
1fe10 6d 61 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77  mapPut(pBt, pNew
1fe11 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42  ->pgno, PTRMAP_B
1fe12 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70  TREE, pParent->p
1fe13 67 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  gno, &rc);..    
1fe14 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1fe15 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
1fe16 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
1fe17 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20 20 20  _cleanup;..     
1fe18 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
1fe19 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
1fe1a 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70  * Free any old p
1fe1b 61 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e  ages that were n
1fe1c 6f 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77  ot reused as new
1fe1d 20 70 61 67 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20   pages...  */.. 
1fe1e 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29   while( i<nOld )
1fe1f 7b 0d 0a 20 20 20 20 66 72 65 65 50 61 67 65 28  {..    freePage(
1fe20 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 63 29 3b 0d  apOld[i], &rc);.
1fe21 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1fe22 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
1fe23 75 70 3b 0d 0a 20 20 20 20 72 65 6c 65 61 73 65  up;..    release
1fe24 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0d  Page(apOld[i]);.
1fe25 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20  .    apOld[i] = 
1fe26 30 3b 0d 0a 20 20 20 20 69 2b 2b 3b 0d 0a 20 20  0;..    i++;..  
1fe27 7d 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  }....  /*..  ** 
1fe28 50 75 74 20 74 68 65 20 6e 65 77 20 70 61 67 65  Put the new page
1fe29 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f  s in accending o
1fe2a 72 64 65 72 2e 20 20 54 68 69 73 20 68 65 6c 70  rder.  This help
1fe2b 73 20 74 6f 0d 0a 20 20 2a 2a 20 6b 65 65 70 20  s to..  ** keep 
1fe2c 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 64  entries in the d
1fe2d 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 65  isk file in orde
1fe2e 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 61 6e  r so that a scan
1fe2f 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61  ..  ** of the ta
1fe30 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 20  ble is a linear 
1fe31 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65  scan through the
1fe32 20 66 69 6c 65 2e 20 20 54 68 61 74 0d 0a 20 20   file.  That..  
1fe33 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73  ** in turn helps
1fe34 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
1fe35 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72  ystem to deliver
1fe36 20 70 61 67 65 73 0d 0a 20 20 2a 2a 20 66 72 6f   pages..  ** fro
1fe37 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20  m the disk more 
1fe38 72 61 70 69 64 6c 79 2e 0d 0a 20 20 2a 2a 0d 0a  rapidly...  **..
1fe39 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69    ** An O(n^2) i
1fe3a 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c  nsertion sort al
1fe3b 67 6f 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c  gorithm is used,
1fe3c 20 62 75 74 20 73 69 6e 63 65 0d 0a 20 20 2a 2a   but since..  **
1fe3d 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65   n is never more
1fe3e 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c   than NB (a smal
1fe3f 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61  l constant), tha
1fe40 74 20 73 68 6f 75 6c 64 0d 0a 20 20 2a 2a 20 6e  t should..  ** n
1fe41 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e  ot be a problem.
1fe42 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57 68 65  ..  **..  ** Whe
1fe43 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e  n NB==3, this on
1fe44 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d  e optimization m
1fe45 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73  akes the databas
1fe46 65 0d 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35  e..  ** about 25
1fe47 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72  % faster for lar
1fe48 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e  ge insertions an
1fe49 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0d 0a 20 20  d deletions...  
1fe4a 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  */..  for(i=0; i
1fe4b 3c 6b 2d 31 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  <k-1; i++){..   
1fe4c 20 69 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e 65   int minV = apNe
1fe4d 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0d 0a 20 20 20  w[i]->pgno;..   
1fe4e 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0d 0a   int minI = i;..
1fe4f 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a      for(j=i+1; j
1fe50 3c 6b 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20  <k; j++){..     
1fe51 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70   if( apNew[j]->p
1fe52 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69  gno<(unsigned)mi
1fe53 6e 56 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 6d  nV ){..        m
1fe54 69 6e 49 20 3d 20 6a 3b 0d 0a 20 20 20 20 20 20  inI = j;..      
1fe55 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 6a    minV = apNew[j
1fe56 5d 2d 3e 70 67 6e 6f 3b 0d 0a 20 20 20 20 20 20  ]->pgno;..      
1fe57 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  }..    }..    if
1fe58 28 20 6d 69 6e 49 3e 69 20 29 7b 0d 0a 20 20 20  ( minI>i ){..   
1fe59 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0d     MemPage *pT;.
1fe5a 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e 65  .      pT = apNe
1fe5b 77 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 61 70 4e  w[i];..      apN
1fe5c 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69  ew[i] = apNew[mi
1fe5d 6e 49 5d 3b 0d 0a 20 20 20 20 20 20 61 70 4e 65  nI];..      apNe
1fe5e 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0d 0a 20  w[minI] = pT;.. 
1fe5f 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 54 52 41     }..  }..  TRA
1fe60 43 45 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29  CE(("new: %d(%d)
1fe61 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
1fe62 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c  d(%d) %d(%d)\n",
1fe63 0d 0a 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e  ..    apNew[0]->
1fe64 70 67 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0d  pgno, szNew[0],.
1fe65 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61  .    nNew>=2 ? a
1fe66 70 4e 65 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20  pNew[1]->pgno : 
1fe67 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e  0, nNew>=2 ? szN
1fe68 65 77 5b 31 5d 20 3a 20 30 2c 0d 0a 20 20 20 20  ew[1] : 0,..    
1fe69 6e 4e 65 77 3e 3d 33 20 3f 20 61 70 4e 65 77 5b  nNew>=3 ? apNew[
1fe6a 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e  2]->pgno : 0, nN
1fe6b 65 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d  ew>=3 ? szNew[2]
1fe6c 20 3a 20 30 2c 0d 0a 20 20 20 20 6e 4e 65 77 3e   : 0,..    nNew>
1fe6d 3d 34 20 3f 20 61 70 4e 65 77 5b 33 5d 2d 3e 70  =4 ? apNew[3]->p
1fe6e 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34  gno : 0, nNew>=4
1fe6f 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c   ? szNew[3] : 0,
1fe70 0d 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20  ..    nNew>=5 ? 
1fe71 61 70 4e 65 77 5b 34 5d 2d 3e 70 67 6e 6f 20 3a  apNew[4]->pgno :
1fe72 20 30 2c 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a   0, nNew>=5 ? sz
1fe73 4e 65 77 5b 34 5d 20 3a 20 30 29 29 3b 0d 0a 0d  New[4] : 0));...
1fe74 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1fe75 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1fe76 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50  le(pParent->pDbP
1fe77 61 67 65 29 20 29 3b 0d 0a 20 20 70 75 74 34 62  age) );..  put4b
1fe78 79 74 65 28 70 52 69 67 68 74 2c 20 61 70 4e 65  yte(pRight, apNe
1fe79 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67 6e 6f 29  w[nNew-1]->pgno)
1fe7a 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  ;....  /*..  ** 
1fe7b 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74  Evenly distribut
1fe7c 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 70  e the data in ap
1fe7d 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68  Cell[] across th
1fe7e 65 20 6e 65 77 20 70 61 67 65 73 2e 0d 0a 20 20  e new pages...  
1fe7f 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65  ** Insert divide
1fe80 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61  r cells into pPa
1fe81 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72  rent as necessar
1fe82 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6a 20 3d 20  y...  */..  j = 
1fe83 30 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0;..  for(i=0; i
1fe84 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0d 0a 20 20  <nNew; i++){..  
1fe85 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68    /* Assemble th
1fe86 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61  e new sibling pa
1fe87 67 65 2e 20 2a 2f 0d 0a 20 20 20 20 4d 65 6d 50  ge. */..    MemP
1fe88 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65  age *pNew = apNe
1fe89 77 5b 69 5d 3b 0d 0a 20 20 20 20 61 73 73 65 72  w[i];..    asser
1fe8a 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( j<nMaxCells )
1fe8b 3b 0d 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  ;..    zeroPage(
1fe8c 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 73 29  pNew, pageFlags)
1fe8d 3b 0d 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50  ;..    assembleP
1fe8e 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77  age(pNew, cntNew
1fe8f 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a  [i]-j, &apCell[j
1fe90 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0d  ], &szCell[j]);.
1fe91 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
1fe92 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e  w->nCell>0 || (n
1fe93 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77  New==1 && cntNew
1fe94 5b 30 5d 3d 3d 30 29 20 29 3b 0d 0a 20 20 20 20  [0]==0) );..    
1fe95 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f  assert( pNew->nO
1fe96 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0d 0a 0d  verflow==0 );...
1fe97 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b  .    j = cntNew[
1fe98 69 5d 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66  i];....    /* If
1fe99 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67   the sibling pag
1fe9a 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76  e assembled abov
1fe9b 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72 69  e was not the ri
1fe9c 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67  ght-most sibling
1fe9d 2c 0d 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74  ,..    ** insert
1fe9e 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20   a divider cell 
1fe9f 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  into the parent 
1fea0 70 61 67 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  page...    */.. 
1fea1 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65     assert( i<nNe
1fea2 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20  w-1 || j==nCell 
1fea3 29 3b 0d 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43  );..    if( j<nC
1fea4 65 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 75 38  ell ){..      u8
1fea5 20 2a 70 43 65 6c 6c 3b 0d 0a 20 20 20 20 20 20   *pCell;..      
1fea6 75 38 20 2a 70 54 65 6d 70 3b 0d 0a 20 20 20 20  u8 *pTemp;..    
1fea7 20 20 69 6e 74 20 73 7a 3b 0d 0a 0d 0a 20 20 20    int sz;....   
1fea8 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61     assert( j<nMa
1fea9 78 43 65 6c 6c 73 20 29 3b 0d 0a 20 20 20 20 20  xCells );..     
1feaa 20 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b   pCell = apCell[
1feab 6a 5d 3b 0d 0a 20 20 20 20 20 20 73 7a 20 3d 20  j];..      sz = 
1feac 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 66  szCell[j] + leaf
1fead 43 6f 72 72 65 63 74 69 6f 6e 3b 0d 0a 20 20 20  Correction;..   
1feae 20 20 20 70 54 65 6d 70 20 3d 20 26 61 4f 76 66     pTemp = &aOvf
1feaf 6c 53 70 61 63 65 5b 69 4f 76 66 6c 53 70 61 63  lSpace[iOvflSpac
1feb0 65 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21  e];..      if( !
1feb1 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20  pNew->leaf ){.. 
1feb2 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
1feb3 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70  New->aData[8], p
1feb4 43 65 6c 6c 2c 20 34 29 3b 0d 0a 20 20 20 20 20  Cell, 4);..     
1feb5 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 66 44   }else if( leafD
1feb6 61 74 61 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ata ){..        
1feb7 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 20 69  /* If the tree i
1feb8 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72  s a leaf-data tr
1feb9 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62 6c  ee, and the sibl
1feba 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73 2c  ings are leaves,
1febb 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68   ..        ** th
1febc 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64  en there is no d
1febd 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61  ivider cell in a
1febe 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64  pCell[]. Instead
1febf 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0d 0a  , the divider ..
1fec0 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20          ** cell 
1fec1 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
1fec2 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  integer key for 
1fec3 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  the right-most c
1fec4 65 6c 6c 20 6f 66 20 0d 0a 20 20 20 20 20 20 20  ell of ..       
1fec5 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d   ** the sibling-
1fec6 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61  page assembled a
1fec7 62 6f 76 65 20 6f 6e 6c 79 2e 0d 0a 20 20 20 20  bove only...    
1fec8 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
1fec9 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0d 0a  CellInfo info;..
1feca 20 20 20 20 20 20 20 20 6a 2d 2d 3b 0d 0a 20 20          j--;..  
1fecb 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65        btreeParse
1fecc 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70  CellPtr(pNew, ap
1fecd 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b  Cell[j], &info);
1fece 0d 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20  ..        pCell 
1fecf 3d 20 70 54 65 6d 70 3b 0d 0a 20 20 20 20 20 20  = pTemp;..      
1fed0 20 20 73 7a 20 3d 20 34 20 2b 20 70 75 74 56 61    sz = 4 + putVa
1fed1 72 69 6e 74 28 26 70 43 65 6c 6c 5b 34 5d 2c 20  rint(&pCell[4], 
1fed2 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0d 0a 20 20 20  info.nKey);..   
1fed3 20 20 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0d       pTemp = 0;.
1fed4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
1fed5 20 20 20 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20         pCell -= 
1fed6 34 3b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f  4;..        /* O
1fed7 62 73 63 75 72 65 20 63 61 73 65 20 66 6f 72 20  bscure case for 
1fed8 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 20 74 72  non-leaf-data tr
1fed9 65 65 73 3a 20 49 66 20 74 68 65 20 63 65 6c 6c  ees: If the cell
1feda 20 61 74 20 70 43 65 6c 6c 20 77 61 73 0d 0a 20   at pCell was.. 
1fedb 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f         ** previo
1fedc 75 73 6c 79 20 73 74 6f 72 65 64 20 6f 6e 20 61  usly stored on a
1fedd 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61 6e 64 20   leaf node, and 
1fede 69 74 73 20 72 65 70 6f 72 74 65 64 20 73 69 7a  its reported siz
1fedf 65 20 77 61 73 20 34 0d 0a 20 20 20 20 20 20 20  e was 4..       
1fee0 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65 6e 20   ** bytes, then 
1fee1 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20  it may actually 
1fee2 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
1fee3 74 68 69 73 20 0d 0a 20 20 20 20 20 20 20 20 2a  this ..        *
1fee4 2a 20 28 73 65 65 20 62 74 72 65 65 50 61 72 73  * (see btreePars
1fee5 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 79  eCellPtr(), 4 by
1fee6 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69 6d  tes is the minim
1fee7 75 6d 20 73 69 7a 65 20 6f 66 0d 0a 20 20 20 20  um size of..    
1fee8 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29      ** any cell)
1fee9 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f  . But it is impo
1feea 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68  rtant to pass th
1feeb 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74  e correct size t
1feec 6f 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  o ..        ** i
1feed 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20  nsertCell(), so 
1feee 72 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c  reparse the cell
1feef 20 6e 6f 77 2e 0d 0a 20 20 20 20 20 20 20 20 2a   now...        *
1fef0 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f  *..        ** No
1fef1 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6e  te that this can
1fef2 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e   never happen in
1fef3 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20   an SQLite data 
1fef4 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0d 0a 20 20  file, as all..  
1fef5 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61        ** cells a
1fef6 72 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79  re at least 4 by
1fef7 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70  tes. It only hap
1fef8 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20  pens in b-trees 
1fef9 75 73 65 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a  used..        **
1fefa 20 74 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e   to evaluate "IN
1fefb 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61   (SELECT ...)" a
1fefc 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73  nd similar claus
1fefd 65 73 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d  es...        */.
1fefe 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43  .        if( szC
1feff 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0d 0a 20 20  ell[j]==4 ){..  
1ff00 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c          assert(l
1ff01 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34  eafCorrection==4
1ff02 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 7a  );..          sz
1ff03 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
1ff04 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0d  Parent, pCell);.
1ff05 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
1ff06 20 20 7d 0d 0a 20 20 20 20 20 20 69 4f 76 66 6c    }..      iOvfl
1ff07 53 70 61 63 65 20 2b 3d 20 73 7a 3b 0d 0a 20 20  Space += sz;..  
1ff08 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
1ff09 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 32 33  pBt->maxLocal+23
1ff0a 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
1ff0b 74 28 20 69 4f 76 66 6c 53 70 61 63 65 20 3c 3d  t( iOvflSpace <=
1ff0c 20 28 69 6e 74 29 70 42 74 2d 3e 70 61 67 65 53   (int)pBt->pageS
1ff0d 69 7a 65 20 29 3b 0d 0a 20 20 20 20 20 20 69 6e  ize );..      in
1ff0e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74  sertCell(pParent
1ff0f 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20  , nxDiv, pCell, 
1ff10 73 7a 2c 20 70 54 65 6d 70 2c 20 70 4e 65 77 2d  sz, pTemp, pNew-
1ff11 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20  >pgno, &rc);..  
1ff12 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1ff13 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c  TE_OK ) goto bal
1ff14 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  ance_cleanup;.. 
1ff15 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
1ff16 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1ff17 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44  able(pParent->pD
1ff18 62 50 61 67 65 29 20 29 3b 0d 0a 0d 0a 20 20 20  bPage) );....   
1ff19 20 20 20 6a 2b 2b 3b 0d 0a 20 20 20 20 20 20 6e     j++;..      n
1ff1a 78 44 69 76 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a  xDiv++;..    }..
1ff1b 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 6a    }..  assert( j
1ff1c 3d 3d 6e 43 65 6c 6c 20 29 3b 0d 0a 20 20 61 73  ==nCell );..  as
1ff1d 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0d  sert( nOld>0 );.
1ff1e 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e  .  assert( nNew>
1ff1f 30 20 29 3b 0d 0a 20 20 69 66 28 20 28 70 61 67  0 );..  if( (pag
1ff20 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41  eFlags & PTF_LEA
1ff21 46 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 75 38  F)==0 ){..    u8
1ff22 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f   *zChild = &apCo
1ff23 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74  py[nOld-1]->aDat
1ff24 61 5b 38 5d 3b 0d 0a 20 20 20 20 6d 65 6d 63 70  a[8];..    memcp
1ff25 79 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d  y(&apNew[nNew-1]
1ff26 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69  ->aData[8], zChi
1ff27 6c 64 2c 20 34 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ld, 4);..  }....
1ff28 20 20 69 66 28 20 69 73 52 6f 6f 74 20 26 26 20    if( isRoot && 
1ff29 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d  pParent->nCell==
1ff2a 30 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 68 64  0 && pParent->hd
1ff2b 72 4f 66 66 73 65 74 3c 3d 61 70 4e 65 77 5b 30  rOffset<=apNew[0
1ff2c 5d 2d 3e 6e 46 72 65 65 20 29 7b 0d 0a 20 20 20  ]->nFree ){..   
1ff2d 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67   /* The root pag
1ff2e 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20  e of the b-tree 
1ff2f 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  now contains no 
1ff30 63 65 6c 6c 73 2e 20 54 68 65 20 6f 6e 6c 79 20  cells. The only 
1ff31 73 69 62 6c 69 6e 67 0d 0a 20 20 20 20 2a 2a 20  sibling..    ** 
1ff32 70 61 67 65 20 69 73 20 74 68 65 20 72 69 67 68  page is the righ
1ff33 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70  t-child of the p
1ff34 61 72 65 6e 74 2e 20 43 6f 70 79 20 74 68 65 20  arent. Copy the 
1ff35 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0d  contents of the.
1ff36 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 20 70 61  .    ** child pa
1ff37 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ge into the pare
1ff38 6e 74 2c 20 64 65 63 72 65 61 73 69 6e 67 20 74  nt, decreasing t
1ff39 68 65 20 6f 76 65 72 61 6c 6c 20 68 65 69 67 68  he overall heigh
1ff3a 74 20 6f 66 20 74 68 65 0d 0a 20 20 20 20 2a 2a  t of the..    **
1ff3b 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
1ff3c 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 73 20 69  e by one. This i
1ff3d 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20 74  s described as t
1ff3e 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 68 61 6c  he "balance-shal
1ff3f 6c 6f 77 65 72 22 0d 0a 20 20 20 20 2a 2a 20 73  lower"..    ** s
1ff40 75 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20  ub-algorithm in 
1ff41 73 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 74 69  some documentati
1ff42 6f 6e 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  on...    **..   
1ff43 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   ** If this is a
1ff44 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
1ff45 74 61 62 61 73 65 2c 20 74 68 65 20 63 61 6c 6c  tabase, the call
1ff46 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74   to copyNodeCont
1ff47 65 6e 74 28 29 20 0d 0a 20 20 20 20 2a 2a 20 73  ent() ..    ** s
1ff48 65 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d  ets all pointer-
1ff49 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f 72 72  map entries corr
1ff4a 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74  esponding to dat
1ff4b 61 62 61 73 65 20 69 6d 61 67 65 20 70 61 67 65  abase image page
1ff4c 73 20 0d 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77  s ..    ** for w
1ff4d 68 69 63 68 20 74 68 65 20 70 6f 69 6e 74 65 72  hich the pointer
1ff4e 20 69 73 20 73 74 6f 72 65 64 20 77 69 74 68 69   is stored withi
1ff4f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65  n the content be
1ff50 69 6e 67 20 63 6f 70 69 65 64 2e 0d 0a 20 20 20  ing copied...   
1ff51 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20   **..    ** The 
1ff52 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62 65  second assert be
1ff53 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 61  low verifies tha
1ff54 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  t the child page
1ff55 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65 64   is defragmented
1ff56 0d 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73  ..    ** (it mus
1ff57 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 20  t be, as it was 
1ff58 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74  just reconstruct
1ff59 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c  ed using assembl
1ff5a 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0d 0a  ePage()). This..
1ff5b 20 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74      ** is import
1ff5c 61 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e  ant if the paren
1ff5d 74 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74  t page happens t
1ff5e 6f 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74  o be page 1 of t
1ff5f 68 65 20 64 61 74 61 62 61 73 65 0d 0a 20 20 20  he database..   
1ff60 20 2a 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0d 0a   ** image.  */..
1ff61 20 20 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77      assert( nNew
1ff62 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ==1 );..    asse
1ff63 72 74 28 20 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46  rt( apNew[0]->nF
1ff64 72 65 65 20 3d 3d 20 0d 0a 20 20 20 20 20 20 20  ree == ..       
1ff65 20 28 67 65 74 32 62 79 74 65 28 26 61 70 4e 65   (get2byte(&apNe
1ff66 77 5b 30 5d 2d 3e 61 44 61 74 61 5b 35 5d 29 2d  w[0]->aData[5])-
1ff67 61 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c 6c 4f 66  apNew[0]->cellOf
1ff68 66 73 65 74 2d 61 70 4e 65 77 5b 30 5d 2d 3e 6e  fset-apNew[0]->n
1ff69 43 65 6c 6c 2a 32 29 20 0d 0a 20 20 20 20 29 3b  Cell*2) ..    );
1ff6a 0d 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f  ..    copyNodeCo
1ff6b 6e 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d 2c 20  ntent(apNew[0], 
1ff6c 70 50 61 72 65 6e 74 2c 20 26 72 63 29 3b 0d 0a  pParent, &rc);..
1ff6d 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4e      freePage(apN
1ff6e 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0d 0a 20 20  ew[0], &rc);..  
1ff6f 7d 65 6c 73 65 20 69 66 28 20 49 53 41 55 54 4f  }else if( ISAUTO
1ff70 56 41 43 55 55 4d 20 29 7b 0d 0a 20 20 20 20 2f  VACUUM ){..    /
1ff71 2a 20 46 69 78 20 74 68 65 20 70 6f 69 6e 74 65  * Fix the pointe
1ff72 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r-map entries fo
1ff73 72 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 20  r all the cells 
1ff74 74 68 61 74 20 77 65 72 65 20 73 68 69 66 74 65  that were shifte
1ff75 64 20 61 72 6f 75 6e 64 2e 20 0d 0a 20 20 20 20  d around. ..    
1ff76 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73 65 76  ** There are sev
1ff77 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 74  eral different t
1ff78 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d  ypes of pointer-
1ff79 6d 61 70 20 65 6e 74 72 69 65 73 20 74 68 61 74  map entries that
1ff7a 20 6e 65 65 64 20 74 6f 0d 0a 20 20 20 20 2a 2a   need to..    **
1ff7b 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62   be dealt with b
1ff7c 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  y this routine. 
1ff7d 53 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 68 61  Some of these ha
1ff7e 76 65 20 62 65 65 6e 20 73 65 74 20 61 6c 72 65  ve been set alre
1ff7f 61 64 79 2c 20 62 75 74 0d 0a 20 20 20 20 2a 2a  ady, but..    **
1ff80 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e 20   many have not. 
1ff81 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  The following is
1ff82 20 61 20 73 75 6d 6d 61 72 79 3a 0d 0a 20 20 20   a summary:..   
1ff83 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 31 29   **..    **   1)
1ff84 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 73 73   The entries ass
1ff85 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77  ociated with new
1ff86 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74   sibling pages t
1ff87 68 61 74 20 77 65 72 65 20 6e 6f 74 0d 0a 20 20  hat were not..  
1ff88 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e    **      siblin
1ff89 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  gs when this fun
1ff8a 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64  ction was called
1ff8b 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c 72  . These have alr
1ff8c 65 61 64 79 0d 0a 20 20 20 20 2a 2a 20 20 20 20  eady..    **    
1ff8d 20 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64    been set. We d
1ff8e 6f 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72  on't need to wor
1ff8f 72 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62  ry about old sib
1ff90 6c 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0d  lings that were.
1ff91 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76  .    **      mov
1ff92 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c  ed to the free-l
1ff93 69 73 74 20 2d 20 74 68 65 20 66 72 65 65 50 61  ist - the freePa
1ff94 67 65 28 29 20 63 6f 64 65 20 68 61 73 20 74 61  ge() code has ta
1ff95 6b 65 6e 20 63 61 72 65 0d 0a 20 20 20 20 2a 2a  ken care..    **
1ff96 20 20 20 20 20 20 6f 66 20 74 68 6f 73 65 2e 0d        of those..
1ff97 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
1ff98 20 20 32 29 20 54 68 65 20 70 6f 69 6e 74 65 72    2) The pointer
1ff99 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73 73  -map entries ass
1ff9a 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1ff9b 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 0d   first overflow.
1ff9c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 70 61 67  .    **      pag
1ff9d 65 20 69 6e 20 61 6e 79 20 6f 76 65 72 66 6c 6f  e in any overflo
1ff9e 77 20 63 68 61 69 6e 73 20 75 73 65 64 20 62 79  w chains used by
1ff9f 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c   new divider cel
1ffa0 6c 73 2e 20 54 68 65 73 65 20 0d 0a 20 20 20 20  ls. These ..    
1ffa1 2a 2a 20 20 20 20 20 20 68 61 76 65 20 61 6c 73  **      have als
1ffa2 6f 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 74  o already been t
1ffa3 61 6b 65 6e 20 63 61 72 65 20 6f 66 20 62 79 20  aken care of by 
1ffa4 74 68 65 20 69 6e 73 65 72 74 43 65 6c 6c 28 29  the insertCell()
1ffa5 20 63 6f 64 65 2e 0d 0a 20 20 20 20 2a 2a 0d 0a   code...    **..
1ffa6 20 20 20 20 2a 2a 20 20 20 33 29 20 49 66 20 74      **   3) If t
1ffa7 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  he sibling pages
1ffa8 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c   are not leaves,
1ffa9 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20   then the child 
1ffaa 70 61 67 65 73 20 6f 66 0d 0a 20 20 20 20 2a 2a  pages of..    **
1ffab 20 20 20 20 20 20 63 65 6c 6c 73 20 73 74 6f 72        cells stor
1ffac 65 64 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 6e  ed on the siblin
1ffad 67 20 70 61 67 65 73 20 6d 61 79 20 6e 65 65 64  g pages may need
1ffae 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0d   to be updated..
1ffaf 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
1ffb0 20 20 34 29 20 49 66 20 74 68 65 20 73 69 62 6c    4) If the sibl
1ffb1 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f  ing pages are no
1ffb2 74 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65  t internal intke
1ffb3 79 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 61 6e  y nodes, then an
1ffb4 79 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f  y..    **      o
1ffb5 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 75 73  verflow pages us
1ffb6 65 64 20 62 79 20 74 68 65 73 65 20 63 65 6c 6c  ed by these cell
1ffb7 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65  s may need to be
1ffb8 20 75 70 64 61 74 65 64 0d 0a 20 20 20 20 2a 2a   updated..    **
1ffb9 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61 6c 20        (internal 
1ffba 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76  intkey nodes nev
1ffbb 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74  er contain point
1ffbc 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20  ers to overflow 
1ffbd 70 61 67 65 73 29 2e 0d 0a 20 20 20 20 2a 2a 0d  pages)...    **.
1ffbe 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20  .    **   5) If 
1ffbf 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
1ffc0 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73  s are not leaves
1ffc1 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74  , then the point
1ffc2 65 72 2d 6d 61 70 0d 0a 20 20 20 20 2a 2a 20 20  er-map..    **  
1ffc3 20 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20      entries for 
1ffc4 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
1ffc5 70 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69  pages of each si
1ffc6 62 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0d 0a  bling may need..
1ffc7 20 20 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62      **      to b
1ffc8 65 20 75 70 64 61 74 65 64 2e 0d 0a 20 20 20 20  e updated...    
1ffc9 2a 2a 0d 0a 20 20 20 20 2a 2a 20 43 61 73 65 73  **..    ** Cases
1ffca 20 31 20 61 6e 64 20 32 20 61 72 65 20 64 65 61   1 and 2 are dea
1ffcb 6c 74 20 77 69 74 68 20 61 62 6f 76 65 20 62 79  lt with above by
1ffcc 20 6f 74 68 65 72 20 63 6f 64 65 2e 20 54 68 65   other code. The
1ffcd 20 6e 65 78 74 0d 0a 20 20 20 20 2a 2a 20 62 6c   next..    ** bl
1ffce 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 63  ock deals with c
1ffcf 61 73 65 73 20 33 20 61 6e 64 20 34 20 61 6e 64  ases 3 and 4 and
1ffd0 20 74 68 65 20 6f 6e 65 20 61 66 74 65 72 20 74   the one after t
1ffd1 68 61 74 2c 20 63 61 73 65 20 35 2e 20 53 69 6e  hat, case 5. Sin
1ffd2 63 65 0d 0a 20 20 20 20 2a 2a 20 73 65 74 74 69  ce..    ** setti
1ffd3 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70  ng a pointer map
1ffd4 20 65 6e 74 72 79 20 69 73 20 61 20 72 65 6c 61   entry is a rela
1ffd5 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 76 65  tively expensive
1ffd6 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73   operation, this
1ffd7 0d 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 6f 6e  ..    ** code on
1ffd8 6c 79 20 73 65 74 73 20 70 6f 69 6e 74 65 72 20  ly sets pointer 
1ffd9 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
1ffda 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 6c 6f  child or overflo
1ffdb 77 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76  w pages that hav
1ffdc 65 0d 0a 20 20 20 20 2a 2a 20 61 63 74 75 61 6c  e..    ** actual
1ffdd 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 65 65 6e  ly moved between
1ffde 20 70 61 67 65 73 2e 20 20 2a 2f 0d 0a 20 20 20   pages.  */..   
1ffdf 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d   MemPage *pNew =
1ffe0 20 61 70 4e 65 77 5b 30 5d 3b 0d 0a 20 20 20 20   apNew[0];..    
1ffe1 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20  MemPage *pOld = 
1ffe2 61 70 43 6f 70 79 5b 30 5d 3b 0d 0a 20 20 20 20  apCopy[0];..    
1ffe3 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  int nOverflow = 
1ffe4 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  pOld->nOverflow;
1ffe5 0d 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 74 4f  ..    int iNextO
1ffe6 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c  ld = pOld->nCell
1ffe7 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0d 0a 20   + nOverflow;.. 
1ffe8 20 20 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f 77     int iOverflow
1ffe9 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f 20   = (nOverflow ? 
1ffea 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  pOld->aOvfl[0].i
1ffeb 64 78 20 3a 20 2d 31 29 3b 0d 0a 20 20 20 20 6a  dx : -1);..    j
1ffec 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1ffed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffee 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c    /* Current 'ol
1ffef 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  d' sibling page 
1fff0 2a 2f 0d 0a 20 20 20 20 6b 20 3d 20 30 3b 20 20  */..    k = 0;  
1fff1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fff2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
1fff3 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62 6c  rrent 'new' sibl
1fff4 69 6e 67 20 70 61 67 65 20 2a 2f 0d 0a 20 20 20  ing page */..   
1fff5 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
1fff6 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  l; i++){..      
1fff7 69 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20  int isDivider = 
1fff8 30 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28  0;..      while(
1fff9 20 69 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0d   i==iNextOld ){.
1fffa 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c  .        /* Cell
1fffb 20 69 20 69 73 20 74 68 65 20 63 65 6c 6c 20 69   i is the cell i
1fffc 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
1fffd 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 63 65  wing the last ce
1fffe 6c 6c 20 6f 6e 20 6f 6c 64 0d 0a 20 20 20 20 20  ll on old..     
1ffff 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61     ** sibling pa
20000 67 65 20 6a 2e 20 49 66 20 74 68 65 20 73 69 62  ge j. If the sib
20001 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65  lings are not le
20002 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e 0d 0a  af pages of an..
20003 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b 65          ** intke
20004 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 63  y b-tree, then c
20005 65 6c 6c 20 69 20 77 61 73 20 61 20 64 69 76 69  ell i was a divi
20006 64 65 72 20 63 65 6c 6c 2e 20 2a 2f 0d 0a 20 20  der cell. */..  
20007 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 2b        assert( j+
20008 31 20 3c 20 41 72 72 61 79 53 69 7a 65 28 61 70  1 < ArraySize(ap
20009 43 6f 70 79 29 20 29 3b 0d 0a 20 20 20 20 20 20  Copy) );..      
2000a 20 20 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b    pOld = apCopy[
2000b 2b 2b 6a 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69  ++j];..        i
2000c 4e 65 78 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c  NextOld = i + !l
2000d 65 61 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e  eafData + pOld->
2000e 6e 43 65 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f  nCell + pOld->nO
2000f 76 65 72 66 6c 6f 77 3b 0d 0a 20 20 20 20 20 20  verflow;..      
20010 20 20 69 66 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65    if( pOld->nOve
20011 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20 20 20 20  rflow ){..      
20012 20 20 20 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20      nOverflow = 
20013 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  pOld->nOverflow;
20014 0d 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 65  ..          iOve
20015 72 66 6c 6f 77 20 3d 20 69 20 2b 20 21 6c 65 61  rflow = i + !lea
20016 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 61 4f  fData + pOld->aO
20017 76 66 6c 5b 30 5d 2e 69 64 78 3b 0d 0a 20 20 20  vfl[0].idx;..   
20018 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
20019 69 73 44 69 76 69 64 65 72 20 3d 20 21 6c 65 61  isDivider = !lea
2001a 66 44 61 74 61 3b 20 20 0d 0a 20 20 20 20 20 20  fData;  ..      
2001b 7d 0d 0a 0d 0a 20 20 20 20 20 20 61 73 73 65 72  }....      asser
2001c 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c  t(nOverflow>0 ||
2001d 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 3b 0d   iOverflow<i );.
2001e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f  .      assert(nO
2001f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c  verflow<2 || pOl
20020 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d  d->aOvfl[0].idx=
20021 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e  =pOld->aOvfl[1].
20022 69 64 78 2d 31 29 3b 0d 0a 20 20 20 20 20 20 61  idx-1);..      a
20023 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c  ssert(nOverflow<
20024 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c  3 || pOld->aOvfl
20025 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61  [1].idx==pOld->a
20026 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b 0d  Ovfl[2].idx-1);.
20027 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 69 4f  .      if( i==iO
20028 76 65 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20 20  verflow ){..    
20029 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20      isDivider = 
2002a 31 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  1;..        if( 
2002b 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20  (--nOverflow)>0 
2002c 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 4f  ){..          iO
2002d 76 65 72 66 6c 6f 77 2b 2b 3b 0d 0a 20 20 20 20  verflow++;..    
2002e 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
2002f 0d 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 63  ..      if( i==c
20030 6e 74 4e 65 77 5b 6b 5d 20 29 7b 0d 0a 20 20 20  ntNew[k] ){..   
20031 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69       /* Cell i i
20032 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64  s the cell immed
20033 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67  iately following
20034 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f   the last cell o
20035 6e 20 6e 65 77 0d 0a 20 20 20 20 20 20 20 20 2a  n new..        *
20036 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6b  * sibling page k
20037 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  . If the sibling
20038 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70  s are not leaf p
20039 61 67 65 73 20 6f 66 20 61 6e 0d 0a 20 20 20 20  ages of an..    
2003a 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d      ** intkey b-
2003b 74 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20  tree, then cell 
2003c 69 20 69 73 20 61 20 64 69 76 69 64 65 72 20 63  i is a divider c
2003d 65 6c 6c 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20  ell.  */..      
2003e 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 2b    pNew = apNew[+
2003f 2b 6b 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  +k];..        if
20040 28 20 21 6c 65 61 66 44 61 74 61 20 29 20 63 6f  ( !leafData ) co
20041 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 7d  ntinue;..      }
20042 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
20043 6a 3c 6e 4f 6c 64 20 29 3b 0d 0a 20 20 20 20 20  j<nOld );..     
20044 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 20   assert( k<nNew 
20045 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49  );....      /* I
20046 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6f  f the cell was o
20047 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 65  riginally divide
20048 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 6e  r cell (and is n
20049 6f 74 20 6e 6f 77 29 20 6f 72 0d 0a 20 20 20 20  ot now) or..    
2004a 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77    ** an overflow
2004b 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65   cell, or if the
2004c 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65   cell was locate
2004d 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  d on a different
2004e 20 73 69 62 6c 69 6e 67 0d 0a 20 20 20 20 20 20   sibling..      
2004f 2a 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74  ** page before t
20050 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68  he balancing, th
20051 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  en the pointer m
20052 61 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63  ap entries assoc
20053 69 61 74 65 64 0d 0a 20 20 20 20 20 20 2a 2a 20  iated..      ** 
20054 77 69 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f  with any child o
20055 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  r overflow pages
20056 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61   need to be upda
20057 74 65 64 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20  ted.  */..      
20058 69 66 28 20 69 73 44 69 76 69 64 65 72 20 7c 7c  if( isDivider ||
20059 20 70 4f 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65   pOld->pgno!=pNe
2005a 77 2d 3e 70 67 6e 6f 20 29 7b 0d 0a 20 20 20 20  w->pgno ){..    
2005b 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72      if( !leafCor
2005c 72 65 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20 20  rection ){..    
2005d 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28        ptrmapPut(
2005e 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 61 70  pBt, get4byte(ap
2005f 43 65 6c 6c 5b 69 5d 29 2c 20 50 54 52 4d 41 50  Cell[i]), PTRMAP
20060 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d 3e 70 67  _BTREE, pNew->pg
20061 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20 20  no, &rc);..     
20062 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66     }..        if
20063 28 20 73 7a 43 65 6c 6c 5b 69 5d 3e 70 4e 65 77  ( szCell[i]>pNew
20064 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0d 0a 20  ->minLocal ){.. 
20065 20 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50           ptrmapP
20066 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20  utOvflPtr(pNew, 
20067 61 70 43 65 6c 6c 5b 69 5d 2c 20 26 72 63 29 3b  apCell[i], &rc);
20068 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
20069 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20     }..    }.... 
2006a 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72     if( !leafCorr
2006b 65 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20 20 20  ection ){..     
2006c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77   for(i=0; i<nNew
2006d 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; i++){..       
2006e 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 62   u32 key = get4b
2006f 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e 61  yte(&apNew[i]->a
20070 44 61 74 61 5b 38 5d 29 3b 0d 0a 20 20 20 20 20  Data[8]);..     
20071 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
20072 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54  , key, PTRMAP_BT
20073 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70  REE, apNew[i]->p
20074 67 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  gno, &rc);..    
20075 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69    }..    }....#i
20076 66 20 30 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20  f 0..    /* The 
20077 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73  ptrmapCheckPages
20078 28 29 20 63 6f 6e 74 61 69 6e 73 20 61 73 73 65  () contains asse
20079 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
2007a 74 68 61 74 20 76 65 72 69 66 79 20 74 68 61 74  that verify that
2007b 0d 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 70 6f 69  ..    ** all poi
2007c 6e 74 65 72 20 6d 61 70 20 70 61 67 65 73 20 61  nter map pages a
2007d 72 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79  re set correctly
2007e 2e 20 54 68 69 73 20 69 73 20 68 65 6c 70 66 75  . This is helpfu
2007f 6c 20 77 68 69 6c 65 20 0d 0a 20 20 20 20 2a 2a  l while ..    **
20080 20 64 65 62 75 67 67 69 6e 67 2e 20 54 68 69 73   debugging. This
20081 20 69 73 20 75 73 75 61 6c 6c 79 20 64 69 73 61   is usually disa
20082 62 6c 65 64 20 62 65 63 61 75 73 65 20 61 20 63  bled because a c
20083 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20  orrupt database 
20084 6d 61 79 0d 0a 20 20 20 20 2a 2a 20 63 61 75 73  may..    ** caus
20085 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74  e an assert() st
20086 61 74 65 6d 65 6e 74 20 74 6f 20 66 61 69 6c 2e  atement to fail.
20087 20 20 2a 2f 0d 0a 20 20 20 20 70 74 72 6d 61 70    */..    ptrmap
20088 43 68 65 63 6b 50 61 67 65 73 28 61 70 4e 65 77  CheckPages(apNew
20089 2c 20 6e 4e 65 77 29 3b 0d 0a 20 20 20 20 70 74  , nNew);..    pt
2008a 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 26  rmapCheckPages(&
2008b 70 50 61 72 65 6e 74 2c 20 31 29 3b 0d 0a 23 65  pParent, 1);..#e
2008c 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61  ndif..  }....  a
2008d 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
2008e 69 73 49 6e 69 74 20 29 3b 0d 0a 20 20 54 52 41  isInit );..  TRA
2008f 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69  CE(("BALANCE: fi
20090 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 6e  nished: old=%d n
20091 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e  ew=%d cells=%d\n
20092 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 4f  ",..          nO
20093 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29  ld, nNew, nCell)
20094 29 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a  );....  /*..  **
20095 20 43 6c 65 61 6e 75 70 20 62 65 66 6f 72 65 20   Cleanup before 
20096 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 20 20 2a 2f  returning...  */
20097 0d 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  ..balance_cleanu
20098 70 3a 0d 0a 20 20 73 71 6c 69 74 65 33 53 63 72  p:..  sqlite3Scr
20099 61 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29  atchFree(apCell)
2009a 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
2009b 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  nOld; i++){..   
2009c 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f   releasePage(apO
2009d 6c 64 5b 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20  ld[i]);..  }..  
2009e 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
2009f 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 72 65 6c 65   i++){..    rele
200a0 61 73 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d  asePage(apNew[i]
200a1 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  );..  }....  ret
200a2 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  urn rc;..}......
200a3 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
200a4 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
200a5 68 65 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67  hen the root pag
200a6 65 20 6f 66 20 61 20 62 2d 74 72 65 65 20 73 74  e of a b-tree st
200a7 72 75 63 74 75 72 65 20 69 73 0d 0a 2a 2a 20 6f  ructure is..** o
200a8 76 65 72 66 75 6c 6c 20 28 68 61 73 20 6f 6e 65  verfull (has one
200a9 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f   or more overflo
200aa 77 20 70 61 67 65 73 29 2e 0d 0a 2a 2a 0d 0a 2a  w pages)...**..*
200ab 2a 20 41 20 6e 65 77 20 63 68 69 6c 64 20 70 61  * A new child pa
200ac 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ge is allocated 
200ad 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  and the contents
200ae 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
200af 72 6f 6f 74 0d 0a 2a 2a 20 70 61 67 65 2c 20 69  root..** page, i
200b0 6e 63 6c 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f  ncluding overflo
200b1 77 20 63 65 6c 6c 73 2c 20 61 72 65 20 63 6f 70  w cells, are cop
200b2 69 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68 69  ied into the chi
200b3 6c 64 2e 20 54 68 65 20 72 6f 6f 74 0d 0a 2a 2a  ld. The root..**
200b4 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6f 76   page is then ov
200b5 65 72 77 72 69 74 74 65 6e 20 74 6f 20 6d 61 6b  erwritten to mak
200b6 65 20 69 74 20 61 6e 20 65 6d 70 74 79 20 70 61  e it an empty pa
200b7 67 65 20 77 69 74 68 20 74 68 65 20 72 69 67 68  ge with the righ
200b8 74 2d 63 68 69 6c 64 20 0d 0a 2a 2a 20 70 6f 69  t-child ..** poi
200b9 6e 74 65 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  nter pointing to
200ba 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 0d 0a   the new page...
200bb 2a 2a 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65  **..** Before re
200bc 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70 6f 69  turning, all poi
200bd 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
200be 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
200bf 6f 20 70 61 67 65 73 20 0d 0a 2a 2a 20 74 68 61  o pages ..** tha
200c0 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 2d  t the new child-
200c1 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e  page now contain
200c2 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 72  s pointers to ar
200c3 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 0d 0a  e updated. The..
200c4 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70  ** entry corresp
200c5 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65  onding to the ne
200c6 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f  w right-child po
200c7 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f  inter of the roo
200c8 74 0d 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c  t..** page is al
200c9 73 6f 20 75 70 64 61 74 65 64 2e 0d 0a 2a 2a 0d  so updated...**.
200ca 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
200cb 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73  l, *ppChild is s
200cc 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  et to contain a 
200cd 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
200ce 20 63 68 69 6c 64 20 0d 0a 2a 2a 20 70 61 67 65   child ..** page
200cf 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69   and SQLITE_OK i
200d0 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  s returned. In t
200d1 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
200d2 6c 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0d  ler is required.
200d3 0a 2a 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65  .** to call rele
200d4 61 73 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70  asePage() on *pp
200d5 43 68 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e  Child exactly on
200d6 63 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ce. If an error 
200d7 6f 63 63 75 72 73 2c 0d 0a 2a 2a 20 61 6e 20 65  occurs,..** an e
200d8 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
200d9 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 43 68 69  urned and *ppChi
200da 6c 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0d  ld is set to 0..
200db 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
200dc 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 4d  balance_deeper(M
200dd 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 2c 20 4d  emPage *pRoot, M
200de 65 6d 50 61 67 65 20 2a 2a 70 70 43 68 69 6c 64  emPage **ppChild
200df 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  ){..  int rc;   
200e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
200e1 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76       /* Return v
200e2 61 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f  alue from subpro
200e3 63 65 64 75 72 65 73 20 2a 2f 0d 0a 20 20 4d 65  cedures */..  Me
200e4 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 20 3d 20  mPage *pChild = 
200e5 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
200e6 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 65 77  Pointer to a new
200e7 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0d 0a   child page */..
200e8 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64    Pgno pgnoChild
200e9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
200ea 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
200eb 6f 66 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64  of the new child
200ec 20 70 61 67 65 20 2a 2f 0d 0a 20 20 42 74 53 68   page */..  BtSh
200ed 61 72 65 64 20 2a 70 42 74 20 3d 20 70 52 6f 6f  ared *pBt = pRoo
200ee 74 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 54 68  t->pBt;    /* Th
200ef 65 20 42 54 72 65 65 20 2a 2f 0d 0a 0d 0a 20 20  e BTree */....  
200f0 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 6e  assert( pRoot->n
200f1 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0d 0a 20  Overflow>0 );.. 
200f2 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
200f3 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
200f4 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 0d 0a 20 20  >mutex) );....  
200f5 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74  /* Make pRoot, t
200f6 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
200f7 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74  the b-tree, writ
200f8 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61  able. Allocate a
200f9 20 6e 65 77 20 0d 0a 20 20 2a 2a 20 70 61 67 65   new ..  ** page
200fa 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d   that will becom
200fb 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d  e the new right-
200fc 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20  child of pPage. 
200fd 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  Copy the content
200fe 73 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e  s..  ** of the n
200ff 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52  ode stored on pR
20100 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  oot into the new
20101 20 63 68 69 6c 64 20 70 61 67 65 2e 0d 0a 20 20   child page...  
20102 2a 2f 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  */..  rc = sqlit
20103 65 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f  e3PagerWrite(pRo
20104 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20  ot->pDbPage);.. 
20105 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
20106 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  OK ){..    rc = 
20107 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
20108 65 28 70 42 74 2c 26 70 43 68 69 6c 64 2c 26 70  e(pBt,&pChild,&p
20109 67 6e 6f 43 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e  gnoChild,pRoot->
2010a 70 67 6e 6f 2c 30 29 3b 0d 0a 20 20 20 20 63 6f  pgno,0);..    co
2010b 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52  pyNodeContent(pR
2010c 6f 6f 74 2c 20 70 43 68 69 6c 64 2c 20 26 72 63  oot, pChild, &rc
2010d 29 3b 0d 0a 20 20 20 20 69 66 28 20 49 53 41 55  );..    if( ISAU
2010e 54 4f 56 41 43 55 55 4d 20 29 7b 0d 0a 20 20 20  TOVACUUM ){..   
2010f 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
20110 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 50 54 52  , pgnoChild, PTR
20111 4d 41 50 5f 42 54 52 45 45 2c 20 70 52 6f 6f 74  MAP_BTREE, pRoot
20112 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0d 0a 20  ->pgno, &rc);.. 
20113 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28     }..  }..  if(
20114 20 72 63 20 29 7b 0d 0a 20 20 20 20 2a 70 70 43   rc ){..    *ppC
20115 68 69 6c 64 20 3d 20 30 3b 0d 0a 20 20 20 20 72  hild = 0;..    r
20116 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c  eleasePage(pChil
20117 64 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  d);..    return 
20118 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65  rc;..  }..  asse
20119 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
2011a 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 69  Iswriteable(pChi
2011b 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0d  ld->pDbPage) );.
2011c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2011d 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
2011e 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  le(pRoot->pDbPag
2011f 65 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  e) );..  assert(
20120 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d   pChild->nCell==
20121 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0d  pRoot->nCell );.
20122 0a 0d 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c  ...  TRACE(("BAL
20123 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20  ANCE: copy root 
20124 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70  %d into %d\n", p
20125 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69  Root->pgno, pChi
20126 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0d 0a 0d 0a 20  ld->pgno));.... 
20127 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 6f 76 65   /* Copy the ove
20128 72 66 6c 6f 77 20 63 65 6c 6c 73 20 66 72 6f 6d  rflow cells from
20129 20 70 52 6f 6f 74 20 74 6f 20 70 43 68 69 6c 64   pRoot to pChild
2012a 20 2a 2f 0d 0a 20 20 6d 65 6d 63 70 79 28 70 43   */..  memcpy(pC
2012b 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f  hild->aOvfl, pRo
2012c 6f 74 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74  ot->aOvfl, pRoot
2012d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65  ->nOverflow*size
2012e 6f 66 28 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c 5b  of(pRoot->aOvfl[
2012f 30 5d 29 29 3b 0d 0a 20 20 70 43 68 69 6c 64 2d  0]));..  pChild-
20130 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 52 6f  >nOverflow = pRo
20131 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0d 0a  ot->nOverflow;..
20132 0d 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20  ..  /* Zero the 
20133 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 52 6f 6f  contents of pRoo
20134 74 2e 20 54 68 65 6e 20 69 6e 73 74 61 6c 6c 20  t. Then install 
20135 70 43 68 69 6c 64 20 61 73 20 74 68 65 20 72 69  pChild as the ri
20136 67 68 74 2d 63 68 69 6c 64 2e 20 2a 2f 0d 0a 20  ght-child. */.. 
20137 20 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 2c   zeroPage(pRoot,
20138 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30   pChild->aData[0
20139 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b 0d  ] & ~PTF_LEAF);.
2013a 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 52 6f  .  put4byte(&pRo
2013b 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d  ot->aData[pRoot-
2013c 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70  >hdrOffset+8], p
2013d 67 6e 6f 43 68 69 6c 64 29 3b 0d 0a 0d 0a 20 20  gnoChild);....  
2013e 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 68 69 6c  *ppChild = pChil
2013f 64 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d;..  return SQL
20140 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
20141 0d 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68  ..** The page th
20142 61 74 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c  at pCur currentl
20143 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20  y points to has 
20144 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69  just been modifi
20145 65 64 20 69 6e 0d 0a 2a 2a 20 73 6f 6d 65 20 77  ed in..** some w
20146 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ay. This functio
20147 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 69 66  n figures out if
20148 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 74 69   this modificati
20149 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0d 0a 2a 2a  on means the..**
2014a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 62   tree needs to b
2014b 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20  e balanced, and 
2014c 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 20  if so calls the 
2014d 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c 61  appropriate bala
2014e 6e 63 69 6e 67 20 0d 0a 2a 2a 20 72 6f 75 74 69  ncing ..** routi
2014f 6e 65 2e 20 42 61 6c 61 6e 63 69 6e 67 20 72 6f  ne. Balancing ro
20150 75 74 69 6e 65 73 20 61 72 65 3a 0d 0a 2a 2a 0d  utines are:..**.
20151 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 71 75  .**   balance_qu
20152 69 63 6b 28 29 0d 0a 2a 2a 20 20 20 62 61 6c 61  ick()..**   bala
20153 6e 63 65 5f 64 65 65 70 65 72 28 29 0d 0a 2a 2a  nce_deeper()..**
20154 20 20 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f     balance_nonro
20155 6f 74 28 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ot()..*/..static
20156 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43   int balance(BtC
20157 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d 0a 20  ursor *pCur){.. 
20158 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
20159 5f 4f 4b 3b 0d 0a 20 20 63 6f 6e 73 74 20 69 6e  _OK;..  const in
2015a 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e 70  t nMin = pCur->p
2015b 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2a  Bt->usableSize *
2015c 20 32 20 2f 20 33 3b 0d 0a 20 20 75 38 20 61 42   2 / 3;..  u8 aB
2015d 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
2015e 5b 31 33 5d 3b 0d 0a 20 20 75 38 20 2a 70 46 72  [13];..  u8 *pFr
2015f 65 65 20 3d 20 30 3b 0d 0a 0d 0a 20 20 54 45 53  ee = 0;....  TES
20160 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e  TONLY( int balan
20161 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20  ce_quick_called 
20162 3d 20 30 20 29 3b 0d 0a 20 20 54 45 53 54 4f 4e  = 0 );..  TESTON
20163 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  LY( int balance_
20164 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20  deeper_called = 
20165 30 20 29 3b 0d 0a 0d 0a 20 20 64 6f 20 7b 0d 0a  0 );....  do {..
20166 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20      int iPage = 
20167 70 43 75 72 2d 3e 69 50 61 67 65 3b 0d 0a 20 20  pCur->iPage;..  
20168 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
20169 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
2016a 69 50 61 67 65 5d 3b 0d 0a 0d 0a 20 20 20 20 69  iPage];....    i
2016b 66 28 20 69 50 61 67 65 3d 3d 30 20 29 7b 0d 0a  f( iPage==0 ){..
2016c 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d        if( pPage-
2016d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0d 0a 20  >nOverflow ){.. 
2016e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
2016f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  ot page of the b
20170 2d 74 72 65 65 20 69 73 20 6f 76 65 72 66 75 6c  -tree is overful
20171 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  l. In this case 
20172 63 61 6c 6c 20 74 68 65 0d 0a 20 20 20 20 20 20  call the..      
20173 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 64 65 65    ** balance_dee
20174 70 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  per() function t
20175 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 63  o create a new c
20176 68 69 6c 64 20 66 6f 72 20 74 68 65 20 72 6f 6f  hild for the roo
20177 74 2d 70 61 67 65 0d 0a 20 20 20 20 20 20 20 20  t-page..        
20178 2a 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 65 20  ** and copy the 
20179 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
2017a 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67   of the root-pag
2017b 65 20 74 6f 20 69 74 2e 20 54 68 65 0d 0a 20 20  e to it. The..  
2017c 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74        ** next it
2017d 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
2017e 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61  o-loop will bala
2017f 6e 63 65 20 74 68 65 20 63 68 69 6c 64 20 70 61  nce the child pa
20180 67 65 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 20  ge...        */ 
20181 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
20182 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65  ( (balance_deepe
20183 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29  r_called++)==0 )
20184 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ;..        rc = 
20185 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70  balance_deeper(p
20186 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50  Page, &pCur->apP
20187 61 67 65 5b 31 5d 29 3b 0d 0a 20 20 20 20 20 20  age[1]);..      
20188 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
20189 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
2018a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
2018b 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 43  1;..          pC
2018c 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30  ur->aiIdx[0] = 0
2018d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 43 75  ;..          pCu
2018e 72 2d 3e 61 69 49 64 78 5b 31 5d 20 3d 20 30 3b  r->aiIdx[1] = 0;
2018f 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ..          asse
20190 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
20191 5b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  [1]->nOverflow )
20192 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
20193 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
20194 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
20195 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69    }..    }else i
20196 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  f( pPage->nOverf
20197 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d  low==0 && pPage-
20198 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e 20 29 7b 0d  >nFree<=nMin ){.
20199 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
2019a 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2019b 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 74   MemPage * const
2019c 20 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d   pParent = pCur-
2019d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 2d 31 5d  >apPage[iPage-1]
2019e 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e  ;..      int con
2019f 73 74 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e  st iIdx = pCur->
201a0 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0d  aiIdx[iPage-1];.
201a1 0a 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ...      rc = sq
201a2 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
201a3 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
201a4 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
201a5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
201a6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
201a7 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45  MIT_QUICKBALANCE
201a8 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ..        if( pP
201a9 61 67 65 2d 3e 68 61 73 44 61 74 61 0d 0a 20 20  age->hasData..  
201aa 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 2d         && pPage-
201ab 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 0d 0a 20  >nOverflow==1.. 
201ac 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65          && pPage
201ad 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d  ->aOvfl[0].idx==
201ae 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 0d 0a 20 20  pPage->nCell..  
201af 20 20 20 20 20 20 20 26 26 20 70 50 61 72 65 6e         && pParen
201b0 74 2d 3e 70 67 6e 6f 21 3d 31 0d 0a 20 20 20 20  t->pgno!=1..    
201b1 20 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 2d       && pParent-
201b2 3e 6e 43 65 6c 6c 3d 3d 69 49 64 78 0d 0a 20 20  >nCell==iIdx..  
201b3 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20        ){..      
201b4 20 20 20 20 2f 2a 20 43 61 6c 6c 20 62 61 6c 61      /* Call bala
201b5 6e 63 65 5f 71 75 69 63 6b 28 29 20 74 6f 20 63  nce_quick() to c
201b6 72 65 61 74 65 20 61 20 6e 65 77 20 73 69 62 6c  reate a new sibl
201b7 69 6e 67 20 6f 66 20 70 50 61 67 65 20 6f 6e 20  ing of pPage on 
201b8 77 68 69 63 68 0d 0a 20 20 20 20 20 20 20 20 20  which..         
201b9 20 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65   ** to store the
201ba 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20   overflow cell. 
201bb 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20  balance_quick() 
201bc 69 6e 73 65 72 74 73 20 61 20 6e 65 77 20 63 65  inserts a new ce
201bd 6c 6c 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ll..          **
201be 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2c 20 77   into pParent, w
201bf 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 20 70  hich may cause p
201c0 50 61 72 65 6e 74 20 6f 76 65 72 66 6c 6f 77 2e  Parent overflow.
201c1 20 49 66 20 74 68 69 73 0d 0a 20 20 20 20 20 20   If this..      
201c2 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20      ** happens, 
201c3 74 68 65 20 6e 65 78 74 20 69 6e 74 65 72 61 74  the next interat
201c4 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f  ion of the do-lo
201c5 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20  op will balance 
201c6 70 50 61 72 65 6e 74 20 0d 0a 20 20 20 20 20 20  pParent ..      
201c7 20 20 20 20 2a 2a 20 75 73 65 20 65 69 74 68 65      ** use eithe
201c8 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  r balance_nonroo
201c9 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f 64  t() or balance_d
201ca 65 65 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 74  eeper(). Until t
201cb 68 69 73 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  his..          *
201cc 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 6f  * happens, the o
201cd 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20  verflow cell is 
201ce 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 42  stored in the aB
201cf 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
201d0 5b 5d 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  []..          **
201d1 20 62 75 66 66 65 72 2e 20 0d 0a 20 20 20 20 20   buffer. ..     
201d2 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20       **..       
201d3 20 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73     ** The purpos
201d4 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
201d5 6e 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74  ng assert() is t
201d6 6f 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c  o check that onl
201d7 79 20 61 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  y a..          *
201d8 2a 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f  * single call to
201d9 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29   balance_quick()
201da 20 69 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63   is made for eac
201db 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 0d 0a  h call to this..
201dc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e            ** fun
201dd 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 77  ction. If this w
201de 65 72 65 20 6e 6f 74 20 76 65 72 69 66 69 65 64  ere not verified
201df 2c 20 61 20 73 75 62 74 6c 65 20 62 75 67 20 69  , a subtle bug i
201e0 6e 76 6f 6c 76 69 6e 67 20 72 65 75 73 65 0d 0a  nvolving reuse..
201e1 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20            ** of 
201e2 74 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63  the aBalanceQuic
201e3 6b 53 70 61 63 65 5b 5d 20 6d 69 67 68 74 20 73  kSpace[] might s
201e4 6e 65 61 6b 20 69 6e 2e 0d 0a 20 20 20 20 20 20  neak in...      
201e5 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
201e6 20 20 61 73 73 65 72 74 28 20 28 62 61 6c 61 6e    assert( (balan
201e7 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 2b  ce_quick_called+
201e8 2b 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  +)==0 );..      
201e9 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
201ea 5f 71 75 69 63 6b 28 70 50 61 72 65 6e 74 2c 20  _quick(pParent, 
201eb 70 50 61 67 65 2c 20 61 42 61 6c 61 6e 63 65 51  pPage, aBalanceQ
201ec 75 69 63 6b 53 70 61 63 65 29 3b 0d 0a 20 20 20  uickSpace);..   
201ed 20 20 20 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64       }else..#end
201ee 69 66 0d 0a 20 20 20 20 20 20 20 20 7b 0d 0a 20  if..        {.. 
201ef 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74           /* In t
201f0 68 69 73 20 63 61 73 65 2c 20 63 61 6c 6c 20 62  his case, call b
201f1 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
201f2 20 74 6f 20 72 65 64 69 73 74 72 69 62 75 74 65   to redistribute
201f3 20 63 65 6c 6c 73 0d 0a 20 20 20 20 20 20 20 20   cells..        
201f4 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 50 61    ** between pPa
201f5 67 65 20 61 6e 64 20 75 70 20 74 6f 20 32 20 6f  ge and up to 2 o
201f6 66 20 69 74 73 20 73 69 62 6c 69 6e 67 20 70 61  f its sibling pa
201f7 67 65 73 2e 20 54 68 69 73 20 69 6e 76 6f 6c 76  ges. This involv
201f8 65 73 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  es..          **
201f9 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63   modifying the c
201fa 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 61 72 65  ontents of pPare
201fb 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61  nt, which may ca
201fc 75 73 65 20 70 50 61 72 65 6e 74 20 74 6f 0d 0a  use pParent to..
201fd 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63            ** bec
201fe 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20  ome overfull or 
201ff 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e  underfull. The n
20200 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
20201 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0d 0a 20 20   the do-loop..  
20202 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20          ** will 
20203 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65  balance the pare
20204 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72 65  nt page to corre
20205 63 74 20 74 68 69 73 2e 0d 0a 20 20 20 20 20 20  ct this...      
20206 20 20 20 20 2a 2a 20 0d 0a 20 20 20 20 20 20 20      ** ..       
20207 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72     ** If the par
20208 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73  ent page becomes
20209 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f   overfull, the o
2020a 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20  verflow cell or 
2020b 63 65 6c 6c 73 0d 0a 20 20 20 20 20 20 20 20 20  cells..         
2020c 20 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69   ** are stored i
2020d 6e 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  n the pSpace buf
2020e 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d  fer allocated im
2020f 6d 65 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e  mediately below.
20210 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   ..          ** 
20211 41 20 73 75 62 73 65 71 75 65 6e 74 20 69 74 65  A subsequent ite
20212 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f  ration of the do
20213 2d 6c 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20  -loop will deal 
20214 77 69 74 68 20 74 68 69 73 20 62 79 0d 0a 20 20  with this by..  
20215 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69          ** calli
20216 6e 67 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  ng balance_nonro
20217 6f 74 28 29 20 28 62 61 6c 61 6e 63 65 5f 64 65  ot() (balance_de
20218 65 70 65 72 28 29 20 6d 61 79 20 62 65 20 63 61  eper() may be ca
20219 6c 6c 65 64 20 66 69 72 73 74 2c 0d 0a 20 20 20  lled first,..   
2021a 20 20 20 20 20 20 20 2a 2a 20 62 75 74 20 69 74         ** but it
2021b 20 64 6f 65 73 6e 27 74 20 64 65 61 6c 20 77 69   doesn't deal wi
2021c 74 68 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  th overflow cell
2021d 73 20 2d 20 6a 75 73 74 20 6d 6f 76 65 73 20 74  s - just moves t
2021e 68 65 6d 20 74 6f 20 61 0d 0a 20 20 20 20 20 20  hem to a..      
2021f 20 20 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74      ** different
20220 20 70 61 67 65 29 2e 20 4f 6e 63 65 20 74 68 69   page). Once thi
20221 73 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  s subsequent cal
20222 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  l to balance_non
20223 72 6f 6f 74 28 29 20 0d 0a 20 20 20 20 20 20 20  root() ..       
20224 20 20 20 2a 2a 20 68 61 73 20 63 6f 6d 70 6c 65     ** has comple
20225 74 65 64 2c 20 69 74 20 69 73 20 73 61 66 65 20  ted, it is safe 
20226 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 70  to release the p
20227 53 70 61 63 65 20 62 75 66 66 65 72 20 75 73 65  Space buffer use
20228 64 20 62 79 0d 0a 20 20 20 20 20 20 20 20 20 20  d by..          
20229 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  ** the previous 
2022a 63 61 6c 6c 2c 20 61 73 20 74 68 65 20 6f 76 65  call, as the ove
2022b 72 66 6c 6f 77 20 63 65 6c 6c 20 64 61 74 61 20  rflow cell data 
2022c 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 0d  will have been .
2022d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
2022e 70 69 65 64 20 65 69 74 68 65 72 20 69 6e 74 6f  pied either into
2022f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 64   the body of a d
20230 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 72 20  atabase page or 
20231 69 6e 74 6f 20 74 68 65 20 6e 65 77 0d 0a 20 20  into the new..  
20232 20 20 20 20 20 20 20 20 2a 2a 20 70 53 70 61 63          ** pSpac
20233 65 20 62 75 66 66 65 72 20 70 61 73 73 65 64 20  e buffer passed 
20234 74 6f 20 74 68 65 20 6c 61 74 74 65 72 20 63 61  to the latter ca
20235 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f  ll to balance_no
20236 6e 72 6f 6f 74 28 29 2e 0d 0a 20 20 20 20 20 20  nroot()...      
20237 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
20238 20 20 75 38 20 2a 70 53 70 61 63 65 20 3d 20 73    u8 *pSpace = s
20239 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
2023a 28 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 67 65  (pCur->pBt->page
2023b 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Size);..        
2023c 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e    rc = balance_n
2023d 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c 20  onroot(pParent, 
2023e 69 49 64 78 2c 20 70 53 70 61 63 65 2c 20 69 50  iIdx, pSpace, iP
2023f 61 67 65 3d 3d 31 29 3b 0d 0a 20 20 20 20 20 20  age==1);..      
20240 20 20 20 20 69 66 28 20 70 46 72 65 65 20 29 7b      if( pFree ){
20241 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ..            /*
20242 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f 74   If pFree is not
20243 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73   NULL, it points
20244 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62   to the pSpace b
20245 75 66 66 65 72 20 75 73 65 64 20 0d 0a 20 20 20  uffer used ..   
20246 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61           ** by a
20247 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
20248 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  o balance_nonroo
20249 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  t(). Its content
2024a 73 20 61 72 65 0d 0a 20 20 20 20 20 20 20 20 20  s are..         
2024b 20 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64     ** now stored
2024c 20 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20   either on real 
2024d 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f  database pages o
2024e 72 20 77 69 74 68 69 6e 20 74 68 65 20 0d 0a 20  r within the .. 
2024f 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65             ** ne
20250 77 20 70 53 70 61 63 65 20 62 75 66 66 65 72 2c  w pSpace buffer,
20251 20 73 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61   so it may be sa
20252 66 65 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e  fely freed here.
20253 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20   */..           
20254 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65   sqlite3PageFree
20255 28 70 46 72 65 65 29 3b 0d 0a 20 20 20 20 20 20  (pFree);..      
20256 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20      }....       
20257 20 20 20 2f 2a 20 54 68 65 20 70 53 70 61 63 65     /* The pSpace
20258 20 62 75 66 66 65 72 20 77 69 6c 6c 20 62 65 20   buffer will be 
20259 66 72 65 65 64 20 61 66 74 65 72 20 74 68 65 20  freed after the 
2025a 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0d 0a 20 20  next call to..  
2025b 20 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e          ** balan
2025c 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2c 20 6f 72  ce_nonroot(), or
2025d 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 69   just before thi
2025e 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
2025f 6e 73 2c 20 77 68 69 63 68 65 76 65 72 0d 0a 20  ns, whichever.. 
20260 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 65           ** come
20261 73 20 66 69 72 73 74 2e 20 2a 2f 0d 0a 20 20 20  s first. */..   
20262 20 20 20 20 20 20 20 70 46 72 65 65 20 3d 20 70         pFree = p
20263 53 70 61 63 65 3b 0d 0a 20 20 20 20 20 20 20 20  Space;..        
20264 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  }..      }....  
20265 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72      pPage->nOver
20266 66 6c 6f 77 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20  flow = 0;....   
20267 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69     /* The next i
20268 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
20269 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 73  do-loop balances
2026a 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
2026b 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 72 65 6c 65  . */..      rele
2026c 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0d  asePage(pPage);.
2026d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61  .      pCur->iPa
2026e 67 65 2d 2d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ge--;..    }..  
2026f 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49  }while( rc==SQLI
20270 54 45 5f 4f 4b 20 29 3b 0d 0a 0d 0a 20 20 69 66  TE_OK );....  if
20271 28 20 70 46 72 65 65 20 29 7b 0d 0a 20 20 20 20  ( pFree ){..    
20272 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
20273 70 46 72 65 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20  pFree);..  }..  
20274 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
20275 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 73 65 72 74  .../*..** Insert
20276 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
20277 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 54  to the BTree.  T
20278 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20  he key is given 
20279 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0d 0a  by (pKey,nKey)..
2027a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20  ** and the data 
2027b 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61  is given by (pDa
2027c 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20  ta,nData).  The 
2027d 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f  cursor is used o
2027e 6e 6c 79 20 74 6f 0d 0a 2a 2a 20 64 65 66 69 6e  nly to..** defin
2027f 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65  e what table the
20280 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62   record should b
20281 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e  e inserted into.
20282 20 20 54 68 65 20 63 75 72 73 6f 72 0d 0a 2a 2a    The cursor..**
20283 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
20284 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f  g at a random lo
20285 63 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  cation...**..** 
20286 46 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61  For an INTKEY ta
20287 62 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b  ble, only the nK
20288 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ey value of the 
20289 6b 65 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b  key is used.  pK
2028a 65 79 20 69 73 0d 0a 2a 2a 20 69 67 6e 6f 72 65  ey is..** ignore
2028b 64 2e 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41  d.  For a ZERODA
2028c 54 41 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44  TA table, the pD
2028d 61 74 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72  ata and nData ar
2028e 65 20 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0d  e both ignored..
2028f 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 73  .**..** If the s
20290 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65  eekResult parame
20291 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ter is non-zero,
20292 20 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66   then a successf
20293 75 6c 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 4d  ul call to..** M
20294 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20  ovetoUnpacked() 
20295 74 6f 20 73 65 65 6b 20 63 75 72 73 6f 72 20 70  to seek cursor p
20296 43 75 72 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b  Cur to (pKey, nK
20297 65 79 29 20 68 61 73 20 61 6c 72 65 61 64 79 0d  ey) has already.
20298 0a 2a 2a 20 62 65 65 6e 20 70 65 72 66 6f 72 6d  .** been perform
20299 65 64 2e 20 73 65 65 6b 52 65 73 75 6c 74 20 69  ed. seekResult i
2029a 73 20 74 68 65 20 73 65 61 72 63 68 20 72 65 73  s the search res
2029b 75 6c 74 20 72 65 74 75 72 6e 65 64 20 28 61 20  ult returned (a 
2029c 6e 65 67 61 74 69 76 65 0d 0a 2a 2a 20 6e 75 6d  negative..** num
2029d 62 65 72 20 69 66 20 70 43 75 72 20 70 6f 69 6e  ber if pCur poin
2029e 74 73 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74  ts at an entry t
2029f 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  hat is smaller t
202a0 68 61 6e 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29  han (pKey, nKey)
202a1 2c 20 6f 72 0d 0a 2a 2a 20 61 20 70 6f 73 69 74  , or..** a posit
202a2 69 76 65 20 76 61 6c 75 65 20 69 66 20 70 43 75  ive value if pCu
202a3 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65  r points at an e
202a4 74 72 79 20 74 68 61 74 20 69 73 20 6c 61 72 67  try that is larg
202a5 65 72 20 74 68 61 6e 20 0d 0a 2a 2a 20 28 70 4b  er than ..** (pK
202a6 65 79 2c 20 6e 4b 65 79 29 29 2e 20 0d 0a 2a 2a  ey, nKey)). ..**
202a7 0d 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b  ..** If the seek
202a8 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72  Result parameter
202a9 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
202aa 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 67 75  en the caller gu
202ab 61 72 61 6e 74 65 65 73 20 74 68 61 74 0d 0a 2a  arantees that..*
202ac 2a 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73  * cursor pCur is
202ad 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
202ae 20 65 78 69 73 74 69 6e 67 20 63 6f 70 79 20 6f   existing copy o
202af 66 20 61 20 72 6f 77 20 74 68 61 74 20 69 73 20  f a row that is 
202b0 74 6f 20 62 65 0d 0a 2a 2a 20 6f 76 65 72 77 72  to be..** overwr
202b1 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 20 73  itten.  If the s
202b2 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65  eekResult parame
202b3 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 63  ter is 0, then c
202b4 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 0d 0a  ursor pCur may..
202b5 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20  ** point to any 
202b6 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 65  entry or to no e
202b7 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 20  ntry at all and 
202b8 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  so this function
202b9 20 68 61 73 20 74 6f 20 73 65 65 6b 0d 0a 2a 2a   has to seek..**
202ba 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f   the cursor befo
202bb 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63  re the new key c
202bc 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0d  an be inserted..
202bd 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
202be 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
202bf 74 72 65 65 49 6e 73 65 72 74 28 0d 0a 20 20 42  treeInsert(..  B
202c0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
202c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
202c2 20 49 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74   Insert data int
202c3 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 74  o the table of t
202c4 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 20  his cursor */.. 
202c5 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
202c6 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20  y, i64 nKey,    
202c7 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66 20 74 68  /* The key of th
202c8 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0d  e new record */.
202c9 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
202ca 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
202cb 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 6f 66    /* The data of
202cc 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
202cd 2a 2f 0d 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c  */..  int nZero,
202ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
202cf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
202d0 66 20 65 78 74 72 61 20 30 20 62 79 74 65 73 20  f extra 0 bytes 
202d1 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64 61 74  to append to dat
202d2 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 70 70 65  a */..  int appe
202d3 6e 64 42 69 61 73 2c 20 20 20 20 20 20 20 20 20  ndBias,         
202d4 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
202d5 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79  f this is likely
202d6 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0d 0a 20   an append */.. 
202d7 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 20   int seekResult 
202d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
202d9 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69  /* Result of pri
202da 6f 72 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  or MovetoUnpacke
202db 64 28 29 20 63 61 6c 6c 20 2a 2f 0d 0a 29 7b 0d  d() call */..){.
202dc 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e  .  int rc;..  in
202dd 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 65 73 75  t loc = seekResu
202de 6c 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  lt;          /* 
202df 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 73 69 72  -1: before desir
202e0 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 2b 31 3a  ed location  +1:
202e1 20 61 66 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74   after */..  int
202e2 20 73 7a 4e 65 77 20 3d 20 30 3b 0d 0a 20 20 69   szNew = 0;..  i
202e3 6e 74 20 69 64 78 3b 0d 0a 20 20 4d 65 6d 50 61  nt idx;..  MemPa
202e4 67 65 20 2a 70 50 61 67 65 3b 0d 0a 20 20 42 74  ge *pPage;..  Bt
202e5 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70  ree *p = pCur->p
202e6 42 74 72 65 65 3b 0d 0a 20 20 42 74 53 68 61 72  Btree;..  BtShar
202e7 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
202e8 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
202e9 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0d 0a 20 20  ar *oldCell;..  
202ea 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e  unsigned char *n
202eb 65 77 43 65 6c 6c 20 3d 20 30 3b 0d 0a 0d 0a 20  ewCell = 0;.... 
202ec 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
202ed 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e==CURSOR_FAULT 
202ee 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
202ef 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d  pCur->skipNext!=
202f0 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20  SQLITE_OK );..  
202f1 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73    return pCur->s
202f2 6b 69 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 0d  kipNext;..  }...
202f3 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
202f4 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
202f5 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
202f6 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20  pCur->wrFlag && 
202f7 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
202f8 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 0d  on==TRANS_WRITE.
202f9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
202fa 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73  & (pBt->btsFlags
202fb 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59   & BTS_READ_ONLY
202fc 29 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  )==0 );..  asser
202fd 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68  t( hasSharedCach
202fe 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43  eTableLock(p, pC
202ff 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43  ur->pgnoRoot, pC
20300 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c  ur->pKeyInfo!=0,
20301 20 32 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41   2) );....  /* A
20302 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63  ssert that the c
20303 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e 20 63  aller has been c
20304 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 74 68  onsistent. If th
20305 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  is cursor was op
20306 65 6e 65 64 0d 0a 20 20 2a 2a 20 65 78 70 65 63  ened..  ** expec
20307 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d  ting an index b-
20308 74 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 63  tree, then the c
20309 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20  aller should be 
2030a 69 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0d 0a  inserting blob..
2030b 20 20 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e    ** keys with n
2030c 6f 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  o associated dat
2030d 61 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  a. If the cursor
2030e 20 77 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65   was opened expe
2030f 63 74 69 6e 67 20 61 6e 0d 0a 20 20 2a 2a 20 69  cting an..  ** i
20310 6e 74 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65  ntkey table, the
20311 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62   caller should b
20312 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 65  e inserting inte
20313 67 65 72 20 6b 65 79 73 20 77 69 74 68 20 61 0d  ger keys with a.
20314 0a 20 20 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 73  .  ** blob of as
20315 73 6f 63 69 61 74 65 64 20 64 61 74 61 2e 20 20  sociated data.  
20316 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70  */..  assert( (p
20317 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e  Key==0)==(pCur->
20318 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0d  pKeyInfo==0) );.
20319 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ...  /* If this 
2031a 69 73 20 61 6e 20 69 6e 73 65 72 74 20 69 6e 74  is an insert int
2031b 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65  o a table b-tree
2031c 2c 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79  , invalidate any
2031d 20 69 6e 63 72 62 6c 6f 62 20 0d 0a 20 20 2a 2a   incrblob ..  **
2031e 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
2031f 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 72   the row being r
20320 65 70 6c 61 63 65 64 20 28 61 73 73 75 6d 69 6e  eplaced (assumin
20321 67 20 74 68 69 73 20 69 73 20 61 20 72 65 70 6c  g this is a repl
20322 61 63 65 0d 0a 20 20 2a 2a 20 6f 70 65 72 61 74  ace..  ** operat
20323 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 73 20 6e  ion - if it is n
20324 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ot, the followin
20325 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e 20 20  g is a no-op).  
20326 2a 2f 0d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e  */..  if( pCur->
20327 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0d 0a  pKeyInfo==0 ){..
20328 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e      invalidateIn
20329 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c  crblobCursors(p,
2032a 20 6e 4b 65 79 2c 20 30 29 3b 0d 0a 20 20 7d 0d   nKey, 0);..  }.
2032b 0a 0d 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65  ...  /* Save the
2032c 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e   positions of an
2032d 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20  y other cursors 
2032e 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62  open on this tab
2032f 6c 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  le...  **..  ** 
20330 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74  In some cases, t
20331 68 65 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65  he call to btree
20332 4d 6f 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69  Moveto() below i
20333 73 20 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0d 0a  s a no-op. For..
20334 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68    ** example, wh
20335 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 64 61 74  en inserting dat
20336 61 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 77  a into a table w
20337 69 74 68 20 61 75 74 6f 2d 67 65 6e 65 72 61 74  ith auto-generat
20338 65 64 20 69 6e 74 65 67 65 72 0d 0a 20 20 2a 2a  ed integer..  **
20339 20 6b 65 79 73 2c 20 74 68 65 20 56 44 42 45 20   keys, the VDBE 
2033a 6c 61 79 65 72 20 69 6e 76 6f 6b 65 73 20 73 71  layer invokes sq
2033b 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29  lite3BtreeLast()
2033c 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74   to figure out t
2033d 68 65 20 0d 0a 20 20 2a 2a 20 69 6e 74 65 67 65  he ..  ** intege
2033e 72 20 6b 65 79 20 74 6f 20 75 73 65 2e 20 49 74  r key to use. It
2033f 20 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 69 73   then calls this
20340 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 61 63 74   function to act
20341 75 61 6c 6c 79 20 69 6e 73 65 72 74 20 74 68 65  ually insert the
20342 20 0d 0a 20 20 2a 2a 20 64 61 74 61 20 69 6e 74   ..  ** data int
20343 6f 20 74 68 65 20 69 6e 74 6b 65 79 20 42 2d 54  o the intkey B-T
20344 72 65 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ree. In this cas
20345 65 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e btreeMoveto() 
20346 72 65 63 6f 67 6e 69 7a 65 73 0d 0a 20 20 2a 2a  recognizes..  **
20347 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
20348 20 69 73 20 61 6c 72 65 61 64 79 20 77 68 65 72   is already wher
20349 65 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  e it needs to be
2034a 20 61 6e 64 20 72 65 74 75 72 6e 73 20 77 69 74   and returns wit
2034b 68 6f 75 74 0d 0a 20 20 2a 2a 20 64 6f 69 6e 67  hout..  ** doing
2034c 20 61 6e 79 20 77 6f 72 6b 2e 20 54 6f 20 61 76   any work. To av
2034d 6f 69 64 20 74 68 77 61 72 74 69 6e 67 20 74 68  oid thwarting th
2034e 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ese optimization
2034f 73 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  s, it is importa
20350 6e 74 0d 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20  nt..  ** not to 
20351 63 6c 65 61 72 20 74 68 65 20 63 75 72 73 6f 72  clear the cursor
20352 20 68 65 72 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   here...  */..  
20353 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
20354 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  ors(pBt, pCur->p
20355 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0d  gnoRoot, pCur);.
20356 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
20357 72 6e 20 72 63 3b 0d 0a 20 20 69 66 28 20 21 6c  rn rc;..  if( !l
20358 6f 63 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  oc ){..    rc = 
20359 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  btreeMoveto(pCur
2035a 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70  , pKey, nKey, ap
2035b 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b  pendBias, &loc);
2035c 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ..    if( rc ) r
2035d 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a  eturn rc;..  }..
2035e 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
2035f 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
20360 41 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65  ALID || (pCur->e
20361 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
20362 56 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 3b  VALID && loc) );
20363 0d 0a 0d 0a 20 20 70 50 61 67 65 20 3d 20 70 43  ....  pPage = pC
20364 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
20365 3e 69 50 61 67 65 5d 3b 0d 0a 20 20 61 73 73 65  >iPage];..  asse
20366 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  rt( pPage->intKe
20367 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0d  y || nKey>=0 );.
20368 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
20369 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65  ->leaf || !pPage
2036a 2d 3e 69 6e 74 4b 65 79 20 29 3b 0d 0a 0d 0a 20  ->intKey );.... 
2036b 20 54 52 41 43 45 28 28 22 49 4e 53 45 52 54 3a   TRACE(("INSERT:
2036c 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79 3d 25   table=%d nkey=%
2036d 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70 61 67  lld ndata=%d pag
2036e 65 3d 25 64 20 25 73 5c 6e 22 2c 0d 0a 20 20 20  e=%d %s\n",..   
2036f 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e         pCur->pgn
20370 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 61  oRoot, nKey, nDa
20371 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  ta, pPage->pgno,
20372 0d 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d  ..          loc=
20373 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22  =0 ? "overwrite"
20374 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29   : "new entry"))
20375 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
20376 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0d 0a 20  ge->isInit );.. 
20377 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61   allocateTempSpa
20378 63 65 28 70 42 74 29 3b 0d 0a 20 20 6e 65 77 43  ce(pBt);..  newC
20379 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53  ell = pBt->pTmpS
2037a 70 61 63 65 3b 0d 0a 20 20 69 66 28 20 6e 65 77  pace;..  if( new
2037b 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  Cell==0 ) return
2037c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
2037d 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c    rc = fillInCel
2037e 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c  l(pPage, newCell
2037f 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44  , pKey, nKey, pD
20380 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72  ata, nData, nZer
20381 6f 2c 20 26 73 7a 4e 65 77 29 3b 0d 0a 20 20 69  o, &szNew);..  i
20382 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
20383 5f 69 6e 73 65 72 74 3b 0d 0a 20 20 61 73 73 65  _insert;..  asse
20384 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53  rt( szNew==cellS
20385 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65  izePtr(pPage, ne
20386 77 43 65 6c 6c 29 20 29 3b 0d 0a 20 20 61 73 73  wCell) );..  ass
20387 65 72 74 28 20 73 7a 4e 65 77 20 3c 3d 20 4d 58  ert( szNew <= MX
20388 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
20389 29 3b 0d 0a 20 20 69 64 78 20 3d 20 70 43 75 72  );..  idx = pCur
2038a 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
2038b 61 67 65 5d 3b 0d 0a 20 20 69 66 28 20 6c 6f 63  age];..  if( loc
2038c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 75 31 36 20  ==0 ){..    u16 
2038d 73 7a 4f 6c 64 3b 0d 0a 20 20 20 20 61 73 73 65  szOld;..    asse
2038e 72 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e  rt( idx<pPage->n
2038f 43 65 6c 6c 20 29 3b 0d 0a 20 20 20 20 72 63 20  Cell );..    rc 
20390 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
20391 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
20392 67 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  ge);..    if( rc
20393 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20   ){..      goto 
20394 65 6e 64 5f 69 6e 73 65 72 74 3b 0d 0a 20 20 20  end_insert;..   
20395 20 7d 0d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c 20   }..    oldCell 
20396 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
20397 2c 20 69 64 78 29 3b 0d 0a 20 20 20 20 69 66 28  , idx);..    if(
20398 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
20399 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e  ..      memcpy(n
2039a 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c  ewCell, oldCell,
2039b 20 34 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20   4);..    }..   
2039c 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a   szOld = cellSiz
2039d 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43  ePtr(pPage, oldC
2039e 65 6c 6c 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  ell);..    rc = 
2039f 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
203a0 20 6f 6c 64 43 65 6c 6c 29 3b 0d 0a 20 20 20 20   oldCell);..    
203a1 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20  dropCell(pPage, 
203a2 69 64 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29  idx, szOld, &rc)
203a3 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  ;..    if( rc ) 
203a4 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b  goto end_insert;
203a5 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  ..  }else if( lo
203a6 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43  c<0 && pPage->nC
203a7 65 6c 6c 3e 30 20 29 7b 0d 0a 20 20 20 20 61 73  ell>0 ){..    as
203a8 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61  sert( pPage->lea
203a9 66 20 29 3b 0d 0a 20 20 20 20 69 64 78 20 3d 20  f );..    idx = 
203aa 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ++pCur->aiIdx[pC
203ab 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20 20 7d  ur->iPage];..  }
203ac 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73 65 72  else{..    asser
203ad 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  t( pPage->leaf )
203ae 3b 0d 0a 20 20 7d 0d 0a 20 20 69 6e 73 65 72 74  ;..  }..  insert
203af 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
203b0 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c   newCell, szNew,
203b1 20 30 2c 20 30 2c 20 26 72 63 29 3b 0d 0a 20 20   0, 0, &rc);..  
203b2 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
203b3 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 2d 3e  TE_OK || pPage->
203b4 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 67 65  nCell>0 || pPage
203b5 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b  ->nOverflow>0 );
203b6 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 65  ....  /* If no e
203b7 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64  rror has occured
203b8 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20 61   and pPage has a
203b9 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c  n overflow cell,
203ba 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20   call balance() 
203bb 0d 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74  ..  ** to redist
203bc 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73  ribute the cells
203bd 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65   within the tree
203be 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28  . Since balance(
203bf 29 20 6d 61 79 20 6d 6f 76 65 0d 0a 20 20 2a 2a  ) may move..  **
203c0 20 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72   the cursor, zer
203c1 6f 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69  o the BtCursor.i
203c2 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74  nfo.nSize and Bt
203c3 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79  Cursor.validNKey
203c4 0d 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73  ..  ** variables
203c5 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 50 72  ...  **..  ** Pr
203c6 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20  evious versions 
203c7 6f 66 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 64  of SQLite called
203c8 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f   moveToRoot() to
203c9 20 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   move the cursor
203ca 0d 0a 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74  ..  ** back to t
203cb 68 65 20 72 6f 6f 74 20 70 61 67 65 20 61 73 20  he root page as 
203cc 62 61 6c 61 6e 63 65 28 29 20 75 73 65 64 20 74  balance() used t
203cd 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  o invalidate the
203ce 20 63 6f 6e 74 65 6e 74 73 0d 0a 20 20 2a 2a 20   contents..  ** 
203cf 6f 66 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61  of BtCursor.apPa
203d0 67 65 5b 5d 20 61 6e 64 20 42 74 43 75 72 73 6f  ge[] and BtCurso
203d1 72 2e 61 69 49 64 78 5b 5d 2e 20 49 6e 73 74 65  r.aiIdx[]. Inste
203d2 61 64 20 6f 66 20 64 6f 69 6e 67 20 74 68 61 74  ad of doing that
203d3 2c 0d 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20  ,..  ** set the 
203d4 63 75 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20  cursor state to 
203d5 22 69 6e 76 61 6c 69 64 22 2e 20 54 68 69 73 20  "invalid". This 
203d6 6d 61 6b 65 73 20 63 6f 6d 6d 6f 6e 20 69 6e 73  makes common ins
203d7 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 0d 0a  ert operations..
203d8 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 61    ** slightly fa
203d9 73 74 65 72 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  ster...  **..  *
203da 2a 20 54 68 65 72 65 20 69 73 20 61 20 73 75 62  * There is a sub
203db 74 6c 65 20 62 75 74 20 69 6d 70 6f 72 74 61 6e  tle but importan
203dc 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 68  t optimization h
203dd 65 72 65 20 74 6f 6f 2e 20 57 68 65 6e 20 69 6e  ere too. When in
203de 73 65 72 74 69 6e 67 0d 0a 20 20 2a 2a 20 6d 75  serting..  ** mu
203df 6c 74 69 70 6c 65 20 72 65 63 6f 72 64 73 20 69  ltiple records i
203e0 6e 74 6f 20 61 6e 20 69 6e 74 6b 65 79 20 62 2d  nto an intkey b-
203e1 74 72 65 65 20 75 73 69 6e 67 20 61 20 73 69 6e  tree using a sin
203e2 67 6c 65 20 63 75 72 73 6f 72 20 28 61 73 20 63  gle cursor (as c
203e3 61 6e 0d 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20  an..  ** happen 
203e4 77 68 69 6c 65 20 70 72 6f 63 65 73 73 69 6e 67  while processing
203e5 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f   an "INSERT INTO
203e6 20 2e 2e 2e 20 53 45 4c 45 43 54 22 20 73 74 61   ... SELECT" sta
203e7 74 65 6d 65 6e 74 29 2c 20 69 74 0d 0a 20 20 2a  tement), it..  *
203e8 2a 20 69 73 20 61 64 76 61 6e 74 61 67 65 6f 75  * is advantageou
203e9 73 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63  s to leave the c
203ea 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
203eb 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
203ec 20 69 6e 0d 0a 20 20 2a 2a 20 74 68 65 20 62 2d   in..  ** the b-
203ed 74 72 65 65 20 69 66 20 70 6f 73 73 69 62 6c 65  tree if possible
203ee 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  . If the cursor 
203ef 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
203f0 20 74 6f 20 74 68 65 20 6c 61 73 74 0d 0a 20 20   to the last..  
203f1 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ** entry in the 
203f2 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 6e  table, and the n
203f3 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65 64  ext row inserted
203f4 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20   has an integer 
203f5 6b 65 79 0d 0a 20 20 2a 2a 20 6c 61 72 67 65 72  key..  ** larger
203f6 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73   than the larges
203f7 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20  t existing key, 
203f8 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
203f9 6f 20 69 6e 73 65 72 74 20 74 68 65 0d 0a 20 20  o insert the..  
203fa 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73  ** row without s
203fb 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f  eeking the curso
203fc 72 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61  r. This can be a
203fd 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65   big performance
203fe 20 62 6f 6f 73 74 2e 0d 0a 20 20 2a 2f 0d 0a 20   boost...  */.. 
203ff 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
20400 65 20 3d 20 30 3b 0d 0a 20 20 70 43 75 72 2d 3e  e = 0;..  pCur->
20401 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0d 0a  validNKey = 0;..
20402 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
20403 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f  _OK && pPage->nO
20404 76 65 72 66 6c 6f 77 20 29 7b 0d 0a 20 20 20 20  verflow ){..    
20405 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
20406 72 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4d 75  r);....    /* Mu
20407 73 74 20 6d 61 6b 65 20 73 75 72 65 20 6e 4f 76  st make sure nOv
20408 65 72 66 6c 6f 77 20 69 73 20 72 65 73 65 74 20  erflow is reset 
20409 74 6f 20 7a 65 72 6f 20 65 76 65 6e 20 69 66 20  to zero even if 
2040a 74 68 65 20 62 61 6c 61 6e 63 65 28 29 0d 0a 20  the balance().. 
2040b 20 20 20 2a 2a 20 66 61 69 6c 73 2e 20 49 6e 74     ** fails. Int
2040c 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63  ernal data struc
2040d 74 75 72 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  ture corruption 
2040e 77 69 6c 6c 20 72 65 73 75 6c 74 20 6f 74 68 65  will result othe
2040f 72 77 69 73 65 2e 20 0d 0a 20 20 20 20 2a 2a 20  rwise. ..    ** 
20410 41 6c 73 6f 2c 20 73 65 74 20 74 68 65 20 63 75  Also, set the cu
20411 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20 69 6e  rsor state to in
20412 76 61 6c 69 64 2e 20 54 68 69 73 20 73 74 6f 70  valid. This stop
20413 73 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  s saveCursorPosi
20414 74 69 6f 6e 28 29 0d 0a 20 20 20 20 2a 2a 20 66  tion()..    ** f
20415 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 73 61  rom trying to sa
20416 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  ve the current p
20417 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  osition of the c
20418 75 72 73 6f 72 2e 20 20 2a 2f 0d 0a 20 20 20 20  ursor.  */..    
20419 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
2041a 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72  r->iPage]->nOver
2041b 66 6c 6f 77 20 3d 20 30 3b 0d 0a 20 20 20 20 70  flow = 0;..    p
2041c 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
2041d 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0d 0a 20  RSOR_INVALID;.. 
2041e 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43   }..  assert( pC
2041f 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
20420 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c  >iPage]->nOverfl
20421 6f 77 3d 3d 30 20 29 3b 0d 0a 0d 0a 65 6e 64 5f  ow==0 );....end_
20422 69 6e 73 65 72 74 3a 0d 0a 20 20 72 65 74 75 72  insert:..  retur
20423 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
20424 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e  ** Delete the en
20425 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72  try that the cur
20426 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
20427 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0d  to.  The cursor.
20428 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  .** is left poin
20429 74 69 6e 67 20 61 74 20 61 20 61 72 62 69 74 72  ting at a arbitr
2042a 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0d 0a 2a  ary location...*
2042b 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2042c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
2042d 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f  eeDelete(BtCurso
2042e 72 20 2a 70 43 75 72 29 7b 0d 0a 20 20 42 74 72  r *pCur){..  Btr
2042f 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42  ee *p = pCur->pB
20430 74 72 65 65 3b 0d 0a 20 20 42 74 53 68 61 72 65  tree;..  BtShare
20431 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
20432 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0d 0a                ..
20433 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
20434 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20435 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
20436 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 4d 65 6d 50   code */..  MemP
20437 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20  age *pPage;     
20438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20439 20 2f 2a 20 50 61 67 65 20 74 6f 20 64 65 6c 65   /* Page to dele
2043a 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20 2a 2f 0d  te cell from */.
2043b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2043c 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20   *pCell;        
2043d 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
2043e 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20 64 65  er to cell to de
2043f 6c 65 74 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  lete */..  int i
20440 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20 20  CellIdx;        
20441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20442 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c 6c  /* Index of cell
20443 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0d 0a 20   to delete */.. 
20444 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b   int iCellDepth;
20445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20446 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f        /* Depth o
20447 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e  f node containin
20448 67 20 70 43 65 6c 6c 20 2a 2f 20 0d 0a 0d 0a 20  g pCell */ .... 
20449 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
2044a 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
2044b 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42  );..  assert( pB
2044c 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
2044d 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
2044e 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 74  ..  assert( (pBt
2044f 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
20450 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 29  _READ_ONLY)==0 )
20451 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ;..  assert( pCu
20452 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0d 0a 20 20  r->wrFlag );..  
20453 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65  assert( hasShare
20454 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
20455 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  p, pCur->pgnoRoo
20456 74 2c 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  t, pCur->pKeyInf
20457 6f 21 3d 30 2c 20 32 29 20 29 3b 0d 0a 20 20 61  o!=0, 2) );..  a
20458 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43  ssert( !hasReadC
20459 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 43 75 72  onflicts(p, pCur
2045a 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0d 0a  ->pgnoRoot) );..
2045b 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 43  ..  if( NEVER(pC
2045c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
2045d 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70  iPage]>=pCur->ap
2045e 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
2045f 5d 2d 3e 6e 43 65 6c 6c 29 20 0d 0a 20 20 20 7c  ]->nCell) ..   |
20460 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 53  | NEVER(pCur->eS
20461 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
20462 49 44 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 72  ID)..  ){..    r
20463 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
20464 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e  OR;  /* Somethin
20465 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e  g has gone awry.
20466 20 2a 2f 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   */..  }....  /*
20467 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64 65   If this is a de
20468 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 74  lete operation t
20469 6f 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 66  o remove a row f
2046a 72 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 72  rom a table b-tr
2046b 65 65 2c 0d 0a 20 20 2a 2a 20 69 6e 76 61 6c 69  ee,..  ** invali
2046c 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f  date any incrblo
2046d 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
2046e 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  n the row being 
2046f 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0d 0a 20 20  deleted.  */..  
20470 69 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e  if( pCur->pKeyIn
20471 66 6f 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e  fo==0 ){..    in
20472 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62  validateIncrblob
20473 43 75 72 73 6f 72 73 28 70 2c 20 70 43 75 72 2d  Cursors(p, pCur-
20474 3e 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0d  >info.nKey, 0);.
20475 0a 20 20 7d 0d 0a 0d 0a 20 20 69 43 65 6c 6c 44  .  }....  iCellD
20476 65 70 74 68 20 3d 20 70 43 75 72 2d 3e 69 50 61  epth = pCur->iPa
20477 67 65 3b 0d 0a 20 20 69 43 65 6c 6c 49 64 78 20  ge;..  iCellIdx 
20478 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 43  = pCur->aiIdx[iC
20479 65 6c 6c 44 65 70 74 68 5d 3b 0d 0a 20 20 70 50  ellDepth];..  pP
2047a 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
2047b 67 65 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b 0d  ge[iCellDepth];.
2047c 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43  .  pCell = findC
2047d 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
2047e 49 64 78 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  Idx);....  /* If
2047f 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69   the page contai
20480 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20 74  ning the entry t
20481 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f 74 20  o delete is not 
20482 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d 6f 76  a leaf page, mov
20483 65 0d 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73  e..  ** the curs
20484 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73  or to the larges
20485 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
20486 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61 6c  ree that is smal
20487 6c 65 72 20 74 68 61 6e 0d 0a 20 20 2a 2a 20 74  ler than..  ** t
20488 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64  he entry being d
20489 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c  eleted. This cel
2048a 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74  l will replace t
2048b 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65  he cell being de
2048c 6c 65 74 65 64 0d 0a 20 20 2a 2a 20 66 72 6f 6d  leted..  ** from
2048d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
2048e 64 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75  de. The 'previou
2048f 73 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64  s' entry is used
20490 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61   for this instea
20491 64 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27  d..  ** of the '
20492 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20  next' entry, as 
20493 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74  the previous ent
20494 72 79 20 69 73 20 61 6c 77 61 79 73 20 61 20 70  ry is always a p
20495 61 72 74 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a  art of the..  **
20496 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64   sub-tree headed
20497 20 62 79 20 74 68 65 20 63 68 69 6c 64 20 70 61   by the child pa
20498 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 62  ge of the cell b
20499 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68  eing deleted. Th
2049a 69 73 20 6d 61 6b 65 73 0d 0a 20 20 2a 2a 20 62  is makes..  ** b
2049b 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 74 72 65  alancing the tre
2049c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  e following the 
2049d 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e  delete operation
2049e 20 65 61 73 69 65 72 2e 20 20 2a 2f 0d 0a 20 20   easier.  */..  
2049f 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
204a0 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 6f 74   ){..    int not
204a1 55 73 65 64 3b 0d 0a 20 20 20 20 72 63 20 3d 20  Used;..    rc = 
204a2 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
204a3 69 6f 75 73 28 70 43 75 72 2c 20 26 6e 6f 74 55  ious(pCur, &notU
204a4 73 65 64 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  sed);..    if( r
204a5 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  c ) return rc;..
204a6 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 61 76 65    }....  /* Save
204a7 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f   the positions o
204a8 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73  f any other curs
204a9 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73  ors open on this
204aa 20 74 61 62 6c 65 20 62 65 66 6f 72 65 0d 0a 20   table before.. 
204ab 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 6e 79 20 6d   ** making any m
204ac 6f 64 69 66 69 63 61 74 69 6f 6e 73 2e 20 4d 61  odifications. Ma
204ad 6b 65 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  ke the page cont
204ae 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79  aining the entry
204af 20 74 6f 20 62 65 20 0d 0a 20 20 2a 2a 20 64 65   to be ..  ** de
204b0 6c 65 74 65 64 20 77 72 69 74 61 62 6c 65 2e 20  leted writable. 
204b1 54 68 65 6e 20 66 72 65 65 20 61 6e 79 20 6f 76  Then free any ov
204b2 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73  erflow pages ass
204b3 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
204b4 20 0d 0a 20 20 2a 2a 20 65 6e 74 72 79 20 61 6e   ..  ** entry an
204b5 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f 76 65  d finally remove
204b6 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66   the cell itself
204b7 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
204b8 20 70 61 67 65 2e 20 20 0d 0a 20 20 2a 2f 0d 0a   page.  ..  */..
204b9 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
204ba 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
204bb 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
204bc 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  ;..  if( rc ) re
204bd 74 75 72 6e 20 72 63 3b 0d 0a 20 20 72 63 20 3d  turn rc;..  rc =
204be 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
204bf 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
204c0 65 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 20  e);..  if( rc ) 
204c1 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 72 63  return rc;..  rc
204c2 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61   = clearCell(pPa
204c3 67 65 2c 20 70 43 65 6c 6c 29 3b 0d 0a 20 20 64  ge, pCell);..  d
204c4 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ropCell(pPage, i
204c5 43 65 6c 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a  CellIdx, cellSiz
204c6 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
204c7 6c 29 2c 20 26 72 63 29 3b 0d 0a 20 20 69 66 28  l), &rc);..  if(
204c8 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
204c9 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
204ca 63 65 6c 6c 20 64 65 6c 65 74 65 64 20 77 61 73  cell deleted was
204cb 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 6f 6e 20   not located on 
204cc 61 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  a leaf page, the
204cd 6e 20 74 68 65 20 63 75 72 73 6f 72 0d 0a 20 20  n the cursor..  
204ce 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ** is currently 
204cf 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
204d0 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 69 6e  largest entry in
204d1 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65   the sub-tree he
204d2 61 64 65 64 0d 0a 20 20 2a 2a 20 62 79 20 74 68  aded..  ** by th
204d3 65 20 63 68 69 6c 64 2d 70 61 67 65 20 6f 66 20  e child-page of 
204d4 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 77 61  the cell that wa
204d5 73 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 66  s just deleted f
204d6 72 6f 6d 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0d  rom an internal.
204d7 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 54 68 65 20  .  ** node. The 
204d8 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6c 65  cell from the le
204d9 61 66 20 6e 6f 64 65 20 6e 65 65 64 73 20 74 6f  af node needs to
204da 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65   be moved to the
204db 20 69 6e 74 65 72 6e 61 6c 0d 0a 20 20 2a 2a 20   internal..  ** 
204dc 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 63 65 20  node to replace 
204dd 74 68 65 20 64 65 6c 65 74 65 64 20 63 65 6c 6c  the deleted cell
204de 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 21 70 50  .  */..  if( !pP
204df 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a 20 20  age->leaf ){..  
204e0 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66    MemPage *pLeaf
204e1 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
204e2 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20  pCur->iPage];.. 
204e3 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0d 0a 20     int nCell;.. 
204e4 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72     Pgno n = pCur
204e5 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65  ->apPage[iCellDe
204e6 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0d 0a 20  pth+1]->pgno;.. 
204e7 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
204e8 20 2a 70 54 6d 70 3b 0d 0a 0d 0a 20 20 20 20 70   *pTmp;....    p
204e9 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
204ea 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43  pLeaf, pLeaf->nC
204eb 65 6c 6c 2d 31 29 3b 0d 0a 20 20 20 20 6e 43 65  ell-1);..    nCe
204ec 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  ll = cellSizePtr
204ed 28 70 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0d  (pLeaf, pCell);.
204ee 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f  .    assert( MX_
204ef 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 3e  CELL_SIZE(pBt) >
204f0 3d 20 6e 43 65 6c 6c 20 29 3b 0d 0a 0d 0a 20 20  = nCell );....  
204f1 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70    allocateTempSp
204f2 61 63 65 28 70 42 74 29 3b 0d 0a 20 20 20 20 70  ace(pBt);..    p
204f3 54 6d 70 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53  Tmp = pBt->pTmpS
204f4 70 61 63 65 3b 0d 0a 0d 0a 20 20 20 20 72 63 20  pace;....    rc 
204f5 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
204f6 69 74 65 28 70 4c 65 61 66 2d 3e 70 44 62 50 61  ite(pLeaf->pDbPa
204f7 67 65 29 3b 0d 0a 20 20 20 20 69 6e 73 65 72 74  ge);..    insert
204f8 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
204f9 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 2c 20 6e  lIdx, pCell-4, n
204fa 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c 20 6e 2c  Cell+4, pTmp, n,
204fb 20 26 72 63 29 3b 0d 0a 20 20 20 20 64 72 6f 70   &rc);..    drop
204fc 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c 65 61  Cell(pLeaf, pLea
204fd 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 65 6c  f->nCell-1, nCel
204fe 6c 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20 69 66  l, &rc);..    if
204ff 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
20500 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 42  ;..  }....  /* B
20501 61 6c 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e  alance the tree.
20502 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 64 65   If the entry de
20503 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65  leted was locate
20504 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65  d on a leaf page
20505 2c 0d 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  ,..  ** then the
20506 20 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f   cursor still po
20507 69 6e 74 73 20 74 6f 20 74 68 61 74 20 70 61 67  ints to that pag
20508 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
20509 74 68 65 20 66 69 72 73 74 0d 0a 20 20 2a 2a 20  the first..  ** 
2050a 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28  call to balance(
2050b 29 20 72 65 70 61 69 72 73 20 74 68 65 20 74 72  ) repairs the tr
2050c 65 65 2c 20 61 6e 64 20 74 68 65 20 69 66 28 2e  ee, and the if(.
2050d 2e 2e 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73  ..) condition is
2050e 0d 0a 20 20 2a 2a 20 6e 65 76 65 72 20 74 72 75  ..  ** never tru
2050f 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4f  e...  **..  ** O
20510 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
20511 20 65 6e 74 72 79 20 64 65 6c 65 74 65 64 20 77   entry deleted w
20512 61 73 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61  as on an interna
20513 6c 20 6e 6f 64 65 20 70 61 67 65 2c 20 74 68 65  l node page, the
20514 6e 0d 0a 20 20 2a 2a 20 70 43 75 72 20 69 73 20  n..  ** pCur is 
20515 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
20516 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d 20 77  leaf page from w
20517 68 69 63 68 20 61 20 63 65 6c 6c 20 77 61 73 20  hich a cell was 
20518 72 65 6d 6f 76 65 64 20 74 6f 0d 0a 20 20 2a 2a  removed to..  **
20519 20 72 65 70 6c 61 63 65 20 74 68 65 20 63 65 6c   replace the cel
2051a 6c 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74  l deleted from t
2051b 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
2051c 2e 20 54 68 69 73 20 69 73 20 73 6c 69 67 68 74  . This is slight
2051d 6c 79 0d 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20  ly..  ** tricky 
2051e 61 73 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  as the leaf node
2051f 20 6d 61 79 20 62 65 20 75 6e 64 65 72 66 75 6c   may be underful
20520 6c 2c 20 61 6e 64 20 74 68 65 20 69 6e 74 65 72  l, and the inter
20521 6e 61 6c 20 6e 6f 64 65 20 6d 61 79 0d 0a 20 20  nal node may..  
20522 2a 2a 20 62 65 20 65 69 74 68 65 72 20 75 6e 64  ** be either und
20523 65 72 20 6f 72 20 6f 76 65 72 66 75 6c 6c 2e 20  er or overfull. 
20524 49 6e 20 74 68 69 73 20 63 61 73 65 20 72 75 6e  In this case run
20525 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 61   the balancing a
20526 6c 67 6f 72 69 74 68 6d 0d 0a 20 20 2a 2a 20 6f  lgorithm..  ** o
20527 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20  n the leaf node 
20528 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 62 61  first. If the ba
20529 6c 61 6e 63 65 20 70 72 6f 63 65 65 64 73 20 66  lance proceeds f
2052a 61 72 20 65 6e 6f 75 67 68 20 75 70 20 74 68 65  ar enough up the
2052b 0d 0a 20 20 2a 2a 20 74 72 65 65 20 74 68 61 74  ..  ** tree that
2052c 20 77 65 20 63 61 6e 20 62 65 20 73 75 72 65 20   we can be sure 
2052d 74 68 61 74 20 61 6e 79 20 70 72 6f 62 6c 65 6d  that any problem
2052e 20 69 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   in the internal
2052f 20 6e 6f 64 65 20 68 61 73 0d 0a 20 20 2a 2a 20   node has..  ** 
20530 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 2c 20  been corrected, 
20531 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 72 77  so be it. Otherw
20532 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c 61 6e  ise, after balan
20533 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 6e 6f  cing the leaf no
20534 64 65 2c 0d 0a 20 20 2a 2a 20 77 61 6c 6b 20 74  de,..  ** walk t
20535 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 65  he cursor up the
20536 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e 74   tree to the int
20537 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 62  ernal node and b
20538 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0d 0a 20  alance it as .. 
20539 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0d 0a 20   ** well.  */.. 
2053a 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43   rc = balance(pC
2053b 75 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d  ur);..  if( rc==
2053c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75  SQLITE_OK && pCu
2053d 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65  r->iPage>iCellDe
2053e 70 74 68 20 29 7b 0d 0a 20 20 20 20 77 68 69 6c  pth ){..    whil
2053f 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 69  e( pCur->iPage>i
20540 43 65 6c 6c 44 65 70 74 68 20 29 7b 0d 0a 20 20  CellDepth ){..  
20541 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
20542 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
20543 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0d 0a 20  r->iPage--]);.. 
20544 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 62     }..    rc = b
20545 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0d 0a 20  alance(pCur);.. 
20546 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d   }....  if( rc==
20547 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
20548 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75    moveToRoot(pCu
20549 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  r);..  }..  retu
2054a 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
2054b 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
2054c 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57   BTree table.  W
2054d 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62  rite into *piTab
2054e 6c 65 20 74 68 65 20 70 61 67 65 0d 0a 2a 2a 20  le the page..** 
2054f 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
20550 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
20551 6e 65 77 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a  new table...**..
20552 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74  ** The type of t
20553 79 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ype is determine
20554 64 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70  d by the flags p
20555 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20  arameter.  Only 
20556 74 68 65 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  the..** followin
20557 67 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67  g values of flag
20558 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  s are currently 
20559 69 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76  in use.  Other v
2055a 61 6c 75 65 73 20 66 6f 72 0d 0a 2a 2a 20 66 6c  alues for..** fl
2055b 61 67 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f  ags might not wo
2055c 72 6b 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  rk:..**..**     
2055d 42 54 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52  BTREE_INTKEY|BTR
2055e 45 45 5f 4c 45 41 46 44 41 54 41 20 20 20 20 20  EE_LEAFDATA     
2055f 55 73 65 64 20 66 6f 72 20 53 51 4c 20 74 61 62  Used for SQL tab
20560 6c 65 73 20 77 69 74 68 20 72 6f 77 69 64 20 6b  les with rowid k
20561 65 79 73 0d 0a 2a 2a 20 20 20 20 20 42 54 52 45  eys..**     BTRE
20562 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 20 20 20  E_ZERODATA      
20563 20 20 20 20 20 20 20 20 20 20 20 20 55 73 65 64              Used
20564 20 66 6f 72 20 53 51 4c 20 69 6e 64 69 63 65 73   for SQL indices
20565 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
20566 20 62 74 72 65 65 43 72 65 61 74 65 54 61 62 6c   btreeCreateTabl
20567 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
20568 2a 70 69 54 61 62 6c 65 2c 20 69 6e 74 20 63 72  *piTable, int cr
20569 65 61 74 65 54 61 62 46 6c 61 67 73 29 7b 0d 0a  eateTabFlags){..
2056a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
2056b 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 4d 65 6d  = p->pBt;..  Mem
2056c 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0d 0a 20 20  Page *pRoot;..  
2056d 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0d 0a  Pgno pgnoRoot;..
2056e 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74    int rc;..  int
2056f 20 70 74 66 46 6c 61 67 73 3b 20 20 20 20 20 20   ptfFlags;      
20570 20 20 20 20 2f 2a 20 50 61 67 65 2d 74 79 70 65      /* Page-type
20571 20 66 6c 61 67 65 20 66 6f 72 20 74 68 65 20 72   flage for the r
20572 6f 6f 74 20 70 61 67 65 20 6f 66 20 6e 65 77 20  oot page of new 
20573 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73  table */....  as
20574 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
20575 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20  eeHoldsMutex(p) 
20576 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42  );..  assert( pB
20577 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
20578 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
20579 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 74  ..  assert( (pBt
2057a 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
2057b 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 29  _READ_ONLY)==0 )
2057c 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  ;....#ifdef SQLI
2057d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
2057e 55 4d 0d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63  UM..  rc = alloc
2057f 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
20580 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
20581 6f 6f 74 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 69  oot, 1, 0);..  i
20582 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20 72 65  f( rc ){..    re
20583 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 23  turn rc;..  }..#
20584 65 6c 73 65 0d 0a 20 20 69 66 28 20 70 42 74 2d  else..  if( pBt-
20585 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0d 0a  >autoVacuum ){..
20586 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76      Pgno pgnoMov
20587 65 3b 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20  e;      /* Move 
20588 61 20 70 61 67 65 20 68 65 72 65 20 74 6f 20 6d  a page here to m
20589 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65  ake room for the
2058a 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0d 0a 20   root-page */.. 
2058b 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67     MemPage *pPag
2058c 65 4d 6f 76 65 3b 20 2f 2a 20 54 68 65 20 70 61  eMove; /* The pa
2058d 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a  ge to move to. *
2058e 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 72 65 61  /....    /* Crea
2058f 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65  ting a new table
20590 20 6d 61 79 20 70 72 6f 62 61 62 6c 79 20 72 65   may probably re
20591 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20  quire moving an 
20592 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  existing databas
20593 65 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b  e..    ** to mak
20594 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 6e  e room for the n
20595 65 77 20 74 61 62 6c 65 73 20 72 6f 6f 74 20 70  ew tables root p
20596 61 67 65 2e 20 49 6e 20 63 61 73 65 20 74 68 69  age. In case thi
20597 73 20 70 61 67 65 20 74 75 72 6e 73 0d 0a 20 20  s page turns..  
20598 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 61    ** out to be a
20599 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  n overflow page,
2059a 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72   delete all over
2059b 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 63 61  flow page-map ca
2059c 63 68 65 73 0d 0a 20 20 20 20 2a 2a 20 68 65 6c  ches..    ** hel
2059d 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 6f 72  d by open cursor
2059e 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  s...    */..    
2059f 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
205a0 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
205a1 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ....    /* Read 
205a2 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74  the value of met
205a3 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61  a[3] from the da
205a4 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d  tabase to determ
205a5 69 6e 65 20 77 68 65 72 65 20 74 68 65 0d 0a 20  ine where the.. 
205a6 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20     ** root page 
205a7 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
205a8 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61   should go. meta
205a9 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65  [3] is the large
205aa 73 74 20 72 6f 6f 74 2d 70 61 67 65 0d 0a 20 20  st root-page..  
205ab 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20    ** created so 
205ac 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20  far, so the new 
205ad 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65  root-page is (me
205ae 74 61 5b 33 5d 2b 31 29 2e 0d 0a 20 20 20 20 2a  ta[3]+1)...    *
205af 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  /..    sqlite3Bt
205b0 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54  reeGetMeta(p, BT
205b1 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54  REE_LARGEST_ROOT
205b2 5f 50 41 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74  _PAGE, &pgnoRoot
205b3 29 3b 0d 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74  );..    pgnoRoot
205b4 2b 2b 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68  ++;....    /* Th
205b5 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 65 20  e new root-page 
205b6 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  may not be alloc
205b7 61 74 65 64 20 6f 6e 20 61 20 70 6f 69 6e 74 65  ated on a pointe
205b8 72 2d 6d 61 70 20 70 61 67 65 2c 20 6f 72 20 74  r-map page, or t
205b9 68 65 0d 0a 20 20 20 20 2a 2a 20 50 45 4e 44 49  he..    ** PENDI
205ba 4e 47 5f 42 59 54 45 20 70 61 67 65 2e 0d 0a 20  NG_BYTE page... 
205bb 20 20 20 2a 2f 0d 0a 20 20 20 20 77 68 69 6c 65     */..    while
205bc 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 52 4d  ( pgnoRoot==PTRM
205bd 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70  AP_PAGENO(pBt, p
205be 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0d 0a 20 20 20  gnoRoot) ||..   
205bf 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50       pgnoRoot==P
205c0 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
205c1 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20 20 20  (pBt) ){..      
205c2 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0d 0a 20 20 20  pgnoRoot++;..   
205c3 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
205c4 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0d 0a  pgnoRoot>=3 );..
205c5 0d 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ..    /* Allocat
205c6 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61  e a page. The pa
205c7 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c  ge that currentl
205c8 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e  y resides at pgn
205c9 6f 52 6f 6f 74 20 77 69 6c 6c 0d 0a 20 20 20 20  oRoot will..    
205ca 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74  ** be moved to t
205cb 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  he allocated pag
205cc 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c  e (unless the al
205cd 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70  located page hap
205ce 70 65 6e 73 0d 0a 20 20 20 20 2a 2a 20 74 6f 20  pens..    ** to 
205cf 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f  reside at pgnoRo
205d0 6f 74 29 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ot)...    */..  
205d1 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
205d2 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
205d3 50 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d  PageMove, &pgnoM
205d4 6f 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31  ove, pgnoRoot, 1
205d5 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d  );..    if( rc!=
205d6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
205d7 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
205d8 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28      }....    if(
205d9 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 6e 6f 52   pgnoMove!=pgnoR
205da 6f 6f 74 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  oot ){..      /*
205db 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 65   pgnoRoot is the
205dc 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c 20   page that will 
205dd 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
205de 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0d 0a 20 20  root-page of..  
205df 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 74      ** the new t
205e0 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 61  able (assuming a
205e1 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 20  n error did not 
205e2 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 77  occur). But we w
205e3 65 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6c  ere..      ** al
205e4 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65  located pgnoMove
205e5 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28 69  . If required (i
205e6 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f  .e. if it was no
205e7 74 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 20 20 20  t allocated..   
205e8 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69     ** by extendi
205e9 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68  ng the file), th
205ea 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61  e current page a
205eb 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d  t position pgnoM
205ec 6f 76 65 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73  ove..      ** is
205ed 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c   already journal
205ee 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  ed...      */.. 
205ef 20 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20       u8 eType = 
205f0 30 3b 0d 0a 20 20 20 20 20 20 50 67 6e 6f 20 69  0;..      Pgno i
205f1 50 74 72 50 61 67 65 20 3d 20 30 3b 0d 0a 0d 0a  PtrPage = 0;....
205f2 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
205f3 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0d 0a 0d  e(pPageMove);...
205f4 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  .      /* Move t
205f5 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  he page currentl
205f6 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f  y at pgnoRoot to
205f7 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0d 0a 20   pgnoMove. */.. 
205f8 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47       rc = btreeG
205f9 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
205fa 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29  Root, &pRoot, 0)
205fb 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  ;..      if( rc!
205fc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
205fd 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
205fe 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
205ff 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
20600 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
20601 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67  &eType, &iPtrPag
20602 65 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 65  e);..      if( e
20603 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type==PTRMAP_ROO
20604 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d 3d  TPAGE || eType==
20605 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
20606 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
20607 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
20608 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  BKPT;..      }..
20609 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2060a 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
2060b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
2060c 70 52 6f 6f 74 29 3b 0d 0a 20 20 20 20 20 20 20  pRoot);..       
2060d 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
2060e 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
2060f 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  rt( eType!=PTRMA
20610 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0d 0a 20  P_ROOTPAGE );.. 
20611 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
20612 70 65 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe!=PTRMAP_FREEP
20613 41 47 45 20 29 3b 0d 0a 20 20 20 20 20 20 72 63  AGE );..      rc
20614 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
20615 70 42 74 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70  pBt, pRoot, eTyp
20616 65 2c 20 69 50 74 72 50 61 67 65 2c 20 70 67 6e  e, iPtrPage, pgn
20617 6f 4d 6f 76 65 2c 20 30 29 3b 0d 0a 20 20 20 20  oMove, 0);..    
20618 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
20619 6f 6f 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f  oot);....      /
2061a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67  * Obtain the pag
2061b 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f  e at pgnoRoot */
2061c 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
2061d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
2061e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
2061f 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
20620 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
20621 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74  ge(pBt, pgnoRoot
20622 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0d 0a 20  , &pRoot, 0);.. 
20623 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
20624 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
20625 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
20626 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 63       }..      rc
20627 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
20628 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  rite(pRoot->pDbP
20629 61 67 65 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  age);..      if(
2062a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2062b 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  {..        relea
2062c 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0d 0a  sePage(pRoot);..
2062d 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
2062e 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  c;..      }..   
2062f 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70   }else{..      p
20630 52 6f 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65  Root = pPageMove
20631 3b 0d 0a 20 20 20 20 7d 20 0d 0a 0d 0a 20 20 20  ;..    } ....   
20632 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 70   /* Update the p
20633 6f 69 6e 74 65 72 2d 6d 61 70 20 61 6e 64 20 6d  ointer-map and m
20634 65 74 61 2d 64 61 74 61 20 77 69 74 68 20 74 68  eta-data with th
20635 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 65 20  e new root-page 
20636 6e 75 6d 62 65 72 2e 20 2a 2f 0d 0a 20 20 20 20  number. */..    
20637 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
20638 67 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f  gnoRoot, PTRMAP_
20639 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 26 72 63  ROOTPAGE, 0, &rc
2063a 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29  );..    if( rc )
2063b 7b 0d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  {..      release
2063c 50 61 67 65 28 70 52 6f 6f 74 29 3b 0d 0a 20 20  Page(pRoot);..  
2063d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a      return rc;..
2063e 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
2063f 57 68 65 6e 20 74 68 65 20 6e 65 77 20 72 6f 6f  When the new roo
20640 74 20 70 61 67 65 20 77 61 73 20 61 6c 6c 6f 63  t page was alloc
20641 61 74 65 64 2c 20 70 61 67 65 20 31 20 77 61 73  ated, page 1 was
20642 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 69   made writable i
20643 6e 0d 0a 20 20 20 20 2a 2a 20 6f 72 64 65 72 20  n..    ** order 
20644 65 69 74 68 65 72 20 74 6f 20 69 6e 63 72 65 61  either to increa
20645 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  se the database 
20646 66 69 6c 65 73 69 7a 65 2c 20 6f 72 20 74 6f 20  filesize, or to 
20647 64 65 63 72 65 6d 65 6e 74 20 74 68 65 0d 0a 20  decrement the.. 
20648 20 20 20 2a 2a 20 66 72 65 65 6c 69 73 74 20 63     ** freelist c
20649 6f 75 6e 74 2e 20 20 48 65 6e 63 65 2c 20 74 68  ount.  Hence, th
2064a 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  e sqlite3BtreeUp
2064b 64 61 74 65 4d 65 74 61 28 29 20 63 61 6c 6c 20  dateMeta() call 
2064c 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0d 0a 20 20  cannot fail...  
2064d 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74    */..    assert
2064e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
2064f 77 72 69 74 65 61 62 6c 65 28 70 42 74 2d 3e 70  writeable(pBt->p
20650 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20  Page1->pDbPage) 
20651 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  );..    rc = sql
20652 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
20653 65 74 61 28 70 2c 20 34 2c 20 70 67 6e 6f 52 6f  eta(p, 4, pgnoRo
20654 6f 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 4e 45  ot);..    if( NE
20655 56 45 52 28 72 63 29 20 29 7b 0d 0a 20 20 20 20  VER(rc) ){..    
20656 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
20657 6f 6f 74 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  oot);..      ret
20658 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a  urn rc;..    }..
20659 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
2065a 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
2065b 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f  eePage(pBt, &pRo
2065c 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31  ot, &pgnoRoot, 1
2065d 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  , 0);..    if( r
2065e 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  c ) return rc;..
2065f 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 61    }..#endif..  a
20660 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
20661 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
20662 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 29  Root->pDbPage) )
20663 3b 0d 0a 20 20 69 66 28 20 63 72 65 61 74 65 54  ;..  if( createT
20664 61 62 46 6c 61 67 73 20 26 20 42 54 52 45 45 5f  abFlags & BTREE_
20665 49 4e 54 4b 45 59 20 29 7b 0d 0a 20 20 20 20 70  INTKEY ){..    p
20666 74 66 46 6c 61 67 73 20 3d 20 50 54 46 5f 49 4e  tfFlags = PTF_IN
20667 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 44  TKEY | PTF_LEAFD
20668 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46 3b 0d  ATA | PTF_LEAF;.
20669 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
2066a 74 66 46 6c 61 67 73 20 3d 20 50 54 46 5f 5a 45  tfFlags = PTF_ZE
2066b 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41  RODATA | PTF_LEA
2066c 46 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 65 72 6f 50  F;..  }..  zeroP
2066d 61 67 65 28 70 52 6f 6f 74 2c 20 70 74 66 46 6c  age(pRoot, ptfFl
2066e 61 67 73 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ags);..  sqlite3
2066f 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f 6f 74  PagerUnref(pRoot
20670 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20 20 61  ->pDbPage);..  a
20671 73 73 65 72 74 28 20 28 70 42 74 2d 3e 6f 70 65  ssert( (pBt->ope
20672 6e 46 6c 61 67 73 20 26 20 42 54 52 45 45 5f 53  nFlags & BTREE_S
20673 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20 70 67 6e  INGLE)==0 || pgn
20674 6f 52 6f 6f 74 3d 3d 32 20 29 3b 0d 0a 20 20 2a  oRoot==2 );..  *
20675 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 70  piTable = (int)p
20676 67 6e 6f 52 6f 6f 74 3b 0d 0a 20 20 72 65 74 75  gnoRoot;..  retu
20677 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
20678 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
20679 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
2067a 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
2067b 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61  ee *p, int *piTa
2067c 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  ble, int flags){
2067d 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73  ..  int rc;..  s
2067e 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
2067f 28 70 29 3b 0d 0a 20 20 72 63 20 3d 20 62 74 72  (p);..  rc = btr
20680 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c  eeCreateTable(p,
20681 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29   piTable, flags)
20682 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
20683 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20 72 65  eLeave(p);..  re
20684 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
20685 2a 0d 0a 2a 2a 20 45 72 61 73 65 20 74 68 65 20  *..** Erase the 
20686 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 70  given database p
20687 61 67 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20  age and all its 
20688 63 68 69 6c 64 72 65 6e 2e 20 20 52 65 74 75 72  children.  Retur
20689 6e 0d 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 74  n..** the page t
2068a 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0d  o the freelist..
2068b 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
2068c 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
2068d 65 28 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a  e(..  BtShared *
2068e 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pBt,           /
2068f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61 74  * The BTree that
20690 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
20691 62 6c 65 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 70  ble */..  Pgno p
20692 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  gno,            
20693 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
20694 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0d 0a 20  r to clear */.. 
20695 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61   int freePageFla
20696 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61  g,        /* Dea
20697 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20  llocate page if 
20698 74 72 75 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  true */..  int *
20699 70 6e 43 68 61 6e 67 65 20 20 20 20 20 20 20 20  pnChange        
2069a 20 20 20 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65      /* Add numbe
2069b 72 20 6f 66 20 43 65 6c 6c 73 20 66 72 65 65 64  r of Cells freed
2069c 20 74 6f 20 74 68 69 73 20 63 6f 75 6e 74 65 72   to this counter
2069d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 4d 65 6d 50 61   */..){..  MemPa
2069e 67 65 20 2a 70 50 61 67 65 3b 0d 0a 20 20 69 6e  ge *pPage;..  in
2069f 74 20 72 63 3b 0d 0a 20 20 75 6e 73 69 67 6e 65  t rc;..  unsigne
206a0 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0d 0a  d char *pCell;..
206a1 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20 61 73    int i;....  as
206a2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
206a3 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
206a4 74 65 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 70  tex) );..  if( p
206a5 67 6e 6f 3e 62 74 72 65 65 50 61 67 65 63 6f 75  gno>btreePagecou
206a6 6e 74 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20  nt(pBt) ){..    
206a7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
206a8 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 20 20 7d  RRUPT_BKPT;..  }
206a9 0d 0a 0d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e  ....  rc = getAn
206aa 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70  dInitPage(pBt, p
206ab 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0d 0a 20  gno, &pPage);.. 
206ac 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
206ad 20 72 63 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b   rc;..  for(i=0;
206ae 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   i<pPage->nCell;
206af 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 70 43 65 6c   i++){..    pCel
206b0 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
206b1 67 65 2c 20 69 29 3b 0d 0a 20 20 20 20 69 66 28  ge, i);..    if(
206b2 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
206b3 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65  ..      rc = cle
206b4 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70  arDatabasePage(p
206b5 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65  Bt, get4byte(pCe
206b6 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65  ll), 1, pnChange
206b7 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
206b8 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74   ) goto cleardat
206b9 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0d 0a  abasepage_out;..
206ba 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
206bb 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
206bc 20 70 43 65 6c 6c 29 3b 0d 0a 20 20 20 20 69 66   pCell);..    if
206bd 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
206be 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
206bf 74 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21  t;..  }..  if( !
206c0 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a  pPage->leaf ){..
206c1 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
206c2 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
206c3 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
206c4 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70  >aData[8]), 1, p
206c5 6e 43 68 61 6e 67 65 29 3b 0d 0a 20 20 20 20 69  nChange);..    i
206c6 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65  f( rc ) goto cle
206c7 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
206c8 75 74 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  ut;..  }else if(
206c9 20 70 6e 43 68 61 6e 67 65 20 29 7b 0d 0a 20 20   pnChange ){..  
206ca 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
206cb 3e 69 6e 74 4b 65 79 20 29 3b 0d 0a 20 20 20 20  >intKey );..    
206cc 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50 61  *pnChange += pPa
206cd 67 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 20 20 7d 0d  ge->nCell;..  }.
206ce 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46  .  if( freePageF
206cf 6c 61 67 20 29 7b 0d 0a 20 20 20 20 66 72 65 65  lag ){..    free
206d0 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29  Page(pPage, &rc)
206d1 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  ;..  }else if( (
206d2 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
206d3 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
206d4 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0d 0a 20  bPage))==0 ){.. 
206d5 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
206d6 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e, pPage->aData[
206d7 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0d  0] | PTF_LEAF);.
206d8 0a 20 20 7d 0d 0a 0d 0a 63 6c 65 61 72 64 61 74  .  }....cleardat
206d9 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0d 0a  abasepage_out:..
206da 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
206db 61 67 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  age);..  return 
206dc 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
206dd 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f   Delete all info
206de 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73  rmation from a s
206df 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74  ingle table in t
206e0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 69 54  he database.  iT
206e1 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 74 68 65 20  able is..** the 
206e2 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
206e3 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74  he root of the t
206e4 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74 68 69  able.  After thi
206e5 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
206e6 73 2c 0d 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20  s,..** the root 
206e7 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20 62  page is empty, b
206e8 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2e  ut still exists.
206e9 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ..**..** This ro
206ea 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20  utine will fail 
206eb 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  with SQLITE_LOCK
206ec 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ED if there are 
206ed 61 6e 79 20 6f 70 65 6e 0d 0a 2a 2a 20 72 65 61  any open..** rea
206ee 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65  d cursors on the
206ef 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72   table.  Open wr
206f0 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 20  ite cursors are 
206f1 6d 6f 76 65 64 20 74 6f 20 74 68 65 0d 0a 2a 2a  moved to the..**
206f2 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62   root of the tab
206f3 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70  le...**..** If p
206f4 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e  nChange is not N
206f5 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20  ULL, then table 
206f6 69 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61  iTable must be a
206f7 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20  n intkey table. 
206f8 54 68 65 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 20  The..** integer 
206f9 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f  value pointed to
206fa 20 62 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20   by pnChange is 
206fb 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
206fc 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a 2a 2a  he number of..**
206fd 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
206fe 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  table...*/..SQLI
206ff 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20700 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
20701 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20  Table(Btree *p, 
20702 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20  int iTable, int 
20703 2a 70 6e 43 68 61 6e 67 65 29 7b 0d 0a 20 20 69  *pnChange){..  i
20704 6e 74 20 72 63 3b 0d 0a 20 20 42 74 53 68 61 72  nt rc;..  BtShar
20705 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
20706 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
20707 65 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20 61 73  eEnter(p);..  as
20708 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73  sert( p->inTrans
20709 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
2070a 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64  ....  /* Invalid
2070b 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62  ate all incrblob
2070c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
2070d 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28 61   table iTable (a
2070e 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0d 0a  ssuming iTable..
2070f 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74    ** is the root
20710 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72   of a table b-tr
20711 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f  ee - if it is no
20712 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
20713 20 63 61 6c 6c 20 69 73 0d 0a 20 20 2a 2a 20 61   call is..  ** a
20714 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0d 0a 20 20   no-op).  */..  
20715 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c  invalidateIncrbl
20716 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20  obCursors(p, 0, 
20717 31 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 61  1);....  rc = sa
20718 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
20719 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20  , (Pgno)iTable, 
2071a 30 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49 54  0);..  if( SQLIT
2071b 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0d 0a 20 20 20  E_OK==rc ){..   
2071c 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62   rc = clearDatab
2071d 61 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67  asePage(pBt, (Pg
2071e 6e 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e  no)iTable, 0, pn
2071f 43 68 61 6e 67 65 29 3b 0d 0a 20 20 7d 0d 0a 20  Change);..  }.. 
20720 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
20721 76 65 28 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e  ve(p);..  return
20722 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
20723 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f  * Erase all info
20724 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 62  rmation in a tab
20725 6c 65 20 61 6e 64 20 61 64 64 20 74 68 65 20 72  le and add the r
20726 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65  oot of the table
20727 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 66 72 65 65   to..** the free
20728 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74  list.  Except, t
20729 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70  he root of the p
2072a 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28  rinciple table (
2072b 74 68 65 20 6f 6e 65 20 6f 6e 0d 0a 2a 2a 20 70  the one on..** p
2072c 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20  age 1) is never 
2072d 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65  added to the fre
2072e 65 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  elist...**..** T
2072f 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
20730 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54   fail with SQLIT
20731 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72  E_LOCKED if ther
20732 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0d 0a  e are any open..
20733 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68  ** cursors on th
20734 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  e table...**..**
20735 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69   If AUTOVACUUM i
20736 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68  s enabled and th
20737 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c 65  e page at iTable
20738 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74   is not the last
20739 0d 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69  ..** root page i
2073a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
2073b 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61  ile, then the la
2073c 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0d 0a 2a  st root page ..*
2073d 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
2073e 65 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20  e file is moved 
2073f 69 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f  into the slot fo
20740 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20  rmerly occupied 
20741 62 79 0d 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e  by..** iTable an
20742 64 20 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74  d that last slot
20743 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69   formerly occupi
20744 65 64 20 62 79 20 74 68 65 20 6c 61 73 74 20 72  ed by the last r
20745 6f 6f 74 20 70 61 67 65 0d 0a 2a 2a 20 69 73 20  oot page..** is 
20746 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65  added to the fre
20747 65 6c 69 73 74 20 69 6e 73 74 65 61 64 20 6f 66  elist instead of
20748 20 69 54 61 62 6c 65 2e 20 20 49 6e 20 74 68 69   iTable.  In thi
20749 73 20 73 61 79 2c 20 61 6c 6c 0d 0a 2a 2a 20 72  s say, all..** r
2074a 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20 6b 65  oot pages are ke
2074b 70 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  pt at the beginn
2074c 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  ing of the datab
2074d 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 0d  ase file, which.
2074e 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79  .** is necessary
2074f 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d 20   for AUTOVACUUM 
20750 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20 20  to work right.  
20751 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20  *piMoved is set 
20752 74 6f 20 74 68 65 20 0d 0a 2a 2a 20 70 61 67 65  to the ..** page
20753 20 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65   number that use
20754 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74  d to be the last
20755 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68   root page in th
20756 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0d 0a 2a  e file before..*
20757 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20  * the move.  If 
20758 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76  no page gets mov
20759 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20  ed, *piMoved is 
2075a 73 65 74 20 74 6f 20 30 2e 0d 0a 2a 2a 20 54 68  set to 0...** Th
2075b 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65  e last root page
2075c 20 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e 20   is recorded in 
2075d 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 20  meta[3] and the 
2075e 76 61 6c 75 65 20 6f 66 0d 0a 2a 2a 20 6d 65 74  value of..** met
2075f 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 64 20  a[3] is updated 
20760 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  by this procedur
20761 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
20762 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c  nt btreeDropTabl
20763 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  e(Btree *p, Pgno
20764 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69   iTable, int *pi
20765 4d 6f 76 65 64 29 7b 0d 0a 20 20 69 6e 74 20 72  Moved){..  int r
20766 63 3b 0d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  c;..  MemPage *p
20767 50 61 67 65 20 3d 20 30 3b 0d 0a 20 20 42 74 53  Page = 0;..  BtS
20768 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
20769 70 42 74 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  pBt;....  assert
2076a 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
2076b 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0d 0a  ldsMutex(p) );..
2076c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54    assert( p->inT
2076d 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
2076e 45 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 74 20  E );....  /* It 
2076f 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72  is illegal to dr
20770 6f 70 20 61 20 74 61 62 6c 65 20 69 66 20 61 6e  op a table if an
20771 79 20 63 75 72 73 6f 72 73 20 61 72 65 20 6f 70  y cursors are op
20772 65 6e 20 6f 6e 20 74 68 65 0d 0a 20 20 2a 2a 20  en on the..  ** 
20773 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 69  database. This i
20774 73 20 62 65 63 61 75 73 65 20 69 6e 20 61 75 74  s because in aut
20775 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 68  o-vacuum mode th
20776 65 20 62 61 63 6b 65 6e 64 20 6d 61 79 0d 0a 20  e backend may.. 
20777 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f 76 65   ** need to move
20778 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d 70 61   another root-pa
20779 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67 61 70  ge to fill a gap
2077a 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c   left by the del
2077b 65 74 65 64 0d 0a 20 20 2a 2a 20 72 6f 6f 74 20  eted..  ** root 
2077c 70 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65 6e  page. If an open
2077d 20 63 75 72 73 6f 72 20 77 61 73 20 75 73 69 6e   cursor was usin
2077e 67 20 74 68 69 73 20 70 61 67 65 20 61 20 70 72  g this page a pr
2077f 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0d 0a 20 20  oblem would ..  
20780 2a 2a 20 6f 63 63 75 72 2e 0d 0a 20 20 2a 2a 0d  ** occur...  **.
20781 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72 6f 72  .  ** This error
20782 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e 67 20   is caught long 
20783 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c 20 72  before control r
20784 65 61 63 68 65 73 20 74 68 69 73 20 70 6f 69 6e  eaches this poin
20785 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  t...  */..  if( 
20786 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73  NEVER(pBt->pCurs
20787 6f 72 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  or) ){..    sqli
20788 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
20789 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d  cked(p->db, pBt-
2078a 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65  >pCursor->pBtree
2078b 2d 3e 64 62 29 3b 0d 0a 20 20 20 20 72 65 74 75  ->db);..    retu
2078c 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
2078d 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0d 0a 20  _SHAREDCACHE;.. 
2078e 20 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 62 74 72   }....  rc = btr
2078f 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28  eeGetPage(pBt, (
20790 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50  Pgno)iTable, &pP
20791 61 67 65 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20  age, 0);..  if( 
20792 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d  rc ) return rc;.
20793 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
20794 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70  treeClearTable(p
20795 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0d 0a 20  , iTable, 0);.. 
20796 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
20797 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
20798 65 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  e);..    return 
20799 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70  rc;..  }....  *p
2079a 69 4d 6f 76 65 64 20 3d 20 30 3b 0d 0a 0d 0a 20  iMoved = 0;.... 
2079b 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 7b   if( iTable>1 ){
2079c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2079d 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d  OMIT_AUTOVACUUM.
2079e 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50  .    freePage(pP
2079f 61 67 65 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  age, &rc);..    
207a0 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
207a1 65 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20  e);..#else..    
207a2 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
207a3 75 75 6d 20 29 7b 0d 0a 20 20 20 20 20 20 50 67  uum ){..      Pg
207a4 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0d  no maxRootPgno;.
207a5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
207a6 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54  reeGetMeta(p, BT
207a7 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54  REE_LARGEST_ROOT
207a8 5f 50 41 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50  _PAGE, &maxRootP
207a9 67 6e 6f 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 69  gno);....      i
207aa 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f  f( iTable==maxRo
207ab 6f 74 50 67 6e 6f 20 29 7b 0d 0a 20 20 20 20 20  otPgno ){..     
207ac 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62     /* If the tab
207ad 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64  le being dropped
207ae 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 69   is the table wi
207af 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 72  th the largest r
207b0 6f 6f 74 2d 70 61 67 65 0d 0a 20 20 20 20 20 20  oot-page..      
207b1 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74    ** number in t
207b2 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 75 74  he database, put
207b3 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
207b4 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e  n the free list.
207b5 20 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20   ..        */.. 
207b6 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
207b7 70 50 61 67 65 2c 20 26 72 63 29 3b 0d 0a 20 20  pPage, &rc);..  
207b8 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
207b9 65 28 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  e(pPage);..     
207ba 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
207bb 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
207bc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
207bd 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
207be 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
207bf 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  /* The table bei
207c0 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20  ng dropped does 
207c1 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61 72  not have the lar
207c2 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0d 0a  gest root-page..
207c3 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
207c4 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
207c5 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70  e. So move the p
207c6 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e  age that does in
207c7 74 6f 20 74 68 65 20 0d 0a 20 20 20 20 20 20 20  to the ..       
207c8 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20   ** gap left by 
207c9 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74  the deleted root
207ca 2d 70 61 67 65 2e 0d 0a 20 20 20 20 20 20 20 20  -page...        
207cb 2a 2f 0d 0a 20 20 20 20 20 20 20 20 4d 65 6d 50  */..        MemP
207cc 61 67 65 20 2a 70 4d 6f 76 65 3b 0d 0a 20 20 20  age *pMove;..   
207cd 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
207ce 28 70 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20  (pPage);..      
207cf 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
207d0 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  age(pBt, maxRoot
207d1 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29  Pgno, &pMove, 0)
207d2 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
207d3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
207d4 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
207d5 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d  n rc;..        }
207d6 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  ..        rc = r
207d7 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
207d8 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52   pMove, PTRMAP_R
207d9 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62  OOTPAGE, 0, iTab
207da 6c 65 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  le, 0);..       
207db 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f   releasePage(pMo
207dc 76 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ve);..        if
207dd 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
207de 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65  ){..          re
207df 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20  turn rc;..      
207e0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 70 4d 6f    }..        pMo
207e1 76 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  ve = 0;..       
207e2 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
207e3 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50  ge(pBt, maxRootP
207e4 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b  gno, &pMove, 0);
207e5 0d 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 61  ..        freePa
207e6 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0d  ge(pMove, &rc);.
207e7 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
207e8 50 61 67 65 28 70 4d 6f 76 65 29 3b 0d 0a 20 20  Page(pMove);..  
207e9 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
207ea 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
207eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
207ec 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
207ed 20 20 20 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20       *piMoved = 
207ee 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0d 0a 20 20  maxRootPgno;..  
207ef 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f      }....      /
207f0 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 6d  * Set the new 'm
207f1 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 61  ax-root-page' va
207f2 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  lue in the datab
207f3 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 73  ase header. This
207f4 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68  ..      ** is th
207f5 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73  e old value less
207f6 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d   one, less one m
207f7 6f 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70  ore if that happ
207f8 65 6e 73 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a  ens to..      **
207f9 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20   be a root-page 
207fa 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65  number, less one
207fb 20 61 67 61 69 6e 20 69 66 20 74 68 61 74 20 69   again if that i
207fc 73 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  s the..      ** 
207fd 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
207fe 45 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  E...      */..  
207ff 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
20800 2d 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28  -;..      while(
20801 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45   maxRootPgno==PE
20802 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
20803 70 42 74 29 0d 0a 20 20 20 20 20 20 20 20 20 20  pBt)..          
20804 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50     || PTRMAP_ISP
20805 41 47 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  AGE(pBt, maxRoot
20806 50 67 6e 6f 29 20 29 7b 0d 0a 20 20 20 20 20 20  Pgno) ){..      
20807 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b    maxRootPgno--;
20808 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
20809 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74   assert( maxRoot
2080a 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
2080b 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0d  TE_PAGE(pBt) );.
2080c 0a 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ...      rc = sq
2080d 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
2080e 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f  Meta(p, 4, maxRo
2080f 6f 74 50 67 6e 6f 29 3b 0d 0a 20 20 20 20 7d 65  otPgno);..    }e
20810 6c 73 65 7b 0d 0a 20 20 20 20 20 20 66 72 65 65  lse{..      free
20811 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29  Page(pPage, &rc)
20812 3b 0d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ;..      release
20813 50 61 67 65 28 70 50 61 67 65 29 3b 0d 0a 20 20  Page(pPage);..  
20814 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d    }..#endif..  }
20815 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49 66  else{..    /* If
20816 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
20817 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65  pTable was calle
20818 64 20 6f 6e 20 70 61 67 65 20 31 2e 0d 0a 20 20  d on page 1...  
20819 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c 79    ** This really
2081a 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68 61   never should ha
2081b 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20 61  ppen except in a
2081c 20 63 6f 72 72 75 70 74 0d 0a 20 20 20 20 2a 2a   corrupt..    **
2081d 20 64 61 74 61 62 61 73 65 2e 20 0d 0a 20 20 20   database. ..   
2081e 20 2a 2f 0d 0a 20 20 20 20 7a 65 72 6f 50 61 67   */..    zeroPag
2081f 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54  e(pPage, PTF_INT
20820 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0d  KEY|PTF_LEAF );.
20821 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
20822 28 70 50 61 67 65 29 3b 0d 0a 20 20 7d 0d 0a 20  (pPage);..  }.. 
20823 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0d 0a 7d   return rc;  ..}
20824 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
20825 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
20826 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65  eDropTable(Btree
20827 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c   *p, int iTable,
20828 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0d   int *piMoved){.
20829 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73 71  .  int rc;..  sq
2082a 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
2082b 70 29 3b 0d 0a 20 20 72 63 20 3d 20 62 74 72 65  p);..  rc = btre
2082c 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20 69 54  eDropTable(p, iT
2082d 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29 3b 0d  able, piMoved);.
2082e 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
2082f 65 61 76 65 28 70 29 3b 0d 0a 20 20 72 65 74 75  eave(p);..  retu
20830 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  rn rc;..}....../
20831 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
20832 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  ion may only be 
20833 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d  called if the b-
20834 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  tree connection 
20835 61 6c 72 65 61 64 79 0d 0a 2a 2a 20 68 61 73 20  already..** has 
20836 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  a read or write 
20837 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
20838 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
20839 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 61 64 20 74  ...**..** Read t
2083a 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
2083b 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74  ion out of a dat
2083c 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74  abase file.  Met
2083d 61 5b 30 5d 0d 0a 2a 2a 20 69 73 20 74 68 65 20  a[0]..** is the 
2083e 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70  number of free p
2083f 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ages currently i
20840 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
20841 20 4d 65 74 61 5b 31 5d 0d 0a 2a 2a 20 74 68 72   Meta[1]..** thr
20842 6f 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72  ough meta[15] ar
20843 65 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  e available for 
20844 75 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61  use by higher la
20845 79 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0d 0a  yers.  Meta[0]..
20846 2a 2a 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c  ** is read-only,
20847 20 74 68 65 20 6f 74 68 65 72 73 20 61 72 65 20   the others are 
20848 72 65 61 64 2f 77 72 69 74 65 2e 0d 0a 2a 2a 20  read/write...** 
20849 0d 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20  ..** The schema 
2084a 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 20 6d 65  layer numbers me
2084b 74 61 20 76 61 6c 75 65 73 20 64 69 66 66 65 72  ta values differ
2084c 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 65 20 73  ently.  At the s
2084d 63 68 65 6d 61 0d 0a 2a 2a 20 6c 61 79 65 72 20  chema..** layer 
2084e 28 61 6e 64 20 74 68 65 20 53 65 74 43 6f 6f 6b  (and the SetCook
2084f 69 65 20 61 6e 64 20 52 65 61 64 43 6f 6f 6b 69  ie and ReadCooki
20850 65 20 6f 70 63 6f 64 65 73 29 20 74 68 65 20 6e  e opcodes) the n
20851 75 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 66 72 65  umber of..** fre
20852 65 20 70 61 67 65 73 20 69 73 20 6e 6f 74 20 76  e pages is not v
20853 69 73 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f 6b  isible.  So Cook
20854 69 65 5b 30 5d 20 69 73 20 74 68 65 20 73 61 6d  ie[0] is the sam
20855 65 20 61 73 20 4d 65 74 61 5b 31 5d 2e 0d 0a 2a  e as Meta[1]...*
20856 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
20857 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
20858 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65  reeGetMeta(Btree
20859 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33   *p, int idx, u3
2085a 32 20 2a 70 4d 65 74 61 29 7b 0d 0a 20 20 42 74  2 *pMeta){..  Bt
2085b 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
2085c 3e 70 42 74 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74  >pBt;....  sqlit
2085d 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
2085e 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ..  assert( p->i
2085f 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e  nTrans>TRANS_NON
20860 45 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  E );..  assert( 
20861 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79  SQLITE_OK==query
20862 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
20863 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52  Lock(p, MASTER_R
20864 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20  OOT, READ_LOCK) 
20865 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 42  );..  assert( pB
20866 74 2d 3e 70 50 61 67 65 31 20 29 3b 0d 0a 20 20  t->pPage1 );..  
20867 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26  assert( idx>=0 &
20868 26 20 69 64 78 3c 3d 31 35 20 29 3b 0d 0a 0d 0a  & idx<=15 );....
20869 20 20 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62    *pMeta = get4b
2086a 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
2086b 2d 3e 61 44 61 74 61 5b 33 36 20 2b 20 69 64 78  ->aData[36 + idx
2086c 2a 34 5d 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  *4]);....  /* If
2086d 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20   auto-vacuum is 
2086e 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 69 73  disabled in this
2086f 20 62 75 69 6c 64 20 61 6e 64 20 74 68 69 73 20   build and this 
20870 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  is an auto-vacuu
20871 6d 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  m..  ** database
20872 2c 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61 62  , mark the datab
20873 61 73 65 20 61 73 20 72 65 61 64 2d 6f 6e 6c 79  ase as read-only
20874 2e 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  .  */..#ifdef SQ
20875 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
20876 43 55 55 4d 0d 0a 20 20 69 66 28 20 69 64 78 3d  CUUM..  if( idx=
20877 3d 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52  =BTREE_LARGEST_R
20878 4f 4f 54 5f 50 41 47 45 20 26 26 20 2a 70 4d 65  OOT_PAGE && *pMe
20879 74 61 3e 30 20 29 7b 0d 0a 20 20 20 20 70 42 74  ta>0 ){..    pBt
2087a 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54  ->btsFlags |= BT
2087b 53 5f 52 45 41 44 5f 4f 4e 4c 59 3b 0d 0a 20 20  S_READ_ONLY;..  
2087c 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 73  }..#endif....  s
2087d 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
2087e 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  (p);..}..../*..*
2087f 2a 20 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66  * Write meta-inf
20880 6f 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e  ormation back in
20881 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
20882 20 20 4d 65 74 61 5b 30 5d 20 69 73 0d 0a 2a 2a    Meta[0] is..**
20883 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d   read-only and m
20884 61 79 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65  ay not be writte
20885 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  n...*/..SQLITE_P
20886 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20887 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
20888 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  a(Btree *p, int 
20889 69 64 78 2c 20 75 33 32 20 69 4d 65 74 61 29 7b  idx, u32 iMeta){
2088a 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ..  BtShared *pB
2088b 74 20 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 75  t = p->pBt;..  u
2088c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50  nsigned char *pP
2088d 31 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  1;..  int rc;.. 
2088e 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20   assert( idx>=1 
2088f 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0d 0a 20  && idx<=15 );.. 
20890 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
20891 65 72 28 70 29 3b 0d 0a 20 20 61 73 73 65 72 74  er(p);..  assert
20892 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
20893 41 4e 53 5f 57 52 49 54 45 20 29 3b 0d 0a 20 20  ANS_WRITE );..  
20894 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
20895 67 65 31 21 3d 30 20 29 3b 0d 0a 20 20 70 50 31  ge1!=0 );..  pP1
20896 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e   = pBt->pPage1->
20897 61 44 61 74 61 3b 0d 0a 20 20 72 63 20 3d 20 73  aData;..  rc = s
20898 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
20899 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
2089a 62 50 61 67 65 29 3b 0d 0a 20 20 69 66 28 20 72  bPage);..  if( r
2089b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
2089c 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
2089d 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c 20  P1[36 + idx*4], 
2089e 69 4d 65 74 61 29 3b 0d 0a 23 69 66 6e 64 65 66  iMeta);..#ifndef
2089f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
208a0 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 69 66 28  OVACUUM..    if(
208a1 20 69 64 78 3d 3d 42 54 52 45 45 5f 49 4e 43 52   idx==BTREE_INCR
208a2 5f 56 41 43 55 55 4d 20 29 7b 0d 0a 20 20 20 20  _VACUUM ){..    
208a3 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61    assert( pBt->a
208a4 75 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65  utoVacuum || iMe
208a5 74 61 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ta==0 );..      
208a6 61 73 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30  assert( iMeta==0
208a7 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0d   || iMeta==1 );.
208a8 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72  .      pBt->incr
208a9 56 61 63 75 75 6d 20 3d 20 28 75 38 29 69 4d 65  Vacuum = (u8)iMe
208aa 74 61 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  ta;..    }..#end
208ab 69 66 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  if..  }..  sqlit
208ac 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
208ad 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
208ae 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  }....#ifndef SQL
208af 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
208b0 55 4e 54 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  UNT../*..** The 
208b1 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20  first argument, 
208b2 70 43 75 72 2c 20 69 73 20 61 20 63 75 72 73 6f  pCur, is a curso
208b3 72 20 6f 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65  r opened on some
208b4 20 62 2d 74 72 65 65 2e 20 43 6f 75 6e 74 20 74   b-tree. Count t
208b5 68 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  he..** number of
208b6 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
208b7 62 2d 74 72 65 65 20 61 6e 64 20 77 72 69 74 65  b-tree and write
208b8 20 74 68 65 20 72 65 73 75 6c 74 20 74 6f 20 2a   the result to *
208b9 70 6e 45 6e 74 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a  pnEntry...**..**
208ba 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
208bb 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 6f 70  turned if the op
208bc 65 72 61 74 69 6f 6e 20 69 73 20 73 75 63 63 65  eration is succe
208bd 73 73 66 75 6c 6c 79 20 65 78 65 63 75 74 65 64  ssfully executed
208be 2e 20 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  . ..** Otherwise
208bf 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73  , if an error is
208c0 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 69 2e   encountered (i.
208c1 65 2e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f  e. an IO error o
208c2 72 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63  r database..** c
208c3 6f 72 72 75 70 74 69 6f 6e 29 20 61 6e 20 53 51  orruption) an SQ
208c4 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
208c5 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f  is returned...*/
208c6 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
208c7 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
208c8 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20  eCount(BtCursor 
208c9 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e 45 6e  *pCur, i64 *pnEn
208ca 74 72 79 29 7b 0d 0a 20 20 69 36 34 20 6e 45 6e  try){..  i64 nEn
208cb 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20  try = 0;        
208cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
208cd 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e   Value to return
208ce 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f 0d   in *pnEntry */.
208cf 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
208d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
208d1 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
208d2 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 69  n code */....  i
208d3 66 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  f( pCur->pgnoRoo
208d4 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70 6e  t==0 ){..    *pn
208d5 45 6e 74 72 79 20 3d 20 30 3b 0d 0a 20 20 20 20  Entry = 0;..    
208d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
208d7 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 6d  ;..  }..  rc = m
208d8 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
208d9 0d 0a 0d 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20  ....  /* Unless 
208da 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
208db 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   the following l
208dc 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65  oop runs one ite
208dd 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0d  ration for each.
208de 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68  .  ** page in th
208df 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75  e B-Tree structu
208e0 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e  re (not includin
208e1 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  g overflow pages
208e2 29 2e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 77 68 69  ). ..  */..  whi
208e3 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
208e4 4b 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 49  K ){..    int iI
208e5 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
208e6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
208e7 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e  Index of child n
208e8 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f  ode in parent */
208e9 0d 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  ..    MemPage *p
208ea 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
208eb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
208ec 65 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ent page of the 
208ed 62 2d 74 72 65 65 20 2a 2f 0d 0a 0d 0a 20 20 20  b-tree */....   
208ee 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
208ef 20 6c 65 61 66 20 70 61 67 65 20 6f 72 20 74 68   leaf page or th
208f0 65 20 74 72 65 65 20 69 73 20 6e 6f 74 20 61 6e  e tree is not an
208f1 20 69 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 74   int-key tree, t
208f2 68 65 6e 20 0d 0a 20 20 20 20 2a 2a 20 74 68 69  hen ..    ** thi
208f3 73 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  s page contains 
208f4 63 6f 75 6e 74 61 62 6c 65 20 65 6e 74 72 69 65  countable entrie
208f5 73 2e 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  s. Increment the
208f6 20 65 6e 74 72 79 20 63 6f 75 6e 74 65 72 0d 0a   entry counter..
208f7 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67      ** according
208f8 6c 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ly...    */..   
208f9 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
208fa 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
208fb 65 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70 50 61  e];..    if( pPa
208fc 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61  ge->leaf || !pPa
208fd 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0d 0a 20  ge->intKey ){.. 
208fe 20 20 20 20 20 6e 45 6e 74 72 79 20 2b 3d 20 70       nEntry += p
208ff 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0d 0a 20 20  Page->nCell;..  
20900 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 70 50    }....    /* pP
20901 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  age is a leaf no
20902 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61  de. This loop na
20903 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73  vigates the curs
20904 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0d 0a  or so that it ..
20905 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f      ** points to
20906 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72   the first inter
20907 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74  ior cell that it
20908 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70   points to the p
20909 61 72 65 6e 74 20 6f 66 0d 0a 20 20 20 20 2a 2a  arent of..    **
2090a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
2090b 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20  n the tree that 
2090c 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
2090d 20 76 69 73 69 74 65 64 2e 20 54 68 65 0d 0a 20   visited. The.. 
2090e 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64     ** pCur->aiId
2090f 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76  x[pCur->iPage] v
20910 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 74  alue is set to t
20911 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
20912 70 61 72 65 6e 74 20 63 65 6c 6c 0d 0a 20 20 20  parent cell..   
20913 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c   ** of the page,
20914 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65   or to the numbe
20915 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68  r of cells in th
20916 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65  e page if the ne
20917 78 74 20 70 61 67 65 0d 0a 20 20 20 20 2a 2a 20  xt page..    ** 
20918 74 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20  to visit is the 
20919 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69  right-child of i
2091a 74 73 20 70 61 72 65 6e 74 2e 0d 0a 20 20 20 20  ts parent...    
2091b 2a 2a 0d 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c  **..    ** If al
2091c 6c 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 74  l pages in the t
2091d 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69  ree have been vi
2091e 73 69 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51  sited, return SQ
2091f 4c 49 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0d 0a  LITE_OK to the..
20920 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 0d 0a      ** caller...
20921 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
20922 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0d 0a  pPage->leaf ){..
20923 20 20 20 20 20 20 64 6f 20 7b 0d 0a 20 20 20 20        do {..    
20924 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50      if( pCur->iP
20925 61 67 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  age==0 ){..     
20926 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67 65       /* All page
20927 73 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20  s of the b-tree 
20928 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65  have been visite
20929 64 2e 20 52 65 74 75 72 6e 20 73 75 63 63 65 73  d. Return succes
2092a 73 66 75 6c 6c 79 2e 20 2a 2f 0d 0a 20 20 20 20  sfully. */..    
2092b 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 20 3d        *pnEntry =
2092c 20 6e 45 6e 74 72 79 3b 0d 0a 20 20 20 20 20 20   nEntry;..      
2092d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2092e 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 7d  E_OK;..        }
2092f 0d 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f  ..        moveTo
20930 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0d 0a 20  Parent(pCur);.. 
20931 20 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43       }while ( pC
20932 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
20933 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70  iPage]>=pCur->ap
20934 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
20935 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0d 0a 0d 0a 20  ]->nCell );.... 
20936 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
20937 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b  [pCur->iPage]++;
20938 0d 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20  ..      pPage = 
20939 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
2093a 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a 20 20 20 20  r->iPage];..    
2093b 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 44 65 73 63  }....    /* Desc
2093c 65 6e 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64  end to the child
2093d 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 63 65 6c   node of the cel
2093e 6c 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  l that the curso
2093f 72 20 63 75 72 72 65 6e 74 6c 79 20 0d 0a 20 20  r currently ..  
20940 20 20 2a 2a 20 70 6f 69 6e 74 73 20 61 74 2e 20    ** points at. 
20941 54 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68  This is the righ
20942 74 2d 63 68 69 6c 64 20 69 66 20 28 69 49 64 78  t-child if (iIdx
20943 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 2e  ==pPage->nCell).
20944 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 49  ..    */..    iI
20945 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78  dx = pCur->aiIdx
20946 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0d 0a  [pCur->iPage];..
20947 20 20 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50      if( iIdx==pP
20948 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0d 0a 20  age->nCell ){.. 
20949 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
2094a 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34  Child(pCur, get4
2094b 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
2094c 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
2094d 73 65 74 2b 38 5d 29 29 3b 0d 0a 20 20 20 20 7d  set+8]));..    }
2094e 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20  else{..      rc 
2094f 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
20950 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e  ur, get4byte(fin
20951 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 64  dCell(pPage, iId
20952 78 29 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  x)));..    }..  
20953 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 6e 20 65 72 72  }....  /* An err
20954 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  or has occurred.
20955 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
20956 20 63 6f 64 65 2e 20 2a 2f 0d 0a 20 20 72 65 74   code. */..  ret
20957 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 23 65 6e 64  urn rc;..}..#end
20958 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  if..../*..** Ret
20959 75 72 6e 20 74 68 65 20 70 61 67 65 72 20 61 73  urn the pager as
2095a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2095b 42 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75  BTree.  This rou
2095c 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
2095d 0d 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64  ..** testing and
2095e 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
2095f 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
20960 56 41 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69  VATE Pager *sqli
20961 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74  te3BtreePager(Bt
20962 72 65 65 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75  ree *p){..  retu
20963 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  rn p->pBt->pPage
20964 72 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66  r;..}....#ifndef
20965 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
20966 45 47 52 49 54 59 5f 43 48 45 43 4b 0d 0a 2f 2a  EGRITY_CHECK../*
20967 0d 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65  ..** Append a me
20968 73 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 72  ssage to the err
20969 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e  or message strin
2096a 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  g...*/..static v
2096b 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d  oid checkAppendM
2096c 73 67 28 0d 0a 20 20 49 6e 74 65 67 72 69 74 79  sg(..  Integrity
2096d 43 6b 20 2a 70 43 68 65 63 6b 2c 0d 0a 20 20 63  Ck *pCheck,..  c
2096e 68 61 72 20 2a 7a 4d 73 67 31 2c 0d 0a 20 20 63  har *zMsg1,..  c
2096f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
20970 61 74 2c 0d 0a 20 20 2e 2e 2e 0d 0a 29 7b 0d 0a  at,..  .....){..
20971 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20    va_list ap;.. 
20972 20 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78   if( !pCheck->mx
20973 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  Err ) return;.. 
20974 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d   pCheck->mxErr--
20975 3b 0d 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72  ;..  pCheck->nEr
20976 72 2b 2b 3b 0d 0a 20 20 76 61 5f 73 74 61 72 74  r++;..  va_start
20977 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d 0a  (ap, zFormat);..
20978 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72    if( pCheck->er
20979 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0d 0a 20  rMsg.nChar ){.. 
2097a 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
2097b 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b  umAppend(&pCheck
2097c 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20  ->errMsg, "\n", 
2097d 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  1);..  }..  if( 
2097e 7a 4d 73 67 31 20 29 7b 0d 0a 20 20 20 20 73 71  zMsg1 ){..    sq
2097f 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
20980 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72  end(&pCheck->err
20981 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b  Msg, zMsg1, -1);
20982 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
20983 56 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b  VXPrintf(&pCheck
20984 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f  ->errMsg, 1, zFo
20985 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76 61  rmat, ap);..  va
20986 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 69 66 28  _end(ap);..  if(
20987 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e   pCheck->errMsg.
20988 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
20989 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c  .    pCheck->mal
2098a 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a  locFailed = 1;..
2098b 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f    }..}..#endif /
2098c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
2098d 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f  TEGRITY_CHECK */
2098e 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
2098f 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
20990 59 5f 43 48 45 43 4b 0d 0a 2f 2a 0d 0a 2a 2a 20  Y_CHECK../*..** 
20991 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66  Add 1 to the ref
20992 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72  erence count for
20993 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66   page iPage.  If
20994 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63   this is the sec
20995 6f 6e 64 0d 0a 2a 2a 20 72 65 66 65 72 65 6e 63  ond..** referenc
20996 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61  e to the page, a
20997 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  dd an error mess
20998 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a  age to pCheck->z
20999 45 72 72 4d 73 67 2e 0d 0a 2a 2a 20 52 65 74 75  ErrMsg...** Retu
2099a 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72  rn 1 if there ar
2099b 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66  e 2 ore more ref
2099c 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
2099d 61 67 65 20 61 6e 64 20 30 20 69 66 0d 0a 2a 2a  age and 0 if..**
2099e 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
2099f 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20  first reference 
209a0 74 6f 20 74 68 65 20 70 61 67 65 2e 0d 0a 2a 2a  to the page...**
209a1 0d 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20  ..** Also check 
209a2 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75  that the page nu
209a3 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64  mber is in bound
209a4 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
209a5 6e 74 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65  nt checkRef(Inte
209a6 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c  grityCk *pCheck,
209a7 20 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61   Pgno iPage, cha
209a8 72 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0d 0a 20  r *zContext){.. 
209a9 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
209aa 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 69 66 28  return 1;..  if(
209ab 20 69 50 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e   iPage>pCheck->n
209ac 50 61 67 65 20 29 7b 0d 0a 20 20 20 20 63 68 65  Page ){..    che
209ad 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
209ae 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 69  ck, zContext, "i
209af 6e 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62  nvalid page numb
209b0 65 72 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0d  er %d", iPage);.
209b1 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a  .    return 1;..
209b2 20 20 7d 0d 0a 20 20 69 66 28 20 70 43 68 65 63    }..  if( pChec
209b3 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d  k->anRef[iPage]=
209b4 3d 31 20 29 7b 0d 0a 20 20 20 20 63 68 65 63 6b  =1 ){..    check
209b5 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
209b6 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64  , zContext, "2nd
209b7 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   reference to pa
209b8 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0d  ge %d", iPage);.
209b9 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a  .    return 1;..
209ba 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 20 28    }..  return  (
209bb 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
209bc 61 67 65 5d 2b 2b 29 3e 31 3b 0d 0a 7d 0d 0a 0d  age]++)>1;..}...
209bd 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
209be 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d  OMIT_AUTOVACUUM.
209bf 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ./*..** Check th
209c0 61 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20  at the entry in 
209c1 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
209c2 66 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20  for page iChild 
209c3 6d 61 70 73 20 74 6f 20 0d 0a 2a 2a 20 70 61 67  maps to ..** pag
209c4 65 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74  e iParent, point
209c5 65 72 20 74 79 70 65 20 70 74 72 54 79 70 65 2e  er type ptrType.
209c6 20 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20   If not, append 
209c7 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
209c8 0d 0a 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0d  ..** to pCheck..
209c9 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
209ca 20 63 68 65 63 6b 50 74 72 6d 61 70 28 0d 0a 20   checkPtrmap(.. 
209cb 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43   IntegrityCk *pC
209cc 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67  heck,   /* Integ
209cd 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e 74 65  rity check conte
209ce 78 74 20 2a 2f 0d 0a 20 20 50 67 6e 6f 20 69 43  xt */..  Pgno iC
209cf 68 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  hild,           
209d0 2f 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e 75  /* Child page nu
209d1 6d 62 65 72 20 2a 2f 0d 0a 20 20 75 38 20 65 54  mber */..  u8 eT
209d2 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
209d3 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f    /* Expected po
209d4 69 6e 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a  inter map type *
209d5 2f 0d 0a 20 20 50 67 6e 6f 20 69 50 61 72 65 6e  /..  Pgno iParen
209d6 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  t,          /* E
209d7 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20  xpected pointer 
209d8 6d 61 70 20 70 61 72 65 6e 74 20 70 61 67 65 20  map parent page 
209d9 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 63 68 61  number */..  cha
209da 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20  r *zContext     
209db 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 64      /* Context d
209dc 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 65 64  escription (used
209dd 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 29 20   for error msg) 
209de 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
209df 3b 0d 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54  ;..  u8 ePtrmapT
209e0 79 70 65 3b 0d 0a 20 20 50 67 6e 6f 20 69 50 74  ype;..  Pgno iPt
209e1 72 6d 61 70 50 61 72 65 6e 74 3b 0d 0a 0d 0a 20  rmapParent;.... 
209e2 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
209e3 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68  pCheck->pBt, iCh
209e4 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70  ild, &ePtrmapTyp
209e5 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e  e, &iPtrmapParen
209e6 74 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53  t);..  if( rc!=S
209e7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
209e8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
209e9 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
209ea 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
209eb 29 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63  ) pCheck->malloc
209ec 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20  Failed = 1;..   
209ed 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
209ee 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
209ef 2c 20 22 46 61 69 6c 65 64 20 74 6f 20 72 65 61  , "Failed to rea
209f0 64 20 70 74 72 6d 61 70 20 6b 65 79 3d 25 64 22  d ptrmap key=%d"
209f1 2c 20 69 43 68 69 6c 64 29 3b 0d 0a 20 20 20 20  , iChild);..    
209f2 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a  return;..  }....
209f3 20 20 69 66 28 20 65 50 74 72 6d 61 70 54 79 70    if( ePtrmapTyp
209f4 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50 74 72  e!=eType || iPtr
209f5 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61 72 65  mapParent!=iPare
209f6 6e 74 20 29 7b 0d 0a 20 20 20 20 63 68 65 63 6b  nt ){..    check
209f7 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
209f8 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0d 0a 20 20  , zContext, ..  
209f9 20 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70      "Bad ptr map
209fa 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78   entry key=%d ex
209fb 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67  pected=(%d,%d) g
209fc 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0d 0a 20  ot=(%d,%d)", .. 
209fd 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79       iChild, eTy
209fe 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74  pe, iParent, ePt
209ff 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61  rmapType, iPtrma
20a00 70 50 61 72 65 6e 74 29 3b 0d 0a 20 20 7d 0d 0a  pParent);..  }..
20a01 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  }..#endif..../*.
20a02 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e  .** Check the in
20a03 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66  tegrity of the f
20a04 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e  reelist or of an
20a05 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c   overflow page l
20a06 69 73 74 2e 0d 0a 2a 2a 20 56 65 72 69 66 79 20  ist...** Verify 
20a07 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
20a08 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  of pages on the 
20a09 6c 69 73 74 20 69 73 20 4e 2e 0d 0a 2a 2f 0d 0a  list is N...*/..
20a0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
20a0b 6b 4c 69 73 74 28 0d 0a 20 20 49 6e 74 65 67 72  kList(..  Integr
20a0c 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
20a0d 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65  /* Integrity che
20a0e 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  cking context */
20a0f 0d 0a 20 20 69 6e 74 20 69 73 46 72 65 65 4c 69  ..  int isFreeLi
20a10 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75  st,       /* Tru
20a11 65 20 66 6f 72 20 61 20 66 72 65 65 6c 69 73 74  e for a freelist
20a12 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 6f 76 65  .  False for ove
20a13 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 20  rflow page list 
20a14 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 61 67 65 2c  */..  int iPage,
20a15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
20a16 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 66  age number for f
20a17 69 72 73 74 20 70 61 67 65 20 69 6e 20 74 68 65  irst page in the
20a18 20 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20   list */..  int 
20a19 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N,              
20a1a 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 6e 75    /* Expected nu
20a1b 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
20a1c 20 74 68 65 20 6c 69 73 74 20 2a 2f 0d 0a 20 20   the list */..  
20a1d 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
20a1e 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
20a1f 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
20a20 67 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ges */..){..  in
20a21 74 20 69 3b 0d 0a 20 20 69 6e 74 20 65 78 70 65  t i;..  int expe
20a22 63 74 65 64 20 3d 20 4e 3b 0d 0a 20 20 69 6e 74  cted = N;..  int
20a23 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b   iFirst = iPage;
20a24 0d 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e  ..  while( N-- >
20a25 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78   0 && pCheck->mx
20a26 45 72 72 20 29 7b 0d 0a 20 20 20 20 44 62 50 61  Err ){..    DbPa
20a27 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0d 0a  ge *pOvflPage;..
20a28 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
20a29 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0d 0a 20  r *pOvflData;.. 
20a2a 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29     if( iPage<1 )
20a2b 7b 0d 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  {..      checkAp
20a2c 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
20a2d 7a 43 6f 6e 74 65 78 74 2c 0d 0a 20 20 20 20 20  zContext,..     
20a2e 20 20 20 20 22 25 64 20 6f 66 20 25 64 20 70 61      "%d of %d pa
20a2f 67 65 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d  ges missing from
20a30 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73   overflow list s
20a31 74 61 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0d  tarting at %d",.
20a32 0a 20 20 20 20 20 20 20 20 20 20 4e 2b 31 2c 20  .          N+1, 
20a33 65 78 70 65 63 74 65 64 2c 20 69 46 69 72 73 74  expected, iFirst
20a34 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
20a35 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
20a36 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
20a37 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78  , iPage, zContex
20a38 74 29 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20  t) ) break;..   
20a39 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65   if( sqlite3Page
20a3a 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 50 61  rGet(pCheck->pPa
20a3b 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61 67 65  ger, (Pgno)iPage
20a3c 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20 29 7b  , &pOvflPage) ){
20a3d 0d 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  ..      checkApp
20a3e 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
20a3f 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65 64  Context, "failed
20a40 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64 22   to get page %d"
20a41 2c 20 69 50 61 67 65 29 3b 0d 0a 20 20 20 20 20  , iPage);..     
20a42 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
20a43 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20      pOvflData = 
20a44 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
20a45 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
20a46 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b  Data(pOvflPage);
20a47 0d 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65  ..    if( isFree
20a48 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 69  List ){..      i
20a49 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  nt n = get4byte(
20a4a 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0d  &pOvflData[4]);.
20a4b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20a4c 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d  OMIT_AUTOVACUUM.
20a4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
20a4e 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
20a4f 75 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63  um ){..        c
20a50 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
20a51 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50  k, iPage, PTRMAP
20a52 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43  _FREEPAGE, 0, zC
20a53 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20  ontext);..      
20a54 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20  }..#endif..     
20a55 20 69 66 28 20 6e 3e 28 69 6e 74 29 70 43 68 65   if( n>(int)pChe
20a56 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ck->pBt->usableS
20a57 69 7a 65 2f 34 2d 32 20 29 7b 0d 0a 20 20 20 20  ize/4-2 ){..    
20a58 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
20a59 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
20a5a 65 78 74 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  ext,..          
20a5b 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20   "freelist leaf 
20a5c 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e  count too big on
20a5d 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
20a5e 29 3b 0d 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b  );..        N--;
20a5f 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
20a60 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
20a61 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20   i<n; i++){..   
20a62 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65         Pgno iFre
20a63 65 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  ePage = get4byte
20a64 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a  (&pOvflData[8+i*
20a65 34 5d 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  4]);..#ifndef SQ
20a66 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
20a67 43 55 55 4d 0d 0a 20 20 20 20 20 20 20 20 20 20  CUUM..          
20a68 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d  if( pCheck->pBt-
20a69 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0d 0a  >autoVacuum ){..
20a6a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63              chec
20a6b 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
20a6c 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41  iFreePage, PTRMA
20a6d 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a  P_FREEPAGE, 0, z
20a6e 43 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20  Context);..     
20a6f 20 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a       }..#endif..
20a70 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b 52            checkR
20a71 65 66 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65  ef(pCheck, iFree
20a72 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
20a73 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
20a74 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0d 0a 20 20       N -= n;..  
20a75 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 23 69      }..    }..#i
20a76 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
20a77 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20  T_AUTOVACUUM..  
20a78 20 20 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2f    else{..      /
20a79 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61  * If this databa
20a7a 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
20a7b 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67  -vacuum and iPag
20a7c 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73  e is not the las
20a7d 74 0d 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65  t..      ** page
20a7e 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   in this overflo
20a7f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68  w list, check th
20a80 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  at the pointer-m
20a81 61 70 20 65 6e 74 72 79 20 66 6f 72 0d 0a 20 20  ap entry for..  
20a82 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f      ** the follo
20a83 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65  wing page matche
20a84 73 20 69 50 61 67 65 2e 0d 0a 20 20 20 20 20 20  s iPage...      
20a85 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70 43  */..      if( pC
20a86 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  heck->pBt->autoV
20a87 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0d  acuum && N>0 ){.
20a88 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74  .        i = get
20a89 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29  4byte(pOvflData)
20a8a 3b 0d 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ;..        check
20a8b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
20a8c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
20a8d 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  W2, iPage, zCont
20a8e 65 78 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ext);..      }..
20a8f 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20      }..#endif.. 
20a90 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62     iPage = get4b
20a91 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0d  yte(pOvflData);.
20a92 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
20a93 72 55 6e 72 65 66 28 70 4f 76 66 6c 50 61 67 65  rUnref(pOvflPage
20a94 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64  );..  }..}..#end
20a95 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
20a96 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
20a97 4b 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  K */....#ifndef 
20a98 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
20a99 47 52 49 54 59 5f 43 48 45 43 4b 0d 0a 2f 2a 0d  GRITY_CHECK../*.
20a9a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73  .** Do various s
20a9b 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20  anity checks on 
20a9c 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66  a single page of
20a9d 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e   a tree.  Return
20a9e 0d 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65  ..** the tree de
20a9f 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73  pth.  Root pages
20aa0 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65   return 0.  Pare
20aa1 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  nts of root page
20aa2 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20  s..** return 1, 
20aa3 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0d 0a 2a  and so forth...*
20aa4 2a 20 0d 0a 2a 2a 20 54 68 65 73 65 20 63 68 65  * ..** These che
20aa5 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0d 0a 2a  cks are done:..*
20aa6 2a 0d 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d  *..**      1.  M
20aa7 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 63 65  ake sure that ce
20aa8 6c 6c 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63  lls and freebloc
20aa9 6b 73 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61  ks do not overla
20aaa 70 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  p..**          b
20aab 75 74 20 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f  ut combine to co
20aac 6d 70 6c 65 74 65 6c 79 20 63 6f 76 65 72 20 74  mpletely cover t
20aad 68 65 20 70 61 67 65 2e 0d 0a 2a 2a 20 20 4e 4f  he page...**  NO
20aae 20 20 32 2e 20 20 4d 61 6b 65 20 73 75 72 65 20    2.  Make sure 
20aaf 63 65 6c 6c 20 6b 65 79 73 20 61 72 65 20 69 6e  cell keys are in
20ab0 20 6f 72 64 65 72 2e 0d 0a 2a 2a 20 20 4e 4f 20   order...**  NO 
20ab1 20 33 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e   3.  Make sure n
20ab2 6f 20 6b 65 79 20 69 73 20 6c 65 73 73 20 74 68  o key is less th
20ab3 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a  an or equal to z
20ab4 4c 6f 77 65 72 42 6f 75 6e 64 2e 0d 0a 2a 2a 20  LowerBound...** 
20ab5 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73 75   NO  4.  Make su
20ab6 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67 72 65  re no key is gre
20ab7 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
20ab8 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f 75 6e  al to zUpperBoun
20ab9 64 2e 0d 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20  d...**      5.  
20aba 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
20abb 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20  ity of overflow 
20abc 70 61 67 65 73 2e 0d 0a 2a 2a 20 20 20 20 20 20  pages...**      
20abd 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20  6.  Recursively 
20abe 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61  call checkTreePa
20abf 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72  ge on all childr
20ac0 65 6e 2e 0d 0a 2a 2a 20 20 20 20 20 20 37 2e 20  en...**      7. 
20ac1 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
20ac2 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68   depth of all ch
20ac3 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73 61  ildren is the sa
20ac4 6d 65 2e 0d 0a 2a 2a 20 20 20 20 20 20 38 2e 20  me...**      8. 
20ac5 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20   Make sure this 
20ac6 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 74  page is at least
20ac7 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73   33% full or els
20ac8 65 20 69 74 20 69 73 0d 0a 2a 2a 20 20 20 20 20  e it is..**     
20ac9 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66       the root of
20aca 20 74 68 65 20 74 72 65 65 2e 0d 0a 2a 2f 0d 0a   the tree...*/..
20acb 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
20acc 54 72 65 65 50 61 67 65 28 0d 0a 20 20 49 6e 74  TreePage(..  Int
20acd 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b  egrityCk *pCheck
20ace 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f  ,  /* Context fo
20acf 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65  r the sanity che
20ad0 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 61  ck */..  int iPa
20ad1 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ge,            /
20ad2 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * Page number of
20ad3 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65   the page to che
20ad4 63 6b 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  ck */..  char *z
20ad5 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 2c 20 2f  ParentContext, /
20ad6 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74  * Parent context
20ad7 20 2a 2f 0d 0a 20 20 69 36 34 20 2a 70 6e 50 61   */..  i64 *pnPa
20ad8 72 65 6e 74 4d 69 6e 4b 65 79 2c 20 0d 0a 20 20  rentMinKey, ..  
20ad9 69 36 34 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78  i64 *pnParentMax
20ada 4b 65 79 0d 0a 29 7b 0d 0a 20 20 4d 65 6d 50 61  Key..){..  MemPa
20adb 67 65 20 2a 70 50 61 67 65 3b 0d 0a 20 20 69 6e  ge *pPage;..  in
20adc 74 20 69 2c 20 72 63 2c 20 64 65 70 74 68 2c 20  t i, rc, depth, 
20add 64 32 2c 20 70 67 6e 6f 2c 20 63 6e 74 3b 0d 0a  d2, pgno, cnt;..
20ade 20 20 69 6e 74 20 68 64 72 2c 20 63 65 6c 6c 53    int hdr, cellS
20adf 74 61 72 74 3b 0d 0a 20 20 69 6e 74 20 6e 43 65  tart;..  int nCe
20ae0 6c 6c 3b 0d 0a 20 20 75 38 20 2a 64 61 74 61 3b  ll;..  u8 *data;
20ae1 0d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ..  BtShared *pB
20ae2 74 3b 0d 0a 20 20 69 6e 74 20 75 73 61 62 6c 65  t;..  int usable
20ae3 53 69 7a 65 3b 0d 0a 20 20 63 68 61 72 20 7a 43  Size;..  char zC
20ae4 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0d 0a 20 20  ontext[100];..  
20ae5 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0d 0a  char *hit = 0;..
20ae6 20 20 69 36 34 20 6e 4d 69 6e 4b 65 79 20 3d 20    i64 nMinKey = 
20ae7 30 3b 0d 0a 20 20 69 36 34 20 6e 4d 61 78 4b 65  0;..  i64 nMaxKe
20ae8 79 20 3d 20 30 3b 0d 0a 0d 0a 20 20 73 71 6c 69  y = 0;....  sqli
20ae9 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
20aea 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
20aeb 43 6f 6e 74 65 78 74 2c 20 22 50 61 67 65 20 25  Context, "Page %
20aec 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0d 0a 0d  d: ", iPage);...
20aed 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
20aee 20 74 68 65 20 70 61 67 65 20 65 78 69 73 74 73   the page exists
20aef 0d 0a 20 20 2a 2f 0d 0a 20 20 70 42 74 20 3d 20  ..  */..  pBt = 
20af0 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0d 0a 20 20  pCheck->pBt;..  
20af1 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74  usableSize = pBt
20af2 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0d 0a 20  ->usableSize;.. 
20af3 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
20af4 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 69 66 28  return 0;..  if(
20af5 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
20af6 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74  , iPage, zParent
20af7 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72  Context) ) retur
20af8 6e 20 30 3b 0d 0a 20 20 69 66 28 20 28 72 63 20  n 0;..  if( (rc 
20af9 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
20afa 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c  Bt, (Pgno)iPage,
20afb 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20   &pPage, 0))!=0 
20afc 29 7b 0d 0a 20 20 20 20 63 68 65 63 6b 41 70 70  ){..    checkApp
20afd 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
20afe 43 6f 6e 74 65 78 74 2c 0d 0a 20 20 20 20 20 20  Context,..      
20aff 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20   "unable to get 
20b00 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f 72 20  the page. error 
20b01 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b 0d 0a  code=%d", rc);..
20b02 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
20b03 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6c 65 61 72   }....  /* Clear
20b04 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20   MemPage.isInit 
20b05 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
20b06 20 63 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65   corruption dete
20b07 63 74 69 6f 6e 20 63 6f 64 65 20 69 6e 0d 0a 20  ction code in.. 
20b08 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67   ** btreeInitPag
20b09 65 28 29 20 69 73 20 65 78 65 63 75 74 65 64 2e  e() is executed.
20b0a 20 20 2a 2f 0d 0a 20 20 70 50 61 67 65 2d 3e 69    */..  pPage->i
20b0b 73 49 6e 69 74 20 3d 20 30 3b 0d 0a 20 20 69 66  sInit = 0;..  if
20b0c 28 20 28 72 63 20 3d 20 62 74 72 65 65 49 6e 69  ( (rc = btreeIni
20b0d 74 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30  tPage(pPage))!=0
20b0e 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
20b0f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52   rc==SQLITE_CORR
20b10 55 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f  UPT );  /* The o
20b11 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 72  nly possible err
20b12 6f 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 65  or from InitPage
20b13 20 2a 2f 0d 0a 20 20 20 20 63 68 65 63 6b 41 70   */..    checkAp
20b14 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
20b15 7a 43 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 20 20  zContext, ..    
20b16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
20b17 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
20b18 72 65 74 75 72 6e 73 20 65 72 72 6f 72 20 63 6f  returns error co
20b19 64 65 20 25 64 22 2c 20 72 63 29 3b 0d 0a 20 20  de %d", rc);..  
20b1a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
20b1b 61 67 65 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  age);..    retur
20b1c 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  n 0;..  }....  /
20b1d 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20  * Check out all 
20b1e 74 68 65 20 63 65 6c 6c 73 2e 0d 0a 20 20 2a 2f  the cells...  */
20b1f 0d 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0d 0a  ..  depth = 0;..
20b20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
20b21 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68  ge->nCell && pCh
20b22 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29  eck->mxErr; i++)
20b23 7b 0d 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c  {..    u8 *pCell
20b24 3b 0d 0a 20 20 20 20 75 33 32 20 73 7a 3b 0d 0a  ;..    u32 sz;..
20b25 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
20b26 6f 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 68 65  o;....    /* Che
20b27 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76 65 72 66  ck payload overf
20b28 6c 6f 77 20 70 61 67 65 73 0d 0a 20 20 20 20 2a  low pages..    *
20b29 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  /..    sqlite3_s
20b2a 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
20b2b 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65  Context), zConte
20b2c 78 74 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  xt,..           
20b2d 20 20 22 4f 6e 20 74 72 65 65 20 70 61 67 65 20    "On tree page 
20b2e 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69  %d cell %d: ", i
20b2f 50 61 67 65 2c 20 69 29 3b 0d 0a 20 20 20 20 70  Page, i);..    p
20b30 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
20b31 70 50 61 67 65 2c 69 29 3b 0d 0a 20 20 20 20 62  pPage,i);..    b
20b32 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
20b33 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
20b34 69 6e 66 6f 29 3b 0d 0a 20 20 20 20 73 7a 20 3d  info);..    sz =
20b35 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0d 0a 20 20   info.nData;..  
20b36 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e    if( !pPage->in
20b37 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e  tKey ) sz += (in
20b38 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0d 0a 20 20  t)info.nKey;..  
20b39 20 20 2f 2a 20 46 6f 72 20 69 6e 74 4b 65 79 20    /* For intKey 
20b3a 70 61 67 65 73 2c 20 63 68 65 63 6b 20 74 68 61  pages, check tha
20b3b 74 20 74 68 65 20 6b 65 79 73 20 61 72 65 20 69  t the keys are i
20b3c 6e 20 6f 72 64 65 72 2e 0d 0a 20 20 20 20 2a 2f  n order...    */
20b3d 0d 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 69  ..    else if( i
20b3e 3d 3d 30 20 29 20 6e 4d 69 6e 4b 65 79 20 3d 20  ==0 ) nMinKey = 
20b3f 6e 4d 61 78 4b 65 79 20 3d 20 69 6e 66 6f 2e 6e  nMaxKey = info.n
20b40 4b 65 79 3b 0d 0a 20 20 20 20 65 6c 73 65 7b 0d  Key;..    else{.
20b41 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e  .      if( info.
20b42 6e 4b 65 79 20 3c 3d 20 6e 4d 61 78 4b 65 79 20  nKey <= nMaxKey 
20b43 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 65 63  ){..        chec
20b44 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
20b45 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0d 0a 20  k, zContext, .. 
20b46 20 20 20 20 20 20 20 20 20 20 20 22 52 6f 77 69             "Rowi
20b47 64 20 25 6c 6c 64 20 6f 75 74 20 6f 66 20 6f 72  d %lld out of or
20b48 64 65 72 20 28 70 72 65 76 69 6f 75 73 20 77 61  der (previous wa
20b49 73 20 25 6c 6c 64 29 22 2c 20 69 6e 66 6f 2e 6e  s %lld)", info.n
20b4a 4b 65 79 2c 20 6e 4d 61 78 4b 65 79 29 3b 0d 0a  Key, nMaxKey);..
20b4b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 6e        }..      n
20b4c 4d 61 78 4b 65 79 20 3d 20 69 6e 66 6f 2e 6e 4b  MaxKey = info.nK
20b4d 65 79 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ey;..    }..    
20b4e 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f  assert( sz==info
20b4f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0d 0a 20 20  .nPayload );..  
20b50 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e    if( (sz>info.n
20b51 4c 6f 63 61 6c 29 20 0d 0a 20 20 20 20 20 26 26  Local) ..     &&
20b52 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f   (&pCell[info.iO
20b53 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65  verflow]<=&pPage
20b54 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61  ->aData[pBt->usa
20b55 62 6c 65 53 69 7a 65 5d 29 0d 0a 20 20 20 20 29  bleSize])..    )
20b56 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61  {..      int nPa
20b57 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e  ge = (sz - info.
20b58 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53  nLocal + usableS
20b59 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65  ize - 5)/(usable
20b5a 53 69 7a 65 20 2d 20 34 29 3b 0d 0a 20 20 20 20  Size - 4);..    
20b5b 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20    Pgno pgnoOvfl 
20b5c 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
20b5d 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
20b5e 5d 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ]);..#ifndef SQL
20b5f 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
20b60 55 55 4d 0d 0a 20 20 20 20 20 20 69 66 28 20 70  UUM..      if( p
20b61 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
20b62 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  {..        check
20b63 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
20b64 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f  gnoOvfl, PTRMAP_
20b65 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65  OVERFLOW1, iPage
20b66 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  , zContext);..  
20b67 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20      }..#endif.. 
20b68 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 28 70       checkList(p
20b69 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76  Check, 0, pgnoOv
20b6a 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74  fl, nPage, zCont
20b6b 65 78 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  ext);..    }....
20b6c 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e      /* Check san
20b6d 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c  ity of left chil
20b6e 64 20 70 61 67 65 2e 0d 0a 20 20 20 20 2a 2f 0d  d page...    */.
20b6f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
20b70 3e 6c 65 61 66 20 29 7b 0d 0a 20 20 20 20 20 20  >leaf ){..      
20b71 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
20b72 70 43 65 6c 6c 29 3b 0d 0a 23 69 66 6e 64 65 66  pCell);..#ifndef
20b73 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
20b74 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 20 20 69  OVACUUM..      i
20b75 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
20b76 75 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63  um ){..        c
20b77 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
20b78 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f  k, pgno, PTRMAP_
20b79 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43  BTREE, iPage, zC
20b7a 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20  ontext);..      
20b7b 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20  }..#endif..     
20b7c 20 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50   d2 = checkTreeP
20b7d 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f  age(pCheck, pgno
20b7e 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 26 6e 4d 69  , zContext, &nMi
20b7f 6e 4b 65 79 2c 20 69 3d 3d 30 20 3f 20 4e 55 4c  nKey, i==0 ? NUL
20b80 4c 20 3a 20 26 6e 4d 61 78 4b 65 79 29 3b 0d 0a  L : &nMaxKey);..
20b81 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 26 26        if( i>0 &&
20b82 20 64 32 21 3d 64 65 70 74 68 20 29 7b 0d 0a 20   d2!=depth ){.. 
20b83 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
20b84 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
20b85 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64 20 70  ontext, "Child p
20b86 61 67 65 20 64 65 70 74 68 20 64 69 66 66 65 72  age depth differ
20b87 73 22 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  s");..      }.. 
20b88 20 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b       depth = d2;
20b89 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
20b8a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
20b8b 61 66 20 29 7b 0d 0a 20 20 20 20 70 67 6e 6f 20  af ){..    pgno 
20b8c 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
20b8d 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
20b8e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0d 0a  hdrOffset+8]);..
20b8f 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
20b90 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e  intf(sizeof(zCon
20b91 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c  text), zContext,
20b92 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ..             
20b93 20 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65          "On page
20b94 20 25 64 20 61 74 20 72 69 67 68 74 20 63 68 69   %d at right chi
20b95 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0d 0a  ld: ", iPage);..
20b96 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20b97 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a  MIT_AUTOVACUUM..
20b98 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
20b99 6f 56 61 63 75 75 6d 20 29 7b 0d 0a 20 20 20 20  oVacuum ){..    
20b9a 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43    checkPtrmap(pC
20b9b 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d  heck, pgno, PTRM
20b9c 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c  AP_BTREE, iPage,
20b9d 20 7a 43 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20   zContext);..   
20b9e 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
20b9f 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43  checkTreePage(pC
20ba0 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e  heck, pgno, zCon
20ba1 74 65 78 74 2c 20 4e 55 4c 4c 2c 20 21 70 50 61  text, NULL, !pPa
20ba2 67 65 2d 3e 6e 43 65 6c 6c 20 3f 20 4e 55 4c 4c  ge->nCell ? NULL
20ba3 20 3a 20 26 6e 4d 61 78 4b 65 79 29 3b 0d 0a 20   : &nMaxKey);.. 
20ba4 20 7d 0d 0a 20 0d 0a 20 20 2f 2a 20 46 6f 72 20   }.. ..  /* For 
20ba5 69 6e 74 4b 65 79 20 6c 65 61 66 20 70 61 67 65  intKey leaf page
20ba6 73 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  s, check that th
20ba7 65 20 6d 69 6e 2f 6d 61 78 20 6b 65 79 73 20 61  e min/max keys a
20ba8 72 65 20 69 6e 20 6f 72 64 65 72 0d 0a 20 20 2a  re in order..  *
20ba9 2a 20 77 69 74 68 20 61 6e 79 20 6c 65 66 74 2f  * with any left/
20baa 70 61 72 65 6e 74 2f 72 69 67 68 74 20 70 61 67  parent/right pag
20bab 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  es...  */..  if(
20bac 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 20   pPage->leaf && 
20bad 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
20bae 0d 0a 20 20 20 20 2f 2a 20 69 66 20 77 65 20 61  ..    /* if we a
20baf 72 65 20 61 20 6c 65 66 74 20 63 68 69 6c 64 20  re a left child 
20bb0 70 61 67 65 20 2a 2f 0d 0a 20 20 20 20 69 66 28  page */..    if(
20bb1 20 70 6e 50 61 72 65 6e 74 4d 69 6e 4b 65 79 20   pnParentMinKey 
20bb2 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 69 66 20  ){..      /* if 
20bb3 77 65 20 61 72 65 20 74 68 65 20 6c 65 66 74 20  we are the left 
20bb4 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 20  most child page 
20bb5 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 21 70  */..      if( !p
20bb6 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 20 29 7b  nParentMaxKey ){
20bb7 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 4d  ..        if( nM
20bb8 61 78 4b 65 79 20 3e 20 2a 70 6e 50 61 72 65 6e  axKey > *pnParen
20bb9 74 4d 69 6e 4b 65 79 20 29 7b 0d 0a 20 20 20 20  tMinKey ){..    
20bba 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
20bbb 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
20bbc 6e 74 65 78 74 2c 20 0d 0a 20 20 20 20 20 20 20  ntext, ..       
20bbd 20 20 20 20 20 20 20 22 52 6f 77 69 64 20 25 6c         "Rowid %l
20bbe 6c 64 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 20  ld out of order 
20bbf 28 6d 61 78 20 6c 61 72 67 65 72 20 74 68 61 6e  (max larger than
20bc0 20 70 61 72 65 6e 74 20 6d 69 6e 20 6f 66 20 25   parent min of %
20bc1 6c 6c 64 29 22 2c 0d 0a 20 20 20 20 20 20 20 20  lld)",..        
20bc2 20 20 20 20 20 20 6e 4d 61 78 4b 65 79 2c 20 2a        nMaxKey, *
20bc3 70 6e 50 61 72 65 6e 74 4d 69 6e 4b 65 79 29 3b  pnParentMinKey);
20bc4 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
20bc5 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
20bc6 20 20 20 69 66 28 20 6e 4d 69 6e 4b 65 79 20 3c     if( nMinKey <
20bc7 3d 20 2a 70 6e 50 61 72 65 6e 74 4d 69 6e 4b 65  = *pnParentMinKe
20bc8 79 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  y ){..          
20bc9 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
20bca 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
20bcb 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ..             
20bcc 20 22 52 6f 77 69 64 20 25 6c 6c 64 20 6f 75 74   "Rowid %lld out
20bcd 20 6f 66 20 6f 72 64 65 72 20 28 6d 69 6e 20 6c   of order (min l
20bce 65 73 73 20 74 68 61 6e 20 70 61 72 65 6e 74 20  ess than parent 
20bcf 6d 69 6e 20 6f 66 20 25 6c 6c 64 29 22 2c 0d 0a  min of %lld)",..
20bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 4d                nM
20bd1 69 6e 4b 65 79 2c 20 2a 70 6e 50 61 72 65 6e 74  inKey, *pnParent
20bd2 4d 69 6e 4b 65 79 29 3b 0d 0a 20 20 20 20 20 20  MinKey);..      
20bd3 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
20bd4 20 6e 4d 61 78 4b 65 79 20 3e 20 2a 70 6e 50 61   nMaxKey > *pnPa
20bd5 72 65 6e 74 4d 61 78 4b 65 79 20 29 7b 0d 0a 20  rentMaxKey ){.. 
20bd6 20 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70           checkAp
20bd7 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
20bd8 7a 43 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 20 20  zContext, ..    
20bd9 20 20 20 20 20 20 20 20 20 20 22 52 6f 77 69 64            "Rowid
20bda 20 25 6c 6c 64 20 6f 75 74 20 6f 66 20 6f 72 64   %lld out of ord
20bdb 65 72 20 28 6d 61 78 20 6c 61 72 67 65 72 20 74  er (max larger t
20bdc 68 61 6e 20 70 61 72 65 6e 74 20 6d 61 78 20 6f  han parent max o
20bdd 66 20 25 6c 6c 64 29 22 2c 0d 0a 20 20 20 20 20  f %lld)",..     
20bde 20 20 20 20 20 20 20 20 20 6e 4d 61 78 4b 65 79           nMaxKey
20bdf 2c 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65  , *pnParentMaxKe
20be0 79 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  y);..        }..
20be1 20 20 20 20 20 20 20 20 2a 70 6e 50 61 72 65 6e          *pnParen
20be2 74 4d 69 6e 4b 65 79 20 3d 20 6e 4d 61 78 4b 65  tMinKey = nMaxKe
20be3 79 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  y;..      }..   
20be4 20 2f 2a 20 65 6c 73 65 20 69 66 20 77 65 27 72   /* else if we'r
20be5 65 20 61 20 72 69 67 68 74 20 63 68 69 6c 64 20  e a right child 
20be6 70 61 67 65 20 2a 2f 0d 0a 20 20 20 20 7d 20 65  page */..    } e
20be7 6c 73 65 20 69 66 28 20 70 6e 50 61 72 65 6e 74  lse if( pnParent
20be8 4d 61 78 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20  MaxKey ){..     
20be9 20 69 66 28 20 6e 4d 69 6e 4b 65 79 20 3c 3d 20   if( nMinKey <= 
20bea 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 20  *pnParentMaxKey 
20beb 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 65 63  ){..        chec
20bec 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
20bed 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0d 0a 20  k, zContext, .. 
20bee 20 20 20 20 20 20 20 20 20 20 20 22 52 6f 77 69             "Rowi
20bef 64 20 25 6c 6c 64 20 6f 75 74 20 6f 66 20 6f 72  d %lld out of or
20bf0 64 65 72 20 28 6d 69 6e 20 6c 65 73 73 20 74 68  der (min less th
20bf1 61 6e 20 70 61 72 65 6e 74 20 6d 61 78 20 6f 66  an parent max of
20bf2 20 25 6c 6c 64 29 22 2c 0d 0a 20 20 20 20 20 20   %lld)",..      
20bf3 20 20 20 20 20 20 6e 4d 69 6e 4b 65 79 2c 20 2a        nMinKey, *
20bf4 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 29 3b  pnParentMaxKey);
20bf5 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
20bf6 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68  ..  }....  /* Ch
20bf7 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65  eck for complete
20bf8 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65   coverage of the
20bf9 20 70 61 67 65 0d 0a 20 20 2a 2f 0d 0a 20 20 64   page..  */..  d
20bfa 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
20bfb 74 61 3b 0d 0a 20 20 68 64 72 20 3d 20 70 50 61  ta;..  hdr = pPa
20bfc 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0d 0a  ge->hdrOffset;..
20bfd 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50    hit = sqlite3P
20bfe 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e  ageMalloc( pBt->
20bff 70 61 67 65 53 69 7a 65 20 29 3b 0d 0a 20 20 69  pageSize );..  i
20c00 66 28 20 68 69 74 3d 3d 30 20 29 7b 0d 0a 20 20  f( hit==0 ){..  
20c01 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63    pCheck->malloc
20c02 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 7d  Failed = 1;..  }
20c03 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20 63  else{..    int c
20c04 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67  ontentOffset = g
20c05 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 26  et2byteNotZero(&
20c06 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0d 0a 20  data[hdr+5]);.. 
20c07 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e 74 65     assert( conte
20c08 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62 6c 65  ntOffset<=usable
20c09 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f  Size );  /* Enfo
20c0a 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69  rced by btreeIni
20c0b 74 50 61 67 65 28 29 20 2a 2f 0d 0a 20 20 20 20  tPage() */..    
20c0c 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 65  memset(hit+conte
20c0d 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 61  ntOffset, 0, usa
20c0e 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 4f  bleSize-contentO
20c0f 66 66 73 65 74 29 3b 0d 0a 20 20 20 20 6d 65 6d  ffset);..    mem
20c10 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74  set(hit, 1, cont
20c11 65 6e 74 4f 66 66 73 65 74 29 3b 0d 0a 20 20 20  entOffset);..   
20c12 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74   nCell = get2byt
20c13 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b  e(&data[hdr+3]);
20c14 0d 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20  ..    cellStart 
20c15 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70  = hdr + 12 - 4*p
20c16 50 61 67 65 2d 3e 6c 65 61 66 3b 0d 0a 20 20 20  Page->leaf;..   
20c17 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
20c18 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  l; i++){..      
20c19 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74  int pc = get2byt
20c1a 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72  e(&data[cellStar
20c1b 74 2b 69 2a 32 5d 29 3b 0d 0a 20 20 20 20 20 20  t+i*2]);..      
20c1c 75 33 32 20 73 69 7a 65 20 3d 20 36 35 35 33 36  u32 size = 65536
20c1d 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d  ;..      int j;.
20c1e 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 3d 75  .      if( pc<=u
20c1f 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0d 0a  sableSize-4 ){..
20c20 20 20 20 20 20 20 20 20 73 69 7a 65 20 3d 20 63          size = c
20c21 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
20c22 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0d 0a 20  , &data[pc]);.. 
20c23 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
20c24 28 20 28 69 6e 74 29 28 70 63 2b 73 69 7a 65 2d  ( (int)(pc+size-
20c25 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29  1)>=usableSize )
20c26 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  {..        check
20c27 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
20c28 2c 20 30 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  , 0, ..         
20c29 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64     "Corruption d
20c2a 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20  etected in cell 
20c2b 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69  %d on page %d",i
20c2c 2c 69 50 61 67 65 29 3b 0d 0a 20 20 20 20 20 20  ,iPage);..      
20c2d 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
20c2e 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b  for(j=pc+size-1;
20c2f 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74   j>=pc; j--) hit
20c30 5b 6a 5d 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d  [j]++;..      }.
20c31 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 20 3d 20  .    }..    i = 
20c32 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
20c33 64 72 2b 31 5d 29 3b 0d 0a 20 20 20 20 77 68 69  dr+1]);..    whi
20c34 6c 65 28 20 69 3e 30 20 29 7b 0d 0a 20 20 20 20  le( i>0 ){..    
20c35 20 20 69 6e 74 20 73 69 7a 65 2c 20 6a 3b 0d 0a    int size, j;..
20c36 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c        assert( i<
20c37 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b  =usableSize-4 );
20c38 20 20 20 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64       /* Enforced
20c39 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67   by btreeInitPag
20c3a 65 28 29 20 2a 2f 0d 0a 20 20 20 20 20 20 73 69  e() */..      si
20c3b 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ze = get2byte(&d
20c3c 61 74 61 5b 69 2b 32 5d 29 3b 0d 0a 20 20 20 20  ata[i+2]);..    
20c3d 20 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65    assert( i+size
20c3e 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20  <=usableSize ); 
20c3f 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20   /* Enforced by 
20c40 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
20c41 2a 2f 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  */..      for(j=
20c42 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b 20  i+size-1; j>=i; 
20c43 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0d 0a  j--) hit[j]++;..
20c44 20 20 20 20 20 20 6a 20 3d 20 67 65 74 32 62 79        j = get2by
20c45 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0d 0a 20  te(&data[i]);.. 
20c46 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d       assert( j==
20c47 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 20 29 3b  0 || j>i+size );
20c48 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79    /* Enforced by
20c49 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
20c4a 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
20c4b 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69 7a 65  t( j<=usableSize
20c4c 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66 6f 72  -4 );   /* Enfor
20c4d 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74  ced by btreeInit
20c4e 50 61 67 65 28 29 20 2a 2f 0d 0a 20 20 20 20 20  Page() */..     
20c4f 20 69 20 3d 20 6a 3b 0d 0a 20 20 20 20 7d 0d 0a   i = j;..    }..
20c50 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b      for(i=cnt=0;
20c51 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69   i<usableSize; i
20c52 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  ++){..      if( 
20c53 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0d 0a 20 20  hit[i]==0 ){..  
20c54 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0d 0a 20 20        cnt++;..  
20c55 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69      }else if( hi
20c56 74 5b 69 5d 3e 31 20 29 7b 0d 0a 20 20 20 20 20  t[i]>1 ){..     
20c57 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
20c58 67 28 70 43 68 65 63 6b 2c 20 30 2c 0d 0a 20 20  g(pCheck, 0,..  
20c59 20 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c          "Multipl
20c5a 65 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20  e uses for byte 
20c5b 25 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20  %d of page %d", 
20c5c 69 2c 20 69 50 61 67 65 29 3b 0d 0a 20 20 20 20  i, iPage);..    
20c5d 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
20c5e 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
20c5f 69 66 28 20 63 6e 74 21 3d 64 61 74 61 5b 68 64  if( cnt!=data[hd
20c60 72 2b 37 5d 20 29 7b 0d 0a 20 20 20 20 20 20 63  r+7] ){..      c
20c61 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
20c62 68 65 63 6b 2c 20 30 2c 20 0d 0a 20 20 20 20 20  heck, 0, ..     
20c63 20 20 20 20 20 22 46 72 61 67 6d 65 6e 74 61 74       "Fragmentat
20c64 69 6f 6e 20 6f 66 20 25 64 20 62 79 74 65 73 20  ion of %d bytes 
20c65 72 65 70 6f 72 74 65 64 20 61 73 20 25 64 20 6f  reported as %d o
20c66 6e 20 70 61 67 65 20 25 64 22 2c 0d 0a 20 20 20  n page %d",..   
20c67 20 20 20 20 20 20 20 63 6e 74 2c 20 64 61 74 61         cnt, data
20c68 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 65 29 3b  [hdr+7], iPage);
20c69 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
20c6a 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
20c6b 68 69 74 29 3b 0d 0a 20 20 72 65 6c 65 61 73 65  hit);..  release
20c6c 50 61 67 65 28 70 50 61 67 65 29 3b 0d 0a 20 20  Page(pPage);..  
20c6d 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0d  return depth+1;.
20c6e 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
20c6f 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
20c70 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0d 0a 0d 0a  ITY_CHECK */....
20c71 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20c72 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
20c73 45 43 4b 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  ECK../*..** This
20c74 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20   routine does a 
20c75 63 6f 6d 70 6c 65 74 65 20 63 68 65 63 6b 20 6f  complete check o
20c76 66 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65  f the given BTre
20c77 65 20 66 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d  e file.  aRoot[]
20c78 20 69 73 0d 0a 2a 2a 20 61 6e 20 61 72 72 61 79   is..** an array
20c79 20 6f 66 20 70 61 67 65 73 20 6e 75 6d 62 65 72   of pages number
20c7a 73 20 77 65 72 65 20 65 61 63 68 20 70 61 67 65  s were each page
20c7b 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 72   number is the r
20c7c 6f 6f 74 20 70 61 67 65 20 6f 66 0d 0a 2a 2a 20  oot page of..** 
20c7d 61 20 74 61 62 6c 65 2e 20 20 6e 52 6f 6f 74 20  a table.  nRoot 
20c7e 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
20c7f 20 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f   entries in aRoo
20c80 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 72 65 61  t...**..** A rea
20c81 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77  d-only or read-w
20c82 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
20c83 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
20c84 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 0d 0a  before calling..
20c85 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
20c86 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65 20  ...**..** Write 
20c87 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
20c88 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e 45  ror seen in *pnE
20c89 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72 20  rr.  Except for 
20c8a 73 6f 6d 65 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20  some memory..** 
20c8b 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
20c8c 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  s,  an error mes
20c8d 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d  sage held in mem
20c8e 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
20c8f 6d 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20  m..** malloc is 
20c90 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45  returned if *pnE
20c91 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  rr is non-zero. 
20c92 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68   If *pnErr==0 th
20c93 65 6e 20 4e 55 4c 4c 20 69 73 0d 0a 2a 2a 20 72  en NULL is..** r
20c94 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d  eturned.  If a m
20c95 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
20c96 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
20c97 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
20c98 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
20c99 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
20c9a 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79  e3BtreeIntegrity
20c9b 43 68 65 63 6b 28 0d 0a 20 20 42 74 72 65 65 20  Check(..  Btree 
20c9c 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62  *p,     /* The b
20c9d 74 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b  tree to be check
20c9e 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 52  ed */..  int *aR
20c9f 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72  oot,   /* An arr
20ca0 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  ay of root pages
20ca1 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64   numbers for ind
20ca2 69 76 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f  ividual trees */
20ca3 0d 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20  ..  int nRoot,  
20ca4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
20ca5 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b  ntries in aRoot[
20ca6 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 45 72  ] */..  int mxEr
20ca7 72 2c 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65  r,    /* Stop re
20ca8 70 6f 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61  porting errors a
20ca9 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a  fter this many *
20caa 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 45 72 72 20  /..  int *pnErr 
20cab 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62     /* Write numb
20cac 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65  er of errors see
20cad 6e 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62  n to this variab
20cae 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 50 67 6e  le */..){..  Pgn
20caf 6f 20 69 3b 0d 0a 20 20 69 6e 74 20 6e 52 65 66  o i;..  int nRef
20cb0 3b 0d 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b  ;..  IntegrityCk
20cb1 20 73 43 68 65 63 6b 3b 0d 0a 20 20 42 74 53 68   sCheck;..  BtSh
20cb2 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
20cb3 42 74 3b 0d 0a 20 20 63 68 61 72 20 7a 45 72 72  Bt;..  char zErr
20cb4 5b 31 30 30 5d 3b 0d 0a 0d 0a 20 20 73 71 6c 69  [100];....  sqli
20cb5 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
20cb6 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
20cb7 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f  inTrans>TRANS_NO
20cb8 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61  NE && pBt->inTra
20cb9 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e  nsaction>TRANS_N
20cba 4f 4e 45 20 29 3b 0d 0a 20 20 6e 52 65 66 20 3d  ONE );..  nRef =
20cbb 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
20cbc 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
20cbd 72 29 3b 0d 0a 20 20 73 43 68 65 63 6b 2e 70 42  r);..  sCheck.pB
20cbe 74 20 3d 20 70 42 74 3b 0d 0a 20 20 73 43 68 65  t = pBt;..  sChe
20cbf 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d  ck.pPager = pBt-
20cc0 3e 70 50 61 67 65 72 3b 0d 0a 20 20 73 43 68 65  >pPager;..  sChe
20cc1 63 6b 2e 6e 50 61 67 65 20 3d 20 62 74 72 65 65  ck.nPage = btree
20cc2 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b  Pagecount(sCheck
20cc3 2e 70 42 74 29 3b 0d 0a 20 20 73 43 68 65 63 6b  .pBt);..  sCheck
20cc4 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0d  .mxErr = mxErr;.
20cc5 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d  .  sCheck.nErr =
20cc6 20 30 3b 0d 0a 20 20 73 43 68 65 63 6b 2e 6d 61   0;..  sCheck.ma
20cc7 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0d  llocFailed = 0;.
20cc8 0a 20 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0d 0a  .  *pnErr = 0;..
20cc9 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 50 61    if( sCheck.nPa
20cca 67 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  ge==0 ){..    sq
20ccb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
20ccc 70 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  p);..    return 
20ccd 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73 43 68 65 63  0;..  }..  sChec
20cce 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 65  k.anRef = sqlite
20ccf 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b  3Malloc( (sCheck
20cd0 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66  .nPage+1)*sizeof
20cd1 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d  (sCheck.anRef[0]
20cd2 29 20 29 3b 0d 0a 20 20 69 66 28 20 21 73 43 68  ) );..  if( !sCh
20cd3 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0d 0a 20 20  eck.anRef ){..  
20cd4 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0d 0a 20    *pnErr = 1;.. 
20cd5 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
20cd6 65 61 76 65 28 70 29 3b 0d 0a 20 20 20 20 72 65  eave(p);..    re
20cd7 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 0;..  }..  
20cd8 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65  for(i=0; i<=sChe
20cd9 63 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20  ck.nPage; i++){ 
20cda 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20  sCheck.anRef[i] 
20cdb 3d 20 30 3b 20 7d 0d 0a 20 20 69 20 3d 20 50 45  = 0; }..  i = PE
20cdc 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
20cdd 70 42 74 29 3b 0d 0a 20 20 69 66 28 20 69 3c 3d  pBt);..  if( i<=
20cde 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0d  sCheck.nPage ){.
20cdf 0a 20 20 20 20 73 43 68 65 63 6b 2e 61 6e 52 65  .    sCheck.anRe
20ce0 66 5b 69 5d 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a  f[i] = 1;..  }..
20ce1 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
20ce2 6d 49 6e 69 74 28 26 73 43 68 65 63 6b 2e 65 72  mInit(&sCheck.er
20ce3 72 4d 73 67 2c 20 7a 45 72 72 2c 20 73 69 7a 65  rMsg, zErr, size
20ce4 6f 66 28 7a 45 72 72 29 2c 20 32 30 30 30 30 29  of(zErr), 20000)
20ce5 3b 0d 0a 20 20 73 43 68 65 63 6b 2e 65 72 72 4d  ;..  sCheck.errM
20ce6 73 67 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 32  sg.useMalloc = 2
20ce7 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;....  /* Check 
20ce8 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66  the integrity of
20ce9 20 74 68 65 20 66 72 65 65 6c 69 73 74 0d 0a 20   the freelist.. 
20cea 20 2a 2f 0d 0a 20 20 63 68 65 63 6b 4c 69 73 74   */..  checkList
20ceb 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74  (&sCheck, 1, get
20cec 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
20ced 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0d  e1->aData[32]),.
20cee 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65 74  .            get
20cef 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
20cf0 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20  e1->aData[36]), 
20cf1 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a 20  "Main freelist: 
20cf2 22 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63  ");....  /* Chec
20cf3 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73  k all the tables
20cf4 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69  ...  */..  for(i
20cf5 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74  =0; (int)i<nRoot
20cf6 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72   && sCheck.mxErr
20cf7 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28  ; i++){..    if(
20cf8 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63   aRoot[i]==0 ) c
20cf9 6f 6e 74 69 6e 75 65 3b 0d 0a 23 69 66 6e 64 65  ontinue;..#ifnde
20cfa 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
20cfb 54 4f 56 41 43 55 55 4d 0d 0a 20 20 20 20 69 66  TOVACUUM..    if
20cfc 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
20cfd 6d 20 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20  m && aRoot[i]>1 
20cfe 29 7b 0d 0a 20 20 20 20 20 20 63 68 65 63 6b 50  ){..      checkP
20cff 74 72 6d 61 70 28 26 73 43 68 65 63 6b 2c 20 61  trmap(&sCheck, a
20d00 52 6f 6f 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f  Root[i], PTRMAP_
20d01 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 30 29 3b  ROOTPAGE, 0, 0);
20d02 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
20d03 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 50 61  .    checkTreePa
20d04 67 65 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f  ge(&sCheck, aRoo
20d05 74 5b 69 5d 2c 20 22 4c 69 73 74 20 6f 66 20 74  t[i], "List of t
20d06 72 65 65 20 72 6f 6f 74 73 3a 20 22 2c 20 4e 55  ree roots: ", NU
20d07 4c 4c 2c 20 4e 55 4c 4c 29 3b 0d 0a 20 20 7d 0d  LL, NULL);..  }.
20d08 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ...  /* Make sur
20d09 65 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20  e every page in 
20d0a 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 66 65  the file is refe
20d0b 72 65 6e 63 65 64 0d 0a 20 20 2a 2f 0d 0a 20 20  renced..  */..  
20d0c 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43 68 65  for(i=1; i<=sChe
20d0d 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43 68 65  ck.nPage && sChe
20d0e 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0d  ck.mxErr; i++){.
20d0f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
20d10 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a  MIT_AUTOVACUUM..
20d11 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61      if( sCheck.a
20d12 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0d 0a 20  nRef[i]==0 ){.. 
20d13 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
20d14 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20  Msg(&sCheck, 0, 
20d15 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65  "Page %d is neve
20d16 72 20 75 73 65 64 22 2c 20 69 29 3b 0d 0a 20 20  r used", i);..  
20d17 20 20 7d 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20    }..#else..    
20d18 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
20d19 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
20d1a 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75  -vacuum, make su
20d1b 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e  re no tables con
20d1c 74 61 69 6e 0d 0a 20 20 20 20 2a 2a 20 72 65 66  tain..    ** ref
20d1d 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74  erences to point
20d1e 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0d 0a 20  er-map pages... 
20d1f 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73     */..    if( s
20d20 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d  Check.anRef[i]==
20d21 30 20 26 26 20 0d 0a 20 20 20 20 20 20 20 28 50  0 && ..       (P
20d22 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
20d23 2c 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d  , i)!=i || !pBt-
20d24 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0d  >autoVacuum) ){.
20d25 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
20d26 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
20d27 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65  , "Page %d is ne
20d28 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0d 0a  ver used", i);..
20d29 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73      }..    if( s
20d2a 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d  Check.anRef[i]!=
20d2b 30 20 26 26 20 0d 0a 20 20 20 20 20 20 20 28 50  0 && ..       (P
20d2c 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
20d2d 2c 20 69 29 3d 3d 69 20 26 26 20 70 42 74 2d 3e  , i)==i && pBt->
20d2e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0d 0a  autoVacuum) ){..
20d2f 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
20d30 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c  dMsg(&sCheck, 0,
20d31 20 22 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61   "Pointer map pa
20d32 67 65 20 25 64 20 69 73 20 72 65 66 65 72 65 6e  ge %d is referen
20d33 63 65 64 22 2c 20 69 29 3b 0d 0a 20 20 20 20 7d  ced", i);..    }
20d34 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d  ..#endif..  }...
20d35 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
20d36 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 69  this analysis di
20d37 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 20  d not leave any 
20d38 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0d 0a  unref() pages...
20d39 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20    ** This is an 
20d3a 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74  internal consist
20d3b 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69  ency check; an i
20d3c 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0d 0a  ntegrity check..
20d3d 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65    ** of the inte
20d3e 67 72 69 74 79 20 63 68 65 63 6b 2e 0d 0a 20 20  grity check...  
20d3f 2a 2f 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28  */..  if( NEVER(
20d40 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50  nRef != sqlite3P
20d41 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74  agerRefcount(pBt
20d42 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0d 0a 20  ->pPager)) ){.. 
20d43 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
20d44 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0d 0a  g(&sCheck, 0, ..
20d45 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69        "Outstandi
20d46 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f  ng page count go
20d47 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64  es from %d to %d
20d48 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61   during this ana
20d49 6c 79 73 69 73 22 2c 0d 0a 20 20 20 20 20 20 6e  lysis",..      n
20d4a 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65  Ref, sqlite3Page
20d4b 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
20d4c 50 61 67 65 72 29 0d 0a 20 20 20 20 29 3b 0d 0a  Pager)..    );..
20d4d 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6c 65 61    }....  /* Clea
20d4e 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74  n  up and report
20d4f 20 65 72 72 6f 72 73 2e 0d 0a 20 20 2a 2f 0d 0a   errors...  */..
20d50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
20d51 61 76 65 28 70 29 3b 0d 0a 20 20 73 71 6c 69 74  ave(p);..  sqlit
20d52 65 33 5f 66 72 65 65 28 73 43 68 65 63 6b 2e 61  e3_free(sCheck.a
20d53 6e 52 65 66 29 3b 0d 0a 20 20 69 66 28 20 73 43  nRef);..  if( sC
20d54 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
20d55 64 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  d ){..    sqlite
20d56 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26  3StrAccumReset(&
20d57 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0d  sCheck.errMsg);.
20d58 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43  .    *pnErr = sC
20d59 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0d 0a 20 20  heck.nErr+1;..  
20d5a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
20d5b 0d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68  ..  *pnErr = sCh
20d5c 65 63 6b 2e 6e 45 72 72 3b 0d 0a 20 20 69 66 28  eck.nErr;..  if(
20d5d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20   sCheck.nErr==0 
20d5e 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  ) sqlite3StrAccu
20d5f 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65  mReset(&sCheck.e
20d60 72 72 4d 73 67 29 3b 0d 0a 20 20 72 65 74 75 72  rrMsg);..  retur
20d61 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  n sqlite3StrAccu
20d62 6d 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e  mFinish(&sCheck.
20d63 65 72 72 4d 73 67 29 3b 0d 0a 7d 0d 0a 23 65 6e  errMsg);..}..#en
20d64 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
20d65 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
20d66 43 4b 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  CK */..../*..** 
20d67 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20  Return the full 
20d68 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
20d69 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62  underlying datab
20d6a 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a  ase file...**..*
20d6b 2a 20 54 68 65 20 70 61 67 65 72 20 66 69 6c 65  * The pager file
20d6c 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e  name is invarian
20d6d 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  t as long as the
20d6e 20 70 61 67 65 72 20 69 73 0d 0a 2a 2a 20 6f 70   pager is..** op
20d6f 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65  en so it is safe
20d70 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f   to access witho
20d71 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20  ut the BtShared 
20d72 6d 75 74 65 78 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  mutex...*/..SQLI
20d73 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
20d74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
20d75 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42  reeGetFilename(B
20d76 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20 61 73 73  tree *p){..  ass
20d77 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
20d78 67 65 72 21 3d 30 20 29 3b 0d 0a 20 20 72 65 74  ger!=0 );..  ret
20d79 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
20d7a 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  Filename(p->pBt-
20d7b 3e 70 50 61 67 65 72 29 3b 0d 0a 7d 0d 0a 0d 0a  >pPager);..}....
20d7c 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
20d7d 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  e pathname of th
20d7e 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
20d7f 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
20d80 2e 20 54 68 65 20 72 65 74 75 72 6e 0d 0a 2a 2a  . The return..**
20d81 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72   value of this r
20d82 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61  outine is the sa
20d83 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  me regardless of
20d84 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75   whether the jou
20d85 72 6e 61 6c 20 66 69 6c 65 0d 0a 2a 2a 20 68 61  rnal file..** ha
20d86 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f  s been created o
20d87 72 20 6e 6f 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  r not...**..** T
20d88 68 65 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c  he pager journal
20d89 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
20d8a 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
20d8b 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0d 0a  s the pager is..
20d8c 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73  ** open so it is
20d8d 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20   safe to access 
20d8e 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68  without the BtSh
20d8f 61 72 65 64 20 6d 75 74 65 78 2e 0d 0a 2a 2f 0d  ared mutex...*/.
20d90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20d91 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
20d92 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e  te3BtreeGetJourn
20d93 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29  alname(Btree *p)
20d94 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  {..  assert( p->
20d95 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29  pBt->pPager!=0 )
20d96 3b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ;..  return sqli
20d97 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e  te3PagerJournaln
20d98 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67  ame(p->pBt->pPag
20d99 65 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  er);..}..../*..*
20d9a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
20d9b 6f 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69  o if a transacti
20d9c 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0d 0a 2a  on is active...*
20d9d 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
20d9e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
20d9f 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65  eeIsInTrans(Btre
20da0 65 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74  e *p){..  assert
20da1 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ( p==0 || sqlite
20da2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
20da3 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  db->mutex) );.. 
20da4 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70   return (p && (p
20da5 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
20da6 5f 57 52 49 54 45 29 29 3b 0d 0a 7d 0d 0a 0d 0a  _WRITE));..}....
20da7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20da8 4d 49 54 5f 57 41 4c 0d 0a 2f 2a 0d 0a 2a 2a 20  MIT_WAL../*..** 
20da9 52 75 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e 74  Run a checkpoint
20daa 20 6f 6e 20 74 68 65 20 42 74 72 65 65 20 70 61   on the Btree pa
20dab 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
20dac 74 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d  t argument...**.
20dad 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
20dae 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 69 73  E_LOCKED if this
20daf 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 63 6f   or any other co
20db0 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 6e 20  nnection has an 
20db1 6f 70 65 6e 20 0d 0a 2a 2a 20 74 72 61 6e 73 61  open ..** transa
20db2 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 68 61  ction on the sha
20db3 72 65 64 2d 63 61 63 68 65 20 74 68 65 20 61 72  red-cache the ar
20db4 67 75 6d 65 6e 74 20 42 74 72 65 65 20 69 73 20  gument Btree is 
20db5 63 6f 6e 6e 65 63 74 65 64 20 74 6f 2e 0d 0a 2a  connected to...*
20db6 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  *..** Parameter 
20db7 65 4d 6f 64 65 20 69 73 20 6f 6e 65 20 6f 66 20  eMode is one of 
20db8 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
20db9 54 5f 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20  T_PASSIVE, FULL 
20dba 6f 72 20 52 45 53 54 41 52 54 2e 0d 0a 2a 2f 0d  or RESTART...*/.
20dbb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20dbc 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
20dbd 43 68 65 63 6b 70 6f 69 6e 74 28 42 74 72 65 65  Checkpoint(Btree
20dbe 20 2a 70 2c 20 69 6e 74 20 65 4d 6f 64 65 2c 20   *p, int eMode, 
20dbf 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 69 6e 74 20  int *pnLog, int 
20dc0 2a 70 6e 43 6b 70 74 29 7b 0d 0a 20 20 69 6e 74  *pnCkpt){..  int
20dc1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
20dc2 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20  ..  if( p ){..  
20dc3 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
20dc4 3d 20 70 2d 3e 70 42 74 3b 0d 0a 20 20 20 20 73  = p->pBt;..    s
20dc5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
20dc6 28 70 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 42  (p);..    if( pB
20dc7 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
20dc8 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0d  !=TRANS_NONE ){.
20dc9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
20dca 54 45 5f 4c 4f 43 4b 45 44 3b 0d 0a 20 20 20 20  TE_LOCKED;..    
20dcb 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63  }else{..      rc
20dcc 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43   = sqlite3PagerC
20dcd 68 65 63 6b 70 6f 69 6e 74 28 70 42 74 2d 3e 70  heckpoint(pBt->p
20dce 50 61 67 65 72 2c 20 65 4d 6f 64 65 2c 20 70 6e  Pager, eMode, pn
20dcf 4c 6f 67 2c 20 70 6e 43 6b 70 74 29 3b 0d 0a 20  Log, pnCkpt);.. 
20dd0 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
20dd1 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d  3BtreeLeave(p);.
20dd2 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
20dd3 63 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  c;..}..#endif...
20dd4 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ./*..** Return n
20dd5 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 61  on-zero if a rea
20dd6 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72 61  d (or write) tra
20dd7 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
20dd8 76 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ve...*/..SQLITE_
20dd9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20dda 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64  te3BtreeIsInRead
20ddb 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b  Trans(Btree *p){
20ddc 0d 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b  ..  assert( p );
20ddd 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
20dde 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
20ddf 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  ->db->mutex) );.
20de0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54  .  return p->inT
20de1 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
20de2 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50  ;..}....SQLITE_P
20de3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20de4 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75  e3BtreeIsInBacku
20de5 70 28 42 74 72 65 65 20 2a 70 29 7b 0d 0a 20 20  p(Btree *p){..  
20de6 61 73 73 65 72 74 28 20 70 20 29 3b 0d 0a 20 20  assert( p );..  
20de7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
20de8 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
20de9 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 72  ->mutex) );..  r
20dea 65 74 75 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70  eturn p->nBackup
20deb 21 3d 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  !=0;..}..../*..*
20dec 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
20ded 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
20dee 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d  r to a blob of m
20def 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
20df0 20 77 69 74 68 0d 0a 2a 2a 20 61 20 73 69 6e 67   with..** a sing
20df1 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e  le shared-btree.
20df2 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75   The memory is u
20df3 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f  sed by client co
20df4 64 65 20 66 6f 72 20 69 74 73 20 6f 77 6e 0d 0a  de for its own..
20df5 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f 72  ** purposes (for
20df6 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f   example, to sto
20df7 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c 20  re a high-level 
20df8 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65  schema associate
20df9 64 20 77 69 74 68 20 0d 0a 2a 2a 20 74 68 65 20  d with ..** the 
20dfa 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54  shared-btree). T
20dfb 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d  he btree layer m
20dfc 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65  anages reference
20dfd 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73   counting issues
20dfe 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69  ...**..** The fi
20dff 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73  rst time this is
20e00 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61   called on a sha
20e01 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65  red-btree, nByte
20e02 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  s bytes of memor
20e03 79 0d 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61  y..** are alloca
20e04 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64  ted, zeroed, and
20e05 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
20e06 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63   caller. For eac
20e07 68 20 73 75 62 73 65 71 75 65 6e 74 20 0d 0a 2a  h subsequent ..*
20e08 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65  * call the nByte
20e09 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69  s parameter is i
20e0a 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69  gnored and a poi
20e0b 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65  nter to the same
20e0c 20 62 6c 6f 62 0d 0a 2a 2a 20 6f 66 20 6d 65 6d   blob..** of mem
20e0d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0d 0a  ory returned. ..
20e0e 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42  **..** If the nB
20e0f 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69  ytes parameter i
20e10 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  s 0 and the blob
20e11 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e   of memory has n
20e12 6f 74 20 79 65 74 20 62 65 65 6e 0d 0a 2a 2a 20  ot yet been..** 
20e13 61 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c  allocated, a nul
20e14 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  l pointer is ret
20e15 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c  urned. If the bl
20e16 6f 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ob has already b
20e17 65 65 6e 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  een..** allocate
20e18 64 2c 20 69 74 20 69 73 20 72 65 74 75 72 6e 65  d, it is returne
20e19 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0d 0a 2a 2a  d as normal...**
20e1a 0d 0a 2a 2a 20 4a 75 73 74 20 62 65 66 6f 72 65  ..** Just before
20e1b 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65   the shared-btre
20e1c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 68 65  e is closed, the
20e1d 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 64   function passed
20e1e 20 61 73 20 74 68 65 20 0d 0a 2a 2a 20 78 46 72   as the ..** xFr
20e1f 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e  ee argument when
20e20 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
20e21 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64 65 20  cation was made 
20e22 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68  is invoked on th
20e23 65 20 0d 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20 61  e ..** blob of a
20e24 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e  llocated memory.
20e25 20 54 68 65 20 78 46 72 65 65 20 66 75 6e 63 74   The xFree funct
20e26 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63  ion should not c
20e27 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  all sqlite3_free
20e28 28 29 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65  ()..** on the me
20e29 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20  mory, the btree 
20e2a 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e  layer does that.
20e2b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
20e2c 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
20e2d 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74  e3BtreeSchema(Bt
20e2e 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74  ree *p, int nByt
20e2f 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29  es, void(*xFree)
20e30 28 76 6f 69 64 20 2a 29 29 7b 0d 0a 20 20 42 74  (void *)){..  Bt
20e31 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
20e32 3e 70 42 74 3b 0d 0a 20 20 73 71 6c 69 74 65 33  >pBt;..  sqlite3
20e33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0d 0a  BtreeEnter(p);..
20e34 20 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68    if( !pBt->pSch
20e35 65 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b  ema && nBytes ){
20e36 0d 0a 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65  ..    pBt->pSche
20e37 6d 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ma = sqlite3DbMa
20e38 6c 6c 6f 63 5a 65 72 6f 28 30 2c 20 6e 42 79 74  llocZero(0, nByt
20e39 65 73 29 3b 0d 0a 20 20 20 20 70 42 74 2d 3e 78  es);..    pBt->x
20e3a 46 72 65 65 53 63 68 65 6d 61 20 3d 20 78 46 72  FreeSchema = xFr
20e3b 65 65 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  ee;..  }..  sqli
20e3c 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
20e3d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d  ;..  return pBt-
20e3e 3e 70 53 63 68 65 6d 61 3b 0d 0a 7d 0d 0a 0d 0a  >pSchema;..}....
20e3f 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  /*..** Return SQ
20e40 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52  LITE_LOCKED_SHAR
20e41 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68  EDCACHE if anoth
20e42 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20 73  er user of the s
20e43 61 6d 65 20 73 68 61 72 65 64 20 0d 0a 2a 2a 20  ame shared ..** 
20e44 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67  btree as the arg
20e45 75 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c  ument handle hol
20e46 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ds an exclusive 
20e47 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 0d 0a 2a 2a  lock on the ..**
20e48 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
20e49 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20  able. Otherwise 
20e4a 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a  SQLITE_OK...*/..
20e4b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20e4c 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
20e4d 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65  chemaLocked(Btre
20e4e 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 72 63  e *p){..  int rc
20e4f 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
20e50 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
20e51 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
20e52 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
20e53 45 6e 74 65 72 28 70 29 3b 0d 0a 20 20 72 63 20  Enter(p);..  rc 
20e54 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63  = querySharedCac
20e55 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d  heTableLock(p, M
20e56 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44  ASTER_ROOT, READ
20e57 5f 4c 4f 43 4b 29 3b 0d 0a 20 20 61 73 73 65 72  _LOCK);..  asser
20e58 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
20e59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c   || rc==SQLITE_L
20e5a 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
20e5b 45 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42  E );..  sqlite3B
20e5c 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20  treeLeave(p);.. 
20e5d 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
20e5e 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
20e5f 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
20e60 41 43 48 45 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 62 74  ACHE../*..** Obt
20e61 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  ain a lock on th
20e62 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f  e table whose ro
20e63 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e  ot page is iTab.
20e64 20 20 54 68 65 0d 0a 2a 2a 20 6c 6f 63 6b 20 69    The..** lock i
20e65 73 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69  s a write lock i
20e66 66 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73  f isWritelock is
20e67 20 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20   true or a read 
20e68 6c 6f 63 6b 0d 0a 2a 2a 20 69 66 20 69 74 20 69  lock..** if it i
20e69 73 20 66 61 6c 73 65 2e 0d 0a 2a 2f 0d 0a 53 51  s false...*/..SQ
20e6a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20e6b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63   sqlite3BtreeLoc
20e6c 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  kTable(Btree *p,
20e6d 20 69 6e 74 20 69 54 61 62 2c 20 75 38 20 69 73   int iTab, u8 is
20e6e 57 72 69 74 65 4c 6f 63 6b 29 7b 0d 0a 20 20 69  WriteLock){..  i
20e6f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
20e70 4b 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  K;..  assert( p-
20e71 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f  >inTrans!=TRANS_
20e72 4e 4f 4e 45 20 29 3b 0d 0a 20 20 69 66 28 20 70  NONE );..  if( p
20e73 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0d 0a 20  ->sharable ){.. 
20e74 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d     u8 lockType =
20e75 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57   READ_LOCK + isW
20e76 72 69 74 65 4c 6f 63 6b 3b 0d 0a 20 20 20 20 61  riteLock;..    a
20e77 73 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b  ssert( READ_LOCK
20e78 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  +1==WRITE_LOCK )
20e79 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ;..    assert( i
20e7a 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c  sWriteLock==0 ||
20e7b 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20   isWriteLock==1 
20e7c 29 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65  );....    sqlite
20e7d 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0d  3BtreeEnter(p);.
20e7e 0a 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53  .    rc = queryS
20e7f 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
20e80 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63  ock(p, iTab, loc
20e81 6b 54 79 70 65 29 3b 0d 0a 20 20 20 20 69 66 28  kType);..    if(
20e82 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
20e83 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65  {..      rc = se
20e84 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
20e85 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c  eLock(p, iTab, l
20e86 6f 63 6b 54 79 70 65 29 3b 0d 0a 20 20 20 20 7d  ockType);..    }
20e87 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ..    sqlite3Btr
20e88 65 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20 7d  eeLeave(p);..  }
20e89 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
20e8a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66  }..#endif....#if
20e8b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
20e8c 5f 49 4e 43 52 42 4c 4f 42 0d 0a 2f 2a 0d 0a 2a  _INCRBLOB../*..*
20e8d 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20  * Argument pCsr 
20e8e 6d 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72  must be a cursor
20e8f 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
20e90 69 6e 67 20 6f 6e 20 61 6e 20 0d 0a 2a 2a 20 49  ing on an ..** I
20e91 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72  NTKEY table curr
20e92 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61  ently pointing a
20e93 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20  t a valid table 
20e94 65 6e 74 72 79 2e 20 0d 0a 2a 2a 20 54 68 69 73  entry. ..** This
20e95 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69   function modifi
20e96 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f 72  es the data stor
20e97 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
20e98 61 74 20 65 6e 74 72 79 2e 0d 0a 2a 2a 0d 0a 2a  at entry...**..*
20e99 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  * Only the data 
20e9a 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79  content may only
20e9b 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74   be modified, it
20e9c 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
20e9d 20 74 6f 20 0d 0a 2a 2a 20 63 68 61 6e 67 65 20   to ..** change 
20e9e 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
20e9f 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49  e data stored. I
20ea0 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
20ea1 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0d 0a  is called with..
20ea2 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68  ** parameters th
20ea3 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72  at attempt to wr
20ea4 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  ite past the end
20ea5 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 6e 67   of the existing
20ea6 20 64 61 74 61 2c 0d 0a 2a 2a 20 6e 6f 20 6d 6f   data,..** no mo
20ea7 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20  difications are 
20ea8 6d 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 5f  made and SQLITE_
20ea9 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72  CORRUPT is retur
20eaa 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ned...*/..SQLITE
20eab 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20eac 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61  ite3BtreePutData
20ead 28 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  (BtCursor *pCsr,
20eae 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
20eaf 20 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0d   amt, void *z){.
20eb0 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 61 73  .  int rc;..  as
20eb1 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
20eb2 73 4d 75 74 65 78 28 70 43 73 72 29 20 29 3b 0d  sMutex(pCsr) );.
20eb3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
20eb4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
20eb5 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  sr->pBtree->db->
20eb6 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73  mutex) );..  ass
20eb7 65 72 74 28 20 70 43 73 72 2d 3e 69 73 49 6e 63  ert( pCsr->isInc
20eb8 72 62 6c 6f 62 48 61 6e 64 6c 65 20 29 3b 0d 0a  rblobHandle );..
20eb9 0d 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ..  rc = restore
20eba 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
20ebb 43 73 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  Csr);..  if( rc!
20ebc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
20ebd 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
20ebe 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43   }..  assert( pC
20ebf 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  sr->eState!=CURS
20ec0 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
20ec1 3b 0d 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65  ;..  if( pCsr->e
20ec2 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41  State!=CURSOR_VA
20ec3 4c 49 44 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  LID ){..    retu
20ec4 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b  rn SQLITE_ABORT;
20ec5 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68  ..  }....  /* Ch
20ec6 65 63 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74  eck some assumpt
20ec7 69 6f 6e 73 3a 20 0d 0a 20 20 2a 2a 20 20 20 28  ions: ..  **   (
20ec8 61 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  a) the cursor is
20ec9 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e   open for writin
20eca 67 2c 0d 0a 20 20 2a 2a 20 20 20 28 62 29 20 74  g,..  **   (b) t
20ecb 68 65 72 65 20 69 73 20 61 20 72 65 61 64 2f 77  here is a read/w
20ecc 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
20ecd 20 6f 70 65 6e 2c 0d 0a 20 20 2a 2a 20 20 20 28   open,..  **   (
20ece 63 29 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  c) the connectio
20ecf 6e 20 68 6f 6c 64 73 20 61 20 77 72 69 74 65 2d  n holds a write-
20ed0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c  lock on the tabl
20ed1 65 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c  e (if required),
20ed2 0d 0a 20 20 2a 2a 20 20 20 28 64 29 20 74 68 65  ..  **   (d) the
20ed3 72 65 20 61 72 65 20 6e 6f 20 63 6f 6e 66 6c 69  re are no confli
20ed4 63 74 69 6e 67 20 72 65 61 64 2d 6c 6f 63 6b 73  cting read-locks
20ed5 2c 20 61 6e 64 0d 0a 20 20 2a 2a 20 20 20 28 65  , and..  **   (e
20ed6 29 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69  ) the cursor poi
20ed7 6e 74 73 20 61 74 20 61 20 76 61 6c 69 64 20 72  nts at a valid r
20ed8 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20  ow of an intKey 
20ed9 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  table...  */..  
20eda 69 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c 61  if( !pCsr->wrFla
20edb 67 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  g ){..    return
20edc 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
20edd 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
20ede 28 20 28 70 43 73 72 2d 3e 70 42 74 2d 3e 62 74  ( (pCsr->pBt->bt
20edf 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41  sFlags & BTS_REA
20ee0 44 5f 4f 4e 4c 59 29 3d 3d 30 0d 0a 20 20 20 20  D_ONLY)==0..    
20ee1 20 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73            && pCs
20ee2 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  r->pBt->inTransa
20ee3 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
20ee4 54 45 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  TE );..  assert(
20ee5 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54   hasSharedCacheT
20ee6 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70  ableLock(pCsr->p
20ee7 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e  Btree, pCsr->pgn
20ee8 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0d  oRoot, 0, 2) );.
20ee9 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52  .  assert( !hasR
20eea 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73  eadConflicts(pCs
20eeb 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d  r->pBtree, pCsr-
20eec 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0d 0a 20  >pgnoRoot) );.. 
20eed 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61   assert( pCsr->a
20eee 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67  pPage[pCsr->iPag
20eef 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 3b 0d 0a 0d  e]->intKey );...
20ef0 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73  .  return access
20ef1 50 61 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66  Payload(pCsr, of
20ef2 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69  fset, amt, (unsi
20ef3 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 31  gned char *)z, 1
20ef4 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a  );..}..../* ..**
20ef5 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 74   Set a flag on t
20ef6 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 61  his cursor to ca
20ef7 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e  che the location
20ef8 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d 20  s of pages from 
20ef9 74 68 65 20 0d 0a 2a 2a 20 6f 76 65 72 66 6c 6f  the ..** overflo
20efa 77 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63  w list for the c
20efb 75 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73  urrent row. This
20efc 20 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73   is used by curs
20efd 6f 72 73 20 6f 70 65 6e 65 64 0d 0a 2a 2a 20 66  ors opened..** f
20efe 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  or incremental b
20eff 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0d 0a 2a 2a  lob IO only...**
20f00 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
20f01 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 6f  on sets a flag o
20f02 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c 20  nly. The actual 
20f03 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 61  page location ca
20f04 63 68 65 0d 0a 2a 2a 20 28 73 74 6f 72 65 64 20  che..** (stored 
20f05 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
20f06 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
20f07 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
20f08 79 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 61  y function..** a
20f09 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28  ccessPayload() (
20f0a 74 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74  the worker funct
20f0b 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42  ion for sqlite3B
20f0c 74 72 65 65 44 61 74 61 28 29 20 61 6e 64 0d 0a  treeData() and..
20f0d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  ** sqlite3BtreeP
20f0e 75 74 44 61 74 61 28 29 29 2e 0d 0a 2a 2f 0d 0a  utData())...*/..
20f0f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20f10 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
20f11 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74  CacheOverflow(Bt
20f12 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0d 0a  Cursor *pCur){..
20f13 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
20f14 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
20f15 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
20f16 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
20f17 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e  d(pCur->pBtree->
20f18 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  db->mutex) );.. 
20f19 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66   invalidateOverf
20f1a 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0d  lowCache(pCur);.
20f1b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62  .  pCur->isIncrb
20f1c 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 3b 0d 0a  lobHandle = 1;..
20f1d 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  }..#endif..../*.
20f1e 0a 2a 2a 20 53 65 74 20 62 6f 74 68 20 74 68 65  .** Set both the
20f1f 20 22 72 65 61 64 20 76 65 72 73 69 6f 6e 22 20   "read version" 
20f20 28 73 69 6e 67 6c 65 20 62 79 74 65 20 61 74 20  (single byte at 
20f21 62 79 74 65 20 6f 66 66 73 65 74 20 31 38 29 20  byte offset 18) 
20f22 61 6e 64 20 0d 0a 2a 2a 20 22 77 72 69 74 65 20  and ..** "write 
20f23 76 65 72 73 69 6f 6e 22 20 28 73 69 6e 67 6c 65  version" (single
20f24 20 62 79 74 65 20 61 74 20 62 79 74 65 20 6f 66   byte at byte of
20f25 66 73 65 74 20 31 39 29 20 66 69 65 6c 64 73 20  fset 19) fields 
20f26 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0d  in the database.
20f27 0a 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 69 56  .** header to iV
20f28 65 72 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ersion...*/..SQL
20f29 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20f2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 56  sqlite3BtreeSetV
20f2b 65 72 73 69 6f 6e 28 42 74 72 65 65 20 2a 70 42  ersion(Btree *pB
20f2c 74 72 65 65 2c 20 69 6e 74 20 69 56 65 72 73 69  tree, int iVersi
20f2d 6f 6e 29 7b 0d 0a 20 20 42 74 53 68 61 72 65 64  on){..  BtShared
20f2e 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e   *pBt = pBtree->
20f2f 70 42 74 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 20  pBt;..  int rc; 
20f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f31 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
20f32 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 0d 0a 20 20  n code */.. ..  
20f33 61 73 73 65 72 74 28 20 69 56 65 72 73 69 6f 6e  assert( iVersion
20f34 3d 3d 31 20 7c 7c 20 69 56 65 72 73 69 6f 6e 3d  ==1 || iVersion=
20f35 3d 32 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  =2 );....  /* If
20f36 20 73 65 74 74 69 6e 67 20 74 68 65 20 76 65 72   setting the ver
20f37 73 69 6f 6e 20 66 69 65 6c 64 73 20 74 6f 20 31  sion fields to 1
20f38 2c 20 64 6f 20 6e 6f 74 20 61 75 74 6f 6d 61 74  , do not automat
20f39 69 63 61 6c 6c 79 20 6f 70 65 6e 20 74 68 65 0d  ically open the.
20f3a 0a 20 20 2a 2a 20 57 41 4c 20 63 6f 6e 6e 65 63  .  ** WAL connec
20f3b 74 69 6f 6e 2c 20 65 76 65 6e 20 69 66 20 74 68  tion, even if th
20f3c 65 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 73  e version fields
20f3d 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 73   are currently s
20f3e 65 74 20 74 6f 20 32 2e 0d 0a 20 20 2a 2f 0d 0a  et to 2...  */..
20f3f 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20    pBt->btsFlags 
20f40 26 3d 20 7e 42 54 53 5f 4e 4f 5f 57 41 4c 3b 0d  &= ~BTS_NO_WAL;.
20f41 0a 20 20 69 66 28 20 69 56 65 72 73 69 6f 6e 3d  .  if( iVersion=
20f42 3d 31 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61  =1 ) pBt->btsFla
20f43 67 73 20 7c 3d 20 42 54 53 5f 4e 4f 5f 57 41 4c  gs |= BTS_NO_WAL
20f44 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  ;....  rc = sqli
20f45 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
20f46 6e 73 28 70 42 74 72 65 65 2c 20 30 29 3b 0d 0a  ns(pBtree, 0);..
20f47 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
20f48 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 75 38 20 2a  _OK ){..    u8 *
20f49 61 44 61 74 61 20 3d 20 70 42 74 2d 3e 70 50 61  aData = pBt->pPa
20f4a 67 65 31 2d 3e 61 44 61 74 61 3b 0d 0a 20 20 20  ge1->aData;..   
20f4b 20 69 66 28 20 61 44 61 74 61 5b 31 38 5d 21 3d   if( aData[18]!=
20f4c 28 75 38 29 69 56 65 72 73 69 6f 6e 20 7c 7c 20  (u8)iVersion || 
20f4d 61 44 61 74 61 5b 31 39 5d 21 3d 28 75 38 29 69  aData[19]!=(u8)i
20f4e 56 65 72 73 69 6f 6e 20 29 7b 0d 0a 20 20 20 20  Version ){..    
20f4f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
20f50 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42  reeBeginTrans(pB
20f51 74 72 65 65 2c 20 32 29 3b 0d 0a 20 20 20 20 20  tree, 2);..     
20f52 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
20f53 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  OK ){..        r
20f54 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
20f55 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65  Write(pBt->pPage
20f56 31 2d 3e 70 44 62 50 61 67 65 29 3b 0d 0a 20 20  1->pDbPage);..  
20f57 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
20f58 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
20f59 20 20 20 20 20 20 61 44 61 74 61 5b 31 38 5d 20        aData[18] 
20f5a 3d 20 28 75 38 29 69 56 65 72 73 69 6f 6e 3b 0d  = (u8)iVersion;.
20f5b 0a 20 20 20 20 20 20 20 20 20 20 61 44 61 74 61  .          aData
20f5c 5b 31 39 5d 20 3d 20 28 75 38 29 69 56 65 72 73  [19] = (u8)iVers
20f5d 69 6f 6e 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  ion;..        }.
20f5e 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
20f5f 0a 20 20 7d 0d 0a 0d 0a 20 20 70 42 74 2d 3e 62  .  }....  pBt->b
20f60 74 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f  tsFlags &= ~BTS_
20f61 4e 4f 5f 57 41 4c 3b 0d 0a 20 20 72 65 74 75 72  NO_WAL;..  retur
20f62 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a  n rc;..}..../***
20f63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
20f64 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a  of btree.c *****
20f65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
20f68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
20f69 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 2e 63  in file backup.c
20f6a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
20f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
20f6d 0d 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  ..** 2009 Januar
20f6e 79 20 32 38 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  y 28..**..** The
20f6f 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
20f70 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
20f71 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
20f72 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
20f73 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
20f74 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
20f75 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
20f76 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
20f77 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
20f78 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
20f79 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
20f7a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
20f7b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
20f7c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
20f7d 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
20f7e 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
20f7f 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
20f80 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
20f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
20f85 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
20f86 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
20f87 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
20f88 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58  lite3_backup_XXX
20f89 28 29 20 0d 0a 2a 2a 20 41 50 49 20 66 75 6e 63  () ..** API func
20f8a 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 72 65  tions and the re
20f8b 6c 61 74 65 64 20 66 65 61 74 75 72 65 73 2e 0d  lated features..
20f8c 0a 2a 2f 0d 0a 0d 0a 2f 2a 20 4d 61 63 72 6f 20  .*/..../* Macro 
20f8d 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69  to find the mini
20f8e 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72  mum of two numer
20f8f 69 63 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f 0d 0a  ic values...*/..
20f90 23 69 66 6e 64 65 66 20 4d 49 4e 0d 0a 23 20 64  #ifndef MIN..# d
20f91 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
20f92 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29  (x)<(y)?(x):(y))
20f93 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
20f94 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c  ** Structure all
20f95 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
20f96 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
20f97 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 73 71  ...*/..struct sq
20f98 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b 0d 0a  lite3_backup {..
20f99 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 74    sqlite3* pDest
20f9a 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 65  Db;        /* De
20f9b 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
20f9c 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  se handle */..  
20f9d 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20  Btree *pDest;   
20f9e 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
20f9f 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66  ination b-tree f
20fa0 69 6c 65 20 2a 2f 0d 0a 20 20 75 33 32 20 69 44  ile */..  u32 iD
20fa1 65 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20  estSchema;      
20fa2 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73     /* Original s
20fa3 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20  chema cookie in 
20fa4 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0d 0a  destination */..
20fa5 20 20 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65    int bDestLocke
20fa6 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  d;         /* Tr
20fa7 75 65 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d  ue once a write-
20fa8 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
20fa9 70 65 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0d  pen on pDest */.
20faa 0a 0d 0a 20 20 50 67 6e 6f 20 69 4e 65 78 74 3b  ...  Pgno iNext;
20fab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20fac 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
20fad 74 68 65 20 6e 65 78 74 20 73 6f 75 72 63 65 20  the next source 
20fae 70 61 67 65 20 74 6f 20 63 6f 70 79 20 2a 2f 0d  page to copy */.
20faf 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 63  .  sqlite3* pSrc
20fb0 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  Db;         /* S
20fb1 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68  ource database h
20fb2 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 42 74 72 65  andle */..  Btre
20fb3 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20  e *pSrc;        
20fb4 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 62       /* Source b
20fb5 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0d 0a 0d  -tree file */...
20fb6 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
20fb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
20fb8 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72  ackup process er
20fb9 72 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20  ror code */.... 
20fba 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 76 61   /* These two va
20fbb 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 74 20  riables are set 
20fbc 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 74 6f  by every call to
20fbd 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20   backup_step(). 
20fbe 54 68 65 79 20 61 72 65 0d 0a 20 20 2a 2a 20 72  They are..  ** r
20fbf 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20  ead by calls to 
20fc0 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67  backup_remaining
20fc1 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 61  () and backup_pa
20fc2 67 65 63 6f 75 6e 74 28 29 2e 0d 0a 20 20 2a 2f  gecount()...  */
20fc3 0d 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e  ..  Pgno nRemain
20fc4 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ing;         /* 
20fc5 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
20fc6 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0d  left to copy */.
20fc7 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75  .  Pgno nPagecou
20fc8 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  nt;         /* T
20fc9 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
20fca 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0d  ages to copy */.
20fcb 0a 0d 0a 20 20 69 6e 74 20 69 73 41 74 74 61 63  ...  int isAttac
20fcc 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  hed;          /*
20fcd 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b 75   True once backu
20fce 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  p has been regis
20fcf 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 72  tered with pager
20fd0 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62   */..  sqlite3_b
20fd1 61 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20  ackup *pNext;   
20fd2 2f 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61  /* Next backup a
20fd3 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73  ssociated with s
20fd4 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0d 0a  ource pager */..
20fd5 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 48 52  };..../*..** THR
20fd6 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54 45 53  EAD SAFETY NOTES
20fd7 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 4f 6e 63 65  :..**..**   Once
20fd8 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65   it has been cre
20fd9 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75  ated using backu
20fda 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67  p_init(), a sing
20fdb 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  le sqlite3_backu
20fdc 70 0d 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72  p..**   structur
20fdd 65 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  e may be accesse
20fde 64 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73  d via two groups
20fdf 20 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20   of thread-safe 
20fe0 65 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0d 0a 2a  entry points:..*
20fe1 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 20  *..**     * Via 
20fe2 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
20fe3 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e  up_XXX() API fun
20fe4 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65  ction backup_ste
20fe5 70 28 29 20 61 6e 64 20 0d 0a 2a 2a 20 20 20 20  p() and ..**    
20fe6 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 68     backup_finish
20fe7 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 66  (). Both these f
20fe8 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e 20  unctions obtain 
20fe9 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
20fea 61 73 65 0d 0a 2a 2a 20 20 20 20 20 20 20 68 61  ase..**       ha
20feb 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74  ndle mutex and t
20fec 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
20fed 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75  ted with the sou
20fee 72 63 65 20 42 74 53 68 61 72 65 64 20 0d 0a 2a  rce BtShared ..*
20fef 2a 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72  *       structur
20ff0 65 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72  e, in that order
20ff1 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20  ...**..**     * 
20ff2 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 55 70  Via the BackupUp
20ff3 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b 75  date() and Backu
20ff4 70 52 65 73 74 61 72 74 28 29 20 66 75 6e 63 74  pRestart() funct
20ff5 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 65 0d  ions, which are.
20ff6 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65  .**       invoke
20ff7 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 6c  d by the pager l
20ff8 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 76  ayer to report v
20ff9 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 61  arious state cha
20ffa 6e 67 65 73 20 69 6e 0d 0a 2a 2a 20 20 20 20 20  nges in..**     
20ffb 20 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65    the page cache
20ffc 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
20ffd 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
20ffe 62 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0d  base. The mutex.
20fff 0a 2a 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69  .**       associ
21000 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f  ated with the so
21001 75 72 63 65 20 64 61 74 61 62 61 73 65 20 42 74  urce database Bt
21002 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
21003 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 0d 0a 2a   will always ..*
21004 2a 20 20 20 20 20 20 20 62 65 20 68 65 6c 64 20  *       be held 
21005 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 20 74  when either of t
21006 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hese functions a
21007 72 65 20 69 6e 76 6f 6b 65 64 2e 0d 0a 2a 2a 0d  re invoked...**.
21008 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 72 20  .**   The other 
21009 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58  sqlite3_backup_X
2100a 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  XX() API functio
2100b 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d 61 69  ns, backup_remai
2100c 6e 69 6e 67 28 29 20 61 6e 64 0d 0a 2a 2a 20 20  ning() and..**  
2100d 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e   backup_pagecoun
2100e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72 65  t() are not thre
2100f 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f 6e  ad-safe function
21010 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 63  s. If they are c
21011 61 6c 6c 65 64 0d 0a 2a 2a 20 20 20 77 68 69 6c  alled..**   whil
21012 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  e some other thr
21013 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62  ead is calling b
21014 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20  ackup_step() or 
21015 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c  backup_finish(),
21016 0d 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65  ..**   the value
21017 73 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62  s returned may b
21018 65 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65  e invalid. There
21019 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61   is no way for a
2101a 20 63 61 6c 6c 20 74 6f 0d 0a 2a 2a 20 20 20 42   call to..**   B
2101b 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72  ackupUpdate() or
2101c 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 29   BackupRestart()
2101d 20 74 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69   to interfere wi
2101e 74 68 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  th backup_remain
2101f 69 6e 67 28 29 0d 0a 2a 2a 20 20 20 6f 72 20 62  ing()..**   or b
21020 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28  ackup_pagecount(
21021 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 44 65 70  )...**..**   Dep
21022 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51  ending on the SQ
21023 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69  Lite configurati
21024 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  on, the database
21025 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0d   handles and/or.
21026 0a 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20  .**   the Btree 
21027 6f 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65  objects may have
21028 20 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78   their own mutex
21029 65 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20  es that require 
2102a 6c 6f 63 6b 69 6e 67 2e 0d 0a 2a 2a 20 20 20 4e  locking...**   N
2102b 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65  on-sharable Btre
2102c 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  es (in-memory da
2102d 74 61 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d  tabases for exam
2102e 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76  ple), do not hav
2102f 65 0d 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61 74  e..**   associat
21030 65 64 20 6d 75 74 65 78 65 73 2e 0d 0a 2a 2f 0d  ed mutexes...*/.
21031 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
21032 20 61 20 70 6f 69 6e 74 65 72 20 63 6f 72 72 65   a pointer corre
21033 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61  sponding to data
21034 62 61 73 65 20 7a 44 62 20 28 69 2e 65 2e 20 22  base zDb (i.e. "
21035 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 29 0d 0a  main", "temp")..
21036 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ** in connection
21037 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 49 66 20   handle pDb. If 
21038 73 75 63 68 20 61 20 64 61 74 61 62 61 73 65 20  such a database 
21039 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
2103a 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 61 20 4e 55   return..** a NU
2103b 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 77  LL pointer and w
2103c 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
2103d 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f 72 44  ssage to pErrorD
2103e 62 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  b...**..** If th
2103f 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73  e "temp" databas
21040 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  e is requested, 
21041 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  it may need to b
21042 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  e opened by this
21043 20 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20   ..** function. 
21044 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
21045 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73  rs while doing s
21046 6f 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20  o, return 0 and 
21047 77 72 69 74 65 20 61 6e 20 0d 0a 2a 2a 20 65 72  write an ..** er
21048 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
21049 45 72 72 6f 72 44 62 2e 0d 0a 2a 2f 0d 0a 73 74  ErrorDb...*/..st
2104a 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e 64  atic Btree *find
2104b 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a 70  Btree(sqlite3 *p
2104c 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 33  ErrorDb, sqlite3
2104d 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 61   *pDb, const cha
2104e 72 20 2a 7a 44 62 29 7b 0d 0a 20 20 69 6e 74 20  r *zDb){..  int 
2104f 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  i = sqlite3FindD
21050 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b  bName(pDb, zDb);
21051 0d 0a 0d 0a 20 20 69 66 28 20 69 3d 3d 31 20 29  ....  if( i==1 )
21052 7b 0d 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50  {..    Parse *pP
21053 61 72 73 65 3b 0d 0a 20 20 20 20 69 6e 74 20 72  arse;..    int r
21054 63 20 3d 20 30 3b 0d 0a 20 20 20 20 70 50 61 72  c = 0;..    pPar
21055 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
21056 6b 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f  kAllocZero(pErro
21057 72 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61  rDb, sizeof(*pPa
21058 72 73 65 29 29 3b 0d 0a 20 20 20 20 69 66 28 20  rse));..    if( 
21059 70 50 61 72 73 65 3d 3d 30 20 29 7b 0d 0a 20 20  pParse==0 ){..  
2105a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2105b 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54  (pErrorDb, SQLIT
2105c 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66  E_NOMEM, "out of
2105d 20 6d 65 6d 6f 72 79 22 29 3b 0d 0a 20 20 20 20   memory");..    
2105e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2105f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  MEM;..    }else{
21060 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ..      pParse->
21061 64 62 20 3d 20 70 44 62 3b 0d 0a 20 20 20 20 20  db = pDb;..     
21062 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 65 6e   if( sqlite3Open
21063 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61  TempDatabase(pPa
21064 72 73 65 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  rse) ){..       
21065 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45   sqlite3Error(pE
21066 72 72 6f 72 44 62 2c 20 70 50 61 72 73 65 2d 3e  rrorDb, pParse->
21067 72 63 2c 20 22 25 73 22 2c 20 70 50 61 72 73 65  rc, "%s", pParse
21068 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20  ->zErrMsg);..   
21069 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2106a 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 7d  _ERROR;..      }
2106b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
2106c 62 46 72 65 65 28 70 45 72 72 6f 72 44 62 2c 20  bFree(pErrorDb, 
2106d 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29  pParse->zErrMsg)
2106e 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2106f 53 74 61 63 6b 46 72 65 65 28 70 45 72 72 6f 72  StackFree(pError
21070 44 62 2c 20 70 50 61 72 73 65 29 3b 0d 0a 20 20  Db, pParse);..  
21071 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 20    }..    if( rc 
21072 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
21073 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   0;..    }..  }.
21074 0a 0d 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0d  ...  if( i<0 ){.
21075 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
21076 72 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49  r(pErrorDb, SQLI
21077 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f  TE_ERROR, "unkno
21078 77 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c  wn database %s",
21079 20 7a 44 62 29 3b 0d 0a 20 20 20 20 72 65 74 75   zDb);..    retu
2107a 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 0;..  }....  
2107b 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b  return pDb->aDb[
2107c 69 5d 2e 70 42 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  i].pBt;..}..../*
2107d 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ..** Attempt to 
2107e 73 65 74 20 74 68 65 20 70 61 67 65 20 73 69 7a  set the page siz
2107f 65 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61  e of the destina
21080 74 69 6f 6e 20 74 6f 20 6d 61 74 63 68 20 74 68  tion to match th
21081 65 20 70 61 67 65 20 73 69 7a 65 0d 0a 2a 2a 20  e page size..** 
21082 6f 66 20 74 68 65 20 73 6f 75 72 63 65 2e 0d 0a  of the source...
21083 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  */..static int s
21084 65 74 44 65 73 74 50 67 73 7a 28 73 71 6c 69 74  etDestPgsz(sqlit
21085 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0d 0a  e3_backup *p){..
21086 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 72 63 20    int rc;..  rc 
21087 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
21088 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65  tPageSize(p->pDe
21089 73 74 2c 73 71 6c 69 74 65 33 42 74 72 65 65 47  st,sqlite3BtreeG
2108a 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53  etPageSize(p->pS
2108b 72 63 29 2c 2d 31 2c 30 29 3b 0d 0a 20 20 72 65  rc),-1,0);..  re
2108c 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
2108d 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 20  *..** Create an 
2108e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 70  sqlite3_backup p
2108f 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 20 74  rocess to copy t
21090 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a  he contents of z
21091 53 72 63 44 62 20 66 72 6f 6d 0d 0a 2a 2a 20 63  SrcDb from..** c
21092 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
21093 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 74   pSrcDb to zDest
21094 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 49  Db in pDestDb. I
21095 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65  f successful, re
21096 74 75 72 6e 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74  turn..** a point
21097 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71  er to the new sq
21098 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
21099 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ect...**..** If 
2109a 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2109b 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
2109c 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63  d and an error c
2109d 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65  ode and error me
2109e 73 73 61 67 65 0d 0a 2a 2a 20 73 74 6f 72 65 64  ssage..** stored
2109f 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e   in database han
210a0 64 6c 65 20 70 44 65 73 74 44 62 2e 0d 0a 2a 2f  dle pDestDb...*/
210a1 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ..SQLITE_API sql
210a2 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c  ite3_backup *sql
210a3 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
210a4 28 0d 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44  (..  sqlite3* pD
210a5 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20  estDb,          
210a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
210a7 74 61 62 61 73 65 20 74 6f 20 77 72 69 74 65 20  tabase to write 
210a8 74 6f 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  to */..  const c
210a9 68 61 72 20 2a 7a 44 65 73 74 44 62 2c 20 20 20  har *zDestDb,   
210aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
210ab 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
210ac 73 65 20 77 69 74 68 69 6e 20 70 44 65 73 74 44  se within pDestD
210ad 62 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 2a  b */..  sqlite3*
210ae 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 20 20   pSrcDb,        
210af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
210b0 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
210b1 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 72 6f  tion to read fro
210b2 6d 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  m */..  const ch
210b3 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 20  ar *zSrcDb      
210b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
210b5 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
210b6 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 20  e within pSrcDb 
210b7 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
210b8 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20  3_backup *p;    
210b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
210ba 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
210bb 72 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f  rn */....  /* Lo
210bc 63 6b 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  ck the source da
210bd 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54  tabase handle. T
210be 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
210bf 61 74 61 62 61 73 65 0d 0a 20 20 2a 2a 20 68 61  atabase..  ** ha
210c0 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b  ndle is not lock
210c1 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69  ed in this routi
210c2 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 6c 6f  ne, but it is lo
210c3 63 6b 65 64 20 69 6e 0d 0a 20 20 2a 2a 20 73 71  cked in..  ** sq
210c4 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
210c5 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 73  p(). The user is
210c6 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e 73   required to ens
210c7 75 72 65 20 74 68 61 74 20 6e 6f 0d 0a 20 20 2a  ure that no..  *
210c8 2a 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61  * other thread a
210c9 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74  ccesses the dest
210ca 69 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66  ination handle f
210cb 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0d  or the duration.
210cc 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63  .  ** of the bac
210cd 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  kup operation.  
210ce 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75  Any attempt to u
210cf 73 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  se the destinati
210d0 6f 6e 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  on..  ** databas
210d1 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69  e connection whi
210d2 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 20 69  le a backup is i
210d3 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79 20 63  n progress may c
210d4 61 75 73 65 0d 0a 20 20 2a 2a 20 61 20 6d 61 6c  ause..  ** a mal
210d5 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64 65  function or a de
210d6 61 64 6c 6f 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20  adlock...  */.. 
210d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
210d8 6e 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74  nter(pSrcDb->mut
210d9 65 78 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ex);..  sqlite3_
210da 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73  mutex_enter(pDes
210db 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 0d 0a  tDb->mutex);....
210dc 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d 70 44    if( pSrcDb==pD
210dd 65 73 74 44 62 20 29 7b 0d 0a 20 20 20 20 73 71  estDb ){..    sq
210de 6c 69 74 65 33 45 72 72 6f 72 28 0d 0a 20 20 20  lite3Error(..   
210df 20 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 51       pDestDb, SQ
210e0 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75  LITE_ERROR, "sou
210e1 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74  rce and destinat
210e2 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74  ion must be dist
210e3 69 6e 63 74 22 0d 0a 20 20 20 20 29 3b 0d 0a 20  inct"..    );.. 
210e4 20 20 20 70 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c     p = 0;..  }el
210e5 73 65 20 7b 0d 0a 20 20 20 20 2f 2a 20 41 6c 6c  se {..    /* All
210e6 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
210e7 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 61  a new sqlite3_ba
210e8 63 6b 75 70 20 6f 62 6a 65 63 74 2e 2e 2e 0d 0a  ckup object.....
210e9 20 20 20 20 2a 2a 20 45 56 49 44 45 4e 43 45 2d      ** EVIDENCE-
210ea 4f 46 3a 20 52 2d 36 34 38 35 32 2d 32 31 35 39  OF: R-64852-2159
210eb 31 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61  1 The sqlite3_ba
210ec 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63  ckup object is c
210ed 72 65 61 74 65 64 20 62 79 20 61 0d 0a 20 20 20  reated by a..   
210ee 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69   ** call to sqli
210ef 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
210f0 29 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79  ) and is destroy
210f1 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0d  ed by a call to.
210f2 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
210f3 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e  backup_finish().
210f4 20 2a 2f 0d 0a 20 20 20 20 70 20 3d 20 28 73 71   */..    p = (sq
210f5 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73  lite3_backup *)s
210f6 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
210f7 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
210f8 6b 75 70 29 29 3b 0d 0a 20 20 20 20 69 66 28 20  kup));..    if( 
210f9 21 70 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  !p ){..      sql
210fa 69 74 65 33 45 72 72 6f 72 28 70 44 65 73 74 44  ite3Error(pDestD
210fb 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  b, SQLITE_NOMEM,
210fc 20 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d   0);..    }..  }
210fd 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
210fe 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65  allocation succe
210ff 65 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74  eded, populate t
21100 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a  he new object. *
21101 2f 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20  /..  if( p ){.. 
21102 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
21103 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62  sizeof(sqlite3_b
21104 61 63 6b 75 70 29 29 3b 0d 0a 20 20 20 20 70 2d  ackup));..    p-
21105 3e 70 53 72 63 20 3d 20 66 69 6e 64 42 74 72 65  >pSrc = findBtre
21106 65 28 70 44 65 73 74 44 62 2c 20 70 53 72 63 44  e(pDestDb, pSrcD
21107 62 2c 20 7a 53 72 63 44 62 29 3b 0d 0a 20 20 20  b, zSrcDb);..   
21108 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69 6e 64   p->pDest = find
21109 42 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70  Btree(pDestDb, p
2110a 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44 62 29  DestDb, zDestDb)
2110b 3b 0d 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 44  ;..    p->pDestD
2110c 62 20 3d 20 70 44 65 73 74 44 62 3b 0d 0a 20 20  b = pDestDb;..  
2110d 20 20 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53    p->pSrcDb = pS
2110e 72 63 44 62 3b 0d 0a 20 20 20 20 70 2d 3e 69 4e  rcDb;..    p->iN
2110f 65 78 74 20 3d 20 31 3b 0d 0a 20 20 20 20 70 2d  ext = 1;..    p-
21110 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 30 3b  >isAttached = 0;
21111 0d 0a 0d 0a 20 20 20 20 69 66 28 20 30 3d 3d 70  ....    if( 0==p
21112 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e  ->pSrc || 0==p->
21113 70 44 65 73 74 20 7c 7c 20 73 65 74 44 65 73 74  pDest || setDest
21114 50 67 73 7a 28 70 29 3d 3d 53 51 4c 49 54 45 5f  Pgsz(p)==SQLITE_
21115 4e 4f 4d 45 4d 20 29 7b 0d 0a 20 20 20 20 20 20  NOMEM ){..      
21116 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29  /* One (or both)
21117 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61   of the named da
21118 74 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20  tabases did not 
21119 65 78 69 73 74 20 6f 72 20 61 6e 20 4f 4f 4d 0d  exist or an OOM.
2111a 0a 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  .      ** error 
2111b 77 61 73 20 68 69 74 2e 20 20 54 68 65 20 65 72  was hit.  The er
2111c 72 6f 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ror has already 
2111d 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74  been written int
2111e 6f 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  o the..      ** 
2111f 70 44 65 73 74 44 62 20 68 61 6e 64 6c 65 2e 20  pDestDb handle. 
21120 20 41 6c 6c 20 74 68 61 74 20 69 73 20 6c 65 66   All that is lef
21121 74 20 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20  t to do here is 
21122 66 72 65 65 20 74 68 65 0d 0a 20 20 20 20 20 20  free the..      
21123 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
21124 70 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 20 20  p structure...  
21125 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71      */..      sq
21126 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a  lite3_free(p);..
21127 20 20 20 20 20 20 70 20 3d 20 30 3b 0d 0a 20 20        p = 0;..  
21128 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20    }..  }..  if( 
21129 70 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 70 53 72  p ){..    p->pSr
2112a 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0d 0a 20  c->nBackup++;.. 
2112b 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f   }....  sqlite3_
2112c 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73  mutex_leave(pDes
2112d 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  tDb->mutex);..  
2112e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2112f 61 76 65 28 70 53 72 63 44 62 2d 3e 6d 75 74 65  ave(pSrcDb->mute
21130 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b  x);..  return p;
21131 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 72  ..}..../*..** Ar
21132 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61 6e 20  gument rc is an 
21133 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
21134 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  e. Return true i
21135 66 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20  f this error is 
21136 0d 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20  ..** considered 
21137 66 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e 74  fatal if encount
21138 65 72 65 64 20 64 75 72 69 6e 67 20 61 20 62 61  ered during a ba
21139 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ckup operation. 
2113a 41 6c 6c 20 65 72 72 6f 72 73 0d 0a 2a 2a 20 61  All errors..** a
2113b 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61  re considered fa
2113c 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53  tal except for S
2113d 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53  QLITE_BUSY and S
2113e 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0d 0a 2a  QLITE_LOCKED...*
2113f 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  /..static int is
21140 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72  FatalError(int r
21141 63 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 28 72  c){..  return (r
21142 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
21143 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc!=SQLITE_BUSY 
21144 26 26 20 41 4c 57 41 59 53 28 72 63 21 3d 53 51  && ALWAYS(rc!=SQ
21145 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 29 3b 0d 0a  LITE_LOCKED));..
21146 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 61  }..../*..** Para
21147 6d 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70  meter zSrcData p
21148 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65  oints to a buffe
21149 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
2114a 20 64 61 74 61 20 66 6f 72 20 0d 0a 2a 2a 20 70   data for ..** p
2114b 61 67 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20  age iSrcPg from 
2114c 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
2114d 61 73 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64  ase. Copy this d
2114e 61 74 61 20 69 6e 74 6f 20 74 68 65 20 0d 0a 2a  ata into the ..*
2114f 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  * destination da
21150 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  tabase...*/..sta
21151 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 4f 6e  tic int backupOn
21152 65 50 61 67 65 28 73 71 6c 69 74 65 33 5f 62 61  ePage(sqlite3_ba
21153 63 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 69 53  ckup *p, Pgno iS
21154 72 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 20 2a  rcPg, const u8 *
21155 7a 53 72 63 44 61 74 61 29 7b 0d 0a 20 20 50 61  zSrcData){..  Pa
21156 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73  ger * const pDes
21157 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  tPager = sqlite3
21158 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44  BtreePager(p->pD
21159 65 73 74 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 69  est);..  const i
2115a 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d 20 73 71  nt nSrcPgsz = sq
2115b 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
2115c 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0d  eSize(p->pSrc);.
2115d 0a 20 20 69 6e 74 20 6e 44 65 73 74 50 67 73 7a  .  int nDestPgsz
2115e 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
2115f 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44  etPageSize(p->pD
21160 65 73 74 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 69  est);..  const i
21161 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e  nt nCopy = MIN(n
21162 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 74 50 67  SrcPgsz, nDestPg
21163 73 7a 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 69 36  sz);..  const i6
21164 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 69 53  4 iEnd = (i64)iS
21165 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 50 67  rcPg*(i64)nSrcPg
21166 73 7a 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  sz;..#ifdef SQLI
21167 54 45 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 20 20  TE_HAS_CODEC..  
21168 69 6e 74 20 6e 53 72 63 52 65 73 65 72 76 65 20  int nSrcReserve 
21169 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
2116a 74 52 65 73 65 72 76 65 28 70 2d 3e 70 53 72 63  tReserve(p->pSrc
2116b 29 3b 0d 0a 20 20 69 6e 74 20 6e 44 65 73 74 52  );..  int nDestR
2116c 65 73 65 72 76 65 20 3d 20 73 71 6c 69 74 65 33  eserve = sqlite3
2116d 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28  BtreeGetReserve(
2116e 70 2d 3e 70 44 65 73 74 29 3b 0d 0a 23 65 6e 64  p->pDest);..#end
2116f 69 66 0d 0a 0d 0a 20 20 69 6e 74 20 72 63 20 3d  if....  int rc =
21170 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69   SQLITE_OK;..  i
21171 36 34 20 69 4f 66 66 3b 0d 0a 0d 0a 20 20 61 73  64 iOff;....  as
21172 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f  sert( p->bDestLo
21173 63 6b 65 64 20 29 3b 0d 0a 20 20 61 73 73 65 72  cked );..  asser
21174 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72  t( !isFatalError
21175 28 70 2d 3e 72 63 29 20 29 3b 0d 0a 20 20 61 73  (p->rc) );..  as
21176 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45  sert( iSrcPg!=PE
21177 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
21178 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b  p->pSrc->pBt) );
21179 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63  ..  assert( zSrc
2117a 44 61 74 61 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  Data );....  /* 
2117b 43 61 74 63 68 20 74 68 65 20 63 61 73 65 20 77  Catch the case w
2117c 68 65 72 65 20 74 68 65 20 64 65 73 74 69 6e 61  here the destina
2117d 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65  tion is an in-me
2117e 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 61 6e  mory database an
2117f 64 20 74 68 65 0d 0a 20 20 2a 2a 20 70 61 67 65  d the..  ** page
21180 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20 73 6f   sizes of the so
21181 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61  urce and destina
21182 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 0d 0a 20  tion differ. .. 
21183 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 53 72 63 50   */..  if( nSrcP
21184 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26  gsz!=nDestPgsz &
21185 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  & sqlite3PagerIs
21186 4d 65 6d 64 62 28 70 44 65 73 74 50 61 67 65 72  Memdb(pDestPager
21187 29 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  ) ){..    rc = S
21188 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d  QLITE_READONLY;.
21189 0a 20 20 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53  .  }....#ifdef S
2118a 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0d  QLITE_HAS_CODEC.
2118b 0a 20 20 2f 2a 20 42 61 63 6b 75 70 20 69 73 20  .  /* Backup is 
2118c 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 69 66 20  not possible if 
2118d 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6f 66  the page size of
2118e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
2118f 20 69 73 20 63 68 61 6e 67 69 6e 67 0d 0a 20 20   is changing..  
21190 2a 2a 20 61 6e 64 20 61 20 63 6f 64 65 63 20 69  ** and a codec i
21191 73 20 69 6e 20 75 73 65 2e 0d 0a 20 20 2a 2f 0d  s in use...  */.
21192 0a 20 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21  .  if( nSrcPgsz!
21193 3d 6e 44 65 73 74 50 67 73 7a 20 26 26 20 73 71  =nDestPgsz && sq
21194 6c 69 74 65 33 50 61 67 65 72 47 65 74 43 6f 64  lite3PagerGetCod
21195 65 63 28 70 44 65 73 74 50 61 67 65 72 29 21 3d  ec(pDestPager)!=
21196 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  0 ){..    rc = S
21197 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d  QLITE_READONLY;.
21198 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 42 61 63  .  }....  /* Bac
21199 6b 75 70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  kup is not possi
2119a 62 6c 65 20 69 66 20 74 68 65 20 6e 75 6d 62 65  ble if the numbe
2119b 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 65  r of bytes of re
2119c 73 65 72 76 65 20 73 70 61 63 65 20 64 69 66 66  serve space diff
2119d 65 72 0d 0a 20 20 2a 2a 20 62 65 74 77 65 65 6e  er..  ** between
2119e 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   source and dest
2119f 69 6e 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65  ination.  If the
211a0 72 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e  re is a differen
211a1 63 65 2c 20 74 72 79 20 74 6f 0d 0a 20 20 2a 2a  ce, try to..  **
211a2 20 66 69 78 20 74 68 65 20 64 65 73 74 69 6e 61   fix the destina
211a3 74 69 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69  tion to agree wi
211a4 74 68 20 74 68 65 20 73 6f 75 72 63 65 2e 20 20  th the source.  
211a5 49 66 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  If that is not p
211a6 6f 73 73 69 62 6c 65 2c 0d 0a 20 20 2a 2a 20 74  ossible,..  ** t
211a7 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 63  hen the backup c
211a8 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64 2e 0d 0a  annot proceed...
211a9 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 53 72 63    */..  if( nSrc
211aa 52 65 73 65 72 76 65 21 3d 6e 44 65 73 74 52 65  Reserve!=nDestRe
211ab 73 65 72 76 65 20 29 7b 0d 0a 20 20 20 20 75 33  serve ){..    u3
211ac 32 20 6e 65 77 50 67 73 7a 20 3d 20 6e 53 72 63  2 newPgsz = nSrc
211ad 50 67 73 7a 3b 0d 0a 20 20 20 20 72 63 20 3d 20  Pgsz;..    rc = 
211ae 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
211af 61 67 65 73 69 7a 65 28 70 44 65 73 74 50 61 67  agesize(pDestPag
211b0 65 72 2c 20 26 6e 65 77 50 67 73 7a 2c 20 6e 53  er, &newPgsz, nS
211b1 72 63 52 65 73 65 72 76 65 29 3b 0d 0a 20 20 20  rcReserve);..   
211b2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
211b3 4f 4b 20 26 26 20 6e 65 77 50 67 73 7a 21 3d 6e  OK && newPgsz!=n
211b4 53 72 63 50 67 73 7a 20 29 20 72 63 20 3d 20 53  SrcPgsz ) rc = S
211b5 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d  QLITE_READONLY;.
211b6 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
211b7 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72    /* This loop r
211b8 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  uns once for eac
211b9 68 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  h destination pa
211ba 67 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68  ge spanned by th
211bb 65 20 73 6f 75 72 63 65 20 0d 0a 20 20 2a 2a 20  e source ..  ** 
211bc 70 61 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69  page. For each i
211bd 74 65 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62  teration, variab
211be 6c 65 20 69 4f 66 66 20 69 73 20 73 65 74 20 74  le iOff is set t
211bf 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65  o the byte offse
211c0 74 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  t..  ** of the d
211c1 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e  estination page.
211c2 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 4f  ..  */..  for(iO
211c3 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 72  ff=iEnd-(i64)nSr
211c4 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 54  cPgsz; rc==SQLIT
211c5 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e  E_OK && iOff<iEn
211c6 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 67  d; iOff+=nDestPg
211c7 73 7a 29 7b 0d 0a 20 20 20 20 44 62 50 61 67 65  sz){..    DbPage
211c8 20 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0d 0a   *pDestPg = 0;..
211c9 20 20 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d      Pgno iDest =
211ca 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65   (Pgno)(iOff/nDe
211cb 73 74 50 67 73 7a 29 2b 31 3b 0d 0a 20 20 20 20  stPgsz)+1;..    
211cc 69 66 28 20 69 44 65 73 74 3d 3d 50 45 4e 44 49  if( iDest==PENDI
211cd 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e  NG_BYTE_PAGE(p->
211ce 70 44 65 73 74 2d 3e 70 42 74 29 20 29 20 63 6f  pDest->pBt) ) co
211cf 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28  ntinue;..    if(
211d0 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
211d1 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
211d2 74 28 70 44 65 73 74 50 61 67 65 72 2c 20 69 44  t(pDestPager, iD
211d3 65 73 74 2c 20 26 70 44 65 73 74 50 67 29 29 0d  est, &pDestPg)).
211d4 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f  .     && SQLITE_
211d5 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
211d6 33 50 61 67 65 72 57 72 69 74 65 28 70 44 65 73  3PagerWrite(pDes
211d7 74 50 67 29 29 0d 0a 20 20 20 20 29 7b 0d 0a 20  tPg))..    ){.. 
211d8 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a       const u8 *z
211d9 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69  In = &zSrcData[i
211da 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0d 0a  Off%nSrcPgsz];..
211db 20 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44        u8 *zDestD
211dc 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ata = sqlite3Pag
211dd 65 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50  erGetData(pDestP
211de 67 29 3b 0d 0a 20 20 20 20 20 20 75 38 20 2a 7a  g);..      u8 *z
211df 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61  Out = &zDestData
211e0 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d  [iOff%nDestPgsz]
211e1 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43 6f  ;....      /* Co
211e2 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  py the data from
211e3 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
211e4 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e   into the destin
211e5 61 74 69 6f 6e 20 70 61 67 65 2e 0d 0a 20 20 20  ation page...   
211e6 20 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72     ** Then clear
211e7 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72   the Btree layer
211e8 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20   MemPage.isInit 
211e9 66 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20  flag. Both this 
211ea 6d 6f 64 75 6c 65 0d 0a 20 20 20 20 20 20 2a 2a  module..      **
211eb 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 63   and the pager c
211ec 6f 64 65 20 75 73 65 20 74 68 69 73 20 74 72 69  ode use this tri
211ed 63 6b 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65  ck (clearing the
211ee 20 66 69 72 73 74 20 62 79 74 65 0d 0a 20 20 20   first byte..   
211ef 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67     ** of the pag
211f0 65 20 27 65 78 74 72 61 27 20 73 70 61 63 65 20  e 'extra' space 
211f1 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68  to invalidate th
211f2 65 20 42 74 72 65 65 20 6c 61 79 65 72 73 0d 0a  e Btree layers..
211f3 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 64 20        ** cached 
211f4 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 61 67  parse of the pag
211f5 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e  e). MemPage.isIn
211f6 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0d 0a 20  it is marked .. 
211f7 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 45       ** "MUST BE
211f8 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 73   FIRST" for this
211f9 20 70 75 72 70 6f 73 65 2e 0d 0a 20 20 20 20 20   purpose...     
211fa 20 2a 2f 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70   */..      memcp
211fb 79 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f  y(zOut, zIn, nCo
211fc 70 79 29 3b 0d 0a 20 20 20 20 20 20 28 28 75 38  py);..      ((u8
211fd 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
211fe 65 74 45 78 74 72 61 28 70 44 65 73 74 50 67 29  etExtra(pDestPg)
211ff 29 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  )[0] = 0;..    }
21200 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ..    sqlite3Pag
21201 65 72 55 6e 72 65 66 28 70 44 65 73 74 50 67 29  erUnref(pDestPg)
21202 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75  ;..  }....  retu
21203 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
21204 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20  .** If pFile is 
21205 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72  currently larger
21206 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65   than iSize byte
21207 73 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65  s, then truncate
21208 20 69 74 20 74 6f 0d 0a 2a 2a 20 65 78 61 63 74   it to..** exact
21209 6c 79 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20  ly iSize bytes. 
2120a 49 66 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20  If pFile is not 
2120b 6c 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a  larger than iSiz
2120c 65 20 62 79 74 65 73 2c 20 74 68 65 6e 0d 0a 2a  e bytes, then..*
2120d 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
2120e 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d  is a no-op...**.
2120f 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
21210 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
21211 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
21212 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
21213 72 72 6f 72 20 0d 0a 2a 2a 20 63 6f 64 65 20 69  rror ..** code i
21214 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
21215 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
21216 6e 74 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74  nt backupTruncat
21217 65 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69  eFile(sqlite3_fi
21218 6c 65 20 2a 70 46 69 6c 65 2c 20 69 36 34 20 69  le *pFile, i64 i
21219 53 69 7a 65 29 7b 0d 0a 20 20 69 36 34 20 69 43  Size){..  i64 iC
2121a 75 72 72 65 6e 74 3b 0d 0a 20 20 69 6e 74 20 72  urrent;..  int r
2121b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
2121c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26 69 43  eSize(pFile, &iC
2121d 75 72 72 65 6e 74 29 3b 0d 0a 20 20 69 66 28 20  urrent);..  if( 
2121e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
2121f 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65 20   iCurrent>iSize 
21220 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ){..    rc = sql
21221 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
21222 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0d 0a 20  File, iSize);.. 
21223 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
21224 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
21225 67 69 73 74 65 72 20 74 68 69 73 20 62 61 63 6b  gister this back
21226 75 70 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74  up object with t
21227 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f  he associated so
21228 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 0d 0a  urce pager for..
21229 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77 68 65  ** callbacks whe
2122a 6e 20 70 61 67 65 73 20 61 72 65 20 63 68 61 6e  n pages are chan
2122b 67 65 64 20 6f 72 20 74 68 65 20 63 61 63 68 65  ged or the cache
2122c 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0d 0a 2a   invalidated...*
2122d 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  /..static void a
2122e 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63  ttachBackupObjec
2122f 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t(sqlite3_backup
21230 20 2a 70 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33   *p){..  sqlite3
21231 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 0d 0a 20  _backup **pp;.. 
21232 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
21233 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
21234 70 2d 3e 70 53 72 63 29 20 29 3b 0d 0a 20 20 70  p->pSrc) );..  p
21235 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  p = sqlite3Pager
21236 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65  BackupPtr(sqlite
21237 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70  3BtreePager(p->p
21238 53 72 63 29 29 3b 0d 0a 20 20 70 2d 3e 70 4e 65  Src));..  p->pNe
21239 78 74 20 3d 20 2a 70 70 3b 0d 0a 20 20 2a 70 70  xt = *pp;..  *pp
2123a 20 3d 20 70 3b 0d 0a 20 20 70 2d 3e 69 73 41 74   = p;..  p->isAt
2123b 74 61 63 68 65 64 20 3d 20 31 3b 0d 0a 7d 0d 0a  tached = 1;..}..
2123c 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 70 79 20 6e 50  ../*..** Copy nP
2123d 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d 20 74  age pages from t
2123e 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 65 65  he source b-tree
2123f 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74   to the destinat
21240 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ion...*/..SQLITE
21241 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
21242 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c  _backup_step(sql
21243 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20  ite3_backup *p, 
21244 69 6e 74 20 6e 50 61 67 65 29 7b 0d 0a 20 20 69  int nPage){..  i
21245 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74 20 64 65  nt rc;..  int de
21246 73 74 4d 6f 64 65 3b 20 20 20 20 20 20 20 2f 2a  stMode;       /*
21247 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6a 6f 75   Destination jou
21248 72 6e 61 6c 20 6d 6f 64 65 20 2a 2f 0d 0a 20 20  rnal mode */..  
21249 69 6e 74 20 70 67 73 7a 53 72 63 20 3d 20 30 3b  int pgszSrc = 0;
2124a 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61      /* Source pa
2124b 67 65 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 69 6e  ge size */..  in
2124c 74 20 70 67 73 7a 44 65 73 74 20 3d 20 30 3b 20  t pgszDest = 0; 
2124d 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e    /* Destination
2124e 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0d 0a 0d   page size */...
2124f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
21250 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62  _enter(p->pSrcDb
21251 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 73 71 6c  ->mutex);..  sql
21252 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
21253 2d 3e 70 53 72 63 29 3b 0d 0a 20 20 69 66 28 20  ->pSrc);..  if( 
21254 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0d 0a 20  p->pDestDb ){.. 
21255 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
21256 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44  _enter(p->pDestD
21257 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d  b->mutex);..  }.
21258 0a 0d 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b  ...  rc = p->rc;
21259 0d 0a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c  ..  if( !isFatal
2125a 45 72 72 6f 72 28 72 63 29 20 29 7b 0d 0a 20 20  Error(rc) ){..  
2125b 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20    Pager * const 
2125c 70 53 72 63 50 61 67 65 72 20 3d 20 73 71 6c 69  pSrcPager = sqli
2125d 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
2125e 3e 70 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 53  >pSrc);     /* S
2125f 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0d 0a  ource pager */..
21260 20 20 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73      Pager * cons
21261 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73  t pDestPager = s
21262 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
21263 28 70 2d 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a  (p->pDest);   /*
21264 20 44 65 73 74 20 70 61 67 65 72 20 2a 2f 0d 0a   Dest pager */..
21265 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
21266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21267 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
21268 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a  or variable */..
21269 20 20 20 20 69 6e 74 20 6e 53 72 63 50 61 67 65      int nSrcPage
2126a 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20   = -1;          
2126b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
2126c 66 20 73 6f 75 72 63 65 20 64 62 20 69 6e 20 70  f source db in p
2126d 61 67 65 73 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  ages */..    int
2126e 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 30   bCloseTrans = 0
2126f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21270 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 20 64  /* True if src d
21271 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c 6f 63  b requires unloc
21272 6b 69 6e 67 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f  king */....    /
21273 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  * If the source 
21274 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74  pager is current
21275 6c 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72  ly in a write-tr
21276 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72  ansaction, retur
21277 6e 0d 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  n..    ** SQLITE
21278 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c  _BUSY immediatel
21279 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  y...    */..    
2127a 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26  if( p->pDestDb &
2127b 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  & p->pSrc->pBt->
2127c 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
2127d 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0d 0a 20  RANS_WRITE ){.. 
2127e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2127f 5f 42 55 53 59 3b 0d 0a 20 20 20 20 7d 65 6c 73  _BUSY;..    }els
21280 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  e{..      rc = S
21281 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
21282 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20  ....    /* Lock 
21283 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
21284 64 61 74 61 62 61 73 65 2c 20 69 66 20 69 74 20  database, if it 
21285 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c  is not locked al
21286 72 65 61 64 79 2e 20 2a 2f 0d 0a 20 20 20 20 69  ready. */..    i
21287 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63  f( SQLITE_OK==rc
21288 20 26 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b   && p->bDestLock
21289 65 64 3d 3d 30 0d 0a 20 20 20 20 20 26 26 20 53  ed==0..     && S
2128a 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
2128b 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
2128c 6e 54 72 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c  nTrans(p->pDest,
2128d 20 32 29 29 20 0d 0a 20 20 20 20 29 7b 0d 0a 20   2)) ..    ){.. 
2128e 20 20 20 20 20 70 2d 3e 62 44 65 73 74 4c 6f 63       p->bDestLoc
2128f 6b 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  ked = 1;..      
21290 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
21291 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 42 54  eta(p->pDest, BT
21292 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  REE_SCHEMA_VERSI
21293 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 53 63 68  ON, &p->iDestSch
21294 65 6d 61 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  ema);..    }....
21295 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
21296 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d  is no open read-
21297 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
21298 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
21299 73 65 2c 20 6f 70 65 6e 0d 0a 20 20 20 20 2a 2a  se, open..    **
2129a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74   one now. If a t
2129b 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
2129c 65 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20  ened here, then 
2129d 69 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65  it will be close
2129e 64 0d 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65  d..    ** before
2129f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65   this function e
212a0 78 69 74 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  xits...    */.. 
212a1 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
212a2 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74  E_OK && 0==sqlit
212a3 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
212a4 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 7b  rans(p->pSrc) ){
212a5 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ..      rc = sql
212a6 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
212a7 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b  ans(p->pSrc, 0);
212a8 0d 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72  ..      bCloseTr
212a9 61 6e 73 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  ans = 1;..    }.
212aa 0a 0d 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74  ...    /* Do not
212ab 20 61 6c 6c 6f 77 20 62 61 63 6b 75 70 20 69 66   allow backup if
212ac 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
212ad 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20   database is in 
212ae 57 41 4c 20 6d 6f 64 65 0d 0a 20 20 20 20 2a 2a  WAL mode..    **
212af 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 69   and the page si
212b0 7a 65 73 20 61 72 65 20 64 69 66 66 65 72 65 6e  zes are differen
212b1 74 20 62 65 74 77 65 65 6e 20 73 6f 75 72 63 65  t between source
212b2 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
212b3 20 2a 2f 0d 0a 20 20 20 20 70 67 73 7a 53 72 63   */..    pgszSrc
212b4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
212b5 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53  etPageSize(p->pS
212b6 72 63 29 3b 0d 0a 20 20 20 20 70 67 73 7a 44 65  rc);..    pgszDe
212b7 73 74 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  st = sqlite3Btre
212b8 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e  eGetPageSize(p->
212b9 70 44 65 73 74 29 3b 0d 0a 20 20 20 20 64 65 73  pDest);..    des
212ba 74 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 50  tMode = sqlite3P
212bb 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f  agerGetJournalMo
212bc 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  de(sqlite3BtreeP
212bd 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 29 3b  ager(p->pDest));
212be 0d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ..    if( SQLITE
212bf 5f 4f 4b 3d 3d 72 63 20 26 26 20 64 65 73 74 4d  _OK==rc && destM
212c0 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
212c1 41 4c 4d 4f 44 45 5f 57 41 4c 20 26 26 20 70 67  ALMODE_WAL && pg
212c2 73 7a 53 72 63 21 3d 70 67 73 7a 44 65 73 74 20  szSrc!=pgszDest 
212c3 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ){..      rc = S
212c4 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0d  QLITE_READONLY;.
212c5 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20  .    }..  ..    
212c6 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65 72  /* Now that ther
212c7 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  e is a read-lock
212c8 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   on the source d
212c9 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 20 74  atabase, query t
212ca 68 65 0d 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63  he..    ** sourc
212cb 65 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 20  e pager for the 
212cc 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
212cd 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
212ce 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 6e 53  ..    */..    nS
212cf 72 63 50 61 67 65 20 3d 20 28 69 6e 74 29 73 71  rcPage = (int)sq
212d0 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 50 61  lite3BtreeLastPa
212d1 67 65 28 70 2d 3e 70 53 72 63 29 3b 0d 0a 20 20  ge(p->pSrc);..  
212d2 20 20 61 73 73 65 72 74 28 20 6e 53 72 63 50 61    assert( nSrcPa
212d3 67 65 3e 3d 30 20 29 3b 0d 0a 20 20 20 20 66 6f  ge>=0 );..    fo
212d4 72 28 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c 30  r(ii=0; (nPage<0
212d5 20 7c 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 26   || ii<nPage) &&
212d6 20 70 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e 6f   p->iNext<=(Pgno
212d7 29 6e 53 72 63 50 61 67 65 20 26 26 20 21 72 63  )nSrcPage && !rc
212d8 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  ; ii++){..      
212d9 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50  const Pgno iSrcP
212da 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20  g = p->iNext;   
212db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
212dc 20 53 6f 75 72 63 65 20 70 61 67 65 20 6e 75 6d   Source page num
212dd 62 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66  ber */..      if
212de 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e  ( iSrcPg!=PENDIN
212df 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70  G_BYTE_PAGE(p->p
212e0 53 72 63 2d 3e 70 42 74 29 20 29 7b 0d 0a 20 20  Src->pBt) ){..  
212e1 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 53        DbPage *pS
212e2 72 63 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  rcPg;           
212e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
212e4 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65    /* Source page
212e5 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 20 20   object */..    
212e6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
212e7 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67  PagerGet(pSrcPag
212e8 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72  er, iSrcPg, &pSr
212e9 63 50 67 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  cPg);..        i
212ea 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
212eb 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   ){..          r
212ec 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67  c = backupOnePag
212ed 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c  e(p, iSrcPg, sql
212ee 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
212ef 28 70 53 72 63 50 67 29 29 3b 0d 0a 20 20 20 20  (pSrcPg));..    
212f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
212f1 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b  erUnref(pSrcPg);
212f2 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
212f3 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 2d 3e 69     }..      p->i
212f4 4e 65 78 74 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a  Next++;..    }..
212f5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
212f6 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
212f7 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20  p->nPagecount = 
212f8 6e 53 72 63 50 61 67 65 3b 0d 0a 20 20 20 20 20  nSrcPage;..     
212f9 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d   p->nRemaining =
212fa 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69   nSrcPage+1-p->i
212fb 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 69 66 28  Next;..      if(
212fc 20 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e 6f 29   p->iNext>(Pgno)
212fd 6e 53 72 63 50 61 67 65 20 29 7b 0d 0a 20 20 20  nSrcPage ){..   
212fe 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
212ff 5f 44 4f 4e 45 3b 0d 0a 20 20 20 20 20 20 7d 65  _DONE;..      }e
21300 6c 73 65 20 69 66 28 20 21 70 2d 3e 69 73 41 74  lse if( !p->isAt
21301 74 61 63 68 65 64 20 29 7b 0d 0a 20 20 20 20 20  tached ){..     
21302 20 20 20 61 74 74 61 63 68 42 61 63 6b 75 70 4f     attachBackupO
21303 62 6a 65 63 74 28 70 29 3b 0d 0a 20 20 20 20 20  bject(p);..     
21304 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20   }..    }..  .. 
21305 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
21306 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
21307 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73  field in the des
21308 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
21309 65 2e 20 54 68 69 73 0d 0a 20 20 20 20 2a 2a 20  e. This..    ** 
2130a 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  is to make sure 
2130b 74 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 2d  that the schema-
2130c 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64  version really d
2130d 6f 65 73 20 63 68 61 6e 67 65 20 69 6e 0d 0a 20  oes change in.. 
2130e 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77     ** the case w
2130f 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 65 20  here the source 
21310 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  and destination 
21311 64 61 74 61 62 61 73 65 73 20 68 61 76 65 20 74  databases have t
21312 68 65 0d 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20  he..    ** same 
21313 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0d  schema version..
21314 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28  .    */..    if(
21315 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
21316 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   ){..      rc = 
21317 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
21318 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c  teMeta(p->pDest,
21319 31 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61  1,p->iDestSchema
2131a 2b 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  +1);..      if( 
2131b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2131c 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d  ..        if( p-
2131d 3e 70 44 65 73 74 44 62 20 29 7b 0d 0a 20 20 20  >pDestDb ){..   
2131e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2131f 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
21320 61 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 2d 31  a(p->pDestDb, -1
21321 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
21322 20 20 20 20 20 20 20 69 66 28 20 64 65 73 74 4d         if( destM
21323 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
21324 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 7b 0d 0a 20  ALMODE_WAL ){.. 
21325 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
21326 6c 69 74 65 33 42 74 72 65 65 53 65 74 56 65 72  lite3BtreeSetVer
21327 73 69 6f 6e 28 70 2d 3e 70 44 65 73 74 2c 20 32  sion(p->pDest, 2
21328 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
21329 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
2132a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2132b 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ){..        int 
2132c 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0d 0a  nDestTruncate;..
2132d 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e          /* Set n
2132e 44 65 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20  DestTruncate to 
2132f 74 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72  the final number
21330 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
21331 20 64 65 73 74 69 6e 61 74 69 6f 6e 0d 0a 20 20   destination..  
21332 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
21333 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61 74  e. The complicat
21334 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74  ion here is that
21335 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
21336 20 70 61 67 65 0d 0a 20 20 20 20 20 20 20 20 2a   page..        *
21337 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69  * size may be di
21338 66 66 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73  fferent to the s
21339 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e  ource page size.
2133a 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20   ..        **.. 
2133b 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65         ** If the
2133c 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a   source page siz
2133d 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  e is smaller tha
2133e 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  n the destinatio
2133f 6e 20 70 61 67 65 20 73 69 7a 65 2c 20 0d 0a 20  n page size, .. 
21340 20 20 20 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20         ** round 
21341 75 70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  up. In this case
21342 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   the call to sql
21343 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 29  ite3OsTruncate()
21344 20 62 65 6c 6f 77 20 77 69 6c 6c 0d 0a 20 20 20   below will..   
21345 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68 65 20       ** fix the 
21346 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
21347 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69 73 20  . However it is 
21348 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c  important to cal
21349 6c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 71  l..        ** sq
2134a 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
2134b 74 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73  teImage() here s
2134c 6f 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73  o that any pages
2134d 20 69 6e 20 74 68 65 20 0d 0a 20 20 20 20 20 20   in the ..      
2134e 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e    ** destination
2134f 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 62   file that lie b
21350 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 54  eyond the nDestT
21351 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 72  runcate page mar
21352 6b 20 61 72 65 0d 0a 20 20 20 20 20 20 20 20 2a  k are..        *
21353 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20  * journalled by 
21354 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
21355 4f 6e 65 28 29 20 62 65 66 6f 72 65 20 74 68 65  One() before the
21356 79 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 0d  y are destroyed.
21357 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 74  .        ** by t
21358 68 65 20 66 69 6c 65 20 74 72 75 6e 63 61 74 69  he file truncati
21359 6f 6e 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d  on...        */.
2135a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2135b 20 70 67 73 7a 53 72 63 3d 3d 73 71 6c 69 74 65   pgszSrc==sqlite
2135c 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2135d 65 28 70 2d 3e 70 53 72 63 29 20 29 3b 0d 0a 20  e(p->pSrc) );.. 
2135e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2135f 67 73 7a 44 65 73 74 3d 3d 73 71 6c 69 74 65 33  gszDest==sqlite3
21360 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
21361 28 70 2d 3e 70 44 65 73 74 29 20 29 3b 0d 0a 20  (p->pDest) );.. 
21362 20 20 20 20 20 20 20 69 66 28 20 70 67 73 7a 53         if( pgszS
21363 72 63 3c 70 67 73 7a 44 65 73 74 20 29 7b 0d 0a  rc<pgszDest ){..
21364 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 61            int ra
21365 74 69 6f 20 3d 20 70 67 73 7a 44 65 73 74 2f 70  tio = pgszDest/p
21366 67 73 7a 53 72 63 3b 0d 0a 20 20 20 20 20 20 20  gszSrc;..       
21367 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
21368 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74   = (nSrcPage+rat
21369 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0d 0a 20 20  io-1)/ratio;..  
2136a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65 73          if( nDes
2136b 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29  tTruncate==(int)
2136c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
2136d 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29  E(p->pDest->pBt)
2136e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
2136f 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d   nDestTruncate--
21370 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
21371 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
21372 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 54            nDestT
21373 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50 61  runcate = nSrcPa
21374 67 65 20 2a 20 28 70 67 73 7a 53 72 63 2f 70 67  ge * (pgszSrc/pg
21375 73 7a 44 65 73 74 29 3b 0d 0a 20 20 20 20 20 20  szDest);..      
21376 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73 71 6c    }..        sql
21377 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
21378 65 49 6d 61 67 65 28 70 44 65 73 74 50 61 67 65  eImage(pDestPage
21379 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65  r, nDestTruncate
2137a 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69 66  );....        if
2137b 28 20 70 67 73 7a 53 72 63 3c 70 67 73 7a 44 65  ( pgszSrc<pgszDe
2137c 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  st ){..         
2137d 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63   /* If the sourc
2137e 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73  e page-size is s
2137f 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
21380 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65  destination page
21381 2d 73 69 7a 65 2c 0d 0a 20 20 20 20 20 20 20 20  -size,..        
21382 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 61 20 74    ** two extra t
21383 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 64 20 74  hings may need t
21384 6f 20 68 61 70 70 65 6e 3a 0d 0a 20 20 20 20 20  o happen:..     
21385 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20       **..       
21386 20 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65     **   * The de
21387 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65  stination may ne
21388 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74  ed to be truncat
21389 65 64 2c 20 61 6e 64 0d 0a 20 20 20 20 20 20 20  ed, and..       
2138a 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 20     **..         
2138b 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 74 6f   **   * Data sto
2138c 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 73  red on the pages
2138d 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c   immediately fol
2138e 6c 6f 77 69 6e 67 20 74 68 65 20 0d 0a 20 20 20  lowing the ..   
2138f 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70 65         **     pe
21390 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 20  nding-byte page 
21391 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  in the source da
21392 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64 20  tabase may need 
21393 74 6f 20 62 65 0d 0a 20 20 20 20 20 20 20 20 20  to be..         
21394 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69   **     copied i
21395 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74  nto the destinat
21396 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0d 0a 20  ion database... 
21397 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20           */..   
21398 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 36 34         const i64
21399 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 70 67   iSize = (i64)pg
2139a 73 7a 53 72 63 20 2a 20 28 69 36 34 29 6e 53 72  szSrc * (i64)nSr
2139b 63 50 61 67 65 3b 0d 0a 20 20 20 20 20 20 20 20  cPage;..        
2139c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
2139d 20 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73   const pFile = s
2139e 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
2139f 70 44 65 73 74 50 61 67 65 72 29 3b 0d 0a 20 20  pDestPager);..  
213a0 20 20 20 20 20 20 20 20 69 36 34 20 69 4f 66 66          i64 iOff
213a1 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 36 34  ;..          i64
213a2 20 69 45 6e 64 3b 0d 0a 0d 0a 20 20 20 20 20 20   iEnd;....      
213a3 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
213a4 65 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  e );..          
213a5 61 73 73 65 72 74 28 20 28 69 36 34 29 6e 44 65  assert( (i64)nDe
213a6 73 74 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29  stTruncate*(i64)
213a7 70 67 73 7a 44 65 73 74 20 3e 3d 20 69 53 69 7a  pgszDest >= iSiz
213a8 65 20 7c 7c 20 28 0d 0a 20 20 20 20 20 20 20 20  e || (..        
213a9 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75          nDestTru
213aa 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e  ncate==(int)(PEN
213ab 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
213ac 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29  ->pDest->pBt)-1)
213ad 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  ..             &
213ae 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47  & iSize>=PENDING
213af 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 3c 3d  _BYTE && iSize<=
213b0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 70 67 73  PENDING_BYTE+pgs
213b1 7a 44 65 73 74 0d 0a 20 20 20 20 20 20 20 20 20  zDest..         
213b2 20 29 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20   ));....        
213b3 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 65    /* This call e
213b4 6e 73 75 72 65 73 20 74 68 61 74 20 61 6c 6c 20  nsures that all 
213b5 64 61 74 61 20 72 65 71 75 69 72 65 64 20 74 6f  data required to
213b6 20 72 65 63 72 65 61 74 65 20 74 68 65 20 6f 72   recreate the or
213b7 69 67 69 6e 61 6c 0d 0a 20 20 20 20 20 20 20 20  iginal..        
213b8 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61    ** database ha
213b9 73 20 62 65 65 6e 20 73 74 6f 72 65 64 20 69 6e  s been stored in
213ba 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72   the journal for
213bb 20 70 44 65 73 74 50 61 67 65 72 20 61 6e 64 20   pDestPager and 
213bc 74 68 65 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  the..          *
213bd 2a 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64  * journal synced
213be 20 74 6f 20 64 69 73 6b 2e 20 53 6f 20 61 74 20   to disk. So at 
213bf 74 68 69 73 20 70 6f 69 6e 74 20 77 65 20 6d 61  this point we ma
213c0 79 20 73 61 66 65 6c 79 20 6d 6f 64 69 66 79 0d  y safely modify.
213c1 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
213c2 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
213c3 69 6e 20 61 6e 79 20 77 61 79 2c 20 6b 6e 6f 77  in any way, know
213c4 69 6e 67 20 74 68 61 74 20 69 66 20 61 20 70 6f  ing that if a po
213c5 77 65 72 20 66 61 69 6c 75 72 65 0d 0a 20 20 20  wer failure..   
213c6 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73         ** occurs
213c7 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  , the original d
213c8 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20  atabase will be 
213c9 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  reconstructed fr
213ca 6f 6d 20 74 68 65 20 0d 0a 20 20 20 20 20 20 20  om the ..       
213cb 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69     ** journal fi
213cc 6c 65 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20  le.  */..       
213cd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
213ce 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
213cf 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30  ne(pDestPager, 0
213d0 2c 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  , 1);....       
213d1 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
213d2 65 78 74 72 61 20 70 61 67 65 73 20 61 6e 64 20  extra pages and 
213d3 74 72 75 6e 63 61 74 65 20 74 68 65 20 64 61 74  truncate the dat
213d4 61 62 61 73 65 20 66 69 6c 65 20 61 73 20 72 65  abase file as re
213d5 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 20 20 20  quired */..     
213d6 20 20 20 20 20 69 45 6e 64 20 3d 20 4d 49 4e 28       iEnd = MIN(
213d7 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20 70  PENDING_BYTE + p
213d8 67 73 7a 44 65 73 74 2c 20 69 53 69 7a 65 29 3b  gszDest, iSize);
213d9 0d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ..          for(
213da 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f  ..            iO
213db 66 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b  ff=PENDING_BYTE+
213dc 70 67 73 7a 53 72 63 3b 20 0d 0a 20 20 20 20 20  pgszSrc; ..     
213dd 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
213de 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e  E_OK && iOff<iEn
213df 64 3b 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  d; ..           
213e0 20 69 4f 66 66 2b 3d 70 67 73 7a 53 72 63 0d 0a   iOff+=pgszSrc..
213e1 20 20 20 20 20 20 20 20 20 20 29 7b 0d 0a 20 20            ){..  
213e2 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 20            PgHdr 
213e3 2a 70 53 72 63 50 67 20 3d 20 30 3b 0d 0a 20 20  *pSrcPg = 0;..  
213e4 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
213e5 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 50  Pgno iSrcPg = (P
213e6 67 6e 6f 29 28 28 69 4f 66 66 2f 70 67 73 7a 53  gno)((iOff/pgszS
213e7 72 63 29 2b 31 29 3b 0d 0a 20 20 20 20 20 20 20  rc)+1);..       
213e8 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
213e9 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61  3PagerGet(pSrcPa
213ea 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53  ger, iSrcPg, &pS
213eb 72 63 50 67 29 3b 0d 0a 20 20 20 20 20 20 20 20  rcPg);..        
213ec 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
213ed 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
213ee 20 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74          u8 *zDat
213ef 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  a = sqlite3Pager
213f0 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b  GetData(pSrcPg);
213f1 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
213f2 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
213f3 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61  ite(pFile, zData
213f4 2c 20 70 67 73 7a 53 72 63 2c 20 69 4f 66 66 29  , pgszSrc, iOff)
213f5 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
213f6 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ..            sq
213f7 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
213f8 70 53 72 63 50 67 29 3b 0d 0a 20 20 20 20 20 20  pSrcPg);..      
213f9 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
213fa 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
213fb 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
213fc 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 54 72     rc = backupTr
213fd 75 6e 63 61 74 65 46 69 6c 65 28 70 46 69 6c 65  uncateFile(pFile
213fe 2c 20 69 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20  , iSize);..     
213ff 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20       }....      
21400 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20      /* Sync the 
21401 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
21402 20 64 69 73 6b 2e 20 2a 2f 0d 0a 20 20 20 20 20   disk. */..     
21403 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
21404 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
21405 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
21406 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65  te3PagerSync(pDe
21407 73 74 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 20  stPager);..     
21408 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
21409 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2140a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
2140b 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  gerCommitPhaseOn
2140c 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c  e(pDestPager, 0,
2140d 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   0);..        }.
2140e 0a 20 20 20 20 0d 0a 20 20 20 20 20 20 20 20 2f  .    ..        /
2140f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74  * Finish committ
21410 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74  ing the transact
21411 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 74 69  ion to the desti
21412 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
21413 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28   */..        if(
21414 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0d 0a   SQLITE_OK==rc..
21415 20 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49           && SQLI
21416 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
21417 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
21418 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74  haseTwo(p->pDest
21419 2c 20 30 29 29 0d 0a 20 20 20 20 20 20 20 20 29  , 0))..        )
2141a 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  {..          rc 
2141b 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a  = SQLITE_DONE;..
2141c 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2141d 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20   }..    }..  .. 
2141e 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54     /* If bCloseT
2141f 72 61 6e 73 20 69 73 20 74 72 75 65 2c 20 74 68  rans is true, th
21420 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
21421 20 6f 70 65 6e 65 64 20 61 20 72 65 61 64 20 74   opened a read t
21422 72 61 6e 73 61 63 74 69 6f 6e 0d 0a 20 20 20 20  ransaction..    
21423 2a 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65  ** on the source
21424 20 64 61 74 61 62 61 73 65 2e 20 43 6c 6f 73 65   database. Close
21425 20 74 68 65 20 72 65 61 64 20 74 72 61 6e 73 61   the read transa
21426 63 74 69 6f 6e 20 68 65 72 65 2e 20 54 68 65 72  ction here. Ther
21427 65 20 69 73 0d 0a 20 20 20 20 2a 2a 20 6e 6f 20  e is..    ** no 
21428 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74 68  need to check th
21429 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20  e return values 
2142a 6f 66 20 74 68 65 20 62 74 72 65 65 20 6d 65 74  of the btree met
2142b 68 6f 64 73 20 68 65 72 65 2c 20 61 73 0d 0a 20  hods here, as.. 
2142c 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e     ** "committin
2142d 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74  g" a read-only t
2142e 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f  ransaction canno
2142f 74 20 66 61 69 6c 2e 0d 0a 20 20 20 20 2a 2f 0d  t fail...    */.
21430 0a 20 20 20 20 69 66 28 20 62 43 6c 6f 73 65 54  .    if( bCloseT
21431 72 61 6e 73 20 29 7b 0d 0a 20 20 20 20 20 20 54  rans ){..      T
21432 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32  ESTONLY( int rc2
21433 20 29 3b 0d 0a 20 20 20 20 20 20 54 45 53 54 4f   );..      TESTO
21434 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73 71  NLY( rc2  = ) sq
21435 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
21436 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72 63  PhaseOne(p->pSrc
21437 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 54 45 53  , 0);..      TES
21438 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20  TONLY( rc2 |= ) 
21439 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
2143a 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53  itPhaseTwo(p->pS
2143b 72 63 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 61  rc, 0);..      a
2143c 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49  ssert( rc2==SQLI
2143d 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 7d 0d  TE_OK );..    }.
2143e 0a 20 20 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  .  ..    if( rc=
2143f 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
21440 4d 45 4d 20 29 7b 0d 0a 20 20 20 20 20 20 72 63  MEM ){..      rc
21441 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
21442 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d 3e  ..    }..    p->
21443 72 63 20 3d 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20  rc = rc;..  }.. 
21444 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
21445 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
21446 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70  mutex_leave(p->p
21447 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0d  DestDb->mutex);.
21448 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 42  .  }..  sqlite3B
21449 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72  treeLeave(p->pSr
2144a 63 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  c);..  sqlite3_m
2144b 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53  utex_leave(p->pS
2144c 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  rcDb->mutex);.. 
2144d 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
2144e 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65  ../*..** Release
2144f 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61   all resources a
21450 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
21451 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
21452 2a 20 68 61 6e 64 6c 65 2e 0d 0a 2a 2f 0d 0a 53  * handle...*/..S
21453 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
21454 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
21455 69 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ish(sqlite3_back
21456 75 70 20 2a 70 29 7b 0d 0a 20 20 73 71 6c 69 74  up *p){..  sqlit
21457 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20  e3_backup **pp; 
21458 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21459 2f 2a 20 50 74 72 20 74 6f 20 68 65 61 64 20 6f  /* Ptr to head o
2145a 66 20 70 61 67 65 72 73 20 62 61 63 6b 75 70 20  f pagers backup 
2145b 6c 69 73 74 20 2a 2f 0d 0a 20 20 4d 55 54 45 58  list */..  MUTEX
2145c 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f  _LOGIC( sqlite3_
2145d 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 29 20  mutex *mutex; ) 
2145e 2f 2a 20 4d 75 74 65 78 20 74 6f 20 70 72 6f 74  /* Mutex to prot
2145f 65 63 74 20 73 6f 75 72 63 65 20 64 61 74 61 62  ect source datab
21460 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63  ase */..  int rc
21461 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21463 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
21464 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 45 6e 74  n */....  /* Ent
21465 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 20 2a  er the mutexes *
21466 2f 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  /..  if( p==0 ) 
21467 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
21468 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
21469 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63  ex_enter(p->pSrc
2146a 44 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 73  Db->mutex);..  s
2146b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
2146c 28 70 2d 3e 70 53 72 63 29 3b 0d 0a 20 20 4d 55  (p->pSrc);..  MU
2146d 54 45 58 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78  TEX_LOGIC( mutex
2146e 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75   = p->pSrcDb->mu
2146f 74 65 78 3b 20 29 0d 0a 20 20 69 66 28 20 70 2d  tex; )..  if( p-
21470 3e 70 44 65 73 74 44 62 20 29 7b 0d 0a 20 20 20  >pDestDb ){..   
21471 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
21472 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d  nter(p->pDestDb-
21473 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a 0d  >mutex);..  }...
21474 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69  .  /* Detach thi
21475 73 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68  s backup from th
21476 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20  e source pager. 
21477 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65  */..  if( p->pDe
21478 73 74 44 62 20 29 7b 0d 0a 20 20 20 20 70 2d 3e  stDb ){..    p->
21479 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b  pSrc->nBackup--;
2147a 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e  ..  }..  if( p->
2147b 69 73 41 74 74 61 63 68 65 64 20 29 7b 0d 0a 20  isAttached ){.. 
2147c 20 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50     pp = sqlite3P
2147d 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71  agerBackupPtr(sq
2147e 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
2147f 70 2d 3e 70 53 72 63 29 29 3b 0d 0a 20 20 20 20  p->pSrc));..    
21480 77 68 69 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b  while( *pp!=p ){
21481 0d 0a 20 20 20 20 20 20 70 70 20 3d 20 26 28 2a  ..      pp = &(*
21482 70 70 29 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20  pp)->pNext;..   
21483 20 7d 0d 0a 20 20 20 20 2a 70 70 20 3d 20 70 2d   }..    *pp = p-
21484 3e 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a  >pNext;..  }....
21485 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61    /* If a transa
21486 63 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f  ction is still o
21487 70 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65  pen on the Btree
21488 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20  , roll it back. 
21489 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  */..  sqlite3Btr
2148a 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44  eeRollback(p->pD
2148b 65 73 74 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 65  est);....  /* Se
2148c 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  t the error code
2148d 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   of the destinat
2148e 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e  ion database han
2148f 64 6c 65 2e 20 2a 2f 0d 0a 20 20 72 63 20 3d 20  dle. */..  rc = 
21490 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44  (p->rc==SQLITE_D
21491 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  ONE) ? SQLITE_OK
21492 20 3a 20 70 2d 3e 72 63 3b 0d 0a 20 20 73 71 6c   : p->rc;..  sql
21493 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65  ite3Error(p->pDe
21494 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0d 0a 0d  stDb, rc, 0);...
21495 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d  .  /* Exit the m
21496 75 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20  utexes and free 
21497 74 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65  the backup conte
21498 78 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f  xt structure. */
21499 0d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  ..  if( p->pDest
2149a 44 62 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  Db ){..    sqlit
2149b 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
2149c 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78  ->pDestDb->mutex
2149d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
2149e 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e  e3BtreeLeave(p->
2149f 70 53 72 63 29 3b 0d 0a 20 20 69 66 28 20 70 2d  pSrc);..  if( p-
214a0 3e 70 44 65 73 74 44 62 20 29 7b 0d 0a 20 20 20  >pDestDb ){..   
214a1 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d 4f 46 3a   /* EVIDENCE-OF:
214a2 20 52 2d 36 34 38 35 32 2d 32 31 35 39 31 20 54   R-64852-21591 T
214a3 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
214a4 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61  p object is crea
214a5 74 65 64 20 62 79 20 61 0d 0a 20 20 20 20 2a 2a  ted by a..    **
214a6 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
214a7 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 61  _backup_init() a
214a8 6e 64 20 69 73 20 64 65 73 74 72 6f 79 65 64 20  nd is destroyed 
214a9 62 79 20 61 20 63 61 6c 6c 20 74 6f 0d 0a 20 20  by a call to..  
214aa 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63    ** sqlite3_bac
214ab 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 2a 2f  kup_finish(). */
214ac 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
214ad 65 65 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73  ee(p);..  }..  s
214ae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
214af 76 65 28 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  ve(mutex);..  re
214b0 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
214b1 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
214b2 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
214b3 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63   still to be bac
214b4 6b 65 64 20 75 70 20 61 73 20 6f 66 20 74 68 65  ked up as of the
214b5 20 6d 6f 73 74 20 72 65 63 65 6e 74 0d 0a 2a 2a   most recent..**
214b6 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
214b7 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0d  _backup_step()..
214b8 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
214b9 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  int sqlite3_back
214ba 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c  up_remaining(sql
214bb 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b  ite3_backup *p){
214bc 0d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52  ..  return p->nR
214bd 65 6d 61 69 6e 69 6e 67 3b 0d 0a 7d 0d 0a 0d 0a  emaining;..}....
214be 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
214bf 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
214c0 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73  f pages in the s
214c1 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 61  ource database a
214c2 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 0d 0a  s of the most ..
214c3 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  ** recent call t
214c4 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
214c5 5f 73 74 65 70 28 29 2e 0d 0a 2a 2f 0d 0a 53 51  _step()...*/..SQ
214c6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
214c7 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65  ite3_backup_page
214c8 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61  count(sqlite3_ba
214c9 63 6b 75 70 20 2a 70 29 7b 0d 0a 20 20 72 65 74  ckup *p){..  ret
214ca 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e  urn p->nPagecoun
214cb 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  t;..}..../*..** 
214cc 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
214cd 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68   called after th
214ce 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61  e contents of pa
214cf 67 65 20 69 50 61 67 65 20 6f 66 20 74 68 65 0d  ge iPage of the.
214d0 0a 2a 2a 20 73 6f 75 72 63 65 20 64 61 74 61 62  .** source datab
214d1 61 73 65 20 68 61 76 65 20 62 65 65 6e 20 6d 6f  ase have been mo
214d2 64 69 66 69 65 64 2e 20 49 66 20 70 61 67 65 20  dified. If page 
214d3 69 50 61 67 65 20 68 61 73 20 61 6c 72 65 61 64  iPage has alread
214d4 79 20 62 65 65 6e 20 0d 0a 2a 2a 20 63 6f 70 69  y been ..** copi
214d5 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74  ed into the dest
214d6 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
214d7 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20  , then the data 
214d8 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0d 0a  written to the..
214d9 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  ** destination i
214da 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64 61 74 65  s now invalidate
214db 64 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 69  d. The destinati
214dc 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50 61 67 65  on copy of iPage
214dd 20 6e 65 65 64 73 0d 0a 2a 2a 20 74 6f 20 62 65   needs..** to be
214de 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
214df 65 20 6e 65 77 20 64 61 74 61 20 62 65 66 6f 72  e new data befor
214e0 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  e the backup ope
214e1 72 61 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 63 6f  ration is..** co
214e2 6d 70 6c 65 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  mplete...**..** 
214e3 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
214e4 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  at the mutex ass
214e5 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
214e6 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
214e7 0d 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69  ..** correspondi
214e8 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65  ng to the source
214e9 20 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c   database is hel
214ea 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  d when this func
214eb 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c  tion is..** call
214ec 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ed...*/..SQLITE_
214ed 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
214ee 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65  ite3BackupUpdate
214ef 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
214f0 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69  *pBackup, Pgno i
214f1 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a  Page, const u8 *
214f2 61 44 61 74 61 29 7b 0d 0a 20 20 73 71 6c 69 74  aData){..  sqlit
214f3 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20  e3_backup *p;   
214f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214f5 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
214f6 61 62 6c 65 20 2a 2f 0d 0a 20 20 66 6f 72 28 70  able */..  for(p
214f7 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70  =pBackup; p; p=p
214f8 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 61  ->pNext){..    a
214f9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
214fa 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72  utex_held(p->pSr
214fb 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  c->pBt->mutex) )
214fc 3b 0d 0a 20 20 20 20 69 66 28 20 21 69 73 46 61  ;..    if( !isFa
214fd 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20  talError(p->rc) 
214fe 26 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78  && iPage<p->iNex
214ff 74 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54  t ){..      /* T
21500 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73  he backup proces
21501 73 20 70 20 68 61 73 20 61 6c 72 65 61 64 79 20  s p has already 
21502 63 6f 70 69 65 64 20 70 61 67 65 20 69 50 61 67  copied page iPag
21503 65 2e 20 42 75 74 20 6e 6f 77 20 69 74 0d 0a 20  e. But now it.. 
21504 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e       ** has been
21505 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 20 74   modified by a t
21506 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
21507 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20  e source pager. 
21508 43 6f 70 79 0d 0a 20 20 20 20 20 20 2a 2a 20 74  Copy..      ** t
21509 68 65 20 6e 65 77 20 64 61 74 61 20 69 6e 74 6f  he new data into
2150a 20 74 68 65 20 62 61 63 6b 75 70 2e 0d 0a 20 20   the backup...  
2150b 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e      */..      in
2150c 74 20 72 63 3b 0d 0a 20 20 20 20 20 20 61 73 73  t rc;..      ass
2150d 65 72 74 28 20 70 2d 3e 70 44 65 73 74 44 62 20  ert( p->pDestDb 
2150e 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
2150f 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
21510 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  >pDestDb->mutex)
21511 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61  ;..      rc = ba
21512 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69  ckupOnePage(p, i
21513 50 61 67 65 2c 20 61 44 61 74 61 29 3b 0d 0a 20  Page, aData);.. 
21514 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
21515 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73  ex_leave(p->pDes
21516 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  tDb->mutex);..  
21517 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
21518 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72  SQLITE_BUSY && r
21519 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  c!=SQLITE_LOCKED
2151a 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72   );..      if( r
2151b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
2151c 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  .        p->rc =
2151d 20 72 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20   rc;..      }.. 
2151e 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
2151f 2f 2a 0d 0a 2a 2a 20 52 65 73 74 61 72 74 20 74  /*..** Restart t
21520 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73  he backup proces
21521 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  s. This is calle
21522 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  d when the pager
21523 20 6c 61 79 65 72 0d 0a 2a 2a 20 64 65 74 65 63   layer..** detec
21524 74 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ts that the data
21525 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f  base has been mo
21526 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 78 74  dified by an ext
21527 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0d 0a  ernal database..
21528 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49  ** connection. I
21529 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
2152a 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b  e is no way of k
2152b 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20  nowing which of 
2152c 74 68 65 0d 0a 2a 2a 20 70 61 67 65 73 20 74 68  the..** pages th
2152d 61 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70  at have been cop
2152e 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73  ied into the des
2152f 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
21530 65 20 61 72 65 20 73 74 69 6c 6c 20 0d 0a 2a 2a  e are still ..**
21531 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68   valid and which
21532 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65   are not, so the
21533 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20   entire process 
21534 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74  needs to be rest
21535 61 72 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  arted...**..** I
21536 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
21537 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  t the mutex asso
21538 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
21539 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0d  BtShared object.
2153a 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
2153b 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20  g to the source 
2153c 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64  database is held
2153d 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
2153e 69 6f 6e 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65  ion is..** calle
2153f 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
21540 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21541 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
21542 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
21543 2a 70 42 61 63 6b 75 70 29 7b 0d 0a 20 20 73 71  *pBackup){..  sq
21544 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b  lite3_backup *p;
21545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21546 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
21547 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 66 6f  ariable */..  fo
21548 72 28 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20  r(p=pBackup; p; 
21549 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20  p=p->pNext){..  
2154a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2154b 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
2154c 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  pSrc->pBt->mutex
2154d 29 20 29 3b 0d 0a 20 20 20 20 70 2d 3e 69 4e 65  ) );..    p->iNe
2154e 78 74 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d  xt = 1;..  }..}.
2154f 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
21550 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0d 0a 2f  E_OMIT_VACUUM../
21551 2a 0d 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63  *..** Copy the c
21552 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20  omplete content 
21553 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20  of pBtFrom into 
21554 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61  pBtTo.  A transa
21555 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75 73 74 20 62  ction..** must b
21556 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f 74  e active for bot
21557 68 20 66 69 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  h files...**..**
21558 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c   The size of fil
21559 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64  e pTo may be red
2155a 75 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65  uced by this ope
2155b 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68  ration. If anyth
2155c 69 6e 67 20 0d 0a 2a 2a 20 67 6f 65 73 20 77 72  ing ..** goes wr
2155d 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63  ong, the transac
2155e 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72  tion on pTo is r
2155f 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73  olled back. If s
21560 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0d  uccessful, the .
21561 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
21562 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66  is committed bef
21563 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  ore returning...
21564 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
21565 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
21566 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65  reeCopyFile(Btre
21567 65 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70  e *pTo, Btree *p
21568 46 72 6f 6d 29 7b 0d 0a 20 20 69 6e 74 20 72 63  From){..  int rc
21569 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  ;..  sqlite3_fil
2156a 65 20 2a 70 46 64 3b 20 20 20 20 20 20 20 20 20  e *pFd;         
2156b 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
2156c 63 72 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61  criptor for data
2156d 62 61 73 65 20 70 54 6f 20 2a 2f 0d 0a 20 20 73  base pTo */..  s
2156e 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b  qlite3_backup b;
2156f 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
21570 45 6e 74 65 72 28 70 54 6f 29 3b 0d 0a 20 20 73  Enter(pTo);..  s
21571 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
21572 28 70 46 72 6f 6d 29 3b 0d 0a 0d 0a 20 20 61 73  (pFrom);....  as
21573 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
21574 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54 6f 29  eeIsInTrans(pTo)
21575 20 29 3b 0d 0a 20 20 70 46 64 20 3d 20 73 71 6c   );..  pFd = sql
21576 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 73 71  ite3PagerFile(sq
21577 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
21578 70 54 6f 29 29 3b 0d 0a 20 20 69 66 28 20 70 46  pTo));..  if( pF
21579 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0d 0a  d->pMethods ){..
2157a 20 20 20 20 69 36 34 20 6e 42 79 74 65 20 3d 20      i64 nByte = 
2157b 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
2157c 61 67 65 53 69 7a 65 28 70 46 72 6f 6d 29 2a 28  ageSize(pFrom)*(
2157d 69 36 34 29 73 71 6c 69 74 65 33 42 74 72 65 65  i64)sqlite3Btree
2157e 4c 61 73 74 50 61 67 65 28 70 46 72 6f 6d 29 3b  LastPage(pFrom);
2157f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
21580 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
21581 70 46 64 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54  pFd, SQLITE_FCNT
21582 4c 5f 4f 56 45 52 57 52 49 54 45 2c 20 26 6e 42  L_OVERWRITE, &nB
21583 79 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  yte);..    if( r
21584 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55  c==SQLITE_NOTFOU
21585 4e 44 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45  ND ) rc = SQLITE
21586 5f 4f 4b 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  _OK;..    if( rc
21587 20 29 20 67 6f 74 6f 20 63 6f 70 79 5f 66 69 6e   ) goto copy_fin
21588 69 73 68 65 64 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ished;..  }.... 
21589 20 2f 2a 20 53 65 74 20 75 70 20 61 6e 20 73 71   /* Set up an sq
2158a 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
2158b 65 63 74 2e 20 73 71 6c 69 74 65 33 5f 62 61 63  ect. sqlite3_bac
2158c 6b 75 70 2e 70 44 65 73 74 44 62 20 6d 75 73 74  kup.pDestDb must
2158d 20 62 65 20 73 65 74 0d 0a 20 20 2a 2a 20 74 6f   be set..  ** to
2158e 20 30 2e 20 54 68 69 73 20 69 73 20 75 73 65 64   0. This is used
2158f 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
21590 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  tations of sqlit
21591 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
21592 0d 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c 69 74  ..  ** and sqlit
21593 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
21594 28 29 20 74 6f 20 64 65 74 65 63 74 20 74 68 61  () to detect tha
21595 74 20 74 68 65 79 20 61 72 65 20 62 65 69 6e 67  t they are being
21596 20 63 61 6c 6c 65 64 0d 0a 20 20 2a 2a 20 66 72   called..  ** fr
21597 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  om this function
21598 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20 62  , not directly b
21599 79 20 74 68 65 20 75 73 65 72 2e 0d 0a 20 20 2a  y the user...  *
2159a 2f 0d 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20  /..  memset(&b, 
2159b 30 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0d 0a  0, sizeof(b));..
2159c 20 20 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72    b.pSrcDb = pFr
2159d 6f 6d 2d 3e 64 62 3b 0d 0a 20 20 62 2e 70 53 72  om->db;..  b.pSr
2159e 63 20 3d 20 70 46 72 6f 6d 3b 0d 0a 20 20 62 2e  c = pFrom;..  b.
2159f 70 44 65 73 74 20 3d 20 70 54 6f 3b 0d 0a 20 20  pDest = pTo;..  
215a0 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0d 0a 0d 0a  b.iNext = 1;....
215a1 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46 20    /* 0x7FFFFFFF 
215a2 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69  is the hard limi
215a3 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72  t for the number
215a4 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20 64   of pages in a d
215a5 61 74 61 62 61 73 65 0d 0a 20 20 2a 2a 20 66 69  atabase..  ** fi
215a6 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74  le. By passing t
215a7 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65  his as the numbe
215a8 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f  r of pages to co
215a9 70 79 20 74 6f 0d 0a 20 20 2a 2a 20 73 71 6c 69  py to..  ** sqli
215aa 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
215ab 29 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e  ), we can guaran
215ac 74 65 65 20 74 68 61 74 20 74 68 65 20 63 6f 70  tee that the cop
215ad 79 20 66 69 6e 69 73 68 65 73 20 0d 0a 20 20 2a  y finishes ..  *
215ae 2a 20 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c  * within a singl
215af 65 20 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61  e call (unless a
215b0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e  n error occurs).
215b1 20 54 68 65 20 61 73 73 65 72 74 28 29 20 73 74   The assert() st
215b2 61 74 65 6d 65 6e 74 0d 0a 20 20 2a 2a 20 63 68  atement..  ** ch
215b3 65 63 6b 73 20 74 68 69 73 20 61 73 73 75 6d 70  ecks this assump
215b4 74 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73  tion - (p->rc) s
215b5 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
215b6 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 4f  either SQLITE_DO
215b7 4e 45 20 0d 0a 20 20 2a 2a 20 6f 72 20 61 6e 20  NE ..  ** or an 
215b8 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 20 20 2a  error code...  *
215b9 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  /..  sqlite3_bac
215ba 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30 78 37  kup_step(&b, 0x7
215bb 46 46 46 46 46 46 46 29 3b 0d 0a 20 20 61 73 73  FFFFFFF);..  ass
215bc 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49 54  ert( b.rc!=SQLIT
215bd 45 5f 4f 4b 20 29 3b 0d 0a 20 20 72 63 20 3d 20  E_OK );..  rc = 
215be 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
215bf 69 6e 69 73 68 28 26 62 29 3b 0d 0a 20 20 69 66  inish(&b);..  if
215c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
215c1 29 7b 0d 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74  ){..    pTo->pBt
215c2 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d 20 7e 42  ->btsFlags &= ~B
215c3 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45  TS_PAGESIZE_FIXE
215c4 44 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  D;..  }else{..  
215c5 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c    sqlite3PagerCl
215c6 65 61 72 43 61 63 68 65 28 73 71 6c 69 74 65 33  earCache(sqlite3
215c7 42 74 72 65 65 50 61 67 65 72 28 62 2e 70 44 65  BtreePager(b.pDe
215c8 73 74 29 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  st));..  }....  
215c9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
215ca 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54  treeIsInTrans(pT
215cb 6f 29 3d 3d 30 20 29 3b 0d 0a 63 6f 70 79 5f 66  o)==0 );..copy_f
215cc 69 6e 69 73 68 65 64 3a 0d 0a 20 20 73 71 6c 69  inished:..  sqli
215cd 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 46  te3BtreeLeave(pF
215ce 72 6f 6d 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  rom);..  sqlite3
215cf 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3b  BtreeLeave(pTo);
215d0 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
215d1 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
215d2 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20  ITE_OMIT_VACUUM 
215d3 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
215d4 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63  ***** End of bac
215d5 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  kup.c **********
215d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215d8 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
215d9 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
215da 65 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a  e vdbemem.c ****
215db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215dd 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
215de 30 30 34 20 4d 61 79 20 32 36 0d 0a 2a 2a 0d 0a  004 May 26..**..
215df 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
215e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
215e1 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
215e2 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
215e3 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
215e4 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
215e5 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
215e6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
215e7 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
215e8 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
215e9 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
215ea 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
215eb 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
215ec 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
215ed 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
215ee 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
215ef 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
215f0 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
215f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
215f5 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  **..**..** This 
215f6 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
215f7 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75  de use to manipu
215f8 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63  late "Mem" struc
215f9 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 0d 0a  ture.  A "Mem"..
215fa 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e 67  ** stores a sing
215fb 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  le value in the 
215fc 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e  VDBE.  Mem is an
215fd 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
215fe 65 20 76 69 73 69 62 6c 65 0d 0a 2a 2a 20 6f 6e  e visible..** on
215ff 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44  ly within the VD
21600 42 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72  BE.  Interface r
21601 6f 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f  outines refer to
21602 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65   a Mem using the
21603 0d 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65  ..** name sqlite
21604 5f 76 61 6c 75 65 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a  _value..*/..../*
21605 0d 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  ..** If pMem is 
21606 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61  an object with a
21607 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65   valid string re
21608 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68  presentation, th
21609 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 65  is routine..** e
2160a 6e 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72  nsures the inter
2160b 6e 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  nal encoding for
2160c 20 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72   the string repr
2160d 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0d 0a 2a  esentation is..*
2160e 2a 20 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20  * 'desiredEnc', 
2160f 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54  one of SQLITE_UT
21610 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  F8, SQLITE_UTF16
21611 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46  LE or SQLITE_UTF
21612 31 36 42 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  16BE...**..** If
21613 20 70 4d 65 6d 20 69 73 20 6e 6f 74 20 61 20 73   pMem is not a s
21614 74 72 69 6e 67 20 6f 62 6a 65 63 74 2c 20 6f 72  tring object, or
21615 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66   the encoding of
21616 20 74 68 65 20 73 74 72 69 6e 67 0d 0a 2a 2a 20   the string..** 
21617 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
21618 73 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64  s already stored
21619 20 75 73 69 6e 67 20 74 68 65 20 72 65 71 75 65   using the reque
2161a 73 74 65 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74  sted encoding, t
2161b 68 65 6e 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75  hen this..** rou
2161c 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
2161d 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f  ..**..** SQLITE_
2161e 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
2161f 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
21620 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28   is successful (
21621 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29  or not required)
21622 2e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  ...** SQLITE_NOM
21623 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e  EM may be return
21624 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ed if a malloc()
21625 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f   fails during co
21626 6e 76 65 72 73 69 6f 6e 0d 0a 2a 2a 20 62 65 74  nversion..** bet
21627 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0d 0a 2a  ween formats...*
21628 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
21629 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
2162a 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
2162b 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64  Mem *pMem, int d
2162c 65 73 69 72 65 64 45 6e 63 29 7b 0d 0a 20 20 69  esiredEnc){..  i
2162d 6e 74 20 72 63 3b 0d 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
2162e 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
2162f 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
21630 0d 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69  ..  assert( desi
21631 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
21632 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e  TF8 || desiredEn
21633 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
21634 45 0d 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  E..           ||
21635 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
21636 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0d 0a  ITE_UTF16BE );..
21637 20 20 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c    if( !(pMem->fl
21638 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20  ags&MEM_Str) || 
21639 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72  pMem->enc==desir
2163a 65 64 45 6e 63 20 29 7b 0d 0a 20 20 20 20 72 65  edEnc ){..    re
2163b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
2163c 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2163d 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
2163e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
2163f 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
21640 78 29 20 29 3b 0d 0a 23 69 66 64 65 66 20 53 51  x) );..#ifdef SQ
21641 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d  LITE_OMIT_UTF16.
21642 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
21643 5f 45 52 52 4f 52 3b 0d 0a 23 65 6c 73 65 0d 0a  _ERROR;..#else..
21644 0d 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c  ..  /* MemTransl
21645 61 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e  ate() may return
21646 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51   SQLITE_OK or SQ
21647 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e  LITE_NOMEM. If N
21648 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
21649 2c 0d 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  ,..  ** then the
2164a 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
2164b 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68   value may not h
2164c 61 76 65 20 63 68 61 6e 67 65 64 2e 0d 0a 20 20  ave changed...  
2164d 2a 2f 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  */..  rc = sqlit
2164e 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
2164f 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73  te(pMem, (u8)des
21650 69 72 65 64 45 6e 63 29 3b 0d 0a 20 20 61 73 73  iredEnc);..  ass
21651 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ert(rc==SQLITE_O
21652 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  K    || rc==SQLI
21653 54 45 5f 4e 4f 4d 45 4d 29 3b 0d 0a 20 20 61 73  TE_NOMEM);..  as
21654 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f  sert(rc==SQLITE_
21655 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65  OK    || pMem->e
21656 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b  nc!=desiredEnc);
21657 0d 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53  ..  assert(rc==S
21658 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70  QLITE_NOMEM || p
21659 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65  Mem->enc==desire
2165a 64 45 6e 63 29 3b 0d 0a 20 20 72 65 74 75 72 6e  dEnc);..  return
2165b 20 72 63 3b 0d 0a 23 65 6e 64 69 66 0d 0a 7d 0d   rc;..#endif..}.
2165c 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65 20 73  .../*..** Make s
2165d 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 6e  ure pMem->z poin
2165e 74 73 20 74 6f 20 61 20 77 72 69 74 61 62 6c 65  ts to a writable
2165f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61   allocation of a
21660 74 20 6c 65 61 73 74 20 0d 0a 2a 2a 20 6e 20 62  t least ..** n b
21661 79 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  ytes...**..** If
21662 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
21663 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
21664 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  ins string or bl
21665 6f 62 20 64 61 74 61 0d 0a 2a 2a 20 61 6e 64 20  ob data..** and 
21666 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
21667 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  nt passed to thi
21668 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72  s function is tr
21669 75 65 2c 20 74 68 65 20 0d 0a 2a 2a 20 63 75 72  ue, the ..** cur
2166a 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20  rent content of 
2166b 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73  the cell is pres
2166c 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65  erved. Otherwise
2166d 2c 20 69 74 20 6d 61 79 0d 0a 2a 2a 20 62 65 20  , it may..** be 
2166e 64 69 73 63 61 72 64 65 64 2e 20 20 0d 0a 2a 2a  discarded.  ..**
2166f 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
21670 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f  on sets the MEM_
21671 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65  Dyn flag and cle
21672 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c  ars any xDel cal
21673 6c 62 61 63 6b 2e 0d 0a 2a 2a 20 49 74 20 61 6c  lback...** It al
21674 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70  so clears MEM_Ep
21675 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74  hem and MEM_Stat
21676 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 65  ic. If the prese
21677 72 76 65 20 66 6c 61 67 20 69 73 20 0d 0a 2a 2a  rve flag is ..**
21678 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20   not set, Mem.n 
21679 69 73 20 7a 65 72 6f 65 64 2e 0d 0a 2a 2f 0d 0a  is zeroed...*/..
2167a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2167b 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
2167c 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c  mGrow(Mem *pMem,
2167d 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73   int n, int pres
2167e 65 72 76 65 29 7b 0d 0a 20 20 61 73 73 65 72 74  erve){..  assert
2167f 28 20 31 20 3e 3d 0d 0a 20 20 20 20 28 28 70 4d  ( 1 >=..    ((pM
21680 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70  em->zMalloc && p
21681 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d  Mem->zMalloc==pM
21682 65 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20  em->z) ? 1 : 0) 
21683 2b 0d 0a 20 20 20 20 28 28 28 70 4d 65 6d 2d 3e  +..    (((pMem->
21684 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 26 26  flags&MEM_Dyn)&&
21685 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 31 20  pMem->xDel) ? 1 
21686 3a 20 30 29 20 2b 20 0d 0a 20 20 20 20 28 28 70  : 0) + ..    ((p
21687 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45  Mem->flags&MEM_E
21688 70 68 65 6d 29 20 3f 20 31 20 3a 20 30 29 20 2b  phem) ? 1 : 0) +
21689 20 0d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66   ..    ((pMem->f
2168a 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29  lags&MEM_Static)
2168b 20 3f 20 31 20 3a 20 30 29 0d 0a 20 20 29 3b 0d   ? 1 : 0)..  );.
2168c 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
2168d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
2168e 65 74 29 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 69  et)==0 );....  i
2168f 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32  f( n<32 ) n = 32
21690 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
21691 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65  DbMallocSize(pMe
21692 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61  m->db, pMem->zMa
21693 6c 6c 6f 63 29 3c 6e 20 29 7b 0d 0a 20 20 20 20  lloc)<n ){..    
21694 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20  if( preserve && 
21695 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a  pMem->z==pMem->z
21696 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 20  Malloc ){..     
21697 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
21698 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
21699 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
2169a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
2169b 2d 3e 7a 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20  ->z, n);..      
2169c 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0d 0a 20  preserve = 0;.. 
2169d 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2169e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2169f 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
216a0 4d 61 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20 20 20  Malloc);..      
216a1 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pMem->zMalloc = 
216a2 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
216a3 61 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b  aw(pMem->db, n);
216a4 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
216a5 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26    if( pMem->z &&
216a6 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65   preserve && pMe
216a7 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d  m->zMalloc && pM
216a8 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61  em->z!=pMem->zMa
216a9 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 6d 65 6d  lloc ){..    mem
216aa 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  cpy(pMem->zMallo
216ab 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 6d  c, pMem->z, pMem
216ac 2d 3e 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  ->n);..  }..  if
216ad 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
216ae 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78  M_Dyn && pMem->x
216af 44 65 6c 20 29 7b 0d 0a 20 20 20 20 70 4d 65 6d  Del ){..    pMem
216b0 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28  ->xDel((void *)(
216b1 70 4d 65 6d 2d 3e 7a 29 29 3b 0d 0a 20 20 7d 0d  pMem->z));..  }.
216b2 0a 0d 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70  ...  pMem->z = p
216b3 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0d 0a 20  Mem->zMalloc;.. 
216b4 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20   if( pMem->z==0 
216b5 29 7b 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ){..    pMem->fl
216b6 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d  ags = MEM_Null;.
216b7 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
216b8 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
216b9 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
216ba 61 74 69 63 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70  atic);..  }..  p
216bb 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0d 0a  Mem->xDel = 0;..
216bc 20 20 72 65 74 75 72 6e 20 28 70 4d 65 6d 2d 3e    return (pMem->
216bd 7a 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  z ? SQLITE_OK : 
216be 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0d 0a  SQLITE_NOMEM);..
216bf 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65  }..../*..** Make
216c0 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f   the given Mem o
216c1 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20  bject MEM_Dyn.  
216c2 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
216c3 6d 61 6b 65 20 69 74 20 73 6f 0d 0a 2a 2a 20 74  make it so..** t
216c4 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20  hat any TEXT or 
216c5 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20  BLOB content is 
216c6 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
216c7 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0d 0a   obtained from..
216c8 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e  ** malloc().  In
216c9 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e   this way, we kn
216ca 6f 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f  ow that the memo
216cb 72 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65  ry is safe to be
216cc 0d 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e  ..** overwritten
216cd 20 6f 72 20 61 6c 74 65 72 65 64 2e 0d 0a 2a 2a   or altered...**
216ce 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
216cf 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
216d0 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
216d1 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
216d2 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
216d3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
216d4 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
216d5 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29  eable(Mem *pMem)
216d6 7b 0d 0a 20 20 69 6e 74 20 66 3b 0d 0a 20 20 61  {..  int f;..  a
216d7 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
216d8 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
216d9 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
216da 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  b->mutex) );..  
216db 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
216dc 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29  lags&MEM_RowSet)
216dd 3d 3d 30 20 29 3b 0d 0a 20 20 45 78 70 61 6e 64  ==0 );..  Expand
216de 42 6c 6f 62 28 70 4d 65 6d 29 3b 0d 0a 20 20 66  Blob(pMem);..  f
216df 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0d   = pMem->flags;.
216e0 0a 20 20 69 66 28 20 28 66 26 28 4d 45 4d 5f 53  .  if( (f&(MEM_S
216e1 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 26 26  tr|MEM_Blob)) &&
216e2 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
216e3 7a 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20  zMalloc ){..    
216e4 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
216e5 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65  emGrow(pMem, pMe
216e6 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 29 7b 0d  m->n + 2, 1) ){.
216e7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
216e8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
216e9 20 7d 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b   }..    pMem->z[
216ea 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0d 0a 20  pMem->n] = 0;.. 
216eb 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d     pMem->z[pMem-
216ec 3e 6e 2b 31 5d 20 3d 20 30 3b 0d 0a 20 20 20 20  >n+1] = 0;..    
216ed 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
216ee 45 4d 5f 54 65 72 6d 3b 0d 0a 23 69 66 64 65 66  EM_Term;..#ifdef
216ef 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
216f0 20 20 20 70 4d 65 6d 2d 3e 70 53 63 6f 70 79 46     pMem->pScopyF
216f1 72 6f 6d 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66  rom = 0;..#endif
216f2 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
216f3 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
216f4 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  .../*..** If the
216f5 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20   given Mem* has 
216f6 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
216f7 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f  il, turn it into
216f8 20 61 6e 20 6f 72 64 69 6e 61 72 79 0d 0a 2a 2a   an ordinary..**
216f9 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20   blob stored in 
216fa 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
216fb 63 61 74 65 64 20 73 70 61 63 65 2e 0d 0a 2a 2f  cated space...*/
216fc 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
216fd 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0d 0a  _OMIT_INCRBLOB..
216fe 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
216ff 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
21700 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
21701 2a 70 4d 65 6d 29 7b 0d 0a 20 20 69 66 28 20 70  *pMem){..  if( p
21702 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
21703 5f 5a 65 72 6f 20 29 7b 0d 0a 20 20 20 20 69 6e  _Zero ){..    in
21704 74 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 61 73  t nByte;..    as
21705 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
21706 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0d 0a 20  s&MEM_Blob );.. 
21707 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d     assert( (pMem
21708 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
21709 65 74 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 61  et)==0 );..    a
2170a 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
2170b 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
2170c 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
2170d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 0d 0a  b->mutex) );....
2170e 20 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65      /* Set nByte
2170f 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
21710 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
21711 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78   to store the ex
21712 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0d  panded blob. */.
21713 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65  .    nByte = pMe
21714 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 6e  m->n + pMem->u.n
21715 5a 65 72 6f 3b 0d 0a 20 20 20 20 69 66 28 20 6e  Zero;..    if( n
21716 42 79 74 65 3c 3d 30 20 29 7b 0d 0a 20 20 20 20  Byte<=0 ){..    
21717 20 20 6e 42 79 74 65 20 3d 20 31 3b 0d 0a 20 20    nByte = 1;..  
21718 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73 71 6c    }..    if( sql
21719 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
2171a 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20  pMem, nByte, 1) 
2171b 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){..      return
2171c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
2171d 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 6d 65 6d      }....    mem
2171e 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65  set(&pMem->z[pMe
2171f 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e  m->n], 0, pMem->
21720 75 2e 6e 5a 65 72 6f 29 3b 0d 0a 20 20 20 20 70  u.nZero);..    p
21721 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e  Mem->n += pMem->
21722 75 2e 6e 5a 65 72 6f 3b 0d 0a 20 20 20 20 70 4d  u.nZero;..    pM
21723 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  em->flags &= ~(M
21724 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d  EM_Zero|MEM_Term
21725 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  );..  }..  retur
21726 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
21727 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d  .#endif....../*.
21728 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
21729 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 5c  e given Mem is \
2172a 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64  u0000 terminated
2172b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2172c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2172d 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
2172e 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  nate(Mem *pMem){
2172f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ..  assert( pMem
21730 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
21731 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
21732 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
21733 3b 0d 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e  ;..  if( (pMem->
21734 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d  flags & MEM_Term
21735 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66  )!=0 || (pMem->f
21736 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d  lags & MEM_Str)=
21737 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
21738 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f  n SQLITE_OK;   /
21739 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  * Nothing to do 
2173a 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 73  */..  }..  if( s
2173b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
2173c 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b  w(pMem, pMem->n+
2173d 32 2c 20 31 29 20 29 7b 0d 0a 20 20 20 20 72 65  2, 1) ){..    re
2173e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2173f 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4d 65 6d 2d  M;..  }..  pMem-
21740 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b  >z[pMem->n] = 0;
21741 0d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d  ..  pMem->z[pMem
21742 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0d 0a 20 20 70  ->n+1] = 0;..  p
21743 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
21744 4d 5f 54 65 72 6d 3b 0d 0a 20 20 72 65 74 75 72  M_Term;..  retur
21745 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
21746 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 4d 45  .../*..** Add ME
21747 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74  M_Str to the set
21748 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69   of representati
21749 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65  ons for the give
2174a 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0d  n Mem.  Numbers.
2174b 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
2174c 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
2174d 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e  snprintf().  Con
2174e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74  verting a BLOB t
2174f 6f 20 61 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 69  o a string..** i
21750 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a  s a no-op...**..
21751 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72  ** Existing repr
21752 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f  esentations MEM_
21753 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
21754 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c   are *not* inval
21755 69 64 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  idated...**..** 
21756 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65  A MEM_Null value
21757 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70   will never be p
21758 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
21759 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e  nction. This fun
2175a 63 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 75 73 65  ction is..** use
2175b 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67  d for converting
2175c 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20   values to text 
2175d 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f  for returning to
2175e 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20   the user (i.e. 
2175f 76 69 61 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  via..** sqlite3_
21760 76 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f  value_text()), o
21761 72 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  r for ensuring t
21762 68 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65  hat values to be
21763 20 75 73 65 64 20 61 73 20 62 74 72 65 65 0d 0a   used as btree..
21764 2a 2a 20 6b 65 79 73 20 61 72 65 20 73 74 72 69  ** keys are stri
21765 6e 67 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d  ngs. In the form
21766 65 72 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70  er case a NULL p
21767 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
21768 65 64 20 74 68 65 0d 0a 2a 2a 20 75 73 65 72 20  ed the..** user 
21769 61 6e 64 20 74 68 65 20 6c 61 74 65 72 20 69 73  and the later is
2176a 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f   an internal pro
2176b 67 72 61 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0d  gramming error..
2176c 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2176d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2176e 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
2176f 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 65  Mem *pMem, int e
21770 6e 63 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  nc){..  int rc =
21771 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69   SQLITE_OK;..  i
21772 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e 66 6c  nt fg = pMem->fl
21773 61 67 73 3b 0d 0a 20 20 63 6f 6e 73 74 20 69 6e  ags;..  const in
21774 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0d 0a 0d  t nByte = 32;...
21775 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
21776 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
21777 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
21778 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
21779 0d 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67  ..  assert( !(fg
2177a 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0d 0a 20  &MEM_Zero) );.. 
2177b 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 4d   assert( !(fg&(M
2177c 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
2177d 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
2177e 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  fg&(MEM_Int|MEM_
2177f 52 65 61 6c 29 20 29 3b 0d 0a 20 20 61 73 73 65  Real) );..  asse
21780 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
21781 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
21782 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 45 49  );..  assert( EI
21783 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
21784 4e 54 28 70 4d 65 6d 29 20 29 3b 0d 0a 0d 0a 0d  NT(pMem) );.....
21785 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
21786 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
21787 6e 42 79 74 65 2c 20 30 29 20 29 7b 0d 0a 20 20  nByte, 0) ){..  
21788 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
21789 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  NOMEM;..  }.... 
2178a 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20 6f   /* For a Real o
2178b 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20 73  r Integer, use s
2178c 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
2178d 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65 20   to produce the 
2178e 55 54 46 2d 38 0d 0a 20 20 2a 2a 20 73 74 72 69  UTF-8..  ** stri
2178f 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
21790 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20  n of the value. 
21791 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71  Then, if the req
21792 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0d 0a  uired encoding..
21793 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65    ** is UTF-16le
21794 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20   or UTF-16be do 
21795 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0d 0a  a translation...
21796 20 20 2a 2a 20 0d 0a 20 20 2a 2a 20 46 49 58 20    ** ..  ** FIX 
21797 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20  ME: It would be 
21798 62 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65  better if sqlite
21799 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75  3_snprintf() cou
2179a 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e 0d 0a 20  ld do UTF-16... 
2179b 20 2a 2f 0d 0a 20 20 69 66 28 20 66 67 20 26 20   */..  if( fg & 
2179c 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20 20 20  MEM_Int ){..    
2179d 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
2179e 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
2179f 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
217a0 2e 69 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  .i);..  }else{..
217a1 20 20 20 20 61 73 73 65 72 74 28 20 66 67 20 26      assert( fg &
217a2 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0d 0a 20 20   MEM_Real );..  
217a3 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
217a4 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e  tf(nByte, pMem->
217a5 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65  z, "%!.15g", pMe
217a6 6d 2d 3e 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70  m->r);..  }..  p
217a7 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33  Mem->n = sqlite3
217a8 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a  Strlen30(pMem->z
217a9 29 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20  );..  pMem->enc 
217aa 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0d 0a  = SQLITE_UTF8;..
217ab 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
217ac 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72   MEM_Str|MEM_Ter
217ad 6d 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  m;..  sqlite3Vdb
217ae 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
217af 70 4d 65 6d 2c 20 65 6e 63 29 3b 0d 0a 20 20 72  pMem, enc);..  r
217b0 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
217b1 2f 2a 0d 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65  /*..** Memory ce
217b2 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73  ll pMem contains
217b3 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20   the context of 
217b4 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  an aggregate fun
217b5 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 69 73 20  ction...** This 
217b6 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68  routine calls th
217b7 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 6f  e finalize metho
217b8 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e 63 74  d for that funct
217b9 69 6f 6e 2e 20 20 54 68 65 0d 0a 2a 2a 20 72 65  ion.  The..** re
217ba 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 72  sult of the aggr
217bb 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 20  egate is stored 
217bc 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0d  back into pMem..
217bd 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53  .**..** Return S
217be 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74  QLITE_ERROR if t
217bf 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70  he finalizer rep
217c0 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20  orts an error.  
217c1 53 51 4c 49 54 45 5f 4f 4b 0d 0a 2a 2a 20 6f 74  SQLITE_OK..** ot
217c2 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a 53 51  herwise...*/..SQ
217c3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
217c4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
217c5 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65  inalize(Mem *pMe
217c6 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  m, FuncDef *pFun
217c7 63 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  c){..  int rc = 
217c8 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66  SQLITE_OK;..  if
217c9 28 20 41 4c 57 41 59 53 28 70 46 75 6e 63 20 26  ( ALWAYS(pFunc &
217ca 26 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69  & pFunc->xFinali
217cb 7a 65 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ze) ){..    sqli
217cc 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
217cd 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ..    assert( (p
217ce 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
217cf 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 75  _Null)!=0 || pFu
217d0 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 66  nc==pMem->u.pDef
217d1 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
217d2 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
217d3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
217d4 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
217d5 65 78 29 20 29 3b 0d 0a 20 20 20 20 6d 65 6d 73  ex) );..    mems
217d6 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65  et(&ctx, 0, size
217d7 6f 66 28 63 74 78 29 29 3b 0d 0a 20 20 20 20 63  of(ctx));..    c
217d8 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
217d9 5f 4e 75 6c 6c 3b 0d 0a 20 20 20 20 63 74 78 2e  _Null;..    ctx.
217da 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b  s.db = pMem->db;
217db 0d 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d  ..    ctx.pMem =
217dc 20 70 4d 65 6d 3b 0d 0a 20 20 20 20 63 74 78 2e   pMem;..    ctx.
217dd 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0d 0a  pFunc = pFunc;..
217de 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61      pFunc->xFina
217df 6c 69 7a 65 28 26 63 74 78 29 3b 20 2f 2a 20 49  lize(&ctx); /* I
217e0 4d 50 3a 20 52 2d 32 34 35 30 35 2d 32 33 32 33  MP: R-24505-2323
217e1 30 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74  0 */..    assert
217e2 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67  ( 0==(pMem->flag
217e3 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70  s&MEM_Dyn) && !p
217e4 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0d 0a 20 20  Mem->xDel );..  
217e5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
217e6 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e  pMem->db, pMem->
217e7 7a 4d 61 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20 6d  zMalloc);..    m
217e8 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 78  emcpy(pMem, &ctx
217e9 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e 73  .s, sizeof(ctx.s
217ea 29 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 63 74  ));..    rc = ct
217eb 78 2e 69 73 45 72 72 6f 72 3b 0d 0a 20 20 7d 0d  x.isError;..  }.
217ec 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
217ed 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  ..../*..** If th
217ee 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f  e memory cell co
217ef 6e 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20  ntains a string 
217f0 76 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20  value that must 
217f1 62 65 20 66 72 65 65 64 20 62 79 0d 0a 2a 2a 20  be freed by..** 
217f2 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65  invoking an exte
217f3 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66  rnal callback, f
217f4 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c  ree it now. Call
217f5 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
217f6 6e 0d 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66  n..** does not f
217f7 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c  ree any Mem.zMal
217f8 6c 6f 63 20 62 75 66 66 65 72 2e 0d 0a 2a 2f 0d  loc buffer...*/.
217f9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
217fa 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
217fb 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
217fc 61 6c 28 4d 65 6d 20 2a 70 29 7b 0d 0a 20 20 61  al(Mem *p){..  a
217fd 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20  ssert( p->db==0 
217fe 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
217ff 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
21800 65 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d  ex) );..  if( p-
21801 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29  >flags&MEM_Agg )
21802 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
21803 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c  beMemFinalize(p,
21804 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0d 0a 20 20   p->u.pDef);..  
21805 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c    assert( (p->fl
21806 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d  ags & MEM_Agg)==
21807 30 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0 );..    sqlite
21808 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
21809 70 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  p);..  }else if(
2180a 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79   p->flags&MEM_Dy
2180b 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0d  n && p->xDel ){.
2180c 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
2180d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65  >flags&MEM_RowSe
2180e 74 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 70 2d  t)==0 );..    p-
2180f 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 70 2d  >xDel((void *)p-
21810 3e 7a 29 3b 0d 0a 20 20 20 20 70 2d 3e 78 44 65  >z);..    p->xDe
21811 6c 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 20  l = 0;..  }else 
21812 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  if( p->flags&MEM
21813 5f 52 6f 77 53 65 74 20 29 7b 0d 0a 20 20 20 20  _RowSet ){..    
21814 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
21815 61 72 28 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29  ar(p->u.pRowSet)
21816 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  ;..  }else if( p
21817 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d  ->flags&MEM_Fram
21818 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  e ){..    sqlite
21819 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
2181a 70 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  p);..  }..}..../
2181b 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e  *..** Release an
2181c 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79  y memory held by
2181d 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d   the Mem. This m
2181e 61 79 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d  ay leave the Mem
2181f 20 69 6e 20 61 6e 0d 0a 2a 2a 20 69 6e 63 6f 6e   in an..** incon
21820 73 69 73 74 65 6e 74 20 73 74 61 74 65 2c 20 66  sistent state, f
21821 6f 72 20 65 78 61 6d 70 6c 65 20 77 69 74 68 20  or example with 
21822 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0d 0a  (Mem.z==0) and..
21823 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51  ** (Mem.type==SQ
21824 4c 49 54 45 5f 54 45 58 54 29 2e 0d 0a 2a 2f 0d  LITE_TEXT)...*/.
21825 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21826 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
21827 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a  MemRelease(Mem *
21828 70 29 7b 0d 0a 20 20 56 64 62 65 4d 65 6d 52 65  p){..  VdbeMemRe
21829 6c 65 61 73 65 28 70 29 3b 0d 0a 20 20 73 71 6c  lease(p);..  sql
2182a 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
2182b 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0d 0a  , p->zMalloc);..
2182c 20 20 70 2d 3e 7a 20 3d 20 30 3b 0d 0a 20 20 70    p->z = 0;..  p
2182d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a  ->zMalloc = 0;..
2182e 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0d 0a    p->xDel = 0;..
2182f 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76  }..../*..** Conv
21830 65 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45  ert a 64-bit IEE
21831 45 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20  E double into a 
21832 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
21833 74 65 67 65 72 2e 0d 0a 2a 2a 20 49 66 20 74 68  teger...** If th
21834 65 20 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20  e double is too 
21835 6c 61 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78  large, return 0x
21836 38 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30  8000000000000000
21837 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 73 74 20 73  ...**..** Most s
21838 79 73 74 65 6d 73 20 61 70 70 65 61 72 20 74 6f  ystems appear to
21839 20 64 6f 20 74 68 69 73 20 73 69 6d 70 6c 79 20   do this simply 
2183a 62 79 20 61 73 73 69 67 6e 69 6e 67 0d 0a 2a 2a  by assigning..**
2183b 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 77   variables and w
2183c 69 74 68 6f 75 74 20 74 68 65 20 65 78 74 72 61  ithout the extra
2183d 20 72 61 6e 67 65 20 74 65 73 74 73 2e 20 20 42   range tests.  B
2183e 75 74 0d 0a 2a 2a 20 74 68 65 72 65 20 61 72 65  ut..** there are
2183f 20 72 65 70 6f 72 74 73 20 74 68 61 74 20 77 69   reports that wi
21840 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 61 6e 20  ndows throws an 
21841 65 78 70 65 63 74 69 6f 6e 0d 0a 2a 2a 20 69 66  expection..** if
21842 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f   the floating po
21843 69 6e 74 20 76 61 6c 75 65 20 69 73 20 6f 75 74  int value is out
21844 20 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 65 20   of range. (See 
21845 74 69 63 6b 65 74 20 23 32 38 38 30 2e 29 0d 0a  ticket #2880.)..
21846 2a 2a 20 42 65 63 61 75 73 65 20 77 65 20 64 6f  ** Because we do
21847 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20   not completely 
21848 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 70  understand the p
21849 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0d  roblem, we will.
2184a 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 6f 6e  .** take the con
2184b 73 65 72 76 61 74 69 76 65 20 61 70 70 72 6f 61  servative approa
2184c 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20 64 6f  ch and always do
2184d 20 72 61 6e 67 65 20 74 65 73 74 73 0d 0a 2a 2a   range tests..**
2184e 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69   before attempti
2184f 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  ng the conversio
21850 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  n...*/..static i
21851 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34  64 doubleToInt64
21852 28 64 6f 75 62 6c 65 20 72 29 7b 0d 0a 23 69 66  (double r){..#if
21853 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21854 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a  FLOATING_POINT..
21855 20 20 2f 2a 20 57 68 65 6e 20 66 6c 6f 61 74 69    /* When floati
21856 6e 67 2d 70 6f 69 6e 74 20 69 73 20 6f 6d 69 74  ng-point is omit
21857 74 65 64 2c 20 64 6f 75 62 6c 65 20 61 6e 64 20  ted, double and 
21858 69 6e 74 36 34 20 61 72 65 20 74 68 65 20 73 61  int64 are the sa
21859 6d 65 20 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 72  me thing */..  r
2185a 65 74 75 72 6e 20 72 3b 0d 0a 23 65 6c 73 65 0d  eturn r;..#else.
2185b 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 4d 61 6e 79  .  /*..  ** Many
2185c 20 63 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e   compilers we en
2185d 63 6f 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64  counter do not d
2185e 65 66 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20  efine constants 
2185f 66 6f 72 20 74 68 65 0d 0a 20 20 2a 2a 20 6d 69  for the..  ** mi
21860 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75  nimum and maximu
21861 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  m 64-bit integer
21862 73 2c 20 6f 72 20 74 68 65 79 20 64 65 66 69 6e  s, or they defin
21863 65 20 74 68 65 6d 0d 0a 20 20 2a 2a 20 69 6e 63  e them..  ** inc
21864 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 41 6e  onsistently.  An
21865 64 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 75 6e  d many do not un
21866 64 65 72 73 74 61 6e 64 20 74 68 65 20 22 4c 4c  derstand the "LL
21867 22 20 6e 6f 74 61 74 69 6f 6e 2e 0d 0a 20 20 2a  " notation...  *
21868 2a 20 53 6f 20 77 65 20 64 65 66 69 6e 65 20 6f  * So we define o
21869 75 72 20 6f 77 6e 20 73 74 61 74 69 63 20 63 6f  ur own static co
2186a 6e 73 74 61 6e 74 73 20 68 65 72 65 20 75 73 69  nstants here usi
2186b 6e 67 20 6e 6f 74 68 69 6e 67 0d 0a 20 20 2a 2a  ng nothing..  **
2186c 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 33   larger than a 3
2186d 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 63 6f  2-bit integer co
2186e 6e 73 74 61 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20  nstant...  */.. 
2186f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36   static const i6
21870 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 45  4 maxInt = LARGE
21871 53 54 5f 49 4e 54 36 34 3b 0d 0a 20 20 73 74 61  ST_INT64;..  sta
21872 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 69  tic const i64 mi
21873 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f  nInt = SMALLEST_
21874 49 4e 54 36 34 3b 0d 0a 0d 0a 20 20 69 66 28 20  INT64;....  if( 
21875 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74  r<(double)minInt
21876 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
21877 6d 69 6e 49 6e 74 3b 0d 0a 20 20 7d 65 6c 73 65  minInt;..  }else
21878 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d   if( r>(double)m
21879 61 78 49 6e 74 20 29 7b 0d 0a 20 20 20 20 2f 2a  axInt ){..    /*
2187a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 65   minInt is corre
2187b 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61  ct here - not ma
2187c 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 20  xInt.  It turns 
2187d 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e 69  out that assigni
2187e 6e 67 0d 0a 20 20 20 20 2a 2a 20 61 20 76 65 72  ng..    ** a ver
2187f 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65  y large positive
21880 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e   number to an in
21881 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e  teger results in
21882 20 61 20 76 65 72 79 20 6c 61 72 67 65 0d 0a 20   a very large.. 
21883 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69     ** negative i
21884 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61  nteger.  This ma
21885 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75  kes no sense, bu
21886 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36  t it is what x86
21887 20 68 61 72 64 77 61 72 65 0d 0a 20 20 20 20 2a   hardware..    *
21888 2a 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f  * does so for co
21889 6d 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77  mpatibility we w
2188a 69 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20  ill do the same 
2188b 69 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0d  in software. */.
2188c 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49  .    return minI
2188d 6e 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  nt;..  }else{.. 
2188e 20 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 72     return (i64)r
2188f 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
21890 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
21891 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20  rn some kind of 
21892 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68  integer value wh
21893 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20  ich is the best 
21894 77 65 20 63 61 6e 20 64 6f 0d 0a 2a 2a 20 61 74  we can do..** at
21895 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
21896 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d  e value that *pM
21897 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20  em describes as 
21898 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a 2a 2a 20  an integer...** 
21899 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e  If pMem is an in
2189a 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
2189b 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20  value is exact. 
2189c 20 49 66 20 70 4d 65 6d 20 69 73 0d 0a 2a 2a 20   If pMem is..** 
2189d 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  a floating-point
2189e 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
2189f 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20  returned is the 
218a0 69 6e 74 65 67 65 72 20 70 61 72 74 2e 0d 0a 2a  integer part...*
218a1 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73  * If pMem is a s
218a2 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74  tring or blob, t
218a3 68 65 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61  hen we make an a
218a4 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72  ttempt to conver
218a5 74 0d 0a 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20  t..** it into a 
218a6 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75  integer and retu
218a7 72 6e 20 74 68 61 74 2e 20 20 49 66 20 70 4d 65  rn that.  If pMe
218a8 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 0d  m represents an.
218a9 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e 55 4c 4c 20  .** an SQL-NULL 
218aa 76 61 6c 75 65 2c 20 72 65 74 75 72 6e 20 30 2e  value, return 0.
218ab 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 4d 65 6d  ..**..** If pMem
218ac 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 74   represents a st
218ad 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 73 20  ring value, its 
218ae 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62  encoding might b
218af 65 20 63 68 61 6e 67 65 64 2e 0d 0a 2a 2f 0d 0a  e changed...*/..
218b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
218b1 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  64 sqlite3VdbeIn
218b2 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  tValue(Mem *pMem
218b3 29 7b 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  ){..  int flags;
218b4 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ..  assert( pMem
218b5 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
218b6 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
218b7 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
218b8 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  ;..  assert( EIG
218b9 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
218ba 54 28 70 4d 65 6d 29 20 29 3b 0d 0a 20 20 66 6c  T(pMem) );..  fl
218bb 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67  ags = pMem->flag
218bc 73 3b 0d 0a 20 20 69 66 28 20 66 6c 61 67 73 20  s;..  if( flags 
218bd 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20  & MEM_Int ){..  
218be 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75    return pMem->u
218bf 2e 69 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  .i;..  }else if(
218c0 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61   flags & MEM_Rea
218c1 6c 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  l ){..    return
218c2 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
218c3 4d 65 6d 2d 3e 72 29 3b 0d 0a 20 20 7d 65 6c 73  Mem->r);..  }els
218c4 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d  e if( flags & (M
218c5 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
218c6 20 29 7b 0d 0a 20 20 20 20 69 36 34 20 76 61 6c   ){..    i64 val
218c7 75 65 20 3d 20 30 3b 0d 0a 20 20 20 20 61 73 73  ue = 0;..    ass
218c8 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 7c 7c 20  ert( pMem->z || 
218c9 70 4d 65 6d 2d 3e 6e 3d 3d 30 20 29 3b 0d 0a 20  pMem->n==0 );.. 
218ca 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4d 65     testcase( pMe
218cb 6d 2d 3e 7a 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  m->z==0 );..    
218cc 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d  sqlite3Atoi64(pM
218cd 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 2c 20 70  em->z, &value, p
218ce 4d 65 6d 2d 3e 6e 2c 20 70 4d 65 6d 2d 3e 65 6e  Mem->n, pMem->en
218cf 63 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  c);..    return 
218d0 76 61 6c 75 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b  value;..  }else{
218d1 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
218d2 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
218d3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 65 73  * Return the bes
218d4 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  t representation
218d5 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65   of pMem that we
218d6 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0d   can get into a.
218d7 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 20  .** double.  If 
218d8 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 20  pMem is already 
218d9 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69  a double or an i
218da 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 69  nteger, return i
218db 74 73 0d 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49  ts..** value.  I
218dc 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67  f it is a string
218dd 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
218de 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
218df 20 64 6f 75 62 6c 65 2e 0d 0a 2a 2a 20 49 66 20   double...** If 
218e0 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65  it is a NULL, re
218e1 74 75 72 6e 20 30 2e 30 2e 0d 0a 2a 2f 0d 0a 53  turn 0.0...*/..S
218e2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f  QLITE_PRIVATE do
218e3 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65  uble sqlite3Vdbe
218e4 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70  RealValue(Mem *p
218e5 4d 65 6d 29 7b 0d 0a 20 20 61 73 73 65 72 74 28  Mem){..  assert(
218e6 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
218e7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
218e8 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
218e9 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ex) );..  assert
218ea 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
218eb 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0d  GNMENT(pMem) );.
218ec 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
218ed 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
218ee 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65  ..    return pMe
218ef 6d 2d 3e 72 3b 0d 0a 20 20 7d 65 6c 73 65 20 69  m->r;..  }else i
218f0 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
218f1 20 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20 20   MEM_Int ){..   
218f2 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29   return (double)
218f3 70 4d 65 6d 2d 3e 75 2e 69 3b 0d 0a 20 20 7d 65  pMem->u.i;..  }e
218f4 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
218f5 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
218f6 45 4d 5f 42 6c 6f 62 29 20 29 7b 0d 0a 20 20 20  EM_Blob) ){..   
218f7 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
218f8 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
218f9 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
218fa 49 4e 54 2e 2e 2e 20 2a 2f 0d 0a 20 20 20 20 64  INT... */..    d
218fb 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75  ouble val = (dou
218fc 62 6c 65 29 30 3b 0d 0a 20 20 20 20 73 71 6c 69  ble)0;..    sqli
218fd 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c  te3AtoF(pMem->z,
218fe 20 26 76 61 6c 2c 20 70 4d 65 6d 2d 3e 6e 2c 20   &val, pMem->n, 
218ff 70 4d 65 6d 2d 3e 65 6e 63 29 3b 0d 0a 20 20 20  pMem->enc);..   
21900 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 20 20   return val;..  
21901 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 28  }else{..    /* (
21902 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
21903 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
21904 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
21905 2e 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e  . */..    return
21906 20 28 64 6f 75 62 6c 65 29 30 3b 0d 0a 20 20 7d   (double)0;..  }
21907 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
21908 65 20 4d 45 4d 20 73 74 72 75 63 74 75 72 65 20  e MEM structure 
21909 69 73 20 61 6c 72 65 61 64 79 20 61 20 4d 45 4d  is already a MEM
2190a 5f 52 65 61 6c 2e 20 20 54 72 79 20 74 6f 20 61  _Real.  Try to a
2190b 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61 0d 0a 2a  lso make it a..*
2190c 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77 65 20  * MEM_Int if we 
2190d 63 61 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  can...*/..SQLITE
2190e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2190f 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
21910 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d  Affinity(Mem *pM
21911 65 6d 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  em){..  assert( 
21912 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
21913 4d 5f 52 65 61 6c 20 29 3b 0d 0a 20 20 61 73 73  M_Real );..  ass
21914 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
21915 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
21916 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
21917 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
21918 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
21919 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
2191a 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ex) );..  assert
2191b 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
2191c 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0d  GNMENT(pMem) );.
2191d 0a 0d 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  ...  pMem->u.i =
2191e 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
2191f 4d 65 6d 2d 3e 72 29 3b 0d 0a 0d 0a 20 20 2f 2a  Mem->r);....  /*
21920 20 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76   Only mark the v
21921 61 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67  alue as an integ
21922 65 72 20 69 66 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  er if..  **..  *
21923 2a 20 20 20 20 28 31 29 20 74 68 65 20 72 6f 75  *    (1) the rou
21924 6e 64 2d 74 72 69 70 20 63 6f 6e 76 65 72 73 69  nd-trip conversi
21925 6f 6e 20 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65  on real->int->re
21926 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61  al is a no-op, a
21927 6e 64 0d 0a 20 20 2a 2a 20 20 20 20 28 32 29 20  nd..  **    (2) 
21928 54 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 6e  The integer is n
21929 65 69 74 68 65 72 20 74 68 65 20 6c 61 72 67 65  either the large
2192a 73 74 20 6e 6f 72 20 74 68 65 20 73 6d 61 6c 6c  st nor the small
2192b 65 73 74 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20  est..  **       
2192c 20 70 6f 73 73 69 62 6c 65 20 69 6e 74 65 67 65   possible intege
2192d 72 20 28 74 69 63 6b 65 74 20 23 33 39 32 32 29  r (ticket #3922)
2192e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65  ..  **..  ** The
2192f 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72   second and thir
21930 64 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 66  d terms in the f
21931 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74 69  ollowing conditi
21932 6f 6e 61 6c 20 65 6e 66 6f 72 63 65 73 0d 0a 20  onal enforces.. 
21933 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 63   ** the second c
21934 6f 6e 64 69 74 69 6f 6e 20 75 6e 64 65 72 20 74  ondition under t
21935 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 74 68  he assumption th
21936 61 74 20 61 64 64 69 74 69 6f 6e 20 6f 76 65 72  at addition over
21937 66 6c 6f 77 20 63 61 75 73 65 73 0d 0a 20 20 2a  flow causes..  *
21938 2a 20 76 61 6c 75 65 73 20 74 6f 20 77 72 61 70  * values to wrap
21939 20 61 72 6f 75 6e 64 2e 20 20 4f 6e 20 78 38 36   around.  On x86
2193a 20 68 61 72 64 77 61 72 65 2c 20 74 68 65 20 74   hardware, the t
2193b 68 69 72 64 20 74 65 72 6d 20 69 73 20 61 6c 77  hird term is alw
2193c 61 79 73 0d 0a 20 20 2a 2a 20 74 72 75 65 20 61  ays..  ** true a
2193d 6e 64 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69 74  nd could be omit
2193e 74 65 64 2e 20 20 42 75 74 20 77 65 20 6c 65 61  ted.  But we lea
2193f 76 65 20 69 74 20 69 6e 20 62 65 63 61 75 73 65  ve it in because
21940 20 6f 74 68 65 72 0d 0a 20 20 2a 2a 20 61 72 63   other..  ** arc
21941 68 69 74 65 63 74 75 72 65 73 20 6d 69 67 68 74  hitectures might
21942 20 62 65 68 61 76 65 20 64 69 66 66 65 72 65 6e   behave differen
21943 74 6c 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  tly...  */..  if
21944 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 62  ( pMem->r==(doub
21945 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 20  le)pMem->u.i && 
21946 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c 45  pMem->u.i>SMALLE
21947 53 54 5f 49 4e 54 36 34 0d 0a 20 20 20 20 20 20  ST_INT64..      
21948 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e  && ALWAYS(pMem->
21949 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36  u.i<LARGEST_INT6
2194a 34 29 20 29 7b 0d 0a 20 20 20 20 70 4d 65 6d 2d  4) ){..    pMem-
2194b 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
2194c 74 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  t;..  }..}..../*
2194d 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65  ..** Convert pMe
2194e 6d 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67 65  m to type intege
2194f 72 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20 61  r.  Invalidate a
21950 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65  ny prior represe
21951 6e 74 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 53  ntations...*/..S
21952 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
21953 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
21954 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 20 2a  Integerify(Mem *
21955 70 4d 65 6d 29 7b 0d 0a 20 20 61 73 73 65 72 74  pMem){..  assert
21956 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
21957 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
21958 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
21959 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
2195a 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
2195b 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
2195c 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 45   );..  assert( E
2195d 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
2195e 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0d 0a 0d 0a  ENT(pMem) );....
2195f 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71    pMem->u.i = sq
21960 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
21961 65 28 70 4d 65 6d 29 3b 0d 0a 20 20 4d 65 6d 53  e(pMem);..  MemS
21962 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c  etTypeFlag(pMem,
21963 20 4d 45 4d 5f 49 6e 74 29 3b 0d 0a 20 20 72 65   MEM_Int);..  re
21964 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
21965 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e  .}..../*..** Con
21966 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61  vert pMem so tha
21967 74 20 69 74 20 69 73 20 6f 66 20 74 79 70 65 20  t it is of type 
21968 4d 45 4d 5f 52 65 61 6c 2e 0d 0a 2a 2a 20 49 6e  MEM_Real...** In
21969 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
2196a 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
2196b 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ns...*/..SQLITE_
2196c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2196d 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
2196e 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0d 0a 20  y(Mem *pMem){.. 
2196f 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
21970 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
21971 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
21972 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  >db->mutex) );..
21973 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
21974 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
21975 4d 65 6d 29 20 29 3b 0d 0a 0d 0a 20 20 70 4d 65  Mem) );....  pMe
21976 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 33 56 64  m->r = sqlite3Vd
21977 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d  beRealValue(pMem
21978 29 3b 0d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  );..  MemSetType
21979 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52  Flag(pMem, MEM_R
2197a 65 61 6c 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  eal);..  return 
2197b 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
2197c 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ./*..** Convert 
2197d 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20  pMem so that it 
2197e 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65  has types MEM_Re
2197f 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72  al or MEM_Int or
21980 20 62 6f 74 68 2e 0d 0a 2a 2a 20 49 6e 76 61 6c   both...** Inval
21981 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
21982 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
21983 0d 0a 2a 2a 0d 0a 2a 2a 20 45 76 65 72 79 20 65  ..**..** Every e
21984 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f  ffort is made to
21985 20 66 6f 72 63 65 20 74 68 65 20 63 6f 6e 76 65   force the conve
21986 72 73 69 6f 6e 2c 20 65 76 65 6e 20 69 66 20 74  rsion, even if t
21987 68 65 20 69 6e 70 75 74 0d 0a 2a 2a 20 69 73 20  he input..** is 
21988 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 6f  a string that do
21989 65 73 20 6e 6f 74 20 6c 6f 6f 6b 20 63 6f 6d 70  es not look comp
2198a 6c 65 74 65 6c 79 20 6c 69 6b 65 20 61 20 6e 75  letely like a nu
2198b 6d 62 65 72 2e 20 20 43 6f 6e 76 65 72 74 0d 0a  mber.  Convert..
2198c 2a 2a 20 61 73 20 6d 75 63 68 20 6f 66 20 74 68  ** as much of th
2198d 65 20 73 74 72 69 6e 67 20 61 73 20 77 65 20 63  e string as we c
2198e 61 6e 20 61 6e 64 20 69 67 6e 6f 72 65 20 74 68  an and ignore th
2198f 65 20 72 65 73 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  e rest...*/..SQL
21990 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21991 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
21992 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d  merify(Mem *pMem
21993 29 7b 0d 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d  ){..  if( (pMem-
21994 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e  >flags & (MEM_In
21995 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e  t|MEM_Real|MEM_N
21996 75 6c 6c 29 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  ull))==0 ){..   
21997 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
21998 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f  flags & (MEM_Blo
21999 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29  b|MEM_Str))!=0 )
2199a 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
2199b 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
2199c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2199d 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
2199e 29 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 30 3d  ) );..    if( 0=
2199f 3d 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70  =sqlite3Atoi64(p
219a0 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 75  Mem->z, &pMem->u
219a1 2e 69 2c 20 70 4d 65 6d 2d 3e 6e 2c 20 70 4d 65  .i, pMem->n, pMe
219a2 6d 2d 3e 65 6e 63 29 20 29 7b 0d 0a 20 20 20 20  m->enc) ){..    
219a3 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
219a4 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pMem, MEM_Int);
219a5 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
219a6 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71      pMem->r = sq
219a7 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
219a8 75 65 28 70 4d 65 6d 29 3b 0d 0a 20 20 20 20 20  ue(pMem);..     
219a9 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
219aa 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b  pMem, MEM_Real);
219ab 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
219ac 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
219ad 74 79 28 70 4d 65 6d 29 3b 0d 0a 20 20 20 20 7d  ty(pMem);..    }
219ae 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
219af 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
219b0 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
219b1 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 21 3d 30 20  l|MEM_Null))!=0 
219b2 29 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  );..  pMem->flag
219b3 73 20 26 3d 20 7e 28 4d 45 4d 5f 53 74 72 7c 4d  s &= ~(MEM_Str|M
219b4 45 4d 5f 42 6c 6f 62 29 3b 0d 0a 20 20 72 65 74  EM_Blob);..  ret
219b5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
219b6 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65  }..../*..** Dele
219b7 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
219b8 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
219b9 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
219ba 6e 20 2a 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e  n *pMem to NULL.
219bb 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
219bc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
219bd 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
219be 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0d 0a 20 20 69  Mem *pMem){..  i
219bf 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
219c0 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0d 0a 20   MEM_Frame ){.. 
219c1 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46     VdbeFrame *pF
219c2 72 61 6d 65 20 3d 20 70 4d 65 6d 2d 3e 75 2e 70  rame = pMem->u.p
219c3 46 72 61 6d 65 3b 0d 0a 20 20 20 20 70 46 72 61  Frame;..    pFra
219c4 6d 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 46  me->pParent = pF
219c5 72 61 6d 65 2d 3e 76 2d 3e 70 44 65 6c 46 72 61  rame->v->pDelFra
219c6 6d 65 3b 0d 0a 20 20 20 20 70 46 72 61 6d 65 2d  me;..    pFrame-
219c7 3e 76 2d 3e 70 44 65 6c 46 72 61 6d 65 20 3d 20  >v->pDelFrame = 
219c8 70 46 72 61 6d 65 3b 0d 0a 20 20 7d 0d 0a 20 20  pFrame;..  }..  
219c9 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
219ca 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0d  & MEM_RowSet ){.
219cb 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53  .    sqlite3RowS
219cc 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e  etClear(pMem->u.
219cd 70 52 6f 77 53 65 74 29 3b 0d 0a 20 20 7d 0d 0a  pRowSet);..  }..
219ce 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
219cf 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pMem, MEM_Null)
219d0 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  ;..  pMem->type 
219d1 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0d 0a  = SQLITE_NULL;..
219d2 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65  }..../*..** Dele
219d3 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
219d4 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
219d5 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20  e value to be a 
219d6 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0d 0a  BLOB of length..
219d7 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ** n containing 
219d8 61 6c 6c 20 7a 65 72 6f 73 2e 0d 0a 2a 2f 0d 0a  all zeros...*/..
219d9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
219da 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
219db 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65  emSetZeroBlob(Me
219dc 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b  m *pMem, int n){
219dd 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
219de 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
219df 0d 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ..  pMem->flags 
219e0 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a  = MEM_Blob|MEM_Z
219e1 65 72 6f 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 74 79  ero;..  pMem->ty
219e2 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  pe = SQLITE_BLOB
219e3 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 30  ;..  pMem->n = 0
219e4 3b 0d 0a 20 20 69 66 28 20 6e 3c 30 20 29 20 6e  ;..  if( n<0 ) n
219e5 20 3d 20 30 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 75   = 0;..  pMem->u
219e6 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0d 0a 20 20 70  .nZero = n;..  p
219e7 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
219e8 45 5f 55 54 46 38 3b 0d 0a 0d 0a 23 69 66 64 65  E_UTF8;....#ifde
219e9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
219ea 43 52 42 4c 4f 42 0d 0a 20 20 73 71 6c 69 74 65  CRBLOB..  sqlite
219eb 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
219ec 6d 2c 20 6e 2c 20 30 29 3b 0d 0a 20 20 69 66 28  m, n, 0);..  if(
219ed 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0d 0a 20 20 20   pMem->z ){..   
219ee 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0d 0a 20   pMem->n = n;.. 
219ef 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e     memset(pMem->
219f0 7a 2c 20 30 2c 20 6e 29 3b 0d 0a 20 20 7d 0d 0a  z, 0, n);..  }..
219f1 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  #endif..}..../*.
219f2 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
219f3 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
219f4 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
219f5 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20  stored in *pMem 
219f6 74 6f 20 76 61 6c 2c 0d 0a 2a 2a 20 6d 61 6e 69  to val,..** mani
219f7 66 65 73 74 20 74 79 70 65 20 49 4e 54 45 47 45  fest type INTEGE
219f8 52 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  R...*/..SQLITE_P
219f9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
219fa 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
219fb 36 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 36  64(Mem *pMem, i6
219fc 34 20 76 61 6c 29 7b 0d 0a 20 20 73 71 6c 69 74  4 val){..  sqlit
219fd 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
219fe 28 70 4d 65 6d 29 3b 0d 0a 20 20 70 4d 65 6d 2d  (pMem);..  pMem-
219ff 3e 75 2e 69 20 3d 20 76 61 6c 3b 0d 0a 20 20 70  >u.i = val;..  p
21a00 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
21a01 5f 49 6e 74 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 74  _Int;..  pMem->t
21a02 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
21a03 45 47 45 52 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e  EGER;..}....#ifn
21a04 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21a05 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a  FLOATING_POINT..
21a06 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  /*..** Delete an
21a07 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
21a08 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
21a09 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ue stored in *pM
21a0a 65 6d 20 74 6f 20 76 61 6c 2c 0d 0a 2a 2a 20 6d  em to val,..** m
21a0b 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41  anifest type REA
21a0c 4c 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  L...*/..SQLITE_P
21a0d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21a0e 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75  te3VdbeMemSetDou
21a0f 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64  ble(Mem *pMem, d
21a10 6f 75 62 6c 65 20 76 61 6c 29 7b 0d 0a 20 20 69  ouble val){..  i
21a11 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28  f( sqlite3IsNaN(
21a12 76 61 6c 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  val) ){..    sql
21a13 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
21a14 6c 6c 28 70 4d 65 6d 29 3b 0d 0a 20 20 7d 65 6c  ll(pMem);..  }el
21a15 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  se{..    sqlite3
21a16 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
21a17 4d 65 6d 29 3b 0d 0a 20 20 20 20 70 4d 65 6d 2d  Mem);..    pMem-
21a18 3e 72 20 3d 20 76 61 6c 3b 0d 0a 20 20 20 20 70  >r = val;..    p
21a19 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
21a1a 5f 52 65 61 6c 3b 0d 0a 20 20 20 20 70 4d 65 6d  _Real;..    pMem
21a1b 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
21a1c 46 4c 4f 41 54 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  FLOAT;..  }..}..
21a1d 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
21a1e 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
21a1f 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
21a20 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
21a21 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0d 0a 2a  pMem to be an..*
21a22 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20  * empty boolean 
21a23 69 6e 64 65 78 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  index...*/..SQLI
21a24 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21a25 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
21a26 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65  tRowSet(Mem *pMe
21a27 6d 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  m){..  sqlite3 *
21a28 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0d 0a  db = pMem->db;..
21a29 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
21a2a 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70  );..  assert( (p
21a2b 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
21a2c 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0d 0a  _RowSet)==0 );..
21a2d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
21a2e 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0d 0a  Release(pMem);..
21a2f 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20    pMem->zMalloc 
21a30 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
21a31 63 52 61 77 28 64 62 2c 20 36 34 29 3b 0d 0a 20  cRaw(db, 64);.. 
21a32 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
21a33 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 70 4d  ailed ){..    pM
21a34 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
21a35 4e 75 6c 6c 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  Null;..  }else{.
21a36 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
21a37 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0d 0a 20  m->zMalloc );.. 
21a38 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53     pMem->u.pRowS
21a39 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53  et = sqlite3RowS
21a3a 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d  etInit(db, pMem-
21a3b 3e 7a 4d 61 6c 6c 6f 63 2c 20 0d 0a 20 20 20 20  >zMalloc, ..    
21a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a3e 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
21a3f 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d  ocSize(db, pMem-
21a40 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0d 0a 20 20 20  >zMalloc));..   
21a41 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75   assert( pMem->u
21a42 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 3b 0d 0a  .pRowSet!=0 );..
21a43 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
21a44 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0d 0a 20  = MEM_RowSet;.. 
21a45 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
21a46 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
21a47 68 65 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f  he Mem object co
21a48 6e 74 61 69 6e 73 20 61 20 54 45 58 54 20 6f 72  ntains a TEXT or
21a49 20 42 4c 4f 42 20 74 68 61 74 20 69 73 0d 0a 2a   BLOB that is..*
21a4a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68  * too large - wh
21a4b 6f 73 65 20 73 69 7a 65 20 65 78 63 65 65 64 73  ose size exceeds
21a4c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
21a4d 54 48 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  TH...*/..SQLITE_
21a4e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21a4f 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
21a50 28 4d 65 6d 20 2a 70 29 7b 0d 0a 20 20 61 73 73  (Mem *p){..  ass
21a51 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b  ert( p->db!=0 );
21a52 0d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ..  if( p->flags
21a53 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
21a54 42 6c 6f 62 29 20 29 7b 0d 0a 20 20 20 20 69 6e  Blob) ){..    in
21a55 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0d 0a 20 20 20  t n = p->n;..   
21a56 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
21a57 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0d 0a 20 20 20  MEM_Zero ){..   
21a58 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65     n += p->u.nZe
21a59 72 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ro;..    }..    
21a5a 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e  return n>p->db->
21a5b 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
21a5c 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0d 0a 20 20  MIT_LENGTH];..  
21a5d 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0d  }..  return 0; .
21a5e 0a 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  .}....#ifdef SQL
21a5f 49 54 45 5f 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a  ITE_DEBUG../*..*
21a60 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70  * This routine p
21a61 72 65 70 61 72 65 73 20 61 20 6d 65 6d 6f 72 79  repares a memory
21a62 20 63 65 6c 6c 20 66 6f 72 20 6d 6f 64 69 63 61   cell for modica
21a63 74 69 6f 6e 20 62 79 20 62 72 65 61 6b 69 6e 67  tion by breaking
21a64 0d 0a 2a 2a 20 69 74 73 20 6c 69 6e 6b 20 74 6f  ..** its link to
21a65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20   a shallow copy 
21a66 61 6e 64 20 62 79 20 6d 61 72 6b 69 6e 67 20 61  and by marking a
21a67 6e 79 20 63 75 72 72 65 6e 74 20 73 68 61 6c 6c  ny current shall
21a68 6f 77 0d 0a 2a 2a 20 63 6f 70 69 65 73 20 6f 66  ow..** copies of
21a69 20 74 68 69 73 20 63 65 6c 6c 20 61 73 20 69 6e   this cell as in
21a6a 76 61 6c 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  valid...**..** T
21a6b 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
21a6c 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
21a6d 67 67 69 6e 67 20 6f 6e 6c 79 20 2d 20 74 6f 20  gging only - to 
21a6e 6d 61 6b 65 20 73 75 72 65 20 73 68 61 6c 6c 6f  make sure shallo
21a6f 77 0d 0a 2a 2a 20 63 6f 70 69 65 73 20 61 72 65  w..** copies are
21a70 20 6e 6f 74 20 6d 69 73 75 73 65 64 2e 0d 0a 2a   not misused...*
21a71 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
21a72 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
21a73 62 65 4d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e  beMemAboutToChan
21a74 67 65 28 56 64 62 65 20 2a 70 56 64 62 65 2c 20  ge(Vdbe *pVdbe, 
21a75 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0d 0a 20 20 69  Mem *pMem){..  i
21a76 6e 74 20 69 3b 0d 0a 20 20 4d 65 6d 20 2a 70 58  nt i;..  Mem *pX
21a77 3b 0d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 58  ;..  for(i=1, pX
21a78 3d 26 70 56 64 62 65 2d 3e 61 4d 65 6d 5b 31 5d  =&pVdbe->aMem[1]
21a79 3b 20 69 3c 3d 70 56 64 62 65 2d 3e 6e 4d 65 6d  ; i<=pVdbe->nMem
21a7a 3b 20 69 2b 2b 2c 20 70 58 2b 2b 29 7b 0d 0a 20  ; i++, pX++){.. 
21a7b 20 20 20 69 66 28 20 70 58 2d 3e 70 53 63 6f 70     if( pX->pScop
21a7c 79 46 72 6f 6d 3d 3d 70 4d 65 6d 20 29 7b 0d 0a  yFrom==pMem ){..
21a7d 20 20 20 20 20 20 70 58 2d 3e 66 6c 61 67 73 20        pX->flags 
21a7e 7c 3d 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 3b 0d  |= MEM_Invalid;.
21a7f 0a 20 20 20 20 20 20 70 58 2d 3e 70 53 63 6f 70  .      pX->pScop
21a80 79 46 72 6f 6d 20 3d 20 30 3b 0d 0a 20 20 20 20  yFrom = 0;..    
21a81 7d 0d 0a 20 20 7d 0d 0a 20 20 70 4d 65 6d 2d 3e  }..  }..  pMem->
21a82 70 53 63 6f 70 79 46 72 6f 6d 20 3d 20 30 3b 0d  pScopyFrom = 0;.
21a83 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
21a84 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0d 0a 0d  LITE_DEBUG */...
21a85 0a 2f 2a 0d 0a 2a 2a 20 53 69 7a 65 20 6f 66 20  ./*..** Size of 
21a86 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69  struct Mem not i
21a87 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 6d  ncluding the Mem
21a88 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e  .zMalloc member.
21a89 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4d 45  ..*/..#define ME
21a8a 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f  MCELLSIZE (size_
21a8b 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d  t)(&(((Mem *)0)-
21a8c 3e 7a 4d 61 6c 6c 6f 63 29 29 0d 0a 0d 0a 2f 2a  >zMalloc))..../*
21a8d 0d 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61  ..** Make an sha
21a8e 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72  llow copy of pFr
21a8f 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72  om into pTo.  Pr
21a90 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0d  ior contents of.
21a91 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65 65  .** pTo are free
21a92 64 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a  d.  The pFrom->z
21a93 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64 75   field is not du
21a94 70 6c 69 63 61 74 65 64 2e 20 20 49 66 0d 0a 2a  plicated.  If..*
21a95 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73  * pFrom->z is us
21a96 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20  ed, then pTo->z 
21a97 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61  points to the sa
21a98 6d 65 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f  me thing as pFro
21a99 6d 2d 3e 7a 0d 0a 2a 2a 20 61 6e 64 20 66 6c 61  m->z..** and fla
21a9a 67 73 20 67 65 74 73 20 73 72 63 54 79 70 65 20  gs gets srcType 
21a9b 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 65  (either MEM_Ephe
21a9c 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29  m or MEM_Static)
21a9d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
21a9e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21a9f 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
21aa0 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63  Copy(Mem *pTo, c
21aa1 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c  onst Mem *pFrom,
21aa2 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0d 0a   int srcType){..
21aa3 20 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d    assert( (pFrom
21aa4 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
21aa5 77 53 65 74 29 3d 3d 30 20 29 3b 0d 0a 20 20 56  wSet)==0 );..  V
21aa6 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54  dbeMemRelease(pT
21aa7 6f 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 70 54  o);..  memcpy(pT
21aa8 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c  o, pFrom, MEMCEL
21aa9 4c 53 49 5a 45 29 3b 0d 0a 20 20 70 54 6f 2d 3e  LSIZE);..  pTo->
21aaa 78 44 65 6c 20 3d 20 30 3b 0d 0a 20 20 69 66 28  xDel = 0;..  if(
21aab 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d   (pFrom->flags&M
21aac 45 4d 5f 53 74 61 74 69 63 29 3d 3d 30 20 29 7b  EM_Static)==0 ){
21aad 0d 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73  ..    pTo->flags
21aae 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d 45   &= ~(MEM_Dyn|ME
21aaf 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68  M_Static|MEM_Eph
21ab0 65 6d 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  em);..    assert
21ab1 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45  ( srcType==MEM_E
21ab2 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d  phem || srcType=
21ab3 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0d 0a  =MEM_Static );..
21ab4 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c      pTo->flags |
21ab5 3d 20 73 72 63 54 79 70 65 3b 0d 0a 20 20 7d 0d  = srcType;..  }.
21ab6 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b  .}..../*..** Mak
21ab7 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66  e a full copy of
21ab8 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e   pFrom into pTo.
21ab9 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73    Prior contents
21aba 20 6f 66 20 70 54 6f 20 61 72 65 0d 0a 2a 2a 20   of pTo are..** 
21abb 66 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 65  freed before the
21abc 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0d 0a   copy is made...
21abd 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
21abe 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
21abf 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70  beMemCopy(Mem *p
21ac0 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70  To, const Mem *p
21ac1 46 72 6f 6d 29 7b 0d 0a 20 20 69 6e 74 20 72 63  From){..  int rc
21ac2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d   = SQLITE_OK;...
21ac3 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 72 6f  .  assert( (pFro
21ac4 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
21ac5 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0d 0a 20 20  owSet)==0 );..  
21ac6 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
21ac7 54 6f 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 70  To);..  memcpy(p
21ac8 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45  To, pFrom, MEMCE
21ac9 4c 4c 53 49 5a 45 29 3b 0d 0a 20 20 70 54 6f 2d  LLSIZE);..  pTo-
21aca 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44  >flags &= ~MEM_D
21acb 79 6e 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 54 6f  yn;....  if( pTo
21acc 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  ->flags&(MEM_Str
21acd 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0d 0a 20  |MEM_Blob) ){.. 
21ace 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d     if( 0==(pFrom
21acf 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74  ->flags&MEM_Stat
21ad0 69 63 29 20 29 7b 0d 0a 20 20 20 20 20 20 70 54  ic) ){..      pT
21ad1 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  o->flags |= MEM_
21ad2 45 70 68 65 6d 3b 0d 0a 20 20 20 20 20 20 72 63  Ephem;..      rc
21ad3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
21ad4 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
21ad5 54 6f 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  To);..    }..  }
21ad6 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ....  return rc;
21ad7 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72  ..}..../*..** Tr
21ad8 61 6e 73 66 65 72 20 74 68 65 20 63 6f 6e 74 65  ansfer the conte
21ad9 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20  nts of pFrom to 
21ada 70 54 6f 2e 20 41 6e 79 20 65 78 69 73 74 69 6e  pTo. Any existin
21adb 67 20 76 61 6c 75 65 20 69 6e 20 70 54 6f 20 69  g value in pTo i
21adc 73 0d 0a 2a 2a 20 66 72 65 65 64 2e 20 49 66 20  s..** freed. If 
21add 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 65  pFrom contains e
21ade 70 68 65 6d 65 72 61 6c 20 64 61 74 61 2c 20 61  phemeral data, a
21adf 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0d 0a   copy is made...
21ae0 2a 2a 0d 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e  **..** pFrom con
21ae1 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c  tains an SQL NUL
21ae2 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  L when this rout
21ae3 69 6e 65 20 72 65 74 75 72 6e 73 2e 0d 0a 2a 2f  ine returns...*/
21ae4 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
21ae5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
21ae6 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54  eMemMove(Mem *pT
21ae7 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0d  o, Mem *pFrom){.
21ae8 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d  .  assert( pFrom
21ae9 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
21aea 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46  e3_mutex_held(pF
21aeb 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  rom->db->mutex) 
21aec 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54  );..  assert( pT
21aed 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  o->db==0 || sqli
21aee 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
21aef 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  To->db->mutex) )
21af0 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 46 72  ;..  assert( pFr
21af1 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f  om->db==0 || pTo
21af2 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d  ->db==0 || pFrom
21af3 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b  ->db==pTo->db );
21af4 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  ....  sqlite3Vdb
21af5 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29  eMemRelease(pTo)
21af6 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c  ;..  memcpy(pTo,
21af7 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d   pFrom, sizeof(M
21af8 65 6d 29 29 3b 0d 0a 20 20 70 46 72 6f 6d 2d 3e  em));..  pFrom->
21af9 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
21afa 3b 0d 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c  ;..  pFrom->xDel
21afb 20 3d 20 30 3b 0d 0a 20 20 70 46 72 6f 6d 2d 3e   = 0;..  pFrom->
21afc 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a 7d 0d  zMalloc = 0;..}.
21afd 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65  .../*..** Change
21afe 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
21aff 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 74 72 69  Mem to be a stri
21b00 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e 0d 0a 2a  ng or a BLOB...*
21b01 2a 0d 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79  *..** The memory
21b02 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61   management stra
21b03 74 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e 20  tegy depends on 
21b04 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
21b05 20 78 44 65 6c 0d 0a 2a 2a 20 70 61 72 61 6d 65   xDel..** parame
21b06 74 65 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75  ter. If the valu
21b07 65 20 70 61 73 73 65 64 20 69 73 20 53 51 4c 49  e passed is SQLI
21b08 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68  TE_TRANSIENT, th
21b09 65 6e 20 74 68 65 20 0d 0a 2a 2a 20 73 74 72 69  en the ..** stri
21b0a 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74  ng is copied int
21b0b 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65 78  o a (possibly ex
21b0c 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 20 6d  isting) buffer m
21b0d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 0d 0a  anaged by the ..
21b0e 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65  ** Mem structure
21b0f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79  . Otherwise, any
21b10 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 72   existing buffer
21b11 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 68   is freed and th
21b12 65 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f  e..** pointer co
21b13 70 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  pied...**..** If
21b14 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 74   the string is t
21b15 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 74 20  oo large (if it 
21b16 65 78 63 65 65 64 73 20 74 68 65 20 53 51 4c 49  exceeds the SQLI
21b17 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0d  TE_LIMIT_LENGTH.
21b18 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 20  .** size limit) 
21b19 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61  then no memory a
21b1a 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
21b1b 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e 67  .  If the string
21b1c 20 63 61 6e 20 62 65 0d 0a 2a 2a 20 73 74 6f 72   can be..** stor
21b1d 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63  ed without alloc
21b1e 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68  ating memory, th
21b1f 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20  en it is.  If a 
21b20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
21b21 6e 0d 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65  n..** is require
21b22 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  d to store the s
21b23 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75  tring, then valu
21b24 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63  e of pMem is unc
21b25 68 61 6e 67 65 64 2e 20 20 49 6e 0d 0a 2a 2a 20  hanged.  In..** 
21b26 65 69 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c  either case, SQL
21b27 49 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65  ITE_TOOBIG is re
21b28 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  turned...*/..SQL
21b29 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21b2a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
21b2b 74 53 74 72 28 0d 0a 20 20 4d 65 6d 20 2a 70 4d  tStr(..  Mem *pM
21b2c 65 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em,          /* 
21b2d 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73  Memory cell to s
21b2e 65 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c  et to string val
21b2f 75 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  ue */..  const c
21b30 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20  har *z,      /* 
21b31 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a  String pointer *
21b32 2f 0d 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20  /..  int n,     
21b33 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
21b34 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20  s in string, or 
21b35 6e 65 67 61 74 69 76 65 20 2a 2f 0d 0a 20 20 75  negative */..  u
21b36 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20  8 enc,          
21b37 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f     /* Encoding o
21b38 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42  f z.  0 for BLOB
21b39 73 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78  s */..  void (*x
21b3a 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f 2a 20 44  Del)(void*) /* D
21b3b 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
21b3c 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  on */..){..  int
21b3d 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 20   nByte = n;     
21b3e 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 6f   /* New value fo
21b3f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0d 0a 20 20  r pMem->n */..  
21b40 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20  int iLimit;     
21b41 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
21b42 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72  llowed string or
21b43 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0d 0a 20   blob size */.. 
21b44 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20   u16 flags = 0; 
21b45 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75       /* New valu
21b46 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67  e for pMem->flag
21b47 73 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  s */....  assert
21b48 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
21b49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
21b4a 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
21b4b 74 65 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  tex) );..  asser
21b4c 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
21b4d 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
21b4e 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 7a   );....  /* If z
21b4f 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
21b50 65 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20  er, set pMem to 
21b51 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e  contain an SQL N
21b52 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 21  ULL. */..  if( !
21b53 7a 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  z ){..    sqlite
21b54 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
21b55 70 4d 65 6d 29 3b 0d 0a 20 20 20 20 72 65 74 75  pMem);..    retu
21b56 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20  rn SQLITE_OK;.. 
21b57 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 4d 65 6d   }....  if( pMem
21b58 2d 3e 64 62 20 29 7b 0d 0a 20 20 20 20 69 4c 69  ->db ){..    iLi
21b59 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 2d 3e  mit = pMem->db->
21b5a 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
21b5b 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0d 0a 20 20  MIT_LENGTH];..  
21b5c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 4c 69 6d  }else{..    iLim
21b5d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  it = SQLITE_MAX_
21b5e 4c 45 4e 47 54 48 3b 0d 0a 20 20 7d 0d 0a 20 20  LENGTH;..  }..  
21b5f 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f  flags = (enc==0?
21b60 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72  MEM_Blob:MEM_Str
21b61 29 3b 0d 0a 20 20 69 66 28 20 6e 42 79 74 65 3c  );..  if( nByte<
21b62 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  0 ){..    assert
21b63 28 20 65 6e 63 21 3d 30 20 29 3b 0d 0a 20 20 20  ( enc!=0 );..   
21b64 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45   if( enc==SQLITE
21b65 5f 55 54 46 38 20 29 7b 0d 0a 20 20 20 20 20 20  _UTF8 ){..      
21b66 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79  for(nByte=0; nBy
21b67 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b  te<=iLimit && z[
21b68 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29  nByte]; nByte++)
21b69 7b 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  {}..    }else{..
21b6a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 74 65 3d        for(nByte=
21b6b 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74  0; nByte<=iLimit
21b6c 20 26 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20   && (z[nByte] | 
21b6d 7a 5b 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79  z[nByte+1]); nBy
21b6e 74 65 2b 3d 32 29 7b 7d 0d 0a 20 20 20 20 7d 0d  te+=2){}..    }.
21b6f 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 4d 45  .    flags |= ME
21b70 4d 5f 54 65 72 6d 3b 0d 0a 20 20 7d 0d 0a 0d 0a  M_Term;..  }....
21b71 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
21b72 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68  ng block sets th
21b73 65 20 6e 65 77 20 76 61 6c 75 65 73 20 6f 66 20  e new values of 
21b74 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d 2e 78 44  Mem.z and Mem.xD
21b75 65 6c 2e 20 49 74 0d 0a 20 20 2a 2a 20 61 6c 73  el. It..  ** als
21b76 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 69 6e  o sets a flag in
21b77 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
21b78 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 69 63  "flags" to indic
21b79 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 0d 0a  ate the memory..
21b7a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 20    ** management 
21b7b 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e 20  (one of MEM_Dyn 
21b7c 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0d  or MEM_Static)..
21b7d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 78 44 65  .  */..  if( xDe
21b7e 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  l==SQLITE_TRANSI
21b7f 45 4e 54 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  ENT ){..    int 
21b80 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0d  nAlloc = nByte;.
21b81 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d  .    if( flags&M
21b82 45 4d 5f 54 65 72 6d 20 29 7b 0d 0a 20 20 20 20  EM_Term ){..    
21b83 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63    nAlloc += (enc
21b84 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
21b85 32 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  2);..    }..    
21b86 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74  if( nByte>iLimit
21b87 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72   ){..      retur
21b88 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b  n SQLITE_TOOBIG;
21b89 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
21b8a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
21b8b 72 6f 77 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63  row(pMem, nAlloc
21b8c 2c 20 30 29 20 29 7b 0d 0a 20 20 20 20 20 20 72  , 0) ){..      r
21b8d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
21b8e 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  EM;..    }..    
21b8f 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 2c 20  memcpy(pMem->z, 
21b90 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0d 0a 20 20 7d  z, nAlloc);..  }
21b91 65 6c 73 65 20 69 66 28 20 78 44 65 6c 3d 3d 53  else if( xDel==S
21b92 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 7b  QLITE_DYNAMIC ){
21b93 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
21b94 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
21b95 29 3b 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d  );..    pMem->zM
21b96 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 20  alloc = pMem->z 
21b97 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0d 0a 20 20  = (char *)z;..  
21b98 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
21b99 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
21b9a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
21b9b 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0d 0a 20  elease(pMem);.. 
21b9c 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68     pMem->z = (ch
21b9d 61 72 20 2a 29 7a 3b 0d 0a 20 20 20 20 70 4d 65  ar *)z;..    pMe
21b9e 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0d  m->xDel = xDel;.
21b9f 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28  .    flags |= ((
21ba0 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41  xDel==SQLITE_STA
21ba1 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a  TIC)?MEM_Static:
21ba2 4d 45 4d 5f 44 79 6e 29 3b 0d 0a 20 20 7d 0d 0a  MEM_Dyn);..  }..
21ba3 0d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42  ..  pMem->n = nB
21ba4 79 74 65 3b 0d 0a 20 20 70 4d 65 6d 2d 3e 66 6c  yte;..  pMem->fl
21ba5 61 67 73 20 3d 20 66 6c 61 67 73 3b 0d 0a 20 20  ags = flags;..  
21ba6 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 28 65 6e 63  pMem->enc = (enc
21ba7 3d 3d 30 20 3f 20 53 51 4c 49 54 45 5f 55 54 46  ==0 ? SQLITE_UTF
21ba8 38 20 3a 20 65 6e 63 29 3b 0d 0a 20 20 70 4d 65  8 : enc);..  pMe
21ba9 6d 2d 3e 74 79 70 65 20 3d 20 28 65 6e 63 3d 3d  m->type = (enc==
21baa 30 20 3f 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20  0 ? SQLITE_BLOB 
21bab 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 29 3b 0d  : SQLITE_TEXT);.
21bac 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
21bad 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d 0a 20 20  E_OMIT_UTF16..  
21bae 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53  if( pMem->enc!=S
21baf 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20 73 71  QLITE_UTF8 && sq
21bb0 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64  lite3VdbeMemHand
21bb1 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 29 7b 0d 0a  leBom(pMem) ){..
21bb2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
21bb3 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 23  E_NOMEM;..  }..#
21bb4 65 6e 64 69 66 0d 0a 0d 0a 20 20 69 66 28 20 6e  endif....  if( n
21bb5 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0d 0a  Byte>iLimit ){..
21bb6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
21bb7 45 5f 54 4f 4f 42 49 47 3b 0d 0a 20 20 7d 0d 0a  E_TOOBIG;..  }..
21bb8 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
21bb9 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
21bba 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76  ** Compare the v
21bbb 61 6c 75 65 73 20 63 6f 6e 74 61 69 6e 65 64 20  alues contained 
21bbc 62 79 20 74 68 65 20 74 77 6f 20 6d 65 6d 6f 72  by the two memor
21bbd 79 20 63 65 6c 6c 73 2c 20 72 65 74 75 72 6e 69  y cells, returni
21bbe 6e 67 0d 0a 2a 2a 20 6e 65 67 61 74 69 76 65 2c  ng..** negative,
21bbf 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76   zero or positiv
21bc0 65 20 69 66 20 70 4d 65 6d 31 20 69 73 20 6c 65  e if pMem1 is le
21bc1 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74  ss than, equal t
21bc2 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 0d 0a 2a  o, or greater..*
21bc3 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 53 6f  * than pMem2. So
21bc4 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 20 4e  rting order is N
21bc5 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 6f 6c  ULL's first, fol
21bc6 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 72 73  lowed by numbers
21bc7 20 28 69 6e 74 65 67 65 72 73 0d 0a 2a 2a 20 61   (integers..** a
21bc8 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 64  nd reals) sorted
21bc9 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 6f   numerically, fo
21bca 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 6f  llowed by text o
21bcb 72 64 65 72 65 64 20 62 79 20 74 68 65 20 63 6f  rdered by the co
21bcc 6c 6c 61 74 69 6e 67 0d 0a 2a 2a 20 73 65 71 75  llating..** sequ
21bcd 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66  ence pColl and f
21bce 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72  inally blob's or
21bcf 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28  dered by memcmp(
21bd0 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 77 6f 20 4e  )...**..** Two N
21bd1 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
21bd2 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20  onsidered equal 
21bd3 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
21bd4 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
21bd5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21bd6 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73  3MemCompare(cons
21bd7 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f  t Mem *pMem1, co
21bd8 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20  nst Mem *pMem2, 
21bd9 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70  const CollSeq *p
21bda 43 6f 6c 6c 29 7b 0d 0a 20 20 69 6e 74 20 72 63  Coll){..  int rc
21bdb 3b 0d 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b  ;..  int f1, f2;
21bdc 0d 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64  ..  int combined
21bdd 5f 66 6c 61 67 73 3b 0d 0a 0d 0a 20 20 66 31 20  _flags;....  f1 
21bde 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b 0d  = pMem1->flags;.
21bdf 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66  .  f2 = pMem2->f
21be0 6c 61 67 73 3b 0d 0a 20 20 63 6f 6d 62 69 6e 65  lags;..  combine
21be1 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b  d_flags = f1|f2;
21be2 0d 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 6d  ..  assert( (com
21be3 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d 45  bined_flags & ME
21be4 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0d  M_RowSet)==0 );.
21be5 0a 20 0d 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20  . ..  /* If one 
21be6 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69  value is NULL, i
21be7 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74  t is less than t
21be8 68 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74  he other. If bot
21be9 68 20 76 61 6c 75 65 73 0d 0a 20 20 2a 2a 20 61  h values..  ** a
21bea 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20  re NULL, return 
21beb 30 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  0...  */..  if( 
21bec 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d  combined_flags&M
21bed 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20  EM_Null ){..    
21bee 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e  return (f2&MEM_N
21bef 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e  ull) - (f1&MEM_N
21bf0 75 6c 6c 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ull);..  }....  
21bf1 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20  /* If one value 
21bf2 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20  is a number and 
21bf3 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74  the other is not
21bf4 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  , the number is 
21bf5 6c 65 73 73 2e 0d 0a 20 20 2a 2a 20 49 66 20 62  less...  ** If b
21bf6 6f 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c  oth are numbers,
21bf7 20 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c   compare as real
21bf8 73 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65  s if one is a re
21bf9 61 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65  al, or as intege
21bfa 72 73 0d 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68  rs..  ** if both
21bfb 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65   values are inte
21bfc 67 65 72 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  gers...  */..  i
21bfd 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  f( combined_flag
21bfe 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  s&(MEM_Int|MEM_R
21bff 65 61 6c 29 20 29 7b 0d 0a 20 20 20 20 69 66 28  eal) ){..    if(
21c00 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 7c 4d   !(f1&(MEM_Int|M
21c01 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0d 0a 20 20  EM_Real)) ){..  
21c02 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20      return 1;.. 
21c03 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 21 28     }..    if( !(
21c04 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  f2&(MEM_Int|MEM_
21c05 52 65 61 6c 29 29 20 29 7b 0d 0a 20 20 20 20 20  Real)) ){..     
21c06 20 72 65 74 75 72 6e 20 2d 31 3b 0d 0a 20 20 20   return -1;..   
21c07 20 7d 0d 0a 20 20 20 20 69 66 28 20 28 66 31 20   }..    if( (f1 
21c08 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d  & f2 & MEM_Int)=
21c09 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 64 6f 75  =0 ){..      dou
21c0a 62 6c 65 20 72 31 2c 20 72 32 3b 0d 0a 20 20 20  ble r1, r2;..   
21c0b 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52     if( (f1&MEM_R
21c0c 65 61 6c 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  eal)==0 ){..    
21c0d 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65      r1 = (double
21c0e 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0d 0a 20 20  )pMem1->u.i;..  
21c0f 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
21c10 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e      r1 = pMem1->
21c11 72 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  r;..      }..   
21c12 20 20 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52     if( (f2&MEM_R
21c13 65 61 6c 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  eal)==0 ){..    
21c14 20 20 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65      r2 = (double
21c15 29 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0d 0a 20 20  )pMem2->u.i;..  
21c16 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
21c17 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d 3e      r2 = pMem2->
21c18 72 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  r;..      }..   
21c19 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72     if( r1<r2 ) r
21c1a 65 74 75 72 6e 20 2d 31 3b 0d 0a 20 20 20 20 20  eturn -1;..     
21c1b 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74   if( r1>r2 ) ret
21c1c 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 72 65  urn 1;..      re
21c1d 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d 65 6c  turn 0;..    }el
21c1e 73 65 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  se{..      asser
21c1f 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b  t( f1&MEM_Int );
21c20 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
21c21 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0d 0a 20  f2&MEM_Int );.. 
21c22 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
21c23 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i < pMem2->u.i
21c24 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0d 0a 20   ) return -1;.. 
21c25 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
21c26 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i > pMem2->u.i
21c27 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20   ) return 1;..  
21c28 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
21c29 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
21c2a 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69  * If one value i
21c2b 73 20 61 20 73 74 72 69 6e 67 20 61 6e 64 20 74  s a string and t
21c2c 68 65 20 6f 74 68 65 72 20 69 73 20 61 20 62 6c  he other is a bl
21c2d 6f 62 2c 20 74 68 65 20 73 74 72 69 6e 67 20 69  ob, the string i
21c2e 73 20 6c 65 73 73 2e 0d 0a 20 20 2a 2a 20 49 66  s less...  ** If
21c2f 20 62 6f 74 68 20 61 72 65 20 73 74 72 69 6e 67   both are string
21c30 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 69 6e 67  s, compare using
21c31 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66   the collating f
21c32 75 6e 63 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2f 0d  unctions...  */.
21c33 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f  .  if( combined_
21c34 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b  flags&MEM_Str ){
21c35 0d 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20  ..    if( (f1 & 
21c36 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0d 0a  MEM_Str)==0 ){..
21c37 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d        return 1;.
21c38 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
21c39 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f2 & MEM_Str)==
21c3a 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  0 ){..      retu
21c3b 72 6e 20 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  rn -1;..    }...
21c3c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
21c3d 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e  m1->enc==pMem2->
21c3e 65 6e 63 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  enc );..    asse
21c3f 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d  rt( pMem1->enc==
21c40 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0d  SQLITE_UTF8 || .
21c41 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65  .            pMe
21c42 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
21c43 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31  UTF16LE || pMem1
21c44 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
21c45 46 31 36 42 45 20 29 3b 0d 0a 0d 0a 20 20 20 20  F16BE );....    
21c46 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  /* The collation
21c47 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62   sequence must b
21c48 65 20 64 65 66 69 6e 65 64 20 61 74 20 74 68 69  e defined at thi
21c49 73 20 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66  s point, even if
21c4a 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 75 73 65  ..    ** the use
21c4b 72 20 64 65 6c 65 74 65 73 20 74 68 65 20 63 6f  r deletes the co
21c4c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
21c4d 20 61 66 74 65 72 20 74 68 65 20 76 64 62 65 20   after the vdbe 
21c4e 70 72 6f 67 72 61 6d 20 69 73 0d 0a 20 20 20 20  program is..    
21c4f 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 74 68 69  ** compiled (thi
21c50 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 61 79 73  s was not always
21c51 20 74 68 65 20 63 61 73 65 29 2e 0d 0a 20 20 20   the case)...   
21c52 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
21c53 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c   !pColl || pColl
21c54 2d 3e 78 43 6d 70 20 29 3b 0d 0a 0d 0a 20 20 20  ->xCmp );....   
21c55 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0d 0a 20   if( pColl ){.. 
21c56 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
21c57 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20  enc==pColl->enc 
21c58 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  ){..        /* T
21c59 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 20 61  he strings are a
21c5a 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f  lready in the co
21c5b 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20  rrect encoding. 
21c5c 20 43 61 6c 6c 20 74 68 65 0d 0a 20 20 20 20 20   Call the..     
21c5d 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
21c5e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 74   function direct
21c5f 6c 79 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72  ly */..        r
21c60 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d  eturn pColl->xCm
21c61 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70  p(pColl->pUser,p
21c62 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a  Mem1->n,pMem1->z
21c63 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d  ,pMem2->n,pMem2-
21c64 3e 7a 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  >z);..      }els
21c65 65 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  e{..        cons
21c66 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b  t void *v1, *v2;
21c67 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31  ..        int n1
21c68 2c 20 6e 32 3b 0d 0a 20 20 20 20 20 20 20 20 4d  , n2;..        M
21c69 65 6d 20 63 31 3b 0d 0a 20 20 20 20 20 20 20 20  em c1;..        
21c6a 4d 65 6d 20 63 32 3b 0d 0a 20 20 20 20 20 20 20  Mem c2;..       
21c6b 20 6d 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20   memset(&c1, 0, 
21c6c 73 69 7a 65 6f 66 28 63 31 29 29 3b 0d 0a 20 20  sizeof(c1));..  
21c6d 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 32        memset(&c2
21c6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 32 29 29  , 0, sizeof(c2))
21c6f 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
21c70 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
21c71 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 6d 31 2c  Copy(&c1, pMem1,
21c72 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0d 0a 20 20   MEM_Ephem);..  
21c73 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21c74 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
21c75 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d 5f  &c2, pMem2, MEM_
21c76 45 70 68 65 6d 29 3b 0d 0a 20 20 20 20 20 20 20  Ephem);..       
21c77 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v1 = sqlite3Val
21c78 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
21c79 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f 6c  value*)&c1, pCol
21c7a 6c 2d 3e 65 6e 63 29 3b 0d 0a 20 20 20 20 20 20  l->enc);..      
21c7b 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30    n1 = v1==0 ? 0
21c7c 20 3a 20 63 31 2e 6e 3b 0d 0a 20 20 20 20 20 20   : c1.n;..      
21c7d 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61    v2 = sqlite3Va
21c7e 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
21c7f 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f  _value*)&c2, pCo
21c80 6c 6c 2d 3e 65 6e 63 29 3b 0d 0a 20 20 20 20 20  ll->enc);..     
21c81 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20     n2 = v2==0 ? 
21c82 30 20 3a 20 63 32 2e 6e 3b 0d 0a 20 20 20 20 20  0 : c2.n;..     
21c83 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78     rc = pColl->x
21c84 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72  Cmp(pColl->pUser
21c85 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32  , n1, v1, n2, v2
21c86 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
21c87 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
21c88 65 28 26 63 31 29 3b 0d 0a 20 20 20 20 20 20 20  e(&c1);..       
21c89 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
21c8a 65 6c 65 61 73 65 28 26 63 32 29 3b 0d 0a 20 20  elease(&c2);..  
21c8b 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
21c8c 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
21c8d 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55  ..    /* If a NU
21c8e 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70  LL pointer was p
21c8f 61 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c  assed as the col
21c90 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66  late function, f
21c91 61 6c 6c 20 74 68 72 6f 75 67 68 0d 0a 20 20 20  all through..   
21c92 20 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20   ** to the blob 
21c93 63 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d  case and use mem
21c94 63 6d 70 28 29 2e 20 20 2a 2f 0d 0a 20 20 7d 0d  cmp().  */..  }.
21c95 0a 20 0d 0a 20 20 2f 2a 20 42 6f 74 68 20 76 61  . ..  /* Both va
21c96 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62 6c 6f  lues must be blo
21c97 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75 73 69  bs.  Compare usi
21c98 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f  ng memcmp().  */
21c99 0d 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28  ..  rc = memcmp(
21c9a 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 2d  pMem1->z, pMem2-
21c9b 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 4d  >z, (pMem1->n>pM
21c9c 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e 6e  em2->n)?pMem2->n
21c9d 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0d 0a 20 20 69  :pMem1->n);..  i
21c9e 66 28 20 72 63 3d 3d 30 20 29 7b 0d 0a 20 20 20  f( rc==0 ){..   
21c9f 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d   rc = pMem1->n -
21ca0 20 70 4d 65 6d 32 2d 3e 6e 3b 0d 0a 20 20 7d 0d   pMem2->n;..  }.
21ca1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
21ca2 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 6f 76 65 20  ..../*..** Move 
21ca3 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74  data out of a bt
21ca4 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20  ree key or data 
21ca5 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61  field and into a
21ca6 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0d   Mem structure..
21ca7 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20  .** The data or 
21ca8 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f  key is taken fro
21ca9 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  m the entry that
21caa 20 70 43 75 72 20 69 73 20 63 75 72 72 65 6e 74   pCur is current
21cab 6c 79 20 70 6f 69 6e 74 69 6e 67 0d 0a 2a 2a 20  ly pointing..** 
21cac 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20  to.  offset and 
21cad 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68  amt determine wh
21cae 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  at portion of th
21caf 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f  e data or key to
21cb0 20 72 65 74 72 69 65 76 65 2e 0d 0a 2a 2a 20 6b   retrieve...** k
21cb1 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65  ey is true to ge
21cb2 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c  t the key or fal
21cb3 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20  se to get data. 
21cb4 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77   The result is w
21cb5 72 69 74 74 65 6e 0d 0a 2a 2a 20 69 6e 74 6f 20  ritten..** into 
21cb6 74 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74  the pMem element
21cb7 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70 4d  ...**..** The pM
21cb8 65 6d 20 73 74 72 75 63 74 75 72 65 20 69 73 20  em structure is 
21cb9 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 75 6e  assumed to be un
21cba 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e  initialized.  An
21cbb 79 20 70 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0d  y prior content.
21cbc 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69 74 74  .** is overwritt
21cbd 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69 6e 67  en without being
21cbe 20 66 72 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   freed...**..** 
21cbf 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
21cc0 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65  fails for any re
21cc1 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74  ason (malloc ret
21cc2 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61  urns NULL or una
21cc3 62 6c 65 0d 0a 2a 2a 20 74 6f 20 72 65 61 64 20  ble..** to read 
21cc4 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74  from the disk) t
21cc5 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20  hen the pMem is 
21cc6 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  left in an incon
21cc7 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0d 0a  sistent state...
21cc8 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
21cc9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
21cca 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0d  beMemFromBtree(.
21ccb 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
21ccc 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70  r,   /* Cursor p
21ccd 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72  ointing at recor
21cce 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a  d to retrieve. *
21ccf 2f 0d 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 2c  /..  int offset,
21cd0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
21cd1 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20   from the start 
21cd2 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 75 72  of data to retur
21cd3 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f  n bytes from. */
21cd4 0d 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20  ..  int amt,    
21cd5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
21cd6 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 75  of bytes to retu
21cd7 72 6e 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6b 65  rn. */..  int ke
21cd8 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  y,          /* I
21cd9 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65  f true, retrieve
21cda 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20   from the btree 
21cdb 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a  key, not data. *
21cdc 2f 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20  /..  Mem *pMem  
21cdd 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
21cde 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68  eturn data in th
21cdf 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65  is Mem structure
21ce0 2e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63 68 61 72  . */..){..  char
21ce1 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20   *zData;        
21ce2 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65  /* Data from the
21ce3 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0d   btree layer */.
21ce4 0a 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65  .  int available
21ce5 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72   = 0;  /* Number
21ce6 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61   of bytes availa
21ce7 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  ble on the local
21ce8 20 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0d 0a   btree page */..
21ce9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
21cea 45 5f 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20  E_OK; /* Return 
21ceb 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  code */....  ass
21cec 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
21ced 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70  eCursorIsValid(p
21cee 43 75 72 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  Cur) );....  /* 
21cef 4e 6f 74 65 3a 20 74 68 65 20 63 61 6c 6c 73 20  Note: the calls 
21cf0 74 6f 20 42 74 72 65 65 4b 65 79 46 65 74 63 68  to BtreeKeyFetch
21cf1 28 29 20 61 6e 64 20 44 61 74 61 46 65 74 63 68  () and DataFetch
21cf2 28 29 20 62 65 6c 6f 77 20 61 73 73 65 72 74 28  () below assert(
21cf3 29 20 0d 0a 20 20 2a 2a 20 74 68 61 74 20 62 6f  ) ..  ** that bo
21cf4 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20  th the BtShared 
21cf5 61 6e 64 20 64 61 74 61 62 61 73 65 20 68 61 6e  and database han
21cf6 64 6c 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  dle mutexes are 
21cf7 68 65 6c 64 2e 20 2a 2f 0d 0a 20 20 61 73 73 65  held. */..  asse
21cf8 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
21cf9 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
21cfa 30 20 29 3b 0d 0a 20 20 69 66 28 20 6b 65 79 20  0 );..  if( key 
21cfb 29 7b 0d 0a 20 20 20 20 7a 44 61 74 61 20 3d 20  ){..    zData = 
21cfc 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42  (char *)sqlite3B
21cfd 74 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75  treeKeyFetch(pCu
21cfe 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0d  r, &available);.
21cff 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a  .  }else{..    z
21d00 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73  Data = (char *)s
21d01 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
21d02 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69  etch(pCur, &avai
21d03 6c 61 62 6c 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20  lable);..  }..  
21d04 61 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30  assert( zData!=0
21d05 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 6f 66 66   );....  if( off
21d06 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62  set+amt<=availab
21d07 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61  le && (pMem->fla
21d08 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29  gs&MEM_Dyn)==0 )
21d09 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
21d0a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
21d0b 6d 29 3b 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  m);..    pMem->z
21d0c 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74   = &zData[offset
21d0d 5d 3b 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ];..    pMem->fl
21d0e 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d  ags = MEM_Blob|M
21d0f 45 4d 5f 45 70 68 65 6d 3b 0d 0a 20 20 7d 65 6c  EM_Ephem;..  }el
21d10 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  se if( SQLITE_OK
21d11 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56  ==(rc = sqlite3V
21d12 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
21d13 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0d 0a   amt+2, 0)) ){..
21d14 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
21d15 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44  = MEM_Blob|MEM_D
21d16 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0d 0a 20 20  yn|MEM_Term;..  
21d17 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b    pMem->enc = 0;
21d18 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  ..    pMem->type
21d19 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0d   = SQLITE_BLOB;.
21d1a 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0d  .    if( key ){.
21d1b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
21d1c 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72  te3BtreeKey(pCur
21d1d 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
21d1e 4d 65 6d 2d 3e 7a 29 3b 0d 0a 20 20 20 20 7d 65  Mem->z);..    }e
21d1f 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  lse{..      rc =
21d20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
21d21 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  a(pCur, offset, 
21d22 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0d 0a  amt, pMem->z);..
21d23 20 20 20 20 7d 0d 0a 20 20 20 20 70 4d 65 6d 2d      }..    pMem-
21d24 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0d 0a 20 20  >z[amt] = 0;..  
21d25 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d    pMem->z[amt+1]
21d26 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 72   = 0;..    if( r
21d27 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
21d28 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21d29 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
21d2a 6d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  m);..    }..  }.
21d2b 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74  .  pMem->n = amt
21d2c 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  ;....  return rc
21d2d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68 69 73 20  ;..}..../* This 
21d2e 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
21d2f 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72   available inter
21d30 6e 61 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74  nally, it is not
21d31 20 70 61 72 74 20 6f 66 20 74 68 65 0d 0a 2a 2a   part of the..**
21d32 20 65 78 74 65 72 6e 61 6c 20 41 50 49 2e 20 49   external API. I
21d33 74 20 77 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d  t works in a sim
21d34 69 6c 61 72 20 77 61 79 20 74 6f 20 73 71 6c 69  ilar way to sqli
21d35 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
21d36 2c 0d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 65  ,..** except the
21d37 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 69   data returned i
21d38 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e  s in the encodin
21d39 67 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  g specified by t
21d3a 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a 20 70 61  he second..** pa
21d3b 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20 6d  rameter, which m
21d3c 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51  ust be one of SQ
21d3d 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51  LITE_UTF16BE, SQ
21d3e 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 0d  LITE_UTF16LE or.
21d3f 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e  .** SQLITE_UTF8.
21d40 0d 0a 2a 2a 0d 0a 2a 2a 20 28 32 30 30 36 2d 30  ..**..** (2006-0
21d41 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20  2-16:)  The enc 
21d42 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d  value can be or-
21d43 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55  ed with SQLITE_U
21d44 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0d 0a 2a  TF16_ALIGNED...*
21d45 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 65  * If that is the
21d46 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20   case, then the 
21d47 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61  result must be a
21d48 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65  ligned on an eve
21d49 6e 20 62 79 74 65 0d 0a 2a 2a 20 62 6f 75 6e 64  n byte..** bound
21d4a 61 72 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ary...*/..SQLITE
21d4b 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
21d4c 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
21d4d 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
21d4e 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e  lue* pVal, u8 en
21d4f 63 29 7b 0d 0a 20 20 69 66 28 20 21 70 56 61 6c  c){..  if( !pVal
21d50 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 0d 0a   ) return 0;....
21d51 20 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d 3e    assert( pVal->
21d52 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
21d53 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 6c  _mutex_held(pVal
21d54 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d  ->db->mutex) );.
21d55 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e 63 26  .  assert( (enc&
21d56 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45  3)==(enc&~SQLITE
21d57 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
21d58 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70  );..  assert( (p
21d59 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Val->flags & MEM
21d5a 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0d 0a  _RowSet)==0 );..
21d5b 0d 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c  ..  if( pVal->fl
21d5c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d  ags&MEM_Null ){.
21d5d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  .    return 0;..
21d5e 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 28    }..  assert( (
21d5f 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20  MEM_Blob>>3) == 
21d60 4d 45 4d 5f 53 74 72 20 29 3b 0d 0a 20 20 70 56  MEM_Str );..  pV
21d61 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56  al->flags |= (pV
21d62 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
21d63 42 6c 6f 62 29 3e 3e 33 3b 0d 0a 20 20 45 78 70  Blob)>>3;..  Exp
21d64 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0d 0a  andBlob(pVal);..
21d65 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67    if( pVal->flag
21d66 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0d 0a 20 20  s&MEM_Str ){..  
21d67 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
21d68 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c  ngeEncoding(pVal
21d69 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  , enc & ~SQLITE_
21d6a 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0d  UTF16_ALIGNED);.
21d6b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20  .    if( (enc & 
21d6c 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
21d6d 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28  GNED)!=0 && 1==(
21d6e 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f  1&SQLITE_PTR_TO_
21d6f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b  INT(pVal->z)) ){
21d70 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
21d71 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28  (pVal->flags & (
21d72 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
21d73 61 74 69 63 29 29 21 3d 30 20 29 3b 0d 0a 20 20  atic))!=0 );..  
21d74 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
21d75 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
21d76 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54  ble(pVal)!=SQLIT
21d77 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
21d78 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20   return 0;..    
21d79 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
21d7a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
21d7b 6c 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29  lTerminate(pVal)
21d7c 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 31 32 37  ; /* IMP: R-3127
21d7d 35 2d 34 34 30 36 30 20 2a 2f 0d 0a 20 20 7d 65  5-44060 */..  }e
21d7e 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  lse{..    assert
21d7f 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  ( (pVal->flags&M
21d80 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0d 0a  EM_Blob)==0 );..
21d81 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
21d82 65 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c  emStringify(pVal
21d83 2c 20 65 6e 63 29 3b 0d 0a 20 20 20 20 61 73 73  , enc);..    ass
21d84 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 54  ert( 0==(1&SQLIT
21d85 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61  E_PTR_TO_INT(pVa
21d86 6c 2d 3e 7a 29 29 20 29 3b 0d 0a 20 20 7d 0d 0a  l->z)) );..  }..
21d87 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65    assert(pVal->e
21d88 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49  nc==(enc & ~SQLI
21d89 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
21d8a 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30  ) || pVal->db==0
21d8b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
21d8c 7c 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c  || pVal->db->mal
21d8d 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20  locFailed );..  
21d8e 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28  if( pVal->enc==(
21d8f 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54  enc & ~SQLITE_UT
21d90 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0d  F16_ALIGNED) ){.
21d91 0a 20 20 20 20 72 65 74 75 72 6e 20 70 56 61 6c  .    return pVal
21d92 2d 3e 7a 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ->z;..  }else{..
21d93 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
21d94 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
21d95 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c  Create a new sql
21d96 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
21d97 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  t...*/..SQLITE_P
21d98 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76  RIVATE sqlite3_v
21d99 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c  alue *sqlite3Val
21d9a 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64  ueNew(sqlite3 *d
21d9b 62 29 7b 0d 0a 20 20 4d 65 6d 20 2a 70 20 3d 20  b){..  Mem *p = 
21d9c 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
21d9d 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
21d9e 70 29 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b  p));..  if( p ){
21d9f 0d 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  ..    p->flags =
21da0 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20 20 20   MEM_Null;..    
21da1 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  p->type = SQLITE
21da2 5f 4e 55 4c 4c 3b 0d 0a 20 20 20 20 70 2d 3e 64  _NULL;..    p->d
21da3 62 20 3d 20 64 62 3b 0d 0a 20 20 7d 0d 0a 20 20  b = db;..  }..  
21da4 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a  return p;..}....
21da5 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  /*..** Create a 
21da6 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  new sqlite3_valu
21da7 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69  e object, contai
21da8 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
21da9 66 20 70 45 78 70 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  f pExpr...**..**
21daa 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73   This only works
21dab 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65   for very simple
21dac 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61   expressions tha
21dad 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65  t consist of one
21dae 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a 2a 20 74 6f   constant..** to
21daf 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22  ken (i.e. "5", "
21db0 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 6e 67  5.1", "'a string
21db1 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70 72  '"). If the expr
21db2 65 73 73 69 6f 6e 20 63 61 6e 0d 0a 2a 2a 20 62  ession can..** b
21db3 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65  e converted dire
21db4 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75  ctly into a valu
21db5 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  e, then the valu
21db6 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  e is allocated a
21db7 6e 64 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  nd..** a pointer
21db8 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56   written to *ppV
21db9 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69  al. The caller i
21dba 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
21dbb 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0d 0a  r deallocating..
21dbc 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 20  ** the value by 
21dbd 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71  passing it to sq
21dbe 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29  lite3ValueFree()
21dbf 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68   later on. If th
21dc0 65 20 65 78 70 72 65 73 73 69 6f 6e 0d 0a 2a 2a  e expression..**
21dc1 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
21dc2 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c  rted to a value,
21dc3 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20   then *ppVal is 
21dc4 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0d 0a 2a 2f  set to NULL...*/
21dc5 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
21dc6 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
21dc7 65 46 72 6f 6d 45 78 70 72 28 0d 0a 20 20 73 71  eFromExpr(..  sq
21dc8 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
21dc9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
21dca 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
21dcb 6f 6e 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  on */..  Expr *p
21dcc 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
21dcd 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
21dce 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65  sion to evaluate
21dcf 20 2a 2f 0d 0a 20 20 75 38 20 65 6e 63 2c 20 20   */..  u8 enc,  
21dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21dd1 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20   /* Encoding to 
21dd2 75 73 65 20 2a 2f 0d 0a 20 20 75 38 20 61 66 66  use */..  u8 aff
21dd3 69 6e 69 74 79 2c 20 20 20 20 20 20 20 20 20 20  inity,          
21dd4 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20      /* Affinity 
21dd5 74 6f 20 75 73 65 20 2a 2f 0d 0a 20 20 73 71 6c  to use */..  sql
21dd6 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56  ite3_value **ppV
21dd7 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  al     /* Write 
21dd8 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 68 65  the new value he
21dd9 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  re */..){..  int
21dda 20 6f 70 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 56   op;..  char *zV
21ddb 61 6c 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74  al = 0;..  sqlit
21ddc 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d  e3_value *pVal =
21ddd 20 30 3b 0d 0a 20 20 69 6e 74 20 6e 65 67 49 6e   0;..  int negIn
21dde 74 20 3d 20 31 3b 0d 0a 20 20 63 6f 6e 73 74 20  t = 1;..  const 
21ddf 63 68 61 72 20 2a 7a 4e 65 67 20 3d 20 22 22 3b  char *zNeg = "";
21de0 0d 0a 0d 0a 20 20 69 66 28 20 21 70 45 78 70 72  ....  if( !pExpr
21de1 20 29 7b 0d 0a 20 20 20 20 2a 70 70 56 61 6c 20   ){..    *ppVal 
21de2 3d 20 30 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  = 0;..    return
21de3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d   SQLITE_OK;..  }
21de4 0d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  ..  op = pExpr->
21de5 6f 70 3b 0d 0a 0d 0a 20 20 2f 2a 20 6f 70 20 63  op;....  /* op c
21de6 61 6e 20 6f 6e 6c 79 20 62 65 20 54 4b 5f 52 45  an only be TK_RE
21de7 47 49 53 54 45 52 20 69 66 20 77 65 20 68 61 76  GISTER if we hav
21de8 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
21de9 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
21dea 41 54 33 2e 0d 0a 20 20 2a 2a 20 54 68 65 20 69  AT3...  ** The i
21deb 66 64 65 66 20 68 65 72 65 20 69 73 20 74 6f 20  fdef here is to 
21dec 65 6e 61 62 6c 65 20 75 73 20 74 6f 20 61 63 68  enable us to ach
21ded 69 65 76 65 20 31 30 30 25 20 62 72 61 6e 63 68  ieve 100% branch
21dee 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 65   test coverage e
21def 76 65 6e 0d 0a 20 20 2a 2a 20 77 68 65 6e 20 53  ven..  ** when S
21df0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
21df1 54 33 20 69 73 20 6f 6d 69 74 74 65 64 2e 0d 0a  T3 is omitted...
21df2 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c    */..#ifdef SQL
21df3 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
21df4 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52  ..  if( op==TK_R
21df5 45 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70  EGISTER ) op = p
21df6 45 78 70 72 2d 3e 6f 70 32 3b 0d 0a 23 65 6c 73  Expr->op2;..#els
21df7 65 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6f  e..  if( NEVER(o
21df8 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20  p==TK_REGISTER) 
21df9 29 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70  ) op = pExpr->op
21dfa 32 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20  2;..#endif....  
21dfb 2f 2a 20 48 61 6e 64 6c 65 20 6e 65 67 61 74 69  /* Handle negati
21dfc 76 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 61  ve integers in a
21dfd 20 73 69 6e 67 6c 65 20 73 74 65 70 2e 20 20 54   single step.  T
21dfe 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 69 6e  his is needed in
21dff 20 74 68 65 0d 0a 20 20 2a 2a 20 63 61 73 65 20   the..  ** case 
21e00 77 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69  when the value i
21e01 73 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 34  s -9223372036854
21e02 37 37 35 38 30 38 2e 0d 0a 20 20 2a 2f 0d 0a 20  775808...  */.. 
21e03 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e   if( op==TK_UMIN
21e04 55 53 0d 0a 20 20 20 26 26 20 28 70 45 78 70 72  US..   && (pExpr
21e05 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ->pLeft->op==TK_
21e06 49 4e 54 45 47 45 52 20 7c 7c 20 70 45 78 70 72  INTEGER || pExpr
21e07 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ->pLeft->op==TK_
21e08 46 4c 4f 41 54 29 20 29 7b 0d 0a 20 20 20 20 70  FLOAT) ){..    p
21e09 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Expr = pExpr->pL
21e0a 65 66 74 3b 0d 0a 20 20 20 20 6f 70 20 3d 20 70  eft;..    op = p
21e0b 45 78 70 72 2d 3e 6f 70 3b 0d 0a 20 20 20 20 6e  Expr->op;..    n
21e0c 65 67 49 6e 74 20 3d 20 2d 31 3b 0d 0a 20 20 20  egInt = -1;..   
21e0d 20 7a 4e 65 67 20 3d 20 22 2d 22 3b 0d 0a 20 20   zNeg = "-";..  
21e0e 7d 0d 0a 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54  }....  if( op==T
21e0f 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d  K_STRING || op==
21e10 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d  TK_FLOAT || op==
21e11 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0d 0a 20  TK_INTEGER ){.. 
21e12 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65     pVal = sqlite
21e13 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0d 0a  3ValueNew(db);..
21e14 20 20 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20      if( pVal==0 
21e15 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a  ) goto no_mem;..
21e16 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
21e17 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21e18 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0d 0a  P_IntValue) ){..
21e19 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21e1a 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 56 61  eMemSetInt64(pVa
21e1b 6c 2c 20 28 69 36 34 29 70 45 78 70 72 2d 3e 75  l, (i64)pExpr->u
21e1c 2e 69 56 61 6c 75 65 2a 6e 65 67 49 6e 74 29 3b  .iValue*negInt);
21e1d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
21e1e 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74      zVal = sqlit
21e1f 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
21e20 73 25 73 22 2c 20 7a 4e 65 67 2c 20 70 45 78 70  s%s", zNeg, pExp
21e21 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a 20  r->u.zToken);.. 
21e22 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d 3d 30       if( zVal==0
21e23 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d   ) goto no_mem;.
21e24 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
21e25 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
21e26 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45  -1, zVal, SQLITE
21e27 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59  _UTF8, SQLITE_DY
21e28 4e 41 4d 49 43 29 3b 0d 0a 20 20 20 20 20 20 69  NAMIC);..      i
21e29 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  f( op==TK_FLOAT 
21e2a 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53  ) pVal->type = S
21e2b 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0d 0a 20 20  QLITE_FLOAT;..  
21e2c 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 28 6f 70    }..    if( (op
21e2d 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20  ==TK_INTEGER || 
21e2e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26  op==TK_FLOAT ) &
21e2f 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  & affinity==SQLI
21e30 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0d 0a  TE_AFF_NONE ){..
21e31 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
21e32 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  ueApplyAffinity(
21e33 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46  pVal, SQLITE_AFF
21e34 5f 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45  _NUMERIC, SQLITE
21e35 5f 55 54 46 38 29 3b 0d 0a 20 20 20 20 7d 65 6c  _UTF8);..    }el
21e36 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
21e37 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69  e3ValueApplyAffi
21e38 6e 69 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e  nity(pVal, affin
21e39 69 74 79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  ity, SQLITE_UTF8
21e3a 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
21e3b 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26  f( pVal->flags &
21e3c 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65   (MEM_Int|MEM_Re
21e3d 61 6c 29 20 29 20 70 56 61 6c 2d 3e 66 6c 61 67  al) ) pVal->flag
21e3e 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0d 0a  s &= ~MEM_Str;..
21e3f 20 20 20 20 69 66 28 20 65 6e 63 21 3d 53 51 4c      if( enc!=SQL
21e40 49 54 45 5f 55 54 46 38 20 29 7b 0d 0a 20 20 20  ITE_UTF8 ){..   
21e41 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
21e42 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61  angeEncoding(pVa
21e43 6c 2c 20 65 6e 63 29 3b 0d 0a 20 20 20 20 7d 0d  l, enc);..    }.
21e44 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
21e45 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0d 0a  =TK_UMINUS ) {..
21e46 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e      /* This bran
21e47 63 68 20 68 61 70 70 65 6e 73 20 66 6f 72 20 6d  ch happens for m
21e48 75 6c 74 69 70 6c 65 20 6e 65 67 61 74 69 76 65  ultiple negative
21e49 20 73 69 67 6e 73 2e 20 20 45 78 3a 20 2d 28 2d   signs.  Ex: -(-
21e4a 35 29 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 53  5) */..    if( S
21e4b 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65  QLITE_OK==sqlite
21e4c 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64  3ValueFromExpr(d
21e4d 62 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65  b,pExpr->pLeft,e
21e4e 6e 63 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61  nc,affinity,&pVa
21e4f 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  l) ){..      sql
21e50 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72  ite3VdbeMemNumer
21e51 69 66 79 28 70 56 61 6c 29 3b 0d 0a 20 20 20 20  ify(pVal);..    
21e52 20 20 69 66 28 20 70 56 61 6c 2d 3e 75 2e 69 3d    if( pVal->u.i=
21e53 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20  =SMALLEST_INT64 
21e54 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 56 61 6c  ){..        pVal
21e55 2d 3e 66 6c 61 67 73 20 26 3d 20 4d 45 4d 5f 49  ->flags &= MEM_I
21e56 6e 74 3b 0d 0a 20 20 20 20 20 20 20 20 70 56 61  nt;..        pVa
21e57 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  l->flags |= MEM_
21e58 52 65 61 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70  Real;..        p
21e59 56 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65  Val->r = (double
21e5a 29 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0d  )LARGEST_INT64;.
21e5b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
21e5c 20 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69         pVal->u.i
21e5d 20 3d 20 2d 70 56 61 6c 2d 3e 75 2e 69 3b 0d 0a   = -pVal->u.i;..
21e5e 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70        }..      p
21e5f 56 61 6c 2d 3e 72 20 3d 20 2d 70 56 61 6c 2d 3e  Val->r = -pVal->
21e60 72 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r;..      sqlite
21e61 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e  3ValueApplyAffin
21e62 69 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69  ity(pVal, affini
21e63 74 79 2c 20 65 6e 63 29 3b 0d 0a 20 20 20 20 7d  ty, enc);..    }
21e64 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70  ..  }else if( op
21e65 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20  ==TK_NULL ){..  
21e66 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    pVal = sqlite3
21e67 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0d 0a 20  ValueNew(db);.. 
21e68 20 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 29     if( pVal==0 )
21e69 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20   goto no_mem;.. 
21e6a 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
21e6b 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
21e6c 45 52 41 4c 0d 0a 20 20 65 6c 73 65 20 69 66 28  ERAL..  else if(
21e6d 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0d   op==TK_BLOB ){.
21e6e 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0d 0a  .    int nVal;..
21e6f 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
21e70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d  r->u.zToken[0]==
21e71 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e  'x' || pExpr->u.
21e72 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29  zToken[0]=='X' )
21e73 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
21e74 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
21e75 5d 3d 3d 27 5c 27 27 20 29 3b 0d 0a 20 20 20 20  ]=='\'' );..    
21e76 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
21e77 6c 75 65 4e 65 77 28 64 62 29 3b 0d 0a 20 20 20  lueNew(db);..   
21e78 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74   if( !pVal ) got
21e79 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20 7a  o no_mem;..    z
21e7a 56 61 6c 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e  Val = &pExpr->u.
21e7b 7a 54 6f 6b 65 6e 5b 32 5d 3b 0d 0a 20 20 20 20  zToken[2];..    
21e7c 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 53 74  nVal = sqlite3St
21e7d 72 6c 65 6e 33 30 28 7a 56 61 6c 29 2d 31 3b 0d  rlen30(zVal)-1;.
21e7e 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 56 61  .    assert( zVa
21e7f 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27 20 29 3b  l[nVal]=='\'' );
21e80 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
21e81 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 6c 2c  eMemSetStr(pVal,
21e82 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f   sqlite3HexToBlo
21e83 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c  b(db, zVal, nVal
21e84 29 2c 20 6e 56 61 6c 2f 32 2c 0d 0a 20 20 20 20  ), nVal/2,..    
21e85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e86 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 5f 44       0, SQLITE_D
21e87 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 7d 0d 0a 23  YNAMIC);..  }..#
21e88 65 6e 64 69 66 0d 0a 0d 0a 20 20 69 66 28 20 70  endif....  if( p
21e89 56 61 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  Val ){..    sqli
21e8a 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54  te3VdbeMemStoreT
21e8b 79 70 65 28 70 56 61 6c 29 3b 0d 0a 20 20 7d 0d  ype(pVal);..  }.
21e8c 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c  .  *ppVal = pVal
21e8d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
21e8e 54 45 5f 4f 4b 3b 0d 0a 0d 0a 6e 6f 5f 6d 65 6d  TE_OK;....no_mem
21e8f 3a 0d 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  :..  db->mallocF
21e90 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 73 71  ailed = 1;..  sq
21e91 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
21e92 7a 56 61 6c 29 3b 0d 0a 20 20 73 71 6c 69 74 65  zVal);..  sqlite
21e93 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
21e94 3b 0d 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b  ;..  *ppVal = 0;
21e95 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
21e96 45 5f 4e 4f 4d 45 4d 3b 0d 0a 7d 0d 0a 0d 0a 2f  E_NOMEM;..}..../
21e97 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  *..** Change the
21e98 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66   string value of
21e99 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
21e9a 65 20 6f 62 6a 65 63 74 0d 0a 2a 2f 0d 0a 53 51  e object..*/..SQ
21e9b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21e9c 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65  d sqlite3ValueSe
21e9d 74 53 74 72 28 0d 0a 20 20 73 71 6c 69 74 65 33  tStr(..  sqlite3
21e9e 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f  _value *v,     /
21e9f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 73 65  * Value to be se
21ea0 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 2c 20 20  t */..  int n,  
21ea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21ea2 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e   Length of strin
21ea3 67 20 7a 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  g z */..  const 
21ea4 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20  void *z,        
21ea5 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e  /* Text of the n
21ea6 65 77 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 20 20  ew string */..  
21ea7 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
21ea8 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e        /* Encodin
21ea9 67 20 74 6f 20 75 73 65 20 2a 2f 0d 0a 20 20 76  g to use */..  v
21eaa 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
21eab 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74  *)   /* Destruct
21eac 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e  or for the strin
21ead 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20  g */..){..  if( 
21eae 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d  v ) sqlite3VdbeM
21eaf 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29  emSetStr((Mem *)
21eb0 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  v, z, n, enc, xD
21eb1 65 6c 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  el);..}..../*..*
21eb2 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 74 65  * Free an sqlite
21eb3 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0d 0a  3_value object..
21eb4 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
21eb5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
21eb6 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33  alueFree(sqlite3
21eb7 5f 76 61 6c 75 65 20 2a 76 29 7b 0d 0a 20 20 69  _value *v){..  i
21eb8 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0d  f( !v ) return;.
21eb9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
21eba 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29  mRelease((Mem *)
21ebb 76 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62  v);..  sqlite3Db
21ebc 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e  Free(((Mem*)v)->
21ebd 64 62 2c 20 76 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  db, v);..}..../*
21ebe 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
21ebf 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
21ec0 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
21ec1 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75  alue object assu
21ec2 6d 69 6e 67 0d 0a 2a 2a 20 74 68 61 74 20 69 74  ming..** that it
21ec3 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69   uses the encodi
21ec4 6e 67 20 22 65 6e 63 22 0d 0a 2a 2f 0d 0a 53 51  ng "enc"..*/..SQ
21ec5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
21ec6 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
21ec7 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  es(sqlite3_value
21ec8 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b   *pVal, u8 enc){
21ec9 0d 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65  ..  Mem *p = (Me
21eca 6d 2a 29 70 56 61 6c 3b 0d 0a 20 20 69 66 28 20  m*)pVal;..  if( 
21ecb 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  (p->flags & MEM_
21ecc 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69  Blob)!=0 || sqli
21ecd 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
21ece 6c 2c 20 65 6e 63 29 20 29 7b 0d 0a 20 20 20 20  l, enc) ){..    
21ecf 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
21ed0 45 4d 5f 5a 65 72 6f 20 29 7b 0d 0a 20 20 20 20  EM_Zero ){..    
21ed1 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20    return p->n + 
21ed2 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0d 0a 20 20 20  p->u.nZero;..   
21ed3 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72   }else{..      r
21ed4 65 74 75 72 6e 20 70 2d 3e 6e 3b 0d 0a 20 20 20  eturn p->n;..   
21ed5 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
21ed6 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a  n 0;..}..../****
21ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
21ed8 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a  f vdbemem.c ****
21ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21edb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
21edc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
21edd 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63  n file vdbeaux.c
21ede 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
21edf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
21ee1 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62  .** 2003 Septemb
21ee2 65 72 20 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  er 6..**..** The
21ee3 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
21ee4 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
21ee5 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
21ee6 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
21ee7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
21ee8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
21ee9 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
21eea 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
21eeb 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
21eec 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
21eed 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
21eee 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
21eef 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
21ef0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
21ef1 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
21ef2 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
21ef3 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
21ef4 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
21ef5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ef6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ef7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ef8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
21ef9 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
21efa 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f  ins code used fo
21efb 72 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74  r creating, dest
21efc 72 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75  roying, and popu
21efd 6c 61 74 69 6e 67 0d 0a 2a 2a 20 61 20 56 44 42  lating..** a VDB
21efe 45 20 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65  E (or an "sqlite
21eff 33 5f 73 74 6d 74 22 20 61 73 20 69 74 20 69 73  3_stmt" as it is
21f00 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75   known to the ou
21f01 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50  tside world.)  P
21f02 72 69 6f 72 0d 0a 2a 2a 20 74 6f 20 76 65 72 73  rior..** to vers
21f03 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74  ion 2.8.7, all t
21f04 68 69 73 20 63 6f 64 65 20 77 61 73 20 63 6f 6d  his code was com
21f05 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 76  bined into the v
21f06 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c  dbe.c source fil
21f07 65 2e 0d 0a 2a 2a 20 42 75 74 20 74 68 61 74 20  e...** But that 
21f08 66 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e 67  file was getting
21f09 20 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 73   too big so this
21f0a 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 72   subroutines wer
21f0b 65 20 73 70 6c 69 74 20 6f 75 74 2e 0d 0a 2a 2f  e split out...*/
21f0c 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57  ......../*..** W
21f0d 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 74 68  hen debugging th
21f0e 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  e code generator
21f0f 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 20 64   in a symbolic d
21f10 65 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e  ebugger, one can
21f11 0d 0a 2a 2a 20 73 65 74 20 74 68 65 20 73 71 6c  ..** set the sql
21f12 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
21f13 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20  ce to 1 and all 
21f14 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20  opcodes will be 
21f15 70 72 69 6e 74 65 64 0d 0a 2a 2a 20 61 73 20 74  printed..** as t
21f16 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74 6f  hey are added to
21f17 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   the instruction
21f18 20 73 74 72 65 61 6d 2e 0d 0a 2a 2f 0d 0a 23 69   stream...*/..#i
21f19 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
21f1a 47 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  G..SQLITE_PRIVAT
21f1b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
21f1c 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b  eAddopTrace = 0;
21f1d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a  ..#endif....../*
21f1e 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  ..** Create a ne
21f1f 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61  w virtual databa
21f20 73 65 20 65 6e 67 69 6e 65 2e 0d 0a 2a 2f 0d 0a  se engine...*/..
21f21 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
21f22 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65  dbe *sqlite3Vdbe
21f23 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a  Create(sqlite3 *
21f24 64 62 29 7b 0d 0a 20 20 56 64 62 65 20 2a 70 3b  db){..  Vdbe *p;
21f25 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44  ..  p = sqlite3D
21f26 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
21f27 73 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b 0d  sizeof(Vdbe) );.
21f28 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
21f29 74 75 72 6e 20 30 3b 0d 0a 20 20 70 2d 3e 64 62  turn 0;..  p->db
21f2a 20 3d 20 64 62 3b 0d 0a 20 20 69 66 28 20 64 62   = db;..  if( db
21f2b 2d 3e 70 56 64 62 65 20 29 7b 0d 0a 20 20 20 20  ->pVdbe ){..    
21f2c 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65 76  db->pVdbe->pPrev
21f2d 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d   = p;..  }..  p-
21f2e 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64  >pNext = db->pVd
21f2f 62 65 3b 0d 0a 20 20 70 2d 3e 70 50 72 65 76 20  be;..  p->pPrev 
21f30 3d 20 30 3b 0d 0a 20 20 64 62 2d 3e 70 56 64 62  = 0;..  db->pVdb
21f31 65 20 3d 20 70 3b 0d 0a 20 20 70 2d 3e 6d 61 67  e = p;..  p->mag
21f32 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f  ic = VDBE_MAGIC_
21f33 49 4e 49 54 3b 0d 0a 20 20 72 65 74 75 72 6e 20  INIT;..  return 
21f34 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
21f35 52 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c  Remember the SQL
21f36 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72   string for a pr
21f37 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
21f38 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
21f39 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21f3a 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62  e3VdbeSetSql(Vdb
21f3b 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *p, const char
21f3c 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20   *z, int n, int 
21f3d 69 73 50 72 65 70 61 72 65 56 32 29 7b 0d 0a 20  isPrepareV2){.. 
21f3e 20 61 73 73 65 72 74 28 20 69 73 50 72 65 70 61   assert( isPrepa
21f3f 72 65 56 32 3d 3d 31 20 7c 7c 20 69 73 50 72 65  reV2==1 || isPre
21f40 70 61 72 65 56 32 3d 3d 30 20 29 3b 0d 0a 20 20  pareV2==0 );..  
21f41 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
21f42 6e 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  n;..#ifdef SQLIT
21f43 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 20 20  E_OMIT_TRACE..  
21f44 69 66 28 20 21 69 73 50 72 65 70 61 72 65 56 32  if( !isPrepareV2
21f45 20 29 20 72 65 74 75 72 6e 3b 0d 0a 23 65 6e 64   ) return;..#end
21f46 69 66 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  if..  assert( p-
21f47 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0d 0a 20 20 70  >zSql==0 );..  p
21f48 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33  ->zSql = sqlite3
21f49 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c  DbStrNDup(p->db,
21f4a 20 7a 2c 20 6e 29 3b 0d 0a 20 20 70 2d 3e 69 73   z, n);..  p->is
21f4b 50 72 65 70 61 72 65 56 32 20 3d 20 28 75 38 29  PrepareV2 = (u8)
21f4c 69 73 50 72 65 70 61 72 65 56 32 3b 0d 0a 7d 0d  isPrepareV2;..}.
21f4d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
21f4e 20 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61   the SQL associa
21f4f 74 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61  ted with a prepa
21f50 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a  red statement..*
21f51 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  /..SQLITE_API co
21f52 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
21f53 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74  3_sql(sqlite3_st
21f54 6d 74 20 2a 70 53 74 6d 74 29 7b 0d 0a 20 20 56  mt *pStmt){..  V
21f55 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
21f56 29 70 53 74 6d 74 3b 0d 0a 20 20 72 65 74 75 72  )pStmt;..  retur
21f57 6e 20 28 70 20 26 26 20 70 2d 3e 69 73 50 72 65  n (p && p->isPre
21f58 70 61 72 65 56 32 29 20 3f 20 70 2d 3e 7a 53 71  pareV2) ? p->zSq
21f59 6c 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  l : 0;..}..../*.
21f5a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e  .** Swap all con
21f5b 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f  tent between two
21f5c 20 56 44 42 45 20 73 74 72 75 63 74 75 72 65 73   VDBE structures
21f5d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
21f5e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21f5f 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20  e3VdbeSwap(Vdbe 
21f60 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0d  *pA, Vdbe *pB){.
21f61 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54  .  Vdbe tmp, *pT
21f62 6d 70 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 54 6d  mp;..  char *zTm
21f63 70 3b 0d 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b  p;..  tmp = *pA;
21f64 0d 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0d 0a  ..  *pA = *pB;..
21f65 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0d 0a 20 20    *pB = tmp;..  
21f66 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74  pTmp = pA->pNext
21f67 3b 0d 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d  ;..  pA->pNext =
21f68 20 70 42 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 70   pB->pNext;..  p
21f69 42 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b  B->pNext = pTmp;
21f6a 0d 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70  ..  pTmp = pA->p
21f6b 50 72 65 76 3b 0d 0a 20 20 70 41 2d 3e 70 50 72  Prev;..  pA->pPr
21f6c 65 76 20 3d 20 70 42 2d 3e 70 50 72 65 76 3b 0d  ev = pB->pPrev;.
21f6d 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 3d 20 70  .  pB->pPrev = p
21f6e 54 6d 70 3b 0d 0a 20 20 7a 54 6d 70 20 3d 20 70  Tmp;..  zTmp = p
21f6f 41 2d 3e 7a 53 71 6c 3b 0d 0a 20 20 70 41 2d 3e  A->zSql;..  pA->
21f70 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b  zSql = pB->zSql;
21f71 0d 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a  ..  pB->zSql = z
21f72 54 6d 70 3b 0d 0a 20 20 70 42 2d 3e 69 73 50 72  Tmp;..  pB->isPr
21f73 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73  epareV2 = pA->is
21f74 50 72 65 70 61 72 65 56 32 3b 0d 0a 7d 0d 0a 0d  PrepareV2;..}...
21f75 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
21f76 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 54 75 72  EBUG../*..** Tur
21f77 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f 72 20  n tracing on or 
21f78 6f 66 66 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  off..*/..SQLITE_
21f79 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21f7a 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64  ite3VdbeTrace(Vd
21f7b 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61  be *p, FILE *tra
21f7c 63 65 29 7b 0d 0a 20 20 70 2d 3e 74 72 61 63 65  ce){..  p->trace
21f7d 20 3d 20 74 72 61 63 65 3b 0d 0a 7d 0d 0a 23 65   = trace;..}..#e
21f7e 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ndif..../*..** R
21f7f 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61  esize the Vdbe.a
21f80 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61 74  Op array so that
21f81 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74 20   it is at least 
21f82 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74 68  one op larger th
21f83 61 6e 20 0d 0a 2a 2a 20 69 74 20 77 61 73 2e 0d  an ..** it was..
21f84 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 6f 75  .**..** If an ou
21f85 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f  t-of-memory erro
21f86 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72  r occurs while r
21f87 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61  esizing the arra
21f88 79 2c 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 53 51  y, return..** SQ
21f89 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74  LITE_NOMEM. In t
21f8a 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f  his case Vdbe.aO
21f8b 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c  p and Vdbe.nOpAl
21f8c 6c 6f 63 20 72 65 6d 61 69 6e 20 0d 0a 2a 2a 20  loc remain ..** 
21f8d 75 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20  unchanged (this 
21f8e 69 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f  is so that any o
21f8f 70 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61  pcodes already a
21f90 6c 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20  llocated can be 
21f91 0d 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64  ..** correctly d
21f92 65 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67  eallocated along
21f93 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f   with the rest o
21f94 66 20 74 68 65 20 56 64 62 65 29 2e 0d 0a 2a 2f  f the Vdbe)...*/
21f95 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 67 72 6f  ..static int gro
21f96 77 4f 70 41 72 72 61 79 28 56 64 62 65 20 2a 70  wOpArray(Vdbe *p
21f97 29 7b 0d 0a 20 20 56 64 62 65 4f 70 20 2a 70 4e  ){..  VdbeOp *pN
21f98 65 77 3b 0d 0a 20 20 69 6e 74 20 6e 4e 65 77 20  ew;..  int nNew 
21f99 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f  = (p->nOpAlloc ?
21f9a 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a   p->nOpAlloc*2 :
21f9b 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65   (int)(1024/size
21f9c 6f 66 28 4f 70 29 29 29 3b 0d 0a 20 20 70 4e 65  of(Op)));..  pNe
21f9d 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
21f9e 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61  lloc(p->db, p->a
21f9f 4f 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28  Op, nNew*sizeof(
21fa0 4f 70 29 29 3b 0d 0a 20 20 69 66 28 20 70 4e 65  Op));..  if( pNe
21fa1 77 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6e 4f 70  w ){..    p->nOp
21fa2 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
21fa3 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64  bMallocSize(p->d
21fa4 62 2c 20 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28  b, pNew)/sizeof(
21fa5 4f 70 29 3b 0d 0a 20 20 20 20 70 2d 3e 61 4f 70  Op);..    p->aOp
21fa6 20 3d 20 70 4e 65 77 3b 0d 0a 20 20 7d 0d 0a 20   = pNew;..  }.. 
21fa7 20 72 65 74 75 72 6e 20 28 70 4e 65 77 20 3f 20   return (pNew ? 
21fa8 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
21fa9 54 45 5f 4e 4f 4d 45 4d 29 3b 0d 0a 7d 0d 0a 0d  TE_NOMEM);..}...
21faa 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ./*..** Add a ne
21fab 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  w instruction to
21fac 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73   the list of ins
21fad 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  tructions curren
21fae 74 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 56 44 42  t in the..** VDB
21faf 45 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61  E.  Return the a
21fb0 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65  ddress of the ne
21fb1 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a  w instruction...
21fb2 2a 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  **..** Parameter
21fb3 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 70 20  s:..**..**    p 
21fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f                Po
21fb5 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 42  inter to the VDB
21fb6 45 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 6f 70 20  E..**..**    op 
21fb7 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
21fb8 20 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73   opcode for this
21fb9 20 69 6e 73 74 72 75 63 74 69 6f 6e 0d 0a 2a 2a   instruction..**
21fba 0d 0a 2a 2a 20 20 20 20 70 31 2c 20 70 32 2c 20  ..**    p1, p2, 
21fbb 70 33 20 20 20 20 20 20 4f 70 65 72 61 6e 64 73  p3      Operands
21fbc 0d 0a 2a 2a 0d 0a 2a 2a 20 55 73 65 20 74 68 65  ..**..** Use the
21fbd 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
21fbe 6c 76 65 4c 61 62 65 6c 28 29 20 66 75 6e 63 74  lveLabel() funct
21fbf 69 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20 61 64  ion to fix an ad
21fc0 64 72 65 73 73 20 61 6e 64 0d 0a 2a 2a 20 74 68  dress and..** th
21fc1 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  e sqlite3VdbeCha
21fc2 6e 67 65 50 34 28 29 20 66 75 6e 63 74 69 6f 6e  ngeP4() function
21fc3 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 76   to change the v
21fc4 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 0d 0a  alue of the P4..
21fc5 2a 2a 20 6f 70 65 72 61 6e 64 2e 0d 0a 2a 2f 0d  ** operand...*/.
21fc6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21fc7 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
21fc8 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69  ddOp3(Vdbe *p, i
21fc9 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
21fca 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0d  nt p2, int p3){.
21fcb 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 56 64 62  .  int i;..  Vdb
21fcc 65 4f 70 20 2a 70 4f 70 3b 0d 0a 0d 0a 20 20 69  eOp *pOp;....  i
21fcd 20 3d 20 70 2d 3e 6e 4f 70 3b 0d 0a 20 20 61 73   = p->nOp;..  as
21fce 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
21fcf 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
21fd0 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6f 70  );..  assert( op
21fd1 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 3b  >0 && op<0xff );
21fd2 0d 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c  ..  if( p->nOpAl
21fd3 6c 6f 63 3c 3d 69 20 29 7b 0d 0a 20 20 20 20 69  loc<=i ){..    i
21fd4 66 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70  f( growOpArray(p
21fd5 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  ) ){..      retu
21fd6 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
21fd7 7d 0d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b 0d 0a  }..  p->nOp++;..
21fd8 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
21fd9 69 5d 3b 0d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  i];..  pOp->opco
21fda 64 65 20 3d 20 28 75 38 29 6f 70 3b 0d 0a 20 20  de = (u8)op;..  
21fdb 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0d 0a 20 20  pOp->p5 = 0;..  
21fdc 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0d 0a 20  pOp->p1 = p1;.. 
21fdd 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0d 0a   pOp->p2 = p2;..
21fde 20 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0d    pOp->p3 = p3;.
21fdf 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30  .  pOp->p4.p = 0
21fe0 3b 0d 0a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  ;..  pOp->p4type
21fe1 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0d 0a   = P4_NOTUSED;..
21fe2 2f 2f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  //#ifdef SQLITE_
21fe3 44 45 42 55 47 0d 0a 20 20 70 4f 70 2d 3e 7a 43  DEBUG..  pOp->zC
21fe4 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0d 0a 20 20 2f  omment = 0;..  /
21fe5 2f 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65  /if( sqlite3Vdbe
21fe6 41 64 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c  AddopTrace ) sql
21fe7 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
21fe8 30 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d  0, i, &p->aOp[i]
21fe9 29 3b 0d 0a 2f 2f 23 65 6e 64 69 66 0d 0a 23 69  );..//#endif..#i
21fea 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
21feb 45 0d 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73  E..  pOp->cycles
21fec 20 3d 20 30 3b 0d 0a 20 20 70 4f 70 2d 3e 63 6e   = 0;..  pOp->cn
21fed 74 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a  t = 0;..#endif..
21fee 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a 7d 0d 0a    return i;..}..
21fef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
21ff0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
21ff1 64 4f 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp0(Vdbe *p, in
21ff2 74 20 6f 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e  t op){..  return
21ff3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21ff4 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20  p3(p, op, 0, 0, 
21ff5 30 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50  0);..}..SQLITE_P
21ff6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21ff7 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 62  e3VdbeAddOp1(Vdb
21ff8 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
21ff9 74 20 70 31 29 7b 0d 0a 20 20 72 65 74 75 72 6e  t p1){..  return
21ffa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21ffb 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c  p3(p, op, p1, 0,
21ffc 20 30 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f   0);..}..SQLITE_
21ffd 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21ffe 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 64  te3VdbeAddOp2(Vd
21fff 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
22000 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0d  nt p1, int p2){.
22001 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
22002 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f  3VdbeAddOp3(p, o
22003 70 2c 20 70 31 2c 20 70 32 2c 20 30 29 3b 0d 0a  p, p1, p2, 0);..
22004 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64  }....../*..** Ad
22005 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74  d an opcode that
22006 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34   includes the p4
22007 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e   value as a poin
22008 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ter...*/..SQLITE
22009 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2200a 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0d  ite3VdbeAddOp4(.
2200b 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
2200c 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68         /* Add th
2200d 65 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73  e opcode to this
2200e 20 56 4d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70   VM */..  int op
2200f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
22010 20 54 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20   The new opcode 
22011 2a 2f 0d 0a 20 20 69 6e 74 20 70 31 2c 20 20 20  */..  int p1,   
22012 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22013 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a   P1 operand */..
22014 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 20 20    int p2,       
22015 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 32 20        /* The P2 
22016 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 69 6e  operand */..  in
22017 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 20  t p3,           
22018 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 72    /* The P3 oper
22019 61 6e 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  and */..  const 
2201a 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a  char *zP4,    /*
2201b 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   The P4 operand 
2201c 2a 2f 0d 0a 20 20 69 6e 74 20 70 34 74 79 70 65  */..  int p4type
2201d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20            /* P4 
2201e 6f 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0d  operand type */.
2201f 0a 29 7b 0d 0a 20 20 69 6e 74 20 61 64 64 72 20  .){..  int addr 
22020 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
22021 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70  Op3(p, op, p1, p
22022 32 2c 20 70 33 29 3b 0d 0a 20 20 73 71 6c 69 74  2, p3);..  sqlit
22023 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 70  e3VdbeChangeP4(p
22024 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 70 34 74  , addr, zP4, p4t
22025 79 70 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ype);..  return 
22026 61 64 64 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  addr;..}..../*..
22027 2a 2a 20 41 64 64 20 61 6e 20 4f 50 5f 50 61 72  ** Add an OP_Par
22028 73 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 2e  seSchema opcode.
22029 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
2202a 73 20 62 72 6f 6b 65 6e 20 6f 75 74 20 66 72 6f  s broken out fro
2202b 6d 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  m..** sqlite3Vdb
2202c 65 41 64 64 4f 70 34 28 29 20 73 69 6e 63 65 20  eAddOp4() since 
2202d 69 74 20 6e 65 65 64 73 20 74 6f 20 61 6c 73 6f  it needs to also
2202e 20 6e 65 65 64 73 20 74 6f 20 6d 61 72 6b 20 61   needs to mark a
2202f 6c 6c 20 62 74 72 65 65 73 0d 0a 2a 2a 20 61 73  ll btrees..** as
22030 20 68 61 76 69 6e 67 20 62 65 65 6e 20 75 73 65   having been use
22031 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 7a  d...**..** The z
22032 57 68 65 72 65 20 73 74 72 69 6e 67 20 6d 75 73  Where string mus
22033 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61  t have been obta
22034 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
22035 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2a 20  3_malloc()...** 
22036 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
22037 6c 20 74 61 6b 65 20 6f 77 6e 65 72 73 68 69 70  l take ownership
22038 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   of the allocate
22039 64 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 53  d memory...*/..S
2203a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2203b 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  id sqlite3VdbeAd
2203c 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 56  dParseSchemaOp(V
2203d 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 44 62 2c  dbe *p, int iDb,
2203e 20 63 68 61 72 20 2a 7a 57 68 65 72 65 29 7b 0d   char *zWhere){.
2203f 0a 20 20 69 6e 74 20 6a 3b 0d 0a 20 20 69 6e 74  .  int j;..  int
22040 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
22041 64 62 65 41 64 64 4f 70 33 28 70 2c 20 4f 50 5f  dbeAddOp3(p, OP_
22042 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
22043 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 73 71 6c 69  , 0, 0);..  sqli
22044 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
22045 70 2c 20 61 64 64 72 2c 20 7a 57 68 65 72 65 2c  p, addr, zWhere,
22046 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a 20   P4_DYNAMIC);.. 
22047 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 64   for(j=0; j<p->d
22048 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 20 73 71 6c  b->nDb; j++) sql
22049 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
2204a 65 28 70 2c 20 6a 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  e(p, j);..}..../
2204b 2a 0d 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  *..** Add an opc
2204c 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
2204d 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
2204e 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0d 0a 2a  s an integer...*
2204f 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
22050 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
22051 65 41 64 64 4f 70 34 49 6e 74 28 0d 0a 20 20 56  eAddOp4Int(..  V
22052 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
22053 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70     /* Add the op
22054 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20  code to this VM 
22055 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20  */..  int op,   
22056 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22057 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0d 0a   new opcode */..
22058 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20    int p1,       
22059 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20        /* The P1 
2205a 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 69 6e  operand */..  in
2205b 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20  t p2,           
2205c 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72    /* The P2 oper
2205d 61 6e 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 33  and */..  int p3
2205e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2205f 20 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20   The P3 operand 
22060 2a 2f 0d 0a 20 20 69 6e 74 20 70 34 20 20 20 20  */..  int p4    
22061 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22062 20 50 34 20 6f 70 65 72 61 6e 64 20 61 73 20 61   P4 operand as a
22063 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0d 0a 29 7b  n integer */..){
22064 0d 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73  ..  int addr = s
22065 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22066 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20  (p, op, p1, p2, 
22067 70 33 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  p3);..  sqlite3V
22068 64 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61  dbeChangeP4(p, a
22069 64 64 72 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ddr, SQLITE_INT_
2206a 54 4f 5f 50 54 52 28 70 34 29 2c 20 50 34 5f 49  TO_PTR(p4), P4_I
2206b 4e 54 33 32 29 3b 0d 0a 20 20 72 65 74 75 72 6e  NT32);..  return
2206c 20 61 64 64 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   addr;..}..../*.
2206d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
2206e 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20   symbolic label 
2206f 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69  for an instructi
22070 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20  on that has yet 
22071 74 6f 20 62 65 0d 0a 2a 2a 20 63 6f 64 65 64 2e  to be..** coded.
22072 20 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c    The symbolic l
22073 61 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a  abel is really j
22074 75 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e  ust a negative n
22075 75 6d 62 65 72 2e 20 20 54 68 65 0d 0a 2a 2a 20  umber.  The..** 
22076 6c 61 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65  label can be use
22077 64 20 61 73 20 74 68 65 20 50 32 20 76 61 6c 75  d as the P2 valu
22078 65 20 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f  e of an operatio
22079 6e 2e 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0d  n.  Later, when.
2207a 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69 73  .** the label is
2207b 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 61 20 73   resolved to a s
2207c 70 65 63 69 66 69 63 20 61 64 64 72 65 73 73 2c  pecific address,
2207d 20 74 68 65 20 56 44 42 45 20 77 69 6c 6c 20 73   the VDBE will s
2207e 63 61 6e 0d 0a 2a 2a 20 74 68 72 6f 75 67 68 20  can..** through 
2207f 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 6c 69  its operation li
22080 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 61 6c  st and change al
22081 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 20 77  l values of P2 w
22082 68 69 63 68 20 6d 61 74 63 68 0d 0a 2a 2a 20 74  hich match..** t
22083 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 68  he label into th
22084 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 65  e resolved addre
22085 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ss...**..** The 
22086 56 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20  VDBE knows that 
22087 61 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20  a P2 value is a 
22088 6c 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61  label because la
22089 62 65 6c 73 20 61 72 65 0d 0a 2a 2a 20 61 6c 77  bels are..** alw
2208a 61 79 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64  ays negative and
2208b 20 50 32 20 76 61 6c 75 65 73 20 61 72 65 20 73   P2 values are s
2208c 75 70 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e  uppose to be non
2208d 2d 6e 65 67 61 74 69 76 65 2e 0d 0a 2a 2a 20 48  -negative...** H
2208e 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69 76 65  ence, a negative
2208f 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c   P2 value is a l
22090 61 62 65 6c 20 74 68 61 74 20 68 61 73 20 79 65  abel that has ye
22091 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76 65 64  t to be resolved
22092 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 5a 65 72 6f 20 69  ...**..** Zero i
22093 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20  s returned if a 
22094 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0d  malloc() fails..
22095 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
22096 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
22097 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62  dbeMakeLabel(Vdb
22098 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  e *p){..  int i;
22099 0d 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65  ..  i = p->nLabe
2209a 6c 2b 2b 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  l++;..  assert( 
2209b 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
2209c 41 47 49 43 5f 49 4e 49 54 20 29 3b 0d 0a 20 20  AGIC_INIT );..  
2209d 69 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c  if( i>=p->nLabel
2209e 41 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20 20 69 6e  Alloc ){..    in
2209f 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41  t n = p->nLabelA
220a0 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0d 0a 20 20 20  lloc*2 + 5;..   
220a1 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c   p->aLabel = sql
220a2 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
220a3 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c  ree(p->db, p->aL
220a4 61 62 65 6c 2c 0d 0a 20 20 20 20 20 20 20 20 20  abel,..         
220a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
220a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 2a                n*
220a7 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c  sizeof(p->aLabel
220a8 5b 30 5d 29 29 3b 0d 0a 20 20 20 20 70 2d 3e 6e  [0]));..    p->n
220a9 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71 6c  LabelAlloc = sql
220aa 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
220ab 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65  (p->db, p->aLabe
220ac 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61  l)/sizeof(p->aLa
220ad 62 65 6c 5b 30 5d 29 3b 0d 0a 20 20 7d 0d 0a 20  bel[0]);..  }.. 
220ae 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29   if( p->aLabel )
220af 7b 0d 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  {..    p->aLabel
220b0 5b 69 5d 20 3d 20 2d 31 3b 0d 0a 20 20 7d 0d 0a  [i] = -1;..  }..
220b1 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0d 0a    return -1-i;..
220b2 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 73 6f  }..../*..** Reso
220b3 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 6f  lve label "x" to
220b4 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73 20   be the address 
220b5 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  of the next inst
220b6 72 75 63 74 69 6f 6e 20 74 6f 0d 0a 2a 2a 20 62  ruction to..** b
220b7 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65  e inserted.  The
220b8 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d   parameter "x" m
220b9 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62  ust have been ob
220ba 74 61 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20  tained from..** 
220bb 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
220bc 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
220bd 61 62 65 6c 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c  abel()...*/..SQL
220be 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
220bf 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
220c0 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70  lveLabel(Vdbe *p
220c1 2c 20 69 6e 74 20 78 29 7b 0d 0a 20 20 69 6e 74  , int x){..  int
220c2 20 6a 20 3d 20 2d 31 2d 78 3b 0d 0a 20 20 61 73   j = -1-x;..  as
220c3 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
220c4 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
220c5 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6a 3e  );..  assert( j>
220c6 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 65  =0 && j<p->nLabe
220c7 6c 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 61  l );..  if( p->a
220c8 4c 61 62 65 6c 20 29 7b 0d 0a 20 20 20 20 70 2d  Label ){..    p-
220c9 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e  >aLabel[j] = p->
220ca 6e 4f 70 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  nOp;..  }..}....
220cb 2f 2a 0d 0a 2a 2a 20 4d 61 72 6b 20 74 68 65 20  /*..** Mark the 
220cc 56 44 42 45 20 61 73 20 6f 6e 65 20 74 68 61 74  VDBE as one that
220cd 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 72 75 6e   can only be run
220ce 20 6f 6e 65 20 74 69 6d 65 2e 0d 0a 2a 2f 0d 0a   one time...*/..
220cf 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
220d0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52  oid sqlite3VdbeR
220d1 75 6e 4f 6e 6c 79 4f 6e 63 65 28 56 64 62 65 20  unOnlyOnce(Vdbe 
220d2 2a 70 29 7b 0d 0a 20 20 70 2d 3e 72 75 6e 4f 6e  *p){..  p->runOn
220d3 6c 79 4f 6e 63 65 20 3d 20 31 3b 0d 0a 7d 0d 0a  lyOnce = 1;..}..
220d4 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
220d5 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 33  DEBUG /* sqlite3
220d6 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29  AssertMayAbort()
220d7 20 6c 6f 67 69 63 20 2a 2f 0d 0a 0d 0a 2f 2a 0d   logic */..../*.
220d8 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
220d9 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e 63 74  g type and funct
220da 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20  ion are used to 
220db 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
220dc 61 6c 6c 20 6f 70 63 6f 64 65 73 0d 0a 2a 2a 20  all opcodes..** 
220dd 69 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20 70  in a Vdbe main p
220de 72 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68 20  rogram and each 
220df 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  of the sub-progr
220e0 61 6d 73 20 28 74 72 69 67 67 65 72 73 29 20 69  ams (triggers) i
220e1 74 20 6d 61 79 20 0d 0a 2a 2a 20 69 6e 76 6f 6b  t may ..** invok
220e2 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e  e directly or in
220e3 64 69 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f  directly. It sho
220e4 75 6c 64 20 62 65 20 75 73 65 64 20 61 73 20 66  uld be used as f
220e5 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ollows:..**..** 
220e6 20 20 4f 70 20 2a 70 4f 70 3b 0d 0a 2a 2a 20 20    Op *pOp;..**  
220e7 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65   VdbeOpIter sIte
220e8 72 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 6d 65 6d  r;..**..**   mem
220e9 73 65 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73  set(&sIter, 0, s
220ea 69 7a 65 6f 66 28 73 49 74 65 72 29 29 3b 0d 0a  izeof(sIter));..
220eb 2a 2a 20 20 20 73 49 74 65 72 2e 76 20 3d 20 76  **   sIter.v = v
220ec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
220ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20               // 
220ee 76 20 69 73 20 6f 66 20 74 79 70 65 20 56 64 62  v is of type Vdb
220ef 65 2a 20 0d 0a 2a 2a 20 20 20 77 68 69 6c 65 28  e* ..**   while(
220f0 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65   (pOp = opIterNe
220f1 78 74 28 26 73 49 74 65 72 29 29 20 29 7b 0d 0a  xt(&sIter)) ){..
220f2 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 73 6f 6d  **     // Do som
220f3 65 74 68 69 6e 67 20 77 69 74 68 20 70 4f 70 0d  ething with pOp.
220f4 0a 2a 2a 20 20 20 7d 0d 0a 2a 2a 20 20 20 73 71  .**   }..**   sq
220f5 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64  lite3DbFree(v->d
220f6 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b  b, sIter.apSub);
220f7 0d 0a 2a 2a 20 0d 0a 2a 2f 0d 0a 74 79 70 65 64  ..** ..*/..typed
220f8 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70  ef struct VdbeOp
220f9 49 74 65 72 20 56 64 62 65 4f 70 49 74 65 72 3b  Iter VdbeOpIter;
220fa 0d 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 49  ..struct VdbeOpI
220fb 74 65 72 20 7b 0d 0a 20 20 56 64 62 65 20 2a 76  ter {..  Vdbe *v
220fc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
220fd 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 69      /* Vdbe to i
220fe 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74  terate through t
220ff 68 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a 2f  he opcodes of */
22100 0d 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a  ..  SubProgram *
22101 2a 61 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f  *apSub;        /
22102 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 70 72  * Array of subpr
22103 6f 67 72 61 6d 73 20 2a 2f 0d 0a 20 20 69 6e 74  ograms */..  int
22104 20 6e 53 75 62 3b 20 20 20 20 20 20 20 20 20 20   nSub;          
22105 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
22106 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
22107 61 70 53 75 62 20 2a 2f 0d 0a 20 20 69 6e 74 20  apSub */..  int 
22108 69 41 64 64 72 3b 20 20 20 20 20 20 20 20 20 20  iAddr;          
22109 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2210a 73 20 6f 66 20 6e 65 78 74 20 69 6e 73 74 72 75  s of next instru
2210b 63 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20  ction to return 
2210c 2a 2f 0d 0a 20 20 69 6e 74 20 69 53 75 62 3b 20  */..  int iSub; 
2210d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2210e 20 2f 2a 20 30 20 3d 20 6d 61 69 6e 20 70 72 6f   /* 0 = main pro
2210f 67 72 61 6d 2c 20 31 20 3d 20 66 69 72 73 74 20  gram, 1 = first 
22110 73 75 62 2d 70 72 6f 67 72 61 6d 20 65 74 63 2e  sub-program etc.
22111 20 2a 2f 0d 0a 7d 3b 0d 0a 73 74 61 74 69 63 20   */..};..static 
22112 4f 70 20 2a 6f 70 49 74 65 72 4e 65 78 74 28 56  Op *opIterNext(V
22113 64 62 65 4f 70 49 74 65 72 20 2a 70 29 7b 0d 0a  dbeOpIter *p){..
22114 20 20 56 64 62 65 20 2a 76 20 3d 20 70 2d 3e 76    Vdbe *v = p->v
22115 3b 0d 0a 20 20 4f 70 20 2a 70 52 65 74 20 3d 20  ;..  Op *pRet = 
22116 30 3b 0d 0a 20 20 4f 70 20 2a 61 4f 70 3b 0d 0a  0;..  Op *aOp;..
22117 20 20 69 6e 74 20 6e 4f 70 3b 0d 0a 0d 0a 20 20    int nOp;....  
22118 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e  if( p->iSub<=p->
22119 6e 53 75 62 20 29 7b 0d 0a 0d 0a 20 20 20 20 69  nSub ){....    i
2211a 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 7b  f( p->iSub==0 ){
2211b 0d 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 76 2d  ..      aOp = v-
2211c 3e 61 4f 70 3b 0d 0a 20 20 20 20 20 20 6e 4f 70  >aOp;..      nOp
2211d 20 3d 20 76 2d 3e 6e 4f 70 3b 0d 0a 20 20 20 20   = v->nOp;..    
2211e 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 4f  }else{..      aO
2211f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e  p = p->apSub[p->
22120 69 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b 0d 0a 20  iSub-1]->aOp;.. 
22121 20 20 20 20 20 6e 4f 70 20 3d 20 70 2d 3e 61 70       nOp = p->ap
22122 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e  Sub[p->iSub-1]->
22123 6e 4f 70 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  nOp;..    }..   
22124 20 61 73 73 65 72 74 28 20 70 2d 3e 69 41 64 64   assert( p->iAdd
22125 72 3c 6e 4f 70 20 29 3b 0d 0a 0d 0a 20 20 20 20  r<nOp );....    
22126 70 52 65 74 20 3d 20 26 61 4f 70 5b 70 2d 3e 69  pRet = &aOp[p->i
22127 41 64 64 72 5d 3b 0d 0a 20 20 20 20 70 2d 3e 69  Addr];..    p->i
22128 41 64 64 72 2b 2b 3b 0d 0a 20 20 20 20 69 66 28  Addr++;..    if(
22129 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f 70 20 29   p->iAddr==nOp )
2212a 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 53 75 62  {..      p->iSub
2212b 2b 2b 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 41  ++;..      p->iA
2212c 64 64 72 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d  ddr = 0;..    }.
2212d 0a 20 20 0d 0a 20 20 20 20 69 66 28 20 70 52 65  .  ..    if( pRe
2212e 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55  t->p4type==P4_SU
2212f 42 50 52 4f 47 52 41 4d 20 29 7b 0d 0a 20 20 20  BPROGRAM ){..   
22130 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
22131 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f  p->nSub+1)*sizeo
22132 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0d  f(SubProgram*);.
22133 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20  .      int j;.. 
22134 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
22135 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0d 0a  p->nSub; j++){..
22136 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61          if( p->a
22137 70 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70  pSub[j]==pRet->p
22138 34 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65  4.pProgram ) bre
22139 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
2213a 20 20 20 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e 53      if( j==p->nS
2213b 75 62 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  ub ){..        p
2213c 2d 3e 61 70 53 75 62 20 3d 20 73 71 6c 69 74 65  ->apSub = sqlite
2213d 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
2213e 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 70 53 75 62  (v->db, p->apSub
2213f 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 20  , nByte);..     
22140 20 20 20 69 66 28 20 21 70 2d 3e 61 70 53 75 62     if( !p->apSub
22141 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70   ){..          p
22142 52 65 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  Ret = 0;..      
22143 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
22144 20 20 20 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e      p->apSub[p->
22145 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e  nSub++] = pRet->
22146 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0d 0a 20 20  p4.pProgram;..  
22147 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
22148 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
22149 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0d 0a    return pRet;..
2214a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63  }..../*..** Chec
2214b 6b 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d  k if the program
2214c 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56   stored in the V
2214d 4d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  M associated wit
2214e 68 20 70 50 61 72 73 65 20 6d 61 79 0d 0a 2a 2a  h pParse may..**
2214f 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 20   throw an ABORT 
22150 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 73 69  exception (causi
22151 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ng the statement
22152 2c 20 62 75 74 20 6e 6f 74 20 65 6e 74 69 72 65  , but not entire
22153 20 74 72 61 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2a   transaction..**
22154 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
22155 63 6b 29 2e 20 54 68 69 73 20 63 6f 6e 64 69 74  ck). This condit
22156 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74  ion is true if t
22157 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  he main program 
22158 6f 72 20 61 6e 79 0d 0a 2a 2a 20 73 75 62 2d 70  or any..** sub-p
22159 72 6f 67 72 61 6d 73 20 63 6f 6e 74 61 69 6e 73  rograms contains
2215a 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
2215b 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  owing:..**..**  
2215c 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69 74 68   *  OP_Halt with
2215d 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54   P1=SQLITE_CONST
2215e 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f  RAINT and P2=OE_
2215f 41 62 6f 72 74 2e 0d 0a 2a 2a 20 20 20 2a 20 20  Abort...**   *  
22160 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77 69  OP_HaltIfNull wi
22161 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e  th P1=SQLITE_CON
22162 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f  STRAINT and P2=O
22163 45 5f 41 62 6f 72 74 2e 0d 0a 2a 2a 20 20 20 2a  E_Abort...**   *
22164 20 20 4f 50 5f 44 65 73 74 72 6f 79 0d 0a 2a 2a    OP_Destroy..**
22165 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65     *  OP_VUpdate
22166 0d 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65  ..**   *  OP_VRe
22167 6e 61 6d 65 0d 0a 2a 2a 20 20 20 2a 20 20 4f 50  name..**   *  OP
22168 5f 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 68 20  _FkCounter with 
22169 50 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 74 65  P2==0 (immediate
2216a 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
2216b 73 74 72 61 69 6e 74 29 0d 0a 2a 2a 0d 0a 2a 2a  straint)..**..**
2216c 20 54 68 65 6e 20 63 68 65 63 6b 20 74 68 61 74   Then check that
2216d 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61   the value of Pa
2216e 72 73 65 2e 6d 61 79 41 62 6f 72 74 20 69 73 20  rse.mayAbort is 
2216f 74 72 75 65 20 69 66 20 61 6e 0d 0a 2a 2a 20 41  true if an..** A
22170 42 4f 52 54 20 6d 61 79 20 62 65 20 74 68 72 6f  BORT may be thro
22171 77 6e 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  wn, or false oth
22172 65 72 77 69 73 65 2e 20 52 65 74 75 72 6e 20 74  erwise. Return t
22173 72 75 65 20 69 66 20 69 74 20 64 6f 65 73 0d 0a  rue if it does..
22174 2a 2a 20 6d 61 74 63 68 2c 20 6f 72 20 66 61 6c  ** match, or fal
22175 73 65 20 6f 74 68 65 72 77 69 73 65 2e 20 54 68  se otherwise. Th
22176 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
22177 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73  ntended to be us
22178 65 64 20 61 73 0d 0a 2a 2a 20 70 61 72 74 20 6f  ed as..** part o
22179 66 20 61 6e 20 61 73 73 65 72 74 20 73 74 61 74  f an assert stat
2217a 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 63 6f 6d  ement in the com
2217b 70 69 6c 65 72 2e 20 53 69 6d 69 6c 61 72 20 74  piler. Similar t
2217c 6f 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 61 73 73  o:..**..**   ass
2217d 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62 65  ert( sqlite3Vdbe
2217e 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 70  AssertMayAbort(p
2217f 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 50  Parse->pVdbe, pP
22180 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 20  arse->mayAbort) 
22181 29 3b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  );..*/..SQLITE_P
22182 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
22183 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41  e3VdbeAssertMayA
22184 62 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 6e  bort(Vdbe *v, in
22185 74 20 6d 61 79 41 62 6f 72 74 29 7b 0d 0a 20 20  t mayAbort){..  
22186 69 6e 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30  int hasAbort = 0
22187 3b 0d 0a 20 20 4f 70 20 2a 70 4f 70 3b 0d 0a 20  ;..  Op *pOp;.. 
22188 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65   VdbeOpIter sIte
22189 72 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73 49  r;..  memset(&sI
2218a 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ter, 0, sizeof(s
2218b 49 74 65 72 29 29 3b 0d 0a 20 20 73 49 74 65 72  Iter));..  sIter
2218c 2e 76 20 3d 20 76 3b 0d 0a 0d 0a 20 20 77 68 69  .v = v;....  whi
2218d 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65  le( (pOp = opIte
2218e 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 21 3d  rNext(&sIter))!=
2218f 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6f 70  0 ){..    int op
22190 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f  code = pOp->opco
22191 64 65 3b 0d 0a 20 20 20 20 69 66 28 20 6f 70 63  de;..    if( opc
22192 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20  ode==OP_Destroy 
22193 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55  || opcode==OP_VU
22194 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d  pdate || opcode=
22195 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 0d 0a 23 69  =OP_VRename ..#i
22196 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22197 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0d 0a 20  T_FOREIGN_KEY.. 
22198 20 20 20 20 7c 7c 20 28 6f 70 63 6f 64 65 3d 3d      || (opcode==
22199 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 26 26 20  OP_FkCounter && 
2219a 70 4f 70 2d 3e 70 31 3d 3d 30 20 26 26 20 70 4f  pOp->p1==0 && pO
2219b 70 2d 3e 70 32 3d 3d 31 29 20 0d 0a 23 65 6e 64  p->p2==1) ..#end
2219c 69 66 0d 0a 20 20 20 20 20 7c 7c 20 28 28 6f 70  if..     || ((op
2219d 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 7c 7c  code==OP_Halt ||
2219e 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74   opcode==OP_Halt
2219f 49 66 4e 75 6c 6c 29 20 0d 0a 20 20 20 20 20 20  IfNull) ..      
221a0 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c  && (pOp->p1==SQL
221a1 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26  ITE_CONSTRAINT &
221a2 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62  & pOp->p2==OE_Ab
221a3 6f 72 74 29 29 0d 0a 20 20 20 20 29 7b 0d 0a 20  ort))..    ){.. 
221a4 20 20 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20       hasAbort = 
221a5 31 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1;..      break;
221a6 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
221a7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d  sqlite3DbFree(v-
221a8 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62  >db, sIter.apSub
221a9 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 74 75 72  );....  /* Retur
221aa 6e 20 74 72 75 65 20 69 66 20 68 61 73 41 62 6f  n true if hasAbo
221ab 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72  rt==mayAbort. Or
221ac 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
221ad 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0d 0a 20  lure occured... 
221ae 20 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61   ** If malloc fa
221af 69 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 77  iled, then the w
221b0 68 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76  hile() loop abov
221b1 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 69  e may not have i
221b2 74 65 72 61 74 65 64 0d 0a 20 20 2a 2a 20 74 68  terated..  ** th
221b3 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65  rough all opcode
221b4 73 20 61 6e 64 20 68 61 73 41 62 6f 72 74 20 6d  s and hasAbort m
221b5 61 79 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72  ay be set incorr
221b6 65 63 74 6c 79 2e 20 52 65 74 75 72 6e 0d 0a 20  ectly. Return.. 
221b7 20 2a 2a 20 74 72 75 65 20 66 6f 72 20 74 68 69   ** true for thi
221b8 73 20 63 61 73 65 20 74 6f 20 70 72 65 76 65 6e  s case to preven
221b9 74 20 74 68 65 20 61 73 73 65 72 74 28 29 20 69  t the assert() i
221ba 6e 20 74 68 65 20 63 61 6c 6c 65 72 73 20 66 72  n the callers fr
221bb 61 6d 65 0d 0a 20 20 2a 2a 20 66 72 6f 6d 20 66  ame..  ** from f
221bc 61 69 6c 69 6e 67 2e 20 20 2a 2f 0d 0a 20 20 72  ailing.  */..  r
221bd 65 74 75 72 6e 20 28 20 76 2d 3e 64 62 2d 3e 6d  eturn ( v->db->m
221be 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 68  allocFailed || h
221bf 61 73 41 62 6f 72 74 3d 3d 6d 61 79 41 62 6f 72  asAbort==mayAbor
221c0 74 20 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20  t );..}..#endif 
221c1 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
221c2 2d 20 74 68 65 20 73 71 6c 69 74 65 33 41 73 73  - the sqlite3Ass
221c3 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 66 75  ertMayAbort() fu
221c4 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  nction */..../*.
221c5 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68  .** Loop through
221c6 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f   the program loo
221c7 6b 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75  king for P2 valu
221c8 65 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61  es that are nega
221c9 74 69 76 65 0d 0a 2a 2a 20 6f 6e 20 6a 75 6d 70  tive..** on jump
221ca 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20   instructions.  
221cb 45 61 63 68 20 73 75 63 68 20 76 61 6c 75 65 20  Each such value 
221cc 69 73 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73  is a label.  Res
221cd 6f 6c 76 65 20 74 68 65 0d 0a 2a 2a 20 6c 61 62  olve the..** lab
221ce 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20 74 68  el by setting th
221cf 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20 69 74  e P2 value to it
221d0 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65  s correct non-ze
221d1 72 6f 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a  ro value...**..*
221d2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
221d3 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66  s called once af
221d4 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  ter all opcodes 
221d5 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74  have been insert
221d6 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 56 61 72 69  ed...**..** Vari
221d7 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72  able *pMaxFuncAr
221d8 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  gs is set to the
221d9 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f   maximum value o
221da 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e  f any P2 argumen
221db 74 20 0d 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f  t ..** to an OP_
221dc 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67  Function, OP_Agg
221dd 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74  Step or OP_VFilt
221de 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20  er opcode. This 
221df 69 73 20 75 73 65 64 20 62 79 20 0d 0a 2a 2a 20  is used by ..** 
221e0 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
221e1 65 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74  eady() to size t
221e2 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20  he Vdbe.apArg[] 
221e3 61 72 72 61 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  array...**..** T
221e4 68 65 20 4f 70 2e 6f 70 66 6c 61 67 73 20 66 69  he Op.opflags fi
221e5 65 6c 64 20 69 73 20 73 65 74 20 6f 6e 20 61 6c  eld is set on al
221e6 6c 20 6f 70 63 6f 64 65 73 2e 0d 0a 2a 2f 0d 0a  l opcodes...*/..
221e7 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f  static void reso
221e8 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 65  lveP2Values(Vdbe
221e9 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75   *p, int *pMaxFu
221ea 6e 63 41 72 67 73 29 7b 0d 0a 20 20 69 6e 74 20  ncArgs){..  int 
221eb 69 3b 0d 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72  i;..  int nMaxAr
221ec 67 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 72  gs = *pMaxFuncAr
221ed 67 73 3b 0d 0a 20 20 4f 70 20 2a 70 4f 70 3b 0d  gs;..  Op *pOp;.
221ee 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d  .  int *aLabel =
221ef 20 70 2d 3e 61 4c 61 62 65 6c 3b 0d 0a 20 20 70   p->aLabel;..  p
221f0 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0d  ->readOnly = 1;.
221f1 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f  .  for(pOp=p->aO
221f2 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69  p, i=p->nOp-1; i
221f3 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29  >=0; i--, pOp++)
221f4 7b 0d 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65  {..    u8 opcode
221f5 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0d   = pOp->opcode;.
221f6 0a 0d 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 66 6c  ...    pOp->opfl
221f7 61 67 73 20 3d 20 73 71 6c 69 74 65 33 4f 70 63  ags = sqlite3Opc
221f8 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f  odeProperty[opco
221f9 64 65 5d 3b 0d 0a 20 20 20 20 69 66 28 20 6f 70  de];..    if( op
221fa 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f  code==OP_Functio
221fb 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f  n || opcode==OP_
221fc 41 67 67 53 74 65 70 20 29 7b 0d 0a 20 20 20 20  AggStep ){..    
221fd 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d    if( pOp->p5>nM
221fe 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67  axArgs ) nMaxArg
221ff 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0d 0a 20 20  s = pOp->p5;..  
22200 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 63    }else if( (opc
22201 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74  ode==OP_Transact
22202 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d  ion && pOp->p2!=
22203 30 29 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50  0) || opcode==OP
22204 5f 56 61 63 75 75 6d 20 29 7b 0d 0a 20 20 20 20  _Vacuum ){..    
22205 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20    p->readOnly = 
22206 30 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0;..#ifndef SQLI
22207 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
22208 41 42 4c 45 0d 0a 20 20 20 20 7d 65 6c 73 65 20  ABLE..    }else 
22209 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  if( opcode==OP_V
2220a 55 70 64 61 74 65 20 29 7b 0d 0a 20 20 20 20 20  Update ){..     
2220b 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61   if( pOp->p2>nMa
2220c 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73  xArgs ) nMaxArgs
2220d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0d 0a 20 20 20   = pOp->p2;..   
2220e 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64   }else if( opcod
2220f 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b  e==OP_VFilter ){
22210 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0d 0a  ..      int n;..
22211 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
22212 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b  >nOp - i >= 3 );
22213 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
22214 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
22215 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0d 0a 20  OP_Integer );.. 
22216 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d       n = pOp[-1]
22217 2e 70 31 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  .p1;..      if( 
22218 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  n>nMaxArgs ) nMa
22219 78 41 72 67 73 20 3d 20 6e 3b 0d 0a 23 65 6e 64  xArgs = n;..#end
2221a 69 66 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  if..    }else if
2221b 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 78  ( opcode==OP_Nex
2221c 74 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f  t || opcode==OP_
2221d 53 6f 72 74 65 72 4e 65 78 74 20 29 7b 0d 0a 20  SorterNext ){.. 
2221e 20 20 20 20 20 70 4f 70 2d 3e 70 34 2e 78 41 64       pOp->p4.xAd
2221f 76 61 6e 63 65 20 3d 20 73 71 6c 69 74 65 33 42  vance = sqlite3B
22220 74 72 65 65 4e 65 78 74 3b 0d 0a 20 20 20 20 20  treeNext;..     
22221 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
22222 34 5f 41 44 56 41 4e 43 45 3b 0d 0a 20 20 20 20  4_ADVANCE;..    
22223 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
22224 3d 3d 4f 50 5f 50 72 65 76 20 29 7b 0d 0a 20 20  ==OP_Prev ){..  
22225 20 20 20 20 70 4f 70 2d 3e 70 34 2e 78 41 64 76      pOp->p4.xAdv
22226 61 6e 63 65 20 3d 20 73 71 6c 69 74 65 33 42 74  ance = sqlite3Bt
22227 72 65 65 50 72 65 76 69 6f 75 73 3b 0d 0a 20 20  reePrevious;..  
22228 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
22229 3d 20 50 34 5f 41 44 56 41 4e 43 45 3b 0d 0a 20  = P4_ADVANCE;.. 
2222a 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20     }....    if( 
2222b 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20  (pOp->opflags & 
2222c 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d 30 20 26  OPFLG_JUMP)!=0 &
2222d 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0d 0a  & pOp->p2<0 ){..
2222e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31        assert( -1
2222f 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62  -pOp->p2<p->nLab
22230 65 6c 20 29 3b 0d 0a 20 20 20 20 20 20 70 4f 70  el );..      pOp
22231 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31  ->p2 = aLabel[-1
22232 2d 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 20 20  -pOp->p2];..    
22233 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  }..  }..  sqlite
22234 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70  3DbFree(p->db, p
22235 2d 3e 61 4c 61 62 65 6c 29 3b 0d 0a 20 20 70 2d  ->aLabel);..  p-
22236 3e 61 4c 61 62 65 6c 20 3d 20 30 3b 0d 0a 0d 0a  >aLabel = 0;....
22237 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 20    *pMaxFuncArgs 
22238 3d 20 6e 4d 61 78 41 72 67 73 3b 0d 0a 7d 0d 0a  = nMaxArgs;..}..
22239 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
2223a 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
2223b 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
2223c 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ion to be insert
2223d 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ed...*/..SQLITE_
2223e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2223f 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
22240 64 72 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20  dr(Vdbe *p){..  
22241 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
22242 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
22243 54 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70  T );..  return p
22244 2d 3e 6e 4f 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ->nOp;..}..../*.
22245 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
22246 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  n returns a poin
22247 74 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79  ter to the array
22248 20 6f 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f   of opcodes asso
22249 63 69 61 74 65 64 20 77 69 74 68 0d 0a 2a 2a 20  ciated with..** 
2224a 74 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20  the Vdbe passed 
2224b 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2224c 75 6d 65 6e 74 2e 20 49 74 20 69 73 20 74 68 65  ument. It is the
2224d 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73   callers respons
2224e 69 62 69 6c 69 74 79 0d 0a 2a 2a 20 74 6f 20 61  ibility..** to a
2224f 72 72 61 6e 67 65 20 66 6f 72 20 74 68 65 20 72  rrange for the r
22250 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 74 6f  eturned array to
22251 20 62 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   be eventually f
22252 72 65 65 64 20 75 73 69 6e 67 20 74 68 65 20 0d  reed using the .
22253 0a 2a 2a 20 76 64 62 65 46 72 65 65 4f 70 41 72  .** vdbeFreeOpAr
22254 72 61 79 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0d  ray() function..
22255 0a 2a 2a 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 72  .**..** Before r
22256 65 74 75 72 6e 69 6e 67 2c 20 2a 70 6e 4f 70 20  eturning, *pnOp 
22257 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
22258 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
22259 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 0d  in the returned.
2225a 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c 73 6f 2c  .** array. Also,
2225b 20 2a 70 6e 4d 61 78 41 72 67 20 69 73 20 73 65   *pnMaxArg is se
2225c 74 20 74 6f 20 74 68 65 20 6c 61 72 67 65 72 20  t to the larger 
2225d 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74 20 76  of its current v
2225e 61 6c 75 65 20 61 6e 64 20 0d 0a 2a 2a 20 74 68  alue and ..** th
2225f 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
22260 69 65 73 20 69 6e 20 74 68 65 20 56 64 62 65 2e  ies in the Vdbe.
22261 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20 72 65  apArg[] array re
22262 71 75 69 72 65 64 20 74 6f 20 65 78 65 63 75 74  quired to execut
22263 65 20 74 68 65 20 0d 0a 2a 2a 20 72 65 74 75 72  e the ..** retur
22264 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f  ned program...*/
22265 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
22266 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
22267 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28  VdbeTakeOpArray(
22268 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e  Vdbe *p, int *pn
22269 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72  Op, int *pnMaxAr
2226a 67 29 7b 0d 0a 20 20 56 64 62 65 4f 70 20 2a 61  g){..  VdbeOp *a
2226b 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0d 0a 20 20  Op = p->aOp;..  
2226c 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 20 21  assert( aOp && !
2226d 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
2226e 6c 65 64 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43  led );....  /* C
2226f 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65  heck that sqlite
22270 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29  3VdbeUsesBtree()
22271 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20   was not called 
22272 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0d 0a 20  on this VM */.. 
22273 20 61 73 73 65 72 74 28 20 70 2d 3e 62 74 72 65   assert( p->btre
22274 65 4d 61 73 6b 3d 3d 30 20 29 3b 0d 0a 0d 0a 20  eMask==0 );.... 
22275 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
22276 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0d 0a  (p, pnMaxArg);..
22277 20 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70    *pnOp = p->nOp
22278 3b 0d 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b  ;..  p->aOp = 0;
22279 0d 0a 20 20 72 65 74 75 72 6e 20 61 4f 70 3b 0d  ..  return aOp;.
2227a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64  .}..../*..** Add
2227b 20 61 20 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66   a whole list of
2227c 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 74   operations to t
2227d 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 73 74 61  he operation sta
2227e 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68 65 0d  ck.  Return the.
2227f 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f 66 20 74  .** address of t
22280 68 65 20 66 69 72 73 74 20 6f 70 65 72 61 74 69  he first operati
22281 6f 6e 20 61 64 64 65 64 2e 0d 0a 2a 2f 0d 0a 53  on added...*/..S
22282 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22283 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
22284 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c 20  OpList(Vdbe *p, 
22285 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c  int nOp, VdbeOpL
22286 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 7b  ist const *aOp){
22287 0d 0a 20 20 69 6e 74 20 61 64 64 72 3b 0d 0a 20  ..  int addr;.. 
22288 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
22289 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
2228a 49 54 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  IT );..  if( p->
2228b 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e  nOp + nOp > p->n
2228c 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f  OpAlloc && growO
2228d 70 41 72 72 61 79 28 70 29 20 29 7b 0d 0a 20 20  pArray(p) ){..  
2228e 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
2228f 0d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f  ..  addr = p->nO
22290 70 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53  p;..  if( ALWAYS
22291 28 6e 4f 70 3e 30 29 20 29 7b 0d 0a 20 20 20 20  (nOp>0) ){..    
22292 69 6e 74 20 69 3b 0d 0a 20 20 20 20 56 64 62 65  int i;..    Vdbe
22293 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49  OpList const *pI
22294 6e 20 3d 20 61 4f 70 3b 0d 0a 20 20 20 20 66 6f  n = aOp;..    fo
22295 72 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b  r(i=0; i<nOp; i+
22296 2b 2c 20 70 49 6e 2b 2b 29 7b 0d 0a 20 20 20 20  +, pIn++){..    
22297 20 20 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e    int p2 = pIn->
22298 70 32 3b 0d 0a 20 20 20 20 20 20 56 64 62 65 4f  p2;..      VdbeO
22299 70 20 2a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f  p *pOut = &p->aO
2229a 70 5b 69 2b 61 64 64 72 5d 3b 0d 0a 20 20 20 20  p[i+addr];..    
2229b 20 20 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 20 3d    pOut->opcode =
2229c 20 70 49 6e 2d 3e 6f 70 63 6f 64 65 3b 0d 0a 20   pIn->opcode;.. 
2229d 20 20 20 20 20 70 4f 75 74 2d 3e 70 31 20 3d 20       pOut->p1 = 
2229e 70 49 6e 2d 3e 70 31 3b 0d 0a 20 20 20 20 20 20  pIn->p1;..      
2229f 69 66 28 20 70 32 3c 30 20 26 26 20 28 73 71 6c  if( p2<0 && (sql
222a0 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 72  ite3OpcodeProper
222a1 74 79 5b 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 5d  ty[pOut->opcode]
222a2 20 26 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d   & OPFLG_JUMP)!=
222a3 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 4f  0 ){..        pO
222a4 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 20 2b 20  ut->p2 = addr + 
222a5 41 44 44 52 28 70 32 29 3b 0d 0a 20 20 20 20 20  ADDR(p2);..     
222a6 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
222a7 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b 0d   pOut->p2 = p2;.
222a8 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
222a9 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e  pOut->p3 = pIn->
222aa 70 33 3b 0d 0a 20 20 20 20 20 20 70 4f 75 74 2d  p3;..      pOut-
222ab 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
222ac 55 53 45 44 3b 0d 0a 20 20 20 20 20 20 70 4f 75  USED;..      pOu
222ad 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0d 0a 20 20  t->p4.p = 0;..  
222ae 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30      pOut->p5 = 0
222af 3b 0d 0a 2f 2f 23 69 66 64 65 66 20 53 51 4c 49  ;..//#ifdef SQLI
222b0 54 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 20 20  TE_DEBUG..      
222b1 70 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d  pOut->zComment =
222b2 20 30 3b 0d 0a 20 20 20 20 20 20 2f 2f 69 66 28   0;..      //if(
222b3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f   sqlite3VdbeAddo
222b4 70 54 72 61 63 65 20 29 7b 0d 0a 20 20 20 20 20  pTrace ){..     
222b5 20 2f 2f 20 20 73 71 6c 69 74 65 33 56 64 62 65   //  sqlite3Vdbe
222b6 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 64 64  PrintOp(0, i+add
222b7 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64  r, &p->aOp[i+add
222b8 72 5d 29 3b 0d 0a 20 20 20 20 20 20 2f 2f 7d 0d  r]);..      //}.
222b9 0a 2f 2f 23 65 6e 64 69 66 0d 0a 20 20 20 20 7d  .//#endif..    }
222ba 0d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20  ..    p->nOp += 
222bb 6e 4f 70 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  nOp;..  }..  ret
222bc 75 72 6e 20 61 64 64 72 3b 0d 0a 7d 0d 0a 0d 0a  urn addr;..}....
222bd 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  /*..** Change th
222be 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
222bf 31 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  1 operand for a 
222c0 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
222c1 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 69 73 20 72  tion...** This r
222c2 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c  outine is useful
222c3 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72   when a large pr
222c4 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20  ogram is loaded 
222c5 66 72 6f 6d 20 61 0d 0a 2a 2a 20 73 74 61 74 69  from a..** stati
222c6 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71  c array using sq
222c7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
222c8 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74  st but we want t
222c9 6f 20 6d 61 6b 65 20 61 0d 0a 2a 2a 20 66 65 77  o make a..** few
222ca 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74   minor changes t
222cb 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0d 0a  o the program...
222cc 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
222cd 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
222ce 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65  dbeChangeP1(Vdbe
222cf 20 2a 70 2c 20 75 33 32 20 61 64 64 72 2c 20 69   *p, u32 addr, i
222d0 6e 74 20 76 61 6c 29 7b 0d 0a 20 20 61 73 73 65  nt val){..  asse
222d1 72 74 28 20 70 21 3d 30 20 29 3b 0d 0a 20 20 69  rt( p!=0 );..  i
222d2 66 28 20 28 28 75 33 32 29 70 2d 3e 6e 4f 70 29  f( ((u32)p->nOp)
222d3 3e 61 64 64 72 20 29 7b 0d 0a 20 20 20 20 70 2d  >addr ){..    p-
222d4 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20  >aOp[addr].p1 = 
222d5 76 61 6c 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  val;..  }..}....
222d6 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  /*..** Change th
222d7 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
222d8 32 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  2 operand for a 
222d9 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
222da 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 69 73 20 72  tion...** This r
222db 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c  outine is useful
222dc 20 66 6f 72 20 73 65 74 74 69 6e 67 20 61 20 6a   for setting a j
222dd 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e  ump destination.
222de 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
222df 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
222e0 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64  3VdbeChangeP2(Vd
222e1 62 65 20 2a 70 2c 20 75 33 32 20 61 64 64 72 2c  be *p, u32 addr,
222e2 20 69 6e 74 20 76 61 6c 29 7b 0d 0a 20 20 61 73   int val){..  as
222e3 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0d 0a 20  sert( p!=0 );.. 
222e4 20 69 66 28 20 28 28 75 33 32 29 70 2d 3e 6e 4f   if( ((u32)p->nO
222e5 70 29 3e 61 64 64 72 20 29 7b 0d 0a 20 20 20 20  p)>addr ){..    
222e6 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20  p->aOp[addr].p2 
222e7 3d 20 76 61 6c 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  = val;..  }..}..
222e8 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20  ../*..** Change 
222e9 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
222ea 20 50 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P3 operand for 
222eb 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72  a specific instr
222ec 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c  uction...*/..SQL
222ed 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
222ee 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
222ef 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 75 33  geP3(Vdbe *p, u3
222f0 32 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29  2 addr, int val)
222f1 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  {..  assert( p!=
222f2 30 20 29 3b 0d 0a 20 20 69 66 28 20 28 28 75 33  0 );..  if( ((u3
222f3 32 29 70 2d 3e 6e 4f 70 29 3e 61 64 64 72 20 29  2)p->nOp)>addr )
222f4 7b 0d 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64  {..    p->aOp[ad
222f5 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0d 0a 20  dr].p3 = val;.. 
222f6 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
222f7 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
222f8 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 61   of the P5 opera
222f9 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20  nd for the most 
222fa 72 65 63 65 6e 74 6c 79 0d 0a 2a 2a 20 61 64 64  recently..** add
222fb 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0d 0a 2a  ed operation...*
222fc 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
222fd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
222fe 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20  beChangeP5(Vdbe 
222ff 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0d 0a 20 20  *p, u8 val){..  
22300 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0d  assert( p!=0 );.
22301 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b  .  if( p->aOp ){
22302 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
22303 3e 6e 4f 70 3e 30 20 29 3b 0d 0a 20 20 20 20 70  >nOp>0 );..    p
22304 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
22305 70 35 20 3d 20 76 61 6c 3b 0d 0a 20 20 7d 0d 0a  p5 = val;..  }..
22306 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e  }..../*..** Chan
22307 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  ge the P2 operan
22308 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  d of instruction
22309 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74   addr so that it
2230a 20 70 6f 69 6e 74 73 20 74 6f 0d 0a 2a 2a 20 74   points to..** t
2230b 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  he address of th
2230c 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
2230d 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0d  on to be coded..
2230e 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2230f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22310 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62  VdbeJumpHere(Vdb
22311 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b  e *p, int addr){
22312 0d 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72  ..  assert( addr
22313 3e 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  >=0 || p->db->ma
22314 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20  llocFailed );.. 
22315 20 69 66 28 20 61 64 64 72 3e 3d 30 20 29 20 73   if( addr>=0 ) s
22316 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
22317 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e  P2(p, addr, p->n
22318 4f 70 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  Op);..}....../*.
22319 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74  .** If the input
2231a 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
2231b 72 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c  re is ephemeral,
2231c 20 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20   then free it.  
2231d 49 66 0d 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44  If..** the FuncD
2231e 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d  ef is not epherm
2231f 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68  al, then do noth
22320 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ing...*/..static
22321 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65   void freeEpheme
22322 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ralFunction(sqli
22323 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66  te3 *db, FuncDef
22324 20 2a 70 44 65 66 29 7b 0d 0a 20 20 69 66 28 20   *pDef){..  if( 
22325 41 4c 57 41 59 53 28 70 44 65 66 29 20 26 26 20  ALWAYS(pDef) && 
22326 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
22327 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
22328 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c  )!=0 ){..    sql
22329 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2232a 44 65 66 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  Def);..  }..}...
2232b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62  .static void vdb
2232c 65 46 72 65 65 4f 70 41 72 72 61 79 28 73 71 6c  eFreeOpArray(sql
2232d 69 74 65 33 20 2a 2c 20 4f 70 20 2a 2c 20 69 6e  ite3 *, Op *, in
2232e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65  t);..../*..** De
2232f 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 20  lete a P4 value 
22330 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0d 0a 2a  if necessary...*
22331 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  /..static void f
22332 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64  reeP4(sqlite3 *d
22333 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76  b, int p4type, v
22334 6f 69 64 20 2a 70 34 29 7b 0d 0a 20 20 69 66 28  oid *p4){..  if(
22335 20 70 34 20 29 7b 0d 0a 20 20 20 20 61 73 73 65   p4 ){..    asse
22336 72 74 28 20 64 62 20 29 3b 0d 0a 20 20 20 20 73  rt( db );..    s
22337 77 69 74 63 68 28 20 70 34 74 79 70 65 20 29 7b  witch( p4type ){
22338 0d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  ..      case P4_
22339 52 45 41 4c 3a 0d 0a 20 20 20 20 20 20 63 61 73  REAL:..      cas
2233a 65 20 50 34 5f 49 4e 54 36 34 3a 0d 0a 20 20 20  e P4_INT64:..   
2233b 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d     case P4_DYNAM
2233c 49 43 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20  IC:..      case 
2233d 50 34 5f 4b 45 59 49 4e 46 4f 3a 0d 0a 20 20 20  P4_KEYINFO:..   
2233e 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52     case P4_INTAR
2233f 52 41 59 3a 0d 0a 20 20 20 20 20 20 63 61 73 65  RAY:..      case
22340 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
22341 4f 46 46 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20  OFF: {..        
22342 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22343 2c 20 70 34 29 3b 0d 0a 20 20 20 20 20 20 20 20  , p4);..        
22344 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d  break;..      }.
22345 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d  .      case P4_M
22346 50 52 49 4e 54 46 3a 20 7b 0d 0a 20 20 20 20 20  PRINTF: {..     
22347 20 20 20 69 66 28 20 64 62 2d 3e 70 6e 42 79 74     if( db->pnByt
22348 65 73 46 72 65 65 64 3d 3d 30 20 29 20 73 71 6c  esFreed==0 ) sql
22349 69 74 65 33 5f 66 72 65 65 28 70 34 29 3b 0d 0a  ite3_free(p4);..
2234a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
2234b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 63        }..      c
2234c 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a  ase P4_VDBEFUNC:
2234d 20 7b 0d 0a 20 20 20 20 20 20 20 20 56 64 62 65   {..        Vdbe
2234e 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20  Func *pVdbeFunc 
2234f 3d 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34  = (VdbeFunc *)p4
22350 3b 0d 0a 20 20 20 20 20 20 20 20 66 72 65 65 45  ;..        freeE
22351 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e  phemeralFunction
22352 28 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e  (db, pVdbeFunc->
22353 70 46 75 6e 63 29 3b 0d 0a 20 20 20 20 20 20 20  pFunc);..       
22354 20 69 66 28 20 64 62 2d 3e 70 6e 42 79 74 65 73   if( db->pnBytes
22355 46 72 65 65 64 3d 3d 30 20 29 20 73 71 6c 69 74  Freed==0 ) sqlit
22356 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44  e3VdbeDeleteAuxD
22357 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30  ata(pVdbeFunc, 0
22358 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
22359 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56  te3DbFree(db, pV
2235a 64 62 65 46 75 6e 63 29 3b 0d 0a 20 20 20 20 20  dbeFunc);..     
2235b 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
2235c 20 7d 0d 0a 20 20 20 20 20 20 63 61 73 65 20 50   }..      case P
2235d 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0d 0a 20 20  4_FUNCDEF: {..  
2235e 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65        freeEpheme
2235f 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ralFunction(db, 
22360 28 46 75 6e 63 44 65 66 2a 29 70 34 29 3b 0d 0a  (FuncDef*)p4);..
22361 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
22362 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 63        }..      c
22363 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0d 0a 20  ase P4_MEM: {.. 
22364 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 70         if( db->p
22365 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30 20 29  nBytesFreed==0 )
22366 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
22367 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 73  ite3ValueFree((s
22368 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34  qlite3_value*)p4
22369 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  );..        }els
2236a 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 4d 65  e{..          Me
2236b 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 34 3b  m *p = (Mem*)p4;
2236c 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
2236d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
2236e 3e 7a 4d 61 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20  >zMalloc);..    
2236f 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
22370 72 65 65 28 64 62 2c 20 70 29 3b 0d 0a 20 20 20  ree(db, p);..   
22371 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
22372 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d  break;..      }.
22373 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 56  .      case P4_V
22374 54 41 42 20 3a 20 7b 0d 0a 20 20 20 20 20 20 20  TAB : {..       
22375 20 69 66 28 20 64 62 2d 3e 70 6e 42 79 74 65 73   if( db->pnBytes
22376 46 72 65 65 64 3d 3d 30 20 29 20 73 71 6c 69 74  Freed==0 ) sqlit
22377 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54  e3VtabUnlock((VT
22378 61 62 6c 65 20 2a 29 70 34 29 3b 0d 0a 20 20 20  able *)p4);..   
22379 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2237a 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
2237b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72  ..}..../*..** Fr
2237c 65 65 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c  ee the space all
2237d 6f 63 61 74 65 64 20 66 6f 72 20 61 4f 70 20 61  ocated for aOp a
2237e 6e 64 20 61 6e 79 20 70 34 20 76 61 6c 75 65 73  nd any p4 values
2237f 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
22380 68 65 0d 0a 2a 2a 20 6f 70 63 6f 64 65 73 20 63  he..** opcodes c
22381 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 2e  ontained within.
22382 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 20 4e   If aOp is not N
22383 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ULL it is assume
22384 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0d 0a 2a  d to contain ..*
22385 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 0d  * nOp entries. .
22386 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
22387 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
22388 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70  (sqlite3 *db, Op
22389 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b   *aOp, int nOp){
2238a 0d 0a 20 20 69 66 28 20 61 4f 70 20 29 7b 0d 0a  ..  if( aOp ){..
2238b 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0d 0a 20 20      Op *pOp;..  
2238c 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70    for(pOp=aOp; p
2238d 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f  Op<&aOp[nOp]; pO
2238e 70 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 66 72 65  p++){..      fre
2238f 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74  eP4(db, pOp->p4t
22390 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b  ype, pOp->p4.p);
22391 0d 0a 2f 2f 23 69 66 64 65 66 20 53 51 4c 49 54  ..//#ifdef SQLIT
22392 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 20 20 73  E_DEBUG..      s
22393 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22394 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b   pOp->zComment);
22395 0d 0a 2f 2f 23 65 6e 64 69 66 20 20 20 20 20 0d  ..//#endif     .
22396 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73  .    }..  }..  s
22397 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22398 20 61 4f 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   aOp);..}..../*.
22399 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 53 75 62  .** Link the Sub
2239a 50 72 6f 67 72 61 6d 20 6f 62 6a 65 63 74 20 70  Program object p
2239b 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
2239c 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 6e 74  ond argument int
2239d 6f 20 74 68 65 20 6c 69 6e 6b 65 64 0d 0a 2a 2a  o the linked..**
2239e 20 6c 69 73 74 20 61 74 20 56 64 62 65 2e 70 53   list at Vdbe.pS
2239f 75 62 50 72 6f 67 72 61 6d 2e 20 54 68 69 73 20  ubProgram. This 
223a0 6c 69 73 74 20 69 73 20 75 73 65 64 20 74 6f 20  list is used to 
223a1 64 65 6c 65 74 65 20 61 6c 6c 20 73 75 62 2d 70  delete all sub-p
223a2 72 6f 67 72 61 6d 0d 0a 2a 2a 20 6f 62 6a 65 63  rogram..** objec
223a3 74 73 20 77 68 65 6e 20 74 68 65 20 56 4d 20 69  ts when the VM i
223a4 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75  s no longer requ
223a5 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ired...*/..SQLIT
223a6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
223a7 71 6c 69 74 65 33 56 64 62 65 4c 69 6e 6b 53 75  qlite3VdbeLinkSu
223a8 62 50 72 6f 67 72 61 6d 28 56 64 62 65 20 2a 70  bProgram(Vdbe *p
223a9 56 64 62 65 2c 20 53 75 62 50 72 6f 67 72 61 6d  Vdbe, SubProgram
223aa 20 2a 70 29 7b 0d 0a 20 20 70 2d 3e 70 4e 65 78   *p){..  p->pNex
223ab 74 20 3d 20 70 56 64 62 65 2d 3e 70 50 72 6f 67  t = pVdbe->pProg
223ac 72 61 6d 3b 0d 0a 20 20 70 56 64 62 65 2d 3e 70  ram;..  pVdbe->p
223ad 50 72 6f 67 72 61 6d 20 3d 20 70 3b 0d 0a 7d 0d  Program = p;..}.
223ae 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65  .../*..** Change
223af 20 74 68 65 20 6f 70 63 6f 64 65 20 61 74 20 61   the opcode at a
223b0 64 64 72 20 69 6e 74 6f 20 4f 50 5f 4e 6f 6f 70  ddr into OP_Noop
223b1 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
223b2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
223b3 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
223b4 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  p(Vdbe *p, int a
223b5 64 64 72 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e  ddr){..  if( p->
223b6 61 4f 70 20 29 7b 0d 0a 20 20 20 20 56 64 62 65  aOp ){..    Vdbe
223b7 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  Op *pOp = &p->aO
223b8 70 5b 61 64 64 72 5d 3b 0d 0a 20 20 20 20 73 71  p[addr];..    sq
223b9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
223ba 62 3b 0d 0a 20 20 20 20 66 72 65 65 50 34 28 64  b;..    freeP4(d
223bb 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
223bc 70 4f 70 2d 3e 70 34 2e 70 29 3b 0d 0a 20 20 20  pOp->p4.p);..   
223bd 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20   memset(pOp, 0, 
223be 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b  sizeof(pOp[0]));
223bf 0d 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64  ..    pOp->opcod
223c0 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0d 0a 20 20  e = OP_Noop;..  
223c1 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  }..}..../*..** C
223c2 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
223c3 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e  of the P4 operan
223c4 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  d for a specific
223c5 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a   instruction...*
223c6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
223c7 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20  s useful when a 
223c8 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73  large program is
223c9 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0d 0a   loaded from a..
223ca 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20  ** static array 
223cb 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  using sqlite3Vdb
223cc 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77  eAddOpList but w
223cd 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61  e want to make a
223ce 0d 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63  ..** few minor c
223cf 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72  hanges to the pr
223d0 6f 67 72 61 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  ogram...**..** I
223d1 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20  f n>=0 then the 
223d2 50 34 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79  P4 operand is dy
223d3 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74  namic, meaning t
223d4 68 61 74 20 61 20 63 6f 70 79 20 6f 66 0d 0a 2a  hat a copy of..*
223d5 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  * the string is 
223d6 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  made into memory
223d7 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
223d8 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
223d9 0d 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20  ..** A value of 
223da 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79 20  n==0 means copy 
223db 62 79 74 65 73 20 6f 66 20 7a 50 34 20 75 70 20  bytes of zP4 up 
223dc 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67  to and including
223dd 20 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 6e   the..** first n
223de 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e  ull byte.  If n>
223df 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20  0 then copy n+1 
223e0 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0d 0a 2a  bytes of zP4...*
223e1 2a 0d 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b  *..** If n==P4_K
223e2 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20  EYINFO it means 
223e3 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f  that zP4 is a po
223e4 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
223e5 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a  fo structure...*
223e6 2a 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65  * A copy is made
223e7 20 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20   of the KeyInfo 
223e8 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d  structure into m
223e9 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
223ea 72 6f 6d 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  rom..** sqlite3_
223eb 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72  malloc, to be fr
223ec 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62  eed when the Vdb
223ed 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0d  e is finalized..
223ee 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46  .** n==P4_KEYINF
223ef 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 61  O_HANDOFF indica
223f0 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 6f 69  tes that zP4 poi
223f1 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  nts to a KeyInfo
223f2 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 73   structure..** s
223f3 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
223f4 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
223f5 68 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  has obtained fro
223f6 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
223f7 2e 20 54 68 65 20 0d 0a 2a 2a 20 63 61 6c 6c 65  . The ..** calle
223f8 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65  r should not fre
223f9 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  e the allocation
223fa 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65  , it will be fre
223fb 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65  ed when the Vdbe
223fc 20 69 73 0d 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65   is..** finalize
223fd 64 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 4f 74 68 65  d...** ..** Othe
223fe 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50  r values of n (P
223ff 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c  4_STATIC, P4_COL
22400 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63  LSEQ etc.) indic
22401 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69  ate that zP4 poi
22402 6e 74 73 0d 0a 2a 2a 20 74 6f 20 61 20 73 74 72  nts..** to a str
22403 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65  ing or structure
22404 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74   that is guarant
22405 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72  eed to exist for
22406 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
22407 0d 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49  ..** the Vdbe. I
22408 6e 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65  n these cases we
22409 20 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74   can just copy t
2240a 68 65 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d  he pointer...**.
2240b 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30 20 74 68  .** If addr<0 th
2240c 65 6e 20 63 68 61 6e 67 65 20 50 34 20 6f 6e 20  en change P4 on 
2240d 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
2240e 79 20 69 6e 73 65 72 74 65 64 20 69 6e 73 74 72  y inserted instr
2240f 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c  uction...*/..SQL
22410 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22411 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
22412 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 69 6e  geP4(Vdbe *p, in
22413 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68  t addr, const ch
22414 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e 29 7b  ar *zP4, int n){
22415 0d 0a 20 20 4f 70 20 2a 70 4f 70 3b 0d 0a 20 20  ..  Op *pOp;..  
22416 73 71 6c 69 74 65 33 20 2a 64 62 3b 0d 0a 20 20  sqlite3 *db;..  
22417 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0d  assert( p!=0 );.
22418 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a  .  db = p->db;..
22419 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
2241a 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
2241b 4e 49 54 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d  NIT );..  if( p-
2241c 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d  >aOp==0 || db->m
2241d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a  allocFailed ){..
2241e 20 20 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b      if ( n!=P4_K
2241f 45 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f  EYINFO && n!=P4_
22420 56 54 41 42 20 29 20 7b 0d 0a 20 20 20 20 20 20  VTAB ) {..      
22421 66 72 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 76  freeP4(db, n, (v
22422 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a  oid*)*(char**)&z
22423 50 34 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  P4);..    }..   
22424 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
22425 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e   assert( p->nOp>
22426 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
22427 61 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0d 0a  addr<p->nOp );..
22428 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0d    if( addr<0 ){.
22429 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e  .    addr = p->n
2242a 4f 70 20 2d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20  Op - 1;..  }..  
2242b 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64  pOp = &p->aOp[ad
2242c 64 72 5d 3b 0d 0a 20 20 66 72 65 65 50 34 28 64  dr];..  freeP4(d
2242d 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
2242e 70 4f 70 2d 3e 70 34 2e 70 29 3b 0d 0a 20 20 70  pOp->p4.p);..  p
2242f 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0d 0a 20  Op->p4.p = 0;.. 
22430 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32   if( n==P4_INT32
22431 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 4e 6f 74 65   ){..    /* Note
22432 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 73  : this cast is s
22433 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 65  afe, because the
22434 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69   origin data poi
22435 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0d 0a 20  nt was an int.. 
22436 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63     ** that was c
22437 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20  ast to a (const 
22438 63 68 61 72 20 2a 29 2e 20 2a 2f 0d 0a 20 20 20  char *). */..   
22439 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c   pOp->p4.i = SQL
2243a 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a  ITE_PTR_TO_INT(z
2243b 50 34 29 3b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70  P4);..    pOp->p
2243c 34 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 32  4type = P4_INT32
2243d 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  ;..  }else if( z
2243e 50 34 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 4f  P4==0 ){..    pO
2243f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0d 0a 20 20  p->p4.p = 0;..  
22440 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
22441 50 34 5f 4e 4f 54 55 53 45 44 3b 0d 0a 20 20 7d  P4_NOTUSED;..  }
22442 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b  else if( n==P4_K
22443 45 59 49 4e 46 4f 20 29 7b 0d 0a 20 20 20 20 4b  EYINFO ){..    K
22444 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
22445 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 6c  ;..    int nFiel
22446 64 2c 20 6e 42 79 74 65 3b 0d 0a 0d 0a 20 20 20  d, nByte;....   
22447 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49   nField = ((KeyI
22448 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c  nfo*)zP4)->nFiel
22449 64 3b 0d 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  d;..    nByte = 
2244a 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f  sizeof(*pKeyInfo
2244b 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73  ) + (nField-1)*s
2244c 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e  izeof(pKeyInfo->
2244d 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65  aColl[0]) + nFie
2244e 6c 64 3b 0d 0a 20 20 20 20 70 4b 65 79 49 6e 66  ld;..    pKeyInf
2244f 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
22450 6c 6f 63 52 61 77 28 30 2c 20 6e 42 79 74 65 29  locRaw(0, nByte)
22451 3b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  ;..    pOp->p4.p
22452 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e  KeyInfo = pKeyIn
22453 66 6f 3b 0d 0a 20 20 20 20 69 66 28 20 70 4b 65  fo;..    if( pKe
22454 79 49 6e 66 6f 20 29 7b 0d 0a 20 20 20 20 20 20  yInfo ){..      
22455 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0d  u8 *aSortOrder;.
22456 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 28 63  .      memcpy((c
22457 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 7a  har*)pKeyInfo, z
22458 50 34 2c 20 6e 42 79 74 65 20 2d 20 6e 46 69 65  P4, nByte - nFie
22459 6c 64 29 3b 0d 0a 20 20 20 20 20 20 61 53 6f 72  ld);..      aSor
2245a 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 6e 66  tOrder = pKeyInf
2245b 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b 0d 0a  o->aSortOrder;..
2245c 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74 4f        if( aSortO
2245d 72 64 65 72 20 29 7b 0d 0a 20 20 20 20 20 20 20  rder ){..       
2245e 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
2245f 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65  Order = (unsigne
22460 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66  d char*)&pKeyInf
22461 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d  o->aColl[nField]
22462 3b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ;..        memcp
22463 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  y(pKeyInfo->aSor
22464 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64  tOrder, aSortOrd
22465 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0d 0a 20 20  er, nField);..  
22466 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 4f 70      }..      pOp
22467 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45  ->p4type = P4_KE
22468 59 49 4e 46 4f 3b 0d 0a 20 20 20 20 7d 65 6c 73  YINFO;..    }els
22469 65 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d  e{..      p->db-
2246a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2246b 31 3b 0d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70  1;..      pOp->p
2246c 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
2246d 45 44 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65  ED;..    }..  }e
2246e 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45  lse if( n==P4_KE
2246f 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b  YINFO_HANDOFF ){
22470 0d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20  ..    pOp->p4.p 
22471 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0d 0a 20  = (void*)zP4;.. 
22472 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
22473 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0d 0a 20 20   P4_KEYINFO;..  
22474 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f  }else if( n==P4_
22475 56 54 41 42 20 29 7b 0d 0a 20 20 20 20 70 4f 70  VTAB ){..    pOp
22476 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29  ->p4.p = (void*)
22477 7a 50 34 3b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70  zP4;..    pOp->p
22478 34 74 79 70 65 20 3d 20 50 34 5f 56 54 41 42 3b  4type = P4_VTAB;
22479 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  ..    sqlite3Vta
2247a 62 4c 6f 63 6b 28 28 56 54 61 62 6c 65 20 2a 29  bLock((VTable *)
2247b 7a 50 34 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  zP4);..    asser
2247c 74 28 20 28 28 56 54 61 62 6c 65 20 2a 29 7a 50  t( ((VTable *)zP
2247d 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b  4)->db==p->db );
2247e 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3c  ..  }else if( n<
2247f 30 20 29 7b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70  0 ){..    pOp->p
22480 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34  4.p = (void*)zP4
22481 3b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  ;..    pOp->p4ty
22482 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61  pe = (signed cha
22483 72 29 6e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  r)n;..  }else{..
22484 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e      if( n==0 ) n
22485 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
22486 33 30 28 7a 50 34 29 3b 0d 0a 20 20 20 20 70 4f  30(zP4);..    pO
22487 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65  p->p4.z = sqlite
22488 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62  3DbStrNDup(p->db
22489 2c 20 7a 50 34 2c 20 6e 29 3b 0d 0a 20 20 20 20  , zP4, n);..    
2248a 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
2248b 5f 44 59 4e 41 4d 49 43 3b 0d 0a 20 20 7d 0d 0a  _DYNAMIC;..  }..
2248c 7d 0d 0a 0d 0a 2f 2f 23 69 66 6e 64 65 66 20 4e  }....//#ifndef N
2248d 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68  DEBUG../*..** Ch
2248e 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74  ange the comment
2248f 20 6f 6e 20 74 68 65 20 74 68 65 20 6d 6f 73 74   on the the most
22490 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20   recently coded 
22491 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 4f 72  instruction.  Or
22492 0d 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 4e 6f  ..** insert a No
22493 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 65 20  -op and add the 
22494 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 74 20  comment to that 
22495 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  new instruction.
22496 20 20 54 68 69 73 0d 0a 2a 2a 20 6d 61 6b 65 73    This..** makes
22497 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 72   the code easier
22498 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 20   to read during 
22499 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e 65  debugging.  None
2249a 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   of this happens
2249b 0d 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63  ..** in a produc
2249c 74 69 6f 6e 20 62 75 69 6c 64 2e 0d 0a 2a 2f 0d  tion build...*/.
2249d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62  .static void vdb
2249e 65 56 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a  eVComment(Vdbe *
2249f 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
224a0 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20  Format, va_list 
224a1 61 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  ap){..  assert( 
224a2 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61  p->nOp>0 || p->a
224a3 4f 70 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  Op==0 );..  asse
224a4 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c  rt( p->aOp==0 ||
224a5 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31   p->aOp[p->nOp-1
224a6 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c  ].zComment==0 ||
224a7 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
224a8 69 6c 65 64 20 29 3b 0d 0a 20 20 69 66 28 20 70  iled );..  if( p
224a9 2d 3e 6e 4f 70 20 29 7b 0d 0a 20 20 20 20 61 73  ->nOp ){..    as
224aa 73 65 72 74 28 20 70 2d 3e 61 4f 70 20 29 3b 0d  sert( p->aOp );.
224ab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
224ac 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70  ee(p->db, p->aOp
224ad 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
224ae 65 6e 74 29 3b 0d 0a 20 20 20 20 70 2d 3e 61 4f  ent);..    p->aO
224af 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d  p[p->nOp-1].zCom
224b0 6d 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 4d  ment = sqlite3VM
224b1 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46  Printf(p->db, zF
224b2 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 7d  ormat, ap);..  }
224b3 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..}..SQLITE_PRIV
224b4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
224b5 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
224b6 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
224b7 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0d  *zFormat, ...){.
224b8 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a  .  va_list ap;..
224b9 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20    if( p ){..    
224ba 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
224bb 72 6d 61 74 29 3b 0d 0a 20 20 20 20 76 64 62 65  rmat);..    vdbe
224bc 56 43 6f 6d 6d 65 6e 74 28 70 2c 20 7a 46 6f 72  VComment(p, zFor
224bd 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 20 20 76  mat, ap);..    v
224be 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 7d 0d  a_end(ap);..  }.
224bf 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
224c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
224c1 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56  dbeNoopComment(V
224c2 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  dbe *p, const ch
224c3 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
224c4 29 7b 0d 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  ){..  va_list ap
224c5 3b 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20  ;..  if( p ){.. 
224c6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
224c7 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29  dOp0(p, OP_Noop)
224c8 3b 0d 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28  ;..    va_start(
224c9 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d 0a 20  ap, zFormat);.. 
224ca 20 20 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28     vdbeVComment(
224cb 70 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  p, zFormat, ap);
224cc 0d 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29  ..    va_end(ap)
224cd 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 2f 2f 23 65 6e  ;..  }..}..//#en
224ce 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a  dif  /* NDEBUG *
224cf 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  /..../*..** Retu
224d0 72 6e 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f  rn the opcode fo
224d1 72 20 61 20 67 69 76 65 6e 20 61 64 64 72 65 73  r a given addres
224d2 73 2e 20 20 49 66 20 74 68 65 20 61 64 64 72 65  s.  If the addre
224d3 73 73 20 69 73 20 2d 31 2c 20 74 68 65 6e 0d 0a  ss is -1, then..
224d4 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6d 6f  ** return the mo
224d5 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65  st recently inse
224d6 72 74 65 64 20 6f 70 63 6f 64 65 2e 0d 0a 2a 2a  rted opcode...**
224d7 0d 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  ..** If a memory
224d8 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
224d9 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 70  r has occurred p
224da 72 69 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c  rior to the call
224db 69 6e 67 20 6f 66 20 74 68 69 73 0d 0a 2a 2a 20  ing of this..** 
224dc 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 61 20  routine, then a 
224dd 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 75 6d  pointer to a dum
224de 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c 20 62  my VdbeOp will b
224df 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 61  e returned.  Tha
224e0 74 20 6f 70 63 6f 64 65 0d 0a 2a 2a 20 69 73 20  t opcode..** is 
224e1 72 65 61 64 61 62 6c 65 20 62 75 74 20 6e 6f 74  readable but not
224e2 20 77 72 69 74 61 62 6c 65 2c 20 74 68 6f 75 67   writable, thoug
224e3 68 20 69 74 20 69 73 20 63 61 73 74 20 74 6f 20  h it is cast to 
224e4 61 20 77 72 69 74 61 62 6c 65 20 76 61 6c 75 65  a writable value
224e5 2e 0d 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  ...** The return
224e6 20 6f 66 20 61 20 64 75 6d 6d 79 20 6f 70 63 6f   of a dummy opco
224e7 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61  de allows the ca
224e8 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66  ll to continue f
224e9 75 6e 63 74 69 6f 6e 69 6e 67 0d 0a 2a 2a 20 61  unctioning..** a
224ea 66 74 65 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74  fter a OOM fault
224eb 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
224ec 74 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  to check to see 
224ed 69 66 20 74 68 65 20 72 65 74 75 72 6e 20 66 72  if the return fr
224ee 6f 6d 20 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75  om ..** this rou
224ef 74 69 6e 65 20 69 73 20 61 20 76 61 6c 69 64 20  tine is a valid 
224f0 70 6f 69 6e 74 65 72 2e 20 20 42 75 74 20 62 65  pointer.  But be
224f1 63 61 75 73 65 20 74 68 65 20 64 75 6d 6d 79 2e  cause the dummy.
224f2 6f 70 63 6f 64 65 20 69 73 20 30 2c 0d 0a 2a 2a  opcode is 0,..**
224f3 20 64 75 6d 6d 79 20 77 69 6c 6c 20 6e 65 76 65   dummy will neve
224f4 72 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e  r be written to.
224f5 20 20 54 68 69 73 20 69 73 20 76 65 72 69 66 69    This is verifi
224f6 65 64 20 62 79 20 63 6f 64 65 20 69 6e 73 70 65  ed by code inspe
224f7 63 74 69 6f 6e 20 61 6e 64 0d 0a 2a 2a 20 62 79  ction and..** by
224f8 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 56 61   running with Va
224f9 6c 67 72 69 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  lgrind...**..** 
224fa 41 62 6f 75 74 20 74 68 65 20 23 69 66 64 65 66  About the #ifdef
224fb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
224fc 43 45 3a 20 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74  CE:  Normally, t
224fd 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
224fe 65 76 65 72 20 63 61 6c 6c 65 64 0d 0a 2a 2a 20  ever called..** 
224ff 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f 70 3e 30 2e  unless p->nOp>0.
22500 20 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73    This is becaus
22501 65 20 69 6e 20 74 68 65 20 61 62 73 65 6e 73 65  e in the absense
22502 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
22503 54 52 41 43 45 2c 0d 0a 2a 2a 20 61 6e 20 4f 50  TRACE,..** an OP
22504 5f 54 72 61 63 65 20 69 6e 73 74 72 75 63 74 69  _Trace instructi
22505 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 69 6e 73  on is always ins
22506 65 72 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  erted by sqlite3
22507 56 64 62 65 47 65 74 28 29 20 61 73 20 73 6f 6f  VdbeGet() as soo
22508 6e 20 61 73 0d 0a 2a 2a 20 61 20 6e 65 77 20 56  n as..** a new V
22509 44 42 45 20 69 73 20 63 72 65 61 74 65 64 2e 20  DBE is created. 
2250a 20 53 6f 20 77 65 20 61 72 65 20 66 72 65 65 20   So we are free 
2250b 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f 20 70  to set addr to p
2250c 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75 74 0d  ->nOp-1 without.
2250d 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64 6f  .** having to do
2250e 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61  uble-check to ma
2250f 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
22510 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d 6e   result is non-n
22511 65 67 61 74 69 76 65 2e 20 42 75 74 0d 0a 2a 2a  egative. But..**
22512 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
22513 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 64  TRACE is defined
22514 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69  , the OP_Trace i
22515 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65  s omitted and we
22516 20 64 6f 20 6e 65 65 64 20 74 6f 0d 0a 2a 2a 20   do need to..** 
22517 63 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20  check the value 
22518 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f  of p->nOp-1 befo
22519 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0d 0a  re continuing...
2251a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
2251b 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
2251c 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65  e3VdbeGetOp(Vdbe
2251d 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0d   *p, int addr){.
2251e 0a 20 20 2f 2a 20 43 38 39 20 73 70 65 63 69 66  .  /* C89 specif
2251f 69 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ies that the con
22520 73 74 61 6e 74 20 22 64 75 6d 6d 79 22 20 77 69  stant "dummy" wi
22521 6c 6c 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ll be initialize
22522 64 20 74 6f 20 61 6c 6c 0d 0a 20 20 2a 2a 20 7a  d to all..  ** z
22523 65 72 6f 73 2c 20 77 68 69 63 68 20 69 73 20 63  eros, which is c
22524 6f 72 72 65 63 74 2e 20 20 4d 53 56 43 20 67 65  orrect.  MSVC ge
22525 6e 65 72 61 74 65 73 20 61 20 77 61 72 6e 69 6e  nerates a warnin
22526 67 2c 20 6e 65 76 65 72 74 68 65 6c 65 73 73 2e  g, nevertheless.
22527 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 56 64   */..  static Vd
22528 62 65 4f 70 20 64 75 6d 6d 79 3b 20 20 2f 2a 20  beOp dummy;  /* 
22529 49 67 6e 6f 72 65 20 74 68 65 20 4d 53 56 43 20  Ignore the MSVC 
2252a 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 6e 6f  warning about no
2252b 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 2a 2f 0d   initializer */.
2252c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
2252d 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
2252e 49 4e 49 54 20 29 3b 0d 0a 20 20 69 66 28 20 61  INIT );..  if( a
2252f 64 64 72 3c 30 20 29 7b 0d 0a 23 69 66 64 65 66  ddr<0 ){..#ifdef
22530 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
22531 43 45 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  CE..    if( p->n
22532 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 28  Op==0 ) return (
22533 56 64 62 65 4f 70 2a 29 26 64 75 6d 6d 79 3b 0d  VdbeOp*)&dummy;.
22534 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 61 64 64  .#endif..    add
22535 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0d  r = p->nOp - 1;.
22536 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
22537 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72  (addr>=0 && addr
22538 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64  <p->nOp) || p->d
22539 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2253a 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d  );..  if( p->db-
2253b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2253c 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 56 64  ..    return (Vd
2253d 62 65 4f 70 2a 29 26 64 75 6d 6d 79 3b 0d 0a 20  beOp*)&dummy;.. 
2253e 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74   }else{..    ret
2253f 75 72 6e 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72  urn &p->aOp[addr
22540 5d 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 69  ];..  }..}....#i
22541 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
22542 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
22543 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
22544 55 47 29 20 5c 0d 0a 20 20 20 20 20 7c 7c 20 64  UG) \..     || d
22545 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46  efined(VDBE_PROF
22546 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ILE) || defined(
22547 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0d 0a 2f  SQLITE_DEBUG)../
22548 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20  *..** Compute a 
22549 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63  string that desc
2254a 72 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72  ribes the P4 par
2254b 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70  ameter for an op
2254c 63 6f 64 65 2e 0d 0a 2a 2a 20 55 73 65 20 7a 54  code...** Use zT
2254d 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75  emp for any requ
2254e 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62  ired temporary b
2254f 75 66 66 65 72 20 73 70 61 63 65 2e 0d 0a 2a 2f  uffer space...*/
22550 0d 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64  ..static char *d
22551 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70  isplayP4(Op *pOp
22552 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69  , char *zTemp, i
22553 6e 74 20 6e 54 65 6d 70 29 7b 0d 0a 20 20 63 68  nt nTemp){..  ch
22554 61 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b  ar *zP4 = zTemp;
22555 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d  ..  assert( nTem
22556 70 3e 3d 32 30 20 29 3b 0d 0a 20 20 73 77 69 74  p>=20 );..  swit
22557 63 68 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 20  ch( pOp->p4type 
22558 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 50 34 5f  ){..    case P4_
22559 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 3a 0d  KEYINFO_STATIC:.
2255a 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59  .    case P4_KEY
2255b 49 4e 46 4f 3a 20 7b 0d 0a 20 20 20 20 20 20 69  INFO: {..      i
2255c 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 20 20 20 20  nt i, j;..      
2255d 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2255e 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79  o = pOp->p4.pKey
2255f 49 6e 66 6f 3b 0d 0a 20 20 20 20 20 20 73 71 6c  Info;..      sql
22560 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
22561 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b 65 79  emp, zTemp, "key
22562 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 49 6e  info(%d", pKeyIn
22563 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0d 0a 20 20  fo->nField);..  
22564 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 53      i = sqlite3S
22565 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b 0d  trlen30(zTemp);.
22566 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
22567 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j<pKeyInfo->nFie
22568 6c 64 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20  ld; j++){..     
22569 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2256a 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  l = pKeyInfo->aC
2256b 6f 6c 6c 5b 6a 5d 3b 0d 0a 20 20 20 20 20 20 20  oll[j];..       
2256c 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0d 0a 20   if( pColl ){.. 
2256d 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d           int n =
2256e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2256f 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d  (pColl->zName);.
22570 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
22571 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0d 0a 20  +n>nTemp-6 ){.. 
22572 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
22573 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e  y(&zTemp[i],",..
22574 2e 22 2c 34 29 3b 0d 0a 20 20 20 20 20 20 20 20  .",4);..        
22575 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
22576 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
22577 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20     zTemp[i++] = 
22578 27 2c 27 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ',';..          
22579 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  if( pKeyInfo->aS
2257a 6f 72 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79  ortOrder && pKey
2257b 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
2257c 5b 6a 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  [j] ){..        
2257d 20 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d      zTemp[i++] =
2257e 20 27 2d 27 3b 0d 0a 20 20 20 20 20 20 20 20 20   '-';..         
2257f 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 6d 65   }..          me
22580 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 20  mcpy(&zTemp[i], 
22581 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31  pColl->zName,n+1
22582 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 20  );..          i 
22583 2b 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 20 20 7d  += n;..        }
22584 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54 65  else if( i+4<nTe
22585 6d 70 2d 36 20 29 7b 0d 0a 20 20 20 20 20 20 20  mp-6 ){..       
22586 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
22587 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0d 0a  [i],",nil",4);..
22588 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34            i += 4
22589 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
2258a 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7a 54 65      }..      zTe
2258b 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0d 0a  mp[i++] = ')';..
2258c 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d        zTemp[i] =
2258d 20 30 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   0;..      asser
2258e 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0d 0a 20  t( i<nTemp );.. 
2258f 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
22590 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 50 34 5f   }..    case P4_
22591 43 4f 4c 4c 53 45 51 3a 20 7b 0d 0a 20 20 20 20  COLLSEQ: {..    
22592 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
22593 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c   = pOp->p4.pColl
22594 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
22595 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
22596 20 7a 54 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71   zTemp, "collseq
22597 28 25 2e 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d  (%.20s)", pColl-
22598 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20  >zName);..      
22599 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
2259a 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44     case P4_FUNCD
2259b 45 46 3a 20 7b 0d 0a 20 20 20 20 20 20 46 75 6e  EF: {..      Fun
2259c 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 4f 70  cDef *pDef = pOp
2259d 2d 3e 70 34 2e 70 46 75 6e 63 3b 0d 0a 20 20 20  ->p4.pFunc;..   
2259e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
2259f 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
225a0 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 66  , "%s(%d)", pDef
225a1 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e  ->zName, pDef->n
225a2 41 72 67 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  Arg);..      bre
225a3 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
225a4 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b  case P4_INT64: {
225a5 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
225a6 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
225a7 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a  zTemp, "%lld", *
225a8 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0d 0a  pOp->p4.pI64);..
225a9 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
225aa 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 50 34    }..    case P4
225ab 5f 49 4e 54 33 32 3a 20 7b 0d 0a 20 20 20 20 20  _INT32: {..     
225ac 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
225ad 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
225ae 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29  "%d", pOp->p4.i)
225af 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
225b0 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65  .    }..    case
225b1 20 50 34 5f 52 45 41 4c 3a 20 7b 0d 0a 20 20 20   P4_REAL: {..   
225b2 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
225b3 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
225b4 2c 20 22 25 2e 31 36 67 22 2c 20 2a 70 4f 70 2d  , "%.16g", *pOp-
225b5 3e 70 34 2e 70 52 65 61 6c 29 3b 0d 0a 20 20 20  >p4.pReal);..   
225b6 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
225b7 0d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45  ..    case P4_ME
225b8 4d 3a 20 7b 0d 0a 20 20 20 20 20 20 4d 65 6d 20  M: {..      Mem 
225b9 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e  *pMem = pOp->p4.
225ba 70 4d 65 6d 3b 0d 0a 20 20 20 20 20 20 69 66 28  pMem;..      if(
225bb 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
225bc 45 4d 5f 53 74 72 20 29 7b 0d 0a 20 20 20 20 20  EM_Str ){..     
225bd 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a     zP4 = pMem->z
225be 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  ;..      }else i
225bf 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
225c0 20 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20 20   MEM_Int ){..   
225c1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
225c2 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
225c3 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d  mp, "%lld", pMem
225c4 2d 3e 75 2e 69 29 3b 0d 0a 20 20 20 20 20 20 7d  ->u.i);..      }
225c5 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66  else if( pMem->f
225c6 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
225c7 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
225c8 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
225c9 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36  mp, zTemp, "%.16
225ca 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0d 0a 20  g", pMem->r);.. 
225cb 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
225cc 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
225cd 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20  _Null ){..      
225ce 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
225cf 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
225d0 20 22 4e 55 4c 4c 22 29 3b 0d 0a 20 20 20 20 20   "NULL");..     
225d1 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
225d2 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
225d3 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20  lags & MEM_Blob 
225d4 29 3b 0d 0a 20 20 20 20 20 20 20 20 7a 50 34 20  );..        zP4 
225d5 3d 20 22 28 62 6c 6f 62 29 22 3b 0d 0a 20 20 20  = "(blob)";..   
225d6 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61     }..      brea
225d7 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64  k;..    }..#ifnd
225d8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
225d9 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 20  IRTUALTABLE..   
225da 20 63 61 73 65 20 50 34 5f 56 54 41 42 3a 20 7b   case P4_VTAB: {
225db 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
225dc 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f  vtab *pVtab = pO
225dd 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74  p->p4.pVtab->pVt
225de 61 62 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  ab;..      sqlit
225df 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
225e0 70 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a  p, zTemp, "vtab:
225e1 25 70 3a 25 70 22 2c 20 70 56 74 61 62 2c 20 70  %p:%p", pVtab, p
225e2 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0d  Vtab->pModule);.
225e3 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
225e4 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20     }..#endif..  
225e5 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52    case P4_INTARR
225e6 41 59 3a 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c  AY: {..      sql
225e7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
225e8 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74  emp, zTemp, "int
225e9 61 72 72 61 79 22 29 3b 0d 0a 20 20 20 20 20 20  array");..      
225ea 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
225eb 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52     case P4_SUBPR
225ec 4f 47 52 41 4d 3a 20 7b 0d 0a 20 20 20 20 20 20  OGRAM: {..      
225ed 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
225ee 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
225ef 70 72 6f 67 72 61 6d 22 29 3b 0d 0a 20 20 20 20  program");..    
225f0 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
225f1 0a 20 20 20 20 63 61 73 65 20 50 34 5f 41 44 56  .    case P4_ADV
225f2 41 4e 43 45 3a 20 7b 0d 0a 20 20 20 20 20 20 7a  ANCE: {..      z
225f3 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20  Temp[0] = 0;..  
225f4 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
225f5 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  }..    default: 
225f6 7b 0d 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70  {..      zP4 = p
225f7 4f 70 2d 3e 70 34 2e 7a 3b 0d 0a 20 20 20 20 20  Op->p4.z;..     
225f8 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0d 0a   if( zP4==0 ){..
225f9 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a 54          zP4 = zT
225fa 65 6d 70 3b 0d 0a 20 20 20 20 20 20 20 20 7a 54  emp;..        zT
225fb 65 6d 70 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 20  emp[0] = 0;..   
225fc 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
225fd 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34 21  ..  assert( zP4!
225fe 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  =0 );..  return 
225ff 7a 50 34 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  zP4;..}..#endif.
22600 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 63 6c 61 72  .../*..** Declar
22601 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68  e to the Vdbe th
22602 61 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 6a  at the BTree obj
22603 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b 69  ect at db->aDb[i
22604 5d 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a  ] is used...**..
22605 2a 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 20  ** The prepared 
22606 73 74 61 74 65 6d 65 6e 74 73 20 6e 65 65 64 20  statements need 
22607 74 6f 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e  to know in advan
22608 63 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  ce the complete 
22609 73 65 74 20 6f 66 0d 0a 2a 2a 20 61 74 74 61 63  set of..** attac
2260a 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 68  hed databases th
2260b 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 2e 20  at will be use. 
2260c 20 41 20 6d 61 73 6b 20 6f 66 20 74 68 65 73 65   A mask of these
2260d 20 64 61 74 61 62 61 73 65 73 0d 0a 2a 2a 20 69   databases..** i
2260e 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 69 6e 20  s maintained in 
2260f 70 2d 3e 62 74 72 65 65 4d 61 73 6b 2e 20 20 54  p->btreeMask.  T
22610 68 65 20 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 20 76  he p->lockMask v
22611 61 6c 75 65 20 69 73 20 74 68 65 20 73 75 62 73  alue is the subs
22612 65 74 20 6f 66 0d 0a 2a 2a 20 70 2d 3e 62 74 72  et of..** p->btr
22613 65 65 4d 61 73 6b 20 6f 66 20 64 61 74 61 62 61  eeMask of databa
22614 73 65 73 20 74 68 61 74 20 77 69 6c 6c 20 72 65  ses that will re
22615 71 75 69 72 65 20 61 20 6c 6f 63 6b 2e 0d 0a 2a  quire a lock...*
22616 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
22617 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
22618 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65  beUsesBtree(Vdbe
22619 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0d 0a 20 20   *p, int i){..  
2261a 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
2261b 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20  i<p->db->nDb && 
2261c 69 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 79 44  i<(int)sizeof(yD
2261d 62 4d 61 73 6b 29 2a 38 20 29 3b 0d 0a 20 20 61  bMask)*8 );..  a
2261e 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 73 69  ssert( i<(int)si
2261f 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 73  zeof(p->btreeMas
22620 6b 29 2a 38 20 29 3b 0d 0a 20 20 70 2d 3e 62 74  k)*8 );..  p->bt
22621 72 65 65 4d 61 73 6b 20 7c 3d 20 28 28 79 44 62  reeMask |= ((yDb
22622 4d 61 73 6b 29 31 29 3c 3c 69 3b 0d 0a 20 20 69  Mask)1)<<i;..  i
22623 66 28 20 69 21 3d 31 20 26 26 20 73 71 6c 69 74  f( i!=1 && sqlit
22624 65 33 42 74 72 65 65 53 68 61 72 61 62 6c 65 28  e3BtreeSharable(
22625 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  p->db->aDb[i].pB
22626 74 29 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6c 6f  t) ){..    p->lo
22627 63 6b 4d 61 73 6b 20 7c 3d 20 28 28 79 44 62 4d  ckMask |= ((yDbM
22628 61 73 6b 29 31 29 3c 3c 69 3b 0d 0a 20 20 7d 0d  ask)1)<<i;..  }.
22629 0a 7d 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e  .}....#if !defin
2262a 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2262b 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
2262c 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2262d 45 3e 30 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 53  E>0../*..** If S
2262e 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
2262f 64 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61  d to support sha
22630 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61  red-cache mode a
22631 6e 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73  nd to be threads
22632 61 66 65 2c 0d 0a 2a 2a 20 74 68 69 73 20 72 6f  afe,..** this ro
22633 75 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68  utine obtains th
22634 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74  e mutex associat
22635 65 64 20 77 69 74 68 20 65 61 63 68 20 42 74 53  ed with each BtS
22636 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 0d  hared structure.
22637 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20  .** that may be 
22638 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20  accessed by the 
22639 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  VM passed as an 
2263a 61 72 67 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69  argument. In doi
2263b 6e 67 20 73 6f 20 69 74 20 61 6c 73 6f 0d 0a 2a  ng so it also..*
2263c 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68 61  * sets the BtSha
2263d 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66  red.db member of
2263e 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 53   each of the BtS
2263f 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73  hared structures
22640 2c 20 65 6e 73 75 72 69 6e 67 0d 0a 2a 2a 20 74  , ensuring..** t
22641 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  hat the correct 
22642 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c  busy-handler cal
22643 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
22644 20 69 66 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a   if required...*
22645 2a 0d 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20  *..** If SQLite 
22646 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
22647 65 20 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f  e but does suppo
22648 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  rt shared-cache 
22649 6d 6f 64 65 2c 20 74 68 65 6e 0d 0a 2a 2a 20 73  mode, then..** s
2264a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
2264b 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  () is invoked to
2264c 20 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65   set the BtShare
2264d 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0d 0a  d.db variables..
2264e 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53  ** of all of BtS
2264f 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73  hared structures
22650 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20   accessible via 
22651 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
22652 64 6c 65 20 0d 0a 2a 2a 20 61 73 73 6f 63 69 61  dle ..** associa
22653 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e  ted with the VM.
22654 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 53 51 4c 69  ..**..** If SQLi
22655 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  te is not thread
22656 73 61 66 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f  safe and does no
22657 74 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 64  t support shared
22658 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 69  -cache mode, thi
22659 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  s..** function i
2265a 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a  s a no-op...**..
2265b 2a 2a 20 54 68 65 20 70 2d 3e 62 74 72 65 65 4d  ** The p->btreeM
2265c 61 73 6b 20 66 69 65 6c 64 20 69 73 20 61 20 62  ask field is a b
2265d 69 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 62 74  itmask of all bt
2265e 72 65 65 73 20 74 68 61 74 20 74 68 65 20 70 72  rees that the pr
2265f 65 70 61 72 65 64 20 0d 0a 2a 2a 20 73 74 61 74  epared ..** stat
22660 65 6d 65 6e 74 20 70 20 77 69 6c 6c 20 65 76 65  ement p will eve
22661 72 20 75 73 65 2e 20 20 4c 65 74 20 4e 20 62 65  r use.  Let N be
22662 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
22663 69 74 73 20 69 6e 20 70 2d 3e 62 74 72 65 65 4d  its in p->btreeM
22664 61 73 6b 0d 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  ask..** correspo
22665 6e 64 69 6e 67 20 74 6f 20 62 74 72 65 65 73 20  nding to btrees 
22666 74 68 61 74 20 75 73 65 20 73 68 61 72 65 64 20  that use shared 
22667 63 61 63 68 65 2e 20 20 54 68 65 6e 20 74 68 65  cache.  Then the
22668 20 72 75 6e 74 69 6d 65 20 6f 66 0d 0a 2a 2a 20   runtime of..** 
22669 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2266a 4e 2a 4e 2e 20 20 42 75 74 20 61 73 20 4e 20 69  N*N.  But as N i
2266b 73 20 72 61 72 65 6c 79 20 6d 6f 72 65 20 74 68  s rarely more th
2266c 61 6e 20 31 2c 20 74 68 69 73 20 73 68 6f 75 6c  an 1, this shoul
2266d 64 20 6e 6f 74 0d 0a 2a 2a 20 62 65 20 61 20 70  d not..** be a p
2266e 72 6f 62 6c 65 6d 2e 0d 0a 2a 2f 0d 0a 53 51 4c  roblem...*/..SQL
2266f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22670 20 73 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65   sqlite3VdbeEnte
22671 72 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20 69  r(Vdbe *p){..  i
22672 6e 74 20 69 3b 0d 0a 20 20 79 44 62 4d 61 73 6b  nt i;..  yDbMask
22673 20 6d 61 73 6b 3b 0d 0a 20 20 73 71 6c 69 74 65   mask;..  sqlite
22674 33 20 2a 64 62 3b 0d 0a 20 20 44 62 20 2a 61 44  3 *db;..  Db *aD
22675 62 3b 0d 0a 20 20 69 6e 74 20 6e 44 62 3b 0d 0a  b;..  int nDb;..
22676 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 4d 61 73    if( p->lockMas
22677 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 20  k==0 ) return;  
22678 2f 2a 20 54 68 65 20 63 6f 6d 6d 6f 6e 20 63 61  /* The common ca
22679 73 65 20 2a 2f 0d 0a 20 20 64 62 20 3d 20 70 2d  se */..  db = p-
2267a 3e 64 62 3b 0d 0a 20 20 61 44 62 20 3d 20 64 62  >db;..  aDb = db
2267b 2d 3e 61 44 62 3b 0d 0a 20 20 6e 44 62 20 3d 20  ->aDb;..  nDb = 
2267c 64 62 2d 3e 6e 44 62 3b 0d 0a 20 20 66 6f 72 28  db->nDb;..  for(
2267d 69 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 3c 6e  i=0, mask=1; i<n
2267e 44 62 3b 20 69 2b 2b 2c 20 6d 61 73 6b 20 2b 3d  Db; i++, mask +=
2267f 20 6d 61 73 6b 29 7b 0d 0a 20 20 20 20 69 66 28   mask){..    if(
22680 20 69 21 3d 31 20 26 26 20 28 6d 61 73 6b 20 26   i!=1 && (mask &
22681 20 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 29 21 3d 30   p->lockMask)!=0
22682 20 26 26 20 41 4c 57 41 59 53 28 61 44 62 5b 69   && ALWAYS(aDb[i
22683 5d 2e 70 42 74 21 3d 30 29 20 29 7b 0d 0a 20 20  ].pBt!=0) ){..  
22684 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
22685 45 6e 74 65 72 28 61 44 62 5b 69 5d 2e 70 42 74  Enter(aDb[i].pBt
22686 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
22687 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66  }..#endif....#if
22688 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
22689 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
2268a 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48  HE) && SQLITE_TH
2268b 52 45 41 44 53 41 46 45 3e 30 0d 0a 2f 2a 0d 0a  READSAFE>0../*..
2268c 2a 2a 20 55 6e 6c 6f 63 6b 20 61 6c 6c 20 6f 66  ** Unlock all of
2268d 20 74 68 65 20 62 74 72 65 65 73 20 70 72 65 76   the btrees prev
2268e 69 6f 75 73 6c 79 20 6c 6f 63 6b 65 64 20 62 79  iously locked by
2268f 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
22690 65 33 56 64 62 65 45 6e 74 65 72 28 29 2e 0d 0a  e3VdbeEnter()...
22691 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
22692 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
22693 64 62 65 4c 65 61 76 65 28 56 64 62 65 20 2a 70  dbeLeave(Vdbe *p
22694 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
22695 79 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0d 0a 20  yDbMask mask;.. 
22696 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0d 0a 20   sqlite3 *db;.. 
22697 20 44 62 20 2a 61 44 62 3b 0d 0a 20 20 69 6e 74   Db *aDb;..  int
22698 20 6e 44 62 3b 0d 0a 20 20 69 66 28 20 70 2d 3e   nDb;..  if( p->
22699 6c 6f 63 6b 4d 61 73 6b 3d 3d 30 20 29 20 72 65  lockMask==0 ) re
2269a 74 75 72 6e 3b 20 20 2f 2a 20 54 68 65 20 63 6f  turn;  /* The co
2269b 6d 6d 6f 6e 20 63 61 73 65 20 2a 2f 0d 0a 20 20  mmon case */..  
2269c 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20 61  db = p->db;..  a
2269d 44 62 20 3d 20 64 62 2d 3e 61 44 62 3b 0d 0a 20  Db = db->aDb;.. 
2269e 20 6e 44 62 20 3d 20 64 62 2d 3e 6e 44 62 3b 0d   nDb = db->nDb;.
2269f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6d 61 73 6b  .  for(i=0, mask
226a0 3d 31 3b 20 69 3c 6e 44 62 3b 20 69 2b 2b 2c 20  =1; i<nDb; i++, 
226a1 6d 61 73 6b 20 2b 3d 20 6d 61 73 6b 29 7b 0d 0a  mask += mask){..
226a2 20 20 20 20 69 66 28 20 69 21 3d 31 20 26 26 20      if( i!=1 && 
226a3 28 6d 61 73 6b 20 26 20 70 2d 3e 6c 6f 63 6b 4d  (mask & p->lockM
226a4 61 73 6b 29 21 3d 30 20 26 26 20 41 4c 57 41 59  ask)!=0 && ALWAY
226a5 53 28 61 44 62 5b 69 5d 2e 70 42 74 21 3d 30 29  S(aDb[i].pBt!=0)
226a6 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
226a7 65 33 42 74 72 65 65 4c 65 61 76 65 28 61 44 62  e3BtreeLeave(aDb
226a8 5b 69 5d 2e 70 42 74 29 3b 0d 0a 20 20 20 20 7d  [i].pBt);..    }
226a9 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66  ..  }..}..#endif
226aa 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
226ab 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c  VDBE_PROFILE) ||
226ac 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
226ad 44 45 42 55 47 29 0d 0a 2f 2a 0d 0a 2a 2a 20 50  DEBUG)../*..** P
226ae 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70  rint a single op
226af 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74  code.  This rout
226b0 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ine is used for 
226b1 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0d  debugging only..
226b2 0a 2a 2f 0d 0a 0d 0a 76 6f 69 64 20 64 6f 50 72  .*/....void doPr
226b3 6f 66 69 6c 65 4d 73 67 28 63 6f 6e 73 74 20 63  ofileMsg(const c
226b4 68 61 72 2a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45  har*);....SQLITE
226b5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
226b6 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
226b7 28 46 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74  (FILE *pOut, int
226b8 20 70 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0d 0a   pc, Op *pOp){..
226b9 20 20 63 68 61 72 20 2a 7a 50 34 3b 0d 0a 20 20    char *zP4;..  
226ba 63 68 61 72 20 7a 50 74 72 5b 35 30 5d 3b 0d 0a  char zPtr[50];..
226bb 20 20 63 68 61 72 20 62 75 66 5b 30 78 31 30 30    char buf[0x100
226bc 30 5d 3b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  0];..  static co
226bd 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
226be 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20  t1 = "%4d %-13s 
226bf 25 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 73  %4d %4d %4d %-4s
226c0 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0d 0a 20 20   %.2X %s\n";..  
226c1 2f 2f 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20  //if( pOut==0 ) 
226c2 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0d 0a  pOut = stdout;..
226c3 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50    zP4 = displayP
226c4 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a  4(pOp, zPtr, siz
226c5 65 6f 66 28 7a 50 74 72 29 29 3b 0d 0a 20 20 73  eof(zPtr));..  s
226c6 70 72 69 6e 74 66 28 62 75 66 2c 20 7a 46 6f 72  printf(buf, zFor
226c7 6d 61 74 31 2c 20 70 63 2c 20 0d 0a 20 20 20 20  mat1, pc, ..    
226c8 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e    sqlite3OpcodeN
226c9 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29  ame(pOp->opcode)
226ca 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
226cb 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34  p2, pOp->p3, zP4
226cc 2c 20 70 4f 70 2d 3e 70 35 2c 0d 0a 2f 2f 23 69  , pOp->p5,..//#i
226cd 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
226ce 47 0d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43  G..      pOp->zC
226cf 6f 6d 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43  omment ? pOp->zC
226d0 6f 6d 6d 65 6e 74 20 3a 20 22 22 0d 0a 2f 2f 23  omment : ""..//#
226d1 65 6c 73 65 0d 0a 2f 2f 20 20 20 20 20 20 22 22  else..//      ""
226d2 0d 0a 2f 2f 23 65 6e 64 69 66 0d 0a 20 20 29 3b  ..//#endif..  );
226d3 0d 0a 20 20 64 6f 50 72 6f 66 69 6c 65 4d 73 67  ..  doProfileMsg
226d4 28 62 75 66 29 3b 0d 0a 20 20 2f 2f 66 66 6c 75  (buf);..  //fflu
226d5 73 68 28 70 4f 75 74 29 3b 0d 0a 7d 0d 0a 23 65  sh(pOut);..}..#e
226d6 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ndif..../*..** R
226d7 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 20  elease an array 
226d8 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74  of N Mem element
226d9 73 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  s..*/..static vo
226da 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  id releaseMemArr
226db 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e  ay(Mem *p, int N
226dc 29 7b 0d 0a 20 20 69 66 28 20 70 20 26 26 20 4e  ){..  if( p && N
226dd 20 29 7b 0d 0a 20 20 20 20 4d 65 6d 20 2a 70 45   ){..    Mem *pE
226de 6e 64 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  nd;..    sqlite3
226df 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20   *db = p->db;.. 
226e0 20 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69     u8 malloc_fai
226e1 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63  led = db->malloc
226e2 46 61 69 6c 65 64 3b 0d 0a 20 20 20 20 69 66 28  Failed;..    if(
226e3 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65   db->pnBytesFree
226e4 64 20 29 7b 0d 0a 20 20 20 20 20 20 66 6f 72 28  d ){..      for(
226e5 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45  pEnd=&p[N]; p<pE
226e6 6e 64 3b 20 70 2b 2b 29 7b 0d 0a 20 20 20 20 20  nd; p++){..     
226e7 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
226e8 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29  (db, p->zMalloc)
226e9 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
226ea 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d    return;..    }
226eb 0d 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26  ..    for(pEnd=&
226ec 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b  p[N]; p<pEnd; p+
226ed 2b 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  +){..      asser
226ee 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64  t( (&p[1])==pEnd
226ef 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31   || p[0].db==p[1
226f0 5d 2e 64 62 20 29 3b 0d 0a 0d 0a 20 20 20 20 20  ].db );....     
226f1 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69   /* This block i
226f2 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69  s really an inli
226f3 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73  ned version of s
226f4 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
226f5 65 61 73 65 28 29 0d 0a 20 20 20 20 20 20 2a 2a  ease()..      **
226f6 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61   that takes adva
226f7 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63  ntage of the fac
226f8 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  t that the memor
226f9 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20  y cell value is 
226fa 0d 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67  ..      ** being
226fb 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74   set to NULL aft
226fc 65 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79  er releasing any
226fd 20 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63   dynamic resourc
226fe 65 73 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20  es...      **.. 
226ff 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 74       ** The just
22700 69 66 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 75  ification for du
22701 70 6c 69 63 61 74 69 6e 67 20 63 6f 64 65 20 69  plicating code i
22702 73 20 74 68 61 74 20 61 63 63 6f 72 64 69 6e 67  s that according
22703 20 74 6f 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63   to ..      ** c
22704 61 6c 6c 67 72 69 6e 64 2c 20 74 68 69 73 20 63  allgrind, this c
22705 61 75 73 65 73 20 61 20 63 65 72 74 61 69 6e 20  auses a certain 
22706 74 65 73 74 20 63 61 73 65 20 74 6f 20 68 69 74  test case to hit
22707 20 74 68 65 20 43 50 55 20 34 2e 37 20 0d 0a 20   the CPU 4.7 .. 
22708 20 20 20 20 20 2a 2a 20 70 65 72 63 65 6e 74 20       ** percent 
22709 6c 65 73 73 20 28 78 38 36 20 6c 69 6e 75 78 2c  less (x86 linux,
2270a 20 67 63 63 20 76 65 72 73 69 6f 6e 20 34 2e 31   gcc version 4.1
2270b 2e 32 2c 20 2d 4f 36 29 20 74 68 61 6e 20 69 66  .2, -O6) than if
2270c 20 0d 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69   ..      ** sqli
2270d 74 65 33 4d 65 6d 52 65 6c 65 61 73 65 28 29 20  te3MemRelease() 
2270e 77 65 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  were called from
2270f 20 68 65 72 65 2e 20 57 69 74 68 20 2d 4f 32 2c   here. With -O2,
22710 20 74 68 69 73 20 6a 75 6d 70 73 0d 0a 20 20 20   this jumps..   
22711 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20 70 65 72     ** to 6.6 per
22712 63 65 6e 74 2e 20 54 68 65 20 74 65 73 74 20 63  cent. The test c
22713 61 73 65 20 69 73 20 69 6e 73 65 72 74 69 6e 67  ase is inserting
22714 20 31 30 30 30 20 72 6f 77 73 20 69 6e 74 6f 20   1000 rows into 
22715 61 20 74 61 62 6c 65 20 0d 0a 20 20 20 20 20 20  a table ..      
22716 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64 65 78  ** with no index
22717 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c  es using a singl
22718 65 20 70 72 65 70 61 72 65 64 20 49 4e 53 45 52  e prepared INSER
22719 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 69 6e  T statement, bin
2271a 64 28 29 20 0d 0a 20 20 20 20 20 20 2a 2a 20 61  d() ..      ** a
2271b 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73 65  nd reset(). Inse
2271c 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64 20  rts are grouped 
2271d 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74 69  into a transacti
2271e 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  on...      */.. 
2271f 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67       if( p->flag
22720 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44  s&(MEM_Agg|MEM_D
22721 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d 45 4d  yn|MEM_Frame|MEM
22722 5f 52 6f 77 53 65 74 29 20 29 7b 0d 0a 20 20 20  _RowSet) ){..   
22723 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22724 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0d 0a  MemRelease(p);..
22725 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
22726 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20  p->zMalloc ){.. 
22727 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
22728 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c  Free(db, p->zMal
22729 6c 6f 63 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  loc);..        p
2272a 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a  ->zMalloc = 0;..
2272b 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
2272c 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f   p->flags = MEM_
2272d 49 6e 76 61 6c 69 64 3b 0d 0a 20 20 20 20 7d 0d  Invalid;..    }.
2272e 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
2272f 61 69 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66  ailed = malloc_f
22730 61 69 6c 65 64 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  ailed;..  }..}..
22731 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65 20  ../*..** Delete 
22732 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65  a VdbeFrame obje
22733 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65  ct and its conte
22734 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20 6f  nts. VdbeFrame o
22735 62 6a 65 63 74 73 20 61 72 65 0d 0a 2a 2a 20 61  bjects are..** a
22736 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
22737 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64  OP_Program opcod
22738 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65  e in sqlite3Vdbe
22739 45 78 65 63 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c  Exec()...*/..SQL
2273a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2273b 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d   sqlite3VdbeFram
2273c 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d  eDelete(VdbeFram
2273d 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  e *p){..  int i;
2273e 0d 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20  ..  Mem *aMem = 
2273f 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b  VdbeFrameMem(p);
22740 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ..  VdbeCursor *
22741 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75  *apCsr = (VdbeCu
22742 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d  rsor **)&aMem[p-
22743 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0d 0a 20 20  >nChildMem];..  
22744 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
22745 68 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0d 0a  hildCsr; i++){..
22746 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
22747 72 65 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20  reeCursor(p->v, 
22748 61 70 43 73 72 5b 69 5d 29 3b 0d 0a 20 20 7d 0d  apCsr[i]);..  }.
22749 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
2274a 61 79 28 61 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69  ay(aMem, p->nChi
2274b 6c 64 4d 65 6d 29 3b 0d 0a 20 20 73 71 6c 69 74  ldMem);..  sqlit
2274c 65 33 44 62 46 72 65 65 28 70 2d 3e 76 2d 3e 64  e3DbFree(p->v->d
2274d 62 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66  b, p);..}....#if
2274e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2274f 5f 45 58 50 4c 41 49 4e 0d 0a 2f 2a 0d 0a 2a 2a  _EXPLAIN../*..**
22750 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67 20   Give a listing 
22751 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 69  of the program i
22752 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  n the virtual ma
22753 63 68 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  chine...**..** T
22754 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  he interface is 
22755 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69  the same as sqli
22756 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20  te3VdbeExec().  
22757 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0d 0a  But instead of..
22758 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63  ** running the c
22759 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20  ode, it invokes 
2275a 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63  the callback onc
2275b 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72  e for each instr
2275c 75 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 69 73  uction...** This
2275d 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65 64   feature is used
2275e 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45   to implement "E
2275f 58 50 4c 41 49 4e 22 2e 0d 0a 2a 2a 0d 0a 2a 2a  XPLAIN"...**..**
22760 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e   When p->explain
22761 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75  ==1, each instru
22762 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e  ction is listed.
22763 20 20 57 68 65 6e 0d 0a 2a 2a 20 70 2d 3e 65 78    When..** p->ex
22764 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f  plain==2, only O
22765 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75  P_Explain instru
22766 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65  ctions are liste
22767 64 20 61 6e 64 20 74 68 65 73 65 0d 0a 2a 2a 20  d and these..** 
22768 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64  are shown in a d
22769 69 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e  ifferent format.
2276a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20    p->explain==2 
2276b 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
2276c 6d 65 6e 74 0d 0a 2a 2a 20 45 58 50 4c 41 49 4e  ment..** EXPLAIN
2276d 20 51 55 45 52 59 20 50 4c 41 4e 2e 0d 0a 2a 2a   QUERY PLAN...**
2276e 0d 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70  ..** When p->exp
2276f 6c 61 69 6e 3d 3d 31 2c 20 66 69 72 73 74 20 74  lain==1, first t
22770 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  he main program 
22771 69 73 20 6c 69 73 74 65 64 2c 20 74 68 65 6e 20  is listed, then 
22772 65 61 63 68 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  each of..** the 
22773 74 72 69 67 67 65 72 20 73 75 62 70 72 6f 67 72  trigger subprogr
22774 61 6d 73 20 61 72 65 20 6c 69 73 74 65 64 20 6f  ams are listed o
22775 6e 65 20 62 79 20 6f 6e 65 2e 0d 0a 2a 2f 0d 0a  ne by one...*/..
22776 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
22777 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69  nt sqlite3VdbeLi
22778 73 74 28 0d 0a 20 20 56 64 62 65 20 2a 70 20 20  st(..  Vdbe *p  
22779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2277a 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0d   /* The VDBE */.
2277b 0a 29 7b 0d 0a 20 20 69 6e 74 20 6e 52 6f 77 3b  .){..  int nRow;
2277c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2277d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2277e 74 6f 70 20 77 68 65 6e 20 72 6f 77 20 63 6f 75  top when row cou
2277f 6e 74 20 72 65 61 63 68 65 73 20 74 68 69 73 20  nt reaches this 
22780 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 75 62 20 3d  */..  int nSub =
22781 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
22782 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
22783 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64 62 65  mber of sub-vdbe
22784 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f  s seen so far */
22785 0d 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a  ..  SubProgram *
22786 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20 20  *apSub = 0;     
22787 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
22788 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20 2a  y of sub-vdbes *
22789 2f 0d 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d  /..  Mem *pSub =
2278a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2278b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
2278c 6f 72 79 20 63 65 6c 6c 20 68 6f 6c 64 20 61 72  ory cell hold ar
2278d 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 73 20  ray of subprogs 
2278e 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */..  sqlite3 *d
2278f 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20  b = p->db;      
22790 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22791 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
22792 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ction */..  int 
22793 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
22794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22795 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
22796 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20   */..  int rc = 
22797 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
22798 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
22799 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20  eturn code */.. 
2279a 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d   Mem *pMem = &p-
2279b 3e 61 4d 65 6d 5b 31 5d 3b 20 20 20 20 20 20 20  >aMem[1];       
2279c 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d        /* First M
2279d 65 6d 20 6f 66 20 72 65 73 75 6c 74 20 73 65 74  em of result set
2279e 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   */....  assert(
2279f 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0d 0a   p->explain );..
227a0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
227a1 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
227a2 55 4e 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  UN );..  assert(
227a3 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
227a4 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  K || p->rc==SQLI
227a5 54 45 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63  TE_BUSY || p->rc
227a6 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
227a7 3b 0d 0a 0d 0a 20 20 2f 2a 20 45 76 65 6e 20 74  ;....  /* Even t
227a8 68 6f 75 67 68 20 74 68 69 73 20 6f 70 63 6f 64  hough this opcod
227a9 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 64  e does not use d
227aa 79 6e 61 6d 69 63 20 73 74 72 69 6e 67 73 20 66  ynamic strings f
227ab 6f 72 0d 0a 20 20 2a 2a 20 74 68 65 20 72 65 73  or..  ** the res
227ac 75 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f 6c 75  ult, result colu
227ad 6d 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65 20 64  mns may become d
227ae 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20 75 73  ynamic if the us
227af 65 72 20 63 61 6c 6c 73 0d 0a 20 20 2a 2a 20 73  er calls..  ** s
227b0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
227b1 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67 20  xt16(), causing 
227b2 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f  a translation to
227b3 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67   UTF-16 encoding
227b4 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 72 65 6c 65 61  ...  */..  relea
227b5 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d 2c  seMemArray(pMem,
227b6 20 38 29 3b 0d 0a 20 20 70 2d 3e 70 52 65 73 75   8);..  p->pResu
227b7 6c 74 53 65 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20  ltSet = 0;....  
227b8 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  if( p->rc==SQLIT
227b9 45 5f 4e 4f 4d 45 4d 20 29 7b 0d 0a 20 20 20 20  E_NOMEM ){..    
227ba 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
227bb 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
227bc 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
227bd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
227be 78 74 28 29 20 6f 72 0d 0a 20 20 20 20 2a 2a 20  xt() or..    ** 
227bf 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
227c0 65 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20  ext16() failed. 
227c1 20 2a 2f 0d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c   */..    db->mal
227c2 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a  locFailed = 1;..
227c3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
227c4 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 0d  E_ERROR;..  }...
227c5 0a 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 6e  .  /* When the n
227c6 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20  umber of output 
227c7 72 6f 77 73 20 72 65 61 63 68 65 73 20 6e 52 6f  rows reaches nRo
227c8 77 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  w, that means th
227c9 65 0d 0a 20 20 2a 2a 20 6c 69 73 74 69 6e 67 20  e..  ** listing 
227ca 68 61 73 20 66 69 6e 69 73 68 65 64 20 61 6e 64  has finished and
227cb 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
227cc 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 53 51  should return SQ
227cd 4c 49 54 45 5f 44 4f 4e 45 2e 0d 0a 20 20 2a 2a  LITE_DONE...  **
227ce 20 6e 52 6f 77 20 69 73 20 74 68 65 20 73 75 6d   nRow is the sum
227cf 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
227d0 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 6d 61  f rows in the ma
227d1 69 6e 20 70 72 6f 67 72 61 6d 2c 20 70 6c 75 73  in program, plus
227d2 0d 0a 20 20 2a 2a 20 74 68 65 20 73 75 6d 20 6f  ..  ** the sum o
227d3 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
227d4 72 6f 77 73 20 69 6e 20 61 6c 6c 20 74 72 69 67  rows in all trig
227d5 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d 73 20  ger subprograms 
227d6 65 6e 63 6f 75 6e 74 65 72 65 64 0d 0a 20 20 2a  encountered..  *
227d7 2a 20 73 6f 20 66 61 72 2e 20 20 54 68 65 20 6e  * so far.  The n
227d8 52 6f 77 20 76 61 6c 75 65 20 77 69 6c 6c 20 69  Row value will i
227d9 6e 63 72 65 61 73 65 20 61 73 20 6e 65 77 20 74  ncrease as new t
227da 72 69 67 67 65 72 20 73 75 62 70 72 6f 67 72 61  rigger subprogra
227db 6d 73 20 61 72 65 0d 0a 20 20 2a 2a 20 65 6e 63  ms are..  ** enc
227dc 6f 75 6e 74 65 72 65 64 2c 20 62 75 74 20 70 2d  ountered, but p-
227dd 3e 70 63 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  >pc will eventua
227de 6c 6c 79 20 63 61 74 63 68 20 75 70 20 74 6f 20  lly catch up to 
227df 6e 52 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6e  nRow...  */..  n
227e0 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 3b 0d 0a 20  Row = p->nOp;.. 
227e1 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
227e2 3d 31 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  =1 ){..    /* Th
227e3 65 20 66 69 72 73 74 20 38 20 6d 65 6d 6f 72 79  e first 8 memory
227e4 20 63 65 6c 6c 73 20 61 72 65 20 75 73 65 64 20   cells are used 
227e5 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73  for the result s
227e6 65 74 2e 20 20 53 6f 20 77 65 20 77 69 6c 6c 0d  et.  So we will.
227e7 0a 20 20 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 65  .    ** commande
227e8 65 72 20 74 68 65 20 39 74 68 20 63 65 6c 6c 20  er the 9th cell 
227e9 74 6f 20 75 73 65 20 61 73 20 73 74 6f 72 61 67  to use as storag
227ea 65 20 66 6f 72 20 61 6e 20 61 72 72 61 79 20 6f  e for an array o
227eb 66 20 70 6f 69 6e 74 65 72 73 0d 0a 20 20 20 20  f pointers..    
227ec 2a 2a 20 74 6f 20 74 72 69 67 67 65 72 20 73 75  ** to trigger su
227ed 62 70 72 6f 67 72 61 6d 73 2e 20 20 54 68 65 20  bprograms.  The 
227ee 56 44 42 45 20 69 73 20 67 75 61 72 61 6e 74 65  VDBE is guarante
227ef 65 64 20 74 6f 20 68 61 76 65 20 61 74 20 6c 65  ed to have at le
227f0 61 73 74 20 39 0d 0a 20 20 20 20 2a 2a 20 63 65  ast 9..    ** ce
227f1 6c 6c 73 2e 20 20 2a 2f 0d 0a 20 20 20 20 61 73  lls.  */..    as
227f2 73 65 72 74 28 20 70 2d 3e 6e 4d 65 6d 3e 39 20  sert( p->nMem>9 
227f3 29 3b 0d 0a 20 20 20 20 70 53 75 62 20 3d 20 26  );..    pSub = &
227f4 70 2d 3e 61 4d 65 6d 5b 39 5d 3b 0d 0a 20 20 20  p->aMem[9];..   
227f5 20 69 66 28 20 70 53 75 62 2d 3e 66 6c 61 67 73   if( pSub->flags
227f6 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0d 0a 20 20  &MEM_Blob ){..  
227f7 20 20 20 20 2f 2a 20 4f 6e 20 74 68 65 20 66 69      /* On the fi
227f8 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  rst call to sqli
227f9 74 65 33 5f 73 74 65 70 28 29 2c 20 70 53 75 62  te3_step(), pSub
227fa 20 77 69 6c 6c 20 68 6f 6c 64 20 61 20 4e 55 4c   will hold a NUL
227fb 4c 2e 20 20 49 74 20 69 73 0d 0a 20 20 20 20 20  L.  It is..     
227fc 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   ** initialized 
227fd 74 6f 20 61 20 42 4c 4f 42 20 62 79 20 74 68 65  to a BLOB by the
227fe 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 70   P4_SUBPROGRAM p
227ff 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 20  rocessing logic 
22800 62 65 6c 6f 77 20 2a 2f 0d 0a 20 20 20 20 20 20  below */..      
22801 6e 53 75 62 20 3d 20 70 53 75 62 2d 3e 6e 2f 73  nSub = pSub->n/s
22802 69 7a 65 6f 66 28 56 64 62 65 2a 29 3b 0d 0a 20  izeof(Vdbe*);.. 
22803 20 20 20 20 20 61 70 53 75 62 20 3d 20 28 53 75       apSub = (Su
22804 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70 53 75 62  bProgram **)pSub
22805 2d 3e 7a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ->z;..    }..   
22806 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62   for(i=0; i<nSub
22807 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 6e  ; i++){..      n
22808 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b 69 5d 2d  Row += apSub[i]-
22809 3e 6e 4f 70 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  >nOp;..    }..  
2280a 7d 0d 0a 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20 20  }....  do{..    
2280b 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0d 0a 20 20  i = p->pc++;..  
2280c 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26  }while( i<nRow &
2280d 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20  & p->explain==2 
2280e 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63  && p->aOp[i].opc
2280f 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20  ode!=OP_Explain 
22810 29 3b 0d 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f  );..  if( i>=nRo
22811 77 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 72 63 20  w ){..    p->rc 
22812 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
22813 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f    rc = SQLITE_DO
22814 4e 45 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  NE;..  }else if(
22815 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
22816 75 70 74 65 64 20 29 7b 0d 0a 20 20 20 20 70 2d  upted ){..    p-
22817 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  >rc = SQLITE_INT
22818 45 52 52 55 50 54 3b 0d 0a 20 20 20 20 72 63 20  ERRUPT;..    rc 
22819 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  = SQLITE_ERROR;.
2281a 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
2281b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
2281c 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
2281d 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63  ite3ErrStr(p->rc
2281e 29 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  ));..  }else{.. 
2281f 20 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 20     char *z;..   
22820 20 4f 70 20 2a 70 4f 70 3b 0d 0a 20 20 20 20 69   Op *pOp;..    i
22821 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b 0d 0a  f( i<p->nOp ){..
22822 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74        /* The out
22823 70 75 74 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20  put line number 
22824 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20  is small enough 
22825 74 68 61 74 20 77 65 20 61 72 65 20 73 74 69 6c  that we are stil
22826 6c 20 69 6e 20 74 68 65 0d 0a 20 20 20 20 20 20  l in the..      
22827 2a 2a 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e  ** main program.
22828 20 2a 2f 0d 0a 20 20 20 20 20 20 70 4f 70 20 3d   */..      pOp =
22829 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0d 0a 20 20   &p->aOp[i];..  
2282a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
2282b 2f 2a 20 57 65 20 61 72 65 20 63 75 72 72 65 6e  /* We are curren
2282c 74 6c 79 20 6c 69 73 74 69 6e 67 20 73 75 62 70  tly listing subp
2282d 72 6f 67 72 61 6d 73 2e 20 20 46 69 67 75 72 65  rograms.  Figure
2282e 20 6f 75 74 20 77 68 69 63 68 20 6f 6e 65 20 61   out which one a
2282f 6e 64 0d 0a 20 20 20 20 20 20 2a 2a 20 70 69 63  nd..      ** pic
22830 6b 20 75 70 20 74 68 65 20 61 70 70 72 6f 70 72  k up the appropr
22831 69 61 74 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0d  iate opcode. */.
22832 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20  .      int j;.. 
22833 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e 4f 70       i -= p->nOp
22834 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  ;..      for(j=0
22835 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e 6e  ; i>=apSub[j]->n
22836 4f 70 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20  Op; j++){..     
22837 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d     i -= apSub[j]
22838 2d 3e 6e 4f 70 3b 0d 0a 20 20 20 20 20 20 7d 0d  ->nOp;..      }.
22839 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70  .      pOp = &ap
2283a 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0d  Sub[j]->aOp[i];.
2283b 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
2283c 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b  p->explain==1 ){
2283d 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ..      pMem->fl
2283e 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0d 0a  ags = MEM_Int;..
2283f 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
22840 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
22841 52 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  R;..      pMem->
22842 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20  u.i = i;        
22843 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22844 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
22845 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  am counter */.. 
22846 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0d 0a 20 20       pMem++;..  
22847 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ..      pMem->fl
22848 61 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63  ags = MEM_Static
22849 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72  |MEM_Str|MEM_Ter
2284a 6d 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  m;..      pMem->
2284b 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  z = (char*)sqlit
2284c 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70  e3OpcodeName(pOp
2284d 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20 4f  ->opcode);  /* O
2284e 70 63 6f 64 65 20 2a 2f 0d 0a 20 20 20 20 20 20  pcode */..      
2284f 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21  assert( pMem->z!
22850 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70 4d 65  =0 );..      pMe
22851 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  m->n = sqlite3St
22852 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b  rlen30(pMem->z);
22853 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79  ..      pMem->ty
22854 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54  pe = SQLITE_TEXT
22855 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65  ;..      pMem->e
22856 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
22857 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  ;..      pMem++;
22858 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 57 68 65  ....      /* Whe
22859 6e 20 61 6e 20 4f 50 5f 50 72 6f 67 72 61 6d 20  n an OP_Program 
2285a 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e  opcode is encoun
2285b 74 65 72 20 28 74 68 65 20 6f 6e 6c 79 20 6f 70  ter (the only op
2285c 63 6f 64 65 20 74 68 61 74 20 68 61 73 0d 0a 20  code that has.. 
2285d 20 20 20 20 20 2a 2a 20 61 20 50 34 5f 53 55 42       ** a P4_SUB
2285e 50 52 4f 47 52 41 4d 20 61 72 67 75 6d 65 6e 74  PROGRAM argument
2285f 29 2c 20 65 78 70 61 6e 64 20 74 68 65 20 73 69  ), expand the si
22860 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  ze of the array 
22861 6f 66 20 73 75 62 70 72 6f 67 72 61 6d 73 0d 0a  of subprograms..
22862 20 20 20 20 20 20 2a 2a 20 6b 65 70 74 20 69 6e        ** kept in
22863 20 70 2d 3e 61 4d 65 6d 5b 39 5d 2e 7a 20 74 6f   p->aMem[9].z to
22864 20 68 6f 6c 64 20 74 68 65 20 6e 65 77 20 70 72   hold the new pr
22865 6f 67 72 61 6d 20 2d 20 61 73 73 75 6d 69 6e 67  ogram - assuming
22866 20 74 68 69 73 20 73 75 62 70 72 6f 67 72 61 6d   this subprogram
22867 0d 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 6e  ..      ** has n
22868 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ot already been 
22869 73 65 65 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  seen...      */.
2286a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
2286b 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52  p4type==P4_SUBPR
2286c 4f 47 52 41 4d 20 29 7b 0d 0a 20 20 20 20 20 20  OGRAM ){..      
2286d 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e    int nByte = (n
2286e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75  Sub+1)*sizeof(Su
2286f 62 50 72 6f 67 72 61 6d 2a 29 3b 0d 0a 20 20 20  bProgram*);..   
22870 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20 20 20       int j;..   
22871 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
22872 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20  nSub; j++){..   
22873 20 20 20 20 20 20 20 69 66 28 20 61 70 53 75 62         if( apSub
22874 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72  [j]==pOp->p4.pPr
22875 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0d 0a  ogram ) break;..
22876 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
22877 20 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26     if( j==nSub &
22878 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c  & SQLITE_OK==sql
22879 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
2287a 70 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20  pSub, nByte, 1) 
2287b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 70  ){..          ap
2287c 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61  Sub = (SubProgra
2287d 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0d 0a 20  m **)pSub->z;.. 
2287e 20 20 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e           apSub[n
2287f 53 75 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34  Sub++] = pOp->p4
22880 2e 70 50 72 6f 67 72 61 6d 3b 0d 0a 20 20 20 20  .pProgram;..    
22881 20 20 20 20 20 20 70 53 75 62 2d 3e 66 6c 61 67        pSub->flag
22882 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0d 0a  s |= MEM_Blob;..
22883 20 20 20 20 20 20 20 20 20 20 70 53 75 62 2d 3e            pSub->
22884 6e 20 3d 20 6e 53 75 62 2a 73 69 7a 65 6f 66 28  n = nSub*sizeof(
22885 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0d 0a 20  SubProgram*);.. 
22886 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
22887 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  }..    }....    
22888 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
22889 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20 70 4d 65 6d  M_Int;..    pMem
2288a 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  ->u.i = pOp->p1;
2288b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2288c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20            /* P1 
2288d 2a 2f 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79  */..    pMem->ty
2288e 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  pe = SQLITE_INTE
2288f 47 45 52 3b 0d 0a 20 20 20 20 70 4d 65 6d 2b 2b  GER;..    pMem++
22890 3b 0d 0a 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  ;....    pMem->f
22891 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0d  lags = MEM_Int;.
22892 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  .    pMem->u.i =
22893 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20   pOp->p2;       
22894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22895 20 20 20 2f 2a 20 50 32 20 2a 2f 0d 0a 20 20 20     /* P2 */..   
22896 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
22897 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0d 0a 20  LITE_INTEGER;.. 
22898 20 20 20 70 4d 65 6d 2b 2b 3b 0d 0a 0d 0a 20 20     pMem++;....  
22899 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
2289a 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20 70 4d  MEM_Int;..    pM
2289b 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  em->u.i = pOp->p
2289c 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  3;              
2289d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2289e 33 20 2a 2f 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e  3 */..    pMem->
2289f 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e  type = SQLITE_IN
228a0 54 45 47 45 52 3b 0d 0a 20 20 20 20 70 4d 65 6d  TEGER;..    pMem
228a1 2b 2b 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 73  ++;....    if( s
228a2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
228a3 77 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29  w(pMem, 32, 0) )
228a4 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
228a5 50 34 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73 73  P4 */..      ass
228a6 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c  ert( p->db->mall
228a7 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20  ocFailed );..   
228a8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
228a9 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 7d 0d 0a  _ERROR;..    }..
228aa 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
228ab 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74  = MEM_Dyn|MEM_St
228ac 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0d 0a 20 20 20  r|MEM_Term;..   
228ad 20 7a 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70   z = displayP4(p
228ae 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29  Op, pMem->z, 32)
228af 3b 0d 0a 20 20 20 20 69 66 28 20 7a 21 3d 70 4d  ;..    if( z!=pM
228b0 65 6d 2d 3e 7a 20 29 7b 0d 0a 20 20 20 20 20 20  em->z ){..      
228b1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
228b2 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31  tStr(pMem, z, -1
228b3 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
228b4 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
228b5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d        assert( pM
228b6 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0d 0a 20 20 20  em->z!=0 );..   
228b7 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
228b8 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
228b9 6d 2d 3e 7a 29 3b 0d 0a 20 20 20 20 20 20 70 4d  m->z);..      pM
228ba 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
228bb 5f 55 54 46 38 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _UTF8;..    }.. 
228bc 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
228bd 53 51 4c 49 54 45 5f 54 45 58 54 3b 0d 0a 20 20  SQLITE_TEXT;..  
228be 20 20 70 4d 65 6d 2b 2b 3b 0d 0a 0d 0a 20 20 20    pMem++;....   
228bf 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
228c0 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  =1 ){..      if(
228c1 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
228c2 72 6f 77 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20  row(pMem, 4, 0) 
228c3 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
228c4 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  rt( p->db->mallo
228c5 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20 20  cFailed );..    
228c6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
228c7 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20  E_ERROR;..      
228c8 7d 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  }..      pMem->f
228c9 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d  lags = MEM_Dyn|M
228ca 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
228cb 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20  ..      pMem->n 
228cc 3d 20 32 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  = 2;..      sqli
228cd 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
228ce 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c  pMem->z, "%.2x",
228cf 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20   pOp->p5);   /* 
228d0 50 35 20 2a 2f 0d 0a 20 20 20 20 20 20 70 4d 65  P5 */..      pMe
228d1 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
228d2 5f 54 45 58 54 3b 0d 0a 20 20 20 20 20 20 70 4d  _TEXT;..      pM
228d3 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
228d4 5f 55 54 46 38 3b 0d 0a 20 20 20 20 20 20 70 4d  _UTF8;..      pM
228d5 65 6d 2b 2b 3b 0d 0a 20 20 0d 0a 2f 2f 23 69 66  em++;..  ..//#if
228d6 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
228d7 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  ..      if( pOp-
228d8 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0d 0a 20 20  >zComment ){..  
228d9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
228da 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  s = MEM_Str|MEM_
228db 54 65 72 6d 3b 0d 0a 20 20 20 20 20 20 20 20 70  Term;..        p
228dc 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43  Mem->z = pOp->zC
228dd 6f 6d 6d 65 6e 74 3b 0d 0a 20 20 20 20 20 20 20  omment;..       
228de 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74   pMem->n = sqlit
228df 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d  e3Strlen30(pMem-
228e0 3e 7a 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 4d  >z);..        pM
228e1 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
228e2 5f 55 54 46 38 3b 0d 0a 20 20 20 20 20 20 20 20  _UTF8;..        
228e3 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
228e4 49 54 45 5f 54 45 58 54 3b 0d 0a 20 20 20 20 20  ITE_TEXT;..     
228e5 20 7d 65 6c 73 65 0d 0a 2f 2f 23 65 6e 64 69 66   }else..//#endif
228e6 0d 0a 20 20 20 20 20 20 7b 0d 0a 20 20 20 20 20  ..      {..     
228e7 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
228e8 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20   MEM_Null;      
228e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228ea 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0d 0a   /* Comment */..
228eb 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79          pMem->ty
228ec 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
228ed 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
228ee 7d 0d 0a 0d 0a 20 20 20 20 70 2d 3e 6e 52 65 73  }....    p->nRes
228ef 43 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 34 2a 28  Column = 8 - 4*(
228f0 70 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0d 0a  p->explain-1);..
228f1 20 20 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65      p->pResultSe
228f2 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 3b  t = &p->aMem[1];
228f3 0d 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  ..    p->rc = SQ
228f4 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 72 63  LITE_OK;..    rc
228f5 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0d 0a   = SQLITE_ROW;..
228f6 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
228f7 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
228f8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
228f9 41 49 4e 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66  AIN */....#ifdef
228fa 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 2f   SQLITE_DEBUG../
228fb 2a 0d 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  *..** Print the 
228fc 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65  SQL that was use
228fd 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
228fe 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a  VDBE program...*
228ff 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
22900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
22901 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20  bePrintSql(Vdbe 
22902 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 6e 4f 70 20  *p){..  int nOp 
22903 3d 20 70 2d 3e 6e 4f 70 3b 0d 0a 20 20 56 64 62  = p->nOp;..  Vdb
22904 65 4f 70 20 2a 70 4f 70 3b 0d 0a 20 20 69 66 28  eOp *pOp;..  if(
22905 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b   nOp<1 ) return;
22906 0d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  ..  pOp = &p->aO
22907 70 5b 30 5d 3b 0d 0a 20 20 69 66 28 20 70 4f 70  p[0];..  if( pOp
22908 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61  ->opcode==OP_Tra
22909 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21  ce && pOp->p4.z!
2290a 3d 30 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74  =0 ){..    const
2290b 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e   char *z = pOp->
2290c 70 34 2e 7a 3b 0d 0a 20 20 20 20 77 68 69 6c 65  p4.z;..    while
2290d 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
2290e 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0d 0a 20 20 20  (*z) ) z++;..   
2290f 20 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25   printf("SQL: [%
22910 73 5d 5c 6e 22 2c 20 7a 29 3b 0d 0a 20 20 7d 0d  s]\n", z);..  }.
22911 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  .}..#endif....#i
22912 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
22913 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
22914 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
22915 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0d  ENABLE_IOTRACE).
22916 0a 2f 2a 0d 0a 2a 2a 20 50 72 69 6e 74 20 61 6e  ./*..** Print an
22917 20 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65   IOTRACE message
22918 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e   showing SQL con
22919 74 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  tent...*/..SQLIT
2291a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2291b 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63  qlite3VdbeIOTrac
2291c 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0d 0a  eSql(Vdbe *p){..
2291d 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e    int nOp = p->n
2291e 4f 70 3b 0d 0a 20 20 56 64 62 65 4f 70 20 2a 70  Op;..  VdbeOp *p
2291f 4f 70 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74  Op;..  if( sqlit
22920 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 29 20 72  e3IoTrace==0 ) r
22921 65 74 75 72 6e 3b 0d 0a 20 20 69 66 28 20 6e 4f  eturn;..  if( nO
22922 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  p<1 ) return;.. 
22923 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30   pOp = &p->aOp[0
22924 5d 3b 0d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f  ];..  if( pOp->o
22925 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20  pcode==OP_Trace 
22926 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20  && pOp->p4.z!=0 
22927 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a  ){..    int i, j
22928 3b 0d 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30  ;..    char z[10
22929 30 30 5d 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  00];..    sqlite
2292a 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
2292b 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70  f(z), z, "%s", p
2292c 4f 70 2d 3e 70 34 2e 7a 29 3b 0d 0a 20 20 20 20  Op->p4.z);..    
2292d 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74 65 33  for(i=0; sqlite3
2292e 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69  Isspace(z[i]); i
2292f 2b 2b 29 7b 7d 0d 0a 20 20 20 20 66 6f 72 28 6a  ++){}..    for(j
22930 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0d  =0; z[i]; i++){.
22931 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
22932 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20  e3Isspace(z[i]) 
22933 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
22934 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0d 0a  z[i-1]!=' ' ){..
22935 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d            z[j++]
22936 20 3d 20 27 20 27 3b 0d 0a 20 20 20 20 20 20 20   = ' ';..       
22937 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   }..      }else{
22938 0d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  ..        z[j++]
22939 20 3d 20 7a 5b 69 5d 3b 0d 0a 20 20 20 20 20 20   = z[i];..      
2293a 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 7a 5b  }..    }..    z[
2293b 6a 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 73 71 6c  j] = 0;..    sql
2293c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51 4c  ite3IoTrace("SQL
2293d 20 25 73 5c 6e 22 2c 20 7a 29 3b 0d 0a 20 20 7d   %s\n", z);..  }
2293e 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ..}..#endif /* !
2293f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
22940 45 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  E && SQLITE_ENAB
22941 4c 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0d 0a 0d  LE_IOTRACE */...
22942 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ./*..** Allocate
22943 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66 69   space from a fi
22944 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 72 20  xed size buffer 
22945 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
22946 6e 74 65 72 20 74 6f 0d 0a 2a 2a 20 74 68 61 74  nter to..** that
22947 20 73 70 61 63 65 2e 20 20 49 66 20 69 6e 73 75   space.  If insu
22948 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 69  fficient space i
22949 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 74  s available, ret
2294a 75 72 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a  urn NULL...**..*
2294b 2a 20 54 68 65 20 70 42 75 66 20 70 61 72 61 6d  * The pBuf param
2294c 65 74 65 72 20 69 73 20 74 68 65 20 69 6e 69 74  eter is the init
2294d 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 61 20 70  ial value of a p
2294e 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 69 6c  ointer which wil
2294f 6c 0d 0a 2a 2a 20 72 65 63 65 69 76 65 20 74 68  l..** receive th
22950 65 20 6e 65 77 20 6d 65 6d 6f 72 79 2e 20 20 70  e new memory.  p
22951 42 75 66 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  Buf is normally 
22952 4e 55 4c 4c 2e 20 20 49 66 20 70 42 75 66 20 69  NULL.  If pBuf i
22953 73 20 6e 6f 74 0d 0a 2a 2a 20 4e 55 4c 4c 2c 20  s not..** NULL, 
22954 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6d 65  it means that me
22955 6d 6f 72 79 20 73 70 61 63 65 20 68 61 73 20 61  mory space has a
22956 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f  lready been allo
22957 63 61 74 65 64 20 61 6e 64 20 74 68 61 74 0d 0a  cated and that..
22958 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
22959 73 68 6f 75 6c 64 20 6e 6f 74 20 61 6c 6c 6f 63  should not alloc
2295a 61 74 65 20 61 6e 79 20 6e 65 77 20 6d 65 6d 6f  ate any new memo
2295b 72 79 2e 20 20 57 68 65 6e 20 70 42 75 66 20 69  ry.  When pBuf i
2295c 73 20 6e 6f 74 0d 0a 2a 2a 20 4e 55 4c 4c 20 73  s not..** NULL s
2295d 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 70 42 75  imply return pBu
2295e 66 2e 20 20 4f 6e 6c 79 20 61 6c 6c 6f 63 61 74  f.  Only allocat
2295f 65 20 6e 65 77 20 6d 65 6d 6f 72 79 20 73 70 61  e new memory spa
22960 63 65 20 77 68 65 6e 20 70 42 75 66 0d 0a 2a 2a  ce when pBuf..**
22961 20 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a   is NULL...**..*
22962 2a 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 6e  * nByte is the n
22963 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
22964 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e 0d  f space needed..
22965 0a 2a 2a 0d 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20  .**..** *ppFrom 
22966 70 6f 69 6e 74 73 20 74 6f 20 61 76 61 69 6c 61  points to availa
22967 62 6c 65 20 73 70 61 63 65 20 61 6e 64 20 70 45  ble space and pE
22968 6e 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  nd points to the
22969 20 65 6e 64 20 6f 66 20 74 68 65 0d 0a 2a 2a 20   end of the..** 
2296a 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 2e  available space.
2296b 20 20 57 68 65 6e 20 73 70 61 63 65 20 69 73 20    When space is 
2296c 61 6c 6c 6f 63 61 74 65 64 2c 20 2a 70 70 46 72  allocated, *ppFr
2296d 6f 6d 20 69 73 20 61 64 76 61 6e 63 65 64 20 70  om is advanced p
2296e 61 73 74 0d 0a 2a 2a 20 74 68 65 20 65 6e 64 20  ast..** the end 
2296f 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  of the allocated
22970 20 73 70 61 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   space...**..** 
22971 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75  *pnByte is a cou
22972 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62  nter of the numb
22973 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
22974 70 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66  pace that have f
22975 61 69 6c 65 64 0d 0a 2a 2a 20 74 6f 20 61 6c 6c  ailed..** to all
22976 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 65  ocate.  If there
22977 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74   is insufficient
22978 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f   space in *ppFro
22979 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  m to satisfy the
2297a 0d 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68  ..** request, th
2297b 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e  en increment *pn
2297c 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75  Byte by the amou
2297d 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  nt of the reques
2297e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  t...*/..static v
2297f 6f 69 64 20 2a 61 6c 6c 6f 63 53 70 61 63 65 28  oid *allocSpace(
22980 0d 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  ..  void *pBuf, 
22981 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72           /* Wher
22982 65 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72  e return pointer
22983 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20   will be stored 
22984 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c  */..  int nByte,
22985 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
22986 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f  mber of bytes to
22987 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0d 0a 20 20   allocate */..  
22988 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20  u8 **ppFrom,    
22989 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
2298a 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70  Allocate from *p
2298b 70 46 72 6f 6d 20 2a 2f 0d 0a 20 20 75 38 20 2a  pFrom */..  u8 *
2298c 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  pEnd,           
2298d 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31   /* Pointer to 1
2298e 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
2298f 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75  nd of *ppFrom bu
22990 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  ffer */..  int *
22991 70 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20  pnByte          
22992 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  /* If allocation
22993 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c   cannot be made,
22994 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79   increment *pnBy
22995 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 61 73 73  te */..){..  ass
22996 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
22997 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f  ALIGNMENT(*ppFro
22998 6d 29 20 29 3b 0d 0a 20 20 69 66 28 20 70 42 75  m) );..  if( pBu
22999 66 20 29 20 72 65 74 75 72 6e 20 70 42 75 66 3b  f ) return pBuf;
2299a 0d 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e  ..  nByte = ROUN
2299b 44 38 28 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66  D8(nByte);..  if
2299c 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79  ( &(*ppFrom)[nBy
2299d 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0d 0a  te] <= pEnd ){..
2299e 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64      pBuf = (void
2299f 2a 29 2a 70 70 46 72 6f 6d 3b 0d 0a 20 20 20 20  *)*ppFrom;..    
229a0 2a 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65  *ppFrom += nByte
229a1 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
229a2 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74   *pnByte += nByt
229a3 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  e;..  }..  retur
229a4 6e 20 70 42 75 66 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  n pBuf;..}..../*
229a5 0d 0a 2a 2a 20 52 65 77 69 6e 64 20 74 68 65 20  ..** Rewind the 
229a6 56 44 42 45 20 62 61 63 6b 20 74 6f 20 74 68 65  VDBE back to the
229a7 20 62 65 67 69 6e 6e 69 6e 67 20 69 6e 20 70 72   beginning in pr
229a8 65 70 61 72 61 74 69 6f 6e 20 66 6f 72 0d 0a 2a  eparation for..*
229a9 2a 20 72 75 6e 6e 69 6e 67 20 69 74 2e 0d 0a 2a  * running it...*
229aa 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
229ab 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
229ac 62 65 52 65 77 69 6e 64 28 56 64 62 65 20 2a 70  beRewind(Vdbe *p
229ad 29 7b 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ){..#if defined(
229ae 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
229af 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52   defined(VDBE_PR
229b0 4f 46 49 4c 45 29 0d 0a 20 20 69 6e 74 20 69 3b  OFILE)..  int i;
229b1 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 61 73 73 65  ..#endif..  asse
229b2 72 74 28 20 70 21 3d 30 20 29 3b 0d 0a 20 20 61  rt( p!=0 );..  a
229b3 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
229b4 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
229b5 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 72   );....  /* Ther
229b6 65 20 73 68 6f 75 6c 64 20 62 65 20 61 74 20 6c  e should be at l
229b7 65 61 73 74 20 6f 6e 65 20 6f 70 63 6f 64 65 2e  east one opcode.
229b8 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ..  */..  assert
229b9 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0d 0a 0d  ( p->nOp>0 );...
229ba 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61  .  /* Set the ma
229bb 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 49  gic to VDBE_MAGI
229bc 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 74  C_RUN sooner rat
229bd 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e 20  her than later. 
229be 2a 2f 0d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d  */..  p->magic =
229bf 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b   VDBE_MAGIC_RUN;
229c0 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
229c1 45 5f 44 45 42 55 47 0d 0a 20 20 66 6f 72 28 69  E_DEBUG..  for(i
229c2 3d 31 3b 20 69 3c 70 2d 3e 6e 4d 65 6d 3b 20 69  =1; i<p->nMem; i
229c3 2b 2b 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  ++){..    assert
229c4 28 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 64 62 3d  ( p->aMem[i].db=
229c5 3d 70 2d 3e 64 62 20 29 3b 0d 0a 20 20 7d 0d 0a  =p->db );..  }..
229c6 23 65 6e 64 69 66 0d 0a 20 20 70 2d 3e 70 63 20  #endif..  p->pc 
229c7 3d 20 2d 31 3b 0d 0a 20 20 70 2d 3e 72 63 20 3d  = -1;..  p->rc =
229c8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 70   SQLITE_OK;..  p
229c9 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20  ->errorAction = 
229ca 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 70 2d 3e  OE_Abort;..  p->
229cb 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
229cc 49 43 5f 52 55 4e 3b 0d 0a 20 20 70 2d 3e 6e 43  IC_RUN;..  p->nC
229cd 68 61 6e 67 65 20 3d 20 30 3b 0d 0a 20 20 70 2d  hange = 0;..  p-
229ce 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0d 0a  >cacheCtr = 1;..
229cf 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c    p->minWriteFil
229d0 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0d 0a  eFormat = 255;..
229d1 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20    p->iStatement 
229d2 3d 20 30 3b 0d 0a 20 20 70 2d 3e 6e 46 6b 43 6f  = 0;..  p->nFkCo
229d3 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b 0d 0a 23  nstraint = 0;..#
229d4 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
229d5 4c 45 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  LE..  for(i=0; i
229d6 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0d 0a  <p->nOp; i++){..
229d7 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e      p->aOp[i].cn
229d8 74 20 3d 20 30 3b 0d 0a 20 20 20 20 70 2d 3e 61  t = 0;..    p->a
229d9 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 20 3d 20 30  Op[i].cycles = 0
229da 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
229db 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 65 70  }..../*..** Prep
229dc 61 72 65 20 61 20 76 69 72 74 75 61 6c 20 6d 61  are a virtual ma
229dd 63 68 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74  chine for execut
229de 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 69 72 73  ion for the firs
229df 74 20 74 69 6d 65 20 61 66 74 65 72 0d 0a 2a 2a  t time after..**
229e0 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 76 69   creating the vi
229e1 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
229e2 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68  This involves th
229e3 69 6e 67 73 20 73 75 63 68 0d 0a 2a 2a 20 61 73  ings such..** as
229e4 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63   allocating stac
229e5 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74  k space and init
229e6 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f  ializing the pro
229e7 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0d 0a 2a  gram counter...*
229e8 2a 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45  * After the VDBE
229e9 20 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c   has be prepped,
229ea 20 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75   it can be execu
229eb 74 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f  ted by one or mo
229ec 72 65 0d 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  re..** calls to 
229ed 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
229ee 29 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ).  ..**..** Thi
229ef 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62  s function may b
229f0 65 20 63 61 6c 6c 65 64 20 65 78 61 63 74 20 6f  e called exact o
229f1 6e 63 65 20 6f 6e 20 61 20 65 61 63 68 20 76 69  nce on a each vi
229f2 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0d 0a  rtual machine...
229f3 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
229f4 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
229f5 74 68 65 20 56 4d 20 68 61 73 20 62 65 65 6e 20  the VM has been 
229f6 22 70 61 63 6b 61 67 65 64 22 20 61 6e 64 20 69  "packaged" and i
229f7 73 20 72 65 61 64 79 0d 0a 2a 2a 20 74 6f 20 72  s ready..** to r
229f8 75 6e 2e 20 20 41 66 74 65 72 20 74 68 69 73 20  un.  After this 
229f9 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
229fa 64 2c 20 66 75 74 68 65 72 20 63 61 6c 6c 73 20  d, futher calls 
229fb 74 6f 20 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 56  to ..** sqlite3V
229fc 64 62 65 41 64 64 4f 70 28 29 20 66 75 6e 63 74  dbeAddOp() funct
229fd 69 6f 6e 73 20 61 72 65 20 70 72 6f 68 69 62 69  ions are prohibi
229fe 74 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ted.  This routi
229ff 6e 65 20 64 69 73 63 6f 6e 6e 65 63 74 73 0d 0a  ne disconnects..
22a00 2a 2a 20 74 68 65 20 56 64 62 65 20 66 72 6f 6d  ** the Vdbe from
22a01 20 74 68 65 20 50 61 72 73 65 20 6f 62 6a 65 63   the Parse objec
22a02 74 20 74 68 61 74 20 68 65 6c 70 65 64 20 67 65  t that helped ge
22a03 6e 65 72 61 74 65 20 69 74 20 73 6f 20 74 68 61  nerate it so tha
22a04 74 20 74 68 65 0d 0a 2a 2a 20 74 68 65 20 56 64  t the..** the Vd
22a05 62 65 20 62 65 63 6f 6d 65 73 20 61 6e 20 69 6e  be becomes an in
22a06 64 65 70 65 6e 64 65 6e 74 20 65 6e 74 69 74 79  dependent entity
22a07 20 61 6e 64 20 74 68 65 20 50 61 72 73 65 20 6f   and the Parse o
22a08 62 6a 65 63 74 20 63 61 6e 20 62 65 0d 0a 2a 2a  bject can be..**
22a09 20 64 65 73 74 72 6f 79 65 64 2e 0d 0a 2a 2a 0d   destroyed...**.
22a0a 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
22a0b 74 65 33 56 64 62 65 52 65 77 69 6e 64 28 29 20  te3VdbeRewind() 
22a0c 70 72 6f 63 65 64 75 72 65 20 74 6f 20 72 65 73  procedure to res
22a0d 74 6f 72 65 20 61 20 76 69 72 74 75 61 6c 20 6d  tore a virtual m
22a0e 61 63 68 69 6e 65 20 62 61 63 6b 0d 0a 2a 2a 20  achine back..** 
22a0f 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73  to its initial s
22a10 74 61 74 65 20 61 66 74 65 72 20 69 74 20 68 61  tate after it ha
22a11 73 20 62 65 65 6e 20 72 75 6e 2e 0d 0a 2a 2f 0d  s been run...*/.
22a12 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22a13 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
22a14 4d 61 6b 65 52 65 61 64 79 28 0d 0a 20 20 56 64  MakeReady(..  Vd
22a15 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  be *p,          
22a16 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22a17 54 68 65 20 56 44 42 45 20 2a 2f 0d 0a 20 20 50  The VDBE */..  P
22a18 61 72 73 65 20 2a 70 50 61 72 73 65 20 20 20 20  arse *pParse    
22a19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22a1a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
22a1b 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74   */..){..  sqlit
22a1c 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
22a1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22a1e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
22a1f 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  tion */..  int n
22a20 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Var;            
22a21 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
22a22 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72  ber of parameter
22a23 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65 6d  s */..  int nMem
22a24 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22a25 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
22a26 20 6f 66 20 56 4d 20 6d 65 6d 6f 72 79 20 72 65   of VM memory re
22a27 67 69 73 74 65 72 73 20 2a 2f 0d 0a 20 20 69 6e  gisters */..  in
22a28 74 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20  t nCursor;      
22a29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22a2a 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
22a2b 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20  s required */.. 
22a2c 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20 20   int nArg;      
22a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a2e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
22a2f 75 6d 65 6e 74 73 20 69 6e 20 73 75 62 70 72 6f  uments in subpro
22a30 67 72 61 6d 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  grams */..  int 
22a31 6e 4f 6e 63 65 3b 20 20 20 20 20 20 20 20 20 20  nOnce;          
22a32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
22a33 6d 62 65 72 20 6f 66 20 4f 50 5f 4f 6e 63 65 20  mber of OP_Once 
22a34 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0d  instructions */.
22a35 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
22a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a37 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
22a38 72 20 2a 2f 0d 0a 20 20 75 38 20 2a 7a 43 73 72  r */..  u8 *zCsr
22a39 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22a3a 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
22a3b 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61   available for a
22a3c 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20  llocation */..  
22a3d 75 38 20 2a 7a 45 6e 64 3b 20 20 20 20 20 20 20  u8 *zEnd;       
22a3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22a3f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73  * First byte pas
22a40 74 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  t allocated memo
22a41 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79  ry */..  int nBy
22a42 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
22a43 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 6d          /* How m
22a44 75 63 68 20 65 78 74 72 61 20 6d 65 6d 6f 72 79  uch extra memory
22a45 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0d 0a 0d   is needed */...
22a46 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
22a47 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  );..  assert( p-
22a48 3e 6e 4f 70 3e 30 20 29 3b 0d 0a 20 20 61 73 73  >nOp>0 );..  ass
22a49 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29  ert( pParse!=0 )
22a4a 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
22a4b 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
22a4c 43 5f 49 4e 49 54 20 29 3b 0d 0a 20 20 64 62 20  C_INIT );..  db 
22a4d 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20 61 73 73 65  = p->db;..  asse
22a4e 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
22a4f 69 6c 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 6e 56  iled==0 );..  nV
22a50 61 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61  ar = pParse->nVa
22a51 72 3b 0d 0a 20 20 6e 4d 65 6d 20 3d 20 70 50 61  r;..  nMem = pPa
22a52 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 6e 43  rse->nMem;..  nC
22a53 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e  ursor = pParse->
22a54 6e 54 61 62 3b 0d 0a 20 20 6e 41 72 67 20 3d 20  nTab;..  nArg = 
22a55 70 50 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 3b  pParse->nMaxArg;
22a56 0d 0a 20 20 6e 4f 6e 63 65 20 3d 20 70 50 61 72  ..  nOnce = pPar
22a57 73 65 2d 3e 6e 4f 6e 63 65 3b 0d 0a 20 20 69 66  se->nOnce;..  if
22a58 28 20 6e 4f 6e 63 65 3d 3d 30 20 29 20 6e 4f 6e  ( nOnce==0 ) nOn
22a59 63 65 20 3d 20 31 3b 20 2f 2a 20 45 6e 73 75 72  ce = 1; /* Ensur
22a5a 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 62  e at least one b
22a5b 79 74 65 20 69 6e 20 70 2d 3e 61 4f 6e 63 65 46  yte in p->aOnceF
22a5c 6c 61 67 5b 5d 20 2a 2f 0d 0a 20 20 0d 0a 20 20  lag[] */..  ..  
22a5d 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 75 72 73  /* For each curs
22a5e 6f 72 20 72 65 71 75 69 72 65 64 2c 20 61 6c 73  or required, als
22a5f 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 65 6d  o allocate a mem
22a60 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 6d 6f 72 79  ory cell. Memory
22a61 0d 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 28 6e 4d  ..  ** cells (nM
22a62 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 2e 2e 6e  em+1-nCursor)..n
22a63 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20  Mem, inclusive, 
22a64 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73  will never be us
22a65 65 64 20 62 79 0d 0a 20 20 2a 2a 20 74 68 65 20  ed by..  ** the 
22a66 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49 6e  vdbe program. In
22a67 73 74 65 61 64 20 74 68 65 79 20 61 72 65 20 75  stead they are u
22a68 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
22a69 73 70 61 63 65 20 66 6f 72 0d 0a 20 20 2a 2a 20  space for..  ** 
22a6a 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 72  VdbeCursor/BtCur
22a6b 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e 20  sor structures. 
22a6c 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  The blob of memo
22a6d 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
22a6e 74 68 20 0d 0a 20 20 2a 2a 20 63 75 72 73 6f 72  th ..  ** cursor
22a6f 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20   0 is stored in 
22a70 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d  memory cell nMem
22a71 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e  . Memory cell (n
22a72 4d 65 6d 2d 31 29 0d 0a 20 20 2a 2a 20 73 74 6f  Mem-1)..  ** sto
22a73 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  res the blob of 
22a74 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
22a75 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c  d with cursor 1,
22a76 20 65 74 63 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a   etc...  **..  *
22a77 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f  * See also: allo
22a78 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0d 0a 20  cateCursor()... 
22a79 20 2a 2f 0d 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e   */..  nMem += n
22a7a 43 75 72 73 6f 72 3b 0d 0a 0d 0a 20 20 2f 2a 20  Cursor;....  /* 
22a7b 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
22a7c 6f 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74  or memory regist
22a7d 65 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c  ers, SQL variabl
22a7e 65 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73  es, VDBE cursors
22a7f 20 61 6e 64 20 0d 0a 20 20 2a 2a 20 61 6e 20 61   and ..  ** an a
22a80 72 72 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20  rray to marshal 
22a81 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  SQL function arg
22a82 75 6d 65 6e 74 73 20 69 6e 2e 0d 0a 20 20 2a 2f  uments in...  */
22a83 0d 0a 20 20 7a 43 73 72 20 3d 20 28 75 38 2a 29  ..  zCsr = (u8*)
22a84 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 5d 3b  &p->aOp[p->nOp];
22a85 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
22a86 20 61 76 61 6c 69 61 62 6c 65 20 66 6f 72 20 61   avaliable for a
22a87 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20  llocation */..  
22a88 7a 45 6e 64 20 3d 20 28 75 38 2a 29 26 70 2d 3e  zEnd = (u8*)&p->
22a89 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d  aOp[p->nOpAlloc]
22a8a 3b 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65  ;  /* First byte
22a8b 20 70 61 73 74 20 65 6e 64 20 6f 66 20 7a 43 73   past end of zCs
22a8c 72 5b 5d 20 2a 2f 0d 0a 0d 0a 20 20 72 65 73 6f  r[] */....  reso
22a8d 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20 26  lveP2Values(p, &
22a8e 6e 41 72 67 29 3b 0d 0a 20 20 70 2d 3e 75 73 65  nArg);..  p->use
22a8f 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 28  sStmtJournal = (
22a90 75 38 29 28 70 50 61 72 73 65 2d 3e 69 73 4d 75  u8)(pParse->isMu
22a91 6c 74 69 57 72 69 74 65 20 26 26 20 70 50 61 72  ltiWrite && pPar
22a92 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 3b 0d 0a  se->mayAbort);..
22a93 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78    if( pParse->ex
22a94 70 6c 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30  plain && nMem<10
22a95 20 29 7b 0d 0a 20 20 20 20 6e 4d 65 6d 20 3d 20   ){..    nMem = 
22a96 31 30 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73  10;..  }..  mems
22a97 65 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64  et(zCsr, 0, zEnd
22a98 2d 7a 43 73 72 29 3b 0d 0a 20 20 7a 43 73 72 20  -zCsr);..  zCsr 
22a99 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29  += (zCsr - (u8*)
22a9a 30 29 26 37 3b 0d 0a 20 20 61 73 73 65 72 74 28  0)&7;..  assert(
22a9b 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
22a9c 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0d 0a  NMENT(zCsr) );..
22a9d 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 30    p->expired = 0
22a9e 3b 0d 0a 0d 0a 20 20 2f 2a 20 4d 65 6d 6f 72 79  ;....  /* Memory
22a9f 20 66 6f 72 20 72 65 67 69 73 74 65 72 73 2c 20   for registers, 
22aa0 70 61 72 61 6d 65 74 65 72 73 2c 20 63 75 72 73  parameters, curs
22aa1 6f 72 2c 20 65 74 63 2c 20 69 73 20 61 6c 6c 6f  or, etc, is allo
22aa2 63 61 74 65 64 20 69 6e 20 74 77 6f 0d 0a 20 20  cated in two..  
22aa3 2a 2a 20 70 61 73 73 65 73 2e 20 20 4f 6e 20 74  ** passes.  On t
22aa4 68 65 20 66 69 72 73 74 20 70 61 73 73 2c 20 77  he first pass, w
22aa5 65 20 74 72 79 20 74 6f 20 72 65 75 73 65 20 75  e try to reuse u
22aa6 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 20 74  nused space at t
22aa7 68 65 20 0d 0a 20 20 2a 2a 20 65 6e 64 20 6f 66  he ..  ** end of
22aa8 20 74 68 65 20 6f 70 63 6f 64 65 20 61 72 72 61   the opcode arra
22aa9 79 2e 20 20 49 66 20 77 65 20 61 72 65 20 75 6e  y.  If we are un
22aaa 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20  able to satisfy 
22aab 61 6c 6c 20 6d 65 6d 6f 72 79 0d 0a 20 20 2a 2a  all memory..  **
22aac 20 72 65 71 75 69 72 65 6d 65 6e 74 73 20 62 79   requirements by
22aad 20 72 65 75 73 69 6e 67 20 74 68 65 20 6f 70 63   reusing the opc
22aae 6f 64 65 20 61 72 72 61 79 20 74 61 69 6c 2c 20  ode array tail, 
22aaf 74 68 65 6e 20 74 68 65 20 73 65 63 6f 6e 64 0d  then the second.
22ab0 0a 20 20 2a 2a 20 70 61 73 73 20 77 69 6c 6c 20  .  ** pass will 
22ab1 66 69 6c 6c 20 69 6e 20 74 68 65 20 72 65 73 74  fill in the rest
22ab2 20 75 73 69 6e 67 20 61 20 66 72 65 73 68 20 61   using a fresh a
22ab3 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 0d 0a 20 20  llocation.  ..  
22ab4 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69 73 20 74 77  **..  ** This tw
22ab5 6f 2d 70 61 73 73 20 61 70 70 72 6f 61 63 68 20  o-pass approach 
22ab6 74 68 61 74 20 72 65 75 73 65 73 20 61 73 20 6d  that reuses as m
22ab7 75 63 68 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f  uch memory as po
22ab8 73 73 69 62 6c 65 20 66 72 6f 6d 0d 0a 20 20 2a  ssible from..  *
22ab9 2a 20 74 68 65 20 6c 65 66 74 6f 76 65 72 20 73  * the leftover s
22aba 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20  pace at the end 
22abb 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 61 72  of the opcode ar
22abc 72 61 79 20 63 61 6e 20 73 69 67 6e 69 66 69 63  ray can signific
22abd 61 6e 74 6c 79 0d 0a 20 20 2a 2a 20 72 65 64 75  antly..  ** redu
22abe 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ce the amount of
22abf 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20   memory held by 
22ac0 61 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  a prepared state
22ac1 6d 65 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 64  ment...  */..  d
22ac2 6f 20 7b 0d 0a 20 20 20 20 6e 42 79 74 65 20 3d  o {..    nByte =
22ac3 20 30 3b 0d 0a 20 20 20 20 70 2d 3e 61 4d 65 6d   0;..    p->aMem
22ac4 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d   = allocSpace(p-
22ac5 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65  >aMem, nMem*size
22ac6 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20  of(Mem), &zCsr, 
22ac7 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0d 0a  zEnd, &nByte);..
22ac8 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 61 6c      p->aVar = al
22ac9 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61 56 61 72  locSpace(p->aVar
22aca 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65  , nVar*sizeof(Me
22acb 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c  m), &zCsr, zEnd,
22acc 20 26 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 70   &nByte);..    p
22acd 2d 3e 61 70 41 72 67 20 3d 20 61 6c 6c 6f 63 53  ->apArg = allocS
22ace 70 61 63 65 28 70 2d 3e 61 70 41 72 67 2c 20 6e  pace(p->apArg, n
22acf 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29  Arg*sizeof(Mem*)
22ad0 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26  , &zCsr, zEnd, &
22ad1 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 70 2d 3e  nByte);..    p->
22ad2 61 7a 56 61 72 20 3d 20 61 6c 6c 6f 63 53 70 61  azVar = allocSpa
22ad3 63 65 28 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61  ce(p->azVar, nVa
22ad4 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c  r*sizeof(char*),
22ad5 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
22ad6 42 79 74 65 29 3b 0d 0a 20 20 20 20 70 2d 3e 61  Byte);..    p->a
22ad7 70 43 73 72 20 3d 20 61 6c 6c 6f 63 53 70 61 63  pCsr = allocSpac
22ad8 65 28 70 2d 3e 61 70 43 73 72 2c 20 6e 43 75 72  e(p->apCsr, nCur
22ad9 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 43  sor*sizeof(VdbeC
22ada 75 72 73 6f 72 2a 29 2c 0d 0a 20 20 20 20 20 20  ursor*),..      
22adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22adc 20 20 20 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c      &zCsr, zEnd,
22add 20 26 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 70   &nByte);..    p
22ade 2d 3e 61 4f 6e 63 65 46 6c 61 67 20 3d 20 61 6c  ->aOnceFlag = al
22adf 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61 4f 6e 63  locSpace(p->aOnc
22ae0 65 46 6c 61 67 2c 20 6e 4f 6e 63 65 2c 20 26 7a  eFlag, nOnce, &z
22ae1 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
22ae2 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 6e 42 79  e);..    if( nBy
22ae3 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e  te ){..      p->
22ae4 70 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 44  pFree = sqlite3D
22ae5 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
22ae6 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a  nByte);..    }..
22ae7 20 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46      zCsr = p->pF
22ae8 72 65 65 3b 0d 0a 20 20 20 20 7a 45 6e 64 20 3d  ree;..    zEnd =
22ae9 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0d 0a   &zCsr[nByte];..
22aea 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 20    }while( nByte 
22aeb 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  && !db->mallocFa
22aec 69 6c 65 64 20 29 3b 0d 0a 0d 0a 20 20 70 2d 3e  iled );....  p->
22aed 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29 6e  nCursor = (u16)n
22aee 43 75 72 73 6f 72 3b 0d 0a 20 20 70 2d 3e 6e 4f  Cursor;..  p->nO
22aef 6e 63 65 46 6c 61 67 20 3d 20 6e 4f 6e 63 65 3b  nceFlag = nOnce;
22af0 0d 0a 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20  ..  if( p->aVar 
22af1 29 7b 0d 0a 20 20 20 20 70 2d 3e 6e 56 61 72 20  ){..    p->nVar 
22af2 3d 20 28 79 6e 56 61 72 29 6e 56 61 72 3b 0d 0a  = (ynVar)nVar;..
22af3 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e      for(n=0; n<n
22af4 56 61 72 3b 20 6e 2b 2b 29 7b 0d 0a 20 20 20 20  Var; n++){..    
22af5 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61    p->aVar[n].fla
22af6 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a  gs = MEM_Null;..
22af7 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d        p->aVar[n]
22af8 2e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20 7d  .db = db;..    }
22af9 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e  ..  }..  if( p->
22afa 61 7a 56 61 72 20 29 7b 0d 0a 20 20 20 20 70 2d  azVar ){..    p-
22afb 3e 6e 7a 56 61 72 20 3d 20 70 50 61 72 73 65 2d  >nzVar = pParse-
22afc 3e 6e 7a 56 61 72 3b 0d 0a 20 20 20 20 6d 65 6d  >nzVar;..    mem
22afd 63 70 79 28 70 2d 3e 61 7a 56 61 72 2c 20 70 50  cpy(p->azVar, pP
22afe 61 72 73 65 2d 3e 61 7a 56 61 72 2c 20 70 2d 3e  arse->azVar, p->
22aff 6e 7a 56 61 72 2a 73 69 7a 65 6f 66 28 70 2d 3e  nzVar*sizeof(p->
22b00 61 7a 56 61 72 5b 30 5d 29 29 3b 0d 0a 20 20 20  azVar[0]));..   
22b01 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2d 3e   memset(pParse->
22b02 61 7a 56 61 72 2c 20 30 2c 20 70 50 61 72 73 65  azVar, 0, pParse
22b03 2d 3e 6e 7a 56 61 72 2a 73 69 7a 65 6f 66 28 70  ->nzVar*sizeof(p
22b04 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b 30 5d 29  Parse->azVar[0])
22b05 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  );..  }..  if( p
22b06 2d 3e 61 4d 65 6d 20 29 7b 0d 0a 20 20 20 20 70  ->aMem ){..    p
22b07 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20  ->aMem--;       
22b08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22b09 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72  * aMem[] goes fr
22b0a 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0d 0a 20  om 1..nMem */.. 
22b0b 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65     p->nMem = nMe
22b0c 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
22b0d 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20     /*       not 
22b0e 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a  from 0..nMem-1 *
22b0f 2f 0d 0a 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20  /..    for(n=1; 
22b10 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0d 0a  n<=nMem; n++){..
22b11 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d        p->aMem[n]
22b12 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 76  .flags = MEM_Inv
22b13 61 6c 69 64 3b 0d 0a 20 20 20 20 20 20 70 2d 3e  alid;..      p->
22b14 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b  aMem[n].db = db;
22b15 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
22b16 70 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 70 50 61  p->explain = pPa
22b17 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3b 0d 0a 20  rse->explain;.. 
22b18 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 77 69   sqlite3VdbeRewi
22b19 6e 64 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  nd(p);..}..../*.
22b1a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45  .** Close a VDBE
22b1b 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65   cursor and rele
22b1c 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f  ase all the reso
22b1d 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73 6f  urces that curso
22b1e 72 20 0d 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74  r ..** happens t
22b1f 6f 20 68 6f 6c 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  o hold...*/..SQL
22b20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22b21 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
22b22 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20  Cursor(Vdbe *p, 
22b23 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29  VdbeCursor *pCx)
22b24 7b 0d 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20  {..  if( pCx==0 
22b25 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d  ){..    return;.
22b26 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 56  .  }..  sqlite3V
22b27 64 62 65 53 6f 72 74 65 72 43 6c 6f 73 65 28 70  dbeSorterClose(p
22b28 2d 3e 64 62 2c 20 70 43 78 29 3b 0d 0a 20 20 69  ->db, pCx);..  i
22b29 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0d 0a  f( pCx->pBt ){..
22b2a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
22b2b 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b  Close(pCx->pBt);
22b2c 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78  ..    /* The pCx
22b2d 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62  ->pCursor will b
22b2e 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69  e close automati
22b2f 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69  cally, if it exi
22b30 73 74 73 2c 20 62 79 0d 0a 20 20 20 20 2a 2a 20  sts, by..    ** 
22b31 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20  the call above. 
22b32 2a 2f 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  */..  }else if( 
22b33 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0d  pCx->pCursor ){.
22b34 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
22b35 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78  eCloseCursor(pCx
22b36 2d 3e 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20 7d  ->pCursor);..  }
22b37 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22b38 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
22b39 4c 45 0d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70  LE..  if( pCx->p
22b3a 56 74 61 62 43 75 72 73 6f 72 20 29 7b 0d 0a 20  VtabCursor ){.. 
22b3b 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f     sqlite3_vtab_
22b3c 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
22b3d 73 6f 72 20 3d 20 70 43 78 2d 3e 70 56 74 61 62  sor = pCx->pVtab
22b3e 43 75 72 73 6f 72 3b 0d 0a 20 20 20 20 63 6f 6e  Cursor;..    con
22b3f 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
22b40 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 43 78  e *pModule = pCx
22b41 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 20 20  ->pModule;..    
22b42 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
22b43 3d 20 31 3b 0d 0a 20 20 20 20 70 4d 6f 64 75 6c  = 1;..    pModul
22b44 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43  e->xClose(pVtabC
22b45 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20 70 2d 3e  ursor);..    p->
22b46 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
22b47 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
22b48 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 70 79  }..../*..** Copy
22b49 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72   the values stor
22b4a 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 46 72  ed in the VdbeFr
22b4b 61 6d 65 20 73 74 72 75 63 74 75 72 65 20 74 6f  ame structure to
22b4c 20 69 74 73 20 56 64 62 65 2e 20 54 68 69 73 0d   its Vdbe. This.
22b4d 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 66 6f 72  .** is used, for
22b4e 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61   example, when a
22b4f 20 74 72 69 67 67 65 72 20 73 75 62 2d 70 72 6f   trigger sub-pro
22b50 67 72 61 6d 20 69 73 20 68 61 6c 74 65 64 20 74  gram is halted t
22b51 6f 20 72 65 73 74 6f 72 65 0d 0a 2a 2a 20 63 6f  o restore..** co
22b52 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d 61 69  ntrol to the mai
22b53 6e 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f 0d 0a  n program...*/..
22b54 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
22b55 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  nt sqlite3VdbeFr
22b56 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46  ameRestore(VdbeF
22b57 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0d 0a  rame *pFrame){..
22b58 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61    Vdbe *v = pFra
22b59 6d 65 2d 3e 76 3b 0d 0a 20 20 76 2d 3e 61 4f 6e  me->v;..  v->aOn
22b5a 63 65 46 6c 61 67 20 3d 20 70 46 72 61 6d 65 2d  ceFlag = pFrame-
22b5b 3e 61 4f 6e 63 65 46 6c 61 67 3b 0d 0a 20 20 76  >aOnceFlag;..  v
22b5c 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20 3d 20 70 46  ->nOnceFlag = pF
22b5d 72 61 6d 65 2d 3e 6e 4f 6e 63 65 46 6c 61 67 3b  rame->nOnceFlag;
22b5e 0d 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 70 46 72  ..  v->aOp = pFr
22b5f 61 6d 65 2d 3e 61 4f 70 3b 0d 0a 20 20 76 2d 3e  ame->aOp;..  v->
22b60 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4f  nOp = pFrame->nO
22b61 70 3b 0d 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20  p;..  v->aMem = 
22b62 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0d 0a 20  pFrame->aMem;.. 
22b63 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d   v->nMem = pFram
22b64 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 76 2d 3e 61  e->nMem;..  v->a
22b65 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61  pCsr = pFrame->a
22b66 70 43 73 72 3b 0d 0a 20 20 76 2d 3e 6e 43 75 72  pCsr;..  v->nCur
22b67 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43  sor = pFrame->nC
22b68 75 72 73 6f 72 3b 0d 0a 20 20 76 2d 3e 64 62 2d  ursor;..  v->db-
22b69 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72  >lastRowid = pFr
22b6a 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0d  ame->lastRowid;.
22b6b 0a 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20  .  v->nChange = 
22b6c 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b  pFrame->nChange;
22b6d 0d 0a 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d  ..  return pFram
22b6e 65 2d 3e 70 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  e->pc;..}..../*.
22b6f 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75  .** Close all cu
22b70 72 73 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41  rsors...**..** A
22b71 6c 73 6f 20 72 65 6c 65 61 73 65 20 61 6e 79 20  lso release any 
22b72 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 68  dynamic memory h
22b73 65 6c 64 20 62 79 20 74 68 65 20 56 4d 20 69 6e  eld by the VM in
22b74 20 74 68 65 20 56 64 62 65 2e 61 4d 65 6d 20 6d   the Vdbe.aMem m
22b75 65 6d 6f 72 79 20 0d 0a 2a 2a 20 63 65 6c 6c 20  emory ..** cell 
22b76 61 72 72 61 79 2e 20 54 68 69 73 20 69 73 20 6e  array. This is n
22b77 65 63 65 73 73 61 72 79 20 61 73 20 74 68 65 20  ecessary as the 
22b78 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61  memory cell arra
22b79 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 0d 0a 2a  y may contain..*
22b7a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 56 64  * pointers to Vd
22b7b 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 2c  beFrame objects,
22b7c 20 77 68 69 63 68 20 6d 61 79 20 69 6e 20 74 75   which may in tu
22b7d 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74  rn contain point
22b7e 65 72 73 20 74 6f 0d 0a 2a 2a 20 6f 70 65 6e 20  ers to..** open 
22b7f 63 75 72 73 6f 72 73 2e 0d 0a 2a 2f 0d 0a 73 74  cursors...*/..st
22b80 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41  atic void closeA
22b81 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a  llCursors(Vdbe *
22b82 70 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 46  p){..  if( p->pF
22b83 72 61 6d 65 20 29 7b 0d 0a 20 20 20 20 56 64 62  rame ){..    Vdb
22b84 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0d  eFrame *pFrame;.
22b85 0a 20 20 20 20 66 6f 72 28 70 46 72 61 6d 65 3d  .    for(pFrame=
22b86 70 2d 3e 70 46 72 61 6d 65 3b 20 70 46 72 61 6d  p->pFrame; pFram
22b87 65 2d 3e 70 50 61 72 65 6e 74 3b 20 70 46 72 61  e->pParent; pFra
22b88 6d 65 3d 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  me=pFrame->pPare
22b89 6e 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  nt);..    sqlite
22b8a 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72  3VdbeFrameRestor
22b8b 65 28 70 46 72 61 6d 65 29 3b 0d 0a 20 20 7d 0d  e(pFrame);..  }.
22b8c 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 30  .  p->pFrame = 0
22b8d 3b 0d 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 20 3d  ;..  p->nFrame =
22b8e 20 30 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 2d 3e   0;....  if( p->
22b8f 61 70 43 73 72 20 29 7b 0d 0a 20 20 20 20 69 6e  apCsr ){..    in
22b90 74 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  t i;..    for(i=
22b91 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b  0; i<p->nCursor;
22b92 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 56 64   i++){..      Vd
22b93 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70  beCursor *pC = p
22b94 2d 3e 61 70 43 73 72 5b 69 5d 3b 0d 0a 20 20 20  ->apCsr[i];..   
22b95 20 20 20 69 66 28 20 70 43 20 29 7b 0d 0a 20 20     if( pC ){..  
22b96 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22b97 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70  eFreeCursor(p, p
22b98 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e  C);..        p->
22b99 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0d 0a 20  apCsr[i] = 0;.. 
22b9a 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
22b9b 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65   }..  if( p->aMe
22b9c 6d 20 29 7b 0d 0a 20 20 20 20 72 65 6c 65 61 73  m ){..    releas
22b9d 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d  eMemArray(&p->aM
22b9e 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b  em[1], p->nMem);
22b9f 0d 0a 20 20 7d 0d 0a 20 20 77 68 69 6c 65 28 20  ..  }..  while( 
22ba0 70 2d 3e 70 44 65 6c 46 72 61 6d 65 20 29 7b 0d  p->pDelFrame ){.
22ba1 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a  .    VdbeFrame *
22ba2 70 44 65 6c 20 3d 20 70 2d 3e 70 44 65 6c 46 72  pDel = p->pDelFr
22ba3 61 6d 65 3b 0d 0a 20 20 20 20 70 2d 3e 70 44 65  ame;..    p->pDe
22ba4 6c 46 72 61 6d 65 20 3d 20 70 44 65 6c 2d 3e 70  lFrame = pDel->p
22ba5 50 61 72 65 6e 74 3b 0d 0a 20 20 20 20 73 71 6c  Parent;..    sql
22ba6 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c  ite3VdbeFrameDel
22ba7 65 74 65 28 70 44 65 6c 29 3b 0d 0a 20 20 7d 0d  ete(pDel);..  }.
22ba8 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 65  .}..../*..** Cle
22ba9 61 6e 20 75 70 20 74 68 65 20 56 4d 20 61 66 74  an up the VM aft
22baa 65 72 20 65 78 65 63 75 74 69 6f 6e 2e 0d 0a 2a  er execution...*
22bab 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
22bac 6e 65 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69  ne will automati
22bad 63 61 6c 6c 79 20 63 6c 6f 73 65 20 61 6e 79 20  cally close any 
22bae 63 75 72 73 6f 72 73 2c 20 6c 69 73 74 73 2c 20  cursors, lists, 
22baf 61 6e 64 2f 6f 72 0d 0a 2a 2a 20 73 6f 72 74 65  and/or..** sorte
22bb0 72 73 20 74 68 61 74 20 77 65 72 65 20 6c 65 66  rs that were lef
22bb1 74 20 6f 70 65 6e 2e 20 20 49 74 20 61 6c 73 6f  t open.  It also
22bb2 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c   deletes the val
22bb3 75 65 73 20 6f 66 0d 0a 2a 2a 20 76 61 72 69 61  ues of..** varia
22bb4 62 6c 65 73 20 69 6e 20 74 68 65 20 61 56 61 72  bles in the aVar
22bb5 5b 5d 20 61 72 72 61 79 2e 0d 0a 2a 2f 0d 0a 73  [] array...*/..s
22bb6 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61 6e  tatic void Clean
22bb7 75 70 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20  up(Vdbe *p){..  
22bb8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
22bb9 3e 64 62 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53  >db;....#ifdef S
22bba 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 2f  QLITE_DEBUG..  /
22bbb 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 72 74  * Execute assert
22bbc 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  () statements to
22bbd 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
22bbe 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e   Vdbe.apCsr[] an
22bbf 64 20 0d 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d  d ..  ** Vdbe.aM
22bc0 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65  em[] arrays have
22bc1 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c   already been cl
22bc2 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0d 0a 20  eaned up.  */.. 
22bc3 20 69 6e 74 20 69 3b 0d 0a 20 20 69 66 28 20 70   int i;..  if( p
22bc4 2d 3e 61 70 43 73 72 20 29 20 66 6f 72 28 69 3d  ->apCsr ) for(i=
22bc5 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b  0; i<p->nCursor;
22bc6 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d   i++) assert( p-
22bc7 3e 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0d  >apCsr[i]==0 );.
22bc8 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29  .  if( p->aMem )
22bc9 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  {..    for(i=1; 
22bca 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 29  i<=p->nMem; i++)
22bcb 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 65 6d   assert( p->aMem
22bcc 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49  [i].flags==MEM_I
22bcd 6e 76 61 6c 69 64 20 29 3b 0d 0a 20 20 7d 0d 0a  nvalid );..  }..
22bce 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 73 71 6c 69  #endif....  sqli
22bcf 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
22bd0 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 70 2d  >zErrMsg);..  p-
22bd1 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d 0a 20  >zErrMsg = 0;.. 
22bd2 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d   p->pResultSet =
22bd3 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
22bd4 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
22bd5 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
22bd6 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72  s that will be r
22bd7 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
22bd8 53 51 4c 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  SQL..** statemen
22bd9 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73  t. This is now s
22bda 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  et at compile ti
22bdb 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20  me, rather than 
22bdc 64 75 72 69 6e 67 0d 0a 2a 2a 20 65 78 65 63 75  during..** execu
22bdd 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65  tion of the vdbe
22bde 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74   program so that
22bdf 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22be0 63 6f 75 6e 74 28 29 20 63 61 6e 0d 0a 2a 2a 20  count() can..** 
22be1 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  be called on an 
22be2 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65  SQL statement be
22be3 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65  fore sqlite3_ste
22be4 70 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  p()...*/..SQLITE
22be5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22be6 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
22be7 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ols(Vdbe *p, int
22be8 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0d 0a 20   nResColumn){.. 
22be9 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0d   Mem *pColName;.
22bea 0a 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 73 71 6c  .  int n;..  sql
22beb 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
22bec 3b 0d 0a 0d 0a 20 20 72 65 6c 65 61 73 65 4d 65  ;....  releaseMe
22bed 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61  mArray(p->aColNa
22bee 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d  me, p->nResColum
22bef 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0d 0a 20  n*COLNAME_N);.. 
22bf0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22bf1 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b  b, p->aColName);
22bf2 0d 0a 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75  ..  n = nResColu
22bf3 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0d 0a 20  mn*COLNAME_N;.. 
22bf4 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d   p->nResColumn =
22bf5 20 28 75 31 36 29 6e 52 65 73 43 6f 6c 75 6d 6e   (u16)nResColumn
22bf6 3b 0d 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65  ;..  p->aColName
22bf7 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d   = pColName = (M
22bf8 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  em*)sqlite3DbMal
22bf9 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
22bfa 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0d 0a 20 20  of(Mem)*n );..  
22bfb 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d  if( p->aColName=
22bfc 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  =0 ) return;..  
22bfd 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29  while( n-- > 0 )
22bfe 7b 0d 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d  {..    pColName-
22bff 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
22c00 6c 3b 0d 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65  l;..    pColName
22c01 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20  ->db = p->db;.. 
22c02 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0d 0a     pColName++;..
22c03 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
22c04 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66   Set the name of
22c05 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75   the idx'th colu
22c06 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  mn to be returne
22c07 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61  d by the SQL sta
22c08 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 20 7a 4e 61 6d  tement...** zNam
22c09 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e  e must be a poin
22c0a 74 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72  ter to a nul ter
22c0b 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0d  minated string..
22c0c 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 63 61 6c  .**..** This cal
22c0d 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 65 20 61  l must be made a
22c0e 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73  fter a call to s
22c0f 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
22c10 43 6f 6c 73 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  Cols()...**..** 
22c11 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65  The final parame
22c12 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 20  ter, xDel, must 
22c13 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  be one of SQLITE
22c14 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 45  _DYNAMIC, SQLITE
22c15 5f 53 54 41 54 49 43 0d 0a 2a 2a 20 6f 72 20 53  _STATIC..** or S
22c16 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e  QLITE_TRANSIENT.
22c17 20 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 45   If it is SQLITE
22c18 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74  _DYNAMIC, then t
22c19 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
22c1a 64 0d 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d  d..** to by zNam
22c1b 65 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  e will be freed 
22c1c 62 79 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  by sqlite3DbFree
22c1d 28 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65  () when the vdbe
22c1e 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0d 0a   is destroyed...
22c1f 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
22c20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
22c21 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 0d 0a 20  beSetColName(.. 
22c22 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
22c23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22c24 20 20 2f 2a 20 56 64 62 65 20 62 65 69 6e 67 20    /* Vdbe being 
22c25 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0d 0a 20  configured */.. 
22c26 20 69 6e 74 20 69 64 78 2c 20 20 20 20 20 20 20   int idx,       
22c27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22c28 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
22c29 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c 69  lumn zName appli
22c2a 65 73 20 74 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20  es to */..  int 
22c2b 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  var,            
22c2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22c2d 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41  One of the COLNA
22c2e 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a  ME_* constants *
22c2f 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
22c30 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
22c31 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
22c32 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61   to buffer conta
22c33 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0d 0a 20  ining name */.. 
22c34 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
22c35 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  id*)            
22c36 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61    /* Memory mana
22c37 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20  gement strategy 
22c38 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0d 0a 29 7b  for zName */..){
22c39 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 4d  ..  int rc;..  M
22c3a 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0d 0a 20  em *pColName;.. 
22c3b 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e   assert( idx<p->
22c3c 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0d 0a 20  nResColumn );.. 
22c3d 20 61 73 73 65 72 74 28 20 76 61 72 3c 43 4f 4c   assert( var<COL
22c3e 4e 41 4d 45 5f 4e 20 29 3b 0d 0a 20 20 69 66 28  NAME_N );..  if(
22c3f 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
22c40 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 61 73 73  iled ){..    ass
22c41 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 78  ert( !zName || x
22c42 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41  Del!=SQLITE_DYNA
22c43 4d 49 43 20 29 3b 0d 0a 20 20 20 20 72 65 74 75  MIC );..    retu
22c44 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
22c45 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
22c46 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20   p->aColName!=0 
22c47 29 3b 0d 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d  );..  pColName =
22c48 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69   &(p->aColName[i
22c49 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f  dx+var*p->nResCo
22c4a 6c 75 6d 6e 5d 29 3b 0d 0a 20 20 72 63 20 3d 20  lumn]);..  rc = 
22c4b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
22c4c 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a  tStr(pColName, z
22c4d 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
22c4e 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0d 0a 20  _UTF8, xDel);.. 
22c4f 20 61 73 73 65 72 74 28 20 72 63 21 3d 30 20 7c   assert( rc!=0 |
22c50 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 6f  | !zName || (pCo
22c51 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 4d  lName->flags&MEM
22c52 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0d 0a 20 20  _Term)!=0 );..  
22c53 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
22c54 0a 2f 2a 0d 0a 2a 2a 20 41 20 72 65 61 64 20 6f  ./*..** A read o
22c55 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
22c56 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e  ion may or may n
22c57 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20  ot be active on 
22c58 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0d  database handle.
22c59 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61  .** db. If a tra
22c5a 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
22c5b 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49  ve, commit it. I
22c5c 66 20 74 68 65 72 65 20 69 73 20 61 0d 0a 2a 2a  f there is a..**
22c5d 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
22c5e 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65  on spanning more
22c5f 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61   than one databa
22c60 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f  se file, this ro
22c61 75 74 69 6e 65 0d 0a 2a 2a 20 74 61 6b 65 73 20  utine..** takes 
22c62 63 61 72 65 20 6f 66 20 74 68 65 20 6d 61 73 74  care of the mast
22c63 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b  er journal trick
22c64 65 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ery...*/..static
22c65 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 28   int vdbeCommit(
22c66 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62  sqlite3 *db, Vdb
22c67 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  e *p){..  int i;
22c68 0d 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d  ..  int nTrans =
22c69 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f   0;  /* Number o
22c6a 66 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68  f databases with
22c6b 20 61 6e 20 61 63 74 69 76 65 20 77 72 69 74 65   an active write
22c6c 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d  -transaction */.
22c6d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
22c6e 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74 20 6e 65  TE_OK;..  int ne
22c6f 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0d 0a  edXcommit = 0;..
22c70 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
22c71 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
22c72 45 0d 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 69  E..  /* With thi
22c73 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 65  s option, sqlite
22c74 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 20 64  3VtabSync() is d
22c75 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 69 6d  efined to be sim
22c76 70 6c 79 20 0d 0a 20 20 2a 2a 20 53 51 4c 49 54  ply ..  ** SQLIT
22c77 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74  E_OK so p is not
22c78 20 75 73 65 64 2e 20 0d 0a 20 20 2a 2f 0d 0a 20   used. ..  */.. 
22c79 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
22c7a 52 28 70 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  R(p);..#endif...
22c7b 0a 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69  .  /* Before doi
22c7c 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  ng anything else
22c7d 2c 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63  , call the xSync
22c7e 28 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  () callback for 
22c7f 61 6e 79 0d 0a 20 20 2a 2a 20 76 69 72 74 75 61  any..  ** virtua
22c80 6c 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20  l module tables 
22c81 77 72 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20  written in this 
22c82 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69  transaction. Thi
22c83 73 20 68 61 73 20 74 6f 0d 0a 20 20 2a 2a 20 62  s has to..  ** b
22c84 65 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 64 65  e done before de
22c85 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74 68 65  termining whethe
22c86 72 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  r a master journ
22c87 61 6c 20 66 69 6c 65 20 69 73 20 0d 0a 20 20 2a  al file is ..  *
22c88 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73 20 61  * required, as a
22c89 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61  n xSync() callba
22c8a 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20 61 74  ck may add an at
22c8b 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 0d  tached database.
22c8c 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72 61  .  ** to the tra
22c8d 6e 73 61 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d  nsaction...  */.
22c8e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
22c8f 74 61 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e  tabSync(db, &p->
22c90 7a 45 72 72 4d 73 67 29 3b 0d 0a 0d 0a 20 20 2f  zErrMsg);....  /
22c91 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65  * This loop dete
22c92 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 68  rmines (a) if th
22c93 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68  e commit hook sh
22c94 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20  ould be invoked 
22c95 61 6e 64 0d 0a 20 20 2a 2a 20 28 62 29 20 68 6f  and..  ** (b) ho
22c96 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20  w many database 
22c97 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20  files have open 
22c98 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
22c99 6e 73 2c 20 6e 6f 74 20 0d 0a 20 20 2a 2a 20 69  ns, not ..  ** i
22c9a 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d  ncluding the tem
22c9b 70 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20  p database. (b) 
22c9c 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63  is important bec
22c9d 61 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61  ause if more tha
22c9e 6e 20 0d 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74  n ..  ** one dat
22c9f 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 61  abase file has a
22ca0 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61  n open write tra
22ca1 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74  nsaction, a mast
22ca2 65 72 20 6a 6f 75 72 6e 61 6c 0d 0a 20 20 2a 2a  er journal..  **
22ca3 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 72 65   file is require
22ca4 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20  d for an atomic 
22ca5 63 6f 6d 6d 69 74 2e 0d 0a 20 20 2a 2f 20 0d 0a  commit...  */ ..
22ca6 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
22ca7 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
22ca8 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0d 0a 20  ->nDb; i++){ .. 
22ca9 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
22caa 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0d  db->aDb[i].pBt;.
22cab 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
22cac 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
22cad 42 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 6e 65  Bt) ){..      ne
22cae 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b 0d 0a  edXcommit = 1;..
22caf 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 20 29        if( i!=1 )
22cb0 20 6e 54 72 61 6e 73 2b 2b 3b 0d 0a 20 20 20 20   nTrans++;..    
22cb1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
22cb2 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f 63 6b  gerExclusiveLock
22cb3 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  (sqlite3BtreePag
22cb4 65 72 28 70 42 74 29 29 3b 0d 0a 20 20 20 20 7d  er(pBt));..    }
22cb5 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 21  ..  }..  if( rc!
22cb6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
22cb7 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
22cb8 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68   }....  /* If th
22cb9 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 74  ere are any writ
22cba 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  e-transactions a
22cbb 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68  t all, invoke th
22cbc 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f  e commit hook */
22cbd 0d 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d  ..  if( needXcom
22cbe 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d  mit && db->xComm
22cbf 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0d 0a 20  itCallback ){.. 
22cc0 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d     rc = db->xCom
22cc1 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  mitCallback(db->
22cc2 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0d 0a 20 20  pCommitArg);..  
22cc3 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20    if( rc ){..   
22cc4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
22cc5 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0d 0a 20 20  _CONSTRAINT;..  
22cc6 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
22cc7 20 54 68 65 20 73 69 6d 70 6c 65 20 63 61 73 65   The simple case
22cc8 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20   - no more than 
22cc9 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
22cca 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 6e 67 20  e (not counting 
22ccb 74 68 65 0d 0a 20 20 2a 2a 20 54 45 4d 50 20 64  the..  ** TEMP d
22ccc 61 74 61 62 61 73 65 29 20 68 61 73 20 61 20 74  atabase) has a t
22ccd 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76  ransaction activ
22cce 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 6e 6f  e.   There is no
22ccf 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0d 0a 20   need for the.. 
22cd0 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e   ** master-journ
22cd1 61 6c 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  al...  **..  ** 
22cd2 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  If the return va
22cd3 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74  lue of sqlite3Bt
22cd4 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29  reeGetFilename()
22cd5 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74   is a zero lengt
22cd6 68 0d 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20  h..  ** string, 
22cd7 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69  it means the mai
22cd8 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d  n database is :m
22cd9 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70  emory: or a temp
22cda 20 66 69 6c 65 2e 20 20 49 6e 20 0d 0a 20 20 2a   file.  In ..  *
22cdb 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 20 64  * that case we d
22cdc 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74  o not support at
22cdd 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20  omic multi-file 
22cde 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20  commits, so use 
22cdf 74 68 65 20 0d 0a 20 20 2a 2a 20 73 69 6d 70 6c  the ..  ** simpl
22ce0 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e  e case then too.
22ce1 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 30 3d  ..  */..  if( 0=
22ce2 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  =sqlite3Strlen30
22ce3 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  (sqlite3BtreeGet
22ce4 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62  Filename(db->aDb
22ce5 5b 30 5d 2e 70 42 74 29 29 0d 0a 20 20 20 7c 7c  [0].pBt))..   ||
22ce6 20 6e 54 72 61 6e 73 3c 3d 31 0d 0a 20 20 29 7b   nTrans<=1..  ){
22ce7 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72  ..    for(i=0; r
22ce8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
22ce9 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
22cea 0d 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ..      Btree *p
22ceb 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
22cec 70 42 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  pBt;..      if( 
22ced 70 42 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  pBt ){..        
22cee 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
22cef 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
22cf0 70 42 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  pBt, 0);..      
22cf1 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  }..    }....    
22cf2 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 74  /* Do the commit
22cf3 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 74   only if all dat
22cf4 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 75  abases successfu
22cf5 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 61  lly complete pha
22cf6 73 65 20 31 2e 20 0d 0a 20 20 20 20 2a 2a 20 49  se 1. ..    ** I
22cf7 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72  f one of the Btr
22cf8 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
22cf9 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20  () calls fails, 
22cfa 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61  this indicates a
22cfb 6e 0d 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72  n..    ** IO err
22cfc 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e  or while deletin
22cfd 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20  g or truncating 
22cfe 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  a journal file. 
22cff 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0d  It is unlikely,.
22d00 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c  .    ** but coul
22d01 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69  d happen. In thi
22d02 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70  s case abandon p
22d03 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65  rocessing and re
22d04 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0d  turn the error..
22d05 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72  .    */..    for
22d06 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
22d07 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
22d08 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 42  ; i++){..      B
22d09 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
22d0a 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20  aDb[i].pBt;..   
22d0b 20 20 20 69 66 28 20 70 42 74 20 29 7b 0d 0a 20     if( pBt ){.. 
22d0c 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
22d0d 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
22d0e 61 73 65 54 77 6f 28 70 42 74 2c 20 30 29 3b 0d  aseTwo(pBt, 0);.
22d0f 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
22d10 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
22d11 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
22d12 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d   sqlite3VtabComm
22d13 69 74 28 64 62 29 3b 0d 0a 20 20 20 20 7d 0d 0a  it(db);..    }..
22d14 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20    }....  /* The 
22d15 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54  complex case - T
22d16 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d  here is a multi-
22d17 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73  file write-trans
22d18 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0d 0a  action active...
22d19 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72    ** This requir
22d1a 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  es a master jour
22d1b 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75  nal file to ensu
22d1c 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
22d1d 6f 6e 20 69 73 0d 0a 20 20 2a 2a 20 63 6f 6d 6d  on is..  ** comm
22d1e 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0d  itted atomicly..
22d1f 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  .  */..#ifndef S
22d20 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
22d21 4f 0d 0a 20 20 65 6c 73 65 7b 0d 0a 20 20 20 20  O..  else{..    
22d22 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
22d23 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0d 0a 20  s = db->pVfs;.. 
22d24 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20     int needSync 
22d25 3d 20 30 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a  = 0;..    char *
22d26 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 2f  zMaster = 0;   /
22d27 2a 20 46 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20  * File-name for 
22d28 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
22d29 61 6c 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20  al */..    char 
22d2a 63 6f 6e 73 74 20 2a 7a 4d 61 69 6e 46 69 6c 65  const *zMainFile
22d2b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
22d2c 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61  etFilename(db->a
22d2d 44 62 5b 30 5d 2e 70 42 74 29 3b 0d 0a 20 20 20  Db[0].pBt);..   
22d2e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
22d2f 4d 61 73 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20  Master = 0;..   
22d30 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b   i64 offset = 0;
22d31 0d 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0d 0a  ..    int res;..
22d32 20 20 20 20 69 6e 74 20 72 65 74 72 79 43 6f 75      int retryCou
22d33 6e 74 20 3d 20 30 3b 0d 0a 20 20 20 20 69 6e 74  nt = 0;..    int
22d34 20 6e 4d 61 69 6e 46 69 6c 65 3b 0d 0a 0d 0a 20   nMainFile;.... 
22d35 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 61 20 6d     /* Select a m
22d36 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
22d37 6c 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20  le name */..    
22d38 6e 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69  nMainFile = sqli
22d39 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 61 69  te3Strlen30(zMai
22d3a 6e 46 69 6c 65 29 3b 0d 0a 20 20 20 20 7a 4d 61  nFile);..    zMa
22d3b 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  ster = sqlite3MP
22d3c 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a  rintf(db, "%s-mj
22d3d 58 58 58 58 58 58 39 58 58 7a 22 2c 20 7a 4d 61  XXXXXX9XXz", zMa
22d3e 69 6e 46 69 6c 65 29 3b 0d 0a 20 20 20 20 69 66  inFile);..    if
22d3f 28 20 7a 4d 61 73 74 65 72 3d 3d 30 20 29 20 72  ( zMaster==0 ) r
22d40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
22d41 45 4d 3b 0d 0a 20 20 20 20 64 6f 20 7b 0d 0a 20  EM;..    do {.. 
22d42 20 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d       u32 iRandom
22d43 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 65 74  ;..      if( ret
22d44 72 79 43 6f 75 6e 74 20 29 7b 0d 0a 20 20 20 20  ryCount ){..    
22d45 20 20 20 20 69 66 28 20 72 65 74 72 79 43 6f 75      if( retryCou
22d46 6e 74 3e 31 30 30 20 29 7b 0d 0a 20 20 20 20 20  nt>100 ){..     
22d47 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67       sqlite3_log
22d48 28 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 22 4d  (SQLITE_FULL, "M
22d49 4a 20 64 65 6c 65 74 65 3a 20 25 73 22 2c 20 7a  J delete: %s", z
22d4a 4d 61 73 74 65 72 29 3b 0d 0a 20 20 20 20 20 20  Master);..      
22d4b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c      sqlite3OsDel
22d4c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ete(pVfs, zMaste
22d4d 72 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  r, 0);..        
22d4e 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
22d4f 20 20 7d 65 6c 73 65 20 69 66 28 20 72 65 74 72    }else if( retr
22d50 79 43 6f 75 6e 74 3d 3d 31 20 29 7b 0d 0a 20 20  yCount==1 ){..  
22d51 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22d52 6c 6f 67 28 53 51 4c 49 54 45 5f 46 55 4c 4c 2c  log(SQLITE_FULL,
22d53 20 22 4d 4a 20 63 6f 6c 6c 69 64 65 3a 20 25 73   "MJ collide: %s
22d54 22 2c 20 7a 4d 61 73 74 65 72 29 3b 0d 0a 20 20  ", zMaster);..  
22d55 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
22d56 0d 0a 20 20 20 20 20 20 72 65 74 72 79 43 6f 75  ..      retryCou
22d57 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 73 71 6c  nt++;..      sql
22d58 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
22d59 73 69 7a 65 6f 66 28 69 52 61 6e 64 6f 6d 29 2c  sizeof(iRandom),
22d5a 20 26 69 52 61 6e 64 6f 6d 29 3b 0d 0a 20 20 20   &iRandom);..   
22d5b 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
22d5c 6e 74 66 28 31 33 2c 20 26 7a 4d 61 73 74 65 72  ntf(13, &zMaster
22d5d 5b 6e 4d 61 69 6e 46 69 6c 65 5d 2c 20 22 2d 6d  [nMainFile], "-m
22d5e 6a 25 30 36 58 39 25 30 32 58 22 2c 0d 0a 20 20  j%06X9%02X",..  
22d5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22d60 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 52               (iR
22d61 61 6e 64 6f 6d 3e 3e 38 29 26 30 78 66 66 66 66  andom>>8)&0xffff
22d62 66 66 2c 20 69 52 61 6e 64 6f 6d 26 30 78 66 66  ff, iRandom&0xff
22d63 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  );..      /* The
22d64 20 61 6e 74 69 70 65 6e 75 6c 74 69 6d 61 74 65   antipenultimate
22d65 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
22d66 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
22d67 20 6e 61 6d 65 20 6d 75 73 74 0d 0a 20 20 20 20   name must..    
22d68 20 20 2a 2a 20 62 65 20 22 39 22 20 74 6f 20 61    ** be "9" to a
22d69 76 6f 69 64 20 6e 61 6d 65 20 63 6f 6c 6c 69 73  void name collis
22d6a 69 6f 6e 73 20 77 68 65 6e 20 75 73 69 6e 67 20  ions when using 
22d6b 38 2b 33 20 66 69 6c 65 6e 61 6d 65 73 2e 20 2a  8+3 filenames. *
22d6c 2f 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  /..      assert(
22d6d 20 7a 4d 61 73 74 65 72 5b 73 71 6c 69 74 65 33   zMaster[sqlite3
22d6e 53 74 72 6c 65 6e 33 30 28 7a 4d 61 73 74 65 72  Strlen30(zMaster
22d6f 29 2d 33 5d 3d 3d 27 39 27 20 29 3b 0d 0a 20 20  )-3]=='9' );..  
22d70 20 20 20 20 73 71 6c 69 74 65 33 46 69 6c 65 53      sqlite3FileS
22d71 75 66 66 69 78 33 28 7a 4d 61 69 6e 46 69 6c 65  uffix3(zMainFile
22d72 2c 20 7a 4d 61 73 74 65 72 29 3b 0d 0a 20 20 20  , zMaster);..   
22d73 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
22d74 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d  sAccess(pVfs, zM
22d75 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43  aster, SQLITE_AC
22d76 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65  CESS_EXISTS, &re
22d77 73 29 3b 0d 0a 20 20 20 20 7d 77 68 69 6c 65 28  s);..    }while(
22d78 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
22d79 26 20 72 65 73 20 29 3b 0d 0a 20 20 20 20 69 66  & res );..    if
22d7a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
22d7b 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65  ){..      /* Ope
22d7c 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  n the master jou
22d7d 72 6e 61 6c 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  rnal. */..      
22d7e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
22d7f 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a  enMalloc(pVfs, z
22d80 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72  Master, &pMaster
22d81 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 53 51  , ..          SQ
22d82 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
22d83 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
22d84 43 52 45 41 54 45 7c 0d 0a 20 20 20 20 20 20 20  CREATE|..       
22d85 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
22d86 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f  XCLUSIVE|SQLITE_
22d87 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
22d88 4e 41 4c 2c 20 30 0d 0a 20 20 20 20 20 20 29 3b  NAL, 0..      );
22d89 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
22d8a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
22d8b 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
22d8c 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
22d8d 65 72 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  er);..      retu
22d8e 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20  rn rc;..    }.. 
22d8f 0d 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  ..    /* Write t
22d90 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20  he name of each 
22d91 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
22d92 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
22d93 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0d 0a 20   into the new.. 
22d94 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75     ** master jou
22d95 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e  rnal file. If an
22d96 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74   error occurs at
22d97 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73   this point clos
22d98 65 0d 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65  e..    ** and de
22d99 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
22d9a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c  journal file. Al
22d9b 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c  l the individual
22d9c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0d 0a   journal files..
22d9d 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76      ** still hav
22d9e 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20  e 'null' as the 
22d9f 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70  master journal p
22da0 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20  ointer, so they 
22da1 77 69 6c 6c 20 72 6f 6c 6c 0d 0a 20 20 20 20 2a  will roll..    *
22da2 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65  * back independe
22da3 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75 72  ntly if a failur
22da4 65 20 6f 63 63 75 72 73 2e 0d 0a 20 20 20 20 2a  e occurs...    *
22da5 2f 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  /..    for(i=0; 
22da6 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
22da7 0d 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ..      Btree *p
22da8 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
22da9 70 42 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  pBt;..      if( 
22daa 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
22dab 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0d 0a 20  Trans(pBt) ){.. 
22dac 20 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73         char cons
22dad 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 6c 69 74  t *zFile = sqlit
22dae 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61  e3BtreeGetJourna
22daf 6c 6e 61 6d 65 28 70 42 74 29 3b 0d 0a 20 20 20  lname(pBt);..   
22db0 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d       if( zFile==
22db1 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  0 ){..          
22db2 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67  continue;  /* Ig
22db3 6e 6f 72 65 20 54 45 4d 50 20 61 6e 64 20 3a 6d  nore TEMP and :m
22db4 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73  emory: databases
22db5 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a   */..        }..
22db6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
22db7 7a 46 69 6c 65 5b 30 5d 21 3d 30 20 29 3b 0d 0a  zFile[0]!=0 );..
22db8 20 20 20 20 20 20 20 20 69 66 28 20 21 6e 65 65          if( !nee
22db9 64 53 79 6e 63 20 26 26 20 21 73 71 6c 69 74 65  dSync && !sqlite
22dba 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c  3BtreeSyncDisabl
22dbb 65 64 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20  ed(pBt) ){..    
22dbc 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d        needSync =
22dbd 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a   1;..        }..
22dbe 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
22dbf 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73  ite3OsWrite(pMas
22dc0 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c 69  ter, zFile, sqli
22dc1 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c  te3Strlen30(zFil
22dc2 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0d 0a  e)+1, offset);..
22dc3 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b          offset +
22dc4 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
22dc5 30 28 7a 46 69 6c 65 29 2b 31 3b 0d 0a 20 20 20  0(zFile)+1;..   
22dc6 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
22dc7 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
22dc8 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
22dc9 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29  oseFree(pMaster)
22dca 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
22dcb 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
22dcc 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0d  s, zMaster, 0);.
22dcd 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
22dce 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61  e3DbFree(db, zMa
22dcf 73 74 65 72 29 3b 0d 0a 20 20 20 20 20 20 20 20  ster);..        
22dd0 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20    return rc;..  
22dd1 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
22dd2 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
22dd3 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65  * Sync the maste
22dd4 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
22dd5 49 66 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51  If the IOCAP_SEQ
22dd6 55 45 4e 54 49 41 4c 20 64 65 76 69 63 65 0d 0a  UENTIAL device..
22dd7 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73      ** flag is s
22dd8 65 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 72  et this is not r
22dd9 65 71 75 69 72 65 64 2e 0d 0a 20 20 20 20 2a 2f  equired...    */
22dda 0d 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53 79  ..    if( needSy
22ddb 6e 63 20 0d 0a 20 20 20 20 20 26 26 20 30 3d 3d  nc ..     && 0==
22ddc 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65  (sqlite3OsDevice
22ddd 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
22dde 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 45 5f  pMaster)&SQLITE_
22ddf 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
22de0 29 0d 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54  )..     && SQLIT
22de1 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
22de2 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74 65  te3OsSync(pMaste
22de3 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  r, SQLITE_SYNC_N
22de4 4f 52 4d 41 4c 29 29 0d 0a 20 20 20 20 29 7b 0d  ORMAL))..    ){.
22de5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
22de6 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65  CloseFree(pMaste
22de7 72 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  r);..      sqlit
22de8 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
22de9 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0d 0a 20   zMaster, 0);.. 
22dea 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
22deb 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
22dec 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ..      return r
22ded 63 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  c;..    }....   
22dee 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65   /* Sync all the
22def 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76   db files involv
22df0 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  ed in the transa
22df1 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20  ction. The same 
22df2 63 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 73 65 74  call..    ** set
22df3 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  s the master jou
22df4 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20  rnal pointer in 
22df5 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20  each individual 
22df6 6a 6f 75 72 6e 61 6c 2e 20 49 66 0d 0a 20 20 20  journal. If..   
22df7 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   ** an error occ
22df8 75 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74  urs here, do not
22df9 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   delete the mast
22dfa 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
22dfb 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
22dfc 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 6f 63   If the error oc
22dfd 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 20  curs during the 
22dfe 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 0d 0a 20  first call to.. 
22dff 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72     ** sqlite3Btr
22e00 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
22e01 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  (), then there i
22e02 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20  s a chance that 
22e03 74 68 65 0d 0a 20 20 20 20 2a 2a 20 6d 61 73 74  the..    ** mast
22e04 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
22e05 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65 64  will be orphaned
22e06 2e 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74 20  . But we cannot 
22e07 64 65 6c 65 74 65 20 69 74 2c 0d 0a 20 20 20 20  delete it,..    
22e08 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d  ** in case the m
22e09 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
22e0a 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74  le name was writ
22e0b 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
22e0c 72 6e 61 6c 0d 0a 20 20 20 20 2a 2a 20 66 69 6c  rnal..    ** fil
22e0d 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 69  e before the fai
22e0e 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0d 0a  lure occurred...
22e0f 20 20 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28      */..    for(
22e10 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
22e11 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b  OK && i<db->nDb;
22e12 20 69 2b 2b 29 7b 20 0d 0a 20 20 20 20 20 20 42   i++){ ..      B
22e13 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
22e14 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20  aDb[i].pBt;..   
22e15 20 20 20 69 66 28 20 70 42 74 20 29 7b 0d 0a 20     if( pBt ){.. 
22e16 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
22e17 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
22e18 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73  aseOne(pBt, zMas
22e19 74 65 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ter);..      }..
22e1a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
22e1b 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d  e3OsCloseFree(pM
22e1c 61 73 74 65 72 29 3b 0d 0a 20 20 20 20 61 73 73  aster);..    ass
22e1d 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
22e1e 42 55 53 59 20 29 3b 0d 0a 20 20 20 20 69 66 28  BUSY );..    if(
22e1f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
22e20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
22e21 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
22e22 65 72 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  er);..      retu
22e23 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  rn rc;..    }...
22e24 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74  .    /* Delete t
22e25 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
22e26 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d  l file. This com
22e27 6d 69 74 73 20 74 68 65 20 74 72 61 6e 73 61 63  mits the transac
22e28 74 69 6f 6e 2e 20 41 66 74 65 72 0d 0a 20 20 20  tion. After..   
22e29 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 20 74   ** doing this t
22e2a 68 65 20 64 69 72 65 63 74 6f 72 79 20 69 73 20  he directory is 
22e2b 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62 65 66  synced again bef
22e2c 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69 64 75  ore any individu
22e2d 61 6c 0d 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  al..    ** trans
22e2e 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 65  action files are
22e2f 20 64 65 6c 65 74 65 64 2e 0d 0a 20 20 20 20 2a   deleted...    *
22e30 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  /..    rc = sqli
22e31 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
22e32 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0d 0a  , zMaster, 1);..
22e33 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
22e34 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0d  e(db, zMaster);.
22e35 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30  .    zMaster = 0
22e36 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  ;..    if( rc ){
22e37 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ..      return r
22e38 63 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  c;..    }....   
22e39 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e   /* All files an
22e3a 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61  d directories ha
22e3b 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
22e3c 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66  synced, so the f
22e3d 6f 6c 6c 6f 77 69 6e 67 0d 0a 20 20 20 20 2a 2a  ollowing..    **
22e3e 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
22e3f 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
22e40 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20  eTwo() are only 
22e41 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e  closing files an
22e42 64 0d 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69  d..    ** deleti
22e43 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
22e44 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f   journals. If so
22e45 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
22e46 6e 67 20 77 68 69 6c 65 0d 0a 20 20 20 20 2a 2a  ng while..    **
22e47 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69   this is happeni
22e48 6e 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c  ng we don't real
22e49 6c 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74  ly care. The int
22e4a 65 67 72 69 74 79 20 6f 66 20 74 68 65 0d 0a 20  egrity of the.. 
22e4b 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
22e4c 6e 20 69 73 20 61 6c 72 65 61 64 79 20 67 75 61  n is already gua
22e4d 72 61 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d  ranteed, but som
22e4e 65 20 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a  e stray 'cold' j
22e4f 6f 75 72 6e 61 6c 73 0d 0a 20 20 20 20 2a 2a 20  ournals..    ** 
22e50 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 61 72 6f  may be lying aro
22e51 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e 67 20 61  und. Returning a
22e52 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e  n error code won
22e53 27 74 20 68 65 6c 70 20 6d 61 74 74 65 72 73 2e  't help matters.
22e54 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 64 69  ..    */..    di
22e55 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
22e56 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0d 0a 20 20  io_errors();..  
22e57 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
22e58 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20  nignMalloc();.. 
22e59 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
22e5a 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0d 0a 20  ->nDb; i++){ .. 
22e5b 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
22e5c 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
22e5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  ;..      if( pBt
22e5e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
22e5f 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
22e60 68 61 73 65 54 77 6f 28 70 42 74 2c 20 31 29 3b  haseTwo(pBt, 1);
22e61 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
22e62 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  ..    sqlite3End
22e63 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d  BenignMalloc();.
22e64 0a 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75  .    enable_simu
22e65 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
22e66 29 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65  );....    sqlite
22e67 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b  3VtabCommit(db);
22e68 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
22e69 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
22e6a 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 69 73  ..../* ..** This
22e6b 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
22e6c 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33  that the sqlite3
22e6d 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 63  .activeVdbeCnt c
22e6e 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0d 0a 2a  ount variable..*
22e6f 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75  * matches the nu
22e70 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 69  mber of vdbe's i
22e71 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 74  n the list sqlit
22e72 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 72  e3.pVdbe that ar
22e73 65 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  e..** currently 
22e74 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72  active. An asser
22e75 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68  tion fails if th
22e76 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20  e two counts do 
22e77 6e 6f 74 20 6d 61 74 63 68 2e 0d 0a 2a 2a 20 54  not match...** T
22e78 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
22e79 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e  al self-check on
22e7a 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61  ly - it is not a
22e7b 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63  n essential proc
22e7c 65 73 73 69 6e 67 0d 0a 2a 2a 20 73 74 65 70 2e  essing..** step.
22e7d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73  ..**..** This is
22e7e 20 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42   a no-op if NDEB
22e7f 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0d 0a  UG is defined...
22e80 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
22e81 55 47 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  UG..static void 
22e82 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
22e83 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  nt(sqlite3 *db){
22e84 0d 0a 20 20 56 64 62 65 20 2a 70 3b 0d 0a 20 20  ..  Vdbe *p;..  
22e85 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0d 0a 20 20  int cnt = 0;..  
22e86 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0d  int nWrite = 0;.
22e87 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65  .  p = db->pVdbe
22e88 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  ;..  while( p ){
22e89 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67  ..    if( p->mag
22e8a 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
22e8b 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29  UN && p->pc>=0 )
22e8c 7b 0d 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0d  {..      cnt++;.
22e8d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 65  .      if( p->re
22e8e 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69  adOnly==0 ) nWri
22e8f 74 65 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  te++;..    }..  
22e90 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0d    p = p->pNext;.
22e91 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
22e92 63 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56  cnt==db->activeV
22e93 64 62 65 43 6e 74 20 29 3b 0d 0a 20 20 61 73 73  dbeCnt );..  ass
22e94 65 72 74 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d  ert( nWrite==db-
22e95 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b  >writeVdbeCnt );
22e96 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 64 65 66  ..}..#else..#def
22e97 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76 65 56  ine checkActiveV
22e98 64 62 65 43 6e 74 28 78 29 0d 0a 23 65 6e 64 69  dbeCnt(x)..#endi
22e99 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 6f 72 20  f..../*..** For 
22e9a 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74  every Btree that
22e9b 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e   in database con
22e9c 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68  nection db which
22e9d 20 0d 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d   ..** has been m
22e9e 6f 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20  odified, "trip" 
22e9f 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61  or invalidate ea
22ea0 63 68 20 63 75 72 73 6f 72 20 69 6e 0d 0a 2a 2a  ch cursor in..**
22ea1 20 74 68 61 74 20 42 74 72 65 65 20 6d 69 67 68   that Btree migh
22ea2 74 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69  t have been modi
22ea3 66 69 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  fied so that the
22ea4 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 63 61 6e 20   cursor..** can 
22ea5 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 67  never be used ag
22ea6 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 70 65  ain.  This happe
22ea7 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61  ns when a rollba
22ea8 63 6b 0d 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20  ck..*** occurs. 
22ea9 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 70   We have to trip
22eaa 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63   all the other c
22eab 75 72 73 6f 72 73 2c 20 65 76 65 6e 0d 0a 2a 2a  ursors, even..**
22eac 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68   cursor from oth
22ead 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72  er VMs in differ
22eae 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ent database con
22eaf 6e 65 63 74 69 6f 6e 73 2c 0d 0a 2a 2a 20 73 6f  nections,..** so
22eb0 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68   that none of th
22eb1 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68  em try to use th
22eb2 65 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20  e data at which 
22eb3 74 68 65 79 0d 0a 2a 2a 20 77 65 72 65 20 70 6f  they..** were po
22eb4 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68  inting and which
22eb5 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65   now may have be
22eb6 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0d 0a  en changed due..
22eb7 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61  ** to the rollba
22eb8 63 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 6d 65  ck...**..** Reme
22eb9 6d 62 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c  mber that a roll
22eba 62 61 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20  back can delete 
22ebb 74 61 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20  tables complete 
22ebc 61 6e 64 0d 0a 2a 2a 20 72 65 6f 72 64 65 72 20  and..** reorder 
22ebd 72 6f 6f 74 70 61 67 65 73 2e 20 20 53 6f 20 69  rootpages.  So i
22ebe 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69  t is not suffici
22ebf 65 6e 74 20 6a 75 73 74 20 74 6f 20 73 61 76 65  ent just to save
22ec0 0d 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 6f  ..** the state o
22ec1 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 57  f the cursor.  W
22ec2 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61 6c 69  e have to invali
22ec3 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 0d  date the cursor.
22ec4 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 69  .** so that it i
22ec5 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67 61  s never used aga
22ec6 69 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  in...*/..static 
22ec7 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43  void invalidateC
22ec8 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64  ursorsOnModified
22ec9 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a  Btrees(sqlite3 *
22eca 64 62 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  db){..  int i;..
22ecb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
22ecc 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
22ecd 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e   Btree *p = db->
22ece 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20  aDb[i].pBt;..   
22ecf 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 74 65   if( p && sqlite
22ed0 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
22ed1 70 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  p) ){..      sql
22ed2 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
22ed3 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 54  Cursors(p, SQLIT
22ed4 45 5f 41 42 4f 52 54 29 3b 0d 0a 20 20 20 20 7d  E_ABORT);..    }
22ed5 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
22ed6 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 20 70  ** If the Vdbe p
22ed7 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
22ed8 73 74 20 61 72 67 75 6d 65 6e 74 20 6f 70 65 6e  st argument open
22ed9 65 64 20 61 20 73 74 61 74 65 6d 65 6e 74 2d 74  ed a statement-t
22eda 72 61 6e 73 61 63 74 69 6f 6e 2c 0d 0a 2a 2a 20  ransaction,..** 
22edb 63 6c 6f 73 65 20 69 74 20 6e 6f 77 2e 20 41 72  close it now. Ar
22edc 67 75 6d 65 6e 74 20 65 4f 70 20 6d 75 73 74 20  gument eOp must 
22edd 62 65 20 65 69 74 68 65 72 20 53 41 56 45 50 4f  be either SAVEPO
22ede 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 0d  INT_ROLLBACK or.
22edf 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  .** SAVEPOINT_RE
22ee0 4c 45 41 53 45 2e 20 49 66 20 69 74 20 69 73 20  LEASE. If it is 
22ee1 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
22ee2 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 73 74 61  CK, then the sta
22ee3 74 65 6d 65 6e 74 0d 0a 2a 2a 20 74 72 61 6e 73  tement..** trans
22ee4 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64  action is rolled
22ee5 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 69 73   back. If eOp is
22ee6 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
22ee7 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0d 0a 2a  SE, then the ..*
22ee8 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  * statement tran
22ee9 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 74  saction is commt
22eea 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ted...**..** If 
22eeb 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
22eec 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f  rs, an SQLITE_IO
22eed 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ERR_XXX error co
22eee 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
22eef 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53  ..** Otherwise S
22ef0 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 53  QLITE_OK...*/..S
22ef1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22ef2 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f  t sqlite3VdbeClo
22ef3 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65  seStatement(Vdbe
22ef4 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0d 0a   *p, int eOp){..
22ef5 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74    sqlite3 *const
22ef6 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20   db = p->db;..  
22ef7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
22ef8 4f 4b 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 70  OK;....  /* If p
22ef9 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20  ->iStatement is 
22efa 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
22efb 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 62  o, then this Vdb
22efc 65 20 6f 70 65 6e 65 64 20 61 20 0d 0a 20 20 2a  e opened a ..  *
22efd 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  * statement tran
22efe 73 61 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f  saction that sho
22eff 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65  uld be closed he
22f00 72 65 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63  re. The only exc
22f01 65 70 74 69 6f 6e 0d 0a 20 20 2a 2a 20 69 73 20  eption..  ** is 
22f02 74 68 61 74 20 61 6e 20 49 4f 20 65 72 72 6f 72  that an IO error
22f03 20 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 65   may have occure
22f04 64 2c 20 63 61 75 73 69 6e 67 20 61 6e 20 65 6d  d, causing an em
22f05 65 72 67 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b  ergency rollback
22f06 2e 0d 0a 20 20 2a 2a 20 49 6e 20 74 68 69 73 20  ...  ** In this 
22f07 63 61 73 65 20 28 64 62 2d 3e 6e 53 74 61 74 65  case (db->nState
22f08 6d 65 6e 74 3d 3d 30 29 2c 20 61 6e 64 20 74 68  ment==0), and th
22f09 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74  ere is nothing t
22f0a 6f 20 64 6f 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  o do...  */..  i
22f0b 66 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e  f( db->nStatemen
22f0c 74 20 26 26 20 70 2d 3e 69 53 74 61 74 65 6d 65  t && p->iStateme
22f0d 6e 74 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69  nt ){..    int i
22f0e 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  ;..    const int
22f0f 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d   iSavepoint = p-
22f10 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0d 0a  >iStatement-1;..
22f11 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4f  ..    assert( eO
22f12 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
22f13 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41  LBACK || eOp==SA
22f14 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29  VEPOINT_RELEASE)
22f15 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ;..    assert( d
22f16 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20  b->nStatement>0 
22f17 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
22f18 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 28  p->iStatement==(
22f19 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64  db->nStatement+d
22f1a 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 29  b->nSavepoint) )
22f1b 3b 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30  ;....    for(i=0
22f1c 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
22f1d 29 7b 20 0d 0a 20 20 20 20 20 20 69 6e 74 20 72  ){ ..      int r
22f1e 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  c2 = SQLITE_OK;.
22f1f 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
22f20 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
22f21 42 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  Bt;..      if( p
22f22 42 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  Bt ){..        i
22f23 66 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e  f( eOp==SAVEPOIN
22f24 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b 0d 0a 20  T_ROLLBACK ){.. 
22f25 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73           rc2 = s
22f26 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70  qlite3BtreeSavep
22f27 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f  oint(pBt, SAVEPO
22f28 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53  INT_ROLLBACK, iS
22f29 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 20 20  avepoint);..    
22f2a 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
22f2b 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f  f( rc2==SQLITE_O
22f2c 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  K ){..          
22f2d 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72  rc2 = sqlite3Btr
22f2e 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c  eeSavepoint(pBt,
22f2f 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
22f30 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b  SE, iSavepoint);
22f31 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
22f32 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
22f33 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
22f34 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0d 0a       rc = rc2;..
22f35 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
22f36 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64   }..    }..    d
22f37 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b  b->nStatement--;
22f38 0d 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d  ..    p->iStatem
22f39 65 6e 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20  ent = 0;....    
22f3a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
22f3b 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  K ){..      if( 
22f3c 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  eOp==SAVEPOINT_R
22f3d 4f 4c 4c 42 41 43 4b 20 29 7b 0d 0a 20 20 20 20  OLLBACK ){..    
22f3e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
22f3f 56 74 61 62 53 61 76 65 70 6f 69 6e 74 28 64 62  VtabSavepoint(db
22f40 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  , SAVEPOINT_ROLL
22f41 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74  BACK, iSavepoint
22f42 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
22f43 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
22f44 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
22f45 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61   rc = sqlite3Vta
22f46 62 53 61 76 65 70 6f 69 6e 74 28 64 62 2c 20 53  bSavepoint(db, S
22f47 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
22f48 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0d 0a  , iSavepoint);..
22f49 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
22f4a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
22f4b 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
22f4c 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 72  ction is being r
22f4d 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 61 6c 73 6f  olled back, also
22f4e 20 72 65 73 74 6f 72 65 20 74 68 65 20 0d 0a 20   restore the .. 
22f4f 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 68     ** database h
22f50 61 6e 64 6c 65 73 20 64 65 66 65 72 72 65 64 20  andles deferred 
22f51 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
22f52 65 72 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  er to the value 
22f53 69 74 20 68 61 64 20 77 68 65 6e 20 0d 0a 20 20  it had when ..  
22f54 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
22f55 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
22f56 61 73 20 6f 70 65 6e 65 64 2e 20 20 2a 2f 0d 0a  as opened.  */..
22f57 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56      if( eOp==SAV
22f58 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
22f59 29 7b 0d 0a 20 20 20 20 20 20 64 62 2d 3e 6e 44  ){..      db->nD
22f5a 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 70 2d  eferredCons = p-
22f5b 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 0d 0a  >nStmtDefCons;..
22f5c 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
22f5d 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
22f5e 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
22f5f 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
22f60 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  en a transaction
22f61 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 64   opened by the d
22f62 61 74 61 62 61 73 65 20 0d 0a 2a 2a 20 68 61 6e  atabase ..** han
22f63 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  dle associated w
22f64 69 74 68 20 74 68 65 20 56 4d 20 70 61 73 73 65  ith the VM passe
22f65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
22f66 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20   is about to be 
22f67 0d 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20  ..** committed. 
22f68 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74  If there are out
22f69 73 74 61 6e 64 69 6e 67 20 64 65 66 65 72 72 65  standing deferre
22f6a 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
22f6b 6e 73 74 72 61 69 6e 74 0d 0a 2a 2a 20 76 69 6f  nstraint..** vio
22f6c 6c 61 74 69 6f 6e 73 2c 20 72 65 74 75 72 6e 20  lations, return 
22f6d 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 4f 74  SQLITE_ERROR. Ot
22f6e 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
22f6f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  OK...**..** If t
22f70 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e  here are outstan
22f71 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74 69 6f  ding FK violatio
22f72 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ns and this func
22f73 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0d 0a 2a  tion returns ..*
22f74 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  * SQLITE_ERROR, 
22f75 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f  set the result o
22f76 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c 49  f the VM to SQLI
22f77 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e  TE_CONSTRAINT an
22f78 64 20 77 72 69 74 65 0d 0a 2a 2a 20 61 6e 20 65  d write..** an e
22f79 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
22f7a 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20  it. Then return 
22f7b 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0d 0a 2a  SQLITE_ERROR...*
22f7c 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
22f7d 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
22f7e 45 59 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EY..SQLITE_PRIVA
22f7f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
22f80 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a  beCheckFk(Vdbe *
22f81 70 2c 20 69 6e 74 20 64 65 66 65 72 72 65 64 29  p, int deferred)
22f82 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
22f83 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20 69 66 28   = p->db;..  if(
22f84 20 28 64 65 66 65 72 72 65 64 20 26 26 20 64 62   (deferred && db
22f85 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e  ->nDeferredCons>
22f86 30 29 20 7c 7c 20 28 21 64 65 66 65 72 72 65 64  0) || (!deferred
22f87 20 26 26 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72   && p->nFkConstr
22f88 61 69 6e 74 3e 30 29 20 29 7b 0d 0a 20 20 20 20  aint>0) ){..    
22f89 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43  p->rc = SQLITE_C
22f8a 4f 4e 53 54 52 41 49 4e 54 3b 0d 0a 20 20 20 20  ONSTRAINT;..    
22f8b 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d  p->errorAction =
22f8c 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20   OE_Abort;..    
22f8d 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
22f8e 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
22f8f 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  , "foreign key c
22f90 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
22f91 22 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  ");..    return 
22f92 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
22f93 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
22f94 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e 64  ITE_OK;..}..#end
22f95 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  if..../*..** Thi
22f96 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
22f97 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56  led the when a V
22f98 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c  DBE tries to hal
22f99 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0d  t.  If the VDBE.
22f9a 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61  .** has made cha
22f9b 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61  nges and is in a
22f9c 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20  utocommit mode, 
22f9d 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73  then commit thos
22f9e 65 0d 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  e..** changes.  
22f9f 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73  If a rollback is
22fa0 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f   needed, then do
22fa1 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0d 0a   the rollback...
22fa2 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
22fa3 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
22fa4 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20  way to move the 
22fa5 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72  state of a VM fr
22fa6 6f 6d 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41  om..** SQLITE_MA
22fa7 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54  GIC_RUN to SQLIT
22fa8 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49  E_MAGIC_HALT.  I
22fa9 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
22faa 0d 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f  ..** call this o
22fab 6e 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69  n a VM that is i
22fac 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47  n the SQLITE_MAG
22fad 49 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0d 0a  IC_HALT state...
22fae 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e  **..** Return an
22faf 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49 66   error code.  If
22fb0 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c   the commit coul
22fb1 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62  d not complete b
22fb2 65 63 61 75 73 65 20 6f 66 0d 0a 2a 2a 20 6c 6f  ecause of..** lo
22fb3 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72  ck contention, r
22fb4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
22fb5 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 42 55  Y.  If SQLITE_BU
22fb6 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  SY is returned, 
22fb7 69 74 0d 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 65  it..** means the
22fb8 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 68   close did not h
22fb9 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 20  appen and needs 
22fba 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e 0d  to be repeated..
22fbb 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
22fbc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
22fbd 64 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29  dbeHalt(Vdbe *p)
22fbe 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  {..  int rc;    
22fbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22fc0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
22fc1 73 74 6f 72 65 20 74 72 61 6e 73 69 65 6e 74 20  store transient 
22fc2 72 65 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0d  return codes */.
22fc3 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22fc4 20 70 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 2f 2a 20   p->db;....  /* 
22fc5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
22fc6 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63  ntains the logic
22fc7 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
22fc8 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   if a statement 
22fc9 6f 72 0d 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  or..  ** transac
22fca 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d  tion will be com
22fcb 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
22fcc 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c   back as a resul
22fcd 74 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 65  t of the..  ** e
22fce 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73  xecution of this
22fcf 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
22fd0 2e 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49  . ..  **..  ** I
22fd1 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c  f any of the fol
22fd2 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63  lowing errors oc
22fd3 63 75 72 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  cur:..  **..  **
22fd4 20 20 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45       SQLITE_NOME
22fd5 4d 0d 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49  M..  **     SQLI
22fd6 54 45 5f 49 4f 45 52 52 0d 0a 20 20 2a 2a 20 20  TE_IOERR..  **  
22fd7 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0d 0a     SQLITE_FULL..
22fd8 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f    **     SQLITE_
22fd9 49 4e 54 45 52 52 55 50 54 0d 0a 20 20 2a 2a 0d  INTERRUPT..  **.
22fda 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69  .  ** Then the i
22fdb 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69  nternal cache mi
22fdc 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65  ght have been le
22fdd 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
22fde 73 74 65 6e 74 0d 0a 20 20 2a 2a 20 73 74 61 74  stent..  ** stat
22fdf 65 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72  e.  We need to r
22fe0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74  ollback the stat
22fe1 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
22fe2 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 0d 0a  n, if there is..
22fe3 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65    ** one, or the
22fe4 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61   complete transa
22fe5 63 74 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69  ction if there i
22fe6 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74  s no statement t
22fe7 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 20 20 2a  ransaction...  *
22fe8 2f 0d 0a 0d 0a 20 20 69 66 28 20 70 2d 3e 64 62  /....  if( p->db
22fe9 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
22fea 7b 0d 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  {..    p->rc = S
22feb 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
22fec 7d 0d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 6e 63  }..  if( p->aOnc
22fed 65 46 6c 61 67 20 29 20 6d 65 6d 73 65 74 28 70  eFlag ) memset(p
22fee 2d 3e 61 4f 6e 63 65 46 6c 61 67 2c 20 30 2c 20  ->aOnceFlag, 0, 
22fef 70 2d 3e 6e 4f 6e 63 65 46 6c 61 67 29 3b 0d 0a  p->nOnceFlag);..
22ff0 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72    closeAllCursor
22ff1 73 28 70 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  s(p);..  if( p->
22ff2 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
22ff3 43 5f 52 55 4e 20 29 7b 0d 0a 20 20 20 20 72 65  C_RUN ){..    re
22ff4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
22ff5 0a 20 20 7d 0d 0a 20 20 63 68 65 63 6b 41 63 74  .  }..  checkAct
22ff6 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0d  iveVdbeCnt(db);.
22ff7 0a 0d 0a 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69  ...  /* No commi
22ff8 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65  t or rollback ne
22ff9 65 64 65 64 20 69 66 20 74 68 65 20 70 72 6f 67  eded if the prog
22ffa 72 61 6d 20 6e 65 76 65 72 20 73 74 61 72 74 65  ram never starte
22ffb 64 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 70  d */..  if( p->p
22ffc 63 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74  c>=0 ){..    int
22ffd 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 69 6d 61   mrc;   /* Prima
22ffe 72 79 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72  ry error code fr
22fff 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0d 0a 20 20 20  om p->rc */..   
23000 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e 74 4f   int eStatementO
23001 70 20 3d 20 30 3b 0d 0a 20 20 20 20 69 6e 74 20  p = 0;..    int 
23002 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20  isSpecialError; 
23003 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
23004 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 27  t to true if a '
23005 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20 2a  special' error *
23006 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b  /....    /* Lock
23007 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64   all btrees used
23008 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
23009 74 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65  t */..    sqlite
2300a 33 56 64 62 65 45 6e 74 65 72 28 70 29 3b 0d 0a  3VdbeEnter(p);..
2300b 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66  ..    /* Check f
2300c 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70  or one of the sp
2300d 65 63 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0d  ecial errors */.
2300e 0a 20 20 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63  .    mrc = p->rc
2300f 20 26 20 30 78 66 66 3b 0d 0a 20 20 20 20 61 73   & 0xff;..    as
23010 73 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c  sert( p->rc!=SQL
23011 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
23012 44 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 65 72  D );  /* This er
23013 72 6f 72 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78  ror no longer ex
23014 69 73 74 73 20 2a 2f 0d 0a 20 20 20 20 69 73 53  ists */..    isS
23015 70 65 63 69 61 6c 45 72 72 6f 72 20 3d 20 6d 72  pecialError = mr
23016 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
23017 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49  || mrc==SQLITE_I
23018 4f 45 52 52 0d 0a 20 20 20 20 20 20 20 20 20 20  OERR..          
23019 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6d 72             || mr
2301a 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  c==SQLITE_INTERR
2301b 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49  UPT || mrc==SQLI
2301c 54 45 5f 46 55 4c 4c 3b 0d 0a 20 20 20 20 69 66  TE_FULL;..    if
2301d 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72  ( isSpecialError
2301e 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66   ){..      /* If
2301f 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72   the query was r
23020 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 74 68 65  ead-only and the
23021 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53   error code is S
23022 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 2c  QLITE_INTERRUPT,
23023 20 0d 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 20 72   ..      ** no r
23024 6f 6c 6c 62 61 63 6b 20 69 73 20 6e 65 63 65 73  ollback is neces
23025 73 61 72 79 2e 20 4f 74 68 65 72 77 69 73 65 2c  sary. Otherwise,
23026 20 61 74 20 6c 65 61 73 74 20 61 20 73 61 76 65   at least a save
23027 70 6f 69 6e 74 20 0d 0a 20 20 20 20 20 20 2a 2a  point ..      **
23028 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
23029 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t be rolled back
2302a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
2302b 64 61 74 61 62 61 73 65 20 74 6f 20 61 20 0d 0a  database to a ..
2302c 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74        ** consist
2302d 65 6e 74 20 73 74 61 74 65 2e 0d 0a 20 20 20 20  ent state...    
2302e 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 45    **..      ** E
2302f 76 65 6e 20 69 66 20 74 68 65 20 73 74 61 74 65  ven if the state
23030 6d 65 6e 74 20 69 73 20 72 65 61 64 2d 6f 6e 6c  ment is read-onl
23031 79 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  y, it is importa
23032 6e 74 20 74 6f 20 70 65 72 66 6f 72 6d 0d 0a 20  nt to perform.. 
23033 20 20 20 20 20 2a 2a 20 61 20 73 74 61 74 65 6d       ** a statem
23034 65 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74 69  ent or transacti
23035 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72  on rollback oper
23036 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 65 72  ation. If the er
23037 72 6f 72 20 0d 0a 20 20 20 20 20 20 2a 2a 20 6f  ror ..      ** o
23038 63 63 75 72 65 64 20 77 68 69 6c 65 20 77 72 69  ccured while wri
23039 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72  ting to the jour
2303a 6e 61 6c 2c 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  nal, sub-journal
2303b 20 6f 72 20 64 61 74 61 62 61 73 65 0d 0a 20 20   or database..  
2303c 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 73 20 70      ** file as p
2303d 61 72 74 20 6f 66 20 61 6e 20 65 66 66 6f 72 74  art of an effort
2303e 20 74 6f 20 66 72 65 65 20 75 70 20 63 61 63 68   to free up cach
2303f 65 20 73 70 61 63 65 20 28 73 65 65 20 66 75 6e  e space (see fun
23040 63 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  ction..      ** 
23041 70 61 67 65 72 53 74 72 65 73 73 28 29 20 69 6e  pagerStress() in
23042 20 70 61 67 65 72 2e 63 29 2c 20 74 68 65 20 72   pager.c), the r
23043 6f 6c 6c 62 61 63 6b 20 69 73 20 72 65 71 75 69  ollback is requi
23044 72 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 0d  red to restore .
23045 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 61  .      ** the pa
23046 67 65 72 20 74 6f 20 61 20 63 6f 6e 73 69 73 74  ger to a consist
23047 65 6e 74 20 73 74 61 74 65 2e 0d 0a 20 20 20 20  ent state...    
23048 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20    */..      if( 
23049 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c 20  !p->readOnly || 
2304a 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45  mrc!=SQLITE_INTE
2304b 52 52 55 50 54 20 29 7b 0d 0a 20 20 20 20 20 20  RRUPT ){..      
2304c 20 20 69 66 28 20 28 6d 72 63 3d 3d 53 51 4c 49    if( (mrc==SQLI
2304d 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d  TE_NOMEM || mrc=
2304e 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 20 26 26  =SQLITE_FULL) &&
2304f 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
23050 6e 61 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  nal ){..        
23051 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d    eStatementOp =
23052 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
23053 41 43 4b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  ACK;..        }e
23054 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
23055 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63 65 64  /* We are forced
23056 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68   to roll back th
23057 65 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  e active transac
23058 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 6f 69  tion. Before doi
23059 6e 67 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ng..          **
2305a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 6f   so, abort any o
2305b 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20  ther statements 
2305c 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72 72  this handle curr
2305d 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76 65  ently has active
2305e 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d  ...          */.
2305f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c  .          inval
23060 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f  idateCursorsOnMo
23061 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29  difiedBtrees(db)
23062 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
23063 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
23064 64 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  db);..          
23065 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
23066 70 6f 69 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20  points(db);..   
23067 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
23068 6f 6d 6d 69 74 20 3d 20 31 3b 0d 0a 20 20 20 20  ommit = 1;..    
23069 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
2306a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
2306b 43 68 65 63 6b 20 66 6f 72 20 69 6d 6d 65 64 69  Check for immedi
2306c 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ate foreign key 
2306d 76 69 6f 6c 61 74 69 6f 6e 73 2e 20 2a 2f 0d 0a  violations. */..
2306e 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53      if( p->rc==S
2306f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
23070 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
23071 65 63 6b 46 6b 28 70 2c 20 30 29 3b 0d 0a 20 20  eckFk(p, 0);..  
23072 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20    }..  ..    /* 
23073 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d  If the auto-comm
23074 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 61  it flag is set a
23075 6e 64 20 74 68 69 73 20 69 73 20 74 68 65 20 6f  nd this is the o
23076 6e 6c 79 20 61 63 74 69 76 65 20 77 72 69 74 65  nly active write
23077 72 20 0d 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74  r ..    ** VM, t
23078 68 65 6e 20 77 65 20 64 6f 20 65 69 74 68 65 72  hen we do either
23079 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c   a commit or rol
2307a 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 75 72  lback of the cur
2307b 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
2307c 2e 20 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  . ..    **..    
2307d 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62 6c  ** Note: This bl
2307e 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69 66  ock also runs if
2307f 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
23080 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64 6c  ial errors handl
23081 65 64 20 0d 0a 20 20 20 20 2a 2a 20 61 62 6f 76  ed ..    ** abov
23082 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  e has occurred. 
23083 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
23084 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e  ( !sqlite3VtabIn
23085 53 79 6e 63 28 64 62 29 20 0d 0a 20 20 20 20 20  Sync(db) ..     
23086 26 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  && db->autoCommi
23087 74 20 0d 0a 20 20 20 20 20 26 26 20 64 62 2d 3e  t ..     && db->
23088 77 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70  writeVdbeCnt==(p
23089 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0d  ->readOnly==0) .
2308a 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 69  .    ){..      i
2308b 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  f( p->rc==SQLITE
2308c 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72  _OK || (p->error
2308d 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20  Action==OE_Fail 
2308e 26 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72  && !isSpecialErr
2308f 6f 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  or) ){..        
23090 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
23091 43 68 65 63 6b 46 6b 28 70 2c 20 31 29 3b 0d 0a  CheckFk(p, 1);..
23092 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
23093 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
23094 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45          if( NEVE
23095 52 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29  R(p->readOnly) )
23096 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  {..            s
23097 71 6c 69 74 65 33 56 64 62 65 4c 65 61 76 65 28  qlite3VdbeLeave(
23098 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  p);..           
23099 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2309a 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 20  RROR;..         
2309b 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63   }..          rc
2309c 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
2309d 41 49 4e 54 3b 0d 0a 20 20 20 20 20 20 20 20 7d  AINT;..        }
2309e 65 6c 73 65 7b 20 0d 0a 20 20 20 20 20 20 20 20  else{ ..        
2309f 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f    /* The auto-co
230a0 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 75  mmit flag is tru
230a1 65 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  e, the vdbe prog
230a2 72 61 6d 20 77 61 73 20 73 75 63 63 65 73 73 66  ram was successf
230a3 75 6c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  ul ..          *
230a4 2a 20 6f 72 20 68 69 74 20 61 6e 20 27 4f 52 20  * or hit an 'OR 
230a5 46 41 49 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74  FAIL' constraint
230a6 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 6e   and there are n
230a7 6f 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 69  o deferred forei
230a8 67 6e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  gn..          **
230a9 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
230aa 20 74 6f 20 68 6f 6c 64 20 75 70 20 74 68 65 20   to hold up the 
230ab 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69  transaction. Thi
230ac 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69 74  s means a commit
230ad 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   ..          ** 
230ae 69 73 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0d  is required. */.
230af 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
230b0 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 70  vdbeCommit(db, p
230b1 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
230b2 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
230b3 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 2d  QLITE_BUSY && p-
230b4 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0d 0a 20 20  >readOnly ){..  
230b5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
230b6 64 62 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20  dbeLeave(p);..  
230b7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
230b8 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20 20  QLITE_BUSY;..   
230b9 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72       }else if( r
230ba 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
230bb 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63  .          p->rc
230bc 20 3d 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20   = rc;..        
230bd 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
230be 6b 41 6c 6c 28 64 62 29 3b 0d 0a 20 20 20 20 20  kAll(db);..     
230bf 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
230c0 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72       db->nDeferr
230c1 65 64 43 6f 6e 73 20 3d 20 30 3b 0d 0a 20 20 20  edCons = 0;..   
230c2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
230c3 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e  mmitInternalChan
230c4 67 65 73 28 64 62 29 3b 0d 0a 20 20 20 20 20 20  ges(db);..      
230c5 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65    }..      }else
230c6 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
230c7 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
230c8 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
230c9 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e     db->nStatemen
230ca 74 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73  t = 0;..    }els
230cb 65 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74  e if( eStatement
230cc 4f 70 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  Op==0 ){..      
230cd 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  if( p->rc==SQLIT
230ce 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72  E_OK || p->error
230cf 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20  Action==OE_Fail 
230d0 29 7b 0d 0a 20 20 20 20 20 20 20 20 65 53 74 61  ){..        eSta
230d1 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50  tementOp = SAVEP
230d2 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3b 0d 0a 20  OINT_RELEASE;.. 
230d3 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
230d4 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f  ->errorAction==O
230d5 45 5f 41 62 6f 72 74 20 29 7b 0d 0a 20 20 20 20  E_Abort ){..    
230d6 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70      eStatementOp
230d7 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c   = SAVEPOINT_ROL
230d8 4c 42 41 43 4b 3b 0d 0a 20 20 20 20 20 20 7d 65  LBACK;..      }e
230d9 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  lse{..        in
230da 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f  validateCursorsO
230db 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28  nModifiedBtrees(
230dc 64 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  db);..        sq
230dd 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
230de 28 64 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  (db);..        s
230df 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
230e0 6f 69 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20 20  oints(db);..    
230e1 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
230e2 69 74 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d  it = 1;..      }
230e3 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20  ..    }..  ..   
230e4 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e   /* If eStatemen
230e5 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  tOp is non-zero,
230e6 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   then a statemen
230e7 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65  t transaction ne
230e8 65 64 73 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 62  eds to..    ** b
230e9 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  e committed or r
230ea 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c  olled back. Call
230eb 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
230ec 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0d  eStatement() to.
230ed 0a 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49  .    ** do so. I
230ee 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  f this operation
230ef 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
230f0 72 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  r, and the curre
230f1 6e 74 20 73 74 61 74 65 6d 65 6e 74 0d 0a 20 20  nt statement..  
230f2 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20    ** error code 
230f3 69 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20  is SQLITE_OK or 
230f4 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
230f5 54 2c 20 74 68 65 6e 20 70 72 6f 6d 6f 74 65 20  T, then promote 
230f6 74 68 65 0d 0a 20 20 20 20 2a 2a 20 63 75 72 72  the..    ** curr
230f7 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 65 72  ent statement er
230f8 72 6f 72 20 63 6f 64 65 2e 0d 0a 20 20 20 20 2a  ror code...    *
230f9 2f 0d 0a 20 20 20 20 69 66 28 20 65 53 74 61 74  /..    if( eStat
230fa 65 6d 65 6e 74 4f 70 20 29 7b 0d 0a 20 20 20 20  ementOp ){..    
230fb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
230fc 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
230fd 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  (p, eStatementOp
230fe 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
230ff 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28   ){..        if(
23100 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
23101 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  K || p->rc==SQLI
23102 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b  TE_CONSTRAINT ){
23103 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  ..          p->r
23104 63 20 3d 20 72 63 3b 0d 0a 20 20 20 20 20 20 20  c = rc;..       
23105 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23106 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
23107 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ;..          p->
23108 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d 0a 20 20  zErrMsg = 0;..  
23109 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
2310a 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f   invalidateCurso
2310b 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65  rsOnModifiedBtre
2310c 65 73 28 64 62 29 3b 0d 0a 20 20 20 20 20 20 20  es(db);..       
2310d 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
2310e 41 6c 6c 28 64 62 29 3b 0d 0a 20 20 20 20 20 20  All(db);..      
2310f 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
23110 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0d 0a 20  vepoints(db);.. 
23111 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
23112 6f 6d 6d 69 74 20 3d 20 31 3b 0d 0a 20 20 20 20  ommit = 1;..    
23113 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a    }..    }..  ..
23114 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77      /* If this w
23115 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50  as an INSERT, UP
23116 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61  DATE or DELETE a
23117 6e 64 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20  nd no statement 
23118 74 72 61 6e 73 61 63 74 69 6f 6e 0d 0a 20 20 20  transaction..   
23119 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 6f 6c   ** has been rol
2311a 6c 65 64 20 62 61 63 6b 2c 20 75 70 64 61 74 65  led back, update
2311b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
2311c 6e 6e 65 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d  nnection change-
2311d 63 6f 75 6e 74 65 72 2e 20 0d 0a 20 20 20 20 2a  counter. ..    *
2311e 2f 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68  /..    if( p->ch
2311f 61 6e 67 65 43 6e 74 4f 6e 20 29 7b 0d 0a 20 20  angeCntOn ){..  
23120 20 20 20 20 69 66 28 20 65 53 74 61 74 65 6d 65      if( eStateme
23121 6e 74 4f 70 21 3d 53 41 56 45 50 4f 49 4e 54 5f  ntOp!=SAVEPOINT_
23122 52 4f 4c 4c 42 41 43 4b 20 29 7b 0d 0a 20 20 20  ROLLBACK ){..   
23123 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23124 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70  SetChanges(db, p
23125 2d 3e 6e 43 68 61 6e 67 65 29 3b 0d 0a 20 20 20  ->nChange);..   
23126 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
23127 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
23128 74 43 68 61 6e 67 65 73 28 64 62 2c 20 30 29 3b  tChanges(db, 0);
23129 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
2312a 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b   p->nChange = 0;
2312b 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20  ..    }..  ..   
2312c 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20   /* Rollback or 
2312d 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d  commit any schem
2312e 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f  a changes that o
2312f 63 63 75 72 72 65 64 2e 20 2a 2f 0d 0a 20 20 20  ccurred. */..   
23130 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49   if( p->rc!=SQLI
23131 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61  TE_OK && db->fla
23132 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
23133 43 68 61 6e 67 65 73 20 29 7b 0d 0a 20 20 20 20  Changes ){..    
23134 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
23135 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
23136 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20 64 62 2d   -1);..      db-
23137 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c  >flags = (db->fl
23138 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74  ags | SQLITE_Int
23139 65 72 6e 43 68 61 6e 67 65 73 29 3b 0d 0a 20 20  ernChanges);..  
2313a 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65    }....    /* Re
2313b 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 73 20  lease the locks 
2313c 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  */..    sqlite3V
2313d 64 62 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20 20  dbeLeave(p);..  
2313e 7d 0d 0a 0d 0a 20 20 2f 2a 20 57 65 20 68 61 76  }....  /* We hav
2313f 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68  e successfully h
23140 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64  alted and closed
23141 20 74 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64   the VM.  Record
23142 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0d 0a   this fact. */..
23143 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29    if( p->pc>=0 )
23144 7b 0d 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76  {..    db->activ
23145 65 56 64 62 65 43 6e 74 2d 2d 3b 0d 0a 20 20 20  eVdbeCnt--;..   
23146 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c   if( !p->readOnl
23147 79 20 29 7b 0d 0a 20 20 20 20 20 20 64 62 2d 3e  y ){..      db->
23148 77 72 69 74 65 56 64 62 65 43 6e 74 2d 2d 3b 0d  writeVdbeCnt--;.
23149 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65  .    }..    asse
2314a 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
2314b 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 65  beCnt>=db->write
2314c 56 64 62 65 43 6e 74 20 29 3b 0d 0a 20 20 7d 0d  VdbeCnt );..  }.
2314d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
2314e 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0d 0a  BE_MAGIC_HALT;..
2314f 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62    checkActiveVdb
23150 65 43 6e 74 28 64 62 29 3b 0d 0a 20 20 69 66 28  eCnt(db);..  if(
23151 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
23152 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 70 2d 3e  iled ){..    p->
23153 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
23154 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  M;..  }....  /* 
23155 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d  If the auto-comm
23156 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  it flag is set t
23157 6f 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79  o true, then any
23158 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 72 65   locks that were
23159 20 68 65 6c 64 0d 0a 20 20 2a 2a 20 62 79 20 63   held..  ** by c
2315a 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76  onnection db hav
2315b 65 20 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 61  e now been relea
2315c 73 65 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65  sed. Call sqlite
2315d 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63  3ConnectionUnloc
2315e 6b 65 64 28 29 20 0d 0a 20 20 2a 2a 20 74 6f 20  ked() ..  ** to 
2315f 69 6e 76 6f 6b 65 20 61 6e 79 20 72 65 71 75 69  invoke any requi
23160 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  red unlock-notif
23161 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0d 0a 20 20  y callbacks...  
23162 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e 61 75  */..  if( db->au
23163 74 6f 43 6f 6d 6d 69 74 20 29 7b 0d 0a 20 20 20  toCommit ){..   
23164 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
23165 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 29 3b 0d  onUnlocked(db);.
23166 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74  .  }....  assert
23167 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
23168 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74  Cnt>0 || db->aut
23169 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
2316a 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->nStatement==0 
2316b 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  );..  return (p-
2316c 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  >rc==SQLITE_BUSY
2316d 20 3f 20 53 51 4c 49 54 45 5f 42 55 53 59 20 3a   ? SQLITE_BUSY :
2316e 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0d 0a 7d 0d   SQLITE_OK);..}.
2316f 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68  ...../*..** Each
23170 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65 20   VDBE holds the 
23171 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d 6f  result of the mo
23172 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74 65  st recent sqlite
23173 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0d 0a 2a  3_step() call..*
23174 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69  * in p->rc.  Thi
23175 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
23176 68 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20  hat result back 
23177 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a  to SQLITE_OK...*
23178 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
23179 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
2317a 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c  beResetStepResul
2317b 74 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20 70  t(Vdbe *p){..  p
2317c 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
2317d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
2317e 6f 70 79 20 74 68 65 20 65 72 72 6f 72 20 63 6f  opy the error co
2317f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73  de and error mes
23180 73 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74  sage belonging t
23181 6f 20 74 68 65 20 56 44 42 45 20 70 61 73 73 65  o the VDBE passe
23182 64 0d 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72  d..** as the fir
23183 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 69  st argument to i
23184 74 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  ts database hand
23185 6c 65 20 28 73 6f 20 74 68 61 74 20 74 68 65 79  le (so that they
23186 20 77 69 6c 6c 20 62 65 20 0d 0a 2a 2a 20 72 65   will be ..** re
23187 74 75 72 6e 65 64 20 62 79 20 63 61 6c 6c 73 20  turned by calls 
23188 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  to sqlite3_errco
23189 64 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  de() and sqlite3
2318a 5f 65 72 72 6d 73 67 28 29 29 2e 0d 0a 2a 2a 0d  _errmsg())...**.
2318b 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2318c 6e 20 64 6f 65 73 20 6e 6f 74 20 63 6c 65 61 72  n does not clear
2318d 20 74 68 65 20 56 44 42 45 20 65 72 72 6f 72 20   the VDBE error 
2318e 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 67 65 2c  code or message,
2318f 20 6a 75 73 74 0d 0a 2a 2a 20 63 6f 70 69 65 73   just..** copies
23190 20 74 68 65 6d 20 74 6f 20 74 68 65 20 64 61 74   them to the dat
23191 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0d 0a 2a  abase handle...*
23192 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
23193 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
23194 65 54 72 61 6e 73 66 65 72 45 72 72 6f 72 28 56  eTransferError(V
23195 64 62 65 20 2a 70 29 7b 0d 0a 20 20 73 71 6c 69  dbe *p){..  sqli
23196 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
23197 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 2d 3e  ..  int rc = p->
23198 72 63 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 7a 45  rc;..  if( p->zE
23199 72 72 4d 73 67 20 29 7b 0d 0a 20 20 20 20 75 38  rrMsg ){..    u8
2319a 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20   mallocFailed = 
2319b 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2319c 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ;..    sqlite3Be
2319d 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
2319e 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2319f 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
231a0 45 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72  Err, -1, p->zErr
231a1 4d 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Msg, SQLITE_UTF8
231a2 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
231a3 4e 54 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  NT);..    sqlite
231a4 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
231a5 28 29 3b 0d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  ();..    db->mal
231a6 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c  locFailed = mall
231a7 6f 63 46 61 69 6c 65 64 3b 0d 0a 20 20 20 20 64  ocFailed;..    d
231a8 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
231a9 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
231aa 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
231ab 20 72 63 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20   rc, 0);..  }.. 
231ac 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
231ad 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 65 61 6e 20 75  ../*..** Clean u
231ae 70 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65  p a VDBE after e
231af 78 65 63 75 74 69 6f 6e 20 62 75 74 20 64 6f 20  xecution but do 
231b0 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 56  not delete the V
231b1 44 42 45 20 6a 75 73 74 20 79 65 74 2e 0d 0a 2a  DBE just yet...*
231b2 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f  * Write any erro
231b3 72 20 6d 65 73 73 61 67 65 73 20 69 6e 74 6f 20  r messages into 
231b4 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 65 74 75  *pzErrMsg.  Retu
231b5 72 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  rn the result co
231b6 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65  de...**..** Afte
231b7 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
231b8 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 20  s run, the VDBE 
231b9 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 20  should be ready 
231ba 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0d 0a  to be executed..
231bb 2a 2a 20 61 67 61 69 6e 2e 0d 0a 2a 2a 0d 0a 2a  ** again...**..*
231bc 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20  * To look at it 
231bd 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69  another way, thi
231be 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73  s routine resets
231bf 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
231c0 65 0d 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61  e..** virtual ma
231c1 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f  chine from VDBE_
231c2 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42  MAGIC_RUN or VDB
231c3 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63  E_MAGIC_HALT bac
231c4 6b 20 74 6f 0d 0a 2a 2a 20 56 44 42 45 5f 4d 41  k to..** VDBE_MA
231c5 47 49 43 5f 49 4e 49 54 2e 0d 0a 2a 2f 0d 0a 53  GIC_INIT...*/..S
231c6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
231c7 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  t sqlite3VdbeRes
231c8 65 74 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20  et(Vdbe *p){..  
231c9 73 71 6c 69 74 65 33 20 2a 64 62 3b 0d 0a 20 20  sqlite3 *db;..  
231ca 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 0d 0a 20  db = p->db;.... 
231cb 20 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69   /* If the VM di
231cc 64 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d  d not run to com
231cd 70 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74  pletion or if it
231ce 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0d   encountered an.
231cf 0a 20 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65  .  ** error, the
231d0 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68  n it might not h
231d1 61 76 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20  ave been halted 
231d2 70 72 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61  properly.  So ha
231d3 6c 74 0d 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e  lt..  ** it now.
231d4 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
231d5 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0d 0a 0d  3VdbeHalt(p);...
231d6 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42  .  /* If the VDB
231d7 45 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65  E has be run eve
231d8 6e 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65  n partially, the
231d9 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65  n transfer the e
231da 72 72 6f 72 20 63 6f 64 65 0d 0a 20 20 2a 2a 20  rror code..  ** 
231db 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  and error messag
231dc 65 20 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20  e from the VDBE 
231dd 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61  into the main da
231de 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
231df 2e 20 20 42 75 74 0d 0a 20 20 2a 2a 20 69 66 20  .  But..  ** if 
231e0 74 68 65 20 56 44 42 45 20 68 61 73 20 6a 75 73  the VDBE has jus
231e1 74 20 62 65 65 6e 20 73 65 74 20 74 6f 20 72 75  t been set to ru
231e2 6e 20 62 75 74 20 68 61 73 20 6e 6f 74 20 61 63  n but has not ac
231e3 74 75 61 6c 6c 79 20 65 78 65 63 75 74 65 64 20  tually executed 
231e4 61 6e 79 0d 0a 20 20 2a 2a 20 69 6e 73 74 72 75  any..  ** instru
231e5 63 74 69 6f 6e 73 20 79 65 74 2c 20 6c 65 61 76  ctions yet, leav
231e6 65 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  e the main datab
231e7 61 73 65 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d  ase error inform
231e8 61 74 69 6f 6e 20 75 6e 63 68 61 6e 67 65 64 2e  ation unchanged.
231e9 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d  ..  */..  if( p-
231ea 3e 70 63 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 73  >pc>=0 ){..    s
231eb 71 6c 69 74 65 33 56 64 62 65 54 72 61 6e 73 66  qlite3VdbeTransf
231ec 65 72 45 72 72 6f 72 28 70 29 3b 0d 0a 20 20 20  erError(p);..   
231ed 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
231ee 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d  b, p->zErrMsg);.
231ef 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
231f0 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d  = 0;..    if( p-
231f1 3e 72 75 6e 4f 6e 6c 79 4f 6e 63 65 20 29 20 70  >runOnlyOnce ) p
231f2 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0d 0a  ->expired = 1;..
231f3 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
231f4 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20  c && p->expired 
231f5 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 65  ){..    /* The e
231f6 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20  xpired flag was 
231f7 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20  set on the VDBE 
231f8 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
231f9 20 63 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 74 6f   call..    ** to
231fa 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
231fb 20 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79   For consistency
231fc 20 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f   (since sqlite3_
231fd 73 74 65 70 28 29 20 77 61 73 0d 0a 20 20 20 20  step() was..    
231fe 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20  ** called), set 
231ff 74 68 65 20 64 61 74 61 62 61 73 65 20 65 72 72  the database err
23200 6f 72 20 69 6e 20 74 68 69 73 20 63 61 73 65 20  or in this case 
23201 61 73 20 77 65 6c 6c 2e 0d 0a 20 20 20 20 2a 2f  as well...    */
23202 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
23203 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29  or(db, p->rc, 0)
23204 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  ;..    sqlite3Va
23205 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
23206 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d  rr, -1, p->zErrM
23207 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  sg, SQLITE_UTF8,
23208 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
23209 54 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  T);..    sqlite3
2320a 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
2320b 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 70 2d 3e  rrMsg);..    p->
2320c 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d 0a 20 20  zErrMsg = 0;..  
2320d 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 63 6c 61 69  }....  /* Reclai
2320e 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65  m all memory use
2320f 64 20 62 79 20 74 68 65 20 56 44 42 45 0d 0a 20  d by the VDBE.. 
23210 20 2a 2f 0d 0a 20 20 43 6c 65 61 6e 75 70 28 70   */..  Cleanup(p
23211 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 61 76 65 20  );....  /* Save 
23212 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d  profiling inform
23213 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20  ation from this 
23214 56 44 42 45 20 72 75 6e 2e 0d 0a 20 20 2a 2f 0d  VDBE run...  */.
23215 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
23216 46 49 4c 45 0d 0a 09 7b 0d 0a 09 09 65 78 74 65  FILE...{....exte
23217 72 6e 20 42 4f 4f 4c 20 62 4e 65 65 64 50 72 6f  rn BOOL bNeedPro
23218 66 69 6c 65 3b 0d 0a 09 09 2f 2f 46 49 4c 45 20  file;....//FILE 
23219 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64  *out = fopen("vd
2321a 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c  be_profile.out",
2321b 20 22 61 22 29 3b 0d 0a 09 09 69 66 28 62 4e 65   "a");....if(bNe
2321c 65 64 50 72 6f 66 69 6c 65 29 0d 0a 09 09 7b 0d  edProfile)....{.
2321d 0a 09 09 09 69 6e 74 20 69 3b 0d 0a 09 09 09 64  ....int i;.....d
2321e 6f 50 72 6f 66 69 6c 65 4d 73 67 28 22 2d 2d 2d  oProfileMsg("---
2321f 2d 2d 2d 2d 2d 2d 2d 2d 20 71 75 65 72 79 20 62  -------- query b
23220 65 67 69 6e 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  egin -----------
23221 5c 6e 22 29 3b 0d 0a 09 09 09 66 6f 72 28 69 3d  \n");.....for(i=
23222 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b  0; i<p->nOp; i++
23223 29 0d 0a 09 09 09 7b 0d 0a 09 09 09 09 63 68 61  ).....{......cha
23224 72 20 62 75 66 5b 30 78 31 30 30 30 5d 3b 0d 0a  r buf[0x1000];..
23225 09 09 09 09 73 70 72 69 6e 74 66 28 62 75 66 2c  ....sprintf(buf,
23226 20 22 25 36 64 20 25 31 30 49 36 34 64 20 25 38   "%6d %10I64d %8
23227 49 36 34 64 20 22 2c 0d 0a 09 09 09 09 09 70 2d  I64d ",.......p-
23228 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0d 0a 09 09  >aOp[i].cnt,....
23229 09 09 09 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63  ...p->aOp[i].cyc
2322a 6c 65 73 2c 0d 0a 09 09 09 09 09 70 2d 3e 61 4f  les,.......p->aO
2322b 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e  p[i].cnt>0 ? p->
2322c 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d  aOp[i].cycles/p-
2322d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0d  >aOp[i].cnt : 0.
2322e 0a 09 09 09 09 09 29 3b 0d 0a 09 09 09 09 64 6f  ......);......do
2322f 50 72 6f 66 69 6c 65 4d 73 67 28 62 75 66 29 3b  ProfileMsg(buf);
23230 0d 0a 09 09 09 09 73 71 6c 69 74 65 33 56 64 62  ......sqlite3Vdb
23231 65 50 72 69 6e 74 4f 70 28 4e 55 4c 4c 2c 20 69  ePrintOp(NULL, i
23232 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0d 0a  , &p->aOp[i]);..
23233 09 09 09 7d 0d 0a 09 09 09 64 6f 50 72 6f 66 69  ...}.....doProfi
23234 6c 65 4d 73 67 28 22 2d 2d 2d 2d 2d 2d 2d 2d 2d  leMsg("---------
23235 2d 2d 20 71 75 65 72 79 20 65 6e 64 20 2d 2d 2d  -- query end ---
23236 2d 2d 2d 2d 2d 2d 2d 2d 5c 6e 5c 6e 22 29 3b 0d  --------\n\n");.
23237 0a 09 09 7d 0d 0a 09 09 2f 2f 66 63 6c 6f 73 65  ...}....//fclose
23238 28 6f 75 74 29 3b 0d 0a 09 7d 0d 0a 23 65 6e 64  (out);...}..#end
23239 69 66 0d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d  if..  p->magic =
2323a 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
2323b 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72  ;..  return p->r
2323c 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b  c & db->errMask;
2323d 0d 0a 7d 0d 0a 20 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ..}.. ../*..** C
2323e 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65  lean up and dele
2323f 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20  te a VDBE after 
23240 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75  execution.  Retu
23241 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  rn an integer wh
23242 69 63 68 20 69 73 0d 0a 2a 2a 20 74 68 65 20 72  ich is..** the r
23243 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69  esult code.  Wri
23244 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
23245 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a  sage text into *
23246 70 7a 45 72 72 4d 73 67 2e 0d 0a 2a 2f 0d 0a 53  pzErrMsg...*/..S
23247 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
23248 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  t sqlite3VdbeFin
23249 61 6c 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0d  alize(Vdbe *p){.
2324a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2324b 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 2d  TE_OK;..  if( p-
2324c 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
2324d 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67  IC_RUN || p->mag
2324e 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48  ic==VDBE_MAGIC_H
2324f 41 4c 54 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ALT ){..    rc =
23250 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65   sqlite3VdbeRese
23251 74 28 70 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  t(p);..    asser
23252 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e  t( (rc & p->db->
23253 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0d  errMask)==rc );.
23254 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 56  .  }..  sqlite3V
23255 64 62 65 44 65 6c 65 74 65 28 70 29 3b 0d 0a 20  dbeDelete(p);.. 
23256 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
23257 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c 20 74 68  ../*..** Call th
23258 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
23259 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e   each auxdata en
2325a 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63  try in pVdbeFunc
2325b 20 66 6f 72 20 77 68 69 63 68 0d 0a 2a 2a 20 74   for which..** t
2325c 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2325d 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20   bit in mask is 
2325e 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20  clear.  Auxdata 
2325f 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33  entries beyond 3
23260 31 0d 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73  1..** are always
23261 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20   destroyed.  To 
23262 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64  destroy all auxd
23263 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c  ata entries, cal
23264 6c 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69  l this..** routi
23265 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e  ne with mask==0.
23266 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
23267 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23268 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
23269 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64  ta(VdbeFunc *pVd
2326a 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b  beFunc, int mask
2326b 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
2326c 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65  for(i=0; i<pVdbe
2326d 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29  Func->nAux; i++)
2326e 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 41 75  {..    struct Au
2326f 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 70  xData *pAux = &p
23270 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
23271 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 28 69 3e  i];..    if( (i>
23272 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 28 28  31 || !(mask&(((
23273 75 33 32 29 31 29 3c 3c 69 29 29 29 20 26 26 20  u32)1)<<i))) && 
23274 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0d 0a 20  pAux->pAux ){.. 
23275 20 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78       if( pAux->x
23276 44 65 6c 65 74 65 20 29 7b 0d 0a 20 20 20 20 20  Delete ){..     
23277 20 20 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65     pAux->xDelete
23278 28 70 41 75 78 2d 3e 70 41 75 78 29 3b 0d 0a 20  (pAux->pAux);.. 
23279 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 41       }..      pA
2327a 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0d 0a 20  ux->pAux = 0;.. 
2327b 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
2327c 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20  /*..** Free all 
2327d 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
2327e 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65 20  d with the Vdbe 
2327f 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
23280 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0d 0a  cond argument...
23281 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  ** The differenc
23282 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 66  e between this f
23283 75 6e 63 74 69 6f 6e 20 61 6e 64 20 73 71 6c 69  unction and sqli
23284 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 29 20  te3VdbeDelete() 
23285 69 73 20 74 68 61 74 0d 0a 2a 2a 20 56 64 62 65  is that..** Vdbe
23286 44 65 6c 65 74 65 28 29 20 61 6c 73 6f 20 75 6e  Delete() also un
23287 6c 69 6e 6b 73 20 74 68 65 20 56 64 62 65 20 66  links the Vdbe f
23288 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20  rom the list of 
23289 56 4d 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  VMs associated w
2328a 69 74 68 0d 0a 2a 2a 20 74 68 65 20 64 61 74 61  ith..** the data
2328b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
2328c 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2328d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2328e 33 56 64 62 65 44 65 6c 65 74 65 4f 62 6a 65 63  3VdbeDeleteObjec
2328f 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56  t(sqlite3 *db, V
23290 64 62 65 20 2a 70 29 7b 0d 0a 20 20 53 75 62 50  dbe *p){..  SubP
23291 72 6f 67 72 61 6d 20 2a 70 53 75 62 2c 20 2a 70  rogram *pSub, *p
23292 4e 65 78 74 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d  Next;..  int i;.
23293 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
23294 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 64 62  ==0 || p->db==db
23295 20 29 3b 0d 0a 20 20 72 65 6c 65 61 73 65 4d 65   );..  releaseMe
23296 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20  mArray(p->aVar, 
23297 70 2d 3e 6e 56 61 72 29 3b 0d 0a 20 20 72 65 6c  p->nVar);..  rel
23298 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e  easeMemArray(p->
23299 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65  aColName, p->nRe
2329a 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f  sColumn*COLNAME_
2329b 4e 29 3b 0d 0a 20 20 66 6f 72 28 70 53 75 62 3d  N);..  for(pSub=
2329c 70 2d 3e 70 50 72 6f 67 72 61 6d 3b 20 70 53 75  p->pProgram; pSu
2329d 62 3b 20 70 53 75 62 3d 70 4e 65 78 74 29 7b 0d  b; pSub=pNext){.
2329e 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 53 75  .    pNext = pSu
2329f 62 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 76  b->pNext;..    v
232a0 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 64  dbeFreeOpArray(d
232a1 62 2c 20 70 53 75 62 2d 3e 61 4f 70 2c 20 70 53  b, pSub->aOp, pS
232a2 75 62 2d 3e 6e 4f 70 29 3b 0d 0a 20 20 20 20 73  ub->nOp);..    s
232a3 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
232a4 20 70 53 75 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20   pSub);..  }..  
232a5 66 6f 72 28 69 3d 70 2d 3e 6e 7a 56 61 72 2d 31  for(i=p->nzVar-1
232a6 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 20 73 71 6c  ; i>=0; i--) sql
232a7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
232a8 2d 3e 61 7a 56 61 72 5b 69 5d 29 3b 0d 0a 20 20  ->azVar[i]);..  
232a9 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28  vdbeFreeOpArray(
232aa 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70 2d 3e 6e  db, p->aOp, p->n
232ab 4f 70 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44  Op);..  sqlite3D
232ac 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 4c 61  bFree(db, p->aLa
232ad 62 65 6c 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  bel);..  sqlite3
232ae 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43  DbFree(db, p->aC
232af 6f 6c 4e 61 6d 65 29 3b 0d 0a 20 20 73 71 6c 69  olName);..  sqli
232b0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
232b1 3e 7a 53 71 6c 29 3b 0d 0a 20 20 73 71 6c 69 74  >zSql);..  sqlit
232b2 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
232b3 70 46 72 65 65 29 3b 0d 0a 23 69 66 20 64 65 66  pFree);..#if def
232b4 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
232b5 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 29  LE_TREE_EXPLAIN)
232b6 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ..  sqlite3DbFre
232b7 65 28 64 62 2c 20 70 2d 3e 7a 45 78 70 6c 61 69  e(db, p->zExplai
232b8 6e 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62  n);..  sqlite3Db
232b9 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 45 78 70  Free(db, p->pExp
232ba 6c 61 69 6e 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  lain);..#endif..
232bb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
232bc 64 62 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  db, p);..}..../*
232bd 0d 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  ..** Delete an e
232be 6e 74 69 72 65 20 56 44 42 45 2e 0d 0a 2a 2f 0d  ntire VDBE...*/.
232bf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
232c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
232c1 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b  Delete(Vdbe *p){
232c2 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ..  sqlite3 *db;
232c3 0d 0a 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28  ....  if( NEVER(
232c4 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0d  p==0) ) return;.
232c5 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a  .  db = p->db;..
232c6 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
232c7 7b 0d 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d  {..    p->pPrev-
232c8 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78  >pNext = p->pNex
232c9 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  t;..  }else{..  
232ca 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 56    assert( db->pV
232cb 64 62 65 3d 3d 70 20 29 3b 0d 0a 20 20 20 20 64  dbe==p );..    d
232cc 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e 70 4e  b->pVdbe = p->pN
232cd 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  ext;..  }..  if(
232ce 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0d 0a 20 20   p->pNext ){..  
232cf 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65    p->pNext->pPre
232d0 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0d 0a 20  v = p->pPrev;.. 
232d1 20 7d 0d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   }..  p->magic =
232d2 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44   VDBE_MAGIC_DEAD
232d3 3b 0d 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0d  ;..  p->db = 0;.
232d4 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  .  sqlite3VdbeDe
232d5 6c 65 74 65 4f 62 6a 65 63 74 28 64 62 2c 20 70  leteObject(db, p
232d6 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
232d7 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75  Make sure the cu
232d8 72 73 6f 72 20 70 20 69 73 20 72 65 61 64 79 20  rsor p is ready 
232d9 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  to read or write
232da 20 74 68 65 20 72 6f 77 20 74 6f 20 77 68 69 63   the row to whic
232db 68 20 69 74 0d 0a 2a 2a 20 77 61 73 20 6c 61 73  h it..** was las
232dc 74 20 70 6f 73 69 74 69 6f 6e 65 64 2e 20 20 52  t positioned.  R
232dd 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
232de 6f 64 65 20 69 66 20 61 6e 20 4f 4f 4d 20 66 61  ode if an OOM fa
232df 75 6c 74 20 6f 72 20 49 2f 4f 20 65 72 72 6f 72  ult or I/O error
232e0 0d 0a 2a 2a 20 70 72 65 76 65 6e 74 73 20 75 73  ..** prevents us
232e1 20 66 72 6f 6d 20 70 6f 73 69 74 69 6f 6e 69 6e   from positionin
232e2 67 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  g the cursor to 
232e3 69 74 73 20 63 6f 72 72 65 63 74 20 70 6f 73 69  its correct posi
232e4 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  tion...**..** If
232e5 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74   a MoveTo operat
232e6 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f  ion is pending o
232e7 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73  n the given curs
232e8 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74  or, then do that
232e9 0d 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e  ..** MoveTo now.
232ea 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73 20    If no move is 
232eb 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20 74  pending, check t
232ec 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f 77  o see if the row
232ed 20 68 61 73 20 62 65 65 6e 0d 0a 2a 2a 20 64 65   has been..** de
232ee 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75  leted out from u
232ef 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20  nder the cursor 
232f0 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d  and if it has, m
232f1 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0d 0a  ark the row as..
232f2 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0d 0a  ** a NULL row...
232f3 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 63 75  **..** If the cu
232f4 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  rsor is already 
232f5 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
232f6 63 6f 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20  correct row and 
232f7 74 68 61 74 20 72 6f 77 20 68 61 73 0d 0a 2a 2a  that row has..**
232f8 20 6e 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 65   not been delete
232f9 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72  d out from under
232fa 20 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65   the cursor, the
232fb 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
232fc 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a  s a no-op...*/..
232fd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
232fe 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  nt sqlite3VdbeCu
232ff 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43  rsorMoveto(VdbeC
23300 75 72 73 6f 72 20 2a 70 29 7b 0d 0a 20 20 69 66  ursor *p){..  if
23301 28 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  ( p->deferredMov
23302 65 74 6f 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  eto ){..    int 
23303 72 65 73 2c 20 72 63 3b 0d 0a 23 69 66 64 65 66  res, rc;..#ifdef
23304 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20   SQLITE_TEST..  
23305 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
23306 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
23307 74 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  t;..#endif..    
23308 61 73 73 65 72 74 28 20 70 2d 3e 69 73 54 61 62  assert( p->isTab
23309 6c 65 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  le );..    rc = 
2330a 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
2330b 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e 70 43  toUnpacked(p->pC
2330c 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f 76  ursor, 0, p->mov
2330d 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26 72  etoTarget, 0, &r
2330e 65 73 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  es);..    if( rc
2330f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20   ) return rc;.. 
23310 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20     p->lastRowid 
23311 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  = p->movetoTarge
23312 74 3b 0d 0a 20 20 20 20 69 66 28 20 72 65 73 21  t;..    if( res!
23313 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
23314 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
23315 0d 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 73  ..    p->rowidIs
23316 56 61 6c 69 64 20 3d 20 31 3b 0d 0a 23 69 66 64  Valid = 1;..#ifd
23317 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a  ef SQLITE_TEST..
23318 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72      sqlite3_sear
23319 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0d 0a 23 65 6e  ch_count++;..#en
2331a 64 69 66 0d 0a 20 20 20 20 70 2d 3e 64 65 66 65  dif..    p->defe
2331b 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0d  rredMoveto = 0;.
2331c 0a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61  .    p->cacheSta
2331d 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
2331e 45 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  E;..  }else if( 
2331f 41 4c 57 41 59 53 28 70 2d 3e 70 43 75 72 73 6f  ALWAYS(p->pCurso
23320 72 29 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 68  r) ){..    int h
23321 61 73 4d 6f 76 65 64 3b 0d 0a 20 20 20 20 69 6e  asMoved;..    in
23322 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74  t rc = sqlite3Bt
23323 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
23324 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 68  d(p->pCursor, &h
23325 61 73 4d 6f 76 65 64 29 3b 0d 0a 20 20 20 20 69  asMoved);..    i
23326 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
23327 63 3b 0d 0a 20 20 20 20 69 66 28 20 68 61 73 4d  c;..    if( hasM
23328 6f 76 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 70  oved ){..      p
23329 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
2332a 43 41 43 48 45 5f 53 54 41 4c 45 3b 0d 0a 20 20  CACHE_STALE;..  
2332b 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d      p->nullRow =
2332c 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   1;..    }..  }.
2332d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2332e 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
2332f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
23330 66 75 6e 63 74 69 6f 6e 73 3a 0d 0a 2a 2a 0d 0a  functions:..**..
23331 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  ** sqlite3VdbeSe
23332 72 69 61 6c 54 79 70 65 28 29 0d 0a 2a 2a 20 73  rialType()..** s
23333 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
23334 54 79 70 65 4c 65 6e 28 29 0d 0a 2a 2a 20 73 71  TypeLen()..** sq
23335 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 4c  lite3VdbeSerialL
23336 65 6e 28 29 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  en()..** sqlite3
23337 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 29 0d  VdbeSerialPut().
23338 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
23339 65 72 69 61 6c 47 65 74 28 29 0d 0a 2a 2a 0d 0a  erialGet()..**..
2333a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74  ** encapsulate t
2333b 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 72  he code that ser
2333c 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66  ializes values f
2333d 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51  or storage in SQ
2333e 4c 69 74 65 0d 0a 2a 2a 20 64 61 74 61 20 61 6e  Lite..** data an
2333f 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e  d index records.
23340 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64   Each serialized
23341 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20   value consists 
23342 6f 66 20 61 0d 0a 2a 2a 20 27 73 65 72 69 61 6c  of a..** 'serial
23343 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f  -type' and a blo
23344 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73  b of data. The s
23345 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e  erial type is an
23346 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   8-byte unsigned
23347 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74  ..** integer, st
23348 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74  ored as a varint
23349 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 61 6e 20  ...**..** In an 
2334a 53 51 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63  SQLite index rec
2334b 6f 72 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20  ord, the serial 
2334c 74 79 70 65 20 69 73 20 73 74 6f 72 65 64 20 64  type is stored d
2334d 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 0d 0a  irectly before..
2334e 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 64  ** the blob of d
2334f 61 74 61 20 74 68 61 74 20 69 74 20 63 6f 72 72  ata that it corr
23350 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61  esponds to. In a
23351 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c 20 61   table record, a
23352 6c 6c 20 73 65 72 69 61 6c 0d 0a 2a 2a 20 74 79  ll serial..** ty
23353 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61  pes are stored a
23354 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
23355 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 74  he record, and t
23356 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 61  he blobs of data
23357 20 61 74 0d 0a 2a 2a 20 74 68 65 20 65 6e 64 2e   at..** the end.
23358 20 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e   Hence these fun
23359 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65  ctions allow the
2335a 20 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c   caller to handl
2335b 65 20 74 68 65 0d 0a 2a 2a 20 73 65 72 69 61 6c  e the..** serial
2335c 2d 74 79 70 65 20 61 6e 64 20 64 61 74 61 20 62  -type and data b
2335d 6c 6f 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0d  lob seperately..
2335e 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  .**..** The foll
2335f 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63  owing table desc
23360 72 69 62 65 73 20 74 68 65 20 76 61 72 69 6f 75  ribes the variou
23361 73 20 73 74 6f 72 61 67 65 20 63 6c 61 73 73 65  s storage classe
23362 73 20 66 6f 72 20 64 61 74 61 3a 0d 0a 2a 2a 0d  s for data:..**.
23363 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 74 79 70  .**   serial typ
23364 65 20 20 20 20 20 20 20 20 62 79 74 65 73 20 6f  e        bytes o
23365 66 20 64 61 74 61 20 20 20 20 20 20 74 79 70 65  f data      type
23366 0d 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..**   ---------
23367 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d  -----     ------
23368 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d  ---------    ---
23369 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a  ------------..**
2336a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
2336b 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
2336c 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0d 0a 2a           NULL..*
2336d 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20  *      1        
2336e 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20               1  
2336f 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
23370 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 20 20 20   integer..**    
23371 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
23372 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
23373 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
23374 67 65 72 0d 0a 2a 2a 20 20 20 20 20 20 33 20 20  ger..**      3  
23375 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23376 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20     3            
23377 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0d 0a  signed integer..
23378 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20  **      4       
23379 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20                4 
2337a 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
2337b 64 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 20 20  d integer..**   
2337c 20 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20     5            
2337d 20 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20           6      
2337e 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
2337f 65 67 65 72 0d 0a 2a 2a 20 20 20 20 20 20 36 20  eger..**      6 
23380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23381 20 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20      8           
23382 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0d   signed integer.
23383 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 20  .**      7      
23384 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
23385 20 20 20 20 20 20 20 20 20 20 20 20 49 45 45 45              IEEE
23386 20 66 6c 6f 61 74 0d 0a 2a 2a 20 20 20 20 20 20   float..**      
23387 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  8               
23388 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
23389 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74     Integer const
2338a 61 6e 74 20 30 0d 0a 2a 2a 20 20 20 20 20 20 39  ant 0..**      9
2338b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2338c 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
2338d 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61    Integer consta
2338e 6e 74 20 31 0d 0a 2a 2a 20 20 20 20 20 31 30 2c  nt 1..**     10,
2338f 31 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20  11              
23390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23391 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 65 78   reserved for ex
23392 70 61 6e 73 69 6f 6e 0d 0a 2a 2a 20 20 20 20 4e  pansion..**    N
23393 3e 3d 31 32 20 61 6e 64 20 65 76 65 6e 20 20 20  >=12 and even   
23394 20 20 20 20 28 4e 2d 31 32 29 2f 32 20 20 20 20      (N-12)/2    
23395 20 20 20 20 42 4c 4f 42 0d 0a 2a 2a 20 20 20 20      BLOB..**    
23396 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64 20 20 20  N>=13 and odd   
23397 20 20 20 20 20 28 4e 2d 31 33 29 2f 32 20 20 20       (N-13)/2   
23398 20 20 20 20 20 74 65 78 74 0d 0a 2a 2a 0d 0a 2a       text..**..*
23399 2a 20 54 68 65 20 38 20 61 6e 64 20 39 20 74 79  * The 8 and 9 ty
2339a 70 65 73 20 77 65 72 65 20 61 64 64 65 64 20 69  pes were added i
2339b 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66 6f  n 3.3.0, file fo
2339c 72 6d 61 74 20 34 2e 20 20 50 72 69 6f 72 20 76  rmat 4.  Prior v
2339d 65 72 73 69 6f 6e 73 0d 0a 2a 2a 20 6f 66 20 53  ersions..** of S
2339e 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75  QLite will not u
2339f 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73 65 20  nderstand those 
233a0 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0d 0a 2a  serial types...*
233a1 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  /..../*..** Retu
233a2 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79  rn the serial-ty
233a3 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65  pe for the value
233a4 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e   stored in pMem.
233a5 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
233a6 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
233a7 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d  VdbeSerialType(M
233a8 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69  em *pMem, int fi
233a9 6c 65 5f 66 6f 72 6d 61 74 29 7b 0d 0a 20 20 69  le_format){..  i
233aa 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d  nt flags = pMem-
233ab 3e 66 6c 61 67 73 3b 0d 0a 20 20 69 6e 74 20 6e  >flags;..  int n
233ac 3b 0d 0a 0d 0a 20 20 69 66 28 20 66 6c 61 67 73  ;....  if( flags
233ad 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20  &MEM_Null ){..  
233ae 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
233af 0d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45  ..  if( flags&ME
233b0 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20 20 20 2f 2a  M_Int ){..    /*
233b1 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 74   Figure out whet
233b2 68 65 72 20 74 6f 20 75 73 65 20 31 2c 20 32 2c  her to use 1, 2,
233b3 20 34 2c 20 36 20 6f 72 20 38 20 62 79 74 65 73   4, 6 or 8 bytes
233b4 2e 20 2a 2f 0d 0a 23 20 20 20 64 65 66 69 6e 65  . */..#   define
233b5 20 4d 41 58 5f 36 42 59 54 45 20 28 28 28 28 69   MAX_6BYTE ((((i
233b6 36 34 29 30 78 30 30 30 30 38 30 30 30 29 3c 3c  64)0x00008000)<<
233b7 33 32 29 2d 31 29 0d 0a 20 20 20 20 69 36 34 20  32)-1)..    i64 
233b8 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0d 0a  i = pMem->u.i;..
233b9 20 20 20 20 75 36 34 20 75 3b 0d 0a 20 20 20 20      u64 u;..    
233ba 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e  if( file_format>
233bb 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29  =4 && (i&1)==i )
233bc 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
233bd 38 2b 28 75 33 32 29 69 3b 0d 0a 20 20 20 20 7d  8+(u32)i;..    }
233be 0d 0a 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b  ..    if( i<0 ){
233bf 0d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 28 2d  ..      if( i<(-
233c0 4d 41 58 5f 36 42 59 54 45 29 20 29 20 72 65 74  MAX_6BYTE) ) ret
233c1 75 72 6e 20 36 3b 0d 0a 20 20 20 20 20 20 2f 2a  urn 6;..      /*
233c2 20 50 72 65 76 69 6f 75 73 20 74 65 73 74 20 70   Previous test p
233c3 72 65 76 65 6e 74 73 3a 20 20 75 20 3d 20 2d 28  revents:  u = -(
233c4 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  -922337203685477
233c5 35 38 30 38 29 20 2a 2f 0d 0a 20 20 20 20 20 20  5808) */..      
233c6 75 20 3d 20 2d 69 3b 0d 0a 20 20 20 20 7d 65 6c  u = -i;..    }el
233c7 73 65 7b 0d 0a 20 20 20 20 20 20 75 20 3d 20 69  se{..      u = i
233c8 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
233c9 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74 75 72  ( u<=127 ) retur
233ca 6e 20 31 3b 0d 0a 20 20 20 20 69 66 28 20 75 3c  n 1;..    if( u<
233cb 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 6e 20  =32767 ) return 
233cc 32 3b 0d 0a 20 20 20 20 69 66 28 20 75 3c 3d 38  2;..    if( u<=8
233cd 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 20  388607 ) return 
233ce 33 3b 0d 0a 20 20 20 20 69 66 28 20 75 3c 3d 32  3;..    if( u<=2
233cf 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75  147483647 ) retu
233d0 72 6e 20 34 3b 0d 0a 20 20 20 20 69 66 28 20 75  rn 4;..    if( u
233d1 3c 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65  <=MAX_6BYTE ) re
233d2 74 75 72 6e 20 35 3b 0d 0a 20 20 20 20 72 65 74  turn 5;..    ret
233d3 75 72 6e 20 36 3b 0d 0a 20 20 7d 0d 0a 20 20 69  urn 6;..  }..  i
233d4 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52 65 61  f( flags&MEM_Rea
233d5 6c 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  l ){..    return
233d6 20 37 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65   7;..  }..  asse
233d7 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 61  rt( pMem->db->ma
233d8 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 6c  llocFailed || fl
233d9 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  ags&(MEM_Str|MEM
233da 5f 42 6c 6f 62 29 20 29 3b 0d 0a 20 20 6e 20 3d  _Blob) );..  n =
233db 20 70 4d 65 6d 2d 3e 6e 3b 0d 0a 20 20 69 66 28   pMem->n;..  if(
233dc 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72   flags & MEM_Zer
233dd 6f 20 29 7b 0d 0a 20 20 20 20 6e 20 2b 3d 20 70  o ){..    n += p
233de 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0d 0a 20  Mem->u.nZero;.. 
233df 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e   }..  assert( n>
233e0 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  =0 );..  return 
233e1 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28  ((n*2) + 12 + ((
233e2 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d  flags&MEM_Str)!=
233e3 30 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  0));..}..../*..*
233e4 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
233e5 67 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20  gth of the data 
233e6 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
233e7 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65   the supplied se
233e8 72 69 61 6c 2d 74 79 70 65 2e 0d 0a 2a 2f 0d 0a  rial-type...*/..
233e9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
233ea 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
233eb 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 20  rialTypeLen(u32 
233ec 73 65 72 69 61 6c 5f 74 79 70 65 29 7b 0d 0a 20  serial_type){.. 
233ed 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
233ee 3e 3d 31 32 20 29 7b 0d 0a 20 20 20 20 72 65 74  >=12 ){..    ret
233ef 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 79 70 65  urn (serial_type
233f0 2d 31 32 29 2f 32 3b 0d 0a 20 20 7d 65 6c 73 65  -12)/2;..  }else
233f1 7b 0d 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  {..    static co
233f2 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 3d  nst u8 aSize[] =
233f3 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34   { 0, 1, 2, 3, 4
233f4 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 2c  , 6, 8, 8, 0, 0,
233f5 20 30 2c 20 30 20 7d 3b 0d 0a 20 20 20 20 72 65   0, 0 };..    re
233f6 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61  turn aSize[seria
233f7 6c 5f 74 79 70 65 5d 3b 0d 0a 20 20 7d 0d 0a 7d  l_type];..  }..}
233f8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 77 65  ..../*..** If we
233f9 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69   are on an archi
233fa 74 65 63 74 75 72 65 20 77 69 74 68 20 6d 69 78  tecture with mix
233fb 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69  ed-endian floati
233fc 6e 67 20 0d 0a 2a 2a 20 70 6f 69 6e 74 73 20 28  ng ..** points (
233fd 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 73  ex: ARM7) then s
233fe 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20 34 20  wap the lower 4 
233ff 62 79 74 65 73 20 77 69 74 68 20 74 68 65 20 0d  bytes with the .
23400 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74 65  .** upper 4 byte
23401 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72  s.  Return the r
23402 65 73 75 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46  esult...**..** F
23403 6f 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63  or most architec
23404 74 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61  tures, this is a
23405 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   no-op...**..** 
23406 28 6c 61 74 65 72 29 3a 20 20 49 74 20 69 73 20  (later):  It is 
23407 72 65 70 6f 72 74 65 64 20 74 6f 20 6d 65 20 74  reported to me t
23408 68 61 74 20 74 68 65 20 6d 69 78 65 64 2d 65 6e  hat the mixed-en
23409 64 69 61 6e 20 70 72 6f 62 6c 65 6d 0d 0a 2a 2a  dian problem..**
2340a 20 6f 6e 20 41 52 4d 37 20 69 73 20 61 6e 20 69   on ARM7 is an i
2340b 73 73 75 65 20 77 69 74 68 20 47 43 43 2c 20 6e  ssue with GCC, n
2340c 6f 74 20 77 69 74 68 20 74 68 65 20 41 52 4d 37  ot with the ARM7
2340d 20 63 68 69 70 2e 20 20 49 74 20 73 65 65 6d 73   chip.  It seems
2340e 0d 0a 2a 2a 20 74 68 61 74 20 65 61 72 6c 79 20  ..** that early 
2340f 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43 20  versions of GCC 
23410 73 74 6f 72 65 64 20 74 68 65 20 74 77 6f 20 77  stored the two w
23411 6f 72 64 73 20 6f 66 20 61 20 36 34 2d 62 69 74  ords of a 64-bit
23412 0d 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 74 68  ..** float in th
23413 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e 20 20  e wrong order.  
23414 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 20 68  And that error h
23415 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 61 74  as been propagat
23416 65 64 0d 0a 2a 2a 20 65 76 65 72 20 73 69 6e 63  ed..** ever sinc
23417 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 73  e.  The blame is
23418 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
23419 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 67   with GCC, thoug
2341a 68 2e 0d 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74  h...** GCC might
2341b 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69   have just copyi
2341c 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66  ng the problem f
2341d 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70  rom a prior comp
2341e 69 6c 65 72 2e 0d 0a 2a 2a 20 49 20 61 6d 20 61  iler...** I am a
2341f 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65  lso told that ne
23420 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  wer versions of 
23421 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  GCC that follow 
23422 61 20 64 69 66 66 65 72 65 6e 74 0d 0a 2a 2a 20  a different..** 
23423 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65  ABI get the byte
23424 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0d 0a 2a   order right...*
23425 2a 0d 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73  *..** Developers
23426 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e   using SQLite on
23427 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20   an ARM7 should 
23428 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20  compile and run 
23429 74 68 65 69 72 0d 0a 2a 2a 20 61 70 70 6c 69 63  their..** applic
2342a 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51  ation using -DSQ
2342b 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20  LITE_DEBUG=1 at 
2342c 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74  least once.  Wit
2342d 68 20 44 45 42 55 47 0d 0a 2a 2a 20 65 6e 61 62  h DEBUG..** enab
2342e 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 74  led, some assert
2342f 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73  s below will ens
23430 75 72 65 20 74 68 61 74 20 74 68 65 20 62 79 74  ure that the byt
23431 65 20 6f 72 64 65 72 20 6f 66 0d 0a 2a 2a 20 66  e order of..** f
23432 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
23433 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74 2e  lues is correct.
23434 0d 0a 2a 2a 0d 0a 2a 2a 20 28 32 30 30 37 2d 30  ..**..** (2007-0
23435 38 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e  8-30)  Frank van
23436 20 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65   Vugt has studie
23437 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63  d this problem c
23438 6c 6f 73 65 6c 79 0d 0a 2a 2a 20 61 6e 64 20 68  losely..** and h
23439 61 73 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64  as send his find
2343a 69 6e 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69  ings to the SQLi
2343b 74 65 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20  te developers.  
2343c 46 72 61 6e 6b 0d 0a 2a 2a 20 77 72 69 74 65 73  Frank..** writes
2343d 20 74 68 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78   that some Linux
2343e 20 6b 65 72 6e 65 6c 73 20 6f 66 66 65 72 20 66   kernels offer f
2343f 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 68 61  loating point ha
23440 72 64 77 61 72 65 0d 0a 2a 2a 20 65 6d 75 6c 61  rdware..** emula
23441 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20 6f  tion that uses o
23442 6e 6c 79 20 33 32 2d 62 69 74 20 6d 61 6e 74 69  nly 32-bit manti
23443 73 73 61 73 20 69 6e 73 74 65 61 64 20 6f 66 20  ssas instead of 
23444 61 20 66 75 6c 6c 20 0d 0a 2a 2a 20 34 38 2d 62  a full ..** 48-b
23445 69 74 73 20 61 73 20 72 65 71 75 69 72 65 64 20  its as required 
23446 62 79 20 74 68 65 20 49 45 45 45 20 73 74 61 6e  by the IEEE stan
23447 64 61 72 64 2e 20 20 28 54 68 69 73 20 69 73 20  dard.  (This is 
23448 74 68 65 0d 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46  the..** CONFIG_F
23449 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 69 6f  PE_FASTFPE optio
2344a 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 79 73  n.)  On such sys
2344b 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70  tems, floating p
2344c 6f 69 6e 74 0d 0a 2a 2a 20 62 79 74 65 20 73 77  oint..** byte sw
2344d 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 76  apping becomes v
2344e 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e  ery complicated.
2344f 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c    To avoid probl
23450 65 6d 73 2c 0d 0a 2a 2a 20 74 68 65 20 6e 65 63  ems,..** the nec
23451 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 70  essary byte swap
23452 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 20  ping is carried 
23453 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62  out using a 64-b
23454 69 74 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 72  it integer..** r
23455 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d  ather than a 64-
23456 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e  bit float.  Fran
23457 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61  k assures us tha
23458 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0d  t the code here.
23459 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69  .** works for hi
2345a 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65  m.  We, the deve
2345b 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20  lopers, have no 
2345c 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65  way to independe
2345d 6e 74 6c 79 0d 0a 2a 2a 20 76 65 72 69 66 79 20  ntly..** verify 
2345e 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20  this, but Frank 
2345f 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68  seems to know wh
23460 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67  at he is talking
23461 20 61 62 6f 75 74 0d 0a 2a 2a 20 73 6f 20 77 65   about..** so we
23462 20 74 72 75 73 74 20 68 69 6d 2e 0d 0a 2a 2f 0d   trust him...*/.
23463 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
23464 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49  IXED_ENDIAN_64BI
23465 54 5f 46 4c 4f 41 54 0d 0a 73 74 61 74 69 63 20  T_FLOAT..static 
23466 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75 36  u64 floatSwap(u6
23467 34 20 69 6e 29 7b 0d 0a 20 20 75 6e 69 6f 6e 20  4 in){..  union 
23468 7b 0d 0a 20 20 20 20 75 36 34 20 72 3b 0d 0a 20  {..    u64 r;.. 
23469 20 20 20 75 33 32 20 69 5b 32 5d 3b 0d 0a 20 20     u32 i[2];..  
2346a 7d 20 75 3b 0d 0a 20 20 75 33 32 20 74 3b 0d 0a  } u;..  u32 t;..
2346b 0d 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0d 0a 20  ..  u.r = in;.. 
2346c 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0d 0a 20 20   t = u.i[0];..  
2346d 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b  u.i[0] = u.i[1];
2346e 0d 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0d  ..  u.i[1] = t;.
2346f 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0d 0a  .  return u.r;..
23470 7d 0d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70  }..# define swap
23471 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
23472 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77  (X)  X = floatSw
23473 61 70 28 58 29 0d 0a 23 65 6c 73 65 0d 0a 23 20  ap(X)..#else..# 
23474 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64  define swapMixed
23475 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 0d 0a  EndianFloat(X)..
23476 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
23477 20 57 72 69 74 65 20 74 68 65 20 73 65 72 69 61   Write the seria
23478 6c 69 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20  lized data blob 
23479 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74  for the value st
2347a 6f 72 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74  ored in pMem int
2347b 6f 20 0d 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69  o ..** buf. It i
2347c 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  s assumed that t
2347d 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c  he caller has al
2347e 6c 6f 63 61 74 65 64 20 73 75 66 66 69 63 69 65  located sufficie
2347f 6e 74 20 73 70 61 63 65 2e 0d 0a 2a 2a 20 52 65  nt space...** Re
23480 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
23481 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e  of bytes written
23482 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 6e 42 75 66 20 69  ...**..** nBuf i
23483 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  s the amount of 
23484 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 62 75  space left in bu
23485 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 74 20  f[].  nBuf must 
23486 61 6c 77 61 79 73 20 62 65 0d 0a 2a 2a 20 6c 61  always be..** la
23487 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  rge enough to ho
23488 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 69  ld the entire fi
23489 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 66  eld.  Except, if
2348a 20 74 68 65 20 66 69 65 6c 64 20 69 73 0d 0a 2a   the field is..*
2348b 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20  * a blob with a 
2348c 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
2348d 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67  , then buf[] mig
2348e 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 72  ht be just the r
2348f 69 67 68 74 0d 0a 2a 2a 20 73 69 7a 65 20 74 6f  ight..** size to
23490 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67   hold everything
23491 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20   except for the 
23492 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
23493 2e 20 20 49 66 20 62 75 66 5b 5d 0d 0a 2a 2a 20  .  If buf[]..** 
23494 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75  is only big enou
23495 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e  gh to hold the n
23496 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20  on-zero prefix, 
23497 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20  then only write 
23498 74 68 61 74 0d 0a 2a 2a 20 70 72 65 66 69 78 20  that..** prefix 
23499 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74  into buf[].  But
2349a 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72   if buf[] is lar
2349b 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ge enough to hol
2349c 64 20 62 6f 74 68 20 74 68 65 0d 0a 2a 2a 20 70  d both the..** p
2349d 72 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61  refix and the ta
2349e 69 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68  il then write th
2349f 65 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74  e prefix and set
234a0 20 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c   the tail to all
234a1 0d 0a 2a 2a 20 7a 65 72 6f 73 2e 0d 0a 2a 2a 0d  ..** zeros...**.
234a2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
234a3 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
234a4 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ctually written 
234a5 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65  into buf[].  The
234a6 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 6f 66 20 62   number..** of b
234a7 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f  ytes in the zero
234a8 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20  -filled tail is 
234a9 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
234aa 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c  return value onl
234ab 79 0d 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62  y..** if those b
234ac 79 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64  ytes were zeroed
234ad 20 69 6e 20 62 75 66 5b 5d 2e 0d 0a 2a 2f 20 0d   in buf[]...*/ .
234ae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
234af 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53  u32 sqlite3VdbeS
234b0 65 72 69 61 6c 50 75 74 28 75 38 20 2a 62 75 66  erialPut(u8 *buf
234b1 2c 20 69 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20  , int nBuf, Mem 
234b2 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f  *pMem, int file_
234b3 66 6f 72 6d 61 74 29 7b 0d 0a 20 20 75 33 32 20  format){..  u32 
234b4 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71  serial_type = sq
234b5 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
234b6 79 70 65 28 70 4d 65 6d 2c 20 66 69 6c 65 5f 66  ype(pMem, file_f
234b7 6f 72 6d 61 74 29 3b 0d 0a 20 20 75 33 32 20 6c  ormat);..  u32 l
234b8 65 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 74 65  en;....  /* Inte
234b9 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f 0d  ger and Real */.
234ba 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
234bb 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f  pe<=7 && serial_
234bc 74 79 70 65 3e 30 20 29 7b 0d 0a 20 20 20 20 75  type>0 ){..    u
234bd 36 34 20 76 3b 0d 0a 20 20 20 20 75 33 32 20 69  64 v;..    u32 i
234be 3b 0d 0a 20 20 20 20 69 66 28 20 73 65 72 69 61  ;..    if( seria
234bf 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0d 0a 20 20  l_type==7 ){..  
234c0 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
234c1 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d  of(v)==sizeof(pM
234c2 65 6d 2d 3e 72 29 20 29 3b 0d 0a 20 20 20 20 20  em->r) );..     
234c3 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65   memcpy(&v, &pMe
234c4 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29  m->r, sizeof(v))
234c5 3b 0d 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78  ;..      swapMix
234c6 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29  edEndianFloat(v)
234c7 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
234c8 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
234c9 2e 69 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  .i;..    }..    
234ca 6c 65 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65  len = i = sqlite
234cb 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
234cc 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  en(serial_type);
234cd 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65  ..    assert( le
234ce 6e 3c 3d 28 75 33 32 29 6e 42 75 66 20 29 3b 0d  n<=(u32)nBuf );.
234cf 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20  .    while( i-- 
234d0 29 7b 0d 0a 20 20 20 20 20 20 62 75 66 5b 69 5d  ){..      buf[i]
234d1 20 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 3b   = (u8)(v&0xFF);
234d2 0d 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b  ..      v >>= 8;
234d3 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74  ..    }..    ret
234d4 75 72 6e 20 6c 65 6e 3b 0d 0a 20 20 7d 0d 0a 0d  urn len;..  }...
234d5 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20  .  /* String or 
234d6 62 6c 6f 62 20 2a 2f 0d 0a 20 20 69 66 28 20 73  blob */..  if( s
234d7 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29  erial_type>=12 )
234d8 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
234d9 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d  Mem->n + ((pMem-
234da 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
234db 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f  o)?pMem->u.nZero
234dc 3a 30 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20  :0)..           
234dd 20 20 3d 3d 20 28 69 6e 74 29 73 71 6c 69 74 65    == (int)sqlite
234de 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
234df 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 20  en(serial_type) 
234e0 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
234e1 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b  pMem->n<=nBuf );
234e2 0d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d  ..    len = pMem
234e3 2d 3e 6e 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79  ->n;..    memcpy
234e4 28 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c  (buf, pMem->z, l
234e5 65 6e 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 4d  en);..    if( pM
234e6 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
234e7 5a 65 72 6f 20 29 7b 0d 0a 20 20 20 20 20 20 6c  Zero ){..      l
234e8 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a  en += pMem->u.nZ
234e9 65 72 6f 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  ero;..      asse
234ea 72 74 28 20 6e 42 75 66 3e 3d 30 20 29 3b 0d 0a  rt( nBuf>=0 );..
234eb 20 20 20 20 20 20 69 66 28 20 6c 65 6e 20 3e 20        if( len > 
234ec 28 75 33 32 29 6e 42 75 66 20 29 7b 0d 0a 20 20  (u32)nBuf ){..  
234ed 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75 33 32        len = (u32
234ee 29 6e 42 75 66 3b 0d 0a 20 20 20 20 20 20 7d 0d  )nBuf;..      }.
234ef 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62  .      memset(&b
234f0 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20  uf[pMem->n], 0, 
234f1 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0d 0a 20  len-pMem->n);.. 
234f2 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e     }..    return
234f3 20 6c 65 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20   len;..  }....  
234f4 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74  /* NULL or const
234f5 61 6e 74 73 20 30 20 6f 72 20 31 20 2a 2f 0d 0a  ants 0 or 1 */..
234f6 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a    return 0;..}..
234f7 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 73 65 72 69 61  ../*..** Deseria
234f8 6c 69 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c  lize the data bl
234f9 6f 62 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ob pointed to by
234fa 20 62 75 66 20 61 73 20 73 65 72 69 61 6c 20 74   buf as serial t
234fb 79 70 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0d  ype serial_type.
234fc 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
234fd 65 20 72 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d  e result in pMem
234fe 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  .  Return the nu
234ff 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
23500 61 64 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c 49 54 45  ad...*/ ..SQLITE
23501 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
23502 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
23503 74 28 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  t(..  const unsi
23504 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c 20  gned char *buf, 
23505 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f      /* Buffer to
23506 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72 6f   deserialize fro
23507 6d 20 2a 2f 0d 0a 20 20 75 33 32 20 73 65 72 69  m */..  u32 seri
23508 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20  al_type,        
23509 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20        /* Serial 
2350a 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c  type to deserial
2350b 69 7a 65 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70  ize */..  Mem *p
2350c 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  Mem             
2350d 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
2350e 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20  y cell to write 
2350f 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0d 0a 29  value into */..)
23510 7b 0d 0a 20 20 73 77 69 74 63 68 28 20 73 65 72  {..  switch( ser
23511 69 61 6c 5f 74 79 70 65 20 29 7b 0d 0a 20 20 20  ial_type ){..   
23512 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52   case 10:   /* R
23513 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75  eserved for futu
23514 72 65 20 75 73 65 20 2a 2f 0d 0a 20 20 20 20 63  re use */..    c
23515 61 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73  ase 11:   /* Res
23516 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65  erved for future
23517 20 75 73 65 20 2a 2f 0d 0a 20 20 20 20 63 61 73   use */..    cas
23518 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20  e 0: {  /* NULL 
23519 2a 2f 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  */..      pMem->
2351a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
2351b 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
2351c 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65  .    }..    case
2351d 20 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20   1: { /* 1-byte 
2351e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
2351f 2f 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  /..      pMem->u
23520 2e 69 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61  .i = (signed cha
23521 72 29 62 75 66 5b 30 5d 3b 0d 0a 20 20 20 20 20  r)buf[0];..     
23522 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
23523 45 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20 20 20 72  EM_Int;..      r
23524 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d  eturn 1;..    }.
23525 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 2f  .    case 2: { /
23526 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
23527 69 6e 74 65 67 65 72 20 2a 2f 0d 0a 20 20 20 20  integer */..    
23528 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28    pMem->u.i = ((
23529 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66  (signed char)buf
2352a 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31  [0])<<8) | buf[1
2352b 5d 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ];..      pMem->
2352c 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
2352d 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32  ..      return 2
2352e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2352f 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74  se 3: { /* 3-byt
23530 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
23531 20 2a 2f 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d   */..      pMem-
23532 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64  >u.i = (((signed
23533 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31   char)buf[0])<<1
23534 36 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29  6) | (buf[1]<<8)
23535 20 7c 20 62 75 66 5b 32 5d 3b 0d 0a 20 20 20 20   | buf[2];..    
23536 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
23537 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20 20 20  MEM_Int;..      
23538 72 65 74 75 72 6e 20 33 3b 0d 0a 20 20 20 20 7d  return 3;..    }
23539 0d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b 20  ..    case 4: { 
2353a 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 4-byte signed
2353b 20 69 6e 74 65 67 65 72 20 2a 2f 0d 0a 20 20 20   integer */..   
2353c 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
2353d 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62  buf[0]<<24) | (b
2353e 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75  uf[1]<<16) | (bu
2353f 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33  f[2]<<8) | buf[3
23540 5d 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ];..      pMem->
23541 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
23542 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 34  ..      return 4
23543 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
23544 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62 79 74  se 5: { /* 6-byt
23545 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
23546 20 2a 2f 0d 0a 20 20 20 20 20 20 75 36 34 20 78   */..      u64 x
23547 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61   = (((signed cha
23548 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20  r)buf[0])<<8) | 
23549 62 75 66 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 75  buf[1];..      u
2354a 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c  32 y = (buf[2]<<
2354b 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31  24) | (buf[3]<<1
2354c 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29  6) | (buf[4]<<8)
2354d 20 7c 20 62 75 66 5b 35 5d 3b 0d 0a 20 20 20 20   | buf[5];..    
2354e 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20    x = (x<<32) | 
2354f 79 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  y;..      pMem->
23550 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b  u.i = *(i64*)&x;
23551 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ..      pMem->fl
23552 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0d 0a  ags = MEM_Int;..
23553 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0d        return 6;.
23554 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65  .    }..    case
23555 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20   6:   /* 8-byte 
23556 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
23557 2f 0d 0a 20 20 20 20 63 61 73 65 20 37 3a 20 7b  /..    case 7: {
23558 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74 69 6e   /* IEEE floatin
23559 67 20 70 6f 69 6e 74 20 2a 2f 0d 0a 20 20 20 20  g point */..    
2355a 20 20 75 36 34 20 78 3b 0d 0a 20 20 20 20 20 20    u64 x;..      
2355b 75 33 32 20 79 3b 0d 0a 23 69 66 20 21 64 65 66  u32 y;..#if !def
2355c 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
2355d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2355e 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
2355f 49 4e 54 29 0d 0a 20 20 20 20 20 20 2f 2a 20 56  INT)..      /* V
23560 65 72 69 66 79 20 74 68 61 74 20 69 6e 74 65 67  erify that integ
23561 65 72 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67  ers and floating
23562 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73   point values us
23563 65 20 74 68 65 20 73 61 6d 65 0d 0a 20 20 20 20  e the same..    
23564 20 20 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e    ** byte order.
23565 20 20 4f 72 2c 20 74 68 61 74 20 69 66 20 53 51    Or, that if SQ
23566 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41  LITE_MIXED_ENDIA
23567 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 20 69 73  N_64BIT_FLOAT is
23568 0d 0a 20 20 20 20 20 20 2a 2a 20 64 65 66 69 6e  ..      ** defin
23569 65 64 20 74 68 61 74 20 36 34 2d 62 69 74 20 66  ed that 64-bit f
2356a 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
2356b 6c 75 65 73 20 72 65 61 6c 6c 79 20 61 72 65 20  lues really are 
2356c 6d 69 78 65 64 0d 0a 20 20 20 20 20 20 2a 2a 20  mixed..      ** 
2356d 65 6e 64 69 61 6e 2e 0d 0a 20 20 20 20 20 20 2a  endian...      *
2356e 2f 0d 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  /..      static 
2356f 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28  const u64 t1 = (
23570 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30 29  (u64)0x3ff00000)
23571 3c 3c 33 32 3b 0d 0a 20 20 20 20 20 20 73 74 61  <<32;..      sta
23572 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65  tic const double
23573 20 72 31 20 3d 20 31 2e 30 3b 0d 0a 20 20 20 20   r1 = 1.0;..    
23574 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0d 0a    u64 t2 = t1;..
23575 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
23576 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0d  ndianFloat(t2);.
23577 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
23578 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f  izeof(r1)==sizeo
23579 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28  f(t2) && memcmp(
2357a 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66  &r1, &t2, sizeof
2357b 28 72 31 29 29 3d 3d 30 20 29 3b 0d 0a 23 65 6e  (r1))==0 );..#en
2357c 64 69 66 0d 0a 0d 0a 20 20 20 20 20 20 78 20 3d  dif....      x =
2357d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20   (buf[0]<<24) | 
2357e 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28  (buf[1]<<16) | (
2357f 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[2]<<8) | buf
23580 5b 33 5d 3b 0d 0a 20 20 20 20 20 20 79 20 3d 20  [3];..      y = 
23581 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 28  (buf[4]<<24) | (
23582 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 62  buf[5]<<16) | (b
23583 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[6]<<8) | buf[
23584 37 5d 3b 0d 0a 20 20 20 20 20 20 78 20 3d 20 28  7];..      x = (
23585 78 3c 3c 33 32 29 20 7c 20 79 3b 0d 0a 20 20 20  x<<32) | y;..   
23586 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79     if( serial_ty
23587 70 65 3d 3d 36 20 29 7b 0d 0a 20 20 20 20 20 20  pe==6 ){..      
23588 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28    pMem->u.i = *(
23589 69 36 34 2a 29 26 78 3b 0d 0a 20 20 20 20 20 20  i64*)&x;..      
2358a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
2358b 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20 20 20  MEM_Int;..      
2358c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2358d 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 78  assert( sizeof(x
2358e 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66 28 70  )==8 && sizeof(p
2358f 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0d 0a 20  Mem->r)==8 );.. 
23590 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64         swapMixed
23591 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b 0d  EndianFloat(x);.
23592 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
23593 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69  &pMem->r, &x, si
23594 7a 65 6f 66 28 78 29 29 3b 0d 0a 20 20 20 20 20  zeof(x));..     
23595 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
23596 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d   sqlite3IsNaN(pM
23597 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c  em->r) ? MEM_Nul
23598 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0d 0a 20  l : MEM_Real;.. 
23599 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65       }..      re
2359a 74 75 72 6e 20 38 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 8;..    }..
2359b 20 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f      case 8:    /
2359c 2a 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0d 0a  * Integer 0 */..
2359d 20 20 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f      case 9: {  /
2359e 2a 20 49 6e 74 65 67 65 72 20 31 20 2a 2f 0d 0a  * Integer 1 */..
2359f 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
235a0 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b  = serial_type-8;
235a1 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ..      pMem->fl
235a2 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0d 0a  ags = MEM_Int;..
235a3 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d        return 0;.
235a4 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64 65 66 61  .    }..    defa
235a5 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20 75 33  ult: {..      u3
235a6 32 20 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f  2 len = (serial_
235a7 74 79 70 65 2d 31 32 29 2f 32 3b 0d 0a 20 20 20  type-12)/2;..   
235a8 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68     pMem->z = (ch
235a9 61 72 20 2a 29 62 75 66 3b 0d 0a 20 20 20 20 20  ar *)buf;..     
235aa 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0d   pMem->n = len;.
235ab 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  .      pMem->xDe
235ac 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66  l = 0;..      if
235ad 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 78  ( serial_type&0x
235ae 30 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  01 ){..        p
235af 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
235b0 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d  _Str | MEM_Ephem
235b1 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
235b2 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
235b3 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20  lags = MEM_Blob 
235b4 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0d 0a 20 20  | MEM_Ephem;..  
235b5 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74      }..      ret
235b6 75 72 6e 20 6c 65 6e 3b 0d 0a 20 20 20 20 7d 0d  urn len;..    }.
235b7 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30  .  }..  return 0
235b8 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
235b9 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
235ba 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
235bb 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65  sufficient space
235bc 20 66 6f 72 20 61 6e 20 55 6e 70 61 63 6b 65 64   for an Unpacked
235bd 52 65 63 6f 72 64 0d 0a 2a 2a 20 73 74 72 75 63  Record..** struc
235be 74 75 72 65 20 6c 61 72 67 65 20 65 6e 6f 75 67  ture large enoug
235bf 68 20 74 6f 20 62 65 20 75 73 65 64 20 77 69 74  h to be used wit
235c0 68 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  h sqlite3VdbeRec
235c1 6f 72 64 55 6e 70 61 63 6b 28 29 20 69 66 0d 0a  ordUnpack() if..
235c2 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67  ** the first arg
235c3 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
235c4 65 72 20 74 6f 20 4b 65 79 49 6e 66 6f 20 73 74  er to KeyInfo st
235c5 72 75 63 74 75 72 65 20 70 4b 65 79 49 6e 66 6f  ructure pKeyInfo
235c6 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 70  ...**..** The sp
235c7 61 63 65 20 69 73 20 65 69 74 68 65 72 20 61 6c  ace is either al
235c8 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 73 71  located using sq
235c9 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
235ca 28 29 20 6f 72 20 66 72 6f 6d 20 77 69 74 68 69  () or from withi
235cb 6e 0d 0a 2a 2a 20 74 68 65 20 75 6e 61 6c 69 67  n..** the unalig
235cc 6e 65 64 20 62 75 66 66 65 72 20 70 61 73 73 65  ned buffer passe
235cd 64 20 76 69 61 20 74 68 65 20 73 65 63 6f 6e 64  d via the second
235ce 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d   and third argum
235cf 65 6e 74 73 20 28 70 72 65 73 75 6d 61 62 6c 79  ents (presumably
235d0 0d 0a 2a 2a 20 73 74 61 63 6b 20 73 70 61 63 65  ..** stack space
235d1 29 2e 20 49 66 20 74 68 65 20 66 6f 72 6d 65 72  ). If the former
235d2 2c 20 74 68 65 6e 20 2a 70 70 46 72 65 65 20 69  , then *ppFree i
235d3 73 20 73 65 74 20 74 6f 20 61 20 70 6f 69 6e 74  s set to a point
235d4 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64 0d 0a  er that should..
235d5 2a 2a 20 62 65 20 65 76 65 6e 74 75 61 6c 6c 79  ** be eventually
235d6 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61   freed by the ca
235d7 6c 6c 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74  ller using sqlit
235d8 65 33 44 62 46 72 65 65 28 29 2e 20 4f 72 2c 20  e3DbFree(). Or, 
235d9 69 66 20 74 68 65 20 0d 0a 2a 2a 20 61 6c 6c 6f  if the ..** allo
235da 63 61 74 69 6f 6e 20 63 6f 6d 65 73 20 66 72 6f  cation comes fro
235db 6d 20 74 68 65 20 70 53 70 61 63 65 2f 73 7a 53  m the pSpace/szS
235dc 70 61 63 65 20 62 75 66 66 65 72 2c 20 2a 70 70  pace buffer, *pp
235dd 46 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 4e  Free is set to N
235de 55 4c 4c 0d 0a 2a 2a 20 62 65 66 6f 72 65 20 72  ULL..** before r
235df 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  eturning...**..*
235e0 2a 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f  * If an OOM erro
235e1 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
235e2 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d  s returned...*/.
235e3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
235e4 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
235e5 73 71 6c 69 74 65 33 56 64 62 65 41 6c 6c 6f 63  sqlite3VdbeAlloc
235e6 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 0d  UnpackedRecord(.
235e7 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
235e8 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Info,           
235e9 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 6f     /* Descriptio
235ea 6e 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  n of the record 
235eb 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70 53 70 61  */..  char *pSpa
235ec 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ce,             
235ed 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e        /* Unalign
235ee 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 62  ed space availab
235ef 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 53  le */..  int szS
235f0 70 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  pace,           
235f1 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
235f2 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 6e 20   of pSpace[] in 
235f3 62 79 74 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72  bytes */..  char
235f4 20 2a 2a 70 70 46 72 65 65 20 20 20 20 20 20 20   **ppFree       
235f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
235f6 55 54 3a 20 43 61 6c 6c 65 72 20 73 68 6f 75 6c  UT: Caller shoul
235f7 64 20 66 72 65 65 20 74 68 69 73 20 70 6f 69 6e  d free this poin
235f8 74 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 55 6e  ter */..){..  Un
235f9 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 3b  packedRecord *p;
235fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
235fb 20 55 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64   Unpacked record
235fc 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 20   to return */.. 
235fd 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20 20   int nOff;      
235fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
235ff 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 70 53   /* Increment pS
23600 70 61 63 65 20 62 79 20 6e 4f 66 66 20 74 6f 20  pace by nOff to 
23601 61 6c 69 67 6e 20 69 74 20 2a 2f 0d 0a 20 20 69  align it */..  i
23602 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
23603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23604 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
23605 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 2a  s required for *
23606 70 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 57 65 20  p */....  /* We 
23607 77 61 6e 74 20 74 6f 20 73 68 69 66 74 20 74 68  want to shift th
23608 65 20 70 6f 69 6e 74 65 72 20 70 53 70 61 63 65  e pointer pSpace
23609 20 75 70 20 73 75 63 68 20 74 68 61 74 20 69 74   up such that it
2360a 20 69 73 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   is 8-byte align
2360b 65 64 2e 0d 0a 20 20 2a 2a 20 54 68 75 73 2c 20  ed...  ** Thus, 
2360c 77 65 20 6e 65 65 64 20 74 6f 20 63 61 6c 63 75  we need to calcu
2360d 6c 61 74 65 20 61 20 76 61 6c 75 65 2c 20 6e 4f  late a value, nO
2360e 66 66 2c 20 62 65 74 77 65 65 6e 20 30 20 61 6e  ff, between 0 an
2360f 64 20 37 2c 20 74 6f 20 73 68 69 66 74 20 0d 0a  d 7, to shift ..
23610 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 66 20    ** it by.  If 
23611 70 53 70 61 63 65 20 69 73 20 61 6c 72 65 61 64  pSpace is alread
23612 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64  y 8-byte aligned
23613 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 62 65  , nOff should be
23614 20 7a 65 72 6f 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   zero...  */..  
23615 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c  nOff = (8 - (SQL
23616 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
23617 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 37  Space) & 7)) & 7
23618 3b 0d 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55  ;..  nByte = ROU
23619 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63  ND8(sizeof(Unpac
2361a 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69  kedRecord)) + si
2361b 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49  zeof(Mem)*(pKeyI
2361c 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 29 3b 0d  nfo->nField+1);.
2361d 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53  .  if( nByte>szS
2361e 70 61 63 65 2b 6e 4f 66 66 20 29 7b 0d 0a 20 20  pace+nOff ){..  
2361f 20 20 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52    p = (UnpackedR
23620 65 63 6f 72 64 20 2a 29 73 71 6c 69 74 65 33 44  ecord *)sqlite3D
23621 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49  bMallocRaw(pKeyI
23622 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b  nfo->db, nByte);
23623 0d 0a 20 20 20 20 2a 70 70 46 72 65 65 20 3d 20  ..    *ppFree = 
23624 28 63 68 61 72 20 2a 29 70 3b 0d 0a 20 20 20 20  (char *)p;..    
23625 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 20  if( !p ) return 
23626 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  0;..  }else{..  
23627 20 20 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52    p = (UnpackedR
23628 65 63 6f 72 64 2a 29 26 70 53 70 61 63 65 5b 6e  ecord*)&pSpace[n
23629 4f 66 66 5d 3b 0d 0a 20 20 20 20 2a 70 70 46 72  Off];..    *ppFr
2362a 65 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ee = 0;..  }....
2362b 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 28 4d 65 6d    p->aMem = (Mem
2362c 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b 52 4f  *)&((char*)p)[RO
2362d 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61  UND8(sizeof(Unpa
2362e 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b 0d 0a  ckedRecord))];..
2362f 20 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20    p->pKeyInfo = 
23630 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 70 2d 3e  pKeyInfo;..  p->
23631 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66  nField = pKeyInf
23632 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0d 0a  o->nField + 1;..
23633 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a    return p;..}..
23634 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65 6e 20 74  ../*..** Given t
23635 68 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63  he nKey-byte enc
23636 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72  oding of a recor
23637 64 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 6f 70  d in pKey[], pop
23638 75 6c 61 74 65 20 74 68 65 20 0d 0a 2a 2a 20 55  ulate the ..** U
23639 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74  npackedRecord st
2363a 72 75 63 74 75 72 65 20 69 6e 64 69 63 61 74 65  ructure indicate
2363b 64 20 62 79 20 74 68 65 20 66 6f 75 72 74 68 20  d by the fourth 
2363c 61 72 67 75 6d 65 6e 74 20 77 69 74 68 20 74 68  argument with th
2363d 65 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  e..** contents o
2363e 66 20 74 68 65 20 64 65 63 6f 64 65 64 20 72 65  f the decoded re
2363f 63 6f 72 64 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c 49  cord...*/ ..SQLI
23640 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23641 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
23642 64 55 6e 70 61 63 6b 28 0d 0a 20 20 4b 65 79 49  dUnpack(..  KeyI
23643 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20  nfo *pKeyInfo,  
23644 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
23645 6e 20 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f  n about the reco
23646 72 64 20 66 6f 72 6d 61 74 20 2a 2f 0d 0a 20 20  rd format */..  
23647 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20  int nKey,       
23648 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
23649 66 20 74 68 65 20 62 69 6e 61 72 79 20 72 65 63  f the binary rec
2364a 6f 72 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ord */..  const 
2364b 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 20 20  void *pKey,     
2364c 20 2f 2a 20 54 68 65 20 62 69 6e 61 72 79 20 72   /* The binary r
2364d 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 55 6e 70 61  ecord */..  Unpa
2364e 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 20 20 20  ckedRecord *p   
2364f 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
23650 68 69 73 20 73 74 72 75 63 74 75 72 65 20 62 65  his structure be
23651 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20  fore returning. 
23652 2a 2f 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20  */..){..  const 
23653 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
23654 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73  Key = (const uns
23655 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65  igned char *)pKe
23656 79 3b 0d 0a 20 20 69 6e 74 20 64 3b 20 0d 0a 20  y;..  int d; .. 
23657 20 75 33 32 20 69 64 78 3b 20 20 20 20 20 20 20   u32 idx;       
23658 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23659 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 61 4b   /* Offset in aK
2365a 65 79 5b 5d 20 74 6f 20 72 65 61 64 20 66 72 6f  ey[] to read fro
2365b 6d 20 2a 2f 0d 0a 20 20 75 31 36 20 75 3b 20 20  m */..  u16 u;  
2365c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2365d 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67          /* Unsig
2365e 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72  ned loop counter
2365f 20 2a 2f 0d 0a 20 20 75 33 32 20 73 7a 48 64 72   */..  u32 szHdr
23660 3b 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d  ;..  Mem *pMem =
23661 20 70 2d 3e 61 4d 65 6d 3b 0d 0a 0d 0a 20 20 70   p->aMem;....  p
23662 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0d 0a 20 20  ->flags = 0;..  
23663 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
23664 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65  TE_ALIGNMENT(pMe
23665 6d 29 20 29 3b 0d 0a 20 20 69 64 78 20 3d 20 67  m) );..  idx = g
23666 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c  etVarint32(aKey,
23667 20 73 7a 48 64 72 29 3b 0d 0a 20 20 64 20 3d 20   szHdr);..  d = 
23668 73 7a 48 64 72 3b 0d 0a 20 20 75 20 3d 20 30 3b  szHdr;..  u = 0;
23669 0d 0a 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73  ..  while( idx<s
2366a 7a 48 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69  zHdr && u<p->nFi
2366b 65 6c 64 20 26 26 20 64 3c 3d 6e 4b 65 79 20 29  eld && d<=nKey )
2366c 7b 0d 0a 20 20 20 20 75 33 32 20 73 65 72 69 61  {..    u32 seria
2366d 6c 5f 74 79 70 65 3b 0d 0a 0d 0a 20 20 20 20 69  l_type;....    i
2366e 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33  dx += getVarint3
2366f 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 65  2(&aKey[idx], se
23670 72 69 61 6c 5f 74 79 70 65 29 3b 0d 0a 20 20 20  rial_type);..   
23671 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 4b 65   pMem->enc = pKe
23672 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0d 0a 20 20 20  yInfo->enc;..   
23673 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 79   pMem->db = pKey
23674 49 6e 66 6f 2d 3e 64 62 3b 0d 0a 20 20 20 20 2f  Info->db;..    /
23675 2a 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20  * pMem->flags = 
23676 30 3b 20 2f 2f 20 73 71 6c 69 74 65 33 56 64 62  0; // sqlite3Vdb
23677 65 53 65 72 69 61 6c 47 65 74 28 29 20 77 69 6c  eSerialGet() wil
23678 6c 20 73 65 74 20 74 68 69 73 20 66 6f 72 20 75  l set this for u
23679 73 20 2a 2f 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e  s */..    pMem->
2367a 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a 20 20  zMalloc = 0;..  
2367b 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 64    d += sqlite3Vd
2367c 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65  beSerialGet(&aKe
2367d 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  y[d], serial_typ
2367e 65 2c 20 70 4d 65 6d 29 3b 0d 0a 20 20 20 20 70  e, pMem);..    p
2367f 4d 65 6d 2b 2b 3b 0d 0a 20 20 20 20 75 2b 2b 3b  Mem++;..    u++;
23680 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
23681 20 75 3c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46   u<=pKeyInfo->nF
23682 69 65 6c 64 20 2b 20 31 20 29 3b 0d 0a 20 20 70  ield + 1 );..  p
23683 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 3b 0d 0a 7d  ->nField = u;..}
23684 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
23685 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 65  function compare
23686 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 20  s the two table 
23687 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72 65  rows or index re
23688 63 6f 72 64 73 0d 0a 2a 2a 20 73 70 65 63 69 66  cords..** specif
23689 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70  ied by {nKey1, p
2368a 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32  Key1} and pPKey2
2368b 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  .  It returns a 
2368c 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0d 0a  negative, zero..
2368d 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ** or positive i
2368e 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69  nteger if key1 i
2368f 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
23690 61 6c 20 74 6f 20 6f 72 20 0d 0a 2a 2a 20 67 72  al to or ..** gr
23691 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e  eater than key2.
23692 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b    The {nKey1, pK
23693 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65  ey1} key must be
23694 20 61 20 62 6c 6f 62 0d 0a 2a 2a 20 63 72 65 61   a blob..** crea
23695 74 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b  ted by th OP_Mak
23696 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f  eRecord opcode o
23697 66 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65  f the VDBE.  The
23698 20 70 50 4b 65 79 32 0d 0a 2a 2a 20 6b 65 79 20   pPKey2..** key 
23699 6d 75 73 74 20 62 65 20 61 20 70 61 72 73 65 64  must be a parsed
2369a 20 6b 65 79 20 73 75 63 68 20 61 73 20 6f 62 74   key such as obt
2369b 61 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 73  ained from..** s
2369c 71 6c 69 74 65 33 56 64 62 65 50 61 72 73 65 52  qlite3VdbeParseR
2369d 65 63 6f 72 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4b  ecord...**..** K
2369e 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f 20  ey1 and Key2 do 
2369f 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e 74  not have to cont
236a0 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ain the same num
236a1 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0d 0a  ber of fields...
236a2 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20  ** The key with 
236a3 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20  fewer fields is 
236a4 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73  usually compares
236a5 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0d   less than the .
236a6 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20  .** longer key. 
236a7 20 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20   However if the 
236a8 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
236a9 20 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32   flags in pPKey2
236aa 20 69 73 20 73 65 74 0d 0a 2a 2a 20 61 6e 64 20   is set..** and 
236ab 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69  the common prefi
236ac 78 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74  xes are equal, t
236ad 68 65 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73  hen key1 is less
236ae 20 74 68 61 6e 20 6b 65 79 32 2e 0d 0a 2a 2a 20   than key2...** 
236af 4f 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b  Or if the UNPACK
236b0 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 58 20  ED_MATCH_PREFIX 
236b1 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20  flag is set and 
236b2 74 68 65 20 70 72 65 66 69 78 65 73 20 61 72 65  the prefixes are
236b3 0d 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 6e  ..** equal, then
236b4 20 74 68 65 20 6b 65 79 73 20 61 72 65 20 63 6f   the keys are co
236b5 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 65  nsidered to be e
236b6 71 75 61 6c 20 61 6e 64 0d 0a 2a 2a 20 74 68 65  qual and..** the
236b7 20 70 61 72 74 73 20 62 65 79 6f 6e 64 20 74 68   parts beyond th
236b8 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20  e common prefix 
236b9 61 72 65 20 69 67 6e 6f 72 65 64 2e 0d 0a 2a 2f  are ignored...*/
236ba 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
236bb 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
236bc 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0d 0a  RecordCompare(..
236bd 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e    int nKey1, con
236be 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20  st void *pKey1, 
236bf 2f 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0d 0a  /* Left key */..
236c0 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
236c1 20 2a 70 50 4b 65 79 32 20 20 20 20 20 20 20 20   *pPKey2        
236c2 2f 2a 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0d  /* Right key */.
236c3 0a 29 7b 0d 0a 20 20 69 6e 74 20 64 31 3b 20 20  .){..  int d1;  
236c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
236c5 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20  set into aKey[] 
236c6 6f 66 20 6e 65 78 74 20 64 61 74 61 20 65 6c 65  of next data ele
236c7 6d 65 6e 74 20 2a 2f 0d 0a 20 20 75 33 32 20 69  ment */..  u32 i
236c8 64 78 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  dx1;          /*
236c9 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65   Offset into aKe
236ca 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 65 61 64  y[] of next head
236cb 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f 0d 0a 20  er element */.. 
236cc 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 20 20   u32 szHdr1;    
236cd 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
236ce 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 65 72   bytes in header
236cf 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 20 3d 20 30   */..  int i = 0
236d0 3b 0d 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  ;..  int nField;
236d1 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0d  ..  int rc = 0;.
236d2 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
236d3 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20  d char *aKey1 = 
236d4 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
236d5 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0d 0a 20  char *)pKey1;.. 
236d6 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
236d7 66 6f 3b 0d 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b  fo;..  Mem mem1;
236d8 0d 0a 0d 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d  ....  pKeyInfo =
236d9 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66   pPKey2->pKeyInf
236da 6f 3b 0d 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d  o;..  mem1.enc =
236db 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0d   pKeyInfo->enc;.
236dc 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65  .  mem1.db = pKe
236dd 79 49 6e 66 6f 2d 3e 64 62 3b 0d 0a 20 20 2f 2a  yInfo->db;..  /*
236de 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b   mem1.flags = 0;
236df 20 20 2f 2f 20 57 69 6c 6c 20 62 65 20 69 6e 69    // Will be ini
236e0 74 69 61 6c 69 7a 65 64 20 62 79 20 73 71 6c 69  tialized by sqli
236e1 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
236e2 28 29 20 2a 2f 0d 0a 20 20 56 56 41 5f 4f 4e 4c  () */..  VVA_ONL
236e3 59 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20  Y( mem1.zMalloc 
236e4 3d 20 30 3b 20 29 20 2f 2a 20 4f 6e 6c 79 20 6e  = 0; ) /* Only n
236e5 65 65 64 65 64 20 62 79 20 61 73 73 65 72 74 28  eeded by assert(
236e6 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0d  ) statements */.
236e7 0a 0d 0a 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 72  ...  /* Compiler
236e8 73 20 6d 61 79 20 63 6f 6d 70 6c 61 69 6e 20 74  s may complain t
236e9 68 61 74 20 6d 65 6d 31 2e 75 2e 69 20 69 73 20  hat mem1.u.i is 
236ea 70 6f 74 65 6e 74 69 61 6c 6c 79 20 75 6e 69 6e  potentially unin
236eb 69 74 69 61 6c 69 7a 65 64 2e 0d 0a 20 20 2a 2a  itialized...  **
236ec 20 57 65 20 63 6f 75 6c 64 20 69 6e 69 74 69 61   We could initia
236ed 6c 69 7a 65 20 69 74 2c 20 61 73 20 73 68 6f 77  lize it, as show
236ee 6e 20 68 65 72 65 2c 20 74 6f 20 73 69 6c 65 6e  n here, to silen
236ef 63 65 20 74 68 6f 73 65 20 63 6f 6d 70 6c 61 69  ce those complai
236f0 6e 74 73 2e 0d 0a 20 20 2a 2a 20 42 75 74 20 69  nts...  ** But i
236f1 6e 20 66 61 63 74 2c 20 6d 65 6d 31 2e 75 2e 69  n fact, mem1.u.i
236f2 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 63 74 75   will never actu
236f3 61 6c 6c 79 20 62 65 20 75 73 65 64 20 75 6e 69  ally be used uni
236f4 6e 69 74 69 61 6c 69 7a 65 64 2c 20 61 6e 64 20  nitialized, and 
236f5 64 6f 69 6e 67 20 0d 0a 20 20 2a 2a 20 74 68 65  doing ..  ** the
236f6 20 75 6e 6e 65 63 65 73 73 61 72 79 20 69 6e 69   unnecessary ini
236f7 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73 20  tialization has 
236f8 61 20 6d 65 61 73 75 72 61 62 6c 65 20 6e 65 67  a measurable neg
236f9 61 74 69 76 65 20 70 65 72 66 6f 72 6d 61 6e 63  ative performanc
236fa 65 0d 0a 20 20 2a 2a 20 69 6d 70 61 63 74 2c 20  e..  ** impact, 
236fb 73 69 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69  since this routi
236fc 6e 65 20 69 73 20 61 20 76 65 72 79 20 68 69 67  ne is a very hig
236fd 68 20 72 75 6e 6e 65 72 2e 20 20 41 6e 64 20 73  h runner.  And s
236fe 6f 2c 20 77 65 20 63 68 6f 6f 73 65 0d 0a 20 20  o, we choose..  
236ff 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65  ** to ignore the
23700 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
23701 67 73 20 61 6e 64 20 6c 65 61 76 65 20 74 68 69  gs and leave thi
23702 73 20 76 61 72 69 61 62 6c 65 20 75 6e 69 6e 69  s variable unini
23703 74 69 61 6c 69 7a 65 64 2e 0d 0a 20 20 2a 2f 0d  tialized...  */.
23704 0a 20 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e 69 20  .  /*  mem1.u.i 
23705 3d 20 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e 65 65  = 0;  // not nee
23706 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c  ded, here to sil
23707 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61  ence compiler wa
23708 72 6e 69 6e 67 20 2a 2f 0d 0a 20 20 0d 0a 20 20  rning */..  ..  
23709 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e 74  idx1 = getVarint
2370a 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 31  32(aKey1, szHdr1
2370b 29 3b 0d 0a 20 20 64 31 20 3d 20 73 7a 48 64 72  );..  d1 = szHdr
2370c 31 3b 0d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70  1;..  nField = p
2370d 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b  KeyInfo->nField;
2370e 0d 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c  ..  while( idx1<
2370f 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65  szHdr1 && i<pPKe
23710 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0d 0a 20  y2->nField ){.. 
23711 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79     u32 serial_ty
23712 70 65 31 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52  pe1;....    /* R
23713 65 61 64 20 74 68 65 20 73 65 72 69 61 6c 20 74  ead the serial t
23714 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78  ypes for the nex
23715 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63  t element in eac
23716 68 20 6b 65 79 2e 20 2a 2f 0d 0a 20 20 20 20 69  h key. */..    i
23717 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69 6e 74  dx1 += getVarint
23718 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20  32( aKey1+idx1, 
23719 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0d  serial_type1 );.
2371a 0a 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65  .    if( d1>=nKe
2371b 79 31 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  y1 && sqlite3Vdb
2371c 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73  eSerialTypeLen(s
2371d 65 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29  erial_type1)>0 )
2371e 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 2f   break;....    /
2371f 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61  * Extract the va
23720 6c 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 61  lues to be compa
23721 72 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  red...    */..  
23722 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33 56    d1 += sqlite3V
23723 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b  dbeSerialGet(&aK
23724 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f  ey1[d1], serial_
23725 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0d 0a  type1, &mem1);..
23726 0d 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
23727 63 6f 6d 70 61 72 69 73 6f 6e 0d 0a 20 20 20 20  comparison..    
23728 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  */..    rc = sql
23729 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26  ite3MemCompare(&
2372a 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61  mem1, &pPKey2->a
2372b 4d 65 6d 5b 69 5d 2c 0d 0a 20 20 20 20 20 20 20  Mem[i],..       
2372c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2372d 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70      i<nField ? p
2372e 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
2372f 5d 20 3a 20 30 29 3b 0d 0a 20 20 20 20 69 66 28  ] : 0);..    if(
23730 20 72 63 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20   rc!=0 ){..     
23731 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d   assert( mem1.zM
23732 61 6c 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20  alloc==0 );  /* 
23733 53 65 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f  See comment belo
23734 77 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  w */....      /*
23735 20 49 6e 76 65 72 74 20 74 68 65 20 72 65 73 75   Invert the resu
23736 6c 74 20 69 66 20 77 65 20 61 72 65 20 75 73 69  lt if we are usi
23737 6e 67 20 44 45 53 43 20 73 6f 72 74 20 6f 72 64  ng DESC sort ord
23738 65 72 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66  er. */..      if
23739 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  ( pKeyInfo->aSor
2373a 74 4f 72 64 65 72 20 26 26 20 69 3c 6e 46 69 65  tOrder && i<nFie
2373b 6c 64 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e  ld && pKeyInfo->
2373c 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b  aSortOrder[i] ){
2373d 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 2d  ..        rc = -
2373e 72 63 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  rc;..      }..  
2373f 20 20 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20    ..      /* If 
23740 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43  the PREFIX_SEARC
23741 48 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  H flag is set an
23742 64 20 61 6c 6c 20 66 69 65 6c 64 73 20 65 78 63  d all fields exc
23743 65 70 74 20 74 68 65 20 66 69 6e 61 6c 0d 0a 20  ept the final.. 
23744 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20 66 69       ** rowid fi
23745 65 6c 64 20 77 65 72 65 20 65 71 75 61 6c 2c 20  eld were equal, 
23746 74 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 50  then clear the P
23747 52 45 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61  REFIX_SEARCH fla
23748 67 20 61 6e 64 20 73 65 74 20 0d 0a 20 20 20 20  g and set ..    
23749 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77    ** pPKey2->row
2374a 69 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  id to the value 
2374b 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 69 65  of the rowid fie
2374c 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b  ld in (pKey1, nK
2374d 65 79 31 29 2e 0d 0a 20 20 20 20 20 20 2a 2a 20  ey1)...      ** 
2374e 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
2374f 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  the OP_IsUnique 
23750 6f 70 63 6f 64 65 2e 0d 0a 20 20 20 20 20 20 2a  opcode...      *
23751 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70 50  /..      if( (pP
23752 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e  Key2->flags & UN
23753 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45  PACKED_PREFIX_SE
23754 41 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b  ARCH) && i==(pPK
23755 65 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29  ey2->nField-1) )
23756 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  {..        asser
23757 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 20  t( idx1==szHdr1 
23758 26 26 20 72 63 20 29 3b 0d 0a 20 20 20 20 20 20  && rc );..      
23759 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 66    assert( mem1.f
2375a 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
2375b 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 4b 65 79  ;..        pPKey
2375c 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50  2->flags &= ~UNP
2375d 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
2375e 52 43 48 3b 0d 0a 20 20 20 20 20 20 20 20 70 50  RCH;..        pP
2375f 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65  Key2->rowid = me
23760 6d 31 2e 75 2e 69 3b 0d 0a 20 20 20 20 20 20 7d  m1.u.i;..      }
23761 0d 0a 20 20 20 20 0d 0a 20 20 20 20 20 20 72 65  ..    ..      re
23762 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d  turn rc;..    }.
23763 0a 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 7d 0d 0a  .    i++;..  }..
23764 0d 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79  ..  /* No memory
23765 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65   allocation is e
23766 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31  ver used on mem1
23767 2e 20 20 50 72 6f 76 65 20 74 68 69 73 20 75 73  .  Prove this us
23768 69 6e 67 0d 0a 20 20 2a 2a 20 74 68 65 20 66 6f  ing..  ** the fo
23769 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29  llowing assert()
2376a 2e 20 20 49 66 20 74 68 65 20 61 73 73 65 72 74  .  If the assert
2376b 28 29 20 66 61 69 6c 73 2c 20 69 74 20 69 6e 64  () fails, it ind
2376c 69 63 61 74 65 73 20 61 0d 0a 20 20 2a 2a 20 6d  icates a..  ** m
2376d 65 6d 6f 72 79 20 6c 65 61 6b 20 61 6e 64 20 61  emory leak and a
2376e 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71   need to call sq
2376f 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
23770 61 73 65 28 26 6d 65 6d 31 29 2e 0d 0a 20 20 2a  ase(&mem1)...  *
23771 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  /..  assert( mem
23772 31 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b 0d  1.zMalloc==0 );.
23773 0a 0d 0a 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65  ...  /* rc==0 he
23774 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e  re means that on
23775 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61  e of the keys ra
23776 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20  n out of fields 
23777 61 6e 64 0d 0a 20 20 2a 2a 20 61 6c 6c 20 74 68  and..  ** all th
23778 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74  e fields up to t
23779 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 65  hat point were e
2377a 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e 50  qual. If the UNP
2377b 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0d 0a 20  ACKED_INCRKEY.. 
2377c 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c   ** flag is set,
2377d 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20   then break the 
2377e 74 69 65 20 62 79 20 74 72 65 61 74 69 6e 67 20  tie by treating 
2377f 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0d  key2 as larger..
23780 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41  .  ** If the UPA
23781 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43  CKED_PREFIX_MATC
23782 48 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  H flag is set, t
23783 68 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f  hen keys with co
23784 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 0d 0a 20  mmon prefixes.. 
23785 20 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72   ** are consider
23786 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20  ed to be equal. 
23787 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
23788 6c 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68  longer key is th
23789 65 20 0d 0a 20 20 2a 2a 20 6c 61 72 67 65 72 2e  e ..  ** larger.
2378a 20 20 41 73 20 69 74 20 68 61 70 70 65 6e 73 2c    As it happens,
2378b 20 74 68 65 20 70 50 4b 65 79 32 20 77 69 6c 6c   the pPKey2 will
2378c 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 6c   always be the l
2378d 6f 6e 67 65 72 0d 0a 20 20 2a 2a 20 69 66 20 74  onger..  ** if t
2378e 68 65 72 65 20 69 73 20 61 20 64 69 66 66 65 72  here is a differ
2378f 65 6e 63 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61  ence...  */..  a
23790 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29 3b 0d  ssert( rc==0 );.
23791 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66  .  if( pPKey2->f
23792 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
23793 49 4e 43 52 4b 45 59 20 29 7b 0d 0a 20 20 20 20  INCRKEY ){..    
23794 72 63 20 3d 20 2d 31 3b 0d 0a 20 20 7d 65 6c 73  rc = -1;..  }els
23795 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c  e if( pPKey2->fl
23796 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50  ags & UNPACKED_P
23797 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b 0d 0a  REFIX_MATCH ){..
23798 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d      /* Leave rc=
23799 3d 30 20 2a 2f 0d 0a 20 20 7d 65 6c 73 65 20 69  =0 */..  }else i
2379a 66 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29  f( idx1<szHdr1 )
2379b 7b 0d 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a  {..    rc = 1;..
2379c 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
2379d 3b 0d 0a 7d 0d 0a 20 0d 0a 0d 0a 2f 2a 0d 0a 2a  ;..}.. ..../*..*
2379e 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74  * pCur points at
2379f 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 20   an index entry 
237a0 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68  created using th
237a1 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  e OP_MakeRecord 
237a2 6f 70 63 6f 64 65 2e 0d 0a 2a 2a 20 52 65 61 64  opcode...** Read
237a3 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20   the rowid (the 
237a4 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68  last field in th
237a5 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74  e record) and st
237a6 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64  ore it in *rowid
237a7 2e 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  ...** Return SQL
237a8 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
237a9 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61  hing works, or a
237aa 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68  n error code oth
237ab 65 72 77 69 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  erwise...**..** 
237ac 70 43 75 72 20 6d 69 67 68 74 20 62 65 20 70 6f  pCur might be po
237ad 69 6e 74 69 6e 67 20 74 6f 20 74 65 78 74 20 6f  inting to text o
237ae 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 63  btained from a c
237af 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20  orrupt database 
237b0 66 69 6c 65 2e 0d 0a 2a 2a 20 53 6f 20 74 68 65  file...** So the
237b1 20 63 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74 20   content cannot 
237b2 62 65 20 74 72 75 73 74 65 64 2e 20 20 44 6f 20  be trusted.  Do 
237b3 61 70 70 72 6f 70 72 69 61 74 65 20 63 68 65 63  appropriate chec
237b4 6b 73 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ks on the conten
237b5 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  t...*/..SQLITE_P
237b6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
237b7 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 73  e3VdbeIdxRowid(s
237b8 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 43 75  qlite3 *db, BtCu
237b9 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20  rsor *pCur, i64 
237ba 2a 72 6f 77 69 64 29 7b 0d 0a 20 20 69 36 34 20  *rowid){..  i64 
237bb 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0d 0a 20  nCellKey = 0;.. 
237bc 20 69 6e 74 20 72 63 3b 0d 0a 20 20 75 33 32 20   int rc;..  u32 
237bd 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a  szHdr;        /*
237be 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61   Size of the hea
237bf 64 65 72 20 2a 2f 0d 0a 20 20 75 33 32 20 74 79  der */..  u32 ty
237c0 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53  peRowid;    /* S
237c1 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68  erial type of th
237c2 65 20 72 6f 77 69 64 20 2a 2f 0d 0a 20 20 75 33  e rowid */..  u3
237c3 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20 20  2 lenRowid;     
237c4 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 72  /* Size of the r
237c5 6f 77 69 64 20 2a 2f 0d 0a 20 20 4d 65 6d 20 6d  owid */..  Mem m
237c6 2c 20 76 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44  , v;....  UNUSED
237c7 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29 3b 0d  _PARAMETER(db);.
237c8 0a 0d 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  ...  /* Get the 
237c9 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 64 65  size of the inde
237ca 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 20 69  x entry.  Only i
237cb 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 20 6f  ndices entries o
237cc 66 20 6c 65 73 73 0d 0a 20 20 2a 2a 20 74 68 61  f less..  ** tha
237cd 6e 20 32 47 69 42 20 61 72 65 20 73 75 70 70 6f  n 2GiB are suppo
237ce 72 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c 61  rt - anything la
237cf 72 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 61  rge must be data
237d0 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
237d1 0d 0a 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75  ..  ** Any corru
237d2 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65  ption is detecte
237d3 64 20 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65  d in sqlite3Btre
237d4 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c  eParseCellPtr(),
237d5 20 74 68 6f 75 67 68 2c 20 73 6f 0d 0a 20 20 2a   though, so..  *
237d6 2a 20 74 68 69 73 20 63 6f 64 65 20 63 61 6e 20  * this code can 
237d7 73 61 66 65 6c 79 20 61 73 73 75 6d 65 20 74 68  safely assume th
237d8 61 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33  at nCellKey is 3
237d9 32 2d 62 69 74 73 20 20 0d 0a 20 20 2a 2f 0d 0a  2-bits  ..  */..
237da 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
237db 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61  3BtreeCursorIsVa
237dc 6c 69 64 28 70 43 75 72 29 20 29 3b 0d 0a 20 20  lid(pCur) );..  
237dd 56 56 41 5f 4f 4e 4c 59 28 72 63 20 3d 29 20 73  VVA_ONLY(rc =) s
237de 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
237df 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b  ze(pCur, &nCellK
237e0 65 79 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ey);..  assert( 
237e1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
237e2 20 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20       /* pCur is 
237e3 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20  always valid so 
237e4 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66  KeySize cannot f
237e5 61 69 6c 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ail */..  assert
237e6 28 20 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51  ( (nCellKey & SQ
237e7 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
237e8 75 36 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0d  u64)nCellKey );.
237e9 0a 0d 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20  ...  /* Read in 
237ea 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e  the complete con
237eb 74 65 6e 74 20 6f 66 20 74 68 65 20 69 6e 64 65  tent of the inde
237ec 78 20 65 6e 74 72 79 20 2a 2f 0d 0a 20 20 6d 65  x entry */..  me
237ed 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
237ee 6f 66 28 6d 29 29 3b 0d 0a 20 20 72 63 20 3d 20  of(m));..  rc = 
237ef 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
237f0 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c  omBtree(pCur, 0,
237f1 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20   (int)nCellKey, 
237f2 31 2c 20 26 6d 29 3b 0d 0a 20 20 69 66 28 20 72  1, &m);..  if( r
237f3 63 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  c ){..    return
237f4 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f   rc;..  }....  /
237f5 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72  * The index entr
237f6 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74  y must begin wit
237f7 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20  h a header size 
237f8 2a 2f 0d 0a 20 20 28 76 6f 69 64 29 67 65 74 56  */..  (void)getV
237f9 61 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a  arint32((u8*)m.z
237fa 2c 20 73 7a 48 64 72 29 3b 0d 0a 20 20 74 65 73  , szHdr);..  tes
237fb 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 33 20  tcase( szHdr==3 
237fc 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
237fd 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0d 0a 20  szHdr==m.n );.. 
237fe 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a   if( unlikely(sz
237ff 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 29 73 7a  Hdr<3 || (int)sz
23800 48 64 72 3e 6d 2e 6e 29 20 29 7b 0d 0a 20 20 20  Hdr>m.n) ){..   
23801 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f   goto idx_rowid_
23802 63 6f 72 72 75 70 74 69 6f 6e 3b 0d 0a 20 20 7d  corruption;..  }
23803 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73  ....  /* The las
23804 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69  t field of the i
23805 6e 64 65 78 20 73 68 6f 75 6c 64 20 62 65 20 61  ndex should be a
23806 6e 20 69 6e 74 65 67 65 72 20 2d 20 74 68 65 20  n integer - the 
23807 52 4f 57 49 44 2e 0d 0a 20 20 2a 2a 20 56 65 72  ROWID...  ** Ver
23808 69 66 79 20 74 68 61 74 20 74 68 65 20 6c 61 73  ify that the las
23809 74 20 65 6e 74 72 79 20 72 65 61 6c 6c 79 20 69  t entry really i
2380a 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 2a 2f  s an integer. */
2380b 0d 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  ..  (void)getVar
2380c 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b  int32((u8*)&m.z[
2380d 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f  szHdr-1], typeRo
2380e 77 69 64 29 3b 0d 0a 20 20 74 65 73 74 63 61 73  wid);..  testcas
2380f 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 31 20  e( typeRowid==1 
23810 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
23811 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b 0d  typeRowid==2 );.
23812 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
23813 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0d 0a 20 20  eRowid==3 );..  
23814 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f  testcase( typeRo
23815 77 69 64 3d 3d 34 20 29 3b 0d 0a 20 20 74 65 73  wid==4 );..  tes
23816 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
23817 3d 3d 35 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  ==5 );..  testca
23818 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36  se( typeRowid==6
23819 20 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28   );..  testcase(
2381a 20 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b   typeRowid==8 );
2381b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79  ..  testcase( ty
2381c 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0d 0a 20  peRowid==9 );.. 
2381d 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79   if( unlikely(ty
2381e 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70  peRowid<1 || typ
2381f 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70 65  eRowid>9 || type
23820 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0d 0a 20 20  Rowid==7) ){..  
23821 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64    goto idx_rowid
23822 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0d 0a 20 20  _corruption;..  
23823 7d 0d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20  }..  lenRowid = 
23824 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
23825 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77  lTypeLen(typeRow
23826 69 64 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65  id);..  testcase
23827 28 20 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64  ( (u32)m.n==szHd
23828 72 2b 6c 65 6e 52 6f 77 69 64 20 29 3b 0d 0a 20  r+lenRowid );.. 
23829 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75   if( unlikely((u
2382a 33 32 29 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e  32)m.n<szHdr+len
2382b 52 6f 77 69 64 29 20 29 7b 0d 0a 20 20 20 20 67  Rowid) ){..    g
2382c 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f  oto idx_rowid_co
2382d 72 72 75 70 74 69 6f 6e 3b 0d 0a 20 20 7d 0d 0a  rruption;..  }..
2382e 0d 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65  ..  /* Fetch the
2382f 20 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65   integer off the
23830 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65   end of the inde
23831 78 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 73  x record */..  s
23832 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
23833 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e  Get((u8*)&m.z[m.
23834 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70  n-lenRowid], typ
23835 65 52 6f 77 69 64 2c 20 26 76 29 3b 0d 0a 20 20  eRowid, &v);..  
23836 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0d  *rowid = v.u.i;.
23837 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
23838 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0d 0a 20  mRelease(&m);.. 
23839 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2383a 4b 3b 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20  K;....  /* Jump 
2383b 68 65 72 65 20 69 66 20 64 61 74 61 62 61 73 65  here if database
2383c 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64   corruption is d
2383d 65 74 65 63 74 65 64 20 61 66 74 65 72 20 6d 20  etected after m 
2383e 68 61 73 20 62 65 65 6e 0d 0a 20 20 2a 2a 20 61  has been..  ** a
2383f 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65 65 20  llocated.  Free 
23840 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61 6e 64  the m object and
23841 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
23842 4f 52 52 55 50 54 2e 20 2a 2f 0d 0a 69 64 78 5f  ORRUPT. */..idx_
23843 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e  rowid_corruption
23844 3a 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d  :..  testcase( m
23845 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0d 0a  .zMalloc!=0 );..
23846 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
23847 52 65 6c 65 61 73 65 28 26 6d 29 3b 0d 0a 20 20  Release(&m);..  
23848 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
23849 52 52 55 50 54 5f 42 4b 50 54 3b 0d 0a 7d 0d 0a  RRUPT_BKPT;..}..
2384a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 61 72 65  ../*..** Compare
2384b 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20   the key of the 
2384c 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74  index entry that
2384d 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f   cursor pC is po
2384e 69 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 6e 73  inting to agains
2384f 74 0d 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74  t..** the key st
23850 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b 65  ring in pUnpacke
23851 64 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a  d.  Write into *
23852 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0d 0a 2a  pRes a number..*
23853 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69  * that is negati
23854 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73  ve, zero, or pos
23855 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c  itive if pC is l
23856 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
23857 74 6f 2c 0d 0a 2a 2a 20 6f 72 20 67 72 65 61 74  to,..** or great
23858 65 72 20 74 68 61 6e 20 70 55 6e 70 61 63 6b 65  er than pUnpacke
23859 64 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  d.  Return SQLIT
2385a 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
2385b 0d 0a 2a 2a 0d 0a 2a 2a 20 70 55 6e 70 61 63 6b  ..**..** pUnpack
2385c 65 64 20 69 73 20 65 69 74 68 65 72 20 63 72 65  ed is either cre
2385d 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 20 72  ated without a r
2385e 6f 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e 63  owid or is trunc
2385f 61 74 65 64 20 73 6f 20 74 68 61 74 20 69 74 0d  ated so that it.
23860 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f  .** omits the ro
23861 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20  wid at the end. 
23862 20 54 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   The rowid at th
23863 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
23864 65 78 20 65 6e 74 72 79 0d 0a 2a 2a 20 69 73 20  ex entry..** is 
23865 69 67 6e 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e  ignored as well.
23866 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20 72 6f    Hence, this ro
23867 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61  utine only compa
23868 72 65 73 20 74 68 65 20 70 72 65 66 69 78 65 73  res the prefixes
23869 20 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79   ..** of the key
2386a 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 66  s prior to the f
2386b 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74 20  inal rowid, not 
2386c 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 2e 0d  the entire key..
2386d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2386e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2386f 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
23870 28 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  (..  VdbeCursor 
23871 2a 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pC,            
23872 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74   /* The cursor t
23873 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73  o compare agains
23874 74 20 2a 2f 0d 0a 20 20 55 6e 70 61 63 6b 65 64  t */..  Unpacked
23875 52 65 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65  Record *pUnpacke
23876 64 2c 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  d,  /* Unpacked 
23877 76 65 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74  version of key t
23878 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73  o compare agains
23879 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 72 65 73  t */..  int *res
2387a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2387b 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
2387c 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 75   comparison resu
2387d 6c 74 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a  lt here */..){..
2387e 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d    i64 nCellKey =
2387f 20 30 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a   0;..  int rc;..
23880 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
23881 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0d   = pC->pCursor;.
23882 0a 20 20 4d 65 6d 20 6d 3b 0d 0a 0d 0a 20 20 61  .  Mem m;....  a
23883 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
23884 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
23885 28 70 43 75 72 29 20 29 3b 0d 0a 20 20 56 56 41  (pCur) );..  VVA
23886 5f 4f 4e 4c 59 28 72 63 20 3d 29 20 73 71 6c 69  _ONLY(rc =) sqli
23887 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
23888 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29  pCur, &nCellKey)
23889 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  ;..  assert( rc=
2388a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20  =SQLITE_OK );   
2388b 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61   /* pCur is alwa
2388c 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53  ys valid so KeyS
2388d 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20  ize cannot fail 
2388e 2a 2f 0d 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65  */..  /* nCellKe
2388f 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  y will always be
23890 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30   between 0 and 0
23891 78 66 66 66 66 66 66 66 66 20 62 65 63 61 75 73  xffffffff becaus
23892 65 20 6f 66 20 74 68 65 20 73 61 79 0d 0a 20 20  e of the say..  
23893 2a 2a 20 74 68 61 74 20 62 74 72 65 65 50 61 72  ** that btreePar
23894 73 65 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20  seCellPtr() and 
23895 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
23896 33 32 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65  32() are impleme
23897 6e 74 65 64 20 2a 2f 0d 0a 20 20 69 66 28 20 6e  nted */..  if( n
23898 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43  CellKey<=0 || nC
23899 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66  ellKey>0x7ffffff
2389a 66 20 29 7b 0d 0a 20 20 20 20 2a 72 65 73 20 3d  f ){..    *res =
2389b 20 30 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   0;..    return 
2389c 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
2389d 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d  KPT;..  }..  mem
2389e 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f  set(&m, 0, sizeo
2389f 66 28 6d 29 29 3b 0d 0a 20 20 72 63 20 3d 20 73  f(m));..  rc = s
238a0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
238a1 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 73  mBtree(pC->pCurs
238a2 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c  or, 0, (int)nCel
238a3 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0d 0a 20  lKey, 1, &m);.. 
238a4 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
238a5 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d  return rc;..  }.
238a6 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e 70 61  .  assert( pUnpa
238a7 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e  cked->flags & UN
238a8 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41  PACKED_PREFIX_MA
238a9 54 43 48 20 29 3b 0d 0a 20 20 2a 72 65 73 20 3d  TCH );..  *res =
238aa 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
238ab 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d  rdCompare(m.n, m
238ac 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b 0d  .z, pUnpacked);.
238ad 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
238ae 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0d 0a 20  mRelease(&m);.. 
238af 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
238b0 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
238b1 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
238b2 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  s the value to b
238b3 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 75  e returned by su
238b4 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
238b5 6f 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 68  o..** sqlite3_ch
238b6 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20 64  anges() on the d
238b7 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 27  atabase handle '
238b8 64 62 27 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  db'. ..*/..SQLIT
238b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
238ba 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61  qlite3VdbeSetCha
238bb 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
238bc 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0d  , int nChange){.
238bd 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
238be 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
238bf 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 64  ->mutex) );..  d
238c0 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68  b->nChange = nCh
238c1 61 6e 67 65 3b 0d 0a 20 20 64 62 2d 3e 6e 54 6f  ange;..  db->nTo
238c2 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68  talChange += nCh
238c3 61 6e 67 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ange;..}..../*..
238c4 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e  ** Set a flag in
238c5 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64   the vdbe to upd
238c6 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63  ate the change c
238c7 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69  ounter when it i
238c8 73 20 66 69 6e 61 6c 69 73 65 64 0d 0a 2a 2a 20  s finalised..** 
238c9 6f 72 20 72 65 73 65 74 2e 0d 0a 2a 2f 0d 0a 53  or reset...*/..S
238ca 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
238cb 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
238cc 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20  untChanges(Vdbe 
238cd 2a 76 29 7b 0d 0a 20 20 76 2d 3e 63 68 61 6e 67  *v){..  v->chang
238ce 65 43 6e 74 4f 6e 20 3d 20 31 3b 0d 0a 7d 0d 0a  eCntOn = 1;..}..
238cf 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 72 6b 20 65 76  ../*..** Mark ev
238d0 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61  ery prepared sta
238d1 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65  tement associate
238d2 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
238d3 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a 2a  e connection..**
238d4 20 61 73 20 65 78 70 69 72 65 64 2e 0d 0a 2a 2a   as expired...**
238d5 0d 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 64 20  ..** An expired 
238d6 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20  statement means 
238d7 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 74 69  that recompilati
238d8 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  on of the statem
238d9 65 6e 74 20 69 73 0d 0a 2a 2a 20 72 65 63 6f 6d  ent is..** recom
238da 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e 74  mend.  Statement
238db 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74 68  s expire when th
238dc 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61 74  ings happen that
238dd 20 6d 61 6b 65 20 74 68 65 69 72 0d 0a 2a 2a 20   make their..** 
238de 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74  programs obsolet
238df 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65  e.  Removing use
238e0 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
238e1 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67  ons or collating
238e2 0d 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20  ..** sequences, 
238e3 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61  or changing an a
238e4 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e  uthorization fun
238e5 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79  ction are the ty
238e6 70 65 73 20 6f 66 0d 0a 2a 2a 20 74 68 69 6e 67  pes of..** thing
238e7 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70  s that make prep
238e8 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
238e9 6f 62 73 6f 6c 65 74 65 2e 0d 0a 2a 2f 0d 0a 53  obsolete...*/..S
238ea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
238eb 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  id sqlite3Expire
238ec 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
238ed 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ts(sqlite3 *db){
238ee 0d 0a 20 20 56 64 62 65 20 2a 70 3b 0d 0a 20 20  ..  Vdbe *p;..  
238ef 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62  for(p = db->pVdb
238f0 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  e; p; p=p->pNext
238f1 29 7b 0d 0a 20 20 20 20 70 2d 3e 65 78 70 69 72  ){..    p->expir
238f2 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d  ed = 1;..  }..}.
238f3 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
238f4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73   the database as
238f5 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
238f6 65 20 56 64 62 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  e Vdbe...*/..SQL
238f7 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
238f8 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65  te3 *sqlite3Vdbe
238f9 44 62 28 56 64 62 65 20 2a 76 29 7b 0d 0a 20 20  Db(Vdbe *v){..  
238fa 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0d 0a 7d  return v->db;..}
238fb 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
238fc 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
238fd 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
238fe 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
238ff 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 62  ning the value b
23900 6f 75 6e 64 0d 0a 2a 2a 20 70 61 72 61 6d 65 74  ound..** paramet
23901 65 72 20 69 56 61 72 20 6f 66 20 56 4d 20 76 2e  er iVar of VM v.
23902 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20   Except, if the 
23903 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20  value is an SQL 
23904 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 0d 0a 2a  NULL, return ..*
23905 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55 6e 6c  * 0 instead. Unl
23906 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20  ess it is NULL, 
23907 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 61  apply affinity a
23908 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65 20 53  ff (one of the S
23909 51 4c 49 54 45 5f 41 46 46 5f 2a 0d 0a 2a 2a 20  QLITE_AFF_*..** 
2390a 63 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74 68  constants) to th
2390b 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 72  e value before r
2390c 65 74 75 72 6e 69 6e 67 20 69 74 2e 0d 0a 2a 2a  eturning it...**
2390d 0d 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  ..** The returne
2390e 64 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20  d value must be 
2390f 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c  freed by the cal
23910 6c 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ler using sqlite
23911 33 56 61 6c 75 65 46 72 65 65 28 29 2e 0d 0a 2a  3ValueFree()...*
23912 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
23913 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  E sqlite3_value 
23914 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 56  *sqlite3VdbeGetV
23915 61 6c 75 65 28 56 64 62 65 20 2a 76 2c 20 69 6e  alue(Vdbe *v, in
23916 74 20 69 56 61 72 2c 20 75 38 20 61 66 66 29 7b  t iVar, u8 aff){
23917 0d 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 72  ..  assert( iVar
23918 3e 30 20 29 3b 0d 0a 20 20 69 66 28 20 76 20 29  >0 );..  if( v )
23919 7b 0d 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  {..    Mem *pMem
2391a 20 3d 20 26 76 2d 3e 61 56 61 72 5b 69 56 61 72   = &v->aVar[iVar
2391b 2d 31 5d 3b 0d 0a 20 20 20 20 69 66 28 20 30 3d  -1];..    if( 0=
2391c 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  =(pMem->flags & 
2391d 4d 45 4d 5f 4e 75 6c 6c 29 20 29 7b 0d 0a 20 20  MEM_Null) ){..  
2391e 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2391f 65 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74 65  e *pRet = sqlite
23920 33 56 61 6c 75 65 4e 65 77 28 76 2d 3e 64 62 29  3ValueNew(v->db)
23921 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 52 65  ;..      if( pRe
23922 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  t ){..        sq
23923 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
23924 28 28 4d 65 6d 20 2a 29 70 52 65 74 2c 20 70 4d  ((Mem *)pRet, pM
23925 65 6d 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  em);..        sq
23926 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
23927 66 66 69 6e 69 74 79 28 70 52 65 74 2c 20 61 66  ffinity(pRet, af
23928 66 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  f, SQLITE_UTF8);
23929 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2392a 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70  3VdbeMemStoreTyp
2392b 65 28 28 4d 65 6d 20 2a 29 70 52 65 74 29 3b 0d  e((Mem *)pRet);.
2392c 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
2392d 72 65 74 75 72 6e 20 70 52 65 74 3b 0d 0a 20 20  return pRet;..  
2392e 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
2392f 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn 0;..}..../*..
23930 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 53 51 4c  ** Configure SQL
23931 20 76 61 72 69 61 62 6c 65 20 69 56 61 72 20 73   variable iVar s
23932 6f 20 74 68 61 74 20 62 69 6e 64 69 6e 67 20 61  o that binding a
23933 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 69 74   new value to it
23934 20 73 69 67 6e 61 6c 73 0d 0a 2a 2a 20 74 6f 20   signals..** to 
23935 73 71 6c 69 74 65 33 5f 72 65 6f 70 74 69 6d 69  sqlite3_reoptimi
23936 7a 65 28 29 20 74 68 61 74 20 72 65 2d 70 72 65  ze() that re-pre
23937 70 61 72 69 6e 67 20 74 68 65 20 73 74 61 74 65  paring the state
23938 6d 65 6e 74 20 6d 61 79 20 72 65 73 75 6c 74 0d  ment may result.
23939 0a 2a 2a 20 69 6e 20 61 20 62 65 74 74 65 72 20  .** in a better 
2393a 71 75 65 72 79 20 70 6c 61 6e 2e 0d 0a 2a 2f 0d  query plan...*/.
2393b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2393c 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
2393d 53 65 74 56 61 72 6d 61 73 6b 28 56 64 62 65 20  SetVarmask(Vdbe 
2393e 2a 76 2c 20 69 6e 74 20 69 56 61 72 29 7b 0d 0a  *v, int iVar){..
2393f 20 20 61 73 73 65 72 74 28 20 69 56 61 72 3e 30    assert( iVar>0
23940 20 29 3b 0d 0a 20 20 69 66 28 20 69 56 61 72 3e   );..  if( iVar>
23941 33 32 20 29 7b 0d 0a 20 20 20 20 76 2d 3e 65 78  32 ){..    v->ex
23942 70 6d 61 73 6b 20 3d 20 30 78 66 66 66 66 66 66  pmask = 0xffffff
23943 66 66 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  ff;..  }else{.. 
23944 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 7c 3d     v->expmask |=
23945 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69 56 61   ((u32)1 << (iVa
23946 72 2d 31 29 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  r-1));..  }..}..
23947 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
23948 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78  * End of vdbeaux
23949 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2394a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2394b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2394c 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2394d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
2394e 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  beapi.c ********
2394f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23951 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 34 20  */../*..** 2004 
23952 4d 61 79 20 32 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54  May 26..**..** T
23953 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
23954 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
23955 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
23956 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
23957 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
23958 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
23959 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
2395a 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2395b 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2395c 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2395d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2395e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2395f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
23960 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
23961 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
23962 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
23963 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
23964 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
23965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23967 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23968 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
23969 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
2396a 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
2396b 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  se to implement 
2396c 41 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61  APIs that are pa
2396d 72 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 56 44  rt of the..** VD
2396e 42 45 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 6e 64  BE...*/....#ifnd
2396f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
23970 45 50 52 45 43 41 54 45 44 0d 0a 2f 2a 0d 0a 2a  EPRECATED../*..*
23971 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e  * Return TRUE (n
23972 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20  on-zero) of the 
23973 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69  statement suppli
23974 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
23975 74 20 6e 65 65 64 73 0d 0a 2a 2a 20 74 6f 20 62  t needs..** to b
23976 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41  e recompiled.  A
23977 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 73   statement needs
23978 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65   to be recompile
23979 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0d 0a  d whenever the..
2397a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76  ** execution env
2397b 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73  ironment changes
2397c 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 77   in a way that w
2397d 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70  ould alter the p
2397e 72 6f 67 72 61 6d 0d 0a 2a 2a 20 74 68 61 74 20  rogram..** that 
2397f 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
23980 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 6f  ) generates.  Fo
23981 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65  r example, if ne
23982 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0d 0a  w functions or..
23983 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
23984 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69 73  uences are regis
23985 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61  tered or if an a
23986 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69  uthorizer functi
23987 6f 6e 20 69 73 0d 0a 2a 2a 20 61 64 64 65 64 20  on is..** added 
23988 6f 72 20 63 68 61 6e 67 65 64 2e 0d 0a 2a 2f 0d  or changed...*/.
23989 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2398a 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28  sqlite3_expired(
2398b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2398c 74 6d 74 29 7b 0d 0a 20 20 56 64 62 65 20 2a 70  tmt){..  Vdbe *p
2398d 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
2398e 0d 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  ..  return p==0 
2398f 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0d 0a  || p->expired;..
23990 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  }..#endif..../*.
23991 0a 2a 2a 20 43 68 65 63 6b 20 6f 6e 20 61 20 56  .** Check on a V
23992 64 62 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  dbe to make sure
23993 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e   it has not been
23994 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 4c 6f 67   finalized.  Log
23995 0d 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 61 6e  ..** an error an
23996 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  d return true if
23997 20 69 74 20 68 61 73 20 62 65 65 6e 20 66 69 6e   it has been fin
23998 61 6c 69 7a 65 64 20 28 6f 72 20 69 73 20 6f 74  alized (or is ot
23999 68 65 72 77 69 73 65 0d 0a 2a 2a 20 69 6e 76 61  herwise..** inva
2399a 6c 69 64 29 2e 20 20 52 65 74 75 72 6e 20 66 61  lid).  Return fa
2399b 6c 73 65 20 69 66 20 69 74 20 69 73 20 6f 6b 2e  lse if it is ok.
2399c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
2399d 20 76 64 62 65 53 61 66 65 74 79 28 56 64 62 65   vdbeSafety(Vdbe
2399e 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e   *p){..  if( p->
2399f 64 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  db==0 ){..    sq
239a0 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45  lite3_log(SQLITE
239a1 5f 4d 49 53 55 53 45 2c 20 22 41 50 49 20 63 61  _MISUSE, "API ca
239a2 6c 6c 65 64 20 77 69 74 68 20 66 69 6e 61 6c 69  lled with finali
239a3 7a 65 64 20 70 72 65 70 61 72 65 64 20 73 74 61  zed prepared sta
239a4 74 65 6d 65 6e 74 22 29 3b 0d 0a 20 20 20 20 72  tement");..    r
239a5 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 65 6c 73  eturn 1;..  }els
239a6 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  e{..    return 0
239a7 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73 74 61 74 69  ;..  }..}..stati
239a8 63 20 69 6e 74 20 76 64 62 65 53 61 66 65 74 79  c int vdbeSafety
239a9 4e 6f 74 4e 75 6c 6c 28 56 64 62 65 20 2a 70 29  NotNull(Vdbe *p)
239aa 7b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  {..  if( p==0 ){
239ab 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f  ..    sqlite3_lo
239ac 67 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c  g(SQLITE_MISUSE,
239ad 20 22 41 50 49 20 63 61 6c 6c 65 64 20 77 69 74   "API called wit
239ae 68 20 4e 55 4c 4c 20 70 72 65 70 61 72 65 64 20  h NULL prepared 
239af 73 74 61 74 65 6d 65 6e 74 22 29 3b 0d 0a 20 20  statement");..  
239b0 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d    return 1;..  }
239b1 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72  else{..    retur
239b2 6e 20 76 64 62 65 53 61 66 65 74 79 28 70 29 3b  n vdbeSafety(p);
239b3 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
239b4 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
239b5 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f 79   routine destroy
239b6 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68  s a virtual mach
239b7 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 65 61  ine that is crea
239b8 74 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65 20 73  ted by..** the s
239b9 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29  qlite3_compile()
239ba 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 6e   routine. The in
239bb 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 69  teger returned i
239bc 73 20 61 6e 20 53 51 4c 49 54 45 5f 0d 0a 2a 2a  s an SQLITE_..**
239bd 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65   success/failure
239be 20 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72   code that descr
239bf 69 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20  ibes the result 
239c0 6f 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65  of executing the
239c1 20 76 69 72 74 75 61 6c 0d 0a 2a 2a 20 6d 61 63   virtual..** mac
239c2 68 69 6e 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  hine...**..** Th
239c3 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
239c4 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
239c5 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
239c6 65 64 20 62 79 0d 0a 2a 2a 20 73 71 6c 69 74 65  ed by..** sqlite
239c7 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c  3_errcode(), sql
239c8 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e  ite3_errmsg() an
239c9 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  d sqlite3_errmsg
239ca 31 36 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  16()...*/..SQLIT
239cb 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
239cc 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
239cd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
239ce 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69  ..  int rc;..  i
239cf 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0d 0a  f( pStmt==0 ){..
239d0 20 20 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54      /* IMPLEMENT
239d1 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 35 37 32 32  ATION-OF: R-5722
239d2 38 2d 31 32 39 30 34 20 49 6e 76 6f 6b 69 6e 67  8-12904 Invoking
239d3 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
239d4 65 28 29 20 6f 6e 20 61 20 4e 55 4c 4c 0d 0a 20  e() on a NULL.. 
239d5 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 69 73     ** pointer is
239d6 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f   a harmless no-o
239d7 70 2e 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20  p. */..    rc = 
239d8 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65  SQLITE_OK;..  }e
239d9 6c 73 65 7b 0d 0a 20 20 20 20 56 64 62 65 20 2a  lse{..    Vdbe *
239da 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  v = (Vdbe*)pStmt
239db 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ;..    sqlite3 *
239dc 64 62 20 3d 20 76 2d 3e 64 62 3b 0d 0a 23 69 66  db = v->db;..#if
239dd 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
239de 46 45 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  FE..    sqlite3_
239df 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0d 0a 23  mutex *mutex;..#
239e0 65 6e 64 69 66 0d 0a 20 20 20 20 69 66 28 20 76  endif..    if( v
239e1 64 62 65 53 61 66 65 74 79 28 76 29 20 29 20 72  dbeSafety(v) ) r
239e2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
239e3 55 53 45 5f 42 4b 50 54 3b 0d 0a 23 69 66 20 53  USE_BKPT;..#if S
239e4 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
239e5 0d 0a 20 20 20 20 6d 75 74 65 78 20 3d 20 76 2d  ..    mutex = v-
239e6 3e 64 62 2d 3e 6d 75 74 65 78 3b 0d 0a 23 65 6e  >db->mutex;..#en
239e7 64 69 66 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  dif..    sqlite3
239e8 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
239e9 65 78 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  ex);..    rc = s
239ea 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
239eb 7a 65 28 76 29 3b 0d 0a 20 20 20 20 72 63 20 3d  ze(v);..    rc =
239ec 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
239ed 64 62 2c 20 72 63 29 3b 0d 0a 20 20 20 20 73 71  db, rc);..    sq
239ee 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
239ef 65 28 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a  e(mutex);..  }..
239f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
239f1 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 65 72 6d 69 6e  .../*..** Termin
239f2 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
239f3 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20  execution of an 
239f4 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  SQL statement an
239f5 64 20 72 65 73 65 74 20 69 74 0d 0a 2a 2a 20 62  d reset it..** b
239f6 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 74  ack to its start
239f7 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 61  ing state so tha
239f8 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 73  t it can be reus
239f9 65 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 6f  ed. A success co
239fa 64 65 20 66 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20  de from..** the 
239fb 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20  prior execution 
239fc 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
239fd 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
239fe 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72  e sets the error
239ff 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67   code and string
23a00 20 72 65 74 75 72 6e 65 64 20 62 79 0d 0a 2a 2a   returned by..**
23a01 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
23a02 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  (), sqlite3_errm
23a03 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
23a04 5f 65 72 72 6d 73 67 31 36 28 29 2e 0d 0a 2a 2f  _errmsg16()...*/
23a05 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
23a06 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73   sqlite3_reset(s
23a07 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
23a08 6d 74 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  mt){..  int rc;.
23a09 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20  .  if( pStmt==0 
23a0a 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
23a0b 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65  ITE_OK;..  }else
23a0c 7b 0d 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  {..    Vdbe *v =
23a0d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0d 0a   (Vdbe*)pStmt;..
23a0e 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
23a0f 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d  x_enter(v->db->m
23a10 75 74 65 78 29 3b 0d 0a 20 20 20 20 72 63 20 3d  utex);..    rc =
23a11 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65   sqlite3VdbeRese
23a12 74 28 76 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  t(v);..    sqlit
23a13 65 33 56 64 62 65 52 65 77 69 6e 64 28 76 29 3b  e3VdbeRewind(v);
23a14 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72  ..    assert( (r
23a15 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d  c & (v->db->errM
23a16 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0d 0a 20 20  ask))==rc );..  
23a17 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
23a18 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 29  iExit(v->db, rc)
23a19 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ;..    sqlite3_m
23a1a 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62  utex_leave(v->db
23a1b 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a  ->mutex);..  }..
23a1c 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
23a1d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 61 6c  .../*..** Set al
23a1e 6c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73  l the parameters
23a1f 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64   in the compiled
23a20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
23a21 6f 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 53 51 4c  o NULL...*/..SQL
23a22 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
23a23 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e  te3_clear_bindin
23a24 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
23a25 2a 70 53 74 6d 74 29 7b 0d 0a 20 20 69 6e 74 20  *pStmt){..  int 
23a26 69 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  i;..  int rc = S
23a27 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 56 64 62  QLITE_OK;..  Vdb
23a28 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53  e *p = (Vdbe*)pS
23a29 74 6d 74 3b 0d 0a 23 69 66 20 53 51 4c 49 54 45  tmt;..#if SQLITE
23a2a 5f 54 48 52 45 41 44 53 41 46 45 0d 0a 20 20 73  _THREADSAFE..  s
23a2b 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
23a2c 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 70 53  tex = ((Vdbe*)pS
23a2d 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b  tmt)->db->mutex;
23a2e 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c 69  ..#endif..  sqli
23a2f 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
23a30 6d 75 74 65 78 29 3b 0d 0a 20 20 66 6f 72 28 69  mutex);..  for(i
23a31 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69  =0; i<p->nVar; i
23a32 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ++){..    sqlite
23a33 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
23a34 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0d 0a 20  &p->aVar[i]);.. 
23a35 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c     p->aVar[i].fl
23a36 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d  ags = MEM_Null;.
23a37 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e 69  .  }..  if( p->i
23a38 73 50 72 65 70 61 72 65 56 32 20 26 26 20 70 2d  sPrepareV2 && p-
23a39 3e 65 78 70 6d 61 73 6b 20 29 7b 0d 0a 20 20 20  >expmask ){..   
23a3a 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b   p->expired = 1;
23a3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
23a3c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
23a3d 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  ex);..  return r
23a3e 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a  c;..}....../****
23a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a40 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
23a41 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a  _value_  *******
23a42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a43 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65  ********..** The
23a44 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
23a45 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f  nes extract info
23a46 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d  rmation from a M
23a47 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61  em or sqlite3_va
23a48 6c 75 65 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72  lue..** structur
23a49 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  e...*/..SQLITE_A
23a4a 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
23a4b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
23a4c 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  b(sqlite3_value 
23a4d 2a 70 56 61 6c 29 7b 0d 0a 20 20 4d 65 6d 20 2a  *pVal){..  Mem *
23a4e 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0d  p = (Mem*)pVal;.
23a4f 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
23a50 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
23a51 53 74 72 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Str) ){..    sql
23a52 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
23a53 64 42 6c 6f 62 28 70 29 3b 0d 0a 20 20 20 20 70  dBlob(p);..    p
23a54 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f  ->flags &= ~MEM_
23a55 53 74 72 3b 0d 0a 20 20 20 20 70 2d 3e 66 6c 61  Str;..    p->fla
23a56 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0d  gs |= MEM_Blob;.
23a57 0a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  .    return p->n
23a58 20 3f 20 70 2d 3e 7a 20 3a 20 30 3b 0d 0a 20 20   ? p->z : 0;..  
23a59 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75  }else{..    retu
23a5a 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  rn sqlite3_value
23a5b 5f 74 65 78 74 28 70 56 61 6c 29 3b 0d 0a 20 20  _text(pVal);..  
23a5c 7d 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49  }..}..SQLITE_API
23a5d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
23a5e 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ue_bytes(sqlite3
23a5f 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0d 0a  _value *pVal){..
23a60 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
23a61 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c  ValueBytes(pVal,
23a62 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0d 0a   SQLITE_UTF8);..
23a63 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  }..SQLITE_API in
23a64 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
23a65 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
23a66 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0d 0a 20  value *pVal){.. 
23a67 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
23a68 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20  alueBytes(pVal, 
23a69 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
23a6a 56 45 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f  VE);..}..SQLITE_
23a6b 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
23a6c 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
23a6d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
23a6e 56 61 6c 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20  Val){..  return 
23a6f 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
23a70 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29  alue((Mem*)pVal)
23a71 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ;..}..SQLITE_API
23a72 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
23a73 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76  ue_int(sqlite3_v
23a74 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0d 0a 20 20  alue *pVal){..  
23a75 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69  return (int)sqli
23a76 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
23a77 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0d 0a 7d 0d  (Mem*)pVal);..}.
23a78 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
23a79 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
23a7a 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c  _value_int64(sql
23a7b 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
23a7c 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  ){..  return sql
23a7d 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
23a7e 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0d 0a 7d  ((Mem*)pVal);..}
23a7f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
23a80 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
23a81 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
23a82 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  text(sqlite3_val
23a83 75 65 20 2a 70 56 61 6c 29 7b 0d 0a 20 20 72 65  ue *pVal){..  re
23a84 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69  turn (const unsi
23a85 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69  gned char *)sqli
23a86 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
23a87 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
23a88 0d 0a 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..}..#ifndef SQL
23a89 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d 0a  ITE_OMIT_UTF16..
23a8a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
23a8b 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
23a8c 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c 69  alue_text16(sqli
23a8d 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29  te3_value* pVal)
23a8e 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  {..  return sqli
23a8f 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
23a90 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  l, SQLITE_UTF16N
23a91 41 54 49 56 45 29 3b 0d 0a 7d 0d 0a 53 51 4c 49  ATIVE);..}..SQLI
23a92 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
23a93 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
23a94 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65  _text16be(sqlite
23a95 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0d  3_value *pVal){.
23a96 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
23a97 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
23a98 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29   SQLITE_UTF16BE)
23a99 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ;..}..SQLITE_API
23a9a 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
23a9b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
23a9c 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6le(sqlite3_valu
23a9d 65 20 2a 70 56 61 6c 29 7b 0d 0a 20 20 72 65 74  e *pVal){..  ret
23a9e 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
23a9f 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
23aa0 45 5f 55 54 46 31 36 4c 45 29 3b 0d 0a 7d 0d 0a  E_UTF16LE);..}..
23aa1 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
23aa2 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a  _OMIT_UTF16 */..
23aa3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23aa4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
23aa5 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
23aa6 20 70 56 61 6c 29 7b 0d 0a 20 20 72 65 74 75 72   pVal){..  retur
23aa7 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0d 0a 7d  n pVal->type;..}
23aa8 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
23aa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23aaa 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  * sqlite3_result
23aab 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _  *************
23aac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23aad 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  **..** The follo
23aae 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72  wing routines ar
23aaf 65 20 75 73 65 64 20 62 79 20 75 73 65 72 2d 64  e used by user-d
23ab0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
23ab1 20 74 6f 20 73 70 65 63 69 66 79 0d 0a 2a 2a 20   to specify..** 
23ab2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 73  the function res
23ab3 75 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ult...**..** The
23ab4 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 29   setStrOrError()
23ab5 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 73   funtion calls s
23ab6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
23ab7 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 74  Str() to store t
23ab8 68 65 0d 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73  he..** result as
23ab9 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
23aba 62 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72  b but if the str
23abb 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74  ing or blob is t
23abc 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0d 0a 2a 2a  oo large, it..**
23abd 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 65   then sets the e
23abe 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c  rror code to SQL
23abf 49 54 45 5f 54 4f 4f 42 49 47 0d 0a 2a 2f 0d 0a  ITE_TOOBIG..*/..
23ac0 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 52  static void setR
23ac1 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28  esultStrOrError(
23ac2 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ..  sqlite3_cont
23ac3 65 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46  ext *pCtx,  /* F
23ac4 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
23ac5 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
23ac6 20 2a 7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   *z,          /*
23ac7 20 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20   String pointer 
23ac8 2a 2f 0d 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20  */..  int n,    
23ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23aca 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67   Bytes in string
23acb 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f  , or negative */
23acc 0d 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  ..  u8 enc,     
23acd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
23ace 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30  ncoding of z.  0
23acf 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0d 0a 20   for BLOBs */.. 
23ad0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
23ad1 69 64 2a 29 20 20 20 20 20 2f 2a 20 44 65 73 74  id*)     /* Dest
23ad2 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
23ad3 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 73 71  */..){..  if( sq
23ad4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
23ad5 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
23ad6 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3d 3d 53  n, enc, xDel)==S
23ad7 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 7b 0d  QLITE_TOOBIG ){.
23ad8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
23ad9 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
23ada 28 70 43 74 78 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  (pCtx);..  }..}.
23adb 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
23adc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23add 62 6c 6f 62 28 0d 0a 20 20 73 71 6c 69 74 65 33  blob(..  sqlite3
23ade 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
23adf 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ..  const void *
23ae0 7a 2c 20 0d 0a 20 20 69 6e 74 20 6e 2c 20 0d 0a  z, ..  int n, ..
23ae1 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
23ae2 6f 69 64 20 2a 29 0d 0a 29 7b 0d 0a 20 20 61 73  oid *)..){..  as
23ae3 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0d 0a 20  sert( n>=0 );.. 
23ae4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
23ae5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
23ae6 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
23ae7 3b 0d 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74  ;..  setResultSt
23ae8 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a  rOrError(pCtx, z
23ae9 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0d 0a  , n, 0, xDel);..
23aea 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  }..SQLITE_API vo
23aeb 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
23aec 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  t_double(sqlite3
23aed 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
23aee 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0d 0a 20  double rVal){.. 
23aef 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
23af0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
23af1 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
23af2 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
23af3 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43  MemSetDouble(&pC
23af4 74 78 2d 3e 73 2c 20 72 56 61 6c 29 3b 0d 0a 7d  tx->s, rVal);..}
23af5 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ..SQLITE_API voi
23af6 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
23af7 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63  _error(sqlite3_c
23af8 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f  ontext *pCtx, co
23af9 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
23afa 20 6e 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20   n){..  assert( 
23afb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
23afc 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
23afd 75 74 65 78 29 20 29 3b 0d 0a 20 20 70 43 74 78  utex) );..  pCtx
23afe 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
23aff 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 73 71 6c  TE_ERROR;..  sql
23b00 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
23b01 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
23b02 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
23b03 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
23b04 3b 0d 0a 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51  ;..}..#ifndef SQ
23b05 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d  LITE_OMIT_UTF16.
23b06 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
23b07 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23b08 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
23b09 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63  context *pCtx, c
23b0a 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
23b0b 74 20 6e 29 7b 0d 0a 20 20 61 73 73 65 72 74 28  t n){..  assert(
23b0c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
23b0d 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
23b0e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 70 43 74  mutex) );..  pCt
23b0f 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
23b10 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 73 71  ITE_ERROR;..  sq
23b11 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
23b12 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
23b13 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  n, SQLITE_UTF16N
23b14 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52  ATIVE, SQLITE_TR
23b15 41 4e 53 49 45 4e 54 29 3b 0d 0a 7d 0d 0a 23 65  ANSIENT);..}..#e
23b16 6e 64 69 66 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ndif..SQLITE_API
23b17 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
23b18 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33  sult_int(sqlite3
23b19 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
23b1a 69 6e 74 20 69 56 61 6c 29 7b 0d 0a 20 20 61 73  int iVal){..  as
23b1b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
23b1c 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
23b1d 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  .db->mutex) );..
23b1e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
23b1f 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e  SetInt64(&pCtx->
23b20 73 2c 20 28 69 36 34 29 69 56 61 6c 29 3b 0d 0a  s, (i64)iVal);..
23b21 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  }..SQLITE_API vo
23b22 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
23b23 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  t_int64(sqlite3_
23b24 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
23b25 36 34 20 69 56 61 6c 29 7b 0d 0a 20 20 61 73 73  64 iVal){..  ass
23b26 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
23b27 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
23b28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  db->mutex) );.. 
23b29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
23b2a 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73  etInt64(&pCtx->s
23b2b 2c 20 69 56 61 6c 29 3b 0d 0a 7d 0d 0a 53 51 4c  , iVal);..}..SQL
23b2c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
23b2d 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
23b2e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
23b2f 20 2a 70 43 74 78 29 7b 0d 0a 20 20 61 73 73 65   *pCtx){..  asse
23b30 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
23b31 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
23b32 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  b->mutex) );..  
23b33 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
23b34 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b  tNull(&pCtx->s);
23b35 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..}..SQLITE_API 
23b36 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
23b37 75 6c 74 5f 74 65 78 74 28 0d 0a 20 20 73 71 6c  ult_text(..  sql
23b38 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
23b39 74 78 2c 20 0d 0a 20 20 63 6f 6e 73 74 20 63 68  tx, ..  const ch
23b3a 61 72 20 2a 7a 2c 20 0d 0a 20 20 69 6e 74 20 6e  ar *z, ..  int n
23b3b 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  ,..  void (*xDel
23b3c 29 28 76 6f 69 64 20 2a 29 0d 0a 29 7b 0d 0a 20  )(void *)..){.. 
23b3d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
23b3e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
23b3f 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
23b40 3b 0d 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74  ;..  setResultSt
23b41 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a  rOrError(pCtx, z
23b42 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , n, SQLITE_UTF8
23b43 2c 20 78 44 65 6c 29 3b 0d 0a 7d 0d 0a 23 69 66  , xDel);..}..#if
23b44 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23b45 5f 55 54 46 31 36 0d 0a 53 51 4c 49 54 45 5f 41  _UTF16..SQLITE_A
23b46 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
23b47 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0d 0a  result_text16(..
23b48 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
23b49 74 20 2a 70 43 74 78 2c 20 0d 0a 20 20 63 6f 6e  t *pCtx, ..  con
23b4a 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0d 0a 20 20  st void *z, ..  
23b4b 69 6e 74 20 6e 2c 20 0d 0a 20 20 76 6f 69 64 20  int n, ..  void 
23b4c 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0d  (*xDel)(void *).
23b4d 0a 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 73  .){..  assert( s
23b4e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
23b4f 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
23b50 74 65 78 29 20 29 3b 0d 0a 20 20 73 65 74 52 65  tex) );..  setRe
23b51 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70  sultStrOrError(p
23b52 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  Ctx, z, n, SQLIT
23b53 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78  E_UTF16NATIVE, x
23b54 44 65 6c 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45  Del);..}..SQLITE
23b55 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
23b56 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
23b57 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  e(..  sqlite3_co
23b58 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0d 0a 20  ntext *pCtx, .. 
23b59 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
23b5a 0d 0a 20 20 69 6e 74 20 6e 2c 20 0d 0a 20 20 76  ..  int n, ..  v
23b5b 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
23b5c 20 2a 29 0d 0a 29 7b 0d 0a 20 20 61 73 73 65 72   *)..){..  asser
23b5d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
23b5e 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
23b5f 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 73  ->mutex) );..  s
23b60 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
23b61 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53  or(pCtx, z, n, S
23b62 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 78  QLITE_UTF16BE, x
23b63 44 65 6c 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45  Del);..}..SQLITE
23b64 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
23b65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
23b66 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  e(..  sqlite3_co
23b67 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0d 0a 20  ntext *pCtx, .. 
23b68 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
23b69 0d 0a 20 20 69 6e 74 20 6e 2c 20 0d 0a 20 20 76  ..  int n, ..  v
23b6a 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
23b6b 20 2a 29 0d 0a 29 7b 0d 0a 20 20 61 73 73 65 72   *)..){..  asser
23b6c 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
23b6d 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
23b6e 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 73  ->mutex) );..  s
23b6f 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
23b70 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53  or(pCtx, z, n, S
23b71 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78  QLITE_UTF16LE, x
23b72 44 65 6c 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  Del);..}..#endif
23b73 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
23b74 55 54 46 31 36 20 2a 2f 0d 0a 53 51 4c 49 54 45  UTF16 */..SQLITE
23b75 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
23b76 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73  3_result_value(s
23b77 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
23b78 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 61  pCtx, sqlite3_va
23b79 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0d 0a 20  lue *pValue){.. 
23b7a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
23b7b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
23b7c 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
23b7d 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
23b7e 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73  MemCopy(&pCtx->s
23b7f 2c 20 70 56 61 6c 75 65 29 3b 0d 0a 7d 0d 0a 53  , pValue);..}..S
23b80 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
23b81 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
23b82 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63  roblob(sqlite3_c
23b83 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
23b84 74 20 6e 29 7b 0d 0a 20 20 61 73 73 65 72 74 28  t n){..  assert(
23b85 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
23b86 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
23b87 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 73 71 6c  mutex) );..  sql
23b88 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
23b89 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c  roBlob(&pCtx->s,
23b8a 20 6e 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f   n);..}..SQLITE_
23b8b 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
23b8c 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
23b8d 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  de(sqlite3_conte
23b8e 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72  xt *pCtx, int er
23b8f 72 43 6f 64 65 29 7b 0d 0a 20 20 70 43 74 78 2d  rCode){..  pCtx-
23b90 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 6f  >isError = errCo
23b91 64 65 3b 0d 0a 20 20 69 66 28 20 70 43 74 78 2d  de;..  if( pCtx-
23b92 3e 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  >s.flags & MEM_N
23b93 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ull ){..    sqli
23b94 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
23b95 28 26 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74  (&pCtx->s, sqlit
23b96 65 33 45 72 72 53 74 72 28 65 72 72 43 6f 64 65  e3ErrStr(errCode
23b97 29 2c 20 2d 31 2c 20 0d 0a 20 20 20 20 20 20 20  ), -1, ..       
23b98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23b99 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
23b9a 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
23b9b 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 46 6f 72    }..}..../* For
23b9c 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f  ce an SQLITE_TOO
23b9d 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0d 0a 53  BIG error. */..S
23b9e 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
23b9f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
23ba0 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74  ror_toobig(sqlit
23ba1 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
23ba2 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ){..  assert( sq
23ba3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
23ba4 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
23ba5 65 78 29 20 29 3b 0d 0a 20 20 70 43 74 78 2d 3e  ex) );..  pCtx->
23ba6 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45  isError = SQLITE
23ba7 5f 54 4f 4f 42 49 47 3b 0d 0a 20 20 73 71 6c 69  _TOOBIG;..  sqli
23ba8 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
23ba9 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72 69  (&pCtx->s, "stri
23baa 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62  ng or blob too b
23bab 69 67 22 2c 20 2d 31 2c 20 0d 0a 20 20 20 20 20  ig", -1, ..     
23bac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23bad 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
23bae 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
23baf 7d 0d 0a 0d 0a 2f 2a 20 41 6e 20 53 51 4c 49 54  }..../* An SQLIT
23bb0 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a  E_NOMEM error. *
23bb1 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  /..SQLITE_API vo
23bb2 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
23bb3 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71  t_error_nomem(sq
23bb4 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
23bb5 43 74 78 29 7b 0d 0a 20 20 61 73 73 65 72 74 28  Ctx){..  assert(
23bb6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
23bb7 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
23bb8 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 73 71 6c  mutex) );..  sql
23bb9 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
23bba 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0d 0a 20  ll(&pCtx->s);.. 
23bbb 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
23bbc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
23bbd 20 20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61    pCtx->s.db->ma
23bbe 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d  llocFailed = 1;.
23bbf 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
23bc0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
23bc1 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 61  lled after a tra
23bc2 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65  nsaction has bee
23bc3 6e 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 74 20  n committed. It 
23bc4 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 63 61 6c  ..** invokes cal
23bc5 6c 62 61 63 6b 73 20 72 65 67 69 73 74 65 72 65  lbacks registere
23bc6 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 77  d with sqlite3_w
23bc7 61 6c 5f 68 6f 6f 6b 28 29 20 61 73 20 72 65 71  al_hook() as req
23bc8 75 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  uired...*/..stat
23bc9 69 63 20 69 6e 74 20 64 6f 57 61 6c 43 61 6c 6c  ic int doWalCall
23bca 62 61 63 6b 73 28 73 71 6c 69 74 65 33 20 2a 64  backs(sqlite3 *d
23bcb 62 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  b){..  int rc = 
23bcc 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 23 69 66 6e  SQLITE_OK;..#ifn
23bcd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23bce 57 41 4c 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  WAL..  int i;.. 
23bcf 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
23bd0 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  nDb; i++){..    
23bd1 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
23bd2 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20  >aDb[i].pBt;..  
23bd3 20 20 69 66 28 20 70 42 74 20 29 7b 0d 0a 20 20    if( pBt ){..  
23bd4 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79 20 3d      int nEntry =
23bd5 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c   sqlite3PagerWal
23bd6 43 61 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33  Callback(sqlite3
23bd7 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29 29  BtreePager(pBt))
23bd8 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  ;..      if( db-
23bd9 3e 78 57 61 6c 43 61 6c 6c 62 61 63 6b 20 26 26  >xWalCallback &&
23bda 20 6e 45 6e 74 72 79 3e 30 20 26 26 20 72 63 3d   nEntry>0 && rc=
23bdb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
23bdc 20 20 20 20 20 20 20 72 63 20 3d 20 64 62 2d 3e         rc = db->
23bdd 78 57 61 6c 43 61 6c 6c 62 61 63 6b 28 64 62 2d  xWalCallback(db-
23bde 3e 70 57 61 6c 41 72 67 2c 20 64 62 2c 20 64 62  >pWalArg, db, db
23bdf 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ->aDb[i].zName, 
23be0 6e 45 6e 74 72 79 29 3b 0d 0a 20 20 20 20 20 20  nEntry);..      
23be1 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23  }..    }..  }..#
23be2 65 6e 64 69 66 0d 0a 20 20 72 65 74 75 72 6e 20  endif..  return 
23be3 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
23be4 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61   Execute the sta
23be5 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69  tement pStmt, ei
23be6 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77  ther until a row
23be7 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64   of data is read
23be8 79 2c 20 74 68 65 0d 0a 2a 2a 20 73 74 61 74 65  y, the..** state
23be9 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65  ment is complete
23bea 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61  ly executed or a
23beb 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0d  n error occurs..
23bec 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
23bed 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tine implements 
23bee 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20  the bulk of the 
23bef 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 65  logic behind the
23bf0 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0d 0a   sqlite_step()..
23bf1 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c  ** API.  The onl
23bf2 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20  y thing omitted 
23bf3 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  is the automatic
23bf4 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20   recompile if a 
23bf5 0d 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e  ..** schema chan
23bf6 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  ge has occurred.
23bf7 20 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73    That detail is
23bf8 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0d   handled by the.
23bf9 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65  .** outer sqlite
23bfa 33 5f 73 74 65 70 28 29 20 77 72 61 70 70 65 72  3_step() wrapper
23bfb 20 70 72 6f 63 65 64 75 72 65 2e 0d 0a 2a 2f 0d   procedure...*/.
23bfc 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
23bfd 74 65 33 53 74 65 70 28 56 64 62 65 20 2a 70 29  te3Step(Vdbe *p)
23bfe 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
23bff 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a  ;..  int rc;....
23c00 20 20 61 73 73 65 72 74 28 70 29 3b 0d 0a 20 20    assert(p);..  
23c01 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44  if( p->magic!=VD
23c02 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0d  BE_MAGIC_RUN ){.
23c03 0a 20 20 20 20 2f 2a 20 57 65 20 75 73 65 64 20  .    /* We used 
23c04 74 6f 20 72 65 71 75 69 72 65 20 74 68 61 74 20  to require that 
23c05 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20  sqlite3_reset() 
23c06 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
23c07 20 72 65 74 72 79 69 6e 67 0d 0a 20 20 20 20 2a   retrying..    *
23c08 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
23c09 20 61 66 74 65 72 20 61 6e 79 20 65 72 72 6f 72   after any error
23c0a 20 6f 72 20 61 66 74 65 72 20 53 51 4c 49 54 45   or after SQLITE
23c0b 5f 44 4f 4e 45 2e 20 20 42 75 74 20 62 65 67 69  _DONE.  But begi
23c0c 6e 6e 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 77 69  nning..    ** wi
23c0d 74 68 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30  th version 3.7.0
23c0e 2c 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 69  , we changed thi
23c0f 73 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  s so that sqlite
23c10 33 5f 72 65 73 65 74 28 29 20 77 6f 75 6c 64 0d  3_reset() would.
23c11 0a 20 20 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65  .    ** be calle
23c12 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
23c13 69 6e 73 74 65 61 64 20 6f 66 20 74 68 72 6f 77  instead of throw
23c14 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d  ing the SQLITE_M
23c15 49 53 55 53 45 20 65 72 72 6f 72 2e 0d 0a 20 20  ISUSE error...  
23c16 20 20 2a 2a 20 54 68 69 73 20 22 61 75 74 6f 6d    ** This "autom
23c17 61 74 69 63 2d 72 65 73 65 74 22 20 63 68 61 6e  atic-reset" chan
23c18 67 65 20 69 73 20 6e 6f 74 20 74 65 63 68 6e 69  ge is not techni
23c19 63 61 6c 6c 79 20 61 6e 20 69 6e 63 6f 6d 70 61  cally an incompa
23c1a 74 69 62 69 6c 69 74 79 2c 20 0d 0a 20 20 20 20  tibility, ..    
23c1b 2a 2a 20 73 69 6e 63 65 20 61 6e 79 20 61 70 70  ** since any app
23c1c 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 72 65  lication that re
23c1d 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45  ceives an SQLITE
23c1e 5f 4d 49 53 55 53 45 20 69 73 20 62 72 6f 6b 65  _MISUSE is broke
23c1f 6e 20 62 79 0d 0a 20 20 20 20 2a 2a 20 64 65 66  n by..    ** def
23c20 69 6e 69 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a 2a  inition...    **
23c21 0d 0a 20 20 20 20 2a 2a 20 4e 65 76 65 72 74 68  ..    ** Neverth
23c22 65 6c 65 73 73 2c 20 73 6f 6d 65 20 70 75 62 6c  eless, some publ
23c23 69 73 68 65 64 20 61 70 70 6c 69 63 61 74 69 6f  ished applicatio
23c24 6e 73 20 74 68 61 74 20 77 65 72 65 20 6f 72 69  ns that were ori
23c25 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e 0d  ginally written.
23c26 0a 20 20 20 20 2a 2a 20 66 6f 72 20 76 65 72 73  .    ** for vers
23c27 69 6f 6e 20 33 2e 36 2e 32 33 20 6f 72 20 65 61  ion 3.6.23 or ea
23c28 72 6c 69 65 72 20 64 6f 20 69 6e 20 66 61 63 74  rlier do in fact
23c29 20 64 65 70 65 6e 64 20 6f 6e 20 53 51 4c 49 54   depend on SQLIT
23c2a 45 5f 4d 49 53 55 53 45 20 0d 0a 20 20 20 20 2a  E_MISUSE ..    *
23c2b 2a 20 72 65 74 75 72 6e 73 2c 20 61 6e 64 20 74  * returns, and t
23c2c 68 6f 73 65 20 77 65 72 65 20 62 72 6f 6b 65 6e  hose were broken
23c2d 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 69   by the automati
23c2e 63 2d 72 65 73 65 74 20 63 68 61 6e 67 65 2e 20  c-reset change. 
23c2f 20 41 73 20 61 0d 0a 20 20 20 20 2a 2a 20 61 20   As a..    ** a 
23c30 77 6f 72 6b 2d 61 72 6f 75 6e 64 2c 20 74 68 65  work-around, the
23c31 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
23c32 4f 52 45 53 45 54 20 63 6f 6d 70 69 6c 65 2d 74  ORESET compile-t
23c33 69 6d 65 20 72 65 73 74 6f 72 65 73 20 74 68 65  ime restores the
23c34 0d 0a 20 20 20 20 2a 2a 20 6c 65 67 61 63 79 20  ..    ** legacy 
23c35 62 65 68 61 76 69 6f 72 20 6f 66 20 72 65 74 75  behavior of retu
23c36 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4d 49 53  rning SQLITE_MIS
23c37 55 53 45 20 66 6f 72 20 63 61 73 65 73 20 77 68  USE for cases wh
23c38 65 72 65 20 74 68 65 20 0d 0a 20 20 20 20 2a 2a  ere the ..    **
23c39 20 70 72 65 76 69 6f 75 73 20 73 71 6c 69 74 65   previous sqlite
23c3a 33 5f 73 74 65 70 28 29 20 72 65 74 75 72 6e 65  3_step() returne
23c3b 64 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65  d something othe
23c3c 72 20 74 68 61 6e 20 61 20 53 51 4c 49 54 45 5f  r than a SQLITE_
23c3d 4c 4f 43 4b 45 44 0d 0a 20 20 20 20 2a 2a 20 6f  LOCKED..    ** o
23c3e 72 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72  r SQLITE_BUSY er
23c3f 72 6f 72 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 23 69  ror...    */..#i
23c40 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
23c41 5f 41 55 54 4f 52 45 53 45 54 0d 0a 20 20 20 20  _AUTORESET..    
23c42 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  if( p->rc==SQLIT
23c43 45 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d  E_BUSY || p->rc=
23c44 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
23c45 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
23c46 5f 72 65 73 65 74 28 28 73 71 6c 69 74 65 33 5f  _reset((sqlite3_
23c47 73 74 6d 74 2a 29 70 29 3b 0d 0a 20 20 20 20 7d  stmt*)p);..    }
23c48 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 65 74  else{..      ret
23c49 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
23c4a 45 5f 42 4b 50 54 3b 0d 0a 20 20 20 20 7d 0d 0a  E_BKPT;..    }..
23c4b 23 65 6c 73 65 0d 0a 20 20 20 20 73 71 6c 69 74  #else..    sqlit
23c4c 65 33 5f 72 65 73 65 74 28 28 73 71 6c 69 74 65  e3_reset((sqlite
23c4d 33 5f 73 74 6d 74 2a 29 70 29 3b 0d 0a 23 65 6e  3_stmt*)p);..#en
23c4e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  dif..  }....  /*
23c4f 20 43 68 65 63 6b 20 74 68 61 74 20 6d 61 6c 6c   Check that mall
23c50 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69  oc() has not fai
23c51 6c 65 64 2e 20 49 66 20 69 74 20 68 61 73 2c 20  led. If it has, 
23c52 72 65 74 75 72 6e 20 65 61 72 6c 79 2e 20 2a 2f  return early. */
23c53 0d 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0d  ..  db = p->db;.
23c54 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
23c55 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20  cFailed ){..    
23c56 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  p->rc = SQLITE_N
23c57 4f 4d 45 4d 3b 0d 0a 20 20 20 20 72 65 74 75 72  OMEM;..    retur
23c58 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
23c59 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 2d  .  }....  if( p-
23c5a 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70  >pc<=0 && p->exp
23c5b 69 72 65 64 20 29 7b 0d 0a 20 20 20 20 70 2d 3e  ired ){..    p->
23c5c 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45  rc = SQLITE_SCHE
23c5d 4d 41 3b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51  MA;..    rc = SQ
23c5e 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20  LITE_ERROR;..   
23c5f 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65   goto end_of_ste
23c60 70 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  p;..  }..  if( p
23c61 2d 3e 70 63 3c 30 20 29 7b 0d 0a 20 20 20 20 2f  ->pc<0 ){..    /
23c62 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  * If there are n
23c63 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e  o other statemen
23c64 74 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e  ts currently run
23c65 6e 69 6e 67 2c 20 74 68 65 6e 0d 0a 20 20 20 20  ning, then..    
23c66 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 6e 74  ** reset the int
23c67 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68  errupt flag.  Th
23c68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 63 61  is prevents a ca
23c69 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ll to sqlite3_in
23c6a 74 65 72 72 75 70 74 0d 0a 20 20 20 20 2a 2a 20  terrupt..    ** 
23c6b 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 6e  from interruptin
23c6c 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68  g a statement th
23c6d 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 73  at has not yet s
23c6e 74 61 72 74 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d  tarted...    */.
23c6f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
23c70 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
23c71 0d 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69  ..      db->u1.i
23c72 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30  sInterrupted = 0
23c73 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
23c74 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72 69 74  assert( db->writ
23c75 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62  eVdbeCnt>0 || db
23c76 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20  ->autoCommit==0 
23c77 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64  || db->nDeferred
23c78 43 6f 6e 73 3d 3d 30 20 29 3b 0d 0a 0d 0a 23 69  Cons==0 );....#i
23c79 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23c7a 54 5f 54 52 41 43 45 0d 0a 20 20 20 20 69 66 28  T_TRACE..    if(
23c7b 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26   db->xProfile &&
23c7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
23c7d 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
23c7e 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 49 6e  3OsCurrentTimeIn
23c7f 74 36 34 28 64 62 2d 3e 70 56 66 73 2c 20 26 70  t64(db->pVfs, &p
23c80 2d 3e 73 74 61 72 74 54 69 6d 65 29 3b 0d 0a 20  ->startTime);.. 
23c81 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a     }..#endif....
23c82 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64      db->activeVd
23c83 62 65 43 6e 74 2b 2b 3b 0d 0a 20 20 20 20 69 66  beCnt++;..    if
23c84 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30  ( p->readOnly==0
23c85 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65   ) db->writeVdbe
23c86 43 6e 74 2b 2b 3b 0d 0a 20 20 20 20 70 2d 3e 70  Cnt++;..    p->p
23c87 63 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 23 69 66  c = 0;..  }..#if
23c88 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23c89 5f 45 58 50 4c 41 49 4e 0d 0a 20 20 69 66 28 20  _EXPLAIN..  if( 
23c8a 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0d 0a 20  p->explain ){.. 
23c8b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
23c8c 64 62 65 4c 69 73 74 28 70 29 3b 0d 0a 20 20 7d  dbeList(p);..  }
23c8d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  else..#endif /* 
23c8e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
23c8f 41 49 4e 20 2a 2f 0d 0a 20 20 7b 0d 0a 20 20 20  AIN */..  {..   
23c90 20 64 62 2d 3e 76 64 62 65 45 78 65 63 43 6e 74   db->vdbeExecCnt
23c91 2b 2b 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71  ++;..    rc = sq
23c92 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29  lite3VdbeExec(p)
23c93 3b 0d 0a 20 20 20 20 64 62 2d 3e 76 64 62 65 45  ;..    db->vdbeE
23c94 78 65 63 43 6e 74 2d 2d 3b 0d 0a 20 20 7d 0d 0a  xecCnt--;..  }..
23c95 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
23c96 5f 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 20 20 2f  _OMIT_TRACE..  /
23c97 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f  * Invoke the pro
23c98 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66  file callback if
23c99 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0d 0a 20   there is one.. 
23c9a 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 21 3d 53   */..  if( rc!=S
23c9b 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 64 62 2d  QLITE_ROW && db-
23c9c 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62  >xProfile && !db
23c9d 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70  ->init.busy && p
23c9e 2d 3e 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 73  ->zSql ){..    s
23c9f 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f  qlite3_int64 iNo
23ca0 77 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  w;..    sqlite3O
23ca1 73 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36  sCurrentTimeInt6
23ca2 34 28 64 62 2d 3e 70 56 66 73 2c 20 26 69 4e 6f  4(db->pVfs, &iNo
23ca3 77 29 3b 0d 0a 20 20 20 20 64 62 2d 3e 78 50 72  w);..    db->xPr
23ca4 6f 66 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69  ofile(db->pProfi
23ca5 6c 65 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20  leArg, p->zSql, 
23ca6 28 69 4e 6f 77 20 2d 20 70 2d 3e 73 74 61 72 74  (iNow - p->start
23ca7 54 69 6d 65 29 2a 31 30 30 30 30 30 30 29 3b 0d  Time)*1000000);.
23ca8 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
23ca9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
23caa 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20 20 20 61 73  _DONE ){..    as
23cab 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c  sert( p->rc==SQL
23cac 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 70  ITE_OK );..    p
23cad 2d 3e 72 63 20 3d 20 64 6f 57 61 6c 43 61 6c 6c  ->rc = doWalCall
23cae 62 61 63 6b 73 28 64 62 29 3b 0d 0a 20 20 20 20  backs(db);..    
23caf 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54  if( p->rc!=SQLIT
23cb0 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72  E_OK ){..      r
23cb1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
23cb2 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
23cb3 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d  .  db->errCode =
23cb4 20 72 63 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49   rc;..  if( SQLI
23cb5 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65  TE_NOMEM==sqlite
23cb6 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
23cb7 70 2d 3e 72 63 29 20 29 7b 0d 0a 20 20 20 20 70  p->rc) ){..    p
23cb8 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
23cb9 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 65 6e 64 5f 6f  MEM;..  }..end_o
23cba 66 5f 73 74 65 70 3a 0d 0a 20 20 2f 2a 20 41 74  f_step:..  /* At
23cbb 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 61   this point loca
23cbc 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 6f  l variable rc ho
23cbd 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 68  lds the value th
23cbe 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0d 0a 20  at should be .. 
23cbf 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20   ** returned if 
23cc0 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77  this statement w
23cc1 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  as compiled usin
23cc2 67 20 74 68 65 20 6c 65 67 61 63 79 20 0d 0a 20  g the legacy .. 
23cc3 20 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70   ** sqlite3_prep
23cc4 61 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e  are() interface.
23cc5 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   According to th
23cc6 65 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e  e docs, this can
23cc7 20 6f 6e 6c 79 0d 0a 20 20 2a 2a 20 62 65 20 6f   only..  ** be o
23cc8 6e 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  ne of the values
23cc9 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 73   in the first as
23cca 73 65 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61  sert() below. Va
23ccb 72 69 61 62 6c 65 20 70 2d 3e 72 63 20 0d 0a 20  riable p->rc .. 
23ccc 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   ** contains the
23ccd 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c   value that woul
23cce 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  d be returned if
23ccf 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
23cd0 65 28 29 20 0d 0a 20 20 2a 2a 20 77 65 72 65 20  e() ..  ** were 
23cd1 63 61 6c 6c 65 64 20 6f 6e 20 73 74 61 74 65 6d  called on statem
23cd2 65 6e 74 20 70 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ent p...  */..  
23cd3 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
23cd4 54 45 5f 52 4f 57 20 20 7c 7c 20 72 63 3d 3d 53  TE_ROW  || rc==S
23cd5 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 7c 7c 20  QLITE_DONE   || 
23cd6 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52  rc==SQLITE_ERROR
23cd7 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 72 63 3d   ..       || rc=
23cd8 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20  =SQLITE_BUSY || 
23cd9 72 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53  rc==SQLITE_MISUS
23cda 45 0d 0a 20 20 29 3b 0d 0a 20 20 61 73 73 65 72  E..  );..  asser
23cdb 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45  t( p->rc!=SQLITE
23cdc 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 21 3d 53  _ROW && p->rc!=S
23cdd 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0d 0a 20  QLITE_DONE );.. 
23cde 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72   if( p->isPrepar
23cdf 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c 49 54  eV2 && rc!=SQLIT
23ce0 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c  E_ROW && rc!=SQL
23ce1 49 54 45 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20 20  ITE_DONE ){..   
23ce2 20 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74   /* If this stat
23ce3 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72  ement was prepar
23ce4 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
23ce5 5f 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 61  _prepare_v2(), a
23ce6 6e 64 20 61 6e 0d 0a 20 20 20 20 2a 2a 20 65 72  nd an..    ** er
23ce7 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c  ror has occured,
23ce8 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65   then return the
23ce9 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70   error code in p
23cea 2d 3e 72 63 20 74 6f 20 74 68 65 0d 0a 20 20 20  ->rc to the..   
23ceb 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 20   ** caller. Set 
23cec 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  the error code i
23ced 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  n the database h
23cee 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 73 61 6d  andle to the sam
23cef 65 20 76 61 6c 75 65 2e 0d 0a 20 20 20 20 2a 2f  e value...    */
23cf0 20 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   ..    rc = sqli
23cf1 74 65 33 56 64 62 65 54 72 61 6e 73 66 65 72 45  te3VdbeTransferE
23cf2 72 72 6f 72 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20  rror(p);..  }.. 
23cf3 20 72 65 74 75 72 6e 20 28 72 63 26 64 62 2d 3e   return (rc&db->
23cf4 65 72 72 4d 61 73 6b 29 3b 0d 0a 7d 0d 0a 0d 0a  errMask);..}....
23cf5 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  /*..** The maxim
23cf6 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  um number of tim
23cf7 65 73 20 74 68 61 74 20 61 20 73 74 61 74 65 6d  es that a statem
23cf8 65 6e 74 20 77 69 6c 6c 20 74 72 79 20 74 6f 20  ent will try to 
23cf9 72 65 70 61 72 73 65 0d 0a 2a 2a 20 69 74 73 65  reparse..** itse
23cfa 6c 66 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67  lf before giving
23cfb 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e   up and returnin
23cfc 67 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e  g SQLITE_SCHEMA.
23cfd 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
23cfe 4c 49 54 45 5f 4d 41 58 5f 53 43 48 45 4d 41 5f  LITE_MAX_SCHEMA_
23cff 52 45 54 52 59 0d 0a 23 20 64 65 66 69 6e 65 20  RETRY..# define 
23d00 53 51 4c 49 54 45 5f 4d 41 58 5f 53 43 48 45 4d  SQLITE_MAX_SCHEM
23d01 41 5f 52 45 54 52 59 20 35 0d 0a 23 65 6e 64 69  A_RETRY 5..#endi
23d02 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  f..../*..** This
23d03 20 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65   is the top-leve
23d04 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  l implementation
23d05 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70   of sqlite3_step
23d06 28 29 2e 20 20 43 61 6c 6c 0d 0a 2a 2a 20 73 71  ().  Call..** sq
23d07 6c 69 74 65 33 53 74 65 70 28 29 20 74 6f 20 64  lite3Step() to d
23d08 6f 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f  o most of the wo
23d09 72 6b 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61  rk.  If a schema
23d0a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0d 0a   error occurs,..
23d0b 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 52  ** call sqlite3R
23d0c 65 70 72 65 70 61 72 65 28 29 20 61 6e 64 20 74  eprepare() and t
23d0d 72 79 20 61 67 61 69 6e 2e 0d 0a 2a 2f 0d 0a 53  ry again...*/..S
23d0e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
23d0f 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74  lite3_step(sqlit
23d10 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
23d11 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
23d12 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20  ITE_OK;      /* 
23d13 52 65 73 75 6c 74 20 66 72 6f 6d 20 73 71 6c 69  Result from sqli
23d14 74 65 33 53 74 65 70 28 29 20 2a 2f 0d 0a 20 20  te3Step() */..  
23d15 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45  int rc2 = SQLITE
23d16 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 73 75  _OK;     /* Resu
23d17 6c 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 52  lt from sqlite3R
23d18 65 70 72 65 70 61 72 65 28 29 20 2a 2f 0d 0a 20  eprepare() */.. 
23d19 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
23d1a 2a 29 70 53 74 6d 74 3b 20 20 2f 2a 20 74 68 65  *)pStmt;  /* the
23d1b 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
23d1c 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 6e  ent */..  int cn
23d1d 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
23d1e 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f     /* Counter to
23d1f 20 70 72 65 76 65 6e 74 20 69 6e 66 69 6e 69 74   prevent infinit
23d20 65 20 6c 6f 6f 70 20 6f 66 20 72 65 70 72 65 70  e loop of reprep
23d21 61 72 65 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ares */..  sqlit
23d22 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
23d23 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
23d24 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
23d25 2f 0d 0a 0d 0a 20 20 69 66 28 20 76 64 62 65 53  /....  if( vdbeS
23d26 61 66 65 74 79 4e 6f 74 4e 75 6c 6c 28 76 29 20  afetyNotNull(v) 
23d27 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ){..    return S
23d28 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50  QLITE_MISUSE_BKP
23d29 54 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 20 3d 20  T;..  }..  db = 
23d2a 76 2d 3e 64 62 3b 0d 0a 20 20 73 71 6c 69 74 65  v->db;..  sqlite
23d2b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
23d2c 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 77 68 69  ->mutex);..  whi
23d2d 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  le( (rc = sqlite
23d2e 33 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54  3Step(v))==SQLIT
23d2f 45 5f 53 43 48 45 4d 41 0d 0a 20 20 20 20 20 20  E_SCHEMA..      
23d30 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 53 51     && cnt++ < SQ
23d31 4c 49 54 45 5f 4d 41 58 5f 53 43 48 45 4d 41 5f  LITE_MAX_SCHEMA_
23d32 52 45 54 52 59 0d 0a 20 20 20 20 20 20 20 20 20  RETRY..         
23d33 26 26 20 28 72 63 32 20 3d 20 72 63 20 3d 20 73  && (rc2 = rc = s
23d34 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
23d35 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  v))==SQLITE_OK )
23d36 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  {..    sqlite3_r
23d37 65 73 65 74 28 70 53 74 6d 74 29 3b 0d 0a 20 20  eset(pStmt);..  
23d38 20 20 61 73 73 65 72 74 28 20 76 2d 3e 65 78 70    assert( v->exp
23d39 69 72 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 7d 0d  ired==0 );..  }.
23d3a 0a 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49  .  if( rc2!=SQLI
23d3b 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41 59 53 28  TE_OK && ALWAYS(
23d3c 76 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20  v->isPrepareV2) 
23d3d 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45  && ALWAYS(db->pE
23d3e 72 72 29 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54  rr) ){..    /* T
23d3f 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20  his case occurs 
23d40 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f  after failing to
23d41 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71   recompile an sq
23d42 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0d 0a 20  l statement. .. 
23d43 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20     ** The error 
23d44 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65  message from the
23d45 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61   SQL compiler ha
23d46 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c  s already been l
23d47 6f 61 64 65 64 20 0d 0a 20 20 20 20 2a 2a 20 69  oaded ..    ** i
23d48 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
23d49 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62 6c   handle. This bl
23d4a 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 65  ock copies the e
23d4b 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0d 0a 20  rror message .. 
23d4c 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64     ** from the d
23d4d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69  atabase handle i
23d4e 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e  nto the statemen
23d4f 74 20 61 6e 64 20 73 65 74 73 20 74 68 65 20 73  t and sets the s
23d50 74 61 74 65 6d 65 6e 74 0d 0a 20 20 20 20 2a 2a  tatement..    **
23d51 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   program counter
23d52 20 74 6f 20 30 20 74 6f 20 65 6e 73 75 72 65 20   to 0 to ensure 
23d53 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 73 74  that when the st
23d54 61 74 65 6d 65 6e 74 20 69 73 20 0d 0a 20 20 20  atement is ..   
23d55 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72   ** finalized or
23d56 20 72 65 73 65 74 20 74 68 65 20 70 61 72 73 65   reset the parse
23d57 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  r error message 
23d58 69 73 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61  is available via
23d59 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  ..    ** sqlite3
23d5a 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
23d5b 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e  lite3_errcode().
23d5c 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 6f  ..    */..    co
23d5d 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  nst char *zErr =
23d5e 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
23d5f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
23d60 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0d 0a 20  t(db->pErr); .. 
23d61 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23d62 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29  (db, v->zErrMsg)
23d63 3b 0d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e  ;..    if( !db->
23d64 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
23d65 0a 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73  .      v->zErrMs
23d66 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
23d67 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0d 0a  Dup(db, zErr);..
23d68 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63        v->rc = rc
23d69 32 3b 0d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  2;..    } else {
23d6a 0d 0a 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d  ..      v->zErrM
23d6b 73 67 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 76  sg = 0;..      v
23d6c 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49  ->rc = rc = SQLI
23d6d 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d  TE_NOMEM;..    }
23d6e 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71  ..  }..  rc = sq
23d6f 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
23d70 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33   rc);..  sqlite3
23d71 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
23d72 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75  >mutex);..  retu
23d73 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
23d74 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
23d75 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61  user data from a
23d76 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
23d77 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
23d78 65 74 75 72 6e 20 61 0d 0a 2a 2a 20 70 6f 69 6e  eturn a..** poin
23d79 74 65 72 20 74 6f 20 69 74 2e 0d 0a 2a 2f 0d 0a  ter to it...*/..
23d7a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
23d7b 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  *sqlite3_user_da
23d7c 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
23d7d 78 74 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72  xt *p){..  asser
23d7e 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63  t( p && p->pFunc
23d7f 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 2d   );..  return p-
23d80 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72 44 61 74  >pFunc->pUserDat
23d81 61 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  a;..}..../*..** 
23d82 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72  Extract the user
23d83 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c   data from a sql
23d84 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72  ite3_context str
23d85 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
23d86 6e 20 61 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  n a..** pointer 
23d87 74 6f 20 69 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  to it...**..** I
23d88 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46  MPLEMENTATION-OF
23d89 3a 20 52 2d 34 36 37 39 38 2d 35 30 33 30 31 20  : R-46798-50301 
23d8a 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  The sqlite3_cont
23d8b 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20  ext_db_handle() 
23d8c 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2a 20 72 65  interface..** re
23d8d 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20  turns a copy of 
23d8e 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  the pointer to t
23d8f 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
23d90 65 63 74 69 6f 6e 20 28 74 68 65 20 31 73 74 0d  ection (the 1st.
23d91 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 29 20 6f  .** parameter) o
23d92 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72  f the sqlite3_cr
23d93 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20  eate_function() 
23d94 61 6e 64 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  and..** sqlite3_
23d95 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
23d96 36 28 29 20 72 6f 75 74 69 6e 65 73 20 74 68 61  6() routines tha
23d97 74 20 6f 72 69 67 69 6e 61 6c 6c 79 20 72 65 67  t originally reg
23d98 69 73 74 65 72 65 64 20 74 68 65 0d 0a 2a 2a 20  istered the..** 
23d99 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69  application defi
23d9a 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a  ned function...*
23d9b 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  /..SQLITE_API sq
23d9c 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63  lite3 *sqlite3_c
23d9d 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
23d9e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
23d9f 20 2a 70 29 7b 0d 0a 20 20 61 73 73 65 72 74 28   *p){..  assert(
23da0 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29   p && p->pFunc )
23da1 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73  ;..  return p->s
23da2 2e 64 62 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .db;..}..../*..*
23da3 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
23da4 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
23da5 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20  ation of an SQL 
23da6 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 6c  function that al
23da7 77 61 79 73 0d 0a 2a 2a 20 66 61 69 6c 73 20 77  ways..** fails w
23da8 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
23da9 73 61 67 65 20 73 74 61 74 69 6e 67 20 74 68 61  sage stating tha
23daa 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  t the function i
23dab 73 20 75 73 65 64 20 69 6e 20 74 68 65 0d 0a 2a  s used in the..*
23dac 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74 2e  * wrong context.
23dad 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 76    The sqlite3_ov
23dae 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
23daf 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f 6e 73  ) API might cons
23db0 74 72 75 63 74 0d 0a 2a 2a 20 53 51 4c 20 66 75  truct..** SQL fu
23db1 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 20  nction that use 
23db2 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f 20  this routine so 
23db3 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
23db4 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0d 0a 2a  ns will exist..*
23db5 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c  * for name resol
23db6 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63  ution but are ac
23db7 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65  tually overloade
23db8 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75  d by the xFindFu
23db9 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 65 74 68 6f  nction..** metho
23dba 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62  d of virtual tab
23dbb 6c 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  les...*/..SQLITE
23dbc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23dbd 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
23dbe 74 69 6f 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33  tion(..  sqlite3
23dbf 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23dc0 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74  t,  /* The funct
23dc1 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74  ion calling cont
23dc2 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 4e 6f  ext */..  int No
23dc3 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 20 20  tUsed,          
23dc4 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
23dc5 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
23dc6 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a  he function */..
23dc7 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
23dc8 2a 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a 20  **NotUsed2   /* 
23dc9 56 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72  Value of each ar
23dca 67 75 6d 65 6e 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  gument */..){.. 
23dcb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
23dcc 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46  me = context->pF
23dcd 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 63  unc->zName;..  c
23dce 68 61 72 20 2a 7a 45 72 72 3b 0d 0a 20 20 55 4e  har *zErr;..  UN
23dcf 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
23dd0 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
23dd1 32 29 3b 0d 0a 20 20 7a 45 72 72 20 3d 20 73 71  2);..  zErr = sq
23dd2 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a  lite3_mprintf(..
23dd3 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f        "unable to
23dd4 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73   use function %s
23dd5 20 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65   in the requeste
23dd6 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d  d context", zNam
23dd7 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72  e);..  sqlite3_r
23dd8 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
23dd9 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d  ext, zErr, -1);.
23dda 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
23ddb 7a 45 72 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  zErr);..}..../*.
23ddc 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20  .** Allocate or 
23ddd 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65  return the aggre
23dde 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  gate context for
23ddf 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
23de0 2e 20 20 41 20 6e 65 77 0d 0a 2a 2a 20 63 6f 6e  .  A new..** con
23de1 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  text is allocate
23de2 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63  d on the first c
23de3 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74  all.  Subsequent
23de4 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68   calls return th
23de5 65 0d 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65  e..** same conte
23de6 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74 75  xt that was retu
23de7 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61  rned on prior ca
23de8 6c 6c 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  lls...*/..SQLITE
23de9 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
23dea 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
23deb 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  text(sqlite3_con
23dec 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79  text *p, int nBy
23ded 74 65 29 7b 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65  te){..  Mem *pMe
23dee 6d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 20  m;..  assert( p 
23def 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70  && p->pFunc && p
23df0 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29  ->pFunc->xStep )
23df1 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
23df2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
23df3 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  p->s.db->mutex) 
23df4 29 3b 0d 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e  );..  pMem = p->
23df5 70 4d 65 6d 3b 0d 0a 20 20 74 65 73 74 63 61 73  pMem;..  testcas
23df6 65 28 20 6e 42 79 74 65 3c 30 20 29 3b 0d 0a 20  e( nByte<0 );.. 
23df7 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67   if( (pMem->flag
23df8 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20  s & MEM_Agg)==0 
23df9 29 7b 0d 0a 20 20 20 20 69 66 28 20 6e 42 79 74  ){..    if( nByt
23dfa 65 3c 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73  e<=0 ){..      s
23dfb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
23dfc 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65  easeExternal(pMe
23dfd 6d 29 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d  m);..      pMem-
23dfe 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
23dff 6c 3b 0d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  l;..      pMem->
23e00 7a 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73  z = 0;..    }els
23e01 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e{..      sqlite
23e02 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
23e03 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0d 0a 20  m, nByte, 0);.. 
23e04 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
23e05 20 3d 20 4d 45 4d 5f 41 67 67 3b 0d 0a 20 20 20   = MEM_Agg;..   
23e06 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20     pMem->u.pDef 
23e07 3d 20 70 2d 3e 70 46 75 6e 63 3b 0d 0a 20 20 20  = p->pFunc;..   
23e08 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29     if( pMem->z )
23e09 7b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  {..        memse
23e0a 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42  t(pMem->z, 0, nB
23e0b 79 74 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  yte);..      }..
23e0c 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
23e0d 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d  turn (void*)pMem
23e0e 2d 3e 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ->z;..}..../*..*
23e0f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 75 78  * Return the aux
23e10 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74  ilary data point
23e11 65 72 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20  er, if any, for 
23e12 74 68 65 20 69 41 72 67 27 74 68 20 61 72 67 75  the iArg'th argu
23e13 6d 65 6e 74 20 74 6f 0d 0a 2a 2a 20 74 68 65 20  ment to..** the 
23e14 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65  user-function de
23e15 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 0d 0a  fined by pCtx...
23e16 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  */..SQLITE_API v
23e17 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74  oid *sqlite3_get
23e18 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33  _auxdata(sqlite3
23e19 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
23e1a 69 6e 74 20 69 41 72 67 29 7b 0d 0a 20 20 56 64  int iArg){..  Vd
23e1b 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
23e1c 63 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  c;....  assert( 
23e1d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
23e1e 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
23e1f 75 74 65 78 29 20 29 3b 0d 0a 20 20 70 56 64 62  utex) );..  pVdb
23e20 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56  eFunc = pCtx->pV
23e21 64 62 65 46 75 6e 63 3b 0d 0a 20 20 69 66 28 20  dbeFunc;..  if( 
23e22 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41  !pVdbeFunc || iA
23e23 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e  rg>=pVdbeFunc->n
23e24 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b  Aux || iArg<0 ){
23e25 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
23e26 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
23e27 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
23e28 69 41 72 67 5d 2e 70 41 75 78 3b 0d 0a 7d 0d 0a  iArg].pAux;..}..
23e29 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65  ../*..** Set the
23e2a 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 70   auxilary data p
23e2b 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74  ointer and delet
23e2c 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20  e function, for 
23e2d 74 68 65 20 69 41 72 67 27 74 68 0d 0a 2a 2a 20  the iArg'th..** 
23e2e 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
23e2f 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65  user-function de
23e30 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41  fined by pCtx. A
23e31 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
23e32 65 20 69 73 0d 0a 2a 2a 20 64 65 6c 65 74 65 64  e is..** deleted
23e33 20 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20   by calling the 
23e34 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20  delete function 
23e35 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 69  specified when i
23e36 74 20 77 61 73 20 73 65 74 2e 0d 0a 2a 2f 0d 0a  t was set...*/..
23e37 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
23e38 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
23e39 61 74 61 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ata(..  sqlite3_
23e3a 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0d  context *pCtx, .
23e3b 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0d 0a 20  .  int iArg, .. 
23e3c 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0d 0a 20   void *pAux, .. 
23e3d 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29   void (*xDelete)
23e3e 28 76 6f 69 64 2a 29 0d 0a 29 7b 0d 0a 20 20 73  (void*)..){..  s
23e3f 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70  truct AuxData *p
23e40 41 75 78 44 61 74 61 3b 0d 0a 20 20 56 64 62 65  AuxData;..  Vdbe
23e41 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b  Func *pVdbeFunc;
23e42 0d 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29  ..  if( iArg<0 )
23e43 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0d 0a 0d   goto failed;...
23e44 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
23e45 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
23e46 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
23e47 20 29 3b 0d 0a 20 20 70 56 64 62 65 46 75 6e 63   );..  pVdbeFunc
23e48 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75   = pCtx->pVdbeFu
23e49 6e 63 3b 0d 0a 20 20 69 66 28 20 21 70 56 64 62  nc;..  if( !pVdb
23e4a 65 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75  eFunc || pVdbeFu
23e4b 6e 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29  nc->nAux<=iArg )
23e4c 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20  {..    int nAux 
23e4d 3d 20 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70  = (pVdbeFunc ? p
23e4e 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a  VdbeFunc->nAux :
23e4f 20 30 29 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 4d   0);..    int nM
23e50 61 6c 6c 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56  alloc = sizeof(V
23e51 64 62 65 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f  dbeFunc) + sizeo
23e52 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61  f(struct AuxData
23e53 29 2a 69 41 72 67 3b 0d 0a 20 20 20 20 70 56 64  )*iArg;..    pVd
23e54 62 65 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33  beFunc = sqlite3
23e55 44 62 52 65 61 6c 6c 6f 63 28 70 43 74 78 2d 3e  DbRealloc(pCtx->
23e56 73 2e 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2c  s.db, pVdbeFunc,
23e57 20 6e 4d 61 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20   nMalloc);..    
23e58 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 29  if( !pVdbeFunc )
23e59 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 61  {..      goto fa
23e5a 69 6c 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  iled;..    }..  
23e5b 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e    pCtx->pVdbeFun
23e5c 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0d 0a  c = pVdbeFunc;..
23e5d 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62      memset(&pVdb
23e5e 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75  eFunc->apAux[nAu
23e5f 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74  x], 0, sizeof(st
23e60 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69  ruct AuxData)*(i
23e61 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0d 0a 20  Arg+1-nAux));.. 
23e62 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41     pVdbeFunc->nA
23e63 75 78 20 3d 20 69 41 72 67 2b 31 3b 0d 0a 20 20  ux = iArg+1;..  
23e64 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75    pVdbeFunc->pFu
23e65 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63  nc = pCtx->pFunc
23e66 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 41 75 78  ;..  }....  pAux
23e67 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e  Data = &pVdbeFun
23e68 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0d  c->apAux[iArg];.
23e69 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d  .  if( pAuxData-
23e6a 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74  >pAux && pAuxDat
23e6b 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0d 0a 20  a->xDelete ){.. 
23e6c 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65     pAuxData->xDe
23e6d 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70  lete(pAuxData->p
23e6e 41 75 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 41  Aux);..  }..  pA
23e6f 75 78 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70  uxData->pAux = p
23e70 41 75 78 3b 0d 0a 20 20 70 41 75 78 44 61 74 61  Aux;..  pAuxData
23e71 2d 3e 78 44 65 6c 65 74 65 20 3d 20 78 44 65 6c  ->xDelete = xDel
23e72 65 74 65 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d  ete;..  return;.
23e73 0a 0d 0a 66 61 69 6c 65 64 3a 0d 0a 20 20 69 66  ...failed:..  if
23e74 28 20 78 44 65 6c 65 74 65 20 29 7b 0d 0a 20 20  ( xDelete ){..  
23e75 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b    xDelete(pAux);
23e76 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e  ..  }..}....#ifn
23e77 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23e78 44 45 50 52 45 43 41 54 45 44 0d 0a 2f 2a 0d 0a  DEPRECATED../*..
23e79 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
23e7a 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
23e7b 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e Step function 
23e7c 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68  of a aggregate h
23e7d 61 73 20 62 65 65 6e 20 0d 0a 2a 2a 20 63 61 6c  as been ..** cal
23e7e 6c 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  led...**..** Thi
23e7f 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65  s function is de
23e80 70 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f  precated.  Do no
23e81 74 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77  t use it for new
23e82 20 63 6f 64 65 2e 20 20 49 74 20 69 73 0d 0a 2a   code.  It is..*
23e83 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74  * provide only t
23e84 6f 20 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67  o avoid breaking
23e85 20 6c 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e   legacy code.  N
23e86 65 77 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ew aggregate fun
23e87 63 74 69 6f 6e 0d 0a 2a 2a 20 69 6d 70 6c 65 6d  ction..** implem
23e88 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  entations should
23e89 20 6b 65 65 70 20 74 68 65 69 72 20 6f 77 6e 20   keep their own 
23e8a 63 6f 75 6e 74 73 20 77 69 74 68 69 6e 20 74 68  counts within th
23e8b 65 69 72 20 61 67 67 72 65 67 61 74 65 0d 0a 2a  eir aggregate..*
23e8c 2a 20 63 6f 6e 74 65 78 74 2e 0d 0a 2a 2f 0d 0a  * context...*/..
23e8d 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23e8e 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
23e8f 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63  _count(sqlite3_c
23e90 6f 6e 74 65 78 74 20 2a 70 29 7b 0d 0a 20 20 61  ontext *p){..  a
23e91 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
23e92 4d 65 6d 20 26 26 20 70 2d 3e 70 46 75 6e 63 20  Mem && p->pFunc 
23e93 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74  && p->pFunc->xSt
23e94 65 70 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ep );..  return 
23e95 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0d 0a 7d 0d 0a  p->pMem->n;..}..
23e96 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
23e97 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
23e98 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
23e99 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
23e9a 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  for the statemen
23e9b 74 20 70 53 74 6d 74 2e 0d 0a 2a 2f 0d 0a 53 51  t pStmt...*/..SQ
23e9c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
23e9d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
23e9e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
23e9f 70 53 74 6d 74 29 7b 0d 0a 20 20 56 64 62 65 20  pStmt){..  Vdbe 
23ea0 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70  *pVm = (Vdbe *)p
23ea1 53 74 6d 74 3b 0d 0a 20 20 72 65 74 75 72 6e 20  Stmt;..  return 
23ea2 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43  pVm ? pVm->nResC
23ea3 6f 6c 75 6d 6e 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d  olumn : 0;..}...
23ea4 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./*..** Return t
23ea5 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
23ea6 75 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 72  ues available fr
23ea7 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  om the current r
23ea8 6f 77 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 63 75  ow of the..** cu
23ea9 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
23eaa 67 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  g statement pStm
23eab 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  t...*/..SQLITE_A
23eac 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
23ead 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ata_count(sqlite
23eae 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0d  3_stmt *pStmt){.
23eaf 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28  .  Vdbe *pVm = (
23eb0 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0d 0a 20  Vdbe *)pStmt;.. 
23eb1 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70   if( pVm==0 || p
23eb2 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d  Vm->pResultSet==
23eb3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20  0 ) return 0;.. 
23eb4 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65   return pVm->nRe
23eb5 73 43 6f 6c 75 6d 6e 3b 0d 0a 7d 0d 0a 0d 0a 0d  sColumn;..}.....
23eb6 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ./*..** Check to
23eb7 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69   see if column i
23eb8 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e  Col of the given
23eb9 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61   statement is va
23eba 6c 69 64 2e 20 20 49 66 0d 0a 2a 2a 20 69 74 20  lid.  If..** it 
23ebb 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  is, return a poi
23ebc 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20  nter to the Mem 
23ebd 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66  for the value of
23ebe 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0d 0a 2a   that column...*
23ebf 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74  * If iCol is not
23ec0 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61   valid, return a
23ec1 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
23ec2 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61  m which has a va
23ec3 6c 75 65 0d 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e  lue..** of NULL.
23ec4 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 4d 65 6d  ..*/..static Mem
23ec5 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69   *columnMem(sqli
23ec6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
23ec7 20 69 6e 74 20 69 29 7b 0d 0a 20 20 56 64 62 65   int i){..  Vdbe
23ec8 20 2a 70 56 6d 3b 0d 0a 20 20 4d 65 6d 20 2a 70   *pVm;..  Mem *p
23ec9 4f 75 74 3b 0d 0a 0d 0a 20 20 70 56 6d 20 3d 20  Out;....  pVm = 
23eca 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0d 0a  (Vdbe *)pStmt;..
23ecb 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d    if( pVm && pVm
23ecc 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20  ->pResultSet!=0 
23ecd 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f  && i<pVm->nResCo
23ece 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0d  lumn && i>=0 ){.
23ecf 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
23ed0 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
23ed1 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20 70  ->mutex);..    p
23ed2 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73  Out = &pVm->pRes
23ed3 75 6c 74 53 65 74 5b 69 5d 3b 0d 0a 20 20 7d 65  ultSet[i];..  }e
23ed4 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20  lse{..    /* If 
23ed5 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
23ed6 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
23ed7 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f  rgument is out o
23ed8 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e 0d  f range, return.
23ed9 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65  .    ** a pointe
23eda 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  r to the followi
23edb 6e 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f 62  ng static Mem ob
23edc 6a 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 61  ject which conta
23edd 69 6e 73 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  ins the..    ** 
23ede 76 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20  value SQL NULL. 
23edf 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  Even though the 
23ee0 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f  Mem structure co
23ee1 6e 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e  ntains an elemen
23ee2 74 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70  t..    ** of typ
23ee3 65 20 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 69  e i64, on certai
23ee4 6e 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20  n architectures 
23ee5 28 78 38 36 29 20 77 69 74 68 20 63 65 72 74 61  (x86) with certa
23ee6 69 6e 20 63 6f 6d 70 69 6c 65 72 0d 0a 20 20 20  in compiler..   
23ee7 20 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d 4f   ** switches (-O
23ee8 73 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69 67  s), gcc may alig
23ee9 6e 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65 63  n this Mem objec
23eea 74 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62 6f  t on a 4-byte bo
23eeb 75 6e 64 61 72 79 0d 0a 20 20 20 20 2a 2a 20 69  undary..    ** i
23eec 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62  nstead of an 8-b
23eed 79 74 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c  yte one. This al
23eee 6c 20 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78  l works fine, ex
23eef 63 65 70 74 20 74 68 61 74 20 77 68 65 6e 0d 0a  cept that when..
23ef0 20 20 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77      ** running w
23ef1 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47  ith SQLITE_DEBUG
23ef2 20 64 65 66 69 6e 65 64 20 74 68 65 20 53 51 4c   defined the SQL
23ef3 69 74 65 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d  ite code sometim
23ef4 65 73 20 61 73 73 65 72 74 28 29 73 0d 0a 20 20  es assert()s..  
23ef5 20 20 2a 2a 20 74 68 61 74 20 61 20 4d 65 6d 20    ** that a Mem 
23ef6 73 74 72 75 63 74 75 72 65 20 69 73 20 6c 6f 63  structure is loc
23ef7 61 74 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74  ated on an 8-byt
23ef8 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 6f 20 70  e boundary. To p
23ef9 72 65 76 65 6e 74 0d 0a 20 20 20 20 2a 2a 20 74  revent..    ** t
23efa 68 65 73 65 20 61 73 73 65 72 74 28 29 73 20 66  hese assert()s f
23efb 72 6f 6d 20 66 61 69 6c 69 6e 67 2c 20 77 68 65  rom failing, whe
23efc 6e 20 62 75 69 6c 64 69 6e 67 20 77 69 74 68 20  n building with 
23efd 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 66  SQLITE_DEBUG def
23efe 69 6e 65 64 0d 0a 20 20 20 20 2a 2a 20 75 73 69  ined..    ** usi
23eff 6e 67 20 67 63 63 2c 20 77 65 20 66 6f 72 63 65  ng gcc, we force
23f00 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 38   nullMem to be 8
23f01 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 75 73  -byte aligned us
23f02 69 6e 67 20 74 68 65 20 6d 61 67 69 63 61 6c 0d  ing the magical.
23f03 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 69 62  .    ** __attrib
23f04 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38  ute__((aligned(8
23f05 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f 0d 0a  ))) macro.  */..
23f06 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
23f07 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0d 0a 23   Mem nullMem ..#
23f08 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
23f09 45 5f 44 45 42 55 47 29 20 26 26 20 64 65 66 69  E_DEBUG) && defi
23f0a 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0d 0a 20  ned(__GNUC__).. 
23f0b 20 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65       __attribute
23f0c 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29  __((aligned(8)))
23f0d 20 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20   ..#endif..     
23f0e 20 3d 20 7b 30 2c 20 22 22 2c 20 28 64 6f 75 62   = {0, "", (doub
23f0f 6c 65 29 30 2c 20 7b 30 7d 2c 20 30 2c 20 4d 45  le)0, {0}, 0, ME
23f10 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e  M_Null, SQLITE_N
23f11 55 4c 4c 2c 20 30 2c 0d 0a 23 69 66 64 65 66 20  ULL, 0,..#ifdef 
23f12 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20  SQLITE_DEBUG..  
23f13 20 20 20 20 20 20 20 30 2c 20 30 2c 20 20 2f 2a         0, 0,  /*
23f14 20 70 53 63 6f 70 79 46 72 6f 6d 2c 20 70 46 69   pScopyFrom, pFi
23f15 6c 6c 65 72 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  ller */..#endif.
23f16 0a 20 20 20 20 20 20 20 20 20 30 2c 20 30 20 7d  .         0, 0 }
23f17 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 56 6d  ;....    if( pVm
23f18 20 26 26 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e   && ALWAYS(pVm->
23f19 64 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  db) ){..      sq
23f1a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
23f1b 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  r(pVm->db->mutex
23f1c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
23f1d 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20  3Error(pVm->db, 
23f1e 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29  SQLITE_RANGE, 0)
23f1f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4f  ;..    }..    pO
23f20 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c  ut = (Mem*)&null
23f21 4d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  Mem;..  }..  ret
23f22 75 72 6e 20 70 4f 75 74 3b 0d 0a 7d 0d 0a 0d 0a  urn pOut;..}....
23f23 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
23f24 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61  tion is called a
23f25 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e  fter invoking an
23f26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58   sqlite3_value_X
23f27 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61  XX function on a
23f28 20 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c   ..** column val
23f29 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65  ue (i.e. a value
23f2a 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 76 61   returned by eva
23f2b 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65  luating an SQL e
23f2c 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
23f2d 0d 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 74  ..** select list
23f2e 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
23f2f 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 79  tement) that may
23f30 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28   cause a malloc(
23f31 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0d 0a  ) failure. If ..
23f32 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  ** malloc() has 
23f33 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65  failed, the thre
23f34 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ads mallocFailed
23f35 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64   flag is cleared
23f36 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0d   and the result.
23f37 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74  .** code of stat
23f38 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20  ement pStmt set 
23f39 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  to SQLITE_NOMEM.
23f3a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 70 65 63 69 66 69  ..**..** Specifi
23f3b 63 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63  cally, this is c
23f3c 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
23f3d 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 73  n:..**..**     s
23f3e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
23f3f 74 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69  t()..**     sqli
23f40 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
23f41 28 29 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  ()..**     sqlit
23f42 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
23f43 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ..**     sqlite3
23f44 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
23f45 0d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ..**     sqlite3
23f46 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0d 0a  _column_real()..
23f47 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
23f48 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0d 0a 2a  olumn_bytes()..*
23f49 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
23f4a 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0d 0a  lumn_bytes16()..
23f4b 2a 2a 20 20 20 20 20 73 71 69 69 74 65 33 5f 63  **     sqiite3_c
23f4c 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 0d 0a 2a 2f  olumn_blob()..*/
23f4d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  ..static void co
23f4e 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
23f4f 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
23f50 70 53 74 6d 74 29 0d 0a 7b 0d 0a 20 20 2f 2a 20  pStmt)..{..  /* 
23f51 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  If malloc() fail
23f52 65 64 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63  ed during an enc
23f53 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e  oding conversion
23f54 20 77 69 74 68 69 6e 20 61 6e 0d 0a 20 20 2a 2a   within an..  **
23f55 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
23f56 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 73 65  XXX API, then se
23f57 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  t the return cod
23f58 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  e of the stateme
23f59 6e 74 20 74 6f 0d 0a 20 20 2a 2a 20 53 51 4c 49  nt to..  ** SQLI
23f5a 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65  TE_NOMEM. The ne
23f5b 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70  xt call to _step
23f5c 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c 6c  () (if any) will
23f5d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
23f5e 52 52 4f 52 0d 0a 20 20 2a 2a 20 61 6e 64 20 5f  RROR..  ** and _
23f5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20  finalize() will 
23f60 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0d 0a 20  return NOMEM... 
23f61 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 70 20 3d   */..  Vdbe *p =
23f62 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0d   (Vdbe *)pStmt;.
23f63 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20  .  if( p ){..   
23f64 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 33   p->rc = sqlite3
23f65 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70  ApiExit(p->db, p
23f66 2d 3e 72 63 29 3b 0d 0a 20 20 20 20 73 71 6c 69  ->rc);..    sqli
23f67 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
23f68 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a  p->db->mutex);..
23f69 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a    }..}..../*****
23f6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f6b 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f  ******* sqlite3_
23f6c 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a  column_  *******
23f6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f6e 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65  ********..** The
23f6f 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
23f70 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
23f71 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74 73 20  access elements 
23f72 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  of the current r
23f73 6f 77 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  ow..** in the re
23f74 73 75 6c 74 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 53  sult set...*/..S
23f75 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
23f76 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
23f77 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65  lumn_blob(sqlite
23f78 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
23f79 6e 74 20 69 29 7b 0d 0a 20 20 63 6f 6e 73 74 20  nt i){..  const 
23f7a 76 6f 69 64 20 2a 76 61 6c 3b 0d 0a 20 20 76 61  void *val;..  va
23f7b 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
23f7c 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65  e_blob( columnMe
23f7d 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0d 0a 20  m(pStmt,i) );.. 
23f7e 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
23f7f 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f  there is no enco
23f80 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c  ding conversion,
23f81 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69   value_blob() mi
23f82 67 68 74 0d 0a 20 20 2a 2a 20 6e 65 65 64 20 74  ght..  ** need t
23f83 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20  o call malloc() 
23f84 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65  to expand the re
23f85 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c  sult of a zerobl
23f86 6f 62 28 29 20 0d 0a 20 20 2a 2a 20 65 78 70 72  ob() ..  ** expr
23f87 65 73 73 69 6f 6e 2e 20 0d 0a 20 20 2a 2f 0d 0a  ession. ..  */..
23f88 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
23f89 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0d 0a 20  ilure(pStmt);.. 
23f8a 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 7d 0d   return val;..}.
23f8b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23f8c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
23f8d 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ytes(sqlite3_stm
23f8e 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
23f8f 7b 0d 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73  {..  int val = s
23f90 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
23f91 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  es( columnMem(pS
23f92 74 6d 74 2c 69 29 20 29 3b 0d 0a 20 20 63 6f 6c  tmt,i) );..  col
23f93 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
23f94 28 70 53 74 6d 74 29 3b 0d 0a 20 20 72 65 74 75  (pStmt);..  retu
23f95 72 6e 20 76 61 6c 3b 0d 0a 7d 0d 0a 53 51 4c 49  rn val;..}..SQLI
23f96 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
23f97 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
23f98 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
23f99 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d 0a  pStmt, int i){..
23f9a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69    int val = sqli
23f9b 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
23f9c 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  6( columnMem(pSt
23f9d 6d 74 2c 69 29 20 29 3b 0d 0a 20 20 63 6f 6c 75  mt,i) );..  colu
23f9e 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
23f9f 70 53 74 6d 74 29 3b 0d 0a 20 20 72 65 74 75 72  pStmt);..  retur
23fa0 6e 20 76 61 6c 3b 0d 0a 7d 0d 0a 53 51 4c 49 54  n val;..}..SQLIT
23fa1 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c  E_API double sql
23fa2 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
23fa3 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  le(sqlite3_stmt 
23fa4 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d  *pStmt, int i){.
23fa5 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20  .  double val = 
23fa6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
23fa7 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  uble( columnMem(
23fa8 70 53 74 6d 74 2c 69 29 20 29 3b 0d 0a 20 20 63  pStmt,i) );..  c
23fa9 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
23faa 72 65 28 70 53 74 6d 74 29 3b 0d 0a 20 20 72 65  re(pStmt);..  re
23fab 74 75 72 6e 20 76 61 6c 3b 0d 0a 7d 0d 0a 53 51  turn val;..}..SQ
23fac 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
23fad 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  ite3_column_int(
23fae 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
23faf 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d 0a 20 20  tmt, int i){..  
23fb0 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
23fb1 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c  3_value_int( col
23fb2 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
23fb3 29 3b 0d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c  );..  columnMall
23fb4 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
23fb5 3b 0d 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  ;..  return val;
23fb6 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..}..SQLITE_API 
23fb7 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
23fb8 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
23fb9 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  4(sqlite3_stmt *
23fba 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d 0a  pStmt, int i){..
23fbb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76    sqlite_int64 v
23fbc 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
23fbd 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e  ue_int64( column
23fbe 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0d  Mem(pStmt,i) );.
23fbf 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
23fc0 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0d 0a  ailure(pStmt);..
23fc1 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 7d    return val;..}
23fc2 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
23fc3 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
23fc4 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
23fc5 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74  _text(sqlite3_st
23fc6 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
23fc7 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  ){..  const unsi
23fc8 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d  gned char *val =
23fc9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
23fca 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  ext( columnMem(p
23fcb 53 74 6d 74 2c 69 29 20 29 3b 0d 0a 20 20 63 6f  Stmt,i) );..  co
23fcc 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
23fcd 65 28 70 53 74 6d 74 29 3b 0d 0a 20 20 72 65 74  e(pStmt);..  ret
23fce 75 72 6e 20 76 61 6c 3b 0d 0a 7d 0d 0a 53 51 4c  urn val;..}..SQL
23fcf 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
23fd0 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63  value *sqlite3_c
23fd1 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69  olumn_value(sqli
23fd2 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
23fd3 20 69 6e 74 20 69 29 7b 0d 0a 20 20 4d 65 6d 20   int i){..  Mem 
23fd4 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65  *pOut = columnMe
23fd5 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0d 0a 20 20  m(pStmt, i);..  
23fd6 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26  if( pOut->flags&
23fd7 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0d 0a 20  MEM_Static ){.. 
23fd8 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26     pOut->flags &
23fd9 3d 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0d 0a  = ~MEM_Static;..
23fda 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
23fdb 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0d 0a 20  |= MEM_Ephem;.. 
23fdc 20 7d 0d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   }..  columnMall
23fdd 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
23fde 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c  ;..  return (sql
23fdf 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 70 4f 75  ite3_value *)pOu
23fe0 74 3b 0d 0a 7d 0d 0a 23 69 66 6e 64 65 66 20 53  t;..}..#ifndef S
23fe1 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
23fe2 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ..SQLITE_API con
23fe3 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
23fe4 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73  _column_text16(s
23fe5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
23fe6 6d 74 2c 20 69 6e 74 20 69 29 7b 0d 0a 20 20 63  mt, int i){..  c
23fe7 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d  onst void *val =
23fe8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
23fe9 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ext16( columnMem
23fea 28 70 53 74 6d 74 2c 69 29 20 29 3b 0d 0a 20 20  (pStmt,i) );..  
23feb 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
23fec 75 72 65 28 70 53 74 6d 74 29 3b 0d 0a 20 20 72  ure(pStmt);..  r
23fed 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 7d 0d 0a 23  eturn val;..}..#
23fee 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
23fef 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 53  OMIT_UTF16 */..S
23ff0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
23ff1 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
23ff2 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
23ff3 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d 0a  pStmt, int i){..
23ff4 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71    int iType = sq
23ff5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
23ff6 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
23ff7 74 2c 69 29 20 29 3b 0d 0a 20 20 63 6f 6c 75 6d  t,i) );..  colum
23ff8 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
23ff9 53 74 6d 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e  Stmt);..  return
23ffa 20 69 54 79 70 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   iType;..}..../*
23ffb 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
23ffc 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65 72  unction is exper
23ffd 69 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a  imental and subj
23ffe 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72  ect to change or
23fff 0d 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0d  ..** removal */.
24000 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  ./*int sqlite3_c
24001 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79  olumn_numeric_ty
24002 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  pe(sqlite3_stmt 
24003 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0d  *pStmt, int i){.
24004 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .**  return sqli
24005 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
24006 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65  c_type( columnMe
24007 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0d 0a 2a  m(pStmt,i) );..*
24008 2a 7d 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  *}..*/..../*..**
24009 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74   Convert the N-t
2400a 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74  h element of pSt
2400b 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69  mt->pColName[] i
2400c 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69  nto a string usi
2400d 6e 67 0d 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74  ng..** xFunc() t
2400e 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 20  hen return that 
2400f 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 73  string.  If N is
24010 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72   out of range, r
24011 65 74 75 72 6e 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a  eturn 0...**..**
24012 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f   There are up to
24013 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63   5 names for eac
24014 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79  h column.  useTy
24015 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  pe determines wh
24016 69 63 68 0d 0a 2a 2a 20 6e 61 6d 65 20 69 73 20  ich..** name is 
24017 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20  returned.  Here 
24018 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0d 0a  are the names:..
24019 2a 2a 0d 0a 2a 2a 20 20 20 20 30 20 20 20 20 20  **..**    0     
2401a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   The column name
2401b 20 61 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65   as it should be
2401c 20 64 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f   displayed for o
2401d 75 74 70 75 74 0d 0a 2a 2a 20 20 20 20 31 20 20  utput..**    1  
2401e 20 20 20 20 54 68 65 20 64 61 74 61 74 79 70 65      The datatype
2401f 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f   name for the co
24020 6c 75 6d 6e 0d 0a 2a 2a 20 20 20 20 32 20 20 20  lumn..**    2   
24021 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74     The name of t
24022 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
24023 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69   the column deri
24024 76 65 73 20 66 72 6f 6d 0d 0a 2a 2a 20 20 20 20  ves from..**    
24025 33 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20  3      The name 
24026 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  of the table tha
24027 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72  t the column der
24028 69 76 65 73 20 66 72 6f 6d 0d 0a 2a 2a 20 20 20  ives from..**   
24029 20 34 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65   4      The name
2402a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
2402b 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 72 65  lumn that the re
2402c 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69  sult column deri
2402d 76 65 73 20 66 72 6f 6d 0d 0a 2a 2a 0d 0a 2a 2a  ves from..**..**
2402e 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
2402f 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 63  s not a simple c
24030 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
24031 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 70  (if it is an exp
24032 72 65 73 73 69 6f 6e 0d 0a 2a 2a 20 6f 72 20 61  ression..** or a
24033 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20   constant) then 
24034 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61  useTypes 2, 3, a
24035 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c  nd 4 return NULL
24036 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f  ...*/..static co
24037 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e  nst void *column
24038 4e 61 6d 65 28 0d 0a 20 20 73 71 6c 69 74 65 33  Name(..  sqlite3
24039 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0d 0a 20  _stmt *pStmt,.. 
2403a 20 69 6e 74 20 4e 2c 0d 0a 20 20 63 6f 6e 73 74   int N,..  const
2403b 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28   void *(*xFunc)(
2403c 4d 65 6d 2a 29 2c 0d 0a 20 20 69 6e 74 20 75 73  Mem*),..  int us
2403d 65 54 79 70 65 0d 0a 29 7b 0d 0a 20 20 63 6f 6e  eType..){..  con
2403e 73 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30  st void *ret = 0
2403f 3b 0d 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ;..  Vdbe *p = (
24040 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0d 0a 20  Vdbe *)pStmt;.. 
24041 20 69 6e 74 20 6e 3b 0d 0a 20 20 73 71 6c 69 74   int n;..  sqlit
24042 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0d  e3 *db = p->db;.
24043 0a 20 20 0d 0a 20 20 61 73 73 65 72 74 28 20 64  .  ..  assert( d
24044 62 21 3d 30 20 29 3b 0d 0a 20 20 6e 20 3d 20 73  b!=0 );..  n = s
24045 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
24046 75 6e 74 28 70 53 74 6d 74 29 3b 0d 0a 20 20 69  unt(pStmt);..  i
24047 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29  f( N<n && N>=0 )
24048 7b 0d 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 54  {..    N += useT
24049 79 70 65 2a 6e 3b 0d 0a 20 20 20 20 73 71 6c 69  ype*n;..    sqli
2404a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2404b 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 20  db->mutex);..   
2404c 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
2404d 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0d  locFailed==0 );.
2404e 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63  .    ret = xFunc
2404f 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d  (&p->aColName[N]
24050 29 3b 0d 0a 20 20 20 20 20 2f 2a 20 41 20 6d 61  );..     /* A ma
24051 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 61  lloc may have fa
24052 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74  iled inside of t
24053 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e  he xFunc() call.
24054 20 49 66 20 74 68 69 73 0d 0a 20 20 20 20 2a 2a   If this..    **
24055 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c   is the case, cl
24056 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  ear the mallocFa
24057 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65  iled flag and re
24058 74 75 72 6e 20 4e 55 4c 4c 2e 0d 0a 20 20 20 20  turn NULL...    
24059 2a 2f 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  */..    if( db->
2405a 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
2405b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
2405c 63 46 61 69 6c 65 64 20 3d 20 30 3b 0d 0a 20 20  cFailed = 0;..  
2405d 20 20 20 20 72 65 74 20 3d 20 30 3b 0d 0a 20 20      ret = 0;..  
2405e 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
2405f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
24060 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20  >mutex);..  }.. 
24061 20 72 65 74 75 72 6e 20 72 65 74 3b 0d 0a 7d 0d   return ret;..}.
24062 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
24063 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
24064 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
24065 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65  he result set re
24066 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0d 0a 2a  turned by SQL..*
24067 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  * statement pStm
24068 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  t...*/..SQLITE_A
24069 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
2406a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2406b 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  me(sqlite3_stmt 
2406c 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0d  *pStmt, int N){.
2406d 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
2406e 4e 61 6d 65 28 0d 0a 20 20 20 20 20 20 70 53 74  Name(..      pSt
2406f 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
24070 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
24071 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
24072 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0d   COLNAME_NAME);.
24073 0a 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
24074 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d 0a 53  TE_OMIT_UTF16..S
24075 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
24076 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
24077 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69  lumn_name16(sqli
24078 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
24079 20 69 6e 74 20 4e 29 7b 0d 0a 20 20 72 65 74 75   int N){..  retu
2407a 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d 0a  rn columnName(..
2407b 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
2407c 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
2407d 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
2407e 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
2407f 41 4d 45 5f 4e 41 4d 45 29 3b 0d 0a 7d 0d 0a 23  AME_NAME);..}..#
24080 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
24081 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20  Constraint:  If 
24082 79 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f  you have ENABLE_
24083 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20  COLUMN_METADATA 
24084 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 0d 0a 2a  then you must..*
24085 2a 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49  * not define OMI
24086 54 5f 44 45 43 4c 54 59 50 45 2e 0d 0a 2a 2f 0d  T_DECLTYPE...*/.
24087 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
24088 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
24089 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
2408a 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
2408b 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0d 0a 23 20  MN_METADATA)..# 
2408c 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74 20  error "Must not 
2408d 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c 49  define both SQLI
2408e 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
2408f 20 5c 0d 0a 20 20 20 20 20 20 20 20 20 61 6e 64   \..         and
24090 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
24091 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0d  OLUMN_METADATA".
24092 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64  .#endif....#ifnd
24093 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
24094 45 43 4c 54 59 50 45 0d 0a 2f 2a 0d 0a 2a 2a 20  ECLTYPE../*..** 
24095 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d  Return the colum
24096 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  n declaration ty
24097 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c  pe (if applicabl
24098 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 68 20  e) of the 'i'th 
24099 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 6f 66 20 74 68  column..** of th
2409a 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
2409b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 53  SQL statement pS
2409c 74 6d 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  tmt...*/..SQLITE
2409d 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
2409e 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
2409f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33  decltype(sqlite3
240a0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
240a1 74 20 4e 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20  t N){..  return 
240a2 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d 0a 20 20 20  columnName(..   
240a3 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
240a4 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
240a5 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
240a6 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44  _text, COLNAME_D
240a7 45 43 4c 54 59 50 45 29 3b 0d 0a 7d 0d 0a 23 69  ECLTYPE);..}..#i
240a8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
240a9 54 5f 55 54 46 31 36 0d 0a 53 51 4c 49 54 45 5f  T_UTF16..SQLITE_
240aa 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
240ab 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
240ac 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65  ecltype16(sqlite
240ad 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
240ae 6e 74 20 4e 29 7b 0d 0a 20 20 72 65 74 75 72 6e  nt N){..  return
240af 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d 0a 20 20   columnName(..  
240b0 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
240b1 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
240b2 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
240b3 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
240b4 45 5f 44 45 43 4c 54 59 50 45 29 3b 0d 0a 7d 0d  E_DECLTYPE);..}.
240b5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
240b6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d  E_OMIT_UTF16 */.
240b7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
240b8 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20  E_OMIT_DECLTYPE 
240b9 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  */....#ifdef SQL
240ba 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
240bb 4e 5f 4d 45 54 41 44 41 54 41 0d 0a 2f 2a 0d 0a  N_METADATA../*..
240bc 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61  ** Return the na
240bd 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
240be 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20  se from which a 
240bf 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65  result column de
240c0 72 69 76 65 73 2e 0d 0a 2a 2a 20 4e 55 4c 4c 20  rives...** NULL 
240c1 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
240c2 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
240c3 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
240c4 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
240c5 0d 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c  ..** anything el
240c6 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  se which is not 
240c7 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65  an unabiguous re
240c8 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74  ference to a dat
240c9 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0d 0a 2a  abase column...*
240ca 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  /..SQLITE_API co
240cb 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
240cc 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
240cd 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
240ce 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
240cf 4e 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 63 6f  N){..  return co
240d0 6c 75 6d 6e 4e 61 6d 65 28 0d 0a 20 20 20 20 20  lumnName(..     
240d1 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
240d2 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
240d3 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
240d4 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  ext, COLNAME_DAT
240d5 41 42 41 53 45 29 3b 0d 0a 7d 0d 0a 23 69 66 6e  ABASE);..}..#ifn
240d6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
240d7 55 54 46 31 36 0d 0a 53 51 4c 49 54 45 5f 41 50  UTF16..SQLITE_AP
240d8 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
240d9 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
240da 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c  abase_name16(sql
240db 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
240dc 2c 20 69 6e 74 20 4e 29 7b 0d 0a 20 20 72 65 74  , int N){..  ret
240dd 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d  urn columnName(.
240de 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
240df 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
240e0 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
240e1 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
240e2 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0d  NAME_DATABASE);.
240e3 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
240e4 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
240e5 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  */..../*..** Ret
240e6 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
240e7 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  the table from w
240e8 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f  hich a result co
240e9 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0d 0a 2a  lumn derives...*
240ea 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
240eb 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  ed if the result
240ec 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78   column is an ex
240ed 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73  pression or cons
240ee 74 61 6e 74 20 6f 72 0d 0a 2a 2a 20 61 6e 79 74  tant or..** anyt
240ef 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20  hing else which 
240f0 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67  is not an unabig
240f1 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74  uous reference t
240f2 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c  o a database col
240f3 75 6d 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  umn...*/..SQLITE
240f4 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
240f5 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
240f6 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74  table_name(sqlit
240f7 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
240f8 69 6e 74 20 4e 29 7b 0d 0a 20 20 72 65 74 75 72  int N){..  retur
240f9 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d 0a 20  n columnName(.. 
240fa 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
240fb 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
240fc 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
240fd 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
240fe 5f 54 41 42 4c 45 29 3b 0d 0a 7d 0d 0a 23 69 66  _TABLE);..}..#if
240ff 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24100 5f 55 54 46 31 36 0d 0a 53 51 4c 49 54 45 5f 41  _UTF16..SQLITE_A
24101 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
24102 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
24103 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  ble_name16(sqlit
24104 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
24105 69 6e 74 20 4e 29 7b 0d 0a 20 20 72 65 74 75 72  int N){..  retur
24106 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0d 0a 20  n columnName(.. 
24107 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
24108 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
24109 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
2410a 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
2410b 4d 45 5f 54 41 42 4c 45 29 3b 0d 0a 7d 0d 0a 23  ME_TABLE);..}..#
2410c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2410d 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 0d  OMIT_UTF16 */...
2410e 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./*..** Return t
2410f 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
24110 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d  able column from
24111 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20   which a result 
24112 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0d  column derives..
24113 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
24114 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
24115 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
24116 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
24117 6e 73 74 61 6e 74 20 6f 72 0d 0a 2a 2a 20 61 6e  nstant or..** an
24118 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63  ything else whic
24119 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62  h is not an unab
2411a 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65  iguous reference
2411b 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63   to a database c
2411c 6f 6c 75 6d 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  olumn...*/..SQLI
2411d 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
2411e 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
2411f 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71  n_origin_name(sq
24120 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
24121 74 2c 20 69 6e 74 20 4e 29 7b 0d 0a 20 20 72 65  t, int N){..  re
24122 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
24123 0d 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  ..      pStmt, N
24124 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
24125 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
24126 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
24127 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0d 0a 7d 0d  AME_COLUMN);..}.
24128 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24129 4f 4d 49 54 5f 55 54 46 31 36 0d 0a 53 51 4c 49  OMIT_UTF16..SQLI
2412a 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
2412b 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
2412c 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28  n_origin_name16(
2412d 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2412e 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0d 0a 20 20  tmt, int N){..  
2412f 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
24130 65 28 0d 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  e(..      pStmt,
24131 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
24132 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
24133 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
24134 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b  COLNAME_COLUMN);
24135 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
24136 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
24137 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53   */..#endif /* S
24138 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
24139 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0d  UMN_METADATA */.
2413a 0a 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...../**********
2413b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2413c 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69  ***** sqlite3_bi
2413d 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nd_  ***********
2413e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2413f 0d 0a 2a 2a 20 0d 0a 2a 2a 20 52 6f 75 74 69 6e  ..** ..** Routin
24140 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 61 63  es used to attac
24141 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64  h values to wild
24142 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69  cards in a compi
24143 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  led SQL statemen
24144 74 2e 0d 0a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 55  t...*/../*..** U
24145 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20  nbind the value 
24146 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c  bound to variabl
24147 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d  e i in virtual m
24148 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69  achine p. This i
24149 73 20 74 68 65 20 0d 0a 2a 2a 20 74 68 65 20 73  s the ..** the s
2414a 61 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20 61  ame as binding a
2414b 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74   NULL value to t
2414c 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68  he column. If th
2414d 65 20 22 69 22 20 70 61 72 61 6d 65 74 65 72 20  e "i" parameter 
2414e 69 73 0d 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61  is..** out of ra
2414f 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  nge, then SQLITE
24150 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e  _RANGE is return
24151 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c  ed. Othewise SQL
24152 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ITE_OK...**..** 
24153 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 61  A successful eva
24154 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  luation of this 
24155 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73  routine acquires
24156 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e   the mutex on p.
24157 0d 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69  ..** the mutex i
24158 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e  s released if an
24159 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
2415a 6f 63 63 75 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  occurs...**..** 
2415b 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73  The error code s
2415c 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73  tored in databas
2415d 65 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77  e p->db is overw
2415e 72 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20  ritten with the 
2415f 72 65 74 75 72 6e 0d 0a 2a 2a 20 76 61 6c 75 65  return..** value
24160 20 69 6e 20 61 6e 79 20 63 61 73 65 2e 0d 0a 2a   in any case...*
24161 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  /..static int vd
24162 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a 70  beUnbind(Vdbe *p
24163 2c 20 69 6e 74 20 69 29 7b 0d 0a 20 20 4d 65 6d  , int i){..  Mem
24164 20 2a 70 56 61 72 3b 0d 0a 20 20 69 66 28 20 76   *pVar;..  if( v
24165 64 62 65 53 61 66 65 74 79 4e 6f 74 4e 75 6c 6c  dbeSafetyNotNull
24166 28 70 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  (p) ){..    retu
24167 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
24168 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 73  _BKPT;..  }..  s
24169 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2416a 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
2416b 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  ;..  if( p->magi
2416c 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c!=VDBE_MAGIC_RU
2416d 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b  N || p->pc>=0 ){
2416e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
2416f 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45  or(p->db, SQLITE
24170 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0d 0a 20 20  _MISUSE, 0);..  
24171 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
24172 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
24173 65 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ex);..    sqlite
24174 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4d 49 53  3_log(SQLITE_MIS
24175 55 53 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 22  USE, ..        "
24176 62 69 6e 64 20 6f 6e 20 61 20 62 75 73 79 20 70  bind on a busy p
24177 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
24178 74 3a 20 5b 25 73 5d 22 2c 20 70 2d 3e 7a 53 71  t: [%s]", p->zSq
24179 6c 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  l);..    return 
2417a 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
2417b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  PT;..  }..  if( 
2417c 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72  i<1 || i>p->nVar
2417d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2417e 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c  Error(p->db, SQL
2417f 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0d 0a  ITE_RANGE, 0);..
24180 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
24181 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
24182 75 74 65 78 29 3b 0d 0a 20 20 20 20 72 65 74 75  utex);..    retu
24183 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 3b  rn SQLITE_RANGE;
24184 0d 0a 20 20 7d 0d 0a 20 20 69 2d 2d 3b 0d 0a 20  ..  }..  i--;.. 
24185 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72   pVar = &p->aVar
24186 5b 69 5d 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  [i];..  sqlite3V
24187 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 56  dbeMemRelease(pV
24188 61 72 29 3b 0d 0a 20 20 70 56 61 72 2d 3e 66 6c  ar);..  pVar->fl
24189 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d  ags = MEM_Null;.
2418a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
2418b 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  p->db, SQLITE_OK
2418c 2c 20 30 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  , 0);....  /* If
2418d 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 73 70   the bit corresp
2418e 6f 6e 64 69 6e 67 20 74 6f 20 74 68 69 73 20 76  onding to this v
2418f 61 72 69 61 62 6c 65 20 69 6e 20 56 64 62 65 2e  ariable in Vdbe.
24190 65 78 70 6d 61 73 6b 20 69 73 20 73 65 74 2c 20  expmask is set, 
24191 74 68 65 6e 20 0d 0a 20 20 2a 2a 20 62 69 6e 64  then ..  ** bind
24192 69 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65 20  ing a new value 
24193 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  to this variable
24194 20 69 6e 76 61 6c 69 64 61 74 65 73 20 74 68 65   invalidates the
24195 20 63 75 72 72 65 6e 74 20 71 75 65 72 79 20 70   current query p
24196 6c 61 6e 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  lan...  **..  **
24197 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d   IMPLEMENTATION-
24198 4f 46 3a 20 52 2d 34 38 34 34 30 2d 33 37 35 39  OF: R-48440-3759
24199 35 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69  5 If the specifi
2419a 63 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f  c value bound to
2419b 20 68 6f 73 74 0d 0a 20 20 2a 2a 20 70 61 72 61   host..  ** para
2419c 6d 65 74 65 72 20 69 6e 20 74 68 65 20 57 48 45  meter in the WHE
2419d 52 45 20 63 6c 61 75 73 65 20 6d 69 67 68 74 20  RE clause might 
2419e 69 6e 66 6c 75 65 6e 63 65 20 74 68 65 20 63 68  influence the ch
2419f 6f 69 63 65 20 6f 66 20 71 75 65 72 79 20 70 6c  oice of query pl
241a0 61 6e 0d 0a 20 20 2a 2a 20 66 6f 72 20 61 20 73  an..  ** for a s
241a1 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74  tatement, then t
241a2 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c  he statement wil
241a3 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
241a4 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 0d 0a  ly recompiled,..
241a5 20 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65    ** as if there
241a6 20 68 61 64 20 62 65 65 6e 20 61 20 73 63 68 65   had been a sche
241a7 6d 61 20 63 68 61 6e 67 65 2c 20 6f 6e 20 74 68  ma change, on th
241a8 65 20 66 69 72 73 74 20 73 71 6c 69 74 65 33 5f  e first sqlite3_
241a9 73 74 65 70 28 29 20 63 61 6c 6c 0d 0a 20 20 2a  step() call..  *
241aa 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20  * following any 
241ab 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 62 69  change to the bi
241ac 6e 64 69 6e 67 73 20 6f 66 20 74 68 61 74 20 70  ndings of that p
241ad 61 72 61 6d 65 74 65 72 2e 0d 0a 20 20 2a 2f 0d  arameter...  */.
241ae 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70  .  if( p->isPrep
241af 61 72 65 56 32 20 26 26 0d 0a 20 20 20 20 20 28  areV2 &&..     (
241b0 28 69 3c 33 32 20 26 26 20 70 2d 3e 65 78 70 6d  (i<32 && p->expm
241b1 61 73 6b 20 26 20 28 28 75 33 32 29 31 20 3c 3c  ask & ((u32)1 <<
241b2 20 69 29 29 20 7c 7c 20 70 2d 3e 65 78 70 6d 61   i)) || p->expma
241b3 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 29 0d  sk==0xffffffff).
241b4 0a 20 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 65 78  .  ){..    p->ex
241b5 70 69 72 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d  pired = 1;..  }.
241b6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
241b7 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
241b8 2a 20 42 69 6e 64 20 61 20 74 65 78 74 20 6f 72  * Bind a text or
241b9 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0d 0a 2a 2f   BLOB value...*/
241ba 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e  ..static int bin
241bb 64 54 65 78 74 28 0d 0a 20 20 73 71 6c 69 74 65  dText(..  sqlite
241bc 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20  3_stmt *pStmt,  
241bd 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
241be 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73  t to bind agains
241bf 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 2c 20 20  t */..  int i,  
241c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
241c1 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 70  * Index of the p
241c2 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e 64  arameter to bind
241c3 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69   */..  const voi
241c4 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a  d *zData,     /*
241c5 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
241c6 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64  data to be bound
241c7 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 44 61 74 61   */..  int nData
241c8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
241c9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
241ca 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62   of data to be b
241cb 6f 75 6e 64 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  ound */..  void 
241cc 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20  (*xDel)(void*), 
241cd 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
241ce 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0d  for the data */.
241cf 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20  .  u8 encoding  
241d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
241d1 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61  oding for the da
241d2 74 61 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62  ta */..){..  Vdb
241d3 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
241d4 53 74 6d 74 3b 0d 0a 20 20 4d 65 6d 20 2a 70 56  Stmt;..  Mem *pV
241d5 61 72 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  ar;..  int rc;..
241d6 0d 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62  ..  rc = vdbeUnb
241d7 69 6e 64 28 70 2c 20 69 29 3b 0d 0a 20 20 69 66  ind(p, i);..  if
241d8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
241d9 29 7b 0d 0a 20 20 20 20 69 66 28 20 7a 44 61 74  ){..    if( zDat
241da 61 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70  a!=0 ){..      p
241db 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
241dc 2d 31 5d 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  -1];..      rc =
241dd 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
241de 65 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61 74  etStr(pVar, zDat
241df 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64 69  a, nData, encodi
241e0 6e 67 2c 20 78 44 65 6c 29 3b 0d 0a 20 20 20 20  ng, xDel);..    
241e1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
241e2 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21  _OK && encoding!
241e3 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  =0 ){..        r
241e4 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  c = sqlite3VdbeC
241e5 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56  hangeEncoding(pV
241e6 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b  ar, ENC(p->db));
241e7 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
241e8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
241e9 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0d 0a 20 20  >db, rc, 0);..  
241ea 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
241eb 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72  ApiExit(p->db, r
241ec 63 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  c);..    }..    
241ed 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
241ee 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
241ef 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  );..  }else if( 
241f0 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 53 54 41  xDel!=SQLITE_STA
241f1 54 49 43 20 26 26 20 78 44 65 6c 21 3d 53 51 4c  TIC && xDel!=SQL
241f2 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 29 7b  ITE_TRANSIENT ){
241f3 0d 0a 20 20 20 20 78 44 65 6c 28 28 76 6f 69 64  ..    xDel((void
241f4 2a 29 7a 44 61 74 61 29 3b 0d 0a 20 20 7d 0d 0a  *)zData);..  }..
241f5 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
241f6 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 42 69 6e 64  ...../*..** Bind
241f7 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f   a blob value to
241f8 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
241f9 74 20 76 61 72 69 61 62 6c 65 2e 0d 0a 2a 2f 0d  t variable...*/.
241fa 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
241fb 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
241fc 62 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  b(..  sqlite3_st
241fd 6d 74 20 2a 70 53 74 6d 74 2c 20 0d 0a 20 20 69  mt *pStmt, ..  i
241fe 6e 74 20 69 2c 20 0d 0a 20 20 63 6f 6e 73 74 20  nt i, ..  const 
241ff 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0d 0a 20  void *zData, .. 
24200 20 69 6e 74 20 6e 44 61 74 61 2c 20 0d 0a 20 20   int nData, ..  
24201 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
24202 64 2a 29 0d 0a 29 7b 0d 0a 20 20 72 65 74 75 72  d*)..){..  retur
24203 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74  n bindText(pStmt
24204 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  , i, zData, nDat
24205 61 2c 20 78 44 65 6c 2c 20 30 29 3b 0d 0a 7d 0d  a, xDel, 0);..}.
24206 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
24207 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75  sqlite3_bind_dou
24208 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ble(sqlite3_stmt
24209 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20   *pStmt, int i, 
2420a 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b 0d  double rValue){.
2420b 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 56 64  .  int rc;..  Vd
2420c 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
2420d 70 53 74 6d 74 3b 0d 0a 20 20 72 63 20 3d 20 76  pStmt;..  rc = v
2420e 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
2420f 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
24210 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71  TE_OK ){..    sq
24211 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
24212 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69  ouble(&p->aVar[i
24213 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0d 0a 20  -1], rValue);.. 
24214 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
24215 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
24216 74 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  tex);..  }..  re
24217 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 53 51 4c  turn rc;..}..SQL
24218 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
24219 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c  te3_bind_int(sql
2421a 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e  ite3_stmt *p, in
2421b 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29  t i, int iValue)
2421c 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  {..  return sqli
2421d 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
2421e 2c 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65  , i, (i64)iValue
2421f 29 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50  );..}..SQLITE_AP
24220 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
24221 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33  nd_int64(sqlite3
24222 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
24223 74 20 69 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  t i, sqlite_int6
24224 34 20 69 56 61 6c 75 65 29 7b 0d 0a 20 20 69 6e  4 iValue){..  in
24225 74 20 72 63 3b 0d 0a 20 20 56 64 62 65 20 2a 70  t rc;..  Vdbe *p
24226 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
24227 3b 0d 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e  ;..  rc = vdbeUn
24228 62 69 6e 64 28 70 2c 20 69 29 3b 0d 0a 20 20 69  bind(p, i);..  i
24229 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2422a 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2422b 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
2422c 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69  &p->aVar[i-1], i
2422d 56 61 6c 75 65 29 3b 0d 0a 20 20 20 20 73 71 6c  Value);..    sql
2422e 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2422f 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  (p->db->mutex);.
24230 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
24231 63 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50  c;..}..SQLITE_AP
24232 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
24233 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f  nd_null(sqlite3_
24234 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
24235 20 69 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d   i){..  int rc;.
24236 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
24237 62 65 2a 29 70 53 74 6d 74 3b 0d 0a 20 20 72 63  be*)pStmt;..  rc
24238 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
24239 20 69 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d   i);..  if( rc==
2423a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
2423b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2423c 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
2423d 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ex);..  }..  ret
2423e 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 53 51 4c 49  urn rc;..}..SQLI
2423f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
24240 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 20 0d 0a  e3_bind_text( ..
24241 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
24242 70 53 74 6d 74 2c 20 0d 0a 20 20 69 6e 74 20 69  pStmt, ..  int i
24243 2c 20 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  , ..  const char
24244 20 2a 7a 44 61 74 61 2c 20 0d 0a 20 20 69 6e 74   *zData, ..  int
24245 20 6e 44 61 74 61 2c 20 0d 0a 20 20 76 6f 69 64   nData, ..  void
24246 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0d   (*xDel)(void*).
24247 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 62 69  .){..  return bi
24248 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c  ndText(pStmt, i,
24249 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78   zData, nData, x
2424a 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Del, SQLITE_UTF8
2424b 29 3b 0d 0a 7d 0d 0a 23 69 66 6e 64 65 66 20 53  );..}..#ifndef S
2424c 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2424d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
2424e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
2424f 78 74 31 36 28 0d 0a 20 20 73 71 6c 69 74 65 33  xt16(..  sqlite3
24250 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0d 0a  _stmt *pStmt, ..
24251 20 20 69 6e 74 20 69 2c 20 0d 0a 20 20 63 6f 6e    int i, ..  con
24252 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20  st void *zData, 
24253 0d 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0d  ..  int nData, .
24254 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
24255 76 6f 69 64 2a 29 0d 0a 29 7b 0d 0a 20 20 72 65  void*)..){..  re
24256 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53  turn bindText(pS
24257 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e  tmt, i, zData, n
24258 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49  Data, xDel, SQLI
24259 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b  TE_UTF16NATIVE);
2425a 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
2425b 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2425c 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20   */..SQLITE_API 
2425d 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
2425e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
2425f 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
24260 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  i, const sqlite3
24261 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b  _value *pValue){
24262 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73  ..  int rc;..  s
24263 77 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74  witch( pValue->t
24264 79 70 65 20 29 7b 0d 0a 20 20 20 20 63 61 73 65  ype ){..    case
24265 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
24266 20 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73   {..      rc = s
24267 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
24268 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c  4(pStmt, i, pVal
24269 75 65 2d 3e 75 2e 69 29 3b 0d 0a 20 20 20 20 20  ue->u.i);..     
2426a 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2426b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2426c 46 4c 4f 41 54 3a 20 7b 0d 0a 20 20 20 20 20 20  FLOAT: {..      
2426d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
2426e 64 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20  d_double(pStmt, 
2426f 69 2c 20 70 56 61 6c 75 65 2d 3e 72 29 3b 0d 0a  i, pValue->r);..
24270 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
24271 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
24272 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0d 0a 20 20  LITE_BLOB: {..  
24273 20 20 20 20 69 66 28 20 70 56 61 6c 75 65 2d 3e      if( pValue->
24274 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
24275 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
24276 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  = sqlite3_bind_z
24277 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69  eroblob(pStmt, i
24278 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a 65 72  , pValue->u.nZer
24279 6f 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  o);..      }else
2427a 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
2427b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
2427c 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c  b(pStmt, i, pVal
2427d 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e  ue->z, pValue->n
2427e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  ,SQLITE_TRANSIEN
2427f 54 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  T);..      }..  
24280 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
24281 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  }..    case SQLI
24282 54 45 5f 54 45 58 54 3a 20 7b 0d 0a 20 20 20 20  TE_TEXT: {..    
24283 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 74 28    rc = bindText(
24284 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65  pStmt,i,  pValue
24285 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20  ->z, pValue->n, 
24286 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
24287 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
24288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24289 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0d 0a   pValue->enc);..
2428a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
2428b 20 20 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74    }..    default
2428c 3a 20 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  : {..      rc = 
2428d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
2428e 6c 28 70 53 74 6d 74 2c 20 69 29 3b 0d 0a 20 20  l(pStmt, i);..  
2428f 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
24290 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
24291 20 72 63 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f   rc;..}..SQLITE_
24292 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
24293 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71  bind_zeroblob(sq
24294 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
24295 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29  t, int i, int n)
24296 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  {..  int rc;..  
24297 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
24298 2a 29 70 53 74 6d 74 3b 0d 0a 20 20 72 63 20 3d  *)pStmt;..  rc =
24299 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
2429a 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
2429b 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
2429c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
2429d 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56  tZeroBlob(&p->aV
2429e 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0d 0a 20 20  ar[i-1], n);..  
2429f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
242a0 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
242a1 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  ex);..  }..  ret
242a2 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
242a3 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
242a4 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63 61  number of wildca
242a5 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  rds that can be 
242a6 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75 6e  potentially boun
242a7 64 20 74 6f 2e 0d 0a 2a 2a 20 54 68 69 73 20 72  d to...** This r
242a8 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20  outine is added 
242a9 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a  to support DBD::
242aa 53 51 4c 69 74 65 2e 20 20 0d 0a 2a 2f 0d 0a 53  SQLite.  ..*/..S
242ab 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
242ac 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
242ad 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74  eter_count(sqlit
242ae 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
242af 0d 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ..  Vdbe *p = (V
242b0 64 62 65 2a 29 70 53 74 6d 74 3b 0d 0a 20 20 72  dbe*)pStmt;..  r
242b1 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61  eturn p ? p->nVa
242b2 72 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  r : 0;..}..../*.
242b3 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
242b4 61 6d 65 20 6f 66 20 61 20 77 69 6c 64 63 61 72  ame of a wildcar
242b5 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 52 65  d parameter.  Re
242b6 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
242b7 20 69 6e 64 65 78 0d 0a 2a 2a 20 69 73 20 6f 75   index..** is ou
242b8 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66  t of range or if
242b9 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 69 73   the wildcard is
242ba 20 75 6e 6e 61 6d 65 64 2e 0d 0a 2a 2a 0d 0a 2a   unnamed...**..*
242bb 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  * The result is 
242bc 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0d 0a 2a  always UTF-8...*
242bd 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  /..SQLITE_API co
242be 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
242bf 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
242c0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
242c1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
242c2 29 7b 0d 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  ){..  Vdbe *p = 
242c3 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0d 0a 20  (Vdbe*)pStmt;.. 
242c4 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31   if( p==0 || i<1
242c5 20 7c 7c 20 69 3e 70 2d 3e 6e 7a 56 61 72 20 29   || i>p->nzVar )
242c6 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {..    return 0;
242c7 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
242c8 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0d 0a  p->azVar[i-1];..
242c9 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65  }..../*..** Give
242ca 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  n a wildcard par
242cb 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74  ameter name, ret
242cc 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
242cd 20 74 68 65 20 76 61 72 69 61 62 6c 65 0d 0a 2a   the variable..*
242ce 2a 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65  * with that name
242cf 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  .  If there is n
242d0 6f 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20  o variable with 
242d1 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0d  the given name,.
242d2 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a  .** return 0...*
242d3 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
242d4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
242d5 65 50 61 72 61 6d 65 74 65 72 49 6e 64 65 78 28  eParameterIndex(
242d6 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63  Vdbe *p, const c
242d7 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
242d8 6e 4e 61 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 69  nName){..  int i
242d9 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  ;..  if( p==0 ){
242da 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
242db 0a 20 20 7d 0d 0a 20 20 69 66 28 20 7a 4e 61 6d  .  }..  if( zNam
242dc 65 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  e ){..    for(i=
242dd 30 3b 20 69 3c 70 2d 3e 6e 7a 56 61 72 3b 20 69  0; i<p->nzVar; i
242de 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 73  ++){..      cons
242df 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e 61  t char *z = p->a
242e0 7a 56 61 72 5b 69 5d 3b 0d 0a 20 20 20 20 20 20  zVar[i];..      
242e1 69 66 28 20 7a 20 26 26 20 6d 65 6d 63 6d 70 28  if( z && memcmp(
242e2 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61 6d 65 29 3d 3d  z,zName,nName)==
242e3 30 20 26 26 20 7a 5b 6e 4e 61 6d 65 5d 3d 3d 30  0 && z[nName]==0
242e4 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74   ){..        ret
242e5 75 72 6e 20 69 2b 31 3b 0d 0a 20 20 20 20 20 20  urn i+1;..      
242e6 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  }..    }..  }.. 
242e7 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 53   return 0;..}..S
242e8 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
242e9 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
242ea 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74  eter_index(sqlit
242eb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
242ec 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
242ed 65 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71  e){..  return sq
242ee 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74  lite3VdbeParamet
242ef 65 72 49 6e 64 65 78 28 28 56 64 62 65 2a 29 70  erIndex((Vdbe*)p
242f0 53 74 6d 74 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c  Stmt, zName, sql
242f1 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
242f2 6d 65 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  me));..}..../*..
242f3 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c 20  ** Transfer all 
242f4 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 68  bindings from th
242f5 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e  e first statemen
242f6 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73 65  t over to the se
242f7 63 6f 6e 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  cond...*/..SQLIT
242f8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
242f9 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
242fa 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
242fb 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73  mt *pFromStmt, s
242fc 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f  qlite3_stmt *pTo
242fd 53 74 6d 74 29 7b 0d 0a 20 20 56 64 62 65 20 2a  Stmt){..  Vdbe *
242fe 70 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70  pFrom = (Vdbe*)p
242ff 46 72 6f 6d 53 74 6d 74 3b 0d 0a 20 20 56 64 62  FromStmt;..  Vdb
24300 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a 29  e *pTo = (Vdbe*)
24301 70 54 6f 53 74 6d 74 3b 0d 0a 20 20 69 6e 74 20  pToStmt;..  int 
24302 69 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54  i;..  assert( pT
24303 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62  o->db==pFrom->db
24304 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
24305 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d  To->nVar==pFrom-
24306 3e 6e 56 61 72 20 29 3b 0d 0a 20 20 73 71 6c 69  >nVar );..  sqli
24307 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
24308 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pTo->db->mutex);
24309 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
2430a 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29  From->nVar; i++)
2430b 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
2430c 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e  beMemMove(&pTo->
2430d 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d  aVar[i], &pFrom-
2430e 3e 61 56 61 72 5b 69 5d 29 3b 0d 0a 20 20 7d 0d  >aVar[i]);..  }.
2430f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
24310 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e  _leave(pTo->db->
24311 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72  mutex);..  retur
24312 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
24313 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
24314 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
24315 44 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 70 72 65 63  D../*..** Deprec
24316 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e  ated external in
24317 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e  terface.  Intern
24318 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63  al/core SQLite c
24319 6f 64 65 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  ode..** should c
2431a 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73  all sqlite3Trans
2431b 66 65 72 42 69 6e 64 69 6e 67 73 2e 0d 0a 2a 2a  ferBindings...**
2431c 0d 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 75 73  ..** Is is misus
2431d 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72  e to call this r
2431e 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 61 74  outine with stat
2431f 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 66 66  ements from diff
24320 65 72 65 6e 74 0d 0a 2a 2a 20 64 61 74 61 62 61  erent..** databa
24321 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20  se connections. 
24322 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 20   But as this is 
24323 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  a deprecated int
24324 65 72 66 61 63 65 2c 20 77 65 0d 0a 2a 2a 20 77  erface, we..** w
24325 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74  ill not bother t
24326 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74  o check for that
24327 20 63 6f 6e 64 69 74 69 6f 6e 2e 0d 0a 2a 2a 0d   condition...**.
24328 0a 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73  .** If the two s
24329 74 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69  tatements contai
2432a 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75  n a different nu
2432b 6d 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73  mber of bindings
2432c 2c 20 74 68 65 6e 0d 0a 2a 2a 20 61 6e 20 53 51  , then..** an SQ
2432d 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65  LITE_ERROR is re
2432e 74 75 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67  turned.  Nothing
2432f 20 65 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f   else can go wro
24330 6e 67 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65  ng, so otherwise
24331 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ..** SQLITE_OK i
24332 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d  s returned...*/.
24333 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
24334 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72  sqlite3_transfer
24335 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  _bindings(sqlite
24336 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d  3_stmt *pFromStm
24337 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  t, sqlite3_stmt 
24338 2a 70 54 6f 53 74 6d 74 29 7b 0d 0a 20 20 56 64  *pToStmt){..  Vd
24339 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62  be *pFrom = (Vdb
2433a 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0d 0a 20  e*)pFromStmt;.. 
2433b 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64   Vdbe *pTo = (Vd
2433c 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0d 0a 20 20  be*)pToStmt;..  
2433d 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21  if( pFrom->nVar!
2433e 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0d 0a 20  =pTo->nVar ){.. 
2433f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
24340 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 20 20  _ERROR;..  }..  
24341 69 66 28 20 70 54 6f 2d 3e 69 73 50 72 65 70 61  if( pTo->isPrepa
24342 72 65 56 32 20 26 26 20 70 54 6f 2d 3e 65 78 70  reV2 && pTo->exp
24343 6d 61 73 6b 20 29 7b 0d 0a 20 20 20 20 70 54 6f  mask ){..    pTo
24344 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0d 0a  ->expired = 1;..
24345 20 20 7d 0d 0a 20 20 69 66 28 20 70 46 72 6f 6d    }..  if( pFrom
24346 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26  ->isPrepareV2 &&
24347 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 6b 20   pFrom->expmask 
24348 29 7b 0d 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 65  ){..    pFrom->e
24349 78 70 69 72 65 64 20 3d 20 31 3b 0d 0a 20 20 7d  xpired = 1;..  }
2434a 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
2434b 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e  e3TransferBindin
2434c 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54  gs(pFromStmt, pT
2434d 6f 53 74 6d 74 29 3b 0d 0a 7d 0d 0a 23 65 6e 64  oStmt);..}..#end
2434e 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  if..../*..** Ret
2434f 75 72 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a  urn the sqlite3*
24350 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
24351 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 70 72   to which the pr
24352 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
24353 20 67 69 76 65 6e 0d 0a 2a 2a 20 69 6e 20 74 68   given..** in th
24354 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e  e argument belon
24355 67 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  gs.  This is the
24356 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 68   same database h
24357 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 0d 0a  andle that was..
24358 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67  ** the first arg
24359 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c  ument to the sql
2435a 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 74  ite3_prepare() t
2435b 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20  hat was used to 
2435c 63 72 65 61 74 65 0d 0a 2a 2a 20 74 68 65 20 73  create..** the s
2435d 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
2435e 66 69 72 73 74 20 70 6c 61 63 65 2e 0d 0a 2a 2f  first place...*/
2435f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ..SQLITE_API sql
24360 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62  ite3 *sqlite3_db
24361 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f  _handle(sqlite3_
24362 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0d 0a 20  stmt *pStmt){.. 
24363 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20   return pStmt ? 
24364 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
24365 64 62 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  db : 0;..}..../*
24366 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  ..** Return true
24367 20 69 66 20 74 68 65 20 70 72 65 70 61 72 65 64   if the prepared
24368 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 67 75   statement is gu
24369 61 72 61 6e 74 65 65 64 20 74 6f 20 6e 6f 74 20  aranteed to not 
2436a 6d 6f 64 69 66 79 20 74 68 65 0d 0a 2a 2a 20 64  modify the..** d
2436b 61 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 53 51  atabase...*/..SQ
2436c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2436d 69 74 65 33 5f 73 74 6d 74 5f 72 65 61 64 6f 6e  ite3_stmt_readon
2436e 6c 79 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ly(sqlite3_stmt 
2436f 2a 70 53 74 6d 74 29 7b 0d 0a 20 20 72 65 74 75  *pStmt){..  retu
24370 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 56 64 62  rn pStmt ? ((Vdb
24371 65 2a 29 70 53 74 6d 74 29 2d 3e 72 65 61 64 4f  e*)pStmt)->readO
24372 6e 6c 79 20 3a 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f  nly : 1;..}..../
24373 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  *..** Return tru
24374 65 20 69 66 20 74 68 65 20 70 72 65 70 61 72 65  e if the prepare
24375 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69  d statement is i
24376 6e 20 6e 65 65 64 20 6f 66 20 62 65 69 6e 67 20  n need of being 
24377 72 65 73 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  reset...*/..SQLI
24378 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
24379 65 33 5f 73 74 6d 74 5f 62 75 73 79 28 73 71 6c  e3_stmt_busy(sql
2437a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
2437b 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  ){..  Vdbe *v = 
2437c 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0d 0a 20  (Vdbe*)pStmt;.. 
2437d 20 72 65 74 75 72 6e 20 76 21 3d 30 20 26 26 20   return v!=0 && 
2437e 76 2d 3e 70 63 3e 30 20 26 26 20 76 2d 3e 6d 61  v->pc>0 && v->ma
2437f 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
24380 52 55 4e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  RUN;..}..../*..*
24381 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
24382 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70  er to the next p
24383 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
24384 74 20 61 66 74 65 72 20 70 53 74 6d 74 20 61 73  t after pStmt as
24385 73 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69 74  sociated..** wit
24386 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
24387 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70  ction pDb.  If p
24388 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65  Stmt is NULL, re
24389 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 0d 0a  turn the first..
2438a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ** prepared stat
2438b 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61  ement for the da
2438c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2438d 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  n.  Return NULL 
2438e 69 66 20 74 68 65 72 65 0d 0a 2a 2a 20 61 72 65  if there..** are
2438f 20 6e 6f 20 6d 6f 72 65 2e 0d 0a 2a 2f 0d 0a 53   no more...*/..S
24390 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
24391 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f  3_stmt *sqlite3_
24392 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65  next_stmt(sqlite
24393 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f  3 *pDb, sqlite3_
24394 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0d 0a 20  stmt *pStmt){.. 
24395 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
24396 4e 65 78 74 3b 0d 0a 20 20 73 71 6c 69 74 65 33  Next;..  sqlite3
24397 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62  _mutex_enter(pDb
24398 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 69 66 28  ->mutex);..  if(
24399 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0d 0a 20 20   pStmt==0 ){..  
2439a 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74    pNext = (sqlit
2439b 65 33 5f 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56  e3_stmt*)pDb->pV
2439c 64 62 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  dbe;..  }else{..
2439d 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c      pNext = (sql
2439e 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 64 62  ite3_stmt*)((Vdb
2439f 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 78 74  e*)pStmt)->pNext
243a0 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
243a1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44  3_mutex_leave(pD
243a2 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  b->mutex);..  re
243a3 74 75 72 6e 20 70 4e 65 78 74 3b 0d 0a 7d 0d 0a  turn pNext;..}..
243a4 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
243a5 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73  the value of a s
243a6 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f  tatus counter fo
243a7 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  r a prepared sta
243a8 74 65 6d 65 6e 74 0d 0a 2a 2f 0d 0a 53 51 4c 49  tement..*/..SQLI
243a9 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
243aa 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73  e3_stmt_status(s
243ab 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
243ac 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  mt, int op, int 
243ad 72 65 73 65 74 46 6c 61 67 29 7b 0d 0a 20 20 56  resetFlag){..  V
243ae 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64  dbe *pVdbe = (Vd
243af 62 65 2a 29 70 53 74 6d 74 3b 0d 0a 20 20 69 6e  be*)pStmt;..  in
243b0 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f  t v = pVdbe->aCo
243b1 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0d 0a 20 20  unter[op-1];..  
243b2 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20  if( resetFlag ) 
243b3 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b  pVdbe->aCounter[
243b4 6f 70 2d 31 5d 20 3d 20 30 3b 0d 0a 20 20 72 65  op-1] = 0;..  re
243b5 74 75 72 6e 20 76 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn v;..}..../*
243b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
243b7 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 20 2a  d of vdbeapi.c *
243b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
243bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
243bc 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 74 72  egin file vdbetr
243bd 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ace.c **********
243be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
243c0 2f 2a 0d 0a 2a 2a 20 32 30 30 39 20 4e 6f 76 65  /*..** 2009 Nove
243c1 6d 62 65 72 20 32 35 0d 0a 2a 2a 0d 0a 2a 2a 20  mber 25..**..** 
243c2 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
243c3 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
243c4 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
243c5 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
243c6 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  ..** a legal not
243c7 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
243c8 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  lessing:..**..**
243c9 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
243ca 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
243cb 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
243cc 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
243cd 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
243ce 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
243cf 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  s...**    May yo
243d0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
243d1 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
243d2 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
243d3 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
243d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
243d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
243d8 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .**..** This fil
243d9 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
243da 75 73 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74  used to insert t
243db 68 65 20 76 61 6c 75 65 73 20 6f 66 20 68 6f 73  he values of hos
243dc 74 20 70 61 72 61 6d 65 74 65 72 73 0d 0a 2a 2a  t parameters..**
243dd 20 28 61 6b 61 20 22 77 69 6c 64 63 61 72 64 73   (aka "wildcards
243de 22 29 20 69 6e 74 6f 20 74 68 65 20 53 51 4c 20  ") into the SQL 
243df 74 65 78 74 20 6f 75 74 70 75 74 20 62 79 20 73  text output by s
243e0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 2e 0d  qlite3_trace()..
243e1 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 56 64 62 65  .**..** The Vdbe
243e2 20 70 61 72 73 65 2d 74 72 65 65 20 65 78 70 6c   parse-tree expl
243e3 61 69 6e 65 72 20 69 73 20 61 6c 73 6f 20 66 6f  ainer is also fo
243e4 75 6e 64 20 68 65 72 65 2e 0d 0a 2a 2f 0d 0a 0d  und here...*/...
243e5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
243e6 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 0d 0a 2f 2a  OMIT_TRACE..../*
243e7 0d 0a 2a 2a 20 7a 53 71 6c 20 69 73 20 61 20 7a  ..** zSql is a z
243e8 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ero-terminated s
243e9 74 72 69 6e 67 20 6f 66 20 55 54 46 2d 38 20 53  tring of UTF-8 S
243ea 51 4c 20 74 65 78 74 2e 20 20 52 65 74 75 72 6e  QL text.  Return
243eb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a   the number of..
243ec 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 69 73  ** bytes in this
243ed 20 74 65 78 74 20 75 70 20 74 6f 20 62 75 74 20   text up to but 
243ee 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 66 69  excluding the fi
243ef 72 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e  rst character in
243f0 0d 0a 2a 2a 20 61 20 68 6f 73 74 20 70 61 72 61  ..** a host para
243f1 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20 74  meter.  If the t
243f2 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  ext contains no 
243f3 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 2c  host parameters,
243f4 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 74 68 65 20   return..** the 
243f5 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
243f6 62 79 74 65 73 20 69 6e 20 74 68 65 20 74 65 78  bytes in the tex
243f7 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  t...*/..static i
243f8 6e 74 20 66 69 6e 64 4e 65 78 74 48 6f 73 74 50  nt findNextHostP
243f9 61 72 61 6d 65 74 65 72 28 63 6f 6e 73 74 20 63  arameter(const c
243fa 68 61 72 20 2a 7a 53 71 6c 2c 20 69 6e 74 20 2a  har *zSql, int *
243fb 70 6e 54 6f 6b 65 6e 29 7b 0d 0a 20 20 69 6e 74  pnToken){..  int
243fc 20 74 6f 6b 65 6e 54 79 70 65 3b 0d 0a 20 20 69   tokenType;..  i
243fd 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0d 0a  nt nTotal = 0;..
243fe 20 20 69 6e 74 20 6e 3b 0d 0a 0d 0a 20 20 2a 70    int n;....  *p
243ff 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0d 0a 20 20 77  nToken = 0;..  w
24400 68 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 29 7b  hile( zSql[0] ){
24401 0d 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  ..    n = sqlite
24402 33 47 65 74 54 6f 6b 65 6e 28 28 75 38 2a 29 7a  3GetToken((u8*)z
24403 53 71 6c 2c 20 26 74 6f 6b 65 6e 54 79 70 65 29  Sql, &tokenType)
24404 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  ;..    assert( n
24405 3e 30 20 26 26 20 74 6f 6b 65 6e 54 79 70 65 21  >0 && tokenType!
24406 3d 54 4b 5f 49 4c 4c 45 47 41 4c 20 29 3b 0d 0a  =TK_ILLEGAL );..
24407 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 54 79 70      if( tokenTyp
24408 65 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29  e==TK_VARIABLE )
24409 7b 0d 0a 20 20 20 20 20 20 2a 70 6e 54 6f 6b 65  {..      *pnToke
2440a 6e 20 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 62 72  n = n;..      br
2440b 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2440c 20 6e 54 6f 74 61 6c 20 2b 3d 20 6e 3b 0d 0a 20   nTotal += n;.. 
2440d 20 20 20 7a 53 71 6c 20 2b 3d 20 6e 3b 0d 0a 20     zSql += n;.. 
2440e 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f   }..  return nTo
2440f 74 61 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  tal;..}..../*..*
24410 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
24411 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
24412 72 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69  r to a nul-termi
24413 6e 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20  nated string in 
24414 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 6f 62 74 61 69  memory..** obtai
24415 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
24416 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 73  DbMalloc(). If s
24417 71 6c 69 74 65 33 2e 76 64 62 65 45 78 65 63 43  qlite3.vdbeExecC
24418 6e 74 20 69 73 20 31 2c 20 74 68 65 6e 20 74 68  nt is 1, then th
24419 65 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e  e..** string con
2441a 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20  tains a copy of 
2441b 7a 52 61 77 53 71 6c 20 62 75 74 20 77 69 74 68  zRawSql but with
2441c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73   host parameters
2441d 20 65 78 70 61 6e 64 65 64 20 74 6f 20 0d 0a 2a   expanded to ..*
2441e 2a 20 74 68 65 69 72 20 63 75 72 72 65 6e 74 20  * their current 
2441f 62 69 6e 64 69 6e 67 73 2e 20 4f 72 2c 20 69 66  bindings. Or, if
24420 20 73 71 6c 69 74 65 33 2e 76 64 62 65 45 78 65   sqlite3.vdbeExe
24421 63 43 6e 74 20 69 73 20 67 72 65 61 74 65 72 20  cCnt is greater 
24422 74 68 61 6e 20 31 2c 20 0d 0a 2a 2a 20 74 68 65  than 1, ..** the
24423 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  n the returned s
24424 74 72 69 6e 67 20 68 6f 6c 64 73 20 61 20 63 6f  tring holds a co
24425 70 79 20 6f 66 20 7a 52 61 77 53 71 6c 20 77 69  py of zRawSql wi
24426 74 68 20 22 2d 2d 20 22 20 70 72 65 70 65 6e 64  th "-- " prepend
24427 65 64 0d 0a 2a 2a 20 74 6f 20 65 61 63 68 20 6c  ed..** to each l
24428 69 6e 65 20 6f 66 20 74 65 78 74 2e 0d 0a 2a 2a  ine of text...**
24429 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  ..** The calling
2442a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73   function is res
2442b 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b  ponsible for mak
2442c 69 6e 67 20 73 75 72 65 20 74 68 65 20 6d 65 6d  ing sure the mem
2442d 6f 72 79 20 72 65 74 75 72 6e 65 64 0d 0a 2a 2a  ory returned..**
2442e 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   is eventually f
2442f 72 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 4c  reed...**..** AL
24430 47 4f 52 49 54 48 4d 3a 20 20 53 63 61 6e 20 74  GORITHM:  Scan t
24431 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
24432 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 68 6f 73 74  looking for host
24433 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 61   parameters in a
24434 6e 79 20 6f 66 0d 0a 2a 2a 20 74 68 65 73 65 20  ny of..** these 
24435 66 6f 72 6d 73 3a 20 20 3f 2c 20 3f 4e 2c 20 24  forms:  ?, ?N, $
24436 41 2c 20 40 41 2c 20 3a 41 2e 20 20 54 61 6b 65  A, @A, :A.  Take
24437 20 63 61 72 65 20 74 6f 20 61 76 6f 69 64 20 74   care to avoid t
24438 65 78 74 20 77 69 74 68 69 6e 0d 0a 2a 2a 20 73  ext within..** s
24439 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 2c 20  tring literals, 
2443a 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65  quoted identifie
2443b 72 20 6e 61 6d 65 73 2c 20 61 6e 64 20 63 6f 6d  r names, and com
2443c 6d 65 6e 74 73 2e 20 20 46 6f 72 20 74 65 78 74  ments.  For text
2443d 20 66 6f 72 6d 73 2c 0d 0a 2a 2a 20 74 68 65 20   forms,..** the 
2443e 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  host parameter i
2443f 6e 64 65 78 20 69 73 20 66 6f 75 6e 64 20 62 79  ndex is found by
24440 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 70 65   scanning the pe
24441 72 70 61 72 65 64 0d 0a 2a 2a 20 73 74 61 74 65  rpared..** state
24442 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 63 6f 72  ment for the cor
24443 72 65 73 70 6f 6e 64 69 6e 67 20 4f 50 5f 56 61  responding OP_Va
24444 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 20  riable opcode.  
24445 4f 6e 63 65 20 74 68 65 20 68 6f 73 74 0d 0a 2a  Once the host..*
24446 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 65  * parameter inde
24447 78 20 69 73 20 6b 6e 6f 77 6e 2c 20 6c 6f 63 61  x is known, loca
24448 74 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  te the value in 
24449 70 2d 3e 61 56 61 72 5b 5d 2e 20 20 54 68 65 6e  p->aVar[].  Then
2444a 20 72 65 6e 64 65 72 0d 0a 2a 2a 20 74 68 65 20   render..** the 
2444b 76 61 6c 75 65 20 61 73 20 61 20 6c 69 74 65 72  value as a liter
2444c 61 6c 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74  al in place of t
2444d 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  he host paramete
2444e 72 20 6e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  r name...*/..SQL
2444f 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
24450 20 2a 73 71 6c 69 74 65 33 56 64 62 65 45 78 70   *sqlite3VdbeExp
24451 61 6e 64 53 71 6c 28 0d 0a 20 20 56 64 62 65 20  andSql(..  Vdbe 
24452 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
24453 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61      /* The prepa
24454 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 62 65  red statement be
24455 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 2a 2f  ing evaluated */
24456 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
24457 7a 52 61 77 53 71 6c 20 20 20 20 20 20 2f 2a 20  zRawSql      /* 
24458 52 61 77 20 74 65 78 74 20 6f 66 20 74 68 65 20  Raw text of the 
24459 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  SQL statement */
2445a 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  ..){..  sqlite3 
2445b 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
2445c 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2445d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a   connection */..
2445e 20 20 69 6e 74 20 69 64 78 20 3d 20 30 3b 20 20    int idx = 0;  
2445f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
24460 64 65 78 20 6f 66 20 61 20 68 6f 73 74 20 70 61  dex of a host pa
24461 72 61 6d 65 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  rameter */..  in
24462 74 20 6e 65 78 74 49 6e 64 65 78 20 3d 20 31 3b  t nextIndex = 1;
24463 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
24464 6f 66 20 6e 65 78 74 20 3f 20 68 6f 73 74 20 70  of next ? host p
24465 61 72 61 6d 65 74 65 72 20 2a 2f 0d 0a 20 20 69  arameter */..  i
24466 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
24467 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
24468 68 20 6f 66 20 61 20 74 6f 6b 65 6e 20 70 72 65  h of a token pre
24469 66 69 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54  fix */..  int nT
2446a 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
2446b 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2446c 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f  the parameter to
2446d 6b 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b  ken */..  int i;
2446e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2446f 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
24470 65 72 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 56  er */..  Mem *pV
24471 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
24472 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 61 20    /* Value of a 
24473 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 2a  host parameter *
24474 2f 0d 0a 20 20 53 74 72 41 63 63 75 6d 20 6f 75  /..  StrAccum ou
24475 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
24476 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20   Accumulate the 
24477 6f 75 74 70 75 74 20 68 65 72 65 20 2a 2f 0d 0a  output here */..
24478 20 20 63 68 61 72 20 7a 42 61 73 65 5b 31 30 30    char zBase[100
24479 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ];         /* In
2447a 69 74 69 61 6c 20 77 6f 72 6b 69 6e 67 20 73 70  itial working sp
2447b 61 63 65 20 2a 2f 0d 0a 0d 0a 20 20 64 62 20 3d  ace */....  db =
2447c 20 70 2d 3e 64 62 3b 0d 0a 20 20 73 71 6c 69 74   p->db;..  sqlit
2447d 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26  e3StrAccumInit(&
2447e 6f 75 74 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65  out, zBase, size
2447f 6f 66 28 7a 42 61 73 65 29 2c 20 0d 0a 20 20 20  of(zBase), ..   
24480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24481 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51     db->aLimit[SQ
24482 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
24483 48 5d 29 3b 0d 0a 20 20 6f 75 74 2e 64 62 20 3d  H]);..  out.db =
24484 20 64 62 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e   db;..  if( db->
24485 76 64 62 65 45 78 65 63 43 6e 74 3e 31 20 29 7b  vdbeExecCnt>1 ){
24486 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 52  ..    while( *zR
24487 61 77 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20 20  awSql ){..      
24488 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
24489 72 74 20 3d 20 7a 52 61 77 53 71 6c 3b 0d 0a 20  rt = zRawSql;.. 
2448a 20 20 20 20 20 77 68 69 6c 65 28 20 2a 28 7a 52       while( *(zR
2448b 61 77 53 71 6c 2b 2b 29 21 3d 27 5c 6e 27 20 26  awSql++)!='\n' &
2448c 26 20 2a 7a 52 61 77 53 71 6c 20 29 3b 0d 0a 20  & *zRawSql );.. 
2448d 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
2448e 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c  ccumAppend(&out,
2448f 20 22 2d 2d 20 22 2c 20 33 29 3b 0d 0a 20 20 20   "-- ", 3);..   
24490 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
24491 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 7a  umAppend(&out, z
24492 53 74 61 72 74 2c 20 28 69 6e 74 29 28 7a 52 61  Start, (int)(zRa
24493 77 53 71 6c 2d 7a 53 74 61 72 74 29 29 3b 0d 0a  wSql-zStart));..
24494 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d      }..  }else{.
24495 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 52 61 77  .    while( zRaw
24496 53 71 6c 5b 30 5d 20 29 7b 0d 0a 20 20 20 20 20  Sql[0] ){..     
24497 20 6e 20 3d 20 66 69 6e 64 4e 65 78 74 48 6f 73   n = findNextHos
24498 74 50 61 72 61 6d 65 74 65 72 28 7a 52 61 77 53  tParameter(zRawS
24499 71 6c 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0d 0a 20  ql, &nToken);.. 
2449a 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30       assert( n>0
2449b 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   );..      sqlit
2449c 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
2449d 28 26 6f 75 74 2c 20 7a 52 61 77 53 71 6c 2c 20  (&out, zRawSql, 
2449e 6e 29 3b 0d 0a 20 20 20 20 20 20 7a 52 61 77 53  n);..      zRawS
2449f 71 6c 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20 20 20  ql += n;..      
244a0 61 73 73 65 72 74 28 20 7a 52 61 77 53 71 6c 5b  assert( zRawSql[
244a1 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e 3d 3d 30 20  0] || nToken==0 
244a2 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 54  );..      if( nT
244a3 6f 6b 65 6e 3d 3d 30 20 29 20 62 72 65 61 6b 3b  oken==0 ) break;
244a4 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 52 61 77  ..      if( zRaw
244a5 53 71 6c 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0d 0a  Sql[0]=='?' ){..
244a6 20 20 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b          if( nTok
244a7 65 6e 3e 31 20 29 7b 0d 0a 20 20 20 20 20 20 20  en>1 ){..       
244a8 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
244a9 65 33 49 73 64 69 67 69 74 28 7a 52 61 77 53 71  e3Isdigit(zRawSq
244aa 6c 5b 31 5d 29 20 29 3b 0d 0a 20 20 20 20 20 20  l[1]) );..      
244ab 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 49 6e      sqlite3GetIn
244ac 74 33 32 28 26 7a 52 61 77 53 71 6c 5b 31 5d 2c  t32(&zRawSql[1],
244ad 20 26 69 64 78 29 3b 0d 0a 20 20 20 20 20 20 20   &idx);..       
244ae 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
244af 20 20 20 69 64 78 20 3d 20 6e 65 78 74 49 6e 64     idx = nextInd
244b0 65 78 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ex;..        }..
244b1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
244b2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 52        assert( zR
244b3 61 77 53 71 6c 5b 30 5d 3d 3d 27 3a 27 20 7c 7c  awSql[0]==':' ||
244b4 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 24 27   zRawSql[0]=='$'
244b5 20 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d   || zRawSql[0]==
244b6 27 40 27 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  '@' );..        
244b7 74 65 73 74 63 61 73 65 28 20 7a 52 61 77 53 71  testcase( zRawSq
244b8 6c 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0d 0a 20 20  l[0]==':' );..  
244b9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
244ba 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 24 27 20  zRawSql[0]=='$' 
244bb 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 65 73 74  );..        test
244bc 63 61 73 65 28 20 7a 52 61 77 53 71 6c 5b 30 5d  case( zRawSql[0]
244bd 3d 3d 27 40 27 20 29 3b 0d 0a 20 20 20 20 20 20  =='@' );..      
244be 20 20 69 64 78 20 3d 20 73 71 6c 69 74 65 33 56    idx = sqlite3V
244bf 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e 64 65  dbeParameterInde
244c0 78 28 70 2c 20 7a 52 61 77 53 71 6c 2c 20 6e 54  x(p, zRawSql, nT
244c1 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20  oken);..        
244c2 61 73 73 65 72 74 28 20 69 64 78 3e 30 20 29 3b  assert( idx>0 );
244c3 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
244c4 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 54 6f 6b   zRawSql += nTok
244c5 65 6e 3b 0d 0a 20 20 20 20 20 20 6e 65 78 74 49  en;..      nextI
244c6 6e 64 65 78 20 3d 20 69 64 78 20 2b 20 31 3b 0d  ndex = idx + 1;.
244c7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
244c8 64 78 3e 30 20 26 26 20 69 64 78 3c 3d 70 2d 3e  dx>0 && idx<=p->
244c9 6e 56 61 72 20 29 3b 0d 0a 20 20 20 20 20 20 70  nVar );..      p
244ca 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
244cb 64 78 2d 31 5d 3b 0d 0a 20 20 20 20 20 20 69 66  dx-1];..      if
244cc 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20  ( pVar->flags & 
244cd 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20  MEM_Null ){..   
244ce 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
244cf 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c  ccumAppend(&out,
244d0 20 22 4e 55 4c 4c 22 2c 20 34 29 3b 0d 0a 20 20   "NULL", 4);..  
244d1 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 56      }else if( pV
244d2 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ar->flags & MEM_
244d3 49 6e 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Int ){..        
244d4 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 26  sqlite3XPrintf(&
244d5 6f 75 74 2c 20 22 25 6c 6c 64 22 2c 20 70 56 61  out, "%lld", pVa
244d6 72 2d 3e 75 2e 69 29 3b 0d 0a 20 20 20 20 20 20  r->u.i);..      
244d7 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e  }else if( pVar->
244d8 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c  flags & MEM_Real
244d9 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
244da 69 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74  ite3XPrintf(&out
244db 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 56 61 72  , "%!.15g", pVar
244dc 2d 3e 72 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ->r);..      }el
244dd 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61  se if( pVar->fla
244de 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0d  gs & MEM_Str ){.
244df 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
244e0 4f 4d 49 54 5f 55 54 46 31 36 0d 0a 20 20 20 20  OMIT_UTF16..    
244e1 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43      u8 enc = ENC
244e2 28 64 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  (db);..        i
244e3 66 28 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55  f( enc!=SQLITE_U
244e4 54 46 38 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  TF8 ){..        
244e5 20 20 4d 65 6d 20 75 74 66 38 3b 0d 0a 20 20 20    Mem utf8;..   
244e6 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 75         memset(&u
244e7 74 66 38 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  tf8, 0, sizeof(u
244e8 74 66 38 29 29 3b 0d 0a 20 20 20 20 20 20 20 20  tf8));..        
244e9 20 20 75 74 66 38 2e 64 62 20 3d 20 64 62 3b 0d    utf8.db = db;.
244ea 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
244eb 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
244ec 26 75 74 66 38 2c 20 70 56 61 72 2d 3e 7a 2c 20  &utf8, pVar->z, 
244ed 70 56 61 72 2d 3e 6e 2c 20 65 6e 63 2c 20 53 51  pVar->n, enc, SQ
244ee 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20  LITE_STATIC);.. 
244ef 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
244f0 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
244f1 6e 67 28 26 75 74 66 38 2c 20 53 51 4c 49 54 45  ng(&utf8, SQLITE
244f2 5f 55 54 46 38 29 3b 0d 0a 20 20 20 20 20 20 20  _UTF8);..       
244f3 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74     sqlite3XPrint
244f4 66 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 27 22  f(&out, "'%.*q'"
244f5 2c 20 75 74 66 38 2e 6e 2c 20 75 74 66 38 2e 7a  , utf8.n, utf8.z
244f6 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71  );..          sq
244f7 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
244f8 61 73 65 28 26 75 74 66 38 29 3b 0d 0a 20 20 20  ase(&utf8);..   
244f9 20 20 20 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64       }else..#end
244fa 69 66 0d 0a 20 20 20 20 20 20 20 20 7b 0d 0a 20  if..        {.. 
244fb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
244fc 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 27  XPrintf(&out, "'
244fd 25 2e 2a 71 27 22 2c 20 70 56 61 72 2d 3e 6e 2c  %.*q'", pVar->n,
244fe 20 70 56 61 72 2d 3e 7a 29 3b 0d 0a 20 20 20 20   pVar->z);..    
244ff 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c      }..      }el
24500 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61  se if( pVar->fla
24501 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
24502 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
24503 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22  3XPrintf(&out, "
24504 7a 65 72 6f 62 6c 6f 62 28 25 64 29 22 2c 20 70  zeroblob(%d)", p
24505 56 61 72 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0d 0a  Var->u.nZero);..
24506 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
24507 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 56        assert( pV
24508 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ar->flags & MEM_
24509 42 6c 6f 62 20 29 3b 0d 0a 20 20 20 20 20 20 20  Blob );..       
2450a 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
2450b 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22 78 27  Append(&out, "x'
2450c 22 2c 20 32 29 3b 0d 0a 20 20 20 20 20 20 20 20  ", 2);..        
2450d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 61 72 2d  for(i=0; i<pVar-
2450e 3e 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  >n; i++){..     
2450f 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69       sqlite3XPri
24510 6e 74 66 28 26 6f 75 74 2c 20 22 25 30 32 78 22  ntf(&out, "%02x"
24511 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d 26 30 78 66  , pVar->z[i]&0xf
24512 66 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  f);..        }..
24513 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
24514 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f  trAccumAppend(&o
24515 75 74 2c 20 22 27 22 2c 20 31 29 3b 0d 0a 20 20  ut, "'", 1);..  
24516 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
24517 7d 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  }..  return sqli
24518 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
24519 68 28 26 6f 75 74 29 3b 0d 0a 7d 0d 0a 0d 0a 23  h(&out);..}....#
2451a 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
2451b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
2451c 43 45 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  CE */..../******
2451d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2451e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2451f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24521 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  *******..** The 
24522 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 69  following code i
24523 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 61  mplements the da
24524 74 61 2d 73 74 72 75 63 74 75 72 65 20 65 78 70  ta-structure exp
24525 6c 61 69 6e 69 6e 67 20 6c 6f 67 69 63 0d 0a 2a  laining logic..*
24526 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 2e 0d  * for the Vdbe..
24527 0a 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e  .*/....#if defin
24528 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
24529 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 29 0d 0a  _TREE_EXPLAIN)..
2452a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74  ../*..** Allocat
2452b 65 20 61 20 6e 65 77 20 45 78 70 6c 61 69 6e 20  e a new Explain 
2452c 6f 62 6a 65 63 74 0d 0a 2a 2f 0d 0a 53 51 4c 49  object..*/..SQLI
2452d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2452e 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 42 65  sqlite3ExplainBe
2452f 67 69 6e 28 56 64 62 65 20 2a 70 56 64 62 65 29  gin(Vdbe *pVdbe)
24530 7b 0d 0a 20 20 69 66 28 20 70 56 64 62 65 20 29  {..  if( pVdbe )
24531 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  {..    sqlite3Be
24532 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
24533 29 3b 0d 0a 20 20 20 20 45 78 70 6c 61 69 6e 20  );..    Explain 
24534 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  *p = sqlite3_mal
24535 6c 6f 63 28 20 73 69 7a 65 6f 66 28 45 78 70 6c  loc( sizeof(Expl
24536 61 69 6e 29 20 29 3b 0d 0a 20 20 20 20 69 66 28  ain) );..    if(
24537 20 70 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d   p ){..      mem
24538 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
24539 28 2a 70 29 29 3b 0d 0a 20 20 20 20 20 20 70 2d  (*p));..      p-
2453a 3e 70 56 64 62 65 20 3d 20 70 56 64 62 65 3b 0d  >pVdbe = pVdbe;.
2453b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2453c 72 65 65 28 70 56 64 62 65 2d 3e 70 45 78 70 6c  ree(pVdbe->pExpl
2453d 61 69 6e 29 3b 0d 0a 20 20 20 20 20 20 70 56 64  ain);..      pVd
2453e 62 65 2d 3e 70 45 78 70 6c 61 69 6e 20 3d 20 70  be->pExplain = p
2453f 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
24540 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 70 2d  StrAccumInit(&p-
24541 3e 73 74 72 2c 20 70 2d 3e 7a 42 61 73 65 2c 20  >str, p->zBase, 
24542 73 69 7a 65 6f 66 28 70 2d 3e 7a 42 61 73 65 29  sizeof(p->zBase)
24543 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
24544 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
24545 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b  ITE_MAX_LENGTH);
24546 0d 0a 20 20 20 20 20 20 70 2d 3e 73 74 72 2e 75  ..      p->str.u
24547 73 65 4d 61 6c 6c 6f 63 20 3d 20 32 3b 0d 0a 20  seMalloc = 2;.. 
24548 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
24549 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
2454a 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 20 20  nMalloc();..    
2454b 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  }..  }..}..../*.
2454c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
2454d 69 66 20 74 68 65 20 45 78 70 6c 61 69 6e 20 65  if the Explain e
2454e 6e 64 73 20 77 69 74 68 20 61 20 6e 65 77 2d 6c  nds with a new-l
2454f 69 6e 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ine...*/..static
24550 20 69 6e 74 20 65 6e 64 73 57 69 74 68 4e 4c 28   int endsWithNL(
24551 45 78 70 6c 61 69 6e 20 2a 70 29 7b 0d 0a 20 20  Explain *p){..  
24552 72 65 74 75 72 6e 20 70 20 26 26 20 70 2d 3e 73  return p && p->s
24553 74 72 2e 7a 54 65 78 74 20 26 26 20 70 2d 3e 73  tr.zText && p->s
24554 74 72 2e 6e 43 68 61 72 0d 0a 20 20 20 20 20 20  tr.nChar..      
24555 20 20 20 20 20 26 26 20 70 2d 3e 73 74 72 2e 7a       && p->str.z
24556 54 65 78 74 5b 70 2d 3e 73 74 72 2e 6e 43 68 61  Text[p->str.nCha
24557 72 2d 31 5d 3d 3d 27 5c 6e 27 3b 0d 0a 7d 0d 0a  r-1]=='\n';..}..
24558 20 20 20 20 0d 0a 2f 2a 0d 0a 2a 2a 20 41 70 70      ../*..** App
24559 65 6e 64 20 74 65 78 74 20 74 6f 20 74 68 65 20  end text to the 
2455a 69 6e 64 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2f 0d  indentation..*/.
2455b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2455c 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c  void sqlite3Expl
2455d 61 69 6e 50 72 69 6e 74 66 28 56 64 62 65 20 2a  ainPrintf(Vdbe *
2455e 70 56 64 62 65 2c 20 63 6f 6e 73 74 20 63 68 61  pVdbe, const cha
2455f 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
24560 7b 0d 0a 20 20 45 78 70 6c 61 69 6e 20 2a 70 3b  {..  Explain *p;
24561 0d 0a 20 20 69 66 28 20 70 56 64 62 65 20 26 26  ..  if( pVdbe &&
24562 20 28 70 20 3d 20 70 56 64 62 65 2d 3e 70 45 78   (p = pVdbe->pEx
24563 70 6c 61 69 6e 29 21 3d 30 20 29 7b 0d 0a 20 20  plain)!=0 ){..  
24564 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20    va_list ap;.. 
24565 20 20 20 69 66 28 20 70 2d 3e 6e 49 6e 64 65 6e     if( p->nInden
24566 74 20 26 26 20 65 6e 64 73 57 69 74 68 4e 4c 28  t && endsWithNL(
24567 70 29 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74  p) ){..      int
24568 20 6e 20 3d 20 70 2d 3e 6e 49 6e 64 65 6e 74 3b   n = p->nIndent;
24569 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 41 72  ..      if( n>Ar
2456a 72 61 79 53 69 7a 65 28 70 2d 3e 61 49 6e 64 65  raySize(p->aInde
2456b 6e 74 29 20 29 20 6e 20 3d 20 41 72 72 61 79 53  nt) ) n = ArrayS
2456c 69 7a 65 28 70 2d 3e 61 49 6e 64 65 6e 74 29 3b  ize(p->aIndent);
2456d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  ..      sqlite3A
2456e 70 70 65 6e 64 53 70 61 63 65 28 26 70 2d 3e 73  ppendSpace(&p->s
2456f 74 72 2c 20 70 2d 3e 61 49 6e 64 65 6e 74 5b 6e  tr, p->aIndent[n
24570 2d 31 5d 29 3b 0d 0a 20 20 20 20 7d 20 20 20 0d  -1]);..    }   .
24571 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70  .    va_start(ap
24572 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d 0a 20 20 20  , zFormat);..   
24573 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66   sqlite3VXPrintf
24574 28 26 70 2d 3e 73 74 72 2c 20 31 2c 20 7a 46 6f  (&p->str, 1, zFo
24575 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 20 20  rmat, ap);..    
24576 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 7d  va_end(ap);..  }
24577 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 70  ..}..../*..** Ap
24578 70 65 6e 64 20 61 20 27 5c 6e 27 20 69 66 20 74  pend a '\n' if t
24579 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  here is not alre
2457a 61 64 79 20 6f 6e 65 2e 0d 0a 2a 2f 0d 0a 53 51  ady one...*/..SQ
2457b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2457c 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e  d sqlite3Explain
2457d 4e 4c 28 56 64 62 65 20 2a 70 56 64 62 65 29 7b  NL(Vdbe *pVdbe){
2457e 0d 0a 20 20 45 78 70 6c 61 69 6e 20 2a 70 3b 0d  ..  Explain *p;.
2457f 0a 20 20 69 66 28 20 70 56 64 62 65 20 26 26 20  .  if( pVdbe && 
24580 28 70 20 3d 20 70 56 64 62 65 2d 3e 70 45 78 70  (p = pVdbe->pExp
24581 6c 61 69 6e 29 21 3d 30 20 26 26 20 21 65 6e 64  lain)!=0 && !end
24582 73 57 69 74 68 4e 4c 28 70 29 20 29 7b 0d 0a 20  sWithNL(p) ){.. 
24583 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
24584 75 6d 41 70 70 65 6e 64 28 26 70 2d 3e 73 74 72  umAppend(&p->str
24585 2c 20 22 5c 6e 22 2c 20 31 29 3b 0d 0a 20 20 7d  , "\n", 1);..  }
24586 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 75  ..}..../*..** Pu
24587 73 68 20 61 20 6e 65 77 20 69 6e 64 65 6e 74 61  sh a new indenta
24588 74 69 6f 6e 20 6c 65 76 65 6c 2e 20 20 53 75 62  tion level.  Sub
24589 73 65 71 75 65 6e 74 20 6c 69 6e 65 73 20 77 69  sequent lines wi
2458a 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 0d 0a  ll be indented..
2458b 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  ** so that they 
2458c 62 65 67 69 6e 20 61 74 20 74 68 65 20 63 75 72  begin at the cur
2458d 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69  rent cursor posi
2458e 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  tion...*/..SQLIT
2458f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24590 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 75 73  qlite3ExplainPus
24591 68 28 56 64 62 65 20 2a 70 56 64 62 65 29 7b 0d  h(Vdbe *pVdbe){.
24592 0a 20 20 45 78 70 6c 61 69 6e 20 2a 70 3b 0d 0a  .  Explain *p;..
24593 20 20 69 66 28 20 70 56 64 62 65 20 26 26 20 28    if( pVdbe && (
24594 70 20 3d 20 70 56 64 62 65 2d 3e 70 45 78 70 6c  p = pVdbe->pExpl
24595 61 69 6e 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  ain)!=0 ){..    
24596 69 66 28 20 70 2d 3e 73 74 72 2e 7a 54 65 78 74  if( p->str.zText
24597 20 26 26 20 70 2d 3e 6e 49 6e 64 65 6e 74 3c 41   && p->nIndent<A
24598 72 72 61 79 53 69 7a 65 28 70 2d 3e 61 49 6e 64  rraySize(p->aInd
24599 65 6e 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 63  ent) ){..      c
2459a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
2459b 2d 3e 73 74 72 2e 7a 54 65 78 74 3b 0d 0a 20 20  ->str.zText;..  
2459c 20 20 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e 73      int i = p->s
2459d 74 72 2e 6e 43 68 61 72 2d 31 3b 0d 0a 20 20 20  tr.nChar-1;..   
2459e 20 20 20 69 6e 74 20 78 3b 0d 0a 20 20 20 20 20     int x;..     
2459f 20 77 68 69 6c 65 28 20 69 3e 3d 30 20 26 26 20   while( i>=0 && 
245a0 7a 5b 69 5d 21 3d 27 5c 6e 27 20 29 7b 20 69 2d  z[i]!='\n' ){ i-
245a1 2d 3b 20 7d 0d 0a 20 20 20 20 20 20 78 20 3d 20  -; }..      x = 
245a2 28 70 2d 3e 73 74 72 2e 6e 43 68 61 72 20 2d 20  (p->str.nChar - 
245a3 31 29 20 2d 20 69 3b 0d 0a 20 20 20 20 20 20 69  1) - i;..      i
245a4 66 28 20 70 2d 3e 6e 49 6e 64 65 6e 74 20 26 26  f( p->nIndent &&
245a5 20 78 3c 70 2d 3e 61 49 6e 64 65 6e 74 5b 70 2d   x<p->aIndent[p-
245a6 3e 6e 49 6e 64 65 6e 74 2d 31 5d 20 29 7b 0d 0a  >nIndent-1] ){..
245a7 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 61          x = p->a
245a8 49 6e 64 65 6e 74 5b 70 2d 3e 6e 49 6e 64 65 6e  Indent[p->nInden
245a9 74 2d 31 5d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  t-1];..      }..
245aa 20 20 20 20 20 20 70 2d 3e 61 49 6e 64 65 6e 74        p->aIndent
245ab 5b 70 2d 3e 6e 49 6e 64 65 6e 74 5d 20 3d 20 78  [p->nIndent] = x
245ac 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d  ;..    }..    p-
245ad 3e 6e 49 6e 64 65 6e 74 2b 2b 3b 0d 0a 20 20 7d  >nIndent++;..  }
245ae 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f  ..}..../*..** Po
245af 70 20 74 68 65 20 69 6e 64 65 6e 74 61 74 69 6f  p the indentatio
245b0 6e 20 73 74 61 63 6b 20 62 79 20 6f 6e 65 20 6c  n stack by one l
245b1 65 76 65 6c 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  evel...*/..SQLIT
245b2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
245b3 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 6f 70  qlite3ExplainPop
245b4 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20 69 66  (Vdbe *p){..  if
245b5 28 20 70 20 26 26 20 70 2d 3e 70 45 78 70 6c 61  ( p && p->pExpla
245b6 69 6e 20 29 20 70 2d 3e 70 45 78 70 6c 61 69 6e  in ) p->pExplain
245b7 2d 3e 6e 49 6e 64 65 6e 74 2d 2d 3b 0d 0a 7d 0d  ->nIndent--;..}.
245b8 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 74  .../*..** Free t
245b9 68 65 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 73  he indentation s
245ba 74 72 75 63 74 75 72 65 0d 0a 2a 2f 0d 0a 53 51  tructure..*/..SQ
245bb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
245bc 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e  d sqlite3Explain
245bd 46 69 6e 69 73 68 28 56 64 62 65 20 2a 70 56 64  Finish(Vdbe *pVd
245be 62 65 29 7b 0d 0a 20 20 69 66 28 20 70 56 64 62  be){..  if( pVdb
245bf 65 20 26 26 20 70 56 64 62 65 2d 3e 70 45 78 70  e && pVdbe->pExp
245c0 6c 61 69 6e 20 29 7b 0d 0a 20 20 20 20 73 71 6c  lain ){..    sql
245c1 69 74 65 33 5f 66 72 65 65 28 70 56 64 62 65 2d  ite3_free(pVdbe-
245c2 3e 7a 45 78 70 6c 61 69 6e 29 3b 0d 0a 20 20 20  >zExplain);..   
245c3 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e   sqlite3ExplainN
245c4 4c 28 70 56 64 62 65 29 3b 0d 0a 20 20 20 20 70  L(pVdbe);..    p
245c5 56 64 62 65 2d 3e 7a 45 78 70 6c 61 69 6e 20 3d  Vdbe->zExplain =
245c6 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
245c7 46 69 6e 69 73 68 28 26 70 56 64 62 65 2d 3e 70  Finish(&pVdbe->p
245c8 45 78 70 6c 61 69 6e 2d 3e 73 74 72 29 3b 0d 0a  Explain->str);..
245c9 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
245ca 28 70 56 64 62 65 2d 3e 70 45 78 70 6c 61 69 6e  (pVdbe->pExplain
245cb 29 3b 0d 0a 20 20 20 20 70 56 64 62 65 2d 3e 70  );..    pVdbe->p
245cc 45 78 70 6c 61 69 6e 20 3d 20 30 3b 0d 0a 20 20  Explain = 0;..  
245cd 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
245ce 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 7d  gnMalloc();..  }
245cf 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
245d0 74 75 72 6e 20 74 68 65 20 65 78 70 6c 61 6e 61  turn the explana
245d1 74 69 6f 6e 20 6f 66 20 61 20 76 69 72 74 75 61  tion of a virtua
245d2 6c 20 6d 61 63 68 69 6e 65 2e 0d 0a 2a 2f 0d 0a  l machine...*/..
245d3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
245d4 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
245d5 65 33 56 64 62 65 45 78 70 6c 61 6e 61 74 69 6f  e3VdbeExplanatio
245d6 6e 28 56 64 62 65 20 2a 70 56 64 62 65 29 7b 0d  n(Vdbe *pVdbe){.
245d7 0a 20 20 72 65 74 75 72 6e 20 28 70 56 64 62 65  .  return (pVdbe
245d8 20 26 26 20 70 56 64 62 65 2d 3e 7a 45 78 70 6c   && pVdbe->zExpl
245d9 61 69 6e 29 20 3f 20 70 56 64 62 65 2d 3e 7a 45  ain) ? pVdbe->zE
245da 78 70 6c 61 69 6e 20 3a 20 30 3b 0d 0a 7d 0d 0a  xplain : 0;..}..
245db 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
245dc 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
245dd 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
245de 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
245df 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  etrace.c *******
245e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e2 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
245e3 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
245e4 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e vdbe.c *******
245e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e7 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
245e8 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
245e9 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74  ..**..** The aut
245ea 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
245eb 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
245ec 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
245ed 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20   place of..** a 
245ee 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
245ef 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
245f0 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79  :..**..**    May
245f1 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
245f2 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20   not evil...**  
245f3 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
245f4 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
245f5 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
245f6 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20  ive others...** 
245f7 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
245f8 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
245f9 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
245fa 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a  you give...**..*
245fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245ff 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65  ********..** The
24600 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
24601 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78  le implements ex
24602 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f  ecution method o
24603 66 20 74 68 65 20 0d 0a 2a 2a 20 56 69 72 74 75  f the ..** Virtu
24604 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67 69  al Database Engi
24605 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 73 65  ne (VDBE).  A se
24606 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 76 64  parate file ("vd
24607 62 65 61 75 78 2e 63 22 29 0d 0a 2a 2a 20 68 61  beaux.c")..** ha
24608 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69  ndles housekeepi
24609 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 20  ng details such 
2460a 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20  as creating and 
2460b 64 65 6c 65 74 69 6e 67 0d 0a 2a 2a 20 56 44 42  deleting..** VDB
2460c 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68  E instances.  Th
2460d 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c  is file is solel
2460e 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20  y interested in 
2460f 65 78 65 63 75 74 69 6e 67 0d 0a 2a 2a 20 74 68  executing..** th
24610 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0d  e VDBE program..
24611 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74 68 65 20 65  .**..** In the e
24612 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63  xternal interfac
24613 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73  e, an "sqlite3_s
24614 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61 71  tmt*" is an opaq
24615 75 65 20 70 6f 69 6e 74 65 72 0d 0a 2a 2a 20 74  ue pointer..** t
24616 6f 20 61 20 56 44 42 45 2e 0d 0a 2a 2a 0d 0a 2a  o a VDBE...**..*
24617 2a 20 54 68 65 20 53 51 4c 20 70 61 72 73 65 72  * The SQL parser
24618 20 67 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f   generates a pro
24619 67 72 61 6d 20 77 68 69 63 68 20 69 73 20 74 68  gram which is th
2461a 65 6e 20 65 78 65 63 75 74 65 64 20 62 79 0d 0a  en executed by..
2461b 2a 2a 20 74 68 65 20 56 44 42 45 20 74 6f 20 64  ** the VDBE to d
2461c 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 74 68  o the work of th
2461d 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
2461e 20 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20    VDBE programs 
2461f 61 72 65 20 0d 0a 2a 2a 20 73 69 6d 69 6c 61 72  are ..** similar
24620 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 73 65   in form to asse
24621 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e 20 20  mbly language.  
24622 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 6e 73  The program cons
24623 69 73 74 73 20 6f 66 0d 0a 2a 2a 20 61 20 6c 69  ists of..** a li
24624 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f 66  near sequence of
24625 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 61   operations.  Ea
24626 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73  ch operation has
24627 20 61 6e 20 6f 70 63 6f 64 65 20 0d 0a 2a 2a 20   an opcode ..** 
24628 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20  and 5 operands. 
24629 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32   Operands P1, P2
2462a 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74  , and P3 are int
2462b 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20  egers.  Operand 
2462c 50 34 20 0d 0a 2a 2a 20 69 73 20 61 20 6e 75 6c  P4 ..** is a nul
2462d 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  l-terminated str
2462e 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35  ing.  Operand P5
2462f 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
24630 63 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 20 46  character...** F
24631 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61  ew opcodes use a
24632 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0d 0a  ll 5 operands...
24633 2a 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69  **..** Computati
24634 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20 73  on results are s
24635 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f  tored on a set o
24636 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62  f registers numb
24637 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0d 0a  ered beginning..
24638 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f  ** with 1 and go
24639 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e  ing up to Vdbe.n
2463a 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73  Mem.  Each regis
2463b 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0d 0a 2a  ter can store..*
2463c 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65  * either an inte
2463d 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  ger, a null-term
2463e 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61  inated string, a
2463f 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0d   floating point.
24640 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74  .** number, or t
24641 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61  he SQL "NULL" va
24642 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69  lue.  An implici
24643 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f  t conversion fro
24644 6d 20 6f 6e 65 0d 0a 2a 2a 20 74 79 70 65 20 74  m one..** type t
24645 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75  o the other occu
24646 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  rs as necessary.
24647 0d 0a 2a 2a 20 0d 0a 2a 2a 20 4d 6f 73 74 20 6f  ..** ..** Most o
24648 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
24649 69 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e  is file is taken
2464a 20 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74   up by the sqlit
2464b 65 33 56 64 62 65 45 78 65 63 28 29 0d 0a 2a 2a  e3VdbeExec()..**
2464c 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20   function which 
2464d 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66  does the work of
2464e 20 69 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20   interpreting a 
2464f 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a  VDBE program...*
24650 2a 20 42 75 74 20 6f 74 68 65 72 20 72 6f 75 74  * But other rout
24651 69 6e 65 73 20 61 72 65 20 61 6c 73 6f 20 70 72  ines are also pr
24652 6f 76 69 64 65 64 20 74 6f 20 68 65 6c 70 20 69  ovided to help i
24653 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 0d 0a 2a  n building up..*
24654 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 6e 73 74  * a program inst
24655 72 75 63 74 69 6f 6e 20 62 79 20 69 6e 73 74 72  ruction by instr
24656 75 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  uction...**..** 
24657 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 20  Various scripts 
24658 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 65  scan this source
24659 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74   file in order t
2465a 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c 0d  o generate HTML.
2465b 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  .** documentatio
2465c 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73  n, headers files
2465d 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76  , or other deriv
2465e 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66  ed files.  The f
2465f 6f 72 6d 61 74 74 69 6e 67 0d 0a 2a 2a 20 6f 66  ormatting..** of
24660 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   the code in thi
24661 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65  s file is, there
24662 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e  fore, important.
24663 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d    See other comm
24664 65 6e 74 73 0d 0a 2a 2a 20 69 6e 20 74 68 69 73  ents..** in this
24665 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c   file for detail
24666 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c  s.  If in doubt,
24667 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20   do not deviate 
24668 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0d 0a 2a  from existing..*
24669 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64  * commenting and
2466a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61   indentation pra
2466b 63 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e  ctices when chan
2466c 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63  ging or adding c
2466d 6f 64 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  ode...*/..../*..
2466e 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73 20 6d  ** Invoke this m
2466f 61 63 72 6f 20 6f 6e 20 6d 65 6d 6f 72 79 20 63  acro on memory c
24670 65 6c 6c 73 20 6a 75 73 74 20 70 72 69 6f 72 20  ells just prior 
24671 74 6f 20 63 68 61 6e 67 69 6e 67 20 74 68 65 0d  to changing the.
24672 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65  .** value of the
24673 20 63 65 6c 6c 2e 20 20 54 68 69 73 20 6d 61 63   cell.  This mac
24674 72 6f 20 76 65 72 69 66 69 65 73 20 74 68 61 74  ro verifies that
24675 20 73 68 61 6c 6c 6f 77 20 63 6f 70 69 65 73 20   shallow copies 
24676 61 72 65 0d 0a 2a 2a 20 6e 6f 74 20 6d 69 73 75  are..** not misu
24677 73 65 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66  sed...*/..#ifdef
24678 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 23   SQLITE_DEBUG..#
24679 20 64 65 66 69 6e 65 20 6d 65 6d 41 62 6f 75 74   define memAbout
2467a 54 6f 43 68 61 6e 67 65 28 50 2c 4d 29 20 73 71  ToChange(P,M) sq
2467b 6c 69 74 65 33 56 64 62 65 4d 65 6d 41 62 6f 75  lite3VdbeMemAbou
2467c 74 54 6f 43 68 61 6e 67 65 28 50 2c 4d 29 0d 0a  tToChange(P,M)..
2467d 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
2467e 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65  memAboutToChange
2467f 28 50 2c 4d 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d  (P,M)..#endif...
24680 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ./*..** The foll
24681 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
24682 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65  iable is increme
24683 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d 65 20  nted every time 
24684 61 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 6d 6f 76  a cursor..** mov
24685 65 73 2c 20 65 69 74 68 65 72 20 62 79 20 74 68  es, either by th
24686 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f  e OP_SeekXX, OP_
24687 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76  Next, or OP_Prev
24688 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 74   opcodes.  The t
24689 65 73 74 0d 0a 2a 2a 20 70 72 6f 63 65 64 75 72  est..** procedur
2468a 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f  es use this info
2468b 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20  rmation to make 
2468c 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 63 65  sure that indice
2468d 73 20 61 72 65 0d 0a 2a 2a 20 77 6f 72 6b 69 6e  s are..** workin
2468e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 68  g correctly.  Th
2468f 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20  is variable has 
24690 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65  no function othe
24691 72 20 74 68 61 6e 20 74 6f 0d 0a 2a 2a 20 68 65  r than to..** he
24692 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
24693 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
24694 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0d  of the library..
24695 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
24696 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f  TE_TEST..SQLITE_
24697 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
24698 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30  search_count = 0
24699 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  ;..#endif..../*.
2469a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c  .** When this gl
2469b 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73  obal variable is
2469c 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65   positive, it ge
2469d 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f  ts decremented o
2469e 6e 63 65 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 65  nce before..** e
2469f 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ach instruction 
246a0 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57 68  in the VDBE.  Wh
246a1 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a 65  en it reaches ze
246a2 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74  ro, the u1.isInt
246a3 65 72 72 75 70 74 65 64 0d 0a 2a 2a 20 66 69 65  errupted..** fie
246a4 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ld of the sqlite
246a5 33 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73  3 structure is s
246a6 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  et in order to s
246a7 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72  imulate an inter
246a8 72 75 70 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  rupt...**..** Th
246a9 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 75  is facility is u
246aa 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
246ab 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20  purposes only.  
246ac 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63  It does not func
246ad 74 69 6f 6e 0d 0a 2a 2a 20 69 6e 20 61 6e 20 6f  tion..** in an o
246ae 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0d 0a  rdinary build...
246af 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
246b0 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f 41  E_TEST..SQLITE_A
246b1 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
246b2 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d  nterrupt_count =
246b3 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f   0;..#endif..../
246b4 2a 0d 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  *..** The next g
246b5 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69  lobal variable i
246b6 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61  s incremented ea
246b7 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f 53  ch type the OP_S
246b8 6f 72 74 20 6f 70 63 6f 64 65 0d 0a 2a 2a 20 69  ort opcode..** i
246b9 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65  s executed.  The
246ba 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
246bb 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d   use this inform
246bc 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
246bd 72 65 20 74 68 61 74 0d 0a 2a 2a 20 73 6f 72 74  re that..** sort
246be 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67  ing is occurring
246bf 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e   or not occurrin
246c0 67 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65  g at appropriate
246c1 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76   times.   This v
246c2 61 72 69 61 62 6c 65 0d 0a 2a 2a 20 68 61 73 20  ariable..** has 
246c3 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65  no function othe
246c4 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76  r than to help v
246c5 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63  erify the correc
246c6 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  t operation of t
246c7 68 65 0d 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0d  he..** library..
246c8 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
246c9 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45 5f  TE_TEST..SQLITE_
246ca 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
246cb 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d  sort_count = 0;.
246cc 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
246cd 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61  * The next globa
246ce 6c 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72  l variable recor
246cf 64 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ds the size of t
246d0 68 65 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42  he largest MEM_B
246d1 6c 6f 62 0d 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53  lob..** or MEM_S
246d2 74 72 20 74 68 61 74 20 68 61 73 20 62 65 65 6e  tr that has been
246d3 20 75 73 65 64 20 62 79 20 61 20 56 44 42 45 20   used by a VDBE 
246d4 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74 65 73  opcode.  The tes
246d5 74 20 70 72 6f 63 65 64 75 72 65 73 0d 0a 2a 2a  t procedures..**
246d6 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d   use this inform
246d7 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
246d8 72 65 20 74 68 61 74 20 74 68 65 20 7a 65 72 6f  re that the zero
246d9 2d 62 6c 6f 62 20 66 75 6e 63 74 69 6f 6e 61 6c  -blob functional
246da 69 74 79 0d 0a 2a 2a 20 69 73 20 77 6f 72 6b 69  ity..** is worki
246db 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 20  ng correctly.   
246dc 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61  This variable ha
246dd 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
246de 68 65 72 20 74 68 61 6e 20 74 6f 0d 0a 2a 2a 20  her than to..** 
246df 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20  help verify the 
246e0 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f  correct operatio
246e1 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  n of the library
246e2 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
246e3 4c 49 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54  LITE_TEST..SQLIT
246e4 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
246e5 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d  3_max_blobsize =
246e6 20 30 3b 0d 0a 73 74 61 74 69 63 20 76 6f 69 64   0;..static void
246e7 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69   updateMaxBlobsi
246e8 7a 65 28 4d 65 6d 20 2a 70 29 7b 0d 0a 20 20 69  ze(Mem *p){..  i
246e9 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28  f( (p->flags & (
246ea 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
246eb 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71  ))!=0 && p->n>sq
246ec 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69  lite3_max_blobsi
246ed 7a 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ze ){..    sqlit
246ee 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
246ef 3d 20 70 2d 3e 6e 3b 0d 0a 20 20 7d 0d 0a 7d 0d  = p->n;..  }..}.
246f0 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
246f1 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61  * The next globa
246f2 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e  l variable is in
246f3 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
246f4 79 70 65 20 74 68 65 20 4f 50 5f 46 6f 75 6e 64  ype the OP_Found
246f5 20 6f 70 63 6f 64 65 0d 0a 2a 2a 20 69 73 20 65   opcode..** is e
246f6 78 65 63 75 74 65 64 2e 20 54 68 69 73 20 69 73  xecuted. This is
246f7 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 77 68   used to test wh
246f8 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
246f9 20 66 6f 72 65 69 67 6e 20 6b 65 79 0d 0a 2a 2a   foreign key..**
246fa 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c 65   operation imple
246fb 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 4f 50 5f  mented using OP_
246fc 46 6b 49 73 5a 65 72 6f 20 69 73 20 77 6f 72 6b  FkIsZero is work
246fd 69 6e 67 2e 20 54 68 69 73 20 76 61 72 69 61 62  ing. This variab
246fe 6c 65 0d 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75  le..** has no fu
246ff 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
24700 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 66 79  n to help verify
24701 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
24702 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0d 0a 2a  ration of the..*
24703 2a 20 6c 69 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a  * library...*/..
24704 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
24705 53 54 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ST..SQLITE_API i
24706 6e 74 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64  nt sqlite3_found
24707 5f 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a 23 65 6e  _count = 0;..#en
24708 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 65  dif..../*..** Te
24709 73 74 20 61 20 72 65 67 69 73 74 65 72 20 74 6f  st a register to
2470a 20 73 65 65 20 69 66 20 69 74 20 65 78 63 65 65   see if it excee
2470b 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  ds the current m
2470c 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65  aximum blob size
2470d 2e 0d 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73  ...** If it does
2470e 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77  , record the new
2470f 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69   maximum blob si
24710 7a 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 64 65 66  ze...*/..#if def
24711 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
24712 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
24713 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
24714 4e 5f 54 45 53 54 29 0d 0a 23 20 64 65 66 69 6e  N_TEST)..# defin
24715 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f  e UPDATE_MAX_BLO
24716 42 53 49 5a 45 28 50 29 20 20 75 70 64 61 74 65  BSIZE(P)  update
24717 4d 61 78 42 6c 6f 62 73 69 7a 65 28 50 29 0d 0a  MaxBlobsize(P)..
24718 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
24719 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
2471a 49 5a 45 28 50 29 0d 0a 23 65 6e 64 69 66 0d 0a  IZE(P)..#endif..
2471b 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 76 65 72 74  ../*..** Convert
2471c 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 73   the given regis
2471d 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ter into a strin
2471e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e  g if it isn't on
2471f 65 0d 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52  e..** already. R
24720 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
24721 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
24722 6c 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  ls...*/..#define
24723 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e   Stringify(P, en
24724 63 29 20 5c 0d 0a 20 20 20 69 66 28 28 28 50 29  c) \..   if(((P)
24725 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  ->flags&(MEM_Str
24726 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26  |MEM_Blob))==0 &
24727 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  & sqlite3VdbeMem
24728 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29  Stringify(P,enc)
24729 29 20 5c 0d 0a 20 20 20 20 20 7b 20 67 6f 74 6f  ) \..     { goto
2472a 20 6e 6f 5f 6d 65 6d 3b 20 7d 0d 0a 0d 0a 2f 2a   no_mem; }..../*
2472b 0d 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61  ..** An ephemera
2472c 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28  l string value (
2472d 73 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65  signified by the
2472e 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29   MEM_Ephem flag)
2472f 20 63 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 61 20   contains..** a 
24730 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e  pointer to a dyn
24731 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
24732 65 64 20 73 74 72 69 6e 67 20 77 68 65 72 65 20  ed string where 
24733 73 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74  some other entit
24734 79 0d 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73  y..** is respons
24735 69 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63  ible for dealloc
24736 61 74 69 6e 67 20 74 68 61 74 20 73 74 72 69 6e  ating that strin
24737 67 2e 20 20 42 65 63 61 75 73 65 20 74 68 65 20  g.  Because the 
24738 72 65 67 69 73 74 65 72 0d 0a 2a 2a 20 64 6f 65  register..** doe
24739 73 20 6e 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68  s not control th
2473a 65 20 73 74 72 69 6e 67 2c 20 69 74 20 6d 69 67  e string, it mig
2473b 68 74 20 62 65 20 64 65 6c 65 74 65 64 20 77 69  ht be deleted wi
2473c 74 68 6f 75 74 20 74 68 65 20 72 65 67 69 73 74  thout the regist
2473d 65 72 0d 0a 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69  er..** knowing i
2473e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  t...**..** This 
2473f 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72 74 73  routine converts
24740 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   an ephemeral st
24741 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79 6e 61  ring into a dyna
24742 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
24743 64 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68 61  d..** string tha
24744 74 20 74 68 65 20 72 65 67 69 73 74 65 72 20 69  t the register i
24745 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e 20  tself controls. 
24746 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
24747 20 69 74 0d 0a 2a 2a 20 63 6f 6e 76 65 72 74 73   it..** converts
24748 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74   an MEM_Ephem st
24749 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d  ring into an MEM
2474a 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0d 0a 2a 2f  _Dyn string...*/
2474b 0d 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65  ..#define Deephe
2474c 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0d 0a 20  meralize(P) \.. 
2474d 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67    if( ((P)->flag
2474e 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20  s&MEM_Ephem)!=0 
2474f 5c 0d 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c  \..       && sql
24750 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
24751 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67  riteable(P) ){ g
24752 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0d 0a 0d 0a  oto no_mem;}....
24753 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  /* Return true i
24754 66 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73  f the cursor was
24755 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 74 68   opened using th
24756 65 20 4f 50 5f 4f 70 65 6e 53 6f 72 74 65 72 20  e OP_OpenSorter 
24757 6f 70 63 6f 64 65 2e 20 2a 2f 0d 0a 23 69 66 64  opcode. */..#ifd
24758 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
24759 45 52 47 45 5f 53 4f 52 54 0d 0a 23 20 64 65 66  ERGE_SORT..# def
2475a 69 6e 65 20 69 73 53 6f 72 74 65 72 28 78 29 20  ine isSorter(x) 
2475b 30 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69  0..#else..# defi
2475c 6e 65 20 69 73 53 6f 72 74 65 72 28 78 29 20 28  ne isSorter(x) (
2475d 28 78 29 2d 3e 70 53 6f 72 74 65 72 21 3d 30 29  (x)->pSorter!=0)
2475e 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
2475f 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d  ** Argument pMem
24760 20 70 6f 69 6e 74 73 20 61 74 20 61 20 72 65 67   points at a reg
24761 69 73 74 65 72 20 74 68 61 74 20 77 69 6c 6c 20  ister that will 
24762 62 65 20 70 61 73 73 65 64 20 74 6f 20 61 0d 0a  be passed to a..
24763 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  ** user-defined 
24764 66 75 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75  function or retu
24765 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  rned to the user
24766 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f   as the result o
24767 66 20 61 20 71 75 65 72 79 2e 0d 0a 2a 2a 20 54  f a query...** T
24768 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
24769 20 74 68 65 20 70 4d 65 6d 2d 3e 74 79 70 65 20   the pMem->type 
2476a 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 62 79  variable used by
2476b 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   the sqlite3_val
2476c 75 65 5f 2a 28 29 20 0d 0a 2a 2a 20 72 6f 75 74  ue_*() ..** rout
2476d 69 6e 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ines...*/..SQLIT
2476e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2476f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f  qlite3VdbeMemSto
24770 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 6d  reType(Mem *pMem
24771 29 7b 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 20  ){..  int flags 
24772 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0d 0a  = pMem->flags;..
24773 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45    if( flags & ME
24774 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 70  M_Null ){..    p
24775 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
24776 54 45 5f 4e 55 4c 4c 3b 0d 0a 20 20 7d 0d 0a 20  TE_NULL;..  }.. 
24777 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20   else if( flags 
24778 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a 20 20  & MEM_Int ){..  
24779 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
2477a 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0d 0a  QLITE_INTEGER;..
2477b 20 20 7d 0d 0a 20 20 65 6c 73 65 20 69 66 28 20    }..  else if( 
2477c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c  flags & MEM_Real
2477d 20 29 7b 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74   ){..    pMem->t
2477e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f  ype = SQLITE_FLO
2477f 41 54 3b 0d 0a 20 20 7d 0d 0a 20 20 65 6c 73 65  AT;..  }..  else
24780 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
24781 5f 53 74 72 20 29 7b 0d 0a 20 20 20 20 70 4d 65  _Str ){..    pMe
24782 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
24783 5f 54 45 58 54 3b 0d 0a 20 20 7d 65 6c 73 65 7b  _TEXT;..  }else{
24784 0d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  ..    pMem->type
24785 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0d   = SQLITE_BLOB;.
24786 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
24787 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43  * Allocate VdbeC
24788 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75  ursor number iCu
24789 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
2478a 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74  nter to it.  Ret
2478b 75 72 6e 20 4e 55 4c 4c 0d 0a 2a 2a 20 69 66 20  urn NULL..** if 
2478c 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
2478d 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  mory...*/..stati
2478e 63 20 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c  c VdbeCursor *al
2478f 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 0d 0a 20  locateCursor(.. 
24790 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
24791 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
24792 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f  rtual machine */
24793 0d 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  ..  int iCur,   
24794 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
24795 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 56 64  ex of the new Vd
24796 62 65 43 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 69  beCursor */..  i
24797 6e 74 20 6e 46 69 65 6c 64 2c 20 20 20 20 20 20  nt nField,      
24798 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
24799 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
2479a 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a  table or index *
2479b 2f 0d 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  /..  int iDb,   
2479c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2479d 74 61 62 61 73 65 20 74 68 65 20 63 75 72 73 6f  tabase the curso
2479e 72 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72  r belongs to, or
2479f 20 2d 31 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73   -1 */..  int is
247a0 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 20 20  BtreeCursor     
247a1 2f 2a 20 54 72 75 65 20 66 6f 72 20 42 2d 54 72  /* True for B-Tr
247a2 65 65 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 70  ee.  False for p
247a3 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76  seudo-table or v
247a4 74 61 62 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 2f 2a  tab */..){..  /*
247a5 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79   Find the memory
247a6 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20   cell that will 
247a7 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  be used to store
247a8 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d   the blob of mem
247a9 6f 72 79 0d 0a 20 20 2a 2a 20 72 65 71 75 69 72  ory..  ** requir
247aa 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65  ed for this Vdbe
247ab 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
247ac 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65  . It is convenie
247ad 6e 74 20 74 6f 20 75 73 65 20 61 20 0d 0a 20 20  nt to use a ..  
247ae 2a 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63  ** vdbe memory c
247af 65 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68  ell to manage th
247b0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
247b1 69 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72  ion required for
247b2 20 61 0d 0a 20 20 2a 2a 20 56 64 62 65 43 75 72   a..  ** VdbeCur
247b3 73 6f 72 20 73 74 72 75 63 74 75 72 65 20 66 6f  sor structure fo
247b4 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
247b5 72 65 61 73 6f 6e 73 3a 0d 0a 20 20 2a 2a 0d 0a  reasons:..  **..
247b6 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74 69 6d    **   * Sometim
247b7 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  es cursor number
247b8 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 61  s are used for a
247b9 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65   couple of diffe
247ba 72 65 6e 74 0d 0a 20 20 2a 2a 20 20 20 20 20 70  rent..  **     p
247bb 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64 62  urposes in a vdb
247bc 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20 64  e program. The d
247bd 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d 69  ifferent uses mi
247be 67 68 74 20 72 65 71 75 69 72 65 0d 0a 20 20 2a  ght require..  *
247bf 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20  *     different 
247c0 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  sized allocation
247c1 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  s. Memory cells 
247c2 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65  provide growable
247c3 0d 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63  ..  **     alloc
247c4 61 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20  ations...  **.. 
247c5 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69   **   * When usi
247c6 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  ng ENABLE_MEMORY
247c7 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d  _MANAGEMENT, mem
247c8 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73  ory cell buffers
247c9 20 63 61 6e 0d 0a 20 20 2a 2a 20 20 20 20 20 62   can..  **     b
247ca 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76  e freed lazily v
247cb 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  ia the sqlite3_r
247cc 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
247cd 41 50 49 2e 20 54 68 69 73 0d 0a 20 20 2a 2a 20  API. This..  ** 
247ce 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68      minimizes th
247cf 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c  e number of mall
247d0 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79  oc calls made by
247d1 20 74 68 65 20 73 79 73 74 65 6d 2e 0d 0a 20 20   the system...  
247d2 2a 2a 0d 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20  **..  ** Memory 
247d3 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72  cells for cursor
247d4 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  s are allocated 
247d5 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  at the top of th
247d6 65 20 61 64 64 72 65 73 73 0d 0a 20 20 2a 2a 20  e address..  ** 
247d7 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65  space. Memory ce
247d8 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72  ll (p->nMem) cor
247d9 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73  responds to curs
247da 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0d  or 0. Space for.
247db 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 69  .  ** cursor 1 i
247dc 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 6d  s managed by mem
247dd 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65  ory cell (p->nMe
247de 6d 2d 31 29 2c 20 65 74 63 2e 0d 0a 20 20 2a 2f  m-1), etc...  */
247df 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20  ..  Mem *pMem = 
247e0 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d  &p->aMem[p->nMem
247e1 2d 69 43 75 72 5d 3b 0d 0a 0d 0a 20 20 69 6e 74  -iCur];....  int
247e2 20 6e 42 79 74 65 3b 0d 0a 20 20 56 64 62 65 43   nByte;..  VdbeC
247e3 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30 3b 0d  ursor *pCx = 0;.
247e4 0a 20 20 6e 42 79 74 65 20 3d 20 0d 0a 20 20 20  .  nByte = ..   
247e5 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66     ROUND8(sizeof
247e6 28 56 64 62 65 43 75 72 73 6f 72 29 29 20 2b 20  (VdbeCursor)) + 
247e7 0d 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 65  ..      (isBtree
247e8 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74  Cursor?sqlite3Bt
247e9 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a  reeCursorSize():
247ea 30 29 20 2b 20 0d 0a 20 20 20 20 20 20 32 2a 6e  0) + ..      2*n
247eb 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32  Field*sizeof(u32
247ec 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  );....  assert( 
247ed 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  iCur<p->nCursor 
247ee 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 61 70 43  );..  if( p->apC
247ef 73 72 5b 69 43 75 72 5d 20 29 7b 0d 0a 20 20 20  sr[iCur] ){..   
247f0 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
247f1 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43  Cursor(p, p->apC
247f2 73 72 5b 69 43 75 72 5d 29 3b 0d 0a 20 20 20 20  sr[iCur]);..    
247f3 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d  p->apCsr[iCur] =
247f4 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   0;..  }..  if( 
247f5 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74  SQLITE_OK==sqlit
247f6 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
247f7 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b  em, nByte, 0) ){
247f8 0d 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69  ..    p->apCsr[i
247f9 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 56 64  Cur] = pCx = (Vd
247fa 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d 2d 3e  beCursor*)pMem->
247fb 7a 3b 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  z;..    memset(p
247fc 43 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 56 64  Cx, 0, sizeof(Vd
247fd 62 65 43 75 72 73 6f 72 29 29 3b 0d 0a 20 20 20  beCursor));..   
247fe 20 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b   pCx->iDb = iDb;
247ff 0d 0a 20 20 20 20 70 43 78 2d 3e 6e 46 69 65 6c  ..    pCx->nFiel
24800 64 20 3d 20 6e 46 69 65 6c 64 3b 0d 0a 20 20 20  d = nField;..   
24801 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b 0d 0a   if( nField ){..
24802 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70 65        pCx->aType
24803 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d 2d   = (u32 *)&pMem-
24804 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  >z[ROUND8(sizeof
24805 28 56 64 62 65 43 75 72 73 6f 72 29 29 5d 3b 0d  (VdbeCursor))];.
24806 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
24807 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b  isBtreeCursor ){
24808 0d 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75  ..      pCx->pCu
24809 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72  rsor = (BtCursor
2480a 2a 29 0d 0a 20 20 20 20 20 20 20 20 20 20 26 70  *)..          &p
2480b 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69  Mem->z[ROUND8(si
2480c 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29  zeof(VdbeCursor)
2480d 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f  )+2*nField*sizeo
2480e 66 28 75 33 32 29 5d 3b 0d 0a 20 20 20 20 20 20  f(u32)];..      
2480f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
24810 6f 72 5a 65 72 6f 28 70 43 78 2d 3e 70 43 75 72  orZero(pCx->pCur
24811 73 6f 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  sor);..    }..  
24812 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b  }..  return pCx;
24813 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 72  ..}..../*..** Tr
24814 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76  y to convert a v
24815 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65  alue into a nume
24816 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 69  ric representati
24817 6f 6e 20 69 66 20 77 65 20 63 61 6e 0d 0a 2a 2a  on if we can..**
24818 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 6c   do so without l
24819 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69  oss of informati
2481a 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  on.  In other wo
2481b 72 64 73 2c 20 69 66 20 74 68 65 20 73 74 72 69  rds, if the stri
2481c 6e 67 0d 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b  ng..** looks lik
2481d 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76  e a number, conv
2481e 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e 75  ert it into a nu
2481f 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f 65  mber.  If it doe
24820 73 20 6e 6f 74 0d 0a 2a 2a 20 6c 6f 6f 6b 20 6c  s not..** look l
24821 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65  ike a number, le
24822 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0d 0a 2a  ave it alone...*
24823 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  /..static void a
24824 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
24825 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0d  ity(Mem *pRec){.
24826 0a 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c  .  if( (pRec->fl
24827 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c  ags & (MEM_Real|
24828 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0d  MEM_Int))==0 ){.
24829 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c  .    double rVal
2482a 75 65 3b 0d 0a 20 20 20 20 69 36 34 20 69 56 61  ue;..    i64 iVa
2482b 6c 75 65 3b 0d 0a 20 20 20 20 75 38 20 65 6e 63  lue;..    u8 enc
2482c 20 3d 20 70 52 65 63 2d 3e 65 6e 63 3b 0d 0a 20   = pRec->enc;.. 
2482d 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c     if( (pRec->fl
2482e 61 67 73 26 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  ags&MEM_Str)==0 
2482f 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 69  ) return;..    i
24830 66 28 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70  f( sqlite3AtoF(p
24831 52 65 63 2d 3e 7a 2c 20 26 72 56 61 6c 75 65 2c  Rec->z, &rValue,
24832 20 70 52 65 63 2d 3e 6e 2c 20 65 6e 63 29 3d 3d   pRec->n, enc)==
24833 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20  0 ) return;..   
24834 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 41   if( 0==sqlite3A
24835 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26  toi64(pRec->z, &
24836 69 56 61 6c 75 65 2c 20 70 52 65 63 2d 3e 6e 2c  iValue, pRec->n,
24837 20 65 6e 63 29 20 29 7b 0d 0a 20 20 20 20 20 20   enc) ){..      
24838 70 52 65 63 2d 3e 75 2e 69 20 3d 20 69 56 61 6c  pRec->u.i = iVal
24839 75 65 3b 0d 0a 20 20 20 20 20 20 70 52 65 63 2d  ue;..      pRec-
2483a 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
2483b 74 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  t;..    }else{..
2483c 20 20 20 20 20 20 70 52 65 63 2d 3e 72 20 3d 20        pRec->r = 
2483d 72 56 61 6c 75 65 3b 0d 0a 20 20 20 20 20 20 70  rValue;..      p
2483e 52 65 63 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Rec->flags |= ME
2483f 4d 5f 52 65 61 6c 3b 0d 0a 20 20 20 20 7d 0d 0a  M_Real;..    }..
24840 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
24841 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64   Processing is d
24842 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20  etermine by the 
24843 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74  affinity paramet
24844 65 72 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49  er:..**..** SQLI
24845 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0d  TE_AFF_INTEGER:.
24846 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  .** SQLITE_AFF_R
24847 45 41 4c 3a 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f  EAL:..** SQLITE_
24848 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0d 0a 2a 2a  AFF_NUMERIC:..**
24849 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65      Try to conve
2484a 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e  rt pRec to an in
2484b 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
2484c 74 69 6f 6e 20 6f 72 20 61 20 0d 0a 2a 2a 20 20  tion or a ..**  
2484d 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74    floating-point
2484e 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
2484f 69 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65  if an integer re
24850 70 72 65 73 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a  presentation..**
24851 20 20 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69      is not possi
24852 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ble.  Note that 
24853 74 68 65 20 69 6e 74 65 67 65 72 20 72 65 70 72  the integer repr
24854 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0d 0a 2a  esentation is..*
24855 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72 65 66  *    always pref
24856 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66 20 74  erred, even if t
24857 68 65 20 61 66 66 69 6e 69 74 79 20 69 73 20 52  he affinity is R
24858 45 41 4c 2c 20 62 65 63 61 75 73 65 0d 0a 2a 2a  EAL, because..**
24859 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 72      an integer r
2485a 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
2485b 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66 69   more space effi
2485c 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0d 0a  cient on disk...
2485d 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  **..** SQLITE_AF
2485e 46 5f 54 45 58 54 3a 0d 0a 2a 2a 20 20 20 20 43  F_TEXT:..**    C
2485f 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61  onvert pRec to a
24860 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61   text representa
24861 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51  tion...**..** SQ
24862 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0d 0a  LITE_AFF_NONE:..
24863 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52  **    No-op.  pR
24864 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  ec is unchanged.
24865 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
24866 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28  d applyAffinity(
24867 0d 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20  ..  Mem *pRec,  
24868 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
24869 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66  alue to apply af
2486a 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0d 0a 20 20  finity to */..  
2486b 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20  char affinity,  
2486c 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
2486d 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65  ity to be applie
2486e 64 20 2a 2f 0d 0a 20 20 75 38 20 65 6e 63 20 20  d */..  u8 enc  
2486f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
24870 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63  se this text enc
24871 6f 64 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  oding */..){..  
24872 69 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51  if( affinity==SQ
24873 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b  LITE_AFF_TEXT ){
24874 0d 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74  ..    /* Only at
24875 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72  tempt the conver
24876 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20  sion to TEXT if 
24877 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65  there is an inte
24878 67 65 72 20 6f 72 20 72 65 61 6c 0d 0a 20 20 20  ger or real..   
24879 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69   ** representati
2487a 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c  on (blob and NUL
2487b 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e  L do not get con
2487c 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20 73  verted) but no s
2487d 74 72 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 72 65  tring..    ** re
2487e 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0d 0a 20  presentation... 
2487f 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 30     */..    if( 0
24880 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ==(pRec->flags&M
24881 45 4d 5f 53 74 72 29 20 26 26 20 28 70 52 65 63  EM_Str) && (pRec
24882 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61  ->flags&(MEM_Rea
24883 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0d 0a  l|MEM_Int)) ){..
24884 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24885 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 52  eMemStringify(pR
24886 65 63 2c 20 65 6e 63 29 3b 0d 0a 20 20 20 20 7d  ec, enc);..    }
24887 0d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67  ..    pRec->flag
24888 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c  s &= ~(MEM_Real|
24889 4d 45 4d 5f 49 6e 74 29 3b 0d 0a 20 20 7d 65 6c  MEM_Int);..  }el
2488a 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 21  se if( affinity!
2488b 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
2488c 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
2488d 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
2488e 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c  E_AFF_INTEGER ||
2488f 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
24890 45 5f 41 46 46 5f 52 45 41 4c 0d 0a 20 20 20 20  E_AFF_REAL..    
24891 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69           || affi
24892 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
24893 5f 4e 55 4d 45 52 49 43 20 29 3b 0d 0a 20 20 20  _NUMERIC );..   
24894 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
24895 69 6e 69 74 79 28 70 52 65 63 29 3b 0d 0a 20 20  inity(pRec);..  
24896 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67    if( pRec->flag
24897 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0d  s & MEM_Real ){.
24898 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24899 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74  beIntegerAffinit
2489a 79 28 70 52 65 63 29 3b 0d 0a 20 20 20 20 7d 0d  y(pRec);..    }.
2489b 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
2489c 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  * Try to convert
2489d 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66   the type of a f
2489e 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
2489f 20 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c   or a result col
248a0 75 6d 6e 0d 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e  umn..** into a n
248a1 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74  umeric represent
248a2 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68  ation.  Use eith
248a3 65 72 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45  er INTEGER or RE
248a4 41 4c 20 77 68 69 63 68 65 76 65 72 0d 0a 2a 2a  AL whichever..**
248a5 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e   is appropriate.
248a6 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68    But only do th
248a7 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20  e conversion if 
248a8 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77  it is possible w
248a9 69 74 68 6f 75 74 0d 0a 2a 2a 20 6c 6f 73 73 20  ithout..** loss 
248aa 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  of information a
248ab 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72 65  nd return the re
248ac 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74 68  vised type of th
248ad 65 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d  e argument...*/.
248ae 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
248af 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
248b0 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
248b1 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
248b2 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20  ..  Mem *pMem = 
248b3 28 4d 65 6d 2a 29 70 56 61 6c 3b 0d 0a 20 20 69  (Mem*)pVal;..  i
248b4 66 28 20 70 4d 65 6d 2d 3e 74 79 70 65 3d 3d 53  f( pMem->type==S
248b5 51 4c 49 54 45 5f 54 45 58 54 20 29 7b 0d 0a 20  QLITE_TEXT ){.. 
248b6 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41     applyNumericA
248b7 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0d 0a  ffinity(pMem);..
248b8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
248b9 65 6d 53 74 6f 72 65 54 79 70 65 28 70 4d 65 6d  emStoreType(pMem
248ba 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  );..  }..  retur
248bb 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0d 0a 7d  n pMem->type;..}
248bc 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 70 6f 72  ..../*..** Expor
248bd 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61  ted version of a
248be 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20  pplyAffinity(). 
248bf 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f  This one works o
248c0 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  n sqlite3_value*
248c1 2c 20 0d 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69  , ..** not the i
248c2 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70  nternal Mem* typ
248c3 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
248c4 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
248c5 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
248c6 69 6e 69 74 79 28 0d 0a 20 20 73 71 6c 69 74 65  inity(..  sqlite
248c7 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0d  3_value *pVal, .
248c8 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20  .  u8 affinity, 
248c9 0d 0a 20 20 75 38 20 65 6e 63 0d 0a 29 7b 0d 0a  ..  u8 enc..){..
248ca 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
248cb 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66  (Mem *)pVal, aff
248cc 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0d 0a 7d 0d  inity, enc);..}.
248cd 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
248ce 5f 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 57  _DEBUG../*..** W
248cf 72 69 74 65 20 61 20 6e 69 63 65 20 73 74 72 69  rite a nice stri
248d0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
248d1 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  n of the content
248d2 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0d 0a  s of cell pMem..
248d3 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a  ** into buffer z
248d4 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66  Buf, length nBuf
248d5 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
248d6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
248d7 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
248d8 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  rint(Mem *pMem, 
248d9 63 68 61 72 20 2a 7a 42 75 66 29 7b 0d 0a 20 20  char *zBuf){..  
248da 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75  char *zCsr = zBu
248db 66 3b 0d 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d  f;..  int f = pM
248dc 65 6d 2d 3e 66 6c 61 67 73 3b 0d 0a 0d 0a 20 20  em->flags;....  
248dd 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
248de 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65  r *const encname
248df 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28  s[] = {"(X)", "(
248e0 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22  8)", "(16LE)", "
248e1 28 31 36 42 45 29 22 7d 3b 0d 0a 0d 0a 20 20 69  (16BE)"};....  i
248e2 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b  f( f&MEM_Blob ){
248e3 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ..    int i;..  
248e4 20 20 63 68 61 72 20 63 3b 0d 0a 20 20 20 20 69    char c;..    i
248e5 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
248e6 7b 0d 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27  {..      c = 'z'
248e7 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
248e8 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69   (f & (MEM_Stati
248e9 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  c|MEM_Ephem))==0
248ea 20 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69   );..    }else i
248eb 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69  f( f & MEM_Stati
248ec 63 20 29 7b 0d 0a 20 20 20 20 20 20 63 20 3d 20  c ){..      c = 
248ed 27 74 27 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  't';..      asse
248ee 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79  rt( (f & (MEM_Dy
248ef 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  n|MEM_Ephem))==0
248f0 20 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69   );..    }else i
248f1 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d  f( f & MEM_Ephem
248f2 20 29 7b 0d 0a 20 20 20 20 20 20 63 20 3d 20 27   ){..      c = '
248f3 65 27 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  e';..      asser
248f4 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61  t( (f & (MEM_Sta
248f5 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30  tic|MEM_Dyn))==0
248f6 20 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   );..    }else{.
248f7 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0d  .      c = 's';.
248f8 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71  .    }....    sq
248f9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
248fa 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20  00, zCsr, "%c", 
248fb 63 29 3b 0d 0a 20 20 20 20 7a 43 73 72 20 2b 3d  c);..    zCsr +=
248fc 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
248fd 28 7a 43 73 72 29 3b 0d 0a 20 20 20 20 73 71 6c  (zCsr);..    sql
248fe 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
248ff 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20  0, zCsr, "%d[", 
24900 70 4d 65 6d 2d 3e 6e 29 3b 0d 0a 20 20 20 20 7a  pMem->n);..    z
24901 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
24902 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0d 0a 20  rlen30(zCsr);.. 
24903 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36     for(i=0; i<16
24904 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69   && i<pMem->n; i
24905 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ++){..      sqli
24906 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
24907 2c 20 7a 43 73 72 2c 20 22 25 30 32 58 22 2c 20  , zCsr, "%02X", 
24908 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d  ((int)pMem->z[i]
24909 20 26 20 30 78 46 46 29 29 3b 0d 0a 20 20 20 20   & 0xFF));..    
2490a 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
2490b 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b  3Strlen30(zCsr);
2490c 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72  ..    }..    for
2490d 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c  (i=0; i<16 && i<
2490e 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0d 0a  pMem->n; i++){..
2490f 20 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70        char z = p
24910 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0d 0a 20 20 20 20  Mem->z[i];..    
24911 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e    if( z<32 || z>
24912 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20  126 ) *zCsr++ = 
24913 27 2e 27 3b 0d 0a 20 20 20 20 20 20 65 6c 73 65  '.';..      else
24914 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0d 0a 20   *zCsr++ = z;.. 
24915 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71 6c 69     }....    sqli
24916 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
24917 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65  , zCsr, "]%s", e
24918 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e  ncnames[pMem->en
24919 63 5d 29 3b 0d 0a 20 20 20 20 7a 43 73 72 20 2b  c]);..    zCsr +
2491a 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2491b 30 28 7a 43 73 72 29 3b 0d 0a 20 20 20 20 69 66  0(zCsr);..    if
2491c 28 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ( f & MEM_Zero )
2491d 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2491e 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a  _snprintf(100, z
2491f 43 73 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d  Csr,"+%dz",pMem-
24920 3e 75 2e 6e 5a 65 72 6f 29 3b 0d 0a 20 20 20 20  >u.nZero);..    
24921 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
24922 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b  3Strlen30(zCsr);
24923 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 2a 7a 43  ..    }..    *zC
24924 73 72 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 7d 65  sr = '\0';..  }e
24925 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f  lse if( f & MEM_
24926 53 74 72 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  Str ){..    int 
24927 6a 2c 20 6b 3b 0d 0a 20 20 20 20 7a 42 75 66 5b  j, k;..    zBuf[
24928 30 5d 20 3d 20 27 20 27 3b 0d 0a 20 20 20 20 69  0] = ' ';..    i
24929 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
2492a 7b 0d 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d  {..      zBuf[1]
2492b 20 3d 20 27 7a 27 3b 0d 0a 20 20 20 20 20 20 61   = 'z';..      a
2492c 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
2492d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65  _Static|MEM_Ephe
2492e 6d 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 7d  m))==0 );..    }
2492f 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
24930 5f 53 74 61 74 69 63 20 29 7b 0d 0a 20 20 20 20  _Static ){..    
24931 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b    zBuf[1] = 't';
24932 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
24933 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45  (f & (MEM_Dyn|ME
24934 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0d  M_Ephem))==0 );.
24935 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66  .    }else if( f
24936 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0d   & MEM_Ephem ){.
24937 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d  .      zBuf[1] =
24938 20 27 65 27 3b 0d 0a 20 20 20 20 20 20 61 73 73   'e';..      ass
24939 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53  ert( (f & (MEM_S
2493a 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d  tatic|MEM_Dyn))=
2493b 3d 30 20 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  =0 );..    }else
2493c 7b 0d 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d  {..      zBuf[1]
2493d 20 3d 20 27 73 27 3b 0d 0a 20 20 20 20 7d 0d 0a   = 's';..    }..
2493e 20 20 20 20 6b 20 3d 20 32 3b 0d 0a 20 20 20 20      k = 2;..    
2493f 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
24940 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20  (100, &zBuf[k], 
24941 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0d  "%d", pMem->n);.
24942 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65  .    k += sqlite
24943 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b  3Strlen30(&zBuf[
24944 6b 5d 29 3b 0d 0a 20 20 20 20 7a 42 75 66 5b 6b  k]);..    zBuf[k
24945 2b 2b 5d 20 3d 20 27 5b 27 3b 0d 0a 20 20 20 20  ++] = '[';..    
24946 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26  for(j=0; j<15 &&
24947 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29   j<pMem->n; j++)
24948 7b 0d 0a 20 20 20 20 20 20 75 38 20 63 20 3d 20  {..      u8 c = 
24949 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0d 0a 20 20 20  pMem->z[j];..   
2494a 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26     if( c>=0x20 &
2494b 26 20 63 3c 30 78 37 66 20 29 7b 0d 0a 20 20 20  & c<0x7f ){..   
2494c 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d       zBuf[k++] =
2494d 20 63 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65   c;..      }else
2494e 7b 0d 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b  {..        zBuf[
2494f 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0d 0a 20 20 20  k++] = '.';..   
24950 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
24951 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27   zBuf[k++] = ']'
24952 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ;..    sqlite3_s
24953 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75  nprintf(100,&zBu
24954 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70  f[k], encnames[p
24955 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0d 0a 20 20 20  Mem->enc]);..   
24956 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72   k += sqlite3Str
24957 6c 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b  len30(&zBuf[k]);
24958 0d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20  ..    zBuf[k++] 
24959 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65  = 0;..  }..}..#e
2495a 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53  ndif....#ifdef S
2495b 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 2f 2a 0d  QLITE_DEBUG../*.
2495c 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61  .** Print the va
2495d 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 74 65  lue of a registe
2495e 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75  r for tracing pu
2495f 72 70 6f 73 65 73 3a 0d 0a 2a 2f 0d 0a 73 74 61  rposes:..*/..sta
24960 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63  tic void memTrac
24961 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74  ePrint(FILE *out
24962 2c 20 4d 65 6d 20 2a 70 29 7b 0d 0a 20 20 69 66  , Mem *p){..  if
24963 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
24964 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 66 70  _Null ){..    fp
24965 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c  rintf(out, " NUL
24966 4c 22 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  L");..  }else if
24967 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d  ( (p->flags & (M
24968 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29  EM_Int|MEM_Str))
24969 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  ==(MEM_Int|MEM_S
2496a 74 72 29 20 29 7b 0d 0a 20 20 20 20 66 70 72 69  tr) ){..    fpri
2496b 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c  ntf(out, " si:%l
2496c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0d 0a 20  ld", p->u.i);.. 
2496d 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
2496e 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b  ags & MEM_Int ){
2496f 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ..    fprintf(ou
24970 74 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d  t, " i:%lld", p-
24971 3e 75 2e 69 29 3b 0d 0a 23 69 66 6e 64 65 66 20  >u.i);..#ifndef 
24972 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
24973 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 7d 65  TING_POINT..  }e
24974 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  lse if( p->flags
24975 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0d 0a   & MEM_Real ){..
24976 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
24977 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 3b   " r:%g", p->r);
24978 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 65 6c 73  ..#endif..  }els
24979 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26  e if( p->flags &
2497a 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0d 0a   MEM_RowSet ){..
2497b 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
2497c 20 22 20 28 72 6f 77 73 65 74 29 22 29 3b 0d 0a   " (rowset)");..
2497d 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 63 68    }else{..    ch
2497e 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0d 0a 20  ar zBuf[200];.. 
2497f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
24980 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c 20  mPrettyPrint(p, 
24981 7a 42 75 66 29 3b 0d 0a 20 20 20 20 66 70 72 69  zBuf);..    fpri
24982 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b 0d 0a  ntf(out, " ");..
24983 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
24984 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0d 0a 20   "%s", zBuf);.. 
24985 20 7d 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f   }..}..static vo
24986 69 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65  id registerTrace
24987 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20  (FILE *out, int 
24988 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0d 0a  iReg, Mem *p){..
24989 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
2498a 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65  REG[%d] = ", iRe
2498b 67 29 3b 0d 0a 20 20 6d 65 6d 54 72 61 63 65 50  g);..  memTraceP
2498c 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0d 0a 20  rint(out, p);.. 
2498d 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
2498e 6e 22 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  n");..}..#endif.
2498f 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
24990 5f 44 45 42 55 47 0d 0a 23 20 20 64 65 66 69 6e  _DEBUG..#  defin
24991 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45  e REGISTER_TRACE
24992 28 52 2c 4d 29 20 69 66 28 70 2d 3e 74 72 61 63  (R,M) if(p->trac
24993 65 29 72 65 67 69 73 74 65 72 54 72 61 63 65 28  e)registerTrace(
24994 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0d 0a 23  p->trace,R,M)..#
24995 65 6c 73 65 0d 0a 23 20 20 64 65 66 69 6e 65 20  else..#  define 
24996 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 52  REGISTER_TRACE(R
24997 2c 4d 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  ,M)..#endif.....
24998 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
24999 46 49 4c 45 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20  FILE..../* ..** 
2499a 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
2499b 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
2499c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
2499d 65 6d 65 6e 74 69 6e 67 20 0d 0a 2a 2a 20 68 69  ementing ..** hi
2499e 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  gh-performance t
2499f 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0d  iming routines..
249a0 0a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/../**********
249a1 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74  **** Include hwt
249a2 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ime.h in the mid
249a3 64 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a  dle of vdbe.c **
249a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249a5 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
249a6 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
249a7 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
249a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249aa 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
249ab 30 38 20 4d 61 79 20 32 37 0d 0a 2a 2a 0d 0a 2a  08 May 27..**..*
249ac 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
249ad 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
249ae 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
249af 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
249b0 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
249b1 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
249b2 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
249b3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
249b4 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
249b5 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
249b6 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
249b7 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
249b8 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
249b9 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
249ba 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
249bb 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
249bc 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
249bd 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
249be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
249c2 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ******..**..** T
249c3 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
249c4 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64  s inline asm cod
249c5 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67  e for retrieving
249c6 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e   "high-performan
249c7 63 65 22 0d 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  ce"..** counters
249c8 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
249c9 50 55 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  PUs...*/..#ifnde
249ca 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0d 0a 23 64  f _HWTIME_H_..#d
249cb 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f  efine _HWTIME_H_
249cc 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66  ..../*..** The f
249cd 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
249ce 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70   only works on p
249cf 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72  entium-class (or
249d0 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f   newer) processo
249d1 72 73 2e 0d 0a 2a 2a 20 49 74 20 75 73 65 73 20  rs...** It uses 
249d2 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65  the RDTSC opcode
249d3 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63   to read the cyc
249d4 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f  le count value o
249d5 75 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 70 72  ut of the..** pr
249d6 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
249d7 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
249d8 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
249d9 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0d 0a  d for high-res..
249da 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0d 0a 2a  ** profiling...*
249db 2f 0d 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28  /..#if (defined(
249dc 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66  __GNUC__) || def
249dd 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20  ined(_MSC_VER)) 
249de 26 26 20 5c 0d 0a 20 20 20 20 20 20 28 64 65 66  && \..      (def
249df 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
249e0 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
249e1 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
249e2 38 36 29 29 0d 0a 0d 0a 20 20 23 69 66 20 64 65  86))....  #if de
249e3 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0d  fined(__GNUC__).
249e4 0a 0d 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  ...  __inline__ 
249e5 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
249e6 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
249e7 29 7b 0d 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  ){..     unsigne
249e8 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0d 0a 20  d int lo, hi;.. 
249e9 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
249ea 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63  latile__ ("rdtsc
249eb 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22  " : "=a" (lo), "
249ec 3d 64 22 20 28 68 69 29 29 3b 0d 0a 20 20 20 20  =d" (hi));..    
249ed 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f   return (sqlite_
249ee 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20  uint64)hi << 32 
249ef 7c 20 6c 6f 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  | lo;..  }....  
249f0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
249f1 53 43 5f 56 45 52 29 0d 0a 0d 0a 20 20 5f 5f 64  SC_VER)....  __d
249f2 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
249f3 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
249f4 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
249f5 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
249f6 29 7b 0d 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b  ){..     __asm {
249f7 0d 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0d  ..        rdtsc.
249f8 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20  .        ret    
249f9 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75     ; return valu
249fa 65 20 61 74 20 45 44 58 3a 45 41 58 0d 0a 20 20  e at EDX:EAX..  
249fb 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 23     }..  }....  #
249fc 65 6e 64 69 66 0d 0a 0d 0a 23 65 6c 69 66 20 28  endif....#elif (
249fd 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
249fe 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
249ff 38 36 5f 36 34 5f 5f 29 29 0d 0a 0d 0a 20 20 5f  86_64__))....  _
24a00 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
24a01 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
24a02 77 74 69 6d 65 28 76 6f 69 64 29 7b 0d 0a 20 20  wtime(void){..  
24a03 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
24a04 67 20 76 61 6c 3b 0d 0a 20 20 20 20 20 20 5f 5f  g val;..      __
24a05 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
24a06 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
24a07 41 22 20 28 76 61 6c 29 29 3b 0d 0a 20 20 20 20  A" (val));..    
24a08 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0d 0a 20    return val;.. 
24a09 20 7d 0d 0a 20 0d 0a 23 65 6c 69 66 20 28 64 65   }.. ..#elif (de
24a0a 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
24a0b 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63  && defined(__ppc
24a0c 5f 5f 29 29 0d 0a 0d 0a 20 20 5f 5f 69 6e 6c 69  __))....  __inli
24a0d 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
24a0e 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
24a0f 28 76 6f 69 64 29 7b 0d 0a 20 20 20 20 20 20 75  (void){..      u
24a10 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
24a11 67 20 72 65 74 76 61 6c 3b 0d 0a 20 20 20 20 20  g retval;..     
24a12 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a   unsigned long j
24a13 75 6e 6b 3b 0d 0a 20 20 20 20 20 20 5f 5f 61 73  unk;..      __as
24a14 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
24a15 20 28 22 5c 6e 5c 0d 0a 20 20 20 20 20 20 20 20   ("\n\..        
24a16 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20    1:      mftbu 
24a17 20 20 25 31 5c 6e 5c 0d 0a 20 20 20 20 20 20 20    %1\n\..       
24a18 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20             mftb 
24a19 20 20 20 25 4c 30 5c 6e 5c 0d 0a 20 20 20 20 20     %L0\n\..     
24a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
24a1b 62 75 20 20 20 25 30 5c 6e 5c 0d 0a 20 20 20 20  bu   %0\n\..    
24a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d                cm
24a1d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0d 0a  pw    %0,%1\n\..
24a1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a1f 20 20 62 6e 65 20 20 20 20 20 31 62 22 0d 0a 20    bne     1b".. 
24a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a21 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29   : "=r" (retval)
24a22 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0d  , "=r" (junk));.
24a23 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
24a24 74 76 61 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 65  tval;..  }....#e
24a25 6c 73 65 0d 0a 0d 0a 20 20 23 65 72 72 6f 72 20  lse....  #error 
24a26 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
24a27 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
24a28 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
24a29 70 6c 61 74 66 6f 72 6d 2e 0d 0a 0d 0a 20 20 2f  platform.....  /
24a2a 2a 0d 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69  *..  ** To compi
24a2b 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65  le without imple
24a2c 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48  menting sqlite3H
24a2d 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
24a2e 20 70 6c 61 74 66 6f 72 6d 2c 0d 0a 20 20 2a 2a   platform,..  **
24a2f 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20   you can remove 
24a30 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72  the above #error
24a31 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c   and use the fol
24a32 6c 6f 77 69 6e 67 0d 0a 20 20 2a 2a 20 73 74 75  lowing..  ** stu
24a33 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75  b function.  You
24a34 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e   will lose timin
24a35 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61  g support for ma
24a36 6e 79 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ny..  ** of the 
24a37 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65  debugging and te
24a38 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c  sting utilities,
24a39 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61   but it should a
24a3a 74 0d 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  t..  ** least co
24a3b 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0d 0a  mpile and run...
24a3c 20 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49    */..SQLITE_PRI
24a3d 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69  VATE   sqlite_ui
24a3e 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
24a3f 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  me(void){ return
24a40 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
24a41 29 30 29 3b 20 7d 0d 0a 0d 0a 23 65 6e 64 69 66  )0); }....#endif
24a42 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ....#endif /* !d
24a43 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48  efined(_HWTIME_H
24a44 5f 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  _) */..../******
24a45 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
24a46 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
24a47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24a48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24a49 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
24a4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
24a4b 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
24a4c 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63  ft off in vdbe.c
24a4d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
24a4e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 23 65 6e  ********/....#en
24a4f 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
24a50 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45  e CHECK_FOR_INTE
24a51 52 52 55 50 54 20 6d 61 63 72 6f 20 64 65 66 69  RRUPT macro defi
24a52 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74  ned here looks t
24a53 6f 20 73 65 65 20 69 66 20 74 68 65 0d 0a 2a 2a  o see if the..**
24a54 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
24a55 70 74 28 29 20 72 6f 75 74 69 6e 65 20 68 61 73  pt() routine has
24a56 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20 20 49   been called.  I
24a57 66 20 69 74 20 68 61 73 20 62 65 65 6e 2c 20 74  f it has been, t
24a58 68 65 6e 0d 0a 2a 2a 20 70 72 6f 63 65 73 73 69  hen..** processi
24a59 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45 20 70  ng of the VDBE p
24a5a 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65 72 72  rogram is interr
24a5b 75 70 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  upted...**..** T
24a5c 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20  his macro added 
24a5d 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75 63  to every instruc
24a5e 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61  tion that does a
24a5f 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74   jump in order t
24a60 6f 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  o..** implement 
24a61 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65  a loop.  This te
24a62 73 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e  st used to be on
24a63 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e   every single in
24a64 73 74 72 75 63 74 69 6f 6e 2c 0d 0a 2a 2a 20 62  struction,..** b
24a65 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65  ut that meant we
24a66 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68   more testing th
24a67 61 6e 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42  an we needed.  B
24a68 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74  y only testing t
24a69 68 65 0d 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a  he..** flag on j
24a6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
24a6b 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c  , we get a (smal
24a6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65  l) speed improve
24a6d 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ment...*/..#defi
24a6e 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54  ne CHECK_FOR_INT
24a6f 45 52 52 55 50 54 20 5c 0d 0a 20 20 20 69 66 28  ERRUPT \..   if(
24a70 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
24a71 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f  upted ) goto abo
24a72 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72  rt_due_to_interr
24a73 75 70 74 3b 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65  upt;......#ifnde
24a74 66 20 4e 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a  f NDEBUG../*..**
24a75 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
24a76 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
24a77 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 61 73 73  om within an ass
24a78 65 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e  ert() expression
24a79 2e 20 49 74 0d 0a 2a 2a 20 63 68 65 63 6b 73 20  . It..** checks 
24a7a 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33  that the sqlite3
24a7b 2e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 76 61  .nTransaction va
24a7c 72 69 61 62 6c 65 20 69 73 20 63 6f 72 72 65 63  riable is correc
24a7d 74 6c 79 20 73 65 74 20 74 6f 0d 0a 2a 2a 20 74  tly set to..** t
24a7e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  he number of non
24a7f 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76  -transaction sav
24a80 65 70 6f 69 6e 74 73 20 63 75 72 72 65 6e 74 6c  epoints currentl
24a81 79 20 69 6e 20 74 68 65 20 0d 0a 2a 2a 20 6c 69  y in the ..** li
24a82 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72 74 69  nked list starti
24a83 6e 67 20 61 74 20 73 71 6c 69 74 65 33 2e 70 53  ng at sqlite3.pS
24a84 61 76 65 70 6f 69 6e 74 2e 0d 0a 2a 2a 20 0d 0a  avepoint...** ..
24a85 2a 2a 20 55 73 61 67 65 3a 0d 0a 2a 2a 0d 0a 2a  ** Usage:..**..*
24a86 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68  *     assert( ch
24a87 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e  eckSavepointCoun
24a88 74 28 64 62 29 20 29 3b 0d 0a 2a 2f 0d 0a 73 74  t(db) );..*/..st
24a89 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61  atic int checkSa
24a8a 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c  vepointCount(sql
24a8b 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 69 6e  ite3 *db){..  in
24a8c 74 20 6e 20 3d 20 30 3b 0d 0a 20 20 53 61 76 65  t n = 0;..  Save
24a8d 70 6f 69 6e 74 20 2a 70 3b 0d 0a 20 20 66 6f 72  point *p;..  for
24a8e 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  (p=db->pSavepoin
24a8f 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  t; p; p=p->pNext
24a90 29 20 6e 2b 2b 3b 0d 0a 20 20 61 73 73 65 72 74  ) n++;..  assert
24a91 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70  ( n==(db->nSavep
24a92 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61  oint + db->isTra
24a93 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
24a94 74 29 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  t) );..  return 
24a95 31 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  1;..}..#endif...
24a96 0a 2f 2a 0d 0a 2a 2a 20 54 72 61 6e 73 66 65 72  ./*..** Transfer
24a97 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
24a98 65 78 74 20 66 72 6f 6d 20 61 6e 20 73 71 6c 69  ext from an sqli
24a99 74 65 33 5f 76 74 61 62 2e 7a 45 72 72 4d 73 67  te3_vtab.zErrMsg
24a9a 20 28 74 65 78 74 20 73 74 6f 72 65 64 0d 0a 2a   (text stored..*
24a9b 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  * in memory obta
24a9c 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
24a9d 33 5f 6d 61 6c 6c 6f 63 29 20 69 6e 74 6f 20 61  3_malloc) into a
24a9e 20 56 64 62 65 2e 7a 45 72 72 4d 73 67 20 28 74   Vdbe.zErrMsg (t
24a9f 65 78 74 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 69  ext stored..** i
24aa0 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
24aa1 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62  d from sqlite3Db
24aa2 4d 61 6c 6c 6f 63 29 2e 0d 0a 2a 2f 0d 0a 73 74  Malloc)...*/..st
24aa3 61 74 69 63 20 76 6f 69 64 20 69 6d 70 6f 72 74  atic void import
24aa4 56 74 61 62 45 72 72 4d 73 67 28 56 64 62 65 20  VtabErrMsg(Vdbe 
24aa5 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  *p, sqlite3_vtab
24aa6 20 2a 70 56 74 61 62 29 7b 0d 0a 20 20 73 71 6c   *pVtab){..  sql
24aa7 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
24aa8 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  ;..  sqlite3DbFr
24aa9 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
24aaa 67 29 3b 0d 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  g);..  p->zErrMs
24aab 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
24aac 44 75 70 28 64 62 2c 20 70 56 74 61 62 2d 3e 7a  Dup(db, pVtab->z
24aad 45 72 72 4d 73 67 29 3b 0d 0a 20 20 73 71 6c 69  ErrMsg);..  sqli
24aae 74 65 33 5f 66 72 65 65 28 70 56 74 61 62 2d 3e  te3_free(pVtab->
24aaf 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 70 56 74  zErrMsg);..  pVt
24ab0 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
24ab1 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
24ab2 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 20  Execute as much 
24ab3 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  of a VDBE progra
24ab4 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e  m as we can then
24ab5 20 72 65 74 75 72 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a   return...**..**
24ab6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
24ab7 52 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20  Ready() must be 
24ab8 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68  called before th
24ab9 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72  is routine in or
24aba 64 65 72 20 74 6f 0d 0a 2a 2a 20 63 6c 6f 73 65  der to..** close
24abb 20 74 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74   the program wit
24abc 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c  h a final OP_Hal
24abd 74 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20  t and to set up 
24abe 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 0d 0a 2a  the callbacks..*
24abf 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20  * and the error 
24ac0 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 72 2e  message pointer.
24ac1 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 65 76 65  ..**..** Wheneve
24ac2 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c  r a row or resul
24ac3 74 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61  t data is availa
24ac4 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ble, this routin
24ac5 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0d 0a 2a  e will either..*
24ac6 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73  * invoke the res
24ac7 75 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66  ult callback (if
24ac8 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f   there is one) o
24ac9 72 20 72 65 74 75 72 6e 20 77 69 74 68 0d 0a 2a  r return with..*
24aca 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e 0d 0a 2a  * SQLITE_ROW...*
24acb 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74 65  *..** If an atte
24acc 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  mpt is made to o
24acd 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61 74  pen a locked dat
24ace 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 69 73  abase, then this
24acf 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 77 69 6c   routine..** wil
24ad0 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 20  l either invoke 
24ad1 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
24ad2 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f  k (if there is o
24ad3 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0d 0a  ne) or it will..
24ad4 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ** return SQLITE
24ad5 5f 42 55 53 59 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  _BUSY...**..** I
24ad6 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
24ad7 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  s, an error mess
24ad8 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74  age is written t
24ad9 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
24ada 64 0d 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  d..** from sqlit
24adb 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
24adc 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61  p->zErrMsg is ma
24add 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
24ade 68 61 74 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 20  hat memory...** 
24adf 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  The error code i
24ae0 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72  s stored in p->r
24ae1 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69  c and this routi
24ae2 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ne returns SQLIT
24ae3 45 5f 45 52 52 4f 52 2e 0d 0a 2a 2a 0d 0a 2a 2a  E_ERROR...**..**
24ae4 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
24ae5 20 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f   ever returns no
24ae6 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
24ae7 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0d 0a   program exits..
24ae8 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20  ** immediately. 
24ae9 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e   There will be n
24aea 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  o error message 
24aeb 62 75 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69  but the p->rc fi
24aec 65 6c 64 20 69 73 0d 0a 2a 2a 20 73 65 74 20 74  eld is..** set t
24aed 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 61  o SQLITE_ABORT a
24aee 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
24aef 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49  will return SQLI
24af0 54 45 5f 45 52 52 4f 52 2e 0d 0a 2a 2a 0d 0a 2a  TE_ERROR...**..*
24af1 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
24af2 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73  ation error caus
24af3 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73  es p->rc to be s
24af4 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d  et to SQLITE_NOM
24af5 45 4d 20 61 6e 64 20 74 68 69 73 0d 0a 2a 2a 20  EM and this..** 
24af6 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72  routine to retur
24af7 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0d  n SQLITE_ERROR..
24af8 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 20 66 61  .**..** Other fa
24af9 74 61 6c 20 65 72 72 6f 72 73 20 72 65 74 75 72  tal errors retur
24afa 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0d  n SQLITE_ERROR..
24afb 0a 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 74 68  .**..** After th
24afc 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66  is routine has f
24afd 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 33  inished, sqlite3
24afe 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 73  VdbeFinalize() s
24aff 68 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20 75 73 65  hould be..** use
24b00 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68  d to clean up th
24b01 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 20  e mess that was 
24b02 6c 65 66 74 20 62 65 68 69 6e 64 2e 0d 0a 2a 2f  left behind...*/
24b03 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
24b04 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
24b05 45 78 65 63 28 0d 0a 20 20 56 64 62 65 20 2a 70  Exec(..  Vdbe *p
24b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b07 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
24b08 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 70 63  */..){..  int pc
24b09 3d 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  =0;             
24b0a 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67       /* The prog
24b0b 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a  ram counter */..
24b0c 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61    Op *aOp = p->a
24b0d 4f 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Op;          /* 
24b0e 43 6f 70 79 20 6f 66 20 70 2d 3e 61 4f 70 20 2a  Copy of p->aOp *
24b0f 2f 0d 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20  /..  Op *pOp;   
24b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b11 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61  /* Current opera
24b12 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  tion */..  int r
24b13 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
24b14 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
24b15 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 20 20 73  o return */..  s
24b16 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
24b17 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  db;       /* The
24b18 20 64 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20 20   database */..  
24b19 75 38 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e  u8 resetSchemaOn
24b1a 46 61 75 6c 74 20 3d 20 30 3b 20 2f 2a 20 52 65  Fault = 0; /* Re
24b1b 73 65 74 20 73 63 68 65 6d 61 20 61 66 74 65 72  set schema after
24b1c 20 61 6e 20 65 72 72 6f 72 20 69 66 20 70 6f 73   an error if pos
24b1d 69 74 69 76 65 20 2a 2f 0d 0a 20 20 75 38 20 65  itive */..  u8 e
24b1e 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 62  ncoding = ENC(db
24b1f 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61  );     /* The da
24b20 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20  tabase encoding 
24b21 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
24b22 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53  TE_OMIT_PROGRESS
24b23 5f 43 41 4c 4c 42 41 43 4b 0d 0a 20 20 69 6e 74  _CALLBACK..  int
24b24 20 63 68 65 63 6b 50 72 6f 67 72 65 73 73 3b 20   checkProgress; 
24b25 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
24b26 69 66 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  if progress call
24b27 62 61 63 6b 73 20 61 72 65 20 65 6e 61 62 6c 65  backs are enable
24b28 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50 72 6f  d */..  int nPro
24b29 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20  gressOps = 0;   
24b2a 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78     /* Opcodes ex
24b2b 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f  ecuted since pro
24b2c 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20  gress callback. 
24b2d 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 4d 65  */..#endif..  Me
24b2e 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65  m *aMem = p->aMe
24b2f 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  m;       /* Copy
24b30 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0d 0a   of p->aMem */..
24b31 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b    Mem *pIn1 = 0;
24b32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24b33 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e  1st input operan
24b34 64 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 49 6e  d */..  Mem *pIn
24b35 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  2 = 0;          
24b36 20 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20     /* 2nd input 
24b37 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 4d 65  operand */..  Me
24b38 6d 20 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20  m *pIn3 = 0;    
24b39 20 20 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20           /* 3rd 
24b3a 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f  input operand */
24b3b 0d 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20  ..  Mem *pOut = 
24b3c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
24b3d 2a 20 4f 75 74 70 75 74 20 6f 70 65 72 61 6e 64  * Output operand
24b3e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6d 70   */..  int iComp
24b3f 61 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  are = 0;        
24b40 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c    /* Result of l
24b41 61 73 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f  ast OP_Compare o
24b42 70 65 72 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69  peration */..  i
24b43 6e 74 20 2a 61 50 65 72 6d 75 74 65 20 3d 20 30  nt *aPermute = 0
24b44 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65 72  ;         /* Per
24b45 6d 75 74 61 74 69 6f 6e 20 6f 66 20 63 6f 6c 75  mutation of colu
24b46 6d 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 61  mns for OP_Compa
24b47 72 65 20 2a 2f 0d 0a 20 20 69 36 34 20 6c 61 73  re */..  i64 las
24b48 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73  tRowid = db->las
24b49 74 52 6f 77 69 64 3b 20 20 2f 2a 20 53 61 76 65  tRowid;  /* Save
24b4a 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c  d value of the l
24b4b 61 73 74 20 69 6e 73 65 72 74 20 52 4f 57 49 44  ast insert ROWID
24b4c 20 2a 2f 0d 0a 23 69 66 64 65 66 20 56 44 42 45   */..#ifdef VDBE
24b4d 5f 50 52 4f 46 49 4c 45 0d 0a 20 20 75 36 34 20  _PROFILE..  u64 
24b4e 73 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20  start;          
24b4f 20 20 20 20 20 20 20 2f 2a 20 43 50 55 20 63 6c         /* CPU cl
24b50 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74 61  ock count at sta
24b51 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0d  rt of opcode */.
24b52 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20  .  int origPc;  
24b53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24b54 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   Program counter
24b55 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63   at start of opc
24b56 6f 64 65 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a  ode */..#endif..
24b57 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    /*************
24b58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24b59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24b5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24b5b 2a 2a 2a 2a 2a 2a 2a 0d 0a 20 20 2a 2a 20 41 75  *******..  ** Au
24b5c 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
24b5d 72 61 74 65 64 20 63 6f 64 65 0d 0a 20 20 2a 2a  rated code..  **
24b5e 0d 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  ** The follo
24b5f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20 61 75  wing union is au
24b60 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
24b61 72 61 74 65 64 20 62 79 20 74 68 65 0d 0a 20 20  rated by the..  
24b62 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 73  ** vdbe-compress
24b63 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54 68  .tcl script.  Th
24b64 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  e purpose of thi
24b65 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0d 0a 20  s union is to.. 
24b66 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61   ** reduce the a
24b67 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73  mount of stack s
24b68 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79  pace required by
24b69 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d   this function..
24b6a 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e  .  ** See commen
24b6b 74 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63  ts in the vdbe-c
24b6c 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69  ompress.tcl scri
24b6d 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0d  pt for details..
24b6e 0a 20 20 2a 2f 0d 0a 20 20 75 6e 69 6f 6e 20 76  .  */..  union v
24b6f 64 62 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0d 0a  dbeExecUnion {..
24b70 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 59 69      struct OP_Yi
24b71 65 6c 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  eld_stack_vars {
24b72 0d 0a 20 20 20 20 20 20 69 6e 74 20 70 63 44 65  ..      int pcDe
24b73 73 74 3b 0d 0a 20 20 20 20 7d 20 61 61 3b 0d 0a  st;..    } aa;..
24b74 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75      struct OP_Nu
24b75 6c 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  ll_stack_vars {.
24b76 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 0d  .      int cnt;.
24b77 0a 20 20 20 20 7d 20 61 62 3b 0d 0a 20 20 20 20  .    } ab;..    
24b78 73 74 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62  struct OP_Variab
24b79 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  le_stack_vars {.
24b7a 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 72  .      Mem *pVar
24b7b 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65  ;       /* Value
24b7c 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72   being transferr
24b7d 65 64 20 2a 2f 0d 0a 20 20 20 20 7d 20 61 63 3b  ed */..    } ac;
24b7e 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24b7f 4d 6f 76 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  Move_stack_vars 
24b80 7b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  {..      char *z
24b81 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c  Malloc;   /* Hol
24b82 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f  ding variable fo
24b83 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  r allocated memo
24b84 72 79 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74  ry */..      int
24b85 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   n;           /*
24b86 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73   Number of regis
24b87 74 65 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70  ters left to cop
24b88 79 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  y */..      int 
24b89 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
24b8a 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
24b8b 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 20 20 20 20   from */..      
24b8c 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20  int p2;         
24b8d 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
24b8e 63 6f 70 79 20 74 6f 20 2a 2f 0d 0a 20 20 20 20  copy to */..    
24b8f 7d 20 61 64 3b 0d 0a 20 20 20 20 73 74 72 75 63  } ad;..    struc
24b90 74 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 5f 73  t OP_ResultRow_s
24b91 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20  tack_vars {..   
24b92 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0d 0a 20     Mem *pMem;.. 
24b93 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20       int i;..   
24b94 20 7d 20 61 65 3b 0d 0a 20 20 20 20 73 74 72 75   } ae;..    stru
24b95 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61  ct OP_Concat_sta
24b96 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20  ck_vars {..     
24b97 20 69 36 34 20 6e 42 79 74 65 3b 0d 0a 20 20 20   i64 nByte;..   
24b98 20 7d 20 61 66 3b 0d 0a 20 20 20 20 73 74 72 75   } af;..    stru
24b99 63 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f  ct OP_Remainder_
24b9a 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20  stack_vars {..  
24b9b 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20      int flags;  
24b9c 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20      /* Combined 
24b9d 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d  MEM_* flags from
24b9e 20 62 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0d   both inputs */.
24b9f 0a 20 20 20 20 20 20 69 36 34 20 69 41 3b 20 20  .      i64 iA;  
24ba0 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
24ba1 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20  r value of left 
24ba2 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 20 20  operand */..    
24ba3 20 20 69 36 34 20 69 42 3b 20 20 20 20 20 20 20    i64 iB;       
24ba4 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c    /* Integer val
24ba5 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72  ue of right oper
24ba6 61 6e 64 20 2a 2f 0d 0a 20 20 20 20 20 20 64 6f  and */..      do
24ba7 75 62 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a  uble rA;      /*
24ba8 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c   Real value of l
24ba9 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a  eft operand */..
24baa 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 42 3b        double rB;
24bab 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61        /* Real va
24bac 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65  lue of right ope
24bad 72 61 6e 64 20 2a 2f 0d 0a 20 20 20 20 7d 20 61  rand */..    } a
24bae 67 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f  g;..    struct O
24baf 50 5f 46 75 6e 63 74 69 6f 6e 5f 73 74 61 63 6b  P_Function_stack
24bb0 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 69  _vars {..      i
24bb1 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20 4d 65 6d  nt i;..      Mem
24bb2 20 2a 70 41 72 67 3b 0d 0a 20 20 20 20 20 20 73   *pArg;..      s
24bb3 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63  qlite3_context c
24bb4 74 78 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  tx;..      sqlit
24bb5 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
24bb6 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0d  ;..      int n;.
24bb7 0a 20 20 20 20 7d 20 61 68 3b 0d 0a 20 20 20 20  .    } ah;..    
24bb8 73 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74 52  struct OP_ShiftR
24bb9 69 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ight_stack_vars 
24bba 7b 0d 0a 20 20 20 20 20 20 69 36 34 20 69 41 3b  {..      i64 iA;
24bbb 0d 0a 20 20 20 20 20 20 75 36 34 20 75 41 3b 0d  ..      u64 uA;.
24bbc 0a 20 20 20 20 20 20 69 36 34 20 69 42 3b 0d 0a  .      i64 iB;..
24bbd 20 20 20 20 20 20 75 38 20 6f 70 3b 0d 0a 20 20        u8 op;..  
24bbe 20 20 7d 20 61 69 3b 0d 0a 20 20 20 20 73 74 72    } ai;..    str
24bbf 75 63 74 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f  uct OP_Ge_stack_
24bc0 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e  vars {..      in
24bc1 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20  t res;          
24bc2 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 74    /* Result of t
24bc3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66  he comparison of
24bc4 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20 70 49   pIn1 against pI
24bc5 6e 33 20 2a 2f 0d 0a 20 20 20 20 20 20 63 68 61  n3 */..      cha
24bc6 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20  r affinity;     
24bc7 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20   /* Affinity to 
24bc8 75 73 65 20 66 6f 72 20 63 6f 6d 70 61 72 69 73  use for comparis
24bc9 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 75 31 36  on */..      u16
24bca 20 66 6c 61 67 73 31 3b 20 20 20 20 20 20 20 20   flags1;        
24bcb 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 6e 69 74   /* Copy of init
24bcc 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 70 49 6e  ial value of pIn
24bcd 31 2d 3e 66 6c 61 67 73 20 2a 2f 0d 0a 20 20 20  1->flags */..   
24bce 20 20 20 75 31 36 20 66 6c 61 67 73 33 3b 20 20     u16 flags3;  
24bcf 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
24bd0 66 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  f initial value 
24bd1 6f 66 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 2a  of pIn3->flags *
24bd2 2f 0d 0a 20 20 20 20 7d 20 61 6a 3b 0d 0a 20 20  /..    } aj;..  
24bd3 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 6d 70    struct OP_Comp
24bd4 61 72 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  are_stack_vars {
24bd5 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0d 0a  ..      int n;..
24bd6 20 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20        int i;..  
24bd7 20 20 20 20 69 6e 74 20 70 31 3b 0d 0a 20 20 20      int p1;..   
24bd8 20 20 20 69 6e 74 20 70 32 3b 0d 0a 20 20 20 20     int p2;..    
24bd9 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f 20    const KeyInfo 
24bda 2a 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 20 20  *pKeyInfo;..    
24bdb 20 20 69 6e 74 20 69 64 78 3b 0d 0a 20 20 20 20    int idx;..    
24bdc 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
24bdd 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
24bde 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73  g sequence to us
24bdf 65 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a  e on this term *
24be0 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 62 52 65  /..      int bRe
24be1 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  v;          /* T
24be2 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e 44 49  rue for DESCENDI
24be3 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f  NG sort order */
24be4 0d 0a 20 20 20 20 7d 20 61 6b 3b 0d 0a 20 20 20  ..    } ak;..   
24be5 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74   struct OP_Or_st
24be6 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24be7 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20    int v1;    /* 
24be8 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30  Left operand:  0
24be9 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45  ==FALSE, 1==TRUE
24bea 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20  , 2==UNKNOWN or 
24beb 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 69  NULL */..      i
24bec 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
24bed 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
24bee 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
24bef 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
24bf0 4c 20 2a 2f 0d 0a 20 20 20 20 7d 20 61 6c 3b 0d  L */..    } al;.
24bf1 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
24bf2 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  fNot_stack_vars 
24bf3 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0d  {..      int c;.
24bf4 0a 20 20 20 20 7d 20 61 6d 3b 0d 0a 20 20 20 20  .    } am;..    
24bf5 73 74 72 75 63 74 20 4f 50 5f 43 6f 6c 75 6d 6e  struct OP_Column
24bf6 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20  _stack_vars {.. 
24bf7 20 20 20 20 20 75 33 32 20 70 61 79 6c 6f 61 64       u32 payload
24bf8 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
24bf9 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
24bfa 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 20  e record */..   
24bfb 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69     i64 payloadSi
24bfc 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20  ze64; /* Number 
24bfd 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
24bfe 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 20 20 20  record */..     
24bff 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
24c00 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20      /* P1 value 
24c01 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f  of the opcode */
24c02 0d 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20  ..      int p2; 
24c03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f             /* co
24c04 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72  lumn number to r
24c05 65 74 72 69 65 76 65 20 2a 2f 0d 0a 20 20 20 20  etrieve */..    
24c06 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
24c07 3b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45  ;    /* The VDBE
24c08 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 20 20   cursor */..    
24c09 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20    char *zRec;   
24c0a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
24c0b 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f  to complete reco
24c0c 72 64 2d 64 61 74 61 20 2a 2f 0d 0a 20 20 20 20  rd-data */..    
24c0d 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
24c0e 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65  r;   /* The BTre
24c0f 65 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 20  e cursor */..   
24c10 20 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20     u32 *aType;  
24c11 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69        /* aType[i
24c12 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65  ] holds the nume
24c13 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20  ric type of the 
24c14 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a  i-th column */..
24c15 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 66 73        u32 *aOffs
24c16 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66  et;      /* aOff
24c17 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74  set[i] is offset
24c18 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74   to start of dat
24c19 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d  a for i-th colum
24c1a 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  n */..      int 
24c1b 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f  nField;        /
24c1c 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * number of fiel
24c1d 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
24c1e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6c   */..      int l
24c1f 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
24c20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   The length of t
24c21 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
24c22 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ta for the colum
24c23 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  n */..      int 
24c24 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
24c25 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
24c26 2f 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  /..      char *z
24c27 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50  Data;       /* P
24c28 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72  art of the recor
24c29 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20  d being decoded 
24c2a 2a 2f 0d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  */..      Mem *p
24c2b 44 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  Dest;        /* 
24c2c 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74  Where to write t
24c2d 68 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c  he extracted val
24c2e 75 65 20 2a 2f 0d 0a 20 20 20 20 20 20 4d 65 6d  ue */..      Mem
24c2f 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   sMem;          
24c30 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74  /* For storing t
24c31 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20  he record being 
24c32 64 65 63 6f 64 65 64 20 2a 2f 0d 0a 20 20 20 20  decoded */..    
24c33 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20    u8 *zIdx;     
24c34 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
24c35 74 6f 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20  to header */..  
24c36 20 20 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b      u8 *zEndHdr;
24c37 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
24c38 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  r to first byte 
24c39 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72  after the header
24c3a 20 2a 2f 0d 0a 20 20 20 20 20 20 75 33 32 20 6f   */..      u32 o
24c3b 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
24c3c 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65   Offset into the
24c3d 20 64 61 74 61 20 2a 2f 0d 0a 20 20 20 20 20 20   data */..      
24c3e 75 33 32 20 73 7a 46 69 65 6c 64 3b 20 20 20 20  u32 szField;    
24c3f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
24c40 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 6f 6e  bytes in the con
24c41 74 65 6e 74 20 6f 66 20 61 20 66 69 65 6c 64 20  tent of a field 
24c42 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  */..      int sz
24c43 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Hdr;         /* 
24c44 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64  Size of the head
24c45 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74  er size field at
24c46 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64   start of record
24c47 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 61   */..      int a
24c48 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  vail;         /*
24c49 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
24c4a 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61   of available da
24c4b 74 61 20 2a 2f 0d 0a 20 20 20 20 20 20 75 33 32  ta */..      u32
24c4c 20 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   t;             
24c4d 2f 2a 20 41 20 74 79 70 65 20 63 6f 64 65 20 66  /* A type code f
24c4e 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 20 68  rom the record h
24c4f 65 61 64 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20  eader */..      
24c50 4d 65 6d 20 2a 70 52 65 67 3b 20 20 20 20 20 20  Mem *pReg;      
24c51 20 20 20 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c     /* PseudoTabl
24c52 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72  e input register
24c53 20 2a 2f 0d 0a 20 20 20 20 7d 20 61 6e 3b 0d 0a   */..    } an;..
24c54 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 66      struct OP_Af
24c55 66 69 6e 69 74 79 5f 73 74 61 63 6b 5f 76 61 72  finity_stack_var
24c56 73 20 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74  s {..      const
24c57 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79   char *zAffinity
24c58 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e  ;   /* The affin
24c59 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65  ity to be applie
24c5a 64 20 2a 2f 0d 0a 20 20 20 20 20 20 63 68 61 72  d */..      char
24c5b 20 63 41 66 66 3b 20 20 20 20 20 20 20 20 20 20   cAff;          
24c5c 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
24c5d 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 61 66   character of af
24c5e 66 69 6e 69 74 79 20 2a 2f 0d 0a 20 20 20 20 7d  finity */..    }
24c5f 20 61 6f 3b 0d 0a 20 20 20 20 73 74 72 75 63 74   ao;..    struct
24c60 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 5f 73   OP_MakeRecord_s
24c61 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20  tack_vars {..   
24c62 20 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72     u8 *zNewRecor
24c63 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62  d;        /* A b
24c64 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68  uffer to hold th
24c65 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e  e data for the n
24c66 65 77 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20  ew record */..  
24c67 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20      Mem *pRec;  
24c68 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
24c69 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0d  e new record */.
24c6a 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 74 61  .      u64 nData
24c6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
24c6c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
24c6d 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a   of data space *
24c6e 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 64  /..      int nHd
24c6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
24c70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
24c71 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61  es of header spa
24c72 63 65 20 2a 2f 0d 0a 20 20 20 20 20 20 69 36 34  ce */..      i64
24c73 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
24c74 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
24c75 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
24c76 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20  his record */.. 
24c77 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20       int nZero; 
24c78 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
24c79 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79  umber of zero by
24c7a 74 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f  tes at the end o
24c7b 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d  f the record */.
24c7c 0a 20 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69  .      int nVari
24c7d 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nt;           /*
24c7e 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
24c7f 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0d   in a varint */.
24c80 0a 20 20 20 20 20 20 75 33 32 20 73 65 72 69 61  .      u32 seria
24c81 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a  l_type;       /*
24c82 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0d 0a   Type field */..
24c83 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61        Mem *pData
24c84 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
24c85 46 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62  First field to b
24c86 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20  e combined into 
24c87 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20  the record */.. 
24c88 20 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b       Mem *pLast;
24c89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
24c8a 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ast field of the
24c8b 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 20 20   record */..    
24c8c 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20    int nField;   
24c8d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
24c8e 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
24c8f 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20  the record */.. 
24c90 20 20 20 20 20 63 68 61 72 20 2a 7a 41 66 66 69       char *zAffi
24c91 6e 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54  nity;       /* T
24c92 68 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  he affinity stri
24c93 6e 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ng for the recor
24c94 64 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  d */..      int 
24c95 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20  file_format;    
24c96 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61     /* File forma
24c97 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63  t to use for enc
24c98 6f 64 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 20 20  oding */..      
24c99 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
24c9a 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
24c9b 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f  used in zNewReco
24c9c 72 64 5b 5d 20 2a 2f 0d 0a 20 20 20 20 20 20 69  rd[] */..      i
24c9d 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  nt len;         
24c9e 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
24c9f 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0d 0a 20  of a field */.. 
24ca0 20 20 20 7d 20 61 70 3b 0d 0a 20 20 20 20 73 74     } ap;..    st
24ca1 72 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f 73 74  ruct OP_Count_st
24ca2 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24ca3 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0d 0a 20    i64 nEntry;.. 
24ca4 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
24ca5 43 72 73 72 3b 0d 0a 20 20 20 20 7d 20 61 71 3b  Crsr;..    } aq;
24ca6 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24ca7 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f  Savepoint_stack_
24ca8 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e  vars {..      in
24ca9 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20  t p1;           
24caa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24cab 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65   Value of P1 ope
24cac 72 61 6e 64 20 2a 2f 0d 0a 20 20 20 20 20 20 63  rand */..      c
24cad 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
24cae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24caf 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f  * Name of savepo
24cb0 69 6e 74 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e  int */..      in
24cb1 74 20 6e 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20  t nName;..      
24cb2 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b  Savepoint *pNew;
24cb3 0d 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e  ..      Savepoin
24cb4 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0d 0a  t *pSavepoint;..
24cb5 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20        Savepoint 
24cb6 2a 70 54 6d 70 3b 0d 0a 20 20 20 20 20 20 69 6e  *pTmp;..      in
24cb7 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0d 0a 20  t iSavepoint;.. 
24cb8 20 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20       int ii;..  
24cb9 20 20 7d 20 61 72 3b 0d 0a 20 20 20 20 73 74 72    } ar;..    str
24cba 75 63 74 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  uct OP_AutoCommi
24cbb 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a  t_stack_vars {..
24cbc 20 20 20 20 20 20 69 6e 74 20 64 65 73 69 72 65        int desire
24cbd 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0d 0a 20 20  dAutoCommit;..  
24cbe 20 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 63      int iRollbac
24cbf 6b 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 74 75  k;..      int tu
24cc0 72 6e 4f 6e 41 43 3b 0d 0a 20 20 20 20 7d 20 61  rnOnAC;..    } a
24cc1 73 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f  s;..    struct O
24cc2 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 74  P_Transaction_st
24cc3 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24cc4 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0d 0a 20    Btree *pBt;.. 
24cc5 20 20 20 7d 20 61 74 3b 0d 0a 20 20 20 20 73 74     } at;..    st
24cc6 72 75 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b  ruct OP_ReadCook
24cc7 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  ie_stack_vars {.
24cc8 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61  .      int iMeta
24cc9 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  ;..      int iDb
24cca 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  ;..      int iCo
24ccb 6f 6b 69 65 3b 0d 0a 20 20 20 20 7d 20 61 75 3b  okie;..    } au;
24ccc 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24ccd 53 65 74 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f  SetCookie_stack_
24cce 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 44 62  vars {..      Db
24ccf 20 2a 70 44 62 3b 0d 0a 20 20 20 20 7d 20 61 76   *pDb;..    } av
24cd0 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  ;..    struct OP
24cd1 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 5f 73 74  _VerifyCookie_st
24cd2 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24cd3 20 20 69 6e 74 20 69 4d 65 74 61 3b 0d 0a 20 20    int iMeta;..  
24cd4 20 20 20 20 69 6e 74 20 69 47 65 6e 3b 0d 0a 20      int iGen;.. 
24cd5 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b       Btree *pBt;
24cd6 0d 0a 20 20 20 20 7d 20 61 77 3b 0d 0a 20 20 20  ..    } aw;..   
24cd7 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 57   struct OP_OpenW
24cd8 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  rite_stack_vars 
24cd9 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69  {..      int nFi
24cda 65 6c 64 3b 0d 0a 20 20 20 20 20 20 4b 65 79 49  eld;..      KeyI
24cdb 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0d 0a  nfo *pKeyInfo;..
24cdc 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0d 0a 20        int p2;.. 
24cdd 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20       int iDb;.. 
24cde 20 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67 3b       int wrFlag;
24cdf 0d 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ..      Btree *p
24ce0 58 3b 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75  X;..      VdbeCu
24ce1 72 73 6f 72 20 2a 70 43 75 72 3b 0d 0a 20 20 20  rsor *pCur;..   
24ce2 20 20 20 44 62 20 2a 70 44 62 3b 0d 0a 20 20 20     Db *pDb;..   
24ce3 20 7d 20 61 78 3b 0d 0a 20 20 20 20 73 74 72 75   } ax;..    stru
24ce4 63 74 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  ct OP_OpenEpheme
24ce5 72 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ral_stack_vars {
24ce6 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ..      VdbeCurs
24ce7 6f 72 20 2a 70 43 78 3b 0d 0a 20 20 20 20 7d 20  or *pCx;..    } 
24ce8 61 79 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20  ay;..    struct 
24ce9 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e 5f 73 74  OP_SorterOpen_st
24cea 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24ceb 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
24cec 78 3b 0d 0a 20 20 20 20 7d 20 61 7a 3b 0d 0a 20  x;..    } az;.. 
24ced 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65     struct OP_Ope
24cee 6e 50 73 65 75 64 6f 5f 73 74 61 63 6b 5f 76 61  nPseudo_stack_va
24cef 72 73 20 7b 0d 0a 20 20 20 20 20 20 56 64 62 65  rs {..      Vdbe
24cf0 43 75 72 73 6f 72 20 2a 70 43 78 3b 0d 0a 20 20  Cursor *pCx;..  
24cf1 20 20 7d 20 62 61 3b 0d 0a 20 20 20 20 73 74 72    } ba;..    str
24cf2 75 63 74 20 4f 50 5f 53 65 65 6b 47 74 5f 73 74  uct OP_SeekGt_st
24cf3 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24cf4 20 20 69 6e 74 20 72 65 73 3b 0d 0a 20 20 20 20    int res;..    
24cf5 20 20 69 6e 74 20 6f 63 3b 0d 0a 20 20 20 20 20    int oc;..     
24cf6 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
24cf7 0d 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64  ..      Unpacked
24cf8 52 65 63 6f 72 64 20 72 3b 0d 0a 20 20 20 20 20  Record r;..     
24cf9 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0d 0a 20 20   int nField;..  
24cfa 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20      i64 iKey;   
24cfb 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
24cfc 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74  we are to seek t
24cfd 6f 20 2a 2f 0d 0a 20 20 20 20 7d 20 62 62 3b 0d  o */..    } bb;.
24cfe 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53  .    struct OP_S
24cff 65 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  eek_stack_vars {
24d00 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ..      VdbeCurs
24d01 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20 7d 20 62  or *pC;..    } b
24d02 63 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f  c;..    struct O
24d03 50 5f 46 6f 75 6e 64 5f 73 74 61 63 6b 5f 76 61  P_Found_stack_va
24d04 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  rs {..      int 
24d05 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0d 0a  alreadyExists;..
24d06 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
24d07 20 2a 70 43 3b 0d 0a 20 20 20 20 20 20 69 6e 74   *pC;..      int
24d08 20 72 65 73 3b 0d 0a 20 20 20 20 20 20 63 68 61   res;..      cha
24d09 72 20 2a 70 46 72 65 65 3b 0d 0a 20 20 20 20 20  r *pFree;..     
24d0a 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
24d0b 2a 70 49 64 78 4b 65 79 3b 0d 0a 20 20 20 20 20  *pIdxKey;..     
24d0c 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
24d0d 72 3b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 61  r;..      char a
24d0e 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73  TempRec[ROUND8(s
24d0f 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65  izeof(UnpackedRe
24d10 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28  cord)) + sizeof(
24d11 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0d 0a 20 20  Mem)*3 + 7];..  
24d12 20 20 7d 20 62 64 3b 0d 0a 20 20 20 20 73 74 72    } bd;..    str
24d13 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f  uct OP_IsUnique_
24d14 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20  stack_vars {..  
24d15 20 20 20 20 75 31 36 20 69 69 3b 0d 0a 20 20 20      u16 ii;..   
24d16 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
24d17 43 78 3b 0d 0a 20 20 20 20 20 20 42 74 43 75 72  Cx;..      BtCur
24d18 73 6f 72 20 2a 70 43 72 73 72 3b 0d 0a 20 20 20  sor *pCrsr;..   
24d19 20 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0d 0a     u16 nField;..
24d1a 20 20 20 20 20 20 4d 65 6d 20 2a 61 4d 78 3b 0d        Mem *aMx;.
24d1b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52  .      UnpackedR
24d1c 65 63 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20  ecord r;        
24d1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54            /* B-T
24d1e 72 65 65 20 69 6e 64 65 78 20 73 65 61 72 63 68  ree index search
24d1f 20 6b 65 79 20 2a 2f 0d 0a 20 20 20 20 20 20 69   key */..      i
24d20 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20  64 R;           
24d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d22 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65    /* Rowid store
24d23 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
24d24 20 2a 2f 0d 0a 20 20 20 20 7d 20 62 65 3b 0d 0a   */..    } be;..
24d25 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 6f      struct OP_No
24d26 74 45 78 69 73 74 73 5f 73 74 61 63 6b 5f 76 61  tExists_stack_va
24d27 72 73 20 7b 0d 0a 20 20 20 20 20 20 56 64 62 65  rs {..      Vdbe
24d28 43 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20  Cursor *pC;..   
24d29 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
24d2a 73 72 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72  sr;..      int r
24d2b 65 73 3b 0d 0a 20 20 20 20 20 20 75 36 34 20 69  es;..      u64 i
24d2c 4b 65 79 3b 0d 0a 20 20 20 20 7d 20 62 66 3b 0d  Key;..    } bf;.
24d2d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e  .    struct OP_N
24d2e 65 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61  ewRowid_stack_va
24d2f 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 36 34 20  rs {..      i64 
24d30 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
24d31 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
24d32 77 69 64 20 2a 2f 0d 0a 20 20 20 20 20 20 56 64  wid */..      Vd
24d33 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20  beCursor *pC;   
24d34 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
24d35 66 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 74  f table to get t
24d36 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0d  he new rowid */.
24d37 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 20  .      int res; 
24d38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24d39 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71   Result of an sq
24d3a 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29  lite3BtreeLast()
24d3b 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 63   */..      int c
24d3c 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
24d3d 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20    /* Counter to 
24d3e 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 65 72  limit the number
24d3f 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a 2f 0d   of searches */.
24d40 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  .      Mem *pMem
24d41 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
24d42 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
24d43 67 20 6c 61 72 67 65 73 74 20 72 6f 77 69 64 20  g largest rowid 
24d44 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  for AUTOINCREMEN
24d45 54 20 2a 2f 0d 0a 20 20 20 20 20 20 56 64 62 65  T */..      Vdbe
24d46 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
24d47 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65     /* Root frame
24d48 20 6f 66 20 56 44 42 45 20 2a 2f 0d 0a 20 20 20   of VDBE */..   
24d49 20 7d 20 62 67 3b 0d 0a 20 20 20 20 73 74 72 75   } bg;..    stru
24d4a 63 74 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 5f  ct OP_InsertInt_
24d4b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20  stack_vars {..  
24d4c 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20      Mem *pData; 
24d4d 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c        /* MEM cel
24d4e 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66  l holding data f
24d4f 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
24d50 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0d   be inserted */.
24d51 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4b 65 79  .      Mem *pKey
24d52 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20  ;        /* MEM 
24d53 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79  cell holding key
24d54 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64    for the record
24d55 20 2a 2f 0d 0a 20 20 20 20 20 20 69 36 34 20 69   */..      i64 i
24d56 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Key;         /* 
24d57 54 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49  The integer ROWI
24d58 44 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65  D or key for the
24d59 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e   record to be in
24d5a 73 65 72 74 65 64 20 2a 2f 0d 0a 20 20 20 20 20  serted */..     
24d5b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
24d5c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20     /* Cursor to 
24d5d 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
24d5e 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 74   insert is writt
24d5f 65 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74  en */..      int
24d60 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 2f   nZero;        /
24d61 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  * Number of zero
24d62 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64  -bytes to append
24d63 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 73   */..      int s
24d64 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20  eekResult;   /* 
24d65 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20  Result of prior 
24d66 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20  seek or 0 if no 
24d67 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c  USESEEKRESULT fl
24d68 61 67 20 2a 2f 0d 0a 20 20 20 20 20 20 63 6f 6e  ag */..      con
24d69 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f  st char *zDb;  /
24d6a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
24d6b 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70  - used by the up
24d6c 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0d 0a 20 20  date hook */..  
24d6d 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
24d6e 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e  zTbl; /* Table n
24d6f 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68  ame - used by th
24d70 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f  e opdate hook */
24d71 0d 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20  ..      int op; 
24d72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
24d73 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68  ode for update h
24d74 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41  ook: SQLITE_UPDA
24d75 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53  TE or SQLITE_INS
24d76 45 52 54 20 2a 2f 0d 0a 20 20 20 20 7d 20 62 68  ERT */..    } bh
24d77 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  ;..    struct OP
24d78 5f 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61  _Delete_stack_va
24d79 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 36 34 20  rs {..      i64 
24d7a 69 4b 65 79 3b 0d 0a 20 20 20 20 20 20 56 64 62  iKey;..      Vdb
24d7b 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20  eCursor *pC;..  
24d7c 20 20 7d 20 62 69 3b 0d 0a 20 20 20 20 73 74 72    } bi;..    str
24d7d 75 63 74 20 4f 50 5f 53 6f 72 74 65 72 43 6f 6d  uct OP_SorterCom
24d7e 70 61 72 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  pare_stack_vars 
24d7f 7b 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  {..      VdbeCur
24d80 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20 20 20  sor *pC;..      
24d81 69 6e 74 20 72 65 73 3b 0d 0a 20 20 20 20 7d 20  int res;..    } 
24d82 62 6a 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20  bj;..    struct 
24d83 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 5f 73 74  OP_SorterData_st
24d84 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24d85 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
24d86 3b 0d 0a 20 20 20 20 7d 20 62 6b 3b 0d 0a 20 20  ;..    } bk;..  
24d87 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 44    struct OP_RowD
24d88 61 74 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ata_stack_vars {
24d89 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ..      VdbeCurs
24d8a 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20 20 20 42  or *pC;..      B
24d8b 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0d  tCursor *pCrsr;.
24d8c 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0d 0a 20  .      u32 n;.. 
24d8d 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0d 0a 20       i64 n64;.. 
24d8e 20 20 20 7d 20 62 6c 3b 0d 0a 20 20 20 20 73 74     } bl;..    st
24d8f 72 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74  ruct OP_Rowid_st
24d90 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20  ack_vars {..    
24d91 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
24d92 3b 0d 0a 20 20 20 20 20 20 69 36 34 20 76 3b 0d  ;..      i64 v;.
24d93 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
24d94 74 61 62 20 2a 70 56 74 61 62 3b 0d 0a 20 20 20  tab *pVtab;..   
24d95 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33     const sqlite3
24d96 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
24d97 3b 0d 0a 20 20 20 20 7d 20 62 6d 3b 0d 0a 20 20  ;..    } bm;..  
24d98 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75 6c 6c    struct OP_Null
24d99 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  Row_stack_vars {
24d9a 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ..      VdbeCurs
24d9b 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20 7d 20 62  or *pC;..    } b
24d9c 6e 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f  n;..    struct O
24d9d 50 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76 61 72  P_Last_stack_var
24d9e 73 20 7b 0d 0a 20 20 20 20 20 20 56 64 62 65 43  s {..      VdbeC
24d9f 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20  ursor *pC;..    
24da0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
24da1 72 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 65  r;..      int re
24da2 73 3b 0d 0a 20 20 20 20 7d 20 62 6f 3b 0d 0a 20  s;..    } bo;.. 
24da3 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 77     struct OP_Rew
24da4 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ind_stack_vars {
24da5 0d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ..      VdbeCurs
24da6 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20 20 20 42  or *pC;..      B
24da7 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0d  tCursor *pCrsr;.
24da8 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0d  .      int res;.
24da9 0a 20 20 20 20 7d 20 62 70 3b 0d 0a 20 20 20 20  .    } bp;..    
24daa 73 74 72 75 63 74 20 4f 50 5f 4e 65 78 74 5f 73  struct OP_Next_s
24dab 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20  tack_vars {..   
24dac 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
24dad 43 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 65  C;..      int re
24dae 73 3b 0d 0a 20 20 20 20 7d 20 62 71 3b 0d 0a 20  s;..    } bq;.. 
24daf 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78     struct OP_Idx
24db0 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72  Insert_stack_var
24db1 73 20 7b 0d 0a 20 20 20 20 20 20 56 64 62 65 43  s {..      VdbeC
24db2 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20  ursor *pC;..    
24db3 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
24db4 72 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b  r;..      int nK
24db5 65 79 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74  ey;..      const
24db6 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0d 0a 20 20   char *zKey;..  
24db7 20 20 7d 20 62 72 3b 0d 0a 20 20 20 20 73 74 72    } br;..    str
24db8 75 63 74 20 4f 50 5f 49 64 78 44 65 6c 65 74 65  uct OP_IdxDelete
24db9 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20  _stack_vars {.. 
24dba 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
24dbb 2a 70 43 3b 0d 0a 20 20 20 20 20 20 42 74 43 75  *pC;..      BtCu
24dbc 72 73 6f 72 20 2a 70 43 72 73 72 3b 0d 0a 20 20  rsor *pCrsr;..  
24dbd 20 20 20 20 69 6e 74 20 72 65 73 3b 0d 0a 20 20      int res;..  
24dbe 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
24dbf 72 64 20 72 3b 0d 0a 20 20 20 20 7d 20 62 73 3b  rd r;..    } bs;
24dc0 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24dc1 49 64 78 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76  IdxRowid_stack_v
24dc2 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 42 74 43  ars {..      BtC
24dc3 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0d 0a 20  ursor *pCrsr;.. 
24dc4 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
24dc5 2a 70 43 3b 0d 0a 20 20 20 20 20 20 69 36 34 20  *pC;..      i64 
24dc6 72 6f 77 69 64 3b 0d 0a 20 20 20 20 7d 20 62 74  rowid;..    } bt
24dc7 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  ;..    struct OP
24dc8 5f 49 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72  _IdxGE_stack_var
24dc9 73 20 7b 0d 0a 20 20 20 20 20 20 56 64 62 65 43  s {..      VdbeC
24dca 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20  ursor *pC;..    
24dcb 20 20 69 6e 74 20 72 65 73 3b 0d 0a 20 20 20 20    int res;..    
24dcc 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
24dcd 20 72 3b 0d 0a 20 20 20 20 7d 20 62 75 3b 0d 0a   r;..    } bu;..
24dce 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65      struct OP_De
24dcf 73 74 72 6f 79 5f 73 74 61 63 6b 5f 76 61 72 73  stroy_stack_vars
24dd0 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 4d   {..      int iM
24dd1 6f 76 65 64 3b 0d 0a 20 20 20 20 20 20 69 6e 74  oved;..      int
24dd2 20 69 43 6e 74 3b 0d 0a 20 20 20 20 20 20 56 64   iCnt;..      Vd
24dd3 62 65 20 2a 70 56 64 62 65 3b 0d 0a 20 20 20 20  be *pVdbe;..    
24dd4 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20 20 20 20    int iDb;..    
24dd5 7d 20 62 76 3b 0d 0a 20 20 20 20 73 74 72 75 63  } bv;..    struc
24dd6 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 61 63 6b  t OP_Clear_stack
24dd7 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 69  _vars {..      i
24dd8 6e 74 20 6e 43 68 61 6e 67 65 3b 0d 0a 20 20 20  nt nChange;..   
24dd9 20 7d 20 62 77 3b 0d 0a 20 20 20 20 73 74 72 75   } bw;..    stru
24dda 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  ct OP_CreateTabl
24ddb 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a  e_stack_vars {..
24ddc 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0d        int pgno;.
24ddd 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73  .      int flags
24dde 3b 0d 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62  ;..      Db *pDb
24ddf 3b 0d 0a 20 20 20 20 7d 20 62 78 3b 0d 0a 20 20  ;..    } bx;..  
24de0 20 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 73    struct OP_Pars
24de1 65 53 63 68 65 6d 61 5f 73 74 61 63 6b 5f 76 61  eSchema_stack_va
24de2 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  rs {..      int 
24de3 69 44 62 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73  iDb;..      cons
24de4 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b  t char *zMaster;
24de5 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53  ..      char *zS
24de6 71 6c 3b 0d 0a 20 20 20 20 20 20 49 6e 69 74 44  ql;..      InitD
24de7 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0d 0a 20  ata initData;.. 
24de8 20 20 20 7d 20 62 79 3b 0d 0a 20 20 20 20 73 74     } by;..    st
24de9 72 75 63 74 20 4f 50 5f 49 6e 74 65 67 72 69 74  ruct OP_Integrit
24dea 79 43 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  yCk_stack_vars {
24deb 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f  ..      int nRoo
24dec 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  t;      /* Numbe
24ded 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 63  r of tables to c
24dee 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f  heck.  (Number o
24def 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a  f root pages.) *
24df0 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 52  /..      int *aR
24df1 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61  oot;     /* Arra
24df2 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75  y of rootpage nu
24df3 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73  mbers for tables
24df4 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a   to be checked *
24df5 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20  /..      int j; 
24df6 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
24df7 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 20   counter */..   
24df8 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20     int nErr;    
24df9 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
24dfa 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20  errors reported 
24dfb 2a 2f 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a  */..      char *
24dfc 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  z;        /* Tex
24dfd 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72  t of the error r
24dfe 65 70 6f 72 74 20 2a 2f 0d 0a 20 20 20 20 20 20  eport */..      
24dff 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20  Mem *pnErr;     
24e00 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70  /* Register keep
24e01 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72  ing track of err
24e02 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f  ors remaining */
24e03 0d 0a 20 20 20 20 7d 20 62 7a 3b 0d 0a 20 20 20  ..    } bz;..   
24e04 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65   struct OP_RowSe
24e05 74 52 65 61 64 5f 73 74 61 63 6b 5f 76 61 72 73  tRead_stack_vars
24e06 20 7b 0d 0a 20 20 20 20 20 20 69 36 34 20 76 61   {..      i64 va
24e07 6c 3b 0d 0a 20 20 20 20 7d 20 63 61 3b 0d 0a 20  l;..    } ca;.. 
24e08 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77     struct OP_Row
24e09 53 65 74 54 65 73 74 5f 73 74 61 63 6b 5f 76 61  SetTest_stack_va
24e0a 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  rs {..      int 
24e0b 69 53 65 74 3b 0d 0a 20 20 20 20 20 20 69 6e 74  iSet;..      int
24e0c 20 65 78 69 73 74 73 3b 0d 0a 20 20 20 20 7d 20   exists;..    } 
24e0d 63 62 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20  cb;..    struct 
24e0e 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 61 63 6b  OP_Program_stack
24e0f 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 69  _vars {..      i
24e10 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt nMem;        
24e11 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24e12 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73   of memory regis
24e13 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f  ters for sub-pro
24e14 67 72 61 6d 20 2a 2f 0d 0a 20 20 20 20 20 20 69  gram */..      i
24e15 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
24e16 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
24e17 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65  of runtime space
24e18 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75   required for su
24e19 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20 20  b-program */..  
24e1a 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20      Mem *pRt;   
24e1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
24e1c 65 67 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63  egister to alloc
24e1d 61 74 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63  ate runtime spac
24e1e 65 20 2a 2f 0d 0a 20 20 20 20 20 20 4d 65 6d 20  e */..      Mem 
24e1f 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
24e20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69      /* Used to i
24e21 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d  terate through m
24e22 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0d 0a  emory cells */..
24e23 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b        Mem *pEnd;
24e24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24e25 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c   Last memory cel
24e26 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 2a  l in new array *
24e27 2f 0d 0a 20 20 20 20 20 20 56 64 62 65 46 72 61  /..      VdbeFra
24e28 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
24e29 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61   /* New vdbe fra
24e2a 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e  me to execute in
24e2b 20 2a 2f 0d 0a 20 20 20 20 20 20 53 75 62 50 72   */..      SubPr
24e2c 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b  ogram *pProgram;
24e2d 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61     /* Sub-progra
24e2e 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0d  m to execute */.
24e2f 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20  .      void *t; 
24e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24e31 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79  * Token identify
24e32 69 6e 67 20 74 72 69 67 67 65 72 20 2a 2f 0d 0a  ing trigger */..
24e33 20 20 20 20 7d 20 63 63 3b 0d 0a 20 20 20 20 73      } cc;..    s
24e34 74 72 75 63 74 20 4f 50 5f 50 61 72 61 6d 5f 73  truct OP_Param_s
24e35 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20  tack_vars {..   
24e36 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46     VdbeFrame *pF
24e37 72 61 6d 65 3b 0d 0a 20 20 20 20 20 20 4d 65 6d  rame;..      Mem
24e38 20 2a 70 49 6e 3b 0d 0a 20 20 20 20 7d 20 63 64   *pIn;..    } cd
24e39 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  ;..    struct OP
24e3a 5f 4d 65 6d 4d 61 78 5f 73 74 61 63 6b 5f 76 61  _MemMax_stack_va
24e3b 72 73 20 7b 0d 0a 20 20 20 20 20 20 4d 65 6d 20  rs {..      Mem 
24e3c 2a 70 49 6e 31 3b 0d 0a 20 20 20 20 20 20 56 64  *pIn1;..      Vd
24e3d 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
24e3e 0d 0a 20 20 20 20 7d 20 63 65 3b 0d 0a 20 20 20  ..    } ce;..   
24e3f 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 74   struct OP_AggSt
24e40 65 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  ep_stack_vars {.
24e41 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0d 0a 20  .      int n;.. 
24e42 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20       int i;..   
24e43 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0d 0a 20     Mem *pMem;.. 
24e44 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0d       Mem *pRec;.
24e45 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
24e46 6f 6e 74 65 78 74 20 63 74 78 3b 0d 0a 20 20 20  ontext ctx;..   
24e47 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
24e48 20 2a 2a 61 70 56 61 6c 3b 0d 0a 20 20 20 20 7d   **apVal;..    }
24e49 20 63 66 3b 0d 0a 20 20 20 20 73 74 72 75 63 74   cf;..    struct
24e4a 20 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73 74 61   OP_AggFinal_sta
24e4b 63 6b 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20  ck_vars {..     
24e4c 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0d 0a 20 20 20   Mem *pMem;..   
24e4d 20 7d 20 63 67 3b 0d 0a 20 20 20 20 73 74 72 75   } cg;..    stru
24e4e 63 74 20 4f 50 5f 43 68 65 63 6b 70 6f 69 6e 74  ct OP_Checkpoint
24e4f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d 0a 20  _stack_vars {.. 
24e50 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20       int i;     
24e51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e52 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
24e53 6e 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20 69  nter */..      i
24e54 6e 74 20 61 52 65 73 5b 33 5d 3b 20 20 20 20 20  nt aRes[3];     
24e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24e56 2a 20 52 65 73 75 6c 74 73 20 2a 2f 0d 0a 20 20  * Results */..  
24e57 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
24e58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e59 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73      /* Write res
24e5a 75 6c 74 73 20 68 65 72 65 20 2a 2f 0d 0a 20 20  ults here */..  
24e5b 20 20 7d 20 63 68 3b 0d 0a 20 20 20 20 73 74 72    } ch;..    str
24e5c 75 63 74 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f  uct OP_JournalMo
24e5d 64 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  de_stack_vars {.
24e5e 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
24e5f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
24e60 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
24e61 74 6f 20 63 68 61 6e 67 65 20 6a 6f 75 72 6e 61  to change journa
24e62 6c 20 6d 6f 64 65 20 6f 66 20 2a 2f 0d 0a 20 20  l mode of */..  
24e63 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
24e64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
24e65 20 20 20 20 2f 2a 20 50 61 67 65 72 20 61 73 73      /* Pager ass
24e66 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 42 74  ociated with pBt
24e67 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 65   */..      int e
24e68 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  New;            
24e69 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
24e6a 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 2a  w journal mode *
24e6b 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 65 4f 6c  /..      int eOl
24e6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
24e6d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24e6e 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65  old journal mode
24e6f 20 2a 2f 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74   */..      const
24e70 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
24e71 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ;          /* Na
24e72 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  me of database f
24e73 69 6c 65 20 66 6f 72 20 70 50 61 67 65 72 20 2a  ile for pPager *
24e74 2f 0d 0a 20 20 20 20 7d 20 63 69 3b 0d 0a 20 20  /..    } ci;..  
24e75 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72    struct OP_Incr
24e76 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72  Vacuum_stack_var
24e77 73 20 7b 0d 0a 20 20 20 20 20 20 42 74 72 65 65  s {..      Btree
24e78 20 2a 70 42 74 3b 0d 0a 20 20 20 20 7d 20 63 6a   *pBt;..    } cj
24e79 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  ;..    struct OP
24e7a 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61  _VBegin_stack_va
24e7b 72 73 20 7b 0d 0a 20 20 20 20 20 20 56 54 61 62  rs {..      VTab
24e7c 6c 65 20 2a 70 56 54 61 62 3b 0d 0a 20 20 20 20  le *pVTab;..    
24e7d 7d 20 63 6b 3b 0d 0a 20 20 20 20 73 74 72 75 63  } ck;..    struc
24e7e 74 20 4f 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b  t OP_VOpen_stack
24e7f 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 56  _vars {..      V
24e80 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
24e81 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
24e82 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
24e83 61 62 43 75 72 73 6f 72 3b 0d 0a 20 20 20 20 20  abCursor;..     
24e84 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
24e85 56 74 61 62 3b 0d 0a 20 20 20 20 20 20 73 71 6c  Vtab;..      sql
24e86 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
24e87 64 75 6c 65 3b 0d 0a 20 20 20 20 7d 20 63 6c 3b  dule;..    } cl;
24e88 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24e89 56 46 69 6c 74 65 72 5f 73 74 61 63 6b 5f 76 61  VFilter_stack_va
24e8a 72 73 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  rs {..      int 
24e8b 6e 41 72 67 3b 0d 0a 20 20 20 20 20 20 69 6e 74  nArg;..      int
24e8c 20 69 51 75 65 72 79 3b 0d 0a 20 20 20 20 20 20   iQuery;..      
24e8d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
24e8e 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0d 0a  dule *pModule;..
24e8f 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72        Mem *pQuer
24e90 79 3b 0d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  y;..      Mem *p
24e91 41 72 67 63 3b 0d 0a 20 20 20 20 20 20 73 71 6c  Argc;..      sql
24e92 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
24e93 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0d 0a   *pVtabCursor;..
24e94 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
24e95 61 62 20 2a 70 56 74 61 62 3b 0d 0a 20 20 20 20  ab *pVtab;..    
24e96 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
24e97 75 72 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72  ur;..      int r
24e98 65 73 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69  es;..      int i
24e99 3b 0d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 2a 61  ;..      Mem **a
24e9a 70 41 72 67 3b 0d 0a 20 20 20 20 7d 20 63 6d 3b  pArg;..    } cm;
24e9b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ..    struct OP_
24e9c 56 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61  VColumn_stack_va
24e9d 72 73 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  rs {..      sqli
24e9e 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
24e9f 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71  ..      const sq
24ea0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
24ea1 6f 64 75 6c 65 3b 0d 0a 20 20 20 20 20 20 4d 65  odule;..      Me
24ea2 6d 20 2a 70 44 65 73 74 3b 0d 0a 20 20 20 20 20  m *pDest;..     
24ea3 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
24ea4 20 73 43 6f 6e 74 65 78 74 3b 0d 0a 20 20 20 20   sContext;..    
24ea5 7d 20 63 6e 3b 0d 0a 20 20 20 20 73 74 72 75 63  } cn;..    struc
24ea6 74 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 6b  t OP_VNext_stack
24ea7 5f 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 73  _vars {..      s
24ea8 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
24ea9 61 62 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74  ab;..      const
24eaa 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
24eab 2a 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 20 20 20  *pModule;..     
24eac 20 69 6e 74 20 72 65 73 3b 0d 0a 20 20 20 20 20   int res;..     
24ead 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
24eae 72 3b 0d 0a 20 20 20 20 7d 20 63 6f 3b 0d 0a 20  r;..    } co;.. 
24eaf 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65     struct OP_VRe
24eb0 6e 61 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  name_stack_vars 
24eb1 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
24eb2 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0d 0a 20  _vtab *pVtab;.. 
24eb3 20 20 20 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b       Mem *pName;
24eb4 0d 0a 20 20 20 20 7d 20 63 70 3b 0d 0a 20 20 20  ..    } cp;..   
24eb5 20 73 74 72 75 63 74 20 4f 50 5f 56 55 70 64 61   struct OP_VUpda
24eb6 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0d  te_stack_vars {.
24eb7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
24eb8 74 61 62 20 2a 70 56 74 61 62 3b 0d 0a 20 20 20  tab *pVtab;..   
24eb9 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c     sqlite3_modul
24eba 65 20 2a 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 20  e *pModule;..   
24ebb 20 20 20 69 6e 74 20 6e 41 72 67 3b 0d 0a 20 20     int nArg;..  
24ebc 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20      int i;..    
24ebd 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
24ebe 6f 77 69 64 3b 0d 0a 20 20 20 20 20 20 4d 65 6d  owid;..      Mem
24ebf 20 2a 2a 61 70 41 72 67 3b 0d 0a 20 20 20 20 20   **apArg;..     
24ec0 20 4d 65 6d 20 2a 70 58 3b 0d 0a 20 20 20 20 7d   Mem *pX;..    }
24ec1 20 63 71 3b 0d 0a 20 20 20 20 73 74 72 75 63 74   cq;..    struct
24ec2 20 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f   OP_Trace_stack_
24ec3 76 61 72 73 20 7b 0d 0a 20 20 20 20 20 20 63 68  vars {..      ch
24ec4 61 72 20 2a 7a 54 72 61 63 65 3b 0d 0a 20 20 20  ar *zTrace;..   
24ec5 20 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 20     char *z;..   
24ec6 20 7d 20 63 72 3b 0d 0a 20 20 7d 20 75 3b 0d 0a   } cr;..  } u;..
24ec7 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74    /* End automat
24ec8 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
24ec9 20 63 6f 64 65 0d 0a 20 20 2a 2a 2a 2a 2a 2a 2a   code..  *******
24eca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ecb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ecc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ecd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
24ece 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ..  assert( p->m
24ecf 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
24ed0 5f 52 55 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69  _RUN );  /* sqli
24ed1 74 65 33 5f 73 74 65 70 28 29 20 76 65 72 69 66  te3_step() verif
24ed2 69 65 73 20 74 68 69 73 20 2a 2f 0d 0a 20 20 73  ies this */..  s
24ed3 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65 72 28  qlite3VdbeEnter(
24ed4 70 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 72 63  p);..  if( p->rc
24ed5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
24ed6 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68  {..    /* This h
24ed7 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c  appens if a mall
24ed8 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61  oc() inside a ca
24ed9 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ll to sqlite3_co
24eda 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0d 0a  lumn_text() or..
24edb 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63      ** sqlite3_c
24edc 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
24edd 61 69 6c 65 64 2e 20 20 2a 2f 0d 0a 20 20 20 20  ailed.  */..    
24ede 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20  goto no_mem;..  
24edf 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  }..  assert( p->
24ee0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
24ee1 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42   p->rc==SQLITE_B
24ee2 55 53 59 20 29 3b 0d 0a 20 20 70 2d 3e 72 63 20  USY );..  p->rc 
24ee3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
24ee4 61 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61  assert( p->expla
24ee5 69 6e 3d 3d 30 20 29 3b 0d 0a 20 20 70 2d 3e 70  in==0 );..  p->p
24ee6 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0d 0a  ResultSet = 0;..
24ee7 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65    db->busyHandle
24ee8 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0d 0a 20 20  r.nBusy = 0;..  
24ee9 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
24eea 55 50 54 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  UPT;..  sqlite3V
24eeb 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29  dbeIOTraceSql(p)
24eec 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
24eed 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
24eee 43 41 4c 4c 42 41 43 4b 0d 0a 20 20 63 68 65 63  CALLBACK..  chec
24eef 6b 50 72 6f 67 72 65 73 73 20 3d 20 64 62 2d 3e  kProgress = db->
24ef0 78 50 72 6f 67 72 65 73 73 21 3d 30 3b 0d 0a 23  xProgress!=0;..#
24ef1 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
24ef2 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 73 71  LITE_DEBUG..  sq
24ef3 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
24ef4 4d 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 69 66 28  Malloc();..  if(
24ef5 20 70 2d 3e 70 63 3d 3d 30 20 20 26 26 20 28 70   p->pc==0  && (p
24ef6 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
24ef7 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67  LITE_VdbeListing
24ef8 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74  )!=0 ){..    int
24ef9 20 69 3b 0d 0a 20 20 20 20 70 72 69 6e 74 66 28   i;..    printf(
24efa 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69  "VDBE Program Li
24efb 73 74 69 6e 67 3a 5c 6e 22 29 3b 0d 0a 20 20 20  sting:\n");..   
24efc 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
24efd 74 53 71 6c 28 70 29 3b 0d 0a 20 20 20 20 66 6f  tSql(p);..    fo
24efe 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
24eff 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 73 71   i++){..      sq
24f00 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
24f01 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70  (stdout, i, &aOp
24f02 5b 69 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  [i]);..    }..  
24f03 7d 0d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42  }..  sqlite3EndB
24f04 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0d 0a  enignMalloc();..
24f05 23 65 6e 64 69 66 0d 0a 20 20 66 6f 72 28 70 63  #endif..  for(pc
24f06 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49  =p->pc; rc==SQLI
24f07 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0d 0a 20  TE_OK; pc++){.. 
24f08 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30     assert( pc>=0
24f09 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b   && pc<p->nOp );
24f0a 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ..    if( db->ma
24f0b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
24f0c 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 23 69 66 64 65  o no_mem;..#ifde
24f0d 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0d 0a  f VDBE_PROFILE..
24f0e 20 20 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b      origPc = pc;
24f0f 0d 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73 71  ..    start = sq
24f10 6c 69 74 65 33 48 77 74 69 6d 65 28 29 3b 0d 0a  lite3Hwtime();..
24f11 23 65 6e 64 69 66 0d 0a 20 20 20 20 70 4f 70 20  #endif..    pOp 
24f12 3d 20 26 61 4f 70 5b 70 63 5d 3b 0d 0a 0d 0a 20  = &aOp[pc];.... 
24f13 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77     /* Only allow
24f14 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c 49   tracing if SQLI
24f15 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69  TE_DEBUG is defi
24f16 6e 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 23 69  ned...    */..#i
24f17 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
24f18 47 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72  G..    if( p->tr
24f19 61 63 65 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  ace ){..      if
24f1a 28 20 70 63 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ( pc==0 ){..    
24f1b 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 45      printf("VDBE
24f1c 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61 63 65   Execution Trace
24f1d 3a 5c 6e 22 29 3b 0d 0a 20 20 20 20 20 20 20 20  :\n");..        
24f1e 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
24f1f 53 71 6c 28 70 29 3b 0d 0a 20 20 20 20 20 20 7d  Sql(p);..      }
24f20 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
24f21 64 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72  dbePrintOp(p->tr
24f22 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0d 0a  ace, pc, pOp);..
24f23 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20      }..#endif.. 
24f24 20 20 20 20 20 0d 0a 0d 0a 20 20 20 20 2f 2a 20       ....    /* 
24f25 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
24f26 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c  we need to simul
24f27 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 70 74  ate an interrupt
24f28 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70  .  This only hap
24f29 70 65 6e 73 0d 0a 20 20 20 20 2a 2a 20 69 66 20  pens..    ** if 
24f2a 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 61  we have a specia
24f2b 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0d 0a 20  l test build... 
24f2c 20 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51     */..#ifdef SQ
24f2d 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 20 20 69  LITE_TEST..    i
24f2e 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  f( sqlite3_inter
24f2f 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0d  rupt_count>0 ){.
24f30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
24f31 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d  nterrupt_count--
24f32 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ;..      if( sql
24f33 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63  ite3_interrupt_c
24f34 6f 75 6e 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ount==0 ){..    
24f35 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
24f36 72 72 75 70 74 28 64 62 29 3b 0d 0a 20 20 20 20  rrupt(db);..    
24f37 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64    }..    }..#end
24f38 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  if....#ifndef SQ
24f39 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
24f3a 53 53 5f 43 41 4c 4c 42 41 43 4b 0d 0a 20 20 20  SS_CALLBACK..   
24f3b 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f   /* Call the pro
24f3c 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69  gress callback i
24f3d 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72  f it is configur
24f3e 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 75 69  ed and the requi
24f3f 72 65 64 20 6e 75 6d 62 65 72 0d 0a 20 20 20 20  red number..    
24f40 2a 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 68  ** of VDBE ops h
24f41 61 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 65  ave been execute
24f42 64 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 20  d (either since 
24f43 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20  this invocation 
24f44 6f 66 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  of..    ** sqlit
24f45 65 33 56 64 62 65 45 78 65 63 28 29 20 6f 72 20  e3VdbeExec() or 
24f46 73 69 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20  since last time 
24f47 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
24f48 6c 62 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64  lback was called
24f49 29 2e 0d 0a 20 20 20 20 2a 2a 20 49 66 20 74 68  )...    ** If th
24f4a 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
24f4b 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
24f4c 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 20 76  zero, exit the v
24f4d 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 77  irtual machine w
24f4e 69 74 68 0d 0a 20 20 20 20 2a 2a 20 61 20 72 65  ith..    ** a re
24f4f 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 45  turn code SQLITE
24f50 5f 41 42 4f 52 54 2e 0d 0a 20 20 20 20 2a 2f 0d  _ABORT...    */.
24f51 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 50 72  .    if( checkPr
24f52 6f 67 72 65 73 73 20 29 7b 0d 0a 20 20 20 20 20  ogress ){..     
24f53 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65   if( db->nProgre
24f54 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73  ssOps==nProgress
24f55 4f 70 73 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Ops ){..        
24f56 69 6e 74 20 70 72 63 3b 0d 0a 20 20 20 20 20 20  int prc;..      
24f57 20 20 70 72 63 20 3d 20 64 62 2d 3e 78 50 72 6f    prc = db->xPro
24f58 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72  gress(db->pProgr
24f59 65 73 73 41 72 67 29 3b 0d 0a 20 20 20 20 20 20  essArg);..      
24f5a 20 20 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0d    if( prc!=0 ){.
24f5b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
24f5c 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
24f5d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ;..          got
24f5e 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
24f5f 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  t;..        }.. 
24f60 20 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73         nProgress
24f61 4f 70 73 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  Ops = 0;..      
24f62 7d 0d 0a 20 20 20 20 20 20 6e 50 72 6f 67 72 65  }..      nProgre
24f63 73 73 4f 70 73 2b 2b 3b 0d 0a 20 20 20 20 7d 0d  ssOps++;..    }.
24f64 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 2f  .#endif....    /
24f65 2a 20 4f 6e 20 61 6e 79 20 6f 70 63 6f 64 65 20  * On any opcode 
24f66 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 2d 70  with the "out2-p
24f67 72 65 72 65 6c 61 73 65 22 20 74 61 67 2c 20 66  rerelase" tag, f
24f68 72 65 65 20 61 6e 79 0d 0a 20 20 20 20 2a 2a 20  ree any..    ** 
24f69 65 78 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74  external allocat
24f6a 69 6f 6e 73 20 6f 75 74 20 6f 66 20 6d 65 6d 5b  ions out of mem[
24f6b 70 32 5d 20 61 6e 64 20 73 65 74 20 6d 65 6d 5b  p2] and set mem[
24f6c 70 32 5d 20 74 6f 20 62 65 0d 0a 20 20 20 20 2a  p2] to be..    *
24f6d 2a 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 69  * an undefined i
24f6e 6e 74 65 67 65 72 2e 20 20 4f 70 63 6f 64 65 73  nteger.  Opcodes
24f6f 20 77 69 6c 6c 20 65 69 74 68 65 72 20 66 69 6c   will either fil
24f70 6c 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72  l in the integer
24f71 0d 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f  ..    ** value o
24f72 72 20 63 6f 6e 76 65 72 74 20 6d 65 6d 5b 70 32  r convert mem[p2
24f73 5d 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74  ] to a different
24f74 20 74 79 70 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a   type...    */..
24f75 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
24f76 3e 6f 70 66 6c 61 67 73 3d 3d 73 71 6c 69 74 65  >opflags==sqlite
24f77 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b  3OpcodeProperty[
24f78 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 20 29 3b 0d  pOp->opcode] );.
24f79 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
24f7a 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55  flags & OPFLG_OU
24f7b 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b  T2_PRERELEASE ){
24f7c 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
24f7d 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0d 0a 20 20  pOp->p2>0 );..  
24f7e 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
24f7f 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d  >p2<=p->nMem );.
24f80 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 61  .      pOut = &a
24f81 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20  Mem[pOp->p2];.. 
24f82 20 20 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43       memAboutToC
24f83 68 61 6e 67 65 28 70 2c 20 70 4f 75 74 29 3b 0d  hange(p, pOut);.
24f84 0a 20 20 20 20 20 20 56 64 62 65 4d 65 6d 52 65  .      VdbeMemRe
24f85 6c 65 61 73 65 28 70 4f 75 74 29 3b 0d 0a 20 20  lease(pOut);..  
24f86 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
24f87 3d 20 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 20 20  = MEM_Int;..    
24f88 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 61 6e 69  }....    /* Sani
24f89 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6f  ty checking on o
24f8a 74 68 65 72 20 6f 70 65 72 61 6e 64 73 20 2a 2f  ther operands */
24f8b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
24f8c 44 45 42 55 47 0d 0a 20 20 20 20 69 66 28 20 28  DEBUG..    if( (
24f8d 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f  pOp->opflags & O
24f8e 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b 0d  PFLG_IN1)!=0 ){.
24f8f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24f90 4f 70 2d 3e 70 31 3e 30 20 29 3b 0d 0a 20 20 20  Op->p1>0 );..   
24f91 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
24f92 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a  p1<=p->nMem );..
24f93 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65        assert( me
24f94 6d 49 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70  mIsValid(&aMem[p
24f95 4f 70 2d 3e 70 31 5d 29 20 29 3b 0d 0a 20 20 20  Op->p1]) );..   
24f96 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
24f97 45 28 70 4f 70 2d 3e 70 31 2c 20 26 61 4d 65 6d  E(pOp->p1, &aMem
24f98 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0d 0a 20 20 20  [pOp->p1]);..   
24f99 20 7d 0d 0a 20 20 20 20 69 66 28 20 28 70 4f 70   }..    if( (pOp
24f9a 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c  ->opflags & OPFL
24f9b 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0d 0a 20 20  G_IN2)!=0 ){..  
24f9c 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
24f9d 3e 70 32 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20  >p2>0 );..      
24f9e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
24f9f 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a 20 20 20  =p->nMem );..   
24fa0 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73     assert( memIs
24fa1 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70 4f 70 2d  Valid(&aMem[pOp-
24fa2 3e 70 32 5d 29 20 29 3b 0d 0a 20 20 20 20 20 20  >p2]) );..      
24fa3 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
24fa4 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 4f  Op->p2, &aMem[pO
24fa5 70 2d 3e 70 32 5d 29 3b 0d 0a 20 20 20 20 7d 0d  p->p2]);..    }.
24fa6 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f  .    if( (pOp->o
24fa7 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 49  pflags & OPFLG_I
24fa8 4e 33 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  N3)!=0 ){..     
24fa9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
24faa 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73  >0 );..      ass
24fab 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
24fac 3e 6e 4d 65 6d 20 29 3b 0d 0a 20 20 20 20 20 20  >nMem );..      
24fad 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c  assert( memIsVal
24fae 69 64 28 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  id(&aMem[pOp->p3
24faf 5d 29 20 29 3b 0d 0a 20 20 20 20 20 20 52 45 47  ]) );..      REG
24fb0 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
24fb1 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  >p3, &aMem[pOp->
24fb2 70 33 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  p3]);..    }..  
24fb3 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c    if( (pOp->opfl
24fb4 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32  ags & OPFLG_OUT2
24fb5 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 61  )!=0 ){..      a
24fb6 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
24fb7 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
24fb8 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  t( pOp->p2<=p->n
24fb9 4d 65 6d 20 29 3b 0d 0a 20 20 20 20 20 20 6d 65  Mem );..      me
24fba 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70  mAboutToChange(p
24fbb 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  , &aMem[pOp->p2]
24fbc 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
24fbd 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  f( (pOp->opflags
24fbe 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d   & OPFLG_OUT3)!=
24fbf 30 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  0 ){..      asse
24fc0 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b  rt( pOp->p3>0 );
24fc1 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
24fc2 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
24fc3 20 29 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 41 62   );..      memAb
24fc4 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 26  outToChange(p, &
24fc5 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 29 3b 0d  aMem[pOp->p3]);.
24fc6 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
24fc7 20 20 0d 0a 20 20 20 20 73 77 69 74 63 68 28 20    ..    switch( 
24fc8 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0d 0a  pOp->opcode ){..
24fc9 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
24fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24fcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24fcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24fcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24fce 0d 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77  ..** What follow
24fcf 73 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73  s is a massive s
24fd0 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
24fd1 77 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20  where each case 
24fd2 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0d 0a 2a 2a  implements a..**
24fd3 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72 75   separate instru
24fd4 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72  ction in the vir
24fd5 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49  tual machine.  I
24fd6 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20  f we follow the 
24fd7 75 73 75 61 6c 0d 0a 2a 2a 20 69 6e 64 65 6e 74  usual..** indent
24fd8 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e  ation convention
24fd9 73 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f  s, each case sho
24fda 75 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20  uld be indented 
24fdb 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75  by 6 spaces.  Bu
24fdc 74 0d 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20  t..** that is a 
24fdd 6c 6f 74 20 6f 66 20 77 61 73 74 65 64 20 73 70  lot of wasted sp
24fde 61 63 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20  ace on the left 
24fdf 6d 61 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20  margin.  So the 
24fe0 63 6f 64 65 20 77 69 74 68 69 6e 0d 0a 2a 2a 20  code within..** 
24fe1 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65  the switch state
24fe2 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b 20  ment will break 
24fe3 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20  with convention 
24fe4 61 6e 64 20 62 65 20 66 6c 75 73 68 2d 6c 65 66  and be flush-lef
24fe5 74 2e 20 41 6e 6f 74 68 65 72 0d 0a 2a 2a 20 62  t. Another..** b
24fe6 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69  ig comment (simi
24fe7 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29  lar to this one)
24fe8 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70   will mark the p
24fe9 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65  oint in the code
24fea 20 77 68 65 72 65 0d 0a 2a 2a 20 77 65 20 74 72   where..** we tr
24feb 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 6f  ansition back to
24fec 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74   normal indentat
24fed 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ion...**..** The
24fee 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65   formatting of e
24fef 61 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f  ach case is impo
24ff0 72 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65  rtant.  The make
24ff1 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0d  file for SQLite.
24ff2 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77  .** generates tw
24ff3 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64  o C files "opcod
24ff4 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64  es.h" and "opcod
24ff5 65 73 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e  es.c" by scannin
24ff6 67 20 74 68 69 73 0d 0a 2a 2a 20 66 69 6c 65 20  g this..** file 
24ff7 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65  looking for line
24ff8 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
24ff9 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 54  h "case OP_".  T
24ffa 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c  he opcodes.h fil
24ffb 65 73 0d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66  es..** will be f
24ffc 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 69  illed with #defi
24ffd 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 6e  nes that give un
24ffe 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 6c  ique integer val
24fff 75 65 73 20 74 6f 20 65 61 63 68 0d 0a 2a 2a 20  ues to each..** 
25000 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f  opcode and the o
25001 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73  pcodes.c file is
25002 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20   filled with an 
25003 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73  array of strings
25004 20 77 68 65 72 65 0d 0a 2a 2a 20 65 61 63 68 20   where..** each 
25005 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 79  string is the sy
25006 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20  mbolic name for 
25007 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
25008 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68  g opcode.  If th
25009 65 0d 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65  e..** case state
2500a 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64  ment is followed
2500b 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66   by a comment of
2500c 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61   the form "/# sa
2500d 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0d 0a 2a  me as ... #/"..*
2500e 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69  * that comment i
2500f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
25010 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c  ine the particul
25011 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ar value of the 
25012 6f 70 63 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  opcode...**..** 
25013 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69  Other keywords i
25014 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68  n the comment th
25015 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20  at follows each 
25016 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74 6f  case are used to
25017 0d 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74  ..** construct t
25018 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c  he OPFLG_INITIAL
25019 49 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20  IZER value that 
2501a 69 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f  initializes opco
2501b 64 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0d 0a 2a  deProperty[]...*
2501c 2a 20 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75  * Keywords inclu
2501d 64 65 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e  de: in1, in2, in
2501e 33 2c 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61  3, out2_prerelea
2501f 73 65 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20  se, out2, out3. 
25020 20 53 65 65 0d 0a 2a 2a 20 74 68 65 20 6d 6b 6f   See..** the mko
25021 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70  pcodeh.awk scrip
25022 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  t for additional
25023 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a   information...*
25024 2a 0d 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61 74  *..** Documentat
25025 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20 6f  ion about VDBE o
25026 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72 61  pcodes is genera
25027 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 20  ted by scanning 
25028 74 68 69 73 20 66 69 6c 65 0d 0a 2a 2a 20 66 6f  this file..** fo
25029 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20  r lines of that 
2502a 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a  contain "Opcode:
2502b 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e  ".  That line an
2502c 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  d all subsequent
2502d 0d 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e  ..** comment lin
2502e 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74  es are used in t
2502f 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66  he generation of
25030 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c   the opcode.html
25031 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0d 0a   documentation..
25032 2a 2a 20 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ** file...**..**
25033 20 53 55 4d 4d 41 52 59 3a 0d 0a 2a 2a 0d 0a 2a   SUMMARY:..**..*
25034 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67  *     Formatting
25035 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
25036 20 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63   scripts that sc
25037 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0d 0a 2a  an this file...*
25038 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76  *     Do not dev
25039 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f  iate from the fo
2503a 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63  rmatting style c
2503b 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e  urrently in use.
2503c 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
2503d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2503e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2503f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25041 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ***/..../* Opcod
25042 65 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20  e:  Goto * P2 * 
25043 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 75  * *..**..** An u
25044 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d  nconditional jum
25045 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e  p to address P2.
25046 0d 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 69 6e  ..** The next in
25047 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 75 74  struction execut
25048 65 64 20 77 69 6c 6c 20 62 65 20 0d 0a 2a 2a 20  ed will be ..** 
25049 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65 78  the one at index
2504a 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65 67   P2 from the beg
2504b 69 6e 6e 69 6e 67 20 6f 66 0d 0a 2a 2a 20 74 68  inning of..** th
2504c 65 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f 0d 0a  e program...*/..
2504d 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20  case OP_Goto: { 
2504e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
2504f 75 6d 70 20 2a 2f 0d 0a 20 20 43 48 45 43 4b 5f  ump */..  CHECK_
25050 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0d 0a  FOR_INTERRUPT;..
25051 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
25052 20 31 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d   1;..  break;..}
25053 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  ..../* Opcode:  
25054 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20  Gosub P1 P2 * * 
25055 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65 20  *..**..** Write 
25056 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64 72  the current addr
25057 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65  ess onto registe
25058 72 20 50 31 0d 0a 2a 2a 20 61 6e 64 20 74 68 65  r P1..** and the
25059 6e 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73  n jump to addres
2505a 73 20 50 32 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  s P2...*/..case 
2505b 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 20 20 20  OP_Gosub: {     
2505c 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
2505d 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
2505e 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
2505f 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a 20  1<=p->nMem );.. 
25060 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
25061 70 2d 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72  p->p1];..  asser
25062 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  t( (pIn1->flags 
25063 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b  & MEM_Dyn)==0 );
25064 0d 0a 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68  ..  memAboutToCh
25065 61 6e 67 65 28 70 2c 20 70 49 6e 31 29 3b 0d 0a  ange(p, pIn1);..
25066 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
25067 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 70 49 6e 31  MEM_Int;..  pIn1
25068 2d 3e 75 2e 69 20 3d 20 70 63 3b 0d 0a 20 20 52  ->u.i = pc;..  R
25069 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
2506a 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0d 0a 20  p->p1, pIn1);.. 
2506b 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
2506c 31 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d  1;..  break;..}.
2506d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52  .../* Opcode:  R
2506e 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a  eturn P1 * * * *
2506f 0d 0a 2a 2a 0d 0a 2a 2a 20 4a 75 6d 70 20 74 6f  ..**..** Jump to
25070 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
25071 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ction after the 
25072 61 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73  address in regis
25073 74 65 72 20 50 31 2e 0d 0a 2a 2f 0d 0a 63 61 73  ter P1...*/..cas
25074 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20  e OP_Return: {  
25075 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
25076 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d  */..  pIn1 = &aM
25077 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  em[pOp->p1];..  
25078 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
25079 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
2507a 0d 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49  ..  pc = (int)pI
2507b 6e 31 2d 3e 75 2e 69 3b 0d 0a 20 20 62 72 65 61  n1->u.i;..  brea
2507c 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f  k;..}..../* Opco
2507d 64 65 3a 20 20 59 69 65 6c 64 20 50 31 20 2a 20  de:  Yield P1 * 
2507e 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 77  * * *..**..** Sw
2507f 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63  ap the program c
25080 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 65 20  ounter with the 
25081 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
25082 72 20 50 31 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  r P1...*/..case 
25083 4f 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20  OP_Yield: {     
25084 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f         /* in1 */
25085 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
25086 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25087 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0d 0a  d into u.aa */..
25088 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0d 0a 23    int pcDest;..#
25089 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
2508a 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
2508b 6e 74 6f 20 75 2e 61 61 20 2a 2f 0d 0a 20 20 70  nto u.aa */..  p
2508c 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
2508d 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  >p1];..  assert(
2508e 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
2508f 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0d 0a  MEM_Dyn)==0 );..
25090 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
25091 4d 45 4d 5f 49 6e 74 3b 0d 0a 20 20 75 2e 61 61  MEM_Int;..  u.aa
25092 2e 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70  .pcDest = (int)p
25093 49 6e 31 2d 3e 75 2e 69 3b 0d 0a 20 20 70 49 6e  In1->u.i;..  pIn
25094 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0d 0a 20 20  1->u.i = pc;..  
25095 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
25096 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0d 0a  Op->p1, pIn1);..
25097 20 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 65    pc = u.aa.pcDe
25098 73 74 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d  st;..  break;..}
25099 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  ..../* Opcode:  
2509a 48 61 6c 74 49 66 4e 75 6c 6c 20 20 50 31 20 50  HaltIfNull  P1 P
2509b 32 20 50 33 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a  2 P3 P4 *..**..*
2509c 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75  * Check the valu
2509d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  e in register P3
2509e 2e 20 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c  .  If it is NULL
2509f 20 74 68 65 6e 20 48 61 6c 74 20 75 73 69 6e 67   then Halt using
250a0 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 50  ..** parameter P
250a1 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20 61 73  1, P2, and P4 as
250a2 20 69 66 20 74 68 69 73 20 77 65 72 65 20 61 20   if this were a 
250a3 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Halt instruction
250a4 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a 20 76 61  .  If the..** va
250a5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
250a6 50 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  P3 is not NULL, 
250a7 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
250a8 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  e is a no-op...*
250a9 2f 0d 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 49  /..case OP_HaltI
250aa 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a  fNull: {      /*
250ab 20 69 6e 33 20 2a 2f 0d 0a 20 20 70 49 6e 33 20   in3 */..  pIn3 
250ac 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  = &aMem[pOp->p3]
250ad 3b 0d 0a 20 20 69 66 28 20 28 70 49 6e 33 2d 3e  ;..  if( (pIn3->
250ae 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
250af 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20  )==0 ) break;.. 
250b0 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68   /* Fall through
250b1 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f   into OP_Halt */
250b2 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
250b3 3a 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20  :  Halt P1 P2 * 
250b4 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 69  P4 *..**..** Exi
250b5 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20  t immediately.  
250b6 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73  All open cursors
250b7 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65 64  , etc are closed
250b8 0d 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  ..** automatical
250b9 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 69  ly...**..** P1 i
250ba 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64  s the result cod
250bb 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
250bc 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71  lite3_exec(), sq
250bd 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0d 0a  lite3_reset(),..
250be 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69  ** or sqlite3_fi
250bf 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61  nalize().  For a
250c0 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68   normal halt, th
250c1 69 73 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c  is should be SQL
250c2 49 54 45 5f 4f 4b 20 28 30 29 2e 0d 0a 2a 2a 20  ITE_OK (0)...** 
250c3 46 6f 72 20 65 72 72 6f 72 73 2c 20 69 74 20 63  For errors, it c
250c4 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72  an be some other
250c5 20 76 61 6c 75 65 2e 20 20 49 66 20 50 31 21 3d   value.  If P1!=
250c6 30 20 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 64  0 then P2 will d
250c7 65 74 65 72 6d 69 6e 65 0d 0a 2a 2a 20 77 68 65  etermine..** whe
250c8 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72  ther or not to r
250c9 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75 72 72  ollback the curr
250ca 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
250cb 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63    Do not rollbac
250cc 6b 0d 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f  k..** if P2==OE_
250cd 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c  Fail. Do the rol
250ce 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f  lback if P2==OE_
250cf 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50 32  Rollback.  If P2
250d0 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0d 0a 2a 2a 20  ==OE_Abort,..** 
250d1 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c  then back out al
250d2 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 68  l changes that h
250d3 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 75 72  ave occurred dur
250d4 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 74 69  ing this executi
250d5 6f 6e 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 56 44  on of the..** VD
250d6 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72  BE, but do not r
250d7 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
250d8 73 61 63 74 69 6f 6e 2e 20 0d 0a 2a 2a 0d 0a 2a  saction. ..**..*
250d9 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e  * If P4 is not n
250da 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61  ull then it is a
250db 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
250dc 73 74 72 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  string...**..** 
250dd 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c  There is an impl
250de 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22  ied "Halt 0 0 0"
250df 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73   instruction ins
250e0 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72  erted at the ver
250e1 79 20 65 6e 64 20 6f 66 0d 0a 2a 2a 20 65 76 65  y end of..** eve
250e2 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20  ry program.  So 
250e3 61 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20  a jump past the 
250e4 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
250e5 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0d   of the program.
250e6 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20  .** is the same 
250e7 61 73 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c  as executing Hal
250e8 74 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  t...*/..case OP_
250e9 48 61 6c 74 3a 20 7b 0d 0a 20 20 69 66 28 20 70  Halt: {..  if( p
250ea 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 4f  Op->p1==SQLITE_O
250eb 4b 20 26 26 20 70 2d 3e 70 46 72 61 6d 65 20 29  K && p->pFrame )
250ec 7b 0d 0a 20 20 20 20 2f 2a 20 48 61 6c 74 20 74  {..    /* Halt t
250ed 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20  he sub-program. 
250ee 52 65 74 75 72 6e 20 63 6f 6e 74 72 6f 6c 20 74  Return control t
250ef 6f 20 74 68 65 20 70 61 72 65 6e 74 20 66 72 61  o the parent fra
250f0 6d 65 2e 20 2a 2f 0d 0a 20 20 20 20 56 64 62 65  me. */..    Vdbe
250f1 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20  Frame *pFrame = 
250f2 70 2d 3e 70 46 72 61 6d 65 3b 0d 0a 20 20 20 20  p->pFrame;..    
250f3 70 2d 3e 70 46 72 61 6d 65 20 3d 20 70 46 72 61  p->pFrame = pFra
250f4 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 0d 0a 20 20  me->pParent;..  
250f5 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d 3b 0d 0a    p->nFrame--;..
250f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
250f7 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d  etChanges(db, p-
250f8 3e 6e 43 68 61 6e 67 65 29 3b 0d 0a 20 20 20 20  >nChange);..    
250f9 70 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  pc = sqlite3Vdbe
250fa 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72  FrameRestore(pFr
250fb 61 6d 65 29 3b 0d 0a 20 20 20 20 6c 61 73 74 52  ame);..    lastR
250fc 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52  owid = db->lastR
250fd 6f 77 69 64 3b 0d 0a 20 20 20 20 69 66 28 20 70  owid;..    if( p
250fe 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72  Op->p2==OE_Ignor
250ff 65 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49  e ){..      /* I
25100 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 20 69 73  nstruction pc is
25101 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
25102 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
25103 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 0d 0a 20   sub-program .. 
25104 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c       ** currentl
25105 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64 2e 20  y being halted. 
25106 49 66 20 74 68 65 20 70 32 20 69 6e 73 74 72 75  If the p2 instru
25107 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f 50  ction of this OP
25108 5f 48 61 6c 74 0d 0a 20 20 20 20 20 20 2a 2a 20  _Halt..      ** 
25109 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73  instruction is s
2510a 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c  et to OE_Ignore,
2510b 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72   then the sub-pr
2510c 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69 6e  ogram is throwin
2510d 67 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49  g..      ** an I
2510e 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e  GNORE exception.
2510f 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75   In this case ju
25110 6d 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73  mp to the addres
25111 73 20 73 70 65 63 69 66 69 65 64 0d 0a 20 20 20  s specified..   
25112 20 20 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20     ** as the p2 
25113 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f  of the calling O
25114 50 5f 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0d 0a  P_Program.  */..
25115 20 20 20 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f        pc = p->aO
25116 70 5b 70 63 5d 2e 70 32 2d 31 3b 0d 0a 20 20 20  p[pc].p2-1;..   
25117 20 7d 0d 0a 20 20 20 20 61 4f 70 20 3d 20 70 2d   }..    aOp = p-
25118 3e 61 4f 70 3b 0d 0a 20 20 20 20 61 4d 65 6d 20  >aOp;..    aMem 
25119 3d 20 70 2d 3e 61 4d 65 6d 3b 0d 0a 20 20 20 20  = p->aMem;..    
2511a 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  break;..  }.... 
2511b 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31   p->rc = pOp->p1
2511c 3b 0d 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74  ;..  p->errorAct
2511d 69 6f 6e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70  ion = (u8)pOp->p
2511e 32 3b 0d 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63  2;..  p->pc = pc
2511f 3b 0d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  ;..  if( pOp->p4
25120 2e 7a 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  .z ){..    asser
25121 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45  t( p->rc!=SQLITE
25122 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69  _OK );..    sqli
25123 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
25124 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
25125 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0d  s", pOp->p4.z);.
25126 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73  .    testcase( s
25127 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
25128 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d 0a 20  ig.xLog!=0 );.. 
25129 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 70     sqlite3_log(p
2512a 4f 70 2d 3e 70 31 2c 20 22 61 62 6f 72 74 20 61  Op->p1, "abort a
2512b 74 20 25 64 20 69 6e 20 5b 25 73 5d 3a 20 25 73  t %d in [%s]: %s
2512c 22 2c 20 70 63 2c 20 70 2d 3e 7a 53 71 6c 2c 20  ", pc, p->zSql, 
2512d 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0d 0a 20 20 7d  pOp->p4.z);..  }
2512e 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 29  else if( p->rc )
2512f 7b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  {..    testcase(
25130 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
25131 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d  nfig.xLog!=0 );.
25132 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67  .    sqlite3_log
25133 28 70 4f 70 2d 3e 70 31 2c 20 22 63 6f 6e 73 74  (pOp->p1, "const
25134 72 61 69 6e 74 20 66 61 69 6c 65 64 20 61 74 20  raint failed at 
25135 25 64 20 69 6e 20 5b 25 73 5d 22 2c 20 70 63 2c  %d in [%s]", pc,
25136 20 70 2d 3e 7a 53 71 6c 29 3b 0d 0a 20 20 7d 0d   p->zSql);..  }.
25137 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
25138 64 62 65 48 61 6c 74 28 70 29 3b 0d 0a 20 20 61  dbeHalt(p);..  a
25139 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
2513a 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51  E_BUSY || rc==SQ
2513b 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
2513c 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0d 0a  QLITE_ERROR );..
2513d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2513e 5f 42 55 53 59 20 29 7b 0d 0a 20 20 20 20 70 2d  _BUSY ){..    p-
2513f 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
25140 45 5f 42 55 53 59 3b 0d 0a 20 20 7d 65 6c 73 65  E_BUSY;..  }else
25141 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  {..    assert( r
25142 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
25143 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f  p->rc==SQLITE_CO
25144 4e 53 54 52 41 49 4e 54 20 29 3b 0d 0a 20 20 20  NSTRAINT );..   
25145 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
25146 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44  ITE_OK || db->nD
25147 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b  eferredCons>0 );
25148 0d 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63  ..    rc = p->rc
25149 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20   ? SQLITE_ERROR 
2514a 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a  : SQLITE_DONE;..
2514b 20 20 7d 0d 0a 20 20 67 6f 74 6f 20 76 64 62 65    }..  goto vdbe
2514c 5f 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f  _return;..}..../
2514d 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65  * Opcode: Intege
2514e 72 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a  r P1 P2 * * *..*
2514f 2a 0d 0a 2a 2a 20 54 68 65 20 33 32 2d 62 69 74  *..** The 32-bit
25150 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 50   integer value P
25151 31 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  1 is written int
25152 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0d 0a  o register P2...
25153 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65  */..case OP_Inte
25154 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 2f  ger: {         /
25155 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
25156 65 20 2a 2f 0d 0a 20 20 70 4f 75 74 2d 3e 75 2e  e */..  pOut->u.
25157 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a 20 20  i = pOp->p1;..  
25158 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20  break;..}..../* 
25159 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20  Opcode: Int64 * 
2515a 50 32 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a  P2 * P4 *..**..*
2515b 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
2515c 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e  r to a 64-bit in
2515d 74 65 67 65 72 20 76 61 6c 75 65 2e 0d 0a 2a 2a  teger value...**
2515e 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75   Write that valu
2515f 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
25160 50 32 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  P2...*/..case OP
25161 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 20 20  _Int64: {       
25162 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
25163 65 6c 65 61 73 65 20 2a 2f 0d 0a 20 20 61 73 73  elease */..  ass
25164 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36  ert( pOp->p4.pI6
25165 34 21 3d 30 20 29 3b 0d 0a 20 20 70 4f 75 74 2d  4!=0 );..  pOut-
25166 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e  >u.i = *pOp->p4.
25167 70 49 36 34 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d  pI64;..  break;.
25168 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  .}....#ifndef SQ
25169 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2516a 4e 47 5f 50 4f 49 4e 54 0d 0a 2f 2a 20 4f 70 63  NG_POINT../* Opc
2516b 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a  ode: Real * P2 *
2516c 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34   P4 *..**..** P4
2516d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2516e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69   a 64-bit floati
2516f 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0d  ng point value..
25170 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76  .** Write that v
25171 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
25172 65 72 20 50 32 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  er P2...*/..case
25173 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20   OP_Real: {     
25174 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
25175 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32  s TK_FLOAT, out2
25176 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a  -prerelease */..
25177 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
25178 4d 45 4d 5f 52 65 61 6c 3b 0d 0a 20 20 61 73 73  MEM_Real;..  ass
25179 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e  ert( !sqlite3IsN
2517a 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61  aN(*pOp->p4.pRea
2517b 6c 29 20 29 3b 0d 0a 20 20 70 4f 75 74 2d 3e 72  l) );..  pOut->r
2517c 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61   = *pOp->p4.pRea
2517d 6c 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d  l;..  break;..}.
2517e 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 4f 70  .#endif..../* Op
2517f 63 6f 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20  code: String8 * 
25180 50 32 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a  P2 * P4 *..**..*
25181 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61  * P4 points to a
25182 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20   nul terminated 
25183 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 54 68  UTF-8 string. Th
25184 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61  is opcode is tra
25185 6e 73 66 6f 72 6d 65 64 20 0d 0a 2a 2a 20 69 6e  nsformed ..** in
25186 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 20  to an OP_String 
25187 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 65  before it is exe
25188 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 69  cuted for the fi
25189 72 73 74 20 74 69 6d 65 2e 0d 0a 2a 2f 0d 0a 63  rst time...*/..c
2518a 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20  ase OP_String8: 
2518b 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d  {         /* sam
2518c 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20  e as TK_STRING, 
2518d 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
2518e 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
2518f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0d 0a 20  p->p4.z!=0 );.. 
25190 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f   pOp->opcode = O
25191 50 5f 53 74 72 69 6e 67 3b 0d 0a 20 20 70 4f 70  P_String;..  pOp
25192 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 53 74  ->p1 = sqlite3St
25193 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a  rlen30(pOp->p4.z
25194 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  );....#ifndef SQ
25195 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d  LITE_OMIT_UTF16.
25196 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21  .  if( encoding!
25197 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0d  =SQLITE_UTF8 ){.
25198 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
25199 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
2519a 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  Out, pOp->p4.z, 
2519b 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
2519c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2519d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
2519e 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f  LITE_TOOBIG ) go
2519f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a 20 20 20  to too_big;..   
251a0 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
251a1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
251a2 65 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20  eEncoding(pOut, 
251a3 65 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f  encoding) ) goto
251a4 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20 61 73   no_mem;..    as
251a5 73 65 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c  sert( pOut->zMal
251a6 6c 6f 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0d  loc==pOut->z );.
251a7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 75  .    assert( pOu
251a8 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  t->flags & MEM_D
251a9 79 6e 20 29 3b 0d 0a 20 20 20 20 70 4f 75 74 2d  yn );..    pOut-
251aa 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a 20  >zMalloc = 0;.. 
251ab 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c     pOut->flags |
251ac 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0d 0a 20  = MEM_Static;.. 
251ad 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26     pOut->flags &
251ae 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0d 0a 20 20 20  = ~MEM_Dyn;..   
251af 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65   if( pOp->p4type
251b0 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0d  ==P4_DYNAMIC ){.
251b1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
251b2 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34  Free(db, pOp->p4
251b3 2e 7a 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  .z);..    }..   
251b4 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
251b5 34 5f 44 59 4e 41 4d 49 43 3b 0d 0a 20 20 20 20  4_DYNAMIC;..    
251b6 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74  pOp->p4.z = pOut
251b7 2d 3e 7a 3b 0d 0a 20 20 20 20 70 4f 70 2d 3e 70  ->z;..    pOp->p
251b8 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0d 0a 20 20  1 = pOut->n;..  
251b9 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 66 28  }..#endif..  if(
251ba 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69   pOp->p1>db->aLi
251bb 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
251bc 5f 4c 45 4e 47 54 48 5d 20 29 7b 0d 0a 20 20 20  _LENGTH] ){..   
251bd 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a   goto too_big;..
251be 20 20 7d 0d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74    }..  /* Fall t
251bf 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
251c0 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69  xt case, OP_Stri
251c1 6e 67 20 2a 2f 0d 0a 7d 0d 0a 20 20 0d 0a 2f 2a  ng */..}..  ../*
251c2 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 20   Opcode: String 
251c3 50 31 20 50 32 20 2a 20 50 34 20 2a 0d 0a 2a 2a  P1 P2 * P4 *..**
251c4 0d 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  ..** The string 
251c5 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67  value P4 of leng
251c6 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73  th P1 (bytes) is
251c7 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
251c8 74 65 72 20 50 32 2e 0d 0a 2a 2f 0d 0a 63 61 73  ter P2...*/..cas
251c9 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20  e OP_String: {  
251ca 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
251cb 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 20  prerelease */.. 
251cc 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
251cd 2e 7a 21 3d 30 20 29 3b 0d 0a 20 20 70 4f 75 74  .z!=0 );..  pOut
251ce 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74  ->flags = MEM_St
251cf 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d  r|MEM_Static|MEM
251d0 5f 54 65 72 6d 3b 0d 0a 20 20 70 4f 75 74 2d 3e  _Term;..  pOut->
251d1 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0d 0a  z = pOp->p4.z;..
251d2 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d    pOut->n = pOp-
251d3 3e 70 31 3b 0d 0a 20 20 70 4f 75 74 2d 3e 65 6e  >p1;..  pOut->en
251d4 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0d 0a 20  c = encoding;.. 
251d5 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
251d6 53 49 5a 45 28 70 4f 75 74 29 3b 0d 0a 20 20 62  SIZE(pOut);..  b
251d7 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f  reak;..}..../* O
251d8 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32  pcode: Null * P2
251d9 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20   P3 * *..**..** 
251da 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e 74  Write a NULL int
251db 6f 20 72 65 67 69 73 74 65 72 73 20 50 32 2e 20  o registers P2. 
251dc 20 49 66 20 50 33 20 67 72 65 61 74 65 72 20 74   If P3 greater t
251dd 68 61 6e 20 50 32 2c 20 74 68 65 6e 20 61 6c 73  han P2, then als
251de 6f 20 77 72 69 74 65 0d 0a 2a 2a 20 4e 55 4c 4c  o write..** NULL
251df 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
251e0 33 20 61 6e 64 20 65 76 65 72 20 72 65 67 69 73  3 and ever regis
251e1 74 65 72 20 69 6e 20 62 65 74 77 65 65 6e 20 50  ter in between P
251e2 32 20 61 6e 64 20 50 33 2e 20 20 49 66 20 50 33  2 and P3.  If P3
251e3 0d 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61  ..** is less tha
251e4 6e 20 50 32 20 28 74 79 70 69 63 61 6c 6c 79 20  n P2 (typically 
251e5 50 33 20 69 73 20 7a 65 72 6f 29 20 74 68 65 6e  P3 is zero) then
251e6 20 6f 6e 6c 79 20 72 65 67 69 73 74 65 72 20 50   only register P
251e7 32 20 69 73 0d 0a 2a 2a 20 73 65 74 20 74 6f 20  2 is..** set to 
251e8 4e 55 4c 4c 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f  NULL..*/..case O
251e9 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  P_Null: {       
251ea 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
251eb 65 6c 65 61 73 65 20 2a 2f 0d 0a 23 69 66 20 30  elease */..#if 0
251ec 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
251ed 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
251ee 75 2e 61 62 20 2a 2f 0d 0a 20 20 69 6e 74 20 63  u.ab */..  int c
251ef 6e 74 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  nt;..#endif /* l
251f0 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
251f1 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a  oved into u.ab *
251f2 2f 0d 0a 20 20 75 2e 61 62 2e 63 6e 74 20 3d 20  /..  u.ab.cnt = 
251f3 70 4f 70 2d 3e 70 33 2d 70 4f 70 2d 3e 70 32 3b  pOp->p3-pOp->p2;
251f4 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
251f5 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d  >p3<=p->nMem );.
251f6 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
251f7 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20 77 68   MEM_Null;..  wh
251f8 69 6c 65 28 20 75 2e 61 62 2e 63 6e 74 3e 30 20  ile( u.ab.cnt>0 
251f9 29 7b 0d 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0d  ){..    pOut++;.
251fa 0a 20 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43  .    memAboutToC
251fb 68 61 6e 67 65 28 70 2c 20 70 4f 75 74 29 3b 0d  hange(p, pOut);.
251fc 0a 20 20 20 20 56 64 62 65 4d 65 6d 52 65 6c 65  .    VdbeMemRele
251fd 61 73 65 28 70 4f 75 74 29 3b 0d 0a 20 20 20 20  ase(pOut);..    
251fe 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
251ff 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20 20 20 75 2e 61  M_Null;..    u.a
25200 62 2e 63 6e 74 2d 2d 3b 0d 0a 20 20 7d 0d 0a 20  b.cnt--;..  }.. 
25201 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a   break;..}......
25202 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20  /* Opcode: Blob 
25203 50 31 20 50 32 20 2a 20 50 34 0d 0a 2a 2a 0d 0a  P1 P2 * P4..**..
25204 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20  ** P4 points to 
25205 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50  a blob of data P
25206 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53  1 bytes long.  S
25207 74 6f 72 65 20 74 68 69 73 0d 0a 2a 2a 20 62 6c  tore this..** bl
25208 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ob in register P
25209 32 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  2...*/..case OP_
2520a 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20  Blob: {         
2520b 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
2520c 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 20 20  rerelease */..  
2520d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20  assert( pOp->p1 
2520e 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  <= SQLITE_MAX_LE
2520f 4e 47 54 48 20 29 3b 0d 0a 20 20 73 71 6c 69 74  NGTH );..  sqlit
25210 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
25211 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  pOut, pOp->p4.z,
25212 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b   pOp->p1, 0, 0);
25213 0d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20  ..  pOut->enc = 
25214 65 6e 63 6f 64 69 6e 67 3b 0d 0a 20 20 55 50 44  encoding;..  UPD
25215 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
25216 28 70 4f 75 74 29 3b 0d 0a 20 20 62 72 65 61 6b  (pOut);..  break
25217 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
25218 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 50  e: Variable P1 P
25219 32 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  2 * P4 *..**..**
2521a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61   Transfer the va
2521b 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61  lues of bound pa
2521c 72 61 6d 65 74 65 72 20 50 31 20 69 6e 74 6f 20  rameter P1 into 
2521d 72 65 67 69 73 74 65 72 20 50 32 0d 0a 2a 2a 0d  register P2..**.
2521e 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d  .** If the param
2521f 65 74 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74  eter is named, t
25220 68 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70  hen its name app
25221 65 61 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50  ears in P4 and P
25222 33 3d 3d 31 2e 0d 0a 2a 2a 20 54 68 65 20 50 34  3==1...** The P4
25223 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62   value is used b
25224 79 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  y sqlite3_bind_p
25225 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e  arameter_name().
25226 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 56 61  ..*/..case OP_Va
25227 72 69 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20  riable: {       
25228 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
25229 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 23 69 66 20  release */..#if 
2522a 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
2522b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
2522c 20 75 2e 61 63 20 2a 2f 0d 0a 20 20 4d 65 6d 20   u.ac */..  Mem 
2522d 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f 2a 20  *pVar;       /* 
2522e 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 61 6e  Value being tran
2522f 73 66 65 72 72 65 64 20 2a 2f 0d 0a 23 65 6e 64  sferred */..#end
25230 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
25231 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
25232 20 75 2e 61 63 20 2a 2f 0d 0a 0d 0a 20 20 61 73   u.ac */....  as
25233 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
25234 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
25235 56 61 72 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  Var );..  assert
25236 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c  ( pOp->p4.z==0 |
25237 7c 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 70 2d 3e  | pOp->p4.z==p->
25238 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d  azVar[pOp->p1-1]
25239 20 29 3b 0d 0a 20 20 75 2e 61 63 2e 70 56 61 72   );..  u.ac.pVar
2523a 20 3d 20 26 70 2d 3e 61 56 61 72 5b 70 4f 70 2d   = &p->aVar[pOp-
2523b 3e 70 31 20 2d 20 31 5d 3b 0d 0a 20 20 69 66 28  >p1 - 1];..  if(
2523c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
2523d 6f 6f 42 69 67 28 75 2e 61 63 2e 70 56 61 72 29  ooBig(u.ac.pVar)
2523e 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 74 6f   ){..    goto to
2523f 6f 5f 62 69 67 3b 0d 0a 20 20 7d 0d 0a 20 20 73  o_big;..  }..  s
25240 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
25241 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75  llowCopy(pOut, u
25242 2e 61 63 2e 70 56 61 72 2c 20 4d 45 4d 5f 53 74  .ac.pVar, MEM_St
25243 61 74 69 63 29 3b 0d 0a 20 20 55 50 44 41 54 45  atic);..  UPDATE
25244 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
25245 75 74 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  ut);..  break;..
25246 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
25247 4d 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20  Move P1 P2 P3 * 
25248 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 76 65 20 74  *..**..** Move t
25249 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67  he values in reg
2524a 69 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d  ister P1..P1+P3-
2524b 31 20 6f 76 65 72 20 69 6e 74 6f 0d 0a 2a 2a 20  1 over into..** 
2524c 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e 50 32  registers P2..P2
2524d 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 65 72  +P3-1.  Register
2524e 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72  s P1..P1+P1-1 ar
2524f 65 0d 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69  e..** left holdi
25250 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69  ng a NULL.  It i
25251 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 72  s an error for r
25252 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0d 0a  egister ranges..
25253 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61  ** P1..P1+P3-1 a
25254 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74  nd P2..P2+P3-1 t
25255 6f 20 6f 76 65 72 6c 61 70 2e 0d 0a 2a 2f 0d 0a  o overlap...*/..
25256 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0d  case OP_Move: {.
25257 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
25258 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
25259 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0d 0a 20   into u.ad */.. 
2525a 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
2525b 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72    /* Holding var
2525c 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61  iable for alloca
2525d 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0d 0a 20  ted memory */.. 
2525e 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
2525f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
25260 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f  egisters left to
25261 20 63 6f 70 79 20 2a 2f 0d 0a 20 20 69 6e 74 20   copy */..  int 
25262 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
25263 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
25264 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20   from */..  int 
25265 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p2;          /* 
25266 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
25267 20 74 6f 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f   to */..#endif /
25268 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25269 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
2526a 64 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 61 64 2e 6e  d */....  u.ad.n
2526b 20 3d 20 70 4f 70 2d 3e 70 33 3b 0d 0a 20 20 75   = pOp->p3;..  u
2526c 2e 61 64 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31  .ad.p1 = pOp->p1
2526d 3b 0d 0a 20 20 75 2e 61 64 2e 70 32 20 3d 20 70  ;..  u.ad.p2 = p
2526e 4f 70 2d 3e 70 32 3b 0d 0a 20 20 61 73 73 65 72  Op->p2;..  asser
2526f 74 28 20 75 2e 61 64 2e 6e 3e 30 20 26 26 20 75  t( u.ad.n>0 && u
25270 2e 61 64 2e 70 31 3e 30 20 26 26 20 75 2e 61 64  .ad.p1>0 && u.ad
25271 2e 70 32 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65  .p2>0 );..  asse
25272 72 74 28 20 75 2e 61 64 2e 70 31 2b 75 2e 61 64  rt( u.ad.p1+u.ad
25273 2e 6e 3c 3d 75 2e 61 64 2e 70 32 20 7c 7c 20 75  .n<=u.ad.p2 || u
25274 2e 61 64 2e 70 32 2b 75 2e 61 64 2e 6e 3c 3d 75  .ad.p2+u.ad.n<=u
25275 2e 61 64 2e 70 31 20 29 3b 0d 0a 0d 0a 20 20 70  .ad.p1 );....  p
25276 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 64  In1 = &aMem[u.ad
25277 2e 70 31 5d 3b 0d 0a 20 20 70 4f 75 74 20 3d 20  .p1];..  pOut = 
25278 26 61 4d 65 6d 5b 75 2e 61 64 2e 70 32 5d 3b 0d  &aMem[u.ad.p2];.
25279 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 64 2e 6e  .  while( u.ad.n
2527a 2d 2d 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  -- ){..    asser
2527b 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65 6d 5b 70  t( pOut<=&aMem[p
2527c 2d 3e 6e 4d 65 6d 5d 20 29 3b 0d 0a 20 20 20 20  ->nMem] );..    
2527d 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26 61  assert( pIn1<=&a
2527e 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0d  Mem[p->nMem] );.
2527f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d  .    assert( mem
25280 49 73 56 61 6c 69 64 28 70 49 6e 31 29 20 29 3b  IsValid(pIn1) );
25281 0d 0a 20 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f  ..    memAboutTo
25282 43 68 61 6e 67 65 28 70 2c 20 70 4f 75 74 29 3b  Change(p, pOut);
25283 0d 0a 20 20 20 20 75 2e 61 64 2e 7a 4d 61 6c 6c  ..    u.ad.zMall
25284 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  oc = pOut->zMall
25285 6f 63 3b 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  oc;..    pOut->z
25286 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a 20 20 20  Malloc = 0;..   
25287 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
25288 6f 76 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b  ove(pOut, pIn1);
25289 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2528a 44 45 42 55 47 0d 0a 20 20 20 20 69 66 28 20 70  DEBUG..    if( p
2528b 4f 75 74 2d 3e 70 53 63 6f 70 79 46 72 6f 6d 3e  Out->pScopyFrom>
2528c 3d 26 61 4d 65 6d 5b 75 2e 61 64 2e 70 31 5d 20  =&aMem[u.ad.p1] 
2528d 26 26 20 70 4f 75 74 2d 3e 70 53 63 6f 70 79 46  && pOut->pScopyF
2528e 72 6f 6d 3c 26 61 4d 65 6d 5b 75 2e 61 64 2e 70  rom<&aMem[u.ad.p
2528f 31 2b 70 4f 70 2d 3e 70 33 5d 20 29 7b 0d 0a 20  1+pOp->p3] ){.. 
25290 20 20 20 20 20 70 4f 75 74 2d 3e 70 53 63 6f 70       pOut->pScop
25291 79 46 72 6f 6d 20 2b 3d 20 75 2e 61 64 2e 70 31  yFrom += u.ad.p1
25292 20 2d 20 70 4f 70 2d 3e 70 32 3b 0d 0a 20 20 20   - pOp->p2;..   
25293 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
25294 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pIn1->zMalloc = 
25295 75 2e 61 64 2e 7a 4d 61 6c 6c 6f 63 3b 0d 0a 20  u.ad.zMalloc;.. 
25296 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
25297 45 28 75 2e 61 64 2e 70 32 2b 2b 2c 20 70 4f 75  E(u.ad.p2++, pOu
25298 74 29 3b 0d 0a 20 20 20 20 70 49 6e 31 2b 2b 3b  t);..    pIn1++;
25299 0d 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0d 0a 20  ..    pOut++;.. 
2529a 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d   }..  break;..}.
2529b 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  .../* Opcode: Co
2529c 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a  py P1 P2 * * *..
2529d 2a 2a 0d 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f  **..** Make a co
2529e 70 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  py of register P
2529f 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  1 into register 
252a0 50 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  P2...**..** This
252a1 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b   instruction mak
252a2 65 73 20 61 20 64 65 65 70 20 63 6f 70 79 20 6f  es a deep copy o
252a3 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 41 20  f the value.  A 
252a4 64 75 70 6c 69 63 61 74 65 0d 0a 2a 2a 20 69 73  duplicate..** is
252a5 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 73 74 72   made of any str
252a6 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f 6e 73  ing or blob cons
252a7 74 61 6e 74 2e 20 20 53 65 65 20 61 6c 73 6f 20  tant.  See also 
252a8 4f 50 5f 53 43 6f 70 79 2e 0d 0a 2a 2f 0d 0a 63  OP_SCopy...*/..c
252a9 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 20 20  ase OP_Copy: {  
252aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
252ab 31 2c 20 6f 75 74 32 20 2a 2f 0d 0a 20 20 70 49  1, out2 */..  pI
252ac 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
252ad 70 31 5d 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26  p1];..  pOut = &
252ae 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a  aMem[pOp->p2];..
252af 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d    assert( pOut!=
252b0 70 49 6e 31 20 29 3b 0d 0a 20 20 73 71 6c 69 74  pIn1 );..  sqlit
252b1 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
252b2 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
252b3 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0d 0a 20 20   MEM_Ephem);..  
252b4 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70  Deephemeralize(p
252b5 4f 75 74 29 3b 0d 0a 20 20 52 45 47 49 53 54 45  Out);..  REGISTE
252b6 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
252b7 20 70 4f 75 74 29 3b 0d 0a 20 20 62 72 65 61 6b   pOut);..  break
252b8 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
252b9 65 3a 20 53 43 6f 70 79 20 50 31 20 50 32 20 2a  e: SCopy P1 P2 *
252ba 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 61 6b   * *..**..** Mak
252bb 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79  e a shallow copy
252bc 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
252bd 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
252be 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  ...**..** This i
252bf 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73  nstruction makes
252c0 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20   a shallow copy 
252c1 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49  of the value.  I
252c2 66 20 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20  f the value..** 
252c3 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  is a string or b
252c4 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  lob, then the co
252c5 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69  py is only a poi
252c6 6e 74 65 72 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  nter to the..** 
252c7 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e  original and hen
252c8 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e  ce if the origin
252c9 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 69  al changes so wi
252ca 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0d 0a 2a 2a  ll the copy...**
252cb 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f   Worse, if the o
252cc 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c  riginal is deall
252cd 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79  ocated, the copy
252ce 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64   becomes invalid
252cf 2e 0d 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70  ...** Thus the p
252d0 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72  rogram must guar
252d1 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f  antee that the o
252d2 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74  riginal will not
252d3 20 63 68 61 6e 67 65 0d 0a 2a 2a 20 64 75 72 69   change..** duri
252d4 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  ng the lifetime 
252d5 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73  of the copy.  Us
252d6 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b  e OP_Copy to mak
252d7 65 20 61 20 63 6f 6d 70 6c 65 74 65 0d 0a 2a 2a  e a complete..**
252d8 20 63 6f 70 79 2e 0d 0a 2a 2f 0d 0a 63 61 73 65   copy...*/..case
252d9 20 4f 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20   OP_SCopy: {    
252da 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20          /* in1, 
252db 6f 75 74 32 20 2a 2f 0d 0a 20 20 70 49 6e 31 20  out2 */..  pIn1 
252dc 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
252dd 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ;..  pOut = &aMe
252de 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 61  m[pOp->p2];..  a
252df 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e  ssert( pOut!=pIn
252e0 31 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  1 );..  sqlite3V
252e1 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
252e2 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45  y(pOut, pIn1, ME
252e3 4d 5f 45 70 68 65 6d 29 3b 0d 0a 23 69 66 64 65  M_Ephem);..#ifde
252e4 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a  f SQLITE_DEBUG..
252e5 20 20 69 66 28 20 70 4f 75 74 2d 3e 70 53 63 6f    if( pOut->pSco
252e6 70 79 46 72 6f 6d 3d 3d 30 20 29 20 70 4f 75 74  pyFrom==0 ) pOut
252e7 2d 3e 70 53 63 6f 70 79 46 72 6f 6d 20 3d 20 70  ->pScopyFrom = p
252e8 49 6e 31 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  In1;..#endif..  
252e9 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
252ea 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0d 0a  Op->p2, pOut);..
252eb 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
252ec 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c 74  * Opcode: Result
252ed 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d  Row P1 P2 * * *.
252ee 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 67 69  .**..** The regi
252ef 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68  sters P1 through
252f0 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e   P1+P2-1 contain
252f1 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
252f2 0d 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68  ..** results. Th
252f3 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73  is opcode causes
252f4 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65   the sqlite3_ste
252f5 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d  p() call to term
252f6 69 6e 61 74 65 0d 0a 2a 2a 20 77 69 74 68 20 61  inate..** with a
252f7 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74  n SQLITE_ROW ret
252f8 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20  urn code and it 
252f9 73 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69  sets up the sqli
252fa 74 65 33 5f 73 74 6d 74 0d 0a 2a 2a 20 73 74 72  te3_stmt..** str
252fb 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64  ucture to provid
252fc 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  e access to the 
252fd 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20 61 73  top P1 values as
252fe 20 74 68 65 20 72 65 73 75 6c 74 0d 0a 2a 2a 20   the result..** 
252ff 72 6f 77 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f  row...*/..case O
25300 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0d 0a  P_ResultRow: {..
25301 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
25302 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
25303 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0d 0a 20 20  into u.ae */..  
25304 4d 65 6d 20 2a 70 4d 65 6d 3b 0d 0a 20 20 69 6e  Mem *pMem;..  in
25305 74 20 69 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  t i;..#endif /* 
25306 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
25307 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 65 20  moved into u.ae 
25308 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  */..  assert( p-
25309 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70  >nResColumn==pOp
2530a 2d 3e 70 32 20 29 3b 0d 0a 20 20 61 73 73 65 72  ->p2 );..  asser
2530b 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0d  t( pOp->p1>0 );.
2530c 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
2530d 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  p1+pOp->p2<=p->n
2530e 4d 65 6d 2b 31 20 29 3b 0d 0a 0d 0a 20 20 2f 2a  Mem+1 );....  /*
2530f 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65   If this stateme
25310 6e 74 20 68 61 73 20 76 69 6f 6c 61 74 65 64 20  nt has violated 
25311 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67  immediate foreig
25312 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
25313 73 2c 20 64 6f 0d 0a 20 20 2a 2a 20 6e 6f 74 20  s, do..  ** not 
25314 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
25315 72 20 6f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69  r of rows modifi
25316 65 64 2e 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52  ed. And do not R
25317 45 4c 45 41 53 45 20 74 68 65 20 73 74 61 74 65  ELEASE the state
25318 6d 65 6e 74 0d 0a 20 20 2a 2a 20 74 72 61 6e 73  ment..  ** trans
25319 61 63 74 69 6f 6e 2e 20 49 74 20 6e 65 65 64 73  action. It needs
2531a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
2531b 63 6b 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 53  ck.  */..  if( S
2531c 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
2531d 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b  sqlite3VdbeCheck
2531e 46 6b 28 70 2c 20 30 29 29 20 29 7b 0d 0a 20 20  Fk(p, 0)) ){..  
2531f 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 66 6c    assert( db->fl
25320 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74  ags&SQLITE_Count
25321 52 6f 77 73 20 29 3b 0d 0a 20 20 20 20 61 73 73  Rows );..    ass
25322 65 72 74 28 20 70 2d 3e 75 73 65 73 53 74 6d 74  ert( p->usesStmt
25323 4a 6f 75 72 6e 61 6c 20 29 3b 0d 0a 20 20 20 20  Journal );..    
25324 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  break;..  }.... 
25325 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54   /* If the SQLIT
25326 45 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67  E_CountRows flag
25327 20 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69 74   is set in sqlit
25328 65 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  e3.flags mask, t
25329 68 65 6e 0d 0a 20 20 2a 2a 20 44 4d 4c 20 73 74  hen..  ** DML st
2532a 61 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20  atements invoke 
2532b 74 68 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72  this opcode to r
2532c 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
2532d 20 6f 66 20 72 6f 77 73 0d 0a 20 20 2a 2a 20 6d   of rows..  ** m
2532e 6f 64 69 66 69 65 64 20 74 6f 20 74 68 65 20 75  odified to the u
2532f 73 65 72 2e 20 54 68 69 73 20 69 73 20 74 68 65  ser. This is the
25330 20 6f 6e 6c 79 20 77 61 79 20 74 68 61 74 20 61   only way that a
25331 20 56 4d 20 74 68 61 74 0d 0a 20 20 2a 2a 20 6f   VM that..  ** o
25332 70 65 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74  pens a statement
25333 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79   transaction may
25334 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63   invoke this opc
25335 6f 64 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ode...  **..  **
25336 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 69 73   In case this is
25337 20 73 75 63 68 20 61 20 73 74 61 74 65 6d 65 6e   such a statemen
25338 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 74 61  t, close any sta
25339 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
2533a 6f 6e 0d 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20  on..  ** opened 
2533b 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f 72  by this VM befor
2533c 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74  e returning cont
2533d 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 2e  rol to the user.
2533e 20 54 68 69 73 20 69 73 20 74 6f 0d 0a 20 20 2a   This is to..  *
2533f 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 74  * ensure that st
25340 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
25341 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20  ions are always 
25342 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72  nested, not over
25343 6c 61 70 70 69 6e 67 2e 0d 0a 20 20 2a 2a 20 49  lapping...  ** I
25344 66 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65  f the open state
25345 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
25346 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68   is not closed h
25347 65 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73  ere, then the us
25348 65 72 0d 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65  er..  ** may ste
25349 70 20 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61  p another VM tha
2534a 74 20 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20  t opens its own 
2534b 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
2534c 63 74 69 6f 6e 2e 20 54 68 69 73 0d 0a 20 20 2a  ction. This..  *
2534d 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 6f 76  * may lead to ov
2534e 65 72 6c 61 70 70 69 6e 67 20 73 74 61 74 65 6d  erlapping statem
2534f 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ent transactions
25350 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68  ...  **..  ** Th
25351 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
25352 73 61 63 74 69 6f 6e 20 69 73 20 6e 65 76 65 72  saction is never
25353 20 61 20 74 6f 70 2d 6c 65 76 65 6c 20 74 72 61   a top-level tra
25354 6e 73 61 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65  nsaction.  Hence
25355 0d 0a 20 20 2a 2a 20 74 68 65 20 52 45 4c 45 41  ..  ** the RELEA
25356 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 20 63 61  SE call below ca
25357 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0d 0a 20  n never fail... 
25358 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
25359 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->iStatement==0 
2535a 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  || db->flags&SQL
2535b 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b  ITE_CountRows );
2535c 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2535d 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
2535e 6e 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f  nt(p, SAVEPOINT_
2535f 52 45 4c 45 41 53 45 29 3b 0d 0a 20 20 69 66 28  RELEASE);..  if(
25360 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54   NEVER(rc!=SQLIT
25361 45 5f 4f 4b 29 20 29 7b 0d 0a 20 20 20 20 62 72  E_OK) ){..    br
25362 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  eak;..  }....  /
25363 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  * Invalidate all
25364 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f   ephemeral curso
25365 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0d  r row caches */.
25366 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d  .  p->cacheCtr =
25367 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b 20   (p->cacheCtr + 
25368 32 29 7c 31 3b 0d 0a 0d 0a 20 20 2f 2a 20 4d 61  2)|1;....  /* Ma
25369 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75  ke sure the resu
2536a 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  lts of the curre
2536b 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20  nt row are \000 
2536c 74 65 72 6d 69 6e 61 74 65 64 0d 0a 20 20 2a 2a  terminated..  **
2536d 20 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73   and have an ass
2536e 69 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65  igned type.  The
2536f 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d   results are de-
25370 65 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73  ephemeralized as
25371 0d 0a 20 20 2a 2a 20 61 20 73 69 64 65 20 65 66  ..  ** a side ef
25372 66 65 63 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75  fect...  */..  u
25373 2e 61 65 2e 70 4d 65 6d 20 3d 20 70 2d 3e 70 52  .ae.pMem = p->pR
25374 65 73 75 6c 74 53 65 74 20 3d 20 26 61 4d 65 6d  esultSet = &aMem
25375 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 66 6f  [pOp->p1];..  fo
25376 72 28 75 2e 61 65 2e 69 3d 30 3b 20 75 2e 61 65  r(u.ae.i=0; u.ae
25377 2e 69 3c 70 4f 70 2d 3e 70 32 3b 20 75 2e 61 65  .i<pOp->p2; u.ae
25378 2e 69 2b 2b 29 7b 0d 0a 20 20 20 20 61 73 73 65  .i++){..    asse
25379 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 26  rt( memIsValid(&
2537a 75 2e 61 65 2e 70 4d 65 6d 5b 75 2e 61 65 2e 69  u.ae.pMem[u.ae.i
2537b 5d 29 20 29 3b 0d 0a 20 20 20 20 44 65 65 70 68  ]) );..    Deeph
2537c 65 6d 65 72 61 6c 69 7a 65 28 26 75 2e 61 65 2e  emeralize(&u.ae.
2537d 70 4d 65 6d 5b 75 2e 61 65 2e 69 5d 29 3b 0d 0a  pMem[u.ae.i]);..
2537e 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61      assert( (u.a
2537f 65 2e 70 4d 65 6d 5b 75 2e 61 65 2e 69 5d 2e 66  e.pMem[u.ae.i].f
25380 6c 61 67 73 20 26 20 4d 45 4d 5f 45 70 68 65 6d  lags & MEM_Ephem
25381 29 3d 3d 30 0d 0a 20 20 20 20 20 20 20 20 20 20  )==0..          
25382 20 20 7c 7c 20 28 75 2e 61 65 2e 70 4d 65 6d 5b    || (u.ae.pMem[
25383 75 2e 61 65 2e 69 5d 2e 66 6c 61 67 73 20 26 20  u.ae.i].flags & 
25384 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
25385 62 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 73  b))==0 );..    s
25386 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
25387 54 65 72 6d 69 6e 61 74 65 28 26 75 2e 61 65 2e  Terminate(&u.ae.
25388 70 4d 65 6d 5b 75 2e 61 65 2e 69 5d 29 3b 0d 0a  pMem[u.ae.i]);..
25389 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
2538a 65 6d 53 74 6f 72 65 54 79 70 65 28 26 75 2e 61  emStoreType(&u.a
2538b 65 2e 70 4d 65 6d 5b 75 2e 61 65 2e 69 5d 29 3b  e.pMem[u.ae.i]);
2538c 0d 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ..    REGISTER_T
2538d 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61  RACE(pOp->p1+u.a
2538e 65 2e 69 2c 20 26 75 2e 61 65 2e 70 4d 65 6d 5b  e.i, &u.ae.pMem[
2538f 75 2e 61 65 2e 69 5d 29 3b 0d 0a 20 20 7d 0d 0a  u.ae.i]);..  }..
25390 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
25391 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f  Failed ) goto no
25392 5f 6d 65 6d 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65  _mem;....  /* Re
25393 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0d  turn SQLITE_ROW.
25394 0a 20 20 2a 2f 0d 0a 20 20 70 2d 3e 70 63 20 3d  .  */..  p->pc =
25395 20 70 63 20 2b 20 31 3b 0d 0a 20 20 72 63 20 3d   pc + 1;..  rc =
25396 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0d 0a 20 20   SQLITE_ROW;..  
25397 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
25398 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
25399 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50 32 20  e: Concat P1 P2 
2539a 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 41  P3 * *..**..** A
2539b 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20 72  dd the text in r
2539c 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20  egister P1 onto 
2539d 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
2539e 65 78 74 20 69 6e 0d 0a 2a 2a 20 72 65 67 69 73  ext in..** regis
2539f 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65  ter P2 and store
253a0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
253a1 65 67 69 73 74 65 72 20 50 33 2e 0d 0a 2a 2a 20  egister P3...** 
253a2 49 66 20 65 69 74 68 65 72 20 74 68 65 20 50 31  If either the P1
253a3 20 6f 72 20 50 32 20 74 65 78 74 20 61 72 65 20   or P2 text are 
253a4 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20  NULL then store 
253a5 4e 55 4c 4c 20 69 6e 20 50 33 2e 0d 0a 2a 2a 0d  NULL in P3...**.
253a6 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c  .**   P3 = P2 ||
253a7 20 50 31 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74 20 69   P1..**..** It i
253a8 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31  s illegal for P1
253a9 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68   and P3 to be th
253aa 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e  e same register.
253ab 20 53 6f 6d 65 74 69 6d 65 73 2c 0d 0a 2a 2a 20   Sometimes,..** 
253ac 69 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d  if P3 is the sam
253ad 65 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32  e register as P2
253ae 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  , the implementa
253af 74 69 6f 6e 20 69 73 20 61 62 6c 65 0d 0a 2a 2a  tion is able..**
253b0 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63   to avoid a memc
253b1 70 79 28 29 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  py()...*/..case 
253b2 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20  OP_Concat: {    
253b3 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
253b4 73 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31  s TK_CONCAT, in1
253b5 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0d 0a  , in2, out3 */..
253b6 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
253b7 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
253b8 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0d 0a 20 20  into u.af */..  
253b9 69 36 34 20 6e 42 79 74 65 3b 0d 0a 23 65 6e 64  i64 nByte;..#end
253ba 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
253bb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
253bc 20 75 2e 61 66 20 2a 2f 0d 0a 0d 0a 20 20 70 49   u.af */....  pI
253bd 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
253be 70 31 5d 3b 0d 0a 20 20 70 49 6e 32 20 3d 20 26  p1];..  pIn2 = &
253bf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a  aMem[pOp->p2];..
253c0 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70    pOut = &aMem[p
253c1 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 61 73 73 65  Op->p3];..  asse
253c2 72 74 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29  rt( pIn1!=pOut )
253c3 3b 0d 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  ;..  if( (pIn1->
253c4 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c  flags | pIn2->fl
253c5 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  ags) & MEM_Null 
253c6 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ){..    sqlite3V
253c7 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
253c8 75 74 29 3b 0d 0a 20 20 20 20 62 72 65 61 6b 3b  ut);..    break;
253c9 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 45 78 70  ..  }..  if( Exp
253ca 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c 7c  andBlob(pIn1) ||
253cb 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32   ExpandBlob(pIn2
253cc 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  ) ) goto no_mem;
253cd 0d 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 49  ..  Stringify(pI
253ce 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a  n1, encoding);..
253cf 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32    Stringify(pIn2
253d0 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20 20  , encoding);..  
253d1 75 2e 61 66 2e 6e 42 79 74 65 20 3d 20 70 49 6e  u.af.nByte = pIn
253d2 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0d  1->n + pIn2->n;.
253d3 0a 20 20 69 66 28 20 75 2e 61 66 2e 6e 42 79 74  .  if( u.af.nByt
253d4 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  e>db->aLimit[SQL
253d5 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
253d6 5d 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 74  ] ){..    goto t
253d7 6f 6f 5f 62 69 67 3b 0d 0a 20 20 7d 0d 0a 20 20  oo_big;..  }..  
253d8 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
253d9 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0d 0a  Out, MEM_Str);..
253da 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
253db 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28  eMemGrow(pOut, (
253dc 69 6e 74 29 75 2e 61 66 2e 6e 42 79 74 65 2b 32  int)u.af.nByte+2
253dd 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b  , pOut==pIn2) ){
253de 0d 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ..    goto no_me
253df 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  m;..  }..  if( p
253e0 4f 75 74 21 3d 70 49 6e 32 20 29 7b 0d 0a 20 20  Out!=pIn2 ){..  
253e1 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a    memcpy(pOut->z
253e2 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d  , pIn2->z, pIn2-
253e3 3e 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d  >n);..  }..  mem
253e4 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 6e  cpy(&pOut->z[pIn
253e5 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c 20  2->n], pIn1->z, 
253e6 70 49 6e 31 2d 3e 6e 29 3b 0d 0a 20 20 70 4f 75  pIn1->n);..  pOu
253e7 74 2d 3e 7a 5b 75 2e 61 66 2e 6e 42 79 74 65 5d  t->z[u.af.nByte]
253e8 20 3d 20 30 3b 0d 0a 20 20 70 4f 75 74 2d 3e 7a   = 0;..  pOut->z
253e9 5b 75 2e 61 66 2e 6e 42 79 74 65 2b 31 5d 20 3d  [u.af.nByte+1] =
253ea 20 30 3b 0d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61   0;..  pOut->fla
253eb 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0d  gs |= MEM_Term;.
253ec 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
253ed 74 29 75 2e 61 66 2e 6e 42 79 74 65 3b 0d 0a 20  t)u.af.nByte;.. 
253ee 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63   pOut->enc = enc
253ef 6f 64 69 6e 67 3b 0d 0a 20 20 55 50 44 41 54 45  oding;..  UPDATE
253f0 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
253f1 75 74 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  ut);..  break;..
253f2 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
253f3 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a  Add P1 P2 P3 * *
253f4 0d 0a 2a 2a 0d 0a 2a 2a 20 41 64 64 20 74 68 65  ..**..** Add the
253f5 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
253f6 65 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c  er P1 to the val
253f7 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
253f8 32 0d 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20  2..** and store 
253f9 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
253fa 67 69 73 74 65 72 20 50 33 2e 0d 0a 2a 2a 20 49  gister P3...** I
253fb 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
253fc 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
253fd 6c 74 20 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d  lt is NULL...*/.
253fe 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c 74  ./* Opcode: Mult
253ff 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a 20  iply P1 P2 P3 * 
25400 2a 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 75  *..**..**..** Mu
25401 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65  ltiply the value
25402 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
25403 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  by the value in 
25404 72 65 67 69 73 74 65 72 20 50 32 0d 0a 2a 2a 20  register P2..** 
25405 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
25406 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
25407 20 50 33 2e 0d 0a 2a 2a 20 49 66 20 65 69 74 68   P3...** If eith
25408 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
25409 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
2540a 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70  NULL...*/../* Op
2540b 63 6f 64 65 3a 20 53 75 62 74 72 61 63 74 20 50  code: Subtract P
2540c 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d  1 P2 P3 * *..**.
2540d 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 68 65  .** Subtract the
2540e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
2540f 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 20 76  er P1 from the v
25410 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
25411 20 50 32 0d 0a 2a 2a 20 61 6e 64 20 73 74 6f 72   P2..** and stor
25412 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
25413 72 65 67 69 73 74 65 72 20 50 33 2e 0d 0a 2a 2a  register P3...**
25414 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
25415 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
25416 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0d 0a 2a  sult is NULL...*
25417 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69  /../* Opcode: Di
25418 76 69 64 65 20 50 31 20 50 32 20 50 33 20 2a 20  vide P1 P2 P3 * 
25419 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 44 69 76 69 64 65  *..**..** Divide
2541a 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
2541b 67 69 73 74 65 72 20 50 31 20 62 79 20 74 68 65  gister P1 by the
2541c 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
2541d 65 72 20 50 32 0d 0a 2a 2a 20 61 6e 64 20 73 74  er P2..** and st
2541e 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
2541f 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 28 50  n register P3 (P
25420 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 74 68 65  3=P2/P1). If the
25421 20 76 61 6c 75 65 20 69 6e 20 0d 0a 2a 2a 20 72   value in ..** r
25422 65 67 69 73 74 65 72 20 50 31 20 69 73 20 7a 65  egister P1 is ze
25423 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ro, then the res
25424 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20  ult is NULL. If 
25425 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
25426 0d 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 72  ..** NULL, the r
25427 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0d 0a  esult is NULL...
25428 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  */../* Opcode: R
25429 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50  emainder P1 P2 P
2542a 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 43 6f  3 * *..**..** Co
2542b 6d 70 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e  mpute the remain
2542c 64 65 72 20 61 66 74 65 72 20 69 6e 74 65 67 65  der after intege
2542d 72 20 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68  r division of th
2542e 65 20 76 61 6c 75 65 20 69 6e 0d 0a 2a 2a 20 72  e value in..** r
2542f 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 68  egister P1 by th
25430 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
25431 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65  ter P2 and store
25432 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 50   the result in P
25433 33 2e 20 0d 0a 2a 2a 20 49 66 20 74 68 65 20 76  3. ..** If the v
25434 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
25435 20 50 32 20 69 73 20 7a 65 72 6f 20 74 68 65 20   P2 is zero the 
25436 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0d  result is NULL..
25437 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70  .** If either op
25438 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74  erand is NULL, t
25439 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
2543a 4c 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  L...*/..case OP_
2543b 41 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  Add:            
2543c 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
2543d 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20  s TK_PLUS, in1, 
2543e 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0d 0a 63 61  in2, out3 */..ca
2543f 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20  se OP_Subtract: 
25440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25441 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53  same as TK_MINUS
25442 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
25443 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4d 75 6c   */..case OP_Mul
25444 74 69 70 6c 79 3a 20 20 20 20 20 20 20 20 20 20  tiply:          
25445 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
25446 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32  K_STAR, in1, in2
25447 2c 20 6f 75 74 33 20 2a 2f 0d 0a 63 61 73 65 20  , out3 */..case 
25448 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 20 20  OP_Divide:      
25449 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
2544a 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69  e as TK_SLASH, i
2544b 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
2544c 0d 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e  ..case OP_Remain
2544d 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20  der: {          
2544e 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
2544f 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  EM, in1, in2, ou
25450 74 33 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f 2a  t3 */..#if 0  /*
25451 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
25452 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67   moved into u.ag
25453 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73   */..  int flags
25454 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e  ;      /* Combin
25455 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66  ed MEM_* flags f
25456 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20  rom both inputs 
25457 2a 2f 0d 0a 20 20 69 36 34 20 69 41 3b 20 20 20  */..  i64 iA;   
25458 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
25459 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f   value of left o
2545a 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 69 36 34  perand */..  i64
2545b 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   iB;         /* 
2545c 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66  Integer value of
2545d 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
2545e 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20  /..  double rA; 
2545f 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
25460 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
25461 6e 64 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c 65 20  nd */..  double 
25462 72 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c  rB;      /* Real
25463 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20   value of right 
25464 6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 23 65 6e 64  operand */..#end
25465 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
25466 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
25467 20 75 2e 61 67 20 2a 2f 0d 0a 0d 0a 20 20 70 49   u.ag */....  pI
25468 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
25469 70 31 5d 3b 0d 0a 20 20 61 70 70 6c 79 4e 75 6d  p1];..  applyNum
2546a 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e  ericAffinity(pIn
2546b 31 29 3b 0d 0a 20 20 70 49 6e 32 20 3d 20 26 61  1);..  pIn2 = &a
2546c 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20  Mem[pOp->p2];.. 
2546d 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
2546e 69 6e 69 74 79 28 70 49 6e 32 29 3b 0d 0a 20 20  inity(pIn2);..  
2546f 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pOut = &aMem[pOp
25470 2d 3e 70 33 5d 3b 0d 0a 20 20 75 2e 61 67 2e 66  ->p3];..  u.ag.f
25471 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61  lags = pIn1->fla
25472 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
25473 3b 0d 0a 20 20 69 66 28 20 28 75 2e 61 67 2e 66  ;..  if( (u.ag.f
25474 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
25475 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  !=0 ) goto arith
25476 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
25477 6e 75 6c 6c 3b 0d 0a 20 20 69 66 28 20 28 70 49  null;..  if( (pI
25478 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 32  n1->flags & pIn2
25479 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
2547a 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0d 0a  t)==MEM_Int ){..
2547b 20 20 20 20 75 2e 61 67 2e 69 41 20 3d 20 70 49      u.ag.iA = pI
2547c 6e 31 2d 3e 75 2e 69 3b 0d 0a 20 20 20 20 75 2e  n1->u.i;..    u.
2547d 61 67 2e 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e  ag.iB = pIn2->u.
2547e 69 3b 0d 0a 20 20 20 20 73 77 69 74 63 68 28 20  i;..    switch( 
2547f 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0d 0a  pOp->opcode ){..
25480 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64        case OP_Ad
25481 64 3a 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  d:       if( sql
25482 69 74 65 33 41 64 64 49 6e 74 36 34 28 26 75 2e  ite3AddInt64(&u.
25483 61 67 2e 69 42 2c 75 2e 61 67 2e 69 41 29 20 29  ag.iB,u.ag.iA) )
25484 20 67 6f 74 6f 20 66 70 5f 6d 61 74 68 3b 20 20   goto fp_math;  
25485 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
25486 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20  se OP_Subtract: 
25487 20 69 66 28 20 73 71 6c 69 74 65 33 53 75 62 49   if( sqlite3SubI
25488 6e 74 36 34 28 26 75 2e 61 67 2e 69 42 2c 75 2e  nt64(&u.ag.iB,u.
25489 61 67 2e 69 41 29 20 29 20 67 6f 74 6f 20 66 70  ag.iA) ) goto fp
2548a 5f 6d 61 74 68 3b 20 20 62 72 65 61 6b 3b 0d 0a  _math;  break;..
2548b 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75        case OP_Mu
2548c 6c 74 69 70 6c 79 3a 20 20 69 66 28 20 73 71 6c  ltiply:  if( sql
2548d 69 74 65 33 4d 75 6c 49 6e 74 36 34 28 26 75 2e  ite3MulInt64(&u.
2548e 61 67 2e 69 42 2c 75 2e 61 67 2e 69 41 29 20 29  ag.iB,u.ag.iA) )
2548f 20 67 6f 74 6f 20 66 70 5f 6d 61 74 68 3b 20 20   goto fp_math;  
25490 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
25491 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0d  se OP_Divide: {.
25492 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
25493 67 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61  g.iA==0 ) goto a
25494 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
25495 5f 69 73 5f 6e 75 6c 6c 3b 0d 0a 20 20 20 20 20  _is_null;..     
25496 20 20 20 69 66 28 20 75 2e 61 67 2e 69 41 3d 3d     if( u.ag.iA==
25497 2d 31 20 26 26 20 75 2e 61 67 2e 69 42 3d 3d 53  -1 && u.ag.iB==S
25498 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20  MALLEST_INT64 ) 
25499 67 6f 74 6f 20 66 70 5f 6d 61 74 68 3b 0d 0a 20  goto fp_math;.. 
2549a 20 20 20 20 20 20 20 75 2e 61 67 2e 69 42 20 2f         u.ag.iB /
2549b 3d 20 75 2e 61 67 2e 69 41 3b 0d 0a 20 20 20 20  = u.ag.iA;..    
2549c 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
2549d 20 20 7d 0d 0a 20 20 20 20 20 20 64 65 66 61 75    }..      defau
2549e 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 69  lt: {..        i
2549f 66 28 20 75 2e 61 67 2e 69 41 3d 3d 30 20 29 20  f( u.ag.iA==0 ) 
254a0 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
254a1 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0d  result_is_null;.
254a2 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
254a3 67 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 67 2e  g.iA==-1 ) u.ag.
254a4 69 41 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  iA = 1;..       
254a5 20 75 2e 61 67 2e 69 42 20 25 3d 20 75 2e 61 67   u.ag.iB %= u.ag
254a6 2e 69 41 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  .iA;..        br
254a7 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
254a8 20 20 20 7d 0d 0a 20 20 20 20 70 4f 75 74 2d 3e     }..    pOut->
254a9 75 2e 69 20 3d 20 75 2e 61 67 2e 69 42 3b 0d 0a  u.i = u.ag.iB;..
254aa 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
254ab 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
254ac 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 66 70  );..  }else{..fp
254ad 5f 6d 61 74 68 3a 0d 0a 20 20 20 20 75 2e 61 67  _math:..    u.ag
254ae 2e 72 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  .rA = sqlite3Vdb
254af 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29  eRealValue(pIn1)
254b0 3b 0d 0a 20 20 20 20 75 2e 61 67 2e 72 42 20 3d  ;..    u.ag.rB =
254b1 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
254b2 56 61 6c 75 65 28 70 49 6e 32 29 3b 0d 0a 20 20  Value(pIn2);..  
254b3 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
254b4 70 63 6f 64 65 20 29 7b 0d 0a 20 20 20 20 20 20  pcode ){..      
254b5 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20  case OP_Add:    
254b6 20 20 20 20 20 75 2e 61 67 2e 72 42 20 2b 3d 20       u.ag.rB += 
254b7 75 2e 61 67 2e 72 41 3b 20 20 20 20 20 20 20 62  u.ag.rA;       b
254b8 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
254b9 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20  e OP_Subtract:  
254ba 20 20 75 2e 61 67 2e 72 42 20 2d 3d 20 75 2e 61    u.ag.rB -= u.a
254bb 67 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 61  g.rA;       brea
254bc 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 4f  k;..      case O
254bd 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75  P_Multiply:    u
254be 2e 61 67 2e 72 42 20 2a 3d 20 75 2e 61 67 2e 72  .ag.rB *= u.ag.r
254bf 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  A;       break;.
254c0 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44  .      case OP_D
254c1 69 76 69 64 65 3a 20 7b 0d 0a 20 20 20 20 20 20  ivide: {..      
254c2 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
254c3 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
254c4 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
254c5 4f 49 4e 54 2e 2e 2e 20 2a 2f 0d 0a 20 20 20 20  OINT... */..    
254c6 20 20 20 20 69 66 28 20 75 2e 61 67 2e 72 41 3d      if( u.ag.rA=
254c7 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 67 6f 74  =(double)0 ) got
254c8 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
254c9 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0d 0a 20 20  ult_is_null;..  
254ca 20 20 20 20 20 20 75 2e 61 67 2e 72 42 20 2f 3d        u.ag.rB /=
254cb 20 75 2e 61 67 2e 72 41 3b 0d 0a 20 20 20 20 20   u.ag.rA;..     
254cc 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
254cd 20 7d 0d 0a 20 20 20 20 20 20 64 65 66 61 75 6c   }..      defaul
254ce 74 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 75 2e  t: {..        u.
254cf 61 67 2e 69 41 20 3d 20 28 69 36 34 29 75 2e 61  ag.iA = (i64)u.a
254d0 67 2e 72 41 3b 0d 0a 20 20 20 20 20 20 20 20 75  g.rA;..        u
254d1 2e 61 67 2e 69 42 20 3d 20 28 69 36 34 29 75 2e  .ag.iB = (i64)u.
254d2 61 67 2e 72 42 3b 0d 0a 20 20 20 20 20 20 20 20  ag.rB;..        
254d3 69 66 28 20 75 2e 61 67 2e 69 41 3d 3d 30 20 29  if( u.ag.iA==0 )
254d4 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63   goto arithmetic
254d5 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b  _result_is_null;
254d6 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  ..        if( u.
254d7 61 67 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 67  ag.iA==-1 ) u.ag
254d8 2e 69 41 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  .iA = 1;..      
254d9 20 20 75 2e 61 67 2e 72 42 20 3d 20 28 64 6f 75    u.ag.rB = (dou
254da 62 6c 65 29 28 75 2e 61 67 2e 69 42 20 25 20 75  ble)(u.ag.iB % u
254db 2e 61 67 2e 69 41 29 3b 0d 0a 20 20 20 20 20 20  .ag.iA);..      
254dc 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
254dd 7d 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 64 65 66  }..    }..#ifdef
254de 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
254df 41 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 20  ATING_POINT..   
254e0 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61   pOut->u.i = u.a
254e1 67 2e 72 42 3b 0d 0a 20 20 20 20 4d 65 6d 53 65  g.rB;..    MemSe
254e2 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
254e3 4d 45 4d 5f 49 6e 74 29 3b 0d 0a 23 65 6c 73 65  MEM_Int);..#else
254e4 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
254e5 33 49 73 4e 61 4e 28 75 2e 61 67 2e 72 42 29 20  3IsNaN(u.ag.rB) 
254e6 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 61  ){..      goto a
254e7 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
254e8 5f 69 73 5f 6e 75 6c 6c 3b 0d 0a 20 20 20 20 7d  _is_null;..    }
254e9 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20  ..    pOut->r = 
254ea 75 2e 61 67 2e 72 42 3b 0d 0a 20 20 20 20 4d 65  u.ag.rB;..    Me
254eb 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
254ec 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0d 0a 20  t, MEM_Real);.. 
254ed 20 20 20 69 66 28 20 28 75 2e 61 67 2e 66 6c 61     if( (u.ag.fla
254ee 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d  gs & MEM_Real)==
254ef 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  0 ){..      sqli
254f0 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
254f1 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0d 0a 20  finity(pOut);.. 
254f2 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20     }..#endif..  
254f3 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 61  }..  break;....a
254f4 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
254f5 5f 69 73 5f 6e 75 6c 6c 3a 0d 0a 20 20 73 71 6c  _is_null:..  sql
254f6 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
254f7 6c 6c 28 70 4f 75 74 29 3b 0d 0a 20 20 62 72 65  ll(pOut);..  bre
254f8 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
254f9 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a  ode: CollSeq * *
254fa 20 50 34 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34 20 69   P4..**..** P4 i
254fb 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
254fc 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e   CollSeq struct.
254fd 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c   If the next cal
254fe 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63  l to a user func
254ff 74 69 6f 6e 0d 0a 2a 2a 20 6f 72 20 61 67 67 72  tion..** or aggr
25500 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69  egate calls sqli
25501 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65  te3GetFuncCollSe
25502 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74  q(), this collat
25503 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c  ion sequence wil
25504 6c 0d 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65  l..** be returne
25505 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  d. This is used 
25506 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  by the built-in 
25507 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64  min(), max() and
25508 20 6e 75 6c 6c 69 66 28 29 0d 0a 2a 2a 20 66 75   nullif()..** fu
25509 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  nctions...**..**
2550a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 75   The interface u
2550b 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
2550c 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2550d 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20   aforementioned 
2550e 66 75 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 74 6f  functions..** to
2550f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f   retrieve the co
25510 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
25511 20 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 63   set by this opc
25512 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  ode is not avail
25513 61 62 6c 65 0d 0a 2a 2a 20 70 75 62 6c 69 63 6c  able..** publicl
25514 79 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20  y, only to user 
25515 66 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65  functions define
25516 64 20 69 6e 20 66 75 6e 63 2e 63 2e 0d 0a 2a 2f  d in func.c...*/
25517 0d 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65  ..case OP_CollSe
25518 71 3a 20 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  q: {..  assert( 
25519 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
2551a 43 4f 4c 4c 53 45 51 20 29 3b 0d 0a 20 20 62 72  COLLSEQ );..  br
2551b 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70  eak;..}..../* Op
2551c 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50  code: Function P
2551d 31 20 50 32 20 50 33 20 50 34 20 50 35 0d 0a 2a  1 P2 P3 P4 P5..*
2551e 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75  *..** Invoke a u
2551f 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34  ser function (P4
25520 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
25521 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75   a Function stru
25522 63 74 75 72 65 20 74 68 61 74 0d 0a 2a 2a 20 64  cture that..** d
25523 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74  efines the funct
25524 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67  ion) with P5 arg
25525 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f  uments taken fro
25526 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e  m register P2 an
25527 64 0d 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  d..** successors
25528 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66  .  The result of
25529 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
2552a 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
2552b 74 65 72 20 50 33 2e 0d 0a 2a 2a 20 52 65 67 69  ter P3...** Regi
2552c 73 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74  ster P3 must not
2552d 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   be one of the f
2552e 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0d  unction inputs..
2552f 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 69 73 20 61 20  .**..** P1 is a 
25530 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 69  32-bit bitmask i
25531 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65  ndicating whethe
25532 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61 72  r or not each ar
25533 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0d 0a  gument to the ..
25534 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  ** function was 
25535 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65  determined to be
25536 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d   constant at com
25537 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68  pile time. If th
25538 65 20 66 69 72 73 74 0d 0a 2a 2a 20 61 72 67 75  e first..** argu
25539 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e  ment was constan
2553a 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20  t then bit 0 of 
2553b 50 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 20  P1 is set. This 
2553c 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
2553d 6d 69 6e 65 0d 0a 2a 2a 20 77 68 65 74 68 65 72  mine..** whether
2553e 20 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f 63   meta data assoc
2553f 69 61 74 65 64 20 77 69 74 68 20 61 20 75 73 65  iated with a use
25540 72 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  r function argum
25541 65 6e 74 20 75 73 69 6e 67 20 74 68 65 0d 0a 2a  ent using the..*
25542 2a 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  * sqlite3_set_au
25543 78 64 61 74 61 28 29 20 41 50 49 20 6d 61 79 20  xdata() API may 
25544 62 65 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e  be safely retain
25545 65 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  ed until the nex
25546 74 0d 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  t..** invocation
25547 20 6f 66 20 74 68 69 73 20 6f 70 63 6f 64 65 2e   of this opcode.
25548 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
25549 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 20 41  o: AggStep and A
2554a 67 67 46 69 6e 61 6c 0d 0a 2a 2f 0d 0a 63 61 73  ggFinal..*/..cas
2554b 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b  e OP_Function: {
2554c 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
2554d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
2554e 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0d 0a  d into u.ah */..
2554f 20 20 69 6e 74 20 69 3b 0d 0a 20 20 4d 65 6d 20    int i;..  Mem 
25550 2a 70 41 72 67 3b 0d 0a 20 20 73 71 6c 69 74 65  *pArg;..  sqlite
25551 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0d 0a  3_context ctx;..
25552 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25553 2a 2a 61 70 56 61 6c 3b 0d 0a 20 20 69 6e 74 20  **apVal;..  int 
25554 6e 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  n;..#endif /* lo
25555 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
25556 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f  ved into u.ah */
25557 0d 0a 0d 0a 20 20 75 2e 61 68 2e 6e 20 3d 20 70  ....  u.ah.n = p
25558 4f 70 2d 3e 70 35 3b 0d 0a 20 20 75 2e 61 68 2e  Op->p5;..  u.ah.
25559 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67  apVal = p->apArg
2555a 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  ;..  assert( u.a
2555b 68 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 61 68 2e  h.apVal || u.ah.
2555c 6e 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  n==0 );..  asser
2555d 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
2555e 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
2555f 20 29 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61   );..  pOut = &a
25560 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20  Mem[pOp->p3];.. 
25561 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67   memAboutToChang
25562 65 28 70 2c 20 70 4f 75 74 29 3b 0d 0a 0d 0a 20  e(p, pOut);.... 
25563 20 61 73 73 65 72 74 28 20 75 2e 61 68 2e 6e 3d   assert( u.ah.n=
25564 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30  =0 || (pOp->p2>0
25565 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 68   && pOp->p2+u.ah
25566 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29  .n<=p->nMem+1) )
25567 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ;..  assert( pOp
25568 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20  ->p3<pOp->p2 || 
25569 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32  pOp->p3>=pOp->p2
2556a 2b 75 2e 61 68 2e 6e 20 29 3b 0d 0a 20 20 75 2e  +u.ah.n );..  u.
2556b 61 68 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d 5b  ah.pArg = &aMem[
2556c 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 66 6f 72  pOp->p2];..  for
2556d 28 75 2e 61 68 2e 69 3d 30 3b 20 75 2e 61 68 2e  (u.ah.i=0; u.ah.
2556e 69 3c 75 2e 61 68 2e 6e 3b 20 75 2e 61 68 2e 69  i<u.ah.n; u.ah.i
2556f 2b 2b 2c 20 75 2e 61 68 2e 70 41 72 67 2b 2b 29  ++, u.ah.pArg++)
25570 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  {..    assert( m
25571 65 6d 49 73 56 61 6c 69 64 28 75 2e 61 68 2e 70  emIsValid(u.ah.p
25572 41 72 67 29 20 29 3b 0d 0a 20 20 20 20 75 2e 61  Arg) );..    u.a
25573 68 2e 61 70 56 61 6c 5b 75 2e 61 68 2e 69 5d 20  h.apVal[u.ah.i] 
25574 3d 20 75 2e 61 68 2e 70 41 72 67 3b 0d 0a 20 20  = u.ah.pArg;..  
25575 20 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65    Deephemeralize
25576 28 75 2e 61 68 2e 70 41 72 67 29 3b 0d 0a 20 20  (u.ah.pArg);..  
25577 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
25578 53 74 6f 72 65 54 79 70 65 28 75 2e 61 68 2e 70  StoreType(u.ah.p
25579 41 72 67 29 3b 0d 0a 20 20 20 20 52 45 47 49 53  Arg);..    REGIS
2557a 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
2557b 32 2b 75 2e 61 68 2e 69 2c 20 75 2e 61 68 2e 70  2+u.ah.i, u.ah.p
2557c 41 72 67 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  Arg);..  }....  
2557d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
2557e 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20  ype==P4_FUNCDEF 
2557f 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  || pOp->p4type==
25580 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b 0d 0a  P4_VDBEFUNC );..
25581 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70    if( pOp->p4typ
25582 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 7b  e==P4_FUNCDEF ){
25583 0d 0a 20 20 20 20 75 2e 61 68 2e 63 74 78 2e 70  ..    u.ah.ctx.p
25584 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
25585 46 75 6e 63 3b 0d 0a 20 20 20 20 75 2e 61 68 2e  Func;..    u.ah.
25586 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20  ctx.pVdbeFunc = 
25587 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  0;..  }else{..  
25588 20 20 75 2e 61 68 2e 63 74 78 2e 70 56 64 62 65    u.ah.ctx.pVdbe
25589 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63  Func = (VdbeFunc
2558a 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46  *)pOp->p4.pVdbeF
2558b 75 6e 63 3b 0d 0a 20 20 20 20 75 2e 61 68 2e 63  unc;..    u.ah.c
2558c 74 78 2e 70 46 75 6e 63 20 3d 20 75 2e 61 68 2e  tx.pFunc = u.ah.
2558d 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2d 3e 70  ctx.pVdbeFunc->p
2558e 46 75 6e 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  Func;..  }....  
2558f 75 2e 61 68 2e 63 74 78 2e 73 2e 66 6c 61 67 73  u.ah.ctx.s.flags
25590 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20   = MEM_Null;..  
25591 75 2e 61 68 2e 63 74 78 2e 73 2e 64 62 20 3d 20  u.ah.ctx.s.db = 
25592 64 62 3b 0d 0a 20 20 75 2e 61 68 2e 63 74 78 2e  db;..  u.ah.ctx.
25593 73 2e 78 44 65 6c 20 3d 20 30 3b 0d 0a 20 20 75  s.xDel = 0;..  u
25594 2e 61 68 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f  .ah.ctx.s.zMallo
25595 63 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 54  c = 0;....  /* T
25596 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d  he output cell m
25597 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  ay already have 
25598 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74  a buffer allocat
25599 65 64 2e 20 4d 6f 76 65 0d 0a 20 20 2a 2a 20 74  ed. Move..  ** t
2559a 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 75 2e  he pointer to u.
2559b 61 68 2e 63 74 78 2e 73 20 73 6f 20 69 6e 20 63  ah.ctx.s so in c
2559c 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e  ase the user-fun
2559d 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0d 0a 20  ction can use.. 
2559e 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20   ** the already 
2559f 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
255a0 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f   instead of allo
255a1 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65  cating a new one
255a2 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ...  */..  sqlit
255a3 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75  e3VdbeMemMove(&u
255a4 2e 61 68 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29  .ah.ctx.s, pOut)
255a5 3b 0d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  ;..  MemSetTypeF
255a6 6c 61 67 28 26 75 2e 61 68 2e 63 74 78 2e 73 2c  lag(&u.ah.ctx.s,
255a7 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0d 0a 0d 0a 20   MEM_Null);.... 
255a8 20 75 2e 61 68 2e 63 74 78 2e 69 73 45 72 72 6f   u.ah.ctx.isErro
255a9 72 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 75 2e  r = 0;..  if( u.
255aa 61 68 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c  ah.ctx.pFunc->fl
255ab 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
255ac 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0d 0a 20  C_NEEDCOLL ){.. 
255ad 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 61     assert( pOp>a
255ae 4f 70 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  Op );..    asser
255af 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70  t( pOp[-1].p4typ
255b0 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b  e==P4_COLLSEQ );
255b1 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ..    assert( pO
255b2 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50  p[-1].opcode==OP
255b3 5f 43 6f 6c 6c 53 65 71 20 29 3b 0d 0a 20 20 20  _CollSeq );..   
255b4 20 75 2e 61 68 2e 63 74 78 2e 70 43 6f 6c 6c 20   u.ah.ctx.pColl 
255b5 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f  = pOp[-1].p4.pCo
255b6 6c 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d 3e  ll;..  }..  db->
255b7 6c 61 73 74 52 6f 77 69 64 20 3d 20 6c 61 73 74  lastRowid = last
255b8 52 6f 77 69 64 3b 0d 0a 20 20 28 2a 75 2e 61 68  Rowid;..  (*u.ah
255b9 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e  .ctx.pFunc->xFun
255ba 63 29 28 26 75 2e 61 68 2e 63 74 78 2c 20 75 2e  c)(&u.ah.ctx, u.
255bb 61 68 2e 6e 2c 20 75 2e 61 68 2e 61 70 56 61 6c  ah.n, u.ah.apVal
255bc 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 34 35  ); /* IMP: R-245
255bd 30 35 2d 32 33 32 33 30 20 2a 2f 0d 0a 20 20 6c  05-23230 */..  l
255be 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c  astRowid = db->l
255bf 61 73 74 52 6f 77 69 64 3b 0d 0a 0d 0a 20 20 2f  astRowid;....  /
255c0 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61  * If any auxilia
255c1 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e  ry data function
255c2 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c  s have been call
255c3 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20  ed by this user 
255c4 66 75 6e 63 74 69 6f 6e 2c 0d 0a 20 20 2a 2a 20  function,..  ** 
255c5 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c  immediately call
255c6 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
255c7 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74  for any non-stat
255c8 69 63 20 76 61 6c 75 65 73 2e 0d 0a 20 20 2a 2f  ic values...  */
255c9 0d 0a 20 20 69 66 28 20 75 2e 61 68 2e 63 74 78  ..  if( u.ah.ctx
255ca 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0d 0a 20  .pVdbeFunc ){.. 
255cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
255cc 6c 65 74 65 41 75 78 44 61 74 61 28 75 2e 61 68  leteAuxData(u.ah
255cd 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20  .ctx.pVdbeFunc, 
255ce 70 4f 70 2d 3e 70 31 29 3b 0d 0a 20 20 20 20 70  pOp->p1);..    p
255cf 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63  Op->p4.pVdbeFunc
255d0 20 3d 20 75 2e 61 68 2e 63 74 78 2e 70 56 64 62   = u.ah.ctx.pVdb
255d1 65 46 75 6e 63 3b 0d 0a 20 20 20 20 70 4f 70 2d  eFunc;..    pOp-
255d2 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42  >p4type = P4_VDB
255d3 45 46 55 4e 43 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  EFUNC;..  }.... 
255d4 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
255d5 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 2f 2a  ailed ){..    /*
255d6 20 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d   Even though a m
255d7 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c  alloc() has fail
255d8 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ed, the implemen
255d9 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0d 0a 20  tation of the.. 
255da 20 20 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74     ** user funct
255db 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c  ion may have cal
255dc 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72  led an sqlite3_r
255dd 65 73 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63  esult_XXX() func
255de 74 69 6f 6e 0d 0a 20 20 20 20 2a 2a 20 74 6f 20  tion..    ** to 
255df 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 2e 20  return a value. 
255e0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61  The following ca
255e1 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e 79 20  ll releases any 
255e2 72 65 73 6f 75 72 63 65 73 0d 0a 20 20 20 20 2a  resources..    *
255e3 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
255e4 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e 0d  h such a value..
255e5 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  .    */..    sql
255e6 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
255e7 73 65 28 26 75 2e 61 68 2e 63 74 78 2e 73 29 3b  se(&u.ah.ctx.s);
255e8 0d 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ..    goto no_me
255e9 6d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  m;..  }....  /* 
255ea 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
255eb 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f  returned an erro
255ec 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65  r, throw an exce
255ed 70 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 66 28 20  ption */..  if( 
255ee 75 2e 61 68 2e 63 74 78 2e 69 73 45 72 72 6f 72  u.ah.ctx.isError
255ef 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
255f0 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
255f1 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
255f2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
255f3 65 78 74 28 26 75 2e 61 68 2e 63 74 78 2e 73 29  ext(&u.ah.ctx.s)
255f4 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 75 2e 61  );..    rc = u.a
255f5 68 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0d 0a  h.ctx.isError;..
255f6 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 70 79    }....  /* Copy
255f7 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
255f8 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f  he function into
255f9 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0d   register P3 */.
255fa 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
255fb 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e  angeEncoding(&u.
255fc 61 68 2e 63 74 78 2e 73 2c 20 65 6e 63 6f 64 69  ah.ctx.s, encodi
255fd 6e 67 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  ng);..  sqlite3V
255fe 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c  dbeMemMove(pOut,
255ff 20 26 75 2e 61 68 2e 63 74 78 2e 73 29 3b 0d 0a   &u.ah.ctx.s);..
25600 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
25601 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 29  eMemTooBig(pOut)
25602 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 74 6f   ){..    goto to
25603 6f 5f 62 69 67 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23  o_big;..  }....#
25604 69 66 20 30 0d 0a 20 20 2f 2a 20 54 68 65 20 61  if 0..  /* The a
25605 70 70 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  pp-defined funct
25606 69 6f 6e 20 68 61 73 20 64 6f 6e 65 20 73 6f 6d  ion has done som
25607 65 74 68 69 6e 67 20 74 68 61 74 20 61 73 20 63  ething that as c
25608 61 75 73 65 64 20 74 68 69 73 0d 0a 20 20 2a 2a  aused this..  **
25609 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 78   statement to ex
2560a 70 69 72 65 2e 20 20 28 50 65 72 68 61 70 73 20  pire.  (Perhaps 
2560b 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  the function cal
2560c 6c 65 64 20 73 71 6c 69 74 65 33 5f 65 78 65 63  led sqlite3_exec
2560d 28 29 0d 0a 20 20 2a 2a 20 77 69 74 68 20 61 20  ()..  ** with a 
2560e 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
2560f 74 65 6d 65 6e 74 2e 29 0d 0a 20 20 2a 2f 0d 0a  tement.)..  */..
25610 20 20 69 66 28 20 70 2d 3e 65 78 70 69 72 65 64    if( p->expired
25611 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41   ) rc = SQLITE_A
25612 42 4f 52 54 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  BORT;..#endif...
25613 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
25614 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29  E(pOp->p3, pOut)
25615 3b 0d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  ;..  UPDATE_MAX_
25616 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0d  BLOBSIZE(pOut);.
25617 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a  .  break;..}....
25618 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e  /* Opcode: BitAn
25619 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a  d P1 P2 P3 * *..
2561a 2a 2a 0d 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  **..** Take the 
2561b 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f 66 20  bit-wise AND of 
2561c 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
2561d 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32  gister P1 and P2
2561e 20 61 6e 64 0d 0a 2a 2a 20 73 74 6f 72 65 20 74   and..** store t
2561f 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
25620 69 73 74 65 72 20 50 33 2e 0d 0a 2a 2a 20 49 66  ister P3...** If
25621 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
25622 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
25623 74 20 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  t is NULL...*/..
25624 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72  /* Opcode: BitOr
25625 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a 2a   P1 P2 P3 * *..*
25626 2a 0d 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62  *..** Take the b
25627 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68  it-wise OR of th
25628 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
25629 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
2562a 6e 64 0d 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65  nd..** store the
2562b 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73   result in regis
2562c 74 65 72 20 50 33 2e 0d 0a 2a 2a 20 49 66 20 65  ter P3...** If e
2562d 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
2562e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
2562f 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 2f 2a  is NULL...*/../*
25630 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 4c 65   Opcode: ShiftLe
25631 66 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0d  ft P1 P2 P3 * *.
25632 0a 2a 2a 0d 0a 2a 2a 20 53 68 69 66 74 20 74 68  .**..** Shift th
25633 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
25634 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74  in register P2 t
25635 6f 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 68  o the left by th
25636 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  e..** number of 
25637 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62  bits specified b
25638 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e  y the integer in
25639 20 72 65 67 69 73 74 65 72 20 50 31 2e 0d 0a 2a   register P1...*
2563a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75  * Store the resu
2563b 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  lt in register P
2563c 33 2e 0d 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  3...** If either
2563d 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
2563e 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
2563f 4c 4c 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f  LL...*/../* Opco
25640 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50  de: ShiftRight P
25641 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d  1 P2 P3 * *..**.
25642 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e  .** Shift the in
25643 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72  teger value in r
25644 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68  egister P2 to th
25645 65 20 72 69 67 68 74 20 62 79 20 74 68 65 0d 0a  e right by the..
25646 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  ** number of bit
25647 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  s specified by t
25648 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65  he integer in re
25649 67 69 73 74 65 72 20 50 31 2e 0d 0a 2a 2a 20 53  gister P1...** S
2564a 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
2564b 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0d  in register P3..
2564c 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
2564d 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
2564e 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
2564f 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 42 69  ..*/..case OP_Bi
25650 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20  tAnd:           
25651 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
25652 20 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c   TK_BITAND, in1,
25653 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0d 0a 63   in2, out3 */..c
25654 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20  ase OP_BitOr:   
25655 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25656 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
25657 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  OR, in1, in2, ou
25658 74 33 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 53  t3 */..case OP_S
25659 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20 20  hiftLeft:       
2565a 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
2565b 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31  s TK_LSHIFT, in1
2565c 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0d 0a  , in2, out3 */..
2565d 63 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67  case OP_ShiftRig
2565e 68 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ht: {           
2565f 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53  /* same as TK_RS
25660 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  HIFT, in1, in2, 
25661 6f 75 74 33 20 2a 2f 0d 0a 23 69 66 20 30 20 20  out3 */..#if 0  
25662 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
25663 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
25664 61 69 20 2a 2f 0d 0a 20 20 69 36 34 20 69 41 3b  ai */..  i64 iA;
25665 0d 0a 20 20 75 36 34 20 75 41 3b 0d 0a 20 20 69  ..  u64 uA;..  i
25666 36 34 20 69 42 3b 0d 0a 20 20 75 38 20 6f 70 3b  64 iB;..  u8 op;
25667 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ..#endif /* loca
25668 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25669 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0d 0a  d into u.ai */..
2566a 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  ..  pIn1 = &aMem
2566b 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 70 49  [pOp->p1];..  pI
2566c 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n2 = &aMem[pOp->
2566d 70 32 5d 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26  p2];..  pOut = &
2566e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a  aMem[pOp->p3];..
2566f 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
25670 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
25671 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d  ) & MEM_Null ){.
25672 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25673 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29  MemSetNull(pOut)
25674 3b 0d 0a 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  ;..    break;.. 
25675 20 7d 0d 0a 20 20 75 2e 61 69 2e 69 41 20 3d 20   }..  u.ai.iA = 
25676 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
25677 6c 75 65 28 70 49 6e 32 29 3b 0d 0a 20 20 75 2e  lue(pIn2);..  u.
25678 61 69 2e 69 42 20 3d 20 73 71 6c 69 74 65 33 56  ai.iB = sqlite3V
25679 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31  dbeIntValue(pIn1
2567a 29 3b 0d 0a 20 20 75 2e 61 69 2e 6f 70 20 3d 20  );..  u.ai.op = 
2567b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0d 0a 20 20  pOp->opcode;..  
2567c 69 66 28 20 75 2e 61 69 2e 6f 70 3d 3d 4f 50 5f  if( u.ai.op==OP_
2567d 42 69 74 41 6e 64 20 29 7b 0d 0a 20 20 20 20 75  BitAnd ){..    u
2567e 2e 61 69 2e 69 41 20 26 3d 20 75 2e 61 69 2e 69  .ai.iA &= u.ai.i
2567f 42 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  B;..  }else if( 
25680 75 2e 61 69 2e 6f 70 3d 3d 4f 50 5f 42 69 74 4f  u.ai.op==OP_BitO
25681 72 20 29 7b 0d 0a 20 20 20 20 75 2e 61 69 2e 69  r ){..    u.ai.i
25682 41 20 7c 3d 20 75 2e 61 69 2e 69 42 3b 0d 0a 20  A |= u.ai.iB;.. 
25683 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e   }else if( u.ai.
25684 69 42 21 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73  iB!=0 ){..    as
25685 73 65 72 74 28 20 75 2e 61 69 2e 6f 70 3d 3d 4f  sert( u.ai.op==O
25686 50 5f 53 68 69 66 74 52 69 67 68 74 20 7c 7c 20  P_ShiftRight || 
25687 75 2e 61 69 2e 6f 70 3d 3d 4f 50 5f 53 68 69 66  u.ai.op==OP_Shif
25688 74 4c 65 66 74 20 29 3b 0d 0a 0d 0a 20 20 20 20  tLeft );....    
25689 2f 2a 20 49 66 20 73 68 69 66 74 69 6e 67 20 62  /* If shifting b
2568a 79 20 61 20 6e 65 67 61 74 69 76 65 20 61 6d 6f  y a negative amo
2568b 75 6e 74 2c 20 73 68 69 66 74 20 69 6e 20 74 68  unt, shift in th
2568c 65 20 6f 74 68 65 72 20 64 69 72 65 63 74 69 6f  e other directio
2568d 6e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 75 2e  n */..    if( u.
2568e 61 69 2e 69 42 3c 30 20 29 7b 0d 0a 20 20 20 20  ai.iB<0 ){..    
2568f 20 20 61 73 73 65 72 74 28 20 4f 50 5f 53 68 69    assert( OP_Shi
25690 66 74 52 69 67 68 74 3d 3d 4f 50 5f 53 68 69 66  ftRight==OP_Shif
25691 74 4c 65 66 74 2b 31 20 29 3b 0d 0a 20 20 20 20  tLeft+1 );..    
25692 20 20 75 2e 61 69 2e 6f 70 20 3d 20 32 2a 4f 50    u.ai.op = 2*OP
25693 5f 53 68 69 66 74 4c 65 66 74 20 2b 20 31 20 2d  _ShiftLeft + 1 -
25694 20 75 2e 61 69 2e 6f 70 3b 0d 0a 20 20 20 20 20   u.ai.op;..     
25695 20 75 2e 61 69 2e 69 42 20 3d 20 75 2e 61 69 2e   u.ai.iB = u.ai.
25696 69 42 3e 28 2d 36 34 29 20 3f 20 2d 75 2e 61 69  iB>(-64) ? -u.ai
25697 2e 69 42 20 3a 20 36 34 3b 0d 0a 20 20 20 20 7d  .iB : 64;..    }
25698 0d 0a 0d 0a 20 20 20 20 69 66 28 20 75 2e 61 69  ....    if( u.ai
25699 2e 69 42 3e 3d 36 34 20 29 7b 0d 0a 20 20 20 20  .iB>=64 ){..    
2569a 20 20 75 2e 61 69 2e 69 41 20 3d 20 28 75 2e 61    u.ai.iA = (u.a
2569b 69 2e 69 41 3e 3d 30 20 7c 7c 20 75 2e 61 69 2e  i.iA>=0 || u.ai.
2569c 6f 70 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74  op==OP_ShiftLeft
2569d 29 20 3f 20 30 20 3a 20 2d 31 3b 0d 0a 20 20 20  ) ? 0 : -1;..   
2569e 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6d   }else{..      m
2569f 65 6d 63 70 79 28 26 75 2e 61 69 2e 75 41 2c 20  emcpy(&u.ai.uA, 
256a0 26 75 2e 61 69 2e 69 41 2c 20 73 69 7a 65 6f 66  &u.ai.iA, sizeof
256a1 28 75 2e 61 69 2e 75 41 29 29 3b 0d 0a 20 20 20  (u.ai.uA));..   
256a2 20 20 20 69 66 28 20 75 2e 61 69 2e 6f 70 3d 3d     if( u.ai.op==
256a3 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29 7b 0d  OP_ShiftLeft ){.
256a4 0a 20 20 20 20 20 20 20 20 75 2e 61 69 2e 75 41  .        u.ai.uA
256a5 20 3c 3c 3d 20 75 2e 61 69 2e 69 42 3b 0d 0a 20   <<= u.ai.iB;.. 
256a6 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
256a7 20 20 20 20 20 75 2e 61 69 2e 75 41 20 3e 3e 3d       u.ai.uA >>=
256a8 20 75 2e 61 69 2e 69 42 3b 0d 0a 20 20 20 20 20   u.ai.iB;..     
256a9 20 20 20 2f 2a 20 53 69 67 6e 2d 65 78 74 65 6e     /* Sign-exten
256aa 64 20 6f 6e 20 61 20 72 69 67 68 74 20 73 68 69  d on a right shi
256ab 66 74 20 6f 66 20 61 20 6e 65 67 61 74 69 76 65  ft of a negative
256ac 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20 20   number */..    
256ad 20 20 20 20 69 66 28 20 75 2e 61 69 2e 69 41 3c      if( u.ai.iA<
256ae 30 20 29 20 75 2e 61 69 2e 75 41 20 7c 3d 20 28  0 ) u.ai.uA |= (
256af 28 28 28 75 36 34 29 30 78 66 66 66 66 66 66 66  (((u64)0xfffffff
256b0 66 29 3c 3c 33 32 29 7c 30 78 66 66 66 66 66 66  f)<<32)|0xffffff
256b1 66 66 29 20 3c 3c 20 28 36 34 2d 75 2e 61 69 2e  ff) << (64-u.ai.
256b2 69 42 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  iB);..      }.. 
256b3 20 20 20 20 20 6d 65 6d 63 70 79 28 26 75 2e 61       memcpy(&u.a
256b4 69 2e 69 41 2c 20 26 75 2e 61 69 2e 75 41 2c 20  i.iA, &u.ai.uA, 
256b5 73 69 7a 65 6f 66 28 75 2e 61 69 2e 69 41 29 29  sizeof(u.ai.iA))
256b6 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
256b7 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61   pOut->u.i = u.a
256b8 69 2e 69 41 3b 0d 0a 20 20 4d 65 6d 53 65 74 54  i.iA;..  MemSetT
256b9 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
256ba 4d 5f 49 6e 74 29 3b 0d 0a 20 20 62 72 65 61 6b  M_Int);..  break
256bb 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
256bc 65 3a 20 41 64 64 49 6d 6d 20 20 50 31 20 50 32  e: AddImm  P1 P2
256bd 20 2a 20 2a 20 2a 0d 0a 2a 2a 20 0d 0a 2a 2a 20   * * *..** ..** 
256be 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 6e 74  Add the constant
256bf 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c 75 65   P2 to the value
256c0 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
256c1 0d 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  ..** The result 
256c2 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e 74  is always an int
256c3 65 67 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f  eger...**..** To
256c4 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73   force any regis
256c5 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74  ter to be an int
256c6 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30  eger, just add 0
256c7 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 41  ...*/..case OP_A
256c8 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20  ddImm: {        
256c9 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0d 0a 20      /* in1 */.. 
256ca 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
256cb 70 2d 3e 70 31 5d 3b 0d 0a 20 20 6d 65 6d 41 62  p->p1];..  memAb
256cc 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 70  outToChange(p, p
256cd 49 6e 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  In1);..  sqlite3
256ce 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
256cf 79 28 70 49 6e 31 29 3b 0d 0a 20 20 70 49 6e 31  y(pIn1);..  pIn1
256d0 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 32  ->u.i += pOp->p2
256d1 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  ;..  break;..}..
256d2 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73  ../* Opcode: Mus
256d3 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a  tBeInt P1 P2 * *
256d4 20 2a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 46 6f 72 63   *..** ..** Forc
256d5 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
256d6 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
256d7 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66   an integer.  If
256d8 20 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20 69   the value..** i
256d9 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69  n P1 is not an i
256da 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f  nteger and canno
256db 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69  t be converted i
256dc 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0d 0a  nto an integer..
256dd 2a 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20  ** without data 
256de 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20  loss, then jump 
256df 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
256e0 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0d 0a  2, or if P2==0..
256e1 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49  ** raise an SQLI
256e2 54 45 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65  TE_MISMATCH exce
256e3 70 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  ption...*/..case
256e4 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b   OP_MustBeInt: {
256e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
256e6 75 6d 70 2c 20 69 6e 31 20 2a 2f 0d 0a 20 20 70  ump, in1 */..  p
256e7 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
256e8 3e 70 31 5d 3b 0d 0a 20 20 61 70 70 6c 79 41 66  >p1];..  applyAf
256e9 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c  finity(pIn1, SQL
256ea 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c  ITE_AFF_NUMERIC,
256eb 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20 20 69   encoding);..  i
256ec 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
256ed 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b  & MEM_Int)==0 ){
256ee 0d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ..    if( pOp->p
256ef 32 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72  2==0 ){..      r
256f0 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41  c = SQLITE_MISMA
256f1 54 43 48 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  TCH;..      goto
256f2 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
256f3 72 6f 72 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  ror;..    }else{
256f4 0d 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  ..      pc = pOp
256f5 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20 20 20 20 7d  ->p2 - 1;..    }
256f6 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
256f7 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
256f8 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0d 0a  In1, MEM_Int);..
256f9 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
256fa 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
256fb 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
256fc 5f 50 4f 49 4e 54 0d 0a 2f 2a 20 4f 70 63 6f 64  _POINT../* Opcod
256fd 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 79 20  e: RealAffinity 
256fe 50 31 20 2a 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  P1 * * * *..**..
256ff 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 20 50  ** If register P
25700 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67  1 holds an integ
25701 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  er convert it to
25702 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0d 0a   a real value...
25703 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  **..** This opco
25704 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  de is used when 
25705 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72  extracting infor
25706 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f  mation from a co
25707 6c 75 6d 6e 20 74 68 61 74 0d 0a 2a 2a 20 68 61  lumn that..** ha
25708 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e  s REAL affinity.
25709 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61    Such column va
2570a 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  lues may still b
2570b 65 20 73 74 6f 72 65 64 20 61 73 0d 0a 2a 2a 20  e stored as..** 
2570c 69 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70  integers, for sp
2570d 61 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20  ace efficiency, 
2570e 62 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63  but after extrac
2570f 74 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65  tion we want the
25710 6d 0d 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e  m..** to have on
25711 6c 79 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e  ly a real value.
25712 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 52 65  ..*/..case OP_Re
25713 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b 20 20 20  alAffinity: {   
25714 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25715 2a 20 69 6e 31 20 2a 2f 0d 0a 20 20 70 49 6e 31  * in1 */..  pIn1
25716 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
25717 5d 3b 0d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e  ];..  if( pIn1->
25718 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
25719 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ){..    sqlite3V
2571a 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49  dbeMemRealify(pI
2571b 6e 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65  n1);..  }..  bre
2571c 61 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ak;..}..#endif..
2571d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2571e 5f 4f 4d 49 54 5f 43 41 53 54 0d 0a 2f 2a 20 4f  _OMIT_CAST../* O
2571f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 50 31  pcode: ToText P1
25720 20 2a 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a   * * * *..**..**
25721 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
25722 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
25723 74 6f 20 62 65 20 74 65 78 74 2e 0d 0a 2a 2a 20  to be text...** 
25724 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
25725 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74  numeric, convert
25726 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20   it to a string 
25727 75 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 65 71  using the..** eq
25728 75 69 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e  uivalent of prin
25729 74 66 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75  tf().  Blob valu
2572a 65 73 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64  es are unchanged
2572b 20 61 6e 64 0d 0a 2a 2a 20 61 72 65 20 61 66 74   and..** are aft
2572c 65 72 77 61 72 64 73 20 73 69 6d 70 6c 79 20 69  erwards simply i
2572d 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 74 65  nterpreted as te
2572e 78 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 4e 55  xt...**..** A NU
2572f 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
25730 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
25731 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
25732 61 69 6e 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  ains NULL...*/..
25733 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20  case OP_ToText: 
25734 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
25735 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
25736 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f  _TO_TEXT, in1 */
25737 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  ..  pIn1 = &aMem
25738 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 6d 65  [pOp->p1];..  me
25739 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70  mAboutToChange(p
2573a 2c 20 70 49 6e 31 29 3b 0d 0a 20 20 69 66 28 20  , pIn1);..  if( 
2573b 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
2573c 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0d  M_Null ) break;.
2573d 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53  .  assert( MEM_S
2573e 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33  tr==(MEM_Blob>>3
2573f 29 20 29 3b 0d 0a 20 20 70 49 6e 31 2d 3e 66 6c  ) );..  pIn1->fl
25740 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c  ags |= (pIn1->fl
25741 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33  ags&MEM_Blob)>>3
25742 3b 0d 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69  ;..  applyAffini
25743 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f  ty(pIn1, SQLITE_
25744 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69  AFF_TEXT, encodi
25745 6e 67 29 3b 0d 0a 20 20 72 63 20 3d 20 45 78 70  ng);..  rc = Exp
25746 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0d 0a  andBlob(pIn1);..
25747 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
25748 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20  flags & MEM_Str 
25749 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2574a 6c 65 64 20 29 3b 0d 0a 20 20 70 49 6e 31 2d 3e  led );..  pIn1->
2574b 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49  flags &= ~(MEM_I
2574c 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  nt|MEM_Real|MEM_
2574d 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0d  Blob|MEM_Zero);.
2574e 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
2574f 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0d 0a 20  OBSIZE(pIn1);.. 
25750 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   break;..}..../*
25751 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20   Opcode: ToBlob 
25752 50 31 20 2a 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  P1 * * * *..**..
25753 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c  ** Force the val
25754 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
25755 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0d  1 to be a BLOB..
25756 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
25757 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e   is numeric, con
25758 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72  vert it to a str
25759 69 6e 67 20 66 69 72 73 74 2e 0d 0a 2a 2a 20 53  ing first...** S
2575a 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c  trings are simpl
2575b 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20  y reinterpreted 
2575c 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f  as blobs with no
2575d 20 63 68 61 6e 67 65 0d 0a 2a 2a 20 74 6f 20 74   change..** to t
2575e 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61  he underlying da
2575f 74 61 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 4e 55  ta...**..** A NU
25760 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
25761 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
25762 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
25763 61 69 6e 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  ains NULL...*/..
25764 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20  case OP_ToBlob: 
25765 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
25766 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
25767 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f  _TO_BLOB, in1 */
25768 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  ..  pIn1 = &aMem
25769 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 69 66  [pOp->p1];..  if
2576a 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
2576b 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b  MEM_Null ) break
2576c 3b 0d 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  ;..  if( (pIn1->
2576d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
2576e 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61 70 70  )==0 ){..    app
2576f 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c  lyAffinity(pIn1,
25770 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
25771 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20 20  , encoding);..  
25772 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
25773 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20  flags & MEM_Str 
25774 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
25775 6c 65 64 20 29 3b 0d 0a 20 20 20 20 4d 65 6d 53  led );..    MemS
25776 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c  etTypeFlag(pIn1,
25777 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0d 0a 20 20 7d   MEM_Blob);..  }
25778 65 6c 73 65 7b 0d 0a 20 20 20 20 70 49 6e 31 2d  else{..    pIn1-
25779 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
2577a 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42 6c  TypeMask&~MEM_Bl
2577b 6f 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20 55 50 44  ob);..  }..  UPD
2577c 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
2577d 28 70 49 6e 31 29 3b 0d 0a 20 20 62 72 65 61 6b  (pIn1);..  break
2577e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
2577f 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20  e: ToNumeric P1 
25780 2a 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  * * * *..**..** 
25781 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20  Force the value 
25782 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74  in register P1 t
25783 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69  o be numeric (ei
25784 74 68 65 72 20 61 6e 0d 0a 2a 2a 20 69 6e 74 65  ther an..** inte
25785 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e  ger or a floatin
25786 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29  g-point number.)
25787 0d 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
25788 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f  e is text or blo
25789 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72  b, try to conver
2578a 74 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67  t it to an using
2578b 20 74 68 65 0d 0a 2a 2a 20 65 71 75 69 76 61 6c   the..** equival
2578c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72  ent of atoi() or
2578d 20 61 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72   atof() and stor
2578e 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63  e 0 if no such c
2578f 6f 6e 76 65 72 73 69 6f 6e 20 0d 0a 2a 2a 20 69  onversion ..** i
25790 73 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 2a 2a 0d  s possible...**.
25791 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65  .** A NULL value
25792 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20   is not changed 
25793 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
25794 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c    It remains NUL
25795 4c 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  L...*/..case OP_
25796 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20 20  ToNumeric: {    
25797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25798 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e   same as TK_TO_N
25799 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0d 0a  UMERIC, in1 */..
2579a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
2579b 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 73 71 6c 69  Op->p1];..  sqli
2579c 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69  te3VdbeMemNumeri
2579d 66 79 28 70 49 6e 31 29 3b 0d 0a 20 20 62 72 65  fy(pIn1);..  bre
2579e 61 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f  ak;..}..#endif /
2579f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  * SQLITE_OMIT_CA
257a0 53 54 20 2a 2f 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f  ST */..../* Opco
257a1 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a  de: ToInt P1 * *
257a2 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72   * *..**..** For
257a3 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
257a4 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
257a5 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49  e an integer.  I
257a6 66 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  f..** The value 
257a7 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20 72  is currently a r
257a8 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70  eal number, drop
257a9 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c 20   its fractional 
257aa 70 61 72 74 2e 0d 0a 2a 2a 20 49 66 20 74 68 65  part...** If the
257ab 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
257ac 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
257ad 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
257ae 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
257af 65 0d 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  e..** equivalent
257b0 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73   of atoi() and s
257b1 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63  tore 0 if no suc
257b2 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  h conversion is 
257b3 70 6f 73 73 69 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a  possible...**..*
257b4 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
257b5 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
257b6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
257b7 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
257b8 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 54 6f  ..*/..case OP_To
257b9 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  Int: {          
257ba 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
257bb 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e  as TK_TO_INT, in
257bc 31 20 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d 20 26  1 */..  pIn1 = &
257bd 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a  aMem[pOp->p1];..
257be 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
257bf 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
257c0 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0 ){..    sqlite
257c1 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
257c2 66 79 28 70 49 6e 31 29 3b 0d 0a 20 20 7d 0d 0a  fy(pIn1);..  }..
257c3 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 23    break;..}....#
257c4 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
257c5 54 45 5f 4f 4d 49 54 5f 43 41 53 54 29 20 26 26  TE_OMIT_CAST) &&
257c6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
257c7 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
257c8 4f 49 4e 54 29 0d 0a 2f 2a 20 4f 70 63 6f 64 65  OINT)../* Opcode
257c9 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a 20 2a 20  : ToReal P1 * * 
257ca 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 63  * *..**..** Forc
257cb 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
257cc 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
257cd 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
257ce 74 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 20 49 66  t number...** If
257cf 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75   The value is cu
257d0 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67  rrently an integ
257d1 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0d  er, convert it..
257d2 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
257d3 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
257d4 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
257d5 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65   it to an intege
257d6 72 20 75 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20  r using the..** 
257d7 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74  equivalent of at
257d8 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30  oi() and store 0
257d9 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f  .0 if no such co
257da 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73  nversion is poss
257db 69 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20  ible...**..** A 
257dc 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
257dd 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
257de 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
257df 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f  emains NULL...*/
257e0 0d 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c  ..case OP_ToReal
257e1 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
257e2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
257e3 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20  TK_TO_REAL, in1 
257e4 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d  */..  pIn1 = &aM
257e5 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  em[pOp->p1];..  
257e6 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65  memAboutToChange
257e7 28 70 2c 20 70 49 6e 31 29 3b 0d 0a 20 20 69 66  (p, pIn1);..  if
257e8 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
257e9 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b   MEM_Null)==0 ){
257ea 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
257eb 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
257ec 29 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b  );..  }..  break
257ed 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
257ee 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
257ef 4f 4d 49 54 5f 43 41 53 54 29 20 26 26 20 21 64  OMIT_CAST) && !d
257f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
257f1 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
257f2 54 29 20 2a 2f 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f  T) */..../* Opco
257f3 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20  de: Lt P1 P2 P3 
257f4 50 34 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 43 6f  P4 P5..**..** Co
257f5 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73  mpare the values
257f6 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
257f7 61 6e 64 20 50 33 2e 20 20 49 66 20 72 65 67 28  and P3.  If reg(
257f8 50 33 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e  P3)<reg(P1) then
257f9 0d 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64  ..** jump to add
257fa 72 65 73 73 20 50 32 2e 20 20 0d 0a 2a 2a 0d 0a  ress P2.  ..**..
257fb 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  ** If the SQLITE
257fc 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20  _JUMPIFNULL bit 
257fd 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 6e 64  of P5 is set and
257fe 20 65 69 74 68 65 72 20 72 65 67 28 50 31 29 20   either reg(P1) 
257ff 6f 72 0d 0a 2a 2a 20 72 65 67 28 50 33 29 20 69  or..** reg(P3) i
25800 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65  s NULL then take
25801 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20 74   the jump.  If t
25802 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  he SQLITE_JUMPIF
25803 4e 55 4c 4c 20 0d 0a 2a 2a 20 62 69 74 20 69 73  NULL ..** bit is
25804 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c   clear then fall
25805 20 74 68 72 6f 75 67 68 20 69 66 20 65 69 74 68   through if eith
25806 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
25807 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  LL...**..** The 
25808 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
25809 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75  portion of P5 mu
2580a 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69 74  st be an affinit
2580b 79 20 63 68 61 72 61 63 74 65 72 20 2d 0d 0a 2a  y character -..*
2580c 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  * SQLITE_AFF_TEX
2580d 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  T, SQLITE_AFF_IN
2580e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f  TEGER, and so fo
2580f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20  rth. An attempt 
25810 69 73 20 6d 61 64 65 20 0d 0a 2a 2a 20 74 6f 20  is made ..** to 
25811 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70 75  coerce both inpu
25812 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  ts according to 
25813 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 62 65  this affinity be
25814 66 6f 72 65 20 74 68 65 0d 0a 2a 2a 20 63 6f 6d  fore the..** com
25815 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e  parison is made.
25816 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41   If the SQLITE_A
25817 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c  FF_MASK is 0x00,
25818 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0d 0a 2a   then numeric..*
25819 2a 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73  * affinity is us
2581a 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68  ed. Note that th
2581b 65 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65  e affinity conve
2581c 72 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65  rsions are store
2581d 64 0d 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20  d..** back into 
2581e 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74  the input regist
2581f 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20  ers P1 and P3.  
25820 53 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63  So this opcode c
25821 61 6e 20 63 61 75 73 65 0d 0a 2a 2a 20 70 65 72  an cause..** per
25822 73 69 73 74 65 6e 74 20 63 68 61 6e 67 65 73 20  sistent changes 
25823 74 6f 20 72 65 67 69 73 74 65 72 73 20 50 31 20  to registers P1 
25824 61 6e 64 20 50 33 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  and P3...**..** 
25825 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 73  Once any convers
25826 69 6f 6e 73 20 68 61 76 65 20 74 61 6b 65 6e 20  ions have taken 
25827 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 68  place, and neith
25828 65 72 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c  er value is NULL
25829 2c 20 0d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  , ..** the value
2582a 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64 2e 20  s are compared. 
2582b 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61  If both values a
2582c 72 65 20 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65  re blobs then me
2582d 6d 63 6d 70 28 29 20 69 73 0d 0a 2a 2a 20 75 73  mcmp() is..** us
2582e 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
2582f 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
25830 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
25831 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0d 0a  If both values..
25832 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 65  ** are text, the
25833 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
25834 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  e collating func
25835 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69  tion specified i
25836 6e 0d 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  n..** P4 is  use
25837 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
25838 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
25839 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
2583a 74 68 65 6e 0d 0a 2a 2a 20 6d 65 6d 63 6d 70 28  then..** memcmp(
2583b 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  ) is used to com
2583c 70 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67  pare text string
2583d 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  .  If both value
2583e 73 20 61 72 65 0d 0a 2a 2a 20 6e 75 6d 65 72 69  s are..** numeri
2583f 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69  c, then a numeri
25840 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  c comparison is 
25841 75 73 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f  used. If the two
25842 20 76 61 6c 75 65 73 0d 0a 2a 2a 20 61 72 65 20   values..** are 
25843 6f 66 20 64 69 66 66 65 72 65 6e 74 20 74 79 70  of different typ
25844 65 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 73  es, then numbers
25845 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
25846 6c 65 73 73 20 74 68 61 6e 0d 0a 2a 2a 20 73 74  less than..** st
25847 72 69 6e 67 73 20 61 6e 64 20 73 74 72 69 6e 67  rings and string
25848 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
25849 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73   less than blobs
2584a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
2584b 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20   SQLITE_STOREP2 
2584c 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74  bit of P5 is set
2584d 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 75  , then do not ju
2584e 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0d 0a 2a  mp.  Instead,..*
2584f 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61  * store a boolea
25850 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65 72  n result (either
25851 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c   0, or 1, or NUL
25852 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  L) in register P
25853 32 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64  2...*/../* Opcod
25854 65 3a 20 4e 65 20 50 31 20 50 32 20 50 33 20 50  e: Ne P1 P2 P3 P
25855 34 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  4 P5..**..** Thi
25856 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
25857 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
25858 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
25859 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
2585a 0d 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64  ..** the operand
2585b 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50  s in registers P
2585c 31 20 61 6e 64 20 50 33 20 61 72 65 20 6e 6f 74  1 and P3 are not
2585d 20 65 71 75 61 6c 2e 20 20 53 65 65 20 74 68 65   equal.  See the
2585e 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 0d 0a   Lt opcode for..
2585f 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
25860 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  formation...**..
25861 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c  ** If SQLITE_NUL
25862 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50 35  LEQ is set in P5
25863 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
25864 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69   of comparison i
25865 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 0d  s always either.
25866 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73  .** true or fals
25867 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e  e and is never N
25868 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70  ULL.  If both op
25869 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20  erands are NULL 
2586a 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0d  then the result.
2586b 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f  .** of compariso
2586c 6e 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20  n is false.  If 
2586d 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69  either operand i
2586e 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
2586f 72 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0d  result is true..
25870 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f  .** If neither o
25871 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74  perand is NULL t
25872 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65  he result is the
25873 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f 75 6c   same as it woul
25874 64 20 62 65 20 69 66 0d 0a 2a 2a 20 74 68 65 20  d be if..** the 
25875 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 6c  SQLITE_NULLEQ fl
25876 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64 20  ag were omitted 
25877 66 72 6f 6d 20 50 35 2e 0d 0a 2a 2f 0d 0a 2f 2a  from P5...*/../*
25878 20 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50   Opcode: Eq P1 P
25879 32 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a 0d 0a  2 P3 P4 P5..**..
2587a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
2587b 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
2587c 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
2587d 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
2587e 6b 65 6e 20 69 66 0d 0a 2a 2a 20 74 68 65 20 6f  ken if..** the o
2587f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73  perands in regis
25880 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61  ters P1 and P3 a
25881 72 65 20 65 71 75 61 6c 2e 0d 0a 2a 2a 20 53 65  re equal...** Se
25882 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
25883 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
25884 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d  nformation...**.
25885 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55  .** If SQLITE_NU
25886 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50  LLEQ is set in P
25887 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  5 then the resul
25888 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20  t of comparison 
25889 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72  is always either
2588a 0d 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c  ..** true or fal
2588b 73 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20  se and is never 
2588c 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f  NULL.  If both o
2588d 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c  perands are NULL
2588e 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
2588f 0d 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73  ..** of comparis
25890 6f 6e 20 69 73 20 74 72 75 65 2e 20 20 49 66 20  on is true.  If 
25891 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69  either operand i
25892 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
25893 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 2e  result is false.
25894 0d 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20  ..** If neither 
25895 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
25896 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  the result is th
25897 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f 75  e same as it wou
25898 6c 64 20 62 65 20 69 66 0d 0a 2a 2a 20 74 68 65  ld be if..** the
25899 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66   SQLITE_NULLEQ f
2589a 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64  lag were omitted
2589b 20 66 72 6f 6d 20 50 35 2e 0d 0a 2a 2f 0d 0a 2f   from P5...*/../
2589c 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20  * Opcode: Le P1 
2589d 50 32 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a 0d  P2 P3 P4 P5..**.
2589e 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
2589f 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
258a0 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
258a1 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
258a2 61 6b 65 6e 20 69 66 0d 0a 2a 2a 20 74 68 65 20  aken if..** the 
258a3 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
258a4 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74  ter P3 is less t
258a5 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
258a6 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0d 0a  the content of..
258a7 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20  ** register P1. 
258a8 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
258a9 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
258aa 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a  l information...
258ab 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47  */../* Opcode: G
258ac 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  t P1 P2 P3 P4 P5
258ad 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 77 6f  ..**..** This wo
258ae 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
258af 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
258b0 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
258b1 20 69 73 20 74 61 6b 65 6e 20 69 66 0d 0a 2a 2a   is taken if..**
258b2 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
258b3 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67  register P3 is g
258b4 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
258b5 63 6f 6e 74 65 6e 74 20 6f 66 0d 0a 2a 2a 20 72  content of..** r
258b6 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65  egister P1.  See
258b7 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
258b8 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
258b9 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  formation...*/..
258ba 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 20 50 31  /* Opcode: Ge P1
258bb 20 50 32 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a   P2 P3 P4 P5..**
258bc 0d 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  ..** This works 
258bd 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
258be 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
258bf 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
258c0 74 61 6b 65 6e 20 69 66 0d 0a 2a 2a 20 74 68 65  taken if..** the
258c1 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
258c2 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74  ster P3 is great
258c3 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
258c4 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
258c5 6f 66 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  of..** register 
258c6 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20  P1.  See the Lt 
258c7 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
258c8 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
258c9 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  n...*/..case OP_
258ca 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq:             
258cb 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
258cc 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  EQ, jump, in1, i
258cd 6e 33 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4e  n3 */..case OP_N
258ce 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e:              
258cf 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
258d0 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  E, jump, in1, in
258d1 33 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4c 74  3 */..case OP_Lt
258d2 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
258d3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54  /* same as TK_LT
258d4 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
258d5 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4c 65 3a   */..case OP_Le:
258d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
258d7 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c  * same as TK_LE,
258d8 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
258d9 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20  */..case OP_Gt: 
258da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
258db 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20   same as TK_GT, 
258dc 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
258dd 2f 0d 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b  /..case OP_Ge: {
258de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
258df 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a  same as TK_GE, j
258e0 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
258e1 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
258e2 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
258e3 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0d 0a  d into u.aj */..
258e4 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20    int res;      
258e5 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
258e6 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
258e7 6e 20 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73  n of pIn1 agains
258e8 74 20 70 49 6e 33 20 2a 2f 0d 0a 20 20 63 68 61  t pIn3 */..  cha
258e9 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20  r affinity;     
258ea 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20   /* Affinity to 
258eb 75 73 65 20 66 6f 72 20 63 6f 6d 70 61 72 69 73  use for comparis
258ec 6f 6e 20 2a 2f 0d 0a 20 20 75 31 36 20 66 6c 61  on */..  u16 fla
258ed 67 73 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  gs1;         /* 
258ee 43 6f 70 79 20 6f 66 20 69 6e 69 74 69 61 6c 20  Copy of initial 
258ef 76 61 6c 75 65 20 6f 66 20 70 49 6e 31 2d 3e 66  value of pIn1->f
258f0 6c 61 67 73 20 2a 2f 0d 0a 20 20 75 31 36 20 66  lags */..  u16 f
258f1 6c 61 67 73 33 3b 20 20 20 20 20 20 20 20 20 2f  lags3;         /
258f2 2a 20 43 6f 70 79 20 6f 66 20 69 6e 69 74 69 61  * Copy of initia
258f3 6c 20 76 61 6c 75 65 20 6f 66 20 70 49 6e 33 2d  l value of pIn3-
258f4 3e 66 6c 61 67 73 20 2a 2f 0d 0a 23 65 6e 64 69  >flags */..#endi
258f5 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
258f6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
258f7 75 2e 61 6a 20 2a 2f 0d 0a 0d 0a 20 20 70 49 6e  u.aj */....  pIn
258f8 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
258f9 31 5d 3b 0d 0a 20 20 70 49 6e 33 20 3d 20 26 61  1];..  pIn3 = &a
258fa 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20  Mem[pOp->p3];.. 
258fb 20 75 2e 61 6a 2e 66 6c 61 67 73 31 20 3d 20 70   u.aj.flags1 = p
258fc 49 6e 31 2d 3e 66 6c 61 67 73 3b 0d 0a 20 20 75  In1->flags;..  u
258fd 2e 61 6a 2e 66 6c 61 67 73 33 20 3d 20 70 49 6e  .aj.flags3 = pIn
258fe 33 2d 3e 66 6c 61 67 73 3b 0d 0a 20 20 69 66 28  3->flags;..  if(
258ff 20 28 75 2e 61 6a 2e 66 6c 61 67 73 31 20 7c 20   (u.aj.flags1 | 
25900 75 2e 61 6a 2e 66 6c 61 67 73 33 29 26 4d 45 4d  u.aj.flags3)&MEM
25901 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20 2f 2a  _Null ){..    /*
25902 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 70 65   One or both ope
25903 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a  rands are NULL *
25904 2f 0d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  /..    if( pOp->
25905 70 35 20 26 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  p5 & SQLITE_NULL
25906 45 51 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  EQ ){..      /* 
25907 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51  If SQLITE_NULLEQ
25908 20 69 73 20 73 65 74 20 28 77 68 69 63 68 20 77   is set (which w
25909 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  ill only happen 
2590a 69 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  if the operator 
2590b 69 73 0d 0a 20 20 20 20 20 20 2a 2a 20 4f 50 5f  is..      ** OP_
2590c 45 71 20 6f 72 20 4f 50 5f 4e 65 29 20 74 68 65  Eq or OP_Ne) the
2590d 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20  n take the jump 
2590e 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69 6e 67  or not depending
2590f 20 6f 6e 20 77 68 65 74 68 65 72 0d 0a 20 20 20   on whether..   
25910 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 62 6f 74     ** or not bot
25911 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 6e  h operands are n
25912 75 6c 6c 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ull...      */..
25913 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
25914 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 71  p->opcode==OP_Eq
25915 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   || pOp->opcode=
25916 3d 4f 50 5f 4e 65 20 29 3b 0d 0a 20 20 20 20 20  =OP_Ne );..     
25917 20 75 2e 61 6a 2e 72 65 73 20 3d 20 28 75 2e 61   u.aj.res = (u.a
25918 6a 2e 66 6c 61 67 73 31 20 26 20 75 2e 61 6a 2e  j.flags1 & u.aj.
25919 66 6c 61 67 73 33 20 26 20 4d 45 4d 5f 4e 75 6c  flags3 & MEM_Nul
2591a 6c 29 3d 3d 30 3b 0d 0a 20 20 20 20 7d 65 6c 73  l)==0;..    }els
2591b 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 53 51 4c  e{..      /* SQL
2591c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 63 6c  ITE_NULLEQ is cl
2591d 65 61 72 20 61 6e 64 20 61 74 20 6c 65 61 73 74  ear and at least
2591e 20 6f 6e 65 20 6f 70 65 72 61 6e 64 20 69 73 20   one operand is 
2591f 4e 55 4c 4c 2c 0d 0a 20 20 20 20 20 20 2a 2a 20  NULL,..      ** 
25920 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
25921 69 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0d  is always NULL..
25922 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 75  .      ** The ju
25923 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74  mp is taken if t
25924 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  he SQLITE_JUMPIF
25925 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65 74 2e  NULL bit is set.
25926 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
25927 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
25928 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29  SQLITE_STOREP2 )
25929 7b 0d 0a 20 20 20 20 20 20 20 20 70 4f 75 74 20  {..        pOut 
2592a 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
2592b 3b 0d 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65  ;..        MemSe
2592c 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
2592d 4d 45 4d 5f 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20  MEM_Null);..    
2592e 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
2592f 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74  CE(pOp->p2, pOut
25930 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  );..      }else 
25931 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51  if( pOp->p5 & SQ
25932 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
25933 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 63 20 3d  ){..        pc =
25934 20 70 4f 70 2d 3e 70 32 2d 31 3b 0d 0a 20 20 20   pOp->p2-1;..   
25935 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61     }..      brea
25936 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c  k;..    }..  }el
25937 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 4e 65 69 74  se{..    /* Neit
25938 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
25939 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61  ULL.  Do a compa
2593a 72 69 73 6f 6e 2e 20 2a 2f 0d 0a 20 20 20 20 75  rison. */..    u
2593b 2e 61 6a 2e 61 66 66 69 6e 69 74 79 20 3d 20 70  .aj.affinity = p
2593c 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f  Op->p5 & SQLITE_
2593d 41 46 46 5f 4d 41 53 4b 3b 0d 0a 20 20 20 20 69  AFF_MASK;..    i
2593e 66 28 20 75 2e 61 6a 2e 61 66 66 69 6e 69 74 79  f( u.aj.affinity
2593f 20 29 7b 0d 0a 20 20 20 20 20 20 61 70 70 6c 79   ){..      apply
25940 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75  Affinity(pIn1, u
25941 2e 61 6a 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e  .aj.affinity, en
25942 63 6f 64 69 6e 67 29 3b 0d 0a 20 20 20 20 20 20  coding);..      
25943 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
25944 6e 33 2c 20 75 2e 61 6a 2e 61 66 66 69 6e 69 74  n3, u.aj.affinit
25945 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20  y, encoding);.. 
25946 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c       if( db->mal
25947 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
25948 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20 7d 0d   no_mem;..    }.
25949 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ...    assert( p
2594a 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43  Op->p4type==P4_C
2594b 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e 70  OLLSEQ || pOp->p
2594c 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0d 0a 20  4.pColl==0 );.. 
2594d 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49     ExpandBlob(pI
2594e 6e 31 29 3b 0d 0a 20 20 20 20 45 78 70 61 6e 64  n1);..    Expand
2594f 42 6c 6f 62 28 70 49 6e 33 29 3b 0d 0a 20 20 20  Blob(pIn3);..   
25950 20 75 2e 61 6a 2e 72 65 73 20 3d 20 73 71 6c 69   u.aj.res = sqli
25951 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49  te3MemCompare(pI
25952 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70  n3, pIn1, pOp->p
25953 34 2e 70 43 6f 6c 6c 29 3b 0d 0a 20 20 7d 0d 0a  4.pColl);..  }..
25954 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
25955 70 63 6f 64 65 20 29 7b 0d 0a 20 20 20 20 63 61  pcode ){..    ca
25956 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 75 2e 61  se OP_Eq:    u.a
25957 6a 2e 72 65 73 20 3d 20 75 2e 61 6a 2e 72 65 73  j.res = u.aj.res
25958 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0d  ==0;     break;.
25959 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a  .    case OP_Ne:
2595a 20 20 20 20 75 2e 61 6a 2e 72 65 73 20 3d 20 75      u.aj.res = u
2595b 2e 61 6a 2e 72 65 73 21 3d 30 3b 20 20 20 20 20  .aj.res!=0;     
2595c 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65  break;..    case
2595d 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 6a 2e   OP_Lt:    u.aj.
2595e 72 65 73 20 3d 20 75 2e 61 6a 2e 72 65 73 3c 30  res = u.aj.res<0
2595f 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  ;      break;.. 
25960 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20     case OP_Le:  
25961 20 20 75 2e 61 6a 2e 72 65 73 20 3d 20 75 2e 61    u.aj.res = u.a
25962 6a 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72  j.res<=0;     br
25963 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65 20 4f  eak;..    case O
25964 50 5f 47 74 3a 20 20 20 20 75 2e 61 6a 2e 72 65  P_Gt:    u.aj.re
25965 73 20 3d 20 75 2e 61 6a 2e 72 65 73 3e 30 3b 20  s = u.aj.res>0; 
25966 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
25967 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
25968 75 2e 61 6a 2e 72 65 73 20 3d 20 75 2e 61 6a 2e  u.aj.res = u.aj.
25969 72 65 73 3e 3d 30 3b 20 20 20 20 20 62 72 65 61  res>=0;     brea
2596a 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  k;..  }....  if(
2596b 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
2596c 45 5f 53 54 4f 52 45 50 32 20 29 7b 0d 0a 20 20  E_STOREP2 ){..  
2596d 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70    pOut = &aMem[p
2596e 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 20 20 6d 65  Op->p2];..    me
2596f 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70  mAboutToChange(p
25970 2c 20 70 4f 75 74 29 3b 0d 0a 20 20 20 20 4d 65  , pOut);..    Me
25971 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
25972 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0d 0a 20 20  t, MEM_Int);..  
25973 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
25974 61 6a 2e 72 65 73 3b 0d 0a 20 20 20 20 52 45 47  aj.res;..    REG
25975 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
25976 3e 70 32 2c 20 70 4f 75 74 29 3b 0d 0a 20 20 7d  >p2, pOut);..  }
25977 65 6c 73 65 20 69 66 28 20 75 2e 61 6a 2e 72 65  else if( u.aj.re
25978 73 20 29 7b 0d 0a 20 20 20 20 70 63 20 3d 20 70  s ){..    pc = p
25979 4f 70 2d 3e 70 32 2d 31 3b 0d 0a 20 20 7d 0d 0a  Op->p2-1;..  }..
2597a 0d 0a 20 20 2f 2a 20 55 6e 64 6f 20 61 6e 79 20  ..  /* Undo any 
2597b 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20  changes made by 
2597c 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 20  applyAffinity() 
2597d 74 6f 20 74 68 65 20 69 6e 70 75 74 20 72 65 67  to the input reg
2597e 69 73 74 65 72 73 2e 20 2a 2f 0d 0a 20 20 70 49  isters. */..  pI
2597f 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 28 70 49 6e  n1->flags = (pIn
25980 31 2d 3e 66 6c 61 67 73 26 7e 4d 45 4d 5f 54 79  1->flags&~MEM_Ty
25981 70 65 4d 61 73 6b 29 20 7c 20 28 75 2e 61 6a 2e  peMask) | (u.aj.
25982 66 6c 61 67 73 31 26 4d 45 4d 5f 54 79 70 65 4d  flags1&MEM_TypeM
25983 61 73 6b 29 3b 0d 0a 20 20 70 49 6e 33 2d 3e 66  ask);..  pIn3->f
25984 6c 61 67 73 20 3d 20 28 70 49 6e 33 2d 3e 66 6c  lags = (pIn3->fl
25985 61 67 73 26 7e 4d 45 4d 5f 54 79 70 65 4d 61 73  ags&~MEM_TypeMas
25986 6b 29 20 7c 20 28 75 2e 61 6a 2e 66 6c 61 67 73  k) | (u.aj.flags
25987 33 26 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 29 3b  3&MEM_TypeMask);
25988 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d  ..  break;..}...
25989 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d  ./* Opcode: Perm
2598a 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34  utation * * * P4
2598b 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 74 20 74   *..**..** Set t
2598c 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75  he permutation u
2598d 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f  sed by the OP_Co
2598e 6d 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74  mpare operator t
2598f 6f 20 62 65 20 74 68 65 20 61 72 72 61 79 0d 0a  o be the array..
25990 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69  ** of integers i
25991 6e 20 50 34 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  n P4...**..** Th
25992 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 69 73  e permutation is
25993 20 6f 6e 6c 79 20 76 61 6c 69 64 20 75 6e 74 69   only valid unti
25994 6c 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 50 65  l the next OP_Pe
25995 72 6d 75 74 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f  rmutation, OP_Co
25996 6d 70 61 72 65 2c 0d 0a 2a 2a 20 4f 50 5f 48 61  mpare,..** OP_Ha
25997 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 73 75 6c 74  lt, or OP_Result
25998 52 6f 77 2e 20 20 54 79 70 69 63 61 6c 6c 79 20  Row.  Typically 
25999 74 68 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69  the OP_Permutati
2599a 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 0d  on should occur.
2599b 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  .** immediately 
2599c 70 72 69 6f 72 20 74 6f 20 74 68 65 20 4f 50 5f  prior to the OP_
2599d 43 6f 6d 70 61 72 65 2e 0d 0a 2a 2f 0d 0a 63 61  Compare...*/..ca
2599e 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  se OP_Permutatio
2599f 6e 3a 20 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  n: {..  assert( 
259a0 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
259a1 49 4e 54 41 52 52 41 59 20 29 3b 0d 0a 20 20 61  INTARRAY );..  a
259a2 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61  ssert( pOp->p4.a
259a3 69 20 29 3b 0d 0a 20 20 61 50 65 72 6d 75 74 65  i );..  aPermute
259a4 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0d 0a   = pOp->p4.ai;..
259a5 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
259a6 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72  * Opcode: Compar
259a7 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0d  e P1 P2 P3 P4 *.
259a8 0a 2a 2a 0d 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  .**..** Compare 
259a9 74 77 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72  two vectors of r
259aa 65 67 69 73 74 65 72 73 20 69 6e 20 72 65 67 28  egisters in reg(
259ab 50 31 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31  P1)..reg(P1+P3-1
259ac 29 20 28 63 61 6c 6c 20 74 68 69 73 0d 0a 2a 2a  ) (call this..**
259ad 20 76 65 63 74 6f 72 20 22 41 22 29 20 61 6e 64   vector "A") and
259ae 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65 67   in reg(P2)..reg
259af 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29 2e  (P2+P3-1) ("B").
259b0 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75 6c    Save the resul
259b1 74 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 63 6f 6d  t of..** the com
259b2 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20  parison for use 
259b3 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a  by the next OP_J
259b4 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0d 0a 2a  ump instruct...*
259b5 2a 0d 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65  *..** P4 is a Ke
259b6 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
259b7 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c  that defines col
259b8 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
259b9 20 61 6e 64 20 73 6f 72 74 0d 0a 2a 2a 20 6f 72   and sort..** or
259ba 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d  ders for the com
259bb 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65  parison.  The pe
259bc 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65  rmutation applie
259bd 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0d 0a  s to registers..
259be 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65  ** only.  The Ke
259bf 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61  yInfo elements a
259c0 72 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69  re used sequenti
259c1 61 6c 6c 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ally...**..** Th
259c2 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  e comparison is 
259c3 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f  a sort compariso
259c4 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70  n, so NULLs comp
259c5 61 72 65 20 65 71 75 61 6c 2c 0d 0a 2a 2a 20 4e  are equal,..** N
259c6 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 68  ULLs are less th
259c7 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62  an numbers, numb
259c8 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 61  ers are less tha
259c9 6e 20 73 74 72 69 6e 67 73 2c 0d 0a 2a 2a 20 61  n strings,..** a
259ca 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c  nd strings are l
259cb 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0d  ess than blobs..
259cc 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 43 6f 6d  .*/..case OP_Com
259cd 70 61 72 65 3a 20 7b 0d 0a 23 69 66 20 30 20 20  pare: {..#if 0  
259ce 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
259cf 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
259d0 61 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 3b 0d  ak */..  int n;.
259d1 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74  .  int i;..  int
259d2 20 70 31 3b 0d 0a 20 20 69 6e 74 20 70 32 3b 0d   p1;..  int p2;.
259d3 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f  .  const KeyInfo
259d4 20 2a 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 69   *pKeyInfo;..  i
259d5 6e 74 20 69 64 78 3b 0d 0a 20 20 43 6f 6c 6c 53  nt idx;..  CollS
259d6 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a  eq *pColl;    /*
259d7 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
259d8 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68  nce to use on th
259d9 69 73 20 74 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e  is term */..  in
259da 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20  t bRev;         
259db 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53   /* True for DES
259dc 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64  CENDING sort ord
259dd 65 72 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a  er */..#endif /*
259de 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
259df 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b   moved into u.ak
259e0 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 61 6b 2e 6e 20   */....  u.ak.n 
259e1 3d 20 70 4f 70 2d 3e 70 33 3b 0d 0a 20 20 75 2e  = pOp->p3;..  u.
259e2 61 6b 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f  ak.pKeyInfo = pO
259e3 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0d  p->p4.pKeyInfo;.
259e4 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6b 2e  .  assert( u.ak.
259e5 6e 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  n>0 );..  assert
259e6 28 20 75 2e 61 6b 2e 70 4b 65 79 49 6e 66 6f 21  ( u.ak.pKeyInfo!
259e7 3d 30 20 29 3b 0d 0a 20 20 75 2e 61 6b 2e 70 31  =0 );..  u.ak.p1
259e8 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a 20 20 75   = pOp->p1;..  u
259e9 2e 61 6b 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .ak.p2 = pOp->p2
259ea 3b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45  ;..#if SQLITE_DE
259eb 42 55 47 0d 0a 20 20 69 66 28 20 61 50 65 72 6d  BUG..  if( aPerm
259ec 75 74 65 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  ute ){..    int 
259ed 6b 2c 20 6d 78 20 3d 20 30 3b 0d 0a 20 20 20 20  k, mx = 0;..    
259ee 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61 6b 2e  for(k=0; k<u.ak.
259ef 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50 65 72  n; k++) if( aPer
259f0 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d 78 20  mute[k]>mx ) mx 
259f1 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b 0d 0a  = aPermute[k];..
259f2 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6b      assert( u.ak
259f3 2e 70 31 3e 30 20 26 26 20 75 2e 61 6b 2e 70 31  .p1>0 && u.ak.p1
259f4 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29  +mx<=p->nMem+1 )
259f5 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ;..    assert( u
259f6 2e 61 6b 2e 70 32 3e 30 20 26 26 20 75 2e 61 6b  .ak.p2>0 && u.ak
259f7 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b  .p2+mx<=p->nMem+
259f8 31 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  1 );..  }else{..
259f9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6b      assert( u.ak
259fa 2e 70 31 3e 30 20 26 26 20 75 2e 61 6b 2e 70 31  .p1>0 && u.ak.p1
259fb 2b 75 2e 61 6b 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d  +u.ak.n<=p->nMem
259fc 2b 31 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  +1 );..    asser
259fd 74 28 20 75 2e 61 6b 2e 70 32 3e 30 20 26 26 20  t( u.ak.p2>0 && 
259fe 75 2e 61 6b 2e 70 32 2b 75 2e 61 6b 2e 6e 3c 3d  u.ak.p2+u.ak.n<=
259ff 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0d 0a 20 20  p->nMem+1 );..  
25a00 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
25a01 49 54 45 5f 44 45 42 55 47 20 2a 2f 0d 0a 20 20  ITE_DEBUG */..  
25a02 66 6f 72 28 75 2e 61 6b 2e 69 3d 30 3b 20 75 2e  for(u.ak.i=0; u.
25a03 61 6b 2e 69 3c 75 2e 61 6b 2e 6e 3b 20 75 2e 61  ak.i<u.ak.n; u.a
25a04 6b 2e 69 2b 2b 29 7b 0d 0a 20 20 20 20 75 2e 61  k.i++){..    u.a
25a05 6b 2e 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65  k.idx = aPermute
25a06 20 3f 20 61 50 65 72 6d 75 74 65 5b 75 2e 61 6b   ? aPermute[u.ak
25a07 2e 69 5d 20 3a 20 75 2e 61 6b 2e 69 3b 0d 0a 20  .i] : u.ak.i;.. 
25a08 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73     assert( memIs
25a09 56 61 6c 69 64 28 26 61 4d 65 6d 5b 75 2e 61 6b  Valid(&aMem[u.ak
25a0a 2e 70 31 2b 75 2e 61 6b 2e 69 64 78 5d 29 20 29  .p1+u.ak.idx]) )
25a0b 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  ;..    assert( m
25a0c 65 6d 49 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b  emIsValid(&aMem[
25a0d 75 2e 61 6b 2e 70 32 2b 75 2e 61 6b 2e 69 64 78  u.ak.p2+u.ak.idx
25a0e 5d 29 20 29 3b 0d 0a 20 20 20 20 52 45 47 49 53  ]) );..    REGIS
25a0f 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6b 2e 70  TER_TRACE(u.ak.p
25a10 31 2b 75 2e 61 6b 2e 69 64 78 2c 20 26 61 4d 65  1+u.ak.idx, &aMe
25a11 6d 5b 75 2e 61 6b 2e 70 31 2b 75 2e 61 6b 2e 69  m[u.ak.p1+u.ak.i
25a12 64 78 5d 29 3b 0d 0a 20 20 20 20 52 45 47 49 53  dx]);..    REGIS
25a13 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6b 2e 70  TER_TRACE(u.ak.p
25a14 32 2b 75 2e 61 6b 2e 69 64 78 2c 20 26 61 4d 65  2+u.ak.idx, &aMe
25a15 6d 5b 75 2e 61 6b 2e 70 32 2b 75 2e 61 6b 2e 69  m[u.ak.p2+u.ak.i
25a16 64 78 5d 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  dx]);..    asser
25a17 74 28 20 75 2e 61 6b 2e 69 3c 75 2e 61 6b 2e 70  t( u.ak.i<u.ak.p
25a18 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
25a19 29 3b 0d 0a 20 20 20 20 75 2e 61 6b 2e 70 43 6f  );..    u.ak.pCo
25a1a 6c 6c 20 3d 20 75 2e 61 6b 2e 70 4b 65 79 49 6e  ll = u.ak.pKeyIn
25a1b 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6b 2e 69  fo->aColl[u.ak.i
25a1c 5d 3b 0d 0a 20 20 20 20 75 2e 61 6b 2e 62 52 65  ];..    u.ak.bRe
25a1d 76 20 3d 20 75 2e 61 6b 2e 70 4b 65 79 49 6e 66  v = u.ak.pKeyInf
25a1e 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 2e  o->aSortOrder[u.
25a1f 61 6b 2e 69 5d 3b 0d 0a 20 20 20 20 69 43 6f 6d  ak.i];..    iCom
25a20 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65  pare = sqlite3Me
25a21 6d 43 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75  mCompare(&aMem[u
25a22 2e 61 6b 2e 70 31 2b 75 2e 61 6b 2e 69 64 78 5d  .ak.p1+u.ak.idx]
25a23 2c 20 26 61 4d 65 6d 5b 75 2e 61 6b 2e 70 32 2b  , &aMem[u.ak.p2+
25a24 75 2e 61 6b 2e 69 64 78 5d 2c 20 75 2e 61 6b 2e  u.ak.idx], u.ak.
25a25 70 43 6f 6c 6c 29 3b 0d 0a 20 20 20 20 69 66 28  pColl);..    if(
25a26 20 69 43 6f 6d 70 61 72 65 20 29 7b 0d 0a 20 20   iCompare ){..  
25a27 20 20 20 20 69 66 28 20 75 2e 61 6b 2e 62 52 65      if( u.ak.bRe
25a28 76 20 29 20 69 43 6f 6d 70 61 72 65 20 3d 20 2d  v ) iCompare = -
25a29 69 43 6f 6d 70 61 72 65 3b 0d 0a 20 20 20 20 20  iCompare;..     
25a2a 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
25a2b 20 20 7d 0d 0a 20 20 61 50 65 72 6d 75 74 65 20    }..  aPermute 
25a2c 3d 20 30 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  = 0;..  break;..
25a2d 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
25a2e 4a 75 6d 70 20 50 31 20 50 32 20 50 33 20 2a 20  Jump P1 P2 P3 * 
25a2f 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4a 75 6d 70 20 74  *..**..** Jump t
25a30 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  o the instructio
25a31 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 31 2c  n at address P1,
25a32 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 65 6e   P2, or P3 depen
25a33 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0d  ding on whether.
25a34 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  .** in the most 
25a35 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 72  recent OP_Compar
25a36 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  e instruction th
25a37 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 20  e P1 vector was 
25a38 6c 65 73 73 20 74 68 61 6e 0d 0a 2a 2a 20 65 71  less than..** eq
25a39 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
25a3a 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
25a3b 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
25a3c 65 6c 79 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f  ely...*/..case O
25a3d 50 5f 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20  P_Jump: {       
25a3e 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
25a3f 0d 0a 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65  ..  if( iCompare
25a40 3c 30 20 29 7b 0d 0a 20 20 20 20 70 63 20 3d 20  <0 ){..    pc = 
25a41 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0d 0a 20 20  pOp->p1 - 1;..  
25a42 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6d 70 61  }else if( iCompa
25a43 72 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 63  re==0 ){..    pc
25a44 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d   = pOp->p2 - 1;.
25a45 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
25a46 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b  c = pOp->p3 - 1;
25a47 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d  ..  }..  break;.
25a48 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}..../* Opcode:
25a49 20 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20   And P1 P2 P3 * 
25a4a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 61 6b 65 20 74  *..**..** Take t
25a4b 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f  he logical AND o
25a4c 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20  f the values in 
25a4d 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
25a4e 20 50 32 20 61 6e 64 0d 0a 2a 2a 20 77 72 69 74   P2 and..** writ
25a4f 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
25a50 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0d 0a  o register P3...
25a51 2a 2a 0d 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  **..** If either
25a52 20 50 31 20 6f 72 20 50 32 20 69 73 20 30 20 28   P1 or P2 is 0 (
25a53 66 61 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20  false) then the 
25a54 72 65 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e  result is 0 even
25a55 20 69 66 0d 0a 2a 2a 20 74 68 65 20 6f 74 68 65   if..** the othe
25a56 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e  r input is NULL.
25a57 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75    A NULL and tru
25a58 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67  e or two NULLs g
25a59 69 76 65 0d 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f  ive..** a NULL o
25a5a 75 74 70 75 74 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f  utput...*/../* O
25a5b 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50 32 20  pcode: Or P1 P2 
25a5c 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54  P3 * *..**..** T
25a5d 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20  ake the logical 
25a5e 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  OR of the values
25a5f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
25a60 61 6e 64 20 50 32 20 61 6e 64 0d 0a 2a 2a 20 73  and P2 and..** s
25a61 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20  tore the answer 
25a62 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0d  in register P3..
25a63 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 65 69 74 68 65  .**..** If eithe
25a64 72 20 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f  r P1 or P2 is no
25a65 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74 68 65  nzero (true) the
25a66 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
25a67 31 20 28 74 72 75 65 29 0d 0a 2a 2a 20 65 76 65  1 (true)..** eve
25a68 6e 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 69  n if the other i
25a69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
25a6a 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20   NULL and false 
25a6b 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0d 0a 2a 2a  or two NULLs..**
25a6c 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74   give a NULL out
25a6d 70 75 74 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f  put...*/..case O
25a6e 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20  P_And:          
25a6f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
25a70 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c  K_AND, in1, in2,
25a71 20 6f 75 74 33 20 2a 2f 0d 0a 63 61 73 65 20 4f   out3 */..case O
25a72 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
25a73 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
25a74 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
25a75 6f 75 74 33 20 2a 2f 0d 0a 23 69 66 20 30 20 20  out3 */..#if 0  
25a76 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
25a77 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
25a78 61 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 76 31 3b  al */..  int v1;
25a79 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
25a7a 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20  and:  0==FALSE, 
25a7b 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e  1==TRUE, 2==UNKN
25a7c 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a  OWN or NULL */..
25a7d 20 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20    int v2;    /* 
25a7e 52 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30  Right operand: 0
25a7f 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45  ==FALSE, 1==TRUE
25a80 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20  , 2==UNKNOWN or 
25a81 4e 55 4c 4c 20 2a 2f 0d 0a 23 65 6e 64 69 66 20  NULL */..#endif 
25a82 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
25a83 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
25a84 61 6c 20 2a 2f 0d 0a 0d 0a 20 20 70 49 6e 31 20  al */....  pIn1 
25a85 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
25a86 3b 0d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ;..  if( pIn1->f
25a87 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
25a88 29 7b 0d 0a 20 20 20 20 75 2e 61 6c 2e 76 31 20  ){..    u.al.v1 
25a89 3d 20 32 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  = 2;..  }else{..
25a8a 20 20 20 20 75 2e 61 6c 2e 76 31 20 3d 20 73 71      u.al.v1 = sq
25a8b 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
25a8c 65 28 70 49 6e 31 29 21 3d 30 3b 0d 0a 20 20 7d  e(pIn1)!=0;..  }
25a8d 0d 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d  ..  pIn2 = &aMem
25a8e 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 69 66  [pOp->p2];..  if
25a8f 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20  ( pIn2->flags & 
25a90 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20  MEM_Null ){..   
25a91 20 75 2e 61 6c 2e 76 32 20 3d 20 32 3b 0d 0a 20   u.al.v2 = 2;.. 
25a92 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 75 2e 61   }else{..    u.a
25a93 6c 2e 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64  l.v2 = sqlite3Vd
25a94 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
25a95 21 3d 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  !=0;..  }..  if(
25a96 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
25a97 5f 41 6e 64 20 29 7b 0d 0a 20 20 20 20 73 74 61  _And ){..    sta
25a98 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
25a99 65 64 20 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69  ed char and_logi
25a9a 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c  c[] = { 0, 0, 0,
25a9b 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20   0, 1, 2, 0, 2, 
25a9c 32 20 7d 3b 0d 0a 20 20 20 20 75 2e 61 6c 2e 76  2 };..    u.al.v
25a9d 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e  1 = and_logic[u.
25a9e 61 6c 2e 76 31 2a 33 2b 75 2e 61 6c 2e 76 32 5d  al.v1*3+u.al.v2]
25a9f 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
25aa0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
25aa1 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c  signed char or_l
25aa2 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c  ogic[] = { 0, 1,
25aa3 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20   2, 1, 1, 1, 2, 
25aa4 31 2c 20 32 20 7d 3b 0d 0a 20 20 20 20 75 2e 61  1, 2 };..    u.a
25aa5 6c 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b  l.v1 = or_logic[
25aa6 75 2e 61 6c 2e 76 31 2a 33 2b 75 2e 61 6c 2e 76  u.al.v1*3+u.al.v
25aa7 32 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4f 75 74  2];..  }..  pOut
25aa8 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
25aa9 5d 3b 0d 0a 20 20 69 66 28 20 75 2e 61 6c 2e 76  ];..  if( u.al.v
25aaa 31 3d 3d 32 20 29 7b 0d 0a 20 20 20 20 4d 65 6d  1==2 ){..    Mem
25aab 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
25aac 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0d 0a 20 20  , MEM_Null);..  
25aad 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 4f 75 74  }else{..    pOut
25aae 2d 3e 75 2e 69 20 3d 20 75 2e 61 6c 2e 76 31 3b  ->u.i = u.al.v1;
25aaf 0d 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ..    MemSetType
25ab0 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
25ab1 6e 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65  nt);..  }..  bre
25ab2 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
25ab3 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a  ode: Not P1 P2 *
25ab4 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 74   * *..**..** Int
25ab5 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65  erpret the value
25ab6 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
25ab7 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  as a boolean val
25ab8 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0d 0a  ue.  Store the..
25ab9 2a 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c  ** boolean compl
25aba 65 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65  ement in registe
25abb 72 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61  r P2.  If the va
25abc 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
25abd 50 31 20 69 73 20 0d 0a 2a 2a 20 4e 55 4c 4c 2c  P1 is ..** NULL,
25abe 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20   then a NULL is 
25abf 73 74 6f 72 65 64 20 69 6e 20 50 32 2e 0d 0a 2a  stored in P2...*
25ac0 2f 0d 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 3a 20  /..case OP_Not: 
25ac1 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
25ac2 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
25ac3 4f 54 2c 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f  OT, in1, out2 */
25ac4 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  ..  pIn1 = &aMem
25ac5 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 70 4f  [pOp->p1];..  pO
25ac6 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  ut = &aMem[pOp->
25ac7 70 32 5d 3b 0d 0a 20 20 69 66 28 20 70 49 6e 31  p2];..  if( pIn1
25ac8 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
25ac9 6c 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ll ){..    sqlit
25aca 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
25acb 28 70 4f 75 74 29 3b 0d 0a 20 20 7d 65 6c 73 65  (pOut);..  }else
25acc 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
25acd 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f  beMemSetInt64(pO
25ace 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 65  ut, !sqlite3Vdbe
25acf 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b  IntValue(pIn1));
25ad0 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d  ..  }..  break;.
25ad1 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}..../* Opcode:
25ad2 20 42 69 74 4e 6f 74 20 50 31 20 50 32 20 2a 20   BitNot P1 P2 * 
25ad3 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 74 65  * *..**..** Inte
25ad4 72 70 72 65 74 20 74 68 65 20 63 6f 6e 74 65 6e  rpret the conten
25ad5 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  t of register P1
25ad6 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   as an integer. 
25ad7 20 53 74 6f 72 65 20 74 68 65 0d 0a 2a 2a 20 6f   Store the..** o
25ad8 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f  nes-complement o
25ad9 66 20 74 68 65 20 50 31 20 76 61 6c 75 65 20 69  f the P1 value i
25ada 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
25adb 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0d 0a 2a    If P1 holds..*
25adc 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74  * a NULL then st
25add 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50 32  ore a NULL in P2
25ade 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 42  ...*/..case OP_B
25adf 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  itNot: {        
25ae0 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
25ae1 54 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 2c 20  TK_BITNOT, in1, 
25ae2 6f 75 74 32 20 2a 2f 0d 0a 20 20 70 49 6e 31 20  out2 */..  pIn1 
25ae3 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
25ae4 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ;..  pOut = &aMe
25ae5 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 69  m[pOp->p2];..  i
25ae6 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
25ae7 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20   MEM_Null ){..  
25ae8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
25ae9 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0d 0a  SetNull(pOut);..
25aea 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71    }else{..    sq
25aeb 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
25aec 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69  nt64(pOut, ~sqli
25aed 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
25aee 70 49 6e 31 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20  pIn1));..  }..  
25aef 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20  break;..}..../* 
25af0 4f 70 63 6f 64 65 3a 20 4f 6e 63 65 20 50 31 20  Opcode: Once P1 
25af1 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  P2 * * *..**..**
25af2 20 43 68 65 63 6b 20 69 66 20 4f 50 5f 4f 6e 63   Check if OP_Onc
25af3 65 20 66 6c 61 67 20 50 31 20 69 73 20 73 65 74  e flag P1 is set
25af4 2e 20 49 66 20 73 6f 2c 20 6a 75 6d 70 20 74 6f  . If so, jump to
25af5 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
25af6 20 4f 74 68 65 72 77 69 73 65 2c 0d 0a 2a 2a 20   Otherwise,..** 
25af7 73 65 74 20 74 68 65 20 66 6c 61 67 20 61 6e 64  set the flag and
25af8 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
25af9 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
25afa 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53  ction...**..** S
25afb 65 65 20 61 6c 73 6f 3a 20 4a 75 6d 70 4f 6e 63  ee also: JumpOnc
25afc 65 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4f  e..*/..case OP_O
25afd 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20  nce: {          
25afe 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0d 0a 20     /* jump */.. 
25aff 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
25b00 3c 70 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20 29 3b  <p->nOnceFlag );
25b01 0d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 6e 63 65  ..  if( p->aOnce
25b02 46 6c 61 67 5b 70 4f 70 2d 3e 70 31 5d 20 29 7b  Flag[pOp->p1] ){
25b03 0d 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  ..    pc = pOp->
25b04 70 32 2d 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  p2-1;..  }else{.
25b05 0a 20 20 20 20 70 2d 3e 61 4f 6e 63 65 46 6c 61  .    p->aOnceFla
25b06 67 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 31 3b 0d  g[pOp->p1] = 1;.
25b07 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  .  }..  break;..
25b08 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
25b09 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0d  If P1 P2 P3 * *.
25b0a 0a 2a 2a 0d 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  .**..** Jump to 
25b0b 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
25b0c 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
25b0d 73 20 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c  s true.  The val
25b0e 75 65 0d 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64  ue..** is consid
25b0f 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20  ered true if it 
25b10 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e  is numeric and n
25b11 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
25b12 20 76 61 6c 75 65 0d 0a 2a 2a 20 69 6e 20 50 31   value..** in P1
25b13 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
25b14 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50  ke the jump if P
25b15 33 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0d 0a  3 is non-zero...
25b16 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  */../* Opcode: I
25b17 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20  fNot P1 P2 P3 * 
25b18 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4a 75 6d 70 20 74  *..**..** Jump t
25b19 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75  o P2 if the valu
25b1a 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
25b1b 20 69 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20   is False.  The 
25b1c 76 61 6c 75 65 0d 0a 2a 2a 20 69 73 20 63 6f 6e  value..** is con
25b1d 73 69 64 65 72 65 64 20 66 61 6c 73 65 20 69 66  sidered false if
25b1e 20 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69   it has a numeri
25b1f 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e  c value of zero.
25b20 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0d 0a    If the value..
25b21 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c  ** in P1 is NULL
25b22 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
25b23 75 6d 70 20 69 66 20 50 33 20 69 73 20 7a 65 72  ump if P3 is zer
25b24 6f 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  o...*/..case OP_
25b25 49 66 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  If:             
25b26 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
25b27 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49 66 4e   */..case OP_IfN
25b28 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ot: {           
25b29 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
25b2a 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
25b2b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25b2c 64 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0d 0a  d into u.am */..
25b2d 20 20 69 6e 74 20 63 3b 0d 0a 23 65 6e 64 69 66    int c;..#endif
25b2e 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25b2f 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25b30 2e 61 6d 20 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d  .am */..  pIn1 =
25b31 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
25b32 0d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c  ..  if( pIn1->fl
25b33 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
25b34 7b 0d 0a 20 20 20 20 75 2e 61 6d 2e 63 20 3d 20  {..    u.am.c = 
25b35 70 4f 70 2d 3e 70 33 3b 0d 0a 20 20 7d 65 6c 73  pOp->p3;..  }els
25b36 65 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e{..#ifdef SQLIT
25b37 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
25b38 50 4f 49 4e 54 0d 0a 20 20 20 20 75 2e 61 6d 2e  POINT..    u.am.
25b39 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  c = sqlite3VdbeI
25b3a 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  ntValue(pIn1)!=0
25b3b 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 75 2e  ;..#else..    u.
25b3c 61 6d 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64  am.c = sqlite3Vd
25b3d 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31  beRealValue(pIn1
25b3e 29 21 3d 30 2e 30 3b 0d 0a 23 65 6e 64 69 66 0d  )!=0.0;..#endif.
25b3f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
25b40 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29  code==OP_IfNot )
25b41 20 75 2e 61 6d 2e 63 20 3d 20 21 75 2e 61 6d 2e   u.am.c = !u.am.
25b42 63 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 75  c;..  }..  if( u
25b43 2e 61 6d 2e 63 20 29 7b 0d 0a 20 20 20 20 70 63  .am.c ){..    pc
25b44 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0d 0a 20   = pOp->p2-1;.. 
25b45 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d   }..  break;..}.
25b46 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73  .../* Opcode: Is
25b47 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a  Null P1 P2 * * *
25b48 0d 0a 2a 2a 0d 0a 2a 2a 20 4a 75 6d 70 20 74 6f  ..**..** Jump to
25b49 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
25b4a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
25b4b 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 63 61  is NULL...*/..ca
25b4c 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20  se OP_IsNull: { 
25b4d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
25b4e 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c  me as TK_ISNULL,
25b4f 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0d 0a 20   jump, in1 */.. 
25b50 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
25b51 70 2d 3e 70 31 5d 3b 0d 0a 20 20 69 66 28 20 28  p->p1];..  if( (
25b52 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
25b53 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0d 0a 20  M_Null)!=0 ){.. 
25b54 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
25b55 2d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65  - 1;..  }..  bre
25b56 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
25b57 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20  ode: NotNull P1 
25b58 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  P2 * * *..**..**
25b59 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
25b5a 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
25b5b 73 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e  ster P1 is not N
25b5c 55 4c 4c 2e 20 20 0d 0a 2a 2f 0d 0a 63 61 73 65  ULL.  ..*/..case
25b5d 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20   OP_NotNull: {  
25b5e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
25b5f 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c  e as TK_NOTNULL,
25b60 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0d 0a 20   jump, in1 */.. 
25b61 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
25b62 70 2d 3e 70 31 5d 3b 0d 0a 20 20 69 66 28 20 28  p->p1];..  if( (
25b63 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
25b64 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0d 0a 20  M_Null)==0 ){.. 
25b65 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
25b66 2d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65  - 1;..  }..  bre
25b67 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
25b68 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50  ode: Column P1 P
25b69 32 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a 0d 0a  2 P3 P4 P5..**..
25b6a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
25b6b 20 64 61 74 61 20 74 68 61 74 20 63 75 72 73 6f   data that curso
25b6c 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 20 61  r P1 points to a
25b6d 73 20 61 20 73 74 72 75 63 74 75 72 65 20 62 75  s a structure bu
25b6e 69 6c 74 20 75 73 69 6e 67 0d 0a 2a 2a 20 74 68  ilt using..** th
25b6f 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73  e MakeRecord ins
25b70 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65 20  truction.  (See 
25b71 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f  the MakeRecord o
25b72 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
25b73 6f 6e 61 6c 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61  onal..** informa
25b74 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
25b75 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74  ormat of the dat
25b76 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 65  a.)  Extract the
25b77 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0d 0a 2a   P2-th column..*
25b78 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f  * from this reco
25b79 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  rd.  If there ar
25b7a 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b  e less that (P2+
25b7b 31 29 20 0d 0a 2a 2a 20 76 61 6c 75 65 73 20 69  1) ..** values i
25b7c 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78  n the record, ex
25b7d 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0d 0a 2a  tract a NULL...*
25b7e 2a 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  *..** The value 
25b7f 65 78 74 72 61 63 74 65 64 20 69 73 20 73 74 6f  extracted is sto
25b80 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
25b81 50 33 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  P3...**..** If t
25b82 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  he column contai
25b83 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32  ns fewer than P2
25b84 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78   fields, then ex
25b85 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f  tract a NULL.  O
25b86 72 2c 0d 0a 2a 2a 20 69 66 20 74 68 65 20 50 34  r,..** if the P4
25b87 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50   argument is a P
25b88 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61  4_MEM use the va
25b89 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 72  lue of the P4 ar
25b8a 67 75 6d 65 6e 74 20 61 73 0d 0a 2a 2a 20 74 68  gument as..** th
25b8b 65 20 72 65 73 75 6c 74 2e 0d 0a 2a 2a 0d 0a 2a  e result...**..*
25b8c 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f  * If the OPFLAG_
25b8d 43 4c 45 41 52 43 41 43 48 45 20 62 69 74 20 69  CLEARCACHE bit i
25b8e 73 20 73 65 74 20 6f 6e 20 50 35 20 61 6e 64 20  s set on P5 and 
25b8f 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d 74  P1 is a pseudo-t
25b90 61 62 6c 65 20 63 75 72 73 6f 72 2c 0d 0a 2a 2a  able cursor,..**
25b91 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 20   then the cache 
25b92 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  of the cursor is
25b93 20 72 65 73 65 74 20 70 72 69 6f 72 20 74 6f 20   reset prior to 
25b94 65 78 74 72 61 63 74 69 6e 67 20 74 68 65 20 63  extracting the c
25b95 6f 6c 75 6d 6e 2e 0d 0a 2a 2a 20 54 68 65 20 66  olumn...** The f
25b96 69 72 73 74 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61  irst OP_Column a
25b97 67 61 69 6e 73 74 20 61 20 70 73 65 75 64 6f 2d  gainst a pseudo-
25b98 74 61 62 6c 65 20 61 66 74 65 72 20 74 68 65 20  table after the 
25b99 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6e  value of the con
25b9a 74 65 6e 74 0d 0a 2a 2a 20 72 65 67 69 73 74 65  tent..** registe
25b9b 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 73 68  r has changed sh
25b9c 6f 75 6c 64 20 68 61 76 65 20 74 68 69 73 20 62  ould have this b
25b9d 69 74 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 63 61 73  it set...*/..cas
25b9e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0d 0a  e OP_Column: {..
25b9f 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
25ba0 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
25ba1 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0d 0a 20 20  into u.an */..  
25ba2 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b  u32 payloadSize;
25ba3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
25ba4 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63  bytes in the rec
25ba5 6f 72 64 20 2a 2f 0d 0a 20 20 69 36 34 20 70 61  ord */..  i64 pa
25ba6 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20  yloadSize64; /* 
25ba7 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
25ba8 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
25ba9 0d 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20  ..  int p1;     
25baa 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c         /* P1 val
25bab 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
25bac 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 32 3b 20 20   */..  int p2;  
25bad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c            /* col
25bae 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65  umn number to re
25baf 74 72 69 65 76 65 20 2a 2f 0d 0a 20 20 56 64 62  trieve */..  Vdb
25bb0 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20  eCursor *pC;    
25bb1 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
25bb2 6f 72 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  or */..  char *z
25bb3 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
25bb4 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
25bb5 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
25bb6 2f 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  /..  BtCursor *p
25bb7 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42  Crsr;   /* The B
25bb8 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0d 0a  Tree cursor */..
25bb9 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20    u32 *aType;   
25bba 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d       /* aType[i]
25bbb 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72   holds the numer
25bbc 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69  ic type of the i
25bbd 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20  -th column */.. 
25bbe 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20   u32 *aOffset;  
25bbf 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69      /* aOffset[i
25bc0 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73  ] is offset to s
25bc1 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72  tart of data for
25bc2 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0d   i-th column */.
25bc3 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20  .  int nField;  
25bc4 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20        /* number 
25bc5 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
25bc6 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 69 6e   record */..  in
25bc7 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  t len;          
25bc8 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f   /* The length o
25bc9 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  f the serialized
25bca 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f   data for the co
25bcb 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  lumn */..  int i
25bcc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
25bcd 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
25bce 0d 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  ..  char *zData;
25bcf 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f         /* Part o
25bd0 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69  f the record bei
25bd1 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0d 0a 20  ng decoded */.. 
25bd2 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20   Mem *pDest;    
25bd3 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
25bd4 77 72 69 74 65 20 74 68 65 20 65 78 74 72 61 63  write the extrac
25bd5 74 65 64 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20  ted value */..  
25bd6 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20  Mem sMem;       
25bd7 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e     /* For storin
25bd8 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69  g the record bei
25bd9 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0d 0a 20  ng decoded */.. 
25bda 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20   u8 *zIdx;      
25bdb 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74      /* Index int
25bdc 6f 20 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20 75  o header */..  u
25bdd 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20  8 *zEndHdr;     
25bde 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
25bdf 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72  first byte after
25be0 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0d 0a   the header */..
25be1 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20    u32 offset;   
25be2 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
25be3 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0d  nto the data */.
25be4 0a 20 20 75 33 32 20 73 7a 46 69 65 6c 64 3b 20  .  u32 szField; 
25be5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
25be6 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
25be7 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 66 69 65  content of a fie
25be8 6c 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 7a 48  ld */..  int szH
25be9 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  dr;         /* S
25bea 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65  ize of the heade
25beb 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20  r size field at 
25bec 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20  start of record 
25bed 2a 2f 0d 0a 20 20 69 6e 74 20 61 76 61 69 6c 3b  */..  int avail;
25bee 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
25bef 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61  er of bytes of a
25bf0 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f  vailable data */
25bf1 0d 0a 20 20 75 33 32 20 74 3b 20 20 20 20 20 20  ..  u32 t;      
25bf2 20 20 20 20 20 20 20 2f 2a 20 41 20 74 79 70 65         /* A type
25bf3 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 72   code from the r
25bf4 65 63 6f 72 64 20 68 65 61 64 65 72 20 2a 2f 0d  ecord header */.
25bf5 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 20 20  .  Mem *pReg;   
25bf6 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64 6f 54        /* PseudoT
25bf7 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67 69 73  able input regis
25bf8 74 65 72 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f  ter */..#endif /
25bf9 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25bfa 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
25bfb 6e 20 2a 2f 0d 0a 0d 0a 0d 0a 20 20 75 2e 61 6e  n */......  u.an
25bfc 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a  .p1 = pOp->p1;..
25bfd 20 20 75 2e 61 6e 2e 70 32 20 3d 20 70 4f 70 2d    u.an.p2 = pOp-
25bfe 3e 70 32 3b 0d 0a 20 20 75 2e 61 6e 2e 70 43 20  >p2;..  u.an.pC 
25bff 3d 20 30 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26  = 0;..  memset(&
25c00 75 2e 61 6e 2e 73 4d 65 6d 2c 20 30 2c 20 73 69  u.an.sMem, 0, si
25c01 7a 65 6f 66 28 75 2e 61 6e 2e 73 4d 65 6d 29 29  zeof(u.an.sMem))
25c02 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  ;..  assert( u.a
25c03 6e 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  n.p1<p->nCursor 
25c04 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f  );..  assert( pO
25c05 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p3>0 && pOp->
25c06 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a  p3<=p->nMem );..
25c07 20 20 75 2e 61 6e 2e 70 44 65 73 74 20 3d 20 26    u.an.pDest = &
25c08 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a  aMem[pOp->p3];..
25c09 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e    memAboutToChan
25c0a 67 65 28 70 2c 20 75 2e 61 6e 2e 70 44 65 73 74  ge(p, u.an.pDest
25c0b 29 3b 0d 0a 20 20 75 2e 61 6e 2e 7a 52 65 63 20  );..  u.an.zRec 
25c0c 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69  = 0;....  /* Thi
25c0d 73 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65  s block sets the
25c0e 20 76 61 72 69 61 62 6c 65 20 75 2e 61 6e 2e 70   variable u.an.p
25c0f 61 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65  ayloadSize to be
25c10 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
25c11 72 20 6f 66 0d 0a 20 20 2a 2a 20 62 79 74 65 73  r of..  ** bytes
25c12 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0d   in the record..
25c13 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 75 2e 61 6e  .  **..  ** u.an
25c14 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20  .zRec is set to 
25c15 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  be the complete 
25c16 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 6f  text of the reco
25c17 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 69  rd if it is avai
25c18 6c 61 62 6c 65 2e 0d 0a 20 20 2a 2a 20 54 68 65  lable...  ** The
25c19 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
25c1a 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20   text is always 
25c1b 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73  available for ps
25c1c 65 75 64 6f 2d 74 61 62 6c 65 73 0d 0a 20 20 2a  eudo-tables..  *
25c1d 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
25c1e 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63  is stored in a c
25c1f 75 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c  ursor, the compl
25c20 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0d  ete record text.
25c21 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61  .  ** might be a
25c22 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
25c23 20 75 2e 61 6e 2e 70 43 2d 3e 61 52 6f 77 20 63   u.an.pC->aRow c
25c24 61 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67  ache.  Or it mig
25c25 68 74 20 6e 6f 74 20 62 65 2e 0d 0a 20 20 2a 2a  ht not be...  **
25c26 20 49 66 20 74 68 65 20 64 61 74 61 20 69 73 20   If the data is 
25c27 75 6e 61 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e  unavailable,  u.
25c28 61 6e 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74  an.zRec is set t
25c29 6f 20 4e 55 4c 4c 2e 0d 0a 20 20 2a 2a 0d 0a 20  o NULL...  **.. 
25c2a 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f 6d 70   ** We also comp
25c2b 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ute the number o
25c2c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
25c2d 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 63 75   record.  For cu
25c2e 72 73 6f 72 73 2c 0d 0a 20 20 2a 2a 20 74 68 65  rsors,..  ** the
25c2f 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
25c30 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ns is stored in 
25c31 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 6e  the VdbeCursor.n
25c32 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0d 0a  Field element...
25c33 20 20 2a 2f 0d 0a 20 20 75 2e 61 6e 2e 70 43 20    */..  u.an.pC 
25c34 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6e 2e  = p->apCsr[u.an.
25c35 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  p1];..  assert( 
25c36 75 2e 61 6e 2e 70 43 21 3d 30 20 29 3b 0d 0a 23  u.an.pC!=0 );..#
25c37 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25c38 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d  IT_VIRTUALTABLE.
25c39 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6e 2e  .  assert( u.an.
25c3a 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d  pC->pVtabCursor=
25c3b 3d 30 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  =0 );..#endif.. 
25c3c 20 75 2e 61 6e 2e 70 43 72 73 72 20 3d 20 75 2e   u.an.pCrsr = u.
25c3d 61 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0d  an.pC->pCursor;.
25c3e 0a 20 20 69 66 28 20 75 2e 61 6e 2e 70 43 72 73  .  if( u.an.pCrs
25c3f 72 21 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  r!=0 ){..    /* 
25c40 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74  The record is st
25c41 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 65 65  ored in a B-Tree
25c42 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   */..    rc = sq
25c43 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
25c44 6f 76 65 74 6f 28 75 2e 61 6e 2e 70 43 29 3b 0d  oveto(u.an.pC);.
25c45 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
25c46 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
25c47 65 72 72 6f 72 3b 0d 0a 20 20 20 20 69 66 28 20  error;..    if( 
25c48 75 2e 61 6e 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.an.pC->nullRow
25c49 20 29 7b 0d 0a 20 20 20 20 20 20 75 2e 61 6e 2e   ){..      u.an.
25c4a 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b  payloadSize = 0;
25c4b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
25c4c 75 2e 61 6e 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.an.pC->cacheSt
25c4d 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
25c4e 72 20 29 7b 0d 0a 20 20 20 20 20 20 75 2e 61 6e  r ){..      u.an
25c4f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75  .payloadSize = u
25c50 2e 61 6e 2e 70 43 2d 3e 70 61 79 6c 6f 61 64 53  .an.pC->payloadS
25c51 69 7a 65 3b 0d 0a 20 20 20 20 20 20 75 2e 61 6e  ize;..      u.an
25c52 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 75  .zRec = (char*)u
25c53 2e 61 6e 2e 70 43 2d 3e 61 52 6f 77 3b 0d 0a 20  .an.pC->aRow;.. 
25c54 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61     }else if( u.a
25c55 6e 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b  n.pC->isIndex ){
25c56 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
25c57 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
25c58 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6e 2e 70  orIsValid(u.an.p
25c59 43 72 73 72 29 20 29 3b 0d 0a 20 20 20 20 20 20  Crsr) );..      
25c5a 56 56 41 5f 4f 4e 4c 59 28 72 63 20 3d 29 20 73  VVA_ONLY(rc =) s
25c5b 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
25c5c 7a 65 28 75 2e 61 6e 2e 70 43 72 73 72 2c 20 26  ze(u.an.pCrsr, &
25c5d 75 2e 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.an.payloadSize
25c5e 36 34 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  64);..      asse
25c5f 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
25c60 4b 20 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 62  K );   /* True b
25c61 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72  ecause of Cursor
25c62 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 61 62  Moveto() call ab
25c63 6f 76 65 20 2a 2f 0d 0a 20 20 20 20 20 20 2f 2a  ove */..      /*
25c64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
25c65 73 65 43 65 6c 6c 50 74 72 28 29 20 75 73 65 73  seCellPtr() uses
25c66 20 67 65 74 56 61 72 69 6e 74 33 32 28 29 20 74   getVarint32() t
25c67 6f 20 65 78 74 72 61 63 74 20 74 68 65 0d 0a 20  o extract the.. 
25c68 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61 64 20       ** payload 
25c69 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73 20 69  size, so it is i
25c6a 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 75 2e  mpossible for u.
25c6b 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34  an.payloadSize64
25c6c 20 74 6f 20 62 65 0d 0a 20 20 20 20 20 20 2a 2a   to be..      **
25c6d 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32 20   larger than 32 
25c6e 62 69 74 73 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  bits. */..      
25c6f 61 73 73 65 72 74 28 20 28 75 2e 61 6e 2e 70 61  assert( (u.an.pa
25c70 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51  yloadSize64 & SQ
25c71 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
25c72 75 36 34 29 75 2e 61 6e 2e 70 61 79 6c 6f 61 64  u64)u.an.payload
25c73 53 69 7a 65 36 34 20 29 3b 0d 0a 20 20 20 20 20  Size64 );..     
25c74 20 75 2e 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a   u.an.payloadSiz
25c75 65 20 3d 20 28 75 33 32 29 75 2e 61 6e 2e 70 61  e = (u32)u.an.pa
25c76 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0d 0a 20 20  yloadSize64;..  
25c77 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
25c78 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
25c79 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
25c7a 64 28 75 2e 61 6e 2e 70 43 72 73 72 29 20 29 3b  d(u.an.pCrsr) );
25c7b 0d 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59  ..      VVA_ONLY
25c7c 28 72 63 20 3d 29 20 73 71 6c 69 74 65 33 42 74  (rc =) sqlite3Bt
25c7d 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e 61 6e  reeDataSize(u.an
25c7e 2e 70 43 72 73 72 2c 20 26 75 2e 61 6e 2e 70 61  .pCrsr, &u.an.pa
25c7f 79 6c 6f 61 64 53 69 7a 65 29 3b 0d 0a 20 20 20  yloadSize);..   
25c80 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
25c81 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a  QLITE_OK );   /*
25c82 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e 6e   DataSize() cann
25c83 6f 74 20 66 61 69 6c 20 2a 2f 0d 0a 20 20 20 20  ot fail */..    
25c84 7d 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 41  }..  }else if( A
25c85 4c 57 41 59 53 28 75 2e 61 6e 2e 70 43 2d 3e 70  LWAYS(u.an.pC->p
25c86 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3e 30 29  seudoTableReg>0)
25c87 20 29 7b 0d 0a 20 20 20 20 75 2e 61 6e 2e 70 52   ){..    u.an.pR
25c88 65 67 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 6e 2e  eg = &aMem[u.an.
25c89 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52  pC->pseudoTableR
25c8a 65 67 5d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  eg];..    assert
25c8b 28 20 75 2e 61 6e 2e 70 52 65 67 2d 3e 66 6c 61  ( u.an.pReg->fla
25c8c 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b  gs & MEM_Blob );
25c8d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ..    assert( me
25c8e 6d 49 73 56 61 6c 69 64 28 75 2e 61 6e 2e 70 52  mIsValid(u.an.pR
25c8f 65 67 29 20 29 3b 0d 0a 20 20 20 20 75 2e 61 6e  eg) );..    u.an
25c90 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75  .payloadSize = u
25c91 2e 61 6e 2e 70 52 65 67 2d 3e 6e 3b 0d 0a 20 20  .an.pReg->n;..  
25c92 20 20 75 2e 61 6e 2e 7a 52 65 63 20 3d 20 75 2e    u.an.zRec = u.
25c93 61 6e 2e 70 52 65 67 2d 3e 7a 3b 0d 0a 20 20 20  an.pReg->z;..   
25c94 20 75 2e 61 6e 2e 70 43 2d 3e 63 61 63 68 65 53   u.an.pC->cacheS
25c95 74 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35  tatus = (pOp->p5
25c96 26 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43  &OPFLAG_CLEARCAC
25c97 48 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c  HE) ? CACHE_STAL
25c98 45 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b  E : p->cacheCtr;
25c99 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ..    assert( u.
25c9a 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d  an.payloadSize==
25c9b 30 20 7c 7c 20 75 2e 61 6e 2e 7a 52 65 63 21 3d  0 || u.an.zRec!=
25c9c 30 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  0 );..  }else{..
25c9d 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20      /* Consider 
25c9e 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 4e 55  the row to be NU
25c9f 4c 4c 20 2a 2f 0d 0a 20 20 20 20 75 2e 61 6e 2e  LL */..    u.an.
25ca0 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b  payloadSize = 0;
25ca1 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ..  }....  /* If
25ca2 20 75 2e 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a   u.an.payloadSiz
25ca3 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73  e is 0, then jus
25ca4 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 2e 20  t store a NULL. 
25ca5 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   This can happen
25ca6 20 62 65 63 61 75 73 65 20 6f 66 0d 0a 20 20 2a   because of..  *
25ca7 2a 20 6e 75 6c 6c 52 6f 77 20 6f 72 20 62 65 63  * nullRow or bec
25ca8 61 75 73 65 20 6f 66 20 61 20 63 6f 72 72 75 70  ause of a corrup
25ca9 74 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0d 0a  t database. */..
25caa 20 20 69 66 28 20 75 2e 61 6e 2e 70 61 79 6c 6f    if( u.an.paylo
25cab 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0d 0a 20 20  adSize==0 ){..  
25cac 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
25cad 28 75 2e 61 6e 2e 70 44 65 73 74 2c 20 4d 45 4d  (u.an.pDest, MEM
25cae 5f 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 67 6f 74  _Null);..    got
25caf 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
25cb0 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
25cb1 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
25cb2 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
25cb3 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 75 2e  >=0 );..  if( u.
25cb4 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3e  an.payloadSize >
25cb5 20 28 75 33 32 29 64 62 2d 3e 61 4c 69 6d 69 74   (u32)db->aLimit
25cb6 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
25cb7 4e 47 54 48 5d 20 29 7b 0d 0a 20 20 20 20 67 6f  NGTH] ){..    go
25cb8 74 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a 20 20 7d  to too_big;..  }
25cb9 0d 0a 0d 0a 20 20 75 2e 61 6e 2e 6e 46 69 65 6c  ....  u.an.nFiel
25cba 64 20 3d 20 75 2e 61 6e 2e 70 43 2d 3e 6e 46 69  d = u.an.pC->nFi
25cbb 65 6c 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  eld;..  assert( 
25cbc 75 2e 61 6e 2e 70 32 3c 75 2e 61 6e 2e 6e 46 69  u.an.p2<u.an.nFi
25cbd 65 6c 64 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52  eld );....  /* R
25cbe 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 74 68  ead and parse th
25cbf 65 20 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20  e table header. 
25cc0 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
25cc1 74 73 20 6f 66 20 74 68 65 20 70 61 72 73 65 0d  ts of the parse.
25cc2 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72  .  ** into the r
25cc3 65 63 6f 72 64 20 68 65 61 64 65 72 20 63 61 63  ecord header cac
25cc4 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  he fields of the
25cc5 20 63 75 72 73 6f 72 2e 0d 0a 20 20 2a 2f 0d 0a   cursor...  */..
25cc6 20 20 75 2e 61 6e 2e 61 54 79 70 65 20 3d 20 75    u.an.aType = u
25cc7 2e 61 6e 2e 70 43 2d 3e 61 54 79 70 65 3b 0d 0a  .an.pC->aType;..
25cc8 20 20 69 66 28 20 75 2e 61 6e 2e 70 43 2d 3e 63    if( u.an.pC->c
25cc9 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63  acheStatus==p->c
25cca 61 63 68 65 43 74 72 20 29 7b 0d 0a 20 20 20 20  acheCtr ){..    
25ccb 75 2e 61 6e 2e 61 4f 66 66 73 65 74 20 3d 20 75  u.an.aOffset = u
25ccc 2e 61 6e 2e 70 43 2d 3e 61 4f 66 66 73 65 74 3b  .an.pC->aOffset;
25ccd 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
25cce 61 73 73 65 72 74 28 75 2e 61 6e 2e 61 54 79 70  assert(u.an.aTyp
25ccf 65 29 3b 0d 0a 20 20 20 20 75 2e 61 6e 2e 61 76  e);..    u.an.av
25cd0 61 69 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 75 2e  ail = 0;..    u.
25cd1 61 6e 2e 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d  an.pC->aOffset =
25cd2 20 75 2e 61 6e 2e 61 4f 66 66 73 65 74 20 3d 20   u.an.aOffset = 
25cd3 26 75 2e 61 6e 2e 61 54 79 70 65 5b 75 2e 61 6e  &u.an.aType[u.an
25cd4 2e 6e 46 69 65 6c 64 5d 3b 0d 0a 20 20 20 20 75  .nField];..    u
25cd5 2e 61 6e 2e 70 43 2d 3e 70 61 79 6c 6f 61 64 53  .an.pC->payloadS
25cd6 69 7a 65 20 3d 20 75 2e 61 6e 2e 70 61 79 6c 6f  ize = u.an.paylo
25cd7 61 64 53 69 7a 65 3b 0d 0a 20 20 20 20 75 2e 61  adSize;..    u.a
25cd8 6e 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  n.pC->cacheStatu
25cd9 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 74 72 3b  s = p->cacheCtr;
25cda 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  ....    /* Figur
25cdb 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62  e out how many b
25cdc 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65 20  ytes are in the 
25cdd 68 65 61 64 65 72 20 2a 2f 0d 0a 20 20 20 20 69  header */..    i
25cde 66 28 20 75 2e 61 6e 2e 7a 52 65 63 20 29 7b 0d  f( u.an.zRec ){.
25cdf 0a 20 20 20 20 20 20 75 2e 61 6e 2e 7a 44 61 74  .      u.an.zDat
25ce0 61 20 3d 20 75 2e 61 6e 2e 7a 52 65 63 3b 0d 0a  a = u.an.zRec;..
25ce1 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
25ce2 20 20 69 66 28 20 75 2e 61 6e 2e 70 43 2d 3e 69    if( u.an.pC->i
25ce3 73 49 6e 64 65 78 20 29 7b 0d 0a 20 20 20 20 20  sIndex ){..     
25ce4 20 20 20 75 2e 61 6e 2e 7a 44 61 74 61 20 3d 20     u.an.zData = 
25ce5 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74  (char*)sqlite3Bt
25ce6 72 65 65 4b 65 79 46 65 74 63 68 28 75 2e 61 6e  reeKeyFetch(u.an
25ce7 2e 70 43 72 73 72 2c 20 26 75 2e 61 6e 2e 61 76  .pCrsr, &u.an.av
25ce8 61 69 6c 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ail);..      }el
25ce9 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 75 2e 61  se{..        u.a
25cea 6e 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 72 2a  n.zData = (char*
25ceb 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74  )sqlite3BtreeDat
25cec 61 46 65 74 63 68 28 75 2e 61 6e 2e 70 43 72 73  aFetch(u.an.pCrs
25ced 72 2c 20 26 75 2e 61 6e 2e 61 76 61 69 6c 29 3b  r, &u.an.avail);
25cee 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
25cef 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
25cf0 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
25cf1 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
25cf2 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
25cf3 0d 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20  ..      ** save 
25cf4 74 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74  the payload in t
25cf5 68 65 20 75 2e 61 6e 2e 70 43 2d 3e 61 52 6f 77  he u.an.pC->aRow
25cf6 20 63 61 63 68 65 2e 20 20 54 68 61 74 20 77 69   cache.  That wi
25cf7 6c 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d 0d  ll save us from.
25cf8 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67  .      ** having
25cf9 20 74 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f   to make additio
25cfa 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74  nal calls to fet
25cfb 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70  ch the content p
25cfc 6f 72 74 69 6f 6e 20 6f 66 0d 0a 20 20 20 20 20  ortion of..     
25cfd 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 2e 0d   ** the record..
25cfe 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
25cff 20 61 73 73 65 72 74 28 20 75 2e 61 6e 2e 61 76   assert( u.an.av
25d00 61 69 6c 3e 3d 30 20 29 3b 0d 0a 20 20 20 20 20  ail>=0 );..     
25d01 20 69 66 28 20 75 2e 61 6e 2e 70 61 79 6c 6f 61   if( u.an.payloa
25d02 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 75 2e  dSize <= (u32)u.
25d03 61 6e 2e 61 76 61 69 6c 20 29 7b 0d 0a 20 20 20  an.avail ){..   
25d04 20 20 20 20 20 75 2e 61 6e 2e 7a 52 65 63 20 3d       u.an.zRec =
25d05 20 75 2e 61 6e 2e 7a 44 61 74 61 3b 0d 0a 20 20   u.an.zData;..  
25d06 20 20 20 20 20 20 75 2e 61 6e 2e 70 43 2d 3e 61        u.an.pC->a
25d07 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6e 2e  Row = (u8*)u.an.
25d08 7a 44 61 74 61 3b 0d 0a 20 20 20 20 20 20 7d 65  zData;..      }e
25d09 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 75 2e  lse{..        u.
25d0a 61 6e 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b  an.pC->aRow = 0;
25d0b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
25d0c 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  ..    /* The fol
25d0d 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 69 73  lowing assert is
25d0e 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73   true in all cas
25d0f 65 73 20 61 63 63 65 70 74 20 77 68 65 6e 0d 0a  es accept when..
25d10 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
25d11 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65  ase file has bee
25d12 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78 74 65  n corrupted exte
25d13 72 6e 61 6c 6c 79 2e 0d 0a 20 20 20 20 2a 2a 20  rnally...    ** 
25d14 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6e 2e     assert( u.an.
25d15 7a 52 65 63 21 3d 30 20 7c 7c 20 75 2e 61 6e 2e  zRec!=0 || u.an.
25d16 61 76 61 69 6c 3e 3d 75 2e 61 6e 2e 70 61 79 6c  avail>=u.an.payl
25d17 6f 61 64 53 69 7a 65 20 7c 7c 20 75 2e 61 6e 2e  oadSize || u.an.
25d18 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0d 0a  avail>=9 ); */..
25d19 20 20 20 20 75 2e 61 6e 2e 73 7a 48 64 72 20 3d      u.an.szHdr =
25d1a 20 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38   getVarint32((u8
25d1b 2a 29 75 2e 61 6e 2e 7a 44 61 74 61 2c 20 75 2e  *)u.an.zData, u.
25d1c 61 6e 2e 6f 66 66 73 65 74 29 3b 0d 0a 0d 0a 20  an.offset);.... 
25d1d 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
25d1e 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
25d1f 73 65 20 68 61 73 20 6e 6f 74 20 67 69 76 65 6e  se has not given
25d20 20 75 73 20 61 6e 20 6f 76 65 72 73 69 7a 65 20   us an oversize 
25d21 68 65 61 64 65 72 2e 0d 0a 20 20 20 20 2a 2a 20  header...    ** 
25d22 44 6f 20 74 68 69 73 20 6e 6f 77 20 74 6f 20 61  Do this now to a
25d23 76 6f 69 64 20 61 6e 20 6f 76 65 72 73 69 7a 65  void an oversize
25d24 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
25d25 6f 6e 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  on...    **..   
25d26 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 69 65 73   ** Type entries
25d27 20 63 61 6e 20 62 65 20 62 65 74 77 65 65 6e 20   can be between 
25d28 31 20 61 6e 64 20 35 20 62 79 74 65 73 20 65 61  1 and 5 bytes ea
25d29 63 68 2e 20 20 42 75 74 20 34 20 61 6e 64 20 35  ch.  But 4 and 5
25d2a 20 62 79 74 65 0d 0a 20 20 20 20 2a 2a 20 74 79   byte..    ** ty
25d2b 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63 68 20  pes use so much 
25d2c 64 61 74 61 20 73 70 61 63 65 20 74 68 61 74 20  data space that 
25d2d 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  there can only b
25d2e 65 20 34 30 39 36 20 61 6e 64 20 33 32 20 6f 66  e 4096 and 32 of
25d2f 0d 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 72  ..    ** them, r
25d30 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 6f  espectively.  So
25d31 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 61   the maximum hea
25d32 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75 6c  der length resul
25d33 74 73 20 66 72 6f 6d 20 61 0d 0a 20 20 20 20 2a  ts from a..    *
25d34 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
25d35 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
25d36 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
25d37 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
25d38 65 0d 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20  e..    ** extra 
25d39 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65  bytes for the he
25d3a 61 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65  ader length itse
25d3b 6c 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33  lf.  32768*3 + 3
25d3c 20 3d 20 39 38 33 30 37 2e 0d 0a 20 20 20 20 2a   = 98307...    *
25d3d 2f 0d 0a 20 20 20 20 69 66 28 20 75 2e 61 6e 2e  /..    if( u.an.
25d3e 6f 66 66 73 65 74 20 3e 20 39 38 33 30 37 20 29  offset > 98307 )
25d3f 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  {..      rc = SQ
25d40 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
25d41 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f  T;..      goto o
25d42 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0d 0a 20  p_column_out;.. 
25d43 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43     }....    /* C
25d44 6f 6d 70 75 74 65 20 69 6e 20 75 2e 61 6e 2e 6c  ompute in u.an.l
25d45 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
25d46 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 77   bytes of data w
25d47 65 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 69  e need to read i
25d48 6e 20 6f 72 64 65 72 0d 0a 20 20 20 20 2a 2a 20  n order..    ** 
25d49 74 6f 20 67 65 74 20 75 2e 61 6e 2e 6e 46 69 65  to get u.an.nFie
25d4a 6c 64 20 74 79 70 65 20 76 61 6c 75 65 73 2e 20  ld type values. 
25d4b 20 75 2e 61 6e 2e 6f 66 66 73 65 74 20 69 73 20   u.an.offset is 
25d4c 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  an upper bound o
25d4d 6e 20 74 68 69 73 2e 20 20 42 75 74 0d 0a 20 20  n this.  But..  
25d4e 20 20 2a 2a 20 75 2e 61 6e 2e 6e 46 69 65 6c 64    ** u.an.nField
25d4f 20 6d 69 67 68 74 20 62 65 20 73 69 67 6e 69 66   might be signif
25d50 69 63 61 6e 74 6c 79 20 6c 65 73 73 20 74 68 61  icantly less tha
25d51 6e 20 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65  n the true numbe
25d52 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 0d 0a 20 20  r of columns..  
25d53 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c    ** in the tabl
25d54 65 2c 20 61 6e 64 20 69 6e 20 74 68 61 74 20 63  e, and in that c
25d55 61 73 65 2c 20 35 2a 75 2e 61 6e 2e 6e 46 69 65  ase, 5*u.an.nFie
25d56 6c 64 2b 33 20 6d 69 67 68 74 20 62 65 20 73 6d  ld+3 might be sm
25d57 61 6c 6c 65 72 20 74 68 61 6e 20 75 2e 61 6e 2e  aller than u.an.
25d58 6f 66 66 73 65 74 2e 0d 0a 20 20 20 20 2a 2a 20  offset...    ** 
25d59 57 65 20 77 61 6e 74 20 74 6f 20 6d 69 6e 69 6d  We want to minim
25d5a 69 7a 65 20 75 2e 61 6e 2e 6c 65 6e 20 69 6e 20  ize u.an.len in 
25d5b 6f 72 64 65 72 20 74 6f 20 6c 69 6d 69 74 20 74  order to limit t
25d5c 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  he size of the m
25d5d 65 6d 6f 72 79 0d 0a 20 20 20 20 2a 2a 20 61 6c  emory..    ** al
25d5e 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 65 63 69  location, especi
25d5f 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 72 75 70  ally if a corrup
25d60 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  t database file 
25d61 68 61 73 20 63 61 75 73 65 64 20 75 2e 61 6e 2e  has caused u.an.
25d62 6f 66 66 73 65 74 0d 0a 20 20 20 20 2a 2a 20 74  offset..    ** t
25d63 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64 2e 20  o be oversized. 
25d64 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69 74 65  Offset is limite
25d65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f 76 65  d to 98307 above
25d66 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d 69 67  .  But 98307 mig
25d67 68 74 0d 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c  ht..    ** still
25d68 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 6d   exceed Robson m
25d69 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
25d6a 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 20   limits on some 
25d6b 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e 0d  configurations..
25d6c 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
25d6d 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
25d6e 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
25d6f 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
25d70 2c 20 75 2e 61 6e 2e 6e 46 69 65 6c 64 2a 35 2b  , u.an.nField*5+
25d71 33 0d 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c  3..    ** will l
25d72 69 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d  ikely be much sm
25d73 61 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6e  aller since u.an
25d74 2e 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b  .nField will lik
25d75 65 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e  ely be less than
25d76 0d 0a 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73  ..    ** 20 or s
25d77 6f 2e 20 20 54 68 69 73 20 69 6e 73 75 72 65 73  o.  This insures
25d78 20 74 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d   that Robson mem
25d79 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c  ory allocation l
25d7a 69 6d 69 74 73 20 61 72 65 0d 0a 20 20 20 20 2a  imits are..    *
25d7b 2a 20 6e 6f 74 20 65 78 63 65 65 64 65 64 20 65  * not exceeded e
25d7c 76 65 6e 20 66 6f 72 20 63 6f 72 72 75 70 74 20  ven for corrupt 
25d7d 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0d  database files..
25d7e 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 75 2e 61  .    */..    u.a
25d7f 6e 2e 6c 65 6e 20 3d 20 75 2e 61 6e 2e 6e 46 69  n.len = u.an.nFi
25d80 65 6c 64 2a 35 20 2b 20 33 3b 0d 0a 20 20 20 20  eld*5 + 3;..    
25d81 69 66 28 20 75 2e 61 6e 2e 6c 65 6e 20 3e 20 28  if( u.an.len > (
25d82 69 6e 74 29 75 2e 61 6e 2e 6f 66 66 73 65 74 20  int)u.an.offset 
25d83 29 20 75 2e 61 6e 2e 6c 65 6e 20 3d 20 28 69 6e  ) u.an.len = (in
25d84 74 29 75 2e 61 6e 2e 6f 66 66 73 65 74 3b 0d 0a  t)u.an.offset;..
25d85 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 4b 65 79  ..    /* The Key
25d86 46 65 74 63 68 28 29 20 6f 72 20 44 61 74 61 46  Fetch() or DataF
25d87 65 74 63 68 28 29 20 61 62 6f 76 65 20 61 72 65  etch() above are
25d88 20 66 61 73 74 20 61 6e 64 20 77 69 6c 6c 20 67   fast and will g
25d89 65 74 20 74 68 65 20 65 6e 74 69 72 65 0d 0a 20  et the entire.. 
25d8a 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61     ** record hea
25d8b 64 65 72 20 69 6e 20 6d 6f 73 74 20 63 61 73 65  der in most case
25d8c 73 2e 20 20 42 75 74 20 74 68 65 79 20 77 69 6c  s.  But they wil
25d8d 6c 20 66 61 69 6c 20 74 6f 20 67 65 74 20 74 68  l fail to get th
25d8e 65 20 63 6f 6d 70 6c 65 74 65 0d 0a 20 20 20 20  e complete..    
25d8f 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64 65 72  ** record header
25d90 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 20 68   if the record h
25d91 65 61 64 65 72 20 64 6f 65 73 20 6e 6f 74 20 66  eader does not f
25d92 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70  it on a single p
25d93 61 67 65 0d 0a 20 20 20 20 2a 2a 20 69 6e 20 74  age..    ** in t
25d94 68 65 20 42 2d 54 72 65 65 2e 20 20 57 68 65 6e  he B-Tree.  When
25d95 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 75   that happens, u
25d96 73 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  se sqlite3VdbeMe
25d97 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 74 6f 0d  mFromBtree() to.
25d98 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 72 65 20  .    ** acquire 
25d99 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 65 61  the complete hea
25d9a 64 65 72 20 74 65 78 74 2e 0d 0a 20 20 20 20 2a  der text...    *
25d9b 2f 0d 0a 20 20 20 20 69 66 28 20 21 75 2e 61 6e  /..    if( !u.an
25d9c 2e 7a 52 65 63 20 26 26 20 75 2e 61 6e 2e 61 76  .zRec && u.an.av
25d9d 61 69 6c 3c 75 2e 61 6e 2e 6c 65 6e 20 29 7b 0d  ail<u.an.len ){.
25d9e 0a 20 20 20 20 20 20 75 2e 61 6e 2e 73 4d 65 6d  .      u.an.sMem
25d9f 2e 66 6c 61 67 73 20 3d 20 30 3b 0d 0a 20 20 20  .flags = 0;..   
25da0 20 20 20 75 2e 61 6e 2e 73 4d 65 6d 2e 64 62 20     u.an.sMem.db 
25da1 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  = 0;..      rc =
25da2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
25da3 72 6f 6d 42 74 72 65 65 28 75 2e 61 6e 2e 70 43  romBtree(u.an.pC
25da4 72 73 72 2c 20 30 2c 20 75 2e 61 6e 2e 6c 65 6e  rsr, 0, u.an.len
25da5 2c 20 75 2e 61 6e 2e 70 43 2d 3e 69 73 49 6e 64  , u.an.pC->isInd
25da6 65 78 2c 20 26 75 2e 61 6e 2e 73 4d 65 6d 29 3b  ex, &u.an.sMem);
25da7 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
25da8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
25da9 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
25daa 6c 75 6d 6e 5f 6f 75 74 3b 0d 0a 20 20 20 20 20  lumn_out;..     
25dab 20 7d 0d 0a 20 20 20 20 20 20 75 2e 61 6e 2e 7a   }..      u.an.z
25dac 44 61 74 61 20 3d 20 75 2e 61 6e 2e 73 4d 65 6d  Data = u.an.sMem
25dad 2e 7a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  .z;..    }..    
25dae 75 2e 61 6e 2e 7a 45 6e 64 48 64 72 20 3d 20 28  u.an.zEndHdr = (
25daf 75 38 20 2a 29 26 75 2e 61 6e 2e 7a 44 61 74 61  u8 *)&u.an.zData
25db0 5b 75 2e 61 6e 2e 6c 65 6e 5d 3b 0d 0a 20 20 20  [u.an.len];..   
25db1 20 75 2e 61 6e 2e 7a 49 64 78 20 3d 20 28 75 38   u.an.zIdx = (u8
25db2 20 2a 29 26 75 2e 61 6e 2e 7a 44 61 74 61 5b 75   *)&u.an.zData[u
25db3 2e 61 6e 2e 73 7a 48 64 72 5d 3b 0d 0a 0d 0a 20  .an.szHdr];.... 
25db4 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68     /* Scan the h
25db5 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 74  eader and use it
25db6 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
25db7 75 2e 61 6e 2e 61 54 79 70 65 5b 5d 20 61 6e 64  u.an.aType[] and
25db8 20 75 2e 61 6e 2e 61 4f 66 66 73 65 74 5b 5d 0d   u.an.aOffset[].
25db9 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e 20  .    ** arrays. 
25dba 20 75 2e 61 6e 2e 61 54 79 70 65 5b 75 2e 61 6e   u.an.aType[u.an
25dbb 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  .i] will contain
25dbc 20 74 68 65 20 74 79 70 65 20 69 6e 74 65 67 65   the type intege
25dbd 72 20 66 6f 72 20 74 68 65 20 75 2e 61 6e 2e 69  r for the u.an.i
25dbe 2d 74 68 0d 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  -th..    ** colu
25dbf 6d 6e 20 61 6e 64 20 75 2e 61 6e 2e 61 4f 66 66  mn and u.an.aOff
25dc0 73 65 74 5b 75 2e 61 6e 2e 69 5d 20 77 69 6c 6c  set[u.an.i] will
25dc1 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 75 2e 61   contain the u.a
25dc2 6e 2e 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68  n.offset from th
25dc3 65 20 62 65 67 69 6e 6e 69 6e 67 0d 0a 20 20 20  e beginning..   
25dc4 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72   ** of the recor
25dc5 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
25dc6 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  f the data for t
25dc7 68 65 20 75 2e 61 6e 2e 69 2d 74 68 20 63 6f 6c  he u.an.i-th col
25dc8 75 6d 6e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  umn..    */..   
25dc9 20 66 6f 72 28 75 2e 61 6e 2e 69 3d 30 3b 20 75   for(u.an.i=0; u
25dca 2e 61 6e 2e 69 3c 75 2e 61 6e 2e 6e 46 69 65 6c  .an.i<u.an.nFiel
25dcb 64 3b 20 75 2e 61 6e 2e 69 2b 2b 29 7b 0d 0a 20  d; u.an.i++){.. 
25dcc 20 20 20 20 20 69 66 28 20 75 2e 61 6e 2e 7a 49       if( u.an.zI
25dcd 64 78 3c 75 2e 61 6e 2e 7a 45 6e 64 48 64 72 20  dx<u.an.zEndHdr 
25dce 29 7b 0d 0a 20 20 20 20 20 20 20 20 75 2e 61 6e  ){..        u.an
25dcf 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6e 2e 69 5d  .aOffset[u.an.i]
25dd0 20 3d 20 75 2e 61 6e 2e 6f 66 66 73 65 74 3b 0d   = u.an.offset;.
25dd1 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
25dd2 6e 2e 7a 49 64 78 5b 30 5d 3c 30 78 38 30 20 29  n.zIdx[0]<0x80 )
25dd3 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61  {..          u.a
25dd4 6e 2e 74 20 3d 20 75 2e 61 6e 2e 7a 49 64 78 5b  n.t = u.an.zIdx[
25dd5 30 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 75  0];..          u
25dd6 2e 61 6e 2e 7a 49 64 78 2b 2b 3b 0d 0a 20 20 20  .an.zIdx++;..   
25dd7 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
25dd8 20 20 20 20 20 20 20 75 2e 61 6e 2e 7a 49 64 78         u.an.zIdx
25dd9 20 2b 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61   += sqlite3GetVa
25dda 72 69 6e 74 33 32 28 75 2e 61 6e 2e 7a 49 64 78  rint32(u.an.zIdx
25ddb 2c 20 26 75 2e 61 6e 2e 74 29 3b 0d 0a 20 20 20  , &u.an.t);..   
25ddc 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
25ddd 75 2e 61 6e 2e 61 54 79 70 65 5b 75 2e 61 6e 2e  u.an.aType[u.an.
25dde 69 5d 20 3d 20 75 2e 61 6e 2e 74 3b 0d 0a 20 20  i] = u.an.t;..  
25ddf 20 20 20 20 20 20 75 2e 61 6e 2e 73 7a 46 69 65        u.an.szFie
25de0 6c 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ld = sqlite3Vdbe
25de1 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e  SerialTypeLen(u.
25de2 61 6e 2e 74 29 3b 0d 0a 20 20 20 20 20 20 20 20  an.t);..        
25de3 75 2e 61 6e 2e 6f 66 66 73 65 74 20 2b 3d 20 75  u.an.offset += u
25de4 2e 61 6e 2e 73 7a 46 69 65 6c 64 3b 0d 0a 20 20  .an.szField;..  
25de5 20 20 20 20 20 20 69 66 28 20 75 2e 61 6e 2e 6f        if( u.an.o
25de6 66 66 73 65 74 3c 75 2e 61 6e 2e 73 7a 46 69 65  ffset<u.an.szFie
25de7 6c 64 20 29 7b 20 20 2f 2a 20 54 72 75 65 20 69  ld ){  /* True i
25de8 66 20 75 2e 61 6e 2e 6f 66 66 73 65 74 20 6f 76  f u.an.offset ov
25de9 65 72 66 6c 6f 77 73 20 2a 2f 0d 0a 20 20 20 20  erflows */..    
25dea 20 20 20 20 20 20 75 2e 61 6e 2e 7a 49 64 78 20        u.an.zIdx 
25deb 3d 20 26 75 2e 61 6e 2e 7a 45 6e 64 48 64 72 5b  = &u.an.zEndHdr[
25dec 31 5d 3b 20 20 2f 2a 20 46 6f 72 63 65 73 20 53  1];  /* Forces S
25ded 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 72 65  QLITE_CORRUPT re
25dee 74 75 72 6e 20 62 65 6c 6f 77 20 2a 2f 0d 0a 20  turn below */.. 
25def 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d           break;.
25df0 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
25df1 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
25df2 20 20 2f 2a 20 49 66 20 75 2e 61 6e 2e 69 20 69    /* If u.an.i i
25df3 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e 61 6e  s less that u.an
25df4 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 74 68  .nField, then th
25df5 65 72 65 20 61 72 65 20 6c 65 73 73 20 66 69 65  ere are less fie
25df6 6c 64 73 20 69 6e 20 74 68 69 73 0d 0a 20 20 20  lds in this..   
25df7 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 74       ** record t
25df8 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e  han SetNumColumn
25df9 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 72  s indicated ther
25dfa 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e  e are columns in
25dfb 20 74 68 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a   the..        **
25dfc 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
25dfd 75 2e 61 6e 2e 6f 66 66 73 65 74 20 66 6f 72 20  u.an.offset for 
25dfe 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e  any extra column
25dff 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
25e00 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  ..        ** the
25e01 20 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68   record to 0. Th
25e02 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65  is tells code be
25e03 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e  low to store a N
25e04 55 4c 4c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ULL..        ** 
25e05 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65 72  instead of deser
25e06 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65  ializing a value
25e07 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64   from the record
25e08 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
25e09 20 20 20 20 20 20 20 75 2e 61 6e 2e 61 4f 66 66         u.an.aOff
25e0a 73 65 74 5b 75 2e 61 6e 2e 69 5d 20 3d 20 30 3b  set[u.an.i] = 0;
25e0b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
25e0c 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
25e0d 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61  eMemRelease(&u.a
25e0e 6e 2e 73 4d 65 6d 29 3b 0d 0a 20 20 20 20 75 2e  n.sMem);..    u.
25e0f 61 6e 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20  an.sMem.flags = 
25e10 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a 0d 0a 20 20 20  MEM_Null;....   
25e11 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72   /* If we have r
25e12 65 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20  ead more header 
25e13 64 61 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f  data than was co
25e14 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68  ntained in the h
25e15 65 61 64 65 72 2c 0d 0a 20 20 20 20 2a 2a 20 6f  eader,..    ** o
25e16 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20  r if the end of 
25e17 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61  the last field a
25e18 70 70 65 61 72 73 20 74 6f 20 62 65 20 70 61 73  ppears to be pas
25e19 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
25e1a 0d 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c  ..    ** record,
25e1b 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f   or if the end o
25e1c 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64  f the last field
25e1d 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 62   appears to be b
25e1e 65 66 6f 72 65 20 74 68 65 20 65 6e 64 0d 0a 20  efore the end.. 
25e1f 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63     ** of the rec
25e20 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 69  ord (when all fi
25e21 65 6c 64 73 20 70 72 65 73 65 6e 74 29 2c 20 74  elds present), t
25e22 68 65 6e 20 77 65 20 6d 75 73 74 20 62 65 20 64  hen we must be d
25e23 65 61 6c 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 77  ealing..    ** w
25e24 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61  ith a corrupt da
25e25 74 61 62 61 73 65 2e 0d 0a 20 20 20 20 2a 2f 0d  tabase...    */.
25e26 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6e 2e 7a  .    if( (u.an.z
25e27 49 64 78 20 3e 20 75 2e 61 6e 2e 7a 45 6e 64 48  Idx > u.an.zEndH
25e28 64 72 29 20 7c 7c 20 28 75 2e 61 6e 2e 6f 66 66  dr) || (u.an.off
25e29 73 65 74 20 3e 20 75 2e 61 6e 2e 70 61 79 6c 6f  set > u.an.paylo
25e2a 61 64 53 69 7a 65 29 0d 0a 20 20 20 20 20 20 20  adSize)..       
25e2b 20 20 7c 7c 20 28 75 2e 61 6e 2e 7a 49 64 78 3d    || (u.an.zIdx=
25e2c 3d 75 2e 61 6e 2e 7a 45 6e 64 48 64 72 20 26 26  =u.an.zEndHdr &&
25e2d 20 75 2e 61 6e 2e 6f 66 66 73 65 74 21 3d 75 2e   u.an.offset!=u.
25e2e 61 6e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 20  an.payloadSize) 
25e2f 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ){..      rc = S
25e30 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
25e31 50 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20  PT;..      goto 
25e32 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0d 0a  op_column_out;..
25e33 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
25e34 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75 6d  /* Get the colum
25e35 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49  n information. I
25e36 66 20 75 2e 61 6e 2e 61 4f 66 66 73 65 74 5b 75  f u.an.aOffset[u
25e37 2e 61 6e 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a  .an.p2] is non-z
25e38 65 72 6f 2c 20 74 68 65 6e 0d 0a 20 20 2a 2a 20  ero, then..  ** 
25e39 64 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20  deserialize the 
25e3a 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72  value from the r
25e3b 65 63 6f 72 64 2e 20 49 66 20 75 2e 61 6e 2e 61  ecord. If u.an.a
25e3c 4f 66 66 73 65 74 5b 75 2e 61 6e 2e 70 32 5d 20  Offset[u.an.p2] 
25e3d 69 73 20 7a 65 72 6f 2c 0d 0a 20 20 2a 2a 20 74  is zero,..  ** t
25e3e 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
25e3f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20  t enough fields 
25e40 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  in the record to
25e41 20 73 61 74 69 73 66 79 20 74 68 65 0d 0a 20 20   satisfy the..  
25e42 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e 20  ** request.  In 
25e43 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74  this case, set t
25e44 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72  he value NULL or
25e45 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 0d   to P4 if P4 is.
25e46 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  .  ** a pointer 
25e47 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e  to a Mem object.
25e48 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 75 2e  ..  */..  if( u.
25e49 61 6e 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6e 2e  an.aOffset[u.an.
25e4a 70 32 5d 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  p2] ){..    asse
25e4b 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
25e4c 4b 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 75 2e  K );..    if( u.
25e4d 61 6e 2e 7a 52 65 63 20 29 7b 0d 0a 20 20 20 20  an.zRec ){..    
25e4e 20 20 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65    VdbeMemRelease
25e4f 28 75 2e 61 6e 2e 70 44 65 73 74 29 3b 0d 0a 20  (u.an.pDest);.. 
25e50 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25e51 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29  SerialGet((u8 *)
25e52 26 75 2e 61 6e 2e 7a 52 65 63 5b 75 2e 61 6e 2e  &u.an.zRec[u.an.
25e53 61 4f 66 66 73 65 74 5b 75 2e 61 6e 2e 70 32 5d  aOffset[u.an.p2]
25e54 5d 2c 20 75 2e 61 6e 2e 61 54 79 70 65 5b 75 2e  ], u.an.aType[u.
25e55 61 6e 2e 70 32 5d 2c 20 75 2e 61 6e 2e 70 44 65  an.p2], u.an.pDe
25e56 73 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  st);..    }else{
25e57 0d 0a 20 20 20 20 20 20 75 2e 61 6e 2e 6c 65 6e  ..      u.an.len
25e58 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
25e59 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6e  rialTypeLen(u.an
25e5a 2e 61 54 79 70 65 5b 75 2e 61 6e 2e 70 32 5d 29  .aType[u.an.p2])
25e5b 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
25e5c 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61  VdbeMemMove(&u.a
25e5d 6e 2e 73 4d 65 6d 2c 20 75 2e 61 6e 2e 70 44 65  n.sMem, u.an.pDe
25e5e 73 74 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  st);..      rc =
25e5f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
25e60 72 6f 6d 42 74 72 65 65 28 75 2e 61 6e 2e 70 43  romBtree(u.an.pC
25e61 72 73 72 2c 20 75 2e 61 6e 2e 61 4f 66 66 73 65  rsr, u.an.aOffse
25e62 74 5b 75 2e 61 6e 2e 70 32 5d 2c 20 75 2e 61 6e  t[u.an.p2], u.an
25e63 2e 6c 65 6e 2c 20 75 2e 61 6e 2e 70 43 2d 3e 69  .len, u.an.pC->i
25e64 73 49 6e 64 65 78 2c 20 26 75 2e 61 6e 2e 73 4d  sIndex, &u.an.sM
25e65 65 6d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  em);..      if( 
25e66 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
25e67 0d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f  ..        goto o
25e68 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0d 0a 20  p_column_out;.. 
25e69 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 75 2e       }..      u.
25e6a 61 6e 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6e 2e  an.zData = u.an.
25e6b 73 4d 65 6d 2e 7a 3b 0d 0a 20 20 20 20 20 20 73  sMem.z;..      s
25e6c 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
25e6d 47 65 74 28 28 75 38 2a 29 75 2e 61 6e 2e 7a 44  Get((u8*)u.an.zD
25e6e 61 74 61 2c 20 75 2e 61 6e 2e 61 54 79 70 65 5b  ata, u.an.aType[
25e6f 75 2e 61 6e 2e 70 32 5d 2c 20 75 2e 61 6e 2e 70  u.an.p2], u.an.p
25e70 44 65 73 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Dest);..    }.. 
25e71 20 20 20 75 2e 61 6e 2e 70 44 65 73 74 2d 3e 65     u.an.pDest->e
25e72 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0d 0a  nc = encoding;..
25e73 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 66    }else{..    if
25e74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
25e75 34 5f 4d 45 4d 20 29 7b 0d 0a 20 20 20 20 20 20  4_MEM ){..      
25e76 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68  sqlite3VdbeMemSh
25e77 61 6c 6c 6f 77 43 6f 70 79 28 75 2e 61 6e 2e 70  allowCopy(u.an.p
25e78 44 65 73 74 2c 20 70 4f 70 2d 3e 70 34 2e 70 4d  Dest, pOp->p4.pM
25e79 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b  em, MEM_Static);
25e7a 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
25e7b 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
25e7c 61 67 28 75 2e 61 6e 2e 70 44 65 73 74 2c 20 4d  ag(u.an.pDest, M
25e7d 45 4d 5f 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 7d  EM_Null);..    }
25e7e 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ..  }....  /* If
25e7f 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   we dynamically 
25e80 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20  allocated space 
25e81 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61  to hold the data
25e82 20 28 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 73   (in the..  ** s
25e83 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
25e84 6d 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62  mBtree() call ab
25e85 6f 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66  ove) then transf
25e86 65 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68  er control of th
25e87 61 74 0d 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63  at..  ** dynamic
25e88 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
25e89 70 61 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65  pace over to the
25e8a 20 75 2e 61 6e 2e 70 44 65 73 74 20 73 74 72 75   u.an.pDest stru
25e8b 63 74 75 72 65 2e 0d 0a 20 20 2a 2a 20 54 68 69  cture...  ** Thi
25e8c 73 20 70 72 65 76 65 6e 74 73 20 61 20 6d 65 6d  s prevents a mem
25e8d 6f 72 79 20 63 6f 70 79 2e 0d 0a 20 20 2a 2f 0d  ory copy...  */.
25e8e 0a 20 20 69 66 28 20 75 2e 61 6e 2e 73 4d 65 6d  .  if( u.an.sMem
25e8f 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20  .zMalloc ){..   
25e90 20 61 73 73 65 72 74 28 20 75 2e 61 6e 2e 73 4d   assert( u.an.sM
25e91 65 6d 2e 7a 3d 3d 75 2e 61 6e 2e 73 4d 65 6d 2e  em.z==u.an.sMem.
25e92 7a 4d 61 6c 6c 6f 63 20 29 3b 0d 0a 20 20 20 20  zMalloc );..    
25e93 61 73 73 65 72 74 28 20 21 28 75 2e 61 6e 2e 70  assert( !(u.an.p
25e94 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45  Dest->flags & ME
25e95 4d 5f 44 79 6e 29 20 29 3b 0d 0a 20 20 20 20 61  M_Dyn) );..    a
25e96 73 73 65 72 74 28 20 21 28 75 2e 61 6e 2e 70 44  ssert( !(u.an.pD
25e97 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  est->flags & (ME
25e98 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29  M_Blob|MEM_Str))
25e99 20 7c 7c 20 75 2e 61 6e 2e 70 44 65 73 74 2d 3e   || u.an.pDest->
25e9a 7a 3d 3d 75 2e 61 6e 2e 73 4d 65 6d 2e 7a 20 29  z==u.an.sMem.z )
25e9b 3b 0d 0a 20 20 20 20 75 2e 61 6e 2e 70 44 65 73  ;..    u.an.pDes
25e9c 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45  t->flags &= ~(ME
25e9d 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74  M_Ephem|MEM_Stat
25e9e 69 63 29 3b 0d 0a 20 20 20 20 75 2e 61 6e 2e 70  ic);..    u.an.p
25e9f 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  Dest->flags |= M
25ea0 45 4d 5f 54 65 72 6d 3b 0d 0a 20 20 20 20 75 2e  EM_Term;..    u.
25ea1 61 6e 2e 70 44 65 73 74 2d 3e 7a 20 3d 20 75 2e  an.pDest->z = u.
25ea2 61 6e 2e 73 4d 65 6d 2e 7a 3b 0d 0a 20 20 20 20  an.sMem.z;..    
25ea3 75 2e 61 6e 2e 70 44 65 73 74 2d 3e 7a 4d 61 6c  u.an.pDest->zMal
25ea4 6c 6f 63 20 3d 20 75 2e 61 6e 2e 73 4d 65 6d 2e  loc = u.an.sMem.
25ea5 7a 4d 61 6c 6c 6f 63 3b 0d 0a 20 20 7d 0d 0a 0d  zMalloc;..  }...
25ea6 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
25ea7 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
25ea8 62 6c 65 28 75 2e 61 6e 2e 70 44 65 73 74 29 3b  ble(u.an.pDest);
25ea9 0d 0a 0d 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75  ....op_column_ou
25eaa 74 3a 0d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  t:..  UPDATE_MAX
25eab 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 61 6e 2e 70  _BLOBSIZE(u.an.p
25eac 44 65 73 74 29 3b 0d 0a 20 20 52 45 47 49 53 54  Dest);..  REGIST
25ead 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
25eae 2c 20 75 2e 61 6e 2e 70 44 65 73 74 29 3b 0d 0a  , u.an.pDest);..
25eaf 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
25eb0 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69  * Opcode: Affini
25eb1 74 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0d  ty P1 P2 * P4 *.
25eb2 0a 2a 2a 0d 0a 2a 2a 20 41 70 70 6c 79 20 61 66  .**..** Apply af
25eb3 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72 61  finities to a ra
25eb4 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 73 74  nge of P2 regist
25eb5 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74  ers starting wit
25eb6 68 20 50 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34  h P1...**..** P4
25eb7 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61   is a string tha
25eb8 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65  t is P2 characte
25eb9 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68  rs long. The nth
25eba 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
25ebb 65 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64  e..** string ind
25ebc 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d  icates the colum
25ebd 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  n affinity that 
25ebe 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66  should be used f
25ebf 6f 72 20 74 68 65 20 6e 74 68 0d 0a 2a 2a 20 6d  or the nth..** m
25ec0 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68  emory cell in th
25ec1 65 20 72 61 6e 67 65 2e 0d 0a 2a 2f 0d 0a 63 61  e range...*/..ca
25ec2 73 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20  se OP_Affinity: 
25ec3 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  {..#if 0  /* loc
25ec4 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
25ec5 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0d  ed into u.ao */.
25ec6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25ec7 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54  Affinity;   /* T
25ec8 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62  he affinity to b
25ec9 65 20 61 70 70 6c 69 65 64 20 2a 2f 0d 0a 20 20  e applied */..  
25eca 63 68 61 72 20 63 41 66 66 3b 20 20 20 20 20 20  char cAff;      
25ecb 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
25ecc 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 20 6f  ngle character o
25ecd 66 20 61 66 66 69 6e 69 74 79 20 2a 2f 0d 0a 23  f affinity */..#
25ece 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
25ecf 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
25ed0 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0d 0a 0d 0a 20  nto u.ao */.... 
25ed1 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20   u.ao.zAffinity 
25ed2 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0d 0a 20 20  = pOp->p4.z;..  
25ed3 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 7a 41 66  assert( u.ao.zAf
25ed4 66 69 6e 69 74 79 21 3d 30 20 29 3b 0d 0a 20 20  finity!=0 );..  
25ed5 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 7a 41 66  assert( u.ao.zAf
25ed6 66 69 6e 69 74 79 5b 70 4f 70 2d 3e 70 32 5d 3d  finity[pOp->p2]=
25ed7 3d 30 20 29 3b 0d 0a 20 20 70 49 6e 31 20 3d 20  =0 );..  pIn1 = 
25ed8 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  &aMem[pOp->p1];.
25ed9 0a 20 20 77 68 69 6c 65 28 20 28 75 2e 61 6f 2e  .  while( (u.ao.
25eda 63 41 66 66 20 3d 20 2a 28 75 2e 61 6f 2e 7a 41  cAff = *(u.ao.zA
25edb 66 66 69 6e 69 74 79 2b 2b 29 29 21 3d 30 20 29  ffinity++))!=0 )
25edc 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
25edd 49 6e 31 20 3c 3d 20 26 70 2d 3e 61 4d 65 6d 5b  In1 <= &p->aMem[
25ede 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0d 0a 20 20 20  p->nMem] );..   
25edf 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61   assert( memIsVa
25ee0 6c 69 64 28 70 49 6e 31 29 20 29 3b 0d 0a 20 20  lid(pIn1) );..  
25ee1 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e    ExpandBlob(pIn
25ee2 31 29 3b 0d 0a 20 20 20 20 61 70 70 6c 79 41 66  1);..    applyAf
25ee3 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61  finity(pIn1, u.a
25ee4 6f 2e 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e 67  o.cAff, encoding
25ee5 29 3b 0d 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0d  );..    pIn1++;.
25ee6 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  .  }..  break;..
25ee7 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
25ee8 4d 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32  MakeRecord P1 P2
25ee9 20 50 33 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a   P3 P4 *..**..**
25eea 20 43 6f 6e 76 65 72 74 20 50 32 20 72 65 67 69   Convert P2 regi
25eeb 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
25eec 77 69 74 68 20 50 31 20 69 6e 74 6f 20 74 68 65  with P1 into the
25eed 20 5b 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 5d   [record format]
25eee 0d 0a 2a 2a 20 75 73 65 20 61 73 20 61 20 64 61  ..** use as a da
25eef 74 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64  ta record in a d
25ef0 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
25ef1 20 61 73 20 61 20 6b 65 79 0d 0a 2a 2a 20 69 6e   as a key..** in
25ef2 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20   an index.  The 
25ef3 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
25ef4 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20   can decode the 
25ef5 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0d 0a 2a  record later...*
25ef6 2a 0d 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20  *..** P4 may be 
25ef7 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73  a string that is
25ef8 20 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c   P2 characters l
25ef9 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68  ong.  The nth ch
25efa 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 0d 0a  aracter of the..
25efb 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 61  ** string indica
25efc 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  tes the column a
25efd 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
25efe 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
25eff 74 68 65 20 6e 74 68 0d 0a 2a 2a 20 66 69 65 6c  the nth..** fiel
25f00 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
25f01 65 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ey...**..** The 
25f02 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61  mapping from cha
25f03 72 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69  racter to affini
25f04 74 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74  ty is given by t
25f05 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0d 0a  he SQLITE_AFF_..
25f06 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  ** macros define
25f07 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
25f08 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 50 34 20  ...**..** If P4 
25f09 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c  is NULL then all
25f0a 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 61   index fields ha
25f0b 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ve the affinity 
25f0c 4e 4f 4e 45 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  NONE...*/..case 
25f0d 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b  OP_MakeRecord: {
25f0e 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
25f0f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25f10 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0d 0a  d into u.ap */..
25f11 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64    u8 *zNewRecord
25f12 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75  ;        /* A bu
25f13 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
25f14 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65   data for the ne
25f15 77 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 4d  w record */..  M
25f16 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20  em *pRec;       
25f17 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
25f18 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 75 36   record */..  u6
25f19 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  4 nData;        
25f1a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
25f1b 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
25f1c 73 70 61 63 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  space */..  int 
25f1d 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  nHdr;           
25f1e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
25f1f 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20  bytes of header 
25f20 73 70 61 63 65 20 2a 2f 0d 0a 20 20 69 36 34 20  space */..  i64 
25f21 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20  nByte;          
25f22 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65     /* Data space
25f23 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68   required for th
25f24 69 73 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20  is record */..  
25f25 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20  int nZero;      
25f26 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
25f27 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61   of zero bytes a
25f28 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
25f29 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 69 6e   record */..  in
25f2a 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 20 20  t nVarint;      
25f2b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
25f2c 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 61 72  f bytes in a var
25f2d 69 6e 74 20 2a 2f 0d 0a 20 20 75 33 32 20 73 65  int */..  u32 se
25f2e 72 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20  rial_type;      
25f2f 20 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a   /* Type field *
25f30 2f 0d 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 30  /..  Mem *pData0
25f31 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
25f32 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65  irst field to be
25f33 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74   combined into t
25f34 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20  he record */..  
25f35 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20  Mem *pLast;     
25f36 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66         /* Last f
25f37 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f  ield of the reco
25f38 72 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46 69  rd */..  int nFi
25f39 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  eld;            
25f3a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
25f3b 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  lds in the recor
25f3c 64 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 41  d */..  char *zA
25f3d 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 2f  ffinity;       /
25f3e 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 73  * The affinity s
25f3f 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72 65  tring for the re
25f40 63 6f 72 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 66  cord */..  int f
25f41 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20  ile_format;     
25f42 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74    /* File format
25f43 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f   to use for enco
25f44 64 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ding */..  int i
25f45 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25f46 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20    /* Space used 
25f47 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20  in zNewRecord[] 
25f48 2a 2f 0d 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20  */..  int len;  
25f49 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25f4a 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c  Length of a fiel
25f4b 64 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  d */..#endif /* 
25f4c 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
25f4d 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20  moved into u.ap 
25f4e 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 41 73 73 75 6d  */....  /* Assum
25f4f 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 63  ing the record c
25f50 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73  ontains N fields
25f51 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72  , the record for
25f52 6d 61 74 20 6c 6f 6f 6b 73 0d 0a 20 20 2a 2a 20  mat looks..  ** 
25f53 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 20 20 2a 2a  like this:..  **
25f54 0d 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..  ** ---------
25f55 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f56 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f58 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d  ---------------.
25f59 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65  .  ** | hdr-size
25f5a 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 70 65   | type 0 | type
25f5b 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20   1 | ... | type 
25f5c 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e  N-1 | data0 | ..
25f5d 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c 0d 0a  . | data N-1 |..
25f5e 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    ** -----------
25f5f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f61 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25f62 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 20  -------------.. 
25f63 20 2a 2a 0d 0a 20 20 2a 2a 20 44 61 74 61 28 30   **..  ** Data(0
25f64 29 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  ) is taken from 
25f65 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61  register P1.  Da
25f66 74 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d  ta(1) comes from
25f67 20 72 65 67 69 73 74 65 72 20 50 31 2b 31 0d 0a   register P1+1..
25f68 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74    ** and so frot
25f69 68 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 45  h...  **..  ** E
25f6a 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20 69  ach type field i
25f6b 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72 65  s a varint repre
25f6c 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72 69  senting the seri
25f6d 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 0d 0a  al type of the..
25f6e 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69    ** correspondi
25f6f 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20  ng data element 
25f70 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 65  (see sqlite3Vdbe
25f71 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 54  SerialType()). T
25f72 68 65 0d 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a  he..  ** hdr-siz
25f73 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20  e field is also 
25f74 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69  a varint which i
25f75 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f  s the offset fro
25f76 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0d  m the beginning.
25f77 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63  .  ** of the rec
25f78 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0d 0a 20  ord to data0... 
25f79 20 2a 2f 0d 0a 20 20 75 2e 61 70 2e 6e 44 61 74   */..  u.ap.nDat
25f7a 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  a = 0;         /
25f7b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
25f7c 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20  s of data space 
25f7d 2a 2f 0d 0a 20 20 75 2e 61 70 2e 6e 48 64 72 20  */..  u.ap.nHdr 
25f7e 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
25f7f 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
25f80 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65   of header space
25f81 20 2a 2f 0d 0a 20 20 75 2e 61 70 2e 6e 5a 65 72   */..  u.ap.nZer
25f82 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  o = 0;         /
25f83 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  * Number of zero
25f84 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e   bytes at the en
25f85 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  d of the record 
25f86 2a 2f 0d 0a 20 20 75 2e 61 70 2e 6e 46 69 65 6c  */..  u.ap.nFiel
25f87 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a 20 20  d = pOp->p1;..  
25f88 75 2e 61 70 2e 7a 41 66 66 69 6e 69 74 79 20 3d  u.ap.zAffinity =
25f89 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0d 0a 20 20 61   pOp->p4.z;..  a
25f8a 73 73 65 72 74 28 20 75 2e 61 70 2e 6e 46 69 65  ssert( u.ap.nFie
25f8b 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e  ld>0 && pOp->p2>
25f8c 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61  0 && pOp->p2+u.a
25f8d 70 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65  p.nField<=p->nMe
25f8e 6d 2b 31 20 29 3b 0d 0a 20 20 75 2e 61 70 2e 70  m+1 );..  u.ap.p
25f8f 44 61 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75 2e  Data0 = &aMem[u.
25f90 61 70 2e 6e 46 69 65 6c 64 5d 3b 0d 0a 20 20 75  ap.nField];..  u
25f91 2e 61 70 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  .ap.nField = pOp
25f92 2d 3e 70 32 3b 0d 0a 20 20 75 2e 61 70 2e 70 4c  ->p2;..  u.ap.pL
25f93 61 73 74 20 3d 20 26 75 2e 61 70 2e 70 44 61 74  ast = &u.ap.pDat
25f94 61 30 5b 75 2e 61 70 2e 6e 46 69 65 6c 64 2d 31  a0[u.ap.nField-1
25f95 5d 3b 0d 0a 20 20 75 2e 61 70 2e 66 69 6c 65 5f  ];..  u.ap.file_
25f96 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57  format = p->minW
25f97 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0d  riteFileFormat;.
25f98 0a 0d 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79  ...  /* Identify
25f99 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69   the output regi
25f9a 73 74 65 72 20 2a 2f 0d 0a 20 20 61 73 73 65 72  ster */..  asser
25f9b 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e  t( pOp->p3<pOp->
25f9c 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70  p1 || pOp->p3>=p
25f9d 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29  Op->p1+pOp->p2 )
25f9e 3b 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ;..  pOut = &aMe
25f9f 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 6d  m[pOp->p3];..  m
25fa0 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28  emAboutToChange(
25fa1 70 2c 20 70 4f 75 74 29 3b 0d 0a 0d 0a 20 20 2f  p, pOut);....  /
25fa2 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
25fa3 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74  he elements that
25fa4 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68   will make up th
25fa5 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75  e record to figu
25fa6 72 65 0d 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77  re..  ** out how
25fa7 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72   much space is r
25fa8 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20  equired for the 
25fa9 6e 65 77 20 72 65 63 6f 72 64 2e 0d 0a 20 20 2a  new record...  *
25faa 2f 0d 0a 20 20 66 6f 72 28 75 2e 61 70 2e 70 52  /..  for(u.ap.pR
25fab 65 63 3d 75 2e 61 70 2e 70 44 61 74 61 30 3b 20  ec=u.ap.pData0; 
25fac 75 2e 61 70 2e 70 52 65 63 3c 3d 75 2e 61 70 2e  u.ap.pRec<=u.ap.
25fad 70 4c 61 73 74 3b 20 75 2e 61 70 2e 70 52 65 63  pLast; u.ap.pRec
25fae 2b 2b 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  ++){..    assert
25faf 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75 2e 61  ( memIsValid(u.a
25fb0 70 2e 70 52 65 63 29 20 29 3b 0d 0a 20 20 20 20  p.pRec) );..    
25fb1 69 66 28 20 75 2e 61 70 2e 7a 41 66 66 69 6e 69  if( u.ap.zAffini
25fb2 74 79 20 29 7b 0d 0a 20 20 20 20 20 20 61 70 70  ty ){..      app
25fb3 6c 79 41 66 66 69 6e 69 74 79 28 75 2e 61 70 2e  lyAffinity(u.ap.
25fb4 70 52 65 63 2c 20 75 2e 61 70 2e 7a 41 66 66 69  pRec, u.ap.zAffi
25fb5 6e 69 74 79 5b 75 2e 61 70 2e 70 52 65 63 2d 75  nity[u.ap.pRec-u
25fb6 2e 61 70 2e 70 44 61 74 61 30 5d 2c 20 65 6e 63  .ap.pData0], enc
25fb7 6f 64 69 6e 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a  oding);..    }..
25fb8 20 20 20 20 69 66 28 20 75 2e 61 70 2e 70 52 65      if( u.ap.pRe
25fb9 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72  c->flags&MEM_Zer
25fba 6f 20 26 26 20 75 2e 61 70 2e 70 52 65 63 2d 3e  o && u.ap.pRec->
25fbb 6e 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  n>0 ){..      sq
25fbc 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
25fbd 6e 64 42 6c 6f 62 28 75 2e 61 70 2e 70 52 65 63  ndBlob(u.ap.pRec
25fbe 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 75  );..    }..    u
25fbf 2e 61 70 2e 73 65 72 69 61 6c 5f 74 79 70 65 20  .ap.serial_type 
25fc0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
25fc1 69 61 6c 54 79 70 65 28 75 2e 61 70 2e 70 52 65  ialType(u.ap.pRe
25fc2 63 2c 20 75 2e 61 70 2e 66 69 6c 65 5f 66 6f 72  c, u.ap.file_for
25fc3 6d 61 74 29 3b 0d 0a 20 20 20 20 75 2e 61 70 2e  mat);..    u.ap.
25fc4 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
25fc5 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
25fc6 2e 61 70 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .ap.serial_type)
25fc7 3b 0d 0a 20 20 20 20 75 2e 61 70 2e 6e 44 61 74  ;..    u.ap.nDat
25fc8 61 20 2b 3d 20 75 2e 61 70 2e 6c 65 6e 3b 0d 0a  a += u.ap.len;..
25fc9 20 20 20 20 75 2e 61 70 2e 6e 48 64 72 20 2b 3d      u.ap.nHdr +=
25fca 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
25fcb 6e 28 75 2e 61 70 2e 73 65 72 69 61 6c 5f 74 79  n(u.ap.serial_ty
25fcc 70 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 75 2e  pe);..    if( u.
25fcd 61 70 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  ap.pRec->flags &
25fce 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0d 0a 20 20   MEM_Zero ){..  
25fcf 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65      /* Only pure
25fd0 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f   zero-filled BLO
25fd1 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20  Bs can be input 
25fd2 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0d  to this Opcode..
25fd3 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20  .      ** We do 
25fd4 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20  not allow blobs 
25fd5 77 69 74 68 20 61 20 70 72 65 66 69 78 20 61 6e  with a prefix an
25fd6 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  d a zero-filled 
25fd7 74 61 69 6c 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  tail. */..      
25fd8 75 2e 61 70 2e 6e 5a 65 72 6f 20 2b 3d 20 75 2e  u.ap.nZero += u.
25fd9 61 70 2e 70 52 65 63 2d 3e 75 2e 6e 5a 65 72 6f  ap.pRec->u.nZero
25fda 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ;..    }else if(
25fdb 20 75 2e 61 70 2e 6c 65 6e 20 29 7b 0d 0a 20 20   u.ap.len ){..  
25fdc 20 20 20 20 75 2e 61 70 2e 6e 5a 65 72 6f 20 3d      u.ap.nZero =
25fdd 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   0;..    }..  }.
25fde 0a 0d 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  ...  /* Add the 
25fdf 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 20 76  initial header v
25fe0 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20  arint and total 
25fe1 74 68 65 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 75  the size */..  u
25fe2 2e 61 70 2e 6e 48 64 72 20 2b 3d 20 75 2e 61 70  .ap.nHdr += u.ap
25fe3 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 74  .nVarint = sqlit
25fe4 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61 70  e3VarintLen(u.ap
25fe5 2e 6e 48 64 72 29 3b 0d 0a 20 20 69 66 28 20 75  .nHdr);..  if( u
25fe6 2e 61 70 2e 6e 56 61 72 69 6e 74 3c 73 71 6c 69  .ap.nVarint<sqli
25fe7 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61  te3VarintLen(u.a
25fe8 70 2e 6e 48 64 72 29 20 29 7b 0d 0a 20 20 20 20  p.nHdr) ){..    
25fe9 75 2e 61 70 2e 6e 48 64 72 2b 2b 3b 0d 0a 20 20  u.ap.nHdr++;..  
25fea 7d 0d 0a 20 20 75 2e 61 70 2e 6e 42 79 74 65 20  }..  u.ap.nByte 
25feb 3d 20 75 2e 61 70 2e 6e 48 64 72 2b 75 2e 61 70  = u.ap.nHdr+u.ap
25fec 2e 6e 44 61 74 61 2d 75 2e 61 70 2e 6e 5a 65 72  .nData-u.ap.nZer
25fed 6f 3b 0d 0a 20 20 69 66 28 20 75 2e 61 70 2e 6e  o;..  if( u.ap.n
25fee 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  Byte>db->aLimit[
25fef 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
25ff0 47 54 48 5d 20 29 7b 0d 0a 20 20 20 20 67 6f 74  GTH] ){..    got
25ff1 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a 20 20 7d 0d  o too_big;..  }.
25ff2 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ...  /* Make sur
25ff3 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67  e the output reg
25ff4 69 73 74 65 72 20 68 61 73 20 61 20 62 75 66 66  ister has a buff
25ff5 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20  er large enough 
25ff6 74 6f 20 73 74 6f 72 65 0d 0a 20 20 2a 2a 20 74  to store..  ** t
25ff7 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 54  he new record. T
25ff8 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74  he output regist
25ff9 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 20  er (pOp->p3) is 
25ffa 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0d 0a  not allowed to..
25ffb 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74    ** be one of t
25ffc 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65  he input registe
25ffd 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20  rs (because the 
25ffe 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74  following call t
25fff 6f 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56  o..  ** sqlite3V
26000 64 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75  dbeMemGrow() cou
26001 6c 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76  ld clobber the v
26002 61 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69  alue before it i
26003 73 20 75 73 65 64 29 2e 0d 0a 20 20 2a 2f 0d 0a  s used)...  */..
26004 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
26005 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28  eMemGrow(pOut, (
26006 69 6e 74 29 75 2e 61 70 2e 6e 42 79 74 65 2c 20  int)u.ap.nByte, 
26007 30 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20  0) ){..    goto 
26008 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20 20  no_mem;..  }..  
26009 75 2e 61 70 2e 7a 4e 65 77 52 65 63 6f 72 64 20  u.ap.zNewRecord 
2600a 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b  = (u8 *)pOut->z;
2600b 0d 0a 0d 0a 20 20 2f 2a 20 57 72 69 74 65 20 74  ....  /* Write t
2600c 68 65 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20 20  he record */..  
2600d 75 2e 61 70 2e 69 20 3d 20 70 75 74 56 61 72 69  u.ap.i = putVari
2600e 6e 74 33 32 28 75 2e 61 70 2e 7a 4e 65 77 52 65  nt32(u.ap.zNewRe
2600f 63 6f 72 64 2c 20 75 2e 61 70 2e 6e 48 64 72 29  cord, u.ap.nHdr)
26010 3b 0d 0a 20 20 66 6f 72 28 75 2e 61 70 2e 70 52  ;..  for(u.ap.pR
26011 65 63 3d 75 2e 61 70 2e 70 44 61 74 61 30 3b 20  ec=u.ap.pData0; 
26012 75 2e 61 70 2e 70 52 65 63 3c 3d 75 2e 61 70 2e  u.ap.pRec<=u.ap.
26013 70 4c 61 73 74 3b 20 75 2e 61 70 2e 70 52 65 63  pLast; u.ap.pRec
26014 2b 2b 29 7b 0d 0a 20 20 20 20 75 2e 61 70 2e 73  ++){..    u.ap.s
26015 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c  erial_type = sql
26016 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
26017 70 65 28 75 2e 61 70 2e 70 52 65 63 2c 20 75 2e  pe(u.ap.pRec, u.
26018 61 70 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  ap.file_format);
26019 0d 0a 20 20 20 20 75 2e 61 70 2e 69 20 2b 3d 20  ..    u.ap.i += 
2601a 70 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e 61  putVarint32(&u.a
2601b 70 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61  p.zNewRecord[u.a
2601c 70 2e 69 5d 2c 20 75 2e 61 70 2e 73 65 72 69 61  p.i], u.ap.seria
2601d 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a  l_type);      /*
2601e 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0d   serial type */.
2601f 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 75 2e 61 70  .  }..  for(u.ap
26020 2e 70 52 65 63 3d 75 2e 61 70 2e 70 44 61 74 61  .pRec=u.ap.pData
26021 30 3b 20 75 2e 61 70 2e 70 52 65 63 3c 3d 75 2e  0; u.ap.pRec<=u.
26022 61 70 2e 70 4c 61 73 74 3b 20 75 2e 61 70 2e 70  ap.pLast; u.ap.p
26023 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69  Rec++){  /* seri
26024 61 6c 20 64 61 74 61 20 2a 2f 0d 0a 20 20 20 20  al data */..    
26025 75 2e 61 70 2e 69 20 2b 3d 20 73 71 6c 69 74 65  u.ap.i += sqlite
26026 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 26  3VdbeSerialPut(&
26027 75 2e 61 70 2e 7a 4e 65 77 52 65 63 6f 72 64 5b  u.ap.zNewRecord[
26028 75 2e 61 70 2e 69 5d 2c 20 28 69 6e 74 29 28 75  u.ap.i], (int)(u
26029 2e 61 70 2e 6e 42 79 74 65 2d 75 2e 61 70 2e 69  .ap.nByte-u.ap.i
2602a 29 2c 20 75 2e 61 70 2e 70 52 65 63 2c 75 2e 61  ), u.ap.pRec,u.a
2602b 70 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0d  p.file_format);.
2602c 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2602d 75 2e 61 70 2e 69 3d 3d 75 2e 61 70 2e 6e 42 79  u.ap.i==u.ap.nBy
2602e 74 65 20 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  te );....  asser
2602f 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
26030 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
26031 20 29 3b 0d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d   );..  pOut->n =
26032 20 28 69 6e 74 29 75 2e 61 70 2e 6e 42 79 74 65   (int)u.ap.nByte
26033 3b 0d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  ;..  pOut->flags
26034 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
26035 4d 5f 44 79 6e 3b 0d 0a 20 20 70 4f 75 74 2d 3e  M_Dyn;..  pOut->
26036 78 44 65 6c 20 3d 20 30 3b 0d 0a 20 20 69 66 28  xDel = 0;..  if(
26037 20 75 2e 61 70 2e 6e 5a 65 72 6f 20 29 7b 0d 0a   u.ap.nZero ){..
26038 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72      pOut->u.nZer
26039 6f 20 3d 20 75 2e 61 70 2e 6e 5a 65 72 6f 3b 0d  o = u.ap.nZero;.
2603a 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
2603b 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0d 0a 20   |= MEM_Zero;.. 
2603c 20 7d 0d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20   }..  pOut->enc 
2603d 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20  = SQLITE_UTF8;  
2603e 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62  /* In case the b
2603f 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76  lob is ever conv
26040 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f  erted to text */
26041 0d 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ..  REGISTER_TRA
26042 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74  CE(pOp->p3, pOut
26043 29 3b 0d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  );..  UPDATE_MAX
26044 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
26045 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d  ..  break;..}...
26046 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 6e  ./* Opcode: Coun
26047 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a  t P1 P2 * * *..*
26048 2a 0d 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  *..** Store the 
26049 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
2604a 73 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61  s (an integer va
2604b 6c 75 65 29 20 69 6e 20 74 68 65 20 74 61 62 6c  lue) in the tabl
2604c 65 20 6f 72 20 69 6e 64 65 78 20 0d 0a 2a 2a 20  e or index ..** 
2604d 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72  opened by cursor
2604e 20 50 31 20 69 6e 20 72 65 67 69 73 74 65 72 20   P1 in register 
2604f 50 32 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20  P2..*/..#ifndef 
26050 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45  SQLITE_OMIT_BTRE
26051 45 43 4f 55 4e 54 0d 0a 63 61 73 65 20 4f 50 5f  ECOUNT..case OP_
26052 43 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20  Count: {        
26053 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
26054 61 73 65 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f  ase */..#if 0  /
26055 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
26056 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
26057 71 20 2a 2f 0d 0a 20 20 69 36 34 20 6e 45 6e 74  q */..  i64 nEnt
26058 72 79 3b 0d 0a 20 20 42 74 43 75 72 73 6f 72 20  ry;..  BtCursor 
26059 2a 70 43 72 73 72 3b 0d 0a 23 65 6e 64 69 66 20  *pCrsr;..#endif 
2605a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
2605b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
2605c 61 71 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 61 71 2e  aq */....  u.aq.
2605d 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72  pCrsr = p->apCsr
2605e 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73  [pOp->p1]->pCurs
2605f 6f 72 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59  or;..  if( ALWAY
26060 53 28 75 2e 61 71 2e 70 43 72 73 72 29 20 29 7b  S(u.aq.pCrsr) ){
26061 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
26062 65 33 42 74 72 65 65 43 6f 75 6e 74 28 75 2e 61  e3BtreeCount(u.a
26063 71 2e 70 43 72 73 72 2c 20 26 75 2e 61 71 2e 6e  q.pCrsr, &u.aq.n
26064 45 6e 74 72 79 29 3b 0d 0a 20 20 7d 65 6c 73 65  Entry);..  }else
26065 7b 0d 0a 20 20 20 20 75 2e 61 71 2e 6e 45 6e 74  {..    u.aq.nEnt
26066 72 79 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  ry = 0;..  }..  
26067 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 71  pOut->u.i = u.aq
26068 2e 6e 45 6e 74 72 79 3b 0d 0a 20 20 62 72 65 61  .nEntry;..  brea
26069 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  k;..}..#endif...
2606a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65  ./* Opcode: Save
2606b 70 6f 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20  point P1 * * P4 
2606c 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 70 65 6e 2c 20  *..**..** Open, 
2606d 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62  release or rollb
2606e 61 63 6b 20 74 68 65 20 73 61 76 65 70 6f 69 6e  ack the savepoin
2606f 74 20 6e 61 6d 65 64 20 62 79 20 70 61 72 61 6d  t named by param
26070 65 74 65 72 20 50 34 2c 20 64 65 70 65 6e 64 69  eter P4, dependi
26071 6e 67 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 76 61  ng..** on the va
26072 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f 20 6f 70  lue of P1. To op
26073 65 6e 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69  en a new savepoi
26074 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f 20 72 65  nt, P1==0. To re
26075 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
26076 6e 0d 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 73  n..** existing s
26077 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 31 2c  avepoint, P1==1,
26078 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20   or to rollback 
26079 61 6e 20 65 78 69 73 74 69 6e 67 20 73 61 76 65  an existing save
2607a 70 6f 69 6e 74 20 50 31 3d 3d 32 2e 0d 0a 2a 2f  point P1==2...*/
2607b 0d 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 70 6f  ..case OP_Savepo
2607c 69 6e 74 3a 20 7b 0d 0a 23 69 66 20 30 20 20 2f  int: {..#if 0  /
2607d 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
2607e 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
2607f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 31 3b 20  r */..  int p1; 
26080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26081 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
26082 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a   of P1 operand *
26083 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  /..  char *zName
26084 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26085 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
26086 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0d 0a 20 20  savepoint */..  
26087 69 6e 74 20 6e 4e 61 6d 65 3b 0d 0a 20 20 53 61  int nName;..  Sa
26088 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0d 0a  vepoint *pNew;..
26089 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61    Savepoint *pSa
2608a 76 65 70 6f 69 6e 74 3b 0d 0a 20 20 53 61 76 65  vepoint;..  Save
2608b 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0d 0a 20 20  point *pTmp;..  
2608c 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0d  int iSavepoint;.
2608d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 23 65 6e 64  .  int ii;..#end
2608e 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
2608f 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
26090 20 75 2e 61 72 20 2a 2f 0d 0a 0d 0a 20 20 75 2e   u.ar */....  u.
26091 61 72 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b  ar.p1 = pOp->p1;
26092 0d 0a 20 20 75 2e 61 72 2e 7a 4e 61 6d 65 20 3d  ..  u.ar.zName =
26093 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0d 0a 0d 0a 20   pOp->p4.z;.... 
26094 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
26095 74 68 65 20 75 2e 61 72 2e 70 31 20 70 61 72 61  the u.ar.p1 para
26096 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e 20  meter is valid. 
26097 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  Also that if the
26098 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0d 0a 20  re is no open.. 
26099 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c   ** transaction,
2609a 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e   then there cann
2609b 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f  ot be any savepo
2609c 69 6e 74 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61  ints...  */..  a
2609d 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65  ssert( db->pSave
2609e 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e  point==0 || db->
2609f 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b  autoCommit==0 );
260a0 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 72  ..  assert( u.ar
260a1 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42  .p1==SAVEPOINT_B
260a2 45 47 49 4e 7c 7c 75 2e 61 72 2e 70 31 3d 3d 53  EGIN||u.ar.p1==S
260a3 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
260a4 7c 7c 75 2e 61 72 2e 70 31 3d 3d 53 41 56 45 50  ||u.ar.p1==SAVEP
260a5 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
260a6 0d 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ..  assert( db->
260a7 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62  pSavepoint || db
260a8 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53  ->isTransactionS
260a9 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0d 0a  avepoint==0 );..
260aa 20 20 61 73 73 65 72 74 28 20 63 68 65 63 6b 53    assert( checkS
260ab 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62  avepointCount(db
260ac 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 75 2e  ) );....  if( u.
260ad 61 72 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  ar.p1==SAVEPOINT
260ae 5f 42 45 47 49 4e 20 29 7b 0d 0a 20 20 20 20 69  _BEGIN ){..    i
260af 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65  f( db->writeVdbe
260b0 43 6e 74 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  Cnt>0 ){..      
260b1 2f 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f 69  /* A new savepoi
260b2 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72 65  nt cannot be cre
260b3 61 74 65 64 20 69 66 20 74 68 65 72 65 20 61 72  ated if there ar
260b4 65 20 61 63 74 69 76 65 20 77 72 69 74 65 0d 0a  e active write..
260b5 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65        ** stateme
260b6 6e 74 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72  nts (i.e. open r
260b7 65 61 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d  ead/write increm
260b8 65 6e 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c  ental blob handl
260b9 65 73 29 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  es)...      */..
260ba 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
260bb 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
260bc 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20  sg, db, "cannot 
260bd 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 2d  open savepoint -
260be 20 22 0d 0a 20 20 20 20 20 20 20 20 22 53 51 4c   "..        "SQL
260bf 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70   statements in p
260c0 72 6f 67 72 65 73 73 22 29 3b 0d 0a 20 20 20 20  rogress");..    
260c1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
260c2 53 59 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  SY;..    }else{.
260c3 0a 20 20 20 20 20 20 75 2e 61 72 2e 6e 4e 61 6d  .      u.ar.nNam
260c4 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
260c5 6e 33 30 28 75 2e 61 72 2e 7a 4e 61 6d 65 29 3b  n30(u.ar.zName);
260c6 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
260c7 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
260c8 41 42 4c 45 0d 0a 20 20 20 20 20 20 2f 2a 20 54  ABLE..      /* T
260c9 68 69 73 20 63 61 6c 6c 20 69 73 20 4f 6b 20 65  his call is Ok e
260ca 76 65 6e 20 69 66 20 74 68 69 73 20 73 61 76 65  ven if this save
260cb 70 6f 69 6e 74 20 69 73 20 61 63 74 75 61 6c 6c  point is actuall
260cc 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0d  y a transaction.
260cd 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 6f  .      ** savepo
260ce 69 6e 74 20 28 61 6e 64 20 74 68 65 72 65 66 6f  int (and therefo
260cf 72 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 70 72  re should not pr
260d0 6f 6d 70 74 20 78 53 61 76 65 70 6f 69 6e 74 28  ompt xSavepoint(
260d1 29 29 20 63 61 6c 6c 62 61 63 6b 73 2e 0d 0a 20  )) callbacks... 
260d2 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20       ** If this 
260d3 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  is a transaction
260d4 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67   savepoint being
260d5 20 6f 70 65 6e 65 64 2c 20 69 74 20 69 73 20 67   opened, it is g
260d6 75 61 72 61 6e 74 65 65 64 0d 0a 20 20 20 20 20  uaranteed..     
260d7 20 2a 2a 20 74 68 61 74 20 74 68 65 20 64 62 2d   ** that the db-
260d8 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79  >aVTrans[] array
260d9 20 69 73 20 65 6d 70 74 79 2e 20 20 2a 2f 0d 0a   is empty.  */..
260da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
260db 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20  ->autoCommit==0 
260dc 7c 7c 20 64 62 2d 3e 6e 56 54 72 61 6e 73 3d 3d  || db->nVTrans==
260dd 30 20 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  0 );..      rc =
260de 20 73 71 6c 69 74 65 33 56 74 61 62 53 61 76 65   sqlite3VtabSave
260df 70 6f 69 6e 74 28 64 62 2c 20 53 41 56 45 50 4f  point(db, SAVEPO
260e0 49 4e 54 5f 42 45 47 49 4e 2c 0d 0a 20 20 20 20  INT_BEGIN,..    
260e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
260e2 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
260e3 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e  nStatement+db->n
260e4 53 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 20  Savepoint);..   
260e5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
260e6 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
260e7 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0d  t_due_to_error;.
260e8 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 20  .#endif....     
260e9 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   /* Create a new
260ea 20 73 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63   savepoint struc
260eb 74 75 72 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  ture. */..      
260ec 75 2e 61 72 2e 70 4e 65 77 20 3d 20 73 71 6c 69  u.ar.pNew = sqli
260ed 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
260ee 62 2c 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f  b, sizeof(Savepo
260ef 69 6e 74 29 2b 75 2e 61 72 2e 6e 4e 61 6d 65 2b  int)+u.ar.nName+
260f0 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 75  1);..      if( u
260f1 2e 61 72 2e 70 4e 65 77 20 29 7b 0d 0a 20 20 20  .ar.pNew ){..   
260f2 20 20 20 20 20 75 2e 61 72 2e 70 4e 65 77 2d 3e       u.ar.pNew->
260f3 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29  zName = (char *)
260f4 26 75 2e 61 72 2e 70 4e 65 77 5b 31 5d 3b 0d 0a  &u.ar.pNew[1];..
260f5 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 75          memcpy(u
260f6 2e 61 72 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c  .ar.pNew->zName,
260f7 20 75 2e 61 72 2e 7a 4e 61 6d 65 2c 20 75 2e 61   u.ar.zName, u.a
260f8 72 2e 6e 4e 61 6d 65 2b 31 29 3b 0d 0a 0d 0a 20  r.nName+1);.... 
260f9 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
260fa 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74 72  re is no open tr
260fb 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
260fc 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20 73  mark this as a s
260fd 70 65 63 69 61 6c 0d 0a 20 20 20 20 20 20 20 20  pecial..        
260fe 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20  ** "transaction 
260ff 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0d 0a  savepoint". */..
26100 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
26101 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0d 0a 20  autoCommit ){.. 
26102 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
26103 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0d 0a 20 20  oCommit = 0;..  
26104 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72          db->isTr
26105 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
26106 6e 74 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  nt = 1;..       
26107 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
26108 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
26109 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  t++;..        }.
2610a 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  ...        /* Li
2610b 6e 6b 20 74 68 65 20 6e 65 77 20 73 61 76 65 70  nk the new savep
2610c 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61  oint into the da
2610d 74 61 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20  tabase handle's 
2610e 6c 69 73 74 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  list. */..      
2610f 20 20 75 2e 61 72 2e 70 4e 65 77 2d 3e 70 4e 65    u.ar.pNew->pNe
26110 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  xt = db->pSavepo
26111 69 6e 74 3b 0d 0a 20 20 20 20 20 20 20 20 64 62  int;..        db
26112 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75  ->pSavepoint = u
26113 2e 61 72 2e 70 4e 65 77 3b 0d 0a 20 20 20 20 20  .ar.pNew;..     
26114 20 20 20 75 2e 61 72 2e 70 4e 65 77 2d 3e 6e 44     u.ar.pNew->nD
26115 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 64 62  eferredCons = db
26116 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b  ->nDeferredCons;
26117 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
26118 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
26119 75 2e 61 72 2e 69 53 61 76 65 70 6f 69 6e 74 20  u.ar.iSavepoint 
2611a 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46  = 0;....    /* F
2611b 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73 61  ind the named sa
2611c 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65 72  vepoint. If ther
2611d 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61 76  e is no such sav
2611e 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e 0d  epoint, then an.
2611f 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  .    ** an error
26120 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
26121 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0d 0a 20  the user.  */.. 
26122 20 20 20 66 6f 72 28 0d 0a 20 20 20 20 20 20 75     for(..      u
26123 2e 61 72 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d  .ar.pSavepoint =
26124 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b   db->pSavepoint;
26125 0d 0a 20 20 20 20 20 20 75 2e 61 72 2e 70 53 61  ..      u.ar.pSa
26126 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74  vepoint && sqlit
26127 65 33 53 74 72 49 43 6d 70 28 75 2e 61 72 2e 70  e3StrICmp(u.ar.p
26128 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65  Savepoint->zName
26129 2c 20 75 2e 61 72 2e 7a 4e 61 6d 65 29 3b 0d 0a  , u.ar.zName);..
2612a 20 20 20 20 20 20 75 2e 61 72 2e 70 53 61 76 65        u.ar.pSave
2612b 70 6f 69 6e 74 20 3d 20 75 2e 61 72 2e 70 53 61  point = u.ar.pSa
2612c 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0d 0a  vepoint->pNext..
2612d 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 75 2e      ){..      u.
2612e 61 72 2e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b  ar.iSavepoint++;
2612f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
26130 20 21 75 2e 61 72 2e 70 53 61 76 65 70 6f 69 6e   !u.ar.pSavepoin
26131 74 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  t ){..      sqli
26132 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
26133 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6e  >zErrMsg, db, "n
26134 6f 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74  o such savepoint
26135 3a 20 25 73 22 2c 20 75 2e 61 72 2e 7a 4e 61 6d  : %s", u.ar.zNam
26136 65 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  e);..      rc = 
26137 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
26138 20 20 20 7d 65 6c 73 65 20 69 66 28 0d 0a 20 20     }else if(..  
26139 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 56        db->writeV
2613a 64 62 65 43 6e 74 3e 30 20 7c 7c 20 28 75 2e 61  dbeCnt>0 || (u.a
2613b 72 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  r.p1==SAVEPOINT_
2613c 52 4f 4c 4c 42 41 43 4b 20 26 26 20 64 62 2d 3e  ROLLBACK && db->
2613d 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 29  activeVdbeCnt>1)
2613e 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20  ..    ){..      
2613f 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  /* It is not pos
26140 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65  sible to release
26141 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65   (commit) a save
26142 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 20 61  point if there a
26143 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 61 63 74  re..      ** act
26144 69 76 65 20 77 72 69 74 65 20 73 74 61 74 65 6d  ive write statem
26145 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74 20  ents. It is not 
26146 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c 6c  possible to roll
26147 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e 74  back a savepoint
26148 0d 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68  ..      ** if th
26149 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
2614a 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74  ve statements at
2614b 20 61 6c 6c 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d   all...      */.
2614c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
2614d 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
2614e 4d 73 67 2c 20 64 62 2c 0d 0a 20 20 20 20 20 20  Msg, db,..      
2614f 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76    "cannot %s sav
26150 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61  epoint - SQL sta
26151 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
26152 65 73 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 28  ess",..        (
26153 75 2e 61 72 2e 70 31 3d 3d 53 41 56 45 50 4f 49  u.ar.p1==SAVEPOI
26154 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72  NT_ROLLBACK ? "r
26155 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c 65 61  ollback": "relea
26156 73 65 22 29 0d 0a 20 20 20 20 20 20 29 3b 0d 0a  se")..      );..
26157 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
26158 45 5f 42 55 53 59 3b 0d 0a 20 20 20 20 7d 65 6c  E_BUSY;..    }el
26159 73 65 7b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20  se{....      /* 
2615a 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  Determine whethe
2615b 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 20 69 73  r or not this is
2615c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73   a transaction s
2615d 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c  avepoint. If so,
2615e 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74  ..      ** and t
2615f 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 45  his is a RELEASE
26160 20 63 6f 6d 6d 61 6e 64 2c 20 74 68 65 6e 20 74   command, then t
26161 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
26162 61 63 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a  action..      **
26163 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0d 0a   is committed...
26164 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
26165 69 6e 74 20 69 73 54 72 61 6e 73 61 63 74 69 6f  int isTransactio
26166 6e 20 3d 20 75 2e 61 72 2e 70 53 61 76 65 70 6f  n = u.ar.pSavepo
26167 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26  int->pNext==0 &&
26168 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69   db->isTransacti
26169 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0d 0a 20 20  onSavepoint;..  
2616a 20 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61      if( isTransa
2616b 63 74 69 6f 6e 20 26 26 20 75 2e 61 72 2e 70 31  ction && u.ar.p1
2616c 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
2616d 41 53 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ASE ){..        
2616e 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  if( (rc = sqlite
2616f 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20  3VdbeCheckFk(p, 
26170 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  1))!=SQLITE_OK )
26171 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  {..          got
26172 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0d 0a  o vdbe_return;..
26173 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
26174 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
26175 74 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  t = 1;..        
26176 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48  if( sqlite3VdbeH
26177 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42  alt(p)==SQLITE_B
26178 55 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  USY ){..        
26179 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0d 0a 20    p->pc = pc;.. 
2617a 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
2617b 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0d 0a 20 20  oCommit = 0;..  
2617c 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20          p->rc = 
2617d 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
2617e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ;..          got
2617f 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0d 0a  o vdbe_return;..
26180 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
26181 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63     db->isTransac
26182 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  tionSavepoint = 
26183 30 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  0;..        rc =
26184 20 70 2d 3e 72 63 3b 0d 0a 20 20 20 20 20 20 7d   p->rc;..      }
26185 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 75  else{..        u
26186 2e 61 72 2e 69 53 61 76 65 70 6f 69 6e 74 20 3d  .ar.iSavepoint =
26187 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20   db->nSavepoint 
26188 2d 20 75 2e 61 72 2e 69 53 61 76 65 70 6f 69 6e  - u.ar.iSavepoin
26189 74 20 2d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  t - 1;..        
2618a 66 6f 72 28 75 2e 61 72 2e 69 69 3d 30 3b 20 75  for(u.ar.ii=0; u
2618b 2e 61 72 2e 69 69 3c 64 62 2d 3e 6e 44 62 3b 20  .ar.ii<db->nDb; 
2618c 75 2e 61 72 2e 69 69 2b 2b 29 7b 0d 0a 20 20 20  u.ar.ii++){..   
2618d 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
2618e 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
2618f 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 72 2e 69  t(db->aDb[u.ar.i
26190 69 5d 2e 70 42 74 2c 20 75 2e 61 72 2e 70 31 2c  i].pBt, u.ar.p1,
26191 20 75 2e 61 72 2e 69 53 61 76 65 70 6f 69 6e 74   u.ar.iSavepoint
26192 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  );..          if
26193 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
26194 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
26195 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
26196 6f 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 20 20  o_error;..      
26197 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
26198 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  ..        if( u.
26199 61 72 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  ar.p1==SAVEPOINT
2619a 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62  _ROLLBACK && (db
2619b 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49  ->flags&SQLITE_I
2619c 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30  nternChanges)!=0
2619d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
2619e 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
2619f 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
261a0 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  b);..          s
261a1 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
261a2 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 2d 31  nalSchema(db, -1
261a3 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 64 62  );..          db
261a4 2d 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66  ->flags = (db->f
261a5 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e  lags | SQLITE_In
261a6 74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0d 0a 20  ternChanges);.. 
261a7 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
261a8 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 52 65  }....      /* Re
261a9 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
261aa 68 65 72 20 74 68 69 73 20 69 73 20 61 20 52 45  her this is a RE
261ab 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43  LEASE or ROLLBAC
261ac 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c 0d 0a  K, destroy all..
261ad 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 6f 69        ** savepoi
261ae 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 69 64  nts nested insid
261af 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69  e of the savepoi
261b0 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65  nt being operate
261b1 64 20 6f 6e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  d on. */..      
261b2 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65  while( db->pSave
261b3 70 6f 69 6e 74 21 3d 75 2e 61 72 2e 70 53 61 76  point!=u.ar.pSav
261b4 65 70 6f 69 6e 74 20 29 7b 0d 0a 20 20 20 20 20  epoint ){..     
261b5 20 20 20 75 2e 61 72 2e 70 54 6d 70 20 3d 20 64     u.ar.pTmp = d
261b6 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0d 0a  b->pSavepoint;..
261b7 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76          db->pSav
261b8 65 70 6f 69 6e 74 20 3d 20 75 2e 61 72 2e 70 54  epoint = u.ar.pT
261b9 6d 70 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  mp->pNext;..    
261ba 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
261bb 65 28 64 62 2c 20 75 2e 61 72 2e 70 54 6d 70 29  e(db, u.ar.pTmp)
261bc 3b 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e  ;..        db->n
261bd 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0d 0a 20 20  Savepoint--;..  
261be 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f      }....      /
261bf 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 45 4c  * If it is a REL
261c0 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 74 72  EASE, then destr
261c1 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  oy the savepoint
261c2 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20   being operated 
261c3 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6f  on..      ** too
261c4 2e 20 49 66 20 69 74 20 69 73 20 61 20 52 4f 4c  . If it is a ROL
261c5 4c 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e 20 73  LBACK TO, then s
261c6 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  et the number of
261c7 20 64 65 66 65 72 72 65 64 0d 0a 20 20 20 20 20   deferred..     
261c8 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76   ** constraint v
261c9 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 6e  iolations presen
261ca 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
261cb 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73  e to the value s
261cc 74 6f 72 65 64 0d 0a 20 20 20 20 20 20 2a 2a 20  tored..      ** 
261cd 77 68 65 6e 20 74 68 65 20 73 61 76 65 70 6f 69  when the savepoi
261ce 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 20  nt was created. 
261cf 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 75   */..      if( u
261d0 2e 61 72 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .ar.p1==SAVEPOIN
261d1 54 5f 52 45 4c 45 41 53 45 20 29 7b 0d 0a 20 20  T_RELEASE ){..  
261d2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
261d3 61 72 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64  ar.pSavepoint==d
261d4 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b  b->pSavepoint );
261d5 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53  ..        db->pS
261d6 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 72 2e  avepoint = u.ar.
261d7 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78  pSavepoint->pNex
261d8 74 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t;..        sqli
261d9 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e  te3DbFree(db, u.
261da 61 72 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b 0d  ar.pSavepoint);.
261db 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73  .        if( !is
261dc 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0d 0a  Transaction ){..
261dd 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53            db->nS
261de 61 76 65 70 6f 69 6e 74 2d 2d 3b 0d 0a 20 20 20  avepoint--;..   
261df 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65       }..      }e
261e0 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 64 62  lse{..        db
261e1 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20  ->nDeferredCons 
261e2 3d 20 75 2e 61 72 2e 70 53 61 76 65 70 6f 69 6e  = u.ar.pSavepoin
261e3 74 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73  t->nDeferredCons
261e4 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  ;..      }....  
261e5 20 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73      if( !isTrans
261e6 61 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20 20 20  action ){..     
261e7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
261e8 74 61 62 53 61 76 65 70 6f 69 6e 74 28 64 62 2c  tabSavepoint(db,
261e9 20 75 2e 61 72 2e 70 31 2c 20 75 2e 61 72 2e 69   u.ar.p1, u.ar.i
261ea 53 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 20  Savepoint);..   
261eb 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
261ec 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
261ed 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
261ee 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
261ef 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 62 72 65 61  }..  }....  brea
261f0 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f  k;..}..../* Opco
261f1 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50  de: AutoCommit P
261f2 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  1 P2 * * *..**..
261f3 2a 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  ** Set the datab
261f4 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  ase auto-commit 
261f5 66 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72  flag to P1 (1 or
261f6 20 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72   0). If P2 is tr
261f7 75 65 2c 20 72 6f 6c 6c 0d 0a 2a 2a 20 62 61 63  ue, roll..** bac
261f8 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20  k any currently 
261f9 61 63 74 69 76 65 20 62 74 72 65 65 20 74 72 61  active btree tra
261fa 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68  nsactions. If th
261fb 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
261fc 76 65 0d 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72  ve..** VMs (apar
261fd 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29  t from this one)
261fe 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43  , then a ROLLBAC
261ff 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d  K fails.  A COMM
26200 49 54 20 66 61 69 6c 73 20 69 66 0d 0a 2a 2a 20  IT fails if..** 
26201 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65  there are active
26202 20 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 20   writing VMs or 
26203 61 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 20  active VMs that 
26204 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65  use shared cache
26205 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  ...**..** This i
26206 6e 73 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65  nstruction cause
26207 73 20 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74  s the VM to halt
26208 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 41  ...*/..case OP_A
26209 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b 0d 0a 23 69  utoCommit: {..#i
2620a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
2620b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2620c 74 6f 20 75 2e 61 73 20 2a 2f 0d 0a 20 20 69 6e  to u.as */..  in
2620d 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  t desiredAutoCom
2620e 6d 69 74 3b 0d 0a 20 20 69 6e 74 20 69 52 6f 6c  mit;..  int iRol
2620f 6c 62 61 63 6b 3b 0d 0a 20 20 69 6e 74 20 74 75  lback;..  int tu
26210 72 6e 4f 6e 41 43 3b 0d 0a 23 65 6e 64 69 66 20  rnOnAC;..#endif 
26211 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
26212 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
26213 61 73 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 61 73 2e  as */....  u.as.
26214 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
26215 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a 20 20  t = pOp->p1;..  
26216 75 2e 61 73 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d  u.as.iRollback =
26217 20 70 4f 70 2d 3e 70 32 3b 0d 0a 20 20 75 2e 61   pOp->p2;..  u.a
26218 73 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61  s.turnOnAC = u.a
26219 73 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  s.desiredAutoCom
2621a 6d 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f  mit && !db->auto
2621b 43 6f 6d 6d 69 74 3b 0d 0a 20 20 61 73 73 65 72  Commit;..  asser
2621c 74 28 20 75 2e 61 73 2e 64 65 73 69 72 65 64 41  t( u.as.desiredA
2621d 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20  utoCommit==1 || 
2621e 75 2e 61 73 2e 64 65 73 69 72 65 64 41 75 74 6f  u.as.desiredAuto
2621f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0d 0a 20 20  Commit==0 );..  
26220 61 73 73 65 72 74 28 20 75 2e 61 73 2e 64 65 73  assert( u.as.des
26221 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d  iredAutoCommit==
26222 31 20 7c 7c 20 75 2e 61 73 2e 69 52 6f 6c 6c 62  1 || u.as.iRollb
26223 61 63 6b 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73  ack==0 );..  ass
26224 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56  ert( db->activeV
26225 64 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a 20  dbeCnt>0 );  /* 
26226 41 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f 6e  At least this on
26227 65 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 2a  e VM is active *
26228 2f 0d 0a 0d 0a 20 20 69 66 28 20 75 2e 61 73 2e  /....  if( u.as.
26229 74 75 72 6e 4f 6e 41 43 20 26 26 20 75 2e 61 73  turnOnAC && u.as
2622a 2e 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62  .iRollback && db
2622b 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
2622c 31 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20  1 ){..    /* If 
2622d 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
2622e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52 4f   implements a RO
2622f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f 74 68 65 72  LLBACK and other
26230 20 56 4d 73 20 61 72 65 0d 0a 20 20 20 20 2a 2a   VMs are..    **
26231 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20   still running, 
26232 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
26233 6e 20 69 73 20 61 63 74 69 76 65 2c 20 72 65 74  n is active, ret
26234 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64  urn an error ind
26235 69 63 61 74 69 6e 67 0d 0a 20 20 20 20 2a 2a 20  icating..    ** 
26236 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 56  that the other V
26237 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65  Ms must complete
26238 20 66 69 72 73 74 2e 0d 0a 20 20 20 20 2a 2f 0d   first...    */.
26239 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
2623a 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
2623b 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72  g, db, "cannot r
2623c 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74  ollback transact
2623d 69 6f 6e 20 2d 20 22 0d 0a 20 20 20 20 20 20 20  ion - "..       
2623e 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73   "SQL statements
2623f 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0d   in progress");.
26240 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
26241 5f 42 55 53 59 3b 0d 0a 20 20 7d 65 6c 73 65 20  _BUSY;..  }else 
26242 69 66 28 20 75 2e 61 73 2e 74 75 72 6e 4f 6e 41  if( u.as.turnOnA
26243 43 20 26 26 20 21 75 2e 61 73 2e 69 52 6f 6c 6c  C && !u.as.iRoll
26244 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74  back && db->writ
26245 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0d 0a 20  eVdbeCnt>0 ){.. 
26246 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e     /* If this in
26247 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d  struction implem
26248 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e  ents a COMMIT an
26249 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20  d other VMs are 
2624a 77 72 69 74 69 6e 67 0d 0a 20 20 20 20 2a 2a 20  writing..    ** 
2624b 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
2624c 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
2624d 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75  the other VMs mu
2624e 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73  st complete firs
2624f 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  t...    */..    
26250 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
26251 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
26252 2c 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74  , "cannot commit
26253 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22   transaction - "
26254 0d 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73  ..        "SQL s
26255 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
26256 67 72 65 73 73 22 29 3b 0d 0a 20 20 20 20 72 63  gress");..    rc
26257 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d   = SQLITE_BUSY;.
26258 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61  .  }else if( u.a
26259 73 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  s.desiredAutoCom
2625a 6d 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d  mit!=db->autoCom
2625b 6d 69 74 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  mit ){..    if( 
2625c 75 2e 61 73 2e 69 52 6f 6c 6c 62 61 63 6b 20 29  u.as.iRollback )
2625d 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
2625e 20 75 2e 61 73 2e 64 65 73 69 72 65 64 41 75 74   u.as.desiredAut
2625f 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0d 0a 20  oCommit==1 );.. 
26260 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
26261 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0d 0a 20 20  backAll(db);..  
26262 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
26263 69 74 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 65 6c  it = 1;..    }el
26264 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c  se if( (rc = sql
26265 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28  ite3VdbeCheckFk(
26266 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f  p, 1))!=SQLITE_O
26267 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  K ){..      goto
26268 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0d 0a 20   vdbe_return;.. 
26269 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2626a 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
2626b 3d 20 28 75 38 29 75 2e 61 73 2e 64 65 73 69 72  = (u8)u.as.desir
2626c 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0d 0a 20  edAutoCommit;.. 
2626d 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2626e 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c  VdbeHalt(p)==SQL
2626f 49 54 45 5f 42 55 53 59 20 29 7b 0d 0a 20 20 20  ITE_BUSY ){..   
26270 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b       p->pc = pc;
26271 0d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ..        db->au
26272 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28  toCommit = (u8)(
26273 31 2d 75 2e 61 73 2e 64 65 73 69 72 65 64 41 75  1-u.as.desiredAu
26274 74 6f 43 6f 6d 6d 69 74 29 3b 0d 0a 20 20 20 20  toCommit);..    
26275 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
26276 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20   SQLITE_BUSY;.. 
26277 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65         goto vdbe
26278 5f 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20  _return;..      
26279 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73  }..    }..    as
2627a 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65  sert( db->nState
2627b 6d 65 6e 74 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  ment==0 );..    
2627c 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
2627d 70 6f 69 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20  points(db);..   
2627e 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
2627f 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
26280 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
26281 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
26282 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
26283 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 7d 0d 0a  _ERROR;..    }..
26284 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
26285 74 75 72 6e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  turn;..  }else{.
26286 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
26287 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
26288 67 2c 20 64 62 2c 0d 0a 20 20 20 20 20 20 20 20  g, db,..        
26289 28 21 75 2e 61 73 2e 64 65 73 69 72 65 64 41 75  (!u.as.desiredAu
2628a 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f  toCommit)?"canno
2628b 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61  t start a transa
2628c 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74  ction within a t
2628d 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0d 0a 20  ransaction":(.. 
2628e 20 20 20 20 20 20 20 28 75 2e 61 73 2e 69 52 6f         (u.as.iRo
2628f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20  llback)?"cannot 
26290 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72  rollback - no tr
26291 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
26292 69 76 65 22 3a 0d 0a 20 20 20 20 20 20 20 20 20  ive":..         
26293 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
26294 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72  t commit - no tr
26295 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
26296 69 76 65 22 29 29 3b 0d 0a 0d 0a 20 20 20 20 72  ive"));....    r
26297 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
26298 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b  ;..  }..  break;
26299 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
2629a 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50 31  : Transaction P1
2629b 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a   P2 * * *..**..*
2629c 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61  * Begin a transa
2629d 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e  ction.  The tran
2629e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65  saction ends whe
2629f 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f  n a Commit or Ro
262a0 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 6f 70 63 6f 64  llback..** opcod
262a1 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  e is encountered
262a2 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
262a3 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
262a4 73 65 74 74 69 6e 67 2c 20 74 68 65 0d 0a 2a 2a  setting, the..**
262a5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67   transaction mig
262a6 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65  ht also be rolle
262a7 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72  d back if an err
262a8 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
262a9 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 69 73  d...**..** P1 is
262aa 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
262ab 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
262ac 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61  on which the tra
262ad 6e 73 61 63 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20  nsaction is..** 
262ae 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20  started.  Index 
262af 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  0 is the main da
262b0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
262b1 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0d 0a  index 1 is the..
262b2 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  ** file used for
262b3 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
262b4 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32  s.  Indices of 2
262b5 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65   or more are use
262b6 64 20 66 6f 72 0d 0a 2a 2a 20 61 74 74 61 63 68  d for..** attach
262b7 65 64 20 64 61 74 61 62 61 73 65 73 2e 0d 0a 2a  ed databases...*
262b8 2a 0d 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e  *..** If P2 is n
262b9 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20  on-zero, then a 
262ba 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
262bb 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 41  n is started.  A
262bc 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69   RESERVED lock i
262bd 73 0d 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f  s..** obtained o
262be 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
262bf 69 6c 65 20 77 68 65 6e 20 61 20 77 72 69 74 65  ile when a write
262c0 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
262c1 73 74 61 72 74 65 64 2e 20 20 4e 6f 0d 0a 2a 2a  started.  No..**
262c2 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63   other process c
262c3 61 6e 20 73 74 61 72 74 20 61 6e 6f 74 68 65 72  an start another
262c4 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
262c5 6f 6e 20 77 68 69 6c 65 20 74 68 69 73 20 74 72  on while this tr
262c6 61 6e 73 61 63 74 69 6f 6e 20 69 73 0d 0a 2a 2a  ansaction is..**
262c7 20 75 6e 64 65 72 77 61 79 2e 20 20 53 74 61 72   underway.  Star
262c8 74 69 6e 67 20 61 20 77 72 69 74 65 20 74 72 61  ting a write tra
262c9 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f 20 63 72  nsaction also cr
262ca 65 61 74 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b  eates a rollback
262cb 20 6a 6f 75 72 6e 61 6c 2e 20 41 0d 0a 2a 2a 20   journal. A..** 
262cc 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
262cd 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65  n must be starte
262ce 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  d before any cha
262cf 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61 64 65  nges can be made
262d0 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61   to the..** data
262d1 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73 20  base.  If P2 is 
262d2 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 65  2 or greater the
262d3 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  n an EXCLUSIVE l
262d4 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74 61  ock is also obta
262d5 69 6e 65 64 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20  ined..** on the 
262d6 66 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  file...**..** If
262d7 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
262d8 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20  tion is started 
262d9 61 6e 64 20 74 68 65 20 56 64 62 65 2e 75 73 65  and the Vdbe.use
262da 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 6c 61  sStmtJournal fla
262db 67 20 69 73 0d 0a 2a 2a 20 74 72 75 65 20 28 74  g is..** true (t
262dc 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20  his flag is set 
262dd 69 66 20 74 68 65 20 56 64 62 65 20 6d 61 79 20  if the Vdbe may 
262de 6d 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 61 6e  modify more than
262df 20 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d 61 79   one row and may
262e0 0d 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42  ..** throw an AB
262e1 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 29 2c 20  ORT exception), 
262e2 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
262e3 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f  saction may also
262e4 20 62 65 20 6f 70 65 6e 65 64 2e 0d 0a 2a 2a 20   be opened...** 
262e5 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c  More specificall
262e6 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  y, a statement t
262e7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
262e8 65 6e 65 64 20 69 66 66 20 74 68 65 20 64 61 74  ened iff the dat
262e9 61 62 61 73 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63  abase..** connec
262ea 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c  tion is currentl
262eb 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d  y not in autocom
262ec 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 20  mit mode, or if 
262ed 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 0d  there are other.
262ee 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 65  .** active state
262ef 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65  ments. A stateme
262f0 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  nt transaction a
262f1 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 74  llows the affect
262f2 73 20 6f 66 20 74 68 69 73 0d 0a 2a 2a 20 56 44  s of this..** VD
262f3 42 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  BE to be rolled 
262f4 62 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  back after an er
262f5 72 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69  ror without havi
262f6 6e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  ng to roll back 
262f7 74 68 65 0d 0a 2a 2a 20 65 6e 74 69 72 65 20 74  the..** entire t
262f8 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e  ransaction. If n
262f9 6f 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  o error is encou
262fa 6e 74 65 72 65 64 2c 20 74 68 65 20 73 74 61 74  ntered, the stat
262fb 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
262fc 6e 0d 0a 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d  n..** will autom
262fd 61 74 69 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20  atically commit 
262fe 77 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61  when the VDBE ha
262ff 6c 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  lts...**..** If 
26300 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  P2 is zero, then
26301 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20   a read-lock is 
26302 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20  obtained on the 
26303 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
26304 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e  */..case OP_Tran
26305 73 61 63 74 69 6f 6e 3a 20 7b 0d 0a 23 69 66 20  saction: {..#if 
26306 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
26307 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
26308 20 75 2e 61 74 20 2a 2f 0d 0a 20 20 42 74 72 65   u.at */..  Btre
26309 65 20 2a 70 42 74 3b 0d 0a 23 65 6e 64 69 66 20  e *pBt;..#endif 
2630a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
2630b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
2630c 61 74 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72  at */....  asser
2630d 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
2630e 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62   pOp->p1<db->nDb
2630f 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28   );..  assert( (
26310 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
26311 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f  ((yDbMask)1)<<pO
26312 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0d 0a 20  p->p1))!=0 );.. 
26313 20 75 2e 61 74 2e 70 42 74 20 3d 20 64 62 2d 3e   u.at.pBt = db->
26314 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74  aDb[pOp->p1].pBt
26315 3b 0d 0a 0d 0a 20 20 69 66 28 20 75 2e 61 74 2e  ;....  if( u.at.
26316 70 42 74 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  pBt ){..    rc =
26317 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
26318 69 6e 54 72 61 6e 73 28 75 2e 61 74 2e 70 42 74  inTrans(u.at.pBt
26319 2c 20 70 4f 70 2d 3e 70 32 29 3b 0d 0a 20 20 20  , pOp->p2);..   
2631a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2631b 42 55 53 59 20 29 7b 0d 0a 20 20 20 20 20 20 70  BUSY ){..      p
2631c 2d 3e 70 63 20 3d 20 70 63 3b 0d 0a 20 20 20 20  ->pc = pc;..    
2631d 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53    p->rc = rc = S
2631e 51 4c 49 54 45 5f 42 55 53 59 3b 0d 0a 20 20 20  QLITE_BUSY;..   
2631f 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
26320 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  urn;..    }..   
26321 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
26322 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74  OK ){..      got
26323 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
26324 72 72 6f 72 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  rror;..    }....
26325 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20      if( pOp->p2 
26326 26 26 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f  && p->usesStmtJo
26327 75 72 6e 61 6c 0d 0a 20 20 20 20 20 26 26 20 28  urnal..     && (
26328 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
26329 30 20 7c 7c 20 64 62 2d 3e 61 63 74 69 76 65 56  0 || db->activeV
2632a 64 62 65 43 6e 74 3e 31 29 0d 0a 20 20 20 20 29  dbeCnt>1)..    )
2632b 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
2632c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
2632d 6e 54 72 61 6e 73 28 75 2e 61 74 2e 70 42 74 29  nTrans(u.at.pBt)
2632e 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70   );..      if( p
2632f 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->iStatement==0 
26330 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
26331 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  rt( db->nStateme
26332 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e 53 61  nt>=0 && db->nSa
26333 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0d 0a 20  vepoint>=0 );.. 
26334 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74         db->nStat
26335 65 6d 65 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20 20  ement++;..      
26336 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20    p->iStatement 
26337 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  = db->nSavepoint
26338 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e   + db->nStatemen
26339 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  t;..      }.... 
2633a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2633b 33 56 74 61 62 53 61 76 65 70 6f 69 6e 74 28 64  3VtabSavepoint(d
2633c 62 2c 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  b, SAVEPOINT_BEG
2633d 49 4e 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  IN, p->iStatemen
2633e 74 2d 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  t-1);..      if(
2633f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
26340 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
26341 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
26342 6e 53 74 6d 74 28 75 2e 61 74 2e 70 42 74 2c 20  nStmt(u.at.pBt, 
26343 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 29 3b 0d  p->iStatement);.
26344 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  .      }....    
26345 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63    /* Store the c
26346 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
26347 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
26348 64 6c 65 73 20 64 65 66 65 72 72 65 64 20 63 6f  dles deferred co
26349 6e 73 74 72 61 69 6e 74 0d 0a 20 20 20 20 20 20  nstraint..      
2634a 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 49 66 20 74  ** counter. If t
2634b 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
2634c 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  nsaction needs t
2634d 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
2634e 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  ,..      ** the 
2634f 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f  value of this co
26350 75 6e 74 65 72 20 6e 65 65 64 73 20 74 6f 20 62  unter needs to b
26351 65 20 72 65 73 74 6f 72 65 64 20 74 6f 6f 2e 20  e restored too. 
26352 20 2a 2f 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 53   */..      p->nS
26353 74 6d 74 44 65 66 43 6f 6e 73 20 3d 20 64 62 2d  tmtDefCons = db-
26354 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0d  >nDeferredCons;.
26355 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 62  .    }..  }..  b
26356 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f  reak;..}..../* O
26357 70 63 6f 64 65 3a 20 52 65 61 64 43 6f 6f 6b 69  pcode: ReadCooki
26358 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a  e P1 P2 P3 * *..
26359 2a 2a 0d 0a 2a 2a 20 52 65 61 64 20 63 6f 6f 6b  **..** Read cook
2635a 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 6f  ie number P3 fro
2635b 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 6e  m database P1 an
2635c 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
2635d 72 65 67 69 73 74 65 72 20 50 32 2e 0d 0a 2a 2a  register P2...**
2635e 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 63   P3==1 is the sc
2635f 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
26360 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61  3==2 is the data
26361 62 61 73 65 20 66 6f 72 6d 61 74 2e 0d 0a 2a 2a  base format...**
26362 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65   P3==3 is the re
26363 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20  commended pager 
26364 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20  cache size, and 
26365 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30  so forth.  P1==0
26366 20 69 73 0d 0a 2a 2a 20 74 68 65 20 6d 61 69 6e   is..** the main
26367 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
26368 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
26369 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
2636a 65 64 20 74 6f 20 73 74 6f 72 65 0d 0a 2a 2a 20  ed to store..** 
2636b 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
2636c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20  ...**..** There 
2636d 6d 75 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c  must be a read-l
2636e 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
2636f 61 73 65 20 28 65 69 74 68 65 72 20 61 20 74 72  ase (either a tr
26370 61 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75  ansaction..** mu
26371 73 74 20 62 65 20 73 74 61 72 74 65 64 20 6f 72  st be started or
26372 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
26373 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 29 20 62  n open cursor) b
26374 65 66 6f 72 65 0d 0a 2a 2a 20 65 78 65 63 75 74  efore..** execut
26375 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63  ing this instruc
26376 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  tion...*/..case 
26377 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 3a 20 7b  OP_ReadCookie: {
26378 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26379 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
2637a 65 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f 2a 20  e */..#if 0  /* 
2637b 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
2637c 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20  moved into u.au 
2637d 2a 2f 0d 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b  */..  int iMeta;
2637e 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20 20  ..  int iDb;..  
2637f 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0d 0a 23 65  int iCookie;..#e
26380 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
26381 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
26382 74 6f 20 75 2e 61 75 20 2a 2f 0d 0a 0d 0a 20 20  to u.au */....  
26383 75 2e 61 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e  u.au.iDb = pOp->
26384 70 31 3b 0d 0a 20 20 75 2e 61 75 2e 69 43 6f 6f  p1;..  u.au.iCoo
26385 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0d 0a  kie = pOp->p3;..
26386 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
26387 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  3<SQLITE_N_BTREE
26388 5f 4d 45 54 41 20 29 3b 0d 0a 20 20 61 73 73 65  _META );..  asse
26389 72 74 28 20 75 2e 61 75 2e 69 44 62 3e 3d 30 20  rt( u.au.iDb>=0 
2638a 26 26 20 75 2e 61 75 2e 69 44 62 3c 64 62 2d 3e  && u.au.iDb<db->
2638b 6e 44 62 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  nDb );..  assert
2638c 28 20 64 62 2d 3e 61 44 62 5b 75 2e 61 75 2e 69  ( db->aDb[u.au.i
2638d 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0d 0a 20  Db].pBt!=0 );.. 
2638e 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
2638f 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62 4d  eeMask & (((yDbM
26390 61 73 6b 29 31 29 3c 3c 75 2e 61 75 2e 69 44 62  ask)1)<<u.au.iDb
26391 29 29 21 3d 30 20 29 3b 0d 0a 0d 0a 20 20 73 71  ))!=0 );....  sq
26392 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
26393 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 75 2e 69  a(db->aDb[u.au.i
26394 44 62 5d 2e 70 42 74 2c 20 75 2e 61 75 2e 69 43  Db].pBt, u.au.iC
26395 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75  ookie, (u32 *)&u
26396 2e 61 75 2e 69 4d 65 74 61 29 3b 0d 0a 20 20 70  .au.iMeta);..  p
26397 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 75 2e  Out->u.i = u.au.
26398 69 4d 65 74 61 3b 0d 0a 20 20 62 72 65 61 6b 3b  iMeta;..  break;
26399 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
2639a 3a 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50  : SetCookie P1 P
2639b 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  2 P3 * *..**..**
2639c 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65   Write the conte
2639d 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
2639e 33 20 28 69 6e 74 65 72 70 72 65 74 65 64 20 61  3 (interpreted a
2639f 73 20 61 6e 20 69 6e 74 65 67 65 72 29 0d 0a 2a  s an integer)..*
263a0 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75  * into cookie nu
263a1 6d 62 65 72 20 50 32 20 6f 66 20 64 61 74 61 62  mber P2 of datab
263a2 61 73 65 20 50 31 2e 20 20 50 32 3d 3d 31 20 69  ase P1.  P2==1 i
263a3 73 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72  s the schema ver
263a4 73 69 6f 6e 2e 20 20 0d 0a 2a 2a 20 50 32 3d 3d  sion.  ..** P2==
263a5 32 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  2 is the databas
263a6 65 20 66 6f 72 6d 61 74 2e 20 50 32 3d 3d 33 20  e format. P2==3 
263a7 69 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64  is the recommend
263a8 65 64 20 70 61 67 65 72 20 63 61 63 68 65 20 0d  ed pager cache .
263a9 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f  .** size, and so
263aa 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69   forth.  P1==0 i
263ab 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  s the main datab
263ac 61 73 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d  ase file and P1=
263ad 3d 31 20 69 73 20 74 68 65 20 0d 0a 2a 2a 20 64  =1 is the ..** d
263ae 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65  atabase file use
263af 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f  d to store tempo
263b0 72 61 72 79 20 74 61 62 6c 65 73 2e 0d 0a 2a 2a  rary tables...**
263b1 0d 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  ..** A transacti
263b2 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
263b3 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  ed before execut
263b4 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
263b5 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 53 65  ..*/..case OP_Se
263b6 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20  tCookie: {      
263b7 20 2f 2a 20 69 6e 33 20 2a 2f 0d 0a 23 69 66 20   /* in3 */..#if 
263b8 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
263b9 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
263ba 20 75 2e 61 76 20 2a 2f 0d 0a 20 20 44 62 20 2a   u.av */..  Db *
263bb 70 44 62 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  pDb;..#endif /* 
263bc 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
263bd 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20  moved into u.av 
263be 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
263bf 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42  p->p2<SQLITE_N_B
263c0 54 52 45 45 5f 4d 45 54 41 20 29 3b 0d 0a 20 20  TREE_META );..  
263c1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
263c2 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
263c3 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 61 73 73 65  ->nDb );..  asse
263c4 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
263c5 6b 20 26 20 28 28 28 79 44 62 4d 61 73 6b 29 31  k & (((yDbMask)1
263c6 29 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  )<<pOp->p1))!=0 
263c7 29 3b 0d 0a 20 20 75 2e 61 76 2e 70 44 62 20 3d  );..  u.av.pDb =
263c8 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70   &db->aDb[pOp->p
263c9 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75  1];..  assert( u
263ca 2e 61 76 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20  .av.pDb->pBt!=0 
263cb 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
263cc 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
263cd 48 65 6c 64 28 64 62 2c 20 70 4f 70 2d 3e 70 31  Held(db, pOp->p1
263ce 2c 20 30 29 20 29 3b 0d 0a 20 20 70 49 6e 33 20  , 0) );..  pIn3 
263cf 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  = &aMem[pOp->p3]
263d0 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
263d1 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
263d2 6e 33 29 3b 0d 0a 20 20 2f 2a 20 53 65 65 20 6e  n3);..  /* See n
263d3 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 65 78 20  ote about index 
263d4 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f 50 5f 52  shifting on OP_R
263d5 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0d 0a 20 20  eadCookie */..  
263d6 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
263d7 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e 61 76  eUpdateMeta(u.av
263d8 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70 2d 3e  .pDb->pBt, pOp->
263d9 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75  p2, (int)pIn3->u
263da 2e 69 29 3b 0d 0a 20 20 69 66 28 20 70 4f 70 2d  .i);..  if( pOp-
263db 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45 4d  >p2==BTREE_SCHEM
263dc 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0d 0a 20 20  A_VERSION ){..  
263dd 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63    /* When the sc
263de 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e  hema cookie chan
263df 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20  ges, record the 
263e0 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72  new cookie inter
263e1 6e 61 6c 6c 79 20 2a 2f 0d 0a 20 20 20 20 75 2e  nally */..    u.
263e2 61 76 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  av.pDb->pSchema-
263e3 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d  >schema_cookie =
263e4 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b   (int)pIn3->u.i;
263e5 0d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  ..    db->flags 
263e6 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
263e7 43 68 61 6e 67 65 73 3b 0d 0a 20 20 7d 65 6c 73  Changes;..  }els
263e8 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42  e if( pOp->p2==B
263e9 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
263ea 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 52 65 63 6f   ){..    /* Reco
263eb 72 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  rd changes in th
263ec 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f  e file format */
263ed 0d 0a 20 20 20 20 75 2e 61 76 2e 70 44 62 2d 3e  ..    u.av.pDb->
263ee 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
263ef 72 6d 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d  rmat = (u8)pIn3-
263f0 3e 75 2e 69 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  >u.i;..  }..  if
263f1 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 7b 0d  ( pOp->p1==1 ){.
263f2 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61  .    /* Invalida
263f3 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20  te all prepared 
263f4 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e 65  statements whene
263f5 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61 74  ver the TEMP dat
263f6 61 62 61 73 65 0d 0a 20 20 20 20 2a 2a 20 73 63  abase..    ** sc
263f7 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e  hema is changed.
263f8 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a    Ticket #1644 *
263f9 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  /..    sqlite3Ex
263fa 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
263fb 65 6d 65 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20  ements(db);..   
263fc 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b   p->expired = 0;
263fd 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d  ..  }..  break;.
263fe 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}..../* Opcode:
263ff 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31   VerifyCookie P1
26400 20 50 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a   P2 P3 * *..**..
26401 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c  ** Check the val
26402 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74  ue of global dat
26403 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 20  abase parameter 
26404 6e 75 6d 62 65 72 20 30 20 28 74 68 65 0d 0a 2a  number 0 (the..*
26405 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  * schema version
26406 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20  ) and make sure 
26407 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50  it is equal to P
26408 32 20 61 6e 64 20 74 68 61 74 20 74 68 65 0d 0a  2 and that the..
26409 2a 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f  ** generation co
2640a 75 6e 74 65 72 20 6f 6e 20 74 68 65 20 6c 6f 63  unter on the loc
2640b 61 6c 20 73 63 68 65 6d 61 20 70 61 72 73 65 20  al schema parse 
2640c 65 71 75 61 6c 73 20 50 33 2e 0d 0a 2a 2a 0d 0a  equals P3...**..
2640d 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74  ** P1 is the dat
2640e 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69  abase number whi
2640f 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20  ch is 0 for the 
26410 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
26411 6c 65 0d 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72  le..** and 1 for
26412 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e   the file holdin
26413 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  g temporary tabl
26414 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68  es and some high
26415 65 72 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 66 6f  er number..** fo
26416 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  r auxiliary data
26417 62 61 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  bases...**..** T
26418 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65  he cookie change
26419 73 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e  s its value when
2641a 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73  ever the databas
2641b 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
2641c 2e 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61  ...** This opera
2641d 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
2641e 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 61 74  detect when that
2641f 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20   the cookie has 
26420 63 68 61 6e 67 65 64 0d 0a 2a 2a 20 61 6e 64 20  changed..** and 
26421 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  that the current
26422 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74   process needs t
26423 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68  o reread the sch
26424 65 6d 61 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 69 74  ema...**..** Eit
26425 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  her a transactio
26426 6e 20 6e 65 65 64 73 20 74 6f 20 68 61 76 65 20  n needs to have 
26427 62 65 65 6e 20 73 74 61 72 74 65 64 20 6f 72 20  been started or 
26428 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73  an OP_Open needs
26429 0d 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65 63 75  ..** to be execu
2642a 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c 69 73  ted (to establis
2642b 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29 20 62  h a read lock) b
2642c 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64  efore this opcod
2642d 65 20 69 73 0d 0a 2a 2a 20 69 6e 76 6f 6b 65 64  e is..** invoked
2642e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 56  ...*/..case OP_V
2642f 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0d 0a  erifyCookie: {..
26430 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
26431 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
26432 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0d 0a 20 20  into u.aw */..  
26433 69 6e 74 20 69 4d 65 74 61 3b 0d 0a 20 20 69 6e  int iMeta;..  in
26434 74 20 69 47 65 6e 3b 0d 0a 20 20 42 74 72 65 65  t iGen;..  Btree
26435 20 2a 70 42 74 3b 0d 0a 23 65 6e 64 69 66 20 2f   *pBt;..#endif /
26436 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
26437 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
26438 77 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  w */....  assert
26439 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
2643a 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
2643b 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70  );..  assert( (p
2643c 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28  ->btreeMask & ((
2643d 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70  (yDbMask)1)<<pOp
2643e 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0d 0a 20 20  ->p1))!=0 );..  
2643f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
26440 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
26441 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 29 20 29  b, pOp->p1, 0) )
26442 3b 0d 0a 20 20 75 2e 61 77 2e 70 42 74 20 3d 20  ;..  u.aw.pBt = 
26443 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
26444 2e 70 42 74 3b 0d 0a 20 20 69 66 28 20 75 2e 61  .pBt;..  if( u.a
26445 77 2e 70 42 74 20 29 7b 0d 0a 20 20 20 20 73 71  w.pBt ){..    sq
26446 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
26447 61 28 75 2e 61 77 2e 70 42 74 2c 20 42 54 52 45  a(u.aw.pBt, BTRE
26448 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
26449 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 77 2e 69  , (u32 *)&u.aw.i
2644a 4d 65 74 61 29 3b 0d 0a 20 20 20 20 75 2e 61 77  Meta);..    u.aw
2644b 2e 69 47 65 6e 20 3d 20 64 62 2d 3e 61 44 62 5b  .iGen = db->aDb[
2644c 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61  pOp->p1].pSchema
2644d 2d 3e 69 47 65 6e 65 72 61 74 69 6f 6e 3b 0d 0a  ->iGeneration;..
2644e 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 75 2e    }else{..    u.
2644f 61 77 2e 69 47 65 6e 20 3d 20 75 2e 61 77 2e 69  aw.iGen = u.aw.i
26450 4d 65 74 61 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  Meta = 0;..  }..
26451 20 20 69 66 28 20 75 2e 61 77 2e 69 4d 65 74 61    if( u.aw.iMeta
26452 21 3d 70 4f 70 2d 3e 70 32 20 7c 7c 20 75 2e 61  !=pOp->p2 || u.a
26453 77 2e 69 47 65 6e 21 3d 70 4f 70 2d 3e 70 33 20  w.iGen!=pOp->p3 
26454 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ){..    sqlite3D
26455 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
26456 72 4d 73 67 29 3b 0d 0a 20 20 20 20 70 2d 3e 7a  rMsg);..    p->z
26457 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
26458 44 62 53 74 72 44 75 70 28 64 62 2c 20 22 64 61  DbStrDup(db, "da
26459 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
2645a 73 20 63 68 61 6e 67 65 64 22 29 3b 0d 0a 20 20  s changed");..  
2645b 20 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68 65    /* If the sche
2645c 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74  ma-cookie from t
2645d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
2645e 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6f   matches the coo
2645f 6b 69 65 0d 0a 20 20 20 20 2a 2a 20 73 74 6f 72  kie..    ** stor
26460 65 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d 6d  ed with the in-m
26461 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
26462 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 65  tion of the sche
26463 6d 61 2c 20 64 6f 0d 0a 20 20 20 20 2a 2a 20 6e  ma, do..    ** n
26464 6f 74 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63  ot reload the sc
26465 68 65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61  hema from the da
26466 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 20 20  tabase file...  
26467 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 49 66 20    **..    ** If 
26468 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73 20 61  virtual-tables a
26469 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69 73 20  re in use, this 
2646a 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f  is not just an o
2646b 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0d 0a 20 20  ptimization...  
2646c 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 61    ** Often, v-ta
2646d 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69 72  bles store their
2646e 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20 53   data in other S
2646f 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77 68  QLite tables, wh
26470 69 63 68 0d 0a 20 20 20 20 2a 2a 20 61 72 65 20  ich..    ** are 
26471 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74  queried from wit
26472 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20  hin xNext() and 
26473 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65  other v-table me
26474 74 68 6f 64 73 20 75 73 69 6e 67 0d 0a 20 20 20  thods using..   
26475 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65   ** prepared que
26476 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20  ries. If such a 
26477 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d  query is out-of-
26478 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20  date, we do not 
26479 77 61 6e 74 20 74 6f 0d 0a 20 20 20 20 2a 2a 20  want to..    ** 
2647a 64 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61  discard the data
2647b 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20  base schema, as 
2647c 74 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d  the user code im
2647d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0d 0a  plementing the..
2647e 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77      ** v-table w
2647f 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20  ould have to be 
26480 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 73 71  ready for the sq
26481 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
26482 74 75 72 65 20 69 74 73 65 6c 66 0d 0a 20 20 20  ture itself..   
26483 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69   ** to be invali
26484 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 73  dated whenever s
26485 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73  qlite3_step() is
26486 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
26487 68 69 6e 0d 0a 20 20 20 20 2a 2a 20 61 20 76 2d  hin..    ** a v-
26488 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0d 0a 20  table method... 
26489 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 64     */..    if( d
2648a 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e  b->aDb[pOp->p1].
2648b 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
2648c 63 6f 6f 6b 69 65 21 3d 75 2e 61 77 2e 69 4d 65  cookie!=u.aw.iMe
2648d 74 61 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  ta ){..      sql
2648e 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2648f 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d  lSchema(db, pOp-
26490 3e 70 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  >p1);..    }....
26491 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
26492 20 31 3b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51   1;..    rc = SQ
26493 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0d 0a 20 20  LITE_SCHEMA;..  
26494 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  }..  break;..}..
26495 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65  ../* Opcode: Ope
26496 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 20 50  nRead P1 P2 P3 P
26497 34 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 70 65  4 P5..**..** Ope
26498 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75  n a read-only cu
26499 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74  rsor for the dat
2649a 61 62 61 73 65 20 74 61 62 6c 65 20 77 68 6f 73  abase table whos
2649b 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 0d 0a  e root page is..
2649c 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 74 61 62  ** P2 in a datab
2649d 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 64  ase file.  The d
2649e 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
2649f 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 50 33  determined by P3
264a0 2e 20 0d 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61  . ..** P3==0 mea
264a1 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ns the main data
264a2 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e  base, P3==1 mean
264a3 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75  s the database u
264a4 73 65 64 20 66 6f 72 20 0d 0a 2a 2a 20 74 65 6d  sed for ..** tem
264a5 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61  porary tables, a
264a6 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 73  nd P3>1 means us
264a7 65 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ed the correspon
264a8 64 69 6e 67 20 61 74 74 61 63 68 65 64 0d 0a 2a  ding attached..*
264a9 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 69 76  * database.  Giv
264aa 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72  e the new cursor
264ab 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f   an identifier o
264ac 66 20 50 31 2e 20 20 54 68 65 20 50 31 0d 0a 2a  f P1.  The P1..*
264ad 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f  * values need no
264ae 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 20  t be contiguous 
264af 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 65  but all P1 value
264b0 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c  s should be smal
264b1 6c 20 69 6e 74 65 67 65 72 73 2e 0d 0a 2a 2a 20  l integers...** 
264b2 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66  It is an error f
264b3 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61  or P1 to be nega
264b4 74 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  tive...**..** If
264b5 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20   P5!=0 then use 
264b6 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
264b7 65 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68  egister P2 as th
264b8 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74  e root page, not
264b9 0d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  ..** the value o
264ba 66 20 50 32 20 69 74 73 65 6c 66 2e 0d 0a 2a 2a  f P2 itself...**
264bb 0d 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20  ..** There will 
264bc 62 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f  be a read lock o
264bd 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  n the database w
264be 68 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73  henever there is
264bf 20 61 6e 0d 0a 2a 2a 20 6f 70 65 6e 20 63 75 72   an..** open cur
264c0 73 6f 72 2e 20 20 49 66 20 74 68 65 20 64 61 74  sor.  If the dat
264c1 61 62 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b  abase was unlock
264c2 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73  ed prior to this
264c3 20 69 6e 73 74 72 75 63 74 69 6f 6e 0d 0a 2a 2a   instruction..**
264c4 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c 6f 63   then a read loc
264c5 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 61 73  k is acquired as
264c6 20 70 61 72 74 20 6f 66 20 74 68 69 73 20 69 6e   part of this in
264c7 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 72 65  struction.  A re
264c8 61 64 0d 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f  ad..** lock allo
264c9 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ws other process
264ca 65 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 64  es to read the d
264cb 61 74 61 62 61 73 65 20 62 75 74 20 70 72 6f 68  atabase but proh
264cc 69 62 69 74 73 0d 0a 2a 2a 20 61 6e 79 20 6f 74  ibits..** any ot
264cd 68 65 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d  her process from
264ce 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64   modifying the d
264cf 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 72 65  atabase.  The re
264d0 61 64 20 6c 6f 63 6b 20 69 73 0d 0a 2a 2a 20 72  ad lock is..** r
264d1 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 6c 6c  eleased when all
264d2 20 63 75 72 73 6f 72 73 20 61 72 65 20 63 6c 6f   cursors are clo
264d3 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 69 6e  sed.  If this in
264d4 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65 6d 70  struction attemp
264d5 74 73 0d 0a 2a 2a 20 74 6f 20 67 65 74 20 61 20  ts..** to get a 
264d6 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 61  read lock but fa
264d7 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74 20  ils, the script 
264d8 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68 20  terminates with 
264d9 61 6e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  an..** SQLITE_BU
264da 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a  SY error code...
264db 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 34 20 76 61  **..** The P4 va
264dc 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 65  lue may be eithe
264dd 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34  r an integer (P4
264de 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69  _INT32) or a poi
264df 6e 74 65 72 20 74 6f 0d 0a 2a 2a 20 61 20 4b 65  nter to..** a Ke
264e0 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
264e1 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66  (P4_KEYINFO). If
264e2 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72   it is a pointer
264e3 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0d 0a   to a KeyInfo ..
264e4 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  ** structure, th
264e5 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72  en said structur
264e6 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f  e defines the co
264e7 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74  ntent and collat
264e8 69 6e 67 20 0d 0a 2a 2a 20 73 65 71 75 65 6e 63  ing ..** sequenc
264e9 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62  e of the index b
264ea 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68  eing opened. Oth
264eb 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73  erwise, if P4 is
264ec 20 61 6e 20 69 6e 74 65 67 65 72 20 0d 0a 2a 2a   an integer ..**
264ed 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65   value, it is se
264ee 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
264ef 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
264f0 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  e table...**..**
264f1 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72   See also OpenWr
264f2 69 74 65 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63  ite...*/../* Opc
264f3 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 50  ode: OpenWrite P
264f4 31 20 50 32 20 50 33 20 50 34 20 50 35 0d 0a 2a  1 P2 P3 P4 P5..*
264f5 2a 0d 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61  *..** Open a rea
264f6 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 6e  d/write cursor n
264f7 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 74  amed P1 on the t
264f8 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68  able or index wh
264f9 6f 73 65 20 72 6f 6f 74 0d 0a 2a 2a 20 70 61 67  ose root..** pag
264fa 65 20 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20  e is P2.  Or if 
264fb 50 35 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f  P5!=0 use the co
264fc 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
264fd 72 20 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65  r P2 to find the
264fe 0d 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0d  ..** root page..
264ff 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 34 20 76  .**..** The P4 v
26500 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68  alue may be eith
26501 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50  er an integer (P
26502 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f  4_INT32) or a po
26503 69 6e 74 65 72 20 74 6f 0d 0a 2a 2a 20 61 20 4b  inter to..** a K
26504 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
26505 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49   (P4_KEYINFO). I
26506 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65  f it is a pointe
26507 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0d  r to a KeyInfo .
26508 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74  .** structure, t
26509 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75  hen said structu
2650a 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63  re defines the c
2650b 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61  ontent and colla
2650c 74 69 6e 67 20 0d 0a 2a 2a 20 73 65 71 75 65 6e  ting ..** sequen
2650d 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ce of the index 
2650e 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74  being opened. Ot
2650f 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69  herwise, if P4 i
26510 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0d 0a 2a  s an integer ..*
26511 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73  * value, it is s
26512 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
26513 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
26514 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f 20  he table, or to 
26515 74 68 65 0d 0a 2a 2a 20 6c 61 72 67 65 73 74 20  the..** largest 
26516 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c  index of any col
26517 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  umn of the table
26518 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c   that is actuall
26519 79 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  y used...**..** 
2651a 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
2651b 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65   works just like
2651c 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 74   OpenRead except
2651d 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74   that it opens t
2651e 68 65 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 69 6e  he cursor..** in
2651f 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65   read/write mode
26520 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74  .  For a given t
26521 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20  able, there can 
26522 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72  be one or more r
26523 65 61 64 2d 6f 6e 6c 79 0d 0a 2a 2a 20 63 75 72  ead-only..** cur
26524 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65  sors or a single
26525 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
26526 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e  or but not both.
26527 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73  ..**..** See als
26528 6f 20 4f 70 65 6e 52 65 61 64 2e 0d 0a 2a 2f 0d  o OpenRead...*/.
26529 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 52 65 61  .case OP_OpenRea
2652a 64 3a 0d 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  d:..case OP_Open
2652b 57 72 69 74 65 3a 20 7b 0d 0a 23 69 66 20 30 20  Write: {..#if 0 
2652c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
2652d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
2652e 2e 61 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46  .ax */..  int nF
2652f 69 65 6c 64 3b 0d 0a 20 20 4b 65 79 49 6e 66 6f  ield;..  KeyInfo
26530 20 2a 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 69   *pKeyInfo;..  i
26531 6e 74 20 70 32 3b 0d 0a 20 20 69 6e 74 20 69 44  nt p2;..  int iD
26532 62 3b 0d 0a 20 20 69 6e 74 20 77 72 46 6c 61 67  b;..  int wrFlag
26533 3b 0d 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0d  ;..  Btree *pX;.
26534 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
26535 43 75 72 3b 0d 0a 20 20 44 62 20 2a 70 44 62 3b  Cur;..  Db *pDb;
26536 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ..#endif /* loca
26537 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
26538 64 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0d 0a  d into u.ax */..
26539 0d 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 69 72  ..  if( p->expir
2653a 65 64 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  ed ){..    rc = 
2653b 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0d 0a 20  SQLITE_ABORT;.. 
2653c 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a     break;..  }..
2653d 0d 0a 20 20 75 2e 61 78 2e 6e 46 69 65 6c 64 20  ..  u.ax.nField 
2653e 3d 20 30 3b 0d 0a 20 20 75 2e 61 78 2e 70 4b 65  = 0;..  u.ax.pKe
2653f 79 49 6e 66 6f 20 3d 20 30 3b 0d 0a 20 20 75 2e  yInfo = 0;..  u.
26540 61 78 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b  ax.p2 = pOp->p2;
26541 0d 0a 20 20 75 2e 61 78 2e 69 44 62 20 3d 20 70  ..  u.ax.iDb = p
26542 4f 70 2d 3e 70 33 3b 0d 0a 20 20 61 73 73 65 72  Op->p3;..  asser
26543 74 28 20 75 2e 61 78 2e 69 44 62 3e 3d 30 20 26  t( u.ax.iDb>=0 &
26544 26 20 75 2e 61 78 2e 69 44 62 3c 64 62 2d 3e 6e  & u.ax.iDb<db->n
26545 44 62 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  Db );..  assert(
26546 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
26547 20 28 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c   (((yDbMask)1)<<
26548 75 2e 61 78 2e 69 44 62 29 29 21 3d 30 20 29 3b  u.ax.iDb))!=0 );
26549 0d 0a 20 20 75 2e 61 78 2e 70 44 62 20 3d 20 26  ..  u.ax.pDb = &
2654a 64 62 2d 3e 61 44 62 5b 75 2e 61 78 2e 69 44 62  db->aDb[u.ax.iDb
2654b 5d 3b 0d 0a 20 20 75 2e 61 78 2e 70 58 20 3d 20  ];..  u.ax.pX = 
2654c 75 2e 61 78 2e 70 44 62 2d 3e 70 42 74 3b 0d 0a  u.ax.pDb->pBt;..
2654d 20 20 61 73 73 65 72 74 28 20 75 2e 61 78 2e 70    assert( u.ax.p
2654e 58 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70  X!=0 );..  if( p
2654f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f  Op->opcode==OP_O
26550 70 65 6e 57 72 69 74 65 20 29 7b 0d 0a 20 20 20  penWrite ){..   
26551 20 75 2e 61 78 2e 77 72 46 6c 61 67 20 3d 20 31   u.ax.wrFlag = 1
26552 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ;..    assert( s
26553 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65  qlite3SchemaMute
26554 78 48 65 6c 64 28 64 62 2c 20 75 2e 61 78 2e 69  xHeld(db, u.ax.i
26555 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20 20 20 69  Db, 0) );..    i
26556 66 28 20 75 2e 61 78 2e 70 44 62 2d 3e 70 53 63  f( u.ax.pDb->pSc
26557 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
26558 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  t < p->minWriteF
26559 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0d 0a 20 20  ileFormat ){..  
2655a 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46      p->minWriteF
2655b 69 6c 65 46 6f 72 6d 61 74 20 3d 20 75 2e 61 78  ileFormat = u.ax
2655c 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66  .pDb->pSchema->f
2655d 69 6c 65 5f 66 6f 72 6d 61 74 3b 0d 0a 20 20 20  ile_format;..   
2655e 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20   }..  }else{..  
2655f 20 20 75 2e 61 78 2e 77 72 46 6c 61 67 20 3d 20    u.ax.wrFlag = 
26560 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  0;..  }..  if( p
26561 4f 70 2d 3e 70 35 20 29 7b 0d 0a 20 20 20 20 61  Op->p5 ){..    a
26562 73 73 65 72 74 28 20 75 2e 61 78 2e 70 32 3e 30  ssert( u.ax.p2>0
26563 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
26564 20 75 2e 61 78 2e 70 32 3c 3d 70 2d 3e 6e 4d 65   u.ax.p2<=p->nMe
26565 6d 20 29 3b 0d 0a 20 20 20 20 70 49 6e 32 20 3d  m );..    pIn2 =
26566 20 26 61 4d 65 6d 5b 75 2e 61 78 2e 70 32 5d 3b   &aMem[u.ax.p2];
26567 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ..    assert( me
26568 6d 49 73 56 61 6c 69 64 28 70 49 6e 32 29 20 29  mIsValid(pIn2) )
26569 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ;..    assert( (
2656a 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn2->flags & ME
2656b 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0d 0a 20 20  M_Int)!=0 );..  
2656c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
2656d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29  Integerify(pIn2)
2656e 3b 0d 0a 20 20 20 20 75 2e 61 78 2e 70 32 20 3d  ;..    u.ax.p2 =
2656f 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b   (int)pIn2->u.i;
26570 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61  ..    /* The u.a
26571 78 2e 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79  x.p2 value alway
26572 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70  s comes from a p
26573 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61  rior OP_CreateTa
26574 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e 64 0d 0a  ble opcode and..
26575 20 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f      ** that opco
26576 64 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73  de will always s
26577 65 74 20 74 68 65 20 75 2e 61 78 2e 70 32 20 76  et the u.ax.p2 v
26578 61 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72  alue to 2 or mor
26579 65 20 6f 72 20 65 6c 73 65 20 66 61 69 6c 2e 0d  e or else fail..
2657a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65  .    ** If there
2657b 20 77 65 72 65 20 61 20 66 61 69 6c 75 72 65 2c   were a failure,
2657c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
2657d 61 74 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 61  atement would ha
2657e 76 65 20 68 61 6c 74 65 64 0d 0a 20 20 20 20 2a  ve halted..    *
2657f 2a 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e  * before reachin
26580 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  g this instructi
26581 6f 6e 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  on. */..    if( 
26582 4e 45 56 45 52 28 75 2e 61 78 2e 70 32 3c 32 29  NEVER(u.ax.p2<2)
26583 20 29 20 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d   ) {..      rc =
26584 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
26585 42 4b 50 54 3b 0d 0a 20 20 20 20 20 20 67 6f 74  BKPT;..      got
26586 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
26587 72 72 6f 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rror;..    }..  
26588 7d 0d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  }..  if( pOp->p4
26589 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  type==P4_KEYINFO
2658a 20 29 7b 0d 0a 20 20 20 20 75 2e 61 78 2e 70 4b   ){..    u.ax.pK
2658b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34  eyInfo = pOp->p4
2658c 2e 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 20 20  .pKeyInfo;..    
2658d 75 2e 61 78 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65  u.ax.pKeyInfo->e
2658e 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b  nc = ENC(p->db);
2658f 0d 0a 20 20 20 20 75 2e 61 78 2e 6e 46 69 65 6c  ..    u.ax.nFiel
26590 64 20 3d 20 75 2e 61 78 2e 70 4b 65 79 49 6e 66  d = u.ax.pKeyInf
26591 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0d 0a 20 20  o->nField+1;..  
26592 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
26593 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
26594 29 7b 0d 0a 20 20 20 20 75 2e 61 78 2e 6e 46 69  ){..    u.ax.nFi
26595 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b  eld = pOp->p4.i;
26596 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
26597 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0d 0a   pOp->p1>=0 );..
26598 20 20 75 2e 61 78 2e 70 43 75 72 20 3d 20 61 6c    u.ax.pCur = al
26599 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
2659a 70 4f 70 2d 3e 70 31 2c 20 75 2e 61 78 2e 6e 46  pOp->p1, u.ax.nF
2659b 69 65 6c 64 2c 20 75 2e 61 78 2e 69 44 62 2c 20  ield, u.ax.iDb, 
2659c 31 29 3b 0d 0a 20 20 69 66 28 20 75 2e 61 78 2e  1);..  if( u.ax.
2659d 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  pCur==0 ) goto n
2659e 6f 5f 6d 65 6d 3b 0d 0a 20 20 75 2e 61 78 2e 70  o_mem;..  u.ax.p
2659f 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  Cur->nullRow = 1
265a0 3b 0d 0a 20 20 75 2e 61 78 2e 70 43 75 72 2d 3e  ;..  u.ax.pCur->
265a1 69 73 4f 72 64 65 72 65 64 20 3d 20 31 3b 0d 0a  isOrdered = 1;..
265a2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
265a3 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 2e 70  reeCursor(u.ax.p
265a4 58 2c 20 75 2e 61 78 2e 70 32 2c 20 75 2e 61 78  X, u.ax.p2, u.ax
265a5 2e 77 72 46 6c 61 67 2c 20 75 2e 61 78 2e 70 4b  .wrFlag, u.ax.pK
265a6 65 79 49 6e 66 6f 2c 20 75 2e 61 78 2e 70 43 75  eyInfo, u.ax.pCu
265a7 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20  r->pCursor);..  
265a8 75 2e 61 78 2e 70 43 75 72 2d 3e 70 4b 65 79 49  u.ax.pCur->pKeyI
265a9 6e 66 6f 20 3d 20 75 2e 61 78 2e 70 4b 65 79 49  nfo = u.ax.pKeyI
265aa 6e 66 6f 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 69 6e  nfo;....  /* Sin
265ab 63 65 20 69 74 20 70 65 72 66 6f 72 6d 73 20 6e  ce it performs n
265ac 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  o memory allocat
265ad 69 6f 6e 20 6f 72 20 49 4f 2c 20 74 68 65 20 6f  ion or IO, the o
265ae 6e 6c 79 20 76 61 6c 75 65 20 74 68 61 74 0d 0a  nly value that..
265af 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
265b0 65 43 75 72 73 6f 72 28 29 20 6d 61 79 20 72 65  eCursor() may re
265b1 74 75 72 6e 20 69 73 20 53 51 4c 49 54 45 5f 4f  turn is SQLITE_O
265b2 4b 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  K. */..  assert(
265b3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
265b4 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 65 74 20 74 68  ;....  /* Set th
265b5 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54  e VdbeCursor.isT
265b6 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 65 78  able and isIndex
265b7 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72 65 76   variables. Prev
265b8 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66  ious versions of
265b9 0d 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 73  ..  ** SQLite us
265ba 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74  ed to check if t
265bb 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c 61  he root-page fla
265bc 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74 20  gs were sane at 
265bd 74 68 69 73 20 70 6f 69 6e 74 0d 0a 20 20 2a 2a  this point..  **
265be 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61   and report data
265bf 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
265c0 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74  if they were not
265c1 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b  , but this check
265c2 20 68 61 73 0d 0a 20 20 2a 2a 20 73 69 6e 63 65   has..  ** since
265c3 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20   moved into the 
265c4 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f  btree layer.  */
265c5 0d 0a 20 20 75 2e 61 78 2e 70 43 75 72 2d 3e 69  ..  u.ax.pCur->i
265c6 73 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34  sTable = pOp->p4
265c7 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f  type!=P4_KEYINFO
265c8 3b 0d 0a 20 20 75 2e 61 78 2e 70 43 75 72 2d 3e  ;..  u.ax.pCur->
265c9 69 73 49 6e 64 65 78 20 3d 20 21 75 2e 61 78 2e  isIndex = !u.ax.
265ca 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 3b 0d 0a  pCur->isTable;..
265cb 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
265cc 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70  * Opcode: OpenEp
265cd 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20 2a 20  hemeral P1 P2 * 
265ce 50 34 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 70  P4 P5..**..** Op
265cf 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20  en a new cursor 
265d0 50 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e  P1 to a transien
265d1 74 20 74 61 62 6c 65 2e 0d 0a 2a 2a 20 54 68 65  t table...** The
265d2 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79   cursor is alway
265d3 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72  s opened read/wr
265d4 69 74 65 20 65 76 65 6e 20 69 66 20 0d 0a 2a 2a  ite even if ..**
265d5 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
265d6 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e  se is read-only.
265d7 20 20 54 68 65 20 65 70 68 65 6d 65 72 61 6c 0d    The ephemeral.
265d8 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c  .** table is del
265d9 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  eted automatical
265da 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 73  ly when the curs
265db 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0d 0a 2a  or is closed...*
265dc 2a 0d 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20  *..** P2 is the 
265dd 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
265de 73 20 69 6e 20 74 68 65 20 65 70 68 65 6d 65 72  s in the ephemer
265df 61 6c 20 74 61 62 6c 65 2e 0d 0a 2a 2a 20 54 68  al table...** Th
265e0 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20  e cursor points 
265e1 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 6c 65  to a BTree table
265e2 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f   if P4==0 and to
265e3 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 0d 0a   a BTree index..
265e4 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 20  ** if P4 is not 
265e5 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74  0.  If P4 is not
265e6 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73   NULL, it points
265e7 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
265e8 72 75 63 74 75 72 65 0d 0a 2a 2a 20 74 68 61 74  ructure..** that
265e9 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72   defines the for
265ea 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74  mat of keys in t
265eb 68 65 20 69 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a 2a  he index...**..*
265ec 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61  * This opcode wa
265ed 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70  s once called Op
265ee 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61  enTemp.  But tha
265ef 74 20 63 72 65 61 74 65 64 0d 0a 2a 2a 20 63 6f  t created..** co
265f0 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65 20  nfusion because 
265f1 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20 74  the term "temp t
265f2 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65 66  able", might ref
265f3 65 72 20 65 69 74 68 65 72 0d 0a 2a 2a 20 74 6f  er either..** to
265f4 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74   a TEMP table at
265f5 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20   the SQL level, 
265f6 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70  or to a table op
265f7 65 6e 65 64 20 62 79 0d 0a 2a 2a 20 74 68 69 73  ened by..** this
265f8 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74   opcode.  Then t
265f9 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63  his opcode was c
265fa 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e  all OpenVirtual.
265fb 20 20 42 75 74 0d 0a 2a 2a 20 74 68 61 74 20 63    But..** that c
265fc 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e  reated confusion
265fd 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20   with the whole 
265fe 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64  virtual-table id
265ff 65 61 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ea...**..** The 
26600 50 35 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e  P5 parameter can
26601 20 62 65 20 61 20 6d 61 73 6b 20 6f 66 20 74 68   be a mask of th
26602 65 20 42 54 52 45 45 5f 2a 20 66 6c 61 67 73 20  e BTREE_* flags 
26603 64 65 66 69 6e 65 64 0d 0a 2a 2a 20 69 6e 20 62  defined..** in b
26604 74 72 65 65 2e 68 2e 20 20 54 68 65 73 65 20 66  tree.h.  These f
26605 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 20 61 73 70  lags control asp
26606 65 63 74 73 20 6f 66 20 74 68 65 20 6f 70 65 72  ects of the oper
26607 61 74 69 6f 6e 20 6f 66 0d 0a 2a 2a 20 74 68 65  ation of..** the
26608 20 62 74 72 65 65 2e 20 20 54 68 65 20 42 54 52   btree.  The BTR
26609 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20  EE_OMIT_JOURNAL 
2660a 61 6e 64 20 42 54 52 45 45 5f 53 49 4e 47 4c 45  and BTREE_SINGLE
2660b 20 66 6c 61 67 73 20 61 72 65 0d 0a 2a 2a 20 61   flags are..** a
2660c 64 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  dded automatical
2660d 6c 79 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f  ly...*/../* Opco
2660e 64 65 3a 20 4f 70 65 6e 41 75 74 6f 69 6e 64 65  de: OpenAutoinde
2660f 78 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0d 0a  x P1 P2 * P4 *..
26610 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  **..** This opco
26611 64 65 20 77 6f 72 6b 73 20 74 68 65 20 73 61 6d  de works the sam
26612 65 20 61 73 20 4f 50 5f 4f 70 65 6e 45 70 68 65  e as OP_OpenEphe
26613 6d 65 72 61 6c 2e 20 20 49 74 20 68 61 73 20 61  meral.  It has a
26614 0d 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 6e  ..** different n
26615 61 6d 65 20 74 6f 20 64 69 73 74 69 6e 67 75 69  ame to distingui
26616 73 68 20 69 74 73 20 75 73 65 2e 20 20 54 61 62  sh its use.  Tab
26617 6c 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  les created usin
26618 67 0d 0a 2a 2a 20 62 79 20 74 68 69 73 20 6f 70  g..** by this op
26619 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 73 65  code will be use
2661a 64 20 66 6f 72 20 61 75 74 6f 6d 61 74 69 63 61  d for automatica
2661b 6c 6c 79 20 63 72 65 61 74 65 64 20 74 72 61 6e  lly created tran
2661c 73 69 65 6e 74 0d 0a 2a 2a 20 69 6e 64 69 63 65  sient..** indice
2661d 73 20 69 6e 20 6a 6f 69 6e 73 2e 0d 0a 2a 2f 0d  s in joins...*/.
2661e 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 41 75 74  .case OP_OpenAut
2661f 6f 69 6e 64 65 78 3a 20 0d 0a 63 61 73 65 20 4f  oindex: ..case O
26620 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a  P_OpenEphemeral:
26621 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   {..#if 0  /* lo
26622 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
26623 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f  ved into u.ay */
26624 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ..  VdbeCursor *
26625 70 43 78 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  pCx;..#endif /* 
26626 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
26627 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20  moved into u.ay 
26628 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  */..  static con
26629 73 74 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  st int vfsFlags 
2662a 3d 0d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  =..      SQLITE_
2662b 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
2662c 0d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  ..      SQLITE_O
2662d 50 45 4e 5f 43 52 45 41 54 45 20 7c 0d 0a 20 20  PEN_CREATE |..  
2662e 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
2662f 45 58 43 4c 55 53 49 56 45 20 7c 0d 0a 20 20 20  EXCLUSIVE |..   
26630 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44     SQLITE_OPEN_D
26631 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0d 0a  ELETEONCLOSE |..
26632 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
26633 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b 0d  N_TRANSIENT_DB;.
26634 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ...  assert( pOp
26635 2d 3e 70 31 3e 3d 30 20 29 3b 0d 0a 20 20 75 2e  ->p1>=0 );..  u.
26636 61 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74  ay.pCx = allocat
26637 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e  eCursor(p, pOp->
26638 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c  p1, pOp->p2, -1,
26639 20 31 29 3b 0d 0a 20 20 69 66 28 20 75 2e 61 79   1);..  if( u.ay
2663a 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  .pCx==0 ) goto n
2663b 6f 5f 6d 65 6d 3b 0d 0a 20 20 75 2e 61 79 2e 70  o_mem;..  u.ay.p
2663c 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  Cx->nullRow = 1;
2663d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2663e 42 74 72 65 65 4f 70 65 6e 28 64 62 2d 3e 70 56  BtreeOpen(db->pV
2663f 66 73 2c 20 30 2c 20 64 62 2c 20 26 75 2e 61 79  fs, 0, db, &u.ay
26640 2e 70 43 78 2d 3e 70 42 74 2c 0d 0a 20 20 20 20  .pCx->pBt,..    
26641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26642 20 20 20 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a      BTREE_OMIT_J
26643 4f 55 52 4e 41 4c 20 7c 20 42 54 52 45 45 5f 53  OURNAL | BTREE_S
26644 49 4e 47 4c 45 20 7c 20 70 4f 70 2d 3e 70 35 2c  INGLE | pOp->p5,
26645 20 76 66 73 46 6c 61 67 73 29 3b 0d 0a 20 20 69   vfsFlags);..  i
26646 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
26647 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   ){..    rc = sq
26648 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
26649 72 61 6e 73 28 75 2e 61 79 2e 70 43 78 2d 3e 70  rans(u.ay.pCx->p
2664a 42 74 2c 20 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20  Bt, 1);..  }..  
2664b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2664c 4b 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20  K ){..    /* If 
2664d 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65  a transient inde
2664e 78 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 63  x is required, c
2664f 72 65 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c  reate it by call
26650 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69  ing..    ** sqli
26651 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
26652 62 6c 65 28 29 20 77 69 74 68 20 74 68 65 20 42  ble() with the B
26653 54 52 45 45 5f 42 4c 4f 42 4b 45 59 20 66 6c 61  TREE_BLOBKEY fla
26654 67 20 62 65 66 6f 72 65 0d 0a 20 20 20 20 2a 2a  g before..    **
26655 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 20   opening it. If 
26656 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c  a transient tabl
26657 65 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 6a  e is required, j
26658 75 73 74 20 75 73 65 20 74 68 65 0d 0a 20 20 20  ust use the..   
26659 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   ** automaticall
2665a 79 20 63 72 65 61 74 65 64 20 74 61 62 6c 65 20  y created table 
2665b 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 31  with root-page 1
2665c 20 28 61 6e 20 42 4c 4f 42 5f 49 4e 54 4b 45 59   (an BLOB_INTKEY
2665d 20 74 61 62 6c 65 29 2e 0d 0a 20 20 20 20 2a 2f   table)...    */
2665e 0d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ..    if( pOp->p
2665f 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0d 0a 20  4.pKeyInfo ){.. 
26660 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0d 0a       int pgno;..
26661 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
26662 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45  p->p4type==P4_KE
26663 59 49 4e 46 4f 20 29 3b 0d 0a 20 20 20 20 20 20  YINFO );..      
26664 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
26665 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e 61  eCreateTable(u.a
26666 79 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e  y.pCx->pBt, &pgn
26667 6f 2c 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59  o, BTREE_BLOBKEY
26668 20 7c 20 70 4f 70 2d 3e 70 35 29 3b 0d 0a 20 20   | pOp->p5);..  
26669 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2666a 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
2666b 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d    assert( pgno==
2666c 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b  MASTER_ROOT+1 );
2666d 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ..        rc = s
2666e 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
2666f 72 28 75 2e 61 79 2e 70 43 78 2d 3e 70 42 74 2c  r(u.ay.pCx->pBt,
26670 20 70 67 6e 6f 2c 20 31 2c 0d 0a 20 20 20 20 20   pgno, 1,..     
26671 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26672 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 79 49             (KeyI
26673 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20  nfo*)pOp->p4.z, 
26674 75 2e 61 79 2e 70 43 78 2d 3e 70 43 75 72 73 6f  u.ay.pCx->pCurso
26675 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 75 2e 61  r);..        u.a
26676 79 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20  y.pCx->pKeyInfo 
26677 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e  = pOp->p4.pKeyIn
26678 66 6f 3b 0d 0a 20 20 20 20 20 20 20 20 75 2e 61  fo;..        u.a
26679 79 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d  y.pCx->pKeyInfo-
2667a 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62  >enc = ENC(p->db
2667b 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
2667c 20 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54     u.ay.pCx->isT
2667d 61 62 6c 65 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  able = 0;..    }
2667e 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20  else{..      rc 
2667f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
26680 72 73 6f 72 28 75 2e 61 79 2e 70 43 78 2d 3e 70  rsor(u.ay.pCx->p
26681 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  Bt, MASTER_ROOT,
26682 20 31 2c 20 30 2c 20 75 2e 61 79 2e 70 43 78 2d   1, 0, u.ay.pCx-
26683 3e 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20  >pCursor);..    
26684 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61    u.ay.pCx->isTa
26685 62 6c 65 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  ble = 1;..    }.
26686 0a 20 20 7d 0d 0a 20 20 75 2e 61 79 2e 70 43 78  .  }..  u.ay.pCx
26687 2d 3e 69 73 4f 72 64 65 72 65 64 20 3d 20 28 70  ->isOrdered = (p
26688 4f 70 2d 3e 70 35 21 3d 42 54 52 45 45 5f 55 4e  Op->p5!=BTREE_UN
26689 4f 52 44 45 52 45 44 29 3b 0d 0a 20 20 75 2e 61  ORDERED);..  u.a
2668a 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d  y.pCx->isIndex =
2668b 20 21 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61   !u.ay.pCx->isTa
2668c 62 6c 65 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  ble;..  break;..
2668d 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
2668e 4f 70 65 6e 53 6f 72 74 65 72 20 50 31 20 50 32  OpenSorter P1 P2
2668f 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20   * P4 *..**..** 
26690 54 68 69 73 20 6f 70 63 6f 64 65 20 77 6f 72 6b  This opcode work
26691 73 20 6c 69 6b 65 20 4f 50 5f 4f 70 65 6e 45 70  s like OP_OpenEp
26692 68 65 6d 65 72 61 6c 20 65 78 63 65 70 74 20 74  hemeral except t
26693 68 61 74 20 69 74 20 6f 70 65 6e 73 0d 0a 2a 2a  hat it opens..**
26694 20 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64   a transient ind
26695 65 78 20 74 68 61 74 20 69 73 20 73 70 65 63 69  ex that is speci
26696 66 69 63 61 6c 6c 79 20 64 65 73 69 67 6e 65 64  fically designed
26697 20 74 6f 20 73 6f 72 74 20 6c 61 72 67 65 0d 0a   to sort large..
26698 2a 2a 20 74 61 62 6c 65 73 20 75 73 69 6e 67 20  ** tables using 
26699 61 6e 20 65 78 74 65 72 6e 61 6c 20 6d 65 72 67  an external merg
2669a 65 2d 73 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d  e-sort algorithm
2669b 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 53  ...*/..case OP_S
2669c 6f 72 74 65 72 4f 70 65 6e 3a 20 7b 0d 0a 23 69  orterOpen: {..#i
2669d 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
2669e 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2669f 74 6f 20 75 2e 61 7a 20 2a 2f 0d 0a 20 20 56 64  to u.az */..  Vd
266a0 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0d 0a  beCursor *pCx;..
266a1 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
266a2 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
266a3 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0d 0a 23 69  into u.az */..#i
266a4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
266a5 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0d 0a 20 20  T_MERGE_SORT..  
266a6 75 2e 61 7a 2e 70 43 78 20 3d 20 61 6c 6c 6f 63  u.az.pCx = alloc
266a7 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
266a8 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d  ->p1, pOp->p2, -
266a9 31 2c 20 31 29 3b 0d 0a 20 20 69 66 28 20 75 2e  1, 1);..  if( u.
266aa 61 7a 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f  az.pCx==0 ) goto
266ab 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 75 2e 61 7a   no_mem;..  u.az
266ac 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  .pCx->pKeyInfo =
266ad 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66   pOp->p4.pKeyInf
266ae 6f 3b 0d 0a 20 20 75 2e 61 7a 2e 70 43 78 2d 3e  o;..  u.az.pCx->
266af 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20  pKeyInfo->enc = 
266b0 45 4e 43 28 70 2d 3e 64 62 29 3b 0d 0a 20 20 75  ENC(p->db);..  u
266b1 2e 61 7a 2e 70 43 78 2d 3e 69 73 53 6f 72 74 65  .az.pCx->isSorte
266b2 72 20 3d 20 31 3b 0d 0a 20 20 72 63 20 3d 20 73  r = 1;..  rc = s
266b3 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72  qlite3VdbeSorter
266b4 49 6e 69 74 28 64 62 2c 20 75 2e 61 7a 2e 70 43  Init(db, u.az.pC
266b5 78 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 70 4f  x);..#else..  pO
266b6 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4f  p->opcode = OP_O
266b7 70 65 6e 45 70 68 65 6d 65 72 61 6c 3b 0d 0a 20  penEphemeral;.. 
266b8 20 70 63 2d 2d 3b 0d 0a 23 65 6e 64 69 66 0d 0a   pc--;..#endif..
266b9 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
266ba 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73  * Opcode: OpenPs
266bb 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20  eudo P1 P2 P3 * 
266bc 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 70 65 6e 20 61  *..**..** Open a
266bd 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 61 74   new cursor that
266be 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b   points to a fak
266bf 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e  e table that con
266c0 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0d 0a  tains a single..
266c1 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 20  ** row of data. 
266c2 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
266c3 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e 20  that one row in 
266c4 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6d  the content of m
266c5 65 6d 6f 72 79 0d 0a 2a 2a 20 72 65 67 69 73 74  emory..** regist
266c6 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 72  er P2.  In other
266c7 20 77 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 50   words, cursor P
266c8 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c 69  1 becomes an ali
266c9 61 73 20 66 6f 72 20 74 68 65 20 0d 0a 2a 2a 20  as for the ..** 
266ca 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74  MEM_Blob content
266cb 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 72 65   contained in re
266cc 67 69 73 74 65 72 20 50 32 2e 0d 0a 2a 2a 0d 0a  gister P2...**..
266cd 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c  ** A pseudo-tabl
266ce 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  e created by thi
266cf 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  s opcode is used
266d0 20 74 6f 20 68 6f 6c 64 20 61 20 73 69 6e 67 6c   to hold a singl
266d1 65 0d 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74  e..** row output
266d2 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72   from the sorter
266d3 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77   so that the row
266d4 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73   can be decompos
266d5 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 69 6e 64 69  ed into..** indi
266d6 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75  vidual columns u
266d7 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75  sing the OP_Colu
266d8 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20  mn opcode.  The 
266d9 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
266da 0d 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79  ..** is the only
266db 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74   cursor opcode t
266dc 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61  hat works with a
266dd 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0d 0a   pseudo-table...
266de 2a 2a 0d 0a 2a 2a 20 50 33 20 69 73 20 74 68 65  **..** P3 is the
266df 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
266e0 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73  s in the records
266e1 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74   that will be st
266e2 6f 72 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65 20  ored by..** the 
266e3 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0d 0a 2a  pseudo-table...*
266e4 2f 0d 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50  /..case OP_OpenP
266e5 73 65 75 64 6f 3a 20 7b 0d 0a 23 69 66 20 30 20  seudo: {..#if 0 
266e6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
266e7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
266e8 2e 62 61 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75  .ba */..  VdbeCu
266e9 72 73 6f 72 20 2a 70 43 78 3b 0d 0a 23 65 6e 64  rsor *pCx;..#end
266ea 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
266eb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
266ec 20 75 2e 62 61 20 2a 2f 0d 0a 0d 0a 20 20 61 73   u.ba */....  as
266ed 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
266ee 20 29 3b 0d 0a 20 20 75 2e 62 61 2e 70 43 78 20   );..  u.ba.pCx 
266ef 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
266f0 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  (p, pOp->p1, pOp
266f1 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 3b 0d 0a 20  ->p3, -1, 0);.. 
266f2 20 69 66 28 20 75 2e 62 61 2e 70 43 78 3d 3d 30   if( u.ba.pCx==0
266f3 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d   ) goto no_mem;.
266f4 0a 20 20 75 2e 62 61 2e 70 43 78 2d 3e 6e 75 6c  .  u.ba.pCx->nul
266f5 6c 52 6f 77 20 3d 20 31 3b 0d 0a 20 20 75 2e 62  lRow = 1;..  u.b
266f6 61 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 62  a.pCx->pseudoTab
266f7 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 3b  leReg = pOp->p2;
266f8 0d 0a 20 20 75 2e 62 61 2e 70 43 78 2d 3e 69 73  ..  u.ba.pCx->is
266f9 54 61 62 6c 65 20 3d 20 31 3b 0d 0a 20 20 75 2e  Table = 1;..  u.
266fa 62 61 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20  ba.pCx->isIndex 
266fb 3d 20 30 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  = 0;..  break;..
266fc 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
266fd 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a  Close P1 * * * *
266fe 0d 0a 2a 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20 61  ..**..** Close a
266ff 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
26700 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e  ly opened as P1.
26701 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0d 0a    If P1 is not..
26702 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  ** currently ope
26703 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  n, this instruct
26704 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d  ion is a no-op..
26705 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 43 6c 6f  .*/..case OP_Clo
26706 73 65 3a 20 7b 0d 0a 20 20 61 73 73 65 72 74 28  se: {..  assert(
26707 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
26708 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
26709 72 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  r );..  sqlite3V
2670a 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c  dbeFreeCursor(p,
2670b 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
2670c 31 5d 29 3b 0d 0a 20 20 70 2d 3e 61 70 43 73 72  1]);..  p->apCsr
2670d 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b 0d 0a  [pOp->p1] = 0;..
2670e 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
2670f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 65  * Opcode: SeekGe
26710 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0d 0a   P1 P2 P3 P4 *..
26711 2a 2a 0d 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  **..** If cursor
26712 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
26713 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
26714 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
26715 65 67 65 72 20 6b 65 79 73 29 2c 20 0d 0a 2a 2a  eger keys), ..**
26716 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
26717 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
26718 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75   the key.  If cu
26719 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0d  rsor P1 refers .
2671a 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
2671b 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
2671c 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
2671d 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
2671e 73 74 65 72 73 20 0d 0a 2a 2a 20 74 68 61 74 20  sters ..** that 
2671f 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
26720 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
26721 79 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 70 6f  y. ..**..** Repo
26722 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31  sition cursor P1
26723 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69   so that  it poi
26724 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c  nts to the small
26725 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0d  est entry that .
26726 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74  .** is greater t
26727 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
26728 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49  the key value. I
26729 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
2672a 65 63 6f 72 64 73 20 0d 0a 2a 2a 20 67 72 65 61  ecords ..** grea
2672b 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
2672c 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
2672d 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
2672e 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
2672f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c  ...**..** See al
26730 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f  so: Found, NotFo
26731 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53  und, Distinct, S
26732 65 65 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53  eekLt, SeekGt, S
26733 65 65 6b 4c 65 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70  eekLe..*/../* Op
26734 63 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 31 20  code: SeekGt P1 
26735 50 32 20 50 33 20 50 34 20 2a 0d 0a 2a 2a 0d 0a  P2 P3 P4 *..**..
26736 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  ** If cursor P1 
26737 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c  refers to an SQL
26738 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74   table (B-Tree t
26739 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72  hat uses integer
2673a 20 6b 65 79 73 29 2c 20 0d 0a 2a 2a 20 75 73 65   keys), ..** use
2673b 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
2673c 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b  gister P3 as a k
2673d 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31  ey. If cursor P1
2673e 20 72 65 66 65 72 73 20 0d 0a 2a 2a 20 74 6f 20   refers ..** to 
2673f 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
26740 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
26741 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
26742 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0d  f P4 registers .
26743 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65  .** that are use
26744 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64  d as an unpacked
26745 20 69 6e 64 65 78 20 6b 65 79 2e 20 0d 0a 2a 2a   index key. ..**
26746 0d 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20  ..** Reposition 
26747 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
26748 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  t  it points to 
26749 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74  the smallest ent
2674a 72 79 20 74 68 61 74 20 0d 0a 2a 2a 20 69 73 20  ry that ..** is 
2674b 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
2674c 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
2674d 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f  here are no reco
2674e 72 64 73 20 67 72 65 61 74 65 72 20 74 68 61 6e  rds greater than
2674f 20 0d 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e   ..** the key an
26750 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
26751 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
26752 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61  2...**..** See a
26753 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
26754 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
26755 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 2c 20  SeekLt, SeekGe, 
26756 53 65 65 6b 4c 65 0d 0a 2a 2f 0d 0a 2f 2a 20 4f  SeekLe..*/../* O
26757 70 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31  pcode: SeekLt P1
26758 20 50 32 20 50 33 20 50 34 20 2a 20 0d 0a 2a 2a   P2 P3 P4 * ..**
26759 0d 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  ..** If cursor P
2675a 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
2675b 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
2675c 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
2675d 65 72 20 6b 65 79 73 29 2c 20 0d 0a 2a 2a 20 75  er keys), ..** u
2675e 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  se the value in 
2675f 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61  register P3 as a
26760 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20   key. If cursor 
26761 50 31 20 72 65 66 65 72 73 20 0d 0a 2a 2a 20 74  P1 refers ..** t
26762 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
26763 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
26764 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
26765 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
26766 20 0d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75   ..** that are u
26767 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b  sed as an unpack
26768 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0d 0a  ed index key. ..
26769 2a 2a 0d 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  **..** Repositio
2676a 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
2676b 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
2676c 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  o the largest en
2676d 74 72 79 20 74 68 61 74 20 0d 0a 2a 2a 20 69 73  try that ..** is
2676e 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b   less than the k
2676f 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
26770 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
26771 73 20 6c 65 73 73 20 74 68 61 6e 20 0d 0a 2a 2a  s less than ..**
26772 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
26773 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
26774 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0d 0a 2a  n jump to P2...*
26775 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  *..** See also: 
26776 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
26777 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47   Distinct, SeekG
26778 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
26779 65 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65  e..*/../* Opcode
2677a 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 32 20 50  : SeekLe P1 P2 P
2677b 33 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 49  3 P4 *..**..** I
2677c 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
2677d 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62  rs to an SQL tab
2677e 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20  le (B-Tree that 
2677f 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79  uses integer key
26780 73 29 2c 20 0d 0a 2a 2a 20 75 73 65 20 74 68 65  s), ..** use the
26781 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
26782 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20  er P3 as a key. 
26783 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
26784 65 72 73 20 0d 0a 2a 2a 20 74 6f 20 61 6e 20 53  ers ..** to an S
26785 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50  QL index, then P
26786 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  3 is the first i
26787 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34  n an array of P4
26788 20 72 65 67 69 73 74 65 72 73 20 0d 0a 2a 2a 20   registers ..** 
26789 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73  that are used as
2678a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
2678b 65 78 20 6b 65 79 2e 20 0d 0a 2a 2a 0d 0a 2a 2a  ex key. ..**..**
2678c 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73   Reposition curs
2678d 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74  or P1 so that it
2678e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c   points to the l
2678f 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61  argest entry tha
26790 74 20 0d 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74  t ..** is less t
26791 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
26792 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49  the key value. I
26793 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
26794 65 63 6f 72 64 73 20 0d 0a 2a 2a 20 6c 65 73 73  ecords ..** less
26795 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
26796 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32  o the key and P2
26797 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
26798 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0d 0a  en jump to P2...
26799 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  **..** See also:
2679a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
2679b 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b  , Distinct, Seek
2679c 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b  Gt, SeekGe, Seek
2679d 4c 74 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  Lt..*/..case OP_
2679e 53 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20  SeekLt:         
2679f 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0d  /* jump, in3 */.
267a0 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a  .case OP_SeekLe:
267a1 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
267a2 2c 20 69 6e 33 20 2a 2f 0d 0a 63 61 73 65 20 4f  , in3 */..case O
267a3 50 5f 53 65 65 6b 47 65 3a 20 20 20 20 20 20 20  P_SeekGe:       
267a4 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
267a5 2f 0d 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 47  /..case OP_SeekG
267a6 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75  t: {       /* ju
267a7 6d 70 2c 20 69 6e 33 20 2a 2f 0d 0a 23 69 66 20  mp, in3 */..#if 
267a8 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
267a9 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
267aa 20 75 2e 62 62 20 2a 2f 0d 0a 20 20 69 6e 74 20   u.bb */..  int 
267ab 72 65 73 3b 0d 0a 20 20 69 6e 74 20 6f 63 3b 0d  res;..  int oc;.
267ac 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
267ad 43 3b 0d 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  C;..  UnpackedRe
267ae 63 6f 72 64 20 72 3b 0d 0a 20 20 69 6e 74 20 6e  cord r;..  int n
267af 46 69 65 6c 64 3b 0d 0a 20 20 69 36 34 20 69 4b  Field;..  i64 iK
267b0 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ey;      /* The 
267b1 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20  rowid we are to 
267b2 73 65 65 6b 20 74 6f 20 2a 2f 0d 0a 23 65 6e 64  seek to */..#end
267b3 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
267b4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
267b5 20 75 2e 62 62 20 2a 2f 0d 0a 0d 0a 20 20 61 73   u.bb */....  as
267b6 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
267b7 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
267b8 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20 61 73 73  Cursor );..  ass
267b9 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20  ert( pOp->p2!=0 
267ba 29 3b 0d 0a 20 20 75 2e 62 62 2e 70 43 20 3d 20  );..  u.bb.pC = 
267bb 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
267bc 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e  ];..  assert( u.
267bd 62 62 2e 70 43 21 3d 30 20 29 3b 0d 0a 20 20 61  bb.pC!=0 );..  a
267be 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 2d 3e  ssert( u.bb.pC->
267bf 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d  pseudoTableReg==
267c0 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
267c1 4f 50 5f 53 65 65 6b 4c 65 20 3d 3d 20 4f 50 5f  OP_SeekLe == OP_
267c2 53 65 65 6b 4c 74 2b 31 20 29 3b 0d 0a 20 20 61  SeekLt+1 );..  a
267c3 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 47 65  ssert( OP_SeekGe
267c4 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 32 20   == OP_SeekLt+2 
267c5 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 4f 50  );..  assert( OP
267c6 5f 53 65 65 6b 47 74 20 3d 3d 20 4f 50 5f 53 65  _SeekGt == OP_Se
267c7 65 6b 4c 74 2b 33 20 29 3b 0d 0a 20 20 61 73 73  ekLt+3 );..  ass
267c8 65 72 74 28 20 75 2e 62 62 2e 70 43 2d 3e 69 73  ert( u.bb.pC->is
267c9 4f 72 64 65 72 65 64 20 29 3b 0d 0a 20 20 69 66  Ordered );..  if
267ca 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e 70 43  ( ALWAYS(u.bb.pC
267cb 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 7b  ->pCursor!=0) ){
267cc 0d 0a 20 20 20 20 75 2e 62 62 2e 6f 63 20 3d 20  ..    u.bb.oc = 
267cd 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0d 0a 20 20  pOp->opcode;..  
267ce 20 20 75 2e 62 62 2e 70 43 2d 3e 6e 75 6c 6c 52    u.bb.pC->nullR
267cf 6f 77 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66 28  ow = 0;..    if(
267d0 20 75 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bb.pC->isTabl
267d1 65 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54  e ){..      /* T
267d2 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 20 69  he input value i
267d3 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 6f 66  n P3 might be of
267d4 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 65 67   any type: integ
267d5 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 6e 67  er, real, string
267d6 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 62  ,..      ** blob
267d7 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 20  , or NULL.  But 
267d8 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  it needs to be a
267d9 6e 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65  n integer before
267da 20 77 65 20 63 61 6e 20 64 6f 0d 0a 20 20 20 20   we can do..    
267db 20 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73    ** the seek, s
267dc 6f 20 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0d  o covert it. */.
267dd 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 61  .      pIn3 = &a
267de 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20  Mem[pOp->p3];.. 
267df 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69       applyNumeri
267e0 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 3b  cAffinity(pIn3);
267e1 0d 0a 20 20 20 20 20 20 75 2e 62 62 2e 69 4b 65  ..      u.bb.iKe
267e2 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  y = sqlite3VdbeI
267e3 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0d 0a  ntValue(pIn3);..
267e4 20 20 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 72        u.bb.pC->r
267e5 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
267e6 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  ....      /* If 
267e7 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 6f 75  the P3 value cou
267e8 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  ld not be conver
267e9 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  ted into an inte
267ea 67 65 72 20 77 69 74 68 6f 75 74 0d 0a 20 20 20  ger without..   
267eb 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e     ** loss of in
267ec 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e 20  formation, then 
267ed 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73 69  special processi
267ee 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e 2e  ng is required..
267ef 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20  . */..      if( 
267f0 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn3->flags & M
267f1 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0d 0a 20  EM_Int)==0 ){.. 
267f2 20 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33         if( (pIn3
267f3 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
267f4 61 6c 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  al)==0 ){..     
267f5 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50       /* If the P
267f6 33 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62  3 value cannot b
267f7 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
267f8 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e   any kind of a n
267f9 75 6d 62 65 72 2c 0d 0a 20 20 20 20 20 20 20 20  umber,..        
267fa 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 73 65    ** then the se
267fb 65 6b 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  ek is not possib
267fc 6c 65 2c 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50  le, so jump to P
267fd 32 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20  2 */..          
267fe 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
267ff 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ;..          bre
26800 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ak;..        }..
26801 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65          /* If we
26802 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e   reach this poin
26803 74 2c 20 74 68 65 6e 20 74 68 65 20 50 33 20 76  t, then the P3 v
26804 61 6c 75 65 20 6d 75 73 74 20 62 65 20 61 20 66  alue must be a f
26805 6c 6f 61 74 69 6e 67 0d 0a 20 20 20 20 20 20 20  loating..       
26806 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72   ** point number
26807 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 61 73  . */..        as
26808 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c 61  sert( (pIn3->fla
26809 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 3d  gs & MEM_Real)!=
2680a 30 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20  0 );....        
2680b 69 66 28 20 75 2e 62 62 2e 69 4b 65 79 3d 3d 53  if( u.bb.iKey==S
2680c 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26  MALLEST_INT64 &&
2680d 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c   (pIn3->r<(doubl
2680e 65 29 75 2e 62 62 2e 69 4b 65 79 20 7c 7c 20 70  e)u.bb.iKey || p
2680f 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0d 0a 20 20  In3->r>0) ){..  
26810 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
26811 33 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c  3 value is too l
26812 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 64  arge in magnitud
26813 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65  e to be expresse
26814 64 20 61 73 20 61 6e 0d 0a 20 20 20 20 20 20 20  d as an..       
26815 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a     ** integer. *
26816 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62  /..          u.b
26817 62 2e 72 65 73 20 3d 20 31 3b 0d 0a 20 20 20 20  b.res = 1;..    
26818 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e        if( pIn3->
26819 72 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  r<0 ){..        
2681a 20 20 20 20 69 66 28 20 75 2e 62 62 2e 6f 63 3e      if( u.bb.oc>
2681b 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 61  =OP_SeekGe ){  a
2681c 73 73 65 72 74 28 20 75 2e 62 62 2e 6f 63 3d 3d  ssert( u.bb.oc==
2681d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 62  OP_SeekGe || u.b
2681e 62 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  b.oc==OP_SeekGt 
2681f 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
26820 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
26821 72 65 65 46 69 72 73 74 28 75 2e 62 62 2e 70 43  reeFirst(u.bb.pC
26822 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 62  ->pCursor, &u.bb
26823 2e 72 65 73 29 3b 0d 0a 20 20 20 20 20 20 20 20  .res);..        
26824 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
26825 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61  LITE_OK ) goto a
26826 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
26827 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  r;..            
26828 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  }..          }el
26829 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  se{..           
2682a 20 69 66 28 20 75 2e 62 62 2e 6f 63 3c 3d 4f 50   if( u.bb.oc<=OP
2682b 5f 53 65 65 6b 4c 65 20 29 7b 20 20 61 73 73 65  _SeekLe ){  asse
2682c 72 74 28 20 75 2e 62 62 2e 6f 63 3d 3d 4f 50 5f  rt( u.bb.oc==OP_
2682d 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 62 62 2e 6f  SeekLt || u.bb.o
2682e 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0d  c==OP_SeekLe );.
2682f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
26830 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
26831 4c 61 73 74 28 75 2e 62 62 2e 70 43 2d 3e 70 43  Last(u.bb.pC->pC
26832 75 72 73 6f 72 2c 20 26 75 2e 62 62 2e 72 65 73  ursor, &u.bb.res
26833 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
26834 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
26835 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
26836 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0d 0a  _due_to_error;..
26837 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20              }.. 
26838 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
26839 20 20 20 20 20 20 69 66 28 20 75 2e 62 62 2e 72        if( u.bb.r
2683a 65 73 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  es ){..         
2683b 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
2683c 2d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  - 1;..          
2683d 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  }..          bre
2683e 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  ak;..        }el
2683f 73 65 20 69 66 28 20 75 2e 62 62 2e 6f 63 3d 3d  se if( u.bb.oc==
26840 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 62  OP_SeekLt || u.b
26841 62 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20  b.oc==OP_SeekGe 
26842 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ){..          /*
26843 20 55 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67   Use the ceiling
26844 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  () function to c
26845 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74  onvert real->int
26846 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 69   */..          i
26847 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f  f( pIn3->r > (do
26848 75 62 6c 65 29 75 2e 62 62 2e 69 4b 65 79 20 29  uble)u.bb.iKey )
26849 20 75 2e 62 62 2e 69 4b 65 79 2b 2b 3b 0d 0a 20   u.bb.iKey++;.. 
2684a 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
2684b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
2684c 74 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63  the floor() func
2684d 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20  tion to convert 
2684e 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0d 0a 20 20  real->int */..  
2684f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
26850 75 2e 62 62 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.bb.oc==OP_Seek
26851 4c 65 20 7c 7c 20 75 2e 62 62 2e 6f 63 3d 3d 4f  Le || u.bb.oc==O
26852 50 5f 53 65 65 6b 47 74 20 29 3b 0d 0a 20 20 20  P_SeekGt );..   
26853 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d         if( pIn3-
26854 3e 72 20 3c 20 28 64 6f 75 62 6c 65 29 75 2e 62  >r < (double)u.b
26855 62 2e 69 4b 65 79 20 29 20 75 2e 62 62 2e 69 4b  b.iKey ) u.bb.iK
26856 65 79 2d 2d 3b 0d 0a 20 20 20 20 20 20 20 20 7d  ey--;..        }
26857 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
26858 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
26859 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
2685a 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bb.pC->pCurso
2685b 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 62 62 2e  r, 0, (u64)u.bb.
2685c 69 4b 65 79 2c 20 30 2c 20 26 75 2e 62 62 2e 72  iKey, 0, &u.bb.r
2685d 65 73 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  es);..      if( 
2685e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2685f 0d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  ..        goto a
26860 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
26861 72 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  r;..      }..   
26862 20 20 20 69 66 28 20 75 2e 62 62 2e 72 65 73 3d     if( u.bb.res=
26863 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 75  =0 ){..        u
26864 2e 62 62 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bb.pC->rowidIsV
26865 61 6c 69 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20  alid = 1;..     
26866 20 20 20 75 2e 62 62 2e 70 43 2d 3e 6c 61 73 74     u.bb.pC->last
26867 52 6f 77 69 64 20 3d 20 75 2e 62 62 2e 69 4b 65  Rowid = u.bb.iKe
26868 79 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  y;..      }..   
26869 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 75   }else{..      u
2686a 2e 62 62 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  .bb.nField = pOp
2686b 2d 3e 70 34 2e 69 3b 0d 0a 20 20 20 20 20 20 61  ->p4.i;..      a
2686c 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
2686d 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0d  pe==P4_INT32 );.
2686e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
2686f 2e 62 62 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0d  .bb.nField>0 );.
26870 0a 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 70 4b  .      u.bb.r.pK
26871 65 79 49 6e 66 6f 20 3d 20 75 2e 62 62 2e 70 43  eyInfo = u.bb.pC
26872 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 20  ->pKeyInfo;..   
26873 20 20 20 75 2e 62 62 2e 72 2e 6e 46 69 65 6c 64     u.bb.r.nField
26874 20 3d 20 28 75 31 36 29 75 2e 62 62 2e 6e 46 69   = (u16)u.bb.nFi
26875 65 6c 64 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  eld;....      /*
26876 20 54 68 65 20 6e 65 78 74 20 6c 69 6e 65 20 6f   The next line o
26877 66 20 63 6f 64 65 20 63 6f 6d 70 75 74 65 73 20  f code computes 
26878 61 73 20 66 6f 6c 6c 6f 77 73 2c 20 6f 6e 6c 79  as follows, only
26879 20 66 61 73 74 65 72 3a 0d 0a 20 20 20 20 20 20   faster:..      
2687a 2a 2a 20 20 20 69 66 28 20 75 2e 62 62 2e 6f 63  **   if( u.bb.oc
2687b 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75  ==OP_SeekGt || u
2687c 2e 62 62 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c  .bb.oc==OP_SeekL
2687d 65 20 29 7b 0d 0a 20 20 20 20 20 20 2a 2a 20 20  e ){..      **  
2687e 20 20 20 75 2e 62 62 2e 72 2e 66 6c 61 67 73 20     u.bb.r.flags 
2687f 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  = UNPACKED_INCRK
26880 45 59 3b 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20  EY;..      **   
26881 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2a 2a  }else{..      **
26882 20 20 20 20 20 75 2e 62 62 2e 72 2e 66 6c 61 67       u.bb.r.flag
26883 73 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 2a 2a  s = 0;..      **
26884 20 20 20 7d 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a     }..      */..
26885 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 66 6c 61        u.bb.r.fla
26886 67 73 20 3d 20 28 75 31 36 29 28 55 4e 50 41 43  gs = (u16)(UNPAC
26887 4b 45 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28 31  KED_INCRKEY * (1
26888 20 26 20 28 75 2e 62 62 2e 6f 63 20 2d 20 4f 50   & (u.bb.oc - OP
26889 5f 53 65 65 6b 4c 74 29 29 29 3b 0d 0a 20 20 20  _SeekLt)));..   
2688a 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e     assert( u.bb.
2688b 6f 63 21 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c  oc!=OP_SeekGt ||
2688c 20 75 2e 62 62 2e 72 2e 66 6c 61 67 73 3d 3d 55   u.bb.r.flags==U
2688d 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
2688e 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
2688f 28 20 75 2e 62 62 2e 6f 63 21 3d 4f 50 5f 53 65  ( u.bb.oc!=OP_Se
26890 65 6b 4c 65 20 7c 7c 20 75 2e 62 62 2e 72 2e 66  ekLe || u.bb.r.f
26891 6c 61 67 73 3d 3d 55 4e 50 41 43 4b 45 44 5f 49  lags==UNPACKED_I
26892 4e 43 52 4b 45 59 20 29 3b 0d 0a 20 20 20 20 20  NCRKEY );..     
26893 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 6f 63   assert( u.bb.oc
26894 21 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75  !=OP_SeekGe || u
26895 2e 62 62 2e 72 2e 66 6c 61 67 73 3d 3d 30 20 29  .bb.r.flags==0 )
26896 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
26897 20 75 2e 62 62 2e 6f 63 21 3d 4f 50 5f 53 65 65   u.bb.oc!=OP_See
26898 6b 4c 74 20 7c 7c 20 75 2e 62 62 2e 72 2e 66 6c  kLt || u.bb.r.fl
26899 61 67 73 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 20  ags==0 );....   
2689a 20 20 20 75 2e 62 62 2e 72 2e 61 4d 65 6d 20 3d     u.bb.r.aMem =
2689b 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
2689c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2689d 44 45 42 55 47 0d 0a 20 20 20 20 20 20 7b 20 69  DEBUG..      { i
2689e 6e 74 20 69 3b 20 66 6f 72 28 69 3d 30 3b 20 69  nt i; for(i=0; i
2689f 3c 75 2e 62 62 2e 72 2e 6e 46 69 65 6c 64 3b 20  <u.bb.r.nField; 
268a0 69 2b 2b 29 20 61 73 73 65 72 74 28 20 6d 65 6d  i++) assert( mem
268a1 49 73 56 61 6c 69 64 28 26 75 2e 62 62 2e 72 2e  IsValid(&u.bb.r.
268a2 61 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d 0d 0a 23  aMem[i]) ); }..#
268a3 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 45 78 70  endif..      Exp
268a4 61 6e 64 42 6c 6f 62 28 75 2e 62 62 2e 72 2e 61  andBlob(u.bb.r.a
268a5 4d 65 6d 29 3b 0d 0a 20 20 20 20 20 20 72 63 20  Mem);..      rc 
268a6 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
268a7 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62  vetoUnpacked(u.b
268a8 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  b.pC->pCursor, &
268a9 75 2e 62 62 2e 72 2c 20 30 2c 20 30 2c 20 26 75  u.bb.r, 0, 0, &u
268aa 2e 62 62 2e 72 65 73 29 3b 0d 0a 20 20 20 20 20  .bb.res);..     
268ab 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
268ac 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67  OK ){..        g
268ad 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
268ae 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 20 20 7d  _error;..      }
268af 0d 0a 20 20 20 20 20 20 75 2e 62 62 2e 70 43 2d  ..      u.bb.pC-
268b0 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
268b1 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 75  0;..    }..    u
268b2 2e 62 62 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bb.pC->deferred
268b3 4d 6f 76 65 74 6f 20 3d 20 30 3b 0d 0a 20 20 20  Moveto = 0;..   
268b4 20 75 2e 62 62 2e 70 43 2d 3e 63 61 63 68 65 53   u.bb.pC->cacheS
268b5 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
268b6 41 4c 45 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c  ALE;..#ifdef SQL
268b7 49 54 45 5f 54 45 53 54 0d 0a 20 20 20 20 73 71  ITE_TEST..    sq
268b8 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
268b9 6e 74 2b 2b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  nt++;..#endif.. 
268ba 20 20 20 69 66 28 20 75 2e 62 62 2e 6f 63 3e 3d     if( u.bb.oc>=
268bb 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 61 73  OP_SeekGe ){  as
268bc 73 65 72 74 28 20 75 2e 62 62 2e 6f 63 3d 3d 4f  sert( u.bb.oc==O
268bd 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 62 62  P_SeekGe || u.bb
268be 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29  .oc==OP_SeekGt )
268bf 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 75 2e 62  ;..      if( u.b
268c0 62 2e 72 65 73 3c 30 20 7c 7c 20 28 75 2e 62 62  b.res<0 || (u.bb
268c1 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e 62 62 2e  .res==0 && u.bb.
268c2 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 29 20 29  oc==OP_SeekGt) )
268c3 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
268c4 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
268c5 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bb.pC->pCurso
268c6 72 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0d 0a  r, &u.bb.res);..
268c7 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
268c8 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
268c9 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
268ca 72 6f 72 3b 0d 0a 20 20 20 20 20 20 20 20 75 2e  ror;..        u.
268cb 62 62 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bb.pC->rowidIsVa
268cc 6c 69 64 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  lid = 0;..      
268cd 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
268ce 75 2e 62 62 2e 72 65 73 20 3d 20 30 3b 0d 0a 20  u.bb.res = 0;.. 
268cf 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73       }..    }els
268d0 65 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  e{..      assert
268d1 28 20 75 2e 62 62 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.bb.oc==OP_Se
268d2 65 6b 4c 74 20 7c 7c 20 75 2e 62 62 2e 6f 63 3d  ekLt || u.bb.oc=
268d3 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0d 0a 20  =OP_SeekLe );.. 
268d4 20 20 20 20 20 69 66 28 20 75 2e 62 62 2e 72 65       if( u.bb.re
268d5 73 3e 30 20 7c 7c 20 28 75 2e 62 62 2e 72 65 73  s>0 || (u.bb.res
268d6 3d 3d 30 20 26 26 20 75 2e 62 62 2e 6f 63 3d 3d  ==0 && u.bb.oc==
268d7 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0d 0a 20  OP_SeekLt) ){.. 
268d8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
268d9 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
268da 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bb.pC->pCurso
268db 72 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0d 0a  r, &u.bb.res);..
268dc 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
268dd 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
268de 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
268df 72 6f 72 3b 0d 0a 20 20 20 20 20 20 20 20 75 2e  ror;..        u.
268e0 62 62 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bb.pC->rowidIsVa
268e1 6c 69 64 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  lid = 0;..      
268e2 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
268e3 2f 2a 20 75 2e 62 62 2e 72 65 73 20 6d 69 67 68  /* u.bb.res migh
268e4 74 20 62 65 20 6e 65 67 61 74 69 76 65 20 62 65  t be negative be
268e5 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
268e6 69 73 20 65 6d 70 74 79 2e 20 20 43 68 65 63 6b  is empty.  Check
268e7 20 74 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20   to..        ** 
268e8 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 74  see if this is t
268e9 68 65 20 63 61 73 65 2e 0d 0a 20 20 20 20 20 20  he case...      
268ea 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 75 2e    */..        u.
268eb 62 62 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 33  bb.res = sqlite3
268ec 42 74 72 65 65 45 6f 66 28 75 2e 62 62 2e 70 43  BtreeEof(u.bb.pC
268ed 2d 3e 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20 20  ->pCursor);..   
268ee 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
268ef 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
268f0 3e 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 75  >0 );..    if( u
268f1 2e 62 62 2e 72 65 73 20 29 7b 0d 0a 20 20 20 20  .bb.res ){..    
268f2 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
268f3 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65   1;..    }..  }e
268f4 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69  lse{..    /* Thi
268f5 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61  s happens when a
268f6 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65  ttempting to ope
268f7 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61  n the sqlite3_ma
268f8 73 74 65 72 20 74 61 62 6c 65 0d 0a 20 20 20 20  ster table..    
268f9 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 63 65  ** for read acce
268fa 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ss returns SQLIT
268fb 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 69 73  E_EMPTY. In this
268fc 20 63 61 73 65 20 61 6c 77 61 79 73 0d 0a 20 20   case always..  
268fd 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75    ** take the ju
268fe 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 20  mp (since there 
268ff 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 69  are no records i
26900 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0d 0a 20  n the table)... 
26901 20 20 20 2a 2f 0d 0a 20 20 20 20 70 63 20 3d 20     */..    pc = 
26902 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20 20  pOp->p2 - 1;..  
26903 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  }..  break;..}..
26904 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65  ../* Opcode: See
26905 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a  k P1 P2 * * *..*
26906 2a 0d 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f  *..** P1 is an o
26907 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72  pen table cursor
26908 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77   and P2 is a row
26909 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72  id integer.  Arr
2690a 61 6e 67 65 0d 0a 2a 2a 20 66 6f 72 20 50 31 20  ange..** for P1 
2690b 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20  to move so that 
2690c 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
2690d 20 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20   rowid given by 
2690e 50 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  P2...**..** This
2690f 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 64   is actually a d
26910 65 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20 4e  eferred seek.  N
26911 6f 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79 20  othing actually 
26912 68 61 70 70 65 6e 73 20 75 6e 74 69 6c 0d 0a 2a  happens until..*
26913 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  * the cursor is 
26914 75 73 65 64 20 74 6f 20 72 65 61 64 20 61 20 72  used to read a r
26915 65 63 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79  ecord.  That way
26916 2c 20 69 66 20 6e 6f 20 72 65 61 64 73 0d 0a 2a  , if no reads..*
26917 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65  * occur, no unne
26918 63 65 73 73 61 72 79 20 49 2f 4f 20 68 61 70 70  cessary I/O happ
26919 65 6e 73 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f  ens...*/..case O
2691a 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20  P_Seek: {    /* 
2691b 69 6e 32 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f  in2 */..#if 0  /
2691c 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
2691d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
2691e 63 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73  c */..  VdbeCurs
2691f 6f 72 20 2a 70 43 3b 0d 0a 23 65 6e 64 69 66 20  or *pC;..#endif 
26920 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
26921 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
26922 62 63 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72  bc */....  asser
26923 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
26924 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
26925 73 6f 72 20 29 3b 0d 0a 20 20 75 2e 62 63 2e 70  sor );..  u.bc.p
26926 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
26927 2d 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74  ->p1];..  assert
26928 28 20 75 2e 62 63 2e 70 43 21 3d 30 20 29 3b 0d  ( u.bc.pC!=0 );.
26929 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
2692a 62 63 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bc.pC->pCursor!=
2692b 30 29 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  0) ){..    asser
2692c 74 28 20 75 2e 62 63 2e 70 43 2d 3e 69 73 54 61  t( u.bc.pC->isTa
2692d 62 6c 65 20 29 3b 0d 0a 20 20 20 20 75 2e 62 63  ble );..    u.bc
2692e 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30  .pC->nullRow = 0
2692f 3b 0d 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61  ;..    pIn2 = &a
26930 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20  Mem[pOp->p2];.. 
26931 20 20 20 75 2e 62 63 2e 70 43 2d 3e 6d 6f 76 65     u.bc.pC->move
26932 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 74  toTarget = sqlit
26933 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
26934 49 6e 32 29 3b 0d 0a 20 20 20 20 75 2e 62 63 2e  In2);..    u.bc.
26935 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
26936 20 3d 20 30 3b 0d 0a 20 20 20 20 75 2e 62 63 2e   = 0;..    u.bc.
26937 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
26938 74 6f 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20  to = 1;..  }..  
26939 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 20 20 0d 0a 0d  break;..}..  ...
2693a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 6e  ./* Opcode: Foun
2693b 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0d  d P1 P2 P3 P4 *.
2693c 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 50 34 3d 3d 30  .**..** If P4==0
2693d 20 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 50   then register P
2693e 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63  3 holds a blob c
2693f 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61  onstructed by Ma
26940 6b 65 52 65 63 6f 72 64 2e 20 20 49 66 0d 0a 2a  keRecord.  If..*
26941 2a 20 50 34 3e 30 20 74 68 65 6e 20 72 65 67 69  * P4>0 then regi
26942 73 74 65 72 20 50 33 20 69 73 20 74 68 65 20 66  ster P3 is the f
26943 69 72 73 74 20 6f 66 20 50 34 20 72 65 67 69 73  irst of P4 regis
26944 74 65 72 73 20 74 68 61 74 20 66 6f 72 6d 20 61  ters that form a
26945 6e 20 75 6e 70 61 63 6b 65 64 0d 0a 2a 2a 20 72  n unpacked..** r
26946 65 63 6f 72 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43  ecord...**..** C
26947 75 72 73 6f 72 20 50 31 20 69 73 20 6f 6e 20 61  ursor P1 is on a
26948 6e 20 69 6e 64 65 78 20 62 74 72 65 65 2e 20 20  n index btree.  
26949 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 64  If the record id
2694a 65 6e 74 69 66 69 65 64 20 62 79 20 50 33 20 61  entified by P3 a
2694b 6e 64 20 50 34 0d 0a 2a 2a 20 69 73 20 61 20 70  nd P4..** is a p
2694c 72 65 66 69 78 20 6f 66 20 61 6e 79 20 65 6e 74  refix of any ent
2694d 72 79 20 69 6e 20 50 31 20 74 68 65 6e 20 61 20  ry in P1 then a 
2694e 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20  jump is made to 
2694f 50 32 20 61 6e 64 0d 0a 2a 2a 20 50 31 20 69 73  P2 and..** P1 is
26950 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
26951 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65  t the matching e
26952 6e 74 72 79 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70  ntry...*/../* Op
26953 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50  code: NotFound P
26954 31 20 50 32 20 50 33 20 50 34 20 2a 0d 0a 2a 2a  1 P2 P3 P4 *..**
26955 0d 0a 2a 2a 20 49 66 20 50 34 3d 3d 30 20 74 68  ..** If P4==0 th
26956 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 68  en register P3 h
26957 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  olds a blob cons
26958 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52  tructed by MakeR
26959 65 63 6f 72 64 2e 20 20 49 66 0d 0a 2a 2a 20 50  ecord.  If..** P
2695a 34 3e 30 20 74 68 65 6e 20 72 65 67 69 73 74 65  4>0 then registe
2695b 72 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73  r P3 is the firs
2695c 74 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72  t of P4 register
2695d 73 20 74 68 61 74 20 66 6f 72 6d 20 61 6e 20 75  s that form an u
2695e 6e 70 61 63 6b 65 64 0d 0a 2a 2a 20 72 65 63 6f  npacked..** reco
2695f 72 64 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 43 75 72  rd...** ..** Cur
26960 73 6f 72 20 50 31 20 69 73 20 6f 6e 20 61 6e 20  sor P1 is on an 
26961 69 6e 64 65 78 20 62 74 72 65 65 2e 20 20 49 66  index btree.  If
26962 20 74 68 65 20 72 65 63 6f 72 64 20 69 64 65 6e   the record iden
26963 74 69 66 69 65 64 20 62 79 20 50 33 20 61 6e 64  tified by P3 and
26964 20 50 34 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 74   P4..** is not t
26965 68 65 20 70 72 65 66 69 78 20 6f 66 20 61 6e 79  he prefix of any
26966 20 65 6e 74 72 79 20 69 6e 20 50 31 20 74 68 65   entry in P1 the
26967 6e 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  n a jump is made
26968 20 74 6f 20 50 32 2e 20 20 49 66 20 50 31 20 0d   to P2.  If P1 .
26969 0a 2a 2a 20 64 6f 65 73 20 63 6f 6e 74 61 69 6e  .** does contain
2696a 20 61 6e 20 65 6e 74 72 79 20 77 68 6f 73 65 20   an entry whose 
2696b 70 72 65 66 69 78 20 6d 61 74 63 68 65 73 20 74  prefix matches t
2696c 68 65 20 50 33 2f 50 34 20 72 65 63 6f 72 64 20  he P3/P4 record 
2696d 74 68 65 6e 20 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a  then control..**
2696e 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 74   falls through t
2696f 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
26970 75 63 74 69 6f 6e 20 61 6e 64 20 50 31 20 69 73  uction and P1 is
26971 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
26972 74 20 74 68 65 0d 0a 2a 2a 20 6d 61 74 63 68 69  t the..** matchi
26973 6e 67 20 65 6e 74 72 79 2e 0d 0a 2a 2a 0d 0a 2a  ng entry...**..*
26974 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
26975 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73  d, NotExists, Is
26976 55 6e 69 71 75 65 0d 0a 2a 2f 0d 0a 63 61 73 65  Unique..*/..case
26977 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20   OP_NotFound:   
26978 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
26979 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 46 6f 75   */..case OP_Fou
2697a 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  nd: {        /* 
2697b 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0d 0a 23 69  jump, in3 */..#i
2697c 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
2697d 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2697e 74 6f 20 75 2e 62 64 20 2a 2f 0d 0a 20 20 69 6e  to u.bd */..  in
2697f 74 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b  t alreadyExists;
26980 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ..  VdbeCursor *
26981 70 43 3b 0d 0a 20 20 69 6e 74 20 72 65 73 3b 0d  pC;..  int res;.
26982 0a 20 20 63 68 61 72 20 2a 70 46 72 65 65 3b 0d  .  char *pFree;.
26983 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
26984 64 20 2a 70 49 64 78 4b 65 79 3b 0d 0a 20 20 55  d *pIdxKey;..  U
26985 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b  npackedRecord r;
26986 0d 0a 20 20 63 68 61 72 20 61 54 65 6d 70 52 65  ..  char aTempRe
26987 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  c[ROUND8(sizeof(
26988 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29  UnpackedRecord))
26989 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33   + sizeof(Mem)*3
2698a 20 2b 20 37 5d 3b 0d 0a 23 65 6e 64 69 66 20 2f   + 7];..#endif /
2698b 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
2698c 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
2698d 64 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20 53  d */....#ifdef S
2698e 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 73 71  QLITE_TEST..  sq
2698f 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 6e  lite3_found_coun
26990 74 2b 2b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  t++;..#endif....
26991 20 20 75 2e 62 64 2e 61 6c 72 65 61 64 79 45 78    u.bd.alreadyEx
26992 69 73 74 73 20 3d 20 30 3b 0d 0a 20 20 61 73 73  ists = 0;..  ass
26993 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
26994 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
26995 75 72 73 6f 72 20 29 3b 0d 0a 20 20 61 73 73 65  ursor );..  asse
26996 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
26997 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0d 0a 20 20  =P4_INT32 );..  
26998 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bd.pC = p->apC
26999 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  sr[pOp->p1];..  
2699a 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 21  assert( u.bd.pC!
2699b 3d 30 20 29 3b 0d 0a 20 20 70 49 6e 33 20 3d 20  =0 );..  pIn3 = 
2699c 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d  &aMem[pOp->p3];.
2699d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
2699e 62 64 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bd.pC->pCursor!=
2699f 30 29 20 29 7b 0d 0a 0d 0a 20 20 20 20 61 73 73  0) ){....    ass
269a0 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 69 73  ert( u.bd.pC->is
269a1 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20 20 20  Table==0 );..   
269a2 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30   if( pOp->p4.i>0
269a3 20 29 7b 0d 0a 20 20 20 20 20 20 75 2e 62 64 2e   ){..      u.bd.
269a4 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62  r.pKeyInfo = u.b
269a5 64 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0d  d.pC->pKeyInfo;.
269a6 0a 20 20 20 20 20 20 75 2e 62 64 2e 72 2e 6e 46  .      u.bd.r.nF
269a7 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d  ield = (u16)pOp-
269a8 3e 70 34 2e 69 3b 0d 0a 20 20 20 20 20 20 75 2e  >p4.i;..      u.
269a9 62 64 2e 72 2e 61 4d 65 6d 20 3d 20 70 49 6e 33  bd.r.aMem = pIn3
269aa 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
269ab 5f 44 45 42 55 47 0d 0a 20 20 20 20 20 20 7b 20  _DEBUG..      { 
269ac 69 6e 74 20 69 3b 20 66 6f 72 28 69 3d 30 3b 20  int i; for(i=0; 
269ad 69 3c 75 2e 62 64 2e 72 2e 6e 46 69 65 6c 64 3b  i<u.bd.r.nField;
269ae 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 6d 65   i++) assert( me
269af 6d 49 73 56 61 6c 69 64 28 26 75 2e 62 64 2e 72  mIsValid(&u.bd.r
269b0 2e 61 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d 0d 0a  .aMem[i]) ); }..
269b1 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 75 2e  #endif..      u.
269b2 62 64 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  bd.r.flags = UNP
269b3 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
269b4 43 48 3b 0d 0a 20 20 20 20 20 20 75 2e 62 64 2e  CH;..      u.bd.
269b5 70 49 64 78 4b 65 79 20 3d 20 26 75 2e 62 64 2e  pIdxKey = &u.bd.
269b6 72 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  r;..    }else{..
269b7 20 20 20 20 20 20 75 2e 62 64 2e 70 49 64 78 4b        u.bd.pIdxK
269b8 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
269b9 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52 65 63  AllocUnpackedRec
269ba 6f 72 64 28 0d 0a 20 20 20 20 20 20 20 20 20 20  ord(..          
269bb 75 2e 62 64 2e 70 43 2d 3e 70 4b 65 79 49 6e 66  u.bd.pC->pKeyInf
269bc 6f 2c 20 75 2e 62 64 2e 61 54 65 6d 70 52 65 63  o, u.bd.aTempRec
269bd 2c 20 73 69 7a 65 6f 66 28 75 2e 62 64 2e 61 54  , sizeof(u.bd.aT
269be 65 6d 70 52 65 63 29 2c 20 26 75 2e 62 64 2e 70  empRec), &u.bd.p
269bf 46 72 65 65 0d 0a 20 20 20 20 20 20 29 3b 0d 0a  Free..      );..
269c0 20 20 20 20 20 20 69 66 28 20 75 2e 62 64 2e 70        if( u.bd.p
269c1 49 64 78 4b 65 79 3d 3d 30 20 29 20 67 6f 74 6f  IdxKey==0 ) goto
269c2 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20 20 20   no_mem;..      
269c3 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
269c4 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
269c5 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
269c6 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20   (pIn3->flags & 
269c7 4d 45 4d 5f 5a 65 72 6f 29 3d 3d 30 20 29 3b 20  MEM_Zero)==0 ); 
269c8 20 2f 2a 20 7a 65 72 6f 62 6c 6f 62 73 20 61 6c   /* zeroblobs al
269c9 72 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 2a  ready expanded *
269ca 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
269cb 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
269cc 28 75 2e 62 64 2e 70 43 2d 3e 70 4b 65 79 49 6e  (u.bd.pC->pKeyIn
269cd 66 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e  fo, pIn3->n, pIn
269ce 33 2d 3e 7a 2c 20 75 2e 62 64 2e 70 49 64 78 4b  3->z, u.bd.pIdxK
269cf 65 79 29 3b 0d 0a 20 20 20 20 20 20 75 2e 62 64  ey);..      u.bd
269d0 2e 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20  .pIdxKey->flags 
269d1 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46  |= UNPACKED_PREF
269d2 49 58 5f 4d 41 54 43 48 3b 0d 0a 20 20 20 20 7d  IX_MATCH;..    }
269d3 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
269d4 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
269d5 61 63 6b 65 64 28 75 2e 62 64 2e 70 43 2d 3e 70  acked(u.bd.pC->p
269d6 43 75 72 73 6f 72 2c 20 75 2e 62 64 2e 70 49 64  Cursor, u.bd.pId
269d7 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 75 2e 62  xKey, 0, 0, &u.b
269d8 64 2e 72 65 73 29 3b 0d 0a 20 20 20 20 69 66 28  d.res);..    if(
269d9 20 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30 20 29 7b   pOp->p4.i==0 ){
269da 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
269db 62 46 72 65 65 28 64 62 2c 20 75 2e 62 64 2e 70  bFree(db, u.bd.p
269dc 46 72 65 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Free);..    }.. 
269dd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
269de 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 62  E_OK ){..      b
269df 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
269e0 20 20 75 2e 62 64 2e 61 6c 72 65 61 64 79 45 78    u.bd.alreadyEx
269e1 69 73 74 73 20 3d 20 28 75 2e 62 64 2e 72 65 73  ists = (u.bd.res
269e2 3d 3d 30 29 3b 0d 0a 20 20 20 20 75 2e 62 64 2e  ==0);..    u.bd.
269e3 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
269e4 74 6f 20 3d 20 30 3b 0d 0a 20 20 20 20 75 2e 62  to = 0;..    u.b
269e5 64 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  d.pC->cacheStatu
269e6 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
269e7 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 4f 70  ..  }..  if( pOp
269e8 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75  ->opcode==OP_Fou
269e9 6e 64 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 75  nd ){..    if( u
269ea 2e 62 64 2e 61 6c 72 65 61 64 79 45 78 69 73 74  .bd.alreadyExist
269eb 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  s ) pc = pOp->p2
269ec 20 2d 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d   - 1;..  }else{.
269ed 0a 20 20 20 20 69 66 28 20 21 75 2e 62 64 2e 61  .    if( !u.bd.a
269ee 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70  lreadyExists ) p
269ef 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
269f0 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d  ..  }..  break;.
269f1 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}..../* Opcode:
269f2 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 32 20   IsUnique P1 P2 
269f3 50 33 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  P3 P4 *..**..** 
269f4 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65  Cursor P1 is ope
269f5 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d  n on an index b-
269f6 74 72 65 65 20 2d 20 74 68 61 74 20 69 73 20 74  tree - that is t
269f7 6f 20 73 61 79 2c 20 61 20 62 74 72 65 65 20 77  o say, a btree w
269f8 68 69 63 68 0d 0a 2a 2a 20 6e 6f 20 64 61 74 61  hich..** no data
269f9 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20 6b   and where the k
269fa 65 79 20 61 72 65 20 72 65 63 6f 72 64 73 20 67  ey are records g
269fb 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d  enerated by OP_M
269fc 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 0d 0a  akeRecord with..
269fd 2a 2a 20 74 68 65 20 6c 69 73 74 20 66 69 65 6c  ** the list fiel
269fe 64 20 62 65 69 6e 67 20 74 68 65 20 69 6e 74 65  d being the inte
269ff 67 65 72 20 52 4f 57 49 44 20 6f 66 20 74 68 65  ger ROWID of the
26a00 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
26a01 69 6e 64 65 78 0d 0a 2a 2a 20 65 6e 74 72 79 20  index..** entry 
26a02 72 65 66 65 72 73 20 74 6f 2e 0d 0a 2a 2a 0d 0a  refers to...**..
26a03 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69 73 74  ** The P3 regist
26a04 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69  er contains an i
26a05 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75  nteger record nu
26a06 6d 62 65 72 2e 20 43 61 6c 6c 20 74 68 69 73 20  mber. Call this 
26a07 72 65 63 6f 72 64 20 0d 0a 2a 2a 20 6e 75 6d 62  record ..** numb
26a08 65 72 20 52 2e 20 52 65 67 69 73 74 65 72 20 50  er R. Register P
26a09 34 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  4 is the first i
26a0a 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 63 6f 6e  n a set of N con
26a0b 74 69 67 75 6f 75 73 20 72 65 67 69 73 74 65 72  tiguous register
26a0c 73 0d 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20  s..** that make 
26a0d 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  up an unpacked i
26a0e 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 63 61  ndex key that ca
26a0f 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 63  n be used with c
26a10 75 72 73 6f 72 20 50 31 2e 0d 0a 2a 2a 20 54 68  ursor P1...** Th
26a11 65 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 6e  e value of N can
26a12 20 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 6f   be inferred fro
26a13 6d 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e 20  m the cursor. N 
26a14 69 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f 77  includes the row
26a15 69 64 0d 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70  id..** value app
26a16 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64  ended to the end
26a17 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 72 65   of the index re
26a18 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64  cord. This rowid
26a19 20 76 61 6c 75 65 20 6d 61 79 0d 0a 2a 2a 20 6f   value may..** o
26a1a 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65  r may not be the
26a1b 20 73 61 6d 65 20 61 73 20 52 2e 0d 0a 2a 2a 0d   same as R...**.
26a1c 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68  .** If any of th
26a1d 65 20 4e 20 72 65 67 69 73 74 65 72 73 20 62 65  e N registers be
26a1e 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 65 67  ginning with reg
26a1f 69 73 74 65 72 20 50 34 20 63 6f 6e 74 61 69 6e  ister P4 contain
26a20 73 20 61 20 4e 55 4c 4c 0d 0a 2a 2a 20 76 61 6c  s a NULL..** val
26a21 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  ue, jump immedia
26a22 74 65 6c 79 20 74 6f 20 50 32 2e 0d 0a 2a 2a 0d  tely to P2...**.
26a23 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
26a24 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
26a25 63 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72  checks if cursor
26a26 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20   P1 contains an 
26a27 65 6e 74 72 79 0d 0a 2a 2a 20 77 68 65 72 65 20  entry..** where 
26a28 74 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20  the first (N-1) 
26a29 66 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74  fields match but
26a2a 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65   the rowid value
26a2b 20 61 74 20 74 68 65 20 65 6e 64 0d 0a 2a 2a 20   at the end..** 
26a2c 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  of the index ent
26a2d 72 79 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20  ry is not R. If 
26a2e 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
26a2f 20 65 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20   entry, control 
26a30 6a 75 6d 70 73 0d 0a 2a 2a 20 74 6f 20 69 6e 73  jumps..** to ins
26a31 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68  truction P2. Oth
26a32 65 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69  erwise, the rowi
26a33 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63  d of the conflic
26a34 74 69 6e 67 20 69 6e 64 65 78 0d 0a 2a 2a 20 65  ting index..** e
26a35 6e 74 72 79 20 69 73 20 63 6f 70 69 65 64 20 74  ntry is copied t
26a36 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e  o register P3 an
26a37 64 20 63 6f 6e 74 72 6f 6c 20 66 61 6c 6c 73 20  d control falls 
26a38 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
26a39 65 78 74 0d 0a 2a 2a 20 69 6e 73 74 72 75 63 74  ext..** instruct
26a3a 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65  ion...**..** See
26a3b 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c   also: NotFound,
26a3c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f 75 6e   NotExists, Foun
26a3d 64 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49  d..*/..case OP_I
26a3e 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20  sUnique: {      
26a3f 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
26a40 2f 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  /..#if 0  /* loc
26a41 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
26a42 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0d  ed into u.be */.
26a43 0a 20 20 75 31 36 20 69 69 3b 0d 0a 20 20 56 64  .  u16 ii;..  Vd
26a44 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0d 0a  beCursor *pCx;..
26a45 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
26a46 72 3b 0d 0a 20 20 75 31 36 20 6e 46 69 65 6c 64  r;..  u16 nField
26a47 3b 0d 0a 20 20 4d 65 6d 20 2a 61 4d 78 3b 0d 0a  ;..  Mem *aMx;..
26a48 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
26a49 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   r;             
26a4a 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69       /* B-Tree i
26a4b 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20  ndex search key 
26a4c 2a 2f 0d 0a 20 20 69 36 34 20 52 3b 20 20 20 20  */..  i64 R;    
26a4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a4e 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
26a4f 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  d stored in regi
26a50 73 74 65 72 20 50 33 20 2a 2f 0d 0a 23 65 6e 64  ster P3 */..#end
26a51 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
26a52 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
26a53 20 75 2e 62 65 20 2a 2f 0d 0a 0d 0a 20 20 70 49   u.be */....  pI
26a54 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n3 = &aMem[pOp->
26a55 70 33 5d 3b 0d 0a 20 20 75 2e 62 65 2e 61 4d 78  p3];..  u.be.aMx
26a56 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34   = &aMem[pOp->p4
26a57 2e 69 5d 3b 0d 0a 20 20 2f 2a 20 41 73 73 65 72  .i];..  /* Asser
26a58 74 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  t that the value
26a59 73 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  s of parameters 
26a5a 50 31 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e  P1 and P4 are in
26a5b 20 72 61 6e 67 65 2e 20 2a 2f 0d 0a 20 20 61 73   range. */..  as
26a5c 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
26a5d 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0d 0a  e==P4_INT32 );..
26a5e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
26a5f 34 2e 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34  4.i>0 && pOp->p4
26a60 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a  .i<=p->nMem );..
26a61 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
26a62 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
26a63 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 0d  p->nCursor );...
26a64 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 69  .  /* Find the i
26a65 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f 0d  ndex cursor. */.
26a66 0a 20 20 75 2e 62 65 2e 70 43 78 20 3d 20 70 2d  .  u.be.pCx = p-
26a67 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
26a68 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 65  ..  assert( u.be
26a69 2e 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f  .pCx->deferredMo
26a6a 76 65 74 6f 3d 3d 30 20 29 3b 0d 0a 20 20 75 2e  veto==0 );..  u.
26a6b 62 65 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75  be.pCx->seekResu
26a6c 6c 74 20 3d 20 30 3b 0d 0a 20 20 75 2e 62 65 2e  lt = 0;..  u.be.
26a6d 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75 73  pCx->cacheStatus
26a6e 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0d   = CACHE_STALE;.
26a6f 0a 20 20 75 2e 62 65 2e 70 43 72 73 72 20 3d 20  .  u.be.pCrsr = 
26a70 75 2e 62 65 2e 70 43 78 2d 3e 70 43 75 72 73 6f  u.be.pCx->pCurso
26a71 72 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 6e  r;....  /* If an
26a72 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  y of the values 
26a73 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74  are NULL, take t
26a74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0d 0a 20 20 75  he jump. */..  u
26a75 2e 62 65 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62  .be.nField = u.b
26a76 65 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d  e.pCx->pKeyInfo-
26a77 3e 6e 46 69 65 6c 64 3b 0d 0a 20 20 66 6f 72 28  >nField;..  for(
26a78 75 2e 62 65 2e 69 69 3d 30 3b 20 75 2e 62 65 2e  u.be.ii=0; u.be.
26a79 69 69 3c 75 2e 62 65 2e 6e 46 69 65 6c 64 3b 20  ii<u.be.nField; 
26a7a 75 2e 62 65 2e 69 69 2b 2b 29 7b 0d 0a 20 20 20  u.be.ii++){..   
26a7b 20 69 66 28 20 75 2e 62 65 2e 61 4d 78 5b 75 2e   if( u.be.aMx[u.
26a7c 62 65 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d  be.ii].flags & M
26a7d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20  EM_Null ){..    
26a7e 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
26a7f 20 31 3b 0d 0a 20 20 20 20 20 20 75 2e 62 65 2e   1;..      u.be.
26a80 70 43 72 73 72 20 3d 20 30 3b 0d 0a 20 20 20 20  pCrsr = 0;..    
26a81 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
26a82 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
26a83 28 75 2e 62 65 2e 61 4d 78 5b 75 2e 62 65 2e 6e  (u.be.aMx[u.be.n
26a84 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 20 4d  Field].flags & M
26a85 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0d 0a  EM_Null)==0 );..
26a86 0d 0a 20 20 69 66 28 20 75 2e 62 65 2e 70 43 72  ..  if( u.be.pCr
26a87 73 72 21 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a  sr!=0 ){..    /*
26a88 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e   Populate the in
26a89 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20  dex search key. 
26a8a 2a 2f 0d 0a 20 20 20 20 75 2e 62 65 2e 72 2e 70  */..    u.be.r.p
26a8b 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 65 2e 70  KeyInfo = u.be.p
26a8c 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20  Cx->pKeyInfo;.. 
26a8d 20 20 20 75 2e 62 65 2e 72 2e 6e 46 69 65 6c 64     u.be.r.nField
26a8e 20 3d 20 75 2e 62 65 2e 6e 46 69 65 6c 64 20 2b   = u.be.nField +
26a8f 20 31 3b 0d 0a 20 20 20 20 75 2e 62 65 2e 72 2e   1;..    u.be.r.
26a90 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44  flags = UNPACKED
26a91 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 3b 0d  _PREFIX_SEARCH;.
26a92 0a 20 20 20 20 75 2e 62 65 2e 72 2e 61 4d 65 6d  .    u.be.r.aMem
26a93 20 3d 20 75 2e 62 65 2e 61 4d 78 3b 0d 0a 23 69   = u.be.aMx;..#i
26a94 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
26a95 47 0d 0a 20 20 20 20 7b 20 69 6e 74 20 69 3b 20  G..    { int i; 
26a96 66 6f 72 28 69 3d 30 3b 20 69 3c 75 2e 62 65 2e  for(i=0; i<u.be.
26a97 72 2e 6e 46 69 65 6c 64 3b 20 69 2b 2b 29 20 61  r.nField; i++) a
26a98 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69  ssert( memIsVali
26a99 64 28 26 75 2e 62 65 2e 72 2e 61 4d 65 6d 5b 69  d(&u.be.r.aMem[i
26a9a 5d 29 20 29 3b 20 7d 0d 0a 23 65 6e 64 69 66 0d  ]) ); }..#endif.
26a9b 0a 0d 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63  ...    /* Extrac
26a9c 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 75  t the value of u
26a9d 2e 62 65 2e 52 20 66 72 6f 6d 20 72 65 67 69 73  .be.R from regis
26a9e 74 65 72 20 50 33 2e 20 2a 2f 0d 0a 20 20 20 20  ter P3. */..    
26a9f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
26aa0 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0d  tegerify(pIn3);.
26aa1 0a 20 20 20 20 75 2e 62 65 2e 52 20 3d 20 70 49  .    u.be.R = pI
26aa2 6e 33 2d 3e 75 2e 69 3b 0d 0a 0d 0a 20 20 20 20  n3->u.i;....    
26aa3 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 42 2d  /* Search the B-
26aa4 54 72 65 65 20 69 6e 64 65 78 2e 20 49 66 20 6e  Tree index. If n
26aa5 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65  o conflicting re
26aa6 63 6f 72 64 20 69 73 20 66 6f 75 6e 64 2c 20 6a  cord is found, j
26aa7 75 6d 70 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 50  ump..    ** to P
26aa8 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 63 6f  2. Otherwise, co
26aa9 70 79 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  py the rowid of 
26aaa 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20  the conflicting 
26aab 72 65 63 6f 72 64 20 74 6f 0d 0a 20 20 20 20 2a  record to..    *
26aac 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e  * register P3 an
26aad 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  d fall through t
26aae 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
26aaf 75 63 74 69 6f 6e 2e 20 20 2a 2f 0d 0a 20 20 20  uction.  */..   
26ab0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
26ab1 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
26ab2 28 75 2e 62 65 2e 70 43 72 73 72 2c 20 26 75 2e  (u.be.pCrsr, &u.
26ab3 62 65 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62  be.r, 0, 0, &u.b
26ab4 65 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c  e.pCx->seekResul
26ab5 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 28 75 2e  t);..    if( (u.
26ab6 62 65 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50  be.r.flags & UNP
26ab7 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
26ab8 52 43 48 29 20 7c 7c 20 75 2e 62 65 2e 72 2e 72  RCH) || u.be.r.r
26ab9 6f 77 69 64 3d 3d 75 2e 62 65 2e 52 20 29 7b 0d  owid==u.be.R ){.
26aba 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
26abb 3e 70 32 20 2d 20 31 3b 0d 0a 20 20 20 20 7d 65  >p2 - 1;..    }e
26abc 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 49 6e 33  lse{..      pIn3
26abd 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 72 2e 72  ->u.i = u.be.r.r
26abe 6f 77 69 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  owid;..    }..  
26abf 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  }..  break;..}..
26ac0 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74  ../* Opcode: Not
26ac1 45 78 69 73 74 73 20 50 31 20 50 32 20 50 33 20  Exists P1 P2 P3 
26ac2 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 55 73 65 20  * *..**..** Use 
26ac3 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
26ac4 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 6e  egister P3 as an
26ac5 20 69 6e 74 65 67 65 72 20 6b 65 79 2e 20 20 49   integer key.  I
26ac6 66 20 61 20 72 65 63 6f 72 64 20 0d 0a 2a 2a 20  f a record ..** 
26ac7 77 69 74 68 20 74 68 61 74 20 6b 65 79 20 64 6f  with that key do
26ac8 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20  es not exist in 
26ac9 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 68 65  table of P1, the
26aca 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0d 0a  n jump to P2. ..
26acb 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  ** If the record
26acc 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 65   does exist, the
26acd 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20  n fall through. 
26ace 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
26acf 65 66 74 20 0d 0a 2a 2a 20 70 6f 69 6e 74 69 6e  eft ..** pointin
26ad0 67 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  g to the record 
26ad1 69 66 20 69 74 20 65 78 69 73 74 73 2e 0d 0a 2a  if it exists...*
26ad2 2a 0d 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72  *..** The differ
26ad3 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69  ence between thi
26ad4 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20  s operation and 
26ad5 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 61 74  NotFound is that
26ad6 20 74 68 69 73 0d 0a 2a 2a 20 6f 70 65 72 61 74   this..** operat
26ad7 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20  ion assumes the 
26ad8 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65  key is an intege
26ad9 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 73  r and that P1 is
26ada 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 73   a table whereas
26adb 0d 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73  ..** NotFound as
26adc 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62  sumes key is a b
26add 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20  lob constructed 
26ade 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20  from MakeRecord 
26adf 61 6e 64 0d 0a 2a 2a 20 50 31 20 69 73 20 61 6e  and..** P1 is an
26ae0 20 69 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   index...**..** 
26ae1 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
26ae2 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69   NotFound, IsUni
26ae3 71 75 65 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  que..*/..case OP
26ae4 5f 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20 20 20  _NotExists: {   
26ae5 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
26ae6 33 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f 2a 20  3 */..#if 0  /* 
26ae7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
26ae8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20  moved into u.bf 
26ae9 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72  */..  VdbeCursor
26aea 20 2a 70 43 3b 0d 0a 20 20 42 74 43 75 72 73 6f   *pC;..  BtCurso
26aeb 72 20 2a 70 43 72 73 72 3b 0d 0a 20 20 69 6e 74  r *pCrsr;..  int
26aec 20 72 65 73 3b 0d 0a 20 20 75 36 34 20 69 4b 65   res;..  u64 iKe
26aed 79 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  y;..#endif /* lo
26aee 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
26aef 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f  ved into u.bf */
26af0 0d 0a 0d 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d  ....  pIn3 = &aM
26af1 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20  em[pOp->p3];..  
26af2 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
26af3 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
26af4 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
26af5 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
26af6 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d  1<p->nCursor );.
26af7 0a 20 20 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e  .  u.bf.pC = p->
26af8 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  apCsr[pOp->p1];.
26af9 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e  .  assert( u.bf.
26afa 70 43 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  pC!=0 );..  asse
26afb 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54  rt( u.bf.pC->isT
26afc 61 62 6c 65 20 29 3b 0d 0a 20 20 61 73 73 65 72  able );..  asser
26afd 74 28 20 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75  t( u.bf.pC->pseu
26afe 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b  doTableReg==0 );
26aff 0d 0a 20 20 75 2e 62 66 2e 70 43 72 73 72 20 3d  ..  u.bf.pCrsr =
26b00 20 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bf.pC->pCurso
26b01 72 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53  r;..  if( ALWAYS
26b02 28 75 2e 62 66 2e 70 43 72 73 72 21 3d 30 29 20  (u.bf.pCrsr!=0) 
26b03 29 7b 0d 0a 20 20 20 20 75 2e 62 66 2e 72 65 73  ){..    u.bf.res
26b04 20 3d 20 30 3b 0d 0a 20 20 20 20 75 2e 62 66 2e   = 0;..    u.bf.
26b05 69 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 2e 69  iKey = pIn3->u.i
26b06 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
26b07 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
26b08 70 61 63 6b 65 64 28 75 2e 62 66 2e 70 43 72 73  packed(u.bf.pCrs
26b09 72 2c 20 30 2c 20 75 2e 62 66 2e 69 4b 65 79 2c  r, 0, u.bf.iKey,
26b0a 20 30 2c 20 26 75 2e 62 66 2e 72 65 73 29 3b 0d   0, &u.bf.res);.
26b0b 0a 20 20 20 20 75 2e 62 66 2e 70 43 2d 3e 6c 61  .    u.bf.pC->la
26b0c 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e  stRowid = pIn3->
26b0d 75 2e 69 3b 0d 0a 20 20 20 20 75 2e 62 66 2e 70  u.i;..    u.bf.p
26b0e 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
26b0f 3d 20 75 2e 62 66 2e 72 65 73 3d 3d 30 20 3f 31  = u.bf.res==0 ?1
26b10 3a 30 3b 0d 0a 20 20 20 20 75 2e 62 66 2e 70 43  :0;..    u.bf.pC
26b11 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0d 0a  ->nullRow = 0;..
26b12 20 20 20 20 75 2e 62 66 2e 70 43 2d 3e 63 61 63      u.bf.pC->cac
26b13 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
26b14 5f 53 54 41 4c 45 3b 0d 0a 20 20 20 20 75 2e 62  _STALE;..    u.b
26b15 66 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  f.pC->deferredMo
26b16 76 65 74 6f 20 3d 20 30 3b 0d 0a 20 20 20 20 69  veto = 0;..    i
26b17 66 28 20 75 2e 62 66 2e 72 65 73 21 3d 30 20 29  f( u.bf.res!=0 )
26b18 7b 0d 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  {..      pc = pO
26b19 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20 20 20 20  p->p2 - 1;..    
26b1a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70    assert( u.bf.p
26b1b 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d  C->rowidIsValid=
26b1c 3d 30 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  =0 );..    }..  
26b1d 20 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52    u.bf.pC->seekR
26b1e 65 73 75 6c 74 20 3d 20 75 2e 62 66 2e 72 65 73  esult = u.bf.res
26b1f 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
26b20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73   /* This happens
26b21 20 77 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74   when an attempt
26b22 20 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 20   to open a read 
26b23 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 0d 0a 20  cursor on the.. 
26b24 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73     ** sqlite_mas
26b25 74 65 72 20 74 61 62 6c 65 20 72 65 74 75 72 6e  ter table return
26b26 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 0d  s SQLITE_EMPTY..
26b27 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 70 63 20  .    */..    pc 
26b28 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a  = pOp->p2 - 1;..
26b29 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66      assert( u.bf
26b2a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
26b2b 64 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 75 2e 62  d==0 );..    u.b
26b2c 66 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74  f.pC->seekResult
26b2d 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72   = 0;..  }..  br
26b2e 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70  eak;..}..../* Op
26b2f 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 50  code: Sequence P
26b30 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  1 P2 * * *..**..
26b31 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74  ** Find the next
26b32 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 65   available seque
26b33 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 63  nce number for c
26b34 75 72 73 6f 72 20 50 31 2e 0d 0a 2a 2a 20 57 72  ursor P1...** Wr
26b35 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 65  ite the sequence
26b36 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67   number into reg
26b37 69 73 74 65 72 20 50 32 2e 0d 0a 2a 2a 20 54 68  ister P2...** Th
26b38 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
26b39 72 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20  r on the cursor 
26b3a 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61  is incremented a
26b3b 66 74 65 72 20 74 68 69 73 0d 0a 2a 2a 20 69 6e  fter this..** in
26b3c 73 74 72 75 63 74 69 6f 6e 2e 20 20 0d 0a 2a 2f  struction.  ..*/
26b3d 0d 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 65 6e  ..case OP_Sequen
26b3e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ce: {           
26b3f 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
26b40 73 65 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  se */..  assert(
26b41 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
26b42 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
26b43 72 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  r );..  assert( 
26b44 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
26b45 5d 21 3d 30 20 29 3b 0d 0a 20 20 70 4f 75 74 2d  ]!=0 );..  pOut-
26b46 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b  >u.i = p->apCsr[
26b47 70 4f 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f 75  pOp->p1]->seqCou
26b48 6e 74 2b 2b 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d  nt++;..  break;.
26b49 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  .}....../* Opcod
26b4a 65 3a 20 4e 65 77 52 6f 77 69 64 20 50 31 20 50  e: NewRowid P1 P
26b4b 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  2 P3 * *..**..**
26b4c 20 47 65 74 20 61 20 6e 65 77 20 69 6e 74 65 67   Get a new integ
26b4d 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  er record number
26b4e 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 64 22 29   (a.k.a "rowid")
26b4f 20 75 73 65 64 20 61 73 20 74 68 65 20 6b 65 79   used as the key
26b50 20 74 6f 20 61 20 74 61 62 6c 65 2e 0d 0a 2a 2a   to a table...**
26b51 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   The record numb
26b52 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76 69 6f  er is not previo
26b53 75 73 6c 79 20 75 73 65 64 20 61 73 20 61 20 6b  usly used as a k
26b54 65 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ey in the databa
26b55 73 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61  se..** table tha
26b56 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e  t cursor P1 poin
26b57 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 20  ts to.  The new 
26b58 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
26b59 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 77 72 69   written..** wri
26b5a 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tten to register
26b5b 20 50 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20   P2...**..** If 
26b5c 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20  P3>0 then P3 is 
26b5d 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68  a register in th
26b5e 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20  e root frame of 
26b5f 74 68 69 73 20 56 44 42 45 20 74 68 61 74 20 68  this VDBE that h
26b60 6f 6c 64 73 20 0d 0a 2a 2a 20 74 68 65 20 6c 61  olds ..** the la
26b61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79  rgest previously
26b62 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72   generated recor
26b63 64 20 6e 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77  d number. No new
26b64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20   record numbers 
26b65 61 72 65 0d 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20  are..** allowed 
26b66 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20  to be less than 
26b67 74 68 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e  this value. When
26b68 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63   this value reac
26b69 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c  hes its maximum,
26b6a 20 0d 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f   ..** an SQLITE_
26b6b 46 55 4c 4c 20 65 72 72 6f 72 20 69 73 20 67 65  FULL error is ge
26b6c 6e 65 72 61 74 65 64 2e 20 54 68 65 20 50 33 20  nerated. The P3 
26b6d 72 65 67 69 73 74 65 72 20 69 73 20 75 70 64 61  register is upda
26b6e 74 65 64 20 77 69 74 68 20 74 68 65 20 27 0d 0a  ted with the '..
26b6f 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65 63  ** generated rec
26b70 6f 72 64 20 6e 75 6d 62 65 72 2e 20 54 68 69 73  ord number. This
26b71 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69 73   P3 mechanism is
26b72 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d   used to help im
26b73 70 6c 65 6d 65 6e 74 20 74 68 65 0d 0a 2a 2a 20  plement the..** 
26b74 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65  AUTOINCREMENT fe
26b75 61 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  ature...*/..case
26b76 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20   OP_NewRowid: { 
26b77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
26b78 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d  2-prerelease */.
26b79 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
26b7a 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
26b7b 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0d 0a 20   into u.bg */.. 
26b7c 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20   i64 v;         
26b7d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
26b7e 65 77 20 72 6f 77 69 64 20 2a 2f 0d 0a 20 20 56  ew rowid */..  V
26b7f 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20  dbeCursor *pC;  
26b80 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
26b81 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20  of table to get 
26b82 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f  the new rowid */
26b83 0d 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20  ..  int res;    
26b84 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
26b85 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74  sult of an sqlit
26b86 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f  e3BtreeLast() */
26b87 0d 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  ..  int cnt;    
26b88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
26b89 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74  unter to limit t
26b8a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61  he number of sea
26b8b 72 63 68 65 73 20 2a 2f 0d 0a 20 20 4d 65 6d 20  rches */..  Mem 
26b8c 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
26b8d 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
26b8e 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72  olding largest r
26b8f 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43  owid for AUTOINC
26b90 52 45 4d 45 4e 54 20 2a 2f 0d 0a 20 20 56 64 62  REMENT */..  Vdb
26b91 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20  eFrame *pFrame; 
26b92 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d      /* Root fram
26b93 65 20 6f 66 20 56 44 42 45 20 2a 2f 0d 0a 23 65  e of VDBE */..#e
26b94 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
26b95 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
26b96 74 6f 20 75 2e 62 67 20 2a 2f 0d 0a 0d 0a 20 20  to u.bg */....  
26b97 75 2e 62 67 2e 76 20 3d 20 30 3b 0d 0a 20 20 75  u.bg.v = 0;..  u
26b98 2e 62 67 2e 72 65 73 20 3d 20 30 3b 0d 0a 20 20  .bg.res = 0;..  
26b99 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
26b9a 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
26b9b 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20 75  >nCursor );..  u
26b9c 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .bg.pC = p->apCs
26b9d 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 61  r[pOp->p1];..  a
26b9e 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 3d  ssert( u.bg.pC!=
26b9f 30 20 29 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45  0 );..  if( NEVE
26ba0 52 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73  R(u.bg.pC->pCurs
26ba1 6f 72 3d 3d 30 29 20 29 7b 0d 0a 20 20 20 20 2f  or==0) ){..    /
26ba2 2a 20 54 68 65 20 7a 65 72 6f 20 69 6e 69 74 69  * The zero initi
26ba3 61 6c 69 7a 61 74 69 6f 6e 20 61 62 6f 76 65 20  alization above 
26ba4 69 73 20 61 6c 6c 20 74 68 61 74 20 69 73 20 6e  is all that is n
26ba5 65 65 64 65 64 20 2a 2f 0d 0a 20 20 7d 65 6c 73  eeded */..  }els
26ba6 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e  e{..    /* The n
26ba7 65 78 74 20 72 6f 77 69 64 20 6f 72 20 72 65 63  ext rowid or rec
26ba8 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 69 66 66  ord number (diff
26ba9 65 72 65 6e 74 20 74 65 72 6d 73 20 66 6f 72 20  erent terms for 
26baa 74 68 65 20 73 61 6d 65 0d 0a 20 20 20 20 2a 2a  the same..    **
26bab 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 61 69   thing) is obtai
26bac 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 74 65  ned in a two-ste
26bad 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0d 0a 20 20  p algorithm...  
26bae 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 46 69 72    **..    ** Fir
26baf 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f  st we attempt to
26bb0 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73   find the larges
26bb1 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
26bb2 20 61 6e 64 20 61 64 64 20 6f 6e 65 0d 0a 20 20   and add one..  
26bb3 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
26bb4 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
26bb5 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
26bb6 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
26bb7 6d 61 78 69 6d 75 6d 0d 0a 20 20 20 20 2a 2a 20  maximum..    ** 
26bb8 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
26bb9 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c  , we have to fal
26bba 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
26bbb 20 73 65 63 6f 6e 64 0d 0a 20 20 20 20 2a 2a 20   second..    ** 
26bbc 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c  probabilistic al
26bbd 67 6f 72 69 74 68 6d 0d 0a 20 20 20 20 2a 2a 0d  gorithm..    **.
26bbe 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  .    ** The seco
26bbf 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  nd algorithm is 
26bc0 74 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69  to select a rowi
26bc1 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20  d at random and 
26bc2 73 65 65 20 69 66 0d 0a 20 20 20 20 2a 2a 20 69  see if..    ** i
26bc3 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
26bc4 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
26bc5 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65  If it does not e
26bc6 78 69 73 74 2c 20 77 65 20 68 61 76 65 0d 0a 20  xist, we have.. 
26bc7 20 20 20 2a 2a 20 73 75 63 63 65 65 64 65 64 2e     ** succeeded.
26bc8 20 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d 20    If the random 
26bc9 72 6f 77 69 64 20 64 6f 65 73 20 65 78 69 73 74  rowid does exist
26bca 2c 20 77 65 20 73 65 6c 65 63 74 20 61 20 6e 65  , we select a ne
26bcb 77 20 6f 6e 65 0d 0a 20 20 20 20 2a 2a 20 61 6e  w one..    ** an
26bcc 64 20 74 72 79 20 61 67 61 69 6e 2c 20 75 70 20  d try again, up 
26bcd 74 6f 20 31 30 30 20 74 69 6d 65 73 2e 0d 0a 20  to 100 times... 
26bce 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72     */..    asser
26bcf 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 73 54 61  t( u.bg.pC->isTa
26bd0 62 6c 65 20 29 3b 0d 0a 0d 0a 23 69 66 64 65 66  ble );....#ifdef
26bd1 20 53 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f   SQLITE_32BIT_RO
26bd2 57 49 44 0d 0a 23 20 20 20 64 65 66 69 6e 65 20  WID..#   define 
26bd3 4d 41 58 5f 52 4f 57 49 44 20 30 78 37 66 66 66  MAX_ROWID 0x7fff
26bd4 66 66 66 66 0d 0a 23 65 6c 73 65 0d 0a 20 20 20  ffff..#else..   
26bd5 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 70 69 6c 65   /* Some compile
26bd6 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75  rs complain abou
26bd7 74 20 63 6f 6e 73 74 61 6e 74 73 20 6f 66 20 74  t constants of t
26bd8 68 65 20 66 6f 72 6d 20 30 78 37 66 66 66 66 66  he form 0x7fffff
26bd9 66 66 66 66 66 66 66 66 66 66 2e 0d 0a 20 20 20  ffffffffff...   
26bda 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f 6d 70 6c   ** Others compl
26bdb 61 69 6e 20 61 62 6f 75 74 20 30 78 37 66 66 66  ain about 0x7fff
26bdc 66 66 66 66 66 66 66 66 66 66 66 66 66 4c 4c 2e  fffffffffffffLL.
26bdd 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
26bde 6d 61 63 72 6f 20 73 65 65 6d 73 0d 0a 20 20 20  macro seems..   
26bdf 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 20 74   ** to provide t
26be0 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 6c  he constant whil
26be1 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 6f 6d  e making all com
26be2 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0d 0a 20  pilers happy... 
26be3 20 20 20 2a 2f 0d 0a 23 20 20 20 64 65 66 69 6e     */..#   defin
26be4 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
26be5 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
26be6 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
26be7 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0d  64)0xffffffff ).
26be8 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 69  .#endif....    i
26be9 66 28 20 21 75 2e 62 67 2e 70 43 2d 3e 75 73 65  f( !u.bg.pC->use
26bea 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0d 0a  RandomRowid ){..
26beb 20 20 20 20 20 20 75 2e 62 67 2e 76 20 3d 20 73        u.bg.v = s
26bec 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61  qlite3BtreeGetCa
26bed 63 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70  chedRowid(u.bg.p
26bee 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20  C->pCursor);..  
26bef 20 20 20 20 69 66 28 20 75 2e 62 67 2e 76 3d 3d      if( u.bg.v==
26bf0 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63  0 ){..        rc
26bf1 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c   = sqlite3BtreeL
26bf2 61 73 74 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75  ast(u.bg.pC->pCu
26bf3 72 73 6f 72 2c 20 26 75 2e 62 67 2e 72 65 73 29  rsor, &u.bg.res)
26bf4 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
26bf5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
26bf6 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
26bf7 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
26bf8 6f 72 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  or;..        }..
26bf9 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 67          if( u.bg
26bfa 2e 72 65 73 20 29 7b 0d 0a 20 20 20 20 20 20 20  .res ){..       
26bfb 20 20 20 75 2e 62 67 2e 76 20 3d 20 31 3b 20 20     u.bg.v = 1;  
26bfc 20 2f 2a 20 49 4d 50 3a 20 52 2d 36 31 39 31 34   /* IMP: R-61914
26bfd 2d 34 38 30 37 34 20 2a 2f 0d 0a 20 20 20 20 20  -48074 */..     
26bfe 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
26bff 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
26c00 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
26c01 73 56 61 6c 69 64 28 75 2e 62 67 2e 70 43 2d 3e  sValid(u.bg.pC->
26c02 70 43 75 72 73 6f 72 29 20 29 3b 0d 0a 20 20 20  pCursor) );..   
26c03 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
26c04 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
26c05 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bg.pC->pCursor
26c06 2c 20 26 75 2e 62 67 2e 76 29 3b 0d 0a 20 20 20  , &u.bg.v);..   
26c07 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
26c08 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
26c09 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c    /* Cannot fail
26c0a 20 66 6f 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65   following Btree
26c0b 4c 61 73 74 28 29 20 2a 2f 0d 0a 20 20 20 20 20  Last() */..     
26c0c 20 20 20 20 20 69 66 28 20 75 2e 62 67 2e 76 3d       if( u.bg.v=
26c0d 3d 4d 41 58 5f 52 4f 57 49 44 20 29 7b 0d 0a 20  =MAX_ROWID ){.. 
26c0e 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 67 2e             u.bg.
26c0f 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77  pC->useRandomRow
26c10 69 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  id = 1;..       
26c11 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
26c12 20 20 20 20 20 20 20 75 2e 62 67 2e 76 2b 2b 3b         u.bg.v++;
26c13 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 39 35     /* IMP: R-295
26c14 33 38 2d 33 34 39 38 37 20 2a 2f 0d 0a 20 20 20  38-34987 */..   
26c15 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
26c16 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a    }..      }....
26c17 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26c18 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
26c19 54 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  T..      if( pOp
26c1a 2d 3e 70 33 20 29 7b 0d 0a 20 20 20 20 20 20 20  ->p3 ){..       
26c1b 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
26c1c 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65  P3 is a valid me
26c1d 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0d 0a 20  mory cell. */.. 
26c1e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
26c1f 4f 70 2d 3e 70 33 3e 30 20 29 3b 0d 0a 20 20 20  Op->p3>0 );..   
26c20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61       if( p->pFra
26c21 6d 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  me ){..         
26c22 20 66 6f 72 28 75 2e 62 67 2e 70 46 72 61 6d 65   for(u.bg.pFrame
26c23 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 67  =p->pFrame; u.bg
26c24 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74  .pFrame->pParent
26c25 3b 20 75 2e 62 67 2e 70 46 72 61 6d 65 3d 75 2e  ; u.bg.pFrame=u.
26c26 62 67 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  bg.pFrame->pPare
26c27 6e 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  nt);..          
26c28 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 50  /* Assert that P
26c29 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d  3 is a valid mem
26c2a 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0d 0a 20 20  ory cell. */..  
26c2b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
26c2c 70 4f 70 2d 3e 70 33 3c 3d 75 2e 62 67 2e 70 46  pOp->p3<=u.bg.pF
26c2d 72 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a 20  rame->nMem );.. 
26c2e 20 20 20 20 20 20 20 20 20 75 2e 62 67 2e 70 4d           u.bg.pM
26c2f 65 6d 20 3d 20 26 75 2e 62 67 2e 70 46 72 61 6d  em = &u.bg.pFram
26c30 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  e->aMem[pOp->p3]
26c31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
26c32 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {..          /* 
26c33 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69  Assert that P3 i
26c34 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79  s a valid memory
26c35 20 63 65 6c 6c 2e 20 2a 2f 0d 0a 20 20 20 20 20   cell. */..     
26c36 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
26c37 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
26c38 0d 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 67  ..          u.bg
26c39 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f  .pMem = &aMem[pO
26c3a 70 2d 3e 70 33 5d 3b 0d 0a 20 20 20 20 20 20 20  p->p3];..       
26c3b 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61     memAboutToCha
26c3c 6e 67 65 28 70 2c 20 75 2e 62 67 2e 70 4d 65 6d  nge(p, u.bg.pMem
26c3d 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
26c3e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d         assert( m
26c3f 65 6d 49 73 56 61 6c 69 64 28 75 2e 62 67 2e 70  emIsValid(u.bg.p
26c40 4d 65 6d 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 20  Mem) );....     
26c41 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
26c42 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 67 2e  E(pOp->p3, u.bg.
26c43 70 4d 65 6d 29 3b 0d 0a 20 20 20 20 20 20 20 20  pMem);..        
26c44 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
26c45 74 65 67 65 72 69 66 79 28 75 2e 62 67 2e 70 4d  tegerify(u.bg.pM
26c46 65 6d 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73  em);..        as
26c47 73 65 72 74 28 20 28 75 2e 62 67 2e 70 4d 65 6d  sert( (u.bg.pMem
26c48 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
26c49 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 6d  t)!=0 );  /* mem
26c4a 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 6e  (P3) holds an in
26c4b 74 65 67 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20  teger */..      
26c4c 20 20 69 66 28 20 75 2e 62 67 2e 70 4d 65 6d 2d    if( u.bg.pMem-
26c4d 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
26c4e 7c 7c 20 75 2e 62 67 2e 70 43 2d 3e 75 73 65 52  || u.bg.pC->useR
26c4f 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0d 0a 20  andomRowid ){.. 
26c50 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
26c51 4c 49 54 45 5f 46 55 4c 4c 3b 20 20 20 2f 2a 20  LITE_FULL;   /* 
26c52 49 4d 50 3a 20 52 2d 31 32 32 37 35 2d 36 31 33  IMP: R-12275-613
26c53 33 38 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  38 */..         
26c54 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
26c55 74 6f 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 20  to_error;..     
26c56 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66     }..        if
26c57 28 20 75 2e 62 67 2e 76 3c 75 2e 62 67 2e 70 4d  ( u.bg.v<u.bg.pM
26c58 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0d 0a 20 20  em->u.i+1 ){..  
26c59 20 20 20 20 20 20 20 20 75 2e 62 67 2e 76 20 3d          u.bg.v =
26c5a 20 75 2e 62 67 2e 70 4d 65 6d 2d 3e 75 2e 69 20   u.bg.pMem->u.i 
26c5b 2b 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  + 1;..        }.
26c5c 0a 20 20 20 20 20 20 20 20 75 2e 62 67 2e 70 4d  .        u.bg.pM
26c5d 65 6d 2d 3e 75 2e 69 20 3d 20 75 2e 62 67 2e 76  em->u.i = u.bg.v
26c5e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64  ;..      }..#end
26c5f 69 66 0d 0a 0d 0a 20 20 20 20 20 20 73 71 6c 69  if....      sqli
26c60 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
26c61 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 43 2d 3e  dRowid(u.bg.pC->
26c62 70 43 75 72 73 6f 72 2c 20 75 2e 62 67 2e 76 3c  pCursor, u.bg.v<
26c63 4d 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e 62 67  MAX_ROWID ? u.bg
26c64 2e 76 2b 31 20 3a 20 30 29 3b 0d 0a 20 20 20 20  .v+1 : 0);..    
26c65 7d 0d 0a 20 20 20 20 69 66 28 20 75 2e 62 67 2e  }..    if( u.bg.
26c66 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77  pC->useRandomRow
26c67 69 64 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  id ){..      /* 
26c68 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f  IMPLEMENTATION-O
26c69 46 3a 20 52 2d 30 37 36 37 37 2d 34 31 38 38 31  F: R-07677-41881
26c6a 20 49 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   If the largest 
26c6b 52 4f 57 49 44 20 69 73 20 65 71 75 61 6c 20 74  ROWID is equal t
26c6c 6f 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20  o the..      ** 
26c6d 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65  largest possible
26c6e 20 69 6e 74 65 67 65 72 20 28 39 32 32 33 33 37   integer (922337
26c6f 32 30 33 36 38 35 34 37 37 35 38 30 37 29 20 74  2036854775807) t
26c70 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
26c71 0d 0a 20 20 20 20 20 20 2a 2a 20 65 6e 67 69 6e  ..      ** engin
26c72 65 20 73 74 61 72 74 73 20 70 69 63 6b 69 6e 67  e starts picking
26c73 20 70 6f 73 69 74 69 76 65 20 63 61 6e 64 69 64   positive candid
26c74 61 74 65 20 52 4f 57 49 44 73 20 61 74 20 72 61  ate ROWIDs at ra
26c75 6e 64 6f 6d 20 75 6e 74 69 6c 0d 0a 20 20 20 20  ndom until..    
26c76 20 20 2a 2a 20 69 74 20 66 69 6e 64 73 20 6f 6e    ** it finds on
26c77 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 72  e that is not pr
26c78 65 76 69 6f 75 73 6c 79 20 75 73 65 64 2e 20 2a  eviously used. *
26c79 2f 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  /..      assert(
26c7a 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20 20   pOp->p3==0 );  
26c7b 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 62 65 20  /* We cannot be 
26c7c 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  in random rowid 
26c7d 6d 6f 64 65 20 69 66 20 74 68 69 73 20 69 73 0d  mode if this is.
26c7e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
26c7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
26c80 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   an AUTOINCREMEN
26c81 54 20 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 20  T table. */..   
26c82 20 20 20 2f 2a 20 6f 6e 20 74 68 65 20 66 69 72     /* on the fir
26c83 73 74 20 61 74 74 65 6d 70 74 2c 20 73 69 6d 70  st attempt, simp
26c84 6c 79 20 64 6f 20 6f 6e 65 20 6d 6f 72 65 20 74  ly do one more t
26c85 68 61 6e 20 70 72 65 76 69 6f 75 73 20 2a 2f 0d  han previous */.
26c86 0a 20 20 20 20 20 20 75 2e 62 67 2e 76 20 3d 20  .      u.bg.v = 
26c87 6c 61 73 74 52 6f 77 69 64 3b 0d 0a 20 20 20 20  lastRowid;..    
26c88 20 20 75 2e 62 67 2e 76 20 26 3d 20 28 4d 41 58    u.bg.v &= (MAX
26c89 5f 52 4f 57 49 44 3e 3e 31 29 3b 20 2f 2a 20 65  _ROWID>>1); /* e
26c8a 6e 73 75 72 65 20 64 6f 65 73 6e 27 74 20 67 6f  nsure doesn't go
26c8b 20 6e 65 67 61 74 69 76 65 20 2a 2f 0d 0a 20 20   negative */..  
26c8c 20 20 20 20 75 2e 62 67 2e 76 2b 2b 3b 20 2f 2a      u.bg.v++; /*
26c8d 20 65 6e 73 75 72 65 20 6e 6f 6e 2d 7a 65 72 6f   ensure non-zero
26c8e 20 2a 2f 0d 0a 20 20 20 20 20 20 75 2e 62 67 2e   */..      u.bg.
26c8f 63 6e 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  cnt = 0;..      
26c90 77 68 69 6c 65 28 20 20 20 28 28 72 63 20 3d 20  while(   ((rc = 
26c91 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
26c92 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 67 2e  toUnpacked(u.bg.
26c93 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
26c94 28 75 36 34 29 75 2e 62 67 2e 76 2c 0d 0a 20 20  (u64)u.bg.v,..  
26c95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
26c98 2c 20 26 75 2e 62 67 2e 72 65 73 29 29 3d 3d 53  , &u.bg.res))==S
26c99 51 4c 49 54 45 5f 4f 4b 29 0d 0a 20 20 20 20 20  QLITE_OK)..     
26c9a 20 20 20 20 20 20 20 26 26 20 28 75 2e 62 67 2e         && (u.bg.
26c9b 72 65 73 3d 3d 30 29 0d 0a 20 20 20 20 20 20 20  res==0)..       
26c9c 20 20 20 20 20 26 26 20 28 2b 2b 75 2e 62 67 2e       && (++u.bg.
26c9d 63 6e 74 3c 31 30 30 29 29 7b 0d 0a 20 20 20 20  cnt<100)){..    
26c9e 20 20 20 20 2f 2a 20 63 6f 6c 6c 69 73 69 6f 6e      /* collision
26c9f 20 2d 20 74 72 79 20 61 6e 6f 74 68 65 72 20 72   - try another r
26ca0 61 6e 64 6f 6d 20 72 6f 77 69 64 20 2a 2f 0d 0a  andom rowid */..
26ca1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
26ca2 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f  randomness(sizeo
26ca3 66 28 75 2e 62 67 2e 76 29 2c 20 26 75 2e 62 67  f(u.bg.v), &u.bg
26ca4 2e 76 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  .v);..        if
26ca5 28 20 75 2e 62 67 2e 63 6e 74 3c 35 20 29 7b 0d  ( u.bg.cnt<5 ){.
26ca6 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72  .          /* tr
26ca7 79 20 22 73 6d 61 6c 6c 22 20 72 61 6e 64 6f 6d  y "small" random
26ca8 20 72 6f 77 69 64 73 20 66 6f 72 20 74 68 65 20   rowids for the 
26ca9 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 73  initial attempts
26caa 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 75   */..          u
26cab 2e 62 67 2e 76 20 26 3d 20 30 78 66 66 66 66 66  .bg.v &= 0xfffff
26cac 66 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  f;..        }els
26cad 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 75 2e  e{..          u.
26cae 62 67 2e 76 20 26 3d 20 28 4d 41 58 5f 52 4f 57  bg.v &= (MAX_ROW
26caf 49 44 3e 3e 31 29 3b 20 2f 2a 20 65 6e 73 75 72  ID>>1); /* ensur
26cb0 65 20 64 6f 65 73 6e 27 74 20 67 6f 20 6e 65 67  e doesn't go neg
26cb1 61 74 69 76 65 20 2a 2f 0d 0a 20 20 20 20 20 20  ative */..      
26cb2 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 75 2e 62    }..        u.b
26cb3 67 2e 76 2b 2b 3b 20 2f 2a 20 65 6e 73 75 72 65  g.v++; /* ensure
26cb4 20 6e 6f 6e 2d 7a 65 72 6f 20 2a 2f 0d 0a 20 20   non-zero */..  
26cb5 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
26cb6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
26cb7 26 20 75 2e 62 67 2e 72 65 73 3d 3d 30 20 29 7b  & u.bg.res==0 ){
26cb8 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ..        rc = S
26cb9 51 4c 49 54 45 5f 46 55 4c 4c 3b 20 20 20 2f 2a  QLITE_FULL;   /*
26cba 20 49 4d 50 3a 20 52 2d 33 38 32 31 39 2d 35 33   IMP: R-38219-53
26cbb 30 30 32 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  002 */..        
26cbc 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
26cbd 6f 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 20 20  o_error;..      
26cbe 7d 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
26cbf 20 75 2e 62 67 2e 76 3e 30 20 29 3b 20 20 2f 2a   u.bg.v>0 );  /*
26cc0 20 45 56 3a 20 52 2d 34 30 38 31 32 2d 30 33 35   EV: R-40812-035
26cc1 37 30 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20  70 */..    }..  
26cc2 20 20 75 2e 62 67 2e 70 43 2d 3e 72 6f 77 69 64    u.bg.pC->rowid
26cc3 49 73 56 61 6c 69 64 20 3d 20 30 3b 0d 0a 20 20  IsValid = 0;..  
26cc4 20 20 75 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72    u.bg.pC->defer
26cc5 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0d 0a  redMoveto = 0;..
26cc6 20 20 20 20 75 2e 62 67 2e 70 43 2d 3e 63 61 63      u.bg.pC->cac
26cc7 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
26cc8 5f 53 54 41 4c 45 3b 0d 0a 20 20 7d 0d 0a 20 20  _STALE;..  }..  
26cc9 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 67  pOut->u.i = u.bg
26cca 2e 76 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d  .v;..  break;..}
26ccb 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  ..../* Opcode: I
26ccc 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 50  nsert P1 P2 P3 P
26ccd 34 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69  4 P5..**..** Wri
26cce 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f  te an entry into
26ccf 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 63 75   the table of cu
26cd0 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 77 20  rsor P1.  A new 
26cd1 65 6e 74 72 79 20 69 73 0d 0a 2a 2a 20 63 72 65  entry is..** cre
26cd2 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73 6e  ated if it doesn
26cd3 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  't already exist
26cd4 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f 72   or the data for
26cd5 20 61 6e 20 65 78 69 73 74 69 6e 67 0d 0a 2a 2a   an existing..**
26cd6 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72   entry is overwr
26cd7 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61  itten.  The data
26cd8 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 4d 45   is the value ME
26cd9 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e  M_Blob stored in
26cda 20 72 65 67 69 73 74 65 72 0d 0a 2a 2a 20 6e 75   register..** nu
26cdb 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79  mber P2. The key
26cdc 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
26cdd 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b  gister P3. The k
26cde 65 79 20 6d 75 73 74 0d 0a 2a 2a 20 62 65 20 61  ey must..** be a
26cdf 20 4d 45 4d 5f 49 6e 74 2e 0d 0a 2a 2a 0d 0a 2a   MEM_Int...**..*
26ce0 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f  * If the OPFLAG_
26ce1 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20  NCHANGE flag of 
26ce2 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  P5 is set, then 
26ce3 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63  the row change c
26ce4 6f 75 6e 74 20 69 73 0d 0a 2a 2a 20 69 6e 63 72  ount is..** incr
26ce5 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69  emented (otherwi
26ce6 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 68 65  se not).  If the
26ce7 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49   OPFLAG_LASTROWI
26ce8 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 73 20  D flag of P5 is 
26ce9 73 65 74 2c 0d 0a 2a 2a 20 74 68 65 6e 20 72 6f  set,..** then ro
26cea 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 6f  wid is stored fo
26ceb 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 74  r subsequent ret
26cec 75 72 6e 20 62 79 20 74 68 65 0d 0a 2a 2a 20 73  urn by the..** s
26ced 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
26cee 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74  rt_rowid() funct
26cef 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69  ion (otherwise i
26cf0 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29  t is unmodified)
26cf1 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
26cf2 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52   OPFLAG_USESEEKR
26cf3 45 53 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35  ESULT flag of P5
26cf4 20 69 73 20 73 65 74 20 61 6e 64 20 69 66 20 74   is set and if t
26cf5 68 65 20 72 65 73 75 6c 74 20 6f 66 0d 0a 2a 2a  he result of..**
26cf6 20 74 68 65 20 6c 61 73 74 20 73 65 65 6b 20 6f   the last seek o
26cf7 70 65 72 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74  peration (OP_Not
26cf8 45 78 69 73 74 73 29 20 77 61 73 20 61 20 73 75  Exists) was a su
26cf9 63 63 65 73 73 2c 20 74 68 65 6e 20 74 68 69 73  ccess, then this
26cfa 0d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 77  ..** operation w
26cfb 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20  ill not attempt 
26cfc 74 6f 20 66 69 6e 64 20 74 68 65 20 61 70 70 72  to find the appr
26cfd 6f 70 72 69 61 74 65 20 72 6f 77 20 62 65 66 6f  opriate row befo
26cfe 72 65 20 64 6f 69 6e 67 0d 0a 2a 2a 20 74 68 65  re doing..** the
26cff 20 69 6e 73 65 72 74 20 62 75 74 20 77 69 6c 6c   insert but will
26d00 20 69 6e 73 74 65 61 64 20 6f 76 65 72 77 72 69   instead overwri
26d01 74 65 20 74 68 65 20 72 6f 77 20 74 68 61 74 20  te the row that 
26d02 74 68 65 20 63 75 72 73 6f 72 20 69 73 0d 0a 2a  the cursor is..*
26d03 2a 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  * currently poin
26d04 74 69 6e 67 20 74 6f 2e 20 20 50 72 65 73 75 6d  ting to.  Presum
26d05 61 62 6c 79 2c 20 74 68 65 20 70 72 69 6f 72 20  ably, the prior 
26d06 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 70 63  OP_NotExists opc
26d07 6f 64 65 0d 0a 2a 2a 20 68 61 73 20 61 6c 72 65  ode..** has alre
26d08 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 74  ady positioned t
26d09 68 65 20 63 75 72 73 6f 72 20 63 6f 72 72 65 63  he cursor correc
26d0a 74 6c 79 2e 20 20 54 68 69 73 20 69 73 20 61 6e  tly.  This is an
26d0b 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0d 0a 2a   optimization..*
26d0c 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 20 70 65  * that boosts pe
26d0d 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 61 76 6f  rformance by avo
26d0e 69 64 69 6e 67 20 72 65 64 75 6e 64 61 6e 74 20  iding redundant 
26d0f 73 65 65 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  seeks...**..** I
26d10 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 55  f the OPFLAG_ISU
26d11 50 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 65  PDATE flag is se
26d12 74 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63  t, then this opc
26d13 6f 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 61  ode is part of a
26d14 6e 0d 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65  n..** UPDATE ope
26d15 72 61 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69  ration.  Otherwi
26d16 73 65 20 28 69 66 20 74 68 65 20 66 6c 61 67 20  se (if the flag 
26d17 69 73 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74  is clear) then t
26d18 68 69 73 20 6f 70 63 6f 64 65 0d 0a 2a 2a 20 69  his opcode..** i
26d19 73 20 70 61 72 74 20 6f 66 20 61 6e 20 49 4e 53  s part of an INS
26d1a 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ERT operation.  
26d1b 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69  The difference i
26d1c 73 20 6f 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74  s only important
26d1d 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 75 70 64 61   to..** the upda
26d1e 74 65 20 68 6f 6f 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a  te hook...**..**
26d1f 20 50 61 72 61 6d 65 74 65 72 20 50 34 20 6d 61   Parameter P4 ma
26d20 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72  y point to a str
26d21 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
26d22 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f  he table-name, o
26d23 72 0d 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  r..** may be NUL
26d24 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  L. If it is not 
26d25 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 75  NULL, then the u
26d26 70 64 61 74 65 2d 68 6f 6f 6b 20 0d 0a 2a 2a 20  pdate-hook ..** 
26d27 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65  (sqlite3.xUpdate
26d28 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76  Callback) is inv
26d29 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  oked following a
26d2a 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65   successful inse
26d2b 72 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 28 57 41 52  rt...**..** (WAR
26d2c 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31  NING/TODO: If P1
26d2d 20 69 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72   is a pseudo-cur
26d2e 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 64 79  sor and P2 is dy
26d2f 6e 61 6d 69 63 61 6c 6c 79 0d 0a 2a 2a 20 61 6c  namically..** al
26d30 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 6f 77  located, then ow
26d31 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 69 73  nership of P2 is
26d32 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 6f 20   transferred to 
26d33 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 73 6f  the pseudo-curso
26d34 72 0d 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 74  r..** and regist
26d35 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 70  er P2 becomes ep
26d36 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 65  hemeral.  If the
26d37 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e 67   cursor is chang
26d38 65 64 2c 20 74 68 65 0d 0a 2a 2a 20 76 61 6c 75  ed, the..** valu
26d39 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32  e of register P2
26d3a 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67   will then chang
26d3b 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  e.  Make sure th
26d3c 69 73 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a 20  is does not..** 
26d3d 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65  cause any proble
26d3e 6d 73 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ms.)..**..** Thi
26d3f 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e  s instruction on
26d40 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c  ly works on tabl
26d41 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c  es.  The equival
26d42 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0d  ent instruction.
26d43 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20  .** for indices 
26d44 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e  is OP_IdxInsert.
26d45 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ..*/../* Opcode:
26d46 20 49 6e 73 65 72 74 49 6e 74 20 50 31 20 50 32   InsertInt P1 P2
26d47 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a 0d 0a 2a   P3 P4 P5..**..*
26d48 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 65 78 61  * This works exa
26d49 63 74 6c 79 20 6c 69 6b 65 20 4f 50 5f 49 6e 73  ctly like OP_Ins
26d4a 65 72 74 20 65 78 63 65 70 74 20 74 68 61 74 20  ert except that 
26d4b 74 68 65 20 6b 65 79 20 69 73 20 74 68 65 0d 0a  the key is the..
26d4c 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
26d4d 20 50 33 2c 20 6e 6f 74 20 74 68 65 20 76 61 6c   P3, not the val
26d4e 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
26d4f 72 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  r stored in regi
26d50 73 74 65 72 20 50 33 2e 0d 0a 2a 2f 0d 0a 63 61  ster P3...*/..ca
26d51 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0d 0a  se OP_Insert: ..
26d52 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e  case OP_InsertIn
26d53 74 3a 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20  t: {..#if 0  /* 
26d54 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
26d55 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 68 20  moved into u.bh 
26d56 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61  */..  Mem *pData
26d57 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63  ;       /* MEM c
26d58 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61  ell holding data
26d59 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20   for the record 
26d5a 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
26d5b 2f 0d 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20  /..  Mem *pKey; 
26d5c 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65         /* MEM ce
26d5d 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20  ll holding key  
26d5e 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a  for the record *
26d5f 2f 0d 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20  /..  i64 iKey;  
26d60 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
26d61 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b  teger ROWID or k
26d62 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ey for the recor
26d63 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  d to be inserted
26d64 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73 6f   */..  VdbeCurso
26d65 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73  r *pC;   /* Curs
26d66 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f  or to table into
26d67 20 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73   which insert is
26d68 20 77 72 69 74 74 65 6e 20 2a 2f 0d 0a 20 20 69   written */..  i
26d69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20  nt nZero;       
26d6a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
26d6b 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro-bytes to appe
26d6c 6e 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 65 65  nd */..  int see
26d6d 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65  kResult;   /* Re
26d6e 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65  sult of prior se
26d6f 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53  ek or 0 if no US
26d70 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67  ESEEKRESULT flag
26d71 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
26d72 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 61  r *zDb;  /* data
26d73 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 64  base name - used
26d74 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 68   by the update h
26d75 6f 6f 6b 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ook */..  const 
26d76 63 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54  char *zTbl; /* T
26d77 61 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64  able name - used
26d78 20 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68   by the opdate h
26d79 6f 6f 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70  ook */..  int op
26d7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
26d7b 70 63 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65  pcode for update
26d7c 20 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50   hook: SQLITE_UP
26d7d 44 41 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49  DATE or SQLITE_I
26d7e 4e 53 45 52 54 20 2a 2f 0d 0a 23 65 6e 64 69 66  NSERT */..#endif
26d7f 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
26d80 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
26d81 2e 62 68 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 62 68  .bh */....  u.bh
26d82 2e 70 44 61 74 61 20 3d 20 26 61 4d 65 6d 5b 70  .pData = &aMem[p
26d83 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 61 73 73 65  Op->p2];..  asse
26d84 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
26d85 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
26d86 72 73 6f 72 20 29 3b 0d 0a 20 20 61 73 73 65 72  rsor );..  asser
26d87 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75 2e  t( memIsValid(u.
26d88 62 68 2e 70 44 61 74 61 29 20 29 3b 0d 0a 20 20  bh.pData) );..  
26d89 75 2e 62 68 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bh.pC = p->apC
26d8a 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  sr[pOp->p1];..  
26d8b 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 21  assert( u.bh.pC!
26d8c 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
26d8d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bh.pC->pCurso
26d8e 72 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  r!=0 );..  asser
26d8f 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 73 65 75  t( u.bh.pC->pseu
26d90 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b  doTableReg==0 );
26d91 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68  ..  assert( u.bh
26d92 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0d  .pC->isTable );.
26d93 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
26d94 45 28 70 4f 70 2d 3e 70 32 2c 20 75 2e 62 68 2e  E(pOp->p2, u.bh.
26d95 70 44 61 74 61 29 3b 0d 0a 0d 0a 20 20 69 66 28  pData);....  if(
26d96 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
26d97 5f 49 6e 73 65 72 74 20 29 7b 0d 0a 20 20 20 20  _Insert ){..    
26d98 75 2e 62 68 2e 70 4b 65 79 20 3d 20 26 61 4d 65  u.bh.pKey = &aMe
26d99 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 20  m[pOp->p3];..   
26d9a 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 4b   assert( u.bh.pK
26d9b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ey->flags & MEM_
26d9c 49 6e 74 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  Int );..    asse
26d9d 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75  rt( memIsValid(u
26d9e 2e 62 68 2e 70 4b 65 79 29 20 29 3b 0d 0a 20 20  .bh.pKey) );..  
26d9f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
26da0 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 68 2e 70  (pOp->p3, u.bh.p
26da1 4b 65 79 29 3b 0d 0a 20 20 20 20 75 2e 62 68 2e  Key);..    u.bh.
26da2 69 4b 65 79 20 3d 20 75 2e 62 68 2e 70 4b 65 79  iKey = u.bh.pKey
26da3 2d 3e 75 2e 69 3b 0d 0a 20 20 7d 65 6c 73 65 7b  ->u.i;..  }else{
26da4 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ..    assert( pO
26da5 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e  p->opcode==OP_In
26da6 73 65 72 74 49 6e 74 20 29 3b 0d 0a 20 20 20 20  sertInt );..    
26da7 75 2e 62 68 2e 69 4b 65 79 20 3d 20 70 4f 70 2d  u.bh.iKey = pOp-
26da8 3e 70 33 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  >p3;..  }....  i
26da9 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46  f( pOp->p5 & OPF
26daa 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d  LAG_NCHANGE ) p-
26dab 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0d 0a 20 20 69  >nChange++;..  i
26dac 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46  f( pOp->p5 & OPF
26dad 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20  LAG_LASTROWID ) 
26dae 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  db->lastRowid = 
26daf 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 62 68  lastRowid = u.bh
26db0 2e 69 4b 65 79 3b 0d 0a 20 20 69 66 28 20 75 2e  .iKey;..  if( u.
26db1 62 68 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20  bh.pData->flags 
26db2 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a 20  & MEM_Null ){.. 
26db3 20 20 20 75 2e 62 68 2e 70 44 61 74 61 2d 3e 7a     u.bh.pData->z
26db4 20 3d 20 30 3b 0d 0a 20 20 20 20 75 2e 62 68 2e   = 0;..    u.bh.
26db5 70 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0d 0a 20  pData->n = 0;.. 
26db6 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73   }else{..    ass
26db7 65 72 74 28 20 75 2e 62 68 2e 70 44 61 74 61 2d  ert( u.bh.pData-
26db8 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c  >flags & (MEM_Bl
26db9 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0d 0a  ob|MEM_Str) );..
26dba 20 20 7d 0d 0a 20 20 75 2e 62 68 2e 73 65 65 6b    }..  u.bh.seek
26dbb 52 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d 3e  Result = ((pOp->
26dbc 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 53  p5 & OPFLAG_USES
26dbd 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e 62  EEKRESULT) ? u.b
26dbe 68 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74  h.pC->seekResult
26dbf 20 3a 20 30 29 3b 0d 0a 20 20 69 66 28 20 75 2e   : 0);..  if( u.
26dc0 62 68 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20  bh.pData->flags 
26dc1 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0d 0a 20  & MEM_Zero ){.. 
26dc2 20 20 20 75 2e 62 68 2e 6e 5a 65 72 6f 20 3d 20     u.bh.nZero = 
26dc3 75 2e 62 68 2e 70 44 61 74 61 2d 3e 75 2e 6e 5a  u.bh.pData->u.nZ
26dc4 65 72 6f 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ero;..  }else{..
26dc5 20 20 20 20 75 2e 62 68 2e 6e 5a 65 72 6f 20 3d      u.bh.nZero =
26dc6 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69   0;..  }..  sqli
26dc7 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
26dc8 64 52 6f 77 69 64 28 75 2e 62 68 2e 70 43 2d 3e  dRowid(u.bh.pC->
26dc9 70 43 75 72 73 6f 72 2c 20 30 29 3b 0d 0a 20 20  pCursor, 0);..  
26dca 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
26dcb 65 49 6e 73 65 72 74 28 75 2e 62 68 2e 70 43 2d  eInsert(u.bh.pC-
26dcc 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62  >pCursor, 0, u.b
26dcd 68 2e 69 4b 65 79 2c 0d 0a 20 20 20 20 20 20 20  h.iKey,..       
26dce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26dcf 20 20 20 75 2e 62 68 2e 70 44 61 74 61 2d 3e 7a     u.bh.pData->z
26dd0 2c 20 75 2e 62 68 2e 70 44 61 74 61 2d 3e 6e 2c  , u.bh.pData->n,
26dd1 20 75 2e 62 68 2e 6e 5a 65 72 6f 2c 0d 0a 20 20   u.bh.nZero,..  
26dd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26dd3 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35 20          pOp->p5 
26dd4 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 2c  & OPFLAG_APPEND,
26dd5 20 75 2e 62 68 2e 73 65 65 6b 52 65 73 75 6c 74   u.bh.seekResult
26dd6 0d 0a 20 20 29 3b 0d 0a 20 20 75 2e 62 68 2e 70  ..  );..  u.bh.p
26dd7 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
26dd8 3d 20 30 3b 0d 0a 20 20 75 2e 62 68 2e 70 43 2d  = 0;..  u.bh.pC-
26dd9 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
26dda 3d 20 30 3b 0d 0a 20 20 75 2e 62 68 2e 70 43 2d  = 0;..  u.bh.pC-
26ddb 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
26ddc 41 43 48 45 5f 53 54 41 4c 45 3b 0d 0a 0d 0a 20  ACHE_STALE;.... 
26ddd 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75   /* Invoke the u
26dde 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65  pdate-hook if re
26ddf 71 75 69 72 65 64 2e 20 2a 2f 0d 0a 20 20 69 66  quired. */..  if
26de0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
26de1 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  && db->xUpdateCa
26de2 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70  llback && pOp->p
26de3 34 2e 7a 20 29 7b 0d 0a 20 20 20 20 75 2e 62 68  4.z ){..    u.bh
26de4 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75  .zDb = db->aDb[u
26de5 2e 62 68 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61  .bh.pC->iDb].zNa
26de6 6d 65 3b 0d 0a 20 20 20 20 75 2e 62 68 2e 7a 54  me;..    u.bh.zT
26de7 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0d  bl = pOp->p4.z;.
26de8 0a 20 20 20 20 75 2e 62 68 2e 6f 70 20 3d 20 28  .    u.bh.op = (
26de9 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41  (pOp->p5 & OPFLA
26dea 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53 51  G_ISUPDATE) ? SQ
26deb 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51  LITE_UPDATE : SQ
26dec 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0d 0a 20  LITE_INSERT);.. 
26ded 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e     assert( u.bh.
26dee 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0d 0a  pC->isTable );..
26def 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43      db->xUpdateC
26df0 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64  allback(db->pUpd
26df1 61 74 65 41 72 67 2c 20 75 2e 62 68 2e 6f 70 2c  ateArg, u.bh.op,
26df2 20 75 2e 62 68 2e 7a 44 62 2c 20 75 2e 62 68 2e   u.bh.zDb, u.bh.
26df3 7a 54 62 6c 2c 20 75 2e 62 68 2e 69 4b 65 79 29  zTbl, u.bh.iKey)
26df4 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ;..    assert( u
26df5 2e 62 68 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29  .bh.pC->iDb>=0 )
26df6 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b  ;..  }..  break;
26df7 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
26df8 3a 20 44 65 6c 65 74 65 20 50 31 20 50 32 20 2a  : Delete P1 P2 *
26df9 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 44 65   P4 *..**..** De
26dfa 6c 65 74 65 20 74 68 65 20 72 65 63 6f 72 64 20  lete the record 
26dfb 61 74 20 77 68 69 63 68 20 74 68 65 20 50 31 20  at which the P1 
26dfc 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e  cursor is curren
26dfd 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0d 0a 2a  tly pointing...*
26dfe 2a 0d 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  *..** The cursor
26dff 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f   will be left po
26e00 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 72  inting at either
26e01 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 65   the next or the
26e02 20 70 72 65 76 69 6f 75 73 0d 0a 2a 2a 20 72 65   previous..** re
26e03 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c  cord in the tabl
26e04 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74  e. If it is left
26e05 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
26e06 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68   next record, th
26e07 65 6e 0d 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20  en..** the next 
26e08 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Next instruction
26e09 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70   will be a no-op
26e0a 2e 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f  .  Hence it is O
26e0b 4b 20 74 6f 20 64 65 6c 65 74 65 0d 0a 2a 2a 20  K to delete..** 
26e0c 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69  a record from wi
26e0d 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f  thin an Next loo
26e0e 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  p...**..** If th
26e0f 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  e OPFLAG_NCHANGE
26e10 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73   flag of P2 is s
26e11 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  et, then the row
26e12 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73   change count is
26e13 0d 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64  ..** incremented
26e14 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29   (otherwise not)
26e15 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 6d 75 73  ...**..** P1 mus
26e16 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f 2d  t not be pseudo-
26e17 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20 74  table.  It has t
26e18 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 62 6c  o be a real tabl
26e19 65 20 77 69 74 68 0d 0a 2a 2a 20 6d 75 6c 74 69  e with..** multi
26e1a 70 6c 65 20 72 6f 77 73 2e 0d 0a 2a 2a 0d 0a 2a  ple rows...**..*
26e1b 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e  * If P4 is not N
26e1c 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ULL, then it is 
26e1d 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
26e1e 74 61 62 6c 65 20 74 68 61 74 20 50 31 20 69 73  table that P1 is
26e1f 0d 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ..** pointing to
26e20 2e 20 20 54 68 65 20 75 70 64 61 74 65 20 68 6f  .  The update ho
26e21 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  ok will be invok
26e22 65 64 2c 20 69 66 20 69 74 20 65 78 69 73 74 73  ed, if it exists
26e23 2e 0d 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e  ...** If P4 is n
26e24 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  ot NULL then the
26e25 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20   P1 cursor must 
26e26 68 61 76 65 20 62 65 65 6e 20 70 6f 73 69 74 69  have been positi
26e27 6f 6e 65 64 0d 0a 2a 2a 20 75 73 69 6e 67 20 4f  oned..** using O
26e28 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 6f 72  P_NotFound prior
26e29 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69   to invoking thi
26e2a 73 20 6f 70 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 63  s opcode...*/..c
26e2b 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b  ase OP_Delete: {
26e2c 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
26e2d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
26e2e 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0d 0a  d into u.bi */..
26e2f 20 20 69 36 34 20 69 4b 65 79 3b 0d 0a 20 20 56    i64 iKey;..  V
26e30 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d 0a  dbeCursor *pC;..
26e31 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
26e32 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
26e33 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0d 0a 0d 0a  into u.bi */....
26e34 20 20 75 2e 62 69 2e 69 4b 65 79 20 3d 20 30 3b    u.bi.iKey = 0;
26e35 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
26e36 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
26e37 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d  1<p->nCursor );.
26e38 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 70 2d 3e  .  u.bi.pC = p->
26e39 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  apCsr[pOp->p1];.
26e3a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e  .  assert( u.bi.
26e3b 70 43 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  pC!=0 );..  asse
26e3c 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 43 75  rt( u.bi.pC->pCu
26e3d 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f  rsor!=0 );  /* O
26e3e 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65  nly valid for re
26e3f 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73  al tables, no ps
26e40 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0d 0a 0d  eudotables */...
26e41 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 64  .  /* If the upd
26e42 61 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65  ate-hook will be
26e43 20 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 75 2e   invoked, set u.
26e44 62 69 2e 69 4b 65 79 20 74 6f 20 74 68 65 20 72  bi.iKey to the r
26e45 6f 77 69 64 20 6f 66 20 74 68 65 0d 0a 20 20 2a  owid of the..  *
26e46 2a 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  * row being dele
26e47 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ted...  */..  if
26e48 28 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c  ( db->xUpdateCal
26e49 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34  lback && pOp->p4
26e4a 2e 7a 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  .z ){..    asser
26e4b 74 28 20 75 2e 62 69 2e 70 43 2d 3e 69 73 54 61  t( u.bi.pC->isTa
26e4c 62 6c 65 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ble );..    asse
26e4d 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77  rt( u.bi.pC->row
26e4e 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f 2a  idIsValid );  /*
26e4f 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 62   lastRowid set b
26e50 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e 6f  y previous OP_No
26e51 74 46 6f 75 6e 64 20 2a 2f 0d 0a 20 20 20 20 75  tFound */..    u
26e52 2e 62 69 2e 69 4b 65 79 20 3d 20 75 2e 62 69 2e  .bi.iKey = u.bi.
26e53 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0d 0a  pC->lastRowid;..
26e54 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20    }....  /* The 
26e55 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65  OP_Delete opcode
26e56 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20   always follows 
26e57 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  an OP_NotExists 
26e58 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0d 0a 20  or OP_Last or.. 
26e59 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e   ** OP_Column on
26e5a 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20   the same table 
26e5b 77 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65  without any inte
26e5c 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  rvening operatio
26e5d 6e 73 20 74 68 61 74 0d 0a 20 20 2a 2a 20 6d 69  ns that..  ** mi
26e5e 67 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61  ght move or inva
26e5f 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f  lidate the curso
26e60 72 2e 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72  r.  Hence cursor
26e61 20 75 2e 62 69 2e 70 43 20 69 73 20 61 6c 77 61   u.bi.pC is alwa
26e62 79 73 20 70 6f 69 6e 74 69 6e 67 0d 0a 20 20 2a  ys pointing..  *
26e63 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20  * to the row to 
26e64 62 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74  be deleted and t
26e65 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  he sqlite3VdbeCu
26e66 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65  rsorMoveto() ope
26e67 72 61 74 69 6f 6e 0d 0a 20 20 2a 2a 20 62 65 6c  ration..  ** bel
26e68 6f 77 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e  ow is always a n
26e69 6f 2d 6f 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20  o-op and cannot 
26e6a 66 61 69 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72  fail.  We will r
26e6b 75 6e 20 69 74 20 61 6e 79 68 6f 77 2c 20 74 68  un it anyhow, th
26e6c 6f 75 67 68 2c 0d 0a 20 20 2a 2a 20 74 6f 20 67  ough,..  ** to g
26e6d 75 61 72 64 20 61 67 61 69 6e 73 74 20 66 75 74  uard against fut
26e6e 75 72 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74  ure changes to t
26e6f 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
26e70 72 2e 0d 0a 20 20 2a 2a 2f 0d 0a 20 20 61 73 73  r...  **/..  ass
26e71 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 64 65  ert( u.bi.pC->de
26e72 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
26e73 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  );..  rc = sqlit
26e74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
26e75 74 6f 28 75 2e 62 69 2e 70 43 29 3b 0d 0a 20 20  to(u.bi.pC);..  
26e76 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51  if( NEVER(rc!=SQ
26e77 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20  LITE_OK) ) goto 
26e78 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
26e79 6f 72 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33  or;....  sqlite3
26e7a 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f  BtreeSetCachedRo
26e7b 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70 43 75  wid(u.bi.pC->pCu
26e7c 72 73 6f 72 2c 20 30 29 3b 0d 0a 20 20 72 63 20  rsor, 0);..  rc 
26e7d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  = sqlite3BtreeDe
26e7e 6c 65 74 65 28 75 2e 62 69 2e 70 43 2d 3e 70 43  lete(u.bi.pC->pC
26e7f 75 72 73 6f 72 29 3b 0d 0a 20 20 75 2e 62 69 2e  ursor);..  u.bi.
26e80 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
26e81 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0d 0a  = CACHE_STALE;..
26e82 0d 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
26e83 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66  e update-hook if
26e84 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0d 0a 20   required. */.. 
26e85 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
26e86 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74  OK && db->xUpdat
26e87 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
26e88 2d 3e 70 34 2e 7a 20 29 7b 0d 0a 20 20 20 20 63  ->p4.z ){..    c
26e89 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
26e8a 20 64 62 2d 3e 61 44 62 5b 75 2e 62 69 2e 70 43   db->aDb[u.bi.pC
26e8b 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  ->iDb].zName;.. 
26e8c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
26e8d 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  Tbl = pOp->p4.z;
26e8e 0d 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74  ..    db->xUpdat
26e8f 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55  eCallback(db->pU
26e90 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54 45  pdateArg, SQLITE
26e91 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54  _DELETE, zDb, zT
26e92 62 6c 2c 20 75 2e 62 69 2e 69 4b 65 79 29 3b 0d  bl, u.bi.iKey);.
26e93 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
26e94 69 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0d  i.pC->iDb>=0 );.
26e95 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 4f 70 2d  .  }..  if( pOp-
26e96 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48  >p2 & OPFLAG_NCH
26e97 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67  ANGE ) p->nChang
26e98 65 2b 2b 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  e++;..  break;..
26e99 7d 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  }../* Opcode: Re
26e9a 73 65 74 43 6f 75 6e 74 20 2a 20 2a 20 2a 20 2a  setCount * * * *
26e9b 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 76   *..**..** The v
26e9c 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e  alue of the chan
26e9d 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 63 6f  ge counter is co
26e9e 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  pied to the data
26e9f 62 61 73 65 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20  base handle..** 
26ea0 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 28  change counter (
26ea1 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73  returned by subs
26ea2 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
26ea3 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
26ea4 29 29 2e 0d 0a 2a 2a 20 54 68 65 6e 20 74 68 65  ))...** Then the
26ea5 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68   VMs internal ch
26ea6 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 72 65 73  ange counter res
26ea7 65 74 73 20 74 6f 20 30 2e 0d 0a 2a 2a 20 54 68  ets to 0...** Th
26ea8 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72  is is used by tr
26ea9 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0d  igger programs..
26eaa 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 52 65 73  .*/..case OP_Res
26eab 65 74 43 6f 75 6e 74 3a 20 7b 0d 0a 20 20 73 71  etCount: {..  sq
26eac 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
26ead 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
26eae 67 65 29 3b 0d 0a 20 20 70 2d 3e 6e 43 68 61 6e  ge);..  p->nChan
26eaf 67 65 20 3d 20 30 3b 0d 0a 20 20 62 72 65 61 6b  ge = 0;..  break
26eb0 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
26eb1 65 3a 20 53 6f 72 74 65 72 43 6f 6d 70 61 72 65  e: SorterCompare
26eb2 20 50 31 20 50 32 20 50 33 0d 0a 2a 2a 0d 0a 2a   P1 P2 P3..**..*
26eb3 2a 20 50 31 20 69 73 20 61 20 73 6f 72 74 65 72  * P1 is a sorter
26eb4 20 63 75 72 73 6f 72 2e 20 54 68 69 73 20 69 6e   cursor. This in
26eb5 73 74 72 75 63 74 69 6f 6e 20 63 6f 6d 70 61 72  struction compar
26eb6 65 73 20 74 68 65 20 72 65 63 6f 72 64 20 62 6c  es the record bl
26eb7 6f 62 20 69 6e 20 0d 0a 2a 2a 20 72 65 67 69 73  ob in ..** regis
26eb8 74 65 72 20 50 33 20 77 69 74 68 20 74 68 65 20  ter P3 with the 
26eb9 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 73  entry that the s
26eba 6f 72 74 65 72 20 63 75 72 73 6f 72 20 63 75 72  orter cursor cur
26ebb 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f  rently points to
26ebc 2e 0d 0a 2a 2a 20 49 66 2c 20 65 78 63 6c 75 64  ...** If, exclud
26ebd 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 66 69  ing the rowid fi
26ebe 65 6c 64 73 20 61 74 20 74 68 65 20 65 6e 64 2c  elds at the end,
26ebf 20 74 68 65 20 74 77 6f 20 72 65 63 6f 72 64 73   the two records
26ec0 20 61 72 65 20 61 20 6d 61 74 63 68 2c 0d 0a 2a   are a match,..*
26ec1 2a 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  * fall through t
26ec2 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
26ec3 75 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73  uction. Otherwis
26ec4 65 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72  e, jump to instr
26ec5 75 63 74 69 6f 6e 20 50 32 2e 0d 0a 2a 2f 0d 0a  uction P2...*/..
26ec6 63 61 73 65 20 4f 50 5f 53 6f 72 74 65 72 43 6f  case OP_SorterCo
26ec7 6d 70 61 72 65 3a 20 7b 0d 0a 23 69 66 20 30 20  mpare: {..#if 0 
26ec8 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
26ec9 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
26eca 2e 62 6a 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75  .bj */..  VdbeCu
26ecb 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 69 6e 74  rsor *pC;..  int
26ecc 20 72 65 73 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a   res;..#endif /*
26ecd 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
26ece 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a   moved into u.bj
26ecf 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 62 6a 2e 70 43   */....  u.bj.pC
26ed0 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
26ed1 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  >p1];..  assert(
26ed2 20 69 73 53 6f 72 74 65 72 28 75 2e 62 6a 2e 70   isSorter(u.bj.p
26ed3 43 29 20 29 3b 0d 0a 20 20 70 49 6e 33 20 3d 20  C) );..  pIn3 = 
26ed4 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d  &aMem[pOp->p3];.
26ed5 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
26ed6 64 62 65 53 6f 72 74 65 72 43 6f 6d 70 61 72 65  dbeSorterCompare
26ed7 28 75 2e 62 6a 2e 70 43 2c 20 70 49 6e 33 2c 20  (u.bj.pC, pIn3, 
26ed8 26 75 2e 62 6a 2e 72 65 73 29 3b 0d 0a 20 20 69  &u.bj.res);..  i
26ed9 66 28 20 75 2e 62 6a 2e 72 65 73 20 29 7b 0d 0a  f( u.bj.res ){..
26eda 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
26edb 2d 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61  -1;..  }..  brea
26edc 6b 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 4f 70 63  k;..};..../* Opc
26edd 6f 64 65 3a 20 53 6f 72 74 65 72 44 61 74 61 20  ode: SorterData 
26ede 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d  P1 P2 * * *..**.
26edf 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
26ee0 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
26ee1 75 72 72 65 6e 74 20 73 6f 72 74 65 72 20 64 61  urrent sorter da
26ee2 74 61 20 66 6f 72 20 73 6f 72 74 65 72 20 63 75  ta for sorter cu
26ee3 72 73 6f 72 20 50 31 2e 0d 0a 2a 2f 0d 0a 63 61  rsor P1...*/..ca
26ee4 73 65 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61  se OP_SorterData
26ee5 3a 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c  : {..#if 0  /* l
26ee6 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
26ee7 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a  oved into u.bk *
26ee8 2f 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  /..  VdbeCursor 
26ee9 2a 70 43 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  *pC;..#endif /* 
26eea 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
26eeb 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20  moved into u.bk 
26eec 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
26eed 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f  TE_OMIT_MERGE_SO
26eee 52 54 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d  RT..  pOut = &aM
26eef 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20  em[pOp->p2];..  
26ef0 75 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bk.pC = p->apC
26ef1 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  sr[pOp->p1];..  
26ef2 61 73 73 65 72 74 28 20 75 2e 62 6b 2e 70 43 2d  assert( u.bk.pC-
26ef3 3e 69 73 53 6f 72 74 65 72 20 29 3b 0d 0a 20 20  >isSorter );..  
26ef4 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
26ef5 53 6f 72 74 65 72 52 6f 77 6b 65 79 28 75 2e 62  SorterRowkey(u.b
26ef6 6b 2e 70 43 2c 20 70 4f 75 74 29 3b 0d 0a 23 65  k.pC, pOut);..#e
26ef7 6c 73 65 0d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  lse..  pOp->opco
26ef8 64 65 20 3d 20 4f 50 5f 52 6f 77 4b 65 79 3b 0d  de = OP_RowKey;.
26ef9 0a 20 20 70 63 2d 2d 3b 0d 0a 23 65 6e 64 69 66  .  pc--;..#endif
26efa 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d  ..  break;..}...
26efb 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44  ./* Opcode: RowD
26efc 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d  ata P1 P2 * * *.
26efd 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 69 6e  .**..** Write in
26efe 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74  to register P2 t
26eff 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20  he complete row 
26f00 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20  data for cursor 
26f01 50 31 2e 0d 0a 2a 2a 20 54 68 65 72 65 20 69 73  P1...** There is
26f02 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69   no interpretati
26f03 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20  on of the data. 
26f04 20 0d 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74   ..** It is just
26f05 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65   copied onto the
26f06 20 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61   P2 register exa
26f07 63 74 6c 79 20 61 73 20 0d 0a 2a 2a 20 69 74 20  ctly as ..** it 
26f08 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20  is found in the 
26f09 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a  database file...
26f0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  **..** If the P1
26f0b 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20   cursor must be 
26f0c 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61  pointing to a va
26f0d 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e  lid row (not a N
26f0e 55 4c 4c 20 72 6f 77 29 0d 0a 2a 2a 20 6f 66 20  ULL row)..** of 
26f0f 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f  a real table, no
26f10 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  t a pseudo-table
26f11 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ...*/../* Opcode
26f12 3a 20 52 6f 77 4b 65 79 20 50 31 20 50 32 20 2a  : RowKey P1 P2 *
26f13 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69   * *..**..** Wri
26f14 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  te into register
26f15 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   P2 the complete
26f16 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63 75 72   row key for cur
26f17 73 6f 72 20 50 31 2e 0d 0a 2a 2a 20 54 68 65 72  sor P1...** Ther
26f18 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65  e is no interpre
26f19 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
26f1a 74 61 2e 20 20 0d 0a 2a 2a 20 54 68 65 20 6b 65  ta.  ..** The ke
26f1b 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f  y is copied onto
26f1c 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72   the P3 register
26f1d 20 65 78 61 63 74 6c 79 20 61 73 20 0d 0a 2a 2a   exactly as ..**
26f1e 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20   it is found in 
26f1f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
26f20 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  e...**..** If th
26f21 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
26f22 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   be pointing to 
26f23 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74  a valid row (not
26f24 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0d 0a 2a 2a   a NULL row)..**
26f25 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65   of a real table
26f26 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74  , not a pseudo-t
26f27 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  able...*/..case 
26f28 4f 50 5f 52 6f 77 4b 65 79 3a 0d 0a 63 61 73 65  OP_RowKey:..case
26f29 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0d 0a   OP_RowData: {..
26f2a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
26f2b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
26f2c 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0d 0a 20 20  into u.bl */..  
26f2d 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d  VdbeCursor *pC;.
26f2e 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
26f2f 73 72 3b 0d 0a 20 20 75 33 32 20 6e 3b 0d 0a 20  sr;..  u32 n;.. 
26f30 20 69 36 34 20 6e 36 34 3b 0d 0a 23 65 6e 64 69   i64 n64;..#endi
26f31 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
26f32 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
26f33 75 2e 62 6c 20 2a 2f 0d 0a 0d 0a 20 20 70 4f 75  u.bl */....  pOu
26f34 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  t = &aMem[pOp->p
26f35 32 5d 3b 0d 0a 20 20 6d 65 6d 41 62 6f 75 74 54  2];..  memAboutT
26f36 6f 43 68 61 6e 67 65 28 70 2c 20 70 4f 75 74 29  oChange(p, pOut)
26f37 3b 0d 0a 0d 0a 20 20 2f 2a 20 4e 6f 74 65 20 74  ;....  /* Note t
26f38 68 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 52  hat RowKey and R
26f39 6f 77 44 61 74 61 20 61 72 65 20 72 65 61 6c 6c  owData are reall
26f3a 79 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  y exactly the sa
26f3b 6d 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  me instruction *
26f3c 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
26f3d 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
26f3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
26f3f 0d 0a 20 20 75 2e 62 6c 2e 70 43 20 3d 20 70 2d  ..  u.bl.pC = p-
26f40 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
26f41 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6c  ..  assert( u.bl
26f42 2e 70 43 2d 3e 69 73 53 6f 72 74 65 72 3d 3d 30  .pC->isSorter==0
26f43 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75   );..  assert( u
26f44 2e 62 6c 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20  .bl.pC->isTable 
26f45 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 21 3d  || pOp->opcode!=
26f46 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b 0d 0a 20  OP_RowData );.. 
26f47 20 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43   assert( u.bl.pC
26f48 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70  ->isIndex || pOp
26f49 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77  ->opcode==OP_Row
26f4a 44 61 74 61 20 29 3b 0d 0a 20 20 61 73 73 65 72  Data );..  asser
26f4b 74 28 20 75 2e 62 6c 2e 70 43 21 3d 30 20 29 3b  t( u.bl.pC!=0 );
26f4c 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6c  ..  assert( u.bl
26f4d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20  .pC->nullRow==0 
26f4e 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e  );..  assert( u.
26f4f 62 6c 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  bl.pC->pseudoTab
26f50 6c 65 52 65 67 3d 3d 30 20 29 3b 0d 0a 20 20 61  leReg==0 );..  a
26f51 73 73 65 72 74 28 20 21 75 2e 62 6c 2e 70 43 2d  ssert( !u.bl.pC-
26f52 3e 69 73 53 6f 72 74 65 72 20 29 3b 0d 0a 20 20  >isSorter );..  
26f53 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 2d  assert( u.bl.pC-
26f54 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0d 0a  >pCursor!=0 );..
26f55 20 20 75 2e 62 6c 2e 70 43 72 73 72 20 3d 20 75    u.bl.pCrsr = u
26f56 2e 62 6c 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b  .bl.pC->pCursor;
26f57 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
26f58 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
26f59 56 61 6c 69 64 28 75 2e 62 6c 2e 70 43 72 73 72  Valid(u.bl.pCrsr
26f5a 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65  ) );....  /* The
26f5b 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64 20 4f   OP_RowKey and O
26f5c 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f 64 65  P_RowData opcode
26f5d 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20  s always follow 
26f5e 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 0d  OP_NotExists or.
26f5f 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 2f  .  ** OP_Rewind/
26f60 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f 20  Op_Next with no 
26f61 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73 74  intervening inst
26f62 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 69  ructions that mi
26f63 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0d 0a  ght invalidate..
26f64 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e    ** the cursor.
26f65 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c    Hence the foll
26f66 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  owing sqlite3Vdb
26f67 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20  eCursorMoveto() 
26f68 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0d 0a  call is always..
26f69 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64    ** a no-op and
26f6a 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
26f6b 20 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69    But we leave i
26f6c 74 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20  t in place as a 
26f6d 73 61 66 65 74 79 2e 0d 0a 20 20 2a 2f 0d 0a 20  safety...  */.. 
26f6e 20 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43   assert( u.bl.pC
26f6f 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
26f70 3d 3d 30 20 29 3b 0d 0a 20 20 72 63 20 3d 20 73  ==0 );..  rc = s
26f71 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
26f72 4d 6f 76 65 74 6f 28 75 2e 62 6c 2e 70 43 29 3b  Moveto(u.bl.pC);
26f73 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ..  if( NEVER(rc
26f74 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67  !=SQLITE_OK) ) g
26f75 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
26f76 5f 65 72 72 6f 72 3b 0d 0a 0d 0a 20 20 69 66 28  _error;....  if(
26f77 20 75 2e 62 6c 2e 70 43 2d 3e 69 73 49 6e 64 65   u.bl.pC->isInde
26f78 78 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  x ){..    assert
26f79 28 20 21 75 2e 62 6c 2e 70 43 2d 3e 69 73 54 61  ( !u.bl.pC->isTa
26f7a 62 6c 65 20 29 3b 0d 0a 20 20 20 20 56 56 41 5f  ble );..    VVA_
26f7b 4f 4e 4c 59 28 72 63 20 3d 29 20 73 71 6c 69 74  ONLY(rc =) sqlit
26f7c 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75  e3BtreeKeySize(u
26f7d 2e 62 6c 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c  .bl.pCrsr, &u.bl
26f7e 2e 6e 36 34 29 3b 0d 0a 20 20 20 20 61 73 73 65  .n64);..    asse
26f7f 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
26f80 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  K );    /* True 
26f81 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f  because of Curso
26f82 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 61  rMoveto() call a
26f83 62 6f 76 65 20 2a 2f 0d 0a 20 20 20 20 69 66 28  bove */..    if(
26f84 20 75 2e 62 6c 2e 6e 36 34 3e 64 62 2d 3e 61 4c   u.bl.n64>db->aL
26f85 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
26f86 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0d 0a 20 20  T_LENGTH] ){..  
26f87 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
26f88 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 75 2e  ;..    }..    u.
26f89 62 6c 2e 6e 20 3d 20 28 75 33 32 29 75 2e 62 6c  bl.n = (u32)u.bl
26f8a 2e 6e 36 34 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  .n64;..  }else{.
26f8b 0a 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 72 63  .    VVA_ONLY(rc
26f8c 20 3d 29 20 73 71 6c 69 74 65 33 42 74 72 65 65   =) sqlite3Btree
26f8d 44 61 74 61 53 69 7a 65 28 75 2e 62 6c 2e 70 43  DataSize(u.bl.pC
26f8e 72 73 72 2c 20 26 75 2e 62 6c 2e 6e 29 3b 0d 0a  rsr, &u.bl.n);..
26f8f 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
26f90 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20  SQLITE_OK );    
26f91 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61  /* DataSize() ca
26f92 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0d 0a 20 20  nnot fail */..  
26f93 20 20 69 66 28 20 75 2e 62 6c 2e 6e 3e 28 75 33    if( u.bl.n>(u3
26f94 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  2)db->aLimit[SQL
26f95 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
26f96 5d 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  ] ){..      goto
26f97 20 74 6f 6f 5f 62 69 67 3b 0d 0a 20 20 20 20 7d   too_big;..    }
26f98 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 73 71 6c  ..  }..  if( sql
26f99 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
26f9a 70 4f 75 74 2c 20 75 2e 62 6c 2e 6e 2c 20 30 29  pOut, u.bl.n, 0)
26f9b 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 6e 6f   ){..    goto no
26f9c 5f 6d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4f  _mem;..  }..  pO
26f9d 75 74 2d 3e 6e 20 3d 20 75 2e 62 6c 2e 6e 3b 0d  ut->n = u.bl.n;.
26f9e 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
26f9f 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62  g(pOut, MEM_Blob
26fa0 29 3b 0d 0a 20 20 69 66 28 20 75 2e 62 6c 2e 70  );..  if( u.bl.p
26fa1 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0d 0a 20  C->isIndex ){.. 
26fa2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
26fa3 74 72 65 65 4b 65 79 28 75 2e 62 6c 2e 70 43 72  treeKey(u.bl.pCr
26fa4 73 72 2c 20 30 2c 20 75 2e 62 6c 2e 6e 2c 20 70  sr, 0, u.bl.n, p
26fa5 4f 75 74 2d 3e 7a 29 3b 0d 0a 20 20 7d 65 6c 73  Out->z);..  }els
26fa6 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  e{..    rc = sql
26fa7 69 74 65 33 42 74 72 65 65 44 61 74 61 28 75 2e  ite3BtreeData(u.
26fa8 62 6c 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62  bl.pCrsr, 0, u.b
26fa9 6c 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0d 0a  l.n, pOut->z);..
26faa 20 20 7d 0d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63    }..  pOut->enc
26fab 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20   = SQLITE_UTF8; 
26fac 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20   /* In case the 
26fad 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 73  blob is ever cas
26fae 74 20 74 6f 20 74 65 78 74 20 2a 2f 0d 0a 20 20  t to text */..  
26faf 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
26fb0 49 5a 45 28 70 4f 75 74 29 3b 0d 0a 20 20 62 72  IZE(pOut);..  br
26fb1 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70  eak;..}..../* Op
26fb2 63 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20 50  code: Rowid P1 P
26fb3 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  2 * * *..**..** 
26fb4 53 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74 65  Store in registe
26fb5 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20  r P2 an integer 
26fb6 77 68 69 63 68 20 69 73 20 74 68 65 20 6b 65 79  which is the key
26fb7 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e   of the table en
26fb8 74 72 79 20 74 68 61 74 0d 0a 2a 2a 20 50 31 20  try that..** P1 
26fb9 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
26fba 6e 74 20 74 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50  nt to...**..** P
26fbb 31 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  1 can be either 
26fbc 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c  an ordinary tabl
26fbd 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74  e or a virtual t
26fbe 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75 73 65  able.  There use
26fbf 64 20 74 6f 0d 0a 2a 2a 20 62 65 20 61 20 73 65  d to..** be a se
26fc0 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69 64  parate OP_VRowid
26fc1 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65 20   opcode for use 
26fc2 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61 62  with virtual tab
26fc3 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0d 0a 2a  les, but this..*
26fc4 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77  * one opcode now
26fc5 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20   works for both 
26fc6 74 61 62 6c 65 20 74 79 70 65 73 2e 0d 0a 2a 2f  table types...*/
26fc7 0d 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a  ..case OP_Rowid:
26fc8 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
26fc9 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
26fca 6c 65 61 73 65 20 2a 2f 0d 0a 23 69 66 20 30 20  lease */..#if 0 
26fcb 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
26fcc 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
26fcd 2e 62 6d 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75  .bm */..  VdbeCu
26fce 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 69 36 34  rsor *pC;..  i64
26fcf 20 76 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76   v;..  sqlite3_v
26fd0 74 61 62 20 2a 70 56 74 61 62 3b 0d 0a 20 20 63  tab *pVtab;..  c
26fd1 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
26fd2 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0d 0a 23  ule *pModule;..#
26fd3 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
26fd4 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
26fd5 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0d 0a 0d 0a 20  nto u.bm */.... 
26fd6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
26fd7 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
26fd8 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20  ->nCursor );..  
26fd9 75 2e 62 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bm.pC = p->apC
26fda 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  sr[pOp->p1];..  
26fdb 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 21  assert( u.bm.pC!
26fdc 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
26fdd 20 75 2e 62 6d 2e 70 43 2d 3e 70 73 65 75 64 6f   u.bm.pC->pseudo
26fde 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0d 0a  TableReg==0 );..
26fdf 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 2d 3e 6e    if( u.bm.pC->n
26fe0 75 6c 6c 52 6f 77 20 29 7b 0d 0a 20 20 20 20 70  ullRow ){..    p
26fe1 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
26fe2 5f 4e 75 6c 6c 3b 0d 0a 20 20 20 20 62 72 65 61  _Null;..    brea
26fe3 6b 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  k;..  }else if( 
26fe4 75 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bm.pC->deferre
26fe5 64 4d 6f 76 65 74 6f 20 29 7b 0d 0a 20 20 20 20  dMoveto ){..    
26fe6 75 2e 62 6d 2e 76 20 3d 20 75 2e 62 6d 2e 70 43  u.bm.v = u.bm.pC
26fe7 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 0d  ->movetoTarget;.
26fe8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26fe9 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
26fea 45 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75  E..  }else if( u
26feb 2e 62 6d 2e 70 43 2d 3e 70 56 74 61 62 43 75 72  .bm.pC->pVtabCur
26fec 73 6f 72 20 29 7b 0d 0a 20 20 20 20 75 2e 62 6d  sor ){..    u.bm
26fed 2e 70 56 74 61 62 20 3d 20 75 2e 62 6d 2e 70 43  .pVtab = u.bm.pC
26fee 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
26fef 56 74 61 62 3b 0d 0a 20 20 20 20 75 2e 62 6d 2e  Vtab;..    u.bm.
26ff0 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 6d 2e 70  pModule = u.bm.p
26ff1 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a  Vtab->pModule;..
26ff2 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6d      assert( u.bm
26ff3 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64  .pModule->xRowid
26ff4 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 75 2e   );..    rc = u.
26ff5 62 6d 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77  bm.pModule->xRow
26ff6 69 64 28 75 2e 62 6d 2e 70 43 2d 3e 70 56 74 61  id(u.bm.pC->pVta
26ff7 62 43 75 72 73 6f 72 2c 20 26 75 2e 62 6d 2e 76  bCursor, &u.bm.v
26ff8 29 3b 0d 0a 20 20 20 20 69 6d 70 6f 72 74 56 74  );..    importVt
26ff9 61 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 62 6d  abErrMsg(p, u.bm
26ffa 2e 70 56 74 61 62 29 3b 0d 0a 23 65 6e 64 69 66  .pVtab);..#endif
26ffb 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
26ffc 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d  VIRTUALTABLE */.
26ffd 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61  .  }else{..    a
26ffe 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e  ssert( u.bm.pC->
26fff 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0d 0a 20  pCursor!=0 );.. 
27000 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
27001 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
27002 75 2e 62 6d 2e 70 43 29 3b 0d 0a 20 20 20 20 69  u.bm.pC);..    i
27003 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
27004 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
27005 0d 0a 20 20 20 20 69 66 28 20 75 2e 62 6d 2e 70  ..    if( u.bm.p
27006 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
27007 29 7b 0d 0a 20 20 20 20 20 20 75 2e 62 6d 2e 76  ){..      u.bm.v
27008 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 6c 61 73 74   = u.bm.pC->last
27009 52 6f 77 69 64 3b 0d 0a 20 20 20 20 7d 65 6c 73  Rowid;..    }els
2700a 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  e{..      rc = s
2700b 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
2700c 7a 65 28 75 2e 62 6d 2e 70 43 2d 3e 70 43 75 72  ze(u.bm.pC->pCur
2700d 73 6f 72 2c 20 26 75 2e 62 6d 2e 76 29 3b 0d 0a  sor, &u.bm.v);..
2700e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
2700f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20  ==SQLITE_OK );  
27010 2f 2a 20 41 6c 77 61 79 73 20 73 6f 20 62 65 63  /* Always so bec
27011 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f  ause of CursorMo
27012 76 65 74 6f 28 29 20 61 62 6f 76 65 20 2a 2f 0d  veto() above */.
27013 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70  .    }..  }..  p
27014 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 6d 2e  Out->u.i = u.bm.
27015 76 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d  v;..  break;..}.
27016 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75  .../* Opcode: Nu
27017 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a  llRow P1 * * * *
27018 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 76 65 20 74 68  ..**..** Move th
27019 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61  e cursor P1 to a
2701a 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 20   null row.  Any 
2701b 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 74  OP_Column operat
2701c 69 6f 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 6f 63  ions..** that oc
2701d 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 75  cur while the cu
2701e 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e  rsor is on the n
2701f 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77  ull row will alw
27020 61 79 73 0d 0a 2a 2a 20 77 72 69 74 65 20 61 20  ays..** write a 
27021 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  NULL...*/..case 
27022 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0d 0a 23  OP_NullRow: {..#
27023 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
27024 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
27025 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0d 0a 20 20 56  nto u.bn */..  V
27026 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d 0a  dbeCursor *pC;..
27027 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
27028 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
27029 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0d 0a 0d 0a  into u.bn */....
2702a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
2702b 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
2702c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 20  p->nCursor );.. 
2702d 20 75 2e 62 6e 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bn.pC = p->ap
2702e 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20  Csr[pOp->p1];.. 
2702f 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43   assert( u.bn.pC
27030 21 3d 30 20 29 3b 0d 0a 20 20 75 2e 62 6e 2e 70  !=0 );..  u.bn.p
27031 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0d  C->nullRow = 1;.
27032 0a 20 20 75 2e 62 6e 2e 70 43 2d 3e 72 6f 77 69  .  u.bn.pC->rowi
27033 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0d 0a 20  dIsValid = 0;.. 
27034 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43   assert( u.bn.pC
27035 2d 3e 70 43 75 72 73 6f 72 20 7c 7c 20 75 2e 62  ->pCursor || u.b
27036 6e 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f  n.pC->pVtabCurso
27037 72 20 29 3b 0d 0a 20 20 69 66 28 20 75 2e 62 6e  r );..  if( u.bn
27038 2e 70 43 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0d  .pC->pCursor ){.
27039 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
2703a 65 43 6c 65 61 72 43 75 72 73 6f 72 28 75 2e 62  eClearCursor(u.b
2703b 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0d  n.pC->pCursor);.
2703c 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  .  }..  break;..
2703d 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
2703e 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Last P1 P2 * * *
2703f 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e 65 78  ..**..** The nex
27040 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77  t use of the Row
27041 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20  id or Column or 
27042 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Next instruction
27043 20 66 6f 72 20 50 31 20 0d 0a 2a 2a 20 77 69 6c   for P1 ..** wil
27044 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c  l refer to the l
27045 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
27046 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
27047 6f 72 20 69 6e 64 65 78 2e 0d 0a 2a 2a 20 49 66  or index...** If
27048 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
27049 64 65 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64  dex is empty and
2704a 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70   P2>0, then jump
2704b 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
2704c 50 32 2e 0d 0a 2a 2a 20 49 66 20 50 32 20 69 73  P2...** If P2 is
2704d 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 61 62   0 or if the tab
2704e 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 6e  le or index is n
2704f 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74  ot empty, fall t
27050 68 72 6f 75 67 68 0d 0a 2a 2a 20 74 6f 20 74 68  hrough..** to th
27051 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
27052 72 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61  ruction...*/..ca
27053 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20  se OP_Last: {   
27054 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0d       /* jump */.
27055 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
27056 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
27057 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0d 0a 20   into u.bo */.. 
27058 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
27059 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ..  BtCursor *pC
2705a 72 73 72 3b 0d 0a 20 20 69 6e 74 20 72 65 73 3b  rsr;..  int res;
2705b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ..#endif /* loca
2705c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
2705d 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0d 0a  d into u.bo */..
2705e 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
2705f 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
27060 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d  1<p->nCursor );.
27061 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e  .  u.bo.pC = p->
27062 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  apCsr[pOp->p1];.
27063 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e  .  assert( u.bo.
27064 70 43 21 3d 30 20 29 3b 0d 0a 20 20 75 2e 62 6f  pC!=0 );..  u.bo
27065 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 43  .pCrsr = u.bo.pC
27066 2d 3e 70 43 75 72 73 6f 72 3b 0d 0a 20 20 75 2e  ->pCursor;..  u.
27067 62 6f 2e 72 65 73 20 3d 20 30 3b 0d 0a 20 20 69  bo.res = 0;..  i
27068 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70  f( ALWAYS(u.bo.p
27069 43 72 73 72 21 3d 30 29 20 29 7b 0d 0a 20 20 20  Crsr!=0) ){..   
2706a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
2706b 65 65 4c 61 73 74 28 75 2e 62 6f 2e 70 43 72 73  eeLast(u.bo.pCrs
2706c 72 2c 20 26 75 2e 62 6f 2e 72 65 73 29 3b 0d 0a  r, &u.bo.res);..
2706d 20 20 7d 0d 0a 20 20 75 2e 62 6f 2e 70 43 2d 3e    }..  u.bo.pC->
2706e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e  nullRow = (u8)u.
2706f 62 6f 2e 72 65 73 3b 0d 0a 20 20 75 2e 62 6f 2e  bo.res;..  u.bo.
27070 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
27071 74 6f 20 3d 20 30 3b 0d 0a 20 20 75 2e 62 6f 2e  to = 0;..  u.bo.
27072 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
27073 20 3d 20 30 3b 0d 0a 20 20 75 2e 62 6f 2e 70 43   = 0;..  u.bo.pC
27074 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
27075 43 41 43 48 45 5f 53 54 41 4c 45 3b 0d 0a 20 20  CACHE_STALE;..  
27076 69 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26  if( pOp->p2>0 &&
27077 20 75 2e 62 6f 2e 72 65 73 20 29 7b 0d 0a 20 20   u.bo.res ){..  
27078 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
27079 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61   1;..  }..  brea
2707a 6b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 20 4f 70  k;..}....../* Op
2707b 63 6f 64 65 3a 20 53 6f 72 74 20 50 31 20 50 32  code: Sort P1 P2
2707c 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54   * * *..**..** T
2707d 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20  his opcode does 
2707e 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65  exactly the same
2707f 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65 77   thing as OP_Rew
27080 69 6e 64 20 65 78 63 65 70 74 20 74 68 61 74 0d  ind except that.
27081 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74  .** it increment
27082 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65  s an undocumente
27083 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  d global variabl
27084 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  e used for testi
27085 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 72 74  ng...**..** Sort
27086 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73  ing is accomplis
27087 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 72  hed by writing r
27088 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f  ecords into a so
27089 72 74 69 6e 67 20 69 6e 64 65 78 2c 0d 0a 2a 2a  rting index,..**
2708a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20   then rewinding 
2708b 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70  that index and p
2708c 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66  laying it back f
2708d 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f  rom beginning to
2708e 0d 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73  ..** end.  We us
2708f 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70  e the OP_Sort op
27090 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20  code instead of 
27091 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20  OP_Rewind to do 
27092 74 68 65 0d 0a 2a 2a 20 72 65 77 69 6e 64 69 6e  the..** rewindin
27093 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c  g so that the gl
27094 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69  obal variable wi
27095 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65  ll be incremente
27096 64 20 61 6e 64 0d 0a 2a 2a 20 72 65 67 72 65 73  d and..** regres
27097 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64  sion tests can d
27098 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
27099 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69   or not the opti
2709a 6d 69 7a 65 72 20 69 73 0d 0a 2a 2a 20 63 6f 72  mizer is..** cor
2709b 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e  rectly optimizin
2709c 67 20 6f 75 74 20 73 6f 72 74 73 2e 0d 0a 2a 2f  g out sorts...*/
2709d 0d 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 65 72  ..case OP_Sorter
2709e 53 6f 72 74 3a 20 20 20 20 2f 2a 20 6a 75 6d 70  Sort:    /* jump
2709f 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
270a0 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f  TE_OMIT_MERGE_SO
270a1 52 54 0d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64  RT..  pOp->opcod
270a2 65 20 3d 20 4f 50 5f 53 6f 72 74 3b 0d 0a 23 65  e = OP_Sort;..#e
270a3 6e 64 69 66 0d 0a 63 61 73 65 20 4f 50 5f 53 6f  ndif..case OP_So
270a4 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  rt: {        /* 
270a5 6a 75 6d 70 20 2a 2f 0d 0a 23 69 66 64 65 66 20  jump */..#ifdef 
270a6 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 73  SQLITE_TEST..  s
270a7 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e  qlite3_sort_coun
270a8 74 2b 2b 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  t++;..  sqlite3_
270a9 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0d  search_count--;.
270aa 0a 23 65 6e 64 69 66 0d 0a 20 20 70 2d 3e 61 43  .#endif..  p->aC
270ab 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54  ounter[SQLITE_ST
270ac 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d  MTSTATUS_SORT-1]
270ad 2b 2b 3b 0d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74  ++;..  /* Fall t
270ae 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52  hrough into OP_R
270af 65 77 69 6e 64 20 2a 2f 0d 0a 7d 0d 0a 2f 2a 20  ewind */..}../* 
270b0 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50  Opcode: Rewind P
270b1 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  1 P2 * * *..**..
270b2 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20  ** The next use 
270b3 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20  of the Rowid or 
270b4 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69  Column or Next i
270b5 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50  nstruction for P
270b6 31 20 0d 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65  1 ..** will refe
270b7 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
270b8 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61  ntry in the data
270b9 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  base table or in
270ba 64 65 78 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20  dex...** If the 
270bb 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
270bc 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30  s empty and P2>0
270bd 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65  , then jump imme
270be 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0d 0a  diately to P2...
270bf 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
270c0 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
270c1 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
270c2 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
270c3 68 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c  h..** to the fol
270c4 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69  lowing instructi
270c5 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  on...*/..case OP
270c6 5f 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20 20  _Rewind: {      
270c7 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0d 0a 23 69    /* jump */..#i
270c8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
270c9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
270ca 74 6f 20 75 2e 62 70 20 2a 2f 0d 0a 20 20 56 64  to u.bp */..  Vd
270cb 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20  beCursor *pC;.. 
270cc 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
270cd 3b 0d 0a 20 20 69 6e 74 20 72 65 73 3b 0d 0a 23  ;..  int res;..#
270ce 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
270cf 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
270d0 6e 74 6f 20 75 2e 62 70 20 2a 2f 0d 0a 0d 0a 20  nto u.bp */.... 
270d1 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
270d2 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
270d3 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20  ->nCursor );..  
270d4 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bp.pC = p->apC
270d5 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20  sr[pOp->p1];..  
270d6 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21  assert( u.bp.pC!
270d7 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
270d8 20 75 2e 62 70 2e 70 43 2d 3e 69 73 53 6f 72 74   u.bp.pC->isSort
270d9 65 72 3d 3d 28 70 4f 70 2d 3e 6f 70 63 6f 64 65  er==(pOp->opcode
270da 3d 3d 4f 50 5f 53 6f 72 74 65 72 53 6f 72 74 29  ==OP_SorterSort)
270db 20 29 3b 0d 0a 20 20 75 2e 62 70 2e 72 65 73 20   );..  u.bp.res 
270dc 3d 20 31 3b 0d 0a 20 20 69 66 28 20 69 73 53 6f  = 1;..  if( isSo
270dd 72 74 65 72 28 75 2e 62 70 2e 70 43 29 20 29 7b  rter(u.bp.pC) ){
270de 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
270df 65 33 56 64 62 65 53 6f 72 74 65 72 52 65 77 69  e3VdbeSorterRewi
270e0 6e 64 28 64 62 2c 20 75 2e 62 70 2e 70 43 2c 20  nd(db, u.bp.pC, 
270e1 26 75 2e 62 70 2e 72 65 73 29 3b 0d 0a 20 20 7d  &u.bp.res);..  }
270e2 65 6c 73 65 7b 0d 0a 20 20 20 20 75 2e 62 70 2e  else{..    u.bp.
270e3 70 43 72 73 72 20 3d 20 75 2e 62 70 2e 70 43 2d  pCrsr = u.bp.pC-
270e4 3e 70 43 75 72 73 6f 72 3b 0d 0a 20 20 20 20 61  >pCursor;..    a
270e5 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 72 73  ssert( u.bp.pCrs
270e6 72 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  r );..    rc = s
270e7 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74  qlite3BtreeFirst
270e8 28 75 2e 62 70 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bp.pCrsr, &u.
270e9 62 70 2e 72 65 73 29 3b 0d 0a 20 20 20 20 75 2e  bp.res);..    u.
270ea 62 70 2e 70 43 2d 3e 61 74 46 69 72 73 74 20 3d  bp.pC->atFirst =
270eb 20 75 2e 62 70 2e 72 65 73 3d 3d 30 20 3f 31 3a   u.bp.res==0 ?1:
270ec 30 3b 0d 0a 20 20 20 20 75 2e 62 70 2e 70 43 2d  0;..    u.bp.pC-
270ed 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
270ee 3d 20 30 3b 0d 0a 20 20 20 20 75 2e 62 70 2e 70  = 0;..    u.bp.p
270ef 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
270f0 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0d 0a 20   CACHE_STALE;.. 
270f1 20 20 20 75 2e 62 70 2e 70 43 2d 3e 72 6f 77 69     u.bp.pC->rowi
270f2 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0d 0a 20  dIsValid = 0;.. 
270f3 20 7d 0d 0a 20 20 75 2e 62 70 2e 70 43 2d 3e 6e   }..  u.bp.pC->n
270f4 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62  ullRow = (u8)u.b
270f5 70 2e 72 65 73 3b 0d 0a 20 20 61 73 73 65 72 74  p.res;..  assert
270f6 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70  ( pOp->p2>0 && p
270f7 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b  Op->p2<p->nOp );
270f8 0d 0a 20 20 69 66 28 20 75 2e 62 70 2e 72 65 73  ..  if( u.bp.res
270f9 20 29 7b 0d 0a 20 20 20 20 70 63 20 3d 20 70 4f   ){..    pc = pO
270fa 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20 20 7d 0d  p->p2 - 1;..  }.
270fb 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a  .  break;..}....
270fc 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20  /* Opcode: Next 
270fd 50 31 20 50 32 20 2a 20 50 34 20 50 35 0d 0a 2a  P1 P2 * P4 P5..*
270fe 2a 0d 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75  *..** Advance cu
270ff 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20  rsor P1 so that 
27100 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
27101 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70   next key/data p
27102 61 69 72 20 69 6e 20 69 74 73 0d 0a 2a 2a 20 74  air in its..** t
27103 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20  able or index.  
27104 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
27105 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70  more key/value p
27106 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74  airs then fall t
27107 68 72 6f 75 67 68 0d 0a 2a 2a 20 74 6f 20 74 68  hrough..** to th
27108 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
27109 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  ruction.  But if
2710a 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 61   the cursor adva
2710b 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 66  nce was successf
2710c 75 6c 2c 0d 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d  ul,..** jump imm
2710d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0d  ediately to P2..
2710e 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 31 20 63  .**..** The P1 c
2710f 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f  ursor must be fo
27110 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  r a real table, 
27111 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62  not a pseudo-tab
27112 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34 20 69  le...**..** P4 i
27113 73 20 61 6c 77 61 79 73 20 6f 66 20 74 79 70 65  s always of type
27114 20 50 34 5f 41 44 56 41 4e 43 45 2e 20 54 68 65   P4_ADVANCE. The
27115 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
27116 72 20 70 6f 69 6e 74 73 20 74 6f 0d 0a 2a 2a 20  r points to..** 
27117 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
27118 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 50  ()...**..** If P
27119 35 20 69 73 20 70 6f 73 69 74 69 76 65 20 61 6e  5 is positive an
2711a 64 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  d the jump is ta
2711b 6b 65 6e 2c 20 74 68 65 6e 20 65 76 65 6e 74 20  ken, then event 
2711c 63 6f 75 6e 74 65 72 0d 0a 2a 2a 20 6e 75 6d 62  counter..** numb
2711d 65 72 20 50 35 2d 31 20 69 6e 20 74 68 65 20 70  er P5-1 in the p
2711e 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2711f 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
27120 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c  ...**..** See al
27121 73 6f 3a 20 50 72 65 76 0d 0a 2a 2f 0d 0a 2f 2a  so: Prev..*/../*
27122 20 4f 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31   Opcode: Prev P1
27123 20 50 32 20 2a 20 2a 20 50 35 0d 0a 2a 2a 0d 0a   P2 * * P5..**..
27124 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73 6f  ** Back up curso
27125 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20  r P1 so that it 
27126 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 72  points to the pr
27127 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61 20  evious key/data 
27128 70 61 69 72 20 69 6e 20 69 74 73 0d 0a 2a 2a 20  pair in its..** 
27129 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
2712a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
2712b 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c  previous key/val
2712c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61  ue pairs then fa
2712d 6c 6c 20 74 68 72 6f 75 67 68 0d 0a 2a 2a 20 74  ll through..** t
2712e 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
2712f 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75  instruction.  Bu
27130 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  t if the cursor 
27131 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65  backup was succe
27132 73 73 66 75 6c 2c 0d 0a 2a 2a 20 6a 75 6d 70 20  ssful,..** jump 
27133 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
27134 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50  2...**..** The P
27135 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  1 cursor must be
27136 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c   for a real tabl
27137 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
27138 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50  table...**..** P
27139 34 20 69 73 20 61 6c 77 61 79 73 20 6f 66 20 74  4 is always of t
2713a 79 70 65 20 50 34 5f 41 44 56 41 4e 43 45 2e 20  ype P4_ADVANCE. 
2713b 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  The function poi
2713c 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0d 0a  nter points to..
2713d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  ** sqlite3BtreeP
2713e 72 65 76 69 6f 75 73 28 29 2e 0d 0a 2a 2a 0d 0a  revious()...**..
2713f 2a 2a 20 49 66 20 50 35 20 69 73 20 70 6f 73 69  ** If P5 is posi
27140 74 69 76 65 20 61 6e 64 20 74 68 65 20 6a 75 6d  tive and the jum
27141 70 20 69 73 20 74 61 6b 65 6e 2c 20 74 68 65 6e  p is taken, then
27142 20 65 76 65 6e 74 20 63 6f 75 6e 74 65 72 0d 0a   event counter..
27143 2a 2a 20 6e 75 6d 62 65 72 20 50 35 2d 31 20 69  ** number P5-1 i
27144 6e 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  n the prepared s
27145 74 61 74 65 6d 65 6e 74 20 69 73 20 69 6e 63 72  tatement is incr
27146 65 6d 65 6e 74 65 64 2e 0d 0a 2a 2f 0d 0a 63 61  emented...*/..ca
27147 73 65 20 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74  se OP_SorterNext
27148 3a 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0d  :    /* jump */.
27149 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
2714a 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0d 0a  MIT_MERGE_SORT..
2714b 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
2714c 4f 50 5f 4e 65 78 74 3b 0d 0a 23 65 6e 64 69 66  OP_Next;..#endif
2714d 0d 0a 63 61 73 65 20 4f 50 5f 50 72 65 76 3a 20  ..case OP_Prev: 
2714e 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
2714f 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 4e 65 78   */..case OP_Nex
27150 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  t: {        /* j
27151 75 6d 70 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f  ump */..#if 0  /
27152 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
27153 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
27154 71 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73  q */..  VdbeCurs
27155 6f 72 20 2a 70 43 3b 0d 0a 20 20 69 6e 74 20 72  or *pC;..  int r
27156 65 73 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  es;..#endif /* l
27157 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
27158 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a  oved into u.bq *
27159 2f 0d 0a 0d 0a 20 20 43 48 45 43 4b 5f 46 4f 52  /....  CHECK_FOR
2715a 5f 49 4e 54 45 52 52 55 50 54 3b 0d 0a 20 20 61  _INTERRUPT;..  a
2715b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
2715c 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
2715d 6e 43 75 72 73 6f 72 20 29 3b 0d 0a 20 20 61 73  nCursor );..  as
2715e 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 3d 41  sert( pOp->p5<=A
2715f 72 72 61 79 53 69 7a 65 28 70 2d 3e 61 43 6f 75  rraySize(p->aCou
27160 6e 74 65 72 29 20 29 3b 0d 0a 20 20 75 2e 62 71  nter) );..  u.bq
27161 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
27162 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 69 66 28 20  Op->p1];..  if( 
27163 75 2e 62 71 2e 70 43 3d 3d 30 20 29 7b 0d 0a 20  u.bq.pC==0 ){.. 
27164 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65     break;  /* Se
27165 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a  e ticket #2273 *
27166 2f 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  /..  }..  assert
27167 28 20 75 2e 62 71 2e 70 43 2d 3e 69 73 53 6f 72  ( u.bq.pC->isSor
27168 74 65 72 3d 3d 28 70 4f 70 2d 3e 6f 70 63 6f 64  ter==(pOp->opcod
27169 65 3d 3d 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74  e==OP_SorterNext
2716a 29 20 29 3b 0d 0a 20 20 69 66 28 20 69 73 53 6f  ) );..  if( isSo
2716b 72 74 65 72 28 75 2e 62 71 2e 70 43 29 20 29 7b  rter(u.bq.pC) ){
2716c 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ..    assert( pO
2716d 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 6f  p->opcode==OP_So
2716e 72 74 65 72 4e 65 78 74 20 29 3b 0d 0a 20 20 20  rterNext );..   
2716f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
27170 65 53 6f 72 74 65 72 4e 65 78 74 28 64 62 2c 20  eSorterNext(db, 
27171 75 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72  u.bq.pC, &u.bq.r
27172 65 73 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  es);..  }else{..
27173 20 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 31      u.bq.res = 1
27174 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ;..    assert( u
27175 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bq.pC->deferred
27176 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0d 0a 20 20  Moveto==0 );..  
27177 20 20 61 73 73 65 72 74 28 20 75 2e 62 71 2e 70    assert( u.bq.p
27178 43 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0d 0a 20  C->pCursor );.. 
27179 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
2717a 6f 70 63 6f 64 65 21 3d 4f 50 5f 4e 65 78 74 20  opcode!=OP_Next 
2717b 7c 7c 20 70 4f 70 2d 3e 70 34 2e 78 41 64 76 61  || pOp->p4.xAdva
2717c 6e 63 65 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  nce==sqlite3Btre
2717d 65 4e 65 78 74 20 29 3b 0d 0a 20 20 20 20 61 73  eNext );..    as
2717e 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
2717f 65 21 3d 4f 50 5f 50 72 65 76 20 7c 7c 20 70 4f  e!=OP_Prev || pO
27180 70 2d 3e 70 34 2e 78 41 64 76 61 6e 63 65 3d 3d  p->p4.xAdvance==
27181 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
27182 69 6f 75 73 20 29 3b 0d 0a 20 20 20 20 72 63 20  ious );..    rc 
27183 3d 20 70 4f 70 2d 3e 70 34 2e 78 41 64 76 61 6e  = pOp->p4.xAdvan
27184 63 65 28 75 2e 62 71 2e 70 43 2d 3e 70 43 75 72  ce(u.bq.pC->pCur
27185 73 6f 72 2c 20 26 75 2e 62 71 2e 72 65 73 29 3b  sor, &u.bq.res);
27186 0d 0a 20 20 7d 0d 0a 20 20 75 2e 62 71 2e 70 43  ..  }..  u.bq.pC
27187 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29  ->nullRow = (u8)
27188 75 2e 62 71 2e 72 65 73 3b 0d 0a 20 20 75 2e 62  u.bq.res;..  u.b
27189 71 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  q.pC->cacheStatu
2718a 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
2718b 0d 0a 20 20 69 66 28 20 75 2e 62 71 2e 72 65 73  ..  if( u.bq.res
2718c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 63 20 3d  ==0 ){..    pc =
2718d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20   pOp->p2 - 1;.. 
2718e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29     if( pOp->p5 )
2718f 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70   p->aCounter[pOp
27190 2d 3e 70 35 2d 31 5d 2b 2b 3b 0d 0a 23 69 66 64  ->p5-1]++;..#ifd
27191 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a  ef SQLITE_TEST..
27192 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72      sqlite3_sear
27193 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0d 0a 23 65 6e  ch_count++;..#en
27194 64 69 66 0d 0a 20 20 7d 0d 0a 20 20 75 2e 62 71  dif..  }..  u.bq
27195 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
27196 64 20 3d 20 30 3b 0d 0a 20 20 62 72 65 61 6b 3b  d = 0;..  break;
27197 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
27198 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 20 50  : IdxInsert P1 P
27199 32 20 50 33 20 2a 20 50 35 0d 0a 2a 2a 0d 0a 2a  2 P3 * P5..**..*
2719a 2a 20 52 65 67 69 73 74 65 72 20 50 32 20 68 6f  * Register P2 ho
2719b 6c 64 73 20 61 6e 20 53 51 4c 20 69 6e 64 65 78  lds an SQL index
2719c 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20   key made using 
2719d 74 68 65 0d 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f  the..** MakeReco
2719e 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  rd instructions.
2719f 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 72    This opcode wr
271a0 69 74 65 73 20 74 68 61 74 20 6b 65 79 0d 0a 2a  ites that key..*
271a1 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78  * into the index
271a2 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74   P1.  Data for t
271a3 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e  he entry is nil.
271a4 0d 0a 2a 2a 0d 0a 2a 2a 20 50 33 20 69 73 20 61  ..**..** P3 is a
271a5 20 66 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69   flag that provi
271a6 64 65 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68  des a hint to th
271a7 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74  e b-tree layer t
271a8 68 61 74 20 74 68 69 73 0d 0a 2a 2a 20 69 6e 73  hat this..** ins
271a9 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f  ert is likely to
271aa 20 62 65 20 61 6e 20 61 70 70 65 6e 64 2e 0d 0a   be an append...
271ab 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  **..** This inst
271ac 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72  ruction only wor
271ad 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 2e 20  ks for indices. 
271ae 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   The equivalent 
271af 69 6e 73 74 72 75 63 74 69 6f 6e 0d 0a 2a 2a 20  instruction..** 
271b0 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f 50  for tables is OP
271b1 5f 49 6e 73 65 72 74 2e 0d 0a 2a 2f 0d 0a 63 61  _Insert...*/..ca
271b2 73 65 20 4f 50 5f 53 6f 72 74 65 72 49 6e 73 65  se OP_SorterInse
271b3 72 74 3a 20 20 20 20 20 20 20 2f 2a 20 69 6e 32  rt:       /* in2
271b4 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
271b5 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f  TE_OMIT_MERGE_SO
271b6 52 54 0d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64  RT..  pOp->opcod
271b7 65 20 3d 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  e = OP_IdxInsert
271b8 3b 0d 0a 23 65 6e 64 69 66 0d 0a 63 61 73 65 20  ;..#endif..case 
271b9 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 7b 20  OP_IdxInsert: { 
271ba 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f         /* in2 */
271bb 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
271bc 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
271bd 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0d 0a  d into u.br */..
271be 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
271bf 3b 0d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ;..  BtCursor *p
271c0 43 72 73 72 3b 0d 0a 20 20 69 6e 74 20 6e 4b 65  Crsr;..  int nKe
271c1 79 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  y;..  const char
271c2 20 2a 7a 4b 65 79 3b 0d 0a 23 65 6e 64 69 66 20   *zKey;..#endif 
271c3 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
271c4 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
271c5 62 72 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72  br */....  asser
271c6 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
271c7 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
271c8 73 6f 72 20 29 3b 0d 0a 20 20 75 2e 62 72 2e 70  sor );..  u.br.p
271c9 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
271ca 2d 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74  ->p1];..  assert
271cb 28 20 75 2e 62 72 2e 70 43 21 3d 30 20 29 3b 0d  ( u.br.pC!=0 );.
271cc 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 72 2e  .  assert( u.br.
271cd 70 43 2d 3e 69 73 53 6f 72 74 65 72 3d 3d 28 70  pC->isSorter==(p
271ce 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
271cf 6f 72 74 65 72 49 6e 73 65 72 74 29 20 29 3b 0d  orterInsert) );.
271d0 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b  .  pIn2 = &aMem[
271d1 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 61 73 73  pOp->p2];..  ass
271d2 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73  ert( pIn2->flags
271d3 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0d 0a   & MEM_Blob );..
271d4 20 20 75 2e 62 72 2e 70 43 72 73 72 20 3d 20 75    u.br.pCrsr = u
271d5 2e 62 72 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b  .br.pC->pCursor;
271d6 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ..  if( ALWAYS(u
271d7 2e 62 72 2e 70 43 72 73 72 21 3d 30 29 20 29 7b  .br.pCrsr!=0) ){
271d8 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ..    assert( u.
271d9 62 72 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d  br.pC->isTable==
271da 30 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 45  0 );..    rc = E
271db 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b  xpandBlob(pIn2);
271dc 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
271dd 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
271de 20 20 69 66 28 20 69 73 53 6f 72 74 65 72 28 75    if( isSorter(u
271df 2e 62 72 2e 70 43 29 20 29 7b 0d 0a 20 20 20 20  .br.pC) ){..    
271e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
271e1 56 64 62 65 53 6f 72 74 65 72 57 72 69 74 65 28  VdbeSorterWrite(
271e2 64 62 2c 20 75 2e 62 72 2e 70 43 2c 20 70 49 6e  db, u.br.pC, pIn
271e3 32 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  2);..      }else
271e4 7b 0d 0a 20 20 20 20 20 20 20 20 75 2e 62 72 2e  {..        u.br.
271e5 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0d  nKey = pIn2->n;.
271e6 0a 20 20 20 20 20 20 20 20 75 2e 62 72 2e 7a 4b  .        u.br.zK
271e7 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0d 0a 20  ey = pIn2->z;.. 
271e8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
271e9 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 75  te3BtreeInsert(u
271ea 2e 62 72 2e 70 43 72 73 72 2c 20 75 2e 62 72 2e  .br.pCrsr, u.br.
271eb 7a 4b 65 79 2c 20 75 2e 62 72 2e 6e 4b 65 79 2c  zKey, u.br.nKey,
271ec 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e   "", 0, 0, pOp->
271ed 70 33 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  p3,..           
271ee 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46   ((pOp->p5 & OPF
271ef 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
271f0 54 29 20 3f 20 75 2e 62 72 2e 70 43 2d 3e 73 65  T) ? u.br.pC->se
271f1 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 0d 0a 20  ekResult : 0).. 
271f2 20 20 20 20 20 20 20 20 20 20 20 29 3b 0d 0a 20             );.. 
271f3 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75         assert( u
271f4 2e 62 72 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .br.pC->deferred
271f5 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0d 0a 20 20  Moveto==0 );..  
271f6 20 20 20 20 20 20 75 2e 62 72 2e 70 43 2d 3e 63        u.br.pC->c
271f7 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
271f8 48 45 5f 53 54 41 4c 45 3b 0d 0a 20 20 20 20 20  HE_STALE;..     
271f9 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
271fa 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
271fb 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c  * Opcode: IdxDel
271fc 65 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ete P1 P2 P3 * *
271fd 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 6e  ..**..** The con
271fe 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73  tent of P3 regis
271ff 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74  ters starting at
27200 20 72 65 67 69 73 74 65 72 20 50 32 20 66 6f 72   register P2 for
27201 6d 0d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65  m..** an unpacke
27202 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69  d index key. Thi
27203 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73  s opcode removes
27204 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d   that entry from
27205 20 74 68 65 20 0d 0a 2a 2a 20 69 6e 64 65 78 20   the ..** index 
27206 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72  opened by cursor
27207 20 50 31 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f   P1...*/..case O
27208 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0d 0a  P_IdxDelete: {..
27209 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
2720a 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
2720b 69 6e 74 6f 20 75 2e 62 73 20 2a 2f 0d 0a 20 20  into u.bs */..  
2720c 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0d  VdbeCursor *pC;.
2720d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
2720e 73 72 3b 0d 0a 20 20 69 6e 74 20 72 65 73 3b 0d  sr;..  int res;.
2720f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
27210 64 20 72 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  d r;..#endif /* 
27211 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
27212 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20  moved into u.bs 
27213 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
27214 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0d 0a 20 20  pOp->p3>0 );..  
27215 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
27216 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70  0 && pOp->p2+pOp
27217 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  ->p3<=p->nMem+1 
27218 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f  );..  assert( pO
27219 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
2721a 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
2721b 3b 0d 0a 20 20 75 2e 62 73 2e 70 43 20 3d 20 70  ;..  u.bs.pC = p
2721c 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
2721d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ;..  assert( u.b
2721e 73 2e 70 43 21 3d 30 20 29 3b 0d 0a 20 20 75 2e  s.pC!=0 );..  u.
2721f 62 73 2e 70 43 72 73 72 20 3d 20 75 2e 62 73 2e  bs.pCrsr = u.bs.
27220 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0d 0a 20 20  pC->pCursor;..  
27221 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 73 2e  if( ALWAYS(u.bs.
27222 70 43 72 73 72 21 3d 30 29 20 29 7b 0d 0a 20 20  pCrsr!=0) ){..  
27223 20 20 75 2e 62 73 2e 72 2e 70 4b 65 79 49 6e 66    u.bs.r.pKeyInf
27224 6f 20 3d 20 75 2e 62 73 2e 70 43 2d 3e 70 4b 65  o = u.bs.pC->pKe
27225 79 49 6e 66 6f 3b 0d 0a 20 20 20 20 75 2e 62 73  yInfo;..    u.bs
27226 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  .r.nField = (u16
27227 29 70 4f 70 2d 3e 70 33 3b 0d 0a 20 20 20 20 75  )pOp->p3;..    u
27228 2e 62 73 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b  .bs.r.flags = 0;
27229 0d 0a 20 20 20 20 75 2e 62 73 2e 72 2e 61 4d 65  ..    u.bs.r.aMe
2722a 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  m = &aMem[pOp->p
2722b 32 5d 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  2];..#ifdef SQLI
2722c 54 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 7b 20  TE_DEBUG..    { 
2722d 69 6e 74 20 69 3b 20 66 6f 72 28 69 3d 30 3b 20  int i; for(i=0; 
2722e 69 3c 75 2e 62 73 2e 72 2e 6e 46 69 65 6c 64 3b  i<u.bs.r.nField;
2722f 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 6d 65   i++) assert( me
27230 6d 49 73 56 61 6c 69 64 28 26 75 2e 62 73 2e 72  mIsValid(&u.bs.r
27231 2e 61 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d 0d 0a  .aMem[i]) ); }..
27232 23 65 6e 64 69 66 0d 0a 20 20 20 20 72 63 20 3d  #endif..    rc =
27233 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
27234 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 73  etoUnpacked(u.bs
27235 2e 70 43 72 73 72 2c 20 26 75 2e 62 73 2e 72 2c  .pCrsr, &u.bs.r,
27236 20 30 2c 20 30 2c 20 26 75 2e 62 73 2e 72 65 73   0, 0, &u.bs.res
27237 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  );..    if( rc==
27238 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62  SQLITE_OK && u.b
27239 73 2e 72 65 73 3d 3d 30 20 29 7b 0d 0a 20 20 20  s.res==0 ){..   
2723a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2723b 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 73 2e  treeDelete(u.bs.
2723c 70 43 72 73 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a  pCrsr);..    }..
2723d 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 73      assert( u.bs
2723e 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
2723f 65 74 6f 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 75  eto==0 );..    u
27240 2e 62 73 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bs.pC->cacheSta
27241 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
27242 45 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b  E;..  }..  break
27243 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
27244 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50  e: IdxRowid P1 P
27245 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  2 * * *..**..** 
27246 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73  Write into regis
27247 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65  ter P2 an intege
27248 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6c  r which is the l
27249 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
2724a 20 72 65 63 6f 72 64 20 61 74 0d 0a 2a 2a 20 74   record at..** t
2724b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
2724c 64 65 78 20 6b 65 79 20 70 6f 69 6e 74 65 64 20  dex key pointed 
2724d 74 6f 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e  to by cursor P1.
2724e 20 20 54 68 69 73 20 69 6e 74 65 67 65 72 20 73    This integer s
2724f 68 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20 74 68 65  hould be..** the
27250 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 74 61   rowid of the ta
27251 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69  ble entry to whi
27252 63 68 20 74 68 69 73 20 69 6e 64 65 78 20 65 6e  ch this index en
27253 74 72 79 20 70 6f 69 6e 74 73 2e 0d 0a 2a 2a 0d  try points...**.
27254 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f  .** See also: Ro
27255 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64 2e  wid, MakeRecord.
27256 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49 64  ..*/..case OP_Id
27257 78 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20  xRowid: {       
27258 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
27259 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 23 69  rerelease */..#i
2725a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
2725b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2725c 74 6f 20 75 2e 62 74 20 2a 2f 0d 0a 20 20 42 74  to u.bt */..  Bt
2725d 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0d 0a  Cursor *pCrsr;..
2725e 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
2725f 3b 0d 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 0d  ;..  i64 rowid;.
27260 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
27261 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
27262 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0d 0a 0d   into u.bt */...
27263 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
27264 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
27265 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0d 0a  <p->nCursor );..
27266 20 20 75 2e 62 74 2e 70 43 20 3d 20 70 2d 3e 61    u.bt.pC = p->a
27267 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a  pCsr[pOp->p1];..
27268 20 20 61 73 73 65 72 74 28 20 75 2e 62 74 2e 70    assert( u.bt.p
27269 43 21 3d 30 20 29 3b 0d 0a 20 20 75 2e 62 74 2e  C!=0 );..  u.bt.
2726a 70 43 72 73 72 20 3d 20 75 2e 62 74 2e 70 43 2d  pCrsr = u.bt.pC-
2726b 3e 70 43 75 72 73 6f 72 3b 0d 0a 20 20 70 4f 75  >pCursor;..  pOu
2726c 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  t->flags = MEM_N
2726d 75 6c 6c 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41  ull;..  if( ALWA
2726e 59 53 28 75 2e 62 74 2e 70 43 72 73 72 21 3d 30  YS(u.bt.pCrsr!=0
2726f 29 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73  ) ){..    rc = s
27270 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
27271 4d 6f 76 65 74 6f 28 75 2e 62 74 2e 70 43 29 3b  Moveto(u.bt.pC);
27272 0d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ..    if( NEVER(
27273 72 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  rc) ) goto abort
27274 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0d 0a  _due_to_error;..
27275 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 74      assert( u.bt
27276 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
27277 65 74 6f 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 61  eto==0 );..    a
27278 73 73 65 72 74 28 20 75 2e 62 74 2e 70 43 2d 3e  ssert( u.bt.pC->
27279 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20  isTable==0 );.. 
2727a 20 20 20 69 66 28 20 21 75 2e 62 74 2e 70 43 2d     if( !u.bt.pC-
2727b 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0d 0a 20 20 20  >nullRow ){..   
2727c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
2727d 64 62 65 49 64 78 52 6f 77 69 64 28 64 62 2c 20  dbeIdxRowid(db, 
2727e 75 2e 62 74 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bt.pCrsr, &u.b
2727f 74 2e 72 6f 77 69 64 29 3b 0d 0a 20 20 20 20 20  t.rowid);..     
27280 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
27281 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67  OK ){..        g
27282 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
27283 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 20 20 7d  _error;..      }
27284 0d 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e  ..      pOut->u.
27285 69 20 3d 20 75 2e 62 74 2e 72 6f 77 69 64 3b 0d  i = u.bt.rowid;.
27286 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  .      pOut->fla
27287 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0d 0a 20  gs = MEM_Int;.. 
27288 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 62 72 65     }..  }..  bre
27289 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
2728a 6f 64 65 3a 20 49 64 78 47 45 20 50 31 20 50 32  ode: IdxGE P1 P2
2728b 20 50 33 20 50 34 20 50 35 0d 0a 2a 2a 0d 0a 2a   P3 P4 P5..**..*
2728c 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65  * The P4 registe
2728d 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69  r values beginni
2728e 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20  ng with P3 form 
2728f 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
27290 78 20 0d 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20  x ..** key that 
27291 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e  omits the ROWID.
27292 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b    Compare this k
27293 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74  ey value against
27294 20 74 68 65 20 69 6e 64 65 78 20 0d 0a 2a 2a 20   the index ..** 
27295 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65  that P1 is curre
27296 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
27297 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52  , ignoring the R
27298 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
27299 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  ndex...**..** If
2729a 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e   the P1 index en
2729b 74 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74  try is greater t
2729c 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
2729d 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0d 0a 2a  the key value..*
2729e 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  * then jump to P
2729f 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 61  2.  Otherwise fa
272a0 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
272a1 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
272a2 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 50  on...**..** If P
272a3 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  5 is non-zero th
272a4 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  en the key value
272a5 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79   is increased by
272a6 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0d 0a 2a 2a   an epsilon ..**
272a7 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f   prior to the co
272a8 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20  mparison.  This 
272a9 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20  make the opcode 
272aa 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20  work like IdxGT 
272ab 65 78 63 65 70 74 0d 0a 2a 2a 20 74 68 61 74 20  except..** that 
272ac 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20  if the key from 
272ad 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 61  register P3 is a
272ae 20 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b   prefix of the k
272af 65 79 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72  ey in the cursor
272b0 2c 0d 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  ,..** the result
272b1 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65 61   is false wherea
272b2 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 74 72  s it would be tr
272b3 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0d 0a  ue with IdxGT...
272b4 2a 2f 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  */../* Opcode: I
272b5 64 78 4c 54 20 50 31 20 50 32 20 50 33 20 50 34  dxLT P1 P2 P3 P4
272b6 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   P5..**..** The 
272b7 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c 75  P4 register valu
272b8 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  es beginning wit
272b9 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70  h P3 form an unp
272ba 61 63 6b 65 64 20 69 6e 64 65 78 20 0d 0a 2a 2a  acked index ..**
272bb 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20   key that omits 
272bc 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70  the ROWID.  Comp
272bd 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c  are this key val
272be 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69  ue against the i
272bf 6e 64 65 78 20 0d 0a 2a 2a 20 74 68 61 74 20 50  ndex ..** that P
272c0 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  1 is currently p
272c1 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f  ointing to, igno
272c2 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f  ring the ROWID o
272c3 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0d  n the P1 index..
272c4 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 50  .**..** If the P
272c5 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  1 index entry is
272c6 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b   less than the k
272c7 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75  ey value then ju
272c8 6d 70 20 74 6f 20 50 32 2e 0d 0a 2a 2a 20 4f 74  mp to P2...** Ot
272c9 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72  herwise fall thr
272ca 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
272cb 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a   instruction...*
272cc 2a 0d 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e  *..** If P5 is n
272cd 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65  on-zero then the
272ce 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e   key value is in
272cf 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70  creased by an ep
272d0 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0d 0a 2a 2a  silon prior ..**
272d1 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
272d2 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  on.  This makes 
272d3 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
272d4 6c 69 6b 65 20 49 64 78 4c 45 2e 0d 0a 2a 2f 0d  like IdxLE...*/.
272d5 0a 63 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20  .case OP_IdxLT: 
272d6 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
272d7 20 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49 64 78   */..case OP_Idx
272d8 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  GE: {        /* 
272d9 6a 75 6d 70 20 2a 2f 0d 0a 23 69 66 20 30 20 20  jump */..#if 0  
272da 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
272db 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
272dc 62 75 20 2a 2f 0d 0a 20 20 56 64 62 65 43 75 72  bu */..  VdbeCur
272dd 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 69 6e 74 20  sor *pC;..  int 
272de 72 65 73 3b 0d 0a 20 20 55 6e 70 61 63 6b 65 64  res;..  Unpacked
272df 52 65 63 6f 72 64 20 72 3b 0d 0a 23 65 6e 64 69  Record r;..#endi
272e0 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
272e1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
272e2 75 2e 62 75 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  u.bu */....  ass
272e3 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
272e4 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
272e5 75 72 73 6f 72 20 29 3b 0d 0a 20 20 75 2e 62 75  ursor );..  u.bu
272e6 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
272e7 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65  Op->p1];..  asse
272e8 72 74 28 20 75 2e 62 75 2e 70 43 21 3d 30 20 29  rt( u.bu.pC!=0 )
272e9 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ;..  assert( u.b
272ea 75 2e 70 43 2d 3e 69 73 4f 72 64 65 72 65 64 20  u.pC->isOrdered 
272eb 29 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53  );..  if( ALWAYS
272ec 28 75 2e 62 75 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bu.pC->pCurso
272ed 72 21 3d 30 29 20 29 7b 0d 0a 20 20 20 20 61 73  r!=0) ){..    as
272ee 73 65 72 74 28 20 75 2e 62 75 2e 70 43 2d 3e 64  sert( u.bu.pC->d
272ef 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
272f0 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
272f1 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70   pOp->p5==0 || p
272f2 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0d 0a 20 20  Op->p5==1 );..  
272f3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
272f4 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
272f5 29 3b 0d 0a 20 20 20 20 75 2e 62 75 2e 72 2e 70  );..    u.bu.r.p
272f6 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 75 2e 70  KeyInfo = u.bu.p
272f7 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20  C->pKeyInfo;..  
272f8 20 20 75 2e 62 75 2e 72 2e 6e 46 69 65 6c 64 20    u.bu.r.nField 
272f9 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69  = (u16)pOp->p4.i
272fa 3b 0d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  ;..    if( pOp->
272fb 70 35 20 29 7b 0d 0a 20 20 20 20 20 20 75 2e 62  p5 ){..      u.b
272fc 75 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41  u.r.flags = UNPA
272fd 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55  CKED_INCRKEY | U
272fe 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d  NPACKED_PREFIX_M
272ff 41 54 43 48 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  ATCH;..    }else
27300 7b 0d 0a 20 20 20 20 20 20 75 2e 62 75 2e 72 2e  {..      u.bu.r.
27301 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44  flags = UNPACKED
27302 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b 0d 0a  _PREFIX_MATCH;..
27303 20 20 20 20 7d 0d 0a 20 20 20 20 75 2e 62 75 2e      }..    u.bu.
27304 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70  r.aMem = &aMem[p
27305 4f 70 2d 3e 70 33 5d 3b 0d 0a 23 69 66 64 65 66  Op->p3];..#ifdef
27306 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0d 0a 20   SQLITE_DEBUG.. 
27307 20 20 20 7b 20 69 6e 74 20 69 3b 20 66 6f 72 28     { int i; for(
27308 69 3d 30 3b 20 69 3c 75 2e 62 75 2e 72 2e 6e 46  i=0; i<u.bu.r.nF
27309 69 65 6c 64 3b 20 69 2b 2b 29 20 61 73 73 65 72  ield; i++) asser
2730a 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 26 75  t( memIsValid(&u
2730b 2e 62 75 2e 72 2e 61 4d 65 6d 5b 69 5d 29 20 29  .bu.r.aMem[i]) )
2730c 3b 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  ; }..#endif..   
2730d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
2730e 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75  eIdxKeyCompare(u
2730f 2e 62 75 2e 70 43 2c 20 26 75 2e 62 75 2e 72 2c  .bu.pC, &u.bu.r,
27310 20 26 75 2e 62 75 2e 72 65 73 29 3b 0d 0a 20 20   &u.bu.res);..  
27311 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
27312 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0d 0a  e==OP_IdxLT ){..
27313 20 20 20 20 20 20 75 2e 62 75 2e 72 65 73 20 3d        u.bu.res =
27314 20 2d 75 2e 62 75 2e 72 65 73 3b 0d 0a 20 20 20   -u.bu.res;..   
27315 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61   }else{..      a
27316 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f  ssert( pOp->opco
27317 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0d  de==OP_IdxGE );.
27318 0a 20 20 20 20 20 20 75 2e 62 75 2e 72 65 73 2b  .      u.bu.res+
27319 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  +;..    }..    i
2731a 66 28 20 75 2e 62 75 2e 72 65 73 3e 30 20 29 7b  f( u.bu.res>0 ){
2731b 0d 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  ..      pc = pOp
2731c 2d 3e 70 32 20 2d 20 31 20 3b 0d 0a 20 20 20 20  ->p2 - 1 ;..    
2731d 7d 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b  }..  }..  break;
2731e 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  ..}..../* Opcode
2731f 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20  : Destroy P1 P2 
27320 50 33 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 44  P3 * *..**..** D
27321 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
27322 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
27323 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f  r index whose ro
27324 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ot page in the d
27325 61 74 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c 65  atabase..** file
27326 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e   is given by P1.
27327 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 61 62  ..**..** The tab
27328 6c 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79  le being destroy
27329 65 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69  ed is in the mai
2732a 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
2732b 69 66 20 50 33 3d 3d 30 2e 20 20 49 66 0d 0a 2a  if P3==0.  If..*
2732c 2a 20 50 33 3d 3d 31 20 74 68 65 6e 20 74 68 65  * P3==1 then the
2732d 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65   table to be cle
2732e 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78  ar is in the aux
2732f 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
27330 66 69 6c 65 0d 0a 2a 2a 20 74 68 61 74 20 69 73  file..** that is
27331 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
27332 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69  ables create usi
27333 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52  ng CREATE TEMPOR
27334 41 52 59 20 54 41 42 4c 45 2e 0d 0a 2a 2a 0d 0a  ARY TABLE...**..
27335 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d  ** If AUTOVACUUM
27336 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65 6e   is enabled then
27337 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
27338 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f 6f  that another roo
27339 74 20 70 61 67 65 0d 0a 2a 2a 20 6d 69 67 68 74  t page..** might
2733a 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   be moved into t
2733b 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64  he newly deleted
2733c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72   root page in or
2733d 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0d  der to keep all.
2733e 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63  .** root pages c
2733f 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65  ontiguous at the
27340 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
27341 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
27342 20 66 6f 72 6d 65 72 0d 0a 2a 2a 20 76 61 6c 75   former..** valu
27343 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61  e of the root pa
27344 67 65 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20  ge that moved - 
27345 69 74 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65  its value before
27346 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72   the move occurr
27347 65 64 20 2d 0d 0a 2a 2a 20 69 73 20 73 74 6f 72  ed -..** is stor
27348 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
27349 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 0d  2.  If no page .
2734a 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73  .** movement was
2734b 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61 75   required (becau
2734c 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  se the table bei
2734d 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20 61  ng dropped was a
2734e 6c 72 65 61 64 79 20 0d 0a 2a 2a 20 74 68 65 20  lready ..** the 
2734f 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20  last one in the 
27350 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61  database) then a
27351 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20   zero is stored 
27352 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0d  in register P2..
27353 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55  .** If AUTOVACUU
27354 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68  M is disabled th
27355 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f  en a zero is sto
27356 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
27357 50 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20  P2...**..** See 
27358 61 6c 73 6f 3a 20 43 6c 65 61 72 0d 0a 2a 2f 0d  also: Clear..*/.
27359 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79  .case OP_Destroy
2735a 3a 20 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d  : {     /* out2-
2735b 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 23  prerelease */..#
2735c 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
2735d 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
2735e 6e 74 6f 20 75 2e 62 76 20 2a 2f 0d 0a 20 20 69  nto u.bv */..  i
2735f 6e 74 20 69 4d 6f 76 65 64 3b 0d 0a 20 20 69 6e  nt iMoved;..  in
27360 74 20 69 43 6e 74 3b 0d 0a 20 20 56 64 62 65 20  t iCnt;..  Vdbe 
27361 2a 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69  *pVdbe;..  int i
27362 44 62 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Db;..#endif /* l
27363 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
27364 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a  oved into u.bv *
27365 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
27366 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
27367 42 4c 45 0d 0a 20 20 75 2e 62 76 2e 69 43 6e 74  BLE..  u.bv.iCnt
27368 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28 75 2e 62   = 0;..  for(u.b
27369 76 2e 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62  v.pVdbe=db->pVdb
2736a 65 3b 20 75 2e 62 76 2e 70 56 64 62 65 3b 20 75  e; u.bv.pVdbe; u
2736b 2e 62 76 2e 70 56 64 62 65 20 3d 20 75 2e 62 76  .bv.pVdbe = u.bv
2736c 2e 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0d  .pVdbe->pNext){.
2736d 0a 20 20 20 20 69 66 28 20 75 2e 62 76 2e 70 56  .    if( u.bv.pV
2736e 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  dbe->magic==VDBE
2736f 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 75 2e  _MAGIC_RUN && u.
27370 62 76 2e 70 56 64 62 65 2d 3e 69 6e 56 74 61 62  bv.pVdbe->inVtab
27371 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 2e 62 76  Method<2 && u.bv
27372 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b  .pVdbe->pc>=0 ){
27373 0d 0a 20 20 20 20 20 20 75 2e 62 76 2e 69 43 6e  ..      u.bv.iCn
27374 74 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  t++;..    }..  }
27375 0d 0a 23 65 6c 73 65 0d 0a 20 20 75 2e 62 76 2e  ..#else..  u.bv.
27376 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76  iCnt = db->activ
27377 65 56 64 62 65 43 6e 74 3b 0d 0a 23 65 6e 64 69  eVdbeCnt;..#endi
27378 66 0d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  f..  pOut->flags
27379 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20   = MEM_Null;..  
2737a 69 66 28 20 75 2e 62 76 2e 69 43 6e 74 3e 31 20  if( u.bv.iCnt>1 
2737b 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
2737c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0d 0a 20 20 20  ITE_LOCKED;..   
2737d 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20   p->errorAction 
2737e 3d 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 7d  = OE_Abort;..  }
2737f 65 6c 73 65 7b 0d 0a 20 20 20 20 75 2e 62 76 2e  else{..    u.bv.
27380 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0d 0a  iDb = pOp->p3;..
27381 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76      assert( u.bv
27382 2e 69 43 6e 74 3d 3d 31 20 29 3b 0d 0a 20 20 20  .iCnt==1 );..   
27383 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
27384 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62 4d  eeMask & (((yDbM
27385 61 73 6b 29 31 29 3c 3c 75 2e 62 76 2e 69 44 62  ask)1)<<u.bv.iDb
27386 29 29 21 3d 30 20 29 3b 0d 0a 20 20 20 20 72 63  ))!=0 );..    rc
27387 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
27388 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62  ropTable(db->aDb
27389 5b 75 2e 62 76 2e 69 44 62 5d 2e 70 42 74 2c 20  [u.bv.iDb].pBt, 
2738a 70 4f 70 2d 3e 70 31 2c 20 26 75 2e 62 76 2e 69  pOp->p1, &u.bv.i
2738b 4d 6f 76 65 64 29 3b 0d 0a 20 20 20 20 70 4f 75  Moved);..    pOu
2738c 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  t->flags = MEM_I
2738d 6e 74 3b 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt;..    pOut->u
2738e 2e 69 20 3d 20 75 2e 62 76 2e 69 4d 6f 76 65 64  .i = u.bv.iMoved
2738f 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
27390 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
27391 4d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  M..    if( rc==S
27392 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 76  QLITE_OK && u.bv
27393 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0d 0a 20  .iMoved!=0 ){.. 
27394 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f 74       sqlite3Root
27395 50 61 67 65 4d 6f 76 65 64 28 64 62 2c 20 75 2e  PageMoved(db, u.
27396 62 76 2e 69 44 62 2c 20 75 2e 62 76 2e 69 4d 6f  bv.iDb, u.bv.iMo
27397 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0d 0a  ved, pOp->p1);..
27398 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 4f 50 5f        /* All OP_
27399 44 65 73 74 72 6f 79 20 6f 70 65 72 61 74 69 6f  Destroy operatio
2739a 6e 73 20 6f 63 63 75 72 20 6f 6e 20 74 68 65 20  ns occur on the 
2739b 73 61 6d 65 20 62 74 72 65 65 20 2a 2f 0d 0a 20  same btree */.. 
2739c 20 20 20 20 20 61 73 73 65 72 74 28 20 72 65 73       assert( res
2739d 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 3d  etSchemaOnFault=
2739e 3d 30 20 7c 7c 20 72 65 73 65 74 53 63 68 65 6d  =0 || resetSchem
2739f 61 4f 6e 46 61 75 6c 74 3d 3d 75 2e 62 76 2e 69  aOnFault==u.bv.i
273a0 44 62 2b 31 20 29 3b 0d 0a 20 20 20 20 20 20 72  Db+1 );..      r
273a1 65 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c  esetSchemaOnFaul
273a2 74 20 3d 20 75 2e 62 76 2e 69 44 62 2b 31 3b 0d  t = u.bv.iDb+1;.
273a3 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
273a4 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
273a5 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  ..../* Opcode: C
273a6 6c 65 61 72 20 50 31 20 50 32 20 50 33 0d 0a 2a  lear P1 P2 P3..*
273a7 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c  *..** Delete all
273a8 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
273a9 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
273aa 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72  or index whose r
273ab 6f 6f 74 20 70 61 67 65 0d 0a 2a 2a 20 69 6e 20  oot page..** in 
273ac 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
273ad 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31  e is given by P1
273ae 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44  .  But, unlike D
273af 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0d 0a  estroy, do not..
273b0 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61  ** remove the ta
273b1 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f  ble or index fro
273b2 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
273b3 69 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ile...**..** The
273b4 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65   table being cle
273b5 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69  ar is in the mai
273b6 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
273b7 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0d 0a 2a  if P2==0.  If..*
273b8 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65  * P2==1 then the
273b9 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65   table to be cle
273ba 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78  ar is in the aux
273bb 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
273bc 66 69 6c 65 0d 0a 2a 2a 20 74 68 61 74 20 69 73  file..** that is
273bd 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
273be 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69  ables create usi
273bf 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52  ng CREATE TEMPOR
273c0 41 52 59 20 54 41 42 4c 45 2e 0d 0a 2a 2a 0d 0a  ARY TABLE...**..
273c1 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c  ** If the P3 val
273c2 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ue is non-zero, 
273c3 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72  then the table r
273c4 65 66 65 72 72 65 64 20 74 6f 20 6d 75 73 74 20  eferred to must 
273c5 62 65 20 61 6e 0d 0a 2a 2a 20 69 6e 74 6b 65 79  be an..** intkey
273c6 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 74   table (an SQL t
273c7 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64  able, not an ind
273c8 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ex). In this cas
273c9 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65  e the row change
273ca 20 0d 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69   ..** count is i
273cb 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68  ncremented by th
273cc 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
273cd 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65   in the table be
273ce 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 0d 0a 2a  ing cleared. ..*
273cf 2a 20 49 66 20 50 33 20 69 73 20 67 72 65 61 74  * If P3 is great
273d0 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  er than zero, th
273d1 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  en the value sto
273d2 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
273d3 50 33 20 69 73 0d 0a 2a 2a 20 61 6c 73 6f 20 69  P3 is..** also i
273d4 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68  ncremented by th
273d5 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
273d6 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65   in the table be
273d7 69 6e 67 20 63 6c 65 61 72 65 64 2e 0d 0a 2a 2a  ing cleared...**
273d8 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44  ..** See also: D
273d9 65 73 74 72 6f 79 0d 0a 2a 2f 0d 0a 63 61 73 65  estroy..*/..case
273da 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0d 0a 23 69   OP_Clear: {..#i
273db 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
273dc 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
273dd 74 6f 20 75 2e 62 77 20 2a 2f 0d 0a 20 20 69 6e  to u.bw */..  in
273de 74 20 6e 43 68 61 6e 67 65 3b 0d 0a 23 65 6e 64  t nChange;..#end
273df 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
273e0 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
273e1 20 75 2e 62 77 20 2a 2f 0d 0a 0d 0a 20 20 75 2e   u.bw */....  u.
273e2 62 77 2e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0d  bw.nChange = 0;.
273e3 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
273e4 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44  treeMask & (((yD
273e5 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70 2d 3e 70  bMask)1)<<pOp->p
273e6 32 29 29 21 3d 30 20 29 3b 0d 0a 20 20 72 63 20  2))!=0 );..  rc 
273e7 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  = sqlite3BtreeCl
273e8 65 61 72 54 61 62 6c 65 28 0d 0a 20 20 20 20 20  earTable(..     
273e9 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32   db->aDb[pOp->p2
273ea 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20  ].pBt, pOp->p1, 
273eb 28 70 4f 70 2d 3e 70 33 20 3f 20 26 75 2e 62 77  (pOp->p3 ? &u.bw
273ec 2e 6e 43 68 61 6e 67 65 20 3a 20 30 29 0d 0a 20  .nChange : 0).. 
273ed 20 29 3b 0d 0a 20 20 69 66 28 20 70 4f 70 2d 3e   );..  if( pOp->
273ee 70 33 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6e 43  p3 ){..    p->nC
273ef 68 61 6e 67 65 20 2b 3d 20 75 2e 62 77 2e 6e 43  hange += u.bw.nC
273f0 68 61 6e 67 65 3b 0d 0a 20 20 20 20 69 66 28 20  hange;..    if( 
273f1 70 4f 70 2d 3e 70 33 3e 30 20 29 7b 0d 0a 20 20  pOp->p3>0 ){..  
273f2 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49      assert( memI
273f3 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70 4f 70  sValid(&aMem[pOp
273f4 2d 3e 70 33 5d 29 20 29 3b 0d 0a 20 20 20 20 20  ->p3]) );..     
273f5 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67   memAboutToChang
273f6 65 28 70 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  e(p, &aMem[pOp->
273f7 70 33 5d 29 3b 0d 0a 20 20 20 20 20 20 61 4d 65  p3]);..      aMe
273f8 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b  m[pOp->p3].u.i +
273f9 3d 20 75 2e 62 77 2e 6e 43 68 61 6e 67 65 3b 0d  = u.bw.nChange;.
273fa 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 62  .    }..  }..  b
273fb 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f  reak;..}..../* O
273fc 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 61 62  pcode: CreateTab
273fd 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a  le P1 P2 * * *..
273fe 2a 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  **..** Allocate 
273ff 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74  a new table in t
27400 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
27401 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f   file if P1==0 o
27402 72 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 61 75 78  r in the..** aux
27403 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
27404 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72  file if P1==1 or
27405 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20   in an attached 
27406 64 61 74 61 62 61 73 65 20 69 66 0d 0a 2a 2a 20  database if..** 
27407 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65  P1>1.  Write the
27408 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
27409 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  r of the new tab
2740a 6c 65 20 69 6e 74 6f 0d 0a 2a 2a 20 72 65 67 69  le into..** regi
2740b 73 74 65 72 20 50 32 0d 0a 2a 2a 0d 0a 2a 2a 20  ster P2..**..** 
2740c 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
2740d 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61  etween a table a
2740e 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74  nd an index is t
2740f 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75  his:  A table mu
27410 73 74 0d 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d  st..** have a 4-
27411 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79  byte integer key
27412 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72   and can have ar
27413 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41  bitrary data.  A
27414 6e 20 69 6e 64 65 78 0d 0a 2a 2a 20 68 61 73 20  n index..** has 
27415 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79  an arbitrary key
27416 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0d 0a 2a   but no data...*
27417 2a 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  *..** See also: 
27418 43 72 65 61 74 65 49 6e 64 65 78 0d 0a 2a 2f 0d  CreateIndex..*/.
27419 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61  ./* Opcode: Crea
2741a 74 65 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20  teIndex P1 P2 * 
2741b 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 6f  * *..**..** Allo
2741c 63 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78  cate a new index
2741d 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
2741e 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
2741f 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0d 0a 2a  ==0 or in the..*
27420 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  * auxiliary data
27421 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d  base file if P1=
27422 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61  =1 or in an atta
27423 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66  ched database if
27424 0d 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74  ..** P1>1.  Writ
27425 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  e the root page 
27426 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
27427 77 20 74 61 62 6c 65 20 69 6e 74 6f 0d 0a 2a 2a  w table into..**
27428 20 72 65 67 69 73 74 65 72 20 50 32 2e 0d 0a 2a   register P2...*
27429 2a 0d 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65  *..** See docume
2742a 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72  ntation on OP_Cr
2742b 65 61 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64  eateTable for ad
2742c 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
2742d 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  tion...*/..case 
2742e 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a 20  OP_CreateIndex: 
2742f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
27430 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
27431 0d 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65  ..case OP_Create
27432 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20  Table: {        
27433 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
27434 65 61 73 65 20 2a 2f 0d 0a 23 69 66 20 30 20 20  ease */..#if 0  
27435 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
27436 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
27437 62 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 70 67 6e  bx */..  int pgn
27438 6f 3b 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  o;..  int flags;
27439 0d 0a 20 20 44 62 20 2a 70 44 62 3b 0d 0a 23 65  ..  Db *pDb;..#e
2743a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
2743b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2743c 74 6f 20 75 2e 62 78 20 2a 2f 0d 0a 0d 0a 20 20  to u.bx */....  
2743d 75 2e 62 78 2e 70 67 6e 6f 20 3d 20 30 3b 0d 0a  u.bx.pgno = 0;..
2743e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
2743f 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
27440 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 61 73  db->nDb );..  as
27441 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
27442 61 73 6b 20 26 20 28 28 28 79 44 62 4d 61 73 6b  ask & (((yDbMask
27443 29 31 29 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d  )1)<<pOp->p1))!=
27444 30 20 29 3b 0d 0a 20 20 75 2e 62 78 2e 70 44 62  0 );..  u.bx.pDb
27445 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d   = &db->aDb[pOp-
27446 3e 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  >p1];..  assert(
27447 20 75 2e 62 78 2e 70 44 62 2d 3e 70 42 74 21 3d   u.bx.pDb->pBt!=
27448 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 4f 70 2d  0 );..  if( pOp-
27449 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61  >opcode==OP_Crea
2744a 74 65 54 61 62 6c 65 20 29 7b 0d 0a 20 20 20 20  teTable ){..    
2744b 2f 2a 20 75 2e 62 78 2e 66 6c 61 67 73 20 3d 20  /* u.bx.flags = 
2744c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f  BTREE_INTKEY; */
2744d 0d 0a 20 20 20 20 75 2e 62 78 2e 66 6c 61 67 73  ..    u.bx.flags
2744e 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b   = BTREE_INTKEY;
2744f 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
27450 75 2e 62 78 2e 66 6c 61 67 73 20 3d 20 42 54 52  u.bx.flags = BTR
27451 45 45 5f 42 4c 4f 42 4b 45 59 3b 0d 0a 20 20 7d  EE_BLOBKEY;..  }
27452 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
27453 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
27454 28 75 2e 62 78 2e 70 44 62 2d 3e 70 42 74 2c 20  (u.bx.pDb->pBt, 
27455 26 75 2e 62 78 2e 70 67 6e 6f 2c 20 75 2e 62 78  &u.bx.pgno, u.bx
27456 2e 66 6c 61 67 73 29 3b 0d 0a 20 20 70 4f 75 74  .flags);..  pOut
27457 2d 3e 75 2e 69 20 3d 20 75 2e 62 78 2e 70 67 6e  ->u.i = u.bx.pgn
27458 6f 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d  o;..  break;..}.
27459 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61  .../* Opcode: Pa
2745a 72 73 65 53 63 68 65 6d 61 20 50 31 20 2a 20 2a  rseSchema P1 * *
2745b 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65   P4 *..**..** Re
2745c 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c  ad and parse all
2745d 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68   entries from th
2745e 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
2745f 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73  table of databas
27460 65 20 50 31 0d 0a 2a 2a 20 74 68 61 74 20 6d 61  e P1..** that ma
27461 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c  tch the WHERE cl
27462 61 75 73 65 20 50 34 2e 20 0d 0a 2a 2a 0d 0a 2a  ause P4. ..**..*
27463 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e  * This opcode in
27464 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 65 72  vokes the parser
27465 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
27466 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
27467 2c 0d 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20  ,..** then runs 
27468 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20  the new virtual 
27469 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 20  machine.  It is 
2746a 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 6e  thus a re-entran
2746b 74 20 6f 70 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 63  t opcode...*/..c
2746c 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65  ase OP_ParseSche
2746d 6d 61 3a 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a  ma: {..#if 0  /*
2746e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2746f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 79   moved into u.by
27470 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d   */..  int iDb;.
27471 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27472 4d 61 73 74 65 72 3b 0d 0a 20 20 63 68 61 72 20  Master;..  char 
27473 2a 7a 53 71 6c 3b 0d 0a 20 20 49 6e 69 74 44 61  *zSql;..  InitDa
27474 74 61 20 69 6e 69 74 44 61 74 61 3b 0d 0a 23 65  ta initData;..#e
27475 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
27476 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
27477 74 6f 20 75 2e 62 79 20 2a 2f 0d 0a 0d 0a 20 20  to u.by */....  
27478 2f 2a 20 41 6e 79 20 70 72 65 70 61 72 65 64 20  /* Any prepared 
27479 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69  statement that i
2747a 6e 76 6f 6b 65 73 20 74 68 69 73 20 6f 70 63 6f  nvokes this opco
2747b 64 65 20 77 69 6c 6c 20 68 6f 6c 64 20 6d 75 74  de will hold mut
2747c 65 78 65 73 0d 0a 20 20 2a 2a 20 6f 6e 20 65 76  exes..  ** on ev
2747d 65 72 79 20 62 74 72 65 65 2e 20 20 54 68 69 73  ery btree.  This
2747e 20 69 73 20 61 20 70 72 65 72 65 71 75 69 73 69   is a prerequisi
2747f 74 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 0d  te for invoking.
27480 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 49 6e 69  .  ** sqlite3Ini
27481 74 43 61 6c 6c 62 61 63 6b 28 29 2e 0d 0a 20 20  tCallback()...  
27482 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
27483 45 5f 44 45 42 55 47 0d 0a 20 20 66 6f 72 28 75  E_DEBUG..  for(u
27484 2e 62 79 2e 69 44 62 3d 30 3b 20 75 2e 62 79 2e  .by.iDb=0; u.by.
27485 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 75 2e 62  iDb<db->nDb; u.b
27486 79 2e 69 44 62 2b 2b 29 7b 0d 0a 20 20 20 20 61  y.iDb++){..    a
27487 73 73 65 72 74 28 20 75 2e 62 79 2e 69 44 62 3d  ssert( u.by.iDb=
27488 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  =1 || sqlite3Btr
27489 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d  eeHoldsMutex(db-
2748a 3e 61 44 62 5b 75 2e 62 79 2e 69 44 62 5d 2e 70  >aDb[u.by.iDb].p
2748b 42 74 29 20 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  Bt) );..  }..#en
2748c 64 69 66 0d 0a 0d 0a 20 20 75 2e 62 79 2e 69 44  dif....  u.by.iD
2748d 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0d 0a 20 20  b = pOp->p1;..  
2748e 61 73 73 65 72 74 28 20 75 2e 62 79 2e 69 44 62  assert( u.by.iDb
2748f 3e 3d 30 20 26 26 20 75 2e 62 79 2e 69 44 62 3c  >=0 && u.by.iDb<
27490 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 61 73  db->nDb );..  as
27491 73 65 72 74 28 20 44 62 48 61 73 50 72 6f 70 65  sert( DbHasPrope
27492 72 74 79 28 64 62 2c 20 75 2e 62 79 2e 69 44 62  rty(db, u.by.iDb
27493 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
27494 64 29 20 29 3b 0d 0a 20 20 2f 2a 20 55 73 65 64  d) );..  /* Used
27495 20 74 6f 20 62 65 20 61 20 63 6f 6e 64 69 74 69   to be a conditi
27496 6f 6e 61 6c 20 2a 2f 20 7b 0d 0a 20 20 20 20 75  onal */ {..    u
27497 2e 62 79 2e 7a 4d 61 73 74 65 72 20 3d 20 53 43  .by.zMaster = SC
27498 48 45 4d 41 5f 54 41 42 4c 45 28 75 2e 62 79 2e  HEMA_TABLE(u.by.
27499 69 44 62 29 3b 0d 0a 20 20 20 20 75 2e 62 79 2e  iDb);..    u.by.
2749a 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62  initData.db = db
2749b 3b 0d 0a 20 20 20 20 75 2e 62 79 2e 69 6e 69 74  ;..    u.by.init
2749c 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e  Data.iDb = pOp->
2749d 70 31 3b 0d 0a 20 20 20 20 75 2e 62 79 2e 69 6e  p1;..    u.by.in
2749e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20  itData.pzErrMsg 
2749f 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0d 0a  = &p->zErrMsg;..
274a0 20 20 20 20 75 2e 62 79 2e 7a 53 71 6c 20 3d 20      u.by.zSql = 
274a1 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
274a2 62 2c 0d 0a 20 20 20 20 20 20 20 22 53 45 4c 45  b,..       "SELE
274a3 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67  CT name, rootpag
274a4 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27  e, sql FROM '%q'
274a5 2e 25 73 20 57 48 45 52 45 20 25 73 20 4f 52 44  .%s WHERE %s ORD
274a6 45 52 20 42 59 20 72 6f 77 69 64 22 2c 0d 0a 20  ER BY rowid",.. 
274a7 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 75 2e        db->aDb[u.
274a8 62 79 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75  by.iDb].zName, u
274a9 2e 62 79 2e 7a 4d 61 73 74 65 72 2c 20 70 4f 70  .by.zMaster, pOp
274aa 2d 3e 70 34 2e 7a 29 3b 0d 0a 20 20 20 20 69 66  ->p4.z);..    if
274ab 28 20 75 2e 62 79 2e 7a 53 71 6c 3d 3d 30 20 29  ( u.by.zSql==0 )
274ac 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  {..      rc = SQ
274ad 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
274ae 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61   }else{..      a
274af 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
274b0 62 75 73 79 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  busy==0 );..    
274b1 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20    db->init.busy 
274b2 3d 20 31 3b 0d 0a 20 20 20 20 20 20 75 2e 62 79  = 1;..      u.by
274b3 2e 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53  .initData.rc = S
274b4 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20  QLITE_OK;..     
274b5 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
274b6 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20  llocFailed );.. 
274b7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
274b8 33 5f 65 78 65 63 28 64 62 2c 20 75 2e 62 79 2e  3_exec(db, u.by.
274b9 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
274ba 74 43 61 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 79  tCallback, &u.by
274bb 2e 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0d 0a  .initData, 0);..
274bc 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
274bd 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 75  LITE_OK ) rc = u
274be 2e 62 79 2e 69 6e 69 74 44 61 74 61 2e 72 63 3b  .by.initData.rc;
274bf 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
274c0 62 46 72 65 65 28 64 62 2c 20 75 2e 62 79 2e 7a  bFree(db, u.by.z
274c1 53 71 6c 29 3b 0d 0a 20 20 20 20 20 20 64 62 2d  Sql);..      db-
274c2 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0d  >init.busy = 0;.
274c3 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69  .    }..  }..  i
274c4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
274c5 4d 45 4d 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f  MEM ){..    goto
274c6 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20   no_mem;..  }.. 
274c7 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 23 69   break;..}....#i
274c8 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
274c9 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0d  E_OMIT_ANALYZE).
274ca 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64  ./* Opcode: Load
274cb 41 6e 61 6c 79 73 69 73 20 50 31 20 2a 20 2a 20  Analysis P1 * * 
274cc 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 61 64  * *..**..** Read
274cd 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
274ce 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74 61  1 table for data
274cf 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61 64  base P1 and load
274d0 20 74 68 65 20 63 6f 6e 74 65 6e 74 0d 0a 2a 2a   the content..**
274d1 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 69   of that table i
274d2 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  nto the internal
274d3 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c   index hash tabl
274d4 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61  e.  This will ca
274d5 75 73 65 0d 0a 2a 2a 20 74 68 65 20 61 6e 61 6c  use..** the anal
274d6 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20  ysis to be used 
274d7 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61  when preparing a
274d8 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75  ll subsequent qu
274d9 65 72 69 65 73 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  eries...*/..case
274da 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73   OP_LoadAnalysis
274db 3a 20 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  : {..  assert( p
274dc 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
274dd 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d  ->p1<db->nDb );.
274de 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
274df 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20  nalysisLoad(db, 
274e0 70 4f 70 2d 3e 70 31 29 3b 0d 0a 20 20 62 72 65  pOp->p1);..  bre
274e1 61 6b 3b 20 20 0d 0a 7d 0d 0a 23 65 6e 64 69 66  ak;  ..}..#endif
274e2 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
274e3 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45  ITE_OMIT_ANALYZE
274e4 29 20 2a 2f 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ) */..../* Opcod
274e5 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20  e: DropTable P1 
274e6 2a 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a  * * P4 *..**..**
274e7 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65   Remove the inte
274e8 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29  rnal (in-memory)
274e9 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
274ea 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0d 0a   that describe..
274eb 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  ** the table nam
274ec 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73  ed P4 in databas
274ed 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63  e P1.  This is c
274ee 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 61  alled after a ta
274ef 62 6c 65 0d 0a 2a 2a 20 69 73 20 64 72 6f 70 70  ble..** is dropp
274f0 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ed in order to k
274f1 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  eep the internal
274f2 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
274f3 6f 66 20 74 68 65 0d 0a 2a 2a 20 73 63 68 65 6d  of the..** schem
274f4 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74  a consistent wit
274f5 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73  h what is on dis
274f6 6b 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  k...*/..case OP_
274f7 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0d 0a 20 20  DropTable: {..  
274f8 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
274f9 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20  DeleteTable(db, 
274fa 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
274fb 2e 7a 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  .z);..  break;..
274fc 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }..../* Opcode: 
274fd 44 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a  DropIndex P1 * *
274fe 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65   P4 *..**..** Re
274ff 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61  move the interna
27500 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61  l (in-memory) da
27501 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ta structures th
27502 61 74 20 64 65 73 63 72 69 62 65 0d 0a 2a 2a 20  at describe..** 
27503 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20  the index named 
27504 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
27505 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
27506 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65  ed after an inde
27507 78 0d 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  x..** is dropped
27508 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
27509 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
2750a 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
2750b 20 74 68 65 0d 0a 2a 2a 20 73 63 68 65 6d 61 20   the..** schema 
2750c 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
2750d 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e  what is on disk.
2750e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 44 72  ..*/..case OP_Dr
2750f 6f 70 49 6e 64 65 78 3a 20 7b 0d 0a 20 20 73 71  opIndex: {..  sq
27510 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
27511 6c 65 74 65 49 6e 64 65 78 28 64 62 2c 20 70 4f  leteIndex(db, pO
27512 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a  p->p1, pOp->p4.z
27513 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d  );..  break;..}.
27514 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72  .../* Opcode: Dr
27515 6f 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a  opTrigger P1 * *
27516 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65   P4 *..**..** Re
27517 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61  move the interna
27518 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61  l (in-memory) da
27519 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ta structures th
2751a 61 74 20 64 65 73 63 72 69 62 65 0d 0a 2a 2a 20  at describe..** 
2751b 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
2751c 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65  d P4 in database
2751d 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61   P1.  This is ca
2751e 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69  lled after a tri
2751f 67 67 65 72 0d 0a 2a 2a 20 69 73 20 64 72 6f 70  gger..** is drop
27520 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ped in order to 
27521 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61  keep the interna
27522 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
27523 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 73 63 68 65   of the..** sche
27524 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69  ma consistent wi
27525 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69  th what is on di
27526 73 6b 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  sk...*/..case OP
27527 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 0d  _DropTrigger: {.
27528 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
27529 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
2752a 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
2752b 70 2d 3e 70 34 2e 7a 29 3b 0d 0a 20 20 62 72 65  p->p4.z);..  bre
2752c 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 23 69 66 6e  ak;..}......#ifn
2752d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2752e 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0d  INTEGRITY_CHECK.
2752f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65  ./* Opcode: Inte
27530 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50 33  grityCk P1 P2 P3
27531 20 2a 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 44 6f   * P5..**..** Do
27532 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
27533 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  the currently op
27534 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74  en database.  St
27535 6f 72 65 20 69 6e 0d 0a 2a 2a 20 72 65 67 69 73  ore in..** regis
27536 74 65 72 20 50 31 20 74 68 65 20 74 65 78 74 20  ter P1 the text 
27537 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  of an error mess
27538 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 20 61  age describing a
27539 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0d 0a 2a 2a  ny problems...**
2753a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20   If no problems 
2753b 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65  are found, store
2753c 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73   a NULL in regis
2753d 74 65 72 20 50 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ter P1...**..** 
2753e 54 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20  The register P3 
2753f 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78  contains the max
27540 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
27541 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0d 0a  llowed errors...
27542 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50  ** At most reg(P
27543 33 29 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62  3) errors will b
27544 65 20 72 65 70 6f 72 74 65 64 2e 0d 0a 2a 2a 20  e reported...** 
27545 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
27546 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f  the analysis sto
27547 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 65  ps as soon as re
27548 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 72 65  g(P1) errors are
27549 20 0d 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67   ..** seen.  Reg
2754a 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 20  (P1) is updated 
2754b 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20  with the number 
2754c 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e  of errors remain
2754d 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ing...**..** The
2754e 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
2754f 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73  rs of all tables
27550 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
27551 20 61 72 65 20 69 6e 74 65 67 65 72 0d 0a 2a 2a   are integer..**
27552 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50   stored in reg(P
27553 31 29 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72  1), reg(P1+1), r
27554 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20  eg(P1+2), ....  
27555 54 68 65 72 65 20 61 72 65 20 50 32 20 74 61 62  There are P2 tab
27556 6c 65 73 0d 0a 2a 2a 20 74 6f 74 61 6c 2e 0d 0a  les..** total...
27557 2a 2a 0d 0a 2a 2a 20 49 66 20 50 35 20 69 73 20  **..** If P5 is 
27558 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 63 68  not zero, the ch
27559 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e 20 74  eck is done on t
2755a 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
2755b 61 62 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 2c 20  abase..** file, 
2755c 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 74  not the main dat
2755d 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 2a 2a 0d  abase file...**.
2755e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
2755f 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
27560 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69  ment the integri
27561 74 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e  ty_check pragma.
27562 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 49 6e  ..*/..case OP_In
27563 74 65 67 72 69 74 79 43 6b 3a 20 7b 0d 0a 23 69  tegrityCk: {..#i
27564 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
27565 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
27566 74 6f 20 75 2e 62 7a 20 2a 2f 0d 0a 20 20 69 6e  to u.bz */..  in
27567 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a  t nRoot;      /*
27568 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65   Number of table
27569 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75  s to check.  (Nu
2756a 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67  mber of root pag
2756b 65 73 2e 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  es.) */..  int *
2756c 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72  aRoot;     /* Ar
2756d 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20  ray of rootpage 
2756e 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c  numbers for tabl
2756f 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64  es to be checked
27570 20 2a 2f 0d 0a 20 20 69 6e 74 20 6a 3b 20 20 20   */..  int j;   
27571 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
27572 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74  ounter */..  int
27573 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20   nErr;       /* 
27574 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
27575 20 72 65 70 6f 72 74 65 64 20 2a 2f 0d 0a 20 20   reported */..  
27576 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
27577 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65  /* Text of the e
27578 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0d 0a  rror report */..
27579 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20    Mem *pnErr;   
2757a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65    /* Register ke
2757b 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65  eping track of e
2757c 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20  rrors remaining 
2757d 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  */..#endif /* lo
2757e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
2757f 76 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f  ved into u.bz */
27580 0d 0a 0d 0a 20 20 75 2e 62 7a 2e 6e 52 6f 6f 74  ....  u.bz.nRoot
27581 20 3d 20 70 4f 70 2d 3e 70 32 3b 0d 0a 20 20 61   = pOp->p2;..  a
27582 73 73 65 72 74 28 20 75 2e 62 7a 2e 6e 52 6f 6f  ssert( u.bz.nRoo
27583 74 3e 30 20 29 3b 0d 0a 20 20 75 2e 62 7a 2e 61  t>0 );..  u.bz.a
27584 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Root = sqlite3Db
27585 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
27586 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e 62 7a 2e  zeof(int)*(u.bz.
27587 6e 52 6f 6f 74 2b 31 29 20 29 3b 0d 0a 20 20 69  nRoot+1) );..  i
27588 66 28 20 75 2e 62 7a 2e 61 52 6f 6f 74 3d 3d 30  f( u.bz.aRoot==0
27589 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d   ) goto no_mem;.
2758a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
2758b 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
2758c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a 20 20 75  =p->nMem );..  u
2758d 2e 62 7a 2e 70 6e 45 72 72 20 3d 20 26 61 4d 65  .bz.pnErr = &aMe
2758e 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 61  m[pOp->p3];..  a
2758f 73 73 65 72 74 28 20 28 75 2e 62 7a 2e 70 6e 45  ssert( (u.bz.pnE
27590 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  rr->flags & MEM_
27591 49 6e 74 29 21 3d 30 20 29 3b 0d 0a 20 20 61 73  Int)!=0 );..  as
27592 73 65 72 74 28 20 28 75 2e 62 7a 2e 70 6e 45 72  sert( (u.bz.pnEr
27593 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  r->flags & (MEM_
27594 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d  Str|MEM_Blob))==
27595 30 20 29 3b 0d 0a 20 20 70 49 6e 31 20 3d 20 26  0 );..  pIn1 = &
27596 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a  aMem[pOp->p1];..
27597 20 20 66 6f 72 28 75 2e 62 7a 2e 6a 3d 30 3b 20    for(u.bz.j=0; 
27598 75 2e 62 7a 2e 6a 3c 75 2e 62 7a 2e 6e 52 6f 6f  u.bz.j<u.bz.nRoo
27599 74 3b 20 75 2e 62 7a 2e 6a 2b 2b 29 7b 0d 0a 20  t; u.bz.j++){.. 
2759a 20 20 20 75 2e 62 7a 2e 61 52 6f 6f 74 5b 75 2e     u.bz.aRoot[u.
2759b 62 7a 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c  bz.j] = (int)sql
2759c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
2759d 28 26 70 49 6e 31 5b 75 2e 62 7a 2e 6a 5d 29 3b  (&pIn1[u.bz.j]);
2759e 0d 0a 20 20 7d 0d 0a 20 20 75 2e 62 7a 2e 61 52  ..  }..  u.bz.aR
2759f 6f 6f 74 5b 75 2e 62 7a 2e 6a 5d 20 3d 20 30 3b  oot[u.bz.j] = 0;
275a0 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
275a1 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a  >p5<db->nDb );..
275a2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
275a3 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62  reeMask & (((yDb
275a4 4d 61 73 6b 29 31 29 3c 3c 70 4f 70 2d 3e 70 35  Mask)1)<<pOp->p5
275a5 29 29 21 3d 30 20 29 3b 0d 0a 20 20 75 2e 62 7a  ))!=0 );..  u.bz
275a6 2e 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  .z = sqlite3Btre
275a7 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28  eIntegrityCheck(
275a8 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d  db->aDb[pOp->p5]
275a9 2e 70 42 74 2c 20 75 2e 62 7a 2e 61 52 6f 6f 74  .pBt, u.bz.aRoot
275aa 2c 20 75 2e 62 7a 2e 6e 52 6f 6f 74 2c 0d 0a 20  , u.bz.nRoot,.. 
275ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275ad 28 69 6e 74 29 75 2e 62 7a 2e 70 6e 45 72 72 2d  (int)u.bz.pnErr-
275ae 3e 75 2e 69 2c 20 26 75 2e 62 7a 2e 6e 45 72 72  >u.i, &u.bz.nErr
275af 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46  );..  sqlite3DbF
275b0 72 65 65 28 64 62 2c 20 75 2e 62 7a 2e 61 52 6f  ree(db, u.bz.aRo
275b1 6f 74 29 3b 0d 0a 20 20 75 2e 62 7a 2e 70 6e 45  ot);..  u.bz.pnE
275b2 72 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 62 7a 2e  rr->u.i -= u.bz.
275b3 6e 45 72 72 3b 0d 0a 20 20 73 71 6c 69 74 65 33  nErr;..  sqlite3
275b4 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
275b5 49 6e 31 29 3b 0d 0a 20 20 69 66 28 20 75 2e 62  In1);..  if( u.b
275b6 7a 2e 6e 45 72 72 3d 3d 30 20 29 7b 0d 0a 20 20  z.nErr==0 ){..  
275b7 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a 2e 7a    assert( u.bz.z
275b8 3d 3d 30 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 20  ==0 );..  }else 
275b9 69 66 28 20 75 2e 62 7a 2e 7a 3d 3d 30 20 29 7b  if( u.bz.z==0 ){
275ba 0d 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ..    goto no_me
275bb 6d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  m;..  }else{..  
275bc 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
275bd 53 65 74 53 74 72 28 70 49 6e 31 2c 20 75 2e 62  SetStr(pIn1, u.b
275be 7a 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  z.z, -1, SQLITE_
275bf 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72  UTF8, sqlite3_fr
275c0 65 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 55 50 44  ee);..  }..  UPD
275c1 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
275c2 28 70 49 6e 31 29 3b 0d 0a 20 20 73 71 6c 69 74  (pIn1);..  sqlit
275c3 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
275c4 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64  ding(pIn1, encod
275c5 69 6e 67 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d  ing);..  break;.
275c6 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
275c7 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
275c8 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0d 0a 0d 0a  ITY_CHECK */....
275c9 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
275ca 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 2a  tAdd P1 P2 * * *
275cb 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 73 65 72 74 20  ..**..** Insert 
275cc 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
275cd 65 20 68 65 6c 64 20 62 79 20 72 65 67 69 73 74  e held by regist
275ce 65 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f  er P2 into a boo
275cf 6c 65 61 6e 20 69 6e 64 65 78 0d 0a 2a 2a 20 68  lean index..** h
275d0 65 6c 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  eld in register 
275d1 50 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 61  P1...**..** An a
275d2 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69  ssertion fails i
275d3 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 69  f P2 is not an i
275d4 6e 74 65 67 65 72 2e 0d 0a 2a 2f 0d 0a 63 61 73  nteger...*/..cas
275d5 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20  e OP_RowSetAdd: 
275d6 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20  {       /* in1, 
275d7 69 6e 32 20 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d  in2 */..  pIn1 =
275d8 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
275d9 0d 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d  ..  pIn2 = &aMem
275da 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 61 73  [pOp->p2];..  as
275db 73 65 72 74 28 20 28 70 49 6e 32 2d 3e 66 6c 61  sert( (pIn2->fla
275dc 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30  gs & MEM_Int)!=0
275dd 20 29 3b 0d 0a 20 20 69 66 28 20 28 70 49 6e 31   );..  if( (pIn1
275de 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
275df 77 53 65 74 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  wSet)==0 ){..   
275e0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
275e1 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0d  etRowSet(pIn1);.
275e2 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  .    if( (pIn1->
275e3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
275e4 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f  et)==0 ) goto no
275e5 5f 6d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71  _mem;..  }..  sq
275e6 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72  lite3RowSetInser
275e7 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65  t(pIn1->u.pRowSe
275e8 74 2c 20 70 49 6e 32 2d 3e 75 2e 69 29 3b 0d 0a  t, pIn2->u.i);..
275e9 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f    break;..}..../
275ea 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74  * Opcode: RowSet
275eb 52 65 61 64 20 50 31 20 50 32 20 50 33 20 2a 20  Read P1 P2 P3 * 
275ec 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 74 72 61 63  *..**..** Extrac
275ed 74 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76  t the smallest v
275ee 61 6c 75 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61  alue from boolea
275ef 6e 20 69 6e 64 65 78 20 50 31 20 61 6e 64 20 70  n index P1 and p
275f0 75 74 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  ut that value in
275f1 74 6f 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  to..** register 
275f2 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 6f 6f 6c  P3.  Or, if bool
275f3 65 61 6e 20 69 6e 64 65 78 20 50 31 20 69 73 20  ean index P1 is 
275f4 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c  initially empty,
275f5 20 6c 65 61 76 65 20 50 33 0d 0a 2a 2a 20 75 6e   leave P3..** un
275f6 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 6d 70  changed and jump
275f7 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   to instruction 
275f8 50 32 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  P2...*/..case OP
275f9 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20  _RowSetRead: {  
275fa 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
275fb 31 2c 20 6f 75 74 33 20 2a 2f 0d 0a 23 69 66 20  1, out3 */..#if 
275fc 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
275fd 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
275fe 20 75 2e 63 61 20 2a 2f 0d 0a 20 20 69 36 34 20   u.ca */..  i64 
275ff 76 61 6c 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  val;..#endif /* 
27600 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
27601 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20  moved into u.ca 
27602 2a 2f 0d 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f  */..  CHECK_FOR_
27603 49 4e 54 45 52 52 55 50 54 3b 0d 0a 20 20 70 49  INTERRUPT;..  pI
27604 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
27605 70 31 5d 3b 0d 0a 20 20 69 66 28 20 28 70 49 6e  p1];..  if( (pIn
27606 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  1->flags & MEM_R
27607 6f 77 53 65 74 29 3d 3d 30 0d 0a 20 20 20 7c 7c  owSet)==0..   ||
27608 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65   sqlite3RowSetNe
27609 78 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53  xt(pIn1->u.pRowS
2760a 65 74 2c 20 26 75 2e 63 61 2e 76 61 6c 29 3d 3d  et, &u.ca.val)==
2760b 30 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  0..  ){..    /* 
2760c 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65  The boolean inde
2760d 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0d 0a 20  x is empty */.. 
2760e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
2760f 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0d  mSetNull(pIn1);.
27610 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
27611 32 20 2d 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b  2 - 1;..  }else{
27612 0d 0a 20 20 20 20 2f 2a 20 41 20 76 61 6c 75 65  ..    /* A value
27613 20 77 61 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d   was pulled from
27614 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0d 0a 20   the index */.. 
27615 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
27616 6d 53 65 74 49 6e 74 36 34 28 26 61 4d 65 6d 5b  mSetInt64(&aMem[
27617 70 4f 70 2d 3e 70 33 5d 2c 20 75 2e 63 61 2e 76  pOp->p3], u.ca.v
27618 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65  al);..  }..  bre
27619 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63  ak;..}..../* Opc
2761a 6f 64 65 3a 20 52 6f 77 53 65 74 54 65 73 74 20  ode: RowSetTest 
2761b 50 31 20 50 32 20 50 33 20 50 34 0d 0a 2a 2a 0d  P1 P2 P3 P4..**.
2761c 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20  .** Register P3 
2761d 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 6f  is assumed to ho
2761e 6c 64 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  ld a 64-bit inte
2761f 67 65 72 20 76 61 6c 75 65 2e 20 49 66 20 72 65  ger value. If re
27620 67 69 73 74 65 72 20 50 31 0d 0a 2a 2a 20 63 6f  gister P1..** co
27621 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 74 20  ntains a RowSet 
27622 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74 20  object and that 
27623 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 63 6f  RowSet object co
27624 6e 74 61 69 6e 73 0d 0a 2a 2a 20 74 68 65 20 76  ntains..** the v
27625 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 2c  alue held in P3,
27626 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 65   jump to registe
27627 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  r P2. Otherwise,
27628 20 69 6e 73 65 72 74 20 74 68 65 0d 0a 2a 2a 20   insert the..** 
27629 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e  integer in P3 in
2762a 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e  to the RowSet an
2762b 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f  d continue on to
2762c 20 74 68 65 0d 0a 2a 2a 20 6e 65 78 74 20 6f 70   the..** next op
2762d 63 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  code...**..** Th
2762e 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20  e RowSet object 
2762f 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72  is optimized for
27630 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20   the case where 
27631 73 75 63 63 65 73 73 69 76 65 20 73 65 74 73 0d  successive sets.
27632 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 2c  .** of integers,
27633 20 77 68 65 72 65 20 65 61 63 68 20 73 65 74 20   where each set 
27634 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c  contains no dupl
27635 69 63 61 74 65 73 2e 20 45 61 63 68 20 73 65 74  icates. Each set
27636 0d 0a 2a 2a 20 6f 66 20 76 61 6c 75 65 73 20 69  ..** of values i
27637 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
27638 61 20 75 6e 69 71 75 65 20 50 34 20 76 61 6c 75  a unique P4 valu
27639 65 2e 20 54 68 65 20 66 69 72 73 74 20 73 65 74  e. The first set
2763a 0d 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 50  ..** must have P
2763b 34 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61 6c 20  4==0, the final 
2763c 73 65 74 20 50 34 3d 2d 31 2e 20 20 50 34 20 6d  set P4=-1.  P4 m
2763d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 2d 31  ust be either -1
2763e 20 6f 72 0d 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67 61   or..** non-nega
2763f 74 69 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d 6e  tive.  For non-n
27640 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 20 6f  egative values o
27641 66 20 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c 6f  f P4 only the lo
27642 77 65 72 20 34 0d 0a 2a 2a 20 62 69 74 73 20 61  wer 4..** bits a
27643 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 2e 0d  re significant..
27644 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 61 6c 6c  .**..** This all
27645 6f 77 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ows optimization
27646 73 3a 20 28 61 29 20 77 68 65 6e 20 50 34 3d 3d  s: (a) when P4==
27647 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  0 there is no ne
27648 65 64 20 74 6f 20 74 65 73 74 0d 0a 2a 2a 20 74  ed to test..** t
27649 68 65 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74  he rowset object
2764a 20 66 6f 72 20 50 33 2c 20 61 73 20 69 74 20 69   for P3, as it i
2764b 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74  s guaranteed not
2764c 20 74 6f 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0d   to contain it,.
2764d 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20 50 34 3d  .** (b) when P4=
2764e 3d 2d 31 20 74 68 65 72 65 20 69 73 20 6e 6f 20  =-1 there is no 
2764f 6e 65 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74  need to insert t
27650 68 65 20 76 61 6c 75 65 2c 20 61 73 20 69 74 20  he value, as it 
27651 77 69 6c 6c 0d 0a 2a 2a 20 6e 65 76 65 72 20 62  will..** never b
27652 65 20 74 65 73 74 65 64 20 66 6f 72 2c 20 61 6e  e tested for, an
27653 64 20 28 63 29 20 77 68 65 6e 20 61 20 76 61 6c  d (c) when a val
27654 75 65 20 74 68 61 74 20 69 73 20 70 61 72 74 20  ue that is part 
27655 6f 66 20 73 65 74 20 58 20 69 73 0d 0a 2a 2a 20  of set X is..** 
27656 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 65 20  inserted, there 
27657 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73 65  is no need to se
27658 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 20 74  arch to see if t
27659 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 61  he same value wa
2765a 73 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  s..** previously
2765b 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 72   inserted as par
2765c 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79  t of set X (only
2765d 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 69   if it was previ
2765e 6f 75 73 6c 79 0d 0a 2a 2a 20 69 6e 73 65 72 74  ously..** insert
2765f 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f  ed as part of so
27660 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0d 0a  me other set)...
27661 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53  */..case OP_RowS
27662 65 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20  etTest: {       
27663 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27664 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
27665 2a 2f 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  */..#if 0  /* lo
27666 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
27667 76 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f  ved into u.cb */
27668 0d 0a 20 20 69 6e 74 20 69 53 65 74 3b 0d 0a 20  ..  int iSet;.. 
27669 20 69 6e 74 20 65 78 69 73 74 73 3b 0d 0a 23 65   int exists;..#e
2766a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
2766b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2766c 74 6f 20 75 2e 63 62 20 2a 2f 0d 0a 0d 0a 20 20  to u.cb */....  
2766d 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
2766e 2d 3e 70 31 5d 3b 0d 0a 20 20 70 49 6e 33 20 3d  ->p1];..  pIn3 =
2766f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
27670 0d 0a 20 20 75 2e 63 62 2e 69 53 65 74 20 3d 20  ..  u.cb.iSet = 
27671 70 4f 70 2d 3e 70 34 2e 69 3b 0d 0a 20 20 61 73  pOp->p4.i;..  as
27672 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67  sert( pIn3->flag
27673 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0d 0a 0d 0a  s&MEM_Int );....
27674 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
27675 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20   anything other 
27676 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 6f 62  than a rowset ob
27677 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  ject in memory c
27678 65 6c 6c 20 50 31 2c 0d 0a 20 20 2a 2a 20 64 65  ell P1,..  ** de
27679 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 20  lete it now and 
2767a 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 69  initialize P1 wi
2767b 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73  th an empty rows
2767c 65 74 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  et..  */..  if( 
2767d 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
2767e 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b  EM_RowSet)==0 ){
2767f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
27680 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49  eMemSetRowSet(pI
27681 6e 31 29 3b 0d 0a 20 20 20 20 69 66 28 20 28 70  n1);..    if( (p
27682 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
27683 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f  _RowSet)==0 ) go
27684 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 7d 0d  to no_mem;..  }.
27685 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ...  assert( pOp
27686 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
27687 33 32 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  32 );..  assert(
27688 20 75 2e 63 62 2e 69 53 65 74 3d 3d 2d 31 20 7c   u.cb.iSet==-1 |
27689 7c 20 75 2e 63 62 2e 69 53 65 74 3e 3d 30 20 29  | u.cb.iSet>=0 )
2768a 3b 0d 0a 20 20 69 66 28 20 75 2e 63 62 2e 69 53  ;..  if( u.cb.iS
2768b 65 74 20 29 7b 0d 0a 20 20 20 20 75 2e 63 62 2e  et ){..    u.cb.
2768c 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 65 33  exists = sqlite3
2768d 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e 31 2d  RowSetTest(pIn1-
2768e 3e 75 2e 70 52 6f 77 53 65 74 2c 0d 0a 20 20 20  >u.pRowSet,..   
2768f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27690 20 20 20 20 20 20 20 20 20 20 20 20 28 75 38 29              (u8)
27691 28 75 2e 63 62 2e 69 53 65 74 3e 3d 30 20 3f 20  (u.cb.iSet>=0 ? 
27692 75 2e 63 62 2e 69 53 65 74 20 26 20 30 78 66 20  u.cb.iSet & 0xf 
27693 3a 20 30 78 66 66 29 2c 0d 0a 20 20 20 20 20 20  : 0xff),..      
27694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27695 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75           pIn3->u
27696 2e 69 29 3b 0d 0a 20 20 20 20 69 66 28 20 75 2e  .i);..    if( u.
27697 63 62 2e 65 78 69 73 74 73 20 29 7b 0d 0a 20 20  cb.exists ){..  
27698 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
27699 20 2d 20 31 3b 0d 0a 20 20 20 20 20 20 62 72 65   - 1;..      bre
2769a 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ak;..    }..  }.
2769b 0a 20 20 69 66 28 20 75 2e 63 62 2e 69 53 65 74  .  if( u.cb.iSet
2769c 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  >=0 ){..    sqli
2769d 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28  te3RowSetInsert(
2769e 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c  pIn1->u.pRowSet,
2769f 20 70 49 6e 33 2d 3e 75 2e 69 29 3b 0d 0a 20 20   pIn3->u.i);..  
276a0 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  }..  break;..}..
276a1 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
276a2 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d  TE_OMIT_TRIGGER.
276a3 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72  .../* Opcode: Pr
276a4 6f 67 72 61 6d 20 50 31 20 50 32 20 50 33 20 50  ogram P1 P2 P3 P
276a5 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 65 63  4 *..**..** Exec
276a6 75 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ute the trigger 
276a7 70 72 6f 67 72 61 6d 20 70 61 73 73 65 64 20 61  program passed a
276a8 73 20 50 34 20 28 74 79 70 65 20 50 34 5f 53 55  s P4 (type P4_SU
276a9 42 50 52 4f 47 52 41 4d 29 2e 20 0d 0a 2a 2a 0d  BPROGRAM). ..**.
276aa 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e 73 20  .** P1 contains 
276ab 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
276ac 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  he memory cell t
276ad 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
276ae 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20 0d 0a   first memory ..
276af 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61 72  ** cell in an ar
276b0 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75 73  ray of values us
276b1 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 20  ed as arguments 
276b2 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  to the sub-progr
276b3 61 6d 2e 20 50 32 20 0d 0a 2a 2a 20 63 6f 6e 74  am. P2 ..** cont
276b4 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73  ains the address
276b5 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74   to jump to if t
276b6 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74  he sub-program t
276b7 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20  hrows an IGNORE 
276b8 0d 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75  ..** exception u
276b9 73 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29  sing the RAISE()
276ba 20 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73   function. Regis
276bb 74 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20  ter P3 contains 
276bc 74 68 65 20 61 64 64 72 65 73 73 20 0d 0a 2a 2a  the address ..**
276bd 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   of a memory cel
276be 6c 20 69 6e 20 74 68 69 73 20 28 74 68 65 20 70  l in this (the p
276bf 61 72 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69  arent) VM that i
276c0 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61  s used to alloca
276c1 74 65 20 74 68 65 20 0d 0a 2a 2a 20 6d 65 6d 6f  te the ..** memo
276c2 72 79 20 72 65 71 75 69 72 65 64 20 62 79 20 74  ry required by t
276c3 68 65 20 73 75 62 2d 76 64 62 65 20 61 74 20 72  he sub-vdbe at r
276c4 75 6e 74 69 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  untime...**..** 
276c5 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
276c6 74 6f 20 74 68 65 20 56 4d 20 63 6f 6e 74 61 69  to the VM contai
276c7 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ning the trigger
276c8 20 70 72 6f 67 72 61 6d 2e 0d 0a 2a 2f 0d 0a 63   program...*/..c
276c9 61 73 65 20 4f 50 5f 50 72 6f 67 72 61 6d 3a 20  ase OP_Program: 
276ca 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
276cb 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c   */..#if 0  /* l
276cc 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
276cd 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a  oved into u.cc *
276ce 2f 0d 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20  /..  int nMem;  
276cf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
276d0 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  Number of memory
276d1 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73   registers for s
276d2 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20  ub-program */.. 
276d3 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20   int nByte;     
276d4 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
276d5 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61  s of runtime spa
276d6 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  ce required for 
276d7 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a  sub-program */..
276d8 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20    Mem *pRt;     
276d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
276da 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74  ister to allocat
276db 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  e runtime space 
276dc 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  */..  Mem *pMem;
276dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
276de 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   Used to iterate
276df 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20   through memory 
276e0 63 65 6c 6c 73 20 2a 2f 0d 0a 20 20 4d 65 6d 20  cells */..  Mem 
276e1 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20  *pEnd;          
276e2 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f      /* Last memo
276e3 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61  ry cell in new a
276e4 72 72 61 79 20 2a 2f 0d 0a 20 20 56 64 62 65 46  rray */..  VdbeF
276e5 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20  rame *pFrame;   
276e6 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66     /* New vdbe f
276e7 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20  rame to execute 
276e8 69 6e 20 2a 2f 0d 0a 20 20 53 75 62 50 72 6f 67  in */..  SubProg
276e9 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20  ram *pProgram;  
276ea 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d 20   /* Sub-program 
276eb 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0d 0a 20  to execute */.. 
276ec 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20   void *t;       
276ed 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
276ee 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72  n identifying tr
276ef 69 67 67 65 72 20 2a 2f 0d 0a 23 65 6e 64 69 66  igger */..#endif
276f0 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
276f1 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
276f2 2e 63 63 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 63 63  .cc */....  u.cc
276f3 2e 70 50 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d  .pProgram = pOp-
276f4 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0d 0a 20  >p4.pProgram;.. 
276f5 20 75 2e 63 63 2e 70 52 74 20 3d 20 26 61 4d 65   u.cc.pRt = &aMe
276f6 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 61  m[pOp->p3];..  a
276f7 73 73 65 72 74 28 20 75 2e 63 63 2e 70 50 72 6f  ssert( u.cc.pPro
276f8 67 72 61 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0d 0a  gram->nOp>0 );..
276f9 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 35  ..  /* If the p5
276fa 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 2c 20   flag is clear, 
276fb 74 68 65 6e 20 72 65 63 75 72 73 69 76 65 20 69  then recursive i
276fc 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69  nvocation of tri
276fd 67 67 65 72 73 20 69 73 0d 0a 20 20 2a 2a 20 64  ggers is..  ** d
276fe 69 73 61 62 6c 65 64 20 66 6f 72 20 62 61 63 6b  isabled for back
276ff 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
27700 69 74 79 20 28 70 35 20 69 73 20 73 65 74 20 69  ity (p5 is set i
27701 66 20 74 68 69 73 20 73 75 62 2d 70 72 6f 67 72  f this sub-progr
27702 61 6d 0d 0a 20 20 2a 2a 20 69 73 20 72 65 61 6c  am..  ** is real
27703 6c 79 20 61 20 74 72 69 67 67 65 72 2c 20 6e 6f  ly a trigger, no
27704 74 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  t a foreign key 
27705 61 63 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20  action, and the 
27706 66 6c 61 67 20 73 65 74 0d 0a 20 20 2a 2a 20 61  flag set..  ** a
27707 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 74 68  nd cleared by th
27708 65 20 22 50 52 41 47 4d 41 20 72 65 63 75 72 73  e "PRAGMA recurs
27709 69 76 65 5f 74 72 69 67 67 65 72 73 22 20 63 6f  ive_triggers" co
2770a 6d 6d 61 6e 64 20 69 73 20 63 6c 65 61 72 29 2e  mmand is clear).
2770b 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 74 20  ..  **..  ** It 
2770c 69 73 20 72 65 63 75 72 73 69 76 65 20 69 6e 76  is recursive inv
2770d 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 67  ocation of trigg
2770e 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 4c 20  ers, at the SQL 
2770f 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 0d 0a  level, that is..
27710 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 49    ** disabled. I
27711 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 73  n some cases a s
27712 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d 61  ingle trigger ma
27713 79 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 20  y generate more 
27714 74 68 61 6e 20 6f 6e 65 0d 0a 20 20 2a 2a 20 53  than one..  ** S
27715 75 62 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68  ubProgram (if th
27716 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 62 65  e trigger may be
27717 20 65 78 65 63 75 74 65 64 20 77 69 74 68 20 6d   executed with m
27718 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66  ore than one dif
27719 66 65 72 65 6e 74 0d 0a 20 20 2a 2a 20 4f 4e 20  ferent..  ** ON 
2771a 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74  CONFLICT algorit
2771b 68 6d 29 2e 20 53 75 62 50 72 6f 67 72 61 6d 20  hm). SubProgram 
2771c 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63  structures assoc
2771d 69 61 74 65 64 20 77 69 74 68 20 61 0d 0a 20 20  iated with a..  
2771e 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65  ** single trigge
2771f 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65 20 73  r all have the s
27720 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ame value for th
27721 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b  e SubProgram.tok
27722 65 6e 0d 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  en..  ** variabl
27723 65 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4f  e.  */..  if( pO
27724 70 2d 3e 70 35 20 29 7b 0d 0a 20 20 20 20 75 2e  p->p5 ){..    u.
27725 63 63 2e 74 20 3d 20 75 2e 63 63 2e 70 50 72 6f  cc.t = u.cc.pPro
27726 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0d 0a 20 20  gram->token;..  
27727 20 20 66 6f 72 28 75 2e 63 63 2e 70 46 72 61 6d    for(u.cc.pFram
27728 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 63  e=p->pFrame; u.c
27729 63 2e 70 46 72 61 6d 65 20 26 26 20 75 2e 63 63  c.pFrame && u.cc
2772a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d  .pFrame->token!=
2772b 75 2e 63 63 2e 74 3b 20 75 2e 63 63 2e 70 46 72  u.cc.t; u.cc.pFr
2772c 61 6d 65 3d 75 2e 63 63 2e 70 46 72 61 6d 65 2d  ame=u.cc.pFrame-
2772d 3e 70 50 61 72 65 6e 74 29 3b 0d 0a 20 20 20 20  >pParent);..    
2772e 69 66 28 20 75 2e 63 63 2e 70 46 72 61 6d 65 20  if( u.cc.pFrame 
2772f 29 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d  ) break;..  }...
27730 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 61 6d 65  .  if( p->nFrame
27731 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  >=db->aLimit[SQL
27732 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45  ITE_LIMIT_TRIGGE
27733 52 5f 44 45 50 54 48 5d 20 29 7b 0d 0a 20 20 20  R_DEPTH] ){..   
27734 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
27735 4f 52 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  OR;..    sqlite3
27736 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
27737 72 72 4d 73 67 2c 20 64 62 2c 20 22 74 6f 6f 20  rrMsg, db, "too 
27738 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 74  many levels of t
27739 72 69 67 67 65 72 20 72 65 63 75 72 73 69 6f 6e  rigger recursion
2773a 22 29 3b 0d 0a 20 20 20 20 62 72 65 61 6b 3b 0d  ");..    break;.
2773b 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 67  .  }....  /* Reg
2773c 69 73 74 65 72 20 75 2e 63 63 2e 70 52 74 20 69  ister u.cc.pRt i
2773d 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
2773e 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69  the memory requi
2773f 72 65 64 20 74 6f 20 73 61 76 65 20 74 68 65 20  red to save the 
27740 73 74 61 74 65 0d 0a 20 20 2a 2a 20 6f 66 20 74  state..  ** of t
27741 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 67 72  he current progr
27742 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f  am, and the memo
27743 72 79 20 72 65 71 75 69 72 65 64 20 61 74 20 72  ry required at r
27744 75 6e 74 69 6d 65 20 74 6f 20 65 78 65 63 75 74  untime to execut
27745 65 0d 0a 20 20 2a 2a 20 74 68 65 20 74 72 69 67  e..  ** the trig
27746 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20  ger program. If 
27747 74 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73  this trigger has
27748 20 62 65 65 6e 20 66 69 72 65 64 20 62 65 66 6f   been fired befo
27749 72 65 2c 20 74 68 65 6e 20 75 2e 63 63 2e 70 52  re, then u.cc.pR
2774a 74 0d 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61  t..  ** is alrea
2774b 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f 74  dy allocated. Ot
2774c 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73 74  herwise, it must
2774d 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
2774e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 75 2e 63    */..  if( (u.c
2774f 63 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  c.pRt->flags&MEM
27750 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a 20  _Frame)==0 ){.. 
27751 20 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d     /* SubProgram
27752 2e 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20  .nMem is set to 
27753 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65  the number of me
27754 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20  mory cells used 
27755 62 79 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 70  by the..    ** p
27756 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e  rogram stored in
27757 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e   SubProgram.aOp.
27758 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73   As well as thes
27759 65 2c 20 6f 6e 65 20 6d 65 6d 6f 72 79 0d 0a 20  e, one memory.. 
2775a 20 20 20 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65     ** cell is re
2775b 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20  quired for each 
2775c 63 75 72 73 6f 72 20 75 73 65 64 20 62 79 20 74  cursor used by t
2775d 68 65 20 70 72 6f 67 72 61 6d 2e 20 53 65 74 20  he program. Set 
2775e 6c 6f 63 61 6c 0d 0a 20 20 20 20 2a 2a 20 76 61  local..    ** va
2775f 72 69 61 62 6c 65 20 75 2e 63 63 2e 6e 4d 65 6d  riable u.cc.nMem
27760 20 28 61 6e 64 20 6c 61 74 65 72 2c 20 56 64 62   (and later, Vdb
27761 65 46 72 61 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d  eFrame.nChildMem
27762 29 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 2e  ) to this value.
27763 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 75 2e  ..    */..    u.
27764 63 63 2e 6e 4d 65 6d 20 3d 20 75 2e 63 63 2e 70  cc.nMem = u.cc.p
27765 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 2b 20  Program->nMem + 
27766 75 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.cc.pProgram->n
27767 43 73 72 3b 0d 0a 20 20 20 20 75 2e 63 63 2e 6e  Csr;..    u.cc.n
27768 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69  Byte = ROUND8(si
27769 7a 65 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29  zeof(VdbeFrame))
2776a 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2776b 2b 20 75 2e 63 63 2e 6e 4d 65 6d 20 2a 20 73 69  + u.cc.nMem * si
2776c 7a 65 6f 66 28 4d 65 6d 29 0d 0a 20 20 20 20 20  zeof(Mem)..     
2776d 20 20 20 20 20 20 20 20 20 2b 20 75 2e 63 63 2e           + u.cc.
2776e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 2a  pProgram->nCsr *
2776f 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73   sizeof(VdbeCurs
27770 6f 72 20 2a 29 0d 0a 20 20 20 20 20 20 20 20 20  or *)..         
27771 20 20 20 20 20 2b 20 75 2e 63 63 2e 70 50 72 6f       + u.cc.pPro
27772 67 72 61 6d 2d 3e 6e 4f 6e 63 65 20 2a 20 73 69  gram->nOnce * si
27773 7a 65 6f 66 28 75 38 29 3b 0d 0a 20 20 20 20 75  zeof(u8);..    u
27774 2e 63 63 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c  .cc.pFrame = sql
27775 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
27776 28 64 62 2c 20 75 2e 63 63 2e 6e 42 79 74 65 29  (db, u.cc.nByte)
27777 3b 0d 0a 20 20 20 20 69 66 28 20 21 75 2e 63 63  ;..    if( !u.cc
27778 2e 70 46 72 61 6d 65 20 29 7b 0d 0a 20 20 20 20  .pFrame ){..    
27779 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a    goto no_mem;..
2777a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
2777b 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
2777c 28 75 2e 63 63 2e 70 52 74 29 3b 0d 0a 20 20 20  (u.cc.pRt);..   
2777d 20 75 2e 63 63 2e 70 52 74 2d 3e 66 6c 61 67 73   u.cc.pRt->flags
2777e 20 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b 0d 0a 20   = MEM_Frame;.. 
2777f 20 20 20 75 2e 63 63 2e 70 52 74 2d 3e 75 2e 70     u.cc.pRt->u.p
27780 46 72 61 6d 65 20 3d 20 75 2e 63 63 2e 70 46 72  Frame = u.cc.pFr
27781 61 6d 65 3b 0d 0a 0d 0a 20 20 20 20 75 2e 63 63  ame;....    u.cc
27782 2e 70 46 72 61 6d 65 2d 3e 76 20 3d 20 70 3b 0d  .pFrame->v = p;.
27783 0a 20 20 20 20 75 2e 63 63 2e 70 46 72 61 6d 65  .    u.cc.pFrame
27784 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 3d 20 75 2e  ->nChildMem = u.
27785 63 63 2e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 75 2e  cc.nMem;..    u.
27786 63 63 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  cc.pFrame->nChil
27787 64 43 73 72 20 3d 20 75 2e 63 63 2e 70 50 72 6f  dCsr = u.cc.pPro
27788 67 72 61 6d 2d 3e 6e 43 73 72 3b 0d 0a 20 20 20  gram->nCsr;..   
27789 20 75 2e 63 63 2e 70 46 72 61 6d 65 2d 3e 70 63   u.cc.pFrame->pc
2778a 20 3d 20 70 63 3b 0d 0a 20 20 20 20 75 2e 63 63   = pc;..    u.cc
2778b 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 3d 20  .pFrame->aMem = 
2778c 70 2d 3e 61 4d 65 6d 3b 0d 0a 20 20 20 20 75 2e  p->aMem;..    u.
2778d 63 63 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20  cc.pFrame->nMem 
2778e 3d 20 70 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20  = p->nMem;..    
2778f 75 2e 63 63 2e 70 46 72 61 6d 65 2d 3e 61 70 43  u.cc.pFrame->apC
27790 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0d 0a  sr = p->apCsr;..
27791 20 20 20 20 75 2e 63 63 2e 70 46 72 61 6d 65 2d      u.cc.pFrame-
27792 3e 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43  >nCursor = p->nC
27793 75 72 73 6f 72 3b 0d 0a 20 20 20 20 75 2e 63 63  ursor;..    u.cc
27794 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70  .pFrame->aOp = p
27795 2d 3e 61 4f 70 3b 0d 0a 20 20 20 20 75 2e 63 63  ->aOp;..    u.cc
27796 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70  .pFrame->nOp = p
27797 2d 3e 6e 4f 70 3b 0d 0a 20 20 20 20 75 2e 63 63  ->nOp;..    u.cc
27798 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d  .pFrame->token =
27799 20 75 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.cc.pProgram->
2779a 74 6f 6b 65 6e 3b 0d 0a 20 20 20 20 75 2e 63 63  token;..    u.cc
2779b 2e 70 46 72 61 6d 65 2d 3e 61 4f 6e 63 65 46 6c  .pFrame->aOnceFl
2779c 61 67 20 3d 20 70 2d 3e 61 4f 6e 63 65 46 6c 61  ag = p->aOnceFla
2779d 67 3b 0d 0a 20 20 20 20 75 2e 63 63 2e 70 46 72  g;..    u.cc.pFr
2779e 61 6d 65 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20 3d  ame->nOnceFlag =
2779f 20 70 2d 3e 6e 4f 6e 63 65 46 6c 61 67 3b 0d 0a   p->nOnceFlag;..
277a0 0d 0a 20 20 20 20 75 2e 63 63 2e 70 45 6e 64 20  ..    u.cc.pEnd 
277a1 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28  = &VdbeFrameMem(
277a2 75 2e 63 63 2e 70 46 72 61 6d 65 29 5b 75 2e 63  u.cc.pFrame)[u.c
277a3 63 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64  c.pFrame->nChild
277a4 4d 65 6d 5d 3b 0d 0a 20 20 20 20 66 6f 72 28 75  Mem];..    for(u
277a5 2e 63 63 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61  .cc.pMem=VdbeFra
277a6 6d 65 4d 65 6d 28 75 2e 63 63 2e 70 46 72 61 6d  meMem(u.cc.pFram
277a7 65 29 3b 20 75 2e 63 63 2e 70 4d 65 6d 21 3d 75  e); u.cc.pMem!=u
277a8 2e 63 63 2e 70 45 6e 64 3b 20 75 2e 63 63 2e 70  .cc.pEnd; u.cc.p
277a9 4d 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 75  Mem++){..      u
277aa 2e 63 63 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  .cc.pMem->flags 
277ab 3d 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 3b 0d 0a  = MEM_Invalid;..
277ac 20 20 20 20 20 20 75 2e 63 63 2e 70 4d 65 6d 2d        u.cc.pMem-
277ad 3e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20 7d  >db = db;..    }
277ae 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
277af 75 2e 63 63 2e 70 46 72 61 6d 65 20 3d 20 75 2e  u.cc.pFrame = u.
277b0 63 63 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65  cc.pRt->u.pFrame
277b1 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ;..    assert( u
277b2 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d  .cc.pProgram->nM
277b3 65 6d 2b 75 2e 63 63 2e 70 50 72 6f 67 72 61 6d  em+u.cc.pProgram
277b4 2d 3e 6e 43 73 72 3d 3d 75 2e 63 63 2e 70 46 72  ->nCsr==u.cc.pFr
277b5 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 29  ame->nChildMem )
277b6 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ;..    assert( u
277b7 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43  .cc.pProgram->nC
277b8 73 72 3d 3d 75 2e 63 63 2e 70 46 72 61 6d 65 2d  sr==u.cc.pFrame-
277b9 3e 6e 43 68 69 6c 64 43 73 72 20 29 3b 0d 0a 20  >nChildCsr );.. 
277ba 20 20 20 61 73 73 65 72 74 28 20 70 63 3d 3d 75     assert( pc==u
277bb 2e 63 63 2e 70 46 72 61 6d 65 2d 3e 70 63 20 29  .cc.pFrame->pc )
277bc 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 2d 3e 6e  ;..  }....  p->n
277bd 46 72 61 6d 65 2b 2b 3b 0d 0a 20 20 75 2e 63 63  Frame++;..  u.cc
277be 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74  .pFrame->pParent
277bf 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0d 0a 20   = p->pFrame;.. 
277c0 20 75 2e 63 63 2e 70 46 72 61 6d 65 2d 3e 6c 61   u.cc.pFrame->la
277c1 73 74 52 6f 77 69 64 20 3d 20 6c 61 73 74 52 6f  stRowid = lastRo
277c2 77 69 64 3b 0d 0a 20 20 75 2e 63 63 2e 70 46 72  wid;..  u.cc.pFr
277c3 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ame->nChange = p
277c4 2d 3e 6e 43 68 61 6e 67 65 3b 0d 0a 20 20 70 2d  ->nChange;..  p-
277c5 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0d 0a 20  >nChange = 0;.. 
277c6 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e 63   p->pFrame = u.c
277c7 63 2e 70 46 72 61 6d 65 3b 0d 0a 20 20 70 2d 3e  c.pFrame;..  p->
277c8 61 4d 65 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56  aMem = aMem = &V
277c9 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 63 63  dbeFrameMem(u.cc
277ca 2e 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0d 0a 20  .pFrame)[-1];.. 
277cb 20 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 63 63 2e   p->nMem = u.cc.
277cc 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65  pFrame->nChildMe
277cd 6d 3b 0d 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72  m;..  p->nCursor
277ce 20 3d 20 28 75 31 36 29 75 2e 63 63 2e 70 46 72   = (u16)u.cc.pFr
277cf 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0d  ame->nChildCsr;.
277d0 0a 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 56  .  p->apCsr = (V
277d1 64 62 65 43 75 72 73 6f 72 20 2a 2a 29 26 61 4d  dbeCursor **)&aM
277d2 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0d 0a  em[p->nMem+1];..
277d3 20 20 70 2d 3e 61 4f 70 20 3d 20 61 4f 70 20 3d    p->aOp = aOp =
277d4 20 75 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.cc.pProgram->
277d5 61 4f 70 3b 0d 0a 20 20 70 2d 3e 6e 4f 70 20 3d  aOp;..  p->nOp =
277d6 20 75 2e 63 63 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.cc.pProgram->
277d7 6e 4f 70 3b 0d 0a 20 20 70 2d 3e 61 4f 6e 63 65  nOp;..  p->aOnce
277d8 46 6c 61 67 20 3d 20 28 75 38 20 2a 29 26 70 2d  Flag = (u8 *)&p-
277d9 3e 61 70 43 73 72 5b 70 2d 3e 6e 43 75 72 73 6f  >apCsr[p->nCurso
277da 72 5d 3b 0d 0a 20 20 70 2d 3e 6e 4f 6e 63 65 46  r];..  p->nOnceF
277db 6c 61 67 20 3d 20 75 2e 63 63 2e 70 50 72 6f 67  lag = u.cc.pProg
277dc 72 61 6d 2d 3e 6e 4f 6e 63 65 3b 0d 0a 20 20 70  ram->nOnce;..  p
277dd 2d 3e 6e 4f 70 20 3d 20 75 2e 63 63 2e 70 50 72  ->nOp = u.cc.pPr
277de 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0d 0a 20 20 70  ogram->nOp;..  p
277df 63 20 3d 20 2d 31 3b 0d 0a 20 20 6d 65 6d 73 65  c = -1;..  memse
277e0 74 28 70 2d 3e 61 4f 6e 63 65 46 6c 61 67 2c 20  t(p->aOnceFlag, 
277e1 30 2c 20 70 2d 3e 6e 4f 6e 63 65 46 6c 61 67 29  0, p->nOnceFlag)
277e2 3b 0d 0a 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d  ;....  break;..}
277e3 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50  ..../* Opcode: P
277e4 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a  aram P1 P2 * * *
277e5 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 70  ..**..** This op
277e6 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65  code is only eve
277e7 72 20 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62  r present in sub
277e8 2d 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64  -programs called
277e9 20 76 69 61 20 74 68 65 20 0d 0a 2a 2a 20 4f 50   via the ..** OP
277ea 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63  _Program instruc
277eb 74 69 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c  tion. Copy a val
277ec 75 65 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  ue currently sto
277ed 72 65 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20  red in a memory 
277ee 0d 0a 2a 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65  ..** cell of the
277ef 20 63 61 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74   calling (parent
277f0 29 20 66 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20  ) frame to cell 
277f1 50 32 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  P2 in the curren
277f2 74 20 66 72 61 6d 65 73 20 0d 0a 2a 2a 20 61 64  t frames ..** ad
277f3 64 72 65 73 73 20 73 70 61 63 65 2e 20 54 68 69  dress space. Thi
277f4 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69  s is used by tri
277f5 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 74 6f  gger programs to
277f6 20 61 63 63 65 73 73 20 74 68 65 20 6e 65 77 2e   access the new.
277f7 2a 20 0d 0a 2a 2a 20 61 6e 64 20 6f 6c 64 2e 2a  * ..** and old.*
277f8 20 76 61 6c 75 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a   values...**..**
277f9 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   The address of 
277fa 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  the cell in the 
277fb 70 61 72 65 6e 74 20 66 72 61 6d 65 20 69 73 20  parent frame is 
277fc 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 61 64  determined by ad
277fd 64 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 76 61 6c  ding..** the val
277fe 75 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67  ue of the P1 arg
277ff 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 76 61 6c  ument to the val
27800 75 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67  ue of the P1 arg
27801 75 6d 65 6e 74 20 74 6f 20 74 68 65 0d 0a 2a 2a  ument to the..**
27802 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67   calling OP_Prog
27803 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ram instruction.
27804 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 50 61  ..*/..case OP_Pa
27805 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ram: {          
27806 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
27807 61 73 65 20 2a 2f 0d 0a 23 69 66 20 30 20 20 2f  ase */..#if 0  /
27808 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
27809 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
2780a 64 20 2a 2f 0d 0a 20 20 56 64 62 65 46 72 61 6d  d */..  VdbeFram
2780b 65 20 2a 70 46 72 61 6d 65 3b 0d 0a 20 20 4d 65  e *pFrame;..  Me
2780c 6d 20 2a 70 49 6e 3b 0d 0a 23 65 6e 64 69 66 20  m *pIn;..#endif 
2780d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
2780e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
2780f 63 64 20 2a 2f 0d 0a 20 20 75 2e 63 64 2e 70 46  cd */..  u.cd.pF
27810 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65  rame = p->pFrame
27811 3b 0d 0a 20 20 75 2e 63 64 2e 70 49 6e 20 3d 20  ;..  u.cd.pIn = 
27812 26 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 61 4d  &u.cd.pFrame->aM
27813 65 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63  em[pOp->p1 + u.c
27814 64 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e  d.pFrame->aOp[u.
27815 63 64 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70  cd.pFrame->pc].p
27816 31 5d 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  1];..  sqlite3Vd
27817 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
27818 28 70 4f 75 74 2c 20 75 2e 63 64 2e 70 49 6e 2c  (pOut, u.cd.pIn,
27819 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0d 0a 20 20   MEM_Ephem);..  
2781a 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e  break;..}....#en
2781b 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53  dif /* #ifndef S
2781c 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2781d 45 52 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66  ER */....#ifndef
2781e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
2781f 45 49 47 4e 5f 4b 45 59 0d 0a 2f 2a 20 4f 70 63  EIGN_KEY../* Opc
27820 6f 64 65 3a 20 46 6b 43 6f 75 6e 74 65 72 20 50  ode: FkCounter P
27821 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a  1 P2 * * *..**..
27822 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 61 20 22  ** Increment a "
27823 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
27824 65 72 22 20 62 79 20 50 32 20 28 50 32 20 6d 61  er" by P2 (P2 ma
27825 79 20 62 65 20 6e 65 67 61 74 69 76 65 20 6f 72  y be negative or
27826 20 70 6f 73 69 74 69 76 65 29 2e 0d 0a 2a 2a 20   positive)...** 
27827 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P1 is non-zer
27828 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  o, the database 
27829 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
2782a 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  er is incremente
2782b 64 20 0d 0a 2a 2a 20 28 64 65 66 65 72 72 65 64  d ..** (deferred
2782c 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
2782d 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 68 65 72  straints). Other
2782e 77 69 73 65 2c 20 69 66 20 50 31 20 69 73 20 7a  wise, if P1 is z
2782f 65 72 6f 2c 20 74 68 65 20 0d 0a 2a 2a 20 73 74  ero, the ..** st
27830 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 72 20  atement counter 
27831 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28  is incremented (
27832 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67  immediate foreig
27833 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
27834 73 29 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  s)...*/..case OP
27835 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0d 0a 20  _FkCounter: {.. 
27836 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0d   if( pOp->p1 ){.
27837 0a 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72  .    db->nDeferr
27838 65 64 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70  edCons += pOp->p
27839 32 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  2;..  }else{..  
2783a 20 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69    p->nFkConstrai
2783b 6e 74 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0d 0a  nt += pOp->p2;..
2783c 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
2783d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46  ..../* Opcode: F
2783e 6b 49 66 5a 65 72 6f 20 50 31 20 50 32 20 2a 20  kIfZero P1 P2 * 
2783f 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  * *..**..** This
27840 20 6f 70 63 6f 64 65 20 74 65 73 74 73 20 69 66   opcode tests if
27841 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63   a foreign key c
27842 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65  onstraint-counte
27843 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 7a  r is currently z
27844 65 72 6f 2e 0d 0a 2a 2a 20 49 66 20 73 6f 2c 20  ero...** If so, 
27845 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74  jump to instruct
27846 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73  ion P2. Otherwis
27847 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  e, fall through 
27848 74 6f 20 74 68 65 20 6e 65 78 74 20 0d 0a 2a 2a  to the next ..**
27849 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a   instruction...*
2784a 2a 0d 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e  *..** If P1 is n
2784b 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
2784c 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
2784d 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
2784e 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74  constraint-count
2784f 65 72 0d 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28  er..** is zero (
27850 74 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75  the one that cou
27851 6e 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e  nts deferred con
27852 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
27853 6e 73 29 2e 20 49 66 20 50 31 20 69 73 0d 0a 2a  ns). If P1 is..*
27854 2a 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70  * zero, the jump
27855 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65   is taken if the
27856 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74   statement const
27857 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73  raint-counter is
27858 20 7a 65 72 6f 0d 0a 2a 2a 20 28 69 6d 6d 65 64   zero..** (immed
27859 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79  iate foreign key
2785a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c   constraint viol
2785b 61 74 69 6f 6e 73 29 2e 0d 0a 2a 2f 0d 0a 63 61  ations)...*/..ca
2785c 73 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 3a 20  se OP_FkIfZero: 
2785d 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  {         /* jum
2785e 70 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4f 70 2d  p */..  if( pOp-
2785f 3e 70 31 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  >p1 ){..    if( 
27860 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e  db->nDeferredCon
27861 73 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 2d  s==0 ) pc = pOp-
27862 3e 70 32 2d 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b  >p2-1;..  }else{
27863 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 46 6b  ..    if( p->nFk
27864 43 6f 6e 73 74 72 61 69 6e 74 3d 3d 30 20 29 20  Constraint==0 ) 
27865 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0d  pc = pOp->p2-1;.
27866 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a  .  }..  break;..
27867 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66  }..#endif /* #if
27868 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27869 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a 2f 0d  _FOREIGN_KEY */.
2786a 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
2786b 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
2786c 4d 45 4e 54 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  MENT../* Opcode:
2786d 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20   MemMax P1 P2 * 
2786e 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 69  * *..**..** P1 i
2786f 73 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20  s a register in 
27870 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f  the root frame o
27871 66 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72  f this VM (the r
27872 6f 6f 74 20 66 72 61 6d 65 20 69 73 0d 0a 2a 2a  oot frame is..**
27873 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
27874 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d  the current fram
27875 65 20 69 66 20 74 68 69 73 20 69 6e 73 74 72 75  e if this instru
27876 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65  ction is being e
27877 78 65 63 75 74 65 64 0d 0a 2a 2a 20 77 69 74 68  xecuted..** with
27878 69 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 61 6d  in a sub-program
27879 29 2e 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ). Set the value
2787a 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
2787b 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  to the maximum o
2787c 66 20 0d 0a 2a 2a 20 69 74 73 20 63 75 72 72 65  f ..** its curre
2787d 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65  nt value and the
2787e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
2787f 65 72 20 50 32 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  er P2...**..** T
27880 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
27881 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20  throws an error 
27882 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  if the memory ce
27883 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61  ll is not initia
27884 6c 6c 79 0d 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  lly..** an integ
27885 65 72 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  er...*/..case OP
27886 5f 4d 65 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20  _MemMax: {      
27887 20 20 2f 2a 20 69 6e 32 20 2a 2f 0d 0a 23 69 66    /* in2 */..#if
27888 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
27889 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
2788a 6f 20 75 2e 63 65 20 2a 2f 0d 0a 20 20 4d 65 6d  o u.ce */..  Mem
2788b 20 2a 70 49 6e 31 3b 0d 0a 20 20 56 64 62 65 46   *pIn1;..  VdbeF
2788c 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0d 0a 23  rame *pFrame;..#
2788d 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
2788e 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
2788f 6e 74 6f 20 75 2e 63 65 20 2a 2f 0d 0a 20 20 69  nto u.ce */..  i
27890 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0d  f( p->pFrame ){.
27891 0a 20 20 20 20 66 6f 72 28 75 2e 63 65 2e 70 46  .    for(u.ce.pF
27892 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20  rame=p->pFrame; 
27893 75 2e 63 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61  u.ce.pFrame->pPa
27894 72 65 6e 74 3b 20 75 2e 63 65 2e 70 46 72 61 6d  rent; u.ce.pFram
27895 65 3d 75 2e 63 65 2e 70 46 72 61 6d 65 2d 3e 70  e=u.ce.pFrame->p
27896 50 61 72 65 6e 74 29 3b 0d 0a 20 20 20 20 75 2e  Parent);..    u.
27897 63 65 2e 70 49 6e 31 20 3d 20 26 75 2e 63 65 2e  ce.pIn1 = &u.ce.
27898 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70  pFrame->aMem[pOp
27899 2d 3e 70 31 5d 3b 0d 0a 20 20 7d 65 6c 73 65 7b  ->p1];..  }else{
2789a 0d 0a 20 20 20 20 75 2e 63 65 2e 70 49 6e 31 20  ..    u.ce.pIn1 
2789b 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
2789c 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
2789d 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75 2e 63  ( memIsValid(u.c
2789e 65 2e 70 49 6e 31 29 20 29 3b 0d 0a 20 20 73 71  e.pIn1) );..  sq
2789f 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
278a0 67 65 72 69 66 79 28 75 2e 63 65 2e 70 49 6e 31  gerify(u.ce.pIn1
278a1 29 3b 0d 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d  );..  pIn2 = &aM
278a2 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20  em[pOp->p2];..  
278a3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
278a4 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0d  tegerify(pIn2);.
278a5 0a 20 20 69 66 28 20 75 2e 63 65 2e 70 49 6e 31  .  if( u.ce.pIn1
278a6 2d 3e 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29  ->u.i<pIn2->u.i)
278a7 7b 0d 0a 20 20 20 20 75 2e 63 65 2e 70 49 6e 31  {..    u.ce.pIn1
278a8 2d 3e 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e  ->u.i = pIn2->u.
278a9 69 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b  i;..  }..  break
278aa 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
278ab 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
278ac 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0d 0a 0d 0a  INCREMENT */....
278ad 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73  /* Opcode: IfPos
278ae 20 50 31 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a   P1 P2 * * *..**
278af 0d 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
278b0 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  e of register P1
278b1 20 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72   is 1 or greater
278b2 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0d 0a 2a  , jump to P2...*
278b3 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  *..** It is ille
278b4 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20  gal to use this 
278b5 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61  instruction on a
278b6 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64   register that d
278b7 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  oes..** not cont
278b8 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
278b9 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
278ba 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
278bb 69 66 20 79 6f 75 20 74 72 79 2e 0d 0a 2a 2f 0d  if you try...*/.
278bc 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a 20  .case OP_IfPos: 
278bd 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
278be 2c 20 69 6e 31 20 2a 2f 0d 0a 20 20 70 49 6e 31  , in1 */..  pIn1
278bf 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
278c0 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 49  ];..  assert( pI
278c1 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  n1->flags&MEM_In
278c2 74 20 29 3b 0d 0a 20 20 69 66 28 20 70 49 6e 31  t );..  if( pIn1
278c3 2d 3e 75 2e 69 3e 30 20 29 7b 0d 0a 20 20 20 20  ->u.i>0 ){..    
278c4 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
278c5 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b  1;..  }..  break
278c6 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64  ;..}..../* Opcod
278c7 65 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a  e: IfNeg P1 P2 *
278c8 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20   * *..**..** If 
278c9 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
278ca 69 73 74 65 72 20 50 31 20 69 73 20 6c 65 73 73  ister P1 is less
278cb 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70   than zero, jump
278cc 20 74 6f 20 50 32 2e 20 0d 0a 2a 2a 0d 0a 2a 2a   to P2. ..**..**
278cd 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
278ce 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
278cf 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
278d0 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0d 0a  ster that does..
278d1 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
278d2 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
278d3 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
278d4 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
278d5 75 20 74 72 79 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  u try...*/..case
278d6 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20   OP_IfNeg: {    
278d7 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
278d8 20 2a 2f 0d 0a 20 20 70 49 6e 31 20 3d 20 26 61   */..  pIn1 = &a
278d9 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20  Mem[pOp->p1];.. 
278da 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66   assert( pIn1->f
278db 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0d  lags&MEM_Int );.
278dc 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69  .  if( pIn1->u.i
278dd 3c 30 20 29 7b 0d 0a 20 20 20 20 20 70 63 20 3d  <0 ){..     pc =
278de 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20   pOp->p2 - 1;.. 
278df 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d   }..  break;..}.
278e0 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66  .../* Opcode: If
278e1 5a 65 72 6f 20 50 31 20 50 32 20 50 33 20 2a 20  Zero P1 P2 P3 * 
278e2 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65  *..**..** The re
278e3 67 69 73 74 65 72 20 50 31 20 6d 75 73 74 20 63  gister P1 must c
278e4 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
278e5 72 2e 20 20 41 64 64 20 6c 69 74 65 72 61 6c 20  r.  Add literal 
278e6 50 33 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 76 61  P3 to the..** va
278e7 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
278e8 50 31 2e 20 20 49 66 20 74 68 65 20 72 65 73 75  P1.  If the resu
278e9 6c 74 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c  lt is exactly 0,
278ea 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0d 0a 2a   jump to P2. ..*
278eb 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  *..** It is ille
278ec 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20  gal to use this 
278ed 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61  instruction on a
278ee 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64   register that d
278ef 6f 65 73 0d 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  oes..** not cont
278f0 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
278f1 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
278f2 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
278f3 69 66 20 79 6f 75 20 74 72 79 2e 0d 0a 2a 2f 0d  if you try...*/.
278f4 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f 3a  .case OP_IfZero:
278f5 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
278f6 70 2c 20 69 6e 31 20 2a 2f 0d 0a 20 20 70 49 6e  p, in1 */..  pIn
278f7 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
278f8 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  1];..  assert( p
278f9 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49  In1->flags&MEM_I
278fa 6e 74 20 29 3b 0d 0a 20 20 70 49 6e 31 2d 3e 75  nt );..  pIn1->u
278fb 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 33 3b 0d 0a  .i += pOp->p3;..
278fc 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3d    if( pIn1->u.i=
278fd 3d 30 20 29 7b 0d 0a 20 20 20 20 20 70 63 20 3d  =0 ){..     pc =
278fe 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20   pOp->p2 - 1;.. 
278ff 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d   }..  break;..}.
27900 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67  .../* Opcode: Ag
27901 67 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 34  gStep * P2 P3 P4
27902 20 50 35 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 65 63   P5..**..** Exec
27903 75 74 65 20 74 68 65 20 73 74 65 70 20 66 75 6e  ute the step fun
27904 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67  ction for an agg
27905 72 65 67 61 74 65 2e 20 20 54 68 65 0d 0a 2a 2a  regate.  The..**
27906 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 50 35   function has P5
27907 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 20 50 34   arguments.   P4
27908 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
27909 20 74 68 65 20 46 75 6e 63 44 65 66 0d 0a 2a 2a   the FuncDef..**
2790a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
2790b 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66 75  specifies the fu
2790c 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 65 67  nction.  Use reg
2790d 69 73 74 65 72 0d 0a 2a 2a 20 50 33 20 61 73 20  ister..** P3 as 
2790e 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e  the accumulator.
2790f 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 50 35 20  ..**..** The P5 
27910 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61  arguments are ta
27911 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65  ken from registe
27912 72 20 50 32 20 61 6e 64 20 69 74 73 0d 0a 2a 2a  r P2 and its..**
27913 20 73 75 63 63 65 73 73 6f 72 73 2e 0d 0a 2a 2f   successors...*/
27914 0d 0a 63 61 73 65 20 4f 50 5f 41 67 67 53 74 65  ..case OP_AggSte
27915 70 3a 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20  p: {..#if 0  /* 
27916 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
27917 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20  moved into u.cf 
27918 2a 2f 0d 0a 20 20 69 6e 74 20 6e 3b 0d 0a 20 20  */..  int n;..  
27919 69 6e 74 20 69 3b 0d 0a 20 20 4d 65 6d 20 2a 70  int i;..  Mem *p
2791a 4d 65 6d 3b 0d 0a 20 20 4d 65 6d 20 2a 70 52 65  Mem;..  Mem *pRe
2791b 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  c;..  sqlite3_co
2791c 6e 74 65 78 74 20 63 74 78 3b 0d 0a 20 20 73 71  ntext ctx;..  sq
2791d 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
2791e 56 61 6c 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  Val;..#endif /* 
2791f 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
27920 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20  moved into u.cf 
27921 2a 2f 0d 0a 0d 0a 20 20 75 2e 63 66 2e 6e 20 3d  */....  u.cf.n =
27922 20 70 4f 70 2d 3e 70 35 3b 0d 0a 20 20 61 73 73   pOp->p5;..  ass
27923 65 72 74 28 20 75 2e 63 66 2e 6e 3e 3d 30 20 29  ert( u.cf.n>=0 )
27924 3b 0d 0a 20 20 75 2e 63 66 2e 70 52 65 63 20 3d  ;..  u.cf.pRec =
27925 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b   &aMem[pOp->p2];
27926 0d 0a 20 20 75 2e 63 66 2e 61 70 56 61 6c 20 3d  ..  u.cf.apVal =
27927 20 70 2d 3e 61 70 41 72 67 3b 0d 0a 20 20 61 73   p->apArg;..  as
27928 73 65 72 74 28 20 75 2e 63 66 2e 61 70 56 61 6c  sert( u.cf.apVal
27929 20 7c 7c 20 75 2e 63 66 2e 6e 3d 3d 30 20 29 3b   || u.cf.n==0 );
2792a 0d 0a 20 20 66 6f 72 28 75 2e 63 66 2e 69 3d 30  ..  for(u.cf.i=0
2792b 3b 20 75 2e 63 66 2e 69 3c 75 2e 63 66 2e 6e 3b  ; u.cf.i<u.cf.n;
2792c 20 75 2e 63 66 2e 69 2b 2b 2c 20 75 2e 63 66 2e   u.cf.i++, u.cf.
2792d 70 52 65 63 2b 2b 29 7b 0d 0a 20 20 20 20 61 73  pRec++){..    as
2792e 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64  sert( memIsValid
2792f 28 75 2e 63 66 2e 70 52 65 63 29 20 29 3b 0d 0a  (u.cf.pRec) );..
27930 20 20 20 20 75 2e 63 66 2e 61 70 56 61 6c 5b 75      u.cf.apVal[u
27931 2e 63 66 2e 69 5d 20 3d 20 75 2e 63 66 2e 70 52  .cf.i] = u.cf.pR
27932 65 63 3b 0d 0a 20 20 20 20 6d 65 6d 41 62 6f 75  ec;..    memAbou
27933 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 75 2e 63  tToChange(p, u.c
27934 66 2e 70 52 65 63 29 3b 0d 0a 20 20 20 20 73 71  f.pRec);..    sq
27935 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
27936 65 54 79 70 65 28 75 2e 63 66 2e 70 52 65 63 29  eType(u.cf.pRec)
27937 3b 0d 0a 20 20 7d 0d 0a 20 20 75 2e 63 66 2e 63  ;..  }..  u.cf.c
27938 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e  tx.pFunc = pOp->
27939 70 34 2e 70 46 75 6e 63 3b 0d 0a 20 20 61 73 73  p4.pFunc;..  ass
2793a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
2793b 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
2793c 65 6d 20 29 3b 0d 0a 20 20 75 2e 63 66 2e 63 74  em );..  u.cf.ct
2793d 78 2e 70 4d 65 6d 20 3d 20 75 2e 63 66 2e 70 4d  x.pMem = u.cf.pM
2793e 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  em = &aMem[pOp->
2793f 70 33 5d 3b 0d 0a 20 20 75 2e 63 66 2e 70 4d 65  p3];..  u.cf.pMe
27940 6d 2d 3e 6e 2b 2b 3b 0d 0a 20 20 75 2e 63 66 2e  m->n++;..  u.cf.
27941 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
27942 4d 5f 4e 75 6c 6c 3b 0d 0a 20 20 75 2e 63 66 2e  M_Null;..  u.cf.
27943 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0d 0a 20 20  ctx.s.z = 0;..  
27944 75 2e 63 66 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c  u.cf.ctx.s.zMall
27945 6f 63 20 3d 20 30 3b 0d 0a 20 20 75 2e 63 66 2e  oc = 0;..  u.cf.
27946 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0d  ctx.s.xDel = 0;.
27947 0a 20 20 75 2e 63 66 2e 63 74 78 2e 73 2e 64 62  .  u.cf.ctx.s.db
27948 20 3d 20 64 62 3b 0d 0a 20 20 75 2e 63 66 2e 63   = db;..  u.cf.c
27949 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0d  tx.isError = 0;.
2794a 0a 20 20 75 2e 63 66 2e 63 74 78 2e 70 43 6f 6c  .  u.cf.ctx.pCol
2794b 6c 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 75 2e  l = 0;..  if( u.
2794c 63 66 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c  cf.ctx.pFunc->fl
2794d 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
2794e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0d 0a 20  C_NEEDCOLL ){.. 
2794f 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70     assert( pOp>p
27950 2d 3e 61 4f 70 20 29 3b 0d 0a 20 20 20 20 61 73  ->aOp );..    as
27951 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34  sert( pOp[-1].p4
27952 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51  type==P4_COLLSEQ
27953 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
27954 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d   pOp[-1].opcode=
27955 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0d 0a  =OP_CollSeq );..
27956 20 20 20 20 75 2e 63 66 2e 63 74 78 2e 70 43 6f      u.cf.ctx.pCo
27957 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e  ll = pOp[-1].p4.
27958 70 43 6f 6c 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 28  pColl;..  }..  (
27959 75 2e 63 66 2e 63 74 78 2e 70 46 75 6e 63 2d 3e  u.cf.ctx.pFunc->
2795a 78 53 74 65 70 29 28 26 75 2e 63 66 2e 63 74 78  xStep)(&u.cf.ctx
2795b 2c 20 75 2e 63 66 2e 6e 2c 20 75 2e 63 66 2e 61  , u.cf.n, u.cf.a
2795c 70 56 61 6c 29 3b 20 2f 2a 20 49 4d 50 3a 20 52  pVal); /* IMP: R
2795d 2d 32 34 35 30 35 2d 32 33 32 33 30 20 2a 2f 0d  -24505-23230 */.
2795e 0a 20 20 69 66 28 20 75 2e 63 66 2e 63 74 78 2e  .  if( u.cf.ctx.
2795f 69 73 45 72 72 6f 72 20 29 7b 0d 0a 20 20 20 20  isError ){..    
27960 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
27961 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
27962 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f  , "%s", sqlite3_
27963 76 61 6c 75 65 5f 74 65 78 74 28 26 75 2e 63 66  value_text(&u.cf
27964 2e 63 74 78 2e 73 29 29 3b 0d 0a 20 20 20 20 72  .ctx.s));..    r
27965 63 20 3d 20 75 2e 63 66 2e 63 74 78 2e 69 73 45  c = u.cf.ctx.isE
27966 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rror;..  }....  
27967 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
27968 6c 65 61 73 65 28 26 75 2e 63 66 2e 63 74 78 2e  lease(&u.cf.ctx.
27969 73 29 3b 0d 0a 0d 0a 20 20 62 72 65 61 6b 3b 0d  s);....  break;.
2796a 0a 7d 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}..../* Opcode:
2796b 20 41 67 67 46 69 6e 61 6c 20 50 31 20 50 32 20   AggFinal P1 P2 
2796c 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 45  * P4 *..**..** E
2796d 78 65 63 75 74 65 20 74 68 65 20 66 69 6e 61 6c  xecute the final
2796e 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  izer function fo
2796f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20  r an aggregate. 
27970 20 50 31 20 69 73 0d 0a 2a 2a 20 74 68 65 20 6d   P1 is..** the m
27971 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
27972 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 6d  hat is the accum
27973 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 61  ulator for the a
27974 67 67 72 65 67 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a  ggregate...**..*
27975 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62  * P2 is the numb
27976 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
27977 74 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75  that the step fu
27978 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64  nction takes and
27979 0d 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69  ..** P4 is a poi
2797a 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63  nter to the Func
2797b 44 65 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e  Def for this fun
2797c 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32 0d 0a  ction.  The P2..
2797d 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ** argument is n
2797e 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ot used by this 
2797f 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20 6f  opcode.  It is o
27980 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69 73  nly there to dis
27981 61 6d 62 69 67 75 61 74 65 0d 0a 2a 2a 20 66 75  ambiguate..** fu
27982 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  nctions that can
27983 20 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75   take varying nu
27984 6d 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e  mbers of argumen
27985 74 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 50 34 20  ts.  The..** P4 
27986 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79  argument is only
27987 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20   needed for the 
27988 64 65 67 65 6e 65 72 61 74 65 20 63 61 73 65 20  degenerate case 
27989 77 68 65 72 65 0d 0a 2a 2a 20 74 68 65 20 73 74  where..** the st
2798a 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  ep function was 
2798b 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63  not previously c
2798c 61 6c 6c 65 64 2e 0d 0a 2a 2f 0d 0a 63 61 73 65  alled...*/..case
2798d 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0d   OP_AggFinal: {.
2798e 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
2798f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
27990 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0d 0a 20   into u.cg */.. 
27991 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0d 0a 23 65 6e   Mem *pMem;..#en
27992 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
27993 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
27994 6f 20 75 2e 63 67 20 2a 2f 0d 0a 20 20 61 73 73  o u.cg */..  ass
27995 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26  ert( pOp->p1>0 &
27996 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d  & pOp->p1<=p->nM
27997 65 6d 20 29 3b 0d 0a 20 20 75 2e 63 67 2e 70 4d  em );..  u.cg.pM
27998 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  em = &aMem[pOp->
27999 70 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  p1];..  assert( 
2799a 28 75 2e 63 67 2e 70 4d 65 6d 2d 3e 66 6c 61 67  (u.cg.pMem->flag
2799b 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d  s & ~(MEM_Null|M
2799c 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0d 0a  EM_Agg))==0 );..
2799d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
2799e 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e  beMemFinalize(u.
2799f 63 67 2e 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34  cg.pMem, pOp->p4
279a0 2e 70 46 75 6e 63 29 3b 0d 0a 20 20 69 66 28 20  .pFunc);..  if( 
279a1 72 63 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  rc ){..    sqlit
279a2 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
279a3 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
279a4 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ", sqlite3_value
279a5 5f 74 65 78 74 28 75 2e 63 67 2e 70 4d 65 6d 29  _text(u.cg.pMem)
279a6 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
279a7 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
279a8 64 69 6e 67 28 75 2e 63 67 2e 70 4d 65 6d 2c 20  ding(u.cg.pMem, 
279a9 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20 20 55 50  encoding);..  UP
279aa 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
279ab 45 28 75 2e 63 67 2e 70 4d 65 6d 29 3b 0d 0a 20  E(u.cg.pMem);.. 
279ac 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
279ad 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 67 2e 70  MemTooBig(u.cg.p
279ae 4d 65 6d 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74  Mem) ){..    got
279af 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a 20 20 7d 0d  o too_big;..  }.
279b0 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a  .  break;..}....
279b1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
279b2 4d 49 54 5f 57 41 4c 0d 0a 2f 2a 20 4f 70 63 6f  MIT_WAL../* Opco
279b3 64 65 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 50  de: Checkpoint P
279b4 31 20 50 32 20 50 33 20 2a 20 2a 0d 0a 2a 2a 0d  1 P2 P3 * *..**.
279b5 0a 2a 2a 20 43 68 65 63 6b 70 6f 69 6e 74 20 64  .** Checkpoint d
279b6 61 74 61 62 61 73 65 20 50 31 2e 20 54 68 69 73  atabase P1. This
279b7 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 50   is a no-op if P
279b8 31 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  1 is not current
279b9 6c 79 20 69 6e 0d 0a 2a 2a 20 57 41 4c 20 6d 6f  ly in..** WAL mo
279ba 64 65 2e 20 50 61 72 61 6d 65 74 65 72 20 50 32  de. Parameter P2
279bb 20 69 73 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54   is one of SQLIT
279bc 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53  E_CHECKPOINT_PAS
279bd 53 49 56 45 2c 20 46 55 4c 4c 0d 0a 2a 2a 20 6f  SIVE, FULL..** o
279be 72 20 52 45 53 54 41 52 54 2e 20 20 57 72 69 74  r RESTART.  Writ
279bf 65 20 31 20 6f 72 20 30 20 69 6e 74 6f 20 6d 65  e 1 or 0 into me
279c0 6d 5b 50 33 5d 20 69 66 20 74 68 65 20 63 68 65  m[P3] if the che
279c1 63 6b 70 6f 69 6e 74 20 72 65 74 75 72 6e 73 0d  ckpoint returns.
279c2 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20  .** SQLITE_BUSY 
279c3 6f 72 20 6e 6f 74 2c 20 72 65 73 70 65 63 74 69  or not, respecti
279c4 76 65 6c 79 2e 20 20 57 72 69 74 65 20 74 68 65  vely.  Write the
279c5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
279c6 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 57 41 4c 20   in the..** WAL 
279c7 61 66 74 65 72 20 74 68 65 20 63 68 65 63 6b 70  after the checkp
279c8 6f 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 5b 50 33  oint into mem[P3
279c9 2b 31 5d 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  +1] and the numb
279ca 65 72 20 6f 66 20 70 61 67 65 73 0d 0a 2a 2a 20  er of pages..** 
279cb 69 6e 20 74 68 65 20 57 41 4c 20 74 68 61 74 20  in the WAL that 
279cc 68 61 76 65 20 62 65 65 6e 20 63 68 65 63 6b 70  have been checkp
279cd 6f 69 6e 74 65 64 20 61 66 74 65 72 20 74 68 65  ointed after the
279ce 20 63 68 65 63 6b 70 6f 69 6e 74 0d 0a 2a 2a 20   checkpoint..** 
279cf 63 6f 6d 70 6c 65 74 65 73 20 69 6e 74 6f 20 6d  completes into m
279d0 65 6d 5b 50 33 2b 32 5d 2e 20 20 48 6f 77 65 76  em[P3+2].  Howev
279d1 65 72 20 6f 6e 20 61 6e 20 65 72 72 6f 72 2c 20  er on an error, 
279d2 6d 65 6d 5b 50 33 2b 31 5d 20 61 6e 64 0d 0a 2a  mem[P3+1] and..*
279d3 2a 20 6d 65 6d 5b 50 33 2b 32 5d 20 61 72 65 20  * mem[P3+2] are 
279d4 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 2d  initialized to -
279d5 31 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f  1...*/..case OP_
279d6 43 68 65 63 6b 70 6f 69 6e 74 3a 20 7b 0d 0a 23  Checkpoint: {..#
279d7 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
279d8 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
279d9 6e 74 6f 20 75 2e 63 68 20 2a 2f 0d 0a 20 20 69  nto u.ch */..  i
279da 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
279db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
279dc 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
279dd 2f 0d 0a 20 20 69 6e 74 20 61 52 65 73 5b 33 5d  /..  int aRes[3]
279de 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
279df 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20       /* Results 
279e0 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  */..  Mem *pMem;
279e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
279e2 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72        /* Write r
279e3 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0d 0a  esults here */..
279e4 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
279e5 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
279e6 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0d 0a 0d 0a  into u.ch */....
279e7 20 20 75 2e 63 68 2e 61 52 65 73 5b 30 5d 20 3d    u.ch.aRes[0] =
279e8 20 30 3b 0d 0a 20 20 75 2e 63 68 2e 61 52 65 73   0;..  u.ch.aRes
279e9 5b 31 5d 20 3d 20 75 2e 63 68 2e 61 52 65 73 5b  [1] = u.ch.aRes[
279ea 32 5d 20 3d 20 2d 31 3b 0d 0a 20 20 61 73 73 65  2] = -1;..  asse
279eb 72 74 28 20 70 4f 70 2d 3e 70 32 3d 3d 53 51 4c  rt( pOp->p2==SQL
279ec 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50  ITE_CHECKPOINT_P
279ed 41 53 53 49 56 45 0d 0a 20 20 20 20 20 20 20 7c  ASSIVE..       |
279ee 7c 20 70 4f 70 2d 3e 70 32 3d 3d 53 51 4c 49 54  | pOp->p2==SQLIT
279ef 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 4c  E_CHECKPOINT_FUL
279f0 4c 0d 0a 20 20 20 20 20 20 20 7c 7c 20 70 4f 70  L..       || pOp
279f1 2d 3e 70 32 3d 3d 53 51 4c 49 54 45 5f 43 48 45  ->p2==SQLITE_CHE
279f2 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41 52 54 0d  CKPOINT_RESTART.
279f3 0a 20 20 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71  .  );..  rc = sq
279f4 6c 69 74 65 33 43 68 65 63 6b 70 6f 69 6e 74 28  lite3Checkpoint(
279f5 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
279f6 2d 3e 70 32 2c 20 26 75 2e 63 68 2e 61 52 65 73  ->p2, &u.ch.aRes
279f7 5b 31 5d 2c 20 26 75 2e 63 68 2e 61 52 65 73 5b  [1], &u.ch.aRes[
279f8 32 5d 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d  2]);..  if( rc==
279f9 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0d 0a  SQLITE_BUSY ){..
279fa 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
279fb 4f 4b 3b 0d 0a 20 20 20 20 75 2e 63 68 2e 61 52  OK;..    u.ch.aR
279fc 65 73 5b 30 5d 20 3d 20 31 3b 0d 0a 20 20 7d 0d  es[0] = 1;..  }.
279fd 0a 20 20 66 6f 72 28 75 2e 63 68 2e 69 3d 30 2c  .  for(u.ch.i=0,
279fe 20 75 2e 63 68 2e 70 4d 65 6d 20 3d 20 26 61 4d   u.ch.pMem = &aM
279ff 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 20 75 2e 63  em[pOp->p3]; u.c
27a00 68 2e 69 3c 33 3b 20 75 2e 63 68 2e 69 2b 2b 2c  h.i<3; u.ch.i++,
27a01 20 75 2e 63 68 2e 70 4d 65 6d 2b 2b 29 7b 0d 0a   u.ch.pMem++){..
27a02 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
27a03 65 6d 53 65 74 49 6e 74 36 34 28 75 2e 63 68 2e  emSetInt64(u.ch.
27a04 70 4d 65 6d 2c 20 28 69 36 34 29 75 2e 63 68 2e  pMem, (i64)u.ch.
27a05 61 52 65 73 5b 75 2e 63 68 2e 69 5d 29 3b 0d 0a  aRes[u.ch.i]);..
27a06 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
27a07 3b 20 20 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23  ;  ..#endif....#
27a08 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27a09 49 54 5f 50 52 41 47 4d 41 0d 0a 2f 2a 20 4f 70  IT_PRAGMA../* Op
27a0a 63 6f 64 65 3a 20 4a 6f 75 72 6e 61 6c 4d 6f 64  code: JournalMod
27a0b 65 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0d  e P1 P2 P3 * P5.
27a0c 0a 2a 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74  .**..** Change t
27a0d 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20  he journal mode 
27a0e 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 20 74  of database P1 t
27a0f 6f 20 50 33 2e 20 50 33 20 6d 75 73 74 20 62 65  o P3. P3 must be
27a10 20 6f 6e 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20   one of the..** 
27a11 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
27a12 45 5f 58 58 58 20 76 61 6c 75 65 73 2e 20 49 66  E_XXX values. If
27a13 20 63 68 61 6e 67 69 6e 67 20 62 65 74 77 65 65   changing betwee
27a14 6e 20 74 68 65 20 76 61 72 69 6f 75 73 20 72 6f  n the various ro
27a15 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 6d 6f 64 65 73  llback..** modes
27a16 20 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61   (delete, trunca
27a17 74 65 2c 20 70 65 72 73 69 73 74 2c 20 6f 66 66  te, persist, off
27a18 20 61 6e 64 20 6d 65 6d 6f 72 79 29 2c 20 74 68   and memory), th
27a19 69 73 20 69 73 20 61 20 73 69 6d 70 6c 65 0d 0a  is is a simple..
27a1a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4e 6f  ** operation. No
27a1b 20 49 4f 20 69 73 20 72 65 71 75 69 72 65 64 2e   IO is required.
27a1c 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 63 68 61 6e  ..**..** If chan
27a1d 67 69 6e 67 20 69 6e 74 6f 20 6f 72 20 6f 75 74  ging into or out
27a1e 20 6f 66 20 57 41 4c 20 6d 6f 64 65 20 74 68 65   of WAL mode the
27a1f 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 6d 6f   procedure is mo
27a20 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 0d  re complicated..
27a21 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 61 20  .**..** Write a 
27a22 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
27a23 67 20 74 68 65 20 66 69 6e 61 6c 20 6a 6f 75 72  g the final jour
27a24 6e 61 6c 2d 6d 6f 64 65 20 74 6f 20 72 65 67 69  nal-mode to regi
27a25 73 74 65 72 20 50 32 2e 0d 0a 2a 2f 0d 0a 63 61  ster P2...*/..ca
27a26 73 65 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f 64  se OP_JournalMod
27a27 65 3a 20 7b 20 20 20 20 2f 2a 20 6f 75 74 32 2d  e: {    /* out2-
27a28 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 23  prerelease */..#
27a29 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
27a2a 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
27a2b 6e 74 6f 20 75 2e 63 69 20 2a 2f 0d 0a 20 20 42  nto u.ci */..  B
27a2c 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20  tree *pBt;      
27a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27a2e 2a 20 42 74 72 65 65 20 74 6f 20 63 68 61 6e 67  * Btree to chang
27a2f 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 6f  e journal mode o
27a30 66 20 2a 2f 0d 0a 20 20 50 61 67 65 72 20 2a 70  f */..  Pager *p
27a31 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20  Pager;          
27a32 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72          /* Pager
27a33 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
27a34 20 70 42 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 65   pBt */..  int e
27a35 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  New;            
27a36 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
27a37 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 2a  w journal mode *
27a38 2f 0d 0a 20 20 69 6e 74 20 65 4f 6c 64 3b 20 20  /..  int eOld;  
27a39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a3a 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c 64 20       /* The old 
27a3b 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 2a 2f 0d  journal mode */.
27a3c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27a3d 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20  Filename;       
27a3e 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
27a3f 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20  tabase file for 
27a40 70 50 61 67 65 72 20 2a 2f 0d 0a 23 65 6e 64 69  pPager */..#endi
27a41 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
27a42 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
27a43 75 2e 63 69 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 63  u.ci */....  u.c
27a44 69 2e 65 4e 65 77 20 3d 20 70 4f 70 2d 3e 70 33  i.eNew = pOp->p3
27a45 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  ;..  assert( u.c
27a46 69 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f  i.eNew==PAGER_JO
27a47 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
27a48 0d 0a 20 20 20 20 20 20 20 7c 7c 20 75 2e 63 69  ..       || u.ci
27a49 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55  .eNew==PAGER_JOU
27a4a 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
27a4b 45 0d 0a 20 20 20 20 20 20 20 7c 7c 20 75 2e 63  E..       || u.c
27a4c 69 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f  i.eNew==PAGER_JO
27a4d 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
27a4e 54 0d 0a 20 20 20 20 20 20 20 7c 7c 20 75 2e 63  T..       || u.c
27a4f 69 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f  i.eNew==PAGER_JO
27a50 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0d 0a 20  URNALMODE_OFF.. 
27a51 20 20 20 20 20 20 7c 7c 20 75 2e 63 69 2e 65 4e        || u.ci.eN
27a52 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ew==PAGER_JOURNA
27a53 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0d 0a 20 20  LMODE_MEMORY..  
27a54 20 20 20 20 20 7c 7c 20 75 2e 63 69 2e 65 4e 65       || u.ci.eNe
27a55 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  w==PAGER_JOURNAL
27a56 4d 4f 44 45 5f 57 41 4c 0d 0a 20 20 20 20 20 20  MODE_WAL..      
27a57 20 7c 7c 20 75 2e 63 69 2e 65 4e 65 77 3d 3d 50   || u.ci.eNew==P
27a58 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
27a59 5f 51 55 45 52 59 0d 0a 20 20 29 3b 0d 0a 20 20  _QUERY..  );..  
27a5a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
27a5b 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
27a5c 2d 3e 6e 44 62 20 29 3b 0d 0a 0d 0a 20 20 75 2e  ->nDb );....  u.
27a5d 63 69 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  ci.pBt = db->aDb
27a5e 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0d 0a  [pOp->p1].pBt;..
27a5f 20 20 75 2e 63 69 2e 70 50 61 67 65 72 20 3d 20    u.ci.pPager = 
27a60 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
27a61 72 28 75 2e 63 69 2e 70 42 74 29 3b 0d 0a 20 20  r(u.ci.pBt);..  
27a62 75 2e 63 69 2e 65 4f 6c 64 20 3d 20 73 71 6c 69  u.ci.eOld = sqli
27a63 74 65 33 50 61 67 65 72 47 65 74 4a 6f 75 72 6e  te3PagerGetJourn
27a64 61 6c 4d 6f 64 65 28 75 2e 63 69 2e 70 50 61 67  alMode(u.ci.pPag
27a65 65 72 29 3b 0d 0a 20 20 69 66 28 20 75 2e 63 69  er);..  if( u.ci
27a66 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55  .eNew==PAGER_JOU
27a67 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 29  RNALMODE_QUERY )
27a68 20 75 2e 63 69 2e 65 4e 65 77 20 3d 20 75 2e 63   u.ci.eNew = u.c
27a69 69 2e 65 4f 6c 64 3b 0d 0a 20 20 69 66 28 20 21  i.eOld;..  if( !
27a6a 73 71 6c 69 74 65 33 50 61 67 65 72 4f 6b 54 6f  sqlite3PagerOkTo
27a6b 43 68 61 6e 67 65 4a 6f 75 72 6e 61 6c 4d 6f 64  ChangeJournalMod
27a6c 65 28 75 2e 63 69 2e 70 50 61 67 65 72 29 20 29  e(u.ci.pPager) )
27a6d 20 75 2e 63 69 2e 65 4e 65 77 20 3d 20 75 2e 63   u.ci.eNew = u.c
27a6e 69 2e 65 4f 6c 64 3b 0d 0a 0d 0a 23 69 66 6e 64  i.eOld;....#ifnd
27a6f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
27a70 41 4c 0d 0a 20 20 75 2e 63 69 2e 7a 46 69 6c 65  AL..  u.ci.zFile
27a71 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 50 61  name = sqlite3Pa
27a72 67 65 72 46 69 6c 65 6e 61 6d 65 28 75 2e 63 69  gerFilename(u.ci
27a73 2e 70 50 61 67 65 72 29 3b 0d 0a 0d 0a 20 20 2f  .pPager);....  /
27a74 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61  * Do not allow a
27a75 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f 20 6a   transition to j
27a76 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 20  ournal_mode=WAL 
27a77 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 0d 0a  for a database..
27a78 20 20 2a 2a 20 69 6e 20 74 65 6d 70 6f 72 61 72    ** in temporar
27a79 79 20 73 74 6f 72 61 67 65 20 6f 72 20 69 66 20  y storage or if 
27a7a 74 68 65 20 56 46 53 20 64 6f 65 73 20 6e 6f 74  the VFS does not
27a7b 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 20   support shared 
27a7c 6d 65 6d 6f 72 79 0d 0a 20 20 2a 2f 0d 0a 20 20  memory..  */..  
27a7d 69 66 28 20 75 2e 63 69 2e 65 4e 65 77 3d 3d 50  if( u.ci.eNew==P
27a7e 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
27a7f 5f 57 41 4c 0d 0a 20 20 20 26 26 20 28 73 71 6c  _WAL..   && (sql
27a80 69 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e 63  ite3Strlen30(u.c
27a81 69 2e 7a 46 69 6c 65 6e 61 6d 65 29 3d 3d 30 20  i.zFilename)==0 
27a82 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
27a83 70 20 66 69 6c 65 20 2a 2f 0d 0a 20 20 20 20 20  p file */..     
27a84 20 20 7c 7c 20 21 73 71 6c 69 74 65 33 50 61 67    || !sqlite3Pag
27a85 65 72 57 61 6c 53 75 70 70 6f 72 74 65 64 28 75  erWalSupported(u
27a86 2e 63 69 2e 70 50 61 67 65 72 29 29 20 20 20 2f  .ci.pPager))   /
27a87 2a 20 4e 6f 20 73 68 61 72 65 64 2d 6d 65 6d 6f  * No shared-memo
27a88 72 79 20 73 75 70 70 6f 72 74 20 2a 2f 0d 0a 20  ry support */.. 
27a89 20 29 7b 0d 0a 20 20 20 20 75 2e 63 69 2e 65 4e   ){..    u.ci.eN
27a8a 65 77 20 3d 20 75 2e 63 69 2e 65 4f 6c 64 3b 0d  ew = u.ci.eOld;.
27a8b 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 28 75  .  }....  if( (u
27a8c 2e 63 69 2e 65 4e 65 77 21 3d 75 2e 63 69 2e 65  .ci.eNew!=u.ci.e
27a8d 4f 6c 64 29 0d 0a 20 20 20 26 26 20 28 75 2e 63  Old)..   && (u.c
27a8e 69 2e 65 4f 6c 64 3d 3d 50 41 47 45 52 5f 4a 4f  i.eOld==PAGER_JO
27a8f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 7c 7c  URNALMODE_WAL ||
27a90 20 75 2e 63 69 2e 65 4e 65 77 3d 3d 50 41 47 45   u.ci.eNew==PAGE
27a91 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41  R_JOURNALMODE_WA
27a92 4c 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 69 66  L)..  ){..    if
27a93 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  ( !db->autoCommi
27a94 74 20 7c 7c 20 64 62 2d 3e 61 63 74 69 76 65 56  t || db->activeV
27a95 64 62 65 43 6e 74 3e 31 20 29 7b 0d 0a 20 20 20  dbeCnt>1 ){..   
27a96 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
27a97 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 73 71 6c  RROR;..      sql
27a98 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
27a99 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0d 0a  ->zErrMsg, db,..
27a9a 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
27a9b 74 20 63 68 61 6e 67 65 20 25 73 20 77 61 6c 20  t change %s wal 
27a9c 6d 6f 64 65 20 66 72 6f 6d 20 77 69 74 68 69 6e  mode from within
27a9d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 2c   a transaction",
27a9e 0d 0a 20 20 20 20 20 20 20 20 20 20 28 75 2e 63  ..          (u.c
27a9f 69 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f  i.eNew==PAGER_JO
27aa0 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 3f 20  URNALMODE_WAL ? 
27aa1 22 69 6e 74 6f 22 20 3a 20 22 6f 75 74 20 6f 66  "into" : "out of
27aa2 22 29 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20  ")..      );..  
27aa3 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
27aa4 7d 65 6c 73 65 7b 0d 0a 0d 0a 20 20 20 20 20 20  }else{....      
27aa5 69 66 28 20 75 2e 63 69 2e 65 4f 6c 64 3d 3d 50  if( u.ci.eOld==P
27aa6 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
27aa7 5f 57 41 4c 20 29 7b 0d 0a 20 20 20 20 20 20 20  _WAL ){..       
27aa8 20 2f 2a 20 49 66 20 6c 65 61 76 69 6e 67 20 57   /* If leaving W
27aa9 41 4c 20 6d 6f 64 65 2c 20 63 6c 6f 73 65 20 74  AL mode, close t
27aaa 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 49 66 20  he log file. If 
27aab 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20  successful, the 
27aac 63 61 6c 6c 0d 0a 20 20 20 20 20 20 20 20 2a 2a  call..        **
27aad 20 74 6f 20 50 61 67 65 72 43 6c 6f 73 65 57 61   to PagerCloseWa
27aae 6c 28 29 20 63 68 65 63 6b 70 6f 69 6e 74 73 20  l() checkpoints 
27aaf 61 6e 64 20 64 65 6c 65 74 65 73 20 74 68 65 20  and deletes the 
27ab0 77 72 69 74 65 2d 61 68 65 61 64 2d 6c 6f 67 0d  write-ahead-log.
27ab1 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65  .        ** file
27ab2 2e 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  . An EXCLUSIVE l
27ab3 6f 63 6b 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  ock may still be
27ab4 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74   held on the dat
27ab5 61 62 61 73 65 20 66 69 6c 65 0d 0a 20 20 20 20  abase file..    
27ab6 20 20 20 20 2a 2a 20 61 66 74 65 72 20 61 20 73      ** after a s
27ab7 75 63 63 65 73 73 66 75 6c 20 72 65 74 75 72 6e  uccessful return
27ab8 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
27ab9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
27aba 74 65 33 50 61 67 65 72 43 6c 6f 73 65 57 61 6c  te3PagerCloseWal
27abb 28 75 2e 63 69 2e 70 50 61 67 65 72 29 3b 0d 0a  (u.ci.pPager);..
27abc 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
27abd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
27abe 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
27abf 61 67 65 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f  agerSetJournalMo
27ac0 64 65 28 75 2e 63 69 2e 70 50 61 67 65 72 2c 20  de(u.ci.pPager, 
27ac1 75 2e 63 69 2e 65 4e 65 77 29 3b 0d 0a 20 20 20  u.ci.eNew);..   
27ac2 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65       }..      }e
27ac3 6c 73 65 20 69 66 28 20 75 2e 63 69 2e 65 4f 6c  lse if( u.ci.eOl
27ac4 64 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  d==PAGER_JOURNAL
27ac5 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0d 0a  MODE_MEMORY ){..
27ac6 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6e 6f          /* Canno
27ac7 74 20 74 72 61 6e 73 69 74 69 6f 6e 20 64 69 72  t transition dir
27ac8 65 63 74 6c 79 20 66 72 6f 6d 20 4d 45 4d 4f 52  ectly from MEMOR
27ac9 59 20 74 6f 20 57 41 4c 2e 20 20 55 73 65 20 6d  Y to WAL.  Use m
27aca 6f 64 65 20 4f 46 46 0d 0a 20 20 20 20 20 20 20  ode OFF..       
27acb 20 2a 2a 20 61 73 20 61 6e 20 69 6e 74 65 72 6d   ** as an interm
27acc 65 64 69 61 74 65 20 2a 2f 0d 0a 20 20 20 20 20  ediate */..     
27acd 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53     sqlite3PagerS
27ace 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 75 2e  etJournalMode(u.
27acf 63 69 2e 70 50 61 67 65 72 2c 20 50 41 47 45 52  ci.pPager, PAGER
27ad0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
27ad1 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  );..      }.... 
27ad2 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 74       /* Open a t
27ad3 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
27ad4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
27ad5 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   Regardless of t
27ad6 68 65 20 6a 6f 75 72 6e 61 6c 0d 0a 20 20 20 20  he journal..    
27ad7 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 69 73 20    ** mode, this 
27ad8 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 77 61  transaction alwa
27ad9 79 73 20 75 73 65 73 20 61 20 72 6f 6c 6c 62 61  ys uses a rollba
27ada 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0d 0a 20 20 20  ck journal...   
27adb 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73 73     */..      ass
27adc 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
27add 65 49 73 49 6e 54 72 61 6e 73 28 75 2e 63 69 2e  eIsInTrans(u.ci.
27ade 70 42 74 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  pBt)==0 );..    
27adf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
27ae0 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
27ae1 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
27ae2 65 53 65 74 56 65 72 73 69 6f 6e 28 75 2e 63 69  eSetVersion(u.ci
27ae3 2e 70 42 74 2c 20 28 75 2e 63 69 2e 65 4e 65 77  .pBt, (u.ci.eNew
27ae4 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
27ae5 4f 44 45 5f 57 41 4c 20 3f 20 32 20 3a 20 31 29  ODE_WAL ? 2 : 1)
27ae6 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
27ae7 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20   }..  }..#endif 
27ae8 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
27ae9 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f 0d 0a 0d 0a  _OMIT_WAL */....
27aea 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20    if( rc ){..   
27aeb 20 75 2e 63 69 2e 65 4e 65 77 20 3d 20 75 2e 63   u.ci.eNew = u.c
27aec 69 2e 65 4f 6c 64 3b 0d 0a 20 20 7d 0d 0a 20 20  i.eOld;..  }..  
27aed 75 2e 63 69 2e 65 4e 65 77 20 3d 20 73 71 6c 69  u.ci.eNew = sqli
27aee 74 65 33 50 61 67 65 72 53 65 74 4a 6f 75 72 6e  te3PagerSetJourn
27aef 61 6c 4d 6f 64 65 28 75 2e 63 69 2e 70 50 61 67  alMode(u.ci.pPag
27af0 65 72 2c 20 75 2e 63 69 2e 65 4e 65 77 29 3b 0d  er, u.ci.eNew);.
27af1 0a 0d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ...  pOut = &aMe
27af2 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0d 0a 20 20 70  m[pOp->p2];..  p
27af3 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
27af4 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c  _Str|MEM_Static|
27af5 4d 45 4d 5f 54 65 72 6d 3b 0d 0a 20 20 70 4f 75  MEM_Term;..  pOu
27af6 74 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 73  t->z = (char *)s
27af7 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4d 6f 64  qlite3JournalMod
27af8 65 6e 61 6d 65 28 75 2e 63 69 2e 65 4e 65 77 29  ename(u.ci.eNew)
27af9 3b 0d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 73  ;..  pOut->n = s
27afa 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
27afb 4f 75 74 2d 3e 7a 29 3b 0d 0a 20 20 70 4f 75 74  Out->z);..  pOut
27afc 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
27afd 54 46 38 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  TF8;..  sqlite3V
27afe 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
27aff 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67  g(pOut, encoding
27b00 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 3b  );..  break;..};
27b01 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
27b02 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 2a  TE_OMIT_PRAGMA *
27b03 2f 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65  /....#if !define
27b04 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  d(SQLITE_OMIT_VA
27b05 43 55 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65  CUUM) && !define
27b06 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54  d(SQLITE_OMIT_AT
27b07 54 41 43 48 29 0d 0a 2f 2a 20 4f 70 63 6f 64 65  TACH)../* Opcode
27b08 3a 20 56 61 63 75 75 6d 20 2a 20 2a 20 2a 20 2a  : Vacuum * * * *
27b09 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 56 61 63 75 75   *..**..** Vacuu
27b0a 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 74  m the entire dat
27b0b 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f 70 63  abase.  This opc
27b0c 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 6f  ode will cause o
27b0d 74 68 65 72 20 76 69 72 74 75 61 6c 0d 0a 2a 2a  ther virtual..**
27b0e 20 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65 20   machines to be 
27b0f 63 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e 2e  created and run.
27b10 20 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65 20    It may not be 
27b11 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
27b12 69 6e 0d 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63  in..** a transac
27b13 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  tion...*/..case 
27b14 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0d 0a 20 20  OP_Vacuum: {..  
27b15 72 63 20 3d 20 73 71 6c 69 74 65 33 52 75 6e 56  rc = sqlite3RunV
27b16 61 63 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73  acuum(&p->zErrMs
27b17 67 2c 20 64 62 29 3b 0d 0a 20 20 62 72 65 61 6b  g, db);..  break
27b18 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
27b19 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
27b1a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
27b1b 55 55 4d 29 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  UUM)../* Opcode:
27b1c 20 49 6e 63 72 56 61 63 75 75 6d 20 50 31 20 50   IncrVacuum P1 P
27b1d 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  2 * * *..**..** 
27b1e 50 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65  Perform a single
27b1f 20 73 74 65 70 20 6f 66 20 74 68 65 20 69 6e 63   step of the inc
27b20 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20  remental vacuum 
27b21 70 72 6f 63 65 64 75 72 65 20 6f 6e 0d 0a 2a 2a  procedure on..**
27b22 20 74 68 65 20 50 31 20 64 61 74 61 62 61 73 65   the P1 database
27b23 2e 20 49 66 20 74 68 65 20 76 61 63 75 75 6d 20  . If the vacuum 
27b24 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 6a 75  has finished, ju
27b25 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
27b26 6e 0d 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72 77  n..** P2. Otherw
27b27 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ise, fall throug
27b28 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
27b29 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  struction...*/..
27b2a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 63 75  case OP_IncrVacu
27b2b 75 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  um: {        /* 
27b2c 6a 75 6d 70 20 2a 2f 0d 0a 23 69 66 20 30 20 20  jump */..#if 0  
27b2d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
27b2e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
27b2f 63 6a 20 2a 2f 0d 0a 20 20 42 74 72 65 65 20 2a  cj */..  Btree *
27b30 70 42 74 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  pBt;..#endif /* 
27b31 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
27b32 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20  moved into u.cj 
27b33 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
27b34 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
27b35 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
27b36 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ..  assert( (p->
27b37 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79  btreeMask & (((y
27b38 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70 2d 3e  DbMask)1)<<pOp->
27b39 70 31 29 29 21 3d 30 20 29 3b 0d 0a 20 20 75 2e  p1))!=0 );..  u.
27b3a 63 6a 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  cj.pBt = db->aDb
27b3b 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0d 0a  [pOp->p1].pBt;..
27b3c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
27b3d 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75 2e  reeIncrVacuum(u.
27b3e 63 6a 2e 70 42 74 29 3b 0d 0a 20 20 69 66 28 20  cj.pBt);..  if( 
27b3f 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
27b40 29 7b 0d 0a 20 20 20 20 70 63 20 3d 20 70 4f 70  ){..    pc = pOp
27b41 2d 3e 70 32 20 2d 20 31 3b 0d 0a 20 20 20 20 72  ->p2 - 1;..    r
27b42 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
27b43 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
27b44 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 4f  ..#endif..../* O
27b45 70 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31  pcode: Expire P1
27b46 20 2a 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a   * * * *..**..**
27b47 20 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c   Cause precompil
27b48 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ed statements to
27b49 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e   become expired.
27b4a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74   An expired stat
27b4b 65 6d 65 6e 74 0d 0a 2a 2a 20 66 61 69 6c 73 20  ement..** fails 
27b4c 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f  with an error co
27b4d 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48  de of SQLITE_SCH
27b4e 45 4d 41 20 69 66 20 69 74 20 69 73 20 65 76 65  EMA if it is eve
27b4f 72 20 65 78 65 63 75 74 65 64 20 0d 0a 2a 2a 20  r executed ..** 
27b50 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73 74 65  (via sqlite3_ste
27b51 70 28 29 29 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49  p())...** ..** I
27b52 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20  f P1 is 0, then 
27b53 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  all SQL statemen
27b54 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65  ts become expire
27b55 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  d. If P1 is non-
27b56 7a 65 72 6f 2c 0d 0a 2a 2a 20 74 68 65 6e 20 6f  zero,..** then o
27b57 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c  nly the currentl
27b58 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74  y executing stat
27b59 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65  ement is affecte
27b5a 64 2e 20 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  d. ..*/..case OP
27b5b 5f 45 78 70 69 72 65 3a 20 7b 0d 0a 20 20 69 66  _Expire: {..  if
27b5c 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0d 0a 20  ( !pOp->p1 ){.. 
27b5d 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
27b5e 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
27b5f 74 73 28 64 62 29 3b 0d 0a 20 20 7d 65 6c 73 65  ts(db);..  }else
27b60 7b 0d 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65  {..    p->expire
27b61 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 62  d = 1;..  }..  b
27b62 72 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e  reak;..}....#ifn
27b63 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27b64 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 2f 2a  SHARED_CACHE../*
27b65 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c 65 4c 6f   Opcode: TableLo
27b66 63 6b 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  ck P1 P2 P3 P4 *
27b67 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 62 74 61 69 6e 20  ..**..** Obtain 
27b68 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72 74  a lock on a part
27b69 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54 68  icular table. Th
27b6a 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
27b6b 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e  s only used when
27b6c 0d 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d  ..** the shared-
27b6d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
27b6e 20 65 6e 61 62 6c 65 64 2e 20 0d 0a 2a 2a 0d 0a   enabled. ..**..
27b6f 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69 6e 64  ** P1 is the ind
27b70 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
27b71 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  se in sqlite3.aD
27b72 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62  b[] of the datab
27b73 61 73 65 0d 0a 2a 2a 20 6f 6e 20 77 68 69 63 68  ase..** on which
27b74 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 71   the lock is acq
27b75 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c 6f  uired.  A readlo
27b76 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69  ck is obtained i
27b77 66 20 50 33 3d 3d 30 20 6f 72 0d 0a 2a 2a 20 61  f P3==0 or..** a
27b78 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50   write lock if P
27b79 33 3d 3d 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 32  3==1...**..** P2
27b7a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f   contains the ro
27b7b 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74  ot-page of the t
27b7c 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0d 0a 2a  able to lock...*
27b7d 2a 0d 0a 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e  *..** P4 contain
27b7e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
27b7f 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
27b80 61 62 6c 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65  able being locke
27b81 64 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 0d  d. This is only.
27b82 0a 2a 2a 20 75 73 65 64 20 74 6f 20 67 65 6e 65  .** used to gene
27b83 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rate an error me
27b84 73 73 61 67 65 20 69 66 20 74 68 65 20 6c 6f 63  ssage if the loc
27b85 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61  k cannot be obta
27b86 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  ined...*/..case 
27b87 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 7b 0d  OP_TableLock: {.
27b88 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  .  u8 isWriteLoc
27b89 6b 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b  k = (u8)pOp->p3;
27b8a 0d 0a 20 20 69 66 28 20 69 73 57 72 69 74 65 4c  ..  if( isWriteL
27b8b 6f 63 6b 20 7c 7c 20 30 3d 3d 28 64 62 2d 3e 66  ock || 0==(db->f
27b8c 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64  lags&SQLITE_Read
27b8d 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 29 7b 0d  Uncommitted) ){.
27b8e 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 4f  .    int p1 = pO
27b8f 70 2d 3e 70 31 3b 20 0d 0a 20 20 20 20 61 73 73  p->p1; ..    ass
27b90 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31  ert( p1>=0 && p1
27b91 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 20  <db->nDb );..   
27b92 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
27b93 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62 4d  eeMask & (((yDbM
27b94 61 73 6b 29 31 29 3c 3c 70 31 29 29 21 3d 30 20  ask)1)<<p1))!=0 
27b95 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
27b96 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c  isWriteLock==0 |
27b97 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31  | isWriteLock==1
27b98 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   );..    rc = sq
27b99 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61  lite3BtreeLockTa
27b9a 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e  ble(db->aDb[p1].
27b9b 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73  pBt, pOp->p2, is
27b9c 57 72 69 74 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20  WriteLock);..   
27b9d 20 69 66 28 20 28 72 63 26 30 78 46 46 29 3d 3d   if( (rc&0xFF)==
27b9e 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b  SQLITE_LOCKED ){
27b9f 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ..      const ch
27ba0 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e  ar *z = pOp->p4.
27ba1 7a 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z;..      sqlite
27ba2 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
27ba3 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74  ErrMsg, db, "dat
27ba4 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c  abase table is l
27ba5 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0d  ocked: %s", z);.
27ba6 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 62  .    }..  }..  b
27ba7 72 65 61 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  reak;..}..#endif
27ba8 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
27ba9 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0d  SHARED_CACHE */.
27baa 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
27bab 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
27bac 42 4c 45 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  BLE../* Opcode: 
27bad 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20  VBegin * * * P4 
27bae 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34 20 6d 61 79  *..**..** P4 may
27baf 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
27bb0 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62   an sqlite3_vtab
27bb1 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73   structure. If s
27bb2 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0d 0a 2a 2a  o, call the ..**
27bb3 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66   xBegin method f
27bb4 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0d 0a  or that table...
27bb5 2a 2a 0d 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65  **..** Also, whe
27bb6 74 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69  ther or not P4 i
27bb7 73 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61  s set, check tha
27bb8 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65  t this is not be
27bb9 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0d  ing called from.
27bba 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c  .** within a cal
27bbb 6c 62 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75  lback to a virtu
27bbc 61 6c 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29  al table xSync()
27bbd 20 6d 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69   method. If it i
27bbe 73 2c 20 74 68 65 20 65 72 72 6f 72 0d 0a 2a 2a  s, the error..**
27bbf 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 73 65   code will be se
27bc0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  t to SQLITE_LOCK
27bc1 45 44 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  ED...*/..case OP
27bc2 5f 56 42 65 67 69 6e 3a 20 7b 0d 0a 23 69 66 20  _VBegin: {..#if 
27bc3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
27bc4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
27bc5 20 75 2e 63 6b 20 2a 2f 0d 0a 20 20 56 54 61 62   u.ck */..  VTab
27bc6 6c 65 20 2a 70 56 54 61 62 3b 0d 0a 23 65 6e 64  le *pVTab;..#end
27bc7 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
27bc8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
27bc9 20 75 2e 63 6b 20 2a 2f 0d 0a 20 20 75 2e 63 6b   u.ck */..  u.ck
27bca 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34  .pVTab = pOp->p4
27bcb 2e 70 56 74 61 62 3b 0d 0a 20 20 72 63 20 3d 20  .pVtab;..  rc = 
27bcc 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
27bcd 28 64 62 2c 20 75 2e 63 6b 2e 70 56 54 61 62 29  (db, u.ck.pVTab)
27bce 3b 0d 0a 20 20 69 66 28 20 75 2e 63 6b 2e 70 56  ;..  if( u.ck.pV
27bcf 54 61 62 20 29 20 69 6d 70 6f 72 74 56 74 61 62  Tab ) importVtab
27bd0 45 72 72 4d 73 67 28 70 2c 20 75 2e 63 6b 2e 70  ErrMsg(p, u.ck.p
27bd1 56 54 61 62 2d 3e 70 56 74 61 62 29 3b 0d 0a 20  VTab->pVtab);.. 
27bd2 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64   break;..}..#end
27bd3 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
27bd4 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
27bd5 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  /....#ifndef SQL
27bd6 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
27bd7 54 41 42 4c 45 0d 0a 2f 2a 20 4f 70 63 6f 64 65  TABLE../* Opcode
27bd8 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a 20 2a  : VCreate P1 * *
27bd9 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34   P4 *..**..** P4
27bda 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
27bdb 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
27bdc 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  in database P1. 
27bdd 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65  Call the xCreate
27bde 20 6d 65 74 68 6f 64 0d 0a 2a 2a 20 66 6f 72 20   method..** for 
27bdf 74 68 61 74 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  that table...*/.
27be0 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74 65  .case OP_VCreate
27be1 3a 20 7b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  : {..  rc = sqli
27be2 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74  te3VtabCallCreat
27be3 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  e(db, pOp->p1, p
27be4 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45  Op->p4.z, &p->zE
27be5 72 72 4d 73 67 29 3b 0d 0a 20 20 62 72 65 61 6b  rrMsg);..  break
27be6 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
27be7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
27be8 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 23  UALTABLE */....#
27be9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27bea 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d  IT_VIRTUALTABLE.
27beb 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 73  ./* Opcode: VDes
27bec 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 2a  troy P1 * * P4 *
27bed 0d 0a 2a 2a 0d 0a 2a 2a 20 50 34 20 69 73 20 74  ..**..** P4 is t
27bee 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72  he name of a vir
27bef 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61  tual table in da
27bf0 74 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c  tabase P1.  Call
27bf1 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65   the xDestroy me
27bf2 74 68 6f 64 0d 0a 2a 2a 20 6f 66 20 74 68 61 74  thod..** of that
27bf3 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 63 61 73   table...*/..cas
27bf4 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b  e OP_VDestroy: {
27bf5 0d 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74  ..  p->inVtabMet
27bf6 68 6f 64 20 3d 20 32 3b 0d 0a 20 20 72 63 20 3d  hod = 2;..  rc =
27bf7 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
27bf8 44 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d  Destroy(db, pOp-
27bf9 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  >p1, pOp->p4.z);
27bfa 0d 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74  ..  p->inVtabMet
27bfb 68 6f 64 20 3d 20 30 3b 0d 0a 20 20 62 72 65 61  hod = 0;..  brea
27bfc 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  k;..}..#endif /*
27bfd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
27bfe 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a  TUALTABLE */....
27bff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27c00 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
27c01 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70  ../* Opcode: VOp
27c02 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0d 0a  en P1 * * P4 *..
27c03 2a 2a 0d 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  **..** P4 is a p
27c04 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
27c05 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
27c06 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
27c07 62 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a  b structure...**
27c08 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20   P1 is a cursor 
27c09 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70  number.  This op
27c0a 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72  code opens a cur
27c0b 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75  sor to the virtu
27c0c 61 6c 0d 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64  al..** table and
27c0d 20 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72   stores that cur
27c0e 73 6f 72 20 69 6e 20 50 31 2e 0d 0a 2a 2f 0d 0a  sor in P1...*/..
27c0f 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b  case OP_VOpen: {
27c10 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  ..#if 0  /* loca
27c11 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
27c12 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0d 0a  d into u.cl */..
27c13 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
27c14 75 72 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  ur;..  sqlite3_v
27c15 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61  tab_cursor *pVta
27c16 62 43 75 72 73 6f 72 3b 0d 0a 20 20 73 71 6c 69  bCursor;..  sqli
27c17 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
27c18 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ..  sqlite3_modu
27c19 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0d 0a 23 65  le *pModule;..#e
27c1a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
27c1b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
27c1c 74 6f 20 75 2e 63 6c 20 2a 2f 0d 0a 0d 0a 20 20  to u.cl */....  
27c1d 75 2e 63 6c 2e 70 43 75 72 20 3d 20 30 3b 0d 0a  u.cl.pCur = 0;..
27c1e 20 20 75 2e 63 6c 2e 70 56 74 61 62 43 75 72 73    u.cl.pVtabCurs
27c1f 6f 72 20 3d 20 30 3b 0d 0a 20 20 75 2e 63 6c 2e  or = 0;..  u.cl.
27c20 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
27c21 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0d 0a 20  pVtab->pVtab;.. 
27c22 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.cl.pModule = 
27c23 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
27c24 2a 29 75 2e 63 6c 2e 70 56 74 61 62 2d 3e 70 4d  *)u.cl.pVtab->pM
27c25 6f 64 75 6c 65 3b 0d 0a 20 20 61 73 73 65 72 74  odule;..  assert
27c26 28 75 2e 63 6c 2e 70 56 74 61 62 20 26 26 20 75  (u.cl.pVtab && u
27c27 2e 63 6c 2e 70 4d 6f 64 75 6c 65 29 3b 0d 0a 20  .cl.pModule);.. 
27c28 20 72 63 20 3d 20 75 2e 63 6c 2e 70 4d 6f 64 75   rc = u.cl.pModu
27c29 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 6c 2e 70  le->xOpen(u.cl.p
27c2a 56 74 61 62 2c 20 26 75 2e 63 6c 2e 70 56 74 61  Vtab, &u.cl.pVta
27c2b 62 43 75 72 73 6f 72 29 3b 0d 0a 20 20 69 6d 70  bCursor);..  imp
27c2c 6f 72 74 56 74 61 62 45 72 72 4d 73 67 28 70 2c  ortVtabErrMsg(p,
27c2d 20 75 2e 63 6c 2e 70 56 74 61 62 29 3b 0d 0a 20   u.cl.pVtab);.. 
27c2e 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
27c2f 72 63 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 6e  rc ){..    /* In
27c30 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33  itialize sqlite3
27c31 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73  _vtab_cursor bas
27c32 65 20 63 6c 61 73 73 20 2a 2f 0d 0a 20 20 20 20  e class */..    
27c33 75 2e 63 6c 2e 70 56 74 61 62 43 75 72 73 6f 72  u.cl.pVtabCursor
27c34 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 6c 2e 70  ->pVtab = u.cl.p
27c35 56 74 61 62 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  Vtab;....    /* 
27c36 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20  Initialise vdbe 
27c37 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  cursor object */
27c38 0d 0a 20 20 20 20 75 2e 63 6c 2e 70 43 75 72 20  ..    u.cl.pCur 
27c39 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
27c3a 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20  (p, pOp->p1, 0, 
27c3b 2d 31 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28  -1, 0);..    if(
27c3c 20 75 2e 63 6c 2e 70 43 75 72 20 29 7b 0d 0a 20   u.cl.pCur ){.. 
27c3d 20 20 20 20 20 75 2e 63 6c 2e 70 43 75 72 2d 3e       u.cl.pCur->
27c3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75 2e  pVtabCursor = u.
27c3f 63 6c 2e 70 56 74 61 62 43 75 72 73 6f 72 3b 0d  cl.pVtabCursor;.
27c40 0a 20 20 20 20 20 20 75 2e 63 6c 2e 70 43 75 72  .      u.cl.pCur
27c41 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 6c  ->pModule = u.cl
27c42 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  .pVtabCursor->pV
27c43 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a 20  tab->pModule;.. 
27c44 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
27c45 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
27c46 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 75 2e  d = 1;..      u.
27c47 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f  cl.pModule->xClo
27c48 73 65 28 75 2e 63 6c 2e 70 56 74 61 62 43 75 72  se(u.cl.pVtabCur
27c49 73 6f 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  sor);..    }..  
27c4a 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d 0d 0a  }..  break;..}..
27c4b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
27c4c 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
27c4d 4c 45 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66  LE */....#ifndef
27c4e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
27c4f 54 55 41 4c 54 41 42 4c 45 0d 0a 2f 2a 20 4f 70  TUALTABLE../* Op
27c50 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31  code: VFilter P1
27c51 20 50 32 20 50 33 20 50 34 20 2a 0d 0a 2a 2a 0d   P2 P3 P4 *..**.
27c52 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73  .** P1 is a curs
27c53 6f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  or opened using 
27c54 56 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e  VOpen.  P2 is an
27c55 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70   address to jump
27c56 20 74 6f 20 69 66 0d 0a 2a 2a 20 74 68 65 20 66   to if..** the f
27c57 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74 20 73  iltered result s
27c58 65 74 20 69 73 20 65 6d 70 74 79 2e 0d 0a 2a 2a  et is empty...**
27c59 0d 0a 2a 2a 20 50 34 20 69 73 20 65 69 74 68 65  ..** P4 is eithe
27c5a 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69  r NULL or a stri
27c5b 6e 67 20 74 68 61 74 20 77 61 73 20 67 65 6e 65  ng that was gene
27c5c 72 61 74 65 64 20 62 79 20 74 68 65 20 78 42 65  rated by the xBe
27c5d 73 74 49 6e 64 65 78 0d 0a 2a 2a 20 6d 65 74 68  stIndex..** meth
27c5e 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  od of the module
27c5f 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 65 74  .  The interpret
27c60 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34 20  ation of the P4 
27c61 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0d 0a  string is left..
27c62 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  ** to the module
27c63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
27c64 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 70  ..**..** This op
27c65 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65  code invokes the
27c66 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
27c67 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  on the virtual t
27c68 61 62 6c 65 20 73 70 65 63 69 66 69 65 64 0d 0a  able specified..
27c69 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65 20 69  ** by P1.  The i
27c6a 6e 74 65 67 65 72 20 71 75 65 72 79 20 70 6c 61  nteger query pla
27c6b 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78  n parameter to x
27c6c 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72 65 64  Filter is stored
27c6d 20 69 6e 20 72 65 67 69 73 74 65 72 0d 0a 2a 2a   in register..**
27c6e 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50 33   P3. Register P3
27c6f 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61 72  +1 stores the ar
27c70 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gc parameter to 
27c71 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  be passed to the
27c72 0d 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74  ..** xFilter met
27c73 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50  hod. Registers P
27c74 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61  3+2..P3+1+argc a
27c75 72 65 20 74 68 65 20 61 72 67 63 0d 0a 2a 2a 20  re the argc..** 
27c76 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d  additional param
27c77 65 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20  eters which are 
27c78 70 61 73 73 65 64 20 74 6f 0d 0a 2a 2a 20 78 46  passed to..** xF
27c79 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e 20 52  ilter as argv. R
27c7a 65 67 69 73 74 65 72 20 50 33 2b 32 20 62 65 63  egister P3+2 bec
27c7b 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65  omes argv[0] whe
27c7c 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c  n passed to xFil
27c7d 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 6a  ter...**..** A j
27c7e 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50  ump is made to P
27c7f 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  2 if the result 
27c80 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 72  set after filter
27c81 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70  ing would be emp
27c82 74 79 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50  ty...*/..case OP
27c83 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a  _VFilter: {   /*
27c84 20 6a 75 6d 70 20 2a 2f 0d 0a 23 69 66 20 30 20   jump */..#if 0 
27c85 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
27c86 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
27c87 2e 63 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41  .cm */..  int nA
27c88 72 67 3b 0d 0a 20 20 69 6e 74 20 69 51 75 65 72  rg;..  int iQuer
27c89 79 3b 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  y;..  const sqli
27c8a 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
27c8b 75 6c 65 3b 0d 0a 20 20 4d 65 6d 20 2a 70 51 75  ule;..  Mem *pQu
27c8c 65 72 79 3b 0d 0a 20 20 4d 65 6d 20 2a 70 41 72  ery;..  Mem *pAr
27c8d 67 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  gc;..  sqlite3_v
27c8e 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61  tab_cursor *pVta
27c8f 62 43 75 72 73 6f 72 3b 0d 0a 20 20 73 71 6c 69  bCursor;..  sqli
27c90 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
27c91 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ..  VdbeCursor *
27c92 70 43 75 72 3b 0d 0a 20 20 69 6e 74 20 72 65 73  pCur;..  int res
27c93 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 4d  ;..  int i;..  M
27c94 65 6d 20 2a 2a 61 70 41 72 67 3b 0d 0a 23 65 6e  em **apArg;..#en
27c95 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
27c96 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
27c97 6f 20 75 2e 63 6d 20 2a 2f 0d 0a 0d 0a 20 20 75  o u.cm */....  u
27c98 2e 63 6d 2e 70 51 75 65 72 79 20 3d 20 26 61 4d  .cm.pQuery = &aM
27c99 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20  em[pOp->p3];..  
27c9a 75 2e 63 6d 2e 70 41 72 67 63 20 3d 20 26 75 2e  u.cm.pArgc = &u.
27c9b 63 6d 2e 70 51 75 65 72 79 5b 31 5d 3b 0d 0a 20  cm.pQuery[1];.. 
27c9c 20 75 2e 63 6d 2e 70 43 75 72 20 3d 20 70 2d 3e   u.cm.pCur = p->
27c9d 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  apCsr[pOp->p1];.
27c9e 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73  .  assert( memIs
27c9f 56 61 6c 69 64 28 75 2e 63 6d 2e 70 51 75 65 72  Valid(u.cm.pQuer
27ca0 79 29 20 29 3b 0d 0a 20 20 52 45 47 49 53 54 45  y) );..  REGISTE
27ca1 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
27ca2 20 75 2e 63 6d 2e 70 51 75 65 72 79 29 3b 0d 0a   u.cm.pQuery);..
27ca3 20 20 61 73 73 65 72 74 28 20 75 2e 63 6d 2e 70    assert( u.cm.p
27ca4 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
27ca5 20 29 3b 0d 0a 20 20 75 2e 63 6d 2e 70 56 74 61   );..  u.cm.pVta
27ca6 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 6d 2e 70  bCursor = u.cm.p
27ca7 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
27ca8 3b 0d 0a 20 20 75 2e 63 6d 2e 70 56 74 61 62 20  ;..  u.cm.pVtab 
27ca9 3d 20 75 2e 63 6d 2e 70 56 74 61 62 43 75 72 73  = u.cm.pVtabCurs
27caa 6f 72 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 75 2e  or->pVtab;..  u.
27cab 63 6d 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63  cm.pModule = u.c
27cac 6d 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  m.pVtab->pModule
27cad 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 72 61 62 20 74  ;....  /* Grab t
27cae 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20  he index number 
27caf 61 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65 74  and argc paramet
27cb0 65 72 73 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ers */..  assert
27cb1 28 20 28 75 2e 63 6d 2e 70 51 75 65 72 79 2d 3e  ( (u.cm.pQuery->
27cb2 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d  flags&MEM_Int)!=
27cb3 30 20 26 26 20 75 2e 63 6d 2e 70 41 72 67 63 2d  0 && u.cm.pArgc-
27cb4 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20  >flags==MEM_Int 
27cb5 29 3b 0d 0a 20 20 75 2e 63 6d 2e 6e 41 72 67 20  );..  u.cm.nArg 
27cb6 3d 20 28 69 6e 74 29 75 2e 63 6d 2e 70 41 72 67  = (int)u.cm.pArg
27cb7 63 2d 3e 75 2e 69 3b 0d 0a 20 20 75 2e 63 6d 2e  c->u.i;..  u.cm.
27cb8 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e  iQuery = (int)u.
27cb9 63 6d 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0d  cm.pQuery->u.i;.
27cba 0a 0d 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ...  /* Invoke t
27cbb 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f  he xFilter metho
27cbc 64 20 2a 2f 0d 0a 20 20 7b 0d 0a 20 20 20 20 75  d */..  {..    u
27cbd 2e 63 6d 2e 72 65 73 20 3d 20 30 3b 0d 0a 20 20  .cm.res = 0;..  
27cbe 20 20 75 2e 63 6d 2e 61 70 41 72 67 20 3d 20 70    u.cm.apArg = p
27cbf 2d 3e 61 70 41 72 67 3b 0d 0a 20 20 20 20 66 6f  ->apArg;..    fo
27cc0 72 28 75 2e 63 6d 2e 69 20 3d 20 30 3b 20 75 2e  r(u.cm.i = 0; u.
27cc1 63 6d 2e 69 3c 75 2e 63 6d 2e 6e 41 72 67 3b 20  cm.i<u.cm.nArg; 
27cc2 75 2e 63 6d 2e 69 2b 2b 29 7b 0d 0a 20 20 20 20  u.cm.i++){..    
27cc3 20 20 75 2e 63 6d 2e 61 70 41 72 67 5b 75 2e 63    u.cm.apArg[u.c
27cc4 6d 2e 69 5d 20 3d 20 26 75 2e 63 6d 2e 70 41 72  m.i] = &u.cm.pAr
27cc5 67 63 5b 75 2e 63 6d 2e 69 2b 31 5d 3b 0d 0a 20  gc[u.cm.i+1];.. 
27cc6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27cc7 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e 63  MemStoreType(u.c
27cc8 6d 2e 61 70 41 72 67 5b 75 2e 63 6d 2e 69 5d 29  m.apArg[u.cm.i])
27cc9 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
27cca 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
27ccb 3d 20 31 3b 0d 0a 20 20 20 20 72 63 20 3d 20 75  = 1;..    rc = u
27ccc 2e 63 6d 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69  .cm.pModule->xFi
27ccd 6c 74 65 72 28 75 2e 63 6d 2e 70 56 74 61 62 43  lter(u.cm.pVtabC
27cce 75 72 73 6f 72 2c 20 75 2e 63 6d 2e 69 51 75 65  ursor, u.cm.iQue
27ccf 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75  ry, pOp->p4.z, u
27cd0 2e 63 6d 2e 6e 41 72 67 2c 20 75 2e 63 6d 2e 61  .cm.nArg, u.cm.a
27cd1 70 41 72 67 29 3b 0d 0a 20 20 20 20 70 2d 3e 69  pArg);..    p->i
27cd2 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b  nVtabMethod = 0;
27cd3 0d 0a 20 20 20 20 69 6d 70 6f 72 74 56 74 61 62  ..    importVtab
27cd4 45 72 72 4d 73 67 28 70 2c 20 75 2e 63 6d 2e 70  ErrMsg(p, u.cm.p
27cd5 56 74 61 62 29 3b 0d 0a 20 20 20 20 69 66 28 20  Vtab);..    if( 
27cd6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
27cd7 0d 0a 20 20 20 20 20 20 75 2e 63 6d 2e 72 65 73  ..      u.cm.res
27cd8 20 3d 20 75 2e 63 6d 2e 70 4d 6f 64 75 6c 65 2d   = u.cm.pModule-
27cd9 3e 78 45 6f 66 28 75 2e 63 6d 2e 70 56 74 61 62  >xEof(u.cm.pVtab
27cda 43 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20 7d 0d  Cursor);..    }.
27cdb 0a 0d 0a 20 20 20 20 69 66 28 20 75 2e 63 6d 2e  ...    if( u.cm.
27cdc 72 65 73 20 29 7b 0d 0a 20 20 20 20 20 20 70 63  res ){..      pc
27cdd 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0d   = pOp->p2 - 1;.
27cde 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 75  .    }..  }..  u
27cdf 2e 63 6d 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f  .cm.pCur->nullRo
27ce0 77 20 3d 20 30 3b 0d 0a 0d 0a 20 20 62 72 65 61  w = 0;....  brea
27ce1 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  k;..}..#endif /*
27ce2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
27ce3 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a  TUALTABLE */....
27ce4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27ce5 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
27ce6 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f  ../* Opcode: VCo
27ce7 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20  lumn P1 P2 P3 * 
27ce8 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 53 74 6f 72 65 20  *..**..** Store 
27ce9 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
27cea 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66   P2-th column of
27ceb 0d 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20  ..** the row of 
27cec 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
27ced 65 20 74 68 61 74 20 74 68 65 20 0d 0a 2a 2a 20  e that the ..** 
27cee 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  P1 cursor is poi
27cef 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65  nting to into re
27cf0 67 69 73 74 65 72 20 50 33 2e 0d 0a 2a 2f 0d 0a  gister P3...*/..
27cf1 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a  case OP_VColumn:
27cf2 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   {..#if 0  /* lo
27cf3 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
27cf4 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f  ved into u.cn */
27cf5 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ..  sqlite3_vtab
27cf6 20 2a 70 56 74 61 62 3b 0d 0a 20 20 63 6f 6e 73   *pVtab;..  cons
27cf7 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
27cf8 20 2a 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 4d 65   *pModule;..  Me
27cf9 6d 20 2a 70 44 65 73 74 3b 0d 0a 20 20 73 71 6c  m *pDest;..  sql
27cfa 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f  ite3_context sCo
27cfb 6e 74 65 78 74 3b 0d 0a 23 65 6e 64 69 66 20 2f  ntext;..#endif /
27cfc 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
27cfd 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
27cfe 6e 20 2a 2f 0d 0a 0d 0a 20 20 56 64 62 65 43 75  n */....  VdbeCu
27cff 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
27d00 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d  apCsr[pOp->p1];.
27d01 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
27d02 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0d  >pVtabCursor );.
27d03 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
27d04 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
27d05 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0d 0a 20 20 75  =p->nMem );..  u
27d06 2e 63 6e 2e 70 44 65 73 74 20 3d 20 26 61 4d 65  .cn.pDest = &aMe
27d07 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a 20 20 6d  m[pOp->p3];..  m
27d08 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28  emAboutToChange(
27d09 70 2c 20 75 2e 63 6e 2e 70 44 65 73 74 29 3b 0d  p, u.cn.pDest);.
27d0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c  .  if( pCur->nul
27d0b 6c 52 6f 77 20 29 7b 0d 0a 20 20 20 20 73 71 6c  lRow ){..    sql
27d0c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
27d0d 6c 6c 28 75 2e 63 6e 2e 70 44 65 73 74 29 3b 0d  ll(u.cn.pDest);.
27d0e 0a 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d  .    break;..  }
27d0f 0d 0a 20 20 75 2e 63 6e 2e 70 56 74 61 62 20 3d  ..  u.cn.pVtab =
27d10 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73   pCur->pVtabCurs
27d11 6f 72 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 75 2e  or->pVtab;..  u.
27d12 63 6e 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63  cn.pModule = u.c
27d13 6e 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  n.pVtab->pModule
27d14 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  ;..  assert( u.c
27d15 6e 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75  n.pModule->xColu
27d16 6d 6e 20 29 3b 0d 0a 20 20 6d 65 6d 73 65 74 28  mn );..  memset(
27d17 26 75 2e 63 6e 2e 73 43 6f 6e 74 65 78 74 2c 20  &u.cn.sContext, 
27d18 30 2c 20 73 69 7a 65 6f 66 28 75 2e 63 6e 2e 73  0, sizeof(u.cn.s
27d19 43 6f 6e 74 65 78 74 29 29 3b 0d 0a 0d 0a 20 20  Context));....  
27d1a 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65  /* The output ce
27d1b 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68  ll may already h
27d1c 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c  ave a buffer all
27d1d 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0d 0a 20 20  ocated. Move..  
27d1e 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ** the current c
27d1f 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 6e 2e  ontents to u.cn.
27d20 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e  sContext.s so in
27d21 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
27d22 75 6e 63 74 69 6f 6e 0d 0a 20 20 2a 2a 20 63 61  unction..  ** ca
27d23 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64  n use the alread
27d24 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  y allocated buff
27d25 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c  er instead of al
27d26 6c 6f 63 61 74 69 6e 67 20 61 0d 0a 20 20 2a 2a  locating a..  **
27d27 20 6e 65 77 20 6f 6e 65 2e 0d 0a 20 20 2a 2f 0d   new one...  */.
27d28 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
27d29 6d 4d 6f 76 65 28 26 75 2e 63 6e 2e 73 43 6f 6e  mMove(&u.cn.sCon
27d2a 74 65 78 74 2e 73 2c 20 75 2e 63 6e 2e 70 44 65  text.s, u.cn.pDe
27d2b 73 74 29 3b 0d 0a 20 20 4d 65 6d 53 65 74 54 79  st);..  MemSetTy
27d2c 70 65 46 6c 61 67 28 26 75 2e 63 6e 2e 73 43 6f  peFlag(&u.cn.sCo
27d2d 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c  ntext.s, MEM_Nul
27d2e 6c 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 75 2e  l);....  rc = u.
27d2f 63 6e 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c  cn.pModule->xCol
27d30 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43  umn(pCur->pVtabC
27d31 75 72 73 6f 72 2c 20 26 75 2e 63 6e 2e 73 43 6f  ursor, &u.cn.sCo
27d32 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b  ntext, pOp->p2);
27d33 0d 0a 20 20 69 6d 70 6f 72 74 56 74 61 62 45 72  ..  importVtabEr
27d34 72 4d 73 67 28 70 2c 20 75 2e 63 6e 2e 70 56 74  rMsg(p, u.cn.pVt
27d35 61 62 29 3b 0d 0a 20 20 69 66 28 20 75 2e 63 6e  ab);..  if( u.cn
27d36 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f  .sContext.isErro
27d37 72 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 75  r ){..    rc = u
27d38 2e 63 6e 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45  .cn.sContext.isE
27d39 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rror;..  }....  
27d3a 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75  /* Copy the resu
27d3b 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  lt of the functi
27d3c 6f 6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 67  on to the P3 reg
27d3d 69 73 74 65 72 2e 20 57 65 0d 0a 20 20 2a 2a 20  ister. We..  ** 
27d3e 64 6f 20 74 68 69 73 20 72 65 67 61 72 64 6c 65  do this regardle
27d3f 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
27d40 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63   not an error oc
27d41 63 75 72 72 65 64 20 74 6f 20 65 6e 73 75 72 65  curred to ensure
27d42 20 61 6e 79 0d 0a 20 20 2a 2a 20 64 79 6e 61 6d   any..  ** dynam
27d43 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  ic allocation in
27d44 20 75 2e 63 6e 2e 73 43 6f 6e 74 65 78 74 2e 73   u.cn.sContext.s
27d45 20 28 61 20 4d 65 6d 20 73 74 72 75 63 74 29 20   (a Mem struct) 
27d46 69 73 20 20 72 65 6c 65 61 73 65 64 2e 0d 0a 20  is  released... 
27d47 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 56 64   */..  sqlite3Vd
27d48 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
27d49 28 26 75 2e 63 6e 2e 73 43 6f 6e 74 65 78 74 2e  (&u.cn.sContext.
27d4a 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0d 0a 20  s, encoding);.. 
27d4b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
27d4c 6f 76 65 28 75 2e 63 6e 2e 70 44 65 73 74 2c 20  ove(u.cn.pDest, 
27d4d 26 75 2e 63 6e 2e 73 43 6f 6e 74 65 78 74 2e 73  &u.cn.sContext.s
27d4e 29 3b 0d 0a 20 20 52 45 47 49 53 54 45 52 5f 54  );..  REGISTER_T
27d4f 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
27d50 63 6e 2e 70 44 65 73 74 29 3b 0d 0a 20 20 55 50  cn.pDest);..  UP
27d51 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
27d52 45 28 75 2e 63 6e 2e 70 44 65 73 74 29 3b 0d 0a  E(u.cn.pDest);..
27d53 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  ..  if( sqlite3V
27d54 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63  dbeMemTooBig(u.c
27d55 6e 2e 70 44 65 73 74 29 20 29 7b 0d 0a 20 20 20  n.pDest) ){..   
27d56 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0d 0a   goto too_big;..
27d57 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
27d58 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
27d59 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
27d5a 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64  ABLE */....#ifnd
27d5b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
27d5c 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 2f 2a 20  IRTUALTABLE../* 
27d5d 4f 70 63 6f 64 65 3a 20 56 4e 65 78 74 20 50 31  Opcode: VNext P1
27d5e 20 50 32 20 2a 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a   P2 * * *..**..*
27d5f 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74 75 61  * Advance virtua
27d60 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20 74 68  l table P1 to th
27d61 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 69 74  e next row in it
27d62 73 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64  s result set and
27d63 0d 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e 73  ..** jump to ins
27d64 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f 72  truction P2.  Or
27d65 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c  , if the virtual
27d66 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 63 68   table has reach
27d67 65 64 0d 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f  ed..** the end o
27d68 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  f its result set
27d69 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f  , then fall thro
27d6a 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
27d6b 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a 2f  instruction...*/
27d6c 0d 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a  ..case OP_VNext:
27d6d 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0d   {   /* jump */.
27d6e 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
27d6f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
27d70 20 69 6e 74 6f 20 75 2e 63 6f 20 2a 2f 0d 0a 20   into u.co */.. 
27d71 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
27d72 56 74 61 62 3b 0d 0a 20 20 63 6f 6e 73 74 20 73  Vtab;..  const s
27d73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
27d74 4d 6f 64 75 6c 65 3b 0d 0a 20 20 69 6e 74 20 72  Module;..  int r
27d75 65 73 3b 0d 0a 20 20 56 64 62 65 43 75 72 73 6f  es;..  VdbeCurso
27d76 72 20 2a 70 43 75 72 3b 0d 0a 23 65 6e 64 69 66  r *pCur;..#endif
27d77 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
27d78 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
27d79 2e 63 6f 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 63 6f  .co */....  u.co
27d7a 2e 72 65 73 20 3d 20 30 3b 0d 0a 20 20 75 2e 63  .res = 0;..  u.c
27d7b 6f 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73  o.pCur = p->apCs
27d7c 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20 20 61  r[pOp->p1];..  a
27d7d 73 73 65 72 74 28 20 75 2e 63 6f 2e 70 43 75 72  ssert( u.co.pCur
27d7e 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b  ->pVtabCursor );
27d7f 0d 0a 20 20 69 66 28 20 75 2e 63 6f 2e 70 43 75  ..  if( u.co.pCu
27d80 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0d 0a 20  r->nullRow ){.. 
27d81 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a     break;..  }..
27d82 20 20 75 2e 63 6f 2e 70 56 74 61 62 20 3d 20 75    u.co.pVtab = u
27d83 2e 63 6f 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .co.pCur->pVtabC
27d84 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0d 0a 20  ursor->pVtab;.. 
27d85 20 75 2e 63 6f 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.co.pModule = 
27d86 75 2e 63 6f 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.co.pVtab->pMod
27d87 75 6c 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ule;..  assert( 
27d88 75 2e 63 6f 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e  u.co.pModule->xN
27d89 65 78 74 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  ext );....  /* I
27d8a 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28  nvoke the xNext(
27d8b 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  ) method of the 
27d8c 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73  module. There is
27d8d 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0d   no way for the.
27d8e 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67  .  ** underlying
27d8f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
27d90 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
27d91 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73  or if one occurs
27d92 20 64 75 72 69 6e 67 0d 0a 20 20 2a 2a 20 78 4e   during..  ** xN
27d93 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20  ext(). Instead, 
27d94 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
27d95 72 73 2c 20 74 72 75 65 20 69 73 20 72 65 74 75  rs, true is retu
27d96 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67  rned (indicating
27d97 20 74 68 61 74 0d 0a 20 20 2a 2a 20 64 61 74 61   that..  ** data
27d98 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61   is available) a
27d99 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  nd the error cod
27d9a 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20  e returned when 
27d9b 78 43 6f 6c 75 6d 6e 20 6f 72 0d 0a 20 20 2a 2a  xColumn or..  **
27d9c 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68   some other meth
27d9d 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b  od is next invok
27d9e 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76  ed on the save v
27d9f 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72  irtual table cur
27da0 73 6f 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 2d  sor...  */..  p-
27da1 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
27da2 31 3b 0d 0a 20 20 72 63 20 3d 20 75 2e 63 6f 2e  1;..  rc = u.co.
27da3 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75  pModule->xNext(u
27da4 2e 63 6f 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .co.pCur->pVtabC
27da5 75 72 73 6f 72 29 3b 0d 0a 20 20 70 2d 3e 69 6e  ursor);..  p->in
27da6 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0d  VtabMethod = 0;.
27da7 0a 20 20 69 6d 70 6f 72 74 56 74 61 62 45 72 72  .  importVtabErr
27da8 4d 73 67 28 70 2c 20 75 2e 63 6f 2e 70 56 74 61  Msg(p, u.co.pVta
27da9 62 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  b);..  if( rc==S
27daa 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
27dab 20 75 2e 63 6f 2e 72 65 73 20 3d 20 75 2e 63 6f   u.co.res = u.co
27dac 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75  .pModule->xEof(u
27dad 2e 63 6f 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .co.pCur->pVtabC
27dae 75 72 73 6f 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ursor);..  }....
27daf 20 20 69 66 28 20 21 75 2e 63 6f 2e 72 65 73 20    if( !u.co.res 
27db0 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ){..    /* If th
27db1 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d  ere is data, jum
27db2 70 20 74 6f 20 50 32 20 2a 2f 0d 0a 20 20 20 20  p to P2 */..    
27db3 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
27db4 3b 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b  ;..  }..  break;
27db5 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
27db6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
27db7 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 23 69  ALTABLE */....#i
27db8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27db9 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a  T_VIRTUALTABLE..
27dba 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61  /* Opcode: VRena
27dbb 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0d 0a  me P1 * * P4 *..
27dbc 2a 2a 0d 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  **..** P4 is a p
27dbd 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
27dbe 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
27dbf 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
27dc0 62 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a  b structure...**
27dc1 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
27dc2 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
27dc3 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d  onding xRename m
27dc4 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65  ethod. The value
27dc5 0d 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  ..** in register
27dc6 20 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73   P1 is passed as
27dc7 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d   the zName argum
27dc8 65 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61  ent to the xRena
27dc9 6d 65 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a  me method...*/..
27dca 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a  case OP_VRename:
27dcb 20 7b 0d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   {..#if 0  /* lo
27dcc 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
27dcd 76 65 64 20 69 6e 74 6f 20 75 2e 63 70 20 2a 2f  ved into u.cp */
27dce 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ..  sqlite3_vtab
27dcf 20 2a 70 56 74 61 62 3b 0d 0a 20 20 4d 65 6d 20   *pVtab;..  Mem 
27dd0 2a 70 4e 61 6d 65 3b 0d 0a 23 65 6e 64 69 66 20  *pName;..#endif 
27dd1 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
27dd2 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
27dd3 63 70 20 2a 2f 0d 0a 0d 0a 20 20 75 2e 63 70 2e  cp */....  u.cp.
27dd4 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
27dd5 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0d 0a 20  pVtab->pVtab;.. 
27dd6 20 75 2e 63 70 2e 70 4e 61 6d 65 20 3d 20 26 61   u.cp.pName = &a
27dd7 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0d 0a 20  Mem[pOp->p1];.. 
27dd8 20 61 73 73 65 72 74 28 20 75 2e 63 70 2e 70 56   assert( u.cp.pV
27dd9 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52  tab->pModule->xR
27dda 65 6e 61 6d 65 20 29 3b 0d 0a 20 20 61 73 73 65  ename );..  asse
27ddb 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75  rt( memIsValid(u
27ddc 2e 63 70 2e 70 4e 61 6d 65 29 20 29 3b 0d 0a 20  .cp.pName) );.. 
27ddd 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
27dde 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 70 2e 70 4e  pOp->p1, u.cp.pN
27ddf 61 6d 65 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  ame);..  assert(
27de0 20 75 2e 63 70 2e 70 4e 61 6d 65 2d 3e 66 6c 61   u.cp.pName->fla
27de1 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0d  gs & MEM_Str );.
27de2 0a 20 20 74 65 73 74 63 61 73 65 28 20 75 2e 63  .  testcase( u.c
27de3 70 2e 70 4e 61 6d 65 2d 3e 65 6e 63 3d 3d 53 51  p.pName->enc==SQ
27de4 4c 49 54 45 5f 55 54 46 38 20 29 3b 0d 0a 20 20  LITE_UTF8 );..  
27de5 74 65 73 74 63 61 73 65 28 20 75 2e 63 70 2e 70  testcase( u.cp.p
27de6 4e 61 6d 65 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Name->enc==SQLIT
27de7 45 5f 55 54 46 31 36 42 45 20 29 3b 0d 0a 20 20  E_UTF16BE );..  
27de8 74 65 73 74 63 61 73 65 28 20 75 2e 63 70 2e 70  testcase( u.cp.p
27de9 4e 61 6d 65 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Name->enc==SQLIT
27dea 45 5f 55 54 46 31 36 4c 45 20 29 3b 0d 0a 20 20  E_UTF16LE );..  
27deb 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
27dec 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75  ChangeEncoding(u
27ded 2e 63 70 2e 70 4e 61 6d 65 2c 20 53 51 4c 49 54  .cp.pName, SQLIT
27dee 45 5f 55 54 46 38 29 3b 0d 0a 20 20 69 66 28 20  E_UTF8);..  if( 
27def 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
27df0 0d 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 70 2e  ..    rc = u.cp.
27df1 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  pVtab->pModule->
27df2 78 52 65 6e 61 6d 65 28 75 2e 63 70 2e 70 56 74  xRename(u.cp.pVt
27df3 61 62 2c 20 75 2e 63 70 2e 70 4e 61 6d 65 2d 3e  ab, u.cp.pName->
27df4 7a 29 3b 0d 0a 20 20 20 20 69 6d 70 6f 72 74 56  z);..    importV
27df5 74 61 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 63  tabErrMsg(p, u.c
27df6 70 2e 70 56 74 61 62 29 3b 0d 0a 20 20 20 20 70  p.pVtab);..    p
27df7 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0d 0a  ->expired = 0;..
27df8 20 20 7d 0d 0a 20 20 62 72 65 61 6b 3b 0d 0a 7d    }..  break;..}
27df9 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e  ..#endif....#ifn
27dfa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27dfb 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 2f 2a  VIRTUALTABLE../*
27dfc 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65   Opcode: VUpdate
27dfd 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0d 0a   P1 P2 P3 P4 *..
27dfe 2a 2a 0d 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  **..** P4 is a p
27dff 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
27e00 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
27e01 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
27e02 62 20 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a  b structure...**
27e03 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
27e04 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
27e05 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d  onding xUpdate m
27e06 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73  ethod. P2 values
27e07 0d 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 75  ..** are contigu
27e08 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  ous memory cells
27e09 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 33 20   starting at P3 
27e0a 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 78  to pass to the x
27e0b 55 70 64 61 74 65 20 0d 0a 2a 2a 20 69 6e 76 6f  Update ..** invo
27e0c 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75  cation. The valu
27e0d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 50  e in register (P
27e0e 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f  3+P2-1) correspo
27e0f 6e 64 73 20 74 6f 20 74 68 65 20 0d 0a 2a 2a 20  nds to the ..** 
27e10 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20  p2th element of 
27e11 74 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70  the argv array p
27e12 61 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65  assed to xUpdate
27e13 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 78 55  ...**..** The xU
27e14 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69 6c  pdate method wil
27e15 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f 72  l do a DELETE or
27e16 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 6f   an INSERT or bo
27e17 74 68 2e 0d 0a 2a 2a 20 54 68 65 20 61 72 67 76  th...** The argv
27e18 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69  [0] element (whi
27e19 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ch corresponds t
27e1a 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33  o memory cell P3
27e1b 29 0d 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77  )..** is the row
27e1c 69 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64  id of a row to d
27e1d 65 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b  elete.  If argv[
27e1e 30 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  0] is NULL then 
27e1f 6e 6f 20 0d 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e  no ..** deletion
27e20 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 61 72   occurs.  The ar
27e21 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73  gv[1] element is
27e22 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
27e23 65 20 6e 65 77 20 0d 0a 2a 2a 20 72 6f 77 2e 20  e new ..** row. 
27e24 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e 55 4c   This can be NUL
27e25 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20 76 69  L to have the vi
27e26 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65 6c 65  rtual table sele
27e27 63 74 20 74 68 65 20 6e 65 77 20 0d 0a 2a 2a 20  ct the new ..** 
27e28 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c 66  rowid for itself
27e29 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e  .  The subsequen
27e2a 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  t elements in th
27e2b 65 20 61 72 72 61 79 20 61 72 65 20 0d 0a 2a 2a  e array are ..**
27e2c 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63   the values of c
27e2d 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65  olumns in the ne
27e2e 77 20 72 6f 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  w row...**..** I
27e2f 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20  f P2==1 then no 
27e30 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72  insert is perfor
27e31 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  med.  argv[0] is
27e32 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0d 0a 2a   the rowid of..*
27e33 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74  * a row to delet
27e34 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 31 20 69 73  e...**..** P1 is
27e35 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e   a boolean flag.
27e36 20 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f   If it is set to
27e37 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 78 55   true and the xU
27e38 70 64 61 74 65 20 63 61 6c 6c 0d 0a 2a 2a 20 69  pdate call..** i
27e39 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  s successful, th
27e3a 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  en the value ret
27e3b 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
27e3c 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
27e3d 69 64 28 29 20 0d 0a 2a 2a 20 69 73 20 73 65 74  id() ..** is set
27e3e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
27e3f 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74   the rowid for t
27e40 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65  he row just inse
27e41 72 74 65 64 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  rted...*/..case 
27e42 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0d 0a 23  OP_VUpdate: {..#
27e43 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
27e44 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
27e45 6e 74 6f 20 75 2e 63 71 20 2a 2f 0d 0a 20 20 73  nto u.cq */..  s
27e46 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
27e47 61 62 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ab;..  sqlite3_m
27e48 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0d  odule *pModule;.
27e49 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0d 0a 20 20  .  int nArg;..  
27e4a 69 6e 74 20 69 3b 0d 0a 20 20 73 71 6c 69 74 65  int i;..  sqlite
27e4b 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0d 0a 20  _int64 rowid;.. 
27e4c 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0d 0a 20   Mem **apArg;.. 
27e4d 20 4d 65 6d 20 2a 70 58 3b 0d 0a 23 65 6e 64 69   Mem *pX;..#endi
27e4e 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
27e4f 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
27e50 75 2e 63 71 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73  u.cq */....  ass
27e51 65 72 74 28 20 70 4f 70 2d 3e 70 32 3d 3d 31 20  ert( pOp->p2==1 
27e52 20 20 20 20 20 20 20 7c 7c 20 70 4f 70 2d 3e 70         || pOp->p
27e53 35 3d 3d 4f 45 5f 46 61 69 6c 20 20 20 7c 7c 20  5==OE_Fail   || 
27e54 70 4f 70 2d 3e 70 35 3d 3d 4f 45 5f 52 6f 6c 6c  pOp->p5==OE_Roll
27e55 62 61 63 6b 0d 0a 20 20 20 20 20 20 20 7c 7c 20  back..       || 
27e56 70 4f 70 2d 3e 70 35 3d 3d 4f 45 5f 41 62 6f 72  pOp->p5==OE_Abor
27e57 74 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 4f 45  t || pOp->p5==OE
27e58 5f 49 67 6e 6f 72 65 20 7c 7c 20 70 4f 70 2d 3e  _Ignore || pOp->
27e59 70 35 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 0d 0a  p5==OE_Replace..
27e5a 20 20 29 3b 0d 0a 20 20 75 2e 63 71 2e 70 56 74    );..  u.cq.pVt
27e5b 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74  ab = pOp->p4.pVt
27e5c 61 62 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 75 2e  ab->pVtab;..  u.
27e5d 63 71 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71  cq.pModule = (sq
27e5e 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75  lite3_module *)u
27e5f 2e 63 71 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .cq.pVtab->pModu
27e60 6c 65 3b 0d 0a 20 20 75 2e 63 71 2e 6e 41 72 67  le;..  u.cq.nArg
27e61 20 3d 20 70 4f 70 2d 3e 70 32 3b 0d 0a 20 20 61   = pOp->p2;..  a
27e62 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
27e63 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0d 0a  pe==P4_VTAB );..
27e64 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 63    if( ALWAYS(u.c
27e65 71 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61  q.pModule->xUpda
27e66 74 65 29 20 29 7b 0d 0a 20 20 20 20 75 38 20 76  te) ){..    u8 v
27e67 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 20 3d 20  tabOnConflict = 
27e68 64 62 2d 3e 76 74 61 62 4f 6e 43 6f 6e 66 6c 69  db->vtabOnConfli
27e69 63 74 3b 0d 0a 20 20 20 20 75 2e 63 71 2e 61 70  ct;..    u.cq.ap
27e6a 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0d  Arg = p->apArg;.
27e6b 0a 20 20 20 20 75 2e 63 71 2e 70 58 20 3d 20 26  .    u.cq.pX = &
27e6c 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0d 0a  aMem[pOp->p3];..
27e6d 20 20 20 20 66 6f 72 28 75 2e 63 71 2e 69 3d 30      for(u.cq.i=0
27e6e 3b 20 75 2e 63 71 2e 69 3c 75 2e 63 71 2e 6e 41  ; u.cq.i<u.cq.nA
27e6f 72 67 3b 20 75 2e 63 71 2e 69 2b 2b 29 7b 0d 0a  rg; u.cq.i++){..
27e70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65        assert( me
27e71 6d 49 73 56 61 6c 69 64 28 75 2e 63 71 2e 70 58  mIsValid(u.cq.pX
27e72 29 20 29 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 41  ) );..      memA
27e73 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20  boutToChange(p, 
27e74 75 2e 63 71 2e 70 58 29 3b 0d 0a 20 20 20 20 20  u.cq.pX);..     
27e75 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
27e76 74 6f 72 65 54 79 70 65 28 75 2e 63 71 2e 70 58  toreType(u.cq.pX
27e77 29 3b 0d 0a 20 20 20 20 20 20 75 2e 63 71 2e 61  );..      u.cq.a
27e78 70 41 72 67 5b 75 2e 63 71 2e 69 5d 20 3d 20 75  pArg[u.cq.i] = u
27e79 2e 63 71 2e 70 58 3b 0d 0a 20 20 20 20 20 20 75  .cq.pX;..      u
27e7a 2e 63 71 2e 70 58 2b 2b 3b 0d 0a 20 20 20 20 7d  .cq.pX++;..    }
27e7b 0d 0a 20 20 20 20 64 62 2d 3e 76 74 61 62 4f 6e  ..    db->vtabOn
27e7c 43 6f 6e 66 6c 69 63 74 20 3d 20 70 4f 70 2d 3e  Conflict = pOp->
27e7d 70 35 3b 0d 0a 20 20 20 20 72 63 20 3d 20 75 2e  p5;..    rc = u.
27e7e 63 71 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64  cq.pModule->xUpd
27e7f 61 74 65 28 75 2e 63 71 2e 70 56 74 61 62 2c 20  ate(u.cq.pVtab, 
27e80 75 2e 63 71 2e 6e 41 72 67 2c 20 75 2e 63 71 2e  u.cq.nArg, u.cq.
27e81 61 70 41 72 67 2c 20 26 75 2e 63 71 2e 72 6f 77  apArg, &u.cq.row
27e82 69 64 29 3b 0d 0a 20 20 20 20 64 62 2d 3e 76 74  id);..    db->vt
27e83 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 20 3d 20 76  abOnConflict = v
27e84 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 3b 0d 0a  tabOnConflict;..
27e85 20 20 20 20 69 6d 70 6f 72 74 56 74 61 62 45 72      importVtabEr
27e86 72 4d 73 67 28 70 2c 20 75 2e 63 71 2e 70 56 74  rMsg(p, u.cq.pVt
27e87 61 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  ab);..    if( rc
27e88 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
27e89 4f 70 2d 3e 70 31 20 29 7b 0d 0a 20 20 20 20 20  Op->p1 ){..     
27e8a 20 61 73 73 65 72 74 28 20 75 2e 63 71 2e 6e 41   assert( u.cq.nA
27e8b 72 67 3e 31 20 26 26 20 75 2e 63 71 2e 61 70 41  rg>1 && u.cq.apA
27e8c 72 67 5b 30 5d 20 26 26 20 28 75 2e 63 71 2e 61  rg[0] && (u.cq.a
27e8d 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d  pArg[0]->flags&M
27e8e 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0d 0a 20 20 20  EM_Null) );..   
27e8f 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64     db->lastRowid
27e90 20 3d 20 6c 61 73 74 52 6f 77 69 64 20 3d 20 75   = lastRowid = u
27e91 2e 63 71 2e 72 6f 77 69 64 3b 0d 0a 20 20 20 20  .cq.rowid;..    
27e92 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  }..    if( rc==S
27e93 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
27e94 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61   && pOp->p4.pVta
27e95 62 2d 3e 62 43 6f 6e 73 74 72 61 69 6e 74 20 29  b->bConstraint )
27e96 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  {..      if( pOp
27e97 2d 3e 70 35 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  ->p5==OE_Ignore 
27e98 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
27e99 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20   SQLITE_OK;..   
27e9a 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
27e9b 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f     p->errorActio
27e9c 6e 20 3d 20 28 28 70 4f 70 2d 3e 70 35 3d 3d 4f  n = ((pOp->p5==O
27e9d 45 5f 52 65 70 6c 61 63 65 29 20 3f 20 4f 45 5f  E_Replace) ? OE_
27e9e 41 62 6f 72 74 20 3a 20 70 4f 70 2d 3e 70 35 29  Abort : pOp->p5)
27e9f 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
27ea0 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 2d  }else{..      p-
27ea1 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0d 0a 20 20 20  >nChange++;..   
27ea2 20 7d 0d 0a 20 20 7d 0d 0a 20 20 62 72 65 61 6b   }..  }..  break
27ea3 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;..}..#endif /* 
27ea4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
27ea5 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 23  UALTABLE */....#
27ea6 69 66 6e 64 65 66 20 20 53 51 4c 49 54 45 5f 4f  ifndef  SQLITE_O
27ea7 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
27ea8 53 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61  S../* Opcode: Pa
27ea9 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20 2a 20  gecount P1 P2 * 
27eaa 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74  * *..**..** Writ
27eab 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75  e the current nu
27eac 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
27ead 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20   database P1 to 
27eae 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0d  memory cell P2..
27eaf 0a 2a 2f 0d 0a 63 61 73 65 20 4f 50 5f 50 61 67  .*/..case OP_Pag
27eb0 65 63 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20  ecount: {       
27eb1 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
27eb2 72 65 6c 65 61 73 65 20 2a 2f 0d 0a 20 20 70 4f  release */..  pO
27eb3 75 74 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65  ut->u.i = sqlite
27eb4 33 42 74 72 65 65 4c 61 73 74 50 61 67 65 28 64  3BtreeLastPage(d
27eb5 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e  b->aDb[pOp->p1].
27eb6 70 42 74 29 3b 0d 0a 20 20 62 72 65 61 6b 3b 0d  pBt);..  break;.
27eb7 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a  .}..#endif......
27eb8 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54 45 5f  #ifndef  SQLITE_
27eb9 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
27eba 41 53 0d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  AS../* Opcode: M
27ebb 61 78 50 67 63 6e 74 20 50 31 20 50 32 20 50 33  axPgcnt P1 P2 P3
27ebc 20 2a 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 72 79   * *..**..** Try
27ebd 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69   to set the maxi
27ebe 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66  mum page count f
27ebf 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 74  or database P1 t
27ec0 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 50  o the value in P
27ec1 33 2e 0d 0a 2a 2a 20 44 6f 20 6e 6f 74 20 6c 65  3...** Do not le
27ec2 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61  t the maximum pa
27ec3 67 65 20 63 6f 75 6e 74 20 66 61 6c 6c 20 62 65  ge count fall be
27ec4 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20  low the current 
27ec5 70 61 67 65 20 63 6f 75 6e 74 20 61 6e 64 0d 0a  page count and..
27ec6 2a 2a 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65  ** do not change
27ec7 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
27ec8 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 69 66  e count value if
27ec9 20 50 33 3d 3d 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   P3==0...**..** 
27eca 53 74 6f 72 65 20 74 68 65 20 6d 61 78 69 6d 75  Store the maximu
27ecb 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 61 66 74  m page count aft
27ecc 65 72 20 74 68 65 20 63 68 61 6e 67 65 20 69 6e  er the change in
27ecd 20 72 65 67 69 73 74 65 72 20 50 32 2e 0d 0a 2a   register P2...*
27ece 2f 0d 0a 63 61 73 65 20 4f 50 5f 4d 61 78 50 67  /..case OP_MaxPg
27ecf 63 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  cnt: {          
27ed0 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
27ed1 65 61 73 65 20 2a 2f 0d 0a 20 20 75 6e 73 69 67  ease */..  unsig
27ed2 6e 65 64 20 69 6e 74 20 6e 65 77 4d 61 78 3b 0d  ned int newMax;.
27ed3 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0d 0a  .  Btree *pBt;..
27ed4 0d 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44  ..  pBt = db->aD
27ed5 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0d  b[pOp->p1].pBt;.
27ed6 0a 20 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0d 0a  .  newMax = 0;..
27ed7 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b    if( pOp->p3 ){
27ed8 0d 0a 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73  ..    newMax = s
27ed9 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 50  qlite3BtreeLastP
27eda 61 67 65 28 70 42 74 29 3b 0d 0a 20 20 20 20 69  age(pBt);..    i
27edb 66 28 20 6e 65 77 4d 61 78 20 3c 20 28 75 6e 73  f( newMax < (uns
27edc 69 67 6e 65 64 29 70 4f 70 2d 3e 70 33 20 29 20  igned)pOp->p3 ) 
27edd 6e 65 77 4d 61 78 20 3d 20 28 75 6e 73 69 67 6e  newMax = (unsign
27ede 65 64 29 70 4f 70 2d 3e 70 33 3b 0d 0a 20 20 7d  ed)pOp->p3;..  }
27edf 0d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ..  pOut->u.i = 
27ee0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50  sqlite3BtreeMaxP
27ee1 61 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65  ageCount(pBt, ne
27ee2 77 4d 61 78 29 3b 0d 0a 20 20 62 72 65 61 6b 3b  wMax);..  break;
27ee3 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  ..}..#endif.....
27ee4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27ee5 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 2f 2a 20 4f  OMIT_TRACE../* O
27ee6 70 63 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 2a  pcode: Trace * *
27ee7 20 2a 20 50 34 20 2a 0d 0a 2a 2a 0d 0a 2a 2a 20   * P4 *..**..** 
27ee8 49 66 20 74 72 61 63 69 6e 67 20 69 73 20 65 6e  If tracing is en
27ee9 61 62 6c 65 64 20 28 62 79 20 74 68 65 20 73 71  abled (by the sq
27eea 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29 20 69  lite3_trace()) i
27eeb 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e 0d 0a  nterface, then..
27eec 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74 72  ** the UTF-8 str
27eed 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  ing contained in
27eee 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 20 6f   P4 is emitted o
27eef 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 6c 6c  n the trace call
27ef0 62 61 63 6b 2e 0d 0a 2a 2f 0d 0a 63 61 73 65 20  back...*/..case 
27ef1 4f 50 5f 54 72 61 63 65 3a 20 7b 0d 0a 23 69 66  OP_Trace: {..#if
27ef2 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
27ef3 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
27ef4 6f 20 75 2e 63 72 20 2a 2f 0d 0a 20 20 63 68 61  o u.cr */..  cha
27ef5 72 20 2a 7a 54 72 61 63 65 3b 0d 0a 20 20 63 68  r *zTrace;..  ch
27ef6 61 72 20 2a 7a 3b 0d 0a 23 65 6e 64 69 66 20 2f  ar *z;..#endif /
27ef7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
27ef8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
27ef9 72 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 64 62  r */....  if( db
27efa 2d 3e 78 54 72 61 63 65 20 26 26 20 28 75 2e 63  ->xTrace && (u.c
27efb 72 2e 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 2d  r.zTrace = (pOp-
27efc 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e  >p4.z ? pOp->p4.
27efd 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 29 21 3d 30  z : p->zSql))!=0
27efe 20 29 7b 0d 0a 20 20 20 20 75 2e 63 72 2e 7a 20   ){..    u.cr.z 
27eff 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 70  = sqlite3VdbeExp
27f00 61 6e 64 53 71 6c 28 70 2c 20 75 2e 63 72 2e 7a  andSql(p, u.cr.z
27f01 54 72 61 63 65 29 3b 0d 0a 20 20 20 20 64 62 2d  Trace);..    db-
27f02 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61  >xTrace(db->pTra
27f03 63 65 41 72 67 2c 20 75 2e 63 72 2e 7a 29 3b 0d  ceArg, u.cr.z);.
27f04 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
27f05 65 65 28 64 62 2c 20 75 2e 63 72 2e 7a 29 3b 0d  ee(db, u.cr.z);.
27f06 0a 20 20 7d 0d 0a 23 69 66 64 65 66 20 53 51 4c  .  }..#ifdef SQL
27f07 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 69 66 28  ITE_DEBUG..  if(
27f08 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
27f09 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d  LITE_SqlTrace)!=
27f0a 30 0d 0a 20 20 20 26 26 20 28 75 2e 63 72 2e 7a  0..   && (u.cr.z
27f0b 54 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34  Trace = (pOp->p4
27f0c 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a  .z ? pOp->p4.z :
27f0d 20 70 2d 3e 7a 53 71 6c 29 29 21 3d 30 0d 0a 20   p->zSql))!=0.. 
27f0e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
27f0f 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c  DebugPrintf("SQL
27f10 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75  -trace: %s\n", u
27f11 2e 63 72 2e 7a 54 72 61 63 65 29 3b 0d 0a 20 20  .cr.zTrace);..  
27f12 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
27f13 49 54 45 5f 44 45 42 55 47 20 2a 2f 0d 0a 20 20  ITE_DEBUG */..  
27f14 62 72 65 61 6b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  break;..}..#endi
27f15 66 0d 0a 0d 0a 0d 0a 2f 2a 20 4f 70 63 6f 64 65  f....../* Opcode
27f16 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a  : Noop * * * * *
27f17 0d 0a 2a 2a 0d 0a 2a 2a 20 44 6f 20 6e 6f 74 68  ..**..** Do noth
27f18 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74 72  ing.  This instr
27f19 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e 20  uction is often 
27f1a 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d 70  useful as a jump
27f1b 0d 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e  ..** destination
27f1c 2e 0d 0a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ...*/../*..** Th
27f1d 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20  e magic Explain 
27f1e 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20  opcode are only 
27f1f 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 65 78  inserted when ex
27f20 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0d  plain==2 (which.
27f21 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 68  .** is to say wh
27f22 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 51  en the EXPLAIN Q
27f23 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 78  UERY PLAN syntax
27f24 20 69 73 20 75 73 65 64 2e 29 0d 0a 2a 2a 20 54   is used.)..** T
27f25 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72  his opcode recor
27f26 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ds information f
27f27 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65  rom the optimize
27f28 72 2e 20 20 49 74 20 69 73 20 74 68 65 0d 0a 2a  r.  It is the..*
27f29 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20  * the same as a 
27f2a 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63  no-op.  This opc
27f2b 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 72  odesnever appear
27f2c 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70  s in a real VM p
27f2d 72 6f 67 72 61 6d 2e 0d 0a 2a 2f 0d 0a 64 65 66  rogram...*/..def
27f2e 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ault: {         
27f2f 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c   /* This is real
27f30 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f  ly OP_Noop and O
27f31 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0d 0a 20 20  P_Explain */..  
27f32 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63  assert( pOp->opc
27f33 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f 70 20 7c 7c 20  ode==OP_Noop || 
27f34 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
27f35 45 78 70 6c 61 69 6e 20 29 3b 0d 0a 20 20 62 72  Explain );..  br
27f36 65 61 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a  eak;..}..../****
27f37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
27f3c 65 20 63 61 73 65 73 20 6f 66 20 74 68 65 20 73  e cases of the s
27f3d 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
27f3e 61 62 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20  above this line 
27f3f 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e  should all be in
27f40 64 65 6e 74 65 64 0d 0a 2a 2a 20 62 79 20 36 20  dented..** by 6 
27f41 73 70 61 63 65 73 2e 20 20 42 75 74 20 74 68 65  spaces.  But the
27f42 20 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73 70 61   left-most 6 spa
27f43 63 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65  ces have been re
27f44 6d 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f 76 65  moved to improve
27f45 20 74 68 65 0d 0a 2a 2a 20 72 65 61 64 61 62 69   the..** readabi
27f46 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 68 69 73  lity.  From this
27f47 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20   point on down, 
27f48 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e  the normal inden
27f49 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 61 72 65  tation rules are
27f4a 0d 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e 0d 0a  ..** restored...
27f4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27f4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
27f50 20 20 20 20 7d 0d 0a 0d 0a 23 69 66 64 65 66 20      }....#ifdef 
27f51 56 44 42 45 5f 50 52 4f 46 49 4c 45 0d 0a 20 20  VDBE_PROFILE..  
27f52 20 20 7b 0d 0a 20 20 20 20 20 20 75 36 34 20 65    {..      u64 e
27f53 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 74 65 33  lapsed = sqlite3
27f54 48 77 74 69 6d 65 28 29 20 2d 20 73 74 61 72 74  Hwtime() - start
27f55 3b 0d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 79  ;..      pOp->cy
27f56 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 3b  cles += elapsed;
27f57 0d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74  ..      pOp->cnt
27f58 2b 2b 3b 0d 0a 23 69 66 20 30 0d 0a 20 20 20 20  ++;..#if 0..    
27f59 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f      fprintf(stdo
27f5a 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65  ut, "%10llu ", e
27f5b 6c 61 70 73 65 64 29 3b 0d 0a 20 20 20 20 20 20  lapsed);..      
27f5c 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
27f5d 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69  ntOp(stdout, ori
27f5e 67 50 63 2c 20 26 61 4f 70 5b 6f 72 69 67 50 63  gPc, &aOp[origPc
27f5f 5d 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  ]);..#endif..   
27f60 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
27f61 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
27f62 6e 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74  ng code adds not
27f63 68 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75  hing to the actu
27f64 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  al functionality
27f65 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  ..    ** of the 
27f66 70 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20  program.  It is 
27f67 6f 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65  only here for te
27f68 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
27f69 69 6e 67 2e 0d 0a 20 20 20 20 2a 2a 20 4f 6e 20  ing...    ** On 
27f6a 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
27f6b 69 74 20 64 6f 65 73 20 62 75 72 6e 20 43 50 55  it does burn CPU
27f6c 20 63 79 63 6c 65 73 20 65 76 65 72 79 20 74 69   cycles every ti
27f6d 6d 65 20 74 68 72 6f 75 67 68 0d 0a 20 20 20 20  me through..    
27f6e 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74 6f 72  ** the evaluator
27f6f 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 63 61   loop.  So we ca
27f70 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74 20 77  n leave it out w
27f71 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20 64 65  hen NDEBUG is de
27f72 66 69 6e 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  fined...    */..
27f73 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
27f74 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d      assert( pc>=
27f75 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20  -1 && pc<p->nOp 
27f76 29 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  );....#ifdef SQL
27f77 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 69  ITE_DEBUG..    i
27f78 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0d 0a  f( p->trace ){..
27f79 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 20        if( rc!=0 
27f7a 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61  ) fprintf(p->tra
27f7b 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29  ce,"rc=%d\n",rc)
27f7c 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ;..      if( pOp
27f7d 2d 3e 6f 70 66 6c 61 67 73 20 26 20 28 4f 50 46  ->opflags & (OPF
27f7e 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41  LG_OUT2_PRERELEA
27f7f 53 45 7c 4f 50 46 4c 47 5f 4f 55 54 32 29 20 29  SE|OPFLG_OUT2) )
27f80 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 67 69 73  {..        regis
27f81 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63  terTrace(p->trac
27f82 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65  e, pOp->p2, &aMe
27f83 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0d 0a 20 20  m[pOp->p2]);..  
27f84 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
27f85 20 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20   pOp->opflags & 
27f86 4f 50 46 4c 47 5f 4f 55 54 33 20 29 7b 0d 0a 20  OPFLG_OUT3 ){.. 
27f87 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54         registerT
27f88 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70  race(p->trace, p
27f89 4f 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f  Op->p3, &aMem[pO
27f8a 70 2d 3e 70 33 5d 29 3b 0d 0a 20 20 20 20 20 20  p->p3]);..      
27f8b 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  }..    }..#endif
27f8c 20 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55    /* SQLITE_DEBU
27f8d 47 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 20 2f 2a  G */..#endif  /*
27f8e 20 4e 44 45 42 55 47 20 2a 2f 0d 0a 20 20 7d 20   NDEBUG */..  } 
27f8f 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74   /* The end of t
27f90 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20  he for(;;) loop 
27f91 74 68 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  the loops throug
27f92 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0d 0a 0d 0a  h opcodes */....
27f93 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68    /* If we reach
27f94 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20   this point, it 
27f95 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63 75  means that execu
27f96 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65 64  tion is finished
27f97 20 77 69 74 68 0d 0a 20 20 2a 2a 20 61 6e 20 65   with..  ** an e
27f98 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e  rror of some kin
27f99 64 2e 0d 0a 20 20 2a 2f 0d 0a 76 64 62 65 5f 65  d...  */..vdbe_e
27f9a 72 72 6f 72 5f 68 61 6c 74 3a 0d 0a 20 20 61 73  rror_halt:..  as
27f9b 73 65 72 74 28 20 72 63 20 29 3b 0d 0a 20 20 70  sert( rc );..  p
27f9c 2d 3e 72 63 20 3d 20 72 63 3b 0d 0a 20 20 74 65  ->rc = rc;..  te
27f9d 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33 47  stcase( sqlite3G
27f9e 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67  lobalConfig.xLog
27f9f 21 3d 30 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  !=0 );..  sqlite
27fa0 33 5f 6c 6f 67 28 72 63 2c 20 22 73 74 61 74 65  3_log(rc, "state
27fa1 6d 65 6e 74 20 61 62 6f 72 74 73 20 61 74 20 25  ment aborts at %
27fa2 64 3a 20 5b 25 73 5d 20 25 73 22 2c 20 0d 0a 20  d: [%s] %s", .. 
27fa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27fa4 20 20 70 63 2c 20 70 2d 3e 7a 53 71 6c 2c 20 70    pc, p->zSql, p
27fa5 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 73  ->zErrMsg);..  s
27fa6 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70  qlite3VdbeHalt(p
27fa7 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
27fa8 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
27fa9 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69   ) db->mallocFai
27faa 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 72 63 20 3d  led = 1;..  rc =
27fab 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a   SQLITE_ERROR;..
27fac 20 20 69 66 28 20 72 65 73 65 74 53 63 68 65 6d    if( resetSchem
27fad 61 4f 6e 46 61 75 6c 74 3e 30 20 29 7b 0d 0a 20  aOnFault>0 ){.. 
27fae 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
27faf 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
27fb0 2c 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e 46  , resetSchemaOnF
27fb1 61 75 6c 74 2d 31 29 3b 0d 0a 20 20 7d 0d 0a 0d  ault-1);..  }...
27fb2 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68  .  /* This is th
27fb3 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f  e only way out o
27fb4 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  f this procedure
27fb5 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0d 0a 20  .  We have to.. 
27fb6 20 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20   ** release the 
27fb7 6d 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65  mutexes on btree
27fb8 73 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75  s that were acqu
27fb9 69 72 65 64 20 61 74 20 74 68 65 0d 0a 20 20 2a  ired at the..  *
27fba 2a 20 74 6f 70 2e 20 2a 2f 0d 0a 76 64 62 65 5f  * top. */..vdbe_
27fbb 72 65 74 75 72 6e 3a 0d 0a 20 20 64 62 2d 3e 6c  return:..  db->l
27fbc 61 73 74 52 6f 77 69 64 20 3d 20 6c 61 73 74 52  astRowid = lastR
27fbd 6f 77 69 64 3b 0d 0a 20 20 73 71 6c 69 74 65 33  owid;..  sqlite3
27fbe 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0d 0a 20  VdbeLeave(p);.. 
27fbf 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20   return rc;.... 
27fc0 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
27fc1 20 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20   if a string or 
27fc2 62 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e  blob larger than
27fc3 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
27fc4 54 48 0d 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f  TH..  ** is enco
27fc5 75 6e 74 65 72 65 64 2e 0d 0a 20 20 2a 2f 0d 0a  untered...  */..
27fc6 74 6f 6f 5f 62 69 67 3a 0d 0a 20 20 73 71 6c 69  too_big:..  sqli
27fc7 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
27fc8 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 73  >zErrMsg, db, "s
27fc9 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f  tring or blob to
27fca 6f 20 62 69 67 22 29 3b 0d 0a 20 20 72 63 20 3d  o big");..  rc =
27fcb 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0d   SQLITE_TOOBIG;.
27fcc 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
27fcd 6f 72 5f 68 61 6c 74 3b 0d 0a 0d 0a 20 20 2f 2a  or_halt;....  /*
27fce 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
27fcf 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
27fd0 73 2e 0d 0a 20 20 2a 2f 0d 0a 6e 6f 5f 6d 65 6d  s...  */..no_mem
27fd1 3a 0d 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  :..  db->mallocF
27fd2 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 73 71  ailed = 1;..  sq
27fd3 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
27fd4 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
27fd5 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
27fd6 3b 0d 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  ;..  rc = SQLITE
27fd7 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 67 6f 74 6f 20  _NOMEM;..  goto 
27fd8 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b  vdbe_error_halt;
27fd9 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  ....  /* Jump to
27fda 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74   here for any ot
27fdb 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61  her kind of fata
27fdc 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72  l error.  The "r
27fdd 63 22 20 76 61 72 69 61 62 6c 65 0d 0a 20 20 2a  c" variable..  *
27fde 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68  * should hold th
27fdf 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0d  e error number..
27fe0 0a 20 20 2a 2f 0d 0a 61 62 6f 72 74 5f 64 75 65  .  */..abort_due
27fe1 5f 74 6f 5f 65 72 72 6f 72 3a 0d 0a 20 20 61 73  _to_error:..  as
27fe2 73 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67  sert( p->zErrMsg
27fe3 3d 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 64 62  ==0 );..  if( db
27fe4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
27fe5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
27fe6 45 4d 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53  EM;..  if( rc!=S
27fe7 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
27fe8 4d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  M ){..    sqlite
27fe9 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
27fea 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22  ErrMsg, db, "%s"
27feb 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
27fec 72 63 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 67 6f  rc));..  }..  go
27fed 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
27fee 6c 74 3b 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70  lt;....  /* Jump
27fef 20 74 6f 20 68 65 72 65 20 69 66 20 74 68 65 20   to here if the 
27ff0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
27ff1 74 28 29 20 41 50 49 20 73 65 74 73 20 74 68 65  t() API sets the
27ff2 20 69 6e 74 65 72 72 75 70 74 0d 0a 20 20 2a 2a   interrupt..  **
27ff3 20 66 6c 61 67 2e 0d 0a 20 20 2a 2f 0d 0a 61 62   flag...  */..ab
27ff4 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72  ort_due_to_inter
27ff5 72 75 70 74 3a 0d 0a 20 20 61 73 73 65 72 74 28  rupt:..  assert(
27ff6 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
27ff7 75 70 74 65 64 20 29 3b 0d 0a 20 20 72 63 20 3d  upted );..  rc =
27ff8 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
27ff9 54 3b 0d 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63  T;..  p->rc = rc
27ffa 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53  ;..  sqlite3SetS
27ffb 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
27ffc 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
27ffd 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
27ffe 0d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  ..  goto vdbe_er
27fff 72 6f 72 5f 68 61 6c 74 3b 0d 0a 7d 0d 0a 0d 0a  ror_halt;..}....
28000 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
28001 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a  End of vdbe.c **
28002 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28003 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28004 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
28005 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
28006 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
28007 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  blob.c *********
28008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2800a 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 37 20 4d 61  ../*..** 2007 Ma
2800b 79 20 31 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  y 1..**..** The 
2800c 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2800d 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2800e 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2800f 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
28010 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
28011 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
28012 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
28013 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
28014 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
28015 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
28016 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
28017 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
28018 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
28019 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2801a 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2801b 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2801c 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
2801d 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
2801e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2801f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d  ***********..**.
28022 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
28023 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
28024 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e   to implement in
28025 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49  cremental BLOB I
28026 2f 4f 2e 0d 0a 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66  /O...*/......#if
28027 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28028 5f 49 4e 43 52 42 4c 4f 42 0d 0a 0d 0a 2f 2a 0d  _INCRBLOB..../*.
28029 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65  .** Valid sqlite
2802a 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20  3_blob* handles 
2802b 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f  point to Incrblo
2802c 62 20 73 74 72 75 63 74 75 72 65 73 2e 0d 0a 2a  b structures...*
2802d 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  /..typedef struc
2802e 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62  t Incrblob Incrb
2802f 6c 6f 62 3b 0d 0a 73 74 72 75 63 74 20 49 6e 63  lob;..struct Inc
28030 72 62 6c 6f 62 20 7b 0d 0a 20 20 69 6e 74 20 66  rblob {..  int f
28031 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
28032 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66     /* Copy of "f
28033 6c 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20  lags" passed to 
28034 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
28035 6e 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42  n() */..  int nB
28036 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  yte;            
28037 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65    /* Size of ope
28038 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73  n blob, in bytes
28039 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4f 66 66 73   */..  int iOffs
2803a 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
2803b 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  * Byte offset of
2803c 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 6f 72 20   blob in cursor 
2803d 64 61 74 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  data */..  int i
2803e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
2803f 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75     /* Table colu
28040 6d 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 20 69  mn this handle i
28041 73 20 6f 70 65 6e 20 6f 6e 20 2a 2f 0d 0a 20 20  s open on */..  
28042 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 20  BtCursor *pCsr; 
28043 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
28044 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c  r pointing at bl
28045 6f 62 20 72 6f 77 20 2a 2f 0d 0a 20 20 73 71 6c  ob row */..  sql
28046 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
28047 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e  ;    /* Statemen
28048 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 73 6f 72  t holding cursor
28049 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69   open */..  sqli
2804a 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
2804b 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63      /* The assoc
2804c 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 2a  iated database *
2804d 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  /..};....../*..*
2804e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2804f 69 73 20 75 73 65 64 20 62 79 20 62 6f 74 68 20  is used by both 
28050 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 61 6e 64 20  blob_open() and 
28051 62 6c 6f 62 5f 72 65 6f 70 65 6e 28 29 2e 20 49  blob_reopen(). I
28052 74 20 73 65 65 6b 73 0d 0a 2a 2a 20 74 68 65 20  t seeks..** the 
28053 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 73  b-tree cursor as
28054 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 62 6c  sociated with bl
28055 6f 62 20 68 61 6e 64 6c 65 20 70 20 74 6f 20 70  ob handle p to p
28056 6f 69 6e 74 20 74 6f 20 72 6f 77 20 69 52 6f 77  oint to row iRow
28057 2e 0d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  ...** If success
28058 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ful, SQLITE_OK i
28059 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 73  s returned and s
2805a 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
2805b 74 6f 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  to..** sqlite3_b
2805c 6c 6f 62 5f 72 65 61 64 28 29 20 6f 72 20 73 71  lob_read() or sq
2805d 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
2805e 28 29 20 61 63 63 65 73 73 20 74 68 65 20 73 70  () access the sp
2805f 65 63 69 66 69 65 64 20 72 6f 77 2e 0d 0a 2a 2a  ecified row...**
28060 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  ..** If an error
28061 20 6f 63 63 75 72 73 2c 20 6f 72 20 69 66 20 74   occurs, or if t
28062 68 65 20 73 70 65 63 69 66 69 65 64 20 72 6f 77  he specified row
28063 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
28064 6f 72 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a 20  or does not..** 
28065 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20  contain a value 
28066 6f 66 20 74 79 70 65 20 54 45 58 54 20 6f 72 20  of type TEXT or 
28067 42 4c 4f 42 20 69 6e 20 74 68 65 20 63 6f 6c 75  BLOB in the colu
28068 6d 6e 20 6e 6f 6d 69 6e 61 74 65 64 20 77 68 65  mn nominated whe
28069 6e 20 74 68 65 0d 0a 2a 2a 20 62 6c 6f 62 20 68  n the..** blob h
2806a 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e 65 64  andle was opened
2806b 2c 20 74 68 65 6e 20 61 6e 20 65 72 72 6f 72 20  , then an error 
2806c 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
2806d 20 61 6e 64 20 2a 70 7a 45 72 72 20 6d 61 79 0d   and *pzErr may.
2806e 0a 2a 2a 20 62 65 20 73 65 74 20 74 6f 20 70 6f  .** be set to po
2806f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
28070 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 65 72  containing an er
28071 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 49 74 20  ror message. It 
28072 69 73 20 74 68 65 0d 0a 2a 2a 20 72 65 73 70 6f  is the..** respo
28073 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
28074 20 63 61 6c 6c 65 72 20 74 6f 20 66 72 65 65 20   caller to free 
28075 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
28076 65 20 62 75 66 66 65 72 20 75 73 69 6e 67 0d 0a  e buffer using..
28077 2a 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ** sqlite3DbFree
28078 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  ()...**..** If a
28079 6e 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63  n error does occ
2807a 75 72 2c 20 74 68 65 6e 20 74 68 65 20 62 2d 74  ur, then the b-t
2807b 72 65 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c  ree cursor is cl
2807c 6f 73 65 64 2e 20 41 6c 6c 20 73 75 62 73 65 71  osed. All subseq
2807d 75 65 6e 74 0d 0a 2a 2a 20 63 61 6c 6c 73 20 74  uent..** calls t
2807e 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  o sqlite3_blob_r
2807f 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74  ead(), blob_writ
28080 65 28 29 20 6f 72 20 62 6c 6f 62 5f 72 65 6f 70  e() or blob_reop
28081 65 6e 28 29 20 77 69 6c 6c 20 0d 0a 2a 2a 20 69  en() will ..** i
28082 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74 75 72  mmediately retur
28083 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0d  n SQLITE_ABORT..
28084 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
28085 62 6c 6f 62 53 65 65 6b 54 6f 52 6f 77 28 49 6e  blobSeekToRow(In
28086 63 72 62 6c 6f 62 20 2a 70 2c 20 73 71 6c 69 74  crblob *p, sqlit
28087 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 63  e3_int64 iRow, c
28088 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0d 0a 20  har **pzErr){.. 
28089 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
2808a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2808b 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a   /* Error code *
2808c 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20  /..  char *zErr 
2808d 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2808e 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65       /* Error me
2808f 73 73 61 67 65 20 2a 2f 0d 0a 20 20 56 64 62 65  ssage */..  Vdbe
28090 20 2a 76 20 3d 20 28 56 64 62 65 20 2a 29 70 2d   *v = (Vdbe *)p-
28091 3e 70 53 74 6d 74 3b 0d 0a 0d 0a 20 20 2f 2a 20  >pStmt;....  /* 
28092 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
28093 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
28094 6e 74 73 20 6f 6e 6c 79 20 76 61 72 69 61 62 6c  nts only variabl
28095 65 20 74 6f 20 69 6e 74 65 67 65 72 20 69 52 6f  e to integer iRo
28096 77 2e 20 0d 0a 20 20 2a 2a 20 54 68 69 73 20 69  w. ..  ** This i
28097 73 20 64 6f 6e 65 20 64 69 72 65 63 74 6c 79 20  s done directly 
28098 69 6e 73 74 65 61 64 20 6f 66 20 75 73 69 6e 67  instead of using
28099 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2809a 74 36 34 28 29 20 74 6f 20 61 76 6f 69 64 20 0d  t64() to avoid .
2809b 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 69 6e 67  .  ** triggering
2809c 20 61 73 73 65 72 74 73 20 72 65 6c 61 74 65 64   asserts related
2809d 20 74 6f 20 6d 75 74 65 78 65 73 2e 0d 0a 20 20   to mutexes...  
2809e 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 76 2d  */..  assert( v-
2809f 3e 61 56 61 72 5b 30 5d 2e 66 6c 61 67 73 26 4d  >aVar[0].flags&M
280a0 45 4d 5f 49 6e 74 20 29 3b 0d 0a 20 20 76 2d 3e  EM_Int );..  v->
280a1 61 56 61 72 5b 30 5d 2e 75 2e 69 20 3d 20 69 52  aVar[0].u.i = iR
280a2 6f 77 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71  ow;....  rc = sq
280a3 6c 69 74 65 33 5f 73 74 65 70 28 70 2d 3e 70 53  lite3_step(p->pS
280a4 74 6d 74 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d  tmt);..  if( rc=
280a5 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a  =SQLITE_ROW ){..
280a6 20 20 20 20 75 33 32 20 74 79 70 65 20 3d 20 76      u32 type = v
280a7 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70  ->apCsr[0]->aTyp
280a8 65 5b 70 2d 3e 69 43 6f 6c 5d 3b 0d 0a 20 20 20  e[p->iCol];..   
280a9 20 69 66 28 20 74 79 70 65 3c 31 32 20 29 7b 0d   if( type<12 ){.
280aa 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71  .      zErr = sq
280ab 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 2d 3e  lite3MPrintf(p->
280ac 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
280ad 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25   value of type %
280ae 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 74  s",..          t
280af 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74  ype==0?"null": t
280b0 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22  ype==7?"real": "
280b1 69 6e 74 65 67 65 72 22 0d 0a 20 20 20 20 20 20  integer"..      
280b2 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  );..      rc = S
280b3 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
280b4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
280b5 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0d  lize(p->pStmt);.
280b6 0a 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20  .      p->pStmt 
280b7 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  = 0;..    }else{
280b8 0d 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ..      p->iOffs
280b9 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 5d  et = v->apCsr[0]
280ba 2d 3e 61 4f 66 66 73 65 74 5b 70 2d 3e 69 43 6f  ->aOffset[p->iCo
280bb 6c 5d 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 42  l];..      p->nB
280bc 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  yte = sqlite3Vdb
280bd 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74  eSerialTypeLen(t
280be 79 70 65 29 3b 0d 0a 20 20 20 20 20 20 70 2d 3e  ype);..      p->
280bf 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72  pCsr =  v->apCsr
280c0 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0d 0a 20  [0]->pCursor;.. 
280c1 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
280c2 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e  eEnterCursor(p->
280c3 70 43 73 72 29 3b 0d 0a 20 20 20 20 20 20 73 71  pCsr);..      sq
280c4 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f  lite3BtreeCacheO
280c5 76 65 72 66 6c 6f 77 28 70 2d 3e 70 43 73 72 29  verflow(p->pCsr)
280c6 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
280c7 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72  BtreeLeaveCursor
280c8 28 70 2d 3e 70 43 73 72 29 3b 0d 0a 20 20 20 20  (p->pCsr);..    
280c9 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20  }..  }....  if( 
280ca 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
280cb 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  {..    rc = SQLI
280cc 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65 20  TE_OK;..  }else 
280cd 69 66 28 20 70 2d 3e 70 53 74 6d 74 20 29 7b 0d  if( p->pStmt ){.
280ce 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
280cf 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53  3_finalize(p->pS
280d0 74 6d 74 29 3b 0d 0a 20 20 20 20 70 2d 3e 70 53  tmt);..    p->pS
280d1 74 6d 74 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66  tmt = 0;..    if
280d2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
280d3 29 7b 0d 0a 20 20 20 20 20 20 7a 45 72 72 20 3d  ){..      zErr =
280d4 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
280d5 70 2d 3e 64 62 2c 20 22 6e 6f 20 73 75 63 68 20  p->db, "no such 
280d6 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52  rowid: %lld", iR
280d7 6f 77 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  ow);..      rc =
280d8 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a   SQLITE_ERROR;..
280d9 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
280da 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33    zErr = sqlite3
280db 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 22  MPrintf(p->db, "
280dc 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72  %s", sqlite3_err
280dd 6d 73 67 28 70 2d 3e 64 62 29 29 3b 0d 0a 20 20  msg(p->db));..  
280de 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73    }..  }....  as
280df 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
280e0 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29  _OK || zErr==0 )
280e1 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  ;..  assert( rc!
280e2 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72  =SQLITE_ROW && r
280e3 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
280e4 3b 0d 0a 0d 0a 20 20 2a 70 7a 45 72 72 20 3d 20  ;....  *pzErr = 
280e5 7a 45 72 72 3b 0d 0a 20 20 72 65 74 75 72 6e 20  zErr;..  return 
280e6 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
280e7 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e   Open a blob han
280e8 64 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  dle...*/..SQLITE
280e9 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
280ea 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0d 0a 20 20 73  _blob_open(..  s
280eb 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20 20  qlite3* db,     
280ec 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
280ed 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
280ee 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  n */..  const ch
280ef 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20 20  ar *zDb,        
280f0 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 64 20  /* The attached 
280f1 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
280f2 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0d  ing the blob */.
280f3 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
280f4 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 68  Table,     /* Th
280f5 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
280f6 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0d 0a  ng the blob */..
280f7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
280f8 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65  olumn,    /* The
280f9 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69   column containi
280fa 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0d 0a  ng the blob */..
280fb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
280fc 52 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  Row,      /* The
280fd 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20   row containing 
280fe 74 68 65 20 67 6c 6f 62 20 2a 2f 0d 0a 20 20 69  the glob */..  i
280ff 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
28100 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d         /* True -
28101 3e 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  > read/write acc
28102 65 73 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65  ess, false -> re
28103 61 64 2d 6f 6e 6c 79 20 2a 2f 0d 0a 20 20 73 71  ad-only */..  sq
28104 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42  lite3_blob **ppB
28105 6c 6f 62 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20  lob   /* Handle 
28106 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
28107 65 20 62 6c 6f 62 20 72 65 74 75 72 6e 65 64 20  e blob returned 
28108 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  here */..){..  i
28109 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 30 3b  nt nAttempt = 0;
2810a 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  ..  int iCol;   
2810b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2810c 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e 20  ndex of zColumn 
2810d 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a 2f  in row-record */
2810e 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73 20 56 44  ....  /* This VD
2810f 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73  BE program seeks
28110 20 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20   a btree cursor 
28111 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  to the identifie
28112 64 20 0d 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c  d ..  ** db/tabl
28113 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65  e/row entry. The
28114 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e   reason for usin
28115 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d  g a vdbe program
28116 20 69 6e 73 74 65 61 64 0d 0a 20 20 2a 2a 20 6f   instead..  ** o
28117 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74  f writing code t
28118 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65  o use the b-tree
28119 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20   layer directly 
2811a 69 73 20 74 68 61 74 20 74 68 65 0d 0a 20 20 2a  is that the..  *
2811b 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77  * vdbe program w
2811c 69 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61  ill take advanta
2811d 67 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75  ge of the variou
2811e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0d 0a  s transaction,..
2811f 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64    ** locking and
28120 20 65 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20   error handling 
28121 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 62  infrastructure b
28122 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64  uilt into the vd
28123 62 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  be...  **..  ** 
28124 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 74 68  After seeking th
28125 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 76 64  e cursor, the vd
28126 62 65 20 65 78 65 63 75 74 65 73 20 61 6e 20 4f  be executes an O
28127 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0d 0a 20 20  P_ResultRow...  
28128 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 6c  ** Code external
28129 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 65   to the Vdbe the
2812a 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 20  n "borrows" the 
2812b 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e  b-tree cursor an
2812c 64 0d 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20  d..  ** uses it 
2812d 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2812e 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c   blob_read(), bl
2812f 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0d  ob_write() and .
28130 0a 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73  .  ** blob_bytes
28131 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a 20  () functions... 
28132 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 73 71   **..  ** The sq
28133 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
28134 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61  () function fina
28135 6c 69 7a 65 73 20 74 68 65 20 76 64 62 65 20 70  lizes the vdbe p
28136 72 6f 67 72 61 6d 2c 0d 0a 20 20 2a 2a 20 77 68  rogram,..  ** wh
28137 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65 20 62  ich closes the b
28138 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e 64  -tree cursor and
28139 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f 6d 6d   (possibly) comm
2813a 69 74 73 20 74 68 65 20 0d 0a 20 20 2a 2a 20 74  its the ..  ** t
2813b 72 61 6e 73 61 63 74 69 6f 6e 2e 0d 0a 20 20 2a  ransaction...  *
2813c 2f 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  /..  static cons
2813d 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65  t VdbeOpList ope
2813e 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0d 0a 20 20 20  nBlob[] = {..   
2813f 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e   {OP_Transaction
28140 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
28141 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72  /* 0: Start a tr
28142 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20  ansaction */..  
28143 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b    {OP_VerifyCook
28144 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  ie, 0, 0, 0},   
28145 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65   /* 1: Check the
28146 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a   schema cookie *
28147 2f 0d 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65  /..    {OP_Table
28148 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Lock, 0, 0, 0}, 
28149 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75        /* 2: Acqu
2814a 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72  ire a read or wr
2814b 69 74 65 20 6c 6f 63 6b 20 2a 2f 0d 0a 0d 0a 20  ite lock */.... 
2814c 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
2814d 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69   following two i
2814e 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72  nstructions is r
2814f 65 70 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50  eplaced by an OP
28150 5f 4e 6f 6f 70 2e 20 2a 2f 0d 0a 20 20 20 20 7b  _Noop. */..    {
28151 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20  OP_OpenRead, 0, 
28152 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a  0, 0},        /*
28153 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20   3: Open cursor 
28154 30 20 66 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f  0 for reading */
28155 0d 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 72  ..    {OP_OpenWr
28156 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  ite, 0, 0, 0},  
28157 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 6e 20       /* 4: Open 
28158 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61  cursor 0 for rea
28159 64 2f 77 72 69 74 65 20 2a 2f 0d 0a 0d 0a 20 20  d/write */....  
2815a 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20    {OP_Variable, 
2815b 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 20  1, 1, 1},       
2815c 20 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 20   /* 5: Push the 
2815d 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 61  rowid to the sta
2815e 63 6b 20 2a 2f 0d 0a 20 20 20 20 7b 4f 50 5f 4e  ck */..    {OP_N
2815f 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 31 30 2c  otExists, 0, 10,
28160 20 31 7d 2c 20 20 20 20 20 20 2f 2a 20 36 3a 20   1},      /* 6: 
28161 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20  Seek the cursor 
28162 2a 2f 0d 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75  */..    {OP_Colu
28163 6d 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20  mn, 0, 0, 1},   
28164 20 20 20 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0d         /* 7  */.
28165 0a 20 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52  .    {OP_ResultR
28166 6f 77 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20  ow, 1, 0, 0},   
28167 20 20 20 20 2f 2a 20 38 20 20 2a 2f 0d 0a 20 20      /* 8  */..  
28168 20 20 7b 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 35    {OP_Goto, 0, 5
28169 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20  , 0},           
2816a 20 2f 2a 20 39 20 20 2a 2f 0d 0a 20 20 20 20 7b   /* 9  */..    {
2816b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20  OP_Close, 0, 0, 
2816c 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0},           /*
2816d 20 31 30 20 2a 2f 0d 0a 20 20 20 20 7b 4f 50 5f   10 */..    {OP_
2816e 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Halt, 0, 0, 0}, 
2816f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 31             /* 11
28170 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 0d 0a 20 20 69   */..  };....  i
28171 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
28172 4b 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  K;..  char *zErr
28173 20 3d 20 30 3b 0d 0a 20 20 54 61 62 6c 65 20 2a   = 0;..  Table *
28174 70 54 61 62 3b 0d 0a 20 20 50 61 72 73 65 20 2a  pTab;..  Parse *
28175 70 50 61 72 73 65 20 3d 20 30 3b 0d 0a 20 20 49  pParse = 0;..  I
28176 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 20 3d  ncrblob *pBlob =
28177 20 30 3b 0d 0a 0d 0a 20 20 66 6c 61 67 73 20 3d   0;....  flags =
28178 20 21 21 66 6c 61 67 73 3b 20 20 20 20 20 20 20   !!flags;       
28179 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
2817a 73 20 3d 20 28 66 6c 61 67 73 20 3f 20 31 20 3a  s = (flags ? 1 :
2817b 20 30 29 3b 20 2a 2f 0d 0a 20 20 2a 70 70 42 6c   0); */..  *ppBl
2817c 6f 62 20 3d 20 30 3b 0d 0a 0d 0a 20 20 73 71 6c  ob = 0;....  sql
2817d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2817e 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 0d 0a  (db->mutex);....
2817f 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62    pBlob = (Incrb
28180 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  lob *)sqlite3DbM
28181 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
28182 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b  zeof(Incrblob));
28183 0d 0a 20 20 69 66 28 20 21 70 42 6c 6f 62 20 29  ..  if( !pBlob )
28184 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f   goto blob_open_
28185 6f 75 74 3b 0d 0a 20 20 70 50 61 72 73 65 20 3d  out;..  pParse =
28186 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c   sqlite3StackAll
28187 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
28188 28 2a 70 50 61 72 73 65 29 29 3b 0d 0a 20 20 69  (*pParse));..  i
28189 66 28 20 21 70 50 61 72 73 65 20 29 20 67 6f 74  f( !pParse ) got
2818a 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b  o blob_open_out;
2818b 0d 0a 0d 0a 20 20 64 6f 20 7b 0d 0a 20 20 20 20  ....  do {..    
2818c 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20 30  memset(pParse, 0
2818d 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29  , sizeof(Parse))
2818e 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  ;..    pParse->d
2818f 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20 73 71 6c  b = db;..    sql
28190 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
28191 45 72 72 29 3b 0d 0a 20 20 20 20 7a 45 72 72 20  Err);..    zErr 
28192 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69  = 0;....    sqli
28193 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
28194 28 64 62 29 3b 0d 0a 20 20 20 20 70 54 61 62 20  (db);..    pTab 
28195 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
28196 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
28197 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0d 0a 20  zTable, zDb);.. 
28198 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49     if( pTab && I
28199 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2819a 7b 0d 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  {..      pTab = 
2819b 30 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0;..      sqlite
2819c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2819d 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
2819e 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73  irtual table: %s
2819f 22 2c 20 7a 54 61 62 6c 65 29 3b 0d 0a 20 20 20  ", zTable);..   
281a0 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
281a1 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 20 20  TE_OMIT_VIEW..  
281a2 20 20 69 66 28 20 70 54 61 62 20 26 26 20 70 54    if( pTab && pT
281a3 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a  ab->pSelect ){..
281a4 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0d        pTab = 0;.
281a5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
281a6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
281a7 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 65 77  cannot open view
281a8 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29 3b 0d  : %s", zTable);.
281a9 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
281aa 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b      if( !pTab ){
281ab 0d 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  ..      if( pPar
281ac 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0d 0a  se->zErrMsg ){..
281ad 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
281ae 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
281af 0d 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d  ..        zErr =
281b0 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
281b1 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  ;..        pPars
281b2 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d  e->zErrMsg = 0;.
281b3 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
281b4 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
281b5 52 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  R;..      sqlite
281b6 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
281b7 62 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20  b);..      goto 
281b8 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a  blob_open_out;..
281b9 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
281ba 4e 6f 77 20 73 65 61 72 63 68 20 70 54 61 62 20  Now search pTab 
281bb 66 6f 72 20 74 68 65 20 65 78 61 63 74 20 63 6f  for the exact co
281bc 6c 75 6d 6e 2e 20 2a 2f 0d 0a 20 20 20 20 66 6f  lumn. */..    fo
281bd 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
281be 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
281bf 2b 29 20 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  +) {..      if( 
281c0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
281c1 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
281c2 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d  zName, zColumn)=
281c3 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 62  =0 ){..        b
281c4 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
281c5 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 69      }..    if( i
281c6 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  Col==pTab->nCol 
281c7 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
281c8 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
281c9 29 3b 0d 0a 20 20 20 20 20 20 7a 45 72 72 20 3d  );..      zErr =
281ca 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
281cb 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c  db, "no such col
281cc 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43  umn: \"%s\"", zC
281cd 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 20 20 20 20 72  olumn);..      r
281ce 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
281cf 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
281d0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
281d1 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  );..      goto b
281d2 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a 20  lob_open_out;.. 
281d3 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49     }....    /* I
281d4 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 62  f the value is b
281d5 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20  eing opened for 
281d6 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74  writing, check t
281d7 68 61 74 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  hat the..    ** 
281d8 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e  column is not in
281d9 64 65 78 65 64 2c 20 61 6e 64 20 74 68 61 74 20  dexed, and that 
281da 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
281db 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  f a foreign key.
281dc 20 0d 0a 20 20 20 20 2a 2a 20 49 74 20 69 73 20   ..    ** It is 
281dd 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c 65  against the rule
281de 73 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75  s to open a colu
281df 6d 6e 20 74 6f 20 77 68 69 63 68 20 65 69 74 68  mn to which eith
281e0 65 72 20 6f 66 20 74 68 65 73 65 0d 0a 20 20 20  er of these..   
281e1 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73   ** descriptions
281e2 20 61 70 70 6c 69 65 73 20 66 6f 72 20 77 72 69   applies for wri
281e3 74 69 6e 67 2e 20 20 2a 2f 0d 0a 20 20 20 20 69  ting.  */..    i
281e4 66 28 20 66 6c 61 67 73 20 29 7b 0d 0a 20 20 20  f( flags ){..   
281e5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
281e6 46 61 75 6c 74 20 3d 20 30 3b 0d 0a 20 20 20 20  Fault = 0;..    
281e7 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a    Index *pIdx;..
281e8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
281e9 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0d  MIT_FOREIGN_KEY.
281ea 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 66  .      if( db->f
281eb 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
281ec 69 67 6e 4b 65 79 73 20 29 7b 0d 0a 20 20 20 20  ignKeys ){..    
281ed 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
281ee 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  t the column is 
281ef 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 6e 20 46  not part of an F
281f0 4b 20 63 68 69 6c 64 20 6b 65 79 20 64 65 66 69  K child key defi
281f1 6e 69 74 69 6f 6e 2e 20 49 74 0d 0a 20 20 20 20  nition. It..    
281f2 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 6e 65      ** is not ne
281f3 63 65 73 73 61 72 79 20 74 6f 20 63 68 65 63 6b  cessary to check
281f4 20 69 66 20 69 74 20 69 73 20 70 61 72 74 20 6f   if it is part o
281f5 66 20 61 20 70 61 72 65 6e 74 20 6b 65 79 2c 20  f a parent key, 
281f6 61 73 20 70 61 72 65 6e 74 0d 0a 20 20 20 20 20  as parent..     
281f7 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c 75 6d 6e     ** key column
281f8 73 20 6d 75 73 74 20 62 65 20 69 6e 64 65 78 65  s must be indexe
281f9 64 2e 20 54 68 65 20 63 68 65 63 6b 20 62 65 6c  d. The check bel
281fa 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20 75 70 20  ow will pick up 
281fb 74 68 69 73 20 0d 0a 20 20 20 20 20 20 20 20 2a  this ..        *
281fc 2a 20 63 61 73 65 2e 20 20 2a 2f 0d 0a 20 20 20  * case.  */..   
281fd 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79       FKey *pFKey
281fe 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  ;..        for(p
281ff 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79  FKey=pTab->pFKey
28200 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
28201 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  FKey->pNextFrom)
28202 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  {..          int
28203 20 6a 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 66   j;..          f
28204 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d  or(j=0; j<pFKey-
28205 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0d 0a 20 20  >nCol; j++){..  
28206 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 46            if( pF
28207 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72  Key->aCol[j].iFr
28208 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0d 0a 20 20 20  om==iCol ){..   
28209 20 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c             zFaul
2820a 74 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b 65 79  t = "foreign key
2820b 22 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ";..            
2820c 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  }..          }..
2820d 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2820e 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
2820f 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
28210 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
28211 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
28212 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a  {..        int j
28213 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  ;..        for(j
28214 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; j<pIdx->nCol
28215 75 6d 6e 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20  umn; j++){..    
28216 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
28217 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f  aiColumn[j]==iCo
28218 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  l ){..          
28219 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e 64 65    zFault = "inde
2821a 78 65 64 22 3b 0d 0a 20 20 20 20 20 20 20 20 20  xed";..         
2821b 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20   }..        }.. 
2821c 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
2821d 28 20 7a 46 61 75 6c 74 20 29 7b 0d 0a 20 20 20  ( zFault ){..   
2821e 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2821f 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0d 0a 20  ee(db, zErr);.. 
28220 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71         zErr = sq
28221 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
28222 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 25 73   "cannot open %s
28223 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74   column for writ
28224 69 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b 0d 0a  ing", zFault);..
28225 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
28226 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20  ITE_ERROR;..    
28227 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
28228 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0d 0a 20  LeaveAll(db);.. 
28229 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62         goto blob
2822a 5f 6f 70 65 6e 5f 6f 75 74 3b 0d 0a 20 20 20 20  _open_out;..    
2822b 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20    }..    }....  
2822c 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d    pBlob->pStmt =
2822d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a   (sqlite3_stmt *
2822e 29 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61  )sqlite3VdbeCrea
2822f 74 65 28 64 62 29 3b 0d 0a 20 20 20 20 61 73 73  te(db);..    ass
28230 65 72 74 28 20 70 42 6c 6f 62 2d 3e 70 53 74 6d  ert( pBlob->pStm
28231 74 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  t || db->mallocF
28232 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20 20 69 66  ailed );..    if
28233 28 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 29  ( pBlob->pStmt )
28234 7b 0d 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76  {..      Vdbe *v
28235 20 3d 20 28 56 64 62 65 20 2a 29 70 42 6c 6f 62   = (Vdbe *)pBlob
28236 2d 3e 70 53 74 6d 74 3b 0d 0a 20 20 20 20 20 20  ->pStmt;..      
28237 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
28238 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
28239 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2823a 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 73 71 6c 69  );....      sqli
2823b 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
2823c 28 76 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42  (v, sizeof(openB
2823d 6c 6f 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65  lob)/sizeof(Vdbe
2823e 4f 70 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f  OpList), openBlo
2823f 62 29 3b 0d 0a 0d 0a 0d 0a 20 20 20 20 20 20 2f  b);......      /
28240 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
28241 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a  OP_Transaction *
28242 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
28243 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
28244 30 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20  0, iDb);..      
28245 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
28246 65 50 32 28 76 2c 20 30 2c 20 66 6c 61 67 73 29  eP2(v, 0, flags)
28247 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43 6f  ;....      /* Co
28248 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 56  nfigure the OP_V
28249 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f 0d 0a  erifyCookie */..
2824a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2824b 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c 20  eChangeP1(v, 1, 
2824c 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  iDb);..      sql
2824d 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
2824e 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63  (v, 1, pTab->pSc
2824f 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
28250 6b 69 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  kie);..      sql
28251 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
28252 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63  (v, 1, pTab->pSc
28253 68 65 6d 61 2d 3e 69 47 65 6e 65 72 61 74 69 6f  hema->iGeneratio
28254 6e 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20  n);....      /* 
28255 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65  Make sure a mute
28256 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  x is held on the
28257 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
28258 65 73 73 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20  essed */..      
28259 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
2825a 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0d 0a  tree(v, iDb); ..
2825b 0d 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69  ..      /* Confi
2825c 67 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c  gure the OP_Tabl
2825d 65 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f  eLock instructio
2825e 6e 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c  n */..#ifdef SQL
2825f 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
28260 43 41 43 48 45 0d 0a 20 20 20 20 20 20 73 71 6c  CACHE..      sql
28261 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
28262 4e 6f 6f 70 28 76 2c 20 32 29 3b 0d 0a 23 65 6c  Noop(v, 2);..#el
28263 73 65 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
28264 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
28265 20 32 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20   2, iDb);..     
28266 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
28267 67 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d  geP2(v, 2, pTab-
28268 3e 74 6e 75 6d 29 3b 0d 0a 20 20 20 20 20 20 73  >tnum);..      s
28269 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2826a 50 33 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b  P3(v, 2, flags);
2826b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
2826c 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 32  dbeChangeP4(v, 2
2826d 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50  , pTab->zName, P
2826e 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 23  4_TRANSIENT);..#
2826f 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 20 20 2f  endif....      /
28270 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 65 72 20  * Remove either 
28271 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  the OP_OpenWrite
28272 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e 20 53 65   or OpenRead. Se
28273 74 20 74 68 65 20 50 32 20 0d 0a 20 20 20 20 20  t the P2 ..     
28274 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f 66   ** parameter of
28275 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70 54   the other to pT
28276 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0d 0a 20  ab->tnum.  */.. 
28277 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28278 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20  ChangeToNoop(v, 
28279 34 20 2d 20 66 6c 61 67 73 29 3b 0d 0a 20 20 20  4 - flags);..   
2827a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2827b 61 6e 67 65 50 32 28 76 2c 20 33 20 2b 20 66 6c  angeP2(v, 3 + fl
2827c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29  ags, pTab->tnum)
2827d 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2827e 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
2827f 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 29 3b  3 + flags, iDb);
28280 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ....      /* Con
28281 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65  figure the numbe
28282 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f  r of columns. Co
28283 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73  nfigure the curs
28284 6f 72 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20  or to..      ** 
28285 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74  think that the t
28286 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72  able has one mor
28287 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74  e column than it
28288 20 72 65 61 6c 6c 79 0d 0a 20 20 20 20 20 20 2a   really..      *
28289 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f  * does. An OP_Co
2828a 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65  lumn to retrieve
2828b 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20   this imaginary 
2828c 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0d 0a 20 20 20  column will..   
2828d 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74     ** always ret
2828e 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e  urn an SQL NULL.
2828f 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20   This is useful 
28290 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73  because it means
28291 0d 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61  ..      ** we ca
28292 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75  n invoke OP_Colu
28293 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68  mn to fill in th
28294 65 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 74  e vdbe cursors t
28295 79 70 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 61  ype ..      ** a
28296 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65 20  nd offset cache 
28297 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20  without causing 
28298 61 6e 79 20 49 4f 2e 0d 0a 20 20 20 20 20 20 2a  any IO...      *
28299 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
2829a 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2829b 33 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f  3+flags, SQLITE_
2829c 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d  INT_TO_PTR(pTab-
2829d 3e 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33  >nCol+1),P4_INT3
2829e 32 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  2);..      sqlit
2829f 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
282a0 2c 20 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 7, pTab->nCol)
282a1 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62  ;..      if( !db
282a2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
282a3 7b 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  {..        pPars
282a4 65 2d 3e 6e 56 61 72 20 3d 20 31 3b 0d 0a 20 20  e->nVar = 1;..  
282a5 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
282a6 65 6d 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  em = 1;..       
282a7 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20   pParse->nTab = 
282a8 31 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1;..        sqli
282a9 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
282aa 28 76 2c 20 70 50 61 72 73 65 29 3b 0d 0a 20 20  (v, pParse);..  
282ab 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
282ac 20 0d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 66 6c   ..    pBlob->fl
282ad 61 67 73 20 3d 20 66 6c 61 67 73 3b 0d 0a 20 20  ags = flags;..  
282ae 20 20 70 42 6c 6f 62 2d 3e 69 43 6f 6c 20 3d 20    pBlob->iCol = 
282af 69 43 6f 6c 3b 0d 0a 20 20 20 20 70 42 6c 6f 62  iCol;..    pBlob
282b0 2d 3e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20  ->db = db;..    
282b1 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
282b2 65 41 6c 6c 28 64 62 29 3b 0d 0a 20 20 20 20 69  eAll(db);..    i
282b3 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
282b4 6c 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f  led ){..      go
282b5 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
282b6 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71  ;..    }..    sq
282b7 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
282b8 28 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 2c 20 31  (pBlob->pStmt, 1
282b9 2c 20 69 52 6f 77 29 3b 0d 0a 20 20 20 20 72 63  , iRow);..    rc
282ba 20 3d 20 62 6c 6f 62 53 65 65 6b 54 6f 52 6f 77   = blobSeekToRow
282bb 28 70 42 6c 6f 62 2c 20 69 52 6f 77 2c 20 26 7a  (pBlob, iRow, &z
282bc 45 72 72 29 3b 0d 0a 20 20 7d 20 77 68 69 6c 65  Err);..  } while
282bd 28 20 28 2b 2b 6e 41 74 74 65 6d 70 74 29 3c 35  ( (++nAttempt)<5
282be 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53   && rc==SQLITE_S
282bf 43 48 45 4d 41 20 29 3b 0d 0a 0d 0a 62 6c 6f 62  CHEMA );....blob
282c0 5f 6f 70 65 6e 5f 6f 75 74 3a 0d 0a 20 20 69 66  _open_out:..  if
282c1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
282c2 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  && db->mallocFai
282c3 6c 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a  led==0 ){..    *
282c4 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65  ppBlob = (sqlite
282c5 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0d  3_blob *)pBlob;.
282c6 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69  .  }else{..    i
282c7 66 28 20 70 42 6c 6f 62 20 26 26 20 70 42 6c 6f  f( pBlob && pBlo
282c8 62 2d 3e 70 53 74 6d 74 20 29 20 73 71 6c 69 74  b->pStmt ) sqlit
282c9 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28  e3VdbeFinalize((
282ca 56 64 62 65 20 2a 29 70 42 6c 6f 62 2d 3e 70 53  Vdbe *)pBlob->pS
282cb 74 6d 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  tmt);..    sqlit
282cc 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 42 6c  e3DbFree(db, pBl
282cd 6f 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  ob);..  }..  sql
282ce 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
282cf 2c 20 28 7a 45 72 72 20 3f 20 22 25 73 22 20 3a  , (zErr ? "%s" :
282d0 20 30 29 2c 20 7a 45 72 72 29 3b 0d 0a 20 20 73   0), zErr);..  s
282d1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
282d2 20 7a 45 72 72 29 3b 0d 0a 20 20 73 71 6c 69 74   zErr);..  sqlit
282d3 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
282d4 70 50 61 72 73 65 29 3b 0d 0a 20 20 72 63 20 3d  pParse);..  rc =
282d5 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
282d6 64 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69  db, rc);..  sqli
282d7 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
282d8 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72  db->mutex);..  r
282d9 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
282da 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62  /*..** Close a b
282db 6c 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20  lob handle that 
282dc 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63  was previously c
282dd 72 65 61 74 65 64 20 75 73 69 6e 67 0d 0a 2a 2a  reated using..**
282de 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70   sqlite3_blob_op
282df 65 6e 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  en()...*/..SQLIT
282e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
282e1 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c  3_blob_close(sql
282e2 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62  ite3_blob *pBlob
282e3 29 7b 0d 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  ){..  Incrblob *
282e4 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
282e5 70 42 6c 6f 62 3b 0d 0a 20 20 69 6e 74 20 72 63  pBlob;..  int rc
282e6 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
282e7 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d  ;....  if( p ){.
282e8 0a 20 20 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b  .    db = p->db;
282e9 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ..    sqlite3_mu
282ea 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
282eb 74 65 78 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  tex);..    rc = 
282ec 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
282ed 28 70 2d 3e 70 53 74 6d 74 29 3b 0d 0a 20 20 20  (p->pStmt);..   
282ee 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
282ef 62 2c 20 70 29 3b 0d 0a 20 20 20 20 73 71 6c 69  b, p);..    sqli
282f0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
282f1 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 7d  db->mutex);..  }
282f2 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20  else{..    rc = 
282f3 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d  SQLITE_OK;..  }.
282f4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
282f5 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 65 72 66 6f  ..../*..** Perfo
282f6 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69  rm a read or wri
282f7 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20  te operation on 
282f8 61 20 62 6c 6f 62 0d 0a 2a 2f 0d 0a 73 74 61 74  a blob..*/..stat
282f9 69 63 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57  ic int blobReadW
282fa 72 69 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33  rite(..  sqlite3
282fb 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0d 0a  _blob *pBlob, ..
282fc 20 20 76 6f 69 64 20 2a 7a 2c 20 0d 0a 20 20 69    void *z, ..  i
282fd 6e 74 20 6e 2c 20 0d 0a 20 20 69 6e 74 20 69 4f  nt n, ..  int iO
282fe 66 66 73 65 74 2c 20 0d 0a 20 20 69 6e 74 20 28  ffset, ..  int (
282ff 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72  *xCall)(BtCursor
28300 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69  *, u32, u32, voi
28301 64 2a 29 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72  d*)..){..  int r
28302 63 3b 0d 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  c;..  Incrblob *
28303 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
28304 70 42 6c 6f 62 3b 0d 0a 20 20 56 64 62 65 20 2a  pBlob;..  Vdbe *
28305 76 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v;..  sqlite3 *d
28306 62 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 3d 3d 30  b;....  if( p==0
28307 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
28308 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20  _MISUSE_BKPT;.. 
28309 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20   db = p->db;..  
2830a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
2830b 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ter(db->mutex);.
2830c 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29 70 2d  .  v = (Vdbe*)p-
2830d 3e 70 53 74 6d 74 3b 0d 0a 0d 0a 20 20 69 66 28  >pStmt;....  if(
2830e 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 3c   n<0 || iOffset<
2830f 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e 29  0 || (iOffset+n)
28310 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0d 0a 20 20  >p->nByte ){..  
28311 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 20    /* Request is 
28312 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 65  out of range. Re
28313 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e 74  turn a transient
28314 20 65 72 72 6f 72 2e 20 2a 2f 0d 0a 20 20 20 20   error. */..    
28315 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
28316 52 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  R;..    sqlite3E
28317 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
28318 45 52 52 4f 52 2c 20 30 29 3b 0d 0a 20 20 7d 65  ERROR, 0);..  }e
28319 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0d  lse if( v==0 ){.
2831a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
2831b 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74   is no statement
2831c 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68   handle, then th
2831d 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61  e blob-handle ha
2831e 73 0d 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  s..    ** alread
2831f 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74  y been invalidat
28320 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  ed. Return SQLIT
28321 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20  E_ABORT in this 
28322 63 61 73 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  case...    */.. 
28323 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41     rc = SQLITE_A
28324 42 4f 52 54 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  BORT;..  }else{.
28325 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74  .    /* Call eit
28326 68 65 72 20 42 74 72 65 65 44 61 74 61 28 29 20  her BtreeData() 
28327 6f 72 20 42 74 72 65 65 50 75 74 44 61 74 61 28  or BtreePutData(
28328 29 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f  ). If SQLITE_ABO
28329 52 54 20 69 73 0d 0a 20 20 20 20 2a 2a 20 72 65  RT is..    ** re
2832a 74 75 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70  turned, clean-up
2832b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68   the statement h
2832c 61 6e 64 6c 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  andle...    */..
2832d 20 20 20 20 61 73 73 65 72 74 28 20 64 62 20 3d      assert( db =
2832e 3d 20 76 2d 3e 64 62 20 29 3b 0d 0a 20 20 20 20  = v->db );..    
2832f 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
28330 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29  rCursor(p->pCsr)
28331 3b 0d 0a 20 20 20 20 72 63 20 3d 20 78 43 61 6c  ;..    rc = xCal
28332 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 66 73  l(p->pCsr, iOffs
28333 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c 20 6e  et+p->iOffset, n
28334 2c 20 7a 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  , z);..    sqlit
28335 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
28336 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0d 0a 20 20  or(p->pCsr);..  
28337 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
28338 5f 41 42 4f 52 54 20 29 7b 0d 0a 20 20 20 20 20  _ABORT ){..     
28339 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
2833a 6c 69 7a 65 28 76 29 3b 0d 0a 20 20 20 20 20 20  lize(v);..      
2833b 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0d 0a 20  p->pStmt = 0;.. 
2833c 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2833d 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
2833e 63 3b 0d 0a 20 20 20 20 20 20 76 2d 3e 72 63 20  c;..      v->rc 
2833f 3d 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  = rc;..    }..  
28340 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  }..  rc = sqlite
28341 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
28342 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
28343 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
28344 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  ex);..  return r
28345 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
28346 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
28347 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0d 0a 2a   blob handle...*
28348 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
28349 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  t sqlite3_blob_r
2834a 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ead(sqlite3_blob
2834b 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a   *pBlob, void *z
2834c 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66  , int n, int iOf
2834d 66 73 65 74 29 7b 0d 0a 20 20 72 65 74 75 72 6e  fset){..  return
2834e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70   blobReadWrite(p
2834f 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66  Blob, z, n, iOff
28350 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65  set, sqlite3Btre
28351 65 44 61 74 61 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  eData);..}..../*
28352 0d 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  ..** Write data 
28353 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  to a blob handle
28354 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
28355 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
28356 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33  ob_write(sqlite3
28357 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f  _blob *pBlob, co
28358 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74  nst void *z, int
28359 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   n, int iOffset)
2835a 7b 0d 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62  {..  return blob
2835b 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c  ReadWrite(pBlob,
2835c 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69   (void *)z, n, i
2835d 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42  Offset, sqlite3B
2835e 74 72 65 65 50 75 74 44 61 74 61 29 3b 0d 0a 7d  treePutData);..}
2835f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 51 75 65 72 79  ..../*..** Query
28360 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 66   a blob handle f
28361 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  or the size of t
28362 68 65 20 64 61 74 61 2e 0d 0a 2a 2a 0d 0a 2a 2a  he data...**..**
28363 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e 6e 42   The Incrblob.nB
28364 79 74 65 20 66 69 65 6c 64 20 69 73 20 66 69 78  yte field is fix
28365 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74  ed for the lifet
28366 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63 72 62  ime of the Incrb
28367 6c 6f 62 0d 0a 2a 2a 20 73 6f 20 6e 6f 20 6d 75  lob..** so no mu
28368 74 65 78 20 69 73 20 72 65 71 75 69 72 65 64 20  tex is required 
28369 66 6f 72 20 61 63 63 65 73 73 2e 0d 0a 2a 2f 0d  for access...*/.
2836a 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2836b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
2836c 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  es(sqlite3_blob 
2836d 2a 70 42 6c 6f 62 29 7b 0d 0a 20 20 49 6e 63 72  *pBlob){..  Incr
2836e 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
2836f 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0d 0a 20 20  lob *)pBlob;..  
28370 72 65 74 75 72 6e 20 28 70 20 26 26 20 70 2d 3e  return (p && p->
28371 70 53 74 6d 74 29 20 3f 20 70 2d 3e 6e 42 79 74  pStmt) ? p->nByt
28372 65 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  e : 0;..}..../*.
28373 0a 2a 2a 20 4d 6f 76 65 20 61 6e 20 65 78 69 73  .** Move an exis
28374 74 69 6e 67 20 62 6c 6f 62 20 68 61 6e 64 6c 65  ting blob handle
28375 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 64   to point to a d
28376 69 66 66 65 72 65 6e 74 20 72 6f 77 20 6f 66 20  ifferent row of 
28377 74 68 65 20 73 61 6d 65 0d 0a 2a 2a 20 64 61 74  the same..** dat
28378 61 62 61 73 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a  abase table...**
28379 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  ..** If an error
2837a 20 6f 63 63 75 72 73 2c 20 6f 72 20 69 66 20 74   occurs, or if t
2837b 68 65 20 73 70 65 63 69 66 69 65 64 20 72 6f 77  he specified row
2837c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
2837d 6f 72 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a 20  or does not..** 
2837e 63 6f 6e 74 61 69 6e 20 61 20 62 6c 6f 62 20 6f  contain a blob o
2837f 72 20 74 65 78 74 20 76 61 6c 75 65 2c 20 74 68  r text value, th
28380 65 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  en an error code
28381 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
28382 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73   the..** databas
28383 65 20 68 61 6e 64 6c 65 20 65 72 72 6f 72 20 63  e handle error c
28384 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20  ode and message 
28385 73 65 74 2e 20 49 66 20 74 68 69 73 20 68 61 70  set. If this hap
28386 70 65 6e 73 2c 20 74 68 65 6e 20 61 6c 6c 20 0d  pens, then all .
28387 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63  .** subsequent c
28388 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
28389 62 6c 6f 62 5f 78 78 78 28 29 20 66 75 6e 63 74  blob_xxx() funct
2838a 69 6f 6e 73 20 28 65 78 63 65 70 74 20 62 6c 6f  ions (except blo
2838b 62 5f 63 6c 6f 73 65 28 29 29 20 0d 0a 2a 2a 20  b_close()) ..** 
2838c 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74 75  immediately retu
2838d 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e  rn SQLITE_ABORT.
2838e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
2838f 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
28390 62 5f 72 65 6f 70 65 6e 28 73 71 6c 69 74 65 33  b_reopen(sqlite3
28391 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 73 71  _blob *pBlob, sq
28392 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
28393 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  ){..  int rc;.. 
28394 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
28395 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
28396 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
28397 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20  ;....  if( p==0 
28398 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
28399 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20  MISUSE_BKPT;..  
2839a 64 62 20 3d 20 70 2d 3e 64 62 3b 0d 0a 20 20 73  db = p->db;..  s
2839b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2839c 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a  er(db->mutex);..
2839d 0d 0a 20 20 69 66 28 20 70 2d 3e 70 53 74 6d 74  ..  if( p->pStmt
2839e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  ==0 ){..    /* I
2839f 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 74  f there is no st
283a0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20  atement handle, 
283a1 74 68 65 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61  then the blob-ha
283a2 6e 64 6c 65 20 68 61 73 0d 0a 20 20 20 20 2a 2a  ndle has..    **
283a3 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e   already been in
283a4 76 61 6c 69 64 61 74 65 64 2e 20 52 65 74 75 72  validated. Retur
283a5 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69  n SQLITE_ABORT i
283a6 6e 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 20 20  n this case...  
283a7 20 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 53    */..    rc = S
283a8 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0d 0a 20 20  QLITE_ABORT;..  
283a9 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 63 68 61 72  }else{..    char
283aa 20 2a 7a 45 72 72 3b 0d 0a 20 20 20 20 72 63 20   *zErr;..    rc 
283ab 3d 20 62 6c 6f 62 53 65 65 6b 54 6f 52 6f 77 28  = blobSeekToRow(
283ac 70 2c 20 69 52 6f 77 2c 20 26 7a 45 72 72 29 3b  p, iRow, &zErr);
283ad 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
283ae 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
283af 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
283b0 62 2c 20 72 63 2c 20 28 7a 45 72 72 20 3f 20 22  b, rc, (zErr ? "
283b1 25 73 22 20 3a 20 30 29 2c 20 7a 45 72 72 29 3b  %s" : 0), zErr);
283b2 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
283b3 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
283b4 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73  ..    }..    ass
283b5 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
283b6 53 43 48 45 4d 41 20 29 3b 0d 0a 20 20 7d 0d 0a  SCHEMA );..  }..
283b7 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
283b8 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
283b9 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ..  assert( rc==
283ba 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
283bb 70 53 74 6d 74 3d 3d 30 20 29 3b 0d 0a 20 20 73  pStmt==0 );..  s
283bc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
283bd 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a  ve(db->mutex);..
283be 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
283bf 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66  ...#endif /* #if
283c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
283c1 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0d 0a 0d 0a  _INCRBLOB */....
283c2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
283c3 45 6e 64 20 6f 66 20 76 64 62 65 62 6c 6f 62 2e  End of vdbeblob.
283c4 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
283c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
283c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
283c7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
283c8 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
283c9 73 6f 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  sort.c *********
283ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
283cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
283cc 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 31 31 20 4a 75  ../*..** 2011 Ju
283cd 6c 79 20 39 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ly 9..**..** The
283ce 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
283cf 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
283d0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
283d1 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
283d2 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
283d3 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
283d4 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
283d5 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
283d6 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
283d7 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
283d8 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
283d9 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
283da 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
283db 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
283dc 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
283dd 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
283de 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
283df 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
283e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
283e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
283e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
283e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a  ************..**
283e4 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
283e5 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ins code for the
283e6 20 56 64 62 65 53 6f 72 74 65 72 20 6f 62 6a 65   VdbeSorter obje
283e7 63 74 2c 20 75 73 65 64 20 69 6e 20 63 6f 6e 63  ct, used in conc
283e8 65 72 74 20 77 69 74 68 0d 0a 2a 2a 20 61 20 56  ert with..** a V
283e9 64 62 65 43 75 72 73 6f 72 20 74 6f 20 73 6f 72  dbeCursor to sor
283ea 74 20 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20  t large numbers 
283eb 6f 66 20 6b 65 79 73 20 28 61 73 20 6d 61 79 20  of keys (as may 
283ec 62 65 20 72 65 71 75 69 72 65 64 2c 20 66 6f 72  be required, for
283ed 0d 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 62 79  ..** example, by
283ee 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74   CREATE INDEX st
283ef 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 61 62 6c  atements on tabl
283f0 65 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  es too large to 
283f1 66 69 74 20 69 6e 20 6d 61 69 6e 0d 0a 2a 2a 20  fit in main..** 
283f2 6d 65 6d 6f 72 79 29 2e 0d 0a 2a 2f 0d 0a 0d 0a  memory)...*/....
283f3 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
283f4 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54  _OMIT_MERGE_SORT
283f5 0d 0a 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75  ....typedef stru
283f6 63 74 20 56 64 62 65 53 6f 72 74 65 72 49 74 65  ct VdbeSorterIte
283f7 72 20 56 64 62 65 53 6f 72 74 65 72 49 74 65 72  r VdbeSorterIter
283f8 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
283f9 74 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20 53  t SorterRecord S
283fa 6f 72 74 65 72 52 65 63 6f 72 64 3b 0d 0a 0d 0a  orterRecord;....
283fb 2f 2a 0d 0a 2a 2a 20 4e 4f 54 45 53 20 4f 4e 20  /*..** NOTES ON 
283fc 44 41 54 41 20 53 54 52 55 43 54 55 52 45 20 55  DATA STRUCTURE U
283fd 53 45 44 20 46 4f 52 20 4e 2d 57 41 59 20 4d 45  SED FOR N-WAY ME
283fe 52 47 45 53 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 41 73  RGES:..**..** As
283ff 20 6b 65 79 73 20 61 72 65 20 61 64 64 65 64 20   keys are added 
28400 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2c 20 74  to the sorter, t
28401 68 65 79 20 61 72 65 20 77 72 69 74 74 65 6e 20  hey are written 
28402 74 6f 20 64 69 73 6b 20 69 6e 20 61 20 73 65 72  to disk in a ser
28403 69 65 73 0d 0a 2a 2a 20 6f 66 20 73 6f 72 74 65  ies..** of sorte
28404 64 20 70 61 63 6b 65 64 2d 6d 65 6d 6f 72 79 2d  d packed-memory-
28405 61 72 72 61 79 73 20 28 50 4d 41 73 29 2e 20 54  arrays (PMAs). T
28406 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20  he size of each 
28407 50 4d 41 20 69 73 20 72 6f 75 67 68 6c 79 0d 0a  PMA is roughly..
28408 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  ** the same as t
28409 68 65 20 63 61 63 68 65 2d 73 69 7a 65 20 61 6c  he cache-size al
2840a 6c 6f 77 65 64 20 66 6f 72 20 74 65 6d 70 6f 72  lowed for tempor
2840b 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 20 49  ary databases. I
2840c 6e 20 6f 72 64 65 72 0d 0a 2a 2a 20 74 6f 20 61  n order..** to a
2840d 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20  llow the caller 
2840e 74 6f 20 65 78 74 72 61 63 74 20 6b 65 79 73 20  to extract keys 
2840f 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20  from the sorter 
28410 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2c  in sorted order,
28411 0d 0a 2a 2a 20 61 6c 6c 20 50 4d 41 73 20 63 75  ..** all PMAs cu
28412 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 6f  rrently stored o
28413 6e 20 64 69 73 6b 20 6d 75 73 74 20 62 65 20 6d  n disk must be m
28414 65 72 67 65 64 20 74 6f 67 65 74 68 65 72 2e 20  erged together. 
28415 54 68 69 73 20 63 6f 6d 6d 65 6e 74 0d 0a 2a 2a  This comment..**
28416 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 64   describes the d
28417 61 74 61 20 73 74 72 75 63 74 75 72 65 20 75 73  ata structure us
28418 65 64 20 74 6f 20 64 6f 20 73 6f 2e 20 54 68 65  ed to do so. The
28419 20 73 74 72 75 63 74 75 72 65 20 73 75 70 70 6f   structure suppo
2841a 72 74 73 20 0d 0a 2a 2a 20 6d 65 72 67 69 6e 67  rts ..** merging
2841b 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61   any number of a
2841c 72 72 61 79 73 20 69 6e 20 61 20 73 69 6e 67 6c  rrays in a singl
2841d 65 20 70 61 73 73 20 77 69 74 68 20 6e 6f 20 72  e pass with no r
2841e 65 64 75 6e 64 61 6e 74 20 63 6f 6d 70 61 72 69  edundant compari
2841f 73 6f 6e 20 0d 0a 2a 2a 20 6f 70 65 72 61 74 69  son ..** operati
28420 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ons...**..** The
28421 20 61 49 74 65 72 5b 5d 20 61 72 72 61 79 20 63   aIter[] array c
28422 6f 6e 74 61 69 6e 73 20 61 6e 20 69 74 65 72 61  ontains an itera
28423 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f 66 20  tor for each of 
28424 74 68 65 20 50 4d 41 73 20 62 65 69 6e 67 20 6d  the PMAs being m
28425 65 72 67 65 64 2e 0d 0a 2a 2a 20 41 6e 20 61 49  erged...** An aI
28426 74 65 72 5b 5d 20 69 74 65 72 61 74 6f 72 20 65  ter[] iterator e
28427 69 74 68 65 72 20 70 6f 69 6e 74 73 20 74 6f 20  ither points to 
28428 61 20 76 61 6c 69 64 20 6b 65 79 20 6f 72 20 65  a valid key or e
28429 6c 73 65 20 69 73 20 61 74 20 45 4f 46 2e 20 46  lse is at EOF. F
2842a 6f 72 20 0d 0a 2a 2a 20 74 68 65 20 70 75 72 70  or ..** the purp
2842b 6f 73 65 73 20 6f 66 20 74 68 65 20 70 61 72 61  oses of the para
2842c 67 72 61 70 68 73 20 62 65 6c 6f 77 2c 20 77 65  graphs below, we
2842d 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
2842e 20 61 72 72 61 79 20 69 73 20 61 63 74 75 61 6c   array is actual
2842f 6c 79 20 0d 0a 2a 2a 20 4e 20 65 6c 65 6d 65 6e  ly ..** N elemen
28430 74 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72  ts in size, wher
28431 65 20 4e 20 69 73 20 74 68 65 20 73 6d 61 6c 6c  e N is the small
28432 65 73 74 20 70 6f 77 65 72 20 6f 66 20 32 20 67  est power of 2 g
28433 72 65 61 74 65 72 20 74 6f 20 6f 72 20 65 71 75  reater to or equ
28434 61 6c 20 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 6e  al ..** to the n
28435 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 6f  umber of iterato
28436 72 73 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e  rs being merged.
28437 20 54 68 65 20 65 78 74 72 61 20 61 49 74 65 72   The extra aIter
28438 5b 5d 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20  [] elements are 
28439 0d 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20  ..** treated as 
2843a 69 66 20 74 68 65 79 20 61 72 65 20 65 6d 70 74  if they are empt
2843b 79 20 28 61 6c 77 61 79 73 20 61 74 20 45 4f 46  y (always at EOF
2843c 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  )...**..** The a
2843d 54 72 65 65 5b 5d 20 61 72 72 61 79 20 69 73 20  Tree[] array is 
2843e 61 6c 73 6f 20 4e 20 65 6c 65 6d 65 6e 74 73 20  also N elements 
2843f 69 6e 20 73 69 7a 65 2e 20 54 68 65 20 76 61 6c  in size. The val
28440 75 65 20 6f 66 20 4e 20 69 73 20 73 74 6f 72 65  ue of N is store
28441 64 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 56 64 62  d in..** the Vdb
28442 65 53 6f 72 74 65 72 2e 6e 54 72 65 65 20 76 61  eSorter.nTree va
28443 72 69 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  riable...**..** 
28444 54 68 65 20 66 69 6e 61 6c 20 28 4e 2f 32 29 20  The final (N/2) 
28445 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 54 72 65  elements of aTre
28446 65 5b 5d 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  e[] contain the 
28447 72 65 73 75 6c 74 73 20 6f 66 20 63 6f 6d 70 61  results of compa
28448 72 69 6e 67 0d 0a 2a 2a 20 70 61 69 72 73 20 6f  ring..** pairs o
28449 66 20 69 74 65 72 61 74 6f 72 20 6b 65 79 73 20  f iterator keys 
2844a 74 6f 67 65 74 68 65 72 2e 20 45 6c 65 6d 65 6e  together. Elemen
2844b 74 20 69 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  t i contains the
2844c 20 72 65 73 75 6c 74 20 6f 66 20 0d 0a 2a 2a 20   result of ..** 
2844d 63 6f 6d 70 61 72 69 6e 67 20 61 49 74 65 72 5b  comparing aIter[
2844e 32 2a 69 2d 4e 5d 20 61 6e 64 20 61 49 74 65 72  2*i-N] and aIter
2844f 5b 32 2a 69 2d 4e 2b 31 5d 2e 20 57 68 69 63 68  [2*i-N+1]. Which
28450 65 76 65 72 20 6b 65 79 20 69 73 20 73 6d 61 6c  ever key is smal
28451 6c 65 72 2c 20 74 68 65 0d 0a 2a 2a 20 61 54 72  ler, the..** aTr
28452 65 65 20 65 6c 65 6d 65 6e 74 20 69 73 20 73 65  ee element is se
28453 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  t to the index o
28454 66 20 69 74 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 46  f it. ..**..** F
28455 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
28456 6f 66 20 74 68 69 73 20 63 6f 6d 70 61 72 69 73  of this comparis
28457 6f 6e 2c 20 45 4f 46 20 69 73 20 63 6f 6e 73 69  on, EOF is consi
28458 64 65 72 65 64 20 67 72 65 61 74 65 72 20 74 68  dered greater th
28459 61 6e 20 61 6e 79 0d 0a 2a 2a 20 6f 74 68 65 72  an any..** other
2845a 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
2845b 68 65 20 6b 65 79 73 20 61 72 65 20 65 71 75 61  he keys are equa
2845c 6c 20 28 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65  l (only possible
2845d 20 77 69 74 68 20 74 77 6f 20 45 4f 46 0d 0a 2a   with two EOF..*
2845e 2a 20 76 61 6c 75 65 73 29 2c 20 69 74 20 64 6f  * values), it do
2845f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 77 68 69  esn't matter whi
28460 63 68 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72  ch index is stor
28461 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ed...**..** The 
28462 28 4e 2f 34 29 20 65 6c 65 6d 65 6e 74 73 20 6f  (N/4) elements o
28463 66 20 61 54 72 65 65 5b 5d 20 74 68 61 74 20 70  f aTree[] that p
28464 72 65 63 65 65 64 20 74 68 65 20 66 69 6e 61 6c  receed the final
28465 20 28 4e 2f 32 29 20 64 65 73 63 72 69 62 65 64   (N/2) described
28466 20 0d 0a 2a 2a 20 61 62 6f 76 65 20 63 6f 6e 74   ..** above cont
28467 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  ains the index o
28468 66 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6f  f the smallest o
28469 66 20 65 61 63 68 20 62 6c 6f 63 6b 20 6f 66 20  f each block of 
2846a 34 20 69 74 65 72 61 74 6f 72 73 2e 0d 0a 2a 2a  4 iterators...**
2846b 20 41 6e 64 20 73 6f 20 6f 6e 2e 20 53 6f 20 74   And so on. So t
2846c 68 61 74 20 61 54 72 65 65 5b 31 5d 20 63 6f 6e  hat aTree[1] con
2846d 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20  tains the index 
2846e 6f 66 20 74 68 65 20 69 74 65 72 61 74 6f 72 20  of the iterator 
2846f 74 68 61 74 20 0d 0a 2a 2a 20 63 75 72 72 65 6e  that ..** curren
28470 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  tly points to th
28471 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 76  e smallest key v
28472 61 6c 75 65 2e 20 61 54 72 65 65 5b 30 5d 20 69  alue. aTree[0] i
28473 73 20 75 6e 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a  s unused...**..*
28474 2a 20 45 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a  * Example:..**..
28475 2a 2a 20 20 20 20 20 61 49 74 65 72 5b 30 5d 20  **     aIter[0] 
28476 2d 3e 20 42 61 6e 61 6e 61 0d 0a 2a 2a 20 20 20  -> Banana..**   
28477 20 20 61 49 74 65 72 5b 31 5d 20 2d 3e 20 46 65    aIter[1] -> Fe
28478 69 6a 6f 61 0d 0a 2a 2a 20 20 20 20 20 61 49 74  ijoa..**     aIt
28479 65 72 5b 32 5d 20 2d 3e 20 45 6c 64 65 72 62 65  er[2] -> Elderbe
2847a 72 72 79 0d 0a 2a 2a 20 20 20 20 20 61 49 74 65  rry..**     aIte
2847b 72 5b 33 5d 20 2d 3e 20 43 75 72 72 61 6e 74 0d  r[3] -> Currant.
2847c 0a 2a 2a 20 20 20 20 20 61 49 74 65 72 5b 34 5d  .**     aIter[4]
2847d 20 2d 3e 20 47 72 61 70 65 66 72 75 69 74 0d 0a   -> Grapefruit..
2847e 2a 2a 20 20 20 20 20 61 49 74 65 72 5b 35 5d 20  **     aIter[5] 
2847f 2d 3e 20 41 70 70 6c 65 0d 0a 2a 2a 20 20 20 20  -> Apple..**    
28480 20 61 49 74 65 72 5b 36 5d 20 2d 3e 20 44 75 72   aIter[6] -> Dur
28481 69 61 6e 0d 0a 2a 2a 20 20 20 20 20 61 49 74 65  ian..**     aIte
28482 72 5b 37 5d 20 2d 3e 20 45 4f 46 0d 0a 2a 2a 0d  r[7] -> EOF..**.
28483 0a 2a 2a 20 20 20 20 20 61 54 72 65 65 5b 5d 20  .**     aTree[] 
28484 3d 20 7b 20 58 2c 20 35 20 20 20 30 2c 20 35 20  = { X, 5   0, 5 
28485 20 20 20 30 2c 20 33 2c 20 35 2c 20 36 20 7d 0d     0, 3, 5, 6 }.
28486 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 75 72 72  .**..** The curr
28487 65 6e 74 20 65 6c 65 6d 65 6e 74 20 69 73 20 22  ent element is "
28488 41 70 70 6c 65 22 20 28 74 68 65 20 76 61 6c 75  Apple" (the valu
28489 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 64  e of the key ind
2848a 69 63 61 74 65 64 20 62 79 20 0d 0a 2a 2a 20 69  icated by ..** i
2848b 74 65 72 61 74 6f 72 20 35 29 2e 20 57 68 65 6e  terator 5). When
2848c 20 74 68 65 20 4e 65 78 74 28 29 20 6f 70 65 72   the Next() oper
2848d 61 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64  ation is invoked
2848e 2c 20 69 74 65 72 61 74 6f 72 20 35 20 77 69 6c  , iterator 5 wil
2848f 6c 0d 0a 2a 2a 20 62 65 20 61 64 76 61 6e 63 65  l..** be advance
28490 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6b 65  d to the next ke
28491 79 20 69 6e 20 69 74 73 20 73 65 67 6d 65 6e 74  y in its segment
28492 2e 20 53 61 79 20 74 68 65 20 6e 65 78 74 20 6b  . Say the next k
28493 65 79 20 69 73 0d 0a 2a 2a 20 22 45 67 67 70 6c  ey is..** "Eggpl
28494 61 6e 74 22 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ant":..**..**   
28495 20 20 61 49 74 65 72 5b 35 5d 20 2d 3e 20 45 67    aIter[5] -> Eg
28496 67 70 6c 61 6e 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54  gplant..**..** T
28497 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61  he contents of a
28498 54 72 65 65 5b 5d 20 61 72 65 20 75 70 64 61 74  Tree[] are updat
28499 65 64 20 66 69 72 73 74 20 62 79 20 63 6f 6d 70  ed first by comp
2849a 61 72 69 6e 67 20 74 68 65 20 6e 65 77 20 69 74  aring the new it
2849b 65 72 61 74 6f 72 0d 0a 2a 2a 20 35 20 6b 65 79  erator..** 5 key
2849c 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
2849d 6b 65 79 20 6f 66 20 69 74 65 72 61 74 6f 72 20  key of iterator 
2849e 34 20 28 73 74 69 6c 6c 20 22 47 72 61 70 65 66  4 (still "Grapef
2849f 72 75 69 74 22 29 2e 20 54 68 65 20 69 74 65 72  ruit"). The iter
284a0 61 74 6f 72 0d 0a 2a 2a 20 35 20 76 61 6c 75 65  ator..** 5 value
284a1 20 69 73 20 73 74 69 6c 6c 20 73 6d 61 6c 6c 65   is still smalle
284a2 72 2c 20 73 6f 20 61 54 72 65 65 5b 36 5d 20 69  r, so aTree[6] i
284a3 73 20 73 65 74 20 74 6f 20 35 2e 20 41 6e 64 20  s set to 5. And 
284a4 73 6f 20 6f 6e 20 75 70 20 74 68 65 20 74 72 65  so on up the tre
284a5 65 2e 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  e...** The value
284a6 20 6f 66 20 69 74 65 72 61 74 6f 72 20 36 20 2d   of iterator 6 -
284a7 20 22 44 75 72 69 61 6e 22 20 2d 20 69 73 20 6e   "Durian" - is n
284a8 6f 77 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ow smaller than 
284a9 74 68 61 74 20 6f 66 20 69 74 65 72 61 74 6f 72  that of iterator
284aa 0d 0a 2a 2a 20 35 2c 20 73 6f 20 61 54 72 65 65  ..** 5, so aTree
284ab 5b 33 5d 20 69 73 20 73 65 74 20 74 6f 20 36 2e  [3] is set to 6.
284ac 20 4b 65 79 20 30 20 69 73 20 73 6d 61 6c 6c 65   Key 0 is smalle
284ad 72 20 74 68 61 6e 20 6b 65 79 20 36 20 28 42 61  r than key 6 (Ba
284ae 6e 61 6e 61 3c 44 75 72 69 61 6e 29 2c 0d 0a 2a  nana<Durian),..*
284af 2a 20 73 6f 20 74 68 65 20 76 61 6c 75 65 20 77  * so the value w
284b0 72 69 74 74 65 6e 20 69 6e 74 6f 20 65 6c 65 6d  ritten into elem
284b1 65 6e 74 20 31 20 6f 66 20 74 68 65 20 61 72 72  ent 1 of the arr
284b2 61 79 20 69 73 20 30 2e 20 41 73 20 66 6f 6c 6c  ay is 0. As foll
284b3 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ows:..**..**    
284b4 20 61 54 72 65 65 5b 5d 20 3d 20 7b 20 58 2c 20   aTree[] = { X, 
284b5 30 20 20 20 30 2c 20 36 20 20 20 20 30 2c 20 33  0   0, 6    0, 3
284b6 2c 20 35 2c 20 36 20 7d 0d 0a 2a 2a 0d 0a 2a 2a  , 5, 6 }..**..**
284b7 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
284b8 20 65 61 63 68 20 74 69 6d 65 20 77 65 20 61 64   each time we ad
284b9 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
284ba 74 20 73 6f 72 74 65 72 20 65 6c 65 6d 65 6e 74  t sorter element
284bb 2c 20 6c 6f 67 32 28 4e 29 0d 0a 2a 2a 20 6b 65  , log2(N)..** ke
284bc 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  y comparison ope
284bd 72 61 74 69 6f 6e 73 20 61 72 65 20 72 65 71 75  rations are requ
284be 69 72 65 64 2c 20 77 68 65 72 65 20 4e 20 69 73  ired, where N is
284bf 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
284c0 65 67 6d 65 6e 74 73 0d 0a 2a 2a 20 62 65 69 6e  egments..** bein
284c1 67 20 6d 65 72 67 65 64 20 28 72 6f 75 6e 64 65  g merged (rounde
284c2 64 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78 74  d up to the next
284c3 20 70 6f 77 65 72 20 6f 66 20 32 29 2e 0d 0a 2a   power of 2)...*
284c4 2f 0d 0a 73 74 72 75 63 74 20 56 64 62 65 53 6f  /..struct VdbeSo
284c5 72 74 65 72 20 7b 0d 0a 20 20 69 6e 74 20 6e 49  rter {..  int nI
284c6 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20 20  nMemory;        
284c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
284c8 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 70 52 65  rent size of pRe
284c9 63 6f 72 64 20 6c 69 73 74 20 61 73 20 50 4d 41  cord list as PMA
284ca 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 72 65 65   */..  int nTree
284cb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
284cc 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 73         /* Used s
284cd 69 7a 65 20 6f 66 20 61 54 72 65 65 2f 61 49 74  ize of aTree/aIt
284ce 65 72 20 28 70 6f 77 65 72 20 6f 66 20 32 29 20  er (power of 2) 
284cf 2a 2f 0d 0a 20 20 56 64 62 65 53 6f 72 74 65 72  */..  VdbeSorter
284d0 49 74 65 72 20 2a 61 49 74 65 72 3b 20 20 20 20  Iter *aIter;    
284d1 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f        /* Array o
284d2 66 20 69 74 65 72 61 74 6f 72 73 20 74 6f 20 6d  f iterators to m
284d3 65 72 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  erge */..  int *
284d4 61 54 72 65 65 3b 20 20 20 20 20 20 20 20 20 20  aTree;          
284d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
284d6 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 69  rrent state of i
284d7 6e 63 72 65 6d 65 6e 74 61 6c 20 6d 65 72 67 65  ncremental merge
284d8 20 2a 2f 0d 0a 20 20 69 36 34 20 69 57 72 69 74   */..  i64 iWrit
284d9 65 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  eOff;           
284da 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
284db 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 77  t write offset w
284dc 69 74 68 69 6e 20 66 69 6c 65 20 70 54 65 6d 70  ithin file pTemp
284dd 31 20 2a 2f 0d 0a 20 20 69 36 34 20 69 52 65 61  1 */..  i64 iRea
284de 64 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  dOff;           
284df 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
284e0 6e 74 20 72 65 61 64 20 6f 66 66 73 65 74 20 77  nt read offset w
284e1 69 74 68 69 6e 20 66 69 6c 65 20 70 54 65 6d 70  ithin file pTemp
284e2 31 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  1 */..  sqlite3_
284e3 66 69 6c 65 20 2a 70 54 65 6d 70 31 3b 20 20 20  file *pTemp1;   
284e4 20 20 20 20 20 20 20 20 2f 2a 20 50 4d 41 20 66          /* PMA f
284e5 69 6c 65 20 31 20 2a 2f 0d 0a 20 20 69 6e 74 20  ile 1 */..  int 
284e6 6e 50 4d 41 3b 20 20 20 20 20 20 20 20 20 20 20  nPMA;           
284e7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
284e8 75 6d 62 65 72 20 6f 66 20 50 4d 41 73 20 73 74  umber of PMAs st
284e9 6f 72 65 64 20 69 6e 20 70 54 65 6d 70 31 20 2a  ored in pTemp1 *
284ea 2f 0d 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72  /..  SorterRecor
284eb 64 20 2a 70 52 65 63 6f 72 64 3b 20 20 20 20 20  d *pRecord;     
284ec 20 20 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20       /* Head of 
284ed 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 63 6f 72 64  in-memory record
284ee 20 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20   list */..  int 
284ef 6d 6e 50 6d 61 53 69 7a 65 3b 20 20 20 20 20 20  mnPmaSize;      
284f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
284f1 69 6e 69 6d 75 6d 20 50 4d 41 20 73 69 7a 65 2c  inimum PMA size,
284f2 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20   in bytes */..  
284f3 69 6e 74 20 6d 78 50 6d 61 53 69 7a 65 3b 20 20  int mxPmaSize;  
284f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284f5 2f 2a 20 4d 61 78 69 6d 75 6d 20 50 4d 41 20 73  /* Maximum PMA s
284f6 69 7a 65 2c 20 69 6e 20 62 79 74 65 73 2e 20 20  ize, in bytes.  
284f7 30 3d 3d 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0d 0a  0==no limit */..
284f8 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
284f9 20 2a 70 55 6e 70 61 63 6b 65 64 3b 20 20 20 20   *pUnpacked;    
284fa 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 75 6e 70    /* Used to unp
284fb 61 63 6b 20 6b 65 79 73 20 2a 2f 0d 0a 7d 3b 0d  ack keys */..};.
284fc 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
284fd 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 69 73 20  llowing type is 
284fe 61 6e 20 69 74 65 72 61 74 6f 72 20 66 6f 72 20  an iterator for 
284ff 61 20 50 4d 41 2e 20 49 74 20 63 61 63 68 65 73  a PMA. It caches
28500 20 74 68 65 20 63 75 72 72 65 6e 74 20 6b 65 79   the current key
28501 20 69 6e 20 0d 0a 2a 2a 20 76 61 72 69 61 62 6c   in ..** variabl
28502 65 73 20 6e 4b 65 79 2f 61 4b 65 79 2e 20 49 66  es nKey/aKey. If
28503 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 69 73   the iterator is
28504 20 61 74 20 45 4f 46 2c 20 70 46 69 6c 65 3d 3d   at EOF, pFile==
28505 30 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 56  0...*/..struct V
28506 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 7b 0d  dbeSorterIter {.
28507 0a 20 20 69 36 34 20 69 52 65 61 64 4f 66 66 3b  .  i64 iReadOff;
28508 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28509 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 65     /* Current re
2850a 61 64 20 6f 66 66 73 65 74 20 2a 2f 0d 0a 20 20  ad offset */..  
2850b 69 36 34 20 69 45 6f 66 3b 20 20 20 20 20 20 20  i64 iEof;       
2850c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2850d 2f 2a 20 31 20 62 79 74 65 20 70 61 73 74 20 45  /* 1 byte past E
2850e 4f 46 20 66 6f 72 20 74 68 69 73 20 69 74 65 72  OF for this iter
2850f 61 74 6f 72 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ator */..  sqlit
28510 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 20  e3_file *pFile; 
28511 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
28512 6c 65 20 69 74 65 72 61 74 6f 72 20 69 73 20 72  le iterator is r
28513 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0d 0a  eading from */..
28514 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20    int nAlloc;   
28515 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28516 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 73 70    /* Bytes of sp
28517 61 63 65 20 61 74 20 61 41 6c 6c 6f 63 20 2a 2f  ace at aAlloc */
28518 0d 0a 20 20 75 38 20 2a 61 41 6c 6c 6f 63 3b 20  ..  u8 *aAlloc; 
28519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2851a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
2851b 20 73 70 61 63 65 20 2a 2f 0d 0a 20 20 69 6e 74   space */..  int
2851c 20 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20   nKey;          
2851d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2851e 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
2851f 69 6e 20 6b 65 79 20 2a 2f 0d 0a 20 20 75 38 20  in key */..  u8 
28520 2a 61 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  *aKey;          
28521 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28522 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 65  Pointer to curre
28523 6e 74 20 6b 65 79 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  nt key */..};...
28524 0a 2f 2a 0d 0a 2a 2a 20 41 20 73 74 72 75 63 74  ./*..** A struct
28525 75 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20 73  ure to store a s
28526 69 6e 67 6c 65 20 72 65 63 6f 72 64 2e 20 41 6c  ingle record. Al
28527 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 63 6f  l in-memory reco
28528 72 64 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65  rds are connecte
28529 64 0d 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 69  d..** together i
2852a 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  nto a linked lis
2852b 74 20 68 65 61 64 65 64 20 61 74 20 56 64 62 65  t headed at Vdbe
2852c 53 6f 72 74 65 72 2e 70 52 65 63 6f 72 64 20 75  Sorter.pRecord u
2852d 73 69 6e 67 20 74 68 65 20 0d 0a 2a 2a 20 53 6f  sing the ..** So
2852e 72 74 65 72 52 65 63 6f 72 64 2e 70 4e 65 78 74  rterRecord.pNext
2852f 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a 73   pointer...*/..s
28530 74 72 75 63 74 20 53 6f 72 74 65 72 52 65 63 6f  truct SorterReco
28531 72 64 20 7b 0d 0a 20 20 76 6f 69 64 20 2a 70 56  rd {..  void *pV
28532 61 6c 3b 0d 0a 20 20 69 6e 74 20 6e 56 61 6c 3b  al;..  int nVal;
28533 0d 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64  ..  SorterRecord
28534 20 2a 70 4e 65 78 74 3b 0d 0a 7d 3b 0d 0a 0d 0a   *pNext;..};....
28535 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77  /* Minimum allow
28536 61 62 6c 65 20 76 61 6c 75 65 20 66 6f 72 20 74  able value for t
28537 68 65 20 56 64 62 65 53 6f 72 74 65 72 2e 6e 57  he VdbeSorter.nW
28538 6f 72 6b 69 6e 67 20 76 61 72 69 61 62 6c 65 20  orking variable 
28539 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 4f 52 54  */..#define SORT
2853a 45 52 5f 4d 49 4e 5f 57 4f 52 4b 49 4e 47 20 31  ER_MIN_WORKING 1
2853b 30 0d 0a 0d 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20  0..../* Maximum 
2853c 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e  number of segmen
2853d 74 73 20 74 6f 20 6d 65 72 67 65 20 69 6e 20 61  ts to merge in a
2853e 20 73 69 6e 67 6c 65 20 70 61 73 73 2e 20 2a 2f   single pass. */
2853f 0d 0a 23 64 65 66 69 6e 65 20 53 4f 52 54 45 52  ..#define SORTER
28540 5f 4d 41 58 5f 4d 45 52 47 45 5f 43 4f 55 4e 54  _MAX_MERGE_COUNT
28541 20 31 36 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72   16..../*..** Fr
28542 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 62 65  ee all memory be
28543 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 56  longing to the V
28544 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 6f 62  dbeSorterIter ob
28545 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74  ject passed as t
28546 68 65 20 73 65 63 6f 6e 64 0d 0a 2a 2a 20 61 72  he second..** ar
28547 67 75 6d 65 6e 74 2e 20 41 6c 6c 20 73 74 72 75  gument. All stru
28548 63 74 75 72 65 20 66 69 65 6c 64 73 20 61 72 65  cture fields are
28549 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 62 65 66   set to zero bef
2854a 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  ore returning...
2854b 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2854c 76 64 62 65 53 6f 72 74 65 72 49 74 65 72 5a 65  vdbeSorterIterZe
2854d 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ro(sqlite3 *db, 
2854e 56 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 2a  VdbeSorterIter *
2854f 70 49 74 65 72 29 7b 0d 0a 20 20 73 71 6c 69 74  pIter){..  sqlit
28550 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
28551 65 72 2d 3e 61 41 6c 6c 6f 63 29 3b 0d 0a 20 20  er->aAlloc);..  
28552 6d 65 6d 73 65 74 28 70 49 74 65 72 2c 20 30 2c  memset(pIter, 0,
28553 20 73 69 7a 65 6f 66 28 56 64 62 65 53 6f 72 74   sizeof(VdbeSort
28554 65 72 49 74 65 72 29 29 3b 0d 0a 7d 0d 0a 0d 0a  erIter));..}....
28555 2f 2a 0d 0a 2a 2a 20 41 64 76 61 6e 63 65 20 69  /*..** Advance i
28556 74 65 72 61 74 6f 72 20 70 49 74 65 72 20 74 6f  terator pIter to
28557 20 74 68 65 20 6e 65 78 74 20 6b 65 79 20 69 6e   the next key in
28558 20 69 74 73 20 50 4d 41 2e 20 52 65 74 75 72 6e   its PMA. Return
28559 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 0d 0a 2a   SQLITE_OK if..*
2855a 2a 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  * no error occur
2855b 73 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  s, or an SQLite 
2855c 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 6e  error code if on
2855d 65 20 64 6f 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  e does...*/..sta
2855e 74 69 63 20 69 6e 74 20 76 64 62 65 53 6f 72 74  tic int vdbeSort
2855f 65 72 49 74 65 72 4e 65 78 74 28 0d 0a 20 20 73  erIterNext(..  s
28560 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
28561 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28562 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
28563 65 20 28 66 6f 72 20 73 71 6c 69 74 65 33 44 62  e (for sqlite3Db
28564 4d 61 6c 6c 6f 63 28 29 20 29 20 2a 2f 0d 0a 20  Malloc() ) */.. 
28565 20 56 64 62 65 53 6f 72 74 65 72 49 74 65 72 20   VdbeSorterIter 
28566 2a 70 49 74 65 72 20 20 20 20 20 20 20 20 20 20  *pIter          
28567 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 74 6f 20   /* Iterator to 
28568 61 64 76 61 6e 63 65 20 2a 2f 0d 0a 29 7b 0d 0a  advance */..){..
28569 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
2856a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2856b 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65    /* Return Code
2856c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65 61 64   */..  int nRead
2856d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2856e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2856f 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 20 2a   of bytes read *
28570 2f 0d 0a 20 20 69 6e 74 20 6e 52 65 63 20 3d 20  /..  int nRec = 
28571 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
28572 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
28573 72 65 63 6f 72 64 20 69 6e 20 62 79 74 65 73 20  record in bytes 
28574 2a 2f 0d 0a 20 20 69 6e 74 20 69 4f 66 66 20 3d  */..  int iOff =
28575 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
28576 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
28577 20 73 65 72 69 61 6c 69 7a 65 64 20 73 69 7a 65   serialized size
28578 20 76 61 72 69 6e 74 20 69 6e 20 62 79 74 65 73   varint in bytes
28579 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   */....  assert(
2857a 20 70 49 74 65 72 2d 3e 69 45 6f 66 3e 3d 70 49   pIter->iEof>=pI
2857b 74 65 72 2d 3e 69 52 65 61 64 4f 66 66 20 29 3b  ter->iReadOff );
2857c 0d 0a 20 20 69 66 28 20 70 49 74 65 72 2d 3e 69  ..  if( pIter->i
2857d 45 6f 66 2d 70 49 74 65 72 2d 3e 69 52 65 61 64  Eof-pIter->iRead
2857e 4f 66 66 3e 35 20 29 7b 0d 0a 20 20 20 20 6e 52  Off>5 ){..    nR
2857f 65 61 64 20 3d 20 35 3b 0d 0a 20 20 7d 65 6c 73  ead = 5;..  }els
28580 65 7b 0d 0a 20 20 20 20 6e 52 65 61 64 20 3d 20  e{..    nRead = 
28581 28 69 6e 74 29 28 70 49 74 65 72 2d 3e 69 45 6f  (int)(pIter->iEo
28582 66 20 2d 20 70 49 74 65 72 2d 3e 69 52 65 61 64  f - pIter->iRead
28583 4f 66 66 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  Off);..  }..  if
28584 28 20 6e 52 65 61 64 3c 3d 30 20 29 7b 0d 0a 20  ( nRead<=0 ){.. 
28585 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 6e     /* This is an
28586 20 45 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 2a   EOF condition *
28587 2f 0d 0a 20 20 20 20 76 64 62 65 53 6f 72 74 65  /..    vdbeSorte
28588 72 49 74 65 72 5a 65 72 6f 28 64 62 2c 20 70 49  rIterZero(db, pI
28589 74 65 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  ter);..    retur
2858a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
2858b 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  }....  rc = sqli
2858c 74 65 33 4f 73 52 65 61 64 28 70 49 74 65 72 2d  te3OsRead(pIter-
2858d 3e 70 46 69 6c 65 2c 20 70 49 74 65 72 2d 3e 61  >pFile, pIter->a
2858e 41 6c 6c 6f 63 2c 20 6e 52 65 61 64 2c 20 70 49  Alloc, nRead, pI
2858f 74 65 72 2d 3e 69 52 65 61 64 4f 66 66 29 3b 0d  ter->iReadOff);.
28590 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
28591 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 4f 66  E_OK ){..    iOf
28592 66 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28  f = getVarint32(
28593 70 49 74 65 72 2d 3e 61 41 6c 6c 6f 63 2c 20 6e  pIter->aAlloc, n
28594 52 65 63 29 3b 0d 0a 20 20 20 20 69 66 28 20 28  Rec);..    if( (
28595 69 4f 66 66 2b 6e 52 65 63 29 3e 6e 52 65 61 64  iOff+nRec)>nRead
28596 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   ){..      int n
28597 52 65 61 64 32 3b 20 20 20 20 20 20 20 20 20 20  Read2;          
28598 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
28599 65 72 20 6f 66 20 65 78 74 72 61 20 62 79 74 65  er of extra byte
2859a 73 20 74 6f 20 72 65 61 64 20 2a 2f 0d 0a 20 20  s to read */..  
2859b 20 20 20 20 69 66 28 20 28 69 4f 66 66 2b 6e 52      if( (iOff+nR
2859c 65 63 29 3e 70 49 74 65 72 2d 3e 6e 41 6c 6c 6f  ec)>pIter->nAllo
2859d 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  c ){..        in
2859e 74 20 6e 4e 65 77 20 3d 20 70 49 74 65 72 2d 3e  t nNew = pIter->
2859f 6e 41 6c 6c 6f 63 2a 32 3b 0d 0a 20 20 20 20 20  nAlloc*2;..     
285a0 20 20 20 77 68 69 6c 65 28 20 28 69 4f 66 66 2b     while( (iOff+
285a1 6e 52 65 63 29 3e 6e 4e 65 77 20 29 20 6e 4e 65  nRec)>nNew ) nNe
285a2 77 20 3d 20 6e 4e 65 77 2a 32 3b 0d 0a 20 20 20  w = nNew*2;..   
285a3 20 20 20 20 20 70 49 74 65 72 2d 3e 61 41 6c 6c       pIter->aAll
285a4 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  oc = sqlite3DbRe
285a5 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20  allocOrFree(db, 
285a6 70 49 74 65 72 2d 3e 61 41 6c 6c 6f 63 2c 20 6e  pIter->aAlloc, n
285a7 4e 65 77 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  New);..        i
285a8 66 28 20 21 70 49 74 65 72 2d 3e 61 41 6c 6c 6f  f( !pIter->aAllo
285a9 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  c ) return SQLIT
285aa 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20  E_NOMEM;..      
285ab 20 20 70 49 74 65 72 2d 3e 6e 41 6c 6c 6f 63 20    pIter->nAlloc 
285ac 3d 20 6e 4e 65 77 3b 0d 0a 20 20 20 20 20 20 7d  = nNew;..      }
285ad 0d 0a 20 20 0d 0a 20 20 20 20 20 20 6e 52 65 61  ..  ..      nRea
285ae 64 32 20 3d 20 69 4f 66 66 20 2b 20 6e 52 65 63  d2 = iOff + nRec
285af 20 2d 20 6e 52 65 61 64 3b 0d 0a 20 20 20 20 20   - nRead;..     
285b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
285b1 65 61 64 28 0d 0a 20 20 20 20 20 20 20 20 20 20  ead(..          
285b2 70 49 74 65 72 2d 3e 70 46 69 6c 65 2c 20 26 70  pIter->pFile, &p
285b3 49 74 65 72 2d 3e 61 41 6c 6c 6f 63 5b 6e 52 65  Iter->aAlloc[nRe
285b4 61 64 5d 2c 20 6e 52 65 61 64 32 2c 20 70 49 74  ad], nRead2, pIt
285b5 65 72 2d 3e 69 52 65 61 64 4f 66 66 2b 6e 52 65  er->iReadOff+nRe
285b6 61 64 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20  ad..      );..  
285b7 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73    }..  }....  as
285b8 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
285b9 5f 4f 4b 20 7c 7c 20 6e 52 65 63 3e 30 20 29 3b  _OK || nRec>0 );
285ba 0d 0a 20 20 70 49 74 65 72 2d 3e 69 52 65 61 64  ..  pIter->iRead
285bb 4f 66 66 20 2b 3d 20 69 4f 66 66 2b 6e 52 65 63  Off += iOff+nRec
285bc 3b 0d 0a 20 20 70 49 74 65 72 2d 3e 6e 4b 65 79  ;..  pIter->nKey
285bd 20 3d 20 6e 52 65 63 3b 0d 0a 20 20 70 49 74 65   = nRec;..  pIte
285be 72 2d 3e 61 4b 65 79 20 3d 20 26 70 49 74 65 72  r->aKey = &pIter
285bf 2d 3e 61 41 6c 6c 6f 63 5b 69 4f 66 66 5d 3b 0d  ->aAlloc[iOff];.
285c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
285c1 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65  ..../*..** Write
285c2 20 61 20 73 69 6e 67 6c 65 20 76 61 72 69 6e 74   a single varint
285c3 2c 20 76 61 6c 75 65 20 69 56 61 6c 2c 20 74 6f  , value iVal, to
285c4 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
285c5 20 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 0d 0a   pFile. Return..
285c6 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  ** SQLITE_OK if 
285c7 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61  successful, or a
285c8 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
285c9 6f 64 65 20 69 66 20 73 6f 6d 65 20 65 72 72 6f  ode if some erro
285ca 72 20 6f 63 63 75 72 73 2e 0d 0a 2a 2a 0d 0a 2a  r occurs...**..*
285cb 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 2a  * The value of *
285cc 70 69 4f 66 66 73 65 74 20 77 68 65 6e 20 74 68  piOffset when th
285cd 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
285ce 61 6c 6c 65 64 20 69 73 20 75 73 65 64 20 61 73  alled is used as
285cf 20 74 68 65 20 62 79 74 65 0d 0a 2a 2a 20 6f 66   the byte..** of
285d0 66 73 65 74 20 69 6e 20 66 69 6c 65 20 70 46 69  fset in file pFi
285d1 6c 65 20 74 6f 20 77 72 69 74 65 20 74 6f 2e 20  le to write to. 
285d2 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
285d3 2c 20 2a 70 69 4f 66 66 73 65 74 20 69 73 20 0d  , *piOffset is .
285d4 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  .** incremented 
285d5 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  by the number of
285d6 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0d   bytes written..
285d7 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
285d8 76 64 62 65 53 6f 72 74 65 72 57 72 69 74 65 56  vdbeSorterWriteV
285d9 61 72 69 6e 74 28 0d 0a 20 20 73 71 6c 69 74 65  arint(..  sqlite
285da 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20  3_file *pFile,  
285db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
285dc 65 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f  e to write to */
285dd 0d 0a 20 20 69 36 34 20 69 56 61 6c 2c 20 20 20  ..  i64 iVal,   
285de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285df 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
285e0 77 72 69 74 65 20 61 73 20 61 20 76 61 72 69 6e  write as a varin
285e1 74 20 2a 2f 0d 0a 20 20 69 36 34 20 2a 70 69 4f  t */..  i64 *piO
285e2 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20  ffset           
285e3 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
285e4 54 3a 20 57 72 69 74 65 20 6f 66 66 73 65 74 20  T: Write offset 
285e5 69 6e 20 66 69 6c 65 20 70 46 69 6c 65 20 2a 2f  in file pFile */
285e6 0d 0a 29 7b 0d 0a 20 20 75 38 20 61 56 61 72 69  ..){..  u8 aVari
285e7 6e 74 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 20  nt[9];          
285e8 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
285e9 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66  r large enough f
285ea 6f 72 20 61 20 76 61 72 69 6e 74 20 2a 2f 0d 0a  or a varint */..
285eb 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20    int nVarint;  
285ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285ed 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
285ee 73 65 64 20 62 79 74 65 73 20 69 6e 20 76 61 72  sed bytes in var
285ef 69 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63  int */..  int rc
285f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
285f1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
285f2 75 6c 74 20 6f 66 20 77 72 69 74 65 28 29 20 63  ult of write() c
285f3 61 6c 6c 20 2a 2f 0d 0a 0d 0a 20 20 6e 56 61 72  all */....  nVar
285f4 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 50 75 74  int = sqlite3Put
285f5 56 61 72 69 6e 74 28 61 56 61 72 69 6e 74 2c 20  Varint(aVarint, 
285f6 69 56 61 6c 29 3b 0d 0a 20 20 72 63 20 3d 20 73  iVal);..  rc = s
285f7 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 46  qlite3OsWrite(pF
285f8 69 6c 65 2c 20 61 56 61 72 69 6e 74 2c 20 6e 56  ile, aVarint, nV
285f9 61 72 69 6e 74 2c 20 2a 70 69 4f 66 66 73 65 74  arint, *piOffset
285fa 29 3b 0d 0a 20 20 2a 70 69 4f 66 66 73 65 74 20  );..  *piOffset 
285fb 2b 3d 20 6e 56 61 72 69 6e 74 3b 0d 0a 0d 0a 20  += nVarint;.... 
285fc 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
285fd 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 61 64 20 61 20  ../*..** Read a 
285fe 73 69 6e 67 6c 65 20 76 61 72 69 6e 74 20 66 72  single varint fr
285ff 6f 6d 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74  om file-descript
28600 6f 72 20 70 46 69 6c 65 2e 20 52 65 74 75 72 6e  or pFile. Return
28601 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 0d 0a 2a   SQLITE_OK if..*
28602 2a 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  * successful, or
28603 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
28604 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 20 65 72   code if some er
28605 72 6f 72 20 6f 63 63 75 72 73 2e 0d 0a 2a 2a 0d  ror occurs...**.
28606 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66  .** The value of
28607 20 2a 70 69 4f 66 66 73 65 74 20 77 68 65 6e 20   *piOffset when 
28608 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
28609 20 63 61 6c 6c 65 64 20 69 73 20 75 73 65 64 20   called is used 
2860a 61 73 20 74 68 65 0d 0a 2a 2a 20 62 79 74 65 20  as the..** byte 
2860b 6f 66 66 73 65 74 20 69 6e 20 66 69 6c 65 20 70  offset in file p
2860c 46 69 6c 65 20 66 72 6f 6d 20 77 68 65 6e 63 65  File from whence
2860d 20 74 6f 20 72 65 61 64 20 74 68 65 20 76 61 72   to read the var
2860e 69 6e 74 2e 20 49 66 20 73 75 63 63 65 73 73 66  int. If successf
2860f 75 6c 0d 0a 2a 2a 20 28 69 2e 65 2e 20 69 66 20  ul..** (i.e. if 
28610 6e 6f 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  no IO error occu
28611 72 73 29 2c 20 74 68 65 6e 20 2a 70 69 4f 66 66  rs), then *piOff
28612 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68  set is set to th
28613 65 20 6f 66 66 73 65 74 20 6f 66 0d 0a 2a 2a 20  e offset of..** 
28614 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70  the first byte p
28615 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
28616 68 65 20 76 61 72 69 6e 74 20 62 65 66 6f 72 65  he varint before
28617 20 72 65 74 75 72 6e 69 6e 67 2e 20 2a 70 69 56   returning. *piV
28618 61 6c 20 69 73 0d 0a 2a 2a 20 73 65 74 20 74 6f  al is..** set to
28619 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
2861a 75 65 20 72 65 61 64 2e 20 49 66 20 61 6e 20 65  ue read. If an e
2861b 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
2861c 20 66 69 6e 61 6c 20 76 61 6c 75 65 73 20 6f 66   final values of
2861d 0d 0a 2a 2a 20 62 6f 74 68 20 2a 70 69 4f 66 66  ..** both *piOff
2861e 73 65 74 20 61 6e 64 20 2a 70 69 56 61 6c 20 61  set and *piVal a
2861f 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a  re undefined...*
28620 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  /..static int vd
28621 62 65 53 6f 72 74 65 72 52 65 61 64 56 61 72 69  beSorterReadVari
28622 6e 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  nt(..  sqlite3_f
28623 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20  ile *pFile,     
28624 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
28625 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0d 0a  o read from */..
28626 20 20 69 36 34 20 2a 70 69 4f 66 66 73 65 74 2c    i64 *piOffset,
28627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28628 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 52 65 61    /* IN/OUT: Rea
28629 64 20 6f 66 66 73 65 74 20 69 6e 20 70 46 69 6c  d offset in pFil
2862a 65 20 2a 2f 0d 0a 20 20 69 36 34 20 2a 70 69 56  e */..  i64 *piV
2862b 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  al              
2862c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
2862d 56 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  Value read from 
2862e 66 69 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75  file */..){..  u
2862f 38 20 61 56 61 72 69 6e 74 5b 39 5d 3b 20 20 20  8 aVarint[9];   
28630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28631 2a 20 42 75 66 66 65 72 20 6c 61 72 67 65 20 65  * Buffer large e
28632 6e 6f 75 67 68 20 66 6f 72 20 61 20 76 61 72 69  nough for a vari
28633 6e 74 20 2a 2f 0d 0a 20 20 69 36 34 20 69 4f 66  nt */..  i64 iOf
28634 66 20 3d 20 2a 70 69 4f 66 66 73 65 74 3b 20 20  f = *piOffset;  
28635 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
28636 65 74 20 69 6e 20 66 69 6c 65 20 74 6f 20 72 65  et in file to re
28637 61 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e  ad from */..  in
28638 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
28639 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2863a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   Return code */.
2863b 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ...  rc = sqlite
2863c 33 4f 73 52 65 61 64 28 70 46 69 6c 65 2c 20 61  3OsRead(pFile, a
2863d 56 61 72 69 6e 74 2c 20 39 2c 20 69 4f 66 66 29  Varint, 9, iOff)
2863e 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
2863f 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 2a  ITE_OK ){..    *
28640 70 69 4f 66 66 73 65 74 20 2b 3d 20 67 65 74 56  piOffset += getV
28641 61 72 69 6e 74 28 61 56 61 72 69 6e 74 2c 20 28  arint(aVarint, (
28642 75 36 34 20 2a 29 70 69 56 61 6c 29 3b 0d 0a 20  u64 *)piVal);.. 
28643 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72   }....  return r
28644 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
28645 49 6e 69 74 69 61 6c 69 7a 65 20 69 74 65 72 61  Initialize itera
28646 74 6f 72 20 70 49 74 65 72 20 74 6f 20 73 63 61  tor pIter to sca
28647 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 50 4d  n through the PM
28648 41 20 73 74 6f 72 65 64 20 69 6e 20 66 69 6c 65  A stored in file
28649 20 70 46 69 6c 65 0d 0a 2a 2a 20 73 74 61 72 74   pFile..** start
2864a 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 53  ing at offset iS
2864b 74 61 72 74 20 61 6e 64 20 65 6e 64 69 6e 67 20  tart and ending 
2864c 61 74 20 6f 66 66 73 65 74 20 69 45 6f 66 2d 31  at offset iEof-1
2864d 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
2864e 0d 0a 2a 2a 20 6c 65 61 76 65 73 20 74 68 65 20  ..** leaves the 
2864f 69 74 65 72 61 74 6f 72 20 70 6f 69 6e 74 69 6e  iterator pointin
28650 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 6b  g to the first k
28651 65 79 20 69 6e 20 74 68 65 20 50 4d 41 20 28 6f  ey in the PMA (o
28652 72 20 45 4f 46 20 69 66 20 74 68 65 20 0d 0a 2a  r EOF if the ..*
28653 2a 20 50 4d 41 20 69 73 20 65 6d 70 74 79 29 2e  * PMA is empty).
28654 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
28655 20 76 64 62 65 53 6f 72 74 65 72 49 74 65 72 49   vdbeSorterIterI
28656 6e 69 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  nit(..  sqlite3 
28657 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
28658 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
28659 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ase handle */.. 
2865a 20 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53 6f   VdbeSorter *pSo
2865b 72 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  rter,           
2865c 20 2f 2a 20 53 6f 72 74 65 72 20 6f 62 6a 65 63   /* Sorter objec
2865d 74 20 2a 2f 0d 0a 20 20 69 36 34 20 69 53 74 61  t */..  i64 iSta
2865e 72 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rt,             
2865f 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74          /* Start
28660 20 6f 66 66 73 65 74 20 69 6e 20 70 46 69 6c 65   offset in pFile
28661 20 2a 2f 0d 0a 20 20 56 64 62 65 53 6f 72 74 65   */..  VdbeSorte
28662 72 49 74 65 72 20 2a 70 49 74 65 72 2c 20 20 20  rIter *pIter,   
28663 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
28664 6f 72 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 2a  or to populate *
28665 2f 0d 0a 20 20 69 36 34 20 2a 70 6e 42 79 74 65  /..  i64 *pnByte
28666 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28667 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
28668 49 6e 63 72 65 6d 65 6e 74 20 74 68 69 73 20 76  Increment this v
28669 61 6c 75 65 20 62 79 20 50 4d 41 20 73 69 7a 65  alue by PMA size
2866a 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
2866b 63 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  c;....  assert( 
2866c 70 53 6f 72 74 65 72 2d 3e 69 57 72 69 74 65 4f  pSorter->iWriteO
2866d 66 66 3e 69 53 74 61 72 74 20 29 3b 0d 0a 20 20  ff>iStart );..  
2866e 61 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 61  assert( pIter->a
2866f 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0d 0a 20 20 70  Alloc==0 );..  p
28670 49 74 65 72 2d 3e 70 46 69 6c 65 20 3d 20 70 53  Iter->pFile = pS
28671 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31 3b 0d 0a  orter->pTemp1;..
28672 20 20 70 49 74 65 72 2d 3e 69 52 65 61 64 4f 66    pIter->iReadOf
28673 66 20 3d 20 69 53 74 61 72 74 3b 0d 0a 20 20 70  f = iStart;..  p
28674 49 74 65 72 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31  Iter->nAlloc = 1
28675 32 38 3b 0d 0a 20 20 70 49 74 65 72 2d 3e 61 41  28;..  pIter->aA
28676 6c 6c 6f 63 20 3d 20 28 75 38 20 2a 29 73 71 6c  lloc = (u8 *)sql
28677 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
28678 64 62 2c 20 70 49 74 65 72 2d 3e 6e 41 6c 6c 6f  db, pIter->nAllo
28679 63 29 3b 0d 0a 20 20 69 66 28 20 21 70 49 74 65  c);..  if( !pIte
2867a 72 2d 3e 61 41 6c 6c 6f 63 20 29 7b 0d 0a 20 20  r->aAlloc ){..  
2867b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2867c 4d 45 4d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  MEM;..  }else{..
2867d 20 20 20 20 69 36 34 20 6e 42 79 74 65 3b 20 20      i64 nByte;  
2867e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2867f 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
28680 73 69 7a 65 20 6f 66 20 50 4d 41 20 69 6e 20 62  size of PMA in b
28681 79 74 65 73 20 2a 2f 0d 0a 20 20 20 20 72 63 20  ytes */..    rc 
28682 3d 20 76 64 62 65 53 6f 72 74 65 72 52 65 61 64  = vdbeSorterRead
28683 56 61 72 69 6e 74 28 70 53 6f 72 74 65 72 2d 3e  Varint(pSorter->
28684 70 54 65 6d 70 31 2c 20 26 70 49 74 65 72 2d 3e  pTemp1, &pIter->
28685 69 52 65 61 64 4f 66 66 2c 20 26 6e 42 79 74 65  iReadOff, &nByte
28686 29 3b 0d 0a 20 20 20 20 2a 70 6e 42 79 74 65 20  );..    *pnByte 
28687 2b 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 70  += nByte;..    p
28688 49 74 65 72 2d 3e 69 45 6f 66 20 3d 20 70 49 74  Iter->iEof = pIt
28689 65 72 2d 3e 69 52 65 61 64 4f 66 66 20 2b 20 6e  er->iReadOff + n
2868a 42 79 74 65 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  Byte;..  }..  if
2868b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2868c 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 76 64 62  ){..    rc = vdb
2868d 65 53 6f 72 74 65 72 49 74 65 72 4e 65 78 74 28  eSorterIterNext(
2868e 64 62 2c 20 70 49 74 65 72 29 3b 0d 0a 20 20 7d  db, pIter);..  }
2868f 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
28690 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f  }....../*..** Co
28691 6d 70 61 72 65 20 6b 65 79 31 20 28 62 75 66 66  mpare key1 (buff
28692 65 72 20 70 4b 65 79 31 2c 20 73 69 7a 65 20 6e  er pKey1, size n
28693 4b 65 79 31 20 62 79 74 65 73 29 20 77 69 74 68  Key1 bytes) with
28694 20 6b 65 79 32 20 28 62 75 66 66 65 72 20 70 4b   key2 (buffer pK
28695 65 79 32 2c 20 0d 0a 2a 2a 20 73 69 7a 65 20 6e  ey2, ..** size n
28696 4b 65 79 32 20 62 79 74 65 73 29 2e 20 20 41 72  Key2 bytes).  Ar
28697 67 75 6d 65 6e 74 20 70 4b 65 79 49 6e 66 6f 20  gument pKeyInfo 
28698 73 75 70 70 6c 69 65 73 20 74 68 65 20 63 6f 6c  supplies the col
28699 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  lation functions
2869a 0d 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65  ..** used by the
2869b 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20   comparison. If 
2869c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2869d 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74   return an SQLit
2869e 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a  e error code...*
2869f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  * Otherwise, ret
286a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
286a1 64 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 61  d set *pRes to a
286a2 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20   negative, zero 
286a3 6f 72 20 70 6f 73 69 74 69 76 65 0d 0a 2a 2a 20  or positive..** 
286a4 76 61 6c 75 65 2c 20 64 65 70 65 6e 64 69 6e 67  value, depending
286a5 20 6f 6e 20 77 68 65 74 68 65 72 20 6b 65 79 31   on whether key1
286a6 20 69 73 20 73 6d 61 6c 6c 65 72 2c 20 65 71 75   is smaller, equ
286a7 61 6c 20 74 6f 20 6f 72 20 6c 61 72 67 65 72 20  al to or larger 
286a8 74 68 61 6e 20 6b 65 79 32 2e 0d 0a 2a 2a 0d 0a  than key2...**..
286a9 2a 2a 20 49 66 20 74 68 65 20 62 4f 6d 69 74 52  ** If the bOmitR
286aa 6f 77 69 64 20 61 72 67 75 6d 65 6e 74 20 69 73  owid argument is
286ab 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 73 73 75 6d   non-zero, assum
286ac 65 20 62 6f 74 68 20 6b 65 79 73 20 65 6e 64 20  e both keys end 
286ad 69 6e 20 61 20 72 6f 77 69 64 0d 0a 2a 2a 20 66  in a rowid..** f
286ae 69 65 6c 64 2e 20 46 6f 72 20 74 68 65 20 70 75  ield. For the pu
286af 72 70 6f 73 65 73 20 6f 66 20 74 68 65 20 63 6f  rposes of the co
286b0 6d 70 61 72 69 73 6f 6e 2c 20 69 67 6e 6f 72 65  mparison, ignore
286b1 20 69 74 2e 20 41 6c 73 6f 2c 20 69 66 20 62 4f   it. Also, if bO
286b2 6d 69 74 52 6f 77 69 64 0d 0a 2a 2a 20 69 73 20  mitRowid..** is 
286b3 74 72 75 65 20 61 6e 64 20 6b 65 79 31 20 63 6f  true and key1 co
286b4 6e 74 61 69 6e 73 20 65 76 65 6e 20 61 20 73 69  ntains even a si
286b5 6e 67 6c 65 20 4e 55 4c 4c 20 76 61 6c 75 65 2c  ngle NULL value,
286b6 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65   it is considere
286b7 64 20 74 6f 0d 0a 2a 2a 20 62 65 20 6c 65 73 73  d to..** be less
286b8 20 74 68 61 6e 20 6b 65 79 32 2e 20 45 76 65 6e   than key2. Even
286b9 20 69 66 20 6b 65 79 32 20 61 6c 73 6f 20 63 6f   if key2 also co
286ba 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76 61 6c 75  ntains NULL valu
286bb 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70  es...**..** If p
286bc 4b 65 79 32 20 69 73 20 70 61 73 73 65 64 20 61  Key2 is passed a
286bd 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
286be 68 65 6e 20 69 74 20 69 73 20 61 73 73 75 6d 65  hen it is assume
286bf 64 20 74 68 61 74 20 74 68 65 20 70 43 73 72 2d  d that the pCsr-
286c0 3e 61 53 70 61 63 65 0d 0a 2a 2a 20 68 61 73 20  >aSpace..** has 
286c1 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  been allocated a
286c2 6e 64 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 75  nd contains an u
286c3 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74  npacked record t
286c4 68 61 74 20 69 73 20 75 73 65 64 20 61 73 20 6b  hat is used as k
286c5 65 79 32 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ey2...*/..static
286c6 20 76 6f 69 64 20 76 64 62 65 53 6f 72 74 65 72   void vdbeSorter
286c7 43 6f 6d 70 61 72 65 28 0d 0a 20 20 56 64 62 65  Compare(..  Vdbe
286c8 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20  Cursor *pCsr,   
286c9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
286ca 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 28 66 6f  ursor object (fo
286cb 72 20 70 4b 65 79 49 6e 66 6f 29 20 2a 2f 0d 0a  r pKeyInfo) */..
286cc 20 20 69 6e 74 20 62 4f 6d 69 74 52 6f 77 69 64    int bOmitRowid
286cd 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
286ce 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 6f 77 69    /* Ignore rowi
286cf 64 20 66 69 65 6c 64 20 61 74 20 65 6e 64 20 6f  d field at end o
286d0 66 20 6b 65 79 73 20 2a 2f 0d 0a 20 20 76 6f 69  f keys */..  voi
286d1 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 4b  d *pKey1, int nK
286d2 65 79 31 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ey1,         /* 
286d3 4c 65 66 74 20 73 69 64 65 20 6f 66 20 63 6f 6d  Left side of com
286d4 70 61 72 69 73 6f 6e 20 2a 2f 0d 0a 20 20 76 6f  parison */..  vo
286d5 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e  id *pKey2, int n
286d6 4b 65 79 32 2c 20 20 20 20 20 20 20 20 20 2f 2a  Key2,         /*
286d7 20 52 69 67 68 74 20 73 69 64 65 20 6f 66 20 63   Right side of c
286d8 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0d 0a 20 20  omparison */..  
286d9 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20  int *pRes       
286da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
286db 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 6f  /* OUT: Result o
286dc 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0d  f comparison */.
286dd 0a 29 7b 0d 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  .){..  KeyInfo *
286de 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 73 72 2d  pKeyInfo = pCsr-
286df 3e 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20 56 64  >pKeyInfo;..  Vd
286e0 62 65 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65  beSorter *pSorte
286e1 72 20 3d 20 70 43 73 72 2d 3e 70 53 6f 72 74 65  r = pCsr->pSorte
286e2 72 3b 0d 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  r;..  UnpackedRe
286e3 63 6f 72 64 20 2a 72 32 20 3d 20 70 53 6f 72 74  cord *r2 = pSort
286e4 65 72 2d 3e 70 55 6e 70 61 63 6b 65 64 3b 0d 0a  er->pUnpacked;..
286e5 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20 69 66    int i;....  if
286e6 28 20 70 4b 65 79 32 20 29 7b 0d 0a 20 20 20 20  ( pKey2 ){..    
286e7 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
286e8 64 55 6e 70 61 63 6b 28 70 4b 65 79 49 6e 66 6f  dUnpack(pKeyInfo
286e9 2c 20 6e 4b 65 79 32 2c 20 70 4b 65 79 32 2c 20  , nKey2, pKey2, 
286ea 72 32 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  r2);..  }....  i
286eb 66 28 20 62 4f 6d 69 74 52 6f 77 69 64 20 29 7b  f( bOmitRowid ){
286ec 0d 0a 20 20 20 20 72 32 2d 3e 6e 46 69 65 6c 64  ..    r2->nField
286ed 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69   = pKeyInfo->nFi
286ee 65 6c 64 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  eld;..    assert
286ef 28 20 72 32 2d 3e 6e 46 69 65 6c 64 3e 30 20 29  ( r2->nField>0 )
286f0 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
286f1 69 3c 72 32 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b  i<r2->nField; i+
286f2 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 72  +){..      if( r
286f3 32 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73  2->aMem[i].flags
286f4 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0d 0a   & MEM_Null ){..
286f5 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20          *pRes = 
286f6 2d 31 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74  -1;..        ret
286f7 75 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  urn;..      }.. 
286f8 20 20 20 7d 0d 0a 20 20 20 20 72 32 2d 3e 66 6c     }..    r2->fl
286f9 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f  ags |= UNPACKED_
286fa 50 52 45 46 49 58 5f 4d 41 54 43 48 3b 0d 0a 20  PREFIX_MATCH;.. 
286fb 20 7d 0d 0a 0d 0a 20 20 2a 70 52 65 73 20 3d 20   }....  *pRes = 
286fc 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
286fd 64 43 6f 6d 70 61 72 65 28 6e 4b 65 79 31 2c 20  dCompare(nKey1, 
286fe 70 4b 65 79 31 2c 20 72 32 29 3b 0d 0a 7d 0d 0a  pKey1, r2);..}..
286ff 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
28700 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
28701 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20   to compare two 
28702 69 74 65 72 61 74 6f 72 20 6b 65 79 73 20 77 68  iterator keys wh
28703 65 6e 20 6d 65 72 67 69 6e 67 20 0d 0a 2a 2a 20  en merging ..** 
28704 6d 75 6c 74 69 70 6c 65 20 62 2d 74 72 65 65 20  multiple b-tree 
28705 73 65 67 6d 65 6e 74 73 2e 20 50 61 72 61 6d 65  segments. Parame
28706 74 65 72 20 69 4f 75 74 20 69 73 20 74 68 65 20  ter iOut is the 
28707 69 6e 64 65 78 20 6f 66 20 74 68 65 20 61 54 72  index of the aTr
28708 65 65 5b 5d 20 0d 0a 2a 2a 20 76 61 6c 75 65 20  ee[] ..** value 
28709 74 6f 20 72 65 63 61 6c 63 75 6c 61 74 65 2e 0d  to recalculate..
2870a 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
2870b 76 64 62 65 53 6f 72 74 65 72 44 6f 43 6f 6d 70  vdbeSorterDoComp
2870c 61 72 65 28 56 64 62 65 43 75 72 73 6f 72 20 2a  are(VdbeCursor *
2870d 70 43 73 72 2c 20 69 6e 74 20 69 4f 75 74 29 7b  pCsr, int iOut){
2870e 0d 0a 20 20 56 64 62 65 53 6f 72 74 65 72 20 2a  ..  VdbeSorter *
2870f 70 53 6f 72 74 65 72 20 3d 20 70 43 73 72 2d 3e  pSorter = pCsr->
28710 70 53 6f 72 74 65 72 3b 0d 0a 20 20 69 6e 74 20  pSorter;..  int 
28711 69 31 3b 0d 0a 20 20 69 6e 74 20 69 32 3b 0d 0a  i1;..  int i2;..
28712 20 20 69 6e 74 20 69 52 65 73 3b 0d 0a 20 20 56    int iRes;..  V
28713 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 2a 70  dbeSorterIter *p
28714 31 3b 0d 0a 20 20 56 64 62 65 53 6f 72 74 65 72  1;..  VdbeSorter
28715 49 74 65 72 20 2a 70 32 3b 0d 0a 0d 0a 20 20 61  Iter *p2;....  a
28716 73 73 65 72 74 28 20 69 4f 75 74 3c 70 53 6f 72  ssert( iOut<pSor
28717 74 65 72 2d 3e 6e 54 72 65 65 20 26 26 20 69 4f  ter->nTree && iO
28718 75 74 3e 30 20 29 3b 0d 0a 0d 0a 20 20 69 66 28  ut>0 );....  if(
28719 20 69 4f 75 74 3e 3d 28 70 53 6f 72 74 65 72 2d   iOut>=(pSorter-
2871a 3e 6e 54 72 65 65 2f 32 29 20 29 7b 0d 0a 20 20  >nTree/2) ){..  
2871b 20 20 69 31 20 3d 20 28 69 4f 75 74 20 2d 20 70    i1 = (iOut - p
2871c 53 6f 72 74 65 72 2d 3e 6e 54 72 65 65 2f 32 29  Sorter->nTree/2)
2871d 20 2a 20 32 3b 0d 0a 20 20 20 20 69 32 20 3d 20   * 2;..    i2 = 
2871e 69 31 20 2b 20 31 3b 0d 0a 20 20 7d 65 6c 73 65  i1 + 1;..  }else
2871f 7b 0d 0a 20 20 20 20 69 31 20 3d 20 70 53 6f 72  {..    i1 = pSor
28720 74 65 72 2d 3e 61 54 72 65 65 5b 69 4f 75 74 2a  ter->aTree[iOut*
28721 32 5d 3b 0d 0a 20 20 20 20 69 32 20 3d 20 70 53  2];..    i2 = pS
28722 6f 72 74 65 72 2d 3e 61 54 72 65 65 5b 69 4f 75  orter->aTree[iOu
28723 74 2a 32 2b 31 5d 3b 0d 0a 20 20 7d 0d 0a 0d 0a  t*2+1];..  }....
28724 20 20 70 31 20 3d 20 26 70 53 6f 72 74 65 72 2d    p1 = &pSorter-
28725 3e 61 49 74 65 72 5b 69 31 5d 3b 0d 0a 20 20 70  >aIter[i1];..  p
28726 32 20 3d 20 26 70 53 6f 72 74 65 72 2d 3e 61 49  2 = &pSorter->aI
28727 74 65 72 5b 69 32 5d 3b 0d 0a 0d 0a 20 20 69 66  ter[i2];....  if
28728 28 20 70 31 2d 3e 70 46 69 6c 65 3d 3d 30 20 29  ( p1->pFile==0 )
28729 7b 0d 0a 20 20 20 20 69 52 65 73 20 3d 20 69 32  {..    iRes = i2
2872a 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  ;..  }else if( p
2872b 32 2d 3e 70 46 69 6c 65 3d 3d 30 20 29 7b 0d 0a  2->pFile==0 ){..
2872c 20 20 20 20 69 52 65 73 20 3d 20 69 31 3b 0d 0a      iRes = i1;..
2872d 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e    }else{..    in
2872e 74 20 72 65 73 3b 0d 0a 20 20 20 20 61 73 73 65  t res;..    asse
2872f 72 74 28 20 70 43 73 72 2d 3e 70 53 6f 72 74 65  rt( pCsr->pSorte
28730 72 2d 3e 70 55 6e 70 61 63 6b 65 64 21 3d 30 20  r->pUnpacked!=0 
28731 29 3b 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 64  );  /* allocated
28732 20 69 6e 20 76 64 62 65 53 6f 72 74 65 72 4d 65   in vdbeSorterMe
28733 72 67 65 28 29 20 2a 2f 0d 0a 20 20 20 20 76 64  rge() */..    vd
28734 62 65 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 28  beSorterCompare(
28735 0d 0a 20 20 20 20 20 20 20 20 70 43 73 72 2c 20  ..        pCsr, 
28736 30 2c 20 70 31 2d 3e 61 4b 65 79 2c 20 70 31 2d  0, p1->aKey, p1-
28737 3e 6e 4b 65 79 2c 20 70 32 2d 3e 61 4b 65 79 2c  >nKey, p2->aKey,
28738 20 70 32 2d 3e 6e 4b 65 79 2c 20 26 72 65 73 0d   p2->nKey, &res.
28739 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20 69 66 28  .    );..    if(
2873a 20 72 65 73 3c 3d 30 20 29 7b 0d 0a 20 20 20 20   res<=0 ){..    
2873b 20 20 69 52 65 73 20 3d 20 69 31 3b 0d 0a 20 20    iRes = i1;..  
2873c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
2873d 69 52 65 73 20 3d 20 69 32 3b 0d 0a 20 20 20 20  iRes = i2;..    
2873e 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 53 6f 72  }..  }....  pSor
2873f 74 65 72 2d 3e 61 54 72 65 65 5b 69 4f 75 74 5d  ter->aTree[iOut]
28740 20 3d 20 69 52 65 73 3b 0d 0a 20 20 72 65 74 75   = iRes;..  retu
28741 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
28742 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69  ..../*..** Initi
28743 61 6c 69 7a 65 20 74 68 65 20 74 65 6d 70 6f 72  alize the tempor
28744 61 72 79 20 69 6e 64 65 78 20 63 75 72 73 6f 72  ary index cursor
28745 20 6a 75 73 74 20 6f 70 65 6e 65 64 20 61 73 20   just opened as 
28746 61 20 73 6f 72 74 65 72 20 63 75 72 73 6f 72 2e  a sorter cursor.
28747 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
28748 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
28749 56 64 62 65 53 6f 72 74 65 72 49 6e 69 74 28 73  VdbeSorterInit(s
2874a 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62 65  qlite3 *db, Vdbe
2874b 43 75 72 73 6f 72 20 2a 70 43 73 72 29 7b 0d 0a  Cursor *pCsr){..
2874c 20 20 69 6e 74 20 70 67 73 7a 3b 20 20 20 20 20    int pgsz;     
2874d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2874e 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 6f    /* Page size o
2874f 66 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  f main database 
28750 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 43 61 63 68  */..  int mxCach
28751 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
28752 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 73        /* Cache s
28753 69 7a 65 20 2a 2f 0d 0a 20 20 56 64 62 65 53 6f  ize */..  VdbeSo
28754 72 74 65 72 20 2a 70 53 6f 72 74 65 72 3b 20 20  rter *pSorter;  
28755 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
28756 20 6e 65 77 20 73 6f 72 74 65 72 20 2a 2f 0d 0a   new sorter */..
28757 20 20 63 68 61 72 20 2a 64 3b 20 20 20 20 20 20    char *d;      
28758 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28759 20 20 2f 2a 20 44 75 6d 6d 79 20 2a 2f 0d 0a 0d    /* Dummy */...
2875a 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d  .  assert( pCsr-
2875b 3e 70 4b 65 79 49 6e 66 6f 20 26 26 20 70 43 73  >pKeyInfo && pCs
2875c 72 2d 3e 70 42 74 3d 3d 30 20 29 3b 0d 0a 20 20  r->pBt==0 );..  
2875d 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 20 3d 20  pCsr->pSorter = 
2875e 70 53 6f 72 74 65 72 20 3d 20 73 71 6c 69 74 65  pSorter = sqlite
2875f 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
28760 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 53 6f 72  , sizeof(VdbeSor
28761 74 65 72 29 29 3b 0d 0a 20 20 69 66 28 20 70 53  ter));..  if( pS
28762 6f 72 74 65 72 3d 3d 30 20 29 7b 0d 0a 20 20 20  orter==0 ){..   
28763 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
28764 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 0d 0a  OMEM;..  }..  ..
28765 20 20 70 53 6f 72 74 65 72 2d 3e 70 55 6e 70 61    pSorter->pUnpa
28766 63 6b 65 64 20 3d 20 73 71 6c 69 74 65 33 56 64  cked = sqlite3Vd
28767 62 65 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52  beAllocUnpackedR
28768 65 63 6f 72 64 28 70 43 73 72 2d 3e 70 4b 65 79  ecord(pCsr->pKey
28769 49 6e 66 6f 2c 20 30 2c 20 30 2c 20 26 64 29 3b  Info, 0, 0, &d);
2876a 0d 0a 20 20 69 66 28 20 70 53 6f 72 74 65 72 2d  ..  if( pSorter-
2876b 3e 70 55 6e 70 61 63 6b 65 64 3d 3d 30 20 29 20  >pUnpacked==0 ) 
2876c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2876d 4d 45 4d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  MEM;..  assert( 
2876e 70 53 6f 72 74 65 72 2d 3e 70 55 6e 70 61 63 6b  pSorter->pUnpack
2876f 65 64 3d 3d 28 55 6e 70 61 63 6b 65 64 52 65 63  ed==(UnpackedRec
28770 6f 72 64 20 2a 29 64 20 29 3b 0d 0a 0d 0a 20 20  ord *)d );....  
28771 69 66 28 20 21 73 71 6c 69 74 65 33 54 65 6d 70  if( !sqlite3Temp
28772 49 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b 0d  InMemory(db) ){.
28773 0a 20 20 20 20 70 67 73 7a 20 3d 20 73 71 6c 69  .    pgsz = sqli
28774 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
28775 69 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ize(db->aDb[0].p
28776 42 74 29 3b 0d 0a 20 20 20 20 70 53 6f 72 74 65  Bt);..    pSorte
28777 72 2d 3e 6d 6e 50 6d 61 53 69 7a 65 20 3d 20 53  r->mnPmaSize = S
28778 4f 52 54 45 52 5f 4d 49 4e 5f 57 4f 52 4b 49 4e  ORTER_MIN_WORKIN
28779 47 20 2a 20 70 67 73 7a 3b 0d 0a 20 20 20 20 6d  G * pgsz;..    m
2877a 78 43 61 63 68 65 20 3d 20 64 62 2d 3e 61 44 62  xCache = db->aDb
2877b 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e 63 61 63  [0].pSchema->cac
2877c 68 65 5f 73 69 7a 65 3b 0d 0a 20 20 20 20 69 66  he_size;..    if
2877d 28 20 6d 78 43 61 63 68 65 3c 53 4f 52 54 45 52  ( mxCache<SORTER
2877e 5f 4d 49 4e 5f 57 4f 52 4b 49 4e 47 20 29 20 6d  _MIN_WORKING ) m
2877f 78 43 61 63 68 65 20 3d 20 53 4f 52 54 45 52 5f  xCache = SORTER_
28780 4d 49 4e 5f 57 4f 52 4b 49 4e 47 3b 0d 0a 20 20  MIN_WORKING;..  
28781 20 20 70 53 6f 72 74 65 72 2d 3e 6d 78 50 6d 61    pSorter->mxPma
28782 53 69 7a 65 20 3d 20 6d 78 43 61 63 68 65 20 2a  Size = mxCache *
28783 20 70 67 73 7a 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20   pgsz;..  }.... 
28784 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
28785 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
28786 46 72 65 65 20 74 68 65 20 6c 69 73 74 20 6f 66  Free the list of
28787 20 73 6f 72 74 65 64 20 72 65 63 6f 72 64 73 20   sorted records 
28788 73 74 61 72 74 69 6e 67 20 61 74 20 70 52 65 63  starting at pRec
28789 6f 72 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ord...*/..static
2878a 20 76 6f 69 64 20 76 64 62 65 53 6f 72 74 65 72   void vdbeSorter
2878b 52 65 63 6f 72 64 46 72 65 65 28 73 71 6c 69 74  RecordFree(sqlit
2878c 65 33 20 2a 64 62 2c 20 53 6f 72 74 65 72 52 65  e3 *db, SorterRe
2878d 63 6f 72 64 20 2a 70 52 65 63 6f 72 64 29 7b 0d  cord *pRecord){.
2878e 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20  .  SorterRecord 
2878f 2a 70 3b 0d 0a 20 20 53 6f 72 74 65 72 52 65 63  *p;..  SorterRec
28790 6f 72 64 20 2a 70 4e 65 78 74 3b 0d 0a 20 20 66  ord *pNext;..  f
28791 6f 72 28 70 3d 70 52 65 63 6f 72 64 3b 20 70 3b  or(p=pRecord; p;
28792 20 70 3d 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20   p=pNext){..    
28793 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
28794 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
28795 46 72 65 65 28 64 62 2c 20 70 29 3b 0d 0a 20 20  Free(db, p);..  
28796 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46  }..}..../*..** F
28797 72 65 65 20 61 6e 79 20 63 75 72 73 6f 72 20 63  ree any cursor c
28798 6f 6d 70 6f 6e 65 6e 74 73 20 61 6c 6c 6f 63 61  omponents alloca
28799 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 56 64  ted by sqlite3Vd
2879a 62 65 53 6f 72 74 65 72 58 58 58 20 72 6f 75 74  beSorterXXX rout
2879b 69 6e 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ines...*/..SQLIT
2879c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2879d 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72  qlite3VdbeSorter
2879e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64  Close(sqlite3 *d
2879f 62 2c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  b, VdbeCursor *p
287a0 43 73 72 29 7b 0d 0a 20 20 56 64 62 65 53 6f 72  Csr){..  VdbeSor
287a1 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70  ter *pSorter = p
287a2 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0d 0a 20  Csr->pSorter;.. 
287a3 20 69 66 28 20 70 53 6f 72 74 65 72 20 29 7b 0d   if( pSorter ){.
287a4 0a 20 20 20 20 69 66 28 20 70 53 6f 72 74 65 72  .    if( pSorter
287a5 2d 3e 61 49 74 65 72 20 29 7b 0d 0a 20 20 20 20  ->aIter ){..    
287a6 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20    int i;..      
287a7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 6f 72 74  for(i=0; i<pSort
287a8 65 72 2d 3e 6e 54 72 65 65 3b 20 69 2b 2b 29 7b  er->nTree; i++){
287a9 0d 0a 20 20 20 20 20 20 20 20 76 64 62 65 53 6f  ..        vdbeSo
287aa 72 74 65 72 49 74 65 72 5a 65 72 6f 28 64 62 2c  rterIterZero(db,
287ab 20 26 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72   &pSorter->aIter
287ac 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  [i]);..      }..
287ad 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
287ae 72 65 65 28 64 62 2c 20 70 53 6f 72 74 65 72 2d  ree(db, pSorter-
287af 3e 61 49 74 65 72 29 3b 0d 0a 20 20 20 20 7d 0d  >aIter);..    }.
287b0 0a 20 20 20 20 69 66 28 20 70 53 6f 72 74 65 72  .    if( pSorter
287b1 2d 3e 70 54 65 6d 70 31 20 29 7b 0d 0a 20 20 20  ->pTemp1 ){..   
287b2 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
287b3 65 46 72 65 65 28 70 53 6f 72 74 65 72 2d 3e 70  eFree(pSorter->p
287b4 54 65 6d 70 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a  Temp1);..    }..
287b5 20 20 20 20 76 64 62 65 53 6f 72 74 65 72 52 65      vdbeSorterRe
287b6 63 6f 72 64 46 72 65 65 28 64 62 2c 20 70 53 6f  cordFree(db, pSo
287b7 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 29 3b 0d  rter->pRecord);.
287b8 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
287b9 65 65 28 64 62 2c 20 70 53 6f 72 74 65 72 2d 3e  ee(db, pSorter->
287ba 70 55 6e 70 61 63 6b 65 64 29 3b 0d 0a 20 20 20  pUnpacked);..   
287bb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
287bc 62 2c 20 70 53 6f 72 74 65 72 29 3b 0d 0a 20 20  b, pSorter);..  
287bd 20 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 20    pCsr->pSorter 
287be 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  = 0;..  }..}....
287bf 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  /*..** Allocate 
287c0 73 70 61 63 65 20 66 6f 72 20 61 20 66 69 6c 65  space for a file
287c1 2d 68 61 6e 64 6c 65 20 61 6e 64 20 6f 70 65 6e  -handle and open
287c2 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
287c3 65 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  e. If successful
287c4 2c 0d 0a 2a 2a 20 73 65 74 20 2a 70 70 46 69 6c  ,..** set *ppFil
287c5 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  e to point to th
287c6 65 20 6d 61 6c 6c 6f 63 27 64 20 66 69 6c 65 2d  e malloc'd file-
287c7 68 61 6e 64 6c 65 20 61 6e 64 20 72 65 74 75 72  handle and retur
287c8 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a  n SQLITE_OK...**
287c9 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20   Otherwise, set 
287ca 2a 70 70 46 69 6c 65 20 74 6f 20 30 20 61 6e 64  *ppFile to 0 and
287cb 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74   return an SQLit
287cc 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a  e error code...*
287cd 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  /..static int vd
287ce 62 65 53 6f 72 74 65 72 4f 70 65 6e 54 65 6d 70  beSorterOpenTemp
287cf 46 69 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62  File(sqlite3 *db
287d0 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  , sqlite3_file *
287d1 2a 70 70 46 69 6c 65 29 7b 0d 0a 20 20 69 6e 74  *ppFile){..  int
287d2 20 64 75 6d 6d 79 3b 0d 0a 20 20 72 65 74 75 72   dummy;..  retur
287d3 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d  n sqlite3OsOpenM
287d4 61 6c 6c 6f 63 28 64 62 2d 3e 70 56 66 73 2c 20  alloc(db->pVfs, 
287d5 30 2c 20 70 70 46 69 6c 65 2c 0d 0a 20 20 20 20  0, ppFile,..    
287d6 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45    SQLITE_OPEN_TE
287d7 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c 0d 0a 20 20  MP_JOURNAL |..  
287d8 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
287d9 52 45 41 44 57 52 49 54 45 20 20 20 20 7c 20 53  READWRITE    | S
287da 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
287db 45 20 7c 0d 0a 20 20 20 20 20 20 53 51 4c 49 54  E |..      SQLIT
287dc 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
287dd 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45      | SQLITE_OPE
287de 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 2c  N_DELETEONCLOSE,
287df 20 26 64 75 6d 6d 79 0d 0a 20 20 29 3b 0d 0a 7d   &dummy..  );..}
287e0 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 65 72 67 65  ..../*..** Merge
287e1 20 74 68 65 20 74 77 6f 20 73 6f 72 74 65 64 20   the two sorted 
287e2 6c 69 73 74 73 20 70 31 20 61 6e 64 20 70 32 20  lists p1 and p2 
287e3 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 69  into a single li
287e4 73 74 2e 0d 0a 2a 2a 20 53 65 74 20 2a 70 70 4f  st...** Set *ppO
287e5 75 74 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f  ut to the head o
287e6 66 20 74 68 65 20 6e 65 77 20 6c 69 73 74 2e 0d  f the new list..
287e7 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
287e8 20 76 64 62 65 53 6f 72 74 65 72 4d 65 72 67 65   vdbeSorterMerge
287e9 28 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  (..  VdbeCursor 
287ea 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
287eb 20 20 20 20 20 2f 2a 20 46 6f 72 20 70 4b 65 79       /* For pKey
287ec 49 6e 66 6f 20 2a 2f 0d 0a 20 20 53 6f 72 74 65  Info */..  Sorte
287ed 72 52 65 63 6f 72 64 20 2a 70 31 2c 20 20 20 20  rRecord *p1,    
287ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
287ef 72 73 74 20 6c 69 73 74 20 74 6f 20 6d 65 72 67  rst list to merg
287f0 65 20 2a 2f 0d 0a 20 20 53 6f 72 74 65 72 52 65  e */..  SorterRe
287f1 63 6f 72 64 20 2a 70 32 2c 20 20 20 20 20 20 20  cord *p2,       
287f2 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
287f3 64 20 6c 69 73 74 20 74 6f 20 6d 65 72 67 65 20  d list to merge 
287f4 2a 2f 0d 0a 20 20 53 6f 72 74 65 72 52 65 63 6f  */..  SorterReco
287f5 72 64 20 2a 2a 70 70 4f 75 74 20 20 20 20 20 20  rd **ppOut      
287f6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 48 65        /* OUT: He
287f7 61 64 20 6f 66 20 6d 65 72 67 65 64 20 6c 69 73  ad of merged lis
287f8 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 53 6f 72 74  t */..){..  Sort
287f9 65 72 52 65 63 6f 72 64 20 2a 70 46 69 6e 61 6c  erRecord *pFinal
287fa 20 3d 20 30 3b 0d 0a 20 20 53 6f 72 74 65 72 52   = 0;..  SorterR
287fb 65 63 6f 72 64 20 2a 2a 70 70 20 3d 20 26 70 46  ecord **pp = &pF
287fc 69 6e 61 6c 3b 0d 0a 20 20 76 6f 69 64 20 2a 70  inal;..  void *p
287fd 56 61 6c 32 20 3d 20 70 32 20 3f 20 70 32 2d 3e  Val2 = p2 ? p2->
287fe 70 56 61 6c 20 3a 20 30 3b 0d 0a 0d 0a 20 20 77  pVal : 0;....  w
287ff 68 69 6c 65 28 20 70 31 20 26 26 20 70 32 20 29  hile( p1 && p2 )
28800 7b 0d 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0d  {..    int res;.
28801 0a 20 20 20 20 76 64 62 65 53 6f 72 74 65 72 43  .    vdbeSorterC
28802 6f 6d 70 61 72 65 28 70 43 73 72 2c 20 30 2c 20  ompare(pCsr, 0, 
28803 70 31 2d 3e 70 56 61 6c 2c 20 70 31 2d 3e 6e 56  p1->pVal, p1->nV
28804 61 6c 2c 20 70 56 61 6c 32 2c 20 70 32 2d 3e 6e  al, pVal2, p2->n
28805 56 61 6c 2c 20 26 72 65 73 29 3b 0d 0a 20 20 20  Val, &res);..   
28806 20 69 66 28 20 72 65 73 3c 3d 30 20 29 7b 0d 0a   if( res<=0 ){..
28807 20 20 20 20 20 20 2a 70 70 20 3d 20 70 31 3b 0d        *pp = p1;.
28808 0a 20 20 20 20 20 20 70 70 20 3d 20 26 70 31 2d  .      pp = &p1-
28809 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 70  >pNext;..      p
2880a 31 20 3d 20 70 31 2d 3e 70 4e 65 78 74 3b 0d 0a  1 = p1->pNext;..
2880b 20 20 20 20 20 20 70 56 61 6c 32 20 3d 20 30 3b        pVal2 = 0;
2880c 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
2880d 20 20 20 20 2a 70 70 20 3d 20 70 32 3b 0d 0a 20      *pp = p2;.. 
2880e 20 20 20 20 20 20 70 70 20 3d 20 26 70 32 2d 3e        pp = &p2->
2880f 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 70 32  pNext;..      p2
28810 20 3d 20 70 32 2d 3e 70 4e 65 78 74 3b 0d 0a 20   = p2->pNext;.. 
28811 20 20 20 20 20 69 66 28 20 70 32 3d 3d 30 20 29       if( p2==0 )
28812 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 70   break;..      p
28813 56 61 6c 32 20 3d 20 70 32 2d 3e 70 56 61 6c 3b  Val2 = p2->pVal;
28814 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
28815 2a 70 70 20 3d 20 70 31 20 3f 20 70 31 20 3a 20  *pp = p1 ? p1 : 
28816 70 32 3b 0d 0a 20 20 2a 70 70 4f 75 74 20 3d 20  p2;..  *ppOut = 
28817 70 46 69 6e 61 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  pFinal;..}..../*
28818 0d 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69  ..** Sort the li
28819 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 72 65 63  nked list of rec
2881a 6f 72 64 73 20 68 65 61 64 65 64 20 61 74 20 70  ords headed at p
2881b 43 73 72 2d 3e 70 52 65 63 6f 72 64 2e 20 52 65  Csr->pRecord. Re
2881c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0d 0a  turn SQLITE_OK..
2881d 2a 2a 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  ** if successful
2881e 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
2881f 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20  rror code (i.e. 
28820 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 69 66  SQLITE_NOMEM) if
28821 20 61 6e 20 65 72 72 6f 72 0d 0a 2a 2a 20 6f 63   an error..** oc
28822 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  curs...*/..stati
28823 63 20 69 6e 74 20 76 64 62 65 53 6f 72 74 65 72  c int vdbeSorter
28824 53 6f 72 74 28 56 64 62 65 43 75 72 73 6f 72 20  Sort(VdbeCursor 
28825 2a 70 43 73 72 29 7b 0d 0a 20 20 69 6e 74 20 69  *pCsr){..  int i
28826 3b 0d 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72  ;..  SorterRecor
28827 64 20 2a 2a 61 53 6c 6f 74 3b 0d 0a 20 20 53 6f  d **aSlot;..  So
28828 72 74 65 72 52 65 63 6f 72 64 20 2a 70 3b 0d 0a  rterRecord *p;..
28829 20 20 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53    VdbeSorter *pS
2882a 6f 72 74 65 72 20 3d 20 70 43 73 72 2d 3e 70 53  orter = pCsr->pS
2882b 6f 72 74 65 72 3b 0d 0a 0d 0a 20 20 61 53 6c 6f  orter;....  aSlo
2882c 74 20 3d 20 28 53 6f 72 74 65 72 52 65 63 6f 72  t = (SorterRecor
2882d 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  d **)sqlite3Mall
2882e 6f 63 5a 65 72 6f 28 36 34 20 2a 20 73 69 7a 65  ocZero(64 * size
2882f 6f 66 28 53 6f 72 74 65 72 52 65 63 6f 72 64 20  of(SorterRecord 
28830 2a 29 29 3b 0d 0a 20 20 69 66 28 20 21 61 53 6c  *));..  if( !aSl
28831 6f 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ot ){..    retur
28832 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
28833 0a 20 20 7d 0d 0a 0d 0a 20 20 70 20 3d 20 70 53  .  }....  p = pS
28834 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 3b 0d  orter->pRecord;.
28835 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0d 0a  .  while( p ){..
28836 20 20 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64      SorterRecord
28837 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65   *pNext = p->pNe
28838 78 74 3b 0d 0a 20 20 20 20 70 2d 3e 70 4e 65 78  xt;..    p->pNex
28839 74 20 3d 20 30 3b 0d 0a 20 20 20 20 66 6f 72 28  t = 0;..    for(
2883a 69 3d 30 3b 20 61 53 6c 6f 74 5b 69 5d 3b 20 69  i=0; aSlot[i]; i
2883b 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 76 64 62 65  ++){..      vdbe
2883c 53 6f 72 74 65 72 4d 65 72 67 65 28 70 43 73 72  SorterMerge(pCsr
2883d 2c 20 70 2c 20 61 53 6c 6f 74 5b 69 5d 2c 20 26  , p, aSlot[i], &
2883e 70 29 3b 0d 0a 20 20 20 20 20 20 61 53 6c 6f 74  p);..      aSlot
2883f 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d  [i] = 0;..    }.
28840 0a 20 20 20 20 61 53 6c 6f 74 5b 69 5d 20 3d 20  .    aSlot[i] = 
28841 70 3b 0d 0a 20 20 20 20 70 20 3d 20 70 4e 65 78  p;..    p = pNex
28842 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 20 3d  t;..  }....  p =
28843 20 30 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20   0;..  for(i=0; 
28844 69 3c 36 34 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  i<64; i++){..   
28845 20 76 64 62 65 53 6f 72 74 65 72 4d 65 72 67 65   vdbeSorterMerge
28846 28 70 43 73 72 2c 20 70 2c 20 61 53 6c 6f 74 5b  (pCsr, p, aSlot[
28847 69 5d 2c 20 26 70 29 3b 0d 0a 20 20 7d 0d 0a 20  i], &p);..  }.. 
28848 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72   pSorter->pRecor
28849 64 20 3d 20 70 3b 0d 0a 0d 0a 20 20 73 71 6c 69  d = p;....  sqli
2884a 74 65 33 5f 66 72 65 65 28 61 53 6c 6f 74 29 3b  te3_free(aSlot);
2884b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
2884c 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a  E_OK;..}....../*
2884d 0d 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63  ..** Write the c
2884e 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
2884f 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  of the in-memory
28850 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 74 6f 20   linked-list to 
28851 61 20 50 4d 41 2e 20 52 65 74 75 72 6e 0d 0a 2a  a PMA. Return..*
28852 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73  * SQLITE_OK if s
28853 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e  uccessful, or an
28854 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
28855 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0d 0a 2a  de otherwise...*
28856 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  *..** The format
28857 20 6f 66 20 61 20 50 4d 41 20 69 73 3a 0d 0a 2a   of a PMA is:..*
28858 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 41 20 76 61  *..**     * A va
28859 72 69 6e 74 2e 20 54 68 69 73 20 76 61 72 69 6e  rint. This varin
2885a 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  t contains the t
2885b 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  otal number of b
2885c 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 0d  ytes of content.
2885d 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65  .**       in the
2885e 20 50 4d 41 20 28 6e 6f 74 20 69 6e 63 6c 75 64   PMA (not includ
2885f 69 6e 67 20 74 68 65 20 76 61 72 69 6e 74 20 69  ing the varint i
28860 74 73 65 6c 66 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tself)...**..** 
28861 20 20 20 20 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72      * One or mor
28862 65 20 72 65 63 6f 72 64 73 20 70 61 63 6b 65 64  e records packed
28863 20 65 6e 64 2d 74 6f 2d 65 6e 64 20 69 6e 20 6f   end-to-end in o
28864 72 64 65 72 20 6f 66 20 61 73 63 65 6e 64 69 6e  rder of ascendin
28865 67 20 6b 65 79 73 2e 20 0d 0a 2a 2a 20 20 20 20  g keys. ..**    
28866 20 20 20 45 61 63 68 20 72 65 63 6f 72 64 20 63     Each record c
28867 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 76 61 72  onsists of a var
28868 69 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  int followed by 
28869 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 28  a blob of data (
2886a 74 68 65 20 0d 0a 2a 2a 20 20 20 20 20 20 20 6b  the ..**       k
2886b 65 79 29 2e 20 54 68 65 20 76 61 72 69 6e 74 20  ey). The varint 
2886c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2886d 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 62 6c   bytes in the bl
2886e 6f 62 20 6f 66 20 64 61 74 61 2e 0d 0a 2a 2f 0d  ob of data...*/.
2886f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65  .static int vdbe
28870 53 6f 72 74 65 72 4c 69 73 74 54 6f 50 4d 41 28  SorterListToPMA(
28871 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62  sqlite3 *db, Vdb
28872 65 43 75 72 73 6f 72 20 2a 70 43 73 72 29 7b 0d  eCursor *pCsr){.
28873 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
28874 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
28875 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
28876 65 20 2a 2f 0d 0a 20 20 56 64 62 65 53 6f 72 74  e */..  VdbeSort
28877 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70 43  er *pSorter = pC
28878 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0d 0a 0d 0a  sr->pSorter;....
28879 20 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 6e    if( pSorter->n
2887a 49 6e 4d 65 6d 6f 72 79 3d 3d 30 20 29 7b 0d 0a  InMemory==0 ){..
2887b 20 20 20 20 61 73 73 65 72 74 28 20 70 53 6f 72      assert( pSor
2887c 74 65 72 2d 3e 70 52 65 63 6f 72 64 3d 3d 30 20  ter->pRecord==0 
2887d 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72  );..    return r
2887e 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20  c;..  }....  rc 
2887f 3d 20 76 64 62 65 53 6f 72 74 65 72 53 6f 72 74  = vdbeSorterSort
28880 28 70 43 73 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  (pCsr);....  /* 
28881 49 66 20 74 68 65 20 66 69 72 73 74 20 74 65 6d  If the first tem
28882 70 6f 72 61 72 79 20 50 4d 41 20 66 69 6c 65 20  porary PMA file 
28883 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6f 70 65  has not been ope
28884 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77  ned, open it now
28885 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 3d 3d  . */..  if( rc==
28886 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 53 6f  SQLITE_OK && pSo
28887 72 74 65 72 2d 3e 70 54 65 6d 70 31 3d 3d 30 20  rter->pTemp1==0 
28888 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 76 64 62  ){..    rc = vdb
28889 65 53 6f 72 74 65 72 4f 70 65 6e 54 65 6d 70 46  eSorterOpenTempF
2888a 69 6c 65 28 64 62 2c 20 26 70 53 6f 72 74 65 72  ile(db, &pSorter
2888b 2d 3e 70 54 65 6d 70 31 29 3b 0d 0a 20 20 20 20  ->pTemp1);..    
2888c 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
2888d 54 45 5f 4f 4b 20 7c 7c 20 70 53 6f 72 74 65 72  TE_OK || pSorter
2888e 2d 3e 70 54 65 6d 70 31 20 29 3b 0d 0a 20 20 20  ->pTemp1 );..   
2888f 20 61 73 73 65 72 74 28 20 70 53 6f 72 74 65 72   assert( pSorter
28890 2d 3e 69 57 72 69 74 65 4f 66 66 3d 3d 30 20 29  ->iWriteOff==0 )
28891 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
28892 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 3d 3d 30 20  Sorter->nPMA==0 
28893 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  );..  }....  if(
28894 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
28895 7b 0d 0a 20 20 20 20 69 36 34 20 69 4f 66 66 20  {..    i64 iOff 
28896 3d 20 70 53 6f 72 74 65 72 2d 3e 69 57 72 69 74  = pSorter->iWrit
28897 65 4f 66 66 3b 0d 0a 20 20 20 20 53 6f 72 74 65  eOff;..    Sorte
28898 72 52 65 63 6f 72 64 20 2a 70 3b 0d 0a 20 20 20  rRecord *p;..   
28899 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20 2a 70   SorterRecord *p
2889a 4e 65 78 74 20 3d 20 30 3b 0d 0a 20 20 20 20 73  Next = 0;..    s
2889b 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2889c 20 65 69 67 68 74 5a 65 72 6f 73 5b 38 5d 20 3d   eightZeros[8] =
2889d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30   { 0, 0, 0, 0, 0
2889e 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0d 0a 0d 0a  , 0, 0, 0 };....
2889f 20 20 20 20 70 53 6f 72 74 65 72 2d 3e 6e 50 4d      pSorter->nPM
288a0 41 2b 2b 3b 0d 0a 20 20 20 20 72 63 20 3d 20 76  A++;..    rc = v
288a1 64 62 65 53 6f 72 74 65 72 57 72 69 74 65 56 61  dbeSorterWriteVa
288a2 72 69 6e 74 28 70 53 6f 72 74 65 72 2d 3e 70 54  rint(pSorter->pT
288a3 65 6d 70 31 2c 20 70 53 6f 72 74 65 72 2d 3e 6e  emp1, pSorter->n
288a4 49 6e 4d 65 6d 6f 72 79 2c 20 26 69 4f 66 66 29  InMemory, &iOff)
288a5 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d 70 53 6f  ;..    for(p=pSo
288a6 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 3b 20 72  rter->pRecord; r
288a7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
288a8 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0d 0a 20 20  p; p=pNext){..  
288a9 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
288aa 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 72 63 20  Next;..      rc 
288ab 3d 20 76 64 62 65 53 6f 72 74 65 72 57 72 69 74  = vdbeSorterWrit
288ac 65 56 61 72 69 6e 74 28 70 53 6f 72 74 65 72 2d  eVarint(pSorter-
288ad 3e 70 54 65 6d 70 31 2c 20 70 2d 3e 6e 56 61 6c  >pTemp1, p->nVal
288ae 2c 20 26 69 4f 66 66 29 3b 0d 0a 0d 0a 20 20 20  , &iOff);....   
288af 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
288b0 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
288b1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
288b2 72 69 74 65 28 70 53 6f 72 74 65 72 2d 3e 70 54  rite(pSorter->pT
288b3 65 6d 70 31 2c 20 70 2d 3e 70 56 61 6c 2c 20 70  emp1, p->pVal, p
288b4 2d 3e 6e 56 61 6c 2c 20 69 4f 66 66 29 3b 0d 0a  ->nVal, iOff);..
288b5 20 20 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20          iOff += 
288b6 70 2d 3e 6e 56 61 6c 3b 0d 0a 20 20 20 20 20 20  p->nVal;..      
288b7 7d 0d 0a 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  }....      sqlit
288b8 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
288b9 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
288ba 2a 20 54 68 69 73 20 61 73 73 65 72 74 20 76 65  * This assert ve
288bb 72 69 66 69 65 73 20 74 68 61 74 20 75 6e 6c 65  rifies that unle
288bc 73 73 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20  ss an error has 
288bd 6f 63 63 75 72 72 65 64 2c 20 74 68 65 20 73 69  occurred, the si
288be 7a 65 20 6f 66 20 0d 0a 20 20 20 20 2a 2a 20 74  ze of ..    ** t
288bf 68 65 20 50 4d 41 20 6f 6e 20 64 69 73 6b 20 69  he PMA on disk i
288c0 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
288c1 65 20 65 78 70 65 63 74 65 64 20 73 69 7a 65 20  e expected size 
288c2 73 74 6f 72 65 64 20 69 6e 0d 0a 20 20 20 20 2a  stored in..    *
288c3 2a 20 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65  * pSorter->nInMe
288c4 6d 6f 72 79 2e 20 2a 2f 20 0d 0a 20 20 20 20 61  mory. */ ..    a
288c5 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
288c6 45 5f 4f 4b 20 7c 7c 20 70 53 6f 72 74 65 72 2d  E_OK || pSorter-
288c7 3e 6e 49 6e 4d 65 6d 6f 72 79 3d 3d 28 0d 0a 20  >nInMemory==(.. 
288c8 20 20 20 20 20 20 20 20 20 69 4f 66 66 2d 70 53           iOff-pS
288c9 6f 72 74 65 72 2d 3e 69 57 72 69 74 65 4f 66 66  orter->iWriteOff
288ca 2d 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65  -sqlite3VarintLe
288cb 6e 28 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65  n(pSorter->nInMe
288cc 6d 6f 72 79 29 0d 0a 20 20 20 20 29 29 3b 0d 0a  mory)..    ));..
288cd 0d 0a 20 20 20 20 70 53 6f 72 74 65 72 2d 3e 69  ..    pSorter->i
288ce 57 72 69 74 65 4f 66 66 20 3d 20 69 4f 66 66 3b  WriteOff = iOff;
288cf 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
288d0 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
288d1 20 20 2f 2a 20 54 65 72 6d 69 6e 61 74 65 20 65    /* Terminate e
288d2 61 63 68 20 66 69 6c 65 20 77 69 74 68 20 38 20  ach file with 8 
288d3 65 78 74 72 61 20 62 79 74 65 73 20 73 6f 20 74  extra bytes so t
288d4 68 61 74 20 66 72 6f 6d 20 61 6e 79 20 6f 66 66  hat from any off
288d5 73 65 74 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6e  set..      ** in
288d6 20 74 68 65 20 66 69 6c 65 20 77 65 20 63 61 6e   the file we can
288d7 20 61 6c 77 61 79 73 20 72 65 61 64 20 39 20 62   always read 9 b
288d8 79 74 65 73 20 77 69 74 68 6f 75 74 20 61 20 53  ytes without a S
288d9 48 4f 52 54 5f 52 45 41 44 20 65 72 72 6f 72 20  HORT_READ error 
288da 2a 2f 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  */..      rc = s
288db 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 53  qlite3OsWrite(pS
288dc 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31 2c 20 65  orter->pTemp1, e
288dd 69 67 68 74 5a 65 72 6f 73 2c 20 38 2c 20 69 4f  ightZeros, 8, iO
288de 66 66 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ff);..    }..   
288df 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72   pSorter->pRecor
288e0 64 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  d = p;..  }.... 
288e1 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
288e2 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61 20 72  ../*..** Add a r
288e3 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 6f 72  ecord to the sor
288e4 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ter...*/..SQLITE
288e5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
288e6 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 57 72  ite3VdbeSorterWr
288e7 69 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  ite(..  sqlite3 
288e8 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
288e9 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
288ea 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ase handle */.. 
288eb 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73   VdbeCursor *pCs
288ec 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
288ed 20 2f 2a 20 53 6f 72 74 65 72 20 63 75 72 73 6f   /* Sorter curso
288ee 72 20 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 56 61  r */..  Mem *pVa
288ef 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
288f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
288f1 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e  y cell containin
288f2 67 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 29 7b 0d  g record */..){.
288f3 0a 20 20 56 64 62 65 53 6f 72 74 65 72 20 2a 70  .  VdbeSorter *p
288f4 53 6f 72 74 65 72 20 3d 20 70 43 73 72 2d 3e 70  Sorter = pCsr->p
288f5 53 6f 72 74 65 72 3b 0d 0a 20 20 69 6e 74 20 72  Sorter;..  int r
288f6 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
288f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
288f8 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a 20 20  turn Code */..  
288f9 53 6f 72 74 65 72 52 65 63 6f 72 64 20 2a 70 4e  SorterRecord *pN
288fa 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
288fb 2f 2a 20 4e 65 77 20 6c 69 73 74 20 65 6c 65 6d  /* New list elem
288fc 65 6e 74 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65  ent */....  asse
288fd 72 74 28 20 70 53 6f 72 74 65 72 20 29 3b 0d 0a  rt( pSorter );..
288fe 20 20 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65    pSorter->nInMe
288ff 6d 6f 72 79 20 2b 3d 20 73 71 6c 69 74 65 33 56  mory += sqlite3V
28900 61 72 69 6e 74 4c 65 6e 28 70 56 61 6c 2d 3e 6e  arintLen(pVal->n
28901 29 20 2b 20 70 56 61 6c 2d 3e 6e 3b 0d 0a 0d 0a  ) + pVal->n;....
28902 20 20 70 4e 65 77 20 3d 20 28 53 6f 72 74 65 72    pNew = (Sorter
28903 52 65 63 6f 72 64 20 2a 29 73 71 6c 69 74 65 33  Record *)sqlite3
28904 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
28905 70 56 61 6c 2d 3e 6e 20 2b 20 73 69 7a 65 6f 66  pVal->n + sizeof
28906 28 53 6f 72 74 65 72 52 65 63 6f 72 64 29 29 3b  (SorterRecord));
28907 0d 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ..  if( pNew==0 
28908 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
28909 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 65  ITE_NOMEM;..  }e
2890a 6c 73 65 7b 0d 0a 20 20 20 20 70 4e 65 77 2d 3e  lse{..    pNew->
2890b 70 56 61 6c 20 3d 20 28 76 6f 69 64 20 2a 29 26  pVal = (void *)&
2890c 70 4e 65 77 5b 31 5d 3b 0d 0a 20 20 20 20 6d 65  pNew[1];..    me
2890d 6d 63 70 79 28 70 4e 65 77 2d 3e 70 56 61 6c 2c  mcpy(pNew->pVal,
2890e 20 70 56 61 6c 2d 3e 7a 2c 20 70 56 61 6c 2d 3e   pVal->z, pVal->
2890f 6e 29 3b 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6e  n);..    pNew->n
28910 56 61 6c 20 3d 20 70 56 61 6c 2d 3e 6e 3b 0d 0a  Val = pVal->n;..
28911 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20      pNew->pNext 
28912 3d 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f  = pSorter->pReco
28913 72 64 3b 0d 0a 20 20 20 20 70 53 6f 72 74 65 72  rd;..    pSorter
28914 2d 3e 70 52 65 63 6f 72 64 20 3d 20 70 4e 65 77  ->pRecord = pNew
28915 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53  ;..  }....  /* S
28916 65 65 20 69 66 20 74 68 65 20 63 6f 6e 74 65 6e  ee if the conten
28917 74 73 20 6f 66 20 74 68 65 20 73 6f 72 74 65 72  ts of the sorter
28918 20 73 68 6f 75 6c 64 20 6e 6f 77 20 62 65 20 77   should now be w
28919 72 69 74 74 65 6e 20 6f 75 74 2e 20 54 68 65 79  ritten out. They
2891a 0d 0a 20 20 2a 2a 20 61 72 65 20 77 72 69 74 74  ..  ** are writt
2891b 65 6e 20 6f 75 74 20 77 68 65 6e 20 65 69 74 68  en out when eith
2891c 65 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  er of the follow
2891d 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0d 0a 20  ing are true:.. 
2891e 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 2a 20 54 68   **..  **   * Th
2891f 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61  e total memory a
28920 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65  llocated for the
28921 20 69 6e 2d 6d 65 6d 6f 72 79 20 6c 69 73 74 20   in-memory list 
28922 69 73 20 67 72 65 61 74 65 72 20 0d 0a 20 20 2a  is greater ..  *
28923 2a 20 20 20 20 20 74 68 61 6e 20 28 70 61 67 65  *     than (page
28924 2d 73 69 7a 65 20 2a 20 63 61 63 68 65 2d 73 69  -size * cache-si
28925 7a 65 29 2c 20 6f 72 0d 0a 20 20 2a 2a 0d 0a 20  ze), or..  **.. 
28926 20 2a 2a 20 20 20 2a 20 54 68 65 20 74 6f 74 61   **   * The tota
28927 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
28928 65 64 20 66 6f 72 20 74 68 65 20 69 6e 2d 6d 65  ed for the in-me
28929 6d 6f 72 79 20 6c 69 73 74 20 69 73 20 67 72 65  mory list is gre
2892a 61 74 65 72 20 0d 0a 20 20 2a 2a 20 20 20 20 20  ater ..  **     
2892b 74 68 61 6e 20 28 70 61 67 65 2d 73 69 7a 65 20  than (page-size 
2892c 2a 20 31 30 29 20 61 6e 64 20 73 71 6c 69 74 65  * 10) and sqlite
2892d 33 48 65 61 70 4e 65 61 72 6c 79 46 75 6c 6c 28  3HeapNearlyFull(
2892e 29 20 72 65 74 75 72 6e 73 20 74 72 75 65 2e 0d  ) returns true..
2892f 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 3d  .  */..  if( rc=
28930 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 53  =SQLITE_OK && pS
28931 6f 72 74 65 72 2d 3e 6d 78 50 6d 61 53 69 7a 65  orter->mxPmaSize
28932 3e 30 20 26 26 20 28 0d 0a 20 20 20 20 20 20 20  >0 && (..       
28933 20 28 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65   (pSorter->nInMe
28934 6d 6f 72 79 3e 70 53 6f 72 74 65 72 2d 3e 6d 78  mory>pSorter->mx
28935 50 6d 61 53 69 7a 65 29 0d 0a 20 20 20 20 20 7c  PmaSize)..     |
28936 7c 20 28 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d  | (pSorter->nInM
28937 65 6d 6f 72 79 3e 70 53 6f 72 74 65 72 2d 3e 6d  emory>pSorter->m
28938 6e 50 6d 61 53 69 7a 65 20 26 26 20 73 71 6c 69  nPmaSize && sqli
28939 74 65 33 48 65 61 70 4e 65 61 72 6c 79 46 75 6c  te3HeapNearlyFul
2893a 6c 28 29 29 0d 0a 20 20 29 29 7b 0d 0a 20 20 20  l())..  )){..   
2893b 20 72 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72   rc = vdbeSorter
2893c 4c 69 73 74 54 6f 50 4d 41 28 64 62 2c 20 70 43  ListToPMA(db, pC
2893d 73 72 29 3b 0d 0a 20 20 20 20 70 53 6f 72 74 65  sr);..    pSorte
2893e 72 2d 3e 6e 49 6e 4d 65 6d 6f 72 79 20 3d 20 30  r->nInMemory = 0
2893f 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75  ;..  }....  retu
28940 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
28941 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
28942 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 56  ion for sqlite3V
28943 64 62 65 53 6f 72 74 65 72 52 65 77 69 6e 64 28  dbeSorterRewind(
28944 29 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ). ..*/..static 
28945 69 6e 74 20 76 64 62 65 53 6f 72 74 65 72 49 6e  int vdbeSorterIn
28946 69 74 4d 65 72 67 65 28 0d 0a 20 20 73 71 6c 69  itMerge(..  sqli
28947 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
28948 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
28949 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
2894a 2f 0d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  /..  VdbeCursor 
2894b 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
2894c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 68       /* Cursor h
2894d 61 6e 64 6c 65 20 66 6f 72 20 74 68 69 73 20 73  andle for this s
2894e 6f 72 74 65 72 20 2a 2f 0d 0a 20 20 69 36 34 20  orter */..  i64 
2894f 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 20 20  *pnByte         
28950 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
28951 75 6d 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  um of bytes in a
28952 6c 6c 20 6f 70 65 6e 65 64 20 50 4d 41 73 20 2a  ll opened PMAs *
28953 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62 65 53 6f 72  /..){..  VdbeSor
28954 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70  ter *pSorter = p
28955 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0d 0a 20  Csr->pSorter;.. 
28956 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
28957 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
28958 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
28959 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  */..  int i;    
2895a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2895b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
2895c 20 69 74 65 72 61 74 6f 72 20 74 68 72 6f 75 67   iterator throug
2895d 68 20 61 49 74 65 72 5b 5d 20 2a 2f 0d 0a 20 20  h aIter[] */..  
2895e 69 36 34 20 6e 42 79 74 65 20 3d 20 30 3b 20 20  i64 nByte = 0;  
2895f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28960 2f 2a 20 54 6f 74 61 6c 20 62 79 74 65 73 20 69  /* Total bytes i
28961 6e 20 61 6c 6c 20 6f 70 65 6e 65 64 20 50 4d 41  n all opened PMA
28962 73 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 69  s */....  /* Ini
28963 74 69 61 6c 69 7a 65 20 74 68 65 20 69 74 65 72  tialize the iter
28964 61 74 6f 72 73 2e 20 2a 2f 0d 0a 20 20 66 6f 72  ators. */..  for
28965 28 69 3d 30 3b 20 69 3c 53 4f 52 54 45 52 5f 4d  (i=0; i<SORTER_M
28966 41 58 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20  AX_MERGE_COUNT; 
28967 69 2b 2b 29 7b 0d 0a 20 20 20 20 56 64 62 65 53  i++){..    VdbeS
28968 6f 72 74 65 72 49 74 65 72 20 2a 70 49 74 65 72  orterIter *pIter
28969 20 3d 20 26 70 53 6f 72 74 65 72 2d 3e 61 49 74   = &pSorter->aIt
2896a 65 72 5b 69 5d 3b 0d 0a 20 20 20 20 72 63 20 3d  er[i];..    rc =
2896b 20 76 64 62 65 53 6f 72 74 65 72 49 74 65 72 49   vdbeSorterIterI
2896c 6e 69 74 28 64 62 2c 20 70 53 6f 72 74 65 72 2c  nit(db, pSorter,
2896d 20 70 53 6f 72 74 65 72 2d 3e 69 52 65 61 64 4f   pSorter->iReadO
2896e 66 66 2c 20 70 49 74 65 72 2c 20 26 6e 42 79 74  ff, pIter, &nByt
2896f 65 29 3b 0d 0a 20 20 20 20 70 53 6f 72 74 65 72  e);..    pSorter
28970 2d 3e 69 52 65 61 64 4f 66 66 20 3d 20 70 49 74  ->iReadOff = pIt
28971 65 72 2d 3e 69 45 6f 66 3b 0d 0a 20 20 20 20 61  er->iEof;..    a
28972 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
28973 45 5f 4f 4b 20 7c 7c 20 70 53 6f 72 74 65 72 2d  E_OK || pSorter-
28974 3e 69 52 65 61 64 4f 66 66 3c 3d 70 53 6f 72 74  >iReadOff<=pSort
28975 65 72 2d 3e 69 57 72 69 74 65 4f 66 66 20 29 3b  er->iWriteOff );
28976 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
28977 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53 6f 72 74  LITE_OK || pSort
28978 65 72 2d 3e 69 52 65 61 64 4f 66 66 3e 3d 70 53  er->iReadOff>=pS
28979 6f 72 74 65 72 2d 3e 69 57 72 69 74 65 4f 66 66  orter->iWriteOff
2897a 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a   ) break;..  }..
2897b 0d 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
2897c 65 20 74 68 65 20 61 54 72 65 65 5b 5d 20 61 72  e the aTree[] ar
2897d 72 61 79 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69  ray. */..  for(i
2897e 3d 70 53 6f 72 74 65 72 2d 3e 6e 54 72 65 65 2d  =pSorter->nTree-
2897f 31 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  1; rc==SQLITE_OK
28980 20 26 26 20 69 3e 30 3b 20 69 2d 2d 29 7b 0d 0a   && i>0; i--){..
28981 20 20 20 20 72 63 20 3d 20 76 64 62 65 53 6f 72      rc = vdbeSor
28982 74 65 72 44 6f 43 6f 6d 70 61 72 65 28 70 43 73  terDoCompare(pCs
28983 72 2c 20 69 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  r, i);..  }.... 
28984 20 2a 70 6e 42 79 74 65 20 3d 20 6e 42 79 74 65   *pnByte = nByte
28985 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
28986 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 6e 63  .}..../*..** Onc
28987 65 20 74 68 65 20 73 6f 72 74 65 72 20 68 61 73  e the sorter has
28988 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64 2c   been populated,
28989 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
2898a 73 20 63 61 6c 6c 65 64 20 74 6f 20 70 72 65 70  s called to prep
2898b 61 72 65 0d 0a 2a 2a 20 66 6f 72 20 69 74 65 72  are..** for iter
2898c 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20 69 74  ating through it
2898d 73 20 63 6f 6e 74 65 6e 74 73 20 69 6e 20 73 6f  s contents in so
2898e 72 74 65 64 20 6f 72 64 65 72 2e 0d 0a 2a 2f 0d  rted order...*/.
2898f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28990 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53  int sqlite3VdbeS
28991 6f 72 74 65 72 52 65 77 69 6e 64 28 73 71 6c 69  orterRewind(sqli
28992 74 65 33 20 2a 64 62 2c 20 56 64 62 65 43 75 72  te3 *db, VdbeCur
28993 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20 2a  sor *pCsr, int *
28994 70 62 45 6f 66 29 7b 0d 0a 20 20 56 64 62 65 53  pbEof){..  VdbeS
28995 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d  orter *pSorter =
28996 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0d   pCsr->pSorter;.
28997 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
28998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28999 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
2899a 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  e */..  sqlite3_
2899b 66 69 6c 65 20 2a 70 54 65 6d 70 32 20 3d 20 30  file *pTemp2 = 0
2899c 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  ;       /* Secon
2899d 64 20 74 65 6d 70 20 66 69 6c 65 20 74 6f 20 75  d temp file to u
2899e 73 65 20 2a 2f 0d 0a 20 20 69 36 34 20 69 57 72  se */..  i64 iWr
2899f 69 74 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20  ite2 = 0;       
289a0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
289a1 65 20 6f 66 66 73 65 74 20 66 6f 72 20 70 54 65  e offset for pTe
289a2 6d 70 32 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 49  mp2 */..  int nI
289a3 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ter;            
289a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
289a5 62 65 72 20 6f 66 20 69 74 65 72 61 74 6f 72 73  ber of iterators
289a6 20 75 73 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20   used */..  int 
289a7 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20  nByte;          
289a8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
289a9 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 72 65  ytes of space re
289aa 71 75 69 72 65 64 20 66 6f 72 20 61 49 74 65 72  quired for aIter
289ab 2f 61 54 72 65 65 20 2a 2f 0d 0a 20 20 69 6e 74  /aTree */..  int
289ac 20 4e 20 3d 20 32 3b 20 20 20 20 20 20 20 20 20   N = 2;         
289ad 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
289ae 50 6f 77 65 72 20 6f 66 20 32 20 3e 3d 20 6e 49  Power of 2 >= nI
289af 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65  ter */....  asse
289b0 72 74 28 20 70 53 6f 72 74 65 72 20 29 3b 0d 0a  rt( pSorter );..
289b1 0d 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 64 61 74  ..  /* If no dat
289b2 61 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74  a has been writt
289b3 65 6e 20 74 6f 20 64 69 73 6b 2c 20 74 68 65 6e  en to disk, then
289b4 20 64 6f 20 6e 6f 74 20 64 6f 20 73 6f 20 6e 6f   do not do so no
289b5 77 2e 20 49 6e 73 74 65 61 64 2c 0d 0a 20 20 2a  w. Instead,..  *
289b6 2a 20 73 6f 72 74 20 74 68 65 20 56 64 62 65 53  * sort the VdbeS
289b7 6f 72 74 65 72 2e 70 52 65 63 6f 72 64 20 6c 69  orter.pRecord li
289b8 73 74 2e 20 54 68 65 20 76 64 62 65 20 6c 61 79  st. The vdbe lay
289b9 65 72 20 77 69 6c 6c 20 72 65 61 64 20 64 61 74  er will read dat
289ba 61 20 64 69 72 65 63 74 6c 79 0d 0a 20 20 2a 2a  a directly..  **
289bb 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
289bc 6f 72 79 20 6c 69 73 74 2e 20 20 2a 2f 0d 0a 20  ory list.  */.. 
289bd 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 6e 50   if( pSorter->nP
289be 4d 41 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70  MA==0 ){..    *p
289bf 62 45 6f 66 20 3d 20 21 70 53 6f 72 74 65 72 2d  bEof = !pSorter-
289c0 3e 70 52 65 63 6f 72 64 3b 0d 0a 20 20 20 20 61  >pRecord;..    a
289c1 73 73 65 72 74 28 20 70 53 6f 72 74 65 72 2d 3e  ssert( pSorter->
289c2 61 54 72 65 65 3d 3d 30 20 29 3b 0d 0a 20 20 20  aTree==0 );..   
289c3 20 72 65 74 75 72 6e 20 76 64 62 65 53 6f 72 74   return vdbeSort
289c4 65 72 53 6f 72 74 28 70 43 73 72 29 3b 0d 0a 20  erSort(pCsr);.. 
289c5 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 57 72 69 74 65   }....  /* Write
289c6 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 2d 74   the current b-t
289c7 72 65 65 20 74 6f 20 61 20 50 4d 41 2e 20 43 6c  ree to a PMA. Cl
289c8 6f 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 63  ose the b-tree c
289c9 75 72 73 6f 72 2e 20 2a 2f 0d 0a 20 20 72 63 20  ursor. */..  rc 
289ca 3d 20 76 64 62 65 53 6f 72 74 65 72 4c 69 73 74  = vdbeSorterList
289cb 54 6f 50 4d 41 28 64 62 2c 20 70 43 73 72 29 3b  ToPMA(db, pCsr);
289cc 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
289cd 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
289ce 63 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63  c;....  /* Alloc
289cf 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 61 49  ate space for aI
289d0 74 65 72 5b 5d 20 61 6e 64 20 61 54 72 65 65 5b  ter[] and aTree[
289d1 5d 2e 20 2a 2f 0d 0a 20 20 6e 49 74 65 72 20 3d  ]. */..  nIter =
289d2 20 70 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 3b 0d   pSorter->nPMA;.
289d3 0a 20 20 69 66 28 20 6e 49 74 65 72 3e 53 4f 52  .  if( nIter>SOR
289d4 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45 5f 43 4f  TER_MAX_MERGE_CO
289d5 55 4e 54 20 29 20 6e 49 74 65 72 20 3d 20 53 4f  UNT ) nIter = SO
289d6 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45 5f 43  RTER_MAX_MERGE_C
289d7 4f 55 4e 54 3b 0d 0a 20 20 61 73 73 65 72 74 28  OUNT;..  assert(
289d8 20 6e 49 74 65 72 3e 30 20 29 3b 0d 0a 20 20 77   nIter>0 );..  w
289d9 68 69 6c 65 28 20 4e 3c 6e 49 74 65 72 20 29 20  hile( N<nIter ) 
289da 4e 20 2b 3d 20 4e 3b 0d 0a 20 20 6e 42 79 74 65  N += N;..  nByte
289db 20 3d 20 4e 20 2a 20 28 73 69 7a 65 6f 66 28 69   = N * (sizeof(i
289dc 6e 74 29 20 2b 20 73 69 7a 65 6f 66 28 56 64 62  nt) + sizeof(Vdb
289dd 65 53 6f 72 74 65 72 49 74 65 72 29 29 3b 0d 0a  eSorterIter));..
289de 20 20 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72    pSorter->aIter
289df 20 3d 20 28 56 64 62 65 53 6f 72 74 65 72 49 74   = (VdbeSorterIt
289e0 65 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  er *)sqlite3DbMa
289e1 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79  llocZero(db, nBy
289e2 74 65 29 3b 0d 0a 20 20 69 66 28 20 21 70 53 6f  te);..  if( !pSo
289e3 72 74 65 72 2d 3e 61 49 74 65 72 20 29 20 72 65  rter->aIter ) re
289e4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
289e5 4d 3b 0d 0a 20 20 70 53 6f 72 74 65 72 2d 3e 61  M;..  pSorter->a
289e6 54 72 65 65 20 3d 20 28 69 6e 74 20 2a 29 26 70  Tree = (int *)&p
289e7 53 6f 72 74 65 72 2d 3e 61 49 74 65 72 5b 4e 5d  Sorter->aIter[N]
289e8 3b 0d 0a 20 20 70 53 6f 72 74 65 72 2d 3e 6e 54  ;..  pSorter->nT
289e9 72 65 65 20 3d 20 4e 3b 0d 0a 0d 0a 20 20 64 6f  ree = N;....  do
289ea 20 7b 0d 0a 20 20 20 20 69 6e 74 20 69 4e 65 77   {..    int iNew
289eb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
289ec 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
289ed 66 20 6e 65 77 2c 20 6d 65 72 67 65 64 2c 20 50  f new, merged, P
289ee 4d 41 20 2a 2f 0d 0a 0d 0a 20 20 20 20 66 6f 72  MA */....    for
289ef 28 69 4e 65 77 3d 30 3b 20 0d 0a 20 20 20 20 20  (iNew=0; ..     
289f0 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b     rc==SQLITE_OK
289f1 20 26 26 20 69 4e 65 77 2a 53 4f 52 54 45 52 5f   && iNew*SORTER_
289f2 4d 41 58 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 3c  MAX_MERGE_COUNT<
289f3 70 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 3b 20 0d  pSorter->nPMA; .
289f4 0a 20 20 20 20 20 20 20 20 69 4e 65 77 2b 2b 0d  .        iNew++.
289f5 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 69  .    ){..      i
289f6 36 34 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20  64 nWrite;      
289f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
289f8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
289f9 20 6e 65 77 20 50 4d 41 20 2a 2f 0d 0a 0d 0a 20   new PMA */.... 
289fa 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
289fb 20 61 72 65 20 53 4f 52 54 45 52 5f 4d 41 58 5f   are SORTER_MAX_
289fc 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 72 20 6c  MERGE_COUNT or l
289fd 65 73 73 20 50 4d 41 73 20 69 6e 20 66 69 6c 65  ess PMAs in file
289fe 20 70 54 65 6d 70 31 2c 0d 0a 20 20 20 20 20 20   pTemp1,..      
289ff 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** initialize an
28a00 20 69 74 65 72 61 74 6f 72 20 66 6f 72 20 65 61   iterator for ea
28a01 63 68 20 6f 66 20 74 68 65 6d 20 61 6e 64 20 62  ch of them and b
28a02 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
28a03 6c 6f 6f 70 2e 0d 0a 20 20 20 20 20 20 2a 2a 20  loop...      ** 
28a04 54 68 65 73 65 20 69 74 65 72 61 74 6f 72 73 20  These iterators 
28a05 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
28a06 74 61 6c 6c 79 20 6d 65 72 67 65 64 20 61 73 20  tally merged as 
28a07 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 63  the VDBE layer c
28a08 61 6c 6c 73 0d 0a 20 20 20 20 20 20 2a 2a 20 73  alls..      ** s
28a09 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72  qlite3VdbeSorter
28a0a 4e 65 78 74 28 29 2e 0d 0a 20 20 20 20 20 20 2a  Next()...      *
28a0b 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 4f 74 68 65  *..      ** Othe
28a0c 72 77 69 73 65 2c 20 69 66 20 70 54 65 6d 70 31  rwise, if pTemp1
28a0d 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74   contains more t
28a0e 68 61 6e 20 53 4f 52 54 45 52 5f 4d 41 58 5f 4d  han SORTER_MAX_M
28a0f 45 52 47 45 5f 43 4f 55 4e 54 20 50 4d 41 73 2c  ERGE_COUNT PMAs,
28a10 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69  ..      ** initi
28a11 61 6c 69 7a 65 20 69 6e 74 65 72 61 74 6f 72 73  alize interators
28a12 20 66 6f 72 20 53 4f 52 54 45 52 5f 4d 41 58 5f   for SORTER_MAX_
28a13 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 74  MERGE_COUNT of t
28a14 68 65 6d 2e 20 54 68 65 73 65 20 50 4d 41 73 0d  hem. These PMAs.
28a15 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6d 65  .      ** are me
28a16 72 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  rged into a sing
28a17 6c 65 20 50 4d 41 20 74 68 61 74 20 69 73 20 77  le PMA that is w
28a18 72 69 74 74 65 6e 20 74 6f 20 66 69 6c 65 20 70  ritten to file p
28a19 54 65 6d 70 32 2e 0d 0a 20 20 20 20 20 20 2a 2f  Temp2...      */
28a1a 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 76 64 62  ..      rc = vdb
28a1b 65 53 6f 72 74 65 72 49 6e 69 74 4d 65 72 67 65  eSorterInitMerge
28a1c 28 64 62 2c 20 70 43 73 72 2c 20 26 6e 57 72 69  (db, pCsr, &nWri
28a1d 74 65 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  te);..      asse
28a1e 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
28a1f 4b 20 7c 7c 20 70 53 6f 72 74 65 72 2d 3e 61 49  K || pSorter->aI
28a20 74 65 72 5b 20 70 53 6f 72 74 65 72 2d 3e 61 54  ter[ pSorter->aT
28a21 72 65 65 5b 31 5d 20 5d 2e 70 46 69 6c 65 20 29  ree[1] ].pFile )
28a22 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  ;..      if( rc!
28a23 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53  =SQLITE_OK || pS
28a24 6f 72 74 65 72 2d 3e 6e 50 4d 41 3c 3d 53 4f 52  orter->nPMA<=SOR
28a25 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45 5f 43 4f  TER_MAX_MERGE_CO
28a26 55 4e 54 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  UNT ){..        
28a27 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d  break;..      }.
28a28 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e  ...      /* Open
28a29 20 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70   the second temp
28a2a 20 66 69 6c 65 2c 20 69 66 20 69 74 20 69 73 20   file, if it is 
28a2b 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
28a2c 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20  . */..      if( 
28a2d 70 54 65 6d 70 32 3d 3d 30 20 29 7b 0d 0a 20 20  pTemp2==0 ){..  
28a2e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 57        assert( iW
28a2f 72 69 74 65 32 3d 3d 30 20 29 3b 0d 0a 20 20 20  rite2==0 );..   
28a30 20 20 20 20 20 72 63 20 3d 20 76 64 62 65 53 6f       rc = vdbeSo
28a31 72 74 65 72 4f 70 65 6e 54 65 6d 70 46 69 6c 65  rterOpenTempFile
28a32 28 64 62 2c 20 26 70 54 65 6d 70 32 29 3b 0d 0a  (db, &pTemp2);..
28a33 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
28a34 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
28a35 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  OK ){..        r
28a36 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72 57 72  c = vdbeSorterWr
28a37 69 74 65 56 61 72 69 6e 74 28 70 54 65 6d 70 32  iteVarint(pTemp2
28a38 2c 20 6e 57 72 69 74 65 2c 20 26 69 57 72 69 74  , nWrite, &iWrit
28a39 65 32 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d  e2);..      }...
28a3a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
28a3b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
28a3c 20 20 20 20 20 69 6e 74 20 62 45 6f 66 20 3d 20       int bEof = 
28a3d 30 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c  0;..        whil
28a3e 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
28a3f 20 26 26 20 62 45 6f 66 3d 3d 30 20 29 7b 0d 0a   && bEof==0 ){..
28a40 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54            int nT
28a41 6f 57 72 69 74 65 3b 0d 0a 20 20 20 20 20 20 20  oWrite;..       
28a42 20 20 20 56 64 62 65 53 6f 72 74 65 72 49 74 65     VdbeSorterIte
28a43 72 20 2a 70 49 74 65 72 20 3d 20 26 70 53 6f 72  r *pIter = &pSor
28a44 74 65 72 2d 3e 61 49 74 65 72 5b 20 70 53 6f 72  ter->aIter[ pSor
28a45 74 65 72 2d 3e 61 54 72 65 65 5b 31 5d 20 5d 3b  ter->aTree[1] ];
28a46 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ..          asse
28a47 72 74 28 20 70 49 74 65 72 2d 3e 70 46 69 6c 65  rt( pIter->pFile
28a48 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e   );..          n
28a49 54 6f 57 72 69 74 65 20 3d 20 70 49 74 65 72 2d  ToWrite = pIter-
28a4a 3e 6e 4b 65 79 20 2b 20 73 71 6c 69 74 65 33 56  >nKey + sqlite3V
28a4b 61 72 69 6e 74 4c 65 6e 28 70 49 74 65 72 2d 3e  arintLen(pIter->
28a4c 6e 4b 65 79 29 3b 0d 0a 20 20 20 20 20 20 20 20  nKey);..        
28a4d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
28a4e 57 72 69 74 65 28 70 54 65 6d 70 32 2c 20 70 49  Write(pTemp2, pI
28a4f 74 65 72 2d 3e 61 41 6c 6c 6f 63 2c 20 6e 54 6f  ter->aAlloc, nTo
28a50 57 72 69 74 65 2c 20 69 57 72 69 74 65 32 29 3b  Write, iWrite2);
28a51 0d 0a 20 20 20 20 20 20 20 20 20 20 69 57 72 69  ..          iWri
28a52 74 65 32 20 2b 3d 20 6e 54 6f 57 72 69 74 65 3b  te2 += nToWrite;
28a53 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
28a54 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
28a55 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  ..            rc
28a56 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f   = sqlite3VdbeSo
28a57 72 74 65 72 4e 65 78 74 28 64 62 2c 20 70 43 73  rterNext(db, pCs
28a58 72 2c 20 26 62 45 6f 66 29 3b 0d 0a 20 20 20 20  r, &bEof);..    
28a59 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
28a5a 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
28a5b 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 53   }....    if( pS
28a5c 6f 72 74 65 72 2d 3e 6e 50 4d 41 3c 3d 53 4f 52  orter->nPMA<=SOR
28a5d 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45 5f 43 4f  TER_MAX_MERGE_CO
28a5e 55 4e 54 20 29 7b 0d 0a 20 20 20 20 20 20 62 72  UNT ){..      br
28a5f 65 61 6b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  eak;..    }else{
28a60 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
28a61 66 69 6c 65 20 2a 70 54 6d 70 20 3d 20 70 53 6f  file *pTmp = pSo
28a62 72 74 65 72 2d 3e 70 54 65 6d 70 31 3b 0d 0a 20  rter->pTemp1;.. 
28a63 20 20 20 20 20 70 53 6f 72 74 65 72 2d 3e 6e 50       pSorter->nP
28a64 4d 41 20 3d 20 69 4e 65 77 3b 0d 0a 20 20 20 20  MA = iNew;..    
28a65 20 20 70 53 6f 72 74 65 72 2d 3e 70 54 65 6d 70    pSorter->pTemp
28a66 31 20 3d 20 70 54 65 6d 70 32 3b 0d 0a 20 20 20  1 = pTemp2;..   
28a67 20 20 20 70 54 65 6d 70 32 20 3d 20 70 54 6d 70     pTemp2 = pTmp
28a68 3b 0d 0a 20 20 20 20 20 20 70 53 6f 72 74 65 72  ;..      pSorter
28a69 2d 3e 69 57 72 69 74 65 4f 66 66 20 3d 20 69 57  ->iWriteOff = iW
28a6a 72 69 74 65 32 3b 0d 0a 20 20 20 20 20 20 70 53  rite2;..      pS
28a6b 6f 72 74 65 72 2d 3e 69 52 65 61 64 4f 66 66 20  orter->iReadOff 
28a6c 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 57 72 69  = 0;..      iWri
28a6d 74 65 32 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d  te2 = 0;..    }.
28a6e 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53  .  }while( rc==S
28a6f 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 0d 0a 20  QLITE_OK );.... 
28a70 20 69 66 28 20 70 54 65 6d 70 32 20 29 7b 0d 0a   if( pTemp2 ){..
28a71 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
28a72 73 65 46 72 65 65 28 70 54 65 6d 70 32 29 3b 0d  seFree(pTemp2);.
28a73 0a 20 20 7d 0d 0a 20 20 2a 70 62 45 6f 66 20 3d  .  }..  *pbEof =
28a74 20 28 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72   (pSorter->aIter
28a75 5b 70 53 6f 72 74 65 72 2d 3e 61 54 72 65 65 5b  [pSorter->aTree[
28a76 31 5d 5d 2e 70 46 69 6c 65 3d 3d 30 29 3b 0d 0a  1]].pFile==0);..
28a77 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
28a78 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 76 61 6e 63  .../*..** Advanc
28a79 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6c  e to the next el
28a7a 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 73 6f 72  ement in the sor
28a7b 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ter...*/..SQLITE
28a7c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
28a7d 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 4e 65  ite3VdbeSorterNe
28a7e 78 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  xt(sqlite3 *db, 
28a7f 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73 72  VdbeCursor *pCsr
28a80 2c 20 69 6e 74 20 2a 70 62 45 6f 66 29 7b 0d 0a  , int *pbEof){..
28a81 20 20 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53    VdbeSorter *pS
28a82 6f 72 74 65 72 20 3d 20 70 43 73 72 2d 3e 70 53  orter = pCsr->pS
28a83 6f 72 74 65 72 3b 0d 0a 20 20 69 6e 74 20 72 63  orter;..  int rc
28a84 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
28a85 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
28a86 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20  urn code */.... 
28a87 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 61 54   if( pSorter->aT
28a88 72 65 65 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  ree ){..    int 
28a89 69 50 72 65 76 20 3d 20 70 53 6f 72 74 65 72 2d  iPrev = pSorter-
28a8a 3e 61 54 72 65 65 5b 31 5d 3b 2f 2a 20 49 6e 64  >aTree[1];/* Ind
28a8b 65 78 20 6f 66 20 69 74 65 72 61 74 6f 72 20 74  ex of iterator t
28a8c 6f 20 61 64 76 61 6e 63 65 20 2a 2f 0d 0a 20 20  o advance */..  
28a8d 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
28a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a8f 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 54 72 65  /* Index of aTre
28a90 65 5b 5d 20 74 6f 20 72 65 63 61 6c 63 75 6c 61  e[] to recalcula
28a91 74 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 72 63 20  te */....    rc 
28a92 3d 20 76 64 62 65 53 6f 72 74 65 72 49 74 65 72  = vdbeSorterIter
28a93 4e 65 78 74 28 64 62 2c 20 26 70 53 6f 72 74 65  Next(db, &pSorte
28a94 72 2d 3e 61 49 74 65 72 5b 69 50 72 65 76 5d 29  r->aIter[iPrev])
28a95 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 28 70 53  ;..    for(i=(pS
28a96 6f 72 74 65 72 2d 3e 6e 54 72 65 65 2b 69 50 72  orter->nTree+iPr
28a97 65 76 29 2f 32 3b 20 72 63 3d 3d 53 51 4c 49 54  ev)/2; rc==SQLIT
28a98 45 5f 4f 4b 20 26 26 20 69 3e 30 3b 20 69 3d 69  E_OK && i>0; i=i
28a99 2f 32 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  /2){..      rc =
28a9a 20 76 64 62 65 53 6f 72 74 65 72 44 6f 43 6f 6d   vdbeSorterDoCom
28a9b 70 61 72 65 28 70 43 73 72 2c 20 69 29 3b 0d 0a  pare(pCsr, i);..
28a9c 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2a 70 62      }....    *pb
28a9d 45 6f 66 20 3d 20 28 70 53 6f 72 74 65 72 2d 3e  Eof = (pSorter->
28a9e 61 49 74 65 72 5b 70 53 6f 72 74 65 72 2d 3e 61  aIter[pSorter->a
28a9f 54 72 65 65 5b 31 5d 5d 2e 70 46 69 6c 65 3d 3d  Tree[1]].pFile==
28aa0 30 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  0);..  }else{.. 
28aa1 20 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20     SorterRecord 
28aa2 2a 70 46 72 65 65 20 3d 20 70 53 6f 72 74 65 72  *pFree = pSorter
28aa3 2d 3e 70 52 65 63 6f 72 64 3b 0d 0a 20 20 20 20  ->pRecord;..    
28aa4 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64  pSorter->pRecord
28aa5 20 3d 20 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b   = pFree->pNext;
28aa6 0d 0a 20 20 20 20 70 46 72 65 65 2d 3e 70 4e 65  ..    pFree->pNe
28aa7 78 74 20 3d 20 30 3b 0d 0a 20 20 20 20 76 64 62  xt = 0;..    vdb
28aa8 65 53 6f 72 74 65 72 52 65 63 6f 72 64 46 72 65  eSorterRecordFre
28aa9 65 28 64 62 2c 20 70 46 72 65 65 29 3b 0d 0a 20  e(db, pFree);.. 
28aaa 20 20 20 2a 70 62 45 6f 66 20 3d 20 21 70 53 6f     *pbEof = !pSo
28aab 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 3b 0d 0a  rter->pRecord;..
28aac 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
28aad 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  OK;..  }..  retu
28aae 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
28aaf 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
28ab0 6e 74 65 72 20 74 6f 20 61 20 62 75 66 66 65 72  nter to a buffer
28ab1 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 73 6f   owned by the so
28ab2 72 74 65 72 20 74 68 61 74 20 63 6f 6e 74 61 69  rter that contai
28ab3 6e 73 20 74 68 65 20 0d 0a 2a 2a 20 63 75 72 72  ns the ..** curr
28ab4 65 6e 74 20 6b 65 79 2e 0d 0a 2a 2f 0d 0a 73 74  ent key...*/..st
28ab5 61 74 69 63 20 76 6f 69 64 20 2a 76 64 62 65 53  atic void *vdbeS
28ab6 6f 72 74 65 72 52 6f 77 6b 65 79 28 0d 0a 20 20  orterRowkey(..  
28ab7 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53 6f 72  VdbeSorter *pSor
28ab8 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ter,            
28ab9 2f 2a 20 53 6f 72 74 65 72 20 6f 62 6a 65 63 74  /* Sorter object
28aba 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 4b 65   */..  int *pnKe
28abb 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
28abc 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
28abd 69 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 6b  ize of current k
28abe 65 79 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  ey in bytes */..
28abf 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79  ){..  void *pKey
28ac0 3b 0d 0a 20 20 69 66 28 20 70 53 6f 72 74 65 72  ;..  if( pSorter
28ac1 2d 3e 61 54 72 65 65 20 29 7b 0d 0a 20 20 20 20  ->aTree ){..    
28ac2 56 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 2a  VdbeSorterIter *
28ac3 70 49 74 65 72 3b 0d 0a 20 20 20 20 70 49 74 65  pIter;..    pIte
28ac4 72 20 3d 20 26 70 53 6f 72 74 65 72 2d 3e 61 49  r = &pSorter->aI
28ac5 74 65 72 5b 20 70 53 6f 72 74 65 72 2d 3e 61 54  ter[ pSorter->aT
28ac6 72 65 65 5b 31 5d 20 5d 3b 0d 0a 20 20 20 20 2a  ree[1] ];..    *
28ac7 70 6e 4b 65 79 20 3d 20 70 49 74 65 72 2d 3e 6e  pnKey = pIter->n
28ac8 4b 65 79 3b 0d 0a 20 20 20 20 70 4b 65 79 20 3d  Key;..    pKey =
28ac9 20 70 49 74 65 72 2d 3e 61 4b 65 79 3b 0d 0a 20   pIter->aKey;.. 
28aca 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2a 70 6e   }else{..    *pn
28acb 4b 65 79 20 3d 20 70 53 6f 72 74 65 72 2d 3e 70  Key = pSorter->p
28acc 52 65 63 6f 72 64 2d 3e 6e 56 61 6c 3b 0d 0a 20  Record->nVal;.. 
28acd 20 20 20 70 4b 65 79 20 3d 20 70 53 6f 72 74 65     pKey = pSorte
28ace 72 2d 3e 70 52 65 63 6f 72 64 2d 3e 70 56 61 6c  r->pRecord->pVal
28acf 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
28ad0 20 70 4b 65 79 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pKey;..}..../*.
28ad1 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 75 72  .** Copy the cur
28ad2 72 65 6e 74 20 73 6f 72 74 65 72 20 6b 65 79 20  rent sorter key 
28ad3 69 6e 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  into the memory 
28ad4 63 65 6c 6c 20 70 4f 75 74 2e 0d 0a 2a 2f 0d 0a  cell pOut...*/..
28ad5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
28ad6 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  nt sqlite3VdbeSo
28ad7 72 74 65 72 52 6f 77 6b 65 79 28 56 64 62 65 43  rterRowkey(VdbeC
28ad8 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 4d 65 6d  ursor *pCsr, Mem
28ad9 20 2a 70 4f 75 74 29 7b 0d 0a 20 20 56 64 62 65   *pOut){..  Vdbe
28ada 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20  Sorter *pSorter 
28adb 3d 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b  = pCsr->pSorter;
28adc 0d 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20  ..  void *pKey; 
28add 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 20  int nKey;       
28ade 20 20 20 20 2f 2a 20 53 6f 72 74 65 72 20 6b 65      /* Sorter ke
28adf 79 20 74 6f 20 63 6f 70 79 20 69 6e 74 6f 20 70  y to copy into p
28ae0 4f 75 74 20 2a 2f 0d 0a 0d 0a 20 20 70 4b 65 79  Out */....  pKey
28ae1 20 3d 20 76 64 62 65 53 6f 72 74 65 72 52 6f 77   = vdbeSorterRow
28ae2 6b 65 79 28 70 53 6f 72 74 65 72 2c 20 26 6e 4b  key(pSorter, &nK
28ae3 65 79 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  ey);..  if( sqli
28ae4 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
28ae5 4f 75 74 2c 20 6e 4b 65 79 2c 20 30 29 20 29 7b  Out, nKey, 0) ){
28ae6 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
28ae7 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d  ITE_NOMEM;..  }.
28ae8 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 6e 4b 65  .  pOut->n = nKe
28ae9 79 3b 0d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  y;..  MemSetType
28aea 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42  Flag(pOut, MEM_B
28aeb 6c 6f 62 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28  lob);..  memcpy(
28aec 70 4f 75 74 2d 3e 7a 2c 20 70 4b 65 79 2c 20 6e  pOut->z, pKey, n
28aed 4b 65 79 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72  Key);....  retur
28aee 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
28aef 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 61 72  .../*..** Compar
28af0 65 20 74 68 65 20 6b 65 79 20 69 6e 20 6d 65 6d  e the key in mem
28af1 6f 72 79 20 63 65 6c 6c 20 70 56 61 6c 20 77 69  ory cell pVal wi
28af2 74 68 20 74 68 65 20 6b 65 79 20 74 68 61 74 20  th the key that 
28af3 74 68 65 20 73 6f 72 74 65 72 20 63 75 72 73 6f  the sorter curso
28af4 72 0d 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20  r..** passed as 
28af5 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
28af6 6e 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  nt currently poi
28af7 6e 74 73 20 74 6f 2e 20 46 6f 72 20 74 68 65 20  nts to. For the 
28af8 70 75 72 70 6f 73 65 73 20 6f 66 0d 0a 2a 2a 20  purposes of..** 
28af9 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20  the comparison, 
28afa 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 69 64  ignore the rowid
28afb 20 66 69 65 6c 64 20 61 74 20 74 68 65 20 65 6e   field at the en
28afc 64 20 6f 66 20 65 61 63 68 20 72 65 63 6f 72 64  d of each record
28afd 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20  ...**..** If an 
28afe 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
28aff 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20 65  turn an SQLite e
28b00 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20  rror code (i.e. 
28b01 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 2e 0d 0a  SQLITE_NOMEM)...
28b02 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65  ** Otherwise, se
28b03 74 20 2a 70 52 65 73 20 74 6f 20 61 20 6e 65 67  t *pRes to a neg
28b04 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70  ative, zero or p
28b05 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69 66  ositive value if
28b06 20 74 68 65 0d 0a 2a 2a 20 6b 65 79 20 69 6e 20   the..** key in 
28b07 70 56 61 6c 20 69 73 20 73 6d 61 6c 6c 65 72 20  pVal is smaller 
28b08 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f  than, equal to o
28b09 72 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  r larger than th
28b0a 65 20 63 75 72 72 65 6e 74 20 73 6f 72 74 65 72  e current sorter
28b0b 0d 0a 2a 2a 20 6b 65 79 2e 0d 0a 2a 2f 0d 0a 53  ..** key...*/..S
28b0c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
28b0d 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72  t sqlite3VdbeSor
28b0e 74 65 72 43 6f 6d 70 61 72 65 28 0d 0a 20 20 56  terCompare(..  V
28b0f 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  dbeCursor *pCsr,
28b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28b11 2a 20 53 6f 72 74 65 72 20 63 75 72 73 6f 72 20  * Sorter cursor 
28b12 2a 2f 0d 0a 20 20 4d 65 6d 20 2a 70 56 61 6c 2c  */..  Mem *pVal,
28b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b14 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
28b15 6f 20 63 6f 6d 70 61 72 65 20 74 6f 20 63 75 72  o compare to cur
28b16 72 65 6e 74 20 73 6f 72 74 65 72 20 6b 65 79 20  rent sorter key 
28b17 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 52 65 73 20  */..  int *pRes 
28b18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b19 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
28b1a 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69 73  sult of comparis
28b1b 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62  on */..){..  Vdb
28b1c 65 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72  eSorter *pSorter
28b1d 20 3d 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72   = pCsr->pSorter
28b1e 3b 0d 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b  ;..  void *pKey;
28b1f 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20   int nKey;      
28b20 20 20 20 20 20 2f 2a 20 53 6f 72 74 65 72 20 6b       /* Sorter k
28b21 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 70 56  ey to compare pV
28b22 61 6c 20 77 69 74 68 20 2a 2f 0d 0a 0d 0a 20 20  al with */....  
28b23 70 4b 65 79 20 3d 20 76 64 62 65 53 6f 72 74 65  pKey = vdbeSorte
28b24 72 52 6f 77 6b 65 79 28 70 53 6f 72 74 65 72 2c  rRowkey(pSorter,
28b25 20 26 6e 4b 65 79 29 3b 0d 0a 20 20 76 64 62 65   &nKey);..  vdbe
28b26 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 28 70 43  SorterCompare(pC
28b27 73 72 2c 20 31 2c 20 70 56 61 6c 2d 3e 7a 2c 20  sr, 1, pVal->z, 
28b28 70 56 61 6c 2d 3e 6e 2c 20 70 4b 65 79 2c 20 6e  pVal->n, pKey, n
28b29 4b 65 79 2c 20 70 52 65 73 29 3b 0d 0a 20 20 72  Key, pRes);..  r
28b2a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
28b2b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a  ..}....#endif /*
28b2c 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
28b2d 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 20  OMIT_MERGE_SORT 
28b2e 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
28b2f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
28b30 65 73 6f 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  esort.c ********
28b31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b33 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
28b34 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
28b35 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a  e journal.c ****
28b36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b38 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
28b39 30 30 37 20 41 75 67 75 73 74 20 32 32 0d 0a 2a  007 August 22..*
28b3a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
28b3b 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
28b3c 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
28b3d 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
28b3e 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
28b3f 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
28b40 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
28b41 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
28b42 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
28b43 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
28b44 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
28b45 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
28b46 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
28b47 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
28b48 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
28b49 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
28b4a 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
28b4b 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
28b4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b50 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  *****..**..** Th
28b51 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
28b52 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e  ts a special kin
28b53 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c  d of sqlite3_fil
28b54 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0d 0a 2a  e object used..*
28b55 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63  * by SQLite to c
28b56 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69  reate journal fi
28b57 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69  les if the atomi
28b58 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
28b59 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20 65 6e 61 62  tion..** is enab
28b5a 6c 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  led...**..** The
28b5b 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61   distinctive cha
28b5c 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74  racteristic of t
28b5d 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  his sqlite3_file
28b5e 20 69 73 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a   is that the..**
28b5f 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20   actual on disk 
28b60 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20  file is created 
28b61 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65  lazily. When the
28b62 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
28b63 2c 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  ,..** the caller
28b64 20 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66   specifies a buf
28b65 66 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20  fer size for an 
28b66 69 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  in-memory buffer
28b67 20 74 6f 0d 0a 2a 2a 20 62 65 20 75 73 65 64 20   to..** be used 
28b68 74 6f 20 73 65 72 76 69 63 65 20 72 65 61 64 28  to service read(
28b69 29 20 61 6e 64 20 77 72 69 74 65 28 29 20 72 65  ) and write() re
28b6a 71 75 65 73 74 73 2e 20 54 68 65 20 61 63 74 75  quests. The actu
28b6b 61 6c 20 66 69 6c 65 0d 0a 2a 2a 20 6f 6e 20 64  al file..** on d
28b6c 69 73 6b 20 69 73 20 6e 6f 74 20 63 72 65 61 74  isk is not creat
28b6d 65 64 20 6f 72 20 70 6f 70 75 6c 61 74 65 64 20  ed or populated 
28b6e 75 6e 74 69 6c 20 65 69 74 68 65 72 3a 0d 0a 2a  until either:..*
28b6f 2a 0d 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 69  *..**   1) The i
28b70 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
28b71 6e 74 61 74 69 6f 6e 20 67 72 6f 77 73 20 74 6f  ntation grows to
28b72 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20  o large for the 
28b73 61 6c 6c 6f 63 61 74 65 64 20 0d 0a 2a 2a 20 20  allocated ..**  
28b74 20 20 20 20 62 75 66 66 65 72 2c 20 6f 72 0d 0a      buffer, or..
28b75 2a 2a 20 20 20 32 29 20 54 68 65 20 73 71 6c 69  **   2) The sqli
28b76 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
28b77 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
28b78 61 6c 6c 65 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 64  alled...*/..#ifd
28b79 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
28b7a 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0d 0a 0d  _ATOMIC_WRITE...
28b7b 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 4a 6f 75 72  .../*..** A Jour
28b7c 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69  nalFile object i
28b7d 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  s a subclass of 
28b7e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65  sqlite3_file use
28b7f 64 20 62 79 0d 0a 2a 2a 20 61 73 20 61 6e 20 6f  d by..** as an o
28b80 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  pen file handle 
28b81 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  for journal file
28b82 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 4a  s...*/..struct J
28b83 6f 75 72 6e 61 6c 46 69 6c 65 20 7b 0d 0a 20 20  ournalFile {..  
28b84 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
28b85 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20  ds *pMethod;    
28b86 2f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f  /* I/O methods o
28b87 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20  n journal files 
28b88 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20  */..  int nBuf; 
28b89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b8a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
28b8b 20 7a 42 75 66 5b 5d 20 69 6e 20 62 79 74 65 73   zBuf[] in bytes
28b8c 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 42 75   */..  char *zBu
28b8d 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
28b8e 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
28b8f 74 6f 20 62 75 66 66 65 72 20 6a 6f 75 72 6e 61  to buffer journa
28b90 6c 20 77 72 69 74 65 73 20 2a 2f 0d 0a 20 20 69  l writes */..  i
28b91 6e 74 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20  nt iSize;       
28b92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28b93 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42 75 66  * Amount of zBuf
28b94 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65  [] currently use
28b95 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c 61 67  d */..  int flag
28b96 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
28b97 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
28b98 20 66 6c 61 67 73 20 2a 2f 0d 0a 20 20 73 71 6c   flags */..  sql
28b99 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20  ite3_vfs *pVfs; 
28b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28b9b 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72  The "real" under
28b9c 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0d 0a 20 20  lying VFS */..  
28b9d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52  sqlite3_file *pR
28b9e 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  eal;            
28b9f 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e  /* The "real" un
28ba0 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65  derlying file de
28ba1 73 63 72 69 70 74 6f 72 20 2a 2f 0d 0a 20 20 63  scriptor */..  c
28ba2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 72  onst char *zJour
28ba3 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f  nal;           /
28ba4 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
28ba5 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0d 0a 7d  urnal file */..}
28ba6 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
28ba7 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f  t JournalFile Jo
28ba8 75 72 6e 61 6c 46 69 6c 65 3b 0d 0a 0d 0a 2f 2a  urnalFile;..../*
28ba9 0d 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20  ..** If it does 
28baa 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
28bab 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70  ts, create and p
28bac 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64  opulate the on-d
28bad 69 73 6b 20 66 69 6c 65 20 0d 0a 2a 2a 20 66 6f  isk file ..** fo
28bae 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e  r JournalFile p.
28baf 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
28bb0 20 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72   createFile(Jour
28bb1 6e 61 6c 46 69 6c 65 20 2a 70 29 7b 0d 0a 20 20  nalFile *p){..  
28bb2 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
28bb3 4f 4b 3b 0d 0a 20 20 69 66 28 20 21 70 2d 3e 70  OK;..  if( !p->p
28bb4 52 65 61 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Real ){..    sql
28bb5 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c  ite3_file *pReal
28bb6 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
28bb7 20 2a 29 26 70 5b 31 5d 3b 0d 0a 20 20 20 20 72   *)&p[1];..    r
28bb8 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
28bb9 6e 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a  n(p->pVfs, p->zJ
28bba 6f 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c 20 70  ournal, pReal, p
28bbb 2d 3e 66 6c 61 67 73 2c 20 30 29 3b 0d 0a 20 20  ->flags, 0);..  
28bbc 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
28bbd 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d  _OK ){..      p-
28bbe 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0d  >pReal = pReal;.
28bbf 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53  .      if( p->iS
28bc0 69 7a 65 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  ize>0 ){..      
28bc1 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69 7a    assert(p->iSiz
28bc2 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0d 0a 20 20  e<=p->nBuf);..  
28bc3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
28bc4 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65  e3OsWrite(p->pRe
28bc5 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e  al, p->zBuf, p->
28bc6 69 53 69 7a 65 2c 20 30 29 3b 0d 0a 20 20 20 20  iSize, 0);..    
28bc7 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
28bc8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
28bc9 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65  ..../*..** Close
28bca 20 74 68 65 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a   the file...*/..
28bcb 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 43  static int jrnlC
28bcc 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
28bcd 65 20 2a 70 4a 66 64 29 7b 0d 0a 20 20 4a 6f 75  e *pJfd){..  Jou
28bce 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
28bcf 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
28bd0 64 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  d;..  if( p->pRe
28bd1 61 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  al ){..    sqlit
28bd2 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65  e3OsClose(p->pRe
28bd3 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  al);..  }..  sql
28bd4 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 75  ite3_free(p->zBu
28bd5 66 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  f);..  return SQ
28bd6 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
28bd7 2a 0d 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  *..** Read data 
28bd8 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0d 0a  from the file...
28bd9 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  */..static int j
28bda 72 6e 6c 52 65 61 64 28 0d 0a 20 20 73 71 6c 69  rnlRead(..  sqli
28bdb 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
28bdc 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
28bdd 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63  l file from whic
28bde 68 20 74 6f 20 72 65 61 64 20 2a 2f 0d 0a 20 20  h to read */..  
28bdf 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20  void *zBuf,     
28be0 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68         /* Put th
28be1 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
28be2 2f 0d 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  /..  int iAmt,  
28be3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
28be4 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
28be5 6f 20 72 65 61 64 20 2a 2f 0d 0a 20 20 73 71 6c  o read */..  sql
28be6 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
28be7 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
28be8 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ding at this off
28be9 73 65 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  set */..){..  in
28bea 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
28beb 3b 0d 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ;..  JournalFile
28bec 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
28bed 6c 65 20 2a 29 70 4a 66 64 3b 0d 0a 20 20 69 66  le *)pJfd;..  if
28bee 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0d 0a 20  ( p->pReal ){.. 
28bef 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
28bf0 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c 20  sRead(p->pReal, 
28bf1 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73  zBuf, iAmt, iOfs
28bf2 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  t);..  }else if(
28bf3 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d   (iAmt+iOfst)>p-
28bf4 3e 69 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20 72  >iSize ){..    r
28bf5 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
28bf6 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0d 0a 20 20  _SHORT_READ;..  
28bf7 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6d 65 6d 63  }else{..    memc
28bf8 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75  py(zBuf, &p->zBu
28bf9 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b  f[iOfst], iAmt);
28bfa 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
28bfb 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
28bfc 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 74   Write data to t
28bfd 68 65 20 66 69 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74  he file...*/..st
28bfe 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69  atic int jrnlWri
28bff 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  te(..  sqlite3_f
28c00 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a  ile *pJfd,    /*
28c01 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
28c02 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20  e into which to 
28c03 77 72 69 74 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73  write */..  cons
28c04 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20  t void *zBuf,   
28c05 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 61 20     /* Take data 
28c06 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 66 72  to be written fr
28c07 6f 6d 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e  om here */..  in
28c08 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20  t iAmt,         
28c09 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
28c0a 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65  f bytes to write
28c0b 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 5f 69 6e   */..  sqlite_in
28c0c 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
28c0d 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61   Begin writing a
28c0e 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e  t this offset in
28c0f 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0d 0a  to the file */..
28c10 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
28c11 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 4a 6f 75  QLITE_OK;..  Jou
28c12 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
28c13 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
28c14 64 3b 0d 0a 20 20 69 66 28 20 21 70 2d 3e 70 52  d;..  if( !p->pR
28c15 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41  eal && (iOfst+iA
28c16 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0d 0a  mt)>p->nBuf ){..
28c17 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46      rc = createF
28c18 69 6c 65 28 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20  ile(p);..  }..  
28c19 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
28c1a 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d  K ){..    if( p-
28c1b 3e 70 52 65 61 6c 20 29 7b 0d 0a 20 20 20 20 20  >pReal ){..     
28c1c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
28c1d 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a  rite(p->pReal, z
28c1e 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74  Buf, iAmt, iOfst
28c1f 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
28c20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d        memcpy(&p-
28c21 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42  >zBuf[iOfst], zB
28c22 75 66 2c 20 69 41 6d 74 29 3b 0d 0a 20 20 20 20  uf, iAmt);..    
28c23 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28    if( p->iSize<(
28c24 69 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0d 0a  iOfst+iAmt) ){..
28c25 20 20 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65          p->iSize
28c26 20 3d 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b   = (iOfst+iAmt);
28c27 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
28c28 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
28c29 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
28c2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69   Truncate the fi
28c2b 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  le...*/..static 
28c2c 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65  int jrnlTruncate
28c2d 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
28c2e 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
28c2f 34 20 73 69 7a 65 29 7b 0d 0a 20 20 69 6e 74 20  4 size){..  int 
28c30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
28c31 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
28c32 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
28c33 20 2a 29 70 4a 66 64 3b 0d 0a 20 20 69 66 28 20   *)pJfd;..  if( 
28c34 70 2d 3e 70 52 65 61 6c 20 29 7b 0d 0a 20 20 20  p->pReal ){..   
28c35 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
28c36 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c  runcate(p->pReal
28c37 2c 20 73 69 7a 65 29 3b 0d 0a 20 20 7d 65 6c 73  , size);..  }els
28c38 65 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53  e if( size<p->iS
28c39 69 7a 65 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 69  ize ){..    p->i
28c3a 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0d 0a 20 20  Size = size;..  
28c3b 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
28c3c 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 79 6e  .}..../*..** Syn
28c3d 63 20 74 68 65 20 66 69 6c 65 2e 0d 0a 2a 2f 0d  c the file...*/.
28c3e 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
28c3f 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
28c40 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61  e *pJfd, int fla
28c41 67 73 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  gs){..  int rc;.
28c42 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
28c43 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
28c44 20 2a 29 70 4a 66 64 3b 0d 0a 20 20 69 66 28 20   *)pJfd;..  if( 
28c45 70 2d 3e 70 52 65 61 6c 20 29 7b 0d 0a 20 20 20  p->pReal ){..   
28c46 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
28c47 79 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c  ync(p->pReal, fl
28c48 61 67 73 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  ags);..  }else{.
28c49 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
28c4a 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  _OK;..  }..  ret
28c4b 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
28c4c 0d 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73  ..** Query the s
28c4d 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
28c4e 69 6e 20 62 79 74 65 73 2e 0d 0a 2a 2f 0d 0a 73  in bytes...*/..s
28c4f 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69  tatic int jrnlFi
28c50 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
28c51 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
28c52 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
28c53 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
28c54 49 54 45 5f 4f 4b 3b 0d 0a 20 20 4a 6f 75 72 6e  ITE_OK;..  Journ
28c55 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
28c56 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
28c57 0d 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ..  if( p->pReal
28c58 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   ){..    rc = sq
28c59 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
28c5a 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29  p->pReal, pSize)
28c5b 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
28c5c 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74   *pSize = (sqlit
28c5d 65 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a  e_int64) p->iSiz
28c5e 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  e;..  }..  retur
28c5f 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
28c60 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68  ** Table of meth
28c61 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 6c 46  ods for JournalF
28c62 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ile sqlite3_file
28c63 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74   object...*/..st
28c64 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69  atic struct sqli
28c65 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a  te3_io_methods J
28c66 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64  ournalFileMethod
28c67 73 20 3d 20 7b 0d 0a 20 20 31 2c 20 20 20 20 20  s = {..  1,     
28c68 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
28c69 69 6f 6e 20 2a 2f 0d 0a 20 20 6a 72 6e 6c 43 6c  ion */..  jrnlCl
28c6a 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f  ose,     /* xClo
28c6b 73 65 20 2a 2f 0d 0a 20 20 6a 72 6e 6c 52 65 61  se */..  jrnlRea
28c6c 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64  d,      /* xRead
28c6d 20 2a 2f 0d 0a 20 20 6a 72 6e 6c 57 72 69 74 65   */..  jrnlWrite
28c6e 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20  ,     /* xWrite 
28c6f 2a 2f 0d 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61  */..  jrnlTrunca
28c70 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74  te,  /* xTruncat
28c71 65 20 2a 2f 0d 0a 20 20 6a 72 6e 6c 53 79 6e 63  e */..  jrnlSync
28c72 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20  ,      /* xSync 
28c73 2a 2f 0d 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69  */..  jrnlFileSi
28c74 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a  ze,  /* xFileSiz
28c75 65 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20  e */..  0,      
28c76 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
28c77 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20 20  */..  0,        
28c78 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20       /* xUnlock 
28c79 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20 20  */..  0,        
28c7a 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
28c7b 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0d 0a 20  servedLock */.. 
28c7c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
28c7d 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  /* xFileControl 
28c7e 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20 20  */..  0,        
28c7f 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53       /* xSectorS
28c80 69 7a 65 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20  ize */..  0,    
28c81 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76           /* xDev
28c82 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
28c83 63 73 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20  cs */..  0,     
28c84 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d          /* xShmM
28c85 61 70 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20  ap */..  0,     
28c86 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 4c          /* xShmL
28c87 6f 63 6b 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20  ock */..  0,    
28c88 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d           /* xShm
28c89 42 61 72 72 69 65 72 20 2a 2f 0d 0a 20 20 30 20  Barrier */..  0 
28c8a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28c8b 78 53 68 6d 55 6e 6d 61 70 20 2a 2f 0d 0a 7d 3b  xShmUnmap */..};
28c8c 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 4f 70 65 6e  ..../* ..** Open
28c8d 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
28c8e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
28c8f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
28c90 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0d 0a 20 20  JournalOpen(..  
28c91 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
28c92 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  s,         /* Th
28c93 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72  e VFS to use for
28c94 20 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f 4f   actual file I/O
28c95 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
28c96 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
28c97 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
28c98 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
28c99 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ..  sqlite3_file
28c9a 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 20 20 2f   *pJfd,        /
28c9b 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20  * Preallocated, 
28c9c 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c  blank file handl
28c9d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c 61 67  e */..  int flag
28c9e 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
28c9f 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 66 6c     /* Opening fl
28ca0 61 67 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42  ags */..  int nB
28ca1 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  uf              
28ca2 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 75       /* Bytes bu
28ca3 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f 70  ffered before op
28ca4 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a  ening the file *
28ca5 2f 0d 0a 29 7b 0d 0a 20 20 4a 6f 75 72 6e 61 6c  /..){..  Journal
28ca6 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e  File *p = (Journ
28ca7 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0d 0a  alFile *)pJfd;..
28ca8 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73    memset(p, 0, s
28ca9 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
28caa 65 28 70 56 66 73 29 29 3b 0d 0a 20 20 69 66 28  e(pVfs));..  if(
28cab 20 6e 42 75 66 3e 30 20 29 7b 0d 0a 20 20 20 20   nBuf>0 ){..    
28cac 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65  p->zBuf = sqlite
28cad 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66  3MallocZero(nBuf
28cae 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  );..    if( !p->
28caf 7a 42 75 66 20 29 7b 0d 0a 20 20 20 20 20 20 72  zBuf ){..      r
28cb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
28cb1 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65  EM;..    }..  }e
28cb2 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  lse{..    return
28cb3 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
28cb4 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64  Vfs, zName, pJfd
28cb5 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0d 0a 20 20  , flags, 0);..  
28cb6 7d 0d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20  }..  p->pMethod 
28cb7 3d 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65  = &JournalFileMe
28cb8 74 68 6f 64 73 3b 0d 0a 20 20 70 2d 3e 6e 42 75  thods;..  p->nBu
28cb9 66 20 3d 20 6e 42 75 66 3b 0d 0a 20 20 70 2d 3e  f = nBuf;..  p->
28cba 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0d 0a  flags = flags;..
28cbb 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20    p->zJournal = 
28cbc 7a 4e 61 6d 65 3b 0d 0a 20 20 70 2d 3e 70 56 66  zName;..  p->pVf
28cbd 73 20 3d 20 70 56 66 73 3b 0d 0a 20 20 72 65 74  s = pVfs;..  ret
28cbe 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
28cbf 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74  }..../*..** If t
28cc0 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f  he argument p po
28cc1 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61  ints to a Journa
28cc2 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c  lFile structure,
28cc3 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79   and the underly
28cc4 69 6e 67 0d 0a 2a 2a 20 66 69 6c 65 20 68 61 73  ing..** file has
28cc5 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72   not yet been cr
28cc6 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74  eated, create it
28cc7 20 6e 6f 77 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54   now...*/..SQLIT
28cc8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
28cc9 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
28cca 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
28ccb 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e 70  *p){..  if( p->p
28ccc 4d 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61  Methods!=&Journa
28ccd 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0d  lFileMethods ){.
28cce 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
28ccf 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 72  TE_OK;..  }..  r
28cd0 65 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c 65  eturn createFile
28cd1 28 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29  ((JournalFile *)
28cd2 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  p);..}..../* ..*
28cd3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
28cd4 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71  ber of bytes req
28cd5 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61  uired to store a
28cd6 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 61   JournalFile tha
28cd7 74 20 75 73 65 73 20 76 66 73 0d 0a 2a 2a 20 70  t uses vfs..** p
28cd8 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68  Vfs to create th
28cd9 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d  e underlying on-
28cda 64 69 73 6b 20 66 69 6c 65 73 2e 0d 0a 2a 2f 0d  disk files...*/.
28cdb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28cdc 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
28cdd 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76  alSize(sqlite3_v
28cde 66 73 20 2a 70 56 66 73 29 7b 0d 0a 20 20 72 65  fs *pVfs){..  re
28cdf 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73  turn (pVfs->szOs
28ce0 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72  File+sizeof(Jour
28ce1 6e 61 6c 46 69 6c 65 29 29 3b 0d 0a 7d 0d 0a 23  nalFile));..}..#
28ce2 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  endif..../******
28ce3 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
28ce4 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
28ce5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ce7 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
28ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
28ce9 66 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e  file memjournal.
28cea 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
28ceb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28cec 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
28ced 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 37  * 2008 October 7
28cee 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74  ..**..** The aut
28cef 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
28cf0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
28cf1 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
28cf2 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20   place of..** a 
28cf3 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
28cf4 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
28cf5 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79  :..**..**    May
28cf6 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
28cf7 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20   not evil...**  
28cf8 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
28cf9 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
28cfa 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
28cfb 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20  ive others...** 
28cfc 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
28cfd 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
28cfe 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
28cff 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a  you give...**..*
28d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d04 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
28d05 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
28d06 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20  ins code use to 
28d07 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d  implement an in-
28d08 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20  memory rollback 
28d09 6a 6f 75 72 6e 61 6c 2e 0d 0a 2a 2a 20 54 68 65  journal...** The
28d0a 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62   in-memory rollb
28d0b 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75  ack journal is u
28d0c 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74  sed to journal t
28d0d 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0d  ransactions for.
28d0e 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64  .** ":memory:" d
28d0f 61 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65  atabases and whe
28d10 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  n the journal_mo
28d11 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61  de=MEMORY pragma
28d12 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2f 0d 0a 0d   is used...*/...
28d13 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
28d14 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e  rences to intern
28d15 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  al structures */
28d16 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
28d17 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a   MemJournal MemJ
28d18 6f 75 72 6e 61 6c 3b 0d 0a 74 79 70 65 64 65 66  ournal;..typedef
28d19 20 73 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e   struct FilePoin
28d1a 74 20 46 69 6c 65 50 6f 69 6e 74 3b 0d 0a 74 79  t FilePoint;..ty
28d1b 70 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c  pedef struct Fil
28d1c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b  eChunk FileChunk
28d1d 3b 0d 0a 0d 0a 2f 2a 20 53 70 61 63 65 20 74 6f  ;..../* Space to
28d1e 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 61   hold the rollba
28d1f 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c  ck journal is al
28d20 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 65  located in incre
28d21 6d 65 6e 74 73 20 6f 66 0d 0a 2a 2a 20 74 68 69  ments of..** thi
28d22 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0d 0a 2a  s many bytes...*
28d23 2a 0d 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 63  *..** The size c
28d24 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74 74 6c  hosen is a littl
28d25 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20 70 6f  e less than a po
28d26 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61  wer of two.  Tha
28d27 74 20 77 61 79 2c 0d 0a 2a 2a 20 74 68 65 20 46  t way,..** the F
28d28 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20  ileChunk object 
28d29 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a 65  will have a size
28d2a 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78 61   that almost exa
28d2b 63 74 6c 79 20 66 69 6c 6c 73 0d 0a 2a 2a 20 61  ctly fills..** a
28d2c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
28d2d 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20  location.  This 
28d2e 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64  mimimizes wasted
28d2f 20 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d   space in power-
28d30 6f 66 2d 74 77 6f 0d 0a 2a 2a 20 6d 65 6d 6f 72  of-two..** memor
28d31 79 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0d 0a 2a  y allocators...*
28d32 2f 0d 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  /..#define JOURN
28d33 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 28 28 69  AL_CHUNKSIZE ((i
28d34 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 6f 66 28  nt)(1024-sizeof(
28d35 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 0d 0a 0d  FileChunk*)))...
28d36 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e  ./* Macro to fin
28d37 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66  d the minimum of
28d38 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c   two numeric val
28d39 75 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  ues...*/..#ifnde
28d3a 66 20 4d 49 4e 0d 0a 23 20 64 65 66 69 6e 65 20  f MIN..# define 
28d3b 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79  MIN(x,y) ((x)<(y
28d3c 29 3f 28 78 29 3a 28 79 29 29 0d 0a 23 65 6e 64  )?(x):(y))..#end
28d3d 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  if..../*..** The
28d3e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
28d3f 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66  l is composed of
28d40 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
28d41 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
28d42 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  es...*/..struct 
28d43 46 69 6c 65 43 68 75 6e 6b 20 7b 0d 0a 20 20 46  FileChunk {..  F
28d44 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b  ileChunk *pNext;
28d45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28d46 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e 20  * Next chunk in 
28d47 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0d 0a  the journal */..
28d48 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52    u8 zChunk[JOUR
28d49 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20  NAL_CHUNKSIZE]; 
28d4a 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
28d4b 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0d 0a 7d  this chunk */..}
28d4c 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69  ;..../*..** An i
28d4d 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
28d4e 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 73  object serves as
28d4f 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74   a cursor into t
28d50 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
28d51 6e 61 6c 2e 0d 0a 2a 2a 20 54 68 65 20 63 75 72  nal...** The cur
28d52 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 65  sor can be eithe
28d53 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72  r for reading or
28d54 20 77 72 69 74 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73   writing...*/..s
28d55 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20  truct FilePoint 
28d56 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  {..  sqlite3_int
28d57 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20  64 iOffset;     
28d58 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66       /* Offset f
28d59 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
28d5a 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f  g of the file */
28d5b 0d 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ..  FileChunk *p
28d5c 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 20 20  Chunk;          
28d5d 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69 63 20      /* Specific 
28d5e 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68  chunk into which
28d5f 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a   cursor points *
28d60 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  /..};..../*..** 
28d61 54 68 69 73 20 73 75 62 63 6c 61 73 73 20 69 73  This subclass is
28d62 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73   a subclass of s
28d63 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61  qlite3_file.  Ea
28d64 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a  ch open memory-j
28d65 6f 75 72 6e 61 6c 0d 0a 2a 2a 20 69 73 20 61 6e  ournal..** is an
28d66 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
28d67 73 20 63 6c 61 73 73 2e 0d 0a 2a 2f 0d 0a 73 74  s class...*/..st
28d68 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ruct MemJournal 
28d69 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  {..  sqlite3_io_
28d6a 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64  methods *pMethod
28d6b 3b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63  ;    /* Parent c
28d6c 6c 61 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49  lass. MUST BE FI
28d6d 52 53 54 20 2a 2f 0d 0a 20 20 46 69 6c 65 43 68  RST */..  FileCh
28d6e 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 20  unk *pFirst;    
28d6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61            /* Hea
28d70 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63  d of in-memory c
28d71 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0d 0a 20 20  hunk-list */..  
28d72 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69  FilePoint endpoi
28d73 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
28d74 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
28d75 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
28d76 65 20 2a 2f 0d 0a 20 20 46 69 6c 65 50 6f 69 6e  e */..  FilePoin
28d77 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 20  t readpoint;    
28d78 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
28d79 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  er to the end of
28d7a 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 28   the last xRead(
28d7b 29 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  ) */..};..../*..
28d7c 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f  ** Read data fro
28d7d 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  m the in-memory 
28d7e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54  journal file.  T
28d7f 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65  his is the imple
28d80 6d 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a 20 6f 66  mentation..** of
28d81 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73   the sqlite3_vfs
28d82 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 2e 0d 0a  .xRead method...
28d83 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  */..static int m
28d84 65 6d 6a 72 6e 6c 52 65 61 64 28 0d 0a 20 20 73  emjrnlRead(..  s
28d85 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
28d86 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
28d87 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77  rnal file from w
28d88 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0d  hich to read */.
28d89 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20  .  void *zBuf,  
28d8a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74            /* Put
28d8b 20 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72   the results her
28d8c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 41 6d 74  e */..  int iAmt
28d8d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
28d8e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
28d8f 73 20 74 6f 20 72 65 61 64 20 2a 2f 0d 0a 20 20  s to read */..  
28d90 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66  sqlite_int64 iOf
28d91 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20  st     /* Begin 
28d92 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
28d93 6f 66 66 73 65 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  offset */..){.. 
28d94 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d   MemJournal *p =
28d95 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70   (MemJournal *)p
28d96 4a 66 64 3b 0d 0a 20 20 75 38 20 2a 7a 4f 75 74  Jfd;..  u8 *zOut
28d97 20 3d 20 7a 42 75 66 3b 0d 0a 20 20 69 6e 74 20   = zBuf;..  int 
28d98 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b 0d 0a 20  nRead = iAmt;.. 
28d99 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65   int iChunkOffse
28d9a 74 3b 0d 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20  t;..  FileChunk 
28d9b 2a 70 43 68 75 6e 6b 3b 0d 0a 0d 0a 20 20 2f 2a  *pChunk;....  /*
28d9c 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72   SQLite never tr
28d9d 69 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74  ies to read past
28d9e 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f   the end of a ro
28d9f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66  llback journal f
28da0 69 6c 65 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ile */..  assert
28da1 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d  ( iOfst+iAmt<=p-
28da2 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65  >endpoint.iOffse
28da3 74 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 2d  t );....  if( p-
28da4 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73  >readpoint.iOffs
28da5 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f 66  et!=iOfst || iOf
28da6 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  st==0 ){..    sq
28da7 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66  lite3_int64 iOff
28da8 20 3d 20 30 3b 0d 0a 20 20 20 20 66 6f 72 28 70   = 0;..    for(p
28da9 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b  Chunk=p->pFirst;
28daa 20 0d 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59   ..        ALWAY
28dab 53 28 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f  S(pChunk) && (iO
28dac 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  ff+JOURNAL_CHUNK
28dad 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0d 0a 20  SIZE)<=iOfst;.. 
28dae 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43         pChunk=pC
28daf 68 75 6e 6b 2d 3e 70 4e 65 78 74 0d 0a 20 20 20  hunk->pNext..   
28db0 20 29 7b 0d 0a 20 20 20 20 20 20 69 4f 66 66 20   ){..      iOff 
28db1 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  += JOURNAL_CHUNK
28db2 53 49 5a 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  SIZE;..    }..  
28db3 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 43 68 75  }else{..    pChu
28db4 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e  nk = p->readpoin
28db5 74 2e 70 43 68 75 6e 6b 3b 0d 0a 20 20 7d 0d 0a  t.pChunk;..  }..
28db6 0d 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  ..  iChunkOffset
28db7 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a   = (int)(iOfst%J
28db8 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
28db9 29 3b 0d 0a 20 20 64 6f 20 7b 0d 0a 20 20 20 20  );..  do {..    
28dba 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55  int iSpace = JOU
28dbb 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d  RNAL_CHUNKSIZE -
28dbc 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0d 0a   iChunkOffset;..
28dbd 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20      int nCopy = 
28dbe 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52  MIN(nRead, (JOUR
28dbf 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20  NAL_CHUNKSIZE - 
28dc0 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0d  iChunkOffset));.
28dc1 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74  .    memcpy(zOut
28dc2 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e  , &pChunk->zChun
28dc3 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c  k[iChunkOffset],
28dc4 20 6e 43 6f 70 79 29 3b 0d 0a 20 20 20 20 7a 4f   nCopy);..    zO
28dc5 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0d 0a 20 20  ut += nCopy;..  
28dc6 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63    nRead -= iSpac
28dc7 65 3b 0d 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66  e;..    iChunkOf
28dc8 66 73 65 74 20 3d 20 30 3b 0d 0a 20 20 7d 20 77  fset = 0;..  } w
28dc9 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26  hile( nRead>=0 &
28dca 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b  & (pChunk=pChunk
28dcb 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 20 6e  ->pNext)!=0 && n
28dcc 52 65 61 64 3e 30 20 29 3b 0d 0a 20 20 70 2d 3e  Read>0 );..  p->
28dcd 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65  readpoint.iOffse
28dce 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0d  t = iOfst+iAmt;.
28dcf 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  .  p->readpoint.
28dd0 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b  pChunk = pChunk;
28dd1 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ....  return SQL
28dd2 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
28dd3 0d 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  ..** Write data 
28dd4 74 6f 20 74 68 65 20 66 69 6c 65 2e 0d 0a 2a 2f  to the file...*/
28dd5 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  ..static int mem
28dd6 6a 72 6e 6c 57 72 69 74 65 28 0d 0a 20 20 73 71  jrnlWrite(..  sq
28dd7 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
28dd8 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72  ,    /* The jour
28dd9 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68  nal file into wh
28dda 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0d  ich to write */.
28ddb 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
28ddc 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b  Buf,      /* Tak
28ddd 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69  e data to be wri
28dde 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a  tten from here *
28ddf 2f 0d 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  /..  int iAmt,  
28de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
28de1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
28de2 6f 20 77 72 69 74 65 20 2a 2f 0d 0a 20 20 73 71  o write */..  sq
28de3 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
28de4 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72       /* Begin wr
28de5 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  iting at this of
28de6 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69  fset into the fi
28de7 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 4d 65 6d  le */..){..  Mem
28de8 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
28de9 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
28dea 0d 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d  ..  int nWrite =
28deb 20 69 41 6d 74 3b 0d 0a 20 20 75 38 20 2a 7a 57   iAmt;..  u8 *zW
28dec 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75  rite = (u8 *)zBu
28ded 66 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6e 20 69 6e  f;....  /* An in
28dee 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
28def 66 69 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  file should only
28df0 20 65 76 65 72 20 62 65 20 61 70 70 65 6e 64 65   ever be appende
28df1 64 20 74 6f 2e 20 52 61 6e 64 6f 6d 0d 0a 20 20  d to. Random..  
28df2 2a 2a 20 61 63 63 65 73 73 20 77 72 69 74 65 73  ** access writes
28df3 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65   are not require
28df4 64 20 62 79 20 73 71 6c 69 74 65 2e 0d 0a 20 20  d by sqlite...  
28df5 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 69 4f  */..  assert( iO
28df6 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74  fst==p->endpoint
28df7 2e 69 4f 66 66 73 65 74 20 29 3b 0d 0a 20 20 55  .iOffset );..  U
28df8 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
28df9 69 4f 66 73 74 29 3b 0d 0a 0d 0a 20 20 77 68 69  iOfst);....  whi
28dfa 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0d  le( nWrite>0 ){.
28dfb 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  .    FileChunk *
28dfc 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70  pChunk = p->endp
28dfd 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0d 0a 20 20  oint.pChunk;..  
28dfe 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73    int iChunkOffs
28dff 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e 65 6e  et = (int)(p->en
28e00 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 25 4a  dpoint.iOffset%J
28e01 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
28e02 29 3b 0d 0a 20 20 20 20 69 6e 74 20 69 53 70 61  );..    int iSpa
28e03 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 2c  ce = MIN(nWrite,
28e04 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
28e05 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
28e06 74 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69  t);....    if( i
28e07 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29  ChunkOffset==0 )
28e08 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20  {..      /* New 
28e09 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65  chunk is require
28e0a 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20  d to extend the 
28e0b 66 69 6c 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  file. */..      
28e0c 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20  FileChunk *pNew 
28e0d 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
28e0e 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e  (sizeof(FileChun
28e0f 6b 29 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  k));..      if( 
28e10 21 70 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20  !pNew ){..      
28e11 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28e12 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  IOERR_NOMEM;..  
28e13 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 4e 65      }..      pNe
28e14 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0d 0a 20  w->pNext = 0;.. 
28e15 20 20 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20       if( pChunk 
28e16 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
28e17 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29 3b  rt( p->pFirst );
28e18 0d 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b  ..        pChunk
28e19 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0d  ->pNext = pNew;.
28e1a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
28e1b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
28e1c 70 2d 3e 70 46 69 72 73 74 20 29 3b 0d 0a 20 20  p->pFirst );..  
28e1d 20 20 20 20 20 20 70 2d 3e 70 46 69 72 73 74 20        p->pFirst 
28e1e 3d 20 70 4e 65 77 3b 0d 0a 20 20 20 20 20 20 7d  = pNew;..      }
28e1f 0d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 6f  ..      p->endpo
28e20 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e 65  int.pChunk = pNe
28e21 77 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  w;..    }....   
28e22 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70   memcpy(&p->endp
28e23 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68  oint.pChunk->zCh
28e24 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74  unk[iChunkOffset
28e25 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63  ], zWrite, iSpac
28e26 65 29 3b 0d 0a 20 20 20 20 7a 57 72 69 74 65 20  e);..    zWrite 
28e27 2b 3d 20 69 53 70 61 63 65 3b 0d 0a 20 20 20 20  += iSpace;..    
28e28 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65  nWrite -= iSpace
28e29 3b 0d 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69  ;..    p->endpoi
28e2a 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53  nt.iOffset += iS
28e2b 70 61 63 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  pace;..  }....  
28e2c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
28e2d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
28e2e 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65  runcate the file
28e2f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
28e30 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74  t memjrnlTruncat
28e31 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
28e32 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74  pJfd, sqlite_int
28e33 36 34 20 73 69 7a 65 29 7b 0d 0a 20 20 4d 65 6d  64 size){..  Mem
28e34 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
28e35 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
28e36 0d 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ..  FileChunk *p
28e37 43 68 75 6e 6b 3b 0d 0a 20 20 61 73 73 65 72 74  Chunk;..  assert
28e38 28 73 69 7a 65 3d 3d 30 29 3b 0d 0a 20 20 55 4e  (size==0);..  UN
28e39 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73  USED_PARAMETER(s
28e3a 69 7a 65 29 3b 0d 0a 20 20 70 43 68 75 6e 6b 20  ize);..  pChunk 
28e3b 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0d 0a 20 20  = p->pFirst;..  
28e3c 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b  while( pChunk ){
28e3d 0d 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20  ..    FileChunk 
28e3e 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0d  *pTmp = pChunk;.
28e3f 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43  .    pChunk = pC
28e40 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20  hunk->pNext;..  
28e41 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
28e42 54 6d 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71  Tmp);..  }..  sq
28e43 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f  lite3MemJournalO
28e44 70 65 6e 28 70 4a 66 64 29 3b 0d 0a 20 20 72 65  pen(pJfd);..  re
28e45 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
28e46 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f  .}..../*..** Clo
28e47 73 65 20 74 68 65 20 66 69 6c 65 2e 0d 0a 2a 2f  se the file...*/
28e48 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  ..static int mem
28e49 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  jrnlClose(sqlite
28e4a 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0d 0a  3_file *pJfd){..
28e4b 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74    memjrnlTruncat
28e4c 65 28 70 4a 66 64 2c 20 30 29 3b 0d 0a 20 20 72  e(pJfd, 0);..  r
28e4d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
28e4e 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
28e4f 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0d 0a  Sync the file...
28e50 2a 2a 0d 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 61  **..** Syncing a
28e51 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
28e52 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  nal is a no-op. 
28e53 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 74   And, in fact, t
28e54 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20  his routine..** 
28e55 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
28e56 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70  in a working imp
28e57 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
28e58 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
28e59 6e 0d 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72  n..** exists pur
28e5a 65 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67  ely as a conting
28e5b 65 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f  ency, in case so
28e5c 6d 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69  me malfunction i
28e5d 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0d 0a 2a 2a  n some other..**
28e5e 20 70 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20   part of SQLite 
28e5f 63 61 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62  causes Sync to b
28e60 65 20 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74  e called by mist
28e61 61 6b 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ake...*/..static
28e62 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63   int memjrnlSync
28e63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e  (sqlite3_file *N
28e64 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55  otUsed, int NotU
28e65 73 65 64 32 29 7b 0d 0a 20 20 55 4e 55 53 45 44  sed2){..  UNUSED
28e66 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
28e67 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d  sed, NotUsed2);.
28e68 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
28e69 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
28e6a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 7a 65  * Query the size
28e6b 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 20   of the file in 
28e6c 62 79 74 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  bytes...*/..stat
28e6d 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69  ic int memjrnlFi
28e6e 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
28e6f 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
28e70 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
28e71 0d 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a  ..  MemJournal *
28e72 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20  p = (MemJournal 
28e73 2a 29 70 4a 66 64 3b 0d 0a 20 20 2a 70 53 69 7a  *)pJfd;..  *pSiz
28e74 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36  e = (sqlite_int6
28e75 34 29 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69  4) p->endpoint.i
28e76 4f 66 66 73 65 74 3b 0d 0a 20 20 72 65 74 75 72  Offset;..  retur
28e77 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
28e78 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 61 62 6c 65 20  .../*..** Table 
28e79 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d  of methods for M
28e7a 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65  emJournal sqlite
28e7b 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0d 0a  3_file object...
28e7c 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  */..static const
28e7d 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
28e7e 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f  io_methods MemJo
28e7f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b  urnalMethods = {
28e80 0d 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  ..  1,          
28e81 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
28e82 6e 20 2a 2f 0d 0a 20 20 6d 65 6d 6a 72 6e 6c 43  n */..  memjrnlC
28e83 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c  lose,     /* xCl
28e84 6f 73 65 20 2a 2f 0d 0a 20 20 6d 65 6d 6a 72 6e  ose */..  memjrn
28e85 6c 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78  lRead,      /* x
28e86 52 65 61 64 20 2a 2f 0d 0a 20 20 6d 65 6d 6a 72  Read */..  memjr
28e87 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a 20  nlWrite,     /* 
28e88 78 57 72 69 74 65 20 2a 2f 0d 0a 20 20 6d 65 6d  xWrite */..  mem
28e89 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 2f  jrnlTruncate,  /
28e8a 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0d 0a  * xTruncate */..
28e8b 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20 20    memjrnlSync,  
28e8c 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0d      /* xSync */.
28e8d 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69  .  memjrnlFileSi
28e8e 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a  ze,  /* xFileSiz
28e8f 65 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20  e */..  0,      
28e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
28e91 63 6b 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20  ck */..  0,     
28e92 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
28e93 6e 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 30 2c 20 20  nlock */..  0,  
28e94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28e95 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
28e96 6f 63 6b 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20  ock */..  0,    
28e97 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
28e98 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0d 0a  FileControl */..
28e99 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
28e9a 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69      /* xSectorSi
28e9b 7a 65 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20  ze */..  0,     
28e9c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
28e9d 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
28e9e 74 69 63 73 20 2a 2f 0d 0a 20 20 30 2c 20 20 20  tics */..  0,   
28e9f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28ea0 78 53 68 6d 4d 61 70 20 2a 2f 0d 0a 20 20 30 2c  xShmMap */..  0,
28ea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28ea2 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 2a 2f 0d 0a  /* xShmLock */..
28ea3 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
28ea4 20 20 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69      /* xShmBarri
28ea5 65 72 20 2a 2f 0d 0a 20 20 30 20 20 20 20 20 20  er */..  0      
28ea6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
28ea7 68 6d 55 6e 6c 6f 63 6b 20 2a 2f 0d 0a 7d 3b 0d  hmUnlock */..};.
28ea8 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 4f 70 65 6e 20  .../* ..** Open 
28ea9 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0d  a journal file..
28eaa 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
28eab 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
28eac 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73  MemJournalOpen(s
28ead 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
28eae 64 29 7b 0d 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61  d){..  MemJourna
28eaf 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e  l *p = (MemJourn
28eb0 61 6c 20 2a 29 70 4a 66 64 3b 0d 0a 20 20 61 73  al *)pJfd;..  as
28eb1 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
28eb2 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b  _ALIGNMENT(p) );
28eb3 0d 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ..  memset(p, 0,
28eb4 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
28eb5 61 6c 53 69 7a 65 28 29 29 3b 0d 0a 20 20 70 2d  alSize());..  p-
28eb6 3e 70 4d 65 74 68 6f 64 20 3d 20 28 73 71 6c 69  >pMethod = (sqli
28eb7 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 2a 29  te3_io_methods*)
28eb8 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f  &MemJournalMetho
28eb9 64 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ds;..}..../*..**
28eba 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
28ebb 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20  the file-handle 
28ebc 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
28ebd 75 6d 65 6e 74 20 69 73 20 0d 0a 2a 2a 20 61 6e  ument is ..** an
28ebe 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
28ebf 61 6c 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  al ..*/..SQLITE_
28ec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
28ec1 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28  te3IsMemJournal(
28ec2 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
28ec3 66 64 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 70  fd){..  return p
28ec4 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26  Jfd->pMethods==&
28ec5 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64  MemJournalMethod
28ec6 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a  s;..}..../* ..**
28ec7 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
28ec8 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
28ec9 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20  ired to store a 
28eca 4d 65 6d 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  MemJournal file 
28ecb 64 65 73 63 72 69 70 74 6f 72 2e 0d 0a 2a 2f 0d  descriptor...*/.
28ecc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28ecd 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  int sqlite3MemJo
28ece 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b  urnalSize(void){
28ecf 0d 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f  ..  return sizeo
28ed0 66 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0d 0a  f(MemJournal);..
28ed1 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }..../**********
28ed2 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 6a  **** End of memj
28ed3 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
28ed4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ed5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ed6 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
28ed7 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
28ed8 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a   walker.c ******
28ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28edb 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
28edc 30 38 20 41 75 67 75 73 74 20 31 36 0d 0a 2a 2a  08 August 16..**
28edd 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
28ede 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
28edf 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
28ee0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
28ee1 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61  ce of..** a lega
28ee2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
28ee3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a  s a blessing:..*
28ee4 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  *..**    May you
28ee5 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
28ee6 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61   evil...**    Ma
28ee7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
28ee8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
28ee9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
28eea 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d  others...**    M
28eeb 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
28eec 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
28eed 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
28eee 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a  give...**..*****
28eef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ef1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ef2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ef3 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69  ****..** This fi
28ef4 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
28ef5 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61  ines used for wa
28ef6 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72  lking the parser
28ef7 20 74 72 65 65 20 66 6f 72 0d 0a 2a 2a 20 61 6e   tree for..** an
28ef8 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0d   SQL statement..
28ef9 0a 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  .*/../* #include
28efa 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0d 0a   <stdlib.h> */..
28efb 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72  /* #include <str
28efc 69 6e 67 2e 68 3e 20 2a 2f 0d 0a 0d 0a 0d 0a 2f  ing.h> */....../
28efd 2a 0d 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  *..** Walk an ex
28efe 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
28eff 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
28f00 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  ack once for eac
28f01 68 20 6e 6f 64 65 0d 0a 2a 2a 20 6f 66 20 74 68  h node..** of th
28f02 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68  e expression, wh
28f03 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20  ile decending.  
28f04 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
28f05 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0d 0a 2a   the callback..*
28f06 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66  * is invoked bef
28f07 6f 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69  ore visiting chi
28f08 6c 64 72 65 6e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20  ldren.)..**..** 
28f09 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
28f0a 20 66 72 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61   from the callba
28f0b 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65  ck should be one
28f0c 20 6f 66 20 74 68 65 20 57 52 43 5f 2a 0d 0a 2a   of the WRC_*..*
28f0d 2a 20 63 6f 6e 73 74 61 6e 74 73 20 74 6f 20 73  * constants to s
28f0e 70 65 63 69 66 79 20 68 6f 77 20 74 6f 20 70 72  pecify how to pr
28f0f 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 77  oceed with the w
28f10 61 6c 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  alk...**..**    
28f11 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20  WRC_Continue    
28f12 20 20 43 6f 6e 74 69 6e 75 65 20 64 65 73 63 65    Continue desce
28f13 6e 64 69 6e 67 20 64 6f 77 6e 20 74 68 65 20 74  nding down the t
28f14 72 65 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ree...**..**    
28f15 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20  WRC_Prune       
28f16 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 6e 64    Do not descend
28f17 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f 64 65   into child node
28f18 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0d 0a 2a  s.  But allow..*
28f19 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
28f1a 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b 20         the walk 
28f1b 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
28f1c 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e 0d   sibling nodes..
28f1d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 57 52 43 5f 41  .**..**    WRC_A
28f1e 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20  bort         Do 
28f1f 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b  no more callback
28f20 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73  s.  Unwind the s
28f21 74 61 63 6b 20 61 6e 64 0d 0a 2a 2a 20 20 20 20  tack and..**    
28f22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28f23 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70    return the top
28f24 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c  -level walk call
28f25 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65  ...**..** The re
28f26 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20  turn value from 
28f27 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
28f28 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61  WRC_Abort to aba
28f29 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61  ndon the tree wa
28f2a 6c 6b 0d 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43  lk..** and WRC_C
28f2b 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69  ontinue to conti
28f2c 6e 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  nue...*/..SQLITE
28f2d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
28f2e 69 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c  ite3WalkExpr(Wal
28f2f 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
28f30 70 72 20 2a 70 45 78 70 72 29 7b 0d 0a 20 20 69  pr *pExpr){..  i
28f31 6e 74 20 72 63 3b 0d 0a 20 20 69 66 28 20 70 45  nt rc;..  if( pE
28f32 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  xpr==0 ) return 
28f33 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0d 0a 20  WRC_Continue;.. 
28f34 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48   testcase( ExprH
28f35 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
28f36 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  , EP_TokenOnly) 
28f37 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
28f38 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
28f39 70 45 78 70 72 2c 20 45 50 5f 52 65 64 75 63 65  pExpr, EP_Reduce
28f3a 64 29 20 29 3b 0d 0a 20 20 72 63 20 3d 20 70 57  d) );..  rc = pW
28f3b 61 6c 6b 65 72 2d 3e 78 45 78 70 72 43 61 6c 6c  alker->xExprCall
28f3c 62 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 45  back(pWalker, pE
28f3d 78 70 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d  xpr);..  if( rc=
28f3e 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 0d 0a 20  =WRC_Continue.. 
28f3f 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
28f40 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
28f41 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54 6f 6b  rty(pExpr,EP_Tok
28f42 65 6e 4f 6e 6c 79 29 20 29 7b 0d 0a 20 20 20 20  enOnly) ){..    
28f43 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
28f44 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 78  xpr(pWalker, pEx
28f45 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74  pr->pLeft) ) ret
28f46 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a  urn WRC_Abort;..
28f47 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
28f48 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
28f49 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20   pExpr->pRight) 
28f4a 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
28f4b 72 74 3b 0d 0a 20 20 20 20 69 66 28 20 45 78 70  rt;..    if( Exp
28f4c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
28f4d 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
28f4e 29 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  ) ){..      if( 
28f4f 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
28f50 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72  t(pWalker, pExpr
28f51 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72  ->x.pSelect) ) r
28f52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
28f53 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
28f54 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
28f55 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
28f56 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ker, pExpr->x.pL
28f57 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  ist) ) return WR
28f58 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20 7d 0d  C_Abort;..    }.
28f59 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
28f5a 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a  c & WRC_Abort;..
28f5b 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c  }..../*..** Call
28f5c 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
28f5d 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
28f5e 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20  ression in list 
28f5f 70 20 6f 72 20 75 6e 74 69 6c 0d 0a 2a 2a 20 61  p or until..** a
28f60 6e 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20  n abort request 
28f61 69 73 20 73 65 65 6e 2e 0d 0a 2a 2f 0d 0a 53 51  is seen...*/..SQ
28f62 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
28f63 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
28f64 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a 70 57 61  List(Walker *pWa
28f65 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 74 20 2a  lker, ExprList *
28f66 70 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  p){..  int i;.. 
28f67 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
28f68 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 20  _item *pItem;.. 
28f69 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 66   if( p ){..    f
28f6a 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70  or(i=p->nExpr, p
28f6b 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20  Item=p->a; i>0; 
28f6c 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a  i--, pItem++){..
28f6d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
28f6e 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
28f6f 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  r, pItem->pExpr)
28f70 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
28f71 6f 72 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ort;..    }..  }
28f72 0d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  ..  return WRC_C
28f73 6f 6e 74 69 6e 75 65 3b 0d 0a 7d 0d 0a 0d 0a 2f  ontinue;..}..../
28f74 2a 0d 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c 20 65  *..** Walk all e
28f75 78 70 72 65 73 73 69 6f 6e 73 20 61 73 73 6f 63  xpressions assoc
28f76 69 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 43  iated with SELEC
28f77 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20  T statement p.  
28f78 44 6f 0d 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f 6b  Do..** not invok
28f79 65 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6c  e the SELECT cal
28f7a 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20  lback on p, but 
28f7b 64 6f 20 28 6f 66 20 63 6f 75 72 73 65 29 20 69  do (of course) i
28f7c 6e 76 6f 6b 65 0d 0a 2a 2a 20 61 6e 79 20 65 78  nvoke..** any ex
28f7d 70 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64  pr callbacks and
28f7e 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b   SELECT callback
28f7f 73 20 74 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d  s that come from
28f80 20 73 75 62 71 75 65 72 69 65 73 2e 0d 0a 2a 2a   subqueries...**
28f81 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   Return WRC_Abor
28f82 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75  t or WRC_Continu
28f83 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
28f84 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
28f85 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72  e3WalkSelectExpr
28f86 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
28f87 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0d 0a 20  , Select *p){.. 
28f88 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
28f89 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
28f8a 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 72  , p->pEList) ) r
28f8b 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
28f8c 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ..  if( sqlite3W
28f8d 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
28f8e 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65   p->pWhere) ) re
28f8f 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d  turn WRC_Abort;.
28f90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
28f91 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b  lkExprList(pWalk
28f92 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29  er, p->pGroupBy)
28f93 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
28f94 6f 72 74 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  ort;..  if( sqli
28f95 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
28f96 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  ker, p->pHaving)
28f97 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
28f98 6f 72 74 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  ort;..  if( sqli
28f99 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
28f9a 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64  pWalker, p->pOrd
28f9b 65 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57  erBy) ) return W
28f9c 52 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 69 66 28  RC_Abort;..  if(
28f9d 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
28f9e 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69  (pWalker, p->pLi
28f9f 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  mit) ) return WR
28fa0 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 69 66 28 20  C_Abort;..  if( 
28fa1 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
28fa2 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66  pWalker, p->pOff
28fa3 73 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  set) ) return WR
28fa4 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 72 65 74 75  C_Abort;..  retu
28fa5 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
28fa6 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 61  ..}..../*..** Wa
28fa7 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 65  lk the parse tre
28fa8 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
28fa9 74 68 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  th all subquerie
28faa 73 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 46 52 4f  s in the..** FRO
28fab 4d 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45  M clause of SELE
28fac 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20  CT statement p. 
28fad 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74   Do not invoke t
28fae 68 65 20 73 65 6c 65 63 74 0d 0a 2a 2a 20 63 61  he select..** ca
28faf 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74  llback on p, but
28fb0 20 64 6f 20 69 6e 76 6f 6b 65 20 69 74 20 6f 6e   do invoke it on
28fb1 20 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 73   each FROM claus
28fb2 65 20 73 75 62 71 75 65 72 79 0d 0a 2a 2a 20 61  e subquery..** a
28fb3 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 71 75 65  nd on any subque
28fb4 72 69 65 73 20 66 75 72 74 68 65 72 20 64 6f 77  ries further dow
28fb5 6e 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20  n in the tree.  
28fb6 52 65 74 75 72 6e 20 0d 0a 2a 2a 20 57 52 43 5f  Return ..** WRC_
28fb7 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e  Abort or WRC_Con
28fb8 74 69 6e 75 65 3b 0d 0a 2a 2f 0d 0a 53 51 4c 49  tinue;..*/..SQLI
28fb9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
28fba 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
28fbb 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61  From(Walker *pWa
28fbc 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
28fbd 7b 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  {..  SrcList *pS
28fbe 72 63 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  rc;..  int i;.. 
28fbf 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
28fc0 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 0d 0a  item *pItem;....
28fc1 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
28fc2 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ;..  if( ALWAYS(
28fc3 70 53 72 63 29 20 29 7b 0d 0a 20 20 20 20 66 6f  pSrc) ){..    fo
28fc4 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20  r(i=pSrc->nSrc, 
28fc5 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69  pItem=pSrc->a; i
28fc6 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
28fc7 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71  ){..      if( sq
28fc8 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
28fc9 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e  pWalker, pItem->
28fca 70 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20 20 20  pSelect) ){..   
28fcb 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
28fcc 41 62 6f 72 74 3b 0d 0a 20 20 20 20 20 20 7d 0d  Abort;..      }.
28fcd 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
28fce 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
28fcf 75 65 3b 0d 0a 7d 20 0d 0a 0d 0a 2f 2a 0d 0a 2a  ue;..} ..../*..*
28fd0 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61  * Call sqlite3Wa
28fd1 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65  lkExpr() for eve
28fd2 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
28fd3 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   Select statemen
28fd4 74 20 70 2e 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20  t p...** Invoke 
28fd5 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
28fd6 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69  t() for subqueri
28fd7 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
28fd8 6c 61 75 73 65 20 61 6e 64 0d 0a 2a 2a 20 6f 6e  lause and..** on
28fd9 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
28fda 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70  lect chain, p->p
28fdb 50 72 69 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52  Prior...**..** R
28fdc 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
28fdd 75 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20  ue under normal 
28fde 63 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74  conditions.  Ret
28fdf 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66  urn WRC_Abort if
28fe0 0d 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e  ..** there is an
28fe1 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0d   abort request..
28fe2 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 57  .**..** If the W
28fe3 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74 20 68  alker does not h
28fe4 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74 43 61  ave an xSelectCa
28fe5 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20 74 68  llback() then th
28fe6 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 69  is routine..** i
28fe7 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72 6e  s a no-op return
28fe8 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  ing WRC_Continue
28fe9 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
28fea 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
28feb 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b  3WalkSelect(Walk
28fec 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
28fed 65 63 74 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20  ect *p){..  int 
28fee 72 63 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20  rc;..  if( p==0 
28fef 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c  || pWalker->xSel
28ff0 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29  ectCallback==0 )
28ff1 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
28ff2 69 6e 75 65 3b 0d 0a 20 20 72 63 20 3d 20 57 52  inue;..  rc = WR
28ff3 43 5f 43 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 77  C_Continue;..  w
28ff4 68 69 6c 65 28 20 70 20 20 29 7b 0d 0a 20 20 20  hile( p  ){..   
28ff5 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78   rc = pWalker->x
28ff6 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70  SelectCallback(p
28ff7 57 61 6c 6b 65 72 2c 20 70 29 3b 0d 0a 20 20 20  Walker, p);..   
28ff8 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b   if( rc ) break;
28ff9 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
28ffa 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28  3WalkSelectExpr(
28ffb 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65  pWalker, p) ) re
28ffc 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d  turn WRC_Abort;.
28ffd 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
28ffe 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70  WalkSelectFrom(p
28fff 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74  Walker, p) ) ret
29000 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a  urn WRC_Abort;..
29001 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f      p = p->pPrio
29002 72 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  r;..  }..  retur
29003 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74  n rc & WRC_Abort
29004 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ;..}..../*******
29005 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77  ******* End of w
29006 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  alker.c ********
29007 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29009 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
2900a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2900b 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a  ile resolve.c **
2900c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2900d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2900e 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  *******/../*..**
2900f 20 32 30 30 38 20 41 75 67 75 73 74 20 31 38 0d   2008 August 18.
29010 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
29011 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
29012 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
29013 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
29014 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
29015 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
29016 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
29017 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
29018 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
29019 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
2901a 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2901b 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2901c 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2901d 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
2901e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2901f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
29020 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
29021 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
29022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29023 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29026 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  *******..**..** 
29027 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
29028 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64  ns routines used
29029 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65   for walking the
2902a 20 70 61 72 73 65 72 20 74 72 65 65 20 61 6e 64   parser tree and
2902b 0d 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61 6c 6c  ..** resolve all
2902c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 79 20   identifiers by 
2902d 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68 65 6d  associating them
2902e 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c   with a particul
2902f 61 72 0d 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64  ar..** table and
29030 20 63 6f 6c 75 6d 6e 2e 0d 0a 2a 2f 0d 0a 2f 2a   column...*/../*
29031 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69   #include <stdli
29032 62 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63  b.h> */../* #inc
29033 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20  lude <string.h> 
29034 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 75 72  */..../*..** Tur
29035 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70 72  n the pExpr expr
29036 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 61  ession into an a
29037 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43 6f  lias for the iCo
29038 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  l-th column of t
29039 68 65 0d 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  he..** result se
2903a 74 20 69 6e 20 70 45 4c 69 73 74 2e 0d 0a 2a 2a  t in pEList...**
2903b 0d 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75  ..** If the resu
2903c 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73  lt set column is
2903d 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e   a simple column
2903e 20 72 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e   reference, then
2903f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a   this routine..*
29040 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61 63 74  * makes an exact
29041 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f 72 20   copy.  But for 
29042 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
29043 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  f expression, th
29044 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d  is..** routine m
29045 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
29046 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  e result set col
29047 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75 6d  umn as the argum
29048 65 6e 74 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 54  ent to the..** T
29049 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20  K_AS operator.  
2904a 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
2904b 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78  or causes the ex
2904c 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0d 0a  pression to be..
2904d 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73  ** evaluated jus
2904e 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20  t once and then 
2904f 72 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20  reused for each 
29050 61 6c 69 61 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  alias...**..** T
29051 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75  he reason for su
29052 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b  ppressing the TK
29053 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68  _AS term when th
29054 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
29055 61 20 73 69 6d 70 6c 65 0d 0a 2a 2a 20 63 6f 6c  a simple..** col
29056 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 73  umn reference is
29057 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6c   so that the col
29058 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 69  umn reference wi
29059 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 64  ll be recognized
2905a 20 61 73 0d 0a 2a 2a 20 75 73 61 62 6c 65 20 62   as..** usable b
2905b 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e  y indices within
2905c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2905d 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67  e processing log
2905e 69 63 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 48 61 63  ic. ..**..** Hac
2905f 6b 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70  k:  The TK_AS op
29060 65 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69  erator is inhibi
29061 74 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d  ted if zType[0]=
29062 3d 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e  ='G'.  This mean
29063 73 0d 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20  s..** that in a 
29064 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c  GROUP BY clause,
29065 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
29066 69 73 20 65 76 61 6c 75 61 74 65 64 20 74 77 69  is evaluated twi
29067 63 65 2e 20 20 48 65 6e 63 65 3a 0d 0a 2a 2a 0d  ce.  Hence:..**.
29068 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72  .**     SELECT r
29069 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20  andom()%5 AS x, 
2906a 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61  count(*) FROM ta
2906b 62 20 47 52 4f 55 50 20 42 59 20 78 0d 0a 2a 2a  b GROUP BY x..**
2906c 0d 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65  ..** Is equivale
2906d 6e 74 20 74 6f 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  nt to:..**..**  
2906e 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d     SELECT random
2906f 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74  ()%5 AS x, count
29070 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f  (*) FROM tab GRO
29071 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29 25 35  UP BY random()%5
29072 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 73  ..**..** The res
29073 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25  ult of random()%
29074 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42  5 in the GROUP B
29075 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62  Y clause is prob
29076 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0d 0a  ably different..
29077 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75  ** from the resu
29078 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  lt in the result
29079 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20  -set.  We might 
2907a 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79  fix this someday
2907b 2e 20 20 4f 72 0d 0a 2a 2a 20 74 68 65 6e 20 61  .  Or..** then a
2907c 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e  gain, we might n
2907d 6f 74 2e 2e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ot.....*/..stati
2907e 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c  c void resolveAl
2907f 69 61 73 28 0d 0a 20 20 50 61 72 73 65 20 2a 70  ias(..  Parse *p
29080 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
29081 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
29082 74 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74  t */..  ExprList
29083 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f   *pEList,      /
29084 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a  * A result set *
29085 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20  /..  int iCol,  
29086 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
29087 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72   column in the r
29088 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70  esult set.  0..p
29089 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a  EList->nExpr-1 *
2908a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  /..  Expr *pExpr
2908b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
2908c 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e  ransform this in
2908d 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74  to an alias to t
2908e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
2908f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
29090 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20 22 47  zType      /* "G
29091 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22  ROUP" or "ORDER"
29092 20 6f 72 20 22 22 20 2a 2f 0d 0a 29 7b 0d 0a 20   or "" */..){.. 
29093 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20   Expr *pOrig;   
29094 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
29095 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
29096 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
29097 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 44 75 70  */..  Expr *pDup
29098 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
29099 43 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f  Copy of pOrig */
2909a 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ..  sqlite3 *db;
2909b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2909c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2909d 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 61 73  ction */....  as
2909e 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26 26  sert( iCol>=0 &&
2909f 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45   iCol<pEList->nE
290a0 78 70 72 20 29 3b 0d 0a 20 20 70 4f 72 69 67 20  xpr );..  pOrig 
290a1 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c  = pEList->a[iCol
290a2 5d 2e 70 45 78 70 72 3b 0d 0a 20 20 61 73 73 65  ].pExpr;..  asse
290a3 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0d  rt( pOrig!=0 );.
290a4 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67  .  assert( pOrig
290a5 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73  ->flags & EP_Res
290a6 6f 6c 76 65 64 20 29 3b 0d 0a 20 20 64 62 20 3d  olved );..  db =
290a7 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20   pParse->db;..  
290a8 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 3d 54  if( pOrig->op!=T
290a9 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79 70  K_COLUMN && zTyp
290aa 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0d 0a 20 20  e[0]!='G' ){..  
290ab 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
290ac 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
290ad 67 2c 20 30 29 3b 0d 0a 20 20 20 20 70 44 75 70  g, 0);..    pDup
290ae 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
290af 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70  pParse, TK_AS, p
290b0 44 75 70 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  Dup, 0, 0);..   
290b1 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72   if( pDup==0 ) r
290b2 65 74 75 72 6e 3b 0d 0a 20 20 20 20 69 66 28 20  eturn;..    if( 
290b3 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
290b4 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0d 0a 20 20  iAlias==0 ){..  
290b5 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43      pEList->a[iC
290b6 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28 75 31  ol].iAlias = (u1
290b7 36 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c  6)(++pParse->nAl
290b8 69 61 73 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ias);..    }..  
290b9 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d    pDup->iTable =
290ba 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d   pEList->a[iCol]
290bb 2e 69 41 6c 69 61 73 3b 0d 0a 20 20 7d 65 6c 73  .iAlias;..  }els
290bc 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f  e if( ExprHasPro
290bd 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f  perty(pOrig, EP_
290be 49 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70 4f 72  IntValue) || pOr
290bf 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20  ig->u.zToken==0 
290c0 29 7b 0d 0a 20 20 20 20 70 44 75 70 20 3d 20 73  ){..    pDup = s
290c1 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
290c2 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0d 0a 20 20  , pOrig, 0);..  
290c3 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20    if( pDup==0 ) 
290c4 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 65 6c 73 65  return;..  }else
290c5 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f  {..    char *zTo
290c6 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a  ken = pOrig->u.z
290c7 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 61 73 73 65  Token;..    asse
290c8 72 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b  rt( zToken!=0 );
290c9 0d 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a  ..    pOrig->u.z
290ca 54 6f 6b 65 6e 20 3d 20 30 3b 0d 0a 20 20 20 20  Token = 0;..    
290cb 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
290cc 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 2c  prDup(db, pOrig,
290cd 20 30 29 3b 0d 0a 20 20 20 20 70 4f 72 69 67 2d   0);..    pOrig-
290ce 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b  >u.zToken = zTok
290cf 65 6e 3b 0d 0a 20 20 20 20 69 66 28 20 70 44 75  en;..    if( pDu
290d0 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  p==0 ) return;..
290d1 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75      assert( (pDu
290d2 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52  p->flags & (EP_R
290d3 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
290d4 6e 6c 79 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 20  nly))==0 );..   
290d5 20 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d   pDup->flags2 |=
290d6 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b   EP2_MallocedTok
290d7 65 6e 3b 0d 0a 20 20 20 20 70 44 75 70 2d 3e 75  en;..    pDup->u
290d8 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65  .zToken = sqlite
290d9 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54  3DbStrDup(db, zT
290da 6f 6b 65 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69  oken);..  }..  i
290db 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  f( pExpr->flags 
290dc 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
290dd 29 7b 0d 0a 20 20 20 20 70 44 75 70 2d 3e 70 43  ){..    pDup->pC
290de 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f  oll = pExpr->pCo
290df 6c 6c 3b 0d 0a 20 20 20 20 70 44 75 70 2d 3e 66  ll;..    pDup->f
290e0 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
290e1 6c 6c 61 74 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  llate;..  }.... 
290e2 20 2f 2a 20 42 65 66 6f 72 65 20 63 61 6c 6c 69   /* Before calli
290e3 6e 67 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  ng sqlite3ExprDe
290e4 6c 65 74 65 28 29 2c 20 73 65 74 20 74 68 65 20  lete(), set the 
290e5 45 50 5f 53 74 61 74 69 63 20 66 6c 61 67 2e 20  EP_Static flag. 
290e6 54 68 69 73 20 0d 0a 20 20 2a 2a 20 70 72 65 76  This ..  ** prev
290e7 65 6e 74 73 20 45 78 70 72 44 65 6c 65 74 65 28  ents ExprDelete(
290e8 29 20 66 72 6f 6d 20 64 65 6c 65 74 69 6e 67 20  ) from deleting 
290e9 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75  the Expr structu
290ea 72 65 20 69 74 73 65 6c 66 2c 0d 0a 20 20 2a 2a  re itself,..  **
290eb 20 61 6c 6c 6f 77 69 6e 67 20 69 74 20 74 6f 20   allowing it to 
290ec 62 65 20 72 65 70 6f 70 75 6c 61 74 65 64 20 62  be repopulated b
290ed 79 20 74 68 65 20 6d 65 6d 63 70 79 28 29 20 6f  y the memcpy() o
290ee 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
290ef 6c 69 6e 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 45  line...  */..  E
290f0 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
290f1 45 78 70 72 2c 20 45 50 5f 53 74 61 74 69 63 29  Expr, EP_Static)
290f2 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
290f3 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
290f4 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 70 45 78  );..  memcpy(pEx
290f5 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66  pr, pDup, sizeof
290f6 28 2a 70 45 78 70 72 29 29 3b 0d 0a 20 20 73 71  (*pExpr));..  sq
290f7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
290f8 70 44 75 70 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  pDup);..}....../
290f9 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  *..** Return TRU
290fa 45 20 69 66 20 74 68 65 20 6e 61 6d 65 20 7a 43  E if the name zC
290fb 6f 6c 20 6f 63 63 75 72 73 20 61 6e 79 77 68 65  ol occurs anywhe
290fc 72 65 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  re in the USING 
290fd 63 6c 61 75 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  clause...**..** 
290fe 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20  Return FALSE if 
290ff 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
29100 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20 69   is NULL or if i
29101 74 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  t does not conta
29102 69 6e 0d 0a 2a 2a 20 7a 43 6f 6c 2e 0d 0a 2a 2f  in..** zCol...*/
29103 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 61 6d  ..static int nam
29104 65 49 6e 55 73 69 6e 67 43 6c 61 75 73 65 28 49  eInUsingClause(I
29105 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 2c 20 63  dList *pUsing, c
29106 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29  onst char *zCol)
29107 7b 0d 0a 20 20 69 66 28 20 70 55 73 69 6e 67 20  {..  if( pUsing 
29108 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6b 3b 0d 0a  ){..    int k;..
29109 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
2910a 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29  Using->nId; k++)
2910b 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  {..      if( sql
2910c 69 74 65 33 53 74 72 49 43 6d 70 28 70 55 73 69  ite3StrICmp(pUsi
2910d 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20  ng->a[k].zName, 
2910e 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72  zCol)==0 ) retur
2910f 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  n 1;..    }..  }
29110 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
29111 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76  ....../*..** Giv
29112 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  en the name of a
29113 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66   column of the f
29114 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a  orm X.Y.Z or Y.Z
29115 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b   or just Z, look
29116 20 75 70 0d 0a 2a 2a 20 74 68 61 74 20 6e 61 6d   up..** that nam
29117 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20  e in the set of 
29118 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e  source tables in
29119 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61   pSrcList and ma
2911a 6b 65 20 74 68 65 20 70 45 78 70 72 20 0d 0a 2a  ke the pExpr ..*
2911b 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  * expression nod
2911c 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20  e refer back to 
2911d 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75  that source colu
2911e 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  mn.  The followi
2911f 6e 67 20 63 68 61 6e 67 65 73 0d 0a 2a 2a 20 61  ng changes..** a
29120 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72  re made to pExpr
29121 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 70 45 78  :..**..**    pEx
29122 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20  pr->iDb         
29123 20 20 53 65 74 20 74 68 65 20 69 6e 64 65 78 20    Set the index 
29124 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20  in db->aDb[] of 
29125 74 68 65 20 64 61 74 61 62 61 73 65 20 58 0d 0a  the database X..
29126 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29127 20 20 20 20 20 20 20 20 20 20 20 28 65 76 65 6e             (even
29128 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64   if X is implied
29129 29 2e 0d 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  )...**    pExpr-
2912a 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 53  >iTable        S
2912b 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72  et to the cursor
2912c 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2912d 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0d 0a  table obtained..
2912e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2912f 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20             from 
29130 70 53 72 63 4c 69 73 74 2e 0d 0a 2a 2a 20 20 20  pSrcList...**   
29131 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20   pExpr->pTab    
29132 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20        Points to 
29133 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
29134 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e  ure of X.Y (even
29135 20 69 66 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20   if..**         
29136 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29137 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69  X and/or Y are i
29138 6d 70 6c 69 65 64 2e 29 0d 0a 2a 2a 20 20 20 20  mplied.)..**    
29139 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20  pExpr->iColumn  
2913a 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20       Set to the 
2913b 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69  column number wi
2913c 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0d  thin the table..
2913d 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .**    pExpr->op
2913e 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20              Set 
2913f 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0d 0a 2a  to TK_COLUMN...*
29140 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  *    pExpr->pLef
29141 74 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78  t         Any ex
29142 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f  pression this po
29143 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74  ints to is delet
29144 65 64 0d 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  ed..**    pExpr-
29145 3e 70 52 69 67 68 74 20 20 20 20 20 20 20 20 41  >pRight        A
29146 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
29147 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  is points to is 
29148 64 65 6c 65 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  deleted...**..**
29149 20 54 68 65 20 7a 44 62 20 76 61 72 69 61 62 6c   The zDb variabl
2914a 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
2914b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 74   the database (t
2914c 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 20 76  he "X").  This v
2914d 61 6c 75 65 20 6d 61 79 20 62 65 0d 0a 2a 2a 20  alue may be..** 
2914e 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 61  NULL meaning tha
2914f 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 65  t name is of the
29150 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e 20   form Y.Z or Z. 
29151 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 64   Any available d
29152 61 74 61 62 61 73 65 0d 0a 2a 2a 20 63 61 6e 20  atabase..** can 
29153 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54  be used.  The zT
29154 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73  able variable is
29155 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
29156 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29   table (the "Y")
29157 2e 20 20 54 68 69 73 0d 0a 2a 2a 20 76 61 6c 75  .  This..** valu
29158 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66  e can be NULL if
29159 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c   zDb is also NUL
2915a 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73  L.  If zTable is
2915b 20 4e 55 4c 4c 20 69 74 0d 0a 2a 2a 20 6d 65 61   NULL it..** mea
2915c 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d  ns that the form
2915d 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20   of the name is 
2915e 5a 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d  Z and that colum
2915f 6e 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c  ns from any tabl
29160 65 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  e..** can be use
29161 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  d...**..** If th
29162 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65  e name cannot be
29163 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 69   resolved unambi
29164 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 61  guously, leave a
29165 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0d  n error message.
29166 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e  .** in pParse an
29167 64 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  d return WRC_Abo
29168 72 74 2e 20 20 52 65 74 75 72 6e 20 57 52 43 5f  rt.  Return WRC_
29169 50 72 75 6e 65 20 6f 6e 20 73 75 63 63 65 73 73  Prune on success
2916a 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
2916b 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0d 0a 20  t lookupName(.. 
2916c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2916d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2916e 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d  sing context */.
2916f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
29170 44 62 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  Db,     /* Name 
29171 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
29172 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
29173 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20  , or NULL */..  
29174 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
29175 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
29176 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
29177 20 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c   column, or NULL
29178 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
29179 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e  r *zCol,    /* N
2917a 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ame of the colum
2917b 6e 2e 20 2a 2f 0d 0a 20 20 4e 61 6d 65 43 6f 6e  n. */..  NameCon
2917c 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a  text *pNC,    /*
2917d 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78   The name contex
2917e 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76  t used to resolv
2917f 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20  e the name */.. 
29180 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
29181 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 74 68        /* Make th
29182 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 6f 69  is EXPR node poi
29183 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74  nt to the select
29184 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 29 7b  ed column */..){
29185 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  ..  int i, j;   
29186 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
29187 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d 0a 20 20   counters */..  
29188 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20  int cnt = 0;    
29189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2918a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
2918b 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e  atching column n
2918c 61 6d 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 63  ames */..  int c
2918d 6e 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20  ntTab = 0;      
2918e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2918f 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69  Number of matchi
29190 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a  ng table names *
29191 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  /..  sqlite3 *db
29192 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
29193 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
29194 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
29195 6e 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 53  n */..  struct S
29196 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
29197 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65  em;       /* Use
29198 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   for looping ove
29199 72 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73  r pSrcList items
2919a 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 53 72   */..  struct Sr
2919b 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74  cList_item *pMat
2919c 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20  ch = 0;  /* The 
2919d 6d 61 74 63 68 69 6e 67 20 70 53 72 63 4c 69 73  matching pSrcLis
2919e 74 20 69 74 65 6d 20 2a 2f 0d 0a 20 20 4e 61 6d  t item */..  Nam
2919f 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43  eContext *pTopNC
291a0 20 3d 20 70 4e 43 3b 20 20 20 20 20 20 20 20 2f   = pNC;        /
291a1 2a 20 46 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74  * First namecont
291a2 65 78 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ext in the list 
291a3 2a 2f 0d 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  */..  Schema *pS
291a4 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 20 20 20  chema = 0;      
291a5 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d          /* Schem
291a6 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  a of the express
291a7 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73  ion */..  int is
291a8 54 72 69 67 67 65 72 20 3d 20 30 3b 0d 0a 0d 0a  Trigger = 0;....
291a9 20 20 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b    assert( pNC );
291aa 20 20 20 20 20 2f 2a 20 74 68 65 20 6e 61 6d 65       /* the name
291ab 20 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20   context cannot 
291ac 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 61  be NULL. */..  a
291ad 73 73 65 72 74 28 20 7a 43 6f 6c 20 29 3b 20 20  ssert( zCol );  
291ae 20 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e    /* The Z in X.
291af 59 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55  Y.Z cannot be NU
291b0 4c 4c 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  LL */..  assert(
291b1 20 7e 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   ~ExprHasAnyProp
291b2 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54  erty(pExpr, EP_T
291b3 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75  okenOnly|EP_Redu
291b4 63 65 64 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  ced) );....  /* 
291b5 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  Initialize the n
291b6 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 68 20  ode to no-match 
291b7 2a 2f 0d 0a 20 20 70 45 78 70 72 2d 3e 69 54 61  */..  pExpr->iTa
291b8 62 6c 65 20 3d 20 2d 31 3b 0d 0a 20 20 70 45 78  ble = -1;..  pEx
291b9 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0d 0a 20  pr->pTab = 0;.. 
291ba 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
291bb 62 6c 65 28 70 45 78 70 72 29 3b 0d 0a 0d 0a 20  ble(pExpr);.... 
291bc 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74 68 65   /* Start at the
291bd 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74   inner-most cont
291be 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74  ext and move out
291bf 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d 61 74  ward until a mat
291c0 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0d 0a  ch is found */..
291c1 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20    while( pNC && 
291c2 63 6e 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 45  cnt==0 ){..    E
291c3 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
291c4 0d 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  ..    SrcList *p
291c5 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  SrcList = pNC->p
291c6 53 72 63 4c 69 73 74 3b 0d 0a 0d 0a 20 20 20 20  SrcList;....    
291c7 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0d  if( pSrcList ){.
291c8 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  .      for(i=0, 
291c9 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e  pItem=pSrcList->
291ca 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e  a; i<pSrcList->n
291cb 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
291cc 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 54 61 62  +){..        Tab
291cd 6c 65 20 2a 70 54 61 62 3b 0d 0a 20 20 20 20 20  le *pTab;..     
291ce 20 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20 20 20     int iDb;..   
291cf 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f       Column *pCo
291d0 6c 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 20 20  l;..  ..        
291d1 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54  pTab = pItem->pT
291d2 61 62 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73  ab;..        ass
291d3 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 20  ert( pTab!=0 && 
291d4 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29  pTab->zName!=0 )
291d5 3b 0d 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  ;..        iDb =
291d6 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
291d7 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
291d8 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 20 20 20  pSchema);..     
291d9 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
291da 3e 6e 43 6f 6c 3e 30 20 29 3b 0d 0a 20 20 20 20  >nCol>0 );..    
291db 20 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0d      if( zTab ){.
291dc 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
291dd 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0d  Item->zAlias ){.
291de 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
291df 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49  r *zTabName = pI
291e0 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0d 0a 20 20  tem->zAlias;..  
291e1 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
291e2 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61  lite3StrICmp(zTa
291e3 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20  bName, zTab)!=0 
291e4 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
291e5 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
291e6 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
291e7 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62  *zTabName = pTab
291e8 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20  ->zName;..      
291e9 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 52 28        if( NEVER(
291ea 7a 54 61 62 4e 61 6d 65 3d 3d 30 29 20 7c 7c 20  zTabName==0) || 
291eb 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
291ec 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d  TabName, zTab)!=
291ed 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  0 ){..          
291ee 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20      continue;.. 
291ef 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20             }..  
291f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44            if( zD
291f1 62 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  b!=0 && sqlite3S
291f2 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69  trICmp(db->aDb[i
291f3 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21  Db].zName, zDb)!
291f4 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  =0 ){..         
291f5 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a       continue;..
291f6 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20              }.. 
291f7 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
291f8 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
291f9 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29  f( 0==(cntTab++)
291fa 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70   ){..          p
291fb 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
291fc 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0d 0a  Item->iCursor;..
291fd 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
291fe 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0d 0a 20  >pTab = pTab;.. 
291ff 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61           pSchema
29200 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
29201 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61  ;..          pMa
29202 74 63 68 20 3d 20 70 49 74 65 6d 3b 0d 0a 20 20  tch = pItem;..  
29203 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
29204 20 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70   for(j=0, pCol=p
29205 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61  Tab->aCol; j<pTa
29206 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43  b->nCol; j++, pC
29207 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  ol++){..        
29208 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
29209 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  ICmp(pCol->zName
2920a 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0d 0a 20  , zCol)==0 ){.. 
2920b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
2920c 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20   there has been 
2920d 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 72 69 6f  exactly one prio
2920e 72 20 6d 61 74 63 68 20 61 6e 64 20 74 68 69 73  r match and this
2920f 20 6d 61 74 63 68 0d 0a 20 20 20 20 20 20 20 20   match..        
29210 20 20 20 20 2a 2a 20 69 73 20 66 6f 72 20 74 68      ** is for th
29211 65 20 72 69 67 68 74 2d 68 61 6e 64 20 74 61 62  e right-hand tab
29212 6c 65 20 6f 66 20 61 20 4e 41 54 55 52 41 4c 20  le of a NATURAL 
29213 4a 4f 49 4e 20 6f 72 20 69 73 20 69 6e 20 61 20  JOIN or is in a 
29214 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  ..            **
29215 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2c 20 74   USING clause, t
29216 68 65 6e 20 73 6b 69 70 20 74 68 69 73 20 6d 61  hen skip this ma
29217 74 63 68 2e 0d 0a 20 20 20 20 20 20 20 20 20 20  tch...          
29218 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20    */..          
29219 20 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0d    if( cnt==1 ){.
2921a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2921b 66 28 20 70 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  f( pItem->jointy
2921c 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20  pe & JT_NATURAL 
2921d 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
2921e 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e             if( n
2921f 61 6d 65 49 6e 55 73 69 6e 67 43 6c 61 75 73 65  ameInUsingClause
29220 28 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 2c 20  (pItem->pUsing, 
29221 7a 43 6f 6c 29 20 29 20 63 6f 6e 74 69 6e 75 65  zCol) ) continue
29222 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
29223 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6e  ..            cn
29224 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  t++;..          
29225 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
29226 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  = pItem->iCursor
29227 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ;..            p
29228 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
29229 62 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  b;..            
2922a 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0d  pMatch = pItem;.
2922b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63  .            pSc
2922c 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
2922d 68 65 6d 61 3b 0d 0a 20 20 20 20 20 20 20 20 20  hema;..         
2922e 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
2922f 20 74 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75   the rowid (colu
29230 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49  mn -1) for the I
29231 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
29232 45 59 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  EY */..         
29233 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
29234 6e 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  n = j==pTab->iPK
29235 65 79 20 3f 20 2d 31 20 3a 20 28 69 31 36 29 6a  ey ? -1 : (i16)j
29236 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ;..            b
29237 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20  reak;..         
29238 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20   }..        }.. 
29239 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d       }..    }...
2923a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2923b 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20  OMIT_TRIGGER..  
2923c 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20    /* If we have 
2923d 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f  not already reso
2923e 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74  lved the name, t
2923f 68 65 6e 20 6d 61 79 62 65 20 0d 0a 20 20 20 20  hen maybe ..    
29240 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a  ** it is a new.*
29241 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65   or old.* trigge
29242 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72  r argument refer
29243 65 6e 63 65 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ence..    */..  
29244 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20    if( zDb==0 && 
29245 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d  zTab!=0 && cnt==
29246 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72  0 && pParse->pTr
29247 69 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0d 0a  iggerTab!=0 ){..
29248 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70        int op = p
29249 50 61 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f  Parse->eTriggerO
2924a 70 3b 0d 0a 20 20 20 20 20 20 54 61 62 6c 65 20  p;..      Table 
2924b 2a 70 54 61 62 20 3d 20 30 3b 0d 0a 20 20 20 20  *pTab = 0;..    
2924c 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b    assert( op==TK
2924d 5f 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d 54  _DELETE || op==T
2924e 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d  K_UPDATE || op==
2924f 54 4b 5f 49 4e 53 45 52 54 20 29 3b 0d 0a 20 20  TK_INSERT );..  
29250 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 44      if( op!=TK_D
29251 45 4c 45 54 45 20 26 26 20 73 71 6c 69 74 65 33  ELETE && sqlite3
29252 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a 54  StrICmp("new",zT
29253 61 62 29 20 3d 3d 20 30 20 29 7b 0d 0a 20 20 20  ab) == 0 ){..   
29254 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
29255 6c 65 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  le = 1;..       
29256 20 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e   pTab = pParse->
29257 70 54 72 69 67 67 65 72 54 61 62 3b 0d 0a 20 20  pTriggerTab;..  
29258 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
29259 21 3d 54 4b 5f 49 4e 53 45 52 54 20 26 26 20 73  !=TK_INSERT && s
2925a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f  qlite3StrICmp("o
2925b 6c 64 22 2c 7a 54 61 62 29 3d 3d 30 20 29 7b 0d  ld",zTab)==0 ){.
2925c 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
2925d 69 54 61 62 6c 65 20 3d 20 30 3b 0d 0a 20 20 20  iTable = 0;..   
2925e 20 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72       pTab = pPar
2925f 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b  se->pTriggerTab;
29260 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  ..      }....   
29261 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 20 0d     if( pTab ){ .
29262 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f  .        int iCo
29263 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70 53 63 68  l;..        pSch
29264 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
29265 65 6d 61 3b 0d 0a 20 20 20 20 20 20 20 20 63 6e  ema;..        cn
29266 74 54 61 62 2b 2b 3b 0d 0a 20 20 20 20 20 20 20  tTab++;..       
29267 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
29268 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
29269 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  ol++){..        
2926a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
2926b 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
2926c 6c 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  l];..          i
2926d 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2926e 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a  p(pCol->zName, z
2926f 43 6f 6c 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Col)==0 ){..    
29270 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
29271 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
29272 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
29273 69 43 6f 6c 20 3d 20 2d 31 3b 0d 0a 20 20 20 20  iCol = -1;..    
29274 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
29275 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
29276 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
29277 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
29278 66 28 20 69 43 6f 6c 3e 3d 70 54 61 62 2d 3e 6e  f( iCol>=pTab->n
29279 43 6f 6c 20 26 26 20 73 71 6c 69 74 65 33 49 73  Col && sqlite3Is
2927a 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0d 0a  Rowid(zCol) ){..
2927b 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d            iCol =
2927c 20 2d 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 49   -1;        /* I
2927d 4d 50 3a 20 52 2d 34 34 39 31 31 2d 35 35 31 32  MP: R-44911-5512
2927e 34 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7d 0d  4 */..        }.
2927f 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
29280 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0d  l<pTab->nCol ){.
29281 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b  .          cnt++
29282 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
29283 20 69 43 6f 6c 3c 30 20 29 7b 0d 0a 20 20 20 20   iCol<0 ){..    
29284 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61          pExpr->a
29285 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
29286 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0d 0a 20  _AFF_INTEGER;.. 
29287 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
29288 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
29289 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
2928a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43      testcase( iC
2928b 6f 6c 3d 3d 33 31 20 29 3b 0d 0a 20 20 20 20 20  ol==31 );..     
2928c 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
2928d 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0d 0a 20 20   iCol==32 );..  
2928e 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2928f 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 28 69 43  ->oldmask |= (iC
29290 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66 66  ol>=32 ? 0xfffff
29291 66 66 66 20 3a 20 28 28 28 75 33 32 29 31 29 3c  fff : (((u32)1)<
29292 3c 69 43 6f 6c 29 29 3b 0d 0a 20 20 20 20 20 20  <iCol));..      
29293 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
29294 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
29295 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0d 0a 20  ( iCol==31 );.. 
29296 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63             testc
29297 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b  ase( iCol==32 );
29298 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  ..            pP
29299 61 72 73 65 2d 3e 6e 65 77 6d 61 73 6b 20 7c 3d  arse->newmask |=
2929a 20 28 69 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66   (iCol>=32 ? 0xf
2929b 66 66 66 66 66 66 66 20 3a 20 28 28 28 75 33 32  fffffff : (((u32
2929c 29 31 29 3c 3c 69 43 6f 6c 29 29 3b 0d 0a 20 20  )1)<<iCol));..  
2929d 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2929e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
2929f 75 6d 6e 20 3d 20 28 69 31 36 29 69 43 6f 6c 3b  umn = (i16)iCol;
292a0 0d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  ..          pExp
292a1 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0d  r->pTab = pTab;.
292a2 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72 69  .          isTri
292a3 67 67 65 72 20 3d 20 31 3b 0d 0a 20 20 20 20 20  gger = 1;..     
292a4 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
292a5 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20     }..#endif /* 
292a6 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
292a7 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f  OMIT_TRIGGER) */
292a8 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20  ....    /*..    
292a9 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e  ** Perhaps the n
292aa 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 65 6e  ame is a referen
292ab 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0d  ce to the ROWID.
292ac 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28  .    */..    if(
292ad 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61   cnt==0 && cntTa
292ae 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49  b==1 && sqlite3I
292af 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0d  sRowid(zCol) ){.
292b0 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0d  .      cnt = 1;.
292b1 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
292b2 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20  olumn = -1;     
292b3 2f 2a 20 49 4d 50 3a 20 52 2d 34 34 39 31 31 2d  /* IMP: R-44911-
292b4 35 35 31 32 34 20 2a 2f 0d 0a 20 20 20 20 20 20  55124 */..      
292b5 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
292b6 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  = SQLITE_AFF_INT
292b7 45 47 45 52 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  EGER;..    }....
292b8 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a 20 49      /*..    ** I
292b9 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 6f  f the input is o
292ba 66 20 74 68 65 20 66 6f 72 6d 20 5a 20 28 6e 6f  f the form Z (no
292bb 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a 29 20  t Y.Z or X.Y.Z) 
292bc 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20 5a 0d  then the name Z.
292bd 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 72 65  .    ** might re
292be 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75 6c 74  fer to an result
292bf 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54 68 69  -set alias.  Thi
292c0 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20 65  s happens, for e
292c1 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0d 0a 20 20  xample, when..  
292c2 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73 6f    ** we are reso
292c3 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 74  lving names in t
292c4 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
292c5 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
292c6 20 63 6f 6d 6d 61 6e 64 3a 0d 0a 20 20 20 20 2a   command:..    *
292c7 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45  *..    **     SE
292c8 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52  LECT a+b AS x FR
292c9 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78  OM table WHERE x
292ca 3c 31 30 3b 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  <10;..    **..  
292cb 20 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69    ** In cases li
292cc 6b 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65  ke this, replace
292cd 20 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f   pExpr with a co
292ce 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  py of the expres
292cf 73 69 6f 6e 20 74 68 61 74 0d 0a 20 20 20 20 2a  sion that..    *
292d0 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75  * forms the resu
292d1 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28 22 61  lt set entry ("a
292d2 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70  +b" in the examp
292d3 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69  le) and return i
292d4 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 20 20 20  mmediately...   
292d5 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68   ** Note that th
292d6 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  e expression in 
292d7 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 73  the result set s
292d8 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61  hould have alrea
292d9 64 79 20 62 65 65 6e 0d 0a 20 20 20 20 2a 2a 20  dy been..    ** 
292da 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20  resolved by the 
292db 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 63  time the WHERE c
292dc 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65  lause is resolve
292dd 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  d...    */..    
292de 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70  if( cnt==0 && (p
292df 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c  EList = pNC->pEL
292e0 69 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d  ist)!=0 && zTab=
292e1 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 66 6f 72  =0 ){..      for
292e2 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e  (j=0; j<pEList->
292e3 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0d 0a 20 20  nExpr; j++){..  
292e4 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20        char *zAs 
292e5 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a  = pEList->a[j].z
292e6 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20 20 20 69  Name;..        i
292e7 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c  f( zAs!=0 && sql
292e8 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c  ite3StrICmp(zAs,
292e9 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0d 0a 20 20   zCol)==0 ){..  
292ea 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4f          Expr *pO
292eb 72 69 67 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  rig;..          
292ec 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
292ed 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72  Left==0 && pExpr
292ee 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0d 0a  ->pRight==0 );..
292ef 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
292f0 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  ( pExpr->x.pList
292f1 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ==0 );..        
292f2 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
292f3 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  >x.pSelect==0 );
292f4 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69  ..          pOri
292f5 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d  g = pEList->a[j]
292f6 2e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 20  .pExpr;..       
292f7 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c     if( !pNC->all
292f8 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73  owAgg && ExprHas
292f9 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20  Property(pOrig, 
292fa 45 50 5f 41 67 67 29 20 29 7b 0d 0a 20 20 20 20  EP_Agg) ){..    
292fb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
292fc 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
292fd 22 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73  "misuse of alias
292fe 65 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22  ed aggregate %s"
292ff 2c 20 7a 41 73 29 3b 0d 0a 20 20 20 20 20 20 20  , zAs);..       
29300 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
29301 41 62 6f 72 74 3b 0d 0a 20 20 20 20 20 20 20 20  Abort;..        
29302 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 72    }..          r
29303 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72  esolveAlias(pPar
29304 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70  se, pEList, j, p
29305 45 78 70 72 2c 20 22 22 29 3b 0d 0a 20 20 20 20  Expr, "");..    
29306 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0d 0a        cnt = 1;..
29307 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
29308 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 0;..         
29309 20 61 73 73 65 72 74 28 20 7a 54 61 62 3d 3d 30   assert( zTab==0
2930a 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b 0d 0a 20   && zDb==0 );.. 
2930b 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6c 6f           goto lo
2930c 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0d 0a 20  okupname_end;.. 
2930d 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
2930e 7d 20 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  } ..    }....   
2930f 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74   /* Advance to t
29310 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e  he next name con
29311 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20  text.  The loop 
29312 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65  will exit when e
29313 69 74 68 65 72 0d 0a 20 20 20 20 2a 2a 20 77 65  ither..    ** we
29314 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 63   have a match (c
29315 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65  nt>0) or when we
29316 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65   run out of name
29317 20 63 6f 6e 74 65 78 74 73 2e 0d 0a 20 20 20 20   contexts...    
29318 2a 2f 0d 0a 20 20 20 20 69 66 28 20 63 6e 74 3d  */..    if( cnt=
29319 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 4e 43  =0 ){..      pNC
2931a 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0d 0a   = pNC->pNext;..
2931b 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
2931c 2f 2a 0d 0a 20 20 2a 2a 20 49 66 20 58 20 61 6e  /*..  ** If X an
2931d 64 20 59 20 61 72 65 20 4e 55 4c 4c 20 28 69 6e  d Y are NULL (in
2931e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20   other words if 
2931f 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  only the column 
29320 6e 61 6d 65 20 5a 20 69 73 0d 0a 20 20 2a 2a 20  name Z is..  ** 
29321 73 75 70 70 6c 69 65 64 29 20 61 6e 64 20 74 68  supplied) and th
29322 65 20 76 61 6c 75 65 20 6f 66 20 5a 20 69 73 20  e value of Z is 
29323 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64 6f 75 62  enclosed in doub
29324 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68 65 6e 0d  le-quotes, then.
29325 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20 73 74 72  .  ** Z is a str
29326 69 6e 67 20 6c 69 74 65 72 61 6c 20 69 66 20 69  ing literal if i
29327 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20  t doesn't match 
29328 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  any column names
29329 2e 20 20 49 6e 20 74 68 61 74 0d 0a 20 20 2a 2a  .  In that..  **
2932a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64 20 74   case, we need t
2932b 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61  o return right a
2932c 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b 65  way and not make
2932d 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 0d   any changes to.
2932e 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0d 0a 20 20  .  ** pExpr...  
2932f 2a 2a 0d 0a 20 20 2a 2a 20 42 65 63 61 75 73 65  **..  ** Because
29330 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61   no reference wa
29331 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20  s made to outer 
29332 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e  contexts, the pN
29333 43 2d 3e 6e 52 65 66 0d 0a 20 20 2a 2a 20 66 69  C->nRef..  ** fi
29334 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61  elds are not cha
29335 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74  nged in any cont
29336 65 78 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ext...  */..  if
29337 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62  ( cnt==0 && zTab
29338 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50 72  ==0 && ExprHasPr
29339 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f  operty(pExpr,EP_
2933a 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0d 0a 20  DblQuoted) ){.. 
2933b 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
2933c 4b 5f 53 54 52 49 4e 47 3b 0d 0a 20 20 20 20 70  K_STRING;..    p
2933d 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0d  Expr->pTab = 0;.
2933e 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
2933f 50 72 75 6e 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Prune;..  }.... 
29340 20 2f 2a 0d 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30   /*..  ** cnt==0
29341 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73   means there was
29342 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74   not match.  cnt
29343 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77  >1 means there w
29344 65 72 65 20 74 77 6f 20 6f 72 0d 0a 20 20 2a 2a  ere two or..  **
29345 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20   more matches.  
29346 45 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 68  Either way, we h
29347 61 76 65 20 61 6e 20 65 72 72 6f 72 2e 0d 0a 20  ave an error... 
29348 20 2a 2f 0d 0a 20 20 69 66 28 20 63 6e 74 21 3d   */..  if( cnt!=
29349 31 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  1 ){..    const 
2934a 63 68 61 72 20 2a 7a 45 72 72 3b 0d 0a 20 20 20  char *zErr;..   
2934b 20 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f   zErr = cnt==0 ?
2934c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e   "no such column
2934d 22 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 63  " : "ambiguous c
2934e 6f 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0d 0a 20 20  olumn name";..  
2934f 20 20 69 66 28 20 7a 44 62 20 29 7b 0d 0a 20 20    if( zDb ){..  
29350 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
29351 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a  Msg(pParse, "%s:
29352 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72   %s.%s.%s", zErr
29353 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f  , zDb, zTab, zCo
29354 6c 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69  l);..    }else i
29355 66 28 20 7a 54 61 62 20 29 7b 0d 0a 20 20 20 20  f( zTab ){..    
29356 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
29357 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25  g(pParse, "%s: %
29358 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54 61  s.%s", zErr, zTa
29359 62 2c 20 7a 43 6f 6c 29 3b 0d 0a 20 20 20 20 7d  b, zCol);..    }
2935a 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c  else{..      sql
2935b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2935c 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a  rse, "%s: %s", z
2935d 45 72 72 2c 20 7a 43 6f 6c 29 3b 0d 0a 20 20 20  Err, zCol);..   
2935e 20 7d 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   }..    pParse->
2935f 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b  checkSchema = 1;
29360 0d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45  ..    pTopNC->nE
29361 72 72 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rr++;..  }....  
29362 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66  /* If a column f
29363 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 70  rom a table in p
29364 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 65 72  SrcList is refer
29365 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63 6f  enced, then reco
29366 72 64 0d 0a 20 20 2a 2a 20 74 68 69 73 20 66 61  rd..  ** this fa
29367 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69  ct in the pSrcLi
29368 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62  st.a[].colUsed b
29369 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20  itmask.  Column 
2936a 30 20 63 61 75 73 65 73 0d 0a 20 20 2a 2a 20 62  0 causes..  ** b
2936b 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20  it 0 to be set. 
2936c 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62   Column 1 sets b
2936d 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f  it 1.  And so fo
2936e 72 74 68 2e 20 20 49 66 20 74 68 65 0d 0a 20 20  rth.  If the..  
2936f 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ** column number
29370 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
29371 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
29372 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61  its in the bitma
29373 73 6b 0d 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65  sk..  ** then se
29374 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72  t the high-order
29375 20 62 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d   bit of the bitm
29376 61 73 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ask...  */..  if
29377 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
29378 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30  >=0 && pMatch!=0
29379 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 20 3d   ){..    int n =
2937a 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
2937b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
2937c 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0d 0a 20 20 20  n==BMS-1 );..   
2937d 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b 0d 0a   if( n>=BMS ){..
2937e 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31 3b        n = BMS-1;
2937f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73  ..    }..    ass
29380 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75  ert( pMatch->iCu
29381 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61  rsor==pExpr->iTa
29382 62 6c 65 20 29 3b 0d 0a 20 20 20 20 70 4d 61 74  ble );..    pMat
29383 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28  ch->colUsed |= (
29384 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0d  (Bitmask)1)<<n;.
29385 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6c 65  .  }....  /* Cle
29386 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  an up and return
29387 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
29388 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
29389 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0d 0a  pExpr->pLeft);..
2938a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d    pExpr->pLeft =
2938b 20 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78   0;..  sqlite3Ex
2938c 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78  prDelete(db, pEx
2938d 70 72 2d 3e 70 52 69 67 68 74 29 3b 0d 0a 20 20  pr->pRight);..  
2938e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20  pExpr->pRight = 
2938f 30 3b 0d 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20  0;..  pExpr->op 
29390 3d 20 28 69 73 54 72 69 67 67 65 72 20 3f 20 54  = (isTrigger ? T
29391 4b 5f 54 52 49 47 47 45 52 20 3a 20 54 4b 5f 43  K_TRIGGER : TK_C
29392 4f 4c 55 4d 4e 29 3b 0d 0a 6c 6f 6f 6b 75 70 6e  OLUMN);..lookupn
29393 61 6d 65 5f 65 6e 64 3a 0d 0a 20 20 69 66 28 20  ame_end:..  if( 
29394 63 6e 74 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 61  cnt==1 ){..    a
29395 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b  ssert( pNC!=0 );
29396 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  ..    sqlite3Aut
29397 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 45  hRead(pParse, pE
29398 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 4e  xpr, pSchema, pN
29399 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0d 0a 20  C->pSrcList);.. 
2939a 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
2939b 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f  the nRef value o
2939c 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65  n all name conte
2939d 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75  xts from TopNC u
2939e 70 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 74 68 65  p to..    ** the
2939f 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65   point where the
293a0 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a   name matched. *
293a1 2f 0d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0d  /..    for(;;){.
293a2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
293a3 54 6f 70 4e 43 21 3d 30 20 29 3b 0d 0a 20 20 20  TopNC!=0 );..   
293a4 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b     pTopNC->nRef+
293a5 2b 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54  +;..      if( pT
293a6 6f 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61  opNC==pNC ) brea
293a7 6b 3b 0d 0a 20 20 20 20 20 20 70 54 6f 70 4e 43  k;..      pTopNC
293a8 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74   = pTopNC->pNext
293a9 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65  ;..    }..    re
293aa 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0d  turn WRC_Prune;.
293ab 0a 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20  .  } else {..   
293ac 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
293ad 74 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  t;..  }..}..../*
293ae 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e  ..** Allocate an
293af 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
293b0 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73  er to an express
293b1 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74 68 65 20  ion to load the 
293b2 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0d 0a 2a 2a 20  column iCol..** 
293b3 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63 65 20  from datasource 
293b4 69 53 72 63 20 69 6e 20 53 72 63 4c 69 73 74 20  iSrc in SrcList 
293b5 70 53 72 63 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  pSrc...*/..SQLIT
293b6 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
293b7 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c  sqlite3CreateCol
293b8 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20  umnExpr(sqlite3 
293b9 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 53  *db, SrcList *pS
293ba 72 63 2c 20 69 6e 74 20 69 53 72 63 2c 20 69 6e  rc, int iSrc, in
293bb 74 20 69 43 6f 6c 29 7b 0d 0a 20 20 45 78 70 72  t iCol){..  Expr
293bc 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70   *p = sqlite3Exp
293bd 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 43 4f  rAlloc(db, TK_CO
293be 4c 55 4d 4e 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  LUMN, 0, 0);..  
293bf 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 73 74  if( p ){..    st
293c0 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
293c1 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63  m *pItem = &pSrc
293c2 2d 3e 61 5b 69 53 72 63 5d 3b 0d 0a 20 20 20 20  ->a[iSrc];..    
293c3 70 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d  p->pTab = pItem-
293c4 3e 70 54 61 62 3b 0d 0a 20 20 20 20 70 2d 3e 69  >pTab;..    p->i
293c5 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
293c6 43 75 72 73 6f 72 3b 0d 0a 20 20 20 20 69 66 28  Cursor;..    if(
293c7 20 70 2d 3e 70 54 61 62 2d 3e 69 50 4b 65 79 3d   p->pTab->iPKey=
293c8 3d 69 43 6f 6c 20 29 7b 0d 0a 20 20 20 20 20 20  =iCol ){..      
293c9 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  p->iColumn = -1;
293ca 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
293cb 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d      p->iColumn =
293cc 20 28 79 6e 56 61 72 29 69 43 6f 6c 3b 0d 0a 20   (ynVar)iCol;.. 
293cd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
293ce 43 6f 6c 3d 3d 42 4d 53 20 29 3b 0d 0a 20 20 20  Col==BMS );..   
293cf 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f     testcase( iCo
293d0 6c 3d 3d 42 4d 53 2d 31 20 29 3b 0d 0a 20 20 20  l==BMS-1 );..   
293d1 20 20 20 70 49 74 65 6d 2d 3e 63 6f 6c 55 73 65     pItem->colUse
293d2 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31  d |= ((Bitmask)1
293d3 29 3c 3c 28 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20  )<<(iCol>=BMS ? 
293d4 42 4d 53 2d 31 20 3a 20 69 43 6f 6c 29 3b 0d 0a  BMS-1 : iCol);..
293d5 20 20 20 20 7d 0d 0a 20 20 20 20 45 78 70 72 53      }..    ExprS
293d6 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  etProperty(p, EP
293d7 5f 52 65 73 6f 6c 76 65 64 29 3b 0d 0a 20 20 7d  _Resolved);..  }
293d8 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d 0a 7d  ..  return p;..}
293d9 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
293da 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 62  routine is callb
293db 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 57  ack for sqlite3W
293dc 61 6c 6b 45 78 70 72 28 29 2e 0d 0a 2a 2a 0d 0a  alkExpr()...**..
293dd 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f  ** Resolve symbo
293de 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54  lic names into T
293df 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f  K_COLUMN operato
293e0 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  rs for the curre
293e1 6e 74 0d 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74  nt..** node in t
293e2 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  he expression tr
293e3 65 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f  ee.  Return 0 to
293e4 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65   continue the se
293e5 61 72 63 68 20 64 6f 77 6e 0d 0a 2a 2a 20 74 68  arch down..** th
293e6 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61  e tree or 2 to a
293e7 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 77 61  bort the tree wa
293e8 6c 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  lk...**..** This
293e9 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
293ea 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  es error checkin
293eb 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c  g and name resol
293ec 75 74 69 6f 6e 20 66 6f 72 0d 0a 2a 2a 20 66 75  ution for..** fu
293ed 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54  nction names.  T
293ee 68 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20  he operator for 
293ef 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
293f0 6f 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0d 0a  ons is changed..
293f1 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e  ** to TK_AGG_FUN
293f2 43 54 49 4f 4e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  CTION...*/..stat
293f3 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78  ic int resolveEx
293f4 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70  prStep(Walker *p
293f5 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
293f6 78 70 72 29 7b 0d 0a 20 20 4e 61 6d 65 43 6f 6e  xpr){..  NameCon
293f7 74 65 78 74 20 2a 70 4e 43 3b 0d 0a 20 20 50 61  text *pNC;..  Pa
293f8 72 73 65 20 2a 70 50 61 72 73 65 3b 0d 0a 0d 0a  rse *pParse;....
293f9 20 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d    pNC = pWalker-
293fa 3e 75 2e 70 4e 43 3b 0d 0a 20 20 61 73 73 65 72  >u.pNC;..  asser
293fb 74 28 20 70 4e 43 21 3d 30 20 29 3b 0d 0a 20 20  t( pNC!=0 );..  
293fc 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
293fd 61 72 73 65 3b 0d 0a 20 20 61 73 73 65 72 74 28  arse;..  assert(
293fe 20 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 72   pParse==pWalker
293ff 2d 3e 70 50 61 72 73 65 20 29 3b 0d 0a 0d 0a 20  ->pParse );.... 
29400 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50   if( ExprHasAnyP
29401 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
29402 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65  P_Resolved) ) re
29403 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0d  turn WRC_Prune;.
29404 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72  .  ExprSetProper
29405 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73  ty(pExpr, EP_Res
29406 6f 6c 76 65 64 29 3b 0d 0a 23 69 66 6e 64 65 66  olved);..#ifndef
29407 20 4e 44 45 42 55 47 0d 0a 20 20 69 66 28 20 70   NDEBUG..  if( p
29408 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20  NC->pSrcList && 
29409 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e  pNC->pSrcList->n
2940a 41 6c 6c 6f 63 3e 30 20 29 7b 0d 0a 20 20 20 20  Alloc>0 ){..    
2940b 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
2940c 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
2940d 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a  t;..    int i;..
2940e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2940f 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53  NC->pSrcList->nS
29410 72 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  rc; i++){..     
29411 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73   assert( pSrcLis
29412 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e  t->a[i].iCursor>
29413 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e  =0 && pSrcList->
29414 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61  a[i].iCursor<pPa
29415 72 73 65 2d 3e 6e 54 61 62 29 3b 0d 0a 20 20 20  rse->nTab);..   
29416 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d   }..  }..#endif.
29417 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72  .  switch( pExpr
29418 2d 3e 6f 70 20 29 7b 0d 0a 0d 0a 23 69 66 20 64  ->op ){....#if d
29419 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
2941a 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45  ABLE_UPDATE_DELE
2941b 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65  TE_LIMIT) && !de
2941c 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2941d 54 5f 53 55 42 51 55 45 52 59 29 0d 0a 20 20 20  T_SUBQUERY)..   
2941e 20 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c 20   /* The special 
2941f 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 20  operator TK_ROW 
29420 6d 65 61 6e 73 20 75 73 65 20 74 68 65 20 72 6f  means use the ro
29421 77 69 64 20 66 6f 72 20 74 68 65 20 66 69 72 73  wid for the firs
29422 74 0d 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  t..    ** column
29423 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
29424 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 75 73  use.  This is us
29425 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20  ed by the LIMIT 
29426 61 6e 64 20 4f 52 44 45 52 20 42 59 0d 0a 20 20  and ORDER BY..  
29427 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f 63    ** clause proc
29428 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 45  essing on UPDATE
29429 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 74   and DELETE stat
2942a 65 6d 65 6e 74 73 2e 0d 0a 20 20 20 20 2a 2f 0d  ements...    */.
2942b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f 57  .    case TK_ROW
2942c 3a 20 7b 0d 0a 20 20 20 20 20 20 53 72 63 4c 69  : {..      SrcLi
2942d 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70  st *pSrcList = p
2942e 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0d 0a 20  NC->pSrcList;.. 
2942f 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
29430 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
29431 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
29432 70 53 72 63 4c 69 73 74 20 26 26 20 70 53 72 63  pSrcList && pSrc
29433 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  List->nSrc==1 );
29434 0d 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d 20  ..      pItem = 
29435 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0d 0a 20  pSrcList->a; .. 
29436 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
29437 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0d 0a 20 20 20   TK_COLUMN;..   
29438 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d     pExpr->pTab =
29439 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0d 0a 20   pItem->pTab;.. 
2943a 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
2943b 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72  le = pItem->iCur
2943c 73 6f 72 3b 0d 0a 20 20 20 20 20 20 70 45 78 70  sor;..      pExp
2943d 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  r->iColumn = -1;
2943e 0d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61  ..      pExpr->a
2943f 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
29440 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0d 0a 20  _AFF_INTEGER;.. 
29441 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
29442 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65   }..#endif /* de
29443 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
29444 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
29445 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
29446 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
29447 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0d 0a 0d  _SUBQUERY) */...
29448 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69  .    /* A lone i
29449 64 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65  dentifier is the
2944a 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d   name of a colum
2944b 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  n...    */..    
2944c 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0d 0a 20  case TK_ID: {.. 
2944d 20 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b       return look
2944e 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30  upName(pParse, 0
2944f 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54  , 0, pExpr->u.zT
29450 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72  oken, pNC, pExpr
29451 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20  );..    }..  .. 
29452 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61     /* A table na
29453 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61  me and column na
29454 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0d 0a 20  me:     ID.ID.. 
29455 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62     ** Or a datab
29456 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63  ase, table and c
29457 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44  olumn:  ID.ID.ID
29458 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61  ..    */..    ca
29459 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0d 0a 20 20  se TK_DOT: {..  
2945a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2945b 7a 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20  zColumn;..      
2945c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
2945d 6c 65 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74  le;..      const
2945e 20 63 68 61 72 20 2a 7a 44 62 3b 0d 0a 20 20 20   char *zDb;..   
2945f 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b     Expr *pRight;
29460 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 69 66 28  ....      /* if(
29461 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62   pSrcList==0 ) b
29462 72 65 61 6b 3b 20 2a 2f 0d 0a 20 20 20 20 20 20  reak; */..      
29463 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
29464 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 69  pRight;..      i
29465 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54  f( pRight->op==T
29466 4b 5f 49 44 20 29 7b 0d 0a 20 20 20 20 20 20 20  K_ID ){..       
29467 20 7a 44 62 20 3d 20 30 3b 0d 0a 20 20 20 20 20   zDb = 0;..     
29468 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 45 78 70     zTable = pExp
29469 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b  r->pLeft->u.zTok
2946a 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20 7a 43 6f  en;..        zCo
2946b 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d 3e 75  lumn = pRight->u
2946c 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 20 20  .zToken;..      
2946d 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2946e 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e  assert( pRight->
2946f 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0d 0a 20  op==TK_DOT );.. 
29470 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 45 78         zDb = pEx
29471 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f  pr->pLeft->u.zTo
29472 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20 7a 54  ken;..        zT
29473 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e 70  able = pRight->p
29474 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d  Left->u.zToken;.
29475 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e  .        zColumn
29476 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68   = pRight->pRigh
29477 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20  t->u.zToken;..  
29478 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74      }..      ret
29479 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70  urn lookupName(p
2947a 50 61 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62  Parse, zDb, zTab
2947b 6c 65 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43  le, zColumn, pNC
2947c 2c 20 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 7d  , pExpr);..    }
2947d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c  ....    /* Resol
2947e 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  ve function name
2947f 73 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  s..    */..    c
29480 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  ase TK_CONST_FUN
29481 43 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  C:..    case TK_
29482 46 55 4e 43 54 49 4f 4e 3a 20 7b 0d 0a 20 20 20  FUNCTION: {..   
29483 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
29484 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
29485 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 61  ist;    /* The a
29486 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0d  rgument list */.
29487 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  .      int n = p
29488 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
29489 78 70 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e  xpr : 0;    /* N
2948a 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2948b 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74  ts */..      int
2948c 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20   no_such_func = 
2948d 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  0;       /* True
2948e 20 69 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63   if no such func
2948f 74 69 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0d 0a  tion exists */..
29490 20 20 20 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f        int wrong_
29491 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b 20 20 20  num_args = 0;   
29492 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 6f    /* True if wro
29493 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ng number of arg
29494 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20 20 20 20  uments */..     
29495 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20 30 3b   int is_agg = 0;
29496 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29497 54 72 75 65 20 69 66 20 69 73 20 61 6e 20 61 67  True if is an ag
29498 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
29499 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 61   */..      int a
2949a 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  uth;            
2949b 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f 72         /* Author
2949c 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20 74  ization to use t
2949d 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a  he function */..
2949e 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20        int nId;  
2949f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
294a1 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e  haracters in fun
294a2 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0d 0a 20  ction name */.. 
294a3 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
294a4 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20  *zId;           
294a5 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
294a6 20 6e 61 6d 65 2e 20 2a 2f 0d 0a 20 20 20 20 20   name. */..     
294a7 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20   FuncDef *pDef; 
294a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
294a9 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
294aa 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  t the function *
294ab 2f 0d 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20  /..      u8 enc 
294ac 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62  = ENC(pParse->db
294ad 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  );   /* The data
294ae 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  base encoding */
294af 0d 0a 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  ....      testca
294b0 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
294b1 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0d  K_CONST_FUNC );.
294b2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
294b3 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
294b4 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
294b5 65 63 74 29 20 29 3b 0d 0a 20 20 20 20 20 20 7a  ect) );..      z
294b6 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54  Id = pExpr->u.zT
294b7 6f 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 6e 49 64  oken;..      nId
294b8 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
294b9 33 30 28 7a 49 64 29 3b 0d 0a 20 20 20 20 20 20  30(zId);..      
294ba 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
294bb 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
294bc 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  e->db, zId, nId,
294bd 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0d 0a 20 20   n, enc, 0);..  
294be 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20      if( pDef==0 
294bf 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 44 65 66  ){..        pDef
294c0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
294c1 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64  nction(pParse->d
294c2 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c  b, zId, nId, -1,
294c3 20 65 6e 63 2c 20 30 29 3b 0d 0a 20 20 20 20 20   enc, 0);..     
294c4 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29     if( pDef==0 )
294c5 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 5f  {..          no_
294c6 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b 0d 0a  such_func = 1;..
294c7 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
294c8 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f            wrong_
294c9 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0d 0a 20  num_args = 1;.. 
294ca 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
294cb 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
294cc 69 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78  is_agg = pDef->x
294cd 46 75 6e 63 3d 3d 30 3b 0d 0a 20 20 20 20 20 20  Func==0;..      
294ce 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
294cf 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
294d0 54 49 4f 4e 0d 0a 20 20 20 20 20 20 69 66 28 20  TION..      if( 
294d1 70 44 65 66 20 29 7b 0d 0a 20 20 20 20 20 20 20  pDef ){..       
294d2 20 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41   auth = sqlite3A
294d3 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
294d4 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e   SQLITE_FUNCTION
294d5 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65  , 0, pDef->zName
294d6 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  , 0);..        i
294d7 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f  f( auth!=SQLITE_
294d8 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
294d9 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 54   if( auth==SQLIT
294da 45 5f 44 45 4e 59 20 29 7b 0d 0a 20 20 20 20 20  E_DENY ){..     
294db 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
294dc 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
294dd 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74  not authorized t
294de 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20  o use function: 
294df 25 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  %s",..          
294e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294e1 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e            pDef->
294e2 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 20  zName);..       
294e3 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b       pNC->nErr++
294e4 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
294e5 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
294e6 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0d 0a  >op = TK_NULL;..
294e7 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
294e8 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a 20 20 20   WRC_Prune;..   
294e9 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
294ea 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 69  .#endif..      i
294eb 66 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e  f( is_agg && !pN
294ec 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0d 0a  C->allowAgg ){..
294ed 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
294ee 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
294ef 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 72 65  "misuse of aggre
294f0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e  gate function %.
294f1 2a 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b  *s()", nId,zId);
294f2 0d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e  ..        pNC->n
294f3 45 72 72 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20  Err++;..        
294f4 69 73 5f 61 67 67 20 3d 20 30 3b 0d 0a 20 20 20  is_agg = 0;..   
294f5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f 5f     }else if( no_
294f6 73 75 63 68 5f 66 75 6e 63 20 29 7b 0d 0a 20 20  such_func ){..  
294f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
294f8 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
294f9 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a  o such function:
294fa 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64   %.*s", nId, zId
294fb 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d  );..        pNC-
294fc 3e 6e 45 72 72 2b 2b 3b 0d 0a 20 20 20 20 20 20  >nErr++;..      
294fd 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f  }else if( wrong_
294fe 6e 75 6d 5f 61 72 67 73 20 29 7b 0d 0a 20 20 20  num_args ){..   
294ff 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
29500 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 6f  rMsg(pParse,"wro
29501 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ng number of arg
29502 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 69  uments to functi
29503 6f 6e 20 25 2e 2a 73 28 29 22 2c 0d 0a 20 20 20  on %.*s()",..   
29504 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a            nId, z
29505 49 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 4e  Id);..        pN
29506 43 2d 3e 6e 45 72 72 2b 2b 3b 0d 0a 20 20 20 20  C->nErr++;..    
29507 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 69    }..      if( i
29508 73 5f 61 67 67 20 29 7b 0d 0a 20 20 20 20 20 20  s_agg ){..      
29509 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
2950a 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0d 0a  _AGG_FUNCTION;..
2950b 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73          pNC->has
2950c 41 67 67 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  Agg = 1;..      
2950d 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f  }..      if( is_
2950e 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77  agg ) pNC->allow
2950f 41 67 67 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  Agg = 0;..      
29510 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c  sqlite3WalkExprL
29511 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69  ist(pWalker, pLi
29512 73 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  st);..      if( 
29513 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c  is_agg ) pNC->al
29514 6c 6f 77 41 67 67 20 3d 20 31 3b 0d 0a 20 20 20  lowAgg = 1;..   
29515 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43     /* FIX ME:  C
29516 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66  ompute pExpr->af
29517 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20  finity based on 
29518 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65 74  the expected ret
29519 75 72 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 74 79  urn..      ** ty
2951a 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  pe of the functi
2951b 6f 6e 20 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  on ..      */.. 
2951c 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
2951d 50 72 75 6e 65 3b 0d 0a 20 20 20 20 7d 0d 0a 23  Prune;..    }..#
2951e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2951f 49 54 5f 53 55 42 51 55 45 52 59 0d 0a 20 20 20  IT_SUBQUERY..   
29520 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
29521 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  ..    case TK_EX
29522 49 53 54 53 3a 20 20 74 65 73 74 63 61 73 65 28  ISTS:  testcase(
29523 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45   pExpr->op==TK_E
29524 58 49 53 54 53 20 29 3b 0d 0a 23 65 6e 64 69 66  XISTS );..#endif
29525 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  ..    case TK_IN
29526 3a 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63  : {..      testc
29527 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
29528 54 4b 5f 49 4e 20 29 3b 0d 0a 20 20 20 20 20 20  TK_IN );..      
29529 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2952a 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2952b 73 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20 20 20  sSelect) ){..   
2952c 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20       int nRef = 
2952d 70 4e 43 2d 3e 6e 52 65 66 3b 0d 0a 23 69 66 6e  pNC->nRef;..#ifn
2952e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2952f 43 48 45 43 4b 0d 0a 20 20 20 20 20 20 20 20 69  CHECK..        i
29530 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20  f( pNC->isCheck 
29531 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71  ){..          sq
29532 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29533 61 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73  arse,"subqueries
29534 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43   prohibited in C
29535 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  HECK constraints
29536 22 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ");..        }..
29537 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 20 20  #endif..        
29538 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
29539 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72  t(pWalker, pExpr
2953a 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0d 0a 20  ->x.pSelect);.. 
2953b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2953c 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29  NC->nRef>=nRef )
2953d 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  ;..        if( n
2953e 52 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29  Ref!=pNC->nRef )
2953f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  {..          Exp
29540 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
29541 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74  pr, EP_VarSelect
29542 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
29543 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72       }..      br
29544 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 69 66  eak;..    }..#if
29545 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29546 5f 43 48 45 43 4b 0d 0a 20 20 20 20 63 61 73 65  _CHECK..    case
29547 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0d   TK_VARIABLE: {.
29548 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e  .      if( pNC->
29549 69 73 43 68 65 63 6b 20 29 7b 0d 0a 20 20 20 20  isCheck ){..    
2954a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2954b 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61  Msg(pParse,"para
2954c 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65  meters prohibite
2954d 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74  d in CHECK const
2954e 72 61 69 6e 74 73 22 29 3b 0d 0a 20 20 20 20 20  raints");..     
2954f 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   }..      break;
29550 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
29551 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 28  .  }..  return (
29552 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
29553 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
29554 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f  ocFailed) ? WRC_
29555 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74  Abort : WRC_Cont
29556 69 6e 75 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  inue;..}..../*..
29557 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 61 20 6c  ** pEList is a l
29558 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
29559 6e 73 20 77 68 69 63 68 20 61 72 65 20 72 65 61  ns which are rea
2955a 6c 6c 79 20 74 68 65 20 72 65 73 75 6c 74 20 73  lly the result s
2955b 65 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 61 20  et of the..** a 
2955c 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2955d 2e 20 20 70 45 20 69 73 20 61 20 74 65 72 6d 20  .  pE is a term 
2955e 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  in an ORDER BY o
2955f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
29560 65 2e 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  e...** This rout
29561 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ine checks to se
29562 65 20 69 66 20 70 45 20 69 73 20 61 20 73 69 6d  e if pE is a sim
29563 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20 77  ple identifier w
29564 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73  hich corresponds
29565 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d 6e  ..** to the AS-n
29566 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68  ame of one of th
29567 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 65  e terms of the e
29568 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20  xpression list. 
29569 20 49 66 20 69 74 20 69 73 2c 0d 0a 2a 2a 20 74   If it is,..** t
2956a 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2956b 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65  rn an integer be
2956c 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68  tween 1 and N wh
2956d 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
2956e 62 65 72 20 6f 66 0d 0a 2a 2a 20 65 6c 65 6d 65  ber of..** eleme
2956f 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63  nts in pEList, c
29570 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
29571 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74  the matching ent
29572 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ry.  If there is
29573 0d 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f  ..** no match, o
29574 72 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61  r if pE is not a
29575 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69   simple identifi
29576 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  er, then this ro
29577 75 74 69 6e 65 0d 0a 2a 2a 20 72 65 74 75 72 6e  utine..** return
29578 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70 45 4c 69   0...**..** pELi
29579 73 74 20 68 61 73 20 62 65 65 6e 20 72 65 73 6f  st has been reso
2957a 6c 76 65 64 2e 20 20 70 45 20 68 61 73 20 6e 6f  lved.  pE has no
2957b 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  t...*/..static i
2957c 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65  nt resolveAsName
2957d 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
2957e 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
2957f 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65  ng context for e
29580 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
29581 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
29582 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f  List,  /* List o
29583 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f  f expressions to
29584 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 45 78 70 72   scan */..  Expr
29585 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f   *pE           /
29586 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20  * Expression we 
29587 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61  are trying to ma
29588 74 63 68 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  tch */..){..  in
29589 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2958a 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2958b 20 2a 2f 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f   */....  UNUSED_
2958c 50 41 52 41 4d 45 54 45 52 28 70 50 61 72 73 65  PARAMETER(pParse
2958d 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 45 2d 3e  );....  if( pE->
2958e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0d 0a 20 20  op==TK_ID ){..  
2958f 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70    char *zCol = p
29590 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20  E->u.zToken;..  
29591 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
29592 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
29593 7b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  {..      char *z
29594 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  As = pEList->a[i
29595 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20  ].zName;..      
29596 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71  if( zAs!=0 && sq
29597 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73  lite3StrICmp(zAs
29598 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0d 0a 20  , zCol)==0 ){.. 
29599 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
2959a 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  1;..      }..   
2959b 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
2959c 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n 0;..}..../*..*
2959d 2a 20 70 45 20 69 73 20 61 20 70 6f 69 6e 74 65  * pE is a pointe
2959e 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69  r to an expressi
2959f 6f 6e 20 77 68 69 63 68 20 69 73 20 61 20 73 69  on which is a si
295a0 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65  ngle term in the
295a1 0d 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 6f 66  ..** ORDER BY of
295a2 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
295a3 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 73 73  CT.  The express
295a4 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ion has not been
295a5 0d 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 76  ..** name resolv
295a6 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 74 20 74  ed...**..** At t
295a7 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f  he point this ro
295a8 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
295a9 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77   we already know
295aa 20 74 68 61 74 20 74 68 65 0d 0a 2a 2a 20 4f 52   that the..** OR
295ab 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e  DER BY term is n
295ac 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e  ot an integer in
295ad 64 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73  dex into the res
295ae 75 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0d 0a  ult set.  That..
295af 2a 2a 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c  ** case is handl
295b0 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
295b1 67 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 0d 0a  g routine...**..
295b2 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6d 61  ** Attempt to ma
295b3 74 63 68 20 70 45 20 61 67 61 69 6e 73 74 20 72  tch pE against r
295b4 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
295b5 73 20 69 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f  s in the left-mo
295b6 73 74 0d 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74  st..** SELECT st
295b7 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e  atement.  Return
295b8 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f 66 20   the index i of 
295b9 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c  the matching col
295ba 75 6d 6e 2c 0d 0a 2a 2a 20 61 73 20 61 6e 20 69  umn,..** as an i
295bb 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 65  ndication to the
295bc 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20   caller that it 
295bd 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 74  should sort by t
295be 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0d  he i-th column..
295bf 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73  .** The left-mos
295c0 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20  t column is 1.  
295c1 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
295c2 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
295c3 65 64 20 69 73 20 74 68 65 0d 0a 2a 2a 20 73 61  ed is the..** sa
295c4 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  me integer value
295c5 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75   that would be u
295c6 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73  sed in the SQL s
295c7 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69  tatement to indi
295c8 63 61 74 65 0d 0a 2a 2a 20 74 68 65 20 63 6f 6c  cate..** the col
295c9 75 6d 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  umn...**..** If 
295ca 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
295cb 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65  h, return 0.  Re
295cc 74 75 72 6e 20 2d 31 20 69 66 20 61 6e 20 65 72  turn -1 if an er
295cd 72 6f 72 20 6f 63 63 75 72 73 2e 0d 0a 2a 2f 0d  ror occurs...*/.
295ce 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
295cf 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f  lveOrderByTermTo
295d0 45 78 70 72 4c 69 73 74 28 0d 0a 20 20 50 61 72  ExprList(..  Par
295d1 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
295d2 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
295d3 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73  xt for error mes
295d4 73 61 67 65 73 20 2a 2f 0d 0a 20 20 53 65 6c 65  sages */..  Sele
295d5 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f  ct *pSelect,   /
295d6 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
295d7 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20  tement with the 
295d8 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
295d9 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45 20 20  */..  Expr *pE  
295da 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
295db 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42  specific ORDER B
295dc 59 20 74 65 72 6d 20 2a 2f 0d 0a 29 7b 0d 0a 20  Y term */..){.. 
295dd 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
295de 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
295df 74 65 72 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69  ter */..  ExprLi
295e0 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20  st *pEList;  /* 
295e1 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  The columns of t
295e2 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
295e3 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ..  NameContext 
295e4 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63  nc;    /* Name c
295e5 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 73 6f 6c  ontext for resol
295e6 76 69 6e 67 20 70 45 20 2a 2f 0d 0a 20 20 73 71  ving pE */..  sq
295e7 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
295e8 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
295e9 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e  nection */..  in
295ea 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
295eb 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
295ec 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72  from subprocedur
295ed 65 73 20 2a 2f 0d 0a 20 20 75 38 20 73 61 76 65  es */..  u8 save
295ee 64 53 75 70 70 45 72 72 3b 20 20 20 2f 2a 20 53  dSuppErr;   /* S
295ef 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62  aved value of db
295f0 2d 3e 73 75 70 70 72 65 73 73 45 72 72 20 2a 2f  ->suppressErr */
295f1 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ....  assert( sq
295f2 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
295f3 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 3b  er(pE, &i)==0 );
295f4 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ..  pEList = pSe
295f5 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0d 0a 0d  lect->pEList;...
295f6 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c  .  /* Resolve al
295f7 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f  l names in the O
295f8 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 70  RDER BY term exp
295f9 72 65 73 73 69 6f 6e 0d 0a 20 20 2a 2f 0d 0a 20  ression..  */.. 
295fa 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20   memset(&nc, 0, 
295fb 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0d 0a 20 20  sizeof(nc));..  
295fc 6e 63 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  nc.pParse = pPar
295fd 73 65 3b 0d 0a 20 20 6e 63 2e 70 53 72 63 4c 69  se;..  nc.pSrcLi
295fe 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53  st = pSelect->pS
295ff 72 63 3b 0d 0a 20 20 6e 63 2e 70 45 4c 69 73 74  rc;..  nc.pEList
29600 20 3d 20 70 45 4c 69 73 74 3b 0d 0a 20 20 6e 63   = pEList;..  nc
29601 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0d 0a  .allowAgg = 1;..
29602 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0d 0a    nc.nErr = 0;..
29603 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
29604 62 3b 0d 0a 20 20 73 61 76 65 64 53 75 70 70 45  b;..  savedSuppE
29605 72 72 20 3d 20 64 62 2d 3e 73 75 70 70 72 65 73  rr = db->suppres
29606 73 45 72 72 3b 0d 0a 20 20 64 62 2d 3e 73 75 70  sErr;..  db->sup
29607 70 72 65 73 73 45 72 72 20 3d 20 31 3b 0d 0a 20  pressErr = 1;.. 
29608 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73   rc = sqlite3Res
29609 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e  olveExprNames(&n
2960a 63 2c 20 70 45 29 3b 0d 0a 20 20 64 62 2d 3e 73  c, pE);..  db->s
2960b 75 70 70 72 65 73 73 45 72 72 20 3d 20 73 61 76  uppressErr = sav
2960c 65 64 53 75 70 70 45 72 72 3b 0d 0a 20 20 69 66  edSuppErr;..  if
2960d 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b  ( rc ) return 0;
2960e 0d 0a 0d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20  ....  /* Try to 
2960f 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 20  match the ORDER 
29610 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67  BY expression ag
29611 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73  ainst an express
29612 69 6f 6e 0d 0a 20 20 2a 2a 20 69 6e 20 74 68 65  ion..  ** in the
29613 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65   result set.  Re
29614 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20  turn an 1-based 
29615 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74  index of the mat
29616 63 68 69 6e 67 0d 0a 20 20 2a 2a 20 72 65 73 75  ching..  ** resu
29617 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e 0d 0a 20  lt-set entry... 
29618 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20   */..  for(i=0; 
29619 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  i<pEList->nExpr;
2961a 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20   i++){..    if( 
2961b 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
2961c 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  re(pEList->a[i].
2961d 70 45 78 70 72 2c 20 70 45 29 3c 32 20 29 7b 0d  pExpr, pE)<2 ){.
2961e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b  .      return i+
2961f 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  1;..    }..  }..
29620 0d 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74  ..  /* If no mat
29621 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f  ch, return 0. */
29622 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
29623 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  ..../*..** Gener
29624 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ate an ORDER BY 
29625 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d  or GROUP BY term
29626 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72   out-of-range er
29627 72 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ror...*/..static
29628 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74   void resolveOut
29629 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 0d 0a 20  OfRangeError(.. 
2962a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2962b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
2962c 72 72 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e 74  rror context int
2962d 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65  o which to write
2962e 20 74 68 65 20 65 72 72 6f 72 20 2a 2f 0d 0a 20   the error */.. 
2962f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
29630 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f 52 44 45  pe,     /* "ORDE
29631 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f  R" or "GROUP" */
29632 0d 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20  ..  int i,      
29633 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
29634 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73 65 64  e index (1-based
29635 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6f 75  ) of the term ou
29636 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0d 0a 20  t of range */.. 
29637 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20 20   int mx         
29638 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65          /* Large
29639 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20 76  st permissible v
2963a 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0d 0a 29 7b  alue of i */..){
2963b 0d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
2963c 4d 73 67 28 70 50 61 72 73 65 2c 20 0d 0a 20 20  Msg(pParse, ..  
2963d 20 20 22 25 72 20 25 73 20 42 59 20 74 65 72 6d    "%r %s BY term
2963e 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20   out of range - 
2963f 73 68 6f 75 6c 64 20 62 65 20 22 0d 0a 20 20 20  should be "..   
29640 20 22 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20   "between 1 and 
29641 25 64 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d  %d", i, zType, m
29642 78 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  x);..}..../*..**
29643 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 44   Analyze the ORD
29644 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20  ER BY clause in 
29645 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
29646 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 4d  T statement.   M
29647 6f 64 69 66 79 0d 0a 2a 2a 20 65 61 63 68 20 74  odify..** each t
29648 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52  erm of the ORDER
29649 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 20   BY clause is a 
2964a 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72  constant integer
2964b 20 62 65 74 77 65 65 6e 20 31 0d 0a 2a 2a 20 61   between 1..** a
2964c 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20  nd N where N is 
2964d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2964e 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d  lumns in the com
2964f 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0d 0a 2a  pound SELECT...*
29650 2a 0d 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74  *..** ORDER BY t
29651 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c  erms that are al
29652 72 65 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72  ready an integer
29653 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e   between 1 and N
29654 20 61 72 65 0d 0a 2a 2a 20 75 6e 6d 6f 64 69 66   are..** unmodif
29655 69 65 64 2e 20 20 4f 52 44 45 52 20 42 59 20 74  ied.  ORDER BY t
29656 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 69 6e  erms that are in
29657 74 65 67 65 72 73 20 6f 75 74 73 69 64 65 20 74  tegers outside t
29658 68 65 20 72 61 6e 67 65 20 6f 66 0d 0a 2a 2a 20  he range of..** 
29659 31 20 74 68 72 6f 75 67 68 20 4e 20 67 65 6e 65  1 through N gene
2965a 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 20 20  rate an error.  
2965b 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
2965c 68 61 74 20 61 72 65 20 65 78 70 72 65 73 73 69  hat are expressi
2965d 6f 6e 73 0d 0a 2a 2a 20 61 72 65 20 6d 61 74 63  ons..** are matc
2965e 68 65 64 20 61 67 61 69 6e 73 74 20 72 65 73 75  hed against resu
2965f 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
29660 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 53  ns of compound S
29661 45 4c 45 43 54 0d 0a 2a 2a 20 62 65 67 69 6e 6e  ELECT..** beginn
29662 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65 66  ing with the lef
29663 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 6e  t-most SELECT an
29664 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 64  d working toward
29665 20 74 68 65 20 72 69 67 68 74 2e 0d 0a 2a 2a 20   the right...** 
29666 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  At the first mat
29667 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59  ch, the ORDER BY
29668 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
29669 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0d  ransformed into.
2966a 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20  .** the integer 
2966b 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0d 0a  column number...
2966c 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  **..** Return th
2966d 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
2966e 72 73 20 73 65 65 6e 2e 0d 0a 2a 2f 0d 0a 73 74  rs seen...*/..st
2966f 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
29670 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28  CompoundOrderBy(
29671 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
29672 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
29673 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
29674 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
29675 67 65 73 20 68 65 72 65 20 2a 2f 0d 0a 20 20 53  ges here */..  S
29676 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20  elect *pSelect  
29677 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
29678 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e  CT statement con
29679 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 45  taining the ORDE
2967a 52 20 42 59 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  R BY */..){..  i
2967b 6e 74 20 69 3b 0d 0a 20 20 45 78 70 72 4c 69 73  nt i;..  ExprLis
2967c 74 20 2a 70 4f 72 64 65 72 42 79 3b 0d 0a 20 20  t *pOrderBy;..  
2967d 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2967e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
2967f 3b 0d 0a 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44  ;..  int moreToD
29680 6f 20 3d 20 31 3b 0d 0a 0d 0a 20 20 70 4f 72 64  o = 1;....  pOrd
29681 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d 3e  erBy = pSelect->
29682 70 4f 72 64 65 72 42 79 3b 0d 0a 20 20 69 66 28  pOrderBy;..  if(
29683 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72   pOrderBy==0 ) r
29684 65 74 75 72 6e 20 30 3b 0d 0a 20 20 64 62 20 3d  eturn 0;..  db =
29685 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 23 69   pParse->db;..#i
29686 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
29687 55 4d 4e 0d 0a 20 20 69 66 28 20 70 4f 72 64 65  UMN..  if( pOrde
29688 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61  rBy->nExpr>db->a
29689 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
2968a 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0d 0a 20  IT_COLUMN] ){.. 
2968b 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2968c 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
2968d 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 4f 52  many terms in OR
2968e 44 45 52 20 42 59 20 63 6c 61 75 73 65 22 29 3b  DER BY clause");
2968f 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d  ..    return 1;.
29690 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  .  }..#endif..  
29691 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65  for(i=0; i<pOrde
29692 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  rBy->nExpr; i++)
29693 7b 0d 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d  {..    pOrderBy-
29694 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0d  >a[i].done = 0;.
29695 0a 20 20 7d 0d 0a 20 20 70 53 65 6c 65 63 74 2d  .  }..  pSelect-
29696 3e 70 4e 65 78 74 20 3d 20 30 3b 0d 0a 20 20 77  >pNext = 0;..  w
29697 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70  hile( pSelect->p
29698 50 72 69 6f 72 20 29 7b 0d 0a 20 20 20 20 70 53  Prior ){..    pS
29699 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70  elect->pPrior->p
2969a 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 74 3b 0d  Next = pSelect;.
2969b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70  .    pSelect = p
2969c 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0d  Select->pPrior;.
2969d 0a 20 20 7d 0d 0a 20 20 77 68 69 6c 65 28 20 70  .  }..  while( p
2969e 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f  Select && moreTo
2969f 44 6f 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63  Do ){..    struc
296a0 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
296a1 2a 70 49 74 65 6d 3b 0d 0a 20 20 20 20 6d 6f 72  *pItem;..    mor
296a2 65 54 6f 44 6f 20 3d 20 30 3b 0d 0a 20 20 20 20  eToDo = 0;..    
296a3 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
296a4 2d 3e 70 45 4c 69 73 74 3b 0d 0a 20 20 20 20 61  ->pEList;..    a
296a5 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
296a6 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   );..    for(i=0
296a7 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
296a8 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  ->a; i<pOrderBy-
296a9 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
296aa 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 6e  em++){..      in
296ab 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0d 0a 20 20  t iCol = -1;..  
296ac 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a 70      Expr *pE, *p
296ad 44 75 70 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  Dup;..      if( 
296ae 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f  pItem->done ) co
296af 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 70  ntinue;..      p
296b0 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  E = pItem->pExpr
296b1 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ;..      if( sql
296b2 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
296b3 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0d  r(pE, &iCol) ){.
296b4 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
296b5 6c 3c 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c  l<=0 || iCol>pEL
296b6 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0d 0a 20  ist->nExpr ){.. 
296b7 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65           resolve
296b8 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28  OutOfRangeError(
296b9 70 50 61 72 73 65 2c 20 22 4f 52 44 45 52 22 2c  pParse, "ORDER",
296ba 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45   i+1, pEList->nE
296bb 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  xpr);..         
296bc 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20   return 1;..    
296bd 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c      }..      }el
296be 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 69 43 6f  se{..        iCo
296bf 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d  l = resolveAsNam
296c0 65 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  e(pParse, pEList
296c1 2c 20 70 45 29 3b 0d 0a 20 20 20 20 20 20 20 20  , pE);..        
296c2 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0d 0a  if( iCol==0 ){..
296c3 20 20 20 20 20 20 20 20 20 20 70 44 75 70 20 3d            pDup =
296c4 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
296c5 64 62 2c 20 70 45 2c 20 30 29 3b 0d 0a 20 20 20  db, pE, 0);..   
296c6 20 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e         if( !db->
296c7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
296c8 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
296c9 65 72 74 28 70 44 75 70 29 3b 0d 0a 20 20 20 20  ert(pDup);..    
296ca 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72          iCol = r
296cb 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
296cc 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72  mToExprList(pPar
296cd 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75  se, pSelect, pDu
296ce 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  p);..          }
296cf 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
296d0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
296d1 2c 20 70 44 75 70 29 3b 0d 0a 20 20 20 20 20 20  , pDup);..      
296d2 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
296d3 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29      if( iCol>0 )
296d4 7b 0d 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  {..        CollS
296d5 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e  eq *pColl = pE->
296d6 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 20 20  pColl;..        
296d7 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e  int flags = pE->
296d8 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
296d9 6c 6c 61 74 65 3b 0d 0a 20 20 20 20 20 20 20 20  llate;..        
296da 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
296db 65 28 64 62 2c 20 70 45 29 3b 0d 0a 20 20 20 20  e(db, pE);..    
296dc 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
296dd 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45   = pE = sqlite3E
296de 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47  xpr(db, TK_INTEG
296df 45 52 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  ER, 0);..       
296e0 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74   if( pE==0 ) ret
296e1 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  urn 1;..        
296e2 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c  pE->pColl = pCol
296e3 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70 45 2d 3e  l;..        pE->
296e4 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56  flags |= EP_IntV
296e5 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0d 0a 20  alue | flags;.. 
296e6 20 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56 61         pE->u.iVa
296e7 6c 75 65 20 3d 20 69 43 6f 6c 3b 0d 0a 20 20 20  lue = iCol;..   
296e8 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4f 72 64       pItem->iOrd
296e9 65 72 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69  erByCol = (u16)i
296ea 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70 49  Col;..        pI
296eb 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0d 0a  tem->done = 1;..
296ec 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
296ed 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d        moreToDo =
296ee 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   1;..      }..  
296ef 20 20 7d 0d 0a 20 20 20 20 70 53 65 6c 65 63 74    }..    pSelect
296f0 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78   = pSelect->pNex
296f1 74 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69  t;..  }..  for(i
296f2 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e  =0; i<pOrderBy->
296f3 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nExpr; i++){..  
296f4 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
296f5 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0d  a[i].done==0 ){.
296f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
296f7 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
296f8 25 72 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  %r ORDER BY term
296f9 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
296fa 61 6e 79 20 22 0d 0a 20 20 20 20 20 20 20 20 20  any "..         
296fb 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68     "column in th
296fc 65 20 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69  e result set", i
296fd 2b 31 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  +1);..      retu
296fe 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
296ff 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  }..  return 0;..
29700 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63  }..../*..** Chec
29701 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  k every term in 
29702 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  the ORDER BY or 
29703 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
29704 70 4f 72 64 65 72 42 79 20 6f 66 0d 0a 2a 2a 20  pOrderBy of..** 
29705 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
29706 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49  ment pSelect.  I
29707 66 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65  f any term is re
29708 66 65 72 65 6e 63 65 20 74 6f 20 61 0d 0a 2a 2a  ference to a..**
29709 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   result set expr
2970a 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72  ession (as deter
2970b 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 78 70  mined by the Exp
2970c 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65  rList.a.iCol fie
2970d 6c 64 29 0d 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e  ld)..** then con
2970e 76 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 69  vert that term i
2970f 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68  nto a copy of th
29710 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
29711 72 65 73 75 6c 74 20 73 65 74 0d 0a 2a 2a 20 63  result set..** c
29712 6f 6c 75 6d 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  olumn...**..** I
29713 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65  f any errors are
29714 20 64 65 74 65 63 74 65 64 2c 20 61 64 64 20 61   detected, add a
29715 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
29716 74 6f 20 70 50 61 72 73 65 20 61 6e 64 0d 0a 2a  to pParse and..*
29717 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * return non-zer
29718 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20  o.  Return zero 
29719 69 66 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 65  if no errors are
2971a 20 73 65 65 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   seen...*/..SQLI
2971b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2971c 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
2971d 65 72 47 72 6f 75 70 42 79 28 0d 0a 20 20 50 61  erGroupBy(..  Pa
2971e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2971f 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
29720 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65  ontext.  Leave e
29721 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
29722 72 65 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20  re */..  Select 
29723 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f  *pSelect,      /
29724 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
29725 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e  tement containin
29726 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0d  g the clause */.
29727 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
29728 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20  derBy,   /* The 
29729 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
2972a 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62  P BY clause to b
2972b 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0d 0a  e processed */..
2972c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2972d 79 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45  ype     /* "ORDE
2972e 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f  R" or "GROUP" */
2972f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  ..){..  int i;..
29730 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
29731 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 45  pParse->db;..  E
29732 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
29733 0d 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ..  struct ExprL
29734 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
29735 0d 0a 0d 0a 20 20 69 66 28 20 70 4f 72 64 65 72  ....  if( pOrder
29736 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d  By==0 || pParse-
29737 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
29738 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 23  d ) return 0;..#
29739 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
2973a 4c 55 4d 4e 0d 0a 20 20 69 66 28 20 70 4f 72 64  LUMN..  if( pOrd
2973b 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e  erBy->nExpr>db->
2973c 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2973d 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0d 0a  MIT_COLUMN] ){..
2973e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2973f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
29740 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25   many terms in %
29741 73 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54  s BY clause", zT
29742 79 70 65 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  ype);..    retur
29743 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  n 1;..  }..#endi
29744 66 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53  f..  pEList = pS
29745 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0d 0a  elect->pEList;..
29746 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
29747 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74  !=0 );  /* sqlit
29748 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75  e3SelectNew() gu
29749 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f  arantees this */
2974a 0d 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ..  for(i=0, pIt
2974b 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
2974c 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
2974d 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
2974e 7b 0d 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d  {..    if( pItem
2974f 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 20 29 7b  ->iOrderByCol ){
29750 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  ..      if( pIte
29751 6d 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 3e 70  m->iOrderByCol>p
29752 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0d  EList->nExpr ){.
29753 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65  .        resolve
29754 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28  OutOfRangeError(
29755 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69  pParse, zType, i
29756 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  +1, pEList->nExp
29757 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74  r);..        ret
29758 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d  urn 1;..      }.
29759 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c  .      resolveAl
2975a 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69  ias(pParse, pELi
2975b 73 74 2c 20 70 49 74 65 6d 2d 3e 69 4f 72 64 65  st, pItem->iOrde
2975c 72 42 79 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d  rByCol-1, pItem-
2975d 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 29 3b 0d  >pExpr, zType);.
2975e 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
2975f 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn 0;..}..../
29760 2a 0d 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69  *..** pOrderBy i
29761 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72  s an ORDER BY or
29762 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
29763 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65   in SELECT state
29764 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0d 0a 2a  ment pSelect...*
29765 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65  * The Name conte
29766 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
29767 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e   statement is pN
29768 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74  C.  zType is eit
29769 68 65 72 0d 0a 2a 2a 20 22 4f 52 44 45 52 22 20  her..** "ORDER" 
2976a 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e  or "GROUP" depen
2976b 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79  ding on which ty
2976c 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72  pe of clause pOr
2976d 64 65 72 42 79 20 69 73 2e 0d 0a 2a 2a 0d 0a 2a  derBy is...**..*
2976e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
2976f 65 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72  esolves each ter
29770 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20  m of the clause 
29771 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69  into an expressi
29772 6f 6e 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6f  on...** If the o
29773 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20  rder-by term is 
29774 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 65 74  an integer I bet
29775 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68  ween 1 and N (wh
29776 65 72 65 20 4e 20 69 73 20 74 68 65 0d 0a 2a 2a  ere N is the..**
29777 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
29778 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
29779 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
2977a 43 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 70  CT) then the exp
2977b 72 65 73 73 69 6f 6e 0d 0a 2a 2a 20 69 6e 20 74  ression..** in t
2977c 68 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73  he resolution is
2977d 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49   a copy of the I
2977e 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65  -th result-set e
2977f 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 0d 0a  xpression.  If..
29780 2a 2a 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20  ** the order-by 
29781 74 65 72 6d 20 69 73 20 61 6e 20 69 64 65 6e 74  term is an ident
29782 69 66 79 20 74 68 61 74 20 63 6f 72 72 65 73 70  ify that corresp
29783 6f 6e 64 73 20 74 6f 20 74 68 65 20 41 53 2d 6e  onds to the AS-n
29784 61 6d 65 20 6f 66 0d 0a 2a 2a 20 61 20 72 65 73  ame of..** a res
29785 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
29786 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 65 72  on, then the ter
29787 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20  m resolves to a 
29788 63 6f 70 79 20 6f 66 20 74 68 65 0d 0a 2a 2a 20  copy of the..** 
29789 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
2978a 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73  ssion.  Otherwis
2978b 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  e, the expressio
2978c 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 69 6e  n is resolved in
2978d 0d 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 77  ..** the usual w
2978e 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 74  ay - using sqlit
2978f 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
29790 65 73 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  es()...**..** Th
29791 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
29792 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
29793 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72   errors.  If err
29794 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0d  ors occur, then.
29795 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61  .** an appropria
29796 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
29797 20 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69   might be left i
29798 6e 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20  n pParse.  (OOM 
29799 65 72 72 6f 72 73 0d 0a 2a 2a 20 65 78 63 65 70  errors..** excep
2979a 74 65 64 2e 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ted.)..*/..stati
2979b 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
2979c 65 72 47 72 6f 75 70 42 79 28 0d 0a 20 20 4e 61  erGroupBy(..  Na
2979d 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
2979e 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
2979f 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53  context of the S
297a0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
297a1 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53  */..  Select *pS
297a2 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54  elect,      /* T
297a3 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
297a4 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64  ent holding pOrd
297a5 65 72 42 79 20 2a 2f 0d 0a 20 20 45 78 70 72 4c  erBy */..  ExprL
297a6 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
297a7 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20   /* An ORDER BY 
297a8 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
297a9 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f  se to resolve */
297aa 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
297ab 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74  zType     /* Eit
297ac 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22  her "ORDER" or "
297ad 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f  GROUP", as appro
297ae 70 72 69 61 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  priate */..){.. 
297af 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
297b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
297b1 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
297b2 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20  */..  int iCol; 
297b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
297b4 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
297b5 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 73 74 72 75  umber */..  stru
297b6 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
297b7 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20   *pItem;   /* A 
297b8 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45  term of the ORDE
297b9 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0d 0a  R BY clause */..
297ba 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
297bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
297bc 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
297bd 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52  ext */..  int nR
297be 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20  esult;          
297bf 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
297c0 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
297c1 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
297c2 0d 0a 0d 0a 20 20 69 66 28 20 70 4f 72 64 65 72  ....  if( pOrder
297c3 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
297c4 3b 0d 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70  ;..  nResult = p
297c5 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e  Select->pEList->
297c6 6e 45 78 70 72 3b 0d 0a 20 20 70 50 61 72 73 65  nExpr;..  pParse
297c7 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0d   = pNC->pParse;.
297c8 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
297c9 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69  m=pOrderBy->a; i
297ca 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
297cb 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
297cc 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  ..    Expr *pE =
297cd 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0d 0a   pItem->pExpr;..
297ce 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c      iCol = resol
297cf 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c  veAsName(pParse,
297d0 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
297d1 2c 20 70 45 29 3b 0d 0a 20 20 20 20 69 66 28 20  , pE);..    if( 
297d2 69 43 6f 6c 3e 30 20 29 7b 0d 0a 20 20 20 20 20  iCol>0 ){..     
297d3 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d   /* If an AS-nam
297d4 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  e match is found
297d5 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45  , mark this ORDE
297d6 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62  R BY column as b
297d7 65 69 6e 67 0d 0a 20 20 20 20 20 20 2a 2a 20 61  eing..      ** a
297d8 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f   copy of the iCo
297d9 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20  l-th result-set 
297da 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62  column.  The sub
297db 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0d  sequent call to.
297dc 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
297dd 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f  3ResolveOrderGro
297de 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76  upBy() will conv
297df 65 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69  ert the expressi
297e0 6f 6e 20 74 6f 20 61 0d 0a 20 20 20 20 20 20 2a  on to a..      *
297e1 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43  * copy of the iC
297e2 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74  ol-th result-set
297e3 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0d   expression. */.
297e4 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4f  .      pItem->iO
297e5 72 64 65 72 42 79 43 6f 6c 20 3d 20 28 75 31 36  rderByCol = (u16
297e6 29 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 63 6f  )iCol;..      co
297e7 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a  ntinue;..    }..
297e8 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
297e9 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c  xprIsInteger(pE,
297ea 20 26 69 43 6f 6c 29 20 29 7b 0d 0a 20 20 20 20   &iCol) ){..    
297eb 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
297ec 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74  Y term is an int
297ed 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 20  eger constant.  
297ee 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 20 63  Again, set the c
297ef 6f 6c 75 6d 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  olumn..      ** 
297f0 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 73  number so that s
297f1 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
297f2 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c  erGroupBy() will
297f3 20 63 6f 6e 76 65 72 74 20 74 68 65 0d 0a 20 20   convert the..  
297f4 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20      ** order-by 
297f5 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f  term to a copy o
297f6 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  f the result-set
297f7 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a   expression */..
297f8 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 31        if( iCol<1
297f9 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 73   ){..        res
297fa 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72  olveOutOfRangeEr
297fb 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70  ror(pParse, zTyp
297fc 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29  e, i+1, nResult)
297fd 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ;..        retur
297fe 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  n 1;..      }.. 
297ff 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4f 72 64       pItem->iOrd
29800 65 72 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69  erByCol = (u16)i
29801 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 74  Col;..      cont
29802 69 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  inue;..    }....
29803 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
29804 2c 20 74 72 65 61 74 20 74 68 65 20 4f 52 44 45  , treat the ORDE
29805 52 20 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20  R BY term as an 
29806 6f 72 64 69 6e 61 72 79 20 65 78 70 72 65 73 73  ordinary express
29807 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 70 49 74 65  ion */..    pIte
29808 6d 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 20 3d  m->iOrderByCol =
29809 20 30 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   0;..    if( sql
2980a 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2980b 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b  ames(pNC, pE) ){
2980c 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ..      return 1
2980d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
2980e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
2980f 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
29810 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  By(pParse, pSele
29811 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a 54  ct, pOrderBy, zT
29812 79 70 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ype);..}..../*..
29813 2a 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73  ** Resolve names
29814 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73   in the SELECT s
29815 74 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 61  tatement p and a
29816 6c 6c 20 6f 66 20 69 74 73 20 64 65 73 63 65 6e  ll of its descen
29817 64 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  dents...*/..stat
29818 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 53 65  ic int resolveSe
29819 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65 72 20  lectStep(Walker 
2981a 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
2981b 20 2a 70 29 7b 0d 0a 20 20 4e 61 6d 65 43 6f 6e   *p){..  NameCon
2981c 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b 20  text *pOuterNC; 
2981d 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 74   /* Context that
2981e 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 53   contains this S
2981f 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 4e 61 6d 65  ELECT */..  Name
29820 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
29821 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
29822 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45  ext of this SELE
29823 43 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 43  CT */..  int isC
29824 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20  ompound;        
29825 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73   /* True if p is
29826 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
29827 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f  ct */..  int nCo
29828 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20  mpound;         
29829 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2982a 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f  mpound terms pro
2982b 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f  cessed so far */
2982c 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
2982d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e;          /* P
2982e 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2982f 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  /..  ExprList *p
29830 45 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20  EList;       /* 
29831 52 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65  Result set expre
29832 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0d 0a 20  ssion list */.. 
29833 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
29834 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
29835 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 45   counter */..  E
29836 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42  xprList *pGroupB
29837 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 47 52  y;     /* The GR
29838 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
29839 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 66  ..  Select *pLef
2983a 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c  tmost;      /* L
2983b 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45  eft-most of SELE
2983c 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64  CT of a compound
2983d 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a   */..  sqlite3 *
2983e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  db;            /
2983f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
29840 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 0d 0a 0d 0a  ction */..  ....
29841 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
29842 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46  ;..  if( p->selF
29843 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76  lags & SF_Resolv
29844 65 64 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ed ){..    retur
29845 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a 20 20  n WRC_Prune;..  
29846 7d 0d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d 20  }..  pOuterNC = 
29847 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0d  pWalker->u.pNC;.
29848 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c  .  pParse = pWal
29849 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0d 0a 20 20  ker->pParse;..  
2984a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2984b 0d 0a 0d 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c  ....  /* Normall
2984c 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  y sqlite3SelectE
2984d 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20  xpand() will be 
2984e 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64  called first and
2984f 20 77 69 6c 6c 20 68 61 76 65 0d 0a 20 20 2a 2a   will have..  **
29850 20 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65   already expande
29851 64 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20  d this SELECT.  
29852 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73  However, if this
29853 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77   is a subquery w
29854 69 74 68 69 6e 0d 0a 20 20 2a 2a 20 61 6e 20 65  ithin..  ** an e
29855 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74  xpression, sqlit
29856 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
29857 65 73 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  es() will be cal
29858 6c 65 64 20 77 69 74 68 6f 75 74 20 61 0d 0a 20  led without a.. 
29859 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74   ** prior call t
2985a 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  o sqlite3SelectE
2985b 78 70 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 74  xpand().  When t
2985c 68 61 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 74  hat happens, let
2985d 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65  ..  ** sqlite3Se
2985e 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 61 6c  lectPrep() do al
2985f 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73  l of the process
29860 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c  ing for this SEL
29861 45 43 54 2e 0d 0a 20 20 2a 2a 20 73 71 6c 69 74  ECT...  ** sqlit
29862 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 77  e3SelectPrep() w
29863 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20  ill invoke both 
29864 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
29865 61 6e 64 28 29 20 61 6e 64 0d 0a 20 20 2a 2a 20  and() and..  ** 
29866 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20  this routine in 
29867 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65  the correct orde
29868 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  r...  */..  if( 
29869 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2986a 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29  F_Expanded)==0 )
2986b 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  {..    sqlite3Se
2986c 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c  lectPrep(pParse,
2986d 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0d 0a   p, pOuterNC);..
2986e 20 20 20 20 72 65 74 75 72 6e 20 28 70 50 61 72      return (pPar
2986f 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
29870 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20  mallocFailed) ? 
29871 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f  WRC_Abort : WRC_
29872 50 72 75 6e 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Prune;..  }.... 
29873 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d   isCompound = p-
29874 3e 70 50 72 69 6f 72 21 3d 30 3b 0d 0a 20 20 6e  >pPrior!=0;..  n
29875 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0d 0a 20  Compound = 0;.. 
29876 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0d   pLeftmost = p;.
29877 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0d 0a  .  while( p ){..
29878 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
29879 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78  selFlags & SF_Ex
2987a 70 61 6e 64 65 64 29 21 3d 30 20 29 3b 0d 0a 20  panded)!=0 );.. 
2987b 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
2987c 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73  elFlags & SF_Res
2987d 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0d 0a 20 20  olved)==0 );..  
2987e 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d    p->selFlags |=
2987f 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0d 0a 0d   SF_Resolved;...
29880 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  .    /* Resolve 
29881 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
29882 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  in the LIMIT and
29883 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e   OFFSET clauses.
29884 20 54 68 65 73 65 0d 0a 20 20 20 20 2a 2a 20 61   These..    ** a
29885 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74  re not allowed t
29886 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e  o refer to any n
29887 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e  ames, so pass an
29888 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65   empty NameConte
29889 78 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  xt...    */..   
2988a 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
2988b 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0d 0a   sizeof(sNC));..
2988c 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
2988d 20 70 50 61 72 73 65 3b 0d 0a 20 20 20 20 69 66   pParse;..    if
2988e 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
2988f 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
29890 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c 0d 0a 20  p->pLimit) ||.. 
29891 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
29892 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
29893 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29  sNC, p->pOffset)
29894 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72   ){..      retur
29895 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a 20 20  n WRC_Abort;..  
29896 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20    }..  ..    /* 
29897 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c  Set up the local
29898 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f   name-context to
29899 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33   pass to sqlite3
2989a 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2989b 28 29 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 72 65  () to..    ** re
2989c 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74  solve the result
2989d 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20  -set expression 
2989e 6c 69 73 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  list...    */.. 
2989f 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20     sNC.allowAgg 
298a0 3d 20 31 3b 0d 0a 20 20 20 20 73 4e 43 2e 70 53  = 1;..    sNC.pS
298a1 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  rcList = p->pSrc
298a2 3b 0d 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74  ;..    sNC.pNext
298a3 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0d 0a 20 20   = pOuterNC;..  
298a4 0d 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ..    /* Resolve
298a5 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65   names in the re
298a6 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0d 0a 20 20  sult set. */..  
298a7 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
298a8 4c 69 73 74 3b 0d 0a 20 20 20 20 61 73 73 65 72  List;..    asser
298a9 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0d  t( pEList!=0 );.
298aa 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
298ab 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
298ac 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 45 78 70 72  ++){..      Expr
298ad 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61   *pX = pEList->a
298ae 5b 69 5d 2e 70 45 78 70 72 3b 0d 0a 20 20 20 20  [i].pExpr;..    
298af 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
298b0 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
298b1 4e 43 2c 20 70 58 29 20 29 7b 0d 0a 20 20 20 20  NC, pX) ){..    
298b2 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
298b3 62 6f 72 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  bort;..      }..
298b4 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f      }..  ..    /
298b5 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65  * Recursively re
298b6 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61  solve names in a
298b7 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0d 0a 20  ll subqueries.. 
298b8 20 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69     */..    for(i
298b9 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  =0; i<p->pSrc->n
298ba 53 72 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  Src; i++){..    
298bb 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
298bc 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
298bd 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0d 0a  p->pSrc->a[i];..
298be 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
298bf 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20  >pSelect ){..   
298c0 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74       NameContext
298c1 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 20 20 2f   *pNC;         /
298c2 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74  * Used to iterat
298c3 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20  e name contexts 
298c4 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  */..        int 
298c5 6e 52 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20  nRef = 0;       
298c6 20 20 20 20 20 20 2f 2a 20 52 65 66 63 6f 75 6e        /* Refcoun
298c7 74 20 66 6f 72 20 70 4f 75 74 65 72 4e 43 20 61  t for pOuterNC a
298c8 6e 64 20 6f 75 74 65 72 20 63 6f 6e 74 65 78 74  nd outer context
298c9 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 63 6f  s */..        co
298ca 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64  nst char *zSaved
298cb 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
298cc 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0d  ->zAuthContext;.
298cd 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ...        /* Co
298ce 75 6e 74 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  unt the total nu
298cf 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
298d0 65 73 20 74 6f 20 70 4f 75 74 65 72 4e 43 20 61  es to pOuterNC a
298d1 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 0d 0a 20  nd all of its.. 
298d2 20 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74         ** parent
298d3 20 63 6f 6e 74 65 78 74 73 2e 20 41 66 74 65 72   contexts. After
298d4 20 72 65 73 6f 6c 76 69 6e 67 20 72 65 66 65 72   resolving refer
298d5 65 6e 63 65 73 20 74 6f 20 65 78 70 72 65 73 73  ences to express
298d6 69 6f 6e 73 20 69 6e 0d 0a 20 20 20 20 20 20 20  ions in..       
298d7 20 2a 2a 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65   ** pItem->pSele
298d8 63 74 2c 20 63 68 65 63 6b 20 69 66 20 74 68 69  ct, check if thi
298d9 73 20 76 61 6c 75 65 20 68 61 73 20 63 68 61 6e  s value has chan
298da 67 65 64 2e 20 49 66 20 73 6f 2c 20 74 68 65 6e  ged. If so, then
298db 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 45 4c  ..        ** SEL
298dc 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 49  ECT statement pI
298dd 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 6d 75 73  tem->pSelect mus
298de 74 20 62 65 20 63 6f 72 72 65 6c 61 74 65 64 2e  t be correlated.
298df 20 53 65 74 20 74 68 65 0d 0a 20 20 20 20 20 20   Set the..      
298e0 20 20 2a 2a 20 70 49 74 65 6d 2d 3e 69 73 43 6f    ** pItem->isCo
298e1 72 72 65 6c 61 74 65 64 20 66 6c 61 67 20 69 66  rrelated flag if
298e2 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73   this is the cas
298e3 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 66  e. */..        f
298e4 6f 72 28 70 4e 43 3d 70 4f 75 74 65 72 4e 43 3b  or(pNC=pOuterNC;
298e5 20 70 4e 43 3b 20 70 4e 43 3d 70 4e 43 2d 3e 70   pNC; pNC=pNC->p
298e6 4e 65 78 74 29 20 6e 52 65 66 20 2b 3d 20 70 4e  Next) nRef += pN
298e7 43 2d 3e 6e 52 65 66 3b 0d 0a 0d 0a 20 20 20 20  C->nRef;....    
298e8 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
298e9 4e 61 6d 65 20 29 20 70 50 61 72 73 65 2d 3e 7a  Name ) pParse->z
298ea 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49  AuthContext = pI
298eb 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20  tem->zName;..   
298ec 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f       sqlite3Reso
298ed 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70  lveSelectNames(p
298ee 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53  Parse, pItem->pS
298ef 65 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e 43 29  elect, pOuterNC)
298f0 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  ;..        pPars
298f1 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  e->zAuthContext 
298f2 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 3b  = zSavedContext;
298f3 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ..        if( pP
298f4 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
298f5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
298f6 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
298f7 74 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 66 6f  t;....        fo
298f8 72 28 70 4e 43 3d 70 4f 75 74 65 72 4e 43 3b 20  r(pNC=pOuterNC; 
298f9 70 4e 43 3b 20 70 4e 43 3d 70 4e 43 2d 3e 70 4e  pNC; pNC=pNC->pN
298fa 65 78 74 29 20 6e 52 65 66 20 2d 3d 20 70 4e 43  ext) nRef -= pNC
298fb 2d 3e 6e 52 65 66 3b 0d 0a 20 20 20 20 20 20 20  ->nRef;..       
298fc 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
298fd 69 73 43 6f 72 72 65 6c 61 74 65 64 3d 3d 30 20  isCorrelated==0 
298fe 26 26 20 6e 52 65 66 3c 3d 30 20 29 3b 0d 0a 20  && nRef<=0 );.. 
298ff 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 73         pItem->is
29900 43 6f 72 72 65 6c 61 74 65 64 20 3d 20 28 6e 52  Correlated = (nR
29901 65 66 21 3d 30 29 3b 0d 0a 20 20 20 20 20 20 7d  ef!=0);..      }
29902 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20  ..    }..  ..   
29903 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
29904 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75   no aggregate fu
29905 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 72  nctions in the r
29906 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 6e  esult-set, and n
29907 6f 20 47 52 4f 55 50 20 42 59 20 0d 0a 20 20 20  o GROUP BY ..   
29908 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   ** expression, 
29909 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67  do not allow agg
2990a 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f  regates in any o
2990b 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  f the other expr
2990c 65 73 73 69 6f 6e 73 2e 0d 0a 20 20 20 20 2a 2f  essions...    */
2990d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ..    assert( (p
2990e 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2990f 41 67 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b  Aggregate)==0 );
29910 0d 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d  ..    pGroupBy =
29911 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0d 0a 20   p->pGroupBy;.. 
29912 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20     if( pGroupBy 
29913 7c 7c 20 73 4e 43 2e 68 61 73 41 67 67 20 29 7b  || sNC.hasAgg ){
29914 0d 0a 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c  ..      p->selFl
29915 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67  ags |= SF_Aggreg
29916 61 74 65 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  ate;..    }else{
29917 0d 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c 6f  ..      sNC.allo
29918 77 41 67 67 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  wAgg = 0;..    }
29919 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20 49 66 20  ..  ..    /* If 
2991a 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20  a HAVING clause 
2991b 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e  is present, then
2991c 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
2991d 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
2991e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  ...    */..    i
2991f 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26  f( p->pHaving &&
29920 20 21 70 47 72 6f 75 70 42 79 20 29 7b 0d 0a 20   !pGroupBy ){.. 
29921 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
29922 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20  rMsg(pParse, "a 
29923 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
29924 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  is required befo
29925 72 65 20 48 41 56 49 4e 47 22 29 3b 0d 0a 20 20  re HAVING");..  
29926 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
29927 62 6f 72 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  bort;..    }..  
29928 0d 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65  ..    /* Add the
29929 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
2992a 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e   to the name-con
2992b 74 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73  text before pars
2992c 69 6e 67 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20  ing the..    ** 
2992d 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
2992e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20  s in the SELECT 
2992f 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20  statement. This 
29930 69 73 20 73 6f 20 74 68 61 74 0d 0a 20 20 20 20  is so that..    
29931 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ** expressions i
29932 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
29933 73 65 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65  se (etc.) can re
29934 66 65 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f  fer to expressio
29935 6e 73 20 62 79 0d 0a 20 20 20 20 2a 2a 20 61 6c  ns by..    ** al
29936 69 61 73 65 73 20 69 6e 20 74 68 65 20 72 65 73  iases in the res
29937 75 6c 74 20 73 65 74 2e 0d 0a 20 20 20 20 2a 2a  ult set...    **
29938 0d 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70  ..    ** Minor p
29939 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69 73  oint: If this is
2993a 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20   the case, then 
2993b 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 77  the expression w
2993c 69 6c 6c 20 62 65 0d 0a 20 20 20 20 2a 2a 20 72  ill be..    ** r
2993d 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20  e-evaluated for 
2993e 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74  each reference t
2993f 6f 20 69 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  o it...    */.. 
29940 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20     sNC.pEList = 
29941 70 2d 3e 70 45 4c 69 73 74 3b 0d 0a 20 20 20 20  p->pEList;..    
29942 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
29943 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
29944 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0d  , p->pWhere) ||.
29945 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  .       sqlite3R
29946 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
29947 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67  &sNC, p->pHaving
29948 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20  )..    ){..     
29949 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
2994a 74 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  t;..    }....   
2994b 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2994c 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c   and GROUP BY cl
2994d 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65  auses may not re
2994e 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0d  fer to terms in.
2994f 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
29950 65 72 69 65 73 20 0d 0a 20 20 20 20 2a 2f 0d 0a  eries ..    */..
29951 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20      sNC.pNext = 
29952 30 3b 0d 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f  0;..    sNC.allo
29953 77 41 67 67 20 3d 20 31 3b 0d 0a 0d 0a 20 20 20  wAgg = 1;....   
29954 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20   /* Process the 
29955 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
29956 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45  for singleton SE
29957 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
29958 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 44  ..    ** The ORD
29959 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72  ER BY clause for
2995a 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 43   compounds SELEC
2995b 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20  T statements is 
2995c 68 61 6e 64 6c 65 64 0d 0a 20 20 20 20 2a 2a 20  handled..    ** 
2995d 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c  below, after all
2995e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
2995f 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  ets for all of t
29960 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0d 0a  he elements of..
29961 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f      ** the compo
29962 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65  und have been re
29963 73 6f 6c 76 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d  solved...    */.
29964 0a 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70  .    if( !isComp
29965 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f  ound && resolveO
29966 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43  rderGroupBy(&sNC
29967 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  , p, p->pOrderBy
29968 2c 20 22 4f 52 44 45 52 22 29 20 29 7b 0d 0a 20  , "ORDER") ){.. 
29969 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
2996a 41 62 6f 72 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Abort;..    }.. 
2996b 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
2996c 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20  cFailed ){..    
2996d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
2996e 72 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a  rt;..    }..  ..
2996f 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74      /* Resolve t
29970 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
29971 73 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65  se.  At the same
29972 20 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65   time, make sure
29973 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52   ..    ** the GR
29974 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f  OUP BY clause do
29975 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
29976 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
29977 6e 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ns...    */..   
29978 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b   if( pGroupBy ){
29979 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ..      struct E
2997a 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
2997b 74 65 6d 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20  tem;..    ..    
2997c 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64    if( resolveOrd
2997d 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20  erGroupBy(&sNC, 
2997e 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52  p, pGroupBy, "GR
2997f 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c  OUP") || db->mal
29980 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
29981 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
29982 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20 20 20 7d  _Abort;..      }
29983 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c  ..      for(i=0,
29984 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d   pItem=pGroupBy-
29985 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e  >a; i<pGroupBy->
29986 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
29987 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  m++){..        i
29988 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
29989 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c  ty(pItem->pExpr,
2998a 20 45 50 5f 41 67 67 29 20 29 7b 0d 0a 20 20 20   EP_Agg) ){..   
2998b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2998c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2998d 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2998e 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  ons are not allo
2998f 77 65 64 20 69 6e 20 22 0d 0a 20 20 20 20 20 20  wed in "..      
29990 20 20 20 20 20 20 20 20 22 74 68 65 20 47 52 4f          "the GRO
29991 55 50 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0d  UP BY clause");.
29992 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
29993 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a 20 20  n WRC_Abort;..  
29994 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
29995 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
29996 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65  * Advance to the
29997 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68   next term of th
29998 65 20 63 6f 6d 70 6f 75 6e 64 0d 0a 20 20 20 20  e compound..    
29999 2a 2f 0d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70  */..    p = p->p
2999a 50 72 69 6f 72 3b 0d 0a 20 20 20 20 6e 43 6f 6d  Prior;..    nCom
2999b 70 6f 75 6e 64 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d  pound++;..  }...
2999c 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
2999d 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20  e ORDER BY on a 
2999e 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20  compound SELECT 
2999f 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20  after all terms 
299a0 6f 66 0d 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d  of..  ** the com
299a1 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20  pound have been 
299a2 72 65 73 6f 6c 76 65 64 2e 0d 0a 20 20 2a 2f 0d  resolved...  */.
299a3 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e  .  if( isCompoun
299a4 64 20 26 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70  d && resolveComp
299a5 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72  oundOrderBy(pPar
299a6 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 29 20 29  se, pLeftmost) )
299a7 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  {..    return WR
299a8 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 7d 0d 0a 0d  C_Abort;..  }...
299a9 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72  .  return WRC_Pr
299aa 75 6e 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  une;..}..../*..*
299ab 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
299ac 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73 69  alks an expressi
299ad 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73 6f  on tree and reso
299ae 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73 20  lves references 
299af 74 6f 0d 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c  to..** table col
299b0 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c 74 2d  umns and result-
299b1 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74  set columns.  At
299b2 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20   the same time, 
299b3 64 6f 20 65 72 72 6f 72 0d 0a 2a 2a 20 63 68 65  do error..** che
299b4 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 6f  cking on functio
299b5 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 20  n usage and set 
299b6 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 67  a flag if any ag
299b7 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
299b8 73 0d 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0d  s..** are seen..
299b9 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c  .**..** To resol
299ba 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73  ve table columns
299bb 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c   references we l
299bc 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f  ook for nodes (o
299bd 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74  r subtrees) of t
299be 68 65 20 0d 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59  he ..** form X.Y
299bf 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73  .Z or Y.Z or jus
299c0 74 20 5a 20 77 68 65 72 65 0d 0a 2a 2a 0d 0a 2a  t Z where..**..*
299c1 2a 20 20 20 20 20 20 58 3a 20 20 20 54 68 65 20  *      X:   The 
299c2 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61  name of a databa
299c3 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 69 6e 22  se.  Ex:  "main"
299c4 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 0d 0a 2a   or "temp" or..*
299c5 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  *           the 
299c6 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 61 73  symbolic name as
299c7 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 54 54  signed to an ATT
299c8 41 43 48 2d 65 64 20 64 61 74 61 62 61 73 65 2e  ACH-ed database.
299c9 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 59 3a  ..**..**      Y:
299ca 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61     The name of a
299cb 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d   table in a FROM
299cc 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20   clause.  Or in 
299cd 61 20 74 72 69 67 67 65 72 0d 0a 2a 2a 20 20 20  a trigger..**   
299ce 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74          one of t
299cf 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73  he special names
299d0 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e   "old" or "new".
299d1 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 5a 3a  ..**..**      Z:
299d2 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61     The name of a
299d3 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65   column in table
299d4 20 59 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   Y...**..** The 
299d5 6e 6f 64 65 20 61 74 20 74 68 65 20 72 6f 6f 74  node at the root
299d6 20 6f 66 20 74 68 65 20 73 75 62 74 72 65 65 20   of the subtree 
299d7 69 73 20 6d 6f 64 69 66 69 65 64 20 61 73 20 66  is modified as f
299d8 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ollows:..**..** 
299d9 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 20     Expr.op      
299da 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f    Changed to TK_
299db 43 4f 4c 55 4d 4e 0d 0a 2a 2a 20 20 20 20 45 78  COLUMN..**    Ex
299dc 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69  pr.pTab      Poi
299dd 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65  nts to the Table
299de 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0d   object for X.Y.
299df 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c  .**    Expr.iCol
299e0 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e  umn   The column
299e1 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20   index in X.Y.  
299e2 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64  -1 for the rowid
299e3 2e 0d 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54  ...**    Expr.iT
299e4 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45  able    The VDBE
299e5 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   cursor number f
299e6 6f 72 20 58 2e 59 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a  or X.Y..**..**..
299e7 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 72 65  ** To resolve re
299e8 73 75 6c 74 2d 73 65 74 20 72 65 66 65 72 65 6e  sult-set referen
299e9 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78  ces, look for ex
299ea 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f  pression nodes o
299eb 66 20 74 68 65 0d 0a 2a 2a 20 66 6f 72 6d 20 5a  f the..** form Z
299ec 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e 64 20   (with no X and 
299ed 59 20 70 72 65 66 69 78 29 20 77 68 65 72 65 20  Y prefix) where 
299ee 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20 74 68  the Z matches th
299ef 65 20 72 69 67 68 74 2d 68 61 6e 64 0d 0a 2a 2a  e right-hand..**
299f0 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 63   size of an AS c
299f1 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65 73  lause in the res
299f2 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45 4c  ult-set of a SEL
299f3 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70 72  ECT.  The Z expr
299f4 65 73 73 69 6f 6e 0d 0a 2a 2a 20 69 73 20 72 65  ession..** is re
299f5 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79  placed by a copy
299f6 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   of the left-han
299f7 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65  d side of the re
299f8 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
299f9 69 6f 6e 2e 0d 0a 2a 2a 20 54 61 62 6c 65 2d 6e  ion...** Table-n
299fa 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e  ame and function
299fb 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75   resolution occu
299fc 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69  rs on the substi
299fd 74 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e  tuted expression
299fe 0d 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20  ..** tree.  For 
299ff 65 78 61 6d 70 6c 65 2c 20 69 6e 3a 0d 0a 2a 2a  example, in:..**
29a00 0d 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54  ..**      SELECT
29a01 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41   a+b AS x, c+d A
29a02 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45  S y FROM t1 ORDE
29a03 52 20 42 59 20 78 3b 0d 0a 2a 2a 0d 0a 2a 2a 20  R BY x;..**..** 
29a04 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f 66 20  The "x" term of 
29a05 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 73 20  the order by is 
29a06 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 2b 62  replaced by "a+b
29a07 22 20 74 6f 20 72 65 6e 64 65 72 3a 0d 0a 2a 2a  " to render:..**
29a08 0d 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54  ..**      SELECT
29a09 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41   a+b AS x, c+d A
29a0a 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45  S y FROM t1 ORDE
29a0b 52 20 42 59 20 61 2b 62 3b 0d 0a 2a 2a 0d 0a 2a  R BY a+b;..**..*
29a0c 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  * Function calls
29a0d 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20   are checked to 
29a0e 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
29a0f 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0d  he function is .
29a10 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20  .** defined and 
29a11 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74  that the correct
29a12 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
29a13 65 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69  ents are specifi
29a14 65 64 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20 66  ed...** If the f
29a15 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67  unction is an ag
29a16 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
29a17 2c 20 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e  , then the pNC->
29a18 68 61 73 41 67 67 20 69 73 0d 0a 2a 2a 20 73 65  hasAgg is..** se
29a19 74 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65  t and the opcode
29a1a 20 69 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d   is changed from
29a1b 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20   TK_FUNCTION to 
29a1c 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e  TK_AGG_FUNCTION.
29a1d 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 72 65  ..** If an expre
29a1e 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61  ssion contains a
29a1f 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
29a20 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50 5f 41  ns then the EP_A
29a21 67 67 0d 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20  gg..** property 
29a22 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
29a23 6e 20 69 73 20 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a  n is set...**..*
29a24 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  * An error messa
29a25 67 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50  ge is left in pP
29a26 61 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67  arse if anything
29a27 20 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20   is amiss.  The 
29a28 6e 75 6d 62 65 72 0d 0a 2a 2a 20 69 66 20 65 72  number..** if er
29a29 72 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65 64  rors is returned
29a2a 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
29a2b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
29a2c 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
29a2d 73 28 20 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  s( ..  NameConte
29a2e 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 2f  xt *pNC,       /
29a2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 72  * Namespace to r
29a30 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f  esolve expressio
29a31 6e 73 20 69 6e 2e 20 2a 2f 0d 0a 20 20 45 78 70  ns in. */..  Exp
29a32 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
29a33 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
29a34 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61  ession to be ana
29a35 6c 79 7a 65 64 2e 20 2a 2f 0d 0a 29 7b 0d 0a 20  lyzed. */..){.. 
29a36 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 67   int savedHasAgg
29a37 3b 0d 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0d 0a  ;..  Walker w;..
29a38 0d 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ..  if( pExpr==0
29a39 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 23 69   ) return 0;..#i
29a3a 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
29a3b 52 5f 44 45 50 54 48 3e 30 0d 0a 20 20 7b 0d 0a  R_DEPTH>0..  {..
29a3c 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
29a3d 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
29a3e 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
29a3f 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74  3ExprCheckHeight
29a40 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
29a41 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 61  nHeight+pNC->pPa
29a42 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 7b  rse->nHeight) ){
29a43 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ..      return 1
29a44 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50  ;..    }..    pP
29a45 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d  arse->nHeight +=
29a46 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b   pExpr->nHeight;
29a47 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20  ..  }..#endif.. 
29a48 20 73 61 76 65 64 48 61 73 41 67 67 20 3d 20 70   savedHasAgg = p
29a49 4e 43 2d 3e 68 61 73 41 67 67 3b 0d 0a 20 20 70  NC->hasAgg;..  p
29a4a 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0d  NC->hasAgg = 0;.
29a4b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61  .  w.xExprCallba
29a4c 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72  ck = resolveExpr
29a4d 53 74 65 70 3b 0d 0a 20 20 77 2e 78 53 65 6c 65  Step;..  w.xSele
29a4e 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  ctCallback = res
29a4f 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0d  olveSelectStep;.
29a50 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e  .  w.pParse = pN
29a51 43 2d 3e 70 50 61 72 73 65 3b 0d 0a 20 20 77 2e  C->pParse;..  w.
29a52 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0d 0a 20 20  u.pNC = pNC;..  
29a53 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
29a54 26 77 2c 20 70 45 78 70 72 29 3b 0d 0a 23 69 66  &w, pExpr);..#if
29a55 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
29a56 5f 44 45 50 54 48 3e 30 0d 0a 20 20 70 4e 43 2d  _DEPTH>0..  pNC-
29a57 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74  >pParse->nHeight
29a58 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67   -= pExpr->nHeig
29a59 68 74 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69  ht;..#endif..  i
29a5a 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c  f( pNC->nErr>0 |
29a5b 7c 20 77 2e 70 50 61 72 73 65 2d 3e 6e 45 72 72  | w.pParse->nErr
29a5c 3e 30 20 29 7b 0d 0a 20 20 20 20 45 78 70 72 53  >0 ){..    ExprS
29a5d 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
29a5e 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0d 0a 20 20  , EP_Error);..  
29a5f 7d 0d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61  }..  if( pNC->ha
29a60 73 41 67 67 20 29 7b 0d 0a 20 20 20 20 45 78 70  sAgg ){..    Exp
29a61 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
29a62 70 72 2c 20 45 50 5f 41 67 67 29 3b 0d 0a 20 20  pr, EP_Agg);..  
29a63 7d 65 6c 73 65 20 69 66 28 20 73 61 76 65 64 48  }else if( savedH
29a64 61 73 41 67 67 20 29 7b 0d 0a 20 20 20 20 70 4e  asAgg ){..    pN
29a65 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0d 0a  C->hasAgg = 1;..
29a66 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 45 78    }..  return Ex
29a67 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
29a68 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0d  xpr, EP_Error);.
29a69 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  .}....../*..** R
29a6a 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73  esolve all names
29a6b 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69   in all expressi
29a6c 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ons of a SELECT 
29a6d 61 6e 64 20 69 6e 20 61 6c 6c 0d 0a 2a 2a 20 64  and in all..** d
29a6e 65 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65  ecendents of the
29a6f 20 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69   SELECT, includi
29a70 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66  ng compounds off
29a71 20 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0d 0a   of p->pPrior,..
29a72 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e  ** subqueries in
29a73 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e   expressions, an
29a74 64 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65  d subqueries use
29a75 64 20 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65  d as FROM clause
29a76 0d 0a 2a 2a 20 74 65 72 6d 73 2e 0d 0a 2a 2a 0d  ..** terms...**.
29a77 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33 52  .** See sqlite3R
29a78 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
29a79 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  ) for a descript
29a7a 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 73  ion of the kinds
29a7b 20 6f 66 0d 0a 2a 2a 20 74 72 61 6e 73 66 6f 72   of..** transfor
29a7c 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63  mations that occ
29a7d 75 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20  ur...**..** All 
29a7e 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
29a7f 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  s should have be
29a80 65 6e 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e  en expanded usin
29a81 67 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c  g..** sqlite3Sel
29a82 65 63 74 45 78 70 61 6e 64 28 29 20 70 72 69 6f  ectExpand() prio
29a83 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68  r to invoking th
29a84 69 73 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d  is routine...*/.
29a85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29a86 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f  void sqlite3Reso
29a87 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 0d  lveSelectNames(.
29a88 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
29a89 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
29a8a 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
29a8b 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c  */..  Select *p,
29a8c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29a8d 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
29a8e 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
29a8f 2e 20 2a 2f 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74  . */..  NameCont
29a90 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f  ext *pOuterNC  /
29a91 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66  * Name context f
29a92 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54  or parent SELECT
29a93 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 29   statement */..)
29a94 7b 0d 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0d 0a  {..  Walker w;..
29a95 0d 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ..  assert( p!=0
29a96 20 29 3b 0d 0a 20 20 77 2e 78 45 78 70 72 43 61   );..  w.xExprCa
29a97 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65  llback = resolve
29a98 45 78 70 72 53 74 65 70 3b 0d 0a 20 20 77 2e 78  ExprStep;..  w.x
29a99 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d  SelectCallback =
29a9a 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74   resolveSelectSt
29a9b 65 70 3b 0d 0a 20 20 77 2e 70 50 61 72 73 65 20  ep;..  w.pParse 
29a9c 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 77 2e 75  = pParse;..  w.u
29a9d 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b  .pNC = pOuterNC;
29a9e 0d 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  ..  sqlite3WalkS
29a9f 65 6c 65 63 74 28 26 77 2c 20 70 29 3b 0d 0a 7d  elect(&w, p);..}
29aa0 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
29aa1 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 6f 6c  *** End of resol
29aa2 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve.c ***********
29aa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29aa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29aa5 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
29aa6 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
29aa7 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
29aa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29aa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29aaa 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
29aab 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a  1 September 15..
29aac 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
29aad 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
29aae 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
29aaf 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
29ab0 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
29ab1 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
29ab2 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
29ab3 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
29ab4 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
29ab5 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
29ab6 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
29ab7 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
29ab8 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
29ab9 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
29aba 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
29abb 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
29abc 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
29abd 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
29abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ac2 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  ******..** This 
29ac3 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f  file contains ro
29ac4 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20  utines used for 
29ac5 61 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73  analyzing expres
29ac6 73 69 6f 6e 73 20 61 6e 64 0d 0a 2a 2a 20 66 6f  sions and..** fo
29ac7 72 20 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42  r generating VDB
29ac8 45 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c  E code that eval
29ac9 75 61 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e  uates expression
29aca 73 20 69 6e 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f  s in SQLite...*/
29acb 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
29acc 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 27  n the 'affinity'
29acd 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
29ace 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e  on pExpr if any.
29acf 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 45 78 70  ..**..** If pExp
29ad0 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61  r is a column, a
29ad1 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
29ad2 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41  column via an 'A
29ad3 53 27 20 61 6c 69 61 73 2c 0d 0a 2a 2a 20 6f 72  S' alias,..** or
29ad4 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69   a sub-select wi
29ad5 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74  th a column as t
29ad6 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c  he return value,
29ad7 20 74 68 65 6e 20 74 68 65 20 0d 0a 2a 2a 20 61   then the ..** a
29ad8 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20  ffinity of that 
29ad9 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e  column is return
29ada 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30  ed. Otherwise, 0
29adb 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c  x00 is returned,
29adc 0d 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  ..** indicating 
29add 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20  no affinity for 
29ade 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0d  the expression..
29adf 0a 2a 2a 0d 0a 2a 2a 20 69 2e 65 2e 20 74 68 65  .**..** i.e. the
29ae0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
29ae1 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68  presssions in th
29ae2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
29ae3 65 6d 65 6e 74 73 20 61 6c 6c 0d 0a 2a 2a 20 68  ements all..** h
29ae4 61 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a  ave an affinity:
29ae5 0d 0a 2a 2a 0d 0a 2a 2a 20 43 52 45 41 54 45 20  ..**..** CREATE 
29ae6 54 41 42 4c 45 20 74 31 28 61 29 3b 0d 0a 2a 2a  TABLE t1(a);..**
29ae7 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
29ae8 31 20 57 48 45 52 45 20 61 3b 0d 0a 2a 2a 20 53  1 WHERE a;..** S
29ae9 45 4c 45 43 54 20 61 20 41 53 20 62 20 46 52 4f  ELECT a AS b FRO
29aea 4d 20 74 31 20 57 48 45 52 45 20 62 3b 0d 0a 2a  M t1 WHERE b;..*
29aeb 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  * SELECT * FROM 
29aec 74 31 20 57 48 45 52 45 20 28 73 65 6c 65 63 74  t1 WHERE (select
29aed 20 61 20 66 72 6f 6d 20 74 31 29 3b 0d 0a 2a 2f   a from t1);..*/
29aee 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
29aef 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70   char sqlite3Exp
29af0 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  rAffinity(Expr *
29af1 70 45 78 70 72 29 7b 0d 0a 20 20 69 6e 74 20 6f  pExpr){..  int o
29af2 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0d 0a  p = pExpr->op;..
29af3 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c    if( op==TK_SEL
29af4 45 43 54 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ECT ){..    asse
29af5 72 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73  rt( pExpr->flags
29af6 26 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b  &EP_xIsSelect );
29af7 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  ..    return sql
29af8 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
29af9 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63  (pExpr->x.pSelec
29afa 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  t->pEList->a[0].
29afb 70 45 78 70 72 29 3b 0d 0a 20 20 7d 0d 0a 23 69  pExpr);..  }..#i
29afc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29afd 54 5f 43 41 53 54 0d 0a 20 20 69 66 28 20 6f 70  T_CAST..  if( op
29afe 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0d 0a 20 20  ==TK_CAST ){..  
29aff 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
29b00 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
29b01 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
29b02 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ;..    return sq
29b03 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
29b04 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  e(pExpr->u.zToke
29b05 6e 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  n);..  }..#endif
29b06 0d 0a 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f  ..  if( (op==TK_
29b07 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70  AGG_COLUMN || op
29b08 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f  ==TK_COLUMN || o
29b09 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20  p==TK_REGISTER) 
29b0a 0d 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e 70  ..   && pExpr->p
29b0b 54 61 62 21 3d 30 0d 0a 20 20 29 7b 0d 0a 20 20  Tab!=0..  ){..  
29b0c 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49    /* op==TK_REGI
29b0d 53 54 45 52 20 26 26 20 70 45 78 70 72 2d 3e 70  STER && pExpr->p
29b0e 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77  Tab!=0 happens w
29b0f 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72  hen pExpr was or
29b10 69 67 69 6e 61 6c 6c 79 0d 0a 20 20 20 20 2a 2a  iginally..    **
29b11 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74   a TK_COLUMN but
29b12 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
29b13 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61  evaluated and ca
29b14 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 74  ched in a regist
29b15 65 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6a  er */..    int j
29b16 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
29b17 6e 3b 0d 0a 20 20 20 20 69 66 28 20 6a 3c 30 20  n;..    if( j<0 
29b18 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
29b19 41 46 46 5f 49 4e 54 45 47 45 52 3b 0d 0a 20 20  AFF_INTEGER;..  
29b1a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
29b1b 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72  >pTab && j<pExpr
29b1c 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0d  ->pTab->nCol );.
29b1d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 45 78 70  .    return pExp
29b1e 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  r->pTab->aCol[j]
29b1f 2e 61 66 66 69 6e 69 74 79 3b 0d 0a 20 20 7d 0d  .affinity;..  }.
29b20 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d  .  return pExpr-
29b21 3e 61 66 66 69 6e 69 74 79 3b 0d 0a 7d 0d 0a 0d  >affinity;..}...
29b22 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
29b23 65 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69  explicit collati
29b24 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
29b25 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  an expression to
29b26 20 74 68 65 0d 0a 2a 2a 20 63 6f 6c 6c 61 74 69   the..** collati
29b27 6e 67 20 73 65 71 75 65 6e 63 65 20 73 75 70 70  ng sequence supp
29b28 6c 69 65 64 20 69 6e 20 74 68 65 20 73 65 63 6f  lied in the seco
29b29 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f  nd argument...*/
29b2a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
29b2b 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
29b2c 70 72 53 65 74 43 6f 6c 6c 28 45 78 70 72 20 2a  prSetColl(Expr *
29b2d 70 45 78 70 72 2c 20 43 6f 6c 6c 53 65 71 20 2a  pExpr, CollSeq *
29b2e 70 43 6f 6c 6c 29 7b 0d 0a 20 20 69 66 28 20 70  pColl){..  if( p
29b2f 45 78 70 72 20 26 26 20 70 43 6f 6c 6c 20 29 7b  Expr && pColl ){
29b30 0d 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f  ..    pExpr->pCo
29b31 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0d 0a 20 20 20  ll = pColl;..   
29b32 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d   pExpr->flags |=
29b33 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0d   EP_ExpCollate;.
29b34 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
29b35 45 78 70 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  Expr;..}..../*..
29b36 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61  ** Set the colla
29b37 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f  ting sequence fo
29b38 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  r expression pEx
29b39 70 72 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6c  pr to be the col
29b3a 6c 61 74 69 6e 67 0d 0a 2a 2a 20 73 65 71 75 65  lating..** seque
29b3b 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 70 54 6f  nce named by pTo
29b3c 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e 20 61 20  ken.   Return a 
29b3d 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
29b3e 65 76 69 73 65 64 20 65 78 70 72 65 73 73 69 6f  evised expressio
29b3f 6e 2e 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61  n...** The colla
29b40 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  ting sequence is
29b41 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 6c   marked as "expl
29b42 69 63 69 74 22 20 75 73 69 6e 67 20 74 68 65 20  icit" using the 
29b43 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0d 0a 2a  EP_ExpCollate..*
29b44 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 6c  * flag.  An expl
29b45 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73  icit collating s
29b46 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 65  equence will ove
29b47 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 0d 0a  rride implicit..
29b48 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
29b49 75 65 6e 63 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  uences...*/..SQL
29b4a 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
29b4b 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74   *sqlite3ExprSet
29b4c 43 6f 6c 6c 42 79 54 6f 6b 65 6e 28 50 61 72 73  CollByToken(Pars
29b4d 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
29b4e 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
29b4f 43 6f 6c 6c 4e 61 6d 65 29 7b 0d 0a 20 20 63 68  CollName){..  ch
29b50 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20  ar *zColl = 0;  
29b51 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71            /* Deq
29b52 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f  uoted name of co
29b53 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
29b54 20 2a 2f 0d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a   */..  CollSeq *
29b55 70 43 6f 6c 6c 3b 0d 0a 20 20 73 71 6c 69 74 65  pColl;..  sqlite
29b56 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
29b57 64 62 3b 0d 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73  db;..  zColl = s
29b58 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
29b59 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d  ken(db, pCollNam
29b5a 65 29 3b 0d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  e);..  pColl = s
29b5b 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
29b5c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  Seq(pParse, zCol
29b5d 6c 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78  l);..  sqlite3Ex
29b5e 70 72 53 65 74 43 6f 6c 6c 28 70 45 78 70 72 2c  prSetColl(pExpr,
29b5f 20 70 43 6f 6c 6c 29 3b 0d 0a 20 20 73 71 6c 69   pColl);..  sqli
29b60 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43  te3DbFree(db, zC
29b61 6f 6c 6c 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  oll);..  return 
29b62 70 45 78 70 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  pExpr;..}..../*.
29b63 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64  .** Return the d
29b64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
29b65 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
29b66 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  e expression pEx
29b67 70 72 2e 20 49 66 0d 0a 2a 2a 20 74 68 65 72 65  pr. If..** there
29b68 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63   is no default c
29b69 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20 72  ollation type, r
29b6a 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f 0d 0a 53 51  eturn 0...*/..SQ
29b6b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
29b6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70  lSeq *sqlite3Exp
29b6d 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  rCollSeq(Parse *
29b6e 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
29b6f 78 70 72 29 7b 0d 0a 20 20 43 6f 6c 6c 53 65 71  xpr){..  CollSeq
29b70 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0d 0a 20 20   *pColl = 0;..  
29b71 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b  Expr *p = pExpr;
29b72 0d 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0d  ..  while( p ){.
29b73 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0d 0a 20 20  .    int op;..  
29b74 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f    pColl = p->pCo
29b75 6c 6c 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 6f  ll;..    if( pCo
29b76 6c 6c 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20  ll ) break;..   
29b77 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0d 0a 20 20   op = p->op;..  
29b78 20 20 69 66 28 20 70 2d 3e 70 54 61 62 21 3d 30    if( p->pTab!=0
29b79 20 26 26 20 28 0d 0a 20 20 20 20 20 20 20 20 6f   && (..        o
29b7a 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
29b7b 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   || op==TK_COLUM
29b7c 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49  N || op==TK_REGI
29b7d 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 54  STER || op==TK_T
29b7e 52 49 47 47 45 52 0d 0a 20 20 20 20 29 29 7b 0d  RIGGER..    )){.
29b7f 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b  .      /* op==TK
29b80 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e  _REGISTER && p->
29b81 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20  pTab!=0 happens 
29b82 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f  when pExpr was o
29b83 72 69 67 69 6e 61 6c 6c 79 0d 0a 20 20 20 20 20  riginally..     
29b84 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   ** a TK_COLUMN 
29b85 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  but was previous
29b86 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  ly evaluated and
29b87 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67   cached in a reg
29b88 69 73 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20  ister */..      
29b89 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
29b8a 6c 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 20  l;..      int j 
29b8b 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a 20  = p->iColumn;.. 
29b8c 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b       if( j>=0 ){
29b8d 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
29b8e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
29b8f 64 62 3b 0d 0a 20 20 20 20 20 20 20 20 7a 43 6f  db;..        zCo
29b90 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43  ll = p->pTab->aC
29b91 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0d 0a 20 20  ol[j].zColl;..  
29b92 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
29b93 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
29b94 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43  (db, ENC(db), zC
29b95 6f 6c 6c 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  oll, 0);..      
29b96 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d    pExpr->pColl =
29b97 20 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 7d   pColl;..      }
29b98 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
29b99 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 6f      }..    if( o
29b9a 70 21 3d 54 4b 5f 43 41 53 54 20 26 26 20 6f 70  p!=TK_CAST && op
29b9b 21 3d 54 4b 5f 55 50 4c 55 53 20 29 7b 0d 0a 20  !=TK_UPLUS ){.. 
29b9c 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
29b9d 20 7d 0d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70   }..    p = p->p
29b9e 4c 65 66 74 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  Left;..  }..  if
29b9f 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  ( sqlite3CheckCo
29ba0 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43  llSeq(pParse, pC
29ba1 6f 6c 6c 29 20 29 7b 20 0d 0a 20 20 20 20 70 43  oll) ){ ..    pC
29ba2 6f 6c 6c 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  oll = 0;..  }.. 
29ba3 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0d 0a   return pColl;..
29ba4 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 70 45 78 70  }..../*..** pExp
29ba5 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20  r is an operand 
29ba6 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  of a comparison 
29ba7 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20  operator.  aff2 
29ba8 69 73 20 74 68 65 0d 0a 2a 2a 20 74 79 70 65 20  is the..** type 
29ba9 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
29baa 6f 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20  other operand.  
29bab 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
29bac 75 72 6e 73 20 74 68 65 0d 0a 2a 2a 20 74 79 70  urns the..** typ
29bad 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  e affinity that 
29bae 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66  should be used f
29baf 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  or the compariso
29bb0 6e 20 6f 70 65 72 61 74 6f 72 2e 0d 0a 2a 2f 0d  n operator...*/.
29bb1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29bb2 63 68 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70  char sqlite3Comp
29bb3 61 72 65 41 66 66 69 6e 69 74 79 28 45 78 70 72  areAffinity(Expr
29bb4 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 61 66   *pExpr, char af
29bb5 66 32 29 7b 0d 0a 20 20 63 68 61 72 20 61 66 66  f2){..  char aff
29bb6 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  1 = sqlite3ExprA
29bb7 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0d  ffinity(pExpr);.
29bb8 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 20 61  .  if( aff1 && a
29bb9 66 66 32 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 42  ff2 ){..    /* B
29bba 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 65  oth sides of the
29bbb 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 20   comparison are 
29bbc 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 20  columns. If one 
29bbd 68 61 73 20 6e 75 6d 65 72 69 63 0d 0a 20 20 20  has numeric..   
29bbe 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73   ** affinity, us
29bbf 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73  e that. Otherwis
29bc0 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74  e use no affinit
29bc1 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  y...    */..    
29bc2 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  if( sqlite3IsNum
29bc3 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66  ericAffinity(aff
29bc4 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e  1) || sqlite3IsN
29bc5 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61  umericAffinity(a
29bc6 66 66 32 29 20 29 7b 0d 0a 20 20 20 20 20 20 72  ff2) ){..      r
29bc7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46  eturn SQLITE_AFF
29bc8 5f 4e 55 4d 45 52 49 43 3b 0d 0a 20 20 20 20 7d  _NUMERIC;..    }
29bc9 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 65 74  else{..      ret
29bca 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
29bcb 4f 4e 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ONE;..    }..  }
29bcc 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 26  else if( !aff1 &
29bcd 26 20 21 61 66 66 32 20 29 7b 0d 0a 20 20 20 20  & !aff2 ){..    
29bce 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20  /* Neither side 
29bcf 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
29bd0 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20  n is a column.  
29bd1 43 6f 6d 70 61 72 65 20 74 68 65 0d 0a 20 20 20  Compare the..   
29bd2 20 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65   ** results dire
29bd3 63 74 6c 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  ctly...    */.. 
29bd4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
29bd5 5f 41 46 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 7d 65  _AFF_NONE;..  }e
29bd6 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 4f 6e 65  lse{..    /* One
29bd7 20 73 69 64 65 20 69 73 20 61 20 63 6f 6c 75 6d   side is a colum
29bd8 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 69 73 20  n, the other is 
29bd9 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 63 6f 6c  not. Use the col
29bda 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 2e 20 2a  umns affinity. *
29bdb 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  /..    assert( a
29bdc 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 3d 3d  ff1==0 || aff2==
29bdd 30 20 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  0 );..    return
29bde 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b 0d   (aff1 + aff2);.
29bdf 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
29be0 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d  * pExpr is a com
29be1 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
29be2 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 79  .  Return the ty
29be3 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74  pe affinity that
29be4 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 62 65 20 61   should..** be a
29be5 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f  pplied to both o
29be6 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f  perands prior to
29be7 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61   doing the compa
29be8 72 69 73 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  rison...*/..stat
29be9 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73  ic char comparis
29bea 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  onAffinity(Expr 
29beb 2a 70 45 78 70 72 29 7b 0d 0a 20 20 63 68 61 72  *pExpr){..  char
29bec 20 61 66 66 3b 0d 0a 20 20 61 73 73 65 72 74 28   aff;..  assert(
29bed 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45   pExpr->op==TK_E
29bee 51 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  Q || pExpr->op==
29bef 54 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e  TK_IN || pExpr->
29bf0 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0d 0a 20 20  op==TK_LT ||..  
29bf1 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
29bf2 70 3d 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70  p==TK_GT || pExp
29bf3 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20  r->op==TK_GE || 
29bf4 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45  pExpr->op==TK_LE
29bf5 20 7c 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 70   ||..          p
29bf6 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20  Expr->op==TK_NE 
29bf7 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
29bf8 5f 49 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _IS || pExpr->op
29bf9 3d 3d 54 4b 5f 49 53 4e 4f 54 20 29 3b 0d 0a 20  ==TK_ISNOT );.. 
29bfa 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
29bfb 70 4c 65 66 74 20 29 3b 0d 0a 20 20 61 66 66 20  pLeft );..  aff 
29bfc 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
29bfd 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 4c 65  inity(pExpr->pLe
29bfe 66 74 29 3b 0d 0a 20 20 69 66 28 20 70 45 78 70  ft);..  if( pExp
29bff 72 2d 3e 70 52 69 67 68 74 20 29 7b 0d 0a 20 20  r->pRight ){..  
29c00 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
29c01 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
29c02 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66  Expr->pRight, af
29c03 66 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  f);..  }else if(
29c04 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
29c05 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
29c06 6c 65 63 74 29 20 29 7b 0d 0a 20 20 20 20 61 66  lect) ){..    af
29c07 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  f = sqlite3Compa
29c08 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  reAffinity(pExpr
29c09 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  ->x.pSelect->pEL
29c0a 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c  ist->a[0].pExpr,
29c0b 20 61 66 66 29 3b 0d 0a 20 20 7d 65 6c 73 65 20   aff);..  }else 
29c0c 69 66 28 20 21 61 66 66 20 29 7b 0d 0a 20 20 20  if( !aff ){..   
29c0d 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
29c0e 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 7d 0d 0a 20 20  F_NONE;..  }..  
29c0f 72 65 74 75 72 6e 20 61 66 66 3b 0d 0a 7d 0d 0a  return aff;..}..
29c10 0d 0a 2f 2a 0d 0a 2a 2a 20 70 45 78 70 72 20 69  ../*..** pExpr i
29c11 73 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65  s a comparison e
29c12 78 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27  xpression, eg. '
29c13 3d 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29  =', '<', IN(...)
29c14 20 65 74 63 2e 0d 0a 2a 2a 20 69 64 78 5f 61 66   etc...** idx_af
29c15 66 69 6e 69 74 79 20 69 73 20 74 68 65 20 61 66  finity is the af
29c16 66 69 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64  finity of an ind
29c17 65 78 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74  exed column. Ret
29c18 75 72 6e 20 74 72 75 65 0d 0a 2a 2a 20 69 66 20  urn true..** if 
29c19 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 61  the index with a
29c1a 66 66 69 6e 69 74 79 20 69 64 78 5f 61 66 66 69  ffinity idx_affi
29c1b 6e 69 74 79 20 6d 61 79 20 62 65 20 75 73 65 64  nity may be used
29c1c 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0d 0a 2a   to implement..*
29c1d 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  * the comparison
29c1e 20 69 6e 20 70 45 78 70 72 2e 0d 0a 2a 2f 0d 0a   in pExpr...*/..
29c1f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
29c20 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  nt sqlite3IndexA
29c21 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a  ffinityOk(Expr *
29c22 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f  pExpr, char idx_
29c23 61 66 66 69 6e 69 74 79 29 7b 0d 0a 20 20 63 68  affinity){..  ch
29c24 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69  ar aff = compari
29c25 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
29c26 72 29 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 61  r);..  switch( a
29c27 66 66 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  ff ){..    case 
29c28 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a  SQLITE_AFF_NONE:
29c29 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ..      return 1
29c2a 3b 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ;..    case SQLI
29c2b 54 45 5f 41 46 46 5f 54 45 58 54 3a 0d 0a 20 20  TE_AFF_TEXT:..  
29c2c 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f 61      return idx_a
29c2d 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
29c2e 41 46 46 5f 54 45 58 54 3b 0d 0a 20 20 20 20 64  AFF_TEXT;..    d
29c2f 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20 20 20 72  efault:..      r
29c30 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e  eturn sqlite3IsN
29c31 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69  umericAffinity(i
29c32 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0d 0a 20  dx_affinity);.. 
29c33 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
29c34 52 65 74 75 72 6e 20 74 68 65 20 50 35 20 76 61  Return the P5 va
29c35 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20  lue that should 
29c36 62 65 20 75 73 65 64 20 66 6f 72 20 61 20 62 69  be used for a bi
29c37 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0d  nary comparison.
29c38 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f 50 5f 45  .** opcode (OP_E
29c39 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e 29 20 75  q, OP_Ge etc.) u
29c3a 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 70  sed to compare p
29c3b 45 78 70 72 31 20 61 6e 64 20 70 45 78 70 72 32  Expr1 and pExpr2
29c3c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 75 38  ...*/..static u8
29c3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35   binaryCompareP5
29c3e 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 45  (Expr *pExpr1, E
29c3f 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e 74  xpr *pExpr2, int
29c40 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0d 0a 20   jumpIfNull){.. 
29c41 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 29   u8 aff = (char)
29c42 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
29c43 69 74 79 28 70 45 78 70 72 32 29 3b 0d 0a 20 20  ity(pExpr2);..  
29c44 61 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65  aff = (u8)sqlite
29c45 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
29c46 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20  (pExpr1, aff) | 
29c47 28 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0d  (u8)jumpIfNull;.
29c48 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0d 0a  .  return aff;..
29c49 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
29c4a 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
29c4b 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
29c4c 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75  quence that shou
29c4d 6c 64 20 62 65 20 75 73 65 64 20 62 79 0d 0a 2a  ld be used by..*
29c4e 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61  * a binary compa
29c4f 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63  rison operator c
29c50 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61  omparing pLeft a
29c51 6e 64 20 70 52 69 67 68 74 2e 0d 0a 2a 2a 0d 0a  nd pRight...**..
29c52 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20 68  ** If the left h
29c53 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68  and expression h
29c54 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73  as a collating s
29c55 65 71 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68  equence type, th
29c56 65 6e 20 69 74 20 69 73 0d 0a 2a 2a 20 75 73 65  en it is..** use
29c57 64 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  d. Otherwise the
29c58 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
29c59 6e 63 65 20 66 6f 72 20 74 68 65 20 72 69 67 68  nce for the righ
29c5a 74 20 68 61 6e 64 20 65 78 70 72 65 73 73 69 6f  t hand expressio
29c5b 6e 0d 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 6f  n..** is used, o
29c5c 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 28 42  r the default (B
29c5d 49 4e 41 52 59 29 20 69 66 20 6e 65 69 74 68 65  INARY) if neithe
29c5e 72 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  r expression has
29c5f 20 61 20 63 6f 6c 6c 61 74 69 6e 67 0d 0a 2a 2a   a collating..**
29c60 20 74 79 70 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41   type...**..** A
29c61 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20 28  rgument pRight (
29c62 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20 6d  but not pLeft) m
29c63 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69  ay be a null poi
29c64 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61  nter. In this ca
29c65 73 65 2c 0d 0a 2a 2a 20 69 74 20 69 73 20 6e 6f  se,..** it is no
29c66 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0d 0a 2a  t considered...*
29c67 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
29c68 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
29c69 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
29c6a 6f 6c 6c 53 65 71 28 0d 0a 20 20 50 61 72 73 65  ollSeq(..  Parse
29c6b 20 2a 70 50 61 72 73 65 2c 20 0d 0a 20 20 45 78   *pParse, ..  Ex
29c6c 70 72 20 2a 70 4c 65 66 74 2c 20 0d 0a 20 20 45  pr *pLeft, ..  E
29c6d 78 70 72 20 2a 70 52 69 67 68 74 0d 0a 29 7b 0d  xpr *pRight..){.
29c6e 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
29c6f 6c 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4c  l;..  assert( pL
29c70 65 66 74 20 29 3b 0d 0a 20 20 69 66 28 20 70 4c  eft );..  if( pL
29c71 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  eft->flags & EP_
29c72 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0d 0a 20  ExpCollate ){.. 
29c73 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
29c74 2d 3e 70 43 6f 6c 6c 20 29 3b 0d 0a 20 20 20 20  ->pColl );..    
29c75 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70  pColl = pLeft->p
29c76 43 6f 6c 6c 3b 0d 0a 20 20 7d 65 6c 73 65 20 69  Coll;..  }else i
29c77 66 28 20 70 52 69 67 68 74 20 26 26 20 70 52 69  f( pRight && pRi
29c78 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ght->flags & EP_
29c79 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0d 0a 20  ExpCollate ){.. 
29c7a 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68     assert( pRigh
29c7b 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0d 0a 20 20 20  t->pColl );..   
29c7c 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d   pColl = pRight-
29c7d 3e 70 43 6f 6c 6c 3b 0d 0a 20 20 7d 65 6c 73 65  >pColl;..  }else
29c7e 7b 0d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  {..    pColl = s
29c7f 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
29c80 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29  q(pParse, pLeft)
29c81 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c  ;..    if( !pCol
29c82 6c 20 29 7b 0d 0a 20 20 20 20 20 20 70 43 6f 6c  l ){..      pCol
29c83 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
29c84 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
29c85 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a  Right);..    }..
29c86 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 43    }..  return pC
29c87 6f 6c 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  oll;..}..../*..*
29c88 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
29c89 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  for a comparison
29c8a 20 6f 70 65 72 61 74 6f 72 2e 0d 0a 2a 2f 0d 0a   operator...*/..
29c8b 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 43  static int codeC
29c8c 6f 6d 70 61 72 65 28 0d 0a 20 20 50 61 72 73 65  ompare(..  Parse
29c8d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20   *pParse,    /* 
29c8e 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64  The parsing (and
29c8f 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
29c90 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  ) context */..  
29c91 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
29c92 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
29c93 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 45 78 70 72  erand */..  Expr
29c94 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a   *pRight,     /*
29c95 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   The right opera
29c96 6e 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70 63  nd */..  int opc
29c97 6f 64 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  ode,       /* Th
29c98 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63  e comparison opc
29c99 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 6e  ode */..  int in
29c9a 31 2c 20 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52  1, int in2, /* R
29c9b 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
29c9c 6f 70 65 72 61 6e 64 73 20 2a 2f 0d 0a 20 20 69  operands */..  i
29c9d 6e 74 20 64 65 73 74 2c 20 20 20 20 20 20 20 20  nt dest,        
29c9e 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
29c9f 20 74 72 75 65 2e 20 20 2a 2f 0d 0a 20 20 69 6e   true.  */..  in
29ca0 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20  t jumpIfNull    
29ca1 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a 75 6d 70  /* If true, jump
29ca2 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61   if either opera
29ca3 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0d 0a 29  nd is NULL */..)
29ca4 7b 0d 0a 20 20 69 6e 74 20 70 35 3b 0d 0a 20 20  {..  int p5;..  
29ca5 69 6e 74 20 61 64 64 72 3b 0d 0a 20 20 43 6f 6c  int addr;..  Col
29ca6 6c 53 65 71 20 2a 70 34 3b 0d 0a 0d 0a 20 20 70  lSeq *p4;....  p
29ca7 34 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72  4 = sqlite3Binar
29ca8 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
29ca9 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70  pParse, pLeft, p
29caa 52 69 67 68 74 29 3b 0d 0a 20 20 70 35 20 3d 20  Right);..  p5 = 
29cab 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28  binaryCompareP5(
29cac 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a  pLeft, pRight, j
29cad 75 6d 70 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 61  umpIfNull);..  a
29cae 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
29caf 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d 3e  eAddOp4(pParse->
29cb0 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69  pVdbe, opcode, i
29cb1 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0d 0a  n2, dest, in1,..
29cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29cb3 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
29cb4 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51  *)p4, P4_COLLSEQ
29cb5 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
29cb6 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65  eChangeP5(pParse
29cb7 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29  ->pVdbe, (u8)p5)
29cb8 3b 0d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72  ;..  return addr
29cb9 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20 53 51 4c 49  ;..}....#if SQLI
29cba 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
29cbb 48 3e 30 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63  H>0../*..** Chec
29cbc 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20  k that argument 
29cbd 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 73 20  nHeight is less 
29cbe 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
29cbf 20 74 68 65 20 6d 61 78 69 6d 75 6d 0d 0a 2a 2a   the maximum..**
29cc0 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74   expression dept
29cc1 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74  h allowed. If it
29cc2 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61   is not, leave a
29cc3 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
29cc4 69 6e 0d 0a 2a 2a 20 70 50 61 72 73 65 2e 0d 0a  in..** pParse...
29cc5 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
29cc6 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
29cc7 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61  prCheckHeight(Pa
29cc8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
29cc9 20 6e 48 65 69 67 68 74 29 7b 0d 0a 20 20 69 6e   nHeight){..  in
29cca 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
29ccb 3b 0d 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68  ;..  int mxHeigh
29ccc 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  t = pParse->db->
29ccd 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
29cce 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b  MIT_EXPR_DEPTH];
29ccf 0d 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e  ..  if( nHeight>
29cd0 6d 78 48 65 69 67 68 74 20 29 7b 0d 0a 20 20 20  mxHeight ){..   
29cd1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
29cd2 28 70 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20  (pParse, ..     
29cd3 20 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72    "Expression tr
29cd4 65 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20  ee is too large 
29cd5 28 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25  (maximum depth %
29cd6 64 29 22 2c 20 6d 78 48 65 69 67 68 74 0d 0a 20  d)", mxHeight.. 
29cd7 20 20 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20     );..    rc = 
29cd8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
29cd9 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
29cda 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68 65 20 66 6f  ..}..../* The fo
29cdb 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75  llowing three fu
29cdc 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 4f  nctions, heightO
29cdd 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 74 4f  fExpr(), heightO
29cde 66 45 78 70 72 4c 69 73 74 28 29 0d 0a 2a 2a 20  fExprList()..** 
29cdf 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65  and heightOfSele
29ce0 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 74  ct(), are used t
29ce1 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
29ce2 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0d 0a  maximum height..
29ce3 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73  ** of any expres
29ce4 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65  sion tree refere
29ce5 6e 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75  nced by the stru
29ce6 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20  cture passed as 
29ce7 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 61 72  the..** first ar
29ce8 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  gument...**..** 
29ce9 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20  If this maximum 
29cea 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
29ceb 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65  r than the curre
29cec 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  nt value pointed
29ced 0d 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69  ..** to by pnHei
29cee 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  ght, the second 
29cef 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20  parameter, then 
29cf0 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f  set *pnHeight to
29cf1 20 74 68 61 74 0d 0a 2a 2a 20 76 61 6c 75 65 2e   that..** value.
29cf2 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
29cf3 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 45  d heightOfExpr(E
29cf4 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48  xpr *p, int *pnH
29cf5 65 69 67 68 74 29 7b 0d 0a 20 20 69 66 28 20 70  eight){..  if( p
29cf6 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e   ){..    if( p->
29cf7 6e 48 65 69 67 68 74 3e 2a 70 6e 48 65 69 67 68  nHeight>*pnHeigh
29cf8 74 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 6e 48  t ){..      *pnH
29cf9 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 65 69 67  eight = p->nHeig
29cfa 68 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ht;..    }..  }.
29cfb 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
29cfc 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
29cfd 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e  (ExprList *p, in
29cfe 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0d 0a 20  t *pnHeight){.. 
29cff 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 69   if( p ){..    i
29d00 6e 74 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69  nt i;..    for(i
29d01 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20  =0; i<p->nExpr; 
29d02 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 68 65 69  i++){..      hei
29d03 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69  ghtOfExpr(p->a[i
29d04 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68  ].pExpr, pnHeigh
29d05 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  t);..    }..  }.
29d06 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
29d07 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53  heightOfSelect(S
29d08 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70  elect *p, int *p
29d09 6e 48 65 69 67 68 74 29 7b 0d 0a 20 20 69 66 28  nHeight){..  if(
29d0a 20 70 20 29 7b 0d 0a 20 20 20 20 68 65 69 67 68   p ){..    heigh
29d0b 74 4f 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72  tOfExpr(p->pWher
29d0c 65 2c 20 70 6e 48 65 69 67 68 74 29 3b 0d 0a 20  e, pnHeight);.. 
29d0d 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28     heightOfExpr(
29d0e 70 2d 3e 70 48 61 76 69 6e 67 2c 20 70 6e 48 65  p->pHaving, pnHe
29d0f 69 67 68 74 29 3b 0d 0a 20 20 20 20 68 65 69 67  ight);..    heig
29d10 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 69 6d  htOfExpr(p->pLim
29d11 69 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0d 0a  it, pnHeight);..
29d12 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
29d13 28 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 6e 48  (p->pOffset, pnH
29d14 65 69 67 68 74 29 3b 0d 0a 20 20 20 20 68 65 69  eight);..    hei
29d15 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
29d16 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 68  >pEList, pnHeigh
29d17 74 29 3b 0d 0a 20 20 20 20 68 65 69 67 68 74 4f  t);..    heightO
29d18 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72  fExprList(p->pGr
29d19 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68 74 29  oupBy, pnHeight)
29d1a 3b 0d 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  ;..    heightOfE
29d1b 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65  xprList(p->pOrde
29d1c 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0d  rBy, pnHeight);.
29d1d 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c  .    heightOfSel
29d1e 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70  ect(p->pPrior, p
29d1f 6e 48 65 69 67 68 74 29 3b 0d 0a 20 20 7d 0d 0a  nHeight);..  }..
29d20 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20  }..../*..** Set 
29d21 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74  the Expr.nHeight
29d22 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65   variable in the
29d23 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
29d24 64 20 61 73 20 61 6e 20 0d 0a 2a 2a 20 61 72 67  d as an ..** arg
29d25 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73  ument. An expres
29d26 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69  sion with no chi
29d27 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73  ldren, Expr.pLis
29d28 74 20 6f 72 20 0d 0a 2a 2a 20 45 78 70 72 2e 70  t or ..** Expr.p
29d29 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61  Select member ha
29d2a 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e  s a height of 1.
29d2b 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65   Any other expre
29d2c 73 73 69 6f 6e 0d 0a 2a 2a 20 68 61 73 20 61 20  ssion..** has a 
29d2d 68 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20  height equal to 
29d2e 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
29d2f 68 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20  ht of any other 
29d30 0d 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20  ..** referenced 
29d31 45 78 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0d 0a  Expr plus one...
29d32 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
29d33 65 78 70 72 53 65 74 48 65 69 67 68 74 28 45 78  exprSetHeight(Ex
29d34 70 72 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 6e  pr *p){..  int n
29d35 48 65 69 67 68 74 20 3d 20 30 3b 0d 0a 20 20 68  Height = 0;..  h
29d36 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
29d37 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 74 29 3b  Left, &nHeight);
29d38 0d 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72  ..  heightOfExpr
29d39 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e 48 65  (p->pRight, &nHe
29d3a 69 67 68 74 29 3b 0d 0a 20 20 69 66 28 20 45 78  ight);..  if( Ex
29d3b 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
29d3c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
29d3d 7b 0d 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53  {..    heightOfS
29d3e 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65  elect(p->x.pSele
29d3f 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0d 0a  ct, &nHeight);..
29d40 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 68 65    }else{..    he
29d41 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
29d42 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69  ->x.pList, &nHei
29d43 67 68 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d  ght);..  }..  p-
29d44 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67  >nHeight = nHeig
29d45 68 74 20 2b 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ht + 1;..}..../*
29d46 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70  ..** Set the Exp
29d47 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62  r.nHeight variab
29d48 6c 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 70  le using the exp
29d49 72 53 65 74 48 65 69 67 68 74 28 29 20 66 75 6e  rSetHeight() fun
29d4a 63 74 69 6f 6e 2e 20 49 66 0d 0a 2a 2a 20 74 68  ction. If..** th
29d4b 65 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61  e height is grea
29d4c 74 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ter than the max
29d4d 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70  imum allowed exp
29d4e 72 65 73 73 69 6f 6e 20 64 65 70 74 68 2c 0d 0a  ression depth,..
29d4f 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  ** leave an erro
29d50 72 20 69 6e 20 70 50 61 72 73 65 2e 0d 0a 2a 2f  r in pParse...*/
29d51 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
29d52 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
29d53 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73 65  rSetHeight(Parse
29d54 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
29d55 70 29 7b 0d 0a 20 20 65 78 70 72 53 65 74 48 65  p){..  exprSetHe
29d56 69 67 68 74 28 70 29 3b 0d 0a 20 20 73 71 6c 69  ight(p);..  sqli
29d57 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
29d58 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48  ht(pParse, p->nH
29d59 65 69 67 68 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  eight);..}..../*
29d5a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
29d5b 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f  maximum height o
29d5c 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e  f any expression
29d5d 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64   tree referenced
29d5e 0d 0a 2a 2a 20 62 79 20 74 68 65 20 73 65 6c 65  ..** by the sele
29d5f 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73  ct statement pas
29d60 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  sed as an argume
29d61 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  nt...*/..SQLITE_
29d62 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29d63 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69  te3SelectExprHei
29d64 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0d  ght(Select *p){.
29d65 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d  .  int nHeight =
29d66 20 30 3b 0d 0a 20 20 68 65 69 67 68 74 4f 66 53   0;..  heightOfS
29d67 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68  elect(p, &nHeigh
29d68 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6e 48  t);..  return nH
29d69 65 69 67 68 74 3b 0d 0a 7d 0d 0a 23 65 6c 73 65  eight;..}..#else
29d6a 0d 0a 20 20 23 64 65 66 69 6e 65 20 65 78 70 72  ..  #define expr
29d6b 53 65 74 48 65 69 67 68 74 28 79 29 0d 0a 23 65  SetHeight(y)..#e
29d6c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
29d6d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 20  AX_EXPR_DEPTH>0 
29d6e 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  */..../*..** Thi
29d6f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
29d70 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f 72 20   core allocator 
29d71 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 2e 0d  for Expr nodes..
29d72 0a 2a 2a 0d 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  .**..** Construc
29d73 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69  t a new expressi
29d74 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75  on node and retu
29d75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
29d76 69 74 2e 20 20 4d 65 6d 6f 72 79 0d 0a 2a 2a 20  it.  Memory..** 
29d77 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e  for this node an
29d78 64 20 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e  d for the pToken
29d79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73   argument is a s
29d7a 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  ingle allocation
29d7b 0d 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72  ..** obtained fr
29d7c 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  om sqlite3DbMall
29d7d 6f 63 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69  oc().  The calli
29d7e 6e 67 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20  ng function..** 
29d7f 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
29d80 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74  or making sure t
29d81 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c  he node eventual
29d82 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e 0d 0a  ly gets freed...
29d83 2a 2a 0d 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74  **..** If dequot
29d84 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  e is true, then 
29d85 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74  the token (if it
29d86 20 65 78 69 73 74 73 29 20 69 73 20 64 65 71 75   exists) is dequ
29d87 6f 74 65 64 2e 0d 0a 2a 2a 20 49 66 20 64 65 71  oted...** If deq
29d88 75 6f 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e  uote is false, n
29d89 6f 20 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70  o dequoting is p
29d8a 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65  erformance.  The
29d8b 20 64 65 51 75 6f 74 65 0d 0a 2a 2a 20 70 61 72   deQuote..** par
29d8c 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65  ameter is ignore
29d8d 64 20 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e  d if pToken is N
29d8e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 74 6f  ULL or if the to
29d8f 6b 65 6e 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a 2a  ken does not..**
29d90 20 61 70 70 65 61 72 20 74 6f 20 62 65 20 71 75   appear to be qu
29d91 6f 74 65 64 2e 20 20 49 66 20 74 68 65 20 71 75  oted.  If the qu
29d92 6f 74 65 73 20 77 65 72 65 20 6f 66 20 74 68 65  otes were of the
29d93 20 66 6f 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75   form "..." (dou
29d94 62 6c 65 2d 71 75 6f 74 65 73 29 0d 0a 2a 2a 20  ble-quotes)..** 
29d95 74 68 65 6e 20 74 68 65 20 45 50 5f 44 62 6c 51  then the EP_DblQ
29d96 75 6f 74 65 64 20 66 6c 61 67 20 69 73 20 73 65  uoted flag is se
29d97 74 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73  t on the express
29d98 69 6f 6e 20 6e 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a  ion node...**..*
29d99 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
29d9a 20 49 66 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47   If op==TK_INTEG
29d9b 45 52 20 61 6e 64 20 70 54 6f 6b 65 6e 20 70 6f  ER and pToken po
29d9c 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67  ints to a string
29d9d 20 74 68 61 74 0d 0a 2a 2a 20 63 61 6e 20 62 65   that..** can be
29d9e 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
29d9f 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
29da0 72 2c 20 74 68 65 6e 20 74 68 65 20 74 6f 6b 65  r, then the toke
29da1 6e 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 73 74 6f  n is not..** sto
29da2 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b 65 6e 2e  red in u.zToken.
29da3 20 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 69    Instead, the i
29da4 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 69 73  nteger values is
29da5 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 69 6e 74   written..** int
29da6 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 20 74  o u.iValue and t
29da7 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 66  he EP_IntValue f
29da8 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 6f 20  lag is set.  No 
29da9 65 78 74 72 61 20 73 74 6f 72 61 67 65 0d 0a 2a  extra storage..*
29daa 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74  * is allocated t
29dab 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 67  o hold the integ
29dac 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 20  er text and the 
29dad 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 20  dequote flag is 
29dae 69 67 6e 6f 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  ignored...*/..SQ
29daf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
29db0 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c  r *sqlite3ExprAl
29db1 6c 6f 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  loc(..  sqlite3 
29db2 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
29db3 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71  /* Handle for sq
29db4 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
29db5 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c  o() (may be null
29db6 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70 2c 20  ) */..  int op, 
29db7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29db8 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70  /* Expression op
29db9 63 6f 64 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  code */..  const
29dba 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20   Token *pToken, 
29dbb 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75     /* Token argu
29dbc 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20  ment.  Might be 
29dbd 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e 74 20 64  NULL */..  int d
29dbe 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 20  equote          
29dbf 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65     /* True to de
29dc0 71 75 6f 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  quote */..){..  
29dc1 45 78 70 72 20 2a 70 4e 65 77 3b 0d 0a 20 20 69  Expr *pNew;..  i
29dc2 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 0d 0a  nt nExtra = 0;..
29dc3 20 20 69 6e 74 20 69 56 61 6c 75 65 20 3d 20 30    int iValue = 0
29dc4 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 54 6f 6b 65  ;....  if( pToke
29dc5 6e 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 6f 70  n ){..    if( op
29dc6 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20  !=TK_INTEGER || 
29dc7 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 0d 0a 20 20  pToken->z==0..  
29dc8 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
29dc9 65 33 47 65 74 49 6e 74 33 32 28 70 54 6f 6b 65  e3GetInt32(pToke
29dca 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 29 3d 3d  n->z, &iValue)==
29dcb 30 20 29 7b 0d 0a 20 20 20 20 20 20 6e 45 78 74  0 ){..      nExt
29dcc 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 2b 31  ra = pToken->n+1
29dcd 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
29dce 20 69 56 61 6c 75 65 3e 3d 30 20 29 3b 0d 0a 20   iValue>=0 );.. 
29dcf 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70 4e 65     }..  }..  pNe
29dd0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
29dd1 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
29dd2 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72 61 29  of(Expr)+nExtra)
29dd3 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  ;..  if( pNew ){
29dd4 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d  ..    pNew->op =
29dd5 20 28 75 38 29 6f 70 3b 0d 0a 20 20 20 20 70 4e   (u8)op;..    pN
29dd6 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0d 0a  ew->iAgg = -1;..
29dd7 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29      if( pToken )
29dd8 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78  {..      if( nEx
29dd9 74 72 61 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  tra==0 ){..     
29dda 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c     pNew->flags |
29ddb 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0d 0a  = EP_IntValue;..
29ddc 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e          pNew->u.
29ddd 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 65 3b  iValue = iValue;
29dde 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
29ddf 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0d 0a          int c;..
29de0 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e          pNew->u.
29de1 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29  zToken = (char*)
29de2 26 70 4e 65 77 5b 31 5d 3b 0d 0a 20 20 20 20 20  &pNew[1];..     
29de3 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65     assert( pToke
29de4 6e 2d 3e 7a 21 3d 30 20 7c 7c 20 70 54 6f 6b 65  n->z!=0 || pToke
29de5 6e 2d 3e 6e 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  n->n==0 );..    
29de6 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e      if( pToken->
29de7 6e 20 29 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d  n ) memcpy(pNew-
29de8 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65  >u.zToken, pToke
29de9 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29  n->z, pToken->n)
29dea 3b 0d 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ;..        pNew-
29deb 3e 75 2e 7a 54 6f 6b 65 6e 5b 70 54 6f 6b 65 6e  >u.zToken[pToken
29dec 2d 3e 6e 5d 20 3d 20 30 3b 0d 0a 20 20 20 20 20  ->n] = 0;..     
29ded 20 20 20 69 66 28 20 64 65 71 75 6f 74 65 20 26     if( dequote &
29dee 26 20 6e 45 78 74 72 61 3e 3d 33 20 0d 0a 20 20  & nExtra>=3 ..  
29def 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 28             && ((
29df0 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d  c = pToken->z[0]
29df1 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d 3d 27 22  )=='\'' || c=='"
29df2 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c 7c 20 63  ' || c=='[' || c
29df3 3d 3d 27 60 27 29 20 29 7b 0d 0a 20 20 20 20 20  =='`') ){..     
29df4 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75       sqlite3Dequ
29df5 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b  ote(pNew->u.zTok
29df6 65 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  en);..          
29df7 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e 65  if( c=='"' ) pNe
29df8 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44  w->flags |= EP_D
29df9 62 6c 51 75 6f 74 65 64 3b 0d 0a 20 20 20 20 20  blQuoted;..     
29dfa 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
29dfb 20 20 20 7d 0d 0a 23 69 66 20 53 51 4c 49 54 45     }..#if SQLITE
29dfc 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
29dfd 30 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 48 65  0..    pNew->nHe
29dfe 69 67 68 74 20 3d 20 31 3b 0d 0a 23 65 6e 64 69  ight = 1;..#endi
29dff 66 20 20 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  f  ..  }..  retu
29e00 72 6e 20 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f  rn pNew;..}..../
29e01 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  *..** Allocate a
29e02 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
29e03 6e 6f 64 65 20 66 72 6f 6d 20 61 20 7a 65 72 6f  node from a zero
29e04 2d 74 65 72 6d 69 6e 61 74 65 64 20 74 6f 6b 65  -terminated toke
29e05 6e 20 74 68 61 74 20 68 61 73 0d 0a 2a 2a 20 61  n that has..** a
29e06 6c 72 65 61 64 79 20 62 65 65 6e 20 64 65 71 75  lready been dequ
29e07 6f 74 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  oted...*/..SQLIT
29e08 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
29e09 73 71 6c 69 74 65 33 45 78 70 72 28 0d 0a 20 20  sqlite3Expr(..  
29e0a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
29e0b 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
29e0c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62 4d  e for sqlite3DbM
29e0d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 79  allocZero() (may
29e0e 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0d 0a 20 20   be null) */..  
29e0f 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
29e10 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
29e11 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0d  ssion opcode */.
29e12 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
29e13 54 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f  Token      /* To
29e14 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d  ken argument.  M
29e15 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0d  ight be NULL */.
29e16 0a 29 7b 0d 0a 20 20 54 6f 6b 65 6e 20 78 3b 0d  .){..  Token x;.
29e17 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b  .  x.z = zToken;
29e18 0d 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e  ..  x.n = zToken
29e19 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   ? sqlite3Strlen
29e1a 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b 0d  30(zToken) : 0;.
29e1b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
29e1c 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 6f  3ExprAlloc(db, o
29e1d 70 2c 20 26 78 2c 20 30 29 3b 0d 0a 7d 0d 0a 0d  p, &x, 0);..}...
29e1e 0a 2f 2a 0d 0a 2a 2a 20 41 74 74 61 63 68 20 73  ./*..** Attach s
29e1f 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 6e  ubtrees pLeft an
29e20 64 20 70 52 69 67 68 74 20 74 6f 20 74 68 65 20  d pRight to the 
29e21 45 78 70 72 20 6e 6f 64 65 20 70 52 6f 6f 74 2e  Expr node pRoot.
29e22 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 52 6f 6f  ..**..** If pRoo
29e23 74 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61  t==NULL that mea
29e24 6e 73 20 74 68 61 74 20 61 20 6d 65 6d 6f 72 79  ns that a memory
29e25 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
29e26 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0d  r has occurred..
29e27 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 61 73 65  .** In that case
29e28 2c 20 64 65 6c 65 74 65 20 74 68 65 20 73 75 62  , delete the sub
29e29 74 72 65 65 73 20 70 4c 65 66 74 20 61 6e 64 20  trees pLeft and 
29e2a 70 52 69 67 68 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  pRight...*/..SQL
29e2b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
29e2c 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61   sqlite3ExprAtta
29e2d 63 68 53 75 62 74 72 65 65 73 28 0d 0a 20 20 73  chSubtrees(..  s
29e2e 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20 20 45  qlite3 *db,..  E
29e2f 78 70 72 20 2a 70 52 6f 6f 74 2c 0d 0a 20 20 45  xpr *pRoot,..  E
29e30 78 70 72 20 2a 70 4c 65 66 74 2c 0d 0a 20 20 45  xpr *pLeft,..  E
29e31 78 70 72 20 2a 70 52 69 67 68 74 0d 0a 29 7b 0d  xpr *pRight..){.
29e32 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d 30 20  .  if( pRoot==0 
29e33 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
29e34 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29e35 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   );..    sqlite3
29e36 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
29e37 4c 65 66 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69  Left);..    sqli
29e38 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
29e39 2c 20 70 52 69 67 68 74 29 3b 0d 0a 20 20 7d 65  , pRight);..  }e
29e3a 6c 73 65 7b 0d 0a 20 20 20 20 69 66 28 20 70 52  lse{..    if( pR
29e3b 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 70  ight ){..      p
29e3c 52 6f 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20 70  Root->pRight = p
29e3d 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 69 66  Right;..      if
29e3e 28 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  ( pRight->flags 
29e3f 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
29e40 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 52 6f 6f  ){..        pRoo
29e41 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  t->flags |= EP_E
29e42 78 70 43 6f 6c 6c 61 74 65 3b 0d 0a 20 20 20 20  xpCollate;..    
29e43 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f 6c 6c      pRoot->pColl
29e44 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c   = pRight->pColl
29e45 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
29e46 7d 0d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74  }..    if( pLeft
29e47 20 29 7b 0d 0a 20 20 20 20 20 20 70 52 6f 6f 74   ){..      pRoot
29e48 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
29e49 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66  ..      if( pLef
29e4a 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
29e4b 70 43 6f 6c 6c 61 74 65 20 29 7b 0d 0a 20 20 20  pCollate ){..   
29e4c 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67       pRoot->flag
29e4d 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61  s |= EP_ExpColla
29e4e 74 65 3b 0d 0a 20 20 20 20 20 20 20 20 70 52 6f  te;..        pRo
29e4f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66  ot->pColl = pLef
29e50 74 2d 3e 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20  t->pColl;..     
29e51 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 65   }..    }..    e
29e52 78 70 72 53 65 74 48 65 69 67 68 74 28 70 52 6f  xprSetHeight(pRo
29e53 6f 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  ot);..  }..}....
29e54 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  /*..** Allocate 
29e55 61 20 45 78 70 72 20 6e 6f 64 65 20 77 68 69 63  a Expr node whic
29e56 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61 6e 79 20  h joins as many 
29e57 61 73 20 74 77 6f 20 73 75 62 74 72 65 65 73 2e  as two subtrees.
29e58 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 65 20 6f 72 20  ..**..** One or 
29e59 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62 74  both of the subt
29e5a 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c  rees can be NULL
29e5b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
29e5c 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0d 0a  ter to the new..
29e5d 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f  ** Expr node.  O
29e5e 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72  r, if an OOM err
29e5f 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70  or occurs, set p
29e60 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
29e61 63 46 61 69 6c 65 64 2c 0d 0a 2a 2a 20 66 72 65  cFailed,..** fre
29e62 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61  e the subtrees a
29e63 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0d  nd return NULL..
29e64 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
29e65 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
29e66 33 50 45 78 70 72 28 0d 0a 20 20 50 61 72 73 65  3PExpr(..  Parse
29e67 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
29e68 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
29e69 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  ntext */..  int 
29e6a 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
29e6b 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
29e6c 6e 20 6f 70 63 6f 64 65 20 2a 2f 0d 0a 20 20 45  n opcode */..  E
29e6d 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20  xpr *pLeft,     
29e6e 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f         /* Left o
29e6f 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 45 78 70  perand */..  Exp
29e70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20  r *pRight,      
29e71 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70       /* Right op
29e72 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73  erand */..  cons
29e73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20  t Token *pToken 
29e74 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
29e75 74 6f 6b 65 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  token */..){..  
29e76 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65  Expr *p = sqlite
29e77 33 45 78 70 72 41 6c 6c 6f 63 28 70 50 61 72 73  3ExprAlloc(pPars
29e78 65 2d 3e 64 62 2c 20 6f 70 2c 20 70 54 6f 6b 65  e->db, op, pToke
29e79 6e 2c 20 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65  n, 1);..  sqlite
29e7a 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72  3ExprAttachSubtr
29e7b 65 65 73 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ees(pParse->db, 
29e7c 70 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  p, pLeft, pRight
29e7d 29 3b 0d 0a 20 20 69 66 28 20 70 20 29 20 7b 0d  );..  if( p ) {.
29e7e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29e7f 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72  CheckHeight(pPar
29e80 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b  se, p->nHeight);
29e81 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
29e82 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
29e83 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 73 73  Join two express
29e84 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 4e  ions using an AN
29e85 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 20  D operator.  If 
29e86 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f  either expressio
29e87 6e 20 69 73 0d 0a 2a 2a 20 4e 55 4c 4c 2c 20 74  n is..** NULL, t
29e88 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20  hen just return 
29e89 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 73  the other expres
29e8a 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  sion...*/..SQLIT
29e8b 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
29e8c 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73  sqlite3ExprAnd(s
29e8d 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
29e8e 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70   *pLeft, Expr *p
29e8f 52 69 67 68 74 29 7b 0d 0a 20 20 69 66 28 20 70  Right){..  if( p
29e90 4c 65 66 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Left==0 ){..    
29e91 72 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0d 0a  return pRight;..
29e92 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67    }else if( pRig
29e93 68 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65  ht==0 ){..    re
29e94 74 75 72 6e 20 70 4c 65 66 74 3b 0d 0a 20 20 7d  turn pLeft;..  }
29e95 65 6c 73 65 7b 0d 0a 20 20 20 20 45 78 70 72 20  else{..    Expr 
29e96 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45  *pNew = sqlite3E
29e97 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f  xprAlloc(db, TK_
29e98 41 4e 44 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  AND, 0, 0);..   
29e99 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61   sqlite3ExprAtta
29e9a 63 68 53 75 62 74 72 65 65 73 28 64 62 2c 20 70  chSubtrees(db, p
29e9b 4e 65 77 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  New, pLeft, pRig
29e9c 68 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ht);..    return
29e9d 20 70 4e 65 77 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a   pNew;..  }..}..
29e9e 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6e 73 74 72 75  ../*..** Constru
29e9f 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73  ct a new express
29ea0 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66  ion node for a f
29ea1 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c  unction with mul
29ea2 74 69 70 6c 65 0d 0a 2a 2a 20 61 72 67 75 6d 65  tiple..** argume
29ea3 6e 74 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  nts...*/..SQLITE
29ea4 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
29ea5 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
29ea6 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
29ea7 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
29ea8 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  t, Token *pToken
29ea9 29 7b 0d 0a 20 20 45 78 70 72 20 2a 70 4e 65 77  ){..  Expr *pNew
29eaa 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
29eab 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a   = pParse->db;..
29eac 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
29ead 20 29 3b 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71   );..  pNew = sq
29eae 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64  lite3ExprAlloc(d
29eaf 62 2c 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20  b, TK_FUNCTION, 
29eb0 70 54 6f 6b 65 6e 2c 20 31 29 3b 0d 0a 20 20 69  pToken, 1);..  i
29eb1 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0d 0a 20  f( pNew==0 ){.. 
29eb2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
29eb3 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69  stDelete(db, pLi
29eb4 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64 20 6d 65  st); /* Avoid me
29eb5 6d 6f 72 79 20 6c 65 61 6b 20 77 68 65 6e 20 6d  mory leak when m
29eb6 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f 0d 0a  alloc fails */..
29eb7 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
29eb8 20 7d 0d 0a 20 20 70 4e 65 77 2d 3e 78 2e 70 4c   }..  pNew->x.pL
29eb9 69 73 74 20 3d 20 70 4c 69 73 74 3b 0d 0a 20 20  ist = pList;..  
29eba 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
29ebb 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
29ebc 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0d  P_xIsSelect) );.
29ebd 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  .  sqlite3ExprSe
29ebe 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
29ebf 70 4e 65 77 29 3b 0d 0a 20 20 72 65 74 75 72 6e  pNew);..  return
29ec0 20 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pNew;..}..../*.
29ec1 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72  .** Assign a var
29ec2 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20  iable number to 
29ec3 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  an expression th
29ec4 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c  at encodes a wil
29ec5 64 63 61 72 64 0d 0a 2a 2a 20 69 6e 20 74 68 65  dcard..** in the
29ec6 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
29ec7 61 74 65 6d 65 6e 74 2e 20 20 0d 0a 2a 2a 0d 0a  atement.  ..**..
29ec8 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e  ** Wildcards con
29ec9 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e  sisting of a sin
29eca 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 73 69  gle "?" are assi
29ecb 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 73 65  gned the next se
29ecc 71 75 65 6e 74 69 61 6c 0d 0a 2a 2a 20 76 61 72  quential..** var
29ecd 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0d 0a 2a  iable number...*
29ece 2a 0d 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20  *..** Wildcards 
29ecf 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e  of the form "?nn
29ed0 6e 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20  n" are assigned 
29ed1 74 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22  the number "nnn"
29ed2 2e 20 20 57 65 20 6d 61 6b 65 0d 0a 2a 2a 20 73  .  We make..** s
29ed3 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74  ure "nnn" is not
29ed4 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64   too be to avoid
29ed5 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72   a denial of ser
29ed6 76 69 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e  vice attack when
29ed7 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 74 61  ..** the SQL sta
29ed8 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f  tement comes fro
29ed9 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f  m an external so
29eda 75 72 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 69  urce...**..** Wi
29edb 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66  ldcards of the f
29edc 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 61  orm ":aaa", "@aa
29edd 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 72  a", or "$aaa" ar
29ede 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73  e assigned the s
29edf 61 6d 65 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 61  ame number..** a
29ee0 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69  s the previous i
29ee1 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73  nstance of the s
29ee2 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f  ame wildcard.  O
29ee3 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  r if this is the
29ee4 20 66 69 72 73 74 0d 0a 2a 2a 20 69 6e 73 74 61   first..** insta
29ee5 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63  nce of the wildc
29ee6 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65  ard, the next se
29ee7 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65  quenial variable
29ee8 20 6e 75 6d 62 65 72 20 69 73 0d 0a 2a 2a 20 61   number is..** a
29ee9 73 73 69 67 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  ssigned...*/..SQ
29eea 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
29eeb 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73  d sqlite3ExprAss
29eec 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72  ignVarNumber(Par
29eed 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
29eee 20 2a 70 45 78 70 72 29 7b 0d 0a 20 20 73 71 6c   *pExpr){..  sql
29eef 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
29ef0 65 2d 3e 64 62 3b 0d 0a 20 20 63 6f 6e 73 74 20  e->db;..  const 
29ef1 63 68 61 72 20 2a 7a 3b 0d 0a 0d 0a 20 20 69 66  char *z;....  if
29ef2 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
29ef3 75 72 6e 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  urn;..  assert( 
29ef4 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
29ef5 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
29ef6 74 56 61 6c 75 65 7c 45 50 5f 52 65 64 75 63 65  tValue|EP_Reduce
29ef7 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  d|EP_TokenOnly) 
29ef8 29 3b 0d 0a 20 20 7a 20 3d 20 70 45 78 70 72 2d  );..  z = pExpr-
29ef9 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 61 73  >u.zToken;..  as
29efa 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0d 0a 20  sert( z!=0 );.. 
29efb 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 21 3d 30   assert( z[0]!=0
29efc 20 29 3b 0d 0a 20 20 69 66 28 20 7a 5b 31 5d 3d   );..  if( z[1]=
29efd 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 57 69  =0 ){..    /* Wi
29efe 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f  ldcard of the fo
29eff 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e 20  rm "?".  Assign 
29f00 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62 6c  the next variabl
29f01 65 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20  e number */..   
29f02 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d 27   assert( z[0]=='
29f03 3f 27 20 29 3b 0d 0a 20 20 20 20 70 45 78 70 72  ?' );..    pExpr
29f04 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56  ->iColumn = (ynV
29f05 61 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 56  ar)(++pParse->nV
29f06 61 72 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ar);..  }else{..
29f07 20 20 20 20 79 6e 56 61 72 20 78 20 3d 20 30 3b      ynVar x = 0;
29f08 0d 0a 20 20 20 20 75 33 32 20 6e 20 3d 20 73 71  ..    u32 n = sq
29f09 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
29f0a 3b 0d 0a 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  ;..    if( z[0]=
29f0b 3d 27 3f 27 20 29 7b 0d 0a 20 20 20 20 20 20 2f  ='?' ){..      /
29f0c 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68  * Wildcard of th
29f0d 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20  e form "?nnn".  
29f0e 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f  Convert "nnn" to
29f0f 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0d   an integer and.
29f10 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 20 69 74  .      ** use it
29f11 20 61 73 20 74 68 65 20 76 61 72 69 61 62 6c 65   as the variable
29f12 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 20 20   number */..    
29f13 20 20 69 36 34 20 69 3b 0d 0a 20 20 20 20 20 20    i64 i;..      
29f14 69 6e 74 20 62 4f 6b 20 3d 20 30 3d 3d 73 71 6c  int bOk = 0==sql
29f15 69 74 65 33 41 74 6f 69 36 34 28 26 7a 5b 31 5d  ite3Atoi64(&z[1]
29f16 2c 20 26 69 2c 20 6e 2d 31 2c 20 53 51 4c 49 54  , &i, n-1, SQLIT
29f17 45 5f 55 54 46 38 29 3b 0d 0a 20 20 20 20 20 20  E_UTF8);..      
29f18 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
29f19 20 78 20 3d 20 28 79 6e 56 61 72 29 69 3b 0d 0a   x = (ynVar)i;..
29f1a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29f1b 69 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 74  i==0 );..      t
29f1c 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b  estcase( i==1 );
29f1d 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
29f1e 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  ( i==db->aLimit[
29f1f 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
29f20 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20  IABLE_NUMBER]-1 
29f21 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
29f22 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69  se( i==db->aLimi
29f23 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
29f24 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
29f25 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 62 4f  );..      if( bO
29f26 6b 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69  k==0 || i<1 || i
29f27 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
29f28 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
29f29 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0d 0a 20 20  E_NUMBER] ){..  
29f2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
29f2b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
29f2c 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d  ariable number m
29f2d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f  ust be between ?
29f2e 31 20 61 6e 64 20 3f 25 64 22 2c 0d 0a 20 20 20  1 and ?%d",..   
29f2f 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69           db->aLi
29f30 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
29f31 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
29f32 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 78 20 3d  ]);..        x =
29f33 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   0;..      }..  
29f34 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 65      if( i>pParse
29f35 2d 3e 6e 56 61 72 20 29 7b 0d 0a 20 20 20 20 20  ->nVar ){..     
29f36 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20     pParse->nVar 
29f37 3d 20 28 69 6e 74 29 69 3b 0d 0a 20 20 20 20 20  = (int)i;..     
29f38 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a   }..    }else{..
29f39 20 20 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72        /* Wildcar
29f3a 64 73 20 6c 69 6b 65 20 22 3a 61 61 61 22 2c 20  ds like ":aaa", 
29f3b 22 24 61 61 61 22 20 6f 72 20 22 40 61 61 61 22  "$aaa" or "@aaa"
29f3c 2e 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d  .  Reuse the sam
29f3d 65 20 76 61 72 69 61 62 6c 65 0d 0a 20 20 20 20  e variable..    
29f3e 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74    ** number as t
29f3f 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 61  he prior appeara
29f40 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20  nce of the same 
29f41 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20  name, or if the 
29f42 6e 61 6d 65 0d 0a 20 20 20 20 20 20 2a 2a 20 68  name..      ** h
29f43 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 72 65  as never appeare
29f44 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20  d before, reuse 
29f45 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c  the same variabl
29f46 65 20 6e 75 6d 62 65 72 0d 0a 20 20 20 20 20 20  e number..      
29f47 2a 2f 0d 0a 20 20 20 20 20 20 79 6e 56 61 72 20  */..      ynVar 
29f48 69 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  i;..      for(i=
29f49 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 7a 56  0; i<pParse->nzV
29f4a 61 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  ar; i++){..     
29f4b 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61     if( pParse->a
29f4c 7a 56 61 72 5b 69 5d 20 26 26 20 6d 65 6d 63 6d  zVar[i] && memcm
29f4d 70 28 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b  p(pParse->azVar[
29f4e 69 5d 2c 7a 2c 6e 2b 31 29 3d 3d 30 20 29 7b 0d  i],z,n+1)==0 ){.
29f4f 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
29f50 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 78 20 3d 20  ->iColumn = x = 
29f51 28 79 6e 56 61 72 29 69 2b 31 3b 0d 0a 20 20 20  (ynVar)i+1;..   
29f52 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
29f53 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
29f54 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 78 3d 3d  }..      if( x==
29f55 30 20 29 20 78 20 3d 20 70 45 78 70 72 2d 3e 69  0 ) x = pExpr->i
29f56 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29  Column = (ynVar)
29f57 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 29  (++pParse->nVar)
29f58 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
29f59 28 20 78 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  ( x>0 ){..      
29f5a 69 66 28 20 78 3e 70 50 61 72 73 65 2d 3e 6e 7a  if( x>pParse->nz
29f5b 56 61 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Var ){..        
29f5c 63 68 61 72 20 2a 2a 61 3b 0d 0a 20 20 20 20 20  char **a;..     
29f5d 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62     a = sqlite3Db
29f5e 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 50 61 72  Realloc(db, pPar
29f5f 73 65 2d 3e 61 7a 56 61 72 2c 20 78 2a 73 69 7a  se->azVar, x*siz
29f60 65 6f 66 28 61 5b 30 5d 29 29 3b 0d 0a 20 20 20  eof(a[0]));..   
29f61 20 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20       if( a==0 ) 
29f62 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 72 72 6f  return;  /* Erro
29f63 72 20 72 65 70 6f 72 74 65 64 20 74 68 72 6f 75  r reported throu
29f64 67 68 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  gh db->mallocFai
29f65 6c 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  led */..        
29f66 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 20 3d 20  pParse->azVar = 
29f67 61 3b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  a;..        mems
29f68 65 74 28 26 61 5b 70 50 61 72 73 65 2d 3e 6e 7a  et(&a[pParse->nz
29f69 56 61 72 5d 2c 20 30 2c 20 28 78 2d 70 50 61 72  Var], 0, (x-pPar
29f6a 73 65 2d 3e 6e 7a 56 61 72 29 2a 73 69 7a 65 6f  se->nzVar)*sizeo
29f6b 66 28 61 5b 30 5d 29 29 3b 0d 0a 20 20 20 20 20  f(a[0]));..     
29f6c 20 20 20 70 50 61 72 73 65 2d 3e 6e 7a 56 61 72     pParse->nzVar
29f6d 20 3d 20 78 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a   = x;..      }..
29f6e 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 21 3d        if( z[0]!=
29f6f 27 3f 27 20 7c 7c 20 70 50 61 72 73 65 2d 3e 61  '?' || pParse->a
29f70 7a 56 61 72 5b 78 2d 31 5d 3d 3d 30 20 29 7b 0d  zVar[x-1]==0 ){.
29f71 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29f72 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
29f73 65 2d 3e 61 7a 56 61 72 5b 78 2d 31 5d 29 3b 0d  e->azVar[x-1]);.
29f74 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
29f75 3e 61 7a 56 61 72 5b 78 2d 31 5d 20 3d 20 73 71  >azVar[x-1] = sq
29f76 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
29f77 62 2c 20 7a 2c 20 6e 29 3b 0d 0a 20 20 20 20 20  b, z, n);..     
29f78 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 20 0d   }..    }..  } .
29f79 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
29f7a 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e  nErr && pParse->
29f7b 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  nVar>db->aLimit[
29f7c 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
29f7d 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b  IABLE_NUMBER] ){
29f7e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
29f7f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
29f80 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69  oo many SQL vari
29f81 61 62 6c 65 73 22 29 3b 0d 0a 20 20 7d 0d 0a 7d  ables");..  }..}
29f82 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 63 75 72  ..../*..** Recur
29f83 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e  sively delete an
29f84 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
29f85 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
29f86 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
29f87 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 71 6c  e3ExprDelete(sql
29f88 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
29f89 70 29 7b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20  p){..  if( p==0 
29f8a 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 2f 2a 20  ) return;..  /* 
29f8b 53 61 6e 69 74 79 20 63 68 65 63 6b 3a 20 41 73  Sanity check: As
29f8c 73 65 72 74 20 74 68 61 74 20 74 68 65 20 49 6e  sert that the In
29f8d 74 56 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 6e 65  tValue is non-ne
29f8e 67 61 74 69 76 65 20 69 66 20 69 74 20 65 78 69  gative if it exi
29f8f 73 74 73 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  sts */..  assert
29f90 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
29f91 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ty(p, EP_IntValu
29f92 65 29 20 7c 7c 20 70 2d 3e 75 2e 69 56 61 6c 75  e) || p->u.iValu
29f93 65 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 21  e>=0 );..  if( !
29f94 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
29f95 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  ty(p, EP_TokenOn
29f96 6c 79 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ly) ){..    sqli
29f97 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
29f98 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20  , p->pLeft);..  
29f99 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
29f9a 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 68  ete(db, p->pRigh
29f9b 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 45 78  t);..    if( !Ex
29f9c 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
29f9d 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 26 20   EP_Reduced) && 
29f9e 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32  (p->flags2 & EP2
29f9f 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 21  _MallocedToken)!
29fa0 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 ){..      sql
29fa1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
29fa2 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a 20 20  ->u.zToken);..  
29fa3 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 45 78 70    }..    if( Exp
29fa4 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
29fa5 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
29fa6 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ..      sqlite3S
29fa7 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
29fa8 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0d 0a  p->x.pSelect);..
29fa9 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
29faa 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
29fab 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78  tDelete(db, p->x
29fac 2e 70 4c 69 73 74 29 3b 0d 0a 20 20 20 20 7d 0d  .pList);..    }.
29fad 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21 45 78 70  .  }..  if( !Exp
29fae 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
29faf 45 50 5f 53 74 61 74 69 63 29 20 29 7b 0d 0a 20  EP_Static) ){.. 
29fb0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
29fb1 28 64 62 2c 20 70 29 3b 0d 0a 20 20 7d 0d 0a 7d  (db, p);..  }..}
29fb2 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
29fb3 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
29fb4 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  bytes allocated 
29fb5 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69  for the expressi
29fb6 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0d 0a 2a  on structure ..*
29fb7 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  * passed as the 
29fb8 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20  first argument. 
29fb9 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 6f  This is always o
29fba 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c 53  ne of EXPR_FULLS
29fbb 49 5a 45 2c 0d 0a 2a 2a 20 45 58 50 52 5f 52 45  IZE,..** EXPR_RE
29fbc 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50  DUCEDSIZE or EXP
29fbd 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e  R_TOKENONLYSIZE.
29fbe 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
29fbf 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28   exprStructSize(
29fc0 45 78 70 72 20 2a 70 29 7b 0d 0a 20 20 69 66 28  Expr *p){..  if(
29fc1 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
29fc2 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
29fc3 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ) ) return EXPR_
29fc4 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0d 0a  TOKENONLYSIZE;..
29fc5 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
29fc6 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75  perty(p, EP_Redu
29fc7 63 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58  ced) ) return EX
29fc8 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0d  PR_REDUCEDSIZE;.
29fc9 0a 20 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46  .  return EXPR_F
29fca 55 4c 4c 53 49 5a 45 3b 0d 0a 7d 0d 0a 0d 0a 2f  ULLSIZE;..}..../
29fcb 2a 0d 0a 2a 2a 20 54 68 65 20 64 75 70 65 64 45  *..** The dupedE
29fcc 78 70 72 2a 53 69 7a 65 28 29 20 72 6f 75 74 69  xpr*Size() routi
29fcd 6e 65 73 20 65 61 63 68 20 72 65 74 75 72 6e 20  nes each return 
29fce 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
29fcf 74 65 73 20 72 65 71 75 69 72 65 64 0d 0a 2a 2a  tes required..**
29fd0 20 74 6f 20 73 74 6f 72 65 20 61 20 63 6f 70 79   to store a copy
29fd1 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
29fd2 6e 20 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20  n or expression 
29fd3 74 72 65 65 2e 20 20 54 68 65 79 20 64 69 66 66  tree.  They diff
29fd4 65 72 20 69 6e 0d 0a 2a 2a 20 68 6f 77 20 6d 75  er in..** how mu
29fd5 63 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 69  ch of the tree i
29fd6 73 20 6d 65 61 73 75 72 65 64 2e 0d 0a 2a 2a 0d  s measured...**.
29fd7 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70  .**     dupedExp
29fd8 72 53 74 72 75 63 74 53 69 7a 65 28 29 20 20 20  rStructSize()   
29fd9 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c 79 20 74    Size of only t
29fda 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72  he Expr structur
29fdb 65 20 0d 0a 2a 2a 20 20 20 20 20 64 75 70 65 64  e ..**     duped
29fdc 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 20 20  ExprNodeSize()  
29fdd 20 20 20 20 20 53 69 7a 65 20 6f 66 20 45 78 70       Size of Exp
29fde 72 20 2b 20 73 70 61 63 65 20 66 6f 72 20 74 6f  r + space for to
29fdf 6b 65 6e 0d 0a 2a 2a 20 20 20 20 20 64 75 70 65  ken..**     dupe
29fe0 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 20 20  dExprSize()     
29fe1 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 6f 6b        Expr + tok
29fe2 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 6f 6d  en + subtree com
29fe3 70 6f 6e 65 6e 74 73 0d 0a 2a 2a 0d 0a 2a 2a 2a  ponents..**..***
29fe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29fe6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29fe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29fe8 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
29fe9 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53 74   The dupedExprSt
29fea 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74  ructSize() funct
29feb 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20  ion returns two 
29fec 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67  values OR-ed tog
29fed 65 74 68 65 72 3a 20 20 0d 0a 2a 2a 20 28 31 29  ether:  ..** (1)
29fee 20 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69   the space requi
29fef 72 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f  red for a copy o
29ff0 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63  f the Expr struc
29ff1 74 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0d 0a  ture only and ..
29ff2 2a 2a 20 28 32 29 20 74 68 65 20 45 50 5f 78 78  ** (2) the EP_xx
29ff3 78 20 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64  x flags that ind
29ff4 69 63 61 74 65 20 77 68 61 74 20 74 68 65 20 73  icate what the s
29ff5 74 72 75 63 74 75 72 65 20 73 69 7a 65 20 73 68  tructure size sh
29ff6 6f 75 6c 64 20 62 65 2e 0d 0a 2a 2a 20 54 68 65  ould be...** The
29ff7 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20 69   return values i
29ff8 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a  s always one of:
29ff9 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 45 58  ..**..**      EX
29ffa 50 52 5f 46 55 4c 4c 53 49 5a 45 0d 0a 2a 2a 20  PR_FULLSIZE..** 
29ffb 20 20 20 20 20 45 58 50 52 5f 52 45 44 55 43 45       EXPR_REDUCE
29ffc 44 53 49 5a 45 20 20 20 7c 20 45 50 5f 52 65 64  DSIZE   | EP_Red
29ffd 75 63 65 64 0d 0a 2a 2a 20 20 20 20 20 20 45 58  uced..**      EX
29ffe 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
29fff 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 0d   | EP_TokenOnly.
2a000 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 69 7a 65  .**..** The size
2a001 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72   of the structur
2a002 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 62  e can be found b
2a003 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 20 72 65  y masking the re
2a004 74 75 72 6e 20 76 61 6c 75 65 0d 0a 2a 2a 20 6f  turn value..** o
2a005 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  f this routine w
2a006 69 74 68 20 30 78 66 66 66 2e 20 20 54 68 65 20  ith 0xfff.  The 
2a007 66 6c 61 67 73 20 63 61 6e 20 62 65 20 66 6f 75  flags can be fou
2a008 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68  nd by masking th
2a009 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  e..** return val
2a00a 75 65 20 77 69 74 68 20 45 50 5f 52 65 64 75 63  ue with EP_Reduc
2a00b 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e  ed|EP_TokenOnly.
2a00c 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68  ..**..** Note th
2a00d 61 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d 45  at with flags==E
2a00e 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 74  XPRDUP_REDUCE, t
2a00f 68 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  his routines wor
2a010 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0d  ks on full-size.
2a011 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20  .** (unreduced) 
2a012 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20  Expr objects as 
2a013 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c  they or original
2a014 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  ly constructed b
2a015 79 20 74 68 65 20 70 61 72 73 65 72 2e 0d 0a 2a  y the parser...*
2a016 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65 73 73  * During express
2a017 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 78  ion analysis, ex
2a018 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tra information 
2a019 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20  is computed and 
2a01a 6d 6f 76 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 6c  moved into..** l
2a01b 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74 65  ater parts of te
2a01c 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e  h Expr object an
2a01d 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  d that extra inf
2a01e 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67  ormation might g
2a01f 65 74 20 63 68 6f 70 70 65 64 0d 0a 2a 2a 20 6f  et chopped..** o
2a020 66 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ff if the expres
2a021 73 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e  sion is reduced.
2a022 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74    Note also that
2a023 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   it does not wor
2a024 6b 20 74 6f 0d 0a 2a 2a 20 6d 61 6b 65 20 61 20  k to..** make a 
2a025 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 63  EXPRDUP_REDUCE c
2a026 6f 70 79 20 6f 66 20 61 20 72 65 64 75 63 65 64  opy of a reduced
2a027 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74   expression.  It
2a028 20 69 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0d 0a   is only legal..
2a029 2a 2a 20 74 6f 20 72 65 64 75 63 65 20 61 20 70  ** to reduce a p
2a02a 72 69 73 74 69 6e 65 20 65 78 70 72 65 73 73 69  ristine expressi
2a02b 6f 6e 20 74 72 65 65 20 66 72 6f 6d 20 74 68 65  on tree from the
2a02c 20 70 61 72 73 65 72 2e 20 20 54 68 65 20 69 6d   parser.  The im
2a02d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a 2a 2a  plementation..**
2a02e 20 6f 66 20 64 75 70 65 64 45 78 70 72 53 74 72   of dupedExprStr
2a02f 75 63 74 53 69 7a 65 28 29 20 63 6f 6e 74 61 69  uctSize() contai
2a030 6e 20 6d 75 6c 74 69 70 6c 65 20 61 73 73 65 72  n multiple asser
2a031 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
2a032 68 61 74 20 61 74 74 65 6d 70 74 0d 0a 2a 2a 20  hat attempt..** 
2a033 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 69 73 20  to enforce this 
2a034 63 6f 6e 73 74 72 61 69 6e 74 2e 0d 0a 2a 2f 0d  constraint...*/.
2a035 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65  .static int dupe
2a036 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
2a037 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
2a038 67 73 29 7b 0d 0a 20 20 69 6e 74 20 6e 53 69 7a  gs){..  int nSiz
2a039 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 66 6c  e;..  assert( fl
2a03a 61 67 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 44  ags==EXPRDUP_RED
2a03b 55 43 45 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 20  UCE || flags==0 
2a03c 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 66  ); /* Only one f
2a03d 6c 61 67 20 76 61 6c 75 65 20 61 6c 6c 6f 77 65  lag value allowe
2a03e 64 20 2a 2f 0d 0a 20 20 69 66 28 20 30 3d 3d 28  d */..  if( 0==(
2a03f 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45  flags&EXPRDUP_RE
2a040 44 55 43 45 29 20 29 7b 0d 0a 20 20 20 20 6e 53  DUCE) ){..    nS
2a041 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c 53  ize = EXPR_FULLS
2a042 49 5a 45 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  IZE;..  }else{..
2a043 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2a044 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
2a045 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  p, EP_TokenOnly|
2a046 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0d 0a  EP_Reduced) );..
2a047 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2a048 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
2a049 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20  EP_FromJoin) ); 
2a04a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ..    assert( (p
2a04b 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d  ->flags2 & EP2_M
2a04c 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30  allocedToken)==0
2a04d 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
2a04e 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50   (p->flags2 & EP
2a04f 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d  2_Irreducible)==
2a050 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d  0 );..    if( p-
2a051 3e 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69  >pLeft || p->pRi
2a052 67 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20  ght || p->pColl 
2a053 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b  || p->x.pList ){
2a054 0d 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20  ..      nSize = 
2a055 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45  EXPR_REDUCEDSIZE
2a056 20 7c 20 45 50 5f 52 65 64 75 63 65 64 3b 0d 0a   | EP_Reduced;..
2a057 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
2a058 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 54    nSize = EXPR_T
2a059 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45  OKENONLYSIZE | E
2a05a 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 0d 0a 20 20  P_TokenOnly;..  
2a05b 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
2a05c 72 6e 20 6e 53 69 7a 65 3b 0d 0a 7d 0d 0a 0d 0a  rn nSize;..}....
2a05d 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
2a05e 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
2a05f 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20   space in bytes 
2a060 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
2a061 65 20 74 68 65 20 63 6f 70 79 20 0d 0a 2a 2a 20  e the copy ..** 
2a062 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75  of the Expr stru
2a063 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f 70 79  cture and a copy
2a064 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 2e 7a   of the Expr.u.z
2a065 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66  Token string (if
2a066 20 74 68 61 74 0d 0a 2a 2a 20 73 74 72 69 6e 67   that..** string
2a067 20 69 73 20 64 65 66 69 6e 65 64 2e 29 0d 0a 2a   is defined.)..*
2a068 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75  /..static int du
2a069 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
2a06a 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
2a06b 67 73 29 7b 0d 0a 20 20 69 6e 74 20 6e 42 79 74  gs){..  int nByt
2a06c 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72  e = dupedExprStr
2a06d 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  uctSize(p, flags
2a06e 29 20 26 20 30 78 66 66 66 3b 0d 0a 20 20 69 66  ) & 0xfff;..  if
2a06f 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2a070 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ty(p, EP_IntValu
2a071 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65  e) && p->u.zToke
2a072 6e 20 29 7b 0d 0a 20 20 20 20 6e 42 79 74 65 20  n ){..    nByte 
2a073 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
2a074 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b  30(p->u.zToken)+
2a075 31 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  1;..  }..  retur
2a076 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b  n ROUND8(nByte);
2a077 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
2a078 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
2a079 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65  of bytes require
2a07a 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 75  d to create a du
2a07b 70 6c 69 63 61 74 65 20 6f 66 20 74 68 65 20 0d  plicate of the .
2a07c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70  .** expression p
2a07d 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
2a07e 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  st argument. The
2a07f 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
2a080 20 69 73 20 61 0d 0a 2a 2a 20 6d 61 73 6b 20 63   is a..** mask c
2a081 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 52 44 55  ontaining EXPRDU
2a082 50 5f 58 58 58 20 66 6c 61 67 73 2e 0d 0a 2a 2a  P_XXX flags...**
2a083 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  ..** The value r
2a084 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73  eturned includes
2a085 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 65   space to create
2a086 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45   a copy of the E
2a087 78 70 72 20 73 74 72 75 63 74 0d 0a 2a 2a 20 69  xpr struct..** i
2a088 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75  tself and the bu
2a089 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f  ffer referred to
2a08a 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65   by Expr.u.zToke
2a08b 6e 2c 20 69 66 20 61 6e 79 2e 0d 0a 2a 2a 0d 0a  n, if any...**..
2a08c 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55  ** If the EXPRDU
2a08d 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73  P_REDUCE flag is
2a08e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72   set, then the r
2a08f 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c  eturn value incl
2a090 75 64 65 73 20 0d 0a 2a 2a 20 73 70 61 63 65 20  udes ..** space 
2a091 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c  to duplicate all
2a092 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74   Expr nodes in t
2a093 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62  he tree formed b
2a094 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0d 0a 2a  y Expr.pLeft ..*
2a095 2a 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68  * and Expr.pRigh
2a096 74 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74  t variables (but
2a097 20 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72   not for any str
2a098 75 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20  uctures pointed 
2a099 74 6f 20 6f 72 20 0d 0a 2a 2a 20 64 65 73 63 65  to or ..** desce
2a09a 6e 64 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78  nded from the Ex
2a09b 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78  pr.x.pList or Ex
2a09c 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72  pr.x.pSelect var
2a09d 69 61 62 6c 65 73 29 2e 0d 0a 2a 2f 0d 0a 73 74  iables)...*/..st
2a09e 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 45 78  atic int dupedEx
2a09f 70 72 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20  prSize(Expr *p, 
2a0a0 69 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 20 20 69  int flags){..  i
2a0a1 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0d 0a 20  nt nByte = 0;.. 
2a0a2 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 6e   if( p ){..    n
2a0a3 42 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72  Byte = dupedExpr
2a0a4 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67  NodeSize(p, flag
2a0a5 73 29 3b 0d 0a 20 20 20 20 69 66 28 20 66 6c 61  s);..    if( fla
2a0a6 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43  gs&EXPRDUP_REDUC
2a0a7 45 20 29 7b 0d 0a 20 20 20 20 20 20 6e 42 79 74  E ){..      nByt
2a0a8 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 53 69  e += dupedExprSi
2a0a9 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 6c 61  ze(p->pLeft, fla
2a0aa 67 73 29 20 2b 20 64 75 70 65 64 45 78 70 72 53  gs) + dupedExprS
2a0ab 69 7a 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 66  ize(p->pRight, f
2a0ac 6c 61 67 73 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  lags);..    }.. 
2a0ad 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79   }..  return nBy
2a0ae 74 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  te;..}..../*..**
2a0af 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2a0b0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c  s similar to sql
2a0b1 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20 65  ite3ExprDup(), e
2a0b2 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70 7a  xcept that if pz
2a0b3 42 75 66 66 65 72 20 0d 0a 2a 2a 20 69 73 20 6e  Buffer ..** is n
2a0b4 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a  ot NULL then *pz
2a0b5 42 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65  Buffer is assume
2a0b6 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20  d to point to a 
2a0b7 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f  buffer large eno
2a0b8 75 67 68 20 0d 0a 2a 2a 20 74 6f 20 73 74 6f 72  ugh ..** to stor
2a0b9 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78  e the copy of ex
2a0ba 70 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20  pression p, the 
2a0bb 63 6f 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a  copies of p->u.z
2a0bc 54 6f 6b 65 6e 0d 0a 2a 2a 20 28 69 66 20 61 70  Token..** (if ap
2a0bd 70 6c 69 63 61 62 6c 65 29 2c 20 61 6e 64 20 74  plicable), and t
2a0be 68 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  he copies of the
2a0bf 20 70 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d   p->pLeft and p-
2a0c0 3e 70 52 69 67 68 74 20 65 78 70 72 65 73 73 69  >pRight expressi
2a0c1 6f 6e 73 2c 0d 0a 2a 2a 20 69 66 20 61 6e 79 2e  ons,..** if any.
2a0c2 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   Before returnin
2a0c3 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 69 73 20  g, *pzBuffer is 
2a0c4 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74  set to the first
2a0c5 20 62 79 74 65 20 70 61 73 73 65 64 20 74 68 65   byte passed the
2a0c6 0d 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66 20  ..** portion of 
2a0c7 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 69 65  the buffer copie
2a0c8 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 20 66  d into by this f
2a0c9 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74  unction...*/..st
2a0ca 61 74 69 63 20 45 78 70 72 20 2a 65 78 70 72 44  atic Expr *exprD
2a0cb 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
2a0cc 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
2a0cd 67 73 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65  gs, u8 **pzBuffe
2a0ce 72 29 7b 0d 0a 20 20 45 78 70 72 20 2a 70 4e 65  r){..  Expr *pNe
2a0cf 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  w = 0;          
2a0d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
2a0d1 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
2a0d2 2f 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a 20  /..  if( p ){.. 
2a0d3 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 52     const int isR
2a0d4 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 26  educed = (flags&
2a0d5 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
2a0d6 0d 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63  ..    u8 *zAlloc
2a0d7 3b 0d 0a 20 20 20 20 75 33 32 20 73 74 61 74 69  ;..    u32 stati
2a0d8 63 46 6c 61 67 20 3d 20 30 3b 0d 0a 0d 0a 20 20  cFlag = 0;....  
2a0d9 20 20 61 73 73 65 72 74 28 20 70 7a 42 75 66 66    assert( pzBuff
2a0da 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63  er==0 || isReduc
2a0db 65 64 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  ed );....    /* 
2a0dc 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 65  Figure out where
2a0dd 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65   to write the ne
2a0de 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  w Expr structure
2a0df 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 7a  . */..    if( pz
2a0e0 42 75 66 66 65 72 20 29 7b 0d 0a 20 20 20 20 20  Buffer ){..     
2a0e1 20 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66   zAlloc = *pzBuf
2a0e2 66 65 72 3b 0d 0a 20 20 20 20 20 20 73 74 61 74  fer;..      stat
2a0e3 69 63 46 6c 61 67 20 3d 20 45 50 5f 53 74 61 74  icFlag = EP_Stat
2a0e4 69 63 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  ic;..    }else{.
2a0e5 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20  .      zAlloc = 
2a0e6 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
2a0e7 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 70 72  aw(db, dupedExpr
2a0e8 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 3b  Size(p, flags));
2a0e9 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4e 65  ..    }..    pNe
2a0ea 77 20 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c  w = (Expr *)zAll
2a0eb 6f 63 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70  oc;....    if( p
2a0ec 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  New ){..      /*
2a0ed 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 74 6f   Set nNewSize to
2a0ee 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f 63 61   the size alloca
2a0ef 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 72 75  ted for the stru
2a0f0 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f  cture pointed to
2a0f1 0d 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 4e  ..      ** by pN
2a0f2 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 68  ew. This is eith
2a0f3 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45  er EXPR_FULLSIZE
2a0f4 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49  , EXPR_REDUCEDSI
2a0f5 5a 45 20 6f 72 0d 0a 20 20 20 20 20 20 2a 2a 20  ZE or..      ** 
2a0f6 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
2a0f7 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65  ZE. nToken is se
2a0f8 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
2a0f9 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
2a0fa 64 0d 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74  d..      ** by t
2a0fb 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70  he copy of the p
2a0fc 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e  ->u.zToken strin
2a0fd 67 20 28 69 66 20 61 6e 79 29 2e 0d 0a 20 20 20  g (if any)...   
2a0fe 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 63 6f 6e     */..      con
2a0ff 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 72  st unsigned nStr
2a100 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64 45  uctSize = dupedE
2a101 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c  xprStructSize(p,
2a102 20 66 6c 61 67 73 29 3b 0d 0a 20 20 20 20 20 20   flags);..      
2a103 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69  const int nNewSi
2a104 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65  ze = nStructSize
2a105 20 26 20 30 78 66 66 66 3b 0d 0a 20 20 20 20 20   & 0xfff;..     
2a106 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0d 0a 20 20   int nToken;..  
2a107 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73      if( !ExprHas
2a108 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49  Property(p, EP_I
2a109 6e 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75  ntValue) && p->u
2a10a 2e 7a 54 6f 6b 65 6e 20 29 7b 0d 0a 20 20 20 20  .zToken ){..    
2a10b 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c      nToken = sql
2a10c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e  ite3Strlen30(p->
2a10d 75 2e 7a 54 6f 6b 65 6e 29 20 2b 20 31 3b 0d 0a  u.zToken) + 1;..
2a10e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2a10f 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 30        nToken = 0
2a110 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2a111 20 20 69 66 28 20 69 73 52 65 64 75 63 65 64 20    if( isReduced 
2a112 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
2a113 72 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  rt( ExprHasPrope
2a114 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65  rty(p, EP_Reduce
2a115 64 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  d)==0 );..      
2a116 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c    memcpy(zAlloc,
2a117 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 3b 0d 0a   p, nNewSize);..
2a118 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2a119 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 20        int nSize 
2a11a 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65  = exprStructSize
2a11b 28 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 6d 65  (p);..        me
2a11c 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20  mcpy(zAlloc, p, 
2a11d 6e 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 20  nSize);..       
2a11e 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b   memset(&zAlloc[
2a11f 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f  nSize], 0, EXPR_
2a120 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b  FULLSIZE-nSize);
2a121 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  ..      }....   
2a122 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50     /* Set the EP
2a123 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b  _Reduced, EP_Tok
2a124 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53  enOnly, and EP_S
2a125 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70 72  tatic flags appr
2a126 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0d 0a 20  opriately. */.. 
2a127 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
2a128 20 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64   &= ~(EP_Reduced
2a129 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50  |EP_TokenOnly|EP
2a12a 5f 53 74 61 74 69 63 29 3b 0d 0a 20 20 20 20 20  _Static);..     
2a12b 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20   pNew->flags |= 
2a12c 6e 53 74 72 75 63 74 53 69 7a 65 20 26 20 28 45  nStructSize & (E
2a12d 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b  P_Reduced|EP_Tok
2a12e 65 6e 4f 6e 6c 79 29 3b 0d 0a 20 20 20 20 20 20  enOnly);..      
2a12f 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73  pNew->flags |= s
2a130 74 61 74 69 63 46 6c 61 67 3b 0d 0a 0d 0a 20 20  taticFlag;....  
2a131 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20      /* Copy the 
2a132 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69  p->u.zToken stri
2a133 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0d 0a  ng, if any. */..
2a134 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e        if( nToken
2a135 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61   ){..        cha
2a136 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77  r *zToken = pNew
2a137 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68  ->u.zToken = (ch
2a138 61 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 77  ar*)&zAlloc[nNew
2a139 53 69 7a 65 5d 3b 0d 0a 20 20 20 20 20 20 20 20  Size];..        
2a13a 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70  memcpy(zToken, p
2a13b 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  ->u.zToken, nTok
2a13c 65 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d  en);..      }...
2a13d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 28  .      if( 0==((
2a13e 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e 66  p->flags|pNew->f
2a13f 6c 61 67 73 29 20 26 20 45 50 5f 54 6f 6b 65 6e  lags) & EP_Token
2a140 4f 6e 6c 79 29 20 29 7b 0d 0a 20 20 20 20 20 20  Only) ){..      
2a141 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
2a142 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74   pNew->x.pSelect
2a143 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73   or pNew->x.pLis
2a144 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0d 0a 20 20  t member. */..  
2a145 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
2a146 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
2a147 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20  xIsSelect) ){.. 
2a148 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78           pNew->x
2a149 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  .pSelect = sqlit
2a14a 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
2a14b 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73  p->x.pSelect, is
2a14c 52 65 64 75 63 65 64 29 3b 0d 0a 20 20 20 20 20  Reduced);..     
2a14d 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2a14e 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69       pNew->x.pLi
2a14f 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2a150 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78  ListDup(db, p->x
2a151 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65  .pList, isReduce
2a152 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  d);..        }..
2a153 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
2a154 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 4e 65 77   /* Fill in pNew
2a155 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 4e 65 77  ->pLeft and pNew
2a156 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0d 0a 20 20  ->pRight. */..  
2a157 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 41      if( ExprHasA
2a158 6e 79 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c  nyProperty(pNew,
2a159 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54   EP_Reduced|EP_T
2a15a 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0d 0a 20 20  okenOnly) ){..  
2a15b 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d 20        zAlloc += 
2a15c 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
2a15d 65 28 70 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20  e(p, flags);..  
2a15e 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
2a15f 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20  sProperty(pNew, 
2a160 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0d 0a  EP_Reduced) ){..
2a161 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
2a162 70 4c 65 66 74 20 3d 20 65 78 70 72 44 75 70 28  pLeft = exprDup(
2a163 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58  db, p->pLeft, EX
2a164 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a  PRDUP_REDUCE, &z
2a165 41 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20 20 20 20  Alloc);..       
2a166 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20     pNew->pRight 
2a167 3d 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d  = exprDup(db, p-
2a168 3e 70 52 69 67 68 74 2c 20 45 58 50 52 44 55 50  >pRight, EXPRDUP
2a169 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63  _REDUCE, &zAlloc
2a16a 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
2a16b 20 20 20 20 20 20 20 69 66 28 20 70 7a 42 75 66         if( pzBuf
2a16c 66 65 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  fer ){..        
2a16d 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a 41    *pzBuffer = zA
2a16e 6c 6c 6f 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d  lloc;..        }
2a16f 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
2a170 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c          pNew->fl
2a171 61 67 73 32 20 3d 20 30 3b 0d 0a 20 20 20 20 20  ags2 = 0;..     
2a172 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41     if( !ExprHasA
2a173 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  nyProperty(p, EP
2a174 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0d 0a  _TokenOnly) ){..
2a175 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
2a176 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  pLeft = sqlite3E
2a177 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c  xprDup(db, p->pL
2a178 65 66 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  eft, 0);..      
2a179 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74      pNew->pRight
2a17a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2a17b 70 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c  p(db, p->pRight,
2a17c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   0);..        }.
2a17d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  .      }....    
2a17e 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
2a17f 20 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pNew;..}..../*.
2a180 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2a181 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  g group of routi
2a182 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f  nes make deep co
2a183 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 69  pies of expressi
2a184 6f 6e 73 2c 0d 0a 2a 2a 20 65 78 70 72 65 73 73  ons,..** express
2a185 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69  ion lists, ID li
2a186 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20  sts, and select 
2a187 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65  statements.  The
2a188 20 63 6f 70 69 65 73 20 63 61 6e 0d 0a 2a 2a 20   copies can..** 
2a189 62 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62  be deleted (by b
2a18a 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74  eing passed to t
2a18b 68 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20  heir respective 
2a18c 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74  ...Delete() rout
2a18d 69 6e 65 73 29 0d 0a 2a 2a 20 77 69 74 68 6f 75  ines)..** withou
2a18e 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 20  t effecting the 
2a18f 6f 72 69 67 69 6e 61 6c 73 2e 0d 0a 2a 2a 0d 0a  originals...**..
2a190 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  ** The expressio
2a191 6e 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20  n list, ID, and 
2a192 73 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74  source lists ret
2a193 75 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78  urn by sqlite3Ex
2a194 70 72 4c 69 73 74 44 75 70 28 29 2c 0d 0a 2a 2a  prListDup(),..**
2a195 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
2a196 70 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  p(), and sqlite3
2a197 53 72 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e  SrcListDup() can
2a198 20 6e 6f 74 20 62 65 20 66 75 72 74 68 65 72 20   not be further 
2a199 65 78 70 61 6e 64 65 64 20 0d 0a 2a 2a 20 62 79  expanded ..** by
2a19a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
2a19b 73 20 74 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74  s to sqlite*List
2a19c 41 70 70 65 6e 64 28 29 20 72 6f 75 74 69 6e 65  Append() routine
2a19d 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 79 20 74  s...**..** Any t
2a19e 61 62 6c 65 73 20 74 68 61 74 20 74 68 65 20 53  ables that the S
2a19f 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70 6f 69  rcList might poi
2a1a0 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 64 75  nt to are not du
2a1a1 70 6c 69 63 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a  plicated...**..*
2a1a2 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61  * The flags para
2a1a3 6d 65 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61  meter contains a
2a1a4 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
2a1a5 74 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 20  the EXPRDUP_XXX 
2a1a6 66 6c 61 67 73 2e 0d 0a 2a 2a 20 49 66 20 74 68  flags...** If th
2a1a7 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  e EXPRDUP_REDUCE
2a1a8 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
2a1a9 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65  en the structure
2a1aa 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 0d 0a   returned is a..
2a1ab 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 76 65 72  ** truncated ver
2a1ac 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 73 75 61  sion of the usua
2a1ad 6c 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  l Expr structure
2a1ae 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74   that will be st
2a1af 6f 72 65 64 20 61 73 0d 0a 2a 2a 20 70 61 72 74  ored as..** part
2a1b0 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
2a1b1 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
2a1b2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2a1b3 20 73 63 68 65 6d 61 2e 0d 0a 2a 2f 0d 0a 53 51   schema...*/..SQ
2a1b4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
2a1b5 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75  r *sqlite3ExprDu
2a1b6 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  p(sqlite3 *db, E
2a1b7 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
2a1b8 73 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 65 78  s){..  return ex
2a1b9 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c 61  prDup(db, p, fla
2a1ba 67 73 2c 20 30 29 3b 0d 0a 7d 0d 0a 53 51 4c 49  gs, 0);..}..SQLI
2a1bb 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c  TE_PRIVATE ExprL
2a1bc 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72  ist *sqlite3Expr
2a1bd 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20  ListDup(sqlite3 
2a1be 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  *db, ExprList *p
2a1bf 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0d 0a 20  , int flags){.. 
2a1c0 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b   ExprList *pNew;
2a1c1 0d 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ..  struct ExprL
2a1c2 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c  ist_item *pItem,
2a1c3 20 2a 70 4f 6c 64 49 74 65 6d 3b 0d 0a 20 20 69   *pOldItem;..  i
2a1c4 6e 74 20 69 3b 0d 0a 20 20 69 66 28 20 70 3d 3d  nt i;..  if( p==
2a1c5 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20  0 ) return 0;.. 
2a1c6 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
2a1c7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
2a1c8 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0d  izeof(*pNew) );.
2a1c9 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
2a1ca 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 70 4e   return 0;..  pN
2a1cb 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30  ew->iECursor = 0
2a1cc 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72  ;..  pNew->nExpr
2a1cd 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20   = pNew->nAlloc 
2a1ce 3d 20 70 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 70  = p->nExpr;..  p
2a1cf 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d  New->a = pItem =
2a1d0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2a1d1 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70  Raw(db,  p->nExp
2a1d2 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d  r*sizeof(p->a[0]
2a1d3 29 20 29 3b 0d 0a 20 20 69 66 28 20 70 49 74 65  ) );..  if( pIte
2a1d4 6d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c  m==0 ){..    sql
2a1d5 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a1d6 4e 65 77 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  New);..    retur
2a1d7 6e 20 30 3b 0d 0a 20 20 7d 20 0d 0a 20 20 70 4f  n 0;..  } ..  pO
2a1d8 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0d 0a  ldItem = p->a;..
2a1d9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2a1da 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
2a1db 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29  m++, pOldItem++)
2a1dc 7b 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c  {..    Expr *pOl
2a1dd 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d  dExpr = pOldItem
2a1de 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 70 49  ->pExpr;..    pI
2a1df 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  tem->pExpr = sql
2a1e0 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2a1e1 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29  pOldExpr, flags)
2a1e2 3b 0d 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e  ;..    pItem->zN
2a1e3 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
2a1e4 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  trDup(db, pOldIt
2a1e5 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20  em->zName);..   
2a1e6 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20   pItem->zSpan = 
2a1e7 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
2a1e8 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53  db, pOldItem->zS
2a1e9 70 61 6e 29 3b 0d 0a 20 20 20 20 70 49 74 65 6d  pan);..    pItem
2a1ea 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f  ->sortOrder = pO
2a1eb 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65  ldItem->sortOrde
2a1ec 72 3b 0d 0a 20 20 20 20 70 49 74 65 6d 2d 3e 64  r;..    pItem->d
2a1ed 6f 6e 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70 49  one = 0;..    pI
2a1ee 74 65 6d 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c  tem->iOrderByCol
2a1ef 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 4f 72   = pOldItem->iOr
2a1f0 64 65 72 42 79 43 6f 6c 3b 0d 0a 20 20 20 20 70  derByCol;..    p
2a1f1 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 70  Item->iAlias = p
2a1f2 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 61 73 3b  OldItem->iAlias;
2a1f3 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2a1f4 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  pNew;..}..../*..
2a1f5 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c 20 74  ** If cursors, t
2a1f6 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 20 61  riggers, views a
2a1f7 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 61 72  nd subqueries ar
2a1f8 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72  e all omitted fr
2a1f9 6f 6d 0d 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64  om..** the build
2a1fa 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74  , then none of t
2a1fb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
2a1fc 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 6f  tines, except fo
2a1fd 72 20 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65  r ..** sqlite3Se
2a1fe 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62  lectDup(), can b
2a1ff 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65  e called. sqlite
2a200 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 20  3SelectDup() is 
2a201 73 6f 6d 65 74 69 6d 65 73 0d 0a 2a 2a 20 63 61  sometimes..** ca
2a202 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
2a203 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a   argument...*/..
2a204 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2a205 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2a206 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2a207 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
2a208 5c 0d 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  \.. || !defined(
2a209 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2a20a 55 45 52 59 29 0d 0a 53 51 4c 49 54 45 5f 50 52  UERY)..SQLITE_PR
2a20b 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
2a20c 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
2a20d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72  (sqlite3 *db, Sr
2a20e 63 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c  cList *p, int fl
2a20f 61 67 73 29 7b 0d 0a 20 20 53 72 63 4c 69 73 74  ags){..  SrcList
2a210 20 2a 70 4e 65 77 3b 0d 0a 20 20 69 6e 74 20 69   *pNew;..  int i
2a211 3b 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0d  ;..  int nByte;.
2a212 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
2a213 74 75 72 6e 20 30 3b 0d 0a 20 20 6e 42 79 74 65  turn 0;..  nByte
2a214 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20   = sizeof(*p) + 
2a215 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a  (p->nSrc>0 ? siz
2a216 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28  eof(p->a[0]) * (
2a217 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b  p->nSrc-1) : 0);
2a218 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ..  pNew = sqlit
2a219 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
2a21a 2c 20 6e 42 79 74 65 20 29 3b 0d 0a 20 20 69 66  , nByte );..  if
2a21b 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
2a21c 72 6e 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6e  rn 0;..  pNew->n
2a21d 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c  Src = pNew->nAll
2a21e 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b 0d 0a 20  oc = p->nSrc;.. 
2a21f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
2a220 53 72 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  Src; i++){..    
2a221 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2a222 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20  tem *pNewItem = 
2a223 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0d 0a 20 20  &pNew->a[i];..  
2a224 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2a225 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20  _item *pOldItem 
2a226 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0d 0a 20 20 20  = &p->a[i];..   
2a227 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a 20   Table *pTab;.. 
2a228 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61     pNewItem->zDa
2a229 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33  tabase = sqlite3
2a22a 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
2a22b 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  dItem->zDatabase
2a22c 29 3b 0d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  );..    pNewItem
2a22d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
2a22e 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
2a22f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0d  ldItem->zName);.
2a230 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
2a231 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44  Alias = sqlite3D
2a232 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
2a233 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0d 0a  Item->zAlias);..
2a234 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f      pNewItem->jo
2a235 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65  intype = pOldIte
2a236 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0d 0a 20 20  m->jointype;..  
2a237 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72    pNewItem->iCur
2a238 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  sor = pOldItem->
2a239 69 43 75 72 73 6f 72 3b 0d 0a 20 20 20 20 70 4e  iCursor;..    pN
2a23a 65 77 49 74 65 6d 2d 3e 61 64 64 72 46 69 6c 6c  ewItem->addrFill
2a23b 53 75 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  Sub = pOldItem->
2a23c 61 64 64 72 46 69 6c 6c 53 75 62 3b 0d 0a 20 20  addrFillSub;..  
2a23d 20 20 70 4e 65 77 49 74 65 6d 2d 3e 72 65 67 52    pNewItem->regR
2a23e 65 74 75 72 6e 20 3d 20 70 4f 6c 64 49 74 65 6d  eturn = pOldItem
2a23f 2d 3e 72 65 67 52 65 74 75 72 6e 3b 0d 0a 20 20  ->regReturn;..  
2a240 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 43 6f    pNewItem->isCo
2a241 72 72 65 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49  rrelated = pOldI
2a242 74 65 6d 2d 3e 69 73 43 6f 72 72 65 6c 61 74 65  tem->isCorrelate
2a243 64 3b 0d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  d;..    pNewItem
2a244 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74  ->zIndex = sqlit
2a245 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2a246 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29  OldItem->zIndex)
2a247 3b 0d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  ;..    pNewItem-
2a248 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 70 4f  >notIndexed = pO
2a249 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78  ldItem->notIndex
2a24a 65 64 3b 0d 0a 20 20 20 20 70 4e 65 77 49 74 65  ed;..    pNewIte
2a24b 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c 64  m->pIndex = pOld
2a24c 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0d 0a 20  Item->pIndex;.. 
2a24d 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74     pTab = pNewIt
2a24e 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49  em->pTab = pOldI
2a24f 74 65 6d 2d 3e 70 54 61 62 3b 0d 0a 20 20 20 20  tem->pTab;..    
2a250 69 66 28 20 70 54 61 62 20 29 7b 0d 0a 20 20 20  if( pTab ){..   
2a251 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b     pTab->nRef++;
2a252 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4e 65  ..    }..    pNe
2a253 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d  wItem->pSelect =
2a254 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
2a255 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
2a256 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b  pSelect, flags);
2a257 0d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ..    pNewItem->
2a258 70 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pOn = sqlite3Exp
2a259 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
2a25a 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0d  m->pOn, flags);.
2a25b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70  .    pNewItem->p
2a25c 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49  Using = sqlite3I
2a25d 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c  dListDup(db, pOl
2a25e 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0d  dItem->pUsing);.
2a25f 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63  .    pNewItem->c
2a260 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65  olUsed = pOldIte
2a261 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0d 0a 20 20 7d  m->colUsed;..  }
2a262 0d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  ..  return pNew;
2a263 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..}..SQLITE_PRIV
2a264 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
2a265 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c  te3IdListDup(sql
2a266 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74  ite3 *db, IdList
2a267 20 2a 70 29 7b 0d 0a 20 20 49 64 4c 69 73 74 20   *p){..  IdList 
2a268 2a 70 4e 65 77 3b 0d 0a 20 20 69 6e 74 20 69 3b  *pNew;..  int i;
2a269 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
2a26a 65 74 75 72 6e 20 30 3b 0d 0a 20 20 70 4e 65 77  eturn 0;..  pNew
2a26b 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2a26c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
2a26d 28 2a 70 4e 65 77 29 20 29 3b 0d 0a 20 20 69 66  (*pNew) );..  if
2a26e 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
2a26f 72 6e 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6e  rn 0;..  pNew->n
2a270 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f  Id = pNew->nAllo
2a271 63 20 3d 20 70 2d 3e 6e 49 64 3b 0d 0a 20 20 70  c = p->nId;..  p
2a272 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33  New->a = sqlite3
2a273 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2a274 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d  p->nId*sizeof(p-
2a275 3e 61 5b 30 5d 29 20 29 3b 0d 0a 20 20 69 66 28  >a[0]) );..  if(
2a276 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0d 0a   pNew->a==0 ){..
2a277 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2a278 65 28 64 62 2c 20 70 4e 65 77 29 3b 0d 0a 20 20  e(db, pNew);..  
2a279 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
2a27a 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
2a27b 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0d 0a 20 20  ->nId; i++){..  
2a27c 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f    struct IdList_
2a27d 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d  item *pNewItem =
2a27e 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0d 0a 20   &pNew->a[i];.. 
2a27f 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74     struct IdList
2a280 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20  _item *pOldItem 
2a281 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0d 0a 20 20 20  = &p->a[i];..   
2a282 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
2a283 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2a284 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
2a285 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 70 4e  >zName);..    pN
2a286 65 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f  ewItem->idx = pO
2a287 6c 64 49 74 65 6d 2d 3e 69 64 78 3b 0d 0a 20 20  ldItem->idx;..  
2a288 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  }..  return pNew
2a289 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ;..}..SQLITE_PRI
2a28a 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
2a28b 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71  ite3SelectDup(sq
2a28c 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63  lite3 *db, Selec
2a28d 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  t *p, int flags)
2a28e 7b 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  {..  Select *pNe
2a28f 77 2c 20 2a 70 50 72 69 6f 72 3b 0d 0a 20 20 69  w, *pPrior;..  i
2a290 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
2a291 20 30 3b 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71   0;..  pNew = sq
2a292 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2a293 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20  (db, sizeof(*p) 
2a294 29 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d  );..  if( pNew==
2a295 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20  0 ) return 0;.. 
2a296 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20   pNew->pEList = 
2a297 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2a298 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74  up(db, p->pEList
2a299 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20 70 4e 65  , flags);..  pNe
2a29a 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65  w->pSrc = sqlite
2a29b 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20  3SrcListDup(db, 
2a29c 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29 3b  p->pSrc, flags);
2a29d 0d 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65  ..  pNew->pWhere
2a29e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2a29f 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c  p(db, p->pWhere,
2a2a0 20 66 6c 61 67 73 29 3b 0d 0a 20 20 70 4e 65 77   flags);..  pNew
2a2a1 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c  ->pGroupBy = sql
2a2a2 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2a2a3 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
2a2a4 20 66 6c 61 67 73 29 3b 0d 0a 20 20 70 4e 65 77   flags);..  pNew
2a2a5 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69  ->pHaving = sqli
2a2a6 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2a2a7 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73  ->pHaving, flags
2a2a8 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64  );..  pNew->pOrd
2a2a9 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  erBy = sqlite3Ex
2a2aa 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
2a2ab 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73  >pOrderBy, flags
2a2ac 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d  );..  pNew->op =
2a2ad 20 70 2d 3e 6f 70 3b 0d 0a 20 20 70 4e 65 77 2d   p->op;..  pNew-
2a2ae 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72  >pPrior = pPrior
2a2af 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2a2b0 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  Dup(db, p->pPrio
2a2b1 72 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20 69 66  r, flags);..  if
2a2b2 28 20 70 50 72 69 6f 72 20 29 20 70 50 72 69 6f  ( pPrior ) pPrio
2a2b3 72 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b  r->pNext = pNew;
2a2b4 0d 0a 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20  ..  pNew->pNext 
2a2b5 3d 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70 4c  = 0;..  pNew->pL
2a2b6 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 78  imit = sqlite3Ex
2a2b7 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 69  prDup(db, p->pLi
2a2b8 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20  mit, flags);..  
2a2b9 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20  pNew->pOffset = 
2a2ba 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2a2bb 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 66  b, p->pOffset, f
2a2bc 6c 61 67 73 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e  lags);..  pNew->
2a2bd 69 4c 69 6d 69 74 20 3d 20 30 3b 0d 0a 20 20 70  iLimit = 0;..  p
2a2be 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30  New->iOffset = 0
2a2bf 3b 0d 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c  ;..  pNew->selFl
2a2c0 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67  ags = p->selFlag
2a2c1 73 20 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65  s & ~SF_UsesEphe
2a2c2 6d 65 72 61 6c 3b 0d 0a 20 20 70 4e 65 77 2d 3e  meral;..  pNew->
2a2c3 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0d  pRightmost = 0;.
2a2c4 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
2a2c5 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0d 0a  nEphm[0] = -1;..
2a2c6 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
2a2c7 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0d 0a 20  Ephm[1] = -1;.. 
2a2c8 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
2a2c9 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0d 0a 20 20  phm[2] = -1;..  
2a2ca 72 65 74 75 72 6e 20 70 4e 65 77 3b 0d 0a 7d 0d  return pNew;..}.
2a2cb 0a 23 65 6c 73 65 0d 0a 53 51 4c 49 54 45 5f 50  .#else..SQLITE_P
2a2cc 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
2a2cd 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2a2ce 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
2a2cf 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  ect *p, int flag
2a2d0 73 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  s){..  assert( p
2a2d1 3d 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e  ==0 );..  return
2a2d2 20 30 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a   0;..}..#endif..
2a2d3 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61  ..../*..** Add a
2a2d4 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20   new element to 
2a2d5 74 68 65 20 65 6e 64 20 6f 66 20 61 6e 20 65 78  the end of an ex
2a2d6 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
2a2d7 49 66 20 70 4c 69 73 74 20 69 73 0d 0a 2a 2a 20  If pList is..** 
2a2d8 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20  initially NULL, 
2a2d9 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65  then create a ne
2a2da 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  w expression lis
2a2db 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20  t...**..** If a 
2a2dc 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2a2dd 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2a2de 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20  the entire list 
2a2df 69 73 20 66 72 65 65 64 20 61 6e 64 0d 0a 2a 2a  is freed and..**
2a2e0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
2a2e1 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20  d.  If non-NULL 
2a2e2 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65  is returned, the
2a2e3 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  n it is guarante
2a2e4 65 64 0d 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ed..** that the 
2a2e5 6e 65 77 20 65 6e 74 72 79 20 77 61 73 20 73 75  new entry was su
2a2e6 63 63 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e  ccessfully appen
2a2e7 64 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ded...*/..SQLITE
2a2e8 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
2a2e9 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
2a2ea 73 74 41 70 70 65 6e 64 28 0d 0a 20 20 50 61 72  stAppend(..  Par
2a2eb 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2a2ec 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2a2ed 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78  context */..  Ex
2a2ee 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
2a2ef 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f        /* List to
2a2f0 20 77 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64   which to append
2a2f1 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20  . Might be NULL 
2a2f2 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70  */..  Expr *pExp
2a2f3 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r             /*
2a2f4 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   Expression to b
2a2f5 65 20 61 70 70 65 6e 64 65 64 2e 20 4d 69 67 68  e appended. Migh
2a2f6 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0d 0a 29 7b  t be NULL */..){
2a2f7 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
2a2f8 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20  = pParse->db;.. 
2a2f9 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
2a2fa 0d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  ..    pList = sq
2a2fb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2a2fc 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70  o(db, sizeof(Exp
2a2fd 72 4c 69 73 74 29 20 29 3b 0d 0a 20 20 20 20 69  rList) );..    i
2a2fe 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0d 0a  f( pList==0 ){..
2a2ff 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65        goto no_me
2a300 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61  m;..    }..    a
2a301 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 41  ssert( pList->nA
2a302 6c 6c 6f 63 3d 3d 30 20 29 3b 0d 0a 20 20 7d 0d  lloc==0 );..  }.
2a303 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41  .  if( pList->nA
2a304 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78  lloc<=pList->nEx
2a305 70 72 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63  pr ){..    struc
2a306 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2a307 2a 61 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 20 3d  *a;..    int n =
2a308 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32   pList->nAlloc*2
2a309 20 2b 20 34 3b 0d 0a 20 20 20 20 61 20 3d 20 73   + 4;..    a = s
2a30a 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
2a30b 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a  db, pList->a, n*
2a30c 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b  sizeof(pList->a[
2a30d 30 5d 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 61  0]));..    if( a
2a30e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f  ==0 ){..      go
2a30f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20  to no_mem;..    
2a310 7d 0d 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 20  }..    pList->a 
2a311 3d 20 61 3b 0d 0a 20 20 20 20 70 4c 69 73 74 2d  = a;..    pList-
2a312 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  >nAlloc = sqlite
2a313 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62  3DbMallocSize(db
2a314 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30 5d  , a)/sizeof(a[0]
2a315 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  );..  }..  asser
2a316 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29  t( pList->a!=0 )
2a317 3b 0d 0a 20 20 69 66 28 20 31 20 29 7b 0d 0a 20  ;..  if( 1 ){.. 
2a318 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2a319 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2a31a 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74   &pList->a[pList
2a31b 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0d 0a 20 20 20  ->nExpr++];..   
2a31c 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c 20 30   memset(pItem, 0
2a31d 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 6d 29  , sizeof(*pItem)
2a31e 29 3b 0d 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70  );..    pItem->p
2a31f 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0d 0a 20  Expr = pExpr;.. 
2a320 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69   }..  return pLi
2a321 73 74 3b 0d 0a 0d 0a 6e 6f 5f 6d 65 6d 3a 20 20  st;....no_mem:  
2a322 20 20 20 0d 0a 20 20 2f 2a 20 41 76 6f 69 64 20     ..  /* Avoid 
2a323 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69  leaking memory i
2a324 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69  f malloc has fai
2a325 6c 65 64 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  led. */..  sqlit
2a326 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2a327 20 70 45 78 70 72 29 3b 0d 0a 20 20 73 71 6c 69   pExpr);..  sqli
2a328 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2a329 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0d 0a 20  e(db, pList);.. 
2a32a 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d   return 0;..}...
2a32b 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20  ./*..** Set the 
2a32c 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61  ExprList.a[].zNa
2a32d 6d 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  me element of th
2a32e 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
2a32f 61 64 64 65 64 20 69 74 65 6d 0d 0a 2a 2a 20 6f  added item..** o
2a330 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
2a331 20 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70   list...**..** p
2a332 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55  List might be NU
2a333 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20  LL following an 
2a334 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20  OOM error.  But 
2a335 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 6e 65 76  pName should nev
2a336 65 72 20 62 65 0d 0a 2a 2a 20 4e 55 4c 4c 2e 20  er be..** NULL. 
2a337 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   If a memory all
2a338 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74  ocation fails, t
2a339 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  he pParse->db->m
2a33a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
2a33b 0d 0a 2a 2a 20 69 73 20 73 65 74 2e 0d 0a 2a 2f  ..** is set...*/
2a33c 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2a33d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
2a33e 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 0d 0a 20  rListSetName(.. 
2a33f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2a340 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2a341 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  ing context */..
2a342 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2a343 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  t,        /* Lis
2a344 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64  t to which to ad
2a345 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0d 0a  d the span. */..
2a346 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20    Token *pName, 
2a347 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2a348 65 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f  e to be added */
2a349 0d 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65 20  ..  int dequote 
2a34a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2a34b 72 75 65 20 74 6f 20 63 61 75 73 65 20 74 68 65  rue to cause the
2a34c 20 6e 61 6d 65 20 74 6f 20 62 65 20 64 65 71 75   name to be dequ
2a34d 6f 74 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 61  oted */..){..  a
2a34e 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
2a34f 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
2a350 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29  allocFailed!=0 )
2a351 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  ;..  if( pList )
2a352 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  {..    struct Ex
2a353 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2a354 65 6d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  em;..    assert(
2a355 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20   pList->nExpr>0 
2a356 29 3b 0d 0a 20 20 20 20 70 49 74 65 6d 20 3d 20  );..    pItem = 
2a357 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d  &pList->a[pList-
2a358 3e 6e 45 78 70 72 2d 31 5d 3b 0d 0a 20 20 20 20  >nExpr-1];..    
2a359 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 7a  assert( pItem->z
2a35a 4e 61 6d 65 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  Name==0 );..    
2a35b 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  pItem->zName = s
2a35c 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2a35d 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d  pParse->db, pNam
2a35e 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b  e->z, pName->n);
2a35f 0d 0a 20 20 20 20 69 66 28 20 64 65 71 75 6f 74  ..    if( dequot
2a360 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  e && pItem->zNam
2a361 65 20 29 20 73 71 6c 69 74 65 33 44 65 71 75 6f  e ) sqlite3Dequo
2a362 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  te(pItem->zName)
2a363 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
2a364 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
2a365 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65  List.a[].zSpan e
2a366 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f  lement of the mo
2a367 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
2a368 64 20 69 74 65 6d 0d 0a 2a 2a 20 6f 6e 20 74 68  d item..** on th
2a369 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
2a36a 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70 4c 69 73 74  t...**..** pList
2a36b 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66   might be NULL f
2a36c 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20  ollowing an OOM 
2a36d 65 72 72 6f 72 2e 20 20 42 75 74 20 70 53 70 61  error.  But pSpa
2a36e 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  n should never b
2a36f 65 0d 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20  e..** NULL.  If 
2a370 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
2a371 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70  ion fails, the p
2a372 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2a373 63 46 61 69 6c 65 64 20 66 6c 61 67 0d 0a 2a 2a  cFailed flag..**
2a374 20 69 73 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 53 51   is set...*/..SQ
2a375 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a376 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
2a377 74 53 65 74 53 70 61 6e 28 0d 0a 20 20 50 61 72  tSetSpan(..  Par
2a378 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2a379 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2a37a 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78  context */..  Ex
2a37b 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
2a37c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f        /* List to
2a37d 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74 68   which to add th
2a37e 65 20 73 70 61 6e 2e 20 2a 2f 0d 0a 20 20 45 78  e span. */..  Ex
2a37f 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 20 20 20  prSpan *pSpan   
2a380 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 61        /* The spa
2a381 6e 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f  n to be added */
2a382 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  ..){..  sqlite3 
2a383 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2a384 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  ;..  assert( pLi
2a385 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c  st!=0 || db->mal
2a386 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0d  locFailed!=0 );.
2a387 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0d  .  if( pList ){.
2a388 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
2a389 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
2a38a 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69   = &pList->a[pLi
2a38b 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0d 0a 20  st->nExpr-1];.. 
2a38c 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
2a38d 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0d 0a 20 20  ->nExpr>0 );..  
2a38e 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
2a38f 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 49  llocFailed || pI
2a390 74 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61  tem->pExpr==pSpa
2a391 6e 2d 3e 70 45 78 70 72 20 29 3b 0d 0a 20 20 20  n->pExpr );..   
2a392 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2a393 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29  b, pItem->zSpan)
2a394 3b 0d 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53  ;..    pItem->zS
2a395 70 61 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53  pan = sqlite3DbS
2a396 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
2a397 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c  *)pSpan->zStart,
2a398 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2a399 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a39a 20 20 20 20 20 20 28 69 6e 74 29 28 70 53 70 61        (int)(pSpa
2a39b 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d  n->zEnd - pSpan-
2a39c 3e 7a 53 74 61 72 74 29 29 3b 0d 0a 20 20 7d 0d  >zStart));..  }.
2a39d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20  .}..../*..** If 
2a39e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  the expression l
2a39f 69 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61  ist pEList conta
2a3a0 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c  ins more than iL
2a3a1 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0d 0a  imit elements,..
2a3a2 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  ** leave an erro
2a3a3 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
2a3a4 72 73 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  rse...*/..SQLITE
2a3a5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a3a6 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
2a3a7 63 6b 4c 65 6e 67 74 68 28 0d 0a 20 20 50 61 72  ckLength(..  Par
2a3a8 73 65 20 2a 70 50 61 72 73 65 2c 0d 0a 20 20 45  se *pParse,..  E
2a3a9 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
2a3aa 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
2a3ab 7a 4f 62 6a 65 63 74 0d 0a 29 7b 0d 0a 20 20 69  zObject..){..  i
2a3ac 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e  nt mx = pParse->
2a3ad 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2a3ae 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b  E_LIMIT_COLUMN];
2a3af 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45  ..  testcase( pE
2a3b0 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
2a3b1 6e 45 78 70 72 3d 3d 6d 78 20 29 3b 0d 0a 20 20  nExpr==mx );..  
2a3b2 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74  testcase( pEList
2a3b3 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
2a3b4 72 3d 3d 6d 78 2b 31 20 29 3b 0d 0a 20 20 69 66  r==mx+1 );..  if
2a3b5 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69  ( pEList && pELi
2a3b6 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0d  st->nExpr>mx ){.
2a3b7 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2a3b8 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
2a3b9 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69  o many columns i
2a3ba 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 3b  n %s", zObject);
2a3bb 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
2a3bc 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
2a3bd 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  ire expression l
2a3be 69 73 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ist...*/..SQLITE
2a3bf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a3c0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2a3c1 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
2a3c2 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2a3c3 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
2a3c4 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
2a3c5 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 20 20  item *pItem;..  
2a3c6 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2a3c7 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73 65 72 74  eturn;..  assert
2a3c8 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c  ( pList->a!=0 ||
2a3c9 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d   (pList->nExpr==
2a3ca 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  0 && pList->nAll
2a3cb 6f 63 3d 3d 30 29 20 29 3b 0d 0a 20 20 61 73 73  oc==0) );..  ass
2a3cc 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ert( pList->nExp
2a3cd 72 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63  r<=pList->nAlloc
2a3ce 20 29 3b 0d 0a 20 20 66 6f 72 28 70 49 74 65 6d   );..  for(pItem
2a3cf 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20  =pList->a, i=0; 
2a3d0 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
2a3d1 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a  i++, pItem++){..
2a3d2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2a3d3 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d  elete(db, pItem-
2a3d4 3e 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 73 71  >pExpr);..    sq
2a3d5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a3d6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a  pItem->zName);..
2a3d7 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2a3d8 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 70  e(db, pItem->zSp
2a3d9 61 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  an);..  }..  sql
2a3da 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a3db 4c 69 73 74 2d 3e 61 29 3b 0d 0a 20 20 73 71 6c  List->a);..  sql
2a3dc 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a3dd 4c 69 73 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  List);..}..../*.
2a3de 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2a3df 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 63 61  es are Walker ca
2a3e0 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72  llbacks.  Walker
2a3e1 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 6e 74  .u.pi is a point
2a3e2 65 72 0d 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74  er..** to an int
2a3e3 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f 75  eger.  These rou
2a3e4 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b 69  tines are checki
2a3e5 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ng an expression
2a3e6 20 74 6f 20 73 65 65 0d 0a 2a 2a 20 69 66 20 69   to see..** if i
2a3e7 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e  t is a constant.
2a3e8 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e    Set *Walker.u.
2a3e9 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  pi to 0 if the e
2a3ea 78 70 72 65 73 73 69 6f 6e 20 69 73 0d 0a 2a 2a  xpression is..**
2a3eb 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0d 0a   not constant...
2a3ec 2a 2a 0d 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c  **..** These cal
2a3ed 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 61  lback routines a
2a3ee 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  re used to imple
2a3ef 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ment the followi
2a3f0 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  ng:..**..**     
2a3f1 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
2a3f2 73 74 61 6e 74 28 29 0d 0a 2a 2a 20 20 20 20 20  stant()..**     
2a3f3 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
2a3f4 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0d 0a  stantNotJoin()..
2a3f5 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  **     sqlite3Ex
2a3f6 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75  prIsConstantOrFu
2a3f7 6e 63 74 69 6f 6e 28 29 0d 0a 2a 2a 0d 0a 2a 2f  nction()..**..*/
2a3f8 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70  ..static int exp
2a3f9 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28  rNodeIsConstant(
2a3fa 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
2a3fb 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0d 0a   Expr *pExpr){..
2a3fc 0d 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65  ..  /* If pWalke
2a3fd 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 65 6e  r->u.i is 3 then
2a3fe 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65   any term of the
2a3ff 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
2a400 20 63 6f 6d 65 73 20 66 72 6f 6d 0d 0a 20 20 2a   comes from..  *
2a401 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e  * the ON or USIN
2a402 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a  G clauses of a j
2a403 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73  oin disqualifies
2a404 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0d   the expression.
2a405 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67  .  ** from being
2a406 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73   considered cons
2a407 74 61 6e 74 2e 20 2a 2f 0d 0a 20 20 69 66 28 20  tant. */..  if( 
2a408 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20  pWalker->u.i==3 
2a409 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  && ExprHasAnyPro
2a40a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2a40b 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0d 0a 20 20  FromJoin) ){..  
2a40c 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
2a40d 20 30 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   0;..    return 
2a40e 57 52 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 7d 0d  WRC_Abort;..  }.
2a40f 0a 0d 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ...  switch( pEx
2a410 70 72 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 2f  pr->op ){..    /
2a411 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74  * Consider funct
2a412 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74  ions to be const
2a413 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72  ant if all their
2a414 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63   arguments are c
2a415 6f 6e 73 74 61 6e 74 0d 0a 20 20 20 20 2a 2a 20  onstant..    ** 
2a416 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69  and pWalker->u.i
2a417 3d 3d 32 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65  ==2 */..    case
2a418 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0d 0a 20   TK_FUNCTION:.. 
2a419 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72       if( pWalker
2a41a 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72  ->u.i==2 ) retur
2a41b 6e 20 30 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 46  n 0;..      /* F
2a41c 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0d 0a  all through */..
2a41d 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0d      case TK_ID:.
2a41e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
2a41f 55 4d 4e 3a 0d 0a 20 20 20 20 63 61 73 65 20 54  UMN:..    case T
2a420 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0d  K_AGG_FUNCTION:.
2a421 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
2a422 5f 43 4f 4c 55 4d 4e 3a 0d 0a 20 20 20 20 20 20  _COLUMN:..      
2a423 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
2a424 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0d 0a 20  >op==TK_ID );.. 
2a425 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2a426 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
2a427 55 4d 4e 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  UMN );..      te
2a428 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2a429 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p==TK_AGG_FUNCTI
2a42a 4f 4e 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  ON );..      tes
2a42b 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
2a42c 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
2a42d 29 3b 0d 0a 20 20 20 20 20 20 70 57 61 6c 6b 65  );..      pWalke
2a42e 72 2d 3e 75 2e 69 20 3d 20 30 3b 0d 0a 20 20 20  r->u.i = 0;..   
2a42f 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
2a430 6f 72 74 3b 0d 0a 20 20 20 20 64 65 66 61 75 6c  ort;..    defaul
2a431 74 3a 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  t:..      testca
2a432 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
2a433 4b 5f 53 45 4c 45 43 54 20 29 3b 20 2f 2a 20 73  K_SELECT ); /* s
2a434 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74  electNodeIsConst
2a435 61 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f  ant will disallo
2a436 77 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  w */..      test
2a437 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
2a438 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 20 2f 2a  =TK_EXISTS ); /*
2a439 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e   selectNodeIsCon
2a43a 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c  stant will disal
2a43b 6c 6f 77 20 2a 2f 0d 0a 20 20 20 20 20 20 72 65  low */..      re
2a43c 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
2a43d 65 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73 74 61 74  e;..  }..}..stat
2a43e 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64  ic int selectNod
2a43f 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b  eIsConstant(Walk
2a440 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
2a441 65 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a  ect *NotUsed){..
2a442 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2a443 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20 20  ER(NotUsed);..  
2a444 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30  pWalker->u.i = 0
2a445 3b 0d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  ;..  return WRC_
2a446 41 62 6f 72 74 3b 0d 0a 7d 0d 0a 73 74 61 74 69  Abort;..}..stati
2a447 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73  c int exprIsCons
2a448 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69  t(Expr *p, int i
2a449 6e 69 74 46 6c 61 67 29 7b 0d 0a 20 20 57 61 6c  nitFlag){..  Wal
2a44a 6b 65 72 20 77 3b 0d 0a 20 20 77 2e 75 2e 69 20  ker w;..  w.u.i 
2a44b 3d 20 69 6e 69 74 46 6c 61 67 3b 0d 0a 20 20 77  = initFlag;..  w
2a44c 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
2a44d 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
2a44e 61 6e 74 3b 0d 0a 20 20 77 2e 78 53 65 6c 65 63  ant;..  w.xSelec
2a44f 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65  tCallback = sele
2a450 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  ctNodeIsConstant
2a451 3b 0d 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  ;..  sqlite3Walk
2a452 45 78 70 72 28 26 77 2c 20 70 29 3b 0d 0a 20 20  Expr(&w, p);..  
2a453 72 65 74 75 72 6e 20 77 2e 75 2e 69 3b 0d 0a 7d  return w.u.i;..}
2a454 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 61 6c 6b 20  ..../*..** Walk 
2a455 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
2a456 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  ee.  Return 1 if
2a457 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2a458 69 73 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a 2a 20  is constant..** 
2a459 61 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f  and 0 if it invo
2a45a 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f  lves variables o
2a45b 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  r function calls
2a45c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 74 68  ...**..** For th
2a45d 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
2a45e 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64  is function, a d
2a45f 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
2a460 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0d  ing (ex: "abc").
2a461 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65  .** is considere
2a462 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74  d a variable but
2a463 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64   a single-quoted
2a464 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62   string (ex: 'ab
2a465 63 27 29 20 69 73 0d 0a 2a 2a 20 61 20 63 6f 6e  c') is..** a con
2a466 73 74 61 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  stant...*/..SQLI
2a467 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2a468 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
2a469 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0d 0a  tant(Expr *p){..
2a46a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43    return exprIsC
2a46b 6f 6e 73 74 28 70 2c 20 31 29 3b 0d 0a 7d 0d 0a  onst(p, 1);..}..
2a46c 0d 0a 2f 2a 0d 0a 2a 2a 20 57 61 6c 6b 20 61 6e  ../*..** Walk an
2a46d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
2a46e 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  .  Return 1 if t
2a46f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2a470 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a 2a 20 74 68   constant..** th
2a471 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 69 67 69  at does no origi
2a472 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 20 4f 4e  nate from the ON
2a473 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
2a474 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0d 0a 2a 2a  s of a join...**
2a475 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20   Return 0 if it 
2a476 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c  involves variabl
2a477 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63  es or function c
2a478 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 66 72  alls or terms fr
2a479 6f 6d 0d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 20  om..** an ON or 
2a47a 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0d 0a 2a  USING clause...*
2a47b 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2a47c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
2a47d 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
2a47e 69 6e 28 45 78 70 72 20 2a 70 29 7b 0d 0a 20 20  in(Expr *p){..  
2a47f 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e  return exprIsCon
2a480 73 74 28 70 2c 20 33 29 3b 0d 0a 7d 0d 0a 0d 0a  st(p, 3);..}....
2a481 2f 2a 0d 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  /*..** Walk an e
2a482 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
2a483 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
2a484 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63   expression is c
2a485 6f 6e 73 74 61 6e 74 0d 0a 2a 2a 20 6f 72 20 61  onstant..** or a
2a486 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77   function call w
2a487 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67  ith constant arg
2a488 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20  uments.  Return 
2a489 61 6e 64 20 30 20 69 66 20 74 68 65 72 65 0d 0a  and 0 if there..
2a48a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69 61  ** are any varia
2a48b 62 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f  bles...**..** Fo
2a48c 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
2a48d 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  f this function,
2a48e 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64   a double-quoted
2a48f 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62   string (ex: "ab
2a490 63 22 29 0d 0a 2a 2a 20 69 73 20 63 6f 6e 73 69  c")..** is consi
2a491 64 65 72 65 64 20 61 20 76 61 72 69 61 62 6c 65  dered a variable
2a492 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75   but a single-qu
2a493 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a  oted string (ex:
2a494 20 27 61 62 63 27 29 20 69 73 0d 0a 2a 2a 20 61   'abc') is..** a
2a495 20 63 6f 6e 73 74 61 6e 74 2e 0d 0a 2a 2f 0d 0a   constant...*/..
2a496 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a497 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
2a498 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69  ConstantOrFuncti
2a499 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0d 0a 20 20  on(Expr *p){..  
2a49a 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e  return exprIsCon
2a49b 73 74 28 70 2c 20 32 29 3b 0d 0a 7d 0d 0a 0d 0a  st(p, 2);..}....
2a49c 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  /*..** If the ex
2a49d 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73  pression p codes
2a49e 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65   a constant inte
2a49f 67 65 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c  ger that is smal
2a4a0 6c 20 65 6e 6f 75 67 68 0d 0a 2a 2a 20 74 6f 20  l enough..** to 
2a4a1 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20  fit in a 32-bit 
2a4a2 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
2a4a3 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 61  1 and put the va
2a4a4 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  lue of the integ
2a4a5 65 72 0d 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75  er..** in *pValu
2a4a6 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65  e.  If the expre
2a4a7 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20  ssion is not an 
2a4a8 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74  integer or if it
2a4a9 20 69 73 20 74 6f 6f 20 62 69 67 0d 0a 2a 2a 20   is too big..** 
2a4aa 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e  to fit in a sign
2a4ab 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  ed 32-bit intege
2a4ac 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20  r, return 0 and 
2a4ad 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e  leave *pValue un
2a4ae 63 68 61 6e 67 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  changed...*/..SQ
2a4af 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a4b0 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e   sqlite3ExprIsIn
2a4b1 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69  teger(Expr *p, i
2a4b2 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0d 0a 20 20  nt *pValue){..  
2a4b3 69 6e 74 20 72 63 20 3d 20 30 3b 0d 0a 0d 0a 20  int rc = 0;.... 
2a4b4 20 2f 2a 20 49 66 20 61 6e 20 65 78 70 72 65 73   /* If an expres
2a4b5 73 69 6f 6e 20 69 73 20 61 6e 20 69 6e 74 65 67  sion is an integ
2a4b6 65 72 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20  er literal that 
2a4b7 66 69 74 73 20 69 6e 20 61 20 73 69 67 6e 65 64  fits in a signed
2a4b8 20 33 32 2d 62 69 74 0d 0a 20 20 2a 2a 20 69 6e   32-bit..  ** in
2a4b9 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
2a4ba 45 50 5f 49 6e 74 56 61 6c 75 65 20 66 6c 61 67  EP_IntValue flag
2a4bb 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61   will have alrea
2a4bc 64 79 20 62 65 65 6e 20 73 65 74 20 2a 2f 0d 0a  dy been set */..
2a4bd 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 21    assert( p->op!
2a4be 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 28  =TK_INTEGER || (
2a4bf 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e  p->flags & EP_In
2a4c0 74 56 61 6c 75 65 29 21 3d 30 0d 0a 20 20 20 20  tValue)!=0..    
2a4c1 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
2a4c2 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a  3GetInt32(p->u.z
2a4c3 54 6f 6b 65 6e 2c 20 26 72 63 29 3d 3d 30 20 29  Token, &rc)==0 )
2a4c4 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 2d 3e 66 6c  ;....  if( p->fl
2a4c5 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75  ags & EP_IntValu
2a4c6 65 20 29 7b 0d 0a 20 20 20 20 2a 70 56 61 6c 75  e ){..    *pValu
2a4c7 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c 75 65 3b  e = p->u.iValue;
2a4c8 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d  ..    return 1;.
2a4c9 0a 20 20 7d 0d 0a 20 20 73 77 69 74 63 68 28 20  .  }..  switch( 
2a4ca 70 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 63 61  p->op ){..    ca
2a4cb 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0d 0a  se TK_UPLUS: {..
2a4cc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2a4cd 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
2a4ce 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65  p->pLeft, pValue
2a4cf 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
2a4d0 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
2a4d1 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0d 0a  e TK_UMINUS: {..
2a4d2 20 20 20 20 20 20 69 6e 74 20 76 3b 0d 0a 20 20        int v;..  
2a4d3 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
2a4d4 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e  xprIsInteger(p->
2a4d5 70 4c 65 66 74 2c 20 26 76 29 20 29 7b 0d 0a 20  pLeft, &v) ){.. 
2a4d6 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65 20 3d         *pValue =
2a4d7 20 2d 76 3b 0d 0a 20 20 20 20 20 20 20 20 72 63   -v;..        rc
2a4d8 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a   = 1;..      }..
2a4d9 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
2a4da 20 20 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74    }..    default
2a4db 3a 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20  : break;..  }.. 
2a4dc 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
2a4dd 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
2a4de 46 41 4c 53 45 20 69 66 20 74 68 65 72 65 20 69  FALSE if there i
2a4df 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74  s no chance that
2a4e0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2a4e1 63 61 6e 20 62 65 20 4e 55 4c 4c 2e 0d 0a 2a 2a  can be NULL...**
2a4e2 0d 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  ..** If the expr
2a4e3 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 62 65 20  ession might be 
2a4e4 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 65  NULL or if the e
2a4e5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 6f 6f  xpression is too
2a4e6 20 63 6f 6d 70 6c 65 78 0d 0a 2a 2a 20 74 6f 20   complex..** to 
2a4e7 74 65 6c 6c 20 72 65 74 75 72 6e 20 54 52 55 45  tell return TRUE
2a4e8 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  .  ..**..** This
2a4e9 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
2a4ea 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74   as an optimizat
2a4eb 69 6f 6e 2c 20 74 6f 20 73 6b 69 70 20 4f 50 5f  ion, to skip OP_
2a4ec 49 73 4e 75 6c 6c 20 6f 70 63 6f 64 65 73 0d 0a  IsNull opcodes..
2a4ed 2a 2a 20 77 68 65 6e 20 77 65 20 6b 6e 6f 77 20  ** when we know 
2a4ee 74 68 61 74 20 61 20 76 61 6c 75 65 20 63 61 6e  that a value can
2a4ef 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 20 48 65  not be NULL.  He
2a4f0 6e 63 65 2c 20 61 20 66 61 6c 73 65 20 70 6f 73  nce, a false pos
2a4f1 69 74 69 76 65 0d 0a 2a 2a 20 28 72 65 74 75 72  itive..** (retur
2a4f2 6e 69 6e 67 20 54 52 55 45 20 77 68 65 6e 20 69  ning TRUE when i
2a4f3 6e 20 66 61 63 74 20 74 68 65 20 65 78 70 72 65  n fact the expre
2a4f4 73 73 69 6f 6e 20 63 61 6e 20 6e 65 76 65 72 20  ssion can never 
2a4f5 62 65 20 4e 55 4c 4c 29 20 6d 69 67 68 74 0d 0a  be NULL) might..
2a4f6 2a 2a 20 62 65 20 61 20 73 6d 61 6c 6c 20 70 65  ** be a small pe
2a4f7 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 20 62 75  rformance hit bu
2a4f8 74 20 69 73 20 6f 74 68 65 72 77 69 73 65 20 68  t is otherwise h
2a4f9 61 72 6d 6c 65 73 73 2e 20 20 4f 6e 20 74 68 65  armless.  On the
2a4fa 20 6f 74 68 65 72 0d 0a 2a 2a 20 68 61 6e 64 2c   other..** hand,
2a4fb 20 61 20 66 61 6c 73 65 20 6e 65 67 61 74 69 76   a false negativ
2a4fc 65 20 28 72 65 74 75 72 6e 69 6e 67 20 46 41 4c  e (returning FAL
2a4fd 53 45 20 77 68 65 6e 20 74 68 65 20 72 65 73 75  SE when the resu
2a4fe 6c 74 20 63 6f 75 6c 64 20 62 65 20 4e 55 4c 4c  lt could be NULL
2a4ff 29 0d 0a 2a 2a 20 77 69 6c 6c 20 6c 69 6b 65 6c  )..** will likel
2a500 79 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69  y result in an i
2a501 6e 63 6f 72 72 65 63 74 20 61 6e 73 77 65 72 2e  ncorrect answer.
2a502 20 20 53 6f 20 77 68 65 6e 20 69 6e 20 64 6f 75    So when in dou
2a503 62 74 2c 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 54  bt, return..** T
2a504 52 55 45 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  RUE...*/..SQLITE
2a505 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2a506 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e 75 6c  ite3ExprCanBeNul
2a507 6c 28 63 6f 6e 73 74 20 45 78 70 72 20 2a 70 29  l(const Expr *p)
2a508 7b 0d 0a 20 20 75 38 20 6f 70 3b 0d 0a 20 20 77  {..  u8 op;..  w
2a509 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  hile( p->op==TK_
2a50a 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  UPLUS || p->op==
2a50b 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 70 20 3d  TK_UMINUS ){ p =
2a50c 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0d 0a 20 20   p->pLeft; }..  
2a50d 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0d 0a 20 20 69  op = p->op;..  i
2a50e 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  f( op==TK_REGIST
2a50f 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 32  ER ) op = p->op2
2a510 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  ;..  switch( op 
2a511 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ){..    case TK_
2a512 49 4e 54 45 47 45 52 3a 0d 0a 20 20 20 20 63 61  INTEGER:..    ca
2a513 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 0d 0a 20  se TK_STRING:.. 
2a514 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54     case TK_FLOAT
2a515 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  :..    case TK_B
2a516 4c 4f 42 3a 0d 0a 20 20 20 20 20 20 72 65 74 75  LOB:..      retu
2a517 72 6e 20 30 3b 0d 0a 20 20 20 20 64 65 66 61 75  rn 0;..    defau
2a518 6c 74 3a 0d 0a 20 20 20 20 20 20 72 65 74 75 72  lt:..      retur
2a519 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  n 1;..  }..}....
2a51a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
2a51b 61 6e 20 4f 50 5f 49 73 4e 75 6c 6c 20 69 6e 73  an OP_IsNull ins
2a51c 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 74 65  truction that te
2a51d 73 74 73 20 72 65 67 69 73 74 65 72 20 69 52 65  sts register iRe
2a51e 67 20 61 6e 64 20 6a 75 6d 70 73 0d 0a 2a 2a 20  g and jumps..** 
2a51f 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 44 65 73  to location iDes
2a520 74 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  t if the value i
2a521 6e 20 69 52 65 67 20 69 73 20 4e 55 4c 4c 2e 20  n iReg is NULL. 
2a522 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 69 52   The value in iR
2a523 65 67 20 0d 0a 2a 2a 20 77 61 73 20 63 6f 6d 70  eg ..** was comp
2a524 75 74 65 64 20 62 79 20 70 45 78 70 72 2e 20 20  uted by pExpr.  
2a525 49 66 20 77 65 20 63 61 6e 20 6c 6f 6f 6b 20 61  If we can look a
2a526 74 20 70 45 78 70 72 20 61 74 20 63 6f 6d 70 69  t pExpr at compi
2a527 6c 65 2d 74 69 6d 65 20 61 6e 64 0d 0a 2a 2a 20  le-time and..** 
2a528 64 65 74 65 72 6d 69 6e 65 20 74 68 61 74 20 69  determine that i
2a529 74 20 63 61 6e 20 6e 65 76 65 72 20 67 65 6e 65  t can never gene
2a52a 72 61 74 65 20 61 20 4e 55 4c 4c 2c 20 74 68 65  rate a NULL, the
2a52b 6e 20 74 68 65 20 4f 50 5f 49 73 4e 75 6c 6c 20  n the OP_IsNull 
2a52c 6f 70 65 72 61 74 69 6f 6e 0d 0a 2a 2a 20 63 61  operation..** ca
2a52d 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 0d 0a 2a  n be omitted...*
2a52e 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2a52f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
2a530 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70  prCodeIsNullJump
2a531 28 0d 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20  (..  Vdbe *v,   
2a532 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2a533 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74  VDBE under const
2a534 72 75 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 63 6f  ruction */..  co
2a535 6e 73 74 20 45 78 70 72 20 2a 70 45 78 70 72 2c  nst Expr *pExpr,
2a536 20 20 2f 2a 20 4f 6e 6c 79 20 67 65 6e 65 72 61    /* Only genera
2a537 74 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 69 66 20  te OP_IsNull if 
2a538 74 68 69 73 20 65 78 70 72 20 63 61 6e 20 62 65  this expr can be
2a539 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e 74 20   NULL */..  int 
2a53a 69 52 65 67 2c 20 20 20 20 20 20 20 20 20 20 20  iReg,           
2a53b 2f 2a 20 54 65 73 74 20 74 68 65 20 76 61 6c 75  /* Test the valu
2a53c 65 20 69 6e 20 74 68 69 73 20 72 65 67 69 73 74  e in this regist
2a53d 65 72 20 66 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a  er for NULL */..
2a53e 20 20 69 6e 74 20 69 44 65 73 74 20 20 20 20 20    int iDest     
2a53f 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
2a540 72 65 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  re if the value 
2a541 69 73 20 6e 75 6c 6c 20 2a 2f 0d 0a 29 7b 0d 0a  is null */..){..
2a542 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
2a543 72 43 61 6e 42 65 4e 75 6c 6c 28 70 45 78 70 72  rCanBeNull(pExpr
2a544 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ) ){..    sqlite
2a545 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a546 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67 2c 20  P_IsNull, iReg, 
2a547 69 44 65 73 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  iDest);..  }..}.
2a548 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
2a549 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
2a54a 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
2a54b 20 61 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 63   a constant whic
2a54c 68 20 77 6f 75 6c 64 20 62 65 0d 0a 2a 2a 20 75  h would be..** u
2a54d 6e 63 68 61 6e 67 65 64 20 62 79 20 4f 50 5f 41  nchanged by OP_A
2a54e 66 66 69 6e 69 74 79 20 77 69 74 68 20 74 68 65  ffinity with the
2a54f 20 61 66 66 69 6e 69 74 79 20 67 69 76 65 6e 20   affinity given 
2a550 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 0d 0a 2a  in the second..*
2a551 2a 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d  * argument...**.
2a552 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2a553 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
2a554 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4f 50 5f  rmine if the OP_
2a555 41 66 66 69 6e 69 74 79 20 6f 70 65 72 61 74 69  Affinity operati
2a556 6f 6e 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 6d  on..** can be om
2a557 69 74 74 65 64 2e 20 20 57 68 65 6e 20 69 6e 20  itted.  When in 
2a558 64 6f 75 62 74 20 72 65 74 75 72 6e 20 46 41 4c  doubt return FAL
2a559 53 45 2e 20 20 41 20 66 61 6c 73 65 20 6e 65 67  SE.  A false neg
2a55a 61 74 69 76 65 0d 0a 2a 2a 20 69 73 20 68 61 72  ative..** is har
2a55b 6d 6c 65 73 73 2e 20 20 41 20 66 61 6c 73 65 20  mless.  A false 
2a55c 70 6f 73 69 74 69 76 65 2c 20 68 6f 77 65 76 65  positive, howeve
2a55d 72 2c 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e  r, can result in
2a55e 20 74 68 65 20 77 72 6f 6e 67 0d 0a 2a 2a 20 61   the wrong..** a
2a55f 6e 73 77 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  nswer...*/..SQLI
2a560 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2a561 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e  qlite3ExprNeedsN
2a562 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  oAffinityChange(
2a563 63 6f 6e 73 74 20 45 78 70 72 20 2a 70 2c 20 63  const Expr *p, c
2a564 68 61 72 20 61 66 66 29 7b 0d 0a 20 20 75 38 20  har aff){..  u8 
2a565 6f 70 3b 0d 0a 20 20 69 66 28 20 61 66 66 3d 3d  op;..  if( aff==
2a566 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
2a567 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 77  ) return 1;..  w
2a568 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  hile( p->op==TK_
2a569 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  UPLUS || p->op==
2a56a 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 70 20 3d  TK_UMINUS ){ p =
2a56b 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0d 0a 20 20   p->pLeft; }..  
2a56c 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0d 0a 20 20 69  op = p->op;..  i
2a56d 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  f( op==TK_REGIST
2a56e 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 32  ER ) op = p->op2
2a56f 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  ;..  switch( op 
2a570 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ){..    case TK_
2a571 49 4e 54 45 47 45 52 3a 20 7b 0d 0a 20 20 20 20  INTEGER: {..    
2a572 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51    return aff==SQ
2a573 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
2a574 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f   || aff==SQLITE_
2a575 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0d 0a 20 20  AFF_NUMERIC;..  
2a576 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b    }..    case TK
2a577 5f 46 4c 4f 41 54 3a 20 7b 0d 0a 20 20 20 20 20  _FLOAT: {..     
2a578 20 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c   return aff==SQL
2a579 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 7c 7c 20  ITE_AFF_REAL || 
2a57a 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
2a57b 4e 55 4d 45 52 49 43 3b 0d 0a 20 20 20 20 7d 0d  NUMERIC;..    }.
2a57c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52  .    case TK_STR
2a57d 49 4e 47 3a 20 7b 0d 0a 20 20 20 20 20 20 72 65  ING: {..      re
2a57e 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54 45  turn aff==SQLITE
2a57f 5f 41 46 46 5f 54 45 58 54 3b 0d 0a 20 20 20 20  _AFF_TEXT;..    
2a580 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  }..    case TK_B
2a581 4c 4f 42 3a 20 7b 0d 0a 20 20 20 20 20 20 72 65  LOB: {..      re
2a582 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 1;..    }..
2a583 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55      case TK_COLU
2a584 4d 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 61 73 73  MN: {..      ass
2a585 65 72 74 28 20 70 2d 3e 69 54 61 62 6c 65 3e 3d  ert( p->iTable>=
2a586 30 20 29 3b 20 20 2f 2a 20 70 20 63 61 6e 6e 6f  0 );  /* p canno
2a587 74 20 62 65 20 70 61 72 74 20 6f 66 20 61 20 43  t be part of a C
2a588 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  HECK constraint 
2a589 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  */..      return
2a58a 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 0d 0a 20   p->iColumn<0.. 
2a58b 20 20 20 20 20 20 20 20 20 26 26 20 28 61 66 66           && (aff
2a58c 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  ==SQLITE_AFF_INT
2a58d 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c  EGER || aff==SQL
2a58e 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29  ITE_AFF_NUMERIC)
2a58f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64 65  ;..    }..    de
2a590 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20  fault: {..      
2a591 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d  return 0;..    }
2a592 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
2a593 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2a594 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  f the given stri
2a595 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63  ng is a row-id c
2a596 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0d 0a 2a 2f 0d  olumn name...*/.
2a597 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a598 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77  int sqlite3IsRow
2a599 69 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  id(const char *z
2a59a 29 7b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ){..  if( sqlite
2a59b 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f  3StrICmp(z, "_RO
2a59c 57 49 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75  WID_")==0 ) retu
2a59d 72 6e 20 31 3b 0d 0a 20 20 69 66 28 20 73 71 6c  rn 1;..  if( sql
2a59e 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
2a59f 52 4f 57 49 44 22 29 3d 3d 30 20 29 20 72 65 74  ROWID")==0 ) ret
2a5a0 75 72 6e 20 31 3b 0d 0a 20 20 69 66 28 20 73 71  urn 1;..  if( sq
2a5a1 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
2a5a2 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75  "OID")==0 ) retu
2a5a3 72 6e 20 31 3b 0d 0a 20 20 72 65 74 75 72 6e 20  rn 1;..  return 
2a5a4 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
2a5a5 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 77  Return true if w
2a5a6 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74 68  e are able to th
2a5a7 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70  e IN operator op
2a5a8 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0d  timization on a.
2a5a9 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65  .** query of the
2a5aa 20 66 6f 72 6d 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20   form..**..**   
2a5ab 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54      x IN (SELECT
2a5ac 20 2e 2e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68   ...)..**..** Wh
2a5ad 65 72 65 20 74 68 65 20 53 45 4c 45 43 54 2e 2e  ere the SELECT..
2a5ae 2e 20 63 6c 61 75 73 65 20 69 73 20 61 73 20 73  . clause is as s
2a5af 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
2a5b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69  parameter to thi
2a5b1 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0d 0a  s..** routine...
2a5b2 2a 2a 0d 0a 2a 2a 20 54 68 65 20 53 65 6c 65 63  **..** The Selec
2a5b3 74 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20  t object passed 
2a5b4 69 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  in has already b
2a5b5 65 65 6e 20 70 72 65 70 72 6f 63 65 73 73 65 64  een preprocessed
2a5b6 20 61 6e 64 20 6e 6f 0d 0a 2a 2a 20 65 72 72 6f   and no..** erro
2a5b7 72 73 20 68 61 76 65 20 62 65 65 6e 20 66 6f 75  rs have been fou
2a5b8 6e 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  nd...*/..#ifndef
2a5b9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2a5ba 51 55 45 52 59 0d 0a 73 74 61 74 69 63 20 69 6e  QUERY..static in
2a5bb 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  t isCandidateFor
2a5bc 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 29  InOpt(Select *p)
2a5bd 7b 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  {..  SrcList *pS
2a5be 72 63 3b 0d 0a 20 20 45 78 70 72 4c 69 73 74 20  rc;..  ExprList 
2a5bf 2a 70 45 4c 69 73 74 3b 0d 0a 20 20 54 61 62 6c  *pEList;..  Tabl
2a5c0 65 20 2a 70 54 61 62 3b 0d 0a 20 20 69 66 28 20  e *pTab;..  if( 
2a5c1 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
2a5c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a5c3 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64     /* right-hand
2a5c4 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20 53   side of IN is S
2a5c5 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 69 66 28 20  ELECT */..  if( 
2a5c6 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75  p->pPrior ) retu
2a5c7 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
2a5c8 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70     /* Not a comp
2a5c9 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0d 0a  ound SELECT */..
2a5ca 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
2a5cb 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
2a5cc 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29  |SF_Aggregate) )
2a5cd 7b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  {..    testcase(
2a5ce 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
2a5cf 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f  (SF_Distinct|SF_
2a5d0 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f  Aggregate))==SF_
2a5d1 44 69 73 74 69 6e 63 74 20 29 3b 0d 0a 20 20 20  Distinct );..   
2a5d2 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73   testcase( (p->s
2a5d3 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
2a5d4 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
2a5d5 61 74 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67  ate))==SF_Aggreg
2a5d6 61 74 65 20 29 3b 0d 0a 20 20 20 20 72 65 74 75  ate );..    retu
2a5d7 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54  rn 0; /* No DIST
2a5d8 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64  INCT keyword and
2a5d9 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75   no aggregate fu
2a5da 6e 63 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 7d 0d  nctions */..  }.
2a5db 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 47  .  assert( p->pG
2a5dc 72 6f 75 70 42 79 3d 3d 30 20 29 3b 20 20 20 20  roupBy==0 );    
2a5dd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
2a5de 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61   no GROUP BY cla
2a5df 75 73 65 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d  use */..  if( p-
2a5e0 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e  >pLimit ) return
2a5e1 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2a5e2 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54   /* Has no LIMIT
2a5e3 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 61 73   clause */..  as
2a5e4 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74  sert( p->pOffset
2a5e5 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
2a5e6 20 20 20 20 20 2f 2a 20 4e 6f 20 4c 49 4d 49 54       /* No LIMIT
2a5e7 20 6d 65 61 6e 73 20 6e 6f 20 4f 46 46 53 45 54   means no OFFSET
2a5e8 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 70 57   */..  if( p->pW
2a5e9 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b  here ) return 0;
2a5ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a5eb 20 48 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c   Has no WHERE cl
2a5ec 61 75 73 65 20 2a 2f 0d 0a 20 20 70 53 72 63 20  ause */..  pSrc 
2a5ed 3d 20 70 2d 3e 70 53 72 63 3b 0d 0a 20 20 61 73  = p->pSrc;..  as
2a5ee 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 3b  sert( pSrc!=0 );
2a5ef 0d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53  ..  if( pSrc->nS
2a5f0 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  rc!=1 ) return 0
2a5f1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ;          /* Si
2a5f2 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f  ngle term in FRO
2a5f3 4d 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69  M clause */..  i
2a5f4 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53  f( pSrc->a[0].pS
2a5f5 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30  elect ) return 0
2a5f6 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 69 73  ;     /* FROM is
2a5f7 20 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 20   not a subquery 
2a5f8 6f 72 20 76 69 65 77 20 2a 2f 0d 0a 20 20 70 54  or view */..  pT
2a5f9 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e  ab = pSrc->a[0].
2a5fa 70 54 61 62 3b 0d 0a 20 20 69 66 28 20 4e 45 56  pTab;..  if( NEV
2a5fb 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65  ER(pTab==0) ) re
2a5fc 74 75 72 6e 20 30 3b 0d 0a 20 20 61 73 73 65 72  turn 0;..  asser
2a5fd 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  t( pTab->pSelect
2a5fe 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
2a5ff 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
2a600 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a   is not a view *
2a601 2f 0d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  /..  if( IsVirtu
2a602 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72  al(pTab) ) retur
2a603 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  n 0;        /* F
2a604 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61  ROM clause not a
2a605 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
2a606 2f 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d  /..  pEList = p-
2a607 3e 70 45 4c 69 73 74 3b 0d 0a 20 20 69 66 28 20  >pEList;..  if( 
2a608 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
2a609 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2a60a 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e     /* One column
2a60b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2a60c 65 74 20 2a 2f 0d 0a 20 20 69 66 28 20 70 45 4c  et */..  if( pEL
2a60d 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  ist->a[0].pExpr-
2a60e 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op!=TK_COLUMN )
2a60f 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65   return 0; /* Re
2a610 73 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sult is a column
2a611 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b   */..  return 1;
2a612 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
2a613 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2a614 45 52 59 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ERY */..../*..**
2a615 20 43 6f 64 65 20 61 6e 20 4f 50 5f 4f 6e 63 65   Code an OP_Once
2a616 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64   instruction and
2a617 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   allocate space 
2a618 66 6f 72 20 69 74 73 20 66 6c 61 67 2e 20 52 65  for its flag. Re
2a619 74 75 72 6e 20 74 68 65 20 0d 0a 2a 2a 20 61 64  turn the ..** ad
2a61a 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77  dress of the new
2a61b 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a   instruction...*
2a61c 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2a61d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64  E int sqlite3Cod
2a61e 65 4f 6e 63 65 28 50 61 72 73 65 20 2a 70 50 61  eOnce(Parse *pPa
2a61f 72 73 65 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76  rse){..  Vdbe *v
2a620 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2a621 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 20  e(pParse);      
2a622 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69  /* Virtual machi
2a623 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  ne being coded *
2a624 2f 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  /..  return sqli
2a625 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a626 20 4f 50 5f 4f 6e 63 65 2c 20 70 50 61 72 73 65   OP_Once, pParse
2a627 2d 3e 6e 4f 6e 63 65 2b 2b 29 3b 0d 0a 7d 0d 0a  ->nOnce++);..}..
2a628 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
2a629 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
2a62a 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  y the implementa
2a62b 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28  tion of the IN (
2a62c 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0d 0a  ...) operator...
2a62d 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74  ** It's job is t
2a62e 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65  o find or create
2a62f 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74   a b-tree struct
2a630 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 20  ure that may be 
2a631 75 73 65 64 0d 0a 2a 2a 20 65 69 74 68 65 72 20  used..** either 
2a632 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62  to test for memb
2a633 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e  ership of the (.
2a634 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74  ..) set or to it
2a635 65 72 61 74 65 20 74 68 72 6f 75 67 68 0d 0a 2a  erate through..*
2a636 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73  * its members, s
2a637 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74  kipping duplicat
2a638 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  es...**..** The 
2a639 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 75 72  index of the cur
2a63a 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  sor opened on th
2a63b 65 20 62 2d 74 72 65 65 20 28 64 61 74 61 62 61  e b-tree (databa
2a63c 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61  se table, databa
2a63d 73 65 20 69 6e 64 65 78 20 0d 0a 2a 2a 20 6f 72  se index ..** or
2a63e 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 29   ephermal table)
2a63f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 58   is stored in pX
2a640 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65 20  ->iTable before 
2a641 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
2a642 74 75 72 6e 73 2e 0d 0a 2a 2a 20 54 68 65 20 72  turns...** The r
2a643 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 6f 66  eturned value of
2a644 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
2a645 6e 64 69 63 61 74 65 73 20 74 68 65 20 62 2d 74  ndicates the b-t
2a646 72 65 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c  ree type, as fol
2a647 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  lows:..**..**   
2a648 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d  IN_INDEX_ROWID -
2a649 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20   The cursor was 
2a64a 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61  opened on a data
2a64b 62 61 73 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 20  base table...** 
2a64c 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58    IN_INDEX_INDEX
2a64d 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61   - The cursor wa
2a64e 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61  s opened on a da
2a64f 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0d 0a 2a  tabase index...*
2a650 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48  *   IN_INDEX_EPH
2a651 20 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20   -   The cursor 
2a652 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20  was opened on a 
2a653 73 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65  specially create
2a654 64 20 61 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 20  d and..**       
2a655 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 70               pop
2a656 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c  ulated epheremal
2a657 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   table...**..** 
2a658 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72  An existing b-tr
2a659 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ee may only be u
2a65a 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43  sed if the SELEC
2a65b 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70  T is of the simp
2a65c 6c 65 0d 0a 2a 2a 20 66 6f 72 6d 3a 0d 0a 2a 2a  le..** form:..**
2a65d 0d 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  ..**     SELECT 
2a65e 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74  <column> FROM <t
2a65f 61 62 6c 65 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  able>..**..** If
2a660 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64 20   the prNotFound 
2a661 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20  parameter is 0, 
2a662 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20  then the b-tree 
2a663 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
2a664 69 74 65 72 61 74 65 0d 0a 2a 2a 20 74 68 72 6f  iterate..** thro
2a665 75 67 68 20 74 68 65 20 73 65 74 20 6d 65 6d 62  ugh the set memb
2a666 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 61 6e  ers, skipping an
2a667 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20 49 6e  y duplicates. In
2a668 20 74 68 69 73 20 63 61 73 65 20 61 6e 0d 0a 2a   this case an..*
2a669 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c  * epheremal tabl
2a66a 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 75  e must be used u
2a66b 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63 74  nless the select
2a66c 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67  ed <column> is g
2a66d 75 61 72 61 6e 74 65 65 64 0d 0a 2a 2a 20 74 6f  uaranteed..** to
2a66e 20 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74   be unique - eit
2a66f 68 65 72 20 62 65 63 61 75 73 65 20 69 74 20 69  her because it i
2a670 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
2a671 4d 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0d 0a  MARY KEY or it..
2a672 2a 2a 20 68 61 73 20 61 20 55 4e 49 51 55 45 20  ** has a UNIQUE 
2a673 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 55 4e  constraint or UN
2a674 49 51 55 45 20 69 6e 64 65 78 2e 0d 0a 2a 2a 0d  IQUE index...**.
2a675 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f 74  .** If the prNot
2a676 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20  Found parameter 
2a677 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74  is not 0, then t
2a678 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 62  he b-tree will b
2a679 65 20 75 73 65 64 20 0d 0a 2a 2a 20 66 6f 72 20  e used ..** for 
2a67a 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73  fast set members
2a67b 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68  hip tests. In th
2a67c 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 65 72  is case an epher
2a67d 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20  emal table must 
2a67e 0d 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e 6c  ..** be used unl
2a67f 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20  ess <column> is 
2a680 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
2a681 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64  RY KEY or an ind
2a682 65 78 20 63 61 6e 20 0d 0a 2a 2a 20 62 65 20 66  ex can ..** be f
2a683 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d  ound with <colum
2a684 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d  n> as its left-m
2a685 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0d 0a 2a 2a 0d  ost column...**.
2a686 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74  .** When the b-t
2a687 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65  ree is being use
2a688 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70  d for membership
2a689 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c   tests, the call
2a68a 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a  ing function..**
2a68b 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77   needs to know w
2a68c 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
2a68d 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
2a68e 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  ains an SQL NULL
2a68f 20 0d 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f   ..** value in o
2a690 72 64 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c  rder to correctl
2a691 79 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65  y evaluate expre
2a692 73 73 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49  ssions like "X I
2a693 4e 20 28 59 2c 20 5a 29 22 2e 0d 0a 2a 2a 20 49  N (Y, Z)"...** I
2a694 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 63  f there is any c
2a695 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 28  hance that the (
2a696 2e 2e 2e 29 20 6d 69 67 68 74 20 63 6f 6e 74 61  ...) might conta
2a697 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  in a NULL value 
2a698 61 74 0d 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c 20  at..** runtime, 
2a699 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72 20  then a register 
2a69a 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
2a69b 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75   the register nu
2a69c 6d 62 65 72 20 77 72 69 74 74 65 6e 0d 0a 2a 2a  mber written..**
2a69d 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e   to *prNotFound.
2a69e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
2a69f 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
2a6a0 28 2e 2e 2e 29 20 63 6f 6e 74 61 69 6e 73 20 61  (...) contains a
2a6a1 0d 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65 2c  ..** NULL value,
2a6a2 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e   then *prNotFoun
2a6a3 64 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e  d is left unchan
2a6a4 67 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ged...**..** If 
2a6a5 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c  a register is al
2a6a6 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 74 73 20  located and its 
2a6a7 6c 6f 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 20  location stored 
2a6a8 69 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c 20  in *prNotFound, 
2a6a9 74 68 65 6e 0d 0a 2a 2a 20 69 74 73 20 69 6e 69  then..** its ini
2a6aa 74 69 61 6c 20 76 61 6c 75 65 20 69 73 20 4e 55  tial value is NU
2a6ab 4c 4c 2e 20 20 49 66 20 74 68 65 20 28 2e 2e 2e  LL.  If the (...
2a6ac 29 20 64 6f 65 73 20 6e 6f 74 20 72 65 6d 61 69  ) does not remai
2a6ad 6e 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a 2a 20 66  n constant..** f
2a6ae 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
2a6af 6f 66 20 74 68 65 20 71 75 65 72 79 20 28 69 2e  of the query (i.
2a6b0 65 2e 20 74 68 65 20 53 45 4c 45 43 54 20 77 69  e. the SELECT wi
2a6b1 74 68 69 6e 20 74 68 65 20 28 2e 2e 2e 29 0d 0a  thin the (...)..
2a6b2 2a 2a 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74  ** is a correlat
2a6b3 65 64 20 73 75 62 71 75 65 72 79 29 20 74 68 65  ed subquery) the
2a6b4 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
2a6b5 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67  he allocated reg
2a6b6 69 73 74 65 72 20 69 73 0d 0a 2a 2a 20 72 65 73  ister is..** res
2a6b7 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20  et to NULL each 
2a6b8 74 69 6d 65 20 74 68 65 20 73 75 62 71 75 65 72  time the subquer
2a6b9 79 20 69 73 20 72 65 72 75 6e 2e 20 54 68 69 73  y is rerun. This
2a6ba 20 61 6c 6c 6f 77 73 20 74 68 65 0d 0a 2a 2a 20   allows the..** 
2a6bb 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64  caller to use vd
2a6bc 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c 65  be code equivale
2a6bd 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  nt to the follow
2a6be 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 69  ing:..**..**   i
2a6bf 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c  f( register==NUL
2a6c0 4c 20 29 7b 0d 0a 2a 2a 20 20 20 20 20 68 61 73  L ){..**     has
2a6c1 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66  _null = <test if
2a6c2 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
2a6c3 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0d 0a  contains null>..
2a6c4 2a 2a 20 20 20 20 20 72 65 67 69 73 74 65 72 20  **     register 
2a6c5 3d 20 31 0d 0a 2a 2a 20 20 20 7d 0d 0a 2a 2a 0d  = 1..**   }..**.
2a6c6 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
2a6c7 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68  avoid running th
2a6c8 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20  e <test if data 
2a6c9 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2a6ca 6e 73 20 6e 75 6c 6c 3e 0d 0a 2a 2a 20 74 65 73  ns null>..** tes
2a6cb 74 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61  t more often tha
2a6cc 6e 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0d  n is necessary..
2a6cd 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2a6ce 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2a6cf 59 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  Y..SQLITE_PRIVAT
2a6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
2a6d1 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a  dInIndex(Parse *
2a6d2 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 58  pParse, Expr *pX
2a6d3 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 6e  , int *prNotFoun
2a6d4 64 29 7b 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70  d){..  Select *p
2a6d5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a6d6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a6d7 53 45 4c 45 43 54 20 74 6f 20 74 68 65 20 72 69  SELECT to the ri
2a6d8 67 68 74 20 6f 66 20 49 4e 20 6f 70 65 72 61 74  ght of IN operat
2a6d9 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 54 79  or */..  int eTy
2a6da 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  pe = 0;         
2a6db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a6dc 2a 20 54 79 70 65 20 6f 66 20 52 48 53 20 74 61  * Type of RHS ta
2a6dd 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 5f 2a 20  ble. IN_INDEX_* 
2a6de 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 61 62 20 3d  */..  int iTab =
2a6df 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2a6e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2a6e1 75 72 73 6f 72 20 6f 66 20 74 68 65 20 52 48 53  ursor of the RHS
2a6e2 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74   table */..  int
2a6e3 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20   mustBeUnique = 
2a6e4 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 3b  (prNotFound==0);
2a6e5 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52 48     /* True if RH
2a6e6 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  S must be unique
2a6e7 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d   */..  Vdbe *v =
2a6e8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2a6e9 70 50 61 72 73 65 29 3b 20 20 20 20 20 2f 2a 20  pParse);     /* 
2a6ea 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  Virtual machine 
2a6eb 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0d 0a  being coded */..
2a6ec 0d 0a 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e  ..  assert( pX->
2a6ed 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0d 0a 0d 0a  op==TK_IN );....
2a6ee 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
2a6ef 65 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67  e if an existing
2a6f0 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
2a6f1 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0d 0a  can be used to..
2a6f2 20 20 2a 2a 20 73 61 74 69 73 66 79 20 74 68 65    ** satisfy the
2a6f3 20 71 75 65 72 79 2e 20 20 54 68 69 73 20 69 73   query.  This is
2a6f4 20 70 72 65 66 65 72 61 62 6c 65 20 74 6f 20 67   preferable to g
2a6f5 65 6e 65 72 61 74 69 6e 67 20 61 20 6e 65 77 20  enerating a new 
2a6f6 0d 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72 61 6c  ..  ** ephemeral
2a6f7 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20   table...  */.. 
2a6f8 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f   p = (ExprHasPro
2a6f9 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73  perty(pX, EP_xIs
2a6fa 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e  Select) ? pX->x.
2a6fb 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0d 0a 20  pSelect : 0);.. 
2a6fc 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 72   if( ALWAYS(pPar
2a6fd 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20  se->nErr==0) && 
2a6fe 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e  isCandidateForIn
2a6ff 4f 70 74 28 70 29 20 29 7b 0d 0a 20 20 20 20 73  Opt(p) ){..    s
2a700 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2a701 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
2a702 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2a703 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
2a704 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
2a705 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a706 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2a707 61 62 6c 65 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f  able <table>. */
2a708 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  ..    Expr *pExp
2a709 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
2a70a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a70b 45 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75  Expression <colu
2a70c 6d 6e 3e 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  mn> */..    int 
2a70d 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
2a70e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a70f 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
2a710 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a  olumn <column> *
2a711 2f 0d 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20  /..    int iDb; 
2a712 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a714 20 44 61 74 61 62 61 73 65 20 69 64 78 20 66 6f   Database idx fo
2a715 72 20 70 54 61 62 20 2a 2f 0d 0a 0d 0a 20 20 20  r pTab */....   
2a716 20 61 73 73 65 72 74 28 20 70 20 29 3b 20 20 20   assert( p );   
2a717 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a718 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20       /* Because 
2a719 6f 66 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f  of isCandidateFo
2a71a 72 49 6e 4f 70 74 28 70 29 20 2a 2f 0d 0a 20 20  rInOpt(p) */..  
2a71b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
2a71c 69 73 74 21 3d 30 20 29 3b 20 20 20 20 20 20 20  ist!=0 );       
2a71d 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65        /* Because
2a71e 20 6f 66 20 69 73 43 61 6e 64 69 64 61 74 65 46   of isCandidateF
2a71f 6f 72 49 6e 4f 70 74 28 70 29 20 2a 2f 0d 0a 20  orInOpt(p) */.. 
2a720 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45     assert( p->pE
2a721 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2a722 21 3d 30 20 29 3b 20 2f 2a 20 42 65 63 61 75 73  !=0 ); /* Becaus
2a723 65 20 6f 66 20 69 73 43 61 6e 64 69 64 61 74 65  e of isCandidate
2a724 46 6f 72 49 6e 4f 70 74 28 70 29 20 2a 2f 0d 0a  ForInOpt(p) */..
2a725 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2a726 53 72 63 21 3d 30 20 29 3b 20 20 20 20 20 20 20  Src!=0 );       
2a727 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75          /* Becau
2a728 73 65 20 6f 66 20 69 73 43 61 6e 64 69 64 61 74  se of isCandidat
2a729 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 2a 2f 0d  eForInOpt(p) */.
2a72a 0a 20 20 20 20 70 54 61 62 20 3d 20 70 2d 3e 70  .    pTab = p->p
2a72b 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0d  Src->a[0].pTab;.
2a72c 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 2d 3e  .    pExpr = p->
2a72d 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2a72e 70 72 3b 0d 0a 20 20 20 20 69 43 6f 6c 20 3d 20  pr;..    iCol = 
2a72f 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d  pExpr->iColumn;.
2a730 0a 20 20 20 0d 0a 20 20 20 20 2f 2a 20 43 6f 64  .   ..    /* Cod
2a731 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f  e an OP_VerifyCo
2a732 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c  okie and OP_Tabl
2a733 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65  eLock for <table
2a734 3e 2e 20 2a 2f 0d 0a 20 20 20 20 69 44 62 20 3d  >. */..    iDb =
2a735 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2a736 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
2a737 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 20 20 73  pSchema);..    s
2a738 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
2a739 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
2a73a 44 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  Db);..    sqlite
2a73b 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
2a73c 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
2a73d 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
2a73e 6d 65 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54  me);....    /* T
2a73f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2a740 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d  only called from
2a741 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20   two places. In 
2a742 62 6f 74 68 20 63 61 73 65 73 20 74 68 65 20 76  both cases the v
2a743 64 62 65 0d 0a 20 20 20 20 2a 2a 20 68 61 73 20  dbe..    ** has 
2a744 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c  already been all
2a745 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 75 6d  ocated. So assum
2a746 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  e sqlite3GetVdbe
2a747 28 29 20 69 73 20 61 6c 77 61 79 73 0d 0a 20 20  () is always..  
2a748 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
2a749 68 65 72 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  here...    */.. 
2a74a 20 20 20 61 73 73 65 72 74 28 76 29 3b 0d 0a 20     assert(v);.. 
2a74b 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
2a74c 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 41 64 64  ..      int iAdd
2a74d 72 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 41 64 64  r;....      iAdd
2a74e 72 20 3d 20 73 71 6c 69 74 65 33 43 6f 64 65 4f  r = sqlite3CodeO
2a74f 6e 63 65 28 70 50 61 72 73 65 29 3b 0d 0a 0d 0a  nce(pParse);....
2a750 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
2a751 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
2a752 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  Tab, iDb, pTab, 
2a753 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0d 0a 20  OP_OpenRead);.. 
2a754 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f       eType = IN_
2a755 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0d 0a 0d 0a  INDEX_ROWID;....
2a756 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a757 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64  eJumpHere(v, iAd
2a758 64 72 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  dr);..    }else{
2a759 0d 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70  ..      Index *p
2a75a 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
2a75b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a75c 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
2a75d 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  e */....      /*
2a75e 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   The collation s
2a75f 65 71 75 65 6e 63 65 20 75 73 65 64 20 62 79 20  equence used by 
2a760 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
2a761 49 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74  If an index is t
2a762 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75  o..      ** be u
2a763 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  sed in place of 
2a764 61 20 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74  a temp-table, it
2a765 20 6d 75 73 74 20 62 65 20 6f 72 64 65 72 65 64   must be ordered
2a766 20 61 63 63 6f 72 64 69 6e 67 0d 0a 20 20 20 20   according..    
2a767 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c    ** to this col
2a768 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e  lation sequence.
2a769 20 20 2a 2f 0d 0a 20 20 20 20 20 20 43 6f 6c 6c    */..      Coll
2a76a 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 6c 69  Seq *pReq = sqli
2a76b 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
2a76c 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2a76d 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pX->pLeft, pExpr
2a76e 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43  );....      /* C
2a76f 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66  heck that the af
2a770 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c  finity that will
2a771 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66   be used to perf
2a772 6f 72 6d 20 74 68 65 20 0d 0a 20 20 20 20 20 20  orm the ..      
2a773 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
2a774 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2a775 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
2a776 20 63 6f 6c 75 6d 6e 2e 20 49 66 0d 0a 20 20 20   column. If..   
2a777 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c     ** it is not,
2a778 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
2a779 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69  ble to use any i
2a77a 6e 64 65 78 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  ndex...      */.
2a77b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 20  .      char aff 
2a77c 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  = comparisonAffi
2a77d 6e 69 74 79 28 70 58 29 3b 0d 0a 20 20 20 20 20  nity(pX);..     
2a77e 20 69 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b   int affinity_ok
2a77f 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = (pTab->aCol[i
2a780 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61  Col].affinity==a
2a781 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f  ff||aff==SQLITE_
2a782 41 46 46 5f 4e 4f 4e 45 29 3b 0d 0a 0d 0a 20 20  AFF_NONE);....  
2a783 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
2a784 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 20  b->pIndex; pIdx 
2a785 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 61  && eType==0 && a
2a786 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 78  ffinity_ok; pIdx
2a787 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a  =pIdx->pNext){..
2a788 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64          if( (pId
2a789 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  x->aiColumn[0]==
2a78a 69 43 6f 6c 29 0d 0a 20 20 20 20 20 20 20 20 20  iCol)..         
2a78b 26 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  && sqlite3FindCo
2a78c 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
2a78d 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b  ), pIdx->azColl[
2a78e 30 5d 2c 20 30 29 3d 3d 70 52 65 71 0d 0a 20 20  0], 0)==pReq..  
2a78f 20 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74         && (!must
2a790 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64  BeUnique || (pId
2a791 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26  x->nColumn==1 &&
2a792 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
2a793 4f 45 5f 4e 6f 6e 65 29 29 0d 0a 20 20 20 20 20  OE_None))..     
2a794 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20     ){..         
2a795 20 69 6e 74 20 69 41 64 64 72 3b 0d 0a 20 20 20   int iAddr;..   
2a796 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4b 65         char *pKe
2a797 79 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 20 20  y;..  ..        
2a798 20 20 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a    pKey = (char *
2a799 29 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79  )sqlite3IndexKey
2a79a 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
2a79b 78 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  x);..          i
2a79c 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 43 6f  Addr = sqlite3Co
2a79d 64 65 4f 6e 63 65 28 70 50 61 72 73 65 29 3b 0d  deOnce(pParse);.
2a79e 0a 20 20 0d 0a 20 20 20 20 20 20 20 20 20 20 73  .  ..          s
2a79f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2a7a0 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c  (v, OP_OpenRead,
2a7a1 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75   iTab, pIdx->tnu
2a7a2 6d 2c 20 69 44 62 2c 0d 0a 20 20 20 20 20 20 20  m, iDb,..       
2a7a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a7a4 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f          pKey,P4_
2a7a5 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2a7a6 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 56 64 62  ;..          Vdb
2a7a7 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
2a7a8 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  ", pIdx->zName))
2a7a9 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 65 54 79  ;..          eTy
2a7aa 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  pe = IN_INDEX_IN
2a7ab 44 45 58 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20  DEX;....        
2a7ac 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2a7ad 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b  pHere(v, iAddr);
2a7ae 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
2a7af 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 70  prNotFound && !p
2a7b0 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2a7b1 6e 6f 74 4e 75 6c 6c 20 29 7b 0d 0a 20 20 20 20  notNull ){..    
2a7b2 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f          *prNotFo
2a7b3 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  und = ++pParse->
2a7b4 6e 4d 65 6d 3b 0d 0a 20 20 20 20 20 20 20 20 20  nMem;..         
2a7b5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a7b6 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2a7b7 20 30 2c 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29   0, *prNotFound)
2a7b8 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
2a7b9 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2a7ba 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
2a7bb 0d 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30  ..  if( eType==0
2a7bc 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 43 6f 75 6c   ){..    /* Coul
2a7bd 64 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 20 65  d not found an e
2a7be 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72  xisting table or
2a7bf 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 61 73   index to use as
2a7c0 20 74 68 65 20 52 48 53 20 62 2d 74 72 65 65 2e   the RHS b-tree.
2a7c1 0d 0a 20 20 20 20 2a 2a 20 57 65 20 77 69 6c 6c  ..    ** We will
2a7c2 20 68 61 76 65 20 74 6f 20 67 65 6e 65 72 61 74   have to generat
2a7c3 65 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74  e an ephemeral t
2a7c4 61 62 6c 65 20 74 6f 20 64 6f 20 74 68 65 20 6a  able to do the j
2a7c5 6f 62 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ob...    */..   
2a7c6 20 64 6f 75 62 6c 65 20 73 61 76 65 64 4e 51 75   double savedNQu
2a7c7 65 72 79 4c 6f 6f 70 20 3d 20 70 50 61 72 73 65  eryLoop = pParse
2a7c8 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 3b 0d 0a 20  ->nQueryLoop;.. 
2a7c9 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e     int rMayHaveN
2a7ca 75 6c 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 65 54  ull = 0;..    eT
2a7cb 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 45  ype = IN_INDEX_E
2a7cc 50 48 3b 0d 0a 20 20 20 20 69 66 28 20 70 72 4e  PH;..    if( prN
2a7cd 6f 74 46 6f 75 6e 64 20 29 7b 0d 0a 20 20 20 20  otFound ){..    
2a7ce 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20    *prNotFound = 
2a7cf 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b  rMayHaveNull = +
2a7d0 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
2a7d1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a7d2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2a7d3 6c 6c 2c 20 30 2c 20 2a 70 72 4e 6f 74 46 6f 75  ll, 0, *prNotFou
2a7d4 6e 64 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  nd);..    }else{
2a7d5 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2a7d6 28 20 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79  ( pParse->nQuery
2a7d7 4c 6f 6f 70 3e 28 64 6f 75 62 6c 65 29 31 20 29  Loop>(double)1 )
2a7d8 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ;..      pParse-
2a7d9 3e 6e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 28 64  >nQueryLoop = (d
2a7da 6f 75 62 6c 65 29 31 3b 0d 0a 20 20 20 20 20 20  ouble)1;..      
2a7db 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69  if( pX->pLeft->i
2a7dc 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 78 70  Column<0 && !Exp
2a7dd 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
2a7de 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pX, EP_xIsSelect
2a7df 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 65 54  ) ){..        eT
2a7e0 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52  ype = IN_INDEX_R
2a7e1 4f 57 49 44 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  OWID;..      }..
2a7e2 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
2a7e3 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
2a7e4 70 50 61 72 73 65 2c 20 70 58 2c 20 72 4d 61 79  pParse, pX, rMay
2a7e5 48 61 76 65 4e 75 6c 6c 2c 20 65 54 79 70 65 3d  HaveNull, eType=
2a7e6 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 29  =IN_INDEX_ROWID)
2a7e7 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ;..    pParse->n
2a7e8 51 75 65 72 79 4c 6f 6f 70 20 3d 20 73 61 76 65  QueryLoop = save
2a7e9 64 4e 51 75 65 72 79 4c 6f 6f 70 3b 0d 0a 20 20  dNQueryLoop;..  
2a7ea 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 58 2d 3e  }else{..    pX->
2a7eb 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0d 0a  iTable = iTab;..
2a7ec 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 65 54    }..  return eT
2a7ed 79 70 65 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  ype;..}..#endif.
2a7ee 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
2a7ef 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c  te code for scal
2a7f0 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73  ar subqueries us
2a7f1 65 64 20 61 73 20 61 20 73 75 62 71 75 65 72 79  ed as a subquery
2a7f2 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 45 58 49   expression, EXI
2a7f3 53 54 53 2c 0d 0a 2a 2a 20 6f 72 20 49 4e 20 6f  STS,..** or IN o
2a7f4 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70  perators.  Examp
2a7f5 6c 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  les:..**..**    
2a7f6 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20   (SELECT a FROM 
2a7f7 62 29 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73  b)          -- s
2a7f8 75 62 71 75 65 72 79 0d 0a 2a 2a 20 20 20 20 20  ubquery..**     
2a7f9 45 58 49 53 54 53 20 28 53 45 4c 45 43 54 20 61  EXISTS (SELECT a
2a7fa 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58   FROM b)   -- EX
2a7fb 49 53 54 53 20 73 75 62 71 75 65 72 79 0d 0a 2a  ISTS subquery..*
2a7fc 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35 2c  *     x IN (4,5,
2a7fd 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20 20  11)             
2a7fe 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   -- IN operator 
2a7ff 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 67  with list on rig
2a800 68 74 2d 68 61 6e 64 20 73 69 64 65 0d 0a 2a 2a  ht-hand side..**
2a801 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43       x IN (SELEC
2a802 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20  T a FROM b)     
2a803 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77  -- IN operator w
2a804 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20  ith subquery on 
2a805 74 68 65 20 72 69 67 68 74 0d 0a 2a 2a 0d 0a 2a  the right..**..*
2a806 2a 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61  * The pExpr para
2a807 6d 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20  meter describes 
2a808 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
2a809 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
2a80a 20 49 4e 0d 0a 2a 2a 20 6f 70 65 72 61 74 6f 72   IN..** operator
2a80b 20 6f 72 20 73 75 62 71 75 65 72 79 2e 0d 0a 2a   or subquery...*
2a80c 2a 0d 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74  *..** If paramet
2a80d 65 72 20 69 73 52 6f 77 69 64 20 69 73 20 6e 6f  er isRowid is no
2a80e 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 65 78 70  n-zero, then exp
2a80f 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 73  ression pExpr is
2a810 20 67 75 61 72 61 6e 74 65 65 64 0d 0a 2a 2a 20   guaranteed..** 
2a811 74 6f 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72  to be of the for
2a812 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20 28 3f  m "<rowid> IN (?
2a813 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72 65 20  , ?, ?)", where 
2a814 3c 72 6f 77 69 64 3e 20 69 73 20 61 20 72 65 66  <rowid> is a ref
2a815 65 72 65 6e 63 65 0d 0a 2a 2a 20 74 6f 20 73 6f  erence..** to so
2a816 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63  me integer key c
2a817 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65  olumn of a table
2a818 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73   B-Tree. In this
2a819 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0d 0a 2a   case, use an..*
2a81a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 20  * intkey B-Tree 
2a81b 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74  to store the set
2a81c 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75   of IN(...) valu
2a81d 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  es instead of th
2a81e 65 20 75 73 75 61 6c 0d 0a 2a 2a 20 28 73 6c 6f  e usual..** (slo
2a81f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65  wer) variable le
2a820 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65  ngth keys B-Tree
2a821 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 72 4d 61  ...**..** If rMa
2a822 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e  yHaveNull is non
2a823 2d 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e  -zero, that mean
2a824 73 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 61  s that the opera
2a825 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 0d 0a 2a  tion is an IN..*
2a826 2a 20 28 6e 6f 74 20 61 20 53 45 4c 45 43 54 20  * (not a SELECT 
2a827 6f 72 20 45 58 49 53 54 53 29 20 61 6e 64 20 74  or EXISTS) and t
2a828 68 61 74 20 74 68 65 20 52 48 53 20 6d 69 67 68  hat the RHS migh
2a829 74 20 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 73  t contains NULLs
2a82a 2e 0d 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72  ...** Furthermor
2a82b 65 2c 20 74 68 65 20 49 4e 20 69 73 20 69 6e 20  e, the IN is in 
2a82c 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61  a WHERE clause a
2a82d 6e 64 20 74 68 61 74 20 77 65 20 72 65 61 6c 6c  nd that we reall
2a82e 79 20 77 61 6e 74 0d 0a 2a 2a 20 74 6f 20 69 74  y want..** to it
2a82f 65 72 61 74 65 20 6f 76 65 72 20 74 68 65 20 52  erate over the R
2a830 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65  HS of the IN ope
2a831 72 61 74 6f 72 20 69 6e 20 6f 72 64 65 72 20 74  rator in order t
2a832 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63 61 74 65  o quickly locate
2a833 0d 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 73 70  ..** all corresp
2a834 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65 6d 65  onding LHS eleme
2a835 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73 20 72  nts.  All this r
2a836 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73 20 69  outine does is i
2a837 6e 69 74 69 61 6c 69 7a 65 0d 0a 2a 2a 20 74 68  nitialize..** th
2a838 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65 6e  e register given
2a839 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   by rMayHaveNull
2a83a 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c 69   to NULL.  Calli
2a83b 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c  ng routines will
2a83c 20 74 61 6b 65 0d 0a 2a 2a 20 63 61 72 65 20 6f   take..** care o
2a83d 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20  f changing this 
2a83e 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74  register value t
2a83f 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68  o non-NULL if th
2a840 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72  e RHS is NULL-fr
2a841 65 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 72  ee...**..** If r
2a842 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a  MayHaveNull is z
2a843 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ero, that means 
2a844 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72  that the subquer
2a845 79 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 0d  y is being used.
2a846 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62 65 72 73 68  .** for membersh
2a847 69 70 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e  ip testing only.
2a848 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65    There is no ne
2a849 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ed to initialize
2a84a 20 61 6e 79 0d 0a 2a 2a 20 72 65 67 69 73 74 65   any..** registe
2a84b 72 73 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  rs to indicate t
2a84c 68 65 20 70 72 65 73 65 6e 73 65 20 6f 72 20 61  he presense or a
2a84d 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20  bsence of NULLs 
2a84e 6f 6e 20 74 68 65 20 52 48 53 2e 0d 0a 2a 2a 0d  on the RHS...**.
2a84f 0a 2a 2a 20 46 6f 72 20 61 20 53 45 4c 45 43 54  .** For a SELECT
2a850 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72 61   or EXISTS opera
2a851 74 6f 72 2c 20 72 65 74 75 72 6e 20 74 68 65 20  tor, return the 
2a852 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68 6f  register that ho
2a853 6c 64 73 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75  lds the..** resu
2a854 6c 74 2e 20 20 46 6f 72 20 49 4e 20 6f 70 65 72  lt.  For IN oper
2a855 61 74 6f 72 73 20 6f 72 20 69 66 20 61 6e 20 65  ators or if an e
2a856 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
2a857 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
2a858 20 30 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66   0...*/..#ifndef
2a859 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2a85a 51 55 45 52 59 0d 0a 53 51 4c 49 54 45 5f 50 52  QUERY..SQLITE_PR
2a85b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2a85c 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0d  3CodeSubselect(.
2a85d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2a85e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
2a85f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2a860 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  ..  Expr *pExpr,
2a861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2a862 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f  he IN, SELECT, o
2a863 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f  r EXISTS operato
2a864 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 4d 61 79  r */..  int rMay
2a865 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20  HaveNull,       
2a866 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74  /* Register that
2a867 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72   records whether
2a868 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20   NULLs exist in 
2a869 52 48 53 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73  RHS */..  int is
2a86a 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20  Rowid           
2a86b 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48    /* If true, LH
2a86c 53 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72  S of IN operator
2a86d 20 69 73 20 61 20 72 6f 77 69 64 20 2a 2f 0d 0a   is a rowid */..
2a86e 29 7b 0d 0a 20 20 69 6e 74 20 74 65 73 74 41 64  ){..  int testAd
2a86f 64 72 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  dr = -1;        
2a870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a871 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61   One-time test a
2a872 64 64 72 65 73 73 20 2a 2f 0d 0a 20 20 69 6e 74  ddress */..  int
2a873 20 72 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20   rReg = 0;      
2a874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a875 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2a876 20 73 74 6f 72 69 6e 67 20 72 65 73 75 6c 74 69   storing resulti
2a877 6e 67 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76  ng */..  Vdbe *v
2a878 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2a879 65 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 69 66  e(pParse);..  if
2a87a 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
2a87b 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 73 71 6c  return 0;..  sql
2a87c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73  ite3ExprCachePus
2a87d 68 28 70 50 61 72 73 65 29 3b 0d 0a 0d 0a 20 20  h(pParse);....  
2a87e 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73  /* This code mus
2a87f 74 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20  t be run in its 
2a880 65 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74  entirety every t
2a881 69 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e  ime it is encoun
2a882 74 65 72 65 64 0d 0a 20 20 2a 2a 20 69 66 20 61  tered..  ** if a
2a883 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ny of the follow
2a884 69 6e 67 20 69 73 20 74 72 75 65 3a 0d 0a 20 20  ing is true:..  
2a885 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54  **..  **    *  T
2a886 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
2a887 64 65 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74  de is a correlat
2a888 65 64 20 73 75 62 71 75 65 72 79 0d 0a 20 20 2a  ed subquery..  *
2a889 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68  *    *  The righ
2a88a 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61  t-hand side is a
2a88b 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
2a88c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 72  t containing var
2a88d 69 61 62 6c 65 73 0d 0a 20 20 2a 2a 20 20 20 20  iables..  **    
2a88e 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64 65  *  We are inside
2a88f 20 61 20 74 72 69 67 67 65 72 0d 0a 20 20 2a 2a   a trigger..  **
2a890 0d 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66  ..  ** If all of
2a891 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66   the above are f
2a892 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61  alse, then we ca
2a893 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  n run this code 
2a894 6a 75 73 74 20 6f 6e 63 65 0d 0a 20 20 2a 2a 20  just once..  ** 
2a895 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73  save the results
2a896 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20  , and reuse the 
2a897 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73  same result on s
2a898 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61  ubsequent invoca
2a899 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  tions...  */..  
2a89a 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  if( !ExprHasAnyP
2a89b 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2a89c 50 5f 56 61 72 53 65 6c 65 63 74 29 20 29 7b 0d  P_VarSelect) ){.
2a89d 0a 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20  .    testAddr = 
2a89e 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63 65 28  sqlite3CodeOnce(
2a89f 70 50 61 72 73 65 29 3b 0d 0a 20 20 7d 0d 0a 0d  pParse);..  }...
2a8a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a8a1 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0d 0a 20 20  OMIT_EXPLAIN..  
2a8a2 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  if( pParse->expl
2a8a3 61 69 6e 3d 3d 32 20 29 7b 0d 0a 20 20 20 20 63  ain==2 ){..    c
2a8a4 68 61 72 20 2a 7a 4d 73 67 20 3d 20 73 71 6c 69  har *zMsg = sqli
2a8a5 74 65 33 4d 50 72 69 6e 74 66 28 0d 0a 20 20 20  te3MPrintf(..   
2a8a6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2c       pParse->db,
2a8a7 20 22 45 58 45 43 55 54 45 20 25 73 25 73 20 53   "EXECUTE %s%s S
2a8a8 55 42 51 55 45 52 59 20 25 64 22 2c 20 74 65 73  UBQUERY %d", tes
2a8a9 74 41 64 64 72 3e 3d 30 3f 22 22 3a 22 43 4f 52  tAddr>=0?"":"COR
2a8aa 52 45 4c 41 54 45 44 20 22 2c 0d 0a 20 20 20 20  RELATED ",..    
2a8ab 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54      pExpr->op==T
2a8ac 4b 5f 49 4e 3f 22 4c 49 53 54 22 3a 22 53 43 41  K_IN?"LIST":"SCA
2a8ad 4c 41 52 22 2c 20 70 50 61 72 73 65 2d 3e 69 4e  LAR", pParse->iN
2a8ae 65 78 74 53 65 6c 65 63 74 49 64 0d 0a 20 20 20  extSelectId..   
2a8af 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   );..    sqlite3
2a8b0 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2a8b1 5f 45 78 70 6c 61 69 6e 2c 20 70 50 61 72 73 65  _Explain, pParse
2a8b2 2d 3e 69 53 65 6c 65 63 74 49 64 2c 20 30 2c 20  ->iSelectId, 0, 
2a8b3 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41  0, zMsg, P4_DYNA
2a8b4 4d 49 43 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  MIC);..  }..#end
2a8b5 69 66 0d 0a 0d 0a 20 20 73 77 69 74 63 68 28 20  if....  switch( 
2a8b6 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0d 0a 20 20  pExpr->op ){..  
2a8b7 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0d    case TK_IN: {.
2a8b8 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69  .      char affi
2a8b9 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 20 20  nity;           
2a8ba 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 6f     /* Affinity o
2a8bb 66 20 74 68 65 20 4c 48 53 20 6f 66 20 74 68 65  f the LHS of the
2a8bc 20 49 4e 20 2a 2f 0d 0a 20 20 20 20 20 20 4b 65   IN */..      Ke
2a8bd 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 20 20  yInfo keyInfo;  
2a8be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
2a8bf 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 67 65 6e  info for the gen
2a8c0 65 72 61 74 65 64 20 74 61 62 6c 65 20 2a 2f 0d  erated table */.
2a8c1 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
2a8c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8c3 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2a8c4 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2a8c5 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  l instruction */
2a8c6 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c  ..      Expr *pL
2a8c7 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
2a8c8 66 74 3b 20 2f 2a 20 74 68 65 20 4c 48 53 20 6f  ft; /* the LHS o
2a8c9 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  f the IN operato
2a8ca 72 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 69 66  r */....      if
2a8cb 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29  ( rMayHaveNull )
2a8cc 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
2a8cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a8ce 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79  OP_Null, 0, rMay
2a8cf 48 61 76 65 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20  HaveNull);..    
2a8d0 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 61 66 66    }....      aff
2a8d1 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
2a8d2 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66  xprAffinity(pLef
2a8d3 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20  t);....      /* 
2a8d4 57 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  Whether this is 
2a8d5 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e  an 'x IN(SELECT.
2a8d6 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e  ..)' or an 'x IN
2a8d7 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 0d 0a 20  (<exprlist>)'.. 
2a8d8 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69       ** expressi
2a8d9 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64  on it is handled
2a8da 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e 20 20   the same way.  
2a8db 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  An ephemeral tab
2a8dc 6c 65 20 69 73 20 0d 0a 20 20 20 20 20 20 2a 2a  le is ..      **
2a8dd 20 66 69 6c 6c 65 64 20 77 69 74 68 20 73 69 6e   filled with sin
2a8de 67 6c 65 2d 66 69 65 6c 64 20 69 6e 64 65 78 20  gle-field index 
2a8df 6b 65 79 73 20 72 65 70 72 65 73 65 6e 74 69 6e  keys representin
2a8e0 67 20 74 68 65 20 72 65 73 75 6c 74 73 0d 0a 20  g the results.. 
2a8e1 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65       ** from the
2a8e2 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65 20 3c   SELECT or the <
2a8e3 65 78 70 72 6c 69 73 74 3e 2e 0d 0a 20 20 20 20  exprlist>...    
2a8e4 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 49    **..      ** I
2a8e5 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65 73  f the 'x' expres
2a8e6 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sion is a column
2a8e7 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 53   value, or the S
2a8e8 45 4c 45 43 54 2e 2e 2e 0d 0a 20 20 20 20 20 20  ELECT.....      
2a8e9 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  ** statement ret
2a8ea 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61  urns a column va
2a8eb 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66  lue, then the af
2a8ec 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0d 0a  finity of that..
2a8ed 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20        ** column 
2a8ee 69 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  is used to build
2a8ef 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e   the index keys.
2a8f0 20 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64   If both 'x' and
2a8f1 20 74 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20 53   the..      ** S
2a8f2 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65  ELECT... stateme
2a8f3 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20  nt are columns, 
2a8f4 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66  then numeric aff
2a8f5 69 6e 69 74 79 20 69 73 20 75 73 65 64 0d 0a 20  inity is used.. 
2a8f6 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 68 65       ** if eithe
2a8f7 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d  r column has NUM
2a8f8 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 52 20  ERIC or INTEGER 
2a8f9 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e 65 69  affinity. If nei
2a8fa 74 68 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20 27  ther..      ** '
2a8fb 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 43  x' nor the SELEC
2a8fc 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61  T... statement a
2a8fd 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e  re columns, then
2a8fe 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74   numeric affinit
2a8ff 79 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75  y..      ** is u
2a900 73 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  sed...      */..
2a901 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
2a902 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ble = pParse->nT
2a903 61 62 2b 2b 3b 0d 0a 20 20 20 20 20 20 61 64 64  ab++;..      add
2a904 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2a905 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
2a906 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72  Ephemeral, pExpr
2a907 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f 77  ->iTable, !isRow
2a908 69 64 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  id);..      if( 
2a909 72 4d 61 79 48 61 76 65 4e 75 6c 6c 3d 3d 30 20  rMayHaveNull==0 
2a90a 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  ) sqlite3VdbeCha
2a90b 6e 67 65 50 35 28 76 2c 20 42 54 52 45 45 5f 55  ngeP5(v, BTREE_U
2a90c 4e 4f 52 44 45 52 45 44 29 3b 0d 0a 20 20 20 20  NORDERED);..    
2a90d 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66    memset(&keyInf
2a90e 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79  o, 0, sizeof(key
2a90f 49 6e 66 6f 29 29 3b 0d 0a 20 20 20 20 20 20 6b  Info));..      k
2a910 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20  eyInfo.nField = 
2a911 31 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20  1;....      if( 
2a912 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2a913 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2a914 65 63 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  ect) ){..       
2a915 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 20 20   /* Case 1:     
2a916 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20  expr IN (SELECT 
2a917 2e 2e 2e 29 0d 0a 20 20 20 20 20 20 20 20 2a 2a  ...)..        **
2a918 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e  ..        ** Gen
2a919 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72  erate code to wr
2a91a 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ite the results 
2a91b 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e  of the select in
2a91c 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  to the temporary
2a91d 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62  ..        ** tab
2a91e 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  le allocated and
2a91f 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0d 0a   opened above...
2a920 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20          */..    
2a921 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64      SelectDest d
2a922 65 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 45 78  est;..        Ex
2a923 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0d  prList *pEList;.
2a924 0a 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ...        asser
2a925 74 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0d 0a  t( !isRowid );..
2a926 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2a927 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
2a928 65 73 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45  est, SRT_Set, pE
2a929 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0d 0a 20  xpr->iTable);.. 
2a92a 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 66 69         dest.affi
2a92b 6e 69 74 79 20 3d 20 28 75 38 29 61 66 66 69 6e  nity = (u8)affin
2a92c 69 74 79 3b 0d 0a 20 20 20 20 20 20 20 20 61 73  ity;..        as
2a92d 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69 54  sert( (pExpr->iT
2a92e 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46 29  able&0x0000FFFF)
2a92f 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  ==pExpr->iTable 
2a930 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 45 78 70  );..        pExp
2a931 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 69 4c  r->x.pSelect->iL
2a932 69 6d 69 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20  imit = 0;..     
2a933 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65     if( sqlite3Se
2a934 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78  lect(pParse, pEx
2a935 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26  pr->x.pSelect, &
2a936 64 65 73 74 29 20 29 7b 0d 0a 20 20 20 20 20 20  dest) ){..      
2a937 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
2a938 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
2a939 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
2a93a 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  ->x.pSelect->pEL
2a93b 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ist;..        if
2a93c 28 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21  ( ALWAYS(pEList!
2a93d 3d 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45  =0 && pEList->nE
2a93e 78 70 72 3e 30 29 20 29 7b 20 0d 0a 20 20 20 20  xpr>0) ){ ..    
2a93f 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43        keyInfo.aC
2a940 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33  oll[0] = sqlite3
2a941 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
2a942 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
2a943 70 72 2d 3e 70 4c 65 66 74 2c 0d 0a 20 20 20 20  pr->pLeft,..    
2a944 20 20 20 20 20 20 20 20 20 20 70 45 4c 69 73 74            pEList
2a945 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0d 0a  ->a[0].pExpr);..
2a946 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2a947 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
2a948 53 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  S(pExpr->x.pList
2a949 21 3d 30 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  !=0) ){..       
2a94a 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20   /* Case 2:     
2a94b 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73  expr IN (exprlis
2a94c 74 29 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  t)..        **..
2a94d 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65          ** For e
2a94e 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  ach expression, 
2a94f 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b  build an index k
2a950 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c  ey from the eval
2a951 75 61 74 69 6f 6e 20 61 6e 64 0d 0a 20 20 20 20  uation and..    
2a952 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20      ** store it 
2a953 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
2a954 20 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72   table. If <expr
2a955 3e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74  > is a column, t
2a956 68 65 6e 20 75 73 65 0d 0a 20 20 20 20 20 20 20  hen use..       
2a957 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73   ** that columns
2a958 20 61 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62   affinity when b
2a959 75 69 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65  uilding index ke
2a95a 79 73 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73  ys. If <expr> is
2a95b 20 6e 6f 74 0d 0a 20 20 20 20 20 20 20 20 2a 2a   not..        **
2a95c 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e   a column, use n
2a95d 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 2e  umeric affinity.
2a95e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20  ..        */..  
2a95f 20 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20        int i;..  
2a960 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
2a961 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  pList = pExpr->x
2a962 2e 70 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20  .pList;..       
2a963 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2a964 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 20  _item *pItem;.. 
2a965 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72         int r1, r
2a966 32 2c 20 72 33 3b 0d 0a 0d 0a 20 20 20 20 20 20  2, r3;....      
2a967 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20    if( !affinity 
2a968 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 66  ){..          af
2a969 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
2a96a 41 46 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 20 20 20  AFF_NONE;..     
2a96b 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 6b 65     }..        ke
2a96c 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d  yInfo.aColl[0] =
2a96d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2a96e 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
2a96f 72 2d 3e 70 4c 65 66 74 29 3b 0d 0a 0d 0a 20 20  r->pLeft);....  
2a970 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
2a971 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65  rough each expre
2a972 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69  ssion in <exprli
2a973 73 74 3e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20  st>. */..       
2a974 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2a975 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2a976 0d 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73  ..        r2 = s
2a977 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a978 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
2a979 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a97a 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2a97b 20 30 2c 20 72 32 29 3b 0d 0a 20 20 20 20 20 20   0, r2);..      
2a97c 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e    for(i=pList->n
2a97d 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73  Expr, pItem=pLis
2a97e 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20  t->a; i>0; i--, 
2a97f 70 49 74 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20  pItem++){..     
2a980 20 20 20 20 20 45 78 70 72 20 2a 70 45 32 20 3d       Expr *pE2 =
2a981 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0d 0a   pItem->pExpr;..
2a982 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 56            int iV
2a983 61 6c 54 6f 49 6e 73 3b 0d 0a 0d 0a 20 20 20 20  alToIns;....    
2a984 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
2a985 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f  expression is no
2a986 74 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20  t constant then 
2a987 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0d  we will need to.
2a988 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69  .          ** di
2a989 73 61 62 6c 65 20 74 68 65 20 74 65 73 74 20 74  sable the test t
2a98a 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
2a98b 64 20 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b  d above that mak
2a98c 65 73 20 73 75 72 65 0d 0a 20 20 20 20 20 20 20  es sure..       
2a98d 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20     ** this code 
2a98e 6f 6e 6c 79 20 65 78 65 63 75 74 65 73 20 6f 6e  only executes on
2a98f 63 65 2e 20 20 42 65 63 61 75 73 65 20 66 6f 72  ce.  Because for
2a990 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0d   a non-constant.
2a991 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78  .          ** ex
2a992 70 72 65 73 73 69 6f 6e 20 77 65 20 6e 65 65 64  pression we need
2a993 20 74 6f 20 72 65 72 75 6e 20 74 68 69 73 20 63   to rerun this c
2a994 6f 64 65 20 65 61 63 68 20 74 69 6d 65 2e 0d 0a  ode each time...
2a995 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20            */..  
2a996 20 20 20 20 20 20 20 20 69 66 28 20 74 65 73 74          if( test
2a997 41 64 64 72 3e 3d 30 20 26 26 20 21 73 71 6c 69  Addr>=0 && !sqli
2a998 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
2a999 74 28 70 45 32 29 20 29 7b 0d 0a 20 20 20 20 20  t(pE2) ){..     
2a99a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2a99b 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
2a99c 2c 20 74 65 73 74 41 64 64 72 29 3b 0d 0a 20 20  , testAddr);..  
2a99d 20 20 20 20 20 20 20 20 20 20 74 65 73 74 41 64            testAd
2a99e 64 72 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 20 20  dr = -1;..      
2a99f 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20      }....       
2a9a0 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74     /* Evaluate t
2a9a1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  he expression an
2a9a2 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f  d insert it into
2a9a3 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
2a9a4 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  */..          if
2a9a5 28 20 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c  ( isRowid && sql
2a9a6 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
2a9a7 72 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e  r(pE2, &iValToIn
2a9a8 73 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  s) ){..         
2a9a9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a9aa 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2a9ab 74 49 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61  tInt, pExpr->iTa
2a9ac 62 6c 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49  ble, r2, iValToI
2a9ad 6e 73 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ns);..          
2a9ae 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2a9af 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33      r3 = sqlite3
2a9b0 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2a9b1 50 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b  Parse, pE2, r1);
2a9b2 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ..            if
2a9b3 28 20 69 73 52 6f 77 69 64 20 29 7b 0d 0a 20 20  ( isRowid ){..  
2a9b4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2a9b5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a9b6 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
2a9b7 33 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  3,..            
2a9b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a9b9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2a9ba 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29  urrentAddr(v)+2)
2a9bb 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
2a9bc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a9bd 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2a9be 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
2a9bf 72 32 2c 20 72 33 29 3b 0d 0a 20 20 20 20 20 20  r2, r3);..      
2a9c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2a9c1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2a9c2 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2a9c3 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2a9c4 72 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69  r3, 1, r2, &affi
2a9c5 6e 69 74 79 2c 20 31 29 3b 0d 0a 20 20 20 20 20  nity, 1);..     
2a9c6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2a9c7 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2a9c8 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2a9c9 72 33 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 20  r3, 1);..       
2a9ca 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2a9cb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2a9cc 64 78 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d  dxInsert, pExpr-
2a9cd 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b 0d 0a 20  >iTable, r2);.. 
2a9ce 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20             }..  
2a9cf 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2a9d0 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73 71     }..        sq
2a9d1 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a9d2 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2a9d3 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2a9d4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2a9d5 70 50 61 72 73 65 2c 20 72 32 29 3b 0d 0a 20 20  pParse, r2);..  
2a9d6 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
2a9d7 20 21 69 73 52 6f 77 69 64 20 29 7b 0d 0a 20 20   !isRowid ){..  
2a9d8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a9d9 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
2a9da 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b 65 79 49  r, (void *)&keyI
2a9db 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29  nfo, P4_KEYINFO)
2a9dc 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2a9dd 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
2a9de 0a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  ...    case TK_E
2a9df 58 49 53 54 53 3a 0d 0a 20 20 20 20 63 61 73 65  XISTS:..    case
2a9e0 20 54 4b 5f 53 45 4c 45 43 54 3a 0d 0a 20 20 20   TK_SELECT:..   
2a9e1 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20   default: {..   
2a9e2 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 68 61     /* If this ha
2a9e3 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72  s to be a scalar
2a9e4 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61   SELECT.  Genera
2a9e5 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74  te code to put t
2a9e6 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c  he..      ** val
2a9e7 75 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63  ue of this selec
2a9e8 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65  t in a memory ce
2a9e9 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68  ll and record th
2a9ea 65 20 6e 75 6d 62 65 72 0d 0a 20 20 20 20 20 20  e number..      
2a9eb 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  ** of the memory
2a9ec 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e   cell in iColumn
2a9ed 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  .  If this is an
2a9ee 20 45 58 49 53 54 53 2c 20 77 72 69 74 65 0d 0a   EXISTS, write..
2a9ef 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65        ** an inte
2a9f0 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73 74  ger 0 (not exist
2a9f1 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73 29  s) or 1 (exists)
2a9f2 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20 63   into a memory c
2a9f3 65 6c 6c 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e  ell..      ** an
2a9f4 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65  d record that me
2a9f5 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f  mory cell in iCo
2a9f6 6c 75 6d 6e 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  lumn...      */.
2a9f7 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
2a9f8 53 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sel;            
2a9f9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a9fa 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2a9fb 20 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0d 0a 20   to encode */.. 
2a9fc 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
2a9fd 64 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  dest;           
2a9fe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f             /* Ho
2a9ff 77 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53  w to deal with S
2aa00 45 4c 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0d  ELECt result */.
2aa01 0a 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ...      testcas
2aa02 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
2aa03 5f 45 58 49 53 54 53 20 29 3b 0d 0a 20 20 20 20  _EXISTS );..    
2aa04 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
2aa05 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  r->op==TK_SELECT
2aa06 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
2aa07 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  t( pExpr->op==TK
2aa08 5f 45 58 49 53 54 53 20 7c 7c 20 70 45 78 70 72  _EXISTS || pExpr
2aa09 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20  ->op==TK_SELECT 
2aa0a 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 61 73 73 65  );....      asse
2aa0b 72 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  rt( ExprHasPrope
2aa0c 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2aa0d 73 53 65 6c 65 63 74 29 20 29 3b 0d 0a 20 20 20  sSelect) );..   
2aa0e 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d     pSel = pExpr-
2aa0f 3e 78 2e 70 53 65 6c 65 63 74 3b 0d 0a 20 20 20  >x.pSelect;..   
2aa10 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2aa11 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
2aa12 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65  0, ++pParse->nMe
2aa13 6d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  m);..      if( p
2aa14 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Expr->op==TK_SEL
2aa15 45 43 54 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ECT ){..        
2aa16 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54  dest.eDest = SRT
2aa17 5f 4d 65 6d 3b 0d 0a 20 20 20 20 20 20 20 20 73  _Mem;..        s
2aa18 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2aa19 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2aa1a 64 65 73 74 2e 69 50 61 72 6d 29 3b 0d 0a 20 20  dest.iParm);..  
2aa1b 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2aa1c 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71  t((v, "Init subq
2aa1d 75 65 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0d  uery result"));.
2aa1e 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
2aa1f 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73         dest.eDes
2aa20 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0d  t = SRT_Exists;.
2aa21 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2aa22 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2aa23 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 73  _Integer, 0, des
2aa24 74 2e 69 50 61 72 6d 29 3b 0d 0a 20 20 20 20 20  t.iParm);..     
2aa25 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2aa26 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20  v, "Init EXISTS 
2aa27 72 65 73 75 6c 74 22 29 29 3b 0d 0a 20 20 20 20  result"));..    
2aa28 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74    }..      sqlit
2aa29 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61  e3ExprDelete(pPa
2aa2a 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70  rse->db, pSel->p
2aa2b 4c 69 6d 69 74 29 3b 0d 0a 20 20 20 20 20 20 70  Limit);..      p
2aa2c 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71  Sel->pLimit = sq
2aa2d 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2aa2e 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  e, TK_INTEGER, 0
2aa2f 2c 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  , 0,..          
2aa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa31 20 20 20 20 20 20 20 20 26 73 71 6c 69 74 65 33          &sqlite3
2aa32 49 6e 74 54 6f 6b 65 6e 73 5b 31 5d 29 3b 0d 0a  IntTokens[1]);..
2aa33 20 20 20 20 20 20 70 53 65 6c 2d 3e 69 4c 69 6d        pSel->iLim
2aa34 69 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69  it = 0;..      i
2aa35 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  f( sqlite3Select
2aa36 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26  (pParse, pSel, &
2aa37 64 65 73 74 29 20 29 7b 0d 0a 20 20 20 20 20 20  dest) ){..      
2aa38 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20    return 0;..   
2aa39 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 52 65 67     }..      rReg
2aa3a 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b 0d 0a   = dest.iParm;..
2aa3b 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72 72        ExprSetIrr
2aa3c 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b  educible(pExpr);
2aa3d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2aa3e 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
2aa3f 69 66 28 20 74 65 73 74 41 64 64 72 3e 3d 30 20  if( testAddr>=0 
2aa40 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ){..    sqlite3V
2aa41 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74  dbeJumpHere(v, t
2aa42 65 73 74 41 64 64 72 29 3b 0d 0a 20 20 7d 0d 0a  estAddr);..  }..
2aa43 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2aa44 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
2aa45 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 52  ;....  return rR
2aa46 65 67 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f  eg;..}..#endif /
2aa47 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  * SQLITE_OMIT_SU
2aa48 42 51 55 45 52 59 20 2a 2f 0d 0a 0d 0a 23 69 66  BQUERY */....#if
2aa49 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2aa4a 5f 53 55 42 51 55 45 52 59 0d 0a 2f 2a 0d 0a 2a  _SUBQUERY../*..*
2aa4b 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2aa4c 66 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73  for an IN expres
2aa4d 73 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sion...**..**   
2aa4e 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20     x IN (SELECT 
2aa4f 2e 2e 2e 29 0d 0a 2a 2a 20 20 20 20 20 20 78 20  ...)..**      x 
2aa50 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c 75 65  IN (value, value
2aa51 2c 20 2e 2e 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54  , ...)..**..** T
2aa52 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  he left-hand sid
2aa53 65 20 28 4c 48 53 29 20 69 73 20 61 20 73 63 61  e (LHS) is a sca
2aa54 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  lar expression. 
2aa55 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   The right-hand 
2aa56 73 69 64 65 20 28 52 48 53 29 0d 0a 2a 2a 20 69  side (RHS)..** i
2aa57 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 7a 65  s an array of ze
2aa58 72 6f 20 6f 72 20 6d 6f 72 65 20 76 61 6c 75 65  ro or more value
2aa59 73 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69  s.  The expressi
2aa5a 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 68  on is true if th
2aa5b 65 20 4c 48 53 20 69 73 0d 0a 2a 2a 20 63 6f 6e  e LHS is..** con
2aa5c 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68  tained within th
2aa5d 65 20 52 48 53 2e 20 20 54 68 65 20 76 61 6c 75  e RHS.  The valu
2aa5e 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  e of the express
2aa5f 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e 20 28  ion is unknown (
2aa60 4e 55 4c 4c 29 0d 0a 2a 2a 20 69 66 20 74 68 65  NULL)..** if the
2aa61 20 4c 48 53 20 69 73 20 4e 55 4c 4c 20 6f 72 20   LHS is NULL or 
2aa62 69 66 20 74 68 65 20 4c 48 53 20 69 73 20 6e 6f  if the LHS is no
2aa63 74 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  t contained with
2aa64 69 6e 20 74 68 65 20 52 48 53 20 61 6e 64 20 74  in the RHS and t
2aa65 68 65 0d 0a 2a 2a 20 52 48 53 20 63 6f 6e 74 61  he..** RHS conta
2aa66 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
2aa67 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 0d 0a 2a 2a  NULL values...**
2aa68 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
2aa69 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
2aa6a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 64 65   will jump to de
2aa6b 73 74 49 66 46 61 6c 73 65 20 69 66 20 74 68 65  stIfFalse if the
2aa6c 20 4c 48 53 20 69 73 20 6e 6f 74 20 0d 0a 2a 2a   LHS is not ..**
2aa6d 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
2aa6e 6e 20 74 68 65 20 52 48 53 2e 20 20 49 66 20 64  n the RHS.  If d
2aa6f 75 65 20 74 6f 20 4e 55 4c 4c 73 20 77 65 20 63  ue to NULLs we c
2aa70 61 6e 6e 6f 74 20 64 65 74 65 72 6d 69 6e 65 20  annot determine 
2aa71 69 66 20 74 68 65 20 4c 48 53 0d 0a 2a 2a 20 69  if the LHS..** i
2aa72 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
2aa73 68 65 20 52 48 53 20 74 68 65 6e 20 6a 75 6d 70  he RHS then jump
2aa74 20 74 6f 20 64 65 73 74 49 66 4e 75 6c 6c 2e 20   to destIfNull. 
2aa75 20 49 66 20 74 68 65 20 4c 48 53 20 69 73 20 63   If the LHS is c
2aa76 6f 6e 74 61 69 6e 65 64 0d 0a 2a 2a 20 77 69 74  ontained..** wit
2aa77 68 69 6e 20 74 68 65 20 52 48 53 20 74 68 65 6e  hin the RHS then
2aa78 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 0d 0a   fall through...
2aa79 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2aa7a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49  sqlite3ExprCodeI
2aa7b 4e 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  N(..  Parse *pPa
2aa7c 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2aa7d 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
2aa7e 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
2aa7f 78 74 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  xt */..  Expr *p
2aa80 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f  Expr,          /
2aa81 2a 20 54 68 65 20 49 4e 20 65 78 70 72 65 73 73  * The IN express
2aa82 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 64 65  ion */..  int de
2aa83 73 74 49 66 46 61 6c 73 65 2c 20 20 20 20 20 20  stIfFalse,      
2aa84 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
2aa85 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61  LHS is not conta
2aa86 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53 20  ined in the RHS 
2aa87 2a 2f 0d 0a 20 20 69 6e 74 20 64 65 73 74 49 66  */..  int destIf
2aa88 4e 75 6c 6c 20 20 20 20 20 20 20 20 2f 2a 20 4a  Null        /* J
2aa89 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20  ump here if the 
2aa8a 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 6b 6e  results are unkn
2aa8b 6f 77 6e 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73  own due to NULLs
2aa8c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
2aa8d 52 68 73 48 61 73 4e 75 6c 6c 20 3d 20 30 3b 20  RhsHasNull = 0; 
2aa8e 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68 61   /* Register tha
2aa8f 74 20 69 73 20 74 72 75 65 20 69 66 20 52 48 53  t is true if RHS
2aa90 20 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76   contains NULL v
2aa91 61 6c 75 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72  alues */..  char
2aa92 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20   affinity;      
2aa93 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20    /* Comparison 
2aa94 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20  affinity to use 
2aa95 2a 2f 0d 0a 20 20 69 6e 74 20 65 54 79 70 65 3b  */..  int eType;
2aa96 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2aa97 79 70 65 20 6f 66 20 74 68 65 20 52 48 53 20 2a  ype of the RHS *
2aa98 2f 0d 0a 20 20 69 6e 74 20 72 31 3b 20 20 20 20  /..  int r1;    
2aa99 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
2aa9a 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67 69  mporary use regi
2aa9b 73 74 65 72 20 2a 2f 0d 0a 20 20 56 64 62 65 20  ster */..  Vdbe 
2aa9c 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2aa9d 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 75 6e   /* Statement un
2aa9e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2aa9f 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6d 70   */....  /* Comp
2aaa0 75 74 65 20 74 68 65 20 52 48 53 2e 20 20 20 41  ute the RHS.   A
2aaa1 66 74 65 72 20 74 68 69 73 20 73 74 65 70 2c 20  fter this step, 
2aaa2 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 63  the table with c
2aaa3 75 72 73 6f 72 0d 0a 20 20 2a 2a 20 70 45 78 70  ursor..  ** pExp
2aaa4 72 2d 3e 69 54 61 62 6c 65 20 77 69 6c 6c 20 63  r->iTable will c
2aaa5 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75  ontains the valu
2aaa6 65 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20  es that make up 
2aaa7 74 68 65 20 52 48 53 2e 0d 0a 20 20 2a 2f 0d 0a  the RHS...  */..
2aaa8 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
2aaa9 64 62 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  dbe;..  assert( 
2aaaa 76 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a  v!=0 );       /*
2aaab 20 4f 4f 4d 20 64 65 74 65 63 74 65 64 20 70 72   OOM detected pr
2aaac 69 6f 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74  ior to this rout
2aaad 69 6e 65 20 2a 2f 0d 0a 20 20 56 64 62 65 4e 6f  ine */..  VdbeNo
2aaae 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62  opComment((v, "b
2aaaf 65 67 69 6e 20 49 4e 20 65 78 70 72 22 29 29 3b  egin IN expr"));
2aab0 0d 0a 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69  ..  eType = sqli
2aab1 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70  te3FindInIndex(p
2aab2 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72  Parse, pExpr, &r
2aab3 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0d 0a 0d 0a  RhsHasNull);....
2aab4 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
2aab5 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
2aab6 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20  use to create a 
2aab7 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73  key from the res
2aab8 75 6c 74 73 0d 0a 20 20 2a 2a 20 6f 66 20 74 68  ults..  ** of th
2aab9 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66  e expression. af
2aaba 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73  finityStr stores
2aabb 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67   a static string
2aabc 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0d 0a 20   suitable for.. 
2aabd 20 2a 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b   ** P4 of OP_Mak
2aabe 65 52 65 63 6f 72 64 2e 0d 0a 20 20 2a 2f 0d 0a  eRecord...  */..
2aabf 20 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d    affinity = com
2aac0 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28  parisonAffinity(
2aac1 70 45 78 70 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  pExpr);....  /* 
2aac2 43 6f 64 65 20 74 68 65 20 4c 48 53 2c 20 74 68  Code the LHS, th
2aac3 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c  e <expr> from "<
2aac4 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e  expr> IN (...)".
2aac5 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
2aac6 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
2aac7 50 61 72 73 65 29 3b 0d 0a 20 20 72 31 20 3d 20  Parse);..  r1 = 
2aac8 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2aac9 67 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 73 71  g(pParse);..  sq
2aaca 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2aacb 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
2aacc 66 74 2c 20 72 31 29 3b 0d 0a 0d 0a 20 20 2f 2a  ft, r1);....  /*
2aacd 20 49 66 20 74 68 65 20 4c 48 53 20 69 73 20 4e   If the LHS is N
2aace 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ULL, then the re
2aacf 73 75 6c 74 20 69 73 20 65 69 74 68 65 72 20 66  sult is either f
2aad0 61 6c 73 65 20 6f 72 20 4e 55 4c 4c 20 64 65 70  alse or NULL dep
2aad1 65 6e 64 69 6e 67 0d 0a 20 20 2a 2a 20 6f 6e 20  ending..  ** on 
2aad2 77 68 65 74 68 65 72 20 74 68 65 20 52 48 53 20  whether the RHS 
2aad3 69 73 20 65 6d 70 74 79 20 6f 72 20 6e 6f 74 2c  is empty or not,
2aad4 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0d 0a   respectively...
2aad5 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 65 73 74    */..  if( dest
2aad6 49 66 4e 75 6c 6c 3d 3d 64 65 73 74 49 66 46 61  IfNull==destIfFa
2aad7 6c 73 65 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 53  lse ){..    /* S
2aad8 68 6f 72 74 63 75 74 20 66 6f 72 20 74 68 65 20  hortcut for the 
2aad9 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72  common case wher
2aada 65 20 74 68 65 20 66 61 6c 73 65 20 61 6e 64 20  e the false and 
2aadb 4e 55 4c 4c 20 6f 75 74 63 6f 6d 65 73 20 61 72  NULL outcomes ar
2aadc 65 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61  e..    ** the sa
2aadd 6d 65 2e 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  me. */..    sqli
2aade 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2aadf 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20   OP_IsNull, r1, 
2aae0 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20  destIfNull);..  
2aae1 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20  }else{..    int 
2aae2 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
2aae3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2aae4 4e 6f 74 4e 75 6c 6c 2c 20 72 31 29 3b 0d 0a 20  NotNull, r1);.. 
2aae5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2aae6 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2aae7 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  d, pExpr->iTable
2aae8 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0d  , destIfFalse);.
2aae9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2aaea 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2aaeb 6f 2c 20 30 2c 20 64 65 73 74 49 66 4e 75 6c 6c  o, 0, destIfNull
2aaec 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2aaed 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2aaee 64 64 72 31 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ddr1);..  }.... 
2aaef 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49   if( eType==IN_I
2aaf0 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0d 0a 20  NDEX_ROWID ){.. 
2aaf1 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61     /* In this ca
2aaf2 73 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 74  se, the RHS is t
2aaf3 68 65 20 52 4f 57 49 44 20 6f 66 20 74 61 62 6c  he ROWID of tabl
2aaf4 65 20 62 2d 74 72 65 65 0d 0a 20 20 20 20 2a 2f  e b-tree..    */
2aaf5 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2aaf6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75  eAddOp2(v, OP_Mu
2aaf7 73 74 42 65 49 6e 74 2c 20 72 31 2c 20 64 65 73  stBeInt, r1, des
2aaf8 74 49 66 46 61 6c 73 65 29 3b 0d 0a 20 20 20 20  tIfFalse);..    
2aaf9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2aafa 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
2aafb 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  s, pExpr->iTable
2aafc 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 72  , destIfFalse, r
2aafd 31 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  1);..  }else{.. 
2aafe 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61     /* In this ca
2aaff 73 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 61  se, the RHS is a
2ab00 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 0d  n index b-tree..
2ab01 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  .    */..    sql
2ab02 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ab03 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 72  , OP_Affinity, r
2ab04 31 2c 20 31 2c 20 30 2c 20 26 61 66 66 69 6e 69  1, 1, 0, &affini
2ab05 74 79 2c 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 2f  ty, 1);....    /
2ab06 2a 20 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d  * If the set mem
2ab07 62 65 72 73 68 69 70 20 74 65 73 74 20 66 61 69  bership test fai
2ab08 6c 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ls, then the res
2ab09 75 6c 74 20 6f 66 20 74 68 65 20 0d 0a 20 20 20  ult of the ..   
2ab0a 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 22   ** "x IN (...)"
2ab0b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74   expression must
2ab0c 20 62 65 20 65 69 74 68 65 72 20 30 20 6f 72 20   be either 0 or 
2ab0d 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 74  NULL. If the set
2ab0e 0d 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e  ..    ** contain
2ab0f 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73  s no NULL values
2ab10 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
2ab11 74 20 69 73 20 30 2e 20 49 66 20 74 68 65 20 73  t is 0. If the s
2ab12 65 74 20 0d 0a 20 20 20 20 2a 2a 20 63 6f 6e 74  et ..    ** cont
2ab13 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
2ab14 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68   NULL values, th
2ab15 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
2ab16 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 65 78 70   the..    ** exp
2ab17 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20  ression is also 
2ab18 4e 55 4c 4c 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  NULL...    */.. 
2ab19 20 20 20 69 66 28 20 72 52 68 73 48 61 73 4e 75     if( rRhsHasNu
2ab1a 6c 6c 3d 3d 30 20 7c 7c 20 64 65 73 74 49 66 46  ll==0 || destIfF
2ab1b 61 6c 73 65 3d 3d 64 65 73 74 49 66 4e 75 6c 6c  alse==destIfNull
2ab1c 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68   ){..      /* Th
2ab1d 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69  is branch runs i
2ab1e 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74  f it is known at
2ab1f 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68   compile time th
2ab20 61 74 20 74 68 65 20 52 48 53 0d 0a 20 20 20 20  at the RHS..    
2ab21 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f 6e 74    ** cannot cont
2ab22 61 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e  ain NULL values.
2ab23 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 61 73   This happens as
2ab24 20 74 68 65 20 72 65 73 75 6c 74 0d 0a 20 20 20   the result..   
2ab25 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20     ** of a "NOT 
2ab26 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74  NULL" constraint
2ab27 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
2ab28 20 73 63 68 65 6d 61 2e 0d 0a 20 20 20 20 20 20   schema...      
2ab29 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 41 6c 73  **..      ** Als
2ab2a 6f 20 72 75 6e 20 74 68 69 73 20 62 72 61 6e 63  o run this branc
2ab2b 68 20 69 66 20 4e 55 4c 4c 20 69 73 20 65 71 75  h if NULL is equ
2ab2c 69 76 61 6c 65 6e 74 20 74 6f 20 46 41 4c 53 45  ivalent to FALSE
2ab2d 0d 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74  ..      ** for t
2ab2e 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 49  his particular I
2ab2f 4e 20 6f 70 65 72 61 74 6f 72 2e 0d 0a 20 20 20  N operator...   
2ab30 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c     */..      sql
2ab31 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e  ite3VdbeAddOp4In
2ab32 74 28 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64  t(v, OP_NotFound
2ab33 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
2ab34 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 72 31   destIfFalse, r1
2ab35 2c 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 7d 65 6c  , 1);....    }el
2ab36 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 6e  se{..      /* In
2ab37 20 74 68 69 73 20 62 72 61 6e 63 68 2c 20 74 68   this branch, th
2ab38 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20  e RHS of the IN 
2ab39 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 20  might contain a 
2ab3a 4e 55 4c 4c 20 61 6e 64 0d 0a 20 20 20 20 20 20  NULL and..      
2ab3b 2a 2a 20 74 68 65 20 70 72 65 73 65 6e 63 65 20  ** the presence 
2ab3c 6f 66 20 61 20 4e 55 4c 4c 20 6f 6e 20 74 68 65  of a NULL on the
2ab3d 20 52 48 53 20 6d 61 6b 65 73 20 61 20 64 69 66   RHS makes a dif
2ab3e 66 65 72 65 6e 63 65 20 69 6e 20 74 68 65 0d 0a  ference in the..
2ab3f 20 20 20 20 20 20 2a 2a 20 6f 75 74 63 6f 6d 65        ** outcome
2ab40 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
2ab41 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32 2c 20 6a     int j1, j2, j
2ab42 33 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 46  3;....      /* F
2ab43 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65  irst check to se
2ab44 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20  e if the LHS is 
2ab45 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
2ab46 20 52 48 53 2e 20 20 49 66 20 73 6f 2c 0d 0a 20   RHS.  If so,.. 
2ab47 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
2ab48 20 70 72 65 73 65 6e 63 65 20 6f 66 20 4e 55 4c   presence of NUL
2ab49 4c 73 20 69 6e 20 74 68 65 20 52 48 53 20 64 6f  Ls in the RHS do
2ab4a 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2c 20 73  es not matter, s
2ab4b 6f 20 6a 75 6d 70 0d 0a 20 20 20 20 20 20 2a 2a  o jump..      **
2ab4c 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65   over all of the
2ab4d 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f   code that follo
2ab4e 77 73 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  ws...      */.. 
2ab4f 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
2ab50 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76  3VdbeAddOp4Int(v
2ab51 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70  , OP_Found, pExp
2ab52 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 31  r->iTable, 0, r1
2ab53 2c 20 31 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f  , 1);....      /
2ab54 2a 20 48 65 72 65 20 77 65 20 62 65 67 69 6e 20  * Here we begin 
2ab55 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
2ab56 74 68 61 74 20 72 75 6e 73 20 69 66 20 74 68 65  that runs if the
2ab57 20 4c 48 53 20 69 73 20 6e 6f 74 0d 0a 20 20 20   LHS is not..   
2ab58 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20     ** contained 
2ab59 77 69 74 68 69 6e 20 74 68 65 20 52 48 53 2e 20  within the RHS. 
2ab5a 20 47 65 6e 65 72 61 74 65 20 61 64 64 69 74 69   Generate additi
2ab5b 6f 6e 61 6c 20 63 6f 64 65 20 74 68 61 74 0d 0a  onal code that..
2ab5c 20 20 20 20 20 20 2a 2a 20 74 65 73 74 73 20 74        ** tests t
2ab5d 68 65 20 52 48 53 20 66 6f 72 20 4e 55 4c 4c 73  he RHS for NULLs
2ab5e 2e 20 20 49 66 20 74 68 65 20 52 48 53 20 63 6f  .  If the RHS co
2ab5f 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 20 74 68  ntains a NULL th
2ab60 65 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 6a 75 6d  en..      ** jum
2ab61 70 20 74 6f 20 64 65 73 74 49 66 4e 75 6c 6c 2e  p to destIfNull.
2ab62 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e    If there are n
2ab63 6f 20 4e 55 4c 4c 73 20 69 6e 20 74 68 65 20 52  o NULLs in the R
2ab64 48 53 20 74 68 65 6e 0d 0a 20 20 20 20 20 20 2a  HS then..      *
2ab65 2a 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 49 66  * jump to destIf
2ab66 46 61 6c 73 65 2e 0d 0a 20 20 20 20 20 20 2a 2f  False...      */
2ab67 0d 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c  ..      j2 = sql
2ab68 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2ab69 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 52  , OP_NotNull, rR
2ab6a 68 73 48 61 73 4e 75 6c 6c 29 3b 0d 0a 20 20 20  hsHasNull);..   
2ab6b 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56     j3 = sqlite3V
2ab6c 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20  dbeAddOp4Int(v, 
2ab6d 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d  OP_Found, pExpr-
2ab6e 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 52 68 73  >iTable, 0, rRhs
2ab6f 48 61 73 4e 75 6c 6c 2c 20 31 29 3b 0d 0a 20 20  HasNull, 1);..  
2ab70 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ab71 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2ab72 67 65 72 2c 20 2d 31 2c 20 72 52 68 73 48 61 73  ger, -1, rRhsHas
2ab73 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 73 71  Null);..      sq
2ab74 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2ab75 65 28 76 2c 20 6a 33 29 3b 0d 0a 20 20 20 20 20  e(v, j3);..     
2ab76 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ab77 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2ab78 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 29   rRhsHasNull, 1)
2ab79 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2ab7a 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2ab7b 6a 32 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  j2);....      /*
2ab7c 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 61 70 70   Jump to the app
2ab7d 72 6f 70 72 69 61 74 65 20 74 61 72 67 65 74 20  ropriate target 
2ab7e 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65  depending on whe
2ab7f 74 68 65 72 20 6f 72 20 6e 6f 74 0d 0a 20 20 20  ther or not..   
2ab80 20 20 20 2a 2a 20 74 68 65 20 52 48 53 20 63 6f     ** the RHS co
2ab81 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0d 0a 20  ntains a NULL.. 
2ab82 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73       */..      s
2ab83 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ab84 28 76 2c 20 4f 50 5f 49 66 2c 20 72 52 68 73 48  (v, OP_If, rRhsH
2ab85 61 73 4e 75 6c 6c 2c 20 64 65 73 74 49 66 4e 75  asNull, destIfNu
2ab86 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ll);..      sqli
2ab87 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ab88 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 64 65 73   OP_Goto, 0, des
2ab89 74 49 66 46 61 6c 73 65 29 3b 0d 0a 0d 0a 20 20  tIfFalse);....  
2ab8a 20 20 20 20 2f 2a 20 54 68 65 20 4f 50 5f 46 6f      /* The OP_Fo
2ab8b 75 6e 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f  und at the top o
2ab8c 66 20 74 68 69 73 20 62 72 61 6e 63 68 20 6a 75  f this branch ju
2ab8d 6d 70 73 20 68 65 72 65 20 77 68 65 6e 20 74 72  mps here when tr
2ab8e 75 65 2c 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63  ue, ..      ** c
2ab8f 61 75 73 69 6e 67 20 74 68 65 20 6f 76 65 72 61  ausing the overa
2ab90 6c 6c 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e  ll IN expression
2ab91 20 65 76 61 6c 75 61 74 69 6f 6e 20 74 6f 20 66   evaluation to f
2ab92 61 6c 6c 20 74 68 72 6f 75 67 68 2e 0d 0a 20 20  all through...  
2ab93 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71      */..      sq
2ab94 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2ab95 65 28 76 2c 20 6a 31 29 3b 0d 0a 20 20 20 20 7d  e(v, j1);..    }
2ab96 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
2ab97 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2ab98 50 61 72 73 65 2c 20 72 31 29 3b 0d 0a 20 20 73  Parse, r1);..  s
2ab99 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
2ab9a 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0d 0a  op(pParse, 1);..
2ab9b 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2ab9c 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 22 29  , "end IN expr")
2ab9d 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  );..}..#endif /*
2ab9e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2ab9f 51 55 45 52 59 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  QUERY */..../*..
2aba0 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20  ** Duplicate an 
2aba1 38 2d 62 79 74 65 20 76 61 6c 75 65 0d 0a 2a 2f  8-byte value..*/
2aba2 0d 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64  ..static char *d
2aba3 75 70 38 62 79 74 65 73 28 56 64 62 65 20 2a 76  up8bytes(Vdbe *v
2aba4 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e  , const char *in
2aba5 29 7b 0d 0a 20 20 63 68 61 72 20 2a 6f 75 74 20  ){..  char *out 
2aba6 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2aba7 63 52 61 77 28 73 71 6c 69 74 65 33 56 64 62 65  cRaw(sqlite3Vdbe
2aba8 44 62 28 76 29 2c 20 38 29 3b 0d 0a 20 20 69 66  Db(v), 8);..  if
2aba9 28 20 6f 75 74 20 29 7b 0d 0a 20 20 20 20 6d 65  ( out ){..    me
2abaa 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29  mcpy(out, in, 8)
2abab 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
2abac 20 6f 75 74 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e   out;..}....#ifn
2abad 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2abae 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a  FLOATING_POINT..
2abaf 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
2abb0 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  an instruction t
2abb1 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65  hat will put the
2abb2 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0d   floating point.
2abb3 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69  .** value descri
2abb4 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d  bed by z[0..n-1]
2abb5 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69   into register i
2abb6 4d 65 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  Mem...**..** The
2abb7 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c   z[] string will
2abb8 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65   probably not be
2abb9 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
2abba 2e 20 20 42 75 74 20 74 68 65 20 0d 0a 2a 2a 20  .  But the ..** 
2abbb 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20 69  z[n] character i
2abbc 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
2abbd 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61  be something tha
2abbe 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0d  t does not look.
2abbf 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e  .** like the con
2abc0 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65  tinuation of the
2abc1 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2f 0d 0a 73 74   number...*/..st
2abc2 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65  atic void codeRe
2abc3 61 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73  al(Vdbe *v, cons
2abc4 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
2abc5 65 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69  egateFlag, int i
2abc6 4d 65 6d 29 7b 0d 0a 20 20 69 66 28 20 41 4c 57  Mem){..  if( ALW
2abc7 41 59 53 28 7a 21 3d 30 29 20 29 7b 0d 0a 20 20  AYS(z!=0) ){..  
2abc8 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0d    double value;.
2abc9 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0d 0a  .    char *zV;..
2abca 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
2abcb 7a 2c 20 26 76 61 6c 75 65 2c 20 73 71 6c 69 74  z, &value, sqlit
2abcc 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c 20 53  e3Strlen30(z), S
2abcd 51 4c 49 54 45 5f 55 54 46 38 29 3b 0d 0a 20 20  QLITE_UTF8);..  
2abce 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74    assert( !sqlit
2abcf 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29  e3IsNaN(value) )
2abd0 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f  ; /* The new Ato
2abd1 46 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20  F never returns 
2abd2 4e 61 4e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  NaN */..    if( 
2abd3 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c  negateFlag ) val
2abd4 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0d 0a 20 20  ue = -value;..  
2abd5 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73    zV = dup8bytes
2abd6 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75  (v, (char*)&valu
2abd7 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
2abd8 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2abd9 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20  _Real, 0, iMem, 
2abda 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b  0, zV, P4_REAL);
2abdb 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66  ..  }..}..#endif
2abdc 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e  ....../*..** Gen
2abdd 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63  erate an instruc
2abde 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70  tion that will p
2abdf 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64  ut the integer d
2abe0 65 73 63 72 69 62 65 20 62 79 0d 0a 2a 2a 20 74  escribe by..** t
2abe1 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e  ext z[0..n-1] in
2abe2 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
2abe3 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 70 72 2e 75  ...**..** Expr.u
2abe4 2e 7a 54 6f 6b 65 6e 20 69 73 20 61 6c 77 61 79  .zToken is alway
2abe5 73 20 55 54 46 38 20 61 6e 64 20 7a 65 72 6f 2d  s UTF8 and zero-
2abe6 74 65 72 6d 69 6e 61 74 65 64 2e 0d 0a 2a 2f 0d  terminated...*/.
2abe7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2abe8 65 49 6e 74 65 67 65 72 28 50 61 72 73 65 20 2a  eInteger(Parse *
2abe9 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
2abea 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67  xpr, int negFlag
2abeb 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0d 0a 20 20  , int iMem){..  
2abec 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2abed 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 69 66 28 20  ->pVdbe;..  if( 
2abee 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
2abef 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0d 0a 20  P_IntValue ){.. 
2abf0 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72     int i = pExpr
2abf1 2d 3e 75 2e 69 56 61 6c 75 65 3b 0d 0a 20 20 20  ->u.iValue;..   
2abf2 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b   assert( i>=0 );
2abf3 0d 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61  ..    if( negFla
2abf4 67 20 29 20 69 20 3d 20 2d 69 3b 0d 0a 20 20 20  g ) i = -i;..   
2abf5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2abf6 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2abf7 2c 20 69 2c 20 69 4d 65 6d 29 3b 0d 0a 20 20 7d  , i, iMem);..  }
2abf8 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20 63  else{..    int c
2abf9 3b 0d 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65  ;..    i64 value
2abfa 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ;..    const cha
2abfb 72 20 2a 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e  r *z = pExpr->u.
2abfc 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 61 73 73  zToken;..    ass
2abfd 65 72 74 28 20 7a 21 3d 30 20 29 3b 0d 0a 20 20  ert( z!=0 );..  
2abfe 20 20 63 20 3d 20 73 71 6c 69 74 65 33 41 74 6f    c = sqlite3Ato
2abff 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 2c 20 73  i64(z, &value, s
2ac00 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2ac01 29 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  ), SQLITE_UTF8);
2ac02 0d 0a 20 20 20 20 69 66 28 20 63 3d 3d 30 20 7c  ..    if( c==0 |
2ac03 7c 20 28 63 3d 3d 32 20 26 26 20 6e 65 67 46 6c  | (c==2 && negFl
2ac04 61 67 29 20 29 7b 0d 0a 20 20 20 20 20 20 63 68  ag) ){..      ch
2ac05 61 72 20 2a 7a 56 3b 0d 0a 20 20 20 20 20 20 69  ar *zV;..      i
2ac06 66 28 20 6e 65 67 46 6c 61 67 20 29 7b 20 76 61  f( negFlag ){ va
2ac07 6c 75 65 20 3d 20 63 3d 3d 32 20 3f 20 53 4d 41  lue = c==2 ? SMA
2ac08 4c 4c 45 53 54 5f 49 4e 54 36 34 20 3a 20 2d 76  LLEST_INT64 : -v
2ac09 61 6c 75 65 3b 20 7d 0d 0a 20 20 20 20 20 20 7a  alue; }..      z
2ac0a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c  V = dup8bytes(v,
2ac0b 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b   (char*)&value);
2ac0c 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
2ac0d 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2ac0e 49 6e 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20  Int64, 0, iMem, 
2ac0f 30 2c 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29  0, zV, P4_INT64)
2ac10 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 23  ;..    }else{..#
2ac11 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2ac12 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
2ac13 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2ac14 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2ac15 22 6f 76 65 72 73 69 7a 65 64 20 69 6e 74 65 67  "oversized integ
2ac16 65 72 3a 20 25 73 25 73 22 2c 20 6e 65 67 46 6c  er: %s%s", negFl
2ac17 61 67 20 3f 20 22 2d 22 20 3a 20 22 22 2c 20 7a  ag ? "-" : "", z
2ac18 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 20  );..#else..     
2ac19 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20   codeReal(v, z, 
2ac1a 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0d  negFlag, iMem);.
2ac1b 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 7d 0d 0a  .#endif..    }..
2ac1c 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
2ac1d 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 65   Clear a cache e
2ac1e 6e 74 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ntry...*/..stati
2ac1f 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72  c void cacheEntr
2ac20 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50  yClear(Parse *pP
2ac21 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f  arse, struct yCo
2ac22 6c 43 61 63 68 65 20 2a 70 29 7b 0d 0a 20 20 69  lCache *p){..  i
2ac23 66 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b  f( p->tempReg ){
2ac24 0d 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  ..    if( pParse
2ac25 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79  ->nTempReg<Array
2ac26 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65  Size(pParse->aTe
2ac27 6d 70 52 65 67 29 20 29 7b 0d 0a 20 20 20 20 20  mpReg) ){..     
2ac28 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65   pParse->aTempRe
2ac29 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52  g[pParse->nTempR
2ac2a 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b  eg++] = p->iReg;
2ac2b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d 3e  ..    }..    p->
2ac2c 74 65 6d 70 52 65 67 20 3d 20 30 3b 0d 0a 20 20  tempReg = 0;..  
2ac2d 7d 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  }..}....../*..**
2ac2e 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20 63   Record in the c
2ac2f 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 61 74  olumn cache that
2ac30 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f   a particular co
2ac31 6c 75 6d 6e 20 66 72 6f 6d 20 61 0d 0a 2a 2a 20  lumn from a..** 
2ac32 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65  particular table
2ac33 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
2ac34 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73  particular regis
2ac35 74 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ter...*/..SQLITE
2ac36 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2ac37 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74  lite3ExprCacheSt
2ac38 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ore(Parse *pPars
2ac39 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74  e, int iTab, int
2ac3a 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29   iCol, int iReg)
2ac3b 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69  {..  int i;..  i
2ac3c 6e 74 20 6d 69 6e 4c 72 75 3b 0d 0a 20 20 69 6e  nt minLru;..  in
2ac3d 74 20 69 64 78 4c 72 75 3b 0d 0a 20 20 73 74 72  t idxLru;..  str
2ac3e 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
2ac3f 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 69  ;....  assert( i
2ac40 52 65 67 3e 30 20 29 3b 20 20 2f 2a 20 52 65 67  Reg>0 );  /* Reg
2ac41 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 61 72  ister numbers ar
2ac42 65 20 61 6c 77 61 79 73 20 70 6f 73 69 74 69 76  e always positiv
2ac43 65 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
2ac44 69 43 6f 6c 3e 3d 2d 31 20 26 26 20 69 43 6f 6c  iCol>=-1 && iCol
2ac45 3c 33 32 37 36 38 20 29 3b 20 20 2f 2a 20 46 69  <32768 );  /* Fi
2ac46 6e 69 74 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  nite column numb
2ac47 65 72 73 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54  ers */....  /* T
2ac48 68 65 20 53 51 4c 49 54 45 5f 43 6f 6c 75 6d 6e  he SQLITE_Column
2ac49 43 61 63 68 65 20 66 6c 61 67 20 64 69 73 61 62  Cache flag disab
2ac4a 6c 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63  les the column c
2ac4b 61 63 68 65 2e 20 20 54 68 69 73 20 69 73 20 75  ache.  This is u
2ac4c 73 65 64 0d 0a 20 20 2a 2a 20 66 6f 72 20 74 65  sed..  ** for te
2ac4d 73 74 69 6e 67 20 6f 6e 6c 79 20 2d 20 74 6f 20  sting only - to 
2ac4e 76 65 72 69 66 79 20 74 68 61 74 20 53 51 4c 69  verify that SQLi
2ac4f 74 65 20 61 6c 77 61 79 73 20 67 65 74 73 20 74  te always gets t
2ac50 68 65 20 73 61 6d 65 20 61 6e 73 77 65 72 0d 0a  he same answer..
2ac51 20 20 2a 2a 20 77 69 74 68 20 61 6e 64 20 77 69    ** with and wi
2ac52 74 68 6f 75 74 20 74 68 65 20 63 6f 6c 75 6d 6e  thout the column
2ac53 20 63 61 63 68 65 2e 0d 0a 20 20 2a 2f 0d 0a 20   cache...  */.. 
2ac54 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2ac55 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2ac56 43 6f 6c 75 6d 6e 43 61 63 68 65 20 29 20 72 65  ColumnCache ) re
2ac57 74 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 46 69  turn;....  /* Fi
2ac58 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79 20  rst replace any 
2ac59 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 0d  existing entry..
2ac5a 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 41 63 74 75  .  **..  ** Actu
2ac5b 61 6c 6c 79 2c 20 74 68 65 20 77 61 79 20 74 68  ally, the way th
2ac5c 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 69  e column cache i
2ac5d 73 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64  s currently used
2ac5e 2c 20 77 65 20 61 72 65 20 67 75 61 72 61 6e 74  , we are guarant
2ac5f 65 65 64 0d 0a 20 20 2a 2a 20 74 68 61 74 20 74  eed..  ** that t
2ac60 68 65 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 6e  he object will n
2ac61 65 76 65 72 20 61 6c 72 65 61 64 79 20 62 65 20  ever already be 
2ac62 69 6e 20 63 61 63 68 65 2e 20 20 56 65 72 69 66  in cache.  Verif
2ac63 79 20 74 68 69 73 20 67 75 61 72 61 6e 74 65 65  y this guarantee
2ac64 2e 0d 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66  ...  */..#ifndef
2ac65 20 4e 44 45 42 55 47 0d 0a 20 20 66 6f 72 28 69   NDEBUG..  for(i
2ac66 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
2ac67 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
2ac68 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
2ac69 2b 2c 20 70 2b 2b 29 7b 0d 0a 23 69 66 20 30 20  +, p++){..#if 0 
2ac6a 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 77 6f 6c  /* This code wol
2ac6b 64 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74  d remove the ent
2ac6c 72 79 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ry from the cach
2ac6d 65 20 69 66 20 69 74 20 65 78 69 73 74 65 64 20  e if it existed 
2ac6e 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  */..    if( p->i
2ac6f 52 65 67 20 26 26 20 70 2d 3e 69 54 61 62 6c 65  Reg && p->iTable
2ac70 3d 3d 69 54 61 62 20 26 26 20 70 2d 3e 69 43 6f  ==iTab && p->iCo
2ac71 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b 0d 0a 20  lumn==iCol ){.. 
2ac72 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43       cacheEntryC
2ac73 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b  lear(pParse, p);
2ac74 0d 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65  ..      p->iLeve
2ac75 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63  l = pParse->iCac
2ac76 68 65 4c 65 76 65 6c 3b 0d 0a 20 20 20 20 20 20  heLevel;..      
2ac77 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0d  p->iReg = iReg;.
2ac78 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20  .      p->lru = 
2ac79 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e  pParse->iCacheCn
2ac7a 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  t++;..      retu
2ac7b 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  rn;..    }..#end
2ac7c 69 66 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  if..    assert( 
2ac7d 70 2d 3e 69 52 65 67 3d 3d 30 20 7c 7c 20 70 2d  p->iReg==0 || p-
2ac7e 3e 69 54 61 62 6c 65 21 3d 69 54 61 62 20 7c 7c  >iTable!=iTab ||
2ac7f 20 70 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f   p->iColumn!=iCo
2ac80 6c 20 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  l );..  }..#endi
2ac81 66 0d 0a 0d 0a 20 20 2f 2a 20 46 69 6e 64 20 61  f....  /* Find a
2ac82 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e 64  n empty slot and
2ac83 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0d 0a   replace it */..
2ac84 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
2ac85 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
2ac86 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
2ac87 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0d  CHE; i++, p++){.
2ac88 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67  .    if( p->iReg
2ac89 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d  ==0 ){..      p-
2ac8a 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  >iLevel = pParse
2ac8b 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0d 0a  ->iCacheLevel;..
2ac8c 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20        p->iTable 
2ac8d 3d 20 69 54 61 62 3b 0d 0a 20 20 20 20 20 20 70  = iTab;..      p
2ac8e 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
2ac8f 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  ;..      p->iReg
2ac90 20 3d 20 69 52 65 67 3b 0d 0a 20 20 20 20 20 20   = iReg;..      
2ac91 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0d  p->tempReg = 0;.
2ac92 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20  .      p->lru = 
2ac93 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e  pParse->iCacheCn
2ac94 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  t++;..      retu
2ac95 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  rn;..    }..  }.
2ac96 0a 0d 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20  ...  /* Replace 
2ac97 74 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c  the last recentl
2ac98 79 20 75 73 65 64 20 2a 2f 0d 0a 20 20 6d 69 6e  y used */..  min
2ac99 4c 72 75 20 3d 20 30 78 37 66 66 66 66 66 66 66  Lru = 0x7fffffff
2ac9a 3b 0d 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d 31  ;..  idxLru = -1
2ac9b 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  ;..  for(i=0, p=
2ac9c 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
2ac9d 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
2ac9e 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
2ac9f 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6c  ){..    if( p->l
2aca0 72 75 3c 6d 69 6e 4c 72 75 20 29 7b 0d 0a 20 20  ru<minLru ){..  
2aca1 20 20 20 20 69 64 78 4c 72 75 20 3d 20 69 3b 0d      idxLru = i;.
2aca2 0a 20 20 20 20 20 20 6d 69 6e 4c 72 75 20 3d 20  .      minLru = 
2aca3 70 2d 3e 6c 72 75 3b 0d 0a 20 20 20 20 7d 0d 0a  p->lru;..    }..
2aca4 20 20 7d 0d 0a 20 20 69 66 28 20 41 4c 57 41 59    }..  if( ALWAY
2aca5 53 28 69 64 78 4c 72 75 3e 3d 30 29 20 29 7b 0d  S(idxLru>=0) ){.
2aca6 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65  .    p = &pParse
2aca7 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 78 4c  ->aColCache[idxL
2aca8 72 75 5d 3b 0d 0a 20 20 20 20 70 2d 3e 69 4c 65  ru];..    p->iLe
2aca9 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  vel = pParse->iC
2acaa 61 63 68 65 4c 65 76 65 6c 3b 0d 0a 20 20 20 20  acheLevel;..    
2acab 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62  p->iTable = iTab
2acac 3b 0d 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d  ;..    p->iColum
2acad 6e 20 3d 20 69 43 6f 6c 3b 0d 0a 20 20 20 20 70  n = iCol;..    p
2acae 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0d 0a  ->iReg = iReg;..
2acaf 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d      p->tempReg =
2acb0 20 30 3b 0d 0a 20 20 20 20 70 2d 3e 6c 72 75 20   0;..    p->lru 
2acb1 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
2acb2 43 6e 74 2b 2b 3b 0d 0a 20 20 20 20 72 65 74 75  Cnt++;..    retu
2acb3 72 6e 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  rn;..  }..}..../
2acb4 2a 0d 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74  *..** Indicate t
2acb5 68 61 74 20 72 65 67 69 73 74 65 72 73 20 62 65  hat registers be
2acb6 74 77 65 65 6e 20 69 52 65 67 2e 2e 69 52 65 67  tween iReg..iReg
2acb7 2b 6e 52 65 67 2d 31 20 61 72 65 20 62 65 69 6e  +nReg-1 are bein
2acb8 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0d 0a  g overwritten...
2acb9 2a 2a 20 50 75 72 67 65 20 74 68 65 20 72 61 6e  ** Purge the ran
2acba 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
2acbb 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20  from the column 
2acbc 63 61 63 68 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  cache...*/..SQLI
2acbd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2acbe 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2acbf 52 65 6d 6f 76 65 28 50 61 72 73 65 20 2a 70 50  Remove(Parse *pP
2acc0 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20  arse, int iReg, 
2acc1 69 6e 74 20 6e 52 65 67 29 7b 0d 0a 20 20 69 6e  int nReg){..  in
2acc2 74 20 69 3b 0d 0a 20 20 69 6e 74 20 69 4c 61 73  t i;..  int iLas
2acc3 74 20 3d 20 69 52 65 67 20 2b 20 6e 52 65 67 20  t = iReg + nReg 
2acc4 2d 20 31 3b 0d 0a 20 20 73 74 72 75 63 74 20 79  - 1;..  struct y
2acc5 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0d 0a 20 20  ColCache *p;..  
2acc6 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
2acc7 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
2acc8 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
2acc9 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20  E; i++, p++){.. 
2acca 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52     int r = p->iR
2accb 65 67 3b 0d 0a 20 20 20 20 69 66 28 20 72 3e 3d  eg;..    if( r>=
2accc 69 52 65 67 20 26 26 20 72 3c 3d 69 4c 61 73 74  iReg && r<=iLast
2accd 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 63 68 65   ){..      cache
2acce 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73  EntryClear(pPars
2accf 65 2c 20 70 29 3b 0d 0a 20 20 20 20 20 20 70 2d  e, p);..      p-
2acd0 3e 69 52 65 67 20 3d 20 30 3b 0d 0a 20 20 20 20  >iReg = 0;..    
2acd1 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  }..  }..}..../*.
2acd2 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65  .** Remember the
2acd3 20 63 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20   current column 
2acd4 63 61 63 68 65 20 63 6f 6e 74 65 78 74 2e 20 20  cache context.  
2acd5 41 6e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 20  Any new entries 
2acd6 61 64 64 65 64 0d 0a 2a 2a 20 61 64 64 65 64 20  added..** added 
2acd7 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  to the column ca
2acd8 63 68 65 20 61 66 74 65 72 20 74 68 69 73 20 63  che after this c
2acd9 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 65 64 20  all are removed 
2acda 77 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 63 6f 72  when the..** cor
2acdb 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 20 6f  responding pop o
2acdc 63 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  ccurs...*/..SQLI
2acdd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2acde 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2acdf 50 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72  Push(Parse *pPar
2ace0 73 65 29 7b 0d 0a 20 20 70 50 61 72 73 65 2d 3e  se){..  pParse->
2ace1 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0d 0a  iCacheLevel++;..
2ace2 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6d 6f  }..../*..** Remo
2ace3 76 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75  ve from the colu
2ace4 6d 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74  mn cache any ent
2ace5 72 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61  ries that were a
2ace6 64 64 65 64 20 73 69 6e 63 65 20 74 68 65 0d 0a  dded since the..
2ace7 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  ** the previous 
2ace8 4e 20 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e  N Push operation
2ace9 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  s.  In other wor
2acea 64 73 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20  ds, restore the 
2aceb 63 61 63 68 65 0d 0a 2a 2a 20 74 6f 20 74 68 65  cache..** to the
2acec 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e   state it was in
2aced 20 4e 20 50 75 73 68 65 73 20 61 67 6f 2e 0d 0a   N Pushes ago...
2acee 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
2acef 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
2acf0 78 70 72 43 61 63 68 65 50 6f 70 28 50 61 72 73  xprCachePop(Pars
2acf1 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 4e  e *pParse, int N
2acf2 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
2acf3 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
2acf4 20 2a 70 3b 0d 0a 20 20 61 73 73 65 72 74 28 20   *p;..  assert( 
2acf5 4e 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  N>0 );..  assert
2acf6 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  ( pParse->iCache
2acf7 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0d 0a 20 20 70  Level>=N );..  p
2acf8 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
2acf9 65 6c 20 2d 3d 20 4e 3b 0d 0a 20 20 66 6f 72 28  el -= N;..  for(
2acfa 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
2acfb 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2acfc 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
2acfd 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20 20 20 20 69  ++, p++){..    i
2acfe 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d  f( p->iReg && p-
2acff 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e  >iLevel>pParse->
2ad00 69 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0d 0a  iCacheLevel ){..
2ad01 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79        cacheEntry
2ad02 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29  Clear(pParse, p)
2ad03 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  ;..      p->iReg
2ad04 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 0;..    }..  
2ad05 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57  }..}..../*..** W
2ad06 68 65 6e 20 61 20 63 61 63 68 65 64 20 63 6f 6c  hen a cached col
2ad07 75 6d 6e 20 69 73 20 72 65 75 73 65 64 2c 20 6d  umn is reused, m
2ad08 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 69 74  ake sure that it
2ad09 73 20 72 65 67 69 73 74 65 72 20 69 73 0d 0a 2a  s register is..*
2ad0a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 76 61 69  * no longer avai
2ad0b 6c 61 62 6c 65 20 61 73 20 61 20 74 65 6d 70 20  lable as a temp 
2ad0c 72 65 67 69 73 74 65 72 2e 20 20 74 69 63 6b 65  register.  ticke
2ad0d 74 20 23 33 38 37 39 3a 20 20 74 68 61 74 20 73  t #3879:  that s
2ad0e 61 6d 65 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72  ame..** register
2ad0f 20 6d 69 67 68 74 20 62 65 20 69 6e 20 74 68 65   might be in the
2ad10 20 63 61 63 68 65 20 69 6e 20 6d 75 6c 74 69 70   cache in multip
2ad11 6c 65 20 70 6c 61 63 65 73 2c 20 73 6f 20 62 65  le places, so be
2ad12 20 73 75 72 65 20 74 6f 0d 0a 2a 2a 20 67 65 74   sure to..** get
2ad13 20 74 68 65 6d 20 61 6c 6c 2e 0d 0a 2a 2f 0d 0a   them all...*/..
2ad14 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
2ad15 74 65 33 45 78 70 72 43 61 63 68 65 50 69 6e 52  te3ExprCachePinR
2ad16 65 67 69 73 74 65 72 28 50 61 72 73 65 20 2a 70  egister(Parse *p
2ad17 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29  Parse, int iReg)
2ad18 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 73  {..  int i;..  s
2ad19 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
2ad1a 2a 70 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 2c 20  *p;..  for(i=0, 
2ad1b 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
2ad1c 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
2ad1d 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
2ad1e 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d  ++){..    if( p-
2ad1f 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0d 0a  >iReg==iReg ){..
2ad20 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67        p->tempReg
2ad21 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 0;..    }..  
2ad22 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  }..}..../*..** G
2ad23 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
2ad24 65 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75  extract the valu
2ad25 65 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68  e of the iCol-th
2ad26 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62   column of a tab
2ad27 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  le...*/..SQLITE_
2ad28 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2ad29 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
2ad2a 6f 6c 75 6d 6e 4f 66 54 61 62 6c 65 28 0d 0a 20  olumnOfTable(.. 
2ad2b 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
2ad2c 20 2f 2a 20 54 68 65 20 56 44 42 45 20 75 6e 64   /* The VDBE und
2ad2d 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
2ad2e 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  */..  Table *pTa
2ad2f 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  b,    /* The tab
2ad30 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
2ad31 65 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 69 6e  e value */..  in
2ad32 74 20 69 54 61 62 43 75 72 2c 20 20 20 20 2f 2a  t iTabCur,    /*
2ad33 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20   The cursor for 
2ad34 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  this table */.. 
2ad35 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
2ad36 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2ad37 20 63 6f 6c 75 6d 6e 20 74 6f 20 65 78 74 72 61   column to extra
2ad38 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  ct */..  int reg
2ad39 4f 75 74 20 20 20 20 20 20 2f 2a 20 45 78 74 72  Out      /* Extr
2ad3a 61 63 74 20 74 68 65 20 76 61 6c 75 64 20 69 6e  act the valud in
2ad3b 74 6f 20 74 68 69 73 20 72 65 67 69 73 74 65 72  to this register
2ad3c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 69   */..){..  if( i
2ad3d 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c 3d 3d 70  Col<0 || iCol==p
2ad3e 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0d 0a 20  Tab->iPKey ){.. 
2ad3f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ad40 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
2ad41 2c 20 69 54 61 62 43 75 72 2c 20 72 65 67 4f 75  , iTabCur, regOu
2ad42 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  t);..  }else{.. 
2ad43 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69     int op = IsVi
2ad44 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50  rtual(pTab) ? OP
2ad45 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f  _VColumn : OP_Co
2ad46 6c 75 6d 6e 3b 0d 0a 20 20 20 20 73 71 6c 69 74  lumn;..    sqlit
2ad47 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2ad48 6f 70 2c 20 69 54 61 62 43 75 72 2c 20 69 43 6f  op, iTabCur, iCo
2ad49 6c 2c 20 72 65 67 4f 75 74 29 3b 0d 0a 20 20 7d  l, regOut);..  }
2ad4a 0d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20  ..  if( iCol>=0 
2ad4b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 43  ){..    sqlite3C
2ad4c 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
2ad4d 70 54 61 62 2c 20 69 43 6f 6c 2c 20 72 65 67 4f  pTab, iCol, regO
2ad4e 75 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  ut);..  }..}....
2ad4f 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
2ad50 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
2ad51 78 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75  xtract the iColu
2ad52 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f  mn-th column fro
2ad53 6d 0d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  m..** table pTab
2ad54 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63   and store the c
2ad55 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61  olumn value in a
2ad56 20 72 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65   register.  An e
2ad57 66 66 6f 72 74 0d 0a 2a 2a 20 69 73 20 6d 61 64  ffort..** is mad
2ad58 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  e to store the c
2ad59 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72  olumn value in r
2ad5a 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 62 75  egister iReg, bu
2ad5b 74 20 74 68 69 73 20 69 73 0d 0a 2a 2a 20 6e 6f  t this is..** no
2ad5c 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 54  t guaranteed.  T
2ad5d 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  he location of t
2ad5e 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  he column value 
2ad5f 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
2ad60 0d 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  ..** There must 
2ad61 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f  be an open curso
2ad62 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61  r to pTab in iTa
2ad63 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ble when this ro
2ad64 75 74 69 6e 65 0d 0a 2a 2a 20 69 73 20 63 61 6c  utine..** is cal
2ad65 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e  led.  If iColumn
2ad66 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20  <0 then code is 
2ad67 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 65  generated that e
2ad68 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 69  xtracts the rowi
2ad69 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
2ad6a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2ad6b 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c  e3ExprCodeGetCol
2ad6c 75 6d 6e 28 0d 0a 20 20 50 61 72 73 65 20 2a 70  umn(..  Parse *p
2ad6d 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73  Parse,   /* Pars
2ad6e 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
2ad6f 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2ad70 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  */..  Table *pTa
2ad71 62 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69  b,     /* Descri
2ad72 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62  ption of the tab
2ad73 6c 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e  le we are readin
2ad74 67 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e 74  g from */..  int
2ad75 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a   iColumn,     /*
2ad76 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61   Index of the ta
2ad77 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20  ble column */.. 
2ad78 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
2ad79 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
2ad7a 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
2ad7b 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  table */..  int 
2ad7c 69 52 65 67 20 20 20 20 20 20 20 20 20 2f 2a 20  iReg         /* 
2ad7d 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 68 65  Store results he
2ad7e 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62  re */..){..  Vdb
2ad7f 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2ad80 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d  Vdbe;..  int i;.
2ad81 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
2ad82 63 68 65 20 2a 70 3b 0d 0a 0d 0a 20 20 66 6f 72  che *p;....  for
2ad83 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
2ad84 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
2ad85 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
2ad86 69 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20 20 20 20  i++, p++){..    
2ad87 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 26  if( p->iReg>0 &&
2ad88 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62   p->iTable==iTab
2ad89 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e  le && p->iColumn
2ad8a 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0d 0a 20 20  ==iColumn ){..  
2ad8b 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61      p->lru = pPa
2ad8c 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b  rse->iCacheCnt++
2ad8d 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2ad8e 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 69  ExprCachePinRegi
2ad8f 73 74 65 72 28 70 50 61 72 73 65 2c 20 70 2d 3e  ster(pParse, p->
2ad90 69 52 65 67 29 3b 0d 0a 20 20 20 20 20 20 72 65  iReg);..      re
2ad91 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0d 0a 20  turn p->iReg;.. 
2ad92 20 20 20 7d 0d 0a 20 20 7d 20 20 0d 0a 20 20 61     }..  }  ..  a
2ad93 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0d 0a  ssert( v!=0 );..
2ad94 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2ad95 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 54 61 62 6c  eGetColumnOfTabl
2ad96 65 28 76 2c 20 70 54 61 62 2c 20 69 54 61 62 6c  e(v, pTab, iTabl
2ad97 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67  e, iColumn, iReg
2ad98 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70  );..  sqlite3Exp
2ad99 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
2ad9a 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  se, iTable, iCol
2ad9b 75 6d 6e 2c 20 69 52 65 67 29 3b 0d 0a 20 20 72  umn, iReg);..  r
2ad9c 65 74 75 72 6e 20 69 52 65 67 3b 0d 0a 7d 0d 0a  eturn iReg;..}..
2ad9d 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 65 61 72 20 61  ../*..** Clear a
2ad9e 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  ll column cache 
2ad9f 65 6e 74 72 69 65 73 2e 0d 0a 2a 2f 0d 0a 53 51  entries...*/..SQ
2ada0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2ada1 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
2ada2 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70  heClear(Parse *p
2ada3 50 61 72 73 65 29 7b 0d 0a 20 20 69 6e 74 20 69  Parse){..  int i
2ada4 3b 0d 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  ;..  struct yCol
2ada5 43 61 63 68 65 20 2a 70 3b 0d 0a 0d 0a 20 20 66  Cache *p;....  f
2ada6 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
2ada7 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
2ada8 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
2ada9 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20 20  ; i++, p++){..  
2adaa 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b    if( p->iReg ){
2adab 0d 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74  ..      cacheEnt
2adac 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20  ryClear(pParse, 
2adad 70 29 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 69 52  p);..      p->iR
2adae 65 67 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  eg = 0;..    }..
2adaf 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
2adb0 20 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74   Record the fact
2adb1 20 74 68 61 74 20 61 6e 20 61 66 66 69 6e 69 74   that an affinit
2adb2 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63  y change has occ
2adb3 75 72 72 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0d  urred on iCount.
2adb4 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 73 74  .** registers st
2adb5 61 72 74 69 6e 67 20 77 69 74 68 20 69 53 74 61  arting with iSta
2adb6 72 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  rt...*/..SQLITE_
2adb7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2adb8 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2adb9 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73  inityChange(Pars
2adba 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2adbb 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 6e  Start, int iCoun
2adbc 74 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78  t){..  sqlite3Ex
2adbd 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70 50  prCacheRemove(pP
2adbe 61 72 73 65 2c 20 69 53 74 61 72 74 2c 20 69 43  arse, iStart, iC
2adbf 6f 75 6e 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ount);..}..../*.
2adc0 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2adc1 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e  e to move conten
2adc2 74 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73  t from registers
2adc3 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e   iFrom...iFrom+n
2adc4 52 65 67 2d 31 0d 0a 2a 2a 20 6f 76 65 72 20 74  Reg-1..** over t
2adc5 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d  o iTo..iTo+nReg-
2adc6 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f 6c 75  1. Keep the colu
2adc7 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f 2d 64  mn cache up-to-d
2adc8 61 74 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ate...*/..SQLITE
2adc9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2adca 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
2adcb 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2adcc 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
2adcd 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0d  iTo, int nReg){.
2adce 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 73 74 72  .  int i;..  str
2adcf 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
2add0 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69  ;..  if( NEVER(i
2add1 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74  From==iTo) ) ret
2add2 75 72 6e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  urn;..  sqlite3V
2add3 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65  dbeAddOp3(pParse
2add4 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65  ->pVdbe, OP_Move
2add5 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52  , iFrom, iTo, nR
2add6 65 67 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 2c  eg);..  for(i=0,
2add7 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
2add8 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
2add9 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
2adda 70 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20 78  p++){..    int x
2addb 20 3d 20 70 2d 3e 69 52 65 67 3b 0d 0a 20 20 20   = p->iReg;..   
2addc 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26   if( x>=iFrom &&
2addd 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b   x<iFrom+nReg ){
2adde 0d 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20  ..      p->iReg 
2addf 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0d 0a 20  += iTo-iFrom;.. 
2ade0 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
2ade1 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
2ade2 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f 6e  code to copy con
2ade3 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74  tent from regist
2ade4 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f  ers iFrom...iFro
2ade5 6d 2b 6e 52 65 67 2d 31 0d 0a 2a 2a 20 6f 76 65  m+nReg-1..** ove
2ade6 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52  r to iTo..iTo+nR
2ade7 65 67 2d 31 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  eg-1...*/..SQLIT
2ade8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2ade9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
2adea 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  py(Parse *pParse
2adeb 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
2adec 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b   iTo, int nReg){
2aded 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 66  ..  int i;..  if
2adee 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69  ( NEVER(iFrom==i
2adef 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  To) ) return;.. 
2adf0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67   for(i=0; i<nReg
2adf1 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c  ; i++){..    sql
2adf2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 70  ite3VdbeAddOp2(p
2adf3 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50  Parse->pVdbe, OP
2adf4 5f 43 6f 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20  _Copy, iFrom+i, 
2adf5 69 54 6f 2b 69 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  iTo+i);..  }..}.
2adf6 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  ...#if defined(S
2adf7 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20  QLITE_DEBUG) || 
2adf8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
2adf9 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0d 0a 2f  OVERAGE_TEST)../
2adfa 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  *..** Return tru
2adfb 65 20 69 66 20 61 6e 79 20 72 65 67 69 73 74 65  e if any registe
2adfc 72 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 69  r in the range i
2adfd 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75  From..iTo (inclu
2adfe 73 69 76 65 29 0d 0a 2a 2a 20 69 73 20 75 73 65  sive)..** is use
2adff 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
2ae00 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0d 0a   column cache...
2ae01 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
2ae02 69 6e 65 20 69 73 20 75 73 65 64 20 77 69 74 68  ine is used with
2ae03 69 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20  in assert() and 
2ae04 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f  testcase() macro
2ae05 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 61 6e 64 20 64  s only..** and d
2ae06 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69  oes not appear i
2ae07 6e 20 61 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64  n a normal build
2ae08 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
2ae09 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61  t usedAsColumnCa
2ae0a 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  che(Parse *pPars
2ae0b 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e  e, int iFrom, in
2ae0c 74 20 69 54 6f 29 7b 0d 0a 20 20 69 6e 74 20 69  t iTo){..  int i
2ae0d 3b 0d 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  ;..  struct yCol
2ae0e 43 61 63 68 65 20 2a 70 3b 0d 0a 20 20 66 6f 72  Cache *p;..  for
2ae0f 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
2ae10 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
2ae11 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
2ae12 69 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20 20 20 20  i++, p++){..    
2ae13 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b  int r = p->iReg;
2ae14 0d 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72  ..    if( r>=iFr
2ae15 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72  om && r<=iTo ) r
2ae16 65 74 75 72 6e 20 31 3b 20 20 20 20 2f 2a 4e 4f  eturn 1;    /*NO
2ae17 5f 54 45 53 54 2a 2f 0d 0a 20 20 7d 0d 0a 20 20  _TEST*/..  }..  
2ae18 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 23 65  return 0;..}..#e
2ae19 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
2ae1a 45 42 55 47 20 7c 7c 20 53 51 4c 49 54 45 5f 43  EBUG || SQLITE_C
2ae1b 4f 56 45 52 41 47 45 5f 54 45 53 54 20 2a 2f 0d  OVERAGE_TEST */.
2ae1c 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
2ae1d 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65  te code into the
2ae1e 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f   current Vdbe to
2ae1f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 69   evaluate the gi
2ae20 76 65 6e 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69  ven..** expressi
2ae21 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20  on.  Attempt to 
2ae22 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2ae23 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74  s in register "t
2ae24 61 72 67 65 74 22 2e 0d 0a 2a 2a 20 52 65 74 75  arget"...** Retu
2ae25 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20  rn the register 
2ae26 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72  where results ar
2ae27 65 20 73 74 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a  e stored...**..*
2ae28 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74  * With this rout
2ae29 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ine, there is no
2ae2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
2ae2b 72 65 73 75 6c 74 73 20 77 69 6c 6c 0d 0a 2a 2a  results will..**
2ae2c 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61   be stored in ta
2ae2d 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c  rget.  The resul
2ae2e 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65  t might be store
2ae2f 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0d  d in some other.
2ae30 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20  .** register if 
2ae31 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74  it is convenient
2ae32 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20   to do so.  The 
2ae33 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2ae34 0d 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20  ..** must check 
2ae35 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
2ae36 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73  and move the res
2ae37 75 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 69  ults to the desi
2ae38 72 65 64 0d 0a 2a 2a 20 72 65 67 69 73 74 65 72  red..** register
2ae39 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2ae3a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2ae3b 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
2ae3c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2ae3d 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
2ae3e 74 61 72 67 65 74 29 7b 0d 0a 20 20 56 64 62 65  target){..  Vdbe
2ae3f 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2ae40 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20  dbe;  /* The VM 
2ae41 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
2ae42 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 70 3b  on */..  int op;
2ae43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae44 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65     /* The opcode
2ae45 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0d   being coded */.
2ae46 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74  .  int inReg = t
2ae47 61 72 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20  arget;       /* 
2ae48 52 65 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69  Results stored i
2ae49 6e 20 72 65 67 69 73 74 65 72 20 69 6e 52 65 67  n register inReg
2ae4a 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 46 72   */..  int regFr
2ae4b 65 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ee1 = 0;        
2ae4c 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20   /* If non-zero 
2ae4d 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72  free this tempor
2ae4e 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0d  ary register */.
2ae4f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32 20  .  int regFree2 
2ae50 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
2ae51 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65  If non-zero free
2ae52 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20   this temporary 
2ae53 72 65 67 69 73 74 65 72 20 2a 2f 0d 0a 20 20 69  register */..  i
2ae54 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 72  nt r1, r2, r3, r
2ae55 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69  4;       /* Vari
2ae56 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 6d  ous register num
2ae57 62 65 72 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  bers */..  sqlit
2ae58 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2ae59 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 74 61  >db; /* The data
2ae5a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2ae5b 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
2ae5c 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67  target>0 && targ
2ae5d 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  et<=pParse->nMem
2ae5e 20 29 3b 0d 0a 20 20 69 66 28 20 76 3d 3d 30 20   );..  if( v==0 
2ae5f 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
2ae60 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
2ae61 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20  ocFailed );..   
2ae62 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d   return 0;..  }.
2ae63 0a 0d 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  ...  if( pExpr==
2ae64 30 20 29 7b 0d 0a 20 20 20 20 6f 70 20 3d 20 54  0 ){..    op = T
2ae65 4b 5f 4e 55 4c 4c 3b 0d 0a 20 20 7d 65 6c 73 65  K_NULL;..  }else
2ae66 7b 0d 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70  {..    op = pExp
2ae67 72 2d 3e 6f 70 3b 0d 0a 20 20 7d 0d 0a 20 20 73  r->op;..  }..  s
2ae68 77 69 74 63 68 28 20 6f 70 20 29 7b 0d 0a 20 20  witch( op ){..  
2ae69 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f    case TK_AGG_CO
2ae6a 4c 55 4d 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 41  LUMN: {..      A
2ae6b 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
2ae6c 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e   = pExpr->pAggIn
2ae6d 66 6f 3b 0d 0a 20 20 20 20 20 20 73 74 72 75 63  fo;..      struc
2ae6e 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
2ae6f 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d  Col = &pAggInfo-
2ae70 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67  >aCol[pExpr->iAg
2ae71 67 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21  g];..      if( !
2ae72 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74  pAggInfo->direct
2ae73 4d 6f 64 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  Mode ){..       
2ae74 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69   assert( pCol->i
2ae75 4d 65 6d 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20  Mem>0 );..      
2ae76 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e    inReg = pCol->
2ae77 69 4d 65 6d 3b 0d 0a 20 20 20 20 20 20 20 20 62  iMem;..        b
2ae78 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  reak;..      }el
2ae79 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d  se if( pAggInfo-
2ae7a 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29  >useSortingIdx )
2ae7b 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
2ae7c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2ae7d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49  OP_Column, pAggI
2ae7e 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 50  nfo->sortingIdxP
2ae7f 54 61 62 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  Tab,..          
2ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae81 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
2ae82 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29  rColumn, target)
2ae83 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;..        break
2ae84 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2ae85 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    /* Otherwise, 
2ae86 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74  fall thru into t
2ae87 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73  he TK_COLUMN cas
2ae88 65 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  e */..    }..   
2ae89 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a   case TK_COLUMN:
2ae8a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 45   {..      if( pE
2ae8b 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b  xpr->iTable<0 ){
2ae8c 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  ..        /* Thi
2ae8d 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77  s only happens w
2ae8e 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b  hen coding check
2ae8f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0d   constraints */.
2ae90 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2ae91 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e   pParse->ckBase>
2ae92 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e  0 );..        in
2ae93 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  Reg = pExpr->iCo
2ae94 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63  lumn + pParse->c
2ae95 6b 42 61 73 65 3b 0d 0a 20 20 20 20 20 20 7d 65  kBase;..      }e
2ae96 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  lse{..        in
2ae97 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
2ae98 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70  rCodeGetColumn(p
2ae99 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 54  Parse, pExpr->pT
2ae9a 61 62 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ab,..           
2ae9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae9c 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f        pExpr->iCo
2ae9d 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61  lumn, pExpr->iTa
2ae9e 62 6c 65 2c 20 74 61 72 67 65 74 29 3b 0d 0a 20  ble, target);.. 
2ae9f 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72       }..      br
2aea0 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2aea1 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52   case TK_INTEGER
2aea2 3a 20 7b 0d 0a 20 20 20 20 20 20 63 6f 64 65 49  : {..      codeI
2aea3 6e 74 65 67 65 72 28 70 50 61 72 73 65 2c 20 70  nteger(pParse, p
2aea4 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 29  Expr, 0, target)
2aea5 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
2aea6 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
2aea7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2aea8 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 20 20  TING_POINT..    
2aea9 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b  case TK_FLOAT: {
2aeaa 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2aeab 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2aeac 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
2aead 6c 75 65 29 20 29 3b 0d 0a 20 20 20 20 20 20 63  lue) );..      c
2aeae 6f 64 65 52 65 61 6c 28 76 2c 20 70 45 78 70 72  odeReal(v, pExpr
2aeaf 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 2c 20 74  ->u.zToken, 0, t
2aeb0 61 72 67 65 74 29 3b 0d 0a 20 20 20 20 20 20 62  arget);..      b
2aeb1 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65  reak;..    }..#e
2aeb2 6e 64 69 66 0d 0a 20 20 20 20 63 61 73 65 20 54  ndif..    case T
2aeb3 4b 5f 53 54 52 49 4e 47 3a 20 7b 0d 0a 20 20 20  K_STRING: {..   
2aeb4 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2aeb5 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2aeb6 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
2aeb7 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
2aeb8 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2aeb9 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61  P_String8, 0, ta
2aeba 72 67 65 74 2c 20 30 2c 20 70 45 78 70 72 2d 3e  rget, 0, pExpr->
2aebb 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0d 0a 20  u.zToken, 0);.. 
2aebc 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2aebd 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   }..    case TK_
2aebe 4e 55 4c 4c 3a 20 7b 0d 0a 20 20 20 20 20 20 73  NULL: {..      s
2aebf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2aec0 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2aec1 74 61 72 67 65 74 29 3b 0d 0a 20 20 20 20 20 20  target);..      
2aec2 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23  break;..    }..#
2aec3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2aec4 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0d  IT_BLOB_LITERAL.
2aec5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f  .    case TK_BLO
2aec6 42 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  B: {..      int 
2aec7 6e 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  n;..      const 
2aec8 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 20 20 20 20  char *z;..      
2aec9 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0d 0a 20 20  char *zBlob;..  
2aeca 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2aecb 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
2aecc 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
2aecd 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
2aece 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  t( pExpr->u.zTok
2aecf 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45  en[0]=='x' || pE
2aed0 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d  xpr->u.zToken[0]
2aed1 3d 3d 27 58 27 20 29 3b 0d 0a 20 20 20 20 20 20  =='X' );..      
2aed2 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
2aed3 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27  .zToken[1]=='\''
2aed4 20 29 3b 0d 0a 20 20 20 20 20 20 7a 20 3d 20 26   );..      z = &
2aed5 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
2aed6 32 5d 3b 0d 0a 20 20 20 20 20 20 6e 20 3d 20 73  2];..      n = s
2aed7 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2aed8 29 20 2d 20 31 3b 0d 0a 20 20 20 20 20 20 61 73  ) - 1;..      as
2aed9 73 65 72 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27 27  sert( z[n]=='\''
2aeda 20 29 3b 0d 0a 20 20 20 20 20 20 7a 42 6c 6f 62   );..      zBlob
2aedb 20 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42   = sqlite3HexToB
2aedc 6c 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44  lob(sqlite3VdbeD
2aedd 62 28 76 29 2c 20 7a 2c 20 6e 29 3b 0d 0a 20 20  b(v), z, n);..  
2aede 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aedf 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62  ddOp4(v, OP_Blob
2aee0 2c 20 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30  , n/2, target, 0
2aee1 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41  , zBlob, P4_DYNA
2aee2 4d 49 43 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  MIC);..      bre
2aee3 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  ak;..    }..#end
2aee4 69 66 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  if..    case TK_
2aee5 56 41 52 49 41 42 4c 45 3a 20 7b 0d 0a 20 20 20  VARIABLE: {..   
2aee6 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2aee7 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2aee8 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
2aee9 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
2aeea 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ( pExpr->u.zToke
2aeeb 6e 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 61  n!=0 );..      a
2aeec 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
2aeed 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0d  zToken[0]!=0 );.
2aeee 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2aeef 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56  beAddOp2(v, OP_V
2aef0 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e  ariable, pExpr->
2aef1 69 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29  iColumn, target)
2aef2 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  ;..      if( pEx
2aef3 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21  pr->u.zToken[1]!
2aef4 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61  =0 ){..        a
2aef5 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
2aef6 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 3f 27 20 0d  zToken[0]=='?' .
2aef7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  .             ||
2aef8 20 73 74 72 63 6d 70 28 70 45 78 70 72 2d 3e 75   strcmp(pExpr->u
2aef9 2e 7a 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 2d  .zToken, pParse-
2aefa 3e 61 7a 56 61 72 5b 70 45 78 70 72 2d 3e 69 43  >azVar[pExpr->iC
2aefb 6f 6c 75 6d 6e 2d 31 5d 29 3d 3d 30 20 29 3b 0d  olumn-1])==0 );.
2aefc 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2aefd 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2aefe 2d 31 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56 61  -1, pParse->azVa
2aeff 72 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  r[pExpr->iColumn
2af00 2d 31 5d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  -1], P4_STATIC);
2af01 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
2af02 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2af03 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49      case TK_REGI
2af04 53 54 45 52 3a 20 7b 0d 0a 20 20 20 20 20 20 69  STER: {..      i
2af05 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54  nReg = pExpr->iT
2af06 61 62 6c 65 3b 0d 0a 20 20 20 20 20 20 62 72 65  able;..      bre
2af07 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2af08 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b 0d 0a 20  case TK_AS: {.. 
2af09 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c       inReg = sql
2af0a 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
2af0b 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
2af0c 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
2af0d 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
2af0e 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
2af0f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
2af10 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 41  ..    case TK_CA
2af11 53 54 3a 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  ST: {..      /* 
2af12 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
2af13 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28  he form:   CAST(
2af14 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20  pLeft AS token) 
2af15 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 66  */..      int af
2af16 66 2c 20 74 6f 5f 6f 70 3b 0d 0a 20 20 20 20 20  f, to_op;..     
2af17 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
2af18 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2af19 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2af1a 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0d 0a 20  eft, target);.. 
2af1b 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2af1c 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2af1d 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
2af1e 29 20 29 3b 0d 0a 20 20 20 20 20 20 61 66 66 20  ) );..      aff 
2af1f 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  = sqlite3Affinit
2af20 79 54 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a  yType(pExpr->u.z
2af21 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 74  Token);..      t
2af22 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20 53 51 4c  o_op = aff - SQL
2af23 49 54 45 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f  ITE_AFF_TEXT + O
2af24 50 5f 54 6f 54 65 78 74 3b 0d 0a 20 20 20 20 20  P_ToText;..     
2af25 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d   assert( to_op==
2af26 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c 7c 20  OP_ToText    || 
2af27 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff!=SQLITE_AFF_
2af28 54 45 58 54 20 20 20 20 29 3b 0d 0a 20 20 20 20  TEXT    );..    
2af29 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
2af2a 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c 7c  =OP_ToBlob    ||
2af2b 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
2af2c 5f 4e 4f 4e 45 20 20 20 20 29 3b 0d 0a 20 20 20  _NONE    );..   
2af2d 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
2af2e 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c  ==OP_ToNumeric |
2af2f 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
2af30 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0d 0a 20 20  F_NUMERIC );..  
2af31 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
2af32 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20  p==OP_ToInt     
2af33 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
2af34 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0d 0a 20  FF_INTEGER );.. 
2af35 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
2af36 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20  op==OP_ToReal   
2af37 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
2af38 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0d 0a  AFF_REAL    );..
2af39 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2af3a 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
2af3b 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63   );..      testc
2af3c 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
2af3d 6f 42 6c 6f 62 20 29 3b 0d 0a 20 20 20 20 20 20  oBlob );..      
2af3e 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d  testcase( to_op=
2af3f 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b  =OP_ToNumeric );
2af40 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2af41 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e  ( to_op==OP_ToIn
2af42 74 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  t );..      test
2af43 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
2af44 54 6f 52 65 61 6c 20 29 3b 0d 0a 20 20 20 20 20  ToReal );..     
2af45 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67   if( inReg!=targ
2af46 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  et ){..        s
2af47 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2af48 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e  (v, OP_SCopy, in
2af49 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0d 0a 20  Reg, target);.. 
2af4a 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74         inReg = t
2af4b 61 72 67 65 74 3b 0d 0a 20 20 20 20 20 20 7d 0d  arget;..      }.
2af4c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2af4d 62 65 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f  beAddOp1(v, to_o
2af4e 70 2c 20 69 6e 52 65 67 29 3b 0d 0a 20 20 20 20  p, inReg);..    
2af4f 20 20 74 65 73 74 63 61 73 65 28 20 75 73 65 64    testcase( used
2af50 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50  AsColumnCache(pP
2af51 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52  arse, inReg, inR
2af52 65 67 29 20 29 3b 0d 0a 20 20 20 20 20 20 73 71  eg) );..      sq
2af53 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
2af54 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
2af55 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0d  rse, inReg, 1);.
2af56 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
2af57 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20     }..#endif /* 
2af58 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
2af59 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 54 4b   */..    case TK
2af5a 5f 4c 54 3a 0d 0a 20 20 20 20 63 61 73 65 20 54  _LT:..    case T
2af5b 4b 5f 4c 45 3a 0d 0a 20 20 20 20 63 61 73 65 20  K_LE:..    case 
2af5c 54 4b 5f 47 54 3a 0d 0a 20 20 20 20 63 61 73 65  TK_GT:..    case
2af5d 20 54 4b 5f 47 45 3a 0d 0a 20 20 20 20 63 61 73   TK_GE:..    cas
2af5e 65 20 54 4b 5f 4e 45 3a 0d 0a 20 20 20 20 63 61  e TK_NE:..    ca
2af5f 73 65 20 54 4b 5f 45 51 3a 20 7b 0d 0a 20 20 20  se TK_EQ: {..   
2af60 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
2af61 3d 3d 4f 50 5f 4c 74 20 29 3b 0d 0a 20 20 20 20  ==OP_Lt );..    
2af62 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d    assert( TK_LE=
2af63 3d 4f 50 5f 4c 65 20 29 3b 0d 0a 20 20 20 20 20  =OP_Le );..     
2af64 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d   assert( TK_GT==
2af65 4f 50 5f 47 74 20 29 3b 0d 0a 20 20 20 20 20 20  OP_Gt );..      
2af66 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f  assert( TK_GE==O
2af67 50 5f 47 65 20 29 3b 0d 0a 20 20 20 20 20 20 61  P_Ge );..      a
2af68 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50  ssert( TK_EQ==OP
2af69 5f 45 71 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  _Eq );..      as
2af6a 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f  sert( TK_NE==OP_
2af6b 4e 65 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  Ne );..      tes
2af6c 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54  tcase( op==TK_LT
2af6d 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63   );..      testc
2af6e 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29  ase( op==TK_LE )
2af6f 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
2af70 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0d  e( op==TK_GT );.
2af71 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2af72 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0d 0a 20   op==TK_GE );.. 
2af73 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2af74 70 3d 3d 54 4b 5f 45 51 20 29 3b 0d 0a 20 20 20  p==TK_EQ );..   
2af75 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2af76 3d 54 4b 5f 4e 45 20 29 3b 0d 0a 20 20 20 20 20  =TK_NE );..     
2af77 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
2af78 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
2af79 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2af7a 26 72 65 67 46 72 65 65 31 29 3b 0d 0a 20 20 20  &regFree1);..   
2af7b 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45     r2 = sqlite3E
2af7c 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2af7d 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
2af7e 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0d 0a  t, &regFree2);..
2af7f 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
2af80 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
2af81 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70  >pLeft, pExpr->p
2af82 52 69 67 68 74 2c 20 6f 70 2c 0d 0a 20 20 20 20  Right, op,..    
2af83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
2af84 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c  , r2, inReg, SQL
2af85 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0d 0a 20  ITE_STOREP2);.. 
2af86 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2af87 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0d 0a 20  egFree1==0 );.. 
2af88 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2af89 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0d 0a 20  egFree2==0 );.. 
2af8a 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2af8b 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   }..    case TK_
2af8c 49 53 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b  IS:..    case TK
2af8d 5f 49 53 4e 4f 54 3a 20 7b 0d 0a 20 20 20 20 20  _ISNOT: {..     
2af8e 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2af8f 4b 5f 49 53 20 29 3b 0d 0a 20 20 20 20 20 20 74  K_IS );..      t
2af90 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2af91 49 53 4e 4f 54 20 29 3b 0d 0a 20 20 20 20 20 20  ISNOT );..      
2af92 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2af93 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2af94 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
2af95 72 65 67 46 72 65 65 31 29 3b 0d 0a 20 20 20 20  regFree1);..    
2af96 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
2af97 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2af98 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
2af99 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0d 0a 20  , &regFree2);.. 
2af9a 20 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54       op = (op==T
2af9b 4b 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20  K_IS) ? TK_EQ : 
2af9c 54 4b 5f 4e 45 3b 0d 0a 20 20 20 20 20 20 63 6f  TK_NE;..      co
2af9d 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
2af9e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2af9f 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
2afa0 70 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  p,..            
2afa1 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e        r1, r2, in
2afa2 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52  Reg, SQLITE_STOR
2afa3 45 50 32 20 7c 20 53 51 4c 49 54 45 5f 4e 55 4c  EP2 | SQLITE_NUL
2afa4 4c 45 51 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  LEQ);..      tes
2afa5 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
2afa6 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  =0 );..      tes
2afa7 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
2afa8 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  =0 );..      bre
2afa9 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2afaa 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0d 0a 20 20  case TK_AND:..  
2afab 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0d 0a 20    case TK_OR:.. 
2afac 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a     case TK_PLUS:
2afad 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54  ..    case TK_ST
2afae 41 52 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b  AR:..    case TK
2afaf 5f 4d 49 4e 55 53 3a 0d 0a 20 20 20 20 63 61 73  _MINUS:..    cas
2afb0 65 20 54 4b 5f 52 45 4d 3a 0d 0a 20 20 20 20 63  e TK_REM:..    c
2afb1 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0d 0a  ase TK_BITAND:..
2afb2 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f      case TK_BITO
2afb3 52 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  R:..    case TK_
2afb4 53 4c 41 53 48 3a 0d 0a 20 20 20 20 63 61 73 65  SLASH:..    case
2afb5 20 54 4b 5f 4c 53 48 49 46 54 3a 0d 0a 20 20 20   TK_LSHIFT:..   
2afb6 20 63 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a   case TK_RSHIFT:
2afb7 20 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   ..    case TK_C
2afb8 4f 4e 43 41 54 3a 20 7b 0d 0a 20 20 20 20 20 20  ONCAT: {..      
2afb9 61 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d  assert( TK_AND==
2afba 4f 50 5f 41 6e 64 20 29 3b 0d 0a 20 20 20 20 20  OP_And );..     
2afbb 20 61 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d   assert( TK_OR==
2afbc 4f 50 5f 4f 72 20 29 3b 0d 0a 20 20 20 20 20 20  OP_Or );..      
2afbd 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d  assert( TK_PLUS=
2afbe 3d 4f 50 5f 41 64 64 20 29 3b 0d 0a 20 20 20 20  =OP_Add );..    
2afbf 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 4e    assert( TK_MIN
2afc0 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 20  US==OP_Subtract 
2afc1 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
2afc2 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
2afc3 61 69 6e 64 65 72 20 29 3b 0d 0a 20 20 20 20 20  ainder );..     
2afc4 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41   assert( TK_BITA
2afc5 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b  ND==OP_BitAnd );
2afc6 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2afc7 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74  TK_BITOR==OP_Bit
2afc8 4f 72 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73  Or );..      ass
2afc9 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f  ert( TK_SLASH==O
2afca 50 5f 44 69 76 69 64 65 20 29 3b 0d 0a 20 20 20  P_Divide );..   
2afcb 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 53     assert( TK_LS
2afcc 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c 65  HIFT==OP_ShiftLe
2afcd 66 74 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73  ft );..      ass
2afce 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d  ert( TK_RSHIFT==
2afcf 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b  OP_ShiftRight );
2afd0 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2afd1 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f  TK_CONCAT==OP_Co
2afd2 6e 63 61 74 20 29 3b 0d 0a 20 20 20 20 20 20 74  ncat );..      t
2afd3 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2afd4 41 4e 44 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  AND );..      te
2afd5 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f  stcase( op==TK_O
2afd6 52 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  R );..      test
2afd7 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55  case( op==TK_PLU
2afd8 53 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  S );..      test
2afd9 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e  case( op==TK_MIN
2afda 55 53 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  US );..      tes
2afdb 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45  tcase( op==TK_RE
2afdc 4d 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  M );..      test
2afdd 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54  case( op==TK_BIT
2afde 41 4e 44 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  AND );..      te
2afdf 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42  stcase( op==TK_B
2afe0 49 54 4f 52 20 29 3b 0d 0a 20 20 20 20 20 20 74  ITOR );..      t
2afe1 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2afe2 53 4c 41 53 48 20 29 3b 0d 0a 20 20 20 20 20 20  SLASH );..      
2afe3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
2afe4 5f 4c 53 48 49 46 54 20 29 3b 0d 0a 20 20 20 20  _LSHIFT );..    
2afe5 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2afe6 54 4b 5f 52 53 48 49 46 54 20 29 3b 0d 0a 20 20  TK_RSHIFT );..  
2afe7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2afe8 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0d 0a  ==TK_CONCAT );..
2afe9 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2afea 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
2afeb 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2afec 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b  eft, &regFree1);
2afed 0d 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c  ..      r2 = sql
2afee 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
2afef 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2aff0 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65  pRight, &regFree
2aff1 32 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  2);..      sqlit
2aff2 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2aff3 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67  op, r2, r1, targ
2aff4 65 74 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  et);..      test
2aff5 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
2aff6 30 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  0 );..      test
2aff7 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
2aff8 30 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  0 );..      brea
2aff9 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  k;..    }..    c
2affa 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b  ase TK_UMINUS: {
2affb 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c  ..      Expr *pL
2affc 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
2affd 66 74 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  ft;..      asser
2affe 74 28 20 70 4c 65 66 74 20 29 3b 0d 0a 20 20 20  t( pLeft );..   
2afff 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70     if( pLeft->op
2b000 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0d  ==TK_INTEGER ){.
2b001 0a 20 20 20 20 20 20 20 20 63 6f 64 65 49 6e 74  .        codeInt
2b002 65 67 65 72 28 70 50 61 72 73 65 2c 20 70 4c 65  eger(pParse, pLe
2b003 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0d  ft, 1, target);.
2b004 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2b005 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
2b006 49 4e 54 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  INT..      }else
2b007 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d   if( pLeft->op==
2b008 54 4b 5f 46 4c 4f 41 54 20 29 7b 0d 0a 20 20 20  TK_FLOAT ){..   
2b009 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2b00a 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2b00b 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
2b00c 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 63 6f  ) );..        co
2b00d 64 65 52 65 61 6c 28 76 2c 20 70 4c 65 66 74 2d  deReal(v, pLeft-
2b00e 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 31 2c 20 74 61  >u.zToken, 1, ta
2b00f 72 67 65 74 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  rget);..#endif..
2b010 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2b011 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 3d        regFree1 =
2b012 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2b013 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2b014 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2b015 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b016 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31  P_Integer, 0, r1
2b017 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 32 20 3d  );..        r2 =
2b018 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2b019 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
2b01a 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
2b01b 72 65 65 32 29 3b 0d 0a 20 20 20 20 20 20 20 20  ree2);..        
2b01c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b01d 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74  3(v, OP_Subtract
2b01e 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74  , r2, r1, target
2b01f 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 65 73 74  );..        test
2b020 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
2b021 30 20 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  0 );..      }.. 
2b022 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72       inReg = tar
2b023 67 65 74 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  get;..      brea
2b024 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  k;..    }..    c
2b025 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 0d 0a  ase TK_BITNOT:..
2b026 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a      case TK_NOT:
2b027 20 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74   {..      assert
2b028 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f  ( TK_BITNOT==OP_
2b029 42 69 74 4e 6f 74 20 29 3b 0d 0a 20 20 20 20 20  BitNot );..     
2b02a 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d   assert( TK_NOT=
2b02b 3d 4f 50 5f 4e 6f 74 20 29 3b 0d 0a 20 20 20 20  =OP_Not );..    
2b02c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2b02d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0d 0a 20 20  TK_BITNOT );..  
2b02e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2b02f 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0d 0a 20 20 20  ==TK_NOT );..   
2b030 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2b031 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2b032 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2b033 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0d 0a 20  , &regFree1);.. 
2b034 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2b035 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0d 0a 20  egFree1==0 );.. 
2b036 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72       inReg = tar
2b037 67 65 74 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  get;..      sqli
2b038 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b039 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b   op, r1, inReg);
2b03a 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2b03b 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
2b03c 54 4b 5f 49 53 4e 55 4c 4c 3a 0d 0a 20 20 20 20  TK_ISNULL:..    
2b03d 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a  case TK_NOTNULL:
2b03e 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 64   {..      int ad
2b03f 64 72 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  dr;..      asser
2b040 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50  t( TK_ISNULL==OP
2b041 5f 49 73 4e 75 6c 6c 20 29 3b 0d 0a 20 20 20 20  _IsNull );..    
2b042 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54    assert( TK_NOT
2b043 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c  NULL==OP_NotNull
2b044 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63   );..      testc
2b045 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
2b046 4c 4c 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  LL );..      tes
2b047 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
2b048 54 4e 55 4c 4c 20 29 3b 0d 0a 20 20 20 20 20 20  TNULL );..      
2b049 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b04a 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b04b 20 31 2c 20 74 61 72 67 65 74 29 3b 0d 0a 20 20   1, target);..  
2b04c 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
2b04d 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
2b04e 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2b04f 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0d 0a  t, &regFree1);..
2b050 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b051 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0d 0a  regFree1==0 );..
2b052 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2b053 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2b054 2c 20 6f 70 2c 20 72 31 29 3b 0d 0a 20 20 20 20  , op, r1);..    
2b055 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b056 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
2b057 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b 0d 0a  , target, -1);..
2b058 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b059 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2b05a 72 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  r);..      break
2b05b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2b05c 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
2b05d 4f 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 41 67 67  ON: {..      Agg
2b05e 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45  Info *pInfo = pE
2b05f 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0d 0a  xpr->pAggInfo;..
2b060 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d        if( pInfo=
2b061 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61  =0 ){..        a
2b062 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2b063 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2b064 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0d 0a  P_IntValue) );..
2b065 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2b066 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2b067 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 72 65  "misuse of aggre
2b068 67 61 74 65 3a 20 25 73 28 29 22 2c 20 70 45 78  gate: %s()", pEx
2b069 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a  pr->u.zToken);..
2b06a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2b06b 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49        inReg = pI
2b06c 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72  nfo->aFunc[pExpr
2b06d 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0d 0a 20  ->iAgg].iMem;.. 
2b06e 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72       }..      br
2b06f 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2b070 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46   case TK_CONST_F
2b071 55 4e 43 3a 0d 0a 20 20 20 20 63 61 73 65 20 54  UNC:..    case T
2b072 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0d 0a 20  K_FUNCTION: {.. 
2b073 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2b074 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c  Farg;       /* L
2b075 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  ist of function 
2b076 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20  arguments */..  
2b077 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20      int nFarg;  
2b078 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2b079 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e  mber of function
2b07a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20   arguments */.. 
2b07b 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44       FuncDef *pD
2b07c 65 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ef;         /* T
2b07d 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  he function defi
2b07e 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f  nition object */
2b07f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b  ..      int nId;
2b080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b081 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
2b082 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e  function name in
2b083 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20 20 20 20   bytes */..     
2b084 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
2b085 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  ;       /* The f
2b086 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0d  unction name */.
2b087 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  .      int const
2b088 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Mask = 0;     /*
2b089 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f   Mask of functio
2b08a 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  n arguments that
2b08b 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f   are constant */
2b08c 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20  ..      int i;  
2b08d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b08e 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
2b08f 2f 0d 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20  /..      u8 enc 
2b090 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20  = ENC(db);      
2b091 2f 2a 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f  /* The text enco
2b092 64 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69  ding used by thi
2b093 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20  s database */.. 
2b094 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
2b095 6f 6c 6c 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41  oll = 0;    /* A
2b096 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2b097 6e 63 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20  nce */....      
2b098 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2b099 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2b09a 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2b09b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2b09c 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46  ( op==TK_CONST_F
2b09d 55 4e 43 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  UNC );..      te
2b09e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46  stcase( op==TK_F
2b09f 55 4e 43 54 49 4f 4e 20 29 3b 0d 0a 20 20 20 20  UNCTION );..    
2b0a0 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79    if( ExprHasAny
2b0a1 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2b0a2 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b  EP_TokenOnly) ){
2b0a3 0d 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20  ..        pFarg 
2b0a4 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  = 0;..      }els
2b0a5 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 46 61 72  e{..        pFar
2b0a6 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  g = pExpr->x.pLi
2b0a7 73 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  st;..      }..  
2b0a8 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72      nFarg = pFar
2b0a9 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72  g ? pFarg->nExpr
2b0aa 20 3a 20 30 3b 0d 0a 20 20 20 20 20 20 61 73 73   : 0;..      ass
2b0ab 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
2b0ac 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2b0ad 49 6e 74 56 61 6c 75 65 29 20 29 3b 0d 0a 20 20  IntValue) );..  
2b0ae 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d      zId = pExpr-
2b0af 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20  >u.zToken;..    
2b0b0 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53    nId = sqlite3S
2b0b1 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0d 0a 20  trlen30(zId);.. 
2b0b2 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69       pDef = sqli
2b0b3 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
2b0b4 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46  db, zId, nId, nF
2b0b5 61 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0d 0a 20  arg, enc, 0);.. 
2b0b6 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30       if( pDef==0
2b0b7 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
2b0b8 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2b0b9 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 66 75  rse, "unknown fu
2b0ba 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c  nction: %.*s()",
2b0bb 20 6e 49 64 2c 20 7a 49 64 29 3b 0d 0a 20 20 20   nId, zId);..   
2b0bc 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2b0bd 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
2b0be 20 41 74 74 65 6d 70 74 20 61 20 64 69 72 65 63   Attempt a direc
2b0bf 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
2b0c0 20 6f 66 20 74 68 65 20 62 75 69 6c 74 2d 69 6e   of the built-in
2b0c1 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 0d   COALESCE() and.
2b0c2 0a 20 20 20 20 20 20 2a 2a 20 49 46 4e 55 4c 4c  .      ** IFNULL
2b0c3 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  () functions.  T
2b0c4 68 69 73 20 61 76 6f 69 64 73 20 75 6e 6e 65 63  his avoids unnec
2b0c5 65 73 73 61 72 79 20 65 76 61 6c 61 74 69 6f 6e  essary evalation
2b0c6 20 6f 66 0d 0a 20 20 20 20 20 20 2a 2a 20 61 72   of..      ** ar
2b0c7 67 75 6d 65 6e 74 73 20 70 61 73 74 20 74 68 65  guments past the
2b0c8 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 20   first non-NULL 
2b0c9 61 72 67 75 6d 65 6e 74 2e 0d 0a 20 20 20 20 20  argument...     
2b0ca 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70   */..      if( p
2b0cb 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
2b0cc 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43  ITE_FUNC_COALESC
2b0cd 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  E ){..        in
2b0ce 74 20 65 6e 64 43 6f 61 6c 65 73 63 65 20 3d 20  t endCoalesce = 
2b0cf 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2b0d0 61 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20 20  abel(v);..      
2b0d1 20 20 61 73 73 65 72 74 28 20 6e 46 61 72 67 3e    assert( nFarg>
2b0d2 3d 32 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  =2 );..        s
2b0d3 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2b0d4 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b  Parse, pFarg->a[
2b0d5 30 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65 74  0].pExpr, target
2b0d6 29 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28  );..        for(
2b0d7 69 3d 31 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b  i=1; i<nFarg; i+
2b0d8 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  +){..          s
2b0d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b0da 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
2b0db 74 61 72 67 65 74 2c 20 65 6e 64 43 6f 61 6c 65  target, endCoale
2b0dc 73 63 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  sce);..         
2b0dd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2b0de 65 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20  eRemove(pParse, 
2b0df 74 61 72 67 65 74 2c 20 31 29 3b 0d 0a 20 20 20  target, 1);..   
2b0e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2b0e1 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72  prCachePush(pPar
2b0e2 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  se);..          
2b0e3 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2b0e4 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61  pParse, pFarg->a
2b0e5 5b 69 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65  [i].pExpr, targe
2b0e6 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  t);..          s
2b0e7 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
2b0e8 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0d 0a  op(pParse, 1);..
2b0e9 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
2b0ea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2b0eb 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
2b0ec 64 43 6f 61 6c 65 73 63 65 29 3b 0d 0a 20 20 20  dCoalesce);..   
2b0ed 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2b0ee 20 20 20 7d 0d 0a 0d 0a 0d 0a 20 20 20 20 20 20     }......      
2b0ef 69 66 28 20 70 46 61 72 67 20 29 7b 0d 0a 20 20  if( pFarg ){..  
2b0f0 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2b0f1 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2b0f2 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0d 0a  Parse, nFarg);..
2b0f3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2b0f4 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
2b0f5 72 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63  rse);     /* Tic
2b0f6 6b 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65  ket 2ea2425d34be
2b0f7 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   */..        sql
2b0f8 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2b0f9 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 46 61  List(pParse, pFa
2b0fa 72 67 2c 20 72 31 2c 20 31 29 3b 0d 0a 20 20 20  rg, r1, 1);..   
2b0fb 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b0fc 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
2b0fd 20 31 29 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74   1);   /* Ticket
2b0fe 20 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f   2ea2425d34be */
2b0ff 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
2b100 20 20 20 20 20 20 20 20 72 31 20 3d 20 30 3b 0d          r1 = 0;.
2b101 0a 20 20 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65  .      }..#ifnde
2b102 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2b103 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 20 20  RTUALTABLE..    
2b104 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76    /* Possibly ov
2b105 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74  erload the funct
2b106 69 6f 6e 20 69 66 20 74 68 65 20 66 69 72 73 74  ion if the first
2b107 20 61 72 67 75 6d 65 6e 74 20 69 73 0d 0a 20 20   argument is..  
2b108 20 20 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c      ** a virtual
2b109 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0d 0a   table column...
2b10a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
2b10b 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75 6e  ** For infix fun
2b10c 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47 4c  ctions (LIKE, GL
2b10d 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64 20  OB, REGEXP, and 
2b10e 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 0d 0a  MATCH) use the..
2b10f 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20        ** second 
2b110 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68  argument, not th
2b111 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 20  e first, as the 
2b112 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74  argument to test
2b113 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 73 65   to..      ** se
2b114 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c  e if it is a col
2b115 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
2b116 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
2b117 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0d 0a 20   done because.. 
2b118 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74       ** the left
2b119 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69   operand of infi
2b11a 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65  x functions (the
2b11b 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74   operand we want
2b11c 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f   to..      ** co
2b11d 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e  ntrol overloadin
2b11e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74 68  g) ends up as th
2b11f 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2b120 74 20 74 6f 20 74 68 65 0d 0a 20 20 20 20 20 20  t to the..      
2b121 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  ** function.  Th
2b122 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20  e expression "A 
2b123 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76  glob B" is equiv
2b124 61 6c 65 6e 74 20 74 6f 20 0d 0a 20 20 20 20 20  alent to ..     
2b125 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20   ** "glob(B,A). 
2b126 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20   We want to use 
2b127 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62  the A in "A glob
2b128 20 42 22 20 74 6f 20 74 65 73 74 0d 0a 20 20 20   B" to test..   
2b129 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69     ** for functi
2b12a 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20  on overloading. 
2b12b 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20   But we use the 
2b12c 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28  B term in "glob(
2b12d 42 2c 41 29 22 2e 0d 0a 20 20 20 20 20 20 2a 2f  B,A)"...      */
2b12e 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 72  ..      if( nFar
2b12f 67 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e  g>=2 && (pExpr->
2b130 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78  flags & EP_Infix
2b131 46 75 6e 63 29 20 29 7b 0d 0a 20 20 20 20 20 20  Func) ){..      
2b132 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
2b133 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63  VtabOverloadFunc
2b134 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e  tion(db, pDef, n
2b135 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b 31  Farg, pFarg->a[1
2b136 5d 2e 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 20  ].pExpr);..     
2b137 20 7d 65 6c 73 65 20 69 66 28 20 6e 46 61 72 67   }else if( nFarg
2b138 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  >0 ){..        p
2b139 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61  Def = sqlite3Vta
2b13a 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f  bOverloadFunctio
2b13b 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72  n(db, pDef, nFar
2b13c 67 2c 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70  g, pFarg->a[0].p
2b13d 45 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  Expr);..      }.
2b13e 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 66  .#endif..      f
2b13f 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72 67 3b  or(i=0; i<nFarg;
2b140 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20   i++){..        
2b141 69 66 28 20 69 3c 33 32 20 26 26 20 73 71 6c 69  if( i<32 && sqli
2b142 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
2b143 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45  t(pFarg->a[i].pE
2b144 78 70 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  xpr) ){..       
2b145 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20     constMask |= 
2b146 28 31 3c 3c 69 29 3b 0d 0a 20 20 20 20 20 20 20  (1<<i);..       
2b147 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
2b148 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
2b149 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
2b14a 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c  OLL)!=0 && !pCol
2b14b 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  l ){..          
2b14c 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
2b14d 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
2b14e 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70  e, pFarg->a[i].p
2b14f 45 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 20 20  Expr);..        
2b150 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
2b151 20 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67    if( pDef->flag
2b152 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
2b153 4e 45 45 44 43 4f 4c 4c 20 29 7b 0d 0a 20 20 20  NEEDCOLL ){..   
2b154 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20       if( !pColl 
2b155 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44  ) pColl = db->pD
2b156 66 6c 74 43 6f 6c 6c 3b 20 0d 0a 20 20 20 20 20  fltColl; ..     
2b157 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b158 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53  dOp4(v, OP_CollS
2b159 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68  eq, 0, 0, 0, (ch
2b15a 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43  ar *)pColl, P4_C
2b15b 4f 4c 4c 53 45 51 29 3b 0d 0a 20 20 20 20 20 20  OLLSEQ);..      
2b15c 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
2b15d 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2b15e 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74  _Function, const
2b15f 4d 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74  Mask, r1, target
2b160 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
2b161 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2b162 2a 29 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44  *)pDef, P4_FUNCD
2b163 45 46 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  EF);..      sqli
2b164 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2b165 76 2c 20 28 75 38 29 6e 46 61 72 67 29 3b 0d 0a  v, (u8)nFarg);..
2b166 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 20        if( nFarg 
2b167 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
2b168 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2b169 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20  nge(pParse, r1, 
2b16a 6e 46 61 72 67 29 3b 0d 0a 20 20 20 20 20 20 7d  nFarg);..      }
2b16b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2b16c 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
2b16d 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2b16e 45 52 59 0d 0a 20 20 20 20 63 61 73 65 20 54 4b  ERY..    case TK
2b16f 5f 45 58 49 53 54 53 3a 0d 0a 20 20 20 20 63 61  _EXISTS:..    ca
2b170 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0d  se TK_SELECT: {.
2b171 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b172 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29   op==TK_EXISTS )
2b173 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
2b174 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  e( op==TK_SELECT
2b175 20 29 3b 0d 0a 20 20 20 20 20 20 69 6e 52 65 67   );..      inReg
2b176 20 3d 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75   = sqlite3CodeSu
2b177 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  bselect(pParse, 
2b178 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0d 0a 20  pExpr, 0, 0);.. 
2b179 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2b17a 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   }..    case TK_
2b17b 49 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74  IN: {..      int
2b17c 20 64 65 73 74 49 66 46 61 6c 73 65 20 3d 20 73   destIfFalse = s
2b17d 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2b17e 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20 20 69  bel(v);..      i
2b17f 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20  nt destIfNull = 
2b180 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2b181 61 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20 20  abel(v);..      
2b182 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b183 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2b184 20 74 61 72 67 65 74 29 3b 0d 0a 20 20 20 20 20   target);..     
2b185 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2b186 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  IN(pParse, pExpr
2b187 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64  , destIfFalse, d
2b188 65 73 74 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20  estIfNull);..   
2b189 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b18a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2b18b 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0d  er, 1, target);.
2b18c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b18d 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2b18e 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0d  , destIfFalse);.
2b18f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b190 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2b191 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 30  ddImm, target, 0
2b192 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
2b193 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2b194 6c 28 76 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29  l(v, destIfNull)
2b195 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
2b196 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f  .    }..#endif /
2b197 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  * SQLITE_OMIT_SU
2b198 42 51 55 45 52 59 20 2a 2f 0d 0a 0d 0a 0d 0a 20  BQUERY */...... 
2b199 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a 20 20 20     /*..    **   
2b19a 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44   x BETWEEN y AND
2b19b 20 7a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20   z..    **..    
2b19c 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 76  ** This is equiv
2b19d 61 6c 65 6e 74 20 74 6f 0d 0a 20 20 20 20 2a 2a  alent to..    **
2b19e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79  ..    **    x>=y
2b19f 20 41 4e 44 20 78 3c 3d 7a 0d 0a 20 20 20 20 2a   AND x<=z..    *
2b1a0 2a 0d 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73  *..    ** X is s
2b1a1 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e  tored in pExpr->
2b1a2 70 4c 65 66 74 2e 0d 0a 20 20 20 20 2a 2a 20 59  pLeft...    ** Y
2b1a3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45   is stored in pE
2b1a4 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d  xpr->pList->a[0]
2b1a5 2e 70 45 78 70 72 2e 0d 0a 20 20 20 20 2a 2a 20  .pExpr...    ** 
2b1a6 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  Z is stored in p
2b1a7 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31  Expr->pList->a[1
2b1a8 5d 2e 70 45 78 70 72 2e 0d 0a 20 20 20 20 2a 2f  ].pExpr...    */
2b1a9 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  ..    case TK_BE
2b1aa 54 57 45 45 4e 3a 20 7b 0d 0a 20 20 20 20 20 20  TWEEN: {..      
2b1ab 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
2b1ac 78 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20 20  xpr->pLeft;..   
2b1ad 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2b1ae 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20  st_item *pLItem 
2b1af 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2b1b0 2d 3e 61 3b 0d 0a 20 20 20 20 20 20 45 78 70 72  ->a;..      Expr
2b1b1 20 2a 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65   *pRight = pLIte
2b1b2 6d 2d 3e 70 45 78 70 72 3b 0d 0a 0d 0a 20 20 20  m->pExpr;....   
2b1b3 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2b1b4 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2b1b5 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 65 67 46  se, pLeft, &regF
2b1b6 72 65 65 31 29 3b 0d 0a 20 20 20 20 20 20 72 32  ree1);..      r2
2b1b7 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2b1b8 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
2b1b9 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 32  Right, &regFree2
2b1ba 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
2b1bb 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
2b1bc 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
2b1bd 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
2b1be 29 3b 0d 0a 20 20 20 20 20 20 72 33 20 3d 20 73  );..      r3 = s
2b1bf 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2b1c0 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
2b1c1 20 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r4 = sqlite3Get
2b1c2 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2b1c3 0d 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ..      codeComp
2b1c4 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66  are(pParse, pLef
2b1c5 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65  t, pRight, OP_Ge
2b1c6 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
2b1c7 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72 33 2c       r1, r2, r3,
2b1c8 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29   SQLITE_STOREP2)
2b1c9 3b 0d 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2b  ;..      pLItem+
2b1ca 2b 3b 0d 0a 20 20 20 20 20 20 70 52 69 67 68 74  +;..      pRight
2b1cb 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72   = pLItem->pExpr
2b1cc 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2b1cd 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2b1ce 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29  Parse, regFree2)
2b1cf 3b 0d 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71  ;..      r2 = sq
2b1d0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
2b1d1 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  p(pParse, pRight
2b1d2 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0d 0a 20  , &regFree2);.. 
2b1d3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2b1d4 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0d 0a 20  egFree2==0 );.. 
2b1d5 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
2b1d6 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20  (pParse, pLeft, 
2b1d7 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72  pRight, OP_Le, r
2b1d8 31 2c 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54  1, r2, r4, SQLIT
2b1d9 45 5f 53 54 4f 52 45 50 32 29 3b 0d 0a 20 20 20  E_STOREP2);..   
2b1da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b1db 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20  dOp3(v, OP_And, 
2b1dc 72 33 2c 20 72 34 2c 20 74 61 72 67 65 74 29 3b  r3, r4, target);
2b1dd 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ..      sqlite3R
2b1de 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2b1df 61 72 73 65 2c 20 72 33 29 3b 0d 0a 20 20 20 20  arse, r3);..    
2b1e0 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2b1e1 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2b1e2 72 34 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  r4);..      brea
2b1e3 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  k;..    }..    c
2b1e4 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0d  ase TK_UPLUS: {.
2b1e5 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73  .      inReg = s
2b1e6 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
2b1e7 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
2b1e8 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
2b1e9 74 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  t);..      break
2b1ea 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
2b1eb 63 61 73 65 20 54 4b 5f 54 52 49 47 47 45 52 3a  case TK_TRIGGER:
2b1ec 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   {..      /* If 
2b1ed 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
2b1ee 5f 54 52 49 47 47 45 52 2c 20 74 68 65 6e 20 74  _TRIGGER, then t
2b1ef 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2b1f0 20 61 20 72 65 66 65 72 65 6e 63 65 0d 0a 20 20   a reference..  
2b1f1 20 20 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c 75      ** to a colu
2b1f2 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20  mn in the new.* 
2b1f3 6f 72 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d  or old.* pseudo-
2b1f4 74 61 62 6c 65 73 20 61 76 61 69 6c 61 62 6c 65  tables available
2b1f5 20 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 74 72   to..      ** tr
2b1f6 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20  igger programs. 
2b1f7 49 6e 20 74 68 69 73 20 63 61 73 65 20 45 78 70  In this case Exp
2b1f8 72 2e 69 54 61 62 6c 65 20 69 73 20 73 65 74 20  r.iTable is set 
2b1f9 74 6f 20 31 20 66 6f 72 20 74 68 65 0d 0a 20 20  to 1 for the..  
2b1fa 20 20 20 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65      ** new.* pse
2b1fb 75 64 6f 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20  udo-table, or 0 
2b1fc 66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73  for the old.* ps
2b1fd 65 75 64 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72  eudo-table. Expr
2b1fe 2e 69 43 6f 6c 75 6d 6e 0d 0a 20 20 20 20 20 20  .iColumn..      
2b1ff 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
2b200 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70   column of the p
2b201 73 65 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20 72  seudo-table to r
2b202 65 61 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74 6f  ead, or to -1 to
2b203 0d 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20  ..      ** read 
2b204 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 2e  the rowid field.
2b205 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20  ..      **..    
2b206 20 20 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73    ** The express
2b207 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ion is implement
2b208 65 64 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f 50  ed using an OP_P
2b209 61 72 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68 65  aram opcode. The
2b20a 20 70 31 0d 0a 20 20 20 20 20 20 2a 2a 20 70 61   p1..      ** pa
2b20b 72 61 6d 65 74 65 72 20 69 73 20 73 65 74 20 74  rameter is set t
2b20c 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72  o 0 for an old.r
2b20d 6f 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20  owid reference, 
2b20e 6f 72 20 74 6f 20 28 69 2b 31 29 0d 0a 20 20 20  or to (i+1)..   
2b20f 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 72 65 6e     ** to referen
2b210 63 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c 75 6d  ce another colum
2b211 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 70  n of the old.* p
2b212 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77 68 65  seudo-table, whe
2b213 72 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 69 20  re ..      ** i 
2b214 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
2b215 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 20  the column. For 
2b216 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 66 65  a new.rowid refe
2b217 72 65 6e 63 65 2c 20 70 31 20 69 73 0d 0a 20 20  rence, p1 is..  
2b218 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e      ** set to (n
2b219 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 20  +1), where n is 
2b21a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2b21b 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 73  lumns in each ps
2b21c 65 75 64 6f 2d 74 61 62 6c 65 2e 0d 0a 20 20 20  eudo-table...   
2b21d 20 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65     ** For a refe
2b21e 72 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68  rence to any oth
2b21f 65 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  er column in the
2b220 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61   new.* pseudo-ta
2b221 62 6c 65 2c 20 70 31 0d 0a 20 20 20 20 20 20 2a  ble, p1..      *
2b222 2a 20 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32  * is set to (n+2
2b223 2b 69 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64  +i), where n and
2b224 20 69 20 61 72 65 20 61 73 20 64 65 66 69 6e 65   i are as define
2b225 64 20 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f  d previously. Fo
2b226 72 0d 0a 20 20 20 20 20 20 2a 2a 20 65 78 61 6d  r..      ** exam
2b227 70 6c 65 2c 20 69 66 20 74 68 65 20 74 61 62 6c  ple, if the tabl
2b228 65 20 6f 6e 20 77 68 69 63 68 20 74 72 69 67 67  e on which trigg
2b229 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 66 69  ers are being fi
2b22a 72 65 64 20 69 73 0d 0a 20 20 20 20 20 20 2a 2a  red is..      **
2b22b 20 64 65 63 6c 61 72 65 64 20 61 73 3a 0d 0a 20   declared as:.. 
2b22c 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a       **..      *
2b22d 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
2b22e 20 74 31 28 61 2c 20 62 29 3b 0d 0a 20 20 20 20   t1(a, b);..    
2b22f 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 54    **..      ** T
2b230 68 65 6e 20 70 31 20 69 73 20 69 6e 74 65 72 70  hen p1 is interp
2b231 72 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  reted as follows
2b232 3a 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20  :..      **..   
2b233 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30 20 20 20     **   p1==0   
2b234 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77 69 64 20  ->    old.rowid 
2b235 20 20 20 20 70 31 3d 3d 33 20 20 20 2d 3e 20 20      p1==3   ->  
2b236 20 20 6e 65 77 2e 72 6f 77 69 64 0d 0a 20 20 20    new.rowid..   
2b237 20 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20 20 20     **   p1==1   
2b238 2d 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20 20 20  ->    old.a     
2b239 20 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e 20 20      p1==4   ->  
2b23a 20 20 6e 65 77 2e 61 0d 0a 20 20 20 20 20 20 2a    new.a..      *
2b23b 2a 20 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20 20  *   p1==2   ->  
2b23c 20 20 6f 6c 64 2e 62 20 20 20 20 20 20 20 20 20    old.b         
2b23d 70 31 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e 65  p1==5   ->    ne
2b23e 77 2e 62 20 20 20 20 20 20 20 0d 0a 20 20 20 20  w.b       ..    
2b23f 20 20 2a 2f 0d 0a 20 20 20 20 20 20 54 61 62 6c    */..      Tabl
2b240 65 20 2a 70 54 61 62 20 3d 20 70 45 78 70 72 2d  e *pTab = pExpr-
2b241 3e 70 54 61 62 3b 0d 0a 20 20 20 20 20 20 69 6e  >pTab;..      in
2b242 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e 69 54  t p1 = pExpr->iT
2b243 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e 6e 43  able * (pTab->nC
2b244 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45 78 70  ol+1) + 1 + pExp
2b245 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a 0d 0a 20  r->iColumn;.... 
2b246 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
2b247 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c  pr->iTable==0 ||
2b248 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
2b249 31 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  1 );..      asse
2b24a 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  rt( pExpr->iColu
2b24b 6d 6e 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d  mn>=-1 && pExpr-
2b24c 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e  >iColumn<pTab->n
2b24d 43 6f 6c 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  Col );..      as
2b24e 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65  sert( pTab->iPKe
2b24f 79 3c 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43  y<0 || pExpr->iC
2b250 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b  olumn!=pTab->iPK
2b251 65 79 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73  ey );..      ass
2b252 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31  ert( p1>=0 && p1
2b253 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32  <(pTab->nCol*2+2
2b254 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 73 71  ) );....      sq
2b255 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b256 76 2c 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31 2c  v, OP_Param, p1,
2b257 20 74 61 72 67 65 74 29 3b 0d 0a 20 20 20 20 20   target);..     
2b258 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2b259 20 22 25 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c   "%s.%s -> $%d",
2b25a 0d 0a 20 20 20 20 20 20 20 20 28 70 45 78 70 72  ..        (pExpr
2b25b 2d 3e 69 54 61 62 6c 65 20 3f 20 22 6e 65 77 22  ->iTable ? "new"
2b25c 20 3a 20 22 6f 6c 64 22 29 2c 0d 0a 20 20 20 20   : "old"),..    
2b25d 20 20 20 20 28 70 45 78 70 72 2d 3e 69 43 6f 6c      (pExpr->iCol
2b25e 75 6d 6e 3c 30 20 3f 20 22 72 6f 77 69 64 22 20  umn<0 ? "rowid" 
2b25f 3a 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61  : pExpr->pTab->a
2b260 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75  Col[pExpr->iColu
2b261 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0d 0a 20 20 20  mn].zName),..   
2b262 20 20 20 20 20 74 61 72 67 65 74 0d 0a 20 20 20       target..   
2b263 20 20 20 29 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65     ));....#ifnde
2b264 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
2b265 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20  OATING_POINT..  
2b266 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
2b267 6c 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66  lumn has REAL af
2b268 66 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63  finity, it may c
2b269 75 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72  urrently be stor
2b26a 65 64 20 61 73 20 61 6e 0d 0a 20 20 20 20 20 20  ed as an..      
2b26b 2a 2a 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20  ** integer. Use 
2b26c 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20  OP_RealAffinity 
2b26d 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
2b26e 69 73 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20  is really real. 
2b26f 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70   */..      if( p
2b270 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  Expr->iColumn>=0
2b271 20 0d 0a 20 20 20 20 20 20 20 26 26 20 70 54 61   ..       && pTa
2b272 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69  b->aCol[pExpr->i
2b273 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79  Column].affinity
2b274 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
2b275 4c 0d 0a 20 20 20 20 20 20 29 7b 0d 0a 20 20 20  L..      ){..   
2b276 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b277 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61  AddOp1(v, OP_Rea
2b278 6c 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67 65  lAffinity, targe
2b279 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65  t);..      }..#e
2b27a 6e 64 69 66 0d 0a 20 20 20 20 20 20 62 72 65 61  ndif..      brea
2b27b 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 0d 0a 20  k;..    }...... 
2b27c 20 20 20 2f 2a 0d 0a 20 20 20 20 2a 2a 20 46 6f     /*..    ** Fo
2b27d 72 6d 20 41 3a 0d 0a 20 20 20 20 2a 2a 20 20 20  rm A:..    **   
2b27e 43 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 54  CASE x WHEN e1 T
2b27f 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54  HEN r1 WHEN e2 T
2b280 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20  HEN r2 ... WHEN 
2b281 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20  eN THEN rN ELSE 
2b282 79 20 45 4e 44 0d 0a 20 20 20 20 2a 2a 0d 0a 20  y END..    **.. 
2b283 20 20 20 2a 2a 20 46 6f 72 6d 20 42 3a 0d 0a 20     ** Form B:.. 
2b284 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48 45     **   CASE WHE
2b285 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45  N e1 THEN r1 WHE
2b286 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e  N e2 THEN r2 ...
2b287 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e   WHEN eN THEN rN
2b288 20 45 4c 53 45 20 79 20 45 4e 44 0d 0a 20 20 20   ELSE y END..   
2b289 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 46 6f 72 6d   **..    ** Form
2b28a 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 61   A is can be tra
2b28b 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 68  nsformed into th
2b28c 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72  e equivalent for
2b28d 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d  m B as follows:.
2b28e 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57  .    **   CASE W
2b28f 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31  HEN x=e1 THEN r1
2b290 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20   WHEN x=e2 THEN 
2b291 72 32 20 2e 2e 2e 0d 0a 20 20 20 20 2a 2a 20 20  r2 .....    **  
2b292 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20        WHEN x=eN 
2b293 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45  THEN rN ELSE y E
2b294 4e 44 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  ND..    **..    
2b295 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 69 73  ** X (if it exis
2b296 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d  ts) is in pExpr-
2b297 3e 70 4c 65 66 74 2e 0d 0a 20 20 20 20 2a 2a 20  >pLeft...    ** 
2b298 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70  Y is in pExpr->p
2b299 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 73  Right.  The Y is
2b29a 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20   also optional. 
2b29b 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0d   If there is no.
2b29c 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61  .    ** ELSE cla
2b29d 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72  use and no other
2b29e 20 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74   term matches, t
2b29f 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
2b2a0 66 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 65 78  f the..    ** ex
2b2a1 70 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e  prssion is NULL.
2b2a2 0d 0a 20 20 20 20 2a 2a 20 45 69 20 69 73 20 69  ..    ** Ei is i
2b2a3 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  n pExpr->pList->
2b2a4 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 73  a[i*2] and Ri is
2b2a5 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61   pExpr->pList->a
2b2a6 5b 69 2a 32 2b 31 5d 2e 0d 0a 20 20 20 20 2a 2a  [i*2+1]...    **
2b2a7 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73  ..    ** The res
2b2a8 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65  ult of the expre
2b2a9 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20  ssion is the Ri 
2b2aa 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61  for the first ma
2b2ab 74 63 68 69 6e 67 20 45 69 2c 0d 0a 20 20 20 20  tching Ei,..    
2b2ac 2a 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69  ** or if there i
2b2ad 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69  s no matching Ei
2b2ae 2c 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20  , the ELSE term 
2b2af 59 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69  Y, or if there i
2b2b0 73 0d 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53  s..    ** no ELS
2b2b1 45 20 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0d 0a 20  E term, NULL... 
2b2b2 20 20 20 2a 2f 0d 0a 20 20 20 20 64 65 66 61 75     */..    defau
2b2b3 6c 74 3a 20 61 73 73 65 72 74 28 20 6f 70 3d 3d  lt: assert( op==
2b2b4 54 4b 5f 43 41 53 45 20 29 3b 20 7b 0d 0a 20 20  TK_CASE ); {..  
2b2b5 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c      int endLabel
2b2b6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b2b7 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61        /* GOTO la
2b2b8 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43  bel for end of C
2b2b9 41 53 45 20 73 74 6d 74 20 2a 2f 0d 0a 20 20 20  ASE stmt */..   
2b2ba 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b     int nextCase;
2b2bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2bc 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62       /* GOTO lab
2b2bd 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e  el for next WHEN
2b2be 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20   clause */..    
2b2bf 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20    int nExpr;    
2b2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2c1 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72      /* 2x number
2b2c2 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a   of WHEN terms *
2b2c3 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20  /..      int i; 
2b2c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2b2c6 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
2b2c7 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2b2c8 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  EList;          
2b2c9 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
2b2ca 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0d  f WHEN terms */.
2b2cb 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
2b2cc 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69  prList_item *aLi
2b2cd 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61  stelem;  /* Arra
2b2ce 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20  y of WHEN terms 
2b2cf 2a 2f 0d 0a 20 20 20 20 20 20 45 78 70 72 20 6f  */..      Expr o
2b2d0 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20  pCompare;       
2b2d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2b2d2 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73  he X==Ei express
2b2d3 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 45 78  ion */..      Ex
2b2d4 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20  pr cacheX;      
2b2d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2d6 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73  /* Cached expres
2b2d7 73 69 6f 6e 20 58 20 2a 2f 0d 0a 20 20 20 20 20  sion X */..     
2b2d8 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20   Expr *pX;      
2b2d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2da 20 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72     /* The X expr
2b2db 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20  ession */..     
2b2dc 20 45 78 70 72 20 2a 70 54 65 73 74 20 3d 20 30   Expr *pTest = 0
2b2dd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b2de 20 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72     /* X==Ei (for
2b2df 6d 20 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20  m A) or just Ei 
2b2e0 28 66 6f 72 6d 20 42 29 20 2a 2f 0d 0a 20 20 20  (form B) */..   
2b2e1 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74     VVA_ONLY( int
2b2e2 20 69 43 61 63 68 65 4c 65 76 65 6c 20 3d 20 70   iCacheLevel = p
2b2e3 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
2b2e4 65 6c 3b 20 29 0d 0a 0d 0a 20 20 20 20 20 20 61  el; )....      a
2b2e5 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2b2e6 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2b2e7 50 5f 78 49 73 53 65 6c 65 63 74 29 20 26 26 20  P_xIsSelect) && 
2b2e8 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29  pExpr->x.pList )
2b2e9 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2b2ea 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d  (pExpr->x.pList-
2b2eb 3e 6e 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30  >nExpr % 2) == 0
2b2ec 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
2b2ed 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d  (pExpr->x.pList-
2b2ee 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0d 0a 20 20  >nExpr > 0);..  
2b2ef 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78      pEList = pEx
2b2f0 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0d 0a 20 20  pr->x.pList;..  
2b2f1 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20      aListelem = 
2b2f2 70 45 4c 69 73 74 2d 3e 61 3b 0d 0a 20 20 20 20  pEList->a;..    
2b2f3 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74    nExpr = pEList
2b2f4 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20 20 20 20  ->nExpr;..      
2b2f5 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74  endLabel = sqlit
2b2f6 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2b2f7 76 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 28  v);..      if( (
2b2f8 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  pX = pExpr->pLef
2b2f9 74 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  t)!=0 ){..      
2b2fa 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0d    cacheX = *pX;.
2b2fb 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
2b2fc 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  e( pX->op==TK_CO
2b2fd 4c 55 4d 4e 20 29 3b 0d 0a 20 20 20 20 20 20 20  LUMN );..       
2b2fe 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f   testcase( pX->o
2b2ff 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29  p==TK_REGISTER )
2b300 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 63 68 65  ;..        cache
2b301 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
2b302 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
2b303 50 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46  Parse, pX, &regF
2b304 72 65 65 31 29 3b 0d 0a 20 20 20 20 20 20 20 20  ree1);..        
2b305 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
2b306 65 31 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  e1==0 );..      
2b307 20 20 63 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b    cacheX.op = TK
2b308 5f 52 45 47 49 53 54 45 52 3b 0d 0a 20 20 20 20  _REGISTER;..    
2b309 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 6f 70      opCompare.op
2b30a 20 3d 20 54 4b 5f 45 51 3b 0d 0a 20 20 20 20 20   = TK_EQ;..     
2b30b 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 4c 65     opCompare.pLe
2b30c 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0d 0a 20  ft = &cacheX;.. 
2b30d 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 26         pTest = &
2b30e 6f 70 43 6f 6d 70 61 72 65 3b 0d 0a 20 20 20 20  opCompare;..    
2b30f 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 62 33      /* Ticket b3
2b310 35 31 64 39 35 66 39 63 64 35 65 66 31 37 65 39  51d95f9cd5ef17e9
2b311 64 39 64 62 61 65 31 38 66 35 63 61 38 36 31 31  d9dbae18f5ca8611
2b312 31 39 30 30 30 31 3a 0d 0a 20 20 20 20 20 20 20  190001:..       
2b313 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 6e   ** The value in
2b314 20 72 65 67 46 72 65 65 31 20 6d 69 67 68 74 20   regFree1 might 
2b315 67 65 74 20 53 43 6f 70 79 2d 65 64 20 69 6e 74  get SCopy-ed int
2b316 6f 20 74 68 65 20 66 69 6c 65 20 72 65 73 75 6c  o the file resul
2b317 74 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  t...        ** S
2b318 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
2b319 20 74 68 65 20 72 65 67 46 72 65 65 31 20 72 65   the regFree1 re
2b31a 67 69 73 74 65 72 20 69 73 20 6e 6f 74 20 72 65  gister is not re
2b31b 75 73 65 64 20 66 6f 72 20 6f 74 68 65 72 0d 0a  used for other..
2b31c 20 20 20 20 20 20 20 20 2a 2a 20 70 75 72 70 6f          ** purpo
2b31d 73 65 73 20 61 6e 64 20 70 6f 73 73 69 62 6c 79  ses and possibly
2b31e 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 2a   overwritten.  *
2b31f 2f 0d 0a 20 20 20 20 20 20 20 20 72 65 67 46 72  /..        regFr
2b320 65 65 31 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ee1 = 0;..      
2b321 7d 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  }..      for(i=0
2b322 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 32  ; i<nExpr; i=i+2
2b323 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
2b324 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
2b325 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
2b326 20 20 20 69 66 28 20 70 58 20 29 7b 0d 0a 20 20     if( pX ){..  
2b327 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b328 70 54 65 73 74 21 3d 30 20 29 3b 0d 0a 20 20 20  pTest!=0 );..   
2b329 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65         opCompare
2b32a 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65  .pRight = aListe
2b32b 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0d 0a 20  lem[i].pExpr;.. 
2b32c 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
2b32d 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d           pTest =
2b32e 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45   aListelem[i].pE
2b32f 78 70 72 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  xpr;..        }.
2b330 0a 20 20 20 20 20 20 20 20 6e 65 78 74 43 61 73  .        nextCas
2b331 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
2b332 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20  akeLabel(v);..  
2b333 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b334 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pTest->op==TK_CO
2b335 4c 55 4d 4e 20 29 3b 0d 0a 20 20 20 20 20 20 20  LUMN );..       
2b336 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2b337 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 73  lse(pParse, pTes
2b338 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51 4c  t, nextCase, SQL
2b339 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
2b33a 0d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ..        testca
2b33b 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b  se( aListelem[i+
2b33c 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  1].pExpr->op==TK
2b33d 5f 43 4f 4c 55 4d 4e 20 29 3b 0d 0a 20 20 20 20  _COLUMN );..    
2b33e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c      testcase( aL
2b33f 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78  istelem[i+1].pEx
2b340 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pr->op==TK_REGIS
2b341 54 45 52 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  TER );..        
2b342 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2b343 70 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65  pParse, aListele
2b344 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61  m[i+1].pExpr, ta
2b345 72 67 65 74 29 3b 0d 0a 20 20 20 20 20 20 20 20  rget);..        
2b346 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b347 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b348 20 65 6e 64 4c 61 62 65 6c 29 3b 0d 0a 20 20 20   endLabel);..   
2b349 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b34a 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
2b34b 20 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71   1);..        sq
2b34c 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2b34d 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73  Label(v, nextCas
2b34e 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  e);..      }..  
2b34f 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70      if( pExpr->p
2b350 52 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20 20  Right ){..      
2b351 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2b352 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0d  hePush(pParse);.
2b353 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b354 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2b355 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
2b356 74 61 72 67 65 74 29 3b 0d 0a 20 20 20 20 20 20  target);..      
2b357 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2b358 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
2b359 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
2b35a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b35b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b35c 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74  _Null, 0, target
2b35d 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
2b35e 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
2b35f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70  allocFailed || p
2b360 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 0d 0a  Parse->nErr>0 ..
2b361 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50             || pP
2b362 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
2b363 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 29  l==iCacheLevel )
2b364 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2b365 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2b366 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0d 0a  (v, endLabel);..
2b367 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
2b368 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2b369 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2b36a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41  ..    case TK_RA
2b36b 49 53 45 3a 20 7b 0d 0a 20 20 20 20 20 20 61 73  ISE: {..      as
2b36c 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66  sert( pExpr->aff
2b36d 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  inity==OE_Rollba
2b36e 63 6b 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ck ..           
2b36f 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  || pExpr->affini
2b370 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0d 0a 20 20  ty==OE_Abort..  
2b371 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70           || pExp
2b372 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
2b373 46 61 69 6c 0d 0a 20 20 20 20 20 20 20 20 20 20  Fail..          
2b374 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e   || pExpr->affin
2b375 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0d 0a  ity==OE_Ignore..
2b376 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
2b377 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 54 72  if( !pParse->pTr
2b378 69 67 67 65 72 54 61 62 20 29 7b 0d 0a 20 20 20  iggerTab ){..   
2b379 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2b37a 72 4d 73 67 28 70 50 61 72 73 65 2c 0d 0a 20 20  rMsg(pParse,..  
2b37b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b37c 20 20 20 20 20 22 52 41 49 53 45 28 29 20 6d 61       "RAISE() ma
2b37d 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77  y only be used w
2b37e 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2d  ithin a trigger-
2b37f 70 72 6f 67 72 61 6d 22 29 3b 0d 0a 20 20 20 20  program");..    
2b380 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
2b381 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
2b382 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  ( pExpr->affinit
2b383 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0d 0a  y==OE_Abort ){..
2b384 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
2b385 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b  ayAbort(pParse);
2b386 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
2b387 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2b388 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2b389 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b   EP_IntValue) );
2b38a 0d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ..      if( pExp
2b38b 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
2b38c 49 67 6e 6f 72 65 20 29 7b 0d 0a 20 20 20 20 20  Ignore ){..     
2b38d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b38e 64 4f 70 34 28 0d 0a 20 20 20 20 20 20 20 20 20  dOp4(..         
2b38f 20 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53     v, OP_Halt, S
2b390 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67 6e  QLITE_OK, OE_Ign
2b391 6f 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75  ore, 0, pExpr->u
2b392 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0d 0a 20 20 20  .zToken,0);..   
2b393 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2b394 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f     sqlite3HaltCo
2b395 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c  nstraint(pParse,
2b396 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
2b397 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
2b398 6e 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  n, 0);..      }.
2b399 0a 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ...      break;.
2b39a 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
2b39b 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65    }..  sqlite3Re
2b39c 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2b39d 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0d  rse, regFree1);.
2b39e 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2b39f 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2b3a0 20 72 65 67 46 72 65 65 32 29 3b 0d 0a 20 20 72   regFree2);..  r
2b3a1 65 74 75 72 6e 20 69 6e 52 65 67 3b 0d 0a 7d 0d  eturn inReg;..}.
2b3a2 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
2b3a3 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75  te code to evalu
2b3a4 61 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ate an expressio
2b3a5 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  n and store the 
2b3a6 72 65 73 75 6c 74 73 0d 0a 2a 2a 20 69 6e 74 6f  results..** into
2b3a7 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 52 65   a register.  Re
2b3a8 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65  turn the registe
2b3a9 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74  r number where t
2b3aa 68 65 20 72 65 73 75 6c 74 73 0d 0a 2a 2a 20 61  he results..** a
2b3ab 72 65 20 73 74 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a  re stored...**..
2b3ac 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 73 74  ** If the regist
2b3ad 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72  er is a temporar
2b3ae 79 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  y register that 
2b3af 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74  can be deallocat
2b3b0 65 64 2c 0d 0a 2a 2a 20 74 68 65 6e 20 77 72 69  ed,..** then wri
2b3b1 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 6e  te its number in
2b3b2 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 68  to *pReg.  If th
2b3b3 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65  e result registe
2b3b4 72 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 61 20 74  r is not..** a t
2b3b5 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73  emporary, then s
2b3b6 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f  et *pReg to zero
2b3b7 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2b3b8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2b3b9 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61  3ExprCodeTemp(Pa
2b3ba 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
2b3bb 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70  r *pExpr, int *p
2b3bc 52 65 67 29 7b 0d 0a 20 20 69 6e 74 20 72 31 20  Reg){..  int r1 
2b3bd 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2b3be 52 65 67 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  Reg(pParse);..  
2b3bf 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
2b3c0 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2b3c1 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31  Parse, pExpr, r1
2b3c2 29 3b 0d 0a 20 20 69 66 28 20 72 32 3d 3d 72 31  );..  if( r2==r1
2b3c3 20 29 7b 0d 0a 20 20 20 20 2a 70 52 65 67 20 3d   ){..    *pReg =
2b3c4 20 72 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a   r1;..  }else{..
2b3c5 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2b3c6 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2b3c7 2c 20 72 31 29 3b 0d 0a 20 20 20 20 2a 70 52 65  , r1);..    *pRe
2b3c8 67 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72  g = 0;..  }..  r
2b3c9 65 74 75 72 6e 20 72 32 3b 0d 0a 7d 0d 0a 0d 0a  eturn r2;..}....
2b3ca 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
2b3cb 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
2b3cc 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69  valuate expressi
2b3cd 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f  on pExpr and sto
2b3ce 72 65 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75 6c  re the..** resul
2b3cf 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74  ts in register t
2b3d0 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75  arget.  The resu
2b3d1 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65  lts are guarante
2b3d2 65 64 20 74 6f 20 61 70 70 65 61 72 0d 0a 2a 2a  ed to appear..**
2b3d3 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72   in register tar
2b3d4 67 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  get...*/..SQLITE
2b3d5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2b3d6 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 72  ite3ExprCode(Par
2b3d7 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2b3d8 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72   *pExpr, int tar
2b3d9 67 65 74 29 7b 0d 0a 20 20 69 6e 74 20 69 6e 52  get){..  int inR
2b3da 65 67 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  eg;....  assert(
2b3db 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72   target>0 && tar
2b3dc 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65  get<=pParse->nMe
2b3dd 6d 20 29 3b 0d 0a 20 20 69 66 28 20 70 45 78 70  m );..  if( pExp
2b3de 72 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  r && pExpr->op==
2b3df 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 0d 0a  TK_REGISTER ){..
2b3e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b3e1 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56  ddOp2(pParse->pV
2b3e2 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 70 45  dbe, OP_Copy, pE
2b3e3 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72  xpr->iTable, tar
2b3e4 67 65 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  get);..  }else{.
2b3e5 0a 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c  .    inReg = sql
2b3e6 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
2b3e7 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
2b3e8 2c 20 74 61 72 67 65 74 29 3b 0d 0a 20 20 20 20  , target);..    
2b3e9 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2b3ea 70 56 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d  pVdbe || pParse-
2b3eb 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2b3ec 64 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 69 6e  d );..    if( in
2b3ed 52 65 67 21 3d 74 61 72 67 65 74 20 26 26 20 70  Reg!=target && p
2b3ee 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0d  Parse->pVdbe ){.
2b3ef 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b3f0 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d  beAddOp2(pParse-
2b3f1 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79  >pVdbe, OP_SCopy
2b3f2 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29  , inReg, target)
2b3f3 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
2b3f4 20 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0d   return target;.
2b3f5 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e  .}..../*..** Gen
2b3f6 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
2b3f7 65 76 61 6c 75 74 65 73 20 74 68 65 20 67 69 76  evalutes the giv
2b3f8 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  en expression an
2b3f9 64 20 70 75 74 73 20 74 68 65 20 72 65 73 75 6c  d puts the resul
2b3fa 74 0d 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65  t..** in registe
2b3fb 72 20 74 61 72 67 65 74 2e 0d 0a 2a 2a 0d 0a 2a  r target...**..*
2b3fc 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f  * Also make a co
2b3fd 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  py of the expres
2b3fe 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e 74  sion results int
2b3ff 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68 65  o another "cache
2b400 22 20 72 65 67 69 73 74 65 72 0d 0a 2a 2a 20 61  " register..** a
2b401 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78  nd modify the ex
2b402 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74  pression so that
2b403 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69   the next time i
2b404 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0d  t is evaluated,.
2b405 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69  .** the result i
2b406 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2b407 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e 0d  cache register..
2b408 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
2b409 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
2b40a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61   expressions tha
2b40b 74 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69  t are used multi
2b40c 70 6c 65 20 0d 0a 2a 2a 20 74 69 6d 65 73 2e 20  ple ..** times. 
2b40d 20 54 68 65 79 20 61 72 65 20 65 76 61 6c 75 61   They are evalua
2b40e 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65  ted once and the
2b40f 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
2b410 65 78 70 72 65 73 73 69 6f 6e 0d 0a 2a 2a 20 61  expression..** a
2b411 72 65 20 72 65 75 73 65 64 2e 0d 0a 2a 2f 0d 0a  re reused...*/..
2b412 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2b413 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
2b414 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65  deAndCache(Parse
2b415 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2b416 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
2b417 74 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d  t){..  Vdbe *v =
2b418 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d   pParse->pVdbe;.
2b419 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0d 0a 20  .  int inReg;.. 
2b41a 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
2b41b 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2b41c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b   pExpr, target);
2b41d 0d 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  ..  assert( targ
2b41e 65 74 3e 30 20 29 3b 0d 0a 20 20 2f 2a 20 54 68  et>0 );..  /* Th
2b41f 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2b420 6c 6c 65 64 20 66 6f 72 20 74 65 72 6d 73 20 74  lled for terms t
2b421 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  o INSERT or UPDA
2b422 54 45 2e 20 20 41 6e 64 20 74 68 65 20 6f 6e 6c  TE.  And the onl
2b423 79 0d 0a 20 20 2a 2a 20 6f 74 68 65 72 20 70 6c  y..  ** other pl
2b424 61 63 65 20 77 68 65 72 65 20 65 78 70 72 65 73  ace where expres
2b425 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 63 6f 6e  sions can be con
2b426 76 65 72 74 65 64 20 69 6e 74 6f 20 54 4b 5f 52  verted into TK_R
2b427 45 47 49 53 54 45 52 20 69 73 0d 0a 20 20 2a 2a  EGISTER is..  **
2b428 20 69 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65   in WHERE clause
2b429 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 53 6f   processing.  So
2b42a 20 61 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d   as currently im
2b42b 70 6c 65 6d 65 6e 74 65 64 2c 20 74 68 65 72 65  plemented, there
2b42c 20 69 73 0d 0a 20 20 2a 2a 20 6e 6f 20 77 61 79   is..  ** no way
2b42d 20 66 6f 72 20 61 20 54 4b 5f 52 45 47 49 53 54   for a TK_REGIST
2b42e 45 52 20 74 6f 20 65 78 69 73 74 20 68 65 72 65  ER to exist here
2b42f 2e 20 20 42 75 74 20 69 74 20 73 65 65 6d 73 20  .  But it seems 
2b430 70 72 75 64 65 6e 74 20 74 6f 0d 0a 20 20 2a 2a  prudent to..  **
2b431 20 6b 65 65 70 20 74 68 65 20 41 4c 57 41 59 53   keep the ALWAYS
2b432 28 29 20 69 6e 20 63 61 73 65 20 74 68 65 20 63  () in case the c
2b433 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20  onditions above 
2b434 63 68 61 6e 67 65 20 77 69 74 68 20 66 75 74 75  change with futu
2b435 72 65 0d 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63  re..  ** modific
2b436 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68 61 6e 63  ations or enhanc
2b437 65 6d 65 6e 74 73 2e 20 2a 2f 0d 0a 20 20 69 66  ements. */..  if
2b438 28 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d 3e  ( ALWAYS(pExpr->
2b439 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 29  op!=TK_REGISTER)
2b43a 20 29 7b 20 20 0d 0a 20 20 20 20 69 6e 74 20 69   ){  ..    int i
2b43b 4d 65 6d 3b 0d 0a 20 20 20 20 69 4d 65 6d 20 3d  Mem;..    iMem =
2b43c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2b43d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2b43e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
2b43f 70 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29  py, inReg, iMem)
2b440 3b 0d 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54  ;..    pExpr->iT
2b441 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0d 0a 20 20  able = iMem;..  
2b442 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70    pExpr->op2 = p
2b443 45 78 70 72 2d 3e 6f 70 3b 0d 0a 20 20 20 20 70  Expr->op;..    p
2b444 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45  Expr->op = TK_RE
2b445 47 49 53 54 45 52 3b 0d 0a 20 20 7d 0d 0a 20 20  GISTER;..  }..  
2b446 72 65 74 75 72 6e 20 69 6e 52 65 67 3b 0d 0a 7d  return inReg;..}
2b447 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
2b448 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52  SQLITE_ENABLE_TR
2b449 45 45 5f 45 58 50 4c 41 49 4e 29 0d 0a 2f 2a 0d  EE_EXPLAIN)../*.
2b44a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 68  .** Generate a h
2b44b 75 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 78  uman-readable ex
2b44c 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 61 6e 20  planation of an 
2b44d 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
2b44e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2b44f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2b450 33 45 78 70 6c 61 69 6e 45 78 70 72 28 56 64 62  3ExplainExpr(Vdb
2b451 65 20 2a 70 4f 75 74 2c 20 45 78 70 72 20 2a 70  e *pOut, Expr *p
2b452 45 78 70 72 29 7b 0d 0a 20 20 69 6e 74 20 6f 70  Expr){..  int op
2b453 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b454 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64      /* The opcod
2b455 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  e being coded */
2b456 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
2b457 7a 42 69 6e 4f 70 20 3d 20 30 3b 20 20 20 2f 2a  zBinOp = 0;   /*
2b458 20 42 69 6e 61 72 79 20 6f 70 65 72 61 74 6f 72   Binary operator
2b459 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
2b45a 72 20 2a 7a 55 6e 69 4f 70 20 3d 20 30 3b 20 20  r *zUniOp = 0;  
2b45b 20 2f 2a 20 55 6e 61 72 79 20 6f 70 65 72 61 74   /* Unary operat
2b45c 6f 72 20 2a 2f 0d 0a 20 20 69 66 28 20 70 45 78  or */..  if( pEx
2b45d 70 72 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 6f 70  pr==0 ){..    op
2b45e 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0d 0a 20 20 7d   = TK_NULL;..  }
2b45f 65 6c 73 65 7b 0d 0a 20 20 20 20 6f 70 20 3d 20  else{..    op = 
2b460 70 45 78 70 72 2d 3e 6f 70 3b 0d 0a 20 20 7d 0d  pExpr->op;..  }.
2b461 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
2b462 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  ..    case TK_AG
2b463 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0d 0a 20 20 20  G_COLUMN: {..   
2b464 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b465 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 41  nPrintf(pOut, "A
2b466 47 47 7b 25 64 3a 25 64 7d 22 2c 0d 0a 20 20 20  GG{%d:%d}",..   
2b467 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
2b468 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69  iTable, pExpr->i
2b469 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 20 20 20 20  Column);..      
2b46a 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
2b46b 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d     case TK_COLUM
2b46c 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  N: {..      if( 
2b46d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20  pExpr->iTable<0 
2b46e 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  ){..        /* T
2b46f 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73  his only happens
2b470 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65   when coding che
2b471 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
2b472 2f 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  /..        sqlit
2b473 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
2b474 70 4f 75 74 2c 20 22 43 4f 4c 55 4d 4e 28 25 64  pOut, "COLUMN(%d
2b475 29 22 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  )", pExpr->iColu
2b476 6d 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  mn);..      }els
2b477 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e{..        sqli
2b478 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2b479 28 70 4f 75 74 2c 20 22 7b 25 64 3a 25 64 7d 22  (pOut, "{%d:%d}"
2b47a 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
2b47b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b47c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 70  pExpr->iTable, p
2b47d 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0d  Expr->iColumn);.
2b47e 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
2b47f 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
2b480 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47     case TK_INTEG
2b481 45 52 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28  ER: {..      if(
2b482 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20   pExpr->flags & 
2b483 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0d 0a  EP_IntValue ){..
2b484 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2b485 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75  xplainPrintf(pOu
2b486 74 2c 20 22 25 64 22 2c 20 70 45 78 70 72 2d 3e  t, "%d", pExpr->
2b487 75 2e 69 56 61 6c 75 65 29 3b 0d 0a 20 20 20 20  u.iValue);..    
2b488 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
2b489 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2b48a 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 25 73  Printf(pOut, "%s
2b48b 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  ", pExpr->u.zTok
2b48c 65 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  en);..      }.. 
2b48d 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2b48e 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2b48f 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2b490 5f 50 4f 49 4e 54 0d 0a 20 20 20 20 63 61 73 65  _POINT..    case
2b491 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0d 0a 20 20   TK_FLOAT: {..  
2b492 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
2b493 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 25  inPrintf(pOut,"%
2b494 73 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  s", pExpr->u.zTo
2b495 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  ken);..      bre
2b496 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  ak;..    }..#end
2b497 69 66 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  if..    case TK_
2b498 53 54 52 49 4e 47 3a 20 7b 0d 0a 20 20 20 20 20  STRING: {..     
2b499 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
2b49a 72 69 6e 74 66 28 70 4f 75 74 2c 22 25 51 22 2c  rintf(pOut,"%Q",
2b49b 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
2b49c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
2b49d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
2b49e 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0d 0a 20 20  e TK_NULL: {..  
2b49f 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
2b4a0 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 4e  inPrintf(pOut,"N
2b4a1 55 4c 4c 22 29 3b 0d 0a 20 20 20 20 20 20 62 72  ULL");..      br
2b4a2 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 69 66  eak;..    }..#if
2b4a3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b4a4 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0d 0a 20  _BLOB_LITERAL.. 
2b4a5 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
2b4a6 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65   {..      sqlite
2b4a7 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70  3ExplainPrintf(p
2b4a8 4f 75 74 2c 22 25 73 22 2c 20 70 45 78 70 72 2d  Out,"%s", pExpr-
2b4a9 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20  >u.zToken);..   
2b4aa 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
2b4ab 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 63 61  ..#endif..    ca
2b4ac 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20  se TK_VARIABLE: 
2b4ad 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2b4ae 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f  ExplainPrintf(pO
2b4af 75 74 2c 22 56 41 52 49 41 42 4c 45 28 25 73 2c  ut,"VARIABLE(%s,
2b4b0 25 64 29 22 2c 0d 0a 20 20 20 20 20 20 20 20 20  %d)",..         
2b4b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4b2 20 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65    pExpr->u.zToke
2b4b3 6e 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  n, pExpr->iColum
2b4b4 6e 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  n);..      break
2b4b5 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2b4b6 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20  se TK_REGISTER: 
2b4b7 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2b4b8 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f  ExplainPrintf(pO
2b4b9 75 74 2c 22 52 45 47 49 53 54 45 52 28 25 64 29  ut,"REGISTER(%d)
2b4ba 22 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  ", pExpr->iTable
2b4bb 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
2b4bc 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
2b4bd 65 20 54 4b 5f 41 53 3a 20 7b 0d 0a 20 20 20 20  e TK_AS: {..    
2b4be 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2b4bf 45 78 70 72 28 70 4f 75 74 2c 20 70 45 78 70 72  Expr(pOut, pExpr
2b4c0 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20 20 20 20  ->pLeft);..     
2b4c1 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2b4c2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2b4c3 4d 49 54 5f 43 41 53 54 0d 0a 20 20 20 20 63 61  MIT_CAST..    ca
2b4c4 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0d 0a 20  se TK_CAST: {.. 
2b4c5 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
2b4c6 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a  ons of the form:
2b4c7 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53     CAST(pLeft AS
2b4c8 20 74 6f 6b 65 6e 29 20 2a 2f 0d 0a 20 20 20 20   token) */..    
2b4c9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
2b4ca 66 66 20 3d 20 22 75 6e 6b 22 3b 0d 0a 20 20 20  ff = "unk";..   
2b4cb 20 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74     switch( sqlit
2b4cc 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70  e3AffinityType(p
2b4cd 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20  Expr->u.zToken) 
2b4ce 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65  ){..        case
2b4cf 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
2b4d0 3a 20 20 20 20 7a 41 66 66 20 3d 20 22 54 45 58  :    zAff = "TEX
2b4d1 54 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  T";     break;..
2b4d2 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
2b4d3 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 20 20 20  ITE_AFF_NONE:   
2b4d4 20 7a 41 66 66 20 3d 20 22 4e 4f 4e 45 22 3b 20   zAff = "NONE"; 
2b4d5 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
2b4d6 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2b4d7 41 46 46 5f 4e 55 4d 45 52 49 43 3a 20 7a 41 66  AFF_NUMERIC: zAf
2b4d8 66 20 3d 20 22 4e 55 4d 45 52 49 43 22 3b 20 20  f = "NUMERIC";  
2b4d9 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
2b4da 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  case SQLITE_AFF_
2b4db 49 4e 54 45 47 45 52 3a 20 7a 41 66 66 20 3d 20  INTEGER: zAff = 
2b4dc 22 49 4e 54 45 47 45 52 22 3b 20 20 62 72 65 61  "INTEGER";  brea
2b4dd 6b 3b 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65  k;..        case
2b4de 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
2b4df 3a 20 20 20 20 7a 41 66 66 20 3d 20 22 52 45 41  :    zAff = "REA
2b4e0 4c 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  L";     break;..
2b4e1 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73        }..      s
2b4e2 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69  qlite3ExplainPri
2b4e3 6e 74 66 28 70 4f 75 74 2c 20 22 43 41 53 54 2d  ntf(pOut, "CAST-
2b4e4 25 73 28 22 2c 20 7a 41 66 66 29 3b 0d 0a 20 20  %s(", zAff);..  
2b4e5 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
2b4e6 69 6e 45 78 70 72 28 70 4f 75 74 2c 20 70 45 78  inExpr(pOut, pEx
2b4e7 70 72 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20 20  pr->pLeft);..   
2b4e8 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b4e9 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 29  nPrintf(pOut, ")
2b4ea 22 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  ");..      break
2b4eb 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  ;..    }..#endif
2b4ec 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2b4ed 43 41 53 54 20 2a 2f 0d 0a 20 20 20 20 63 61 73  CAST */..    cas
2b4ee 65 20 54 4b 5f 4c 54 3a 20 20 20 20 20 20 7a 42  e TK_LT:      zB
2b4ef 69 6e 4f 70 20 3d 20 22 4c 54 22 3b 20 20 20 20  inOp = "LT";    
2b4f0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b4f1 65 20 54 4b 5f 4c 45 3a 20 20 20 20 20 20 7a 42  e TK_LE:      zB
2b4f2 69 6e 4f 70 20 3d 20 22 4c 45 22 3b 20 20 20 20  inOp = "LE";    
2b4f3 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b4f4 65 20 54 4b 5f 47 54 3a 20 20 20 20 20 20 7a 42  e TK_GT:      zB
2b4f5 69 6e 4f 70 20 3d 20 22 47 54 22 3b 20 20 20 20  inOp = "GT";    
2b4f6 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b4f7 65 20 54 4b 5f 47 45 3a 20 20 20 20 20 20 7a 42  e TK_GE:      zB
2b4f8 69 6e 4f 70 20 3d 20 22 47 45 22 3b 20 20 20 20  inOp = "GE";    
2b4f9 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b4fa 65 20 54 4b 5f 4e 45 3a 20 20 20 20 20 20 7a 42  e TK_NE:      zB
2b4fb 69 6e 4f 70 20 3d 20 22 4e 45 22 3b 20 20 20 20  inOp = "NE";    
2b4fc 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b4fd 65 20 54 4b 5f 45 51 3a 20 20 20 20 20 20 7a 42  e TK_EQ:      zB
2b4fe 69 6e 4f 70 20 3d 20 22 45 51 22 3b 20 20 20 20  inOp = "EQ";    
2b4ff 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b500 65 20 54 4b 5f 49 53 3a 20 20 20 20 20 20 7a 42  e TK_IS:      zB
2b501 69 6e 4f 70 20 3d 20 22 49 53 22 3b 20 20 20 20  inOp = "IS";    
2b502 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b503 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 20 20 7a 42  e TK_ISNOT:   zB
2b504 69 6e 4f 70 20 3d 20 22 49 53 4e 4f 54 22 3b 20  inOp = "ISNOT"; 
2b505 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b506 65 20 54 4b 5f 41 4e 44 3a 20 20 20 20 20 7a 42  e TK_AND:     zB
2b507 69 6e 4f 70 20 3d 20 22 41 4e 44 22 3b 20 20 20  inOp = "AND";   
2b508 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b509 65 20 54 4b 5f 4f 52 3a 20 20 20 20 20 20 7a 42  e TK_OR:      zB
2b50a 69 6e 4f 70 20 3d 20 22 4f 52 22 3b 20 20 20 20  inOp = "OR";    
2b50b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b50c 65 20 54 4b 5f 50 4c 55 53 3a 20 20 20 20 7a 42  e TK_PLUS:    zB
2b50d 69 6e 4f 70 20 3d 20 22 41 44 44 22 3b 20 20 20  inOp = "ADD";   
2b50e 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b50f 65 20 54 4b 5f 53 54 41 52 3a 20 20 20 20 7a 42  e TK_STAR:    zB
2b510 69 6e 4f 70 20 3d 20 22 4d 55 4c 22 3b 20 20 20  inOp = "MUL";   
2b511 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b512 65 20 54 4b 5f 4d 49 4e 55 53 3a 20 20 20 7a 42  e TK_MINUS:   zB
2b513 69 6e 4f 70 20 3d 20 22 53 55 42 22 3b 20 20 20  inOp = "SUB";   
2b514 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b515 65 20 54 4b 5f 52 45 4d 3a 20 20 20 20 20 7a 42  e TK_REM:     zB
2b516 69 6e 4f 70 20 3d 20 22 52 45 4d 22 3b 20 20 20  inOp = "REM";   
2b517 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b518 65 20 54 4b 5f 42 49 54 41 4e 44 3a 20 20 7a 42  e TK_BITAND:  zB
2b519 69 6e 4f 70 20 3d 20 22 42 49 54 41 4e 44 22 3b  inOp = "BITAND";
2b51a 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b51b 65 20 54 4b 5f 42 49 54 4f 52 3a 20 20 20 7a 42  e TK_BITOR:   zB
2b51c 69 6e 4f 70 20 3d 20 22 42 49 54 4f 52 22 3b 20  inOp = "BITOR"; 
2b51d 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b51e 65 20 54 4b 5f 53 4c 41 53 48 3a 20 20 20 7a 42  e TK_SLASH:   zB
2b51f 69 6e 4f 70 20 3d 20 22 44 49 56 22 3b 20 20 20  inOp = "DIV";   
2b520 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b521 65 20 54 4b 5f 4c 53 48 49 46 54 3a 20 20 7a 42  e TK_LSHIFT:  zB
2b522 69 6e 4f 70 20 3d 20 22 4c 53 48 49 46 54 22 3b  inOp = "LSHIFT";
2b523 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b524 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 20 7a 42  e TK_RSHIFT:  zB
2b525 69 6e 4f 70 20 3d 20 22 52 53 48 49 46 54 22 3b  inOp = "RSHIFT";
2b526 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2b527 65 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 20 7a 42  e TK_CONCAT:  zB
2b528 69 6e 4f 70 20 3d 20 22 43 4f 4e 43 41 54 22 3b  inOp = "CONCAT";
2b529 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 63   break;....    c
2b52a 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 20  ase TK_UMINUS:  
2b52b 7a 55 6e 69 4f 70 20 3d 20 22 55 4d 49 4e 55 53  zUniOp = "UMINUS
2b52c 22 3b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63  "; break;..    c
2b52d 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 20 20  ase TK_UPLUS:   
2b52e 7a 55 6e 69 4f 70 20 3d 20 22 55 50 4c 55 53 22  zUniOp = "UPLUS"
2b52f 3b 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63  ;  break;..    c
2b530 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 20 20  ase TK_BITNOT:  
2b531 7a 55 6e 69 4f 70 20 3d 20 22 42 49 54 4e 4f 54  zUniOp = "BITNOT
2b532 22 3b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63  "; break;..    c
2b533 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 20 20 20 20  ase TK_NOT:     
2b534 7a 55 6e 69 4f 70 20 3d 20 22 4e 4f 54 22 3b 20  zUniOp = "NOT"; 
2b535 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63     break;..    c
2b536 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 20 20  ase TK_ISNULL:  
2b537 7a 55 6e 69 4f 70 20 3d 20 22 49 53 4e 55 4c 4c  zUniOp = "ISNULL
2b538 22 3b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63  "; break;..    c
2b539 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
2b53a 7a 55 6e 69 4f 70 20 3d 20 22 4e 4f 54 4e 55 4c  zUniOp = "NOTNUL
2b53b 4c 22 3b 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20  L"; break;....  
2b53c 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55    case TK_AGG_FU
2b53d 4e 43 54 49 4f 4e 3a 0d 0a 20 20 20 20 63 61 73  NCTION:..    cas
2b53e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a  e TK_CONST_FUNC:
2b53f 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55  ..    case TK_FU
2b540 4e 43 54 49 4f 4e 3a 20 7b 0d 0a 20 20 20 20 20  NCTION: {..     
2b541 20 45 78 70 72 4c 69 73 74 20 2a 70 46 61 72 67   ExprList *pFarg
2b542 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  ;       /* List 
2b543 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  of function argu
2b544 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20  ments */..      
2b545 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
2b546 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2b547 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0d 0a  _TokenOnly) ){..
2b548 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d 20          pFarg = 
2b549 30 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  0;..      }else{
2b54a 0d 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20  ..        pFarg 
2b54b 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2b54c 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2b54d 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2b54e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 25 73  Printf(pOut, "%s
2b54f 46 55 4e 43 54 49 4f 4e 3a 25 73 28 22 2c 0d 0a  FUNCTION:%s(",..
2b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b551 20 20 20 20 20 20 20 20 20 20 20 6f 70 3d 3d 54             op==T
2b552 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 3f  K_AGG_FUNCTION ?
2b553 20 22 41 47 47 5f 22 20 3a 20 22 22 2c 0d 0a 20   "AGG_" : "",.. 
2b554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b555 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
2b556 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20  >u.zToken);..   
2b557 20 20 20 69 66 28 20 70 46 61 72 67 20 29 7b 0d     if( pFarg ){.
2b558 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b559 45 78 70 6c 61 69 6e 45 78 70 72 4c 69 73 74 28  ExplainExprList(
2b55a 70 4f 75 74 2c 20 70 46 61 72 67 29 3b 0d 0a 20  pOut, pFarg);.. 
2b55b 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71       }..      sq
2b55c 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
2b55d 74 66 28 70 4f 75 74 2c 20 22 29 22 29 3b 0d 0a  tf(pOut, ")");..
2b55e 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
2b55f 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2b560 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2b561 59 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  Y..    case TK_E
2b562 58 49 53 54 53 3a 20 7b 0d 0a 20 20 20 20 20 20  XISTS: {..      
2b563 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
2b564 69 6e 74 66 28 70 4f 75 74 2c 20 22 45 58 49 53  intf(pOut, "EXIS
2b565 54 53 28 22 29 3b 0d 0a 20 20 20 20 20 20 73 71  TS(");..      sq
2b566 6c 69 74 65 33 45 78 70 6c 61 69 6e 53 65 6c 65  lite3ExplainSele
2b567 63 74 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  ct(pOut, pExpr->
2b568 78 2e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  x.pSelect);..   
2b569 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b56a 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 29 22  nPrintf(pOut,")"
2b56b 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
2b56c 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
2b56d 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0d 0a  e TK_SELECT: {..
2b56e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b56f 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c  lainPrintf(pOut,
2b570 20 22 28 22 29 3b 0d 0a 20 20 20 20 20 20 73 71   "(");..      sq
2b571 6c 69 74 65 33 45 78 70 6c 61 69 6e 53 65 6c 65  lite3ExplainSele
2b572 63 74 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  ct(pOut, pExpr->
2b573 78 2e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  x.pSelect);..   
2b574 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b575 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 29  nPrintf(pOut, ")
2b576 22 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  ");..      break
2b577 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2b578 73 65 20 54 4b 5f 49 4e 3a 20 7b 0d 0a 20 20 20  se TK_IN: {..   
2b579 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b57a 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 49  nPrintf(pOut, "I
2b57b 4e 28 22 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  N(");..      sql
2b57c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28  ite3ExplainExpr(
2b57d 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65  pOut, pExpr->pLe
2b57e 66 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ft);..      sqli
2b57f 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2b580 28 70 4f 75 74 2c 20 22 2c 22 29 3b 0d 0a 20 20  (pOut, ",");..  
2b581 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
2b582 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2b583 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0d  P_xIsSelect) ){.
2b584 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b585 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 70 4f  ExplainSelect(pO
2b586 75 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  ut, pExpr->x.pSe
2b587 6c 65 63 74 29 3b 0d 0a 20 20 20 20 20 20 7d 65  lect);..      }e
2b588 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
2b589 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
2b58a 4c 69 73 74 28 70 4f 75 74 2c 20 70 45 78 70 72  List(pOut, pExpr
2b58b 2d 3e 78 2e 70 4c 69 73 74 29 3b 0d 0a 20 20 20  ->x.pList);..   
2b58c 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
2b58d 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2b58e 28 70 4f 75 74 2c 20 22 29 22 29 3b 0d 0a 20 20  (pOut, ")");..  
2b58f 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
2b590 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
2b591 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2b592 59 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a  Y */....    /*..
2b593 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57      **    x BETW
2b594 45 45 4e 20 79 20 41 4e 44 20 7a 0d 0a 20 20 20  EEN y AND z..   
2b595 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 69 73   **..    ** This
2b596 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
2b597 6f 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  o..    **..    *
2b598 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c  *    x>=y AND x<
2b599 3d 7a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  =z..    **..    
2b59a 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 69  ** X is stored i
2b59b 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0d  n pExpr->pLeft..
2b59c 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f  .    ** Y is sto
2b59d 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
2b59e 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e  ist->a[0].pExpr.
2b59f 0d 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74  ..    ** Z is st
2b5a0 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70  ored in pExpr->p
2b5a1 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
2b5a2 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  ...    */..    c
2b5a3 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20  ase TK_BETWEEN: 
2b5a4 7b 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  {..      Expr *p
2b5a5 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  X = pExpr->pLeft
2b5a6 3b 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ;..      Expr *p
2b5a7 59 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  Y = pExpr->x.pLi
2b5a8 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0d  st->a[0].pExpr;.
2b5a9 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 5a 20  .      Expr *pZ 
2b5aa 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2b5ab 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0d 0a 20  ->a[1].pExpr;.. 
2b5ac 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2b5ad 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20  ainPrintf(pOut, 
2b5ae 22 42 45 54 57 45 45 4e 28 22 29 3b 0d 0a 20 20  "BETWEEN(");..  
2b5af 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
2b5b0 69 6e 45 78 70 72 28 70 4f 75 74 2c 20 70 58 29  inExpr(pOut, pX)
2b5b1 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2b5b2 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f  ExplainPrintf(pO
2b5b3 75 74 2c 20 22 2c 22 29 3b 0d 0a 20 20 20 20 20  ut, ",");..     
2b5b4 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
2b5b5 78 70 72 28 70 4f 75 74 2c 20 70 59 29 3b 0d 0a  xpr(pOut, pY);..
2b5b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b5b7 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c  lainPrintf(pOut,
2b5b8 20 22 2c 22 29 3b 0d 0a 20 20 20 20 20 20 73 71   ",");..      sq
2b5b9 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
2b5ba 28 70 4f 75 74 2c 20 70 5a 29 3b 0d 0a 20 20 20  (pOut, pZ);..   
2b5bb 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b5bc 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 29  nPrintf(pOut, ")
2b5bd 22 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  ");..      break
2b5be 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2b5bf 73 65 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b  se TK_TRIGGER: {
2b5c0 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
2b5c1 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b 5f 54  e opcode is TK_T
2b5c2 52 49 47 47 45 52 2c 20 74 68 65 6e 20 74 68 65  RIGGER, then the
2b5c3 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
2b5c4 20 72 65 66 65 72 65 6e 63 65 0d 0a 20 20 20 20   reference..    
2b5c5 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c 75 6d 6e    ** to a column
2b5c6 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 6f 72   in the new.* or
2b5c7 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61   old.* pseudo-ta
2b5c8 62 6c 65 73 20 61 76 61 69 6c 61 62 6c 65 20 74  bles available t
2b5c9 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67  o..      ** trig
2b5ca 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 49 6e  ger programs. In
2b5cb 20 74 68 69 73 20 63 61 73 65 20 45 78 70 72 2e   this case Expr.
2b5cc 69 54 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  iTable is set to
2b5cd 20 31 20 66 6f 72 20 74 68 65 0d 0a 20 20 20 20   1 for the..    
2b5ce 20 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75 64    ** new.* pseud
2b5cf 6f 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66 6f  o-table, or 0 fo
2b5d0 72 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75  r the old.* pseu
2b5d1 64 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e 69  do-table. Expr.i
2b5d2 43 6f 6c 75 6d 6e 0d 0a 20 20 20 20 20 20 2a 2a  Column..      **
2b5d3 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63   is set to the c
2b5d4 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70 73 65  olumn of the pse
2b5d5 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20 72 65 61  udo-table to rea
2b5d6 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74 6f 0d 0a  d, or to -1 to..
2b5d7 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 74 68        ** read th
2b5d8 65 20 72 6f 77 69 64 20 66 69 65 6c 64 2e 0d 0a  e rowid field...
2b5d9 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
2b5da 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
2b5db 69 6e 74 66 28 70 4f 75 74 2c 20 22 25 73 28 25  intf(pOut, "%s(%
2b5dc 64 29 22 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  d)", ..         
2b5dd 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3f   pExpr->iTable ?
2b5de 20 22 4e 45 57 22 20 3a 20 22 4f 4c 44 22 2c 20   "NEW" : "OLD", 
2b5df 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b  pExpr->iColumn);
2b5e0 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2b5e1 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
2b5e2 54 4b 5f 43 41 53 45 3a 20 7b 0d 0a 20 20 20 20  TK_CASE: {..    
2b5e3 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2b5e4 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 43 41  Printf(pOut, "CA
2b5e5 53 45 28 22 29 3b 0d 0a 20 20 20 20 20 20 73 71  SE(");..      sq
2b5e6 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
2b5e7 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e 70 4c  (pOut, pExpr->pL
2b5e8 65 66 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  eft);..      sql
2b5e9 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74  ite3ExplainPrint
2b5ea 66 28 70 4f 75 74 2c 20 22 2c 22 29 3b 0d 0a 20  f(pOut, ",");.. 
2b5eb 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2b5ec 61 69 6e 45 78 70 72 4c 69 73 74 28 70 4f 75 74  ainExprList(pOut
2b5ed 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
2b5ee 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
2b5ef 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66  ..    }..#ifndef
2b5f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2b5f1 47 47 45 52 0d 0a 20 20 20 20 63 61 73 65 20 54  GGER..    case T
2b5f2 4b 5f 52 41 49 53 45 3a 20 7b 0d 0a 20 20 20 20  K_RAISE: {..    
2b5f3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2b5f4 79 70 65 20 3d 20 22 75 6e 6b 22 3b 0d 0a 20 20  ype = "unk";..  
2b5f5 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78 70      switch( pExp
2b5f6 72 2d 3e 61 66 66 69 6e 69 74 79 20 29 7b 0d 0a  r->affinity ){..
2b5f7 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f          case OE_
2b5f8 52 6f 6c 6c 62 61 63 6b 3a 20 20 20 7a 54 79 70  Rollback:   zTyp
2b5f9 65 20 3d 20 22 72 6f 6c 6c 62 61 63 6b 22 3b 20  e = "rollback"; 
2b5fa 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
2b5fb 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 20   case OE_Abort: 
2b5fc 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 61 62       zType = "ab
2b5fd 6f 72 74 22 3b 20 20 20 20 20 62 72 65 61 6b 3b  ort";     break;
2b5fe 0d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 4f  ..        case O
2b5ff 45 5f 46 61 69 6c 3a 20 20 20 20 20 20 20 7a 54  E_Fail:       zT
2b600 79 70 65 20 3d 20 22 66 61 69 6c 22 3b 20 20 20  ype = "fail";   
2b601 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
2b602 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
2b603 65 3a 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  e:     zType = "
2b604 69 67 6e 6f 72 65 22 3b 20 20 20 20 62 72 65 61  ignore";    brea
2b605 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
2b606 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b607 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 52  nPrintf(pOut, "R
2b608 41 49 53 45 2d 25 73 28 25 73 29 22 2c 20 7a 54  AISE-%s(%s)", zT
2b609 79 70 65 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ype, pExpr->u.zT
2b60a 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 62 72  oken);..      br
2b60b 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  eak;..    }..#en
2b60c 64 69 66 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  dif..  }..  if( 
2b60d 7a 42 69 6e 4f 70 20 29 7b 0d 0a 20 20 20 20 73  zBinOp ){..    s
2b60e 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69  qlite3ExplainPri
2b60f 6e 74 66 28 70 4f 75 74 2c 22 25 73 28 22 2c 20  ntf(pOut,"%s(", 
2b610 7a 42 69 6e 4f 70 29 3b 0d 0a 20 20 20 20 73 71  zBinOp);..    sq
2b611 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
2b612 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e 70 4c  (pOut, pExpr->pL
2b613 65 66 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  eft);..    sqlit
2b614 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
2b615 70 4f 75 74 2c 22 2c 22 29 3b 0d 0a 20 20 20 20  pOut,",");..    
2b616 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
2b617 70 72 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  pr(pOut, pExpr->
2b618 70 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 73 71  pRight);..    sq
2b619 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
2b61a 74 66 28 70 4f 75 74 2c 22 29 22 29 3b 0d 0a 20  tf(pOut,")");.. 
2b61b 20 7d 65 6c 73 65 20 69 66 28 20 7a 55 6e 69 4f   }else if( zUniO
2b61c 70 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  p ){..    sqlite
2b61d 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70  3ExplainPrintf(p
2b61e 4f 75 74 2c 22 25 73 28 22 2c 20 7a 55 6e 69 4f  Out,"%s(", zUniO
2b61f 70 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  p);..    sqlite3
2b620 45 78 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74  ExplainExpr(pOut
2b621 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
2b622 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
2b623 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c  lainPrintf(pOut,
2b624 22 29 22 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23  ")");..  }..}..#
2b625 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
2b626 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54  (SQLITE_ENABLE_T
2b627 52 45 45 5f 45 58 50 4c 41 49 4e 29 20 2a 2f 0d  REE_EXPLAIN) */.
2b628 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  ...#if defined(S
2b629 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45  QLITE_ENABLE_TRE
2b62a 45 5f 45 58 50 4c 41 49 4e 29 0d 0a 2f 2a 0d 0a  E_EXPLAIN)../*..
2b62b 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 68 75  ** Generate a hu
2b62c 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 78 70  man-readable exp
2b62d 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 61 6e 20 65  lanation of an e
2b62e 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0d  xpression list..
2b62f 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2b630 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2b631 45 78 70 6c 61 69 6e 45 78 70 72 4c 69 73 74 28  ExplainExprList(
2b632 56 64 62 65 20 2a 70 4f 75 74 2c 20 45 78 70 72  Vdbe *pOut, Expr
2b633 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0d 0a 20  List *pList){.. 
2b634 20 69 6e 74 20 69 3b 0d 0a 20 20 69 66 28 20 70   int i;..  if( p
2b635 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 4c 69 73 74  List==0 || pList
2b636 2d 3e 6e 45 78 70 72 3d 3d 30 20 29 7b 0d 0a 20  ->nExpr==0 ){.. 
2b637 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
2b638 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 28  nPrintf(pOut, "(
2b639 65 6d 70 74 79 2d 6c 69 73 74 29 22 29 3b 0d 0a  empty-list)");..
2b63a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
2b63b 65 6c 73 65 20 69 66 28 20 70 4c 69 73 74 2d 3e  else if( pList->
2b63c 6e 45 78 70 72 3d 3d 31 20 29 7b 0d 0a 20 20 20  nExpr==1 ){..   
2b63d 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
2b63e 78 70 72 28 70 4f 75 74 2c 20 70 4c 69 73 74 2d  xpr(pOut, pList-
2b63f 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0d 0a 20  >a[0].pExpr);.. 
2b640 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
2b641 69 74 65 33 45 78 70 6c 61 69 6e 50 75 73 68 28  ite3ExplainPush(
2b642 70 4f 75 74 29 3b 0d 0a 20 20 20 20 66 6f 72 28  pOut);..    for(
2b643 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
2b644 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  xpr; i++){..    
2b645 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2b646 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 69 74  Printf(pOut, "it
2b647 65 6d 5b 25 64 5d 20 3d 20 22 2c 20 69 29 3b 0d  em[%d] = ", i);.
2b648 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b649 70 6c 61 69 6e 50 75 73 68 28 70 4f 75 74 29 3b  plainPush(pOut);
2b64a 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2b64b 78 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74 2c  xplainExpr(pOut,
2b64c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
2b64d 70 72 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  pr);..      sqli
2b64e 74 65 33 45 78 70 6c 61 69 6e 50 6f 70 28 70 4f  te3ExplainPop(pO
2b64f 75 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ut);..      if( 
2b650 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31  i<pList->nExpr-1
2b651 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
2b652 69 74 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 4f  ite3ExplainNL(pO
2b653 75 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ut);..      }.. 
2b654 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
2b655 33 45 78 70 6c 61 69 6e 50 6f 70 28 70 4f 75 74  3ExplainPop(pOut
2b656 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64  );..  }..}..#end
2b657 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42  if /* SQLITE_DEB
2b658 55 47 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  UG */..../*..** 
2b659 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 70  Return TRUE if p
2b65a 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74  Expr is an const
2b65b 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ant expression t
2b65c 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61  hat is appropria
2b65d 74 65 0d 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f  te..** for facto
2b65e 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f  ring out of a lo
2b65f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65  op.  Appropriate
2b660 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
2b661 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 2a 20 20  :..**..**    *  
2b662 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  Any expression t
2b663 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f  hat evaluates to
2b664 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63   two or more opc
2b665 6f 64 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  odes...**..**   
2b666 20 2a 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67   *  Any OP_Integ
2b667 65 72 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f  er, OP_Real, OP_
2b668 53 74 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c  String, OP_Blob,
2b669 20 4f 50 5f 4e 75 6c 6c 2c 20 0d 0a 2a 2a 20 20   OP_Null, ..**  
2b66a 20 20 20 20 20 6f 72 20 4f 50 5f 56 61 72 69 61       or OP_Varia
2b66b 62 6c 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ble that does no
2b66c 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61  t need to be pla
2b66d 63 65 64 20 69 6e 20 61 20 0d 0a 2a 2a 20 20 20  ced in a ..**   
2b66e 20 20 20 20 73 70 65 63 69 66 69 63 20 72 65 67      specific reg
2b66f 69 73 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ister...**..** T
2b670 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74  here is no point
2b671 20 69 6e 20 66 61 63 74 6f 72 69 6e 67 20 6f 75   in factoring ou
2b672 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63  t single-instruc
2b673 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0d 0a 2a  tion constant..*
2b674 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
2b675 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c  at need to be pl
2b676 61 63 65 64 20 69 6e 20 61 20 70 61 72 74 69 63  aced in a partic
2b677 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 20 20  ular register.  
2b678 0d 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66 61  ..** We could fa
2b679 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20 62  ctor them out, b
2b67a 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c 64  ut then we would
2b67b 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20 61   end up adding a
2b67c 6e 0d 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69  n..** OP_SCopy i
2b67d 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f  nstruction to mo
2b67e 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74  ve the value int
2b67f 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65  o the correct re
2b680 67 69 73 74 65 72 0d 0a 2a 2a 20 6c 61 74 65 72  gister..** later
2b681 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77  .  We might as w
2b682 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65  ell just use the
2b683 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75   original instru
2b684 63 74 69 6f 6e 20 61 6e 64 0d 0a 2a 2a 20 61 76  ction and..** av
2b685 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79  oid the OP_SCopy
2b686 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
2b687 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 65 46  t isAppropriateF
2b688 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72  orFactoring(Expr
2b689 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 21 73 71   *p){..  if( !sq
2b68a 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
2b68b 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b  antNotJoin(p) ){
2b68c 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  ..    return 0; 
2b68d 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e   /* Only constan
2b68e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  t expressions ar
2b68f 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  e appropriate fo
2b690 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0d 0a  r factoring */..
2b691 20 20 7d 0d 0a 20 20 69 66 28 20 28 70 2d 3e 66    }..  if( (p->f
2b692 6c 61 67 73 20 26 20 45 50 5f 46 69 78 65 64 44  lags & EP_FixedD
2b693 65 73 74 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  est)==0 ){..    
2b694 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e  return 1;  /* An
2b695 79 20 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f  y constant witho
2b696 75 74 20 61 20 66 69 78 65 64 20 64 65 73 74 69  ut a fixed desti
2b697 6e 61 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70  nation is approp
2b698 72 69 61 74 65 20 2a 2f 0d 0a 20 20 7d 0d 0a 20  riate */..  }.. 
2b699 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54   while( p->op==T
2b69a 4b 5f 55 50 4c 55 53 20 29 20 70 20 3d 20 70 2d  K_UPLUS ) p = p-
2b69b 3e 70 4c 65 66 74 3b 0d 0a 20 20 73 77 69 74 63  >pLeft;..  switc
2b69c 68 28 20 70 2d 3e 6f 70 20 29 7b 0d 0a 23 69 66  h( p->op ){..#if
2b69d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b69e 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0d 0a 20  _BLOB_LITERAL.. 
2b69f 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
2b6a0 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 63 61  ..#endif..    ca
2b6a1 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0d  se TK_VARIABLE:.
2b6a2 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54  .    case TK_INT
2b6a3 45 47 45 52 3a 0d 0a 20 20 20 20 63 61 73 65 20  EGER:..    case 
2b6a4 54 4b 5f 46 4c 4f 41 54 3a 0d 0a 20 20 20 20 63  TK_FLOAT:..    c
2b6a5 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0d 0a 20 20  ase TK_NULL:..  
2b6a6 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47    case TK_STRING
2b6a7 3a 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63  : {..      testc
2b6a8 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42  ase( p->op==TK_B
2b6a9 4c 4f 42 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  LOB );..      te
2b6aa 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
2b6ab 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0d 0a 20  K_VARIABLE );.. 
2b6ac 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2b6ad 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
2b6ae 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63   );..      testc
2b6af 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46  ase( p->op==TK_F
2b6b0 4c 4f 41 54 20 29 3b 0d 0a 20 20 20 20 20 20 74  LOAT );..      t
2b6b1 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
2b6b2 54 4b 5f 4e 55 4c 4c 20 29 3b 0d 0a 20 20 20 20  TK_NULL );..    
2b6b3 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f    testcase( p->o
2b6b4 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0d  p==TK_STRING );.
2b6b5 0a 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65  .      /* Single
2b6b6 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e  -instruction con
2b6b7 73 74 61 6e 74 73 20 77 69 74 68 20 61 20 66 69  stants with a fi
2b6b8 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  xed destination 
2b6b9 61 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 62 65  are..      ** be
2b6ba 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e  tter done in-lin
2b6bb 65 2e 20 20 49 66 20 77 65 20 66 61 63 74 6f 72  e.  If we factor
2b6bc 20 74 68 65 6d 2c 20 74 68 65 79 20 77 69 6c 6c   them, they will
2b6bd 20 6a 75 73 74 20 65 6e 64 0d 0a 20 20 20 20 20   just end..     
2b6be 20 2a 2a 20 75 70 20 67 65 6e 65 72 61 74 69 6e   ** up generatin
2b6bf 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20 74 6f  g an OP_SCopy to
2b6c0 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20   move the value 
2b6c1 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  to the destinati
2b6c2 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 72 65 67  on..      ** reg
2b6c3 69 73 74 65 72 2e 20 2a 2f 0d 0a 20 20 20 20 20  ister. */..     
2b6c4 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20   return 0;..    
2b6c5 7d 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55  }..    case TK_U
2b6c6 4d 49 4e 55 53 3a 20 7b 0d 0a 20 20 20 20 20 20  MINUS: {..      
2b6c7 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70  if( p->pLeft->op
2b6c8 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d  ==TK_FLOAT || p-
2b6c9 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49  >pLeft->op==TK_I
2b6ca 4e 54 45 47 45 52 20 29 7b 0d 0a 20 20 20 20 20  NTEGER ){..     
2b6cb 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
2b6cc 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65      }..      bre
2b6cd 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2b6ce 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20  default: {..    
2b6cf 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
2b6d0 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 31  .  }..  return 1
2b6d1 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
2b6d2 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6e  f pExpr is a con
2b6d3 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  stant expression
2b6d4 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
2b6d5 69 61 74 65 20 66 6f 72 0d 0a 2a 2a 20 66 61 63  iate for..** fac
2b6d6 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20  toring out of a 
2b6d7 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61 6c 75  loop, then evalu
2b6d8 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ate the expressi
2b6d9 6f 6e 0d 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65  on..** into a re
2b6da 67 69 73 74 65 72 20 61 6e 64 20 63 6f 6e 76 65  gister and conve
2b6db 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  rt the expressio
2b6dc 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49  n into a TK_REGI
2b6dd 53 54 45 52 0d 0a 2a 2a 20 65 78 70 72 65 73 73  STER..** express
2b6de 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
2b6df 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78   int evalConstEx
2b6e0 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  pr(Walker *pWalk
2b6e1 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  er, Expr *pExpr)
2b6e2 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  {..  Parse *pPar
2b6e3 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
2b6e4 61 72 73 65 3b 0d 0a 20 20 73 77 69 74 63 68 28  arse;..  switch(
2b6e5 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0d 0a 20   pExpr->op ){.. 
2b6e6 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 0d 0a     case TK_IN:..
2b6e7 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49      case TK_REGI
2b6e8 53 54 45 52 3a 20 7b 0d 0a 20 20 20 20 20 20 72  STER: {..      r
2b6e9 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2b6ea 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
2b6eb 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0d 0a  e TK_FUNCTION:..
2b6ec 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
2b6ed 46 55 4e 43 54 49 4f 4e 3a 0d 0a 20 20 20 20 63  FUNCTION:..    c
2b6ee 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  ase TK_CONST_FUN
2b6ef 43 3a 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54  C: {..      /* T
2b6f0 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  he arguments to 
2b6f1 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20  a function have 
2b6f2 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
2b6f3 69 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 4d  ion...      ** M
2b6f4 61 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61  ark them this wa
2b6f5 79 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72  y to avoid gener
2b6f6 61 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50  ated unneeded OP
2b6f7 5f 53 43 6f 70 79 0d 0a 20 20 20 20 20 20 2a 2a  _SCopy..      **
2b6f8 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0d   instructions. .
2b6f9 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
2b6fa 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2b6fb 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
2b6fc 74 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  t;..      assert
2b6fd 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2b6fe 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2b6ff 53 65 6c 65 63 74 29 20 29 3b 0d 0a 20 20 20 20  Select) );..    
2b700 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0d 0a    if( pList ){..
2b701 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20          int i = 
2b702 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0d 0a 20  pList->nExpr;.. 
2b703 20 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78         struct Ex
2b704 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2b705 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0d 0a  em = pList->a;..
2b706 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e          for(; i>
2b707 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
2b708 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  {..          if(
2b709 20 41 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e 70   ALWAYS(pItem->p
2b70a 45 78 70 72 29 20 29 20 70 49 74 65 6d 2d 3e 70  Expr) ) pItem->p
2b70b 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
2b70c 50 5f 46 69 78 65 64 44 65 73 74 3b 0d 0a 20 20  P_FixedDest;..  
2b70d 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
2b70e 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2b70f 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
2b710 28 20 69 73 41 70 70 72 6f 70 72 69 61 74 65 46  ( isAppropriateF
2b711 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 45 78 70  orFactoring(pExp
2b712 72 29 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 72  r) ){..    int r
2b713 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  1 = ++pParse->nM
2b714 65 6d 3b 0d 0a 20 20 20 20 69 6e 74 20 72 32 3b  em;..    int r2;
2b715 0d 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74  ..    r2 = sqlit
2b716 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
2b717 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
2b718 72 31 29 3b 0d 0a 20 20 20 20 69 66 28 20 4e 45  r1);..    if( NE
2b719 56 45 52 28 72 31 21 3d 72 32 29 20 29 20 73 71  VER(r1!=r2) ) sq
2b71a 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2b71b 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2b71c 0d 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 32  ..    pExpr->op2
2b71d 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0d 0a 20   = pExpr->op;.. 
2b71e 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
2b71f 4b 5f 52 45 47 49 53 54 45 52 3b 0d 0a 20 20 20  K_REGISTER;..   
2b720 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
2b721 20 72 32 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e   r2;..    return
2b722 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a 20 20 7d   WRC_Prune;..  }
2b723 0d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  ..  return WRC_C
2b724 6f 6e 74 69 6e 75 65 3b 0d 0a 7d 0d 0a 0d 0a 2f  ontinue;..}..../
2b725 2a 0d 0a 2a 2a 20 50 72 65 65 76 61 6c 75 61 74  *..** Preevaluat
2b726 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78  e constant subex
2b727 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68 69 6e  pressions within
2b728 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65   pExpr and store
2b729 20 74 68 65 0d 0a 2a 2a 20 72 65 73 75 6c 74 73   the..** results
2b72a 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20 20   in registers.  
2b72b 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f 20  Modify pExpr so 
2b72c 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61 6e  that the constan
2b72d 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73 0d  t subexpresions.
2b72e 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53  .** are TK_REGIS
2b72f 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74  TER opcodes that
2b730 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 72   refer to the pr
2b731 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73  ecomputed values
2b732 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
2b733 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
2b734 70 20 69 66 20 74 68 65 20 6a 75 6d 70 20 74 6f  p if the jump to
2b735 20 74 68 65 20 63 6f 6f 6b 69 65 2d 63 68 65 63   the cookie-chec
2b736 6b 20 63 6f 64 65 20 68 61 73 0d 0a 2a 2a 20 61  k code has..** a
2b737 6c 72 65 61 64 79 20 6f 63 63 75 72 2e 20 20 53  lready occur.  S
2b738 69 6e 63 65 20 74 68 65 20 63 6f 6f 6b 69 65 2d  ince the cookie-
2b739 63 68 65 63 6b 20 6a 75 6d 70 20 69 73 20 67 65  check jump is ge
2b73a 6e 65 72 61 74 65 64 20 70 72 69 6f 72 20 74 6f  nerated prior to
2b73b 0d 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 73  ..** any other s
2b73c 65 72 69 6f 75 73 20 70 72 6f 63 65 73 73 69 6e  erious processin
2b73d 67 2c 20 74 68 69 73 20 63 68 65 63 6b 20 65 6e  g, this check en
2b73e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 72 65  sures that there
2b73f 20 69 73 20 6e 6f 0d 0a 2a 2a 20 77 61 79 20 74   is no..** way t
2b740 6f 20 61 63 63 69 64 65 6e 74 6c 79 20 62 79 70  o accidently byp
2b741 61 73 73 20 74 68 65 20 63 6f 6e 73 74 61 6e 74  ass the constant
2b742 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 73   initializations
2b743 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
2b744 6f 75 74 69 6e 65 20 69 73 20 61 6c 73 6f 20 61  outine is also a
2b745 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 53 51   no-op if the SQ
2b746 4c 49 54 45 5f 46 61 63 74 6f 72 4f 75 74 43 6f  LITE_FactorOutCo
2b747 6e 73 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  nst optimization
2b748 0d 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64  ..** is disabled
2b749 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33   via the sqlite3
2b74a 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51  _test_control(SQ
2b74b 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50  LITE_TESTCTRL_OP
2b74c 54 49 4d 49 5a 41 54 49 4f 4e 53 29 0d 0a 2a 2a  TIMIZATIONS)..**
2b74d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 69   interface.  Thi
2b74e 73 20 61 6c 6c 6f 77 73 20 74 65 73 74 20 6c 6f  s allows test lo
2b74f 67 69 63 20 74 6f 20 76 65 72 69 66 79 20 74 68  gic to verify th
2b750 61 74 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77  at the same answ
2b751 65 72 20 69 73 0d 0a 2a 2a 20 6f 62 74 61 69 6e  er is..** obtain
2b752 65 64 20 66 6f 72 20 71 75 65 72 69 65 73 20 72  ed for queries r
2b753 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
2b754 74 68 65 72 20 6f 72 20 6e 6f 74 20 63 6f 6e 73  ther or not cons
2b755 74 61 6e 74 73 20 61 72 65 0d 0a 2a 2a 20 70 72  tants are..** pr
2b756 65 63 6f 6d 70 75 74 65 64 20 69 6e 74 6f 20 72  ecomputed into r
2b757 65 67 69 73 74 65 72 73 20 6f 72 20 69 66 20 74  egisters or if t
2b758 68 65 79 20 61 72 65 20 69 6e 73 65 72 74 65 64  hey are inserted
2b759 20 69 6e 2d 6c 69 6e 65 2e 0d 0a 2a 2f 0d 0a 53   in-line...*/..S
2b75a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2b75b 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
2b75c 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73  deConstants(Pars
2b75d 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
2b75e 2a 70 45 78 70 72 29 7b 0d 0a 20 20 57 61 6c 6b  *pExpr){..  Walk
2b75f 65 72 20 77 3b 0d 0a 20 20 69 66 28 20 70 50 61  er w;..  if( pPa
2b760 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20  rse->cookieGoto 
2b761 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 69 66 28  ) return;..  if(
2b762 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c   (pParse->db->fl
2b763 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 61 63  ags & SQLITE_Fac
2b764 74 6f 72 4f 75 74 43 6f 6e 73 74 29 21 3d 30 20  torOutConst)!=0 
2b765 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 77 2e 78  ) return;..  w.x
2b766 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
2b767 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0d 0a 20  valConstExpr;.. 
2b768 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
2b769 63 6b 20 3d 20 30 3b 0d 0a 20 20 77 2e 70 50 61  ck = 0;..  w.pPa
2b76a 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20  rse = pParse;.. 
2b76b 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
2b76c 28 26 77 2c 20 70 45 78 70 72 29 3b 0d 0a 7d 0d  (&w, pExpr);..}.
2b76d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65  ...../*..** Gene
2b76e 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 70  rate code that p
2b76f 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 65 20  ushes the value 
2b770 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74  of every element
2b771 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0d 0a 2a   of the given..*
2b772 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  * expression lis
2b773 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63  t into a sequenc
2b774 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 62  e of registers b
2b775 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 67  eginning at targ
2b776 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75  et...**..** Retu
2b777 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
2b778 20 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61   elements evalua
2b779 74 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ted...*/..SQLITE
2b77a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2b77b 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2b77c 4c 69 73 74 28 0d 0a 20 20 50 61 72 73 65 20 2a  List(..  Parse *
2b77d 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
2b77e 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2b77f 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  /..  ExprList *p
2b780 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 65  List,   /* The e
2b781 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74  xpression list t
2b782 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0d 0a 20  o be coded */.. 
2b783 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20   int target,    
2b784 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
2b785 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f  write results */
2b786 0d 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f  ..  int doHardCo
2b787 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61  py     /* Make a
2b788 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76   hard copy of ev
2b789 65 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0d 0a  ery element */..
2b78a 29 7b 0d 0a 20 20 73 74 72 75 63 74 20 45 78 70  ){..  struct Exp
2b78b 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2b78c 6d 3b 0d 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0d  m;..  int i, n;.
2b78d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
2b78e 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  !=0 );..  assert
2b78f 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0d 0a 20  ( target>0 );.. 
2b790 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2b791 3e 70 56 64 62 65 21 3d 30 20 29 3b 20 20 2f 2a  >pVdbe!=0 );  /*
2b792 20 4e 65 76 65 72 20 67 65 74 73 20 74 68 69 73   Never gets this
2b793 20 66 61 72 20 6f 74 68 65 72 77 69 73 65 20 2a   far otherwise *
2b794 2f 0d 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e  /..  n = pList->
2b795 6e 45 78 70 72 3b 0d 0a 20 20 66 6f 72 28 70 49  nExpr;..  for(pI
2b796 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d  tem=pList->a, i=
2b797 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74  0; i<n; i++, pIt
2b798 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 45 78 70 72  em++){..    Expr
2b799 20 2a 70 45 78 70 72 20 3d 20 70 49 74 65 6d 2d   *pExpr = pItem-
2b79a 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 69 6e 74  >pExpr;..    int
2b79b 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
2b79c 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2b79d 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
2b79e 72 67 65 74 2b 69 29 3b 0d 0a 20 20 20 20 69 66  rget+i);..    if
2b79f 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 2b  ( inReg!=target+
2b7a0 69 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  i ){..      sqli
2b7a1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50  te3VdbeAddOp2(pP
2b7a2 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 64 6f 48  arse->pVdbe, doH
2b7a3 61 72 64 43 6f 70 79 20 3f 20 4f 50 5f 43 6f 70  ardCopy ? OP_Cop
2b7a4 79 20 3a 20 4f 50 5f 53 43 6f 70 79 2c 0d 0a 20  y : OP_SCopy,.. 
2b7a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7a6 20 20 20 20 20 20 20 69 6e 52 65 67 2c 20 74 61         inReg, ta
2b7a7 72 67 65 74 2b 69 29 3b 0d 0a 20 20 20 20 7d 0d  rget+i);..    }.
2b7a8 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6e  .  }..  return n
2b7a9 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  ;..}..../*..** G
2b7aa 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2b7ab 20 61 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61   a BETWEEN opera
2b7ac 74 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  tor...**..**    
2b7ad 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20  x BETWEEN y AND 
2b7ae 7a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 62  z..**..** The ab
2b7af 6f 76 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e  ove is equivalen
2b7b0 74 20 74 6f 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  t to ..**..**   
2b7b1 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0d 0a   x>=y AND x<=z..
2b7b2 2a 2a 0d 0a 2a 2a 20 43 6f 64 65 20 69 74 20 61  **..** Code it a
2b7b3 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63  s such, taking c
2b7b4 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  are to do the co
2b7b5 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69  mmon subexpressi
2b7b6 6f 6e 0d 0a 2a 2a 20 65 6c 65 6d 65 6e 74 61 74  on..** elementat
2b7b7 69 6f 6e 20 6f 66 20 78 2e 0d 0a 2a 2f 0d 0a 73  ion of x...*/..s
2b7b8 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 43  tatic void exprC
2b7b9 6f 64 65 42 65 74 77 65 65 6e 28 0d 0a 20 20 50  odeBetween(..  P
2b7ba 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2b7bb 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20   /* Parsing and 
2b7bc 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
2b7bd 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78  context */..  Ex
2b7be 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
2b7bf 2f 2a 20 54 68 65 20 42 45 54 57 45 45 4e 20 65  /* The BETWEEN e
2b7c0 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20  xpression */..  
2b7c1 69 6e 74 20 64 65 73 74 2c 20 20 20 20 20 20 20  int dest,       
2b7c2 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
2b7c3 66 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  f the jump is ta
2b7c4 6b 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6a 75  ken */..  int ju
2b7c5 6d 70 49 66 54 72 75 65 2c 20 20 20 2f 2a 20 54  mpIfTrue,   /* T
2b7c6 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20  ake the jump if 
2b7c7 74 68 65 20 42 45 54 57 45 45 4e 20 69 73 20 74  the BETWEEN is t
2b7c8 72 75 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6a 75  rue */..  int ju
2b7c9 6d 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 54  mpIfNull    /* T
2b7ca 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20  ake the jump if 
2b7cb 74 68 65 20 42 45 54 57 45 45 4e 20 69 73 20 4e  the BETWEEN is N
2b7cc 55 4c 4c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 45 78  ULL */..){..  Ex
2b7cd 70 72 20 65 78 70 72 41 6e 64 3b 20 20 20 20 20  pr exprAnd;     
2b7ce 2f 2a 20 54 68 65 20 41 4e 44 20 6f 70 65 72 61  /* The AND opera
2b7cf 74 6f 72 20 69 6e 20 20 78 3e 3d 79 20 41 4e 44  tor in  x>=y AND
2b7d0 20 78 3c 3d 7a 20 20 2a 2f 0d 0a 20 20 45 78 70   x<=z  */..  Exp
2b7d1 72 20 63 6f 6d 70 4c 65 66 74 3b 20 20 20 20 2f  r compLeft;    /
2b7d2 2a 20 54 68 65 20 20 78 3e 3d 79 20 20 74 65 72  * The  x>=y  ter
2b7d3 6d 20 2a 2f 0d 0a 20 20 45 78 70 72 20 63 6f 6d  m */..  Expr com
2b7d4 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 54 68 65  pRight;   /* The
2b7d5 20 20 78 3c 3d 7a 20 20 74 65 72 6d 20 2a 2f 0d    x<=z  term */.
2b7d6 0a 20 20 45 78 70 72 20 65 78 70 72 58 3b 20 20  .  Expr exprX;  
2b7d7 20 20 20 20 20 2f 2a 20 54 68 65 20 20 78 20 20       /* The  x  
2b7d8 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  subexpression */
2b7d9 0d 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31  ..  int regFree1
2b7da 20 3d 20 30 3b 20 2f 2a 20 54 65 6d 70 6f 72 61   = 0; /* Tempora
2b7db 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20  ry use register 
2b7dc 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
2b7dd 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2b7de 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
2b7df 6c 65 63 74 29 20 29 3b 0d 0a 20 20 65 78 70 72  lect) );..  expr
2b7e0 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66  X = *pExpr->pLef
2b7e1 74 3b 0d 0a 20 20 65 78 70 72 41 6e 64 2e 6f 70  t;..  exprAnd.op
2b7e2 20 3d 20 54 4b 5f 41 4e 44 3b 0d 0a 20 20 65 78   = TK_AND;..  ex
2b7e3 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63  prAnd.pLeft = &c
2b7e4 6f 6d 70 4c 65 66 74 3b 0d 0a 20 20 65 78 70 72  ompLeft;..  expr
2b7e5 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f  And.pRight = &co
2b7e6 6d 70 52 69 67 68 74 3b 0d 0a 20 20 63 6f 6d 70  mpRight;..  comp
2b7e7 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b  Left.op = TK_GE;
2b7e8 0d 0a 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65  ..  compLeft.pLe
2b7e9 66 74 20 3d 20 26 65 78 70 72 58 3b 0d 0a 20 20  ft = &exprX;..  
2b7ea 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20  compLeft.pRight 
2b7eb 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2b7ec 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0d 0a 20  ->a[0].pExpr;.. 
2b7ed 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20   compRight.op = 
2b7ee 54 4b 5f 4c 45 3b 0d 0a 20 20 63 6f 6d 70 52 69  TK_LE;..  compRi
2b7ef 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  ght.pLeft = &exp
2b7f0 72 58 3b 0d 0a 20 20 63 6f 6d 70 52 69 67 68 74  rX;..  compRight
2b7f1 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
2b7f2 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  >x.pList->a[1].p
2b7f3 45 78 70 72 3b 0d 0a 20 20 65 78 70 72 58 2e 69  Expr;..  exprX.i
2b7f4 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45  Table = sqlite3E
2b7f5 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2b7f6 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67  se, &exprX, &reg
2b7f7 46 72 65 65 31 29 3b 0d 0a 20 20 65 78 70 72 58  Free1);..  exprX
2b7f8 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  .op = TK_REGISTE
2b7f9 52 3b 0d 0a 20 20 69 66 28 20 6a 75 6d 70 49 66  R;..  if( jumpIf
2b7fa 54 72 75 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c  True ){..    sql
2b7fb 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
2b7fc 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 2c  Parse, &exprAnd,
2b7fd 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
2b7fe 6c 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  l);..  }else{.. 
2b7ff 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
2b800 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65  False(pParse, &e
2b801 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75  xprAnd, dest, ju
2b802 6d 70 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 7d 0d  mpIfNull);..  }.
2b803 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2b804 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2b805 20 72 65 67 46 72 65 65 31 29 3b 0d 0a 0d 0a 20   regFree1);.... 
2b806 20 2f 2a 20 45 6e 73 75 72 65 20 61 64 65 71 75   /* Ensure adequ
2b807 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 67  ate test coverag
2b808 65 20 2a 2f 0d 0a 20 20 74 65 73 74 63 61 73 65  e */..  testcase
2b809 28 20 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30 20  ( jumpIfTrue==0 
2b80a 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  && jumpIfNull==0
2b80b 20 26 26 20 72 65 67 46 72 65 65 31 3d 3d 30 20   && regFree1==0 
2b80c 29 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  );..  testcase( 
2b80d 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30 20 26 26  jumpIfTrue==0 &&
2b80e 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 26   jumpIfNull==0 &
2b80f 26 20 72 65 67 46 72 65 65 31 21 3d 30 20 29 3b  & regFree1!=0 );
2b810 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a 75  ..  testcase( ju
2b811 6d 70 49 66 54 72 75 65 3d 3d 30 20 26 26 20 6a  mpIfTrue==0 && j
2b812 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 20 26 26 20  umpIfNull!=0 && 
2b813 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0d 0a  regFree1==0 );..
2b814 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2b815 49 66 54 72 75 65 3d 3d 30 20 26 26 20 6a 75 6d  IfTrue==0 && jum
2b816 70 49 66 4e 75 6c 6c 21 3d 30 20 26 26 20 72 65  pIfNull!=0 && re
2b817 67 46 72 65 65 31 21 3d 30 20 29 3b 0d 0a 20 20  gFree1!=0 );..  
2b818 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
2b819 54 72 75 65 21 3d 30 20 26 26 20 6a 75 6d 70 49  True!=0 && jumpI
2b81a 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46  fNull==0 && regF
2b81b 72 65 65 31 3d 3d 30 20 29 3b 0d 0a 20 20 74 65  ree1==0 );..  te
2b81c 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72  stcase( jumpIfTr
2b81d 75 65 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e  ue!=0 && jumpIfN
2b81e 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72 65  ull==0 && regFre
2b81f 65 31 21 3d 30 20 29 3b 0d 0a 20 20 74 65 73 74  e1!=0 );..  test
2b820 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65  case( jumpIfTrue
2b821 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c  !=0 && jumpIfNul
2b822 6c 21 3d 30 20 26 26 20 72 65 67 46 72 65 65 31  l!=0 && regFree1
2b823 3d 3d 30 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  ==0 );..  testca
2b824 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65 21 3d  se( jumpIfTrue!=
2b825 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21  0 && jumpIfNull!
2b826 3d 30 20 26 26 20 72 65 67 46 72 65 65 31 21 3d  =0 && regFree1!=
2b827 30 20 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  0 );..}..../*..*
2b828 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2b829 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78  for a boolean ex
2b82a 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68  pression such th
2b82b 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64  at a jump is mad
2b82c 65 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62  e..** to the lab
2b82d 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65  el "dest" if the
2b82e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
2b82f 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f  rue but executio
2b830 6e 0d 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20  n..** continues 
2b831 73 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66  straight thru if
2b832 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2b833 69 73 20 66 61 6c 73 65 2e 0d 0a 2a 2a 0d 0a 2a  is false...**..*
2b834 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
2b835 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f  ion evaluates to
2b836 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74   NULL (neither t
2b837 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20  rue nor false), 
2b838 74 68 65 6e 0d 0a 2a 2a 20 74 61 6b 65 20 74 68  then..** take th
2b839 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a 75  e jump if the ju
2b83a 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69 73  mpIfNull flag is
2b83b 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2b83c 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  LL...**..** This
2b83d 20 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e   code depends on
2b83e 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63   the fact that c
2b83f 65 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c  ertain token val
2b840 75 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0d  ues (ex: TK_EQ).
2b841 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65  .** are the same
2b842 20 61 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65   as opcode value
2b843 73 20 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68  s (ex: OP_Eq) th
2b844 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
2b845 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0d 0a   corresponding..
2b846 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53  ** operation.  S
2b847 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20  pecial comments 
2b848 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 20 74 68  in vdbe.c and th
2b849 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20  e mkopcodeh.awk 
2b84a 73 63 72 69 70 74 20 69 6e 0d 0a 2a 2a 20 74 68  script in..** th
2b84b 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 20 63  e make process c
2b84c 61 75 73 65 20 74 68 65 73 65 20 76 61 6c 75 65  ause these value
2b84d 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 73 73  s to align.  Ass
2b84e 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 63 6f  ert()s in the co
2b84f 64 65 0d 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 72  de..** below ver
2b850 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d  ify that the num
2b851 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 64  bers are aligned
2b852 20 63 6f 72 72 65 63 74 6c 79 2e 0d 0a 2a 2f 0d   correctly...*/.
2b853 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2b854 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
2b855 49 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50  IfTrue(Parse *pP
2b856 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
2b857 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74  r, int dest, int
2b858 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0d 0a 20   jumpIfNull){.. 
2b859 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2b85a 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74  e->pVdbe;..  int
2b85b 20 6f 70 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20   op = 0;..  int 
2b85c 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0d 0a 20  regFree1 = 0;.. 
2b85d 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
2b85e 30 3b 0d 0a 20 20 69 6e 74 20 72 31 2c 20 72 32  0;..  int r1, r2
2b85f 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 6a  ;....  assert( j
2b860 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54  umpIfNull==SQLIT
2b861 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20  E_JUMPIFNULL || 
2b862 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
2b863 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d  ..  if( NEVER(v=
2b864 3d 30 29 20 29 20 20 20 20 20 72 65 74 75 72 6e  =0) )     return
2b865 3b 20 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20  ;  /* Existance 
2b866 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 64 20  of VDBE checked 
2b867 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0d 0a 20 20  by caller */..  
2b868 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 3d  if( NEVER(pExpr=
2b869 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  =0) ) return;  /
2b86a 2a 20 4e 6f 20 77 61 79 20 74 68 69 73 20 63 61  * No way this ca
2b86b 6e 20 68 61 70 70 65 6e 20 2a 2f 0d 0a 20 20 6f  n happen */..  o
2b86c 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0d 0a  p = pExpr->op;..
2b86d 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0d    switch( op ){.
2b86e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
2b86f 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 64  : {..      int d
2b870 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  2 = sqlite3VdbeM
2b871 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20  akeLabel(v);..  
2b872 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
2b873 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0d 0a  mpIfNull==0 );..
2b874 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b875 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
2b876 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  e);..      sqlit
2b877 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
2b878 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
2b879 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 6c  ft, d2,jumpIfNul
2b87a 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  l^SQLITE_JUMPIFN
2b87b 55 4c 4c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  ULL);..      sql
2b87c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
2b87d 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
2b87e 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
2b87f 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20  IfNull);..      
2b880 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2b881 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0d  veLabel(v, d2);.
2b882 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b883 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
2b884 65 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 62 72  e, 1);..      br
2b885 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2b886 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b 0d 0a   case TK_OR: {..
2b887 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b888 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
2b889 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2b88a 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
2b88b 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2b88c 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
2b88d 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
2b88e 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
2b88f 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
2b890 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
2b891 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  ull);..      bre
2b892 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2b893 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0d 0a  case TK_NOT: {..
2b894 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b895 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
2b896 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2b897 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
2b898 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
2b899 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
2b89a 6c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  l);..      break
2b89b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2b89c 73 65 20 54 4b 5f 4c 54 3a 0d 0a 20 20 20 20 63  se TK_LT:..    c
2b89d 61 73 65 20 54 4b 5f 4c 45 3a 0d 0a 20 20 20 20  ase TK_LE:..    
2b89e 63 61 73 65 20 54 4b 5f 47 54 3a 0d 0a 20 20 20  case TK_GT:..   
2b89f 20 63 61 73 65 20 54 4b 5f 47 45 3a 0d 0a 20 20   case TK_GE:..  
2b8a0 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0d 0a 20    case TK_NE:.. 
2b8a1 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b     case TK_EQ: {
2b8a2 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2b8a3 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0d  TK_LT==OP_Lt );.
2b8a4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2b8a5 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0d 0a  K_LE==OP_Le );..
2b8a6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
2b8a7 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0d 0a 20  _GT==OP_Gt );.. 
2b8a8 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
2b8a9 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0d 0a 20 20  GE==OP_Ge );..  
2b8aa 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45      assert( TK_E
2b8ab 51 3d 3d 4f 50 5f 45 71 20 29 3b 0d 0a 20 20 20  Q==OP_Eq );..   
2b8ac 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45     assert( TK_NE
2b8ad 3d 3d 4f 50 5f 4e 65 20 29 3b 0d 0a 20 20 20 20  ==OP_Ne );..    
2b8ae 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2b8af 54 4b 5f 4c 54 20 29 3b 0d 0a 20 20 20 20 20 20  TK_LT );..      
2b8b0 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
2b8b1 5f 4c 45 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  _LE );..      te
2b8b2 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
2b8b3 54 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  T );..      test
2b8b4 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20  case( op==TK_GE 
2b8b5 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
2b8b6 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b  se( op==TK_EQ );
2b8b7 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2b8b8 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0d 0a  ( op==TK_NE );..
2b8b9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b8ba 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
2b8bb 0d 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ..      r1 = sql
2b8bc 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
2b8bd 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2b8be 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
2b8bf 29 3b 0d 0a 20 20 20 20 20 20 72 32 20 3d 20 73  );..      r2 = s
2b8c0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
2b8c1 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
2b8c2 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72  ->pRight, &regFr
2b8c3 65 65 32 29 3b 0d 0a 20 20 20 20 20 20 63 6f 64  ee2);..      cod
2b8c4 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
2b8c5 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
2b8c6 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
2b8c7 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
2b8c8 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73       r1, r2, des
2b8c9 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0d  t, jumpIfNull);.
2b8ca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b8cb 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0d   regFree1==0 );.
2b8cc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b8cd 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0d   regFree2==0 );.
2b8ce 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
2b8cf 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 54     }..    case T
2b8d0 4b 5f 49 53 3a 0d 0a 20 20 20 20 63 61 73 65 20  K_IS:..    case 
2b8d1 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0d 0a 20 20 20  TK_ISNOT: {..   
2b8d2 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2b8d3 3d 54 4b 5f 49 53 20 29 3b 0d 0a 20 20 20 20 20  =TK_IS );..     
2b8d4 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2b8d5 4b 5f 49 53 4e 4f 54 20 29 3b 0d 0a 20 20 20 20  K_ISNOT );..    
2b8d6 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
2b8d7 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2b8d8 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
2b8d9 20 26 72 65 67 46 72 65 65 31 29 3b 0d 0a 20 20   &regFree1);..  
2b8da 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
2b8db 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
2b8dc 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
2b8dd 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0d  ht, &regFree2);.
2b8de 0a 20 20 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d  .      op = (op=
2b8df 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20  =TK_IS) ? TK_EQ 
2b8e0 3a 20 54 4b 5f 4e 45 3b 0d 0a 20 20 20 20 20 20  : TK_NE;..      
2b8e1 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
2b8e2 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2b8e3 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
2b8e4 20 6f 70 2c 0d 0a 20 20 20 20 20 20 20 20 20 20   op,..          
2b8e5 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
2b8e6 64 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55 4c  dest, SQLITE_NUL
2b8e7 4c 45 51 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  LEQ);..      tes
2b8e8 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
2b8e9 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  =0 );..      tes
2b8ea 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
2b8eb 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  =0 );..      bre
2b8ec 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2b8ed 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0d  case TK_ISNULL:.
2b8ee 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
2b8ef 4e 55 4c 4c 3a 20 7b 0d 0a 20 20 20 20 20 20 61  NULL: {..      a
2b8f0 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c  ssert( TK_ISNULL
2b8f1 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0d 0a  ==OP_IsNull );..
2b8f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
2b8f3 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74  _NOTNULL==OP_Not
2b8f4 4e 75 6c 6c 20 29 3b 0d 0a 20 20 20 20 20 20 74  Null );..      t
2b8f5 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2b8f6 49 53 4e 55 4c 4c 20 29 3b 0d 0a 20 20 20 20 20  ISNULL );..     
2b8f7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2b8f8 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0d 0a 20 20  K_NOTNULL );..  
2b8f9 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
2b8fa 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
2b8fb 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2b8fc 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0d 0a  t, &regFree1);..
2b8fd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b8fe 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
2b8ff 31 2c 20 64 65 73 74 29 3b 0d 0a 20 20 20 20 20  1, dest);..     
2b900 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
2b901 65 65 31 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20  ee1==0 );..     
2b902 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2b903 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57      case TK_BETW
2b904 45 45 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 74 65  EEN: {..      te
2b905 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
2b906 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ll==0 );..      
2b907 65 78 70 72 43 6f 64 65 42 65 74 77 65 65 6e 28  exprCodeBetween(
2b908 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64  pParse, pExpr, d
2b909 65 73 74 2c 20 31 2c 20 6a 75 6d 70 49 66 4e 75  est, 1, jumpIfNu
2b90a 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  ll);..      brea
2b90b 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64  k;..    }..#ifnd
2b90c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2b90d 55 42 51 55 45 52 59 0d 0a 20 20 20 20 63 61 73  UBQUERY..    cas
2b90e 65 20 54 4b 5f 49 4e 3a 20 7b 0d 0a 20 20 20 20  e TK_IN: {..    
2b90f 20 20 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73    int destIfFals
2b910 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
2b911 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20  akeLabel(v);..  
2b912 20 20 20 20 69 6e 74 20 64 65 73 74 49 66 4e 75      int destIfNu
2b913 6c 6c 20 3d 20 6a 75 6d 70 49 66 4e 75 6c 6c 20  ll = jumpIfNull 
2b914 3f 20 64 65 73 74 20 3a 20 64 65 73 74 49 66 46  ? dest : destIfF
2b915 61 6c 73 65 3b 0d 0a 20 20 20 20 20 20 73 71 6c  alse;..      sql
2b916 69 74 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70  ite3ExprCodeIN(p
2b917 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65  Parse, pExpr, de
2b918 73 74 49 66 46 61 6c 73 65 2c 20 64 65 73 74 49  stIfFalse, destI
2b919 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 73  fNull);..      s
2b91a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b91b 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2b91c 64 65 73 74 29 3b 0d 0a 20 20 20 20 20 20 73 71  dest);..      sq
2b91d 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2b91e 4c 61 62 65 6c 28 76 2c 20 64 65 73 74 49 66 46  Label(v, destIfF
2b91f 61 6c 73 65 29 3b 0d 0a 20 20 20 20 20 20 62 72  alse);..      br
2b920 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  eak;..    }..#en
2b921 64 69 66 0d 0a 20 20 20 20 64 65 66 61 75 6c 74  dif..    default
2b922 3a 20 7b 0d 0a 20 20 20 20 20 20 72 31 20 3d 20  : {..      r1 = 
2b923 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
2b924 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
2b925 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0d 0a  r, &regFree1);..
2b926 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b927 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66  eAddOp3(v, OP_If
2b928 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70  , r1, dest, jump
2b929 49 66 4e 75 6c 6c 21 3d 30 29 3b 0d 0a 20 20 20  IfNull!=0);..   
2b92a 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
2b92b 46 72 65 65 31 3d 3d 30 20 29 3b 0d 0a 20 20 20  Free1==0 );..   
2b92c 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
2b92d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0d 0a 20  pIfNull==0 );.. 
2b92e 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2b92f 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74   }..  }..  sqlit
2b930 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2b931 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
2b932 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 52 65  1);..  sqlite3Re
2b933 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2b934 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20  rse, regFree2); 
2b935 20 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47   ..}..../*..** G
2b936 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2b937 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65   a boolean expre
2b938 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20  ssion such that 
2b939 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0d 0a  a jump is made..
2b93a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20  ** to the label 
2b93b 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65 78  "dest" if the ex
2b93c 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73  pression is fals
2b93d 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0d  e but execution.
2b93e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74  .** continues st
2b93f 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74  raight thru if t
2b940 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2b941 20 74 72 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   true...**..** I
2b942 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2b943 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
2b944 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65  LL (neither true
2b945 20 6e 6f 72 20 66 61 6c 73 65 29 20 74 68 65 6e   nor false) then
2b946 0d 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 6a 75 6d  ..** jump if jum
2b947 70 49 66 4e 75 6c 6c 20 69 73 20 53 51 4c 49 54  pIfNull is SQLIT
2b948 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 6f 72 20  E_JUMPIFNULL or 
2b949 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 66 20  fall through if 
2b94a 6a 75 6d 70 49 66 4e 75 6c 6c 0d 0a 2a 2a 20 69  jumpIfNull..** i
2b94b 73 20 30 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  s 0...*/..SQLITE
2b94c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2b94d 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2b94e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2b94f 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
2b950 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49   dest, int jumpI
2b951 66 4e 75 6c 6c 29 7b 0d 0a 20 20 56 64 62 65 20  fNull){..  Vdbe 
2b952 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2b953 62 65 3b 0d 0a 20 20 69 6e 74 20 6f 70 20 3d 20  be;..  int op = 
2b954 30 3b 0d 0a 20 20 69 6e 74 20 72 65 67 46 72 65  0;..  int regFre
2b955 65 31 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 72  e1 = 0;..  int r
2b956 65 67 46 72 65 65 32 20 3d 20 30 3b 0d 0a 20 20  egFree2 = 0;..  
2b957 69 6e 74 20 72 31 2c 20 72 32 3b 0d 0a 0d 0a 20  int r1, r2;.... 
2b958 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e   assert( jumpIfN
2b959 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50  ull==SQLITE_JUMP
2b95a 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66  IFNULL || jumpIf
2b95b 4e 75 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 69 66  Null==0 );..  if
2b95c 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
2b95d 72 65 74 75 72 6e 3b 20 2f 2a 20 45 78 69 73 74  return; /* Exist
2b95e 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68 65  ance of VDBE che
2b95f 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20 2a  cked by caller *
2b960 2f 0d 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  /..  if( pExpr==
2b961 30 20 29 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  0 )    return;..
2b962 0d 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65  ..  /* The value
2b963 20 6f 66 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e   of pExpr->op an
2b964 64 20 6f 70 20 61 72 65 20 72 65 6c 61 74 65 64  d op are related
2b965 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 20 20   as follows:..  
2b966 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 70  **..  **       p
2b967 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20  Expr->op        
2b968 20 20 20 20 6f 70 0d 0a 20 20 2a 2a 20 20 20 20      op..  **    
2b969 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20     ---------    
2b96a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d        ----------
2b96b 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f  ..  **       TK_
2b96c 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  ISNULL          
2b96d 4f 50 5f 4e 6f 74 4e 75 6c 6c 0d 0a 20 20 2a 2a  OP_NotNull..  **
2b96e 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c         TK_NOTNUL
2b96f 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e  L         OP_IsN
2b970 75 6c 6c 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20  ull..  **       
2b971 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20  TK_NE           
2b972 20 20 20 4f 50 5f 45 71 0d 0a 20 20 2a 2a 20 20     OP_Eq..  **  
2b973 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20       TK_EQ      
2b974 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0d 0a 20          OP_Ne.. 
2b975 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20   **       TK_GT 
2b976 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f               OP_
2b977 4c 65 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  Le..  **       T
2b978 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  K_LE            
2b979 20 20 4f 50 5f 47 74 0d 0a 20 20 2a 2a 20 20 20    OP_Gt..  **   
2b97a 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20      TK_GE       
2b97b 20 20 20 20 20 20 20 4f 50 5f 4c 74 0d 0a 20 20         OP_Lt..  
2b97c 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20  **       TK_LT  
2b97d 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47              OP_G
2b97e 65 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 46 6f  e..  **..  ** Fo
2b97f 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  r other values o
2b980 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20  f pExpr->op, op 
2b981 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  is undefined and
2b982 20 75 6e 75 73 65 64 2e 0d 0a 20 20 2a 2a 20 54   unused...  ** T
2b983 68 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20  he value of TK_ 
2b984 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74  and OP_ constant
2b985 73 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73  s are arranged s
2b986 75 63 68 20 74 68 61 74 20 77 65 0d 0a 20 20 2a  uch that we..  *
2b987 2a 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68  * can compute th
2b988 65 20 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20  e mapping above 
2b989 75 73 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77  using the follow
2b98a 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0d  ing expression..
2b98b 0a 20 20 2a 2a 20 41 73 73 65 72 74 28 29 73 20  .  ** Assert()s 
2b98c 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  verify that the 
2b98d 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73 20 63  computation is c
2b98e 6f 72 72 65 63 74 2e 0d 0a 20 20 2a 2f 0d 0a 20  orrect...  */.. 
2b98f 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f   op = ((pExpr->o
2b990 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 29  p+(TK_ISNULL&1))
2b991 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c 26 31  ^1)-(TK_ISNULL&1
2b992 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 56 65 72 69 66  );....  /* Verif
2b993 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d  y correct alignm
2b994 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f  ent of TK_ and O
2b995 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0d 0a 20 20  P_ constants..  
2b996 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 45  */..  assert( pE
2b997 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55  xpr->op!=TK_ISNU
2b998 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74  LL || op==OP_Not
2b999 4e 75 6c 6c 20 29 3b 0d 0a 20 20 61 73 73 65 72  Null );..  asser
2b99a 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
2b99b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d  _NOTNULL || op==
2b99c 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0d 0a 20 20  OP_IsNull );..  
2b99d 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2b99e 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d  p!=TK_NE || op==
2b99f 4f 50 5f 45 71 20 29 3b 0d 0a 20 20 61 73 73 65  OP_Eq );..  asse
2b9a0 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
2b9a1 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e  K_EQ || op==OP_N
2b9a2 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  e );..  assert( 
2b9a3 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54  pExpr->op!=TK_LT
2b9a4 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b   || op==OP_Ge );
2b9a5 0d 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ..  assert( pExp
2b9a6 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20  r->op!=TK_LE || 
2b9a7 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0d 0a 20 20  op==OP_Gt );..  
2b9a8 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2b9a9 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d  p!=TK_GT || op==
2b9aa 4f 50 5f 4c 65 20 29 3b 0d 0a 20 20 61 73 73 65  OP_Le );..  asse
2b9ab 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
2b9ac 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GE || op==OP_L
2b9ad 74 20 29 3b 0d 0a 0d 0a 20 20 73 77 69 74 63 68  t );....  switch
2b9ae 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0d 0a  ( pExpr->op ){..
2b9af 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a      case TK_AND:
2b9b0 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61   {..      testca
2b9b1 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
2b9b2 30 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  0 );..      sqli
2b9b3 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
2b9b4 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2b9b5 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
2b9b6 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 73  fNull);..      s
2b9b7 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2b9b8 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
2b9b9 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
2b9ba 75 6d 70 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20  umpIfNull);..   
2b9bb 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
2b9bc 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  ..    case TK_OR
2b9bd 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 64  : {..      int d
2b9be 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  2 = sqlite3VdbeM
2b9bf 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20  akeLabel(v);..  
2b9c0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
2b9c1 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0d 0a  mpIfNull==0 );..
2b9c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b9c3 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
2b9c4 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  e);..      sqlit
2b9c5 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
2b9c6 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2b9c7 74 2c 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c  t, d2, jumpIfNul
2b9c8 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  l^SQLITE_JUMPIFN
2b9c9 55 4c 4c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  ULL);..      sql
2b9ca 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2b9cb 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2b9cc 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
2b9cd 70 49 66 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20  pIfNull);..     
2b9ce 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b9cf 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b  lveLabel(v, d2);
2b9d0 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2b9d1 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
2b9d2 73 65 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 62  se, 1);..      b
2b9d3 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
2b9d4 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b    case TK_NOT: {
2b9d5 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2b9d6 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
2b9d7 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
2b9d8 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
2b9d9 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2b9da 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
2b9db 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  ll);..      brea
2b9dc 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  k;..    }..    c
2b9dd 61 73 65 20 54 4b 5f 4c 54 3a 0d 0a 20 20 20 20  ase TK_LT:..    
2b9de 63 61 73 65 20 54 4b 5f 4c 45 3a 0d 0a 20 20 20  case TK_LE:..   
2b9df 20 63 61 73 65 20 54 4b 5f 47 54 3a 0d 0a 20 20   case TK_GT:..  
2b9e0 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0d 0a 20    case TK_GE:.. 
2b9e1 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0d 0a     case TK_NE:..
2b9e2 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20      case TK_EQ: 
2b9e3 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  {..      testcas
2b9e4 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0d  e( op==TK_LT );.
2b9e5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b9e6 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0d 0a 20   op==TK_LE );.. 
2b9e7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2b9e8 70 3d 3d 54 4b 5f 47 54 20 29 3b 0d 0a 20 20 20  p==TK_GT );..   
2b9e9 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2b9ea 3d 54 4b 5f 47 45 20 29 3b 0d 0a 20 20 20 20 20  =TK_GE );..     
2b9eb 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2b9ec 4b 5f 45 51 20 29 3b 0d 0a 20 20 20 20 20 20 74  K_EQ );..      t
2b9ed 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2b9ee 4e 45 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  NE );..      tes
2b9ef 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
2b9f0 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 72  l==0 );..      r
2b9f1 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
2b9f2 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
2b9f3 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
2b9f4 65 67 46 72 65 65 31 29 3b 0d 0a 20 20 20 20 20  egFree1);..     
2b9f5 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
2b9f6 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
2b9f7 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
2b9f8 20 26 72 65 67 46 72 65 65 32 29 3b 0d 0a 20 20   &regFree2);..  
2b9f9 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
2b9fa 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2b9fb 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
2b9fc 67 68 74 2c 20 6f 70 2c 0d 0a 20 20 20 20 20 20  ght, op,..      
2b9fd 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20              r1, 
2b9fe 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  r2, dest, jumpIf
2b9ff 4e 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 74 65  Null);..      te
2ba00 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
2ba01 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  ==0 );..      te
2ba02 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
2ba03 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 62 72  ==0 );..      br
2ba04 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2ba05 20 63 61 73 65 20 54 4b 5f 49 53 3a 0d 0a 20 20   case TK_IS:..  
2ba06 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a    case TK_ISNOT:
2ba07 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61   {..      testca
2ba08 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
2ba09 4b 5f 49 53 20 29 3b 0d 0a 20 20 20 20 20 20 74  K_IS );..      t
2ba0a 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
2ba0b 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20 29 3b 0d  op==TK_ISNOT );.
2ba0c 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2ba0d 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
2ba0e 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2ba0f 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29  Left, &regFree1)
2ba10 3b 0d 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71  ;..      r2 = sq
2ba11 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
2ba12 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
2ba13 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65  >pRight, &regFre
2ba14 65 32 29 3b 0d 0a 20 20 20 20 20 20 6f 70 20 3d  e2);..      op =
2ba15 20 28 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f   (pExpr->op==TK_
2ba16 49 53 29 20 3f 20 54 4b 5f 4e 45 20 3a 20 54 4b  IS) ? TK_NE : TK
2ba17 5f 45 51 3b 0d 0a 20 20 20 20 20 20 63 6f 64 65  _EQ;..      code
2ba18 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
2ba19 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
2ba1a 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
2ba1b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2ba1c 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74      r1, r2, dest
2ba1d 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29  , SQLITE_NULLEQ)
2ba1e 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
2ba1f 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
2ba20 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
2ba21 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
2ba22 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
2ba23 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65  .    }..    case
2ba24 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0d 0a 20 20 20   TK_ISNULL:..   
2ba25 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c   case TK_NOTNULL
2ba26 3a 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63  : {..      testc
2ba27 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
2ba28 4c 4c 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  LL );..      tes
2ba29 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
2ba2a 54 4e 55 4c 4c 20 29 3b 0d 0a 20 20 20 20 20 20  TNULL );..      
2ba2b 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2ba2c 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2ba2d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
2ba2e 72 65 67 46 72 65 65 31 29 3b 0d 0a 20 20 20 20  regFree1);..    
2ba2f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ba30 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64  Op2(v, op, r1, d
2ba31 65 73 74 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  est);..      tes
2ba32 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
2ba33 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  =0 );..      bre
2ba34 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2ba35 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
2ba36 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61   {..      testca
2ba37 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
2ba38 30 20 29 3b 0d 0a 20 20 20 20 20 20 65 78 70 72  0 );..      expr
2ba39 43 6f 64 65 42 65 74 77 65 65 6e 28 70 50 61 72  CodeBetween(pPar
2ba3a 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74 2c  se, pExpr, dest,
2ba3b 20 30 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b   0, jumpIfNull);
2ba3c 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2ba3d 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
2ba3e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2ba3f 45 52 59 0d 0a 20 20 20 20 63 61 73 65 20 54 4b  ERY..    case TK
2ba40 5f 49 4e 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66  _IN: {..      if
2ba41 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 29 7b 0d  ( jumpIfNull ){.
2ba42 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ba43 45 78 70 72 43 6f 64 65 49 4e 28 70 50 61 72 73  ExprCodeIN(pPars
2ba44 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74 2c 20  e, pExpr, dest, 
2ba45 64 65 73 74 29 3b 0d 0a 20 20 20 20 20 20 7d 65  dest);..      }e
2ba46 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  lse{..        in
2ba47 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20 73  t destIfNull = s
2ba48 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2ba49 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20 20 20  bel(v);..       
2ba4a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2ba4b 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  IN(pParse, pExpr
2ba4c 2c 20 64 65 73 74 2c 20 64 65 73 74 49 66 4e 75  , dest, destIfNu
2ba4d 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  ll);..        sq
2ba4e 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2ba4f 4c 61 62 65 6c 28 76 2c 20 64 65 73 74 49 66 4e  Label(v, destIfN
2ba50 75 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ull);..      }..
2ba51 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
2ba52 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20    }..#endif..   
2ba53 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20   default: {..   
2ba54 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2ba55 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2ba56 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46  se, pExpr, &regF
2ba57 72 65 65 31 29 3b 0d 0a 20 20 20 20 20 20 73 71  ree1);..      sq
2ba58 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2ba59 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c  v, OP_IfNot, r1,
2ba5a 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
2ba5b 6c 21 3d 30 29 3b 0d 0a 20 20 20 20 20 20 74 65  l!=0);..      te
2ba5c 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
2ba5d 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  ==0 );..      te
2ba5e 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
2ba5f 6c 6c 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ll==0 );..      
2ba60 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
2ba61 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c   }..  sqlite3Rel
2ba62 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2ba63 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0d 0a  se, regFree1);..
2ba64 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2ba65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2ba66 72 65 67 46 72 65 65 32 29 3b 0d 0a 7d 0d 0a 0d  regFree2);..}...
2ba67 0a 2f 2a 0d 0a 2a 2a 20 44 6f 20 61 20 64 65 65  ./*..** Do a dee
2ba68 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  p comparison of 
2ba69 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  two expression t
2ba6a 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 30 20  rees.  Return 0 
2ba6b 69 66 20 74 68 65 20 74 77 6f 0d 0a 2a 2a 20 65  if the two..** e
2ba6c 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 63  xpressions are c
2ba6d 6f 6d 70 6c 65 74 65 6c 79 20 69 64 65 6e 74 69  ompletely identi
2ba6e 63 61 6c 2e 20 20 52 65 74 75 72 6e 20 31 20 69  cal.  Return 1 i
2ba6f 66 20 74 68 65 79 20 64 69 66 66 65 72 20 6f 6e  f they differ on
2ba70 6c 79 0d 0a 2a 2a 20 62 79 20 61 20 43 4f 4c 4c  ly..** by a COLL
2ba71 41 54 45 20 6f 70 65 72 61 74 6f 72 20 61 74 20  ATE operator at 
2ba72 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 20  the top level.  
2ba73 52 65 74 75 72 6e 20 32 20 69 66 20 74 68 65 72  Return 2 if ther
2ba74 65 20 61 72 65 20 64 69 66 66 65 72 65 6e 63 65  e are difference
2ba75 73 0d 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e  s..** other than
2ba76 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 43   the top-level C
2ba77 4f 4c 4c 41 54 45 20 6f 70 65 72 61 74 6f 72 2e  OLLATE operator.
2ba78 0d 0a 2a 2a 0d 0a 2a 2a 20 53 6f 6d 65 74 69 6d  ..**..** Sometim
2ba79 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  es this routine 
2ba7a 77 69 6c 6c 20 72 65 74 75 72 6e 20 32 20 65 76  will return 2 ev
2ba7b 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20 65 78  en if the two ex
2ba7c 70 72 65 73 73 69 6f 6e 73 0d 0a 2a 2a 20 72 65  pressions..** re
2ba7d 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61 6c  ally are equival
2ba7e 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e 6e  ent.  If we cann
2ba7f 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20 74 68  ot prove that th
2ba80 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  e expressions ar
2ba81 65 0d 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c  e..** identical,
2ba82 20 77 65 20 72 65 74 75 72 6e 20 32 20 6a 75 73   we return 2 jus
2ba83 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20 20 53  t to be safe.  S
2ba84 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  o if this routin
2ba85 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 32 2c  e..** returns 2,
2ba86 20 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74   then you do not
2ba87 20 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72   really know for
2ba88 20 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20   certain if the 
2ba89 74 77 6f 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69  two..** expressi
2ba8a 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65  ons are the same
2ba8b 2e 20 20 42 75 74 20 69 66 20 79 6f 75 20 67 65  .  But if you ge
2ba8c 74 20 61 20 30 20 6f 72 20 31 20 72 65 74 75 72  t a 0 or 1 retur
2ba8d 6e 2c 20 74 68 65 6e 20 79 6f 75 0d 0a 2a 2a 20  n, then you..** 
2ba8e 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 65 20  can be sure the 
2ba8f 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20  expressions are 
2ba90 74 68 65 20 73 61 6d 65 2e 20 20 49 6e 20 74 68  the same.  In th
2ba91 65 20 70 6c 61 63 65 73 20 77 68 65 72 65 0d 0a  e places where..
2ba92 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2ba93 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f 65 73  is used, it does
2ba94 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67 65 74   not hurt to get
2ba95 20 61 6e 20 65 78 74 72 61 20 32 20 2d 20 74 68   an extra 2 - th
2ba96 61 74 0d 0a 2a 2a 20 6a 75 73 74 20 6d 69 67 68  at..** just migh
2ba97 74 20 72 65 73 75 6c 74 20 69 6e 20 73 6f 6d 65  t result in some
2ba98 20 73 6c 69 67 68 74 6c 79 20 73 6c 6f 77 65 72   slightly slower
2ba99 20 63 6f 64 65 2e 20 20 42 75 74 20 72 65 74 75   code.  But retu
2ba9a 72 6e 69 6e 67 0d 0a 2a 2a 20 61 6e 20 69 6e 63  rning..** an inc
2ba9b 6f 72 72 65 63 74 20 30 20 6f 72 20 31 20 63 6f  orrect 0 or 1 co
2ba9c 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 6d 61  uld lead to a ma
2ba9d 6c 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  lfunction...*/..
2ba9e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2ba9f 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
2baa0 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20  mpare(Expr *pA, 
2baa1 45 78 70 72 20 2a 70 42 29 7b 0d 0a 20 20 69 66  Expr *pB){..  if
2baa2 28 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29  ( pA==0||pB==0 )
2baa3 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42  {..    return pB
2baa4 3d 3d 70 41 20 3f 20 30 20 3a 20 32 3b 0d 0a 20  ==pA ? 0 : 2;.. 
2baa5 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 21 45   }..  assert( !E
2baa6 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
2baa7 79 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  y(pA, EP_TokenOn
2baa8 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
2baa9 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78  ;..  assert( !Ex
2baaa 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
2baab 28 70 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  (pB, EP_TokenOnl
2baac 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
2baad 0d 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ..  if( ExprHasP
2baae 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 78  roperty(pA, EP_x
2baaf 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45 78 70  IsSelect) || Exp
2bab0 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c  rHasProperty(pB,
2bab1 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2bab2 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b  {..    return 2;
2bab3 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 28 70 41  ..  }..  if( (pA
2bab4 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73  ->flags & EP_Dis
2bab5 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61  tinct)!=(pB->fla
2bab6 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
2bab7 29 20 29 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20  ) ) return 2;.. 
2bab8 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d   if( pA->op!=pB-
2bab9 3e 6f 70 20 29 20 72 65 74 75 72 6e 20 32 3b 0d  >op ) return 2;.
2baba 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78  .  if( sqlite3Ex
2babb 70 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c  prCompare(pA->pL
2babc 65 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20  eft, pB->pLeft) 
2babd 29 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20 69  ) return 2;..  i
2babe 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  f( sqlite3ExprCo
2babf 6d 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74  mpare(pA->pRight
2bac0 2c 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20  , pB->pRight) ) 
2bac1 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20 69 66 28  return 2;..  if(
2bac2 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2bac3 43 6f 6d 70 61 72 65 28 70 41 2d 3e 78 2e 70 4c  Compare(pA->x.pL
2bac4 69 73 74 2c 20 70 42 2d 3e 78 2e 70 4c 69 73 74  ist, pB->x.pList
2bac5 29 20 29 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20  ) ) return 2;.. 
2bac6 20 69 66 28 20 70 41 2d 3e 69 54 61 62 6c 65 21   if( pA->iTable!
2bac7 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c 7c 20 70  =pB->iTable || p
2bac8 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e  A->iColumn!=pB->
2bac9 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 75 72 6e  iColumn ) return
2baca 20 32 3b 0d 0a 20 20 69 66 28 20 45 78 70 72 48   2;..  if( ExprH
2bacb 61 73 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45  asProperty(pA, E
2bacc 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0d 0a  P_IntValue) ){..
2bacd 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73      if( !ExprHas
2bace 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50 5f  Property(pB, EP_
2bacf 49 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70 41 2d  IntValue) || pA-
2bad0 3e 75 2e 69 56 61 6c 75 65 21 3d 70 42 2d 3e 75  >u.iValue!=pB->u
2bad1 2e 69 56 61 6c 75 65 20 29 7b 0d 0a 20 20 20 20  .iValue ){..    
2bad2 20 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20 20    return 2;..   
2bad3 20 7d 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   }..  }else if( 
2bad4 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pA->op!=TK_COLUM
2bad5 4e 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f 6b 65  N && pA->u.zToke
2bad6 6e 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 45 78  n ){..    if( Ex
2bad7 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42  prHasProperty(pB
2bad8 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c  , EP_IntValue) |
2bad9 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a 54  | NEVER(pB->u.zT
2bada 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65 74 75 72  oken==0) ) retur
2badb 6e 20 32 3b 0d 0a 20 20 20 20 69 66 28 20 73 74  n 2;..    if( st
2badc 72 63 6d 70 28 70 41 2d 3e 75 2e 7a 54 6f 6b 65  rcmp(pA->u.zToke
2badd 6e 2c 70 42 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 21  n,pB->u.zToken)!
2bade 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  =0 ){..      ret
2badf 75 72 6e 20 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20  urn 2;..    }.. 
2bae0 20 7d 0d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66   }..  if( (pA->f
2bae1 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
2bae2 6c 61 74 65 29 21 3d 28 70 42 2d 3e 66 6c 61 67  late)!=(pB->flag
2bae3 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
2bae4 65 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a  e) ) return 1;..
2bae5 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 73    if( (pA->flags
2bae6 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
2bae7 29 21 3d 30 20 26 26 20 70 41 2d 3e 70 43 6f 6c  )!=0 && pA->pCol
2bae8 6c 21 3d 70 42 2d 3e 70 43 6f 6c 6c 20 29 20 72  l!=pB->pColl ) r
2bae9 65 74 75 72 6e 20 32 3b 0d 0a 20 20 72 65 74 75  eturn 2;..  retu
2baea 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn 0;..}..../*..
2baeb 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 45  ** Compare two E
2baec 78 70 72 4c 69 73 74 20 6f 62 6a 65 63 74 73 2e  xprList objects.
2baed 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 74 68    Return 0 if th
2baee 65 79 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  ey are identical
2baef 20 61 6e 64 20 0d 0a 2a 2a 20 6e 6f 6e 2d 7a 65   and ..** non-ze
2baf0 72 6f 20 69 66 20 74 68 65 79 20 64 69 66 66 65  ro if they diffe
2baf1 72 20 69 6e 20 61 6e 79 20 77 61 79 2e 0d 0a 2a  r in any way...*
2baf2 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
2baf3 6e 65 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ne might return 
2baf4 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 65 71 75  non-zero for equ
2baf5 69 76 61 6c 65 6e 74 20 45 78 70 72 4c 69 73 74  ivalent ExprList
2baf6 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 6f 6e 6c 79  s.  The..** only
2baf7 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 77 69 6c   consequence wil
2baf8 6c 20 62 65 20 64 69 73 61 62 6c 65 64 20 6f 70  l be disabled op
2baf9 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 20 20 42 75  timizations.  Bu
2bafa 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a  t this routine..
2bafb 2a 2a 20 6d 75 73 74 20 6e 65 76 65 72 20 72 65  ** must never re
2bafc 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 74 77  turn 0 if the tw
2bafd 6f 20 45 78 70 72 4c 69 73 74 20 6f 62 6a 65 63  o ExprList objec
2bafe 74 73 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  ts are different
2baff 2c 20 6f 72 0d 0a 2a 2a 20 61 20 6d 61 6c 66 75  , or..** a malfu
2bb00 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75  nction will resu
2bb01 6c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 77 6f 20  lt...**..** Two 
2bb02 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 61 72  NULL pointers ar
2bb03 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
2bb04 62 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42 75  be the same.  Bu
2bb05 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  t a NULL pointer
2bb06 0d 0a 2a 2a 20 61 6c 77 61 79 73 20 64 69 66 66  ..** always diff
2bb07 65 72 73 20 66 72 6f 6d 20 61 20 6e 6f 6e 2d 4e  ers from a non-N
2bb08 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f  ULL pointer...*/
2bb09 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2bb0a 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
2bb0b 4c 69 73 74 43 6f 6d 70 61 72 65 28 45 78 70 72  ListCompare(Expr
2bb0c 4c 69 73 74 20 2a 70 41 2c 20 45 78 70 72 4c 69  List *pA, ExprLi
2bb0d 73 74 20 2a 70 42 29 7b 0d 0a 20 20 69 6e 74 20  st *pB){..  int 
2bb0e 69 3b 0d 0a 20 20 69 66 28 20 70 41 3d 3d 30 20  i;..  if( pA==0 
2bb0f 26 26 20 70 42 3d 3d 30 20 29 20 72 65 74 75 72  && pB==0 ) retur
2bb10 6e 20 30 3b 0d 0a 20 20 69 66 28 20 70 41 3d 3d  n 0;..  if( pA==
2bb11 30 20 7c 7c 20 70 42 3d 3d 30 20 29 20 72 65 74  0 || pB==0 ) ret
2bb12 75 72 6e 20 31 3b 0d 0a 20 20 69 66 28 20 70 41  urn 1;..  if( pA
2bb13 2d 3e 6e 45 78 70 72 21 3d 70 42 2d 3e 6e 45 78  ->nExpr!=pB->nEx
2bb14 70 72 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a  pr ) return 1;..
2bb15 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d    for(i=0; i<pA-
2bb16 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20  >nExpr; i++){.. 
2bb17 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20     Expr *pExprA 
2bb18 3d 20 70 41 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  = pA->a[i].pExpr
2bb19 3b 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  ;..    Expr *pEx
2bb1a 70 72 42 20 3d 20 70 42 2d 3e 61 5b 69 5d 2e 70  prB = pB->a[i].p
2bb1b 45 78 70 72 3b 0d 0a 20 20 20 20 69 66 28 20 70  Expr;..    if( p
2bb1c 41 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65  A->a[i].sortOrde
2bb1d 72 21 3d 70 42 2d 3e 61 5b 69 5d 2e 73 6f 72 74  r!=pB->a[i].sort
2bb1e 4f 72 64 65 72 20 29 20 72 65 74 75 72 6e 20 31  Order ) return 1
2bb1f 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ;..    if( sqlit
2bb20 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45  e3ExprCompare(pE
2bb21 78 70 72 41 2c 20 70 45 78 70 72 42 29 20 29 20  xprA, pExprB) ) 
2bb22 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a  return 1;..  }..
2bb23 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a    return 0;..}..
2bb24 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61 20 6e  ../*..** Add a n
2bb25 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68  ew element to th
2bb26 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  e pAggInfo->aCol
2bb27 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72  [] array.  Retur
2bb28 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0d 0a  n the index of..
2bb29 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65  ** the new eleme
2bb2a 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65  nt.  Return a ne
2bb2b 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66  gative number if
2bb2c 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0d 0a   malloc fails...
2bb2d 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 61  */..static int a
2bb2e 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28  ddAggInfoColumn(
2bb2f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67  sqlite3 *db, Agg
2bb30 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0d 0a 20  Info *pInfo){.. 
2bb31 20 69 6e 74 20 69 3b 0d 0a 20 20 70 49 6e 66 6f   int i;..  pInfo
2bb32 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33  ->aCol = sqlite3
2bb33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0d 0a  ArrayAllocate(..
2bb34 20 20 20 20 20 20 20 64 62 2c 0d 0a 20 20 20 20         db,..    
2bb35 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0d     pInfo->aCol,.
2bb36 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70  .       sizeof(p
2bb37 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0d  Info->aCol[0]),.
2bb38 0a 20 20 20 20 20 20 20 33 2c 0d 0a 20 20 20 20  .       3,..    
2bb39 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75     &pInfo->nColu
2bb3a 6d 6e 2c 0d 0a 20 20 20 20 20 20 20 26 70 49 6e  mn,..       &pIn
2bb3b 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  fo->nColumnAlloc
2bb3c 2c 0d 0a 20 20 20 20 20 20 20 26 69 0d 0a 20 20  ,..       &i..  
2bb3d 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0d  );..  return i;.
2bb3e 0a 7d 20 20 20 20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  .}    ..../*..**
2bb3f 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65   Add a new eleme
2bb40 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e  nt to the pAggIn
2bb41 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 72 61  fo->aFunc[] arra
2bb42 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  y.  Return the i
2bb43 6e 64 65 78 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  ndex of..** the 
2bb44 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65  new element.  Re
2bb45 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20  turn a negative 
2bb46 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63  number if malloc
2bb47 20 66 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 73 74 61   fails...*/..sta
2bb48 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e  tic int addAggIn
2bb49 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a  foFunc(sqlite3 *
2bb4a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e  db, AggInfo *pIn
2bb4b 66 6f 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  fo){..  int i;..
2bb4c 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d    pInfo->aFunc =
2bb4d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c   sqlite3ArrayAll
2bb4e 6f 63 61 74 65 28 0d 0a 20 20 20 20 20 20 20 64  ocate(..       d
2bb4f 62 2c 20 0d 0a 20 20 20 20 20 20 20 70 49 6e 66  b, ..       pInf
2bb50 6f 2d 3e 61 46 75 6e 63 2c 0d 0a 20 20 20 20 20  o->aFunc,..     
2bb51 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e    sizeof(pInfo->
2bb52 61 46 75 6e 63 5b 30 5d 29 2c 0d 0a 20 20 20 20  aFunc[0]),..    
2bb53 20 20 20 33 2c 0d 0a 20 20 20 20 20 20 20 26 70     3,..       &p
2bb54 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0d 0a 20 20  Info->nFunc,..  
2bb55 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75       &pInfo->nFu
2bb56 6e 63 41 6c 6c 6f 63 2c 0d 0a 20 20 20 20 20 20  ncAlloc,..      
2bb57 20 26 69 0d 0a 20 20 29 3b 0d 0a 20 20 72 65 74   &i..  );..  ret
2bb58 75 72 6e 20 69 3b 0d 0a 7d 20 20 20 20 0d 0a 0d  urn i;..}    ...
2bb59 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20  ./*..** This is 
2bb5a 74 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63  the xExprCallbac
2bb5b 6b 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c  k for a tree wal
2bb5c 6b 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64  ker.  It is used
2bb5d 20 74 6f 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   to..** implemen
2bb5e 74 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  t sqlite3ExprAna
2bb5f 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29  lyzeAggregates()
2bb60 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 45 78  .  See sqlite3Ex
2bb61 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
2bb62 74 65 73 0d 0a 2a 2a 20 66 6f 72 20 61 64 64 69  tes..** for addi
2bb63 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
2bb64 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  on...*/..static 
2bb65 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65  int analyzeAggre
2bb66 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 61  gate(Walker *pWa
2bb67 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
2bb68 72 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  r){..  int i;.. 
2bb69 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
2bb6a 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
2bb6b 4e 43 3b 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  NC;..  Parse *pP
2bb6c 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72  arse = pNC->pPar
2bb6d 73 65 3b 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a  se;..  SrcList *
2bb6e 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
2bb6f 70 53 72 63 4c 69 73 74 3b 0d 0a 20 20 41 67 67  pSrcList;..  Agg
2bb70 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d  Info *pAggInfo =
2bb71 20 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0d   pNC->pAggInfo;.
2bb72 0a 0d 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ...  switch( pEx
2bb73 70 72 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 63  pr->op ){..    c
2bb74 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
2bb75 4e 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  N:..    case TK_
2bb76 43 4f 4c 55 4d 4e 3a 20 7b 0d 0a 20 20 20 20 20  COLUMN: {..     
2bb77 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
2bb78 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c  ->op==TK_AGG_COL
2bb79 55 4d 4e 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  UMN );..      te
2bb7a 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2bb7b 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0d  p==TK_COLUMN );.
2bb7c 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
2bb7d 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f  to see if the co
2bb7e 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f  lumn is in one o
2bb7f 66 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  f the tables in 
2bb80 74 68 65 20 46 52 4f 4d 0d 0a 20 20 20 20 20 20  the FROM..      
2bb81 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  ** clause of the
2bb82 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2bb83 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 41   */..      if( A
2bb84 4c 57 41 59 53 28 70 53 72 63 4c 69 73 74 21 3d  LWAYS(pSrcList!=
2bb85 30 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  0) ){..        s
2bb86 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2bb87 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63  em *pItem = pSrc
2bb88 4c 69 73 74 2d 3e 61 3b 0d 0a 20 20 20 20 20 20  List->a;..      
2bb89 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72    for(i=0; i<pSr
2bb8a 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
2bb8b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a 20 20 20  , pItem++){..   
2bb8c 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67         struct Ag
2bb8d 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b  gInfo_col *pCol;
2bb8e 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ..          asse
2bb8f 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
2bb90 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2bb91 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
2bb92 65 64 75 63 65 64 29 20 29 3b 0d 0a 20 20 20 20  educed) );..    
2bb93 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
2bb94 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e  >iTable==pItem->
2bb95 69 43 75 72 73 6f 72 20 29 7b 0d 0a 20 20 20 20  iCursor ){..    
2bb96 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65          /* If we
2bb97 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e   reach this poin
2bb98 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  t, it means that
2bb99 20 70 45 78 70 72 20 72 65 66 65 72 73 20 74 6f   pExpr refers to
2bb9a 20 61 20 74 61 62 6c 65 0d 0a 20 20 20 20 20 20   a table..      
2bb9b 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73        ** that is
2bb9c 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2bb9d 75 73 65 20 6f 66 20 74 68 65 20 61 67 67 72 65  use of the aggre
2bb9e 67 61 74 65 20 71 75 65 72 79 2e 20 20 0d 0a 20  gate query.  .. 
2bb9f 20 20 20 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20             **.. 
2bba0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4d 61             ** Ma
2bba1 6b 65 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20  ke an entry for 
2bba2 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 41  the column in pA
2bba3 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69  ggInfo->aCol[] i
2bba4 66 20 74 68 65 72 65 0d 0a 20 20 20 20 20 20 20  f there..       
2bba5 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61       ** is not a
2bba6 6e 20 65 6e 74 72 79 20 74 68 65 72 65 20 61 6c  n entry there al
2bba7 72 65 61 64 79 2e 0d 0a 20 20 20 20 20 20 20 20  ready...        
2bba8 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
2bba9 20 20 20 20 69 6e 74 20 6b 3b 0d 0a 20 20 20 20      int k;..    
2bbaa 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 70          pCol = p
2bbab 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0d 0a  AggInfo->aCol;..
2bbac 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
2bbad 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d  k=0; k<pAggInfo-
2bbae 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70  >nColumn; k++, p
2bbaf 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  Col++){..       
2bbb0 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
2bbb1 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
2bbb2 69 54 61 62 6c 65 20 26 26 0d 0a 20 20 20 20 20  iTable &&..     
2bbb3 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2bbb4 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70  l->iColumn==pExp
2bbb5 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0d 0a 20  r->iColumn ){.. 
2bbb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2bbb7 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20  reak;..         
2bbb8 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
2bbb9 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
2bbba 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49     if( (k>=pAggI
2bbbb 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0d 0a 20  nfo->nColumn).. 
2bbbc 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
2bbbd 6b 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f  k = addAggInfoCo
2bbbe 6c 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  lumn(pParse->db,
2bbbf 20 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0d   pAggInfo))>=0 .
2bbc0 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0d  .            ){.
2bbc1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2bbc2 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d  Col = &pAggInfo-
2bbc3 3e 61 43 6f 6c 5b 6b 5d 3b 0d 0a 20 20 20 20 20  >aCol[k];..     
2bbc4 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70           pCol->p
2bbc5 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
2bbc6 62 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  b;..            
2bbc7 20 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d    pCol->iTable =
2bbc8 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0d   pExpr->iTable;.
2bbc9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2bbca 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70  Col->iColumn = p
2bbcb 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a  Expr->iColumn;..
2bbcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
2bbcd 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61  ol->iMem = ++pPa
2bbce 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20  rse->nMem;..    
2bbcf 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
2bbd0 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20  iSorterColumn = 
2bbd1 2d 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  -1;..           
2bbd2 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20 3d     pCol->pExpr =
2bbd3 20 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 20   pExpr;..       
2bbd4 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49         if( pAggI
2bbd5 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  nfo->pGroupBy ){
2bbd6 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2bbd7 20 20 69 6e 74 20 6a 2c 20 6e 3b 0d 0a 20 20 20    int j, n;..   
2bbd8 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70               Exp
2bbd9 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67  rList *pGB = pAg
2bbda 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b  gInfo->pGroupBy;
2bbdb 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2bbdc 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2bbdd 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20  t_item *pTerm = 
2bbde 70 47 42 2d 3e 61 3b 0d 0a 20 20 20 20 20 20 20  pGB->a;..       
2bbdf 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42           n = pGB
2bbe0 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20 20 20 20  ->nExpr;..      
2bbe1 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
2bbe2 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65  0; j<n; j++, pTe
2bbe3 72 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  rm++){..        
2bbe4 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
2bbe5 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pE = pTerm->pExp
2bbe6 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  r;..            
2bbe7 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70        if( pE->op
2bbe8 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70  ==TK_COLUMN && p
2bbe9 45 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72  E->iTable==pExpr
2bbea 2d 3e 69 54 61 62 6c 65 20 26 26 0d 0a 20 20 20  ->iTable &&..   
2bbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbec 20 20 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d     pE->iColumn==
2bbed 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  pExpr->iColumn )
2bbee 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  {..             
2bbef 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f         pCol->iSo
2bbf0 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0d  rterColumn = j;.
2bbf1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bbf2 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2bbf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
2bbf4 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2bbf5 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
2bbf6 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
2bbf7 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53      if( pCol->iS
2bbf8 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 7b  orterColumn<0 ){
2bbf9 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2bbfa 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
2bbfb 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66 6f  olumn = pAggInfo
2bbfc 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e  ->nSortingColumn
2bbfd 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ++;..           
2bbfe 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
2bbff 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
2bc00 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77   /* There is now
2bc01 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45   an entry for pE
2bc02 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  xpr in pAggInfo-
2bc03 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0d  >aCol[] (either.
2bc04 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
2bc05 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20 74  because it was t
2bc06 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62  here before or b
2bc07 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20 63  ecause we just c
2bc08 72 65 61 74 65 64 20 69 74 29 2e 0d 0a 20 20 20  reated it)...   
2bc09 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76           ** Conv
2bc0a 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f  ert the pExpr to
2bc0b 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c   be a TK_AGG_COL
2bc0c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f  UMN referring to
2bc0d 20 74 68 61 74 0d 0a 20 20 20 20 20 20 20 20 20   that..         
2bc0e 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e     ** pAggInfo->
2bc0f 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0d 0a 20  aCol[] entry... 
2bc10 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20             */.. 
2bc11 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53             ExprS
2bc12 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45  etIrreducible(pE
2bc13 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  xpr);..         
2bc14 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e     pExpr->pAggIn
2bc15 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0d 0a  fo = pAggInfo;..
2bc16 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
2bc17 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 43  r->op = TK_AGG_C
2bc18 4f 4c 55 4d 4e 3b 0d 0a 20 20 20 20 20 20 20 20  OLUMN;..        
2bc19 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20      pExpr->iAgg 
2bc1a 3d 20 28 69 31 36 29 6b 3b 0d 0a 20 20 20 20 20  = (i16)k;..     
2bc1b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
2bc1c 20 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e           } /* en
2bc1d 64 69 66 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  dif pExpr->iTabl
2bc1e 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  e==pItem->iCurso
2bc1f 72 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7d 20  r */..        } 
2bc20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 65 72  /* end loop over
2bc21 20 70 53 72 63 4c 69 73 74 20 2a 2f 0d 0a 20 20   pSrcList */..  
2bc22 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74      }..      ret
2bc23 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a  urn WRC_Prune;..
2bc24 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
2bc25 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a  TK_AGG_FUNCTION:
2bc26 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   {..      /* The
2bc27 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20   pNC->nDepth==0 
2bc28 74 65 73 74 20 63 61 75 73 65 73 20 61 67 67 72  test causes aggr
2bc29 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
2bc2a 69 6e 20 73 75 62 71 75 65 72 69 65 73 0d 0a 20  in subqueries.. 
2bc2b 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 67       ** to be ig
2bc2c 6e 6f 72 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20  nored */..      
2bc2d 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  if( pNC->nDepth=
2bc2e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f  =0 ){..        /
2bc2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
2bc30 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70  f pExpr is a dup
2bc31 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65  licate of anothe
2bc32 72 20 61 67 67 72 65 67 61 74 65 20 0d 0a 20 20  r aggregate ..  
2bc33 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
2bc34 6e 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64  n that is alread
2bc35 79 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66  y in the pAggInf
2bc36 6f 20 73 74 72 75 63 74 75 72 65 0d 0a 20 20 20  o structure..   
2bc37 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20       */..       
2bc38 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
2bc39 66 75 6e 63 20 2a 70 49 74 65 6d 20 3d 20 70 41  func *pItem = pA
2bc3a 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0d 0a  ggInfo->aFunc;..
2bc3b 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
2bc3c 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
2bc3d 6e 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  nc; i++, pItem++
2bc3e 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  ){..          if
2bc3f 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d  ( sqlite3ExprCom
2bc40 70 61 72 65 28 70 49 74 65 6d 2d 3e 70 45 78 70  pare(pItem->pExp
2bc41 72 2c 20 70 45 78 70 72 29 3d 3d 30 20 29 7b 0d  r, pExpr)==0 ){.
2bc42 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
2bc43 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  ak;..          }
2bc44 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
2bc45 20 20 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67       if( i>=pAgg
2bc46 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0d 0a  Info->nFunc ){..
2bc47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 45 78            /* pEx
2bc48 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20  pr is original. 
2bc49 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 72   Make a new entr
2bc4a 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61  y in pAggInfo->a
2bc4b 46 75 6e 63 5b 5d 0d 0a 20 20 20 20 20 20 20 20  Func[]..        
2bc4c 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20    */..          
2bc4d 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61  u8 enc = ENC(pPa
2bc4e 72 73 65 2d 3e 64 62 29 3b 0d 0a 20 20 20 20 20  rse->db);..     
2bc4f 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49       i = addAggI
2bc50 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e  nfoFunc(pParse->
2bc51 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0d 0a  db, pAggInfo);..
2bc52 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e            if( i>
2bc53 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  =0 ){..         
2bc54 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2bc55 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2bc56 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2bc57 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   );..           
2bc58 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e   pItem = &pAggIn
2bc59 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0d 0a 20  fo->aFunc[i];.. 
2bc5a 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
2bc5b 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b  ->pExpr = pExpr;
2bc5c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49  ..            pI
2bc5d 74 65 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50  tem->iMem = ++pP
2bc5e 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20  arse->nMem;..   
2bc5f 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2bc60 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2bc61 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
2bc62 61 6c 75 65 29 20 29 3b 0d 0a 20 20 20 20 20 20  alue) );..      
2bc63 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75        pItem->pFu
2bc64 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  nc = sqlite3Find
2bc65 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
2bc66 3e 64 62 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  >db,..          
2bc67 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
2bc68 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71 6c 69 74 65  u.zToken, sqlite
2bc69 33 53 74 72 6c 65 6e 33 30 28 70 45 78 70 72 2d  3Strlen30(pExpr-
2bc6a 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0d 0a 20 20 20  >u.zToken),..   
2bc6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc6c 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3f  pExpr->x.pList ?
2bc6d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
2bc6e 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c  >nExpr : 0, enc,
2bc6f 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   0);..          
2bc70 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61    if( pExpr->fla
2bc71 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
2bc72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
2bc73 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
2bc74 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
2bc75 61 62 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20  ab++;..         
2bc76 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2bc77 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
2bc78 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0d  iDistinct = -1;.
2bc79 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  .            }..
2bc7a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
2bc7b 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
2bc7c 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f  /* Make pExpr po
2bc7d 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f  int to the appro
2bc7e 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d  priate pAggInfo-
2bc7f 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0d 0a  >aFunc[] entry..
2bc80 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20          */..    
2bc81 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2bc82 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
2bc83 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f  pExpr, EP_TokenO
2bc84 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
2bc85 29 3b 0d 0a 20 20 20 20 20 20 20 20 45 78 70 72  );..        Expr
2bc86 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70  SetIrreducible(p
2bc87 45 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 20 20  Expr);..        
2bc88 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69  pExpr->iAgg = (i
2bc89 31 36 29 69 3b 0d 0a 20 20 20 20 20 20 20 20 70  16)i;..        p
2bc8a 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d  Expr->pAggInfo =
2bc8b 20 70 41 67 67 49 6e 66 6f 3b 0d 0a 20 20 20 20   pAggInfo;..    
2bc8c 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
2bc8d 72 75 6e 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rune;..      }..
2bc8e 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
2bc8f 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
2bc90 65 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e  e;..}..static in
2bc91 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  t analyzeAggrega
2bc92 74 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b  tesInSelect(Walk
2bc93 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
2bc94 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0d 0a  ect *pSelect){..
2bc95 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
2bc96 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  NC = pWalker->u.
2bc97 70 4e 43 3b 0d 0a 20 20 69 66 28 20 70 4e 43 2d  pNC;..  if( pNC-
2bc98 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0d 0a 20  >nDepth==0 ){.. 
2bc99 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b     pNC->nDepth++
2bc9a 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61  ;..    sqlite3Wa
2bc9b 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
2bc9c 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  , pSelect);..   
2bc9d 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0d   pNC->nDepth--;.
2bc9e 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
2bc9f 50 72 75 6e 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b  Prune;..  }else{
2bca0 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  ..    return WRC
2bca1 5f 43 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 7d 0d  _Continue;..  }.
2bca2 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 61  .}..../*..** Ana
2bca3 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65  lyze the given e
2bca4 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e  xpression lookin
2bca5 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20  g for aggregate 
2bca6 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0d 0a 2a  functions and..*
2bca7 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73 20  * for variables 
2bca8 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
2bca9 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50 61  added to the pPa
2bcaa 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61  rse->aAgg[] arra
2bcab 79 2e 0d 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69  y...** Make addi
2bcac 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74  tional entries t
2bcad 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41  o the pParse->aA
2bcae 67 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65  gg[] array as ne
2bcaf 63 65 73 73 61 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a  cessary...**..**
2bcb0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
2bcb1 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c  ould only be cal
2bcb2 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 65 78  led after the ex
2bcb3 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62 65 65  pression has bee
2bcb4 6e 0d 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62  n..** analyzed b
2bcb5 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  y sqlite3Resolve
2bcb6 45 78 70 72 4e 61 6d 65 73 28 29 2e 0d 0a 2a 2f  ExprNames()...*/
2bcb7 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2bcb8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
2bcb9 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2bcba 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  es(NameContext *
2bcbb 70 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pNC, Expr *pExpr
2bcbc 29 7b 0d 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0d  ){..  Walker w;.
2bcbd 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61  .  w.xExprCallba
2bcbe 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72  ck = analyzeAggr
2bcbf 65 67 61 74 65 3b 0d 0a 20 20 77 2e 78 53 65 6c  egate;..  w.xSel
2bcc0 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e  ectCallback = an
2bcc1 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49  alyzeAggregatesI
2bcc2 6e 53 65 6c 65 63 74 3b 0d 0a 20 20 77 2e 75 2e  nSelect;..  w.u.
2bcc3 70 4e 43 20 3d 20 70 4e 43 3b 0d 0a 20 20 61 73  pNC = pNC;..  as
2bcc4 73 65 72 74 28 20 70 4e 43 2d 3e 70 53 72 63 4c  sert( pNC->pSrcL
2bcc5 69 73 74 21 3d 30 20 29 3b 0d 0a 20 20 73 71 6c  ist!=0 );..  sql
2bcc6 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c  ite3WalkExpr(&w,
2bcc7 20 70 45 78 70 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f   pExpr);..}..../
2bcc8 2a 0d 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  *..** Call sqlit
2bcc9 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
2bcca 72 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76  regates() for ev
2bccb 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
2bccc 6e 20 61 6e 0d 0a 2a 2a 20 65 78 70 72 65 73 73  n an..** express
2bccd 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72  ion list.  Retur
2bcce 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2bccf 65 72 72 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  errors...**..** 
2bcd0 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 66  If an error is f
2bcd1 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79 73  ound, the analys
2bcd2 69 73 20 69 73 20 63 75 74 20 73 68 6f 72 74 2e  is is cut short.
2bcd3 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2bcd4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2bcd5 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
2bcd6 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ist(NameContext 
2bcd7 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a  *pNC, ExprList *
2bcd8 70 4c 69 73 74 29 7b 0d 0a 20 20 73 74 72 75 63  pList){..  struc
2bcd9 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2bcda 2a 70 49 74 65 6d 3b 0d 0a 20 20 69 6e 74 20 69  *pItem;..  int i
2bcdb 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  ;..  if( pList )
2bcdc 7b 0d 0a 20 20 20 20 66 6f 72 28 70 49 74 65 6d  {..    for(pItem
2bcdd 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20  =pList->a, i=0; 
2bcde 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
2bcdf 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a  i++, pItem++){..
2bce0 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2bce1 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2bce2 65 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e 70  es(pNC, pItem->p
2bce3 45 78 70 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Expr);..    }.. 
2bce4 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
2bce5 41 6c 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c  Allocate a singl
2bce6 65 20 6e 65 77 20 72 65 67 69 73 74 65 72 20 66  e new register f
2bce7 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73  or use to hold s
2bce8 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ome intermediate
2bce9 20 72 65 73 75 6c 74 2e 0d 0a 2a 2f 0d 0a 53 51   result...*/..SQ
2bcea 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2bceb 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2bcec 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eg(Parse *pParse
2bced 29 7b 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65  ){..  if( pParse
2bcee 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b  ->nTempReg==0 ){
2bcef 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70  ..    return ++p
2bcf0 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20  Parse->nMem;..  
2bcf1 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72  }..  return pPar
2bcf2 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70  se->aTempReg[--p
2bcf3 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d  Parse->nTempReg]
2bcf4 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44  ;..}..../*..** D
2bcf5 65 61 6c 6c 6f 63 61 74 65 20 61 20 72 65 67 69  eallocate a regi
2bcf6 73 74 65 72 2c 20 6d 61 6b 69 6e 67 20 61 76 61  ster, making ava
2bcf7 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 75 73 65  ilable for reuse
2bcf8 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0d   for some other.
2bcf9 0a 2a 2a 20 70 75 72 70 6f 73 65 2e 0d 0a 2a 2a  .** purpose...**
2bcfa 0d 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74  ..** If a regist
2bcfb 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  er is currently 
2bcfc 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 68  being used by th
2bcfd 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2c 20  e column cache, 
2bcfe 74 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 64 61 6c  then..** the dal
2bcff 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65 66 65  location is defe
2bd00 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 20 63  rred until the c
2bd01 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69 6e 65  olumn cache line
2bd02 20 74 68 61 74 20 75 73 65 73 0d 0a 2a 2a 20 74   that uses..** t
2bd03 68 65 20 72 65 67 69 73 74 65 72 20 62 65 63 6f  he register beco
2bd04 6d 65 73 20 73 74 61 6c 65 2e 0d 0a 2a 2f 0d 0a  mes stale...*/..
2bd05 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2bd06 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61  oid sqlite3Relea
2bd07 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 20  seTempReg(Parse 
2bd08 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
2bd09 67 29 7b 0d 0a 20 20 69 66 28 20 69 52 65 67 20  g){..  if( iReg 
2bd0a 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70  && pParse->nTemp
2bd0b 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50  Reg<ArraySize(pP
2bd0c 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20  arse->aTempReg) 
2bd0d 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a  ){..    int i;..
2bd0e 20 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43      struct yColC
2bd0f 61 63 68 65 20 2a 70 3b 0d 0a 20 20 20 20 66 6f  ache *p;..    fo
2bd10 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
2bd11 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
2bd12 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
2bd13 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0d 0a 20 20 20   i++, p++){..   
2bd14 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d     if( p->iReg==
2bd15 69 52 65 67 20 29 7b 0d 0a 20 20 20 20 20 20 20  iReg ){..       
2bd16 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 31 3b   p->tempReg = 1;
2bd17 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
2bd18 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2bd19 7d 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  }..    pParse->a
2bd1a 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e  TempReg[pParse->
2bd1b 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52  nTempReg++] = iR
2bd1c 65 67 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  eg;..  }..}..../
2bd1d 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f  *..** Allocate o
2bd1e 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62  r deallocate a b
2bd1f 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e  lock of nReg con
2bd20 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
2bd21 72 73 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  rs..*/..SQLITE_P
2bd22 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2bd23 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50  e3GetTempRange(P
2bd24 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
2bd25 74 20 6e 52 65 67 29 7b 0d 0a 20 20 69 6e 74 20  t nReg){..  int 
2bd26 69 2c 20 6e 3b 0d 0a 20 20 69 20 3d 20 70 50 61  i, n;..  i = pPa
2bd27 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0d  rse->iRangeReg;.
2bd28 0a 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e  .  n = pParse->n
2bd29 52 61 6e 67 65 52 65 67 3b 0d 0a 20 20 69 66 28  RangeReg;..  if(
2bd2a 20 6e 52 65 67 3c 3d 6e 20 29 7b 0d 0a 20 20 20   nReg<=n ){..   
2bd2b 20 61 73 73 65 72 74 28 20 21 75 73 65 64 41 73   assert( !usedAs
2bd2c 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
2bd2d 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 3b  se, i, i+n-1) );
2bd2e 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52  ..    pParse->iR
2bd2f 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b  angeReg += nReg;
2bd30 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52  ..    pParse->nR
2bd31 61 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b  angeReg -= nReg;
2bd32 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
2bd33 69 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  i = pParse->nMem
2bd34 2b 31 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d  +1;..    pParse-
2bd35 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0d 0a  >nMem += nReg;..
2bd36 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 69 3b    }..  return i;
2bd37 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..}..SQLITE_PRIV
2bd38 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2bd39 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
2bd3a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2bd3b 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52  int iReg, int nR
2bd3c 65 67 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 45  eg){..  sqlite3E
2bd3d 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70  xprCacheRemove(p
2bd3e 50 61 72 73 65 2c 20 69 52 65 67 2c 20 6e 52 65  Parse, iReg, nRe
2bd3f 67 29 3b 0d 0a 20 20 69 66 28 20 6e 52 65 67 3e  g);..  if( nReg>
2bd40 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
2bd41 67 20 29 7b 0d 0a 20 20 20 20 70 50 61 72 73 65  g ){..    pParse
2bd42 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e 52  ->nRangeReg = nR
2bd43 65 67 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d  eg;..    pParse-
2bd44 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69 52 65  >iRangeReg = iRe
2bd45 67 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  g;..  }..}..../*
2bd46 0d 0a 2a 2a 20 4d 61 72 6b 20 61 6c 6c 20 74 65  ..** Mark all te
2bd47 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
2bd48 73 20 61 73 20 62 65 69 6e 67 20 75 6e 61 76 61  s as being unava
2bd49 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 75 73 65  ilable for reuse
2bd4a 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2bd4b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2bd4c 65 33 43 6c 65 61 72 54 65 6d 70 52 65 67 43 61  e3ClearTempRegCa
2bd4d 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  che(Parse *pPars
2bd4e 65 29 7b 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e  e){..  pParse->n
2bd4f 54 65 6d 70 52 65 67 20 3d 20 30 3b 0d 0a 20 20  TempReg = 0;..  
2bd50 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
2bd51 67 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a  g = 0;..}..../**
2bd52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2bd53 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a   of expr.c *****
2bd54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
2bd57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2bd58 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e 63  gin file alter.c
2bd59 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2bd5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
2bd5c 2a 0d 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75  *..** 2005 Febru
2bd5d 61 72 79 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ary 15..**..** T
2bd5e 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2bd5f 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2bd60 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2bd61 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
2bd62 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2bd63 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2bd64 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
2bd65 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2bd66 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2bd67 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2bd68 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2bd69 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2bd6a 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2bd6b 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
2bd6c 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2bd6d 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2bd6e 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
2bd6f 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
2bd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
2bd74 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2bd75 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
2bd76 74 69 6e 65 73 20 74 68 61 74 20 75 73 65 64 20  tines that used 
2bd77 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44 42 45  to generate VDBE
2bd78 20 63 6f 64 65 0d 0a 2a 2a 20 74 68 61 74 20 69   code..** that i
2bd79 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41 4c  mplements the AL
2bd7a 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e  TER TABLE comman
2bd7b 64 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  d...*/..../*..**
2bd7c 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
2bd7d 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73  s file only exis
2bd7e 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  ts if we are not
2bd7f 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0d 0a 2a   omitting the..*
2bd80 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f  * ALTER TABLE lo
2bd81 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69  gic from the bui
2bd82 6c 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  ld...*/..#ifndef
2bd83 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
2bd84 45 52 54 41 42 4c 45 0d 0a 0d 0a 0d 0a 2f 2a 0d  ERTABLE....../*.
2bd85 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2bd86 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c  n is used by SQL
2bd87 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d   generated to im
2bd88 70 6c 65 6d 65 6e 74 20 74 68 65 20 0d 0a 2a 2a  plement the ..**
2bd89 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d   ALTER TABLE com
2bd8a 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20  mand. The first 
2bd8b 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2bd8c 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45  text of a CREATE
2bd8d 20 54 41 42 4c 45 20 6f 72 0d 0a 2a 2a 20 43 52   TABLE or..** CR
2bd8e 45 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61  EATE INDEX comma
2bd8f 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nd. The second i
2bd90 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
2bd91 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
2bd92 6e 20 0d 0a 2a 2a 20 74 68 65 20 43 52 45 41 54  n ..** the CREAT
2bd93 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54  E TABLE or CREAT
2bd94 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e  E INDEX statemen
2bd95 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69  t is replaced wi
2bd96 74 68 20 74 68 65 20 74 68 69 72 64 0d 0a 2a 2a  th the third..**
2bd97 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68   argument and th
2bd98 65 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65  e result returne
2bd99 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0d 0a 2a 2a  d. Examples:..**
2bd9a 0d 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61  ..** sqlite_rena
2bd9b 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45  me_table('CREATE
2bd9c 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c   TABLE abc(a, b,
2bd9d 20 63 29 27 2c 20 27 64 65 66 27 29 0d 0a 2a 2a   c)', 'def')..**
2bd9e 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20       -> 'CREATE 
2bd9f 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62 2c 20  TABLE def(a, b, 
2bda0 63 29 27 0d 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69  c)'..**..** sqli
2bda1 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
2bda2 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
2bda3 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 66  ON abc(a)', 'def
2bda4 27 29 0d 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43  ')..**     -> 'C
2bda5 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e  REATE INDEX i ON
2bda6 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0d 0a   def(a, b, c)'..
2bda7 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2bda8 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28  renameTableFunc(
2bda9 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ..  sqlite3_cont
2bdaa 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20  ext *context,.. 
2bdab 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20   int NotUsed,.. 
2bdac 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2bdad 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 75 6e 73  *argv..){..  uns
2bdae 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
2bdaf 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
2bdb0 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2bdb1 5b 30 5d 29 3b 0d 0a 20 20 75 6e 73 69 67 6e 65  [0]);..  unsigne
2bdb2 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54  d char const *zT
2bdb3 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74  ableName = sqlit
2bdb4 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2bdb5 67 76 5b 31 5d 29 3b 0d 0a 0d 0a 20 20 69 6e 74  gv[1]);....  int
2bdb6 20 74 6f 6b 65 6e 3b 0d 0a 20 20 54 6f 6b 65 6e   token;..  Token
2bdb7 20 74 6e 61 6d 65 3b 0d 0a 20 20 75 6e 73 69 67   tname;..  unsig
2bdb8 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
2bdb9 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0d 0a 20 20  zCsr = zSql;..  
2bdba 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0d 0a 20 20  int len = 0;..  
2bdbb 63 68 61 72 20 2a 7a 52 65 74 3b 0d 0a 0d 0a 20  char *zRet;.... 
2bdbc 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2bdbd 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2bdbe 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2bdbf 29 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50  );....  UNUSED_P
2bdc0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
2bdc1 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 70  );....  /* The p
2bdc2 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f  rinciple used to
2bdc3 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   locate the tabl
2bdc4 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52  e name in the CR
2bdc5 45 41 54 45 20 54 41 42 4c 45 20 0d 0a 20 20 2a  EATE TABLE ..  *
2bdc6 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  * statement is t
2bdc7 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e 61  hat the table na
2bdc8 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  me is the first 
2bdc9 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b 65 6e 20  non-space token 
2bdca 74 68 61 74 0d 0a 20 20 2a 2a 20 69 73 20 69 6d  that..  ** is im
2bdcb 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
2bdcc 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 6f 72  ed by a TK_LP or
2bdcd 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 6e 2e   TK_USING token.
2bdce 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 7a 53  ..  */..  if( zS
2bdcf 71 6c 20 29 7b 0d 0a 20 20 20 20 64 6f 20 7b 0d  ql ){..    do {.
2bdd0 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73  .      if( !*zCs
2bdd1 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  r ){..        /*
2bdd2 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75   Ran out of inpu
2bdd3 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67  t before finding
2bdd4 20 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72 61 63   an opening brac
2bdd5 6b 65 74 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ket. Return NULL
2bdd6 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 65  . */..        re
2bdd7 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  turn;..      }..
2bdd8 0d 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65  ..      /* Store
2bdd9 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
2bdda 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69  zCsr points to i
2bddb 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0d 0a 20 20 20  n tname. */..   
2bddc 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68     tname.z = (ch
2bddd 61 72 2a 29 7a 43 73 72 3b 0d 0a 20 20 20 20 20  ar*)zCsr;..     
2bdde 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0d   tname.n = len;.
2bddf 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61  ...      /* Adva
2bde0 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20  nce zCsr to the 
2bde1 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72  next token. Stor
2bde2 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70  e that token typ
2bde3 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0d 0a 20  e in 'token',.. 
2bde4 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 73 20       ** and its 
2bde5 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 20  length in 'len' 
2bde6 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78 74  (to be used next
2bde7 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
2bde8 69 73 20 6c 6f 6f 70 29 2e 0d 0a 20 20 20 20 20  is loop)...     
2bde9 20 2a 2f 0d 0a 20 20 20 20 20 20 64 6f 20 7b 0d   */..      do {.
2bdea 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  .        zCsr +=
2bdeb 20 6c 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20 6c   len;..        l
2bdec 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  en = sqlite3GetT
2bded 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65  oken(zCsr, &toke
2bdee 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 20 77 68 69  n);..      } whi
2bdef 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50  le( token==TK_SP
2bdf0 41 43 45 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  ACE );..      as
2bdf1 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0d 0a  sert( len>0 );..
2bdf2 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b      } while( tok
2bdf3 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b  en!=TK_LP && tok
2bdf4 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0d  en!=TK_USING );.
2bdf5 0a 0d 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71  ...    zRet = sq
2bdf6 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2bdf7 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c   "%.*s\"%w\"%s",
2bdf8 20 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20   ((u8*)tname.z) 
2bdf9 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0d 0a  - zSql, zSql, ..
2bdfa 20 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d         zTableNam
2bdfb 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65  e, tname.z+tname
2bdfc 2e 6e 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  .n);..    sqlite
2bdfd 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
2bdfe 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c  ntext, zRet, -1,
2bdff 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
2be00 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
2be01 0a 2a 2a 20 54 68 69 73 20 43 20 66 75 6e 63 74  .** This C funct
2be02 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ion implements a
2be03 6e 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74  n SQL user funct
2be04 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 65 64  ion that is used
2be05 20 62 79 20 53 51 4c 20 63 6f 64 65 0d 0a 2a 2a   by SQL code..**
2be06 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
2be07 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e  e ALTER TABLE ..
2be08 2e 20 52 45 4e 41 4d 45 20 63 6f 6d 6d 61 6e 64  . RENAME command
2be09 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64   to modify the d
2be0a 65 66 69 6e 69 74 69 6f 6e 0d 0a 2a 2a 20 6f 66  efinition..** of
2be0b 20 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b 65 79   any foreign key
2be0c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61   constraints tha
2be0d 74 20 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  t use the table 
2be0e 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 61 73  being renamed as
2be0f 20 74 68 65 20 0d 0a 2a 2a 20 70 61 72 65 6e 74   the ..** parent
2be10 20 74 61 62 6c 65 2e 20 49 74 20 69 73 20 70 61   table. It is pa
2be11 73 73 65 64 20 74 68 72 65 65 20 61 72 67 75 6d  ssed three argum
2be12 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ents:..**..**   
2be13 31 29 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  1) The complete 
2be14 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
2be15 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
2be16 6e 74 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65  nt being modifie
2be17 64 2c 0d 0a 2a 2a 20 20 20 32 29 20 54 68 65 20  d,..**   2) The 
2be18 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  old name of the 
2be19 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61  table being rena
2be1a 6d 65 64 2c 20 61 6e 64 0d 0a 2a 2a 20 20 20 33  med, and..**   3
2be1b 29 20 54 68 65 20 6e 65 77 20 6e 61 6d 65 20 6f  ) The new name o
2be1c 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
2be1d 67 20 72 65 6e 61 6d 65 64 2e 0d 0a 2a 2a 0d 0a  g renamed...**..
2be1e 2a 2a 20 49 74 20 72 65 74 75 72 6e 73 20 74 68  ** It returns th
2be1f 65 20 6e 65 77 20 43 52 45 41 54 45 20 54 41 42  e new CREATE TAB
2be20 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 46 6f  LE statement. Fo
2be21 72 20 65 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a  r example:..**..
2be22 2a 2a 20 20 20 73 71 6c 69 74 65 5f 72 65 6e 61  **   sqlite_rena
2be23 6d 65 5f 70 61 72 65 6e 74 28 27 43 52 45 41 54  me_parent('CREAT
2be24 45 20 54 41 42 4c 45 20 74 31 28 61 20 52 45 46  E TABLE t1(a REF
2be25 45 52 45 4e 43 45 53 20 74 32 29 27 2c 20 27 74  ERENCES t2)', 't
2be26 32 27 2c 20 27 74 33 27 29 0d 0a 2a 2a 20 20 20  2', 't3')..**   
2be27 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54      -> 'CREATE T
2be28 41 42 4c 45 20 74 31 28 61 20 52 45 46 45 52 45  ABLE t1(a REFERE
2be29 4e 43 45 53 20 74 33 29 27 0d 0a 2a 2f 0d 0a 23  NCES t3)'..*/..#
2be2a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2be2b 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0d 0a  IT_FOREIGN_KEY..
2be2c 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61  static void rena
2be2d 6d 65 50 61 72 65 6e 74 46 75 6e 63 28 0d 0a 20  meParentFunc(.. 
2be2e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2be2f 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e   *context,..  in
2be30 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20 20 73 71  t NotUsed,..  sq
2be31 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2be32 67 76 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  gv..){..  sqlite
2be33 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2be34 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2be35 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 63  e(context);..  c
2be36 68 61 72 20 2a 7a 4f 75 74 70 75 74 20 3d 20 30  har *zOutput = 0
2be37 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 52 65 73 75  ;..  char *zResu
2be38 6c 74 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20  lt;..  unsigned 
2be39 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49 6e 70  char const *zInp
2be3a 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ut = sqlite3_val
2be3b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2be3c 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
2be3d 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 6c 64 20 3d  ar const *zOld =
2be3e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2be3f 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20  ext(argv[1]);.. 
2be40 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
2be41 6f 6e 73 74 20 2a 7a 4e 65 77 20 3d 20 73 71 6c  onst *zNew = sql
2be42 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2be43 61 72 67 76 5b 32 5d 29 3b 0d 0a 0d 0a 20 20 75  argv[2]);....  u
2be44 6e 73 69 67 6e 65 64 20 63 6f 6e 73 74 20 63 68  nsigned const ch
2be45 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f  ar *z;         /
2be46 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
2be47 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 3b 20  en */..  int n; 
2be48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be49 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
2be4a 74 68 20 6f 66 20 74 6f 6b 65 6e 20 7a 20 2a 2f  th of token z */
2be4b 0d 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 20 20  ..  int token;  
2be4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be4d 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74      /* Type of t
2be4e 6f 6b 65 6e 20 2a 2f 0d 0a 0d 0a 20 20 55 4e 55  oken */....  UNU
2be4f 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
2be50 74 55 73 65 64 29 3b 0d 0a 20 20 66 6f 72 28 7a  tUsed);..  for(z
2be51 3d 7a 49 6e 70 75 74 3b 20 2a 7a 3b 20 7a 3d 7a  =zInput; *z; z=z
2be52 2b 6e 29 7b 0d 0a 20 20 20 20 6e 20 3d 20 73 71  +n){..    n = sq
2be53 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 2c  lite3GetToken(z,
2be54 20 26 74 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 69   &token);..    i
2be55 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 52 45 46  f( token==TK_REF
2be56 45 52 45 4e 43 45 53 20 29 7b 0d 0a 20 20 20 20  ERENCES ){..    
2be57 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 3b    char *zParent;
2be58 0d 0a 20 20 20 20 20 20 64 6f 20 7b 0d 0a 20 20  ..      do {..  
2be59 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0d 0a 20        z += n;.. 
2be5a 20 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74         n = sqlit
2be5b 65 33 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74  e3GetToken(z, &t
2be5c 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 77  oken);..      }w
2be5d 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f  hile( token==TK_
2be5e 53 50 41 43 45 20 29 3b 0d 0a 0d 0a 20 20 20 20  SPACE );....    
2be5f 20 20 7a 50 61 72 65 6e 74 20 3d 20 73 71 6c 69    zParent = sqli
2be60 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
2be61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a   (const char *)z
2be62 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  , n);..      if(
2be63 20 7a 50 61 72 65 6e 74 3d 3d 30 20 29 20 62 72   zParent==0 ) br
2be64 65 61 6b 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  eak;..      sqli
2be65 74 65 33 44 65 71 75 6f 74 65 28 7a 50 61 72 65  te3Dequote(zPare
2be66 6e 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  nt);..      if( 
2be67 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
2be68 70 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  p((const char *)
2be69 7a 4f 6c 64 2c 20 7a 50 61 72 65 6e 74 29 20 29  zOld, zParent) )
2be6a 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  {..        char 
2be6b 2a 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 4d  *zOut = sqlite3M
2be6c 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 25 2e  Printf(db, "%s%.
2be6d 2a 73 5c 22 25 77 5c 22 22 2c 20 0d 0a 20 20 20  *s\"%w\"", ..   
2be6e 20 20 20 20 20 20 20 20 20 28 7a 4f 75 74 70 75           (zOutpu
2be6f 74 3f 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a  t?zOutput:""), z
2be70 2d 7a 49 6e 70 75 74 2c 20 7a 49 6e 70 75 74 2c  -zInput, zInput,
2be71 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a   (const char *)z
2be72 4e 65 77 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d  New..        );.
2be73 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2be74 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 70  DbFree(db, zOutp
2be75 75 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 7a 4f  ut);..        zO
2be76 75 74 70 75 74 20 3d 20 7a 4f 75 74 3b 0d 0a 20  utput = zOut;.. 
2be77 20 20 20 20 20 20 20 7a 49 6e 70 75 74 20 3d 20         zInput = 
2be78 26 7a 5b 6e 5d 3b 0d 0a 20 20 20 20 20 20 7d 0d  &z[n];..      }.
2be79 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2be7a 46 72 65 65 28 64 62 2c 20 7a 50 61 72 65 6e 74  Free(db, zParent
2be7b 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
2be7c 0d 0a 20 20 7a 52 65 73 75 6c 74 20 3d 20 73 71  ..  zResult = sq
2be7d 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2be7e 20 22 25 73 25 73 22 2c 20 28 7a 4f 75 74 70 75   "%s%s", (zOutpu
2be7f 74 3f 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a  t?zOutput:""), z
2be80 49 6e 70 75 74 29 2c 20 0d 0a 20 20 73 71 6c 69  Input), ..  sqli
2be81 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2be82 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74  context, zResult
2be83 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e  , -1, SQLITE_DYN
2be84 41 4d 49 43 29 3b 0d 0a 20 20 73 71 6c 69 74 65  AMIC);..  sqlite
2be85 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74  3DbFree(db, zOut
2be86 70 75 74 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  put);..}..#endif
2be87 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
2be88 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d  TE_OMIT_TRIGGER.
2be89 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
2be8a 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c  n is used by SQL
2be8b 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d   generated to im
2be8c 70 6c 65 6d 65 6e 74 20 74 68 65 0d 0a 2a 2a 20  plement the..** 
2be8d 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
2be8e 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20 61  and. The first a
2be8f 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74  rgument is the t
2be90 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45 20  ext of a CREATE 
2be91 54 52 49 47 47 45 52 20 0d 0a 2a 2a 20 73 74 61  TRIGGER ..** sta
2be92 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f  tement. The seco
2be93 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61  nd is a table na
2be94 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61  me. The table na
2be95 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
2be96 20 0d 0a 2a 2a 20 54 52 49 47 47 45 52 20 73 74   ..** TRIGGER st
2be97 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61  atement is repla
2be98 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69  ced with the thi
2be99 72 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20  rd argument and 
2be9a 74 68 65 20 72 65 73 75 6c 74 20 0d 0a 2a 2a 20  the result ..** 
2be9b 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69  returned. This i
2be9c 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f 20 72  s analagous to r
2be9d 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 29  enameTableFunc()
2be9e 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74 20 66   above, except f
2be9f 6f 72 20 43 52 45 41 54 45 0d 0a 2a 2a 20 54 52  or CREATE..** TR
2bea0 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41 54  IGGER, not CREAT
2bea1 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45 41  E INDEX and CREA
2bea2 54 45 20 54 41 42 4c 45 2e 0d 0a 2a 2f 0d 0a 73  TE TABLE...*/..s
2bea3 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d  tatic void renam
2bea4 65 54 72 69 67 67 65 72 46 75 6e 63 28 0d 0a 20  eTriggerFunc(.. 
2bea5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2bea6 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e   *context,..  in
2bea7 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20 20 73 71  t NotUsed,..  sq
2bea8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2bea9 67 76 0d 0a 29 7b 0d 0a 20 20 75 6e 73 69 67 6e  gv..){..  unsign
2beaa 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
2beab 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Sql = sqlite3_va
2beac 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2bead 29 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  );..  unsigned c
2beae 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c  har const *zTabl
2beaf 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  eName = sqlite3_
2beb0 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2beb1 31 5d 29 3b 0d 0a 0d 0a 20 20 69 6e 74 20 74 6f  1]);....  int to
2beb2 6b 65 6e 3b 0d 0a 20 20 54 6f 6b 65 6e 20 74 6e  ken;..  Token tn
2beb3 61 6d 65 3b 0d 0a 20 20 69 6e 74 20 64 69 73 74  ame;..  int dist
2beb4 20 3d 20 33 3b 0d 0a 20 20 75 6e 73 69 67 6e 65   = 3;..  unsigne
2beb5 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43  d char const *zC
2beb6 73 72 20 3d 20 7a 53 71 6c 3b 0d 0a 20 20 69 6e  sr = zSql;..  in
2beb7 74 20 6c 65 6e 20 3d 20 30 3b 0d 0a 20 20 63 68  t len = 0;..  ch
2beb8 61 72 20 2a 7a 52 65 74 3b 0d 0a 20 20 73 71 6c  ar *zRet;..  sql
2beb9 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
2beba 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2bebb 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a  ndle(context);..
2bebc 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
2bebd 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a  ETER(NotUsed);..
2bebe 0d 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63  ..  /* The princ
2bebf 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63  iple used to loc
2bec0 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ate the table na
2bec1 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
2bec2 20 54 52 49 47 47 45 52 20 0d 0a 20 20 2a 2a 20   TRIGGER ..  ** 
2bec3 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61  statement is tha
2bec4 74 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  t the table name
2bec5 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f   is the first to
2bec6 6b 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65  ken that is imme
2bec7 64 69 61 74 65 64 6c 79 0d 0a 20 20 2a 2a 20 70  diatedly..  ** p
2bec8 72 65 63 65 64 65 64 20 62 79 20 65 69 74 68 65  receded by eithe
2bec9 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f  r TK_ON or TK_DO
2beca 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64  T and immediated
2becb 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f  ly followed by o
2becc 6e 65 0d 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57  ne..  ** of TK_W
2becd 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72  HEN, TK_BEGIN or
2bece 20 54 4b 5f 46 4f 52 2e 0d 0a 20 20 2a 2f 0d 0a   TK_FOR...  */..
2becf 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0d 0a 20    if( zSql ){.. 
2bed0 20 20 20 64 6f 20 7b 0d 0a 0d 0a 20 20 20 20 20     do {....     
2bed1 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b 0d 0a   if( !*zCsr ){..
2bed2 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f          /* Ran o
2bed3 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66 6f  ut of input befo
2bed4 72 65 20 66 69 6e 64 69 6e 67 20 74 68 65 20 74  re finding the t
2bed5 61 62 6c 65 20 6e 61 6d 65 2e 20 52 65 74 75 72  able name. Retur
2bed6 6e 20 4e 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 20 20  n NULL. */..    
2bed7 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20      return;..   
2bed8 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
2bed9 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e   Store the token
2beda 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74   that zCsr point
2bedb 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a  s to in tname. *
2bedc 2f 0d 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a  /..      tname.z
2bedd 20 3d 20 28 63 68 61 72 2a 29 7a 43 73 72 3b 0d   = (char*)zCsr;.
2bede 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d  .      tname.n =
2bedf 20 6c 65 6e 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f   len;....      /
2bee0 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74  * Advance zCsr t
2bee1 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  o the next token
2bee2 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b  . Store that tok
2bee3 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65  en type in 'toke
2bee4 6e 27 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e  n',..      ** an
2bee5 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20  d its length in 
2bee6 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65  'len' (to be use
2bee7 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  d next iteration
2bee8 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0d   of this loop)..
2bee9 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
2beea 20 64 6f 20 7b 0d 0a 20 20 20 20 20 20 20 20 7a   do {..        z
2beeb 43 73 72 20 2b 3d 20 6c 65 6e 3b 0d 0a 20 20 20  Csr += len;..   
2beec 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74       len = sqlit
2beed 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c  e3GetToken(zCsr,
2beee 20 26 74 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20   &token);..     
2beef 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d   }while( token==
2bef0 54 4b 5f 53 50 41 43 45 20 29 3b 0d 0a 20 20 20  TK_SPACE );..   
2bef1 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30     assert( len>0
2bef2 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20   );....      /* 
2bef3 56 61 72 69 61 62 6c 65 20 27 64 69 73 74 27 20  Variable 'dist' 
2bef4 73 74 6f 72 65 73 20 74 68 65 20 6e 75 6d 62 65  stores the numbe
2bef5 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 61 64  r of tokens read
2bef6 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 0d   since the most.
2bef7 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 65 6e 74  .      ** recent
2bef8 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f 4e   TK_DOT or TK_ON
2bef9 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61  . This means tha
2befa 74 20 77 68 65 6e 20 61 20 57 48 45 4e 2c 20 46  t when a WHEN, F
2befb 4f 52 20 6f 72 20 42 45 47 49 4e 20 0d 0a 20 20  OR or BEGIN ..  
2befc 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 20      ** token is 
2befd 72 65 61 64 20 61 6e 64 20 27 64 69 73 74 27 20  read and 'dist' 
2befe 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20 63 6f  equals 2, the co
2beff 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64 20 61  ndition stated a
2bf00 62 6f 76 65 0d 0a 20 20 20 20 20 20 2a 2a 20 74  bove..      ** t
2bf01 6f 20 62 65 20 6d 65 74 2e 0d 0a 20 20 20 20 20  o be met...     
2bf02 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 4e 6f   **..      ** No
2bf03 74 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f  te that ON canno
2bf04 74 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c  t be a database,
2bf05 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e   table or column
2bf06 20 6e 61 6d 65 2c 20 73 6f 0d 0a 20 20 20 20 20   name, so..     
2bf07 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20   ** there is no 
2bf08 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62  need to worry ab
2bf09 6f 75 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20  out syntax like 
2bf0a 0d 0a 20 20 20 20 20 20 2a 2a 20 22 43 52 45 41  ..      ** "CREA
2bf0b 54 45 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f  TE TRIGGER ... O
2bf0c 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e  N ON.ON BEGIN ..
2bf0d 2e 22 20 65 74 63 2e 0d 0a 20 20 20 20 20 20 2a  ." etc...      *
2bf0e 2f 0d 0a 20 20 20 20 20 20 64 69 73 74 2b 2b 3b  /..      dist++;
2bf0f 0d 0a 20 20 20 20 20 20 69 66 28 20 74 6f 6b 65  ..      if( toke
2bf10 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b  n==TK_DOT || tok
2bf11 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0d 0a 20 20  en==TK_ON ){..  
2bf12 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b 0d        dist = 0;.
2bf13 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 20  .      }..    } 
2bf14 77 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c  while( dist!=2 |
2bf15 7c 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45  | (token!=TK_WHE
2bf16 4e 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46  N && token!=TK_F
2bf17 4f 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f  OR && token!=TK_
2bf18 42 45 47 49 4e 29 20 29 3b 0d 0a 0d 0a 20 20 20  BEGIN) );....   
2bf19 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6e 61   /* Variable tna
2bf1a 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20  me now contains 
2bf1b 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 69  the token that i
2bf1c 73 20 74 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d  s the old table-
2bf1d 6e 61 6d 65 0d 0a 20 20 20 20 2a 2a 20 69 6e 20  name..    ** in 
2bf1e 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47  the CREATE TRIGG
2bf1f 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 20  ER statement... 
2bf20 20 20 20 2a 2f 0d 0a 20 20 20 20 7a 52 65 74 20     */..    zRet 
2bf21 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2bf22 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22  (db, "%.*s\"%w\"
2bf23 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d 65  %s", ((u8*)tname
2bf24 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c  .z) - zSql, zSql
2bf25 2c 20 0d 0a 20 20 20 20 20 20 20 7a 54 61 62 6c  , ..       zTabl
2bf26 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74  eName, tname.z+t
2bf27 6e 61 6d 65 2e 6e 29 3b 0d 0a 20 20 20 20 73 71  name.n);..    sq
2bf28 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2bf29 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c  t(context, zRet,
2bf2a 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41   -1, SQLITE_DYNA
2bf2b 4d 49 43 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23  MIC);..  }..}..#
2bf2c 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49  endif   /* !SQLI
2bf2d 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
2bf2e 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67  */..../*..** Reg
2bf2f 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66  ister built-in f
2bf30 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f  unctions used to
2bf31 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20   help implement 
2bf32 41 4c 54 45 52 20 54 41 42 4c 45 0d 0a 2a 2f 0d  ALTER TABLE..*/.
2bf33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2bf34 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
2bf35 72 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29  rFunctions(void)
2bf36 7b 0d 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49  {..  static SQLI
2bf37 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61  TE_WSD FuncDef a
2bf38 41 6c 74 65 72 54 61 62 6c 65 46 75 6e 63 73 5b  AlterTableFuncs[
2bf39 5d 20 3d 20 7b 0d 0a 20 20 20 20 46 55 4e 43 54  ] = {..    FUNCT
2bf3a 49 4f 4e 28 73 71 6c 69 74 65 5f 72 65 6e 61 6d  ION(sqlite_renam
2bf3b 65 5f 74 61 62 6c 65 2c 20 20 20 32 2c 20 30 2c  e_table,   2, 0,
2bf3c 20 30 2c 20 72 65 6e 61 6d 65 54 61 62 6c 65 46   0, renameTableF
2bf3d 75 6e 63 29 2c 0d 0a 23 69 66 6e 64 65 66 20 53  unc),..#ifndef S
2bf3e 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2bf3f 45 52 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  ER..    FUNCTION
2bf40 28 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74  (sqlite_rename_t
2bf41 72 69 67 67 65 72 2c 20 32 2c 20 30 2c 20 30 2c  rigger, 2, 0, 0,
2bf42 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75   renameTriggerFu
2bf43 6e 63 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  nc),..#endif..#i
2bf44 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2bf45 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0d 0a 20  T_FOREIGN_KEY.. 
2bf46 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69     FUNCTION(sqli
2bf47 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74  te_rename_parent
2bf48 2c 20 20 33 2c 20 30 2c 20 30 2c 20 72 65 6e 61  ,  3, 0, 0, rena
2bf49 6d 65 50 61 72 65 6e 74 46 75 6e 63 29 2c 0d 0a  meParentFunc),..
2bf4a 23 65 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a 20 20  #endif..  };..  
2bf4b 69 6e 74 20 69 3b 0d 0a 20 20 46 75 6e 63 44 65  int i;..  FuncDe
2bf4c 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
2bf4d 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
2bf4e 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
2bf4f 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0d 0a 20 20  lFunctions);..  
2bf50 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d  FuncDef *aFunc =
2bf51 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42   (FuncDef*)&GLOB
2bf52 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 41 6c 74  AL(FuncDef, aAlt
2bf53 65 72 54 61 62 6c 65 46 75 6e 63 73 29 3b 0d 0a  erTableFuncs);..
2bf54 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41  ..  for(i=0; i<A
2bf55 72 72 61 79 53 69 7a 65 28 61 41 6c 74 65 72 54  rraySize(aAlterT
2bf56 61 62 6c 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29  ableFuncs); i++)
2bf57 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75  {..    sqlite3Fu
2bf58 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73  ncDefInsert(pHas
2bf59 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0d 0a  h, &aFunc[i]);..
2bf5a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
2bf5b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2bf5c 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  s used to create
2bf5d 20 74 68 65 20 74 65 78 74 20 6f 66 20 65 78 70   the text of exp
2bf5e 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  ressions of the 
2bf5f 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  form:..**..**   
2bf60 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 31 3e  name=<constant1>
2bf61 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61   OR name=<consta
2bf62 6e 74 32 3e 20 4f 52 20 2e 2e 2e 0d 0a 2a 2a 0d  nt2> OR .....**.
2bf63 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20  .** If argument 
2bf64 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20  zWhere is NULL, 
2bf65 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 73  then a pointer s
2bf66 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
2bf67 20 74 68 65 20 74 65 78 74 20 0d 0a 2a 2a 20 22   the text ..** "
2bf68 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e 22  name=<constant>"
2bf69 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 77 68   is returned, wh
2bf6a 65 72 65 20 3c 63 6f 6e 73 74 61 6e 74 3e 20 69  ere <constant> i
2bf6b 73 20 74 68 65 20 71 75 6f 74 65 64 20 76 65 72  s the quoted ver
2bf6c 73 69 6f 6e 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  sion..** of the 
2bf6d 73 74 72 69 6e 67 20 70 61 73 73 65 64 20 61 73  string passed as
2bf6e 20 61 72 67 75 6d 65 6e 74 20 7a 43 6f 6e 73 74   argument zConst
2bf6f 61 6e 74 2e 20 54 68 65 20 72 65 74 75 72 6e 65  ant. The returne
2bf70 64 20 62 75 66 66 65 72 20 69 73 0d 0a 2a 2a 20  d buffer is..** 
2bf71 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
2bf72 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
2bf73 29 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73  ). It is the res
2bf74 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
2bf75 68 65 0d 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  he..** caller to
2bf76 20 65 6e 73 75 72 65 20 74 68 61 74 20 69 74 20   ensure that it 
2bf77 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72  is eventually fr
2bf78 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  eed...**..** If 
2bf79 61 72 67 75 6d 65 6e 74 20 7a 57 68 65 72 65 20  argument zWhere 
2bf7a 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
2bf7b 6e 20 74 68 65 20 73 74 72 69 6e 67 20 72 65 74  n the string ret
2bf7c 75 72 6e 65 64 20 69 73 20 0d 0a 2a 2a 20 22 3c  urned is ..** "<
2bf7d 77 68 65 72 65 3e 20 4f 52 20 6e 61 6d 65 3d 3c  where> OR name=<
2bf7e 63 6f 6e 73 74 61 6e 74 3e 22 2c 20 77 68 65 72  constant>", wher
2bf7f 65 20 3c 77 68 65 72 65 3e 20 69 73 20 74 68 65  e <where> is the
2bf80 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 57 68   contents of zWh
2bf81 65 72 65 2e 0d 0a 2a 2a 20 49 6e 20 74 68 69 73  ere...** In this
2bf82 20 63 61 73 65 20 7a 57 68 65 72 65 20 69 73 20   case zWhere is 
2bf83 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
2bf84 33 44 62 46 72 65 65 28 29 20 62 65 66 6f 72 65  3DbFree() before
2bf85 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 20   returning...** 
2bf86 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 68 61  ..*/..static cha
2bf87 72 20 2a 77 68 65 72 65 4f 72 4e 61 6d 65 28 73  r *whereOrName(s
2bf88 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72  qlite3 *db, char
2bf89 20 2a 7a 57 68 65 72 65 2c 20 63 68 61 72 20 2a   *zWhere, char *
2bf8a 7a 43 6f 6e 73 74 61 6e 74 29 7b 0d 0a 20 20 63  zConstant){..  c
2bf8b 68 61 72 20 2a 7a 4e 65 77 3b 0d 0a 20 20 69 66  har *zNew;..  if
2bf8c 28 20 21 7a 57 68 65 72 65 20 29 7b 0d 0a 20 20  ( !zWhere ){..  
2bf8d 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
2bf8e 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
2bf8f 65 3d 25 51 22 2c 20 7a 43 6f 6e 73 74 61 6e 74  e=%Q", zConstant
2bf90 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  );..  }else{..  
2bf91 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
2bf92 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20  MPrintf(db, "%s 
2bf93 4f 52 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68  OR name=%Q", zWh
2bf94 65 72 65 2c 20 7a 43 6f 6e 73 74 61 6e 74 29 3b  ere, zConstant);
2bf95 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ..    sqlite3DbF
2bf96 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b  ree(db, zWhere);
2bf97 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2bf98 7a 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20  zNew;..}....#if 
2bf99 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2bf9a 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
2bf9b 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2bf9c 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2bf9d 52 29 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  R)../*..** Gener
2bf9e 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20  ate the text of 
2bf9f 61 20 57 48 45 52 45 20 65 78 70 72 65 73 73 69  a WHERE expressi
2bfa0 6f 6e 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  on which can be 
2bfa1 75 73 65 64 20 74 6f 20 73 65 6c 65 63 74 20 61  used to select a
2bfa2 6c 6c 0d 0a 2a 2a 20 74 61 62 6c 65 73 20 74 68  ll..** tables th
2bfa3 61 74 20 68 61 76 65 20 66 6f 72 65 69 67 6e 20  at have foreign 
2bfa4 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  key constraints 
2bfa5 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 61  that refer to ta
2bfa6 62 6c 65 20 70 54 61 62 20 28 69 2e 65 2e 0d 0a  ble pTab (i.e...
2bfa7 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  ** constraints f
2bfa8 6f 72 20 77 68 69 63 68 20 70 54 61 62 20 69 73  or which pTab is
2bfa9 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
2bfaa 65 29 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69  e) from the sqli
2bfab 74 65 5f 6d 61 73 74 65 72 0d 0a 2a 2a 20 74 61  te_master..** ta
2bfac 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ble...*/..static
2bfad 20 63 68 61 72 20 2a 77 68 65 72 65 46 6f 72 65   char *whereFore
2bfae 69 67 6e 4b 65 79 73 28 50 61 72 73 65 20 2a 70  ignKeys(Parse *p
2bfaf 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2bfb0 61 62 29 7b 0d 0a 20 20 46 4b 65 79 20 2a 70 3b  ab){..  FKey *p;
2bfb1 0d 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65  ..  char *zWhere
2bfb2 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28 70 3d 73   = 0;..  for(p=s
2bfb3 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63  qlite3FkReferenc
2bfb4 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70  es(pTab); p; p=p
2bfb5 2d 3e 70 4e 65 78 74 54 6f 29 7b 0d 0a 20 20 20  ->pNextTo){..   
2bfb6 20 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65 4f   zWhere = whereO
2bfb7 72 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e 64 62  rName(pParse->db
2bfb8 2c 20 7a 57 68 65 72 65 2c 20 70 2d 3e 70 46 72  , zWhere, p->pFr
2bfb9 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 7d  om->zName);..  }
2bfba 0d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72  ..  return zWher
2bfbb 65 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  e;..}..#endif...
2bfbc 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ./*..** Generate
2bfbd 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57   the text of a W
2bfbe 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20  HERE expression 
2bfbf 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
2bfc0 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0d  d to select all.
2bfc1 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72  .** temporary tr
2bfc2 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20  iggers on table 
2bfc3 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71  pTab from the sq
2bfc4 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72  lite_temp_master
2bfc5 20 74 61 62 6c 65 2e 20 49 66 0d 0a 2a 2a 20 74   table. If..** t
2bfc6 61 62 6c 65 20 70 54 61 62 20 68 61 73 20 6e 6f  able pTab has no
2bfc7 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67   temporary trigg
2bfc8 65 72 73 2c 20 6f 72 20 69 73 20 69 74 73 65 6c  ers, or is itsel
2bfc9 66 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  f stored in the 
2bfca 0d 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 64  ..** temporary d
2bfcb 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73  atabase, NULL is
2bfcc 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a   returned...*/..
2bfcd 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68 65  static char *whe
2bfce 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 50  reTempTriggers(P
2bfcf 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2bfd0 62 6c 65 20 2a 70 54 61 62 29 7b 0d 0a 20 20 54  ble *pTab){..  T
2bfd1 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0d 0a  rigger *pTrig;..
2bfd2 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d    char *zWhere =
2bfd3 20 30 3b 0d 0a 20 20 63 6f 6e 73 74 20 53 63 68   0;..  const Sch
2bfd4 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61  ema *pTempSchema
2bfd5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
2bfd6 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f  Db[1].pSchema; /
2bfd7 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61  * Temp db schema
2bfd8 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74   */....  /* If t
2bfd9 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
2bfda 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74  located in the t
2bfdb 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68  emp-db (in which
2bfdc 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0d 0a   case NULL is ..
2bfdd 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c    ** returned, l
2bfde 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
2bfdf 74 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74  tables list of t
2bfe0 72 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63  riggers. For eac
2bfe1 68 20 74 72 69 67 67 65 72 0d 0a 20 20 2a 2a 20  h trigger..  ** 
2bfe2 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74  that is not part
2bfe3 20 6f 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20   of the temp-db 
2bfe4 73 63 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c  schema, add a cl
2bfe5 61 75 73 65 20 74 6f 20 74 68 65 20 57 48 45 52  ause to the WHER
2bfe6 45 20 0d 0a 20 20 2a 2a 20 65 78 70 72 65 73 73  E ..  ** express
2bfe7 69 6f 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20  ion being built 
2bfe8 75 70 20 69 6e 20 7a 57 68 65 72 65 2e 0d 0a 20  up in zWhere... 
2bfe9 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54 61 62 2d   */..  if( pTab-
2bfea 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d 70 53  >pSchema!=pTempS
2bfeb 63 68 65 6d 61 20 29 7b 0d 0a 20 20 20 20 73 71  chema ){..    sq
2bfec 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2bfed 73 65 2d 3e 64 62 3b 0d 0a 20 20 20 20 66 6f 72  se->db;..    for
2bfee 28 70 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72  (pTrig=sqlite3Tr
2bfef 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65  iggerList(pParse
2bff0 2c 20 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20  , pTab); pTrig; 
2bff1 70 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65  pTrig=pTrig->pNe
2bff2 78 74 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  xt){..      if( 
2bff3 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d  pTrig->pSchema==
2bff4 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0d 0a  pTempSchema ){..
2bff5 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d          zWhere =
2bff6 20 77 68 65 72 65 4f 72 4e 61 6d 65 28 64 62 2c   whereOrName(db,
2bff7 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d 3e   zWhere, pTrig->
2bff8 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 7d  zName);..      }
2bff9 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
2bffa 69 66 28 20 7a 57 68 65 72 65 20 29 7b 0d 0a 20  if( zWhere ){.. 
2bffb 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20     char *zNew = 
2bffc 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70  sqlite3MPrintf(p
2bffd 50 61 72 73 65 2d 3e 64 62 2c 20 22 74 79 70 65  Parse->db, "type
2bffe 3d 27 74 72 69 67 67 65 72 27 20 41 4e 44 20 28  ='trigger' AND (
2bfff 25 73 29 22 2c 20 7a 57 68 65 72 65 29 3b 0d 0a  %s)", zWhere);..
2c000 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2c001 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 57  e(pParse->db, zW
2c002 68 65 72 65 29 3b 0d 0a 20 20 20 20 7a 57 68 65  here);..    zWhe
2c003 72 65 20 3d 20 7a 4e 65 77 3b 0d 0a 20 20 7d 0d  re = zNew;..  }.
2c004 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65  .  return zWhere
2c005 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  ;..}..../*..** G
2c006 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
2c007 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20  drop and reload 
2c008 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
2c009 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
2c00a 61 62 6c 65 0d 0a 2a 2a 20 70 54 61 62 20 66 72  able..** pTab fr
2c00b 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  om the database,
2c00c 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67   including trigg
2c00d 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72  ers and temporar
2c00e 79 20 74 72 69 67 67 65 72 73 2e 0d 0a 2a 2a 20  y triggers...** 
2c00f 41 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69  Argument zName i
2c010 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
2c011 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  e table in the d
2c012 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61  atabase schema a
2c013 74 0d 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74  t..** the time t
2c014 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  he generated cod
2c015 65 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54  e is executed. T
2c016 68 69 73 20 63 61 6e 20 62 65 20 64 69 66 66 65  his can be diffe
2c017 72 65 6e 74 20 66 72 6f 6d 0d 0a 2a 2a 20 70 54  rent from..** pT
2c018 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69  ab->zName if thi
2c019 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
2c01a 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f  ing called to co
2c01b 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20 0d 0a  de part of an ..
2c01c 2a 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  ** "ALTER TABLE 
2c01d 52 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74 65  RENAME TO" state
2c01e 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ment...*/..stati
2c01f 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61 62  c void reloadTab
2c020 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a  leSchema(Parse *
2c021 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
2c022 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Tab, const char 
2c023 2a 7a 4e 61 6d 65 29 7b 0d 0a 20 20 56 64 62 65  *zName){..  Vdbe
2c024 20 2a 76 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 57   *v;..  char *zW
2c025 68 65 72 65 3b 0d 0a 20 20 69 6e 74 20 69 44 62  here;..  int iDb
2c026 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c027 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2c028 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
2c029 69 6e 67 20 70 54 61 62 20 2a 2f 0d 0a 23 69 66  ing pTab */..#if
2c02a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c02b 5f 54 52 49 47 47 45 52 0d 0a 20 20 54 72 69 67  _TRIGGER..  Trig
2c02c 67 65 72 20 2a 70 54 72 69 67 3b 0d 0a 23 65 6e  ger *pTrig;..#en
2c02d 64 69 66 0d 0a 0d 0a 20 20 76 20 3d 20 73 71 6c  dif....  v = sql
2c02e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2c02f 73 65 29 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45  se);..  if( NEVE
2c030 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
2c031 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
2c032 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
2c033 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d  lMutexes(pParse-
2c034 3e 64 62 29 20 29 3b 0d 0a 20 20 69 44 62 20 3d  >db) );..  iDb =
2c035 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2c036 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2c037 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2c038 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  ;..  assert( iDb
2c039 3e 3d 30 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65  >=0 );....#ifnde
2c03a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2c03b 49 47 47 45 52 0d 0a 20 20 2f 2a 20 44 72 6f 70  IGGER..  /* Drop
2c03c 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69 67 67   any table trigg
2c03d 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74  ers from the int
2c03e 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 2a 2f  ernal schema. */
2c03f 0d 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 73 71  ..  for(pTrig=sq
2c040 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74  lite3TriggerList
2c041 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 20  (pParse, pTab); 
2c042 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72  pTrig; pTrig=pTr
2c043 69 67 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20  ig->pNext){..   
2c044 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73   int iTrigDb = s
2c045 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2c046 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
2c047 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b  pTrig->pSchema);
2c048 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54  ..    assert( iT
2c049 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54  rigDb==iDb || iT
2c04a 72 69 67 44 62 3d 3d 31 20 29 3b 0d 0a 20 20 20  rigDb==1 );..   
2c04b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c04c 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69  p4(v, OP_DropTri
2c04d 67 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30  gger, iTrigDb, 0
2c04e 2c 20 30 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d  , 0, pTrig->zNam
2c04f 65 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  e, 0);..  }..#en
2c050 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 44 72 6f 70  dif....  /* Drop
2c051 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
2c052 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e  ndex from the in
2c053 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 20  ternal schema.  
2c054 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  */..  sqlite3Vdb
2c055 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
2c056 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c  opTable, iDb, 0,
2c057 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
2c058 20 30 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6c   0);....  /* Rel
2c059 6f 61 64 20 74 68 65 20 74 61 62 6c 65 2c 20 69  oad the table, i
2c05a 6e 64 65 78 20 61 6e 64 20 70 65 72 6d 61 6e 65  ndex and permane
2c05b 6e 74 20 74 72 69 67 67 65 72 20 73 63 68 65 6d  nt trigger schem
2c05c 61 73 2e 20 2a 2f 0d 0a 20 20 7a 57 68 65 72 65  as. */..  zWhere
2c05d 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2c05e 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 74  f(pParse->db, "t
2c05f 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61  bl_name=%Q", zNa
2c060 6d 65 29 3b 0d 0a 20 20 69 66 28 20 21 7a 57 68  me);..  if( !zWh
2c061 65 72 65 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  ere ) return;.. 
2c062 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 50   sqlite3VdbeAddP
2c063 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76 2c 20  arseSchemaOp(v, 
2c064 69 44 62 2c 20 7a 57 68 65 72 65 29 3b 0d 0a 0d  iDb, zWhere);...
2c065 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c066 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20  OMIT_TRIGGER..  
2c067 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 20 74  /* Now, if the t
2c068 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72  able is not stor
2c069 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64  ed in the temp d
2c06a 61 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 64 20  atabase, reload 
2c06b 61 6e 79 20 74 65 6d 70 20 0d 0a 20 20 2a 2a 20  any temp ..  ** 
2c06c 74 72 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 20  triggers. Don't 
2c06d 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63  use IN(...) in c
2c06e 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ase SQLITE_OMIT_
2c06f 53 55 42 51 55 45 52 59 20 69 73 20 64 65 66 69  SUBQUERY is defi
2c070 6e 65 64 2e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ned. ..  */..  i
2c071 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65  f( (zWhere=where
2c072 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61  TempTriggers(pPa
2c073 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29  rse, pTab))!=0 )
2c074 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
2c075 62 65 41 64 64 50 61 72 73 65 53 63 68 65 6d 61  beAddParseSchema
2c076 4f 70 28 76 2c 20 31 2c 20 7a 57 68 65 72 65 29  Op(v, 1, zWhere)
2c077 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
2c078 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 61  }..../*..** Para
2c079 6d 65 74 65 72 20 7a 4e 61 6d 65 20 69 73 20 74  meter zName is t
2c07a 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62  he name of a tab
2c07b 6c 65 20 74 68 61 74 20 69 73 20 61 62 6f 75 74  le that is about
2c07c 20 74 6f 20 62 65 20 61 6c 74 65 72 65 64 0d 0a   to be altered..
2c07d 2a 2a 20 28 65 69 74 68 65 72 20 77 69 74 68 20  ** (either with 
2c07e 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20  ALTER TABLE ... 
2c07f 52 45 4e 41 4d 45 20 54 4f 20 6f 72 20 41 4c 54  RENAME TO or ALT
2c080 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44  ER TABLE ... ADD
2c081 20 43 4f 4c 55 4d 4e 29 2e 0d 0a 2a 2a 20 49 66   COLUMN)...** If
2c082 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   the table is a 
2c083 73 79 73 74 65 6d 20 74 61 62 6c 65 2c 20 74 68  system table, th
2c084 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c 65 61 76  is function leav
2c085 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  es an error mess
2c086 61 67 65 0d 0a 2a 2a 20 69 6e 20 70 50 61 72 73  age..** in pPars
2c087 65 2d 3e 7a 45 72 72 20 28 73 79 73 74 65 6d 20  e->zErr (system 
2c088 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62  tables may not b
2c089 65 20 61 6c 74 65 72 65 64 29 20 61 6e 64 20 72  e altered) and r
2c08a 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  eturns non-zero.
2c08b 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 72 2c 20 69 66 20  ..**..** Or, if 
2c08c 7a 4e 61 6d 65 20 69 73 20 6e 6f 74 20 61 20 73  zName is not a s
2c08d 79 73 74 65 6d 20 74 61 62 6c 65 2c 20 7a 65 72  ystem table, zer
2c08e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  o is returned...
2c08f 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69  */..static int i
2c090 73 53 79 73 74 65 6d 54 61 62 6c 65 28 50 61 72  sSystemTable(Par
2c091 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
2c092 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0d  t char *zName){.
2c093 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2c094 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3e 36 20  rlen30(zName)>6 
2c095 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
2c096 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71  NICmp(zName, "sq
2c097 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0d 0a 20  lite_", 7) ){.. 
2c098 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2c099 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
2c09a 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
2c09b 61 6c 74 65 72 65 64 22 2c 20 7a 4e 61 6d 65 29  altered", zName)
2c09c 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ;..    return 1;
2c09d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2c09e 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
2c09f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2c0a0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22   implement the "
2c0a1 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78 20  ALTER TABLE xxx 
2c0a2 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0d  RENAME TO yyy" .
2c0a3 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0d 0a 2a  .** command. ..*
2c0a4 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2c0a5 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
2c0a6 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0d  terRenameTable(.
2c0a7 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2c0a8 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2c0a9 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  Parser context. 
2c0aa 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  */..  SrcList *p
2c0ab 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2c0ac 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
2c0ad 72 65 6e 61 6d 65 2e 20 2a 2f 0d 0a 20 20 54 6f  rename. */..  To
2c0ae 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20  ken *pName      
2c0af 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
2c0b0 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a  ew table name. *
2c0b1 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 44 62  /..){..  int iDb
2c0b2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c0b3 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74     /* Database t
2c0b4 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
2c0b5 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 63 68 61   table */..  cha
2c0b6 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
2c0b7 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
2c0b8 66 20 64 61 74 61 62 61 73 65 20 69 44 62 20 2a  f database iDb *
2c0b9 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  /..  Table *pTab
2c0ba 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2c0bb 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 72 65  * Table being re
2c0bc 6e 61 6d 65 64 20 2a 2f 0d 0a 20 20 63 68 61 72  named */..  char
2c0bd 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
2c0be 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65        /* NULL-te
2c0bf 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e  rminated version
2c0c0 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0d 0a 20   of pName */ .. 
2c0c1 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2c0c2 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61  Parse->db; /* Da
2c0c3 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2c0c4 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 61 62  n */..  int nTab
2c0c5 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
2c0c6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55    /* Number of U
2c0c7 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20  TF-8 characters 
2c0c8 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0d 0a  in zTabName */..
2c0c9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2c0ca 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f  abName;     /* O
2c0cb 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20  riginal name of 
2c0cc 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  the table */..  
2c0cd 56 64 62 65 20 2a 76 3b 0d 0a 23 69 66 6e 64 65  Vdbe *v;..#ifnde
2c0ce 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2c0cf 49 47 47 45 52 0d 0a 20 20 63 68 61 72 20 2a 7a  IGGER..  char *z
2c0d0 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20  Where = 0;      
2c0d1 20 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75     /* Where clau
2c0d2 73 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d  se to locate tem
2c0d3 70 20 74 72 69 67 67 65 72 73 20 2a 2f 0d 0a 23  p triggers */..#
2c0d4 65 6e 64 69 66 0d 0a 20 20 56 54 61 62 6c 65 20  endif..  VTable 
2c0d5 2a 70 56 54 61 62 20 3d 20 30 3b 20 20 20 20 20  *pVTab = 0;     
2c0d6 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69     /* Non-zero i
2c0d7 66 20 74 68 69 73 20 69 73 20 61 20 76 2d 74 61  f this is a v-ta
2c0d8 62 20 77 69 74 68 20 61 6e 20 78 52 65 6e 61 6d  b with an xRenam
2c0d9 65 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 61  e() */..  int sa
2c0da 76 65 64 44 62 46 6c 61 67 73 3b 20 20 20 20 20  vedDbFlags;     
2c0db 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2c0dc 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20  ue of db->flags 
2c0dd 2a 2f 0d 0a 0d 0a 20 20 73 61 76 65 64 44 62 46  */....  savedDbF
2c0de 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73  lags = db->flags
2c0df 3b 20 20 0d 0a 20 20 69 66 28 20 4e 45 56 45 52  ;  ..  if( NEVER
2c0e0 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  (db->mallocFaile
2c0e1 64 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72  d) ) goto exit_r
2c0e2 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0d 0a 20 20  ename_table;..  
2c0e3 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53  assert( pSrc->nS
2c0e4 72 63 3d 3d 31 20 29 3b 0d 0a 20 20 61 73 73 65  rc==1 );..  asse
2c0e5 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
2c0e6 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2c0e7 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0d 0a  pParse->db) );..
2c0e8 0d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ..  pTab = sqlit
2c0e9 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
2c0ea 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61  arse, 0, pSrc->a
2c0eb 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d  [0].zName, pSrc-
2c0ec 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
2c0ed 3b 0d 0a 20 20 69 66 28 20 21 70 54 61 62 20 29  ;..  if( !pTab )
2c0ee 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
2c0ef 65 5f 74 61 62 6c 65 3b 0d 0a 20 20 69 44 62 20  e_table;..  iDb 
2c0f0 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2c0f1 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2c0f2 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2c0f3 29 3b 0d 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  );..  zDb = db->
2c0f4 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d  aDb[iDb].zName;.
2c0f5 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
2c0f6 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69  SQLITE_PreferBui
2c0f7 6c 74 69 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 65  ltin;....  /* Ge
2c0f8 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61  t a NULL termina
2c0f9 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  ted version of t
2c0fa 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
2c0fb 65 2e 20 2a 2f 0d 0a 20 20 7a 4e 61 6d 65 20 3d  e. */..  zName =
2c0fc 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
2c0fd 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
2c0fe 3b 0d 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  ;..  if( !zName 
2c0ff 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61  ) goto exit_rena
2c100 6d 65 5f 74 61 62 6c 65 3b 0d 0a 0d 0a 20 20 2f  me_table;....  /
2c101 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 20 74  * Check that a t
2c102 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 6e 61  able or index na
2c103 6d 65 64 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73  med 'zName' does
2c104 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
2c105 73 74 0d 0a 20 20 2a 2a 20 69 6e 20 64 61 74 61  st..  ** in data
2c106 62 61 73 65 20 69 44 62 2e 20 49 66 20 73 6f 2c  base iDb. If so,
2c107 20 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f   this is an erro
2c108 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  r...  */..  if( 
2c109 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2c10a 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29  (db, zName, zDb)
2c10b 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64 49   || sqlite3FindI
2c10c 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ndex(db, zName, 
2c10d 7a 44 62 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  zDb) ){..    sql
2c10e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c10f 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20 20 22  rse, ..        "
2c110 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
2c111 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f   another table o
2c112 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69  r index with thi
2c113 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61  s name: %s", zNa
2c114 6d 65 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65  me);..    goto e
2c115 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
2c116 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d  ;..  }....  /* M
2c117 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e  ake sure it is n
2c118 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c  ot a system tabl
2c119 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c  e being altered,
2c11a 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e   or a reserved n
2c11b 61 6d 65 0d 0a 20 20 2a 2a 20 74 68 61 74 20 74  ame..  ** that t
2c11c 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e  he table is bein
2c11d 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0d 0a 20  g renamed to... 
2c11e 20 2a 2f 0d 0a 20 20 69 66 28 20 53 51 4c 49 54   */..  if( SQLIT
2c11f 45 5f 4f 4b 21 3d 69 73 53 79 73 74 65 6d 54 61  E_OK!=isSystemTa
2c120 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
2c121 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20  ->zName) ){..   
2c122 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
2c123 65 5f 74 61 62 6c 65 3b 0d 0a 20 20 7d 0d 0a 20  e_table;..  }.. 
2c124 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2c125 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
2c126 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
2c127 4e 61 6d 65 29 20 29 7b 20 67 6f 74 6f 0d 0a 20  Name) ){ goto.. 
2c128 20 20 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74     exit_rename_t
2c129 61 62 6c 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 69  able;..  }....#i
2c12a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c12b 54 5f 56 49 45 57 0d 0a 20 20 69 66 28 20 70 54  T_VIEW..  if( pT
2c12c 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a  ab->pSelect ){..
2c12d 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2c12e 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
2c12f 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  w %s may not be 
2c130 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e  altered", pTab->
2c131 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67 6f 74  zName);..    got
2c132 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2c133 62 6c 65 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  ble;..  }..#endi
2c134 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  f....#ifndef SQL
2c135 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2c136 5a 41 54 49 4f 4e 0d 0a 20 20 2f 2a 20 49 6e 76  ZATION..  /* Inv
2c137 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a  oke the authoriz
2c138 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20  ation callback. 
2c139 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
2c13a 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2c13b 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f  e, SQLITE_ALTER_
2c13c 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62  TABLE, zDb, pTab
2c13d 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0d 0a  ->zName, 0) ){..
2c13e 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
2c13f 6e 61 6d 65 5f 74 61 62 6c 65 3b 0d 0a 20 20 7d  name_table;..  }
2c140 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e  ..#endif....#ifn
2c141 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c142 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20  VIRTUALTABLE..  
2c143 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47  if( sqlite3ViewG
2c144 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
2c145 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0d 0a  arse, pTab) ){..
2c146 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
2c147 6e 61 6d 65 5f 74 61 62 6c 65 3b 0d 0a 20 20 7d  name_table;..  }
2c148 0d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
2c149 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20 20 20 20  l(pTab) ){..    
2c14a 70 56 54 61 62 20 3d 20 73 71 6c 69 74 65 33 47  pVTab = sqlite3G
2c14b 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
2c14c 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 56 54  b);..    if( pVT
2c14d 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  ab->pVtab->pModu
2c14e 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30 20 29  le->xRename==0 )
2c14f 7b 0d 0a 20 20 20 20 20 20 70 56 54 61 62 20 3d  {..      pVTab =
2c150 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   0;..    }..  }.
2c151 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20  .#endif....  /* 
2c152 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
2c153 69 6f 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65  ion and code the
2c154 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f   VerifyCookie fo
2c155 72 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  r database iDb. 
2c156 0d 0a 20 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69  ..  ** Then modi
2c157 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  fy the schema co
2c158 6f 6b 69 65 20 28 73 69 6e 63 65 20 74 68 65 20  okie (since the 
2c159 41 4c 54 45 52 20 54 41 42 4c 45 20 6d 6f 64 69  ALTER TABLE modi
2c15a 66 69 65 73 20 74 68 65 0d 0a 20 20 2a 2a 20 73  fies the..  ** s
2c15b 63 68 65 6d 61 29 2e 20 4f 70 65 6e 20 61 20 73  chema). Open a s
2c15c 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
2c15d 74 69 6f 6e 20 69 66 20 74 68 65 20 74 61 62 6c  tion if the tabl
2c15e 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 0d 0a  e is a virtual..
2c15f 20 20 2a 2a 20 74 61 62 6c 65 2e 0d 0a 20 20 2a    ** table...  *
2c160 2f 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  /..  v = sqlite3
2c161 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2c162 0d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0d  ..  if( v==0 ){.
2c163 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72  .    goto exit_r
2c164 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0d 0a 20 20  ename_table;..  
2c165 7d 0d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  }..  sqlite3Begi
2c166 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
2c167 70 50 61 72 73 65 2c 20 70 56 54 61 62 21 3d 30  pParse, pVTab!=0
2c168 2c 20 69 44 62 29 3b 0d 0a 20 20 73 71 6c 69 74  , iDb);..  sqlit
2c169 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70  e3ChangeCookie(p
2c16a 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a 0d 0a  Parse, iDb);....
2c16b 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
2c16c 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  a virtual table,
2c16d 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65 6e   invoke the xRen
2c16e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ame() function i
2c16f 66 0d 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64  f..  ** one is d
2c170 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e  efined. The xRen
2c171 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77  ame() callback w
2c172 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e  ill modify the n
2c173 61 6d 65 73 0d 0a 20 20 2a 2a 20 6f 66 20 61 6e  ames..  ** of an
2c174 79 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64  y resources used
2c175 20 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20   by the v-table 
2c176 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28  implementation (
2c177 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0d  including other.
2c178 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62  .  ** SQLite tab
2c179 6c 65 73 29 20 74 68 61 74 20 61 72 65 20 69 64  les) that are id
2c17a 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
2c17b 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74  name of the virt
2c17c 75 61 6c 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f  ual table...  */
2c17d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2c17e 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2c17f 4c 45 0d 0a 20 20 69 66 28 20 70 56 54 61 62 20  LE..  if( pVTab 
2c180 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 20 3d 20  ){..    int i = 
2c181 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d  ++pParse->nMem;.
2c182 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c183 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
2c184 69 6e 67 38 2c 20 30 2c 20 69 2c 20 30 2c 20 7a  ing8, 0, i, 0, z
2c185 4e 61 6d 65 2c 20 30 29 3b 0d 0a 20 20 20 20 73  Name, 0);..    s
2c186 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2c187 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d 65 2c 20  (v, OP_VRename, 
2c188 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 74 20 63  i, 0, 0,(const c
2c189 68 61 72 2a 29 70 56 54 61 62 2c 20 50 34 5f 56  har*)pVTab, P4_V
2c18a 54 41 42 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  TAB);..    sqlit
2c18b 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73  e3MayAbort(pPars
2c18c 65 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  e);..  }..#endif
2c18d 0d 0a 0d 0a 20 20 2f 2a 20 66 69 67 75 72 65 20  ....  /* figure 
2c18e 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46  out how many UTF
2c18f 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 61 72  -8 characters ar
2c190 65 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0d 0a 20  e in zName */.. 
2c191 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62   zTabName = pTab
2c192 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 6e 54 61 62  ->zName;..  nTab
2c193 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 55 74  Name = sqlite3Ut
2c194 66 38 43 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61  f8CharLen(zTabNa
2c195 6d 65 2c 20 2d 31 29 3b 0d 0a 0d 0a 23 69 66 20  me, -1);....#if 
2c196 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2c197 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
2c198 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2c199 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2c19a 52 29 0d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c  R)..  if( db->fl
2c19b 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69  ags&SQLITE_Forei
2c19c 67 6e 4b 65 79 73 20 29 7b 0d 0a 20 20 20 20 2f  gnKeys ){..    /
2c19d 2a 20 49 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79  * If foreign-key
2c19e 20 73 75 70 70 6f 72 74 20 69 73 20 65 6e 61 62   support is enab
2c19f 6c 65 64 2c 20 72 65 77 72 69 74 65 20 74 68 65  led, rewrite the
2c1a0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0d 0a   CREATE TABLE ..
2c1a1 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
2c1a2 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  s corresponding 
2c1a3 74 6f 20 61 6c 6c 20 63 68 69 6c 64 20 74 61 62  to all child tab
2c1a4 6c 65 73 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  les of foreign k
2c1a5 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0d 0a  ey constraints..
2c1a6 20 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 63 68      ** for which
2c1a7 20 74 68 65 20 72 65 6e 61 6d 65 64 20 74 61 62   the renamed tab
2c1a8 6c 65 20 69 73 20 74 68 65 20 70 61 72 65 6e 74  le is the parent
2c1a9 20 74 61 62 6c 65 2e 20 20 2a 2f 0d 0a 20 20 20   table.  */..   
2c1aa 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65   if( (zWhere=whe
2c1ab 72 65 46 6f 72 65 69 67 6e 4b 65 79 73 28 70 50  reForeignKeys(pP
2c1ac 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20  arse, pTab))!=0 
2c1ad 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
2c1ae 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
2c1af 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  rse, ..         
2c1b0 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e   "UPDATE \"%w\".
2c1b1 25 73 20 53 45 54 20 22 0d 0a 20 20 20 20 20 20  %s SET "..      
2c1b2 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
2c1b3 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72  qlite_rename_par
2c1b4 65 6e 74 28 73 71 6c 2c 20 25 51 2c 20 25 51 29  ent(sql, %Q, %Q)
2c1b5 20 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   "..            
2c1b6 20 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a    "WHERE %s;", z
2c1b7 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  Db, SCHEMA_TABLE
2c1b8 28 69 44 62 29 2c 20 7a 54 61 62 4e 61 6d 65 2c  (iDb), zTabName,
2c1b9 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b   zName, zWhere);
2c1ba 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
2c1bb 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65  bFree(db, zWhere
2c1bc 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
2c1bd 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 4d  #endif....  /* M
2c1be 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65  odify the sqlite
2c1bf 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f  _master table to
2c1c0 20 75 73 65 20 74 68 65 20 6e 65 77 20 74 61 62   use the new tab
2c1c1 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0d 0a 20 20 73  le name. */..  s
2c1c2 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
2c1c3 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20 20 20  e(pParse,..     
2c1c4 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53   "UPDATE %Q.%s S
2c1c5 45 54 20 22 0d 0a 23 69 66 64 65 66 20 53 51 4c  ET "..#ifdef SQL
2c1c6 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2c1c7 0d 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c  ..          "sql
2c1c8 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65   = sqlite_rename
2c1c9 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 2c  _table(sql, %Q),
2c1ca 20 22 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 20   "..#else..     
2c1cb 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45       "sql = CASE
2c1cc 20 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   "..            
2c1cd 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72  "WHEN type = 'tr
2c1ce 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69  igger' THEN sqli
2c1cf 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65  te_rename_trigge
2c1d0 72 28 73 71 6c 2c 20 25 51 29 22 0d 0a 20 20 20  r(sql, %Q)"..   
2c1d1 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73           "ELSE s
2c1d2 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
2c1d3 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c  le(sql, %Q) END,
2c1d4 20 22 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   "..#endif..    
2c1d5 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20        "tbl_name 
2c1d6 3d 20 25 51 2c 20 22 0d 0a 20 20 20 20 20 20 20  = %Q, "..       
2c1d7 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 20     "name = CASE 
2c1d8 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  "..            "
2c1d9 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65  WHEN type='table
2c1da 27 20 54 48 45 4e 20 25 51 20 22 0d 0a 20 20 20  ' THEN %Q "..   
2c1db 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e           "WHEN n
2c1dc 61 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65  ame LIKE 'sqlite
2c1dd 5f 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e  _autoindex%%' AN
2c1de 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54  D type='index' T
2c1df 48 45 4e 20 22 0d 0a 20 20 20 20 20 20 20 20 20  HEN "..         
2c1e0 20 20 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74      "'sqlite_aut
2c1e1 6f 69 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c  oindex_' || %Q |
2c1e2 7c 20 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64  | substr(name,%d
2c1e3 2b 31 38 29 20 22 0d 0a 20 20 20 20 20 20 20 20  +18) "..        
2c1e4 20 20 20 20 22 45 4c 53 45 20 6e 61 6d 65 20 45      "ELSE name E
2c1e5 4e 44 20 22 0d 0a 20 20 20 20 20 20 22 57 48 45  ND "..      "WHE
2c1e6 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 41  RE tbl_name=%Q A
2c1e7 4e 44 20 22 0d 0a 20 20 20 20 20 20 20 20 20 20  ND "..          
2c1e8 22 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20 4f  "(type='table' O
2c1e9 52 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 4f  R type='index' O
2c1ea 52 20 74 79 70 65 3d 27 74 72 69 67 67 65 72 27  R type='trigger'
2c1eb 29 3b 22 2c 20 0d 0a 20 20 20 20 20 20 7a 44 62  );", ..      zDb
2c1ec 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2c1ed 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  Db), zName, zNam
2c1ee 65 2c 20 7a 4e 61 6d 65 2c 20 0d 0a 23 69 66 6e  e, zName, ..#ifn
2c1ef 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c1f0 54 52 49 47 47 45 52 0d 0a 20 20 20 20 20 20 7a  TRIGGER..      z
2c1f1 4e 61 6d 65 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20  Name,..#endif.. 
2c1f2 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62       zName, nTab
2c1f3 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0d 0a  Name, zTabName..
2c1f4 20 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20    );....#ifndef 
2c1f5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2c1f6 49 4e 43 52 45 4d 45 4e 54 0d 0a 20 20 2f 2a 20  INCREMENT..  /* 
2c1f7 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  If the sqlite_se
2c1f8 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69  quence table exi
2c1f9 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74 61  sts in this data
2c1fa 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74  base, then updat
2c1fb 65 20 0d 0a 20 20 2a 2a 20 69 74 20 77 69 74 68  e ..  ** it with
2c1fc 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
2c1fd 61 6d 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ame...  */..  if
2c1fe 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  ( sqlite3FindTab
2c1ff 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73  le(db, "sqlite_s
2c200 65 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29  equence", zDb) )
2c201 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  {..    sqlite3Ne
2c202 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
2c203 2c 0d 0a 20 20 20 20 20 20 20 20 22 55 50 44 41  ,..        "UPDA
2c204 54 45 20 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65  TE \"%w\".sqlite
2c205 5f 73 65 71 75 65 6e 63 65 20 73 65 74 20 6e 61  _sequence set na
2c206 6d 65 20 3d 20 25 51 20 57 48 45 52 45 20 6e 61  me = %Q WHERE na
2c207 6d 65 20 3d 20 25 51 22 2c 0d 0a 20 20 20 20 20  me = %Q",..     
2c208 20 20 20 7a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70     zDb, zName, p
2c209 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20  Tab->zName);..  
2c20a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66  }..#endif....#if
2c20b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c20c 5f 54 52 49 47 47 45 52 0d 0a 20 20 2f 2a 20 49  _TRIGGER..  /* I
2c20d 66 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50  f there are TEMP
2c20e 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
2c20f 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20  s table, modify 
2c210 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  the sqlite_temp_
2c211 6d 61 73 74 65 72 0d 0a 20 20 2a 2a 20 74 61 62  master..  ** tab
2c212 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69  le. Don't do thi
2c213 73 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  s if the table b
2c214 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20  eing ALTERed is 
2c215 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69  itself located i
2c216 6e 0d 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70  n..  ** the temp
2c217 20 64 61 74 61 62 61 73 65 2e 0d 0a 20 20 2a 2f   database...  */
2c218 0d 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d  ..  if( (zWhere=
2c219 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72  whereTempTrigger
2c21a 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29  s(pParse, pTab))
2c21b 21 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  !=0 ){..    sqli
2c21c 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
2c21d 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20  Parse, ..       
2c21e 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f   "UPDATE sqlite_
2c21f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20  temp_master SET 
2c220 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  "..            "
2c221 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
2c222 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c  ame_trigger(sql,
2c223 20 25 51 29 2c 20 22 0d 0a 20 20 20 20 20 20 20   %Q), "..       
2c224 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d       "tbl_name =
2c225 20 25 51 20 22 0d 0a 20 20 20 20 20 20 20 20 20   %Q "..         
2c226 20 20 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20     "WHERE %s;", 
2c227 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57  zName, zName, zW
2c228 68 65 72 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69  here);..    sqli
2c229 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57  te3DbFree(db, zW
2c22a 68 65 72 65 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  here);..  }..#en
2c22b 64 69 66 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69  dif....#if !defi
2c22c 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2c22d 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20  FOREIGN_KEY) && 
2c22e 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2c22f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0d 0a 20  OMIT_TRIGGER).. 
2c230 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53   if( db->flags&S
2c231 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
2c232 73 20 29 7b 0d 0a 20 20 20 20 46 4b 65 79 20 2a  s ){..    FKey *
2c233 70 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d 73 71  p;..    for(p=sq
2c234 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65  lite3FkReference
2c235 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d  s(pTab); p; p=p-
2c236 3e 70 4e 65 78 74 54 6f 29 7b 0d 0a 20 20 20 20  >pNextTo){..    
2c237 20 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 20 3d    Table *pFrom =
2c238 20 70 2d 3e 70 46 72 6f 6d 3b 0d 0a 20 20 20 20   p->pFrom;..    
2c239 20 20 69 66 28 20 70 46 72 6f 6d 21 3d 70 54 61    if( pFrom!=pTa
2c23a 62 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65  b ){..        re
2c23b 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28  loadTableSchema(
2c23c 70 50 61 72 73 65 2c 20 70 2d 3e 70 46 72 6f 6d  pParse, p->pFrom
2c23d 2c 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b  , pFrom->zName);
2c23e 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
2c23f 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
2c240 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72  .  /* Drop and r
2c241 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e  eload the intern
2c242 61 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e  al table schema.
2c243 20 2a 2f 0d 0a 20 20 72 65 6c 6f 61 64 54 61 62   */..  reloadTab
2c244 6c 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  leSchema(pParse,
2c245 20 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0d 0a   pTab, zName);..
2c246 0d 0a 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  ..exit_rename_ta
2c247 62 6c 65 3a 0d 0a 20 20 73 71 6c 69 74 65 33 53  ble:..  sqlite3S
2c248 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2c249 20 70 53 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74   pSrc);..  sqlit
2c24a 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
2c24b 6d 65 29 3b 0d 0a 20 20 64 62 2d 3e 66 6c 61 67  me);..  db->flag
2c24c 73 20 3d 20 73 61 76 65 64 44 62 46 6c 61 67 73  s = savedDbFlags
2c24d 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ;..}....../*..**
2c24e 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2c24f 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2c250 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62  file format numb
2c251 65 72 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d  er is at least m
2c252 69 6e 46 6f 72 6d 61 74 2e 0d 0a 2a 2a 20 54 68  inFormat...** Th
2c253 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  e generated code
2c254 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74   will increase t
2c255 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e  he file format n
2c256 75 6d 62 65 72 20 69 66 20 6e 65 63 65 73 73 61  umber if necessa
2c257 72 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ry...*/..SQLITE_
2c258 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2c259 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46  ite3MinimumFileF
2c25a 6f 72 6d 61 74 28 50 61 72 73 65 20 2a 70 50 61  ormat(Parse *pPa
2c25b 72 73 65 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e  rse, int iDb, in
2c25c 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0d 0a 20  t minFormat){.. 
2c25d 20 56 64 62 65 20 2a 76 3b 0d 0a 20 20 76 20 3d   Vdbe *v;..  v =
2c25e 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2c25f 70 50 61 72 73 65 29 3b 0d 0a 20 20 2f 2a 20 54  pParse);..  /* T
2c260 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68  he VDBE should h
2c261 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ave been allocat
2c262 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ed before this r
2c263 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2c264 2e 0d 0a 20 20 2a 2a 20 49 66 20 74 68 61 74 20  ...  ** If that 
2c265 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65  allocation faile
2c266 64 2c 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65  d, we would have
2c267 20 71 75 69 74 20 62 65 66 6f 72 65 20 72 65 61   quit before rea
2c268 63 68 69 6e 67 20 74 68 69 73 0d 0a 20 20 2a 2a  ching this..  **
2c269 20 70 6f 69 6e 74 20 2a 2f 0d 0a 20 20 69 66 28   point */..  if(
2c26a 20 41 4c 57 41 59 53 28 76 29 20 29 7b 0d 0a 20   ALWAYS(v) ){.. 
2c26b 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
2c26c 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2c26d 61 72 73 65 29 3b 0d 0a 20 20 20 20 69 6e 74 20  arse);..    int 
2c26e 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r2 = sqlite3GetT
2c26f 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
2c270 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0d 0a 20 20  .    int j1;..  
2c271 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c272 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f  Op3(v, OP_ReadCo
2c273 6f 6b 69 65 2c 20 69 44 62 2c 20 72 31 2c 20 42  okie, iDb, r1, B
2c274 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
2c275 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2c276 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
2c277 69 44 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  iDb);..    sqlit
2c278 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c279 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69 6e 46  OP_Integer, minF
2c27a 6f 72 6d 61 74 2c 20 72 32 29 3b 0d 0a 20 20 20  ormat, r2);..   
2c27b 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2c27c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47 65  eAddOp3(v, OP_Ge
2c27d 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0d 0a 20  , r2, 0, r1);.. 
2c27e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c27f 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
2c280 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45  okie, iDb, BTREE
2c281 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 32  _FILE_FORMAT, r2
2c282 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2c283 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2c284 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  1);..    sqlite3
2c285 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2c286 50 61 72 73 65 2c 20 72 31 29 3b 0d 0a 20 20 20  Parse, r1);..   
2c287 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2c288 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2c289 32 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  2);..  }..}..../
2c28a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
2c28b 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
2c28c 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41  ter an "ALTER TA
2c28d 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61  BLE ... ADD" sta
2c28e 74 65 6d 65 6e 74 0d 0a 2a 2a 20 68 61 73 20 62  tement..** has b
2c28f 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75  een parsed. Argu
2c290 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e  ment pColDef con
2c291 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
2c292 66 20 74 68 65 20 6e 65 77 0d 0a 2a 2a 20 63 6f  f the new..** co
2c293 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lumn definition.
2c294 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 54 61 62  ..**..** The Tab
2c295 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 50 61  le structure pPa
2c296 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77  rse->pNewTable w
2c297 61 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69  as extended to i
2c298 6e 63 6c 75 64 65 0d 0a 2a 2a 20 74 68 65 20 6e  nclude..** the n
2c299 65 77 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67  ew column during
2c29a 20 70 61 72 73 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53   parsing...*/..S
2c29b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2c29c 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
2c29d 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50  inishAddColumn(P
2c29e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
2c29f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66 29 7b 0d 0a  ken *pColDef){..
2c2a0 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 20 20    Table *pNew;  
2c2a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2c2a2 6f 70 79 20 6f 66 20 70 50 61 72 73 65 2d 3e 70  opy of pParse->p
2c2a3 4e 65 77 54 61 62 6c 65 20 2a 2f 0d 0a 20 20 54  NewTable */..  T
2c2a4 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
2c2a5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2c2a6 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20  e being altered 
2c2a7 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  */..  int iDb;  
2c2a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2a9 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62  /* Database numb
2c2aa 65 72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  er */..  const c
2c2ab 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
2c2ac 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
2c2ad 61 6d 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ame */..  const 
2c2ae 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20  char *zTab;     
2c2af 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
2c2b0 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 43  e */..  char *zC
2c2b1 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
2c2b2 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e    /* Null-termin
2c2b3 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69  ated column defi
2c2b4 6e 69 74 69 6f 6e 20 2a 2f 0d 0a 20 20 43 6f 6c  nition */..  Col
2c2b5 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20  umn *pCol;      
2c2b6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
2c2b7 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 45  w column */..  E
2c2b8 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20  xpr *pDflt;     
2c2b9 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2c2ba 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
2c2bb 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0d  e new column */.
2c2bc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2c2bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c2be 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
2c2bf 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0d 0a 0d 0a 20  nection; */.... 
2c2c0 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2c2c1 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
2c2c2 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
2c2c3 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
2c2c4 72 6e 3b 0d 0a 20 20 70 4e 65 77 20 3d 20 70 50  rn;..  pNew = pP
2c2c5 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
2c2c6 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  ..  assert( pNew
2c2c7 20 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   );....  assert(
2c2c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
2c2c9 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29  dsAllMutexes(db)
2c2ca 20 29 3b 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c   );..  iDb = sql
2c2cb 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2c2cc 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 63 68  x(db, pNew->pSch
2c2cd 65 6d 61 29 3b 0d 0a 20 20 7a 44 62 20 3d 20 64  ema);..  zDb = d
2c2ce 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2c2cf 65 3b 0d 0a 20 20 7a 54 61 62 20 3d 20 26 70 4e  e;..  zTab = &pN
2c2d0 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b 20 20  ew->zName[16];  
2c2d1 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73 71 6c  /* Skip the "sql
2c2d2 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70  ite_altertab_" p
2c2d3 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e 61 6d  refix on the nam
2c2d4 65 20 2a 2f 0d 0a 20 20 70 43 6f 6c 20 3d 20 26  e */..  pCol = &
2c2d5 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d  pNew->aCol[pNew-
2c2d6 3e 6e 43 6f 6c 2d 31 5d 3b 0d 0a 20 20 70 44 66  >nCol-1];..  pDf
2c2d7 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  lt = pCol->pDflt
2c2d8 3b 0d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  ;..  pTab = sqli
2c2d9 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
2c2da 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0d 0a 20 20   zTab, zDb);..  
2c2db 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b 0d  assert( pTab );.
2c2dc 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
2c2dd 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
2c2de 54 49 4f 4e 0d 0a 20 20 2f 2a 20 49 6e 76 6f 6b  TION..  /* Invok
2c2df 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  e the authorizat
2c2e0 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ion callback. */
2c2e1 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ..  if( sqlite3A
2c2e2 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2c2e3 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41   SQLITE_ALTER_TA
2c2e4 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e  BLE, zDb, pTab->
2c2e5 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0d 0a 20 20  zName, 0) ){..  
2c2e6 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
2c2e7 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 49  #endif....  /* I
2c2e8 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  f the default va
2c2e9 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  lue for the new 
2c2ea 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69  column was speci
2c2eb 66 69 65 64 20 77 69 74 68 20 61 20 0d 0a 20 20  fied with a ..  
2c2ec 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c  ** literal NULL,
2c2ed 20 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20   then set pDflt 
2c2ee 74 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c  to 0. This simpl
2c2ef 69 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0d 0a  ifies checking..
2c2f0 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20    ** for an SQL 
2c2f1 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c  NULL default bel
2c2f2 6f 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ow...  */..  if(
2c2f3 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74 2d   pDflt && pDflt-
2c2f4 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0d  >op==TK_NULL ){.
2c2f5 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0d  .    pDflt = 0;.
2c2f6 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65  .  }....  /* Che
2c2f7 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20  ck that the new 
2c2f8 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70  column is not sp
2c2f9 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41  ecified as PRIMA
2c2fa 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
2c2fb 2e 0d 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65  ...  ** If there
2c2fc 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63   is a NOT NULL c
2c2fd 6f 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20  onstraint, then 
2c2fe 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
2c2ff 65 20 66 6f 72 20 74 68 65 0d 0a 20 20 2a 2a 20  e for the..  ** 
2c300 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20  column must not 
2c301 62 65 20 4e 55 4c 4c 2e 0d 0a 20 20 2a 2f 0d 0a  be NULL...  */..
2c302 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 73 50 72    if( pCol->isPr
2c303 69 6d 4b 65 79 20 29 7b 0d 0a 20 20 20 20 73 71  imKey ){..    sq
2c304 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2c305 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64  arse, "Cannot ad
2c306 64 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  d a PRIMARY KEY 
2c307 63 6f 6c 75 6d 6e 22 29 3b 0d 0a 20 20 20 20 72  column");..    r
2c308 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69  eturn;..  }..  i
2c309 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20  f( pNew->pIndex 
2c30a 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ){..    sqlite3E
2c30b 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2c30c 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e  "Cannot add a UN
2c30d 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0d 0a  IQUE column");..
2c30e 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
2c30f 0d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  ..  if( (db->fla
2c310 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
2c311 6e 4b 65 79 73 29 20 26 26 20 70 4e 65 77 2d 3e  nKeys) && pNew->
2c312 70 46 4b 65 79 20 26 26 20 70 44 66 6c 74 20 29  pFKey && pDflt )
2c313 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  {..    sqlite3Er
2c314 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0d  rorMsg(pParse, .
2c315 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74  .        "Cannot
2c316 20 61 64 64 20 61 20 52 45 46 45 52 45 4e 43 45   add a REFERENCE
2c317 53 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f  S column with no
2c318 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76  n-NULL default v
2c319 61 6c 75 65 22 29 3b 0d 0a 20 20 20 20 72 65 74  alue");..    ret
2c31a 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  urn;..  }..  if(
2c31b 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26   pCol->notNull &
2c31c 26 20 21 70 44 66 6c 74 20 29 7b 0d 0a 20 20 20  & !pDflt ){..   
2c31d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2c31e 28 70 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20  (pParse, ..     
2c31f 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61     "Cannot add a
2c320 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d 6e   NOT NULL column
2c321 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 76 61   with default va
2c322 6c 75 65 20 4e 55 4c 4c 22 29 3b 0d 0a 20 20 20  lue NULL");..   
2c323 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d   return;..  }...
2c324 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65  .  /* Ensure the
2c325 20 64 65 66 61 75 6c 74 20 65 78 70 72 65 73 73   default express
2c326 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68 69 6e 67  ion is something
2c327 20 74 68 61 74 20 73 71 6c 69 74 65 33 56 61 6c   that sqlite3Val
2c328 75 65 46 72 6f 6d 45 78 70 72 28 29 0d 0a 20 20  ueFromExpr()..  
2c329 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20 28 69  ** can handle (i
2c32a 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e 54 5f  .e. not CURRENT_
2c32b 54 49 4d 45 20 65 74 63 2e 29 0d 0a 20 20 2a 2f  TIME etc.)..  */
2c32c 0d 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 7b  ..  if( pDflt ){
2c32d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ..    sqlite3_va
2c32e 6c 75 65 20 2a 70 56 61 6c 3b 0d 0a 20 20 20 20  lue *pVal;..    
2c32f 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65  if( sqlite3Value
2c330 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66  FromExpr(db, pDf
2c331 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  lt, SQLITE_UTF8,
2c332 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
2c333 2c 20 26 70 56 61 6c 29 20 29 7b 0d 0a 20 20 20  , &pVal) ){..   
2c334 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2c335 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  led = 1;..      
2c336 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a  return;..    }..
2c337 20 20 20 20 69 66 28 20 21 70 56 61 6c 20 29 7b      if( !pVal ){
2c338 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2c339 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2c33a 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f  "Cannot add a co
2c33b 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f  lumn with non-co
2c33c 6e 73 74 61 6e 74 20 64 65 66 61 75 6c 74 22 29  nstant default")
2c33d 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ;..      return;
2c33e 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
2c33f 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
2c340 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  al);..  }....  /
2c341 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 43 52 45  * Modify the CRE
2c342 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2c343 65 6e 74 2e 20 2a 2f 0d 0a 20 20 7a 43 6f 6c 20  ent. */..  zCol 
2c344 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
2c345 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 43  up(db, (char*)pC
2c346 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65  olDef->z, pColDe
2c347 66 2d 3e 6e 29 3b 0d 0a 20 20 69 66 28 20 7a 43  f->n);..  if( zC
2c348 6f 6c 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20  ol ){..    char 
2c349 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43  *zEnd = &zCol[pC
2c34a 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0d 0a 20 20  olDef->n-1];..  
2c34b 20 20 69 6e 74 20 73 61 76 65 64 44 62 46 6c 61    int savedDbFla
2c34c 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0d  gs = db->flags;.
2c34d 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64  .    while( zEnd
2c34e 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d  >zCol && (*zEnd=
2c34f 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49  =';' || sqlite3I
2c350 73 73 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29  sspace(*zEnd)) )
2c351 7b 0d 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d  {..      *zEnd--
2c352 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20 7d 0d   = '\0';..    }.
2c353 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
2c354 3d 20 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42  = SQLITE_PreferB
2c355 75 69 6c 74 69 6e 3b 0d 0a 20 20 20 20 73 71 6c  uiltin;..    sql
2c356 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2c357 70 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20  pParse, ..      
2c358 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22    "UPDATE \"%w\"
2c359 2e 25 73 20 53 45 54 20 22 0d 0a 20 20 20 20 20  .%s SET "..     
2c35a 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62 73       "sql = subs
2c35b 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20  tr(sql,1,%d) || 
2c35c 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75  ', ' || %Q || su
2c35d 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0d 0a  bstr(sql,%d) "..
2c35e 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74          "WHERE t
2c35f 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
2c360 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0d 0a  D name = %Q", ..
2c361 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d        zDb, SCHEM
2c362 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e  A_TABLE(iDb), pN
2c363 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74  ew->addColOffset
2c364 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64  , zCol, pNew->ad
2c365 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0d 0a 20  dColOffset+1,.. 
2c366 20 20 20 20 20 7a 54 61 62 0d 0a 20 20 20 20 29       zTab..    )
2c367 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
2c368 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0d  Free(db, zCol);.
2c369 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d  .    db->flags =
2c36a 20 73 61 76 65 64 44 62 46 6c 61 67 73 3b 0d 0a   savedDbFlags;..
2c36b 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74    }....  /* If t
2c36c 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2c36d 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75   of the new colu
2c36e 6d 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  mn is NULL, then
2c36f 20 73 65 74 20 74 68 65 20 66 69 6c 65 0d 0a 20   set the file.. 
2c370 20 2a 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32 2e   ** format to 2.
2c371 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
2c372 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77  value of the new
2c373 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e   column is not N
2c374 55 4c 4c 2c 0d 0a 20 20 2a 2a 20 74 68 65 20 66  ULL,..  ** the f
2c375 69 6c 65 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d  ile format becom
2c376 65 73 20 33 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73  es 3...  */..  s
2c377 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c  qlite3MinimumFil
2c378 65 46 6f 72 6d 61 74 28 70 50 61 72 73 65 2c 20  eFormat(pParse, 
2c379 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 33 20 3a  iDb, pDflt ? 3 :
2c37a 20 32 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6c   2);....  /* Rel
2c37b 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 6f  oad the schema o
2c37c 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 74  f the modified t
2c37d 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 72 65 6c 6f  able. */..  relo
2c37e 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
2c37f 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62  arse, pTab, pTab
2c380 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 7d 0d 0a 0d 0a  ->zName);..}....
2c381 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
2c382 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
2c383 79 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74  y the parser aft
2c384 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d  er the table-nam
2c385 65 20 69 6e 0d 0a 2a 2a 20 61 6e 20 22 41 4c 54  e in..** an "ALT
2c386 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d  ER TABLE <table-
2c387 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65  name> ADD" state
2c388 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20  ment is parsed. 
2c389 41 72 67 75 6d 65 6e 74 20 0d 0a 2a 2a 20 70 53  Argument ..** pS
2c38a 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e  rc is the full-n
2c38b 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2c38c 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0d   being altered..
2c38d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .**..** This rou
2c38e 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61  tine makes a (pa
2c38f 72 74 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74  rtial) copy of t
2c390 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
2c391 72 65 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  re..** for the t
2c392 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
2c393 65 64 20 61 6e 64 20 73 65 74 73 20 50 61 72 73  ed and sets Pars
2c394 65 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f 20 70  e.pNewTable to p
2c395 6f 69 6e 74 0d 0a 2a 2a 20 74 6f 20 69 74 2e 20  oint..** to it. 
2c396 52 6f 75 74 69 6e 65 73 20 63 61 6c 6c 65 64 20  Routines called 
2c397 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 73  by the parser as
2c398 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69   the column defi
2c399 6e 69 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20 70 61  nition..** is pa
2c39a 72 73 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74  rsed (i.e. sqlit
2c39b 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61  e3AddColumn()) a
2c39c 64 64 20 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d  dd the new Colum
2c39d 6e 20 64 61 74 61 20 74 6f 20 0d 0a 2a 2a 20 74  n data to ..** t
2c39e 68 65 20 63 6f 70 79 2e 20 54 68 65 20 63 6f 70  he copy. The cop
2c39f 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73  y of the Table s
2c3a0 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65  tructure is dele
2c3a1 74 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a 65 2e  ted by tokenize.
2c3a2 63 20 0d 0a 2a 2a 20 61 66 74 65 72 20 70 61 72  c ..** after par
2c3a3 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65 64  sing is finished
2c3a4 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 6f 75 74 69 6e  ...**..** Routin
2c3a5 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69  e sqlite3AlterFi
2c3a6 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 20  nishAddColumn() 
2c3a7 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74  will be called t
2c3a8 6f 20 63 6f 6d 70 6c 65 74 65 0d 0a 2a 2a 20 63  o complete..** c
2c3a9 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52  oding the "ALTER
2c3aa 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20   TABLE ... ADD" 
2c3ab 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a  statement...*/..
2c3ac 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c3ad 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
2c3ae 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50  BeginAddColumn(P
2c3af 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
2c3b0 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0d 0a 20  cList *pSrc){.. 
2c3b1 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0d 0a 20   Table *pNew;.. 
2c3b2 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a 20   Table *pTab;.. 
2c3b3 20 56 64 62 65 20 2a 76 3b 0d 0a 20 20 69 6e 74   Vdbe *v;..  int
2c3b4 20 69 44 62 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d   iDb;..  int i;.
2c3b5 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0d 0a  .  int nAlloc;..
2c3b6 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2c3b7 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20  pParse->db;.... 
2c3b8 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20   /* Look up the 
2c3b9 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
2c3ba 72 65 64 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72  red. */..  asser
2c3bb 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
2c3bc 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20 20 61 73  able==0 );..  as
2c3bd 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
2c3be 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
2c3bf 73 28 64 62 29 20 29 3b 0d 0a 20 20 69 66 28 20  s(db) );..  if( 
2c3c0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c3c1 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
2c3c2 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a  in_add_column;..
2c3c3 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2c3c4 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
2c3c5 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30  se, 0, pSrc->a[0
2c3c6 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61  ].zName, pSrc->a
2c3c7 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0d  [0].zDatabase);.
2c3c8 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67  .  if( !pTab ) g
2c3c9 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
2c3ca 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a 0d 0a 23 69  dd_column;....#i
2c3cb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c3cc 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a  T_VIRTUALTABLE..
2c3cd 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2c3ce 70 54 61 62 29 20 29 7b 0d 0a 20 20 20 20 73 71  pTab) ){..    sq
2c3cf 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2c3d0 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74  arse, "virtual t
2c3d1 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65  ables may not be
2c3d2 20 61 6c 74 65 72 65 64 22 29 3b 0d 0a 20 20 20   altered");..   
2c3d3 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
2c3d4 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a 20 20  _add_column;..  
2c3d5 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f  }..#endif....  /
2c3d6 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73  * Make sure this
2c3d7 20 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d   is not an attem
2c3d8 70 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76 69  pt to ALTER a vi
2c3d9 65 77 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54  ew. */..  if( pT
2c3da 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a  ab->pSelect ){..
2c3db 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2c3dc 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e  Msg(pParse, "Can
2c3dd 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e  not add a column
2c3de 20 74 6f 20 61 20 76 69 65 77 22 29 3b 0d 0a 20   to a view");.. 
2c3df 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67     goto exit_beg
2c3e0 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a  in_add_column;..
2c3e1 20 20 7d 0d 0a 20 20 69 66 28 20 53 51 4c 49 54    }..  if( SQLIT
2c3e2 45 5f 4f 4b 21 3d 69 73 53 79 73 74 65 6d 54 61  E_OK!=isSystemTa
2c3e3 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
2c3e4 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20  ->zName) ){..   
2c3e5 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
2c3e6 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a 20 20  _add_column;..  
2c3e7 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  }....  assert( p
2c3e8 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  Tab->addColOffse
2c3e9 74 3e 30 20 29 3b 0d 0a 20 20 69 44 62 20 3d 20  t>0 );..  iDb = 
2c3ea 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2c3eb 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
2c3ec 53 63 68 65 6d 61 29 3b 0d 0a 0d 0a 20 20 2f 2a  Schema);....  /*
2c3ed 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74   Put a copy of t
2c3ee 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20  he Table struct 
2c3ef 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  in Parse.pNewTab
2c3f0 6c 65 20 66 6f 72 20 74 68 65 0d 0a 20 20 2a 2a  le for the..  **
2c3f1 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
2c3f2 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64  n() function and
2c3f3 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69   friends to modi
2c3f4 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0d  fy.  But modify.
2c3f5 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62  .  ** the name b
2c3f6 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71 6c  y adding an "sql
2c3f7 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70  ite_altertab_" p
2c3f8 72 65 66 69 78 2e 20 20 42 79 20 61 64 64 69 6e  refix.  By addin
2c3f9 67 20 74 68 69 73 0d 0a 20 20 2a 2a 20 70 72 65  g this..  ** pre
2c3fa 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74  fix, we insure t
2c3fb 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c  hat the name wil
2c3fc 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  l not collide wi
2c3fd 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0d 0a  th an existing..
2c3fe 20 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75    ** table becau
2c3ff 73 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72  se user table ar
2c400 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  e not allowed to
2c401 20 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74   have the "sqlit
2c402 65 5f 22 0d 0a 20 20 2a 2a 20 70 72 65 66 69 78  e_"..  ** prefix
2c403 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0d   on their name..
2c404 0a 20 20 2a 2f 0d 0a 20 20 70 4e 65 77 20 3d 20  .  */..  pNew = 
2c405 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44  (Table*)sqlite3D
2c406 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2c407 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0d  sizeof(Table));.
2c408 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 67  .  if( !pNew ) g
2c409 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
2c40a 64 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a 20 20 70 50  dd_column;..  pP
2c40b 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2c40c 3d 20 70 4e 65 77 3b 0d 0a 20 20 70 4e 65 77 2d  = pNew;..  pNew-
2c40d 3e 6e 52 65 66 20 3d 20 31 3b 0d 0a 20 20 70 4e  >nRef = 1;..  pN
2c40e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61 62 2d  ew->nCol = pTab-
2c40f 3e 6e 43 6f 6c 3b 0d 0a 20 20 61 73 73 65 72 74  >nCol;..  assert
2c410 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20 29  ( pNew->nCol>0 )
2c411 3b 0d 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28  ;..  nAlloc = ((
2c412 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38  (pNew->nCol-1)/8
2c413 29 2a 38 29 2b 38 3b 0d 0a 20 20 61 73 73 65 72  )*8)+8;..  asser
2c414 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d  t( nAlloc>=pNew-
2c415 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25  >nCol && nAlloc%
2c416 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70  8==0 && nAlloc-p
2c417 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0d 0a  New->nCol<8 );..
2c418 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28    pNew->aCol = (
2c419 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44  Column*)sqlite3D
2c41a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2c41b 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e  sizeof(Column)*n
2c41c 41 6c 6c 6f 63 29 3b 0d 0a 20 20 70 4e 65 77 2d  Alloc);..  pNew-
2c41d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
2c41e 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
2c41f 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22  ite_altertab_%s"
2c420 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d  , pTab->zName);.
2c421 0a 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43  .  if( !pNew->aC
2c422 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61  ol || !pNew->zNa
2c423 6d 65 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d  me ){..    db->m
2c424 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2c425 0d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ..    goto exit_
2c426 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
2c427 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 63 70 79  ;..  }..  memcpy
2c428 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61  (pNew->aCol, pTa
2c429 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28  b->aCol, sizeof(
2c42a 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43  Column)*pNew->nC
2c42b 6f 6c 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b  ol);..  for(i=0;
2c42c 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69   i<pNew->nCol; i
2c42d 2b 2b 29 7b 0d 0a 20 20 20 20 43 6f 6c 75 6d 6e  ++){..    Column
2c42e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e   *pCol = &pNew->
2c42f 61 43 6f 6c 5b 69 5d 3b 0d 0a 20 20 20 20 70 43  aCol[i];..    pC
2c430 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ol->zName = sqli
2c431 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2c432 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  pCol->zName);.. 
2c433 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d     pCol->zColl =
2c434 20 30 3b 0d 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a   0;..    pCol->z
2c435 54 79 70 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70  Type = 0;..    p
2c436 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0d  Col->pDflt = 0;.
2c437 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74  .    pCol->zDflt
2c438 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4e   = 0;..  }..  pN
2c439 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62  ew->pSchema = db
2c43a 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2c43b 6d 61 3b 0d 0a 20 20 70 4e 65 77 2d 3e 61 64 64  ma;..  pNew->add
2c43c 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62  ColOffset = pTab
2c43d 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0d  ->addColOffset;.
2c43e 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
2c43f 31 3b 0d 0a 0d 0a 20 20 2f 2a 20 42 65 67 69 6e  1;....  /* Begin
2c440 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61   a transaction a
2c441 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  nd increment the
2c442 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20   schema cookie. 
2c443 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 42 65   */..  sqlite3Be
2c444 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2c445 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
2c446 29 3b 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  );..  v = sqlite
2c447 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2c448 3b 0d 0a 20 20 69 66 28 20 21 76 20 29 20 67 6f  ;..  if( !v ) go
2c449 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64  to exit_begin_ad
2c44a 64 5f 63 6f 6c 75 6d 6e 3b 0d 0a 20 20 73 71 6c  d_column;..  sql
2c44b 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
2c44c 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a  (pParse, iDb);..
2c44d 0d 0a 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  ..exit_begin_add
2c44e 5f 63 6f 6c 75 6d 6e 3a 0d 0a 20 20 73 71 6c 69  _column:..  sqli
2c44f 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2c450 28 64 62 2c 20 70 53 72 63 29 3b 0d 0a 20 20 72  (db, pSrc);..  r
2c451 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  eturn;..}..#endi
2c452 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c 54  f  /* SQLITE_ALT
2c453 45 52 5f 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 2f  ER_TABLE */..../
2c454 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2c455 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a  nd of alter.c **
2c456 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c457 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c458 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
2c459 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c45a 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79  Begin file analy
2c45b 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
2c45c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c45d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
2c45e 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 35 20 4a 75 6c  ./*..** 2005 Jul
2c45f 79 20 38 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  y 8..**..** The 
2c460 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2c461 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2c462 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2c463 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
2c464 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2c465 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2c466 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
2c467 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2c468 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
2c469 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2c46a 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2c46b 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2c46c 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
2c46d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2c46e 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2c46f 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2c470 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
2c471 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
2c472 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c474 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c475 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20  ***********..** 
2c476 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2c477 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69 61 74  ns code associat
2c478 65 64 20 77 69 74 68 20 74 68 65 20 41 4e 41 4c  ed with the ANAL
2c479 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0d 0a 2a 2a  YZE command...**
2c47a 0d 0a 2a 2a 20 54 68 65 20 41 4e 41 4c 59 5a 45  ..** The ANALYZE
2c47b 20 63 6f 6d 6d 61 6e 64 20 67 61 74 68 65 72 20   command gather 
2c47c 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74  statistics about
2c47d 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
2c47e 74 61 62 6c 65 73 0d 0a 2a 2a 20 61 6e 64 20 69  tables..** and i
2c47f 6e 64 69 63 65 73 2e 20 20 54 68 65 73 65 20 73  ndices.  These s
2c480 74 61 74 69 73 74 69 63 73 20 61 72 65 20 6d 61  tatistics are ma
2c481 64 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  de available to 
2c482 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 6e 65  the query planne
2c483 72 0d 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 69 74  r..** to help it
2c484 20 6d 61 6b 65 20 62 65 74 74 65 72 20 64 65 63   make better dec
2c485 69 73 69 6f 6e 73 20 61 62 6f 75 74 20 68 6f 77  isions about how
2c486 20 74 6f 20 70 65 72 66 6f 72 6d 20 71 75 65 72   to perform quer
2c487 69 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ies...**..** The
2c488 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 73 74 65   following syste
2c489 6d 20 74 61 62 6c 65 73 20 61 72 65 20 6f 72 20  m tables are or 
2c48a 68 61 76 65 20 62 65 65 6e 20 73 75 70 70 6f 72  have been suppor
2c48b 74 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ted:..**..**    
2c48c 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c  CREATE TABLE sql
2c48d 69 74 65 5f 73 74 61 74 31 28 74 62 6c 2c 20 69  ite_stat1(tbl, i
2c48e 64 78 2c 20 73 74 61 74 29 3b 0d 0a 2a 2a 20 20  dx, stat);..**  
2c48f 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73    CREATE TABLE s
2c490 71 6c 69 74 65 5f 73 74 61 74 32 28 74 62 6c 2c  qlite_stat2(tbl,
2c491 20 69 64 78 2c 20 73 61 6d 70 6c 65 6e 6f 2c 20   idx, sampleno, 
2c492 73 61 6d 70 6c 65 29 3b 0d 0a 2a 2a 20 20 20 20  sample);..**    
2c493 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c  CREATE TABLE sql
2c494 69 74 65 5f 73 74 61 74 33 28 74 62 6c 2c 20 69  ite_stat3(tbl, i
2c495 64 78 2c 20 6e 45 71 2c 20 6e 4c 74 2c 20 6e 44  dx, nEq, nLt, nD
2c496 4c 74 2c 20 73 61 6d 70 6c 65 29 3b 0d 0a 2a 2a  Lt, sample);..**
2c497 0d 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20  ..** Additional 
2c498 74 61 62 6c 65 73 20 6d 69 67 68 74 20 62 65 20  tables might be 
2c499 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
2c49a 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
2c49b 74 65 2e 0d 0a 2a 2a 20 54 68 65 20 73 71 6c 69  te...** The sqli
2c49c 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69  te_stat2 table i
2c49d 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72  s not created or
2c49e 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65   used unless the
2c49f 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 0d   SQLite version.
2c4a0 0a 2a 2a 20 69 73 20 62 65 74 77 65 65 6e 20 33  .** is between 3
2c4a1 2e 36 2e 31 38 20 61 6e 64 20 33 2e 37 2e 38 2c  .6.18 and 3.7.8,
2c4a2 20 69 6e 63 6c 75 73 69 76 65 2c 20 61 6e 64 20   inclusive, and 
2c4a3 75 6e 6c 65 73 73 20 53 51 4c 69 74 65 20 69 73  unless SQLite is
2c4a4 20 63 6f 6d 70 69 6c 65 64 0d 0a 2a 2a 20 77 69   compiled..** wi
2c4a5 74 68 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  th SQLITE_ENABLE
2c4a6 5f 53 54 41 54 32 2e 20 20 54 68 65 20 73 71 6c  _STAT2.  The sql
2c4a7 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20  ite_stat2 table 
2c4a8 69 73 20 64 65 70 72 65 63 61 74 65 64 2e 0d 0a  is deprecated...
2c4a9 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  ** The sqlite_st
2c4aa 61 74 32 20 74 61 62 6c 65 20 69 73 20 73 75 70  at2 table is sup
2c4ab 65 72 63 65 64 65 64 20 62 79 20 73 71 6c 69 74  erceded by sqlit
2c4ac 65 5f 73 74 61 74 33 2c 20 77 68 69 63 68 20 69  e_stat3, which i
2c4ad 73 20 6f 6e 6c 79 0d 0a 2a 2a 20 63 72 65 61 74  s only..** creat
2c4ae 65 64 20 61 6e 64 20 75 73 65 64 20 62 79 20 53  ed and used by S
2c4af 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 73 20 33  QLite versions 3
2c4b0 2e 37 2e 39 20 61 6e 64 20 6c 61 74 65 72 20 61  .7.9 and later a
2c4b1 6e 64 20 77 69 74 68 0d 0a 2a 2a 20 53 51 4c 49  nd with..** SQLI
2c4b2 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 20  TE_ENABLE_STAT3 
2c4b3 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 66 75  defined.  The fu
2c4b4 63 6e 74 69 6f 6e 61 6c 69 74 79 20 6f 66 20 73  cntionality of s
2c4b5 71 6c 69 74 65 5f 73 74 61 74 33 0d 0a 2a 2a 20  qlite_stat3..** 
2c4b6 69 73 20 61 20 73 75 70 65 72 73 65 74 20 6f 66  is a superset of
2c4b7 20 73 71 6c 69 74 65 5f 73 74 61 74 32 2e 20 20   sqlite_stat2.  
2c4b8 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 6d 61 74 20  ..**..** Format 
2c4b9 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 31 3a  of sqlite_stat1:
2c4ba 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 69  ..**..** There i
2c4bb 73 20 6e 6f 72 6d 61 6c 6c 79 20 6f 6e 65 20 72  s normally one r
2c4bc 6f 77 20 70 65 72 20 69 6e 64 65 78 2c 20 77 69  ow per index, wi
2c4bd 74 68 20 74 68 65 20 69 6e 64 65 78 20 69 64 65  th the index ide
2c4be 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 0d 0a  ntified by the..
2c4bf 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 69  ** name in the i
2c4c0 64 78 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20  dx column.  The 
2c4c1 74 62 6c 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68  tbl column is th
2c4c2 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
2c4c3 62 6c 65 20 74 6f 0d 0a 2a 2a 20 77 68 69 63 68  ble to..** which
2c4c4 20 74 68 65 20 69 6e 64 65 78 20 62 65 6c 6f 6e   the index belon
2c4c5 67 73 2e 20 20 49 6e 20 65 61 63 68 20 73 75 63  gs.  In each suc
2c4c6 68 20 72 6f 77 2c 20 74 68 65 20 73 74 61 74 20  h row, the stat 
2c4c7 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 0d 0a  column will be..
2c4c8 2a 2a 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 73  ** a string cons
2c4c9 69 73 74 69 6e 67 20 6f 66 20 61 20 6c 69 73 74  isting of a list
2c4ca 20 6f 66 20 69 6e 74 65 67 65 72 73 2e 20 20 54   of integers.  T
2c4cb 68 65 20 66 69 72 73 74 20 69 6e 74 65 67 65 72  he first integer
2c4cc 20 69 6e 20 74 68 69 73 0d 0a 2a 2a 20 6c 69 73   in this..** lis
2c4cd 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
2c4ce 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 69  of rows in the i
2c4cf 6e 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20  ndex and in the 
2c4d0 74 61 62 6c 65 2e 20 20 54 68 65 20 73 65 63 6f  table.  The seco
2c4d1 6e 64 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69  nd..** integer i
2c4d2 73 20 74 68 65 20 61 76 65 72 61 67 65 20 6e 75  s the average nu
2c4d3 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
2c4d4 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 68  the index that h
2c4d5 61 76 65 20 74 68 65 20 73 61 6d 65 0d 0a 2a 2a  ave the same..**
2c4d6 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 66 69   value in the fi
2c4d7 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
2c4d8 65 20 69 6e 64 65 78 2e 20 20 54 68 65 20 74 68  e index.  The th
2c4d9 69 72 64 20 69 6e 74 65 67 65 72 20 69 73 20 74  ird integer is t
2c4da 68 65 20 61 76 65 72 61 67 65 0d 0a 2a 2a 20 6e  he average..** n
2c4db 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
2c4dc 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20   the index that 
2c4dd 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 76 61  have the same va
2c4de 6c 75 65 20 66 6f 72 20 74 68 65 20 66 69 72 73  lue for the firs
2c4df 74 20 74 77 6f 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e  t two..** column
2c4e0 73 2e 20 20 54 68 65 20 4e 2d 74 68 20 69 6e 74  s.  The N-th int
2c4e1 65 67 65 72 20 28 66 6f 72 20 4e 3e 31 29 20 69  eger (for N>1) i
2c4e2 73 20 74 68 65 20 61 76 65 72 61 67 65 20 6e 75  s the average nu
2c4e3 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
2c4e4 0d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 77  ..** the index w
2c4e5 68 69 63 68 20 68 61 76 65 20 74 68 65 20 73 61  hich have the sa
2c4e6 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  me value for the
2c4e7 20 66 69 72 73 74 20 4e 2d 31 20 63 6f 6c 75 6d   first N-1 colum
2c4e8 6e 73 2e 20 20 46 6f 72 0d 0a 2a 2a 20 61 20 4b  ns.  For..** a K
2c4e9 2d 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2c 20 74  -column index, t
2c4ea 68 65 72 65 20 77 69 6c 6c 20 62 65 20 4b 2b 31  here will be K+1
2c4eb 20 69 6e 74 65 67 65 72 73 20 69 6e 20 74 68 65   integers in the
2c4ec 20 73 74 61 74 20 63 6f 6c 75 6d 6e 2e 20 20 49   stat column.  I
2c4ed 66 0d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  f..** the index 
2c4ee 69 73 20 75 6e 69 71 75 65 2c 20 74 68 65 6e 20  is unique, then 
2c4ef 74 68 65 20 6c 61 73 74 20 69 6e 74 65 67 65 72  the last integer
2c4f0 20 77 69 6c 6c 20 62 65 20 31 2e 0d 0a 2a 2a 0d   will be 1...**.
2c4f1 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20  .** The list of 
2c4f2 69 6e 74 65 67 65 72 73 20 69 6e 20 74 68 65 20  integers in the 
2c4f3 73 74 61 74 20 63 6f 6c 75 6d 6e 20 63 61 6e 20  stat column can 
2c4f4 6f 70 74 69 6f 6e 61 6c 6c 79 20 62 65 20 66 6f  optionally be fo
2c4f5 6c 6c 6f 77 65 64 0d 0a 2a 2a 20 62 79 20 74 68  llowed..** by th
2c4f6 65 20 6b 65 79 77 6f 72 64 20 22 75 6e 6f 72 64  e keyword "unord
2c4f7 65 72 65 64 22 2e 20 20 54 68 65 20 22 75 6e 6f  ered".  The "uno
2c4f8 72 64 65 72 65 64 22 20 6b 65 79 77 6f 72 64 2c  rdered" keyword,
2c4f9 20 69 66 20 69 74 20 69 73 20 70 72 65 73 65 6e   if it is presen
2c4fa 74 2c 0d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73  t,..** must be s
2c4fb 65 70 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68  eparated from th
2c4fc 65 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 62  e last integer b
2c4fd 79 20 61 20 73 69 6e 67 6c 65 20 73 70 61 63 65  y a single space
2c4fe 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a 20 22 75  .  If the..** "u
2c4ff 6e 6f 72 64 65 72 65 64 22 20 6b 65 79 77 6f 72  nordered" keywor
2c500 64 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68  d is present, th
2c501 65 6e 20 74 68 65 20 71 75 65 72 79 20 70 6c 61  en the query pla
2c502 6e 6e 65 72 20 61 73 73 75 6d 65 73 20 74 68 61  nner assumes tha
2c503 74 0d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  t..** the index 
2c504 69 73 20 75 6e 6f 72 64 65 72 65 64 20 61 6e 64  is unordered and
2c505 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 74 68   will not use th
2c506 65 20 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61  e index for a ra
2c507 6e 67 65 20 71 75 65 72 79 2e 0d 0a 2a 2a 20 0d  nge query...** .
2c508 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74  .** If the sqlit
2c509 65 5f 73 74 61 74 31 2e 69 64 78 20 63 6f 6c 75  e_stat1.idx colu
2c50a 6d 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  mn is NULL, then
2c50b 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2c50c 31 2e 73 74 61 74 0d 0a 2a 2a 20 63 6f 6c 75 6d  1.stat..** colum
2c50d 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  n contains a sin
2c50e 67 6c 65 20 69 6e 74 65 67 65 72 20 77 68 69 63  gle integer whic
2c50f 68 20 69 73 20 74 68 65 20 28 65 73 74 69 6d 61  h is the (estima
2c510 74 65 64 29 20 6e 75 6d 62 65 72 20 6f 66 0d 0a  ted) number of..
2c511 2a 2a 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74  ** rows in the t
2c512 61 62 6c 65 20 69 64 65 6e 74 69 66 69 65 64 20  able identified 
2c513 62 79 20 73 71 6c 69 74 65 5f 73 74 61 74 31 2e  by sqlite_stat1.
2c514 74 62 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72  tbl...**..** For
2c515 6d 61 74 20 6f 66 20 73 71 6c 69 74 65 5f 73 74  mat of sqlite_st
2c516 61 74 32 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  at2:..**..** The
2c517 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 69 73   sqlite_stat2 is
2c518 20 6f 6e 6c 79 20 63 72 65 61 74 65 64 20 61 6e   only created an
2c519 64 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  d is only used i
2c51a 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  f SQLite is comp
2c51b 69 6c 65 64 0d 0a 2a 2a 20 77 69 74 68 20 53 51  iled..** with SQ
2c51c 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2c51d 32 20 61 6e 64 20 69 66 20 74 68 65 20 53 51 4c  2 and if the SQL
2c51e 69 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  ite version numb
2c51f 65 72 20 69 73 20 62 65 74 77 65 65 6e 0d 0a 2a  er is between..*
2c520 2a 20 33 2e 36 2e 31 38 20 61 6e 64 20 33 2e 37  * 3.6.18 and 3.7
2c521 2e 38 2e 20 20 54 68 65 20 22 73 74 61 74 32 22  .8.  The "stat2"
2c522 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20   table contains 
2c523 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2c524 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 61 62 6f 75 74  mation..** about
2c525 20 74 68 65 20 64 69 73 74 72 69 62 75 74 69 6f   the distributio
2c526 6e 20 6f 66 20 6b 65 79 73 20 77 69 74 68 69 6e  n of keys within
2c527 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20   an index.  The 
2c528 69 6e 64 65 78 20 69 73 20 69 64 65 6e 74 69 66  index is identif
2c529 69 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65 20 22  ied by..** the "
2c52a 69 64 78 22 20 63 6f 6c 75 6d 6e 20 61 6e 64 20  idx" column and 
2c52b 74 68 65 20 22 74 62 6c 22 20 63 6f 6c 75 6d 6e  the "tbl" column
2c52c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
2c52d 74 68 65 20 74 61 62 6c 65 20 74 6f 20 77 68 69  the table to whi
2c52e 63 68 0d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  ch..** the index
2c52f 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 72 65   belongs.  There
2c530 20 61 72 65 20 75 73 75 61 6c 6c 79 20 31 30 20   are usually 10 
2c531 72 6f 77 73 20 69 6e 20 74 68 65 20 73 71 6c 69  rows in the sqli
2c532 74 65 5f 73 74 61 74 32 0d 0a 2a 2a 20 74 61 62  te_stat2..** tab
2c533 6c 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  le for each inde
2c534 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73  x...**..** The s
2c535 71 6c 69 74 65 5f 73 74 61 74 32 20 65 6e 74 72  qlite_stat2 entr
2c536 69 65 73 20 66 6f 72 20 61 6e 20 69 6e 64 65 78  ies for an index
2c537 20 74 68 61 74 20 68 61 76 65 20 73 61 6d 70 6c   that have sampl
2c538 65 6e 6f 20 62 65 74 77 65 65 6e 20 30 20 61 6e  eno between 0 an
2c539 64 20 39 0d 0a 2a 2a 20 69 6e 63 6c 75 73 69 76  d 9..** inclusiv
2c53a 65 20 61 72 65 20 73 61 6d 70 6c 65 73 20 6f 66  e are samples of
2c53b 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6b   the left-most k
2c53c 65 79 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  ey value in the 
2c53d 69 6e 64 65 78 20 74 61 6b 65 6e 20 61 74 0d 0a  index taken at..
2c53e 2a 2a 20 65 76 65 6e 6c 79 20 73 70 61 63 65 64  ** evenly spaced
2c53f 20 70 6f 69 6e 74 73 20 61 6c 6f 6e 67 20 74 68   points along th
2c540 65 20 69 6e 64 65 78 2e 20 20 4c 65 74 20 74 68  e index.  Let th
2c541 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 6d 70  e number of samp
2c542 6c 65 73 20 62 65 20 53 0d 0a 2a 2a 20 28 31 30  les be S..** (10
2c543 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64   in the standard
2c544 20 62 75 69 6c 64 29 20 61 6e 64 20 6c 65 74 20   build) and let 
2c545 43 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20  C be the number 
2c546 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 69  of rows in the i
2c547 6e 64 65 78 2e 0d 0a 2a 2a 20 54 68 65 6e 20 74  ndex...** Then t
2c548 68 65 20 73 61 6d 70 6c 65 64 20 72 6f 77 73 20  he sampled rows 
2c549 61 72 65 20 67 69 76 65 6e 20 62 79 3a 0d 0a 2a  are given by:..*
2c54a 2a 0d 0a 2a 2a 20 20 20 20 20 72 6f 77 6e 75 6d  *..**     rownum
2c54b 62 65 72 20 3d 20 28 69 2a 43 2a 32 20 2b 20 43  ber = (i*C*2 + C
2c54c 29 2f 28 53 2a 32 29 0d 0a 2a 2a 0d 0a 2a 2a 20  )/(S*2)..**..** 
2c54d 46 6f 72 20 69 20 62 65 74 77 65 65 6e 20 30 20  For i between 0 
2c54e 61 6e 64 20 53 2d 31 2e 20 20 43 6f 6e 63 65 70  and S-1.  Concep
2c54f 74 75 61 6c 6c 79 2c 20 74 68 65 20 69 6e 64 65  tually, the inde
2c550 78 20 73 70 61 63 65 20 69 73 20 64 69 76 69 64  x space is divid
2c551 65 64 20 69 6e 74 6f 0d 0a 2a 2a 20 53 20 75 6e  ed into..** S un
2c552 69 66 6f 72 6d 20 62 75 63 6b 65 74 73 20 61 6e  iform buckets an
2c553 64 20 74 68 65 20 73 61 6d 70 6c 65 73 20 61 72  d the samples ar
2c554 65 20 74 68 65 20 6d 69 64 64 6c 65 20 72 6f 77  e the middle row
2c555 20 66 72 6f 6d 20 65 61 63 68 20 62 75 63 6b 65   from each bucke
2c556 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66  t...**..** The f
2c557 6f 72 6d 61 74 20 66 6f 72 20 73 71 6c 69 74 65  ormat for sqlite
2c558 5f 73 74 61 74 32 20 69 73 20 72 65 63 6f 72 64  _stat2 is record
2c559 65 64 20 68 65 72 65 20 66 6f 72 20 6c 65 67 61  ed here for lega
2c55a 63 79 20 72 65 66 65 72 65 6e 63 65 2e 20 20 54  cy reference.  T
2c55b 68 69 73 0d 0a 2a 2a 20 76 65 72 73 69 6f 6e 20  his..** version 
2c55c 6f 66 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  of SQLite does n
2c55d 6f 74 20 73 75 70 70 6f 72 74 20 73 71 6c 69 74  ot support sqlit
2c55e 65 5f 73 74 61 74 32 2e 20 20 49 74 20 6e 65 69  e_stat2.  It nei
2c55f 74 68 65 72 20 72 65 61 64 73 20 6e 6f 72 0d 0a  ther reads nor..
2c560 2a 2a 20 77 72 69 74 65 73 20 74 68 65 20 73 71  ** writes the sq
2c561 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65  lite_stat2 table
2c562 2e 20 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  .  This version 
2c563 6f 66 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 73  of SQLite only s
2c564 75 70 70 6f 72 74 73 0d 0a 2a 2a 20 73 71 6c 69  upports..** sqli
2c565 74 65 5f 73 74 61 74 33 2e 0d 0a 2a 2a 0d 0a 2a  te_stat3...**..*
2c566 2a 20 46 6f 72 6d 61 74 20 66 6f 72 20 73 71 6c  * Format for sql
2c567 69 74 65 5f 73 74 61 74 33 3a 0d 0a 2a 2a 0d 0a  ite_stat3:..**..
2c568 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  ** The sqlite_st
2c569 61 74 33 20 69 73 20 61 6e 20 65 6e 68 61 6e 63  at3 is an enhanc
2c56a 65 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 5f  ement to sqlite_
2c56b 73 74 61 74 32 2e 20 20 41 20 6e 65 77 20 6e 61  stat2.  A new na
2c56c 6d 65 20 69 73 0d 0a 2a 2a 20 75 73 65 64 20 74  me is..** used t
2c56d 6f 20 61 76 6f 69 64 20 63 6f 6d 70 61 74 69 62  o avoid compatib
2c56e 69 6c 69 74 79 20 70 72 6f 62 6c 65 6d 73 2e 20  ility problems. 
2c56f 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f   ..**..** The fo
2c570 72 6d 61 74 20 6f 66 20 74 68 65 20 73 71 6c 69  rmat of the sqli
2c571 74 65 5f 73 74 61 74 33 20 74 61 62 6c 65 20 69  te_stat3 table i
2c572 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65  s similar to the
2c573 20 66 6f 72 6d 61 74 20 6f 66 0d 0a 2a 2a 20 74   format of..** t
2c574 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
2c575 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 61 72  table.  There ar
2c576 65 20 6d 75 6c 74 69 70 6c 65 20 65 6e 74 72 69  e multiple entri
2c577 65 73 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  es for each inde
2c578 78 2e 0d 0a 2a 2a 20 54 68 65 20 69 64 78 20 63  x...** The idx c
2c579 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 65 20  olumn names the 
2c57a 69 6e 64 65 78 20 61 6e 64 20 74 68 65 20 74 62  index and the tb
2c57b 6c 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20  l column is the 
2c57c 74 61 62 6c 65 20 6f 66 20 74 68 65 0d 0a 2a 2a  table of the..**
2c57d 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 20   index.  If the 
2c57e 69 64 78 20 61 6e 64 20 74 62 6c 20 63 6f 6c 75  idx and tbl colu
2c57f 6d 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65  mns are the same
2c580 2c 20 74 68 65 6e 20 74 68 65 20 73 61 6d 70 6c  , then the sampl
2c581 65 20 69 73 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  e is..** of the 
2c582 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
2c583 4b 45 59 2e 20 20 54 68 65 20 73 61 6d 70 6c 65  KEY.  The sample
2c584 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 76 61 6c   column is a val
2c585 75 65 20 74 61 6b 65 6e 20 66 72 6f 6d 0d 0a 2a  ue taken from..*
2c586 2a 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  * the left-most 
2c587 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e  column of the in
2c588 64 65 78 2e 20 20 54 68 65 20 6e 45 71 20 63 6f  dex.  The nEq co
2c589 6c 75 6d 6e 20 69 73 20 74 68 65 20 61 70 70 72  lumn is the appr
2c58a 6f 78 69 6d 61 74 65 0d 0a 2a 2a 20 6e 75 6d 62  oximate..** numb
2c58b 65 72 20 6f 66 20 65 6e 74 69 72 65 73 20 69 6e  er of entires in
2c58c 20 74 68 65 20 69 6e 64 65 78 20 77 68 6f 73 65   the index whose
2c58d 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
2c58e 6e 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65  n exactly matche
2c58f 73 0d 0a 2a 2a 20 74 68 65 20 73 61 6d 70 6c 65  s..** the sample
2c590 2e 20 20 6e 4c 74 20 69 73 20 74 68 65 20 61 70  .  nLt is the ap
2c591 70 72 6f 78 69 6d 61 74 65 20 6e 75 6d 62 65 72  proximate number
2c592 20 6f 66 20 65 6e 74 69 72 65 73 20 77 68 6f 73   of entires whos
2c593 65 20 6c 65 66 74 2d 6d 6f 73 74 0d 0a 2a 2a 20  e left-most..** 
2c594 63 6f 6c 75 6d 6e 20 69 73 20 6c 65 73 73 20 74  column is less t
2c595 68 61 6e 20 74 68 65 20 73 61 6d 70 6c 65 2e 20  han the sample. 
2c596 20 54 68 65 20 6e 44 4c 74 20 63 6f 6c 75 6d 6e   The nDLt column
2c597 20 69 73 20 74 68 65 20 61 70 70 72 6f 78 69 6d   is the approxim
2c598 61 74 65 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f  ate..** number o
2c599 66 20 64 69 73 74 69 6e 63 74 20 6c 65 66 74 2d  f distinct left-
2c59a 6d 6f 73 74 20 65 6e 74 72 69 65 73 20 69 6e 20  most entries in 
2c59b 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 61  the index that a
2c59c 72 65 20 6c 65 73 73 20 74 68 61 6e 0d 0a 2a 2a  re less than..**
2c59d 20 74 68 65 20 73 61 6d 70 6c 65 2e 0d 0a 2a 2a   the sample...**
2c59e 0d 0a 2a 2a 20 46 75 74 75 72 65 20 76 65 72 73  ..** Future vers
2c59f 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
2c5a0 69 67 68 74 20 63 68 61 6e 67 65 20 74 6f 20 73  ight change to s
2c5a1 74 6f 72 65 20 61 20 73 74 72 69 6e 67 20 63 6f  tore a string co
2c5a2 6e 74 61 69 6e 69 6e 67 0d 0a 2a 2a 20 6d 75 6c  ntaining..** mul
2c5a3 74 69 70 6c 65 20 69 6e 74 65 67 65 72 73 20 76  tiple integers v
2c5a4 61 6c 75 65 73 20 69 6e 20 74 68 65 20 6e 44 4c  alues in the nDL
2c5a5 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 73 71 6c 69  t column of sqli
2c5a6 74 65 5f 73 74 61 74 33 2e 20 20 54 68 65 20 66  te_stat3.  The f
2c5a7 69 72 73 74 0d 0a 2a 2a 20 69 6e 74 65 67 65 72  irst..** integer
2c5a8 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d   will be the num
2c5a9 62 65 72 20 6f 66 20 70 72 69 6f 72 20 69 6e 64  ber of prior ind
2c5aa 65 78 20 65 6e 74 69 72 65 73 20 74 68 61 74 20  ex entires that 
2c5ab 61 72 65 20 64 69 73 74 69 6e 63 74 20 69 6e 0d  are distinct in.
2c5ac 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73  .** the left-mos
2c5ad 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73  t column.  The s
2c5ae 65 63 6f 6e 64 20 69 6e 74 65 67 65 72 20 77 69  econd integer wi
2c5af 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72  ll be the number
2c5b0 20 6f 66 20 70 72 69 6f 72 20 69 6e 64 65 78 0d   of prior index.
2c5b1 0a 2a 2a 20 65 6e 74 72 69 65 73 20 74 68 61 74  .** entries that
2c5b2 20 61 72 65 20 64 69 73 74 69 6e 63 74 20 69 6e   are distinct in
2c5b3 20 74 68 65 20 66 69 72 73 74 20 74 77 6f 20 63   the first two c
2c5b4 6f 6c 75 6d 6e 73 2e 20 20 54 68 65 20 74 68 69  olumns.  The thi
2c5b5 72 64 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 77  rd integer..** w
2c5b6 69 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d 62 65  ill be the numbe
2c5b7 72 20 6f 66 20 70 72 69 6f 72 20 69 6e 64 65 78  r of prior index
2c5b8 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 61 72   entries that ar
2c5b9 65 20 64 69 73 74 69 6e 63 74 20 69 6e 20 74 68  e distinct in th
2c5ba 65 20 66 69 72 73 74 0d 0a 2a 2a 20 74 68 72 65  e first..** thre
2c5bb 65 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 6e 64 20  e columns.  And 
2c5bc 73 6f 20 66 6f 72 74 68 2e 20 20 57 69 74 68 20  so forth.  With 
2c5bd 74 68 61 74 20 65 78 74 65 6e 73 69 6f 6e 2c 20  that extension, 
2c5be 74 68 65 20 6e 44 4c 74 20 66 69 65 6c 64 20 69  the nDLt field i
2c5bf 73 0d 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e  s..** similar in
2c5c0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
2c5c1 20 73 71 6c 69 74 65 5f 73 74 61 74 31 2e 73 74   sqlite_stat1.st
2c5c2 61 74 20 66 69 65 6c 64 2e 0d 0a 2a 2a 0d 0a 2a  at field...**..*
2c5c3 2a 20 54 68 65 72 65 20 63 61 6e 20 62 65 20 61  * There can be a
2c5c4 6e 20 61 72 62 69 74 72 61 72 79 20 6e 75 6d 62  n arbitrary numb
2c5c5 65 72 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61  er of sqlite_sta
2c5c6 74 33 20 65 6e 74 72 69 65 73 20 70 65 72 20 69  t3 entries per i
2c5c7 6e 64 65 78 2e 0d 0a 2a 2a 20 54 68 65 20 41 4e  ndex...** The AN
2c5c8 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 20 77 69  ALYZE command wi
2c5c9 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 67 65 6e  ll typically gen
2c5ca 65 72 61 74 65 20 73 71 6c 69 74 65 5f 73 74 61  erate sqlite_sta
2c5cb 74 33 20 74 61 62 6c 65 73 0d 0a 2a 2a 20 74 68  t3 tables..** th
2c5cc 61 74 20 63 6f 6e 74 61 69 6e 20 62 65 74 77 65  at contain betwe
2c5cd 65 6e 20 31 30 20 61 6e 64 20 34 30 20 73 61 6d  en 10 and 40 sam
2c5ce 70 6c 65 73 20 77 68 69 63 68 20 61 72 65 20 64  ples which are d
2c5cf 69 73 74 72 69 62 75 74 65 64 20 61 63 72 6f 73  istributed acros
2c5d0 73 0d 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 70  s..** the key sp
2c5d1 61 63 65 2c 20 74 68 6f 75 67 68 20 6e 6f 74 20  ace, though not 
2c5d2 75 6e 69 66 6f 72 6d 6c 79 2c 20 61 6e 64 20 77  uniformly, and w
2c5d3 68 69 63 68 20 69 6e 63 6c 75 64 65 20 73 61 6d  hich include sam
2c5d4 70 6c 65 73 20 77 69 74 68 0d 0a 2a 2a 20 6c 61  ples with..** la
2c5d5 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e  rgest possible n
2c5d6 45 71 20 76 61 6c 75 65 73 2e 0d 0a 2a 2f 0d 0a  Eq values...*/..
2c5d7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2c5d8 4d 49 54 5f 41 4e 41 4c 59 5a 45 0d 0a 0d 0a 2f  MIT_ANALYZE..../
2c5d9 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
2c5da 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
2c5db 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65  e that opens the
2c5dc 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2c5dd 62 6c 65 20 66 6f 72 0d 0a 2a 2a 20 77 72 69 74  ble for..** writ
2c5de 69 6e 67 20 77 69 74 68 20 63 75 72 73 6f 72 20  ing with cursor 
2c5df 69 53 74 61 74 43 75 72 2e 20 49 66 20 74 68 65  iStatCur. If the
2c5e0 20 6c 69 62 72 61 72 79 20 77 61 73 20 62 75 69   library was bui
2c5e1 6c 74 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20  lt with the..** 
2c5e2 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2c5e3 41 54 33 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  AT3 macro define
2c5e4 64 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69  d, then the sqli
2c5e5 74 65 5f 73 74 61 74 33 20 74 61 62 6c 65 20 69  te_stat3 table i
2c5e6 73 0d 0a 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72  s..** opened for
2c5e7 20 77 72 69 74 69 6e 67 20 75 73 69 6e 67 20 63   writing using c
2c5e8 75 72 73 6f 72 20 28 69 53 74 61 74 43 75 72 2b  ursor (iStatCur+
2c5e9 31 29 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  1)..**..** If th
2c5ea 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
2c5eb 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 70  ables does not p
2c5ec 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2c  reviously exist,
2c5ed 20 69 74 20 69 73 20 63 72 65 61 74 65 64 2e 0d   it is created..
2c5ee 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69  .** Similarly, i
2c5ef 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
2c5f0 74 33 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f  t3 table does no
2c5f1 74 20 65 78 69 73 74 20 61 6e 64 20 74 68 65 20  t exist and the 
2c5f2 6c 69 62 72 61 72 79 0d 0a 2a 2a 20 69 73 20 63  library..** is c
2c5f3 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c  ompiled with SQL
2c5f4 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
2c5f5 20 64 65 66 69 6e 65 64 2c 20 69 74 20 69 73 20   defined, it is 
2c5f6 63 72 65 61 74 65 64 2e 20 0d 0a 2a 2a 0d 0a 2a  created. ..**..*
2c5f7 2a 20 41 72 67 75 6d 65 6e 74 20 7a 57 68 65 72  * Argument zWher
2c5f8 65 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74  e may be a point
2c5f9 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63  er to a buffer c
2c5fa 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74 61 62 6c  ontaining a tabl
2c5fb 65 20 6e 61 6d 65 2c 0d 0a 2a 2a 20 6f 72 20 69  e name,..** or i
2c5fc 74 20 6d 61 79 20 62 65 20 61 20 4e 55 4c 4c 20  t may be a NULL 
2c5fd 70 6f 69 6e 74 65 72 2e 20 49 66 20 69 74 20 69  pointer. If it i
2c5fe 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
2c5ff 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 0d   all entries in.
2c600 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73  .** the sqlite_s
2c601 74 61 74 31 20 61 6e 64 20 28 69 66 20 61 70 70  tat1 and (if app
2c602 6c 69 63 61 62 6c 65 29 20 73 71 6c 69 74 65 5f  licable) sqlite_
2c603 73 74 61 74 33 20 74 61 62 6c 65 73 20 61 73 73  stat3 tables ass
2c604 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68  ociated..** with
2c605 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
2c606 20 61 72 65 20 64 65 6c 65 74 65 64 2e 20 49 66   are deleted. If
2c607 20 7a 57 68 65 72 65 3d 3d 30 2c 20 74 68 65 6e   zWhere==0, then
2c608 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
2c609 65 64 0d 0a 2a 2a 20 74 6f 20 64 65 6c 65 74 65  ed..** to delete
2c60a 20 61 6c 6c 20 73 74 61 74 20 74 61 62 6c 65 20   all stat table 
2c60b 65 6e 74 72 69 65 73 2e 0d 0a 2a 2f 0d 0a 73 74  entries...*/..st
2c60c 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 74  atic void openSt
2c60d 61 74 54 61 62 6c 65 28 0d 0a 20 20 50 61 72 73  atTable(..  Pars
2c60e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2c60f 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2c610 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74  ontext */..  int
2c611 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20   iDb,           
2c612 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2c613 62 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b  base we are look
2c614 69 6e 67 20 69 6e 20 2a 2f 0d 0a 20 20 69 6e 74  ing in */..  int
2c615 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 20 20   iStatCur,      
2c616 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
2c617 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2c618 62 6c 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73  ble on this curs
2c619 6f 72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  or */..  const c
2c61a 68 61 72 20 2a 7a 57 68 65 72 65 2c 20 20 20 20  har *zWhere,    
2c61b 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 72 69   /* Delete entri
2c61c 65 73 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c  es for this tabl
2c61d 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0d 0a 20  e or index */.. 
2c61e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 68   const char *zWh
2c61f 65 72 65 54 79 70 65 20 20 2f 2a 20 45 69 74 68  ereType  /* Eith
2c620 65 72 20 22 74 62 6c 22 20 6f 72 20 22 69 64 78  er "tbl" or "idx
2c621 22 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 74 61 74  " */..){..  stat
2c622 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
2c623 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  {..    const cha
2c624 72 20 2a 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 63  r *zName;..    c
2c625 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 73  onst char *zCols
2c626 3b 0d 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d 20  ;..  } aTable[] 
2c627 3d 20 7b 0d 0a 20 20 20 20 7b 20 22 73 71 6c 69  = {..    { "sqli
2c628 74 65 5f 73 74 61 74 31 22 2c 20 22 74 62 6c 2c  te_stat1", "tbl,
2c629 69 64 78 2c 73 74 61 74 22 20 7d 2c 0d 0a 23 69  idx,stat" },..#i
2c62a 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2c62b 4c 45 5f 53 54 41 54 33 0d 0a 20 20 20 20 7b 20  LE_STAT3..    { 
2c62c 22 73 71 6c 69 74 65 5f 73 74 61 74 33 22 2c 20  "sqlite_stat3", 
2c62d 22 74 62 6c 2c 69 64 78 2c 6e 65 71 2c 6e 6c 74  "tbl,idx,neq,nlt
2c62e 2c 6e 64 6c 74 2c 73 61 6d 70 6c 65 22 20 7d 2c  ,ndlt,sample" },
2c62f 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a  ..#endif..  };..
2c630 0d 0a 20 20 69 6e 74 20 61 52 6f 6f 74 5b 5d 20  ..  int aRoot[] 
2c631 3d 20 7b 30 2c 20 30 7d 3b 0d 0a 20 20 75 38 20  = {0, 0};..  u8 
2c632 61 43 72 65 61 74 65 54 62 6c 5b 5d 20 3d 20 7b  aCreateTbl[] = {
2c633 30 2c 20 30 7d 3b 0d 0a 0d 0a 20 20 69 6e 74 20  0, 0};....  int 
2c634 69 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  i;..  sqlite3 *d
2c635 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d  b = pParse->db;.
2c636 0a 20 20 44 62 20 2a 70 44 62 3b 0d 0a 20 20 56  .  Db *pDb;..  V
2c637 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
2c638 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2c639 0d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ..  if( v==0 ) r
2c63a 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73 65 72 74  eturn;..  assert
2c63b 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
2c63c 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62  ldsAllMutexes(db
2c63d 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
2c63e 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2c63f 3d 3d 64 62 20 29 3b 0d 0a 20 20 70 44 62 20 3d  ==db );..  pDb =
2c640 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0d   &db->aDb[iDb];.
2c641 0a 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 6e  ...  /* Create n
2c642 65 77 20 73 74 61 74 69 73 74 69 63 20 74 61 62  ew statistic tab
2c643 6c 65 73 20 69 66 20 74 68 65 79 20 64 6f 20 6e  les if they do n
2c644 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 63 6c 65  ot exist, or cle
2c645 61 72 20 74 68 65 6d 0d 0a 20 20 2a 2a 20 69 66  ar them..  ** if
2c646 20 74 68 65 79 20 64 6f 20 61 6c 72 65 61 64 79   they do already
2c647 20 65 78 69 73 74 2e 0d 0a 20 20 2a 2f 0d 0a 20   exist...  */.. 
2c648 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
2c649 79 53 69 7a 65 28 61 54 61 62 6c 65 29 3b 20 69  ySize(aTable); i
2c64a 2b 2b 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  ++){..    const 
2c64b 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 61 54 61  char *zTab = aTa
2c64c 62 6c 65 5b 69 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  ble[i].zName;.. 
2c64d 20 20 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b     Table *pStat;
2c64e 0d 0a 20 20 20 20 69 66 28 20 28 70 53 74 61 74  ..    if( (pStat
2c64f 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
2c650 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 70 44  ble(db, zTab, pD
2c651 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20 29 7b  b->zName))==0 ){
2c652 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  ..      /* The s
2c653 71 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20 74  qlite_stat[12] t
2c654 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78  able does not ex
2c655 69 73 74 2e 20 43 72 65 61 74 65 20 69 74 2e 20  ist. Create it. 
2c656 4e 6f 74 65 20 74 68 61 74 20 61 20 0d 0a 20 20  Note that a ..  
2c657 20 20 20 20 2a 2a 20 73 69 64 65 2d 65 66 66 65      ** side-effe
2c658 63 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  ct of the CREATE
2c659 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2c65a 20 69 73 20 74 6f 20 6c 65 61 76 65 20 74 68 65   is to leave the
2c65b 20 72 6f 6f 74 70 61 67 65 20 0d 0a 20 20 20 20   rootpage ..    
2c65c 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20    ** of the new 
2c65d 74 61 62 6c 65 20 69 6e 20 72 65 67 69 73 74 65  table in registe
2c65e 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  r pParse->regRoo
2c65f 74 2e 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72  t. This is impor
2c660 74 61 6e 74 20 0d 0a 20 20 20 20 20 20 2a 2a 20  tant ..      ** 
2c661 62 65 63 61 75 73 65 20 74 68 65 20 4f 70 65 6e  because the Open
2c662 57 72 69 74 65 20 6f 70 63 6f 64 65 20 62 65 6c  Write opcode bel
2c663 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 69  ow will be needi
2c664 6e 67 20 69 74 2e 20 2a 2f 0d 0a 20 20 20 20 20  ng it. */..     
2c665 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2c666 72 73 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20  rse(pParse,..   
2c667 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54         "CREATE T
2c668 41 42 4c 45 20 25 51 2e 25 73 28 25 73 29 22 2c  ABLE %Q.%s(%s)",
2c669 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61   pDb->zName, zTa
2c66a 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e 7a 43 6f  b, aTable[i].zCo
2c66b 6c 73 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20  ls..      );..  
2c66c 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70      aRoot[i] = p
2c66d 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0d  Parse->regRoot;.
2c66e 0a 20 20 20 20 20 20 61 43 72 65 61 74 65 54 62  .      aCreateTb
2c66f 6c 5b 69 5d 20 3d 20 31 3b 0d 0a 20 20 20 20 7d  l[i] = 1;..    }
2c670 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  else{..      /* 
2c671 54 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64  The table alread
2c672 79 20 65 78 69 73 74 73 2e 20 49 66 20 7a 57 68  y exists. If zWh
2c673 65 72 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  ere is not NULL,
2c674 20 64 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72   delete all entr
2c675 69 65 73 20 0d 0a 20 20 20 20 20 20 2a 2a 20 61  ies ..      ** a
2c676 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2c677 68 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e  he table zWhere.
2c678 20 49 66 20 7a 57 68 65 72 65 20 69 73 20 4e 55   If zWhere is NU
2c679 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 0d 0a  LL, delete the..
2c67a 20 20 20 20 20 20 2a 2a 20 65 6e 74 69 72 65 20        ** entire 
2c67b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
2c67c 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 20 20 20  table. */..     
2c67d 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70 53 74 61   aRoot[i] = pSta
2c67e 74 2d 3e 74 6e 75 6d 3b 0d 0a 20 20 20 20 20 20  t->tnum;..      
2c67f 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
2c680 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 61 52  (pParse, iDb, aR
2c681 6f 6f 74 5b 69 5d 2c 20 31 2c 20 7a 54 61 62 29  oot[i], 1, zTab)
2c682 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 57 68  ;..      if( zWh
2c683 65 72 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ere ){..        
2c684 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2c685 73 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20 20  se(pParse,..    
2c686 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
2c687 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20  ROM %Q.%s WHERE 
2c688 25 73 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61  %s=%Q", pDb->zNa
2c689 6d 65 2c 20 7a 54 61 62 2c 20 7a 57 68 65 72 65  me, zTab, zWhere
2c68a 54 79 70 65 2c 20 7a 57 68 65 72 65 0d 0a 20 20  Type, zWhere..  
2c68b 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
2c68c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2c68d 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  /* The sqlite_st
2c68e 61 74 5b 31 32 5d 20 74 61 62 6c 65 20 61 6c 72  at[12] table alr
2c68f 65 61 64 79 20 65 78 69 73 74 73 2e 20 20 44 65  eady exists.  De
2c690 6c 65 74 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a  lete all rows. *
2c691 2f 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  /..        sqlit
2c692 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c693 4f 50 5f 43 6c 65 61 72 2c 20 61 52 6f 6f 74 5b  OP_Clear, aRoot[
2c694 69 5d 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20  i], iDb);..     
2c695 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
2c696 0d 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
2c697 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 33 5d 20  sqlite_stat[13] 
2c698 74 61 62 6c 65 73 20 66 6f 72 20 77 72 69 74 69  tables for writi
2c699 6e 67 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d  ng. */..  for(i=
2c69a 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61  0; i<ArraySize(a
2c69b 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0d 0a 20  Table); i++){.. 
2c69c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c69d 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
2c69e 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2b 69  rite, iStatCur+i
2c69f 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29  , aRoot[i], iDb)
2c6a0 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2c6a1 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
2c6a2 2c 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f  , (char *)3, P4_
2c6a3 49 4e 54 33 32 29 3b 0d 0a 20 20 20 20 73 71 6c  INT32);..    sql
2c6a4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2c6a5 28 76 2c 20 61 43 72 65 61 74 65 54 62 6c 5b 69  (v, aCreateTbl[i
2c6a6 5d 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  ]);..  }..}..../
2c6a7 2a 0d 0a 2a 2a 20 52 65 63 6f 6d 6d 65 6e 64 65  *..** Recommende
2c6a8 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 6d 70  d number of samp
2c6a9 6c 65 73 20 66 6f 72 20 73 71 6c 69 74 65 5f 73  les for sqlite_s
2c6aa 74 61 74 33 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  tat3..*/..#ifnde
2c6ab 66 20 53 51 4c 49 54 45 5f 53 54 41 54 33 5f 53  f SQLITE_STAT3_S
2c6ac 41 4d 50 4c 45 53 0d 0a 23 20 64 65 66 69 6e 65  AMPLES..# define
2c6ad 20 53 51 4c 49 54 45 5f 53 54 41 54 33 5f 53 41   SQLITE_STAT3_SA
2c6ae 4d 50 4c 45 53 20 32 34 0d 0a 23 65 6e 64 69 66  MPLES 24..#endif
2c6af 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 72 65 65  ..../*..** Three
2c6b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 2d   SQL functions -
2c6b1 20 73 74 61 74 33 5f 69 6e 69 74 28 29 2c 20 73   stat3_init(), s
2c6b2 74 61 74 33 5f 70 75 73 68 28 29 2c 20 61 6e 64  tat3_push(), and
2c6b3 20 73 74 61 74 33 5f 70 6f 70 28 29 20 2d 0d 0a   stat3_pop() -..
2c6b4 2a 2a 20 73 68 61 72 65 20 61 6e 20 69 6e 73 74  ** share an inst
2c6b5 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
2c6b6 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
2c6b7 74 6f 20 68 6f 6c 64 20 74 68 65 69 72 20 73 74  to hold their st
2c6b8 61 74 65 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ate..** informat
2c6b9 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65  ion...*/..typede
2c6ba 66 20 73 74 72 75 63 74 20 53 74 61 74 33 41 63  f struct Stat3Ac
2c6bb 63 75 6d 20 53 74 61 74 33 41 63 63 75 6d 3b 0d  cum Stat3Accum;.
2c6bc 0a 73 74 72 75 63 74 20 53 74 61 74 33 41 63 63  .struct Stat3Acc
2c6bd 75 6d 20 7b 0d 0a 20 20 74 52 6f 77 63 6e 74 20  um {..  tRowcnt 
2c6be 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  nRow;           
2c6bf 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2c6c0 6f 77 73 20 69 6e 20 74 68 65 20 65 6e 74 69 72  ows in the entir
2c6c1 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 74 52  e table */..  tR
2c6c2 6f 77 63 6e 74 20 6e 50 53 61 6d 70 6c 65 3b 20  owcnt nPSample; 
2c6c3 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 6f          /* How o
2c6c4 66 74 65 6e 20 74 6f 20 64 6f 20 61 20 70 65 72  ften to do a per
2c6c5 69 6f 64 69 63 20 73 61 6d 70 6c 65 20 2a 2f 0d  iodic sample */.
2c6c6 0a 20 20 69 6e 74 20 69 4d 69 6e 3b 20 20 20 20  .  int iMin;    
2c6c7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c6c8 49 6e 64 65 78 20 6f 66 20 65 6e 74 72 79 20 77  Index of entry w
2c6c9 69 74 68 20 6d 69 6e 69 6d 75 6d 20 6e 45 71 20  ith minimum nEq 
2c6ca 61 6e 64 20 68 61 73 68 20 2a 2f 0d 0a 20 20 69  and hash */..  i
2c6cb 6e 74 20 6d 78 53 61 6d 70 6c 65 3b 20 20 20 20  nt mxSample;    
2c6cc 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
2c6cd 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  mum number of sa
2c6ce 6d 70 6c 65 73 20 74 6f 20 61 63 63 75 6d 75 6c  mples to accumul
2c6cf 61 74 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53  ate */..  int nS
2c6d0 61 6d 70 6c 65 3b 20 20 20 20 20 20 20 20 20 20  ample;          
2c6d1 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
2c6d2 75 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73  umber of samples
2c6d3 20 2a 2f 0d 0a 20 20 75 33 32 20 69 50 72 6e 3b   */..  u32 iPrn;
2c6d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6d5 20 2f 2a 20 50 73 65 75 64 6f 2d 72 61 6e 64 6f   /* Pseudo-rando
2c6d6 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f  m number used fo
2c6d7 72 20 73 61 6d 70 6c 69 6e 67 20 2a 2f 0d 0a 20  r sampling */.. 
2c6d8 20 73 74 72 75 63 74 20 53 74 61 74 33 53 61 6d   struct Stat3Sam
2c6d9 70 6c 65 20 7b 0d 0a 20 20 20 20 69 36 34 20 69  ple {..    i64 i
2c6da 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20  Rowid;          
2c6db 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69        /* Rowid i
2c6dc 6e 20 6d 61 69 6e 20 74 61 62 6c 65 20 6f 66 20  n main table of 
2c6dd 74 68 65 20 6b 65 79 20 2a 2f 0d 0a 20 20 20 20  the key */..    
2c6de 74 52 6f 77 63 6e 74 20 6e 45 71 3b 20 20 20 20  tRowcnt nEq;    
2c6df 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71             /* sq
2c6e0 6c 69 74 65 5f 73 74 61 74 33 2e 6e 45 71 20 2a  lite_stat3.nEq *
2c6e1 2f 0d 0a 20 20 20 20 74 52 6f 77 63 6e 74 20 6e  /..    tRowcnt n
2c6e2 4c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Lt;             
2c6e3 20 20 2f 2a 20 73 71 6c 69 74 65 5f 73 74 61 74    /* sqlite_stat
2c6e4 33 2e 6e 4c 74 20 2a 2f 0d 0a 20 20 20 20 74 52  3.nLt */..    tR
2c6e5 6f 77 63 6e 74 20 6e 44 4c 74 3b 20 20 20 20 20  owcnt nDLt;     
2c6e6 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
2c6e7 74 65 5f 73 74 61 74 33 2e 6e 44 4c 74 20 2a 2f  te_stat3.nDLt */
2c6e8 0d 0a 20 20 20 20 75 38 20 69 73 50 53 61 6d 70  ..    u8 isPSamp
2c6e9 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
2c6ea 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 70 65   /* True if a pe
2c6eb 72 69 6f 64 69 63 20 73 61 6d 70 6c 65 20 2a 2f  riodic sample */
2c6ec 0d 0a 20 20 20 20 75 33 32 20 69 48 61 73 68 3b  ..    u32 iHash;
2c6ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6ee 20 2f 2a 20 54 69 65 62 72 65 61 6b 65 72 20 68   /* Tiebreaker h
2c6ef 61 73 68 20 2a 2f 0d 0a 20 20 7d 20 2a 61 3b 20  ash */..  } *a; 
2c6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6f1 20 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20      /* An array 
2c6f2 6f 66 20 73 61 6d 70 6c 65 73 20 2a 2f 0d 0a 7d  of samples */..}
2c6f3 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  ;....#ifdef SQLI
2c6f4 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d  TE_ENABLE_STAT3.
2c6f5 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
2c6f6 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  tation of the st
2c6f7 61 74 33 5f 69 6e 69 74 28 43 2c 53 29 20 53 51  at3_init(C,S) SQ
2c6f8 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  L function.  The
2c6f9 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73 0d   two parameters.
2c6fa 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 75 6d 62  .** are the numb
2c6fb 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
2c6fc 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
2c6fd 20 28 43 29 20 61 6e 64 20 74 68 65 20 6e 75 6d   (C) and the num
2c6fe 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73 0d 0a  ber of samples..
2c6ff 2a 2a 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65  ** to accumulate
2c700 20 28 53 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68   (S)...**..** Th
2c701 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  is routine alloc
2c702 61 74 65 73 20 74 68 65 20 53 74 61 74 33 41 63  ates the Stat3Ac
2c703 63 75 6d 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d  cum object...**.
2c704 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
2c705 61 6c 75 65 20 69 73 20 74 68 65 20 53 74 61 74  alue is the Stat
2c706 33 41 63 63 75 6d 20 6f 62 6a 65 63 74 20 28 50  3Accum object (P
2c707 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  )...*/..static v
2c708 6f 69 64 20 73 74 61 74 33 49 6e 69 74 28 0d 0a  oid stat3Init(..
2c709 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
2c70a 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69  t *context,..  i
2c70b 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69  nt argc,..  sqli
2c70c 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2c70d 0d 0a 29 7b 0d 0a 20 20 53 74 61 74 33 41 63 63  ..){..  Stat3Acc
2c70e 75 6d 20 2a 70 3b 0d 0a 20 20 74 52 6f 77 63 6e  um *p;..  tRowcn
2c70f 74 20 6e 52 6f 77 3b 0d 0a 20 20 69 6e 74 20 6d  t nRow;..  int m
2c710 78 53 61 6d 70 6c 65 3b 0d 0a 20 20 69 6e 74 20  xSample;..  int 
2c711 6e 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50  n;....  UNUSED_P
2c712 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0d  ARAMETER(argc);.
2c713 0a 20 20 6e 52 6f 77 20 3d 20 28 74 52 6f 77 63  .  nRow = (tRowc
2c714 6e 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt)sqlite3_value
2c715 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
2c716 0d 0a 20 20 6d 78 53 61 6d 70 6c 65 20 3d 20 73  ..  mxSample = s
2c717 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
2c718 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20 20 6e 20  (argv[1]);..  n 
2c719 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 73  = sizeof(*p) + s
2c71a 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 2a 6d  izeof(p->a[0])*m
2c71b 78 53 61 6d 70 6c 65 3b 0d 0a 20 20 70 20 3d 20  xSample;..  p = 
2c71c 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
2c71d 6e 20 29 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30  n );..  if( p==0
2c71e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2c71f 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
2c720 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20  mem(context);.. 
2c721 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d     return;..  }.
2c722 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
2c723 6e 29 3b 0d 0a 20 20 70 2d 3e 61 20 3d 20 28 73  n);..  p->a = (s
2c724 74 72 75 63 74 20 53 74 61 74 33 53 61 6d 70 6c  truct Stat3Sampl
2c725 65 2a 29 26 70 5b 31 5d 3b 0d 0a 20 20 70 2d 3e  e*)&p[1];..  p->
2c726 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0d 0a 20 20  nRow = nRow;..  
2c727 70 2d 3e 6d 78 53 61 6d 70 6c 65 20 3d 20 6d 78  p->mxSample = mx
2c728 53 61 6d 70 6c 65 3b 0d 0a 20 20 70 2d 3e 6e 50  Sample;..  p->nP
2c729 53 61 6d 70 6c 65 20 3d 20 70 2d 3e 6e 52 6f 77  Sample = p->nRow
2c72a 2f 28 6d 78 53 61 6d 70 6c 65 2f 33 2b 31 29 20  /(mxSample/3+1) 
2c72b 2b 20 31 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  + 1;..  sqlite3_
2c72c 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f  randomness(sizeo
2c72d 66 28 70 2d 3e 69 50 72 6e 29 2c 20 26 70 2d 3e  f(p->iPrn), &p->
2c72e 69 50 72 6e 29 3b 0d 0a 20 20 73 71 6c 69 74 65  iPrn);..  sqlite
2c72f 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f  3_result_blob(co
2c730 6e 74 65 78 74 2c 20 70 2c 20 73 69 7a 65 6f 66  ntext, p, sizeof
2c731 28 70 29 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  (p), sqlite3_fre
2c732 65 29 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 63  e);..}..static c
2c733 6f 6e 73 74 20 46 75 6e 63 44 65 66 20 73 74 61  onst FuncDef sta
2c734 74 33 49 6e 69 74 46 75 6e 63 64 65 66 20 3d 20  t3InitFuncdef = 
2c735 7b 0d 0a 20 20 32 2c 20 20 20 20 20 20 20 20 20  {..  2,         
2c736 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a         /* nArg *
2c737 2f 0d 0a 20 20 53 51 4c 49 54 45 5f 55 54 46 38  /..  SQLITE_UTF8
2c738 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45  ,      /* iPrefE
2c739 6e 63 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20  nc */..  0,     
2c73a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
2c73b 61 67 73 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20  ags */..  0,    
2c73c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
2c73d 55 73 65 72 44 61 74 61 20 2a 2f 0d 0a 20 20 30  UserData */..  0
2c73e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2c73f 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0d 0a 20 20   /* pNext */..  
2c740 73 74 61 74 33 49 6e 69 74 2c 20 20 20 20 20 20  stat3Init,      
2c741 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0d 0a 20    /* xFunc */.. 
2c742 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
2c743 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0d 0a     /* xStep */..
2c744 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
2c745 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65      /* xFinalize
2c746 20 2a 2f 0d 0a 20 20 22 73 74 61 74 33 5f 69 6e   */..  "stat3_in
2c747 69 74 22 2c 20 20 20 20 20 2f 2a 20 7a 4e 61 6d  it",     /* zNam
2c748 65 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20  e */..  0,      
2c749 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61            /* pHa
2c74a 73 68 20 2a 2f 0d 0a 20 20 30 20 20 20 20 20 20  sh */..  0      
2c74b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 44             /* pD
2c74c 65 73 74 72 75 63 74 6f 72 20 2a 2f 0d 0a 7d 3b  estructor */..};
2c74d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70  ....../*..** Imp
2c74e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2c74f 68 65 20 73 74 61 74 33 5f 70 75 73 68 28 6e 45  he stat3_push(nE
2c750 71 2c 6e 4c 74 2c 6e 44 4c 74 2c 72 6f 77 69 64  q,nLt,nDLt,rowid
2c751 2c 50 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ,P) SQL function
2c752 2e 20 20 54 68 65 0d 0a 2a 2a 20 61 72 67 75 6d  .  The..** argum
2c753 65 6e 74 73 20 64 65 73 63 72 69 62 65 20 61 20  ents describe a 
2c754 73 69 6e 67 6c 65 20 6b 65 79 20 69 6e 73 74 61  single key insta
2c755 6e 63 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  nce.  This routi
2c756 6e 65 20 6d 61 6b 65 73 20 74 68 65 20 0d 0a 2a  ne makes the ..*
2c757 2a 20 64 65 63 69 73 69 6f 6e 20 61 62 6f 75 74  * decision about
2c758 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
2c759 74 6f 20 72 65 74 61 69 6e 20 74 68 69 73 20 6b  to retain this k
2c75a 65 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  ey for the sqlit
2c75b 65 5f 73 74 61 74 33 0d 0a 2a 2a 20 74 61 62 6c  e_stat3..** tabl
2c75c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  e...**..** The r
2c75d 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 4e  eturn value is N
2c75e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ULL...*/..static
2c75f 20 76 6f 69 64 20 73 74 61 74 33 50 75 73 68 28   void stat3Push(
2c760 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ..  sqlite3_cont
2c761 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20  ext *context,.. 
2c762 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71   int argc,..  sq
2c763 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2c764 67 76 0d 0a 29 7b 0d 0a 20 20 53 74 61 74 33 41  gv..){..  Stat3A
2c765 63 63 75 6d 20 2a 70 20 3d 20 28 53 74 61 74 33  ccum *p = (Stat3
2c766 41 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 76  Accum*)sqlite3_v
2c767 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 34  alue_blob(argv[4
2c768 5d 29 3b 0d 0a 20 20 74 52 6f 77 63 6e 74 20 6e  ]);..  tRowcnt n
2c769 45 71 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  Eq = sqlite3_val
2c76a 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d  ue_int64(argv[0]
2c76b 29 3b 0d 0a 20 20 74 52 6f 77 63 6e 74 20 6e 4c  );..  tRowcnt nL
2c76c 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
2c76d 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 31 5d 29  e_int64(argv[1])
2c76e 3b 0d 0a 20 20 74 52 6f 77 63 6e 74 20 6e 44 4c  ;..  tRowcnt nDL
2c76f 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
2c770 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 32 5d 29  e_int64(argv[2])
2c771 3b 0d 0a 20 20 69 36 34 20 72 6f 77 69 64 20 3d  ;..  i64 rowid =
2c772 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
2c773 6e 74 36 34 28 61 72 67 76 5b 33 5d 29 3b 0d 0a  nt64(argv[3]);..
2c774 20 20 75 38 20 69 73 50 53 61 6d 70 6c 65 20 3d    u8 isPSample =
2c775 20 30 3b 0d 0a 20 20 75 38 20 64 6f 49 6e 73 65   0;..  u8 doInse
2c776 72 74 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69  rt = 0;..  int i
2c777 4d 69 6e 20 3d 20 70 2d 3e 69 4d 69 6e 3b 0d 0a  Min = p->iMin;..
2c778 20 20 73 74 72 75 63 74 20 53 74 61 74 33 53 61    struct Stat3Sa
2c779 6d 70 6c 65 20 2a 70 53 61 6d 70 6c 65 3b 0d 0a  mple *pSample;..
2c77a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 75 33 32 20    int i;..  u32 
2c77b 68 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50  h;....  UNUSED_P
2c77c 41 52 41 4d 45 54 45 52 28 63 6f 6e 74 65 78 74  ARAMETER(context
2c77d 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
2c77e 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0d 0a 20  AMETER(argc);.. 
2c77f 20 69 66 28 20 6e 45 71 3d 3d 30 20 29 20 72 65   if( nEq==0 ) re
2c780 74 75 72 6e 3b 0d 0a 20 20 68 20 3d 20 70 2d 3e  turn;..  h = p->
2c781 69 50 72 6e 20 3d 20 70 2d 3e 69 50 72 6e 2a 31  iPrn = p->iPrn*1
2c782 31 30 33 35 31 35 32 34 35 20 2b 20 31 32 33 34  103515245 + 1234
2c783 35 3b 0d 0a 20 20 69 66 28 20 28 6e 4c 74 2f 70  5;..  if( (nLt/p
2c784 2d 3e 6e 50 53 61 6d 70 6c 65 29 21 3d 28 28 6e  ->nPSample)!=((n
2c785 45 71 2b 6e 4c 74 29 2f 70 2d 3e 6e 50 53 61 6d  Eq+nLt)/p->nPSam
2c786 70 6c 65 29 20 29 7b 0d 0a 20 20 20 20 64 6f 49  ple) ){..    doI
2c787 6e 73 65 72 74 20 3d 20 69 73 50 53 61 6d 70 6c  nsert = isPSampl
2c788 65 20 3d 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 20  e = 1;..  }else 
2c789 69 66 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3c 70  if( p->nSample<p
2c78a 2d 3e 6d 78 53 61 6d 70 6c 65 20 29 7b 0d 0a 20  ->mxSample ){.. 
2c78b 20 20 20 64 6f 49 6e 73 65 72 74 20 3d 20 31 3b     doInsert = 1;
2c78c 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
2c78d 69 66 28 20 6e 45 71 3e 70 2d 3e 61 5b 69 4d 69  if( nEq>p->a[iMi
2c78e 6e 5d 2e 6e 45 71 20 7c 7c 20 28 6e 45 71 3d 3d  n].nEq || (nEq==
2c78f 70 2d 3e 61 5b 69 4d 69 6e 5d 2e 6e 45 71 20 26  p->a[iMin].nEq &
2c790 26 20 68 3e 70 2d 3e 61 5b 69 4d 69 6e 5d 2e 69  & h>p->a[iMin].i
2c791 48 61 73 68 29 20 29 7b 0d 0a 20 20 20 20 20 20  Hash) ){..      
2c792 64 6f 49 6e 73 65 72 74 20 3d 20 31 3b 0d 0a 20  doInsert = 1;.. 
2c793 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28     }..  }..  if(
2c794 20 21 64 6f 49 6e 73 65 72 74 20 29 20 72 65 74   !doInsert ) ret
2c795 75 72 6e 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 6e  urn;..  if( p->n
2c796 53 61 6d 70 6c 65 3d 3d 70 2d 3e 6d 78 53 61 6d  Sample==p->mxSam
2c797 70 6c 65 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ple ){..    asse
2c798 72 74 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 20 2d  rt( p->nSample -
2c799 20 69 4d 69 6e 20 2d 20 31 20 3e 3d 20 30 20 29   iMin - 1 >= 0 )
2c79a 3b 0d 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 28 26  ;..    memmove(&
2c79b 70 2d 3e 61 5b 69 4d 69 6e 5d 2c 20 26 70 2d 3e  p->a[iMin], &p->
2c79c 61 5b 69 4d 69 6e 2b 31 5d 2c 20 73 69 7a 65 6f  a[iMin+1], sizeo
2c79d 66 28 70 2d 3e 61 5b 30 5d 29 2a 28 70 2d 3e 6e  f(p->a[0])*(p->n
2c79e 53 61 6d 70 6c 65 2d 69 4d 69 6e 2d 31 29 29 3b  Sample-iMin-1));
2c79f 0d 0a 20 20 20 20 70 53 61 6d 70 6c 65 20 3d 20  ..    pSample = 
2c7a0 26 70 2d 3e 61 5b 70 2d 3e 6e 53 61 6d 70 6c 65  &p->a[p->nSample
2c7a1 2d 31 5d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  -1];..  }else{..
2c7a2 20 20 20 20 70 53 61 6d 70 6c 65 20 3d 20 26 70      pSample = &p
2c7a3 2d 3e 61 5b 70 2d 3e 6e 53 61 6d 70 6c 65 2b 2b  ->a[p->nSample++
2c7a4 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 53 61 6d 70  ];..  }..  pSamp
2c7a5 6c 65 2d 3e 69 52 6f 77 69 64 20 3d 20 72 6f 77  le->iRowid = row
2c7a6 69 64 3b 0d 0a 20 20 70 53 61 6d 70 6c 65 2d 3e  id;..  pSample->
2c7a7 6e 45 71 20 3d 20 6e 45 71 3b 0d 0a 20 20 70 53  nEq = nEq;..  pS
2c7a8 61 6d 70 6c 65 2d 3e 6e 4c 74 20 3d 20 6e 4c 74  ample->nLt = nLt
2c7a9 3b 0d 0a 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 44  ;..  pSample->nD
2c7aa 4c 74 20 3d 20 6e 44 4c 74 3b 0d 0a 20 20 70 53  Lt = nDLt;..  pS
2c7ab 61 6d 70 6c 65 2d 3e 69 48 61 73 68 20 3d 20 68  ample->iHash = h
2c7ac 3b 0d 0a 20 20 70 53 61 6d 70 6c 65 2d 3e 69 73  ;..  pSample->is
2c7ad 50 53 61 6d 70 6c 65 20 3d 20 69 73 50 53 61 6d  PSample = isPSam
2c7ae 70 6c 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 46 69 6e  ple;....  /* Fin
2c7af 64 20 74 68 65 20 6e 65 77 20 6d 69 6e 69 6d 75  d the new minimu
2c7b0 6d 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 6e  m */..  if( p->n
2c7b1 53 61 6d 70 6c 65 3d 3d 70 2d 3e 6d 78 53 61 6d  Sample==p->mxSam
2c7b2 70 6c 65 20 29 7b 0d 0a 20 20 20 20 70 53 61 6d  ple ){..    pSam
2c7b3 70 6c 65 20 3d 20 70 2d 3e 61 3b 0d 0a 20 20 20  ple = p->a;..   
2c7b4 20 69 20 3d 20 30 3b 0d 0a 20 20 20 20 77 68 69   i = 0;..    whi
2c7b5 6c 65 28 20 70 53 61 6d 70 6c 65 2d 3e 69 73 50  le( pSample->isP
2c7b6 53 61 6d 70 6c 65 20 29 7b 0d 0a 20 20 20 20 20  Sample ){..     
2c7b7 20 69 2b 2b 3b 0d 0a 20 20 20 20 20 20 70 53 61   i++;..      pSa
2c7b8 6d 70 6c 65 2b 2b 3b 0d 0a 20 20 20 20 20 20 61  mple++;..      a
2c7b9 73 73 65 72 74 28 20 69 3c 70 2d 3e 6e 53 61 6d  ssert( i<p->nSam
2c7ba 70 6c 65 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ple );..    }.. 
2c7bb 20 20 20 6e 45 71 20 3d 20 70 53 61 6d 70 6c 65     nEq = pSample
2c7bc 2d 3e 6e 45 71 3b 0d 0a 20 20 20 20 68 20 3d 20  ->nEq;..    h = 
2c7bd 70 53 61 6d 70 6c 65 2d 3e 69 48 61 73 68 3b 0d  pSample->iHash;.
2c7be 0a 20 20 20 20 69 4d 69 6e 20 3d 20 69 3b 0d 0a  .    iMin = i;..
2c7bf 20 20 20 20 66 6f 72 28 69 2b 2b 2c 20 70 53 61      for(i++, pSa
2c7c0 6d 70 6c 65 2b 2b 3b 20 69 3c 70 2d 3e 6e 53 61  mple++; i<p->nSa
2c7c1 6d 70 6c 65 3b 20 69 2b 2b 2c 20 70 53 61 6d 70  mple; i++, pSamp
2c7c2 6c 65 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66  le++){..      if
2c7c3 28 20 70 53 61 6d 70 6c 65 2d 3e 69 73 50 53 61  ( pSample->isPSa
2c7c4 6d 70 6c 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  mple ) continue;
2c7c5 0d 0a 20 20 20 20 20 20 69 66 28 20 70 53 61 6d  ..      if( pSam
2c7c6 70 6c 65 2d 3e 6e 45 71 3c 6e 45 71 0d 0a 20 20  ple->nEq<nEq..  
2c7c7 20 20 20 20 20 7c 7c 20 28 70 53 61 6d 70 6c 65       || (pSample
2c7c8 2d 3e 6e 45 71 3d 3d 6e 45 71 20 26 26 20 70 53  ->nEq==nEq && pS
2c7c9 61 6d 70 6c 65 2d 3e 69 48 61 73 68 3c 68 29 0d  ample->iHash<h).
2c7ca 0a 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20  .      ){..     
2c7cb 20 20 20 69 4d 69 6e 20 3d 20 69 3b 0d 0a 20 20     iMin = i;..  
2c7cc 20 20 20 20 20 20 6e 45 71 20 3d 20 70 53 61 6d        nEq = pSam
2c7cd 70 6c 65 2d 3e 6e 45 71 3b 0d 0a 20 20 20 20 20  ple->nEq;..     
2c7ce 20 20 20 68 20 3d 20 70 53 61 6d 70 6c 65 2d 3e     h = pSample->
2c7cf 69 48 61 73 68 3b 0d 0a 20 20 20 20 20 20 7d 0d  iHash;..      }.
2c7d0 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d 3e 69  .    }..    p->i
2c7d1 4d 69 6e 20 3d 20 69 4d 69 6e 3b 0d 0a 20 20 7d  Min = iMin;..  }
2c7d2 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..}..static cons
2c7d3 74 20 46 75 6e 63 44 65 66 20 73 74 61 74 33 50  t FuncDef stat3P
2c7d4 75 73 68 46 75 6e 63 64 65 66 20 3d 20 7b 0d 0a  ushFuncdef = {..
2c7d5 20 20 35 2c 20 20 20 20 20 20 20 20 20 20 20 20    5,            
2c7d6 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0d 0a      /* nArg */..
2c7d7 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20    SQLITE_UTF8,  
2c7d8 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20      /* iPrefEnc 
2c7d9 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20 20  */..  0,        
2c7da 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73          /* flags
2c7db 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20   */..  0,       
2c7dc 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65           /* pUse
2c7dd 72 44 61 74 61 20 2a 2f 0d 0a 20 20 30 2c 20 20  rData */..  0,  
2c7de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c7df 20 70 4e 65 78 74 20 2a 2f 0d 0a 20 20 73 74 61   pNext */..  sta
2c7e0 74 33 50 75 73 68 2c 20 20 20 20 20 20 20 20 2f  t3Push,        /
2c7e1 2a 20 78 46 75 6e 63 20 2a 2f 0d 0a 20 20 30 2c  * xFunc */..  0,
2c7e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7e3 2f 2a 20 78 53 74 65 70 20 2a 2f 0d 0a 20 20 30  /* xStep */..  0
2c7e4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2c7e5 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f   /* xFinalize */
2c7e6 0d 0a 20 20 22 73 74 61 74 33 5f 70 75 73 68 22  ..  "stat3_push"
2c7e7 2c 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a  ,     /* zName *
2c7e8 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  /..  0,         
2c7e9 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20         /* pHash 
2c7ea 2a 2f 0d 0a 20 20 30 20 20 20 20 20 20 20 20 20  */..  0         
2c7eb 20 20 20 20 20 20 20 20 2f 2a 20 70 44 65 73 74          /* pDest
2c7ec 72 75 63 74 6f 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  ructor */..};...
2c7ed 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
2c7ee 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  tation of the st
2c7ef 61 74 33 5f 67 65 74 28 50 2c 4e 2c 2e 2e 2e 29  at3_get(P,N,...)
2c7f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20   SQL function.  
2c7f1 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0d  This routine is.
2c7f2 0a 2a 2a 20 75 73 65 64 20 74 6f 20 71 75 65 72  .** used to quer
2c7f3 79 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20  y the results.  
2c7f4 43 6f 6e 74 65 6e 74 20 69 73 20 72 65 74 75 72  Content is retur
2c7f5 6e 65 64 20 66 6f 72 20 74 68 65 20 4e 74 68 20  ned for the Nth 
2c7f6 73 71 6c 69 74 65 5f 73 74 61 74 33 0d 0a 2a 2a  sqlite_stat3..**
2c7f7 20 72 6f 77 20 77 68 65 72 65 20 4e 20 69 73 20   row where N is 
2c7f8 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 53 2d  between 0 and S-
2c7f9 31 20 61 6e 64 20 53 20 69 73 20 74 68 65 20 6e  1 and S is the n
2c7fa 75 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73  umber of samples
2c7fb 2e 20 20 54 68 65 0d 0a 2a 2a 20 76 61 6c 75 65  .  The..** value
2c7fc 20 72 65 74 75 72 6e 65 64 20 64 65 70 65 6e 64   returned depend
2c7fd 73 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  s on the number 
2c7fe 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 0d 0a 2a  of arguments...*
2c7ff 2a 0d 0a 2a 2a 20 20 20 61 72 67 63 3d 3d 32 20  *..**   argc==2 
2c800 20 20 20 72 65 73 75 6c 74 3a 20 20 72 6f 77 69     result:  rowi
2c801 64 0d 0a 2a 2a 20 20 20 61 72 67 63 3d 3d 33 20  d..**   argc==3 
2c802 20 20 20 72 65 73 75 6c 74 3a 20 20 6e 45 71 0d     result:  nEq.
2c803 0a 2a 2a 20 20 20 61 72 67 63 3d 3d 34 20 20 20  .**   argc==4   
2c804 20 72 65 73 75 6c 74 3a 20 20 6e 4c 74 0d 0a 2a   result:  nLt..*
2c805 2a 20 20 20 61 72 67 63 3d 3d 35 20 20 20 20 72  *   argc==5    r
2c806 65 73 75 6c 74 3a 20 20 6e 44 4c 74 0d 0a 2a 2f  esult:  nDLt..*/
2c807 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74  ..static void st
2c808 61 74 33 47 65 74 28 0d 0a 20 20 73 71 6c 69 74  at3Get(..  sqlit
2c809 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2c80a 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63  ext,..  int argc
2c80b 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ,..  sqlite3_val
2c80c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20  ue **argv..){.. 
2c80d 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
2c80e 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
2c80f 31 5d 29 3b 0d 0a 20 20 53 74 61 74 33 41 63 63  1]);..  Stat3Acc
2c810 75 6d 20 2a 70 20 3d 20 28 53 74 61 74 33 41 63  um *p = (Stat3Ac
2c811 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  cum*)sqlite3_val
2c812 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
2c813 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ;....  assert( p
2c814 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 2d  !=0 );..  if( p-
2c815 3e 6e 53 61 6d 70 6c 65 3c 3d 6e 20 29 20 72 65  >nSample<=n ) re
2c816 74 75 72 6e 3b 0d 0a 20 20 73 77 69 74 63 68 28  turn;..  switch(
2c817 20 61 72 67 63 20 29 7b 0d 0a 20 20 20 20 63 61   argc ){..    ca
2c818 73 65 20 32 3a 20 20 73 71 6c 69 74 65 33 5f 72  se 2:  sqlite3_r
2c819 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
2c81a 65 78 74 2c 20 70 2d 3e 61 5b 6e 5d 2e 69 52 6f  ext, p->a[n].iRo
2c81b 77 69 64 29 3b 20 62 72 65 61 6b 3b 0d 0a 20 20  wid); break;..  
2c81c 20 20 63 61 73 65 20 33 3a 20 20 73 71 6c 69 74    case 3:  sqlit
2c81d 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
2c81e 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 61 5b 6e 5d  context, p->a[n]
2c81f 2e 6e 45 71 29 3b 20 20 20 20 62 72 65 61 6b 3b  .nEq);    break;
2c820 0d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 20 73  ..    case 4:  s
2c821 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2c822 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e  t64(context, p->
2c823 61 5b 6e 5d 2e 6e 4c 74 29 3b 20 20 20 20 62 72  a[n].nLt);    br
2c824 65 61 6b 3b 0d 0a 20 20 20 20 64 65 66 61 75 6c  eak;..    defaul
2c825 74 3a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  t: sqlite3_resul
2c826 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c  t_int64(context,
2c827 20 70 2d 3e 61 5b 6e 5d 2e 6e 44 4c 74 29 3b 20   p->a[n].nDLt); 
2c828 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 7d    break;..  }..}
2c829 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 46  ..static const F
2c82a 75 6e 63 44 65 66 20 73 74 61 74 33 47 65 74 46  uncDef stat3GetF
2c82b 75 6e 63 64 65 66 20 3d 20 7b 0d 0a 20 20 2d 31  uncdef = {..  -1
2c82c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2c82d 2f 2a 20 6e 41 72 67 20 2a 2f 0d 0a 20 20 53 51  /* nArg */..  SQ
2c82e 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20  LITE_UTF8,      
2c82f 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0d 0a  /* iPrefEnc */..
2c830 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
2c831 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0d      /* flags */.
2c832 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
2c833 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74       /* pUserDat
2c834 61 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20  a */..  0,      
2c835 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
2c836 78 74 20 2a 2f 0d 0a 20 20 73 74 61 74 33 47 65  xt */..  stat3Ge
2c837 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46  t,         /* xF
2c838 75 6e 63 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20  unc */..  0,    
2c839 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
2c83a 53 74 65 70 20 2a 2f 0d 0a 20 20 30 2c 20 20 20  Step */..  0,   
2c83b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c83c 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0d 0a 20 20  xFinalize */..  
2c83d 22 73 74 61 74 33 5f 67 65 74 22 2c 20 20 20 20  "stat3_get",    
2c83e 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0d 0a 20 20   /* zName */..  
2c83f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
2c840 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0d 0a 20    /* pHash */.. 
2c841 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
2c842 20 20 20 2f 2a 20 70 44 65 73 74 72 75 63 74 6f     /* pDestructo
2c843 72 20 2a 2f 0d 0a 7d 3b 0d 0a 23 65 6e 64 69 66  r */..};..#endif
2c844 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
2c845 45 5f 53 54 41 54 33 20 2a 2f 0d 0a 0d 0a 0d 0a  E_STAT3 */......
2c846 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  ..../*..** Gener
2c847 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 61  ate code to do a
2c848 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6c  n analysis of al
2c849 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
2c84a 61 74 65 64 20 77 69 74 68 0d 0a 2a 2a 20 61 20  ated with..** a 
2c84b 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0d 0a 2a  single table...*
2c84c 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  /..static void a
2c84d 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 0d  nalyzeOneTable(.
2c84e 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2c84f 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
2c850 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c  ntext */..  Tabl
2c851 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20  e *pTab,     /* 
2c852 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69  Table whose indi
2c853 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e  ces are to be an
2c854 61 6c 79 7a 65 64 20 2a 2f 0d 0a 20 20 49 6e 64  alyzed */..  Ind
2c855 65 78 20 2a 70 4f 6e 6c 79 49 64 78 2c 20 2f 2a  ex *pOnlyIdx, /*
2c856 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 6f 6e   If not NULL, on
2c857 6c 79 20 61 6e 61 6c 79 7a 65 20 74 68 69 73 20  ly analyze this 
2c858 6f 6e 65 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  one index */..  
2c859 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20 20  int iStatCur,   
2c85a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62   /* Index of Vdb
2c85b 65 43 75 72 73 6f 72 20 74 68 61 74 20 77 72 69  eCursor that wri
2c85c 74 65 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73  tes the sqlite_s
2c85d 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  tat1 table */.. 
2c85e 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20 20   int iMem       
2c85f 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d    /* Available m
2c860 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20  emory locations 
2c861 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0d 0a 29  begin here */..)
2c862 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
2c863 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
2c864 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2c865 6e 64 6c 65 20 2a 2f 0d 0a 20 20 49 6e 64 65 78  ndle */..  Index
2c866 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2c867 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
2c868 64 65 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61  dex to being ana
2c869 6c 79 7a 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  lyzed */..  int 
2c86a 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20  iIdxCur;        
2c86b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
2c86c 6f 72 20 6f 70 65 6e 20 6f 6e 20 69 6e 64 65 78  or open on index
2c86d 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20   being analyzed 
2c86e 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76 3b 20 20  */..  Vdbe *v;  
2c86f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c870 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2c871 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20  l machine being 
2c872 62 75 69 6c 74 20 75 70 20 2a 2f 0d 0a 20 20 69  built up */..  i
2c873 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2c874 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2c875 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a  oop counter */..
2c876 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70 3b    int topOfLoop;
2c877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c878 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
2c879 20 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20   loop */..  int 
2c87a 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20  endOfLoop;      
2c87b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c87c 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  end of the loop 
2c87d 2a 2f 0d 0a 20 20 69 6e 74 20 6a 5a 65 72 6f 52  */..  int jZeroR
2c87e 6f 77 73 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  ows = -1;       
2c87f 20 20 20 2f 2a 20 4a 75 6d 70 20 66 72 6f 6d 20     /* Jump from 
2c880 68 65 72 65 20 69 66 20 6e 75 6d 62 65 72 20 6f  here if number o
2c881 66 20 72 6f 77 73 20 69 73 20 7a 65 72 6f 20 2a  f rows is zero *
2c882 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  /..  int iDb;   
2c883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c884 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
2c885 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2c886 67 20 70 54 61 62 20 2a 2f 0d 0a 20 20 69 6e 74  g pTab */..  int
2c887 20 72 65 67 54 61 62 6e 61 6d 65 20 3d 20 69 4d   regTabname = iM
2c888 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a 20 52 65 67  em++;     /* Reg
2c889 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
2c88a 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0d 0a   table name */..
2c88b 20 20 69 6e 74 20 72 65 67 49 64 78 6e 61 6d 65    int regIdxname
2c88c 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f   = iMem++;     /
2c88d 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
2c88e 69 6e 69 6e 67 20 69 6e 64 65 78 20 6e 61 6d 65  ining index name
2c88f 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 53 74   */..  int regSt
2c890 61 74 31 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  at1 = iMem++;   
2c891 20 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 20      /* The stat 
2c892 63 6f 6c 75 6d 6e 20 6f 66 20 73 71 6c 69 74 65  column of sqlite
2c893 5f 73 74 61 74 31 20 2a 2f 0d 0a 23 69 66 64 65  _stat1 */..#ifde
2c894 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2c895 53 54 41 54 33 0d 0a 20 20 69 6e 74 20 72 65 67  STAT3..  int reg
2c896 4e 75 6d 45 71 20 3d 20 72 65 67 53 74 61 74 31  NumEq = regStat1
2c897 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
2c898 6f 66 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 53  of instances.  S
2c899 61 6d 65 20 61 73 20 72 65 67 53 74 61 74 31 20  ame as regStat1 
2c89a 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 4e 75 6d  */..  int regNum
2c89b 4c 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  Lt = iMem++;    
2c89c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2c89d 6b 65 79 73 20 6c 65 73 73 20 74 68 61 6e 20 72  keys less than r
2c89e 65 67 53 61 6d 70 6c 65 20 2a 2f 0d 0a 20 20 69  egSample */..  i
2c89f 6e 74 20 72 65 67 4e 75 6d 44 4c 74 20 3d 20 69  nt regNumDLt = i
2c8a0 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 2f 2a 20 4e  Mem++;      /* N
2c8a1 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63  umber of distinc
2c8a2 74 20 6b 65 79 73 20 6c 65 73 73 20 74 68 61 6e  t keys less than
2c8a3 20 72 65 67 53 61 6d 70 6c 65 20 2a 2f 0d 0a 20   regSample */.. 
2c8a4 20 69 6e 74 20 72 65 67 53 61 6d 70 6c 65 20 3d   int regSample =
2c8a5 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 2f 2a   iMem++;      /*
2c8a6 20 54 68 65 20 6e 65 78 74 20 73 61 6d 70 6c 65   The next sample
2c8a7 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 69 6e 74   value */..  int
2c8a8 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 53   regRowid = regS
2c8a9 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 52 6f 77  ample;    /* Row
2c8aa 69 64 20 6f 66 20 61 20 73 61 6d 70 6c 65 20 2a  id of a sample *
2c8ab 2f 0d 0a 20 20 69 6e 74 20 72 65 67 41 63 63 75  /..  int regAccu
2c8ac 6d 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20  m = iMem++;     
2c8ad 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
2c8ae 20 68 6f 6c 64 20 53 74 61 74 33 41 63 63 75 6d   hold Stat3Accum
2c8af 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e   object */..  in
2c8b0 74 20 72 65 67 4c 6f 6f 70 20 3d 20 69 4d 65 6d  t regLoop = iMem
2c8b1 2b 2b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ++;        /* Lo
2c8b2 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
2c8b3 20 69 6e 74 20 72 65 67 43 6f 75 6e 74 20 3d 20   int regCount = 
2c8b4 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  iMem++;       /*
2c8b5 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   Number of rows 
2c8b6 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  in the table or 
2c8b7 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20  index */..  int 
2c8b8 72 65 67 54 65 6d 70 31 20 3d 20 69 4d 65 6d 2b  regTemp1 = iMem+
2c8b9 2b 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65  +;       /* Inte
2c8ba 72 6d 65 64 69 61 74 65 20 72 65 67 69 73 74 65  rmediate registe
2c8bb 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 54  r */..  int regT
2c8bc 65 6d 70 32 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  emp2 = iMem++;  
2c8bd 20 20 20 20 20 2f 2a 20 49 6e 74 65 72 6d 65 64       /* Intermed
2c8be 69 61 74 65 20 72 65 67 69 73 74 65 72 20 2a 2f  iate register */
2c8bf 0d 0a 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20 31  ..  int once = 1
2c8c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c8c1 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 69 6e 69   /* One-time ini
2c8c2 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0d 0a  tialization */..
2c8c3 20 20 69 6e 74 20 73 68 6f 72 74 4a 75 6d 70 20    int shortJump 
2c8c4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
2c8c5 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 61 64  * Instruction ad
2c8c6 64 72 65 73 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  dress */..  int 
2c8c7 69 54 61 62 43 75 72 20 3d 20 70 50 61 72 73 65  iTabCur = pParse
2c8c8 2d 3e 6e 54 61 62 2b 2b 3b 20 2f 2a 20 54 61 62  ->nTab++; /* Tab
2c8c9 6c 65 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 23 65  le cursor */..#e
2c8ca 6e 64 69 66 0d 0a 20 20 69 6e 74 20 72 65 67 43  ndif..  int regC
2c8cb 6f 6c 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  ol = iMem++;    
2c8cc 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
2c8cd 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  of a column in a
2c8ce 6e 61 6c 79 7a 65 64 20 74 61 62 6c 65 20 2a 2f  nalyzed table */
2c8cf 0d 0a 20 20 69 6e 74 20 72 65 67 52 65 63 20 3d  ..  int regRec =
2c8d0 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20   iMem++;        
2c8d1 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2c8d2 64 69 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72  ding completed r
2c8d3 65 63 6f 72 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  ecord */..  int 
2c8d4 72 65 67 54 65 6d 70 20 3d 20 69 4d 65 6d 2b 2b  regTemp = iMem++
2c8d5 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  ;        /* Temp
2c8d6 6f 72 61 72 79 20 75 73 65 20 72 65 67 69 73 74  orary use regist
2c8d7 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  er */..  int reg
2c8d8 4e 65 77 52 6f 77 69 64 20 3d 20 69 4d 65 6d 2b  NewRowid = iMem+
2c8d9 2b 3b 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66  +;    /* Rowid f
2c8da 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  or the inserted 
2c8db 72 65 63 6f 72 64 20 2a 2f 0d 0a 0d 0a 0d 0a 20  record */...... 
2c8dc 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2c8dd 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  dbe(pParse);..  
2c8de 69 66 28 20 76 3d 3d 30 20 7c 7c 20 4e 45 56 45  if( v==0 || NEVE
2c8df 52 28 70 54 61 62 3d 3d 30 29 20 29 7b 0d 0a 20  R(pTab==0) ){.. 
2c8e0 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d     return;..  }.
2c8e1 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75  .  if( pTab->tnu
2c8e2 6d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  m==0 ){..    /* 
2c8e3 44 6f 20 6e 6f 74 20 67 61 74 68 65 72 20 73 74  Do not gather st
2c8e4 61 74 69 73 74 69 63 73 20 6f 6e 20 76 69 65 77  atistics on view
2c8e5 73 20 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62  s or virtual tab
2c8e6 6c 65 73 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75  les */..    retu
2c8e7 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  rn;..  }..  if( 
2c8e8 6d 65 6d 63 6d 70 28 70 54 61 62 2d 3e 7a 4e 61  memcmp(pTab->zNa
2c8e9 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
2c8ea 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  )==0 ){..    /* 
2c8eb 44 6f 20 6e 6f 74 20 67 61 74 68 65 72 20 73 74  Do not gather st
2c8ec 61 74 69 73 74 69 63 73 20 6f 6e 20 73 79 73 74  atistics on syst
2c8ed 65 6d 20 74 61 62 6c 65 73 20 2a 2f 0d 0a 20 20  em tables */..  
2c8ee 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
2c8ef 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2c8f0 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
2c8f1 74 65 78 65 73 28 64 62 29 20 29 3b 0d 0a 20 20  texes(db) );..  
2c8f2 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2c8f3 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
2c8f4 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a  Tab->pSchema);..
2c8f5 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
2c8f6 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
2c8f7 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65  qlite3SchemaMute
2c8f8 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30  xHeld(db, iDb, 0
2c8f9 29 20 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  ) );..#ifndef SQ
2c8fa 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2c8fb 49 5a 41 54 49 4f 4e 0d 0a 20 20 69 66 28 20 73  IZATION..  if( s
2c8fc 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2c8fd 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
2c8fe 4e 41 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e  NALYZE, pTab->zN
2c8ff 61 6d 65 2c 20 30 2c 0d 0a 20 20 20 20 20 20 64  ame, 0,..      d
2c900 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2c901 65 20 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  e ) ){..    retu
2c902 72 6e 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  rn;..  }..#endif
2c903 0d 0a 0d 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69  ....  /* Establi
2c904 73 68 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  sh a read-lock o
2c905 6e 20 74 68 65 20 74 61 62 6c 65 20 61 74 20 74  n the table at t
2c906 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
2c907 6c 65 76 65 6c 2e 20 2a 2f 0d 0a 20 20 73 71 6c  level. */..  sql
2c908 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
2c909 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
2c90a 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e  >tnum, 0, pTab->
2c90b 7a 4e 61 6d 65 29 3b 0d 0a 0d 0a 20 20 69 49 64  zName);....  iId
2c90c 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  xCur = pParse->n
2c90d 54 61 62 2b 2b 3b 0d 0a 20 20 73 71 6c 69 74 65  Tab++;..  sqlite
2c90e 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2c90f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65  P_String8, 0, re
2c910 67 54 61 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61  gTabname, 0, pTa
2c911 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0d 0a 20  b->zName, 0);.. 
2c912 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
2c913 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
2c914 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
2c915 0d 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0d  ..    int nCol;.
2c916 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
2c917 65 79 3b 0d 0a 20 20 20 20 69 6e 74 20 61 64 64  ey;..    int add
2c918 72 49 66 4e 6f 74 20 3d 20 30 3b 20 20 20 20 20  rIfNot = 0;     
2c919 20 20 20 20 20 20 2f 2a 20 61 64 64 72 65 73 73        /* address
2c91a 20 6f 66 20 4f 50 5f 49 66 4e 6f 74 20 2a 2f 0d   of OP_IfNot */.
2c91b 0a 20 20 20 20 69 6e 74 20 2a 61 43 68 6e 67 41  .    int *aChngA
2c91c 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ddr;            
2c91d 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6a 75    /* Array of ju
2c91e 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  mp instruction a
2c91f 64 64 72 65 73 73 65 73 20 2a 2f 0d 0a 0d 0a 20  ddresses */.... 
2c920 20 20 20 69 66 28 20 70 4f 6e 6c 79 49 64 78 20     if( pOnlyIdx 
2c921 26 26 20 70 4f 6e 6c 79 49 64 78 21 3d 70 49 64  && pOnlyIdx!=pId
2c922 78 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20  x ) continue;.. 
2c923 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
2c924 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20 61 6e  nt((v, "Begin an
2c925 61 6c 79 73 69 73 20 6f 66 20 25 73 22 2c 20 70  alysis of %s", p
2c926 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 20  Idx->zName));.. 
2c927 20 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e     nCol = pIdx->
2c928 6e 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 61 43  nColumn;..    aC
2c929 68 6e 67 41 64 64 72 20 3d 20 73 71 6c 69 74 65  hngAddr = sqlite
2c92a 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2c92b 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f   sizeof(int)*nCo
2c92c 6c 29 3b 0d 0a 20 20 20 20 69 66 28 20 61 43 68  l);..    if( aCh
2c92d 6e 67 41 64 64 72 3d 3d 30 20 29 20 63 6f 6e 74  ngAddr==0 ) cont
2c92e 69 6e 75 65 3b 0d 0a 20 20 20 20 70 4b 65 79 20  inue;..    pKey 
2c92f 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
2c930 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
2c931 64 78 29 3b 0d 0a 20 20 20 20 69 66 28 20 69 4d  dx);..    if( iM
2c932 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70 50  em+1+(nCol*2)>pP
2c933 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0d 0a 20  arse->nMem ){.. 
2c934 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
2c935 6d 20 3d 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c  m = iMem+1+(nCol
2c936 2a 32 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  *2);..    }.... 
2c937 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72     /* Open a cur
2c938 73 6f 72 20 74 6f 20 74 68 65 20 69 6e 64 65 78  sor to the index
2c939 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
2c93a 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
2c93b 20 69 44 62 3d 3d 73 71 6c 69 74 65 33 53 63 68   iDb==sqlite3Sch
2c93c 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
2c93d 49 64 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b  Idx->pSchema) );
2c93e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2c93f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
2c940 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c  enRead, iIdxCur,
2c941 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
2c942 2c 0d 0a 20 20 20 20 20 20 20 20 28 63 68 61 72  ,..        (char
2c943 20 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49   *)pKey, P4_KEYI
2c944 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0d 0a 20  NFO_HANDOFF);.. 
2c945 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2c946 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a  v, "%s", pIdx->z
2c947 4e 61 6d 65 29 29 3b 0d 0a 0d 0a 20 20 20 20 2f  Name));....    /
2c948 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 72  * Populate the r
2c949 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
2c94a 6e 67 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d  ng the index nam
2c94b 65 2e 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74  e. */..    sqlit
2c94c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2c94d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
2c94e 65 67 49 64 78 6e 61 6d 65 2c 20 30 2c 20 70 49  egIdxname, 0, pI
2c94f 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0d 0a  dx->zName, 0);..
2c950 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2c951 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20 20  ENABLE_STAT3..  
2c952 20 20 69 66 28 20 6f 6e 63 65 20 29 7b 0d 0a 20    if( once ){.. 
2c953 20 20 20 20 20 6f 6e 63 65 20 3d 20 30 3b 0d 0a       once = 0;..
2c954 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
2c955 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
2c956 54 61 62 43 75 72 2c 20 69 44 62 2c 20 70 54 61  TabCur, iDb, pTa
2c957 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
2c958 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
2c959 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c95a 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64 78  , OP_Count, iIdx
2c95b 43 75 72 2c 20 72 65 67 43 6f 75 6e 74 29 3b 0d  Cur, regCount);.
2c95c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c95d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2c95e 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 53 54 41  eger, SQLITE_STA
2c95f 54 33 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 54  T3_SAMPLES, regT
2c960 65 6d 70 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69  emp1);..    sqli
2c961 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c962 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2c963 72 65 67 4e 75 6d 45 71 29 3b 0d 0a 20 20 20 20  regNumEq);..    
2c964 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c965 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2c966 20 30 2c 20 72 65 67 4e 75 6d 4c 74 29 3b 0d 0a   0, regNumLt);..
2c967 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c968 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2c969 67 65 72 2c 20 2d 31 2c 20 72 65 67 4e 75 6d 44  ger, -1, regNumD
2c96a 4c 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  Lt);..    sqlite
2c96b 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c96c 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 53 61  P_Null, 0, regSa
2c96d 6d 70 6c 65 2c 20 72 65 67 41 63 63 75 6d 29 3b  mple, regAccum);
2c96e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2c96f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75  eAddOp4(v, OP_Fu
2c970 6e 63 74 69 6f 6e 2c 20 31 2c 20 72 65 67 43 6f  nction, 1, regCo
2c971 75 6e 74 2c 20 72 65 67 41 63 63 75 6d 2c 0d 0a  unt, regAccum,..
2c972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c973 20 20 20 20 20 20 28 63 68 61 72 2a 29 26 73 74        (char*)&st
2c974 61 74 33 49 6e 69 74 46 75 6e 63 64 65 66 2c 20  at3InitFuncdef, 
2c975 50 34 5f 46 55 4e 43 44 45 46 29 3b 0d 0a 20 20  P4_FUNCDEF);..  
2c976 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2c977 6e 67 65 50 35 28 76 2c 20 32 29 3b 0d 0a 23 65  ngeP5(v, 2);..#e
2c978 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
2c979 4e 41 42 4c 45 5f 53 54 41 54 33 20 2a 2f 0d 0a  NABLE_STAT3 */..
2c97a 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6c 6f  ..    /* The blo
2c97b 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  ck of memory cel
2c97c 6c 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 68  ls initialized h
2c97d 65 72 65 20 69 73 20 75 73 65 64 20 61 73 20 66  ere is used as f
2c97e 6f 6c 6c 6f 77 73 2e 0d 0a 20 20 20 20 2a 2a 0d  ollows...    **.
2c97f 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 3a  .    **    iMem:
2c980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c981 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
2c982 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  The total number
2c983 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20   of rows in the 
2c984 74 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2a 0d 0a  table...    **..
2c985 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 31      **    iMem+1
2c986 20 2e 2e 20 69 4d 65 6d 2b 6e 43 6f 6c 3a 20 0d   .. iMem+nCol: .
2c987 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 4e  .    **        N
2c988 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63  umber of distinc
2c989 74 20 65 6e 74 72 69 65 73 20 69 6e 20 69 6e 64  t entries in ind
2c98a 65 78 20 63 6f 6e 73 69 64 65 72 69 6e 67 20 74  ex considering t
2c98b 68 65 20 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20  he ..    **     
2c98c 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 4e 20 63     left-most N c
2c98d 6f 6c 75 6d 6e 73 20 6f 6e 6c 79 2c 20 77 68 65  olumns only, whe
2c98e 72 65 20 4e 20 69 73 20 62 65 74 77 65 65 6e 20  re N is between 
2c98f 31 20 61 6e 64 20 6e 43 6f 6c 2c 20 0d 0a 20 20  1 and nCol, ..  
2c990 20 20 2a 2a 20 20 20 20 20 20 20 20 69 6e 63 6c    **        incl
2c991 75 73 69 76 65 2e 0d 0a 20 20 20 20 2a 2a 0d 0a  usive...    **..
2c992 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 6e      **    iMem+n
2c993 43 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b 32 2a 6e  Col+1 .. Mem+2*n
2c994 43 6f 6c 3a 20 20 0d 0a 20 20 20 20 2a 2a 20 20  Col:  ..    **  
2c995 20 20 20 20 20 20 50 72 65 76 69 6f 75 73 20 76        Previous v
2c996 61 6c 75 65 20 6f 66 20 69 6e 64 65 78 65 64 20  alue of indexed 
2c997 63 6f 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20 6c 65  columns, from le
2c998 66 74 20 74 6f 20 72 69 67 68 74 2e 0d 0a 20 20  ft to right...  
2c999 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 43 65 6c    **..    ** Cel
2c99a 6c 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20  ls iMem through 
2c99b 69 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e  iMem+nCol are in
2c99c 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20  itialized to 0. 
2c99d 54 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 0d  The others are .
2c99e 0a 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  .    ** initiali
2c99f 7a 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  zed to contain a
2c9a0 6e 20 53 51 4c 20 4e 55 4c 4c 2e 0d 0a 20 20 20  n SQL NULL...   
2c9a1 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   */..    for(i=0
2c9a2 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  ; i<=nCol; i++){
2c9a3 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
2c9a4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c9a5 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 4d 65 6d  Integer, 0, iMem
2c9a6 2b 69 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  +i);..    }..   
2c9a7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
2c9a8 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 73  ; i++){..      s
2c9a9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c9aa 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2c9ab 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0d  iMem+nCol+i+1);.
2c9ac 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
2c9ad 20 53 74 61 72 74 20 74 68 65 20 61 6e 61 6c 79   Start the analy
2c9ae 73 69 73 20 6c 6f 6f 70 2e 20 54 68 69 73 20 6c  sis loop. This l
2c9af 6f 6f 70 20 72 75 6e 73 20 74 68 72 6f 75 67 68  oop runs through
2c9b0 20 61 6c 6c 20 74 68 65 20 65 6e 74 72 69 65 73   all the entries
2c9b1 20 69 6e 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20   in..    ** the 
2c9b2 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 20 2a  index b-tree.  *
2c9b3 2f 0d 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70  /..    endOfLoop
2c9b4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2c9b5 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20 20  keLabel(v);..   
2c9b6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c9b7 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
2c9b8 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c   iIdxCur, endOfL
2c9b9 6f 6f 70 29 3b 0d 0a 20 20 20 20 74 6f 70 4f 66  oop);..    topOf
2c9ba 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  Loop = sqlite3Vd
2c9bb 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c9bc 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2c9bd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2c9be 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b  ddImm, iMem, 1);
2c9bf 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 72    /* Increment r
2c9c0 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d  ow counter */...
2c9c1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2c9c2 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  nCol; i++){..   
2c9c3 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2c9c4 6c 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l;..      sqlite
2c9c5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c9c6 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75  P_Column, iIdxCu
2c9c7 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0d 0a  r, i, regCol);..
2c9c8 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29        if( i==0 )
2c9c9 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6c  {..        /* Al
2c9ca 77 61 79 73 20 72 65 63 6f 72 64 20 74 68 65 20  ways record the 
2c9cb 76 65 72 79 20 66 69 72 73 74 20 72 6f 77 20 2a  very first row *
2c9cc 2f 0d 0a 20 20 20 20 20 20 20 20 61 64 64 72 49  /..        addrI
2c9cd 66 4e 6f 74 20 3d 20 73 71 6c 69 74 65 33 56 64  fNot = sqlite3Vd
2c9ce 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2c9cf 66 4e 6f 74 2c 20 69 4d 65 6d 2b 31 29 3b 0d 0a  fNot, iMem+1);..
2c9d0 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61        }..      a
2c9d1 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 7a 43  ssert( pIdx->azC
2c9d2 6f 6c 6c 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20  oll!=0 );..     
2c9d3 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61   assert( pIdx->a
2c9d4 7a 43 6f 6c 6c 5b 69 5d 21 3d 30 20 29 3b 0d 0a  zColl[i]!=0 );..
2c9d5 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
2c9d6 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
2c9d7 65 71 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d  eq(pParse, pIdx-
2c9d8 3e 61 7a 43 6f 6c 6c 5b 69 5d 29 3b 0d 0a 20 20  >azColl[i]);..  
2c9d9 20 20 20 20 61 43 68 6e 67 41 64 64 72 5b 69 5d      aChngAddr[i]
2c9da 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2c9db 64 4f 70 34 28 76 2c 20 4f 50 5f 4e 65 2c 20 72  dOp4(v, OP_Ne, r
2c9dc 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e  egCol, 0, iMem+n
2c9dd 43 6f 6c 2b 69 2b 31 2c 0d 0a 20 20 20 20 20 20  Col+i+1,..      
2c9de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9e0 28 63 68 61 72 2a 29 70 43 6f 6c 6c 2c 20 50 34  (char*)pColl, P4
2c9e1 5f 43 4f 4c 4c 53 45 51 29 3b 0d 0a 20 20 20 20  _COLLSEQ);..    
2c9e2 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2c9e3 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
2c9e4 4e 55 4c 4c 45 51 29 3b 0d 0a 20 20 20 20 20 20  NULLEQ);..      
2c9e5 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2c9e6 22 6a 75 6d 70 20 69 66 20 63 6f 6c 75 6d 6e 20  "jump if column 
2c9e7 25 64 20 63 68 61 6e 67 65 64 22 2c 20 69 29 29  %d changed", i))
2c9e8 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
2c9e9 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20  _ENABLE_STAT3.. 
2c9ea 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b       if( i==0 ){
2c9eb 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2c9ec 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c9ed 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 4e 75 6d  P_AddImm, regNum
2c9ee 45 71 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 20  Eq, 1);..       
2c9ef 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c9f0 20 22 69 6e 63 72 20 72 65 70 65 61 74 20 63 6f   "incr repeat co
2c9f1 75 6e 74 22 29 29 3b 0d 0a 20 20 20 20 20 20 7d  unt"));..      }
2c9f2 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 7d 0d  ..#endif..    }.
2c9f3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c9f4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2c9f5 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29  o, 0, endOfLoop)
2c9f6 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
2c9f7 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20  i<nCol; i++){.. 
2c9f8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c9f9 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 43 68 6e  JumpHere(v, aChn
2c9fa 67 41 64 64 72 5b 69 5d 29 3b 20 20 2f 2a 20 53  gAddr[i]);  /* S
2c9fb 65 74 20 6a 75 6d 70 20 64 65 73 74 20 66 6f 72  et jump dest for
2c9fc 20 74 68 65 20 4f 50 5f 4e 65 20 2a 2f 0d 0a 20   the OP_Ne */.. 
2c9fd 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b       if( i==0 ){
2c9fe 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2c9ff 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2ca00 20 61 64 64 72 49 66 4e 6f 74 29 3b 20 20 20 2f   addrIfNot);   /
2ca01 2a 20 4a 75 6d 70 20 64 65 73 74 20 66 6f 72 20  * Jump dest for 
2ca02 4f 50 5f 49 66 4e 6f 74 20 2a 2f 0d 0a 23 69 66  OP_IfNot */..#if
2ca03 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2ca04 45 5f 53 54 41 54 33 0d 0a 20 20 20 20 20 20 20  E_STAT3..       
2ca05 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ca06 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f  p4(v, OP_Functio
2ca07 6e 2c 20 31 2c 20 72 65 67 4e 75 6d 45 71 2c 20  n, 1, regNumEq, 
2ca08 72 65 67 54 65 6d 70 32 2c 0d 0a 20 20 20 20 20  regTemp2,..     
2ca09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca0a 20 20 20 20 20 28 63 68 61 72 2a 29 26 73 74 61       (char*)&sta
2ca0b 74 33 50 75 73 68 46 75 6e 63 64 65 66 2c 20 50  t3PushFuncdef, P
2ca0c 34 5f 46 55 4e 43 44 45 46 29 3b 0d 0a 20 20 20  4_FUNCDEF);..   
2ca0d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ca0e 43 68 61 6e 67 65 50 35 28 76 2c 20 35 29 3b 0d  ChangeP5(v, 5);.
2ca0f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ca10 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2ca11 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72  _Column, iIdxCur
2ca12 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2c  , pIdx->nColumn,
2ca13 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20   regRowid);..   
2ca14 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ca15 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64  AddOp3(v, OP_Add
2ca16 2c 20 72 65 67 4e 75 6d 45 71 2c 20 72 65 67 4e  , regNumEq, regN
2ca17 75 6d 4c 74 2c 20 72 65 67 4e 75 6d 4c 74 29 3b  umLt, regNumLt);
2ca18 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2ca19 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ca1a 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 4e 75 6d  P_AddImm, regNum
2ca1b 44 4c 74 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20  DLt, 1);..      
2ca1c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ca1d 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2ca1e 72 2c 20 31 2c 20 72 65 67 4e 75 6d 45 71 29 3b  r, 1, regNumEq);
2ca1f 0d 0a 23 65 6e 64 69 66 20 20 20 20 20 20 20 20  ..#endif        
2ca20 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
2ca21 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ca22 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2ca23 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b 0d 0a   iMem+i+1, 1);..
2ca24 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ca25 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2ca26 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69  lumn, iIdxCur, i
2ca27 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29  , iMem+nCol+i+1)
2ca28 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71  ;..    }..    sq
2ca29 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ca2a 61 43 68 6e 67 41 64 64 72 29 3b 0d 0a 0d 0a 20  aChngAddr);.... 
2ca2b 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 6a 75 6d     /* Always jum
2ca2c 70 20 68 65 72 65 20 61 66 74 65 72 20 75 70 64  p here after upd
2ca2d 61 74 69 6e 67 20 74 68 65 20 69 4d 65 6d 2b 31  ating the iMem+1
2ca2e 2e 2e 2e 69 4d 65 6d 2b 31 2b 6e 43 6f 6c 20 63  ...iMem+1+nCol c
2ca2f 6f 75 6e 74 65 72 73 20 2a 2f 0d 0a 20 20 20 20  ounters */..    
2ca30 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2ca31 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66  veLabel(v, endOf
2ca32 4c 6f 6f 70 29 3b 0d 0a 0d 0a 20 20 20 20 73 71  Loop);....    sq
2ca33 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ca34 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49 64 78  v, OP_Next, iIdx
2ca35 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70 29 3b  Cur, topOfLoop);
2ca36 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2ca37 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
2ca38 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b 0d 0a  ose, iIdxCur);..
2ca39 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
2ca3a 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20 20 20 20  ABLE_STAT3..    
2ca3b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ca3c 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  4(v, OP_Function
2ca3d 2c 20 31 2c 20 72 65 67 4e 75 6d 45 71 2c 20 72  , 1, regNumEq, r
2ca3e 65 67 54 65 6d 70 32 2c 0d 0a 20 20 20 20 20 20  egTemp2,..      
2ca3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca40 28 63 68 61 72 2a 29 26 73 74 61 74 33 50 75 73  (char*)&stat3Pus
2ca41 68 46 75 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e  hFuncdef, P4_FUN
2ca42 43 44 45 46 29 3b 0d 0a 20 20 20 20 73 71 6c 69  CDEF);..    sqli
2ca43 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2ca44 76 2c 20 35 29 3b 0d 0a 20 20 20 20 73 71 6c 69  v, 5);..    sqli
2ca45 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ca46 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
2ca47 20 72 65 67 4c 6f 6f 70 29 3b 0d 0a 20 20 20 20   regLoop);..    
2ca48 73 68 6f 72 74 4a 75 6d 70 20 3d 20 0d 0a 20 20  shortJump = ..  
2ca49 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ca4a 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
2ca4b 2c 20 72 65 67 4c 6f 6f 70 2c 20 31 29 3b 0d 0a  , regLoop, 1);..
2ca4c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ca4d 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63  ddOp4(v, OP_Func
2ca4e 74 69 6f 6e 2c 20 31 2c 20 72 65 67 41 63 63 75  tion, 1, regAccu
2ca4f 6d 2c 20 72 65 67 54 65 6d 70 31 2c 0d 0a 20 20  m, regTemp1,..  
2ca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca51 20 20 20 20 28 63 68 61 72 2a 29 26 73 74 61 74      (char*)&stat
2ca52 33 47 65 74 46 75 6e 63 64 65 66 2c 20 50 34 5f  3GetFuncdef, P4_
2ca53 46 55 4e 43 44 45 46 29 3b 0d 0a 20 20 20 20 73  FUNCDEF);..    s
2ca54 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2ca55 50 35 28 76 2c 20 32 29 3b 0d 0a 20 20 20 20 73  P5(v, 2);..    s
2ca56 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2ca57 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2ca58 65 67 54 65 6d 70 31 29 3b 0d 0a 20 20 20 20 73  egTemp1);..    s
2ca59 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2ca5a 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2ca5b 2c 20 69 54 61 62 43 75 72 2c 20 73 68 6f 72 74  , iTabCur, short
2ca5c 4a 75 6d 70 2c 20 72 65 67 54 65 6d 70 31 29 3b  Jump, regTemp1);
2ca5d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2ca5e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2ca5f 6c 75 6d 6e 2c 20 69 54 61 62 43 75 72 2c 20 70  lumn, iTabCur, p
2ca60 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d  Idx->aiColumn[0]
2ca61 2c 20 72 65 67 53 61 6d 70 6c 65 29 3b 0d 0a 20  , regSample);.. 
2ca62 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
2ca63 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
2ca64 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2ca65 30 5d 2c 20 72 65 67 53 61 6d 70 6c 65 29 3b 0d  0], regSample);.
2ca66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2ca67 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e  AddOp4(v, OP_Fun
2ca68 63 74 69 6f 6e 2c 20 31 2c 20 72 65 67 41 63 63  ction, 1, regAcc
2ca69 75 6d 2c 20 72 65 67 4e 75 6d 45 71 2c 0d 0a 20  um, regNumEq,.. 
2ca6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca6b 20 20 20 20 20 28 63 68 61 72 2a 29 26 73 74 61       (char*)&sta
2ca6c 74 33 47 65 74 46 75 6e 63 64 65 66 2c 20 50 34  t3GetFuncdef, P4
2ca6d 5f 46 55 4e 43 44 45 46 29 3b 0d 0a 20 20 20 20  _FUNCDEF);..    
2ca6e 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2ca6f 65 50 35 28 76 2c 20 33 29 3b 0d 0a 20 20 20 20  eP5(v, 3);..    
2ca70 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ca71 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  4(v, OP_Function
2ca72 2c 20 31 2c 20 72 65 67 41 63 63 75 6d 2c 20 72  , 1, regAccum, r
2ca73 65 67 4e 75 6d 4c 74 2c 0d 0a 20 20 20 20 20 20  egNumLt,..      
2ca74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca75 28 63 68 61 72 2a 29 26 73 74 61 74 33 47 65 74  (char*)&stat3Get
2ca76 46 75 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e 43  Funcdef, P4_FUNC
2ca77 44 45 46 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  DEF);..    sqlit
2ca78 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2ca79 2c 20 34 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  , 4);..    sqlit
2ca7a 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2ca7b 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 31 2c 20  OP_Function, 1, 
2ca7c 72 65 67 41 63 63 75 6d 2c 20 72 65 67 4e 75 6d  regAccum, regNum
2ca7d 44 4c 74 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  DLt,..          
2ca7e 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
2ca7f 72 2a 29 26 73 74 61 74 33 47 65 74 46 75 6e 63  r*)&stat3GetFunc
2ca80 64 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29  def, P4_FUNCDEF)
2ca81 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2ca82 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 35 29  beChangeP5(v, 5)
2ca83 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2ca84 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
2ca85 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61  akeRecord, regTa
2ca86 62 6e 61 6d 65 2c 20 36 2c 20 72 65 67 52 65 63  bname, 6, regRec
2ca87 2c 20 22 62 62 62 62 62 62 22 2c 20 30 29 3b 0d  , "bbbbbb", 0);.
2ca88 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2ca89 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2ca8a 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2b  Rowid, iStatCur+
2ca8b 31 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b  1, regNewRowid);
2ca8c 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2ca8d 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
2ca8e 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2b 31  sert, iStatCur+1
2ca8f 2c 20 72 65 67 52 65 63 2c 20 72 65 67 4e 65 77  , regRec, regNew
2ca90 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 73 71 6c  Rowid);..    sql
2ca91 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ca92 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 73 68  , OP_Goto, 0, sh
2ca93 6f 72 74 4a 75 6d 70 29 3b 0d 0a 20 20 20 20 73  ortJump);..    s
2ca94 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2ca95 72 65 28 76 2c 20 73 68 6f 72 74 4a 75 6d 70 2b  re(v, shortJump+
2ca96 32 29 3b 0d 0a 23 65 6e 64 69 66 20 20 20 20 20  2);..#endif     
2ca97 20 20 20 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 74     ....    /* St
2ca98 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
2ca99 69 6e 20 73 71 6c 69 74 65 5f 73 74 61 74 31 2e  in sqlite_stat1.
2ca9a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
2ca9b 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
2ca9c 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74   single row of t
2ca9d 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
2ca9e 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69 72 73  table.  The firs
2ca9f 74 0d 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 6f  t..    ** two co
2caa0 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e 61  lumns are the na
2caa1 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  mes of the table
2caa2 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 65   and index.  The
2caa3 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0d 0a 20   third column.. 
2caa4 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e     ** is a strin
2caa5 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20  g composed of a 
2caa6 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20  list of integer 
2caa7 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74  statistics about
2caa8 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 69 6e 64   the..    ** ind
2caa9 65 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69  ex.  The first i
2caaa 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69  nteger in the li
2caab 73 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  st is the total 
2caac 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
2caad 73 0d 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  s..    ** in the
2caae 20 69 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69   index.  There i
2caaf 73 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c  s one additional
2cab0 20 69 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20   integer in the 
2cab1 6c 69 73 74 20 66 6f 72 20 65 61 63 68 0d 0a 20  list for each.. 
2cab2 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20     ** column of 
2cab3 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73  the table.  This
2cab4 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65   additional inte
2cab5 67 65 72 20 69 73 20 61 20 67 75 65 73 73 20 6f  ger is a guess o
2cab6 66 20 68 6f 77 20 6d 61 6e 79 0d 0a 20 20 20 20  f how many..    
2cab7 2a 2a 20 72 6f 77 73 20 6f 66 20 74 68 65 20 74  ** rows of the t
2cab8 61 62 6c 65 20 74 68 65 20 69 6e 64 65 78 20 77  able the index w
2cab9 69 6c 6c 20 73 65 6c 65 63 74 2e 20 20 49 66 20  ill select.  If 
2caba 44 20 69 73 20 74 68 65 20 63 6f 75 6e 74 20 6f  D is the count o
2cabb 66 20 64 69 73 74 69 6e 63 74 0d 0a 20 20 20 20  f distinct..    
2cabc 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 4b 20  ** values and K 
2cabd 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
2cabe 62 65 72 20 6f 66 20 72 6f 77 73 2c 20 74 68 65  ber of rows, the
2cabf 6e 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73  n the integer is
2cac0 20 63 6f 6d 70 75 74 65 64 0d 0a 20 20 20 20 2a   computed..    *
2cac1 2a 20 61 73 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20  * as:..    **.. 
2cac2 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 3d     **        I =
2cac3 20 28 4b 2b 44 2d 31 29 2f 44 0d 0a 20 20 20 20   (K+D-1)/D..    
2cac4 2a 2a 0d 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d  **..    ** If K=
2cac5 3d 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79  =0 then no entry
2cac6 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68   is made into th
2cac7 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
2cac8 61 62 6c 65 2e 20 20 0d 0a 20 20 20 20 2a 2a 20  able.  ..    ** 
2cac9 49 66 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69  If K>0 then it i
2caca 73 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73  s always the cas
2cacb 65 20 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76  e the D>0 so div
2cacc 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 0d 0a 20  ision by zero.. 
2cacd 20 20 20 2a 2a 20 69 73 20 6e 65 76 65 72 20 70     ** is never p
2cace 6f 73 73 69 62 6c 65 2e 0d 0a 20 20 20 20 2a 2f  ossible...    */
2cacf 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2cad0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2cad1 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 65 67 53 74  opy, iMem, regSt
2cad2 61 74 31 29 3b 0d 0a 20 20 20 20 69 66 28 20 6a  at1);..    if( j
2cad3 5a 65 72 6f 52 6f 77 73 3c 30 20 29 7b 0d 0a 20  ZeroRows<0 ){.. 
2cad4 20 20 20 20 20 6a 5a 65 72 6f 52 6f 77 73 20 3d       jZeroRows =
2cad5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2cad6 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  p1(v, OP_IfNot, 
2cad7 69 4d 65 6d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  iMem);..    }.. 
2cad8 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
2cad9 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  ol; i++){..     
2cada 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2cadb 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2cadc 2c 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c  , 0, regTemp, 0,
2cadd 20 22 20 22 2c 20 30 29 3b 0d 0a 20 20 20 20 20   " ", 0);..     
2cade 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2cadf 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c  p3(v, OP_Concat,
2cae0 20 72 65 67 54 65 6d 70 2c 20 72 65 67 53 74 61   regTemp, regSta
2cae1 74 31 2c 20 72 65 67 53 74 61 74 31 29 3b 0d 0a  t1, regStat1);..
2cae2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2cae3 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
2cae4 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b  d, iMem, iMem+i+
2cae5 31 2c 20 72 65 67 54 65 6d 70 29 3b 0d 0a 20 20  1, regTemp);..  
2cae6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2cae7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2cae8 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29  mm, regTemp, -1)
2cae9 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2caea 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2caeb 5f 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b  _Divide, iMem+i+
2caec 31 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54  1, regTemp, regT
2caed 65 6d 70 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  emp);..      sql
2caee 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2caef 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54  , OP_ToInt, regT
2caf0 65 6d 70 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  emp);..      sql
2caf1 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2caf2 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67  , OP_Concat, reg
2caf3 54 65 6d 70 2c 20 72 65 67 53 74 61 74 31 2c 20  Temp, regStat1, 
2caf4 72 65 67 53 74 61 74 31 29 3b 0d 0a 20 20 20 20  regStat1);..    
2caf5 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  }..    sqlite3Vd
2caf6 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
2caf7 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61  akeRecord, regTa
2caf8 62 6e 61 6d 65 2c 20 33 2c 20 72 65 67 52 65 63  bname, 3, regRec
2caf9 2c 20 22 61 61 61 22 2c 20 30 29 3b 0d 0a 20 20  , "aaa", 0);..  
2cafa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2cafb 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
2cafc 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72 65  id, iStatCur, re
2cafd 67 4e 65 77 52 6f 77 69 64 29 3b 0d 0a 20 20 20  gNewRowid);..   
2cafe 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2caff 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2cb00 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 65   iStatCur, regRe
2cb01 63 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b  c, regNewRowid);
2cb02 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2cb03 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
2cb04 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0d 0a 20 20  LAG_APPEND);..  
2cb05 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65  }....  /* If the
2cb06 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 69 6e   table has no in
2cb07 64 69 63 65 73 2c 20 63 72 65 61 74 65 20 61 20  dices, create a 
2cb08 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 5f 73 74  single sqlite_st
2cb09 61 74 31 20 65 6e 74 72 79 0d 0a 20 20 2a 2a 20  at1 entry..  ** 
2cb0a 63 6f 6e 74 61 69 6e 69 6e 67 20 4e 55 4c 4c 20  containing NULL 
2cb0b 61 73 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d  as the index nam
2cb0c 65 20 61 6e 64 20 74 68 65 20 72 6f 77 20 63 6f  e and the row co
2cb0d 75 6e 74 20 61 73 20 74 68 65 20 63 6f 6e 74 65  unt as the conte
2cb0e 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  nt...  */..  if(
2cb0f 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30   pTab->pIndex==0
2cb10 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2cb11 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2cb12 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43  _OpenRead, iIdxC
2cb13 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  ur, pTab->tnum, 
2cb14 69 44 62 29 3b 0d 0a 20 20 20 20 56 64 62 65 43  iDb);..    VdbeC
2cb15 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
2cb16 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0d   pTab->zName));.
2cb17 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cb18 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75  AddOp2(v, OP_Cou
2cb19 6e 74 2c 20 69 49 64 78 43 75 72 2c 20 72 65 67  nt, iIdxCur, reg
2cb1a 53 74 61 74 31 29 3b 0d 0a 20 20 20 20 73 71 6c  Stat1);..    sql
2cb1b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2cb1c 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78  , OP_Close, iIdx
2cb1d 43 75 72 29 3b 0d 0a 20 20 20 20 6a 5a 65 72 6f  Cur);..    jZero
2cb1e 52 6f 77 73 20 3d 20 73 71 6c 69 74 65 33 56 64  Rows = sqlite3Vd
2cb1f 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2cb20 66 4e 6f 74 2c 20 72 65 67 53 74 61 74 31 29 3b  fNot, regStat1);
2cb21 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
2cb22 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2cb23 65 72 65 28 76 2c 20 6a 5a 65 72 6f 52 6f 77 73  ere(v, jZeroRows
2cb24 29 3b 0d 0a 20 20 20 20 6a 5a 65 72 6f 52 6f 77  );..    jZeroRow
2cb25 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  s = sqlite3VdbeA
2cb26 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
2cb27 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
2cb28 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2cb29 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 49  OP_Null, 0, regI
2cb2a 64 78 6e 61 6d 65 29 3b 0d 0a 20 20 73 71 6c 69  dxname);..  sqli
2cb2b 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2cb2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2cb2d 72 65 67 54 61 62 6e 61 6d 65 2c 20 33 2c 20 72  regTabname, 3, r
2cb2e 65 67 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29  egRec, "aaa", 0)
2cb2f 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
2cb30 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2cb31 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c  Rowid, iStatCur,
2cb32 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0d 0a   regNewRowid);..
2cb33 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2cb34 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
2cb35 2c 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52  , iStatCur, regR
2cb36 65 63 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  ec, regNewRowid)
2cb37 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
2cb38 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
2cb39 41 47 5f 41 50 50 45 4e 44 29 3b 0d 0a 20 20 69  AG_APPEND);..  i
2cb3a 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c  f( pParse->nMem<
2cb3b 72 65 67 52 65 63 20 29 20 70 50 61 72 73 65 2d  regRec ) pParse-
2cb3c 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 63 3b 0d  >nMem = regRec;.
2cb3d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
2cb3e 6d 70 48 65 72 65 28 76 2c 20 6a 5a 65 72 6f 52  mpHere(v, jZeroR
2cb3f 6f 77 73 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a  ows);..}....../*
2cb40 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  ..** Generate co
2cb41 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75  de that will cau
2cb42 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  se the most rece
2cb43 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69  nt index analysi
2cb44 73 20 74 6f 0d 0a 2a 2a 20 62 65 20 6c 6f 61 64  s to..** be load
2cb45 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c  ed into internal
2cb46 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65   hash tables whe
2cb47 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65  re is can be use
2cb48 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  d...*/..static v
2cb49 6f 69 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73  oid loadAnalysis
2cb4a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2cb4b 69 6e 74 20 69 44 62 29 7b 0d 0a 20 20 56 64 62  int iDb){..  Vdb
2cb4c 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
2cb4d 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a  tVdbe(pParse);..
2cb4e 20 20 69 66 28 20 76 20 29 7b 0d 0a 20 20 20 20    if( v ){..    
2cb4f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2cb50 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c  1(v, OP_LoadAnal
2cb51 79 73 69 73 2c 20 69 44 62 29 3b 0d 0a 20 20 7d  ysis, iDb);..  }
2cb52 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  ..}..../*..** Ge
2cb53 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2cb54 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c   will do an anal
2cb55 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74 69 72  ysis of an entir
2cb56 65 20 64 61 74 61 62 61 73 65 0d 0a 2a 2f 0d 0a  e database..*/..
2cb57 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c  static void anal
2cb58 79 7a 65 44 61 74 61 62 61 73 65 28 50 61 72 73  yzeDatabase(Pars
2cb59 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2cb5a 44 62 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  Db){..  sqlite3 
2cb5b 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2cb5c 3b 0d 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  ;..  Schema *pSc
2cb5d 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
2cb5e 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20 20  Db].pSchema;    
2cb5f 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61 74  /* Schema of dat
2cb60 61 62 61 73 65 20 69 44 62 20 2a 2f 0d 0a 20 20  abase iDb */..  
2cb61 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0d 0a 20 20  HashElem *k;..  
2cb62 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0d 0a 20  int iStatCur;.. 
2cb63 20 69 6e 74 20 69 4d 65 6d 3b 0d 0a 0d 0a 20 20   int iMem;....  
2cb64 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
2cb65 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
2cb66 65 2c 20 30 2c 20 69 44 62 29 3b 0d 0a 20 20 69  e, 0, iDb);..  i
2cb67 53 74 61 74 43 75 72 20 3d 20 70 50 61 72 73 65  StatCur = pParse
2cb68 2d 3e 6e 54 61 62 3b 0d 0a 20 20 70 50 61 72 73  ->nTab;..  pPars
2cb69 65 2d 3e 6e 54 61 62 20 2b 3d 20 33 3b 0d 0a 20  e->nTab += 3;.. 
2cb6a 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70   openStatTable(p
2cb6b 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61  Parse, iDb, iSta
2cb6c 74 43 75 72 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  tCur, 0, 0);..  
2cb6d 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iMem = pParse->n
2cb6e 4d 65 6d 2b 31 3b 0d 0a 20 20 61 73 73 65 72 74  Mem+1;..  assert
2cb6f 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d  ( sqlite3SchemaM
2cb70 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62  utexHeld(db, iDb
2cb71 2c 20 30 29 20 29 3b 0d 0a 20 20 66 6f 72 28 6b  , 0) );..  for(k
2cb72 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2cb73 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  (&pSchema->tblHa
2cb74 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65  sh); k; k=sqlite
2cb75 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0d 0a 20  HashNext(k)){.. 
2cb76 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
2cb77 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48   (Table*)sqliteH
2cb78 61 73 68 44 61 74 61 28 6b 29 3b 0d 0a 20 20 20  ashData(k);..   
2cb79 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65   analyzeOneTable
2cb7a 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
2cb7b 2c 20 69 53 74 61 74 43 75 72 2c 20 69 4d 65 6d  , iStatCur, iMem
2cb7c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 6c 6f 61 64 41  );..  }..  loadA
2cb7d 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20  nalysis(pParse, 
2cb7e 69 44 62 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  iDb);..}..../*..
2cb7f 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2cb80 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e   that will do an
2cb81 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 20 73   analysis of a s
2cb82 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 0d 0a  ingle table in..
2cb83 2a 2a 20 61 20 64 61 74 61 62 61 73 65 2e 20 20  ** a database.  
2cb84 49 66 20 70 4f 6e 6c 79 49 64 78 20 69 73 20 6e  If pOnlyIdx is n
2cb85 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20  ot NULL then it 
2cb86 69 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 64 65  is a single inde
2cb87 78 0d 0a 2a 2a 20 69 6e 20 70 54 61 62 20 74 68  x..** in pTab th
2cb88 61 74 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 61  at should be ana
2cb89 6c 79 7a 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  lyzed...*/..stat
2cb8a 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 54  ic void analyzeT
2cb8b 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
2cb8c 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  se, Table *pTab,
2cb8d 20 49 6e 64 65 78 20 2a 70 4f 6e 6c 79 49 64 78   Index *pOnlyIdx
2cb8e 29 7b 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d 0a  ){..  int iDb;..
2cb8f 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0d    int iStatCur;.
2cb90 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  ...  assert( pTa
2cb91 62 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  b!=0 );..  asser
2cb92 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2cb93 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
2cb94 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0d 0a 20  Parse->db) );.. 
2cb95 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2cb96 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
2cb97 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
2cb98 63 68 65 6d 61 29 3b 0d 0a 20 20 73 71 6c 69 74  chema);..  sqlit
2cb99 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2cb9a 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
2cb9b 20 69 44 62 29 3b 0d 0a 20 20 69 53 74 61 74 43   iDb);..  iStatC
2cb9c 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
2cb9d 62 3b 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54  b;..  pParse->nT
2cb9e 61 62 20 2b 3d 20 33 3b 0d 0a 20 20 69 66 28 20  ab += 3;..  if( 
2cb9f 70 4f 6e 6c 79 49 64 78 20 29 7b 0d 0a 20 20 20  pOnlyIdx ){..   
2cba0 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70   openStatTable(p
2cba1 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61  Parse, iDb, iSta
2cba2 74 43 75 72 2c 20 70 4f 6e 6c 79 49 64 78 2d 3e  tCur, pOnlyIdx->
2cba3 7a 4e 61 6d 65 2c 20 22 69 64 78 22 29 3b 0d 0a  zName, "idx");..
2cba4 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6f 70    }else{..    op
2cba5 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61 72  enStatTable(pPar
2cba6 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43 75  se, iDb, iStatCu
2cba7 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  r, pTab->zName, 
2cba8 22 74 62 6c 22 29 3b 0d 0a 20 20 7d 0d 0a 20 20  "tbl");..  }..  
2cba9 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
2cbaa 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 4f  pParse, pTab, pO
2cbab 6e 6c 79 49 64 78 2c 20 69 53 74 61 74 43 75 72  nlyIdx, iStatCur
2cbac 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31  , pParse->nMem+1
2cbad 29 3b 0d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73  );..  loadAnalys
2cbae 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  is(pParse, iDb);
2cbaf 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  ..}..../*..** Ge
2cbb0 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2cbb1 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d  the ANALYZE comm
2cbb2 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72  and.  The parser
2cbb3 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2cbb4 69 6e 65 0d 0a 2a 2a 20 77 68 65 6e 20 69 74 20  ine..** when it 
2cbb5 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e  recognizes an AN
2cbb6 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0d 0a  ALYZE command...
2cbb7 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  **..**        AN
2cbb8 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 20  ALYZE           
2cbb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cbba 20 2d 2d 20 31 0d 0a 2a 2a 20 20 20 20 20 20 20   -- 1..**       
2cbbb 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 61 62   ANALYZE  <datab
2cbbc 61 73 65 3e 20 20 20 20 20 20 20 20 20 20 20 20  ase>            
2cbbd 20 20 20 20 2d 2d 20 32 0d 0a 2a 2a 20 20 20 20      -- 2..**    
2cbbe 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c 64      ANALYZE  ?<d
2cbbf 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65  atabase>.?<table
2cbc0 6e 61 6d 65 3e 20 20 2d 2d 20 33 0d 0a 2a 2a 0d  name>  -- 3..**.
2cbc1 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65  .** Form 1 cause
2cbc2 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e  s all indices in
2cbc3 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61   all attached da
2cbc4 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e  tabases to be an
2cbc5 61 6c 79 7a 65 64 2e 0d 0a 2a 2a 20 46 6f 72 6d  alyzed...** Form
2cbc6 20 32 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20   2 analyzes all 
2cbc7 69 6e 64 69 63 65 73 20 74 68 65 20 73 69 6e 67  indices the sing
2cbc8 6c 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  le database name
2cbc9 64 2e 0d 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e  d...** Form 3 an
2cbca 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63  alyzes all indic
2cbcb 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
2cbcc 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62  th the named tab
2cbcd 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  le...*/..SQLITE_
2cbce 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2cbcf 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73  ite3Analyze(Pars
2cbd0 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
2cbd1 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20   *pName1, Token 
2cbd2 2a 70 4e 61 6d 65 32 29 7b 0d 0a 20 20 73 71 6c  *pName2){..  sql
2cbd3 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2cbd4 65 2d 3e 64 62 3b 0d 0a 20 20 69 6e 74 20 69 44  e->db;..  int iD
2cbd5 62 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  b;..  int i;..  
2cbd6 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0d 0a  char *z, *zDb;..
2cbd7 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a    Table *pTab;..
2cbd8 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a    Index *pIdx;..
2cbd9 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e    Token *pTableN
2cbda 61 6d 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61  ame;....  /* Rea
2cbdb 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
2cbdc 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
2cbdd 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
2cbde 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2cbdf 65 0d 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65  e..  ** and code
2cbe0 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
2cbe1 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0d 0a  eturn NULL. */..
2cbe2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2cbe3 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
2cbe4 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62  texes(pParse->db
2cbe5 29 20 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49  ) );..  if( SQLI
2cbe6 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
2cbe7 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2cbe8 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b   ){..    return;
2cbe9 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72  ..  }....  asser
2cbea 74 28 20 70 4e 61 6d 65 32 21 3d 30 20 7c 7c 20  t( pName2!=0 || 
2cbeb 70 4e 61 6d 65 31 3d 3d 30 20 29 3b 0d 0a 20 20  pName1==0 );..  
2cbec 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b  if( pName1==0 ){
2cbed 0d 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a  ..    /* Form 1:
2cbee 20 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74    Analyze everyt
2cbef 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 66 6f 72  hing */..    for
2cbf0 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
2cbf1 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66   i++){..      if
2cbf2 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75  ( i==1 ) continu
2cbf3 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e  e;  /* Do not an
2cbf4 61 6c 79 7a 65 20 74 68 65 20 54 45 4d 50 20 64  alyze the TEMP d
2cbf5 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20 20 20 20  atabase */..    
2cbf6 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73    analyzeDatabas
2cbf7 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0d 0a 20  e(pParse, i);.. 
2cbf8 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 20 69 66     }..  }else if
2cbf9 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29  ( pName2->n==0 )
2cbfa 7b 0d 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32  {..    /* Form 2
2cbfb 3a 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64  :  Analyze the d
2cbfc 61 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65  atabase or table
2cbfd 20 6e 61 6d 65 64 20 2a 2f 0d 0a 20 20 20 20 69   named */..    i
2cbfe 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  Db = sqlite3Find
2cbff 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0d  Db(db, pName1);.
2cc00 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20  .    if( iDb>=0 
2cc01 29 7b 0d 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a  ){..      analyz
2cc02 65 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65  eDatabase(pParse
2cc03 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 7d 65 6c  , iDb);..    }el
2cc04 73 65 7b 0d 0a 20 20 20 20 20 20 7a 20 3d 20 73  se{..      z = s
2cc05 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2cc06 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b  ken(db, pName1);
2cc07 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b  ..      if( z ){
2cc08 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70  ..        if( (p
2cc09 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Idx = sqlite3Fin
2cc0a 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c 20 30 29  dIndex(db, z, 0)
2cc0b 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )!=0 ){..       
2cc0c 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28     analyzeTable(
2cc0d 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 70 54  pParse, pIdx->pT
2cc0e 61 62 6c 65 2c 20 70 49 64 78 29 3b 0d 0a 20 20  able, pIdx);..  
2cc0f 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2cc10 28 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c  (pTab = sqlite3L
2cc11 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
2cc12 65 2c 20 30 2c 20 7a 2c 20 30 29 29 21 3d 30 20  e, 0, z, 0))!=0 
2cc13 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 6e  ){..          an
2cc14 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73  alyzeTable(pPars
2cc15 65 2c 20 70 54 61 62 2c 20 30 29 3b 0d 0a 20 20  e, pTab, 0);..  
2cc16 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
2cc17 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2cc18 62 2c 20 7a 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  b, z);..      }.
2cc19 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
2cc1a 0d 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 33 3a  ..    /* Form 3:
2cc1b 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 66 75 6c   Analyze the ful
2cc1c 6c 79 20 71 75 61 6c 69 66 69 65 64 20 74 61 62  ly qualified tab
2cc1d 6c 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20  le name */..    
2cc1e 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
2cc1f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
2cc20 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
2cc21 20 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0d 0a   &pTableName);..
2cc22 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29      if( iDb>=0 )
2cc23 7b 0d 0a 20 20 20 20 20 20 7a 44 62 20 3d 20 64  {..      zDb = d
2cc24 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2cc25 65 3b 0d 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  e;..      z = sq
2cc26 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2cc27 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d  en(db, pTableNam
2cc28 65 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a  e);..      if( z
2cc29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28   ){..        if(
2cc2a 20 28 70 49 64 78 20 3d 20 73 71 6c 69 74 65 33   (pIdx = sqlite3
2cc2b 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c  FindIndex(db, z,
2cc2c 20 7a 44 62 29 29 21 3d 30 20 29 7b 0d 0a 20 20   zDb))!=0 ){..  
2cc2d 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54          analyzeT
2cc2e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 49 64  able(pParse, pId
2cc2f 78 2d 3e 70 54 61 62 6c 65 2c 20 70 49 64 78 29  x->pTable, pIdx)
2cc30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
2cc31 20 69 66 28 20 28 70 54 61 62 20 3d 20 73 71 6c   if( (pTab = sql
2cc32 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
2cc33 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44  pParse, 0, z, zD
2cc34 62 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  b))!=0 ){..     
2cc35 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c       analyzeTabl
2cc36 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2cc37 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  0);..        }..
2cc38 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2cc39 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0d 0a 20  bFree(db, z);.. 
2cc3a 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 20 20 20       }..    }   
2cc3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
2cc3c 2a 2a 20 55 73 65 64 20 74 6f 20 70 61 73 73 20  ** Used to pass 
2cc3d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2cc3e 20 74 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65   the analyzer re
2cc3f 61 64 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20  ader through to 
2cc40 74 68 65 0d 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  the..** callback
2cc41 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d 0a 74   routine...*/..t
2cc42 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 6e  ypedef struct an
2cc43 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c 79  alysisInfo analy
2cc44 73 69 73 49 6e 66 6f 3b 0d 0a 73 74 72 75 63 74  sisInfo;..struct
2cc45 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0d   analysisInfo {.
2cc46 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0d  .  sqlite3 *db;.
2cc47 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2cc48 44 61 74 61 62 61 73 65 3b 0d 0a 7d 3b 0d 0a 0d  Database;..};...
2cc49 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 63 61 6c  ./*..** This cal
2cc4a 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
2cc4b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69   once for each i
2cc4c 6e 64 65 78 20 77 68 65 6e 20 72 65 61 64 69 6e  ndex when readin
2cc4d 67 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74 65  g the..** sqlite
2cc4e 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0d  _stat1 table.  .
2cc4f 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 61 72 67 76  .**..**     argv
2cc50 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68  [0] = name of th
2cc51 65 20 74 61 62 6c 65 0d 0a 2a 2a 20 20 20 20 20  e table..**     
2cc52 61 72 67 76 5b 31 5d 20 3d 20 6e 61 6d 65 20 6f  argv[1] = name o
2cc53 66 20 74 68 65 20 69 6e 64 65 78 20 28 6d 69 67  f the index (mig
2cc54 68 74 20 62 65 20 4e 55 4c 4c 29 0d 0a 2a 2a 20  ht be NULL)..** 
2cc55 20 20 20 20 61 72 67 76 5b 32 5d 20 3d 20 72 65      argv[2] = re
2cc56 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73 69  sults of analysi
2cc57 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20 66  s - on integer f
2cc58 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0d 0a  or each column..
2cc59 2a 2a 0d 0a 2a 2a 20 45 6e 74 72 69 65 73 20 66  **..** Entries f
2cc5a 6f 72 20 77 68 69 63 68 20 61 72 67 76 5b 31 5d  or which argv[1]
2cc5b 3d 3d 4e 55 4c 4c 20 73 69 6d 70 6c 79 20 72 65  ==NULL simply re
2cc5c 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20  cord the number 
2cc5d 6f 66 20 72 6f 77 73 20 69 6e 0d 0a 2a 2a 20 74  of rows in..** t
2cc5e 68 65 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73  he table...*/..s
2cc5f 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73  tatic int analys
2cc60 69 73 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70  isLoader(void *p
2cc61 44 61 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20  Data, int argc, 
2cc62 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61  char **argv, cha
2cc63 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0d 0a 20  r **NotUsed){.. 
2cc64 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70   analysisInfo *p
2cc65 49 6e 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73  Info = (analysis
2cc66 49 6e 66 6f 2a 29 70 44 61 74 61 3b 0d 0a 20 20  Info*)pData;..  
2cc67 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0d 0a  Index *pIndex;..
2cc68 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b    Table *pTable;
2cc69 0d 0a 20 20 69 6e 74 20 69 2c 20 63 2c 20 6e 3b  ..  int i, c, n;
2cc6a 0d 0a 20 20 74 52 6f 77 63 6e 74 20 76 3b 0d 0a  ..  tRowcnt v;..
2cc6b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
2cc6c 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72  ....  assert( ar
2cc6d 67 63 3d 3d 33 20 29 3b 0d 0a 20 20 55 4e 55 53  gc==3 );..  UNUS
2cc6e 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
2cc6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0d 0a 0d  tUsed, argc);...
2cc70 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c  .  if( argv==0 |
2cc71 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20  | argv[0]==0 || 
2cc72 61 72 67 76 5b 32 5d 3d 3d 30 20 29 7b 0d 0a 20  argv[2]==0 ){.. 
2cc73 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
2cc74 7d 0d 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71  }..  pTable = sq
2cc75 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70  lite3FindTable(p
2cc76 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30  Info->db, argv[0
2cc77 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62  ], pInfo->zDatab
2cc78 61 73 65 29 3b 0d 0a 20 20 69 66 28 20 70 54 61  ase);..  if( pTa
2cc79 62 6c 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ble==0 ){..    r
2cc7a 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
2cc7b 20 69 66 28 20 61 72 67 76 5b 31 5d 20 29 7b 0d   if( argv[1] ){.
2cc7c 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71  .    pIndex = sq
2cc7d 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70  lite3FindIndex(p
2cc7e 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 31  Info->db, argv[1
2cc7f 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62  ], pInfo->zDatab
2cc80 61 73 65 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  ase);..  }else{.
2cc81 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b  .    pIndex = 0;
2cc82 0d 0a 20 20 7d 0d 0a 20 20 6e 20 3d 20 70 49 6e  ..  }..  n = pIn
2cc83 64 65 78 20 3f 20 70 49 6e 64 65 78 2d 3e 6e 43  dex ? pIndex->nC
2cc84 6f 6c 75 6d 6e 20 3a 20 30 3b 0d 0a 20 20 7a 20  olumn : 0;..  z 
2cc85 3d 20 61 72 67 76 5b 32 5d 3b 0d 0a 20 20 66 6f  = argv[2];..  fo
2cc86 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d  r(i=0; *z && i<=
2cc87 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 76 20  n; i++){..    v 
2cc88 3d 20 30 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  = 0;..    while(
2cc89 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26   (c=z[0])>='0' &
2cc8a 26 20 63 3c 3d 27 39 27 20 29 7b 0d 0a 20 20 20  & c<='9' ){..   
2cc8b 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20     v = v*10 + c 
2cc8c 2d 20 27 30 27 3b 0d 0a 20 20 20 20 20 20 7a 2b  - '0';..      z+
2cc8d 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  +;..    }..    i
2cc8e 66 28 20 69 3d 3d 30 20 29 20 70 54 61 62 6c 65  f( i==0 ) pTable
2cc8f 2d 3e 6e 52 6f 77 45 73 74 20 3d 20 76 3b 0d 0a  ->nRowEst = v;..
2cc90 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d      if( pIndex==
2cc91 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20  0 ) break;..    
2cc92 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74  pIndex->aiRowEst
2cc93 5b 69 5d 20 3d 20 76 3b 0d 0a 20 20 20 20 69 66  [i] = v;..    if
2cc94 28 20 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b  ( *z==' ' ) z++;
2cc95 0d 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70  ..    if( memcmp
2cc96 28 7a 2c 20 22 75 6e 6f 72 64 65 72 65 64 22 2c  (z, "unordered",
2cc97 20 31 30 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20   10)==0 ){..    
2cc98 20 20 70 49 6e 64 65 78 2d 3e 62 55 6e 6f 72 64    pIndex->bUnord
2cc99 65 72 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20  ered = 1;..     
2cc9a 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2cc9b 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
2cc9c 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66  ..}..../*..** If
2cc9d 20 74 68 65 20 49 6e 64 65 78 2e 61 53 61 6d 70   the Index.aSamp
2cc9e 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 6e  le variable is n
2cc9f 6f 74 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65 20  ot NULL, delete 
2cca0 74 68 65 20 61 53 61 6d 70 6c 65 5b 5d 20 61 72  the aSample[] ar
2cca1 72 61 79 0d 0a 2a 2a 20 61 6e 64 20 69 74 73 20  ray..** and its 
2cca2 63 6f 6e 74 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 53  contents...*/..S
2cca3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cca4 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
2cca5 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 73 71 6c  IndexSamples(sql
2cca6 69 74 65 33 20 2a 64 62 2c 20 49 6e 64 65 78 20  ite3 *db, Index 
2cca7 2a 70 49 64 78 29 7b 0d 0a 23 69 66 64 65 66 20  *pIdx){..#ifdef 
2cca8 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2cca9 41 54 33 0d 0a 20 20 69 66 28 20 70 49 64 78 2d  AT3..  if( pIdx-
2ccaa 3e 61 53 61 6d 70 6c 65 20 29 7b 0d 0a 20 20 20  >aSample ){..   
2ccab 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 66 6f 72   int j;..    for
2ccac 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 53  (j=0; j<pIdx->nS
2ccad 61 6d 70 6c 65 3b 20 6a 2b 2b 29 7b 0d 0a 20 20  ample; j++){..  
2ccae 20 20 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20      IndexSample 
2ccaf 2a 70 20 3d 20 26 70 49 64 78 2d 3e 61 53 61 6d  *p = &pIdx->aSam
2ccb0 70 6c 65 5b 6a 5d 3b 0d 0a 20 20 20 20 20 20 69  ple[j];..      i
2ccb1 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c  f( p->eType==SQL
2ccb2 49 54 45 5f 54 45 58 54 20 7c 7c 20 70 2d 3e 65  ITE_TEXT || p->e
2ccb3 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  Type==SQLITE_BLO
2ccb4 42 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  B ){..        sq
2ccb5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ccb6 70 2d 3e 75 2e 7a 29 3b 0d 0a 20 20 20 20 20 20  p->u.z);..      
2ccb7 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71  }..    }..    sq
2ccb8 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ccb9 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 29 3b 0d  pIdx->aSample);.
2ccba 0a 20 20 7d 0d 0a 20 20 69 66 28 20 64 62 20 26  .  }..  if( db &
2ccbb 26 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65  & db->pnBytesFre
2ccbc 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 49  ed==0 ){..    pI
2ccbd 64 78 2d 3e 6e 53 61 6d 70 6c 65 20 3d 20 30 3b  dx->nSample = 0;
2ccbe 0d 0a 20 20 20 20 70 49 64 78 2d 3e 61 53 61 6d  ..    pIdx->aSam
2ccbf 70 6c 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 23  ple = 0;..  }..#
2ccc0 65 6c 73 65 0d 0a 20 20 55 4e 55 53 45 44 5f 50  else..  UNUSED_P
2ccc1 41 52 41 4d 45 54 45 52 28 64 62 29 3b 0d 0a 20  ARAMETER(db);.. 
2ccc2 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2ccc3 52 28 70 49 64 78 29 3b 0d 0a 23 65 6e 64 69 66  R(pIdx);..#endif
2ccc4 0d 0a 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51  ..}....#ifdef SQ
2ccc5 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2ccc6 33 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 61 64 20 63  3../*..** Load c
2ccc7 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  ontent from the 
2ccc8 73 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61 62  sqlite_stat3 tab
2ccc9 6c 65 20 69 6e 74 6f 20 74 68 65 20 49 6e 64 65  le into the Inde
2ccca 78 2e 61 53 61 6d 70 6c 65 5b 5d 0d 0a 2a 2a 20  x.aSample[]..** 
2cccb 61 72 72 61 79 73 20 6f 66 20 61 6c 6c 20 69 6e  arrays of all in
2cccc 64 69 63 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  dices...*/..stat
2cccd 69 63 20 69 6e 74 20 6c 6f 61 64 53 74 61 74 33  ic int loadStat3
2ccce 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2cccf 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0d  nst char *zDb){.
2ccd0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
2ccd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ccd2 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 73   /* Result codes
2ccd3 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65   from subroutine
2ccd4 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  s */..  sqlite3_
2ccd5 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
2ccd6 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20        /* An SQL 
2ccd7 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
2ccd8 72 75 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  run */..  char *
2ccd9 7a 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zSql;           
2ccda 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
2ccdb 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  of the SQL state
2ccdc 6d 65 6e 74 20 2a 2f 0d 0a 20 20 49 6e 64 65 78  ment */..  Index
2ccdd 20 2a 70 50 72 65 76 49 64 78 20 3d 20 30 3b 20   *pPrevIdx = 0; 
2ccde 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76           /* Prev
2ccdf 69 6f 75 73 20 69 6e 64 65 78 20 69 6e 20 74 68  ious index in th
2cce0 65 20 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74  e loop */..  int
2cce1 20 69 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20   idx = 0;       
2cce2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 6c             /* sl
2cce3 6f 74 20 69 6e 20 70 49 64 78 2d 3e 61 53 61 6d  ot in pIdx->aSam
2cce4 70 6c 65 5b 5d 20 66 6f 72 20 6e 65 78 74 20 73  ple[] for next s
2cce5 61 6d 70 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  ample */..  int 
2cce6 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  eType;          
2cce7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2cce8 61 74 79 70 65 20 6f 66 20 61 20 73 61 6d 70 6c  atype of a sampl
2cce9 65 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 53 61 6d  e */..  IndexSam
2ccea 70 6c 65 20 2a 70 53 61 6d 70 6c 65 3b 20 20 20  ple *pSample;   
2cceb 20 20 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20        /* A slot 
2ccec 69 6e 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65  in pIdx->aSample
2cced 5b 5d 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 21  [] */....  if( !
2ccee 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2ccef 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
2ccf0 74 33 22 2c 20 7a 44 62 29 20 29 7b 0d 0a 20 20  t3", zDb) ){..  
2ccf1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2ccf2 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 7a 53  OK;..  }....  zS
2ccf3 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  ql = sqlite3MPri
2ccf4 6e 74 66 28 64 62 2c 20 0d 0a 20 20 20 20 20 20  ntf(db, ..      
2ccf5 22 53 45 4c 45 43 54 20 69 64 78 2c 63 6f 75 6e  "SELECT idx,coun
2ccf6 74 28 2a 29 20 46 52 4f 4d 20 25 51 2e 73 71 6c  t(*) FROM %Q.sql
2ccf7 69 74 65 5f 73 74 61 74 33 22 0d 0a 20 20 20 20  ite_stat3"..    
2ccf8 20 20 22 20 47 52 4f 55 50 20 42 59 20 69 64 78    " GROUP BY idx
2ccf9 22 2c 20 7a 44 62 29 3b 0d 0a 20 20 69 66 28 20  ", zDb);..  if( 
2ccfa 21 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 72 65  !zSql ){..    re
2ccfb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2ccfc 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20  M;..  }..  rc = 
2ccfd 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2ccfe 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
2ccff 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 73 71 6c  Stmt, 0);..  sql
2cd00 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2cd01 53 71 6c 29 3b 0d 0a 20 20 69 66 28 20 72 63 20  Sql);..  if( rc 
2cd02 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a  ) return rc;....
2cd03 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
2cd04 5f 73 74 65 70 28 70 53 74 6d 74 29 3d 3d 53 51  _step(pStmt)==SQ
2cd05 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20 20 20  LITE_ROW ){..   
2cd06 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20   char *zIndex;  
2cd07 20 2f 2a 20 49 6e 64 65 78 20 6e 61 6d 65 20 2a   /* Index name *
2cd08 2f 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  /..    Index *pI
2cd09 64 78 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  dx;    /* Pointe
2cd0a 72 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  r to the index o
2cd0b 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 20 20 69 6e  bject */..    in
2cd0c 74 20 6e 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a  t nSample;    /*
2cd0d 20 4e 75 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c   Number of sampl
2cd0e 65 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 7a 49 6e  es */....    zIn
2cd0f 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29 73 71  dex = (char *)sq
2cd10 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2cd11 74 28 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20  t(pStmt, 0);..  
2cd12 20 20 69 66 28 20 7a 49 6e 64 65 78 3d 3d 30 20    if( zIndex==0 
2cd13 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
2cd14 20 6e 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74   nSample = sqlit
2cd15 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53  e3_column_int(pS
2cd16 74 6d 74 2c 20 31 29 3b 0d 0a 20 20 20 20 70 49  tmt, 1);..    pI
2cd17 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  dx = sqlite3Find
2cd18 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64 65 78  Index(db, zIndex
2cd19 2c 20 7a 44 62 29 3b 0d 0a 20 20 20 20 69 66 28  , zDb);..    if(
2cd1a 20 70 49 64 78 3d 3d 30 20 29 20 63 6f 6e 74 69   pIdx==0 ) conti
2cd1b 6e 75 65 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  nue;..    assert
2cd1c 28 20 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 3d  ( pIdx->nSample=
2cd1d 3d 30 20 29 3b 0d 0a 20 20 20 20 70 49 64 78 2d  =0 );..    pIdx-
2cd1e 3e 6e 53 61 6d 70 6c 65 20 3d 20 6e 53 61 6d 70  >nSample = nSamp
2cd1f 6c 65 3b 0d 0a 20 20 20 20 70 49 64 78 2d 3e 61  le;..    pIdx->a
2cd20 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74 65 33  Sample = sqlite3
2cd21 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e 53 61 6d  MallocZero( nSam
2cd22 70 6c 65 2a 73 69 7a 65 6f 66 28 49 6e 64 65 78  ple*sizeof(Index
2cd23 53 61 6d 70 6c 65 29 20 29 3b 0d 0a 20 20 20 20  Sample) );..    
2cd24 70 49 64 78 2d 3e 61 76 67 45 71 20 3d 20 70 49  pIdx->avgEq = pI
2cd25 64 78 2d 3e 61 69 52 6f 77 45 73 74 5b 31 5d 3b  dx->aiRowEst[1];
2cd26 0d 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e  ..    if( pIdx->
2cd27 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b 0d 0a 20  aSample==0 ){.. 
2cd28 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
2cd29 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20  ailed = 1;..    
2cd2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
2cd2b 7a 65 28 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20  ze(pStmt);..    
2cd2c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2cd2d 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20  NOMEM;..    }.. 
2cd2e 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
2cd2f 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
2cd30 74 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 20  t);..  if( rc ) 
2cd31 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20 20  return rc;....  
2cd32 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
2cd33 72 69 6e 74 66 28 64 62 2c 20 0d 0a 20 20 20 20  rintf(db, ..    
2cd34 20 20 22 53 45 4c 45 43 54 20 69 64 78 2c 6e 65    "SELECT idx,ne
2cd35 71 2c 6e 6c 74 2c 6e 64 6c 74 2c 73 61 6d 70 6c  q,nlt,ndlt,sampl
2cd36 65 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65  e FROM %Q.sqlite
2cd37 5f 73 74 61 74 33 22 2c 20 7a 44 62 29 3b 0d 0a  _stat3", zDb);..
2cd38 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0d 0a    if( !zSql ){..
2cd39 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2cd3a 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20  E_NOMEM;..  }.. 
2cd3b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
2cd3c 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
2cd3d 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d  -1, &pStmt, 0);.
2cd3e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2cd3f 28 64 62 2c 20 7a 53 71 6c 29 3b 0d 0a 20 20 69  (db, zSql);..  i
2cd40 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
2cd41 63 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65 28 20 73  c;....  while( s
2cd42 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
2cd43 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  t)==SQLITE_ROW )
2cd44 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e  {..    char *zIn
2cd45 64 65 78 3b 20 20 20 2f 2a 20 49 6e 64 65 78 20  dex;   /* Index 
2cd46 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 49 6e 64  name */..    Ind
2cd47 65 78 20 2a 70 49 64 78 3b 20 20 20 20 2f 2a 20  ex *pIdx;    /* 
2cd48 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69  Pointer to the i
2cd49 6e 64 65 78 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a  ndex object */..
2cd4a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
2cd4b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2cd4c 74 65 72 20 2a 2f 0d 0a 20 20 20 20 74 52 6f 77  ter */..    tRow
2cd4d 63 6e 74 20 73 75 6d 45 71 3b 20 20 2f 2a 20 53  cnt sumEq;  /* S
2cd4e 75 6d 20 6f 66 20 74 68 65 20 6e 45 71 20 76 61  um of the nEq va
2cd4f 6c 75 65 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 7a  lues */....    z
2cd50 49 6e 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29  Index = (char *)
2cd51 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2cd52 65 78 74 28 70 53 74 6d 74 2c 20 30 29 3b 0d 0a  ext(pStmt, 0);..
2cd53 20 20 20 20 69 66 28 20 7a 49 6e 64 65 78 3d 3d      if( zIndex==
2cd54 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20  0 ) continue;.. 
2cd55 20 20 20 70 49 64 78 20 3d 20 73 71 6c 69 74 65     pIdx = sqlite
2cd56 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
2cd57 49 6e 64 65 78 2c 20 7a 44 62 29 3b 0d 0a 20 20  Index, zDb);..  
2cd58 20 20 69 66 28 20 70 49 64 78 3d 3d 30 20 29 20    if( pIdx==0 ) 
2cd59 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69  continue;..    i
2cd5a 66 28 20 70 49 64 78 3d 3d 70 50 72 65 76 49 64  f( pIdx==pPrevId
2cd5b 78 20 29 7b 0d 0a 20 20 20 20 20 20 69 64 78 2b  x ){..      idx+
2cd5c 2b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  +;..    }else{..
2cd5d 20 20 20 20 20 20 70 50 72 65 76 49 64 78 20 3d        pPrevIdx =
2cd5e 20 70 49 64 78 3b 0d 0a 20 20 20 20 20 20 69 64   pIdx;..      id
2cd5f 78 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20  x = 0;..    }.. 
2cd60 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70     assert( idx<p
2cd61 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 20 29 3b 0d  Idx->nSample );.
2cd62 0a 20 20 20 20 70 53 61 6d 70 6c 65 20 3d 20 26  .    pSample = &
2cd63 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 5b 69 64  pIdx->aSample[id
2cd64 78 5d 3b 0d 0a 20 20 20 20 70 53 61 6d 70 6c 65  x];..    pSample
2cd65 2d 3e 6e 45 71 20 3d 20 28 74 52 6f 77 63 6e 74  ->nEq = (tRowcnt
2cd66 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
2cd67 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 29 3b  int64(pStmt, 1);
2cd68 0d 0a 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e  ..    pSample->n
2cd69 4c 74 20 3d 20 28 74 52 6f 77 63 6e 74 29 73 71  Lt = (tRowcnt)sq
2cd6a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
2cd6b 36 34 28 70 53 74 6d 74 2c 20 32 29 3b 0d 0a 20  64(pStmt, 2);.. 
2cd6c 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 44 4c 74     pSample->nDLt
2cd6d 20 3d 20 28 74 52 6f 77 63 6e 74 29 73 71 6c 69   = (tRowcnt)sqli
2cd6e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
2cd6f 28 70 53 74 6d 74 2c 20 33 29 3b 0d 0a 20 20 20  (pStmt, 3);..   
2cd70 20 69 66 28 20 69 64 78 3d 3d 70 49 64 78 2d 3e   if( idx==pIdx->
2cd71 6e 53 61 6d 70 6c 65 2d 31 20 29 7b 0d 0a 20 20  nSample-1 ){..  
2cd72 20 20 20 20 69 66 28 20 70 53 61 6d 70 6c 65 2d      if( pSample-
2cd73 3e 6e 44 4c 74 3e 30 20 29 7b 0d 0a 20 20 20 20  >nDLt>0 ){..    
2cd74 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 73 75 6d      for(i=0, sum
2cd75 45 71 3d 30 3b 20 69 3c 3d 69 64 78 2d 31 3b 20  Eq=0; i<=idx-1; 
2cd76 69 2b 2b 29 20 73 75 6d 45 71 20 2b 3d 20 70 49  i++) sumEq += pI
2cd77 64 78 2d 3e 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e  dx->aSample[i].n
2cd78 45 71 3b 0d 0a 20 20 20 20 20 20 20 20 70 49 64  Eq;..        pId
2cd79 78 2d 3e 61 76 67 45 71 20 3d 20 28 70 53 61 6d  x->avgEq = (pSam
2cd7a 70 6c 65 2d 3e 6e 4c 74 20 2d 20 73 75 6d 45 71  ple->nLt - sumEq
2cd7b 29 2f 70 53 61 6d 70 6c 65 2d 3e 6e 44 4c 74 3b  )/pSample->nDLt;
2cd7c 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
2cd7d 20 69 66 28 20 70 49 64 78 2d 3e 61 76 67 45 71   if( pIdx->avgEq
2cd7e 3c 3d 30 20 29 20 70 49 64 78 2d 3e 61 76 67 45  <=0 ) pIdx->avgE
2cd7f 71 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  q = 1;..    }.. 
2cd80 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74     eType = sqlit
2cd81 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70  e3_column_type(p
2cd82 53 74 6d 74 2c 20 34 29 3b 0d 0a 20 20 20 20 70  Stmt, 4);..    p
2cd83 53 61 6d 70 6c 65 2d 3e 65 54 79 70 65 20 3d 20  Sample->eType = 
2cd84 28 75 38 29 65 54 79 70 65 3b 0d 0a 20 20 20 20  (u8)eType;..    
2cd85 73 77 69 74 63 68 28 20 65 54 79 70 65 20 29 7b  switch( eType ){
2cd86 0d 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  ..      case SQL
2cd87 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0d 0a  ITE_INTEGER: {..
2cd88 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d          pSample-
2cd89 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f 63  >u.i = sqlite3_c
2cd8a 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d  olumn_int64(pStm
2cd8b 74 2c 20 34 29 3b 0d 0a 20 20 20 20 20 20 20 20  t, 4);..        
2cd8c 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d  break;..      }.
2cd8d 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
2cd8e 54 45 5f 46 4c 4f 41 54 3a 20 7b 0d 0a 20 20 20  TE_FLOAT: {..   
2cd8f 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75 2e       pSample->u.
2cd90 72 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r = sqlite3_colu
2cd91 6d 6e 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c  mn_double(pStmt,
2cd92 20 34 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 72   4);..        br
2cd93 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
2cd94 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
2cd95 5f 4e 55 4c 4c 3a 20 7b 0d 0a 20 20 20 20 20 20  _NULL: {..      
2cd96 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
2cd97 7d 0d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  }..      default
2cd98 3a 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d  : assert( eType=
2cd99 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20  =SQLITE_TEXT || 
2cd9a 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c  eType==SQLITE_BL
2cd9b 4f 42 20 29 3b 20 7b 0d 0a 20 20 20 20 20 20 20  OB ); {..       
2cd9c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
2cd9d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 28   (const char *)(
2cd9e 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2cd9f 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42  (eType==SQLITE_B
2cda0 4c 4f 42 29 20 3f 0d 0a 20 20 20 20 20 20 20 20  LOB) ?..        
2cda1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
2cda2 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  lumn_blob(pStmt,
2cda3 20 34 29 3a 0d 0a 20 20 20 20 20 20 20 20 20 20   4):..          
2cda4 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
2cda5 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 34  mn_text(pStmt, 4
2cda6 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20 29 3b  )..           );
2cda7 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20  ..        int n 
2cda8 3d 20 7a 20 3f 20 73 71 6c 69 74 65 33 5f 63 6f  = z ? sqlite3_co
2cda9 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d 74  lumn_bytes(pStmt
2cdaa 2c 20 34 29 20 3a 20 30 3b 0d 0a 20 20 20 20 20  , 4) : 0;..     
2cdab 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 42 79 74     pSample->nByt
2cdac 65 20 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 20 20  e = n;..        
2cdad 69 66 28 20 6e 20 3c 20 31 29 7b 0d 0a 20 20 20  if( n < 1){..   
2cdae 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e         pSample->
2cdaf 75 2e 7a 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  u.z = 0;..      
2cdb0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
2cdb1 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a      pSample->u.z
2cdb2 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
2cdb3 28 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  (n);..          
2cdb4 69 66 28 20 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a  if( pSample->u.z
2cdb5 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
2cdb6 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2cdb7 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20  iled = 1;..     
2cdb8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
2cdb9 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0d  inalize(pStmt);.
2cdba 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
2cdbb 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2cdbc 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
2cdbd 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
2cdbe 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 2c 20 7a  (pSample->u.z, z
2cdbf 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d  , n);..        }
2cdc0 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
2cdc1 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2cdc2 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2cdc3 28 70 53 74 6d 74 29 3b 0d 0a 7d 0d 0a 23 65 6e  (pStmt);..}..#en
2cdc4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
2cdc5 41 42 4c 45 5f 53 54 41 54 33 20 2a 2f 0d 0a 0d  ABLE_STAT3 */...
2cdc6 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 61 64 20 74 68 65  ./*..** Load the
2cdc7 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
2cdc8 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64  sqlite_stat1 and
2cdc9 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61   sqlite_stat3 ta
2cdca 62 6c 65 73 2e 20 54 68 65 0d 0a 2a 2a 20 63 6f  bles. The..** co
2cdcb 6e 74 65 6e 74 73 20 6f 66 20 73 71 6c 69 74 65  ntents of sqlite
2cdcc 5f 73 74 61 74 31 20 61 72 65 20 75 73 65 64 20  _stat1 are used 
2cdcd 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20  to populate the 
2cdce 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d  Index.aiRowEst[]
2cdcf 0d 0a 2a 2a 20 61 72 72 61 79 73 2e 20 54 68 65  ..** arrays. The
2cdd0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 71 6c   contents of sql
2cdd1 69 74 65 5f 73 74 61 74 33 20 61 72 65 20 75 73  ite_stat3 are us
2cdd2 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74  ed to populate t
2cdd3 68 65 0d 0a 2a 2a 20 49 6e 64 65 78 2e 61 53 61  he..** Index.aSa
2cdd4 6d 70 6c 65 5b 5d 20 61 72 72 61 79 73 2e 0d 0a  mple[] arrays...
2cdd5 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 73 71  **..** If the sq
2cdd6 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
2cdd7 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20   is not present 
2cdd8 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  in the database,
2cdd9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 0d 0a 2a   SQLITE_ERROR..*
2cdda 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  * is returned. I
2cddb 6e 20 74 68 69 73 20 63 61 73 65 2c 20 65 76 65  n this case, eve
2cddc 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  n if SQLITE_ENAB
2cddd 4c 45 5f 53 54 41 54 33 20 77 61 73 20 64 65 66  LE_STAT3 was def
2cdde 69 6e 65 64 20 0d 0a 2a 2a 20 64 75 72 69 6e 67  ined ..** during
2cddf 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e 64   compilation and
2cde0 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2cde1 33 20 74 61 62 6c 65 20 69 73 20 70 72 65 73 65  3 table is prese
2cde2 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69 73 20 0d  nt, no data is .
2cde3 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 69 74  .** read from it
2cde4 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 53 51 4c  ...**..** If SQL
2cde5 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
2cde6 20 77 61 73 20 64 65 66 69 6e 65 64 20 64 75 72   was defined dur
2cde7 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ing compilation 
2cde8 61 6e 64 20 74 68 65 20 0d 0a 2a 2a 20 73 71 6c  and the ..** sql
2cde9 69 74 65 5f 73 74 61 74 33 20 74 61 62 6c 65 20  ite_stat3 table 
2cdea 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69  is not present i
2cdeb 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
2cdec 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 0d  SQLITE_ERROR is.
2cded 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 48 6f  .** returned. Ho
2cdee 77 65 76 65 72 2c 20 69 6e 20 74 68 69 73 20 63  wever, in this c
2cdef 61 73 65 2c 20 64 61 74 61 20 69 73 20 72 65 61  ase, data is rea
2cdf0 64 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74  d from the sqlit
2cdf1 65 5f 73 74 61 74 31 0d 0a 2a 2a 20 74 61 62 6c  e_stat1..** tabl
2cdf2 65 20 28 69 66 20 69 74 20 69 73 20 70 72 65 73  e (if it is pres
2cdf3 65 6e 74 29 20 62 65 66 6f 72 65 20 72 65 74 75  ent) before retu
2cdf4 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  rning...**..** I
2cdf5 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f an OOM error o
2cdf6 63 63 75 72 73 2c 20 74 68 69 73 20 66 75 6e 63  ccurs, this func
2cdf7 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 65 74 73  tion always sets
2cdf8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cdf9 64 2e 0d 0a 2a 2a 20 54 68 69 73 20 6d 65 61 6e  d...** This mean
2cdfa 73 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20  s if the caller 
2cdfb 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62  does not care ab
2cdfc 6f 75 74 20 6f 74 68 65 72 20 65 72 72 6f 72 73  out other errors
2cdfd 2c 20 74 68 65 20 72 65 74 75 72 6e 0d 0a 2a 2a  , the return..**
2cdfe 20 63 6f 64 65 20 6d 61 79 20 62 65 20 69 67 6e   code may be ign
2cdff 6f 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ored...*/..SQLIT
2ce00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2ce01 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
2ce02 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  d(sqlite3 *db, i
2ce03 6e 74 20 69 44 62 29 7b 0d 0a 20 20 61 6e 61 6c  nt iDb){..  anal
2ce04 79 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0d  ysisInfo sInfo;.
2ce05 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0d  .  HashElem *i;.
2ce06 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0d 0a  .  char *zSql;..
2ce07 20 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 61    int rc;....  a
2ce08 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
2ce09 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d   iDb<db->nDb );.
2ce0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
2ce0b 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29  Db[iDb].pBt!=0 )
2ce0c 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 6c 65 61 72 20  ;....  /* Clear 
2ce0d 61 6e 79 20 70 72 69 6f 72 20 73 74 61 74 69 73  any prior statis
2ce0e 74 69 63 73 20 2a 2f 0d 0a 20 20 61 73 73 65 72  tics */..  asser
2ce0f 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  t( sqlite3Schema
2ce10 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44  MutexHeld(db, iD
2ce11 62 2c 20 30 29 20 29 3b 0d 0a 20 20 66 6f 72 28  b, 0) );..  for(
2ce12 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  i=sqliteHashFirs
2ce13 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t(&db->aDb[iDb].
2ce14 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
2ce15 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68  );i;i=sqliteHash
2ce16 4e 65 78 74 28 69 29 29 7b 0d 0a 20 20 20 20 49  Next(i)){..    I
2ce17 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c  ndex *pIdx = sql
2ce18 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0d  iteHashData(i);.
2ce19 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 61  .    sqlite3Defa
2ce1a 75 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b  ultRowEst(pIdx);
2ce1b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2ce1c 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20 20  ENABLE_STAT3..  
2ce1d 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49    sqlite3DeleteI
2ce1e 6e 64 65 78 53 61 6d 70 6c 65 73 28 64 62 2c 20  ndexSamples(db, 
2ce1f 70 49 64 78 29 3b 0d 0a 20 20 20 20 70 49 64 78  pIdx);..    pIdx
2ce20 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 30 3b 0d 0a  ->aSample = 0;..
2ce21 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20  #endif..  }.... 
2ce22 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b   /* Check to mak
2ce23 65 20 73 75 72 65 20 74 68 65 20 73 71 6c 69 74  e sure the sqlit
2ce24 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65 78  e_stat1 table ex
2ce25 69 73 74 73 20 2a 2f 0d 0a 20 20 73 49 6e 66 6f  ists */..  sInfo
2ce26 2e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 73 49 6e  .db = db;..  sIn
2ce27 66 6f 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64  fo.zDatabase = d
2ce28 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2ce29 65 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  e;..  if( sqlite
2ce2a 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
2ce2b 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 73  sqlite_stat1", s
2ce2c 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d  Info.zDatabase)=
2ce2d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
2ce2e 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
2ce2f 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 61  .  }....  /* Loa
2ce30 64 20 6e 65 77 20 73 74 61 74 69 73 74 69 63 73  d new statistics
2ce31 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71 6c 69   out of the sqli
2ce32 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a  te_stat1 table *
2ce33 2f 0d 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69  /..  zSql = sqli
2ce34 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0d  te3MPrintf(db, .
2ce35 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 74  .      "SELECT t
2ce36 62 6c 2c 69 64 78 2c 73 74 61 74 20 46 52 4f 4d  bl,idx,stat FROM
2ce37 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
2ce38 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
2ce39 73 65 29 3b 0d 0a 20 20 69 66 28 20 7a 53 71 6c  se);..  if( zSql
2ce3a 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ==0 ){..    rc =
2ce3b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
2ce3c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63    }else{..    rc
2ce3d 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
2ce3e 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73  db, zSql, analys
2ce3f 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f  isLoader, &sInfo
2ce40 2c 20 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  , 0);..    sqlit
2ce41 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71  e3DbFree(db, zSq
2ce42 6c 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20 20  l);..  }......  
2ce43 2f 2a 20 4c 6f 61 64 20 74 68 65 20 73 74 61 74  /* Load the stat
2ce44 69 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20  istics from the 
2ce45 73 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61 62  sqlite_stat3 tab
2ce46 6c 65 2e 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53  le. */..#ifdef S
2ce47 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2ce48 54 33 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  T3..  if( rc==SQ
2ce49 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
2ce4a 72 63 20 3d 20 6c 6f 61 64 53 74 61 74 33 28 64  rc = loadStat3(d
2ce4b 62 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  b, sInfo.zDataba
2ce4c 73 65 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  se);..  }..#endi
2ce4d 66 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  f....  if( rc==S
2ce4e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0d 0a  QLITE_NOMEM ){..
2ce4f 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2ce50 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a  iled = 1;..  }..
2ce51 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
2ce52 0a 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .....#endif /* S
2ce53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
2ce54 5a 45 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  ZE */..../******
2ce55 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2ce56 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  analyze.c ******
2ce57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce59 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
2ce5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2ce5b 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20 2a 2a  file attach.c **
2ce5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce5e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
2ce5f 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0d 0a  * 2003 April 6..
2ce60 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
2ce61 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
2ce62 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
2ce63 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
2ce64 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
2ce65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2ce66 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
2ce67 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
2ce68 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2ce69 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
2ce6a 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2ce6b 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2ce6c 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2ce6d 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
2ce6e 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2ce6f 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2ce70 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2ce71 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
2ce72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce76 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  ******..** This 
2ce77 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
2ce78 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
2ce79 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48 20  ment the ATTACH 
2ce7a 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61  and DETACH comma
2ce7b 6e 64 73 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 6e  nds...*/....#ifn
2ce7c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2ce7d 41 54 54 41 43 48 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ATTACH../*..** R
2ce7e 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 73  esolve an expres
2ce7f 73 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61  sion that was pa
2ce80 72 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20  rt of an ATTACH 
2ce81 6f 72 20 44 45 54 41 43 48 20 73 74 61 74 65 6d  or DETACH statem
2ce82 65 6e 74 2e 20 54 68 69 73 0d 0a 2a 2a 20 69 73  ent. This..** is
2ce83 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72   slightly differ
2ce84 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69  ent from resolvi
2ce85 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20  ng a normal SQL 
2ce86 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61  expression, beca
2ce87 75 73 65 20 73 69 6d 70 6c 65 0d 0a 2a 2a 20 69  use simple..** i
2ce88 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 74  dentifiers are t
2ce89 72 65 61 74 65 64 20 61 73 20 73 74 72 69 6e 67  reated as string
2ce8a 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  s, not possible 
2ce8b 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20  column names or 
2ce8c 61 6c 69 61 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  aliases...**..**
2ce8d 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72   i.e. if the par
2ce8e 73 65 72 20 73 65 65 73 3a 0d 0a 2a 2a 0d 0a 2a  ser sees:..**..*
2ce8f 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 41 54  *     ATTACH DAT
2ce90 41 42 41 53 45 20 61 62 63 20 41 53 20 64 65 66  ABASE abc AS def
2ce91 0d 0a 2a 2a 0d 0a 2a 2a 20 69 74 20 74 72 65 61  ..**..** it trea
2ce92 74 73 20 74 68 65 20 74 77 6f 20 65 78 70 72 65  ts the two expre
2ce93 73 73 69 6f 6e 73 20 61 73 20 6c 69 74 65 72 61  ssions as litera
2ce94 6c 20 73 74 72 69 6e 67 73 20 27 61 62 63 27 20  l strings 'abc' 
2ce95 61 6e 64 20 27 64 65 66 27 20 69 6e 73 74 65 61  and 'def' instea
2ce96 64 20 6f 66 0d 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67  d of..** looking
2ce97 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   for columns of 
2ce98 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0d 0a  the same name...
2ce99 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79  **..** This only
2ce9a 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65 20   applies to the 
2ce9b 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45 78  root node of pEx
2ce9c 70 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74 65  pr, so the state
2ce9d 6d 65 6e 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ment:..**..**   
2ce9e 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
2ce9f 45 20 61 62 63 7c 7c 64 65 66 20 41 53 20 27 64  E abc||def AS 'd
2cea0 62 32 27 0d 0a 2a 2a 0d 0a 2a 2a 20 77 69 6c 6c  b2'..**..** will
2cea1 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 6e 65   fail because ne
2cea2 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 65 66  ither abc or def
2cea3 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 65 64   can be resolved
2cea4 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
2cea5 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45  t resolveAttachE
2cea6 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20  xpr(NameContext 
2cea7 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45  *pName, Expr *pE
2cea8 78 70 72 29 0d 0a 7b 0d 0a 20 20 69 6e 74 20 72  xpr)..{..  int r
2cea9 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
2ceaa 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0d 0a    if( pExpr ){..
2ceab 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f      if( pExpr->o
2ceac 70 21 3d 54 4b 5f 49 44 20 29 7b 0d 0a 20 20 20  p!=TK_ID ){..   
2cead 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52     rc = sqlite3R
2ceae 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2ceaf 70 4e 61 6d 65 2c 20 70 45 78 70 72 29 3b 0d 0a  pName, pExpr);..
2ceb0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
2ceb1 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69  LITE_OK && !sqli
2ceb2 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
2ceb3 74 28 70 45 78 70 72 29 20 29 7b 0d 0a 20 20 20  t(pExpr) ){..   
2ceb4 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2ceb5 72 4d 73 67 28 70 4e 61 6d 65 2d 3e 70 50 61 72  rMsg(pName->pPar
2ceb6 73 65 2c 20 22 69 6e 76 61 6c 69 64 20 6e 61 6d  se, "invalid nam
2ceb7 65 3a 20 5c 22 25 73 5c 22 22 2c 20 70 45 78 70  e: \"%s\"", pExp
2ceb8 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0d 0a 20  r->u.zToken);.. 
2ceb9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
2ceba 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20  LITE_ERROR;..   
2cebb 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b     }..    }else{
2cebc 0d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f  ..      pExpr->o
2cebd 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0d 0a  p = TK_STRING;..
2cebe 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
2cebf 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
2cec0 2a 0d 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65  *..** An SQL use
2cec1 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  r-function regis
2cec2 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20  tered to do the 
2cec3 77 6f 72 6b 20 6f 66 20 61 6e 20 41 54 54 41 43  work of an ATTAC
2cec4 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  H statement. The
2cec5 0d 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75 6d  ..** three argum
2cec6 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
2cec7 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63 74  tion come direct
2cec8 6c 79 20 66 72 6f 6d 20 61 6e 20 61 74 74 61 63  ly from an attac
2cec9 68 20 73 74 61 74 65 6d 65 6e 74 3a 0d 0a 2a 2a  h statement:..**
2ceca 0d 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  ..**     ATTACH 
2cecb 44 41 54 41 42 41 53 45 20 78 20 41 53 20 79 20  DATABASE x AS y 
2cecc 4b 45 59 20 7a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  KEY z..**..**   
2cecd 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 5f    SELECT sqlite_
2cece 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29 0d  attach(x, y, z).
2cecf 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 6f  .**..** If the o
2ced0 70 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20  ptional "KEY z" 
2ced1 73 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65  syntax is omitte
2ced2 64 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69  d, an SQL NULL i
2ced3 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0d  s passed as the.
2ced4 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65  .** third argume
2ced5 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  nt...*/..static 
2ced6 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e 63 28  void attachFunc(
2ced7 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ..  sqlite3_cont
2ced8 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20  ext *context,.. 
2ced9 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20   int NotUsed,.. 
2ceda 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2cedb 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 69 6e 74  *argv..){..  int
2cedc 20 69 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20   i;..  int rc = 
2cedd 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  0;..  sqlite3 *d
2cede 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
2cedf 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
2cee0 6e 74 65 78 74 29 3b 0d 0a 20 20 63 6f 6e 73 74  ntext);..  const
2cee1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0d 0a 20   char *zName;.. 
2cee2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
2cee3 6c 65 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 50 61  le;..  char *zPa
2cee4 74 68 20 3d 20 30 3b 0d 0a 20 20 63 68 61 72 20  th = 0;..  char 
2cee5 2a 7a 45 72 72 20 3d 20 30 3b 0d 0a 20 20 75 6e  *zErr = 0;..  un
2cee6 73 69 67 6e 65 64 20 69 6e 74 20 66 6c 61 67 73  signed int flags
2cee7 3b 0d 0a 20 20 44 62 20 2a 61 4e 65 77 3b 0d 0a  ;..  Db *aNew;..
2cee8 20 20 63 68 61 72 20 2a 7a 45 72 72 44 79 6e 20    char *zErrDyn 
2cee9 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
2ceea 76 66 73 20 2a 70 56 66 73 3b 0d 0a 0d 0a 20 20  vfs *pVfs;....  
2ceeb 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2ceec 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 0d 0a 20 20  (NotUsed);....  
2ceed 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63  zFile = (const c
2ceee 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
2ceef 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2cef0 29 3b 0d 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63  );..  zName = (c
2cef1 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
2cef2 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2cef3 72 67 76 5b 31 5d 29 3b 0d 0a 20 20 69 66 28 20  rgv[1]);..  if( 
2cef4 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65  zFile==0 ) zFile
2cef5 20 3d 20 22 22 3b 0d 0a 20 20 69 66 28 20 7a 4e   = "";..  if( zN
2cef6 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d  ame==0 ) zName =
2cef7 20 22 22 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65   "";....  /* Che
2cef8 63 6b 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ck for the follo
2cef9 77 69 6e 67 20 65 72 72 6f 72 73 3a 0d 0a 20 20  wing errors:..  
2cefa 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54  **..  **     * T
2cefb 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64  oo many attached
2cefc 20 64 61 74 61 62 61 73 65 73 2c 0d 0a 20 20 2a   databases,..  *
2cefd 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63 74  *     * Transact
2cefe 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  ion currently op
2ceff 65 6e 0d 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53  en..  **     * S
2cf00 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73  pecified databas
2cf01 65 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62  e name already b
2cf02 65 69 6e 67 20 75 73 65 64 2e 0d 0a 20 20 2a 2f  eing used...  */
2cf03 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e  ..  if( db->nDb>
2cf04 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
2cf05 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45  TE_LIMIT_ATTACHE
2cf06 44 5d 2b 32 20 29 7b 0d 0a 20 20 20 20 7a 45 72  D]+2 ){..    zEr
2cf07 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
2cf08 72 69 6e 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d  rintf(db, "too m
2cf09 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74  any attached dat
2cf0a 61 62 61 73 65 73 20 2d 20 6d 61 78 20 25 64 22  abases - max %d"
2cf0b 2c 20 0d 0a 20 20 20 20 20 20 64 62 2d 3e 61 4c  , ..      db->aL
2cf0c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2cf0d 54 5f 41 54 54 41 43 48 45 44 5d 0d 0a 20 20 20  T_ATTACHED]..   
2cf0e 20 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 61 74   );..    goto at
2cf0f 74 61 63 68 5f 65 72 72 6f 72 3b 0d 0a 20 20 7d  tach_error;..  }
2cf10 0d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74  ..  if( !db->aut
2cf11 6f 43 6f 6d 6d 69 74 20 29 7b 0d 0a 20 20 20 20  oCommit ){..    
2cf12 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2cf13 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61  3MPrintf(db, "ca
2cf14 6e 6e 6f 74 20 41 54 54 41 43 48 20 64 61 74 61  nnot ATTACH data
2cf15 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61 6e  base within tran
2cf16 73 61 63 74 69 6f 6e 22 29 3b 0d 0a 20 20 20 20  saction");..    
2cf17 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f  goto attach_erro
2cf18 72 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69  r;..  }..  for(i
2cf19 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
2cf1a 2b 2b 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a  ++){..    char *
2cf1b 7a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  z = db->aDb[i].z
2cf1c 4e 61 6d 65 3b 0d 0a 20 20 20 20 61 73 73 65 72  Name;..    asser
2cf1d 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20 29 3b  t( z && zName );
2cf1e 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
2cf1f 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d  3StrICmp(z, zNam
2cf20 65 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  e)==0 ){..      
2cf21 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2cf22 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61  3MPrintf(db, "da
2cf23 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72  tabase %s is alr
2cf24 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e  eady in use", zN
2cf25 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74  ame);..      got
2cf26 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0d  o attach_error;.
2cf27 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
2cf28 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
2cf29 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74 68   new entry in th
2cf2a 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
2cf2b 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65  y and initialise
2cf2c 20 74 68 65 20 73 63 68 65 6d 61 0d 0a 20 20 2a   the schema..  *
2cf2d 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0d 0a  * hash tables...
2cf2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e    */..  if( db->
2cf2f 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74  aDb==db->aDbStat
2cf30 69 63 20 29 7b 0d 0a 20 20 20 20 61 4e 65 77 20  ic ){..    aNew 
2cf31 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2cf32 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2cf33 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b  db->aDb[0])*3 );
2cf34 0d 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d  ..    if( aNew==
2cf35 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20  0 ) return;..   
2cf36 20 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62   memcpy(aNew, db
2cf37 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62  ->aDb, sizeof(db
2cf38 2d 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0d 0a 20  ->aDb[0])*2);.. 
2cf39 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 4e 65   }else{..    aNe
2cf3a 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
2cf3b 6c 6c 6f 63 28 64 62 2c 20 64 62 2d 3e 61 44 62  lloc(db, db->aDb
2cf3c 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  , sizeof(db->aDb
2cf3d 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29  [0])*(db->nDb+1)
2cf3e 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 61 4e 65   );..    if( aNe
2cf3f 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  w==0 ) return;..
2cf40 20 20 7d 0d 0a 20 20 64 62 2d 3e 61 44 62 20 3d    }..  db->aDb =
2cf41 20 61 4e 65 77 3b 0d 0a 20 20 61 4e 65 77 20 3d   aNew;..  aNew =
2cf42 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44   &db->aDb[db->nD
2cf43 62 5d 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 61 4e  b];..  memset(aN
2cf44 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61  ew, 0, sizeof(*a
2cf45 4e 65 77 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4f  New));....  /* O
2cf46 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
2cf47 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 62 74   file. If the bt
2cf48 72 65 65 20 69 73 20 73 75 63 63 65 73 73 66 75  ree is successfu
2cf49 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73 65 0d  lly opened, use.
2cf4a 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74 61  .  ** it to obta
2cf4b 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
2cf4c 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73 20  schema. At this 
2cf4d 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d 61  point the schema
2cf4e 20 6d 61 79 0d 0a 20 20 2a 2a 20 6f 72 20 6d 61   may..  ** or ma
2cf4f 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  y not be initial
2cf50 69 73 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66  ised...  */..  f
2cf51 6c 61 67 73 20 3d 20 64 62 2d 3e 6f 70 65 6e 46  lags = db->openF
2cf52 6c 61 67 73 3b 0d 0a 20 20 72 63 20 3d 20 73 71  lags;..  rc = sq
2cf53 6c 69 74 65 33 50 61 72 73 65 55 72 69 28 64 62  lite3ParseUri(db
2cf54 2d 3e 70 56 66 73 2d 3e 7a 4e 61 6d 65 2c 20 7a  ->pVfs->zName, z
2cf55 46 69 6c 65 2c 20 26 66 6c 61 67 73 2c 20 26 70  File, &flags, &p
2cf56 56 66 73 2c 20 26 7a 50 61 74 68 2c 20 26 7a 45  Vfs, &zPath, &zE
2cf57 72 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d  rr);..  if( rc!=
2cf58 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
2cf59 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2cf5a 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c  _NOMEM ) db->mal
2cf5b 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a  locFailed = 1;..
2cf5c 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2cf5d 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
2cf5e 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 20 20  , zErr, -1);..  
2cf5f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
2cf60 45 72 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  Err);..    retur
2cf61 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  n;..  }..  asser
2cf62 74 28 20 70 56 66 73 20 29 3b 0d 0a 20 20 66 6c  t( pVfs );..  fl
2cf63 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50  ags |= SQLITE_OP
2cf64 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0d 0a 20 20 72  EN_MAIN_DB;..  r
2cf65 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2cf66 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
2cf67 2c 20 64 62 2c 20 26 61 4e 65 77 2d 3e 70 42 74  , db, &aNew->pBt
2cf68 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20  , 0, flags);..  
2cf69 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 7a 50  sqlite3_free( zP
2cf6a 61 74 68 20 29 3b 0d 0a 20 20 64 62 2d 3e 6e 44  ath );..  db->nD
2cf6b 62 2b 2b 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d  b++;..  if( rc==
2cf6c 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
2cf6d 54 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  T ){..    rc = S
2cf6e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
2cf6f 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69    zErrDyn = sqli
2cf70 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2cf71 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65  database is alre
2cf72 61 64 79 20 61 74 74 61 63 68 65 64 22 29 3b 0d  ady attached");.
2cf73 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d  .  }else if( rc=
2cf74 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
2cf75 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
2cf76 3b 0d 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 63  ;..    aNew->pSc
2cf77 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63  hema = sqlite3Sc
2cf78 68 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 77  hemaGet(db, aNew
2cf79 2d 3e 70 42 74 29 3b 0d 0a 20 20 20 20 69 66 28  ->pBt);..    if(
2cf7a 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20   !aNew->pSchema 
2cf7b 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ){..      rc = S
2cf7c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
2cf7d 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77    }else if( aNew
2cf7e 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
2cf7f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e  format && aNew->
2cf80 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e  pSchema->enc!=EN
2cf81 43 28 64 62 29 20 29 7b 0d 0a 20 20 20 20 20 20  C(db) ){..      
2cf82 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2cf83 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0d 0a 20  3MPrintf(db, .. 
2cf84 20 20 20 20 20 20 20 22 61 74 74 61 63 68 65 64         "attached
2cf85 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20   databases must 
2cf86 75 73 65 20 74 68 65 20 73 61 6d 65 20 74 65 78  use the same tex
2cf87 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61  t encoding as ma
2cf88 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0d 0a  in database");..
2cf89 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2cf8a 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 7d 0d  E_ERROR;..    }.
2cf8b 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71  .    pPager = sq
2cf8c 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
2cf8d 61 4e 65 77 2d 3e 70 42 74 29 3b 0d 0a 20 20 20  aNew->pBt);..   
2cf8e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
2cf8f 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
2cf90 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
2cf91 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
2cf92 42 74 72 65 65 53 65 63 75 72 65 44 65 6c 65 74  BtreeSecureDelet
2cf93 65 28 61 4e 65 77 2d 3e 70 42 74 2c 0d 0a 20 20  e(aNew->pBt,..  
2cf94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cf95 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2cf96 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65 6c  e3BtreeSecureDel
2cf97 65 74 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ete(db->aDb[0].p
2cf98 42 74 2c 2d 31 29 20 29 3b 0d 0a 20 20 7d 0d 0a  Bt,-1) );..  }..
2cf99 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c    aNew->safety_l
2cf9a 65 76 65 6c 20 3d 20 33 3b 0d 0a 20 20 61 4e 65  evel = 3;..  aNe
2cf9b 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  w->zName = sqlit
2cf9c 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
2cf9d 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28 20 72 63  Name);..  if( rc
2cf9e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61  ==SQLITE_OK && a
2cf9f 4e 65 77 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b  New->zName==0 ){
2cfa0 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
2cfa1 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d  E_NOMEM;..  }...
2cfa2 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
2cfa3 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 20 20 69 66  _HAS_CODEC..  if
2cfa4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2cfa5 29 7b 0d 0a 20 20 20 20 65 78 74 65 72 6e 20 69  ){..    extern i
2cfa6 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41  nt sqlite3CodecA
2cfa7 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20  ttach(sqlite3*, 
2cfa8 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
2cfa9 2c 20 69 6e 74 29 3b 0d 0a 20 20 20 20 65 78 74  , int);..    ext
2cfaa 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ern void sqlite3
2cfab 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69  CodecGetKey(sqli
2cfac 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a  te3*, int, void*
2cfad 2a 2c 20 69 6e 74 2a 29 3b 0d 0a 20 20 20 20 69  *, int*);..    i
2cfae 6e 74 20 6e 4b 65 79 3b 0d 0a 20 20 20 20 63 68  nt nKey;..    ch
2cfaf 61 72 20 2a 7a 4b 65 79 3b 0d 0a 20 20 20 20 69  ar *zKey;..    i
2cfb0 6e 74 20 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nt t = sqlite3_v
2cfb1 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32  alue_type(argv[2
2cfb2 5d 29 3b 0d 0a 20 20 20 20 73 77 69 74 63 68 28  ]);..    switch(
2cfb3 20 74 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 73   t ){..      cas
2cfb4 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
2cfb5 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  :..      case SQ
2cfb6 4c 49 54 45 5f 46 4c 4f 41 54 3a 0d 0a 20 20 20  LITE_FLOAT:..   
2cfb7 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
2cfb8 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2cfb9 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b 65 79 20  b, "Invalid key 
2cfba 76 61 6c 75 65 22 29 3b 0d 0a 20 20 20 20 20 20  value");..      
2cfbb 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2cfbc 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  ROR;..        br
2cfbd 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 0d 0a  eak;..        ..
2cfbe 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
2cfbf 45 5f 54 45 58 54 3a 0d 0a 20 20 20 20 20 20 63  E_TEXT:..      c
2cfc0 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
2cfc1 0d 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d  ..        nKey =
2cfc2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2cfc3 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0d 0a  ytes(argv[2]);..
2cfc4 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28          zKey = (
2cfc5 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
2cfc6 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32  alue_blob(argv[2
2cfc7 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  ]);..        rc 
2cfc8 3d 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74  = sqlite3CodecAt
2cfc9 74 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62  tach(db, db->nDb
2cfca 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b  -1, zKey, nKey);
2cfcb 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
2cfcc 0d 0a 0d 0a 20 20 20 20 20 20 63 61 73 65 20 53  ....      case S
2cfcd 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0d 0a 20 20 20  QLITE_NULL:..   
2cfce 20 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79 20 73       /* No key s
2cfcf 70 65 63 69 66 69 65 64 2e 20 20 55 73 65 20 74  pecified.  Use t
2cfd0 68 65 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20  he key from the 
2cfd1 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  main database */
2cfd2 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
2cfd3 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62 2c  3CodecGetKey(db,
2cfd4 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65   0, (void**)&zKe
2cfd5 79 2c 20 26 6e 4b 65 79 29 3b 0d 0a 20 20 20 20  y, &nKey);..    
2cfd6 20 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 20 7c      if( nKey>0 |
2cfd7 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  | sqlite3BtreeGe
2cfd8 74 52 65 73 65 72 76 65 28 64 62 2d 3e 61 44 62  tReserve(db->aDb
2cfd9 5b 30 5d 2e 70 42 74 29 3e 30 20 29 7b 0d 0a 20  [0].pBt)>0 ){.. 
2cfda 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
2cfdb 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
2cfdc 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20  (db, db->nDb-1, 
2cfdd 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0d 0a 20 20  zKey, nKey);..  
2cfde 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
2cfdf 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
2cfe0 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20    }..#endif.... 
2cfe1 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
2cfe2 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63 63 65  was opened succe
2cfe3 73 73 66 75 6c 6c 79 2c 20 72 65 61 64 20 74 68  ssfully, read th
2cfe4 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65  e schema for the
2cfe5 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e 0d 0a   new database...
2cfe6 20 20 2a 2a 20 49 66 20 74 68 69 73 20 66 61 69    ** If this fai
2cfe7 6c 73 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69 6e  ls, or if openin
2cfe8 67 20 74 68 65 20 66 69 6c 65 20 66 61 69 6c 65  g the file faile
2cfe9 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68  d, then close th
2cfea 65 20 66 69 6c 65 20 61 6e 64 20 0d 0a 20 20 2a  e file and ..  *
2cfeb 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74  * remove the ent
2cfec 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e  ry from the db->
2cfed 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65  aDb[] array. i.e
2cfee 2e 20 70 75 74 20 65 76 65 72 79 74 68 69 6e 67  . put everything
2cfef 20 62 61 63 6b 20 74 68 65 20 77 61 79 0d 0a 20   back the way.. 
2cff0 20 2a 2a 20 77 65 20 66 6f 75 6e 64 20 69 74 2e   ** we found it.
2cff1 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63  ..  */..  if( rc
2cff2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
2cff3 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2cff4 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0d 0a 20  EnterAll(db);.. 
2cff5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
2cff6 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e  nit(db, &zErrDyn
2cff7 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  );..    sqlite3B
2cff8 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
2cff9 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 63  ;..  }..  if( rc
2cffa 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 44 62   ){..    int iDb
2cffb 20 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0d   = db->nDb - 1;.
2cffc 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
2cffd 3e 3d 32 20 29 3b 0d 0a 20 20 20 20 69 66 28 20  >=2 );..    if( 
2cffe 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
2cfff 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
2d000 65 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d  e3BtreeClose(db-
2d001 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b 0d  >aDb[iDb].pBt);.
2d002 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
2d003 44 62 5d 2e 70 42 74 20 3d 20 30 3b 0d 0a 20 20  Db].pBt = 0;..  
2d004 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
2d005 2e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0d 0a 20  .pSchema = 0;.. 
2d006 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
2d007 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
2d008 68 65 6d 61 28 64 62 2c 20 2d 31 29 3b 0d 0a 20  hema(db, -1);.. 
2d009 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 69 44 62     db->nDb = iDb
2d00a 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
2d00b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72  QLITE_NOMEM || r
2d00c 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
2d00d 4e 4f 4d 45 4d 20 29 7b 0d 0a 20 20 20 20 20 20  NOMEM ){..      
2d00e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2d00f 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 73 71 6c   = 1;..      sql
2d010 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2d011 45 72 72 44 79 6e 29 3b 0d 0a 20 20 20 20 20 20  ErrDyn);..      
2d012 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2d013 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6f 75  3MPrintf(db, "ou
2d014 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0d 0a  t of memory");..
2d015 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 45      }else if( zE
2d016 72 72 44 79 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20  rrDyn==0 ){..   
2d017 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
2d018 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2d019 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
2d01a 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a  database: %s", z
2d01b 46 69 6c 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  File);..    }.. 
2d01c 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
2d01d 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 20 20 0d 0a  rror;..  }..  ..
2d01e 20 20 72 65 74 75 72 6e 3b 0d 0a 0d 0a 61 74 74    return;....att
2d01f 61 63 68 5f 65 72 72 6f 72 3a 0d 0a 20 20 2f 2a  ach_error:..  /*
2d020 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
2d021 20 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20   if we get here 
2d022 2a 2f 0d 0a 20 20 69 66 28 20 7a 45 72 72 44 79  */..  if( zErrDy
2d023 6e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  n ){..    sqlite
2d024 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
2d025 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c  ontext, zErrDyn,
2d026 20 2d 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74   -1);..    sqlit
2d027 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
2d028 72 44 79 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69  rDyn);..  }..  i
2d029 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f  f( rc ) sqlite3_
2d02a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
2d02b 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0d  e(context, rc);.
2d02c 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20  .}..../*..** An 
2d02d 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  SQL user-functio
2d02e 6e 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20  n registered to 
2d02f 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61  do the work of a
2d030 6e 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65  n DETACH stateme
2d031 6e 74 2e 20 54 68 65 0d 0a 2a 2a 20 74 68 72 65  nt. The..** thre
2d032 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  e arguments to t
2d033 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65  he function come
2d034 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61   directly from a
2d035 20 64 65 74 61 63 68 20 73 74 61 74 65 6d 65 6e   detach statemen
2d036 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 44  t:..**..**     D
2d037 45 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78  ETACH DATABASE x
2d038 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53 45 4c  ..**..**     SEL
2d039 45 43 54 20 73 71 6c 69 74 65 5f 64 65 74 61 63  ECT sqlite_detac
2d03a 68 28 78 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  h(x)..*/..static
2d03b 20 76 6f 69 64 20 64 65 74 61 63 68 46 75 6e 63   void detachFunc
2d03c 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  (..  sqlite3_con
2d03d 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a  text *context,..
2d03e 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a    int NotUsed,..
2d03f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2d040 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 63 6f  **argv..){..  co
2d041 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
2d042 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
2d043 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2d044 78 74 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20  xt(argv[0]);..  
2d045 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
2d046 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
2d047 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
2d048 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 44  ;..  int i;..  D
2d049 62 20 2a 70 44 62 20 3d 20 30 3b 0d 0a 20 20 63  b *pDb = 0;..  c
2d04a 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0d 0a  har zErr[128];..
2d04b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
2d04c 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a  ETER(NotUsed);..
2d04d 0d 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ..  if( zName==0
2d04e 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0d 0a   ) zName = "";..
2d04f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2d050 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
2d051 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
2d052 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70 44 62  i];..    if( pDb
2d053 2d 3e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69  ->pBt==0 ) conti
2d054 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28 20 73 71  nue;..    if( sq
2d055 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 44 62  lite3StrICmp(pDb
2d056 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d  ->zName, zName)=
2d057 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 7d  =0 ) break;..  }
2d058 0d 0a 0d 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d  ....  if( i>=db-
2d059 3e 6e 44 62 20 29 7b 0d 0a 20 20 20 20 73 71 6c  >nDb ){..    sql
2d05a 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
2d05b 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
2d05c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61   "no such databa
2d05d 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  se: %s", zName);
2d05e 0d 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63  ..    goto detac
2d05f 68 5f 65 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 20  h_error;..  }.. 
2d060 20 69 66 28 20 69 3c 32 20 29 7b 0d 0a 20 20 20   if( i<2 ){..   
2d061 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
2d062 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a  f(sizeof(zErr),z
2d063 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74  Err, "cannot det
2d064 61 63 68 20 64 61 74 61 62 61 73 65 20 25 73 22  ach database %s"
2d065 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67  , zName);..    g
2d066 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
2d067 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21 64  ;..  }..  if( !d
2d068 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
2d069 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
2d06a 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
2d06b 72 72 29 2c 20 7a 45 72 72 2c 0d 0a 20 20 20 20  rr), zErr,..    
2d06c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d06d 20 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48 20   "cannot DETACH 
2d06e 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20  database within 
2d06f 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0d 0a  transaction");..
2d070 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
2d071 65 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 20 20 69  error;..  }..  i
2d072 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  f( sqlite3BtreeI
2d073 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62  sInReadTrans(pDb
2d074 2d 3e 70 42 74 29 20 7c 7c 20 73 71 6c 69 74 65  ->pBt) || sqlite
2d075 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70  3BtreeIsInBackup
2d076 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0d 0a 20  (pDb->pBt) ){.. 
2d077 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
2d078 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
2d079 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73 65  ,zErr, "database
2d07a 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20   %s is locked", 
2d07b 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67 6f 74  zName);..    got
2d07c 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0d  o detach_error;.
2d07d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  .  }....  sqlite
2d07e 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d  3BtreeClose(pDb-
2d07f 3e 70 42 74 29 3b 0d 0a 20 20 70 44 62 2d 3e 70  >pBt);..  pDb->p
2d080 42 74 20 3d 20 30 3b 0d 0a 20 20 70 44 62 2d 3e  Bt = 0;..  pDb->
2d081 70 53 63 68 65 6d 61 20 3d 20 30 3b 0d 0a 20 20  pSchema = 0;..  
2d082 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
2d083 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 2d  rnalSchema(db, -
2d084 31 29 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d 0a  1);..  return;..
2d085 0d 0a 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0d  ..detach_error:.
2d086 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2d087 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
2d088 20 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 7d 0d 0a   zErr, -1);..}..
2d089 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 70 72  ../*..** This pr
2d08a 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74 65  ocedure generate
2d08b 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20  s VDBE code for 
2d08c 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74  a single invocat
2d08d 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74 68  ion of either th
2d08e 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74  e..** sqlite_det
2d08f 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f  ach() or sqlite_
2d090 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65  attach() SQL use
2d091 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2f  r functions...*/
2d092 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  ..static void co
2d093 64 65 41 74 74 61 63 68 28 0d 0a 20 20 50 61 72  deAttach(..  Par
2d094 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2d095 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
2d096 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e  context */..  in
2d097 74 20 74 79 70 65 2c 20 20 20 20 20 20 20 20 20  t type,         
2d098 20 20 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c     /* Either SQL
2d099 49 54 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51  ITE_ATTACH or SQ
2d09a 4c 49 54 45 5f 44 45 54 41 43 48 20 2a 2f 0d 0a  LITE_DETACH */..
2d09b 20 20 46 75 6e 63 44 65 66 20 63 6f 6e 73 74 20    FuncDef const 
2d09c 2a 70 46 75 6e 63 2c 2f 2a 20 46 75 6e 63 44 65  *pFunc,/* FuncDe
2d09d 66 20 77 72 61 70 70 65 72 20 66 6f 72 20 64 65  f wrapper for de
2d09e 74 61 63 68 46 75 6e 63 28 29 20 6f 72 20 61 74  tachFunc() or at
2d09f 74 61 63 68 46 75 6e 63 28 29 20 2a 2f 0d 0a 20  tachFunc() */.. 
2d0a0 20 45 78 70 72 20 2a 70 41 75 74 68 41 72 67 2c   Expr *pAuthArg,
2d0a1 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
2d0a2 69 6f 6e 20 74 6f 20 70 61 73 73 20 74 6f 20 61  ion to pass to a
2d0a3 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c  uthorization cal
2d0a4 6c 62 61 63 6b 20 2a 2f 0d 0a 20 20 45 78 70 72  lback */..  Expr
2d0a5 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *pFilename,    
2d0a6 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
2d0a7 62 61 73 65 20 66 69 6c 65 20 2a 2f 0d 0a 20 20  base file */..  
2d0a8 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 20  Expr *pDbname,  
2d0a9 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2d0aa 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
2d0ab 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a  use internally *
2d0ac 2f 0d 0a 20 20 45 78 70 72 20 2a 70 4b 65 79 20  /..  Expr *pKey 
2d0ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2d0ae 61 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65 6e  abase key for en
2d0af 63 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73 69  cryption extensi
2d0b0 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  on */..){..  int
2d0b1 20 72 63 3b 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74   rc;..  NameCont
2d0b2 65 78 74 20 73 4e 61 6d 65 3b 0d 0a 20 20 56 64  ext sName;..  Vd
2d0b3 62 65 20 2a 76 3b 0d 0a 20 20 73 71 6c 69 74 65  be *v;..  sqlite
2d0b4 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3* db = pParse->
2d0b5 64 62 3b 0d 0a 20 20 69 6e 74 20 72 65 67 41 72  db;..  int regAr
2d0b6 67 73 3b 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28  gs;....  memset(
2d0b7 26 73 4e 61 6d 65 2c 20 30 2c 20 73 69 7a 65 6f  &sName, 0, sizeo
2d0b8 66 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b  f(NameContext));
2d0b9 0d 0a 20 20 73 4e 61 6d 65 2e 70 50 61 72 73 65  ..  sName.pParse
2d0ba 20 3d 20 70 50 61 72 73 65 3b 0d 0a 0d 0a 20 20   = pParse;....  
2d0bb 69 66 28 20 0d 0a 20 20 20 20 20 20 53 51 4c 49  if( ..      SQLI
2d0bc 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
2d0bd 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
2d0be 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61 6d 65  sName, pFilename
2d0bf 29 29 20 7c 7c 0d 0a 20 20 20 20 20 20 53 51 4c  )) ||..      SQL
2d0c0 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
2d0c1 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28  solveAttachExpr(
2d0c2 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65 29  &sName, pDbname)
2d0c3 29 20 7c 7c 0d 0a 20 20 20 20 20 20 53 51 4c 49  ) ||..      SQLI
2d0c4 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
2d0c5 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
2d0c6 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0d 0a 20  sName, pKey)).. 
2d0c7 20 29 7b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d   ){..    pParse-
2d0c8 3e 6e 45 72 72 2b 2b 3b 0d 0a 20 20 20 20 67 6f  >nErr++;..    go
2d0c9 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0d 0a  to attach_end;..
2d0ca 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53    }....#ifndef S
2d0cb 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2d0cc 52 49 5a 41 54 49 4f 4e 0d 0a 20 20 69 66 28 20  RIZATION..  if( 
2d0cd 70 41 75 74 68 41 72 67 20 29 7b 0d 0a 20 20 20  pAuthArg ){..   
2d0ce 20 63 68 61 72 20 2a 7a 41 75 74 68 41 72 67 3b   char *zAuthArg;
2d0cf 0d 0a 20 20 20 20 69 66 28 20 70 41 75 74 68 41  ..    if( pAuthA
2d0d0 72 67 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e  rg->op==TK_STRIN
2d0d1 47 20 29 7b 0d 0a 20 20 20 20 20 20 7a 41 75 74  G ){..      zAut
2d0d2 68 41 72 67 20 3d 20 70 41 75 74 68 41 72 67 2d  hArg = pAuthArg-
2d0d3 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20  >u.zToken;..    
2d0d4 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 7a 41  }else{..      zA
2d0d5 75 74 68 41 72 67 20 3d 20 30 3b 0d 0a 20 20 20  uthArg = 0;..   
2d0d6 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
2d0d7 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2d0d8 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74  arse, type, zAut
2d0d9 68 41 72 67 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  hArg, 0, 0);..  
2d0da 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f    if(rc!=SQLITE_
2d0db 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74  OK ){..      got
2d0dc 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0d 0a 20  o attach_end;.. 
2d0dd 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69     }..  }..#endi
2d0de 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2d0df 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a  _AUTHORIZATION *
2d0e0 2f 0d 0a 0d 0a 0d 0a 20 20 76 20 3d 20 73 71 6c  /......  v = sql
2d0e1 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2d0e2 73 65 29 3b 0d 0a 20 20 72 65 67 41 72 67 73 20  se);..  regArgs 
2d0e3 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2d0e4 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29  Range(pParse, 4)
2d0e5 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
2d0e6 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46 69  Code(pParse, pFi
2d0e7 6c 65 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 29  lename, regArgs)
2d0e8 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
2d0e9 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 44 62  Code(pParse, pDb
2d0ea 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 2b 31 29  name, regArgs+1)
2d0eb 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
2d0ec 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4b 65  Code(pParse, pKe
2d0ed 79 2c 20 72 65 67 41 72 67 73 2b 32 29 3b 0d 0a  y, regArgs+2);..
2d0ee 0d 0a 20 20 61 73 73 65 72 74 28 20 76 20 7c 7c  ..  assert( v ||
2d0ef 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2d0f0 64 20 29 3b 0d 0a 20 20 69 66 28 20 76 20 29 7b  d );..  if( v ){
2d0f1 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2d0f2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 75  eAddOp3(v, OP_Fu
2d0f3 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67 41 72  nction, 0, regAr
2d0f4 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41 72 67  gs+3-pFunc->nArg
2d0f5 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0d 0a 20  , regArgs+3);.. 
2d0f6 20 20 20 61 73 73 65 72 74 28 20 70 46 75 6e 63     assert( pFunc
2d0f7 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 28 70  ->nArg==-1 || (p
2d0f8 46 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66 66 29  Func->nArg&0xff)
2d0f9 3d 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20 29 3b  ==pFunc->nArg );
2d0fa 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2d0fb 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38  eChangeP5(v, (u8
2d0fc 29 28 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29 3b  )(pFunc->nArg));
2d0fd 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2d0fe 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
2d0ff 20 28 63 68 61 72 20 2a 29 70 46 75 6e 63 2c 20   (char *)pFunc, 
2d100 50 34 5f 46 55 4e 43 44 45 46 29 3b 0d 0a 0d 0a  P4_FUNCDEF);....
2d101 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f      /* Code an O
2d102 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 61 6e  P_Expire. For an
2d103 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
2d104 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 72 75  t, set P1 to tru
2d105 65 20 28 65 78 70 69 72 65 20 74 68 69 73 0d 0a  e (expire this..
2d106 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
2d107 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41   only). For DETA
2d108 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 61  CH, set it to fa
2d109 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c 20  lse (expire all 
2d10a 65 78 69 73 74 69 6e 67 0d 0a 20 20 20 20 2a 2a  existing..    **
2d10b 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0d 0a 20   statements)... 
2d10c 20 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74     */..    sqlit
2d10d 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2d10e 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65  OP_Expire, (type
2d10f 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29  ==SQLITE_ATTACH)
2d110 29 3b 0d 0a 20 20 7d 0d 0a 20 20 0d 0a 61 74 74  );..  }..  ..att
2d111 61 63 68 5f 65 6e 64 3a 0d 0a 20 20 73 71 6c 69  ach_end:..  sqli
2d112 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2d113 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b 0d 0a 20  , pFilename);.. 
2d114 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2d115 74 65 28 64 62 2c 20 70 44 62 6e 61 6d 65 29 3b  te(db, pDbname);
2d116 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ..  sqlite3ExprD
2d117 65 6c 65 74 65 28 64 62 2c 20 70 4b 65 79 29 3b  elete(db, pKey);
2d118 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61  ..}..../*..** Ca
2d119 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
2d11a 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20  er to compile a 
2d11b 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  DETACH statement
2d11c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 44 45  ...**..**     DE
2d11d 54 41 43 48 20 70 44 62 6e 61 6d 65 0d 0a 2a 2f  TACH pDbname..*/
2d11e 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2d11f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74   void sqlite3Det
2d120 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73  ach(Parse *pPars
2d121 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  e, Expr *pDbname
2d122 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  ){..  static con
2d123 73 74 20 46 75 6e 63 44 65 66 20 64 65 74 61 63  st FuncDef detac
2d124 68 5f 66 75 6e 63 20 3d 20 7b 0d 0a 20 20 20 20  h_func = {..    
2d125 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
2d126 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0d 0a 20 20    /* nArg */..  
2d127 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20    SQLITE_UTF8,  
2d128 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20      /* iPrefEnc 
2d129 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20 20  */..    0,      
2d12a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61            /* fla
2d12b 67 73 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20  gs */..    0,   
2d12c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d12d 70 55 73 65 72 44 61 74 61 20 2a 2f 0d 0a 20 20  pUserData */..  
2d12e 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
2d12f 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0d      /* pNext */.
2d130 0a 20 20 20 20 64 65 74 61 63 68 46 75 6e 63 2c  .    detachFunc,
2d131 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20         /* xFunc 
2d132 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20 20  */..    0,      
2d133 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74            /* xSt
2d134 65 70 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20  ep */..    0,   
2d135 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d136 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0d 0a 20 20  xFinalize */..  
2d137 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68    "sqlite_detach
2d138 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0d  ",  /* zName */.
2d139 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
2d13a 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20         /* pHash 
2d13b 2a 2f 0d 0a 20 20 20 20 30 20 20 20 20 20 20 20  */..    0       
2d13c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 44 65            /* pDe
2d13d 73 74 72 75 63 74 6f 72 20 2a 2f 0d 0a 20 20 7d  structor */..  }
2d13e 3b 0d 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28  ;..  codeAttach(
2d13f 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
2d140 45 54 41 43 48 2c 20 26 64 65 74 61 63 68 5f 66  ETACH, &detach_f
2d141 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c  unc, pDbname, 0,
2d142 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0d 0a 7d   0, pDbname);..}
2d143 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c 65  ..../*..** Calle
2d144 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
2d145 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54  to compile an AT
2d146 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0d  TACH statement..
2d147 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 41 54 54 41  .**..**     ATTA
2d148 43 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20  CH p AS pDbname 
2d149 4b 45 59 20 70 4b 65 79 0d 0a 2a 2f 0d 0a 53 51  KEY pKey..*/..SQ
2d14a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2d14b 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28  d sqlite3Attach(
2d14c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2d14d 78 70 72 20 2a 70 2c 20 45 78 70 72 20 2a 70 44  xpr *p, Expr *pD
2d14e 62 6e 61 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65  bname, Expr *pKe
2d14f 79 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  y){..  static co
2d150 6e 73 74 20 46 75 6e 63 44 65 66 20 61 74 74 61  nst FuncDef atta
2d151 63 68 5f 66 75 6e 63 20 3d 20 7b 0d 0a 20 20 20  ch_func = {..   
2d152 20 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   3,             
2d153 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0d 0a 20     /* nArg */.. 
2d154 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
2d155 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63       /* iPrefEnc
2d156 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20   */..    0,     
2d157 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
2d158 61 67 73 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20  ags */..    0,  
2d159 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d15a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0d 0a 20   pUserData */.. 
2d15b 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
2d15c 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f       /* pNext */
2d15d 0d 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e 63  ..    attachFunc
2d15e 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63  ,       /* xFunc
2d15f 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 20 20 20   */..    0,     
2d160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
2d161 74 65 70 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20  tep */..    0,  
2d162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d163 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0d 0a 20   xFinalize */.. 
2d164 20 20 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63     "sqlite_attac
2d165 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f  h",  /* zName */
2d166 0d 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  ..    0,        
2d167 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68          /* pHash
2d168 20 2a 2f 0d 0a 20 20 20 20 30 20 20 20 20 20 20   */..    0      
2d169 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 44             /* pD
2d16a 65 73 74 72 75 63 74 6f 72 20 2a 2f 0d 0a 20 20  estructor */..  
2d16b 7d 3b 0d 0a 20 20 63 6f 64 65 41 74 74 61 63 68  };..  codeAttach
2d16c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2d16d 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f  ATTACH, &attach_
2d16e 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e  func, p, p, pDbn
2d16f 61 6d 65 2c 20 70 4b 65 79 29 3b 0d 0a 7d 0d 0a  ame, pKey);..}..
2d170 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2d171 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0d  _OMIT_ATTACH */.
2d172 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61  .../*..** Initia
2d173 6c 69 7a 65 20 61 20 44 62 46 69 78 65 72 20 73  lize a DbFixer s
2d174 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20  tructure.  This 
2d175 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20  routine must be 
2d176 63 61 6c 6c 65 64 20 70 72 69 6f 72 0d 0a 2a 2a  called prior..**
2d177 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 20   to passing the 
2d178 73 74 72 75 63 74 75 72 65 20 74 6f 20 6f 6e 65  structure to one
2d179 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 46 69   of the sqliteFi
2d17a 78 41 41 41 41 28 29 20 72 6f 75 74 69 6e 65 73  xAAAA() routines
2d17b 20 62 65 6c 6f 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   below...**..** 
2d17c 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
2d17d 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68   indicates wheth
2d17e 65 72 20 6f 72 20 6e 6f 74 20 66 69 78 61 74 69  er or not fixati
2d17f 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  on is required. 
2d180 20 54 52 55 45 0d 0a 2a 2a 20 6d 65 61 6e 73 20   TRUE..** means 
2d181 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 66 69  we do need to fi
2d182 78 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  x the database r
2d183 65 66 65 72 65 6e 63 65 73 2c 20 46 41 4c 53 45  eferences, FALSE
2d184 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74   means we do not
2d185 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2d186 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2d187 33 46 69 78 49 6e 69 74 28 0d 0a 20 20 44 62 46  3FixInit(..  DbF
2d188 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
2d189 20 2f 2a 20 54 68 65 20 66 69 78 65 72 20 74 6f   /* The fixer to
2d18a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
2d18b 2a 2f 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  */..  Parse *pPa
2d18c 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45 72 72  rse,      /* Err
2d18d 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69 6c 6c  or messages will
2d18e 20 62 65 20 77 72 69 74 74 65 6e 20 68 65 72 65   be written here
2d18f 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 2c 20   */..  int iDb, 
2d190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2d191 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62 61  is is the databa
2d192 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20  se that must be 
2d193 75 73 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  used */..  const
2d194 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f   char *zType,  /
2d195 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67  * "view", "trigg
2d196 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20  er", or "index" 
2d197 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65  */..  const Toke
2d198 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d  n *pName  /* Nam
2d199 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74  e of the view, t
2d19a 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78  rigger, or index
2d19b 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74   */..){..  sqlit
2d19c 65 33 20 2a 64 62 3b 0d 0a 0d 0a 20 20 69 66 28  e3 *db;....  if(
2d19d 20 4e 45 56 45 52 28 69 44 62 3c 30 29 20 7c 7c   NEVER(iDb<0) ||
2d19e 20 69 44 62 3d 3d 31 20 29 20 72 65 74 75 72 6e   iDb==1 ) return
2d19f 20 30 3b 0d 0a 20 20 64 62 20 3d 20 70 50 61 72   0;..  db = pPar
2d1a0 73 65 2d 3e 64 62 3b 0d 0a 20 20 61 73 73 65 72  se->db;..  asser
2d1a1 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44 62 20 29  t( db->nDb>iDb )
2d1a2 3b 0d 0a 20 20 70 46 69 78 2d 3e 70 50 61 72 73  ;..  pFix->pPars
2d1a3 65 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 70  e = pParse;..  p
2d1a4 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e 61  Fix->zDb = db->a
2d1a5 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d 0a  Db[iDb].zName;..
2d1a6 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20    pFix->zType = 
2d1a7 7a 54 79 70 65 3b 0d 0a 20 20 70 46 69 78 2d 3e  zType;..  pFix->
2d1a8 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0d 0a  pName = pName;..
2d1a9 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a    return 1;..}..
2d1aa 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  ../*..** The fol
2d1ab 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f  lowing set of ro
2d1ac 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f  utines walk thro
2d1ad 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74 72  ugh the parse tr
2d1ae 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0d 0a 2a  ee and assign..*
2d1af 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74  * a specific dat
2d1b0 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62  abase to all tab
2d1b1 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68  le references wh
2d1b2 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
2d1b3 20 6e 61 6d 65 0d 0a 2a 2a 20 77 61 73 20 6c 65   name..** was le
2d1b4 66 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69  ft unspecified i
2d1b5 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
2d1b6 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  QL statement.  T
2d1b7 68 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72  he pFix structur
2d1b8 65 0d 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20  e..** must have 
2d1b9 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
2d1ba 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c   by a prior call
2d1bb 20 74 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e   to sqlite3FixIn
2d1bc 69 74 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  it()...**..** Th
2d1bd 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2d1be 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
2d1bf 72 65 20 74 68 61 74 20 61 6e 20 69 6e 64 65 78  re that an index
2d1c0 2c 20 74 72 69 67 67 65 72 2c 20 6f 72 0d 0a 2a  , trigger, or..*
2d1c1 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65 20 64 61  * view in one da
2d1c2 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 20  tabase does not 
2d1c3 72 65 66 65 72 20 74 6f 20 6f 62 6a 65 63 74 73  refer to objects
2d1c4 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
2d1c5 64 61 74 61 62 61 73 65 2e 0d 0a 2a 2a 20 28 45  database...** (E
2d1c6 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69 63 65  xception: indice
2d1c7 73 2c 20 74 72 69 67 67 65 72 73 2c 20 61 6e 64  s, triggers, and
2d1c8 20 76 69 65 77 73 20 69 6e 20 74 68 65 20 54 45   views in the TE
2d1c9 4d 50 20 64 61 74 61 62 61 73 65 20 61 72 65 0d  MP database are.
2d1ca 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72  .** allowed to r
2d1cb 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67  efer to anything
2d1cc 2e 29 20 20 49 66 20 61 20 72 65 66 65 72 65 6e  .)  If a referen
2d1cd 63 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79  ce is explicitly
2d1ce 20 6d 61 64 65 0d 0a 2a 2a 20 74 6f 20 61 6e 20   made..** to an 
2d1cf 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66 66  object in a diff
2d1d0 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c 20  erent database, 
2d1d1 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2d1d2 20 69 73 20 61 64 64 65 64 20 74 6f 0d 0a 2a 2a   is added to..**
2d1d3 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2d1d4 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69   and these routi
2d1d5 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  nes return non-z
2d1d6 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68  ero.  If everyth
2d1d7 69 6e 67 0d 0a 2a 2a 20 63 68 65 63 6b 73 20 6f  ing..** checks o
2d1d8 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e  ut, these routin
2d1d9 65 73 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f  es return 0...*/
2d1da 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2d1db 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53   int sqlite3FixS
2d1dc 72 63 4c 69 73 74 28 0d 0a 20 20 44 62 46 69 78  rcList(..  DbFix
2d1dd 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20  er *pFix,       
2d1de 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68  /* Context of th
2d1df 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0d 0a 20  e fixation */.. 
2d1e0 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 20   SrcList *pList 
2d1e1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 6f 75        /* The Sou
2d1e2 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68 65 63  rce list to chec
2d1e3 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0d  k and modify */.
2d1e4 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  .){..  int i;.. 
2d1e5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
2d1e6 3b 0d 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  ;..  struct SrcL
2d1e7 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2d1e8 0d 0a 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28  ....  if( NEVER(
2d1e9 70 4c 69 73 74 3d 3d 30 29 20 29 20 72 65 74 75  pList==0) ) retu
2d1ea 72 6e 20 30 3b 0d 0a 20 20 7a 44 62 20 3d 20 70  rn 0;..  zDb = p
2d1eb 46 69 78 2d 3e 7a 44 62 3b 0d 0a 20 20 66 6f 72  Fix->zDb;..  for
2d1ec 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
2d1ed 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
2d1ee 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
2d1ef 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 49 74  +){..    if( pIt
2d1f0 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30  em->zDatabase==0
2d1f1 20 29 7b 0d 0a 20 20 20 20 20 20 70 49 74 65 6d   ){..      pItem
2d1f2 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71  ->zDatabase = sq
2d1f3 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 46  lite3DbStrDup(pF
2d1f4 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20  ix->pParse->db, 
2d1f5 7a 44 62 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  zDb);..    }else
2d1f6 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2d1f7 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61  Cmp(pItem->zData
2d1f8 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0d  base,zDb)!=0 ){.
2d1f9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2d1fa 72 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 61  rorMsg(pFix->pPa
2d1fb 72 73 65 2c 0d 0a 20 20 20 20 20 20 20 20 20 22  rse,..         "
2d1fc 25 73 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66  %s %T cannot ref
2d1fd 65 72 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69  erence objects i
2d1fe 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 0d  n database %s",.
2d1ff 0a 20 20 20 20 20 20 20 20 20 70 46 69 78 2d 3e  .         pFix->
2d200 7a 54 79 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61  zType, pFix->pNa
2d201 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61  me, pItem->zData
2d202 62 61 73 65 29 3b 0d 0a 20 20 20 20 20 20 72 65  base);..      re
2d203 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 1;..    }..
2d204 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2d205 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2d206 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2d207 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0d  E_OMIT_TRIGGER).
2d208 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2d209 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
2d20a 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20  pItem->pSelect) 
2d20b 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20  ) return 1;..   
2d20c 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
2d20d 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d  xpr(pFix, pItem-
2d20e 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31  >pOn) ) return 1
2d20f 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a  ;..#endif..  }..
2d210 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a    return 0;..}..
2d211 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2d212 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2d213 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2d214 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0d  E_OMIT_TRIGGER).
2d215 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2d216 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65  int sqlite3FixSe
2d217 6c 65 63 74 28 0d 0a 20 20 44 62 46 69 78 65 72  lect(..  DbFixer
2d218 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a   *pFix,       /*
2d219 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
2d21a 66 69 78 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 53  fixation */..  S
2d21b 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20  elect *pSelect  
2d21c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
2d21d 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  T statement to b
2d21e 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
2d21f 61 74 61 62 61 73 65 20 2a 2f 0d 0a 29 7b 0d 0a  atabase */..){..
2d220 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
2d221 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   ){..    if( sql
2d222 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
2d223 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70  pFix, pSelect->p
2d224 45 4c 69 73 74 29 20 29 7b 0d 0a 20 20 20 20 20  EList) ){..     
2d225 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20   return 1;..    
2d226 7d 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
2d227 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46 69  e3FixSrcList(pFi
2d228 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  x, pSelect->pSrc
2d229 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  ) ){..      retu
2d22a 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
2d22b 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2d22c 45 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65  Expr(pFix, pSele
2d22d 63 74 2d 3e 70 57 68 65 72 65 29 20 29 7b 0d 0a  ct->pWhere) ){..
2d22e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d        return 1;.
2d22f 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
2d230 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
2d231 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48  Fix, pSelect->pH
2d232 61 76 69 6e 67 29 20 29 7b 0d 0a 20 20 20 20 20  aving) ){..     
2d233 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20   return 1;..    
2d234 7d 0d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d  }..    pSelect =
2d235 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
2d236 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
2d237 20 30 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50   0;..}..SQLITE_P
2d238 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2d239 65 33 46 69 78 45 78 70 72 28 0d 0a 20 20 44 62  e3FixExpr(..  Db
2d23a 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
2d23b 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74   /* Context of t
2d23c 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0d 0a  he fixation */..
2d23d 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
2d23e 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
2d23f 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78  ession to be fix
2d240 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
2d241 73 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 77 68 69  se */..){..  whi
2d242 6c 65 28 20 70 45 78 70 72 20 29 7b 0d 0a 20 20  le( pExpr ){..  
2d243 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79    if( ExprHasAny
2d244 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2d245 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20  EP_TokenOnly) ) 
2d246 62 72 65 61 6b 3b 0d 0a 20 20 20 20 69 66 28 20  break;..    if( 
2d247 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2d248 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2d249 65 63 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 69  ect) ){..      i
2d24a 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  f( sqlite3FixSel
2d24b 65 63 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d  ect(pFix, pExpr-
2d24c 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65  >x.pSelect) ) re
2d24d 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 65 6c  turn 1;..    }el
2d24e 73 65 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 73  se{..      if( s
2d24f 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
2d250 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78  t(pFix, pExpr->x
2d251 2e 70 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e  .pList) ) return
2d252 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   1;..    }..    
2d253 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
2d254 70 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e  pr(pFix, pExpr->
2d255 70 52 69 67 68 74 29 20 29 7b 0d 0a 20 20 20 20  pRight) ){..    
2d256 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20    return 1;..   
2d257 20 7d 0d 0a 20 20 20 20 70 45 78 70 72 20 3d 20   }..    pExpr = 
2d258 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20  pExpr->pLeft;.. 
2d259 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d   }..  return 0;.
2d25a 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
2d25b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
2d25c 78 45 78 70 72 4c 69 73 74 28 0d 0a 20 20 44 62  xExprList(..  Db
2d25d 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
2d25e 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74   /* Context of t
2d25f 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0d 0a  he fixation */..
2d260 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2d261 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72  t    /* The expr
2d262 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78  ession to be fix
2d263 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
2d264 73 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  se */..){..  int
2d265 20 69 3b 0d 0a 20 20 73 74 72 75 63 74 20 45 78   i;..  struct Ex
2d266 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2d267 65 6d 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73 74  em;..  if( pList
2d268 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ==0 ) return 0;.
2d269 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
2d26a 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c  m=pList->a; i<pL
2d26b 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  ist->nExpr; i++,
2d26c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20   pItem++){..    
2d26d 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
2d26e 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
2d26f 70 45 78 70 72 29 20 29 7b 0d 0a 20 20 20 20 20  pExpr) ){..     
2d270 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20   return 1;..    
2d271 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
2d272 20 30 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a   0;..}..#endif..
2d273 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2d274 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 53  _OMIT_TRIGGER..S
2d275 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2d276 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67  t sqlite3FixTrig
2d277 67 65 72 53 74 65 70 28 0d 0a 20 20 44 62 46 69  gerStep(..  DbFi
2d278 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
2d279 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
2d27a 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20   fixation */..  
2d27b 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
2d27c 65 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65  ep /* The trigge
2d27d 72 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20  r step be fixed 
2d27e 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
2d27f 2a 2f 0d 0a 29 7b 0d 0a 20 20 77 68 69 6c 65 28  */..){..  while(
2d280 20 70 53 74 65 70 20 29 7b 0d 0a 20 20 20 20 69   pStep ){..    i
2d281 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  f( sqlite3FixSel
2d282 65 63 74 28 70 46 69 78 2c 20 70 53 74 65 70 2d  ect(pFix, pStep-
2d283 3e 70 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20 20  >pSelect) ){..  
2d284 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20      return 1;.. 
2d285 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73 71     }..    if( sq
2d286 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69  lite3FixExpr(pFi
2d287 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65  x, pStep->pWhere
2d288 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  ) ){..      retu
2d289 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
2d28a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2d28b 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70  ExprList(pFix, p
2d28c 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  Step->pExprList)
2d28d 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72   ){..      retur
2d28e 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  n 1;..    }..   
2d28f 20 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e   pStep = pStep->
2d290 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72  pNext;..  }..  r
2d291 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 23 65 6e  eturn 0;..}..#en
2d292 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  dif..../********
2d293 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74  ****** End of at
2d294 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tach.c *********
2d295 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d296 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d297 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
2d298 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2d299 6c 65 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a  le auth.c ******
2d29a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d29b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d29c 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20  ******/../*..** 
2d29d 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31 31 0d  2003 January 11.
2d29e 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
2d29f 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2d2a0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2d2a1 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2d2a2 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
2d2a3 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2d2a4 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2d2a5 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
2d2a6 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2d2a7 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
2d2a8 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2d2a9 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2d2aa 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2d2ab 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
2d2ac 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2d2ad 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2d2ae 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2d2af 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
2d2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2b4 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  *******..** This
2d2b5 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
2d2b6 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
2d2b7 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65  ement the sqlite
2d2b8 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
2d2b9 28 29 0d 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69  ()..** API.  Thi
2d2ba 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e  s facility is an
2d2bb 20 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72   optional featur
2d2bc 65 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  e of the library
2d2bd 2e 20 20 45 6d 62 65 64 64 65 64 0d 0a 2a 2a 20  .  Embedded..** 
2d2be 73 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20  systems that do 
2d2bf 6e 6f 74 20 6e 65 65 64 20 74 68 69 73 20 66 61  not need this fa
2d2c0 63 69 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20  cility may omit 
2d2c1 69 74 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e  it by recompilin
2d2c2 67 0d 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72  g..** the librar
2d2c3 79 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f  y with -DSQLITE_
2d2c4 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2d2c5 4f 4e 3d 31 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  ON=1..*/..../*..
2d2c6 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f  ** All of the co
2d2c7 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
2d2c8 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62  may be omitted b
2d2c9 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69 6e  y defining a sin
2d2ca 67 6c 65 0d 0a 2a 2a 20 6d 61 63 72 6f 2e 0d 0a  gle..** macro...
2d2cb 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
2d2cc 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2d2cd 41 54 49 4f 4e 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ATION..../*..** 
2d2ce 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65  Set or clear the
2d2cf 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a   access authoriz
2d2d0 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 0d  ation function..
2d2d1 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 63 63 65  .**..** The acce
2d2d2 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
2d2d3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20   function is be 
2d2d4 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68  called during th
2d2d5 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0d 0a 2a  e compilation..*
2d2d6 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66  * phase to verif
2d2d7 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  y that the user 
2d2d8 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20  has read and/or 
2d2d9 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72  write access per
2d2da 6d 69 73 73 69 6f 6e 20 6f 6e 0d 0a 2a 2a 20 76  mission on..** v
2d2db 61 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66  arious fields of
2d2dc 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
2d2dd 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
2d2de 6e 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66  nt to the auth f
2d2df 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20 61  unction..** is a
2d2e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64   copy of the 3rd
2d2e1 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
2d2e2 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20  s routine.  The 
2d2e3 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0d  second argument.
2d2e4 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68 20  .** to the auth 
2d2e5 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20  function is one 
2d2e6 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61 6e  of these constan
2d2e7 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  ts:..**..**     
2d2e8 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
2d2e9 49 4e 44 45 58 0d 0a 2a 2a 20 20 20 20 20 20 20  INDEX..**       
2d2ea 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41  SQLITE_CREATE_TA
2d2eb 42 4c 45 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51  BLE..**       SQ
2d2ec 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
2d2ed 5f 49 4e 44 45 58 0d 0a 2a 2a 20 20 20 20 20 20  _INDEX..**      
2d2ee 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
2d2ef 45 4d 50 5f 54 41 42 4c 45 0d 0a 2a 2a 20 20 20  EMP_TABLE..**   
2d2f0 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
2d2f1 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0d 0a  E_TEMP_TRIGGER..
2d2f2 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2d2f3 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57  CREATE_TEMP_VIEW
2d2f4 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  ..**       SQLIT
2d2f5 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52  E_CREATE_TRIGGER
2d2f6 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  ..**       SQLIT
2d2f7 45 5f 43 52 45 41 54 45 5f 56 49 45 57 0d 0a 2a  E_CREATE_VIEW..*
2d2f8 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
2d2f9 45 4c 45 54 45 0d 0a 2a 2a 20 20 20 20 20 20 20  ELETE..**       
2d2fa 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45  SQLITE_DROP_INDE
2d2fb 58 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  X..**       SQLI
2d2fc 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0d 0a 2a  TE_DROP_TABLE..*
2d2fd 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
2d2fe 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0d 0a  ROP_TEMP_INDEX..
2d2ff 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2d300 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0d  DROP_TEMP_TABLE.
2d301 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
2d302 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47  _DROP_TEMP_TRIGG
2d303 45 52 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  ER..**       SQL
2d304 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49  ITE_DROP_TEMP_VI
2d305 45 57 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  EW..**       SQL
2d306 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
2d307 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  ..**       SQLIT
2d308 45 5f 44 52 4f 50 5f 56 49 45 57 0d 0a 2a 2a 20  E_DROP_VIEW..** 
2d309 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53        SQLITE_INS
2d30a 45 52 54 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51  ERT..**       SQ
2d30b 4c 49 54 45 5f 50 52 41 47 4d 41 0d 0a 2a 2a 20  LITE_PRAGMA..** 
2d30c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41        SQLITE_REA
2d30d 44 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  D..**       SQLI
2d30e 54 45 5f 53 45 4c 45 43 54 0d 0a 2a 2a 20 20 20  TE_SELECT..**   
2d30f 20 20 20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53      SQLITE_TRANS
2d310 41 43 54 49 4f 4e 0d 0a 2a 2a 20 20 20 20 20 20  ACTION..**      
2d311 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 0d 0a   SQLITE_UPDATE..
2d312 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  **..** The third
2d313 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
2d314 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61 75 74  ments to the aut
2d315 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74  h function are t
2d316 68 65 20 6e 61 6d 65 20 6f 66 0d 0a 2a 2a 20 74  he name of..** t
2d317 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65  he table and the
2d318 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72 65   column that are
2d319 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64 2e   being accessed.
2d31a 20 20 54 68 65 20 61 75 74 68 20 66 75 6e 63 74    The auth funct
2d31b 69 6f 6e 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20 72  ion..** should r
2d31c 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c  eturn either SQL
2d31d 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44  ITE_OK, SQLITE_D
2d31e 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49  ENY, or SQLITE_I
2d31f 47 4e 4f 52 45 2e 20 20 49 66 0d 0a 2a 2a 20 53  GNORE.  If..** S
2d320 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
2d321 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  rned, it means t
2d322 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c  hat access is al
2d323 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44  lowed.  SQLITE_D
2d324 45 4e 59 0d 0a 2a 2a 20 6d 65 61 6e 73 20 74 68  ENY..** means th
2d325 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  at the SQL state
2d326 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d  ment will never-
2d327 72 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65  run - the sqlite
2d328 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 0d 0a 2a  3_exec() call..*
2d329 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69  * will return wi
2d32a 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51  th an error.  SQ
2d32b 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e  LITE_IGNORE mean
2d32c 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73  s that the SQL s
2d32d 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 73 68 6f  tatement..** sho
2d32e 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74 65  uld run but atte
2d32f 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68 65  mpts to read the
2d330 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d   specified colum
2d331 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55  n will return NU
2d332 4c 4c 0d 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d  LL..** and attem
2d333 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65  pts to write the
2d334 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20   column will be 
2d335 69 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  ignored...**..**
2d336 20 53 65 74 74 69 6e 67 20 74 68 65 20 61 75 74   Setting the aut
2d337 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55  h function to NU
2d338 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68 69 73  LL disables this
2d339 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61   hook.  The defa
2d33a 75 6c 74 0d 0a 2a 2a 20 73 65 74 74 69 6e 67 20  ult..** setting 
2d33b 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  of the auth func
2d33c 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0d 0a 2a  tion is NULL...*
2d33d 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
2d33e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  t sqlite3_set_au
2d33f 74 68 6f 72 69 7a 65 72 28 0d 0a 20 20 73 71 6c  thorizer(..  sql
2d340 69 74 65 33 20 2a 64 62 2c 0d 0a 20 20 69 6e 74  ite3 *db,..  int
2d341 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c   (*xAuth)(void*,
2d342 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  int,const char*,
2d343 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
2d344 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
2d345 61 72 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70  ar*),..  void *p
2d346 41 72 67 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74  Arg..){..  sqlit
2d347 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
2d348 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 64 62  b->mutex);..  db
2d349 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b  ->xAuth = xAuth;
2d34a 0d 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ..  db->pAuthArg
2d34b 20 3d 20 70 41 72 67 3b 0d 0a 20 20 73 71 6c 69   = pArg;..  sqli
2d34c 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
2d34d 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
2d34e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
2d34f 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2d350 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  x);..  return SQ
2d351 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
2d352 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65  *..** Write an e
2d353 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
2d354 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  o pParse->zErrMs
2d355 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73 20  g that explains 
2d356 74 68 61 74 20 74 68 65 0d 0a 2a 2a 20 75 73 65  that the..** use
2d357 72 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f  r-supplied autho
2d358 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
2d359 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c  n returned an il
2d35a 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0d 0a 2a 2f  legal value...*/
2d35b 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  ..static void sq
2d35c 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72  liteAuthBadRetur
2d35d 6e 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61  nCode(Parse *pPa
2d35e 72 73 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  rse){..  sqlite3
2d35f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2d360 20 22 61 75 74 68 6f 72 69 7a 65 72 20 6d 61 6c   "authorizer mal
2d361 66 75 6e 63 74 69 6f 6e 22 29 3b 0d 0a 20 20 70  function");..  p
2d362 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
2d363 54 45 5f 45 52 52 4f 52 3b 0d 0a 7d 0d 0a 0d 0a  TE_ERROR;..}....
2d364 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  /*..** Invoke th
2d365 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2d366 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 70 65 72  callback for per
2d367 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20  mission to read 
2d368 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d  column zCol from
2d369 0d 0a 2a 2a 20 74 61 62 6c 65 20 7a 54 61 62 20  ..** table zTab 
2d36a 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e  in database zDb.
2d36b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
2d36c 73 73 75 6d 65 73 20 74 68 61 74 20 61 6e 20 61  ssumes that an a
2d36d 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0d 0a 2a 2a  uthorization..**
2d36e 20 63 61 6c 6c 62 61 63 6b 20 68 61 73 20 62 65   callback has be
2d36f 65 6e 20 72 65 67 69 73 74 65 72 65 64 20 28 69  en registered (i
2d370 2e 65 2e 20 74 68 61 74 20 73 71 6c 69 74 65 33  .e. that sqlite3
2d371 2e 78 41 75 74 68 20 69 73 20 6e 6f 74 20 4e 55  .xAuth is not NU
2d372 4c 4c 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  LL)...**..** If 
2d373 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 69 73  SQLITE_IGNORE is
2d374 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 70 45   returned and pE
2d375 78 70 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  xpr is not NULL,
2d376 20 74 68 65 6e 20 70 45 78 70 72 20 69 73 20 63   then pExpr is c
2d377 68 61 6e 67 65 64 0d 0a 2a 2a 20 74 6f 20 61 6e  hanged..** to an
2d378 20 53 51 4c 20 4e 55 4c 4c 20 65 78 70 72 65 73   SQL NULL expres
2d379 73 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c  sion. Otherwise,
2d37a 20 69 66 20 70 45 78 70 72 20 69 73 20 4e 55 4c   if pExpr is NUL
2d37b 4c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 49  L, then SQLITE_I
2d37c 47 4e 4f 52 45 0d 0a 2a 2a 20 69 73 20 74 72 65  GNORE..** is tre
2d37d 61 74 65 64 20 61 73 20 53 51 4c 49 54 45 5f 44  ated as SQLITE_D
2d37e 45 4e 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ENY. In this cas
2d37f 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 6c 65  e an error is le
2d380 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0d 0a 2a  ft in pParse...*
2d381 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2d382 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74  E int sqlite3Aut
2d383 68 52 65 61 64 43 6f 6c 28 0d 0a 20 20 50 61 72  hReadCol(..  Par
2d384 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2d385 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d386 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2d387 78 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  xt */..  const c
2d388 68 61 72 20 2a 7a 54 61 62 2c 20 20 20 20 20 20  har *zTab,      
2d389 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2d38a 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 63 6f 6e  e name */..  con
2d38b 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20  st char *zCol,  
2d38c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d38d 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0d 0a  Column name */..
2d38e 20 20 69 6e 74 20 69 44 62 20 20 20 20 20 20 20    int iDb       
2d38f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d390 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
2d391 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 62 61 73  ntaining databas
2d392 65 2e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c  e. */..){..  sql
2d393 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2d394 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  e->db;       /* 
2d395 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
2d396 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 44 62 20  */..  char *zDb 
2d397 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
2d398 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
2d399 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2d39a 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b  se */..  int rc;
2d39b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d39c 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
2d39d 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
2d39e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 72 63   code */....  rc
2d39f 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d   = db->xAuth(db-
2d3a0 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54  >pAuthArg, SQLIT
2d3a1 45 5f 52 45 41 44 2c 20 7a 54 61 62 2c 7a 43 6f  E_READ, zTab,zCo
2d3a2 6c 2c 7a 44 62 2c 70 50 61 72 73 65 2d 3e 7a 41  l,zDb,pParse->zA
2d3a3 75 74 68 43 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  uthContext);..  
2d3a4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
2d3a5 45 4e 59 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  ENY ){..    if( 
2d3a6 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62  db->nDb>2 || iDb
2d3a7 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  !=0 ){..      sq
2d3a8 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2d3a9 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f  arse, "access to
2d3aa 20 25 73 2e 25 73 2e 25 73 20 69 73 20 70 72 6f   %s.%s.%s is pro
2d3ab 68 69 62 69 74 65 64 22 2c 7a 44 62 2c 7a 54 61  hibited",zDb,zTa
2d3ac 62 2c 7a 43 6f 6c 29 3b 0d 0a 20 20 20 20 7d 65  b,zCol);..    }e
2d3ad 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  lse{..      sqli
2d3ae 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2d3af 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25  se, "access to %
2d3b0 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74  s.%s is prohibit
2d3b1 65 64 22 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29  ed", zTab, zCol)
2d3b2 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50  ;..    }..    pP
2d3b3 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
2d3b4 45 5f 41 55 54 48 3b 0d 0a 20 20 7d 65 6c 73 65  E_AUTH;..  }else
2d3b5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2d3b6 49 47 4e 4f 52 45 20 26 26 20 72 63 21 3d 53 51  IGNORE && rc!=SQ
2d3b7 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
2d3b8 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74  sqliteAuthBadRet
2d3b9 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29 3b  urnCode(pParse);
2d3ba 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2d3bb 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
2d3bc 20 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c   The pExpr shoul
2d3bd 64 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e  d be a TK_COLUMN
2d3be 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68   expression.  Th
2d3bf 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64  e table referred
2d3c0 20 74 6f 0d 0a 2a 2a 20 69 73 20 69 6e 20 70 54   to..** is in pT
2d3c1 61 62 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69  abList or else i
2d3c2 74 20 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20  t is the NEW or 
2d3c3 4f 4c 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74  OLD table of a t
2d3c4 72 69 67 67 65 72 2e 20 20 0d 0a 2a 2a 20 43 68  rigger.  ..** Ch
2d3c5 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74  eck to see if it
2d3c6 20 69 73 20 4f 4b 20 74 6f 20 72 65 61 64 20 74   is OK to read t
2d3c7 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 63  his particular c
2d3c8 6f 6c 75 6d 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  olumn...**..** I
2d3c9 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  f the auth funct
2d3ca 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ion returns SQLI
2d3cb 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e 67  TE_IGNORE, chang
2d3cc 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20  e the TK_COLUMN 
2d3cd 0d 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e  ..** instruction
2d3ce 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e   into a TK_NULL.
2d3cf 20 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75    If the auth fu
2d3d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
2d3d1 51 4c 49 54 45 5f 44 45 4e 59 2c 0d 0a 2a 2a 20  QLITE_DENY,..** 
2d3d2 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e  then generate an
2d3d3 20 65 72 72 6f 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c   error...*/..SQL
2d3d4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d3d5 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
2d3d6 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
2d3d7 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  se,        /* Th
2d3d8 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2d3d9 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45 78   */..  Expr *pEx
2d3da 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pr,          /* 
2d3db 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
2d3dc 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a  o check authoriz
2d3dd 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0d 0a 20 20 53  ation on */..  S
2d3de 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20  chema *pSchema, 
2d3df 20 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68 65       /* The sche
2d3e0 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ma of the expres
2d3e1 73 69 6f 6e 20 2a 2f 0d 0a 20 20 53 72 63 4c 69  sion */..  SrcLi
2d3e2 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20  st *pTabList    
2d3e3 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68   /* All table th
2d3e4 61 74 20 70 45 78 70 72 20 6d 69 67 68 74 20 72  at pExpr might r
2d3e5 65 66 65 72 20 74 6f 20 2a 2f 0d 0a 29 7b 0d 0a  efer to */..){..
2d3e6 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2d3e7 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 54  pParse->db;..  T
2d3e8 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
2d3e9 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2d3ea 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0d  e being read */.
2d3eb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2d3ec 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  Col;     /* Name
2d3ed 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
2d3ee 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a  f the table */..
2d3ef 20 20 69 6e 74 20 69 53 72 63 3b 20 20 20 20 20    int iSrc;     
2d3f0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2d3f1 20 69 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b   in pTabList->a[
2d3f2 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67  ] of table being
2d3f3 20 72 65 61 64 20 2a 2f 0d 0a 20 20 69 6e 74 20   read */..  int 
2d3f4 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2d3f5 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f    /* The index o
2d3f6 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
2d3f7 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65  he expression re
2d3f8 66 65 72 73 20 74 6f 20 2a 2f 0d 0a 20 20 69 6e  fers to */..  in
2d3f9 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t iCol;         
2d3fa 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2d3fb 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20  column in table 
2d3fc 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 64 62 2d 3e  */....  if( db->
2d3fd 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72  xAuth==0 ) retur
2d3fe 6e 3b 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  n;..  iDb = sqli
2d3ff 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2d400 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63  (pParse->db, pSc
2d401 68 65 6d 61 29 3b 0d 0a 20 20 69 66 28 20 69 44  hema);..  if( iD
2d402 62 3c 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41  b<0 ){..    /* A
2d403 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61  n attempt to rea
2d404 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f  d a column out o
2d405 66 20 61 20 73 75 62 71 75 65 72 79 20 6f 72 20  f a subquery or 
2d406 6f 74 68 65 72 0d 0a 20 20 20 20 2a 2a 20 74 65  other..    ** te
2d407 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 2a  mporary table. *
2d408 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  /..    return;..
2d409 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28    }....  assert(
2d40a 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43   pExpr->op==TK_C
2d40b 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e  OLUMN || pExpr->
2d40c 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29  op==TK_TRIGGER )
2d40d 3b 0d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  ;..  if( pExpr->
2d40e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29  op==TK_TRIGGER )
2d40f 7b 0d 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50  {..    pTab = pP
2d410 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
2d411 62 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  b;..  }else{..  
2d412 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69    assert( pTabLi
2d413 73 74 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69  st );..    for(i
2d414 53 72 63 3d 30 3b 20 41 4c 57 41 59 53 28 69 53  Src=0; ALWAYS(iS
2d415 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  rc<pTabList->nSr
2d416 63 29 3b 20 69 53 72 63 2b 2b 29 7b 0d 0a 20 20  c); iSrc++){..  
2d417 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
2d418 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73 74 2d  Table==pTabList-
2d419 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73 6f 72  >a[iSrc].iCursor
2d41a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 54 61   ){..        pTa
2d41b 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
2d41c 69 53 72 63 5d 2e 70 54 61 62 3b 0d 0a 20 20 20  iSrc].pTab;..   
2d41d 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
2d41e 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
2d41f 0d 0a 20 20 69 43 6f 6c 20 3d 20 70 45 78 70 72  ..  iCol = pExpr
2d420 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 69 66  ->iColumn;..  if
2d421 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29  ( NEVER(pTab==0)
2d422 20 29 20 72 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20   ) return;....  
2d423 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0d 0a  if( iCol>=0 ){..
2d424 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c      assert( iCol
2d425 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0d 0a  <pTab->nCol );..
2d426 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d      zCol = pTab-
2d427 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
2d428 65 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  e;..  }else if( 
2d429 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29  pTab->iPKey>=0 )
2d42a 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  {..    assert( p
2d42b 54 61 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d  Tab->iPKey<pTab-
2d42c 3e 6e 43 6f 6c 20 29 3b 0d 0a 20 20 20 20 7a 43  >nCol );..    zC
2d42d 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ol = pTab->aCol[
2d42e 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61  pTab->iPKey].zNa
2d42f 6d 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  me;..  }else{.. 
2d430 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57 49 44     zCol = "ROWID
2d431 22 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  ";..  }..  asser
2d432 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
2d433 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 69  <db->nDb );..  i
2d434 66 28 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  f( SQLITE_IGNORE
2d435 3d 3d 73 71 6c 69 74 65 33 41 75 74 68 52 65 61  ==sqlite3AuthRea
2d436 64 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 54 61  dCol(pParse, pTa
2d437 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20  b->zName, zCol, 
2d438 69 44 62 29 20 29 7b 0d 0a 20 20 20 20 70 45 78  iDb) ){..    pEx
2d439 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c  pr->op = TK_NULL
2d43a 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
2d43b 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f 72  .** Do an author
2d43c 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75 73  ization check us
2d43d 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e 64  ing the code and
2d43e 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65 6e   arguments given
2d43f 2e 20 20 52 65 74 75 72 6e 0d 0a 2a 2a 20 65 69  .  Return..** ei
2d440 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28  ther SQLITE_OK (
2d441 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f  zero) or SQLITE_
2d442 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45  IGNORE or SQLITE
2d443 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54  _DENY.  If SQLIT
2d444 45 5f 44 45 4e 59 0d 0a 2a 2a 20 69 73 20 72 65  E_DENY..** is re
2d445 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  turned, then the
2d446 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64   error count and
2d447 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
2d448 6e 20 70 50 61 72 73 65 20 61 72 65 0d 0a 2a 2a  n pParse are..**
2d449 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70   modified approp
2d44a 72 69 61 74 65 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51  riately...*/..SQ
2d44b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2d44c 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2d44d 6b 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  k(..  Parse *pPa
2d44e 72 73 65 2c 0d 0a 20 20 69 6e 74 20 63 6f 64 65  rse,..  int code
2d44f 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ,..  const char 
2d450 2a 7a 41 72 67 31 2c 0d 0a 20 20 63 6f 6e 73 74  *zArg1,..  const
2d451 20 63 68 61 72 20 2a 7a 41 72 67 32 2c 0d 0a 20   char *zArg2,.. 
2d452 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
2d453 67 33 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  g3..){..  sqlite
2d454 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2d455 64 62 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  db;..  int rc;..
2d456 0d 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20  ..  /* Don't do 
2d457 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  any authorizatio
2d458 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 20  n checks if the 
2d459 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 69 74  database is init
2d45a 69 61 6c 69 73 69 6e 67 0d 0a 20 20 2a 2a 20 6f  ialising..  ** o
2d45b 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20  r if the parser 
2d45c 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65 64  is being invoked
2d45d 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c   from within sql
2d45e 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
2d45f 62 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  b...  */..  if( 
2d460 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c  db->init.busy ||
2d461 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
2d462 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
2d463 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d  SQLITE_OK;..  }.
2d464 0a 0d 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75  ...  if( db->xAu
2d465 74 68 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65  th==0 ){..    re
2d466 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
2d467 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 64 62 2d  .  }..  rc = db-
2d468 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68  >xAuth(db->pAuth
2d469 41 72 67 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31  Arg, code, zArg1
2d46a 2c 20 7a 41 72 67 32 2c 20 7a 41 72 67 33 2c 20  , zArg2, zArg3, 
2d46b 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2d46c 74 65 78 74 29 3b 0d 0a 20 20 69 66 28 20 72 63  text);..  if( rc
2d46d 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
2d46e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
2d46f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
2d470 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b  ot authorized");
2d471 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  ..    pParse->rc
2d472 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0d   = SQLITE_AUTH;.
2d473 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21  .  }else if( rc!
2d474 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
2d475 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  !=SQLITE_IGNORE 
2d476 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
2d477 49 54 45 5f 44 45 4e 59 3b 0d 0a 20 20 20 20 73  ITE_DENY;..    s
2d478 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75  qliteAuthBadRetu
2d479 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29 3b 0d  rnCode(pParse);.
2d47a 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
2d47b 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
2d47c 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72 69 7a  Push an authoriz
2d47d 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20  ation context.  
2d47e 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
2d47f 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  ne is called, th
2d480 65 0d 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67 75  e..** zArg3 argu
2d481 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69 7a  ment to authoriz
2d482 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20  ation callbacks 
2d483 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74  will be zContext
2d484 20 75 6e 74 69 6c 0d 0a 2a 2a 20 70 6f 70 70 65   until..** poppe
2d485 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73 65  d.  Or if pParse
2d486 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ==0, this routin
2d487 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  e is a no-op...*
2d488 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2d489 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
2d48a 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0d 0a  thContextPush(..
2d48b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2d48c 0d 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  ..  AuthContext 
2d48d 2a 70 43 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 63  *pContext, ..  c
2d48e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74  onst char *zCont
2d48f 65 78 74 0d 0a 29 7b 0d 0a 20 20 61 73 73 65 72  ext..){..  asser
2d490 74 28 20 70 50 61 72 73 65 20 29 3b 0d 0a 20 20  t( pParse );..  
2d491 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65  pContext->pParse
2d492 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 70 43   = pParse;..  pC
2d493 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e  ontext->zAuthCon
2d494 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a  text = pParse->z
2d495 41 75 74 68 43 6f 6e 74 65 78 74 3b 0d 0a 20 20  AuthContext;..  
2d496 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2d497 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b  text = zContext;
2d498 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 6f  ..}..../*..** Po
2d499 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  p an authorizati
2d49a 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20  on context that 
2d49b 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70  was previously p
2d49c 75 73 68 65 64 0d 0a 2a 2a 20 62 79 20 73 71 6c  ushed..** by sql
2d49d 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
2d49e 75 73 68 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ush..*/..SQLITE_
2d49f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2d4a0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
2d4a1 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20 2a  op(AuthContext *
2d4a2 70 43 6f 6e 74 65 78 74 29 7b 0d 0a 20 20 69 66  pContext){..  if
2d4a3 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72  ( pContext->pPar
2d4a4 73 65 20 29 7b 0d 0a 20 20 20 20 70 43 6f 6e 74  se ){..    pCont
2d4a5 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75  ext->pParse->zAu
2d4a6 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e  thContext = pCon
2d4a7 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  text->zAuthConte
2d4a8 78 74 3b 0d 0a 20 20 20 20 70 43 6f 6e 74 65 78  xt;..    pContex
2d4a9 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0d 0a  t->pParse = 0;..
2d4aa 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66    }..}....#endif
2d4ab 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2d4ac 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f  AUTHORIZATION */
2d4ad 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
2d4ae 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74 68 2e  *** End of auth.
2d4af 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2d4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4b2 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
2d4b3 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2d4b4 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  build.c ********
2d4b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4b7 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
2d4b8 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a  1 September 15..
2d4b9 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
2d4ba 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
2d4bb 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
2d4bc 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
2d4bd 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
2d4be 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2d4bf 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
2d4c0 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
2d4c1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2d4c2 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
2d4c3 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2d4c4 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2d4c5 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2d4c6 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
2d4c7 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2d4c8 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2d4c9 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2d4ca 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
2d4cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d4cf 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  ******..** This 
2d4d0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
2d4d1 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
2d4d2 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79  at are called by
2d4d3 20 74 68 65 20 53 51 4c 69 74 65 20 70 61 72 73   the SQLite pars
2d4d4 65 72 0d 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74  er..** when synt
2d4d5 61 78 20 72 75 6c 65 73 20 61 72 65 20 72 65 64  ax rules are red
2d4d6 75 63 65 64 2e 20 20 54 68 65 20 72 6f 75 74 69  uced.  The routi
2d4d7 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
2d4d8 20 68 61 6e 64 6c 65 20 74 68 65 0d 0a 2a 2a 20   handle the..** 
2d4d9 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 20  following kinds 
2d4da 6f 66 20 53 51 4c 20 73 79 6e 74 61 78 3a 0d 0a  of SQL syntax:..
2d4db 2a 2a 0d 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  **..**     CREAT
2d4dc 45 20 54 41 42 4c 45 0d 0a 2a 2a 20 20 20 20 20  E TABLE..**     
2d4dd 44 52 4f 50 20 54 41 42 4c 45 0d 0a 2a 2a 20 20  DROP TABLE..**  
2d4de 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 0d     CREATE INDEX.
2d4df 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44  .**     DROP IND
2d4e0 45 58 0d 0a 2a 2a 20 20 20 20 20 63 72 65 61 74  EX..**     creat
2d4e1 69 6e 67 20 49 44 20 6c 69 73 74 73 0d 0a 2a 2a  ing ID lists..**
2d4e2 20 20 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53       BEGIN TRANS
2d4e3 41 43 54 49 4f 4e 0d 0a 2a 2a 20 20 20 20 20 43  ACTION..**     C
2d4e4 4f 4d 4d 49 54 0d 0a 2a 2a 20 20 20 20 20 52 4f  OMMIT..**     RO
2d4e5 4c 4c 42 41 43 4b 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a  LLBACK..*/..../*
2d4e6 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
2d4e7 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  e is called when
2d4e8 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65   a new SQL state
2d4e9 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69 6e  ment is beginnin
2d4ea 67 20 74 6f 0d 0a 2a 2a 20 62 65 20 70 61 72 73  g to..** be pars
2d4eb 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20  ed.  Initialize 
2d4ec 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
2d4ed 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0d  ture as needed..
2d4ee 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2d4ef 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2d4f0 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65  BeginParse(Parse
2d4f1 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78   *pParse, int ex
2d4f2 70 6c 61 69 6e 46 6c 61 67 29 7b 0d 0a 20 20 70  plainFlag){..  p
2d4f3 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d  Parse->explain =
2d4f4 20 28 75 38 29 65 78 70 6c 61 69 6e 46 6c 61 67   (u8)explainFlag
2d4f5 3b 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61  ;..  pParse->nVa
2d4f6 72 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66  r = 0;..}....#if
2d4f7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2d4f8 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a 2f  _SHARED_CACHE../
2d4f9 2a 0d 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c  *..** The TableL
2d4fa 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 69 73  ock structure is
2d4fb 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68   only used by th
2d4fc 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  e sqlite3TableLo
2d4fd 63 6b 28 29 20 61 6e 64 0d 0a 2a 2a 20 63 6f 64  ck() and..** cod
2d4fe 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75  eTableLocks() fu
2d4ff 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 73 74  nctions...*/..st
2d500 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b  ruct TableLock {
2d501 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  ..  int iDb;    
2d502 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2d503 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
2d504 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f  ing the table to
2d505 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 20   be locked */.. 
2d506 20 69 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20   int iTab;      
2d507 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f        /* The roo
2d508 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  t page of the ta
2d509 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
2d50a 20 2a 2f 0d 0a 20 20 75 38 20 69 73 57 72 69 74   */..  u8 isWrit
2d50b 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54  eLock;      /* T
2d50c 72 75 65 20 66 6f 72 20 77 72 69 74 65 20 6c 6f  rue for write lo
2d50d 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 61  ck.  False for a
2d50e 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0d 0a 20   read lock */.. 
2d50f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2d510 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me;   /* Name of
2d511 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 7d   the table */..}
2d512 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 63 6f  ;..../*..** Reco
2d513 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
2d514 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b   we want to lock
2d515 20 61 20 74 61 62 6c 65 20 61 74 20 72 75 6e 2d   a table at run-
2d516 74 69 6d 65 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20  time.  ..**..** 
2d517 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  The table to be 
2d518 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 20  locked has root 
2d519 70 61 67 65 20 69 54 61 62 20 61 6e 64 20 69 73  page iTab and is
2d51a 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 61   found in databa
2d51b 73 65 20 69 44 62 2e 0d 0a 2a 2a 20 41 20 72 65  se iDb...** A re
2d51c 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c 6f  ad or a write lo
2d51d 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20  ck can be taken 
2d51e 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57  depending on isW
2d51f 72 69 74 65 6c 6f 63 6b 2e 0d 0a 2a 2a 0d 0a 2a  ritelock...**..*
2d520 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a  * This routine j
2d521 75 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20  ust records the 
2d522 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f  fact that the lo
2d523 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20  ck is desired.  
2d524 54 68 65 0d 0a 2a 2a 20 63 6f 64 65 20 74 6f 20  The..** code to 
2d525 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63  make the lock oc
2d526 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 65 64  cur is generated
2d527 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c   by a later call
2d528 20 74 6f 0d 0a 2a 2a 20 63 6f 64 65 54 61 62 6c   to..** codeTabl
2d529 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f  eLocks() which o
2d52a 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c  ccurs during sql
2d52b 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
2d52c 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ()...*/..SQLITE_
2d52d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2d52e 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0d 0a  ite3TableLock(..
2d52f 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2d530 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2d531 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e  context */..  in
2d532 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
2d533 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2d534 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
2d535 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74  ning the table t
2d536 6f 20 6c 6f 63 6b 20 2a 2f 0d 0a 20 20 69 6e 74  o lock */..  int
2d537 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20   iTab,          
2d538 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75 6d  /* Root page num
2d539 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ber of the table
2d53a 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
2d53b 0d 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  ..  u8 isWriteLo
2d53c 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66  ck,    /* True f
2d53d 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20  or a write lock 
2d53e 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
2d53f 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65   *zName  /* Name
2d540 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2d541 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0d 0a 29   be locked */..)
2d542 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  {..  Parse *pTop
2d543 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
2d544 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
2d545 72 73 65 29 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d  rse);..  int i;.
2d546 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0d 0a  .  int nBytes;..
2d547 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0d    TableLock *p;.
2d548 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
2d549 30 20 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d  0 );....  for(i=
2d54a 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e  0; i<pToplevel->
2d54b 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29  nTableLock; i++)
2d54c 7b 0d 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70  {..    p = &pTop
2d54d 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63  level->aTableLoc
2d54e 6b 5b 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70  k[i];..    if( p
2d54f 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d  ->iDb==iDb && p-
2d550 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0d 0a  >iTab==iTab ){..
2d551 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65        p->isWrite
2d552 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69  Lock = (p->isWri
2d553 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74  teLock || isWrit
2d554 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20 20 20 20 72  eLock);..      r
2d555 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20  eturn;..    }.. 
2d556 20 7d 0d 0a 0d 0a 20 20 6e 42 79 74 65 73 20 3d   }....  nBytes =
2d557 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63   sizeof(TableLoc
2d558 6b 29 20 2a 20 28 70 54 6f 70 6c 65 76 65 6c 2d  k) * (pToplevel-
2d559 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0d  >nTableLock+1);.
2d55a 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54  .  pToplevel->aT
2d55b 61 62 6c 65 4c 6f 63 6b 20 3d 0d 0a 20 20 20 20  ableLock =..    
2d55c 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c    sqlite3DbReall
2d55d 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c 65 76  ocOrFree(pToplev
2d55e 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65 76 65  el->db, pTopleve
2d55f 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  l->aTableLock, n
2d560 42 79 74 65 73 29 3b 0d 0a 20 20 69 66 28 20 70  Bytes);..  if( p
2d561 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65  Toplevel->aTable
2d562 4c 6f 63 6b 20 29 7b 0d 0a 20 20 20 20 70 20 3d  Lock ){..    p =
2d563 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61   &pToplevel->aTa
2d564 62 6c 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65  bleLock[pTopleve
2d565 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d  l->nTableLock++]
2d566 3b 0d 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20  ;..    p->iDb = 
2d567 69 44 62 3b 0d 0a 20 20 20 20 70 2d 3e 69 54 61  iDb;..    p->iTa
2d568 62 20 3d 20 69 54 61 62 3b 0d 0a 20 20 20 20 70  b = iTab;..    p
2d569 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20  ->isWriteLock = 
2d56a 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0d 0a 20 20  isWriteLock;..  
2d56b 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61    p->zName = zNa
2d56c 6d 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  me;..  }else{.. 
2d56d 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54     pToplevel->nT
2d56e 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0d 0a 20  ableLock = 0;.. 
2d56f 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62     pToplevel->db
2d570 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2d571 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f   1;..  }..}..../
2d572 2a 0d 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50  *..** Code an OP
2d573 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72  _TableLock instr
2d574 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20  uction for each 
2d575 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20  table locked by 
2d576 74 68 65 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  the..** statemen
2d577 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79  t (configured by
2d578 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
2d579 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0d 0a  3TableLock())...
2d57a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2d57b 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50  codeTableLocks(P
2d57c 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0d 0a  arse *pParse){..
2d57d 20 20 69 6e 74 20 69 3b 0d 0a 20 20 56 64 62 65    int i;..  Vdbe
2d57e 20 2a 70 56 64 62 65 3b 20 0d 0a 0d 0a 20 20 70   *pVdbe; ....  p
2d57f 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Vdbe = sqlite3Ge
2d580 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a  tVdbe(pParse);..
2d581 20 20 61 73 73 65 72 74 28 20 70 56 64 62 65 21    assert( pVdbe!
2d582 3d 30 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65 33  =0 ); /* sqlite3
2d583 47 65 74 56 64 62 65 20 63 61 6e 6e 6f 74 20 66  GetVdbe cannot f
2d584 61 69 6c 3a 20 56 44 42 45 20 61 6c 72 65 61 64  ail: VDBE alread
2d585 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0d 0a  y allocated */..
2d586 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
2d587 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63  Parse->nTableLoc
2d588 6b 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 54 61  k; i++){..    Ta
2d589 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50  bleLock *p = &pP
2d58a 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
2d58b 5b 69 5d 3b 0d 0a 20 20 20 20 69 6e 74 20 70 31  [i];..    int p1
2d58c 20 3d 20 70 2d 3e 69 44 62 3b 0d 0a 20 20 20 20   = p->iDb;..    
2d58d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d58e 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c  4(pVdbe, OP_Tabl
2d58f 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54  eLock, p1, p->iT
2d590 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f  ab, p->isWriteLo
2d591 63 6b 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ck,..           
2d592 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e             p->zN
2d593 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
2d594 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73 65 0d  ..  }..}..#else.
2d595 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65 54  .  #define codeT
2d596 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0d 0a 23 65  ableLocks(x)..#e
2d597 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ndif..../*..** T
2d598 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2d599 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 73 69  alled after a si
2d59a 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65  ngle SQL stateme
2d59b 6e 74 20 68 61 73 20 62 65 65 6e 0d 0a 2a 2a 20  nt has been..** 
2d59c 70 61 72 73 65 64 20 61 6e 64 20 61 20 56 44 42  parsed and a VDB
2d59d 45 20 70 72 6f 67 72 61 6d 20 74 6f 20 65 78 65  E program to exe
2d59e 63 75 74 65 20 74 68 61 74 20 73 74 61 74 65 6d  cute that statem
2d59f 65 6e 74 20 68 61 73 20 62 65 65 6e 0d 0a 2a 2a  ent has been..**
2d5a0 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 69 73   prepared.  This
2d5a1 20 72 6f 75 74 69 6e 65 20 70 75 74 73 20 74 68   routine puts th
2d5a2 65 20 66 69 6e 69 73 68 69 6e 67 20 74 6f 75 63  e finishing touc
2d5a3 68 65 73 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20 56  hes on the..** V
2d5a4 44 42 45 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  DBE program and 
2d5a5 72 65 73 65 74 73 20 74 68 65 20 70 50 61 72 73  resets the pPars
2d5a6 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
2d5a7 74 68 65 20 6e 65 78 74 0d 0a 2a 2a 20 70 61 72  the next..** par
2d5a8 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65  se...**..** Note
2d5a9 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f   that if an erro
2d5aa 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d  r occurred, it m
2d5ab 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65  ight be the case
2d5ac 20 74 68 61 74 0d 0a 2a 2a 20 6e 6f 20 56 44 42   that..** no VDB
2d5ad 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72  E code was gener
2d5ae 61 74 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ated...*/..SQLIT
2d5af 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2d5b0 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
2d5b1 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ng(Parse *pParse
2d5b2 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ){..  sqlite3 *d
2d5b3 62 3b 0d 0a 20 20 56 64 62 65 20 2a 76 3b 0d 0a  b;..  Vdbe *v;..
2d5b4 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
2d5b5 3e 64 62 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e  >db;..  if( db->
2d5b6 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2d5b7 65 74 75 72 6e 3b 0d 0a 20 20 69 66 28 20 70 50  eturn;..  if( pP
2d5b8 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72  arse->nested ) r
2d5b9 65 74 75 72 6e 3b 0d 0a 20 20 69 66 28 20 70 50  eturn;..  if( pP
2d5ba 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74  arse->nErr ) ret
2d5bb 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 42 65 67  urn;....  /* Beg
2d5bc 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67  in by generating
2d5bd 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f   some terminatio
2d5be 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e  n code at the en
2d5bf 64 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 76  d of the..  ** v
2d5c0 64 62 65 20 70 72 6f 67 72 61 6d 0d 0a 20 20 2a  dbe program..  *
2d5c1 2f 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  /..  v = sqlite3
2d5c2 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2d5c3 0d 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ..  assert( !pPa
2d5c4 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74  rse->isMultiWrit
2d5c5 65 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  e ..       || sq
2d5c6 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d  lite3VdbeAssertM
2d5c7 61 79 41 62 6f 72 74 28 76 2c 20 70 50 61 72 73  ayAbort(v, pPars
2d5c8 65 2d 3e 6d 61 79 41 62 6f 72 74 29 29 3b 0d 0a  e->mayAbort));..
2d5c9 20 20 69 66 28 20 76 20 29 7b 0d 0a 20 20 20 20    if( v ){..    
2d5ca 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d5cb 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0d 0a  0(v, OP_Halt);..
2d5cc 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f  ..    /* The coo
2d5cd 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e  kie mask contain
2d5ce 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61  s one bit for ea
2d5cf 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
2d5d0 20 6f 70 65 6e 2e 0d 0a 20 20 20 20 2a 2a 20 28   open...    ** (
2d5d1 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69  Bit 0 is for mai
2d5d2 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 20  n, bit 1 is for 
2d5d3 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72  temp, and so for
2d5d4 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0d 0a  th.)  Bits are..
2d5d5 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65      ** set for e
2d5d6 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61  ach database tha
2d5d7 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65  t is used.  Gene
2d5d8 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61  rate code to sta
2d5d9 72 74 20 61 0d 0a 20 20 20 20 2a 2a 20 74 72 61  rt a..    ** tra
2d5da 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68  nsaction on each
2d5db 20 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61   used database a
2d5dc 6e 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65  nd to verify the
2d5dd 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0d 0a   schema cookie..
2d5de 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75      ** on each u
2d5df 73 65 64 20 64 61 74 61 62 61 73 65 2e 0d 0a 20  sed database... 
2d5e0 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70     */..    if( p
2d5e1 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
2d5e2 6f 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 79 44  o>0 ){..      yD
2d5e3 62 4d 61 73 6b 20 6d 61 73 6b 3b 0d 0a 20 20 20  bMask mask;..   
2d5e4 20 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20 20 20     int iDb;..   
2d5e5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2d5e6 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72 73 65  mpHere(v, pParse
2d5e7 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29 3b  ->cookieGoto-1);
2d5e8 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d  ..      for(iDb=
2d5e9 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64  0, mask=1; iDb<d
2d5ea 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31  b->nDb; mask<<=1
2d5eb 2c 20 69 44 62 2b 2b 29 7b 0d 0a 20 20 20 20 20  , iDb++){..     
2d5ec 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70     if( (mask & p
2d5ed 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73  Parse->cookieMas
2d5ee 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  k)==0 ) continue
2d5ef 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
2d5f0 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
2d5f1 76 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20  v, iDb);..      
2d5f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d5f3 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63  Op2(v,OP_Transac
2d5f4 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b  tion, iDb, (mask
2d5f5 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65   & pParse->write
2d5f6 4d 61 73 6b 29 21 3d 30 29 3b 0d 0a 20 20 20 20  Mask)!=0);..    
2d5f7 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
2d5f8 2e 62 75 73 79 3d 3d 30 20 29 7b 0d 0a 20 20 20  .busy==0 ){..   
2d5f9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
2d5fa 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65  qlite3SchemaMute
2d5fb 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30  xHeld(db, iDb, 0
2d5fc 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) );..          
2d5fd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d5fe 33 28 76 2c 20 4f 50 5f 56 65 72 69 66 79 43 6f  3(v, OP_VerifyCo
2d5ff 6f 6b 69 65 2c 0d 0a 20 20 20 20 20 20 20 20 20  okie,..         
2d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d601 20 20 20 69 44 62 2c 20 70 50 61 72 73 65 2d 3e     iDb, pParse->
2d602 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d  cookieValue[iDb]
2d603 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
2d604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
2d605 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
2d606 65 6d 61 2d 3e 69 47 65 6e 65 72 61 74 69 6f 6e  ema->iGeneration
2d607 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
2d608 20 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20       }..#ifndef 
2d609 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2d60a 55 41 4c 54 41 42 4c 45 0d 0a 20 20 20 20 20 20  UALTABLE..      
2d60b 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  {..        int i
2d60c 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
2d60d 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56  =0; i<pParse->nV
2d60e 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0d 0a  tabLock; i++){..
2d60f 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2d610 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 73  vtab = (char *)s
2d611 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2d612 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74  db, pParse->apVt
2d613 61 62 4c 6f 63 6b 5b 69 5d 29 3b 0d 0a 20 20 20  abLock[i]);..   
2d614 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2d615 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
2d616 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c 20  Begin, 0, 0, 0, 
2d617 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0d  vtab, P4_VTAB);.
2d618 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
2d619 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61      pParse->nVta
2d61a 62 4c 6f 63 6b 20 3d 20 30 3b 0d 0a 20 20 20 20  bLock = 0;..    
2d61b 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20    }..#endif.... 
2d61c 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c       /* Once all
2d61d 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76   the cookies hav
2d61e 65 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20  e been verified 
2d61f 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  and transactions
2d620 20 6f 70 65 6e 65 64 2c 20 0d 0a 20 20 20 20 20   opened, ..     
2d621 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72   ** obtain the r
2d622 65 71 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f  equired table-lo
2d623 63 6b 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e  cks. This is a n
2d624 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20  o-op unless the 
2d625 0d 0a 20 20 20 20 20 20 2a 2a 20 73 68 61 72 65  ..      ** share
2d626 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20  d-cache feature 
2d627 69 73 20 65 6e 61 62 6c 65 64 2e 0d 0a 20 20 20  is enabled...   
2d628 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 63 6f 64     */..      cod
2d629 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72  eTableLocks(pPar
2d62a 73 65 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  se);....      /*
2d62b 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 20   Initialize any 
2d62c 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 64 61  AUTOINCREMENT da
2d62d 74 61 20 73 74 72 75 63 74 75 72 65 73 20 72 65  ta structures re
2d62e 71 75 69 72 65 64 2e 0d 0a 20 20 20 20 20 20 2a  quired...      *
2d62f 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
2d630 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67  AutoincrementBeg
2d631 69 6e 28 70 50 61 72 73 65 29 3b 0d 0a 0d 0a 20  in(pParse);.... 
2d632 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c       /* Finally,
2d633 20 6a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68   jump back to th
2d634 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
2d635 68 65 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f  he executable co
2d636 64 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71  de. */..      sq
2d637 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2d638 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
2d639 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
2d63a 6f 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  o);..    }..  }.
2d63b 0a 0d 0a 0d 0a 20 20 2f 2a 20 47 65 74 20 74 68  .....  /* Get th
2d63c 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72  e VDBE program r
2d63d 65 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69  eady for executi
2d63e 6f 6e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  on..  */..  if( 
2d63f 76 20 26 26 20 41 4c 57 41 59 53 28 70 50 61 72  v && ALWAYS(pPar
2d640 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20  se->nErr==0) && 
2d641 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2d642 64 20 29 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c  d ){..#ifdef SQL
2d643 49 54 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 46  ITE_DEBUG..    F
2d644 49 4c 45 20 2a 74 72 61 63 65 20 3d 20 28 64 62  ILE *trace = (db
2d645 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2d646 5f 56 64 62 65 54 72 61 63 65 29 21 3d 30 20 3f  _VdbeTrace)!=0 ?
2d647 20 73 74 64 6f 75 74 20 3a 20 30 3b 0d 0a 20 20   stdout : 0;..  
2d648 20 20 73 71 6c 69 74 65 33 56 64 62 65 54 72 61    sqlite3VdbeTra
2d649 63 65 28 76 2c 20 74 72 61 63 65 29 3b 0d 0a 23  ce(v, trace);..#
2d64a 65 6e 64 69 66 0d 0a 20 20 20 20 61 73 73 65 72  endif..    asser
2d64b 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68  t( pParse->iCach
2d64c 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a  eLevel==0 );  /*
2d64d 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65   Disables and re
2d64e 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a  -enables match *
2d64f 2f 0d 0a 20 20 20 20 2f 2a 20 41 20 6d 69 6e 69  /..    /* A mini
2d650 6d 75 6d 20 6f 66 20 6f 6e 65 20 63 75 72 73 6f  mum of one curso
2d651 72 20 69 73 20 72 65 71 75 69 72 65 64 20 69 66  r is required if
2d652 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69   autoincrement i
2d653 73 20 75 73 65 64 0d 0a 20 20 20 20 2a 20 20 53  s used..    *  S
2d654 65 65 20 74 69 63 6b 65 74 20 5b 61 36 39 36 33  ee ticket [a6963
2d655 37 39 63 31 66 30 38 38 36 36 5d 20 2a 2f 0d 0a  79c1f08866] */..
2d656 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
2d657 70 41 69 6e 63 21 3d 30 20 26 26 20 70 50 61 72  pAinc!=0 && pPar
2d658 73 65 2d 3e 6e 54 61 62 3d 3d 30 20 29 20 70 50  se->nTab==0 ) pP
2d659 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b 0d  arse->nTab = 1;.
2d65a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d65b 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61  MakeReady(v, pPa
2d65c 72 73 65 29 3b 0d 0a 20 20 20 20 70 50 61 72 73  rse);..    pPars
2d65d 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44  e->rc = SQLITE_D
2d65e 4f 4e 45 3b 0d 0a 20 20 20 20 70 50 61 72 73 65  ONE;..    pParse
2d65f 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20  ->colNamesSet = 
2d660 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  0;..  }else{..  
2d661 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2d662 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
2d663 7d 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  }..  pParse->nTa
2d664 62 20 3d 20 30 3b 0d 0a 20 20 70 50 61 72 73 65  b = 0;..  pParse
2d665 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0d 0a 20 20 70  ->nMem = 0;..  p
2d666 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30 3b  Parse->nSet = 0;
2d667 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  ..  pParse->nVar
2d668 20 3d 20 30 3b 0d 0a 20 20 70 50 61 72 73 65 2d   = 0;..  pParse-
2d669 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b  >cookieMask = 0;
2d66a 0d 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  ..  pParse->cook
2d66b 69 65 47 6f 74 6f 20 3d 20 30 3b 0d 0a 7d 0d 0a  ieGoto = 0;..}..
2d66c 0d 0a 2f 2a 0d 0a 2a 2a 20 52 75 6e 20 74 68 65  ../*..** Run the
2d66d 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65   parser and code
2d66e 20 67 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72   generator recur
2d66f 73 69 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20  sively in order 
2d670 74 6f 20 67 65 6e 65 72 61 74 65 0d 0a 2a 2a 20  to generate..** 
2d671 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 51 4c  code for the SQL
2d672 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e   statement given
2d673 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
2d674 20 74 68 65 20 70 50 61 72 73 65 20 63 6f 6e 74   the pParse cont
2d675 65 78 74 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  ext..** currentl
2d676 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
2d677 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74 68 65 20  tion.  When the 
2d678 70 61 72 73 65 72 20 69 73 20 72 75 6e 20 72 65  parser is run re
2d679 63 75 72 73 69 76 65 6c 79 0d 0a 2a 2a 20 74 68  cursively..** th
2d67a 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 6e 61  is way, the fina
2d67b 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e 6f 74  l OP_Halt is not
2d67c 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 6f 74   appended and ot
2d67d 68 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  her initializati
2d67e 6f 6e 0d 0a 2a 2a 20 61 6e 64 20 66 69 6e 61 6c  on..** and final
2d67f 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20 61 72  ization steps ar
2d680 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61 75 73  e omitted becaus
2d681 65 20 74 68 6f 73 65 20 61 72 65 20 68 61 6e 64  e those are hand
2d682 6c 69 6e 67 20 62 79 20 74 68 65 0d 0a 2a 2a 20  ling by the..** 
2d683 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65 72  outermost parser
2d684 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 20 65 76  ...**..** Not ev
2d685 65 72 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74  erything is nest
2d686 61 62 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69  able.  This faci
2d687 6c 69 74 79 20 69 73 20 64 65 73 69 67 6e 65 64  lity is designed
2d688 20 74 6f 20 70 65 72 6d 69 74 0d 0a 2a 2a 20 49   to permit..** I
2d689 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61  NSERT, UPDATE, a
2d68a 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74  nd DELETE operat
2d68b 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 51 4c  ions against SQL
2d68c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 65  ITE_MASTER.  Use
2d68d 0d 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f 75  ..** care if you
2d68e 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 74   decide to try t
2d68f 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69  o use this routi
2d690 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65  ne for some othe
2d691 72 20 70 75 72 70 6f 73 65 73 2e 0d 0a 2a 2f 0d  r purposes...*/.
2d692 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2d693 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74  void sqlite3Nest
2d694 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70  edParse(Parse *p
2d695 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
2d696 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
2d697 7b 0d 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  {..  va_list ap;
2d698 0d 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0d  ..  char *zSql;.
2d699 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
2d69a 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33   = 0;..  sqlite3
2d69b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2d69c 62 3b 0d 0a 23 20 64 65 66 69 6e 65 20 53 41 56  b;..# define SAV
2d69d 45 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61  E_SZ  (sizeof(Pa
2d69e 72 73 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28  rse) - offsetof(
2d69f 50 61 72 73 65 2c 6e 56 61 72 29 29 0d 0a 20 20  Parse,nVar))..  
2d6a0 63 68 61 72 20 73 61 76 65 42 75 66 5b 53 41 56  char saveBuf[SAV
2d6a1 45 5f 53 5a 5d 3b 0d 0a 0d 0a 20 20 69 66 28 20  E_SZ];....  if( 
2d6a2 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72  pParse->nErr ) r
2d6a3 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73 65 72 74  eturn;..  assert
2d6a4 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
2d6a5 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74 69  <10 );  /* Nesti
2d6a6 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62  ng should only b
2d6a7 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64 65 70  e of limited dep
2d6a8 74 68 20 2a 2f 0d 0a 20 20 76 61 5f 73 74 61 72  th */..  va_star
2d6a9 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d  t(ap, zFormat);.
2d6aa 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
2d6ab 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
2d6ac 6f 72 6d 61 74 2c 20 61 70 29 3b 0d 0a 20 20 76  ormat, ap);..  v
2d6ad 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20 69 66  a_end(ap);..  if
2d6ae 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0d 0a 20 20  ( zSql==0 ){..  
2d6af 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 41    return;   /* A
2d6b0 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76   malloc must hav
2d6b1 65 20 66 61 69 6c 65 64 20 2a 2f 0d 0a 20 20 7d  e failed */..  }
2d6b2 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  ..  pParse->nest
2d6b3 65 64 2b 2b 3b 0d 0a 20 20 6d 65 6d 63 70 79 28  ed++;..  memcpy(
2d6b4 73 61 76 65 42 75 66 2c 20 26 70 50 61 72 73 65  saveBuf, &pParse
2d6b5 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29  ->nVar, SAVE_SZ)
2d6b6 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61  ;..  memset(&pPa
2d6b7 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41  rse->nVar, 0, SA
2d6b8 56 45 5f 53 5a 29 3b 0d 0a 20 20 73 71 6c 69 74  VE_SZ);..  sqlit
2d6b9 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
2d6ba 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d  se, zSql, &zErrM
2d6bb 73 67 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44  sg);..  sqlite3D
2d6bc 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73  bFree(db, zErrMs
2d6bd 67 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62  g);..  sqlite3Db
2d6be 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0d  Free(db, zSql);.
2d6bf 0a 20 20 6d 65 6d 63 70 79 28 26 70 50 61 72 73  .  memcpy(&pPars
2d6c0 65 2d 3e 6e 56 61 72 2c 20 73 61 76 65 42 75 66  e->nVar, saveBuf
2d6c1 2c 20 53 41 56 45 5f 53 5a 29 3b 0d 0a 20 20 70  , SAVE_SZ);..  p
2d6c2 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b  Parse->nested--;
2d6c3 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f  ..}..../*..** Lo
2d6c4 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
2d6c5 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
2d6c6 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61  t describes a pa
2d6c7 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
2d6c8 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65  e..** table give
2d6c9 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
2d6ca 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70  at table and (op
2d6cb 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61  tionally) the na
2d6cc 6d 65 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 64 61  me of the..** da
2d6cd 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2d6ce 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65  g the table.  Re
2d6cf 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74  turn NULL if not
2d6d0 20 66 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   found...**..** 
2d6d1 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
2d6d2 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
2d6d3 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
2d6d4 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  r the table and 
2d6d5 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 6d 61  the..** first ma
2d6d6 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20  tching table is 
2d6d7 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
2d6d8 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c  hecking for dupl
2d6d9 69 63 61 74 65 20 74 61 62 6c 65 0d 0a 2a 2a 20  icate table..** 
2d6da 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20  names is done.) 
2d6db 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65   The search orde
2d6dc 72 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c  r is TEMP first,
2d6dd 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e   then MAIN, then
2d6de 20 61 6e 79 0d 0a 2a 2a 20 61 75 78 69 6c 69 61   any..** auxilia
2d6df 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64  ry databases add
2d6e0 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ed using the ATT
2d6e1 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0d 0a 2a 2a  ACH command...**
2d6e2 0d 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  ..** See also sq
2d6e3 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
2d6e4 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ()...*/..SQLITE_
2d6e5 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
2d6e6 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
2d6e7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
2d6e8 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2d6e9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
2d6ea 61 62 61 73 65 29 7b 0d 0a 20 20 54 61 62 6c 65  abase){..  Table
2d6eb 20 2a 70 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20   *p = 0;..  int 
2d6ec 69 3b 0d 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  i;..  int nName;
2d6ed 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d  ..  assert( zNam
2d6ee 65 21 3d 30 20 29 3b 0d 0a 20 20 6e 4e 61 6d 65  e!=0 );..  nName
2d6ef 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2d6f0 33 30 28 7a 4e 61 6d 65 29 3b 0d 0a 20 20 2f 2a  30(zName);..  /*
2d6f1 20 41 6c 6c 20 6d 75 74 65 78 65 73 20 61 72 65   All mutexes are
2d6f2 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 63   required for sc
2d6f3 68 65 6d 61 20 61 63 63 65 73 73 2e 20 20 4d 61  hema access.  Ma
2d6f4 6b 65 20 73 75 72 65 20 77 65 20 68 6f 6c 64 20  ke sure we hold 
2d6f5 74 68 65 6d 2e 20 2a 2f 0d 0a 20 20 61 73 73 65  them. */..  asse
2d6f6 72 74 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30  rt( zDatabase!=0
2d6f7 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
2d6f8 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2d6f9 64 62 29 20 29 3b 0d 0a 20 20 66 6f 72 28 69 3d  db) );..  for(i=
2d6fa 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64  OMIT_TEMPDB; i<d
2d6fb 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20  b->nDb; i++){.. 
2d6fc 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
2d6fd 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a   ? i^1 : i;   /*
2d6fe 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66   Search TEMP bef
2d6ff 6f 72 65 20 4d 41 49 4e 20 2a 2f 0d 0a 20 20 20  ore MAIN */..   
2d700 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d   if( zDatabase!=
2d701 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
2d702 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64  Cmp(zDatabase, d
2d703 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
2d704 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20   ) continue;..  
2d705 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2d706 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64  3SchemaMutexHeld
2d707 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0d 0a 20  (db, j, 0) );.. 
2d708 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61     p = sqlite3Ha
2d709 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b  shFind(&db->aDb[
2d70a 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  j].pSchema->tblH
2d70b 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ash, zName, nNam
2d70c 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 20 29  e);..    if( p )
2d70d 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20 20   break;..  }..  
2d70e 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a  return p;..}....
2d70f 2f 2a 0d 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68  /*..** Locate th
2d710 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75  e in-memory stru
2d711 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
2d712 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61  ibes a particula
2d713 72 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a 20 74  r database..** t
2d714 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e  able given the n
2d715 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c  ame of that tabl
2d716 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c  e and (optionall
2d717 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  y) the name of t
2d718 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  he..** database 
2d719 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
2d71a 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55  able.  Return NU
2d71b 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  LL if not found.
2d71c 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0d    Also leave an.
2d71d 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67  .** error messag
2d71e 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
2d71f 72 4d 73 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  rMsg...**..** Th
2d720 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
2d721 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  ween this routin
2d722 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46 69 6e  e and sqlite3Fin
2d723 64 54 61 62 6c 65 28 29 20 69 73 20 74 68 61 74  dTable() is that
2d724 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e   this..** routin
2d725 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72 6f  e leaves an erro
2d726 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
2d727 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65  rse->zErrMsg whe
2d728 72 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69  re..** sqlite3Fi
2d729 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e  ndTable() does n
2d72a 6f 74 2e 0d 0a 2a 2f 0d 0a 69 6e 74 20 63 6f 6e  ot...*/..int con
2d72b 6e 65 63 74 31 43 54 61 62 6c 65 28 73 71 6c 69  nect1CTable(sqli
2d72c 74 65 33 2a 62 2c 20 63 6f 6e 73 74 20 63 68 61  te3*b, const cha
2d72d 72 2a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50  r*);....SQLITE_P
2d72e 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
2d72f 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
2d730 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
2d731 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63  se,         /* c
2d732 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20  ontext in which 
2d733 74 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73  to report errors
2d734 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 56 69 65   */..  int isVie
2d735 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  w,            /*
2d736 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67   True if looking
2d737 20 66 6f 72 20 61 20 56 49 45 57 20 72 61 74 68   for a VIEW rath
2d738 65 72 20 74 68 61 6e 20 61 20 54 41 42 4c 45 20  er than a TABLE 
2d739 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
2d73a 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20   *zName,     /* 
2d73b 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
2d73c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  e we are looking
2d73d 20 66 6f 72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74   for */..  const
2d73e 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20   char *zDbase   
2d73f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2d740 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68   database.  Migh
2d741 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0d 0a 29 7b  t be NULL */..){
2d742 0d 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0d 0a 0d  ..  Table *p;...
2d743 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64  .  /* Read the d
2d744 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
2d745 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2d746 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72  rs, leave an err
2d747 6f 72 20 6d 65 73 73 61 67 65 0d 0a 20 20 2a 2a  or message..  **
2d748 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61   and code in pPa
2d749 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  rse and return N
2d74a 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 53  ULL. */..  if( S
2d74b 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
2d74c 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
2d74d 73 65 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  se) ){..    retu
2d74e 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 0;..  }....  
2d74f 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  p = sqlite3FindT
2d750 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
2d751 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b   zName, zDbase);
2d752 0d 0a 20 20 69 66 28 70 20 3d 3d 20 30 20 26 26  ..  if(p == 0 &&
2d753 20 21 69 73 56 69 65 77 20 26 26 20 21 7a 44 62   !isView && !zDb
2d754 61 73 65 29 0d 0a 20 20 7b 0d 0a 09 20 20 69 66  ase)..  {...  if
2d755 28 63 6f 6e 6e 65 63 74 31 43 54 61 62 6c 65 28  (connect1CTable(
2d756 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d  pParse->db, zNam
2d757 65 29 29 0d 0a 09 09 20 20 70 20 3d 20 73 71 6c  e))....  p = sql
2d758 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50  ite3FindTable(pP
2d759 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c  arse->db, zName,
2d75a 20 7a 44 62 61 73 65 29 3b 0d 0a 20 20 7d 0d 0a   zDbase);..  }..
2d75b 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0d 0a 20    if( p==0 ){.. 
2d75c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2d75d 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20 22  Msg = isView ? "
2d75e 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a 20  no such view" : 
2d75f 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22 3b  "no such table";
2d760 0d 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65  ..    if( zDbase
2d761 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
2d762 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2d763 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20  e, "%s: %s.%s", 
2d764 7a 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e  zMsg, zDbase, zN
2d765 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  ame);..    }else
2d766 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2d767 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2d768 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c   "%s: %s", zMsg,
2d769 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 0d   zName);..    }.
2d76a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65  .    pParse->che
2d76b 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0d 0a 20  ckSchema = 1;.. 
2d76c 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d   }..  return p;.
2d76d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63  .}..../*..** Loc
2d76e 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
2d76f 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
2d770 20 64 65 73 63 72 69 62 65 73 20 0d 0a 2a 2a 20   describes ..** 
2d771 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64  a particular ind
2d772 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  ex given the nam
2d773 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0d  e of that index.
2d774 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65  .** and the name
2d775 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2d776 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
2d777 68 65 20 69 6e 64 65 78 2e 0d 0a 2a 2a 20 52 65  he index...** Re
2d778 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74  turn NULL if not
2d779 20 66 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   found...**..** 
2d77a 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
2d77b 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
2d77c 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
2d77d 72 20 74 68 65 0d 0a 2a 2a 20 74 61 62 6c 65 20  r the..** table 
2d77e 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61  and the first ma
2d77f 74 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20  tching index is 
2d780 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
2d781 68 65 63 6b 69 6e 67 0d 0a 2a 2a 20 66 6f 72 20  hecking..** for 
2d782 64 75 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20  duplicate index 
2d783 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20  names is done.) 
2d784 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65   The search orde
2d785 72 20 69 73 0d 0a 2a 2a 20 54 45 4d 50 20 66 69  r is..** TEMP fi
2d786 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20  rst, then MAIN, 
2d787 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 61  then any auxilia
2d788 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64  ry databases add
2d789 65 64 0d 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ed..** using the
2d78a 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e   ATTACH command.
2d78b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2d78c 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69  VATE Index *sqli
2d78d 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c  te3FindIndex(sql
2d78e 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
2d78f 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e  char *zName, con
2d790 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0d 0a  st char *zDb){..
2d791 20 20 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0d    Index *p = 0;.
2d792 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74  .  int i;..  int
2d793 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2d794 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2d795 0d 0a 20 20 2f 2a 20 41 6c 6c 20 6d 75 74 65 78  ..  /* All mutex
2d796 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20  es are required 
2d797 66 6f 72 20 73 63 68 65 6d 61 20 61 63 63 65 73  for schema acces
2d798 73 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 77 65  s.  Make sure we
2d799 20 68 6f 6c 64 20 74 68 65 6d 2e 20 2a 2f 0d 0a   hold them. */..
2d79a 20 20 61 73 73 65 72 74 28 20 7a 44 62 21 3d 30    assert( zDb!=0
2d79b 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
2d79c 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2d79d 64 62 29 20 29 3b 0d 0a 20 20 66 6f 72 28 69 3d  db) );..  for(i=
2d79e 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64  OMIT_TEMPDB; i<d
2d79f 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20  b->nDb; i++){.. 
2d7a0 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
2d7a1 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20   ? i^1 : i;  /* 
2d7a2 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f  Search TEMP befo
2d7a3 72 65 20 4d 41 49 4e 20 2a 2f 0d 0a 20 20 20 20  re MAIN */..    
2d7a4 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20  Schema *pSchema 
2d7a5 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63  = db->aDb[j].pSc
2d7a6 68 65 6d 61 3b 0d 0a 20 20 20 20 61 73 73 65 72  hema;..    asser
2d7a7 74 28 20 70 53 63 68 65 6d 61 20 29 3b 0d 0a 20  t( pSchema );.. 
2d7a8 20 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71     if( zDb && sq
2d7a9 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62  lite3StrICmp(zDb
2d7aa 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61  , db->aDb[j].zNa
2d7ab 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d  me) ) continue;.
2d7ac 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
2d7ad 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
2d7ae 65 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b  eld(db, j, 0) );
2d7af 0d 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ..    p = sqlite
2d7b0 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65  3HashFind(&pSche
2d7b1 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61  ma->idxHash, zNa
2d7b2 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0d 0a 20 20 20  me, nName);..   
2d7b3 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0d   if( p ) break;.
2d7b4 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
2d7b5 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
2d7b6 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72  eclaim the memor
2d7b7 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64  y used by an ind
2d7b8 65 78 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  ex..*/..static v
2d7b9 6f 69 64 20 66 72 65 65 49 6e 64 65 78 28 73 71  oid freeIndex(sq
2d7ba 6c 69 74 65 33 20 2a 64 62 2c 20 49 6e 64 65 78  lite3 *db, Index
2d7bb 20 2a 70 29 7b 0d 0a 23 69 66 6e 64 65 66 20 53   *p){..#ifndef S
2d7bc 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
2d7bd 5a 45 0d 0a 20 20 73 71 6c 69 74 65 33 44 65 6c  ZE..  sqlite3Del
2d7be 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28  eteIndexSamples(
2d7bf 64 62 2c 20 70 29 3b 0d 0a 23 65 6e 64 69 66 0d  db, p);..#endif.
2d7c0 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2d7c1 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29  (db, p->zColAff)
2d7c2 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  ;..  sqlite3DbFr
2d7c3 65 65 28 64 62 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d  ee(db, p);..}...
2d7c4 0a 2f 2a 0d 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ./*..** For the 
2d7c5 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64  index called zId
2d7c6 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20 66  xName which is f
2d7c7 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
2d7c8 62 61 73 65 20 69 44 62 2c 0d 0a 2a 2a 20 75 6e  base iDb,..** un
2d7c9 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20  like that index 
2d7ca 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74  from its Table t
2d7cb 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69  hen remove the i
2d7cc 6e 64 65 78 20 66 72 6f 6d 0d 0a 2a 2a 20 74 68  ndex from..** th
2d7cd 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  e index hash tab
2d7ce 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20  le and free all 
2d7cf 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
2d7d0 73 20 61 73 73 6f 63 69 61 74 65 64 0d 0a 2a 2a  s associated..**
2d7d1 20 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e   with the index.
2d7d2 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2d7d3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2d7d4 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
2d7d5 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
2d7d6 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
2d7d7 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65  t char *zIdxName
2d7d8 29 7b 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e  ){..  Index *pIn
2d7d9 64 65 78 3b 0d 0a 20 20 69 6e 74 20 6c 65 6e 3b  dex;..  int len;
2d7da 0d 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 3b  ..  Hash *pHash;
2d7db 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ....  assert( sq
2d7dc 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
2d7dd 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29  Held(db, iDb, 0)
2d7de 20 29 3b 0d 0a 20 20 70 48 61 73 68 20 3d 20 26   );..  pHash = &
2d7df 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2d7e0 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0d 0a  hema->idxHash;..
2d7e1 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53    len = sqlite3S
2d7e2 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e 61 6d 65  trlen30(zIdxName
2d7e3 29 3b 0d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73  );..  pIndex = s
2d7e4 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
2d7e5 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65  (pHash, zIdxName
2d7e6 2c 20 6c 65 6e 2c 20 30 29 3b 0d 0a 20 20 69 66  , len, 0);..  if
2d7e7 28 20 41 4c 57 41 59 53 28 70 49 6e 64 65 78 29  ( ALWAYS(pIndex)
2d7e8 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 49 6e   ){..    if( pIn
2d7e9 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
2d7ea 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0d 0a  dex==pIndex ){..
2d7eb 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54        pIndex->pT
2d7ec 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70  able->pIndex = p
2d7ed 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0d 0a 20  Index->pNext;.. 
2d7ee 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2d7ef 20 49 6e 64 65 78 20 2a 70 3b 0d 0a 20 20 20 20   Index *p;..    
2d7f0 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69    /* Justificati
2d7f1 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3b 20  on of ALWAYS(); 
2d7f2 20 54 68 65 20 69 6e 64 65 78 20 6d 75 73 74 20   The index must 
2d7f3 62 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 6f  be on the list o
2d7f4 66 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 69  f..      ** indi
2d7f5 63 65 73 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 70  ces. */..      p
2d7f6 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c   = pIndex->pTabl
2d7f7 65 2d 3e 70 49 6e 64 65 78 3b 0d 0a 20 20 20 20  e->pIndex;..    
2d7f8 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
2d7f9 70 29 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d  p) && p->pNext!=
2d7fa 70 49 6e 64 65 78 20 29 7b 20 70 20 3d 20 70 2d  pIndex ){ p = p-
2d7fb 3e 70 4e 65 78 74 3b 20 7d 0d 0a 20 20 20 20 20  >pNext; }..     
2d7fc 20 69 66 28 20 41 4c 57 41 59 53 28 70 20 26 26   if( ALWAYS(p &&
2d7fd 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65   p->pNext==pInde
2d7fe 78 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  x) ){..        p
2d7ff 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78  ->pNext = pIndex
2d800 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20  ->pNext;..      
2d801 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66 72  }..    }..    fr
2d802 65 65 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64  eeIndex(db, pInd
2d803 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d  ex);..  }..  db-
2d804 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
2d805 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0d  _InternChanges;.
2d806 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 72 61  .}..../*..** Era
2d807 73 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e  se all schema in
2d808 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
2d809 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73  he in-memory has
2d80a 68 20 74 61 62 6c 65 73 20 6f 66 0d 0a 2a 2a 20  h tables of..** 
2d80b 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
2d80c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
2d80d 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
2d80e 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 0d 0a 2a 2a  claim memory..**
2d80f 20 62 65 66 6f 72 65 20 74 68 65 20 64 61 74 61   before the data
2d810 62 61 73 65 20 63 6c 6f 73 65 73 2e 20 20 49 74  base closes.  It
2d811 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
2d812 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
2d813 6b 0d 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 77  k..** if there w
2d814 65 72 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ere schema chang
2d815 65 73 20 64 75 72 69 6e 67 20 74 68 65 20 74 72  es during the tr
2d816 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69 66 20  ansaction or if 
2d817 61 0d 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f 6f  a..** schema-coo
2d818 6b 69 65 20 6d 69 73 6d 61 74 63 68 20 6f 63 63  kie mismatch occ
2d819 75 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  urs...**..** If 
2d81a 69 44 62 3c 30 20 74 68 65 6e 20 72 65 73 65 74  iDb<0 then reset
2d81b 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63   the internal sc
2d81c 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f 72 20  hema tables for 
2d81d 61 6c 6c 20 64 61 74 61 62 61 73 65 0d 0a 2a 2a  all database..**
2d81e 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62 3e   files.  If iDb>
2d81f 3d 30 20 74 68 65 6e 20 72 65 73 65 74 20 74 68  =0 then reset th
2d820 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
2d821 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0d 0a  a for only the..
2d822 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69  ** single file i
2d823 6e 64 69 63 61 74 65 64 2e 0d 0a 2a 2f 0d 0a 53  ndicated...*/..S
2d824 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d825 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
2d826 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
2d827 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
2d828 44 62 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a  Db){..  int i, j
2d829 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  ;..  assert( iDb
2d82a 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 0d 0a 20  <db->nDb );.... 
2d82b 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0d 0a   if( iDb>=0 ){..
2d82c 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20      /* Case 1:  
2d82d 52 65 73 65 74 20 74 68 65 20 73 69 6e 67 6c 65  Reset the single
2d82e 20 73 63 68 65 6d 61 20 69 64 65 6e 74 69 66 69   schema identifi
2d82f 65 64 20 62 79 20 69 44 62 20 2a 2f 0d 0a 20 20  ed by iDb */..  
2d830 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d    Db *pDb = &db-
2d831 3e 61 44 62 5b 69 44 62 5d 3b 0d 0a 20 20 20 20  >aDb[iDb];..    
2d832 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
2d833 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
2d834 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0d 0a 20  b, iDb, 0) );.. 
2d835 20 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e     assert( pDb->
2d836 70 53 63 68 65 6d 61 21 3d 30 20 29 3b 0d 0a 20  pSchema!=0 );.. 
2d837 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61     sqlite3Schema
2d838 43 6c 65 61 72 28 70 44 62 2d 3e 70 53 63 68 65  Clear(pDb->pSche
2d839 6d 61 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49  ma);....    /* I
2d83a 66 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 6f  f any database o
2d83b 74 68 65 72 20 74 68 61 6e 20 54 45 4d 50 20 69  ther than TEMP i
2d83c 73 20 72 65 73 65 74 2c 20 74 68 65 6e 20 61 6c  s reset, then al
2d83d 73 6f 20 72 65 73 65 74 20 54 45 4d 50 0d 0a 20  so reset TEMP.. 
2d83e 20 20 20 2a 2a 20 73 69 6e 63 65 20 54 45 4d 50     ** since TEMP
2d83f 20 6d 69 67 68 74 20 62 65 20 68 6f 6c 64 69 6e   might be holdin
2d840 67 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20  g triggers that 
2d841 72 65 66 65 72 65 6e 63 65 20 74 61 62 6c 65 73  reference tables
2d842 20 69 6e 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20   in the..    ** 
2d843 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 2e 0d  other database..
2d844 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28  .    */..    if(
2d845 20 69 44 62 21 3d 31 20 29 7b 0d 0a 20 20 20 20   iDb!=1 ){..    
2d846 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
2d847 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  [1];..      asse
2d848 72 74 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  rt( pDb->pSchema
2d849 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 73 71  !=0 );..      sq
2d84a 6c 69 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72  lite3SchemaClear
2d84b 28 70 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0d  (pDb->pSchema);.
2d84c 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75  .    }..    retu
2d84d 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 43  rn;..  }..  /* C
2d84e 61 73 65 20 32 20 28 66 72 6f 6d 20 68 65 72 65  ase 2 (from here
2d84f 20 74 6f 20 74 68 65 20 65 6e 64 29 3a 20 52 65   to the end): Re
2d850 73 65 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20  set all schemas 
2d851 66 6f 72 20 61 6c 6c 20 61 74 74 61 63 68 65 64  for all attached
2d852 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 73  ..  ** databases
2d853 2e 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  . */..  assert( 
2d854 69 44 62 3c 30 20 29 3b 0d 0a 20 20 73 71 6c 69  iDb<0 );..  sqli
2d855 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
2d856 28 64 62 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30  (db);..  for(i=0
2d857 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2d858 29 7b 0d 0a 20 20 20 20 44 62 20 2a 70 44 62 20  ){..    Db *pDb 
2d859 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0d 0a  = &db->aDb[i];..
2d85a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63      if( pDb->pSc
2d85b 68 65 6d 61 20 29 7b 0d 0a 20 20 20 20 20 20 73  hema ){..      s
2d85c 71 6c 69 74 65 33 53 63 68 65 6d 61 43 6c 65 61  qlite3SchemaClea
2d85d 72 28 70 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b  r(pDb->pSchema);
2d85e 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
2d85f 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51  db->flags &= ~SQ
2d860 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
2d861 65 73 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 74  es;..  sqlite3Vt
2d862 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29  abUnlockList(db)
2d863 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
2d864 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0d 0a  eLeaveAll(db);..
2d865 0d 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 6f 72  ..  /* If one or
2d866 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 75 78   more of the aux
2d867 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
2d868 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e 20 63  files has been c
2d869 6c 6f 73 65 64 2c 0d 0a 20 20 2a 2a 20 74 68 65  losed,..  ** the
2d86a 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72  n remove them fr
2d86b 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  om the auxiliary
2d86c 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e 20   database list. 
2d86d 20 57 65 20 74 61 6b 65 20 74 68 65 0d 0a 20 20   We take the..  
2d86e 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  ** opportunity t
2d86f 6f 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73  o do this here s
2d870 69 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73  ince we have jus
2d871 74 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66  t deleted all of
2d872 20 74 68 65 0d 0a 20 20 2a 2a 20 73 63 68 65 6d   the..  ** schem
2d873 61 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e  a hash tables an
2d874 64 20 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e  d therefore do n
2d875 6f 74 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20  ot have to make 
2d876 61 6e 79 20 63 68 61 6e 67 65 73 0d 0a 20 20 2a  any changes..  *
2d877 2a 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73  * to any of thos
2d878 65 20 74 61 62 6c 65 73 2e 0d 0a 20 20 2a 2f 0d  e tables...  */.
2d879 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c  .  for(i=j=2; i<
2d87a 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a  db->nDb; i++){..
2d87b 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70      struct Db *p
2d87c 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d  Db = &db->aDb[i]
2d87d 3b 0d 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e  ;..    if( pDb->
2d87e 70 42 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  pBt==0 ){..     
2d87f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2d880 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0d  b, pDb->zName);.
2d881 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
2d882 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 63 6f  e = 0;..      co
2d883 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a  ntinue;..    }..
2d884 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b 0d 0a      if( j<i ){..
2d885 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a 5d        db->aDb[j]
2d886 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0d 0a   = db->aDb[i];..
2d887 20 20 20 20 7d 0d 0a 20 20 20 20 6a 2b 2b 3b 0d      }..    j++;.
2d888 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 65 74 28 26  .  }..  memset(&
2d889 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28  db->aDb[j], 0, (
2d88a 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f  db->nDb-j)*sizeo
2d88b 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0d  f(db->aDb[j]));.
2d88c 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0d  .  db->nDb = j;.
2d88d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d  .  if( db->nDb<=
2d88e 32 20 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62  2 && db->aDb!=db
2d88f 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0d 0a  ->aDbStatic ){..
2d890 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61      memcpy(db->a
2d891 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e 61 44  DbStatic, db->aD
2d892 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e  b, 2*sizeof(db->
2d893 61 44 62 5b 30 5d 29 29 3b 0d 0a 20 20 20 20 73  aDb[0]));..    s
2d894 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2d895 20 64 62 2d 3e 61 44 62 29 3b 0d 0a 20 20 20 20   db->aDb);..    
2d896 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44  db->aDb = db->aD
2d897 62 53 74 61 74 69 63 3b 0d 0a 20 20 7d 0d 0a 7d  bStatic;..  }..}
2d898 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
2d899 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2d89a 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20  d when a commit 
2d89b 6f 63 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  occurs...*/..SQL
2d89c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d89d 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e   sqlite3CommitIn
2d89e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71  ternalChanges(sq
2d89f 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 64  lite3 *db){..  d
2d8a0 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
2d8a1 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
2d8a2 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  s;..}..../*..** 
2d8a3 44 65 6c 65 74 65 20 6d 65 6d 6f 72 79 20 61 6c  Delete memory al
2d8a4 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20  located for the 
2d8a5 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 20  column names of 
2d8a6 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  a table or view 
2d8a7 28 74 68 65 0d 0a 2a 2a 20 54 61 62 6c 65 2e 61  (the..** Table.a
2d8a8 43 6f 6c 5b 5d 20 61 72 72 61 79 29 2e 0d 0a 2a  Col[] array)...*
2d8a9 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  /..static void s
2d8aa 71 6c 69 74 65 44 65 6c 65 74 65 43 6f 6c 75 6d  qliteDeleteColum
2d8ab 6e 4e 61 6d 65 73 28 73 71 6c 69 74 65 33 20 2a  nNames(sqlite3 *
2d8ac 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  db, Table *pTabl
2d8ad 65 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  e){..  int i;.. 
2d8ae 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0d 0a   Column *pCol;..
2d8af 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2d8b0 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 28 70  !=0 );..  if( (p
2d8b1 43 6f 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 43  Col = pTable->aC
2d8b2 6f 6c 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 66  ol)!=0 ){..    f
2d8b3 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65  or(i=0; i<pTable
2d8b4 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
2d8b5 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  l++){..      sql
2d8b6 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2d8b7 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20  Col->zName);..  
2d8b8 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2d8b9 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e  elete(db, pCol->
2d8ba 70 44 66 6c 74 29 3b 0d 0a 20 20 20 20 20 20 73  pDflt);..      s
2d8bb 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2d8bc 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0d 0a   pCol->zDflt);..
2d8bd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2d8be 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54  ree(db, pCol->zT
2d8bf 79 70 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  ype);..      sql
2d8c0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2d8c1 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0d 0a 20 20  Col->zColl);..  
2d8c2 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
2d8c3 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
2d8c4 65 2d 3e 61 43 6f 6c 29 3b 0d 0a 20 20 7d 0d 0a  e->aCol);..  }..
2d8c5 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6d 6f  }..../*..** Remo
2d8c6 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61  ve the memory da
2d8c7 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ta structures as
2d8c8 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2d8c9 65 20 67 69 76 65 6e 0d 0a 2a 2a 20 54 61 62 6c  e given..** Tabl
2d8ca 65 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61  e.  No changes a
2d8cb 72 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20  re made to disk 
2d8cc 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
2d8cd 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ..**..** This ro
2d8ce 75 74 69 6e 65 20 6a 75 73 74 20 64 65 6c 65 74  utine just delet
2d8cf 65 73 20 74 68 65 20 64 61 74 61 20 73 74 72 75  es the data stru
2d8d0 63 74 75 72 65 2e 20 20 49 74 20 64 6f 65 73 20  cture.  It does 
2d8d1 6e 6f 74 20 75 6e 6c 69 6e 6b 0d 0a 2a 2a 20 74  not unlink..** t
2d8d2 68 65 20 74 61 62 6c 65 20 64 61 74 61 20 73 74  he table data st
2d8d3 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65  ructure from the
2d8d4 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 42 75   hash table.  Bu
2d8d5 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 72 6f  t it does destro
2d8d6 79 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 72  y..** memory str
2d8d7 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 69  uctures of the i
2d8d8 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69  ndices and forei
2d8d9 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74  gn keys associat
2d8da 65 64 20 77 69 74 68 20 0d 0a 2a 2a 20 74 68 65  ed with ..** the
2d8db 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c   table...*/..SQL
2d8dc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d8dd 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
2d8de 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ble(sqlite3 *db,
2d8df 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b   Table *pTable){
2d8e0 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ..  Index *pInde
2d8e1 78 2c 20 2a 70 4e 65 78 74 3b 0d 0a 0d 0a 20 20  x, *pNext;....  
2d8e2 61 73 73 65 72 74 28 20 21 70 54 61 62 6c 65 20  assert( !pTable 
2d8e3 7c 7c 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e  || pTable->nRef>
2d8e4 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 44 6f 20  0 );....  /* Do 
2d8e5 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 74  not delete the t
2d8e6 61 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 72  able until the r
2d8e7 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72  eference count r
2d8e8 65 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a 2f 0d  eaches zero. */.
2d8e9 0a 20 20 69 66 28 20 21 70 54 61 62 6c 65 20 29  .  if( !pTable )
2d8ea 20 72 65 74 75 72 6e 3b 0d 0a 20 20 69 66 28 20   return;..  if( 
2d8eb 28 28 21 64 62 20 7c 7c 20 64 62 2d 3e 70 6e 42  ((!db || db->pnB
2d8ec 79 74 65 73 46 72 65 65 64 3d 3d 30 29 20 26 26  ytesFreed==0) &&
2d8ed 20 28 2d 2d 70 54 61 62 6c 65 2d 3e 6e 52 65 66   (--pTable->nRef
2d8ee 29 3e 30 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a  )>0) ) return;..
2d8ef 0d 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c  ..  /* Delete al
2d8f0 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
2d8f1 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74  ated with this t
2d8f2 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28  able. */..  for(
2d8f3 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d  pIndex = pTable-
2d8f4 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b  >pIndex; pIndex;
2d8f5 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0d   pIndex=pNext){.
2d8f6 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e  .    pNext = pIn
2d8f7 64 65 78 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20  dex->pNext;..   
2d8f8 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d   assert( pIndex-
2d8f9 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65  >pSchema==pTable
2d8fa 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0d 0a 20 20  ->pSchema );..  
2d8fb 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d    if( !db || db-
2d8fc 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30  >pnBytesFreed==0
2d8fd 20 29 7b 0d 0a 20 20 20 20 20 20 63 68 61 72 20   ){..      char 
2d8fe 2a 7a 4e 61 6d 65 20 3d 20 70 49 6e 64 65 78 2d  *zName = pIndex-
2d8ff 3e 7a 4e 61 6d 65 3b 20 0d 0a 20 20 20 20 20 20  >zName; ..      
2d900 54 45 53 54 4f 4e 4c 59 20 28 20 49 6e 64 65 78  TESTONLY ( Index
2d901 20 2a 70 4f 6c 64 20 3d 20 29 20 73 71 6c 69 74   *pOld = ) sqlit
2d902 65 33 48 61 73 68 49 6e 73 65 72 74 28 0d 0a 09  e3HashInsert(...
2d903 20 20 26 70 49 6e 64 65 78 2d 3e 70 53 63 68 65    &pIndex->pSche
2d904 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61  ma->idxHash, zNa
2d905 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2d906 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 30 0d 0a 20  n30(zName), 0.. 
2d907 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 61       );..      a
2d908 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20  ssert( db==0 || 
2d909 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74  sqlite3SchemaMut
2d90a 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 70 49  exHeld(db, 0, pI
2d90b 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 20 29  ndex->pSchema) )
2d90c 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2d90d 20 70 4f 6c 64 3d 3d 70 49 6e 64 65 78 20 7c 7c   pOld==pIndex ||
2d90e 20 70 4f 6c 64 3d 3d 30 20 29 3b 0d 0a 20 20 20   pOld==0 );..   
2d90f 20 7d 0d 0a 20 20 20 20 66 72 65 65 49 6e 64 65   }..    freeInde
2d910 78 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0d 0a  x(db, pIndex);..
2d911 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 44 65 6c 65    }....  /* Dele
2d912 74 65 20 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b  te any foreign k
2d913 65 79 73 20 61 74 74 61 63 68 65 64 20 74 6f 20  eys attached to 
2d914 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0d 0a  this table. */..
2d915 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74    sqlite3FkDelet
2d916 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0d 0a  e(db, pTable);..
2d917 0d 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
2d918 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2d919 65 20 69 74 73 65 6c 66 2e 0d 0a 20 20 2a 2f 0d  e itself...  */.
2d91a 0a 20 20 73 71 6c 69 74 65 44 65 6c 65 74 65 43  .  sqliteDeleteC
2d91b 6f 6c 75 6d 6e 4e 61 6d 65 73 28 64 62 2c 20 70  olumnNames(db, p
2d91c 54 61 62 6c 65 29 3b 0d 0a 20 20 73 71 6c 69 74  Table);..  sqlit
2d91d 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
2d91e 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20  ble->zName);..  
2d91f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2d920 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66  , pTable->zColAf
2d921 66 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  f);..  sqlite3Se
2d922 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2d923 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b  Table->pSelect);
2d924 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2d925 5f 4f 4d 49 54 5f 43 48 45 43 4b 0d 0a 20 20 73  _OMIT_CHECK..  s
2d926 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2d927 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68  (db, pTable->pCh
2d928 65 63 6b 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23  eck);..#endif..#
2d929 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d92a 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d  IT_VIRTUALTABLE.
2d92b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c  .  sqlite3VtabCl
2d92c 65 61 72 28 64 62 2c 20 70 54 61 62 6c 65 29 3b  ear(db, pTable);
2d92d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c 69  ..#endif..  sqli
2d92e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
2d92f 61 62 6c 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  able);..}..../*.
2d930 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67  .** Unlink the g
2d931 69 76 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20  iven table from 
2d932 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  the hash tables 
2d933 61 6e 64 20 74 68 65 20 64 65 6c 65 74 65 20 74  and the delete t
2d934 68 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72  he..** table str
2d935 75 63 74 75 72 65 20 77 69 74 68 20 61 6c 6c 20  ucture with all 
2d936 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e 64 20  its indices and 
2d937 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0d 0a 2a  foreign keys...*
2d938 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2d939 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
2d93a 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62  linkAndDeleteTab
2d93b 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  le(sqlite3 *db, 
2d93c 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63  int iDb, const c
2d93d 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0d  har *zTabName){.
2d93e 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0d 0a 20 20  .  Table *p;..  
2d93f 44 62 20 2a 70 44 62 3b 0d 0a 0d 0a 20 20 61 73  Db *pDb;....  as
2d940 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0d 0a  sert( db!=0 );..
2d941 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
2d942 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
2d943 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 54  );..  assert( zT
2d944 61 62 4e 61 6d 65 20 29 3b 0d 0a 20 20 61 73 73  abName );..  ass
2d945 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
2d946 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
2d947 69 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20 74 65  iDb, 0) );..  te
2d948 73 74 63 61 73 65 28 20 7a 54 61 62 4e 61 6d 65  stcase( zTabName
2d949 5b 30 5d 3d 3d 30 20 29 3b 20 20 2f 2a 20 5a 65  [0]==0 );  /* Ze
2d94a 72 6f 2d 6c 65 6e 67 74 68 20 74 61 62 6c 65 20  ro-length table 
2d94b 6e 61 6d 65 73 20 61 72 65 20 61 6c 6c 6f 77 65  names are allowe
2d94c 64 20 2a 2f 0d 0a 20 20 70 44 62 20 3d 20 26 64  d */..  pDb = &d
2d94d 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0d 0a 20 20  b->aDb[iDb];..  
2d94e 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  p = sqlite3HashI
2d94f 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68  nsert(&pDb->pSch
2d950 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 54  ema->tblHash, zT
2d951 61 62 4e 61 6d 65 2c 0d 0a 20 20 20 20 20 20 20  abName,..       
2d952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d953 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2d954 28 7a 54 61 62 4e 61 6d 65 29 2c 30 29 3b 0d 0a  (zTabName),0);..
2d955 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2d956 61 62 6c 65 28 64 62 2c 20 70 29 3b 0d 0a 20 20  able(db, p);..  
2d957 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
2d958 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
2d959 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  s;..}..../*..** 
2d95a 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72  Given a token, r
2d95b 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74  eturn a string t
2d95c 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  hat consists of 
2d95d 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61 74  the text of that
2d95e 0d 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61  ..** token.  Spa
2d95f 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
2d960 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0d 0a  eturned string..
2d961 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ** is obtained f
2d962 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
2d963 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66  () and must be f
2d964 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  reed by the call
2d965 69 6e 67 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ing..** function
2d966 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 79 20 71 75  ...**..** Any qu
2d967 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 20 28 65  otation marks (e
2d968 78 3a 20 20 22 6e 61 6d 65 22 2c 20 27 6e 61 6d  x:  "name", 'nam
2d969 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60  e', [name], or `
2d96a 6e 61 6d 65 60 29 20 74 68 61 74 0d 0a 2a 2a 20  name`) that..** 
2d96b 73 75 72 72 6f 75 6e 64 20 74 68 65 20 62 6f 64  surround the bod
2d96c 79 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 61  y of the token a
2d96d 72 65 20 72 65 6d 6f 76 65 64 2e 0d 0a 2a 2a 0d  re removed...**.
2d96e 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f  .** Tokens are o
2d96f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65  ften just pointe
2d970 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67  rs into the orig
2d971 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e  inal SQL text an
2d972 64 20 73 6f 0d 0a 2a 2a 20 61 72 65 20 6e 6f 74  d so..** are not
2d973 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64   \000 terminated
2d974 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72   and are not per
2d975 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65  sistent.  The re
2d976 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0d 0a 2a  turned string..*
2d977 2a 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e  * is \000 termin
2d978 61 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73  ated and is pers
2d979 69 73 74 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  istent...*/..SQL
2d97a 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
2d97b 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
2d97c 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a  mToken(sqlite3 *
2d97d 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  db, Token *pName
2d97e 29 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  ){..  char *zNam
2d97f 65 3b 0d 0a 20 20 69 66 28 20 70 4e 61 6d 65 20  e;..  if( pName 
2d980 29 7b 0d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  ){..    zName = 
2d981 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2d982 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d  (db, (char*)pNam
2d983 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b  e->z, pName->n);
2d984 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 71  ..    sqlite3Deq
2d985 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0d 0a 20 20  uote(zName);..  
2d986 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 4e 61 6d  }else{..    zNam
2d987 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72  e = 0;..  }..  r
2d988 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0d 0a 7d 0d  eturn zName;..}.
2d989 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e 20 74  .../*..** Open t
2d98a 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2d98b 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e   table stored in
2d98c 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   database number
2d98d 20 69 44 62 20 66 6f 72 0d 0a 2a 2a 20 77 72 69   iDb for..** wri
2d98e 74 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20  ting. The table 
2d98f 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  is opened using 
2d990 63 75 72 73 6f 72 20 30 2e 0d 0a 2a 2f 0d 0a 53  cursor 0...*/..S
2d991 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d992 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61  id sqlite3OpenMa
2d993 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65 20  sterTable(Parse 
2d994 2a 70 2c 20 69 6e 74 20 69 44 62 29 7b 0d 0a 20  *p, int iDb){.. 
2d995 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
2d996 65 33 47 65 74 56 64 62 65 28 70 29 3b 0d 0a 20  e3GetVdbe(p);.. 
2d997 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
2d998 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53 54 45 52  k(p, iDb, MASTER
2d999 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 48 45 4d 41  _ROOT, 1, SCHEMA
2d99a 5f 54 41 42 4c 45 28 69 44 62 29 29 3b 0d 0a 20  _TABLE(iDb));.. 
2d99b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d99c 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
2d99d 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f  te, 0, MASTER_RO
2d99e 4f 54 2c 20 69 44 62 29 3b 0d 0a 20 20 73 71 6c  OT, iDb);..  sql
2d99f 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2d9a0 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29  (v, -1, (char *)
2d9a1 35 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20 20 2f  5, P4_INT32);  /
2d9a2 2a 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62 6c 65  * 5 column table
2d9a3 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 6e 54   */..  if( p->nT
2d9a4 61 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 2d  ab==0 ){..    p-
2d9a5 3e 6e 54 61 62 20 3d 20 31 3b 0d 0a 20 20 7d 0d  >nTab = 1;..  }.
2d9a6 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72  .}..../*..** Par
2d9a7 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69  ameter zName poi
2d9a8 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72  nts to a nul-ter
2d9a9 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72 20 63  minated buffer c
2d9aa 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 61  ontaining the na
2d9ab 6d 65 0d 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61  me..** of a data
2d9ac 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74  base ("main", "t
2d9ad 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65  emp" or the name
2d9ae 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20   of an attached 
2d9af 64 62 29 2e 20 54 68 69 73 0d 0a 2a 2a 20 66 75  db). This..** fu
2d9b0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
2d9b1 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
2d9b2 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69  named database i
2d9b3 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0d  n db->aDb[], or.
2d9b4 0a 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61  .** -1 if the na
2d9b5 6d 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65  med db cannot be
2d9b6 20 66 6f 75 6e 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c   found...*/..SQL
2d9b7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2d9b8 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d  sqlite3FindDbNam
2d9b9 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  e(sqlite3 *db, c
2d9ba 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2d9bb 29 7b 0d 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31  ){..  int i = -1
2d9bc 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74  ;         /* Dat
2d9bd 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0d  abase number */.
2d9be 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0d  .  if( zName ){.
2d9bf 0a 20 20 20 20 44 62 20 2a 70 44 62 3b 0d 0a 20  .    Db *pDb;.. 
2d9c0 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
2d9c1 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2d9c2 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 28 64  );..    for(i=(d
2d9c3 62 2d 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26  b->nDb-1), pDb=&
2d9c4 64 62 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30  db->aDb[i]; i>=0
2d9c5 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0d 0a  ; i--, pDb--){..
2d9c6 20 20 20 20 20 20 69 66 28 20 28 21 4f 4d 49 54        if( (!OMIT
2d9c7 5f 54 45 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20  _TEMPDB || i!=1 
2d9c8 29 20 26 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53  ) && n==sqlite3S
2d9c9 74 72 6c 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61  trlen30(pDb->zNa
2d9ca 6d 65 29 20 26 26 20 0d 0a 20 20 20 20 20 20 20  me) && ..       
2d9cb 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72     0==sqlite3Str
2d9cc 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c  ICmp(pDb->zName,
2d9cd 20 7a 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20 20   zName) ){..    
2d9ce 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
2d9cf 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
2d9d0 0a 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a 7d 0d  .  return i;..}.
2d9d1 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 74 6f  .../*..** The to
2d9d2 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61  ken *pName conta
2d9d3 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ins the name of 
2d9d4 61 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68  a database (eith
2d9d5 65 72 20 22 6d 61 69 6e 22 20 6f 72 0d 0a 2a 2a  er "main" or..**
2d9d6 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e   "temp" or the n
2d9d7 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68  ame of an attach
2d9d8 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f 75  ed db). This rou
2d9d9 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
2d9da 0d 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68  ..** index of th
2d9db 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65  e named database
2d9dc 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f   in db->aDb[], o
2d9dd 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65  r -1 if the name
2d9de 64 20 64 62 20 0d 0a 2a 2a 20 64 6f 65 73 20 6e  d db ..** does n
2d9df 6f 74 20 65 78 69 73 74 2e 0d 0a 2a 2f 0d 0a 53  ot exist...*/..S
2d9e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2d9e1 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  t sqlite3FindDb(
2d9e2 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
2d9e3 65 6e 20 2a 70 4e 61 6d 65 29 7b 0d 0a 20 20 69  en *pName){..  i
2d9e4 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2d9e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9e6 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2d9e7 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 63 68 61  number */..  cha
2d9e8 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
2d9e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9ea 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65    /* Name we are
2d9eb 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a   searching for *
2d9ec 2f 0d 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  /..  zName = sql
2d9ed 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2d9ee 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0d 0a 20  n(db, pName);.. 
2d9ef 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   i = sqlite3Find
2d9f0 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d 65  DbName(db, zName
2d9f1 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46  );..  sqlite3DbF
2d9f2 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0d  ree(db, zName);.
2d9f3 0a 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a 7d 0d  .  return i;..}.
2d9f4 0a 0d 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  .../* The table 
2d9f5 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 67  or view or trigg
2d9f6 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65  er name is passe
2d9f7 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  d to this routin
2d9f8 65 20 76 69 61 20 74 6f 6b 65 6e 73 0d 0a 2a 2a  e via tokens..**
2d9f9 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d   pName1 and pNam
2d9fa 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65  e2. If the table
2d9fb 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20   name was fully 
2d9fc 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65  qualified, for e
2d9fd 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  xample:..**..** 
2d9fe 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78  CREATE TABLE xxx
2d9ff 2e 79 79 79 20 28 2e 2e 2e 29 3b 0d 0a 2a 2a 20  .yyy (...);..** 
2da00 0d 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31  ..** Then pName1
2da01 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22   is set to "xxx"
2da02 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79   and pName2 "yyy
2da03 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ". On the other 
2da04 68 61 6e 64 20 69 66 0d 0a 2a 2a 20 74 68 65 20  hand if..** the 
2da05 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f  table name is no
2da06 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65  t fully qualifie
2da07 64 2c 20 69 2e 65 2e 3a 0d 0a 2a 2a 0d 0a 2a 2a  d, i.e.:..**..**
2da08 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 79 79   CREATE TABLE yy
2da09 79 28 2e 2e 2e 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20  y(...);..**..** 
2da0a 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73  Then pName1 is s
2da0b 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20  et to "yyy" and 
2da0c 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0d 0a 2a  pName2 is ""...*
2da0d 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  *..** This routi
2da0e 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55  ne sets the *ppU
2da0f 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f  nqual pointer to
2da10 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f   point at the to
2da11 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0d 0a  ken (pName1 or..
2da12 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20  ** pName2) that 
2da13 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61  stores the unqua
2da14 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d  lified table nam
2da15 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  e.  The index of
2da16 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73   the..** databas
2da17 65 20 22 78 78 78 22 20 69 73 20 72 65 74 75 72  e "xxx" is retur
2da18 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ned...*/..SQLITE
2da19 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2da1a 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
2da1b 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
2da1c 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  e,      /* Parsi
2da1d 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
2da1e 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
2da1f 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  /..  Token *pNam
2da20 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e1,      /* The 
2da21 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d  "xxx" in the nam
2da22 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22  e "xxx.yyy" or "
2da23 78 78 78 22 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e  xxx" */..  Token
2da24 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f   *pName2,      /
2da25 2a 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74  * The "yyy" in t
2da26 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79  he name "xxx.yyy
2da27 22 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 2a  " */..  Token **
2da28 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57  pUnqual     /* W
2da29 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69  rite the unquali
2da2a 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65  fied object name
2da2b 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   here */..){..  
2da2c 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
2da2d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2da2e 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
2da2f 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20  the object */.. 
2da30 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2da31 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20  Parse->db;....  
2da32 69 66 28 20 41 4c 57 41 59 53 28 70 4e 61 6d 65  if( ALWAYS(pName
2da33 32 21 3d 30 29 20 26 26 20 70 4e 61 6d 65 32 2d  2!=0) && pName2-
2da34 3e 6e 3e 30 20 29 7b 0d 0a 20 20 20 20 69 66 28  >n>0 ){..    if(
2da35 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
2da36 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65   {..      sqlite
2da37 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2da38 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 61 62  , "corrupt datab
2da39 61 73 65 22 29 3b 0d 0a 20 20 20 20 20 20 70 50  ase");..      pP
2da3a 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0d 0a 20  arse->nErr++;.. 
2da3b 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0d       return -1;.
2da3c 0a 20 20 20 20 7d 0d 0a 20 20 20 20 2a 70 55 6e  .    }..    *pUn
2da3d 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0d 0a  qual = pName2;..
2da3e 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
2da3f 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d  3FindDb(db, pNam
2da40 65 31 29 3b 0d 0a 20 20 20 20 69 66 28 20 69 44  e1);..    if( iD
2da41 62 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  b<0 ){..      sq
2da42 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2da43 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64  arse, "unknown d
2da44 61 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61  atabase %T", pNa
2da45 6d 65 31 29 3b 0d 0a 20 20 20 20 20 20 70 50 61  me1);..      pPa
2da46 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0d 0a 20 20  rse->nErr++;..  
2da47 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0d 0a      return -1;..
2da48 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d      }..  }else{.
2da49 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
2da4a 3e 69 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20  >init.iDb==0 || 
2da4b 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b  db->init.busy );
2da4c 0d 0a 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e  ..    iDb = db->
2da4d 69 6e 69 74 2e 69 44 62 3b 0d 0a 20 20 20 20 2a  init.iDb;..    *
2da4e 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31  pUnqual = pName1
2da4f 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
2da50 20 69 44 62 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a   iDb;..}..../*..
2da51 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2da52 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  is used to check
2da53 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 73 74   if the UTF-8 st
2da54 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20  ring zName is a 
2da55 6c 65 67 61 6c 0d 0a 2a 2a 20 75 6e 71 75 61 6c  legal..** unqual
2da56 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61  ified name for a
2da57 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65   new schema obje
2da58 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78  ct (table, index
2da59 2c 20 76 69 65 77 20 6f 72 0d 0a 2a 2a 20 74 72  , view or..** tr
2da5a 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65  igger). All name
2da5b 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65  s are legal exce
2da5c 70 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65  pt those that be
2da5d 67 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72  gin with the str
2da5e 69 6e 67 0d 0a 2a 2a 20 22 73 71 6c 69 74 65 5f  ing..** "sqlite_
2da5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77  " (in upper, low
2da60 65 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65  er or mixed case
2da61 29 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20  ). This portion 
2da62 6f 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65  of the namespace
2da63 0d 0a 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64  ..** is reserved
2da64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
2da65 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
2da66 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2da67 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
2da68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2da69 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2da6a 6d 65 29 7b 0d 0a 20 20 69 66 28 20 21 70 50 61  me){..  if( !pPa
2da6b 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75  rse->db->init.bu
2da6c 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  sy && pParse->ne
2da6d 73 74 65 64 3d 3d 30 20 0d 0a 20 20 20 20 20 20  sted==0 ..      
2da6e 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e      && (pParse->
2da6f 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2da70 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d  TE_WriteSchema)=
2da71 3d 30 0d 0a 20 20 20 20 20 20 20 20 20 20 26 26  =0..          &&
2da72 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49   0==sqlite3StrNI
2da73 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  Cmp(zName, "sqli
2da74 74 65 5f 22 2c 20 37 29 20 29 7b 0d 0a 20 20 20  te_", 7) ){..   
2da75 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2da76 28 70 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74  (pParse, "object
2da77 20 6e 61 6d 65 20 72 65 73 65 72 76 65 64 20 66   name reserved f
2da78 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a  or internal use:
2da79 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20   %s", zName);.. 
2da7a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2da7b 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 20 20  _ERROR;..  }..  
2da7c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2da7d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 42  ;..}..../*..** B
2da7e 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e  egin constructin
2da7f 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65  g a new table re
2da80 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20  presentation in 
2da81 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73  memory.  This is
2da82 0d 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f  ..** the first o
2da83 66 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e  f several action
2da84 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67   routines that g
2da85 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73  et called in res
2da86 70 6f 6e 73 65 0d 0a 2a 2a 20 74 6f 20 61 20 43  ponse..** to a C
2da87 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2da88 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69  ement.  In parti
2da89 63 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74  cular, this rout
2da8a 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 0d 0a 2a  ine is called..*
2da8b 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67 20 74  * after seeing t
2da8c 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22 20 61  okens "CREATE" a
2da8d 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74  nd "TABLE" and t
2da8e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54  he table name. T
2da8f 68 65 20 69 73 54 65 6d 70 0d 0a 2a 2a 20 66 6c  he isTemp..** fl
2da90 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68  ag is true if th
2da91 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62  e table should b
2da92 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
2da93 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
2da94 73 65 0d 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74  se..** file inst
2da95 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61  ead of in the ma
2da96 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
2da97 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61  .  This is norma
2da98 6c 6c 79 20 74 68 65 20 63 61 73 65 0d 0a 2a 2a  lly the case..**
2da99 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 22   when the "TEMP"
2da9a 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 20   or "TEMPORARY" 
2da9b 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 69  keyword occurs i
2da9c 6e 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 43 52  n between..** CR
2da9d 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0d  EATE and TABLE..
2da9e 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 6e 65 77 20  .**..** The new 
2da9f 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20  table record is 
2daa0 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20  initialized and 
2daa1 70 75 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70  put in pParse->p
2daa2 4e 65 77 54 61 62 6c 65 2e 0d 0a 2a 2a 20 41 73  NewTable...** As
2daa3 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43 52 45   more of the CRE
2daa4 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2daa5 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20 61  ent is parsed, a
2daa6 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e  dditional action
2daa7 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69  ..** routines wi
2daa8 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20  ll be called to 
2daa9 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61  add more informa
2daaa 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65 63  tion to this rec
2daab 6f 72 64 2e 0d 0a 2a 2a 20 41 74 20 74 68 65 20  ord...** At the 
2daac 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54  end of the CREAT
2daad 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
2daae 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e  t, the sqlite3En
2daaf 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65  dTable() routine
2dab0 0d 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74  ..** is called t
2dab1 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63  o complete the c
2dab2 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74  onstruction of t
2dab3 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63  he new table rec
2dab4 6f 72 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ord...*/..SQLITE
2dab5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2dab6 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
2dab7 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
2dab8 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63  e,   /* Parser c
2dab9 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 6f 6b  ontext */..  Tok
2daba 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a  en *pName1,   /*
2dabb 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   First part of t
2dabc 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
2dabd 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0d  able or view */.
2dabe 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
2dabf 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61  ,   /* Second pa
2dac0 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f  rt of the name o
2dac1 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  f the table or v
2dac2 69 65 77 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73  iew */..  int is
2dac3 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72  Temp,      /* Tr
2dac4 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
2dac5 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  TEMP table */.. 
2dac6 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20   int isView,    
2dac7 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
2dac8 73 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0d 0a  s is a VIEW */..
2dac9 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c    int isVirtual,
2daca 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2dacb 69 73 20 69 73 20 61 20 56 49 52 54 55 41 4c 20  is is a VIRTUAL 
2dacc 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  table */..  int 
2dacd 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f 2a 20  noErr        /* 
2dace 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 61  Do nothing if ta
2dacf 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73  ble already exis
2dad0 74 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 61 62  ts */..){..  Tab
2dad1 6c 65 20 2a 70 54 61 62 6c 65 3b 0d 0a 20 20 63  le *pTable;..  c
2dad2 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
2dad3 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
2dad4 68 65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0d  he new table */.
2dad5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2dad6 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20   pParse->db;..  
2dad7 56 64 62 65 20 2a 76 3b 0d 0a 20 20 69 6e 74 20  Vdbe *v;..  int 
2dad8 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  iDb;         /* 
2dad9 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  Database number 
2dada 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61  to create the ta
2dadb 62 6c 65 20 69 6e 20 2a 2f 0d 0a 20 20 54 6f 6b  ble in */..  Tok
2dadc 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a  en *pName;    /*
2dadd 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d   Unqualified nam
2dade 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
2dadf 6f 20 63 72 65 61 74 65 20 2a 2f 0d 0a 0d 0a 20  o create */.... 
2dae0 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72   /* The table or
2dae1 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72   view name to cr
2dae2 65 61 74 65 20 69 73 20 70 61 73 73 65 64 20 74  eate is passed t
2dae3 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76  o this routine v
2dae4 69 61 20 74 6f 6b 65 6e 73 0d 0a 20 20 2a 2a 20  ia tokens..  ** 
2dae5 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65  pName1 and pName
2dae6 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  2. If the table 
2dae7 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71  name was fully q
2dae8 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78  ualified, for ex
2dae9 61 6d 70 6c 65 3a 0d 0a 20 20 2a 2a 0d 0a 20 20  ample:..  **..  
2daea 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
2daeb 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0d 0a  xxx.yyy (...);..
2daec 20 20 2a 2a 20 0d 0a 20 20 2a 2a 20 54 68 65 6e    ** ..  ** Then
2daed 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
2daee 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d  o "xxx" and pNam
2daef 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65  e2 "yyy". On the
2daf0 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0d 0a   other hand if..
2daf1 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e    ** the table n
2daf2 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79  ame is not fully
2daf3 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e   qualified, i.e.
2daf4 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 43 52  :..  **..  ** CR
2daf5 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e  EATE TABLE yyy(.
2daf6 2e 2e 29 3b 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ..);..  **..  **
2daf7 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20   Then pName1 is 
2daf8 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64  set to "yyy" and
2daf9 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0d 0a   pName2 is ""...
2dafa 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 63    **..  ** The c
2dafb 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20 74  all below sets t
2dafc 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72  he pName pointer
2dafd 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
2dafe 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f   token (pName1 o
2daff 72 0d 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20  r..  ** pName2) 
2db00 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20  that stores the 
2db01 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  unqualified tabl
2db02 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69  e name. The vari
2db03 61 62 6c 65 20 69 44 62 20 69 73 0d 0a 20 20 2a  able iDb is..  *
2db04 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64  * set to the ind
2db05 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2db06 73 65 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  se that the tabl
2db07 65 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20  e or view is to 
2db08 62 65 0d 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  be..  ** created
2db09 20 69 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 44   in...  */..  iD
2db0a 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
2db0b 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
2db0c 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
2db0d 70 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28 20 69  pName);..  if( i
2db0e 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  Db<0 ) return;..
2db0f 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2db10 44 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 20  DB && isTemp && 
2db11 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 26 26 20 69  pName2->n>0 && i
2db12 44 62 21 3d 31 20 29 7b 0d 0a 20 20 20 20 2f 2a  Db!=1 ){..    /*
2db13 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 74   If creating a t
2db14 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e  emp table, the n
2db15 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71  ame may not be q
2db16 75 61 6c 69 66 69 65 64 2e 20 55 6e 6c 65 73 73  ualified. Unless
2db17 20 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61   ..    ** the da
2db18 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22  tabase name is "
2db19 74 65 6d 70 22 20 61 6e 79 77 61 79 2e 20 20 2a  temp" anyway.  *
2db1a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  /..    sqlite3Er
2db1b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2db1c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2db1d 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 6e 71  name must be unq
2db1e 75 61 6c 69 66 69 65 64 22 29 3b 0d 0a 20 20 20  ualified");..   
2db1f 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
2db20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
2db21 42 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44  B && isTemp ) iD
2db22 62 20 3d 20 31 3b 0d 0a 0d 0a 20 20 70 50 61 72  b = 1;....  pPar
2db23 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d  se->sNameToken =
2db24 20 2a 70 4e 61 6d 65 3b 0d 0a 20 20 7a 4e 61 6d   *pName;..  zNam
2db25 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
2db26 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
2db27 6d 65 29 3b 0d 0a 20 20 69 66 28 20 7a 4e 61 6d  me);..  if( zNam
2db28 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  e==0 ) return;..
2db29 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2db2a 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a  =sqlite3CheckObj
2db2b 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  ectName(pParse, 
2db2c 7a 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20 20 67  zName) ){..    g
2db2d 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
2db2e 65 72 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 20 20 69  error;..  }..  i
2db2f 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d  f( db->init.iDb=
2db30 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b  =1 ) isTemp = 1;
2db31 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2db32 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2db33 49 4f 4e 0d 0a 20 20 61 73 73 65 72 74 28 20 28  ION..  assert( (
2db34 69 73 54 65 6d 70 20 26 20 31 29 3d 3d 69 73 54  isTemp & 1)==isT
2db35 65 6d 70 20 29 3b 0d 0a 20 20 7b 0d 0a 20 20 20  emp );..  {..   
2db36 20 69 6e 74 20 63 6f 64 65 3b 0d 0a 20 20 20 20   int code;..    
2db37 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
2db38 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d  aDb[iDb].zName;.
2db39 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2db3a 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2db3b 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c  , SQLITE_INSERT,
2db3c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 73   SCHEMA_TABLE(is
2db3d 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 29  Temp), 0, zDb) )
2db3e 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  {..      goto be
2db3f 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
2db40 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
2db41 20 69 73 56 69 65 77 20 29 7b 0d 0a 20 20 20 20   isView ){..    
2db42 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2db43 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0d  DB && isTemp ){.
2db44 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
2db45 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
2db46 4d 50 5f 56 49 45 57 3b 0d 0a 20 20 20 20 20 20  MP_VIEW;..      
2db47 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
2db48 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
2db49 45 41 54 45 5f 56 49 45 57 3b 0d 0a 20 20 20 20  EATE_VIEW;..    
2db4a 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d    }..    }else{.
2db4b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
2db4c 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d  _TEMPDB && isTem
2db4d 70 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f  p ){..        co
2db4e 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
2db4f 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0d 0a  TE_TEMP_TABLE;..
2db50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2db51 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
2db52 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
2db53 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
2db54 7d 0d 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  }..    if( !isVi
2db55 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 33  rtual && sqlite3
2db56 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2db57 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 30  , code, zName, 0
2db58 2c 20 7a 44 62 29 20 29 7b 0d 0a 20 20 20 20 20  , zDb) ){..     
2db59 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
2db5a 65 5f 65 72 72 6f 72 3b 0d 0a 20 20 20 20 7d 0d  e_error;..    }.
2db5b 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
2db5c 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
2db5d 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
2db5e 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69  e does not colli
2db5f 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74  de with an exist
2db60 69 6e 67 0d 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ing..  ** index 
2db61 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e  or table name in
2db62 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
2db63 73 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72  se.  Issue an er
2db64 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0d 0a  ror message if..
2db65 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68    ** it does. Th
2db66 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69  e exception is i
2db67 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
2db68 62 65 69 6e 67 20 70 61 72 73 65 64 20 77 61 73  being parsed was
2db69 20 70 61 73 73 65 64 0d 0a 20 20 2a 2a 20 74 6f   passed..  ** to
2db6a 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c   an sqlite3_decl
2db6b 61 72 65 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e  are_vtab() call.
2db6c 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6f 6e   In that case on
2db6d 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ly the column na
2db6e 6d 65 73 0d 0a 20 20 2a 2a 20 61 6e 64 20 74 79  mes..  ** and ty
2db6f 70 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64  pes will be used
2db70 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f  , so there is no
2db71 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66 6f   need to test fo
2db72 72 20 6e 61 6d 65 73 70 61 63 65 0d 0a 20 20 2a  r namespace..  *
2db73 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0d 0a 20  * collisions... 
2db74 20 2a 2f 0d 0a 20 20 69 66 28 20 21 49 4e 5f 44   */..  if( !IN_D
2db75 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0d 0a  ECLARE_VTAB ){..
2db76 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20      char *zDb = 
2db77 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2db78 6d 65 3b 0d 0a 20 20 20 20 69 66 28 20 53 51 4c  me;..    if( SQL
2db79 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
2db7a 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2db7b 29 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  ) ){..      goto
2db7c 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
2db7d 6f 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  or;..    }..    
2db7e 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  pTable = sqlite3
2db7f 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e  FindTable(db, zN
2db80 61 6d 65 2c 20 7a 44 62 29 3b 0d 0a 20 20 20 20  ame, zDb);..    
2db81 69 66 28 20 70 54 61 62 6c 65 20 29 7b 0d 0a 20  if( pTable ){.. 
2db82 20 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20       if( !noErr 
2db83 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
2db84 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2db85 73 65 2c 20 22 74 61 62 6c 65 20 25 54 20 61 6c  se, "table %T al
2db86 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70  ready exists", p
2db87 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 7d 65  Name);..      }e
2db88 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 61 73  lse{..        as
2db89 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e  sert( !db->init.
2db8a 62 75 73 79 20 29 3b 0d 0a 20 20 20 20 20 20 20  busy );..       
2db8b 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
2db8c 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
2db8d 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 7d 0d   iDb);..      }.
2db8e 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69  .      goto begi
2db8f 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0d 0a  n_table_error;..
2db90 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73      }..    if( s
2db91 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2db92 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 21  db, zName, zDb)!
2db93 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 ){..      sql
2db94 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2db95 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20 61  rse, "there is a
2db96 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20  lready an index 
2db97 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65  named %s", zName
2db98 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  );..      goto b
2db99 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
2db9a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
2db9b 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69  .  pTable = sqli
2db9c 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2db9d 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  db, sizeof(Table
2db9e 29 29 3b 0d 0a 20 20 69 66 28 20 70 54 61 62 6c  ));..  if( pTabl
2db9f 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 64 62 2d  e==0 ){..    db-
2dba0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2dba1 31 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  1;..    pParse->
2dba2 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2dba3 4d 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  M;..    pParse->
2dba4 6e 45 72 72 2b 2b 3b 0d 0a 20 20 20 20 67 6f 74  nErr++;..    got
2dba5 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
2dba6 72 6f 72 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54 61  ror;..  }..  pTa
2dba7 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61  ble->zName = zNa
2dba8 6d 65 3b 0d 0a 20 20 70 54 61 62 6c 65 2d 3e 69  me;..  pTable->i
2dba9 50 4b 65 79 20 3d 20 2d 31 3b 0d 0a 20 20 70 54  PKey = -1;..  pT
2dbaa 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20  able->pSchema = 
2dbab 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2dbac 68 65 6d 61 3b 0d 0a 20 20 70 54 61 62 6c 65 2d  hema;..  pTable-
2dbad 3e 6e 52 65 66 20 3d 20 31 3b 0d 0a 20 20 70 54  >nRef = 1;..  pT
2dbae 61 62 6c 65 2d 3e 6e 52 6f 77 45 73 74 20 3d 20  able->nRowEst = 
2dbaf 31 30 30 30 30 30 30 3b 0d 0a 20 20 61 73 73 65  1000000;..  asse
2dbb0 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  rt( pParse->pNew
2dbb1 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20 20 70  Table==0 );..  p
2dbb2 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2dbb3 20 3d 20 70 54 61 62 6c 65 3b 0d 0a 0d 0a 20 20   = pTable;....  
2dbb4 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
2dbb5 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73  e magic sqlite_s
2dbb6 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73  equence table us
2dbb7 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d  ed by autoincrem
2dbb8 65 6e 74 2c 0d 0a 20 20 2a 2a 20 74 68 65 6e 20  ent,..  ** then 
2dbb9 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72  record a pointer
2dbba 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69   to this table i
2dbbb 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
2dbbc 61 73 65 20 73 74 72 75 63 74 75 72 65 0d 0a 20  ase structure.. 
2dbbd 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45   ** so that INSE
2dbbe 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20  RT can find the 
2dbbf 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0d 0a 20  table easily... 
2dbc0 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2dbc1 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
2dbc2 52 45 4d 45 4e 54 0d 0a 20 20 69 66 28 20 21 70  REMENT..  if( !p
2dbc3 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26  Parse->nested &&
2dbc4 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22   strcmp(zName, "
2dbc5 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22  sqlite_sequence"
2dbc6 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73 73  )==0 ){..    ass
2dbc7 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
2dbc8 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
2dbc9 69 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20 20 20  iDb, 0) );..    
2dbca 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d  pTable->pSchema-
2dbcb 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61 62 6c  >pSeqTab = pTabl
2dbcc 65 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  e;..  }..#endif.
2dbcd 0a 0d 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  ...  /* Begin ge
2dbce 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
2dbcf 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 73 65  e that will inse
2dbd0 72 74 20 74 68 65 20 74 61 62 6c 65 20 72 65 63  rt the table rec
2dbd1 6f 72 64 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20 74  ord into..  ** t
2dbd2 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
2dbd3 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20 69 6e   table.  Note in
2dbd4 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 61 74   particular that
2dbd5 20 77 65 20 6d 75 73 74 20 67 6f 20 61 68 65 61   we must go ahea
2dbd6 64 0d 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f  d..  ** and allo
2dbd7 63 61 74 65 20 74 68 65 20 72 65 63 6f 72 64 20  cate the record 
2dbd8 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
2dbd9 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e 20  able entry now. 
2dbda 20 42 65 66 6f 72 65 20 61 6e 79 0d 0a 20 20 2a   Before any..  *
2dbdb 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  * PRIMARY KEY or
2dbdc 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 64 73   UNIQUE keywords
2dbdd 20 61 72 65 20 70 61 72 73 65 64 2e 20 20 54 68   are parsed.  Th
2dbde 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77 69 6c  ose keywords wil
2dbdf 6c 20 63 61 75 73 65 0d 0a 20 20 2a 2a 20 69 6e  l cause..  ** in
2dbe0 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65 61  dices to be crea
2dbe1 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62 6c  ted and the tabl
2dbe2 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63 6f  e record must co
2dbe3 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0d 0a  me before the ..
2dbe4 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48    ** indices.  H
2dbe5 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64  ence, the record
2dbe6 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2dbe7 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c  table must be al
2dbe8 6c 6f 63 61 74 65 64 0d 0a 20 20 2a 2a 20 6e 6f  located..  ** no
2dbe9 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  w...  */..  if( 
2dbea 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
2dbeb 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65  & (v = sqlite3Ge
2dbec 74 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d  tVdbe(pParse))!=
2dbed 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6a 31  0 ){..    int j1
2dbee 3b 0d 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46  ;..    int fileF
2dbef 6f 72 6d 61 74 3b 0d 0a 20 20 20 20 69 6e 74 20  ormat;..    int 
2dbf0 72 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33  reg1, reg2, reg3
2dbf1 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ;..    sqlite3Be
2dbf2 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2dbf3 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
2dbf4 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  );....#ifndef SQ
2dbf5 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2dbf6 4c 54 41 42 4c 45 0d 0a 20 20 20 20 69 66 28 20  LTABLE..    if( 
2dbf7 69 73 56 69 72 74 75 61 6c 20 29 7b 0d 0a 20 20  isVirtual ){..  
2dbf8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2dbf9 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 67  ddOp0(v, OP_VBeg
2dbfa 69 6e 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  in);..    }..#en
2dbfb 64 69 66 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66  dif....    /* If
2dbfc 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
2dbfd 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e   and encoding in
2dbfe 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2dbff 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c  ve not been set,
2dc00 20 0d 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68   ..    ** set th
2dc01 65 6d 20 6e 6f 77 2e 0d 0a 20 20 20 20 2a 2f 0d  em now...    */.
2dc02 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72  .    reg1 = pPar
2dc03 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b  se->regRowid = +
2dc04 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
2dc05 20 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73      reg2 = pPars
2dc06 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70  e->regRoot = ++p
2dc07 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20  Parse->nMem;..  
2dc08 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73    reg3 = ++pPars
2dc09 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 73 71  e->nMem;..    sq
2dc0a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2dc0b 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  v, OP_ReadCookie
2dc0c 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 42 54 52  , iDb, reg3, BTR
2dc0d 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b  EE_FILE_FORMAT);
2dc0e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2dc0f 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
2dc10 62 29 3b 0d 0a 20 20 20 20 6a 31 20 3d 20 73 71  b);..    j1 = sq
2dc11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2dc12 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b  v, OP_If, reg3);
2dc13 0d 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74  ..    fileFormat
2dc14 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
2dc15 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
2dc16 65 46 6d 74 29 21 3d 30 20 3f 0d 0a 20 20 20 20  eFmt)!=0 ?..    
2dc17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
2dc18 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c  : SQLITE_MAX_FIL
2dc19 45 5f 46 4f 52 4d 41 54 3b 0d 0a 20 20 20 20 73  E_FORMAT;..    s
2dc1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dc1b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2dc1c 66 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65 67 33  fileFormat, reg3
2dc1d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2dc1e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2dc1f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
2dc20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41  BTREE_FILE_FORMA
2dc21 54 2c 20 72 65 67 33 29 3b 0d 0a 20 20 20 20 73  T, reg3);..    s
2dc22 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dc23 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2dc24 45 4e 43 28 64 62 29 2c 20 72 65 67 33 29 3b 0d  ENC(db), reg3);.
2dc25 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2dc26 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74  AddOp3(v, OP_Set
2dc27 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52  Cookie, iDb, BTR
2dc28 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47  EE_TEXT_ENCODING
2dc29 2c 20 72 65 67 33 29 3b 0d 0a 20 20 20 20 73 71  , reg3);..    sq
2dc2a 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2dc2b 65 28 76 2c 20 6a 31 29 3b 0d 0a 0d 0a 20 20 20  e(v, j1);....   
2dc2c 20 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72   /* This just cr
2dc2d 65 61 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f  eates a place-ho
2dc2e 6c 64 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74  lder record in t
2dc2f 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2dc30 20 74 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2a 20   table...    ** 
2dc31 54 68 65 20 72 65 63 6f 72 64 20 63 72 65 61 74  The record creat
2dc32 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  ed does not cont
2dc33 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 74  ain anything yet
2dc34 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 72 65  .  It will be re
2dc35 70 6c 61 63 65 64 0d 0a 20 20 20 20 2a 2a 20 62  placed..    ** b
2dc36 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 72 79  y the real entry
2dc37 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 61 74   in code generat
2dc38 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 6e 64  ed at sqlite3End
2dc39 54 61 62 6c 65 28 29 2e 0d 0a 20 20 20 20 2a 2a  Table()...    **
2dc3a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77  ..    ** The row
2dc3b 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65  id for the new e
2dc3c 6e 74 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20  ntry is left in 
2dc3d 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d  register pParse-
2dc3e 3e 72 65 67 52 6f 77 69 64 2e 0d 0a 20 20 20 20  >regRowid...    
2dc3f 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  ** The root page
2dc40 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
2dc41 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65 66 74  ew table is left
2dc42 20 69 6e 20 72 65 67 20 70 50 61 72 73 65 2d 3e   in reg pParse->
2dc43 72 65 67 52 6f 6f 74 2e 0d 0a 20 20 20 20 2a 2a  regRoot...    **
2dc44 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20 72   The rowid and r
2dc45 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
2dc46 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64 65  values are neede
2dc47 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74 68  d by the code th
2dc48 61 74 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  at..    ** sqlit
2dc49 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20  e3EndTable will 
2dc4a 67 65 6e 65 72 61 74 65 2e 0d 0a 20 20 20 20 2a  generate...    *
2dc4b 2f 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2dc4c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2dc4d 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
2dc4e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2dc4f 4c 54 41 42 4c 45 29 0d 0a 20 20 20 20 69 66 28  LTABLE)..    if(
2dc50 20 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 72   isView || isVir
2dc51 74 75 61 6c 20 29 7b 0d 0a 20 20 20 20 20 20 73  tual ){..      s
2dc52 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dc53 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2dc54 30 2c 20 72 65 67 32 29 3b 0d 0a 20 20 20 20 7d  0, reg2);..    }
2dc55 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  else..#endif..  
2dc56 20 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74    {..      sqlit
2dc57 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2dc58 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20  OP_CreateTable, 
2dc59 69 44 62 2c 20 72 65 67 32 29 3b 0d 0a 20 20 20  iDb, reg2);..   
2dc5a 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   }..    sqlite3O
2dc5b 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70  penMasterTable(p
2dc5c 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a 20 20  Parse, iDb);..  
2dc5d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2dc5e 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
2dc5f 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0d 0a 20  id, 0, reg1);.. 
2dc60 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2dc61 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2dc62 20 30 2c 20 72 65 67 33 29 3b 0d 0a 20 20 20 20   0, reg3);..    
2dc63 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2dc64 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
2dc65 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0d  0, reg3, reg1);.
2dc66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2dc67 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
2dc68 41 47 5f 41 50 50 45 4e 44 29 3b 0d 0a 20 20 20  AG_APPEND);..   
2dc69 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2dc6a 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b  p0(v, OP_Close);
2dc6b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4e 6f  ..  }....  /* No
2dc6c 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29  rmal (non-error)
2dc6d 20 72 65 74 75 72 6e 2e 20 2a 2f 0d 0a 20 20 72   return. */..  r
2dc6e 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  eturn;....  /* I
2dc6f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2dc70 73 2c 20 77 65 20 6a 75 6d 70 20 68 65 72 65 20  s, we jump here 
2dc71 2a 2f 0d 0a 62 65 67 69 6e 5f 74 61 62 6c 65 5f  */..begin_table_
2dc72 65 72 72 6f 72 3a 0d 0a 20 20 73 71 6c 69 74 65  error:..  sqlite
2dc73 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
2dc74 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d 0a  e);..  return;..
2dc75 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
2dc76 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
2dc77 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74  o compare two st
2dc78 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d  rings in a case-
2dc79 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e  insensitive mann
2dc7a 65 72 2e 0d 0a 2a 2a 20 49 74 20 69 73 20 73 6c  er...** It is sl
2dc7b 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68  ightly faster th
2dc7c 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  an calling sqlit
2dc7d 65 33 53 74 72 49 43 6d 70 28 29 20 64 69 72 65  e3StrICmp() dire
2dc7e 63 74 6c 79 2c 20 62 75 74 0d 0a 2a 2a 20 70 72  ctly, but..** pr
2dc7f 6f 64 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f  oduces larger co
2dc80 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 41 52 4e  de...**..** WARN
2dc81 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72 6f 20  ING: This macro 
2dc82 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c  is not compatibl
2dc83 65 20 77 69 74 68 20 74 68 65 20 73 74 72 63 6d  e with the strcm
2dc84 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74 0d 0a  p() family. It..
2dc85 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65 20  ** returns true 
2dc86 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 69 6e  if the two strin
2dc87 67 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f 74  gs are equal, ot
2dc88 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0d 0a  herwise false...
2dc89 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 53 54 52 49  */..#define STRI
2dc8a 43 4d 50 28 78 2c 20 79 29 20 28 5c 0d 0a 73 71  CMP(x, y) (\..sq
2dc8b 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
2dc8c 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61  r[*(unsigned cha
2dc8d 72 20 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0d 0a  r *)(x)]==   \..
2dc8e 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
2dc8f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63  wer[*(unsigned c
2dc90 68 61 72 20 2a 29 28 79 29 5d 20 20 20 20 20 5c  har *)(y)]     \
2dc91 0d 0a 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  ..&& sqlite3StrI
2dc92 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b 31 29  Cmp((x)+1,(y)+1)
2dc93 3d 3d 30 20 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ==0 )..../*..** 
2dc94 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e  Add a new column
2dc95 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75   to the table cu
2dc96 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f  rrently being co
2dc97 6e 73 74 72 75 63 74 65 64 2e 0d 0a 2a 2a 0d 0a  nstructed...**..
2dc98 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2dc99 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2dc9a 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63   once for each c
2dc9b 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
2dc9c 6e 0d 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54  n..** in a CREAT
2dc9d 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
2dc9e 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  t.  sqlite3Start
2dc9f 54 61 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c  Table() gets cal
2dca0 6c 65 64 0d 0a 2a 2a 20 66 69 72 73 74 20 74 6f  led..** first to
2dca1 20 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e   get things goin
2dca2 67 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f  g.  Then this ro
2dca3 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2dca4 66 6f 72 20 65 61 63 68 0d 0a 2a 2a 20 63 6f 6c  for each..** col
2dca5 75 6d 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  umn...*/..SQLITE
2dca6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2dca7 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50  lite3AddColumn(P
2dca8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
2dca9 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0d 0a 20 20  ken *pName){..  
2dcaa 54 61 62 6c 65 20 2a 70 3b 0d 0a 20 20 69 6e 74  Table *p;..  int
2dcab 20 69 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d   i;..  char *z;.
2dcac 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
2dcad 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
2dcae 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20  = pParse->db;.. 
2dcaf 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65   if( (p = pParse
2dcb0 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
2dcb1 29 20 72 65 74 75 72 6e 3b 0d 0a 23 69 66 20 53  ) return;..#if S
2dcb2 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
2dcb3 0d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b  ..  if( p->nCol+
2dcb4 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
2dcb5 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
2dcb6 5d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ] ){..    sqlite
2dcb7 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2dcb8 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75  , "too many colu
2dcb9 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a  mns on %s", p->z
2dcba 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 72 65 74 75  Name);..    retu
2dcbb 72 6e 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  rn;..  }..#endif
2dcbc 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e  ..  z = sqlite3N
2dcbd 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
2dcbe 20 70 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28 20   pName);..  if( 
2dcbf 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  z==0 ) return;..
2dcc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2dcc1 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  nCol; i++){..   
2dcc2 20 69 66 28 30 20 3d 3d 20 73 71 6c 69 74 65 33   if(0 == sqlite3
2dcc3 53 74 72 49 43 6d 70 28 7a 2c 20 70 2d 3e 61 43  StrICmp(z, p->aC
2dcc4 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0d  ol[i].zName) ){.
2dcc5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2dcc6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2dcc7 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e  duplicate column
2dcc8 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0d   name: %s", z);.
2dcc9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2dcca 46 72 65 65 28 64 62 2c 20 7a 29 3b 0d 0a 20 20  Free(db, z);..  
2dccb 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20      return;..   
2dccc 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 28   }..  }..  if( (
2dccd 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d 3d  p->nCol & 0x7)==
2dcce 30 20 29 7b 0d 0a 20 20 20 20 43 6f 6c 75 6d 6e  0 ){..    Column
2dccf 20 2a 61 4e 65 77 3b 0d 0a 20 20 20 20 61 4e 65   *aNew;..    aNe
2dcd0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
2dcd1 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f 6c 2c  lloc(db,p->aCol,
2dcd2 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65  (p->nCol+8)*size
2dcd3 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b  of(p->aCol[0]));
2dcd4 0d 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d  ..    if( aNew==
2dcd5 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  0 ){..      sqli
2dcd6 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
2dcd7 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ;..      return;
2dcd8 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d 3e  ..    }..    p->
2dcd9 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0d 0a 20 20  aCol = aNew;..  
2dcda 7d 0d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e  }..  pCol = &p->
2dcdb 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0d 0a  aCol[p->nCol];..
2dcdc 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30    memset(pCol, 0
2dcdd 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c  , sizeof(p->aCol
2dcde 5b 30 5d 29 29 3b 0d 0a 20 20 70 43 6f 6c 2d 3e  [0]));..  pCol->
2dcdf 7a 4e 61 6d 65 20 3d 20 7a 3b 0d 0a 20 0d 0a 20  zName = z;.. .. 
2dce0 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2dce1 6e 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65  no type specifie
2dce2 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20  d, columns have 
2dce3 74 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69  the default affi
2dce4 6e 69 74 79 0d 0a 20 20 2a 2a 20 27 4e 4f 4e 45  nity..  ** 'NONE
2dce5 27 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  '. If there is a
2dce6 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64 2c   type specified,
2dce7 20 74 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64   then sqlite3Add
2dce8 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c  ColumnType() wil
2dce9 6c 0d 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65  l..  ** be calle
2dcea 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20 70 43  d next to set pC
2dceb 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63 6f 72  ol->affinity cor
2dcec 72 65 63 74 6c 79 2e 0d 0a 20 20 2a 2f 0d 0a 20  rectly...  */.. 
2dced 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
2dcee 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
2dcef 45 3b 0d 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b  E;..  p->nCol++;
2dcf0 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
2dcf1 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2dcf2 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
2dcf3 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20  er while in the 
2dcf4 6d 69 64 64 6c 65 20 6f 66 0d 0a 2a 2a 20 70 61  middle of..** pa
2dcf5 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
2dcf6 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
2dcf7 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f   A "NOT NULL" co
2dcf8 6e 73 74 72 61 69 6e 74 20 68 61 73 0d 0a 2a 2a  nstraint has..**
2dcf9 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20   been seen on a 
2dcfa 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f  column.  This ro
2dcfb 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e  utine sets the n
2dcfc 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0d 0a  otNull flag on..
2dcfd 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75  ** the column cu
2dcfe 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
2dcff 6e 73 74 72 75 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d  nstruction...*/.
2dd00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2dd01 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 4e  void sqlite3AddN
2dd02 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50  otNull(Parse *pP
2dd03 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f  arse, int onErro
2dd04 72 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 3b  r){..  Table *p;
2dd05 0d 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  ..  p = pParse->
2dd06 70 4e 65 77 54 61 62 6c 65 3b 0d 0a 20 20 69 66  pNewTable;..  if
2dd07 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28  ( p==0 || NEVER(
2dd08 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74  p->nCol<1) ) ret
2dd09 75 72 6e 3b 0d 0a 20 20 70 2d 3e 61 43 6f 6c 5b  urn;..  p->aCol[
2dd0a 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75  p->nCol-1].notNu
2dd0b 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  ll = (u8)onError
2dd0c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53  ;..}..../*..** S
2dd0d 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74  can the column t
2dd0e 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28  ype name zType (
2dd0f 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e  length nType) an
2dd10 64 20 72 65 74 75 72 6e 20 74 68 65 0d 0a 2a 2a  d return the..**
2dd11 20 61 73 73 6f 63 69 61 74 65 64 20 61 66 66 69   associated affi
2dd12 6e 69 74 79 20 74 79 70 65 2e 0d 0a 2a 2a 0d 0a  nity type...**..
2dd13 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2dd14 64 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65  does a case-inde
2dd15 70 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f  pendent search o
2dd16 66 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20  f zType for the 
2dd17 0d 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20  ..** substrings 
2dd18 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
2dd19 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f   table. If one o
2dd1a 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73  f the substrings
2dd1b 20 69 73 0d 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74   is..** found, t
2dd1c 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2dd1d 20 61 66 66 69 6e 69 74 79 20 69 73 20 72 65 74   affinity is ret
2dd1e 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 20  urned. If zType 
2dd1f 63 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 6d 6f 72  contains..** mor
2dd20 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68  e than one of th
2dd21 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20 65 6e  e substrings, en
2dd22 74 72 69 65 73 20 74 6f 77 61 72 64 20 74 68 65  tries toward the
2dd23 20 74 6f 70 20 6f 66 20 0d 0a 2a 2a 20 74 68 65   top of ..** the
2dd24 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69 6f   table take prio
2dd25 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c  rity. For exampl
2dd26 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20 27  e, if zType is '
2dd27 42 4c 4f 42 49 4e 54 27 2c 20 0d 0a 2a 2a 20 53  BLOBINT', ..** S
2dd28 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
2dd29 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  R is returned...
2dd2a 2a 2a 0d 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67  **..** Substring
2dd2b 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0d       | Affinity.
2dd2c 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
2dd2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2dd2e 2d 2d 2d 2d 0d 0a 2a 2a 20 27 49 4e 54 27 20 20  ----..** 'INT'  
2dd2f 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
2dd30 41 46 46 5f 49 4e 54 45 47 45 52 0d 0a 2a 2a 20  AFF_INTEGER..** 
2dd31 27 43 48 41 52 27 20 20 20 20 20 20 20 20 7c 20  'CHAR'        | 
2dd32 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0d  SQLITE_AFF_TEXT.
2dd33 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20  .** 'CLOB'      
2dd34 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54    | SQLITE_AFF_T
2dd35 45 58 54 0d 0a 2a 2a 20 27 54 45 58 54 27 20 20  EXT..** 'TEXT'  
2dd36 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
2dd37 46 46 5f 54 45 58 54 0d 0a 2a 2a 20 27 42 4c 4f  FF_TEXT..** 'BLO
2dd38 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  B'        | SQLI
2dd39 54 45 5f 41 46 46 5f 4e 4f 4e 45 0d 0a 2a 2a 20  TE_AFF_NONE..** 
2dd3a 27 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20  'REAL'        | 
2dd3b 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0d  SQLITE_AFF_REAL.
2dd3c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20  .** 'FLOA'      
2dd3d 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52    | SQLITE_AFF_R
2dd3e 45 41 4c 0d 0a 2a 2a 20 27 44 4f 55 42 27 20 20  EAL..** 'DOUB'  
2dd3f 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
2dd40 46 46 5f 52 45 41 4c 0d 0a 2a 2a 0d 0a 2a 2a 20  FF_REAL..**..** 
2dd41 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73  If none of the s
2dd42 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  ubstrings in the
2dd43 20 61 62 6f 76 65 20 74 61 62 6c 65 20 61 72 65   above table are
2dd44 20 66 6f 75 6e 64 2c 0d 0a 2a 2a 20 53 51 4c 49   found,..** SQLI
2dd45 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 69  TE_AFF_NUMERIC i
2dd46 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d  s returned...*/.
2dd47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2dd48 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69  char sqlite3Affi
2dd49 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63  nityType(const c
2dd4a 68 61 72 20 2a 7a 49 6e 29 7b 0d 0a 20 20 75 33  har *zIn){..  u3
2dd4b 32 20 68 20 3d 20 30 3b 0d 0a 20 20 63 68 61 72  2 h = 0;..  char
2dd4c 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
2dd4d 46 5f 4e 55 4d 45 52 49 43 3b 0d 0a 0d 0a 20 20  F_NUMERIC;....  
2dd4e 69 66 28 20 7a 49 6e 20 29 20 77 68 69 6c 65 28  if( zIn ) while(
2dd4f 20 7a 49 6e 5b 30 5d 20 29 7b 0d 0a 20 20 20 20   zIn[0] ){..    
2dd50 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 6f 61 73  h = (h<<8) + oas
2dd51 55 70 70 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73  Upper2Lower[(uns
2dd52 69 67 6e 65 64 20 73 68 6f 72 74 29 73 71 6c 69  igned short)sqli
2dd53 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
2dd54 20 26 7a 49 6e 29 5d 3b 0d 0a 20 20 20 20 69 66   &zIn)];..    if
2dd55 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b  ( h==(('c'<<24)+
2dd56 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c  ('h'<<16)+('a'<<
2dd57 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20 20  8)+'r') ){      
2dd58 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20 2a         /* CHAR *
2dd59 2f 0d 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  /..      aff = S
2dd5a 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 20  QLITE_AFF_TEXT; 
2dd5b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
2dd5c 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27  h==(('c'<<24)+('
2dd5d 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29  l'<<16)+('o'<<8)
2dd5e 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f  +'b') ){       /
2dd5f 2a 20 43 4c 4f 42 20 2a 2f 0d 0a 20 20 20 20 20  * CLOB */..     
2dd60 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
2dd61 46 5f 54 45 58 54 3b 0d 0a 20 20 20 20 7d 65 6c  F_TEXT;..    }el
2dd62 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c  se if( h==(('t'<
2dd63 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28  <24)+('e'<<16)+(
2dd64 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20  'x'<<8)+'t') ){ 
2dd65 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f        /* TEXT */
2dd66 0d 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  ..      aff = SQ
2dd67 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0d 0a  LITE_AFF_TEXT;..
2dd68 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
2dd69 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27  =(('b'<<24)+('l'
2dd6a 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27  <<16)+('o'<<8)+'
2dd6b 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b')          /* 
2dd6c 42 4c 4f 42 20 2a 2f 0d 0a 20 20 20 20 20 20 20  BLOB */..       
2dd6d 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 45   && (aff==SQLITE
2dd6e 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20  _AFF_NUMERIC || 
2dd6f 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
2dd70 52 45 41 4c 29 20 29 7b 0d 0a 20 20 20 20 20 20  REAL) ){..      
2dd71 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
2dd72 5f 4e 4f 4e 45 3b 0d 0a 23 69 66 6e 64 65 66 20  _NONE;..#ifndef 
2dd73 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2dd74 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 20 20  TING_POINT..    
2dd75 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
2dd76 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36  r'<<24)+('e'<<16
2dd77 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20  )+('a'<<8)+'l') 
2dd78 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c           /* REAL
2dd79 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 26 26 20   */..        && 
2dd7a 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
2dd7b 4e 55 4d 45 52 49 43 20 29 7b 0d 0a 20 20 20 20  NUMERIC ){..    
2dd7c 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
2dd7d 46 46 5f 52 45 41 4c 3b 0d 0a 20 20 20 20 7d 65  FF_REAL;..    }e
2dd7e 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27  lse if( h==(('f'
2dd7f 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b  <<24)+('l'<<16)+
2dd80 28 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20  ('o'<<8)+'a')   
2dd81 20 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a         /* FLOA *
2dd82 2f 0d 0a 20 20 20 20 20 20 20 20 26 26 20 61 66  /..        && af
2dd83 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  f==SQLITE_AFF_NU
2dd84 4d 45 52 49 43 20 29 7b 0d 0a 20 20 20 20 20 20  MERIC ){..      
2dd85 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
2dd86 5f 52 45 41 4c 3b 0d 0a 20 20 20 20 7d 65 6c 73  _REAL;..    }els
2dd87 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27 3c 3c  e if( h==(('d'<<
2dd88 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27  24)+('o'<<16)+('
2dd89 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20  u'<<8)+'b')     
2dd8a 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f 0d       /* DOUB */.
2dd8b 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
2dd8c 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
2dd8d 52 49 43 20 29 7b 0d 0a 20 20 20 20 20 20 61 66  RIC ){..      af
2dd8e 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  f = SQLITE_AFF_R
2dd8f 45 41 4c 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  EAL;..#endif..  
2dd90 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30    }else if( (h&0
2dd91 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69  x00FFFFFF)==(('i
2dd92 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b  '<<16)+('n'<<8)+
2dd93 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e  't') ){    /* IN
2dd94 54 20 2a 2f 0d 0a 20 20 20 20 20 20 61 66 66 20  T */..      aff 
2dd95 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  = SQLITE_AFF_INT
2dd96 45 47 45 52 3b 0d 0a 20 20 20 20 20 20 62 72 65  EGER;..      bre
2dd97 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ak;..    }..  }.
2dd98 0a 0d 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b  ...  return aff;
2dd99 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
2dd9a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2dd9b 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
2dd9c 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20  er while in the 
2dd9d 6d 69 64 64 6c 65 20 6f 66 0d 0a 2a 2a 20 70 61  middle of..** pa
2dd9e 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
2dd9f 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
2dda0 20 54 68 65 20 70 46 69 72 73 74 20 74 6f 6b 65   The pFirst toke
2dda1 6e 20 69 73 20 74 68 65 20 66 69 72 73 74 0d 0a  n is the first..
2dda2 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  ** token in the 
2dda3 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65  sequence of toke
2dda4 6e 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ns that describe
2dda5 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65   the type of the
2dda6 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72  ..** column curr
2dda7 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
2dda8 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73  truction.   pLas
2dda9 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74 6f  t is the last to
2ddaa 6b 65 6e 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ken..** in the s
2ddab 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68  equence.  Use th
2ddac 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  is information t
2ddad 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74  o construct a st
2ddae 72 69 6e 67 0d 0a 2a 2a 20 74 68 61 74 20 63 6f  ring..** that co
2ddaf 6e 74 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e  ntains the typen
2ddb0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ame of the colum
2ddb1 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74  n and store that
2ddb2 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 69 6e 20 7a   string..** in z
2ddb3 54 79 70 65 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c 49  Type...*/ ..SQLI
2ddb4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2ddb5 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
2ddb6 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72  Type(Parse *pPar
2ddb7 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65  se, Token *pType
2ddb8 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0d  ){..  Table *p;.
2ddb9 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
2ddba 0d 0a 0d 0a 20 20 70 20 3d 20 70 50 61 72 73 65  ....  p = pParse
2ddbb 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0d 0a 20 20  ->pNewTable;..  
2ddbc 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45  if( p==0 || NEVE
2ddbd 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72  R(p->nCol<1) ) r
2ddbe 65 74 75 72 6e 3b 0d 0a 20 20 70 43 6f 6c 20 3d  eturn;..  pCol =
2ddbf 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f   &p->aCol[p->nCo
2ddc0 6c 2d 31 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28  l-1];..  assert(
2ddc1 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3d 3d 30 20   pCol->zType==0 
2ddc2 29 3b 0d 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70  );..  pCol->zTyp
2ddc3 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
2ddc4 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
2ddc5 3e 64 62 2c 20 70 54 79 70 65 29 3b 0d 0a 20 20  >db, pType);..  
2ddc6 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d  pCol->affinity =
2ddc7 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
2ddc8 54 79 70 65 28 70 43 6f 6c 2d 3e 7a 54 79 70 65  Type(pCol->zType
2ddc9 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
2ddca 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  The expression i
2ddcb 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  s the default va
2ddcc 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  lue for the most
2ddcd 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20   recently added 
2ddce 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 6f 66 20 74 68  column..** of th
2ddcf 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c  e table currentl
2ddd0 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
2ddd1 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 65  tion...**..** De
2ddd2 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70 72  fault value expr
2ddd3 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65 20  essions must be 
2ddd4 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73 65  constant.  Raise
2ddd5 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66   an exception if
2ddd6 20 74 68 69 73 0d 0a 2a 2a 20 69 73 20 6e 6f 74   this..** is not
2ddd7 20 74 68 65 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a   the case...**..
2ddd8 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2ddd9 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
2ddda 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e   parser while in
2dddb 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0d 0a   the middle of..
2dddc 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
2dddd 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2ddde 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ent...*/..SQLITE
2dddf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2dde0 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
2dde1 61 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 72  alue(Parse *pPar
2dde2 73 65 2c 20 45 78 70 72 53 70 61 6e 20 2a 70 53  se, ExprSpan *pS
2dde3 70 61 6e 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a  pan){..  Table *
2dde4 70 3b 0d 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  p;..  Column *pC
2dde5 6f 6c 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  ol;..  sqlite3 *
2dde6 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2dde7 0d 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  ..  p = pParse->
2dde8 70 4e 65 77 54 61 62 6c 65 3b 0d 0a 20 20 69 66  pNewTable;..  if
2dde9 28 20 70 21 3d 30 20 29 7b 0d 0a 20 20 20 20 70  ( p!=0 ){..    p
2ddea 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b  Col = &(p->aCol[
2ddeb 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0d 0a 20 20  p->nCol-1]);..  
2ddec 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78    if( !sqlite3Ex
2dded 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75  prIsConstantOrFu
2ddee 6e 63 74 69 6f 6e 28 70 53 70 61 6e 2d 3e 70 45  nction(pSpan->pE
2ddef 78 70 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 73  xpr) ){..      s
2ddf0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2ddf1 50 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74 20  Parse, "default 
2ddf2 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20  value of column 
2ddf3 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73  [%s] is not cons
2ddf4 74 61 6e 74 22 2c 0d 0a 20 20 20 20 20 20 20 20  tant",..        
2ddf5 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d    pCol->zName);.
2ddf6 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
2ddf7 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20     /* A copy of 
2ddf8 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69 6e  pExpr is used in
2ddf9 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 69  stead of the ori
2ddfa 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 20  ginal, as pExpr 
2ddfb 63 6f 6e 74 61 69 6e 73 0d 0a 20 20 20 20 20 20  contains..      
2ddfc 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70  ** tokens that p
2ddfd 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65  oint to volatile
2ddfe 20 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70   memory. The 'sp
2ddff 61 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65  an' of the expre
2de00 73 73 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  ssion..      ** 
2de01 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20 70  is required by p
2de02 72 61 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f  ragma table_info
2de03 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
2de04 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2de05 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70  lete(db, pCol->p
2de06 44 66 6c 74 29 3b 0d 0a 20 20 20 20 20 20 70 43  Dflt);..      pC
2de07 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69  ol->pDflt = sqli
2de08 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2de09 53 70 61 6e 2d 3e 70 45 78 70 72 2c 20 45 58 50  Span->pExpr, EXP
2de0a 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0d 0a 20  RDUP_REDUCE);.. 
2de0b 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2de0c 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66  ee(db, pCol->zDf
2de0d 6c 74 29 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c  lt);..      pCol
2de0e 2d 3e 7a 44 66 6c 74 20 3d 20 73 71 6c 69 74 65  ->zDflt = sqlite
2de0f 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
2de10 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74  char*)pSpan->zSt
2de11 61 72 74 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  art,..          
2de12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de13 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
2de14 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70  (pSpan->zEnd - p
2de15 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0d  Span->zStart));.
2de16 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73  .    }..  }..  s
2de17 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2de18 28 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70  (db, pSpan->pExp
2de19 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  r);..}..../*..**
2de1a 20 44 65 73 69 67 6e 61 74 65 20 74 68 65 20 50   Designate the P
2de1b 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74  RIMARY KEY for t
2de1c 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74  he table.  pList
2de1d 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61   is a list of na
2de1e 6d 65 73 20 0d 0a 2a 2a 20 6f 66 20 63 6f 6c 75  mes ..** of colu
2de1f 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68  mns that form th
2de20 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
2de21 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c  If pList is NULL
2de22 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 6d  , then the..** m
2de23 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64  ost recently add
2de24 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ed column of the
2de25 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 72   table is the pr
2de26 69 6d 61 72 79 20 6b 65 79 2e 0d 0a 2a 2a 0d 0a  imary key...**..
2de27 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68  ** A table can h
2de28 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  ave at most one 
2de29 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66  primary key.  If
2de2a 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61   the table alrea
2de2b 64 79 20 68 61 73 0d 0a 2a 2a 20 61 20 70 72 69  dy has..** a pri
2de2c 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68  mary key (and th
2de2d 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  is is the second
2de2e 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68   primary key) th
2de2f 65 6e 20 63 72 65 61 74 65 20 61 6e 0d 0a 2a 2a  en create an..**
2de30 20 65 72 72 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   error...**..** 
2de31 49 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  If the PRIMARY K
2de32 45 59 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c  EY is on a singl
2de33 65 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64  e column whose d
2de34 61 74 61 74 79 70 65 20 69 73 20 49 4e 54 45 47  atatype is INTEG
2de35 45 52 2c 0d 0a 2a 2a 20 74 68 65 6e 20 77 65 20  ER,..** then we 
2de36 77 69 6c 6c 20 74 72 79 20 74 6f 20 75 73 65 20  will try to use 
2de37 74 68 61 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74  that column as t
2de38 68 65 20 72 6f 77 69 64 2e 20 20 53 65 74 20 74  he rowid.  Set t
2de39 68 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 0d 0a  he Table.iPKey..
2de3a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ** field of the 
2de3b 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73  table under cons
2de3c 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 74  truction to be t
2de3d 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0d  he index of the.
2de3e 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50 52 49 4d  .** INTEGER PRIM
2de3f 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20  ARY KEY column. 
2de40 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
2de41 73 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65  set to -1 if the
2de42 72 65 20 69 73 0d 0a 2a 2a 20 6e 6f 20 49 4e 54  re is..** no INT
2de43 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
2de44 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
2de45 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e 20 49   key is not an I
2de46 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
2de47 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  EY, then create 
2de48 61 20 75 6e 69 71 75 65 0d 0a 2a 2a 20 69 6e 64  a unique..** ind
2de49 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e 20  ex for the key. 
2de4a 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72 65   No index is cre
2de4b 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45 52  ated for INTEGER
2de4c 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0d 0a   PRIMARY KEYs...
2de4d 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
2de4e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
2de4f 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0d 0a 20  ddPrimaryKey(.. 
2de50 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2de51 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2de52 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78 70 72  ntext */..  Expr
2de53 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a  List *pList,  /*
2de54 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e   List of field n
2de55 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78  ames to be index
2de56 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6e 45  ed */..  int onE
2de57 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68  rror,      /* Wh
2de58 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20  at to do with a 
2de59 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c  uniqueness confl
2de5a 69 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 75  ict */..  int au
2de5b 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54  toInc,      /* T
2de5c 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49  rue if the AUTOI
2de5d 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64  NCREMENT keyword
2de5e 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0d 0a   is present */..
2de5f 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
2de60 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f      /* SQLITE_SO
2de61 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53  _ASC or SQLITE_S
2de62 4f 5f 44 45 53 43 20 2a 2f 0d 0a 29 7b 0d 0a 20  O_DESC */..){.. 
2de63 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
2de64 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2de65 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65  ;..  char *zType
2de66 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69 43 6f   = 0;..  int iCo
2de67 6c 20 3d 20 2d 31 2c 20 69 3b 0d 0a 20 20 69 66  l = -1, i;..  if
2de68 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f  ( pTab==0 || IN_
2de69 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67  DECLARE_VTAB ) g
2de6a 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
2de6b 65 78 69 74 3b 0d 0a 20 20 69 66 28 20 70 54 61  exit;..  if( pTa
2de6c 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2de6d 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 29  _HasPrimaryKey )
2de6e 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  {..    sqlite3Er
2de6f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0d  rorMsg(pParse, .
2de70 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20 5c 22  .      "table \"
2de71 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20 74 68  %s\" has more th
2de72 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b  an one primary k
2de73 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ey", pTab->zName
2de74 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 70 72 69  );..    goto pri
2de75 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0d 0a  mary_key_exit;..
2de76 20 20 7d 0d 0a 20 20 70 54 61 62 2d 3e 74 61 62    }..  pTab->tab
2de77 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50  Flags |= TF_HasP
2de78 72 69 6d 61 72 79 4b 65 79 3b 0d 0a 20 20 69 66  rimaryKey;..  if
2de79 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0d 0a 20  ( pList==0 ){.. 
2de7a 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     iCol = pTab->
2de7b 6e 43 6f 6c 20 2d 20 31 3b 0d 0a 20 20 20 20 70  nCol - 1;..    p
2de7c 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2de7d 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0d 0a  isPrimKey = 1;..
2de7e 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 66 6f    }else{..    fo
2de7f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
2de80 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nExpr; i++){..  
2de81 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
2de82 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
2de83 20 69 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20   iCol++){..     
2de84 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
2de85 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69  rICmp(pList->a[i
2de86 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61  ].zName, pTab->a
2de87 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29  Col[iCol].zName)
2de88 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
2de89 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
2de8a 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
2de8b 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61      if( iCol<pTa
2de8c 62 2d 3e 6e 43 6f 6c 20 29 7b 0d 0a 20 20 20 20  b->nCol ){..    
2de8d 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
2de8e 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d  Col].isPrimKey =
2de8f 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   1;..      }..  
2de90 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 70 4c 69    }..    if( pLi
2de91 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69 43  st->nExpr>1 ) iC
2de92 6f 6c 20 3d 20 2d 31 3b 0d 0a 20 20 7d 0d 0a 20  ol = -1;..  }.. 
2de93 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20   if( iCol>=0 && 
2de94 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  iCol<pTab->nCol 
2de95 29 7b 0d 0a 20 20 20 20 7a 54 79 70 65 20 3d 20  ){..    zType = 
2de96 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
2de97 2e 7a 54 79 70 65 3b 0d 0a 20 20 7d 0d 0a 20 20  .zType;..  }..  
2de98 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c  if( zType && sql
2de99 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70  ite3StrICmp(zTyp
2de9a 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30  e, "INTEGER")==0
2de9b 0d 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72  ..        && sor
2de9c 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53  tOrder==SQLITE_S
2de9d 4f 5f 41 53 43 20 29 7b 0d 0a 20 20 20 20 70 54  O_ASC ){..    pT
2de9e 61 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c  ab->iPKey = iCol
2de9f 3b 0d 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79  ;..    pTab->key
2dea0 43 6f 6e 66 20 3d 20 28 75 38 29 6f 6e 45 72 72  Conf = (u8)onErr
2dea1 6f 72 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  or;..    assert(
2dea2 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61   autoInc==0 || a
2dea3 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0d 0a 20 20  utoInc==1 );..  
2dea4 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
2dea5 20 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46 5f 41   |= autoInc*TF_A
2dea6 75 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0d 0a 20  utoincrement;.. 
2dea7 20 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49   }else if( autoI
2dea8 6e 63 20 29 7b 0d 0a 23 69 66 6e 64 65 66 20 53  nc ){..#ifndef S
2dea9 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
2deaa 4e 43 52 45 4d 45 4e 54 0d 0a 20 20 20 20 73 71  NCREMENT..    sq
2deab 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2deac 61 72 73 65 2c 20 22 41 55 54 4f 49 4e 43 52 45  arse, "AUTOINCRE
2dead 4d 45 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c  MENT is only all
2deae 6f 77 65 64 20 6f 6e 20 61 6e 20 22 0d 0a 20 20  owed on an "..  
2deaf 20 20 20 20 20 22 49 4e 54 45 47 45 52 20 50 52       "INTEGER PR
2deb0 49 4d 41 52 59 20 4b 45 59 22 29 3b 0d 0a 23 65  IMARY KEY");..#e
2deb1 6e 64 69 66 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ndif..  }else{..
2deb2 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0d 0a 20      Index *p;.. 
2deb3 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 43 72     p = sqlite3Cr
2deb4 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65  eateIndex(pParse
2deb5 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74  , 0, 0, 0, pList
2deb6 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c  , onError, 0, 0,
2deb7 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0d   sortOrder, 0);.
2deb8 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0d 0a 20  .    if( p ){.. 
2deb9 20 20 20 20 20 70 2d 3e 61 75 74 6f 49 6e 64 65       p->autoInde
2deba 78 20 3d 20 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20  x = 2;..    }.. 
2debb 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0d 0a 20     pList = 0;.. 
2debc 20 7d 0d 0a 0d 0a 70 72 69 6d 61 72 79 5f 6b 65   }....primary_ke
2debd 79 5f 65 78 69 74 3a 0d 0a 20 20 73 71 6c 69 74  y_exit:..  sqlit
2debe 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2debf 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69  (pParse->db, pLi
2dec0 73 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d  st);..  return;.
2dec1 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64  .}..../*..** Add
2dec2 20 61 20 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e   a new CHECK con
2dec3 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 74  straint to the t
2dec4 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
2dec5 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2dec6 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  n...*/..SQLITE_P
2dec7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2dec8 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
2dec9 72 61 69 6e 74 28 0d 0a 20 20 50 61 72 73 65 20  raint(..  Parse 
2deca 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
2decb 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2decc 2f 0d 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63  /..  Expr *pChec
2decd 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 63 68  kExpr  /* The ch
2dece 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  eck expression *
2decf 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  /..){..  sqlite3
2ded0 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ded1 62 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  b;..#ifndef SQLI
2ded2 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0d 0a 20  TE_OMIT_CHECK.. 
2ded3 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
2ded4 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2ded5 3b 0d 0a 20 20 69 66 28 20 70 54 61 62 20 26 26  ;..  if( pTab &&
2ded6 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41   !IN_DECLARE_VTA
2ded7 42 20 29 7b 0d 0a 20 20 20 20 70 54 61 62 2d 3e  B ){..    pTab->
2ded8 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33  pCheck = sqlite3
2ded9 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62  ExprAnd(db, pTab
2deda 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68 65 63 6b  ->pCheck, pCheck
2dedb 45 78 70 72 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d  Expr);..  }else.
2dedc 0a 23 65 6e 64 69 66 0d 0a 20 20 7b 0d 0a 20 20  .#endif..  {..  
2dedd 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2dede 65 74 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78  ete(db, pCheckEx
2dedf 70 72 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  pr);..  }..}....
2dee0 2f 2a 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  /*..** Set the c
2dee1 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
2dee2 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  n of the most re
2dee3 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61  cently parsed ta
2dee4 62 6c 65 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 74  ble column..** t
2dee5 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69  o the CollSeq gi
2dee6 76 65 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ven...*/..SQLITE
2dee7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2dee8 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
2dee9 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
2deea 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  e, Token *pToken
2deeb 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0d  ){..  Table *p;.
2deec 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 63 68 61  .  int i;..  cha
2deed 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20  r *zColl;       
2deee 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74         /* Dequot
2deef 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61  ed name of colla
2def0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f  tion sequence */
2def1 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ..  sqlite3 *db;
2def2 0d 0a 0d 0a 20 20 69 66 28 20 28 70 20 3d 20 70  ....  if( (p = p
2def3 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2def4 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  )==0 ) return;..
2def5 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b    i = p->nCol-1;
2def6 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
2def7 3e 64 62 3b 0d 0a 20 20 7a 43 6f 6c 6c 20 3d 20  >db;..  zColl = 
2def8 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2def9 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29  oken(db, pToken)
2defa 3b 0d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20  ;..  if( !zColl 
2defb 29 20 72 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 69  ) return;....  i
2defc 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  f( sqlite3Locate
2defd 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2defe 7a 43 6f 6c 6c 29 20 29 7b 0d 0a 20 20 20 20 49  zColl) ){..    I
2deff 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20 20  ndex *pIdx;..   
2df00 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c   p->aCol[i].zCol
2df01 6c 20 3d 20 7a 43 6f 6c 6c 3b 0d 0a 20 20 0d 0a  l = zColl;..  ..
2df02 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
2df03 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64  lumn is declared
2df04 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d   as "<name> PRIM
2df05 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20  ARY KEY COLLATE 
2df06 3c 74 79 70 65 3e 22 2c 0d 0a 20 20 20 20 2a 2a  <type>",..    **
2df07 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d   then an index m
2df08 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65  ay have been cre
2df09 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c  ated on this col
2df0a 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0d 0a  umn before the..
2df0b 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e      ** collation
2df0c 20 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e   type was added.
2df0d 20 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66   Correct this if
2df0e 20 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e   it is the case.
2df0f 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 66 6f  ..    */..    fo
2df10 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78  r(pIdx=p->pIndex
2df11 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2df12 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20  x->pNext){..    
2df13 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
2df14 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0d 0a 20  nColumn==1 );.. 
2df15 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
2df16 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b  iColumn[0]==i ){
2df17 0d 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e  ..        pIdx->
2df18 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61  azColl[0] = p->a
2df19 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0d 0a 20  Col[i].zColl;.. 
2df1a 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
2df1b 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
2df1c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2df1d 43 6f 6c 6c 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  Coll);..  }..}..
2df1e 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
2df1f 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
2df20 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
2df21 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61  uence for databa
2df22 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0d 0a  se native text..
2df23 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e  ** encoding iden
2df24 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74  tified by the st
2df25 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  ring zName, leng
2df26 74 68 20 6e 4e 61 6d 65 2e 0d 0a 2a 2a 0d 0a 2a  th nName...**..*
2df27 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74  * If the request
2df28 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
2df29 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61  uence is not ava
2df2a 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61  ilable, or not a
2df2b 76 61 69 6c 61 62 6c 65 0d 0a 2a 2a 20 69 6e 20  vailable..** in 
2df2c 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74  the database nat
2df2d 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68  ive encoding, th
2df2e 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
2df2f 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 20 74  ory is invoked t
2df30 6f 0d 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 74  o..** request it
2df31 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  . If the collati
2df32 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73 20  on factory does 
2df33 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68 20  not supply such 
2df34 61 20 73 65 71 75 65 6e 63 65 2c 0d 0a 2a 2a 20  a sequence,..** 
2df35 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  and the sequence
2df36 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e   is available in
2df37 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e   another text en
2df38 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61  coding, then tha
2df39 74 20 69 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 65  t is..** returne
2df3a 64 20 69 6e 73 74 65 61 64 2e 0d 0a 2a 2a 0d 0a  d instead...**..
2df3b 2a 2a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e  ** If no version
2df3c 73 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  s of the request
2df3d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65  ed collations se
2df3e 71 75 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c  quence are avail
2df3f 61 62 6c 65 2c 20 6f 72 0d 0a 2a 2a 20 61 6e 6f  able, or..** ano
2df40 74 68 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72  ther error occur
2df41 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
2df42 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
2df43 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e   message written
2df44 20 69 6e 74 6f 0d 0a 2a 2a 20 70 50 61 72 73 65   into..** pParse
2df45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
2df46 6f 75 74 69 6e 65 20 69 73 20 61 20 77 72 61 70  outine is a wrap
2df47 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74  per around sqlit
2df48 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 2e  e3FindCollSeq().
2df49 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a    This routine..
2df4a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  ** invokes the c
2df4b 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
2df4c 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 63 6f   if the named co
2df4d 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62  llation cannot b
2df4e 65 20 66 6f 75 6e 64 0d 0a 2a 2a 20 61 6e 64 20  e found..** and 
2df4f 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
2df50 6f 72 20 6d 65 73 73 61 67 65 2e 0d 0a 2a 2a 0d  or message...**.
2df51 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71  .** See also: sq
2df52 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
2df53 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f  (), sqlite3GetCo
2df54 6c 6c 53 65 71 28 29 0d 0a 2a 2f 0d 0a 53 51 4c  llSeq()..*/..SQL
2df55 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
2df56 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61  Seq *sqlite3Loca
2df57 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  teCollSeq(Parse 
2df58 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
2df59 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0d 0a 20 20  har *zName){..  
2df5a 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2df5b 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 75 38 20  arse->db;..  u8 
2df5c 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0d 0a  enc = ENC(db);..
2df5d 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20    u8 initbusy = 
2df5e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0d 0a  db->init.busy;..
2df5f 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2df60 3b 0d 0a 0d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  ;....  pColl = s
2df61 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
2df62 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65  q(db, enc, zName
2df63 2c 20 69 6e 69 74 62 75 73 79 29 3b 0d 0a 20 20  , initbusy);..  
2df64 69 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26  if( !initbusy &&
2df65 20 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f   (!pColl || !pCo
2df66 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b 0d 0a 20 20  ll->xCmp) ){..  
2df67 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2df68 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20  3GetCollSeq(db, 
2df69 65 6e 63 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d  enc, pColl, zNam
2df6a 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 43  e);..    if( !pC
2df6b 6f 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  oll ){..      sq
2df6c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2df6d 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63  arse, "no such c
2df6e 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2df6f 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d  e: %s", zName);.
2df70 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
2df71 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0d 0a   return pColl;..
2df72 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  }....../*..** Ge
2df73 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2df74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20   will increment 
2df75 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2df76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73  e...**..** The s
2df77 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20  chema cookie is 
2df78 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
2df79 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d  e when the schem
2df7a 61 20 66 6f 72 20 74 68 65 0d 0a 2a 2a 20 64 61  a for the..** da
2df7b 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20  tabase changes. 
2df7c 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68 65   After each sche
2df7d 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63  ma change, the c
2df7e 6f 6f 6b 69 65 20 76 61 6c 75 65 0d 0a 2a 2a 20  ookie value..** 
2df7f 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61  changes.  When a
2df80 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72   process first r
2df81 65 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20  eads the schema 
2df82 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 0d 0a  it records the..
2df83 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72  ** cookie.  Ther
2df84 65 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72  eafter, whenever
2df85 20 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65   it goes to acce
2df86 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ss the database,
2df87 0d 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74  ..** it checks t
2df88 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b  he cookie to mak
2df89 65 20 73 75 72 65 20 74 68 65 20 73 63 68 65 6d  e sure the schem
2df8a 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65  a has not change
2df8b 64 0d 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 77  d..** since it w
2df8c 61 73 20 6c 61 73 74 20 72 65 61 64 2e 0d 0a 2a  as last read...*
2df8d 2a 0d 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20  *..** This plan 
2df8e 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c  is not completel
2df8f 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20  y bullet-proof. 
2df90 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   It is possible 
2df91 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 73 63 68 65  for..** the sche
2df92 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c  ma to change mul
2df93 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20  tiple times and 
2df94 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74  for the cookie t
2df95 6f 20 62 65 0d 0a 2a 2a 20 73 65 74 20 62 61 63  o be..** set bac
2df96 6b 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65  k to prior value
2df97 2e 20 20 42 75 74 20 73 63 68 65 6d 61 20 63 68  .  But schema ch
2df98 61 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65 71  anges are infreq
2df99 75 65 6e 74 0d 0a 2a 2a 20 61 6e 64 20 74 68 65  uent..** and the
2df9a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20   probability of 
2df9b 68 69 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65  hitting the same
2df9c 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73   cookie value is
2df9d 20 6f 6e 6c 79 0d 0a 2a 2a 20 31 20 63 68 61 6e   only..** 1 chan
2df9e 63 65 20 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20  ce in 2^32.  So 
2df9f 77 65 27 72 65 20 73 61 66 65 20 65 6e 6f 75 67  we're safe enoug
2dfa0 68 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  h...*/..SQLITE_P
2dfa1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2dfa2 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
2dfa3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
2dfa4 6e 74 20 69 44 62 29 7b 0d 0a 20 20 69 6e 74 20  nt iDb){..  int 
2dfa5 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2dfa6 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
2dfa7 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2dfa8 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20   pParse->db;..  
2dfa9 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2dfaa 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 61 73 73 65  ->pVdbe;..  asse
2dfab 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
2dfac 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69  aMutexHeld(db, i
2dfad 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20 73 71 6c  Db, 0) );..  sql
2dfae 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2dfaf 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62  , OP_Integer, db
2dfb0 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2dfb1 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
2dfb2 65 2b 31 2c 20 72 31 29 3b 0d 0a 20 20 73 71 6c  e+1, r1);..  sql
2dfb3 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2dfb4 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20  , OP_SetCookie, 
2dfb5 69 44 62 2c 20 42 54 52 45 45 5f 53 43 48 45 4d  iDb, BTREE_SCHEM
2dfb6 41 5f 56 45 52 53 49 4f 4e 2c 20 72 31 29 3b 0d  A_VERSION, r1);.
2dfb7 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2dfb8 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2dfb9 20 72 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a   r1);..}..../*..
2dfba 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e  ** Measure the n
2dfbb 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
2dfbc 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75  ers needed to ou
2dfbd 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0d 0a  tput the given..
2dfbe 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  ** identifier.  
2dfbf 54 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72  The number retur
2dfc0 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79  ned includes any
2dfc1 20 71 75 6f 74 65 73 20 75 73 65 64 0d 0a 2a 2a   quotes used..**
2dfc2 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e   but does not in
2dfc3 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74  clude the null t
2dfc4 65 72 6d 69 6e 61 74 6f 72 2e 0d 0a 2a 2a 0d 0a  erminator...**..
2dfc5 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 20  ** The estimate 
2dfc6 69 73 20 63 6f 6e 73 65 72 76 61 74 69 76 65 2e  is conservative.
2dfc7 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 6c 61    It might be la
2dfc8 72 67 65 72 20 74 68 61 74 20 77 68 61 74 20 69  rger that what i
2dfc9 73 0d 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e 65 65  s..** really nee
2dfca 64 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ded...*/..static
2dfcb 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68   int identLength
2dfcc 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
2dfcd 0d 0a 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 66 6f  ..  int n;..  fo
2dfce 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20  r(n=0; *z; n++, 
2dfcf 7a 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 2a  z++){..    if( *
2dfd0 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d  z=='"' ){ n++; }
2dfd1 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2dfd2 6e 20 2b 20 32 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  n + 2;..}..../*.
2dfd3 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
2dfd4 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
2dfd5 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 75  nter to an outpu
2dfd6 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 65  t buffer. The se
2dfd7 63 6f 6e 64 20 0d 0a 2a 2a 20 70 61 72 61 6d 65  cond ..** parame
2dfd8 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
2dfd9 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74   to an integer t
2dfda 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
2dfdb 20 6f 66 66 73 65 74 20 61 74 0d 0a 2a 2a 20 77   offset at..** w
2dfdc 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e  hich to write in
2dfdd 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  to the output bu
2dfde 66 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  ffer. This funct
2dfdf 69 6f 6e 20 63 6f 70 69 65 73 20 74 68 65 0d 0a  ion copies the..
2dfe0 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65  ** nul-terminate
2dfe1 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64  d string pointed
2dfe2 20 74 6f 20 62 79 20 74 68 65 20 74 68 69 72 64   to by the third
2dfe3 20 70 61 72 61 6d 65 74 65 72 2c 20 7a 53 69 67   parameter, zSig
2dfe4 6e 65 64 49 64 65 6e 74 2c 0d 0a 2a 2a 20 74 6f  nedIdent,..** to
2dfe5 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6f   the specified o
2dfe6 66 66 73 65 74 20 69 6e 20 74 68 65 20 62 75 66  ffset in the buf
2dfe7 66 65 72 20 61 6e 64 20 75 70 64 61 74 65 73 20  fer and updates 
2dfe8 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72 0d 0a  *pIdx to refer..
2dfe9 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ** to the first 
2dfea 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 6c  byte after the l
2dfeb 61 73 74 20 62 79 74 65 20 77 72 69 74 74 65 6e  ast byte written
2dfec 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2dfed 67 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49 66 20 74  g...** ..** If t
2dfee 68 65 20 73 74 72 69 6e 67 20 7a 53 69 67 6e 65  he string zSigne
2dfef 64 49 64 65 6e 74 20 63 6f 6e 73 69 73 74 73 20  dIdent consists 
2dff0 65 6e 74 69 72 65 6c 79 20 6f 66 20 61 6c 70 68  entirely of alph
2dff1 61 2d 6e 75 6d 65 72 69 63 0d 0a 2a 2a 20 63 68  a-numeric..** ch
2dff2 61 72 61 63 74 65 72 73 2c 20 64 6f 65 73 20 6e  aracters, does n
2dff3 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
2dff4 64 69 67 69 74 20 61 6e 64 20 69 73 20 6e 6f 74  digit and is not
2dff5 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2c   an SQL keyword,
2dff6 0d 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20  ..** then it is 
2dff7 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 6f 75  copied to the ou
2dff8 74 70 75 74 20 62 75 66 66 65 72 20 65 78 61 63  tput buffer exac
2dff9 74 6c 79 20 61 73 20 69 74 20 69 73 2e 20 4f 74  tly as it is. Ot
2dffa 68 65 72 77 69 73 65 2c 0d 0a 2a 2a 20 69 74 20  herwise,..** it 
2dffb 69 73 20 71 75 6f 74 65 64 20 75 73 69 6e 67 20  is quoted using 
2dffc 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2e 0d 0a  double-quotes...
2dffd 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2dffe 69 64 65 6e 74 50 75 74 28 63 68 61 72 20 2a 7a  identPut(char *z
2dfff 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20 63 68 61  , int *pIdx, cha
2e000 72 20 2a 7a 53 69 67 6e 65 64 49 64 65 6e 74 29  r *zSignedIdent)
2e001 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  {..  unsigned ch
2e002 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28 75 6e  ar *zIdent = (un
2e003 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 53 69  signed char*)zSi
2e004 67 6e 65 64 49 64 65 6e 74 3b 0d 0a 20 20 69 6e  gnedIdent;..  in
2e005 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f 74  t i, j, needQuot
2e006 65 3b 0d 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b  e;..  i = *pIdx;
2e007 0d 0a 0d 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  ....  for(j=0; z
2e008 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0d  Ident[j]; j++){.
2e009 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
2e00a 33 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b  3Isalnum(zIdent[
2e00b 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d  j]) && zIdent[j]
2e00c 21 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0d 0a  !='_' ) break;..
2e00d 20 20 7d 0d 0a 20 20 6e 65 65 64 51 75 6f 74 65    }..  needQuote
2e00e 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69 67 69   = sqlite3Isdigi
2e00f 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20 7c 7c 20  t(zIdent[0]) || 
2e010 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
2e011 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54  de(zIdent, j)!=T
2e012 4b 5f 49 44 3b 0d 0a 20 20 69 66 28 20 21 6e 65  K_ID;..  if( !ne
2e013 65 64 51 75 6f 74 65 20 29 7b 0d 0a 20 20 20 20  edQuote ){..    
2e014 6e 65 65 64 51 75 6f 74 65 20 3d 20 7a 49 64 65  needQuote = zIde
2e015 6e 74 5b 6a 5d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  nt[j];..  }.... 
2e016 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
2e017 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0d 0a   z[i++] = '"';..
2e018 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e    for(j=0; zIden
2e019 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20  t[j]; j++){..   
2e01a 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74   z[i++] = zIdent
2e01b 5b 6a 5d 3b 0d 0a 20 20 20 20 69 66 28 20 7a 49  [j];..    if( zI
2e01c 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a  dent[j]=='"' ) z
2e01d 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0d 0a 20 20  [i++] = '"';..  
2e01e 7d 0d 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f  }..  if( needQuo
2e01f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22  te ) z[i++] = '"
2e020 27 3b 0d 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0d  ';..  z[i] = 0;.
2e021 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0d 0a 7d  .  *pIdx = i;..}
2e022 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  ..../*..** Gener
2e023 61 74 65 20 61 20 43 52 45 41 54 45 20 54 41 42  ate a CREATE TAB
2e024 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 61 70 70  LE statement app
2e025 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65  ropriate for the
2e026 20 67 69 76 65 6e 0d 0a 2a 2a 20 74 61 62 6c 65   given..** table
2e027 2e 20 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c  .  Memory to hol
2e028 64 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68  d the text of th
2e029 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
2e02a 62 74 61 69 6e 65 64 0d 0a 2a 2a 20 66 72 6f 6d  btained..** from
2e02b 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
2e02c 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
2e02d 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
2e02e 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a   function...*/..
2e02f 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 72 65  static char *cre
2e030 61 74 65 54 61 62 6c 65 53 74 6d 74 28 73 71 6c  ateTableStmt(sql
2e031 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
2e032 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6b  *p){..  int i, k
2e033 2c 20 6e 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 53  , n;..  char *zS
2e034 74 6d 74 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 53  tmt;..  char *zS
2e035 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e  ep, *zSep2, *zEn
2e036 64 3b 0d 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  d;..  Column *pC
2e037 6f 6c 3b 0d 0a 20 20 6e 20 3d 20 30 3b 0d 0a 20  ol;..  n = 0;.. 
2e038 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61   for(pCol = p->a
2e039 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e  Col, i=0; i<p->n
2e03a 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; i++, pCol++
2e03b 29 7b 0d 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65  ){..    n += ide
2e03c 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a  ntLength(pCol->z
2e03d 4e 61 6d 65 29 20 2b 20 35 3b 0d 0a 20 20 7d 0d  Name) + 5;..  }.
2e03e 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e  .  n += identLen
2e03f 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a  gth(p->zName);..
2e040 20 20 69 66 28 20 6e 3c 35 30 20 29 7b 20 0d 0a    if( n<50 ){ ..
2e041 20 20 20 20 7a 53 65 70 20 3d 20 22 22 3b 0d 0a      zSep = "";..
2e042 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 22 3b      zSep2 = ",";
2e043 0d 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22 29 22  ..    zEnd = ")"
2e044 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
2e045 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22 3b 0d   zSep = "\n  ";.
2e046 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 5c  .    zSep2 = ",\
2e047 6e 20 20 22 3b 0d 0a 20 20 20 20 7a 45 6e 64 20  n  ";..    zEnd 
2e048 3d 20 22 5c 6e 29 22 3b 0d 0a 20 20 7d 0d 0a 20  = "\n)";..  }.. 
2e049 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e   n += 35 + 6*p->
2e04a 6e 43 6f 6c 3b 0d 0a 20 20 7a 53 74 6d 74 20 3d  nCol;..  zStmt =
2e04b 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2e04c 52 61 77 28 30 2c 20 6e 29 3b 0d 0a 20 20 69 66  Raw(0, n);..  if
2e04d 28 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0d 0a 20  ( zStmt==0 ){.. 
2e04e 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2e04f 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 72 65  led = 1;..    re
2e050 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 0;..  }..  
2e051 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
2e052 28 6e 2c 20 7a 53 74 6d 74 2c 20 22 43 52 45 41  (n, zStmt, "CREA
2e053 54 45 20 54 41 42 4c 45 20 22 29 3b 0d 0a 20 20  TE TABLE ");..  
2e054 6b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  k = sqlite3Strle
2e055 6e 33 30 28 7a 53 74 6d 74 29 3b 0d 0a 20 20 69  n30(zStmt);..  i
2e056 64 65 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26  dentPut(zStmt, &
2e057 6b 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  k, p->zName);.. 
2e058 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28   zStmt[k++] = '(
2e059 27 3b 0d 0a 20 20 66 6f 72 28 70 43 6f 6c 3d 70  ';..  for(pCol=p
2e05a 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70  ->aCol, i=0; i<p
2e05b 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
2e05c 6c 2b 2b 29 7b 0d 0a 20 20 20 20 73 74 61 74 69  l++){..    stati
2e05d 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  c const char * c
2e05e 6f 6e 73 74 20 61 7a 54 79 70 65 5b 5d 20 3d 20  onst azType[] = 
2e05f 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51  {..        /* SQ
2e060 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20  LITE_AFF_TEXT   
2e061 20 2a 2f 20 22 20 54 45 58 54 22 2c 0d 0a 20 20   */ " TEXT",..  
2e062 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
2e063 41 46 46 5f 4e 4f 4e 45 20 20 20 20 2a 2f 20 22  AFF_NONE    */ "
2e064 22 2c 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53  ",..        /* S
2e065 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
2e066 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0d 0a 20 20  C */ " NUM",..  
2e067 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
2e068 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22  AFF_INTEGER */ "
2e069 20 49 4e 54 22 2c 0d 0a 20 20 20 20 20 20 20 20   INT",..        
2e06a 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  /* SQLITE_AFF_RE
2e06b 41 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22  AL    */ " REAL"
2e06c 0d 0a 20 20 20 20 7d 3b 0d 0a 20 20 20 20 69 6e  ..    };..    in
2e06d 74 20 6c 65 6e 3b 0d 0a 20 20 20 20 63 6f 6e 73  t len;..    cons
2e06e 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 0d 0a  t char *zType;..
2e06f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
2e070 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74  printf(n-k, &zSt
2e071 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0d 0a 20  mt[k], zSep);.. 
2e072 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53     k += sqlite3S
2e073 74 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b  trlen30(&zStmt[k
2e074 5d 29 3b 0d 0a 20 20 20 20 7a 53 65 70 20 3d 20  ]);..    zSep = 
2e075 7a 53 65 70 32 3b 0d 0a 20 20 20 20 69 64 65 6e  zSep2;..    iden
2e076 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
2e077 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  pCol->zName);.. 
2e078 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d     assert( pCol-
2e079 3e 61 66 66 69 6e 69 74 79 2d 53 51 4c 49 54 45  >affinity-SQLITE
2e07a 5f 41 46 46 5f 54 45 58 54 20 3e 3d 20 30 20 29  _AFF_TEXT >= 0 )
2e07b 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
2e07c 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53 51  Col->affinity-SQ
2e07d 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 3c 20  LITE_AFF_TEXT < 
2e07e 41 72 72 61 79 53 69 7a 65 28 61 7a 54 79 70 65  ArraySize(azType
2e07f 29 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61  ) );..    testca
2e080 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  se( pCol->affini
2e081 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54  ty==SQLITE_AFF_T
2e082 45 58 54 20 29 3b 0d 0a 20 20 20 20 74 65 73 74  EXT );..    test
2e083 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69  case( pCol->affi
2e084 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
2e085 5f 4e 4f 4e 45 20 29 3b 0d 0a 20 20 20 20 74 65  _NONE );..    te
2e086 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
2e087 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
2e088 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0d 0a 20  FF_NUMERIC );.. 
2e089 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f     testcase( pCo
2e08a 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
2e08b 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
2e08c 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65  );..    testcase
2e08d 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
2e08e 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
2e08f 4c 20 29 3b 0d 0a 20 20 20 20 0d 0a 20 20 20 20  L );..    ..    
2e090 7a 54 79 70 65 20 3d 20 61 7a 54 79 70 65 5b 70  zType = azType[p
2e091 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20  Col->affinity - 
2e092 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 5d  SQLITE_AFF_TEXT]
2e093 3b 0d 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c  ;..    len = sql
2e094 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79  ite3Strlen30(zTy
2e095 70 65 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  pe);..    assert
2e096 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
2e097 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
2e098 45 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  E ..            
2e099 7c 7c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  || pCol->affinit
2e09a 79 3d 3d 73 71 6c 69 74 65 33 41 66 66 69 6e 69  y==sqlite3Affini
2e09b 74 79 54 79 70 65 28 7a 54 79 70 65 29 20 29 3b  tyType(zType) );
2e09c 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 53  ..    memcpy(&zS
2e09d 74 6d 74 5b 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c  tmt[k], zType, l
2e09e 65 6e 29 3b 0d 0a 20 20 20 20 6b 20 2b 3d 20 6c  en);..    k += l
2e09f 65 6e 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  en;..    assert(
2e0a0 20 6b 3c 3d 6e 20 29 3b 0d 0a 20 20 7d 0d 0a 20   k<=n );..  }.. 
2e0a1 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
2e0a2 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d  f(n-k, &zStmt[k]
2e0a3 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0d 0a  , "%s", zEnd);..
2e0a4 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0d    return zStmt;.
2e0a5 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
2e0a6 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2e0a7 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68  led to report th
2e0a8 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74  e final ")" that
2e0a9 20 74 65 72 6d 69 6e 61 74 65 73 0d 0a 2a 2a 20   terminates..** 
2e0aa 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
2e0ab 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a  tatement...**..*
2e0ac 2a 20 54 68 65 20 74 61 62 6c 65 20 73 74 72 75  * The table stru
2e0ad 63 74 75 72 65 20 74 68 61 74 20 6f 74 68 65 72  cture that other
2e0ae 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
2e0af 20 68 61 76 65 20 62 65 65 6e 20 62 75 69 6c 64   have been build
2e0b0 69 6e 67 0d 0a 2a 2a 20 69 73 20 61 64 64 65 64  ing..** is added
2e0b1 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   to the internal
2e0b2 20 68 61 73 68 20 74 61 62 6c 65 73 2c 20 61 73   hash tables, as
2e0b3 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72 73  suming no errors
2e0b4 20 68 61 76 65 0d 0a 2a 2a 20 6f 63 63 75 72 72   have..** occurr
2e0b5 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 65  ed...**..** An e
2e0b6 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74 61 62  ntry for the tab
2e0b7 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68  le is made in th
2e0b8 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f  e master table o
2e0b9 6e 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0d 0a  n disk, unless..
2e0ba 2a 2a 20 74 68 69 73 20 69 73 20 61 20 74 65 6d  ** this is a tem
2e0bb 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72 20  porary table or 
2e0bc 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31  db->init.busy==1
2e0bd 2e 20 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74  .  When db->init
2e0be 2e 62 75 73 79 3d 3d 31 0d 0a 2a 2a 20 69 74 20  .busy==1..** it 
2e0bf 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61  means we are rea
2e0c0 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f  ding the sqlite_
2e0c1 6d 61 73 74 65 72 20 74 61 62 6c 65 20 62 65 63  master table bec
2e0c2 61 75 73 65 20 77 65 20 6a 75 73 74 0d 0a 2a 2a  ause we just..**
2e0c3 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 74 68   connected to th
2e0c4 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 62 65  e database or be
2e0c5 63 61 75 73 65 20 74 68 65 20 73 71 6c 69 74 65  cause the sqlite
2e0c6 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 68 61  _master table ha
2e0c7 73 0d 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20 63  s..** recently c
2e0c8 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 20 65  hanged, so the e
2e0c9 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 74 61  ntry for this ta
2e0ca 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73  ble already exis
2e0cb 74 73 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 73 71  ts in..** the sq
2e0cc 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2e0cd 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61  e.  We do not wa
2e0ce 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 20  nt to create it 
2e0cf 61 67 61 69 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  again...**..** I
2e0d0 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72  f the pSelect ar
2e0d1 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55  gument is not NU
2e0d2 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LL, it means tha
2e0d3 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a  t this routine..
2e0d4 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f  ** was called to
2e0d5 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20   create a table 
2e0d6 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61  generated from a
2e0d7 20 0d 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41   ..** "CREATE TA
2e0d8 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43  BLE ... AS SELEC
2e0d9 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74  T ..." statement
2e0da 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  .  The column na
2e0db 6d 65 73 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 6e  mes of..** the n
2e0dc 65 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61  ew table will ma
2e0dd 74 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73  tch the result s
2e0de 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
2e0df 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
2e0e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2e0e1 65 33 45 6e 64 54 61 62 6c 65 28 0d 0a 20 20 50  e3EndTable(..  P
2e0e2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2e0e3 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
2e0e4 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 6f  context */..  To
2e0e5 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20  ken *pCons,     
2e0e6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 27 2c 27        /* The ','
2e0e7 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65   token after the
2e0e8 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66   last column def
2e0e9 6e 2e 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a  n. */..  Token *
2e0ea 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  pEnd,           
2e0eb 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 27 29   /* The final ')
2e0ec 27 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 43  ' token in the C
2e0ed 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0d 0a  REATE TABLE */..
2e0ee 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2e0ef 74 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c  t         /* Sel
2e0f0 65 63 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41  ect from a "CREA
2e0f1 54 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54  TE ... AS SELECT
2e0f2 22 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 61 62 6c  " */..){..  Tabl
2e0f3 65 20 2a 70 3b 0d 0a 20 20 73 71 6c 69 74 65 33  e *p;..  sqlite3
2e0f4 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2e0f5 62 3b 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d 0a  b;..  int iDb;..
2e0f6 0d 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30  ..  if( (pEnd==0
2e0f7 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20   && pSelect==0) 
2e0f8 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2e0f9 6c 65 64 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  led ){..    retu
2e0fa 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 70 20 3d 20  rn;..  }..  p = 
2e0fb 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2e0fc 65 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  e;..  if( p==0 )
2e0fd 20 72 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 61 73   return;....  as
2e0fe 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e  sert( !db->init.
2e0ff 62 75 73 79 20 7c 7c 20 21 70 53 65 6c 65 63 74  busy || !pSelect
2e100 20 29 3b 0d 0a 0d 0a 20 20 69 44 62 20 3d 20 73   );....  iDb = s
2e101 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2e102 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65  dex(db, p->pSche
2e103 6d 61 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ma);....#ifndef 
2e104 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
2e105 4b 0d 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  K..  /* Resolve 
2e106 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45  names in all CHE
2e107 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78  CK constraint ex
2e108 70 72 65 73 73 69 6f 6e 73 2e 0d 0a 20 20 2a 2f  pressions...  */
2e109 0d 0a 20 20 69 66 28 20 70 2d 3e 70 43 68 65 63  ..  if( p->pChec
2e10a 6b 20 29 7b 0d 0a 20 20 20 20 53 72 63 4c 69 73  k ){..    SrcLis
2e10b 74 20 73 53 72 63 3b 20 20 20 20 20 20 20 20 20  t sSrc;         
2e10c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b            /* Fak
2e10d 65 20 53 72 63 4c 69 73 74 20 66 6f 72 20 70 50  e SrcList for pP
2e10e 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2e10f 2a 2f 0d 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74  */..    NameCont
2e110 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20  ext sNC;        
2e111 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2e112 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 61 72  context for pPar
2e113 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f  se->pNewTable */
2e114 0d 0a 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  ....    memset(&
2e115 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
2e116 4e 43 29 29 3b 0d 0a 20 20 20 20 6d 65 6d 73 65  NC));..    memse
2e117 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65  t(&sSrc, 0, size
2e118 6f 66 28 73 53 72 63 29 29 3b 0d 0a 20 20 20 20  of(sSrc));..    
2e119 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0d 0a  sSrc.nSrc = 1;..
2e11a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e      sSrc.a[0].zN
2e11b 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0d  ame = p->zName;.
2e11c 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70  .    sSrc.a[0].p
2e11d 54 61 62 20 3d 20 70 3b 0d 0a 20 20 20 20 73 53  Tab = p;..    sS
2e11e 72 63 2e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20  rc.a[0].iCursor 
2e11f 3d 20 2d 31 3b 0d 0a 20 20 20 20 73 4e 43 2e 70  = -1;..    sNC.p
2e120 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0d  Parse = pParse;.
2e121 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73  .    sNC.pSrcLis
2e122 74 20 3d 20 26 73 53 72 63 3b 0d 0a 20 20 20 20  t = &sSrc;..    
2e123 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20 31 3b  sNC.isCheck = 1;
2e124 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
2e125 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2e126 73 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65 63  s(&sNC, p->pChec
2e127 6b 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  k) ){..      ret
2e128 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  urn;..    }..  }
2e129 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
2e12a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2e12b 5f 43 48 45 43 4b 29 20 2a 2f 0d 0a 0d 0a 20 20  _CHECK) */....  
2e12c 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e  /* If the db->in
2e12d 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 20  it.busy is 1 it 
2e12e 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61  means we are rea
2e12f 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 66  ding the SQL off
2e130 20 74 68 65 0d 0a 20 20 2a 2a 20 22 73 71 6c 69   the..  ** "sqli
2e131 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73  te_master" or "s
2e132 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
2e133 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  r" table on the 
2e134 64 69 73 6b 2e 0d 0a 20 20 2a 2a 20 53 6f 20 64  disk...  ** So d
2e135 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  o not write to t
2e136 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20  he disk again.  
2e137 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74  Extract the root
2e138 20 70 61 67 65 20 6e 75 6d 62 65 72 0d 0a 20 20   page number..  
2e139 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
2e13a 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e   from the db->in
2e13b 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64  it.newTnum field
2e13c 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d  .  (The page num
2e13d 62 65 72 0d 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  ber..  ** should
2e13e 20 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74   have been put t
2e13f 68 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69  here by the sqli
2e140 74 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65  teOpenCb routine
2e141 2e 29 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  .)..  */..  if( 
2e142 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
2e143 0d 0a 20 20 20 20 70 2d 3e 74 6e 75 6d 20 3d 20  ..    p->tnum = 
2e144 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
2e145 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49  ;..  }....  /* I
2e146 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69  f not initializi
2e147 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  ng, then create 
2e148 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65  a record for the
2e149 20 6e 65 77 20 74 61 62 6c 65 0d 0a 20 20 2a 2a   new table..  **
2e14a 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   in the SQLITE_M
2e14b 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74  ASTER table of t
2e14c 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a 20 20  he database...  
2e14d 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20 74 68 69 73  **..  ** If this
2e14e 20 69 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20   is a TEMPORARY 
2e14f 74 61 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65  table, write the
2e150 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
2e151 61 75 78 69 6c 69 61 72 79 0d 0a 20 20 2a 2a 20  auxiliary..  ** 
2e152 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20  file instead of 
2e153 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61  into the main da
2e154 74 61 62 61 73 65 20 66 69 6c 65 2e 0d 0a 20 20  tabase file...  
2e155 2a 2f 0d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69  */..  if( !db->i
2e156 6e 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20 20  nit.busy ){..   
2e157 20 69 6e 74 20 6e 3b 0d 0a 20 20 20 20 56 64 62   int n;..    Vdb
2e158 65 20 2a 76 3b 0d 0a 20 20 20 20 63 68 61 72 20  e *v;..    char 
2e159 2a 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76  *zType;    /* "v
2e15a 69 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20  iew" or "table" 
2e15b 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  */..    char *zT
2e15c 79 70 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57  ype2;   /* "VIEW
2e15d 22 20 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0d  " or "TABLE" */.
2e15e 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  .    char *zStmt
2e15f 3b 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20  ;    /* Text of 
2e160 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
2e161 20 6f 72 20 43 52 45 41 54 45 20 56 49 45 57 20   or CREATE VIEW 
2e162 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 0d 0a  statement */....
2e163 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
2e164 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d  etVdbe(pParse);.
2e165 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 76  .    if( NEVER(v
2e166 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a  ==0) ) return;..
2e167 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2e168 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
2e169 6f 73 65 2c 20 30 29 3b 0d 0a 0d 0a 20 20 20 20  ose, 0);....    
2e16a 2f 2a 20 0d 0a 20 20 20 20 2a 2a 20 49 6e 69 74  /* ..    ** Init
2e16b 69 61 6c 69 7a 65 20 7a 54 79 70 65 20 66 6f 72  ialize zType for
2e16c 20 74 68 65 20 6e 65 77 20 76 69 65 77 20 6f 72   the new view or
2e16d 20 74 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2f 0d   table...    */.
2e16e 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65 6c  .    if( p->pSel
2e16f 65 63 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ect==0 ){..     
2e170 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61   /* A regular ta
2e171 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 20 20 7a 54  ble */..      zT
2e172 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0d 0a  ype = "table";..
2e173 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22        zType2 = "
2e174 54 41 42 4c 45 22 3b 0d 0a 23 69 66 6e 64 65 66  TABLE";..#ifndef
2e175 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
2e176 57 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  W..    }else{.. 
2e177 20 20 20 20 20 2f 2a 20 41 20 76 69 65 77 20 2a       /* A view *
2e178 2f 0d 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  /..      zType =
2e179 20 22 76 69 65 77 22 3b 0d 0a 20 20 20 20 20 20   "view";..      
2e17a 7a 54 79 70 65 32 20 3d 20 22 56 49 45 57 22 3b  zType2 = "VIEW";
2e17b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 7d 0d  ..#endif..    }.
2e17c 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ...    /* If thi
2e17d 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41  s is a CREATE TA
2e17e 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54  BLE xx AS SELECT
2e17f 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68   ..., execute th
2e180 65 20 53 45 4c 45 43 54 0d 0a 20 20 20 20 2a 2a  e SELECT..    **
2e181 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f   statement to po
2e182 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74  pulate the new t
2e183 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70  able. The root-p
2e184 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
2e185 68 65 0d 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74  he..    ** new t
2e186 61 62 6c 65 20 69 73 20 69 6e 20 72 65 67 69 73  able is in regis
2e187 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52  ter pParse->regR
2e188 6f 6f 74 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  oot...    **..  
2e189 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20 53 45    ** Once the SE
2e18a 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20 63 6f  LECT has been co
2e18b 64 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65  ded by sqlite3Se
2e18c 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20 69 6e  lect(), it is in
2e18d 20 61 0d 0a 20 20 20 20 2a 2a 20 73 75 69 74 61   a..    ** suita
2e18e 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75 65  ble state to que
2e18f 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ry for the colum
2e190 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70 65  n names and type
2e191 73 20 74 6f 20 62 65 20 75 73 65 64 0d 0a 20 20  s to be used..  
2e192 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20    ** by the new 
2e193 74 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2a 0d 0a  table...    **..
2e194 20 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d      ** A shared-
2e195 63 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b  cache write-lock
2e196 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
2e197 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65   to write to the
2e198 20 6e 65 77 20 74 61 62 6c 65 2c 0d 0a 20 20 20   new table,..   
2e199 20 2a 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d   ** as a schema-
2e19a 6c 6f 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61  lock must have a
2e19b 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61  lready been obta
2e19c 69 6e 65 64 20 74 6f 20 63 72 65 61 74 65 20 69  ined to create i
2e19d 74 2e 20 53 69 6e 63 65 0d 0a 20 20 20 20 2a 2a  t. Since..    **
2e19e 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65   a schema-lock e
2e19f 78 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65  xcludes all othe
2e1a0 72 20 64 61 74 61 62 61 73 65 20 75 73 65 72 73  r database users
2e1a1 2c 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b  , the write-lock
2e1a2 20 77 6f 75 6c 64 0d 0a 20 20 20 20 2a 2a 20 62   would..    ** b
2e1a3 65 20 72 65 64 75 6e 64 61 6e 74 2e 0d 0a 20 20  e redundant...  
2e1a4 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 53    */..    if( pS
2e1a5 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20 20 20 20  elect ){..      
2e1a6 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
2e1a7 0d 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  ..      Table *p
2e1a8 53 65 6c 54 61 62 3b 0d 0a 0d 0a 20 20 20 20 20  SelTab;....     
2e1a9 20 61 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e   assert(pParse->
2e1aa 6e 54 61 62 3d 3d 31 29 3b 0d 0a 20 20 20 20 20  nTab==1);..     
2e1ab 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e1ac 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
2e1ad 74 65 2c 20 31 2c 20 70 50 61 72 73 65 2d 3e 72  te, 1, pParse->r
2e1ae 65 67 52 6f 6f 74 2c 20 69 44 62 29 3b 0d 0a 20  egRoot, iDb);.. 
2e1af 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e1b0 43 68 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0d  ChangeP5(v, 1);.
2e1b1 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
2e1b2 54 61 62 20 3d 20 32 3b 0d 0a 20 20 20 20 20 20  Tab = 2;..      
2e1b3 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2e1b4 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
2e1b5 5f 54 61 62 6c 65 2c 20 31 29 3b 0d 0a 20 20 20  _Table, 1);..   
2e1b6 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2e1b7 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
2e1b8 2c 20 26 64 65 73 74 29 3b 0d 0a 20 20 20 20 20  , &dest);..     
2e1b9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e1ba 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
2e1bb 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  1);..      if( p
2e1bc 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
2e1bd 7b 0d 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54  {..        pSelT
2e1be 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75  ab = sqlite3Resu
2e1bf 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50  ltSetOfSelect(pP
2e1c0 61 72 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0d  arse, pSelect);.
2e1c1 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 53 65  .        if( pSe
2e1c2 6c 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  lTab==0 ) return
2e1c3 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ;..        asser
2e1c4 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b  t( p->aCol==0 );
2e1c5 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 43 6f  ..        p->nCo
2e1c6 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f  l = pSelTab->nCo
2e1c7 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 61  l;..        p->a
2e1c8 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 61  Col = pSelTab->a
2e1c9 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70 53  Col;..        pS
2e1ca 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b  elTab->nCol = 0;
2e1cb 0d 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61  ..        pSelTa
2e1cc 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0d 0a 20 20  b->aCol = 0;..  
2e1cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
2e1ce 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 53 65  eteTable(db, pSe
2e1cf 6c 54 61 62 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  lTab);..      }.
2e1d0 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
2e1d1 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
2e1d2 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
2e1d3 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65  e CREATE stateme
2e1d4 6e 74 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70  nt */..    if( p
2e1d5 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20 20 20  Select ){..     
2e1d6 20 7a 53 74 6d 74 20 3d 20 63 72 65 61 74 65 54   zStmt = createT
2e1d7 61 62 6c 65 53 74 6d 74 28 64 62 2c 20 70 29 3b  ableStmt(db, p);
2e1d8 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
2e1d9 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 70 45      n = (int)(pE
2e1da 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e  nd->z - pParse->
2e1db 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20  sNameToken.z) + 
2e1dc 31 3b 0d 0a 20 20 20 20 20 20 7a 53 74 6d 74 20  1;..      zStmt 
2e1dd 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2e1de 28 64 62 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  (db, ..         
2e1df 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a 73   "CREATE %s %.*s
2e1e0 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70 50  ", zType2, n, pP
2e1e1 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
2e1e2 2e 7a 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20  .z..      );..  
2e1e3 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 20    }....    /* A 
2e1e4 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63  slot for the rec
2e1e5 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20  ord has already 
2e1e6 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69  been allocated i
2e1e7 6e 20 74 68 65 20 0d 0a 20 20 20 20 2a 2a 20 53  n the ..    ** S
2e1e8 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
2e1e9 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65  le.  We just nee
2e1ea 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 61 74  d to update that
2e1eb 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0d 0a   slot with all..
2e1ec 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72      ** the infor
2e1ed 6d 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c  mation we've col
2e1ee 6c 65 63 74 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d  lected...    */.
2e1ef 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
2e1f0 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0d  edParse(pParse,.
2e1f1 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
2e1f2 51 2e 25 73 20 22 0d 0a 20 20 20 20 20 20 20 20  Q.%s "..        
2e1f3 20 22 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c   "SET type='%s',
2e1f4 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61   name=%Q, tbl_na
2e1f5 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d  me=%Q, rootpage=
2e1f6 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 0d 0a 20  #%d, sql=%Q ".. 
2e1f7 20 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77        "WHERE row
2e1f8 69 64 3d 23 25 64 22 2c 0d 0a 20 20 20 20 20 20  id=#%d",..      
2e1f9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2e1fa 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
2e1fb 28 69 44 62 29 2c 0d 0a 20 20 20 20 20 20 7a 54  (iDb),..      zT
2e1fc 79 70 65 2c 0d 0a 20 20 20 20 20 20 70 2d 3e 7a  ype,..      p->z
2e1fd 4e 61 6d 65 2c 0d 0a 20 20 20 20 20 20 70 2d 3e  Name,..      p->
2e1fe 7a 4e 61 6d 65 2c 0d 0a 20 20 20 20 20 20 70 50  zName,..      pP
2e1ff 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0d 0a  arse->regRoot,..
2e200 20 20 20 20 20 20 7a 53 74 6d 74 2c 0d 0a 20 20        zStmt,..  
2e201 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52      pParse->regR
2e202 6f 77 69 64 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  owid..    );..  
2e203 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e204 64 62 2c 20 7a 53 74 6d 74 29 3b 0d 0a 20 20 20  db, zStmt);..   
2e205 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
2e206 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
2e207 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  );....#ifndef SQ
2e208 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
2e209 43 52 45 4d 45 4e 54 0d 0a 20 20 20 20 2f 2a 20  CREMENT..    /* 
2e20a 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2e20b 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74  we need to creat
2e20c 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  e an sqlite_sequ
2e20d 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0d 0a  ence table for..
2e20e 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74      ** keeping t
2e20f 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72  rack of autoincr
2e210 65 6d 65 6e 74 20 6b 65 79 73 2e 0d 0a 20 20 20  ement keys...   
2e211 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e   */..    if( p->
2e212 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
2e213 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0d 0a  toincrement ){..
2e214 20 20 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20        Db *pDb = 
2e215 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0d 0a  &db->aDb[iDb];..
2e216 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71        assert( sq
2e217 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
2e218 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29  Held(db, iDb, 0)
2e219 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70   );..      if( p
2e21a 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
2e21b 71 54 61 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  qTab==0 ){..    
2e21c 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
2e21d 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0d 0a  dParse(pParse,..
2e21e 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
2e21f 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c 69 74  E TABLE %Q.sqlit
2e220 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c  e_sequence(name,
2e221 73 65 71 29 22 2c 0d 0a 20 20 20 20 20 20 20 20  seq)",..        
2e222 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0d 0a 20 20    pDb->zName..  
2e223 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
2e224 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  }..    }..#endif
2e225 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 70 61 72  ....    /* Repar
2e226 73 65 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f  se everything to
2e227 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65   update our inte
2e228 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74  rnal data struct
2e229 75 72 65 73 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  ures */..    sql
2e22a 69 74 65 33 56 64 62 65 41 64 64 50 61 72 73 65  ite3VdbeAddParse
2e22b 53 63 68 65 6d 61 4f 70 28 76 2c 20 69 44 62 2c  SchemaOp(v, iDb,
2e22c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
2e22d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2e22e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25  db, "tbl_name='%
2e22f 71 27 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 29 3b  q'", p->zName));
2e230 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20 20 2f 2a 20  ..  }......  /* 
2e231 41 64 64 20 74 68 65 20 74 61 62 6c 65 20 74 6f  Add the table to
2e232 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
2e233 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
2e234 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a   the database...
2e235 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e    */..  if( db->
2e236 69 6e 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20  init.busy ){..  
2e237 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0d 0a    Table *pOld;..
2e238 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68      Schema *pSch
2e239 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61  ema = p->pSchema
2e23a 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ;..    assert( s
2e23b 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65  qlite3SchemaMute
2e23c 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30  xHeld(db, iDb, 0
2e23d 29 20 29 3b 0d 0a 20 20 20 20 70 4f 6c 64 20 3d  ) );..    pOld =
2e23e 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
2e23f 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c  rt(&pSchema->tbl
2e240 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0d  Hash, p->zName,.
2e241 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2e243 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d  lite3Strlen30(p-
2e244 3e 7a 4e 61 6d 65 29 2c 70 29 3b 0d 0a 20 20 20  >zName),p);..   
2e245 20 69 66 28 20 70 4f 6c 64 20 29 7b 0d 0a 20 20   if( pOld ){..  
2e246 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70      assert( p==p
2e247 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f  Old );  /* Mallo
2e248 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c  c must have fail
2e249 65 64 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e  ed inside HashIn
2e24a 73 65 72 74 28 29 20 2a 2f 0d 0a 20 20 20 20 20  sert() */..     
2e24b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2e24c 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 72 65  d = 1;..      re
2e24d 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  turn;..    }..  
2e24e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
2e24f 62 6c 65 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62  ble = 0;..    db
2e250 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0d 0a 20 20 20  ->nTable++;..   
2e251 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51   db->flags |= SQ
2e252 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
2e253 65 73 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  es;....#ifndef S
2e254 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52  QLITE_OMIT_ALTER
2e255 54 41 42 4c 45 0d 0a 20 20 20 20 69 66 28 20 21  TABLE..    if( !
2e256 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a 20  p->pSelect ){.. 
2e257 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2e258 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20  *zName = (const 
2e259 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73  char *)pParse->s
2e25a 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0d 0a 20 20  NameToken.z;..  
2e25b 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0d 0a      int nName;..
2e25c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
2e25d 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20  Select && pCons 
2e25e 26 26 20 70 45 6e 64 20 29 3b 0d 0a 20 20 20 20  && pEnd );..    
2e25f 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d    if( pCons->z==
2e260 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 43  0 ){..        pC
2e261 6f 6e 73 20 3d 20 70 45 6e 64 3b 0d 0a 20 20 20  ons = pEnd;..   
2e262 20 20 20 7d 0d 0a 20 20 20 20 20 20 6e 4e 61 6d     }..      nNam
2e263 65 20 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74  e = (int)((const
2e264 20 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a   char *)pCons->z
2e265 20 2d 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20   - zName);..    
2e266 20 20 70 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65    p->addColOffse
2e267 74 20 3d 20 31 33 20 2b 20 73 71 6c 69 74 65 33  t = 13 + sqlite3
2e268 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d  Utf8CharLen(zNam
2e269 65 2c 20 6e 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  e, nName);..    
2e26a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a  }..#endif..  }..
2e26b 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  }....#ifndef SQL
2e26c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 2f  ITE_OMIT_VIEW../
2e26d 2a 0d 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  *..** The parser
2e26e 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2e26f 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ine in order to 
2e270 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49 45  create a new VIE
2e271 57 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  W..*/..SQLITE_PR
2e272 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2e273 65 33 43 72 65 61 74 65 56 69 65 77 28 0d 0a 20  e3CreateView(.. 
2e274 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2e275 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2e276 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20  ng context */.. 
2e277 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20   Token *pBegin, 
2e278 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
2e279 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
2e27a 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ins the statemen
2e27b 74 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70  t */..  Token *p
2e27c 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68  Name1,     /* Th
2e27d 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
2e27e 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
2e27f 68 65 20 76 69 65 77 20 2a 2f 0d 0a 20 20 54 6f  he view */..  To
2e280 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
2e281 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68   /* The token th
2e282 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
2e283 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f  e of the view */
2e284 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  ..  Select *pSel
2e285 65 63 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45  ect,   /* A SELE
2e286 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  CT statement tha
2e287 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  t will become th
2e288 65 20 6e 65 77 20 76 69 65 77 20 2a 2f 0d 0a 20  e new view */.. 
2e289 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20   int isTemp,    
2e28a 20 20 20 20 2f 2a 20 54 52 55 45 20 66 6f 72 20      /* TRUE for 
2e28b 61 20 54 45 4d 50 4f 52 41 52 59 20 76 69 65 77  a TEMPORARY view
2e28c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 6f 45 72 72   */..  int noErr
2e28d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 70            /* Sup
2e28e 70 72 65 73 73 20 65 72 72 6f 72 20 6d 65 73 73  press error mess
2e28f 61 67 65 73 20 69 66 20 56 49 45 57 20 61 6c 72  ages if VIEW alr
2e290 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0d 0a  eady exists */..
2e291 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0d  ){..  Table *p;.
2e292 0a 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 63 6f 6e  .  int n;..  con
2e293 73 74 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20 54  st char *z;..  T
2e294 6f 6b 65 6e 20 73 45 6e 64 3b 0d 0a 20 20 44 62  oken sEnd;..  Db
2e295 46 69 78 65 72 20 73 46 69 78 3b 0d 0a 20 20 54  Fixer sFix;..  T
2e296 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 3b  oken *pName = 0;
2e297 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d 0a 20 20  ..  int iDb;..  
2e298 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2e299 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 69  arse->db;....  i
2e29a 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e  f( pParse->nVar>
2e29b 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0 ){..    sqlite
2e29c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2e29d 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20 61 72  , "parameters ar
2e29e 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e  e not allowed in
2e29f 20 76 69 65 77 73 22 29 3b 0d 0a 20 20 20 20 73   views");..    s
2e2a0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2e2a1 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
2e2a2 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20  ..    return;.. 
2e2a3 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 53 74 61   }..  sqlite3Sta
2e2a4 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  rtTable(pParse, 
2e2a5 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
2e2a6 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f  isTemp, 1, 0, no
2e2a7 45 72 72 29 3b 0d 0a 20 20 70 20 3d 20 70 50 61  Err);..  p = pPa
2e2a8 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0d  rse->pNewTable;.
2e2a9 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
2e2aa 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0d 0a  Parse->nErr ){..
2e2ab 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2e2ac 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
2e2ad 65 63 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  ect);..    retur
2e2ae 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  n;..  }..  sqlit
2e2af 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
2e2b0 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
2e2b1 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0d 0a  ame2, &pName);..
2e2b2 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2e2b3 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2e2b4 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 20   p->pSchema);.. 
2e2b5 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
2e2b6 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
2e2b7 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20  e, iDb, "view", 
2e2b8 70 4e 61 6d 65 29 0d 0a 20 20 20 20 26 26 20 73  pName)..    && s
2e2b9 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
2e2ba 26 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0d  &sFix, pSelect).
2e2bb 0a 20 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  .  ){..    sqlit
2e2bc 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2e2bd 62 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20  b, pSelect);..  
2e2be 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
2e2bf 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f  ..  /* Make a co
2e2c0 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  py of the entire
2e2c1 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2e2c2 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  t that defines t
2e2c3 68 65 20 76 69 65 77 2e 0d 0a 20 20 2a 2a 20 54  he view...  ** T
2e2c4 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20 61  his will force a
2e2c5 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65  ll the Expr.toke
2e2c6 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62 65  n.z values to be
2e2c7 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0d 0a 20 20   dynamically..  
2e2c8 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74  ** allocated rat
2e2c9 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74  her than point t
2e2ca 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  o the input stri
2e2cb 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73  ng - which means
2e2cc 20 74 68 61 74 0d 0a 20 20 2a 2a 20 74 68 65 79   that..  ** they
2e2cd 20 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66   will persist af
2e2ce 74 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ter the current 
2e2cf 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63  sqlite3_exec() c
2e2d0 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0d 0a 20 20  all returns...  
2e2d1 2a 2f 0d 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74  */..  p->pSelect
2e2d2 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2e2d3 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c  Dup(db, pSelect,
2e2d4 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
2e2d5 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  ;..  sqlite3Sele
2e2d6 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
2e2d7 6c 65 63 74 29 3b 0d 0a 20 20 69 66 28 20 64 62  lect);..  if( db
2e2d8 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2e2d9 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  {..    return;..
2e2da 20 20 7d 0d 0a 20 20 69 66 28 20 21 64 62 2d 3e    }..  if( !db->
2e2db 69 6e 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20  init.busy ){..  
2e2dc 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74    sqlite3ViewGet
2e2dd 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
2e2de 73 65 2c 20 70 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  se, p);..  }....
2e2df 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2e2e0 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54  end of the CREAT
2e2e1 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74  E VIEW statement
2e2e2 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70 6f 69  .  Make sEnd poi
2e2e3 6e 74 20 74 6f 0d 0a 20 20 2a 2a 20 74 68 65 20  nt to..  ** the 
2e2e4 65 6e 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 45  end...  */..  sE
2e2e5 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61  nd = pParse->sLa
2e2e6 73 74 54 6f 6b 65 6e 3b 0d 0a 20 20 69 66 28 20  stToken;..  if( 
2e2e7 41 4c 57 41 59 53 28 73 45 6e 64 2e 7a 5b 30 5d  ALWAYS(sEnd.z[0]
2e2e8 21 3d 30 29 20 26 26 20 73 45 6e 64 2e 7a 5b 30  !=0) && sEnd.z[0
2e2e9 5d 21 3d 27 3b 27 20 29 7b 0d 0a 20 20 20 20 73  ]!=';' ){..    s
2e2ea 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b  End.z += sEnd.n;
2e2eb 0d 0a 20 20 7d 0d 0a 20 20 73 45 6e 64 2e 6e 20  ..  }..  sEnd.n 
2e2ec 3d 20 30 3b 0d 0a 20 20 6e 20 3d 20 28 69 6e 74  = 0;..  n = (int
2e2ed 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69  )(sEnd.z - pBegi
2e2ee 6e 2d 3e 7a 29 3b 0d 0a 20 20 7a 20 3d 20 70 42  n->z);..  z = pB
2e2ef 65 67 69 6e 2d 3e 7a 3b 0d 0a 20 20 77 68 69 6c  egin->z;..  whil
2e2f0 65 28 20 41 4c 57 41 59 53 28 6e 3e 30 29 20 26  e( ALWAYS(n>0) &
2e2f1 26 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  & sqlite3Isspace
2e2f2 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d 3b  (z[n-1]) ){ n--;
2e2f3 20 7d 0d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26   }..  sEnd.z = &
2e2f4 7a 5b 6e 2d 31 5d 3b 0d 0a 20 20 73 45 6e 64 2e  z[n-1];..  sEnd.
2e2f5 6e 20 3d 20 31 3b 0d 0a 0d 0a 20 20 2f 2a 20 55  n = 1;....  /* U
2e2f6 73 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  se sqlite3EndTab
2e2f7 6c 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20  le() to add the 
2e2f8 76 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49  view to the SQLI
2e2f9 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
2e2fa 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64  */..  sqlite3End
2e2fb 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
2e2fc 20 26 73 45 6e 64 2c 20 30 29 3b 0d 0a 20 20 72   &sEnd, 0);..  r
2e2fd 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  eturn;..}..#endi
2e2fe 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2e2ff 5f 56 49 45 57 20 2a 2f 0d 0a 0d 0a 23 69 66 20  _VIEW */....#if 
2e300 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2e301 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
2e302 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2e303 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
2e304 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 54 61 62  ../*..** The Tab
2e305 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 54 61  le structure pTa
2e306 62 6c 65 20 69 73 20 72 65 61 6c 6c 79 20 61 20  ble is really a 
2e307 56 49 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20 74  VIEW.  Fill in t
2e308 68 65 20 6e 61 6d 65 73 20 6f 66 0d 0a 2a 2a 20  he names of..** 
2e309 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  the columns of t
2e30a 68 65 20 76 69 65 77 20 69 6e 20 74 68 65 20 70  he view in the p
2e30b 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e  Table structure.
2e30c 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d    Return the num
2e30d 62 65 72 0d 0a 2a 2a 20 6f 66 20 65 72 72 6f 72  ber..** of error
2e30e 73 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20  s.  If an error 
2e30f 69 73 20 73 65 65 6e 20 6c 65 61 76 65 20 61 6e  is seen leave an
2e310 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
2e311 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  n pParse->zErrMs
2e312 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  g...*/..SQLITE_P
2e313 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e314 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
2e315 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61 72  ames(Parse *pPar
2e316 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  se, Table *pTabl
2e317 65 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 53  e){..  Table *pS
2e318 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61  elTab;   /* A fa
2e319 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  ke table from wh
2e31a 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72  ich we get the r
2e31b 65 73 75 6c 74 20 73 65 74 20 2a 2f 0d 0a 20 20  esult set */..  
2e31c 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20  Select *pSel;   
2e31d 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65    /* Copy of the
2e31e 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70   SELECT that imp
2e31f 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77  lements the view
2e320 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 45 72 72 20   */..  int nErr 
2e321 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  = 0;     /* Numb
2e322 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
2e323 6f 75 6e 74 65 72 65 64 20 2a 2f 0d 0a 20 20 69  ountered */..  i
2e324 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
2e325 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20   /* Temporarily 
2e326 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72  holds the number
2e327 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69   of cursors assi
2e328 67 6e 65 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  gned */..  sqlit
2e329 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e32a 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73  >db;  /* Databas
2e32b 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72  e connection for
2e32c 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a   malloc errors *
2e32d 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68  /..  int (*xAuth
2e32e 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2e32f 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
2e330 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
2e331 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a 0d  const char*);...
2e332 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
2e333 65 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  e );....#ifndef 
2e334 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2e335 55 41 4c 54 41 42 4c 45 0d 0a 20 20 69 66 28 20  UALTABLE..  if( 
2e336 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
2e337 6f 6e 6e 65 63 74 28 70 50 61 72 73 65 2c 20 70  onnect(pParse, p
2e338 54 61 62 6c 65 29 20 29 7b 0d 0a 20 20 20 20 72  Table) ){..    r
2e339 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
2e33a 4f 52 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  OR;..  }..  if( 
2e33b 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 6c 65  IsVirtual(pTable
2e33c 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 23  ) ) return 0;..#
2e33d 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66  endif....#ifndef
2e33e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
2e33f 57 0d 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69  W..  /* A positi
2e340 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68  ve nCol means th
2e341 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  e columns names 
2e342 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72  for this view ar
2e343 65 0d 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  e..  ** already 
2e344 6b 6e 6f 77 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  known...  */..  
2e345 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  if( pTable->nCol
2e346 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a  >0 ) return 0;..
2e347 0d 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76  ..  /* A negativ
2e348 65 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63  e nCol is a spec
2e349 69 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69  ial marker meani
2e34a 6e 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63  ng that we are c
2e34b 75 72 72 65 6e 74 6c 79 0d 0a 20 20 2a 2a 20 74  urrently..  ** t
2e34c 72 79 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65  rying to compute
2e34d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
2e34e 73 2e 20 20 49 66 20 77 65 20 65 6e 74 65 72 20  s.  If we enter 
2e34f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
2e350 68 0d 0a 20 20 2a 2a 20 61 20 6e 65 67 61 74 69  h..  ** a negati
2e351 76 65 20 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e  ve nCol, it mean
2e352 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69  s two or more vi
2e353 65 77 73 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c  ews form a loop,
2e354 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 20 20 2a   like this:..  *
2e355 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41  *..  **     CREA
2e356 54 45 20 56 49 45 57 20 6f 6e 65 20 41 53 20 53  TE VIEW one AS S
2e357 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 77 6f  ELECT * FROM two
2e358 3b 0d 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41  ;..  **     CREA
2e359 54 45 20 56 49 45 57 20 74 77 6f 20 41 53 20 53  TE VIEW two AS S
2e35a 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6f 6e 65  ELECT * FROM one
2e35b 3b 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 41 63  ;..  **..  ** Ac
2e35c 74 75 61 6c 6c 79 2c 20 74 68 65 20 65 72 72 6f  tually, the erro
2e35d 72 20 61 62 6f 76 65 20 69 73 20 6e 6f 77 20 63  r above is now c
2e35e 61 75 67 68 74 20 70 72 69 6f 72 20 74 6f 20 72  aught prior to r
2e35f 65 61 63 68 69 6e 67 20 74 68 69 73 20 70 6f 69  eaching this poi
2e360 6e 74 2e 0d 0a 20 20 2a 2a 20 42 75 74 20 74 68  nt...  ** But th
2e361 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74  e following test
2e362 20 69 73 20 73 74 69 6c 6c 20 69 6d 70 6f 72 74   is still import
2e363 61 6e 74 20 61 73 20 69 74 20 64 6f 65 73 20 63  ant as it does c
2e364 6f 6d 65 20 75 70 0d 0a 20 20 2a 2a 20 69 6e 20  ome up..  ** in 
2e365 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a  the following:..
2e366 20 20 2a 2a 20 0d 0a 20 20 2a 2a 20 20 20 20 20    ** ..  **     
2e367 43 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61 69  CREATE TABLE mai
2e368 6e 2e 65 78 31 28 61 29 3b 0d 0a 20 20 2a 2a 20  n.ex1(a);..  ** 
2e369 20 20 20 20 43 52 45 41 54 45 20 54 45 4d 50 20      CREATE TEMP 
2e36a 56 49 45 57 20 65 78 31 20 41 53 20 53 45 4c 45  VIEW ex1 AS SELE
2e36b 43 54 20 61 20 46 52 4f 4d 20 65 78 31 3b 0d 0a  CT a FROM ex1;..
2e36c 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2e36d 2a 20 46 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b  * FROM temp.ex1;
2e36e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54  ..  */..  if( pT
2e36f 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0d  able->nCol<0 ){.
2e370 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2e371 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
2e372 65 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61  ew %s is circula
2e373 72 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54  rly defined", pT
2e374 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  able->zName);.. 
2e375 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
2e376 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  }..  assert( pTa
2e377 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0d  ble->nCol>=0 );.
2e378 0a 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65  ...  /* If we ge
2e379 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d  t this far, it m
2e37a 65 61 6e 73 20 77 65 20 6e 65 65 64 20 74 6f 20  eans we need to 
2e37b 63 6f 6d 70 75 74 65 20 74 68 65 20 74 61 62 6c  compute the tabl
2e37c 65 20 6e 61 6d 65 73 2e 0d 0a 20 20 2a 2a 20 4e  e names...  ** N
2e37d 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 61 6c  ote that the cal
2e37e 6c 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 75  l to sqlite3Resu
2e37f 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20  ltSetOfSelect() 
2e380 77 69 6c 6c 20 65 78 70 61 6e 64 20 61 6e 79 0d  will expand any.
2e381 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65 6d 65 6e  .  ** "*" elemen
2e382 74 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ts in the result
2e383 73 20 73 65 74 20 6f 66 20 74 68 65 20 76 69 65  s set of the vie
2e384 77 20 61 6e 64 20 77 69 6c 6c 20 61 73 73 69 67  w and will assig
2e385 6e 20 63 75 72 73 6f 72 73 0d 0a 20 20 2a 2a 20  n cursors..  ** 
2e386 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20  to the elements 
2e387 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2e388 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f 20 6e  se.  But we do n
2e389 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20 63 68  ot want these ch
2e38a 61 6e 67 65 73 0d 0a 20 20 2a 2a 20 74 6f 20 62  anges..  ** to b
2e38b 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53 6f  e permanent.  So
2e38c 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e   the computation
2e38d 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63 6f   is done on a co
2e38e 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  py of the SELECT
2e38f 0d 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ..  ** statement
2e390 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
2e391 65 20 76 69 65 77 2e 0d 0a 20 20 2a 2f 0d 0a 20  e view...  */.. 
2e392 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d   assert( pTable-
2e393 3e 70 53 65 6c 65 63 74 20 29 3b 0d 0a 20 20 70  >pSelect );..  p
2e394 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  Sel = sqlite3Sel
2e395 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c  ectDup(db, pTabl
2e396 65 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0d  e->pSelect, 0);.
2e397 0a 20 20 69 66 28 20 70 53 65 6c 20 29 7b 0d 0a  .  if( pSel ){..
2e398 20 20 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f      u8 enableLoo
2e399 6b 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f  kaside = db->loo
2e39a 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b  kaside.bEnabled;
2e39b 0d 0a 20 20 20 20 6e 20 3d 20 70 50 61 72 73 65  ..    n = pParse
2e39c 2d 3e 6e 54 61 62 3b 0d 0a 20 20 20 20 73 71 6c  ->nTab;..    sql
2e39d 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
2e39e 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
2e39f 20 70 53 65 6c 2d 3e 70 53 72 63 29 3b 0d 0a 20   pSel->pSrc);.. 
2e3a0 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20     pTable->nCol 
2e3a1 3d 20 2d 31 3b 0d 0a 20 20 20 20 64 62 2d 3e 6c  = -1;..    db->l
2e3a2 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2e3a3 64 20 3d 20 30 3b 0d 0a 23 69 66 6e 64 65 66 20  d = 0;..#ifndef 
2e3a4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
2e3a5 4f 52 49 5a 41 54 49 4f 4e 0d 0a 20 20 20 20 78  ORIZATION..    x
2e3a6 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68  Auth = db->xAuth
2e3a7 3b 0d 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68  ;..    db->xAuth
2e3a8 20 3d 20 30 3b 0d 0a 20 20 20 20 70 53 65 6c 54   = 0;..    pSelT
2e3a9 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75  ab = sqlite3Resu
2e3aa 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50  ltSetOfSelect(pP
2e3ab 61 72 73 65 2c 20 70 53 65 6c 29 3b 0d 0a 20 20  arse, pSel);..  
2e3ac 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
2e3ad 75 74 68 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 20  uth;..#else..   
2e3ae 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
2e3af 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
2e3b0 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
2e3b1 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20  );..#endif..    
2e3b2 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2e3b3 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c  nabled = enableL
2e3b4 6f 6f 6b 61 73 69 64 65 3b 0d 0a 20 20 20 20 70  ookaside;..    p
2e3b5 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b  Parse->nTab = n;
2e3b6 0d 0a 20 20 20 20 69 66 28 20 70 53 65 6c 54 61  ..    if( pSelTa
2e3b7 62 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65  b ){..      asse
2e3b8 72 74 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  rt( pTable->aCol
2e3b9 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70 54  ==0 );..      pT
2e3ba 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65  able->nCol = pSe
2e3bb 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0d 0a 20 20 20  lTab->nCol;..   
2e3bc 20 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20     pTable->aCol 
2e3bd 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b  = pSelTab->aCol;
2e3be 0d 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d  ..      pSelTab-
2e3bf 3e 6e 43 6f 6c 20 3d 20 30 3b 0d 0a 20 20 20 20  >nCol = 0;..    
2e3c0 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20    pSelTab->aCol 
2e3c1 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  = 0;..      sqli
2e3c2 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 64  te3DeleteTable(d
2e3c3 62 2c 20 70 53 65 6c 54 61 62 29 3b 0d 0a 20 20  b, pSelTab);..  
2e3c4 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2e3c5 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
2e3c6 6c 64 28 64 62 2c 20 30 2c 20 70 54 61 62 6c 65  ld(db, 0, pTable
2e3c7 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0d 0a 20  ->pSchema) );.. 
2e3c8 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63       pTable->pSc
2e3c9 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44  hema->flags |= D
2e3ca 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0d  B_UnresetViews;.
2e3cb 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
2e3cc 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20     pTable->nCol 
2e3cd 3d 20 30 3b 0d 0a 20 20 20 20 20 20 6e 45 72 72  = 0;..      nErr
2e3ce 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ++;..    }..    
2e3cf 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2e3d0 65 74 65 28 64 62 2c 20 70 53 65 6c 29 3b 0d 0a  ete(db, pSel);..
2e3d1 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20    } else {..    
2e3d2 6e 45 72 72 2b 2b 3b 0d 0a 20 20 7d 0d 0a 23 65  nErr++;..  }..#e
2e3d3 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2e3d4 4d 49 54 5f 56 49 45 57 20 2a 2f 0d 0a 20 20 72  MIT_VIEW */..  r
2e3d5 65 74 75 72 6e 20 6e 45 72 72 3b 20 20 0d 0a 7d  eturn nErr;  ..}
2e3d6 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
2e3d7 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2e3d8 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
2e3d9 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2e3da 49 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f 0d  IRTUALTABLE) */.
2e3db 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
2e3dc 45 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 2f 2a 0d  E_OMIT_VIEW../*.
2e3dd 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
2e3de 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
2e3df 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61  every VIEW in da
2e3e0 74 61 62 61 73 65 20 69 64 78 2e 0d 0a 2a 2f 0d  tabase idx...*/.
2e3e1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
2e3e2 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28  iteViewResetAll(
2e3e3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
2e3e4 20 69 64 78 29 7b 0d 0a 20 20 48 61 73 68 45 6c   idx){..  HashEl
2e3e5 65 6d 20 2a 69 3b 0d 0a 20 20 61 73 73 65 72 74  em *i;..  assert
2e3e6 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d  ( sqlite3SchemaM
2e3e7 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 64 78  utexHeld(db, idx
2e3e8 2c 20 30 29 20 29 3b 0d 0a 20 20 69 66 28 20 21  , 0) );..  if( !
2e3e9 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62  DbHasProperty(db
2e3ea 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65  , idx, DB_Unrese
2e3eb 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72 6e  tViews) ) return
2e3ec 3b 0d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  ;..  for(i=sqlit
2e3ed 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
2e3ee 61 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61  aDb[idx].pSchema
2e3ef 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d  ->tblHash); i;i=
2e3f0 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
2e3f1 29 29 7b 0d 0a 20 20 20 20 54 61 62 6c 65 20 2a  )){..    Table *
2e3f2 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
2e3f3 68 44 61 74 61 28 69 29 3b 0d 0a 20 20 20 20 69  hData(i);..    i
2e3f4 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
2e3f5 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
2e3f6 65 44 65 6c 65 74 65 43 6f 6c 75 6d 6e 4e 61 6d  eDeleteColumnNam
2e3f7 65 73 28 64 62 2c 20 70 54 61 62 29 3b 0d 0a 20  es(db, pTab);.. 
2e3f8 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 20       pTab->aCol 
2e3f9 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 54 61 62  = 0;..      pTab
2e3fa 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0d 0a 20 20 20  ->nCol = 0;..   
2e3fb 20 7d 0d 0a 20 20 7d 0d 0a 20 20 44 62 43 6c 65   }..  }..  DbCle
2e3fc 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  arProperty(db, i
2e3fd 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69  dx, DB_UnresetVi
2e3fe 65 77 73 29 3b 0d 0a 7d 0d 0a 23 65 6c 73 65 0d  ews);..}..#else.
2e3ff 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
2e400 56 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42  ViewResetAll(A,B
2e401 29 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  )..#endif /* SQL
2e402 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f  ITE_OMIT_VIEW */
2e403 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
2e404 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
2e405 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20 74  ed by the VDBE t
2e406 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e 74  o adjust the int
2e407 65 72 6e 61 6c 20 73 63 68 65 6d 61 0d 0a 2a 2a  ernal schema..**
2e408 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
2e409 77 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c  when the btree l
2e40a 61 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62  ayer moves a tab
2e40b 6c 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68  le root page. Th
2e40c 65 0d 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20  e..** root-page 
2e40d 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e  of a table or in
2e40e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20  dex in database 
2e40f 69 44 62 20 68 61 73 20 63 68 61 6e 67 65 64 20  iDb has changed 
2e410 66 72 6f 6d 20 69 46 72 6f 6d 0d 0a 2a 2a 20 74  from iFrom..** t
2e411 6f 20 69 54 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  o iTo...**..** T
2e412 69 63 6b 65 74 20 23 31 37 32 38 3a 20 20 54 68  icket #1728:  Th
2e413 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 20 6d  e symbol table m
2e414 69 67 68 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61  ight still conta
2e415 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0d 0a  in information..
2e416 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 20 61 6e 64  ** on tables and
2e417 2f 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61 74  /or indices that
2e418 20 61 72 65 20 74 68 65 20 70 72 6f 63 65 73 73   are the process
2e419 20 6f 66 20 62 65 69 6e 67 20 64 65 6c 65 74 65   of being delete
2e41a 64 2e 0d 0a 2a 2a 20 49 66 20 79 6f 75 20 61 72  d...** If you ar
2e41b 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65 20 6f  e unlucky, one o
2e41c 66 20 74 68 6f 73 65 20 64 65 6c 65 74 65 64 20  f those deleted 
2e41d 69 6e 64 69 63 65 73 20 6f 72 20 74 61 62 6c 65  indices or table
2e41e 73 20 6d 69 67 68 74 0d 0a 2a 2a 20 68 61 76 65  s might..** have
2e41f 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 70 61   the same rootpa
2e420 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65  ge number as the
2e421 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 20 69   real table or i
2e422 6e 64 65 78 20 74 68 61 74 20 69 73 0d 0a 2a 2a  ndex that is..**
2e423 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 53   being moved.  S
2e424 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 70  o we cannot stop
2e425 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 72   searching after
2e426 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68   the first match
2e427 20 0d 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68   ..** because th
2e428 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69  e first match mi
2e429 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  ght be for one o
2e42a 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e  f the deleted in
2e42b 64 69 63 65 73 0d 0a 2a 2a 20 6f 72 20 74 61 62  dices..** or tab
2e42c 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20  les and not the 
2e42d 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74  table/index that
2e42e 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
2e42f 6e 67 20 6d 6f 76 65 64 2e 0d 0a 2a 2a 20 57 65  ng moved...** We
2e430 20 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c   must continue l
2e431 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c  ooping until all
2e432 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69   tables and indi
2e433 63 65 73 20 77 69 74 68 0d 0a 2a 2a 20 72 6f 6f  ces with..** roo
2e434 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76  tpage==iFrom hav
2e435 65 20 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64  e been converted
2e436 20 74 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70   to have a rootp
2e437 61 67 65 20 6f 66 20 69 54 6f 0d 0a 2a 2a 20 69  age of iTo..** i
2e438 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 65  n order to be ce
2e439 72 74 61 69 6e 20 74 68 61 74 20 77 65 20 67 6f  rtain that we go
2e43a 74 20 74 68 65 20 72 69 67 68 74 20 6f 6e 65 2e  t the right one.
2e43b 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
2e43c 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
2e43d 43 55 55 4d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  CUUM..SQLITE_PRI
2e43e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2e43f 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 73  3RootPageMoved(s
2e440 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2e441 69 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  iDb, int iFrom, 
2e442 69 6e 74 20 69 54 6f 29 7b 0d 0a 20 20 48 61 73  int iTo){..  Has
2e443 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0d 0a 20  hElem *pElem;.. 
2e444 20 48 61 73 68 20 2a 70 48 61 73 68 3b 0d 0a 20   Hash *pHash;.. 
2e445 20 44 62 20 2a 70 44 62 3b 0d 0a 0d 0a 20 20 61   Db *pDb;....  a
2e446 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
2e447 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
2e448 2c 20 69 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20  , iDb, 0) );..  
2e449 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
2e44a 44 62 5d 3b 0d 0a 20 20 70 48 61 73 68 20 3d 20  Db];..  pHash = 
2e44b 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74  &pDb->pSchema->t
2e44c 62 6c 48 61 73 68 3b 0d 0a 20 20 66 6f 72 28 70  blHash;..  for(p
2e44d 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46  Elem=sqliteHashF
2e44e 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45 6c  irst(pHash); pEl
2e44f 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65  em; pElem=sqlite
2e450 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29  HashNext(pElem))
2e451 7b 0d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  {..    Table *pT
2e452 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
2e453 61 74 61 28 70 45 6c 65 6d 29 3b 0d 0a 20 20 20  ata(pElem);..   
2e454 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d   if( pTab->tnum=
2e455 3d 69 46 72 6f 6d 20 29 7b 0d 0a 20 20 20 20 20  =iFrom ){..     
2e456 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54   pTab->tnum = iT
2e457 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  o;..    }..  }..
2e458 20 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e    pHash = &pDb->
2e459 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
2e45a 3b 0d 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  ;..  for(pElem=s
2e45b 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70  qliteHashFirst(p
2e45c 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45  Hash); pElem; pE
2e45d 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  lem=sqliteHashNe
2e45e 78 74 28 70 45 6c 65 6d 29 29 7b 0d 0a 20 20 20  xt(pElem)){..   
2e45f 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
2e460 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
2e461 6c 65 6d 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  lem);..    if( p
2e462 49 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d  Idx->tnum==iFrom
2e463 20 29 7b 0d 0a 20 20 20 20 20 20 70 49 64 78 2d   ){..      pIdx-
2e464 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b 0d 0a 20 20  >tnum = iTo;..  
2e465 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e    }..  }..}..#en
2e466 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72  dif..../*..** Wr
2e467 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 73  ite code to eras
2e468 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  e the table with
2e469 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c   root-page iTabl
2e46a 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  e from database 
2e46b 69 44 62 2e 0d 0a 2a 2a 20 41 6c 73 6f 20 77 72  iDb...** Also wr
2e46c 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69  ite code to modi
2e46d 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
2e46e 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69  ster table and i
2e46f 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0d 0a  nternal schema..
2e470 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67  ** if a root-pag
2e471 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62  e of another tab
2e472 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74  le is moved by t
2e473 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77  he btree-layer w
2e474 68 69 6c 73 74 0d 0a 2a 2a 20 65 72 61 73 69 6e  hilst..** erasin
2e475 67 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63  g iTable (this c
2e476 61 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61  an happen with a
2e477 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
2e478 74 61 62 61 73 65 29 2e 0d 0a 2a 2f 20 0d 0a 73  tabase)...*/ ..s
2e479 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 72  tatic void destr
2e47a 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 72 73 65  oyRootPage(Parse
2e47b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 54   *pParse, int iT
2e47c 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 29 7b 0d  able, int iDb){.
2e47d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2e47e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2e47f 73 65 29 3b 0d 0a 20 20 69 6e 74 20 72 31 20 3d  se);..  int r1 =
2e480 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2e481 65 67 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 73  eg(pParse);..  s
2e482 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2e483 28 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20  (v, OP_Destroy, 
2e484 69 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29  iTable, r1, iDb)
2e485 3b 0d 0a 20 20 73 71 6c 69 74 65 33 4d 61 79 41  ;..  sqlite3MayA
2e486 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0d 0a 23  bort(pParse);..#
2e487 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e488 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20  IT_AUTOVACUUM.. 
2e489 20 2f 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 73   /* OP_Destroy s
2e48a 74 6f 72 65 73 20 61 6e 20 69 6e 20 69 6e 74 65  tores an in inte
2e48b 67 65 72 20 72 31 2e 20 49 66 20 74 68 69 73 20  ger r1. If this 
2e48c 69 6e 74 65 67 65 72 0d 0a 20 20 2a 2a 20 69 73  integer..  ** is
2e48d 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
2e48e 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  it is the root p
2e48f 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  age number of a 
2e490 74 61 62 6c 65 20 6d 6f 76 65 64 20 74 6f 0d 0a  table moved to..
2e491 20 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 54    ** location iT
2e492 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  able. The follow
2e493 69 6e 67 20 63 6f 64 65 20 6d 6f 64 69 66 69 65  ing code modifie
2e494 73 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  s the sqlite_mas
2e495 74 65 72 20 74 61 62 6c 65 20 74 6f 0d 0a 20 20  ter table to..  
2e496 2a 2a 20 72 65 66 6c 65 63 74 20 74 68 69 73 2e  ** reflect this.
2e497 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65  ..  **..  ** The
2e498 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20 53   "#NNN" in the S
2e499 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  QL is a special 
2e49a 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d 65  constant that me
2e49b 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61 6c  ans whatever val
2e49c 75 65 0d 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72  ue..  ** is in r
2e49d 65 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65  egister NNN.  Se
2e49e 65 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73 20  e grammar rules 
2e49f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2e4a0 74 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52 0d  the TK_REGISTER.
2e4a1 0a 20 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20  .  ** token for 
2e4a2 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2e4a3 6d 61 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20  mation...  */.. 
2e4a4 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2e4a5 72 73 65 28 70 50 61 72 73 65 2c 20 0d 0a 20 20  rse(pParse, ..  
2e4a6 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73     "UPDATE %Q.%s
2e4a7 20 53 45 54 20 72 6f 6f 74 70 61 67 65 3d 25 64   SET rootpage=%d
2e4a8 20 57 48 45 52 45 20 23 25 64 20 41 4e 44 20 72   WHERE #%d AND r
2e4a9 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c 0d 0a 20  ootpage=#%d",.. 
2e4aa 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e      pParse->db->
2e4ab 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
2e4ac 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2e4ad 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20 72  ), iTable, r1, r
2e4ae 31 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73  1);..#endif..  s
2e4af 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2e4b0 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
2e4b1 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57  ;..}..../*..** W
2e4b2 72 69 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rite VDBE code t
2e4b3 6f 20 65 72 61 73 65 20 74 61 62 6c 65 20 70 54  o erase table pT
2e4b4 61 62 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63  ab and all assoc
2e4b5 69 61 74 65 64 20 69 6e 64 69 63 65 73 20 6f 6e  iated indices on
2e4b6 20 64 69 73 6b 2e 0d 0a 2a 2a 20 43 6f 64 65 20   disk...** Code 
2e4b7 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 73 71  to update the sq
2e4b8 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2e4b9 65 73 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20  es and internal 
2e4ba 73 63 68 65 6d 61 20 64 65 66 69 6e 69 74 69 6f  schema definitio
2e4bb 6e 73 0d 0a 2a 2a 20 69 6e 20 63 61 73 65 20 61  ns..** in case a
2e4bc 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f 6e   root-page belon
2e4bd 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72 20  ging to another 
2e4be 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62  table is moved b
2e4bf 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  y the btree laye
2e4c0 72 0d 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64  r..** is also ad
2e4c1 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 61  ded (this can ha
2e4c2 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74  ppen with an aut
2e4c3 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
2e4c4 65 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  e)...*/..static 
2e4c5 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c  void destroyTabl
2e4c6 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2e4c7 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0d 0a   Table *pTab){..
2e4c8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
2e4c9 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20  IT_AUTOVACUUM.. 
2e4ca 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20   Index *pIdx;.. 
2e4cb 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
2e4cc 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2e4cd 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
2e4ce 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 64  ->pSchema);..  d
2e4cf 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70  estroyRootPage(p
2e4d0 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75  Parse, pTab->tnu
2e4d1 6d 2c 20 69 44 62 29 3b 0d 0a 20 20 66 6f 72 28  m, iDb);..  for(
2e4d2 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
2e4d3 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2e4d4 64 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20  dx->pNext){..   
2e4d5 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
2e4d6 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 74  (pParse, pIdx->t
2e4d7 6e 75 6d 2c 20 69 44 62 29 3b 0d 0a 20 20 7d 0d  num, iDb);..  }.
2e4d8 0a 23 65 6c 73 65 0d 0a 20 20 2f 2a 20 49 66 20  .#else..  /* If 
2e4d9 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 79  the database may
2e4da 20 62 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   be auto-vacuum 
2e4db 63 61 70 61 62 6c 65 20 28 69 66 20 53 51 4c 49  capable (if SQLI
2e4dc 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
2e4dd 55 4d 0d 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  UM..  ** is not 
2e4de 64 65 66 69 6e 65 64 29 2c 20 74 68 65 6e 20 69  defined), then i
2e4df 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  t is important t
2e4e0 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f  o call OP_Destro
2e4e1 79 20 6f 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 74  y on the..  ** t
2e4e2 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72  able and index r
2e4e3 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20 6f 72 64  oot-pages in ord
2e4e4 65 72 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74  er, starting wit
2e4e5 68 20 74 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c  h the numericall
2e4e6 79 20 0d 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74  y ..  ** largest
2e4e7 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
2e4e8 72 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65  r. This guarante
2e4e9 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20  es that none of 
2e4ea 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 73 0d 0a  the root-pages..
2e4eb 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 74 72    ** to be destr
2e4ec 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 74 65  oyed is relocate
2e4ed 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 72 20  d by an earlier 
2e4ee 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65 2e  OP_Destroy. i.e.
2e4ef 20 69 66 20 74 68 65 0d 0a 20 20 2a 2a 20 66 6f   if the..  ** fo
2e4f0 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f 64  llowing were cod
2e4f1 65 64 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  ed:..  **..  ** 
2e4f2 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0d 0a  OP_Destroy 4 0..
2e4f3 20 20 2a 2a 20 2e 2e 2e 0d 0a 20 20 2a 2a 20 4f    ** .....  ** O
2e4f4 50 5f 44 65 73 74 72 6f 79 20 35 20 30 0d 0a 20  P_Destroy 5 0.. 
2e4f5 20 2a 2a 0d 0a 20 20 2a 2a 20 61 6e 64 20 72 6f   **..  ** and ro
2e4f6 6f 74 20 70 61 67 65 20 35 20 68 61 70 70 65 6e  ot page 5 happen
2e4f7 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 72  ed to be the lar
2e4f8 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e  gest root-page n
2e4f9 75 6d 62 65 72 20 69 6e 20 74 68 65 0d 0a 20 20  umber in the..  
2e4fa 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ** database, the
2e4fb 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20 77 6f  n root page 5 wo
2e4fc 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74 6f 20  uld be moved to 
2e4fd 70 61 67 65 20 34 20 62 79 20 74 68 65 20 0d 0a  page 4 by the ..
2e4fe 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f 79    ** "OP_Destroy
2e4ff 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54 68   4 0" opcode. Th
2e500 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f 50  e subsequent "OP
2e501 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77 6f  _Destroy 5 0" wo
2e502 75 6c 64 20 68 69 74 0d 0a 20 20 2a 2a 20 61 20  uld hit..  ** a 
2e503 66 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0d  free-list page..
2e504 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 61  .  */..  int iTa
2e505 62 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0d  b = pTab->tnum;.
2e506 0a 20 20 69 6e 74 20 69 44 65 73 74 72 6f 79 65  .  int iDestroye
2e507 64 20 3d 20 30 3b 0d 0a 0d 0a 20 20 77 68 69 6c  d = 0;....  whil
2e508 65 28 20 31 20 29 7b 0d 0a 20 20 20 20 49 6e 64  e( 1 ){..    Ind
2e509 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20 20 20 69  ex *pIdx;..    i
2e50a 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 3b  nt iLargest = 0;
2e50b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69 44 65 73  ....    if( iDes
2e50c 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61  troyed==0 || iTa
2e50d 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0d  b<iDestroyed ){.
2e50e 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20  .      iLargest 
2e50f 3d 20 69 54 61 62 3b 0d 0a 20 20 20 20 7d 0d 0a  = iTab;..    }..
2e510 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
2e511 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
2e512 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2e513 74 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69  t){..      int i
2e514 49 64 78 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d  Idx = pIdx->tnum
2e515 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2e516 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d   pIdx->pSchema==
2e517 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
2e518 0d 0a 20 20 20 20 20 20 69 66 28 20 28 69 44 65  ..      if( (iDe
2e519 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28 69  stroyed==0 || (i
2e51a 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29 29  Idx<iDestroyed))
2e51b 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65 73   && iIdx>iLarges
2e51c 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 4c  t ){..        iL
2e51d 61 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0d 0a  argest = iIdx;..
2e51e 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
2e51f 20 20 20 20 69 66 28 20 69 4c 61 72 67 65 73 74      if( iLargest
2e520 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  ==0 ){..      re
2e521 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  turn;..    }else
2e522 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  {..      int iDb
2e523 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2e524 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2e525 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2e526 61 29 3b 0d 0a 20 20 20 20 20 20 64 65 73 74 72  a);..      destr
2e527 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
2e528 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62  e, iLargest, iDb
2e529 29 3b 0d 0a 20 20 20 20 20 20 69 44 65 73 74 72  );..      iDestr
2e52a 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b  oyed = iLargest;
2e52b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65  ..    }..  }..#e
2e52c 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ndif..}..../*..*
2e52d 2a 20 52 65 6d 6f 76 65 20 65 6e 74 72 69 65 73  * Remove entries
2e52e 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
2e52f 5f 73 74 61 74 4e 20 74 61 62 6c 65 73 20 28 66  _statN tables (f
2e530 6f 72 20 4e 20 69 6e 20 28 31 2c 32 2c 33 29 29  or N in (1,2,3))
2e531 0d 0a 2a 2a 20 61 66 74 65 72 20 61 20 44 52 4f  ..** after a DRO
2e532 50 20 49 4e 44 45 58 20 6f 72 20 44 52 4f 50 20  P INDEX or DROP 
2e533 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0d 0a  TABLE command...
2e534 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
2e535 73 71 6c 69 74 65 33 43 6c 65 61 72 53 74 61 74  sqlite3ClearStat
2e536 54 61 62 6c 65 73 28 0d 0a 20 20 50 61 72 73 65  Tables(..  Parse
2e537 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2e538 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
2e539 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69   context */..  i
2e53a 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
2e53b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2e53c 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0d  abase number */.
2e53d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2e53e 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 69 64  Type,     /* "id
2e53f 78 22 20 6f 72 20 22 74 62 6c 22 20 2a 2f 0d 0a  x" or "tbl" */..
2e540 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2e541 61 6d 65 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame      /* Name
2e542 20 6f 66 20 69 6e 64 65 78 20 6f 72 20 74 61 62   of index or tab
2e543 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  le */..){..  int
2e544 20 69 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   i;..  const cha
2e545 72 20 2a 7a 44 62 4e 61 6d 65 20 3d 20 70 50 61  r *zDbName = pPa
2e546 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
2e547 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 66 6f 72 28  ].zName;..  for(
2e548 69 3d 31 3b 20 69 3c 3d 33 3b 20 69 2b 2b 29 7b  i=1; i<=3; i++){
2e549 0d 0a 20 20 20 20 63 68 61 72 20 7a 54 61 62 5b  ..    char zTab[
2e54a 32 34 5d 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  24];..    sqlite
2e54b 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
2e54c 66 28 7a 54 61 62 29 2c 7a 54 61 62 2c 22 73 71  f(zTab),zTab,"sq
2e54d 6c 69 74 65 5f 73 74 61 74 25 64 22 2c 69 29 3b  lite_stat%d",i);
2e54e 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
2e54f 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73  3FindTable(pPars
2e550 65 2d 3e 64 62 2c 20 7a 54 61 62 2c 20 7a 44 62  e->db, zTab, zDb
2e551 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20 20 20 20  Name) ){..      
2e552 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2e553 73 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20 20  se(pParse,..    
2e554 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
2e555 20 25 51 2e 25 73 20 57 48 45 52 45 20 25 73 3d   %Q.%s WHERE %s=
2e556 25 51 22 2c 0d 0a 20 20 20 20 20 20 20 20 7a 44  %Q",..        zD
2e557 62 4e 61 6d 65 2c 20 7a 54 61 62 2c 20 7a 54 79  bName, zTab, zTy
2e558 70 65 2c 20 7a 4e 61 6d 65 0d 0a 20 20 20 20 20  pe, zName..     
2e559 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   );..    }..  }.
2e55a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e  .}..../*..** Gen
2e55b 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72  erate code to dr
2e55c 6f 70 20 61 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  op a table...*/.
2e55d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e55e 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
2e55f 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20  DropTable(Parse 
2e560 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2e561 70 54 61 62 2c 20 69 6e 74 20 69 44 62 2c 20 69  pTab, int iDb, i
2e562 6e 74 20 69 73 56 69 65 77 29 7b 0d 0a 20 20 56  nt isView){..  V
2e563 64 62 65 20 2a 76 3b 0d 0a 20 20 73 71 6c 69 74  dbe *v;..  sqlit
2e564 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e565 3e 64 62 3b 0d 0a 20 20 54 72 69 67 67 65 72 20  >db;..  Trigger 
2e566 2a 70 54 72 69 67 67 65 72 3b 0d 0a 20 20 44 62  *pTrigger;..  Db
2e567 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
2e568 5b 69 44 62 5d 3b 0d 0a 0d 0a 20 20 76 20 3d 20  [iDb];....  v = 
2e569 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2e56a 50 61 72 73 65 29 3b 0d 0a 20 20 61 73 73 65 72  Parse);..  asser
2e56b 74 28 20 76 21 3d 30 20 29 3b 0d 0a 20 20 73 71  t( v!=0 );..  sq
2e56c 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2e56d 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2e56e 20 31 2c 20 69 44 62 29 3b 0d 0a 0d 0a 23 69 66   1, iDb);....#if
2e56f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2e570 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20  _VIRTUALTABLE.. 
2e571 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2e572 54 61 62 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Tab) ){..    sql
2e573 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2e574 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0d 0a 20  , OP_VBegin);.. 
2e575 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
2e576 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67  /* Drop all trig
2e577 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20  gers associated 
2e578 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62  with the table b
2e579 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f  eing dropped. Co
2e57a 64 65 0d 0a 20 20 2a 2a 20 69 73 20 67 65 6e 65  de..  ** is gene
2e57b 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 20  rated to remove 
2e57c 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71 6c  entries from sql
2e57d 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f  ite_master and/o
2e57e 72 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74  r..  ** sqlite_t
2e57f 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20 72 65  emp_master if re
2e580 71 75 69 72 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20  quired...  */.. 
2e581 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
2e582 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
2e583 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0d 0a 20  Parse, pTab);.. 
2e584 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72   while( pTrigger
2e585 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
2e586 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
2e587 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma==pTab->pSchem
2e588 61 20 7c 7c 20 0d 0a 20 20 20 20 20 20 20 20 70  a || ..        p
2e589 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61  Trigger->pSchema
2e58a 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ==db->aDb[1].pSc
2e58b 68 65 6d 61 20 29 3b 0d 0a 20 20 20 20 73 71 6c  hema );..    sql
2e58c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
2e58d 74 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  tr(pParse, pTrig
2e58e 67 65 72 29 3b 0d 0a 20 20 20 20 70 54 72 69 67  ger);..    pTrig
2e58f 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e  ger = pTrigger->
2e590 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23  pNext;..  }....#
2e591 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e592 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
2e593 0d 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e  ..  /* Remove an
2e594 79 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65  y entries of the
2e595 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
2e596 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65   table associate
2e597 64 20 77 69 74 68 0d 0a 20 20 2a 2a 20 74 68 65  d with..  ** the
2e598 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
2e599 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f  pped. This is do
2e59a 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61  ne before the ta
2e59b 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0d 0a  ble is dropped..
2e59c 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65    ** at the btre
2e59d 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65  e level, in case
2e59e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
2e59f 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73  ence table needs
2e5a0 20 74 6f 0d 0a 20 20 2a 2a 20 6d 6f 76 65 20 61   to..  ** move a
2e5a1 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68  s a result of th
2e5a2 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70  e drop (can happ
2e5a3 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75  en in auto-vacuu
2e5a4 6d 20 6d 6f 64 65 29 2e 0d 0a 20 20 2a 2f 0d 0a  m mode)...  */..
2e5a5 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46    if( pTab->tabF
2e5a6 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
2e5a7 63 72 65 6d 65 6e 74 20 29 7b 0d 0a 20 20 20 20  crement ){..    
2e5a8 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2e5a9 73 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20 20  se(pParse,..    
2e5aa 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
2e5ab 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  Q.sqlite_sequenc
2e5ac 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22  e WHERE name=%Q"
2e5ad 2c 0d 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  ,..      pDb->zN
2e5ae 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2e5af 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 23  ..    );..  }..#
2e5b0 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 44 72  endif....  /* Dr
2e5b1 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d 41  op all SQLITE_MA
2e5b2 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64 20 69  STER table and i
2e5b3 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68 61  ndex entries tha
2e5b4 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 0d 0a  t refer to the..
2e5b5 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20    ** table. The 
2e5b6 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c 6f 6f  program name loo
2e5b7 70 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6d  ps through the m
2e5b8 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
2e5b9 64 65 6c 65 74 65 73 0d 0a 20 20 2a 2a 20 65 76  deletes..  ** ev
2e5ba 65 72 79 20 72 6f 77 20 74 68 61 74 20 72 65 66  ery row that ref
2e5bb 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 6f  ers to a table o
2e5bc 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  f the same name 
2e5bd 61 73 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67  as the one being
2e5be 0d 0a 20 20 2a 2a 20 64 72 6f 70 70 65 64 2e 20  ..  ** dropped. 
2e5bf 54 72 69 67 67 65 72 73 20 61 72 65 20 68 61 6e  Triggers are han
2e5c0 64 6c 65 64 20 73 65 70 65 72 61 74 65 6c 79 20  dled seperately 
2e5c1 62 65 63 61 75 73 65 20 61 20 74 72 69 67 67 65  because a trigge
2e5c2 72 20 63 61 6e 20 62 65 0d 0a 20 20 2a 2a 20 63  r can be..  ** c
2e5c3 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 74 65  reated in the te
2e5c4 6d 70 20 64 61 74 61 62 61 73 65 20 74 68 61 74  mp database that
2e5c5 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62   refers to a tab
2e5c6 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 0d 0a 20  le in another.. 
2e5c7 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 0d 0a 20   ** database... 
2e5c8 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 4e 65   */..  sqlite3Ne
2e5c9 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
2e5ca 2c 20 0d 0a 20 20 20 20 20 20 22 44 45 4c 45 54  , ..      "DELET
2e5cb 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45  E FROM %Q.%s WHE
2e5cc 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61  RE tbl_name=%Q a
2e5cd 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 65  nd type!='trigge
2e5ce 72 27 22 2c 0d 0a 20 20 20 20 20 20 70 44 62 2d  r'",..      pDb-
2e5cf 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  >zName, SCHEMA_T
2e5d0 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d  ABLE(iDb), pTab-
2e5d1 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28 20  >zName);..  if( 
2e5d2 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69  !isView && !IsVi
2e5d3 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a  rtual(pTab) ){..
2e5d4 20 20 20 20 64 65 73 74 72 6f 79 54 61 62 6c 65      destroyTable
2e5d5 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0d  (pParse, pTab);.
2e5d6 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6d  .  }....  /* Rem
2e5d7 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e  ove the table en
2e5d8 74 72 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27  try from SQLite'
2e5d9 73 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  s internal schem
2e5da 61 20 61 6e 64 20 6d 6f 64 69 66 79 0d 0a 20 20  a and modify..  
2e5db 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  ** the schema co
2e5dc 6f 6b 69 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  okie...  */..  i
2e5dd 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2e5de 62 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  b) ){..    sqlit
2e5df 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2e5e0 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62  OP_VDestroy, iDb
2e5e1 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  , 0, 0, pTab->zN
2e5e2 61 6d 65 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20  ame, 0);..  }.. 
2e5e3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e5e4 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62  p4(v, OP_DropTab
2e5e5 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  le, iDb, 0, 0, p
2e5e6 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0d  Tab->zName, 0);.
2e5e7 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  .  sqlite3Change
2e5e8 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2e5e9 44 62 29 3b 0d 0a 20 20 73 71 6c 69 74 65 56 69  Db);..  sqliteVi
2e5ea 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69  ewResetAll(db, i
2e5eb 44 62 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  Db);..}..../*..*
2e5ec 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2e5ed 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20 74  s called to do t
2e5ee 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52 4f  he work of a DRO
2e5ef 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  P TABLE statemen
2e5f0 74 2e 0d 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20  t...** pName is 
2e5f1 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
2e5f2 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70  table to be drop
2e5f3 70 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ped...*/..SQLITE
2e5f4 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2e5f5 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50  lite3DropTable(P
2e5f6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
2e5f7 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e  cList *pName, in
2e5f8 74 20 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f  t isView, int no
2e5f9 45 72 72 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a  Err){..  Table *
2e5fa 70 54 61 62 3b 0d 0a 20 20 56 64 62 65 20 2a 76  pTab;..  Vdbe *v
2e5fb 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
2e5fc 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a   = pParse->db;..
2e5fd 20 20 69 6e 74 20 69 44 62 3b 0d 0a 0d 0a 20 20    int iDb;....  
2e5fe 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2e5ff 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 67 6f 74  iled ){..    got
2e600 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
2e601 65 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  e;..  }..  asser
2e602 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
2e603 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
2e604 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
2e605 29 3b 0d 0a 20 20 69 66 28 20 6e 6f 45 72 72 20  );..  if( noErr 
2e606 29 20 64 62 2d 3e 73 75 70 70 72 65 73 73 45 72  ) db->suppressEr
2e607 72 2b 2b 3b 0d 0a 20 20 70 54 61 62 20 3d 20 73  r++;..  pTab = s
2e608 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
2e609 65 28 70 50 61 72 73 65 2c 20 69 73 56 69 65 77  e(pParse, isView
2e60a 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
2e60b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e60c 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
2e60d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  e, pName->a[0].z
2e60e 44 61 74 61 62 61 73 65 29 3b 0d 0a 20 20 69 66  Database);..  if
2e60f 28 20 6e 6f 45 72 72 20 29 20 64 62 2d 3e 73 75  ( noErr ) db->su
2e610 70 70 72 65 73 73 45 72 72 2d 2d 3b 0d 0a 0d 0a  ppressErr--;....
2e611 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b    if( pTab==0 ){
2e612 0d 0a 20 20 20 20 69 66 28 20 6e 6f 45 72 72 20  ..    if( noErr 
2e613 29 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72  ) sqlite3CodeVer
2e614 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 70  ifyNamedSchema(p
2e615 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  Parse, pName->a[
2e616 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0d 0a  0].zDatabase);..
2e617 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2e618 6f 70 5f 74 61 62 6c 65 3b 0d 0a 20 20 7d 0d 0a  op_table;..  }..
2e619 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2e61a 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2e61b 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2e61c 0d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ..  assert( iDb>
2e61d 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
2e61e 62 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  b );....  /* If 
2e61f 70 54 61 62 20 69 73 20 61 20 76 69 72 74 75 61  pTab is a virtua
2e620 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69  l table, call Vi
2e621 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
2e622 28 29 20 74 6f 20 65 6e 73 75 72 65 0d 0a 20 20  () to ensure..  
2e623 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c  ** it is initial
2e624 69 7a 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ized...  */..  i
2e625 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2e626 62 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 65  b) && sqlite3Vie
2e627 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2e628 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
2e629 0d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ..    goto exit_
2e62a 64 72 6f 70 5f 74 61 62 6c 65 3b 0d 0a 20 20 7d  drop_table;..  }
2e62b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2e62c 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2e62d 49 4f 4e 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 6e  ION..  {..    in
2e62e 74 20 63 6f 64 65 3b 0d 0a 20 20 20 20 63 6f 6e  t code;..    con
2e62f 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
2e630 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2e631 29 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  );..    const ch
2e632 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
2e633 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  b[iDb].zName;.. 
2e634 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2e635 41 72 67 32 20 3d 20 30 3b 0d 0a 20 20 20 20 69  Arg2 = 0;..    i
2e636 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
2e637 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2e638 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c  TE_DELETE, zTab,
2e639 20 30 2c 20 7a 44 62 29 29 7b 0d 0a 20 20 20 20   0, zDb)){..    
2e63a 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2e63b 5f 74 61 62 6c 65 3b 0d 0a 20 20 20 20 7d 0d 0a  _table;..    }..
2e63c 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29      if( isView )
2e63d 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  {..      if( !OM
2e63e 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
2e63f 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==1 ){..        
2e640 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2e641 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0d 0a 20  OP_TEMP_VIEW;.. 
2e642 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
2e643 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
2e644 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0d 0a 20  TE_DROP_VIEW;.. 
2e645 20 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20       }..#ifndef 
2e646 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2e647 55 41 4c 54 41 42 4c 45 0d 0a 20 20 20 20 7d 65  UALTABLE..    }e
2e648 6c 73 65 20 69 66 28 20 49 73 56 69 72 74 75 61  lse if( IsVirtua
2e649 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20 20 20 20  l(pTab) ){..    
2e64a 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
2e64b 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0d 0a 20 20  DROP_VTABLE;..  
2e64c 20 20 20 20 7a 41 72 67 32 20 3d 20 73 71 6c 69      zArg2 = sqli
2e64d 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
2e64e 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e   pTab)->pMod->zN
2e64f 61 6d 65 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  ame;..#endif..  
2e650 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
2e651 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
2e652 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0d 0a 20   && iDb==1 ){.. 
2e653 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
2e654 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54  LITE_DROP_TEMP_T
2e655 41 42 4c 45 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ABLE;..      }el
2e656 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 64  se{..        cod
2e657 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
2e658 54 41 42 4c 45 3b 0d 0a 20 20 20 20 20 20 7d 0d  TABLE;..      }.
2e659 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
2e65a 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2e65b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70  (pParse, code, p
2e65c 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41 72 67  Tab->zName, zArg
2e65d 32 2c 20 7a 44 62 29 20 29 7b 0d 0a 20 20 20 20  2, zDb) ){..    
2e65e 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2e65f 5f 74 61 62 6c 65 3b 0d 0a 20 20 20 20 7d 0d 0a  _table;..    }..
2e660 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2e661 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2e662 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20   SQLITE_DELETE, 
2e663 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20  pTab->zName, 0, 
2e664 7a 44 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 67  zDb) ){..      g
2e665 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
2e666 62 6c 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ble;..    }..  }
2e667 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 69 66 28 20  ..#endif..  if( 
2e668 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2e669 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
2e66a 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 0d 0a  lite_", 7)==0 ..
2e66b 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53 74      && sqlite3St
2e66c 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61  rNICmp(pTab->zNa
2e66d 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74  me, "sqlite_stat
2e66e 22 2c 20 31 31 29 21 3d 30 20 29 7b 0d 0a 20 20  ", 11)!=0 ){..  
2e66f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2e670 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65  g(pParse, "table
2e671 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64   %s may not be d
2e672 72 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a  ropped", pTab->z
2e673 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67 6f 74 6f  Name);..    goto
2e674 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
2e675 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65  ;..  }....#ifnde
2e676 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2e677 45 57 0d 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20  EW..  /* Ensure 
2e678 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6e 6f  DROP TABLE is no
2e679 74 20 75 73 65 64 20 6f 6e 20 61 20 76 69 65 77  t used on a view
2e67a 2c 20 61 6e 64 20 44 52 4f 50 20 56 49 45 57 20  , and DROP VIEW 
2e67b 69 73 20 6e 6f 74 20 75 73 65 64 0d 0a 20 20 2a  is not used..  *
2e67c 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e 0d 0a 20  * on a table... 
2e67d 20 2a 2f 0d 0a 20 20 69 66 28 20 69 73 56 69 65   */..  if( isVie
2e67e 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65  w && pTab->pSele
2e67f 63 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  ct==0 ){..    sq
2e680 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2e681 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20  arse, "use DROP 
2e682 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20  TABLE to delete 
2e683 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d  table %s", pTab-
2e684 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67 6f  >zName);..    go
2e685 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
2e686 6c 65 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  le;..  }..  if( 
2e687 21 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d  !isView && pTab-
2e688 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20  >pSelect ){..   
2e689 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2e68a 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52  (pParse, "use DR
2e68b 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65 74  OP VIEW to delet
2e68c 65 20 76 69 65 77 20 25 73 22 2c 20 70 54 61 62  e view %s", pTab
2e68d 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 67  ->zName);..    g
2e68e 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
2e68f 62 6c 65 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  ble;..  }..#endi
2e690 66 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61  f....  /* Genera
2e691 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  te code to remov
2e692 65 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  e the table from
2e693 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
2e694 65 0d 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e  e..  ** on disk.
2e695 0d 0a 20 20 2a 2f 0d 0a 20 20 76 20 3d 20 73 71  ..  */..  v = sq
2e696 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2e697 72 73 65 29 3b 0d 0a 20 20 69 66 28 20 76 20 29  rse);..  if( v )
2e698 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  {..    sqlite3Be
2e699 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2e69a 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
2e69b 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 43  );..    sqlite3C
2e69c 6c 65 61 72 53 74 61 74 54 61 62 6c 65 73 28 70  learStatTables(p
2e69d 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 62 6c  Parse, iDb, "tbl
2e69e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
2e69f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b 44  ..    sqlite3FkD
2e6a0 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ropTable(pParse,
2e6a1 20 70 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0d 0a   pName, pTab);..
2e6a2 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 44      sqlite3CodeD
2e6a3 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ropTable(pParse,
2e6a4 20 70 54 61 62 2c 20 69 44 62 2c 20 69 73 56 69   pTab, iDb, isVi
2e6a5 65 77 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 65 78 69  ew);..  }....exi
2e6a6 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0d 0a 20  t_drop_table:.. 
2e6a7 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
2e6a8 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29  elete(db, pName)
2e6a9 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
2e6aa 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2e6ab 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20  alled to create 
2e6ac 61 20 6e 65 77 20 66 6f 72 65 69 67 6e 20 6b 65  a new foreign ke
2e6ad 79 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 0d 0a  y on the table..
2e6ae 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  ** currently und
2e6af 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
2e6b0 20 20 70 46 72 6f 6d 43 6f 6c 20 64 65 74 65 72    pFromCol deter
2e6b1 6d 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6c 75  mines which colu
2e6b2 6d 6e 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 63  mns..** in the c
2e6b3 75 72 72 65 6e 74 20 74 61 62 6c 65 20 70 6f 69  urrent table poi
2e6b4 6e 74 20 74 6f 20 74 68 65 20 66 6f 72 65 69 67  nt to the foreig
2e6b5 6e 20 6b 65 79 2e 20 20 49 66 20 70 46 72 6f 6d  n key.  If pFrom
2e6b6 43 6f 6c 3d 3d 30 20 74 68 65 6e 0d 0a 2a 2a 20  Col==0 then..** 
2e6b7 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65 79 20  connect the key 
2e6b8 74 6f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  to the last colu
2e6b9 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20 70 54  mn inserted.  pT
2e6ba 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  o is the name of
2e6bb 0d 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 72  ..** the table r
2e6bc 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70 54 6f  eferred to.  pTo
2e6bd 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f 66  Col is a list of
2e6be 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6f   tables in the o
2e6bf 74 68 65 72 0d 0a 2a 2a 20 70 54 6f 20 74 61 62  ther..** pTo tab
2e6c0 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65  le that the fore
2e6c1 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20 74  ign key points t
2e6c2 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69  o.  flags contai
2e6c3 6e 73 20 61 6c 6c 0d 0a 2a 2a 20 69 6e 66 6f 72  ns all..** infor
2e6c4 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
2e6c5 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
2e6c6 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20  tion algorithms 
2e6c7 73 70 65 63 69 66 69 65 64 0d 0a 2a 2a 20 69 6e  specified..** in
2e6c8 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20   the ON DELETE, 
2e6c9 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e  ON UPDATE and ON
2e6ca 20 49 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e   INSERT clauses.
2e6cb 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 46 4b 65 79  ..**..** An FKey
2e6cc 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
2e6cd 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64 20  eated and added 
2e6ce 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
2e6cf 72 65 6e 74 6c 79 0d 0a 2a 2a 20 75 6e 64 65 72  rently..** under
2e6d0 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e   construction in
2e6d1 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   the pParse->pNe
2e6d2 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 0d 0a 2a  wTable field...*
2e6d3 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67  *..** The foreig
2e6d4 6e 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72  n key is set for
2e6d5 20 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65   IMMEDIATE proce
2e6d6 73 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71  ssing.  A subseq
2e6d7 75 65 6e 74 20 63 61 6c 6c 0d 0a 2a 2a 20 74 6f  uent call..** to
2e6d8 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72   sqlite3DeferFor
2e6d9 65 69 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20  eignKey() might 
2e6da 63 68 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44  change this to D
2e6db 45 46 45 52 52 45 44 2e 0d 0a 2a 2f 0d 0a 53 51  EFERRED...*/..SQ
2e6dc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e6dd 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  d sqlite3CreateF
2e6de 6f 72 65 69 67 6e 4b 65 79 28 0d 0a 20 20 50 61  oreignKey(..  Pa
2e6df 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2e6e0 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2e6e1 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78 70 72  ntext */..  Expr
2e6e2 4c 69 73 74 20 2a 70 46 72 6f 6d 43 6f 6c 2c 20  List *pFromCol, 
2e6e3 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 74   /* Columns in t
2e6e4 68 69 73 20 74 61 62 6c 65 20 74 68 61 74 20 70  his table that p
2e6e5 6f 69 6e 74 20 74 6f 20 6f 74 68 65 72 20 74 61  oint to other ta
2e6e6 62 6c 65 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20  ble */..  Token 
2e6e7 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 20 20 2f  *pTo,          /
2e6e8 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 74  * Name of the ot
2e6e9 68 65 72 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  her table */..  
2e6ea 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 6f 6c  ExprList *pToCol
2e6eb 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20  ,    /* Columns 
2e6ec 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62  in the other tab
2e6ed 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 66 6c 61  le */..  int fla
2e6ee 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs            /*
2e6ef 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   Conflict resolu
2e6f0 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 2e  tion algorithms.
2e6f1 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74   */..){..  sqlit
2e6f2 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e6f3 3e 64 62 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  >db;..#ifndef SQ
2e6f4 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
2e6f5 4e 5f 4b 45 59 0d 0a 20 20 46 4b 65 79 20 2a 70  N_KEY..  FKey *p
2e6f6 46 4b 65 79 20 3d 20 30 3b 0d 0a 20 20 46 4b 65  FKey = 0;..  FKe
2e6f7 79 20 2a 70 4e 65 78 74 54 6f 3b 0d 0a 20 20 54  y *pNextTo;..  T
2e6f8 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65  able *p = pParse
2e6f9 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0d 0a 20 20  ->pNewTable;..  
2e6fa 69 6e 74 20 6e 42 79 74 65 3b 0d 0a 20 20 69 6e  int nByte;..  in
2e6fb 74 20 69 3b 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c  t i;..  int nCol
2e6fc 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 0d  ;..  char *z;...
2e6fd 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 21 3d  .  assert( pTo!=
2e6fe 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30  0 );..  if( p==0
2e6ff 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
2e700 54 41 42 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e  TAB ) goto fk_en
2e701 64 3b 0d 0a 20 20 69 66 28 20 70 46 72 6f 6d 43  d;..  if( pFromC
2e702 6f 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e  ol==0 ){..    in
2e703 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 6f 6c  t iCol = p->nCol
2e704 2d 31 3b 0d 0a 20 20 20 20 69 66 28 20 4e 45 56  -1;..    if( NEV
2e705 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f 74  ER(iCol<0) ) got
2e706 6f 20 66 6b 5f 65 6e 64 3b 0d 0a 20 20 20 20 69  o fk_end;..    i
2e707 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
2e708 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
2e709 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2e70a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2e70b 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20  "foreign key on 
2e70c 25 73 22 0d 0a 20 20 20 20 20 20 20 20 20 22 20  %s"..         " 
2e70d 73 68 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65  should reference
2e70e 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e   only one column
2e70f 20 6f 66 20 74 61 62 6c 65 20 25 54 22 2c 0d 0a   of table %T",..
2e710 20 20 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c           p->aCol
2e711 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54  [iCol].zName, pT
2e712 6f 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20  o);..      goto 
2e713 66 6b 5f 65 6e 64 3b 0d 0a 20 20 20 20 7d 0d 0a  fk_end;..    }..
2e714 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0d 0a 20      nCol = 1;.. 
2e715 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f   }else if( pToCo
2e716 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78  l && pToCol->nEx
2e717 70 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45  pr!=pFromCol->nE
2e718 78 70 72 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  xpr ){..    sqli
2e719 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2e71a 73 65 2c 0d 0a 20 20 20 20 20 20 20 20 22 6e 75  se,..        "nu
2e71b 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2e71c 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
2e71d 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
2e71e 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 0d 0a 20  e number of ".. 
2e71f 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73 20         "columns 
2e720 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  in the reference
2e721 64 20 74 61 62 6c 65 22 29 3b 0d 0a 20 20 20 20  d table");..    
2e722 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0d 0a 20 20  goto fk_end;..  
2e723 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6e 43 6f 6c  }else{..    nCol
2e724 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78   = pFromCol->nEx
2e725 70 72 3b 0d 0a 20 20 7d 0d 0a 20 20 6e 42 79 74  pr;..  }..  nByt
2e726 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65  e = sizeof(*pFKe
2e727 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69  y) + (nCol-1)*si
2e728 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f 6c  zeof(pFKey->aCol
2e729 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b 20  [0]) + pTo->n + 
2e72a 31 3b 0d 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c  1;..  if( pToCol
2e72b 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   ){..    for(i=0
2e72c 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70  ; i<pToCol->nExp
2e72d 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  r; i++){..      
2e72e 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33  nByte += sqlite3
2e72f 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d  Strlen30(pToCol-
2e730 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31  >a[i].zName) + 1
2e731 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
2e732 20 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   pFKey = sqlite3
2e733 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2e734 20 6e 42 79 74 65 20 29 3b 0d 0a 20 20 69 66 28   nByte );..  if(
2e735 20 70 46 4b 65 79 3d 3d 30 20 29 7b 0d 0a 20 20   pFKey==0 ){..  
2e736 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0d 0a    goto fk_end;..
2e737 20 20 7d 0d 0a 20 20 70 46 4b 65 79 2d 3e 70 46    }..  pFKey->pF
2e738 72 6f 6d 20 3d 20 70 3b 0d 0a 20 20 70 46 4b 65  rom = p;..  pFKe
2e739 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70  y->pNextFrom = p
2e73a 2d 3e 70 46 4b 65 79 3b 0d 0a 20 20 7a 20 3d 20  ->pFKey;..  z = 
2e73b 28 63 68 61 72 2a 29 26 70 46 4b 65 79 2d 3e 61  (char*)&pFKey->a
2e73c 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0d 0a 20 20 70 46  Col[nCol];..  pF
2e73d 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0d 0a 20  Key->zTo = z;.. 
2e73e 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e   memcpy(z, pTo->
2e73f 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0d 0a 20 20 7a  z, pTo->n);..  z
2e740 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0d 0a 20  [pTo->n] = 0;.. 
2e741 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28   sqlite3Dequote(
2e742 7a 29 3b 0d 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d  z);..  z += pTo-
2e743 3e 6e 2b 31 3b 0d 0a 20 20 70 46 4b 65 79 2d 3e  >n+1;..  pFKey->
2e744 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0d 0a 20 20  nCol = nCol;..  
2e745 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20  if( pFromCol==0 
2e746 29 7b 0d 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61  ){..    pFKey->a
2e747 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70  Col[0].iFrom = p
2e748 2d 3e 6e 43 6f 6c 2d 31 3b 0d 0a 20 20 7d 65 6c  ->nCol-1;..  }el
2e749 73 65 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30  se{..    for(i=0
2e74a 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d  ; i<nCol; i++){.
2e74b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20  .      int j;.. 
2e74c 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
2e74d 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0d 0a  p->nCol; j++){..
2e74e 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2e74f 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e 61 43  te3StrICmp(p->aC
2e750 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46 72  ol[j].zName, pFr
2e751 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  omCol->a[i].zNam
2e752 65 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  e)==0 ){..      
2e753 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b      pFKey->aCol[
2e754 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0d 0a 20  i].iFrom = j;.. 
2e755 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d           break;.
2e756 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
2e757 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6a    }..      if( j
2e758 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0d 0a 20 20  >=p->nCol ){..  
2e759 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2e75a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0d 0a  orMsg(pParse, ..
2e75b 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f            "unkno
2e75c 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22  wn column \"%s\"
2e75d 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20   in foreign key 
2e75e 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0d 0a 20  definition", .. 
2e75f 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f           pFromCo
2e760 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0d  l->a[i].zName);.
2e761 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b  .        goto fk
2e762 5f 65 6e 64 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  _end;..      }..
2e763 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
2e764 28 20 70 54 6f 43 6f 6c 20 29 7b 0d 0a 20 20 20  ( pToCol ){..   
2e765 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
2e766 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69  ; i++){..      i
2e767 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
2e768 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61  rlen30(pToCol->a
2e769 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20  [i].zName);..   
2e76a 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69     pFKey->aCol[i
2e76b 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0d 0a 20 20 20  ].zCol = z;..   
2e76c 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f     memcpy(z, pTo
2e76d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c  Col->a[i].zName,
2e76e 20 6e 29 3b 0d 0a 20 20 20 20 20 20 7a 5b 6e 5d   n);..      z[n]
2e76f 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7a 20 2b   = 0;..      z +
2e770 3d 20 6e 2b 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  = n+1;..    }.. 
2e771 20 7d 0d 0a 20 20 70 46 4b 65 79 2d 3e 69 73 44   }..  pFKey->isD
2e772 65 66 65 72 72 65 64 20 3d 20 30 3b 0d 0a 20 20  eferred = 0;..  
2e773 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 30  pFKey->aAction[0
2e774 5d 20 3d 20 28 75 38 29 28 66 6c 61 67 73 20 26  ] = (u8)(flags &
2e775 20 30 78 66 66 29 3b 20 20 20 20 20 20 20 20 20   0xff);         
2e776 20 20 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20     /* ON DELETE 
2e777 61 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 70 46 4b  action */..  pFK
2e778 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 3d  ey->aAction[1] =
2e779 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20   (u8)((flags >> 
2e77a 38 20 29 20 26 20 30 78 66 66 29 3b 20 20 20 20  8 ) & 0xff);    
2e77b 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74  /* ON UPDATE act
2e77c 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65  ion */....  asse
2e77d 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
2e77e 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30  aMutexHeld(db, 0
2e77f 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b  , p->pSchema) );
2e780 0d 0a 20 20 70 4e 65 78 74 54 6f 20 3d 20 28 46  ..  pNextTo = (F
2e781 4b 65 79 20 2a 29 73 71 6c 69 74 65 33 48 61 73  Key *)sqlite3Has
2e782 68 49 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68  hInsert(&p->pSch
2e783 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0d  ema->fkeyHash, .
2e784 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 7a 54  .      pFKey->zT
2e785 6f 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  o, sqlite3Strlen
2e786 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f 29 2c 20  30(pFKey->zTo), 
2e787 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 0d 0a 20  (void *)pFKey.. 
2e788 20 29 3b 0d 0a 20 20 69 66 28 20 70 4e 65 78 74   );..  if( pNext
2e789 54 6f 3d 3d 70 46 4b 65 79 20 29 7b 0d 0a 20 20  To==pFKey ){..  
2e78a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2e78b 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 67 6f 74  ed = 1;..    got
2e78c 6f 20 66 6b 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a  o fk_end;..  }..
2e78d 20 20 69 66 28 20 70 4e 65 78 74 54 6f 20 29 7b    if( pNextTo ){
2e78e 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ..    assert( pN
2e78f 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 3d 3d  extTo->pPrevTo==
2e790 30 20 29 3b 0d 0a 20 20 20 20 70 46 4b 65 79 2d  0 );..    pFKey-
2e791 3e 70 4e 65 78 74 54 6f 20 3d 20 70 4e 65 78 74  >pNextTo = pNext
2e792 54 6f 3b 0d 0a 20 20 20 20 70 4e 65 78 74 54 6f  To;..    pNextTo
2e793 2d 3e 70 50 72 65 76 54 6f 20 3d 20 70 46 4b 65  ->pPrevTo = pFKe
2e794 79 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  y;..  }....  /* 
2e795 4c 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e  Link the foreign
2e796 20 6b 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c   key to the tabl
2e797 65 20 61 73 20 74 68 65 20 6c 61 73 74 20 73 74  e as the last st
2e798 65 70 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 2d 3e  ep...  */..  p->
2e799 70 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b 0d 0a  pFKey = pFKey;..
2e79a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0d 0a 0d 0a    pFKey = 0;....
2e79b 66 6b 5f 65 6e 64 3a 0d 0a 20 20 73 71 6c 69 74  fk_end:..  sqlit
2e79c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b  e3DbFree(db, pFK
2e79d 65 79 29 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ey);..#endif /* 
2e79e 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2e79f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
2e7a0 29 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 45  ) */..  sqlite3E
2e7a1 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2e7a2 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b 0d 0a 20 20  , pFromCol);..  
2e7a3 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2e7a4 65 6c 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c  elete(db, pToCol
2e7a5 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
2e7a6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2e7a7 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49  called when an I
2e7a8 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41  NITIALLY IMMEDIA
2e7a9 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20  TE or INITIALLY 
2e7aa 44 45 46 45 52 52 45 44 0d 0a 2a 2a 20 63 6c 61  DEFERRED..** cla
2e7ab 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70  use is seen as p
2e7ac 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e  art of a foreign
2e7ad 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e   key definition.
2e7ae 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64    The isDeferred
2e7af 0d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  ..** parameter i
2e7b0 73 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c  s 1 for INITIALL
2e7b1 59 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30  Y DEFERRED and 0
2e7b2 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49   for INITIALLY I
2e7b3 4d 4d 45 44 49 41 54 45 2e 0d 0a 2a 2a 20 54 68  MMEDIATE...** Th
2e7b4 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68  e behavior of th
2e7b5 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
2e7b6 63 72 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20  created foreign 
2e7b7 6b 65 79 20 69 73 20 61 64 6a 75 73 74 65 64 0d  key is adjusted.
2e7b8 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e  .** accordingly.
2e7b9 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2e7ba 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2e7bb 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79  3DeferForeignKey
2e7bc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2e7bd 69 6e 74 20 69 73 44 65 66 65 72 72 65 64 29 7b  int isDeferred){
2e7be 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2e7bf 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
2e7c0 59 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  Y..  Table *pTab
2e7c1 3b 0d 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  ;..  FKey *pFKey
2e7c2 3b 0d 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d  ;..  if( (pTab =
2e7c3 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2e7c4 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79  le)==0 || (pFKey
2e7c5 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d   = pTab->pFKey)=
2e7c6 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  =0 ) return;..  
2e7c7 61 73 73 65 72 74 28 20 69 73 44 65 66 65 72 72  assert( isDeferr
2e7c8 65 64 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65 72  ed==0 || isDefer
2e7c9 72 65 64 3d 3d 31 20 29 3b 20 2f 2a 20 45 56 3a  red==1 ); /* EV:
2e7ca 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20 2a   R-30323-21917 *
2e7cb 2f 0d 0a 20 20 70 46 4b 65 79 2d 3e 69 73 44 65  /..  pFKey->isDe
2e7cc 66 65 72 72 65 64 20 3d 20 28 75 38 29 69 73 44  ferred = (u8)isD
2e7cd 65 66 65 72 72 65 64 3b 0d 0a 23 65 6e 64 69 66  eferred;..#endif
2e7ce 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  ..}..../*..** Ge
2e7cf 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2e7d0 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 20   will erase and 
2e7d1 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 49  refill index *pI
2e7d2 64 78 2e 20 20 54 68 69 73 20 69 73 0d 0a 2a 2a  dx.  This is..**
2e7d3 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c   used to initial
2e7d4 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61  ize a newly crea
2e7d5 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20  ted index or to 
2e7d6 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0d 0a 2a  recompute the..*
2e7d7 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20  * content of an 
2e7d8 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  index in respons
2e7d9 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63  e to a REINDEX c
2e7da 6f 6d 6d 61 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ommand...**..** 
2e7db 69 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69  if memRootPage i
2e7dc 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20  s not negative, 
2e7dd 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
2e7de 65 20 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79  e index is newly
2e7df 0d 0a 2a 2a 20 63 72 65 61 74 65 64 2e 20 20 54  ..** created.  T
2e7e0 68 65 20 72 65 67 69 73 74 65 72 20 73 70 65 63  he register spec
2e7e1 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74  ified by memRoot
2e7e2 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Page contains th
2e7e3 65 0d 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20  e..** root page 
2e7e4 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e  number of the in
2e7e5 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f 74  dex.  If memRoot
2e7e6 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65  Page is negative
2e7e7 2c 20 74 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 69  , then..** the i
2e7e8 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69  ndex already exi
2e7e9 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20  sts and must be 
2e7ea 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 62  cleared before b
2e7eb 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e  eing refilled an
2e7ec 64 0d 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70  d..** the root p
2e7ed 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
2e7ee 65 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e  e index is taken
2e7ef 20 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e   from pIndex->tn
2e7f0 75 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  um...*/..static 
2e7f1 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 66 69  void sqlite3Refi
2e7f2 6c 6c 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70  llIndex(Parse *p
2e7f3 50 61 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49  Parse, Index *pI
2e7f4 6e 64 65 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f  ndex, int memRoo
2e7f5 74 50 61 67 65 29 7b 0d 0a 20 20 54 61 62 6c 65  tPage){..  Table
2e7f6 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78 2d   *pTab = pIndex-
2e7f7 3e 70 54 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65  >pTable;  /* The
2e7f8 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 69   table that is i
2e7f9 6e 64 65 78 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  ndexed */..  int
2e7fa 20 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e   iTab = pParse->
2e7fb 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42  nTab++;     /* B
2e7fc 74 72 65 65 20 63 75 72 73 6f 72 20 75 73 65 64  tree cursor used
2e7fd 20 66 6f 72 20 70 54 61 62 20 2a 2f 0d 0a 20 20   for pTab */..  
2e7fe 69 6e 74 20 69 49 64 78 20 3d 20 70 50 61 72 73  int iIdx = pPars
2e7ff 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f  e->nTab++;     /
2e800 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 20 75  * Btree cursor u
2e801 73 65 64 20 66 6f 72 20 70 49 6e 64 65 78 20 2a  sed for pIndex *
2e802 2f 0d 0a 20 20 69 6e 74 20 69 53 6f 72 74 65 72  /..  int iSorter
2e803 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e804 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70      /* Cursor op
2e805 65 6e 65 64 20 62 79 20 4f 70 65 6e 53 6f 72 74  ened by OpenSort
2e806 65 72 20 28 69 66 20 69 6e 20 75 73 65 29 20 2a  er (if in use) *
2e807 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 20  /..  int addr1; 
2e808 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e809 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2e80a 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
2e80b 0d 0a 20 20 69 6e 74 20 61 64 64 72 32 3b 20 20  ..  int addr2;  
2e80c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e80d 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 74 6f     /* Address to
2e80e 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 6e 65 78   jump to for nex
2e80f 74 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0d 0a  t iteration */..
2e810 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20    int tnum;     
2e811 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e812 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
2e813 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 56 64 62   index */..  Vdb
2e814 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2e815 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
2e816 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
2e817 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  o this virtual m
2e818 61 63 68 69 6e 65 20 2a 2f 0d 0a 20 20 4b 65 79  achine */..  Key
2e819 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20  Info *pKey;     
2e81a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b              /* K
2e81b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65 78  eyInfo for index
2e81c 20 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
2e81d 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f  TE_OMIT_MERGE_SO
2e81e 52 54 0d 0a 20 20 69 6e 74 20 72 65 67 49 64 78  RT..  int regIdx
2e81f 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
2e820 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2e821 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  s containing the
2e822 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0d 0a 23   index key */..#
2e823 65 6e 64 69 66 0d 0a 20 20 69 6e 74 20 72 65 67  endif..  int reg
2e824 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 20  Record;         
2e825 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2e826 74 65 72 20 68 6f 6c 64 69 6e 67 20 61 73 73 65  ter holding asse
2e827 6d 62 6c 69 65 64 20 69 6e 64 65 78 20 72 65 63  mblied index rec
2e828 6f 72 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ord */..  sqlite
2e829 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2e82a 64 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  db;      /* The 
2e82b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e82c 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44  ion */..  int iD
2e82d 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2e82e 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e  aToIndex(db, pIn
2e82f 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a  dex->pSchema);..
2e830 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2e831 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2e832 49 4f 4e 0d 0a 20 20 69 66 28 20 73 71 6c 69 74  ION..  if( sqlit
2e833 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
2e834 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e 44  se, SQLITE_REIND
2e835 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  EX, pIndex->zNam
2e836 65 2c 20 30 2c 0d 0a 20 20 20 20 20 20 64 62 2d  e, 0,..      db-
2e837 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20  >aDb[iDb].zName 
2e838 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ) ){..    return
2e839 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
2e83a 0d 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61  ..  /* Require a
2e83b 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
2e83c 68 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66  he table to perf
2e83d 6f 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69  orm this operati
2e83e 6f 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  on */..  sqlite3
2e83f 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
2e840 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75  , iDb, pTab->tnu
2e841 6d 2c 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  m, 1, pTab->zNam
2e842 65 29 3b 0d 0a 0d 0a 20 20 76 20 3d 20 73 71 6c  e);....  v = sql
2e843 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2e844 73 65 29 3b 0d 0a 20 20 69 66 28 20 76 3d 3d 30  se);..  if( v==0
2e845 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 69 66   ) return;..  if
2e846 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ( memRootPage>=0
2e847 20 29 7b 0d 0a 20 20 20 20 74 6e 75 6d 20 3d 20   ){..    tnum = 
2e848 6d 65 6d 52 6f 6f 74 50 61 67 65 3b 0d 0a 20 20  memRootPage;..  
2e849 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 74 6e 75 6d  }else{..    tnum
2e84a 20 3d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b   = pIndex->tnum;
2e84b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2e84c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
2e84d 65 61 72 2c 20 74 6e 75 6d 2c 20 69 44 62 29 3b  ear, tnum, iDb);
2e84e 0d 0a 20 20 7d 0d 0a 20 20 70 4b 65 79 20 3d 20  ..  }..  pKey = 
2e84f 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
2e850 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 6e 64  nfo(pParse, pInd
2e851 65 78 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  ex);..  sqlite3V
2e852 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2e853 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78 2c  OpenWrite, iIdx,
2e854 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0d 0a 20 20   tnum, iDb, ..  
2e855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e856 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20    (char *)pKey, 
2e857 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2e858 46 46 29 3b 0d 0a 20 20 69 66 28 20 6d 65 6d 52  FF);..  if( memR
2e859 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0d 0a 20  ootPage>=0 ){.. 
2e85a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2e85b 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0d 0a 20  angeP5(v, 1);.. 
2e85c 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51   }....#ifndef SQ
2e85d 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f  LITE_OMIT_MERGE_
2e85e 53 4f 52 54 0d 0a 20 20 2f 2a 20 4f 70 65 6e 20  SORT..  /* Open 
2e85f 74 68 65 20 73 6f 72 74 65 72 20 63 75 72 73 6f  the sorter curso
2e860 72 20 69 66 20 77 65 20 61 72 65 20 74 6f 20 75  r if we are to u
2e861 73 65 20 6f 6e 65 2e 20 2a 2f 0d 0a 20 20 69 53  se one. */..  iS
2e862 6f 72 74 65 72 20 3d 20 70 50 61 72 73 65 2d 3e  orter = pParse->
2e863 6e 54 61 62 2b 2b 3b 0d 0a 20 20 73 71 6c 69 74  nTab++;..  sqlit
2e864 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2e865 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e 2c 20 69  OP_SorterOpen, i
2e866 53 6f 72 74 65 72 2c 20 30 2c 20 30 2c 20 28 63  Sorter, 0, 0, (c
2e867 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
2e868 59 49 4e 46 4f 29 3b 0d 0a 23 65 6c 73 65 0d 0a  YINFO);..#else..
2e869 20 20 69 53 6f 72 74 65 72 20 3d 20 69 54 61 62    iSorter = iTab
2e86a 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f  ;..#endif....  /
2e86b 2a 20 4f 70 65 6e 20 74 68 65 20 74 61 62 6c 65  * Open the table
2e86c 2e 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61  . Loop through a
2e86d 6c 6c 20 72 6f 77 73 20 6f 66 20 74 68 65 20 74  ll rows of the t
2e86e 61 62 6c 65 2c 20 69 6e 73 65 72 74 69 6e 67 20  able, inserting 
2e86f 69 6e 64 65 78 0d 0a 20 20 2a 2a 20 72 65 63 6f  index..  ** reco
2e870 72 64 73 20 69 6e 74 6f 20 74 68 65 20 73 6f 72  rds into the sor
2e871 74 65 72 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ter. */..  sqlit
2e872 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
2e873 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70  se, iTab, iDb, p
2e874 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  Tab, OP_OpenRead
2e875 29 3b 0d 0a 20 20 61 64 64 72 31 20 3d 20 73 71  );..  addr1 = sq
2e876 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e877 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54  v, OP_Rewind, iT
2e878 61 62 2c 20 30 29 3b 0d 0a 20 20 72 65 67 52 65  ab, 0);..  regRe
2e879 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65  cord = sqlite3Ge
2e87a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2e87b 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;....#ifndef SQL
2e87c 49 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53  ITE_OMIT_MERGE_S
2e87d 4f 52 54 0d 0a 20 20 73 71 6c 69 74 65 33 47 65  ORT..  sqlite3Ge
2e87e 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
2e87f 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69  Parse, pIndex, i
2e880 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20  Tab, regRecord, 
2e881 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  1);..  sqlite3Vd
2e882 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
2e883 6f 72 74 65 72 49 6e 73 65 72 74 2c 20 69 53 6f  orterInsert, iSo
2e884 72 74 65 72 2c 20 72 65 67 52 65 63 6f 72 64 29  rter, regRecord)
2e885 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
2e886 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2e887 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31 2b 31  t, iTab, addr1+1
2e888 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
2e889 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2e88a 72 31 29 3b 0d 0a 20 20 61 64 64 72 31 20 3d 20  r1);..  addr1 = 
2e88b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e88c 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 6f  2(v, OP_SorterSo
2e88d 72 74 2c 20 69 53 6f 72 74 65 72 2c 20 30 29 3b  rt, iSorter, 0);
2e88e 0d 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e  ..  if( pIndex->
2e88f 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2e890 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6a 32 20   ){..    int j2 
2e891 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2e892 72 65 6e 74 41 64 64 72 28 76 29 20 2b 20 33 3b  rentAddr(v) + 3;
2e893 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2e894 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2e895 74 6f 2c 20 30 2c 20 6a 32 29 3b 0d 0a 20 20 20  to, 0, j2);..   
2e896 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33   addr2 = sqlite3
2e897 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2e898 76 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  v);..    sqlite3
2e899 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2e89a 5f 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 2c 20  _SorterCompare, 
2e89b 69 53 6f 72 74 65 72 2c 20 6a 32 2c 20 72 65 67  iSorter, j2, reg
2e89c 52 65 63 6f 72 64 29 3b 0d 0a 20 20 20 20 73 71  Record);..    sq
2e89d 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
2e89e 69 6e 74 28 0d 0a 20 20 20 20 20 20 20 20 70 50  int(..        pP
2e89f 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20  arse, OE_Abort, 
2e8a0 22 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73  "indexed columns
2e8a1 20 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22   are not unique"
2e8a2 2c 20 50 34 5f 53 54 41 54 49 43 0d 0a 20 20 20  , P4_STATIC..   
2e8a3 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   );..  }else{.. 
2e8a4 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74     addr2 = sqlit
2e8a5 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2e8a6 72 28 76 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71  r(v);..  }..  sq
2e8a7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e8a8 76 2c 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61  v, OP_SorterData
2e8a9 2c 20 69 53 6f 72 74 65 72 2c 20 72 65 67 52 65  , iSorter, regRe
2e8aa 63 6f 72 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65  cord);..  sqlite
2e8ab 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2e8ac 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64  P_IdxInsert, iId
2e8ad 78 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31 29  x, regRecord, 1)
2e8ae 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
2e8af 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
2e8b0 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
2e8b1 29 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 72 65 67  );..#else..  reg
2e8b2 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33  IdxKey = sqlite3
2e8b3 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
2e8b4 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
2e8b5 20 69 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64   iTab, regRecord
2e8b6 2c 20 31 29 3b 0d 0a 20 20 61 64 64 72 32 20 3d  , 1);..  addr2 =
2e8b7 20 61 64 64 72 31 20 2b 20 31 3b 0d 0a 20 20 69   addr1 + 1;..  i
2e8b8 66 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  f( pIndex->onErr
2e8b9 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0d 0a  or!=OE_None ){..
2e8ba 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 72 65      const int re
2e8bb 67 52 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b  gRowid = regIdxK
2e8bc 65 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f  ey + pIndex->nCo
2e8bd 6c 75 6d 6e 3b 0d 0a 20 20 20 20 63 6f 6e 73 74  lumn;..    const
2e8be 20 69 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74 65   int j2 = sqlite
2e8bf 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2e8c0 28 76 29 20 2b 20 32 3b 0d 0a 20 20 20 20 76 6f  (v) + 2;..    vo
2e8c1 69 64 20 2a 20 63 6f 6e 73 74 20 70 52 65 67 4b  id * const pRegK
2e8c2 65 79 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ey = SQLITE_INT_
2e8c3 54 4f 5f 50 54 52 28 72 65 67 49 64 78 4b 65 79  TO_PTR(regIdxKey
2e8c4 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 65  );....    /* The
2e8c5 20 72 65 67 69 73 74 65 72 73 20 61 63 63 65 73   registers acces
2e8c6 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49 73  sed by the OP_Is
2e8c7 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 77 65  Unique opcode we
2e8c8 72 65 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 20 20  re allocated..  
2e8c9 20 20 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74    ** using sqlit
2e8ca 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 29  e3GetTempRange()
2e8cb 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 73   inside of the s
2e8cc 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
2e8cd 64 65 78 4b 65 79 28 29 0d 0a 20 20 20 20 2a 2a  dexKey()..    **
2e8ce 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 4a 75 73   call above. Jus
2e8cf 74 20 62 65 66 6f 72 65 20 74 68 61 74 20 66 75  t before that fu
2e8d0 6e 63 74 69 6f 6e 20 77 61 73 20 66 72 65 65 64  nction was freed
2e8d1 20 74 68 65 79 20 77 65 72 65 20 72 65 6c 65 61   they were relea
2e8d2 73 65 64 0d 0a 20 20 20 20 2a 2a 20 28 6d 61 64  sed..    ** (mad
2e8d3 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74  e available to t
2e8d4 68 65 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20  he compiler for 
2e8d5 72 65 75 73 65 29 20 75 73 69 6e 67 20 0d 0a 20  reuse) using .. 
2e8d6 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 6c     ** sqlite3Rel
2e8d7 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 2e  easeTempRange().
2e8d8 20 53 6f 20 69 6e 20 73 6f 6d 65 20 77 61 79 73   So in some ways
2e8d9 20 68 61 76 69 6e 67 20 74 68 65 20 4f 50 5f 49   having the OP_I
2e8da 73 55 6e 69 71 75 65 0d 0a 20 20 20 20 2a 2a 20  sUnique..    ** 
2e8db 6f 70 63 6f 64 65 20 75 73 65 20 74 68 65 20 76  opcode use the v
2e8dc 61 6c 75 65 73 20 73 74 6f 72 65 64 20 77 69 74  alues stored wit
2e8dd 68 69 6e 20 73 65 65 6d 73 20 64 61 6e 67 65 72  hin seems danger
2e8de 6f 75 73 2e 20 48 6f 77 65 76 65 72 2c 20 73 69  ous. However, si
2e8df 6e 63 65 0d 0a 20 20 20 20 2a 2a 20 77 65 20 63  nce..    ** we c
2e8e0 61 6e 20 62 65 20 73 75 72 65 20 74 68 61 74 20  an be sure that 
2e8e1 6e 6f 20 6f 74 68 65 72 20 74 65 6d 70 20 72 65  no other temp re
2e8e2 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65 65  gisters have bee
2e8e3 6e 20 61 6c 6c 6f 63 61 74 65 64 0d 0a 20 20 20  n allocated..   
2e8e4 20 2a 2a 20 73 69 6e 63 65 20 73 71 6c 69 74 65   ** since sqlite
2e8e5 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2e8e6 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  e() was called, 
2e8e7 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 64 6f  it is safe to do
2e8e8 20 73 6f 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20   so...    */..  
2e8e9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2e8ea 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71  Op4(v, OP_IsUniq
2e8eb 75 65 2c 20 69 49 64 78 2c 20 6a 32 2c 20 72 65  ue, iIdx, j2, re
2e8ec 67 52 6f 77 69 64 2c 20 70 52 65 67 4b 65 79 2c  gRowid, pRegKey,
2e8ed 20 50 34 5f 49 4e 54 33 32 29 3b 0d 0a 20 20 20   P4_INT32);..   
2e8ee 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
2e8ef 74 72 61 69 6e 74 28 0d 0a 20 20 20 20 20 20 20  traint(..       
2e8f0 20 70 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72   pParse, OE_Abor
2e8f1 74 2c 20 22 69 6e 64 65 78 65 64 20 63 6f 6c 75  t, "indexed colu
2e8f2 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e 69 71  mns are not uniq
2e8f3 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ue", P4_STATIC);
2e8f4 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
2e8f5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2e8f6 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78  _IdxInsert, iIdx
2e8f7 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 30 29 3b  , regRecord, 0);
2e8f8 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ..  sqlite3VdbeC
2e8f9 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
2e8fa 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29  G_USESEEKRESULT)
2e8fb 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c  ;..#endif..  sql
2e8fc 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2e8fd 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  eg(pParse, regRe
2e8fe 63 6f 72 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65  cord);..  sqlite
2e8ff 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2e900 50 5f 53 6f 72 74 65 72 4e 65 78 74 2c 20 69 53  P_SorterNext, iS
2e901 6f 72 74 65 72 2c 20 61 64 64 72 32 29 3b 0d 0a  orter, addr2);..
2e902 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2e903 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
2e904 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  ....  sqlite3Vdb
2e905 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
2e906 6f 73 65 2c 20 69 54 61 62 29 3b 0d 0a 20 20 73  ose, iTab);..  s
2e907 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2e908 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
2e909 64 78 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  dx);..  sqlite3V
2e90a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2e90b 43 6c 6f 73 65 2c 20 69 53 6f 72 74 65 72 29 3b  Close, iSorter);
2e90c 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72  ..}..../*..** Cr
2e90d 65 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78  eate a new index
2e90e 20 66 6f 72 20 61 6e 20 53 51 4c 20 74 61 62 6c   for an SQL tabl
2e90f 65 2e 20 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65  e.  pName1.pName
2e910 32 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  2 is the name of
2e911 20 74 68 65 20 69 6e 64 65 78 20 0d 0a 2a 2a 20   the index ..** 
2e912 61 6e 64 20 70 54 62 6c 4c 69 73 74 20 69 73 20  and pTblList is 
2e913 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
2e914 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
2e915 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 42 6f   be indexed.  Bo
2e916 74 68 20 77 69 6c 6c 20 0d 0a 2a 2a 20 62 65 20  th will ..** be 
2e917 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72 69 6d 61  NULL for a prima
2e918 72 79 20 6b 65 79 20 6f 72 20 61 6e 20 69 6e 64  ry key or an ind
2e919 65 78 20 74 68 61 74 20 69 73 20 63 72 65 61 74  ex that is creat
2e91a 65 64 20 74 6f 20 73 61 74 69 73 66 79 20 61 0d  ed to satisfy a.
2e91b 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  .** UNIQUE const
2e91c 72 61 69 6e 74 2e 20 20 49 66 20 70 54 61 62 6c  raint.  If pTabl
2e91d 65 20 61 6e 64 20 70 49 6e 64 65 78 20 61 72 65  e and pIndex are
2e91e 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61 72 73   NULL, use pPars
2e91f 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0d 0a 2a 2a  e->pNewTable..**
2e920 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 74 6f   as the table to
2e921 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70 50   be indexed.  pP
2e922 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2e923 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 74 20  is a table that 
2e924 69 73 0d 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  is..** currently
2e925 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
2e926 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20 54  ed by a CREATE T
2e927 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0d  ABLE statement..
2e928 0a 2a 2a 0d 0a 2a 2a 20 70 4c 69 73 74 20 69 73  .**..** pList is
2e929 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
2e92a 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  ns to be indexed
2e92b 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65  .  pList will be
2e92c 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0d 0a 2a   NULL if this..*
2e92d 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b  * is a primary k
2e92e 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e  ey or unique-con
2e92f 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d  straint on the m
2e930 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d  ost recent colum
2e931 6e 20 61 64 64 65 64 0d 0a 2a 2a 20 74 6f 20 74  n added..** to t
2e932 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  he table current
2e933 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
2e934 63 74 69 6f 6e 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a  ction.  ..**..**
2e935 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 69 73   If the index is
2e936 20 63 72 65 61 74 65 64 20 73 75 63 63 65 73 73   created success
2e937 66 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20 61 20  fully, return a 
2e938 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
2e939 65 77 20 49 6e 64 65 78 0d 0a 2a 2a 20 73 74 72  ew Index..** str
2e93a 75 63 74 75 72 65 2e 20 54 68 69 73 20 69 73 20  ucture. This is 
2e93b 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 41  used by sqlite3A
2e93c 64 64 50 72 69 6d 61 72 79 4b 65 79 28 29 20 74  ddPrimaryKey() t
2e93d 6f 20 6d 61 72 6b 20 74 68 65 20 69 6e 64 65 78  o mark the index
2e93e 0d 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c  ..** as the tabl
2e93f 65 73 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28  es primary key (
2e940 49 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d  Index.autoIndex=
2e941 3d 32 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  =2)...*/..SQLITE
2e942 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a  _PRIVATE Index *
2e943 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
2e944 65 78 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  ex(..  Parse *pP
2e945 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c  arse,     /* All
2e946 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
2e947 75 74 20 74 68 69 73 20 70 61 72 73 65 20 2a 2f  ut this parse */
2e948 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  ..  Token *pName
2e949 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20  1,     /* First 
2e94a 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61  part of index na
2e94b 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  me. May be NULL 
2e94c 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61  */..  Token *pNa
2e94d 6d 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f  me2,     /* Seco
2e94e 6e 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78  nd part of index
2e94f 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55   name. May be NU
2e950 4c 4c 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74  LL */..  SrcList
2e951 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54   *pTblName, /* T
2e952 61 62 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55  able to index. U
2e953 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  se pParse->pNewT
2e954 61 62 6c 65 20 69 66 20 30 20 2a 2f 0d 0a 20 20  able if 0 */..  
2e955 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
2e956 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20     /* A list of 
2e957 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e  columns to be in
2e958 64 65 78 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  dexed */..  int 
2e959 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 2f  onError,       /
2e95a 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49  * OE_Abort, OE_I
2e95b 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63  gnore, OE_Replac
2e95c 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f  e, or OE_None */
2e95d 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72  ..  Token *pStar
2e95e 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52  t,     /* The CR
2e95f 45 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20  EATE token that 
2e960 62 65 67 69 6e 73 20 74 68 69 73 20 73 74 61 74  begins this stat
2e961 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 54 6f 6b 65  ement */..  Toke
2e962 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 2f  n *pEnd,       /
2e963 2a 20 54 68 65 20 22 29 22 20 74 68 61 74 20 63  * The ")" that c
2e964 6c 6f 73 65 73 20 74 68 65 20 43 52 45 41 54 45  loses the CREATE
2e965 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2e966 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 6f 72 74 4f   */..  int sortO
2e967 72 64 65 72 2c 20 20 20 20 20 2f 2a 20 53 6f 72  rder,     /* Sor
2e968 74 20 6f 72 64 65 72 20 6f 66 20 70 72 69 6d 61  t order of prima
2e969 72 79 20 6b 65 79 20 77 68 65 6e 20 70 4c 69 73  ry key when pLis
2e96a 74 3d 3d 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e  t==NULL */..  in
2e96b 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20 20 20  t ifNotExist    
2e96c 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72 20 69   /* Omit error i
2e96d 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20  f index already 
2e96e 65 78 69 73 74 73 20 2a 2f 0d 0a 29 7b 0d 0a 20  exists */..){.. 
2e96f 20 49 6e 64 65 78 20 2a 70 52 65 74 20 3d 20 30   Index *pRet = 0
2e970 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
2e971 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 20   to return */.. 
2e972 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
2e973 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ;     /* Table t
2e974 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0d  o be indexed */.
2e975 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
2e976 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69   = 0;   /* The i
2e977 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74  ndex to be creat
2e978 65 64 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  ed */..  char *z
2e979 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Name = 0;     /*
2e97a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   Name of the ind
2e97b 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e 61  ex */..  int nNa
2e97c 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  me;           /*
2e97d 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
2e97e 63 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a  cters in zName *
2e97f 2f 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a  /..  int i, j;..
2e980 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20    Token nullId; 
2e981 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 74         /* Fake t
2e982 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74  oken for an empt
2e983 79 20 49 44 20 6c 69 73 74 20 2a 2f 0d 0a 20 20  y ID list */..  
2e984 44 62 46 69 78 65 72 20 73 46 69 78 3b 20 20 20  DbFixer sFix;   
2e985 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 73 73 69       /* For assi
2e986 67 6e 69 6e 67 20 64 61 74 61 62 61 73 65 20 6e  gning database n
2e987 61 6d 65 73 20 74 6f 20 70 54 61 62 6c 65 20 2a  ames to pTable *
2e988 2f 0d 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64  /..  int sortOrd
2e989 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20 74  erMask;   /* 1 t
2e98a 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e 20  o honor DESC in 
2e98b 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 67 6e  index.  0 to ign
2e98c 6f 72 65 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ore. */..  sqlit
2e98d 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e98e 3e 64 62 3b 0d 0a 20 20 44 62 20 2a 70 44 62 3b  >db;..  Db *pDb;
2e98f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e990 54 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62  The specific tab
2e991 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
2e992 65 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61  e indexed databa
2e993 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62  se */..  int iDb
2e994 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2e995 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
2e996 74 61 62 61 73 65 20 74 68 61 74 20 69 73 20 62  tabase that is b
2e997 65 69 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0d  eing written */.
2e998 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  .  Token *pName 
2e999 3d 20 30 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61  = 0;    /* Unqua
2e99a 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74  lified name of t
2e99b 68 65 20 69 6e 64 65 78 20 74 6f 20 63 72 65 61  he index to crea
2e99c 74 65 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  te */..  struct 
2e99d 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2e99e 4c 69 73 74 49 74 65 6d 3b 20 2f 2a 20 46 6f 72  ListItem; /* For
2e99f 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 4c   looping over pL
2e9a0 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43  ist */..  int nC
2e9a1 6f 6c 3b 0d 0a 20 20 69 6e 74 20 6e 45 78 74 72  ol;..  int nExtr
2e9a2 61 20 3d 20 30 3b 0d 0a 20 20 63 68 61 72 20 2a  a = 0;..  char *
2e9a3 7a 45 78 74 72 61 3b 0d 0a 0d 0a 20 20 61 73 73  zExtra;....  ass
2e9a4 65 72 74 28 20 70 53 74 61 72 74 3d 3d 30 20 7c  ert( pStart==0 |
2e9a5 7c 20 70 45 6e 64 21 3d 30 20 29 3b 20 2f 2a 20  | pEnd!=0 ); /* 
2e9a6 70 45 6e 64 20 6d 75 73 74 20 62 65 20 6e 6f 6e  pEnd must be non
2e9a7 2d 4e 55 4c 4c 20 69 66 20 70 53 74 61 72 74 20  -NULL if pStart 
2e9a8 69 73 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  is */..  assert(
2e9a9 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
2e9aa 20 29 3b 20 20 20 20 20 20 2f 2a 20 4e 65 76 65   );      /* Neve
2e9ab 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72  r called with pr
2e9ac 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0d 0a 20  ior errors */.. 
2e9ad 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2e9ae 61 69 6c 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c  ailed || IN_DECL
2e9af 41 52 45 5f 56 54 41 42 20 29 7b 0d 0a 20 20 20  ARE_VTAB ){..   
2e9b0 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2e9b1 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a 20  e_index;..  }.. 
2e9b2 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2e9b3 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
2e9b4 61 28 70 50 61 72 73 65 29 20 29 7b 0d 0a 20 20  a(pParse) ){..  
2e9b5 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
2e9b6 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a  te_index;..  }..
2e9b7 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 46 69 6e  ..  /*..  ** Fin
2e9b8 64 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  d the table that
2e9b9 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
2e9ba 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72 6c 79  d.  Return early
2e9bb 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0d 0a   if not found...
2e9bc 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54 62 6c    */..  if( pTbl
2e9bd 4e 61 6d 65 21 3d 30 20 29 7b 0d 0a 0d 0a 20 20  Name!=0 ){....  
2e9be 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f    /* Use the two
2e9bf 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65  -part index name
2e9c0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
2e9c1 65 20 64 61 74 61 62 61 73 65 20 0d 0a 20 20 20  e database ..   
2e9c2 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f   ** to search fo
2e9c3 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69  r the table. 'Fi
2e9c4 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  x' the table nam
2e9c5 65 20 74 6f 20 74 68 69 73 20 64 62 0d 0a 20 20  e to this db..  
2e9c6 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b    ** before look
2e9c7 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65  ing up the table
2e9c8 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61  ...    */..    a
2e9c9 73 73 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26  ssert( pName1 &&
2e9ca 20 70 4e 61 6d 65 32 20 29 3b 0d 0a 20 20 20 20   pName2 );..    
2e9cb 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
2e9cc 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
2e9cd 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
2e9ce 20 26 70 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 69   &pName);..    i
2e9cf 66 28 20 69 44 62 3c 30 20 29 20 67 6f 74 6f 20  f( iDb<0 ) goto 
2e9d0 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2e9d1 78 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  x;..    assert( 
2e9d2 70 4e 61 6d 65 20 26 26 20 70 4e 61 6d 65 2d 3e  pName && pName->
2e9d3 7a 20 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  z );....#ifndef 
2e9d4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50  SQLITE_OMIT_TEMP
2e9d5 44 42 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  DB..    /* If th
2e9d6 65 20 69 6e 64 65 78 20 6e 61 6d 65 20 77 61 73  e index name was
2e9d7 20 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 63 68   unqualified, ch
2e9d8 65 63 6b 20 69 66 20 74 68 65 20 74 68 65 20 74  eck if the the t
2e9d9 61 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 69 73 20  able..    ** is 
2e9da 61 20 74 65 6d 70 20 74 61 62 6c 65 2e 20 49 66  a temp table. If
2e9db 20 73 6f 2c 20 73 65 74 20 74 68 65 20 64 61 74   so, set the dat
2e9dc 61 62 61 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e  abase to 1. Do n
2e9dd 6f 74 20 64 6f 20 74 68 69 73 0d 0a 20 20 20 20  ot do this..    
2e9de 2a 2a 20 69 66 20 69 6e 69 74 69 61 6c 69 73 69  ** if initialisi
2e9df 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 73 63  ng a database sc
2e9e0 68 65 6d 61 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  hema...    */.. 
2e9e1 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
2e9e2 2e 62 75 73 79 20 29 7b 0d 0a 20 20 20 20 20 20  .busy ){..      
2e9e3 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
2e9e4 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
2e9e5 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0d 0a  se, pTblName);..
2e9e6 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32        if( pName2
2e9e7 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26  ->n==0 && pTab &
2e9e8 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d  & pTab->pSchema=
2e9e9 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  =db->aDb[1].pSch
2e9ea 65 6d 61 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ema ){..        
2e9eb 69 44 62 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  iDb = 1;..      
2e9ec 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  }..    }..#endif
2e9ed 0d 0a 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ....    if( sqli
2e9ee 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78  te3FixInit(&sFix
2e9ef 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22  , pParse, iDb, "
2e9f0 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29 20 26  index", pName) &
2e9f1 26 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  &..        sqlit
2e9f2 65 33 46 69 78 53 72 63 4c 69 73 74 28 26 73 46  e3FixSrcList(&sF
2e9f3 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 0d 0a 20  ix, pTblName).. 
2e9f4 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20     ){..      /* 
2e9f5 42 65 63 61 75 73 65 20 74 68 65 20 70 61 72 73  Because the pars
2e9f6 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70 54  er constructs pT
2e9f7 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 69  blName from a si
2e9f8 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c  ngle identifier,
2e9f9 0d 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  ..      ** sqlit
2e9fa 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e  e3FixSrcList can
2e9fb 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0d   never fail. */.
2e9fc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29  .      assert(0)
2e9fd 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 54  ;..    }..    pT
2e9fe 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
2e9ff 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
2ea00 30 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30  0, pTblName->a[0
2ea01 5d 2e 7a 4e 61 6d 65 2c 20 0d 0a 20 20 20 20 20  ].zName, ..     
2ea02 20 20 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30     pTblName->a[0
2ea03 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0d 0a 20  ].zDatabase);.. 
2ea04 20 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20     if( !pTab || 
2ea05 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2ea06 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
2ea07 61 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20  ate_index;..    
2ea08 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
2ea09 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 54  iDb].pSchema==pT
2ea0a 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0d 0a  ab->pSchema );..
2ea0b 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73    }else{..    as
2ea0c 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29  sert( pName==0 )
2ea0d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
2ea0e 53 74 61 72 74 3d 3d 30 20 29 3b 0d 0a 20 20 20  Start==0 );..   
2ea0f 20 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e   pTab = pParse->
2ea10 70 4e 65 77 54 61 62 6c 65 3b 0d 0a 20 20 20 20  pNewTable;..    
2ea11 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f  if( !pTab ) goto
2ea12 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2ea13 65 78 3b 0d 0a 20 20 20 20 69 44 62 20 3d 20 73  ex;..    iDb = s
2ea14 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2ea15 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
2ea16 63 68 65 6d 61 29 3b 0d 0a 20 20 7d 0d 0a 20 20  chema);..  }..  
2ea17 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
2ea18 44 62 5d 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  Db];....  assert
2ea19 28 20 70 54 61 62 21 3d 30 20 29 3b 0d 0a 20 20  ( pTab!=0 );..  
2ea1a 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2ea1b 6e 45 72 72 3d 3d 30 20 29 3b 0d 0a 20 20 69 66  nErr==0 );..  if
2ea1c 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
2ea1d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
2ea1e 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20  sqlite_", 7)==0 
2ea1f 0d 0a 20 20 20 20 20 20 20 26 26 20 6d 65 6d 63  ..       && memc
2ea20 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d 65 5b  mp(&pTab->zName[
2ea21 37 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22 2c 39  7],"altertab_",9
2ea22 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c  )!=0 ){..    sql
2ea23 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2ea24 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d  rse, "table %s m
2ea25 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65  ay not be indexe
2ea26 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  d", pTab->zName)
2ea27 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ;..    goto exit
2ea28 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a  _create_index;..
2ea29 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2ea2a 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 20  ITE_OMIT_VIEW.. 
2ea2b 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
2ea2c 63 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ct ){..    sqlit
2ea2d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2ea2e 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f  e, "views may no
2ea2f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0d  t be indexed");.
2ea30 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63  .    goto exit_c
2ea31 72 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20  reate_index;..  
2ea32 7d 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64  }..#endif..#ifnd
2ea33 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2ea34 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 69  IRTUALTABLE..  i
2ea35 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2ea36 62 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  b) ){..    sqlit
2ea37 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2ea38 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c  e, "virtual tabl
2ea39 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  es may not be in
2ea3a 64 65 78 65 64 22 29 3b 0d 0a 20 20 20 20 67 6f  dexed");..    go
2ea3b 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
2ea3c 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  ndex;..  }..#end
2ea3d 69 66 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a  if....  /*..  **
2ea3e 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f   Find the name o
2ea3f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 4d 61  f the index.  Ma
2ea40 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
2ea41 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 6f   not already ano
2ea42 74 68 65 72 0d 0a 20 20 2a 2a 20 69 6e 64 65 78  ther..  ** index
2ea43 20 6f 72 20 74 61 62 6c 65 20 77 69 74 68 20 74   or table with t
2ea44 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20 0d  he same name.  .
2ea45 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 45 78 63 65  .  **..  ** Exce
2ea46 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61 72  ption:  If we ar
2ea47 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e 61  e reading the na
2ea48 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74  mes of permanent
2ea49 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74 68   indices from th
2ea4a 65 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d  e..  ** sqlite_m
2ea4b 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63  aster table (bec
2ea4c 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  ause some other 
2ea4d 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20  process changed 
2ea4e 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0d  the schema) and.
2ea4f 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65  .  ** one of the
2ea50 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c   index names col
2ea51 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e  lides with the n
2ea52 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61  ame of a tempora
2ea53 72 79 20 74 61 62 6c 65 20 6f 72 0d 0a 20 20 2a  ry table or..  *
2ea54 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65  * index, then we
2ea55 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
2ea56 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69  o process this i
2ea57 6e 64 65 78 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  ndex...  **..  *
2ea58 2a 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74  * If pName==0 it
2ea59 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61   means that we a
2ea5a 72 65 0d 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67  re..  ** dealing
2ea5b 20 77 69 74 68 20 61 20 70 72 69 6d 61 72 79 20   with a primary 
2ea5c 6b 65 79 20 6f 72 20 55 4e 49 51 55 45 20 63 6f  key or UNIQUE co
2ea5d 6e 73 74 72 61 69 6e 74 2e 20 20 57 65 20 68 61  nstraint.  We ha
2ea5e 76 65 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72  ve to invent our
2ea5f 0d 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e  ..  ** own name.
2ea60 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4e  ..  */..  if( pN
2ea61 61 6d 65 20 29 7b 0d 0a 20 20 20 20 7a 4e 61 6d  ame ){..    zNam
2ea62 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
2ea63 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
2ea64 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a 4e  me);..    if( zN
2ea65 61 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78  ame==0 ) goto ex
2ea66 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
2ea67 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ..    assert( pN
2ea68 61 6d 65 2d 3e 7a 21 3d 30 20 29 3b 0d 0a 20 20  ame->z!=0 );..  
2ea69 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2ea6a 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a  =sqlite3CheckObj
2ea6b 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  ectName(pParse, 
2ea6c 7a 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20 20 20  zName) ){..     
2ea6d 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2ea6e 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 7d 0d  e_index;..    }.
2ea6f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  .    if( !db->in
2ea70 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20 20 20  it.busy ){..    
2ea71 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
2ea72 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
2ea73 2c 20 30 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20  , 0)!=0 ){..    
2ea74 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ea75 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65  Msg(pParse, "the
2ea76 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
2ea77 74 61 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c  table named %s",
2ea78 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20   zName);..      
2ea79 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
2ea7a 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 20  te_index;..     
2ea7b 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69   }..    }..    i
2ea7c 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  f( sqlite3FindIn
2ea7d 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70  dex(db, zName, p
2ea7e 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b  Db->zName)!=0 ){
2ea7f 0d 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e  ..      if( !ifN
2ea80 6f 74 45 78 69 73 74 20 29 7b 0d 0a 20 20 20 20  otExist ){..    
2ea81 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ea82 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
2ea83 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78  ex %s already ex
2ea84 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a  ists", zName);..
2ea85 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
2ea86 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 64        assert( !d
2ea87 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0d  b->init.busy );.
2ea88 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ea89 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
2ea8a 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a  (pParse, iDb);..
2ea8b 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 67        }..      g
2ea8c 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
2ea8d 69 6e 64 65 78 3b 0d 0a 20 20 20 20 7d 0d 0a 20  index;..    }.. 
2ea8e 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74   }else{..    int
2ea8f 20 6e 3b 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a   n;..    Index *
2ea90 70 4c 6f 6f 70 3b 0d 0a 20 20 20 20 66 6f 72 28  pLoop;..    for(
2ea91 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70 49 6e 64  pLoop=pTab->pInd
2ea92 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20  ex, n=1; pLoop; 
2ea93 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65  pLoop=pLoop->pNe
2ea94 78 74 2c 20 6e 2b 2b 29 7b 7d 0d 0a 20 20 20 20  xt, n++){}..    
2ea95 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
2ea96 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69  Printf(db, "sqli
2ea97 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25 73 5f  te_autoindex_%s_
2ea98 25 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %d", pTab->zName
2ea99 2c 20 6e 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a  , n);..    if( z
2ea9a 4e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Name==0 ){..    
2ea9b 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
2ea9c 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 7d  te_index;..    }
2ea9d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68  ..  }....  /* Ch
2ea9e 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a  eck for authoriz
2ea9f 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20  ation to create 
2eaa0 61 6e 20 69 6e 64 65 78 2e 0d 0a 20 20 2a 2f 0d  an index...  */.
2eaa1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2eaa2 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2eaa3 4f 4e 0d 0a 20 20 7b 0d 0a 20 20 20 20 63 6f 6e  ON..  {..    con
2eaa4 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70  st char *zDb = p
2eaa5 44 62 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20  Db->zName;..    
2eaa6 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
2eaa7 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
2eaa8 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45  ITE_INSERT, SCHE
2eaa9 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 30  MA_TABLE(iDb), 0
2eaaa 2c 20 7a 44 62 29 20 29 7b 0d 0a 20 20 20 20 20  , zDb) ){..     
2eaab 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2eaac 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 7d 0d  e_index;..    }.
2eaad 0a 20 20 20 20 69 20 3d 20 53 51 4c 49 54 45 5f  .    i = SQLITE_
2eaae 43 52 45 41 54 45 5f 49 4e 44 45 58 3b 0d 0a 20  CREATE_INDEX;.. 
2eaaf 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
2eab0 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 20  PDB && iDb==1 ) 
2eab1 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  i = SQLITE_CREAT
2eab2 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0d 0a 20  E_TEMP_INDEX;.. 
2eab3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
2eab4 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2eab5 69 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i, zName, pTab->
2eab6 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0d 0a  zName, zDb) ){..
2eab7 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2eab8 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a 20  create_index;.. 
2eab9 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69     }..  }..#endi
2eaba 66 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 70 4c 69  f....  /* If pLi
2eabb 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20  st==0, it means 
2eabc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73  this routine was
2eabd 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20   called to make 
2eabe 61 20 70 72 69 6d 61 72 79 0d 0a 20 20 2a 2a 20  a primary..  ** 
2eabf 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  key out of the l
2eac0 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64  ast column added
2eac1 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e   to the table un
2eac2 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2eac3 2e 0d 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74  ...  ** So creat
2eac4 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f  e a fake list to
2eac5 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0d   simulate this..
2eac6 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4c 69  .  */..  if( pLi
2eac7 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 6e 75  st==0 ){..    nu
2eac8 6c 6c 49 64 2e 7a 20 3d 20 70 54 61 62 2d 3e 61  llId.z = pTab->a
2eac9 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31  Col[pTab->nCol-1
2eaca 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 6e 75  ].zName;..    nu
2eacb 6c 6c 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33  llId.n = sqlite3
2eacc 53 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29  Strlen30((char*)
2eacd 6e 75 6c 6c 49 64 2e 7a 29 3b 0d 0a 20 20 20 20  nullId.z);..    
2eace 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
2eacf 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2ead0 61 72 73 65 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  arse, 0, 0);..  
2ead1 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
2ead2 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2ead3 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 73 71  e_index;..    sq
2ead4 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
2ead5 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4c 69  Name(pParse, pLi
2ead6 73 74 2c 20 26 6e 75 6c 6c 49 64 2c 20 30 29 3b  st, &nullId, 0);
2ead7 0d 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 30  ..    pList->a[0
2ead8 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
2ead9 38 29 73 6f 72 74 4f 72 64 65 72 3b 0d 0a 20 20  8)sortOrder;..  
2eada 7d 0d 0a 0d 0a 20 20 2f 2a 20 46 69 67 75 72 65  }....  /* Figure
2eadb 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79   out how many by
2eadc 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65  tes of space are
2eadd 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
2eade 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0d 0a 20  re explicitly.. 
2eadf 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f   ** specified co
2eae0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2eae1 20 6e 61 6d 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20   names...  */.. 
2eae2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
2eae3 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d  t->nExpr; i++){.
2eae4 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
2eae5 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70   = pList->a[i].p
2eae6 45 78 70 72 3b 0d 0a 20 20 20 20 69 66 28 20 70  Expr;..    if( p
2eae7 45 78 70 72 20 29 7b 0d 0a 20 20 20 20 20 20 43  Expr ){..      C
2eae8 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
2eae9 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0d 0a 20  pExpr->pColl;.. 
2eaea 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 70       /* Either p
2eaeb 43 6f 6c 6c 21 3d 30 20 6f 72 20 74 68 65 72 65  Coll!=0 or there
2eaec 20 77 61 73 20 61 6e 20 4f 4f 4d 20 66 61 69 6c   was an OOM fail
2eaed 75 72 65 2e 20 20 42 75 74 20 69 66 20 61 6e 20  ure.  But if an 
2eaee 4f 4f 4d 0d 0a 20 20 20 20 20 20 2a 2a 20 66 61  OOM..      ** fa
2eaef 69 6c 75 72 65 20 77 65 20 68 61 76 65 20 71 75  ilure we have qu
2eaf0 69 74 20 62 65 66 6f 72 65 20 72 65 61 63 68 69  it before reachi
2eaf1 6e 67 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 2a  ng this point. *
2eaf2 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  /..      if( ALW
2eaf3 41 59 53 28 70 43 6f 6c 6c 29 20 29 7b 0d 0a 20  AYS(pColl) ){.. 
2eaf4 20 20 20 20 20 20 20 6e 45 78 74 72 61 20 2b 3d         nExtra +=
2eaf5 20 28 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72   (1 + sqlite3Str
2eaf6 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61  len30(pColl->zNa
2eaf7 6d 65 29 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  me));..      }..
2eaf8 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
2eaf9 2f 2a 20 0d 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61  /* ..  ** Alloca
2eafa 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72  te the index str
2eafb 75 63 74 75 72 65 2e 20 0d 0a 20 20 2a 2f 0d 0a  ucture. ..  */..
2eafc 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
2eafd 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2eafe 3b 0d 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69 73  ;..  nCol = pLis
2eaff 74 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 70 49 6e  t->nExpr;..  pIn
2eb00 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  dex = sqlite3DbM
2eb01 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0d 0a  allocZero(db, ..
2eb02 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69 7a        ROUND8(siz
2eb03 65 6f 66 28 49 6e 64 65 78 29 29 20 2b 20 20 20  eof(Index)) +   
2eb04 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2eb05 64 65 78 20 73 74 72 75 63 74 75 72 65 20 20 2a  dex structure  *
2eb06 2f 0d 0a 20 20 20 20 20 20 52 4f 55 4e 44 38 28  /..      ROUND8(
2eb07 73 69 7a 65 6f 66 28 74 52 6f 77 63 6e 74 29 2a  sizeof(tRowcnt)*
2eb08 28 6e 43 6f 6c 2b 31 29 29 20 2b 20 20 20 2f 2a  (nCol+1)) +   /*
2eb09 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 20   Index.aiRowEst 
2eb0a 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 69 7a 65    */..      size
2eb0b 6f 66 28 63 68 61 72 20 2a 29 2a 6e 43 6f 6c 20  of(char *)*nCol 
2eb0c 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +               
2eb0d 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c   /* Index.azColl
2eb0e 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73       */..      s
2eb0f 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20  izeof(int)*nCol 
2eb10 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +               
2eb11 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43      /* Index.aiC
2eb12 6f 6c 75 6d 6e 20 20 20 2a 2f 0d 0a 20 20 20 20  olumn   */..    
2eb13 20 20 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f    sizeof(u8)*nCo
2eb14 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  l +             
2eb15 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
2eb16 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0d 0a 20  aSortOrder */.. 
2eb17 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b       nName + 1 +
2eb18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2eb1a 65 78 2e 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ex.zName      */
2eb1b 0d 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20 20  ..      nExtra  
2eb1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb1d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2eb1e 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
2eb1f 63 65 20 6e 61 6d 65 73 20 2a 2f 0d 0a 20 20 29  ce names */..  )
2eb20 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  ;..  if( db->mal
2eb21 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
2eb22 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
2eb23 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a  te_index;..  }..
2eb24 20 20 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72    zExtra = (char
2eb25 2a 29 70 49 6e 64 65 78 3b 0d 0a 20 20 70 49 6e  *)pIndex;..  pIn
2eb26 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20  dex->aiRowEst = 
2eb27 28 74 52 6f 77 63 6e 74 2a 29 26 7a 45 78 74 72  (tRowcnt*)&zExtr
2eb28 61 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  a[ROUND8(sizeof(
2eb29 49 6e 64 65 78 29 29 5d 3b 0d 0a 20 20 70 49 6e  Index))];..  pIn
2eb2a 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63  dex->azColl = (c
2eb2b 68 61 72 2a 2a 29 0d 0a 20 20 20 20 20 28 28 63  har**)..     ((c
2eb2c 68 61 72 2a 29 70 49 6e 64 65 78 2d 3e 61 69 52  har*)pIndex->aiR
2eb2d 6f 77 45 73 74 20 2b 20 52 4f 55 4e 44 38 28 73  owEst + ROUND8(s
2eb2e 69 7a 65 6f 66 28 74 52 6f 77 63 6e 74 29 2a 6e  izeof(tRowcnt)*n
2eb2f 43 6f 6c 2b 31 29 29 3b 0d 0a 20 20 61 73 73 65  Col+1));..  asse
2eb30 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
2eb31 4c 49 47 4e 4d 45 4e 54 28 70 49 6e 64 65 78 2d  LIGNMENT(pIndex-
2eb32 3e 61 69 52 6f 77 45 73 74 29 20 29 3b 0d 0a 20  >aiRowEst) );.. 
2eb33 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
2eb34 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 49  YTE_ALIGNMENT(pI
2eb35 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 29 20 29 3b  ndex->azColl) );
2eb36 0d 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f  ..  pIndex->aiCo
2eb37 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a 29 28 26  lumn = (int *)(&
2eb38 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e  pIndex->azColl[n
2eb39 43 6f 6c 5d 29 3b 0d 0a 20 20 70 49 6e 64 65 78  Col]);..  pIndex
2eb3a 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
2eb3b 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61  u8 *)(&pIndex->a
2eb3c 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0d  iColumn[nCol]);.
2eb3d 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  .  pIndex->zName
2eb3e 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e   = (char *)(&pIn
2eb3f 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  dex->aSortOrder[
2eb40 6e 43 6f 6c 5d 29 3b 0d 0a 20 20 7a 45 78 74 72  nCol]);..  zExtr
2eb41 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49  a = (char *)(&pI
2eb42 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d  ndex->zName[nNam
2eb43 65 2b 31 5d 29 3b 0d 0a 20 20 6d 65 6d 63 70 79  e+1]);..  memcpy
2eb44 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20  (pIndex->zName, 
2eb45 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
2eb46 0d 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62  ..  pIndex->pTab
2eb47 6c 65 20 3d 20 70 54 61 62 3b 0d 0a 20 20 70 49  le = pTab;..  pI
2eb48 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20  ndex->nColumn = 
2eb49 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0d 0a 20  pList->nExpr;.. 
2eb4a 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72   pIndex->onError
2eb4b 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0d   = (u8)onError;.
2eb4c 0a 20 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49  .  pIndex->autoI
2eb4d 6e 64 65 78 20 3d 20 28 75 38 29 28 70 4e 61 6d  ndex = (u8)(pNam
2eb4e 65 3d 3d 30 29 3b 0d 0a 20 20 70 49 6e 64 65 78  e==0);..  pIndex
2eb4f 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
2eb50 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2eb51 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
2eb52 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
2eb53 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20  eld(db, iDb, 0) 
2eb54 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b  );....  /* Check
2eb55 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 73 68   to see if we sh
2eb56 6f 75 6c 64 20 68 6f 6e 6f 72 20 44 45 53 43 20  ould honor DESC 
2eb57 72 65 71 75 65 73 74 73 20 6f 6e 20 69 6e 64 65  requests on inde
2eb58 78 20 63 6f 6c 75 6d 6e 73 0d 0a 20 20 2a 2f 0d  x columns..  */.
2eb59 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68  .  if( pDb->pSch
2eb5a 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
2eb5b 3e 3d 34 20 29 7b 0d 0a 20 20 20 20 73 6f 72 74  >=4 ){..    sort
2eb5c 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31 3b 20  OrderMask = -1; 
2eb5d 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 43 20    /* Honor DESC 
2eb5e 2a 2f 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  */..  }else{..  
2eb5f 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20    sortOrderMask 
2eb60 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72  = 0;    /* Ignor
2eb61 65 20 44 45 53 43 20 2a 2f 0d 0a 20 20 7d 0d 0a  e DESC */..  }..
2eb62 0d 0a 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20  ..  /* Scan the 
2eb63 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c  names of the col
2eb64 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
2eb65 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  e to be indexed 
2eb66 61 6e 64 0d 0a 20 20 2a 2a 20 6c 6f 61 64 20 74  and..  ** load t
2eb67 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65  he column indice
2eb68 73 20 69 6e 74 6f 20 74 68 65 20 49 6e 64 65 78  s into the Index
2eb69 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 70   structure.  Rep
2eb6a 6f 72 74 20 61 6e 20 65 72 72 6f 72 0d 0a 20 20  ort an error..  
2eb6b 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  ** if any column
2eb6c 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0d 0a   is not found...
2eb6d 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 4f 44 4f 3a    **..  ** TODO:
2eb6e 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20    Add a test to 
2eb6f 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
2eb70 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69  he same column i
2eb71 73 20 6e 6f 74 20 6e 61 6d 65 64 0d 0a 20 20 2a  s not named..  *
2eb72 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
2eb73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
2eb74 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68   index.  Only th
2eb75 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63 65  e first instance
2eb76 20 6f 66 0d 0a 20 20 2a 2a 20 74 68 65 20 63 6f   of..  ** the co
2eb77 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72 20 62  lumn will ever b
2eb78 65 20 75 73 65 64 20 62 79 20 74 68 65 20 6f 70  e used by the op
2eb79 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 20 74  timizer.  Note t
2eb7a 68 61 74 20 75 73 69 6e 67 20 74 68 65 0d 0a 20  hat using the.. 
2eb7b 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20   ** same column 
2eb7c 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 63  more than once c
2eb7d 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65 72 72 6f  annot be an erro
2eb7e 72 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77  r because that w
2eb7f 6f 75 6c 64 20 0d 0a 20 20 2a 2a 20 62 72 65 61  ould ..  ** brea
2eb80 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  k backwards comp
2eb81 61 74 69 62 69 6c 69 74 79 20 2d 20 69 74 20 6e  atibility - it n
2eb82 65 65 64 73 20 74 6f 20 62 65 20 61 20 77 61 72  eeds to be a war
2eb83 6e 69 6e 67 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66  ning...  */..  f
2eb84 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49 74 65  or(i=0, pListIte
2eb85 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c  m=pList->a; i<pL
2eb86 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  ist->nExpr; i++,
2eb87 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0d 0a   pListItem++){..
2eb88 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2eb89 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74  zColName = pList
2eb8a 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20  Item->zName;..  
2eb8b 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f    Column *pTabCo
2eb8c 6c 3b 0d 0a 20 20 20 20 69 6e 74 20 72 65 71 75  l;..    int requ
2eb8d 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0d  estedSortOrder;.
2eb8e 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  .    char *zColl
2eb8f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2eb90 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e      /* Collation
2eb91 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a   sequence name *
2eb92 2f 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 30  /....    for(j=0
2eb93 2c 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e  , pTabCol=pTab->
2eb94 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
2eb95 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c  ol; j++, pTabCol
2eb96 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  ++){..      if( 
2eb97 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2eb98 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f 6c  ColName, pTabCol
2eb99 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  ->zName)==0 ) br
2eb9a 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
2eb9b 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43   if( j>=pTab->nC
2eb9c 6f 6c 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  ol ){..      sql
2eb9d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2eb9e 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68  rse, "table %s h
2eb9f 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d  as no column nam
2eba0 65 64 20 25 73 22 2c 0d 0a 20 20 20 20 20 20 20  ed %s",..       
2eba1 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
2eba2 6f 6c 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20  olName);..      
2eba3 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
2eba4 65 6d 61 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  ema = 1;..      
2eba5 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
2eba6 5f 69 6e 64 65 78 3b 0d 0a 20 20 20 20 7d 0d 0a  _index;..    }..
2eba7 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f      pIndex->aiCo
2eba8 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0d 0a 20 20  lumn[i] = j;..  
2eba9 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69    /* Justificati
2ebaa 6f 6e 20 6f 66 20 74 68 65 20 41 4c 57 41 59 53  on of the ALWAYS
2ebab 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70  (pListItem->pExp
2ebac 72 2d 3e 70 43 6f 6c 6c 29 3a 20 20 42 65 63 61  r->pColl):  Beca
2ebad 75 73 65 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 74  use of..    ** t
2ebae 68 65 20 77 61 79 20 74 68 65 20 22 69 64 78 6c  he way the "idxl
2ebaf 69 73 74 22 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  ist" non-termina
2ebb0 6c 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64  l is constructed
2ebb1 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2c 0d   by the parser,.
2ebb2 0a 20 20 20 20 2a 2a 20 69 66 20 70 4c 69 73 74  .    ** if pList
2ebb3 49 74 65 6d 2d 3e 70 45 78 70 72 20 69 73 20 6e  Item->pExpr is n
2ebb4 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 65 69 74  ot null then eit
2ebb5 68 65 72 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70  her pListItem->p
2ebb6 45 78 70 72 2d 3e 70 43 6f 6c 6c 0d 0a 20 20 20  Expr->pColl..   
2ebb7 20 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 6f   ** must exist o
2ebb8 72 20 65 6c 73 65 20 74 68 65 72 65 20 6d 75 73  r else there mus
2ebb9 74 20 68 61 76 65 20 62 65 65 6e 20 61 6e 20 4f  t have been an O
2ebba 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 69  OM error.  But i
2ebbb 66 20 74 68 65 72 65 0d 0a 20 20 20 20 2a 2a 20  f there..    ** 
2ebbc 77 61 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72  was an OOM error
2ebbd 2c 20 77 65 20 77 6f 75 6c 64 20 6e 65 76 65 72  , we would never
2ebbe 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e   reach this poin
2ebbf 74 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70  t. */..    if( p
2ebc0 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20  ListItem->pExpr 
2ebc1 26 26 20 41 4c 57 41 59 53 28 70 4c 69 73 74 49  && ALWAYS(pListI
2ebc2 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
2ebc3 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74  l) ){..      int
2ebc4 20 6e 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 7a   nColl;..      z
2ebc5 43 6f 6c 6c 20 3d 20 70 4c 69 73 74 49 74 65 6d  Coll = pListItem
2ebc6 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e  ->pExpr->pColl->
2ebc7 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20 6e 43  zName;..      nC
2ebc8 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 72  oll = sqlite3Str
2ebc9 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31  len30(zColl) + 1
2ebca 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2ebcb 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29   nExtra>=nColl )
2ebcc 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ;..      memcpy(
2ebcd 7a 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e  zExtra, zColl, n
2ebce 43 6f 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 7a 43  Coll);..      zC
2ebcf 6f 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0d 0a 20  oll = zExtra;.. 
2ebd0 20 20 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e       zExtra += n
2ebd1 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 6e 45 78  Coll;..      nEx
2ebd2 74 72 61 20 2d 3d 20 6e 43 6f 6c 6c 3b 0d 0a 20  tra -= nColl;.. 
2ebd3 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
2ebd4 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61   zColl = pTab->a
2ebd5 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0d 0a 20  Col[j].zColl;.. 
2ebd6 20 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20       if( !zColl 
2ebd7 29 7b 0d 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  ){..        zCol
2ebd8 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
2ebd9 6c 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20  l->zName;..     
2ebda 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69   }..    }..    i
2ebdb 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
2ebdc 79 20 26 26 20 21 73 71 6c 69 74 65 33 4c 6f 63  y && !sqlite3Loc
2ebdd 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
2ebde 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0d 0a 20 20  e, zColl) ){..  
2ebdf 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
2ebe0 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a 20 20 20  eate_index;..   
2ebe1 20 7d 0d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e   }..    pIndex->
2ebe2 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c  azColl[i] = zCol
2ebe3 6c 3b 0d 0a 20 20 20 20 72 65 71 75 65 73 74 65  l;..    requeste
2ebe4 64 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c 69  dSortOrder = pLi
2ebe5 73 74 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65  stItem->sortOrde
2ebe6 72 20 26 20 73 6f 72 74 4f 72 64 65 72 4d 61 73  r & sortOrderMas
2ebe7 6b 3b 0d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e  k;..    pIndex->
2ebe8 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
2ebe9 28 75 38 29 72 65 71 75 65 73 74 65 64 53 6f 72  (u8)requestedSor
2ebea 74 4f 72 64 65 72 3b 0d 0a 20 20 7d 0d 0a 20 20  tOrder;..  }..  
2ebeb 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
2ebec 77 45 73 74 28 70 49 6e 64 65 78 29 3b 0d 0a 0d  wEst(pIndex);...
2ebed 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50 61  .  if( pTab==pPa
2ebee 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29  rse->pNewTable )
2ebef 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 72  {..    /* This r
2ebf0 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
2ebf1 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65  called to create
2ebf2 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e   an automatic in
2ebf3 64 65 78 20 61 73 20 61 0d 0a 20 20 20 20 2a 2a  dex as a..    **
2ebf4 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49   result of a PRI
2ebf5 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
2ebf6 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63  UE clause on a c
2ebf7 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
2ebf8 2c 20 6f 72 0d 0a 20 20 20 20 2a 2a 20 61 20 50  , or..    ** a P
2ebf9 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e  RIMARY KEY or UN
2ebfa 49 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c  IQUE clause foll
2ebfb 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e  owing the column
2ebfc 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0d 0a 20   definitions... 
2ebfd 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f     ** i.e. one o
2ebfe 66 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  f:..    **..    
2ebff 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
2ec00 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  t(x PRIMARY KEY,
2ec01 20 79 29 3b 0d 0a 20 20 20 20 2a 2a 20 43 52 45   y);..    ** CRE
2ec02 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c 20 79  ATE TABLE t(x, y
2ec03 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29 29 3b  , UNIQUE(x, y));
2ec04 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
2ec05 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65   Either way, che
2ec06 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2ec07 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68   table already h
2ec08 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78  as such an index
2ec09 2e 20 49 66 0d 0a 20 20 20 20 2a 2a 20 73 6f 2c  . If..    ** so,
2ec0a 20 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72   don't bother cr
2ec0b 65 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e  eating this one.
2ec0c 20 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69   This only appli
2ec0d 65 73 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 61 75  es to..    ** au
2ec0e 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61  tomatically crea
2ec0f 74 65 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65  ted indices. Use
2ec10 72 73 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65  rs can do as the
2ec11 79 20 77 69 73 68 20 77 69 74 68 0d 0a 20 20 20  y wish with..   
2ec12 20 2a 2a 20 65 78 70 6c 69 63 69 74 20 69 6e 64   ** explicit ind
2ec13 69 63 65 73 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  ices...    **.. 
2ec14 20 20 20 2a 2a 20 54 77 6f 20 55 4e 49 51 55 45     ** Two UNIQUE
2ec15 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20   or PRIMARY KEY 
2ec16 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
2ec17 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 69 76  considered equiv
2ec18 61 6c 65 6e 74 0d 0a 20 20 20 20 2a 2a 20 28 61  alent..    ** (a
2ec19 6e 64 20 74 68 75 73 20 73 75 70 70 72 65 73 73  nd thus suppress
2ec1a 69 6e 67 20 74 68 65 20 73 65 63 6f 6e 64 20 6f  ing the second o
2ec1b 6e 65 29 20 65 76 65 6e 20 69 66 20 74 68 65 79  ne) even if they
2ec1c 20 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 0d   have different.
2ec1d 0a 20 20 20 20 2a 2a 20 73 6f 72 74 20 6f 72 64  .    ** sort ord
2ec1e 65 72 73 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  ers...    **..  
2ec1f 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72    ** If there ar
2ec20 65 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c  e different coll
2ec21 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
2ec22 6f 72 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e  or if the column
2ec23 73 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 74 68 65  s of..    ** the
2ec24 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 63 63 75   constraint occu
2ec25 72 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 6f  r in different o
2ec26 72 64 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20  rders, then the 
2ec27 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 0d  constraints are.
2ec28 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72  .    ** consider
2ec29 65 64 20 64 69 73 74 69 6e 63 74 20 61 6e 64 20  ed distinct and 
2ec2a 62 6f 74 68 20 72 65 73 75 6c 74 20 69 6e 20 73  both result in s
2ec2b 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73 2e  eparate indices.
2ec2c 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 49 6e  ..    */..    In
2ec2d 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20 20 20  dex *pIdx;..    
2ec2e 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
2ec2f 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
2ec30 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0d  x=pIdx->pNext){.
2ec31 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0d 0a 20  .      int k;.. 
2ec32 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
2ec33 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
2ec34 6f 6e 65 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  one );..      as
2ec35 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74 6f  sert( pIdx->auto
2ec36 49 6e 64 65 78 20 29 3b 0d 0a 20 20 20 20 20 20  Index );..      
2ec37 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e  assert( pIndex->
2ec38 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2ec39 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28   );....      if(
2ec3a 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d   pIdx->nColumn!=
2ec3b 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  pIndex->nColumn 
2ec3c 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
2ec3d 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49     for(k=0; k<pI
2ec3e 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b  dx->nColumn; k++
2ec3f 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  ){..        cons
2ec40 74 20 63 68 61 72 20 2a 7a 31 3b 0d 0a 20 20 20  t char *z1;..   
2ec41 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2ec42 2a 7a 32 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  *z2;..        if
2ec43 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
2ec44 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43  [k]!=pIndex->aiC
2ec45 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b  olumn[k] ) break
2ec46 3b 0d 0a 20 20 20 20 20 20 20 20 7a 31 20 3d 20  ;..        z1 = 
2ec47 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b  pIdx->azColl[k];
2ec48 0d 0a 20 20 20 20 20 20 20 20 7a 32 20 3d 20 70  ..        z2 = p
2ec49 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d  Index->azColl[k]
2ec4a 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  ;..        if( z
2ec4b 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 65 33  1!=z2 && sqlite3
2ec4c 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20  StrICmp(z1, z2) 
2ec4d 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
2ec4e 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d  }..      if( k==
2ec4f 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pIdx->nColumn ){
2ec50 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ..        if( pI
2ec51 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e  dx->onError!=pIn
2ec52 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0d  dex->onError ){.
2ec53 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
2ec54 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72  is constraint cr
2ec55 65 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 69  eates the same i
2ec56 6e 64 65 78 20 61 73 20 61 20 70 72 65 76 69 6f  ndex as a previo
2ec57 75 73 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  us..          **
2ec58 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63   constraint spec
2ec59 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20  ified somewhere 
2ec5a 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
2ec5b 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a  BLE statement...
2ec5c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77            ** How
2ec5d 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46  ever the ON CONF
2ec5e 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 65  LICT clauses are
2ec5f 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62   different. If b
2ec60 6f 74 68 20 74 68 69 73 20 0d 0a 20 20 20 20 20  oth this ..     
2ec61 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69       ** constrai
2ec62 6e 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69  nt and the previ
2ec63 6f 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63  ous equivalent c
2ec64 6f 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20 65  onstraint have e
2ec65 78 70 6c 69 63 69 74 0d 0a 20 20 20 20 20 20 20  xplicit..       
2ec66 20 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43     ** ON CONFLIC
2ec67 54 20 63 6c 61 75 73 65 73 20 74 68 69 73 20 69  T clauses this i
2ec68 73 20 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65  s an error. Othe
2ec69 72 77 69 73 65 2c 20 75 73 65 20 74 68 65 0d 0a  rwise, use the..
2ec6a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70            ** exp
2ec6b 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65  licitly specifie
2ec6c 64 20 62 65 68 61 76 69 6f 75 72 20 66 6f 72 20  d behaviour for 
2ec6d 74 68 65 20 69 6e 64 65 78 2e 0d 0a 20 20 20 20  the index...    
2ec6e 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
2ec6f 20 20 20 20 69 66 28 20 21 28 70 49 64 78 2d 3e      if( !(pIdx->
2ec70 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
2ec71 75 6c 74 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f  ult || pIndex->o
2ec72 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
2ec73 6c 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  lt) ){..        
2ec74 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ec75 4d 73 67 28 70 50 61 72 73 65 2c 20 0d 0a 20 20  Msg(pParse, ..  
2ec76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
2ec77 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20 43 4f  onflicting ON CO
2ec78 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 73  NFLICT clauses s
2ec79 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b 0d 0a  pecified", 0);..
2ec7a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
2ec7b 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
2ec7c 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66  >onError==OE_Def
2ec7d 61 75 6c 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  ault ){..       
2ec7e 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72       pIdx->onErr
2ec7f 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45  or = pIndex->onE
2ec80 72 72 6f 72 3b 0d 0a 20 20 20 20 20 20 20 20 20  rror;..         
2ec81 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20   }..        }.. 
2ec82 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
2ec83 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a  _create_index;..
2ec84 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
2ec85 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c 69 6e 6b    }....  /* Link
2ec86 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73   the new Index s
2ec87 74 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20  tructure to its 
2ec88 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65  table and to the
2ec89 20 6f 74 68 65 72 0d 0a 20 20 2a 2a 20 69 6e 2d   other..  ** in-
2ec8a 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
2ec8b 73 74 72 75 63 74 75 72 65 73 2e 20 0d 0a 20 20  structures. ..  
2ec8c 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e  */..  if( db->in
2ec8d 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20 20 20  it.busy ){..    
2ec8e 49 6e 64 65 78 20 2a 70 3b 0d 0a 20 20 20 20 61  Index *p;..    a
2ec8f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
2ec90 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
2ec91 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  , 0, pIndex->pSc
2ec92 68 65 6d 61 29 20 29 3b 0d 0a 20 20 20 20 70 20  hema) );..    p 
2ec93 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2ec94 65 72 74 28 26 70 49 6e 64 65 78 2d 3e 70 53 63  ert(&pIndex->pSc
2ec95 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 0d  hema->idxHash, .
2ec96 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ec97 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64 65             pInde
2ec98 78 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  x->zName, sqlite
2ec99 33 53 74 72 6c 65 6e 33 30 28 70 49 6e 64 65 78  3Strlen30(pIndex
2ec9a 2d 3e 7a 4e 61 6d 65 29 2c 0d 0a 20 20 20 20 20  ->zName),..     
2ec9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec9c 20 20 20 20 20 70 49 6e 64 65 78 29 3b 0d 0a 20       pIndex);.. 
2ec9d 20 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20     if( p ){..   
2ec9e 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49     assert( p==pI
2ec9f 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c  ndex );  /* Mall
2eca0 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69  oc must have fai
2eca1 6c 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20 64 62  led */..      db
2eca2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2eca3 20 31 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20   1;..      goto 
2eca4 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2eca5 78 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64  x;..    }..    d
2eca6 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
2eca7 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
2eca8 3b 0d 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e  ;..    if( pTblN
2eca9 61 6d 65 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ame!=0 ){..     
2ecaa 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20   pIndex->tnum = 
2ecab 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
2ecac 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
2ecad 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d  .  /* If the db-
2ecae 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20  >init.busy is 0 
2ecaf 74 68 65 6e 20 63 72 65 61 74 65 20 74 68 65 20  then create the 
2ecb0 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20 20  index on disk.  
2ecb1 54 68 69 73 0d 0a 20 20 2a 2a 20 69 6e 76 6f 6c  This..  ** invol
2ecb2 76 65 73 20 77 72 69 74 69 6e 67 20 74 68 65 20  ves writing the 
2ecb3 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d  index into the m
2ecb4 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
2ecb5 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0d 0a  filling in the..
2ecb6 20 20 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20    ** index with 
2ecb7 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
2ecb8 65 20 63 6f 6e 74 65 6e 74 73 2e 0d 0a 20 20 2a  e contents...  *
2ecb9 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 64 62 2d 3e  *..  ** The db->
2ecba 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20 77  init.busy is 0 w
2ecbb 68 65 6e 20 74 68 65 20 75 73 65 72 20 66 69 72  hen the user fir
2ecbc 73 74 20 65 6e 74 65 72 73 20 61 20 43 52 45 41  st enters a CREA
2ecbd 54 45 20 49 4e 44 45 58 20 0d 0a 20 20 2a 2a 20  TE INDEX ..  ** 
2ecbe 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e 69 6e  command.  db->in
2ecbf 69 74 2e 62 75 73 79 20 69 73 20 31 20 77 68 65  it.busy is 1 whe
2ecc0 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  n a database is 
2ecc1 6f 70 65 6e 65 64 20 61 6e 64 20 0d 0a 20 20 2a  opened and ..  *
2ecc2 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  * CREATE INDEX s
2ecc3 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72 65  tatements are re
2ecc4 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61  ad out of the ma
2ecc5 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e 0d  ster table.  In.
2ecc6 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72  .  ** the latter
2ecc7 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20   case the index 
2ecc8 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f  already exists o
2ecc9 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73  n disk, which is
2ecca 20 77 68 79 0d 0a 20 20 2a 2a 20 77 65 20 64 6f   why..  ** we do
2eccb 6e 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72  n't want to recr
2eccc 65 61 74 65 20 69 74 2e 0d 0a 20 20 2a 2a 0d 0a  eate it...  **..
2eccd 20 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65    ** If pTblName
2ecce 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68 69  ==0 it means thi
2eccf 73 20 69 6e 64 65 78 20 69 73 20 67 65 6e 65 72  s index is gener
2ecd0 61 74 65 64 20 61 73 20 61 20 70 72 69 6d 61 72  ated as a primar
2ecd1 79 20 6b 65 79 0d 0a 20 20 2a 2a 20 6f 72 20 55  y key..  ** or U
2ecd2 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
2ecd3 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42   of a CREATE TAB
2ecd4 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53  LE statement.  S
2ecd5 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 0d 0a  ince the table..
2ecd6 20 20 2a 2a 20 68 61 73 20 6a 75 73 74 20 62 65    ** has just be
2ecd7 65 6e 20 63 72 65 61 74 65 64 2c 20 69 74 20 63  en created, it c
2ecd8 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 20  ontains no data 
2ecd9 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e  and the index in
2ecda 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0d 0a 20 20  itialization..  
2ecdb 2a 2a 20 73 74 65 70 20 63 61 6e 20 62 65 20 73  ** step can be s
2ecdc 6b 69 70 70 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20  kipped...  */.. 
2ecdd 20 65 6c 73 65 7b 20 2f 2a 20 69 66 28 20 64 62   else{ /* if( db
2ecde 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
2ecdf 20 2a 2f 0d 0a 20 20 20 20 56 64 62 65 20 2a 76   */..    Vdbe *v
2ece0 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74  ;..    char *zSt
2ece1 6d 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69 4d 65  mt;..    int iMe
2ece2 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
2ece3 65 6d 3b 0d 0a 0d 0a 20 20 20 20 76 20 3d 20 73  em;....    v = s
2ece4 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2ece5 61 72 73 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  arse);..    if( 
2ece6 76 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74  v==0 ) goto exit
2ece7 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0d 0a  _create_index;..
2ece8 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  ....    /* Creat
2ece9 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66  e the rootpage f
2ecea 6f 72 20 74 68 65 20 69 6e 64 65 78 0d 0a 20 20  or the index..  
2eceb 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65    */..    sqlite
2ecec 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2eced 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
2ecee 69 44 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  iDb);..    sqlit
2ecef 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2ecf0 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20  OP_CreateIndex, 
2ecf1 69 44 62 2c 20 69 4d 65 6d 29 3b 0d 0a 0d 0a 20  iDb, iMem);.... 
2ecf2 20 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68 65     /* Gather the
2ecf3 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   complete text o
2ecf4 66 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44  f the CREATE IND
2ecf5 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74  EX statement int
2ecf6 6f 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53  o..    ** the zS
2ecf7 74 6d 74 20 76 61 72 69 61 62 6c 65 0d 0a 20 20  tmt variable..  
2ecf8 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 53    */..    if( pS
2ecf9 74 61 72 74 20 29 7b 0d 0a 20 20 20 20 20 20 61  tart ){..      a
2ecfa 73 73 65 72 74 28 20 70 45 6e 64 21 3d 30 20 29  ssert( pEnd!=0 )
2ecfb 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61  ;..      /* A na
2ecfc 6d 65 64 20 69 6e 64 65 78 20 77 69 74 68 20 61  med index with a
2ecfd 6e 20 65 78 70 6c 69 63 69 74 20 43 52 45 41 54  n explicit CREAT
2ecfe 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e  E INDEX statemen
2ecff 74 20 2a 2f 0d 0a 20 20 20 20 20 20 7a 53 74 6d  t */..      zStm
2ed00 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
2ed01 74 66 28 64 62 2c 20 22 43 52 45 41 54 45 25 73  tf(db, "CREATE%s
2ed02 20 49 4e 44 45 58 20 25 2e 2a 73 22 2c 0d 0a 20   INDEX %.*s",.. 
2ed03 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 3d 3d         onError==
2ed04 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20 3a 20 22  OE_None ? "" : "
2ed05 20 55 4e 49 51 55 45 22 2c 0d 0a 20 20 20 20 20   UNIQUE",..     
2ed06 20 20 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a     (int)(pEnd->z
2ed07 20 2d 20 70 4e 61 6d 65 2d 3e 7a 29 20 2b 20 31   - pName->z) + 1
2ed08 2c 0d 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65  ,..        pName
2ed09 2d 3e 7a 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  ->z);..    }else
2ed0a 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61  {..      /* An a
2ed0b 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63  utomatic index c
2ed0c 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d  reated by a PRIM
2ed0d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
2ed0e 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d  E constraint */.
2ed0f 0a 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20  .      /* zStmt 
2ed10 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2ed11 28 22 22 29 3b 20 2a 2f 0d 0a 20 20 20 20 20 20  (""); */..      
2ed12 7a 53 74 6d 74 20 3d 20 30 3b 0d 0a 20 20 20 20  zStmt = 0;..    
2ed13 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 64 64 20  }....    /* Add 
2ed14 61 6e 20 65 6e 74 72 79 20 69 6e 20 73 71 6c 69  an entry in sqli
2ed15 74 65 5f 6d 61 73 74 65 72 20 66 6f 72 20 74 68  te_master for th
2ed16 69 73 20 69 6e 64 65 78 0d 0a 20 20 20 20 2a 2f  is index..    */
2ed17 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ..    sqlite3Nes
2ed18 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2ed19 20 0d 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45   ..        "INSE
2ed1a 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41  RT INTO %Q.%s VA
2ed1b 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51 2c  LUES('index',%Q,
2ed1c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0d 0a 20  %Q,#%d,%Q);",.. 
2ed1d 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69         db->aDb[i
2ed1e 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
2ed1f 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0d 0a 20  A_TABLE(iDb),.. 
2ed20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a         pIndex->z
2ed21 4e 61 6d 65 2c 0d 0a 20 20 20 20 20 20 20 20 70  Name,..        p
2ed22 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0d 0a 20 20 20  Tab->zName,..   
2ed23 20 20 20 20 20 69 4d 65 6d 2c 0d 0a 20 20 20 20       iMem,..    
2ed24 20 20 20 20 7a 53 74 6d 74 0d 0a 20 20 20 20 29      zStmt..    )
2ed25 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
2ed26 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b  Free(db, zStmt);
2ed27 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20  ....    /* Fill 
2ed28 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64  the index with d
2ed29 61 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20  ata and reparse 
2ed2a 74 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65  the schema. Code
2ed2b 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 0d 0a 20   an OP_Expire.. 
2ed2c 20 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64     ** to invalid
2ed2d 61 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70  ate all pre-comp
2ed2e 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  iled statements.
2ed2f 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
2ed30 28 20 70 54 62 6c 4e 61 6d 65 20 29 7b 0d 0a 20  ( pTblName ){.. 
2ed31 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
2ed32 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
2ed33 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29 3b 0d 0a  pIndex, iMem);..
2ed34 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 68 61        sqlite3Cha
2ed35 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
2ed36 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 73  , iDb);..      s
2ed37 71 6c 69 74 65 33 56 64 62 65 41 64 64 50 61 72  qlite3VdbeAddPar
2ed38 73 65 53 63 68 65 6d 61 4f 70 28 76 2c 20 69 44  seSchemaOp(v, iD
2ed39 62 2c 0d 0a 20 20 20 20 20 20 20 20 20 73 71 6c  b,..         sql
2ed3a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2ed3b 22 6e 61 6d 65 3d 27 25 71 27 20 41 4e 44 20 74  "name='%q' AND t
2ed3c 79 70 65 3d 27 69 6e 64 65 78 27 22 2c 20 70 49  ype='index'", pI
2ed3d 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a  ndex->zName));..
2ed3e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ed3f 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78  eAddOp1(v, OP_Ex
2ed40 70 69 72 65 2c 20 30 29 3b 0d 0a 20 20 20 20 7d  pire, 0);..    }
2ed41 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 57 68  ..  }....  /* Wh
2ed42 65 6e 20 61 64 64 69 6e 67 20 61 6e 20 69 6e 64  en adding an ind
2ed43 65 78 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  ex to the list o
2ed44 66 20 69 6e 64 69 63 65 73 20 66 6f 72 20 61 20  f indices for a 
2ed45 74 61 62 6c 65 2c 20 6d 61 6b 65 0d 0a 20 20 2a  table, make..  *
2ed46 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64 69 63  * sure all indic
2ed47 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52 65  es labeled OE_Re
2ed48 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74 65 72  place come after
2ed49 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65 6c   all those label
2ed4a 65 64 0d 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f  ed..  ** OE_Igno
2ed4b 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  re.  This is nec
2ed4c 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 20 63  essary for the c
2ed4d 6f 72 72 65 63 74 20 63 6f 6e 73 74 72 61 69 6e  orrect constrain
2ed4e 74 20 63 68 65 63 6b 0d 0a 20 20 2a 2a 20 70 72  t check..  ** pr
2ed4f 6f 63 65 73 73 69 6e 67 20 28 69 6e 20 73 71 6c  ocessing (in sql
2ed50 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
2ed51 74 72 61 69 6e 74 43 68 65 63 6b 73 28 29 29 20  traintChecks()) 
2ed52 61 73 20 70 61 72 74 20 6f 66 0d 0a 20 20 2a 2a  as part of..  **
2ed53 20 55 50 44 41 54 45 20 61 6e 64 20 49 4e 53 45   UPDATE and INSE
2ed54 52 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  RT statements.  
2ed55 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62  ..  */..  if( db
2ed56 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70  ->init.busy || p
2ed57 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0d 0a 20  TblName==0 ){.. 
2ed58 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d     if( onError!=
2ed59 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54  OE_Replace || pT
2ed5a 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0d 0a 20  ab->pIndex==0.. 
2ed5b 20 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d          || pTab-
2ed5c 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72  >pIndex->onError
2ed5d 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0d 0a  ==OE_Replace){..
2ed5e 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e        pIndex->pN
2ed5f 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64  ext = pTab->pInd
2ed60 65 78 3b 0d 0a 20 20 20 20 20 20 70 54 61 62 2d  ex;..      pTab-
2ed61 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78  >pIndex = pIndex
2ed62 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
2ed63 20 20 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68       Index *pOth
2ed64 65 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  er = pTab->pInde
2ed65 78 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28  x;..      while(
2ed66 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 26   pOther->pNext &
2ed67 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d  & pOther->pNext-
2ed68 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70  >onError!=OE_Rep
2ed69 6c 61 63 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  lace ){..       
2ed6a 20 70 4f 74 68 65 72 20 3d 20 70 4f 74 68 65 72   pOther = pOther
2ed6b 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20  ->pNext;..      
2ed6c 7d 0d 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  }..      pIndex-
2ed6d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d  >pNext = pOther-
2ed6e 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 70  >pNext;..      p
2ed6f 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70  Other->pNext = p
2ed70 49 6e 64 65 78 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Index;..    }.. 
2ed71 20 20 20 70 52 65 74 20 3d 20 70 49 6e 64 65 78     pRet = pIndex
2ed72 3b 0d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20  ;..    pIndex = 
2ed73 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  0;..  }....  /* 
2ed74 43 6c 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20  Clean up before 
2ed75 65 78 69 74 69 6e 67 20 2a 2f 0d 0a 65 78 69 74  exiting */..exit
2ed76 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3a 0d 0a  _create_index:..
2ed77 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0d    if( pIndex ){.
2ed78 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2ed79 65 65 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 7a  ee(db, pIndex->z
2ed7a 43 6f 6c 41 66 66 29 3b 0d 0a 20 20 20 20 73 71  ColAff);..    sq
2ed7b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ed7c 70 49 6e 64 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20  pIndex);..  }.. 
2ed7d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2ed7e 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
2ed7f 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 72 63  );..  sqlite3Src
2ed80 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2ed81 54 62 6c 4e 61 6d 65 29 3b 0d 0a 20 20 73 71 6c  TblName);..  sql
2ed82 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2ed83 4e 61 6d 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e  Name);..  return
2ed84 20 70 52 65 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pRet;..}..../*.
2ed85 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 64  .** Fill the Ind
2ed86 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61 72  ex.aiRowEst[] ar
2ed87 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c 74  ray with default
2ed88 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 69   information - i
2ed89 6e 66 6f 72 6d 61 74 69 6f 6e 0d 0a 2a 2a 20 74  nformation..** t
2ed8a 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77  o be used when w
2ed8b 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74  e have not run t
2ed8c 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
2ed8d 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 61 69 52 6f  nd...**..** aiRo
2ed8e 77 45 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f  wEst[0] is suppo
2ed8f 73 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  se to contain th
2ed90 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
2ed91 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65  ents in the inde
2ed92 78 2e 0d 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20  x...** Since we 
2ed93 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65  do not know, gue
2ed94 73 73 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61  ss 1 million.  a
2ed95 69 52 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 6e  iRowEst[1] is an
2ed96 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
2ed97 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72  ..** number of r
2ed98 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
2ed99 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20   that match any 
2ed9a 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65  particular value
2ed9b 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 66 69 72 73   of the..** firs
2ed9c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
2ed9d 69 6e 64 65 78 2e 20 20 61 69 52 6f 77 45 73 74  index.  aiRowEst
2ed9e 5b 32 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61  [2] is an estima
2ed9f 74 65 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  te of the number
2eda0 0d 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68 61  ..** of rows tha
2eda1 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74  t match any part
2eda2 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69 61 74  icular combiniat
2eda3 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74  ion of the first
2eda4 20 32 20 63 6f 6c 75 6d 6e 73 0d 0a 2a 2a 20 6f   2 columns..** o
2eda5 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 41 6e  f the index.  An
2eda6 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 74 20  d so forth.  It 
2eda7 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20 74  must always be t
2eda8 68 65 20 63 61 73 65 20 74 68 61 74 0d 0a 2a 0d  he case that..*.
2eda9 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69  .**           ai
2edaa 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77  RowEst[N]<=aiRow
2edab 45 73 74 5b 4e 2d 31 5d 0d 0a 2a 2a 20 20 20 20  Est[N-1]..**    
2edac 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b         aiRowEst[
2edad 4e 5d 3e 3d 31 0d 0a 2a 2a 0d 0a 2a 2a 20 41 70  N]>=1..**..** Ap
2edae 61 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77  art from that, w
2edaf 65 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f  e have little to
2edb0 20 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69   go on besides i
2edb1 6e 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0d 0a  ntuition as to..
2edb2 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b  ** how aiRowEst[
2edb3 5d 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74  ] should be init
2edb4 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75  ialized.  The nu
2edb5 6d 62 65 72 73 20 67 65 6e 65 72 61 74 65 64 20  mbers generated 
2edb6 68 65 72 65 0d 0a 2a 2a 20 61 72 65 20 62 61 73  here..** are bas
2edb7 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20 76 61  ed on typical va
2edb8 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20 61 63  lues found in ac
2edb9 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0d 0a 2a  tual indices...*
2edba 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2edbb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2edbc 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65  faultRowEst(Inde
2edbd 78 20 2a 70 49 64 78 29 7b 0d 0a 20 20 74 52 6f  x *pIdx){..  tRo
2edbe 77 63 6e 74 20 2a 61 20 3d 20 70 49 64 78 2d 3e  wcnt *a = pIdx->
2edbf 61 69 52 6f 77 45 73 74 3b 0d 0a 20 20 69 6e 74  aiRowEst;..  int
2edc0 20 69 3b 0d 0a 20 20 74 52 6f 77 63 6e 74 20 6e   i;..  tRowcnt n
2edc1 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 21 3d  ;..  assert( a!=
2edc2 30 20 29 3b 0d 0a 20 20 61 5b 30 5d 20 3d 20 70  0 );..  a[0] = p
2edc3 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 6e 52 6f  Idx->pTable->nRo
2edc4 77 45 73 74 3b 0d 0a 20 20 69 66 28 20 61 5b 30  wEst;..  if( a[0
2edc5 5d 3c 31 30 20 29 20 61 5b 30 5d 20 3d 20 31 30  ]<10 ) a[0] = 10
2edc6 3b 0d 0a 20 20 6e 20 3d 20 31 30 3b 0d 0a 20 20  ;..  n = 10;..  
2edc7 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 49 64 78  for(i=1; i<=pIdx
2edc8 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
2edc9 0d 0a 20 20 20 20 61 5b 69 5d 20 3d 20 6e 3b 0d  ..    a[i] = n;.
2edca 0a 20 20 20 20 69 66 28 20 6e 3e 35 20 29 20 6e  .    if( n>5 ) n
2edcb 2d 2d 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  --;..  }..  if( 
2edcc 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
2edcd 45 5f 4e 6f 6e 65 20 29 7b 0d 0a 20 20 20 20 61  E_None ){..    a
2edce 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  [pIdx->nColumn] 
2edcf 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  = 1;..  }..}....
2edd0 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
2edd1 69 6e 65 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e  ine will drop an
2edd2 20 65 78 69 73 74 69 6e 67 20 6e 61 6d 65 64 20   existing named 
2edd3 69 6e 64 65 78 2e 20 20 54 68 69 73 20 72 6f 75  index.  This rou
2edd4 74 69 6e 65 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65  tine..** impleme
2edd5 6e 74 73 20 74 68 65 20 44 52 4f 50 20 49 4e 44  nts the DROP IND
2edd6 45 58 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a  EX statement...*
2edd7 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2edd8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
2edd9 6f 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70  opIndex(Parse *p
2edda 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
2eddb 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 78 69  pName, int ifExi
2eddc 73 74 73 29 7b 0d 0a 20 20 49 6e 64 65 78 20 2a  sts){..  Index *
2eddd 70 49 6e 64 65 78 3b 0d 0a 20 20 56 64 62 65 20  pIndex;..  Vdbe 
2edde 2a 76 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  *v;..  sqlite3 *
2eddf 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2ede0 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d 0a 0d 0a  ..  int iDb;....
2ede1 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2ede2 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 2f  ->nErr==0 );   /
2ede3 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20 77  * Never called w
2ede4 69 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72 73  ith prior errors
2ede5 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d   */..  if( db->m
2ede6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a  allocFailed ){..
2ede7 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2ede8 6f 70 5f 69 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a  op_index;..  }..
2ede9 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d    assert( pName-
2edea 3e 6e 53 72 63 3d 3d 31 20 29 3b 0d 0a 20 20 69  >nSrc==1 );..  i
2edeb 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
2edec 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2eded 70 50 61 72 73 65 29 20 29 7b 0d 0a 20 20 20 20  pParse) ){..    
2edee 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
2edef 6e 64 65 78 3b 0d 0a 20 20 7d 0d 0a 20 20 70 49  ndex;..  }..  pI
2edf0 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69  ndex = sqlite3Fi
2edf1 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e 61 6d  ndIndex(db, pNam
2edf2 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70  e->a[0].zName, p
2edf3 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
2edf4 62 61 73 65 29 3b 0d 0a 20 20 69 66 28 20 70 49  base);..  if( pI
2edf5 6e 64 65 78 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ndex==0 ){..    
2edf6 69 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b  if( !ifExists ){
2edf7 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
2edf8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2edf9 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20  "no such index: 
2edfa 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0d  %S", pName, 0);.
2edfb 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
2edfc 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65     sqlite3CodeVe
2edfd 72 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28  rifyNamedSchema(
2edfe 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61  pParse, pName->a
2edff 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0d  [0].zDatabase);.
2ee00 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50 61 72  .    }..    pPar
2ee01 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20  se->checkSchema 
2ee02 3d 20 31 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65  = 1;..    goto e
2ee03 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0d  xit_drop_index;.
2ee04 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 49 6e 64  .  }..  if( pInd
2ee05 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b  ex->autoIndex ){
2ee06 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
2ee07 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
2ee08 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
2ee09 77 69 74 68 20 55 4e 49 51 55 45 20 22 0d 0a 20  with UNIQUE ".. 
2ee0a 20 20 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59       "or PRIMARY
2ee0b 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20   KEY constraint 
2ee0c 63 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65  cannot be droppe
2ee0d 64 22 2c 20 30 29 3b 0d 0a 20 20 20 20 67 6f 74  d", 0);..    got
2ee0e 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
2ee0f 78 3b 0d 0a 20 20 7d 0d 0a 20 20 69 44 62 20 3d  x;..  }..  iDb =
2ee10 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2ee11 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78  Index(db, pIndex
2ee12 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 23 69 66  ->pSchema);..#if
2ee13 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ee14 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a  _AUTHORIZATION..
2ee15 20 20 7b 0d 0a 20 20 20 20 69 6e 74 20 63 6f 64    {..    int cod
2ee16 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
2ee17 49 4e 44 45 58 3b 0d 0a 20 20 20 20 54 61 62 6c  INDEX;..    Tabl
2ee18 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78  e *pTab = pIndex
2ee19 2d 3e 70 54 61 62 6c 65 3b 0d 0a 20 20 20 20 63  ->pTable;..    c
2ee1a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
2ee1b 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2ee1c 61 6d 65 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  ame;..    const 
2ee1d 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48  char *zTab = SCH
2ee1e 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0d  EMA_TABLE(iDb);.
2ee1f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2ee20 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2ee21 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
2ee22 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29   zTab, 0, zDb) )
2ee23 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  {..      goto ex
2ee24 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0d 0a  it_drop_index;..
2ee25 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 21      }..    if( !
2ee26 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
2ee27 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49  Db ) code = SQLI
2ee28 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
2ee29 45 58 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c  EX;..    if( sql
2ee2a 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2ee2b 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64  arse, code, pInd
2ee2c 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ex->zName, pTab-
2ee2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0d  >zName, zDb) ){.
2ee2e 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
2ee2f 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0d 0a 20 20  _drop_index;..  
2ee30 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66    }..  }..#endif
2ee31 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  ....  /* Generat
2ee32 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
2ee33 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 66   the index and f
2ee34 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74  rom the master t
2ee35 61 62 6c 65 20 2a 2f 0d 0a 20 20 76 20 3d 20 73  able */..  v = s
2ee36 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2ee37 61 72 73 65 29 3b 0d 0a 20 20 69 66 28 20 76 20  arse);..  if( v 
2ee38 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ){..    sqlite3B
2ee39 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
2ee3a 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
2ee3b 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  b);..    sqlite3
2ee3c 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
2ee3d 73 65 2c 0d 0a 20 20 20 20 20 20 20 22 44 45 4c  se,..       "DEL
2ee3e 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57  ETE FROM %Q.%s W
2ee3f 48 45 52 45 20 6e 61 6d 65 3d 25 51 20 41 4e 44  HERE name=%Q AND
2ee40 20 74 79 70 65 3d 27 69 6e 64 65 78 27 22 2c 0d   type='index'",.
2ee41 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  .       db->aDb[
2ee42 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
2ee43 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70  MA_TABLE(iDb), p
2ee44 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0d 0a 20 20  Index->zName..  
2ee45 20 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65    );..    sqlite
2ee46 33 43 6c 65 61 72 53 74 61 74 54 61 62 6c 65 73  3ClearStatTables
2ee47 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 69  (pParse, iDb, "i
2ee48 64 78 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  dx", pIndex->zNa
2ee49 6d 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  me);..    sqlite
2ee4a 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
2ee4b 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a 20 20 20  arse, iDb);..   
2ee4c 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
2ee4d 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2d  (pParse, pIndex-
2ee4e 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0d 0a 20 20  >tnum, iDb);..  
2ee4f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ee50 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49 6e  Op4(v, OP_DropIn
2ee51 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  dex, iDb, 0, 0, 
2ee52 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30  pIndex->zName, 0
2ee53 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 65 78 69 74 5f  );..  }....exit_
2ee54 64 72 6f 70 5f 69 6e 64 65 78 3a 0d 0a 20 20 73  drop_index:..  s
2ee55 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
2ee56 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0d  ete(db, pName);.
2ee57 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 70 41 72  .}..../*..** pAr
2ee58 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ray is a pointer
2ee59 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
2ee5a 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f  objects.  Each o
2ee5b 62 6a 65 63 74 20 69 6e 20 74 68 65 0d 0a 2a 2a  bject in the..**
2ee5c 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72   array is szEntr
2ee5d 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  y bytes in size.
2ee5e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
2ee5f 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0d 0a  llocates a new..
2ee60 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65  ** object on the
2ee61 20 65 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61   end of the arra
2ee62 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 2a 70 6e 45 6e  y...**..** *pnEn
2ee63 74 72 79 20 69 73 20 74 68 65 20 6e 75 6d 62 65  try is the numbe
2ee64 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 72  r of entries alr
2ee65 65 61 64 79 20 69 6e 20 75 73 65 2e 20 20 2a 70  eady in use.  *p
2ee66 6e 41 6c 6c 6f 63 20 69 73 0d 0a 2a 2a 20 74 68  nAlloc is..** th
2ee67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
2ee68 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 74  ocated size of t
2ee69 68 65 20 61 72 72 61 79 2e 20 20 69 6e 69 74 53  he array.  initS
2ee6a 69 7a 65 20 69 73 20 74 68 65 0d 0a 2a 2a 20 73  ize is the..** s
2ee6b 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
2ee6c 20 61 72 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f   array size allo
2ee6d 63 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  cation...**..** 
2ee6e 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  The index of the
2ee6f 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 72 65   new entry is re
2ee70 74 75 72 6e 65 64 20 69 6e 20 2a 70 49 64 78 2e  turned in *pIdx.
2ee71 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ..**..** This ro
2ee72 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20  utine returns a 
2ee73 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
2ee74 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e  rray of objects.
2ee75 20 20 54 68 69 73 0d 0a 2a 2a 20 6d 69 67 68 74    This..** might
2ee76 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
2ee77 74 68 65 20 70 41 72 72 61 79 20 70 61 72 61 6d  the pArray param
2ee78 65 74 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74  eter or it might
2ee79 20 62 65 20 61 20 64 69 66 66 65 72 65 6e 74 0d   be a different.
2ee7a 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 74  .** pointer if t
2ee7b 68 65 20 61 72 72 61 79 20 77 61 73 20 72 65 73  he array was res
2ee7c 69 7a 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ized...*/..SQLIT
2ee7d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
2ee7e 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f  sqlite3ArrayAllo
2ee7f 63 61 74 65 28 0d 0a 20 20 73 71 6c 69 74 65 33  cate(..  sqlite3
2ee80 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43 6f   *db,      /* Co
2ee81 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
2ee82 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69  fy of malloc fai
2ee83 6c 75 72 65 73 20 2a 2f 0d 0a 20 20 76 6f 69 64  lures */..  void
2ee84 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a   *pArray,     /*
2ee85 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   Array of object
2ee86 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61  s.  Might be rea
2ee87 6c 6c 6f 63 61 74 65 64 20 2a 2f 0d 0a 20 20 69  llocated */..  i
2ee88 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20  nt szEntry,     
2ee89 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
2ee8a 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61   object in the a
2ee8b 72 72 61 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  rray */..  int i
2ee8c 6e 69 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20  nitSize,     /* 
2ee8d 53 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61  Suggested initia
2ee8e 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  l allocation, in
2ee8f 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20   elements */..  
2ee90 69 6e 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20  int *pnEntry,   
2ee91 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f    /* Number of o
2ee92 62 6a 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79  bjects currently
2ee93 20 69 6e 20 75 73 65 20 2a 2f 0d 0a 20 20 69 6e   in use */..  in
2ee94 74 20 2a 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20  t *pnAlloc,     
2ee95 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65 20  /* Current size 
2ee96 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
2ee97 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a  n, in elements *
2ee98 2f 0d 0a 20 20 69 6e 74 20 2a 70 49 64 78 20 20  /..  int *pIdx  
2ee99 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2ee9a 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 6e  the index of a n
2ee9b 65 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f 0d  ew slot here */.
2ee9c 0a 29 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d  .){..  char *z;.
2ee9d 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74 72 79 20  .  if( *pnEntry 
2ee9e 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0d 0a  >= *pnAlloc ){..
2ee9f 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0d      void *pNew;.
2eea0 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65  .    int newSize
2eea1 3b 0d 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d  ;..    newSize =
2eea2 20 28 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20   (*pnAlloc)*2 + 
2eea3 69 6e 69 74 53 69 7a 65 3b 0d 0a 20 20 20 20 70  initSize;..    p
2eea4 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
2eea5 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61  ealloc(db, pArra
2eea6 79 2c 20 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74  y, newSize*szEnt
2eea7 72 79 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 4e  ry);..    if( pN
2eea8 65 77 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ew==0 ){..      
2eea9 2a 70 49 64 78 20 3d 20 2d 31 3b 0d 0a 20 20 20  *pIdx = -1;..   
2eeaa 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79     return pArray
2eeab 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 2a 70  ;..    }..    *p
2eeac 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  nAlloc = sqlite3
2eead 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c  DbMallocSize(db,
2eeae 20 70 4e 65 77 29 2f 73 7a 45 6e 74 72 79 3b 0d   pNew)/szEntry;.
2eeaf 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e  .    pArray = pN
2eeb0 65 77 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 20 3d 20  ew;..  }..  z = 
2eeb1 28 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0d 0a  (char*)pArray;..
2eeb2 20 20 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45    memset(&z[*pnE
2eeb3 6e 74 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c  ntry * szEntry],
2eeb4 20 30 2c 20 73 7a 45 6e 74 72 79 29 3b 0d 0a 20   0, szEntry);.. 
2eeb5 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72   *pIdx = *pnEntr
2eeb6 79 3b 0d 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72 79  y;..  ++*pnEntry
2eeb7 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 41 72 72  ;..  return pArr
2eeb8 61 79 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ay;..}..../*..**
2eeb9 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20 65 6c   Append a new el
2eeba 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 67 69 76  ement to the giv
2eebb 65 6e 20 49 64 4c 69 73 74 2e 20 20 43 72 65 61  en IdList.  Crea
2eebc 74 65 20 61 20 6e 65 77 20 49 64 4c 69 73 74 20  te a new IdList 
2eebd 69 66 0d 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0d  if..** need be..
2eebe 0a 2a 2a 0d 0a 2a 2a 20 41 20 6e 65 77 20 49 64  .**..** A new Id
2eebf 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64  List is returned
2eec0 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c  , or NULL if mal
2eec1 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0d 0a 2a 2f  loc() fails...*/
2eec2 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2eec3 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
2eec4 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
2eec5 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74  ite3 *db, IdList
2eec6 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a   *pList, Token *
2eec7 70 54 6f 6b 65 6e 29 7b 0d 0a 20 20 69 6e 74 20  pToken){..  int 
2eec8 69 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73 74 3d  i;..  if( pList=
2eec9 3d 30 20 29 7b 0d 0a 20 20 20 20 70 4c 69 73 74  =0 ){..    pList
2eeca 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2eecb 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2eecc 66 28 49 64 4c 69 73 74 29 20 29 3b 0d 0a 20 20  f(IdList) );..  
2eecd 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
2eece 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20   return 0;..    
2eecf 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20  pList->nAlloc = 
2eed0 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4c 69 73 74  0;..  }..  pList
2eed1 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41 72 72  ->a = sqlite3Arr
2eed2 61 79 41 6c 6c 6f 63 61 74 65 28 0d 0a 20 20 20  ayAllocate(..   
2eed3 20 20 20 64 62 2c 0d 0a 20 20 20 20 20 20 70 4c     db,..      pL
2eed4 69 73 74 2d 3e 61 2c 0d 0a 20 20 20 20 20 20 73  ist->a,..      s
2eed5 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30  izeof(pList->a[0
2eed6 5d 29 2c 0d 0a 20 20 20 20 20 20 35 2c 0d 0a 20  ]),..      5,.. 
2eed7 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64       &pList->nId
2eed8 2c 0d 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d  ,..      &pList-
2eed9 3e 6e 41 6c 6c 6f 63 2c 0d 0a 20 20 20 20 20 20  >nAlloc,..      
2eeda 26 69 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20  &i..  );..  if( 
2eedb 69 3c 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  i<0 ){..    sqli
2eedc 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2eedd 64 62 2c 20 70 4c 69 73 74 29 3b 0d 0a 20 20 20  db, pList);..   
2eede 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d   return 0;..  }.
2eedf 0a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  .  pList->a[i].z
2eee0 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
2eee1 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2eee2 70 54 6f 6b 65 6e 29 3b 0d 0a 20 20 72 65 74 75  pToken);..  retu
2eee3 72 6e 20 70 4c 69 73 74 3b 0d 0a 7d 0d 0a 0d 0a  rn pList;..}....
2eee4 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  /*..** Delete an
2eee5 20 49 64 4c 69 73 74 2e 0d 0a 2a 2f 0d 0a 53 51   IdList...*/..SQ
2eee6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2eee7 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  d sqlite3IdListD
2eee8 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
2eee9 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74  b, IdList *pList
2eeea 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
2eeeb 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2eeec 65 74 75 72 6e 3b 0d 0a 20 20 66 6f 72 28 69 3d  eturn;..  for(i=
2eeed 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b  0; i<pList->nId;
2eeee 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69   i++){..    sqli
2eeef 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
2eef0 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ist->a[i].zName)
2eef1 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
2eef2 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
2eef3 74 2d 3e 61 29 3b 0d 0a 20 20 73 71 6c 69 74 65  t->a);..  sqlite
2eef4 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
2eef5 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  t);..}..../*..**
2eef6 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
2eef7 78 20 69 6e 20 70 4c 69 73 74 20 6f 66 20 74 68  x in pList of th
2eef8 65 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d  e identifier nam
2eef9 65 64 20 7a 49 64 2e 20 20 52 65 74 75 72 6e 20  ed zId.  Return 
2eefa 2d 31 0d 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 6f  -1..** if not fo
2eefb 75 6e 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  und...*/..SQLITE
2eefc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2eefd 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
2eefe 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63  IdList *pList, c
2eeff 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2ef00 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ){..  int i;..  
2ef01 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2ef02 65 74 75 72 6e 20 2d 31 3b 0d 0a 20 20 66 6f 72  eturn -1;..  for
2ef03 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2ef04 49 64 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69  Id; i++){..    i
2ef05 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2ef06 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e  p(pList->a[i].zN
2ef07 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
2ef08 20 72 65 74 75 72 6e 20 69 3b 0d 0a 20 20 7d 0d   return i;..  }.
2ef09 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0d 0a 7d  .  return -1;..}
2ef0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 70 61 6e  ..../*..** Expan
2ef0b 64 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f  d the space allo
2ef0c 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 67 69  cated for the gi
2ef0d 76 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65  ven SrcList obje
2ef0e 63 74 20 62 79 0d 0a 2a 2a 20 63 72 65 61 74 69  ct by..** creati
2ef0f 6e 67 20 6e 45 78 74 72 61 20 6e 65 77 20 73 6c  ng nExtra new sl
2ef10 6f 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ots beginning at
2ef11 20 69 53 74 61 72 74 2e 20 20 69 53 74 61 72 74   iStart.  iStart
2ef12 20 69 73 20 7a 65 72 6f 20 62 61 73 65 64 2e 0d   is zero based..
2ef13 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73 20 61 72  .** New slots ar
2ef14 65 20 7a 65 72 6f 65 64 2e 0d 0a 2a 2a 0d 0a 2a  e zeroed...**..*
2ef15 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73  * For example, s
2ef16 75 70 70 6f 73 65 20 61 20 53 72 63 4c 69 73 74  uppose a SrcList
2ef17 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74 61   initially conta
2ef18 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73 3a  ins two entries:
2ef19 20 41 2c 42 2e 0d 0a 2a 2a 20 54 6f 20 61 70 70   A,B...** To app
2ef1a 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72 69 65  end 3 new entrie
2ef1b 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20  s onto the end, 
2ef1c 64 6f 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  do this:..**..**
2ef1d 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
2ef1e 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53  stEnlarge(db, pS
2ef1f 72 63 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0d 0a  rclist, 3, 2);..
2ef20 2a 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  **..** After the
2ef21 20 63 61 6c 6c 20 61 62 6f 76 65 20 69 74 20 77   call above it w
2ef22 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 3a 20 20 41  ould contain:  A
2ef23 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e  , B, nil, nil, n
2ef24 69 6c 2e 0d 0a 2a 2a 20 49 66 20 74 68 65 20 69  il...** If the i
2ef25 53 74 61 72 74 20 61 72 67 75 6d 65 6e 74 20 68  Start argument h
2ef26 61 64 20 62 65 65 6e 20 31 20 69 6e 73 74 65 61  ad been 1 instea
2ef27 64 20 6f 66 20 32 2c 20 74 68 65 6e 20 74 68 65  d of 2, then the
2ef28 20 72 65 73 75 6c 74 0d 0a 2a 2a 20 77 6f 75 6c   result..** woul
2ef29 64 20 68 61 76 65 20 62 65 65 6e 3a 20 20 41 2c  d have been:  A,
2ef2a 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20   nil, nil, nil, 
2ef2b 42 2e 20 20 54 6f 20 70 72 65 70 65 6e 64 20 74  B.  To prepend t
2ef2c 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c 0d 0a 2a  he new slots,..*
2ef2d 2a 20 74 68 65 20 69 53 74 61 72 74 20 76 61 6c  * the iStart val
2ef2e 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e 20 20  ue would be 0.  
2ef2f 54 68 65 20 72 65 73 75 6c 74 20 74 68 65 6e 20  The result then 
2ef30 77 6f 75 6c 64 0d 0a 2a 2a 20 62 65 3a 20 6e 69  would..** be: ni
2ef31 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20  l, nil, nil, A, 
2ef32 42 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20  B...**..** If a 
2ef33 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2ef34 6e 20 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c  n fails the SrcL
2ef35 69 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ist is unchanged
2ef36 2e 20 20 54 68 65 0d 0a 2a 2a 20 64 62 2d 3e 6d  .  The..** db->m
2ef37 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
2ef38 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20   will be set to 
2ef39 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  true...*/..SQLIT
2ef3a 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
2ef3b 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
2ef3c 74 45 6e 6c 61 72 67 65 28 0d 0a 20 20 73 71 6c  tEnlarge(..  sql
2ef3d 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2ef3e 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2ef3f 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79  ection to notify
2ef40 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a   of OOM errors *
2ef41 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  /..  SrcList *pS
2ef42 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 53  rc,     /* The S
2ef43 72 63 4c 69 73 74 20 74 6f 20 62 65 20 65 6e 6c  rcList to be enl
2ef44 61 72 67 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  arged */..  int 
2ef45 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 2f  nExtra,        /
2ef46 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77 20  * Number of new 
2ef47 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74 6f 20  slots to add to 
2ef48 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0d 0a 20 20  pSrc->a[] */..  
2ef49 69 6e 74 20 69 53 74 61 72 74 20 20 20 20 20 20  int iStart      
2ef4a 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70     /* Index in p
2ef4b 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73  Src->a[] of firs
2ef4c 74 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0d 0a 29  t new slot */..)
2ef4d 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20  {..  int i;.... 
2ef4e 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b   /* Sanity check
2ef4f 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e 67 20 70  ing on calling p
2ef50 61 72 61 6d 65 74 65 72 73 20 2a 2f 0d 0a 20 20  arameters */..  
2ef51 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3e 3d  assert( iStart>=
2ef52 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
2ef53 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0d 0a 20 20  nExtra>=1 );..  
2ef54 61 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20  assert( pSrc!=0 
2ef55 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 53  );..  assert( iS
2ef56 74 61 72 74 3c 3d 70 53 72 63 2d 3e 6e 53 72 63  tart<=pSrc->nSrc
2ef57 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f   );....  /* Allo
2ef58 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  cate additional 
2ef59 73 70 61 63 65 20 69 66 20 6e 65 65 64 65 64 20  space if needed 
2ef5a 2a 2f 0d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e  */..  if( pSrc->
2ef5b 6e 53 72 63 2b 6e 45 78 74 72 61 3e 70 53 72 63  nSrc+nExtra>pSrc
2ef5c 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20  ->nAlloc ){..   
2ef5d 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0d   SrcList *pNew;.
2ef5e 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20  .    int nAlloc 
2ef5f 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78  = pSrc->nSrc+nEx
2ef60 74 72 61 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 47  tra;..    int nG
2ef61 6f 74 3b 0d 0a 20 20 20 20 70 4e 65 77 20 3d 20  ot;..    pNew = 
2ef62 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2ef63 28 64 62 2c 20 70 53 72 63 2c 0d 0a 20 20 20 20  (db, pSrc,..    
2ef64 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
2ef65 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c 6c  f(*pSrc) + (nAll
2ef66 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 72  oc-1)*sizeof(pSr
2ef67 63 2d 3e 61 5b 30 5d 29 20 29 3b 0d 0a 20 20 20  c->a[0]) );..   
2ef68 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0d   if( pNew==0 ){.
2ef69 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
2ef6a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ef6b 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  );..      return
2ef6c 20 70 53 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20   pSrc;..    }.. 
2ef6d 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0d     pSrc = pNew;.
2ef6e 0a 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c  .    nGot = (sql
2ef6f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
2ef70 28 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a  (db, pNew) - siz
2ef71 65 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65  eof(*pSrc))/size
2ef72 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31  of(pSrc->a[0])+1
2ef73 3b 0d 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c  ;..    pSrc->nAl
2ef74 6c 6f 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b  loc = (u16)nGot;
2ef75 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4d 6f  ..  }....  /* Mo
2ef76 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c 6f 74  ve existing slot
2ef77 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66 74 65  s that come afte
2ef78 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e 73 65  r the newly inse
2ef79 72 74 65 64 20 73 6c 6f 74 73 0d 0a 20 20 2a 2a  rted slots..  **
2ef7a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79 20   out of the way 
2ef7b 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 70 53 72 63  */..  for(i=pSrc
2ef7c 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74  ->nSrc-1; i>=iSt
2ef7d 61 72 74 3b 20 69 2d 2d 29 7b 0d 0a 20 20 20 20  art; i--){..    
2ef7e 70 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61  pSrc->a[i+nExtra
2ef7f 5d 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0d  ] = pSrc->a[i];.
2ef80 0a 20 20 7d 0d 0a 20 20 70 53 72 63 2d 3e 6e 53  .  }..  pSrc->nS
2ef81 72 63 20 2b 3d 20 28 69 31 36 29 6e 45 78 74 72  rc += (i16)nExtr
2ef82 61 3b 0d 0a 0d 0a 20 20 2f 2a 20 5a 65 72 6f 20  a;....  /* Zero 
2ef83 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61  the newly alloca
2ef84 74 65 64 20 73 6c 6f 74 73 20 2a 2f 0d 0a 20 20  ted slots */..  
2ef85 6d 65 6d 73 65 74 28 26 70 53 72 63 2d 3e 61 5b  memset(&pSrc->a[
2ef86 69 53 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65  iStart], 0, size
2ef87 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2a 6e  of(pSrc->a[0])*n
2ef88 45 78 74 72 61 29 3b 0d 0a 20 20 66 6f 72 28 69  Extra);..  for(i
2ef89 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74 61 72  =iStart; i<iStar
2ef8a 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29 7b 0d  t+nExtra; i++){.
2ef8b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d 2e  .    pSrc->a[i].
2ef8c 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0d 0a 20  iCursor = -1;.. 
2ef8d 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 74 75 72   }....  /* Retur
2ef8e 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
2ef8f 68 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c  he enlarged SrcL
2ef90 69 73 74 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e  ist */..  return
2ef91 20 70 53 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f   pSrc;..}....../
2ef92 2a 0d 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e  *..** Append a n
2ef93 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f  ew table name to
2ef94 20 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69   the given SrcLi
2ef95 73 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65  st.  Create a ne
2ef96 77 20 53 72 63 4c 69 73 74 20 69 66 0d 0a 2a 2a  w SrcList if..**
2ef97 20 6e 65 65 64 20 62 65 2e 20 20 41 20 6e 65 77   need be.  A new
2ef98 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74 65   entry is create
2ef99 64 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74  d in the SrcList
2ef9a 20 65 76 65 6e 20 69 66 20 70 54 61 62 6c 65 20   even if pTable 
2ef9b 69 73 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a  is NULL...**..**
2ef9c 20 41 20 53 72 63 4c 69 73 74 20 69 73 20 72 65   A SrcList is re
2ef9d 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20  turned, or NULL 
2ef9e 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  if there is an O
2ef9f 4f 4d 20 65 72 72 6f 72 2e 20 20 54 68 65 20 72  OM error.  The r
2efa0 65 74 75 72 6e 65 64 0d 0a 2a 2a 20 53 72 63 4c  eturned..** SrcL
2efa1 69 73 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ist might be the
2efa2 20 73 61 6d 65 20 61 73 20 74 68 65 20 53 72 63   same as the Src
2efa3 4c 69 73 74 20 74 68 61 74 20 77 61 73 20 69 6e  List that was in
2efa4 70 75 74 20 6f 72 20 69 74 20 6d 69 67 68 74 20  put or it might 
2efa5 62 65 0d 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65  be..** a new one
2efa6 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72  .  If an OOM err
2efa7 6f 72 20 64 6f 65 73 20 6f 63 63 75 72 73 2c 20  or does occurs, 
2efa8 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 76  then the prior v
2efa9 61 6c 75 65 20 6f 66 20 70 4c 69 73 74 0d 0a 2a  alue of pList..*
2efaa 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75 74 20  * that is input 
2efab 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
2efac 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
2efad 20 66 72 65 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   freed...**..** 
2efae 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20  If pDatabase is 
2efaf 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61  not null, it mea
2efb0 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  ns that the tabl
2efb1 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61  e has an optiona
2efb2 6c 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e  l..** database n
2efb3 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b  ame prefix.  Lik
2efb4 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61  e this:  "databa
2efb5 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20  se.table".  The 
2efb6 70 44 61 74 61 62 61 73 65 0d 0a 2a 2a 20 70 6f  pDatabase..** po
2efb7 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c  ints to the tabl
2efb8 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70  e name and the p
2efb9 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  Table points to 
2efba 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2efbb 65 2e 0d 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69  e...** The SrcLi
2efbc 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65  st.a[].zName fie
2efbd 6c 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  ld is filled wit
2efbe 68 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  h the table name
2efbf 20 77 68 69 63 68 20 6d 69 67 68 74 0d 0a 2a 2a   which might..**
2efc0 20 63 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c   come from pTabl
2efc1 65 20 28 69 66 20 70 44 61 74 61 62 61 73 65 20  e (if pDatabase 
2efc2 69 73 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d  is NULL) or from
2efc3 20 70 44 61 74 61 62 61 73 65 2e 20 20 0d 0a 2a   pDatabase.  ..*
2efc4 2a 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44  * SrcList.a[].zD
2efc5 61 74 61 62 61 73 65 20 69 73 20 66 69 6c 6c 65  atabase is fille
2efc6 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
2efc7 61 73 65 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54  ase name from pT
2efc8 61 62 6c 65 2c 0d 0a 2a 2a 20 6f 72 20 77 69 74  able,..** or wit
2efc9 68 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 64 61 74  h NULL if no dat
2efca 61 62 61 73 65 20 69 73 20 73 70 65 63 69 66 69  abase is specifi
2efcb 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 6f  ed...**..** In o
2efcc 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 63  ther words, if c
2efcd 61 6c 6c 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a  all like this:..
2efce 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  **..**         s
2efcf 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2efd0 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b 0d 0a 2a  end(D,A,B,0);..*
2efd1 2a 0d 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20  *..** Then B is 
2efd2 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  a table name and
2efd3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
2efd4 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66 69 65  me is unspecifie
2efd5 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0d 0a 2a  d.  If called..*
2efd6 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 2a 2a  * like this:..**
2efd7 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c  ..**         sql
2efd8 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2efd9 64 28 44 2c 41 2c 42 2c 43 29 3b 0d 0a 2a 2a 0d  d(D,A,B,C);..**.
2efda 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68  .** Then C is th
2efdb 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  e table name and
2efdc 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61   B is the databa
2efdd 73 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20 69  se name.  If C i
2efde 73 20 64 65 66 69 6e 65 64 0d 0a 2a 2a 20 74 68  s defined..** th
2efdf 65 6e 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20  en so is B.  In 
2efe0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20  other words, we 
2efe1 6e 65 76 65 72 20 68 61 76 65 20 61 20 63 61 73  never have a cas
2efe2 65 20 77 68 65 72 65 3a 0d 0a 2a 2a 0d 0a 2a 2a  e where:..**..**
2efe3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2efe4 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c  SrcListAppend(D,
2efe5 41 2c 30 2c 43 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20  A,0,C);..**..** 
2efe6 42 6f 74 68 20 70 54 61 62 6c 65 20 61 6e 64 20  Both pTable and 
2efe7 70 44 61 74 61 62 61 73 65 20 61 72 65 20 61 73  pDatabase are as
2efe8 73 75 6d 65 64 20 74 6f 20 62 65 20 71 75 6f 74  sumed to be quot
2efe9 65 64 2e 20 20 54 68 65 79 20 61 72 65 20 64 65  ed.  They are de
2efea 71 75 6f 74 65 64 0d 0a 2a 2a 20 62 65 66 6f 72  quoted..** befor
2efeb 65 20 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f  e being added to
2efec 20 74 68 65 20 53 72 63 4c 69 73 74 2e 0d 0a 2a   the SrcList...*
2efed 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2efee 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
2efef 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2eff0 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
2eff1 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65          /* Conne
2eff2 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20  ction to notify 
2eff3 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  of malloc failur
2eff4 65 73 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74  es */..  SrcList
2eff5 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20   *pList,     /* 
2eff6 41 70 70 65 6e 64 20 74 6f 20 74 68 69 73 20 53  Append to this S
2eff7 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63 72 65  rcList. NULL cre
2eff8 61 74 65 73 20 61 20 6e 65 77 20 53 72 63 4c 69  ates a new SrcLi
2eff9 73 74 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a  st */..  Token *
2effa 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20  pTable,      /* 
2effb 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e 64 20  Table to append 
2effc 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61  */..  Token *pDa
2effd 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61 74  tabase    /* Dat
2effe 61 62 61 73 65 20 6f 66 20 74 68 65 20 74 61 62  abase of the tab
2efff 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 74 72  le */..){..  str
2f000 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2f001 20 2a 70 49 74 65 6d 3b 0d 0a 20 20 61 73 73 65   *pItem;..  asse
2f002 72 74 28 20 70 44 61 74 61 62 61 73 65 3d 3d 30  rt( pDatabase==0
2f003 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 20 29 3b   || pTable!=0 );
2f004 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65    /* Cannot have
2f005 20 43 20 77 69 74 68 6f 75 74 20 42 20 2a 2f 0d   C without B */.
2f006 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
2f007 29 7b 0d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  ){..    pList = 
2f008 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2f009 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53  ero(db, sizeof(S
2f00a 72 63 4c 69 73 74 29 20 29 3b 0d 0a 20 20 20 20  rcList) );..    
2f00b 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2f00c 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 70 4c  eturn 0;..    pL
2f00d 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b  ist->nAlloc = 1;
2f00e 0d 0a 20 20 7d 0d 0a 20 20 70 4c 69 73 74 20 3d  ..  }..  pList =
2f00f 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45   sqlite3SrcListE
2f010 6e 6c 61 72 67 65 28 64 62 2c 20 70 4c 69 73 74  nlarge(db, pList
2f011 2c 20 31 2c 20 70 4c 69 73 74 2d 3e 6e 53 72 63  , 1, pList->nSrc
2f012 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  );..  if( db->ma
2f013 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20  llocFailed ){.. 
2f014 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
2f015 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
2f016 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  t);..    return 
2f017 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 49 74 65 6d  0;..  }..  pItem
2f018 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69   = &pList->a[pLi
2f019 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0d 0a 20 20  st->nSrc-1];..  
2f01a 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26 26  if( pDatabase &&
2f01b 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30   pDatabase->z==0
2f01c 20 29 7b 0d 0a 20 20 20 20 70 44 61 74 61 62 61   ){..    pDataba
2f01d 73 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  se = 0;..  }..  
2f01e 69 66 28 20 70 44 61 74 61 62 61 73 65 20 29 7b  if( pDatabase ){
2f01f 0d 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 65  ..    Token *pTe
2f020 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 3b 0d  mp = pDatabase;.
2f021 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d  .    pDatabase =
2f022 20 70 54 61 62 6c 65 3b 0d 0a 20 20 20 20 70 54   pTable;..    pT
2f023 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0d 0a 20  able = pTemp;.. 
2f024 20 7d 0d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61   }..  pItem->zNa
2f025 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
2f026 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54  FromToken(db, pT
2f027 61 62 6c 65 29 3b 0d 0a 20 20 70 49 74 65 6d 2d  able);..  pItem-
2f028 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c  >zDatabase = sql
2f029 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2f02a 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 65 29  n(db, pDatabase)
2f02b 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73  ;..  return pLis
2f02c 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  t;..}..../*..** 
2f02d 41 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f  Assign VdbeCurso
2f02e 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20  r index numbers 
2f02f 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  to all tables in
2f030 20 61 20 53 72 63 4c 69 73 74 0d 0a 2a 2f 0d 0a   a SrcList..*/..
2f031 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2f032 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
2f033 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
2f034 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
2f035 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0d  rcList *pList){.
2f036 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 73 74 72  .  int i;..  str
2f037 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2f038 20 2a 70 49 74 65 6d 3b 0d 0a 20 20 61 73 73 65   *pItem;..  asse
2f039 72 74 28 70 4c 69 73 74 20 7c 7c 20 70 50 61 72  rt(pList || pPar
2f03a 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
2f03b 69 6c 65 64 20 29 3b 0d 0a 20 20 69 66 28 20 70  iled );..  if( p
2f03c 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20 66 6f 72  List ){..    for
2f03d 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
2f03e 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
2f03f 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
2f040 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  +){..      if( p
2f041 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30  Item->iCursor>=0
2f042 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
2f043 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20   pItem->iCursor 
2f044 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2f045 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ;..      if( pIt
2f046 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a  em->pSelect ){..
2f047 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2f048 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
2f049 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  ors(pParse, pIte
2f04a 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  m->pSelect->pSrc
2f04b 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
2f04c 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a   }..  }..}..../*
2f04d 0d 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  ..** Delete an e
2f04e 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e  ntire SrcList in
2f04f 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20  cluding all its 
2f050 73 75 62 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a  substructure...*
2f051 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2f052 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
2f053 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  cListDelete(sqli
2f054 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74  te3 *db, SrcList
2f055 20 2a 70 4c 69 73 74 29 7b 0d 0a 20 20 69 6e 74   *pList){..  int
2f056 20 69 3b 0d 0a 20 20 73 74 72 75 63 74 20 53 72   i;..  struct Sr
2f057 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
2f058 6d 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73 74 3d  m;..  if( pList=
2f059 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  =0 ) return;..  
2f05a 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d  for(pItem=pList-
2f05b 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74  >a, i=0; i<pList
2f05c 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
2f05d 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69  em++){..    sqli
2f05e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
2f05f 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
2f060 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ..    sqlite3DbF
2f061 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
2f062 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69  Name);..    sqli
2f063 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
2f064 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0d 0a 20  tem->zAlias);.. 
2f065 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2f066 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e 64  (db, pItem->zInd
2f067 65 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ex);..    sqlite
2f068 33 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c  3DeleteTable(db,
2f069 20 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0d 0a   pItem->pTab);..
2f06a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2f06b 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65  tDelete(db, pIte
2f06c 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20  m->pSelect);..  
2f06d 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2f06e 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ete(db, pItem->p
2f06f 4f 6e 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  On);..    sqlite
2f070 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
2f071 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29  , pItem->pUsing)
2f072 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
2f073 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
2f074 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  t);..}..../*..**
2f075 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2f076 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
2f077 61 72 73 65 72 20 74 6f 20 61 64 64 20 61 20 6e  arser to add a n
2f078 65 77 20 74 65 72 6d 20 74 6f 20 74 68 65 0d 0a  ew term to the..
2f079 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 6f 77  ** end of a grow
2f07a 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  ing FROM clause.
2f07b 20 20 54 68 65 20 22 70 22 20 70 61 72 61 6d 65    The "p" parame
2f07c 74 65 72 20 69 73 20 74 68 65 20 70 61 72 74 20  ter is the part 
2f07d 6f 66 0d 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20  of..** the FROM 
2f07e 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 20  clause that has 
2f07f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6e  already been con
2f080 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 69  structed.  "p" i
2f081 73 20 4e 55 4c 4c 0d 0a 2a 2a 20 69 66 20 74 68  s NULL..** if th
2f082 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
2f083 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d  term of the FROM
2f084 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65   clause.  pTable
2f085 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 0d 0a   and pDatabase..
2f086 2a 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20  ** are the name 
2f087 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  of the table and
2f088 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20   database named 
2f089 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2f08a 73 65 20 74 65 72 6d 2e 0d 0a 2a 2a 20 70 44 61  se term...** pDa
2f08b 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69  tabase is NULL i
2f08c 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  f the database n
2f08d 61 6d 65 20 71 75 61 6c 69 66 69 65 72 20 69 73  ame qualifier is
2f08e 20 6d 69 73 73 69 6e 67 20 2d 20 74 68 65 0d 0a   missing - the..
2f08f 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e 20 20  ** usual case.  
2f090 49 66 20 74 68 65 20 74 65 72 6d 20 68 61 73 20  If the term has 
2f091 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41  a alias, then pA
2f092 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74  lias points to t
2f093 68 65 0d 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b  he..** alias tok
2f094 65 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d  en.  If the term
2f095 20 69 73 20 61 20 73 75 62 71 75 65 72 79 2c 20   is a subquery, 
2f096 74 68 65 6e 20 70 53 75 62 71 75 65 72 79 20 69  then pSubquery i
2f097 73 20 74 68 65 0d 0a 2a 2a 20 53 45 4c 45 43 54  s the..** SELECT
2f098 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
2f099 74 68 65 20 73 75 62 71 75 65 72 79 20 65 6e 63  the subquery enc
2f09a 6f 64 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c  odes.  The pTabl
2f09b 65 20 61 6e 64 0d 0a 2a 2a 20 70 44 61 74 61 62  e and..** pDatab
2f09c 61 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  ase parameters a
2f09d 72 65 20 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71  re NULL for subq
2f09e 75 65 72 69 65 73 2e 20 20 54 68 65 20 70 4f 6e  ueries.  The pOn
2f09f 20 61 6e 64 20 70 55 73 69 6e 67 0d 0a 2a 2a 20   and pUsing..** 
2f0a0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74  parameters are t
2f0a1 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
2f0a2 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63  e ON and USING c
2f0a3 6c 61 75 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  lauses...**..** 
2f0a4 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72 63  Return a new Src
2f0a5 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f 64  List which encod
2f0a6 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20 77  es is the FROM w
2f0a7 69 74 68 20 74 68 65 20 6e 65 77 0d 0a 2a 2a 20  ith the new..** 
2f0a8 74 65 72 6d 20 61 64 64 65 64 2e 0d 0a 2a 2f 0d  term added...*/.
2f0a9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f0aa 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  SrcList *sqlite3
2f0ab 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f  SrcListAppendFro
2f0ac 6d 54 65 72 6d 28 0d 0a 20 20 50 61 72 73 65 20  mTerm(..  Parse 
2f0ad 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2f0ae 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2f0af 74 65 78 74 20 2a 2f 0d 0a 20 20 53 72 63 4c 69  text */..  SrcLi
2f0b0 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  st *p,          
2f0b1 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 70     /* The left p
2f0b2 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  art of the FROM 
2f0b3 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20 73  clause already s
2f0b4 65 65 6e 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20  een */..  Token 
2f0b5 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20  *pTable,        
2f0b6 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2f0b7 20 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 6f   table to add to
2f0b8 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2f0b9 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 44   */..  Token *pD
2f0ba 61 74 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f  atabase,       /
2f0bb 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
2f0bc 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2f0bd 67 20 70 54 61 62 6c 65 20 2a 2f 0d 0a 20 20 54  g pTable */..  T
2f0be 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20  oken *pAlias,   
2f0bf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
2f0c0 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
2f0c1 20 74 68 65 20 41 53 20 73 75 62 65 78 70 72 65   the AS subexpre
2f0c2 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 53 65 6c 65  ssion */..  Sele
2f0c3 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20  ct *pSubquery,  
2f0c4 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72      /* A subquer
2f0c5 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  y used in place 
2f0c6 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20  of a table name 
2f0c7 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c  */..  Expr *pOn,
2f0c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f0c9 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f   The ON clause o
2f0ca 66 20 61 20 6a 6f 69 6e 20 2a 2f 0d 0a 20 20 49  f a join */..  I
2f0cb 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20  dList *pUsing   
2f0cc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53         /* The US
2f0cd 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ING clause of a 
2f0ce 6a 6f 69 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73  join */..){..  s
2f0cf 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2f0d0 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 20 20 73 71  em *pItem;..  sq
2f0d1 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2f0d2 73 65 2d 3e 64 62 3b 0d 0a 20 20 69 66 28 20 21  se->db;..  if( !
2f0d3 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20 70 55 73  p && (pOn || pUs
2f0d4 69 6e 67 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  ing) ){..    sql
2f0d5 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2f0d6 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63 6c 61  rse, "a JOIN cla
2f0d7 75 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20  use is required 
2f0d8 62 65 66 6f 72 65 20 25 73 22 2c 20 0d 0a 20 20  before %s", ..  
2f0d9 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22 20      (pOn ? "ON" 
2f0da 3a 20 22 55 53 49 4e 47 22 29 0d 0a 20 20 20 20  : "USING")..    
2f0db 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 61 70 70  );..    goto app
2f0dc 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0d  end_from_error;.
2f0dd 0a 20 20 7d 0d 0a 20 20 70 20 3d 20 73 71 6c 69  .  }..  p = sqli
2f0de 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2f0df 28 64 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20  (db, p, pTable, 
2f0e0 70 44 61 74 61 62 61 73 65 29 3b 0d 0a 20 20 69  pDatabase);..  i
2f0e1 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  f( p==0 || NEVER
2f0e2 28 70 2d 3e 6e 53 72 63 3d 3d 30 29 20 29 7b 0d  (p->nSrc==0) ){.
2f0e3 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 6e 64  .    goto append
2f0e4 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0d 0a 20 20  _from_error;..  
2f0e5 7d 0d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d  }..  pItem = &p-
2f0e6 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0d 0a  >a[p->nSrc-1];..
2f0e7 20 20 61 73 73 65 72 74 28 20 70 41 6c 69 61 73    assert( pAlias
2f0e8 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 41  !=0 );..  if( pA
2f0e9 6c 69 61 73 2d 3e 6e 20 29 7b 0d 0a 20 20 20 20  lias->n ){..    
2f0ea 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20  pItem->zAlias = 
2f0eb 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2f0ec 6f 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29  oken(db, pAlias)
2f0ed 3b 0d 0a 20 20 7d 0d 0a 20 20 70 49 74 65 6d 2d  ;..  }..  pItem-
2f0ee 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75 62 71  >pSelect = pSubq
2f0ef 75 65 72 79 3b 0d 0a 20 20 70 49 74 65 6d 2d 3e  uery;..  pItem->
2f0f0 70 4f 6e 20 3d 20 70 4f 6e 3b 0d 0a 20 20 70 49  pOn = pOn;..  pI
2f0f1 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55  tem->pUsing = pU
2f0f2 73 69 6e 67 3b 0d 0a 20 20 72 65 74 75 72 6e 20  sing;..  return 
2f0f3 70 3b 0d 0a 0d 0a 20 61 70 70 65 6e 64 5f 66 72  p;.... append_fr
2f0f4 6f 6d 5f 65 72 72 6f 72 3a 0d 0a 20 20 61 73 73  om_error:..  ass
2f0f5 65 72 74 28 20 70 3d 3d 30 20 29 3b 0d 0a 20 20  ert( p==0 );..  
2f0f6 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2f0f7 65 28 64 62 2c 20 70 4f 6e 29 3b 0d 0a 20 20 73  e(db, pOn);..  s
2f0f8 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
2f0f9 74 65 28 64 62 2c 20 70 55 73 69 6e 67 29 3b 0d  te(db, pUsing);.
2f0fa 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2f0fb 44 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62 71  Delete(db, pSubq
2f0fc 75 65 72 79 29 3b 0d 0a 20 20 72 65 74 75 72 6e  uery);..  return
2f0fd 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
2f0fe 20 41 64 64 20 61 6e 20 49 4e 44 45 58 45 44 20   Add an INDEXED 
2f0ff 42 59 20 6f 72 20 4e 4f 54 20 49 4e 44 45 58 45  BY or NOT INDEXE
2f100 44 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  D clause to the 
2f101 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64  most recently ad
2f102 64 65 64 20 0d 0a 2a 2a 20 65 6c 65 6d 65 6e 74  ded ..** element
2f103 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 2d 6c   of the source-l
2f104 69 73 74 20 70 61 73 73 65 64 20 61 73 20 74 68  ist passed as th
2f105 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2f106 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  t...*/..SQLITE_P
2f107 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2f108 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
2f109 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  dBy(Parse *pPars
2f10a 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 54  e, SrcList *p, T
2f10b 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42 79  oken *pIndexedBy
2f10c 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 49  ){..  assert( pI
2f10d 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b 0d 0a  ndexedBy!=0 );..
2f10e 20 20 69 66 28 20 70 20 26 26 20 41 4c 57 41 59    if( p && ALWAY
2f10f 53 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 7b 0d  S(p->nSrc>0) ){.
2f110 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
2f111 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2f112 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d  = &p->a[p->nSrc-
2f113 31 5d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  1];..    assert(
2f114 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78   pItem->notIndex
2f115 65 64 3d 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e  ed==0 && pItem->
2f116 7a 49 6e 64 65 78 3d 3d 30 20 29 3b 0d 0a 20 20  zIndex==0 );..  
2f117 20 20 69 66 28 20 70 49 6e 64 65 78 65 64 42 79    if( pIndexedBy
2f118 2d 3e 6e 3d 3d 31 20 26 26 20 21 70 49 6e 64 65  ->n==1 && !pInde
2f119 78 65 64 42 79 2d 3e 7a 20 29 7b 0d 0a 20 20 20  xedBy->z ){..   
2f11a 20 20 20 2f 2a 20 41 20 22 4e 4f 54 20 49 4e 44     /* A "NOT IND
2f11b 45 58 45 44 22 20 63 6c 61 75 73 65 20 77 61 73  EXED" clause was
2f11c 20 73 75 70 70 6c 69 65 64 2e 20 53 65 65 20 70   supplied. See p
2f11d 61 72 73 65 2e 79 20 0d 0a 20 20 20 20 20 20 2a  arse.y ..      *
2f11e 2a 20 63 6f 6e 73 74 72 75 63 74 20 22 69 6e 64  * construct "ind
2f11f 65 78 65 64 5f 6f 70 74 22 20 66 6f 72 20 64 65  exed_opt" for de
2f120 74 61 69 6c 73 2e 20 2a 2f 0d 0a 20 20 20 20 20  tails. */..     
2f121 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78   pItem->notIndex
2f122 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 65 6c  ed = 1;..    }el
2f123 73 65 7b 0d 0a 20 20 20 20 20 20 70 49 74 65 6d  se{..      pItem
2f124 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74  ->zIndex = sqlit
2f125 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
2f126 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e 64  pParse->db, pInd
2f127 65 78 65 64 42 79 29 3b 0d 0a 20 20 20 20 7d 0d  exedBy);..    }.
2f128 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
2f129 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  * When building 
2f12a 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  up a FROM clause
2f12b 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 2c 20   in the parser, 
2f12c 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  the join operato
2f12d 72 0d 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c  r..** is initial
2f12e 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  ly attached to t
2f12f 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 2e  he left operand.
2f130 20 20 42 75 74 20 74 68 65 20 63 6f 64 65 20 67    But the code g
2f131 65 6e 65 72 61 74 6f 72 0d 0a 2a 2a 20 65 78 70  enerator..** exp
2f132 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70  ects the join op
2f133 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20  erator to be on 
2f134 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
2f135 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
2f136 0d 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20  ..** Shifts all 
2f137 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66  join operators f
2f138 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68  rom left to righ
2f139 74 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20  t for an entire 
2f13a 46 52 4f 4d 0d 0a 2a 2a 20 63 6c 61 75 73 65 2e  FROM..** clause.
2f13b 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 61 6d 70 6c 65  ..**..** Example
2f13c 3a 20 53 75 70 70 6f 73 65 20 74 68 65 20 6a 6f  : Suppose the jo
2f13d 69 6e 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a  in is like this:
2f13e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ..**..**        
2f13f 20 20 20 41 20 6e 61 74 75 72 61 6c 20 63 72 6f     A natural cro
2f140 73 73 20 6a 6f 69 6e 20 42 0d 0a 2a 2a 0d 0a 2a  ss join B..**..*
2f141 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 69  * The operator i
2f142 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f 73 73  s "natural cross
2f143 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 20 61   join".  The A a
2f144 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 61 72  nd B operands ar
2f145 65 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 69 6e 20  e stored..** in 
2f146 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61  p->a[0] and p->a
2f147 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 6c  [1], respectivel
2f148 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69  y.  The parser i
2f149 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 20  nitially stores 
2f14a 74 68 65 0d 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  the..** operator
2f14b 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72   with A.  This r
2f14c 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68  outine shifts th
2f14d 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72  at operator over
2f14e 20 74 6f 20 42 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   to B...*/..SQLI
2f14f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2f150 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68  sqlite3SrcListSh
2f151 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c  iftJoinType(SrcL
2f152 69 73 74 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  ist *p){..  if( 
2f153 70 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 3b  p ){..    int i;
2f154 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
2f155 3e 61 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30  >a || p->nSrc==0
2f156 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 70   );..    for(i=p
2f157 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69  ->nSrc-1; i>0; i
2f158 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 61  --){..      p->a
2f159 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  [i].jointype = p
2f15a 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70  ->a[i-1].jointyp
2f15b 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70  e;..    }..    p
2f15c 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20  ->a[0].jointype 
2f15d 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  = 0;..  }..}....
2f15e 2f 2a 0d 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74  /*..** Begin a t
2f15f 72 61 6e 73 61 63 74 69 6f 6e 0d 0a 2a 2f 0d 0a  ransaction..*/..
2f160 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2f161 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
2f162 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
2f163 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 74  e *pParse, int t
2f164 79 70 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  ype){..  sqlite3
2f165 20 2a 64 62 3b 0d 0a 20 20 56 64 62 65 20 2a 76   *db;..  Vdbe *v
2f166 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20  ;..  int i;.... 
2f167 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 21   assert( pParse!
2f168 3d 30 20 29 3b 0d 0a 20 20 64 62 20 3d 20 70 50  =0 );..  db = pP
2f169 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 61 73 73  arse->db;..  ass
2f16a 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0d 0a 2f  ert( db!=0 );../
2f16b 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30  *  if( db->aDb[0
2f16c 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
2f16d 6e 3b 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  n; */..  if( sql
2f16e 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2f16f 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  arse, SQLITE_TRA
2f170 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e  NSACTION, "BEGIN
2f171 22 2c 20 30 2c 20 30 29 20 29 7b 0d 0a 20 20 20  ", 0, 0) ){..   
2f172 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
2f173 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2f174 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  dbe(pParse);..  
2f175 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b  if( !v ) return;
2f176 0d 0a 20 20 69 66 28 20 74 79 70 65 21 3d 54 4b  ..  if( type!=TK
2f177 5f 44 45 46 45 52 52 45 44 20 29 7b 0d 0a 20 20  _DEFERRED ){..  
2f178 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2f179 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
2f17a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2f17b 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73  dOp2(v, OP_Trans
2f17c 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65  action, i, (type
2f17d 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b  ==TK_EXCLUSIVE)+
2f17e 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  1);..      sqlit
2f17f 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
2f180 76 2c 20 69 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  v, i);..    }.. 
2f181 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62   }..  sqlite3Vdb
2f182 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75  eAddOp2(v, OP_Au
2f183 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b  toCommit, 0, 0);
2f184 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f  ..}..../*..** Co
2f185 6d 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74 69  mmit a transacti
2f186 6f 6e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  on..*/..SQLITE_P
2f187 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2f188 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63  te3CommitTransac
2f189 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
2f18a 73 65 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76 3b  se){..  Vdbe *v;
2f18b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  ....  assert( pP
2f18c 61 72 73 65 21 3d 30 20 29 3b 0d 0a 20 20 61 73  arse!=0 );..  as
2f18d 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62  sert( pParse->db
2f18e 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 73 71  !=0 );..  if( sq
2f18f 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2f190 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52  Parse, SQLITE_TR
2f191 41 4e 53 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d  ANSACTION, "COMM
2f192 49 54 22 2c 20 30 2c 20 30 29 20 29 7b 0d 0a 20  IT", 0, 0) ){.. 
2f193 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d     return;..  }.
2f194 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
2f195 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a  tVdbe(pParse);..
2f196 20 20 69 66 28 20 76 20 29 7b 0d 0a 20 20 20 20    if( v ){..    
2f197 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f198 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d  2(v, OP_AutoComm
2f199 69 74 2c 20 31 2c 20 30 29 3b 0d 0a 20 20 7d 0d  it, 1, 0);..  }.
2f19a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 6c  .}..../*..** Rol
2f19b 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74  lback a transact
2f19c 69 6f 6e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ion..*/..SQLITE_
2f19d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2f19e 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
2f19f 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
2f1a0 50 61 72 73 65 29 7b 0d 0a 20 20 56 64 62 65 20  Parse){..  Vdbe 
2f1a1 2a 76 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  *v;....  assert(
2f1a2 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0d 0a 20   pParse!=0 );.. 
2f1a3 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2f1a4 3e 64 62 21 3d 30 20 29 3b 0d 0a 20 20 69 66 28  >db!=0 );..  if(
2f1a5 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2f1a6 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2f1a7 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52  _TRANSACTION, "R
2f1a8 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20  OLLBACK", 0, 0) 
2f1a9 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d  ){..    return;.
2f1aa 0a 20 20 7d 0d 0a 20 20 76 20 3d 20 73 71 6c 69  .  }..  v = sqli
2f1ab 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2f1ac 65 29 3b 0d 0a 20 20 69 66 28 20 76 20 29 7b 0d  e);..  if( v ){.
2f1ad 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2f1ae 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74  AddOp2(v, OP_Aut
2f1af 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 3b 0d  oCommit, 1, 1);.
2f1b0 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
2f1b1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2f1b2 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
2f1b3 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 20   parser when it 
2f1b4 70 61 72 73 65 73 20 61 20 63 6f 6d 6d 61 6e 64  parses a command
2f1b5 20 74 6f 20 63 72 65 61 74 65 2c 0d 0a 2a 2a 20   to create,..** 
2f1b6 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62  release or rollb
2f1b7 61 63 6b 20 61 6e 20 53 51 4c 20 73 61 76 65 70  ack an SQL savep
2f1b8 6f 69 6e 74 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49  oint. ..*/..SQLI
2f1b9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2f1ba 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
2f1bb 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2f1bc 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70  int op, Token *p
2f1bd 4e 61 6d 65 29 7b 0d 0a 20 20 63 68 61 72 20 2a  Name){..  char *
2f1be 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
2f1bf 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61  ameFromToken(pPa
2f1c0 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b  rse->db, pName);
2f1c1 0d 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b  ..  if( zName ){
2f1c2 0d 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20  ..    Vdbe *v = 
2f1c3 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2f1c4 50 61 72 73 65 29 3b 0d 0a 23 69 66 6e 64 65 66  Parse);..#ifndef
2f1c5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2f1c6 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a 20 20 20 20  HORIZATION..    
2f1c7 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
2f1c8 72 20 2a 20 63 6f 6e 73 74 20 61 7a 5b 5d 20 3d  r * const az[] =
2f1c9 20 7b 20 22 42 45 47 49 4e 22 2c 20 22 52 45 4c   { "BEGIN", "REL
2f1ca 45 41 53 45 22 2c 20 22 52 4f 4c 4c 42 41 43 4b  EASE", "ROLLBACK
2f1cb 22 20 7d 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  " };..    assert
2f1cc 28 20 21 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  ( !SAVEPOINT_BEG
2f1cd 49 4e 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f  IN && SAVEPOINT_
2f1ce 52 45 4c 45 41 53 45 3d 3d 31 20 26 26 20 53 41  RELEASE==1 && SA
2f1cf 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
2f1d0 3d 3d 32 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ==2 );..#endif..
2f1d1 20 20 20 20 69 66 28 20 21 76 20 7c 7c 20 73 71      if( !v || sq
2f1d2 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2f1d3 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 41  Parse, SQLITE_SA
2f1d4 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d 2c  VEPOINT, az[op],
2f1d5 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0d 0a 20   zName, 0) ){.. 
2f1d6 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2f1d7 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  ee(pParse->db, z
2f1d8 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20 72 65  Name);..      re
2f1d9 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  turn;..    }..  
2f1da 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2f1db 4f 70 34 28 76 2c 20 4f 50 5f 53 61 76 65 70 6f  Op4(v, OP_Savepo
2f1dc 69 6e 74 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a  int, op, 0, 0, z
2f1dd 4e 61 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43  Name, P4_DYNAMIC
2f1de 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  );..  }..}..../*
2f1df 0d 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  ..** Make sure t
2f1e0 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
2f1e1 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61   is open and ava
2f1e2 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20  ilable for use. 
2f1e3 20 52 65 74 75 72 6e 0d 0a 2a 2a 20 74 68 65 20   Return..** the 
2f1e4 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
2f1e5 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72  .  Leave any err
2f1e6 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74  or messages in t
2f1e7 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
2f1e8 75 72 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ure...*/..SQLITE
2f1e9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2f1ea 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
2f1eb 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  base(Parse *pPar
2f1ec 73 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  se){..  sqlite3 
2f1ed 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2f1ee 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  ;..  if( db->aDb
2f1ef 5b 31 5d 2e 70 42 74 3d 3d 30 20 26 26 20 21 70  [1].pBt==0 && !p
2f1f0 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29  Parse->explain )
2f1f1 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a  {..    int rc;..
2f1f2 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0d      Btree *pBt;.
2f1f3 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2f1f4 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 0d 0a  t int flags = ..
2f1f5 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2f1f6 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
2f1f7 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  |..          SQL
2f1f8 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
2f1f9 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  |..          SQL
2f1fa 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
2f1fb 56 45 20 7c 0d 0a 20 20 20 20 20 20 20 20 20 20  VE |..          
2f1fc 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
2f1fd 54 45 4f 4e 43 4c 4f 53 45 20 7c 0d 0a 20 20 20  TEONCLOSE |..   
2f1fe 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2f1ff 45 4e 5f 54 45 4d 50 5f 44 42 3b 0d 0a 0d 0a 20  EN_TEMP_DB;.... 
2f200 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2f201 74 72 65 65 4f 70 65 6e 28 64 62 2d 3e 70 56 66  treeOpen(db->pVf
2f202 73 2c 20 30 2c 20 64 62 2c 20 26 70 42 74 2c 20  s, 0, db, &pBt, 
2f203 30 2c 20 66 6c 61 67 73 29 3b 0d 0a 20 20 20 20  0, flags);..    
2f204 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2f205 4b 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  K ){..      sqli
2f206 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2f207 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f  se, "unable to o
2f208 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20  pen a temporary 
2f209 64 61 74 61 62 61 73 65 20 22 0d 0a 20 20 20 20  database "..    
2f20a 20 20 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74      "file for st
2f20b 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20  oring temporary 
2f20c 74 61 62 6c 65 73 22 29 3b 0d 0a 20 20 20 20 20  tables");..     
2f20d 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63   pParse->rc = rc
2f20e 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
2f20f 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64  1;..    }..    d
2f210 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20  b->aDb[1].pBt = 
2f211 70 42 74 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  pBt;..    assert
2f212 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ( db->aDb[1].pSc
2f213 68 65 6d 61 20 29 3b 0d 0a 20 20 20 20 69 66 28  hema );..    if(
2f214 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
2f215 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61  qlite3BtreeSetPa
2f216 67 65 53 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e  geSize(pBt, db->
2f217 6e 65 78 74 50 61 67 65 73 69 7a 65 2c 20 2d 31  nextPagesize, -1
2f218 2c 20 30 29 20 29 7b 0d 0a 20 20 20 20 20 20 64  , 0) ){..      d
2f219 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2f21a 3d 20 31 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  = 1;..      retu
2f21b 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn 1;..    }..  
2f21c 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  }..  return 0;..
2f21d 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65  }..../*..** Gene
2f21e 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rate VDBE code t
2f21f 68 61 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20  hat will verify 
2f220 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2f221 65 20 61 6e 64 20 73 74 61 72 74 0d 0a 2a 2a 20  e and start..** 
2f222 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
2f223 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64  on for all named
2f224 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e   database files.
2f225 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 69  ..**..** It is i
2f226 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c  mportant that al
2f227 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73  l schema cookies
2f228 20 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64   be verified and
2f229 20 61 6c 6c 0d 0a 2a 2a 20 72 65 61 64 20 74 72   all..** read tr
2f22a 61 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74  ansactions be st
2f22b 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79  arted before any
2f22c 74 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65  thing else happe
2f22d 6e 73 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 56 44  ns in..** the VD
2f22e 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74  BE program.  But
2f22f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
2f230 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  n be called afte
2f231 72 20 6d 75 63 68 20 6f 74 68 65 72 0d 0a 2a 2a  r much other..**
2f232 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 67   code has been g
2f233 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68 65  enerated.  So he
2f234 72 65 20 69 73 20 77 68 61 74 20 77 65 20 64 6f  re is what we do
2f235 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69  :..**..** The fi
2f236 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f  rst time this ro
2f237 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2f238 20 77 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47   we code an OP_G
2f239 6f 74 6f 20 74 68 61 74 0d 0a 2a 2a 20 77 69 6c  oto that..** wil
2f23a 6c 20 6a 75 6d 70 20 74 6f 20 61 20 73 75 62 72  l jump to a subr
2f23b 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 65 6e  outine at the en
2f23c 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d  d of the program
2f23d 2e 20 20 54 68 65 6e 20 77 65 0d 0a 2a 2a 20 72  .  Then we..** r
2f23e 65 63 6f 72 64 20 65 76 65 72 79 20 64 61 74 61  ecord every data
2f23f 62 61 73 65 20 74 68 61 74 20 6e 65 65 64 73 20  base that needs 
2f240 69 74 73 20 73 63 68 65 6d 61 20 76 65 72 69 66  its schema verif
2f241 69 65 64 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 70  ied in the..** p
2f242 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73  Parse->cookieMas
2f243 6b 20 66 69 65 6c 64 2e 20 20 4c 61 74 65 72 2c  k field.  Later,
2f244 20 61 66 74 65 72 20 61 6c 6c 20 6f 74 68 65 72   after all other
2f245 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 0d 0a   code has been..
2f246 2a 2a 20 67 65 6e 65 72 61 74 65 64 2c 20 74 68  ** generated, th
2f247 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  e subroutine tha
2f248 74 20 64 6f 65 73 20 74 68 65 20 63 6f 6f 6b 69  t does the cooki
2f249 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e 73 20  e verifications 
2f24a 61 6e 64 0d 0a 2a 2a 20 73 74 61 72 74 73 20 74  and..** starts t
2f24b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  he transactions 
2f24c 77 69 6c 6c 20 62 65 20 63 6f 64 65 64 20 61 6e  will be coded an
2f24d 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 50 32  d the OP_Goto P2
2f24e 20 76 61 6c 75 65 0d 0a 2a 2a 20 77 69 6c 6c 20   value..** will 
2f24f 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  be made to point
2f250 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f 75 74   to that subrout
2f251 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65 72 61  ine.  The genera
2f252 74 69 6f 6e 20 6f 66 20 74 68 65 0d 0a 2a 2a 20  tion of the..** 
2f253 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74  cookie verificat
2f254 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20 63  ion subroutine c
2f255 6f 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20 73  ode happens in s
2f256 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
2f257 6e 67 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  ng()...**..** If
2f258 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f 64 65   iDb<0 then code
2f259 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c   the OP_Goto onl
2f25a 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 66 6c  y - don't set fl
2f25b 61 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 65  ag to verify the
2f25c 0d 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61  ..** schema on a
2f25d 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 54  ny databases.  T
2f25e 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
2f25f 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20  to position the 
2f260 4f 50 5f 47 6f 74 6f 0d 0a 2a 2a 20 65 61 72 6c  OP_Goto..** earl
2f261 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62  y in the code, b
2f262 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66  efore we know if
2f263 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61   any database ta
2f264 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65  bles will be use
2f265 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  d...*/..SQLITE_P
2f266 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2f267 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
2f268 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
2f269 65 2c 20 69 6e 74 20 69 44 62 29 7b 0d 0a 20 20  e, int iDb){..  
2f26a 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c  Parse *pToplevel
2f26b 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54   = sqlite3ParseT
2f26c 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b  oplevel(pParse);
2f26d 0d 0a 0d 0a 20 20 69 66 28 20 70 54 6f 70 6c 65  ....  if( pTople
2f26e 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d  vel->cookieGoto=
2f26f 3d 30 20 29 7b 0d 0a 20 20 20 20 56 64 62 65 20  =0 ){..    Vdbe 
2f270 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
2f271 64 62 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0d  dbe(pToplevel);.
2f272 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20  .    if( v==0 ) 
2f273 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73  return;  /* This
2f274 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66   only happens if
2f275 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72 69   there was a pri
2f276 6f 72 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 20  or error */..   
2f277 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b   pToplevel->cook
2f278 69 65 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33  ieGoto = sqlite3
2f279 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2f27a 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0d  _Goto, 0, 0)+1;.
2f27b 0a 20 20 7d 0d 0a 20 20 69 66 28 20 69 44 62 3e  .  }..  if( iDb>
2f27c 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  =0 ){..    sqlit
2f27d 65 33 20 2a 64 62 20 3d 20 70 54 6f 70 6c 65 76  e3 *db = pToplev
2f27e 65 6c 2d 3e 64 62 3b 0d 0a 20 20 20 20 79 44 62  el->db;..    yDb
2f27f 4d 61 73 6b 20 6d 61 73 6b 3b 0d 0a 0d 0a 20 20  Mask mask;....  
2f280 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
2f281 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 20 20 61 73  ->nDb );..    as
2f282 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
2f283 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62  b].pBt!=0 || iDb
2f284 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ==1 );..    asse
2f285 72 74 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d  rt( iDb<SQLITE_M
2f286 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b  AX_ATTACHED+2 );
2f287 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ..    assert( sq
2f288 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
2f289 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29  Held(db, iDb, 0)
2f28a 20 29 3b 0d 0a 20 20 20 20 6d 61 73 6b 20 3d 20   );..    mask = 
2f28b 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 69 44  ((yDbMask)1)<<iD
2f28c 62 3b 0d 0a 20 20 20 20 69 66 28 20 28 70 54 6f  b;..    if( (pTo
2f28d 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61  plevel->cookieMa
2f28e 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b  sk & mask)==0 ){
2f28f 0d 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65  ..      pTopleve
2f290 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 7c 3d  l->cookieMask |=
2f291 20 6d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 70 54   mask;..      pT
2f292 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 56  oplevel->cookieV
2f293 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d 3e  alue[iDb] = db->
2f294 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2f295 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b  ->schema_cookie;
2f296 0d 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  ..      if( !OMI
2f297 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
2f298 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  =1 ){..        s
2f299 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
2f29a 74 61 62 61 73 65 28 70 54 6f 70 6c 65 76 65 6c  tabase(pToplevel
2f29b 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
2f29c 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a   }..  }..}..../*
2f29d 0d 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  ..** If argument
2f29e 20 7a 44 62 20 69 73 20 4e 55 4c 4c 2c 20 74 68   zDb is NULL, th
2f29f 65 6e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 43  en call sqlite3C
2f2a0 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
2f2a1 29 20 66 6f 72 20 65 61 63 68 20 0d 0a 2a 2a 20  ) for each ..** 
2f2a2 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
2f2a3 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e  e. Otherwise, in
2f2a4 76 6f 6b 65 20 69 74 20 66 6f 72 20 74 68 65 20  voke it for the 
2f2a5 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 7a  database named z
2f2a6 44 62 20 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51  Db only...*/..SQ
2f2a7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2f2a8 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72  d sqlite3CodeVer
2f2a9 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 50  ifyNamedSchema(P
2f2aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2f2ab 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0d  nst char *zDb){.
2f2ac 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2f2ad 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20   pParse->db;..  
2f2ae 69 6e 74 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d  int i;..  for(i=
2f2af 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
2f2b0 2b 29 7b 0d 0a 20 20 20 20 44 62 20 2a 70 44 62  +){..    Db *pDb
2f2b1 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0d   = &db->aDb[i];.
2f2b2 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42  .    if( pDb->pB
2f2b3 74 20 26 26 20 28 21 7a 44 62 20 7c 7c 20 30 3d  t && (!zDb || 0=
2f2b4 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
2f2b5 7a 44 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29  zDb, pDb->zName)
2f2b6 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ) ){..      sqli
2f2b7 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
2f2b8 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0d  ema(pParse, i);.
2f2b9 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a  .    }..  }..}..
2f2ba 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74  ../*..** Generat
2f2bb 65 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  e VDBE code that
2f2bc 20 70 72 65 70 61 72 65 73 20 66 6f 72 20 64 6f   prepares for do
2f2bd 69 6e 67 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e  ing an operation
2f2be 20 74 68 61 74 0d 0a 2a 2a 20 6d 69 67 68 74 20   that..** might 
2f2bf 63 68 61 6e 67 65 20 74 68 65 20 64 61 74 61 62  change the datab
2f2c0 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ase...**..** Thi
2f2c1 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
2f2c2 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69   a new transacti
2f2c3 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  on if we are not
2f2c4 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0d   already within.
2f2c5 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f  .** a transactio
2f2c6 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c  n.  If we are al
2f2c7 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74  ready within a t
2f2c8 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
2f2c9 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0d 0a 2a   a checkpoint..*
2f2ca 2a 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20  * is set if the 
2f2cb 73 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72  setStatement par
2f2cc 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e 20  ameter is true. 
2f2cd 20 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68   A checkpoint sh
2f2ce 6f 75 6c 64 0d 0a 2a 2a 20 62 65 20 73 65 74 20  ould..** be set 
2f2cf 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74  for operations t
2f2d0 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28  hat might fail (
2f2d1 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61  due to a constra
2f2d2 69 6e 74 29 20 70 61 72 74 20 6f 66 0d 0a 2a 2a  int) part of..**
2f2d3 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67 68   the way through
2f2d4 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c 20   and which will 
2f2d5 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d  need to undo som
2f2d6 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75 74  e writes without
2f2d7 20 68 61 76 69 6e 67 20 74 6f 0d 0a 2a 2a 20 72   having to..** r
2f2d8 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c  ollback the whol
2f2d9 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
2f2da 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77  For operations w
2f2db 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61  here all constra
2f2dc 69 6e 74 73 0d 0a 2a 2a 20 63 61 6e 20 62 65 20  ints..** can be 
2f2dd 63 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61  checked before a
2f2de 6e 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  ny changes are m
2f2df 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  ade to the datab
2f2e0 61 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72  ase, it is never
2f2e1 0d 0a 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74  ..** necessary t
2f2e2 6f 20 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61  o undo a write a
2f2e3 6e 64 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e  nd the checkpoin
2f2e4 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  t should not be 
2f2e5 73 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  set...*/..SQLITE
2f2e6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2f2e7 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2f2e8 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a  peration(Parse *
2f2e9 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53  pParse, int setS
2f2ea 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44  tatement, int iD
2f2eb 62 29 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70 54  b){..  Parse *pT
2f2ec 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
2f2ed 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
2f2ee 50 61 72 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74  Parse);..  sqlit
2f2ef 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
2f2f0 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ma(pParse, iDb);
2f2f1 0d 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 77  ..  pToplevel->w
2f2f2 72 69 74 65 4d 61 73 6b 20 7c 3d 20 28 28 79 44  riteMask |= ((yD
2f2f3 62 4d 61 73 6b 29 31 29 3c 3c 69 44 62 3b 0d 0a  bMask)1)<<iDb;..
2f2f4 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d    pToplevel->isM
2f2f5 75 6c 74 69 57 72 69 74 65 20 7c 3d 20 73 65 74  ultiWrite |= set
2f2f6 53 74 61 74 65 6d 65 6e 74 3b 0d 0a 7d 0d 0a 0d  Statement;..}...
2f2f7 0a 2f 2a 0d 0a 2a 2a 20 49 6e 64 69 63 61 74 65  ./*..** Indicate
2f2f8 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d   that the statem
2f2f9 65 6e 74 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  ent currently un
2f2fa 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2f2fb 20 6d 69 67 68 74 20 77 72 69 74 65 0d 0a 2a 2a   might write..**
2f2fc 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65   more than one e
2f2fd 6e 74 72 79 20 28 65 78 61 6d 70 6c 65 3a 20 64  ntry (example: d
2f2fe 65 6c 65 74 69 6e 67 20 6f 6e 65 20 72 6f 77 20  eleting one row 
2f2ff 74 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 61  then inserting a
2f300 6e 6f 74 68 65 72 2c 0d 0a 2a 2a 20 69 6e 73 65  nother,..** inse
2f301 72 74 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 72  rting multiple r
2f302 6f 77 73 20 69 6e 20 61 20 74 61 62 6c 65 2c 20  ows in a table, 
2f303 6f 72 20 69 6e 73 65 72 74 69 6e 67 20 61 20 72  or inserting a r
2f304 6f 77 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74  ow and index ent
2f305 72 69 65 73 2e 29 0d 0a 2a 2a 20 49 66 20 61 6e  ries.)..** If an
2f306 20 61 62 6f 72 74 20 6f 63 63 75 72 73 20 61 66   abort occurs af
2f307 74 65 72 20 73 6f 6d 65 20 6f 66 20 74 68 65 73  ter some of thes
2f308 65 20 77 72 69 74 65 73 20 68 61 76 65 20 63 6f  e writes have co
2f309 6d 70 6c 65 74 65 64 2c 20 74 68 65 6e 20 69 74  mpleted, then it
2f30a 20 77 69 6c 6c 0d 0a 2a 2a 20 62 65 20 6e 65 63   will..** be nec
2f30b 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 74  essary to undo t
2f30c 68 65 20 63 6f 6d 70 6c 65 74 65 64 20 77 72 69  he completed wri
2f30d 74 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  tes...*/..SQLITE
2f30e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2f30f 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28  lite3MultiWrite(
2f310 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0d  Parse *pParse){.
2f311 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65  .  Parse *pTople
2f312 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72  vel = sqlite3Par
2f313 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2f314 65 29 3b 0d 0a 20 20 70 54 6f 70 6c 65 76 65 6c  e);..  pToplevel
2f315 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 3d  ->isMultiWrite =
2f316 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a   1;..}..../* ..*
2f317 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
2f318 61 74 6f 72 20 63 61 6c 6c 73 20 74 68 69 73 20  ator calls this 
2f319 72 6f 75 74 69 6e 65 20 69 66 20 69 73 20 64 69  routine if is di
2f31a 73 63 6f 76 65 72 73 20 74 68 61 74 20 69 74 20  scovers that it 
2f31b 69 73 0d 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20  is..** possible 
2f31c 74 6f 20 61 62 6f 72 74 20 61 20 73 74 61 74 65  to abort a state
2f31d 6d 65 6e 74 20 70 72 69 6f 72 20 74 6f 20 63 6f  ment prior to co
2f31e 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 6e 20 6f 72  mpletion.  In or
2f31f 64 65 72 20 74 6f 20 0d 0a 2a 2a 20 70 65 72 66  der to ..** perf
2f320 6f 72 6d 20 74 68 69 73 20 61 62 6f 72 74 20 77  orm this abort w
2f321 69 74 68 6f 75 74 20 63 6f 72 72 75 70 74 69 6e  ithout corruptin
2f322 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  g the database, 
2f323 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 0d  we need to make.
2f324 0a 2a 2a 20 73 75 72 65 20 74 68 61 74 20 74 68  .** sure that th
2f325 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70  e statement is p
2f326 72 6f 74 65 63 74 65 64 20 62 79 20 61 20 73 74  rotected by a st
2f327 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
2f328 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 65 63  ion...**..** Tec
2f329 68 6e 69 63 61 6c 6c 79 2c 20 77 65 20 6f 6e 6c  hnically, we onl
2f32a 79 20 6e 65 65 64 20 74 6f 20 73 65 74 20 74 68  y need to set th
2f32b 65 20 6d 61 79 41 62 6f 72 74 20 66 6c 61 67 20  e mayAbort flag 
2f32c 69 66 20 74 68 65 0d 0a 2a 2a 20 69 73 4d 75 6c  if the..** isMul
2f32d 74 69 57 72 69 74 65 20 66 6c 61 67 20 77 61 73  tiWrite flag was
2f32e 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 2e   previously set.
2f32f 20 20 54 68 65 72 65 20 69 73 20 61 20 74 69 6d    There is a tim
2f330 65 20 64 65 70 65 6e 64 65 6e 63 79 0d 0a 2a 2a  e dependency..**
2f331 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 61   such that the a
2f332 62 6f 72 74 20 6d 75 73 74 20 6f 63 63 75 72 20  bort must occur 
2f333 61 66 74 65 72 20 74 68 65 20 6d 75 6c 74 69 77  after the multiw
2f334 72 69 74 65 2e 20 20 54 68 69 73 20 6d 61 6b 65  rite.  This make
2f335 73 0d 0a 2a 2a 20 73 6f 6d 65 20 73 74 61 74 65  s..** some state
2f336 6d 65 6e 74 73 20 69 6e 76 6f 6c 76 69 6e 67 20  ments involving 
2f337 74 68 65 20 52 45 50 4c 41 43 45 20 63 6f 6e 66  the REPLACE conf
2f338 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
2f339 61 6c 67 6f 72 69 74 68 6d 0d 0a 2a 2a 20 67 6f  algorithm..** go
2f33a 20 61 20 6c 69 74 74 6c 65 20 66 61 73 74 65 72   a little faster
2f33b 2e 20 20 42 75 74 20 74 61 6b 69 6e 67 20 61 64  .  But taking ad
2f33c 76 61 6e 74 61 67 65 20 6f 66 20 74 68 69 73 20  vantage of this 
2f33d 74 69 6d 65 20 64 65 70 65 6e 64 65 6e 63 79 0d  time dependency.
2f33e 0a 2a 2a 20 6d 61 6b 65 73 20 69 74 20 6d 6f 72  .** makes it mor
2f33f 65 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 70  e difficult to p
2f340 72 6f 76 65 20 74 68 61 74 20 74 68 65 20 63 6f  rove that the co
2f341 64 65 20 69 73 20 63 6f 72 72 65 63 74 20 28 69  de is correct (i
2f342 6e 20 0d 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61  n ..** particula
2f343 72 2c 20 69 74 20 70 72 65 76 65 6e 74 73 20 75  r, it prevents u
2f344 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 61  s from writing a
2f345 6e 20 65 66 66 65 63 74 69 76 65 0d 0a 2a 2a 20  n effective..** 
2f346 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
2f347 66 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d  f sqlite3AssertM
2f348 61 79 41 62 6f 72 74 28 29 29 20 61 6e 64 20 73  ayAbort()) and s
2f349 6f 20 77 65 20 68 61 76 65 20 63 68 6f 73 65 6e  o we have chosen
2f34a 0d 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 74 68 65  ..** to take the
2f34b 20 73 61 66 65 20 72 6f 75 74 65 20 61 6e 64 20   safe route and 
2f34c 73 6b 69 70 20 74 68 65 20 6f 70 74 69 6d 69 7a  skip the optimiz
2f34d 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  ation...*/..SQLI
2f34e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2f34f 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
2f350 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0d  Parse *pParse){.
2f351 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65  .  Parse *pTople
2f352 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72  vel = sqlite3Par
2f353 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2f354 65 29 3b 0d 0a 20 20 70 54 6f 70 6c 65 76 65 6c  e);..  pToplevel
2f355 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0d  ->mayAbort = 1;.
2f356 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 64  .}..../*..** Cod
2f357 65 20 61 6e 20 4f 50 5f 48 61 6c 74 20 74 68 61  e an OP_Halt tha
2f358 74 20 63 61 75 73 65 73 20 74 68 65 20 76 64 62  t causes the vdb
2f359 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 53  e to return an S
2f35a 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
2f35b 0d 0a 2a 2a 20 65 72 72 6f 72 2e 20 54 68 65 20  ..** error. The 
2f35c 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65  onError paramete
2f35d 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  r determines whi
2f35e 63 68 20 28 69 66 20 61 6e 79 29 20 6f 66 20 74  ch (if any) of t
2f35f 68 65 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a  he statement..**
2f360 20 61 6e 64 2f 6f 72 20 63 75 72 72 65 6e 74 20   and/or current 
2f361 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
2f362 6f 6c 6c 65 64 20 62 61 63 6b 2e 0d 0a 2a 2f 0d  olled back...*/.
2f363 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f364 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 6c 74  void sqlite3Halt
2f365 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65  Constraint(Parse
2f366 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e   *pParse, int on
2f367 45 72 72 6f 72 2c 20 63 68 61 72 20 2a 70 34 2c  Error, char *p4,
2f368 20 69 6e 74 20 70 34 74 79 70 65 29 7b 0d 0a 20   int p4type){.. 
2f369 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
2f36a 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2f36b 29 3b 0d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f  );..  if( onErro
2f36c 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0d 0a  r==OE_Abort ){..
2f36d 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62      sqlite3MayAb
2f36e 6f 72 74 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  ort(pParse);..  
2f36f 7d 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  }..  sqlite3Vdbe
2f370 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c  AddOp4(v, OP_Hal
2f371 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
2f372 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  AINT, onError, 0
2f373 2c 20 70 34 2c 20 70 34 74 79 70 65 29 3b 0d 0a  , p4, p4type);..
2f374 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65 63  }..../*..** Chec
2f375 6b 20 74 6f 20 73 65 65 20 69 66 20 70 49 6e 64  k to see if pInd
2f376 65 78 20 75 73 65 73 20 74 68 65 20 63 6f 6c 6c  ex uses the coll
2f377 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70  ating sequence p
2f378 43 6f 6c 6c 2e 20 20 52 65 74 75 72 6e 0d 0a 2a  Coll.  Return..*
2f379 2a 20 74 72 75 65 20 69 66 20 69 74 20 64 6f 65  * true if it doe
2f37a 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69  s and false if i
2f37b 74 20 64 6f 65 73 20 6e 6f 74 2e 0d 0a 2a 2f 0d  t does not...*/.
2f37c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f37d 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0d 0a 73 74  OMIT_REINDEX..st
2f37e 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61 74 69  atic int collati
2f37f 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20 63 68  onMatch(const ch
2f380 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65 78  ar *zColl, Index
2f381 20 2a 70 49 6e 64 65 78 29 7b 0d 0a 20 20 69 6e   *pIndex){..  in
2f382 74 20 69 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  t i;..  assert( 
2f383 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0d 0a 20 20 66  zColl!=0 );..  f
2f384 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78  or(i=0; i<pIndex
2f385 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
2f386 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ..    const char
2f387 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a   *z = pIndex->az
2f388 43 6f 6c 6c 5b 69 5d 3b 0d 0a 20 20 20 20 61 73  Coll[i];..    as
2f389 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0d 0a 20  sert( z!=0 );.. 
2f38a 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
2f38b 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c  3StrICmp(z, zCol
2f38c 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  l) ){..      ret
2f38d 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  urn 1;..    }.. 
2f38e 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d   }..  return 0;.
2f38f 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  .}..#endif..../*
2f390 0d 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61  ..** Recompute a
2f391 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
2f392 61 62 20 74 68 61 74 20 75 73 65 20 74 68 65 20  ab that use the 
2f393 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2f394 63 65 20 70 43 6f 6c 6c 2e 0d 0a 2a 2a 20 49 66  ce pColl...** If
2f395 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72   pColl==0 then r
2f396 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64  ecompute all ind
2f397 69 63 65 73 20 6f 66 20 70 54 61 62 2e 0d 0a 2a  ices of pTab...*
2f398 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
2f399 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0d 0a  E_OMIT_REINDEX..
2f39a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e  static void rein
2f39b 64 65 78 54 61 62 6c 65 28 50 61 72 73 65 20 2a  dexTable(Parse *
2f39c 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
2f39d 54 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 20  Tab, char const 
2f39e 2a 7a 43 6f 6c 6c 29 7b 0d 0a 20 20 49 6e 64 65  *zColl){..  Inde
2f39f 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20  x *pIndex;      
2f3a0 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
2f3a1 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
2f3a2 69 74 68 20 70 54 61 62 20 2a 2f 0d 0a 0d 0a 20  ith pTab */.... 
2f3a3 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62   for(pIndex=pTab
2f3a4 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ->pIndex; pIndex
2f3a5 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d  ; pIndex=pIndex-
2f3a6 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66  >pNext){..    if
2f3a7 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f  ( zColl==0 || co
2f3a8 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f  llationMatch(zCo
2f3a9 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0d 0a  ll, pIndex) ){..
2f3aa 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
2f3ab 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2f3ac 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
2f3ad 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2f3ae 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ..      sqlite3B
2f3af 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
2f3b0 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
2f3b1 62 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  b);..      sqlit
2f3b2 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50  e3RefillIndex(pP
2f3b3 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31  arse, pIndex, -1
2f3b4 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
2f3b5 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  }..#endif..../*.
2f3b6 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c  .** Recompute al
2f3b7 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 61 6c 6c  l indices of all
2f3b8 20 74 61 62 6c 65 73 20 69 6e 20 61 6c 6c 20 64   tables in all d
2f3b9 61 74 61 62 61 73 65 73 20 77 68 65 72 65 20 74  atabases where t
2f3ba 68 65 0d 0a 2a 2a 20 69 6e 64 69 63 65 73 20 75  he..** indices u
2f3bb 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  se the collating
2f3bc 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e   sequence pColl.
2f3bd 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68    If pColl==0 th
2f3be 65 6e 20 72 65 63 6f 6d 70 75 74 65 0d 0a 2a 2a  en recompute..**
2f3bf 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 65   all indices eve
2f3c0 72 79 77 68 65 72 65 2e 0d 0a 2a 2f 0d 0a 23 69  rywhere...*/..#i
2f3c1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f3c2 54 5f 52 45 49 4e 44 45 58 0d 0a 73 74 61 74 69  T_REINDEX..stati
2f3c3 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61  c void reindexDa
2f3c4 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a 70  tabases(Parse *p
2f3c5 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73  Parse, char cons
2f3c6 74 20 2a 7a 43 6f 6c 6c 29 7b 0d 0a 20 20 44 62  t *zColl){..  Db
2f3c7 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20   *pDb;          
2f3c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
2f3c9 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a  ingle database *
2f3ca 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  /..  int iDb;   
2f3cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f3cc 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2f3cd 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
2f3ce 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
2f3cf 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2f3d0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2f3d1 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20  connection */.. 
2f3d2 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20   HashElem *k;   
2f3d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f3d4 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
2f3d5 20 74 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a   tables in pDb *
2f3d6 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  /..  Table *pTab
2f3d7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f3d8 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74   /* A table in t
2f3d9 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0d 0a  he database */..
2f3da 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
2f3db 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
2f3dc 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 20 20  Mutexes(db) );  
2f3dd 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 73 63  /* Needed for sc
2f3de 68 65 6d 61 20 61 63 63 65 73 73 20 2a 2f 0d 0a  hema access */..
2f3df 20 20 66 6f 72 28 69 44 62 3d 30 2c 20 70 44 62    for(iDb=0, pDb
2f3e0 3d 64 62 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62  =db->aDb; iDb<db
2f3e1 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44  ->nDb; iDb++, pD
2f3e2 62 2b 2b 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  b++){..    asser
2f3e3 74 28 20 70 44 62 21 3d 30 20 29 3b 0d 0a 20 20  t( pDb!=0 );..  
2f3e4 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 61    for(k=sqliteHa
2f3e5 73 68 46 69 72 73 74 28 26 70 44 62 2d 3e 70 53  shFirst(&pDb->pS
2f3e6 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
2f3e7 20 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73    k; k=sqliteHas
2f3e8 68 4e 65 78 74 28 6b 29 29 7b 0d 0a 20 20 20 20  hNext(k)){..    
2f3e9 20 20 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a    pTab = (Table*
2f3ea 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
2f3eb 6b 29 3b 0d 0a 20 20 20 20 20 20 72 65 69 6e 64  k);..      reind
2f3ec 65 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  exTable(pParse, 
2f3ed 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0d 0a 20  pTab, zColl);.. 
2f3ee 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65     }..  }..}..#e
2f3ef 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47  ndif..../*..** G
2f3f0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2f3f1 20 74 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d   the REINDEX com
2f3f2 6d 61 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  mand...**..**   
2f3f3 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20       REINDEX    
2f3f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f3f5 20 20 20 20 20 20 20 20 2d 2d 20 31 0d 0a 2a 2a          -- 1..**
2f3f6 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
2f3f7 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20   <collation>    
2f3f8 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0d             -- 2.
2f3f9 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44  .**        REIND
2f3fa 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e  EX  ?<database>.
2f3fb 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d  ?<tablename>  --
2f3fc 20 33 0d 0a 2a 2a 20 20 20 20 20 20 20 20 52 45   3..**        RE
2f3fd 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73  INDEX  ?<databas
2f3fe 65 3e 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20  e>.?<indexname> 
2f3ff 20 2d 2d 20 34 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f   -- 4..**..** Fo
2f400 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20  rm 1 causes all 
2f401 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61  indices in all a
2f402 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
2f403 73 20 74 6f 20 62 65 20 72 65 62 75 69 6c 74 2e  s to be rebuilt.
2f404 0d 0a 2a 2a 20 46 6f 72 6d 20 32 20 72 65 62 75  ..** Form 2 rebu
2f405 69 6c 64 73 20 61 6c 6c 20 69 6e 64 69 63 65 73  ilds all indices
2f406 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65   in all database
2f407 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20 6e  s that use the n
2f408 61 6d 65 64 0d 0a 2a 2a 20 63 6f 6c 6c 61 74 69  amed..** collati
2f409 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20 46 6f  ng function.  Fo
2f40a 72 6d 73 20 33 20 61 6e 64 20 34 20 72 65 62 75  rms 3 and 4 rebu
2f40b 69 6c 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e  ild the named in
2f40c 64 65 78 20 6f 72 20 61 6c 6c 0d 0a 2a 2a 20 69  dex or all..** i
2f40d 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65  ndices associate
2f40e 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64  d with the named
2f40f 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 23 69 66   table...*/..#if
2f410 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f411 5f 52 45 49 4e 44 45 58 0d 0a 53 51 4c 49 54 45  _REINDEX..SQLITE
2f412 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2f413 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 72  lite3Reindex(Par
2f414 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2f415 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e  n *pName1, Token
2f416 20 2a 70 4e 61 6d 65 32 29 7b 0d 0a 20 20 43 6f   *pName2){..  Co
2f417 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
2f418 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2f419 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2f41a 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c  to be reindexed,
2f41b 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 63   or NULL */..  c
2f41c 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
2f41d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
2f41e 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72  me of a table or
2f41f 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 63 6f 6e   index */..  con
2f420 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
2f421 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
2f422 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2f423 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54   */..  Table *pT
2f424 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
2f425 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
2f426 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
2f427 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ..  Index *pInde
2f428 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
2f429 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f  /* An index asso
2f42a 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62  ciated with pTab
2f42b 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20   */..  int iDb; 
2f42c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f42d 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2f42e 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20  se index number 
2f42f 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */..  sqlite3 *d
2f430 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2f431 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2f432 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
2f433 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61  .  Token *pObjNa
2f434 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  me;            /
2f435 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
2f436 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20  ble or index to 
2f437 62 65 20 72 65 69 6e 64 65 78 65 64 20 2a 2f 0d  be reindexed */.
2f438 0a 0d 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ...  /* Read the
2f439 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
2f43a 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
2f43b 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
2f43c 72 72 6f 72 20 6d 65 73 73 61 67 65 0d 0a 20 20  rror message..  
2f43d 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
2f43e 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
2f43f 20 4e 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 69 66 28   NULL. */..  if(
2f440 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
2f441 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
2f442 61 72 73 65 29 20 29 7b 0d 0a 20 20 20 20 72 65  arse) ){..    re
2f443 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  turn;..  }....  
2f444 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b  if( pName1==0 ){
2f445 0d 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74  ..    reindexDat
2f446 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30  abases(pParse, 0
2f447 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d  );..    return;.
2f448 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 4e 45 56  .  }else if( NEV
2f449 45 52 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c 7c  ER(pName2==0) ||
2f44a 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b   pName2->z==0 ){
2f44b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  ..    char *zCol
2f44c 6c 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  l;..    assert( 
2f44d 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0d 0a 20 20  pName1->z );..  
2f44e 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
2f44f 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70  3NameFromToken(p
2f450 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65  Parse->db, pName
2f451 31 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a 43  1);..    if( !zC
2f452 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  oll ) return;.. 
2f453 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2f454 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2f455 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c  , ENC(db), zColl
2f456 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  , 0);..    if( p
2f457 43 6f 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 72  Coll ){..      r
2f458 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28  eindexDatabases(
2f459 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0d  pParse, zColl);.
2f45a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2f45b 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b  Free(db, zColl);
2f45c 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d  ..      return;.
2f45d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
2f45e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43  te3DbFree(db, zC
2f45f 6f 6c 6c 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 44  oll);..  }..  iD
2f460 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
2f461 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
2f462 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
2f463 70 4f 62 6a 4e 61 6d 65 29 3b 0d 0a 20 20 69 66  pObjName);..  if
2f464 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e  ( iDb<0 ) return
2f465 3b 0d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  ;..  z = sqlite3
2f466 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
2f467 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b 0d 0a 20 20  , pObjName);..  
2f468 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
2f469 6e 3b 0d 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  n;..  zDb = db->
2f46a 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d  aDb[iDb].zName;.
2f46b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2f46c 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
2f46d 2c 20 7a 44 62 29 3b 0d 0a 20 20 69 66 28 20 70  , zDb);..  if( p
2f46e 54 61 62 20 29 7b 0d 0a 20 20 20 20 72 65 69 6e  Tab ){..    rein
2f46f 64 65 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c  dexTable(pParse,
2f470 20 70 54 61 62 2c 20 30 29 3b 0d 0a 20 20 20 20   pTab, 0);..    
2f471 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2f472 2c 20 7a 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  , z);..    retur
2f473 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 70 49 6e 64 65  n;..  }..  pInde
2f474 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
2f475 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29  ndex(db, z, zDb)
2f476 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  ;..  sqlite3DbFr
2f477 65 65 28 64 62 2c 20 7a 29 3b 0d 0a 20 20 69 66  ee(db, z);..  if
2f478 28 20 70 49 6e 64 65 78 20 29 7b 0d 0a 20 20 20  ( pIndex ){..   
2f479 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
2f47a 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
2f47b 73 65 2c 20 30 2c 20 69 44 62 29 3b 0d 0a 20 20  se, 0, iDb);..  
2f47c 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49    sqlite3RefillI
2f47d 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e  ndex(pParse, pIn
2f47e 64 65 78 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 72  dex, -1);..    r
2f47f 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 73  eturn;..  }..  s
2f480 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2f481 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74  Parse, "unable t
2f482 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f  o identify the o
2f483 62 6a 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e  bject to be rein
2f484 64 65 78 65 64 22 29 3b 0d 0a 7d 0d 0a 23 65 6e  dexed");..}..#en
2f485 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  dif..../*..** Re
2f486 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79  turn a dynamicly
2f487 20 61 6c 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e   allocated KeyIn
2f488 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2f489 74 20 63 61 6e 20 62 65 20 75 73 65 64 0d 0a 2a  t can be used..*
2f48a 2a 20 77 69 74 68 20 4f 50 5f 4f 70 65 6e 52 65  * with OP_OpenRe
2f48b 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69  ad or OP_OpenWri
2f48c 74 65 20 74 6f 20 61 63 63 65 73 73 20 64 61 74  te to access dat
2f48d 61 62 61 73 65 20 69 6e 64 65 78 20 70 49 64 78  abase index pIdx
2f48e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63  ...**..** If suc
2f48f 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74  cessful, a point
2f490 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 74  er to the new st
2f491 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
2f492 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
2f493 65 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  e..** the caller
2f494 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2f495 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  for calling sqli
2f496 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
2f497 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  on the returned 
2f498 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66  ..** pointer. If
2f499 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2f49a 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20   (out of memory 
2f49b 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61  or missing colla
2f49c 74 69 6f 6e 20 0d 0a 2a 2a 20 73 65 71 75 65 6e  tion ..** sequen
2f49d 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  ce), NULL is ret
2f49e 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74  urned and the st
2f49f 61 74 65 20 6f 66 20 70 50 61 72 73 65 20 75 70  ate of pParse up
2f4a0 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74  dated to reflect
2f4a1 0d 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0d  ..** the error..
2f4a2 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2f4a3 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
2f4a4 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
2f4a5 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2f4a6 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0d 0a 20  Index *pIdx){.. 
2f4a7 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74 20 6e   int i;..  int n
2f4a8 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c  Col = pIdx->nCol
2f4a9 75 6d 6e 3b 0d 0a 20 20 69 6e 74 20 6e 42 79 74  umn;..  int nByt
2f4aa 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49  es = sizeof(KeyI
2f4ab 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a  nfo) + (nCol-1)*
2f4ac 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29  sizeof(CollSeq*)
2f4ad 20 2b 20 6e 43 6f 6c 3b 0d 0a 20 20 73 71 6c 69   + nCol;..  sqli
2f4ae 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2f4af 2d 3e 64 62 3b 0d 0a 20 20 4b 65 79 49 6e 66 6f  ->db;..  KeyInfo
2f4b0 20 2a 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e 66   *pKey = (KeyInf
2f4b1 6f 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  o *)sqlite3DbMal
2f4b2 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74  locZero(db, nByt
2f4b3 65 73 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 4b  es);....  if( pK
2f4b4 65 79 20 29 7b 0d 0a 20 20 20 20 70 4b 65 79 2d  ey ){..    pKey-
2f4b5 3e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  >db = pParse->db
2f4b6 3b 0d 0a 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f  ;..    pKey->aSo
2f4b7 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20 2a 29  rtOrder = (u8 *)
2f4b8 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 6e 43  &(pKey->aColl[nC
2f4b9 6f 6c 5d 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  ol]);..    asser
2f4ba 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72 74 4f  t( &pKey->aSortO
2f4bb 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28 28 28  rder[nCol]==&(((
2f4bc 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79 74 65  u8 *)pKey)[nByte
2f4bd 73 5d 29 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28  s]) );..    for(
2f4be 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
2f4bf 29 7b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ){..      char *
2f4c0 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a  zColl = pIdx->az
2f4c1 43 6f 6c 6c 5b 69 5d 3b 0d 0a 20 20 20 20 20 20  Coll[i];..      
2f4c2 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b  assert( zColl );
2f4c3 0d 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43  ..      pKey->aC
2f4c4 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33  oll[i] = sqlite3
2f4c5 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
2f4c6 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0d 0a 20  arse, zColl);.. 
2f4c7 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74       pKey->aSort
2f4c8 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d  Order[i] = pIdx-
2f4c9 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0d  >aSortOrder[i];.
2f4ca 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4b 65 79  .    }..    pKey
2f4cb 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
2f4cc 6e 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  nCol;..  }....  
2f4cd 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2f4ce 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2f4cf 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 29  DbFree(db, pKey)
2f4d0 3b 0d 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b  ;..    pKey = 0;
2f4d1 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
2f4d2 70 4b 65 79 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a  pKey;..}..../***
2f4d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2f4d4 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a  of build.c *****
2f4d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
2f4d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2f4d9 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  in file callback
2f4da 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2f4db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
2f4dd 0d 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 33  ..** 2005 May 23
2f4de 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75   ..**..** The au
2f4df 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2f4e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2f4e1 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2f4e2 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
2f4e3 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2f4e4 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2f4e5 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
2f4e6 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2f4e7 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
2f4e8 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2f4e9 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2f4ea 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2f4eb 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
2f4ec 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2f4ed 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2f4ee 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2f4ef 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
2f4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a  *********..**..*
2f4f5 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2f4f6 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 75  ains functions u
2f4f7 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
2f4f8 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20  e internal hash 
2f4f9 74 61 62 6c 65 73 0d 0a 2a 2a 20 6f 66 20 75 73  tables..** of us
2f4fa 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er defined funct
2f4fb 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ions and collati
2f4fc 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0d 0a 2a  on sequences...*
2f4fd 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  /....../*..** In
2f4fe 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c 6c 61 74  voke the 'collat
2f4ff 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c  ion needed' call
2f500 62 61 63 6b 20 74 6f 20 72 65 71 75 65 73 74 20  back to request 
2f501 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
2f502 65 6e 63 65 0d 0a 2a 2a 20 69 6e 20 74 68 65 20  ence..** in the 
2f503 65 6e 63 6f 64 69 6e 67 20 65 6e 63 20 6f 66 20  encoding enc of 
2f504 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  name zName, leng
2f505 74 68 20 6e 4e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 73  th nName...*/..s
2f506 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43  tatic void callC
2f507 6f 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65  ollNeeded(sqlite
2f508 33 20 2a 64 62 2c 20 69 6e 74 20 65 6e 63 2c 20  3 *db, int enc, 
2f509 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2f50a 65 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 21  e){..  assert( !
2f50b 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
2f50c 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  || !db->xCollNee
2f50d 64 65 64 31 36 20 29 3b 0d 0a 20 20 69 66 28 20  ded16 );..  if( 
2f50e 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
2f50f 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 45  ){..    char *zE
2f510 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
2f511 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e  3DbStrDup(db, zN
2f512 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 21  ame);..    if( !
2f513 7a 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75  zExternal ) retu
2f514 72 6e 3b 0d 0a 20 20 20 20 64 62 2d 3e 78 43 6f  rn;..    db->xCo
2f515 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f  llNeeded(db->pCo
2f516 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c  llNeededArg, db,
2f517 20 65 6e 63 2c 20 7a 45 78 74 65 72 6e 61 6c 29   enc, zExternal)
2f518 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
2f519 46 72 65 65 28 64 62 2c 20 7a 45 78 74 65 72 6e  Free(db, zExtern
2f51a 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 23 69 66 6e 64  al);..  }..#ifnd
2f51b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
2f51c 54 46 31 36 0d 0a 20 20 69 66 28 20 64 62 2d 3e  TF16..  if( db->
2f51d 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 7b  xCollNeeded16 ){
2f51e 0d 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74  ..    char const
2f51f 20 2a 7a 45 78 74 65 72 6e 61 6c 3b 0d 0a 20 20   *zExternal;..  
2f520 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2f521 2a 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 33 56  *pTmp = sqlite3V
2f522 61 6c 75 65 4e 65 77 28 64 62 29 3b 0d 0a 20 20  alueNew(db);..  
2f523 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
2f524 74 53 74 72 28 70 54 6d 70 2c 20 2d 31 2c 20 7a  tStr(pTmp, -1, z
2f525 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46  Name, SQLITE_UTF
2f526 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
2f527 29 3b 0d 0a 20 20 20 20 7a 45 78 74 65 72 6e 61  );..    zExterna
2f528 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  l = sqlite3Value
2f529 54 65 78 74 28 70 54 6d 70 2c 20 53 51 4c 49 54  Text(pTmp, SQLIT
2f52a 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0d  E_UTF16NATIVE);.
2f52b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65 72 6e  .    if( zExtern
2f52c 61 6c 20 29 7b 0d 0a 20 20 20 20 20 20 64 62 2d  al ){..      db-
2f52d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28 64  >xCollNeeded16(d
2f52e 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
2f52f 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
2f530 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
2f531 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
2f532 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 54  ite3ValueFree(pT
2f533 6d 70 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  mp);..  }..#endi
2f534 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  f..}..../*..** T
2f535 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2f536 61 6c 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c  alled if the col
2f537 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66  lation factory f
2f538 61 69 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20  ails to deliver 
2f539 61 0d 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  a..** collation 
2f53a 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  function in the 
2f53b 62 65 73 74 20 65 6e 63 6f 64 69 6e 67 20 62 75  best encoding bu
2f53c 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6f  t there may be o
2f53d 74 68 65 72 20 76 65 72 73 69 6f 6e 73 0d 0a 2a  ther versions..*
2f53e 2a 20 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61 74  * of this collat
2f53f 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f  ion function (fo
2f540 72 20 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63  r other text enc
2f541 6f 64 69 6e 67 73 29 20 61 76 61 69 6c 61 62 6c  odings) availabl
2f542 65 2e 20 55 73 65 20 6f 6e 65 0d 0a 2a 2a 20 6f  e. Use one..** o
2f543 66 20 74 68 65 73 65 20 69 6e 73 74 65 61 64 20  f these instead 
2f544 69 66 20 74 68 65 79 20 65 78 69 73 74 2e 20 41  if they exist. A
2f545 76 6f 69 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e  void a UTF-8 <->
2f546 20 55 54 46 2d 31 36 20 63 6f 6e 76 65 72 73 69   UTF-16 conversi
2f547 6f 6e 20 69 66 0d 0a 2a 2a 20 70 6f 73 73 69 62  on if..** possib
2f548 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  le...*/..static 
2f549 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71  int synthCollSeq
2f54a 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 43 6f  (sqlite3 *db, Co
2f54b 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0d 0a  llSeq *pColl){..
2f54c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2f54d 32 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  2;..  char *z = 
2f54e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20  pColl->zName;.. 
2f54f 20 69 6e 74 20 69 3b 0d 0a 20 20 73 74 61 74 69   int i;..  stati
2f550 63 20 63 6f 6e 73 74 20 75 38 20 61 45 6e 63 5b  c const u8 aEnc[
2f551 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55 54 46  ] = { SQLITE_UTF
2f552 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46  16BE, SQLITE_UTF
2f553 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55 54 46  16LE, SQLITE_UTF
2f554 38 20 7d 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b  8 };..  for(i=0;
2f555 20 69 3c 33 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20   i<3; i++){..   
2f556 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65   pColl2 = sqlite
2f557 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
2f558 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b   aEnc[i], z, 0);
2f559 0d 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32  ..    if( pColl2
2f55a 2d 3e 78 43 6d 70 21 3d 30 20 29 7b 0d 0a 20 20  ->xCmp!=0 ){..  
2f55b 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c      memcpy(pColl
2f55c 2c 20 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66  , pColl2, sizeof
2f55d 28 43 6f 6c 6c 53 65 71 29 29 3b 0d 0a 20 20 20  (CollSeq));..   
2f55e 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d     pColl->xDel =
2f55f 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44   0;         /* D
2f560 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 64  o not copy the d
2f561 65 73 74 72 75 63 74 6f 72 20 2a 2f 0d 0a 20 20  estructor */..  
2f562 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2f563 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  E_OK;..    }..  
2f564 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
2f565 54 45 5f 45 52 52 4f 52 3b 0d 0a 7d 0d 0a 0d 0a  TE_ERROR;..}....
2f566 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
2f567 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69  tion is responsi
2f568 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67  ble for invoking
2f569 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
2f56a 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 0d  actory callback.
2f56b 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74  .** or substitut
2f56c 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20  ing a collation 
2f56d 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69  sequence of a di
2f56e 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
2f56f 20 77 68 65 6e 20 74 68 65 0d 0a 2a 2a 20 72 65   when the..** re
2f570 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f  quested collatio
2f571 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f  n sequence is no
2f572 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74  t available in t
2f573 68 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64  he desired encod
2f574 69 6e 67 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 49 66  ing...** ..** If
2f575 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
2f576 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74   then pColl must
2f577 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61   point to the da
2f578 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65 6e  tabase native en
2f579 63 6f 64 69 6e 67 20 0d 0a 2a 2a 20 63 6f 6c 6c  coding ..** coll
2f57a 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
2f57b 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20  ith name zName, 
2f57c 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0d 0a 2a  length nName...*
2f57d 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  *..** The return
2f57e 20 76 61 6c 75 65 20 69 73 20 65 69 74 68 65 72   value is either
2f57f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
2f580 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20 75 73  equence to be us
2f581 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 0d 0a  ed in database..
2f582 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74  ** db for collat
2f583 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e  ion type name zN
2f584 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
2f585 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e  e, or NULL, if n
2f586 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0d 0a 2a 2a 20  o collation..** 
2f587 73 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65 20  sequence can be 
2f588 66 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53  found...**..** S
2f589 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33  ee also: sqlite3
2f58a 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 2c  LocateCollSeq(),
2f58b 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2f58c 53 65 71 28 29 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  Seq()..*/..SQLIT
2f58d 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
2f58e 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  q *sqlite3GetCol
2f58f 6c 53 65 71 28 0d 0a 20 20 73 71 6c 69 74 65 33  lSeq(..  sqlite3
2f590 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f  * db,          /
2f591 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2f592 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20  onnection */..  
2f593 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
2f594 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 73        /* The des
2f595 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 20 66 6f  ired encoding fo
2f596 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  r the collating 
2f597 73 65 71 75 65 6e 63 65 20 2a 2f 0d 0a 20 20 43  sequence */..  C
2f598 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 2c 20 20  ollSeq *pColl,  
2f599 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
2f59a 67 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  g sequence with 
2f59b 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c  native encoding,
2f59c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 63   or NULL */..  c
2f59d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2f59e 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
2f59f 67 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  g sequence name 
2f5a0 2a 2f 0d 0a 29 7b 0d 0a 20 20 43 6f 6c 6c 53 65  */..){..  CollSe
2f5a1 71 20 2a 70 3b 0d 0a 0d 0a 20 20 70 20 3d 20 70  q *p;....  p = p
2f5a2 43 6f 6c 6c 3b 0d 0a 20 20 69 66 28 20 21 70 20  Coll;..  if( !p 
2f5a3 29 7b 0d 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  ){..    p = sqli
2f5a4 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
2f5a5 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30  b, enc, zName, 0
2f5a6 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21  );..  }..  if( !
2f5a7 70 20 7c 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b  p || !p->xCmp ){
2f5a8 0d 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c  ..    /* No coll
2f5a9 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f  ation sequence o
2f5aa 66 20 74 68 69 73 20 74 79 70 65 20 66 6f 72 20  f this type for 
2f5ab 74 68 69 73 20 65 6e 63 6f 64 69 6e 67 20 69 73  this encoding is
2f5ac 20 72 65 67 69 73 74 65 72 65 64 2e 0d 0a 20 20   registered...  
2f5ad 20 20 2a 2a 20 43 61 6c 6c 20 74 68 65 20 63 6f    ** Call the co
2f5ae 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
2f5af 74 6f 20 73 65 65 20 69 66 20 69 74 20 63 61 6e  to see if it can
2f5b0 20 73 75 70 70 6c 79 20 75 73 20 77 69 74 68 20   supply us with 
2f5b1 6f 6e 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  one...    */..  
2f5b2 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64    callCollNeeded
2f5b3 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 29  (db, enc, zName)
2f5b4 3b 0d 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ;..    p = sqlit
2f5b5 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2f5b6 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29  , enc, zName, 0)
2f5b7 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 20  ;..  }..  if( p 
2f5b8 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20 73  && !p->xCmp && s
2f5b9 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c 20  ynthCollSeq(db, 
2f5ba 70 29 20 29 7b 0d 0a 20 20 20 20 70 20 3d 20 30  p) ){..    p = 0
2f5bb 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
2f5bc 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20  ( !p || p->xCmp 
2f5bd 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0d  );..  return p;.
2f5be 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
2f5bf 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2f5c0 6c 65 64 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69  led on a collati
2f5c1 6f 6e 20 73 65 71 75 65 6e 63 65 20 62 65 66 6f  on sequence befo
2f5c2 72 65 20 69 74 20 69 73 20 75 73 65 64 20 74 6f  re it is used to
2f5c3 0d 0a 2a 2a 20 63 68 65 63 6b 20 74 68 61 74 20  ..** check that 
2f5c4 69 74 20 69 73 20 64 65 66 69 6e 65 64 2e 20 41  it is defined. A
2f5c5 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c  n undefined coll
2f5c6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 65  ation sequence e
2f5c7 78 69 73 74 73 20 77 68 65 6e 0d 0a 2a 2a 20 61  xists when..** a
2f5c8 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 61   database is loa
2f5c9 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ded that contain
2f5ca 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  s references to 
2f5cb 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2f5cc 63 65 73 0d 0a 2a 2a 20 74 68 61 74 20 68 61 76  ces..** that hav
2f5cd 65 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69 6e  e not been defin
2f5ce 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72  ed by sqlite3_cr
2f5cf 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
2f5d0 20 65 74 63 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66   etc...**..** If
2f5d1 20 72 65 71 75 69 72 65 64 2c 20 74 68 69 73 20   required, this 
2f5d2 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68  routine calls th
2f5d3 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65  e 'collation nee
2f5d4 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f  ded' callback to
2f5d5 0d 0a 2a 2a 20 72 65 71 75 65 73 74 20 61 20 64  ..** request a d
2f5d6 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
2f5d7 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2f5d8 6e 63 65 2e 20 49 66 20 74 68 69 73 20 64 6f 65  nce. If this doe
2f5d9 73 6e 27 74 20 77 6f 72 6b 2c 20 0d 0a 2a 2a 20  sn't work, ..** 
2f5da 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f  an equivalent co
2f5db 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2f5dc 20 74 68 61 74 20 75 73 65 73 20 61 20 74 65 78   that uses a tex
2f5dd 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65  t encoding diffe
2f5de 72 65 6e 74 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68  rent..** from th
2f5df 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2f5e0 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c 20  is substituted, 
2f5e1 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61  if one is availa
2f5e2 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ble...*/..SQLITE
2f5e3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2f5e4 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71  ite3CheckCollSeq
2f5e5 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2f5e6 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
2f5e7 0d 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b  ..  if( pColl ){
2f5e8 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ..    const char
2f5e9 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d   *zName = pColl-
2f5ea 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 73 71 6c  >zName;..    sql
2f5eb 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2f5ec 65 2d 3e 64 62 3b 0d 0a 20 20 20 20 43 6f 6c 6c  e->db;..    Coll
2f5ed 53 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  Seq *p = sqlite3
2f5ee 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  GetCollSeq(db, E
2f5ef 4e 43 28 64 62 29 2c 20 70 43 6f 6c 6c 2c 20 7a  NC(db), pColl, z
2f5f0 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  Name);..    if( 
2f5f1 21 70 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  !p ){..      sql
2f5f2 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2f5f3 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  rse, "no such co
2f5f4 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2f5f5 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a  : %s", zName);..
2f5f6 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
2f5f7 72 72 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 65 74  rr++;..      ret
2f5f8 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
2f5f9 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73  ;..    }..    as
2f5fa 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29  sert( p==pColl )
2f5fb 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
2f5fc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
2f5fd 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63  ....../*..** Loc
2f5fe 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ate and return a
2f5ff 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  n entry from the
2f600 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73   db.aCollSeq has
2f601 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20  h table. If the 
2f602 65 6e 74 72 79 0d 0a 2a 2a 20 73 70 65 63 69 66  entry..** specif
2f603 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e 64  ied by zName and
2f604 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66 6f   nName is not fo
2f605 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74 65  und and paramete
2f606 72 20 27 63 72 65 61 74 65 27 20 69 73 0d 0a 2a  r 'create' is..*
2f607 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65  * true, then cre
2f608 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e  ate a new entry.
2f609 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
2f60a 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  n NULL...**..** 
2f60b 45 61 63 68 20 70 6f 69 6e 74 65 72 20 73 74 6f  Each pointer sto
2f60c 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  red in the sqlit
2f60d 65 33 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68  e3.aCollSeq hash
2f60e 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20   table contains 
2f60f 61 6e 0d 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20  an..** array of 
2f610 74 68 72 65 65 20 43 6f 6c 6c 53 65 71 20 73 74  three CollSeq st
2f611 72 75 63 74 75 72 65 73 2e 20 54 68 65 20 66 69  ructures. The fi
2f612 72 73 74 20 69 73 20 74 68 65 20 63 6f 6c 6c 61  rst is the colla
2f613 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0d 0a 2a  tion sequence..*
2f614 2a 20 70 72 65 66 66 65 72 72 65 64 20 66 6f 72  * prefferred for
2f615 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65 63 6f   UTF-8, the seco
2f616 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61 6e 64  nd UTF-16le, and
2f617 20 74 68 65 20 74 68 69 72 64 20 55 54 46 2d 31   the third UTF-1
2f618 36 62 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 74 6f  6be...**..** Sto
2f619 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  red immediately 
2f61a 61 66 74 65 72 20 74 68 65 20 74 68 72 65 65 20  after the three 
2f61b 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2f61c 63 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ces is a copy of
2f61d 0d 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69  ..** the collati
2f61e 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
2f61f 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  . A pointer to t
2f620 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 73 74  his string is st
2f621 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 65 61 63 68  ored in..** each
2f622 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2f623 6e 63 65 20 73 74 72 75 63 74 75 72 65 2e 0d 0a  nce structure...
2f624 2a 2f 0d 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53  */..static CollS
2f625 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71 45  eq *findCollSeqE
2f626 6e 74 72 79 28 0d 0a 20 20 73 71 6c 69 74 65 33  ntry(..  sqlite3
2f627 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
2f628 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2f629 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73  ction */..  cons
2f62a 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
2f62b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2f62c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2f62d 6e 63 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 72  nce */..  int cr
2f62e 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20  eate            
2f62f 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  /* Create a new 
2f630 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 2a 2f  entry if true */
2f631 0d 0a 29 7b 0d 0a 20 20 43 6f 6c 6c 53 65 71 20  ..){..  CollSeq 
2f632 2a 70 43 6f 6c 6c 3b 0d 0a 20 20 69 6e 74 20 6e  *pColl;..  int n
2f633 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
2f634 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0d 0a  rlen30(zName);..
2f635 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2f636 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
2f637 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20  CollSeq, zName, 
2f638 6e 4e 61 6d 65 29 3b 0d 0a 0d 0a 20 20 69 66 28  nName);....  if(
2f639 20 30 3d 3d 70 43 6f 6c 6c 20 26 26 20 63 72 65   0==pColl && cre
2f63a 61 74 65 20 29 7b 0d 0a 20 20 20 20 70 43 6f 6c  ate ){..    pCol
2f63b 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  l = sqlite3DbMal
2f63c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 33 2a 73 69  locZero(db, 3*si
2f63d 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29 20 2b 20 6e  zeof(*pColl) + n
2f63e 4e 61 6d 65 20 2b 20 31 20 29 3b 0d 0a 20 20 20  Name + 1 );..   
2f63f 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0d 0a 20   if( pColl ){.. 
2f640 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44       CollSeq *pD
2f641 65 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70  el = 0;..      p
2f642 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20  Coll[0].zName = 
2f643 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d  (char*)&pColl[3]
2f644 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  ;..      pColl[0
2f645 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
2f646 54 46 38 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c  TF8;..      pCol
2f647 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[1].zName = (ch
2f648 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0d 0a  ar*)&pColl[3];..
2f649 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 65        pColl[1].e
2f64a 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31  nc = SQLITE_UTF1
2f64b 36 4c 45 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c  6LE;..      pCol
2f64c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[2].zName = (ch
2f64d 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0d 0a  ar*)&pColl[3];..
2f64e 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65        pColl[2].e
2f64f 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31  nc = SQLITE_UTF1
2f650 36 42 45 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63  6BE;..      memc
2f651 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  py(pColl[0].zNam
2f652 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  e, zName, nName)
2f653 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  ;..      pColl[0
2f654 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d  ].zName[nName] =
2f655 20 30 3b 0d 0a 20 20 20 20 20 20 70 44 65 6c 20   0;..      pDel 
2f656 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2f657 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ert(&db->aCollSe
2f658 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  q, pColl[0].zNam
2f659 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29  e, nName, pColl)
2f65a 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66  ;....      /* If
2f65b 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
2f65c 75 72 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20  ure occurred in 
2f65d 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
2f65e 74 28 29 2c 20 69 74 20 77 69 6c 6c 20 0d 0a 20  t(), it will .. 
2f65f 20 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74       ** return t
2f660 68 65 20 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72  he pColl pointer
2f661 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 28   to be deleted (
2f662 62 65 63 61 75 73 65 20 69 74 20 77 61 73 6e 27  because it wasn'
2f663 74 20 61 64 64 65 64 0d 0a 20 20 20 20 20 20 2a  t added..      *
2f664 2a 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  * to the hash ta
2f665 62 6c 65 29 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  ble)...      */.
2f666 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2f667 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c 3d 3d  Del==0 || pDel==
2f668 70 43 6f 6c 6c 20 29 3b 0d 0a 20 20 20 20 20 20  pColl );..      
2f669 69 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0d 0a  if( pDel!=0 ){..
2f66a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
2f66b 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20  ocFailed = 1;.. 
2f66c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
2f66d 46 72 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0d  Free(db, pDel);.
2f66e 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
2f66f 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   0;..      }..  
2f670 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
2f671 72 6e 20 70 43 6f 6c 6c 3b 0d 0a 7d 0d 0a 0d 0a  rn pColl;..}....
2f672 2f 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  /*..** Parameter
2f673 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f   zName points to
2f674 20 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64   a UTF-8 encoded
2f675 20 73 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79   string nName by
2f676 74 65 73 20 6c 6f 6e 67 2e 0d 0a 2a 2a 20 52 65  tes long...** Re
2f677 74 75 72 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71  turn the CollSeq
2f678 2a 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  * pointer for th
2f679 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
2f67a 65 6e 63 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65  ence named zName
2f67b 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 63  ..** for the enc
2f67c 6f 64 69 6e 67 20 27 65 6e 63 27 20 66 72 6f 6d  oding 'enc' from
2f67d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27 64   the database 'd
2f67e 62 27 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  b'...**..** If t
2f67f 68 65 20 65 6e 74 72 79 20 73 70 65 63 69 66 69  he entry specifi
2f680 65 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  ed is not found 
2f681 61 6e 64 20 27 63 72 65 61 74 65 27 20 69 73 20  and 'create' is 
2f682 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61 74  true, then creat
2f683 65 20 61 0d 0a 2a 2a 20 6e 65 77 20 65 6e 74 72  e a..** new entr
2f684 79 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65  y.  Otherwise re
2f685 74 75 72 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a  turn NULL...**..
2f686 2a 2a 20 41 20 73 65 70 61 72 61 74 65 20 66 75  ** A separate fu
2f687 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f  nction sqlite3Lo
2f688 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 73  cateCollSeq() is
2f689 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e   a wrapper aroun
2f68a 64 0d 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  d..** this routi
2f68b 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63 61  ne.  sqlite3Loca
2f68c 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76 6f  teCollSeq() invo
2f68d 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  kes the collatio
2f68e 6e 20 66 61 63 74 6f 72 79 0d 0a 2a 2a 20 69 66  n factory..** if
2f68f 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20 67   necessary and g
2f690 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72 6f  enerates an erro
2f691 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65  r message if the
2f692 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2f693 6e 63 65 0d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  nce..** cannot b
2f694 65 20 66 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  e found...**..**
2f695 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74   See also: sqlit
2f696 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
2f697 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  ), sqlite3GetCol
2f698 6c 53 65 71 28 29 0d 0a 2a 2f 0d 0a 53 51 4c 49  lSeq()..*/..SQLI
2f699 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
2f69a 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43  eq *sqlite3FindC
2f69b 6f 6c 6c 53 65 71 28 0d 0a 20 20 73 71 6c 69 74  ollSeq(..  sqlit
2f69c 65 33 20 2a 64 62 2c 0d 0a 20 20 75 38 20 65 6e  e3 *db,..  u8 en
2f69d 63 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  c,..  const char
2f69e 20 2a 7a 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20   *zName,..  int 
2f69f 63 72 65 61 74 65 0d 0a 29 7b 0d 0a 20 20 43 6f  create..){..  Co
2f6a0 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0d 0a 20  llSeq *pColl;.. 
2f6a1 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0d 0a 20   if( zName ){.. 
2f6a2 20 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e 64 43     pColl = findC
2f6a3 6f 6c 6c 53 65 71 45 6e 74 72 79 28 64 62 2c 20  ollSeqEntry(db, 
2f6a4 7a 4e 61 6d 65 2c 20 63 72 65 61 74 65 29 3b 0d  zName, create);.
2f6a5 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
2f6a6 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
2f6a7 43 6f 6c 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73  Coll;..  }..  as
2f6a8 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 46  sert( SQLITE_UTF
2f6a9 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f 55  8==1 && SQLITE_U
2f6aa 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c  TF16LE==2 && SQL
2f6ab 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 29  ITE_UTF16BE==3 )
2f6ac 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63  ;..  assert( enc
2f6ad 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26  >=SQLITE_UTF8 &&
2f6ae 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46   enc<=SQLITE_UTF
2f6af 31 36 42 45 20 29 3b 0d 0a 20 20 69 66 28 20 70  16BE );..  if( p
2f6b0 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20  Coll ) pColl += 
2f6b1 65 6e 63 2d 31 3b 0d 0a 20 20 72 65 74 75 72 6e  enc-1;..  return
2f6b2 20 70 43 6f 6c 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a   pColl;..}..../*
2f6b3 20 44 75 72 69 6e 67 20 74 68 65 20 73 65 61 72   During the sear
2f6b4 63 68 20 66 6f 72 20 74 68 65 20 62 65 73 74 20  ch for the best 
2f6b5 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
2f6b6 69 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64  ion, this proced
2f6b7 75 72 65 0d 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  ure..** is calle
2f6b8 64 20 74 6f 20 74 65 73 74 20 68 6f 77 20 77 65  d to test how we
2f6b9 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ll the function 
2f6ba 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
2f6bb 72 73 74 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a  rst argument..**
2f6bc 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 65 71   matches the req
2f6bd 75 65 73 74 20 66 6f 72 20 61 20 66 75 6e 63 74  uest for a funct
2f6be 69 6f 6e 20 77 69 74 68 20 6e 41 72 67 20 61 72  ion with nArg ar
2f6bf 67 75 6d 65 6e 74 73 20 69 6e 20 61 20 73 79 73  guments in a sys
2f6c0 74 65 6d 0d 0a 2a 2a 20 74 68 61 74 20 75 73 65  tem..** that use
2f6c1 73 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20  s encoding enc. 
2f6c2 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
2f6c3 65 64 20 69 6e 64 69 63 61 74 65 73 20 68 6f 77  ed indicates how
2f6c4 20 77 65 6c 6c 20 74 68 65 0d 0a 2a 2a 20 72 65   well the..** re
2f6c5 71 75 65 73 74 20 69 73 20 6d 61 74 63 68 65 64  quest is matched
2f6c6 2e 20 41 20 68 69 67 68 65 72 20 76 61 6c 75 65  . A higher value
2f6c7 20 69 6e 64 69 63 61 74 65 73 20 61 20 62 65 74   indicates a bet
2f6c8 74 65 72 20 6d 61 74 63 68 2e 0d 0a 2a 2a 0d 0a  ter match...**..
2f6c9 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
2f6ca 76 61 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20  value is always 
2f6cb 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 36 2c  between 0 and 6,
2f6cc 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a   as follows:..**
2f6cd 0d 0a 2a 2a 20 30 3a 20 4e 6f 74 20 61 20 6d 61  ..** 0: Not a ma
2f6ce 74 63 68 2c 20 6f 72 20 69 66 20 6e 41 72 67 3c  tch, or if nArg<
2f6cf 30 20 61 6e 64 20 74 68 65 20 66 75 6e 63 74 69  0 and the functi
2f6d0 6f 6e 20 69 73 20 68 61 73 20 6e 6f 20 69 6d 70  on is has no imp
2f6d1 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0d 0a 2a 2a  lementation...**
2f6d2 20 31 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61   1: A variable a
2f6d3 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f  rguments functio
2f6d4 6e 20 74 68 61 74 20 70 72 65 66 65 72 73 20 55  n that prefers U
2f6d5 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d  TF-8 when a UTF-
2f6d6 31 36 0d 0a 2a 2a 20 20 20 20 65 6e 63 6f 64 69  16..**    encodi
2f6d7 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ng is requested,
2f6d8 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0d   or vice versa..
2f6d9 0a 2a 2a 20 32 3a 20 41 20 76 61 72 69 61 62 6c  .** 2: A variabl
2f6da 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63  e arguments func
2f6db 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20 55  tion that uses U
2f6dc 54 46 2d 31 36 42 45 20 77 68 65 6e 20 55 54 46  TF-16BE when UTF
2f6dd 2d 31 36 4c 45 20 69 73 0d 0a 2a 2a 20 20 20 20  -16LE is..**    
2f6de 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
2f6df 63 65 20 76 65 72 73 61 2e 0d 0a 2a 2a 20 33 3a  ce versa...** 3:
2f6e0 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75   A variable argu
2f6e1 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 75  ments function u
2f6e2 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74 65  sing the same te
2f6e3 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0d 0a 2a 2a  xt encoding...**
2f6e4 20 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77   4: A function w
2f6e5 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75  ith the exact nu
2f6e6 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
2f6e7 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74  s requested that
2f6e8 0d 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20  ..**    prefers 
2f6e9 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46  UTF-8 when a UTF
2f6ea 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  -16 encoding is 
2f6eb 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
2f6ec 63 65 20 76 65 72 73 61 2e 0d 0a 2a 2a 20 35 3a  ce versa...** 5:
2f6ed 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   A function with
2f6ee 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65   the exact numbe
2f6ef 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72  r of arguments r
2f6f0 65 71 75 65 73 74 65 64 20 74 68 61 74 0d 0a 2a  equested that..*
2f6f1 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 54 46  *    prefers UTF
2f6f2 2d 31 36 4c 45 20 77 68 65 6e 20 55 54 46 2d 31  -16LE when UTF-1
2f6f3 36 42 45 20 69 73 20 72 65 71 75 65 73 74 65 64  6BE is requested
2f6f4 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e  , or vice versa.
2f6f5 0d 0a 2a 2a 20 36 3a 20 41 6e 20 65 78 61 63 74  ..** 6: An exact
2f6f6 20 6d 61 74 63 68 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d   match...**..*/.
2f6f7 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74 63  .static int matc
2f6f8 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44 65 66  hQuality(FuncDef
2f6f9 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 75   *p, int nArg, u
2f6fa 38 20 65 6e 63 29 7b 0d 0a 20 20 69 6e 74 20 6d  8 enc){..  int m
2f6fb 61 74 63 68 20 3d 20 30 3b 0d 0a 20 20 69 66 28  atch = 0;..  if(
2f6fc 20 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20   p->nArg==-1 || 
2f6fd 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 0d 0a  p->nArg==nArg ..
2f6fe 20 20 20 7c 7c 20 28 6e 41 72 67 3d 3d 2d 31 20     || (nArg==-1 
2f6ff 26 26 20 28 70 2d 3e 78 46 75 6e 63 21 3d 30 20  && (p->xFunc!=0 
2f700 7c 7c 20 70 2d 3e 78 53 74 65 70 21 3d 30 29 29  || p->xStep!=0))
2f701 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 6d 61 74 63  ..  ){..    matc
2f702 68 20 3d 20 31 3b 0d 0a 20 20 20 20 69 66 28 20  h = 1;..    if( 
2f703 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c  p->nArg==nArg ||
2f704 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0d 0a 20 20   nArg==-1 ){..  
2f705 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0d 0a      match = 4;..
2f706 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 65      }..    if( e
2f707 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20  nc==p->iPrefEnc 
2f708 29 7b 0d 0a 20 20 20 20 20 20 6d 61 74 63 68 20  ){..      match 
2f709 2b 3d 20 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  += 2;..    }..  
2f70a 20 20 65 6c 73 65 20 69 66 28 20 28 65 6e 63 3d    else if( (enc=
2f70b 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
2f70c 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d  && p->iPrefEnc==
2f70d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 20  SQLITE_UTF16BE) 
2f70e 7c 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ||..            
2f70f 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
2f710 46 31 36 42 45 20 26 26 20 70 2d 3e 69 50 72 65  F16BE && p->iPre
2f711 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  fEnc==SQLITE_UTF
2f712 31 36 4c 45 29 20 29 7b 0d 0a 20 20 20 20 20 20  16LE) ){..      
2f713 6d 61 74 63 68 20 2b 3d 20 31 3b 0d 0a 20 20 20  match += 1;..   
2f714 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
2f715 6e 20 6d 61 74 63 68 3b 0d 0a 7d 0d 0a 0d 0a 2f  n match;..}..../
2f716 2a 0d 0a 2a 2a 20 53 65 61 72 63 68 20 61 20 46  *..** Search a F
2f717 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72 20 61  uncDefHash for a
2f718 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
2f719 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 20  he given name.  
2f71a 52 65 74 75 72 6e 0d 0a 2a 2a 20 61 20 70 6f 69  Return..** a poi
2f71b 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74 63  nter to the matc
2f71c 68 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66 20  hing FuncDef if 
2f71d 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20 74  found, or 0 if t
2f71e 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
2f71f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 46 75  ...*/..static Fu
2f720 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53  ncDef *functionS
2f721 65 61 72 63 68 28 0d 0a 20 20 46 75 6e 63 44 65  earch(..  FuncDe
2f722 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f  fHash *pHash,  /
2f723 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20  * Hash table to 
2f724 73 65 61 72 63 68 20 2a 2f 0d 0a 20 20 69 6e 74  search */..  int
2f725 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
2f726 20 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65    /* Hash of the
2f727 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73   name */..  cons
2f728 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20  t char *zFunc,  
2f729 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63   /* Name of func
2f72a 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  tion */..  int n
2f72b 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 20 20  Func            
2f72c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
2f72d 65 73 20 69 6e 20 7a 46 75 6e 63 20 2a 2f 0d 0a  es in zFunc */..
2f72e 29 7b 0d 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  ){..  FuncDef *p
2f72f 3b 0d 0a 20 20 66 6f 72 28 70 3d 70 48 61 73 68  ;..  for(p=pHash
2f730 2d 3e 61 5b 68 5d 3b 20 70 3b 20 70 3d 70 2d 3e  ->a[h]; p; p=p->
2f731 70 48 61 73 68 29 7b 0d 0a 20 20 20 20 69 66 28  pHash){..    if(
2f732 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2f733 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 46 75 6e 63  (p->zName, zFunc
2f734 2c 20 6e 46 75 6e 63 29 3d 3d 30 20 26 26 20 70  , nFunc)==0 && p
2f735 2d 3e 7a 4e 61 6d 65 5b 6e 46 75 6e 63 5d 3d 3d  ->zName[nFunc]==
2f736 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  0 ){..      retu
2f737 72 6e 20 70 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rn p;..    }..  
2f738 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  }..  return 0;..
2f739 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 73 65  }..../*..** Inse
2f73a 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65 66  rt a new FuncDef
2f73b 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 48   into a FuncDefH
2f73c 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e 0d  ash hash table..
2f73d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
2f73e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2f73f 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0d 0a  FuncDefInsert(..
2f740 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
2f741 48 61 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61  Hash,  /* The ha
2f742 73 68 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  sh table into wh
2f743 69 63 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f  ich to insert */
2f744 0d 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65  ..  FuncDef *pDe
2f745 66 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  f        /* The 
2f746 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
2f747 69 6f 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f  ion to insert */
2f748 0d 0a 29 7b 0d 0a 20 20 46 75 6e 63 44 65 66 20  ..){..  FuncDef 
2f749 2a 70 4f 74 68 65 72 3b 0d 0a 20 20 69 6e 74 20  *pOther;..  int 
2f74a 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
2f74b 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e  trlen30(pDef->zN
2f74c 61 6d 65 29 3b 0d 0a 20 20 75 38 20 63 31 20 3d  ame);..  u8 c1 =
2f74d 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65   (u8)pDef->zName
2f74e 5b 30 5d 3b 0d 0a 20 20 69 6e 74 20 68 20 3d 20  [0];..  int h = 
2f74f 28 6f 61 73 55 70 70 65 72 32 4c 6f 77 65 72 5b  (oasUpper2Lower[
2f750 63 31 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41  c1] + nName) % A
2f751 72 72 61 79 53 69 7a 65 28 70 48 61 73 68 2d 3e  rraySize(pHash->
2f752 61 29 3b 0d 0a 20 20 70 4f 74 68 65 72 20 3d 20  a);..  pOther = 
2f753 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70  functionSearch(p
2f754 48 61 73 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a  Hash, h, pDef->z
2f755 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0d 0a 20  Name, nName);.. 
2f756 20 69 66 28 20 70 4f 74 68 65 72 20 29 7b 0d 0a   if( pOther ){..
2f757 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 74 68      assert( pOth
2f758 65 72 21 3d 70 44 65 66 20 26 26 20 70 4f 74 68  er!=pDef && pOth
2f759 65 72 2d 3e 70 4e 65 78 74 21 3d 70 44 65 66 20  er->pNext!=pDef 
2f75a 29 3b 0d 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e  );..    pDef->pN
2f75b 65 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e  ext = pOther->pN
2f75c 65 78 74 3b 0d 0a 20 20 20 20 70 4f 74 68 65 72  ext;..    pOther
2f75d 2d 3e 70 4e 65 78 74 20 3d 20 70 44 65 66 3b 0d  ->pNext = pDef;.
2f75e 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
2f75f 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0d  Def->pNext = 0;.
2f760 0a 20 20 20 20 70 44 65 66 2d 3e 70 48 61 73 68  .    pDef->pHash
2f761 20 3d 20 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 0d   = pHash->a[h];.
2f762 0a 20 20 20 20 70 48 61 73 68 2d 3e 61 5b 68 5d  .    pHash->a[h]
2f763 20 3d 20 70 44 65 66 3b 0d 0a 20 20 7d 0d 0a 7d   = pDef;..  }..}
2f764 0d 0a 20 20 0d 0a 20 20 0d 0a 0d 0a 2f 2a 0d 0a  ..  ..  ..../*..
2f765 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73 65 72  ** Locate a user
2f766 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20   function given 
2f767 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72  a name, a number
2f768 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e   of arguments an
2f769 64 20 61 20 66 6c 61 67 0d 0a 2a 2a 20 69 6e 64  d a flag..** ind
2f76a 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20  icating whether 
2f76b 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  the function pre
2f76c 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65 72  fers UTF-16 over
2f76d 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e 20   UTF-8.  Return 
2f76e 61 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f  a..** pointer to
2f76f 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72   the FuncDef str
2f770 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69  ucture that defi
2f771 6e 65 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f  nes that functio
2f772 6e 2c 20 6f 72 20 72 65 74 75 72 6e 0d 0a 2a 2a  n, or return..**
2f773 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e   NULL if the fun
2f774 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65  ction does not e
2f775 78 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  xist...**..** If
2f776 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20   the createFlag 
2f777 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65  argument is true
2f778 2c 20 74 68 65 6e 20 61 20 6e 65 77 20 28 62 6c  , then a new (bl
2f779 61 6e 6b 29 20 46 75 6e 63 44 65 66 0d 0a 2a 2a  ank) FuncDef..**
2f77a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
2f77b 65 61 74 65 64 20 61 6e 64 20 6c 69 6b 65 64 20  eated and liked 
2f77c 69 6e 74 6f 20 74 68 65 20 22 64 62 22 20 73 74  into the "db" st
2f77d 72 75 63 74 75 72 65 20 69 66 20 61 0d 0a 2a 2a  ructure if a..**
2f77e 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66 75 6e   no matching fun
2f77f 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79  ction previously
2f780 20 65 78 69 73 74 65 64 2e 20 20 57 68 65 6e 20   existed.  When 
2f781 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 74 72  createFlag is tr
2f782 75 65 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ue..** and the n
2f783 41 72 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  Arg parameter is
2f784 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 61   -1, then only a
2f785 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61   function that a
2f786 63 63 65 70 74 73 0d 0a 2a 2a 20 61 6e 79 20 6e  ccepts..** any n
2f787 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2f788 74 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  ts will be retur
2f789 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ned...**..** If 
2f78a 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61  createFlag is fa
2f78b 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69 73 20  lse and nArg is 
2f78c 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72  -1, then the fir
2f78d 73 74 20 76 61 6c 69 64 0d 0a 2a 2a 20 66 75 6e  st valid..** fun
2f78e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72  ction found is r
2f78f 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e 63  eturned.  A func
2f790 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69 66  tion is valid if
2f791 20 65 69 74 68 65 72 20 78 46 75 6e 63 0d 0a 2a   either xFunc..*
2f792 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f  * or xStep is no
2f793 6e 2d 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  n-zero...**..** 
2f794 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  If createFlag is
2f795 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66   false, then a f
2f796 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
2f797 20 72 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61   required name a
2f798 6e 64 0d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  nd..** number of
2f799 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62   arguments may b
2f79a 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20  e returned even 
2f79b 69 66 20 74 68 65 20 65 54 65 78 74 52 65 70 20  if the eTextRep 
2f79c 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0d 0a 2a  flag does not..*
2f79d 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72 65 71  * match that req
2f79e 75 65 73 74 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c  uested...*/..SQL
2f79f 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
2f7a0 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Def *sqlite3Find
2f7a1 46 75 6e 63 74 69 6f 6e 28 0d 0a 20 20 73 71 6c  Function(..  sql
2f7a2 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2f7a3 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62  /* An open datab
2f7a4 61 73 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ase */..  const 
2f7a5 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20  char *zName, /* 
2f7a6 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63  Name of the func
2f7a7 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d  tion.  Not null-
2f7a8 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0d 0a 20  terminated */.. 
2f7a9 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20 20 20 20   int nName,     
2f7aa 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2f7ab 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74   characters in t
2f7ac 68 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e  he name */..  in
2f7ad 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
2f7ae 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
2f7af 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61  guments.  -1 mea
2f7b0 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20 2a 2f  ns any number */
2f7b1 0d 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  ..  u8 enc,     
2f7b2 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
2f7b3 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
2f7b4 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 72 65 61  g */..  int crea
2f7b5 74 65 46 6c 61 67 20 20 20 20 20 2f 2a 20 43 72  teFlag     /* Cr
2f7b6 65 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69  eate new entry i
2f7b7 66 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20  f true and does 
2f7b8 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65 78  not otherwise ex
2f7b9 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46 75  ist */..){..  Fu
2f7ba 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20 20 20  ncDef *p;       
2f7bb 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
2f7bc 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 46 75 6e  riable */..  Fun
2f7bd 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20 30 3b  cDef *pBest = 0;
2f7be 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 20 66   /* Best match f
2f7bf 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0d 0a  ound so far */..
2f7c0 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65 20    int bestScore 
2f7c1 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 6f  = 0;  /* Score o
2f7c2 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f 0d  f best match */.
2f7c3 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  .  int h;       
2f7c4 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76         /* Hash v
2f7c5 61 6c 75 65 20 2a 2f 0d 0a 0d 0a 0d 0a 20 20 61  alue */......  a
2f7c6 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49  ssert( enc==SQLI
2f7c7 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d  TE_UTF8 || enc==
2f7c8 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
2f7c9 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
2f7ca 46 31 36 42 45 20 29 3b 0d 0a 20 20 68 20 3d 20  F16BE );..  h = 
2f7cb 28 6f 61 73 55 70 70 65 72 32 4c 6f 77 65 72 5b  (oasUpper2Lower[
2f7cc 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20  (u8)zName[0]] + 
2f7cd 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69  nName) % ArraySi
2f7ce 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b  ze(db->aFunc.a);
2f7cf 0d 0a 0d 0a 20 20 2f 2a 20 46 69 72 73 74 20 73  ....  /* First s
2f7d0 65 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63  earch for a matc
2f7d1 68 20 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70  h amongst the ap
2f7d2 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2f7d3 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a 20 20  d functions...  
2f7d4 2a 2f 0d 0a 20 20 70 20 3d 20 66 75 6e 63 74 69  */..  p = functi
2f7d5 6f 6e 53 65 61 72 63 68 28 26 64 62 2d 3e 61 46  onSearch(&db->aF
2f7d6 75 6e 63 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e  unc, h, zName, n
2f7d7 4e 61 6d 65 29 3b 0d 0a 20 20 77 68 69 6c 65 28  Name);..  while(
2f7d8 20 70 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 73   p ){..    int s
2f7d9 63 6f 72 65 20 3d 20 6d 61 74 63 68 51 75 61 6c  core = matchQual
2f7da 69 74 79 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63  ity(p, nArg, enc
2f7db 29 3b 0d 0a 20 20 20 20 69 66 28 20 73 63 6f 72  );..    if( scor
2f7dc 65 3e 62 65 73 74 53 63 6f 72 65 20 29 7b 0d 0a  e>bestScore ){..
2f7dd 20 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b        pBest = p;
2f7de 0d 0a 20 20 20 20 20 20 62 65 73 74 53 63 6f 72  ..      bestScor
2f7df 65 20 3d 20 73 63 6f 72 65 3b 0d 0a 20 20 20 20  e = score;..    
2f7e0 7d 0d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e  }..    p = p->pN
2f7e1 65 78 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  ext;..  }....  /
2f7e2 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73  * If no match is
2f7e3 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 74   found, search t
2f7e4 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63  he built-in func
2f7e5 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  tions...  **..  
2f7e6 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  ** If the SQLITE
2f7e7 5f 50 72 65 66 65 72 42 75 69 6c 74 69 6e 20 66  _PreferBuiltin f
2f7e8 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
2f7e9 20 73 65 61 72 63 68 20 74 68 65 20 62 75 69 6c   search the buil
2f7ea 74 2d 69 6e 0d 0a 20 20 2a 2a 20 66 75 6e 63 74  t-in..  ** funct
2f7eb 69 6f 6e 73 20 65 76 65 6e 20 69 66 20 61 20 70  ions even if a p
2f7ec 72 69 6f 72 20 61 70 70 2d 64 65 66 69 6e 65 64  rior app-defined
2f7ed 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 66 6f   function was fo
2f7ee 75 6e 64 2e 20 20 41 6e 64 20 67 69 76 65 0d 0a  und.  And give..
2f7ef 20 20 2a 2a 20 70 72 69 6f 72 69 74 79 20 74 6f    ** priority to
2f7f0 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
2f7f1 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ons...  **..  **
2f7f2 20 45 78 63 65 70 74 2c 20 69 66 20 63 72 65 61   Except, if crea
2f7f3 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20  teFlag is true, 
2f7f4 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
2f7f5 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f  we are trying to
2f7f6 0d 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61  ..  ** install a
2f7f7 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20   new function.  
2f7f8 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65 66  Whatever FuncDef
2f7f9 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
2f7fa 74 75 72 6e 65 64 20 69 74 20 77 69 6c 6c 0d 0a  turned it will..
2f7fb 20 20 2a 2a 20 68 61 76 65 20 66 69 65 6c 64 73    ** have fields
2f7fc 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74   overwritten wit
2f7fd 68 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f  h new informatio
2f7fe 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  n appropriate fo
2f7ff 72 20 74 68 65 0d 0a 20 20 2a 2a 20 6e 65 77 20  r the..  ** new 
2f800 66 75 6e 63 74 69 6f 6e 2e 20 20 42 75 74 20 74  function.  But t
2f801 68 65 20 46 75 6e 63 44 65 66 73 20 66 6f 72 20  he FuncDefs for 
2f802 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
2f803 6e 73 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79  ns are read-only
2f804 2e 0d 0a 20 20 2a 2a 20 53 6f 20 77 65 20 6d 75  ...  ** So we mu
2f805 73 74 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f  st not search fo
2f806 72 20 62 75 69 6c 74 2d 69 6e 73 20 77 68 65 6e  r built-ins when
2f807 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   creating a new 
2f808 66 75 6e 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 20  function...  */ 
2f809 0d 0a 20 20 69 66 28 20 21 63 72 65 61 74 65 46  ..  if( !createF
2f80a 6c 61 67 20 26 26 20 28 70 42 65 73 74 3d 3d 30  lag && (pBest==0
2f80b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 20 26   || (db->flags &
2f80c 20 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75   SQLITE_PreferBu
2f80d 69 6c 74 69 6e 29 21 3d 30 29 20 29 7b 0d 0a 20  iltin)!=0) ){.. 
2f80e 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a     FuncDefHash *
2f80f 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28  pHash = &GLOBAL(
2f810 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c  FuncDefHash, sql
2f811 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
2f812 6f 6e 73 29 3b 0d 0a 20 20 20 20 62 65 73 74 53  ons);..    bestS
2f813 63 6f 72 65 20 3d 20 30 3b 0d 0a 20 20 20 20 70  core = 0;..    p
2f814 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63   = functionSearc
2f815 68 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e 61 6d  h(pHash, h, zNam
2f816 65 2c 20 6e 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  e, nName);..    
2f817 77 68 69 6c 65 28 20 70 20 29 7b 0d 0a 20 20 20  while( p ){..   
2f818 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d     int score = m
2f819 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e  atchQuality(p, n
2f81a 41 72 67 2c 20 65 6e 63 29 3b 0d 0a 20 20 20 20  Arg, enc);..    
2f81b 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74    if( score>best
2f81c 53 63 6f 72 65 20 29 7b 0d 0a 20 20 20 20 20 20  Score ){..      
2f81d 20 20 70 42 65 73 74 20 3d 20 70 3b 0d 0a 20 20    pBest = p;..  
2f81e 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20        bestScore 
2f81f 3d 20 73 63 6f 72 65 3b 0d 0a 20 20 20 20 20 20  = score;..      
2f820 7d 0d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  }..      p = p->
2f821 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  pNext;..    }.. 
2f822 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68   }....  /* If th
2f823 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72  e createFlag par
2f824 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 20 61  ameter is true a
2f825 6e 64 20 74 68 65 20 73 65 61 72 63 68 20 64 69  nd the search di
2f826 64 20 6e 6f 74 20 72 65 76 65 61 6c 20 61 6e 0d  d not reveal an.
2f827 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d 61 74 63  .  ** exact matc
2f828 68 20 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c 20  h for the name, 
2f829 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
2f82a 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67  nts and encoding
2f82b 2c 20 74 68 65 6e 20 61 64 64 20 61 0d 0a 20 20  , then add a..  
2f82c 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20 74 6f 20  ** new entry to 
2f82d 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
2f82e 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0d 0a 20  nd return it... 
2f82f 20 2a 2f 0d 0a 20 20 69 66 28 20 63 72 65 61 74   */..  if( creat
2f830 65 46 6c 61 67 20 26 26 20 28 62 65 73 74 53 63  eFlag && (bestSc
2f831 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73 74 2d 3e  ore<6 || pBest->
2f832 6e 41 72 67 21 3d 6e 41 72 67 29 20 26 26 20 0d  nArg!=nArg) && .
2f833 0a 20 20 20 20 20 20 28 70 42 65 73 74 20 3d 20  .      (pBest = 
2f834 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2f835 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
2f836 70 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31 29 29  pBest)+nName+1))
2f837 21 3d 30 20 29 7b 0d 0a 20 20 20 20 70 42 65 73  !=0 ){..    pBes
2f838 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72  t->zName = (char
2f839 20 2a 29 26 70 42 65 73 74 5b 31 5d 3b 0d 0a 20   *)&pBest[1];.. 
2f83a 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d     pBest->nArg =
2f83b 20 28 75 31 36 29 6e 41 72 67 3b 0d 0a 20 20 20   (u16)nArg;..   
2f83c 20 70 42 65 73 74 2d 3e 69 50 72 65 66 45 6e 63   pBest->iPrefEnc
2f83d 20 3d 20 65 6e 63 3b 0d 0a 20 20 20 20 6d 65 6d   = enc;..    mem
2f83e 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61 6d 65  cpy(pBest->zName
2f83f 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
2f840 0d 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61  ..    pBest->zNa
2f841 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0d 0a  me[nName] = 0;..
2f842 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44      sqlite3FuncD
2f843 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 46  efInsert(&db->aF
2f844 75 6e 63 2c 20 70 42 65 73 74 29 3b 0d 0a 20 20  unc, pBest);..  
2f845 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 42 65 73 74  }....  if( pBest
2f846 20 26 26 20 28 70 42 65 73 74 2d 3e 78 53 74 65   && (pBest->xSte
2f847 70 20 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e  p || pBest->xFun
2f848 63 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 29  c || createFlag)
2f849 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
2f84a 70 42 65 73 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72  pBest;..  }..  r
2f84b 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn 0;..}..../
2f84c 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72  *..** Free all r
2f84d 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62 79  esources held by
2f84e 20 74 68 65 20 73 63 68 65 6d 61 20 73 74 72 75   the schema stru
2f84f 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69 64 2a  cture. The void*
2f850 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73   argument points
2f851 0d 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d 61  ..** at a Schema
2f852 20 73 74 72 75 63 74 2e 20 54 68 69 73 20 66 75   struct. This fu
2f853 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nction does not 
2f854 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72  call sqlite3DbFr
2f855 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20  ee(db, ) on the 
2f856 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73  ..** pointer its
2f857 65 6c 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65  elf, it just cle
2f858 61 6e 73 20 75 70 20 73 75 62 73 69 64 69 61 72  ans up subsidiar
2f859 79 20 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65  y resources (i.e
2f85a 2e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0d 0a  . the contents..
2f85b 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  ** of the schema
2f85c 20 68 61 73 68 20 74 61 62 6c 65 73 29 2e 0d 0a   hash tables)...
2f85d 2a 2a 0d 0a 2a 2a 20 54 68 65 20 53 63 68 65 6d  **..** The Schem
2f85e 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76 61 72  a.cache_size var
2f85f 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 6c 65  iable is not cle
2f860 61 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ared...*/..SQLIT
2f861 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2f862 71 6c 69 74 65 33 53 63 68 65 6d 61 43 6c 65 61  qlite3SchemaClea
2f863 72 28 76 6f 69 64 20 2a 70 29 7b 0d 0a 20 20 48  r(void *p){..  H
2f864 61 73 68 20 74 65 6d 70 31 3b 0d 0a 20 20 48 61  ash temp1;..  Ha
2f865 73 68 20 74 65 6d 70 32 3b 0d 0a 20 20 48 61 73  sh temp2;..  Has
2f866 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0d 0a 20  hElem *pElem;.. 
2f867 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
2f868 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b 0d   = (Schema *)p;.
2f869 0a 0d 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63  ...  temp1 = pSc
2f86a 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0d 0a  hema->tblHash;..
2f86b 20 20 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d    temp2 = pSchem
2f86c 61 2d 3e 74 72 69 67 48 61 73 68 3b 0d 0a 20 20  a->trigHash;..  
2f86d 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
2f86e 26 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  &pSchema->trigHa
2f86f 73 68 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 48  sh);..  sqlite3H
2f870 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 65 6d  ashClear(&pSchem
2f871 61 2d 3e 69 64 78 48 61 73 68 29 3b 0d 0a 20 20  a->idxHash);..  
2f872 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
2f873 48 61 73 68 46 69 72 73 74 28 26 74 65 6d 70 32  HashFirst(&temp2
2f874 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
2f875 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
2f876 45 6c 65 6d 29 29 7b 0d 0a 20 20 20 20 73 71 6c  Elem)){..    sql
2f877 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
2f878 72 28 30 2c 20 28 54 72 69 67 67 65 72 2a 29 73  r(0, (Trigger*)s
2f879 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
2f87a 6c 65 6d 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73  lem));..  }..  s
2f87b 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
2f87c 26 74 65 6d 70 32 29 3b 0d 0a 20 20 73 71 6c 69  &temp2);..  sqli
2f87d 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63  te3HashInit(&pSc
2f87e 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 0d  hema->tblHash);.
2f87f 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
2f880 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
2f881 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp1); pElem; pEl
2f882 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
2f883 74 28 70 45 6c 65 6d 29 29 7b 0d 0a 20 20 20 20  t(pElem)){..    
2f884 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71  Table *pTab = sq
2f885 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
2f886 65 6d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  em);..    sqlite
2f887 33 44 65 6c 65 74 65 54 61 62 6c 65 28 30 2c 20  3DeleteTable(0, 
2f888 70 54 61 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73  pTab);..  }..  s
2f889 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
2f88a 26 74 65 6d 70 31 29 3b 0d 0a 20 20 73 71 6c 69  &temp1);..  sqli
2f88b 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53  te3HashClear(&pS
2f88c 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 29  chema->fkeyHash)
2f88d 3b 0d 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53  ;..  pSchema->pS
2f88e 65 71 54 61 62 20 3d 20 30 3b 0d 0a 20 20 69 66  eqTab = 0;..  if
2f88f 28 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ( pSchema->flags
2f890 20 26 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64   & DB_SchemaLoad
2f891 65 64 20 29 7b 0d 0a 20 20 20 20 70 53 63 68 65  ed ){..    pSche
2f892 6d 61 2d 3e 69 47 65 6e 65 72 61 74 69 6f 6e 2b  ma->iGeneration+
2f893 2b 3b 0d 0a 20 20 20 20 70 53 63 68 65 6d 61 2d  +;..    pSchema-
2f894 3e 66 6c 61 67 73 20 26 3d 20 7e 44 42 5f 53 63  >flags &= ~DB_Sc
2f895 68 65 6d 61 4c 6f 61 64 65 64 3b 0d 0a 20 20 7d  hemaLoaded;..  }
2f896 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69  ..}..../*..** Fi
2f897 6e 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  nd and return th
2f898 65 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61  e schema associa
2f899 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65  ted with a BTree
2f89a 2e 20 20 43 72 65 61 74 65 0d 0a 2a 2a 20 61 20  .  Create..** a 
2f89b 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
2f89c 73 61 72 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  sary...*/..SQLIT
2f89d 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d 61  E_PRIVATE Schema
2f89e 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47   *sqlite3SchemaG
2f89f 65 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  et(sqlite3 *db, 
2f8a0 42 74 72 65 65 20 2a 70 42 74 29 7b 0d 0a 20 20  Btree *pBt){..  
2f8a1 53 63 68 65 6d 61 20 2a 20 70 3b 0d 0a 20 20 69  Schema * p;..  i
2f8a2 66 28 20 70 42 74 20 29 7b 0d 0a 20 20 20 20 70  f( pBt ){..    p
2f8a3 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c   = (Schema *)sql
2f8a4 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
2f8a5 70 42 74 2c 20 73 69 7a 65 6f 66 28 53 63 68 65  pBt, sizeof(Sche
2f8a6 6d 61 29 2c 20 73 71 6c 69 74 65 33 53 63 68 65  ma), sqlite3Sche
2f8a7 6d 61 43 6c 65 61 72 29 3b 0d 0a 20 20 7d 65 6c  maClear);..  }el
2f8a8 73 65 7b 0d 0a 20 20 20 20 70 20 3d 20 28 53 63  se{..    p = (Sc
2f8a9 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 44 62  hema *)sqlite3Db
2f8aa 4d 61 6c 6c 6f 63 5a 65 72 6f 28 30 2c 20 73 69  MallocZero(0, si
2f8ab 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0d 0a  zeof(Schema));..
2f8ac 20 20 7d 0d 0a 20 20 69 66 28 20 21 70 20 29 7b    }..  if( !p ){
2f8ad 0d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  ..    db->malloc
2f8ae 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 7d  Failed = 1;..  }
2f8af 65 6c 73 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e  else if ( 0==p->
2f8b0 66 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0d 0a  file_format ){..
2f8b1 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
2f8b2 6e 69 74 28 26 70 2d 3e 74 62 6c 48 61 73 68 29  nit(&p->tblHash)
2f8b3 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  ;..    sqlite3Ha
2f8b4 73 68 49 6e 69 74 28 26 70 2d 3e 69 64 78 48 61  shInit(&p->idxHa
2f8b5 73 68 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  sh);..    sqlite
2f8b6 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 72  3HashInit(&p->tr
2f8b7 69 67 48 61 73 68 29 3b 0d 0a 20 20 20 20 73 71  igHash);..    sq
2f8b8 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
2f8b9 2d 3e 66 6b 65 79 48 61 73 68 29 3b 0d 0a 20 20  ->fkeyHash);..  
2f8ba 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54    p->enc = SQLIT
2f8bb 45 5f 55 54 46 38 3b 0d 0a 20 20 7d 0d 0a 20 20  E_UTF8;..  }..  
2f8bc 72 65 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a  return p;..}....
2f8bd 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2f8be 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e  End of callback.
2f8bf 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2f8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
2f8c2 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2f8c3 20 42 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65   Begin file dele
2f8c4 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
2f8c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2f8c7 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65  ../*..** 2001 Se
2f8c8 70 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a  ptember 15..**..
2f8c9 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2f8ca 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2f8cb 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2f8cc 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2f8cd 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
2f8ce 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2f8cf 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
2f8d0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2f8d1 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2f8d2 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
2f8d3 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2f8d4 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2f8d5 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2f8d6 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
2f8d7 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2f8d8 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2f8d9 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2f8da 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
2f8db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8df 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
2f8e0 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
2f8e1 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
2f8e2 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
2f8e3 20 70 61 72 73 65 72 0d 0a 2a 2a 20 69 6e 20 6f   parser..** in o
2f8e4 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
2f8e5 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45 54 45   code for DELETE
2f8e6 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73   FROM statements
2f8e7 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
2f8e8 57 68 69 6c 65 20 61 20 53 72 63 4c 69 73 74 20  While a SrcList 
2f8e9 63 61 6e 20 69 6e 20 67 65 6e 65 72 61 6c 20 72  can in general r
2f8ea 65 70 72 65 73 65 6e 74 20 6d 75 6c 74 69 70 6c  epresent multipl
2f8eb 65 20 74 61 62 6c 65 73 20 61 6e 64 20 73 75 62  e tables and sub
2f8ec 71 75 65 72 69 65 73 0d 0a 2a 2a 20 28 61 73 20  queries..** (as 
2f8ed 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2f8ee 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
2f8ef 74 61 74 65 6d 65 6e 74 29 20 69 6e 20 74 68 69  tatement) in thi
2f8f0 73 20 63 61 73 65 20 69 74 20 63 6f 6e 74 61 69  s case it contai
2f8f1 6e 73 0d 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20  ns..** the name 
2f8f2 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
2f8f3 65 2c 20 61 73 20 6f 6e 65 20 6d 69 67 68 74 20  e, as one might 
2f8f4 66 69 6e 64 20 69 6e 20 61 6e 20 49 4e 53 45 52  find in an INSER
2f8f5 54 2c 20 44 45 4c 45 54 45 2c 0d 0a 2a 2a 20 6f  T, DELETE,..** o
2f8f6 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  r UPDATE stateme
2f8f7 6e 74 2e 20 20 4c 6f 6f 6b 20 75 70 20 74 68 61  nt.  Look up tha
2f8f8 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 73  t table in the s
2f8f9 79 6d 62 6f 6c 20 74 61 62 6c 65 20 61 6e 64 0d  ymbol table and.
2f8fa 0a 2a 2a 20 72 65 74 75 72 6e 20 61 20 70 6f 69  .** return a poi
2f8fb 6e 74 65 72 2e 20 20 53 65 74 20 61 6e 20 65 72  nter.  Set an er
2f8fc 72 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20  ror message and 
2f8fd 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  return NULL if t
2f8fe 68 65 20 74 61 62 6c 65 20 0d 0a 2a 2a 20 6e 61  he table ..** na
2f8ff 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  me is not found 
2f900 6f 72 20 69 66 20 61 6e 79 20 6f 74 68 65 72 20  or if any other 
2f901 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0d 0a 2a  error occurs...*
2f902 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
2f903 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65 20 69  ing fields are i
2f904 6e 69 74 69 61 6c 69 7a 65 64 20 61 70 70 72 6f  nitialized appro
2f905 70 72 69 61 74 65 20 69 6e 20 70 53 72 63 3a 0d  priate in pSrc:.
2f906 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 70 53 72 63 2d  .**..**    pSrc-
2f907 3e 61 5b 30 5d 2e 70 54 61 62 20 20 20 20 20 20  >a[0].pTab      
2f908 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
2f909 54 61 62 6c 65 20 6f 62 6a 65 63 74 0d 0a 2a 2a  Table object..**
2f90a 20 20 20 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70      pSrc->a[0].p
2f90b 49 6e 64 65 78 20 20 20 20 20 50 6f 69 6e 74 65  Index     Pointe
2f90c 72 20 74 6f 20 74 68 65 20 49 4e 44 45 58 45 44  r to the INDEXED
2f90d 20 42 59 20 69 6e 64 65 78 2c 20 69 66 20 74 68   BY index, if th
2f90e 65 72 65 20 69 73 20 6f 6e 65 0d 0a 2a 2a 0d 0a  ere is one..**..
2f90f 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
2f910 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
2f911 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50  3SrcListLookup(P
2f912 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
2f913 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0d 0a 20  cList *pSrc){.. 
2f914 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2f915 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53  item *pItem = pS
2f916 72 63 2d 3e 61 3b 0d 0a 20 20 54 61 62 6c 65 20  rc->a;..  Table 
2f917 2a 70 54 61 62 3b 0d 0a 20 20 61 73 73 65 72 74  *pTab;..  assert
2f918 28 20 70 49 74 65 6d 20 26 26 20 70 53 72 63 2d  ( pItem && pSrc-
2f919 3e 6e 53 72 63 3d 3d 31 20 29 3b 0d 0a 20 20 70  >nSrc==1 );..  p
2f91a 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
2f91b 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
2f91c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   0, pItem->zName
2f91d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
2f91e 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44  se);..  sqlite3D
2f91f 65 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73  eleteTable(pPars
2f920 65 2d 3e 64 62 2c 20 70 49 74 65 6d 2d 3e 70 54  e->db, pItem->pT
2f921 61 62 29 3b 0d 0a 20 20 70 49 74 65 6d 2d 3e 70  ab);..  pItem->p
2f922 54 61 62 20 3d 20 70 54 61 62 3b 0d 0a 20 20 69  Tab = pTab;..  i
2f923 66 28 20 70 54 61 62 20 29 7b 0d 0a 20 20 20 20  f( pTab ){..    
2f924 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0d 0a 20  pTab->nRef++;.. 
2f925 20 7d 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
2f926 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
2f927 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20  (pParse, pItem) 
2f928 29 7b 0d 0a 20 20 20 20 70 54 61 62 20 3d 20 30  ){..    pTab = 0
2f929 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
2f92a 20 70 54 61 62 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pTab;..}..../*.
2f92b 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b  .** Check to mak
2f92c 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e  e sure the given
2f92d 20 74 61 62 6c 65 20 69 73 20 77 72 69 74 61 62   table is writab
2f92e 6c 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f  le.  If it is no
2f92f 74 0d 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20  t..** writable, 
2f930 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
2f931 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 72 65  r message and re
2f932 74 75 72 6e 20 31 2e 20 20 49 66 20 69 74 20 69  turn 1.  If it i
2f933 73 0d 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20 72  s..** writable r
2f934 65 74 75 72 6e 20 30 3b 0d 0a 2a 2f 0d 0a 53 51  eturn 0;..*/..SQ
2f935 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2f936 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
2f937 6c 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ly(Parse *pParse
2f938 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69  , Table *pTab, i
2f939 6e 74 20 76 69 65 77 4f 6b 29 7b 0d 0a 20 20 2f  nt viewOk){..  /
2f93a 2a 20 41 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  * A table is not
2f93b 20 77 72 69 74 61 62 6c 65 20 75 6e 64 65 72 20   writable under 
2f93c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 69  the following ci
2f93d 72 63 75 6d 73 74 61 6e 63 65 73 3a 0d 0a 20 20  rcumstances:..  
2f93e 2a 2a 0d 0a 20 20 2a 2a 20 20 20 31 29 20 49 74  **..  **   1) It
2f93f 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61   is a virtual ta
2f940 62 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70 6c 65  ble and no imple
2f941 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2f942 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 0d   xUpdate method.
2f943 0a 20 20 2a 2a 20 20 20 20 20 20 68 61 73 20 62  .  **      has b
2f944 65 65 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f 72  een provided, or
2f945 0d 0a 20 20 2a 2a 20 20 20 32 29 20 49 74 20 69  ..  **   2) It i
2f946 73 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65  s a system table
2f947 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
2f948 73 74 65 72 29 2c 20 74 68 69 73 20 63 61 6c 6c  ster), this call
2f949 20 69 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 20 20   is not..  **   
2f94a 20 20 20 70 61 72 74 20 6f 66 20 61 20 6e 65 73     part of a nes
2f94b 74 65 64 20 70 61 72 73 65 20 61 6e 64 20 77 72  ted parse and wr
2f94c 69 74 61 62 6c 65 5f 73 63 68 65 6d 61 20 70 72  itable_schema pr
2f94d 61 67 6d 61 20 68 61 73 20 6e 6f 74 20 0d 0a 20  agma has not .. 
2f94e 20 2a 2a 20 20 20 20 20 20 62 65 65 6e 20 73 70   **      been sp
2f94f 65 63 69 66 69 65 64 2e 0d 0a 20 20 2a 2a 0d 0a  ecified...  **..
2f950 20 20 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63    ** In either c
2f951 61 73 65 20 6c 65 61 76 65 20 61 6e 20 65 72 72  ase leave an err
2f952 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
2f953 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
2f954 6e 6f 6e 2d 7a 65 72 6f 2e 0d 0a 20 20 2a 2f 0d  non-zero...  */.
2f955 0a 20 20 69 66 28 20 28 20 49 73 56 69 72 74 75  .  if( ( IsVirtu
2f956 61 6c 28 70 54 61 62 29 20 0d 0a 20 20 20 20 20  al(pTab) ..     
2f957 26 26 20 73 71 6c 69 74 65 33 47 65 74 56 54 61  && sqlite3GetVTa
2f958 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ble(pParse->db, 
2f959 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f  pTab)->pMod->pMo
2f95a 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30  dule->xUpdate==0
2f95b 20 29 0d 0a 20 20 20 7c 7c 20 28 20 28 70 54 61   )..   || ( (pTa
2f95c 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2f95d 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30 0d 0a 20  _Readonly)!=0.. 
2f95e 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e      && (pParse->
2f95f 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2f960 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d  TE_WriteSchema)=
2f961 3d 30 0d 0a 20 20 20 20 20 26 26 20 70 50 61 72  =0..     && pPar
2f962 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 0d  se->nested==0 ).
2f963 0a 20 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  .  ){..    sqlit
2f964 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2f965 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
2f966 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64   not be modified
2f967 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
2f968 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d  ..    return 1;.
2f969 0a 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  .  }....#ifndef 
2f96a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2f96b 0d 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20  ..  if( !viewOk 
2f96c 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  && pTab->pSelect
2f96d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
2f96e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2f96f 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25  "cannot modify %
2f970 73 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  s because it is 
2f971 61 20 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e  a view",pTab->zN
2f972 61 6d 65 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  ame);..    retur
2f973 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  n 1;..  }..#endi
2f974 66 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  f..  return 0;..
2f975 7d 0d 0a 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69  }......#if !defi
2f976 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2f977 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
2f978 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
2f979 49 47 47 45 52 29 0d 0a 2f 2a 0d 0a 2a 2a 20 45  IGGER)../*..** E
2f97a 76 61 6c 75 61 74 65 20 61 20 76 69 65 77 20 61  valuate a view a
2f97b 6e 64 20 73 74 6f 72 65 20 69 74 73 20 72 65 73  nd store its res
2f97c 75 6c 74 20 69 6e 20 61 6e 20 65 70 68 65 6d 65  ult in an epheme
2f97d 72 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 0d  ral table.  The.
2f97e 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67 75 6d  .** pWhere argum
2f97f 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  ent is an option
2f980 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  al WHERE clause 
2f981 74 68 61 74 20 72 65 73 74 72 69 63 74 73 20 74  that restricts t
2f982 68 65 0d 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f  he..** set of ro
2f983 77 73 20 69 6e 20 74 68 65 20 76 69 65 77 20 74  ws in the view t
2f984 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 61 64  hat are to be ad
2f985 64 65 64 20 74 6f 20 74 68 65 20 65 70 68 65 6d  ded to the ephem
2f986 65 72 61 6c 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  eral table...*/.
2f987 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f988 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65  void sqlite3Mate
2f989 72 69 61 6c 69 7a 65 56 69 65 77 28 0d 0a 20 20  rializeView(..  
2f98a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2f98b 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2f98c 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61  context */..  Ta
2f98d 62 6c 65 20 2a 70 56 69 65 77 2c 20 20 20 20 20  ble *pView,     
2f98e 20 20 20 2f 2a 20 56 69 65 77 20 64 65 66 69 6e     /* View defin
2f98f 69 74 69 6f 6e 20 2a 2f 0d 0a 20 20 45 78 70 72  ition */..  Expr
2f990 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20   *pWhere,       
2f991 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57 48 45   /* Optional WHE
2f992 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  RE clause to be 
2f993 61 64 64 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  added */..  int 
2f994 69 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  iCur            
2f995 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2f996 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61 6c  r for ephemerial
2f997 20 74 61 62 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20   table */..){.. 
2f998 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
2f999 3b 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75  ;..  Select *pDu
2f99a 70 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  p;..  sqlite3 *d
2f99b 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d  b = pParse->db;.
2f99c 0a 0d 0a 20 20 70 44 75 70 20 3d 20 73 71 6c 69  ...  pDup = sqli
2f99d 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
2f99e 20 70 56 69 65 77 2d 3e 70 53 65 6c 65 63 74 2c   pView->pSelect,
2f99f 20 30 29 3b 0d 0a 20 20 69 66 28 20 70 57 68 65   0);..  if( pWhe
2f9a0 72 65 20 29 7b 0d 0a 20 20 20 20 53 72 63 4c 69  re ){..    SrcLi
2f9a1 73 74 20 2a 70 46 72 6f 6d 3b 0d 0a 20 20 20 20  st *pFrom;..    
2f9a2 0d 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73  ..    pWhere = s
2f9a3 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2f9a4 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0d 0a 20  , pWhere, 0);.. 
2f9a5 20 20 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74     pFrom = sqlit
2f9a6 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2f9a7 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20  db, 0, 0, 0);.. 
2f9a8 20 20 20 69 66 28 20 70 46 72 6f 6d 20 29 7b 0d     if( pFrom ){.
2f9a9 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2f9aa 46 72 6f 6d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  From->nSrc==1 );
2f9ab 0d 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 61  ..      pFrom->a
2f9ac 5b 30 5d 2e 7a 41 6c 69 61 73 20 3d 20 73 71 6c  [0].zAlias = sql
2f9ad 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2f9ae 20 70 56 69 65 77 2d 3e 7a 4e 61 6d 65 29 3b 0d   pView->zName);.
2f9af 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b  .      pFrom->a[
2f9b0 30 5d 2e 70 53 65 6c 65 63 74 20 3d 20 70 44 75  0].pSelect = pDu
2f9b1 70 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  p;..      assert
2f9b2 28 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 4f  ( pFrom->a[0].pO
2f9b3 6e 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 61  n==0 );..      a
2f9b4 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 61 5b  ssert( pFrom->a[
2f9b5 30 5d 2e 70 55 73 69 6e 67 3d 3d 30 20 29 3b 0d  0].pUsing==0 );.
2f9b6 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
2f9b7 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2f9b8 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29  Delete(db, pDup)
2f9b9 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 44  ;..    }..    pD
2f9ba 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  up = sqlite3Sele
2f9bb 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c  ctNew(pParse, 0,
2f9bc 20 70 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c 20   pFrom, pWhere, 
2f9bd 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f9be 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
2f9bf 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2f9c0 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65  (&dest, SRT_Ephe
2f9c1 6d 54 61 62 2c 20 69 43 75 72 29 3b 0d 0a 20 20  mTab, iCur);..  
2f9c2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2f9c3 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64 65 73  arse, pDup, &des
2f9c4 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  t);..  sqlite3Se
2f9c5 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2f9c6 44 75 70 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  Dup);..}..#endif
2f9c7 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
2f9c8 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
2f9c9 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2f9ca 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
2f9cb 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65  */....#if define
2f9cc 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
2f9cd 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
2f9ce 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
2f9cf 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
2f9d0 51 55 45 52 59 29 0d 0a 2f 2a 0d 0a 2a 2a 20 47  QUERY)../*..** G
2f9d1 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70 72 65  enerate an expre
2f9d2 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20 69 6d  ssion tree to im
2f9d3 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48 45 52  plement the WHER
2f9d4 45 2c 20 4f 52 44 45 52 20 42 59 2c 0d 0a 2a 2a  E, ORDER BY,..**
2f9d5 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53 45   and LIMIT/OFFSE
2f9d6 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45 4c  T portion of DEL
2f9d7 45 54 45 20 61 6e 64 20 55 50 44 41 54 45 20 73  ETE and UPDATE s
2f9d8 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a  tatements...**..
2f9d9 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52  **     DELETE FR
2f9da 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48  OM table_wxyz WH
2f9db 45 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42 59  ERE a<5 ORDER BY
2f9dc 20 61 20 4c 49 4d 49 54 20 31 3b 0d 0a 2a 2a 20   a LIMIT 1;..** 
2f9dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9de 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
2f9df 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2f9e0 5f 5f 5f 5f 5f 5f 2f 0d 0a 2a 2a 20 20 20 20 20  ______/..**     
2f9e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9e2 20 20 20 20 20 20 20 20 20 20 70 4c 69 6d 69 74            pLimit
2f9e3 57 68 65 72 65 20 28 70 49 6e 43 6c 61 75 73 65  Where (pInClause
2f9e4 29 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  )..*/..SQLITE_PR
2f9e5 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
2f9e6 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 0d 0a  te3LimitWhere(..
2f9e7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f9e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f9e9 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2f9ea 74 65 78 74 20 2a 2f 0d 0a 20 20 53 72 63 4c 69  text */..  SrcLi
2f9eb 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20  st *pSrc,       
2f9ec 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46          /* the F
2f9ed 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68  ROM clause -- wh
2f9ee 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63  ich tables to sc
2f9ef 61 6e 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  an */..  Expr *p
2f9f0 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20  Where,          
2f9f1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2f9f2 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
2f9f3 62 65 20 6e 75 6c 6c 20 2a 2f 0d 0a 20 20 45 78  be null */..  Ex
2f9f4 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2f9f5 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
2f9f6 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2f9f7 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
2f9f8 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d  */..  Expr *pLim
2f9f9 69 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  it,             
2f9fa 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
2f9fb 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
2f9fc 6e 75 6c 6c 20 2a 2f 0d 0a 20 20 45 78 70 72 20  null */..  Expr 
2f9fd 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20  *pOffset,       
2f9fe 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
2f9ff 46 46 53 45 54 20 63 6c 61 75 73 65 2e 20 20 4d  FFSET clause.  M
2fa00 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0d 0a 20  ay be null */.. 
2fa01 20 63 68 61 72 20 2a 7a 53 74 6d 74 54 79 70 65   char *zStmtType
2fa02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fa03 20 45 69 74 68 65 72 20 44 45 4c 45 54 45 20 6f   Either DELETE o
2fa04 72 20 55 50 44 41 54 45 2e 20 20 46 6f 72 20 65  r UPDATE.  For e
2fa05 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 20 2a  rror messages. *
2fa06 2f 0d 0a 29 7b 0d 0a 20 20 45 78 70 72 20 2a 70  /..){..  Expr *p
2fa07 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e 55 4c  WhereRowid = NUL
2fa08 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72  L;    /* WHERE r
2fa09 6f 77 69 64 20 2e 2e 20 2a 2f 0d 0a 20 20 45 78  owid .. */..  Ex
2fa0a 70 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d 20  pr *pInClause = 
2fa0b 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57 48  NULL;      /* WH
2fa0c 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20 73  ERE rowid IN ( s
2fa0d 65 6c 65 63 74 20 29 20 2a 2f 0d 0a 20 20 45 78  elect ) */..  Ex
2fa0e 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64  pr *pSelectRowid
2fa0f 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45   = NULL;   /* SE
2fa10 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a  LECT rowid ... *
2fa11 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  /..  ExprList *p
2fa12 45 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20  EList = NULL;   
2fa13 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
2fa14 6c 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f  list contaning o
2fa15 6e 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69 64  nly pSelectRowid
2fa16 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a   */..  SrcList *
2fa17 70 53 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c  pSelectSrc = NUL
2fa18 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f  L;  /* SELECT ro
2fa19 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28  wid FROM x ... (
2fa1a 64 75 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0d  dup of pSrc) */.
2fa1b 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
2fa1c 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20  ct = NULL;      
2fa1d 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45  /* Complete SELE
2fa1e 43 54 20 74 72 65 65 20 2a 2f 0d 0a 0d 0a 20 20  CT tree */....  
2fa1f 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
2fa20 65 72 65 20 69 73 6e 27 74 20 61 6e 20 4f 52 44  ere isn't an ORD
2fa21 45 52 20 42 59 20 77 69 74 68 6f 75 74 20 61 20  ER BY without a 
2fa22 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0d 0a 20  LIMIT clause... 
2fa23 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4f 72 64 65   */..  if( pOrde
2fa24 72 42 79 20 26 26 20 28 70 4c 69 6d 69 74 20 3d  rBy && (pLimit =
2fa25 3d 20 30 29 20 29 20 7b 0d 0a 20 20 20 20 73 71  = 0) ) {..    sq
2fa26 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2fa27 61 72 73 65 2c 20 22 4f 52 44 45 52 20 42 59 20  arse, "ORDER BY 
2fa28 77 69 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f 6e  without LIMIT on
2fa29 20 25 73 22 2c 20 7a 53 74 6d 74 54 79 70 65 29   %s", zStmtType)
2fa2a 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69  ;..    goto limi
2fa2b 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
2fa2c 32 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  2;..  }....  /* 
2fa2d 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  We only need to 
2fa2e 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63  generate a selec
2fa2f 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20  t expression if 
2fa30 74 68 65 72 65 0d 0a 20 20 2a 2a 20 69 73 20 61  there..  ** is a
2fa31 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65   limit/offset te
2fa32 72 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0d 0a  rm to enforce...
2fa33 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4c 69 6d    */..  if( pLim
2fa34 69 74 20 3d 3d 20 30 20 29 20 7b 0d 0a 20 20 20  it == 0 ) {..   
2fa35 20 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20 69 73   /* if pLimit is
2fa36 20 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20 77   null, pOffset w
2fa37 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6e 75  ill always be nu
2fa38 6c 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0d 0a  ll as well. */..
2fa39 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 66 66      assert( pOff
2fa3a 73 65 74 20 3d 3d 20 30 20 29 3b 0d 0a 20 20 20  set == 0 );..   
2fa3b 20 72 65 74 75 72 6e 20 70 57 68 65 72 65 3b 0d   return pWhere;.
2fa3c 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e  .  }....  /* Gen
2fa3d 65 72 61 74 65 20 61 20 73 65 6c 65 63 74 20 65  erate a select e
2fa3e 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74  xpression tree t
2fa3f 6f 20 65 6e 66 6f 72 63 65 20 74 68 65 20 6c 69  o enforce the li
2fa40 6d 69 74 2f 6f 66 66 73 65 74 20 0d 0a 20 20 2a  mit/offset ..  *
2fa41 2a 20 74 65 72 6d 20 66 6f 72 20 74 68 65 20 44  * term for the D
2fa42 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
2fa43 73 74 61 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20  statement.  For 
2fa44 65 78 61 6d 70 6c 65 3a 0d 0a 20 20 2a 2a 20 20  example:..  **  
2fa45 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62   DELETE FROM tab
2fa46 6c 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d  le_a WHERE col1=
2fa47 31 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20  1 ORDER BY col2 
2fa48 4c 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31  LIMIT 1 OFFSET 1
2fa49 0d 0a 20 20 2a 2a 20 62 65 63 6f 6d 65 73 3a 0d  ..  ** becomes:.
2fa4a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46  .  **   DELETE F
2fa4b 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52  ROM table_a WHER
2fa4c 45 20 72 6f 77 69 64 20 49 4e 20 28 20 0d 0a 20  E rowid IN ( .. 
2fa4d 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72   **     SELECT r
2fa4e 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c 65 5f  owid FROM table_
2fa4f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f  a WHERE col1=1 O
2fa50 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d  RDER BY col2 LIM
2fa51 49 54 20 31 20 4f 46 46 53 45 54 20 31 0d 0a 20  IT 1 OFFSET 1.. 
2fa52 20 2a 2a 20 20 20 29 3b 0d 0a 20 20 2a 2f 0d 0a   **   );..  */..
2fa53 0d 0a 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64  ..  pSelectRowid
2fa54 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2fa55 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20  pParse, TK_ROW, 
2fa56 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 69 66 28  0, 0, 0);..  if(
2fa57 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d   pSelectRowid ==
2fa58 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
2fa59 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
2fa5a 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c  ..  pEList = sql
2fa5b 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2fa5c 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53  nd(pParse, 0, pS
2fa5d 65 6c 65 63 74 52 6f 77 69 64 29 3b 0d 0a 20 20  electRowid);..  
2fa5e 69 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30 20  if( pEList == 0 
2fa5f 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65  ) goto limit_whe
2fa60 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0d 0a 0d  re_cleanup_2;...
2fa61 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65 20  .  /* duplicate 
2fa62 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2fa63 61 73 20 69 74 20 69 73 20 6e 65 65 64 65 64 20  as it is needed 
2fa64 62 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c 45  by both the DELE
2fa65 54 45 2f 55 50 44 41 54 45 20 74 72 65 65 0d 0a  TE/UPDATE tree..
2fa66 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c    ** and the SEL
2fa67 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0d  ECT subtree. */.
2fa68 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20  .  pSelectSrc = 
2fa69 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
2fa6a 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  p(pParse->db, pS
2fa6b 72 63 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 70  rc, 0);..  if( p
2fa6c 53 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29  SelectSrc == 0 )
2fa6d 20 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45   {..    sqlite3E
2fa6e 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
2fa6f 61 72 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74  arse->db, pEList
2fa70 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d  );..    goto lim
2fa71 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2fa72 5f 32 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  _2;..  }....  /*
2fa73 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 53 45   generate the SE
2fa74 4c 45 43 54 20 65 78 70 72 65 73 73 69 6f 6e 20  LECT expression 
2fa75 74 72 65 65 2e 20 2a 2f 0d 0a 20 20 70 53 65 6c  tree. */..  pSel
2fa76 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2fa77 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 70 45  ectNew(pParse,pE
2fa78 4c 69 73 74 2c 70 53 65 6c 65 63 74 53 72 63 2c  List,pSelectSrc,
2fa79 70 57 68 65 72 65 2c 30 2c 30 2c 0d 0a 20 20 20  pWhere,0,0,..   
2fa7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa7b 20 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72            pOrder
2fa7c 42 79 2c 30 2c 70 4c 69 6d 69 74 2c 70 4f 66 66  By,0,pLimit,pOff
2fa7d 73 65 74 29 3b 0d 0a 20 20 69 66 28 20 70 53 65  set);..  if( pSe
2fa7e 6c 65 63 74 20 3d 3d 20 30 20 29 20 72 65 74 75  lect == 0 ) retu
2fa7f 72 6e 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 6e 6f  rn 0;....  /* no
2fa80 77 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 6e  w generate the n
2fa81 65 77 20 57 48 45 52 45 20 72 6f 77 69 64 20 49  ew WHERE rowid I
2fa82 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68 65  N clause for the
2fa83 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45 20 2a   DELETE/UDPATE *
2fa84 2f 0d 0a 20 20 70 57 68 65 72 65 52 6f 77 69 64  /..  pWhereRowid
2fa85 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2fa86 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20  pParse, TK_ROW, 
2fa87 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 69 66 28  0, 0, 0);..  if(
2fa88 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d 3d 20   pWhereRowid == 
2fa89 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77  0 ) goto limit_w
2fa8a 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b 0d  here_cleanup_1;.
2fa8b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20 73  .  pInClause = s
2fa8c 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2fa8d 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65 72  se, TK_IN, pWher
2fa8e 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0d 0a  eRowid, 0, 0);..
2fa8f 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20    if( pInClause 
2fa90 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
2fa91 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
2fa92 31 3b 0d 0a 0d 0a 20 20 70 49 6e 43 6c 61 75 73  1;....  pInClaus
2fa93 65 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70  e->x.pSelect = p
2fa94 53 65 6c 65 63 74 3b 0d 0a 20 20 70 49 6e 43 6c  Select;..  pInCl
2fa95 61 75 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  ause->flags |= E
2fa96 50 5f 78 49 73 53 65 6c 65 63 74 3b 0d 0a 20 20  P_xIsSelect;..  
2fa97 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
2fa98 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 49 6e  ight(pParse, pIn
2fa99 43 6c 61 75 73 65 29 3b 0d 0a 20 20 72 65 74 75  Clause);..  retu
2fa9a 72 6e 20 70 49 6e 43 6c 61 75 73 65 3b 0d 0a 0d  rn pInClause;...
2fa9b 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20  .  /* something 
2fa9c 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63 6c 65 61  went wrong. clea
2fa9d 6e 20 75 70 20 61 6e 79 74 68 69 6e 67 20 61 6c  n up anything al
2fa9e 6c 6f 63 61 74 65 64 2e 20 2a 2f 0d 0a 6c 69 6d  located. */..lim
2fa9f 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2faa0 5f 31 3a 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  _1:..  sqlite3Se
2faa1 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73  lectDelete(pPars
2faa2 65 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  e->db, pSelect);
2faa3 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 0d  ..  return 0;...
2faa4 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65  .limit_where_cle
2faa5 61 6e 75 70 5f 32 3a 0d 0a 20 20 73 71 6c 69 74  anup_2:..  sqlit
2faa6 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61  e3ExprDelete(pPa
2faa7 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72 65 29  rse->db, pWhere)
2faa8 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
2faa9 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
2faaa 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 29  e->db, pOrderBy)
2faab 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
2faac 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2faad 62 2c 20 70 4c 69 6d 69 74 29 3b 0d 0a 20 20 73  b, pLimit);..  s
2faae 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2faaf 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66  (pParse->db, pOf
2fab0 66 73 65 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e  fset);..  return
2fab1 20 30 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f   0;..}..#endif /
2fab2 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
2fab3 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44  _ENABLE_UPDATE_D
2fab4 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20  ELETE_LIMIT) && 
2fab5 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2fab6 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a  OMIT_SUBQUERY) *
2fab7 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65  /..../*..** Gene
2fab8 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20  rate code for a 
2fab9 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74 61 74  DELETE FROM stat
2faba 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ement...**..**  
2fabb 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
2fabc 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45 20  able_wxyz WHERE 
2fabd 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20 4e 55  a<5 AND b NOT NU
2fabe 4c 4c 3b 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  LL;..**         
2fabf 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f          \_______
2fac0 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  _/       \______
2fac1 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0d 0a 2a 2a 20  __________/..** 
2fac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fac3 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20 20   pTabList       
2fac4 20 20 20 20 20 20 20 70 57 68 65 72 65 0d 0a 2a         pWhere..*
2fac5 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
2fac6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2fac7 6c 65 74 65 46 72 6f 6d 28 0d 0a 20 20 50 61 72  leteFrom(..  Par
2fac8 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2fac9 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2faca 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  r context */..  
2facb 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2facc 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61  t,     /* The ta
2facd 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77  ble from which w
2face 65 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20  e should delete 
2facf 74 68 69 6e 67 73 20 2a 2f 0d 0a 20 20 45 78 70  things */..  Exp
2fad0 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
2fad1 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2fad2 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
2fad3 20 6e 75 6c 6c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   null */..){..  
2fad4 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2fad5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2fad6 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
2fad7 6e 67 69 6e 65 20 2a 2f 0d 0a 20 20 54 61 62 6c  ngine */..  Tabl
2fad8 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
2fad9 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2fada 66 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72  from which recor
2fadb 64 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74  ds will be delet
2fadc 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  ed */..  const c
2fadd 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
2fade 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
2fadf 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
2fae0 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 6e 64 2c 20   */..  int end, 
2fae1 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  addr = 0;     /*
2fae2 20 41 20 63 6f 75 70 6c 65 20 61 64 64 72 65 73   A couple addres
2fae3 73 65 73 20 6f 66 20 67 65 6e 65 72 61 74 65 64  ses of generated
2fae4 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   code */..  int 
2fae5 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
2fae6 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
2fae7 65 72 20 2a 2f 0d 0a 20 20 57 68 65 72 65 49 6e  er */..  WhereIn
2fae8 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
2fae9 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
2faea 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63  bout the WHERE c
2faeb 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 49 6e 64 65  lause */..  Inde
2faec 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
2faed 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
2faee 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 6f  g over indices o
2faef 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a  f the table */..
2faf0 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2faf1 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
2faf2 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2faf3 6f 72 20 70 54 61 62 20 2a 2f 0d 0a 20 20 73 71  or pTab */..  sq
2faf4 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2faf5 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74       /* Main dat
2faf6 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
2faf7 2a 2f 0d 0a 20 20 41 75 74 68 43 6f 6e 74 65 78  */..  AuthContex
2faf8 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20  t sContext;  /* 
2faf9 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f  Authorization co
2fafa 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 4e 61 6d 65  ntext */..  Name
2fafb 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
2fafc 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
2fafd 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78  xt to resolve ex
2fafe 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0d  pressions in */.
2faff 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
2fb00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2fb01 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0d  abase number */.
2fb02 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d 20  .  int memCnt = 
2fb03 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  -1;       /* Mem
2fb04 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
2fb05 72 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 69 6e  r change countin
2fb06 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 61 75  g */..  int rcau
2fb07 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  th;            /
2fb08 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * Value returned
2fb09 20 62 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   by authorizatio
2fb0a 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 0d  n callback */...
2fb0b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2fb0c 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20  OMIT_TRIGGER..  
2fb0d 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20  int isView;     
2fb0e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fb0f 54 72 75 65 20 69 66 20 61 74 74 65 6d 70 74 69  True if attempti
2fb10 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  ng to delete fro
2fb11 6d 20 61 20 76 69 65 77 20 2a 2f 0d 0a 20 20 54  m a view */..  T
2fb12 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2fb13 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
2fb14 69 73 74 20 6f 66 20 74 61 62 6c 65 20 74 72 69  ist of table tri
2fb15 67 67 65 72 73 2c 20 69 66 20 72 65 71 75 69 72  ggers, if requir
2fb16 65 64 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ed */..#endif...
2fb17 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74  .  memset(&sCont
2fb18 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ext, 0, sizeof(s
2fb19 43 6f 6e 74 65 78 74 29 29 3b 0d 0a 20 20 64 62  Context));..  db
2fb1a 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a   = pParse->db;..
2fb1b 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
2fb1c 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
2fb1d 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 67  Failed ){..    g
2fb1e 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2fb1f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 20  cleanup;..  }.. 
2fb20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73   assert( pTabLis
2fb21 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0d 0a 0d  t->nSrc==1 );...
2fb22 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
2fb23 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20   table which we 
2fb24 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65 2e 20  want to delete. 
2fb25 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 73 20   This table has 
2fb26 74 6f 20 62 65 0d 0a 20 20 2a 2a 20 70 75 74 20  to be..  ** put 
2fb27 69 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73 74  in an SrcList st
2fb28 72 75 63 74 75 72 65 20 62 65 63 61 75 73 65 20  ructure because 
2fb29 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62 72  some of the subr
2fb2a 6f 75 74 69 6e 65 73 20 77 65 0d 0a 20 20 2a 2a  outines we..  **
2fb2b 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67   will be calling
2fb2c 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f   are designed to
2fb2d 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69   work with multi
2fb2e 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65  ple tables and e
2fb2f 78 70 65 63 74 0d 0a 20 20 2a 2a 20 61 6e 20 53  xpect..  ** an S
2fb30 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74 65  rcList* paramete
2fb31 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73  r instead of jus
2fb32 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61 6d  t a Table* param
2fb33 65 74 65 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70  eter...  */..  p
2fb34 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
2fb35 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
2fb36 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0d 0a 20  e, pTabList);.. 
2fb37 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20   if( pTab==0 )  
2fb38 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
2fb39 5f 63 6c 65 61 6e 75 70 3b 0d 0a 0d 0a 20 20 2f  _cleanup;....  /
2fb3a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
2fb3b 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67  we have any trig
2fb3c 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20  gers and if the 
2fb3d 74 61 62 6c 65 20 62 65 69 6e 67 0d 0a 20 20 2a  table being..  *
2fb3e 2a 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 69  * deleted from i
2fb3f 73 20 61 20 76 69 65 77 0d 0a 20 20 2a 2f 0d 0a  s a view..  */..
2fb40 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2fb41 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20 70  MIT_TRIGGER..  p
2fb42 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2fb43 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
2fb44 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
2fb45 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b 0d 0a  DELETE, 0, 0);..
2fb46 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d    isView = pTab-
2fb47 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0d 0a 23 65  >pSelect!=0;..#e
2fb48 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 70 54  lse..# define pT
2fb49 72 69 67 67 65 72 20 30 0d 0a 23 20 64 65 66 69  rigger 0..# defi
2fb4a 6e 65 20 69 73 56 69 65 77 20 30 0d 0a 23 65 6e  ne isView 0..#en
2fb4b 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
2fb4c 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 23 20  TE_OMIT_VIEW..# 
2fb4d 75 6e 64 65 66 20 69 73 56 69 65 77 0d 0a 23 20  undef isView..# 
2fb4e 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0d  define isView 0.
2fb4f 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20  .#endif....  /* 
2fb50 49 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c  If pTab is reall
2fb51 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73  y a view, make s
2fb52 75 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20  ure it has been 
2fb53 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0d 0a 20 20  initialized...  
2fb54 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
2fb55 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
2fb56 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
2fb57 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 64  ) ){..    goto d
2fb58 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
2fb59 75 70 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  up;..  }....  if
2fb5a 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f  ( sqlite3IsReadO
2fb5b 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62  nly(pParse, pTab
2fb5c 2c 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29  , (pTrigger?1:0)
2fb5d 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 64  ) ){..    goto d
2fb5e 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
2fb5f 75 70 3b 0d 0a 20 20 7d 0d 0a 20 20 69 44 62 20  up;..  }..  iDb 
2fb60 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2fb61 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
2fb62 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 61 73  >pSchema);..  as
2fb63 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44  sert( iDb<db->nD
2fb64 62 20 29 3b 0d 0a 20 20 7a 44 62 20 3d 20 64 62  b );..  zDb = db
2fb65 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2fb66 3b 0d 0a 20 20 72 63 61 75 74 68 20 3d 20 73 71  ;..  rcauth = sq
2fb67 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2fb68 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
2fb69 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LETE, pTab->zNam
2fb6a 65 2c 20 30 2c 20 7a 44 62 29 3b 0d 0a 20 20 61  e, 0, zDb);..  a
2fb6b 73 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d 53  ssert( rcauth==S
2fb6c 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75  QLITE_OK || rcau
2fb6d 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  th==SQLITE_DENY 
2fb6e 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  || rcauth==SQLIT
2fb6f 45 5f 49 47 4e 4f 52 45 20 29 3b 0d 0a 20 20 69  E_IGNORE );..  i
2fb70 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  f( rcauth==SQLIT
2fb71 45 5f 44 45 4e 59 20 29 7b 0d 0a 20 20 20 20 67  E_DENY ){..    g
2fb72 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2fb73 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 20  cleanup;..  }.. 
2fb74 20 61 73 73 65 72 74 28 21 69 73 56 69 65 77 20   assert(!isView 
2fb75 7c 7c 20 70 54 72 69 67 67 65 72 29 3b 0d 0a 0d  || pTrigger);...
2fb76 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20 63 75  .  /* Assign  cu
2fb77 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 74  rsor number to t
2fb78 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61 6c 6c  he table and all
2fb79 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0d 0a 20   its indices... 
2fb7a 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
2fb7b 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
2fb7c 20 29 3b 0d 0a 20 20 69 43 75 72 20 3d 20 70 54   );..  iCur = pT
2fb7d 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75  abList->a[0].iCu
2fb7e 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  rsor = pParse->n
2fb7f 54 61 62 2b 2b 3b 0d 0a 20 20 66 6f 72 28 70 49  Tab++;..  for(pI
2fb80 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2fb81 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2fb82 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 70  ->pNext){..    p
2fb83 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a  Parse->nTab++;..
2fb84 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 74 61 72    }....  /* Star
2fb85 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65  t the view conte
2fb86 78 74 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  xt..  */..  if( 
2fb87 69 73 56 69 65 77 20 29 7b 0d 0a 20 20 20 20 73  isView ){..    s
2fb88 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
2fb89 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73  tPush(pParse, &s
2fb8a 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a  Context, pTab->z
2fb8b 4e 61 6d 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Name);..  }.... 
2fb8c 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61   /* Begin genera
2fb8d 74 69 6e 67 20 63 6f 64 65 2e 0d 0a 20 20 2a 2f  ting code...  */
2fb8e 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
2fb8f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d  etVdbe(pParse);.
2fb90 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0d 0a  .  if( v==0 ){..
2fb91 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
2fb92 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  from_cleanup;.. 
2fb93 20 7d 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65   }..  if( pParse
2fb94 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71  ->nested==0 ) sq
2fb95 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68  lite3VdbeCountCh
2fb96 61 6e 67 65 73 28 76 29 3b 0d 0a 20 20 73 71 6c  anges(v);..  sql
2fb97 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
2fb98 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
2fb99 31 2c 20 69 44 62 29 3b 0d 0a 0d 0a 20 20 2f 2a  1, iDb);....  /*
2fb9a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e   If we are tryin
2fb9b 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  g to delete from
2fb9c 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
2fb9d 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0d   that view into.
2fb9e 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61  .  ** a ephemera
2fb9f 6c 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a  l table...  */..
2fba0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2fba1 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
2fba2 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2fba3 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0d  E_OMIT_TRIGGER).
2fba4 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
2fba5 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74  ..    sqlite3Mat
2fba6 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61  erializeView(pPa
2fba7 72 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72  rse, pTab, pWher
2fba8 65 2c 20 69 43 75 72 29 3b 0d 0a 20 20 7d 0d 0a  e, iCur);..  }..
2fba9 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 52  #endif....  /* R
2fbaa 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d  esolve the colum
2fbab 6e 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57  n names in the W
2fbac 48 45 52 45 20 63 6c 61 75 73 65 2e 0d 0a 20 20  HERE clause...  
2fbad 2a 2f 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e  */..  memset(&sN
2fbae 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2fbaf 29 29 3b 0d 0a 20 20 73 4e 43 2e 70 50 61 72 73  ));..  sNC.pPars
2fbb0 65 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 73  e = pParse;..  s
2fbb1 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
2fbb2 61 62 4c 69 73 74 3b 0d 0a 20 20 69 66 28 20 73  abList;..  if( s
2fbb3 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2fbb4 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68  rNames(&sNC, pWh
2fbb5 65 72 65 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74  ere) ){..    got
2fbb6 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
2fbb7 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  eanup;..  }.... 
2fbb8 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
2fbb9 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68  he counter of th
2fbba 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2fbbb 20 64 65 6c 65 74 65 64 2c 20 69 66 0d 0a 20 20   deleted, if..  
2fbbc 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69  ** we are counti
2fbbd 6e 67 20 72 6f 77 73 2e 0d 0a 20 20 2a 2f 0d 0a  ng rows...  */..
2fbbe 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
2fbbf 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
2fbc0 77 73 20 29 7b 0d 0a 20 20 20 20 6d 65 6d 43 6e  ws ){..    memCn
2fbc1 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2fbc2 65 6d 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  em;..    sqlite3
2fbc3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fbc4 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d  _Integer, 0, mem
2fbc5 43 6e 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 23 69  Cnt);..  }....#i
2fbc6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2fbc7 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d  T_TRUNCATE_OPTIM
2fbc8 49 5a 41 54 49 4f 4e 0d 0a 20 20 2f 2a 20 53 70  IZATION..  /* Sp
2fbc9 65 63 69 61 6c 20 63 61 73 65 3a 20 41 20 44 45  ecial case: A DE
2fbca 4c 45 54 45 20 77 69 74 68 6f 75 74 20 61 20 57  LETE without a W
2fbcb 48 45 52 45 20 63 6c 61 75 73 65 20 64 65 6c 65  HERE clause dele
2fbcc 74 65 73 20 65 76 65 72 79 74 68 69 6e 67 2e 0d  tes everything..
2fbcd 0a 20 20 2a 2a 20 49 74 20 69 73 20 65 61 73 69  .  ** It is easi
2fbce 65 72 20 6a 75 73 74 20 74 6f 20 65 72 61 73 65  er just to erase
2fbcf 20 74 68 65 20 77 68 6f 6c 65 20 74 61 62 6c 65   the whole table
2fbd0 2e 20 50 72 69 6f 72 20 74 6f 20 76 65 72 73 69  . Prior to versi
2fbd1 6f 6e 20 33 2e 36 2e 35 2c 0d 0a 20 20 2a 2a 20  on 3.6.5,..  ** 
2fbd2 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  this optimizatio
2fbd3 6e 20 63 61 75 73 65 64 20 74 68 65 20 72 6f 77  n caused the row
2fbd4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 74   change count (t
2fbd5 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
2fbd6 64 20 62 79 20 0d 0a 20 20 2a 2a 20 41 50 49 20  d by ..  ** API 
2fbd7 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33  function sqlite3
2fbd8 5f 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 29 20  _count_changes) 
2fbd9 74 6f 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72  to be set incorr
2fbda 65 63 74 6c 79 2e 20 20 2a 2f 0d 0a 20 20 69 66  ectly.  */..  if
2fbdb 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45  ( rcauth==SQLITE
2fbdc 5f 4f 4b 20 26 26 20 70 57 68 65 72 65 3d 3d 30  _OK && pWhere==0
2fbdd 20 26 26 20 21 70 54 72 69 67 67 65 72 20 26 26   && !pTrigger &&
2fbde 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
2fbdf 29 20 0d 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c  ) ..   && 0==sql
2fbe0 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70  ite3FkRequired(p
2fbe1 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
2fbe2 30 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 61 73  0)..  ){..    as
2fbe3 73 65 72 74 28 20 21 69 73 56 69 65 77 20 29 3b  sert( !isView );
2fbe4 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
2fbe5 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6c  eAddOp4(v, OP_Cl
2fbe6 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  ear, pTab->tnum,
2fbe7 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c 0d 0a 20   iDb, memCnt,.. 
2fbe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbe9 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65       pTab->zName
2fbea 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0d 0a 20  , P4_STATIC);.. 
2fbeb 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
2fbec 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2fbed 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2fbee 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  ){..      assert
2fbef 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
2fbf0 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
2fbf1 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2fbf2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fbf3 5f 43 6c 65 61 72 2c 20 70 49 64 78 2d 3e 74 6e  _Clear, pIdx->tn
2fbf4 75 6d 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 7d  um, iDb);..    }
2fbf5 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69  ..  }else..#endi
2fbf6 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2fbf7 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49  _TRUNCATE_OPTIMI
2fbf8 5a 41 54 49 4f 4e 20 2a 2f 0d 0a 20 20 2f 2a 20  ZATION */..  /* 
2fbf9 54 68 65 20 75 73 75 61 6c 20 63 61 73 65 3a 20  The usual case: 
2fbfa 54 68 65 72 65 20 69 73 20 61 20 57 48 45 52 45  There is a WHERE
2fbfb 20 63 6c 61 75 73 65 20 73 6f 20 77 65 20 68 61   clause so we ha
2fbfc 76 65 20 74 6f 20 73 63 61 6e 20 74 68 72 6f 75  ve to scan throu
2fbfd 67 68 0d 0a 20 20 2a 2a 20 74 68 65 20 74 61 62  gh..  ** the tab
2fbfe 6c 65 20 61 6e 64 20 70 69 63 6b 20 77 68 69 63  le and pick whic
2fbff 68 20 72 65 63 6f 72 64 73 20 74 6f 20 64 65 6c  h records to del
2fc00 65 74 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 7b 0d  ete...  */..  {.
2fc01 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 53 65 74  .    int iRowSet
2fc02 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2fc03 6d 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72  m;   /* Register
2fc04 20 66 6f 72 20 72 6f 77 73 65 74 20 6f 66 20 72   for rowset of r
2fc05 6f 77 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ows to delete */
2fc06 0d 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 69 64  ..    int iRowid
2fc07 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2fc08 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f  m;    /* Used fo
2fc09 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69 64 20  r storing rowid 
2fc0a 76 61 6c 75 65 73 2e 20 2a 2f 0d 0a 20 20 20 20  values. */..    
2fc0b 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
2fc0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc0d 2f 2a 20 41 63 74 75 61 6c 20 72 65 67 69 73 74  /* Actual regist
2fc0e 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f  er containing ro
2fc0f 77 69 64 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f  wids */....    /
2fc10 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77 69 64 73  * Collect rowids
2fc11 20 6f 66 20 65 76 65 72 79 20 72 6f 77 20 74 6f   of every row to
2fc12 20 62 65 20 64 65 6c 65 74 65 64 2e 0d 0a 20 20   be deleted...  
2fc13 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65    */..    sqlite
2fc14 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2fc15 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77 53  P_Null, 0, iRowS
2fc16 65 74 29 3b 0d 0a 20 20 20 20 70 57 49 6e 66 6f  et);..    pWInfo
2fc17 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
2fc18 65 67 69 6e 28 0d 0a 20 20 20 20 20 20 20 20 70  egin(..        p
2fc19 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
2fc1a 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 57   pWhere, 0, 0, W
2fc1b 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
2fc1c 4f 4b 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20  OK..    );..    
2fc1d 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20  if( pWInfo==0 ) 
2fc1e 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
2fc1f 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20 20 72  _cleanup;..    r
2fc20 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  egRowid = sqlite
2fc21 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
2fc22 6d 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  mn(pParse, pTab,
2fc23 20 2d 31 2c 20 69 43 75 72 2c 20 69 52 6f 77 69   -1, iCur, iRowi
2fc24 64 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  d);..    sqlite3
2fc25 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fc26 5f 52 6f 77 53 65 74 41 64 64 2c 20 69 52 6f 77  _RowSetAdd, iRow
2fc27 53 65 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d  Set, regRowid);.
2fc28 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  .    if( db->fla
2fc29 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
2fc2a 74 52 6f 77 73 20 29 7b 0d 0a 20 20 20 20 20 20  tRows ){..      
2fc2b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fc2c 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
2fc2d 6d 65 6d 43 6e 74 2c 20 31 29 3b 0d 0a 20 20 20  memCnt, 1);..   
2fc2e 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 57   }..    sqlite3W
2fc2f 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
2fc30 0d 0a 0d 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74  ....    /* Delet
2fc31 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f  e every item who
2fc32 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74  se key was writt
2fc33 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64  en to the list d
2fc34 75 72 69 6e 67 20 74 68 65 0d 0a 20 20 20 20 2a  uring the..    *
2fc35 2a 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e  * database scan.
2fc36 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c    We have to del
2fc37 65 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20  ete items after 
2fc38 74 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70  the scan is comp
2fc39 6c 65 74 65 0d 0a 20 20 20 20 2a 2a 20 62 65 63  lete..    ** bec
2fc3a 61 75 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e  ause deleting an
2fc3b 20 69 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65   item can change
2fc3c 20 74 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e   the scan order.
2fc3d 20 20 2a 2f 0d 0a 20 20 20 20 65 6e 64 20 3d 20    */..    end = 
2fc3e 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2fc3f 61 62 65 6c 28 76 29 3b 0d 0a 0d 0a 20 20 20 20  abel(v);....    
2fc40 2f 2a 20 55 6e 6c 65 73 73 20 74 68 69 73 20 69  /* Unless this i
2fc41 73 20 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 63  s a view, open c
2fc42 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 74  ursors for the t
2fc43 61 62 6c 65 20 77 65 20 61 72 65 20 0d 0a 20 20  able we are ..  
2fc44 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 66 72    ** deleting fr
2fc45 6f 6d 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69  om and all its i
2fc46 6e 64 69 63 65 73 2e 20 49 66 20 74 68 69 73 20  ndices. If this 
2fc47 69 73 20 61 20 76 69 65 77 2c 20 74 68 65 6e 20  is a view, then 
2fc48 74 68 65 0d 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79  the..    ** only
2fc49 20 65 66 66 65 63 74 20 74 68 69 73 20 73 74 61   effect this sta
2fc4a 74 65 6d 65 6e 74 20 68 61 73 20 69 73 20 74 6f  tement has is to
2fc4b 20 66 69 72 65 20 74 68 65 20 49 4e 53 54 45 41   fire the INSTEA
2fc4c 44 20 4f 46 20 0d 0a 20 20 20 20 2a 2a 20 74 72  D OF ..    ** tr
2fc4d 69 67 67 65 72 73 2e 20 20 2a 2f 0d 0a 20 20 20  iggers.  */..   
2fc4e 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0d   if( !isView ){.
2fc4f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70  .      sqlite3Op
2fc50 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65  enTableAndIndice
2fc51 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
2fc52 69 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  iCur, OP_OpenWri
2fc53 74 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  te);..    }.... 
2fc54 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2fc55 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2fc56 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 69 52  P_RowSetRead, iR
2fc57 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69 52 6f 77  owSet, end, iRow
2fc58 69 64 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 44  id);....    /* D
2fc59 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a 2f  elete the row */
2fc5a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2fc5b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2fc5c 4c 45 0d 0a 20 20 20 20 69 66 28 20 49 73 56 69  LE..    if( IsVi
2fc5d 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a  rtual(pTab) ){..
2fc5e 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2fc5f 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
2fc60 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47   char *)sqlite3G
2fc61 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
2fc62 62 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  b);..      sqlit
2fc63 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62  e3VtabMakeWritab
2fc64 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  le(pParse, pTab)
2fc65 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2fc66 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2fc67 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 31 2c 20  _VUpdate, 0, 1, 
2fc68 69 52 6f 77 69 64 2c 20 70 56 54 61 62 2c 20 50  iRowid, pVTab, P
2fc69 34 5f 56 54 41 42 29 3b 0d 0a 20 20 20 20 20 20  4_VTAB);..      
2fc6a 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2fc6b 65 50 35 28 76 2c 20 4f 45 5f 41 62 6f 72 74 29  eP5(v, OE_Abort)
2fc6c 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2fc6d 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29  MayAbort(pParse)
2fc6e 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 0d 0a 23 65  ;..    }else..#e
2fc6f 6e 64 69 66 0d 0a 20 20 20 20 7b 0d 0a 20 20 20  ndif..    {..   
2fc70 20 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20 28     int count = (
2fc71 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
2fc72 30 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 74  0);    /* True t
2fc73 6f 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20  o count changes 
2fc74 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  */..      sqlite
2fc75 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
2fc76 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
2fc77 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20 63   iCur, iRowid, c
2fc78 6f 75 6e 74 2c 20 70 54 72 69 67 67 65 72 2c 20  ount, pTrigger, 
2fc79 4f 45 5f 44 65 66 61 75 6c 74 29 3b 0d 0a 20 20  OE_Default);..  
2fc7a 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 45 6e    }....    /* En
2fc7b 64 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20  d of the delete 
2fc7c 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  loop */..    sql
2fc7d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fc7e 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
2fc7f 64 72 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  dr);..    sqlite
2fc80 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2fc81 6c 28 76 2c 20 65 6e 64 29 3b 0d 0a 0d 0a 20 20  l(v, end);....  
2fc82 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20 63    /* Close the c
2fc83 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
2fc84 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 74 73  he table and its
2fc85 20 69 6e 64 65 78 65 73 2e 20 2a 2f 0d 0a 20 20   indexes. */..  
2fc86 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26    if( !isView &&
2fc87 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
2fc88 29 20 29 7b 0d 0a 20 20 20 20 20 20 66 6f 72 28  ) ){..      for(
2fc89 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
2fc8a 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b  pIndex; pIdx; i+
2fc8b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  +, pIdx=pIdx->pN
2fc8c 65 78 74 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  ext){..        s
2fc8d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fc8e 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
2fc8f 75 72 20 2b 20 69 2c 20 70 49 64 78 2d 3e 74 6e  ur + i, pIdx->tn
2fc90 75 6d 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  um);..      }.. 
2fc91 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2fc92 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
2fc93 73 65 2c 20 69 43 75 72 29 3b 0d 0a 20 20 20 20  se, iCur);..    
2fc94 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 55  }..  }....  /* U
2fc95 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
2fc96 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
2fc97 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
2fc98 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0d 0a 20  ontent of the.. 
2fc99 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69   ** maximum rowi
2fc9a 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73  d counter values
2fc9b 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20   recorded while 
2fc9c 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0d 0a  inserting into..
2fc9d 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65    ** autoincreme
2fc9e 6e 74 20 74 61 62 6c 65 73 2e 0d 0a 20 20 2a 2f  nt tables...  */
2fc9f 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
2fca0 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61  nested==0 && pPa
2fca1 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
2fca2 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ==0 ){..    sqli
2fca3 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
2fca4 45 6e 64 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  End(pParse);..  
2fca5 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 74 75 72 6e  }....  /* Return
2fca6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
2fca7 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 64 65  ows that were de
2fca8 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 72  leted. If this r
2fca9 6f 75 74 69 6e 65 20 69 73 20 0d 0a 20 20 2a 2a  outine is ..  **
2fcaa 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
2fcab 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61   because of a ca
2fcac 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73  ll to sqlite3Nes
2fcad 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e  tedParse(), do n
2fcae 6f 74 0d 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20  ot..  ** invoke 
2fcaf 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
2fcb0 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ction...  */..  
2fcb1 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
2fcb2 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
2fcb3 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73   && !pParse->nes
2fcb4 74 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ted && !pParse->
2fcb5 70 54 72 69 67 67 65 72 54 61 62 20 29 7b 0d 0a  pTriggerTab ){..
2fcb6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fcb7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
2fcb8 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e 74 2c 20 31  ltRow, memCnt, 1
2fcb9 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2fcba 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
2fcbb 20 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65   1);..    sqlite
2fcbc 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2fcbd 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
2fcbe 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65 74 65  ME, "rows delete
2fcbf 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
2fcc0 43 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 64 65 6c 65  C);..  }....dele
2fcc1 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a  te_from_cleanup:
2fcc2 0d 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  ..  sqlite3AuthC
2fcc3 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74  ontextPop(&sCont
2fcc4 65 78 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ext);..  sqlite3
2fcc5 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2fcc6 2c 20 70 54 61 62 4c 69 73 74 29 3b 0d 0a 20 20  , pTabList);..  
2fcc7 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2fcc8 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0d 0a  e(db, pWhere);..
2fcc9 20 20 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 2f 2a    return;..}../*
2fcca 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69   Make sure "isVi
2fccb 65 77 22 20 61 6e 64 20 6f 74 68 65 72 20 6d 61  ew" and other ma
2fccc 63 72 6f 73 20 64 65 66 69 6e 65 64 20 61 62 6f  cros defined abo
2fccd 76 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ve are undefined
2fcce 2e 20 4f 74 68 65 72 77 69 73 65 0d 0a 2a 2a 20  . Otherwise..** 
2fccf 74 68 65 6c 79 20 6d 61 79 20 69 6e 74 65 72 66  thely may interf
2fcd0 65 72 65 20 77 69 74 68 20 63 6f 6d 70 69 6c 61  ere with compila
2fcd1 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66 75  tion of other fu
2fcd2 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20  nctions in this 
2fcd3 66 69 6c 65 0d 0a 2a 2a 20 28 6f 72 20 69 6e 20  file..** (or in 
2fcd4 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 66  another file, if
2fcd5 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d   this file becom
2fcd6 65 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 61  es part of the a
2fcd7 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a  malgamation).  *
2fcd8 2f 0d 0a 23 69 66 64 65 66 20 69 73 56 69 65 77  /..#ifdef isView
2fcd9 0d 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65 77  .. #undef isView
2fcda 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66  ..#endif..#ifdef
2fcdb 20 70 54 72 69 67 67 65 72 0d 0a 20 23 75 6e 64   pTrigger.. #und
2fcdc 65 66 20 70 54 72 69 67 67 65 72 0d 0a 23 65 6e  ef pTrigger..#en
2fcdd 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  dif..../*..** Th
2fcde 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
2fcdf 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 74  ates VDBE code t
2fce0 68 61 74 20 63 61 75 73 65 73 20 61 20 73 69 6e  hat causes a sin
2fce1 67 6c 65 20 72 6f 77 20 6f 66 20 61 0d 0a 2a 2a  gle row of a..**
2fce2 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f   single table to
2fce3 20 62 65 20 64 65 6c 65 74 65 64 2e 0d 0a 2a 2a   be deleted...**
2fce4 0d 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75  ..** The VDBE mu
2fce5 73 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69  st be in a parti
2fce6 63 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e  cular state when
2fce7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2fce8 20 63 61 6c 6c 65 64 2e 0d 0a 2a 2a 20 54 68 65   called...** The
2fce9 73 65 20 61 72 65 20 74 68 65 20 72 65 71 75 69  se are the requi
2fcea 72 65 6d 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  rements:..**..**
2fceb 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77 72     1.  A read/wr
2fcec 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  ite cursor point
2fced 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68 65  ing to pTab, the
2fcee 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
2fcef 67 20 74 68 65 20 72 6f 77 0d 0a 2a 2a 20 20 20  g the row..**   
2fcf0 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65      to be delete
2fcf1 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  d, must be opene
2fcf2 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  d as cursor numb
2fcf3 65 72 20 24 69 43 75 72 2e 0d 0a 2a 2a 0d 0a 2a  er $iCur...**..*
2fcf4 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69  *   2.  Read/wri
2fcf5 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  te cursors for a
2fcf6 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
2fcf7 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  ab must be open 
2fcf8 61 73 0d 0a 2a 2a 20 20 20 20 20 20 20 63 75 72  as..**       cur
2fcf9 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b  sor number base+
2fcfa 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69  i for the i-th i
2fcfb 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ndex...**..**   
2fcfc 33 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e  3.  The record n
2fcfd 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 77  umber of the row
2fcfe 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 6d   to be deleted m
2fcff 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e  ust be stored in
2fd00 0d 0a 2a 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72  ..**       memor
2fd01 79 20 63 65 6c 6c 20 69 52 6f 77 69 64 2e 0d 0a  y cell iRowid...
2fd02 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
2fd03 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
2fd04 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74  de to remove bot
2fd05 68 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f  h the table reco
2fd06 72 64 20 61 6e 64 20 61 6c 6c 20 0d 0a 2a 2a 20  rd and all ..** 
2fd07 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68  index entries th
2fd08 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  at point to that
2fd09 20 72 65 63 6f 72 64 2e 0d 0a 2a 2f 0d 0a 53 51   record...*/..SQ
2fd0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2fd0b 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
2fd0c 65 52 6f 77 44 65 6c 65 74 65 28 0d 0a 20 20 50  eRowDelete(..  P
2fd0d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2fd0e 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2fd0f 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  text */..  Table
2fd10 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a   *pTab,       /*
2fd11 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   Table containin
2fd12 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  g the row to be 
2fd13 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 69 6e  deleted */..  in
2fd14 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20  t iCur,         
2fd15 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2fd16 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
2fd17 2a 2f 0d 0a 20 20 69 6e 74 20 69 52 6f 77 69 64  */..  int iRowid
2fd18 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f  ,        /* Memo
2fd19 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e  ry cell that con
2fd1a 74 61 69 6e 73 20 74 68 65 20 72 6f 77 69 64 20  tains the rowid 
2fd1b 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0d 0a 20 20  to delete */..  
2fd1c 69 6e 74 20 63 6f 75 6e 74 2c 20 20 20 20 20 20  int count,      
2fd1d 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72     /* If non-zer
2fd1e 6f 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  o, increment the
2fd1f 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
2fd20 74 65 72 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65  ter */..  Trigge
2fd21 72 20 2a 70 54 72 69 67 67 65 72 2c 20 2f 2a 20  r *pTrigger, /* 
2fd22 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
2fd23 20 74 6f 20 28 70 6f 74 65 6e 74 69 61 6c 6c 79   to (potentially
2fd24 29 20 66 69 72 65 20 2a 2f 0d 0a 20 20 69 6e 74  ) fire */..  int
2fd25 20 6f 6e 63 6f 6e 66 20 20 20 20 20 20 20 20 20   onconf         
2fd26 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f  /* Default ON CO
2fd27 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 6f  NFLICT policy fo
2fd28 72 20 74 72 69 67 67 65 72 73 20 2a 2f 0d 0a 29  r triggers */..)
2fd29 7b 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  {..  Vdbe *v = p
2fd2a 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20  Parse->pVdbe;   
2fd2b 20 20 20 20 20 2f 2a 20 56 64 62 65 20 2a 2f 0d       /* Vdbe */.
2fd2c 0a 20 20 69 6e 74 20 69 4f 6c 64 20 3d 20 30 3b  .  int iOld = 0;
2fd2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd2e 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69     /* First regi
2fd2f 73 74 65 72 20 69 6e 20 4f 4c 44 2e 2a 20 61 72  ster in OLD.* ar
2fd30 72 61 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c  ray */..  int iL
2fd31 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  abel;           
2fd32 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62            /* Lab
2fd33 65 6c 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 65  el resolved to e
2fd34 6e 64 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20  nd of generated 
2fd35 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  code */....  /* 
2fd36 56 64 62 65 20 69 73 20 67 75 61 72 61 6e 74 65  Vdbe is guarante
2fd37 65 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20  ed to have been 
2fd38 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69  allocated by thi
2fd39 73 20 73 74 61 67 65 2e 20 2a 2f 0d 0a 20 20 61  s stage. */..  a
2fd3a 73 73 65 72 74 28 20 76 20 29 3b 0d 0a 0d 0a 20  ssert( v );.... 
2fd3b 20 2f 2a 20 53 65 65 6b 20 63 75 72 73 6f 72 20   /* Seek cursor 
2fd3c 69 43 75 72 20 74 6f 20 74 68 65 20 72 6f 77 20  iCur to the row 
2fd3d 74 6f 20 64 65 6c 65 74 65 2e 20 49 66 20 74 68  to delete. If th
2fd3e 69 73 20 72 6f 77 20 6e 6f 20 6c 6f 6e 67 65 72  is row no longer
2fd3f 20 65 78 69 73 74 73 20 0d 0a 20 20 2a 2a 20 28   exists ..  ** (
2fd40 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20  this can happen 
2fd41 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f  if a trigger pro
2fd42 67 72 61 6d 20 68 61 73 20 61 6c 72 65 61 64 79  gram has already
2fd43 20 64 65 6c 65 74 65 64 20 69 74 29 2c 20 64 6f   deleted it), do
2fd44 0d 0a 20 20 2a 2a 20 6e 6f 74 20 61 74 74 65 6d  ..  ** not attem
2fd45 70 74 20 74 6f 20 64 65 6c 65 74 65 20 69 74 20  pt to delete it 
2fd46 6f 72 20 66 69 72 65 20 61 6e 79 20 44 45 4c 45  or fire any DELE
2fd47 54 45 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f  TE triggers.  */
2fd48 0d 0a 20 20 69 4c 61 62 65 6c 20 3d 20 73 71 6c  ..  iLabel = sql
2fd49 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2fd4a 6c 28 76 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  l(v);..  sqlite3
2fd4b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2fd4c 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72  _NotExists, iCur
2fd4d 2c 20 69 4c 61 62 65 6c 2c 20 69 52 6f 77 69 64  , iLabel, iRowid
2fd4e 29 3b 0d 0a 20 0d 0a 20 20 2f 2a 20 49 66 20 74  );.. ..  /* If t
2fd4f 68 65 72 65 20 61 72 65 20 61 6e 79 20 74 72 69  here are any tri
2fd50 67 67 65 72 73 20 74 6f 20 66 69 72 65 2c 20 61  ggers to fire, a
2fd51 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65 20  llocate a range 
2fd52 6f 66 20 72 65 67 69 73 74 65 72 73 20 74 6f 0d  of registers to.
2fd53 0a 20 20 2a 2a 20 75 73 65 20 66 6f 72 20 74 68  .  ** use for th
2fd54 65 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63  e old.* referenc
2fd55 65 73 20 69 6e 20 74 68 65 20 74 72 69 67 67 65  es in the trigge
2fd56 72 73 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73  rs.  */..  if( s
2fd57 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64  qlite3FkRequired
2fd58 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
2fd59 2c 20 30 29 20 7c 7c 20 70 54 72 69 67 67 65 72  , 0) || pTrigger
2fd5a 20 29 7b 0d 0a 20 20 20 20 75 33 32 20 6d 61 73   ){..    u32 mas
2fd5b 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
2fd5c 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2fd5d 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20  f OLD.* columns 
2fd5e 69 6e 20 75 73 65 20 2a 2f 0d 0a 20 20 20 20 69  in use */..    i
2fd5f 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
2fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fd61 49 74 65 72 61 74 6f 72 20 75 73 65 64 20 77 68  Iterator used wh
2fd62 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 4f  ile populating O
2fd63 4c 44 2e 2a 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f  LD.* */....    /
2fd64 2a 20 54 4f 44 4f 3a 20 43 6f 75 6c 64 20 75 73  * TODO: Could us
2fd65 65 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  e temporary regi
2fd66 73 74 65 72 73 20 68 65 72 65 2e 20 41 6c 73 6f  sters here. Also
2fd67 20 63 6f 75 6c 64 20 61 74 74 65 6d 70 74 20 74   could attempt t
2fd68 6f 0d 0a 20 20 20 20 2a 2a 20 61 76 6f 69 64 20  o..    ** avoid 
2fd69 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74  copying the cont
2fd6a 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 77 69  ents of the rowi
2fd6b 64 20 72 65 67 69 73 74 65 72 2e 20 20 2a 2f 0d  d register.  */.
2fd6c 0a 20 20 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69  .    mask = sqli
2fd6d 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73  te3TriggerColmas
2fd6e 6b 28 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72  k(..        pPar
2fd6f 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 30 2c  se, pTrigger, 0,
2fd70 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
2fd71 52 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45 52  RE|TRIGGER_AFTER
2fd72 2c 20 70 54 61 62 2c 20 6f 6e 63 6f 6e 66 0d 0a  , pTab, onconf..
2fd73 20 20 20 20 29 3b 0d 0a 20 20 20 20 6d 61 73 6b      );..    mask
2fd74 20 7c 3d 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64   |= sqlite3FkOld
2fd75 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54 61  mask(pParse, pTa
2fd76 62 29 3b 0d 0a 20 20 20 20 69 4f 6c 64 20 3d 20  b);..    iOld = 
2fd77 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0d  pParse->nMem+1;.
2fd78 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
2fd79 6d 20 2b 3d 20 28 31 20 2b 20 70 54 61 62 2d 3e  m += (1 + pTab->
2fd7a 6e 43 6f 6c 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  nCol);....    /*
2fd7b 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 4f 4c   Populate the OL
2fd7c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  D.* pseudo-table
2fd7d 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79 2e   register array.
2fd7e 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 77 69   These values wi
2fd7f 6c 6c 20 62 65 20 0d 0a 20 20 20 20 2a 2a 20 75  ll be ..    ** u
2fd80 73 65 64 20 62 79 20 61 6e 79 20 42 45 46 4f 52  sed by any BEFOR
2fd81 45 20 61 6e 64 20 41 46 54 45 52 20 74 72 69 67  E and AFTER trig
2fd82 67 65 72 73 20 74 68 61 74 20 65 78 69 73 74 2e  gers that exist.
2fd83 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65    */..    sqlite
2fd84 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2fd85 50 5f 43 6f 70 79 2c 20 69 52 6f 77 69 64 2c 20  P_Copy, iRowid, 
2fd86 69 4f 6c 64 29 3b 0d 0a 20 20 20 20 66 6f 72 28  iOld);..    for(
2fd87 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61  iCol=0; iCol<pTa
2fd88 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
2fd89 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 6d 61 73  {..      if( mas
2fd8a 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 20 7c 7c  k==0xffffffff ||
2fd8b 20 6d 61 73 6b 26 28 31 3c 3c 69 43 6f 6c 29 20   mask&(1<<iCol) 
2fd8c 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
2fd8d 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
2fd8e 6c 75 6d 6e 4f 66 54 61 62 6c 65 28 76 2c 20 70  lumnOfTable(v, p
2fd8f 54 61 62 2c 20 69 43 75 72 2c 20 69 43 6f 6c 2c  Tab, iCur, iCol,
2fd90 20 69 4f 6c 64 2b 69 43 6f 6c 2b 31 29 3b 0d 0a   iOld+iCol+1);..
2fd91 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
2fd92 0d 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ..    /* Invoke 
2fd93 42 45 46 4f 52 45 20 44 45 4c 45 54 45 20 74 72  BEFORE DELETE tr
2fd94 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20  igger programs. 
2fd95 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 43  */..    sqlite3C
2fd96 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
2fd97 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2fd98 0d 0a 20 20 20 20 20 20 20 20 54 4b 5f 44 45 4c  ..        TK_DEL
2fd99 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ETE, 0, TRIGGER_
2fd9a 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 69 4f  BEFORE, pTab, iO
2fd9b 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62  ld, onconf, iLab
2fd9c 65 6c 0d 0a 20 20 20 20 29 3b 0d 0a 0d 0a 20 20  el..    );....  
2fd9d 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 63 75    /* Seek the cu
2fd9e 72 73 6f 72 20 74 6f 20 74 68 65 20 72 6f 77 20  rsor to the row 
2fd9f 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 61 67  to be deleted ag
2fda0 61 69 6e 2e 20 49 74 20 6d 61 79 20 62 65 20 74  ain. It may be t
2fda1 68 61 74 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20  hat..    ** the 
2fda2 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20  BEFORE triggers 
2fda3 63 6f 64 65 64 20 61 62 6f 76 65 20 68 61 76 65  coded above have
2fda4 20 61 6c 72 65 61 64 79 20 72 65 6d 6f 76 65 64   already removed
2fda5 20 74 68 65 20 72 6f 77 0d 0a 20 20 20 20 2a 2a   the row..    **
2fda6 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20   being deleted. 
2fda7 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  Do not attempt t
2fda8 6f 20 64 65 6c 65 74 65 20 74 68 65 20 72 6f 77  o delete the row
2fda9 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 2c 20   a second time, 
2fdaa 61 6e 64 20 0d 0a 20 20 20 20 2a 2a 20 64 6f 20  and ..    ** do 
2fdab 6e 6f 74 20 66 69 72 65 20 41 46 54 45 52 20 74  not fire AFTER t
2fdac 72 69 67 67 65 72 73 2e 20 20 2a 2f 0d 0a 20 20  riggers.  */..  
2fdad 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fdae 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2fdaf 73 74 73 2c 20 69 43 75 72 2c 20 69 4c 61 62 65  sts, iCur, iLabe
2fdb0 6c 2c 20 69 52 6f 77 69 64 29 3b 0d 0a 0d 0a 20  l, iRowid);.... 
2fdb1 20 20 20 2f 2a 20 44 6f 20 46 4b 20 70 72 6f 63     /* Do FK proc
2fdb2 65 73 73 69 6e 67 2e 20 54 68 69 73 20 63 61 6c  essing. This cal
2fdb3 6c 20 63 68 65 63 6b 73 20 74 68 61 74 20 61 6e  l checks that an
2fdb4 79 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  y FK constraints
2fdb5 20 74 68 61 74 0d 0a 20 20 20 20 2a 2a 20 72 65   that..    ** re
2fdb6 66 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c  fer to this tabl
2fdb7 65 20 28 69 2e 65 2e 20 63 6f 6e 73 74 72 61 69  e (i.e. constrai
2fdb8 6e 74 73 20 61 74 74 61 63 68 65 64 20 74 6f 20  nts attached to 
2fdb9 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20 0d 0a  other tables) ..
2fdba 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 76      ** are not v
2fdbb 69 6f 6c 61 74 65 64 20 62 79 20 64 65 6c 65 74  iolated by delet
2fdbc 69 6e 67 20 74 68 69 73 20 72 6f 77 2e 20 20 2a  ing this row.  *
2fdbd 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b  /..    sqlite3Fk
2fdbe 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54  Check(pParse, pT
2fdbf 61 62 2c 20 69 4f 6c 64 2c 20 30 29 3b 0d 0a 20  ab, iOld, 0);.. 
2fdc0 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 44 65 6c 65 74   }....  /* Delet
2fdc1 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20  e the index and 
2fdc2 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 53  table entries. S
2fdc3 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 69 66  kip this step if
2fdc4 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 0d   pTab is really.
2fdc5 0a 20 20 2a 2a 20 61 20 76 69 65 77 20 28 69 6e  .  ** a view (in
2fdc6 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
2fdc7 6f 6e 6c 79 20 65 66 66 65 63 74 20 6f 66 20 74  only effect of t
2fdc8 68 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  he DELETE statem
2fdc9 65 6e 74 20 69 73 20 74 6f 0d 0a 20 20 2a 2a 20  ent is to..  ** 
2fdca 66 69 72 65 20 74 68 65 20 49 4e 53 54 45 41 44  fire the INSTEAD
2fdcb 20 4f 46 20 74 72 69 67 67 65 72 73 29 2e 20 20   OF triggers).  
2fdcc 2a 2f 20 0d 0a 20 20 69 66 28 20 70 54 61 62 2d  */ ..  if( pTab-
2fdcd 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0d 0a  >pSelect==0 ){..
2fdce 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
2fdcf 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
2fdd0 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2fdd1 69 43 75 72 2c 20 30 29 3b 0d 0a 20 20 20 20 73  iCur, 0);..    s
2fdd2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fdd3 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69  (v, OP_Delete, i
2fdd4 43 75 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c  Cur, (count?OPFL
2fdd5 41 47 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0d  AG_NCHANGE:0));.
2fdd6 0a 20 20 20 20 69 66 28 20 63 6f 75 6e 74 20 29  .    if( count )
2fdd7 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2fdd8 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2fdd9 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
2fdda 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d   P4_TRANSIENT);.
2fddb 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
2fddc 20 2f 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41   /* Do any ON CA
2fddd 53 43 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20  SCADE, SET NULL 
2fdde 6f 72 20 53 45 54 20 44 45 46 41 55 4c 54 20 6f  or SET DEFAULT o
2fddf 70 65 72 61 74 69 6f 6e 73 20 72 65 71 75 69 72  perations requir
2fde0 65 64 20 74 6f 0d 0a 20 20 2a 2a 20 68 61 6e 64  ed to..  ** hand
2fde1 6c 65 20 72 6f 77 73 20 28 70 6f 73 73 69 62 6c  le rows (possibl
2fde2 79 20 69 6e 20 6f 74 68 65 72 20 74 61 62 6c 65  y in other table
2fde3 73 29 20 74 68 61 74 20 72 65 66 65 72 20 76 69  s) that refer vi
2fde4 61 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 0d  a a foreign key.
2fde5 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77  .  ** to the row
2fde6 20 6a 75 73 74 20 64 65 6c 65 74 65 64 2e 20 2a   just deleted. *
2fde7 2f 20 0d 0a 20 20 73 71 6c 69 74 65 33 46 6b 41  / ..  sqlite3FkA
2fde8 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 70  ctions(pParse, p
2fde9 54 61 62 2c 20 30 2c 20 69 4f 6c 64 29 3b 0d 0a  Tab, 0, iOld);..
2fdea 0d 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 41 46  ..  /* Invoke AF
2fdeb 54 45 52 20 44 45 4c 45 54 45 20 74 72 69 67 67  TER DELETE trigg
2fdec 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0d  er programs. */.
2fded 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  .  sqlite3CodeRo
2fdee 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
2fdef 20 70 54 72 69 67 67 65 72 2c 20 0d 0a 20 20 20   pTrigger, ..   
2fdf0 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c     TK_DELETE, 0,
2fdf1 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20   TRIGGER_AFTER, 
2fdf2 70 54 61 62 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f  pTab, iOld, onco
2fdf3 6e 66 2c 20 69 4c 61 62 65 6c 0d 0a 20 20 29 3b  nf, iLabel..  );
2fdf4 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65  ....  /* Jump he
2fdf5 72 65 20 69 66 20 74 68 65 20 72 6f 77 20 68 61  re if the row ha
2fdf6 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64  d already been d
2fdf7 65 6c 65 74 65 64 20 62 65 66 6f 72 65 20 61 6e  eleted before an
2fdf8 79 20 42 45 46 4f 52 45 0d 0a 20 20 2a 2a 20 74  y BEFORE..  ** t
2fdf9 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20  rigger programs 
2fdfa 77 65 72 65 20 69 6e 76 6f 6b 65 64 2e 20 4f 72  were invoked. Or
2fdfb 20 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72   if a trigger pr
2fdfc 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 20 0d  ogram throws a .
2fdfd 0a 20 20 2a 2a 20 52 41 49 53 45 28 49 47 4e 4f  .  ** RAISE(IGNO
2fdfe 52 45 29 20 65 78 63 65 70 74 69 6f 6e 2e 20 20  RE) exception.  
2fdff 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  */..  sqlite3Vdb
2fe00 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2fe01 20 69 4c 61 62 65 6c 29 3b 0d 0a 7d 0d 0a 0d 0a   iLabel);..}....
2fe02 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
2fe03 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44  ine generates VD
2fe04 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75  BE code that cau
2fe05 73 65 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e  ses the deletion
2fe06 20 6f 66 20 61 6c 6c 0d 0a 2a 2a 20 69 6e 64 65   of all..** inde
2fe07 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69  x entries associ
2fe08 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67  ated with a sing
2fe09 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67  le row of a sing
2fe0a 6c 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a  le table...**..*
2fe0b 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20  * The VDBE must 
2fe0c 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c  be in a particul
2fe0d 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  ar state when th
2fe0e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2fe0f 6c 6c 65 64 2e 0d 0a 2a 2a 20 54 68 65 73 65 20  lled...** These 
2fe10 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d  are the requirem
2fe11 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ents:..**..**   
2fe12 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65  1.  A read/write
2fe13 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
2fe14 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61   to pTab, the ta
2fe15 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
2fe16 68 65 20 72 6f 77 0d 0a 2a 2a 20 20 20 20 20 20  he row..**      
2fe17 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20   to be deleted, 
2fe18 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61  must be opened a
2fe19 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  s cursor number 
2fe1a 22 69 43 75 72 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  "iCur"...**..** 
2fe1b 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74 65    2.  Read/write
2fe1c 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c   cursors for all
2fe1d 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
2fe1e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 73   must be open as
2fe1f 0d 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f  ..**       curso
2fe20 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 20  r number iCur+i 
2fe21 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64  for the i-th ind
2fe22 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 2e  ex...**..**   3.
2fe23 20 20 54 68 65 20 22 69 43 75 72 22 20 63 75 72    The "iCur" cur
2fe24 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e  sor must be poin
2fe25 74 69 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20  ting to the row 
2fe26 74 68 61 74 20 69 73 20 74 6f 20 62 65 0d 0a 2a  that is to be..*
2fe27 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65 64 2e  *       deleted.
2fe28 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
2fe29 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2fe2a 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
2fe2b 78 44 65 6c 65 74 65 28 0d 0a 20 20 50 61 72 73  xDelete(..  Pars
2fe2c 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
2fe2d 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
2fe2e 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
2fe2f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c  ntext */..  Tabl
2fe30 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f  e *pTab,       /
2fe31 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  * Table containi
2fe32 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65  ng the row to be
2fe33 20 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 69   deleted */..  i
2fe34 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
2fe35 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
2fe36 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
2fe37 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 52 65 67   */..  int *aReg
2fe38 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c  Idx       /* Onl
2fe39 79 20 64 65 6c 65 74 65 20 69 66 20 61 52 65 67  y delete if aReg
2fe3a 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64  Idx!=0 && aRegId
2fe3b 78 5b 69 5d 3e 30 20 2a 2f 0d 0a 29 7b 0d 0a 20  x[i]>0 */..){.. 
2fe3c 20 69 6e 74 20 69 3b 0d 0a 20 20 49 6e 64 65 78   int i;..  Index
2fe3d 20 2a 70 49 64 78 3b 0d 0a 20 20 69 6e 74 20 72   *pIdx;..  int r
2fe3e 31 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d 31 2c  1;....  for(i=1,
2fe3f 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2fe40 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70  ex; pIdx; i++, p
2fe41 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2fe42 7b 0d 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  {..    if( aRegI
2fe43 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64 78  dx!=0 && aRegIdx
2fe44 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  [i-1]==0 ) conti
2fe45 6e 75 65 3b 0d 0a 20 20 20 20 72 31 20 3d 20 73  nue;..    r1 = s
2fe46 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
2fe47 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70  dexKey(pParse, p
2fe48 49 64 78 2c 20 69 43 75 72 2c 20 30 2c 20 30 29  Idx, iCur, 0, 0)
2fe49 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2fe4a 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d  beAddOp3(pParse-
2fe4b 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64 78 44 65  >pVdbe, OP_IdxDe
2fe4c 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 72 31  lete, iCur+i, r1
2fe4d 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  ,pIdx->nColumn+1
2fe4e 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  );..  }..}..../*
2fe4f 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  ..** Generate co
2fe50 64 65 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73  de that will ass
2fe51 65 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b  emble an index k
2fe52 65 79 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e  ey and put it in
2fe53 20 72 65 67 69 73 74 65 72 0d 0a 2a 2a 20 72 65   register..** re
2fe54 67 4f 75 74 2e 20 20 54 68 65 20 6b 65 79 20 77  gOut.  The key w
2fe55 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64 65 78  ith be for index
2fe56 20 70 49 64 78 20 77 68 69 63 68 20 69 73 20 61   pIdx which is a
2fe57 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e  n index on pTab.
2fe58 0d 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68 65  ..** iCur is the
2fe59 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72 73   index of a curs
2fe5a 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70  or open on the p
2fe5b 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70 6f  Tab table and po
2fe5c 69 6e 74 69 6e 67 20 74 6f 0d 0a 2a 2a 20 74 68  inting to..** th
2fe5d 65 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65  e entry that nee
2fe5e 64 73 20 69 6e 64 65 78 69 6e 67 2e 0d 0a 2a 2a  ds indexing...**
2fe5f 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 72 65  ..** Return a re
2fe60 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68  gister number wh
2fe61 69 63 68 20 69 73 20 74 68 65 20 66 69 72 73 74  ich is the first
2fe62 20 69 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 0d 0a   in a block of..
2fe63 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 68 61  ** registers tha
2fe64 74 20 68 6f 6c 64 73 20 74 68 65 20 65 6c 65 6d  t holds the elem
2fe65 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 64 65  ents of the inde
2fe66 78 20 6b 65 79 2e 20 20 54 68 65 0d 0a 2a 2a 20  x key.  The..** 
2fe67 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73 74 65  block of registe
2fe68 72 73 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  rs has already b
2fe69 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20  een deallocated 
2fe6a 62 79 20 74 68 65 20 74 69 6d 65 0d 0a 2a 2a 20  by the time..** 
2fe6b 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
2fe6c 75 72 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  urns...*/..SQLIT
2fe6d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2fe6e 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64  lite3GenerateInd
2fe6f 65 78 4b 65 79 28 0d 0a 20 20 50 61 72 73 65 20  exKey(..  Parse 
2fe70 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
2fe71 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2fe72 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64  */..  Index *pId
2fe73 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  x,       /* The 
2fe74 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63 68 20  index for which 
2fe75 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 6b 65  to generate a ke
2fe76 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 75 72  y */..  int iCur
2fe77 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ,          /* Cu
2fe78 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
2fe79 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  the pIdx->pTable
2fe7a 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74   table */..  int
2fe7b 20 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20   regOut,        
2fe7c 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
2fe7d 20 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68   index key to th
2fe7e 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0d 0a  is register */..
2fe7f 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20    int doMakeRec 
2fe80 20 20 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20       /* Run the 
2fe81 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e  OP_MakeRecord in
2fe82 73 74 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75  struction if tru
2fe83 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62 65  e */..){..  Vdbe
2fe84 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2fe85 64 62 65 3b 0d 0a 20 20 69 6e 74 20 6a 3b 0d 0a  dbe;..  int j;..
2fe86 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2fe87 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0d 0a 20  pIdx->pTable;.. 
2fe88 20 69 6e 74 20 72 65 67 42 61 73 65 3b 0d 0a 20   int regBase;.. 
2fe89 20 69 6e 74 20 6e 43 6f 6c 3b 0d 0a 0d 0a 20 20   int nCol;....  
2fe8a 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f  nCol = pIdx->nCo
2fe8b 6c 75 6d 6e 3b 0d 0a 20 20 72 65 67 42 61 73 65  lumn;..  regBase
2fe8c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2fe8d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e  pRange(pParse, n
2fe8e 43 6f 6c 2b 31 29 3b 0d 0a 20 20 73 71 6c 69 74  Col+1);..  sqlit
2fe8f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2fe90 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20  OP_Rowid, iCur, 
2fe91 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0d 0a  regBase+nCol);..
2fe92 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f    for(j=0; j<nCo
2fe93 6c 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e  l; j++){..    in
2fe94 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69  t idx = pIdx->ai
2fe95 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0d 0a 20 20 20 20  Column[j];..    
2fe96 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69  if( idx==pTab->i
2fe97 50 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20 20 73  PKey ){..      s
2fe98 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fe99 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
2fe9a 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42  gBase+nCol, regB
2fe9b 61 73 65 2b 6a 29 3b 0d 0a 20 20 20 20 7d 65 6c  ase+j);..    }el
2fe9c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
2fe9d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2fe9e 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c  OP_Column, iCur,
2fe9f 20 69 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29   idx, regBase+j)
2fea0 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
2fea1 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c  ColumnDefault(v,
2fea2 20 70 54 61 62 2c 20 69 64 78 2c 20 2d 31 29 3b   pTab, idx, -1);
2fea3 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
2fea4 69 66 28 20 64 6f 4d 61 6b 65 52 65 63 20 29 7b  if( doMakeRec ){
2fea5 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ..    const char
2fea6 20 2a 7a 41 66 66 3b 0d 0a 20 20 20 20 69 66 28   *zAff;..    if(
2fea7 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c   pTab->pSelect |
2fea8 7c 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  | (pParse->db->f
2fea9 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 64  lags & SQLITE_Id
2feaa 78 52 65 61 6c 41 73 49 6e 74 29 21 3d 30 20 29  xRealAsInt)!=0 )
2feab 7b 0d 0a 20 20 20 20 20 20 7a 41 66 66 20 3d 20  {..      zAff = 
2feac 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  0;..    }else{..
2fead 20 20 20 20 20 20 7a 41 66 66 20 3d 20 73 71 6c        zAff = sql
2feae 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
2feaf 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0d 0a  yStr(v, pIdx);..
2feb0 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
2feb1 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2feb2 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
2feb3 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20  egBase, nCol+1, 
2feb4 72 65 67 4f 75 74 29 3b 0d 0a 20 20 20 20 73 71  regOut);..    sq
2feb5 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2feb6 34 28 76 2c 20 2d 31 2c 20 7a 41 66 66 2c 20 50  4(v, -1, zAff, P
2feb7 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20  4_TRANSIENT);.. 
2feb8 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c   }..  sqlite3Rel
2feb9 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
2feba 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
2febb 43 6f 6c 2b 31 29 3b 0d 0a 20 20 72 65 74 75 72  Col+1);..  retur
2febc 6e 20 72 65 67 42 61 73 65 3b 0d 0a 7d 0d 0a 0d  n regBase;..}...
2febd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2febe 20 45 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e 63   End of delete.c
2febf 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2fec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2fec2 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2fec3 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 75 6e  * Begin file fun
2fec4 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c.c ************
2fec5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fec7 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 32 20 46  /../*..** 2002 F
2fec8 65 62 72 75 61 72 79 20 32 33 0d 0a 2a 2a 0d 0a  ebruary 23..**..
2fec9 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2feca 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2fecb 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2fecc 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2fecd 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
2fece 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2fecf 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
2fed0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2fed1 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2fed2 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
2fed3 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2fed4 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2fed5 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2fed6 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
2fed7 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2fed8 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2fed9 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2feda 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
2fedb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fedc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fedd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fede 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fedf 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
2fee0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
2fee1 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
2fee2 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73  mplement various
2fee3 20 53 51 4c 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f   SQL..** functio
2fee4 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 0d  ns of SQLite.  .
2fee5 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 69 73  .**..** There is
2fee6 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74   only one export
2fee7 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69  ed symbol in thi
2fee8 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e  s file - the fun
2fee9 63 74 69 6f 6e 0d 0a 2a 2a 20 73 71 6c 69 74 65  ction..** sqlite
2feea 52 65 67 69 73 74 65 72 42 75 69 6c 64 69 6e 46  RegisterBuildinF
2feeb 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64  unctions() found
2feec 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   at the bottom o
2feed 66 20 74 68 65 20 66 69 6c 65 2e 0d 0a 2a 2a 20  f the file...** 
2feee 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68  All other code h
2feef 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0d 0a  as file scope...
2fef0 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  */../* #include 
2fef1 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0d 0a 2f  <stdlib.h> */../
2fef2 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65  * #include <asse
2fef3 72 74 2e 68 3e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  rt.h> */..../*..
2fef4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f  ** Return the co
2fef5 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
2fef6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2fef7 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f   a function...*/
2fef8 0d 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71  ..static CollSeq
2fef9 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63   *sqlite3GetFunc
2fefa 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f  CollSeq(sqlite3_
2fefb 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2fefc 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e  ){..  return con
2fefd 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0d 0a 7d 0d  text->pColl;..}.
2fefe 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d  .../*..** Implem
2feff 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2ff00 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69  non-aggregate mi
2ff01 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75  n() and max() fu
2ff02 6e 63 74 69 6f 6e 73 0d 0a 2a 2f 0d 0a 73 74 61  nctions..*/..sta
2ff03 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 46  tic void minmaxF
2ff04 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  unc(..  sqlite3_
2ff05 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2ff06 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a  ,..  int argc,..
2ff07 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2ff08 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 69 6e  **argv..){..  in
2ff09 74 20 69 3b 0d 0a 20 20 69 6e 74 20 6d 61 73 6b  t i;..  int mask
2ff0a 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69  ;    /* 0 for mi
2ff0b 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66 66 66  n() or 0xfffffff
2ff0c 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0d 0a  f for max() */..
2ff0d 20 20 69 6e 74 20 69 42 65 73 74 3b 0d 0a 20 20    int iBest;..  
2ff0e 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0d  CollSeq *pColl;.
2ff0f 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ...  assert( arg
2ff10 63 3e 31 20 29 3b 0d 0a 20 20 6d 61 73 6b 20 3d  c>1 );..  mask =
2ff11 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
2ff12 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d 30 20 3f  ta(context)==0 ?
2ff13 20 30 20 3a 20 2d 31 3b 0d 0a 20 20 70 43 6f 6c   0 : -1;..  pCol
2ff14 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
2ff15 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
2ff16 74 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  t);..  assert( p
2ff17 43 6f 6c 6c 20 29 3b 0d 0a 20 20 61 73 73 65 72  Coll );..  asser
2ff18 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d  t( mask==-1 || m
2ff19 61 73 6b 3d 3d 30 20 29 3b 0d 0a 20 20 69 42 65  ask==0 );..  iBe
2ff1a 73 74 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 73  st = 0;..  if( s
2ff1b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2ff1c 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
2ff1d 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
2ff1e 3b 0d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  ;..  for(i=1; i<
2ff1f 61 72 67 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  argc; i++){..   
2ff20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
2ff21 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29  ue_type(argv[i])
2ff22 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
2ff23 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 69 66 28  return;..    if(
2ff24 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70   (sqlite3MemComp
2ff25 61 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c  are(argv[iBest],
2ff26 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29   argv[i], pColl)
2ff27 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0d 0a 20 20  ^mask)>=0 ){..  
2ff28 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6d 61      testcase( ma
2ff29 73 6b 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  sk==0 );..      
2ff2a 69 42 65 73 74 20 3d 20 69 3b 0d 0a 20 20 20 20  iBest = i;..    
2ff2b 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  }..  }..  sqlite
2ff2c 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
2ff2d 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65  ontext, argv[iBe
2ff2e 73 74 5d 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  st]);..}..../*..
2ff2f 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 79  ** Return the ty
2ff30 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65  pe of the argume
2ff31 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  nt...*/..static 
2ff32 76 6f 69 64 20 74 79 70 65 6f 66 46 75 6e 63 28  void typeofFunc(
2ff33 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ..  sqlite3_cont
2ff34 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20  ext *context,.. 
2ff35 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20   int NotUsed,.. 
2ff36 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2ff37 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 63 6f 6e  *argv..){..  con
2ff38 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0d  st char *z = 0;.
2ff39 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2ff3a 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20  TER(NotUsed);.. 
2ff3b 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33   switch( sqlite3
2ff3c 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
2ff3d 5b 30 5d 29 20 29 7b 0d 0a 20 20 20 20 63 61 73  [0]) ){..    cas
2ff3e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
2ff3f 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b  : z = "integer";
2ff40 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2ff41 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20  e SQLITE_TEXT:  
2ff42 20 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20    z = "text";   
2ff43 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2ff44 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20  e SQLITE_FLOAT: 
2ff45 20 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20    z = "real";   
2ff46 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
2ff47 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20  e SQLITE_BLOB:  
2ff48 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20    z = "blob";   
2ff49 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 64 65 66   break;..    def
2ff4a 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20 20  ault:           
2ff4b 20 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20    z = "null";   
2ff4c 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20 20   break;..  }..  
2ff4d 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2ff4e 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20  ext(context, z, 
2ff4f 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
2ff50 43 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  C);..}....../*..
2ff51 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2ff52 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 28  n of the length(
2ff53 29 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2f 0d 0a  ) function..*/..
2ff54 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67  static void leng
2ff55 74 68 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74  thFunc(..  sqlit
2ff56 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2ff57 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63  ext,..  int argc
2ff58 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ,..  sqlite3_val
2ff59 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20  ue **argv..){.. 
2ff5a 20 69 6e 74 20 6c 65 6e 3b 0d 0a 0d 0a 20 20 61   int len;....  a
2ff5b 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
2ff5c 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
2ff5d 4d 45 54 45 52 28 61 72 67 63 29 3b 0d 0a 20 20  METER(argc);..  
2ff5e 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
2ff5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
2ff60 30 5d 29 20 29 7b 0d 0a 20 20 20 20 63 61 73 65  0]) ){..    case
2ff61 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0d 0a 20   SQLITE_BLOB:.. 
2ff62 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
2ff63 4e 54 45 47 45 52 3a 0d 0a 20 20 20 20 63 61 73  NTEGER:..    cas
2ff64 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20  e SQLITE_FLOAT: 
2ff65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2ff66 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
2ff67 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  ext, sqlite3_val
2ff68 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
2ff69 29 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  ));..      break
2ff6a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
2ff6b 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20  se SQLITE_TEXT: 
2ff6c 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  {..      const u
2ff6d 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20  nsigned char *z 
2ff6e 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2ff6f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0d 0a  text(argv[0]);..
2ff70 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29        if( z==0 )
2ff71 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20   return;..      
2ff72 6c 65 6e 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  len = 0;..      
2ff73 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0d 0a 20 20  while( *z ){..  
2ff74 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0d 0a 20 20        len++;..  
2ff75 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
2ff76 50 5f 55 54 46 38 28 7a 29 3b 0d 0a 20 20 20 20  P_UTF8(z);..    
2ff77 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74    }..      sqlit
2ff78 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
2ff79 6e 74 65 78 74 2c 20 6c 65 6e 29 3b 0d 0a 20 20  ntext, len);..  
2ff7a 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
2ff7b 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  }..    default: 
2ff7c 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
2ff7d 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e  _result_null(con
2ff7e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20 62 72  text);..      br
2ff7f 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  eak;..    }..  }
2ff80 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d  ..}..../*..** Im
2ff81 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2ff82 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 69  the abs() functi
2ff83 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 4d 50 3a  on...**..** IMP:
2ff84 20 52 2d 32 33 39 37 39 2d 32 36 38 35 35 20 54   R-23979-26855 T
2ff85 68 65 20 61 62 73 28 58 29 20 66 75 6e 63 74 69  he abs(X) functi
2ff86 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 61  on returns the a
2ff87 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66  bsolute value of
2ff88 0d 0a 2a 2a 20 74 68 65 20 6e 75 6d 65 72 69 63  ..** the numeric
2ff89 20 61 72 67 75 6d 65 6e 74 20 58 2e 20 0d 0a 2a   argument X. ..*
2ff8a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  /..static void a
2ff8b 62 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  bsFunc(sqlite3_c
2ff8c 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2ff8d 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
2ff8e 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
2ff8f 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  {..  assert( arg
2ff90 63 3d 3d 31 20 29 3b 0d 0a 20 20 55 4e 55 53 45  c==1 );..  UNUSE
2ff91 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
2ff92 29 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 73 71  );..  switch( sq
2ff93 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2ff94 28 61 72 67 76 5b 30 5d 29 20 29 7b 0d 0a 20 20  (argv[0]) ){..  
2ff95 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
2ff96 54 45 47 45 52 3a 20 7b 0d 0a 20 20 20 20 20 20  TEGER: {..      
2ff97 69 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74  i64 iVal = sqlit
2ff98 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
2ff99 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20 20 20  rgv[0]);..      
2ff9a 69 66 28 20 69 56 61 6c 3c 30 20 29 7b 0d 0a 20  if( iVal<0 ){.. 
2ff9b 20 20 20 20 20 20 20 69 66 28 20 28 69 56 61 6c         if( (iVal
2ff9c 3c 3c 31 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  <<1)==0 ){..    
2ff9d 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d        /* IMP: R-
2ff9e 33 35 34 36 30 2d 31 35 30 38 34 20 49 66 20 58  35460-15084 If X
2ff9f 20 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20   is the integer 
2ffa0 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  -922337203685477
2ffa1 35 38 30 37 20 74 68 65 6e 0d 0a 20 20 20 20 20  5807 then..     
2ffa2 20 20 20 20 20 2a 2a 20 61 62 73 28 58 29 20 74       ** abs(X) t
2ffa3 68 72 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72  hrows an integer
2ffa4 20 6f 76 65 72 66 6c 6f 77 20 65 72 72 6f 72 20   overflow error 
2ffa5 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6e  since there is n
2ffa6 6f 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  o..          ** 
2ffa7 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 73 69 74  equivalent posit
2ffa8 69 76 65 20 36 34 2d 62 69 74 20 74 77 6f 20 63  ive 64-bit two c
2ffa9 6f 6d 70 6c 65 6d 65 6e 74 20 76 61 6c 75 65 2e  omplement value.
2ffaa 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 73   */..          s
2ffab 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2ffac 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e  ror(context, "in
2ffad 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c  teger overflow",
2ffae 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 20   -1);..         
2ffaf 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20   return;..      
2ffb0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 56 61    }..        iVa
2ffb1 6c 20 3d 20 2d 69 56 61 6c 3b 0d 0a 20 20 20 20  l = -iVal;..    
2ffb2 20 20 7d 20 0d 0a 20 20 20 20 20 20 73 71 6c 69    } ..      sqli
2ffb3 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
2ffb4 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b  (context, iVal);
2ffb5 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
2ffb6 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
2ffb7 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0d 0a  SQLITE_NULL: {..
2ffb8 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d        /* IMP: R-
2ffb9 33 37 34 33 34 2d 31 39 39 32 39 20 41 62 73 28  37434-19929 Abs(
2ffba 58 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  X) returns NULL 
2ffbb 69 66 20 58 20 69 73 20 4e 55 4c 4c 2e 20 2a 2f  if X is NULL. */
2ffbc 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
2ffbd 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74  result_null(cont
2ffbe 65 78 74 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  ext);..      bre
2ffbf 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
2ffc0 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20  default: {..    
2ffc1 20 20 2f 2a 20 42 65 63 61 75 73 65 20 73 71 6c    /* Because sql
2ffc2 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
2ffc3 65 28 29 20 72 65 74 75 72 6e 73 20 30 2e 30 20  e() returns 0.0 
2ffc4 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
2ffc5 69 73 20 6e 6f 74 0d 0a 20 20 20 20 20 20 2a 2a  is not..      **
2ffc6 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20   something that 
2ffc7 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64  can be converted
2ffc8 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2c 20   into a number, 
2ffc9 77 65 20 68 61 76 65 3a 0d 0a 20 20 20 20 20 20  we have:..      
2ffca 2a 2a 20 49 4d 50 3a 20 52 2d 35 37 33 32 36 2d  ** IMP: R-57326-
2ffcb 33 31 35 34 31 20 41 62 73 28 58 29 20 72 65 74  31541 Abs(X) ret
2ffcc 75 72 6e 20 30 2e 30 20 69 66 20 58 20 69 73 20  urn 0.0 if X is 
2ffcd 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  a string or blob
2ffce 20 74 68 61 74 0d 0a 20 20 20 20 20 20 2a 2a 20   that..      ** 
2ffcf 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
2ffd0 74 65 64 20 74 6f 20 61 20 6e 75 6d 65 72 69 63  ted to a numeric
2ffd1 20 76 61 6c 75 65 2e 20 0d 0a 20 20 20 20 20 20   value. ..      
2ffd2 2a 2f 0d 0a 20 20 20 20 20 20 64 6f 75 62 6c 65  */..      double
2ffd3 20 72 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   rVal = sqlite3_
2ffd4 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
2ffd5 76 5b 30 5d 29 3b 0d 0a 20 20 20 20 20 20 69 66  v[0]);..      if
2ffd6 28 20 72 56 61 6c 3c 30 20 29 20 72 56 61 6c 20  ( rVal<0 ) rVal 
2ffd7 3d 20 2d 72 56 61 6c 3b 0d 0a 20 20 20 20 20 20  = -rVal;..      
2ffd8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2ffd9 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72  ouble(context, r
2ffda 56 61 6c 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  Val);..      bre
2ffdb 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ak;..    }..  }.
2ffdc 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70  .}..../*..** Imp
2ffdd 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2ffde 68 65 20 73 75 62 73 74 72 28 29 20 66 75 6e 63  he substr() func
2ffdf 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 73 75  tion...**..** su
2ffe0 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 72  bstr(x,p1,p2)  r
2ffe1 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 63  eturns p2 charac
2ffe2 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 69  ters of x[] begi
2ffe3 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0d 0a  nning with p1...
2ffe4 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78  ** p1 is 1-index
2ffe5 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78  ed.  So substr(x
2ffe6 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68  ,1,1) returns th
2ffe7 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
2ffe8 72 0d 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20  r..** of x.  If 
2ffe9 78 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20  x is text, then 
2ffea 77 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e  we actually coun
2ffeb 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  t UTF-8 characte
2ffec 72 73 2e 0d 0a 2a 2a 20 49 66 20 78 20 69 73 20  rs...** If x is 
2ffed 61 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20  a blob, then we 
2ffee 63 6f 75 6e 74 20 62 79 74 65 73 2e 0d 0a 2a 2a  count bytes...**
2ffef 0d 0a 2a 2a 20 49 66 20 70 31 20 69 73 20 6e 65  ..** If p1 is ne
2fff0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 77 65 20  gative, then we 
2fff1 62 65 67 69 6e 20 61 62 73 28 70 31 29 20 66 72  begin abs(p1) fr
2fff2 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 78 5b  om the end of x[
2fff3 5d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 32  ]...**..** If p2
2fff4 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 72 65   is negative, re
2fff5 74 75 72 6e 20 74 68 65 20 70 32 20 63 68 61 72  turn the p2 char
2fff6 61 63 74 65 72 73 20 70 72 65 63 65 65 64 69 6e  acters preceedin
2fff7 67 20 70 31 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  g p1...*/..stati
2fff8 63 20 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e  c void substrFun
2fff9 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  c(..  sqlite3_co
2fffa 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d  ntext *context,.
2fffb 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20  .  int argc,..  
2fffc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2fffd 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73  argv..){..  cons
2fffe 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2ffff 2a 7a 3b 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73  *z;..  const uns
30000 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0d  igned char *z2;.
30001 0a 20 20 69 6e 74 20 6c 65 6e 3b 0d 0a 20 20 69  .  int len;..  i
30002 6e 74 20 70 30 74 79 70 65 3b 0d 0a 20 20 69 36  nt p0type;..  i6
30003 34 20 70 31 2c 20 70 32 3b 0d 0a 20 20 69 6e 74  4 p1, p2;..  int
30004 20 6e 65 67 50 32 20 3d 20 30 3b 0d 0a 0d 0a 20   negP2 = 0;.... 
30005 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33   assert( argc==3
30006 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0d 0a   || argc==2 );..
30007 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
30008 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
30009 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0d 0a  )==SQLITE_NULL..
3000a 20 20 20 7c 7c 20 28 61 72 67 63 3d 3d 33 20 26     || (argc==3 &
3000b 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  & sqlite3_value_
3000c 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3d 3d 53  type(argv[2])==S
3000d 51 4c 49 54 45 5f 4e 55 4c 4c 29 0d 0a 20 20 29  QLITE_NULL)..  )
3000e 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a  {..    return;..
3000f 20 20 7d 0d 0a 20 20 70 30 74 79 70 65 20 3d 20    }..  p0type = 
30010 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
30011 70 65 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20  pe(argv[0]);..  
30012 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  p1 = sqlite3_val
30013 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b  ue_int(argv[1]);
30014 0d 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d  ..  if( p0type==
30015 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0d 0a  SQLITE_BLOB ){..
30016 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
30017 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
30018 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20 7a 20 3d  gv[0]);..    z =
30019 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
3001a 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20  lob(argv[0]);.. 
3001b 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65     if( z==0 ) re
3001c 74 75 72 6e 3b 0d 0a 20 20 20 20 61 73 73 65 72  turn;..    asser
3001d 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f  t( len==sqlite3_
3001e 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
3001f 5b 30 5d 29 20 29 3b 0d 0a 20 20 7d 65 6c 73 65  [0]) );..  }else
30020 7b 0d 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  {..    z = sqlit
30021 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
30022 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20 69 66 28  gv[0]);..    if(
30023 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d   z==0 ) return;.
30024 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0d 0a 20  .    len = 0;.. 
30025 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0d 0a     if( p1<0 ){..
30026 20 20 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20        for(z2=z; 
30027 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0d 0a 20 20  *z2; len++){..  
30028 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
30029 50 5f 55 54 46 38 28 7a 32 29 3b 0d 0a 20 20 20  P_UTF8(z2);..   
3002a 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
3002b 0d 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20  ..  if( argc==3 
3002c 29 7b 0d 0a 20 20 20 20 70 32 20 3d 20 73 71 6c  ){..    p2 = sql
3002d 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
3002e 72 67 76 5b 32 5d 29 3b 0d 0a 20 20 20 20 69 66  rgv[2]);..    if
3002f 28 20 70 32 3c 30 20 29 7b 0d 0a 20 20 20 20 20  ( p2<0 ){..     
30030 20 70 32 20 3d 20 2d 70 32 3b 0d 0a 20 20 20 20   p2 = -p2;..    
30031 20 20 6e 65 67 50 32 20 3d 20 31 3b 0d 0a 20 20    negP2 = 1;..  
30032 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20    }..  }else{.. 
30033 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f     p2 = sqlite3_
30034 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
30035 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d  e(context)->aLim
30036 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
30037 4c 45 4e 47 54 48 5d 3b 0d 0a 20 20 7d 0d 0a 20  LENGTH];..  }.. 
30038 20 69 66 28 20 70 31 3c 30 20 29 7b 0d 0a 20 20   if( p1<0 ){..  
30039 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0d 0a 20 20    p1 += len;..  
3003a 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0d 0a 20    if( p1<0 ){.. 
3003b 20 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0d 0a       p2 += p1;..
3003c 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 29        if( p2<0 )
3003d 20 70 32 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20   p2 = 0;..      
3003e 70 31 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  p1 = 0;..    }..
3003f 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30    }else if( p1>0
30040 20 29 7b 0d 0a 20 20 20 20 70 31 2d 2d 3b 0d 0a   ){..    p1--;..
30041 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e 30    }else if( p2>0
30042 20 29 7b 0d 0a 20 20 20 20 70 32 2d 2d 3b 0d 0a   ){..    p2--;..
30043 20 20 7d 0d 0a 20 20 69 66 28 20 6e 65 67 50 32    }..  if( negP2
30044 20 29 7b 0d 0a 20 20 20 20 70 31 20 2d 3d 20 70   ){..    p1 -= p
30045 32 3b 0d 0a 20 20 20 20 69 66 28 20 70 31 3c 30  2;..    if( p1<0
30046 20 29 7b 0d 0a 20 20 20 20 20 20 70 32 20 2b 3d   ){..      p2 +=
30047 20 70 31 3b 0d 0a 20 20 20 20 20 20 70 31 20 3d   p1;..      p1 =
30048 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   0;..    }..  }.
30049 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30  .  assert( p1>=0
3004a 20 26 26 20 70 32 3e 3d 30 20 29 3b 0d 0a 20 20   && p2>=0 );..  
3004b 69 66 28 20 70 30 74 79 70 65 21 3d 53 51 4c 49  if( p0type!=SQLI
3004c 54 45 5f 42 4c 4f 42 20 29 7b 0d 0a 20 20 20 20  TE_BLOB ){..    
3004d 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70 31 20  while( *z && p1 
3004e 29 7b 0d 0a 20 20 20 20 20 20 53 51 4c 49 54 45  ){..      SQLITE
3004f 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0d 0a  _SKIP_UTF8(z);..
30050 20 20 20 20 20 20 70 31 2d 2d 3b 0d 0a 20 20 20        p1--;..   
30051 20 7d 0d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a   }..    for(z2=z
30052 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d  ; *z2 && p2; p2-
30053 2d 29 7b 0d 0a 20 20 20 20 20 20 53 51 4c 49 54  -){..      SQLIT
30054 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b  E_SKIP_UTF8(z2);
30055 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
30056 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
30057 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
30058 29 7a 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c  )z, (int)(z2-z),
30059 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
3005a 54 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  T);..  }else{.. 
3005b 20 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e     if( p1+p2>len
3005c 20 29 7b 0d 0a 20 20 20 20 20 20 70 32 20 3d 20   ){..      p2 = 
3005d 6c 65 6e 2d 70 31 3b 0d 0a 20 20 20 20 20 20 69  len-p1;..      i
3005e 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30  f( p2<0 ) p2 = 0
3005f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71  ;..    }..    sq
30060 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
30061 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  b(context, (char
30062 2a 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70  *)&z[p1], (int)p
30063 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  2, SQLITE_TRANSI
30064 45 4e 54 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ENT);..  }..}...
30065 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
30066 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f  tation of the ro
30067 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 0d 0a  und() function..
30068 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
30069 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
3006a 5f 50 4f 49 4e 54 0d 0a 73 74 61 74 69 63 20 76  _POINT..static v
3006b 6f 69 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71  oid roundFunc(sq
3006c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
3006d 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
3006e 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
3006f 2a 2a 61 72 67 76 29 7b 0d 0a 20 20 69 6e 74 20  **argv){..  int 
30070 6e 20 3d 20 30 3b 0d 0a 20 20 64 6f 75 62 6c 65  n = 0;..  double
30071 20 72 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 42 75   r;..  char *zBu
30072 66 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72  f;..  assert( ar
30073 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32  gc==1 || argc==2
30074 20 29 3b 0d 0a 20 20 69 66 28 20 61 72 67 63 3d   );..  if( argc=
30075 3d 32 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 53  =2 ){..    if( S
30076 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69  QLITE_NULL==sqli
30077 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
30078 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 72 6e  rgv[1]) ) return
30079 3b 0d 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  ;..    n = sqlit
3007a 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
3007b 76 5b 31 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20  v[1]);..    if( 
3007c 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0d 0a  n>30 ) n = 30;..
3007d 20 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20      if( n<0 ) n 
3007e 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  = 0;..  }..  if(
3007f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
30080 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
30081 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
30082 72 6e 3b 0d 0a 20 20 72 20 3d 20 73 71 6c 69 74  rn;..  r = sqlit
30083 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
30084 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 2f 2a 20  argv[0]);..  /* 
30085 49 66 20 59 3d 3d 30 20 61 6e 64 20 58 20 77 69  If Y==0 and X wi
30086 6c 6c 20 66 69 74 20 69 6e 20 61 20 36 34 2d 62  ll fit in a 64-b
30087 69 74 20 69 6e 74 2c 0d 0a 20 20 2a 2a 20 68 61  it int,..  ** ha
30088 6e 64 6c 65 20 74 68 65 20 72 6f 75 6e 64 69 6e  ndle the roundin
30089 67 20 64 69 72 65 63 74 6c 79 2c 0d 0a 20 20 2a  g directly,..  *
3008a 2a 20 6f 74 68 65 72 77 69 73 65 20 75 73 65 20  * otherwise use 
3008b 70 72 69 6e 74 66 2e 0d 0a 20 20 2a 2f 0d 0a 20  printf...  */.. 
3008c 20 69 66 28 20 6e 3d 3d 30 20 26 26 20 72 3e 3d   if( n==0 && r>=
3008d 30 20 26 26 20 72 3c 4c 41 52 47 45 53 54 5f 49  0 && r<LARGEST_I
3008e 4e 54 36 34 2d 31 20 29 7b 0d 0a 20 20 20 20 72  NT64-1 ){..    r
3008f 20 3d 20 28 64 6f 75 62 6c 65 29 28 28 73 71 6c   = (double)((sql
30090 69 74 65 5f 69 6e 74 36 34 29 28 72 2b 30 2e 35  ite_int64)(r+0.5
30091 29 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  ));..  }else if(
30092 20 6e 3d 3d 30 20 26 26 20 72 3c 30 20 26 26 20   n==0 && r<0 && 
30093 28 2d 72 29 3c 4c 41 52 47 45 53 54 5f 49 4e 54  (-r)<LARGEST_INT
30094 36 34 2d 31 20 29 7b 0d 0a 20 20 20 20 72 20 3d  64-1 ){..    r =
30095 20 2d 28 64 6f 75 62 6c 65 29 28 28 73 71 6c 69   -(double)((sqli
30096 74 65 5f 69 6e 74 36 34 29 28 28 2d 72 29 2b 30  te_int64)((-r)+0
30097 2e 35 29 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  .5));..  }else{.
30098 0a 20 20 20 20 7a 42 75 66 20 3d 20 73 71 6c 69  .    zBuf = sqli
30099 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 2e 2a  te3_mprintf("%.*
3009a 66 22 2c 6e 2c 72 29 3b 0d 0a 20 20 20 20 69 66  f",n,r);..    if
3009b 28 20 7a 42 75 66 3d 3d 30 20 29 7b 0d 0a 20 20  ( zBuf==0 ){..  
3009c 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3009d 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
3009e 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20  ontext);..      
3009f 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a  return;..    }..
300a0 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
300a1 7a 42 75 66 2c 20 26 72 2c 20 73 71 6c 69 74 65  zBuf, &r, sqlite
300a2 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 2c  3Strlen30(zBuf),
300a3 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0d 0a   SQLITE_UTF8);..
300a4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
300a5 28 7a 42 75 66 29 3b 0d 0a 20 20 7d 0d 0a 20 20  (zBuf);..  }..  
300a6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
300a7 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72  ouble(context, r
300a8 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  );..}..#endif...
300a9 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ./*..** Allocate
300aa 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20   nByte bytes of 
300ab 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c 69  space using sqli
300ac 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66  te3_malloc(). If
300ad 20 74 68 65 0d 0a 2a 2a 20 61 6c 6c 6f 63 61 74   the..** allocat
300ae 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20  ion fails, call 
300af 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
300b0 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20  rror_nomem() to 
300b1 6e 6f 74 69 66 79 0d 0a 2a 2a 20 74 68 65 20 64  notify..** the d
300b2 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
300b3 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  hat malloc() has
300b4 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65 74 75   failed and retu
300b5 72 6e 20 4e 55 4c 4c 2e 0d 0a 2a 2a 20 49 66 20  rn NULL...** If 
300b6 6e 42 79 74 65 20 69 73 20 6c 61 72 67 65 72 20  nByte is larger 
300b7 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  than the maximum
300b8 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
300b9 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0d 0a 2a 2a  length, then..**
300ba 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45   raise an SQLITE
300bb 5f 54 4f 4f 42 49 47 20 65 78 63 65 70 74 69 6f  _TOOBIG exceptio
300bc 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  n and return NUL
300bd 4c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  L...*/..static v
300be 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c  oid *contextMall
300bf 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  oc(sqlite3_conte
300c0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34  xt *context, i64
300c1 20 6e 42 79 74 65 29 7b 0d 0a 20 20 63 68 61 72   nByte){..  char
300c2 20 2a 7a 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20   *z;..  sqlite3 
300c3 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
300c4 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
300c5 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 61 73 73  context);..  ass
300c6 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0d  ert( nByte>0 );.
300c7 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79  .  testcase( nBy
300c8 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  te==db->aLimit[S
300c9 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
300ca 54 48 5d 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  TH] );..  testca
300cb 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61  se( nByte==db->a
300cc 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
300cd 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0d  IT_LENGTH]+1 );.
300ce 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d  .  if( nByte>db-
300cf 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
300d0 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0d  IMIT_LENGTH] ){.
300d1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
300d2 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
300d3 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20  (context);..    
300d4 7a 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b  z = 0;..  }else{
300d5 0d 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65  ..    z = sqlite
300d6 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 42 79  3Malloc((int)nBy
300d7 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a  te);..    if( !z
300d8 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
300d9 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
300da 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0d  nomem(context);.
300db 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
300dc 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn z;..}..../
300dd 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  *..** Implementa
300de 74 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 70 65  tion of the uppe
300df 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20  r() and lower() 
300e0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a  SQL functions...
300e1 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
300e2 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69 74 65  upperFunc(sqlite
300e3 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
300e4 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
300e5 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
300e6 67 76 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  gv){..  unsigned
300e7 20 73 68 6f 72 74 20 2a 7a 31 3b 0d 0a 20 20 63   short *z1;..  c
300e8 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 73 68  onst unsigned sh
300e9 6f 72 74 20 2a 7a 32 3b 0d 0a 20 20 69 6e 74 20  ort *z2;..  int 
300ea 69 2c 20 6e 3b 0d 0a 20 20 69 66 28 20 61 72 67  i, n;..  if( arg
300eb 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55  c<1 || SQLITE_NU
300ec 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  LL==sqlite3_valu
300ed 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20  e_type(argv[0]) 
300ee 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7a 32 20  ) return;..  z2 
300ef 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
300f0 64 20 73 68 6f 72 74 20 2a 29 73 71 6c 69 74 65  d short *)sqlite
300f1 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61  3_value_text16(a
300f2 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 6e 20 3d 20  rgv[0]);..  n = 
300f3 77 63 73 6c 65 6e 28 7a 32 29 3b 0d 0a 20 20 2f  wcslen(z2);..  /
300f4 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
300f5 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73  e call to _bytes
300f6 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61  () does not inva
300f7 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74  lidate the _text
300f8 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20  () pointer */.. 
300f9 20 2f 2f 61 73 73 65 72 74 28 20 7a 32 3d 3d 28   //assert( z2==(
300fa 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
300fb 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
300fc 29 20 29 3b 0d 0a 20 20 69 66 28 20 7a 32 20 29  ) );..  if( z2 )
300fd 7b 0d 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74  {..    z1 = cont
300fe 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
300ff 74 2c 20 6e 20 2a 20 32 20 2b 20 32 29 3b 0d 0a  t, n * 2 + 2);..
30100 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0d 0a 20      if( z1 ){.. 
30101 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20       memcpy(z1, 
30102 7a 32 2c 20 6e 20 2a 20 32 20 2b 20 32 29 3b 0d  z2, n * 2 + 2);.
30103 0a 09 20 20 43 68 61 72 55 70 70 65 72 57 28 7a  ..  CharUpperW(z
30104 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  1);..      sqlit
30105 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
30106 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 6e 20  (context, z1, n 
30107 2a 20 32 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  * 2, sqlite3_fre
30108 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  e);..    }..  }.
30109 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
3010a 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74 65  lowerFunc(sqlite
3010b 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3010c 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
3010d 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
3010e 67 76 29 7b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  gv){..  unsigned
3010f 20 73 68 6f 72 74 20 2a 7a 31 3b 0d 0a 20 20 63   short *z1;..  c
30110 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 73 68  onst unsigned sh
30111 6f 72 74 20 2a 7a 32 3b 0d 0a 20 20 69 6e 74 20  ort *z2;..  int 
30112 69 2c 20 6e 3b 0d 0a 20 20 69 66 28 20 61 72 67  i, n;..  if( arg
30113 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55  c<1 || SQLITE_NU
30114 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  LL==sqlite3_valu
30115 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20  e_type(argv[0]) 
30116 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7a 32 20  ) return;..  z2 
30117 3d 20 28 75 6e 73 69 67 6e 65 64 20 73 68 6f 72  = (unsigned shor
30118 74 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t*)sqlite3_value
30119 5f 74 65 78 74 31 36 28 61 72 67 76 5b 30 5d 29  _text16(argv[0])
3011a 3b 0d 0a 20 20 6e 20 3d 20 77 63 73 6c 65 6e 28  ;..  n = wcslen(
3011b 7a 32 29 3b 0d 0a 20 20 2f 2a 20 56 65 72 69 66  z2);..  /* Verif
3011c 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20  y that the call 
3011d 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73  to _bytes() does
3011e 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
3011f 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e  the _text() poin
30120 74 65 72 20 2a 2f 0d 0a 20 20 2f 2f 61 73 73 65  ter */..  //asse
30121 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73  rt( z2==(char*)s
30122 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
30123 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0d 0a 20  t(argv[0]) );.. 
30124 20 69 66 28 20 7a 32 20 29 7b 0d 0a 20 20 20 20   if( z2 ){..    
30125 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  z1 = contextMall
30126 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 20 2a 20  oc(context, n * 
30127 32 20 2b 20 32 29 3b 0d 0a 20 20 20 20 69 66 28  2 + 2);..    if(
30128 20 7a 31 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65   z1 ){..      me
30129 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 20 2a  mcpy(z1, z2, n *
3012a 20 32 20 2b 20 32 29 3b 0d 0a 20 20 20 20 20 20   2 + 2);..      
3012b 20 20 7a 31 5b 69 5d 20 3d 20 6f 61 73 55 70 70    z1[i] = oasUpp
3012c 65 72 32 4c 6f 77 65 72 5b 7a 31 5b 69 5d 5d 3b  er2Lower[z1[i]];
3012d 2f 2f 74 6f 6c 6f 77 65 72 28 7a 31 5b 69 5d 29  //tolower(z1[i])
3012e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
3012f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
30130 5f 74 65 78 74 31 36 28 63 6f 6e 74 65 78 74 2c  _text16(context,
30131 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33   z1, -1, sqlite3
30132 5f 66 72 65 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a  _free);..    }..
30133 20 20 7d 0d 0a 0d 0a 0d 0a 23 69 66 20 30 20 20    }......#if 0  
30134 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
30135 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20   is never used. 
30136 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 43  */../*..** The C
30137 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 20 49 46  OALESCE() and IF
30138 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f 6e 73  NULL() functions
30139 20 75 73 65 64 20 74 6f 20 62 65 20 69 6d 70 6c   used to be impl
3013a 65 6d 65 6e 74 65 64 20 61 73 20 73 68 6f 77 6e  emented as shown
3013b 0d 0a 2a 2a 20 68 65 72 65 2e 20 20 42 75 74 20  ..** here.  But 
3013c 6e 6f 77 20 74 68 65 79 20 61 72 65 20 69 6d 70  now they are imp
3013d 6c 65 6d 65 6e 74 65 64 20 61 73 20 56 44 42 45  lemented as VDBE
3013e 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20 75 6e   code so that un
3013f 75 73 65 64 20 61 72 67 75 6d 65 6e 74 73 0d 0a  used arguments..
30140 2a 2a 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  ** do not have t
30141 6f 20 62 65 20 63 6f 6d 70 75 74 65 64 2e 20 20  o be computed.  
30142 54 68 69 73 20 6c 65 67 61 63 79 20 69 6d 70 6c  This legacy impl
30143 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72 65  ementation is re
30144 74 61 69 6e 65 64 20 61 73 0d 0a 2a 2a 20 63 6f  tained as..** co
30145 6d 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 2f 2a 0d 0a  mment...*/../*..
30146 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
30147 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28  n of the IFNULL(
30148 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f  ), NVL(), and CO
30149 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f  ALESCE() functio
3014a 6e 73 2e 20 20 0d 0a 2a 2a 20 41 6c 6c 20 74 68  ns.  ..** All th
3014b 72 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65 20  ree do the same 
3014c 74 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65 74  thing.  They ret
3014d 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e 6f  urn the first no
3014e 6e 2d 4e 55 4c 4c 0d 0a 2a 2a 20 61 72 67 75 6d  n-NULL..** argum
3014f 65 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ent...*/..static
30150 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46 75 6e 63   void ifnullFunc
30151 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  (..  sqlite3_con
30152 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a  text *context,..
30153 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73    int argc,..  s
30154 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
30155 72 67 76 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69  rgv..){..  int i
30156 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
30157 61 72 67 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  argc; i++){..   
30158 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   if( SQLITE_NULL
30159 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  !=sqlite3_value_
3015a 74 79 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b  type(argv[i]) ){
3015b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
3015c 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
3015d 74 65 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0d  text, argv[i]);.
3015e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
3015f 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65     }..  }..}..#e
30160 6e 64 69 66 20 2f 2a 20 4e 4f 54 20 55 53 45 44  ndif /* NOT USED
30161 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 69 66 6e   */..#define ifn
30162 75 6c 6c 46 75 6e 63 20 76 65 72 73 69 6f 6e 46  ullFunc versionF
30163 75 6e 63 20 20 20 2f 2a 20 53 75 62 73 74 69 74  unc   /* Substit
30164 75 74 65 20 66 75 6e 63 74 69 6f 6e 20 2d 20 6e  ute function - n
30165 65 76 65 72 20 63 61 6c 6c 65 64 20 2a 2f 0d 0a  ever called */..
30166 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65  ../*..** Impleme
30167 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f  ntation of rando
30168 6d 28 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72  m().  Return a r
30169 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20  andom integer.  
3016a 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
3016b 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0d 0a 20  d randomFunc(.. 
3016c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3016d 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e   *context,..  in
3016e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20 20 73 71  t NotUsed,..  sq
3016f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
30170 74 55 73 65 64 32 0d 0a 29 7b 0d 0a 20 20 73 71  tUsed2..){..  sq
30171 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0d 0a 20  lite_int64 r;.. 
30172 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
30173 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
30174 73 65 64 32 29 3b 0d 0a 20 20 73 71 6c 69 74 65  sed2);..  sqlite
30175 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
30176 65 6f 66 28 72 29 2c 20 26 72 29 3b 0d 0a 20 20  eof(r), &r);..  
30177 69 66 28 20 72 3c 30 20 29 7b 0d 0a 20 20 20 20  if( r<0 ){..    
30178 2f 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 70 72  /* We need to pr
30179 65 76 65 6e 74 20 61 20 72 61 6e 64 6f 6d 20 6e  event a random n
3017a 75 6d 62 65 72 20 6f 66 20 30 78 38 30 30 30 30  umber of 0x80000
3017b 30 30 30 30 30 30 30 30 30 30 30 20 0d 0a 20 20  00000000000 ..  
3017c 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32 33 33 37    ** (or -922337
3017d 32 30 33 36 38 35 34 37 37 35 38 30 38 29 20 73  2036854775808) s
3017e 69 6e 63 65 20 77 68 65 6e 20 79 6f 75 20 64 6f  ince when you do
3017f 20 61 62 73 28 29 20 6f 66 20 74 68 61 74 0d 0a   abs() of that..
30180 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66      ** number of
30181 20 79 6f 75 20 67 65 74 20 74 68 65 20 73 61 6d   you get the sam
30182 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61 67 61  e value back aga
30183 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69 73 0d  in.  To do this.
30184 0a 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61 79  .    ** in a way
30185 20 74 68 61 74 20 69 73 20 74 65 73 74 61 62 6c   that is testabl
30186 65 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67 6e  e, mask the sign
30187 20 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67 61   bit off of nega
30188 74 69 76 65 0d 0a 20 20 20 20 2a 2a 20 76 61 6c  tive..    ** val
30189 75 65 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69  ues, resulting i
3018a 6e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  n a positive val
3018b 75 65 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74  ue.  Then take t
3018c 68 65 20 0d 0a 20 20 20 20 2a 2a 20 32 73 20 63  he ..    ** 2s c
3018d 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61  omplement of tha
3018e 74 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65  t positive value
3018f 2e 20 20 54 68 65 20 65 6e 64 20 72 65 73 75 6c  .  The end resul
30190 74 20 63 61 6e 0d 0a 20 20 20 20 2a 2a 20 74 68  t can..    ** th
30191 65 72 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c 65  erefore be no le
30192 73 73 20 74 68 61 6e 20 2d 39 32 32 33 33 37 32  ss than -9223372
30193 30 33 36 38 35 34 37 37 35 38 30 37 2e 0d 0a 20  036854775807... 
30194 20 20 20 2a 2f 0d 0a 20 20 20 20 72 20 3d 20 2d     */..    r = -
30195 28 72 20 5e 20 28 28 28 73 71 6c 69 74 65 33 5f  (r ^ (((sqlite3_
30196 69 6e 74 36 34 29 31 29 3c 3c 36 33 29 29 3b 0d  int64)1)<<63));.
30197 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
30198 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
30199 74 65 78 74 2c 20 72 29 3b 0d 0a 7d 0d 0a 0d 0a  text, r);..}....
3019a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  /*..** Implement
3019b 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62  ation of randomb
3019c 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 72 6e 20  lob(N).  Return 
3019d 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 0d 0a 2a  a random blob..*
3019e 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79 74 65  * that is N byte
3019f 73 20 6c 6f 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61  s long...*/..sta
301a0 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42  tic void randomB
301a1 6c 6f 62 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  lob(..  sqlite3_
301a2 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
301a3 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a  ,..  int argc,..
301a4 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
301a5 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 69 6e  **argv..){..  in
301a6 74 20 6e 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64  t n;..  unsigned
301a7 20 63 68 61 72 20 2a 70 3b 0d 0a 20 20 61 73 73   char *p;..  ass
301a8 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0d  ert( argc==1 );.
301a9 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
301aa 54 45 52 28 61 72 67 63 29 3b 0d 0a 20 20 6e 20  TER(argc);..  n 
301ab 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
301ac 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20  int(argv[0]);.. 
301ad 20 69 66 28 20 6e 3c 31 20 29 7b 0d 0a 20 20 20   if( n<1 ){..   
301ae 20 6e 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20   n = 1;..  }..  
301af 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  p = contextMallo
301b0 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0d 0a  c(context, n);..
301b1 20 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20    if( p ){..    
301b2 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
301b3 73 73 28 6e 2c 20 70 29 3b 0d 0a 20 20 20 20 73  ss(n, p);..    s
301b4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
301b5 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  ob(context, (cha
301b6 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33  r*)p, n, sqlite3
301b7 5f 66 72 65 65 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  _free);..  }..}.
301b8 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d  .../*..** Implem
301b9 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
301ba 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
301bb 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  d() SQL function
301bc 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0d 0a 2a  .  The return..*
301bd 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  * value is the s
301be 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74  ame as the sqlit
301bf 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
301c0 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e 63 74  owid() API funct
301c1 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
301c2 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72   void last_inser
301c3 74 5f 72 6f 77 69 64 28 0d 0a 20 20 73 71 6c 69  t_rowid(..  sqli
301c4 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
301c5 74 65 78 74 2c 20 0d 0a 20 20 69 6e 74 20 4e 6f  text, ..  int No
301c6 74 55 73 65 64 2c 20 0d 0a 20 20 73 71 6c 69 74  tUsed, ..  sqlit
301c7 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
301c8 65 64 32 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74  ed2..){..  sqlit
301c9 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
301ca 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
301cb 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  le(context);..  
301cc 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
301cd 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
301ce 65 64 32 29 3b 0d 0a 20 20 2f 2a 20 49 4d 50 3a  ed2);..  /* IMP:
301cf 20 52 2d 35 31 35 31 33 2d 31 32 30 32 36 20 54   R-51513-12026 T
301d0 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  he last_insert_r
301d1 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74  owid() SQL funct
301d2 69 6f 6e 20 69 73 20 61 0d 0a 20 20 2a 2a 20 77  ion is a..  ** w
301d3 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
301d4 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  e sqlite3_last_i
301d5 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 43 2f  nsert_rowid() C/
301d6 43 2b 2b 20 69 6e 74 65 72 66 61 63 65 0d 0a 20  C++ interface.. 
301d7 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f   ** function. */
301d8 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
301d9 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
301da 2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  , sqlite3_last_i
301db 6e 73 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29  nsert_rowid(db))
301dc 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
301dd 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
301de 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 20 53   the changes() S
301df 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a  QL function...**
301e0 0d 0a 2a 2a 20 49 4d 50 3a 20 52 2d 36 32 30 37  ..** IMP: R-6207
301e1 33 2d 31 31 32 30 39 20 54 68 65 20 63 68 61 6e  3-11209 The chan
301e2 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69  ges() SQL functi
301e3 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 0d  on is a wrapper.
301e4 0a 2a 2a 20 61 72 6f 75 6e 64 20 74 68 65 20 73  .** around the s
301e5 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
301e6 20 43 2f 43 2b 2b 20 66 75 6e 63 74 69 6f 6e 20   C/C++ function 
301e7 61 6e 64 20 68 65 6e 63 65 20 66 6f 6c 6c 6f 77  and hence follow
301e8 73 20 74 68 65 20 73 61 6d 65 0d 0a 2a 2a 20 72  s the same..** r
301e9 75 6c 65 73 20 66 6f 72 20 63 6f 75 6e 74 69 6e  ules for countin
301ea 67 20 63 68 61 6e 67 65 73 2e 0d 0a 2a 2f 0d 0a  g changes...*/..
301eb 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 61 6e  static void chan
301ec 67 65 73 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ges(..  sqlite3_
301ed 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
301ee 2c 0d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  ,..  int NotUsed
301ef 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ,..  sqlite3_val
301f0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0d 0a 29  ue **NotUsed2..)
301f1 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
301f2 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
301f3 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
301f4 74 65 78 74 29 3b 0d 0a 20 20 55 4e 55 53 45 44  text);..  UNUSED
301f5 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
301f6 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d  sed, NotUsed2);.
301f7 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
301f8 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73  t_int(context, s
301f9 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 64  qlite3_changes(d
301fa 62 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  b));..}..../*..*
301fb 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
301fc 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 68   of the total_ch
301fd 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63  anges() SQL func
301fe 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72  tion.  The retur
301ff 6e 20 76 61 6c 75 65 20 69 73 0d 0a 2a 2a 20 74  n value is..** t
30200 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  he same as the s
30201 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
30202 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74  nges() API funct
30203 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ion...*/..static
30204 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e   void total_chan
30205 67 65 73 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ges(..  sqlite3_
30206 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
30207 2c 0d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  ,..  int NotUsed
30208 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ,..  sqlite3_val
30209 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0d 0a 29  ue **NotUsed2..)
3020a 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
3020b 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
3020c 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
3020d 74 65 78 74 29 3b 0d 0a 20 20 55 4e 55 53 45 44  text);..  UNUSED
3020e 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
3020f 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d  sed, NotUsed2);.
30210 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 32 37  .  /* IMP: R-527
30211 35 36 2d 34 31 39 39 33 20 54 68 69 73 20 66 75  56-41993 This fu
30212 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70  nction is a wrap
30213 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 0d 0a  per around the..
30214 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 74    ** sqlite3_tot
30215 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 43 2f 43  al_changes() C/C
30216 2b 2b 20 69 6e 74 65 72 66 61 63 65 2e 20 2a 2f  ++ interface. */
30217 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
30218 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
30219 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
3021a 61 6e 67 65 73 28 64 62 29 29 3b 0d 0a 7d 0d 0a  anges(db));..}..
3021b 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 73 74 72 75 63  ../*..** A struc
3021c 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f  ture defining ho
3021d 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79  w to do GLOB-sty
3021e 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0d  le comparisons..
3021f 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 63 6f 6d 70  .*/..struct comp
30220 61 72 65 49 6e 66 6f 20 7b 0d 0a 20 20 75 38 20  areInfo {..  u8 
30221 6d 61 74 63 68 41 6c 6c 3b 0d 0a 20 20 75 38 20  matchAll;..  u8 
30222 6d 61 74 63 68 4f 6e 65 3b 0d 0a 20 20 75 38 20  matchOne;..  u8 
30223 6d 61 74 63 68 53 65 74 3b 0d 0a 20 20 75 38 20  matchSet;..  u8 
30224 6e 6f 43 61 73 65 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f  noCase;..};..../
30225 2a 0d 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61  *..** For LIKE a
30226 6e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67  nd GLOB matching
30227 20 6f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69   on EBCDIC machi
30228 6e 65 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74  nes, assume that
30229 20 65 76 65 72 79 0d 0a 2a 2a 20 63 68 61 72 61   every..** chara
3022a 63 74 65 72 20 69 73 20 65 78 61 63 74 6c 79 20  cter is exactly 
3022b 6f 6e 65 20 62 79 74 65 20 69 6e 20 73 69 7a 65  one byte in size
3022c 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61  .  Also, all cha
3022d 72 61 63 74 65 72 73 20 61 72 65 0d 0a 2a 2a 20  racters are..** 
3022e 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63 69 70  able to particip
3022f 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63 61 73  ate in upper-cas
30230 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20  e-to-lower-case 
30231 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42 43 44  mappings in EBCD
30232 49 43 0d 0a 2a 2a 20 77 68 65 72 65 61 73 20 6f  IC..** whereas o
30233 6e 6c 79 20 63 68 61 72 61 63 74 65 72 73 20 6c  nly characters l
30234 65 73 73 20 74 68 61 6e 20 30 78 38 30 20 64 6f  ess than 0x80 do
30235 20 69 6e 20 41 53 43 49 49 2e 0d 0a 2a 2f 0d 0a   in ASCII...*/..
30236 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
30237 54 45 5f 45 42 43 44 49 43 29 0d 0a 23 20 64 65  TE_EBCDIC)..# de
30238 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38  fine sqlite3Utf8
30239 52 65 61 64 28 41 2c 43 29 20 20 28 2a 28 41 2b  Read(A,C)  (*(A+
3023a 2b 29 29 0d 0a 23 20 64 65 66 69 6e 65 20 47 6c  +))..# define Gl
3023b 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41  ogUpperToLower(A
3023c 29 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33 55  )   A = sqlite3U
3023d 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0d 0a  pperToLower[A]..
3023e 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
3023f 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72  GlogUpperToLower
30240 28 41 29 20 20 20 69 66 28 20 21 28 28 41 29 26  (A)   if( !((A)&
30241 7e 30 78 37 66 29 20 29 7b 20 41 20 3d 20 73 71  ~0x7f) ){ A = sq
30242 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
30243 72 5b 41 5d 3b 20 7d 0d 0a 23 65 6e 64 69 66 0d  r[A]; }..#endif.
30244 0a 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ...static const 
30245 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
30246 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20  fo globInfo = { 
30247 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30  '*', '?', '[', 0
30248 20 7d 3b 0d 0a 2f 2a 20 54 68 65 20 63 6f 72 72   };../* The corr
30249 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61 76  ect SQL-92 behav
3024a 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20 4c  ior is for the L
3024b 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  IKE operator to 
3024c 69 67 6e 6f 72 65 0d 0a 2a 2a 20 63 61 73 65 2e  ignore..** case.
3024d 20 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45    Thus  'a' LIKE
3024e 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72   'A' would be tr
3024f 75 65 2e 20 2a 2f 0d 0a 73 74 61 74 69 63 20 63  ue. */..static c
30250 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
30251 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f  areInfo likeInfo
30252 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f  Norm = { '%', '_
30253 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0d 0a 2f 2a  ',   0, 1 };../*
30254 20 49 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f   If SQLITE_CASE_
30255 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69  SENSITIVE_LIKE i
30256 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  s defined, then 
30257 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
30258 72 0d 0a 2a 2a 20 69 73 20 63 61 73 65 20 73 65  r..** is case se
30259 6e 73 69 74 69 76 65 20 63 61 75 73 69 6e 67 20  nsitive causing 
3025a 27 61 27 20 4c 49 4b 45 20 27 41 27 20 74 6f 20  'a' LIKE 'A' to 
3025b 62 65 20 66 61 6c 73 65 20 2a 2f 0d 0a 73 74 61  be false */..sta
3025c 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
3025d 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b   compareInfo lik
3025e 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27  eInfoAlt = { '%'
3025f 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b  , '_',   0, 0 };
30260 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 61  ..../*..** Compa
30261 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72  re two UTF-8 str
30262 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74  ings for equalit
30263 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73  y where the firs
30264 74 20 73 74 72 69 6e 67 20 63 61 6e 0d 0a 2a 2a  t string can..**
30265 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20   potentially be 
30266 61 20 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73  a "glob" express
30267 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75  ion.  Return tru
30268 65 20 28 31 29 20 69 66 20 74 68 65 79 0d 0a 2a  e (1) if they..*
30269 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61  * are the same a
3026a 6e 64 20 66 61 6c 73 65 20 28 30 29 20 69 66 20  nd false (0) if 
3026b 74 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65  they are differe
3026c 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 47 6c 6f 62  nt...**..** Glob
3026d 62 69 6e 67 20 72 75 6c 65 73 3a 0d 0a 2a 2a 0d  bing rules:..**.
3026e 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20 20 20 20  .**      '*'    
3026f 20 20 20 4d 61 74 63 68 65 73 20 61 6e 79 20 73     Matches any s
30270 65 71 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20  equence of zero 
30271 6f 72 20 6d 6f 72 65 20 63 68 61 72 61 63 74 65  or more characte
30272 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  rs...**..**     
30273 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63 68   '?'       Match
30274 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63  es exactly one c
30275 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a  haracter...**..*
30276 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20  *     [...]     
30277 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61   Matches one cha
30278 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20  racter from the 
30279 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66  enclosed list of
3027a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
3027b 20 20 20 20 63 68 61 72 61 63 74 65 72 73 2e 0d      characters..
3027c 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e  .**..**     [^..
3027d 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f  .]     Matches o
3027e 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74  ne character not
3027f 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65 64   in the enclosed
30280 20 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57   list...**..** W
30281 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e  ith the [...] an
30282 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e  d [^...] matchin
30283 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74  g, a ']' charact
30284 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64  er can be includ
30285 65 64 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69  ed..** in the li
30286 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20  st by making it 
30287 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
30288 74 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72  ter after '[' or
30289 20 27 5e 27 2e 20 20 41 0d 0a 2a 2a 20 72 61 6e   '^'.  A..** ran
3028a 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  ge of characters
3028b 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 69 65   can be specifie
3028c 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78  d using '-'.  Ex
3028d 61 6d 70 6c 65 3a 0d 0a 2a 2a 20 22 5b 61 2d 7a  ample:..** "[a-z
3028e 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 73  ]" matches any s
3028f 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65  ingle lower-case
30290 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 74   letter.  To mat
30291 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0d 0a  ch a '-', make..
30292 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20 63  ** it the last c
30293 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20  haracter in the 
30294 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  list...**..** Th
30295 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
30296 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62 75 74  ually quick, but
30297 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20   can be N**2 in 
30298 74 68 65 20 77 6f 72 73 74 20 63 61 73 65 2e 0d  the worst case..
30299 0a 2a 2a 0d 0a 2a 2a 20 48 69 6e 74 73 3a 20 74  .**..** Hints: t
3029a 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72 20 27  o match '*' or '
3029b 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69 6e 20  ?', put them in 
3029c 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69 73  "[]".  Like this
3029d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20  :..**..**       
3029e 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20    abc[*]xyz     
3029f 20 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a     Matches "abc*
302a0 78 79 7a 22 20 6f 6e 6c 79 0d 0a 2a 2f 0d 0a 73  xyz" only..*/..s
302a1 74 61 74 69 63 20 69 6e 74 20 70 61 74 74 65 72  tatic int patter
302a2 6e 43 6f 6d 70 61 72 65 28 0d 0a 20 20 63 6f 6e  nCompare(..  con
302a3 73 74 20 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c  st u8 *zPattern,
302a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
302a5 20 54 68 65 20 67 6c 6f 62 20 70 61 74 74 65 72   The glob patter
302a6 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 38  n */..  const u8
302a7 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 20 20   *zString,      
302a8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
302a9 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72  string to compar
302aa 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 67 6c  e against the gl
302ab 6f 62 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 73  ob */..  const s
302ac 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
302ad 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66  o *pInfo, /* Inf
302ae 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 68  ormation about h
302af 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d  ow to do the com
302b0 70 61 72 65 20 2a 2f 0d 0a 20 20 75 33 32 20 65  pare */..  u32 e
302b1 73 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sc              
302b2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
302b3 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
302b4 74 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 33  ter */..){..  u3
302b5 32 20 63 2c 20 63 32 3b 0d 0a 20 20 69 6e 74 20  2 c, c2;..  int 
302b6 69 6e 76 65 72 74 3b 0d 0a 20 20 69 6e 74 20 73  invert;..  int s
302b7 65 65 6e 3b 0d 0a 20 20 75 38 20 6d 61 74 63 68  een;..  u8 match
302b8 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74  One = pInfo->mat
302b9 63 68 4f 6e 65 3b 0d 0a 20 20 75 38 20 6d 61 74  chOne;..  u8 mat
302ba 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d  chAll = pInfo->m
302bb 61 74 63 68 41 6c 6c 3b 0d 0a 20 20 75 38 20 6d  atchAll;..  u8 m
302bc 61 74 63 68 53 65 74 20 3d 20 70 49 6e 66 6f 2d  atchSet = pInfo-
302bd 3e 6d 61 74 63 68 53 65 74 3b 0d 0a 20 20 75 38  >matchSet;..  u8
302be 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d   noCase = pInfo-
302bf 3e 6e 6f 43 61 73 65 3b 20 0d 0a 20 20 69 6e 74  >noCase; ..  int
302c0 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
302c1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
302c2 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61  the previous cha
302c3 72 61 63 74 65 72 20 77 61 73 20 27 65 73 63 61  racter was 'esca
302c4 70 65 27 20 2a 2f 0d 0a 0d 0a 20 20 77 68 69 6c  pe' */....  whil
302c5 65 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55  e( (c = sqlite3U
302c6 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e  tf8Read(zPattern
302c7 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20  ,&zPattern))!=0 
302c8 29 7b 0d 0a 20 20 20 20 69 66 28 20 21 70 72 65  ){..    if( !pre
302c9 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61  vEscape && c==ma
302ca 74 63 68 41 6c 6c 20 29 7b 0d 0a 20 20 20 20 20  tchAll ){..     
302cb 20 77 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74   while( (c=sqlit
302cc 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
302cd 65 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 20  ern,&zPattern)) 
302ce 3d 3d 20 6d 61 74 63 68 41 6c 6c 0d 0a 20 20 20  == matchAll..   
302cf 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63              || c
302d0 20 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0d   == matchOne ){.
302d1 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
302d2 6d 61 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69  matchOne && sqli
302d3 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
302d4 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d  ing, &zString)==
302d5 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  0 ){..          
302d6 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20  return 0;..     
302d7 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
302d8 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b       if( c==0 ){
302d9 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
302da 20 31 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65   1;..      }else
302db 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b 0d 0a   if( c==esc ){..
302dc 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69          c = sqli
302dd 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
302de 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29  tern, &zPattern)
302df 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ;..        if( c
302e0 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
302e1 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20    return 0;..   
302e2 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65       }..      }e
302e3 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
302e4 53 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Set ){..        
302e5 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29  assert( esc==0 )
302e6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  ;         /* Thi
302e7 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c  s is GLOB, not L
302e8 49 4b 45 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  IKE */..        
302e9 61 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74  assert( matchSet
302ea 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27  <0x80 );  /* '['
302eb 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74   is a single-byt
302ec 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0d 0a  e character */..
302ed 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a          while( *
302ee 7a 53 74 72 69 6e 67 20 26 26 20 70 61 74 74 65  zString && patte
302ef 72 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74  rnCompare(&zPatt
302f0 65 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c  ern[-1],zString,
302f1 70 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b  pInfo,esc)==0 ){
302f2 0d 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  ..          SQLI
302f3 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74  TE_SKIP_UTF8(zSt
302f4 72 69 6e 67 29 3b 0d 0a 20 20 20 20 20 20 20 20  ring);..        
302f5 7d 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  }..        retur
302f6 6e 20 2a 7a 53 74 72 69 6e 67 21 3d 30 3b 0d 0a  n *zString!=0;..
302f7 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 77        }..      w
302f8 68 69 6c 65 28 20 28 63 32 20 3d 20 73 71 6c 69  hile( (c2 = sqli
302f9 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
302fa 69 6e 67 2c 26 7a 53 74 72 69 6e 67 29 29 21 3d  ing,&zString))!=
302fb 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66  0 ){..        if
302fc 28 20 6e 6f 43 61 73 65 20 29 7b 0d 0a 20 20 20  ( noCase ){..   
302fd 20 20 20 20 20 20 20 63 32 20 3d 20 6f 61 73 55         c2 = oasU
302fe 70 70 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73 69  pper2Lower[(unsi
302ff 67 6e 65 64 20 73 68 6f 72 74 29 63 32 5d 3b 0d  gned short)c2];.
30300 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 6f  .          c = o
30301 61 73 55 70 70 65 72 32 4c 6f 77 65 72 5b 28 75  asUpper2Lower[(u
30302 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 29 63 5d  nsigned short)c]
30303 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  ;..          whi
30304 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63  le( c2 != 0 && c
30305 32 20 21 3d 20 63 20 29 7b 0d 0a 20 20 20 20 20  2 != c ){..     
30306 20 20 20 20 20 20 20 63 32 20 3d 20 6f 61 73 55         c2 = oasU
30307 70 70 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73 69  pper2Lower[(unsi
30308 67 6e 65 64 20 73 68 6f 72 74 29 73 71 6c 69 74  gned short)sqlit
30309 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
3030a 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 5d 3b 0d  ng, &zString)];.
3030b 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
3030c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3030d 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63          while( c
3030e 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20  2 != 0 && c2 != 
3030f 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  c ){..          
30310 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
30311 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
30312 26 7a 53 74 72 69 6e 67 29 3b 0d 0a 20 20 20 20  &zString);..    
30313 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
30314 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
30315 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  c2==0 ) return 0
30316 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ;..        if( p
30317 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50  atternCompare(zP
30318 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70  attern,zString,p
30319 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75  Info,esc) ) retu
3031a 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rn 1;..      }..
3031b 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d        return 0;.
3031c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21  .    }else if( !
3031d 70 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d  prevEscape && c=
3031e 3d 6d 61 74 63 68 4f 6e 65 20 29 7b 0d 0a 20 20  =matchOne ){..  
3031f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 55      if( sqlite3U
30320 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
30321 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b   &zString)==0 ){
30322 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ..        return
30323 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   0;..      }..  
30324 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d    }else if( c==m
30325 61 74 63 68 53 65 74 20 29 7b 0d 0a 20 20 20 20  atchSet ){..    
30326 20 20 75 33 32 20 70 72 69 6f 72 5f 63 20 3d 20    u32 prior_c = 
30327 30 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  0;..      assert
30328 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f  ( esc==0 );    /
30329 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75  * This only occu
3032a 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74  rs for GLOB, not
3032b 20 4c 49 4b 45 20 2a 2f 0d 0a 20 20 20 20 20 20   LIKE */..      
3032c 73 65 65 6e 20 3d 20 30 3b 0d 0a 20 20 20 20 20  seen = 0;..     
3032d 20 69 6e 76 65 72 74 20 3d 20 30 3b 0d 0a 20 20   invert = 0;..  
3032e 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
3032f 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
30330 20 26 7a 53 74 72 69 6e 67 29 3b 0d 0a 20 20 20   &zString);..   
30331 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65     if( c==0 ) re
30332 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20 63  turn 0;..      c
30333 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
30334 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
30335 50 61 74 74 65 72 6e 29 3b 0d 0a 20 20 20 20 20  Pattern);..     
30336 20 69 66 28 20 63 32 3d 3d 27 5e 27 20 29 7b 0d   if( c2=='^' ){.
30337 0a 20 20 20 20 20 20 20 20 69 6e 76 65 72 74 20  .        invert 
30338 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 63 32  = 1;..        c2
30339 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
3033a 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50  ad(zPattern, &zP
3033b 61 74 74 65 72 6e 29 3b 0d 0a 20 20 20 20 20 20  attern);..      
3033c 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d  }..      if( c2=
3033d 3d 27 5d 27 20 29 7b 0d 0a 20 20 20 20 20 20 20  =']' ){..       
3033e 20 69 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65   if( c==']' ) se
3033f 65 6e 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  en = 1;..       
30340 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
30341 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20  8Read(zPattern, 
30342 26 7a 50 61 74 74 65 72 6e 29 3b 0d 0a 20 20 20  &zPattern);..   
30343 20 20 20 7d 0d 0a 20 20 20 20 20 20 77 68 69 6c     }..      whil
30344 65 28 20 63 32 20 26 26 20 63 32 21 3d 27 5d 27  e( c2 && c2!=']'
30345 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28   ){..        if(
30346 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74   c2=='-' && zPat
30347 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20  tern[0]!=']' && 
30348 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26  zPattern[0]!=0 &
30349 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0d 0a  & prior_c>0 ){..
3034a 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73            c2 = s
3034b 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
3034c 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65  Pattern, &zPatte
3034d 72 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  rn);..          
3034e 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26  if( c>=prior_c &
3034f 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d  & c<=c2 ) seen =
30350 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70   1;..          p
30351 72 69 6f 72 5f 63 20 3d 20 30 3b 0d 0a 20 20 20  rior_c = 0;..   
30352 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
30353 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32         if( c==c2
30354 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
30355 20 73 65 65 6e 20 3d 20 31 3b 0d 0a 20 20 20 20   seen = 1;..    
30356 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
30357 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63 32 3b     prior_c = c2;
30358 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
30359 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
3035a 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
3035b 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0d  rn, &zPattern);.
3035c 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3035d 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65  if( c2==0 || (se
3035e 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20  en ^ invert)==0 
3035f 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
30360 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rn 0;..      }..
30361 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73      }else if( es
30362 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63  c==c && !prevEsc
30363 61 70 65 20 29 7b 0d 0a 20 20 20 20 20 20 70 72  ape ){..      pr
30364 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0d 0a 20  evEscape = 1;.. 
30365 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
30366 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
30367 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26  8Read(zString, &
30368 7a 53 74 72 69 6e 67 29 3b 0d 0a 20 20 20 20 20  zString);..     
30369 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0d 0a   if( noCase ){..
3036a 20 20 20 20 20 20 20 20 63 20 3d 20 6f 61 73 55          c = oasU
3036b 70 70 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73 69  pper2Lower[(unsi
3036c 67 6e 65 64 20 73 68 6f 72 74 29 63 5d 3b 0d 0a  gned short)c];..
3036d 20 20 20 20 20 20 20 20 63 32 20 3d 20 6f 61 73          c2 = oas
3036e 55 70 70 65 72 32 4c 6f 77 65 72 5b 28 75 6e 73  Upper2Lower[(uns
3036f 69 67 6e 65 64 20 73 68 6f 72 74 29 63 32 5d 3b  igned short)c2];
30370 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
30371 20 69 66 28 20 63 21 3d 63 32 20 29 7b 0d 0a 20   if( c!=c2 ){.. 
30372 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
30373 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
30374 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
30375 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
30376 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d  return *zString=
30377 3d 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  =0;..}..../*..**
30378 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
30379 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
3037a 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
3037b 72 20 28 6f 72 20 47 4c 4f 42 20 77 68 69 63 68  r (or GLOB which
3037c 20 69 73 0d 0a 2a 2a 20 6a 75 73 74 20 61 20 76   is..** just a v
3037d 61 72 69 61 74 69 6f 6e 20 6f 66 20 4c 49 4b 45  ariation of LIKE
3037e 29 20 67 65 74 73 20 63 61 6c 6c 65 64 2e 20 20  ) gets called.  
3037f 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
30380 20 74 65 73 74 69 6e 67 0d 0a 2a 2a 20 6f 6e 6c   testing..** onl
30381 79 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53  y...*/..#ifdef S
30382 51 4c 49 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49  QLITE_TEST..SQLI
30383 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
30384 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20  e3_like_count = 
30385 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a  0;..#endif......
30386 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  /*..** Implement
30387 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b  ation of the lik
30388 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  e() SQL function
30389 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
3038a 20 69 6d 70 6c 65 6d 65 6e 74 73 0d 0a 2a 2a 20   implements..** 
3038b 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b  the build-in LIK
3038c 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65  E operator.  The
3038d 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
3038e 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
3038f 69 73 20 74 68 65 0d 0a 2a 2a 20 70 61 74 74 65  is the..** patte
30390 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  rn and the secon
30391 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
30392 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74  e string.  So, t
30393 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
30394 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  s:..**..**      
30395 20 41 20 4c 49 4b 45 20 42 0d 0a 2a 2a 0d 0a 2a   A LIKE B..**..*
30396 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  * is implemented
30397 20 61 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0d 0a   as like(B,A)...
30398 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65  **..** This same
30399 20 66 75 6e 63 74 69 6f 6e 20 28 77 69 74 68 20   function (with 
3039a 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6d 70  a different comp
3039b 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72  areInfo structur
3039c 65 29 20 63 6f 6d 70 75 74 65 73 0d 0a 2a 2a 20  e) computes..** 
3039d 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f  the GLOB operato
3039e 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  r...*/..static v
3039f 6f 69 64 20 6c 69 6b 65 46 75 6e 63 28 0d 0a 20  oid likeFunc(.. 
303a0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
303a1 20 2a 63 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 69   *context, ..  i
303a2 6e 74 20 61 72 67 63 2c 20 0d 0a 20 20 73 71 6c  nt argc, ..  sql
303a3 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
303a4 76 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 75  v..){..  const u
303a5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41  nsigned char *zA
303a6 2c 20 2a 7a 42 3b 0d 0a 20 20 75 33 32 20 65 73  , *zB;..  u32 es
303a7 63 61 70 65 20 3d 20 30 3b 0d 0a 20 20 69 6e 74  cape = 0;..  int
303a8 20 6e 50 61 74 3b 0d 0a 20 20 73 71 6c 69 74 65   nPat;..  sqlite
303a9 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
303aa 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
303ab 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 0d 0a 20  e(context);.... 
303ac 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   zB = sqlite3_va
303ad 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
303ae 29 3b 0d 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74  );..  zA = sqlit
303af 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
303b0 67 76 5b 31 5d 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  gv[1]);....  /* 
303b1 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68  Limit the length
303b2 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20   of the LIKE or 
303b3 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20  GLOB pattern to 
303b4 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0d 0a  avoid problems..
303b5 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63    ** of deep rec
303b6 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62  ursion and N*N b
303b7 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65  ehavior in patte
303b8 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0d 0a 20 20  rnCompare()...  
303b9 2a 2f 0d 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c  */..  nPat = sql
303ba 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
303bb 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 74 65  (argv[0]);..  te
303bc 73 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62  stcase( nPat==db
303bd 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
303be 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45  LIMIT_LIKE_PATTE
303bf 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 3b 0d 0a 20  RN_LENGTH] );.. 
303c0 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 74 3d   testcase( nPat=
303c1 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
303c2 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41  TE_LIMIT_LIKE_PA
303c3 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20  TTERN_LENGTH]+1 
303c4 29 3b 0d 0a 20 20 69 66 28 20 6e 50 61 74 20 3e  );..  if( nPat >
303c5 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
303c6 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41  TE_LIMIT_LIKE_PA
303c7 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 7b  TTERN_LENGTH] ){
303c8 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
303c9 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
303ca 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c 4f  xt, "LIKE or GLO
303cb 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63 6f  B pattern too co
303cc 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0d 0a 20 20  mplex", -1);..  
303cd 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
303ce 20 20 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71    assert( zB==sq
303cf 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
303d0 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a  (argv[0]) );  /*
303d1 20 45 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f   Encoding did no
303d2 74 20 63 68 61 6e 67 65 20 2a 2f 0d 0a 0d 0a 20  t change */.... 
303d3 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0d   if( argc==3 ){.
303d4 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61  .    /* The esca
303d5 70 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72  pe character str
303d6 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 74  ing must consist
303d7 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 46   of a single UTF
303d8 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 20  -8 character... 
303d9 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
303da 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
303db 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  ...    */..    c
303dc 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
303dd 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74  ar *zEsc = sqlit
303de 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
303df 67 76 5b 32 5d 29 3b 0d 0a 20 20 20 20 69 66 28  gv[2]);..    if(
303e0 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72   zEsc==0 ) retur
303e1 6e 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69  n;..    if( sqli
303e2 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28  te3Utf8CharLen((
303e3 63 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21  char*)zEsc, -1)!
303e4 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =1 ){..      sql
303e5 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
303e6 72 28 63 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 20  r(context, ..   
303e7 20 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65         "ESCAPE e
303e8 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62  xpression must b
303e9 65 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61  e a single chara
303ea 63 74 65 72 22 2c 20 2d 31 29 3b 0d 0a 20 20 20  cter", -1);..   
303eb 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20     return;..    
303ec 7d 0d 0a 20 20 20 20 65 73 63 61 70 65 20 3d 20  }..    escape = 
303ed 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
303ee 7a 45 73 63 2c 20 26 7a 45 73 63 29 3b 0d 0a 20  zEsc, &zEsc);.. 
303ef 20 7d 0d 0a 20 20 69 66 28 20 7a 41 20 26 26 20   }..  if( zA && 
303f0 7a 42 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63  zB ){..    struc
303f1 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70  t compareInfo *p
303f2 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75  Info = sqlite3_u
303f3 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
303f4 29 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
303f5 45 5f 54 45 53 54 0d 0a 20 20 20 20 73 71 6c 69  E_TEST..    sqli
303f6 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b  te3_like_count++
303f7 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 0d  ;..#endif..    .
303f8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
303f9 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c  ult_int(context,
303fa 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
303fb 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f 2c 20 65  zB, zA, pInfo, e
303fc 73 63 61 70 65 29 29 3b 0d 0a 20 20 7d 0d 0a 7d  scape));..  }..}
303fd 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65  ..../*..** Imple
303fe 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
303ff 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e   NULLIF(x,y) fun
30400 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75  ction.  The resu
30401 6c 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0d  lt is the first.
30402 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20  .** argument if 
30403 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72  the arguments ar
30404 65 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68  e different.  Th
30405 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
30406 20 69 66 20 74 68 65 0d 0a 2a 2a 20 61 72 67 75   if the..** argu
30407 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20  ments are equal 
30408 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e 0d 0a  to each other...
30409 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
3040a 6e 75 6c 6c 69 66 46 75 6e 63 28 0d 0a 20 20 73  nullifFunc(..  s
3040b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
3040c 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20  context,..  int 
3040d 4e 6f 74 55 73 65 64 2c 0d 0a 20 20 73 71 6c 69  NotUsed,..  sqli
3040e 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
3040f 0d 0a 29 7b 0d 0a 20 20 43 6f 6c 6c 53 65 71 20  ..){..  CollSeq 
30410 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  *pColl = sqlite3
30411 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63  GetFuncCollSeq(c
30412 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 55 4e 55 53  ontext);..  UNUS
30413 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
30414 55 73 65 64 29 3b 0d 0a 20 20 69 66 28 20 73 71  Used);..  if( sq
30415 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
30416 61 72 67 76 5b 30 5d 2c 20 61 72 67 76 5b 31 5d  argv[0], argv[1]
30417 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29 7b 0d 0a  , pColl)!=0 ){..
30418 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
30419 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
3041a 2c 20 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 7d  , argv[0]);..  }
3041b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d  ..}..../*..** Im
3041c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3041d 74 68 65 20 73 71 6c 69 74 65 5f 76 65 72 73 69  the sqlite_versi
3041e 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  on() function.  
3041f 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
30420 65 20 76 65 72 73 69 6f 6e 0d 0a 2a 2a 20 6f 66  e version..** of
30421 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
30422 61 72 79 20 74 68 61 74 20 69 73 20 72 75 6e 6e  ary that is runn
30423 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ing...*/..static
30424 20 76 6f 69 64 20 76 65 72 73 69 6f 6e 46 75 6e   void versionFun
30425 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  c(..  sqlite3_co
30426 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d  ntext *context,.
30427 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d  .  int NotUsed,.
30428 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
30429 20 2a 2a 4e 6f 74 55 73 65 64 32 0d 0a 29 7b 0d   **NotUsed2..){.
3042a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
3042b 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
3042c 74 55 73 65 64 32 29 3b 0d 0a 20 20 2f 2a 20 49  tUsed2);..  /* I
3042d 4d 50 3a 20 52 2d 34 38 36 39 39 2d 34 38 36 31  MP: R-48699-4861
3042e 37 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  7 This function 
3042f 69 73 20 61 6e 20 53 51 4c 20 77 72 61 70 70 65  is an SQL wrappe
30430 72 20 61 72 6f 75 6e 64 20 74 68 65 0d 0a 20 20  r around the..  
30431 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ** sqlite3_libve
30432 72 73 69 6f 6e 28 29 20 43 2d 69 6e 74 65 72 66  rsion() C-interf
30433 61 63 65 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ace. */..  sqlit
30434 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
30435 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
30436 6c 69 62 76 65 72 73 69 6f 6e 28 29 2c 20 2d 31  libversion(), -1
30437 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
30438 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
30439 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3043a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 6f 75 72   the sqlite_sour
3043b 63 65 5f 69 64 28 29 20 66 75 6e 63 74 69 6f 6e  ce_id() function
3043c 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  . The result is 
3043d 61 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 74 68 61  a string..** tha
3043e 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  t identifies the
3043f 20 70 61 72 74 69 63 75 6c 61 72 20 76 65 72 73   particular vers
30440 69 6f 6e 20 6f 66 20 74 68 65 20 73 6f 75 72 63  ion of the sourc
30441 65 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62  e code used to b
30442 75 69 6c 64 0d 0a 2a 2a 20 53 51 4c 69 74 65 2e  uild..** SQLite.
30443 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
30444 64 20 73 6f 75 72 63 65 69 64 46 75 6e 63 28 0d  d sourceidFunc(.
30445 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
30446 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20  xt *context,..  
30447 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0d 0a 20 20  int NotUsed,..  
30448 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
30449 4e 6f 74 55 73 65 64 32 0d 0a 29 7b 0d 0a 20 20  NotUsed2..){..  
3044a 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
3044b 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
3044c 65 64 32 29 3b 0d 0a 20 20 2f 2a 20 49 4d 50 3a  ed2);..  /* IMP:
3044d 20 52 2d 32 34 34 37 30 2d 33 31 31 33 36 20 54   R-24470-31136 T
3044e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
3044f 61 6e 20 53 51 4c 20 77 72 61 70 70 65 72 20 61  an SQL wrapper a
30450 72 6f 75 6e 64 20 74 68 65 0d 0a 20 20 2a 2a 20  round the..  ** 
30451 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64  sqlite3_sourceid
30452 28 29 20 43 20 69 6e 74 65 72 66 61 63 65 2e 20  () C interface. 
30453 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  */..  sqlite3_re
30454 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
30455 74 2c 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63  t, sqlite3_sourc
30456 65 69 64 28 29 2c 20 2d 31 2c 20 53 51 4c 49 54  eid(), -1, SQLIT
30457 45 5f 53 54 41 54 49 43 29 3b 0d 0a 7d 0d 0a 0d  E_STATIC);..}...
30458 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
30459 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
3045a 6c 69 74 65 5f 6c 6f 67 28 29 20 66 75 6e 63 74  lite_log() funct
3045b 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 61 20  ion.  This is a 
3045c 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0d 0a  wrapper around..
3045d 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29  ** sqlite3_log()
3045e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
3045f 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 20 20 54 68  lue is NULL.  Th
30460 65 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74  e function exist
30461 73 20 70 75 72 65 6c 79 20 66 6f 72 0d 0a 2a 2a  s purely for..**
30462 20 69 74 73 20 73 69 64 65 2d 65 66 66 65 63 74   its side-effect
30463 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  s...*/..static v
30464 6f 69 64 20 65 72 72 6c 6f 67 46 75 6e 63 28 0d  oid errlogFunc(.
30465 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
30466 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20  xt *context,..  
30467 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c  int argc,..  sql
30468 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
30469 76 0d 0a 29 7b 0d 0a 20 20 55 4e 55 53 45 44 5f  v..){..  UNUSED_
3046a 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
3046b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
3046c 45 54 45 52 28 63 6f 6e 74 65 78 74 29 3b 0d 0a  ETER(context);..
3046d 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 73 71    sqlite3_log(sq
3046e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
3046f 61 72 67 76 5b 30 5d 29 2c 20 22 25 73 22 2c 20  argv[0]), "%s", 
30470 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
30471 78 74 28 61 72 67 76 5b 31 5d 29 29 3b 0d 0a 7d  xt(argv[1]));..}
30472 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65  ..../*..** Imple
30473 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
30474 20 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f   sqlite_compileo
30475 70 74 69 6f 6e 5f 75 73 65 64 28 29 20 66 75 6e  ption_used() fun
30476 63 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 65 20 72  ction...** The r
30477 65 73 75 6c 74 20 69 73 20 61 6e 20 69 6e 74 65  esult is an inte
30478 67 65 72 20 74 68 61 74 20 69 64 65 6e 74 69 66  ger that identif
30479 69 65 73 20 69 66 20 74 68 65 20 63 6f 6d 70 69  ies if the compi
3047a 6c 65 72 20 6f 70 74 69 6f 6e 0d 0a 2a 2a 20 77  ler option..** w
3047b 61 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  as used to build
3047c 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 23 69   SQLite...*/..#i
3047d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3047e 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f  T_COMPILEOPTION_
3047f 44 49 41 47 53 0d 0a 73 74 61 74 69 63 20 76 6f  DIAGS..static vo
30480 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e  id compileoption
30481 75 73 65 64 46 75 6e 63 28 0d 0a 20 20 73 71 6c  usedFunc(..  sql
30482 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
30483 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61 72  ntext,..  int ar
30484 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  gc,..  sqlite3_v
30485 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d  alue **argv..){.
30486 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
30487 4f 70 74 4e 61 6d 65 3b 0d 0a 20 20 61 73 73 65  OptName;..  asse
30488 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0d 0a  rt( argc==1 );..
30489 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
3048a 45 52 28 61 72 67 63 29 3b 0d 0a 20 20 2f 2a 20  ER(argc);..  /* 
3048b 49 4d 50 3a 20 52 2d 33 39 35 36 34 2d 33 36 33  IMP: R-39564-363
3048c 30 35 20 54 68 65 20 73 71 6c 69 74 65 5f 63 6f  05 The sqlite_co
3048d 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64  mpileoption_used
3048e 28 29 20 53 51 4c 0d 0a 20 20 2a 2a 20 66 75 6e  () SQL..  ** fun
3048f 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70 70  ction is a wrapp
30490 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71  er around the sq
30491 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74  lite3_compileopt
30492 69 6f 6e 5f 75 73 65 64 28 29 20 43 2f 43 2b 2b  ion_used() C/C++
30493 0d 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e  ..  ** function.
30494 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 7a  ..  */..  if( (z
30495 4f 70 74 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74  OptName = (const
30496 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
30497 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
30498 5d 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 73  ]))!=0 ){..    s
30499 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
3049a 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
3049b 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e  e3_compileoption
3049c 5f 75 73 65 64 28 7a 4f 70 74 4e 61 6d 65 29 29  _used(zOptName))
3049d 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69  ;..  }..}..#endi
3049e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
3049f 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44  _COMPILEOPTION_D
304a0 49 41 47 53 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  IAGS */..../*..*
304a1 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
304a2 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 63   of the sqlite_c
304a3 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74  ompileoption_get
304a4 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 0d 0a 2a  () function. ..*
304a5 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  * The result is 
304a6 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 64  a string that id
304a7 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f 6d  entifies the com
304a8 70 69 6c 65 72 20 6f 70 74 69 6f 6e 73 20 0d 0a  piler options ..
304a9 2a 2a 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  ** used to build
304aa 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 23 69   SQLite...*/..#i
304ab 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
304ac 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f  T_COMPILEOPTION_
304ad 44 49 41 47 53 0d 0a 73 74 61 74 69 63 20 76 6f  DIAGS..static vo
304ae 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e  id compileoption
304af 67 65 74 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69  getFunc(..  sqli
304b0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
304b1 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67  text,..  int arg
304b2 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  c,..  sqlite3_va
304b3 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a  lue **argv..){..
304b4 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 61 73 73 65    int n;..  asse
304b5 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0d 0a  rt( argc==1 );..
304b6 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
304b7 45 52 28 61 72 67 63 29 3b 0d 0a 20 20 2f 2a 20  ER(argc);..  /* 
304b8 49 4d 50 3a 20 52 2d 30 34 39 32 32 2d 32 34 30  IMP: R-04922-240
304b9 37 36 20 54 68 65 20 73 71 6c 69 74 65 5f 63 6f  76 The sqlite_co
304ba 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28  mpileoption_get(
304bb 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0d 0a  ) SQL function..
304bc 20 20 2a 2a 20 69 73 20 61 20 77 72 61 70 70 65    ** is a wrappe
304bd 72 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71 6c  r around the sql
304be 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69  ite3_compileopti
304bf 6f 6e 5f 67 65 74 28 29 20 43 2f 43 2b 2b 20 66  on_get() C/C++ f
304c0 75 6e 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a  unction...  */..
304c1 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
304c2 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29  lue_int(argv[0])
304c3 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
304c4 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
304c5 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c  , sqlite3_compil
304c6 65 6f 70 74 69 6f 6e 5f 67 65 74 28 6e 29 2c 20  eoption_get(n), 
304c7 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
304c8 43 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f  C);..}..#endif /
304c9 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
304ca 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47  MPILEOPTION_DIAG
304cb 53 20 2a 2f 0d 0a 0d 0a 2f 2a 20 41 72 72 61 79  S */..../* Array
304cc 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20   for converting 
304cd 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20  from half-bytes 
304ce 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41  (nybbles) into A
304cf 53 43 49 49 20 68 65 78 0d 0a 2a 2a 20 64 69 67  SCII hex..** dig
304d0 69 74 73 2e 20 2a 2f 0d 0a 73 74 61 74 69 63 20  its. */..static 
304d1 63 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64 69  const char hexdi
304d2 67 69 74 73 5b 5d 20 3d 20 7b 0d 0a 20 20 27 30  gits[] = {..  '0
304d3 27 2c 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27  ', '1', '2', '3'
304d4 2c 20 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c  , '4', '5', '6',
304d5 20 27 37 27 2c 0d 0a 20 20 27 38 27 2c 20 27 39   '7',..  '8', '9
304d6 27 2c 20 27 41 27 2c 20 27 42 27 2c 20 27 43 27  ', 'A', 'B', 'C'
304d7 2c 20 27 44 27 2c 20 27 45 27 2c 20 27 46 27 20  , 'D', 'E', 'F' 
304d8 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45  ..};..../*..** E
304d9 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68  XPERIMENTAL - Th
304da 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66  is is not an off
304db 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20  icial function. 
304dc 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
304dd 61 79 0d 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20  ay..** change.  
304de 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
304df 79 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f  y disappear.  Do
304e0 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20   not write code 
304e1 74 68 61 74 20 64 65 70 65 6e 64 73 0d 0a 2a 2a  that depends..**
304e2 20 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f   on this functio
304e3 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6d 70 6c 65  n...**..** Imple
304e4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
304e5 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74 69 6f   QUOTE() functio
304e6 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f  n.  This functio
304e7 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
304e8 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20  ..** argument.  
304e9 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
304ea 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 20  is numeric, the 
304eb 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
304ec 74 68 65 20 73 61 6d 65 20 61 73 0d 0a 2a 2a 20  the same as..** 
304ed 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49  the argument.  I
304ee 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
304ef 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75  s NULL, the retu
304f0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
304f1 73 74 72 69 6e 67 0d 0a 2a 2a 20 22 4e 55 4c 4c  string..** "NULL
304f2 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74  ".  Otherwise, t
304f3 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 65  he argument is e
304f4 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c  nclosed in singl
304f5 65 20 71 75 6f 74 65 73 20 77 69 74 68 0d 0a 2a  e quotes with..*
304f6 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65  * single-quote e
304f7 73 63 61 70 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  scapes...*/..sta
304f8 74 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75  tic void quoteFu
304f9 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
304fa 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
304fb 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
304fc 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0d 0a 20  alue **argv){.. 
304fd 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
304fe 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41   );..  UNUSED_PA
304ff 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0d 0a  RAMETER(argc);..
30500 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65    switch( sqlite
30501 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
30502 76 5b 30 5d 29 20 29 7b 0d 0a 20 20 20 20 63 61  v[0]) ){..    ca
30503 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
30504 52 3a 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c  R:..    case SQL
30505 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0d 0a 20 20  ITE_FLOAT: {..  
30506 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
30507 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
30508 2c 20 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20  , argv[0]);..   
30509 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
3050a 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
3050b 45 5f 42 4c 4f 42 3a 20 7b 0d 0a 20 20 20 20 20  E_BLOB: {..     
3050c 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30   char *zText = 0
3050d 3b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f  ;..      char co
3050e 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c  nst *zBlob = sql
3050f 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
30510 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20 20  argv[0]);..     
30511 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c   int nBlob = sql
30512 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
30513 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20  (argv[0]);..    
30514 20 20 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d    assert( zBlob=
30515 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  =sqlite3_value_b
30516 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20  lob(argv[0]) ); 
30517 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63  /* No encoding c
30518 68 61 6e 67 65 20 2a 2f 0d 0a 20 20 20 20 20 20  hange */..      
30519 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20 2a 29  zText = (char *)
3051a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
3051b 6e 74 65 78 74 2c 20 28 32 2a 28 69 36 34 29 6e  ntext, (2*(i64)n
3051c 42 6c 6f 62 29 2b 34 29 3b 20 0d 0a 20 20 20 20  Blob)+4); ..    
3051d 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0d 0a    if( zText ){..
3051e 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a          int i;..
3051f 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
30520 20 69 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0d   i<nBlob; i++){.
30521 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74  .          zText
30522 5b 28 69 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64  [(i*2)+2] = hexd
30523 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e  igits[(zBlob[i]>
30524 3e 34 29 26 30 78 30 46 5d 3b 0d 0a 20 20 20 20  >4)&0x0F];..    
30525 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32        zText[(i*2
30526 29 2b 33 5d 20 3d 20 68 65 78 64 69 67 69 74 73  )+3] = hexdigits
30527 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30 78 30 46  [(zBlob[i])&0x0F
30528 5d 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  ];..        }.. 
30529 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42         zText[(nB
3052a 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27 27  lob*2)+2] = '\''
3052b 3b 0d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ;..        zText
3052c 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20  [(nBlob*2)+3] = 
3052d 27 5c 30 27 3b 0d 0a 20 20 20 20 20 20 20 20 7a  '\0';..        z
3052e 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0d 0a  Text[0] = 'X';..
3052f 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d          zText[1]
30530 20 3d 20 27 5c 27 27 3b 0d 0a 20 20 20 20 20 20   = '\'';..      
30531 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
30532 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
30533 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Text, -1, SQLITE
30534 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20  _TRANSIENT);..  
30535 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
30536 65 65 28 7a 54 65 78 74 29 3b 0d 0a 20 20 20 20  ee(zText);..    
30537 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b    }..      break
30538 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
30539 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20  se SQLITE_TEXT: 
3053a 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 6a  {..      int i,j
3053b 3b 0d 0a 20 20 20 20 20 20 75 36 34 20 6e 3b 0d  ;..      u64 n;.
3053c 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  .      const uns
3053d 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67  igned char *zArg
3053e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3053f 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0d  _text(argv[0]);.
30540 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0d  .      char *z;.
30541 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 41 72  ...      if( zAr
30542 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a  g==0 ) return;..
30543 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6e        for(i=0, n
30544 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b  =0; zArg[i]; i++
30545 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d  ){ if( zArg[i]==
30546 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0d 0a 20  '\'' ) n++; }.. 
30547 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74       z = context
30548 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
30549 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 29 6e  ((i64)i)+((i64)n
3054a 29 2b 33 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  )+3);..      if(
3054b 20 7a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 7a   z ){..        z
3054c 5b 30 5d 20 3d 20 27 5c 27 27 3b 0d 0a 20 20 20  [0] = '\'';..   
3054d 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d       for(i=0, j=
3054e 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29  1; zArg[i]; i++)
3054f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a  {..          z[j
30550 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0d 0a  ++] = zArg[i];..
30551 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 41            if( zA
30552 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0d 0a  rg[i]=='\'' ){..
30553 20 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b              z[j+
30554 2b 5d 20 3d 20 27 5c 27 27 3b 0d 0a 20 20 20 20  +] = '\'';..    
30555 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
30556 20 7d 0d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b   }..        z[j+
30557 2b 5d 20 3d 20 27 5c 27 27 3b 0d 0a 20 20 20 20  +] = '\'';..    
30558 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0d 0a 20      z[j] = 0;.. 
30559 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
3055a 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
3055b 78 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65  xt, z, j, sqlite
3055c 33 5f 66 72 65 65 29 3b 0d 0a 20 20 20 20 20 20  3_free);..      
3055d 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  }..      break;.
3055e 0a 20 20 20 20 7d 0d 0a 20 20 20 20 64 65 66 61  .    }..    defa
3055f 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20 61 73  ult: {..      as
30560 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
30561 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
30562 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
30563 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
30564 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
30565 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c  text, "NULL", 4,
30566 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
30567 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
30568 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d      }..  }..}...
30569 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 68 65 78 28  ./*..** The hex(
3056a 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74  ) function.  Int
3056b 65 72 70 72 65 74 20 74 68 65 20 61 72 67 75 6d  erpret the argum
3056c 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20  ent as a blob.  
3056d 52 65 74 75 72 6e 0d 0a 2a 2a 20 61 20 68 65 78  Return..** a hex
3056e 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 69  adecimal renderi
3056f 6e 67 20 61 73 20 74 65 78 74 2e 0d 0a 2a 2f 0d  ng as text...*/.
30570 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78  .static void hex
30571 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33  Func(..  sqlite3
30572 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
30573 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d  t,..  int argc,.
30574 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
30575 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 69   **argv..){..  i
30576 6e 74 20 69 2c 20 6e 3b 0d 0a 20 20 63 6f 6e 73  nt i, n;..  cons
30577 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
30578 2a 70 42 6c 6f 62 3b 0d 0a 20 20 63 68 61 72 20  *pBlob;..  char 
30579 2a 7a 48 65 78 2c 20 2a 7a 3b 0d 0a 20 20 61 73  *zHex, *z;..  as
3057a 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
3057b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
3057c 45 54 45 52 28 61 72 67 63 29 3b 0d 0a 20 20 70  ETER(argc);..  p
3057d 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
3057e 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
3057f 5d 29 3b 0d 0a 20 20 6e 20 3d 20 73 71 6c 69 74  ]);..  n = sqlit
30580 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
30581 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 61 73 73 65  rgv[0]);..  asse
30582 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74  rt( pBlob==sqlit
30583 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
30584 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f  gv[0]) );  /* No
30585 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
30586 20 2a 2f 0d 0a 20 20 7a 20 3d 20 7a 48 65 78 20   */..  z = zHex 
30587 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  = contextMalloc(
30588 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e  context, ((i64)n
30589 29 2a 32 20 2b 20 31 29 3b 0d 0a 20 20 69 66 28  )*2 + 1);..  if(
3058a 20 7a 48 65 78 20 29 7b 0d 0a 20 20 20 20 66 6f   zHex ){..    fo
3058b 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c  r(i=0; i<n; i++,
3058c 20 70 42 6c 6f 62 2b 2b 29 7b 0d 0a 20 20 20 20   pBlob++){..    
3058d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
3058e 63 20 3d 20 2a 70 42 6c 6f 62 3b 0d 0a 20 20 20  c = *pBlob;..   
3058f 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64     *(z++) = hexd
30590 69 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78 66  igits[(c>>4)&0xf
30591 5d 3b 0d 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29  ];..      *(z++)
30592 20 3d 20 68 65 78 64 69 67 69 74 73 5b 63 26 30   = hexdigits[c&0
30593 78 66 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  xf];..    }..   
30594 20 2a 7a 20 3d 20 30 3b 0d 0a 20 20 20 20 73 71   *z = 0;..    sq
30595 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
30596 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c  t(context, zHex,
30597 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72   n*2, sqlite3_fr
30598 65 65 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  ee);..  }..}....
30599 2f 2a 0d 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62  /*..** The zerob
3059a 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20  lob(N) function 
3059b 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66  returns a zero-f
3059c 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69  illed blob of si
3059d 7a 65 20 4e 20 62 79 74 65 73 2e 0d 0a 2a 2f 0d  ze N bytes...*/.
3059e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72  .static void zer
3059f 6f 62 6c 6f 62 46 75 6e 63 28 0d 0a 20 20 73 71  oblobFunc(..  sq
305a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
305a1 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61  ontext,..  int a
305a2 72 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  rgc,..  sqlite3_
305a3 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b  value **argv..){
305a4 0d 0a 20 20 69 36 34 20 6e 3b 0d 0a 20 20 73 71  ..  i64 n;..  sq
305a5 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
305a6 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
305a7 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0d  andle(context);.
305a8 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
305a9 3d 31 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f  =1 );..  UNUSED_
305aa 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
305ab 0d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ..  n = sqlite3_
305ac 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76  value_int64(argv
305ad 5b 30 5d 29 3b 0d 0a 20 20 74 65 73 74 63 61 73  [0]);..  testcas
305ae 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  e( n==db->aLimit
305af 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
305b0 4e 47 54 48 5d 20 29 3b 0d 0a 20 20 74 65 73 74  NGTH] );..  test
305b1 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69  case( n==db->aLi
305b2 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
305b3 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0d 0a 20  _LENGTH]+1 );.. 
305b4 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69 6d 69   if( n>db->aLimi
305b5 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
305b6 45 4e 47 54 48 5d 20 29 7b 0d 0a 20 20 20 20 73  ENGTH] ){..    s
305b7 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
305b8 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65  ror_toobig(conte
305b9 78 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  xt);..  }else{..
305ba 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
305bb 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74  lt_zeroblob(cont
305bc 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 20 2f 2a  ext, (int)n); /*
305bd 20 49 4d 50 3a 20 52 2d 30 30 32 39 33 2d 36 34   IMP: R-00293-64
305be 39 39 34 20 2a 2f 0d 0a 20 20 7d 0d 0a 7d 0d 0a  994 */..  }..}..
305bf 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 70  ../*..** The rep
305c0 6c 61 63 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e  lace() function.
305c1 20 20 54 68 72 65 65 20 61 72 67 75 6d 65 6e 74    Three argument
305c2 73 20 61 72 65 20 61 6c 6c 20 73 74 72 69 6e 67  s are all string
305c3 73 3a 20 63 61 6c 6c 0d 0a 2a 2a 20 74 68 65 6d  s: call..** them
305c4 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20 54 68   A, B, and C. Th
305c5 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f  e result is also
305c6 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20   a string which 
305c7 69 73 20 64 65 72 69 76 65 64 0d 0a 2a 2a 20 66  is derived..** f
305c8 72 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63 69  rom A by replaci
305c9 6e 67 20 65 76 65 72 79 20 6f 63 63 75 72 61 6e  ng every occuran
305ca 63 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e 20  ce of B with C. 
305cb 20 54 68 65 20 6d 61 74 63 68 0d 0a 2a 2a 20 6d   The match..** m
305cc 75 73 74 20 62 65 20 65 78 61 63 74 2e 20 20 43  ust be exact.  C
305cd 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
305ce 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e  es are not used.
305cf 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
305d0 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0d 0a  d replaceFunc(..
305d1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
305d2 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69  t *context,..  i
305d3 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69  nt argc,..  sqli
305d4 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
305d5 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 75 6e  ..){..  const un
305d6 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74  signed char *zSt
305d7 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
305d8 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20   input string A 
305d9 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  */..  const unsi
305da 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74  gned char *zPatt
305db 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70  ern;    /* The p
305dc 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20  attern string B 
305dd 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  */..  const unsi
305de 67 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b  gned char *zRep;
305df 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
305e0 65 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e  eplacement strin
305e1 67 20 43 20 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e  g C */..  unsign
305e2 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20  ed char *zOut;  
305e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
305e4 68 65 20 6f 75 74 70 75 74 20 2a 2f 0d 0a 20 20  he output */..  
305e5 69 6e 74 20 6e 53 74 72 3b 20 20 20 20 20 20 20  int nStr;       
305e6 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
305e7 20 6f 66 20 7a 53 74 72 20 2a 2f 0d 0a 20 20 69   of zStr */..  i
305e8 6e 74 20 6e 50 61 74 74 65 72 6e 3b 20 20 20 20  nt nPattern;    
305e9 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
305ea 6f 66 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0d 0a  of zPattern */..
305eb 20 20 69 6e 74 20 6e 52 65 70 3b 20 20 20 20 20    int nRep;     
305ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
305ed 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f 0d 0a 20  ze of zRep */.. 
305ee 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20 20 20   i64 nOut;      
305ef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
305f0 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a 4f 75  imum size of zOu
305f1 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c 6f 6f 70  t */..  int loop
305f2 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  Limit;          
305f3 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d 20   /* Last zStr[] 
305f4 74 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63 68  that might match
305f5 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0d 0a   zPattern[] */..
305f6 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
305f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
305f8 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d 0a  op counters */..
305f9 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
305fa 3d 3d 33 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44  ==3 );..  UNUSED
305fb 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
305fc 3b 0d 0a 20 20 7a 53 74 72 20 3d 20 73 71 6c 69  ;..  zStr = sqli
305fd 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
305fe 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 69 66 28 20  rgv[0]);..  if( 
305ff 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  zStr==0 ) return
30600 3b 0d 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c 69  ;..  nStr = sqli
30601 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
30602 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 61 73 73  argv[0]);..  ass
30603 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74  ert( zStr==sqlit
30604 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
30605 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f  gv[0]) );  /* No
30606 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
30607 20 2a 2f 0d 0a 20 20 7a 50 61 74 74 65 72 6e 20   */..  zPattern 
30608 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
30609 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0d 0a  text(argv[1]);..
3060a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d    if( zPattern==
3060b 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  0 ){..    assert
3060c 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
3060d 74 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53  type(argv[1])==S
3060e 51 4c 49 54 45 5f 4e 55 4c 4c 0d 0a 20 20 20 20  QLITE_NULL..    
3060f 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
30610 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
30611 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d  ndle(context)->m
30612 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a  allocFailed );..
30613 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
30614 0d 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e  ..  if( zPattern
30615 5b 30 5d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61  [0]==0 ){..    a
30616 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76  ssert( sqlite3_v
30617 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
30618 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
30619 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
3061a 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
3061b 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0d  text, argv[0]);.
3061c 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20  .    return;..  
3061d 7d 0d 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d 20  }..  nPattern = 
3061e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
3061f 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20  tes(argv[1]);.. 
30620 20 61 73 73 65 72 74 28 20 7a 50 61 74 74 65 72   assert( zPatter
30621 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
30622 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 20 29  _text(argv[1]) )
30623 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
30624 67 20 63 68 61 6e 67 65 20 2a 2f 0d 0a 20 20 7a  g change */..  z
30625 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Rep = sqlite3_va
30626 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d  lue_text(argv[2]
30627 29 3b 0d 0a 20 20 69 66 28 20 7a 52 65 70 3d 3d  );..  if( zRep==
30628 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 6e  0 ) return;..  n
30629 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Rep = sqlite3_va
3062a 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32  lue_bytes(argv[2
3062b 5d 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 7a  ]);..  assert( z
3062c 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  Rep==sqlite3_val
3062d 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
3062e 20 29 3b 0d 0a 20 20 6e 4f 75 74 20 3d 20 6e 53   );..  nOut = nS
3062f 74 72 20 2b 20 31 3b 0d 0a 20 20 61 73 73 65 72  tr + 1;..  asser
30630 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d  t( nOut<SQLITE_M
30631 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0d 0a 20 20  AX_LENGTH );..  
30632 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61  zOut = contextMa
30633 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 69  lloc(context, (i
30634 36 34 29 6e 4f 75 74 29 3b 0d 0a 20 20 69 66 28  64)nOut);..  if(
30635 20 7a 4f 75 74 3d 3d 30 20 29 7b 0d 0a 20 20 20   zOut==0 ){..   
30636 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
30637 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74   loopLimit = nSt
30638 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0d  r - nPattern;  .
30639 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c  .  for(i=j=0; i<
3063a 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29  =loopLimit; i++)
3063b 7b 0d 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b  {..    if( zStr[
3063c 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20  i]!=zPattern[0] 
3063d 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b  || memcmp(&zStr[
3063e 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50  i], zPattern, nP
3063f 61 74 74 65 72 6e 29 20 29 7b 0d 0a 20 20 20 20  attern) ){..    
30640 20 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53    zOut[j++] = zS
30641 74 72 5b 69 5d 3b 0d 0a 20 20 20 20 7d 65 6c 73  tr[i];..    }els
30642 65 7b 0d 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f  e{..      u8 *zO
30643 6c 64 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  ld;..      sqlit
30644 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
30645 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
30646 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  le(context);..  
30647 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 70      nOut += nRep
30648 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0d 0a 20 20   - nPattern;..  
30649 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 4f      testcase( nO
3064a 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  ut-1==db->aLimit
3064b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
3064c 4e 47 54 48 5d 20 29 3b 0d 0a 20 20 20 20 20 20  NGTH] );..      
3064d 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 32  testcase( nOut-2
3064e 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
3064f 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
30650 5d 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  ] );..      if( 
30651 6e 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69  nOut-1>db->aLimi
30652 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
30653 45 4e 47 54 48 5d 20 29 7b 0d 0a 20 20 20 20 20  ENGTH] ){..     
30654 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
30655 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
30656 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20  ontext);..      
30657 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
30658 4f 75 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  Out);..        r
30659 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d  eturn;..      }.
3065a 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f  .      zOld = zO
3065b 75 74 3b 0d 0a 20 20 20 20 20 20 7a 4f 75 74 20  ut;..      zOut 
3065c 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
3065d 63 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f 75  c(zOut, (int)nOu
3065e 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a  t);..      if( z
3065f 4f 75 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Out==0 ){..     
30660 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
30661 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
30662 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 20 20 20  ntext);..       
30663 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f   sqlite3_free(zO
30664 6c 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 65  ld);..        re
30665 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  turn;..      }..
30666 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f        memcpy(&zO
30667 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65  ut[j], zRep, nRe
30668 70 29 3b 0d 0a 20 20 20 20 20 20 6a 20 2b 3d 20  p);..      j += 
30669 6e 52 65 70 3b 0d 0a 20 20 20 20 20 20 69 20 2b  nRep;..      i +
3066a 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0d 0a 20  = nPattern-1;.. 
3066b 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 61 73 73     }..  }..  ass
3066c 65 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d  ert( j+nStr-i+1=
3066d 3d 6e 4f 75 74 20 29 3b 0d 0a 20 20 6d 65 6d 63  =nOut );..  memc
3066e 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53  py(&zOut[j], &zS
3066f 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0d  tr[i], nStr-i);.
30670 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69  .  j += nStr - i
30671 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6a 3c 3d  ;..  assert( j<=
30672 6e 4f 75 74 20 29 3b 0d 0a 20 20 7a 4f 75 74 5b  nOut );..  zOut[
30673 6a 5d 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74  j] = 0;..  sqlit
30674 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
30675 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
30676 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  Out, j, sqlite3_
30677 66 72 65 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  free);..}..../*.
30678 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
30679 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d 28 29  on of the TRIM()
3067a 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64 20 52  , LTRIM(), and R
3067b 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73  TRIM() functions
3067c 2e 0d 0a 2a 2a 20 54 68 65 20 75 73 65 72 64 61  ...** The userda
3067d 74 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c 65  ta is 0x1 for le
3067e 66 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f 72  ft trim, 0x2 for
3067f 20 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78 33   right trim, 0x3
30680 20 66 6f 72 20 62 6f 74 68 2e 0d 0a 2a 2f 0d 0a   for both...*/..
30681 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d  static void trim
30682 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74 65 33  Func(..  sqlite3
30683 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
30684 74 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d  t,..  int argc,.
30685 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
30686 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 63   **argv..){..  c
30687 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
30688 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20  ar *zIn;        
30689 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
3068a 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73   */..  const uns
3068b 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 43 68 61  igned char *zCha
3068c 72 53 65 74 3b 20 20 20 20 2f 2a 20 53 65 74 20  rSet;    /* Set 
3068d 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f  of characters to
3068e 20 74 72 69 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20   trim */..  int 
3068f 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  nIn;            
30690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30691 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
30692 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0d 0a 20 20   in input */..  
30693 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
30694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30695 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74    /* 1: trimleft
30696 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20 20    2: trimright  
30697 33 3a 20 74 72 69 6d 20 2a 2f 0d 0a 20 20 69 6e  3: trim */..  in
30698 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
30699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3069a 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
3069b 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  */..  unsigned c
3069c 68 61 72 20 2a 61 4c 65 6e 20 3d 20 30 3b 20 20  har *aLen = 0;  
3069d 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
3069e 68 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63  h of each charac
3069f 74 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20  ter in zCharSet 
306a0 2a 2f 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  */..  unsigned c
306a1 68 61 72 20 2a 2a 61 7a 43 68 61 72 20 3d 20 30  har **azChar = 0
306a2 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76  ;       /* Indiv
306a3 69 64 75 61 6c 20 63 68 61 72 61 63 74 65 72 73  idual characters
306a4 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0d   in zCharSet */.
306a5 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20  .  int nChar;   
306a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306a7 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
306a8 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  f characters in 
306a9 7a 43 68 61 72 53 65 74 20 2a 2f 0d 0a 0d 0a 20  zCharSet */.... 
306aa 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
306ab 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
306ac 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b  ==SQLITE_NULL ){
306ad 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20  ..    return;.. 
306ae 20 7d 0d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c 69   }..  zIn = sqli
306af 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
306b0 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 69 66 28 20  rgv[0]);..  if( 
306b1 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  zIn==0 ) return;
306b2 0d 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65  ..  nIn = sqlite
306b3 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
306b4 67 76 5b 30 5d 29 3b 0d 0a 20 20 61 73 73 65 72  gv[0]);..  asser
306b5 74 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f  t( zIn==sqlite3_
306b6 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
306b7 30 5d 29 20 29 3b 0d 0a 20 20 69 66 28 20 61 72  0]) );..  if( ar
306b8 67 63 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 73 74  gc==1 ){..    st
306b9 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
306ba 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b  ned char lenOne[
306bb 5d 20 3d 20 7b 20 31 20 7d 3b 0d 0a 20 20 20 20  ] = { 1 };..    
306bc 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20  static unsigned 
306bd 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4f  char * const azO
306be 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22 20  ne[] = { (u8*)" 
306bf 22 20 7d 3b 0d 0a 20 20 20 20 6e 43 68 61 72 20  " };..    nChar 
306c0 3d 20 31 3b 0d 0a 20 20 20 20 61 4c 65 6e 20 3d  = 1;..    aLen =
306c1 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0d 0a 20   (u8*)lenOne;.. 
306c2 20 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73     azChar = (uns
306c3 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a  igned char **)az
306c4 4f 6e 65 3b 0d 0a 20 20 20 20 7a 43 68 61 72 53  One;..    zCharS
306c5 65 74 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65  et = 0;..  }else
306c6 20 69 66 28 20 28 7a 43 68 61 72 53 65 74 20 3d   if( (zCharSet =
306c7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
306c8 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30  ext(argv[1]))==0
306c9 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b   ){..    return;
306ca 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
306cb 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
306cc 68 61 72 20 2a 7a 3b 0d 0a 20 20 20 20 66 6f 72  har *z;..    for
306cd 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68  (z=zCharSet, nCh
306ce 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b  ar=0; *z; nChar+
306cf 2b 29 7b 0d 0a 20 20 20 20 20 20 53 51 4c 49 54  +){..      SQLIT
306d0 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0d  E_SKIP_UTF8(z);.
306d1 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
306d2 6e 43 68 61 72 3e 30 20 29 7b 0d 0a 20 20 20 20  nChar>0 ){..    
306d3 20 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65    azChar = conte
306d4 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
306d5 2c 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28  , ((i64)nChar)*(
306d6 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29  sizeof(char*)+1)
306d7 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 61 7a  );..      if( az
306d8 43 68 61 72 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Char==0 ){..    
306d9 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20      return;..   
306da 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 4c 65 6e     }..      aLen
306db 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
306dc 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61 72  r*)&azChar[nChar
306dd 5d 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d  ];..      for(z=
306de 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
306df 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
306e0 0d 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72  ..        azChar
306e1 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67  [nChar] = (unsig
306e2 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b 0d 0a 20  ned char *)z;.. 
306e3 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b         SQLITE_SK
306e4 49 50 5f 55 54 46 38 28 7a 29 3b 0d 0a 20 20 20  IP_UTF8(z);..   
306e5 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d       aLen[nChar]
306e6 20 3d 20 28 75 38 29 28 7a 20 2d 20 61 7a 43 68   = (u8)(z - azCh
306e7 61 72 5b 6e 43 68 61 72 5d 29 3b 0d 0a 20 20 20  ar[nChar]);..   
306e8 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
306e9 0d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20  ..  if( nChar>0 
306ea 29 7b 0d 0a 20 20 20 20 66 6c 61 67 73 20 3d 20  ){..    flags = 
306eb 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
306ec 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  T(sqlite3_user_d
306ed 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b 0d 0a  ata(context));..
306ee 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
306ef 31 20 29 7b 0d 0a 20 20 20 20 20 20 77 68 69 6c  1 ){..      whil
306f0 65 28 20 6e 49 6e 3e 30 20 29 7b 0d 0a 20 20 20  e( nIn>0 ){..   
306f1 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30       int len = 0
306f2 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
306f3 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b  =0; i<nChar; i++
306f4 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6c 65  ){..          le
306f5 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0d 0a 20 20  n = aLen[i];..  
306f6 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c          if( len<
306f7 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 7a  =nIn && memcmp(z
306f8 49 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c  In, azChar[i], l
306f9 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d  en)==0 ) break;.
306fa 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
306fb 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72      if( i>=nChar
306fc 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
306fd 20 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0d 0a     zIn += len;..
306fe 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c          nIn -= l
306ff 65 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  en;..      }..  
30700 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 66 6c 61    }..    if( fla
30701 67 73 20 26 20 32 20 29 7b 0d 0a 20 20 20 20 20  gs & 2 ){..     
30702 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b   while( nIn>0 ){
30703 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65  ..        int le
30704 6e 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  n = 0;..        
30705 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72  for(i=0; i<nChar
30706 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; i++){..       
30707 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d     len = aLen[i]
30708 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
30709 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d   len<=nIn && mem
3070a 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e  cmp(&zIn[nIn-len
3070b 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29  ],azChar[i],len)
3070c 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  ==0 ) break;..  
3070d 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
3070e 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20   if( i>=nChar ) 
3070f 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
30710 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0d 0a 20 20 20  nIn -= len;..   
30711 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
30712 20 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b   if( zCharSet ){
30713 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
30714 66 72 65 65 28 61 7a 43 68 61 72 29 3b 0d 0a 20  free(azChar);.. 
30715 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c     }..  }..  sql
30716 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
30717 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
30718 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c 49 54  )zIn, nIn, SQLIT
30719 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 7d  E_TRANSIENT);..}
3071a 0d 0a 0d 0a 0d 0a 2f 2a 20 49 4d 50 3a 20 52 2d  ....../* IMP: R-
3071b 32 35 33 36 31 2d 31 36 31 35 30 20 54 68 69 73  25361-16150 This
3071c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6d 69   function is omi
3071d 74 74 65 64 20 66 72 6f 6d 20 53 51 4c 69 74 65  tted from SQLite
3071e 20 62 79 20 64 65 66 61 75 6c 74 2e 20 49 74 0d   by default. It.
3071f 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  .** is only avai
30720 6c 61 62 6c 65 20 69 66 20 74 68 65 20 53 51 4c  lable if the SQL
30721 49 54 45 5f 53 4f 55 4e 44 45 58 20 63 6f 6d 70  ITE_SOUNDEX comp
30722 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20  ile-time option 
30723 69 73 20 75 73 65 64 0d 0a 2a 2a 20 77 68 65 6e  is used..** when
30724 20 53 51 4c 69 74 65 20 69 73 20 62 75 69 6c 74   SQLite is built
30725 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
30726 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0d 0a 2f 2a  LITE_SOUNDEX../*
30727 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  ..** Compute the
30728 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e   soundex encodin
30729 67 20 6f 66 20 61 20 77 6f 72 64 2e 0d 0a 2a 2a  g of a word...**
3072a 0d 0a 2a 2a 20 49 4d 50 3a 20 52 2d 35 39 37 38  ..** IMP: R-5978
3072b 32 2d 30 30 30 37 32 20 54 68 65 20 73 6f 75 6e  2-00072 The soun
3072c 64 65 78 28 58 29 20 66 75 6e 63 74 69 6f 6e 20  dex(X) function 
3072d 72 65 74 75 72 6e 73 20 61 20 73 74 72 69 6e 67  returns a string
3072e 20 74 68 61 74 20 69 73 20 74 68 65 0d 0a 2a 2a   that is the..**
3072f 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e   soundex encodin
30730 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20  g of the string 
30731 58 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  X. ..*/..static 
30732 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75 6e 63  void soundexFunc
30733 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  (..  sqlite3_con
30734 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d 0a  text *context,..
30735 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20 73    int argc,..  s
30736 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
30737 72 67 76 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20  rgv..){..  char 
30738 7a 52 65 73 75 6c 74 5b 38 5d 3b 0d 0a 20 20 63  zResult[8];..  c
30739 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0d 0a 20  onst u8 *zIn;.. 
3073a 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 73 74   int i, j;..  st
3073b 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
3073c 6e 65 64 20 63 68 61 72 20 69 43 6f 64 65 5b 5d  ned char iCode[]
3073d 20 3d 20 7b 0d 0a 20 20 20 20 30 2c 20 30 2c 20   = {..    0, 0, 
3073e 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
3073f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
30740 20 30 2c 20 30 2c 20 30 2c 0d 0a 20 20 20 20 30   0, 0, 0,..    0
30741 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
30742 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
30743 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0d 0a  0, 0, 0, 0, 0,..
30744 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
30745 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
30746 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
30747 20 30 2c 0d 0a 20 20 20 20 30 2c 20 30 2c 20 30   0,..    0, 0, 0
30748 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
30749 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3074a 30 2c 20 30 2c 20 30 2c 0d 0a 20 20 20 20 30 2c  0, 0, 0,..    0,
3074b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20   0, 1, 2, 3, 0, 
3074c 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32  1, 2, 0, 0, 2, 2
3074d 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0d 0a 20  , 4, 5, 5, 0,.. 
3074e 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33     1, 2, 6, 2, 3
3074f 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 1, 0, 2, 0,
30750 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   2, 0, 0, 0, 0, 
30751 30 2c 0d 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c  0,..    0, 0, 1,
30752 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20   2, 3, 0, 1, 2, 
30753 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35  0, 0, 2, 2, 4, 5
30754 2c 20 35 2c 20 30 2c 0d 0a 20 20 20 20 31 2c 20  , 5, 0,..    1, 
30755 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31  2, 6, 2, 3, 0, 1
30756 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 2, 0, 2, 0,
30757 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0d 0a 20 20   0, 0, 0, 0,..  
30758 7d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72  };..  assert( ar
30759 67 63 3d 3d 31 20 29 3b 0d 0a 20 20 7a 49 6e 20  gc==1 );..  zIn 
3075a 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76  = (u8*)sqlite3_v
3075b 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
3075c 5d 29 3b 0d 0a 20 20 69 66 28 20 7a 49 6e 3d 3d  ]);..  if( zIn==
3075d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22  0 ) zIn = (u8*)"
3075e 22 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a  ";..  for(i=0; z
3075f 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65  In[i] && !sqlite
30760 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29  3Isalpha(zIn[i])
30761 3b 20 69 2b 2b 29 7b 7d 0d 0a 20 20 69 66 28 20  ; i++){}..  if( 
30762 7a 49 6e 5b 69 5d 20 29 7b 0d 0a 20 20 20 20 75  zIn[i] ){..    u
30763 38 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f  8 prevcode = iCo
30764 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b  de[zIn[i]&0x7f];
30765 0d 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d  ..    zResult[0]
30766 20 3d 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65   = sqlite3Touppe
30767 72 28 7a 49 6e 5b 69 5d 29 3b 0d 0a 20 20 20 20  r(zIn[i]);..    
30768 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20  for(j=1; j<4 && 
30769 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0d 0a 20  zIn[i]; i++){.. 
3076a 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20       int code = 
3076b 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37  iCode[zIn[i]&0x7
3076c 66 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 63  f];..      if( c
3076d 6f 64 65 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  ode>0 ){..      
3076e 20 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65 76    if( code!=prev
3076f 63 6f 64 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  code ){..       
30770 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f     prevcode = co
30771 64 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a  de;..          z
30772 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f  Result[j++] = co
30773 64 65 20 2b 20 27 30 27 3b 0d 0a 20 20 20 20 20  de + '0';..     
30774 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73     }..      }els
30775 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 72 65 76  e{..        prev
30776 63 6f 64 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20  code = 0;..     
30777 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 77   }..    }..    w
30778 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0d 0a 20 20  hile( j<4 ){..  
30779 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d      zResult[j++]
3077a 20 3d 20 27 30 27 3b 0d 0a 20 20 20 20 7d 0d 0a   = '0';..    }..
3077b 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d      zResult[j] =
3077c 20 30 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   0;..    sqlite3
3077d 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
3077e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34  text, zResult, 4
3077f 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
30780 4e 54 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  NT);..  }else{..
30781 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 36 34      /* IMP: R-64
30782 38 39 34 2d 35 30 33 32 31 20 54 68 65 20 73 74  894-50321 The st
30783 72 69 6e 67 20 22 3f 30 30 30 22 20 69 73 20 72  ring "?000" is r
30784 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 61  eturned if the a
30785 72 67 75 6d 65 6e 74 0d 0a 20 20 20 20 2a 2a 20  rgument..    ** 
30786 69 73 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 74 61  is NULL or conta
30787 69 6e 73 20 6e 6f 20 41 53 43 49 49 20 61 6c 70  ins no ASCII alp
30788 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65  habetic characte
30789 72 73 2e 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  rs. */..    sqli
3078a 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
3078b 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30 22 2c  context, "?000",
3078c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   4, SQLITE_STATI
3078d 43 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e  C);..  }..}..#en
3078e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f  dif /* SQLITE_SO
3078f 55 4e 44 45 58 20 2a 2f 0d 0a 0d 0a 23 69 66 6e  UNDEX */....#ifn
30790 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
30791 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a  LOAD_EXTENSION..
30792 2f 2a 0d 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f  /*..** A functio
30793 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61 20 73  n that loads a s
30794 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20 65 78  hared-library ex
30795 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72 65 74  tension then ret
30796 75 72 6e 73 20 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a  urns NULL...*/..
30797 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64  static void load
30798 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Ext(sqlite3_cont
30799 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
3079a 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
3079b 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0d 0a  value **argv){..
3079c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
3079d 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ile = (const cha
3079e 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
3079f 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
307a0 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
307a1 7a 50 72 6f 63 3b 0d 0a 20 20 73 71 6c 69 74 65  zProc;..  sqlite
307a2 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
307a3 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
307a4 65 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 63  e(context);..  c
307a5 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
307a6 3b 0d 0a 0d 0a 20 20 69 66 28 20 61 72 67 63 3d  ;....  if( argc=
307a7 3d 32 20 29 7b 0d 0a 20 20 20 20 7a 50 72 6f 63  =2 ){..    zProc
307a8 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
307a9 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
307aa 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20  ext(argv[1]);.. 
307ab 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 50 72   }else{..    zPr
307ac 6f 63 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  oc = 0;..  }..  
307ad 69 66 28 20 7a 46 69 6c 65 20 26 26 20 73 71 6c  if( zFile && sql
307ae 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
307af 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a  ion(db, zFile, z
307b0 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29 20  Proc, &zErrMsg) 
307b1 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
307b2 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
307b3 74 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d  text, zErrMsg, -
307b4 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  1);..    sqlite3
307b5 5f 66 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0d  _free(zErrMsg);.
307b6 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d  .  }..}..#endif.
307b7 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69  ...../*..** An i
307b8 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
307b9 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
307ba 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e  re holds the con
307bb 74 65 78 74 20 6f 66 20 61 0d 0a 2a 2a 20 73 75  text of a..** su
307bc 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67  m() or avg() agg
307bd 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69  regate computati
307be 6f 6e 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66  on...*/..typedef
307bf 20 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 53   struct SumCtx S
307c0 75 6d 43 74 78 3b 0d 0a 73 74 72 75 63 74 20 53  umCtx;..struct S
307c1 75 6d 43 74 78 20 7b 0d 0a 20 20 64 6f 75 62 6c  umCtx {..  doubl
307c2 65 20 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20  e rSum;      /* 
307c3 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  Floating point s
307c4 75 6d 20 2a 2f 0d 0a 20 20 69 36 34 20 69 53 75  um */..  i64 iSu
307c5 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  m;         /* In
307c6 74 65 67 65 72 20 73 75 6d 20 2a 2f 20 20 20 0d  teger sum */   .
307c7 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20 20 20  .  i64 cnt;     
307c8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
307c9 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65  f elements summe
307ca 64 20 2a 2f 0d 0a 20 20 75 38 20 6f 76 65 72 66  d */..  u8 overf
307cb 6c 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  low;      /* Tru
307cc 65 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76 65  e if integer ove
307cd 72 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0d 0a 20  rflow seen */.. 
307ce 20 75 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20   u8 approx;     
307cf 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f     /* True if no
307d0 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  n-integer value 
307d1 77 61 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65  was input to the
307d2 20 73 75 6d 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f   sum */..};..../
307d3 2a 0d 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75  *..** Routines u
307d4 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74  sed to compute t
307d5 68 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 2c  he sum, average,
307d6 20 61 6e 64 20 74 6f 74 61 6c 2e 0d 0a 2a 2a 0d   and total...**.
307d7 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66 75  .** The SUM() fu
307d8 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74  nction follows t
307d9 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20  he (broken) SQL 
307da 73 74 61 6e 64 61 72 64 20 77 68 69 63 68 20 6d  standard which m
307db 65 61 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 69 74  eans..** that it
307dc 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66   returns NULL if
307dd 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f   it sums over no
307de 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20   inputs.  TOTAL 
307df 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 30 2e 30 20  returns..** 0.0 
307e0 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49  in that case.  I
307e1 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41  n addition, TOTA
307e2 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  L always returns
307e3 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0d 0a   a float where..
307e4 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74  ** SUM might ret
307e5 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69  urn an integer i
307e6 66 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75  f it never encou
307e7 6e 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67  nters a floating
307e8 20 70 6f 69 6e 74 0d 0a 2a 2a 20 76 61 6c 75 65   point..** value
307e9 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66  .  TOTAL never f
307ea 61 69 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69  ails, but SUM mi
307eb 67 68 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65  ght through an e
307ec 78 63 65 70 74 69 6f 6e 20 69 66 0d 0a 2a 2a 20  xception if..** 
307ed 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20  it overflows an 
307ee 69 6e 74 65 67 65 72 2e 0d 0a 2a 2f 0d 0a 73 74  integer...*/..st
307ef 61 74 69 63 20 76 6f 69 64 20 73 75 6d 53 74 65  atic void sumSte
307f0 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  p(sqlite3_contex
307f1 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
307f2 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
307f3 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0d 0a 20 20  lue **argv){..  
307f4 53 75 6d 43 74 78 20 2a 70 3b 0d 0a 20 20 69 6e  SumCtx *p;..  in
307f5 74 20 74 79 70 65 3b 0d 0a 20 20 61 73 73 65 72  t type;..  asser
307f6 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0d 0a 20  t( argc==1 );.. 
307f7 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
307f8 52 28 61 72 67 63 29 3b 0d 0a 20 20 70 20 3d 20  R(argc);..  p = 
307f9 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
307fa 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
307fb 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0d  t, sizeof(*p));.
307fc 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65  .  type = sqlite
307fd 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
307fe 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0d 0a  type(argv[0]);..
307ff 20 20 69 66 28 20 70 20 26 26 20 74 79 70 65 21    if( p && type!
30800 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0d  =SQLITE_NULL ){.
30801 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0d 0a  .    p->cnt++;..
30802 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51      if( type==SQ
30803 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0d  LITE_INTEGER ){.
30804 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73  .      i64 v = s
30805 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
30806 36 34 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20  64(argv[0]);..  
30807 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76      p->rSum += v
30808 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d  ;..      if( (p-
30809 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66  >approx|p->overf
3080a 6c 6f 77 29 3d 3d 30 20 26 26 20 73 71 6c 69 74  low)==0 && sqlit
3080b 65 33 41 64 64 49 6e 74 36 34 28 26 70 2d 3e 69  e3AddInt64(&p->i
3080c 53 75 6d 2c 20 76 29 20 29 7b 0d 0a 20 20 20 20  Sum, v) ){..    
3080d 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20      p->overflow 
3080e 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  = 1;..      }.. 
3080f 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
30810 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69   p->rSum += sqli
30811 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
30812 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20  (argv[0]);..    
30813 20 20 70 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b    p->approx = 1;
30814 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d  ..    }..  }..}.
30815 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
30816 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
30817 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
30818 74 29 7b 0d 0a 20 20 53 75 6d 43 74 78 20 2a 70  t){..  SumCtx *p
30819 3b 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  ;..  p = sqlite3
3081a 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
3081b 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0d  xt(context, 0);.
3081c 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63  .  if( p && p->c
3081d 6e 74 3e 30 20 29 7b 0d 0a 20 20 20 20 69 66 28  nt>0 ){..    if(
3081e 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0d   p->overflow ){.
3081f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
30820 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
30821 65 78 74 2c 22 69 6e 74 65 67 65 72 20 6f 76 65  ext,"integer ove
30822 72 66 6c 6f 77 22 2c 2d 31 29 3b 0d 0a 20 20 20  rflow",-1);..   
30823 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 61 70   }else if( p->ap
30824 70 72 6f 78 20 29 7b 0d 0a 20 20 20 20 20 20 73  prox ){..      s
30825 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
30826 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d  uble(context, p-
30827 3e 72 53 75 6d 29 3b 0d 0a 20 20 20 20 7d 65 6c  >rSum);..    }el
30828 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
30829 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
3082a 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d  context, p->iSum
3082b 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
3082c 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  }..static void a
3082d 76 67 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  vgFinalize(sqlit
3082e 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
3082f 65 78 74 29 7b 0d 0a 20 20 53 75 6d 43 74 78 20  ext){..  SumCtx 
30830 2a 70 3b 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74  *p;..  p = sqlit
30831 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
30832 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
30833 3b 0d 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  ;..  if( p && p-
30834 3e 63 6e 74 3e 30 20 29 7b 0d 0a 20 20 20 20 73  >cnt>0 ){..    s
30835 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
30836 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d  uble(context, p-
30837 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d  >rSum/(double)p-
30838 3e 63 6e 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  >cnt);..  }..}..
30839 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61  static void tota
3083a 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65  lFinalize(sqlite
3083b 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3083c 78 74 29 7b 0d 0a 20 20 53 75 6d 43 74 78 20 2a  xt){..  SumCtx *
3083d 70 3b 0d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  p;..  p = sqlite
3083e 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
3083f 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  ext(context, 0);
30840 0d 0a 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30  ..  /* (double)0
30841 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
30842 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
30843 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0d 0a 20 20  _POINT... */..  
30844 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
30845 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
30846 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 28 64 6f   ? p->rSum : (do
30847 75 62 6c 65 29 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  uble)0);..}..../
30848 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
30849 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65  ing structure ke
3084a 65 70 73 20 74 72 61 63 6b 20 6f 66 20 73 74 61  eps track of sta
3084b 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  te information f
3084c 6f 72 20 74 68 65 0d 0a 2a 2a 20 63 6f 75 6e 74  or the..** count
3084d 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  () aggregate fun
3084e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  ction...*/..type
3084f 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 6e 74  def struct Count
30850 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0d 0a 73  Ctx CountCtx;..s
30851 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 7b  truct CountCtx {
30852 0d 0a 20 20 69 36 34 20 6e 3b 0d 0a 7d 3b 0d 0a  ..  i64 n;..};..
30853 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 75 74 69 6e 65  ../*..** Routine
30854 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  s to implement t
30855 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65  he count() aggre
30856 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a  gate function...
30857 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
30858 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65  countStep(sqlite
30859 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3085a 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
3085b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
3085c 67 76 29 7b 0d 0a 20 20 43 6f 75 6e 74 43 74 78  gv){..  CountCtx
3085d 20 2a 70 3b 0d 0a 20 20 70 20 3d 20 73 71 6c 69   *p;..  p = sqli
3085e 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
3085f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
30860 69 7a 65 6f 66 28 2a 70 29 29 3b 0d 0a 20 20 69  izeof(*p));..  i
30861 66 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53  f( (argc==0 || S
30862 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69  QLITE_NULL!=sqli
30863 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
30864 72 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b  rgv[0])) && p ){
30865 0d 0a 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0d 0a 20  ..    p->n++;.. 
30866 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51   }....#ifndef SQ
30867 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
30868 41 54 45 44 0d 0a 20 20 2f 2a 20 54 68 65 20 73  ATED..  /* The s
30869 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
3086a 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63 74 69 6f  _count() functio
3086b 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 2e  n is deprecated.
3086c 20 20 42 75 74 20 6a 75 73 74 20 74 6f 20 6d 61    But just to ma
3086d 6b 65 0d 0a 20 20 2a 2a 20 73 75 72 65 20 69 74  ke..  ** sure it
3086e 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65 73 20   still operates 
3086f 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72 69 66  correctly, verif
30870 79 20 74 68 61 74 20 69 74 73 20 63 6f 75 6e 74  y that its count
30871 20 61 67 72 65 65 73 20 77 69 74 68 20 6f 75 72   agrees with our
30872 20 0d 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c   ..  ** internal
30873 20 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69 6e   count when usin
30874 67 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20 77  g count(*) and w
30875 68 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63 6f  hen the total co
30876 75 6e 74 20 63 61 6e 20 62 65 0d 0a 20 20 2a 2a  unt can be..  **
30877 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20   expressed as a 
30878 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20  32-bit integer. 
30879 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
3087a 67 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c  gc==1 || p==0 ||
3087b 20 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66 66 66   p->n>0x7fffffff
3087c 0d 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70  ..          || p
3087d 2d 3e 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67 67  ->n==sqlite3_agg
3087e 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f 6e  regate_count(con
3087f 74 65 78 74 29 20 29 3b 0d 0a 23 65 6e 64 69 66  text) );..#endif
30880 0d 0a 7d 20 20 20 0d 0a 73 74 61 74 69 63 20 76  ..}   ..static v
30881 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a  oid countFinaliz
30882 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
30883 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0d 0a 20 20  t *context){..  
30884 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0d 0a 20 20  CountCtx *p;..  
30885 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
30886 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
30887 6e 74 65 78 74 2c 20 30 29 3b 0d 0a 20 20 73 71  ntext, 0);..  sq
30888 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
30889 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20  64(context, p ? 
3088a 70 2d 3e 6e 20 3a 20 30 29 3b 0d 0a 7d 0d 0a 0d  p->n : 0);..}...
3088b 0a 2f 2a 0d 0a 2a 2a 20 52 6f 75 74 69 6e 65 73  ./*..** Routines
3088c 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 69   to implement mi
3088d 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67  n() and max() ag
3088e 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
3088f 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  s...*/..static v
30890 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28 0d  oid minmaxStep(.
30891 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
30892 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0d 0a 20  xt *context, .. 
30893 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0d 0a   int NotUsed, ..
30894 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
30895 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 4d 65  **argv..){..  Me
30896 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20  m *pArg  = (Mem 
30897 2a 29 61 72 67 76 5b 30 5d 3b 0d 0a 20 20 4d 65  *)argv[0];..  Me
30898 6d 20 2a 70 42 65 73 74 3b 0d 0a 20 20 55 4e 55  m *pBest;..  UNU
30899 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
3089a 74 55 73 65 64 29 3b 0d 0a 0d 0a 20 20 69 66 28  tUsed);....  if(
3089b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3089c 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
3089d 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
3089e 72 6e 3b 0d 0a 20 20 70 42 65 73 74 20 3d 20 28  rn;..  pBest = (
3089f 4d 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67  Mem *)sqlite3_ag
308a0 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
308a1 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
308a2 2a 70 42 65 73 74 29 29 3b 0d 0a 20 20 69 66 28  *pBest));..  if(
308a3 20 21 70 42 65 73 74 20 29 20 72 65 74 75 72 6e   !pBest ) return
308a4 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 42 65 73 74  ;....  if( pBest
308a5 2d 3e 66 6c 61 67 73 20 29 7b 0d 0a 20 20 20 20  ->flags ){..    
308a6 69 6e 74 20 6d 61 78 3b 0d 0a 20 20 20 20 69 6e  int max;..    in
308a7 74 20 63 6d 70 3b 0d 0a 20 20 20 20 43 6f 6c 6c  t cmp;..    Coll
308a8 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
308a9 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
308aa 65 71 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  eq(context);..  
308ab 20 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66    /* This step f
308ac 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
308ad 66 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e  for both the min
308ae 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67  () and max() agg
308af 72 65 67 61 74 65 73 2c 0d 0a 20 20 20 20 2a 2a  regates,..    **
308b0 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72   the only differ
308b1 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65  ence between the
308b2 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20   two being that 
308b3 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65  the sense of the
308b4 0d 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69  ..    ** compari
308b5 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64 2e  son is inverted.
308b6 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20 61   For the max() a
308b7 67 67 72 65 67 61 74 65 2c 20 74 68 65 0d 0a 20  ggregate, the.. 
308b8 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73     ** sqlite3_us
308b9 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69  er_data() functi
308ba 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64  on returns (void
308bb 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29   *)-1. For min()
308bc 20 69 74 0d 0a 20 20 20 20 2a 2a 20 72 65 74 75   it..    ** retu
308bd 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20  rns (void *)db, 
308be 77 68 65 72 65 20 64 62 20 69 73 20 74 68 65 20  where db is the 
308bf 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
308c0 65 20 70 6f 69 6e 74 65 72 2e 0d 0a 20 20 20 20  e pointer...    
308c1 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68 65  ** Therefore the
308c2 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20   next statement 
308c3 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d  sets variable 'm
308c4 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65  ax' to 1 for the
308c5 20 6d 61 78 28 29 0d 0a 20 20 20 20 2a 2a 20 61   max()..    ** a
308c6 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66  ggregate, or 0 f
308c7 6f 72 20 6d 69 6e 28 29 2e 0d 0a 20 20 20 20 2a  or min()...    *
308c8 2f 0d 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c  /..    max = sql
308c9 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
308ca 6f 6e 74 65 78 74 29 21 3d 30 3b 0d 0a 20 20 20  ontext)!=0;..   
308cb 20 63 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65   cmp = sqlite3Me
308cc 6d 43 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20  mCompare(pBest, 
308cd 70 41 72 67 2c 20 70 43 6f 6c 6c 29 3b 0d 0a 20  pArg, pColl);.. 
308ce 20 20 20 69 66 28 20 28 6d 61 78 20 26 26 20 63     if( (max && c
308cf 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 78 20 26  mp<0) || (!max &
308d0 26 20 63 6d 70 3e 30 29 20 29 7b 0d 0a 20 20 20  & cmp>0) ){..   
308d1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
308d2 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72  mCopy(pBest, pAr
308d3 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65  g);..    }..  }e
308d4 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  lse{..    sqlite
308d5 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65  3VdbeMemCopy(pBe
308d6 73 74 2c 20 70 41 72 67 29 3b 0d 0a 20 20 7d 0d  st, pArg);..  }.
308d7 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
308d8 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73  minMaxFinalize(s
308d9 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
308da 63 6f 6e 74 65 78 74 29 7b 0d 0a 20 20 73 71 6c  context){..  sql
308db 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73  ite3_value *pRes
308dc 3b 0d 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c  ;..  pRes = (sql
308dd 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c  ite3_value *)sql
308de 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
308df 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
308e0 30 29 3b 0d 0a 20 20 69 66 28 20 70 52 65 73 20  0);..  if( pRes 
308e1 29 7b 0d 0a 20 20 20 20 69 66 28 20 41 4c 57 41  ){..    if( ALWA
308e2 59 53 28 70 52 65 73 2d 3e 66 6c 61 67 73 29 20  YS(pRes->flags) 
308e3 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
308e4 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
308e5 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0d 0a  ontext, pRes);..
308e6 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
308e7 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
308e8 28 70 52 65 73 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  (pRes);..  }..}.
308e9 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 67 72 6f 75 70 5f  .../*..** group_
308ea 63 6f 6e 63 61 74 28 45 58 50 52 2c 20 3f 53 45  concat(EXPR, ?SE
308eb 50 41 52 41 54 4f 52 3f 29 0d 0a 2a 2f 0d 0a 73  PARATOR?)..*/..s
308ec 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
308ed 43 6f 6e 63 61 74 53 74 65 70 28 0d 0a 20 20 73  ConcatStep(..  s
308ee 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
308ef 63 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20  context,..  int 
308f0 61 72 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33  argc,..  sqlite3
308f1 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29  _value **argv..)
308f2 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  {..  const char 
308f3 2a 7a 56 61 6c 3b 0d 0a 20 20 53 74 72 41 63 63  *zVal;..  StrAcc
308f4 75 6d 20 2a 70 41 63 63 75 6d 3b 0d 0a 20 20 63  um *pAccum;..  c
308f5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b  onst char *zSep;
308f6 0d 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53  ..  int nVal, nS
308f7 65 70 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61  ep;..  assert( a
308f8 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d  rgc==1 || argc==
308f9 32 20 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  2 );..  if( sqli
308fa 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
308fb 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
308fc 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0d 0a  NULL ) return;..
308fd 20 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41    pAccum = (StrA
308fe 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67  ccum*)sqlite3_ag
308ff 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
30900 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
30901 2a 70 41 63 63 75 6d 29 29 3b 0d 0a 0d 0a 20 20  *pAccum));....  
30902 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0d 0a 20  if( pAccum ){.. 
30903 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
30904 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
30905 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
30906 78 74 29 3b 0d 0a 20 20 20 20 69 6e 74 20 66 69  xt);..    int fi
30907 72 73 74 54 65 72 6d 20 3d 20 70 41 63 63 75 6d  rstTerm = pAccum
30908 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d 30 3b 0d  ->useMalloc==0;.
30909 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 75 73 65  .    pAccum->use
3090a 4d 61 6c 6c 6f 63 20 3d 20 32 3b 0d 0a 20 20 20  Malloc = 2;..   
3090b 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63   pAccum->mxAlloc
3090c 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
3090d 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
3090e 48 5d 3b 0d 0a 20 20 20 20 69 66 28 20 21 66 69  H];..    if( !fi
3090f 72 73 74 54 65 72 6d 20 29 7b 0d 0a 20 20 20 20  rstTerm ){..    
30910 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b    if( argc==2 ){
30911 0d 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d  ..        zSep =
30912 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f   (char*)sqlite3_
30913 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
30914 31 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 6e 53  1]);..        nS
30915 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ep = sqlite3_val
30916 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
30917 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  );..      }else{
30918 0d 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d  ..        zSep =
30919 20 22 2c 22 3b 0d 0a 20 20 20 20 20 20 20 20 6e   ",";..        n
3091a 53 65 70 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  Sep = 1;..      
3091b 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
3091c 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
3091d 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65  Accum, zSep, nSe
3091e 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  p);..    }..    
3091f 7a 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 71  zVal = (char*)sq
30920 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
30921 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 20 20  (argv[0]);..    
30922 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  nVal = sqlite3_v
30923 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
30924 30 5d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  0]);..    sqlite
30925 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
30926 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56  pAccum, zVal, nV
30927 61 6c 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73 74  al);..  }..}..st
30928 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70 43  atic void groupC
30929 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73 71  oncatFinalize(sq
3092a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
3092b 6f 6e 74 65 78 74 29 7b 0d 0a 20 20 53 74 72 41  ontext){..  StrA
3092c 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0d 0a 20  ccum *pAccum;.. 
3092d 20 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 65   pAccum = sqlite
3092e 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
3092f 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  ext(context, 0);
30930 0d 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29  ..  if( pAccum )
30931 7b 0d 0a 20 20 20 20 69 66 28 20 70 41 63 63 75  {..    if( pAccu
30932 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0d 0a 20 20  m->tooBig ){..  
30933 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
30934 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
30935 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20 7d  context);..    }
30936 65 6c 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d  else if( pAccum-
30937 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
30938 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
30939 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
3093a 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20  em(context);..  
3093b 20 20 7d 65 6c 73 65 7b 20 20 20 20 0d 0a 20 20    }else{    ..  
3093c 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3093d 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
3093e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
3093f 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20  Finish(pAccum), 
30940 2d 31 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  -1, ..          
30941 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30942 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0d 0a  sqlite3_free);..
30943 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d      }..  }..}...
30944 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./*..** This rou
30945 74 69 6e 65 20 64 6f 65 73 20 70 65 72 2d 63 6f  tine does per-co
30946 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f  nnection functio
30947 6e 20 72 65 67 69 73 74 72 61 74 69 6f 6e 2e 20  n registration. 
30948 20 4d 6f 73 74 0d 0a 2a 2a 20 6f 66 20 74 68 65   Most..** of the
30949 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
3094a 6f 6e 73 20 61 62 6f 76 65 20 61 72 65 20 70 61  ons above are pa
3094b 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  rt of the global
3094c 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 2e 0d 0a   function set...
3094d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3094e 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20  only deals with 
3094f 74 68 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e  those that are n
30950 6f 74 20 67 6c 6f 62 61 6c 2e 0d 0a 2a 2f 0d 0a  ot global...*/..
30951 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
30952 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
30953 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
30954 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
30955 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  {..  int rc = sq
30956 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
30957 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41 54  unction(db, "MAT
30958 43 48 22 2c 20 32 29 3b 0d 0a 20 20 61 73 73 65  CH", 2);..  asse
30959 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  rt( rc==SQLITE_N
3095a 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  OMEM || rc==SQLI
3095b 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 69 66 28 20  TE_OK );..  if( 
3095c 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
3095d 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c   ){..    db->mal
3095e 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a  locFailed = 1;..
3095f 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
30960 20 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54   Set the LIKEOPT
30961 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61   flag on the 2-a
30962 72 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e  rgument function
30963 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
30964 6e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  name...*/..stati
30965 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70  c void setLikeOp
30966 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a 64  tFlag(sqlite3 *d
30967 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
30968 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67 56 61 6c  Name, u8 flagVal
30969 29 7b 0d 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  ){..  FuncDef *p
3096a 44 65 66 3b 0d 0a 20 20 70 44 65 66 20 3d 20 73  Def;..  pDef = s
3096b 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
3096c 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 71  on(db, zName, sq
3096d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
3096e 61 6d 65 29 2c 0d 0a 20 20 20 20 20 20 20 20 20  ame),..         
3096f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30970 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54      2, SQLITE_UT
30971 46 38 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 41  F8, 0);..  if( A
30972 4c 57 41 59 53 28 70 44 65 66 29 20 29 7b 0d 0a  LWAYS(pDef) ){..
30973 20 20 20 20 70 44 65 66 2d 3e 66 6c 61 67 73 20      pDef->flags 
30974 3d 20 66 6c 61 67 56 61 6c 3b 0d 0a 20 20 7d 0d  = flagVal;..  }.
30975 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67  .}..../*..** Reg
30976 69 73 74 65 72 20 74 68 65 20 62 75 69 6c 74 2d  ister the built-
30977 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42  in LIKE and GLOB
30978 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65   functions.  The
30979 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 0d 0a   caseSensitive..
3097a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 65 74  ** parameter det
3097b 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20  ermines whether 
3097c 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 20  or not the LIKE 
3097d 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 73 65  operator is case
3097e 0d 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e 20  ..** sensitive. 
3097f 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 20   GLOB is always 
30980 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0d  case sensitive..
30981 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
30982 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
30983 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
30984 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
30985 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69  b, int caseSensi
30986 74 69 76 65 29 7b 0d 0a 20 20 73 74 72 75 63 74  tive){..  struct
30987 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49   compareInfo *pI
30988 6e 66 6f 3b 0d 0a 20 20 69 66 28 20 63 61 73 65  nfo;..  if( case
30989 53 65 6e 73 69 74 69 76 65 20 29 7b 0d 0a 20 20  Sensitive ){..  
3098a 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63    pInfo = (struc
3098b 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26  t compareInfo*)&
3098c 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0d 0a 20 20  likeInfoAlt;..  
3098d 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 49 6e 66  }else{..    pInf
3098e 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70  o = (struct comp
3098f 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e  areInfo*)&likeIn
30990 66 6f 4e 6f 72 6d 3b 0d 0a 20 20 7d 0d 0a 20 20  foNorm;..  }..  
30991 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
30992 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c  c(db, "like", 2,
30993 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 70 49   SQLITE_UTF8, pI
30994 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30  nfo, likeFunc, 0
30995 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 73 71 6c 69  , 0, 0);..  sqli
30996 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
30997 2c 20 22 6c 69 6b 65 22 2c 20 33 2c 20 53 51 4c  , "like", 3, SQL
30998 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c  ITE_UTF8, pInfo,
30999 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c   likeFunc, 0, 0,
3099a 20 30 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 43   0);..  sqlite3C
3099b 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 67  reateFunc(db, "g
3099c 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  lob", 2, SQLITE_
3099d 55 54 46 38 2c 20 0d 0a 20 20 20 20 20 20 28 73  UTF8, ..      (s
3099e 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
3099f 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69  o*)&globInfo, li
309a0 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29  keFunc, 0, 0, 0)
309a1 3b 0d 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46  ;..  setLikeOptF
309a2 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20  lag(db, "glob", 
309a3 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
309a4 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43   | SQLITE_FUNC_C
309a5 41 53 45 29 3b 0d 0a 20 20 73 65 74 4c 69 6b 65  ASE);..  setLike
309a6 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b  OptFlag(db, "lik
309a7 65 22 2c 20 0d 0a 20 20 20 20 20 20 63 61 73 65  e", ..      case
309a8 53 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51 4c  Sensitive ? (SQL
309a9 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20  ITE_FUNC_LIKE | 
309aa 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
309ab 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ) : SQLITE_FUNC_
309ac 4c 49 4b 45 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  LIKE);..}..../*.
309ad 0a 2a 2a 20 70 45 78 70 72 20 70 6f 69 6e 74 73  .** pExpr points
309ae 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f   to an expressio
309af 6e 20 77 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e  n which implemen
309b0 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ts a function.  
309b1 49 66 0d 0a 2a 2a 20 69 74 20 69 73 20 61 70 70  If..** it is app
309b2 72 6f 70 72 69 61 74 65 20 74 6f 20 61 70 70 6c  ropriate to appl
309b3 79 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d  y the LIKE optim
309b4 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68 61 74 20  ization to that 
309b5 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 74 68 65  function..** the
309b6 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74 68 72  n set aWc[0] thr
309b7 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f 20 74  ough aWc[2] to t
309b8 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72  he wildcard char
309b9 61 63 74 65 72 73 20 61 6e 64 0d 0a 2a 2a 20 72  acters and..** r
309ba 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
309bb 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
309bc 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c 65  not a LIKE-style
309bd 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0d 0a   function then..
309be 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e  ** return FALSE.
309bf 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
309c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
309c1 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73  IsLikeFunction(s
309c2 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
309c3 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49   *pExpr, int *pI
309c4 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61  sNocase, char *a
309c5 57 63 29 7b 0d 0a 20 20 46 75 6e 63 44 65 66 20  Wc){..  FuncDef 
309c6 2a 70 44 65 66 3b 0d 0a 20 20 69 66 28 20 70 45  *pDef;..  if( pE
309c7 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43  xpr->op!=TK_FUNC
309c8 54 49 4f 4e 20 0d 0a 20 20 20 7c 7c 20 21 70 45  TION ..   || !pE
309c9 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0d 0a 20  xpr->x.pList .. 
309ca 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c    || pExpr->x.pL
309cb 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 0d 0a 20  ist->nExpr!=2.. 
309cc 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
309cd 30 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  0;..  }..  asser
309ce 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
309cf 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
309d0 73 53 65 6c 65 63 74 29 20 29 3b 0d 0a 20 20 70  sSelect) );..  p
309d1 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Def = sqlite3Fin
309d2 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45  dFunction(db, pE
309d3 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0d  xpr->u.zToken, .
309d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
309d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
309d6 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45  lite3Strlen30(pE
309d7 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0d  xpr->u.zToken),.
309d8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
309d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
309da 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29   SQLITE_UTF8, 0)
309db 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  ;..  if( NEVER(p
309dc 44 65 66 3d 3d 30 29 20 7c 7c 20 28 70 44 65 66  Def==0) || (pDef
309dd 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
309de 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29  _FUNC_LIKE)==0 )
309df 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {..    return 0;
309e0 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68  ..  }....  /* Th
309e1 65 20 6d 65 6d 63 70 79 28 29 20 73 74 61 74 65  e memcpy() state
309e2 6d 65 6e 74 20 61 73 73 75 6d 65 73 20 74 68 61  ment assumes tha
309e3 74 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63  t the wildcard c
309e4 68 61 72 61 63 74 65 72 73 20 61 72 65 0d 0a 20  haracters are.. 
309e5 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 68   ** the first th
309e6 72 65 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ree statements i
309e7 6e 20 74 68 65 20 63 6f 6d 70 61 72 65 49 6e 66  n the compareInf
309e8 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  o structure.  Th
309e9 65 0d 0a 20 20 2a 2a 20 61 73 73 65 72 74 73 28  e..  ** asserts(
309ea 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76 65  ) that follow ve
309eb 72 69 66 79 20 74 68 61 74 20 61 73 73 75 6d 70  rify that assump
309ec 74 69 6f 6e 0d 0a 20 20 2a 2f 0d 0a 20 20 6d 65  tion..  */..  me
309ed 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d 3e  mcpy(aWc, pDef->
309ee 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0d 0a  pUserData, 3);..
309ef 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72 2a    assert( (char*
309f0 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d  )&likeInfoAlt ==
309f1 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
309f2 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b  oAlt.matchAll );
309f3 0d 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63  ..  assert( &((c
309f4 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
309f5 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29  t)[1] == (char*)
309f6 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74  &likeInfoAlt.mat
309f7 63 68 4f 6e 65 20 29 3b 0d 0a 20 20 61 73 73 65  chOne );..  asse
309f8 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69  rt( &((char*)&li
309f9 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d  keInfoAlt)[2] ==
309fa 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
309fb 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b  oAlt.matchSet );
309fc 0d 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d  ..  *pIsNocase =
309fd 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20   (pDef->flags & 
309fe 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
309ff 29 3d 3d 30 3b 0d 0a 20 20 72 65 74 75 72 6e 20  )==0;..  return 
30a00 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  1;..}..../*..** 
30a01 41 6c 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 46  All all of the F
30a02 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
30a03 73 20 69 6e 20 74 68 65 20 61 42 75 69 6c 74 69  s in the aBuilti
30a04 6e 46 75 6e 63 5b 5d 20 61 72 72 61 79 20 61 62  nFunc[] array ab
30a05 6f 76 65 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 67  ove..** to the g
30a06 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 68  lobal function h
30a07 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ash table.  This
30a08 20 6f 63 63 75 72 73 20 61 74 20 73 74 61 72 74   occurs at start
30a09 2d 74 69 6d 65 20 28 61 73 0d 0a 2a 2a 20 61 20  -time (as..** a 
30a0a 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 63  consequence of c
30a0b 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
30a0c 6e 69 74 69 61 6c 69 7a 65 28 29 29 2e 0d 0a 2a  nitialize())...*
30a0d 2a 0d 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73  *..** After this
30a0e 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 0d 0a 2a   routine runs..*
30a0f 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
30a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
30a11 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63  gisterGlobalFunc
30a12 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0d 0a 20 20  tions(void){..  
30a13 2f 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c  /*..  ** The fol
30a14 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c  lowing array hol
30a15 64 73 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ds FuncDef struc
30a16 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66  tures for all of
30a17 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0d 0a   the functions..
30a18 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20    ** defined in 
30a19 74 68 69 73 20 66 69 6c 65 2e 0d 0a 20 20 2a 2a  this file...  **
30a1a 0d 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 79  ..  ** The array
30a1b 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74   cannot be const
30a1c 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 65  ant since change
30a1d 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68  s are made to th
30a1e 65 0d 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e  e..  ** FuncDef.
30a1f 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61  pHash elements a
30a20 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54  t start-time.  T
30a21 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  he elements of t
30a22 68 69 73 20 61 72 72 61 79 0d 0a 20 20 2a 2a 20  his array..  ** 
30a23 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66  are read-only af
30a24 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
30a25 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0d  on is complete..
30a26 0a 20 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20  .  */..  static 
30a27 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44  SQLITE_WSD FuncD
30a28 65 66 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b  ef aBuiltinFunc[
30a29 5d 20 3d 20 7b 0d 0a 20 20 20 20 46 55 4e 43 54  ] = {..    FUNCT
30a2a 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20  ION(ltrim,      
30a2b 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30 2c          1, 1, 0,
30a2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
30a2d 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49    ),..    FUNCTI
30a2e 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20  ON(ltrim,       
30a2f 20 20 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20         2, 1, 0, 
30a30 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
30a31 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f   ),..    FUNCTIO
30a32 4e 28 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20  N(rtrim,        
30a33 20 20 20 20 20 20 31 2c 20 32 2c 20 30 2c 20 74        1, 2, 0, t
30a34 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
30a35 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  ),..    FUNCTION
30a36 28 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  (rtrim,         
30a37 20 20 20 20 20 32 2c 20 32 2c 20 30 2c 20 74 72       2, 2, 0, tr
30a38 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
30a39 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ,..    FUNCTION(
30a3a 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
30a3b 20 20 20 20 31 2c 20 33 2c 20 30 2c 20 74 72 69      1, 3, 0, tri
30a3c 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
30a3d 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ..    FUNCTION(t
30a3e 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
30a3f 20 20 20 32 2c 20 33 2c 20 30 2c 20 74 72 69 6d     2, 3, 0, trim
30a40 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0d  Func         ),.
30a41 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69  .    FUNCTION(mi
30a42 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
30a43 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61   -1, 0, 1, minma
30a44 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0d 0a  xFunc       ),..
30a45 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e      FUNCTION(min
30a46 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
30a47 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 20 20   0, 0, 1, 0     
30a48 20 20 20 20 20 20 20 20 20 20 20 29 2c 0d 0a 20             ),.. 
30a49 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 69 6e     AGGREGATE(min
30a4a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
30a4b 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53  1, 0, 1, minmaxS
30a4c 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78  tep,      minMax
30a4d 46 69 6e 61 6c 69 7a 65 20 29 2c 0d 0a 20 20 20  Finalize ),..   
30a4e 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20   FUNCTION(max,  
30a4f 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c               -1,
30a50 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e   1, 1, minmaxFun
30a51 63 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20  c       ),..    
30a52 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20  FUNCTION(max,   
30a53 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
30a54 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20 20  1, 1, 0         
30a55 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 41         ),..    A
30a56 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 20  GGREGATE(max,   
30a57 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31              1, 1
30a58 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c  , 1, minmaxStep,
30a59 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61        minMaxFina
30a5a 6c 69 7a 65 20 29 2c 0d 0a 20 20 20 20 46 55 4e  lize ),..    FUN
30a5b 43 54 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20  CTION(typeof,   
30a5c 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
30a5d 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 20 20 20  0, typeofFunc   
30a5e 20 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43      ),..    FUNC
30a5f 54 49 4f 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20  TION(length,    
30a60 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
30a61 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20 20 20 20  , lengthFunc    
30a62 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54     ),..    FUNCT
30a63 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20  ION(substr,     
30a64 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c          2, 0, 0,
30a65 20 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 20   substrFunc     
30a66 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49    ),..    FUNCTI
30a67 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20 20  ON(substr,      
30a68 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c 20         3, 0, 0, 
30a69 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 20 20  substrFunc      
30a6a 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f   ),..    FUNCTIO
30a6b 4e 28 61 62 73 2c 20 20 20 20 20 20 20 20 20 20  N(abs,          
30a6c 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 61        1, 0, 0, a
30a6d 62 73 46 75 6e 63 20 20 20 20 20 20 20 20 20 20  bsFunc          
30a6e 29 2c 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ),..#ifndef SQLI
30a6f 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
30a70 5f 50 4f 49 4e 54 0d 0a 20 20 20 20 46 55 4e 43  _POINT..    FUNC
30a71 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20  TION(round,     
30a72 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
30a73 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20  , roundFunc     
30a74 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54     ),..    FUNCT
30a75 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20  ION(round,      
30a76 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c          2, 0, 0,
30a77 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20   roundFunc      
30a78 20 20 29 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20 20    ),..#endif..  
30a79 20 20 46 55 4e 43 54 49 4f 4e 28 75 70 70 65 72    FUNCTION(upper
30a7a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
30a7b 2c 20 30 2c 20 30 2c 20 75 70 70 65 72 46 75 6e  , 0, 0, upperFun
30a7c 63 20 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20  c        ),..   
30a7d 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 65 72 2c   FUNCTION(lower,
30a7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
30a7f 20 30 2c 20 30 2c 20 6c 6f 77 65 72 46 75 6e 63   0, 0, lowerFunc
30a80 20 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20          ),..    
30a81 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63  FUNCTION(coalesc
30a82 65 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20  e,           1, 
30a83 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20  0, 0, 0         
30a84 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 46         ),..    F
30a85 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65  UNCTION(coalesce
30a86 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30  ,           0, 0
30a87 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20  , 0, 0          
30a88 20 20 20 20 20 20 29 2c 0d 0a 2f 2a 20 20 46 55        ),../*  FU
30a89 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c  NCTION(coalesce,
30a8a 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c            -1, 0,
30a8b 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20   0, ifnullFunc  
30a8c 20 20 20 20 20 29 2c 20 2a 2f 0d 0a 20 20 20 20       ), */..    
30a8d 7b 2d 31 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c  {-1,SQLITE_UTF8,
30a8e 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c  SQLITE_FUNC_COAL
30a8f 45 53 43 45 2c 30 2c 30 2c 69 66 6e 75 6c 6c 46  ESCE,0,0,ifnullF
30a90 75 6e 63 2c 30 2c 30 2c 22 63 6f 61 6c 65 73 63  unc,0,0,"coalesc
30a91 65 22 2c 30 2c 30 7d 2c 0d 0a 20 20 20 20 46 55  e",0,0},..    FU
30a92 4e 43 54 49 4f 4e 28 68 65 78 2c 20 20 20 20 20  NCTION(hex,     
30a93 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
30a94 20 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20 20   0, hexFunc     
30a95 20 20 20 20 20 29 2c 0d 0a 2f 2a 20 20 46 55 4e       ),../*  FUN
30a96 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c 20 20 20  CTION(ifnull,   
30a97 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
30a98 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20  0, ifnullFunc   
30a99 20 20 20 20 29 2c 20 2a 2f 0d 0a 20 20 20 20 7b      ), */..    {
30a9a 32 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51  2,SQLITE_UTF8,SQ
30a9b 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53  LITE_FUNC_COALES
30a9c 43 45 2c 30 2c 30 2c 69 66 6e 75 6c 6c 46 75 6e  CE,0,0,ifnullFun
30a9d 63 2c 30 2c 30 2c 22 69 66 6e 75 6c 6c 22 2c 30  c,0,0,"ifnull",0
30a9e 2c 30 7d 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49  ,0},..    FUNCTI
30a9f 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20  ON(random,      
30aa0 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20         0, 0, 0, 
30aa1 72 61 6e 64 6f 6d 46 75 6e 63 20 20 20 20 20 20  randomFunc      
30aa2 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f   ),..    FUNCTIO
30aa3 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20  N(randomblob,   
30aa4 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72        1, 0, 0, r
30aa5 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20 20 20 20  andomBlob       
30aa6 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  ),..    FUNCTION
30aa7 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20 20 20  (nullif,        
30aa8 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 6e 75       2, 0, 1, nu
30aa9 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20 20 29  llifFunc       )
30aaa 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ,..    FUNCTION(
30aab 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c 20  sqlite_version, 
30aac 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65 72      0, 0, 0, ver
30aad 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29 2c  sionFunc      ),
30aae 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73  ..    FUNCTION(s
30aaf 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 2c  qlite_source_id,
30ab0 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f 75 72     0, 0, 0, sour
30ab1 63 65 69 64 46 75 6e 63 20 20 20 20 20 29 2c 0d  ceidFunc     ),.
30ab2 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71  .    FUNCTION(sq
30ab3 6c 69 74 65 5f 6c 6f 67 2c 20 20 20 20 20 20 20  lite_log,       
30ab4 20 20 32 2c 20 30 2c 20 30 2c 20 65 72 72 6c 6f    2, 0, 0, errlo
30ab5 67 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0d 0a  gFunc       ),..
30ab6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
30ab7 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f  MIT_COMPILEOPTIO
30ab8 4e 5f 44 49 41 47 53 0d 0a 20 20 20 20 46 55 4e  N_DIAGS..    FUN
30ab9 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 63 6f 6d  CTION(sqlite_com
30aba 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 2c  pileoption_used,
30abb 31 2c 20 30 2c 20 30 2c 20 63 6f 6d 70 69 6c 65  1, 0, 0, compile
30abc 6f 70 74 69 6f 6e 75 73 65 64 46 75 6e 63 20 20  optionusedFunc  
30abd 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  ),..    FUNCTION
30abe 28 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f  (sqlite_compileo
30abf 70 74 69 6f 6e 5f 67 65 74 2c 20 31 2c 20 30 2c  ption_get, 1, 0,
30ac0 20 30 2c 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f   0, compileoptio
30ac1 6e 67 65 74 46 75 6e 63 20 20 29 2c 0d 0a 23 65  ngetFunc  ),..#e
30ac2 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
30ac3 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f  MIT_COMPILEOPTIO
30ac4 4e 5f 44 49 41 47 53 20 2a 2f 0d 0a 20 20 20 20  N_DIAGS */..    
30ac5 46 55 4e 43 54 49 4f 4e 28 71 75 6f 74 65 2c 20  FUNCTION(quote, 
30ac6 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
30ac7 30 2c 20 30 2c 20 71 75 6f 74 65 46 75 6e 63 20  0, 0, quoteFunc 
30ac8 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 46         ),..    F
30ac9 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f 69 6e 73  UNCTION(last_ins
30aca 65 72 74 5f 72 6f 77 69 64 2c 20 20 30 2c 20 30  ert_rowid,  0, 0
30acb 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 65 72 74  , 0, last_insert
30acc 5f 72 6f 77 69 64 29 2c 0d 0a 20 20 20 20 46 55  _rowid),..    FU
30acd 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73 2c 20  NCTION(changes, 
30ace 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c             0, 0,
30acf 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20 20 20   0, changes     
30ad0 20 20 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e       ),..    FUN
30ad1 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61 6e  CTION(total_chan
30ad2 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c 20  ges,      0, 0, 
30ad3 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  0, total_changes
30ad4 20 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43      ),..    FUNC
30ad5 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20  TION(replace,   
30ad6 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30           3, 0, 0
30ad7 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20  , replaceFunc   
30ad8 20 20 20 29 2c 0d 0a 20 20 20 20 46 55 4e 43 54     ),..    FUNCT
30ad9 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20  ION(zeroblob,   
30ada 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
30adb 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20   zeroblobFunc   
30adc 20 20 29 2c 0d 0a 20 20 23 69 66 64 65 66 20 53    ),..  #ifdef S
30add 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0d 0a 20  QLITE_SOUNDEX.. 
30ade 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e     FUNCTION(soun
30adf 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  dex,            
30ae0 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78  1, 0, 0, soundex
30ae1 46 75 6e 63 20 20 20 20 20 20 29 2c 0d 0a 20 20  Func      ),..  
30ae2 23 65 6e 64 69 66 0d 0a 20 20 23 69 66 6e 64 65  #endif..  #ifnde
30ae3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
30ae4 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a 20 20  AD_EXTENSION..  
30ae5 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f    FUNCTION(load_
30ae6 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31  extension,     1
30ae7 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20  , 0, 0, loadExt 
30ae8 20 20 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 20           ),..   
30ae9 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65   FUNCTION(load_e
30aea 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c  xtension,     2,
30aeb 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20   0, 0, loadExt  
30aec 20 20 20 20 20 20 20 20 29 2c 0d 0a 20 20 23 65          ),..  #e
30aed 6e 64 69 66 0d 0a 20 20 20 20 41 47 47 52 45 47  ndif..    AGGREG
30aee 41 54 45 28 73 75 6d 2c 20 20 20 20 20 20 20 20  ATE(sum,        
30aef 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
30af0 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20  sumStep,        
30af1 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20 20 20   sumFinalize    
30af2 29 2c 0d 0a 20 20 20 20 41 47 47 52 45 47 41 54  ),..    AGGREGAT
30af3 45 28 74 6f 74 61 6c 2c 20 20 20 20 20 20 20 20  E(total,        
30af4 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75       1, 0, 0, su
30af5 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 74  mStep,         t
30af6 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20 20  otalFinalize    
30af7 29 2c 0d 0a 20 20 20 20 41 47 47 52 45 47 41 54  ),..    AGGREGAT
30af8 45 28 61 76 67 2c 20 20 20 20 20 20 20 20 20 20  E(avg,          
30af9 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75       1, 0, 0, su
30afa 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 61  mStep,         a
30afb 76 67 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c  vgFinalize    ),
30afc 0d 0a 20 2f 2a 20 41 47 47 52 45 47 41 54 45 28  .. /* AGGREGATE(
30afd 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20  count,          
30afe 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 6f 75 6e     0, 0, 0, coun
30aff 74 53 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75  tStep,       cou
30b00 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29 2c 20 2a  ntFinalize  ), *
30b01 2f 0d 0a 20 20 20 20 7b 30 2c 53 51 4c 49 54 45  /..    {0,SQLITE
30b02 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 46 55 4e  _UTF8,SQLITE_FUN
30b03 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30 2c 63 6f  C_COUNT,0,0,0,co
30b04 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74 46 69 6e  untStep,countFin
30b05 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22 2c 30 2c  alize,"count",0,
30b06 30 7d 2c 0d 0a 20 20 20 20 41 47 47 52 45 47 41  0},..    AGGREGA
30b07 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
30b08 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63        1, 0, 0, c
30b09 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
30b0a 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
30b0b 2c 0d 0a 20 20 20 20 41 47 47 52 45 47 41 54 45  ,..    AGGREGATE
30b0c 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20  (group_concat,  
30b0d 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 6f      1, 0, 0, gro
30b0e 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72  upConcatStep, gr
30b0f 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a  oupConcatFinaliz
30b10 65 29 2c 0d 0a 20 20 20 20 41 47 47 52 45 47 41  e),..    AGGREGA
30b11 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
30b12 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67        2, 0, 0, g
30b13 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
30b14 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
30b15 69 7a 65 29 2c 0d 0a 20 20 0d 0a 20 20 20 20 4c  ize),..  ..    L
30b16 49 4b 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c  IKEFUNC(glob, 2,
30b17 20 26 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49   &globInfo, SQLI
30b18 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c  TE_FUNC_LIKE|SQL
30b19 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0d  ITE_FUNC_CASE),.
30b1a 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  .  #ifdef SQLITE
30b1b 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f  _CASE_SENSITIVE_
30b1c 4c 49 4b 45 0d 0a 20 20 20 20 4c 49 4b 45 46 55  LIKE..    LIKEFU
30b1d 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b  NC(like, 2, &lik
30b1e 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45  eInfoAlt, SQLITE
30b1f 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54  _FUNC_LIKE|SQLIT
30b20 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0d 0a 20  E_FUNC_CASE),.. 
30b21 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65     LIKEFUNC(like
30b22 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c  , 3, &likeInfoAl
30b23 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  t, SQLITE_FUNC_L
30b24 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  IKE|SQLITE_FUNC_
30b25 43 41 53 45 29 2c 0d 0a 20 20 23 65 6c 73 65 0d  CASE),..  #else.
30b26 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69  .    LIKEFUNC(li
30b27 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f  ke, 2, &likeInfo
30b28 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e  Norm, SQLITE_FUN
30b29 43 5f 4c 49 4b 45 29 2c 0d 0a 20 20 20 20 4c 49  C_LIKE),..    LI
30b2a 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20  KEFUNC(like, 3, 
30b2b 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53  &likeInfoNorm, S
30b2c 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
30b2d 2c 0d 0a 20 20 23 65 6e 64 69 66 0d 0a 20 20 7d  ,..  #endif..  }
30b2e 3b 0d 0a 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  ;....  int i;.. 
30b2f 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
30b30 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75  ash = &GLOBAL(Fu
30b31 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74  ncDefHash, sqlit
30b32 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
30b33 73 29 3b 0d 0a 20 20 46 75 6e 63 44 65 66 20 2a  s);..  FuncDef *
30b34 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
30b35 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
30b36 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29  f, aBuiltinFunc)
30b37 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ;....  for(i=0; 
30b38 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69  i<ArraySize(aBui
30b39 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b  ltinFunc); i++){
30b3a 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e  ..    sqlite3Fun
30b3b 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68  cDefInsert(pHash
30b3c 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0d 0a 20  , &aFunc[i]);.. 
30b3d 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65 67   }..  sqlite3Reg
30b3e 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e  isterDateTimeFun
30b3f 63 74 69 6f 6e 73 28 29 3b 0d 0a 23 69 66 6e 64  ctions();..#ifnd
30b40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
30b41 4c 54 45 52 54 41 42 4c 45 0d 0a 20 20 73 71 6c  LTERTABLE..  sql
30b42 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f  ite3AlterFunctio
30b43 6e 73 28 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 7d  ns();..#endif..}
30b44 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
30b45 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 2e  *** End of func.
30b46 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
30b47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b49 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
30b4a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
30b4b 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fkey.c *********
30b4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b4e 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 0d 0a 2a 2a  ***/../*..**..**
30b4f 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
30b50 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
30b51 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
30b52 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
30b53 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
30b54 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
30b55 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
30b56 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
30b57 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
30b58 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
30b59 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
30b5a 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
30b5b 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
30b5c 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
30b5d 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
30b5e 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
30b5f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
30b60 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
30b61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b65 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  ..** This file c
30b66 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
30b67 64 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65  d by the compile
30b68 72 20 74 6f 20 61 64 64 20 66 6f 72 65 69 67 6e  r to add foreign
30b69 20 6b 65 79 0d 0a 2a 2a 20 73 75 70 70 6f 72 74   key..** support
30b6a 20 74 6f 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c   to compiled SQL
30b6b 20 73 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 2a 2f   statements...*/
30b6c 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
30b6d 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
30b6e 4b 45 59 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  KEY..#ifndef SQL
30b6f 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
30b70 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 66 65 72  ..../*..** Defer
30b71 72 65 64 20 61 6e 64 20 49 6d 6d 65 64 69 61 74  red and Immediat
30b72 65 20 46 4b 73 0d 0a 2a 2a 20 2d 2d 2d 2d 2d 2d  e FKs..** ------
30b73 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30b74 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72  ----..**..** For
30b75 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 53 51 4c  eign keys in SQL
30b76 69 74 65 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20  ite come in two 
30b77 66 6c 61 76 6f 75 72 73 3a 20 64 65 66 65 72 72  flavours: deferr
30b78 65 64 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65  ed and immediate
30b79 2e 0d 0a 2a 2a 20 49 66 20 61 6e 20 69 6d 6d 65  ...** If an imme
30b7a 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
30b7b 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  y constraint is 
30b7c 76 69 6f 6c 61 74 65 64 2c 20 53 51 4c 49 54 45  violated, SQLITE
30b7d 5f 43 4f 4e 53 54 52 41 49 4e 54 0d 0a 2a 2a 20  _CONSTRAINT..** 
30b7e 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
30b7f 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
30b80 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
30b81 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49  n rolled back. I
30b82 66 20 61 20 0d 0a 2a 2a 20 64 65 66 65 72 72 65  f a ..** deferre
30b83 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
30b84 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c  nstraint is viol
30b85 61 74 65 64 2c 20 6e 6f 20 61 63 74 69 6f 6e 20  ated, no action 
30b86 69 73 20 74 61 6b 65 6e 20 0d 0a 2a 2a 20 69 6d  is taken ..** im
30b87 6d 65 64 69 61 74 65 6c 79 2e 20 48 6f 77 65 76  mediately. Howev
30b88 65 72 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  er if the applic
30b89 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ation attempts t
30b8a 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 0d 0a 2a  o commit the ..*
30b8b 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  * transaction be
30b8c 66 6f 72 65 20 66 69 78 69 6e 67 20 74 68 65 20  fore fixing the 
30b8d 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
30b8e 74 69 6f 6e 2c 20 74 68 65 20 61 74 74 65 6d 70  tion, the attemp
30b8f 74 20 66 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  t fails...**..**
30b90 20 44 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   Deferred constr
30b91 61 69 6e 74 73 20 61 72 65 20 69 6d 70 6c 65 6d  aints are implem
30b92 65 6e 74 65 64 20 75 73 69 6e 67 20 61 20 73 69  ented using a si
30b93 6d 70 6c 65 20 63 6f 75 6e 74 65 72 20 61 73 73  mple counter ass
30b94 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68  ociated..** with
30b95 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
30b96 6e 64 6c 65 2e 20 54 68 65 20 63 6f 75 6e 74 65  ndle. The counte
30b97 72 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  r is set to zero
30b98 20 65 61 63 68 20 74 69 6d 65 20 61 20 0d 0a 2a   each time a ..*
30b99 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73  * database trans
30b9a 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
30b9b 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 73 74  . Each time a st
30b9c 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75  atement is execu
30b9d 74 65 64 20 0d 0a 2a 2a 20 74 68 61 74 20 63 61  ted ..** that ca
30b9e 75 73 65 73 20 61 20 66 6f 72 65 69 67 6e 20 6b  uses a foreign k
30b9f 65 79 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 74 68  ey violation, th
30ba0 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63  e counter is inc
30ba1 72 65 6d 65 6e 74 65 64 2e 20 45 61 63 68 0d 0a  remented. Each..
30ba2 2a 2a 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d  ** time a statem
30ba3 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 20  ent is executed 
30ba4 74 68 61 74 20 72 65 6d 6f 76 65 73 20 61 6e 20  that removes an 
30ba5 65 78 69 73 74 69 6e 67 20 76 69 6f 6c 61 74 69  existing violati
30ba6 6f 6e 20 66 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20  on from..** the 
30ba7 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f  database, the co
30ba8 75 6e 74 65 72 20 69 73 20 64 65 63 72 65 6d 65  unter is decreme
30ba9 6e 74 65 64 2e 20 57 68 65 6e 20 74 68 65 20 74  nted. When the t
30baa 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0d 0a 2a  ransaction is..*
30bab 2a 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 65  * committed, the
30bac 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 69 66   commit fails if
30bad 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
30bae 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65  ue of the counte
30baf 72 20 69 73 0d 0a 2a 2a 20 67 72 65 61 74 65 72  r is..** greater
30bb0 20 74 68 61 6e 20 7a 65 72 6f 2e 20 54 68 69 73   than zero. This
30bb1 20 73 63 68 65 6d 65 20 68 61 73 20 74 77 6f 20   scheme has two 
30bb2 62 69 67 20 64 72 61 77 62 61 63 6b 73 3a 0d 0a  big drawbacks:..
30bb3 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 57 68 65 6e 20  **..**   * When 
30bb4 61 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 64  a commit fails d
30bb5 75 65 20 74 6f 20 61 20 64 65 66 65 72 72 65 64  ue to a deferred
30bb6 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
30bb7 73 74 72 61 69 6e 74 2c 20 0d 0a 2a 2a 20 20 20  straint, ..**   
30bb8 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61    there is no wa
30bb9 79 20 74 6f 20 74 65 6c 6c 20 77 68 69 63 68 20  y to tell which 
30bba 66 6f 72 65 69 67 6e 20 63 6f 6e 73 74 72 61 69  foreign constrai
30bbb 6e 74 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66  nt is not satisf
30bbc 69 65 64 2c 0d 0a 2a 2a 20 20 20 20 20 6f 72 20  ied,..**     or 
30bbd 77 68 69 63 68 20 72 6f 77 20 69 74 20 69 73 20  which row it is 
30bbe 6e 6f 74 20 73 61 74 69 73 66 69 65 64 20 66 6f  not satisfied fo
30bbf 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 49  r...**..**   * I
30bc0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
30bc1 6f 6e 74 61 69 6e 73 20 66 6f 72 65 69 67 6e 20  ontains foreign 
30bc2 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 73 20 77  key violations w
30bc3 68 65 6e 20 74 68 65 20 0d 0a 2a 2a 20 20 20 20  hen the ..**    
30bc4 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
30bc5 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 6d 61 79  opened, this may
30bc6 20 63 61 75 73 65 20 74 68 65 20 6d 65 63 68 61   cause the mecha
30bc7 6e 69 73 6d 20 74 6f 20 6d 61 6c 66 75 6e 63 74  nism to malfunct
30bc8 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 65 73  ion...**..** Des
30bc9 70 69 74 65 20 74 68 65 73 65 20 70 72 6f 62 6c  pite these probl
30bca 65 6d 73 2c 20 74 68 69 73 20 61 70 70 72 6f 61  ems, this approa
30bcb 63 68 20 69 73 20 61 64 6f 70 74 65 64 20 61 73  ch is adopted as
30bcc 20 69 74 20 73 65 65 6d 73 20 73 69 6d 70 6c 65   it seems simple
30bcd 72 0d 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61  r..** than the a
30bce 6c 74 65 72 6e 61 74 69 76 65 73 2e 0d 0a 2a 2a  lternatives...**
30bcf 0d 0a 2a 2a 20 49 4e 53 45 52 54 20 6f 70 65 72  ..** INSERT oper
30bd0 61 74 69 6f 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ations:..**..** 
30bd1 20 20 49 2e 31 29 20 46 6f 72 20 65 61 63 68 20    I.1) For each 
30bd2 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  FK for which the
30bd3 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63 68   table is the ch
30bd4 69 6c 64 20 74 61 62 6c 65 2c 20 73 65 61 72 63  ild table, searc
30bd5 68 0d 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65  h..**        the
30bd6 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f   parent table fo
30bd7 72 20 61 20 6d 61 74 63 68 2e 20 49 66 20 6e 6f  r a match. If no
30bd8 6e 65 20 69 73 20 66 6f 75 6e 64 20 69 6e 63 72  ne is found incr
30bd9 65 6d 65 6e 74 20 74 68 65 0d 0a 2a 2a 20 20 20  ement the..**   
30bda 20 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 20       constraint 
30bdb 63 6f 75 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a  counter...**..**
30bdc 20 20 20 49 2e 32 29 20 46 6f 72 20 65 61 63 68     I.2) For each
30bdd 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68   FK for which th
30bde 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70  e table is the p
30bdf 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 0d 0a 2a  arent table, ..*
30be0 2a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 20  *        search 
30be1 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
30be2 66 6f 72 20 72 6f 77 73 20 74 68 61 74 20 63 6f  for rows that co
30be3 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
30be4 6e 65 77 0d 0a 2a 2a 20 20 20 20 20 20 20 20 72  new..**        r
30be5 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ow in the parent
30be6 20 74 61 62 6c 65 2e 20 44 65 63 72 65 6d 65 6e   table. Decremen
30be7 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20 66 6f  t the counter fo
30be8 72 20 65 61 63 68 20 72 6f 77 0d 0a 2a 2a 20 20  r each row..**  
30be9 20 20 20 20 20 20 66 6f 75 6e 64 20 28 61 73 20        found (as 
30bea 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  the constraint i
30beb 73 20 6e 6f 77 20 73 61 74 69 73 66 69 65 64 29  s now satisfied)
30bec 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 45 4c 45 54 45  ...**..** DELETE
30bed 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0d 0a 2a 2a   operations:..**
30bee 0d 0a 2a 2a 20 20 20 44 2e 31 29 20 46 6f 72 20  ..**   D.1) For 
30bef 65 61 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63  each FK for whic
30bf0 68 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  h the table is t
30bf1 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20  he child table, 
30bf2 0d 0a 2a 2a 20 20 20 20 20 20 20 20 73 65 61 72  ..**        sear
30bf3 63 68 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  ch the parent ta
30bf4 62 6c 65 20 66 6f 72 20 61 20 72 6f 77 20 74 68  ble for a row th
30bf5 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  at corresponds t
30bf6 6f 20 74 68 65 20 0d 0a 2a 2a 20 20 20 20 20 20  o the ..**      
30bf7 20 20 64 65 6c 65 74 65 64 20 72 6f 77 20 69 6e    deleted row in
30bf8 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
30bf9 2e 20 49 66 20 73 75 63 68 20 61 20 72 6f 77 20  . If such a row 
30bfa 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 0d 0a  is not found, ..
30bfb 2a 2a 20 20 20 20 20 20 20 20 64 65 63 72 65 6d  **        decrem
30bfc 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 65 72 2e  ent the counter.
30bfd 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 44 2e 32 29 20  ..**..**   D.2) 
30bfe 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f 72 20  For each FK for 
30bff 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65 20  which the table 
30c00 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  is the parent ta
30c01 62 6c 65 2c 20 73 65 61 72 63 68 20 0d 0a 2a 2a  ble, search ..**
30c02 20 20 20 20 20 20 20 20 74 68 65 20 63 68 69 6c          the chil
30c03 64 20 74 61 62 6c 65 20 66 6f 72 20 72 6f 77 73  d table for rows
30c04 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
30c05 20 74 6f 20 74 68 65 20 64 65 6c 65 74 65 64 20   to the deleted 
30c06 72 6f 77 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20  row ..**        
30c07 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  in the parent ta
30c08 62 6c 65 2e 20 46 6f 72 20 65 61 63 68 20 66 6f  ble. For each fo
30c09 75 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  und increment th
30c0a 65 20 63 6f 75 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a  e counter...**..
30c0b 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61 74  ** UPDATE operat
30c0c 69 6f 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ions:..**..**   
30c0d 41 6e 20 55 50 44 41 54 45 20 63 6f 6d 6d 61 6e  An UPDATE comman
30c0e 64 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20  d requires that 
30c0f 61 6c 6c 20 34 20 73 74 65 70 73 20 61 62 6f 76  all 4 steps abov
30c10 65 20 61 72 65 20 74 61 6b 65 6e 2c 20 62 75 74  e are taken, but
30c11 20 6f 6e 6c 79 0d 0a 2a 2a 20 20 20 66 6f 72 20   only..**   for 
30c12 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  FK constraints f
30c13 6f 72 20 77 68 69 63 68 20 74 68 65 20 61 66 66  or which the aff
30c14 65 63 74 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72  ected columns ar
30c15 65 20 61 63 74 75 61 6c 6c 79 20 0d 0a 2a 2a 20  e actually ..** 
30c16 20 20 6d 6f 64 69 66 69 65 64 20 28 76 61 6c 75    modified (valu
30c17 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61  es must be compa
30c18 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65 29 2e  red at runtime).
30c19 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68  ..**..** Note th
30c1a 61 74 20 49 2e 31 20 61 6e 64 20 44 2e 31 20 61  at I.1 and D.1 a
30c1b 72 65 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20  re very similar 
30c1c 6f 70 65 72 61 74 69 6f 6e 73 2c 20 61 73 20 61  operations, as a
30c1d 72 65 20 49 2e 32 20 61 6e 64 20 44 2e 32 2e 0d  re I.2 and D.2..
30c1e 0a 2a 2a 20 54 68 69 73 20 73 69 6d 70 6c 69 66  .** This simplif
30c1f 69 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ies the implemen
30c20 74 61 74 69 6f 6e 20 61 20 62 69 74 2e 0d 0a 2a  tation a bit...*
30c21 2a 0d 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75  *..** For the pu
30c22 72 70 6f 73 65 73 20 6f 66 20 69 6d 6d 65 64 69  rposes of immedi
30c23 61 74 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  ate FK constrain
30c24 74 73 2c 20 74 68 65 20 4f 52 20 52 45 50 4c 41  ts, the OR REPLA
30c25 43 45 20 63 6f 6e 66 6c 69 63 74 0d 0a 2a 2a 20  CE conflict..** 
30c26 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 63 6f  resolution is co
30c27 6e 73 69 64 65 72 65 64 20 74 6f 20 64 65 6c 65  nsidered to dele
30c28 74 65 20 72 6f 77 73 20 62 65 66 6f 72 65 20 74  te rows before t
30c29 68 65 20 6e 65 77 20 72 6f 77 20 69 73 20 69 6e  he new row is in
30c2a 73 65 72 74 65 64 2e 0d 0a 2a 2a 20 49 66 20 61  serted...** If a
30c2b 20 64 65 6c 65 74 65 20 63 61 75 73 65 64 20 62   delete caused b
30c2c 79 20 4f 52 20 52 45 50 4c 41 43 45 20 76 69 6f  y OR REPLACE vio
30c2d 6c 61 74 65 73 20 61 6e 20 46 4b 20 63 6f 6e 73  lates an FK cons
30c2e 74 72 61 69 6e 74 2c 20 61 6e 20 65 78 63 65 70  traint, an excep
30c2f 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20 74 68 72 6f  tion..** is thro
30c30 77 6e 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20  wn, even if the 
30c31 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 77 6f  FK constraint wo
30c32 75 6c 64 20 62 65 20 73 61 74 69 73 66 69 65 64  uld be satisfied
30c33 20 61 66 74 65 72 20 74 68 65 20 6e 65 77 20 0d   after the new .
30c34 0a 2a 2a 20 72 6f 77 20 69 73 20 69 6e 73 65 72  .** row is inser
30c35 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6d 6d  ted...**..** Imm
30c36 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e  ediate constrain
30c37 74 73 20 61 72 65 20 75 73 75 61 6c 6c 79 20 68  ts are usually h
30c38 61 6e 64 6c 65 64 20 73 69 6d 69 6c 61 72 6c 79  andled similarly
30c39 2e 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  . The only diffe
30c3a 72 65 6e 63 65 20 0d 0a 2a 2a 20 69 73 20 74 68  rence ..** is th
30c3b 61 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20 75  at the counter u
30c3c 73 65 64 20 69 73 20 73 74 6f 72 65 64 20 61 73  sed is stored as
30c3d 20 70 61 72 74 20 6f 66 20 65 61 63 68 20 69 6e   part of each in
30c3e 64 69 76 69 64 75 61 6c 20 73 74 61 74 65 6d 65  dividual stateme
30c3f 6e 74 0d 0a 2a 2a 20 6f 62 6a 65 63 74 20 28 73  nt..** object (s
30c40 74 72 75 63 74 20 56 64 62 65 29 2e 20 49 66 2c  truct Vdbe). If,
30c41 20 61 66 74 65 72 20 74 68 65 20 73 74 61 74 65   after the state
30c42 6d 65 6e 74 20 68 61 73 20 72 75 6e 2c 20 69 74  ment has run, it
30c43 73 20 69 6d 6d 65 64 69 61 74 65 0d 0a 2a 2a 20  s immediate..** 
30c44 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
30c45 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68  er is greater th
30c46 61 6e 20 7a 65 72 6f 2c 20 69 74 20 72 65 74 75  an zero, it retu
30c47 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rns SQLITE_CONST
30c48 52 41 49 4e 54 0d 0a 2a 2a 20 61 6e 64 20 74 68  RAINT..** and th
30c49 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
30c4a 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
30c4b 64 20 62 61 63 6b 2e 20 41 6e 20 65 78 63 65 70  d back. An excep
30c4c 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52  tion is an INSER
30c4d 54 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  T..** statement 
30c4e 74 68 61 74 20 69 6e 73 65 72 74 73 20 61 20 73  that inserts a s
30c4f 69 6e 67 6c 65 20 72 6f 77 20 6f 6e 6c 79 20 28  ingle row only (
30c50 6e 6f 20 74 72 69 67 67 65 72 73 29 2e 20 49 6e  no triggers). In
30c51 20 74 68 69 73 20 63 61 73 65 2c 0d 0a 2a 2a 20   this case,..** 
30c52 69 6e 73 74 65 61 64 20 6f 66 20 75 73 69 6e 67  instead of using
30c53 20 61 20 63 6f 75 6e 74 65 72 2c 20 61 6e 20 65   a counter, an e
30c54 78 63 65 70 74 69 6f 6e 20 69 73 20 74 68 72 6f  xception is thro
30c55 77 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  wn immediately i
30c56 66 20 74 68 65 0d 0a 2a 2a 20 49 4e 53 45 52 54  f the..** INSERT
30c57 20 76 69 6f 6c 61 74 65 73 20 61 20 66 6f 72 65   violates a fore
30c58 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
30c59 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 65 63 65  nt. This is nece
30c5a 73 73 61 72 79 20 61 73 20 73 75 63 68 0d 0a 2a  ssary as such..*
30c5b 2a 20 61 6e 20 49 4e 53 45 52 54 20 64 6f 65 73  * an INSERT does
30c5c 20 6e 6f 74 20 6f 70 65 6e 20 61 20 73 74 61 74   not open a stat
30c5d 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
30c5e 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 4f 44 4f 3a  n...**..** TODO:
30c5f 20 48 6f 77 20 73 68 6f 75 6c 64 20 64 72 6f 70   How should drop
30c60 70 69 6e 67 20 61 20 74 61 62 6c 65 20 62 65 20  ping a table be 
30c61 68 61 6e 64 6c 65 64 3f 20 48 6f 77 20 73 68 6f  handled? How sho
30c62 75 6c 64 20 72 65 6e 61 6d 69 6e 67 20 61 20 0d  uld renaming a .
30c63 0a 2a 2a 20 74 61 62 6c 65 20 62 65 20 68 61 6e  .** table be han
30c64 64 6c 65 64 3f 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a  dled?..**..**..*
30c65 2a 20 51 75 65 72 79 20 41 50 49 20 4e 6f 74 65  * Query API Note
30c66 73 0d 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s..** ----------
30c67 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20 42 65  -----..**..** Be
30c68 66 6f 72 65 20 63 6f 64 69 6e 67 20 61 6e 20 55  fore coding an U
30c69 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
30c6a 72 6f 77 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74  row operation, t
30c6b 68 65 20 63 6f 64 65 2d 67 65 6e 65 72 61 74 6f  he code-generato
30c6c 72 0d 0a 2a 2a 20 66 6f 72 20 74 68 6f 73 65 20  r..** for those 
30c6d 74 77 6f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6e  two operations n
30c6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  eeds to know whe
30c6f 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
30c70 6f 70 65 72 61 74 69 6f 6e 0d 0a 2a 2a 20 72 65  operation..** re
30c71 71 75 69 72 65 73 20 61 6e 79 20 46 4b 20 70 72  quires any FK pr
30c72 6f 63 65 73 73 69 6e 67 20 61 6e 64 2c 20 69 66  ocessing and, if
30c73 20 73 6f 2c 20 77 68 69 63 68 20 63 6f 6c 75 6d   so, which colum
30c74 6e 73 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  ns of the origin
30c75 61 6c 0d 0a 2a 2a 20 72 6f 77 20 61 72 65 20 72  al..** row are r
30c76 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 46  equired by the F
30c77 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 56 44 42  K processing VDB
30c78 45 20 63 6f 64 65 20 28 69 2e 65 2e 20 69 66 20  E code (i.e. if 
30c79 46 4b 73 20 77 65 72 65 0d 0a 2a 2a 20 69 6d 70  FKs were..** imp
30c7a 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74  lemented using t
30c7b 72 69 67 67 65 72 73 2c 20 77 68 69 63 68 20 6f  riggers, which o
30c7c 66 20 74 68 65 20 6f 6c 64 2e 2a 20 63 6f 6c 75  f the old.* colu
30c7d 6d 6e 73 20 77 6f 75 6c 64 20 62 65 20 0d 0a 2a  mns would be ..*
30c7e 2a 20 61 63 63 65 73 73 65 64 29 2e 20 4e 6f 20  * accessed). No 
30c7f 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72  information is r
30c80 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63  equired by the c
30c81 6f 64 65 2d 67 65 6e 65 72 61 74 6f 72 20 62 65  ode-generator be
30c82 66 6f 72 65 0d 0a 2a 2a 20 63 6f 64 69 6e 67 20  fore..** coding 
30c83 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74  an INSERT operat
30c84 69 6f 6e 2e 20 54 68 65 20 66 75 6e 63 74 69 6f  ion. The functio
30c85 6e 73 20 75 73 65 64 20 62 79 20 74 68 65 20 55  ns used by the U
30c86 50 44 41 54 45 2f 44 45 4c 45 54 45 0d 0a 2a 2a  PDATE/DELETE..**
30c87 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f 64 65   generation code
30c88 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20 74 68   to query for th
30c89 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  is information a
30c8a 72 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 73 71  re:..**..**   sq
30c8b 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28  lite3FkRequired(
30c8c 29 20 2d 20 54 65 73 74 20 74 6f 20 73 65 65 20  ) - Test to see 
30c8d 69 66 20 46 4b 20 70 72 6f 63 65 73 73 69 6e 67  if FK processing
30c8e 20 69 73 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a   is required...*
30c8f 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64  *   sqlite3FkOld
30c90 6d 61 73 6b 28 29 20 20 2d 20 51 75 65 72 79 20  mask()  - Query 
30c91 66 6f 72 20 74 68 65 20 73 65 74 20 6f 66 20 72  for the set of r
30c92 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a 20 63 6f  equired old.* co
30c93 6c 75 6d 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a  lumns...**..**..
30c94 2a 2a 20 45 78 74 65 72 6e 61 6c 6c 79 20 61 63  ** Externally ac
30c95 63 65 73 73 69 62 6c 65 20 6d 6f 64 75 6c 65 20  cessible module 
30c96 66 75 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 2d 2d  functions..** --
30c97 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30c98 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30c99 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 73  ----..**..**   s
30c9a 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 29 20  qlite3FkCheck() 
30c9b 20 20 20 2d 20 43 68 65 63 6b 20 66 6f 72 20 66     - Check for f
30c9c 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61  oreign key viola
30c9d 74 69 6f 6e 73 2e 0d 0a 2a 2a 20 20 20 73 71 6c  tions...**   sql
30c9e 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 29 20  ite3FkActions() 
30c9f 20 2d 20 43 6f 64 65 20 74 72 69 67 67 65 72 73   - Code triggers
30ca0 20 66 6f 72 20 4f 4e 20 55 50 44 41 54 45 2f 4f   for ON UPDATE/O
30ca1 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 73  N DELETE actions
30ca2 2e 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46  ...**   sqlite3F
30ca3 6b 44 65 6c 65 74 65 28 29 20 20 20 2d 20 44 65  kDelete()   - De
30ca4 6c 65 74 65 20 61 6e 20 46 4b 65 79 20 73 74 72  lete an FKey str
30ca5 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f  ucture...*/..../
30ca6 2a 0d 0a 2a 2a 20 56 44 42 45 20 43 61 6c 6c 69  *..** VDBE Calli
30ca7 6e 67 20 43 6f 6e 76 65 6e 74 69 6f 6e 0d 0a 2a  ng Convention..*
30ca8 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
30ca9 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a  ---------..**..*
30caa 2a 20 45 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a  * Example:..**..
30cab 2a 2a 20 20 20 46 6f 72 20 74 68 65 20 66 6f 6c  **   For the fol
30cac 6c 6f 77 69 6e 67 20 49 4e 53 45 52 54 20 73 74  lowing INSERT st
30cad 61 74 65 6d 65 6e 74 3a 0d 0a 2a 2a 0d 0a 2a 2a  atement:..**..**
30cae 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
30caf 45 20 74 31 28 61 2c 20 62 20 49 4e 54 45 47 45  E t1(a, b INTEGE
30cb0 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63  R PRIMARY KEY, c
30cb1 29 3b 0d 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52  );..**     INSER
30cb2 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
30cb3 28 31 2c 20 32 2c 20 33 2e 31 29 3b 0d 0a 2a 2a  (1, 2, 3.1);..**
30cb4 0d 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20  ..**   Register 
30cb5 28 78 29 3a 20 20 20 20 20 20 20 20 32 20 20 20  (x):        2   
30cb6 20 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0d   (type integer).
30cb7 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28  .**   Register (
30cb8 78 2b 31 29 3a 20 20 20 20 20 20 31 20 20 20 20  x+1):      1    
30cb9 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0d 0a  (type integer)..
30cba 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78  **   Register (x
30cbb 2b 32 29 3a 20 20 20 20 20 20 4e 55 4c 4c 20 28  +2):      NULL (
30cbc 74 79 70 65 20 4e 55 4c 4c 29 0d 0a 2a 2a 20 20  type NULL)..**  
30cbd 20 52 65 67 69 73 74 65 72 20 28 78 2b 33 29 3a   Register (x+3):
30cbe 20 20 20 20 20 20 33 2e 31 20 20 28 74 79 70 65        3.1  (type
30cbf 20 72 65 61 6c 29 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a   real)..*/..../*
30cc0 0d 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b  ..** A foreign k
30cc1 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65  ey constraint re
30cc2 71 75 69 72 65 73 20 74 68 61 74 20 74 68 65 20  quires that the 
30cc3 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  key columns in t
30cc4 68 65 20 70 61 72 65 6e 74 0d 0a 2a 2a 20 74 61  he parent..** ta
30cc5 62 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 69  ble are collecti
30cc6 76 65 6c 79 20 73 75 62 6a 65 63 74 20 74 6f 20  vely subject to 
30cc7 61 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d  a UNIQUE or PRIM
30cc8 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69  ARY KEY constrai
30cc9 6e 74 2e 0d 0a 2a 2a 20 47 69 76 65 6e 20 74 68  nt...** Given th
30cca 61 74 20 70 50 61 72 65 6e 74 20 69 73 20 74 68  at pParent is th
30ccb 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66  e parent table f
30ccc 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  or foreign key c
30ccd 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65 79 2c  onstraint pFKey,
30cce 20 0d 0a 2a 2a 20 73 65 61 72 63 68 20 74 68 65   ..** search the
30ccf 20 73 63 68 65 6d 61 20 61 20 75 6e 69 71 75 65   schema a unique
30cd0 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 61   index on the pa
30cd1 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
30cd2 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75  . ..**..** If su
30cd3 63 63 65 73 73 66 75 6c 2c 20 7a 65 72 6f 20 69  ccessful, zero i
30cd4 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74  s returned. If t
30cd5 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 69 73  he parent key is
30cd6 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
30cd7 41 52 59 20 0d 0a 2a 2a 20 4b 45 59 20 63 6f 6c  ARY ..** KEY col
30cd8 75 6d 6e 2c 20 74 68 65 6e 20 6f 75 74 70 75 74  umn, then output
30cd9 20 76 61 72 69 61 62 6c 65 20 2a 70 70 49 64 78   variable *ppIdx
30cda 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
30cdb 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70 49   Otherwise, *ppI
30cdc 64 78 20 0d 0a 2a 2a 20 69 73 20 73 65 74 20 74  dx ..** is set t
30cdd 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 75  o point to the u
30cde 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 0d 0a 2a  nique index. ..*
30cdf 2a 20 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  * ..** If the pa
30ce0 72 65 6e 74 20 6b 65 79 20 63 6f 6e 73 69 73 74  rent key consist
30ce1 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 63 6f  s of a single co
30ce2 6c 75 6d 6e 20 28 74 68 65 20 66 6f 72 65 69 67  lumn (the foreig
30ce3 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
30ce4 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 6f  ..** is not a co
30ce5 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20  mposite foreign 
30ce6 6b 65 79 29 2c 20 6f 75 74 70 75 74 20 76 61 72  key), output var
30ce7 69 61 62 6c 65 20 2a 70 61 69 43 6f 6c 20 69 73  iable *paiCol is
30ce8 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0d 0a 2a   set to NULL...*
30ce9 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  * Otherwise, it 
30cea 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
30ceb 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  to an allocated 
30cec 61 72 72 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c  array of size N,
30ced 20 77 68 65 72 65 0d 0a 2a 2a 20 4e 20 69 73 20   where..** N is 
30cee 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
30cef 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72  lumns in the par
30cf0 65 6e 74 20 6b 65 79 2e 20 54 68 65 20 66 69 72  ent key. The fir
30cf1 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  st element of th
30cf2 65 0d 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 74  e..** array is t
30cf3 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
30cf4 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75  child table colu
30cf5 6d 6e 20 74 68 61 74 20 69 73 20 6d 61 70 70 65  mn that is mappe
30cf6 64 20 62 79 20 74 68 65 20 46 4b 0d 0a 2a 2a 20  d by the FK..** 
30cf7 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68  constraint to th
30cf8 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 63  e parent table c
30cf9 6f 6c 75 6d 6e 20 73 74 6f 72 65 64 20 69 6e 20  olumn stored in 
30cfa 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f  the left-most co
30cfb 6c 75 6d 6e 0d 0a 2a 2a 20 6f 66 20 69 6e 64 65  lumn..** of inde
30cfc 78 20 2a 70 70 49 64 78 2e 20 54 68 65 20 73 65  x *ppIdx. The se
30cfd 63 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 6f 66 20  cond element of 
30cfe 74 68 65 20 61 72 72 61 79 20 69 73 20 74 68 65  the array is the
30cff 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0d 0a 2a   index of the..*
30d00 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f  * child table co
30d01 6c 75 6d 6e 20 74 68 61 74 20 63 6f 72 72 65 73  lumn that corres
30d02 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 73 65 63  ponds to the sec
30d03 6f 6e 64 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f  ond left-most co
30d04 6c 75 6d 6e 20 6f 66 0d 0a 2a 2a 20 2a 70 70 49  lumn of..** *ppI
30d05 64 78 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0d 0a  dx, and so on...
30d06 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  **..** If the re
30d07 71 75 69 72 65 64 20 69 6e 64 65 78 20 63 61 6e  quired index can
30d08 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 65 69  not be found, ei
30d09 74 68 65 72 20 62 65 63 61 75 73 65 3a 0d 0a 2a  ther because:..*
30d0a 2a 0d 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 6e  *..**   1) The n
30d0b 61 6d 65 64 20 70 61 72 65 6e 74 20 6b 65 79 20  amed parent key 
30d0c 63 6f 6c 75 6d 6e 73 20 64 6f 20 6e 6f 74 20 65  columns do not e
30d0d 78 69 73 74 2c 20 6f 72 0d 0a 2a 2a 0d 0a 2a 2a  xist, or..**..**
30d0e 20 20 20 32 29 20 54 68 65 20 6e 61 6d 65 64 20     2) The named 
30d0f 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d  parent key colum
30d10 6e 73 20 64 6f 20 65 78 69 73 74 2c 20 62 75 74  ns do exist, but
30d11 20 61 72 65 20 6e 6f 74 20 73 75 62 6a 65 63 74   are not subject
30d12 20 74 6f 20 61 0d 0a 2a 2a 20 20 20 20 20 20 55   to a..**      U
30d13 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59  NIQUE or PRIMARY
30d14 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 2c   KEY constraint,
30d15 20 6f 72 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 29   or..**..**   3)
30d16 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79 20 63   No parent key c
30d17 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72 6f 76  olumns were prov
30d18 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ided explicitly 
30d19 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0d 0a  as part of the..
30d1a 2a 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e 20  **      foreign 
30d1b 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20  key definition, 
30d1c 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74  and the parent t
30d1d 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 68 61  able does not ha
30d1e 76 65 20 61 0d 0a 2a 2a 20 20 20 20 20 20 50 52  ve a..**      PR
30d1f 49 4d 41 52 59 20 4b 45 59 2c 20 6f 72 0d 0a 2a  IMARY KEY, or..*
30d20 2a 0d 0a 2a 2a 20 20 20 34 29 20 4e 6f 20 70 61  *..**   4) No pa
30d21 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
30d22 20 77 65 72 65 20 70 72 6f 76 69 64 65 64 20 65   were provided e
30d23 78 70 6c 69 63 69 74 6c 79 20 61 73 20 70 61 72  xplicitly as par
30d24 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 20 20 20  t of the..**    
30d25 20 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65    foreign key de
30d26 66 69 6e 69 74 69 6f 6e 2c 20 61 6e 64 20 74 68  finition, and th
30d27 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66  e PRIMARY KEY of
30d28 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
30d29 65 20 0d 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73  e ..**      cons
30d2a 69 73 74 73 20 6f 66 20 61 20 61 20 64 69 66 66  ists of a a diff
30d2b 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  erent number of 
30d2c 63 6f 6c 75 6d 6e 73 20 74 6f 20 74 68 65 20 63  columns to the c
30d2d 68 69 6c 64 20 6b 65 79 20 69 6e 20 0d 0a 2a 2a  hild key in ..**
30d2e 20 20 20 20 20 20 74 68 65 20 63 68 69 6c 64 20        the child 
30d2f 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 74  table...**..** t
30d30 68 65 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20  hen non-zero is 
30d31 72 65 74 75 72 6e 65 64 2c 20 61 6e 64 20 61 20  returned, and a 
30d32 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73  "foreign key mis
30d33 6d 61 74 63 68 22 20 65 72 72 6f 72 20 6c 6f 61  match" error loa
30d34 64 65 64 0d 0a 2a 2a 20 69 6e 74 6f 20 70 50 61  ded..** into pPa
30d35 72 73 65 2e 20 49 66 20 61 6e 20 4f 4f 4d 20 65  rse. If an OOM e
30d36 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6e 6f 6e  rror occurs, non
30d37 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65  -zero is returne
30d38 64 20 61 6e 64 20 74 68 65 0d 0a 2a 2a 20 70 50  d and the..** pP
30d39 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
30d3a 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 73  Failed flag is s
30d3b 65 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  et...*/..static 
30d3c 69 6e 74 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e  int locateFkeyIn
30d3d 64 65 78 28 0d 0a 20 20 50 61 72 73 65 20 2a 70  dex(..  Parse *p
30d3e 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
30d3f 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
30d40 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72   context to stor
30d41 65 20 61 6e 79 20 65 72 72 6f 72 20 69 6e 20 2a  e any error in *
30d42 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 50 61 72  /..  Table *pPar
30d43 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ent,            
30d44 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 74       /* Parent t
30d45 61 62 6c 65 20 6f 66 20 46 4b 20 63 6f 6e 73 74  able of FK const
30d46 72 61 69 6e 74 20 70 46 4b 65 79 20 2a 2f 0d 0a  raint pFKey */..
30d47 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20    FKey *pFKey,  
30d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d49 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
30d4a 20 74 6f 20 66 69 6e 64 20 69 6e 64 65 78 20 66   to find index f
30d4b 6f 72 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a  or */..  Index *
30d4c 2a 70 70 49 64 78 2c 20 20 20 20 20 20 20 20 20  *ppIdx,         
30d4d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
30d4e 20 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e   Unique index on
30d4f 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f   parent table */
30d50 0d 0a 20 20 69 6e 74 20 2a 2a 70 61 69 43 6f 6c  ..  int **paiCol
30d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d52 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 20      /* OUT: Map 
30d53 6f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73  of index columns
30d54 20 69 6e 20 70 46 4b 65 79 20 2a 2f 0d 0a 29 7b   in pFKey */..){
30d55 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20  ..  Index *pIdx 
30d56 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
30d57 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
30d58 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a   to return via *
30d59 70 70 49 64 78 20 2a 2f 0d 0a 20 20 69 6e 74 20  ppIdx */..  int 
30d5a 2a 61 69 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20  *aiCol = 0;     
30d5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d5c 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
30d5d 72 6e 20 76 69 61 20 2a 70 61 69 43 6f 6c 20 2a  rn via *paiCol *
30d5e 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20  /..  int nCol = 
30d5f 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 20 20 20  pFKey->nCol;    
30d60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
30d61 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
30d62 20 70 61 72 65 6e 74 20 6b 65 79 20 2a 2f 0d 0a   parent key */..
30d63 20 20 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70    char *zKey = p
30d64 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 7a 43  FKey->aCol[0].zC
30d65 6f 6c 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ol;   /* Name of
30d66 20 6c 65 66 74 2d 6d 6f 73 74 20 70 61 72 65 6e   left-most paren
30d67 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 2a 2f 0d  t key column */.
30d68 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c  ...  /* The call
30d69 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  er is responsibl
30d6a 65 20 66 6f 72 20 7a 65 72 6f 69 6e 67 20 6f 75  e for zeroing ou
30d6b 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 2e  tput parameters.
30d6c 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
30d6d 70 49 64 78 20 26 26 20 2a 70 70 49 64 78 3d 3d  pIdx && *ppIdx==
30d6e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
30d6f 21 70 61 69 43 6f 6c 20 7c 7c 20 2a 70 61 69 43  !paiCol || *paiC
30d70 6f 6c 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65  ol==0 );..  asse
30d71 72 74 28 20 70 50 61 72 73 65 20 29 3b 0d 0a 0d  rt( pParse );...
30d72 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
30d73 20 61 20 6e 6f 6e 2d 63 6f 6d 70 6f 73 69 74 65   a non-composite
30d74 20 28 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 29   (single column)
30d75 20 66 6f 72 65 69 67 6e 20 6b 65 79 2c 20 63 68   foreign key, ch
30d76 65 63 6b 20 69 66 20 69 74 20 0d 0a 20 20 2a 2a  eck if it ..  **
30d77 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49 4e 54   maps to the INT
30d78 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
30d79 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e   of table pParen
30d7a 74 2e 20 49 66 20 73 6f 2c 20 6c 65 61 76 65 20  t. If so, leave 
30d7b 2a 70 70 49 64 78 20 0d 0a 20 20 2a 2a 20 61 6e  *ppIdx ..  ** an
30d7c 64 20 2a 70 61 69 43 6f 6c 20 73 65 74 20 74 6f  d *paiCol set to
30d7d 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
30d7e 20 65 61 72 6c 79 2e 20 0d 0a 20 20 2a 2a 0d 0a   early. ..  **..
30d7f 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    ** Otherwise, 
30d80 66 6f 72 20 61 20 63 6f 6d 70 6f 73 69 74 65 20  for a composite 
30d81 66 6f 72 65 69 67 6e 20 6b 65 79 20 28 6d 6f 72  foreign key (mor
30d82 65 20 74 68 61 6e 20 6f 6e 65 20 63 6f 6c 75 6d  e than one colum
30d83 6e 29 2c 20 61 6c 6c 6f 63 61 74 65 0d 0a 20 20  n), allocate..  
30d84 2a 2a 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ** space for the
30d85 20 61 69 43 6f 6c 20 61 72 72 61 79 20 28 72 65   aiCol array (re
30d86 74 75 72 6e 65 64 20 76 69 61 20 6f 75 74 70 75  turned via outpu
30d87 74 20 70 61 72 61 6d 65 74 65 72 20 2a 70 61 69  t parameter *pai
30d88 43 6f 6c 29 2e 0d 0a 20 20 2a 2a 20 4e 6f 6e 2d  Col)...  ** Non-
30d89 63 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67  composite foreig
30d8a 6e 20 6b 65 79 73 20 64 6f 20 6e 6f 74 20 72 65  n keys do not re
30d8b 71 75 69 72 65 20 74 68 65 20 61 69 43 6f 6c 20  quire the aiCol 
30d8c 61 72 72 61 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  array...  */..  
30d8d 69 66 28 20 6e 43 6f 6c 3d 3d 31 20 29 7b 0d 0a  if( nCol==1 ){..
30d8e 20 20 20 20 2f 2a 20 54 68 65 20 46 4b 20 6d 61      /* The FK ma
30d8f 70 73 20 74 6f 20 74 68 65 20 49 50 4b 20 69 66  ps to the IPK if
30d90 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
30d91 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0d  owing are true:.
30d92 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
30d93 20 20 31 29 20 54 68 65 72 65 20 69 73 20 61 6e    1) There is an
30d94 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
30d95 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   KEY column and 
30d96 74 68 65 20 46 4b 20 69 73 20 69 6d 70 6c 69 63  the FK is implic
30d97 69 74 6c 79 20 0d 0a 20 20 20 20 2a 2a 20 20 20  itly ..    **   
30d98 20 20 20 6d 61 70 70 65 64 20 74 6f 20 74 68 65     mapped to the
30d99 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 66 20   primary key of 
30d9a 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2c 20 6f  table pParent, o
30d9b 72 0d 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54  r..    **   2) T
30d9c 68 65 20 46 4b 20 69 73 20 65 78 70 6c 69 63 69  he FK is explici
30d9d 74 6c 79 20 6d 61 70 70 65 64 20 74 6f 20 61 20  tly mapped to a 
30d9e 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20  column declared 
30d9f 61 73 20 49 4e 54 45 47 45 52 0d 0a 20 20 20 20  as INTEGER..    
30da0 2a 2a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20  **      PRIMARY 
30da1 4b 45 59 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  KEY...    */..  
30da2 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 69    if( pParent->i
30da3 50 4b 65 79 3e 3d 30 20 29 7b 0d 0a 20 20 20 20  PKey>=0 ){..    
30da4 20 20 69 66 28 20 21 7a 4b 65 79 20 29 20 72 65    if( !zKey ) re
30da5 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20 69  turn 0;..      i
30da6 66 28 20 21 73 71 6c 69 74 65 33 53 74 72 49 43  f( !sqlite3StrIC
30da7 6d 70 28 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c  mp(pParent->aCol
30da8 5b 70 50 61 72 65 6e 74 2d 3e 69 50 4b 65 79 5d  [pParent->iPKey]
30da9 2e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 29 20  .zName, zKey) ) 
30daa 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d  return 0;..    }
30dab 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61  ..  }else if( pa
30dac 69 43 6f 6c 20 29 7b 0d 0a 20 20 20 20 61 73 73  iCol ){..    ass
30dad 65 72 74 28 20 6e 43 6f 6c 3e 31 20 29 3b 0d 0a  ert( nCol>1 );..
30dae 20 20 20 20 61 69 43 6f 6c 20 3d 20 28 69 6e 74      aiCol = (int
30daf 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
30db0 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62  ocRaw(pParse->db
30db1 2c 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 69 6e  , nCol*sizeof(in
30db2 74 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 61  t));..    if( !a
30db3 69 43 6f 6c 20 29 20 72 65 74 75 72 6e 20 31 3b  iCol ) return 1;
30db4 0d 0a 20 20 20 20 2a 70 61 69 43 6f 6c 20 3d 20  ..    *paiCol = 
30db5 61 69 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  aiCol;..  }.... 
30db6 20 66 6f 72 28 70 49 64 78 3d 70 50 61 72 65 6e   for(pIdx=pParen
30db7 74 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  t->pIndex; pIdx;
30db8 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
30db9 74 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 49 64  t){..    if( pId
30dba 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 6e 43 6f 6c  x->nColumn==nCol
30dbb 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f   && pIdx->onErro
30dbc 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20 0d 0a  r!=OE_None ){ ..
30dbd 20 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69 73        /* pIdx is
30dbe 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
30dbf 28 6f 72 20 61 20 50 52 49 4d 41 52 59 20 4b 45  (or a PRIMARY KE
30dc0 59 29 20 61 6e 64 20 68 61 73 20 74 68 65 20 72  Y) and has the r
30dc1 69 67 68 74 20 6e 75 6d 62 65 72 0d 0a 20 20 20  ight number..   
30dc2 20 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73     ** of columns
30dc3 2e 20 49 66 20 65 61 63 68 20 69 6e 64 65 78 65  . If each indexe
30dc4 64 20 63 6f 6c 75 6d 6e 20 63 6f 72 72 65 73 70  d column corresp
30dc5 6f 6e 64 73 20 74 6f 20 61 20 66 6f 72 65 69 67  onds to a foreig
30dc6 6e 20 6b 65 79 0d 0a 20 20 20 20 20 20 2a 2a 20  n key..      ** 
30dc7 63 6f 6c 75 6d 6e 20 6f 66 20 70 46 4b 65 79 2c  column of pFKey,
30dc8 20 74 68 65 6e 20 74 68 69 73 20 69 6e 64 65 78   then this index
30dc9 20 69 73 20 61 20 77 69 6e 6e 65 72 2e 20 20 2a   is a winner.  *
30dca 2f 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 7a  /....      if( z
30dcb 4b 65 79 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Key==0 ){..     
30dcc 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73     /* If zKey is
30dcd 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
30dce 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
30dcf 69 6d 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65  implicitly mappe
30dd0 64 20 74 6f 20 0d 0a 20 20 20 20 20 20 20 20 2a  d to ..        *
30dd1 2a 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  * the PRIMARY KE
30dd2 59 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65  Y of table pPare
30dd3 6e 74 2e 20 54 68 65 20 50 52 49 4d 41 52 59 20  nt. The PRIMARY 
30dd4 4b 45 59 20 69 6e 64 65 78 20 6d 61 79 20 62 65  KEY index may be
30dd5 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 64   ..        ** id
30dd6 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
30dd7 74 65 73 74 20 28 49 6e 64 65 78 2e 61 75 74 6f  test (Index.auto
30dd8 49 6e 64 65 78 3d 3d 32 29 2e 20 20 2a 2f 0d 0a  Index==2).  */..
30dd9 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
30dda 2d 3e 61 75 74 6f 49 6e 64 65 78 3d 3d 32 20 29  ->autoIndex==2 )
30ddb 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  {..          if(
30ddc 20 61 69 43 6f 6c 20 29 7b 0d 0a 20 20 20 20 20   aiCol ){..     
30ddd 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20         int i;.. 
30dde 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69             for(i
30ddf 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
30de0 20 61 69 43 6f 6c 5b 69 5d 20 3d 20 70 46 4b 65   aiCol[i] = pFKe
30de1 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d  y->aCol[i].iFrom
30de2 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
30de3 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
30de4 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
30de5 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
30de6 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73     /* If zKey is
30de7 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 6e 20   non-NULL, then 
30de8 74 68 69 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  this foreign key
30de9 20 77 61 73 20 64 65 63 6c 61 72 65 64 20 74 6f   was declared to
30dea 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 70  ..        ** map
30deb 20 74 6f 20 61 6e 20 65 78 70 6c 69 63 69 74 20   to an explicit 
30dec 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
30ded 69 6e 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74  in table pParent
30dee 2e 20 43 68 65 63 6b 20 69 66 20 74 68 69 73 0d  . Check if this.
30def 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65  .        ** inde
30df0 78 20 6d 61 74 63 68 65 73 20 74 68 6f 73 65 20  x matches those 
30df1 63 6f 6c 75 6d 6e 73 2e 20 41 6c 73 6f 2c 20 63  columns. Also, c
30df2 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 69 6e  heck that the in
30df3 64 65 78 20 75 73 65 73 0d 0a 20 20 20 20 20 20  dex uses..      
30df4 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74    ** the default
30df5 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
30df6 6e 63 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  nces for each co
30df7 6c 75 6d 6e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  lumn. */..      
30df8 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 20    int i, j;..   
30df9 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
30dfa 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  nCol; i++){..   
30dfb 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20         int iCol 
30dfc 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
30dfd 5b 69 5d 3b 20 20 20 20 20 2f 2a 20 49 6e 64 65  [i];     /* Inde
30dfe 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70  x of column in p
30dff 61 72 65 6e 74 20 74 62 6c 20 2a 2f 0d 0a 20 20  arent tbl */..  
30e00 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44          char *zD
30e01 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20  fltColl;        
30e02 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
30e03 2e 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 6f 72 20  . collation for 
30e04 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 20 20 20  column */..     
30e05 20 20 20 20 20 63 68 61 72 20 2a 7a 49 64 78 43       char *zIdxC
30e06 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
30e07 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
30e08 66 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  f indexed column
30e09 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 20 20 20   */....         
30e0a 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78   /* If the index
30e0b 20 75 73 65 73 20 61 20 63 6f 6c 6c 61 74 69 6f   uses a collatio
30e0c 6e 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20  n sequence that 
30e0d 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
30e0e 6d 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  m..          ** 
30e0f 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c  the default coll
30e10 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
30e11 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 74  or the column, t
30e12 68 69 73 20 69 6e 64 65 78 20 69 73 0d 0a 20 20  his index is..  
30e13 20 20 20 20 20 20 20 20 2a 2a 20 75 6e 75 73 61          ** unusa
30e14 62 6c 65 2e 20 42 61 69 6c 20 6f 75 74 20 65 61  ble. Bail out ea
30e15 72 6c 79 20 69 6e 20 74 68 69 73 20 63 61 73 65  rly in this case
30e16 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  .  */..         
30e17 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d 20 70 50 61   zDfltColl = pPa
30e18 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  rent->aCol[iCol]
30e19 2e 7a 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 20  .zColl;..       
30e1a 20 20 20 69 66 28 20 21 7a 44 66 6c 74 43 6f 6c     if( !zDfltCol
30e1b 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  l ){..          
30e1c 20 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d 20 22 42    zDfltColl = "B
30e1d 49 4e 41 52 59 22 3b 0d 0a 20 20 20 20 20 20 20  INARY";..       
30e1e 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
30e1f 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
30e20 6d 70 28 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b  mp(pIdx->azColl[
30e21 69 5d 2c 20 7a 44 66 6c 74 43 6f 6c 6c 29 20 29  i], zDfltColl) )
30e22 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20   break;....     
30e23 20 20 20 20 20 7a 49 64 78 43 6f 6c 20 3d 20 70       zIdxCol = p
30e24 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43 6f  Parent->aCol[iCo
30e25 6c 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20  l].zName;..     
30e26 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
30e27 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20  nCol; j++){..   
30e28 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
30e29 69 74 65 33 53 74 72 49 43 6d 70 28 70 46 4b 65  ite3StrICmp(pFKe
30e2a 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c  y->aCol[j].zCol,
30e2b 20 7a 49 64 78 43 6f 6c 29 3d 3d 30 20 29 7b 0d   zIdxCol)==0 ){.
30e2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
30e2d 66 28 20 61 69 43 6f 6c 20 29 20 61 69 43 6f 6c  f( aiCol ) aiCol
30e2e 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] = pFKey->aCo
30e2f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3b 0d 0a 20 20 20  l[j].iFrom;..   
30e30 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
30e31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
30e32 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
30e33 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d           if( j==
30e34 6e 43 6f 6c 20 29 20 62 72 65 61 6b 3b 0d 0a 20  nCol ) break;.. 
30e35 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
30e36 20 20 69 66 28 20 69 3d 3d 6e 43 6f 6c 20 29 20    if( i==nCol ) 
30e37 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a 20 70  break;      /* p
30e38 49 64 78 20 69 73 20 75 73 61 62 6c 65 20 2a 2f  Idx is usable */
30e39 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
30e3a 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21  ..  }....  if( !
30e3b 70 49 64 78 20 29 7b 0d 0a 20 20 20 20 69 66 28  pIdx ){..    if(
30e3c 20 21 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c   !pParse->disabl
30e3d 65 54 72 69 67 67 65 72 73 20 29 7b 0d 0a 20 20  eTriggers ){..  
30e3e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
30e3f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72  Msg(pParse, "for
30e40 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
30e41 68 22 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  h");..    }..   
30e42 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
30e43 50 61 72 73 65 2d 3e 64 62 2c 20 61 69 43 6f 6c  Parse->db, aiCol
30e44 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31  );..    return 1
30e45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 70 49  ;..  }....  *ppI
30e46 64 78 20 3d 20 70 49 64 78 3b 0d 0a 20 20 72 65  dx = pIdx;..  re
30e47 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn 0;..}..../*
30e48 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
30e49 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
30e4a 6e 20 61 20 72 6f 77 20 69 73 20 69 6e 73 65 72  n a row is inser
30e4b 74 65 64 20 69 6e 74 6f 20 6f 72 20 64 65 6c 65  ted into or dele
30e4c 74 65 64 20 66 72 6f 6d 20 74 68 65 20 0d 0a 2a  ted from the ..*
30e4d 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20 6f 66  * child table of
30e4e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
30e4f 73 74 72 61 69 6e 74 20 70 46 4b 65 79 2e 20 49  straint pFKey. I
30e50 66 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45 20  f an SQL UPDATE 
30e51 69 73 20 65 78 65 63 75 74 65 64 20 0d 0a 2a 2a  is executed ..**
30e52 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20 74 61   on the child ta
30e53 62 6c 65 20 6f 66 20 70 46 4b 65 79 2c 20 74 68  ble of pFKey, th
30e54 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
30e55 6e 76 6f 6b 65 64 20 74 77 69 63 65 20 66 6f 72  nvoked twice for
30e56 20 65 61 63 68 20 72 6f 77 0d 0a 2a 2a 20 61 66   each row..** af
30e57 66 65 63 74 65 64 20 2d 20 6f 6e 63 65 20 74 6f  fected - once to
30e58 20 22 64 65 6c 65 74 65 22 20 74 68 65 20 6f 6c   "delete" the ol
30e59 64 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 6e 20  d row, and then 
30e5a 61 67 61 69 6e 20 74 6f 20 22 69 6e 73 65 72 74  again to "insert
30e5b 22 20 74 68 65 0d 0a 2a 2a 20 6e 65 77 20 72 6f  " the..** new ro
30e5c 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61 63 68 20  w...**..** Each 
30e5d 74 69 6d 65 20 69 74 20 69 73 20 63 61 6c 6c 65  time it is calle
30e5e 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
30e5f 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
30e60 63 6f 64 65 20 74 6f 20 6c 6f 63 61 74 65 20 74  code to locate t
30e61 68 65 0d 0a 2a 2a 20 72 6f 77 20 69 6e 20 74 68  he..** row in th
30e62 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 74  e parent table t
30e63 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
30e64 74 6f 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  to the row being
30e65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 0d   inserted into .
30e66 0a 2a 2a 20 6f 72 20 64 65 6c 65 74 65 64 20 66  .** or deleted f
30e67 72 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61  rom the child ta
30e68 62 6c 65 2e 20 49 66 20 74 68 65 20 70 61 72 65  ble. If the pare
30e69 6e 74 20 72 6f 77 20 63 61 6e 20 62 65 20 66 6f  nt row can be fo
30e6a 75 6e 64 2c 20 6e 6f 20 0d 0a 2a 2a 20 73 70 65  und, no ..** spe
30e6b 63 69 61 6c 20 61 63 74 69 6f 6e 20 69 73 20 74  cial action is t
30e6c 61 6b 65 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c  aken. Otherwise,
30e6d 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 72   if the parent r
30e6e 6f 77 20 63 61 6e 20 2a 6e 6f 74 2a 20 62 65 0d  ow can *not* be.
30e6f 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74 68 65  .** found in the
30e70 20 70 61 72 65 6e 74 20 74 61 62 6c 65 3a 0d 0a   parent table:..
30e71 2a 2a 0d 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69  **..**   Operati
30e72 6f 6e 20 7c 20 46 4b 20 74 79 70 65 20 20 20 7c  on | FK type   |
30e73 20 41 63 74 69 6f 6e 20 74 61 6b 65 6e 0d 0a 2a   Action taken..*
30e74 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
30e75 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30e76 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30e77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30e78 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a  --------------..
30e79 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20  **   INSERT     
30e7a 20 69 6d 6d 65 64 69 61 74 65 20 20 20 49 6e 63   immediate   Inc
30e7b 72 65 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65  rement the "imme
30e7c 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74  diate constraint
30e7d 20 63 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a   counter"...**..
30e7e 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20  **   DELETE     
30e7f 20 69 6d 6d 65 64 69 61 74 65 20 20 20 44 65 63   immediate   Dec
30e80 72 65 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65  rement the "imme
30e81 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74  diate constraint
30e82 20 63 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a   counter"...**..
30e83 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20  **   INSERT     
30e84 20 64 65 66 65 72 72 65 64 20 20 20 20 49 6e 63   deferred    Inc
30e85 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65  rement the "defe
30e86 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20  rred constraint 
30e87 63 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a 2a  counter"...**..*
30e88 2a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20 20  *   DELETE      
30e89 64 65 66 65 72 72 65 64 20 20 20 20 44 65 63 72  deferred    Decr
30e8a 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72  ement the "defer
30e8b 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
30e8c 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a 2a 2a  ounter"...**..**
30e8d 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e   These operation
30e8e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64  s are identified
30e8f 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20   in the comment 
30e90 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  at the top of th
30e91 69 73 20 66 69 6c 65 20 0d 0a 2a 2a 20 28 66 6b  is file ..** (fk
30e92 65 79 2e 63 29 20 61 73 20 22 49 2e 31 22 20 61  ey.c) as "I.1" a
30e93 6e 64 20 22 44 2e 31 22 2e 0d 0a 2a 2f 0d 0a 73  nd "D.1"...*/..s
30e94 74 61 74 69 63 20 76 6f 69 64 20 66 6b 4c 6f 6f  tatic void fkLoo
30e95 6b 75 70 50 61 72 65 6e 74 28 0d 0a 20 20 50 61  kupParent(..  Pa
30e96 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
30e97 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
30e98 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  text */..  int i
30e99 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Db,             
30e9a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74   /* Index of dat
30e9b 61 62 61 73 65 20 68 6f 75 73 69 6e 67 20 70 54  abase housing pT
30e9c 61 62 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a  ab */..  Table *
30e9d 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  pTab,          /
30e9e 2a 20 50 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  * Parent table o
30e9f 66 20 46 4b 20 70 46 4b 65 79 20 2a 2f 0d 0a 20  f FK pFKey */.. 
30ea0 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20   Index *pIdx,   
30ea1 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65         /* Unique
30ea2 20 69 6e 64 65 78 20 6f 6e 20 70 61 72 65 6e 74   index on parent
30ea3 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   key columns in 
30ea4 70 54 61 62 20 2a 2f 0d 0a 20 20 46 4b 65 79 20  pTab */..  FKey 
30ea5 2a 70 46 4b 65 79 2c 20 20 20 20 20 20 20 20 20  *pFKey,         
30ea6 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20   /* Foreign key 
30ea7 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a 20  constraint */.. 
30ea8 20 69 6e 74 20 2a 61 69 43 6f 6c 2c 20 20 20 20   int *aiCol,    
30ea9 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 20 66 72         /* Map fr
30eaa 6f 6d 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  om parent key co
30eab 6c 75 6d 6e 73 20 74 6f 20 63 68 69 6c 64 20 74  lumns to child t
30eac 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0d  able columns */.
30ead 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 2c 20  .  int regData, 
30eae 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
30eaf 65 73 73 20 6f 66 20 61 72 72 61 79 20 63 6f 6e  ess of array con
30eb0 74 61 69 6e 69 6e 67 20 63 68 69 6c 64 20 74 61  taining child ta
30eb1 62 6c 65 20 72 6f 77 20 2a 2f 0d 0a 20 20 69 6e  ble row */..  in
30eb2 74 20 6e 49 6e 63 72 2c 20 20 20 20 20 20 20 20  t nIncr,        
30eb3 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
30eb4 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e   constraint coun
30eb5 74 65 72 20 62 79 20 74 68 69 73 20 2a 2f 0d 0a  ter by this */..
30eb6 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20 20    int isIgnore  
30eb7 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72          /* If tr
30eb8 75 65 2c 20 70 72 65 74 65 6e 64 20 70 54 61 62  ue, pretend pTab
30eb9 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 4e 55   contains all NU
30eba 4c 4c 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 29 7b  LL values */..){
30ebb 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  ..  int i;      
30ebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ebd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30ebe 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
30ebf 6c 65 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76  le */..  Vdbe *v
30ec0 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
30ec1 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 20  e(pParse);      
30ec2 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 61 64     /* Vdbe to ad
30ec3 64 20 63 6f 64 65 20 74 6f 20 2a 2f 0d 0a 20 20  d code to */..  
30ec4 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72 73  int iCur = pPars
30ec5 65 2d 3e 6e 54 61 62 20 2d 20 31 3b 20 20 20 20  e->nTab - 1;    
30ec6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
30ec7 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 75 73  sor number to us
30ec8 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4f 6b 20  e */..  int iOk 
30ec9 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
30eca 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 20  eLabel(v);      
30ecb 20 20 2f 2a 20 6a 75 6d 70 20 68 65 72 65 20 69    /* jump here i
30ecc 66 20 70 61 72 65 6e 74 20 6b 65 79 20 66 6f 75  f parent key fou
30ecd 6e 64 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 49 66  nd */....  /* If
30ece 20 6e 49 6e 63 72 20 69 73 20 6c 65 73 73 20 74   nIncr is less t
30ecf 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63  han zero, then c
30ed0 68 65 63 6b 20 61 74 20 72 75 6e 74 69 6d 65 20  heck at runtime 
30ed1 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
30ed2 0d 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69  ..  ** outstandi
30ed3 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  ng constraints t
30ed4 6f 20 72 65 73 6f 6c 76 65 2e 20 49 66 20 74 68  o resolve. If th
30ed5 65 72 65 20 61 72 65 20 6e 6f 74 2c 20 74 68 65  ere are not, the
30ed6 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0d 0a 20  re is no need.. 
30ed7 20 2a 2a 20 74 6f 20 63 68 65 63 6b 20 69 66 20   ** to check if 
30ed8 64 65 6c 65 74 69 6e 67 20 74 68 69 73 20 72 6f  deleting this ro
30ed9 77 20 72 65 73 6f 6c 76 65 73 20 61 6e 79 20 6f  w resolves any o
30eda 75 74 73 74 61 6e 64 69 6e 67 20 76 69 6f 6c 61  utstanding viola
30edb 74 69 6f 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  tions...  **..  
30edc 2a 2a 20 43 68 65 63 6b 20 69 66 20 61 6e 79 20  ** Check if any 
30edd 6f 66 20 74 68 65 20 6b 65 79 20 63 6f 6c 75 6d  of the key colum
30ede 6e 73 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20  ns in the child 
30edf 74 61 62 6c 65 20 72 6f 77 20 61 72 65 20 4e 55  table row are NU
30ee0 4c 4c 2e 20 49 66 20 0d 0a 20 20 2a 2a 20 61 6e  LL. If ..  ** an
30ee1 79 20 61 72 65 2c 20 74 68 65 6e 20 74 68 65 20  y are, then the 
30ee2 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 63 6f  constraint is co
30ee3 6e 73 69 64 65 72 65 64 20 73 61 74 69 73 66 69  nsidered satisfi
30ee4 65 64 2e 20 4e 6f 20 6e 65 65 64 20 74 6f 20 0d  ed. No need to .
30ee5 0a 20 20 2a 2a 20 73 65 61 72 63 68 20 66 6f 72  .  ** search for
30ee6 20 61 20 6d 61 74 63 68 69 6e 67 20 72 6f 77 20   a matching row 
30ee7 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  in the parent ta
30ee8 62 6c 65 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20  ble.  */..  if( 
30ee9 6e 49 6e 63 72 3c 30 20 29 7b 0d 0a 20 20 20 20  nIncr<0 ){..    
30eea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30eeb 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72 6f  2(v, OP_FkIfZero
30eec 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  , pFKey->isDefer
30eed 72 65 64 2c 20 69 4f 6b 29 3b 0d 0a 20 20 7d 0d  red, iOk);..  }.
30eee 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46  .  for(i=0; i<pF
30eef 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Key->nCol; i++){
30ef0 0d 0a 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d  ..    int iReg =
30ef1 20 61 69 43 6f 6c 5b 69 5d 20 2b 20 72 65 67 44   aiCol[i] + regD
30ef2 61 74 61 20 2b 20 31 3b 0d 0a 20 20 20 20 73 71  ata + 1;..    sq
30ef3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30ef4 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52  v, OP_IsNull, iR
30ef5 65 67 2c 20 69 4f 6b 29 3b 0d 0a 20 20 7d 0d 0a  eg, iOk);..  }..
30ef6 0d 0a 20 20 69 66 28 20 69 73 49 67 6e 6f 72 65  ..  if( isIgnore
30ef7 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  ==0 ){..    if( 
30ef8 70 49 64 78 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  pIdx==0 ){..    
30ef9 20 20 2f 2a 20 49 66 20 70 49 64 78 20 69 73 20    /* If pIdx is 
30efa 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 70  NULL, then the p
30efb 61 72 65 6e 74 20 6b 65 79 20 69 73 20 74 68 65  arent key is the
30efc 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
30efd 20 4b 45 59 0d 0a 20 20 20 20 20 20 2a 2a 20 63   KEY..      ** c
30efe 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70 61 72  olumn of the par
30eff 65 6e 74 20 74 61 62 6c 65 20 28 74 61 62 6c 65  ent table (table
30f00 20 70 54 61 62 29 2e 20 20 2a 2f 0d 0a 20 20 20   pTab).  */..   
30f01 20 20 20 69 6e 74 20 69 4d 75 73 74 42 65 49 6e     int iMustBeIn
30f02 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
30f03 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4d   /* Address of M
30f04 75 73 74 42 65 49 6e 74 20 69 6e 73 74 72 75 63  ustBeInt instruc
30f05 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69  tion */..      i
30f06 6e 74 20 72 65 67 54 65 6d 70 20 3d 20 73 71 6c  nt regTemp = sql
30f07 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
30f08 50 61 72 73 65 29 3b 0d 0a 20 20 0d 0a 20 20 20  Parse);..  ..   
30f09 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 4d 75 73     /* Invoke Mus
30f0a 74 42 65 49 6e 74 20 74 6f 20 63 6f 65 72 63 65  tBeInt to coerce
30f0b 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20 76   the child key v
30f0c 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67  alue to an integ
30f0d 65 72 20 28 69 2e 65 2e 20 0d 0a 20 20 20 20 20  er (i.e. ..     
30f0e 20 2a 2a 20 61 70 70 6c 79 20 74 68 65 20 61 66   ** apply the af
30f0f 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 70 61  finity of the pa
30f10 72 65 6e 74 20 6b 65 79 29 2e 20 49 66 20 74 68  rent key). If th
30f11 69 73 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74  is fails, then t
30f12 68 65 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 69  here..      ** i
30f13 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61  s no matching pa
30f14 72 65 6e 74 20 6b 65 79 2e 20 42 65 66 6f 72 65  rent key. Before
30f15 20 75 73 69 6e 67 20 4d 75 73 74 42 65 49 6e 74   using MustBeInt
30f16 2c 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  , make a copy of
30f17 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76  ..      ** the v
30f18 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  alue. Otherwise,
30f19 20 74 68 65 20 76 61 6c 75 65 20 69 6e 73 65 72   the value inser
30f1a 74 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68 69  ted into the chi
30f1b 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 0d 0a 20  ld key column.. 
30f1c 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 68 61 76       ** will hav
30f1d 65 20 49 4e 54 45 47 45 52 20 61 66 66 69 6e 69  e INTEGER affini
30f1e 74 79 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74  ty applied to it
30f1f 2c 20 77 68 69 63 68 20 6d 61 79 20 6e 6f 74 20  , which may not 
30f20 62 65 20 63 6f 72 72 65 63 74 2e 20 20 2a 2f 0d  be correct.  */.
30f21 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
30f22 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
30f23 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 30 5d 2b 31  Copy, aiCol[0]+1
30f24 2b 72 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d  +regData, regTem
30f25 70 29 3b 0d 0a 20 20 20 20 20 20 69 4d 75 73 74  p);..      iMust
30f26 42 65 49 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  BeInt = sqlite3V
30f27 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30f28 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 54 65  MustBeInt, regTe
30f29 6d 70 2c 20 30 29 3b 0d 0a 20 20 0d 0a 20 20 20  mp, 0);..  ..   
30f2a 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72     /* If the par
30f2b 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68 65  ent table is the
30f2c 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 68 69   same as the chi
30f2d 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65  ld table, and we
30f2e 20 61 72 65 20 61 62 6f 75 74 0d 0a 20 20 20 20   are about..    
30f2f 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e    ** to incremen
30f30 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  t the constraint
30f31 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74  -counter (i.e. t
30f32 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
30f33 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0d 0a 20 20   operation),..  
30f34 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63      ** then chec
30f35 6b 20 69 66 20 74 68 65 20 72 6f 77 20 62 65 69  k if the row bei
30f36 6e 67 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63  ng inserted matc
30f37 68 65 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73  hes itself. If s
30f38 6f 2c 20 64 6f 20 6e 6f 74 0d 0a 20 20 20 20 20  o, do not..     
30f39 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68   ** increment th
30f3a 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75  e constraint-cou
30f3b 6e 74 65 72 2e 20 20 2a 2f 0d 0a 20 20 20 20 20  nter.  */..     
30f3c 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79   if( pTab==pFKey
30f3d 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72  ->pFrom && nIncr
30f3e 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==1 ){..        
30f3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30f40 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67 44  3(v, OP_Eq, regD
30f41 61 74 61 2c 20 69 4f 6b 2c 20 72 65 67 54 65 6d  ata, iOk, regTem
30f42 70 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  p);..      }..  
30f43 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ..      sqlite3O
30f44 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
30f45 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62   iCur, iDb, pTab
30f46 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0d  , OP_OpenRead);.
30f47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
30f48 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
30f49 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
30f4a 30 2c 20 72 65 67 54 65 6d 70 29 3b 0d 0a 20 20  0, regTemp);..  
30f4b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30f4c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
30f4d 2c 20 30 2c 20 69 4f 6b 29 3b 0d 0a 20 20 20 20  , 0, iOk);..    
30f4e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
30f4f 70 48 65 72 65 28 76 2c 20 73 71 6c 69 74 65 33  pHere(v, sqlite3
30f50 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
30f51 76 29 2d 32 29 3b 0d 0a 20 20 20 20 20 20 73 71  v)-2);..      sq
30f52 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
30f53 65 28 76 2c 20 69 4d 75 73 74 42 65 49 6e 74 29  e(v, iMustBeInt)
30f54 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
30f55 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
30f56 50 61 72 73 65 2c 20 72 65 67 54 65 6d 70 29 3b  Parse, regTemp);
30f57 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
30f58 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70      int nCol = p
30f59 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 0d 0a 20 20 20  FKey->nCol;..   
30f5a 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20 3d     int regTemp =
30f5b 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
30f5c 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f  ange(pParse, nCo
30f5d 6c 29 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72  l);..      int r
30f5e 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47  egRec = sqlite3G
30f5f 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
30f60 29 3b 0d 0a 20 20 20 20 20 20 4b 65 79 49 6e 66  );..      KeyInf
30f61 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
30f62 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
30f63 61 72 73 65 2c 20 70 49 64 78 29 3b 0d 0a 20 20  arse, pIdx);..  
30f64 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
30f65 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
30f66 4f 70 65 6e 52 65 61 64 2c 20 69 43 75 72 2c 20  OpenRead, iCur, 
30f67 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pIdx->tnum, iDb)
30f68 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
30f69 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
30f6a 2d 31 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 2c  -1, (char*)pKey,
30f6b 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
30f6c 4f 46 46 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72  OFF);..      for
30f6d 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
30f6e 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  +){..        sql
30f6f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
30f70 2c 20 4f 50 5f 43 6f 70 79 2c 20 61 69 43 6f 6c  , OP_Copy, aiCol
30f71 5b 69 5d 2b 31 2b 72 65 67 44 61 74 61 2c 20 72  [i]+1+regData, r
30f72 65 67 54 65 6d 70 2b 69 29 3b 0d 0a 20 20 20 20  egTemp+i);..    
30f73 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 20 20 2f    }..  ..      /
30f74 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  * If the parent 
30f75 74 61 62 6c 65 20 69 73 20 74 68 65 20 73 61 6d  table is the sam
30f76 65 20 61 73 20 74 68 65 20 63 68 69 6c 64 20 74  e as the child t
30f77 61 62 6c 65 2c 20 61 6e 64 20 77 65 20 61 72 65  able, and we are
30f78 20 61 62 6f 75 74 0d 0a 20 20 20 20 20 20 2a 2a   about..      **
30f79 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68   to increment th
30f7a 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75  e constraint-cou
30f7b 6e 74 65 72 20 28 69 2e 65 2e 20 74 68 69 73 20  nter (i.e. this 
30f7c 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65  is an INSERT ope
30f7d 72 61 74 69 6f 6e 29 2c 0d 0a 20 20 20 20 20 20  ration),..      
30f7e 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20 69 66  ** then check if
30f7f 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69   the row being i
30f80 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65 73 20  nserted matches 
30f81 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c 20 64  itself. If so, d
30f82 6f 20 6e 6f 74 0d 0a 20 20 20 20 20 20 2a 2a 20  o not..      ** 
30f83 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f  increment the co
30f84 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72  nstraint-counter
30f85 2e 20 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20  . ..      **..  
30f86 20 20 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66      ** If any of
30f87 20 74 68 65 20 70 61 72 65 6e 74 2d 6b 65 79 20   the parent-key 
30f88 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c 4c 2c  values are NULL,
30f89 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 61   then the row ca
30f8a 6e 6e 6f 74 20 6d 61 74 63 68 20 0d 0a 20 20 20  nnot match ..   
30f8b 20 20 20 2a 2a 20 69 74 73 65 6c 66 2e 20 53 6f     ** itself. So
30f8c 20 73 65 74 20 4a 55 4d 50 49 46 4e 55 4c 4c 20   set JUMPIFNULL 
30f8d 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  to make sure we 
30f8e 64 6f 20 74 68 65 20 4f 50 5f 46 6f 75 6e 64 20  do the OP_Found 
30f8f 69 66 20 61 6e 79 0d 0a 20 20 20 20 20 20 2a 2a  if any..      **
30f90 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2d 6b   of the parent-k
30f91 65 79 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55  ey values are NU
30f92 4c 4c 20 28 61 74 20 74 68 69 73 20 70 6f 69 6e  LL (at this poin
30f93 74 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 68  t it is known th
30f94 61 74 0d 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 6e  at..      ** non
30f95 65 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6b  e of the child k
30f96 65 79 20 76 61 6c 75 65 73 20 61 72 65 29 2e 0d  ey values are)..
30f97 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
30f98 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79   if( pTab==pFKey
30f99 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72  ->pFrom && nIncr
30f9a 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==1 ){..        
30f9b 69 6e 74 20 69 4a 75 6d 70 20 3d 20 73 71 6c 69  int iJump = sqli
30f9c 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
30f9d 64 72 28 76 29 20 2b 20 6e 43 6f 6c 20 2b 20 31  dr(v) + nCol + 1
30f9e 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
30f9f 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
30fa0 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  {..          int
30fa1 20 69 43 68 69 6c 64 20 3d 20 61 69 43 6f 6c 5b   iChild = aiCol[
30fa2 69 5d 2b 31 2b 72 65 67 44 61 74 61 3b 0d 0a 20  i]+1+regData;.. 
30fa3 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 50 61           int iPa
30fa4 72 65 6e 74 20 3d 20 70 49 64 78 2d 3e 61 69 43  rent = pIdx->aiC
30fa5 6f 6c 75 6d 6e 5b 69 5d 2b 31 2b 72 65 67 44 61  olumn[i]+1+regDa
30fa6 74 61 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 61  ta;..          a
30fa7 73 73 65 72 74 28 20 61 69 43 6f 6c 5b 69 5d 21  ssert( aiCol[i]!
30fa8 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0d  =pTab->iPKey );.
30fa9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
30faa 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
30fab 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
30fac 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ..            /*
30fad 20 54 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20   The parent key 
30fae 69 73 20 61 20 63 6f 6d 70 6f 73 69 74 65 20 6b  is a composite k
30faf 65 79 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73  ey that includes
30fb0 20 74 68 65 20 49 50 4b 20 63 6f 6c 75 6d 6e 20   the IPK column 
30fb1 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
30fb2 69 50 61 72 65 6e 74 20 3d 20 72 65 67 44 61 74  iParent = regDat
30fb3 61 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d  a;..          }.
30fb4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
30fb5 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
30fb6 4f 50 5f 4e 65 2c 20 69 43 68 69 6c 64 2c 20 69  OP_Ne, iChild, i
30fb7 4a 75 6d 70 2c 20 69 50 61 72 65 6e 74 29 3b 0d  Jump, iParent);.
30fb8 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
30fb9 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
30fba 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
30fbb 55 4c 4c 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d  ULL);..        }
30fbc 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
30fbd 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
30fbe 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b  P_Goto, 0, iOk);
30fbf 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 0d 0a 20  ..      }..  .. 
30fc0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30fc1 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
30fc2 65 52 65 63 6f 72 64 2c 20 72 65 67 54 65 6d 70  eRecord, regTemp
30fc3 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b  , nCol, regRec);
30fc4 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
30fc5 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
30fc6 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  1, sqlite3IndexA
30fc7 66 66 69 6e 69 74 79 53 74 72 28 76 2c 70 49 64  ffinityStr(v,pId
30fc8 78 29 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54  x), P4_TRANSIENT
30fc9 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
30fca 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76  3VdbeAddOp4Int(v
30fcb 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 43 75 72  , OP_Found, iCur
30fcc 2c 20 69 4f 6b 2c 20 72 65 67 52 65 63 2c 20 30  , iOk, regRec, 0
30fcd 29 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 73 71  );..  ..      sq
30fce 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
30fcf 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
30fd0 65 63 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ec);..      sqli
30fd1 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
30fd2 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 54  nge(pParse, regT
30fd3 65 6d 70 2c 20 6e 43 6f 6c 29 3b 0d 0a 20 20 20  emp, nCol);..   
30fd4 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28   }..  }....  if(
30fd5 20 21 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72   !pFKey->isDefer
30fd6 72 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  red && !pParse->
30fd7 70 54 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50  pToplevel && !pP
30fd8 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69  arse->isMultiWri
30fd9 74 65 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 53 70  te ){..    /* Sp
30fda 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 74  ecial case: If t
30fdb 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
30fdc 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
30fdd 77 69 6c 6c 20 69 6e 73 65 72 74 20 65 78 61 63  will insert exac
30fde 74 6c 79 0d 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  tly..    ** one 
30fdf 72 6f 77 20 69 6e 74 6f 20 74 68 65 20 74 61 62  row into the tab
30fe0 6c 65 2c 20 72 61 69 73 65 20 61 20 63 6f 6e 73  le, raise a cons
30fe1 74 72 61 69 6e 74 20 69 6d 6d 65 64 69 61 74 65  traint immediate
30fe2 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 0d 0a 20  ly instead of.. 
30fe3 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69     ** incrementi
30fe4 6e 67 20 61 20 63 6f 75 6e 74 65 72 2e 20 54 68  ng a counter. Th
30fe5 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
30fe6 61 73 20 74 68 65 20 56 4d 20 63 6f 64 65 20 69  as the VM code i
30fe7 73 20 62 65 69 6e 67 0d 0a 20 20 20 20 2a 2a 20  s being..    ** 
30fe8 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 77 69  generated for wi
30fe9 6c 6c 20 6e 6f 74 20 6f 70 65 6e 20 61 20 73 74  ll not open a st
30fea 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
30feb 69 6f 6e 2e 20 20 2a 2f 0d 0a 20 20 20 20 61 73  ion.  */..    as
30fec 73 65 72 74 28 20 6e 49 6e 63 72 3d 3d 31 20 29  sert( nIncr==1 )
30fed 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  ;..    sqlite3Ha
30fee 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0d 0a 20  ltConstraint(.. 
30fef 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 4f         pParse, O
30ff0 45 5f 41 62 6f 72 74 2c 20 22 66 6f 72 65 69 67  E_Abort, "foreig
30ff1 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
30ff2 20 66 61 69 6c 65 64 22 2c 20 50 34 5f 53 54 41   failed", P4_STA
30ff3 54 49 43 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d  TIC..    );..  }
30ff4 65 6c 73 65 7b 0d 0a 20 20 20 20 69 66 28 20 6e  else{..    if( n
30ff5 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65 79 2d  Incr>0 && pFKey-
30ff6 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 29  >isDeferred==0 )
30ff7 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
30ff8 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
30ff9 61 72 73 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20  arse)->mayAbort 
30ffa 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  = 1;..    }..   
30ffb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30ffc 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e 74  p2(v, OP_FkCount
30ffd 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66  er, pFKey->isDef
30ffe 65 72 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0d 0a  erred, nIncr);..
30fff 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33    }....  sqlite3
31000 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
31001 28 76 2c 20 69 4f 6b 29 3b 0d 0a 20 20 73 71 6c  (v, iOk);..  sql
31002 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
31003 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
31004 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
31005 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
31006 20 63 61 6c 6c 65 64 20 74 6f 20 67 65 6e 65 72   called to gener
31007 61 74 65 20 63 6f 64 65 20 65 78 65 63 75 74 65  ate code execute
31008 64 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20  d when a row is 
31009 64 65 6c 65 74 65 64 0d 0a 2a 2a 20 66 72 6f 6d  deleted..** from
3100a 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
3100b 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79  e of foreign key
3100c 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65   constraint pFKe
3100d 79 20 61 6e 64 2c 20 69 66 20 70 46 4b 65 79 20  y and, if pFKey 
3100e 69 73 20 0d 0a 2a 2a 20 64 65 66 65 72 72 65 64  is ..** deferred
3100f 2c 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20  , when a row is 
31010 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
31011 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 57 68  e same table. Wh
31012 65 6e 20 67 65 6e 65 72 61 74 69 6e 67 0d 0a 2a  en generating..*
31013 2a 20 63 6f 64 65 20 66 6f 72 20 61 6e 20 53 51  * code for an SQ
31014 4c 20 55 50 44 41 54 45 20 6f 70 65 72 61 74 69  L UPDATE operati
31015 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  on, this functio
31016 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20  n may be called 
31017 74 77 69 63 65 20 2d 0d 0a 2a 2a 20 6f 6e 63 65  twice -..** once
31018 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74 68 65   to "delete" the
31019 20 6f 6c 64 20 72 6f 77 20 61 6e 64 20 6f 6e 63   old row and onc
3101a 65 20 74 6f 20 22 69 6e 73 65 72 74 22 20 74 68  e to "insert" th
3101b 65 20 6e 65 77 20 72 6f 77 2e 0d 0a 2a 2a 0d 0a  e new row...**..
3101c 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
3101d 72 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75  rated by this fu
3101e 6e 63 74 69 6f 6e 20 73 63 61 6e 73 20 74 68 72  nction scans thr
3101f 6f 75 67 68 20 74 68 65 20 72 6f 77 73 20 69 6e  ough the rows in
31020 20 74 68 65 20 63 68 69 6c 64 0d 0a 2a 2a 20 74   the child..** t
31021 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73  able that corres
31022 70 6f 6e 64 20 74 6f 20 74 68 65 20 70 61 72 65  pond to the pare
31023 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 62 65 69  nt table row bei
31024 6e 67 20 64 65 6c 65 74 65 64 20 6f 72 20 69 6e  ng deleted or in
31025 73 65 72 74 65 64 2e 0d 0a 2a 2a 20 46 6f 72 20  serted...** For 
31026 65 61 63 68 20 63 68 69 6c 64 20 72 6f 77 20 66  each child row f
31027 6f 75 6e 64 2c 20 6f 6e 65 20 6f 66 20 74 68 65  ound, one of the
31028 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 63 74 69 6f   following actio
31029 6e 73 20 69 73 20 74 61 6b 65 6e 3a 0d 0a 2a 2a  ns is taken:..**
3102a 0d 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e  ..**   Operation
3102b 20 7c 20 46 4b 20 74 79 70 65 20 20 20 7c 20 41   | FK type   | A
3102c 63 74 69 6f 6e 20 74 61 6b 65 6e 0d 0a 2a 2a 20  ction taken..** 
3102d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
3102e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3102f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31031 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a  ------------..**
31032 20 20 20 44 45 4c 45 54 45 20 20 20 20 20 20 69     DELETE      i
31033 6d 6d 65 64 69 61 74 65 20 20 20 49 6e 63 72 65  mmediate   Incre
31034 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65 64 69  ment the "immedi
31035 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  ate constraint c
31036 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 20 20 20 20  ounter"...**    
31037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31038 20 20 20 20 20 20 20 4f 72 2c 20 69 66 20 74 68         Or, if th
31039 65 20 4f 4e 20 28 55 50 44 41 54 45 7c 44 45 4c  e ON (UPDATE|DEL
3103a 45 54 45 29 20 61 63 74 69 6f 6e 20 69 73 20 52  ETE) action is R
3103b 45 53 54 52 49 43 54 2c 0d 0a 2a 2a 20 20 20 20  ESTRICT,..**    
3103c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3103d 20 20 20 20 20 20 20 74 68 72 6f 77 20 61 20 22         throw a "
3103e 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
3103f 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 20 65  traint failed" e
31040 78 63 65 70 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  xception...**..*
31041 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20 20  *   INSERT      
31042 69 6d 6d 65 64 69 61 74 65 20 20 20 44 65 63 72  immediate   Decr
31043 65 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65 64  ement the "immed
31044 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  iate constraint 
31045 63 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a 2a  counter"...**..*
31046 2a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20 20  *   DELETE      
31047 64 65 66 65 72 72 65 64 20 20 20 20 49 6e 63 72  deferred    Incr
31048 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72  ement the "defer
31049 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
3104a 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 20 20 20 20  ounter"...**    
3104b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3104c 20 20 20 20 20 20 20 4f 72 2c 20 69 66 20 74 68         Or, if th
3104d 65 20 4f 4e 20 28 55 50 44 41 54 45 7c 44 45 4c  e ON (UPDATE|DEL
3104e 45 54 45 29 20 61 63 74 69 6f 6e 20 69 73 20 52  ETE) action is R
3104f 45 53 54 52 49 43 54 2c 0d 0a 2a 2a 20 20 20 20  ESTRICT,..**    
31050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31051 20 20 20 20 20 20 20 74 68 72 6f 77 20 61 20 22         throw a "
31052 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
31053 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 20 65  traint failed" e
31054 78 63 65 70 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  xception...**..*
31055 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20 20  *   INSERT      
31056 64 65 66 65 72 72 65 64 20 20 20 20 44 65 63 72  deferred    Decr
31057 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72  ement the "defer
31058 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
31059 6f 75 6e 74 65 72 22 2e 0d 0a 2a 2a 0d 0a 2a 2a  ounter"...**..**
3105a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e   These operation
3105b 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64  s are identified
3105c 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20   in the comment 
3105d 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  at the top of th
3105e 69 73 20 66 69 6c 65 20 0d 0a 2a 2a 20 28 66 6b  is file ..** (fk
3105f 65 79 2e 63 29 20 61 73 20 22 49 2e 32 22 20 61  ey.c) as "I.2" a
31060 6e 64 20 22 44 2e 32 22 2e 0d 0a 2a 2f 0d 0a 73  nd "D.2"...*/..s
31061 74 61 74 69 63 20 76 6f 69 64 20 66 6b 53 63 61  tatic void fkSca
31062 6e 43 68 69 6c 64 72 65 6e 28 0d 0a 20 20 50 61  nChildren(..  Pa
31063 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
31064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31065 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
31066 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  /..  SrcList *pS
31067 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rc,             
31068 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74 20       /* SrcList 
31069 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
3106a 61 62 6c 65 20 74 6f 20 73 63 61 6e 20 2a 2f 0d  able to scan */.
3106b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 0d  .  Table *pTab,.
3106c 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
3106d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3106e 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65     /* Foreign ke
3106f 79 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 46 4b  y index */..  FK
31070 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20 20  ey *pFKey,      
31071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31072 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 72 65 6c   Foreign key rel
31073 61 74 69 6f 6e 73 68 69 70 20 2a 2f 0d 0a 20 20  ationship */..  
31074 69 6e 74 20 2a 61 69 43 6f 6c 2c 20 20 20 20 20  int *aiCol,     
31075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31076 2f 2a 20 4d 61 70 20 66 72 6f 6d 20 70 49 64 78  /* Map from pIdx
31077 20 63 6f 6c 73 20 74 6f 20 63 68 69 6c 64 20 74   cols to child t
31078 61 62 6c 65 20 63 6f 6c 73 20 2a 2f 0d 0a 20 20  able cols */..  
31079 69 6e 74 20 72 65 67 44 61 74 61 2c 20 20 20 20  int regData,    
3107a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3107b 2f 2a 20 52 65 66 65 72 65 6e 63 65 64 20 74 61  /* Referenced ta
3107c 62 6c 65 20 64 61 74 61 20 73 74 61 72 74 73 20  ble data starts 
3107d 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  here */..  int n
3107e 49 6e 63 72 20 20 20 20 20 20 20 20 20 20 20 20  Incr            
3107f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d             /* Am
31080 6f 75 6e 74 20 74 6f 20 69 6e 63 72 65 6d 65 6e  ount to incremen
31081 74 20 64 65 66 65 72 72 65 64 20 63 6f 75 6e 74  t deferred count
31082 65 72 20 62 79 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  er by */..){..  
31083 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
31084 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20  arse->db;       
31085 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
31086 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b 20  le */..  int i; 
31087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31088 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
31089 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
3108a 0d 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  ..  Expr *pWhere
3108b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
3108c 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61      /* WHERE cla
3108d 75 73 65 20 74 6f 20 73 63 61 6e 20 77 69 74 68  use to scan with
3108e 20 2a 2f 0d 0a 20 20 4e 61 6d 65 43 6f 6e 74 65   */..  NameConte
3108f 78 74 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74 3b  xt sNameContext;
31090 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
31091 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76  t used to resolv
31092 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
31093 2f 0d 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  /..  WhereInfo *
31094 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  pWInfo;         
31095 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
31096 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 57  used by sqlite3W
31097 68 65 72 65 58 58 58 28 29 20 2a 2f 0d 0a 20 20  hereXXX() */..  
31098 69 6e 74 20 69 46 6b 49 66 5a 65 72 6f 20 3d 20  int iFkIfZero = 
31099 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
3109a 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50  /* Address of OP
3109b 5f 46 6b 49 66 5a 65 72 6f 20 2a 2f 0d 0a 20 20  _FkIfZero */..  
3109c 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
3109d 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
3109e 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 21  ;....  assert( !
3109f 70 49 64 78 20 7c 7c 20 70 49 64 78 2d 3e 70 54  pIdx || pIdx->pT
310a0 61 62 6c 65 3d 3d 70 54 61 62 20 29 3b 0d 0a 0d  able==pTab );...
310a1 0a 20 20 69 66 28 20 6e 49 6e 63 72 3c 30 20 29  .  if( nIncr<0 )
310a2 7b 0d 0a 20 20 20 20 69 46 6b 49 66 5a 65 72 6f  {..    iFkIfZero
310a3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
310a4 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a  dOp2(v, OP_FkIfZ
310a5 65 72 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65  ero, pFKey->isDe
310a6 66 65 72 72 65 64 2c 20 30 29 3b 0d 0a 20 20 7d  ferred, 0);..  }
310a7 0d 0a 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65 20  ....  /* Create 
310a8 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 72  an Expr object r
310a9 65 70 72 65 73 65 6e 74 69 6e 67 20 61 6e 20 53  epresenting an S
310aa 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  QL expression li
310ab 6b 65 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  ke:..  **..  ** 
310ac 20 20 3c 70 61 72 65 6e 74 2d 6b 65 79 31 3e 20    <parent-key1> 
310ad 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 31 3e 20 41  = <child-key1> A
310ae 4e 44 20 3c 70 61 72 65 6e 74 2d 6b 65 79 32 3e  ND <parent-key2>
310af 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 32 3e 20   = <child-key2> 
310b0 2e 2e 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  .....  **..  ** 
310b1 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  The collation se
310b2 71 75 65 6e 63 65 20 75 73 65 64 20 66 6f 72 20  quence used for 
310b3 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 73  the comparison s
310b4 68 6f 75 6c 64 20 62 65 20 74 68 61 74 20 6f 66  hould be that of
310b5 0d 0a 20 20 2a 2a 20 74 68 65 20 70 61 72 65 6e  ..  ** the paren
310b6 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20 54  t key columns. T
310b7 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
310b8 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  he parent key co
310b9 6c 75 6d 6e 20 73 68 6f 75 6c 64 0d 0a 20 20 2a  lumn should..  *
310ba 2a 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f 20  * be applied to 
310bb 65 61 63 68 20 63 68 69 6c 64 20 6b 65 79 20 76  each child key v
310bc 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65 20  alue before the 
310bd 63 6f 6d 70 61 72 69 73 6f 6e 20 74 61 6b 65 73  comparison takes
310be 20 70 6c 61 63 65 2e 0d 0a 20 20 2a 2f 0d 0a 20   place...  */.. 
310bf 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65   for(i=0; i<pFKe
310c0 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a  y->nCol; i++){..
310c1 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b      Expr *pLeft;
310c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310c3 20 20 2f 2a 20 56 61 6c 75 65 20 66 72 6f 6d 20    /* Value from 
310c4 70 61 72 65 6e 74 20 74 61 62 6c 65 20 72 6f 77  parent table row
310c5 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a 70   */..    Expr *p
310c6 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  Right;          
310c7 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
310c8 20 72 65 66 20 74 6f 20 63 68 69 6c 64 20 74 61   ref to child ta
310c9 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72  ble */..    Expr
310ca 20 2a 70 45 71 3b 20 20 20 20 20 20 20 20 20 20   *pEq;          
310cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
310cc 72 65 73 73 69 6f 6e 20 28 70 4c 65 66 74 20 3d  ression (pLeft =
310cd 20 70 52 69 67 68 74 29 20 2a 2f 0d 0a 20 20 20   pRight) */..   
310ce 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
310cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
310d0 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
310d1 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65  n in child table
310d2 20 2a 2f 20 0d 0a 20 20 20 20 63 6f 6e 73 74 20   */ ..    const 
310d3 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20  char *zCol;     
310d4 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
310d5 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69  of column in chi
310d6 6c 64 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20  ld table */.... 
310d7 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74     pLeft = sqlit
310d8 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 45  e3Expr(db, TK_RE
310d9 47 49 53 54 45 52 2c 20 30 29 3b 0d 0a 20 20 20  GISTER, 0);..   
310da 20 69 66 28 20 70 4c 65 66 74 20 29 7b 0d 0a 20   if( pLeft ){.. 
310db 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20       /* Set the 
310dc 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
310dd 63 65 20 61 6e 64 20 61 66 66 69 6e 69 74 79 20  ce and affinity 
310de 6f 66 20 74 68 65 20 4c 48 53 20 6f 66 20 65 61  of the LHS of ea
310df 63 68 20 54 4b 5f 45 51 0d 0a 20 20 20 20 20 20  ch TK_EQ..      
310e0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  ** expression to
310e1 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20   the parent key 
310e2 63 6f 6c 75 6d 6e 20 64 65 66 61 75 6c 74 73 2e  column defaults.
310e3 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20    */..      if( 
310e4 70 49 64 78 20 29 7b 0d 0a 20 20 20 20 20 20 20  pIdx ){..       
310e5 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0d 0a   Column *pCol;..
310e6 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70          iCol = p
310e7 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
310e8 3b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 20  ;..        pCol 
310e9 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  = &pTab->aCol[iC
310ea 6f 6c 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ol];..        if
310eb 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69  ( pTab->iPKey==i
310ec 43 6f 6c 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b  Col ) iCol = -1;
310ed 0d 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d  ..        pLeft-
310ee 3e 69 54 61 62 6c 65 20 3d 20 72 65 67 44 61 74  >iTable = regDat
310ef 61 2b 69 43 6f 6c 2b 31 3b 0d 0a 20 20 20 20 20  a+iCol+1;..     
310f0 20 20 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e 69     pLeft->affini
310f1 74 79 20 3d 20 70 43 6f 6c 2d 3e 61 66 66 69 6e  ty = pCol->affin
310f2 69 74 79 3b 0d 0a 20 20 20 20 20 20 20 20 70 4c  ity;..        pL
310f3 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c  eft->pColl = sql
310f4 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
310f5 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c 2d 3e  q(pParse, pCol->
310f6 7a 43 6f 6c 6c 29 3b 0d 0a 20 20 20 20 20 20 7d  zColl);..      }
310f7 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70  else{..        p
310f8 4c 65 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72  Left->iTable = r
310f9 65 67 44 61 74 61 3b 0d 0a 20 20 20 20 20 20 20  egData;..       
310fa 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74 79   pLeft->affinity
310fb 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e   = SQLITE_AFF_IN
310fc 54 45 47 45 52 3b 0d 0a 20 20 20 20 20 20 7d 0d  TEGER;..      }.
310fd 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 43 6f 6c  .    }..    iCol
310fe 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69 43 6f 6c   = aiCol ? aiCol
310ff 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] : pFKey->aCo
31100 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0d 0a 20 20 20  l[0].iFrom;..   
31101 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30   assert( iCol>=0
31102 20 29 3b 0d 0a 20 20 20 20 7a 43 6f 6c 20 3d 20   );..    zCol = 
31103 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43  pFKey->pFrom->aC
31104 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0d  ol[iCol].zName;.
31105 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71  .    pRight = sq
31106 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
31107 5f 49 44 2c 20 7a 43 6f 6c 29 3b 0d 0a 20 20 20  _ID, zCol);..   
31108 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45   pEq = sqlite3PE
31109 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45  xpr(pParse, TK_E
3110a 51 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  Q, pLeft, pRight
3110b 2c 20 30 29 3b 0d 0a 20 20 20 20 70 57 68 65 72  , 0);..    pWher
3110c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
3110d 6e 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70  nd(db, pWhere, p
3110e 45 71 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  Eq);..  }....  /
3110f 2a 20 49 66 20 74 68 65 20 63 68 69 6c 64 20 74  * If the child t
31110 61 62 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65  able is the same
31111 20 61 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   as the parent t
31112 61 62 6c 65 2c 20 61 6e 64 20 74 68 69 73 20 73  able, and this s
31113 63 61 6e 0d 0a 20 20 2a 2a 20 69 73 20 74 61 6b  can..  ** is tak
31114 69 6e 67 20 70 6c 61 63 65 20 61 73 20 70 61 72  ing place as par
31115 74 20 6f 66 20 61 20 44 45 4c 45 54 45 20 6f 70  t of a DELETE op
31116 65 72 61 74 69 6f 6e 20 28 6f 70 65 72 61 74 69  eration (operati
31117 6f 6e 20 44 2e 32 29 2c 20 6f 6d 69 74 20 74 68  on D.2), omit th
31118 65 0d 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e  e..  ** row bein
31119 67 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74  g deleted from t
3111a 68 65 20 73 63 61 6e 20 62 79 20 61 64 64 69 6e  he scan by addin
3111b 67 20 28 24 72 6f 77 69 64 20 21 3d 20 72 6f 77  g ($rowid != row
3111c 69 64 29 20 74 6f 20 74 68 65 20 57 48 45 52 45  id) to the WHERE
3111d 20 0d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2c 20   ..  ** clause, 
3111e 77 68 65 72 65 20 24 72 6f 77 69 64 20 69 73 20  where $rowid is 
3111f 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
31120 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
31121 65 64 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70  ed.  */..  if( p
31122 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f  Tab==pFKey->pFro
31123 6d 20 26 26 20 6e 49 6e 63 72 3e 30 20 29 7b 0d  m && nIncr>0 ){.
31124 0a 20 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20  .    Expr *pEq; 
31125 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31126 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
31127 20 28 70 4c 65 66 74 20 3d 20 70 52 69 67 68 74   (pLeft = pRight
31128 29 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a  ) */..    Expr *
31129 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20  pLeft;          
3112a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
3112b 20 66 72 6f 6d 20 70 61 72 65 6e 74 20 74 61 62   from parent tab
3112c 6c 65 20 72 6f 77 20 2a 2f 0d 0a 20 20 20 20 45  le row */..    E
3112d 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20  xpr *pRight;    
3112e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3112f 43 6f 6c 75 6d 6e 20 72 65 66 20 74 6f 20 63 68  Column ref to ch
31130 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  ild table */..  
31131 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    pLeft = sqlite
31132 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 45 47  3Expr(db, TK_REG
31133 49 53 54 45 52 2c 20 30 29 3b 0d 0a 20 20 20 20  ISTER, 0);..    
31134 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  pRight = sqlite3
31135 45 78 70 72 28 64 62 2c 20 54 4b 5f 43 4f 4c 55  Expr(db, TK_COLU
31136 4d 4e 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28  MN, 0);..    if(
31137 20 70 4c 65 66 74 20 26 26 20 70 52 69 67 68 74   pLeft && pRight
31138 20 29 7b 0d 0a 20 20 20 20 20 20 70 4c 65 66 74   ){..      pLeft
31139 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67 44 61  ->iTable = regDa
3113a 74 61 3b 0d 0a 20 20 20 20 20 20 70 4c 65 66 74  ta;..      pLeft
3113b 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
3113c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b  ITE_AFF_INTEGER;
3113d 0d 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e  ..      pRight->
3113e 69 54 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e 61  iTable = pSrc->a
3113f 5b 30 5d 2e 69 43 75 72 73 6f 72 3b 0d 0a 20 20  [0].iCursor;..  
31140 20 20 20 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c      pRight->iCol
31141 75 6d 6e 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d  umn = -1;..    }
31142 0d 0a 20 20 20 20 70 45 71 20 3d 20 73 71 6c 69  ..    pEq = sqli
31143 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
31144 20 54 4b 5f 4e 45 2c 20 70 4c 65 66 74 2c 20 70   TK_NE, pLeft, p
31145 52 69 67 68 74 2c 20 30 29 3b 0d 0a 20 20 20 20  Right, 0);..    
31146 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
31147 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65  ExprAnd(db, pWhe
31148 72 65 2c 20 70 45 71 29 3b 0d 0a 20 20 7d 0d 0a  re, pEq);..  }..
31149 0d 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
3114a 68 65 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  he references in
3114b 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
3114c 65 2e 20 2a 2f 0d 0a 20 20 6d 65 6d 73 65 74 28  e. */..  memset(
3114d 26 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 30  &sNameContext, 0
3114e 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e  , sizeof(NameCon
3114f 74 65 78 74 29 29 3b 0d 0a 20 20 73 4e 61 6d 65  text));..  sName
31150 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69 73 74  Context.pSrcList
31151 20 3d 20 70 53 72 63 3b 0d 0a 20 20 73 4e 61 6d   = pSrc;..  sNam
31152 65 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20  eContext.pParse 
31153 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 73 71 6c  = pParse;..  sql
31154 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
31155 61 6d 65 73 28 26 73 4e 61 6d 65 43 6f 6e 74 65  ames(&sNameConte
31156 78 74 2c 20 70 57 68 65 72 65 29 3b 0d 0a 0d 0a  xt, pWhere);....
31157 20 20 2f 2a 20 43 72 65 61 74 65 20 56 44 42 45    /* Create VDBE
31158 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68   to loop through
31159 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20   the entries in 
3115a 70 53 72 63 20 74 68 61 74 20 6d 61 74 63 68 20  pSrc that match 
3115b 74 68 65 20 57 48 45 52 45 0d 0a 20 20 2a 2a 20  the WHERE..  ** 
3115c 63 6c 61 75 73 65 2e 20 49 66 20 74 68 65 20 63  clause. If the c
3115d 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6e 6f 74  onstraint is not
3115e 20 64 65 66 65 72 72 65 64 2c 20 74 68 72 6f 77   deferred, throw
3115f 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 66 6f   an exception fo
31160 72 0d 0a 20 20 2a 2a 20 65 61 63 68 20 72 6f 77  r..  ** each row
31161 20 66 6f 75 6e 64 2e 20 4f 74 68 65 72 77 69 73   found. Otherwis
31162 65 2c 20 66 6f 72 20 64 65 66 65 72 72 65 64 20  e, for deferred 
31163 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69 6e 63  constraints, inc
31164 72 65 6d 65 6e 74 20 74 68 65 0d 0a 20 20 2a 2a  rement the..  **
31165 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
31166 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 62 79 20  aint counter by 
31167 6e 49 6e 63 72 20 66 6f 72 20 65 61 63 68 20 72  nIncr for each r
31168 6f 77 20 73 65 6c 65 63 74 65 64 2e 20 20 2a 2f  ow selected.  */
31169 0d 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c  ..  pWInfo = sql
3116a 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
3116b 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 57 68  Parse, pSrc, pWh
3116c 65 72 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a  ere, 0, 0, 0);..
3116d 20 20 69 66 28 20 6e 49 6e 63 72 3e 30 20 26 26    if( nIncr>0 &&
3116e 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72   pFKey->isDeferr
3116f 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  ed==0 ){..    sq
31170 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
31171 65 6c 28 70 50 61 72 73 65 29 2d 3e 6d 61 79 41  el(pParse)->mayA
31172 62 6f 72 74 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a  bort = 1;..  }..
31173 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
31174 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e  Op2(v, OP_FkCoun
31175 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65  ter, pFKey->isDe
31176 66 65 72 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0d  ferred, nIncr);.
31177 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 20 29 7b  .  if( pWInfo ){
31178 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68 65  ..    sqlite3Whe
31179 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0d 0a  reEnd(pWInfo);..
3117a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6c 65 61    }....  /* Clea
3117b 6e 20 75 70 20 74 68 65 20 57 48 45 52 45 20 63  n up the WHERE c
3117c 6c 61 75 73 65 20 63 6f 6e 73 74 72 75 63 74 65  lause constructe
3117d 64 20 61 62 6f 76 65 2e 20 2a 2f 0d 0a 20 20 73  d above. */..  s
3117e 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
3117f 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0d 0a 20  (db, pWhere);.. 
31180 20 69 66 28 20 69 46 6b 49 66 5a 65 72 6f 20 29   if( iFkIfZero )
31181 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
31182 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 46  beJumpHere(v, iF
31183 6b 49 66 5a 65 72 6f 29 3b 0d 0a 20 20 7d 0d 0a  kIfZero);..  }..
31184 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
31185 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
31186 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
31187 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 6e  he head of a lin
31188 6b 65 64 20 6c 69 73 74 20 6f 66 20 46 4b 0d 0a  ked list of FK..
31189 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  ** constraints f
3118a 6f 72 20 77 68 69 63 68 20 74 61 62 6c 65 20 70  or which table p
3118b 54 61 62 20 69 73 20 74 68 65 20 70 61 72 65 6e  Tab is the paren
3118c 74 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61  t table. For exa
3118d 6d 70 6c 65 2c 0d 0a 2a 2a 20 67 69 76 65 6e 20  mple,..** given 
3118e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63  the following sc
3118f 68 65 6d 61 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  hema:..**..**   
31190 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
31191 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0d  a PRIMARY KEY);.
31192 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42  .**   CREATE TAB
31193 4c 45 20 74 32 28 62 20 52 45 46 45 52 45 4e 43  LE t2(b REFERENC
31194 45 53 20 74 31 28 61 29 3b 0d 0a 2a 2a 0d 0a 2a  ES t1(a);..**..*
31195 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  * Calling this f
31196 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 61 62  unction with tab
31197 6c 65 20 22 74 31 22 20 61 73 20 61 6e 20 61 72  le "t1" as an ar
31198 67 75 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61  gument returns a
31199 20 70 6f 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20   pointer..** to 
3119a 74 68 65 20 46 4b 65 79 20 73 74 72 75 63 74 75  the FKey structu
3119b 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  re representing 
3119c 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
3119d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 61  constraint on ta
3119e 62 6c 65 0d 0a 2a 2a 20 22 74 32 22 2e 20 43 61  ble..** "t2". Ca
3119f 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74  lling this funct
311a0 69 6f 6e 20 77 69 74 68 20 22 74 32 22 20 61 73  ion with "t2" as
311a1 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 6f   the argument wo
311a2 75 6c 64 20 72 65 74 75 72 6e 20 61 0d 0a 2a 2a  uld return a..**
311a3 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 61   NULL pointer (a
311a4 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 46  s there are no F
311a5 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  K constraints fo
311a6 72 20 77 68 69 63 68 20 74 32 20 69 73 20 74 68  r which t2 is th
311a7 65 20 70 61 72 65 6e 74 0d 0a 2a 2a 20 74 61 62  e parent..** tab
311a8 6c 65 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  le)...*/..SQLITE
311a9 5f 50 52 49 56 41 54 45 20 46 4b 65 79 20 2a 73  _PRIVATE FKey *s
311aa 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63  qlite3FkReferenc
311ab 65 73 28 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  es(Table *pTab){
311ac 0d 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ..  int nName = 
311ad 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
311ae 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  pTab->zName);.. 
311af 20 72 65 74 75 72 6e 20 28 46 4b 65 79 20 2a 29   return (FKey *)
311b0 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
311b1 26 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  &pTab->pSchema->
311b2 66 6b 65 79 48 61 73 68 2c 20 70 54 61 62 2d 3e  fkeyHash, pTab->
311b3 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0d 0a  zName, nName);..
311b4 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  }..../*..** The 
311b5 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
311b6 69 73 20 61 20 54 72 69 67 67 65 72 20 73 74 72  is a Trigger str
311b7 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64  ucture allocated
311b8 20 62 79 20 74 68 65 20 0d 0a 2a 2a 20 66 6b 41   by the ..** fkA
311b9 63 74 69 6f 6e 54 72 69 67 67 65 72 28 29 20 72  ctionTrigger() r
311ba 6f 75 74 69 6e 65 2e 20 54 68 69 73 20 66 75 6e  outine. This fun
311bb 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68  ction deletes th
311bc 65 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74  e Trigger struct
311bd 75 72 65 0d 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20  ure..** and all 
311be 6f 66 20 69 74 73 20 73 75 62 2d 63 6f 6d 70 6f  of its sub-compo
311bf 6e 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  nents...**..** T
311c0 68 65 20 54 72 69 67 67 65 72 20 73 74 72 75 63  he Trigger struc
311c1 74 75 72 65 20 6f 72 20 61 6e 79 20 6f 66 20 69  ture or any of i
311c2 74 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65 6e 74  ts sub-component
311c3 73 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74  s may be allocat
311c4 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20  ed from..** the 
311c5 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
311c6 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64 61   belonging to da
311c7 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
311c8 4d 65 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  Mem...*/..static
311c9 20 76 6f 69 64 20 66 6b 54 72 69 67 67 65 72 44   void fkTriggerD
311ca 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
311cb 62 4d 65 6d 2c 20 54 72 69 67 67 65 72 20 2a 70  bMem, Trigger *p
311cc 29 7b 0d 0a 20 20 69 66 28 20 70 20 29 7b 0d 0a  ){..  if( p ){..
311cd 20 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20      TriggerStep 
311ce 2a 70 53 74 65 70 20 3d 20 70 2d 3e 73 74 65 70  *pStep = p->step
311cf 5f 6c 69 73 74 3b 0d 0a 20 20 20 20 73 71 6c 69  _list;..    sqli
311d0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
311d1 4d 65 6d 2c 20 70 53 74 65 70 2d 3e 70 57 68 65  Mem, pStep->pWhe
311d2 72 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  re);..    sqlite
311d3 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
311d4 64 62 4d 65 6d 2c 20 70 53 74 65 70 2d 3e 70 45  dbMem, pStep->pE
311d5 78 70 72 4c 69 73 74 29 3b 0d 0a 20 20 20 20 73  xprList);..    s
311d6 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
311d7 74 65 28 64 62 4d 65 6d 2c 20 70 53 74 65 70 2d  te(dbMem, pStep-
311d8 3e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20 20  >pSelect);..    
311d9 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
311da 65 28 64 62 4d 65 6d 2c 20 70 2d 3e 70 57 68 65  e(dbMem, p->pWhe
311db 6e 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  n);..    sqlite3
311dc 44 62 46 72 65 65 28 64 62 4d 65 6d 2c 20 70 29  DbFree(dbMem, p)
311dd 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
311de 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
311df 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 67  n is called to g
311e0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
311e1 74 20 72 75 6e 73 20 77 68 65 6e 20 74 61 62 6c  t runs when tabl
311e2 65 20 70 54 61 62 20 69 73 0d 0a 2a 2a 20 62 65  e pTab is..** be
311e3 69 6e 67 20 64 72 6f 70 70 65 64 20 66 72 6f 6d  ing dropped from
311e4 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 54   the database. T
311e5 68 65 20 53 72 63 4c 69 73 74 20 70 61 73 73 65  he SrcList passe
311e6 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
311e7 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 74 6f 20  argument..** to 
311e8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  this function co
311e9 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20  ntains a single 
311ea 65 6e 74 72 79 20 67 75 61 72 61 6e 74 65 65 64  entry guaranteed
311eb 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0d 0a   to resolve to..
311ec 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e 0d 0a  ** table pTab...
311ed 2a 2a 0d 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c  **..** Normally,
311ee 20 6e 6f 20 63 6f 64 65 20 69 73 20 72 65 71 75   no code is requ
311ef 69 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 69  ired. However, i
311f0 66 20 65 69 74 68 65 72 0d 0a 2a 2a 0d 0a 2a 2a  f either..**..**
311f1 20 20 20 28 61 29 20 54 68 65 20 74 61 62 6c 65     (a) The table
311f2 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
311f3 61 62 6c 65 20 6f 66 20 61 20 46 4b 20 63 6f 6e  able of a FK con
311f4 73 74 72 61 69 6e 74 2c 20 6f 72 0d 0a 2a 2a 20  straint, or..** 
311f5 20 20 28 62 29 20 54 68 65 20 74 61 62 6c 65 20    (b) The table 
311f6 69 73 20 74 68 65 20 63 68 69 6c 64 20 74 61 62  is the child tab
311f7 6c 65 20 6f 66 20 61 20 64 65 66 65 72 72 65 64  le of a deferred
311f8 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 61   FK constraint a
311f9 6e 64 20 69 74 20 69 73 0d 0a 2a 2a 20 20 20 20  nd it is..**    
311fa 20 20 20 64 65 74 65 72 6d 69 6e 65 64 20 61 74     determined at
311fb 20 72 75 6e 74 69 6d 65 20 74 68 61 74 20 74 68   runtime that th
311fc 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64  ere are outstand
311fd 69 6e 67 20 64 65 66 65 72 72 65 64 20 46 4b 20  ing deferred FK 
311fe 0d 0a 2a 2a 20 20 20 20 20 20 20 63 6f 6e 73 74  ..**       const
311ff 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73  raint violations
31200 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
31201 2c 0d 0a 2a 2a 0d 0a 2a 2a 20 74 68 65 6e 20 74  ,..**..** then t
31202 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  he equivalent of
31203 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 3c 74   "DELETE FROM <t
31204 62 6c 3e 22 20 69 73 20 65 78 65 63 75 74 65 64  bl>" is executed
31205 20 62 65 66 6f 72 65 20 64 72 6f 70 70 69 6e 67   before dropping
31206 0d 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 66  ..** the table f
31207 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
31208 2e 20 54 72 69 67 67 65 72 73 20 61 72 65 20 64  . Triggers are d
31209 69 73 61 62 6c 65 64 20 77 68 69 6c 65 20 72 75  isabled while ru
3120a 6e 6e 69 6e 67 20 74 68 69 73 0d 0a 2a 2a 20 44  nning this..** D
3120b 45 4c 45 54 45 2c 20 62 75 74 20 66 6f 72 65 69  ELETE, but forei
3120c 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 20 61  gn key actions a
3120d 72 65 20 6e 6f 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  re not...*/..SQL
3120e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3120f 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61   sqlite3FkDropTa
31210 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
31211 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
31212 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
31213 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
31214 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20  = pParse->db;.. 
31215 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26   if( (db->flags&
31216 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
31217 79 73 29 20 26 26 20 21 49 73 56 69 72 74 75 61  ys) && !IsVirtua
31218 6c 28 70 54 61 62 29 20 26 26 20 21 70 54 61 62  l(pTab) && !pTab
31219 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20  ->pSelect ){..  
3121a 20 20 69 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b    int iSkip = 0;
3121b 0d 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20  ..    Vdbe *v = 
3121c 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
3121d 50 61 72 73 65 29 3b 0d 0a 0d 0a 20 20 20 20 61  Parse);....    a
3121e 73 73 65 72 74 28 20 76 20 29 3b 20 20 20 20 20  ssert( v );     
3121f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31220 56 44 42 45 20 68 61 73 20 61 6c 72 65 61 64 79  VDBE has already
31221 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
31222 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69  */..    if( sqli
31223 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28  te3FkReferences(
31224 70 54 61 62 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  pTab)==0 ){..   
31225 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72     /* Search for
31226 20 61 20 64 65 66 65 72 72 65 64 20 66 6f 72 65   a deferred fore
31227 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
31228 6e 74 20 66 6f 72 20 77 68 69 63 68 20 74 68 69  nt for which thi
31229 73 20 74 61 62 6c 65 0d 0a 20 20 20 20 20 20 2a  s table..      *
3122a 2a 20 69 73 20 74 68 65 20 63 68 69 6c 64 20 74  * is the child t
3122b 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 63 61 6e  able. If one can
3122c 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65  not be found, re
3122d 74 75 72 6e 20 77 69 74 68 6f 75 74 20 0d 0a 20  turn without .. 
3122e 20 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74 69       ** generati
3122f 6e 67 20 61 6e 79 20 56 44 42 45 20 63 6f 64 65  ng any VDBE code
31230 2e 20 49 66 20 6f 6e 65 20 63 61 6e 20 62 65 20  . If one can be 
31231 66 6f 75 6e 64 2c 20 74 68 65 6e 20 6a 75 6d 70  found, then jump
31232 20 6f 76 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20   over..      ** 
31233 74 68 65 20 65 6e 74 69 72 65 20 44 45 4c 45 54  the entire DELET
31234 45 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  E if there are n
31235 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  o outstanding de
31236 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
31237 74 73 0d 0a 20 20 20 20 20 20 2a 2a 20 77 68 65  ts..      ** whe
31238 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  n this statement
31239 20 69 73 20 72 75 6e 2e 20 20 2a 2f 0d 0a 20 20   is run.  */..  
3123a 20 20 20 20 46 4b 65 79 20 2a 70 3b 0d 0a 20 20      FKey *p;..  
3123b 20 20 20 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e      for(p=pTab->
3123c 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70 2d 3e 70  pFKey; p; p=p->p
3123d 4e 65 78 74 46 72 6f 6d 29 7b 0d 0a 20 20 20 20  NextFrom){..    
3123e 20 20 20 20 69 66 28 20 70 2d 3e 69 73 44 65 66      if( p->isDef
3123f 65 72 72 65 64 20 29 20 62 72 65 61 6b 3b 0d 0a  erred ) break;..
31240 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
31241 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0d  f( !p ) return;.
31242 0a 20 20 20 20 20 20 69 53 6b 69 70 20 3d 20 73  .      iSkip = s
31243 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
31244 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20 20 73  bel(v);..      s
31245 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
31246 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c  (v, OP_FkIfZero,
31247 20 31 2c 20 69 53 6b 69 70 29 3b 0d 0a 20 20 20   1, iSkip);..   
31248 20 7d 0d 0a 0d 0a 20 20 20 20 70 50 61 72 73 65   }....    pParse
31249 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72  ->disableTrigger
3124a 73 20 3d 20 31 3b 0d 0a 20 20 20 20 73 71 6c 69  s = 1;..    sqli
3124b 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50  te3DeleteFrom(pP
3124c 61 72 73 65 2c 20 73 71 6c 69 74 65 33 53 72 63  arse, sqlite3Src
3124d 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4e 61 6d  ListDup(db, pNam
3124e 65 2c 20 30 29 2c 20 30 29 3b 0d 0a 20 20 20 20  e, 0), 0);..    
3124f 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 54  pParse->disableT
31250 72 69 67 67 65 72 73 20 3d 20 30 3b 0d 0a 0d 0a  riggers = 0;....
31251 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 44 45      /* If the DE
31252 4c 45 54 45 20 68 61 73 20 67 65 6e 65 72 61 74  LETE has generat
31253 65 64 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72  ed immediate for
31254 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
31255 69 6e 74 20 0d 0a 20 20 20 20 2a 2a 20 76 69 6f  int ..    ** vio
31256 6c 61 74 69 6f 6e 73 2c 20 68 61 6c 74 20 74 68  lations, halt th
31257 65 20 56 44 42 45 20 61 6e 64 20 72 65 74 75 72  e VDBE and retur
31258 6e 20 61 6e 20 65 72 72 6f 72 20 61 74 20 74 68  n an error at th
31259 69 73 20 70 6f 69 6e 74 2c 20 62 65 66 6f 72 65  is point, before
3125a 0d 0a 20 20 20 20 2a 2a 20 61 6e 79 20 6d 6f 64  ..    ** any mod
3125b 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68  ifications to th
3125c 65 20 73 63 68 65 6d 61 20 61 72 65 20 6d 61 64  e schema are mad
3125d 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  e. This is becau
3125e 73 65 20 73 74 61 74 65 6d 65 6e 74 0d 0a 20 20  se statement..  
3125f 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
31260 73 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74  s are not able t
31261 6f 20 72 6f 6c 6c 62 61 63 6b 20 73 63 68 65 6d  o rollback schem
31262 61 20 63 68 61 6e 67 65 73 2e 20 20 2a 2f 0d 0a  a changes.  */..
31263 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31264 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66  ddOp2(v, OP_FkIf
31265 5a 65 72 6f 2c 20 30 2c 20 73 71 6c 69 74 65 33  Zero, 0, sqlite3
31266 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
31267 76 29 2b 32 29 3b 0d 0a 20 20 20 20 73 71 6c 69  v)+2);..    sqli
31268 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
31269 74 28 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72  t(..        pPar
3126a 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22 66  se, OE_Abort, "f
3126b 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
3126c 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20 50  raint failed", P
3126d 34 5f 53 54 41 54 49 43 0d 0a 20 20 20 20 29 3b  4_STATIC..    );
3126e 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69 53 6b 69  ....    if( iSki
3126f 70 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  p ){..      sqli
31270 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
31271 62 65 6c 28 76 2c 20 69 53 6b 69 70 29 3b 0d 0a  bel(v, iSkip);..
31272 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d      }..  }..}...
31273 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
31274 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
31275 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 2c 20  when inserting, 
31276 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61  deleting or upda
31277 74 69 6e 67 20 61 20 72 6f 77 20 6f 66 0d 0a 2a  ting a row of..*
31278 2a 20 74 61 62 6c 65 20 70 54 61 62 20 74 6f 20  * table pTab to 
31279 67 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f  generate VDBE co
3127a 64 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 66 6f  de to perform fo
3127b 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
3127c 61 69 6e 74 20 0d 0a 2a 2a 20 70 72 6f 63 65 73  aint ..** proces
3127d 73 69 6e 67 20 66 6f 72 20 74 68 65 20 6f 70 65  sing for the ope
3127e 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ration...**..** 
3127f 46 6f 72 20 61 20 44 45 4c 45 54 45 20 6f 70 65  For a DELETE ope
31280 72 61 74 69 6f 6e 2c 20 70 61 72 61 6d 65 74 65  ration, paramete
31281 72 20 72 65 67 4f 6c 64 20 69 73 20 70 61 73 73  r regOld is pass
31282 65 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  ed the index of 
31283 74 68 65 0d 0a 2a 2a 20 66 69 72 73 74 20 72 65  the..** first re
31284 67 69 73 74 65 72 20 69 6e 20 61 6e 20 61 72 72  gister in an arr
31285 61 79 20 6f 66 20 28 70 54 61 62 2d 3e 6e 43 6f  ay of (pTab->nCo
31286 6c 2b 31 29 20 72 65 67 69 73 74 65 72 73 20 63  l+1) registers c
31287 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0d 0a 2a  ontaining the..*
31288 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  * rowid of the r
31289 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ow being deleted
3128a 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 65 61  , followed by ea
3128b 63 68 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ch of the column
3128c 20 76 61 6c 75 65 73 0d 0a 2a 2a 20 6f 66 20 74   values..** of t
3128d 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c  he row being del
3128e 65 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66 74 20  eted, from left 
3128f 74 6f 20 72 69 67 68 74 2e 20 50 61 72 61 6d 65  to right. Parame
31290 74 65 72 20 72 65 67 4e 65 77 20 69 73 20 70 61  ter regNew is pa
31291 73 73 65 64 0d 0a 2a 2a 20 7a 65 72 6f 20 69 6e  ssed..** zero in
31292 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d   this case...**.
31293 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 53 45 52  .** For an INSER
31294 54 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65 67  T operation, reg
31295 4f 6c 64 20 69 73 20 70 61 73 73 65 64 20 7a 65  Old is passed ze
31296 72 6f 20 61 6e 64 20 72 65 67 4e 65 77 20 69 73  ro and regNew is
31297 20 70 61 73 73 65 64 20 74 68 65 0d 0a 2a 2a 20   passed the..** 
31298 66 69 72 73 74 20 72 65 67 69 73 74 65 72 20 6f  first register o
31299 66 20 61 6e 20 61 72 72 61 79 20 6f 66 20 28 70  f an array of (p
3129a 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 72 65 67  Tab->nCol+1) reg
3129b 69 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e  isters containin
3129c 67 20 74 68 65 20 6e 65 77 0d 0a 2a 2a 20 72 6f  g the new..** ro
3129d 77 20 64 61 74 61 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  w data...**..** 
3129e 46 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f 70  For an UPDATE op
3129f 65 72 61 74 69 6f 6e 2c 20 74 68 69 73 20 66 75  eration, this fu
312a0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
312a1 20 74 77 69 63 65 2e 20 4f 6e 63 65 20 62 65 66   twice. Once bef
312a2 6f 72 65 0d 0a 2a 2a 20 74 68 65 20 6f 72 69 67  ore..** the orig
312a3 69 6e 61 6c 20 72 65 63 6f 72 64 20 69 73 20 64  inal record is d
312a4 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20  eleted from the 
312a5 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20  table using the 
312a6 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69  calling conventi
312a7 6f 6e 0d 0a 2a 2a 20 64 65 73 63 72 69 62 65 64  on..** described
312a8 20 66 6f 72 20 44 45 4c 45 54 45 2e 20 54 68 65   for DELETE. The
312a9 6e 20 61 67 61 69 6e 20 61 66 74 65 72 20 74 68  n again after th
312aa 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 63 6f 72  e original recor
312ab 64 20 69 73 20 64 65 6c 65 74 65 64 0d 0a 2a 2a  d is deleted..**
312ac 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20   but before the 
312ad 6e 65 77 20 72 65 63 6f 72 64 20 69 73 20 69 6e  new record is in
312ae 73 65 72 74 65 64 20 75 73 69 6e 67 20 74 68 65  serted using the
312af 20 49 4e 53 45 52 54 20 63 6f 6e 76 65 6e 74 69   INSERT conventi
312b0 6f 6e 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  on. ..*/..SQLITE
312b1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
312b2 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 0d 0a 20  lite3FkCheck(.. 
312b3 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
312b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312b5 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
312b6 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70  t */..  Table *p
312b7 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  Tab,            
312b8 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 20 69          /* Row i
312b9 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20  s being deleted 
312ba 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65 20  from this table 
312bb 2a 2f 20 0d 0a 20 20 69 6e 74 20 72 65 67 4f 6c  */ ..  int regOl
312bc 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
312bd 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
312be 75 73 20 72 6f 77 20 64 61 74 61 20 69 73 20 73  us row data is s
312bf 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0d 0a 20  tored here */.. 
312c0 20 69 6e 74 20 72 65 67 4e 65 77 20 20 20 20 20   int regNew     
312c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312c2 20 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61 74 61   /* New row data
312c3 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
312c4 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
312c5 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
312c6 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74  db;       /* Dat
312c7 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  abase handle */.
312c8 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20  .  FKey *pFKey; 
312c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312ca 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
312cb 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 46 4b  erate through FK
312cc 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b  s */..  int iDb;
312cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312ce 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
312cf 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
312d0 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0d  taining pTab */.
312d1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
312d2 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
312d3 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
312d4 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
312d5 67 20 70 54 61 62 20 2a 2f 0d 0a 20 20 69 6e 74  g pTab */..  int
312d6 20 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20   isIgnoreErrors 
312d7 3d 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  = pParse->disabl
312d8 65 54 72 69 67 67 65 72 73 3b 0d 0a 0d 0a 20 20  eTriggers;....  
312d9 2f 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f  /* Exactly one o
312da 66 20 72 65 67 4f 6c 64 20 61 6e 64 20 72 65 67  f regOld and reg
312db 4e 65 77 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f  New should be no
312dc 6e 2d 7a 65 72 6f 2e 20 2a 2f 0d 0a 20 20 61 73  n-zero. */..  as
312dd 73 65 72 74 28 20 28 72 65 67 4f 6c 64 3d 3d 30  sert( (regOld==0
312de 29 21 3d 28 72 65 67 4e 65 77 3d 3d 30 29 20 29  )!=(regNew==0) )
312df 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 66 6f 72  ;....  /* If for
312e0 65 69 67 6e 2d 6b 65 79 73 20 61 72 65 20 64 69  eign-keys are di
312e1 73 61 62 6c 65 64 2c 20 74 68 69 73 20 66 75 6e  sabled, this fun
312e2 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
312e3 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 28 64 62 2d  . */..  if( (db-
312e4 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f  >flags&SQLITE_Fo
312e5 72 65 69 67 6e 4b 65 79 73 29 3d 3d 30 20 29 20  reignKeys)==0 ) 
312e6 72 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 69 44 62  return;....  iDb
312e7 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
312e8 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
312e9 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 7a  ->pSchema);..  z
312ea 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
312eb 5d 2e 7a 4e 61 6d 65 3b 0d 0a 0d 0a 20 20 2f 2a  ].zName;....  /*
312ec 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c   Loop through al
312ed 6c 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  l the foreign ke
312ee 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  y constraints fo
312ef 72 20 77 68 69 63 68 20 70 54 61 62 20 69 73 20  r which pTab is 
312f0 74 68 65 0d 0a 20 20 2a 2a 20 63 68 69 6c 64 20  the..  ** child 
312f1 74 61 62 6c 65 20 28 74 68 65 20 74 61 62 6c 65  table (the table
312f2 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67   that the foreig
312f3 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e  n key definition
312f4 20 69 73 20 70 61 72 74 20 6f 66 29 2e 20 20 2a   is part of).  *
312f5 2f 0d 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70  /..  for(pFKey=p
312f6 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65  Tab->pFKey; pFKe
312f7 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e  y; pFKey=pFKey->
312f8 70 4e 65 78 74 46 72 6f 6d 29 7b 0d 0a 20 20 20  pNextFrom){..   
312f9 20 54 61 62 6c 65 20 2a 70 54 6f 3b 20 20 20 20   Table *pTo;    
312fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
312fb 2a 20 50 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  * Parent table o
312fc 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 46  f foreign key pF
312fd 4b 65 79 20 2a 2f 0d 0a 20 20 20 20 49 6e 64 65  Key */..    Inde
312fe 78 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20  x *pIdx = 0;    
312ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
31300 65 78 20 6f 6e 20 6b 65 79 20 63 6f 6c 75 6d 6e  ex on key column
31301 73 20 69 6e 20 70 54 6f 20 2a 2f 0d 0a 20 20 20  s in pTo */..   
31302 20 69 6e 74 20 2a 61 69 46 72 65 65 20 3d 20 30   int *aiFree = 0
31303 3b 0d 0a 20 20 20 20 69 6e 74 20 2a 61 69 43 6f  ;..    int *aiCo
31304 6c 3b 0d 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c  l;..    int iCol
31305 3b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20  ;..    int i;.. 
31306 20 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20     int isIgnore 
31307 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46  = 0;....    /* F
31308 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74  ind the parent t
31309 61 62 6c 65 20 6f 66 20 74 68 69 73 20 66 6f 72  able of this for
3130a 65 69 67 6e 20 6b 65 79 2e 20 41 6c 73 6f 20 66  eign key. Also f
3130b 69 6e 64 20 61 20 75 6e 69 71 75 65 20 69 6e 64  ind a unique ind
3130c 65 78 20 0d 0a 20 20 20 20 2a 2a 20 6f 6e 20 74  ex ..    ** on t
3130d 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  he parent key co
3130e 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72  lumns in the par
3130f 65 6e 74 20 74 61 62 6c 65 2e 20 49 66 20 65 69  ent table. If ei
31310 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0d 0a  ther of these ..
31311 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 74      ** schema it
31312 65 6d 73 20 63 61 6e 6e 6f 74 20 62 65 20 6c 6f  ems cannot be lo
31313 63 61 74 65 64 2c 20 73 65 74 20 61 6e 20 65 72  cated, set an er
31314 72 6f 72 20 69 6e 20 70 50 61 72 73 65 20 61 6e  ror in pParse an
31315 64 20 72 65 74 75 72 6e 20 0d 0a 20 20 20 20 2a  d return ..    *
31316 2a 20 65 61 72 6c 79 2e 20 20 2a 2f 0d 0a 20 20  * early.  */..  
31317 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69    if( pParse->di
31318 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b  sableTriggers ){
31319 0d 0a 20 20 20 20 20 20 70 54 6f 20 3d 20 73 71  ..      pTo = sq
3131a 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
3131b 62 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 7a  b, pFKey->zTo, z
3131c 44 62 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  Db);..    }else{
3131d 0d 0a 20 20 20 20 20 20 70 54 6f 20 3d 20 73 71  ..      pTo = sq
3131e 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
3131f 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 4b 65  (pParse, 0, pFKe
31320 79 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0d 0a 20  y->zTo, zDb);.. 
31321 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 21 70     }..    if( !p
31322 54 6f 20 7c 7c 20 6c 6f 63 61 74 65 46 6b 65 79  To || locateFkey
31323 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54  Index(pParse, pT
31324 6f 2c 20 70 46 4b 65 79 2c 20 26 70 49 64 78 2c  o, pFKey, &pIdx,
31325 20 26 61 69 46 72 65 65 29 20 29 7b 0d 0a 20 20   &aiFree) ){..  
31326 20 20 20 20 61 73 73 65 72 74 28 20 69 73 49 67      assert( isIg
31327 6e 6f 72 65 45 72 72 6f 72 73 3d 3d 30 20 7c 7c  noreErrors==0 ||
31328 20 28 72 65 67 4f 6c 64 21 3d 30 20 26 26 20 72   (regOld!=0 && r
31329 65 67 4e 65 77 3d 3d 30 29 20 29 3b 0d 0a 20 20  egNew==0) );..  
3132a 20 20 20 20 69 66 28 20 21 69 73 49 67 6e 6f 72      if( !isIgnor
3132b 65 45 72 72 6f 72 73 20 7c 7c 20 64 62 2d 3e 6d  eErrors || db->m
3132c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
3132d 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 69 66 28  turn;..      if(
3132e 20 70 54 6f 3d 3d 30 20 29 7b 0d 0a 20 20 20 20   pTo==0 ){..    
3132f 20 20 20 20 2f 2a 20 49 66 20 69 73 49 67 6e 6f      /* If isIgno
31330 72 65 45 72 72 6f 72 73 20 69 73 20 74 72 75 65  reErrors is true
31331 2c 20 74 68 65 6e 20 61 20 74 61 62 6c 65 20 69  , then a table i
31332 73 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e  s being dropped.
31333 20 49 6e 20 74 68 69 73 0d 0a 20 20 20 20 20 20   In this..      
31334 20 20 2a 2a 20 63 61 73 65 20 53 51 4c 69 74 65    ** case SQLite
31335 20 72 75 6e 73 20 61 20 22 44 45 4c 45 54 45 20   runs a "DELETE 
31336 46 52 4f 4d 20 78 78 78 22 20 6f 6e 20 74 68 65  FROM xxx" on the
31337 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
31338 70 70 65 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a  pped..        **
31339 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c 6c 79   before actually
3133a 20 64 72 6f 70 70 69 6e 67 20 69 74 20 69 6e 20   dropping it in 
3133b 6f 72 64 65 72 20 74 6f 20 63 68 65 63 6b 20 46  order to check F
3133c 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0d 0a  K constraints...
3133d 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68          ** If th
3133e 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  e parent table o
3133f 66 20 61 6e 20 46 4b 20 63 6f 6e 73 74 72 61 69  f an FK constrai
31340 6e 74 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e  nt on the curren
31341 74 20 74 61 62 6c 65 20 69 73 0d 0a 20 20 20 20  t table is..    
31342 20 20 20 20 2a 2a 20 6d 69 73 73 69 6e 67 2c 20      ** missing, 
31343 62 65 68 61 76 65 20 61 73 20 69 66 20 69 74 20  behave as if it 
31344 69 73 20 65 6d 70 74 79 2e 20 69 2e 65 2e 20 64  is empty. i.e. d
31345 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 6c  ecrement the rel
31346 65 76 61 6e 74 0d 0a 20 20 20 20 20 20 20 20 2a  evant..        *
31347 2a 20 46 4b 20 63 6f 75 6e 74 65 72 20 66 6f 72  * FK counter for
31348 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65   each row of the
31349 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 77   current table w
3134a 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 6b 65 79  ith non-NULL key
3134b 73 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a  s...        */..
3134c 20 20 20 20 20 20 20 20 56 64 62 65 20 2a 76 20          Vdbe *v 
3134d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
3134e 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
3134f 20 20 20 69 6e 74 20 69 4a 75 6d 70 20 3d 20 73     int iJump = s
31350 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
31351 74 41 64 64 72 28 76 29 20 2b 20 70 46 4b 65 79  tAddr(v) + pFKey
31352 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0d 0a 20 20 20  ->nCol + 1;..   
31353 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
31354 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  pFKey->nCol; i++
31355 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e  ){..          in
31356 74 20 69 52 65 67 20 3d 20 70 46 4b 65 79 2d 3e  t iReg = pFKey->
31357 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 2b 20  aCol[i].iFrom + 
31358 72 65 67 4f 6c 64 20 2b 20 31 3b 0d 0a 20 20 20  regOld + 1;..   
31359 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3135a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
3135b 73 4e 75 6c 6c 2c 20 69 52 65 67 2c 20 69 4a 75  sNull, iReg, iJu
3135c 6d 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  mp);..        }.
3135d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3135e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3135f 5f 46 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b 65  _FkCounter, pFKe
31360 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 2d  y->isDeferred, -
31361 31 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  1);..      }..  
31362 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20      continue;.. 
31363 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74     }..    assert
31364 28 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31  ( pFKey->nCol==1
31365 20 7c 7c 20 28 61 69 46 72 65 65 20 26 26 20 70   || (aiFree && p
31366 49 64 78 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 69  Idx) );....    i
31367 66 28 20 61 69 46 72 65 65 20 29 7b 0d 0a 20 20  f( aiFree ){..  
31368 20 20 20 20 61 69 43 6f 6c 20 3d 20 61 69 46 72      aiCol = aiFr
31369 65 65 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  ee;..    }else{.
3136a 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70 46  .      iCol = pF
3136b 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72  Key->aCol[0].iFr
3136c 6f 6d 3b 0d 0a 20 20 20 20 20 20 61 69 43 6f 6c  om;..      aiCol
3136d 20 3d 20 26 69 43 6f 6c 3b 0d 0a 20 20 20 20 7d   = &iCol;..    }
3136e 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
3136f 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b  <pFKey->nCol; i+
31370 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 61  +){..      if( a
31371 69 43 6f 6c 5b 69 5d 3d 3d 70 54 61 62 2d 3e 69  iCol[i]==pTab->i
31372 50 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20 20 20  PKey ){..       
31373 20 61 69 43 6f 6c 5b 69 5d 20 3d 20 2d 31 3b 0d   aiCol[i] = -1;.
31374 0a 20 20 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65  .      }..#ifnde
31375 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
31376 54 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a 20 20 20  THORIZATION..   
31377 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 70 65     /* Request pe
31378 72 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64  rmission to read
31379 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20   the parent key 
3137a 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 74 68 65 20  columns. If the 
3137b 0d 0a 20 20 20 20 20 20 2a 2a 20 61 75 74 68 6f  ..      ** autho
3137c 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
3137d 6b 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  k returns SQLITE
3137e 5f 49 47 4e 4f 52 45 2c 20 62 65 68 61 76 65 20  _IGNORE, behave 
3137f 61 73 20 69 66 20 61 6e 79 0d 0a 20 20 20 20 20  as if any..     
31380 20 2a 2a 20 76 61 6c 75 65 73 20 72 65 61 64 20   ** values read 
31381 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20  from the parent 
31382 74 61 62 6c 65 20 61 72 65 20 4e 55 4c 4c 2e 20  table are NULL. 
31383 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 64 62  */..      if( db
31384 2d 3e 78 41 75 74 68 20 29 7b 0d 0a 20 20 20 20  ->xAuth ){..    
31385 20 20 20 20 69 6e 74 20 72 63 61 75 74 68 3b 0d      int rcauth;.
31386 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
31387 43 6f 6c 20 3d 20 70 54 6f 2d 3e 61 43 6f 6c 5b  Col = pTo->aCol[
31388 70 49 64 78 20 3f 20 70 49 64 78 2d 3e 61 69 43  pIdx ? pIdx->aiC
31389 6f 6c 75 6d 6e 5b 69 5d 20 3a 20 70 54 6f 2d 3e  olumn[i] : pTo->
3138a 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  iPKey].zName;.. 
3138b 20 20 20 20 20 20 20 72 63 61 75 74 68 20 3d 20         rcauth = 
3138c 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 43  sqlite3AuthReadC
3138d 6f 6c 28 70 50 61 72 73 65 2c 20 70 54 6f 2d 3e  ol(pParse, pTo->
3138e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20 69 44 62  zName, zCol, iDb
3138f 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 73 49 67  );..        isIg
31390 6e 6f 72 65 20 3d 20 28 72 63 61 75 74 68 3d 3d  nore = (rcauth==
31391 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 29 3b 0d  SQLITE_IGNORE);.
31392 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  .      }..#endif
31393 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
31394 2a 20 54 61 6b 65 20 61 20 73 68 61 72 65 64 2d  * Take a shared-
31395 63 61 63 68 65 20 61 64 76 69 73 6f 72 79 20 72  cache advisory r
31396 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead-lock on the 
31397 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 41 6c  parent table. Al
31398 6c 6f 63 61 74 65 20 0d 0a 20 20 20 20 2a 2a 20  locate ..    ** 
31399 61 20 63 75 72 73 6f 72 20 74 6f 20 75 73 65 20  a cursor to use 
3139a 74 6f 20 73 65 61 72 63 68 20 74 68 65 20 75 6e  to search the un
3139b 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 74 68  ique index on th
3139c 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  e parent key col
3139d 75 6d 6e 73 20 0d 0a 20 20 20 20 2a 2a 20 69 6e  umns ..    ** in
3139e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
3139f 65 2e 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  e.  */..    sqli
313a0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
313a1 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2d 3e 74  rse, iDb, pTo->t
313a2 6e 75 6d 2c 20 30 2c 20 70 54 6f 2d 3e 7a 4e 61  num, 0, pTo->zNa
313a3 6d 65 29 3b 0d 0a 20 20 20 20 70 50 61 72 73 65  me);..    pParse
313a4 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 0d 0a 20 20 20  ->nTab++;....   
313a5 20 69 66 28 20 72 65 67 4f 6c 64 21 3d 30 20 29   if( regOld!=0 )
313a6 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 6f  {..      /* A ro
313a7 77 20 69 73 20 62 65 69 6e 67 20 72 65 6d 6f 76  w is being remov
313a8 65 64 20 66 72 6f 6d 20 74 68 65 20 63 68 69 6c  ed from the chil
313a9 64 20 74 61 62 6c 65 2e 20 53 65 61 72 63 68 20  d table. Search 
313aa 66 6f 72 20 74 68 65 20 70 61 72 65 6e 74 2e 0d  for the parent..
313ab 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65  .      ** If the
313ac 20 70 61 72 65 6e 74 20 64 6f 65 73 20 6e 6f 74   parent does not
313ad 20 65 78 69 73 74 2c 20 72 65 6d 6f 76 69 6e 67   exist, removing
313ae 20 74 68 65 20 63 68 69 6c 64 20 72 6f 77 20 72   the child row r
313af 65 73 6f 6c 76 65 73 20 61 6e 20 0d 0a 20 20 20  esolves an ..   
313b0 20 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e     ** outstandin
313b1 67 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  g foreign key co
313b2 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
313b3 6f 6e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 66 6b  on. */..      fk
313b4 4c 6f 6f 6b 75 70 50 61 72 65 6e 74 28 70 50 61  LookupParent(pPa
313b5 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2c 20 70  rse, iDb, pTo, p
313b6 49 64 78 2c 20 70 46 4b 65 79 2c 20 61 69 43 6f  Idx, pFKey, aiCo
313b7 6c 2c 20 72 65 67 4f 6c 64 2c 20 2d 31 2c 69 73  l, regOld, -1,is
313b8 49 67 6e 6f 72 65 29 3b 0d 0a 20 20 20 20 7d 0d  Ignore);..    }.
313b9 0a 20 20 20 20 69 66 28 20 72 65 67 4e 65 77 21  .    if( regNew!
313ba 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  =0 ){..      /* 
313bb 41 20 72 6f 77 20 69 73 20 62 65 69 6e 67 20 61  A row is being a
313bc 64 64 65 64 20 74 6f 20 74 68 65 20 63 68 69 6c  dded to the chil
313bd 64 20 74 61 62 6c 65 2e 20 49 66 20 61 20 70 61  d table. If a pa
313be 72 65 6e 74 20 72 6f 77 20 63 61 6e 6e 6f 74 0d  rent row cannot.
313bf 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 66 6f 75  .      ** be fou
313c0 6e 64 2c 20 61 64 64 69 6e 67 20 74 68 65 20 63  nd, adding the c
313c1 68 69 6c 64 20 72 6f 77 20 68 61 73 20 76 69 6f  hild row has vio
313c2 6c 61 74 65 64 20 74 68 65 20 46 4b 20 63 6f 6e  lated the FK con
313c3 73 74 72 61 69 6e 74 2e 20 2a 2f 20 0d 0a 20 20  straint. */ ..  
313c4 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65      fkLookupPare
313c5 6e 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  nt(pParse, iDb, 
313c6 70 54 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65 79  pTo, pIdx, pFKey
313c7 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77 2c  , aiCol, regNew,
313c8 20 2b 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0d 0a   +1,isIgnore);..
313c9 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71 6c      }....    sql
313ca 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61  ite3DbFree(db, a
313cb 69 46 72 65 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  iFree);..  }....
313cc 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    /* Loop throug
313cd 68 20 61 6c 6c 20 74 68 65 20 66 6f 72 65 69 67  h all the foreig
313ce 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
313cf 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  s that refer to 
313d0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  this table */.. 
313d1 20 66 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c   for(pFKey = sql
313d2 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
313d3 28 70 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70  (pTab); pFKey; p
313d4 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78  FKey=pFKey->pNex
313d5 74 54 6f 29 7b 0d 0a 20 20 20 20 49 6e 64 65 78  tTo){..    Index
313d6 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *pIdx = 0;     
313d7 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65           /* Fore
313d8 69 67 6e 20 6b 65 79 20 69 6e 64 65 78 20 66 6f  ign key index fo
313d9 72 20 70 46 4b 65 79 20 2a 2f 0d 0a 20 20 20 20  r pFKey */..    
313da 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0d 0a  SrcList *pSrc;..
313db 20 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20 3d      int *aiCol =
313dc 20 30 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 21   0;....    if( !
313dd 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
313de 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  d && !pParse->pT
313df 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50 61 72  oplevel && !pPar
313e0 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  se->isMultiWrite
313e1 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   ){..      asser
313e2 74 28 20 72 65 67 4f 6c 64 3d 3d 30 20 26 26 20  t( regOld==0 && 
313e3 72 65 67 4e 65 77 21 3d 30 20 29 3b 0d 0a 20 20  regNew!=0 );..  
313e4 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6e 67      /* Inserting
313e5 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e   a single row in
313e6 74 6f 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c  to a parent tabl
313e7 65 20 63 61 6e 6e 6f 74 20 63 61 75 73 65 20 61  e cannot cause a
313e8 6e 20 69 6d 6d 65 64 69 61 74 65 0d 0a 20 20 20  n immediate..   
313e9 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65     ** foreign ke
313ea 79 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 53 6f 20  y violation. So 
313eb 64 6f 20 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68  do nothing in th
313ec 69 73 20 63 61 73 65 2e 20 20 2a 2f 0d 0a 20 20  is case.  */..  
313ed 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20      continue;.. 
313ee 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20     }....    if( 
313ef 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28  locateFkeyIndex(
313f0 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46  pParse, pTab, pF
313f1 4b 65 79 2c 20 26 70 49 64 78 2c 20 26 61 69 43  Key, &pIdx, &aiC
313f2 6f 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  ol) ){..      if
313f3 28 20 21 69 73 49 67 6e 6f 72 65 45 72 72 6f 72  ( !isIgnoreError
313f4 73 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  s || db->mallocF
313f5 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0d  ailed ) return;.
313f6 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
313f7 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73  ..    }..    ass
313f8 65 72 74 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46  ert( aiCol || pF
313f9 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0d  Key->nCol==1 );.
313fa 0a 0d 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  ...    /* Create
313fb 20 61 20 53 72 63 4c 69 73 74 20 73 74 72 75 63   a SrcList struc
313fc 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
313fd 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 28  a single table (
313fe 74 68 65 20 74 61 62 6c 65 20 0d 0a 20 20 20 20  the table ..    
313ff 2a 2a 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  ** the foreign k
31400 65 79 20 74 68 61 74 20 72 65 66 65 72 73 20 74  ey that refers t
31401 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 20  o this table is 
31402 61 74 74 61 63 68 65 64 20 74 6f 29 2e 20 54 68  attached to). Th
31403 69 73 0d 0a 20 20 20 20 2a 2a 20 69 73 20 72 65  is..    ** is re
31404 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 73  quired for the s
31405 71 6c 69 74 65 33 57 68 65 72 65 58 58 58 28 29  qlite3WhereXXX()
31406 20 69 6e 74 65 72 66 61 63 65 2e 20 20 2a 2f 0d   interface.  */.
31407 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69  .    pSrc = sqli
31408 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
31409 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a  (db, 0, 0, 0);..
3140a 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b 0d      if( pSrc ){.
3140b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
3140c 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
3140d 6d 20 3d 20 70 53 72 63 2d 3e 61 3b 0d 0a 20 20  m = pSrc->a;..  
3140e 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 20      pItem->pTab 
3140f 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 3b 0d  = pFKey->pFrom;.
31410 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e  .      pItem->zN
31411 61 6d 65 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72  ame = pFKey->pFr
31412 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20  om->zName;..    
31413 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 6e    pItem->pTab->n
31414 52 65 66 2b 2b 3b 0d 0a 20 20 20 20 20 20 70 49  Ref++;..      pI
31415 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70  tem->iCursor = p
31416 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a  Parse->nTab++;..
31417 20 20 0d 0a 20 20 20 20 20 20 69 66 28 20 72 65    ..      if( re
31418 67 4e 65 77 21 3d 30 20 29 7b 0d 0a 20 20 20 20  gNew!=0 ){..    
31419 20 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64 72      fkScanChildr
3141a 65 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c  en(pParse, pSrc,
3141b 20 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46 4b   pTab, pIdx, pFK
3141c 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65  ey, aiCol, regNe
3141d 77 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20 7d  w, -1);..      }
3141e 0d 0a 20 20 20 20 20 20 69 66 28 20 72 65 67 4f  ..      if( regO
3141f 6c 64 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ld!=0 ){..      
31420 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
31421 20 61 20 52 45 53 54 52 49 43 54 20 61 63 74 69   a RESTRICT acti
31422 6f 6e 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  on configured fo
31423 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  r the current op
31424 65 72 61 74 69 6f 6e 0d 0a 20 20 20 20 20 20 20  eration..       
31425 20 2a 2a 20 6f 6e 20 74 68 65 20 70 61 72 65 6e   ** on the paren
31426 74 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20  t table of this 
31427 46 4b 2c 20 74 68 65 6e 20 74 68 72 6f 77 20 61  FK, then throw a
31428 6e 20 65 78 63 65 70 74 69 6f 6e 20 0d 0a 20 20  n exception ..  
31429 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61        ** immedia
3142a 74 65 6c 79 20 69 66 20 74 68 65 20 46 4b 20 63  tely if the FK c
3142b 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f  onstraint is vio
3142c 6c 61 74 65 64 2c 20 65 76 65 6e 20 69 66 20 74  lated, even if t
3142d 68 69 73 20 69 73 20 61 0d 0a 20 20 20 20 20 20  his is a..      
3142e 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20 74 72    ** deferred tr
3142f 69 67 67 65 72 2e 20 54 68 61 74 27 73 20 77 68  igger. That's wh
31430 61 74 20 52 45 53 54 52 49 43 54 20 6d 65 61 6e  at RESTRICT mean
31431 73 2e 20 54 6f 20 64 65 66 65 72 20 63 68 65 63  s. To defer chec
31432 6b 69 6e 67 0d 0a 20 20 20 20 20 20 20 20 2a 2a  king..        **
31433 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2c   the constraint,
31434 20 74 68 65 20 46 4b 20 73 68 6f 75 6c 64 20 73   the FK should s
31435 70 65 63 69 66 79 20 4e 4f 20 41 43 54 49 4f 4e  pecify NO ACTION
31436 20 28 72 65 70 72 65 73 65 6e 74 65 64 0d 0a 20   (represented.. 
31437 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20         ** using 
31438 4f 45 5f 4e 6f 6e 65 29 2e 20 4e 4f 20 41 43 54  OE_None). NO ACT
31439 49 4f 4e 20 69 73 20 74 68 65 20 64 65 66 61 75  ION is the defau
3143a 6c 74 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20  lt.  */..       
3143b 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28   fkScanChildren(
3143c 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 54  pParse, pSrc, pT
3143d 61 62 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c  ab, pIdx, pFKey,
3143e 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c 20   aiCol, regOld, 
3143f 31 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  1);..      }..  
31440 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65      pItem->zName
31441 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71 6c   = 0;..      sql
31442 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
31443 65 28 64 62 2c 20 70 53 72 63 29 3b 0d 0a 20 20  e(db, pSrc);..  
31444 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
31445 44 62 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c  DbFree(db, aiCol
31446 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 23 64  );..  }..}....#d
31447 65 66 69 6e 65 20 43 4f 4c 55 4d 4e 5f 4d 41 53  efine COLUMN_MAS
31448 4b 28 78 29 20 28 28 28 78 29 3e 33 31 29 20 3f  K(x) (((x)>31) ?
31449 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 28   0xffffffff : ((
3144a 75 33 32 29 31 3c 3c 28 78 29 29 29 0d 0a 0d 0a  u32)1<<(x)))....
3144b 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
3144c 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
3144d 65 66 6f 72 65 20 67 65 6e 65 72 61 74 69 6e 67  efore generating
3144e 20 63 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20   code to update 
3144f 6f 72 20 64 65 6c 65 74 65 20 61 20 0d 0a 2a 2a  or delete a ..**
31450 20 72 6f 77 20 63 6f 6e 74 61 69 6e 65 64 20 69   row contained i
31451 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 0d 0a 2a  n table pTab...*
31452 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
31453 45 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b 4f  E u32 sqlite3FkO
31454 6c 64 6d 61 73 6b 28 0d 0a 20 20 50 61 72 73 65  ldmask(..  Parse
31455 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
31456 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
31457 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  rse context */..
31458 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20    Table *pTab   
31459 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3145a 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67    /* Table being
3145b 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0d 0a 29 7b   modified */..){
3145c 0d 0a 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30  ..  u32 mask = 0
3145d 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
3145e 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
3145f 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
31460 0d 0a 20 20 20 20 46 4b 65 79 20 2a 70 3b 0d 0a  ..    FKey *p;..
31461 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20      int i;..    
31462 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70 46 4b 65  for(p=pTab->pFKe
31463 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  y; p; p=p->pNext
31464 46 72 6f 6d 29 7b 0d 0a 20 20 20 20 20 20 66 6f  From){..      fo
31465 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c  r(i=0; i<p->nCol
31466 3b 20 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20 43  ; i++) mask |= C
31467 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70 2d 3e 61 43  OLUMN_MASK(p->aC
31468 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 29 3b 0d 0a 20  ol[i].iFrom);.. 
31469 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72 28 70 3d     }..    for(p=
3146a 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
3146b 63 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d  ces(pTab); p; p=
3146c 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0d 0a 20 20  p->pNextTo){..  
3146d 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
3146e 3d 20 30 3b 0d 0a 20 20 20 20 20 20 6c 6f 63 61  = 0;..      loca
3146f 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72  teFkeyIndex(pPar
31470 73 65 2c 20 70 54 61 62 2c 20 70 2c 20 26 70 49  se, pTab, p, &pI
31471 64 78 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69  dx, 0);..      i
31472 66 28 20 70 49 64 78 20 29 7b 0d 0a 20 20 20 20  f( pIdx ){..    
31473 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
31474 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
31475 2b 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c 55 4d  +) mask |= COLUM
31476 4e 5f 4d 41 53 4b 28 70 49 64 78 2d 3e 61 69 43  N_MASK(pIdx->aiC
31477 6f 6c 75 6d 6e 5b 69 5d 29 3b 0d 0a 20 20 20 20  olumn[i]);..    
31478 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
31479 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0d  .  return mask;.
3147a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
3147b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
3147c 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65 6e 65  lled before gene
3147d 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  rating code to u
3147e 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65 20  pdate or delete 
3147f 61 20 0d 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61  a ..** row conta
31480 69 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70 54  ined in table pT
31481 61 62 2e 20 49 66 20 74 68 65 20 6f 70 65 72 61  ab. If the opera
31482 74 69 6f 6e 20 69 73 20 61 20 44 45 4c 45 54 45  tion is a DELETE
31483 2c 20 74 68 65 6e 0d 0a 2a 2a 20 70 61 72 61 6d  , then..** param
31484 65 74 65 72 20 61 43 68 61 6e 67 65 20 69 73 20  eter aChange is 
31485 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 76 61  passed a NULL va
31486 6c 75 65 2e 20 46 6f 72 20 61 6e 20 55 50 44 41  lue. For an UPDA
31487 54 45 2c 20 61 43 68 61 6e 67 65 20 70 6f 69 6e  TE, aChange poin
31488 74 73 0d 0a 2a 2a 20 74 6f 20 61 6e 20 61 72 72  ts..** to an arr
31489 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68  ay of size N, wh
3148a 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
3148b 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
3148c 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 0d 0a 2a  n table pTab...*
3148d 2a 20 49 66 20 74 68 65 20 69 27 74 68 20 63 6f  * If the i'th co
3148e 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6d 6f 64 69  lumn is not modi
3148f 66 69 65 64 20 62 79 20 74 68 65 20 55 50 44 41  fied by the UPDA
31490 54 45 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72  TE, then the cor
31491 72 65 73 70 6f 6e 64 69 6e 67 20 0d 0a 2a 2a 20  responding ..** 
31492 65 6e 74 72 79 20 69 6e 20 74 68 65 20 61 43 68  entry in the aCh
31493 61 6e 67 65 5b 5d 20 61 72 72 61 79 20 69 73 20  ange[] array is 
31494 73 65 74 20 74 6f 20 2d 31 2e 20 49 66 20 74 68  set to -1. If th
31495 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 6f 64 69  e column is modi
31496 66 69 65 64 2c 0d 0a 2a 2a 20 74 68 65 20 76 61  fied,..** the va
31497 6c 75 65 20 69 73 20 30 20 6f 72 20 67 72 65 61  lue is 0 or grea
31498 74 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 63  ter. Parameter c
31499 68 6e 67 52 6f 77 69 64 20 69 73 20 73 65 74 20  hngRowid is set 
3149a 74 6f 20 74 72 75 65 20 69 66 20 74 68 65 0d 0a  to true if the..
3149b 2a 2a 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  ** UPDATE statem
3149c 65 6e 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65  ent modifies the
3149d 20 72 6f 77 69 64 20 66 69 65 6c 64 73 20 6f 66   rowid fields of
3149e 20 74 68 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d   the table...**.
3149f 0a 2a 2a 20 49 66 20 61 6e 79 20 66 6f 72 65 69  .** If any forei
314a0 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e  gn key processin
314a1 67 20 77 69 6c 6c 20 62 65 20 72 65 71 75 69 72  g will be requir
314a2 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ed, this functio
314a3 6e 20 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 74 72  n returns..** tr
314a4 75 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  ue. If there is 
314a5 6e 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 72  no foreign key r
314a6 65 6c 61 74 65 64 20 70 72 6f 63 65 73 73 69 6e  elated processin
314a7 67 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  g, this function
314a8 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66 61   ..** returns fa
314a9 6c 73 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  lse...*/..SQLITE
314aa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
314ab 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 0d  ite3FkRequired(.
314ac 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
314ad 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
314ae 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
314af 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20  ext */..  Table 
314b0 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20  *pTab,          
314b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
314b2 6c 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65  le being modifie
314b3 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 43 68  d */..  int *aCh
314b4 61 6e 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  ange,           
314b5 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 4e          /* Non-N
314b6 55 4c 4c 20 66 6f 72 20 55 50 44 41 54 45 20 6f  ULL for UPDATE o
314b7 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20  perations */..  
314b8 69 6e 74 20 63 68 6e 67 52 6f 77 69 64 20 20 20  int chngRowid   
314b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
314ba 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41  /* True for UPDA
314bb 54 45 20 74 68 61 74 20 61 66 66 65 63 74 73 20  TE that affects 
314bc 72 6f 77 69 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  rowid */..){..  
314bd 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  if( pParse->db->
314be 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72  flags&SQLITE_For
314bf 65 69 67 6e 4b 65 79 73 20 29 7b 0d 0a 20 20 20  eignKeys ){..   
314c0 20 69 66 28 20 21 61 43 68 61 6e 67 65 20 29 7b   if( !aChange ){
314c1 0d 0a 20 20 20 20 20 20 2f 2a 20 41 20 44 45 4c  ..      /* A DEL
314c2 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 46  ETE operation. F
314c3 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
314c4 73 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65  ssing is require
314c5 64 20 69 66 20 74 68 65 20 0d 0a 20 20 20 20 20  d if the ..     
314c6 20 2a 2a 20 74 61 62 6c 65 20 69 6e 20 71 75 65   ** table in que
314c7 73 74 69 6f 6e 20 69 73 20 65 69 74 68 65 72 20  stion is either 
314c8 74 68 65 20 63 68 69 6c 64 20 6f 72 20 70 61 72  the child or par
314c9 65 6e 74 20 74 61 62 6c 65 20 66 6f 72 20 61 6e  ent table for an
314ca 79 20 0d 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  y ..      ** for
314cb 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
314cc 69 6e 74 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20  int.  */..      
314cd 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 46  return (sqlite3F
314ce 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
314cf 29 20 7c 7c 20 70 54 61 62 2d 3e 70 46 4b 65 79  ) || pTab->pFKey
314d0 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
314d1 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73        /* This is
314d2 20 61 6e 20 55 50 44 41 54 45 2e 20 46 6f 72 65   an UPDATE. Fore
314d3 69 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69  ign key processi
314d4 6e 67 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69  ng is only requi
314d5 72 65 64 20 69 66 20 74 68 65 0d 0a 20 20 20 20  red if the..    
314d6 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6d    ** operation m
314d7 6f 64 69 66 69 65 73 20 6f 6e 65 20 6f 72 20 6d  odifies one or m
314d8 6f 72 65 20 63 68 69 6c 64 20 6f 72 20 70 61 72  ore child or par
314d9 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e  ent key columns.
314da 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 69   */..      int i
314db 3b 0d 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70  ;..      FKey *p
314dc 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43 68  ;....      /* Ch
314dd 65 63 6b 20 69 66 20 61 6e 79 20 63 68 69 6c 64  eck if any child
314de 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65   key columns are
314df 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
314e0 20 2a 2f 0d 0a 20 20 20 20 20 20 66 6f 72 28 70   */..      for(p
314e1 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b  =pTab->pFKey; p;
314e2 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29   p=p->pNextFrom)
314e3 7b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  {..        for(i
314e4 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69  =0; i<p->nCol; i
314e5 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ++){..          
314e6 69 6e 74 20 69 43 68 69 6c 64 4b 65 79 20 3d 20  int iChildKey = 
314e7 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d  p->aCol[i].iFrom
314e8 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
314e9 20 61 43 68 61 6e 67 65 5b 69 43 68 69 6c 64 4b   aChange[iChildK
314ea 65 79 5d 3e 3d 30 20 29 20 72 65 74 75 72 6e 20  ey]>=0 ) return 
314eb 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  1;..          if
314ec 28 20 69 43 68 69 6c 64 4b 65 79 3d 3d 70 54 61  ( iChildKey==pTa
314ed 62 2d 3e 69 50 4b 65 79 20 26 26 20 63 68 6e 67  b->iPKey && chng
314ee 52 6f 77 69 64 20 29 20 72 65 74 75 72 6e 20 31  Rowid ) return 1
314ef 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
314f0 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f      }....      /
314f1 2a 20 43 68 65 63 6b 20 69 66 20 61 6e 79 20 70  * Check if any p
314f2 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
314f3 73 20 61 72 65 20 62 65 69 6e 67 20 6d 6f 64 69  s are being modi
314f4 66 69 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  fied. */..      
314f5 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52  for(p=sqlite3FkR
314f6 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b  eferences(pTab);
314f7 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f   p; p=p->pNextTo
314f8 29 7b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ){..        for(
314f9 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
314fa 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  i++){..         
314fb 20 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70 2d   char *zKey = p-
314fc 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 3b 0d 0a  >aCol[i].zCol;..
314fd 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 4b            int iK
314fe 65 79 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 66  ey;..          f
314ff 6f 72 28 69 4b 65 79 3d 30 3b 20 69 4b 65 79 3c  or(iKey=0; iKey<
31500 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 4b 65 79  pTab->nCol; iKey
31501 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ++){..          
31502 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
31503 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 4b 65   &pTab->aCol[iKe
31504 79 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  y];..           
31505 20 69 66 28 20 28 7a 4b 65 79 20 3f 20 21 73 71   if( (zKey ? !sq
31506 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
31507 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20  l->zName, zKey) 
31508 3a 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  : pCol->isPrimKe
31509 79 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  y) ){..         
3150a 20 20 20 20 20 69 66 28 20 61 43 68 61 6e 67 65       if( aChange
3150b 5b 69 4b 65 79 5d 3e 3d 30 20 29 20 72 65 74 75  [iKey]>=0 ) retu
3150c 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20  rn 1;..         
3150d 20 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d 70       if( iKey==p
3150e 54 61 62 2d 3e 69 50 4b 65 79 20 26 26 20 63 68  Tab->iPKey && ch
3150f 6e 67 52 6f 77 69 64 20 29 20 72 65 74 75 72 6e  ngRowid ) return
31510 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   1;..           
31511 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d   }..          }.
31512 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
31513 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
31514 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d  .  return 0;..}.
31515 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
31516 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
31517 64 20 77 68 65 6e 20 61 6e 20 55 50 44 41 54 45  d when an UPDATE
31518 20 6f 72 20 44 45 4c 45 54 45 20 6f 70 65 72 61   or DELETE opera
31519 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 0d 0a  tion is being ..
3151a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 74  ** compiled on t
3151b 61 62 6c 65 20 70 54 61 62 2c 20 77 68 69 63 68  able pTab, which
3151c 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
3151d 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 2d  able of foreign-
3151e 6b 65 79 20 70 46 4b 65 79 2e 0d 0a 2a 2a 20 49  key pFKey...** I
3151f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  f the current op
31520 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50  eration is an UP
31521 44 41 54 45 2c 20 74 68 65 6e 20 74 68 65 20 70  DATE, then the p
31522 43 68 61 6e 67 65 73 20 70 61 72 61 6d 65 74 65  Changes paramete
31523 72 20 69 73 0d 0a 2a 2a 20 70 61 73 73 65 64 20  r is..** passed 
31524 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
31525 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
31526 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
31527 20 49 66 20 69 74 20 69 73 20 61 0d 0a 2a 2a 20   If it is a..** 
31528 44 45 4c 45 54 45 2c 20 70 43 68 61 6e 67 65 73  DELETE, pChanges
31529 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 4c   is passed a NUL
3152a 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2a 0d 0a  L pointer...**..
3152b 2a 2a 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  ** It returns a 
3152c 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69  pointer to a Tri
3152d 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20 63  gger structure c
3152e 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74 72 69 67  ontaining a trig
3152f 67 65 72 0d 0a 2a 2a 20 65 71 75 69 76 61 6c 65  ger..** equivale
31530 6e 74 20 74 6f 20 74 68 65 20 4f 4e 20 55 50 44  nt to the ON UPD
31531 41 54 45 20 6f 72 20 4f 4e 20 44 45 4c 45 54 45  ATE or ON DELETE
31532 20 61 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65   action specifie
31533 64 20 62 79 20 70 46 4b 65 79 2e 0d 0a 2a 2a 20  d by pFKey...** 
31534 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  If the action is
31535 20 22 4e 4f 20 41 43 54 49 4f 4e 22 20 6f 72 20   "NO ACTION" or 
31536 22 52 45 53 54 52 49 43 54 22 2c 20 74 68 65 6e  "RESTRICT", then
31537 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
31538 69 73 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  is..** returned 
31539 28 74 68 65 73 65 20 61 63 74 69 6f 6e 73 20 72  (these actions r
3153a 65 71 75 69 72 65 20 6e 6f 20 73 70 65 63 69 61  equire no specia
3153b 6c 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68  l handling by th
3153c 65 20 74 72 69 67 67 65 72 73 0d 0a 2a 2a 20 73  e triggers..** s
3153d 75 62 2d 73 79 73 74 65 6d 2c 20 63 6f 64 65 20  ub-system, code 
3153e 66 6f 72 20 74 68 65 6d 20 69 73 20 63 72 65 61  for them is crea
3153f 74 65 64 20 62 79 20 66 6b 53 63 61 6e 43 68 69  ted by fkScanChi
31540 6c 64 72 65 6e 28 29 29 2e 0d 0a 2a 2a 0d 0a 2a  ldren())...**..*
31541 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  * For example, i
31542 66 20 70 46 4b 65 79 20 69 73 20 74 68 65 20 66  f pFKey is the f
31543 6f 72 65 69 67 6e 20 6b 65 79 20 61 6e 64 20 70  oreign key and p
31544 54 61 62 20 69 73 20 74 61 62 6c 65 20 22 70 22  Tab is table "p"
31545 20 69 6e 20 0d 0a 2a 2a 20 74 68 65 20 66 6f 6c   in ..** the fol
31546 6c 6f 77 69 6e 67 20 73 63 68 65 6d 61 3a 0d 0a  lowing schema:..
31547 2a 2a 0d 0a 2a 2a 20 20 20 43 52 45 41 54 45 20  **..**   CREATE 
31548 54 41 42 4c 45 20 70 28 70 6b 20 50 52 49 4d 41  TABLE p(pk PRIMA
31549 52 59 20 4b 45 59 29 3b 0d 0a 2a 2a 20 20 20 43  RY KEY);..**   C
3154a 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 63 6b  REATE TABLE c(ck
3154b 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
3154c 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 29   DELETE CASCADE)
3154d 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 74 68 65 6e 20 74  ;..**..** then t
3154e 68 65 20 72 65 74 75 72 6e 65 64 20 74 72 69 67  he returned trig
3154f 67 65 72 20 73 74 72 75 63 74 75 72 65 20 69 73  ger structure is
31550 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0d   equivalent to:.
31551 0a 2a 2a 0d 0a 2a 2a 20 20 20 43 52 45 41 54 45  .**..**   CREATE
31552 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 44 45 4c   TRIGGER ... DEL
31553 45 54 45 20 4f 4e 20 70 20 42 45 47 49 4e 0d 0a  ETE ON p BEGIN..
31554 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52  **     DELETE FR
31555 4f 4d 20 63 20 57 48 45 52 45 20 63 6b 20 3d 20  OM c WHERE ck = 
31556 6f 6c 64 2e 70 6b 3b 0d 0a 2a 2a 20 20 20 45 4e  old.pk;..**   EN
31557 44 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  D;..**..** The r
31558 65 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20  eturned pointer 
31559 69 73 20 63 61 63 68 65 64 20 61 73 20 70 61 72  is cached as par
3155a 74 20 6f 66 20 74 68 65 20 66 6f 72 65 69 67 6e  t of the foreign
3155b 20 6b 65 79 20 6f 62 6a 65 63 74 2e 20 49 74 0d   key object. It.
3155c 0a 2a 2a 20 69 73 20 65 76 65 6e 74 75 61 6c 6c  .** is eventuall
3155d 79 20 66 72 65 65 64 20 61 6c 6f 6e 67 20 77 69  y freed along wi
3155e 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
3155f 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  he foreign key o
31560 62 6a 65 63 74 20 62 79 20 0d 0a 2a 2a 20 73 71  bject by ..** sq
31561 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 29 2e  lite3FkDelete().
31562 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 54 72 69  ..*/..static Tri
31563 67 67 65 72 20 2a 66 6b 41 63 74 69 6f 6e 54 72  gger *fkActionTr
31564 69 67 67 65 72 28 0d 0a 20 20 50 61 72 73 65 20  igger(..  Parse 
31565 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
31566 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
31567 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20  se context */.. 
31568 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
31569 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3156a 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20   /* Table being 
3156b 75 70 64 61 74 65 64 20 6f 72 20 64 65 6c 65 74  updated or delet
3156c 65 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 46 4b  ed from */..  FK
3156d 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20 20  ey *pFKey,      
3156e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3156f 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20   Foreign key to 
31570 67 65 74 20 61 63 74 69 6f 6e 20 66 6f 72 20 2a  get action for *
31571 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  /..  ExprList *p
31572 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  Changes         
31573 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 2d 6c       /* Change-l
31574 69 73 74 20 66 6f 72 20 55 50 44 41 54 45 2c 20  ist for UPDATE, 
31575 4e 55 4c 4c 20 66 6f 72 20 44 45 4c 45 54 45 20  NULL for DELETE 
31576 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
31577 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
31578 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74  db;       /* Dat
31579 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  abase handle */.
3157a 0a 20 20 69 6e 74 20 61 63 74 69 6f 6e 3b 20 20  .  int action;  
3157b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3157c 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 4f 45 5f     /* One of OE_
3157d 4e 6f 6e 65 2c 20 4f 45 5f 43 61 73 63 61 64 65  None, OE_Cascade
3157e 20 65 74 63 2e 20 2a 2f 0d 0a 20 20 54 72 69 67   etc. */..  Trig
3157f 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
31580 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
31581 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f  rigger definitio
31582 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a  n to return */..
31583 20 20 69 6e 74 20 69 41 63 74 69 6f 6e 20 3d 20    int iAction = 
31584 28 70 43 68 61 6e 67 65 73 21 3d 30 29 3b 20 20  (pChanges!=0);  
31585 20 20 2f 2a 20 31 20 66 6f 72 20 55 50 44 41 54    /* 1 for UPDAT
31586 45 2c 20 30 20 66 6f 72 20 44 45 4c 45 54 45 20  E, 0 for DELETE 
31587 2a 2f 0d 0a 0d 0a 20 20 61 63 74 69 6f 6e 20 3d  */....  action =
31588 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b   pFKey->aAction[
31589 69 41 63 74 69 6f 6e 5d 3b 0d 0a 20 20 70 54 72  iAction];..  pTr
3158a 69 67 67 65 72 20 3d 20 70 46 4b 65 79 2d 3e 61  igger = pFKey->a
3158b 70 54 72 69 67 67 65 72 5b 69 41 63 74 69 6f 6e  pTrigger[iAction
3158c 5d 3b 0d 0a 0d 0a 20 20 69 66 28 20 61 63 74 69  ];....  if( acti
3158d 6f 6e 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 21  on!=OE_None && !
3158e 70 54 72 69 67 67 65 72 20 29 7b 0d 0a 20 20 20  pTrigger ){..   
3158f 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73   u8 enableLookas
31590 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ide;           /
31591 2a 20 43 6f 70 79 20 6f 66 20 64 62 2d 3e 6c 6f  * Copy of db->lo
31592 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
31593 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 63 6f   */..    char co
31594 6e 73 74 20 2a 7a 46 72 6f 6d 3b 20 20 20 20 20  nst *zFrom;     
31595 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
31596 66 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f  f child table */
31597 0d 0a 20 20 20 20 69 6e 74 20 6e 46 72 6f 6d 3b  ..    int nFrom;
31598 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31599 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e      /* Length in
3159a 20 62 79 74 65 73 20 6f 66 20 7a 46 72 6f 6d 20   bytes of zFrom 
3159b 2a 2f 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  */..    Index *p
3159c 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Idx = 0;        
3159d 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
3159e 6b 65 79 20 69 6e 64 65 78 20 66 6f 72 20 74 68  key index for th
3159f 69 73 20 46 4b 20 2a 2f 0d 0a 20 20 20 20 69 6e  is FK */..    in
315a0 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 20 20 20  t *aiCol = 0;   
315a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
315a2 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 73 20  hild table cols 
315a3 2d 3e 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  -> parent key co
315a4 6c 73 20 2a 2f 0d 0a 20 20 20 20 54 72 69 67 67  ls */..    Trigg
315a5 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 3d 20  erStep *pStep = 
315a6 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  0;        /* Fir
315a7 73 74 20 28 6f 6e 6c 79 29 20 73 74 65 70 20 6f  st (only) step o
315a8 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  f trigger progra
315a9 6d 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a  m */..    Expr *
315aa 70 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20  pWhere = 0;     
315ab 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45          /* WHERE
315ac 20 63 6c 61 75 73 65 20 6f 66 20 74 72 69 67 67   clause of trigg
315ad 65 72 20 73 74 65 70 20 2a 2f 0d 0a 20 20 20 20  er step */..    
315ae 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
315af 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
315b0 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20 69 66   Changes list if
315b1 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
315b2 44 45 20 2a 2f 0d 0a 20 20 20 20 53 65 6c 65 63  DE */..    Selec
315b3 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b 20  t *pSelect = 0; 
315b4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 52           /* If R
315b5 45 53 54 52 49 43 54 2c 20 22 53 45 4c 45 43 54  ESTRICT, "SELECT
315b6 20 52 41 49 53 45 28 2e 2e 2e 29 22 20 2a 2f 0d   RAISE(...)" */.
315b7 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
315b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315b9 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
315ba 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20  ariable */..    
315bb 45 78 70 72 20 2a 70 57 68 65 6e 20 3d 20 30 3b  Expr *pWhen = 0;
315bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
315bd 20 57 48 45 4e 20 63 6c 61 75 73 65 20 66 6f 72   WHEN clause for
315be 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0d   the trigger */.
315bf 0a 0d 0a 20 20 20 20 69 66 28 20 6c 6f 63 61 74  ...    if( locat
315c0 65 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73  eFkeyIndex(pPars
315c1 65 2c 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20  e, pTab, pFKey, 
315c2 26 70 49 64 78 2c 20 26 61 69 43 6f 6c 29 20 29  &pIdx, &aiCol) )
315c3 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20   return 0;..    
315c4 61 73 73 65 72 74 28 20 61 69 43 6f 6c 20 7c 7c  assert( aiCol ||
315c5 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20   pFKey->nCol==1 
315c6 29 3b 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 69 3d  );....    for(i=
315c7 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c  0; i<pFKey->nCol
315c8 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 54  ; i++){..      T
315c9 6f 6b 65 6e 20 74 4f 6c 64 20 3d 20 7b 20 22 6f  oken tOld = { "o
315ca 6c 64 22 2c 20 33 20 7d 3b 20 20 2f 2a 20 4c 69  ld", 3 };  /* Li
315cb 74 65 72 61 6c 20 22 6f 6c 64 22 20 74 6f 6b 65  teral "old" toke
315cc 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 54 6f 6b 65  n */..      Toke
315cd 6e 20 74 4e 65 77 20 3d 20 7b 20 22 6e 65 77 22  n tNew = { "new"
315ce 2c 20 33 20 7d 3b 20 20 2f 2a 20 4c 69 74 65 72  , 3 };  /* Liter
315cf 61 6c 20 22 6e 65 77 22 20 74 6f 6b 65 6e 20 2a  al "new" token *
315d0 2f 0d 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74  /..      Token t
315d1 46 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20  FromCol;        
315d2 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
315d3 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20  column in child 
315d4 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 20 20  table */..      
315d5 54 6f 6b 65 6e 20 74 54 6f 43 6f 6c 3b 20 20 20  Token tToCol;   
315d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
315d7 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  ame of column in
315d8 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f   parent table */
315d9 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 46 72 6f  ..      int iFro
315da 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  mCol;           
315db 20 20 20 20 2f 2a 20 49 64 78 20 6f 66 20 63 6f      /* Idx of co
315dc 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61  lumn in child ta
315dd 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 20 20 45 78  ble */..      Ex
315de 70 72 20 2a 70 45 71 3b 20 20 20 20 20 20 20 20  pr *pEq;        
315df 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 46 72            /* tFr
315e0 6f 6d 43 6f 6c 20 3d 20 4f 4c 44 2e 74 54 6f 43  omCol = OLD.tToC
315e1 6f 6c 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 69  ol */....      i
315e2 46 72 6f 6d 43 6f 6c 20 3d 20 61 69 43 6f 6c 20  FromCol = aiCol 
315e3 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b  ? aiCol[i] : pFK
315e4 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f  ey->aCol[0].iFro
315e5 6d 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  m;..      assert
315e6 28 20 69 46 72 6f 6d 43 6f 6c 3e 3d 30 20 29 3b  ( iFromCol>=0 );
315e7 0d 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c 2e 7a  ..      tToCol.z
315e8 20 3d 20 70 49 64 78 20 3f 20 70 54 61 62 2d 3e   = pIdx ? pTab->
315e9 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  aCol[pIdx->aiCol
315ea 75 6d 6e 5b 69 5d 5d 2e 7a 4e 61 6d 65 20 3a 20  umn[i]].zName : 
315eb 22 6f 69 64 22 3b 0d 0a 20 20 20 20 20 20 74 46  "oid";..      tF
315ec 72 6f 6d 43 6f 6c 2e 7a 20 3d 20 70 46 4b 65 79  romCol.z = pFKey
315ed 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46  ->pFrom->aCol[iF
315ee 72 6f 6d 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0d 0a  romCol].zName;..
315ef 0d 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c 2e 6e  ..      tToCol.n
315f0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
315f1 33 30 28 74 54 6f 43 6f 6c 2e 7a 29 3b 0d 0a 20  30(tToCol.z);.. 
315f2 20 20 20 20 20 74 46 72 6f 6d 43 6f 6c 2e 6e 20       tFromCol.n 
315f3 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
315f4 30 28 74 46 72 6f 6d 43 6f 6c 2e 7a 29 3b 0d 0a  0(tFromCol.z);..
315f5 0d 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74  ..      /* Creat
315f6 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
315f7 20 22 4f 4c 44 2e 7a 54 6f 43 6f 6c 20 3d 20 7a   "OLD.zToCol = z
315f8 46 72 6f 6d 43 6f 6c 22 2e 20 49 74 20 69 73 20  FromCol". It is 
315f9 69 6d 70 6f 72 74 61 6e 74 0d 0a 20 20 20 20 20  important..     
315fa 20 2a 2a 20 74 68 61 74 20 74 68 65 20 22 4f 4c   ** that the "OL
315fb 44 2e 7a 54 6f 43 6f 6c 22 20 74 65 72 6d 20 69  D.zToCol" term i
315fc 73 20 6f 6e 20 74 68 65 20 4c 48 53 20 6f 66 20  s on the LHS of 
315fd 74 68 65 20 3d 20 6f 70 65 72 61 74 6f 72 2c 20  the = operator, 
315fe 73 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  so..      ** tha
315ff 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 61  t the affinity a
31600 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  nd collation seq
31601 75 65 6e 63 65 20 61 73 73 6f 63 69 61 74 65 64  uence associated
31602 20 77 69 74 68 20 74 68 65 0d 0a 20 20 20 20 20   with the..     
31603 20 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65   ** parent table
31604 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 74 68   are used for th
31605 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 2a 2f  e comparison. */
31606 0d 0a 20 20 20 20 20 20 70 45 71 20 3d 20 73 71  ..      pEq = sq
31607 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
31608 65 2c 20 54 4b 5f 45 51 2c 0d 0a 20 20 20 20 20  e, TK_EQ,..     
31609 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
3160a 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
3160b 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
3160c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3160d 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
3160e 2c 20 26 74 4f 6c 64 29 2c 0d 0a 20 20 20 20 20  , &tOld),..     
3160f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
31610 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
31611 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f 6c  D, 0, 0, &tToCol
31612 29 0d 0a 20 20 20 20 20 20 20 20 20 20 2c 20 30  )..          , 0
31613 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71  ),..          sq
31614 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
31615 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
31616 26 74 46 72 6f 6d 43 6f 6c 29 0d 0a 20 20 20 20  &tFromCol)..    
31617 20 20 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 70    , 0);..      p
31618 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
31619 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65 72  xprAnd(db, pWher
3161a 65 2c 20 70 45 71 29 3b 0d 0a 0d 0a 20 20 20 20  e, pEq);....    
3161b 20 20 2f 2a 20 46 6f 72 20 4f 4e 20 55 50 44 41    /* For ON UPDA
3161c 54 45 2c 20 63 6f 6e 73 74 72 75 63 74 20 74 68  TE, construct th
3161d 65 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 74  e next term of t
3161e 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 2e 0d  he WHEN clause..
3161f 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 66 69  .      ** The fi
31620 6e 61 6c 20 57 48 45 4e 20 63 6c 61 75 73 65 20  nal WHEN clause 
31621 77 69 6c 6c 20 62 65 20 6c 69 6b 65 20 74 68 69  will be like thi
31622 73 3a 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20  s:..      **..  
31623 20 20 20 20 2a 2a 20 20 20 20 57 48 45 4e 20 4e      **    WHEN N
31624 4f 54 28 6f 6c 64 2e 63 6f 6c 31 20 49 53 20 6e  OT(old.col1 IS n
31625 65 77 2e 63 6f 6c 31 20 41 4e 44 20 2e 2e 2e 20  ew.col1 AND ... 
31626 41 4e 44 20 6f 6c 64 2e 63 6f 6c 4e 20 49 53 20  AND old.colN IS 
31627 6e 65 77 2e 63 6f 6c 4e 29 0d 0a 20 20 20 20 20  new.colN)..     
31628 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 70   */..      if( p
31629 43 68 61 6e 67 65 73 20 29 7b 0d 0a 20 20 20 20  Changes ){..    
3162a 20 20 20 20 70 45 71 20 3d 20 73 71 6c 69 74 65      pEq = sqlite
3162b 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3162c 4b 5f 49 53 2c 0d 0a 20 20 20 20 20 20 20 20 20  K_IS,..         
3162d 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28     sqlite3PExpr(
3162e 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
3162f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
31630 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
31631 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
31632 2c 20 26 74 4f 6c 64 29 2c 0d 0a 20 20 20 20 20  , &tOld),..     
31633 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
31634 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
31635 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43  _ID, 0, 0, &tToC
31636 6f 6c 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  ol),..          
31637 20 20 20 20 30 29 2c 0d 0a 20 20 20 20 20 20 20      0),..       
31638 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
31639 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
3163a 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
3163b 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
3163c 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
3163d 20 30 2c 20 26 74 4e 65 77 29 2c 0d 0a 20 20 20   0, &tNew),..   
3163e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3163f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
31640 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54  TK_ID, 0, 0, &tT
31641 6f 43 6f 6c 29 2c 0d 0a 20 20 20 20 20 20 20 20  oCol),..        
31642 20 20 20 20 20 20 30 29 2c 0d 0a 20 20 20 20 20        0),..     
31643 20 20 20 20 20 20 20 30 29 3b 0d 0a 20 20 20 20         0);..    
31644 20 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69      pWhen = sqli
31645 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70  te3ExprAnd(db, p
31646 57 68 65 6e 2c 20 70 45 71 29 3b 0d 0a 20 20 20  When, pEq);..   
31647 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 20 20     }..  ..      
31648 69 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45 5f 52  if( action!=OE_R
31649 65 73 74 72 69 63 74 20 26 26 20 28 61 63 74 69  estrict && (acti
3164a 6f 6e 21 3d 4f 45 5f 43 61 73 63 61 64 65 20 7c  on!=OE_Cascade |
3164b 7c 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0d 0a  | pChanges) ){..
3164c 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e          Expr *pN
3164d 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  ew;..        if(
3164e 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 43 61 73 63   action==OE_Casc
3164f 61 64 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ade ){..        
31650 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
31651 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
31652 5f 44 4f 54 2c 20 0d 0a 20 20 20 20 20 20 20 20  _DOT, ..        
31653 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72      sqlite3PExpr
31654 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
31655 30 2c 20 30 2c 20 26 74 4e 65 77 29 2c 0d 0a 20  0, 0, &tNew),.. 
31656 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
31657 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
31658 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54  TK_ID, 0, 0, &tT
31659 6f 43 6f 6c 29 0d 0a 20 20 20 20 20 20 20 20 20  oCol)..         
3165a 20 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20   , 0);..        
3165b 7d 65 6c 73 65 20 69 66 28 20 61 63 74 69 6f 6e  }else if( action
3165c 3d 3d 4f 45 5f 53 65 74 44 66 6c 74 20 29 7b 0d  ==OE_SetDflt ){.
3165d 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
3165e 2a 70 44 66 6c 74 20 3d 20 70 46 4b 65 79 2d 3e  *pDflt = pFKey->
3165f 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72 6f  pFrom->aCol[iFro
31660 6d 43 6f 6c 5d 2e 70 44 66 6c 74 3b 0d 0a 20 20  mCol].pDflt;..  
31661 20 20 20 20 20 20 20 20 69 66 28 20 70 44 66 6c          if( pDfl
31662 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  t ){..          
31663 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
31664 45 78 70 72 44 75 70 28 64 62 2c 20 70 44 66 6c  ExprDup(db, pDfl
31665 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  t, 0);..        
31666 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
31667 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
31668 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
31669 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c  , TK_NULL, 0, 0,
3166a 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   0);..          
3166b 7d 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  }..        }else
3166c 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65  {..          pNe
3166d 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  w = sqlite3PExpr
3166e 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c  (pParse, TK_NULL
3166f 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  , 0, 0, 0);..   
31670 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
31671 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
31672 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
31673 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 4e 65  arse, pList, pNe
31674 77 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  w);..        sql
31675 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
31676 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ame(pParse, pLis
31677 74 2c 20 26 74 46 72 6f 6d 43 6f 6c 2c 20 30 29  t, &tFromCol, 0)
31678 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
31679 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  }..    sqlite3Db
3167a 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c 29 3b  Free(db, aiCol);
3167b 0d 0a 0d 0a 20 20 20 20 7a 46 72 6f 6d 20 3d 20  ....    zFrom = 
3167c 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 7a 4e  pFKey->pFrom->zN
3167d 61 6d 65 3b 0d 0a 20 20 20 20 6e 46 72 6f 6d 20  ame;..    nFrom 
3167e 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
3167f 30 28 7a 46 72 6f 6d 29 3b 0d 0a 0d 0a 20 20 20  0(zFrom);....   
31680 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f   if( action==OE_
31681 52 65 73 74 72 69 63 74 20 29 7b 0d 0a 20 20 20  Restrict ){..   
31682 20 20 20 54 6f 6b 65 6e 20 74 46 72 6f 6d 3b 0d     Token tFrom;.
31683 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 61  .      Expr *pRa
31684 69 73 65 3b 20 0d 0a 0d 0a 20 20 20 20 20 20 74  ise; ....      t
31685 46 72 6f 6d 2e 7a 20 3d 20 7a 46 72 6f 6d 3b 0d  From.z = zFrom;.
31686 0a 20 20 20 20 20 20 74 46 72 6f 6d 2e 6e 20 3d  .      tFrom.n =
31687 20 6e 46 72 6f 6d 3b 0d 0a 20 20 20 20 20 20 70   nFrom;..      p
31688 52 61 69 73 65 20 3d 20 73 71 6c 69 74 65 33 45  Raise = sqlite3E
31689 78 70 72 28 64 62 2c 20 54 4b 5f 52 41 49 53 45  xpr(db, TK_RAISE
3168a 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  , "foreign key c
3168b 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
3168c 22 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  ");..      if( p
3168d 52 61 69 73 65 20 29 7b 0d 0a 20 20 20 20 20 20  Raise ){..      
3168e 20 20 70 52 61 69 73 65 2d 3e 61 66 66 69 6e 69    pRaise->affini
3168f 74 79 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a  ty = OE_Abort;..
31690 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70        }..      p
31691 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
31692 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
31693 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71  , ..          sq
31694 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
31695 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70  end(pParse, 0, p
31696 52 61 69 73 65 29 2c 0d 0a 20 20 20 20 20 20 20  Raise),..       
31697 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
31698 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 26  tAppend(db, 0, &
31699 74 46 72 6f 6d 2c 20 30 29 2c 0d 0a 20 20 20 20  tFrom, 0),..    
3169a 20 20 20 20 20 20 70 57 68 65 72 65 2c 0d 0a 20        pWhere,.. 
3169b 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
3169c 2c 20 30 2c 20 30 2c 20 30 0d 0a 20 20 20 20 20  , 0, 0, 0..     
3169d 20 29 3b 0d 0a 20 20 20 20 20 20 70 57 68 65 72   );..      pWher
3169e 65 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  e = 0;..    }...
3169f 0a 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20  .    /* Disable 
316a0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
316a1 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0d 0a   allocation */..
316a2 20 20 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73      enableLookas
316a3 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ide = db->lookas
316a4 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0d 0a 20  ide.bEnabled;.. 
316a5 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
316a6 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0d 0a  .bEnabled = 0;..
316a7 0d 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d  ..    pTrigger =
316a8 20 28 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69   (Trigger *)sqli
316a9 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
316aa 64 62 2c 20 0d 0a 20 20 20 20 20 20 20 20 73 69  db, ..        si
316ab 7a 65 6f 66 28 54 72 69 67 67 65 72 29 20 2b 20  zeof(Trigger) + 
316ac 20 20 20 20 20 20 20 20 2f 2a 20 73 74 72 75 63          /* struc
316ad 74 20 54 72 69 67 67 65 72 20 2a 2f 0d 0a 20 20  t Trigger */..  
316ae 20 20 20 20 20 20 73 69 7a 65 6f 66 28 54 72 69        sizeof(Tri
316af 67 67 65 72 53 74 65 70 29 20 2b 20 20 20 20 20  ggerStep) +     
316b0 2f 2a 20 53 69 6e 67 6c 65 20 73 74 65 70 20 69  /* Single step i
316b1 6e 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  n trigger progra
316b2 6d 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 6e 46  m */..        nF
316b3 72 6f 6d 20 2b 20 31 20 20 20 20 20 20 20 20 20  rom + 1         
316b4 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
316b5 20 66 6f 72 20 70 53 74 65 70 2d 3e 74 61 72 67   for pStep->targ
316b6 65 74 2e 7a 20 2a 2f 0d 0a 20 20 20 20 29 3b 0d  et.z */..    );.
316b7 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
316b8 72 20 29 7b 0d 0a 20 20 20 20 20 20 70 53 74 65  r ){..      pSte
316b9 70 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 73 74  p = pTrigger->st
316ba 65 70 5f 6c 69 73 74 20 3d 20 28 54 72 69 67 67  ep_list = (Trigg
316bb 65 72 53 74 65 70 20 2a 29 26 70 54 72 69 67 67  erStep *)&pTrigg
316bc 65 72 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 70 53  er[1];..      pS
316bd 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20 3d 20  tep->target.z = 
316be 28 63 68 61 72 20 2a 29 26 70 53 74 65 70 5b 31  (char *)&pStep[1
316bf 5d 3b 0d 0a 20 20 20 20 20 20 70 53 74 65 70 2d  ];..      pStep-
316c0 3e 74 61 72 67 65 74 2e 6e 20 3d 20 6e 46 72 6f  >target.n = nFro
316c1 6d 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  m;..      memcpy
316c2 28 28 63 68 61 72 20 2a 29 70 53 74 65 70 2d 3e  ((char *)pStep->
316c3 74 61 72 67 65 74 2e 7a 2c 20 7a 46 72 6f 6d 2c  target.z, zFrom,
316c4 20 6e 46 72 6f 6d 29 3b 0d 0a 20 20 0d 0a 20 20   nFrom);..  ..  
316c5 20 20 20 20 70 53 74 65 70 2d 3e 70 57 68 65 72      pStep->pWher
316c6 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  e = sqlite3ExprD
316c7 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45  up(db, pWhere, E
316c8 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0d  XPRDUP_REDUCE);.
316c9 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70 45  .      pStep->pE
316ca 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  xprList = sqlite
316cb 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
316cc 20 70 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f   pList, EXPRDUP_
316cd 52 45 44 55 43 45 29 3b 0d 0a 20 20 20 20 20 20  REDUCE);..      
316ce 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  pStep->pSelect =
316cf 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
316d0 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45  p(db, pSelect, E
316d1 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0d  XPRDUP_REDUCE);.
316d2 0a 20 20 20 20 20 20 69 66 28 20 70 57 68 65 6e  .      if( pWhen
316d3 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 57 68   ){..        pWh
316d4 65 6e 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  en = sqlite3PExp
316d5 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
316d6 2c 20 70 57 68 65 6e 2c 20 30 2c 20 30 29 3b 0d  , pWhen, 0, 0);.
316d7 0a 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65  .        pTrigge
316d8 72 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74  r->pWhen = sqlit
316d9 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57  e3ExprDup(db, pW
316da 68 65 6e 2c 20 45 58 50 52 44 55 50 5f 52 45 44  hen, EXPRDUP_RED
316db 55 43 45 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  UCE);..      }..
316dc 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
316dd 52 65 2d 65 6e 61 62 6c 65 20 74 68 65 20 6c 6f  Re-enable the lo
316de 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 2c 20  okaside buffer, 
316df 69 66 20 69 74 20 77 61 73 20 64 69 73 61 62 6c  if it was disabl
316e0 65 64 20 65 61 72 6c 69 65 72 2e 20 2a 2f 0d 0a  ed earlier. */..
316e1 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
316e2 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61  e.bEnabled = ena
316e3 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0d 0a 0d  bleLookaside;...
316e4 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
316e5 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72  Delete(db, pWher
316e6 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
316e7 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
316e8 57 68 65 6e 29 3b 0d 0a 20 20 20 20 73 71 6c 69  When);..    sqli
316e9 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
316ea 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0d 0a 20  e(db, pList);.. 
316eb 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
316ec 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
316ed 63 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 64 62  ct);..    if( db
316ee 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
316ef 31 20 29 7b 0d 0a 20 20 20 20 20 20 66 6b 54 72  1 ){..      fkTr
316f0 69 67 67 65 72 44 65 6c 65 74 65 28 64 62 2c 20  iggerDelete(db, 
316f1 70 54 72 69 67 67 65 72 29 3b 0d 0a 20 20 20 20  pTrigger);..    
316f2 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20    return 0;..   
316f3 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
316f4 70 53 74 65 70 21 3d 30 20 29 3b 0d 0a 0d 0a 20  pStep!=0 );.... 
316f5 20 20 20 73 77 69 74 63 68 28 20 61 63 74 69 6f     switch( actio
316f6 6e 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 73 65  n ){..      case
316f7 20 4f 45 5f 52 65 73 74 72 69 63 74 3a 0d 0a 20   OE_Restrict:.. 
316f8 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70         pStep->op
316f9 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 20 0d 0a   = TK_SELECT; ..
316fa 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
316fb 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61        case OE_Ca
316fc 73 63 61 64 65 3a 20 0d 0a 20 20 20 20 20 20 20  scade: ..       
316fd 20 69 66 28 20 21 70 43 68 61 6e 67 65 73 20 29   if( !pChanges )
316fe 7b 20 0d 0a 20 20 20 20 20 20 20 20 20 20 70 53  { ..          pS
316ff 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c  tep->op = TK_DEL
31700 45 54 45 3b 20 0d 0a 20 20 20 20 20 20 20 20 20  ETE; ..         
31701 20 62 72 65 61 6b 3b 20 0d 0a 20 20 20 20 20 20   break; ..      
31702 20 20 7d 0d 0a 20 20 20 20 20 20 64 65 66 61 75    }..      defau
31703 6c 74 3a 0d 0a 20 20 20 20 20 20 20 20 70 53 74  lt:..        pSt
31704 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 55 50 44 41  ep->op = TK_UPDA
31705 54 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  TE;..    }..    
31706 70 53 74 65 70 2d 3e 70 54 72 69 67 20 3d 20 70  pStep->pTrig = p
31707 54 72 69 67 67 65 72 3b 0d 0a 20 20 20 20 70 54  Trigger;..    pT
31708 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20  rigger->pSchema 
31709 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
3170a 0d 0a 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e  ..    pTrigger->
3170b 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61  pTabSchema = pTa
3170c 62 2d 3e 70 53 63 68 65 6d 61 3b 0d 0a 20 20 20  b->pSchema;..   
3170d 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67 65   pFKey->apTrigge
3170e 72 5b 69 41 63 74 69 6f 6e 5d 20 3d 20 70 54 72  r[iAction] = pTr
3170f 69 67 67 65 72 3b 0d 0a 20 20 20 20 70 54 72 69  igger;..    pTri
31710 67 67 65 72 2d 3e 6f 70 20 3d 20 28 70 43 68 61  gger->op = (pCha
31711 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41 54 45  nges ? TK_UPDATE
31712 20 3a 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0d 0a   : TK_DELETE);..
31713 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20    }....  return 
31714 70 54 72 69 67 67 65 72 3b 0d 0a 7d 0d 0a 0d 0a  pTrigger;..}....
31715 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
31716 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
31717 68 65 6e 20 64 65 6c 65 74 69 6e 67 20 6f 72 20  hen deleting or 
31718 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20 74  updating a row t
31719 6f 20 69 6d 70 6c 65 6d 65 6e 74 0d 0a 2a 2a 20  o implement..** 
3171a 61 6e 79 20 72 65 71 75 69 72 65 64 20 43 41 53  any required CAS
3171b 43 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f  CADE, SET NULL o
3171c 72 20 53 45 54 20 44 45 46 41 55 4c 54 20 61 63  r SET DEFAULT ac
3171d 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  tions...*/..SQLI
3171e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3171f 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73  sqlite3FkActions
31720 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
31721 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
31722 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
31723 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c  ntext */..  Tabl
31724 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
31725 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
31726 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74  able being updat
31727 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72  ed or deleted fr
31728 6f 6d 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73  om */..  ExprLis
31729 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20 20  t *pChanges,    
3172a 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e           /* Chan
3172b 67 65 2d 6c 69 73 74 20 66 6f 72 20 55 50 44 41  ge-list for UPDA
3172c 54 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c  TE, NULL for DEL
3172d 45 54 45 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65  ETE */..  int re
3172e 67 4f 6c 64 20 20 20 20 20 20 20 20 20 20 20 20  gOld            
3172f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
31730 72 65 73 73 20 6f 66 20 61 72 72 61 79 20 63 6f  ress of array co
31731 6e 74 61 69 6e 69 6e 67 20 6f 6c 64 20 72 6f 77  ntaining old row
31732 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 2f 2a 20 49 66   */..){..  /* If
31733 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75 70   foreign-key sup
31734 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2c  port is enabled,
31735 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
31736 20 61 6c 6c 20 46 4b 73 20 74 68 61 74 20 0d 0a   all FKs that ..
31737 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 61    ** refer to ta
31738 62 6c 65 20 70 54 61 62 2e 20 49 66 20 74 68 65  ble pTab. If the
31739 72 65 20 69 73 20 61 6e 20 61 63 74 69 6f 6e 20  re is an action 
3173a 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3173b 74 68 65 20 46 4b 20 0d 0a 20 20 2a 2a 20 66 6f  the FK ..  ** fo
3173c 72 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  r this operation
3173d 20 28 65 69 74 68 65 72 20 75 70 64 61 74 65 20   (either update 
3173e 6f 72 20 64 65 6c 65 74 65 29 2c 20 69 6e 76 6f  or delete), invo
3173f 6b 65 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  ke the associate
31740 64 20 0d 0a 20 20 2a 2a 20 74 72 69 67 67 65 72  d ..  ** trigger
31741 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a   sub-program.  *
31742 2f 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  /..  if( pParse-
31743 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
31744 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
31745 0d 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65  ..    FKey *pFKe
31746 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
31747 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
31748 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  variable */..   
31749 20 66 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c   for(pFKey = sql
3174a 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
3174b 28 70 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70  (pTab); pFKey; p
3174c 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78  FKey=pFKey->pNex
3174d 74 54 6f 29 7b 0d 0a 20 20 20 20 20 20 54 72 69  tTo){..      Tri
3174e 67 67 65 72 20 2a 70 41 63 74 69 6f 6e 20 3d 20  gger *pAction = 
3174f 66 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28  fkActionTrigger(
31750 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46  pParse, pTab, pF
31751 4b 65 79 2c 20 70 43 68 61 6e 67 65 73 29 3b 0d  Key, pChanges);.
31752 0a 20 20 20 20 20 20 69 66 28 20 70 41 63 74 69  .      if( pActi
31753 6f 6e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  on ){..        s
31754 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
31755 67 67 65 72 44 69 72 65 63 74 28 70 50 61 72 73  ggerDirect(pPars
31756 65 2c 20 70 41 63 74 69 6f 6e 2c 20 70 54 61 62  e, pAction, pTab
31757 2c 20 72 65 67 4f 6c 64 2c 20 4f 45 5f 41 62 6f  , regOld, OE_Abo
31758 72 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d  rt, 0);..      }
31759 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d  ..    }..  }..}.
3175a 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e  ...#endif /* ifn
3175b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3175c 54 52 49 47 47 45 52 20 2a 2f 0d 0a 0d 0a 2f 2a  TRIGGER */..../*
3175d 0d 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 6d 65  ..** Free all me
3175e 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
3175f 77 69 74 68 20 66 6f 72 65 69 67 6e 20 6b 65 79  with foreign key
31760 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 61 74 74   definitions att
31761 61 63 68 65 64 20 74 6f 0d 0a 2a 2a 20 74 61 62  ached to..** tab
31762 6c 65 20 70 54 61 62 2e 20 52 65 6d 6f 76 65 20  le pTab. Remove 
31763 74 68 65 20 64 65 6c 65 74 65 64 20 66 6f 72 65  the deleted fore
31764 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68  ign keys from th
31765 65 20 53 63 68 65 6d 61 2e 66 6b 65 79 48 61 73  e Schema.fkeyHas
31766 68 0d 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  h..** hash table
31767 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
31768 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
31769 65 33 46 6b 44 65 6c 65 74 65 28 73 71 6c 69 74  e3FkDelete(sqlit
3176a 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70  e3 *db, Table *p
3176b 54 61 62 29 7b 0d 0a 20 20 46 4b 65 79 20 2a 70  Tab){..  FKey *p
3176c 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  FKey;           
3176d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
3176e 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
3176f 0d 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 3b  ..  FKey *pNext;
31770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31771 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70      /* Copy of p
31772 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20  FKey->pNextFrom 
31773 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
31774 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
31775 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28  SchemaMutexHeld(
31776 64 62 2c 20 30 2c 20 70 54 61 62 2d 3e 70 53 63  db, 0, pTab->pSc
31777 68 65 6d 61 29 20 29 3b 0d 0a 20 20 66 6f 72 28  hema) );..  for(
31778 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65  pFKey=pTab->pFKe
31779 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d  y; pFKey; pFKey=
3177a 70 4e 65 78 74 29 7b 0d 0a 0d 0a 20 20 20 20 2f  pNext){....    /
3177b 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 46 4b 20  * Remove the FK 
3177c 66 72 6f 6d 20 74 68 65 20 66 6b 65 79 48 61 73  from the fkeyHas
3177d 68 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f  h hash table. */
3177e 0d 0a 20 20 20 20 69 66 28 20 21 64 62 20 7c 7c  ..    if( !db ||
3177f 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65   db->pnBytesFree
31780 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 69  d==0 ){..      i
31781 66 28 20 70 46 4b 65 79 2d 3e 70 50 72 65 76 54  f( pFKey->pPrevT
31782 6f 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 46  o ){..        pF
31783 4b 65 79 2d 3e 70 50 72 65 76 54 6f 2d 3e 70 4e  Key->pPrevTo->pN
31784 65 78 74 54 6f 20 3d 20 70 46 4b 65 79 2d 3e 70  extTo = pFKey->p
31785 4e 65 78 74 54 6f 3b 0d 0a 20 20 20 20 20 20 7d  NextTo;..      }
31786 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 76  else{..        v
31787 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a  oid *p = (void *
31788 29 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 3b  )pFKey->pNextTo;
31789 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  ..        const 
3178a 63 68 61 72 20 2a 7a 20 3d 20 28 70 20 3f 20 70  char *z = (p ? p
3178b 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 7a  FKey->pNextTo->z
3178c 54 6f 20 3a 20 70 46 4b 65 79 2d 3e 7a 54 6f 29  To : pFKey->zTo)
3178d 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
3178e 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 54  e3HashInsert(&pT
3178f 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b 65  ab->pSchema->fke
31790 79 48 61 73 68 2c 20 7a 2c 20 73 71 6c 69 74 65  yHash, z, sqlite
31791 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c 20 70 29  3Strlen30(z), p)
31792 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
31793 20 20 69 66 28 20 70 46 4b 65 79 2d 3e 70 4e 65    if( pFKey->pNe
31794 78 74 54 6f 20 29 7b 0d 0a 20 20 20 20 20 20 20  xtTo ){..       
31795 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d   pFKey->pNextTo-
31796 3e 70 50 72 65 76 54 6f 20 3d 20 70 46 4b 65 79  >pPrevTo = pFKey
31797 2d 3e 70 50 72 65 76 54 6f 3b 0d 0a 20 20 20 20  ->pPrevTo;..    
31798 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20    }..    }....  
31799 20 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33    /* EV: R-30323
3179a 2d 32 31 39 31 37 20 45 61 63 68 20 66 6f 72 65  -21917 Each fore
3179b 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
3179c 6e 74 20 69 6e 20 53 51 4c 69 74 65 20 69 73 0d  nt in SQLite is.
3179d 0a 20 20 20 20 2a 2a 20 63 6c 61 73 73 69 66 69  .    ** classifi
3179e 65 64 20 61 73 20 65 69 74 68 65 72 20 69 6d 6d  ed as either imm
3179f 65 64 69 61 74 65 20 6f 72 20 64 65 66 65 72 72  ediate or deferr
317a0 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ed...    */..   
317a1 20 61 73 73 65 72 74 28 20 70 46 4b 65 79 2d 3e   assert( pFKey->
317a2 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c  isDeferred==0 ||
317a3 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72   pFKey->isDeferr
317a4 65 64 3d 3d 31 20 29 3b 0d 0a 0d 0a 20 20 20 20  ed==1 );....    
317a5 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 74 72  /* Delete any tr
317a6 69 67 67 65 72 73 20 63 72 65 61 74 65 64 20 74  iggers created t
317a7 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 63 74 69  o implement acti
317a8 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 46 4b 2e  ons for this FK.
317a9 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
317aa 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
317ab 0d 0a 20 20 20 20 66 6b 54 72 69 67 67 65 72 44  ..    fkTriggerD
317ac 65 6c 65 74 65 28 64 62 2c 20 70 46 4b 65 79 2d  elete(db, pFKey-
317ad 3e 61 70 54 72 69 67 67 65 72 5b 30 5d 29 3b 0d  >apTrigger[0]);.
317ae 0a 20 20 20 20 66 6b 54 72 69 67 67 65 72 44 65  .    fkTriggerDe
317af 6c 65 74 65 28 64 62 2c 20 70 46 4b 65 79 2d 3e  lete(db, pFKey->
317b0 61 70 54 72 69 67 67 65 72 5b 31 5d 29 3b 0d 0a  apTrigger[1]);..
317b1 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 70 4e  #endif....    pN
317b2 65 78 74 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65  ext = pFKey->pNe
317b3 78 74 46 72 6f 6d 3b 0d 0a 20 20 20 20 73 71 6c  xtFrom;..    sql
317b4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
317b5 46 4b 65 79 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  FKey);..  }..}..
317b6 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66  #endif /* ifndef
317b7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
317b8 45 49 47 4e 5f 4b 45 59 20 2a 2f 0d 0a 0d 0a 2f  EIGN_KEY */..../
317b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
317ba 6e 64 20 6f 66 20 66 6b 65 79 2e 63 20 2a 2a 2a  nd of fkey.c ***
317bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
317be 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
317bf 42 65 67 69 6e 20 66 69 6c 65 20 69 6e 73 65 72  Begin file inser
317c0 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
317c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
317c3 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70  ./*..** 2001 Sep
317c4 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a  tember 15..**..*
317c5 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
317c6 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
317c7 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
317c8 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
317c9 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
317ca 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
317cb 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
317cc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
317cd 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
317ce 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
317cf 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
317d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
317d1 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
317d2 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
317d3 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
317d4 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
317d5 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
317d6 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
317d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317db 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
317dc 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
317dd 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72  routines that ar
317de 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  e called by the 
317df 70 61 72 73 65 72 0d 0a 2a 2a 20 74 6f 20 68 61  parser..** to ha
317e0 6e 64 6c 65 20 49 4e 53 45 52 54 20 73 74 61 74  ndle INSERT stat
317e1 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65  ements in SQLite
317e2 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ...*/..../*..** 
317e3 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
317e4 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 74  at will open a t
317e5 61 62 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67  able for reading
317e6 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
317e7 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
317e8 65 33 4f 70 65 6e 54 61 62 6c 65 28 0d 0a 20 20  e3OpenTable(..  
317e9 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20 20  Parse *p,       
317ea 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
317eb 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
317ec 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 75 72 2c 20  */..  int iCur, 
317ed 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72        /* The cur
317ee 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
317ef 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e  e table */..  in
317f0 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a  t iDb,        /*
317f1 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   The database in
317f2 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61  dex in sqlite3.a
317f3 44 62 5b 5d 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  Db[] */..  Table
317f4 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68   *pTab,    /* Th
317f5 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70  e table to be op
317f6 65 6e 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f  ened */..  int o
317f7 70 63 6f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50  pcode      /* OP
317f8 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f  _OpenRead or OP_
317f9 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0d 0a 29 7b  OpenWrite */..){
317fa 0d 0a 20 20 56 64 62 65 20 2a 76 3b 0d 0a 20 20  ..  Vdbe *v;..  
317fb 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
317fc 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  ab) ) return;.. 
317fd 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
317fe 64 62 65 28 70 29 3b 0d 0a 20 20 61 73 73 65 72  dbe(p);..  asser
317ff 74 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  t( opcode==OP_Op
31800 65 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64  enWrite || opcod
31801 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29  e==OP_OpenRead )
31802 3b 0d 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c  ;..  sqlite3Tabl
31803 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70 54  eLock(p, iDb, pT
31804 61 62 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64  ab->tnum, (opcod
31805 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  e==OP_OpenWrite)
31806 3f 31 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  ?1:0, pTab->zNam
31807 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  e);..  sqlite3Vd
31808 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 63 6f  beAddOp3(v, opco
31809 64 65 2c 20 69 43 75 72 2c 20 70 54 61 62 2d 3e  de, iCur, pTab->
3180a 74 6e 75 6d 2c 20 69 44 62 29 3b 0d 0a 20 20 73  tnum, iDb);..  s
3180b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
3180c 50 34 28 76 2c 20 2d 31 2c 20 53 51 4c 49 54 45  P4(v, -1, SQLITE
3180d 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62  _INT_TO_PTR(pTab
3180e 2d 3e 6e 43 6f 6c 29 2c 20 50 34 5f 49 4e 54 33  ->nCol), P4_INT3
3180f 32 29 3b 0d 0a 20 20 56 64 62 65 43 6f 6d 6d 65  2);..  VdbeComme
31810 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 54 61  nt((v, "%s", pTa
31811 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 7d 0d 0a  b->zName));..}..
31812 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
31813 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
31814 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
31815 20 73 74 72 69 6e 67 20 61 73 73 6f 63 69 61 74   string associat
31816 65 64 20 77 69 74 68 20 69 6e 64 65 78 0d 0a 2a  ed with index..*
31817 2a 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e  * pIdx. A column
31818 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
31819 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74   has one charact
3181a 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  er for each colu
3181b 6d 6e 20 69 6e 20 0d 0a 2a 2a 20 74 68 65 20 74  mn in ..** the t
3181c 61 62 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 20  able, according 
3181d 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  to the affinity 
3181e 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0d 0a  of the column:..
3181f 2a 2a 0d 0a 2a 2a 20 20 43 68 61 72 61 63 74 65  **..**  Characte
31820 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66  r      Column af
31821 66 69 6e 69 74 79 0d 0a 2a 2a 20 20 2d 2d 2d 2d  finity..**  ----
31822 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31823 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20 20  ----------..**  
31824 27 61 27 20 20 20 20 20 20 20 20 20 20 20 20 54  'a'            T
31825 45 58 54 0d 0a 2a 2a 20 20 27 62 27 20 20 20 20  EXT..**  'b'    
31826 20 20 20 20 20 20 20 20 4e 4f 4e 45 0d 0a 2a 2a          NONE..**
31827 20 20 27 63 27 20 20 20 20 20 20 20 20 20 20 20    'c'           
31828 20 4e 55 4d 45 52 49 43 0d 0a 2a 2a 20 20 27 64   NUMERIC..**  'd
31829 27 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 54  '            INT
3182a 45 47 45 52 0d 0a 2a 2a 20 20 27 65 27 20 20 20  EGER..**  'e'   
3182b 20 20 20 20 20 20 20 20 20 52 45 41 4c 0d 0a 2a           REAL..*
3182c 2a 0d 0a 2a 2a 20 41 6e 20 65 78 74 72 61 20 27  *..** An extra '
3182d 64 27 20 69 73 20 61 70 70 65 6e 64 65 64 20 74  d' is appended t
3182e 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
3182f 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 76 65 72   string to cover
31830 20 74 68 65 0d 0a 2a 2a 20 72 6f 77 69 64 20 74   the..** rowid t
31831 68 61 74 20 61 70 70 65 61 72 73 20 61 73 20 74  hat appears as t
31832 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69  he last column i
31833 6e 20 65 76 65 72 79 20 69 6e 64 65 78 2e 0d 0a  n every index...
31834 2a 2a 0d 0a 2a 2a 20 4d 65 6d 6f 72 79 20 66 6f  **..** Memory fo
31835 72 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e  r the buffer con
31836 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6f 6c 75  taining the colu
31837 6d 6e 20 69 6e 64 65 78 20 61 66 66 69 6e 69 74  mn index affinit
31838 79 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 69 73 20  y string..** is 
31839 6d 61 6e 61 67 65 64 20 61 6c 6f 6e 67 20 77 69  managed along wi
3183a 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
3183b 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
3183c 72 65 2e 20 49 74 20 77 69 6c 6c 20 62 65 0d 0a  re. It will be..
3183d 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e  ** released when
3183e 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e   sqlite3DeleteIn
3183f 64 65 78 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  dex() is called.
31840 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
31841 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
31842 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66  *sqlite3IndexAff
31843 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 76  inityStr(Vdbe *v
31844 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0d  , Index *pIdx){.
31845 0a 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43  .  if( !pIdx->zC
31846 6f 6c 41 66 66 20 29 7b 0d 0a 20 20 20 20 2f 2a  olAff ){..    /*
31847 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
31848 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
31849 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70  y string for a p
3184a 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20  articular index 
3184b 69 73 0d 0a 20 20 20 20 2a 2a 20 72 65 71 75 69  is..    ** requi
3184c 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63  red, it is alloc
3184d 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74  ated and populat
3184e 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74  ed here. It is t
3184f 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0d 0a 20  hen stored as.. 
31850 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20 6f     ** a member o
31851 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  f the Index stru
31852 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65 71  cture for subseq
31853 75 65 6e 74 20 75 73 65 2e 0d 0a 20 20 20 20 2a  uent use...    *
31854 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f  *..    ** The co
31855 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
31856 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
31857 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
31858 62 79 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  by..    ** sqlit
31859 65 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77  eDeleteIndex() w
3185a 68 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74  hen the Index st
3185b 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
3185c 73 20 63 6c 65 61 6e 65 64 0d 0a 20 20 20 20 2a  s cleaned..    *
3185d 2a 20 75 70 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  * up...    */.. 
3185e 20 20 20 69 6e 74 20 6e 3b 0d 0a 20 20 20 20 54     int n;..    T
3185f 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64  able *pTab = pId
31860 78 2d 3e 70 54 61 62 6c 65 3b 0d 0a 20 20 20 20  x->pTable;..    
31861 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
31862 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0d  lite3VdbeDb(v);.
31863 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41  .    pIdx->zColA
31864 66 66 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ff = (char *)sql
31865 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
31866 30 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  0, pIdx->nColumn
31867 2b 32 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70  +2);..    if( !p
31868 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0d  Idx->zColAff ){.
31869 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
3186a 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20  cFailed = 1;..  
3186b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
3186c 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72 28 6e 3d     }..    for(n=
3186d 30 3b 20 6e 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; n<pIdx->nColu
3186e 6d 6e 3b 20 6e 2b 2b 29 7b 0d 0a 20 20 20 20 20  mn; n++){..     
3186f 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e   pIdx->zColAff[n
31870 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70  ] = pTab->aCol[p
31871 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d  Idx->aiColumn[n]
31872 5d 2e 61 66 66 69 6e 69 74 79 3b 0d 0a 20 20 20  ].affinity;..   
31873 20 7d 0d 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43   }..    pIdx->zC
31874 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d 20 53 51 4c  olAff[n++] = SQL
31875 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b  ITE_AFF_INTEGER;
31876 0d 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c  ..    pIdx->zCol
31877 41 66 66 5b 6e 5d 20 3d 20 30 3b 0d 0a 20 20 7d  Aff[n] = 0;..  }
31878 0d 0a 20 0d 0a 20 20 72 65 74 75 72 6e 20 70 49  .. ..  return pI
31879 64 78 2d 3e 7a 43 6f 6c 41 66 66 3b 0d 0a 7d 0d  dx->zColAff;..}.
3187a 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 50 34  .../*..** Set P4
3187b 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
3187c 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f  ently inserted o
3187d 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d  pcode to a colum
3187e 6e 20 61 66 66 69 6e 69 74 79 0d 0a 2a 2a 20 73  n affinity..** s
3187f 74 72 69 6e 67 20 66 6f 72 20 74 61 62 6c 65 20  tring for table 
31880 70 54 61 62 2e 20 41 20 63 6f 6c 75 6d 6e 20 61  pTab. A column a
31881 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68  ffinity string h
31882 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  as one character
31883 0d 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f  ..** for each co
31884 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20 62 79 20  lumn indexed by 
31885 74 68 65 20 69 6e 64 65 78 2c 20 61 63 63 6f 72  the index, accor
31886 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69  ding to the affi
31887 6e 69 74 79 20 6f 66 20 74 68 65 0d 0a 2a 2a 20  nity of the..** 
31888 63 6f 6c 75 6d 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  column:..**..** 
31889 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20   Character      
3188a 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0d  Column affinity.
3188b 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
3188c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3188d 2d 2d 2d 0d 0a 2a 2a 20 20 27 61 27 20 20 20 20  ---..**  'a'    
3188e 20 20 20 20 20 20 20 20 54 45 58 54 0d 0a 2a 2a          TEXT..**
3188f 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20    'b'           
31890 20 4e 4f 4e 45 0d 0a 2a 2a 20 20 27 63 27 20 20   NONE..**  'c'  
31891 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49            NUMERI
31892 43 0d 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20  C..**  'd'      
31893 20 20 20 20 20 20 49 4e 54 45 47 45 52 0d 0a 2a        INTEGER..*
31894 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20  *  'e'          
31895 20 20 52 45 41 4c 0d 0a 2a 2f 0d 0a 53 51 4c 49    REAL..*/..SQLI
31896 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
31897 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
31898 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c  nityStr(Vdbe *v,
31899 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0d 0a   Table *pTab){..
3189a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
3189b 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66  ime a column aff
3189c 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
3189d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61   a particular ta
3189e 62 6c 65 0d 0a 20 20 2a 2a 20 69 73 20 72 65 71  ble..  ** is req
3189f 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c  uired, it is all
318a0 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c  ocated and popul
318a1 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73  ated here. It is
318a2 20 74 68 65 6e 20 0d 0a 20 20 2a 2a 20 73 74 6f   then ..  ** sto
318a3 72 65 64 20 61 73 20 61 20 6d 65 6d 62 65 72 20  red as a member 
318a4 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  of the Table str
318a5 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65  ucture for subse
318a6 71 75 65 6e 74 20 75 73 65 2e 0d 0a 20 20 2a 2a  quent use...  **
318a7 0d 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d  ..  ** The colum
318a8 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
318a9 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  g will eventuall
318aa 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 79 0d  y be deleted by.
318ab 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44 65 6c  .  ** sqlite3Del
318ac 65 74 65 54 61 62 6c 65 28 29 20 77 68 65 6e 20  eteTable() when 
318ad 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
318ae 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63 6c  ure itself is cl
318af 65 61 6e 65 64 20 75 70 2e 0d 0a 20 20 2a 2f 0d  eaned up...  */.
318b0 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43  .  if( !pTab->zC
318b1 6f 6c 41 66 66 20 29 7b 0d 0a 20 20 20 20 63 68  olAff ){..    ch
318b2 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0d 0a 20 20  ar *zColAff;..  
318b3 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 73 71    int i;..    sq
318b4 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
318b5 74 65 33 56 64 62 65 44 62 28 76 29 3b 0d 0a 0d  te3VdbeDb(v);...
318b6 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28  .    zColAff = (
318b7 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62  char *)sqlite3Db
318b8 4d 61 6c 6c 6f 63 52 61 77 28 30 2c 20 70 54 61  MallocRaw(0, pTa
318b9 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0d 0a 20 20 20  b->nCol+1);..   
318ba 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 7b   if( !zColAff ){
318bb 0d 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  ..      db->mall
318bc 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20  ocFailed = 1;.. 
318bd 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20       return;..  
318be 20 20 7d 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 69    }....    for(i
318bf 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
318c0 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 7a  ; i++){..      z
318c1 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61 62  ColAff[i] = pTab
318c2 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69  ->aCol[i].affini
318c3 74 79 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ty;..    }..    
318c4 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43  zColAff[pTab->nC
318c5 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0d 0a 0d 0a 20  ol] = '\0';.... 
318c6 20 20 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66     pTab->zColAff
318c7 20 3d 20 7a 43 6f 6c 41 66 66 3b 0d 0a 20 20 7d   = zColAff;..  }
318c8 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  ....  sqlite3Vdb
318c9 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
318ca 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 2c 20   pTab->zColAff, 
318cb 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a  P4_TRANSIENT);..
318cc 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
318cd 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74  rn non-zero if t
318ce 68 65 20 74 61 62 6c 65 20 70 54 61 62 20 69 6e  he table pTab in
318cf 20 64 61 74 61 62 61 73 65 20 69 44 62 20 6f 72   database iDb or
318d0 20 61 6e 79 20 6f 66 20 69 74 73 20 69 6e 64 69   any of its indi
318d1 63 65 73 0d 0a 2a 2a 20 68 61 76 65 20 62 65 65  ces..** have bee
318d2 6e 20 6f 70 65 6e 65 64 20 61 74 20 61 6e 79 20  n opened at any 
318d3 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 56 44 42  point in the VDB
318d4 45 20 70 72 6f 67 72 61 6d 20 62 65 67 69 6e 6e  E program beginn
318d5 69 6e 67 20 61 74 20 6c 6f 63 61 74 69 6f 6e 0d  ing at location.
318d6 0a 2a 2a 20 69 53 74 61 72 74 41 64 64 72 20 74  .** iStartAddr t
318d7 68 72 6f 75 67 68 74 20 74 68 65 20 65 6e 64 20  hrought the end 
318d8 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
318d9 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
318da 20 73 65 65 20 69 66 20 0d 0a 2a 2a 20 61 20 73   see if ..** a s
318db 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
318dc 66 6f 72 6d 20 20 22 49 4e 53 45 52 54 20 49 4e  form  "INSERT IN
318dd 54 4f 20 3c 69 44 62 2c 20 70 54 61 62 3e 20 53  TO <iDb, pTab> S
318de 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61 6e 20 0d  ELECT ..." can .
318df 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75 74 20  .** run without 
318e0 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20  using temporary 
318e1 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20 72 65  table for the re
318e2 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c  sults of the SEL
318e3 45 43 54 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ECT. ..*/..stati
318e4 63 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65  c int readsTable
318e5 28 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69  (Parse *p, int i
318e6 53 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69  StartAddr, int i
318e7 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  Db, Table *pTab)
318e8 7b 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  {..  Vdbe *v = s
318e9 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29  qlite3GetVdbe(p)
318ea 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69  ;..  int i;..  i
318eb 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65  nt iEnd = sqlite
318ec 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
318ed 28 76 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  (v);..#ifndef SQ
318ee 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
318ef 4c 54 41 42 4c 45 0d 0a 20 20 56 54 61 62 6c 65  LTABLE..  VTable
318f0 20 2a 70 56 54 61 62 20 3d 20 49 73 56 69 72 74   *pVTab = IsVirt
318f1 75 61 6c 28 70 54 61 62 29 20 3f 20 73 71 6c 69  ual(pTab) ? sqli
318f2 74 65 33 47 65 74 56 54 61 62 6c 65 28 70 2d 3e  te3GetVTable(p->
318f3 64 62 2c 20 70 54 61 62 29 20 3a 20 30 3b 0d 0a  db, pTab) : 0;..
318f4 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 66 6f 72 28  #endif....  for(
318f5 69 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c  i=iStartAddr; i<
318f6 69 45 6e 64 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  iEnd; i++){..   
318f7 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73   VdbeOp *pOp = s
318f8 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
318f9 76 2c 20 69 29 3b 0d 0a 20 20 20 20 61 73 73 65  v, i);..    asse
318fa 72 74 28 20 70 4f 70 21 3d 30 20 29 3b 0d 0a 20  rt( pOp!=0 );.. 
318fb 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
318fc 64 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20  de==OP_OpenRead 
318fd 26 26 20 70 4f 70 2d 3e 70 33 3d 3d 69 44 62 20  && pOp->p3==iDb 
318fe 29 7b 0d 0a 20 20 20 20 20 20 49 6e 64 65 78 20  ){..      Index 
318ff 2a 70 49 6e 64 65 78 3b 0d 0a 20 20 20 20 20 20  *pIndex;..      
31900 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f 70 2d 3e  int tnum = pOp->
31901 70 32 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 74  p2;..      if( t
31902 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75 6d 20  num==pTab->tnum 
31903 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
31904 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rn 1;..      }..
31905 20 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78        for(pIndex
31906 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
31907 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49  Index; pIndex=pI
31908 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20  ndex->pNext){.. 
31909 20 20 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d         if( tnum=
3190a 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b  =pIndex->tnum ){
3190b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ..          retu
3190c 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d  rn 1;..        }
3190d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
3190e 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3190f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
31910 4c 45 0d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  LE..    if( pOp-
31911 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65  >opcode==OP_VOpe
31912 6e 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74  n && pOp->p4.pVt
31913 61 62 3d 3d 70 56 54 61 62 20 29 7b 0d 0a 20 20  ab==pVTab ){..  
31914 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
31915 3e 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0d  >p4.pVtab!=0 );.
31916 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
31917 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56  Op->p4type==P4_V
31918 54 41 42 20 29 3b 0d 0a 20 20 20 20 20 20 72 65  TAB );..      re
31919 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 1;..    }..
3191a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 20 20 72  #endif..  }..  r
3191b 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 23  eturn 0;..}....#
3191c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3191d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
3191e 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 63 61 74 65 20  ../*..** Locate 
3191f 6f 72 20 63 72 65 61 74 65 20 61 6e 20 41 75 74  or create an Aut
31920 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75  oincInfo structu
31921 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  re associated wi
31922 74 68 20 74 61 62 6c 65 20 70 54 61 62 0d 0a 2a  th table pTab..*
31923 2a 20 77 68 69 63 68 20 69 73 20 69 6e 20 64 61  * which is in da
31924 74 61 62 61 73 65 20 69 44 62 2e 20 20 52 65 74  tabase iDb.  Ret
31925 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72  urn the register
31926 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
31927 72 65 67 69 73 74 65 72 0d 0a 2a 2a 20 74 68 61  register..** tha
31928 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61 78 69  t holds the maxi
31929 6d 75 6d 20 72 6f 77 69 64 2e 0d 0a 2a 2a 0d 0a  mum rowid...**..
3192a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 74 20 6d  ** There is at m
3192b 6f 73 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49  ost one AutoincI
3192c 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 65  nfo structure pe
3192d 72 20 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20  r table even if 
3192e 74 68 65 0d 0a 2a 2a 20 73 61 6d 65 20 74 61 62  the..** same tab
3192f 6c 65 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d  le is autoincrem
31930 65 6e 74 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  ented multiple t
31931 69 6d 65 73 20 64 75 65 20 74 6f 20 69 6e 73 65  imes due to inse
31932 72 74 73 20 77 69 74 68 69 6e 0d 0a 2a 2a 20 74  rts within..** t
31933 72 69 67 67 65 72 73 2e 20 20 41 20 6e 65 77 20  riggers.  A new 
31934 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75  AutoincInfo stru
31935 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64  cture is created
31936 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 0d   if this is the.
31937 0a 2a 2a 20 66 69 72 73 74 20 75 73 65 20 6f 66  .** first use of
31938 20 74 61 62 6c 65 20 70 54 61 62 2e 20 20 4f 6e   table pTab.  On
31939 20 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75   2nd and subsequ
3193a 65 6e 74 20 75 73 65 73 2c 20 74 68 65 20 6f 72  ent uses, the or
3193b 69 67 69 6e 61 6c 0d 0a 2a 2a 20 41 75 74 6f 69  iginal..** Autoi
3193c 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ncInfo structure
3193d 20 69 73 20 75 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a   is used...**..*
3193e 2a 20 54 68 72 65 65 20 6d 65 6d 6f 72 79 20 6c  * Three memory l
3193f 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c  ocations are all
31940 6f 63 61 74 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ocated:..**..** 
31941 20 20 28 31 29 20 20 52 65 67 69 73 74 65 72 20    (1)  Register 
31942 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d 65  to hold the name
31943 20 6f 66 20 74 68 65 20 70 54 61 62 20 74 61 62   of the pTab tab
31944 6c 65 2e 0d 0a 2a 2a 20 20 20 28 32 29 20 20 52  le...**   (2)  R
31945 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20  egister to hold 
31946 74 68 65 20 6d 61 78 69 6d 75 6d 20 52 4f 57 49  the maximum ROWI
31947 44 20 6f 66 20 70 54 61 62 2e 0d 0a 2a 2a 20 20  D of pTab...**  
31948 20 28 33 29 20 20 52 65 67 69 73 74 65 72 20 74   (3)  Register t
31949 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 77 69 64  o hold the rowid
3194a 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65   in sqlite_seque
3194b 6e 63 65 20 6f 66 20 70 54 61 62 0d 0a 2a 2a 0d  nce of pTab..**.
3194c 0a 2a 2a 20 54 68 65 20 32 6e 64 20 72 65 67 69  .** The 2nd regi
3194d 73 74 65 72 20 69 73 20 74 68 65 20 6f 6e 65 20  ster is the one 
3194e 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64  that is returned
3194f 2e 20 20 54 68 61 74 20 69 73 20 61 6c 6c 20 74  .  That is all t
31950 68 65 0d 0a 2a 2a 20 69 6e 73 65 72 74 20 72 6f  he..** insert ro
31951 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 6b  utine needs to k
31952 6e 6f 77 20 61 62 6f 75 74 2e 0d 0a 2a 2f 0d 0a  now about...*/..
31953 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49  static int autoI
31954 6e 63 42 65 67 69 6e 28 0d 0a 20 20 50 61 72 73  ncBegin(..  Pars
31955 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
31956 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
31957 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62  xt */..  int iDb
31958 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
31959 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
3195a 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54  abase holding pT
3195b 61 62 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a  ab */..  Table *
3195c 70 54 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20  pTab         /* 
3195d 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
3195e 20 77 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0d 0a   writing to */..
3195f 29 7b 0d 0a 20 20 69 6e 74 20 6d 65 6d 49 64 20  ){..  int memId 
31960 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 52 65 67  = 0;      /* Reg
31961 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6d 61  ister holding ma
31962 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0d 0a  ximum rowid */..
31963 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46    if( pTab->tabF
31964 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
31965 63 72 65 6d 65 6e 74 20 29 7b 0d 0a 20 20 20 20  crement ){..    
31966 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c  Parse *pToplevel
31967 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54   = sqlite3ParseT
31968 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b  oplevel(pParse);
31969 0d 0a 20 20 20 20 41 75 74 6f 69 6e 63 49 6e 66  ..    AutoincInf
3196a 6f 20 2a 70 49 6e 66 6f 3b 0d 0a 0d 0a 20 20 20  o *pInfo;....   
3196b 20 70 49 6e 66 6f 20 3d 20 70 54 6f 70 6c 65 76   pInfo = pToplev
3196c 65 6c 2d 3e 70 41 69 6e 63 3b 0d 0a 20 20 20 20  el->pAinc;..    
3196d 77 68 69 6c 65 28 20 70 49 6e 66 6f 20 26 26 20  while( pInfo && 
3196e 70 49 6e 66 6f 2d 3e 70 54 61 62 21 3d 70 54 61  pInfo->pTab!=pTa
3196f 62 20 29 7b 20 70 49 6e 66 6f 20 3d 20 70 49 6e  b ){ pInfo = pIn
31970 66 6f 2d 3e 70 4e 65 78 74 3b 20 7d 0d 0a 20 20  fo->pNext; }..  
31971 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29    if( pInfo==0 )
31972 7b 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 20 3d  {..      pInfo =
31973 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
31974 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Raw(pParse->db, 
31975 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 29 3b  sizeof(*pInfo));
31976 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66  ..      if( pInf
31977 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  o==0 ) return 0;
31978 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 70  ..      pInfo->p
31979 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76 65 6c  Next = pToplevel
3197a 2d 3e 70 41 69 6e 63 3b 0d 0a 20 20 20 20 20 20  ->pAinc;..      
3197b 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63  pToplevel->pAinc
3197c 20 3d 20 70 49 6e 66 6f 3b 0d 0a 20 20 20 20 20   = pInfo;..     
3197d 20 70 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70   pInfo->pTab = p
3197e 54 61 62 3b 0d 0a 20 20 20 20 20 20 70 49 6e 66  Tab;..      pInf
3197f 6f 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0d 0a 20  o->iDb = iDb;.. 
31980 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
31981 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
31982 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
31983 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d  ster to hold nam
31984 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  e of table */.. 
31985 20 20 20 20 20 70 49 6e 66 6f 2d 3e 72 65 67 43       pInfo->regC
31986 74 72 20 3d 20 2b 2b 70 54 6f 70 6c 65 76 65 6c  tr = ++pToplevel
31987 2d 3e 6e 4d 65 6d 3b 20 20 2f 2a 20 4d 61 78 20  ->nMem;  /* Max 
31988 72 6f 77 69 64 20 72 65 67 69 73 74 65 72 20 2a  rowid register *
31989 2f 0d 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76  /..      pToplev
3198a 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20  el->nMem++;     
3198b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3198c 52 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f  Rowid in sqlite_
3198d 73 65 71 75 65 6e 63 65 20 2a 2f 0d 0a 20 20 20  sequence */..   
3198e 20 7d 0d 0a 20 20 20 20 6d 65 6d 49 64 20 3d 20   }..    memId = 
3198f 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 3b 0d 0a  pInfo->regCtr;..
31990 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6d 65    }..  return me
31991 6d 49 64 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  mId;..}..../*..*
31992 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
31993 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 68  enerates code th
31994 61 74 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69  at will initiali
31995 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 0d 0a 2a  ze all of the..*
31996 2a 20 72 65 67 69 73 74 65 72 20 75 73 65 64 20  * register used 
31997 62 79 20 74 68 65 20 61 75 74 6f 69 6e 63 72 65  by the autoincre
31998 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e 20 20 0d  ment tracker.  .
31999 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
3199a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3199b 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67  AutoincrementBeg
3199c 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  in(Parse *pParse
3199d 29 7b 0d 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  ){..  AutoincInf
3199e 6f 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  o *p;           
3199f 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
319a0 61 62 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43  about an AUTOINC
319a1 52 45 4d 45 4e 54 20 2a 2f 0d 0a 20 20 73 71 6c  REMENT */..  sql
319a2 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
319a3 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64  e->db;  /* The d
319a4 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
319a5 6f 6e 20 2a 2f 0d 0a 20 20 44 62 20 2a 70 44 62  on */..  Db *pDb
319a6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
319a7 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
319a8 6f 6e 6c 79 20 61 75 74 6f 69 6e 63 20 74 61 62  only autoinc tab
319a9 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 65 6d  le */..  int mem
319aa 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Id;             
319ab 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
319ac 68 6f 6c 64 69 6e 67 20 6d 61 78 20 72 6f 77 69  holding max rowi
319ad 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72  d */..  int addr
319ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
319af 20 20 20 2f 2a 20 41 20 56 44 42 45 20 61 64 64     /* A VDBE add
319b0 72 65 73 73 20 2a 2f 0d 0a 20 20 56 64 62 65 20  ress */..  Vdbe 
319b1 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
319b2 62 65 3b 20 20 20 2f 2a 20 56 44 42 45 20 75 6e  be;   /* VDBE un
319b3 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
319b4 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73   */....  /* This
319b5 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65   routine is neve
319b6 72 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20  r called during 
319b7 74 72 69 67 67 65 72 2d 67 65 6e 65 72 61 74 69  trigger-generati
319b8 6f 6e 2e 20 20 49 74 20 69 73 0d 0a 20 20 2a 2a  on.  It is..  **
319b9 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
319ba 6d 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  m the top-level 
319bb 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
319bc 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
319bd 62 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  b==0 );..  asser
319be 74 28 20 70 50 61 72 73 65 3d 3d 73 71 6c 69 74  t( pParse==sqlit
319bf 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
319c0 70 50 61 72 73 65 29 20 29 3b 0d 0a 0d 0a 20 20  pParse) );....  
319c1 61 73 73 65 72 74 28 20 76 20 29 3b 20 20 20 2f  assert( v );   /
319c2 2a 20 57 65 20 66 61 69 6c 65 64 20 6c 6f 6e 67  * We failed long
319c3 20 61 67 6f 20 69 66 20 74 68 69 73 20 69 73 20   ago if this is 
319c4 6e 6f 74 20 73 6f 20 2a 2f 0d 0a 20 20 66 6f 72  not so */..  for
319c5 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69  (p = pParse->pAi
319c6 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70 4e  nc; p; p = p->pN
319c7 65 78 74 29 7b 0d 0a 20 20 20 20 70 44 62 20 3d  ext){..    pDb =
319c8 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62   &db->aDb[p->iDb
319c9 5d 3b 0d 0a 20 20 20 20 6d 65 6d 49 64 20 3d 20  ];..    memId = 
319ca 70 2d 3e 72 65 67 43 74 72 3b 0d 0a 20 20 20 20  p->regCtr;..    
319cb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
319cc 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
319cd 62 2c 20 30 2c 20 70 44 62 2d 3e 70 53 63 68 65  b, 0, pDb->pSche
319ce 6d 61 29 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69  ma) );..    sqli
319cf 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
319d0 72 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20  rse, 0, p->iDb, 
319d1 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53  pDb->pSchema->pS
319d2 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65  eqTab, OP_OpenRe
319d3 61 64 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ad);..    sqlite
319d4 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
319d5 50 5f 4e 75 6c 6c 2c 20 30 2c 20 6d 65 6d 49 64  P_Null, 0, memId
319d6 2c 20 6d 65 6d 49 64 2b 31 29 3b 0d 0a 20 20 20  , memId+1);..   
319d7 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
319d8 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
319d9 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
319da 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
319db 53 74 72 69 6e 67 38 2c 20 30 2c 20 6d 65 6d 49  String8, 0, memI
319dc 64 2d 31 2c 20 30 2c 20 70 2d 3e 70 54 61 62 2d  d-1, 0, p->pTab-
319dd 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0d 0a 20 20 20  >zName, 0);..   
319de 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
319df 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
319e0 20 30 2c 20 61 64 64 72 2b 39 29 3b 0d 0a 20 20   0, addr+9);..  
319e1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
319e2 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
319e3 2c 20 30 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0d  , 0, 0, memId);.
319e4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
319e5 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
319e6 20 6d 65 6d 49 64 2d 31 2c 20 61 64 64 72 2b 37   memId-1, addr+7
319e7 2c 20 6d 65 6d 49 64 29 3b 0d 0a 20 20 20 20 73  , memId);..    s
319e8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
319e9 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d  P5(v, SQLITE_JUM
319ea 50 49 46 4e 55 4c 4c 29 3b 0d 0a 20 20 20 20 73  PIFNULL);..    s
319eb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
319ec 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 30 2c  (v, OP_Rowid, 0,
319ed 20 6d 65 6d 49 64 2b 31 29 3b 0d 0a 20 20 20 20   memId+1);..    
319ee 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
319ef 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
319f0 30 2c 20 31 2c 20 6d 65 6d 49 64 29 3b 0d 0a 20  0, 1, memId);.. 
319f1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
319f2 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
319f3 20 30 2c 20 61 64 64 72 2b 39 29 3b 0d 0a 20 20   0, addr+9);..  
319f4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
319f5 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
319f6 30 2c 20 61 64 64 72 2b 32 29 3b 0d 0a 20 20 20  0, addr+2);..   
319f7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
319f8 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
319f9 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0d 0a 20 20  , 0, memId);..  
319fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
319fb 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29  Op0(v, OP_Close)
319fc 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
319fd 0a 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 6d  .** Update the m
319fe 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72  aximum rowid for
319ff 20 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   an autoincremen
31a00 74 20 63 61 6c 63 75 6c 61 74 69 6f 6e 2e 0d 0a  t calculation...
31a01 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
31a02 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 63 61  ine should be ca
31a03 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 74 6f  lled when the to
31a04 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 68  p of the stack h
31a05 6f 6c 64 73 20 61 0d 0a 2a 2a 20 6e 65 77 20 72  olds a..** new r
31a06 6f 77 69 64 20 74 68 61 74 20 69 73 20 61 62 6f  owid that is abo
31a07 75 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  ut to be inserte
31a08 64 2e 20 20 49 66 20 74 68 61 74 20 6e 65 77 20  d.  If that new 
31a09 72 6f 77 69 64 20 69 73 0d 0a 2a 2a 20 6c 61 72  rowid is..** lar
31a0a 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
31a0b 69 6d 75 6d 20 72 6f 77 69 64 20 69 6e 20 74 68  imum rowid in th
31a0c 65 20 6d 65 6d 49 64 20 6d 65 6d 6f 72 79 20 63  e memId memory c
31a0d 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a  ell, then the..*
31a0e 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73  * memory cell is
31a0f 20 75 70 64 61 74 65 64 2e 20 20 54 68 65 20 73   updated.  The s
31a10 74 61 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65  tack is unchange
31a11 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  d...*/..static v
31a12 6f 69 64 20 61 75 74 6f 49 6e 63 53 74 65 70 28  oid autoIncStep(
31a13 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
31a14 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74 20 72 65  nt memId, int re
31a15 67 52 6f 77 69 64 29 7b 0d 0a 20 20 69 66 28 20  gRowid){..  if( 
31a16 6d 65 6d 49 64 3e 30 20 29 7b 0d 0a 20 20 20 20  memId>0 ){..    
31a17 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
31a18 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  2(pParse->pVdbe,
31a19 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d 49   OP_MemMax, memI
31a1a 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20  d, regRowid);.. 
31a1b 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
31a1c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e  This routine gen
31a1d 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20  erates the code 
31a1e 6e 65 65 64 65 64 20 74 6f 20 77 72 69 74 65 20  needed to write 
31a1f 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0d 0a 2a  autoincrement..*
31a20 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  * maximum rowid 
31a21 76 61 6c 75 65 73 20 62 61 63 6b 20 69 6e 74 6f  values back into
31a22 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
31a23 65 6e 63 65 20 72 65 67 69 73 74 65 72 2e 0d 0a  ence register...
31a24 2a 2a 20 45 76 65 72 79 20 73 74 61 74 65 6d 65  ** Every stateme
31a25 6e 74 20 74 68 61 74 20 6d 69 67 68 74 20 64 6f  nt that might do
31a26 20 61 6e 20 49 4e 53 45 52 54 20 69 6e 74 6f 20   an INSERT into 
31a27 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  an autoincrement
31a28 0d 0a 2a 2a 20 74 61 62 6c 65 20 28 65 69 74 68  ..** table (eith
31a29 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 74  er directly or t
31a2a 68 72 6f 75 67 68 20 74 72 69 67 67 65 72 73 29  hrough triggers)
31a2b 20 6e 65 65 64 73 20 74 6f 20 63 61 6c 6c 20 74   needs to call t
31a2c 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  his..** routine 
31a2d 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20  just before the 
31a2e 22 65 78 69 74 22 20 63 6f 64 65 2e 0d 0a 2a 2f  "exit" code...*/
31a2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
31a30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
31a31 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50 61  oincrementEnd(Pa
31a32 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0d 0a 20  rse *pParse){.. 
31a33 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b   AutoincInfo *p;
31a34 0d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ..  Vdbe *v = pP
31a35 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20  arse->pVdbe;..  
31a36 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
31a37 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 61  arse->db;....  a
31a38 73 73 65 72 74 28 20 76 20 29 3b 0d 0a 20 20 66  ssert( v );..  f
31a39 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  or(p = pParse->p
31a3a 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e  Ainc; p; p = p->
31a3b 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 44 62 20  pNext){..    Db 
31a3c 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
31a3d 70 2d 3e 69 44 62 5d 3b 0d 0a 20 20 20 20 69 6e  p->iDb];..    in
31a3e 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34  t j1, j2, j3, j4
31a3f 2c 20 6a 35 3b 0d 0a 20 20 20 20 69 6e 74 20 69  , j5;..    int i
31a40 52 65 63 3b 0d 0a 20 20 20 20 69 6e 74 20 6d 65  Rec;..    int me
31a41 6d 49 64 20 3d 20 70 2d 3e 72 65 67 43 74 72 3b  mId = p->regCtr;
31a42 0d 0a 0d 0a 20 20 20 20 69 52 65 63 20 3d 20 73  ....    iRec = s
31a43 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
31a44 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 61  (pParse);..    a
31a45 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
31a46 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
31a47 2c 20 30 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d  , 0, pDb->pSchem
31a48 61 29 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  a) );..    sqlit
31a49 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
31a4a 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70  se, 0, p->iDb, p
31a4b 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
31a4c 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  qTab, OP_OpenWri
31a4d 74 65 29 3b 0d 0a 20 20 20 20 6a 31 20 3d 20 73  te);..    j1 = s
31a4e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
31a4f 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
31a50 6d 65 6d 49 64 2b 31 29 3b 0d 0a 20 20 20 20 6a  memId+1);..    j
31a51 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
31a52 64 64 4f 70 30 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp0(v, OP_Rewi
31a53 6e 64 29 3b 0d 0a 20 20 20 20 6a 33 20 3d 20 73  nd);..    j3 = s
31a54 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
31a55 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30  (v, OP_Column, 0
31a56 2c 20 30 2c 20 69 52 65 63 29 3b 0d 0a 20 20 20  , 0, iRec);..   
31a57 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j4 = sqlite3Vdb
31a58 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71  eAddOp3(v, OP_Eq
31a59 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 69 52  , memId-1, 0, iR
31a5a 65 63 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ec);..    sqlite
31a5b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
31a5c 50 5f 4e 65 78 74 2c 20 30 2c 20 6a 33 29 3b 0d  P_Next, 0, j3);.
31a5d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
31a5e 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b  JumpHere(v, j2);
31a5f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
31a60 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
31a61 77 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64  wRowid, 0, memId
31a62 2b 31 29 3b 0d 0a 20 20 20 20 6a 35 20 3d 20 73  +1);..    j5 = s
31a63 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
31a64 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0d 0a 20  (v, OP_Goto);.. 
31a65 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
31a66 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0d 0a  mpHere(v, j4);..
31a67 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31a68 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
31a69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0d  d, 0, memId+1);.
31a6a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
31a6b 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
31a6c 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
31a6d 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29  eJumpHere(v, j5)
31a6e 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
31a6f 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
31a70 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64  akeRecord, memId
31a71 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0d 0a 20  -1, 2, iRec);.. 
31a72 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
31a73 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
31a74 74 2c 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d 49  t, 0, iRec, memI
31a75 64 2b 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  d+1);..    sqlit
31a76 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
31a77 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
31a78 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
31a79 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43  beAddOp0(v, OP_C
31a7a 6c 6f 73 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69  lose);..    sqli
31a7b 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
31a7c 67 28 70 50 61 72 73 65 2c 20 69 52 65 63 29 3b  g(pParse, iRec);
31a7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73 65 0d  ..  }..}..#else.
31a7e 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ./*..** If SQLIT
31a7f 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
31a80 4d 45 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c  MENT is defined,
31a81 20 74 68 65 6e 20 74 68 65 20 74 68 72 65 65 20   then the three 
31a82 72 6f 75 74 69 6e 65 73 0d 0a 2a 2a 20 61 62 6f  routines..** abo
31a83 76 65 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70  ve are all no-op
31a84 73 0d 0a 2a 2f 0d 0a 23 20 64 65 66 69 6e 65 20  s..*/..# define 
31a85 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 41 2c 42  autoIncBegin(A,B
31a86 2c 43 29 20 28 30 29 0d 0a 23 20 64 65 66 69 6e  ,C) (0)..# defin
31a87 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28 41 2c  e autoIncStep(A,
31a88 42 2c 43 29 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  B,C)..#endif /* 
31a89 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
31a8a 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0d 0a 0d 0a  INCREMENT */....
31a8b 0d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
31a8c 6c 61 72 61 74 69 6f 6e 20 2a 2f 0d 0a 73 74 61  laration */..sta
31a8d 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
31a8e 6d 69 7a 61 74 69 6f 6e 28 0d 0a 20 20 50 61 72  mization(..  Par
31a8f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
31a90 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
31a91 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  text */..  Table
31a92 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20 20   *pDest,        
31a93 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65   /* The table we
31a94 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69   are inserting i
31a95 6e 74 6f 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74  nto */..  Select
31a96 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
31a97 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
31a98 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20  ement to use as 
31a99 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20  the data source 
31a9a 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f  */..  int onErro
31a9b 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  r,          /* H
31a9c 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e  ow to handle con
31a9d 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a  straint errors *
31a9e 2f 0d 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74  /..  int iDbDest
31a9f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
31aa0 65 20 64 61 74 61 62 61 73 65 20 6f 66 20 70 44  e database of pD
31aa1 65 73 74 20 2a 2f 0d 0a 29 3b 0d 0a 0d 0a 2f 2a  est */..);..../*
31aa2 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
31aa3 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e  e is call to han
31aa4 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66  dle SQL of the f
31aa5 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0d  ollowing forms:.
31aa6 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 69 6e 73 65 72  .**..**    inser
31aa7 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
31aa8 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45 58 50  LIST) values(EXP
31aa9 52 4c 49 53 54 29 0d 0a 2a 2a 20 20 20 20 69 6e  RLIST)..**    in
31aaa 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20  sert into TABLE 
31aab 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74 0d  (IDLIST) select.
31aac 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 49 44 4c 49  .**..** The IDLI
31aad 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ST following the
31aae 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61   table name is a
31aaf 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  lways optional. 
31ab0 20 49 66 20 6f 6d 69 74 74 65 64 2c 0d 0a 2a 2a   If omitted,..**
31ab1 20 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20   then a list of 
31ab2 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20  all columns for 
31ab3 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62  the table is sub
31ab4 73 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49  stituted.  The I
31ab5 44 4c 49 53 54 0d 0a 2a 2a 20 61 70 70 65 61 72  DLIST..** appear
31ab6 73 20 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e  s in the pColumn
31ab7 20 70 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f   parameter.  pCo
31ab8 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20  lumn is NULL if 
31ab9 49 44 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65  IDLIST is omitte
31aba 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 70  d...**..** The p
31abb 4c 69 73 74 20 70 61 72 61 6d 65 74 65 72 20 68  List parameter h
31abc 6f 6c 64 73 20 45 58 50 52 4c 49 53 54 20 69 6e  olds EXPRLIST in
31abd 20 74 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20   the first form 
31abe 6f 66 20 74 68 65 20 49 4e 53 45 52 54 0d 0a 2a  of the INSERT..*
31abf 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76  * statement abov
31ac0 65 2c 20 61 6e 64 20 70 53 65 6c 65 63 74 20 69  e, and pSelect i
31ac1 73 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 74 68 65  s NULL.  For the
31ac2 20 73 65 63 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c   second form, pL
31ac3 69 73 74 20 69 73 0d 0a 2a 2a 20 4e 55 4c 4c 20  ist is..** NULL 
31ac4 61 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 61  and pSelect is a
31ac5 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
31ac6 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
31ac7 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
31ac8 65 0d 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 74  e..** data for t
31ac9 68 65 20 69 6e 73 65 72 74 2e 0d 0a 2a 2a 0d 0a  he insert...**..
31aca 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
31acb 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20 6f 6e  rated follows on
31acc 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70 6c 61  e of four templa
31acd 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69 6d 70  tes.  For a simp
31ace 6c 65 0d 0a 2a 2a 20 73 65 6c 65 63 74 20 77 69  le..** select wi
31acf 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 66  th data coming f
31ad0 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c 61  rom a VALUES cla
31ad1 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65 78  use, the code ex
31ad2 65 63 75 74 65 73 0d 0a 2a 2a 20 6f 6e 63 65 20  ecutes..** once 
31ad3 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68  straight down th
31ad4 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f 2d 63  rough.  Pseudo-c
31ad5 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77 65 20  ode follows (we 
31ad6 63 61 6c 6c 20 74 68 69 73 0d 0a 2a 2a 20 74 68  call this..** th
31ad7 65 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65 22  e "1st template"
31ad8 29 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  ):..**..**      
31ad9 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75     open write cu
31ada 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
31adb 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0d  and its indices.
31adc 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74 73  .**         puts
31add 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65   VALUES clause e
31ade 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20  xpressions onto 
31adf 74 68 65 20 73 74 61 63 6b 0d 0a 2a 2a 20 20 20  the stack..**   
31ae0 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20        write the 
31ae1 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64  resulting record
31ae2 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0d 0a 2a   into <table>..*
31ae3 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
31ae4 70 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 68  p..**..** The th
31ae5 72 65 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65  ree remaining te
31ae6 6d 70 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74  mplates assume t
31ae7 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
31ae8 6f 66 20 74 68 65 20 66 6f 72 6d 0d 0a 2a 2a 0d  of the form..**.
31ae9 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54  .**   INSERT INT
31aea 4f 20 3c 74 61 62 6c 65 3e 20 53 45 4c 45 43 54  O <table> SELECT
31aeb 20 2e 2e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20   .....**..** If 
31aec 74 68 65 20 53 45 4c 45 43 54 20 63 6c 61 75 73  the SELECT claus
31aed 65 20 69 73 20 6f 66 20 74 68 65 20 72 65 73 74  e is of the rest
31aee 72 69 63 74 65 64 20 66 6f 72 6d 20 22 53 45 4c  ricted form "SEL
31aef 45 43 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c  ECT * FROM <tabl
31af0 65 32 3e 22 20 2d 0d 0a 2a 2a 20 69 6e 20 6f 74  e2>" -..** in ot
31af1 68 65 72 20 77 6f 72 64 73 20 69 66 20 74 68 65  her words if the
31af2 20 53 45 4c 45 43 54 20 70 75 6c 6c 73 20 61 6c   SELECT pulls al
31af3 6c 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61  l columns from a
31af4 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 0d 0a 2a   single table..*
31af5 2a 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e  * and there is n
31af6 6f 20 57 48 45 52 45 20 6f 72 20 4c 49 4d 49 54  o WHERE or LIMIT
31af7 20 6f 72 20 47 52 4f 55 50 20 42 59 20 6f 72 20   or GROUP BY or 
31af8 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73  ORDER BY clauses
31af9 2c 20 61 6e 64 0d 0a 2a 2a 20 69 66 20 3c 74 61  , and..** if <ta
31afa 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61 62 6c 65  ble2> and <table
31afb 31 3e 20 61 72 65 20 64 69 73 74 69 6e 63 74 20  1> are distinct 
31afc 74 61 62 6c 65 73 20 62 75 74 20 68 61 76 65 20  tables but have 
31afd 69 64 65 6e 74 69 63 61 6c 0d 0a 2a 2a 20 73 63  identical..** sc
31afe 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69 6e 67  hemas, including
31aff 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 69 6e   all the same in
31b00 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20 73 70  dices, then a sp
31b01 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69  ecial optimizati
31b02 6f 6e 0d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  on..** is invoke
31b03 64 20 74 68 61 74 20 63 6f 70 69 65 73 20 72 61  d that copies ra
31b04 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 3c  w records from <
31b05 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20  table2> over to 
31b06 3c 74 61 62 6c 65 31 3e 2e 0d 0a 2a 2a 20 53 65  <table1>...** Se
31b07 65 20 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69  e the xferOptimi
31b08 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  zation() functio
31b09 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d  n for the implem
31b0a 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
31b0b 0d 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20  ..** template.  
31b0c 54 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20  This is the 2nd 
31b0d 74 65 6d 70 6c 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a  template...**..*
31b0e 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61  *         open a
31b0f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f   write cursor to
31b10 20 3c 74 61 62 6c 65 3e 0d 0a 2a 2a 20 20 20 20   <table>..**    
31b11 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63       open read c
31b12 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32  ursor on <table2
31b13 3e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72  >..**         tr
31b14 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
31b15 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f  ds in <table2> o
31b16 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0d 0a  ver to <table>..
31b17 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65  **         close
31b18 20 63 75 72 73 6f 72 73 0d 0a 2a 2a 20 20 20 20   cursors..**    
31b19 20 20 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64       foreach ind
31b1a 65 78 20 6f 6e 20 3c 74 61 62 6c 65 3e 0d 0a 2a  ex on <table>..*
31b1b 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e  *           open
31b1c 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
31b1d 6f 6e 20 74 68 65 20 3c 74 61 62 6c 65 3e 20 69  on the <table> i
31b1e 6e 64 65 78 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ndex..**        
31b1f 20 20 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63     open a read c
31b20 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 63 6f 72  ursor on the cor
31b21 72 65 73 70 6f 6e 64 69 6e 67 20 3c 74 61 62 6c  responding <tabl
31b22 65 32 3e 20 69 6e 64 65 78 0d 0a 2a 2a 20 20 20  e2> index..**   
31b23 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72          transfer
31b24 20 61 6c 6c 20 72 65 63 6f 72 64 73 20 66 72 6f   all records fro
31b25 6d 20 74 68 65 20 72 65 61 64 20 74 6f 20 74 68  m the read to th
31b26 65 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 0d  e write cursors.
31b27 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6c  .**           cl
31b28 6f 73 65 20 63 75 72 73 6f 72 73 0d 0a 2a 2a 20  ose cursors..** 
31b29 20 20 20 20 20 20 20 20 65 6e 64 20 66 6f 72 65          end fore
31b2a 61 63 68 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ach..**..** The 
31b2b 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69 73 20  3rd template is 
31b2c 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73 65 63  for when the sec
31b2d 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64 6f 65  ond template doe
31b2e 73 20 6e 6f 74 20 61 70 70 6c 79 0d 0a 2a 2a 20  s not apply..** 
31b2f 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20 63  and the SELECT c
31b30 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 72  lause does not r
31b31 65 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65 3e  ead from <table>
31b32 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0d 0a 2a   at any time...*
31b33 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20  * The generated 
31b34 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69  code follows thi
31b35 73 20 74 65 6d 70 6c 61 74 65 3a 0d 0a 2a 2a 0d  s template:..**.
31b36 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20  .**         EOF 
31b37 3c 2d 20 30 0d 0a 2a 2a 20 20 20 20 20 20 20 20  <- 0..**        
31b38 20 58 20 3c 2d 20 41 0d 0a 2a 2a 20 20 20 20 20   X <- A..**     
31b39 20 20 20 20 67 6f 74 6f 20 42 0d 0a 2a 2a 20 20      goto B..**  
31b3a 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
31b3b 20 74 68 65 20 53 45 4c 45 43 54 0d 0a 2a 2a 20   the SELECT..** 
31b3c 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65          loop ove
31b3d 72 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68  r the rows in th
31b3e 65 20 53 45 4c 45 43 54 0d 0a 2a 2a 20 20 20 20  e SELECT..**    
31b3f 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75         load valu
31b40 65 73 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  es into register
31b41 73 20 52 2e 2e 52 2b 6e 0d 0a 2a 2a 20 20 20 20  s R..R+n..**    
31b42 20 20 20 20 20 20 20 79 69 65 6c 64 20 58 0d 0a         yield X..
31b43 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c  **         end l
31b44 6f 6f 70 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  oop..**         
31b45 63 6c 65 61 6e 75 70 20 61 66 74 65 72 20 74 68  cleanup after th
31b46 65 20 53 45 4c 45 43 54 0d 0a 2a 2a 20 20 20 20  e SELECT..**    
31b47 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0d 0a 2a       EOF <- 1..*
31b48 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20  *         yield 
31b49 58 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f  X..**         go
31b4a 74 6f 20 41 0d 0a 2a 2a 20 20 20 20 20 20 42 3a  to A..**      B:
31b4b 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73   open write curs
31b4c 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e  or to <table> an
31b4d 64 20 69 74 73 20 69 6e 64 69 63 65 73 0d 0a 2a  d its indices..*
31b4e 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20  *      C: yield 
31b4f 58 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66  X..**         if
31b50 20 45 4f 46 20 67 6f 74 6f 20 44 0d 0a 2a 2a 20   EOF goto D..** 
31b51 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74          insert t
31b52 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
31b53 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72   into <table> fr
31b54 6f 6d 20 52 2e 2e 52 2b 6e 0d 0a 2a 2a 20 20 20  om R..R+n..**   
31b55 20 20 20 20 20 20 67 6f 74 6f 20 43 0d 0a 2a 2a        goto C..**
31b56 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70        D: cleanup
31b57 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 34 74 68  ..**..** The 4th
31b58 20 74 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65   template is use
31b59 64 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  d if the insert 
31b5a 73 74 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20  statement takes 
31b5b 69 74 73 0d 0a 2a 2a 20 76 61 6c 75 65 73 20 66  its..** values f
31b5c 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 62 75 74  rom a SELECT but
31b5d 20 74 68 65 20 64 61 74 61 20 69 73 20 62 65 69   the data is bei
31b5e 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ng inserted into
31b5f 20 61 20 74 61 62 6c 65 0d 0a 2a 2a 20 74 68 61   a table..** tha
31b60 74 20 69 73 20 61 6c 73 6f 20 72 65 61 64 20 61  t is also read a
31b61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 53 45  s part of the SE
31b62 4c 45 43 54 2e 20 20 49 6e 20 74 68 65 20 74 68  LECT.  In the th
31b63 69 72 64 20 66 6f 72 6d 2c 0d 0a 2a 2a 20 77 65  ird form,..** we
31b64 20 68 61 76 65 20 74 6f 20 75 73 65 20 61 20 69   have to use a i
31b65 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
31b66 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  e to store the r
31b67 65 73 75 6c 74 73 20 6f 66 0d 0a 2a 2a 20 74 68  esults of..** th
31b68 65 20 73 65 6c 65 63 74 2e 20 20 54 68 65 20 74  e select.  The t
31b69 65 6d 70 6c 61 74 65 20 69 73 20 6c 69 6b 65 20  emplate is like 
31b6a 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  this:..**..**   
31b6b 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0d 0a        EOF <- 0..
31b6c 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20  **         X <- 
31b6d 41 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f  A..**         go
31b6e 74 6f 20 42 0d 0a 2a 2a 20 20 20 20 20 20 41 3a  to B..**      A:
31b6f 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53   setup for the S
31b70 45 4c 45 43 54 0d 0a 2a 2a 20 20 20 20 20 20 20  ELECT..**       
31b71 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
31b72 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45  tables in the SE
31b73 4c 45 43 54 0d 0a 2a 2a 20 20 20 20 20 20 20 20  LECT..**        
31b74 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
31b75 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
31b76 2b 6e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  +n..**          
31b77 20 79 69 65 6c 64 20 58 0d 0a 2a 2a 20 20 20 20   yield X..**    
31b78 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0d 0a 2a       end loop..*
31b79 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
31b7a 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
31b7b 43 54 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  CT..**         E
31b7c 4f 46 20 3c 2d 20 31 0d 0a 2a 2a 20 20 20 20 20  OF <- 1..**     
31b7d 20 20 20 20 79 69 65 6c 64 20 58 0d 0a 2a 2a 20      yield X..** 
31b7e 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72          halt-err
31b7f 6f 72 0d 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f  or..**      B: o
31b80 70 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0d 0a  pen temp table..
31b81 2a 2a 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64  **      L: yield
31b82 20 58 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69   X..**         i
31b83 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0d 0a 2a 2a  f EOF goto M..**
31b84 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20           insert 
31b85 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20  row from R..R+n 
31b86 69 6e 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0d  into temp table.
31b87 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f  .**         goto
31b88 20 4c 0d 0a 2a 2a 20 20 20 20 20 20 4d 3a 20 6f   L..**      M: o
31b89 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72  pen write cursor
31b8a 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 20   to <table> and 
31b8b 69 74 73 20 69 6e 64 69 63 65 73 0d 0a 2a 2a 20  its indices..** 
31b8c 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74          rewind t
31b8d 65 6d 70 20 74 61 62 6c 65 0d 0a 2a 2a 20 20 20  emp table..**   
31b8e 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20     C: loop over 
31b8f 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64  rows of intermed
31b90 69 61 74 65 20 74 61 62 6c 65 0d 0a 2a 2a 20 20  iate table..**  
31b91 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
31b92 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e  r values form in
31b93 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
31b94 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0d 0a 2a   into <table>..*
31b95 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
31b96 6f 70 0d 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63  op..**      D: c
31b97 6c 65 61 6e 75 70 0d 0a 2a 2f 0d 0a 53 51 4c 49  leanup..*/..SQLI
31b98 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
31b99 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0d 0a  sqlite3Insert(..
31b9a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
31b9b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
31b9c 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  r context */..  
31b9d 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
31b9e 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  t,    /* Name of
31b9f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
31ba0 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
31ba1 6e 67 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73  ng */..  ExprLis
31ba2 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 2f  t *pList,      /
31ba3 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  * List of values
31ba4 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
31ba5 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53  */..  Select *pS
31ba6 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41  elect,      /* A
31ba7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
31ba8 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20  t to use as the 
31ba9 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0d 0a  data source */..
31baa 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
31bab 6e 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  n,      /* Colum
31bac 6e 20 6e 61 6d 65 73 20 63 6f 72 72 65 73 70 6f  n names correspo
31bad 6e 64 69 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e  nding to IDLIST.
31bae 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6e 45 72 72   */..  int onErr
31baf 6f 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  or           /* 
31bb0 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f  How to handle co
31bb1 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20  nstraint errors 
31bb2 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
31bb3 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
31bb4 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 61  /* The main data
31bb5 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a  base structure *
31bb6 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  /..  Table *pTab
31bb7 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
31bb8 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72  e table to inser
31bb9 74 20 69 6e 74 6f 2e 20 20 61 6b 61 20 54 41 42  t into.  aka TAB
31bba 4c 45 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  LE */..  char *z
31bbb 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Tab;           /
31bbc 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
31bbd 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
31bbe 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
31bbf 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
31bc0 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a 20 4e   *zDb;      /* N
31bc1 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
31bc2 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ase holding this
31bc3 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74   table */..  int
31bc4 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20 20   i, j, idx;     
31bc5 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
31bc6 65 72 73 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a  ers */..  Vdbe *
31bc7 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
31bc8 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
31bc9 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
31bca 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0d 0a 20  al machine */.. 
31bcb 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
31bcc 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
31bcd 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63  oping over indic
31bce 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  es of the table 
31bcf 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d  */..  int nColum
31bd0 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  n;          /* N
31bd1 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
31bd2 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0d   in the data */.
31bd3 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d  .  int nHidden =
31bd4 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62   0;      /* Numb
31bd5 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c  er of hidden col
31bd6 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73  umns if TABLE is
31bd7 20 76 69 72 74 75 61 6c 20 2a 2f 0d 0a 20 20 69   virtual */..  i
31bd8 6e 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20  nt baseCur = 0; 
31bd9 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
31bda 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70  sor number for p
31bdb 54 61 62 20 2a 2f 0d 0a 20 20 69 6e 74 20 6b 65  Tab */..  int ke
31bdc 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20  yColumn = -1;   
31bdd 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69  /* Column that i
31bde 73 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  s the INTEGER PR
31bdf 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0d 0a 20 20  IMARY KEY */..  
31be0 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20  int endOfLoop;  
31be1 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66        /* Label f
31be2 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  or the end of th
31be3 65 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70  e insertion loop
31be4 20 2a 2f 0d 0a 20 20 69 6e 74 20 75 73 65 54 65   */..  int useTe
31be5 6d 70 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20  mpTable = 0; /* 
31be6 53 74 6f 72 65 20 53 45 4c 45 43 54 20 72 65 73  Store SELECT res
31be7 75 6c 74 73 20 69 6e 20 69 6e 74 65 72 6d 65 64  ults in intermed
31be8 69 61 74 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  iate table */.. 
31be9 20 69 6e 74 20 73 72 63 54 61 62 20 3d 20 30 3b   int srcTab = 0;
31bea 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 63         /* Data c
31beb 6f 6d 65 73 20 66 72 6f 6d 20 74 68 69 73 20 74  omes from this t
31bec 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
31bed 69 66 20 3e 3d 30 20 2a 2f 0d 0a 20 20 69 6e 74  if >=0 */..  int
31bee 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 30 3b   addrInsTop = 0;
31bef 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 6c 61     /* Jump to la
31bf0 62 65 6c 20 22 44 22 20 2a 2f 0d 0a 20 20 69 6e  bel "D" */..  in
31bf1 74 20 61 64 64 72 43 6f 6e 74 20 3d 20 30 3b 20  t addrCont = 0; 
31bf2 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 69 6e      /* Top of in
31bf3 73 65 72 74 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c  sert loop. Label
31bf4 20 22 43 22 20 69 6e 20 74 65 6d 70 6c 61 74 65   "C" in template
31bf5 73 20 33 20 61 6e 64 20 34 20 2a 2f 0d 0a 20 20  s 3 and 4 */..  
31bf6 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 20 3d  int addrSelect =
31bf7 20 30 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73   0;   /* Address
31bf8 20 6f 66 20 63 6f 72 6f 75 74 69 6e 65 20 74 68   of coroutine th
31bf9 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
31bfa 65 20 53 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 53  e SELECT */..  S
31bfb 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20  electDest dest; 
31bfc 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
31bfd 69 6f 6e 20 66 6f 72 20 53 45 4c 45 43 54 20 6f  ion for SELECT o
31bfe 6e 20 72 68 73 20 6f 66 20 49 4e 53 45 52 54 20  n rhs of INSERT 
31bff 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  */..  int iDb;  
31c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
31c01 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65  ndex of database
31c02 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a   holding TABLE *
31c03 2f 0d 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  /..  Db *pDb;   
31c04 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
31c05 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
31c06 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e  ining table bein
31c07 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
31c08 2a 2f 0d 0a 20 20 69 6e 74 20 61 70 70 65 6e 64  */..  int append
31c09 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54  Flag = 0;   /* T
31c0a 72 75 65 20 69 66 20 74 68 65 20 69 6e 73 65 72  rue if the inser
31c0b 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  t is likely to b
31c0c 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0d 0a  e an append */..
31c0d 0d 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
31c0e 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0d 0a  allocations */..
31c0f 20 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c    int regFromSel
31c10 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65 20  ect = 0;/* Base 
31c11 72 65 67 69 73 74 65 72 20 66 6f 72 20 64 61 74  register for dat
31c12 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45  a coming from SE
31c13 4c 45 43 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  LECT */..  int r
31c14 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20  egAutoinc = 0;  
31c15 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
31c16 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43  ding the AUTOINC
31c17 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a  REMENT counter *
31c18 2f 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43  /..  int regRowC
31c19 6f 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65  ount = 0;  /* Me
31c1a 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66  mory cell used f
31c1b 6f 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74  or the row count
31c1c 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  er */..  int reg
31c1d 49 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Ins;           /
31c1e 2a 20 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20  * Block of regs 
31c1f 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61  holding rowid+da
31c20 74 61 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  ta being inserte
31c21 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 52  d */..  int regR
31c22 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  owid;         /*
31c23 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69   registers holdi
31c24 6e 67 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20  ng insert rowid 
31c25 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 44 61 74  */..  int regDat
31c26 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72  a;          /* r
31c27 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
31c28 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20  first column to 
31c29 69 6e 73 65 72 74 20 2a 2f 0d 0a 20 20 69 6e 74  insert */..  int
31c2a 20 72 65 67 45 6f 66 20 3d 20 30 3b 20 20 20 20   regEof = 0;    
31c2b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 72     /* Register r
31c2c 65 63 6f 72 64 69 6e 67 20 65 6e 64 20 6f 66 20  ecording end of 
31c2d 53 45 4c 45 43 54 20 64 61 74 61 20 2a 2f 0d 0a  SELECT data */..
31c2e 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 3d    int *aRegIdx =
31c2f 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72   0;     /* One r
31c30 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74 65  egister allocate
31c31 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78 20  d to each index 
31c32 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  */....#ifndef SQ
31c33 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
31c34 52 0d 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b  R..  int isView;
31c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c36 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65   /* True if atte
31c37 6d 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74  mpting to insert
31c38 20 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0d   into a view */.
31c39 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
31c3a 67 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f  gger;          /
31c3b 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
31c3c 72 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72  rs on pTab, if r
31c3d 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 69 6e  equired */..  in
31c3e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  t tmask;        
31c3f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
31c40 6b 20 6f 66 20 74 72 69 67 67 65 72 20 74 69 6d  k of trigger tim
31c41 65 73 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d  es */..#endif...
31c42 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
31c43 64 62 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 64  db;..  memset(&d
31c44 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  est, 0, sizeof(d
31c45 65 73 74 29 29 3b 0d 0a 20 20 69 66 28 20 70 50  est));..  if( pP
31c46 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
31c47 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
31c48 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  {..    goto inse
31c49 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  rt_cleanup;..  }
31c4a 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20  ....  /* Locate 
31c4b 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
31c4c 68 69 63 68 20 77 65 20 77 69 6c 6c 20 62 65 20  hich we will be 
31c4d 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 69 6e  inserting new in
31c4e 66 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 20 20 2a 2f  formation...  */
31c4f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
31c50 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  List->nSrc==1 );
31c51 0d 0a 20 20 7a 54 61 62 20 3d 20 70 54 61 62 4c  ..  zTab = pTabL
31c52 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b  ist->a[0].zName;
31c53 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54  ..  if( NEVER(zT
31c54 61 62 3d 3d 30 29 20 29 20 67 6f 74 6f 20 69 6e  ab==0) ) goto in
31c55 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  sert_cleanup;.. 
31c56 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
31c57 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
31c58 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0d  rse, pTabList);.
31c59 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
31c5a 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  {..    goto inse
31c5b 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  rt_cleanup;..  }
31c5c 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
31c5d 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
31c5e 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
31c5f 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 44  );..  assert( iD
31c60 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20  b<db->nDb );..  
31c61 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
31c62 44 62 5d 3b 0d 0a 20 20 7a 44 62 20 3d 20 70 44  Db];..  zDb = pD
31c63 62 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 69 66 28  b->zName;..  if(
31c64 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
31c65 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
31c66 5f 49 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a  _INSERT, pTab->z
31c67 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b  Name, 0, zDb) ){
31c68 0d 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  ..    goto inser
31c69 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d  t_cleanup;..  }.
31c6a 0a 0d 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  ...  /* Figure o
31c6b 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61 6e  ut if we have an
31c6c 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 69  y triggers and i
31c6d 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
31c6e 67 0d 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 64  g..  ** inserted
31c6f 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77 0d   into is a view.
31c70 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  .  */..#ifndef S
31c71 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
31c72 45 52 0d 0a 20 20 70 54 72 69 67 67 65 72 20 3d  ER..  pTrigger =
31c73 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73   sqlite3Triggers
31c74 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  Exist(pParse, pT
31c75 61 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30  ab, TK_INSERT, 0
31c76 2c 20 26 74 6d 61 73 6b 29 3b 0d 0a 20 20 69 73  , &tmask);..  is
31c77 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65  View = pTab->pSe
31c78 6c 65 63 74 21 3d 30 3b 0d 0a 23 65 6c 73 65 0d  lect!=0;..#else.
31c79 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67 67  .# define pTrigg
31c7a 65 72 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 74  er 0..# define t
31c7b 6d 61 73 6b 20 30 0d 0a 23 20 64 65 66 69 6e 65  mask 0..# define
31c7c 20 69 73 56 69 65 77 20 30 0d 0a 23 65 6e 64 69   isView 0..#endi
31c7d 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
31c7e 5f 4f 4d 49 54 5f 56 49 45 57 0d 0a 23 20 75 6e  _OMIT_VIEW..# un
31c7f 64 65 66 20 69 73 56 69 65 77 0d 0a 23 20 64 65  def isView..# de
31c80 66 69 6e 65 20 69 73 56 69 65 77 20 30 0d 0a 23  fine isView 0..#
31c81 65 6e 64 69 66 0d 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
31c82 20 28 70 54 72 69 67 67 65 72 20 26 26 20 74 6d   (pTrigger && tm
31c83 61 73 6b 29 20 7c 7c 20 28 70 54 72 69 67 67 65  ask) || (pTrigge
31c84 72 3d 3d 30 20 26 26 20 74 6d 61 73 6b 3d 3d 30  r==0 && tmask==0
31c85 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  ) );....  /* If 
31c86 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61  pTab is really a
31c87 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65   view, make sure
31c88 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69   it has been ini
31c89 74 69 61 6c 69 7a 65 64 2e 0d 0a 20 20 2a 2a 20  tialized...  ** 
31c8a 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
31c8b 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20  es() is a no-op 
31c8c 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61  if pTab is not a
31c8d 20 76 69 65 77 20 28 6f 72 20 76 69 72 74 75 61   view (or virtua
31c8e 6c 20 0d 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20  l ..  ** module 
31c8f 74 61 62 6c 65 29 2e 0d 0a 20 20 2a 2f 0d 0a 20  table)...  */.. 
31c90 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77   if( sqlite3View
31c91 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
31c92 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0d  Parse, pTab) ){.
31c93 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
31c94 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a  _cleanup;..  }..
31c95 0d 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68  ..  /* Ensure th
31c96 61 74 3a 0d 0a 20 20 2a 20 20 28 61 29 20 74 68  at:..  *  (a) th
31c97 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72  e table is not r
31c98 65 61 64 2d 6f 6e 6c 79 2c 20 0d 0a 20 20 2a 20  ead-only, ..  * 
31c99 20 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20   (b) that if it 
31c9a 69 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f  is a view then O
31c9b 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72  N INSERT trigger
31c9c 73 20 65 78 69 73 74 0d 0a 20 20 2a 2f 0d 0a 20  s exist..  */.. 
31c9d 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65   if( sqlite3IsRe
31c9e 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70  adOnly(pParse, p
31c9f 54 61 62 2c 20 74 6d 61 73 6b 29 20 29 7b 0d 0a  Tab, tmask) ){..
31ca0 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
31ca1 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 0d  cleanup;..  }...
31ca2 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
31ca3 20 56 44 42 45 0d 0a 20 20 2a 2f 0d 0a 20 20 76   VDBE..  */..  v
31ca4 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
31ca5 65 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 69 66  e(pParse);..  if
31ca6 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e  ( v==0 ) goto in
31ca7 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  sert_cleanup;.. 
31ca8 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
31ca9 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33  ted==0 ) sqlite3
31caa 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73  VdbeCountChanges
31cab 28 76 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42  (v);..  sqlite3B
31cac 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
31cad 6f 6e 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  on(pParse, pSele
31cae 63 74 20 7c 7c 20 70 54 72 69 67 67 65 72 2c 20  ct || pTrigger, 
31caf 69 44 62 29 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66  iDb);....#ifndef
31cb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45   SQLITE_OMIT_XFE
31cb1 52 5f 4f 50 54 0d 0a 20 20 2f 2a 20 49 66 20 74  R_OPT..  /* If t
31cb2 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
31cb3 6f 66 20 74 68 65 20 66 6f 72 6d 0d 0a 20 20 2a  of the form..  *
31cb4 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e  *..  **       IN
31cb5 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
31cb6 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  1> SELECT * FROM
31cb7 20 3c 74 61 62 6c 65 32 3e 3b 0d 0a 20 20 2a 2a   <table2>;..  **
31cb8 0d 0a 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63  ..  ** Then spec
31cb9 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ial optimization
31cba 73 20 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64  s can be applied
31cbb 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74   that make the t
31cbc 72 61 6e 73 66 65 72 0d 0a 20 20 2a 2a 20 76 65  ransfer..  ** ve
31cbd 72 79 20 66 61 73 74 20 61 6e 64 20 77 68 69 63  ry fast and whic
31cbe 68 20 72 65 64 75 63 65 20 66 72 61 67 6d 65 6e  h reduce fragmen
31cbf 74 61 74 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65  tation of indice
31cc0 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54  s...  **..  ** T
31cc1 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
31cc2 65 6d 70 6c 61 74 65 2e 0d 0a 20 20 2a 2f 0d 0a  emplate...  */..
31cc3 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30    if( pColumn==0
31cc4 20 26 26 20 78 66 65 72 4f 70 74 69 6d 69 7a 61   && xferOptimiza
31cc5 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61  tion(pParse, pTa
31cc6 62 2c 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45 72  b, pSelect, onEr
31cc7 72 6f 72 2c 20 69 44 62 29 20 29 7b 0d 0a 20 20  ror, iDb) ){..  
31cc8 20 20 61 73 73 65 72 74 28 20 21 70 54 72 69 67    assert( !pTrig
31cc9 67 65 72 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ger );..    asse
31cca 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0d  rt( pList==0 );.
31ccb 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
31ccc 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  _end;..  }..#end
31ccd 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
31cce 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0d 0a 0d  T_XFER_OPT */...
31ccf 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
31cd0 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   an AUTOINCREMEN
31cd1 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70  T table, look up
31cd2 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75   the sequence nu
31cd3 6d 62 65 72 20 69 6e 20 74 68 65 0d 0a 20 20 2a  mber in the..  *
31cd4 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  * sqlite_sequenc
31cd5 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  e table and stor
31cd6 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  e it in memory c
31cd7 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e 0d  ell regAutoinc..
31cd8 0a 20 20 2a 2f 0d 0a 20 20 72 65 67 41 75 74 6f  .  */..  regAuto
31cd9 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67  inc = autoIncBeg
31cda 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  in(pParse, iDb, 
31cdb 70 54 61 62 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 46  pTab);....  /* F
31cdc 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
31cdd 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61  ny columns of da
31cde 74 61 20 61 72 65 20 73 75 70 70 6c 69 65 64 2e  ta are supplied.
31cdf 20 20 49 66 20 74 68 65 20 64 61 74 61 0d 0a 20    If the data.. 
31ce0 20 2a 2a 20 69 73 20 63 6f 6d 69 6e 67 20 66 72   ** is coming fr
31ce1 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  om a SELECT stat
31ce2 65 6d 65 6e 74 2c 20 74 68 65 6e 20 67 65 6e 65  ement, then gene
31ce3 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e  rate a co-routin
31ce4 65 20 74 68 61 74 0d 0a 20 20 2a 2a 20 70 72 6f  e that..  ** pro
31ce5 64 75 63 65 73 20 61 20 73 69 6e 67 6c 65 20 72  duces a single r
31ce6 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ow of the SELECT
31ce7 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74   on each invocat
31ce8 69 6f 6e 2e 20 20 54 68 65 0d 0a 20 20 2a 2a 20  ion.  The..  ** 
31ce9 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20 74 68  co-routine is th
31cea 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20  e common header 
31ceb 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64 20 34  to the 3rd and 4
31cec 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0d 0a 20  th templates... 
31ced 20 2a 2f 0d 0a 20 20 69 66 28 20 70 53 65 6c 65   */..  if( pSele
31cee 63 74 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 44 61  ct ){..    /* Da
31cef 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f  ta is coming fro
31cf0 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47 65 6e  m a SELECT.  Gen
31cf1 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d  erate code to im
31cf2 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 45 4c  plement that SEL
31cf3 45 43 54 0d 0a 20 20 20 20 2a 2a 20 61 73 20 61  ECT..    ** as a
31cf4 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 68   co-routine.  Th
31cf5 65 20 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f 6e  e code is common
31cf6 20 74 6f 20 62 6f 74 68 20 74 68 65 20 33 72 64   to both the 3rd
31cf7 20 61 6e 64 20 34 74 68 0d 0a 20 20 20 20 2a 2a   and 4th..    **
31cf8 20 74 65 6d 70 6c 61 74 65 73 3a 0d 0a 20 20 20   templates:..   
31cf9 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20   **..    **     
31cfa 20 20 20 20 45 4f 46 20 3c 2d 20 30 0d 0a 20 20      EOF <- 0..  
31cfb 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c    **         X <
31cfc 2d 20 41 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20  - A..    **     
31cfd 20 20 20 20 67 6f 74 6f 20 42 0d 0a 20 20 20 20      goto B..    
31cfe 2a 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70  **      A: setup
31cff 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0d   for the SELECT.
31d00 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
31d01 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61  loop over the ta
31d02 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  bles in the SELE
31d03 43 54 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  CT..    **      
31d04 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20       load value 
31d05 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e  into register R.
31d06 2e 52 2b 6e 0d 0a 20 20 20 20 2a 2a 20 20 20 20  .R+n..    **    
31d07 20 20 20 20 20 20 20 79 69 65 6c 64 20 58 0d 0a         yield X..
31d08 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65      **         e
31d09 6e 64 20 6c 6f 6f 70 0d 0a 20 20 20 20 2a 2a 20  nd loop..    ** 
31d0a 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20          cleanup 
31d0b 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54  after the SELECT
31d0c 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
31d0d 20 45 4f 46 20 3c 2d 20 31 0d 0a 20 20 20 20 2a   EOF <- 1..    *
31d0e 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20  *         yield 
31d0f 58 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  X..    **       
31d10 20 20 68 61 6c 74 2d 65 72 72 6f 72 0d 0a 20 20    halt-error..  
31d11 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 4f 6e 20    **..    ** On 
31d12 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 20  each invocation 
31d13 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e  of the co-routin
31d14 65 2c 20 69 74 20 70 75 74 73 20 61 20 73 69 6e  e, it puts a sin
31d15 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0d 0a  gle row of the..
31d16 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 72 65      ** SELECT re
31d17 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
31d18 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e  ers dest.iMem...
31d19 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e  dest.iMem+dest.n
31d1a 4d 65 6d 2d 31 2e 0d 0a 20 20 20 20 2a 2a 20 28  Mem-1...    ** (
31d1b 54 68 65 73 65 20 6f 75 74 70 75 74 20 72 65 67  These output reg
31d1c 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63  isters are alloc
31d1d 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 53  ated by sqlite3S
31d1e 65 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e 0d  elect().)  When.
31d1f 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45  .    ** the SELE
31d20 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74  CT completes, it
31d21 20 73 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c   sets the EOF fl
31d22 61 67 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  ag stored in reg
31d23 45 6f 66 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  Eof...    */..  
31d24 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0d 0a 0d    int rc, j1;...
31d25 0a 20 20 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b  .    regEof = ++
31d26 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20  pParse->nMem;.. 
31d27 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
31d28 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
31d29 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 29 3b 20  er, 0, regEof); 
31d2a 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30       /* EOF <- 0
31d2b 20 2a 2f 0d 0a 20 20 20 20 56 64 62 65 43 6f 6d   */..    VdbeCom
31d2c 6d 65 6e 74 28 28 76 2c 20 22 53 45 4c 45 43 54  ment((v, "SELECT
31d2d 20 65 6f 66 20 66 6c 61 67 22 29 29 3b 0d 0a 20   eof flag"));.. 
31d2e 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
31d2f 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
31d30 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 2b  SRT_Coroutine, +
31d31 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0d  +pParse->nMem);.
31d32 0a 20 20 20 20 61 64 64 72 53 65 6c 65 63 74 20  .    addrSelect 
31d33 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
31d34 72 65 6e 74 41 64 64 72 28 76 29 2b 32 3b 0d 0a  rentAddr(v)+2;..
31d35 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31d36 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
31d37 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63 74 2d  ger, addrSelect-
31d38 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0d  1, dest.iParm);.
31d39 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
31d3a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
31d3b 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0d 0a  P_Goto, 0, 0);..
31d3c 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
31d3d 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53  (v, "Jump over S
31d3e 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22  ELECT coroutine"
31d3f 29 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65  ));....    /* Re
31d40 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 73  solve the expres
31d41 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c  sions in the SEL
31d42 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  ECT statement an
31d43 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a 2f  d execute it. */
31d44 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
31d45 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
31d46 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
31d47 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
31d48 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c  Parse->nErr==0 |
31d49 7c 20 72 63 20 29 3b 0d 0a 20 20 20 20 69 66 28  | rc );..    if(
31d4a 20 72 63 20 7c 7c 20 4e 45 56 45 52 28 70 50 61   rc || NEVER(pPa
31d4b 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 64 62  rse->nErr) || db
31d4c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
31d4d 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  {..      goto in
31d4e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  sert_cleanup;.. 
31d4f 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
31d50 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
31d51 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65  P_Integer, 1, re
31d52 67 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f  gEof);         /
31d53 2a 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0d 0a 20  * EOF <- 1 */.. 
31d54 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
31d55 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
31d56 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20  , dest.iParm);  
31d57 20 2f 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0d 0a   /* yield X */..
31d58 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31d59 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp2(v, OP_Halt
31d5a 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41  , SQLITE_INTERNA
31d5b 4c 2c 20 4f 45 5f 41 62 6f 72 74 29 3b 0d 0a 20  L, OE_Abort);.. 
31d5c 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
31d5d 76 2c 20 22 45 6e 64 20 6f 66 20 53 45 4c 45 43  v, "End of SELEC
31d5e 54 20 63 6f 72 6f 75 74 69 6e 65 22 29 29 3b 0d  T coroutine"));.
31d5f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
31d60 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
31d61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 61 62            /* lab
31d63 65 6c 20 42 3a 20 2a 2f 0d 0a 0d 0a 20 20 20 20  el B: */....    
31d64 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20  regFromSelect = 
31d65 64 65 73 74 2e 69 4d 65 6d 3b 0d 0a 20 20 20 20  dest.iMem;..    
31d66 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d  assert( pSelect-
31d67 3e 70 45 4c 69 73 74 20 29 3b 0d 0a 20 20 20 20  >pEList );..    
31d68 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63  nColumn = pSelec
31d69 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t->pEList->nExpr
31d6a 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ;..    assert( d
31d6b 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c 75 6d  est.nMem==nColum
31d6c 6e 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53  n );....    /* S
31d6d 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  et useTempTable 
31d6e 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 72  to TRUE if the r
31d6f 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c  esult of the SEL
31d70 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0d 0a 20  ECT statement.. 
31d71 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20     ** should be 
31d72 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74  written into a t
31d73 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28  emporary table (
31d74 74 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53 65  template 4).  Se
31d75 74 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 46 41 4c  t to..    ** FAL
31d76 53 45 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20  SE if each* row 
31d77 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61  of the SELECT ca
31d78 6e 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72  n be written dir
31d79 65 63 74 6c 79 20 69 6e 74 6f 0d 0a 20 20 20 20  ectly into..    
31d7a 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  ** the destinati
31d7b 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61  on table (templa
31d7c 74 65 20 33 29 2e 0d 0a 20 20 20 20 2a 2a 0d 0a  te 3)...    **..
31d7d 20 20 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61      ** A temp ta
31d7e 62 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64  ble must be used
31d7f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
31d80 69 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 61  ing updated is a
31d81 6c 73 6f 20 6f 6e 65 0d 0a 20 20 20 20 2a 2a 20  lso one..    ** 
31d82 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 62 65  of the tables be
31d83 69 6e 67 20 72 65 61 64 20 62 79 20 74 68 65 20  ing read by the 
31d84 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
31d85 2e 20 20 41 6c 73 6f 20 75 73 65 20 61 20 0d 0a  .  Also use a ..
31d86 20 20 20 20 2a 2a 20 74 65 6d 70 20 74 61 62 6c      ** temp tabl
31d87 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66  e in the case of
31d88 20 72 6f 77 20 74 72 69 67 67 65 72 73 2e 0d 0a   row triggers...
31d89 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
31d8a 70 54 72 69 67 67 65 72 20 7c 7c 20 72 65 61 64  pTrigger || read
31d8b 73 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 61  sTable(pParse, a
31d8c 64 64 72 53 65 6c 65 63 74 2c 20 69 44 62 2c 20  ddrSelect, iDb, 
31d8d 70 54 61 62 29 20 29 7b 0d 0a 20 20 20 20 20 20  pTab) ){..      
31d8e 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20 31  useTempTable = 1
31d8f 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
31d90 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
31d91 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 6e   ){..      /* In
31d92 76 6f 6b 65 20 74 68 65 20 63 6f 72 6f 75 74 69  voke the corouti
31d93 6e 65 20 74 6f 20 65 78 74 72 61 63 74 20 69 6e  ne to extract in
31d94 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
31d95 68 65 20 53 45 4c 45 43 54 0d 0a 20 20 20 20 20  he SELECT..     
31d96 20 2a 2a 20 61 6e 64 20 61 64 64 20 69 74 20 74   ** and add it t
31d97 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61  o a transient ta
31d98 62 6c 65 20 73 72 63 54 61 62 2e 20 20 54 68 65  ble srcTab.  The
31d99 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0d   code generated.
31d9a 0a 20 20 20 20 20 20 2a 2a 20 68 65 72 65 20 69  .      ** here i
31d9b 73 20 66 72 6f 6d 20 74 68 65 20 34 74 68 20 74  s from the 4th t
31d9c 65 6d 70 6c 61 74 65 3a 0d 0a 20 20 20 20 20 20  emplate:..      
31d9d 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  **..      **    
31d9e 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74    B: open temp t
31d9f 61 62 6c 65 0d 0a 20 20 20 20 20 20 2a 2a 20 20  able..      **  
31da0 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0d 0a      L: yield X..
31da1 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
31da2 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0d 0a   if EOF goto M..
31da3 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
31da4 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d   insert row from
31da5 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d   R..R+n into tem
31da6 70 20 74 61 62 6c 65 0d 0a 20 20 20 20 20 20 2a  p table..      *
31da7 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c  *         goto L
31da8 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  ..      **      
31da9 4d 3a 20 2e 2e 2e 0d 0a 20 20 20 20 20 20 2a 2f  M: .....      */
31daa 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52  ..      int regR
31dab 65 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ec;          /* 
31dac 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64  Register to hold
31dad 20 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 2a   packed record *
31dae 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67  /..      int reg
31daf 54 65 6d 70 52 6f 77 69 64 3b 20 20 20 20 2f 2a  TempRowid;    /*
31db0 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c   Register to hol
31db1 64 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f 57  d temp table ROW
31db2 49 44 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74  ID */..      int
31db3 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20   addrTop;       
31db4 20 20 2f 2a 20 4c 61 62 65 6c 20 22 4c 22 20 2a    /* Label "L" *
31db5 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  /..      int add
31db6 72 49 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rIf;          /*
31db7 20 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d 70   Address of jump
31db8 20 74 6f 20 4d 20 2a 2f 0d 0a 0d 0a 20 20 20 20   to M */....    
31db9 20 20 73 72 63 54 61 62 20 3d 20 70 50 61 72 73    srcTab = pPars
31dba 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20 20 20 20  e->nTab++;..    
31dbb 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74    regRec = sqlit
31dbc 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
31dbd 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 72 65 67  rse);..      reg
31dbe 54 65 6d 70 52 6f 77 69 64 20 3d 20 73 71 6c 69  TempRowid = sqli
31dbf 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
31dc0 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 73 71  arse);..      sq
31dc1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
31dc2 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
31dc3 72 61 6c 2c 20 73 72 63 54 61 62 2c 20 6e 43 6f  ral, srcTab, nCo
31dc4 6c 75 6d 6e 29 3b 0d 0a 20 20 20 20 20 20 61 64  lumn);..      ad
31dc5 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  drTop = sqlite3V
31dc6 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
31dc7 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
31dc8 6d 29 3b 0d 0a 20 20 20 20 20 20 61 64 64 72 49  m);..      addrI
31dc9 66 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  f = sqlite3VdbeA
31dca 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
31dcb 72 65 67 45 6f 66 29 3b 0d 0a 20 20 20 20 20 20  regEof);..      
31dcc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
31dcd 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
31dce 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63  rd, regFromSelec
31dcf 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52  t, nColumn, regR
31dd0 65 63 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ec);..      sqli
31dd1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
31dd2 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72   OP_NewRowid, sr
31dd3 63 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f 77  cTab, regTempRow
31dd4 69 64 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  id);..      sqli
31dd5 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
31dd6 20 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54   OP_Insert, srcT
31dd7 61 62 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54  ab, regRec, regT
31dd8 65 6d 70 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20  empRowid);..    
31dd9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
31dda 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
31ddb 30 2c 20 61 64 64 72 54 6f 70 29 3b 0d 0a 20 20  0, addrTop);..  
31ddc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
31ddd 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 49  umpHere(v, addrI
31dde 66 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  f);..      sqlit
31ddf 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
31de0 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 29  (pParse, regRec)
31de1 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
31de2 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
31de3 50 61 72 73 65 2c 20 72 65 67 54 65 6d 70 52 6f  Parse, regTempRo
31de4 77 69 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  wid);..    }..  
31de5 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54  }else{..    /* T
31de6 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20  his is the case 
31de7 69 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  if the data for 
31de8 74 68 65 20 49 4e 53 45 52 54 20 69 73 20 63 6f  the INSERT is co
31de9 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
31dea 45 53 0d 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73  ES..    ** claus
31deb 65 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 4e  e..    */..    N
31dec 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0d  ameContext sNC;.
31ded 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  .    memset(&sNC
31dee 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
31def 29 3b 0d 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  );..    sNC.pPar
31df0 73 65 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20  se = pParse;..  
31df1 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0d 0a    srcTab = -1;..
31df2 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65 54      assert( useT
31df3 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a  empTable==0 );..
31df4 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c      nColumn = pL
31df5 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78  ist ? pList->nEx
31df6 70 72 20 3a 20 30 3b 0d 0a 20 20 20 20 66 6f 72  pr : 0;..    for
31df7 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
31df8 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66   i++){..      if
31df9 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
31dfa 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
31dfb 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
31dfc 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67  r) ){..        g
31dfd 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
31dfe 75 70 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  up;..      }..  
31dff 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
31e00 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
31e01 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
31e02 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
31e03 61 74 61 20 6d 61 74 63 68 65 73 20 74 68 65 20  ata matches the 
31e04 6e 75 6d 62 65 72 0d 0a 20 20 2a 2a 20 6f 66 20  number..  ** of 
31e05 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e  columns to be in
31e06 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
31e07 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  table...  */..  
31e08 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
31e09 61 62 29 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28  ab) ){..    for(
31e0a 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
31e0b 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  l; i++){..      
31e0c 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73 48 69  nHidden += (IsHi
31e0d 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62  ddenColumn(&pTab
31e0e 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20 3a  ->aCol[i]) ? 1 :
31e0f 20 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d   0);..    }..  }
31e10 0d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  ..  if( pColumn=
31e11 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26  =0 && nColumn &&
31e12 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d   nColumn!=(pTab-
31e13 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29  >nCol-nHidden) )
31e14 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  {..    sqlite3Er
31e15 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0d  rorMsg(pParse, .
31e16 0a 20 20 20 20 20 20 20 22 74 61 62 6c 65 20 25  .       "table %
31e17 53 20 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73  S has %d columns
31e18 20 62 75 74 20 25 64 20 76 61 6c 75 65 73 20 77   but %d values w
31e19 65 72 65 20 73 75 70 70 6c 69 65 64 22 2c 0d 0a  ere supplied",..
31e1a 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c         pTabList,
31e1b 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e   0, pTab->nCol-n
31e1c 48 69 64 64 65 6e 2c 20 6e 43 6f 6c 75 6d 6e 29  Hidden, nColumn)
31e1d 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ;..    goto inse
31e1e 72 74 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  rt_cleanup;..  }
31e1f 0d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 21  ..  if( pColumn!
31e20 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 70  =0 && nColumn!=p
31e21 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0d 0a  Column->nId ){..
31e22 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
31e23 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64 20  Msg(pParse, "%d 
31e24 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63 6f  values for %d co
31e25 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e 2c  lumns", nColumn,
31e26 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b 0d   pColumn->nId);.
31e27 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
31e28 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a  _cleanup;..  }..
31e29 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 49 4e  ..  /* If the IN
31e2a 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 20 69  SERT statement i
31e2b 6e 63 6c 75 64 65 64 20 61 6e 20 49 44 4c 49 53  ncluded an IDLIS
31e2c 54 20 74 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b  T term, then mak
31e2d 65 20 73 75 72 65 0d 0a 20 20 2a 2a 20 61 6c 6c  e sure..  ** all
31e2e 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
31e2f 20 49 44 4c 49 53 54 20 72 65 61 6c 6c 79 20 61   IDLIST really a
31e30 72 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  re columns of th
31e31 65 20 74 61 62 6c 65 20 61 6e 64 20 0d 0a 20 20  e table and ..  
31e32 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20  ** remember the 
31e33 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e 0d  column indices..
31e34 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20 74  .  **..  ** If t
31e35 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
31e36 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
31e37 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74  KEY column and t
31e38 68 61 74 20 63 6f 6c 75 6d 6e 0d 0a 20 20 2a 2a  hat column..  **
31e39 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65   is named in the
31e3a 20 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65   IDLIST, then re
31e3b 63 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43  cord in the keyC
31e3c 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0d 0a  olumn variable..
31e3d 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69    ** the index i
31e3e 6e 74 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68  nto IDLIST of th
31e3f 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f  e primary key co
31e40 6c 75 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e  lumn.  keyColumn
31e41 20 69 73 0d 0a 20 20 2a 2a 20 74 68 65 20 69 6e   is..  ** the in
31e42 64 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61  dex of the prima
31e43 72 79 20 6b 65 79 20 61 73 20 69 74 20 61 70 70  ry key as it app
31e44 65 61 72 73 20 69 6e 20 49 44 4c 49 53 54 2c 20  ears in IDLIST, 
31e45 6e 6f 74 20 61 73 0d 0a 20 20 2a 2a 20 69 73 20  not as..  ** is 
31e46 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 6f  appears in the o
31e47 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 2e 20 20  riginal table.  
31e48 28 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68  (The index of th
31e49 65 20 70 72 69 6d 61 72 79 0d 0a 20 20 2a 2a 20  e primary..  ** 
31e4a 6b 65 79 20 69 6e 20 74 68 65 20 6f 72 69 67 69  key in the origi
31e4b 6e 61 6c 20 74 61 62 6c 65 20 69 73 20 70 54 61  nal table is pTa
31e4c 62 2d 3e 69 50 4b 65 79 2e 29 0d 0a 20 20 2a 2f  b->iPKey.)..  */
31e4d 0d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20  ..  if( pColumn 
31e4e 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ){..    for(i=0;
31e4f 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   i<pColumn->nId;
31e50 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 70 43   i++){..      pC
31e51 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
31e52 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  = -1;..    }..  
31e53 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f    for(i=0; i<pCo
31e54 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b  lumn->nId; i++){
31e55 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ..      for(j=0;
31e56 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
31e57 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66  ++){..        if
31e58 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
31e59 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a  (pColumn->a[i].z
31e5a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  Name, pTab->aCol
31e5b 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [j].zName)==0 ){
31e5c 0d 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  ..          pCol
31e5d 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20  umn->a[i].idx = 
31e5e 6a 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  j;..          if
31e5f 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79  ( j==pTab->iPKey
31e60 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
31e61 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0d   keyColumn = i;.
31e62 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
31e63 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
31e64 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
31e65 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e   }..      if( j>
31e66 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0d 0a  =pTab->nCol ){..
31e67 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
31e68 74 65 33 49 73 52 6f 77 69 64 28 70 43 6f 6c 75  te3IsRowid(pColu
31e69 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20  mn->a[i].zName) 
31e6a 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6b 65  ){..          ke
31e6b 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0d 0a 20 20  yColumn = i;..  
31e6c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
31e6d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
31e6e 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
31e6f 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
31e70 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
31e71 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ",..            
31e72 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70    pTabList, 0, p
31e73 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61  Column->a[i].zNa
31e74 6d 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  me);..          
31e75 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
31e76 65 6d 61 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  ema = 1;..      
31e77 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
31e78 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20 20 20 20  cleanup;..      
31e79 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
31e7a 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
31e7b 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
31e7c 49 44 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20  IDLIST term but 
31e7d 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
31e7e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79   integer primary
31e7f 0d 0a 20 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20  ..  ** key, the 
31e80 73 65 74 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d  set the keyColum
31e81 6e 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68  n variable to th
31e82 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f  e primary key co
31e83 6c 75 6d 6e 20 69 6e 64 65 78 0d 0a 20 20 2a 2a  lumn index..  **
31e84 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
31e85 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   table definitio
31e86 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  n...  */..  if( 
31e87 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
31e88 6f 6c 75 6d 6e 3e 30 20 29 7b 0d 0a 20 20 20 20  olumn>0 ){..    
31e89 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62  keyColumn = pTab
31e8a 2d 3e 69 50 4b 65 79 3b 0d 0a 20 20 7d 0d 0a 20  ->iPKey;..  }.. 
31e8b 20 20 20 0d 0a 20 20 2f 2a 20 49 6e 69 74 69 61     ..  /* Initia
31e8c 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f  lize the count o
31e8d 66 20 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73  f rows to be ins
31e8e 65 72 74 65 64 0d 0a 20 20 2a 2f 0d 0a 20 20 69  erted..  */..  i
31e8f 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
31e90 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
31e91 29 7b 0d 0a 20 20 20 20 72 65 67 52 6f 77 43 6f  ){..    regRowCo
31e92 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  unt = ++pParse->
31e93 6e 4d 65 6d 3b 0d 0a 20 20 20 20 73 71 6c 69 74  nMem;..    sqlit
31e94 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
31e95 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
31e96 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0d 0a 20 20  egRowCount);..  
31e97 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69  }....  /* If thi
31e98 73 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c  s is not a view,
31e99 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20   open the table 
31e9a 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69  and and all indi
31e9b 63 65 73 20 2a 2f 0d 0a 20 20 69 66 28 20 21 69  ces */..  if( !i
31e9c 73 56 69 65 77 20 29 7b 0d 0a 20 20 20 20 69 6e  sView ){..    in
31e9d 74 20 6e 49 64 78 3b 0d 0a 0d 0a 20 20 20 20 62  t nIdx;....    b
31e9e 61 73 65 43 75 72 20 3d 20 70 50 61 72 73 65 2d  aseCur = pParse-
31e9f 3e 6e 54 61 62 3b 0d 0a 20 20 20 20 6e 49 64 78  >nTab;..    nIdx
31ea0 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61   = sqlite3OpenTa
31ea1 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50  bleAndIndices(pP
31ea2 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65  arse, pTab, base
31ea3 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  Cur, OP_OpenWrit
31ea4 65 29 3b 0d 0a 20 20 20 20 61 52 65 67 49 64 78  e);..    aRegIdx
31ea5 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
31ea6 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
31ea7 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29 3b  (int)*(nIdx+1));
31ea8 0d 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ..    if( aRegId
31ea9 78 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 67  x==0 ){..      g
31eaa 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
31eab 75 70 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  up;..    }..    
31eac 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b  for(i=0; i<nIdx;
31ead 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 61 52   i++){..      aR
31eae 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b 70 50 61  egIdx[i] = ++pPa
31eaf 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20  rse->nMem;..    
31eb0 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54  }..  }....  /* T
31eb1 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 20 6f  his is the top o
31eb2 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72  f the main inser
31eb3 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0d 0a 20 20  tion loop */..  
31eb4 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
31eb5 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73   ){..    /* This
31eb6 20 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65   block codes the
31eb7 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c   top of loop onl
31eb8 79 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65  y.  The complete
31eb9 20 6c 6f 6f 70 20 69 73 20 74 68 65 0d 0a 20 20   loop is the..  
31eba 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70    ** following p
31ebb 73 65 75 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c  seudocode (templ
31ebc 61 74 65 20 34 29 3a 0d 0a 20 20 20 20 2a 2a 0d  ate 4):..    **.
31ebd 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
31ebe 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c  rewind temp tabl
31ebf 65 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 43  e..    **      C
31ec0 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f 77 73  : loop over rows
31ec1 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61 74 65   of intermediate
31ec2 20 74 61 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 20   table..    **  
31ec3 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
31ec4 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e  r values form in
31ec5 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
31ec6 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0d 0a 20   into <table>.. 
31ec7 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e     **         en
31ec8 64 20 6c 6f 6f 70 0d 0a 20 20 20 20 2a 2a 20 20  d loop..    **  
31ec9 20 20 20 20 44 3a 20 2e 2e 2e 0d 0a 20 20 20 20      D: .....    
31eca 2a 2f 0d 0a 20 20 20 20 61 64 64 72 49 6e 73 54  */..    addrInsT
31ecb 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
31ecc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77  AddOp1(v, OP_Rew
31ecd 69 6e 64 2c 20 73 72 63 54 61 62 29 3b 0d 0a 20  ind, srcTab);.. 
31ece 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71     addrCont = sq
31ecf 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
31ed0 41 64 64 72 28 76 29 3b 0d 0a 20 20 7d 65 6c 73  Addr(v);..  }els
31ed1 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
31ed2 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c  ..    /* This bl
31ed3 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f  ock codes the to
31ed4 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20  p of loop only. 
31ed5 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f   The complete lo
31ed6 6f 70 20 69 73 20 74 68 65 0d 0a 20 20 20 20 2a  op is the..    *
31ed7 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75  * following pseu
31ed8 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65  docode (template
31ed9 20 33 29 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20   3):..    **..  
31eda 20 20 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65    **      C: yie
31edb 6c 64 20 58 0d 0a 20 20 20 20 2a 2a 20 20 20 20  ld X..    **    
31edc 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f       if EOF goto
31edd 20 44 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   D..    **      
31ede 20 20 20 69 6e 73 65 72 74 20 74 68 65 20 73 65     insert the se
31edf 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74 6f  lect result into
31ee0 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20 52 2e   <table> from R.
31ee1 2e 52 2b 6e 0d 0a 20 20 20 20 2a 2a 20 20 20 20  .R+n..    **    
31ee2 20 20 20 20 20 67 6f 74 6f 20 43 0d 0a 20 20 20       goto C..   
31ee3 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0d   **      D: ....
31ee4 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 64 64  .    */..    add
31ee5 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  rCont = sqlite3V
31ee6 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
31ee7 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
31ee8 6d 29 3b 0d 0a 20 20 20 20 61 64 64 72 49 6e 73  m);..    addrIns
31ee9 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
31eea 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
31eeb 2c 20 72 65 67 45 6f 66 29 3b 0d 0a 20 20 7d 0d  , regEof);..  }.
31eec 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ...  /* Allocate
31eed 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 68   registers for h
31eee 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64  olding the rowid
31eef 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c   of the new row,
31ef0 0d 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ..  ** the conte
31ef1 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f  nt of the new ro
31ef2 77 2c 20 61 6e 64 20 74 68 65 20 61 73 73 65 6d  w, and the assem
31ef3 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64  blied row record
31ef4 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 72 65 67 52 6f  ...  */..  regRo
31ef5 77 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70  wid = regIns = p
31ef6 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0d 0a  Parse->nMem+1;..
31ef7 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
31ef8 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31  = pTab->nCol + 1
31ef9 3b 0d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  ;..  if( IsVirtu
31efa 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20 20 20  al(pTab) ){..   
31efb 20 72 65 67 52 6f 77 69 64 2b 2b 3b 0d 0a 20 20   regRowid++;..  
31efc 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b    pParse->nMem++
31efd 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 67 44 61 74  ;..  }..  regDat
31efe 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 3b 0d  a = regRowid+1;.
31eff 0a 0d 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20  ...  /* Run the 
31f00 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45  BEFORE and INSTE
31f01 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20  AD OF triggers, 
31f02 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
31f03 0d 0a 20 20 2a 2f 0d 0a 20 20 65 6e 64 4f 66 4c  ..  */..  endOfL
31f04 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
31f05 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a  eMakeLabel(v);..
31f06 20 20 69 66 28 20 74 6d 61 73 6b 20 26 20 54 52    if( tmask & TR
31f07 49 47 47 45 52 5f 42 45 46 4f 52 45 20 29 7b 0d  IGGER_BEFORE ){.
31f08 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73  .    int regCols
31f09 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
31f0a 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
31f0b 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0d 0a 0d  Tab->nCol+1);...
31f0c 0a 20 20 20 20 2f 2a 20 62 75 69 6c 64 20 74 68  .    /* build th
31f0d 65 20 4e 45 57 2e 2a 20 72 65 66 65 72 65 6e 63  e NEW.* referenc
31f0e 65 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61  e row.  Note tha
31f0f 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  t if there is an
31f10 20 49 4e 54 45 47 45 52 0d 0a 20 20 20 20 2a 2a   INTEGER..    **
31f11 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 74   PRIMARY KEY int
31f12 6f 20 77 68 69 63 68 20 61 20 4e 55 4c 4c 20 69  o which a NULL i
31f13 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  s being inserted
31f14 2c 20 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c 6c  , that NULL will
31f15 20 62 65 0d 0a 20 20 20 20 2a 2a 20 74 72 61 6e   be..    ** tran
31f16 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 75 6e  slated into a un
31f17 69 71 75 65 20 49 44 20 66 6f 72 20 74 68 65 20  ique ID for the 
31f18 72 6f 77 2e 20 20 42 75 74 20 6f 6e 20 61 20 42  row.  But on a B
31f19 45 46 4f 52 45 20 74 72 69 67 67 65 72 2c 0d 0a  EFORE trigger,..
31f1a 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74      ** we do not
31f1b 20 6b 6e 6f 77 20 77 68 61 74 20 74 68 65 20 75   know what the u
31f1c 6e 69 71 75 65 20 49 44 20 77 69 6c 6c 20 62 65  nique ID will be
31f1d 20 28 62 65 63 61 75 73 65 20 74 68 65 20 69 6e   (because the in
31f1e 73 65 72 74 20 68 61 73 0d 0a 20 20 20 20 2a 2a  sert has..    **
31f1f 20 6e 6f 74 20 68 61 70 70 65 6e 65 64 20 79 65   not happened ye
31f20 74 29 20 73 6f 20 77 65 20 73 75 62 73 74 69 74  t) so we substit
31f21 75 74 65 20 61 20 72 6f 77 69 64 20 6f 66 20 2d  ute a rowid of -
31f22 31 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  1..    */..    i
31f23 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20 29  f( keyColumn<0 )
31f24 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
31f25 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
31f26 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65  _Integer, -1, re
31f27 67 43 6f 6c 73 29 3b 0d 0a 20 20 20 20 7d 65 6c  gCols);..    }el
31f28 73 65 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a  se{..      int j
31f29 31 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 75 73  1;..      if( us
31f2a 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0d 0a 20  eTempTable ){.. 
31f2b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
31f2c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
31f2d 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b  olumn, srcTab, k
31f2e 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c  eyColumn, regCol
31f2f 73 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  s);..      }else
31f30 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  {..        asser
31f31 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  t( pSelect==0 );
31f32 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75    /* Otherwise u
31f33 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74  seTempTable is t
31f34 72 75 65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  rue */..        
31f35 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
31f36 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
31f37 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  [keyColumn].pExp
31f38 72 2c 20 72 65 67 43 6f 6c 73 29 3b 0d 0a 20 20  r, regCols);..  
31f39 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 6a 31 20      }..      j1 
31f3a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
31f3b 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
31f3c 6c 2c 20 72 65 67 43 6f 6c 73 29 3b 0d 0a 20 20  l, regCols);..  
31f3d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31f3e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
31f3f 67 65 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73  ger, -1, regCols
31f40 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
31f41 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
31f42 20 6a 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c   j1);..      sql
31f43 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
31f44 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
31f45 72 65 67 43 6f 6c 73 29 3b 0d 0a 20 20 20 20 7d  regCols);..    }
31f46 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f  ....    /* Canno
31f47 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20  t have triggers 
31f48 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  on a virtual tab
31f49 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 70  le. If it were p
31f4a 6f 73 73 69 62 6c 65 2c 0d 0a 20 20 20 20 2a 2a  ossible,..    **
31f4b 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c   this block woul
31f4c 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e  d have to accoun
31f4d 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c  t for hidden col
31f4e 75 6d 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  umn...    */..  
31f4f 20 20 61 73 73 65 72 74 28 20 21 49 73 56 69 72    assert( !IsVir
31f50 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0d 0a 0d  tual(pTab) );...
31f51 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74  .    /* Create t
31f52 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61  he new column da
31f53 74 61 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ta..    */..    
31f54 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
31f55 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20  >nCol; i++){..  
31f56 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d      if( pColumn=
31f57 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 6a  =0 ){..        j
31f58 20 3d 20 69 3b 0d 0a 20 20 20 20 20 20 7d 65 6c   = i;..      }el
31f59 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72  se{..        for
31f5a 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d  (j=0; j<pColumn-
31f5b 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20  >nId; j++){..   
31f5c 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75         if( pColu
31f5d 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20  mn->a[j].idx==i 
31f5e 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
31f5f 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
31f60 20 20 20 20 69 66 28 20 28 21 75 73 65 54 65 6d      if( (!useTem
31f61 70 54 61 62 6c 65 20 26 26 20 21 70 4c 69 73 74  pTable && !pList
31f62 29 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26  ) || (pColumn &&
31f63 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64   j>=pColumn->nId
31f64 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  ) ){..        sq
31f65 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
31f66 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  arse, pTab->aCol
31f67 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 43 6f  [i].pDflt, regCo
31f68 6c 73 2b 69 2b 31 29 3b 0d 0a 20 20 20 20 20 20  ls+i+1);..      
31f69 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d  }else if( useTem
31f6a 70 54 61 62 6c 65 20 29 7b 0d 0a 20 20 20 20 20  pTable ){..     
31f6b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
31f6c 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
31f6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 72 65  n, srcTab, j, re
31f6e 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0d 0a 20 20  gCols+i+1); ..  
31f6f 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
31f70 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
31f71 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68  ect==0 ); /* Oth
31f72 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54 61  erwise useTempTa
31f73 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0d 0a  ble is true */..
31f74 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
31f75 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
31f76 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
31f77 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f  [j].pExpr, regCo
31f78 6c 73 2b 69 2b 31 29 3b 0d 0a 20 20 20 20 20 20  ls+i+1);..      
31f79 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  }..    }....    
31f7a 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
31f7b 20 49 4e 53 45 52 54 20 6f 6e 20 61 20 76 69 65   INSERT on a vie
31f7c 77 20 77 69 74 68 20 61 6e 20 49 4e 53 54 45 41  w with an INSTEA
31f7d 44 20 4f 46 20 49 4e 53 45 52 54 20 74 72 69 67  D OF INSERT trig
31f7e 67 65 72 2c 0d 0a 20 20 20 20 2a 2a 20 64 6f 20  ger,..    ** do 
31f7f 6e 6f 74 20 61 74 74 65 6d 70 74 20 61 6e 79 20  not attempt any 
31f80 63 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 66 6f  conversions befo
31f81 72 65 20 61 73 73 65 6d 62 6c 69 6e 67 20 74 68  re assembling th
31f82 65 20 72 65 63 6f 72 64 2e 0d 0a 20 20 20 20 2a  e record...    *
31f83 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72  * If this is a r
31f84 65 61 6c 20 74 61 62 6c 65 2c 20 61 74 74 65 6d  eal table, attem
31f85 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  pt conversions a
31f86 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  s required by th
31f87 65 0d 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20  e..    ** table 
31f88 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 69 65  column affinitie
31f89 73 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  s...    */..    
31f8a 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0d 0a  if( !isView ){..
31f8b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
31f8c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66  eAddOp2(v, OP_Af
31f8d 66 69 6e 69 74 79 2c 20 72 65 67 43 6f 6c 73 2b  finity, regCols+
31f8e 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0d  1, pTab->nCol);.
31f8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61  .      sqlite3Ta
31f90 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76  bleAffinityStr(v
31f91 2c 20 70 54 61 62 29 3b 0d 0a 20 20 20 20 7d 0d  , pTab);..    }.
31f92 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42  ...    /* Fire B
31f93 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44  EFORE or INSTEAD
31f94 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0d   OF triggers */.
31f95 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
31f96 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
31f97 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
31f98 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47 47  INSERT, 0, TRIGG
31f99 45 52 5f 42 45 46 4f 52 45 2c 20 0d 0a 20 20 20  ER_BEFORE, ..   
31f9a 20 20 20 20 20 70 54 61 62 2c 20 72 65 67 43 6f       pTab, regCo
31f9b 6c 73 2d 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c  ls-pTab->nCol-1,
31f9c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c   onError, endOfL
31f9d 6f 6f 70 29 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c  oop);....    sql
31f9e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
31f9f 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
31fa0 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Cols, pTab->nCol
31fa1 2b 31 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  +1);..  }....  /
31fa2 2a 20 50 75 73 68 20 74 68 65 20 72 65 63 6f 72  * Push the recor
31fa3 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  d number for the
31fa4 20 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20   new entry onto 
31fa5 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 0d  the stack.  The.
31fa6 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d  .  ** record num
31fa7 62 65 72 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c  ber is a randoml
31fa8 79 20 67 65 6e 65 72 61 74 65 20 69 6e 74 65 67  y generate integ
31fa9 65 72 20 63 72 65 61 74 65 64 20 62 79 20 4e 65  er created by Ne
31faa 77 52 6f 77 69 64 0d 0a 20 20 2a 2a 20 65 78 63  wRowid..  ** exc
31fab 65 70 74 20 77 68 65 6e 20 74 68 65 20 74 61 62  ept when the tab
31fac 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45  le has an INTEGE
31fad 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
31fae 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68 0d 0a  lumn, in which..
31faf 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 72 65    ** case the re
31fb0 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 74  cord number is t
31fb1 68 65 20 73 61 6d 65 20 61 73 20 74 68 61 74 20  he same as that 
31fb2 63 6f 6c 75 6d 6e 2e 20 0d 0a 20 20 2a 2f 0d 0a  column. ..  */..
31fb3 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
31fb4 0d 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  ..    if( IsVirt
31fb5 75 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20 20  ual(pTab) ){..  
31fb6 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 74      /* The row t
31fb7 68 61 74 20 74 68 65 20 56 55 70 64 61 74 65 20  hat the VUpdate 
31fb8 6f 70 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65  opcode will dele
31fb9 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0d 0a 20 20 20  te: none */..   
31fba 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
31fbb 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
31fbc 20 30 2c 20 72 65 67 49 6e 73 29 3b 0d 0a 20 20   0, regIns);..  
31fbd 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 6b 65 79    }..    if( key
31fbe 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0d 0a 20 20  Column>=0 ){..  
31fbf 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 54      if( useTempT
31fc0 61 62 6c 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  able ){..       
31fc1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
31fc2 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
31fc3 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75   srcTab, keyColu
31fc4 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a  mn, regRowid);..
31fc5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
31fc6 70 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20 20  pSelect ){..    
31fc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
31fc8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
31fc9 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  y, regFromSelect
31fca 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52  +keyColumn, regR
31fcb 6f 77 69 64 29 3b 0d 0a 20 20 20 20 20 20 7d 65  owid);..      }e
31fcc 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 56 64  lse{..        Vd
31fcd 62 65 4f 70 20 2a 70 4f 70 3b 0d 0a 20 20 20 20  beOp *pOp;..    
31fce 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
31fcf 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ode(pParse, pLis
31fd0 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e  t->a[keyColumn].
31fd1 70 45 78 70 72 2c 20 72 65 67 52 6f 77 69 64 29  pExpr, regRowid)
31fd2 3b 0d 0a 20 20 20 20 20 20 20 20 70 4f 70 20 3d  ;..        pOp =
31fd3 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f   sqlite3VdbeGetO
31fd4 70 28 76 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20  p(v, -1);..     
31fd5 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4f     if( ALWAYS(pO
31fd6 70 29 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64  p) && pOp->opcod
31fd7 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26 26 20 21 49  e==OP_Null && !I
31fd8 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
31fd9 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 70 70  {..          app
31fda 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0d 0a 20 20  endFlag = 1;..  
31fdb 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
31fdc 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77 69  ode = OP_NewRowi
31fdd 64 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4f  d;..          pO
31fde 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75 72 3b  p->p1 = baseCur;
31fdf 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ..          pOp-
31fe0 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b 0d  >p2 = regRowid;.
31fe1 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
31fe2 70 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b  p3 = regAutoinc;
31fe3 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
31fe4 20 20 20 7d 0d 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
31fe5 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
31fe6 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  Y expression is 
31fe7 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f  NULL, then use O
31fe8 50 5f 4e 65 77 52 6f 77 69 64 0d 0a 20 20 20 20  P_NewRowid..    
31fe9 20 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65    ** to generate
31fea 20 61 20 75 6e 69 71 75 65 20 70 72 69 6d 61 72   a unique primar
31feb 79 20 6b 65 79 20 76 61 6c 75 65 2e 0d 0a 20 20  y key value...  
31fec 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66      */..      if
31fed 28 20 21 61 70 70 65 6e 64 46 6c 61 67 20 29 7b  ( !appendFlag ){
31fee 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 31  ..        int j1
31fef 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ;..        if( !
31ff0 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
31ff1 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6a 31  ){..          j1
31ff2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
31ff3 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
31ff4 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a  ll, regRowid);..
31ff5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31ff6 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
31ff7 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65  P_NewRowid, base
31ff8 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72  Cur, regRowid, r
31ff9 65 67 41 75 74 6f 69 6e 63 29 3b 0d 0a 20 20 20  egAutoinc);..   
31ffa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
31ffb 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
31ffc 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  );..        }els
31ffd 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6a 31  e{..          j1
31ffe 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
31fff 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0d 0a 20  rrentAddr(v);.. 
32000 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32001 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
32002 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69  _IsNull, regRowi
32003 64 2c 20 6a 31 2b 32 29 3b 0d 0a 20 20 20 20 20  d, j1+2);..     
32004 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73 71     }..        sq
32005 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
32006 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
32007 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20   regRowid);..   
32008 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 20     }..    }else 
32009 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
3200a 61 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  ab) ){..      sq
3200b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3200c 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
3200d 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 7d  egRowid);..    }
3200e 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c  else{..      sql
3200f 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
32010 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62  , OP_NewRowid, b
32011 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64  aseCur, regRowid
32012 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0d 0a  , regAutoinc);..
32013 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67        appendFlag
32014 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 1;..    }..  
32015 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50    autoIncStep(pP
32016 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63  arse, regAutoinc
32017 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 0d 0a  , regRowid);....
32018 20 20 20 20 2f 2a 20 50 75 73 68 20 6f 6e 74 6f      /* Push onto
32019 20 74 68 65 20 73 74 61 63 6b 2c 20 64 61 74 61   the stack, data
3201a 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73   for all columns
3201b 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72   of the new entr
3201c 79 2c 20 62 65 67 69 6e 6e 69 6e 67 0d 0a 20 20  y, beginning..  
3201d 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 66 69    ** with the fi
3201e 72 73 74 20 63 6f 6c 75 6d 6e 2e 0d 0a 20 20 20  rst column...   
3201f 20 2a 2f 0d 0a 20 20 20 20 6e 48 69 64 64 65 6e   */..    nHidden
32020 20 3d 20 30 3b 0d 0a 20 20 20 20 66 6f 72 28 69   = 0;..    for(i
32021 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
32022 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69  ; i++){..      i
32023 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20 72  nt iRegStore = r
32024 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0d 0a 20 20  egRowid+1+i;..  
32025 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
32026 3e 69 50 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20  >iPKey ){..     
32027 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
32028 6f 66 20 74 68 65 20 49 4e 54 45 47 45 52 20 50  of the INTEGER P
32029 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
3202a 6e 20 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55  n is always a NU
3202b 4c 4c 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  LL...        ** 
3202c 57 68 65 6e 65 76 65 72 20 74 68 69 73 20 63 6f  Whenever this co
3202d 6c 75 6d 6e 20 69 73 20 72 65 61 64 2c 20 74 68  lumn is read, th
3202e 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
3202f 77 69 6c 6c 20 62 65 20 73 75 62 73 74 69 74 75  will be substitu
32030 74 65 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ted..        ** 
32031 69 6e 20 69 74 73 20 70 6c 61 63 65 2e 20 20 53  in its place.  S
32032 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73  o will fill this
32033 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e   column with a N
32034 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0d 0a 20 20  ULL to avoid..  
32035 20 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e 67 20        ** taking 
32036 75 70 20 64 61 74 61 20 73 70 61 63 65 20 77 69  up data space wi
32037 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  th information t
32038 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  hat will never b
32039 65 20 75 73 65 64 2e 20 2a 2f 0d 0a 20 20 20 20  e used. */..    
3203a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3203b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
3203c 2c 20 30 2c 20 69 52 65 67 53 74 6f 72 65 29 3b  , 0, iRegStore);
3203d 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ..        contin
3203e 75 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ue;..      }..  
3203f 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d      if( pColumn=
32040 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  =0 ){..        i
32041 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  f( IsHiddenColum
32042 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  n(&pTab->aCol[i]
32043 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) ){..          
32044 61 73 73 65 72 74 28 20 49 73 56 69 72 74 75 61  assert( IsVirtua
32045 6c 28 70 54 61 62 29 20 29 3b 0d 0a 20 20 20 20  l(pTab) );..    
32046 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0d 0a 20        j = -1;.. 
32047 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e           nHidden
32048 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  ++;..        }el
32049 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6a  se{..          j
3204a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b 0d   = i - nHidden;.
3204b 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
3204c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
3204d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f    for(j=0; j<pCo
3204e 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b  lumn->nId; j++){
3204f 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
32050 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64  pColumn->a[j].id
32051 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0d 0a 20  x==i ) break;.. 
32052 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
32053 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30  }..      if( j<0
32054 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c   || nColumn==0 |
32055 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e  | (pColumn && j>
32056 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29  =pColumn->nId) )
32057 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
32058 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
32059 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
3205a 2e 70 44 66 6c 74 2c 20 69 52 65 67 53 74 6f 72  .pDflt, iRegStor
3205b 65 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  e);..      }else
3205c 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
3205d 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  e ){..        sq
3205e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
3205f 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
32060 63 54 61 62 2c 20 6a 2c 20 69 52 65 67 53 74 6f  cTab, j, iRegSto
32061 72 65 29 3b 20 0d 0a 20 20 20 20 20 20 7d 65 6c  re); ..      }el
32062 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
32063 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
32064 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
32065 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f  OP_SCopy, regFro
32066 6d 53 65 6c 65 63 74 2b 6a 2c 20 69 52 65 67 53  mSelect+j, iRegS
32067 74 6f 72 65 29 3b 0d 0a 20 20 20 20 20 20 7d 65  tore);..      }e
32068 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
32069 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
3206a 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a  arse, pList->a[j
3206b 5d 2e 70 45 78 70 72 2c 20 69 52 65 67 53 74 6f  ].pExpr, iRegSto
3206c 72 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  re);..      }.. 
3206d 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47     }....    /* G
3206e 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
3206f 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
32070 73 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69  s and generate i
32071 6e 64 65 78 20 6b 65 79 73 20 61 6e 64 0d 0a 20  ndex keys and.. 
32072 20 20 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73     ** do the ins
32073 65 72 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a 2f 0d  ertion...    */.
32074 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
32075 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
32076 45 0d 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  E..    if( IsVir
32077 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20  tual(pTab) ){.. 
32078 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
32079 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20  *pVTab = (const 
3207a 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65  char *)sqlite3Ge
3207b 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
3207c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
3207d 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
3207e 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
3207f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
32080 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
32081 56 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61 62  VUpdate, 1, pTab
32082 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e 73  ->nCol+2, regIns
32083 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42  , pVTab, P4_VTAB
32084 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
32085 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
32086 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
32087 61 75 6c 74 20 3f 20 4f 45 5f 41 62 6f 72 74 20  ault ? OE_Abort 
32088 3a 20 6f 6e 45 72 72 6f 72 29 3b 0d 0a 20 20 20  : onError);..   
32089 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f     sqlite3MayAbo
3208a 72 74 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20  rt(pParse);..   
3208b 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a   }else..#endif..
3208c 20 20 20 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74      {..      int
3208d 20 69 73 52 65 70 6c 61 63 65 3b 20 20 20 20 2f   isReplace;    /
3208e 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  * Set to true if
3208f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6d 61 79   constraints may
32090 20 63 61 75 73 65 20 61 20 72 65 70 6c 61 63 65   cause a replace
32091 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   */..      sqlit
32092 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
32093 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73  aintChecks(pPars
32094 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72  e, pTab, baseCur
32095 2c 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64  , regIns, aRegId
32096 78 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 6b 65  x,..          ke
32097 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 20 30 2c 20 6f  yColumn>=0, 0, o
32098 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
32099 70 2c 20 26 69 73 52 65 70 6c 61 63 65 0d 0a 20  p, &isReplace.. 
3209a 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 73       );..      s
3209b 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 70 50  qlite3FkCheck(pP
3209c 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20 72  arse, pTab, 0, r
3209d 65 67 49 6e 73 29 3b 0d 0a 20 20 20 20 20 20 73  egIns);..      s
3209e 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e  qlite3CompleteIn
3209f 73 65 72 74 69 6f 6e 28 0d 0a 20 20 20 20 20 20  sertion(..      
320a0 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62      pParse, pTab
320a1 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e  , baseCur, regIn
320a2 73 2c 20 61 52 65 67 49 64 78 2c 20 30 2c 20 61  s, aRegIdx, 0, a
320a3 70 70 65 6e 64 46 6c 61 67 2c 20 69 73 52 65 70  ppendFlag, isRep
320a4 6c 61 63 65 3d 3d 30 0d 0a 20 20 20 20 20 20 29  lace==0..      )
320a5 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
320a6 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
320a7 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74   count of rows t
320a8 68 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64  hat are inserted
320a9 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 64  ..  */..  if( (d
320aa 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
320ab 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20  E_CountRows)!=0 
320ac 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ){..    sqlite3V
320ad 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
320ae 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
320af 75 6e 74 2c 20 31 29 3b 0d 0a 20 20 7d 0d 0a 0d  unt, 1);..  }...
320b0 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
320b1 29 7b 0d 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  ){..    /* Code 
320b2 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 2a  AFTER triggers *
320b3 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  /..    sqlite3Co
320b4 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
320b5 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54  rse, pTrigger, T
320b6 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49  K_INSERT, 0, TRI
320b7 47 47 45 52 5f 41 46 54 45 52 2c 20 0d 0a 20 20  GGER_AFTER, ..  
320b8 20 20 20 20 20 20 70 54 61 62 2c 20 72 65 67 44        pTab, regD
320b9 61 74 61 2d 32 2d 70 54 61 62 2d 3e 6e 43 6f 6c  ata-2-pTab->nCol
320ba 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66  , onError, endOf
320bb 4c 6f 6f 70 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Loop);..  }.... 
320bc 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f   /* The bottom o
320bd 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72  f the main inser
320be 74 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68  tion loop, if th
320bf 65 20 64 61 74 61 20 73 6f 75 72 63 65 0d 0a 20  e data source.. 
320c0 20 2a 2a 20 69 73 20 61 20 53 45 4c 45 43 54 20   ** is a SELECT 
320c1 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 20 20 2a 2f  statement...  */
320c2 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  ..  sqlite3VdbeR
320c3 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
320c4 6e 64 4f 66 4c 6f 6f 70 29 3b 0d 0a 20 20 69 66  ndOfLoop);..  if
320c5 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
320c6 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
320c7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
320c8 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61 64 64  ext, srcTab, add
320c9 72 43 6f 6e 74 29 3b 0d 0a 20 20 20 20 73 71 6c  rCont);..    sql
320ca 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
320cb 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29 3b  (v, addrInsTop);
320cc 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
320cd 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
320ce 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0d 0a 20  ose, srcTab);.. 
320cf 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65   }else if( pSele
320d0 63 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ct ){..    sqlit
320d1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
320d2 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
320d3 43 6f 6e 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69  Cont);..    sqli
320d4 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
320d5 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0d  v, addrInsTop);.
320d6 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 49  .  }....  if( !I
320d7 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 26  sVirtual(pTab) &
320d8 26 20 21 69 73 56 69 65 77 20 29 7b 0d 0a 20 20  & !isView ){..  
320d9 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
320da 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0d  ables opened */.
320db 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
320dc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
320dd 73 65 2c 20 62 61 73 65 43 75 72 29 3b 0d 0a 20  se, baseCur);.. 
320de 20 20 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49     for(idx=1, pI
320df 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
320e0 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
320e1 2d 3e 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b  ->pNext, idx++){
320e2 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
320e3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
320e4 43 6c 6f 73 65 2c 20 69 64 78 2b 62 61 73 65 43  Close, idx+baseC
320e5 75 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  ur);..    }..  }
320e6 0d 0a 0d 0a 69 6e 73 65 72 74 5f 65 6e 64 3a 0d  ....insert_end:.
320e7 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
320e8 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
320e9 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e   table by storin
320ea 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
320eb 20 74 68 65 0d 0a 20 20 2a 2a 20 6d 61 78 69 6d   the..  ** maxim
320ec 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72  um rowid counter
320ed 20 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64   values recorded
320ee 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67   while inserting
320ef 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20 61 75 74 6f   into..  ** auto
320f0 69 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73  increment tables
320f1 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70  ...  */..  if( p
320f2 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
320f3 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69   && pParse->pTri
320f4 67 67 65 72 54 61 62 3d 3d 30 20 29 7b 0d 0a 20  ggerTab==0 ){.. 
320f5 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e     sqlite3Autoin
320f6 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72 73  crementEnd(pPars
320f7 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  e);..  }....  /*
320f8 0d 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  ..  ** Return th
320f9 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
320fa 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 74 68   inserted. If th
320fb 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0d 0a  is routine is ..
320fc 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20    ** generating 
320fd 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20  code because of 
320fe 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
320ff 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20  3NestedParse(), 
32100 64 6f 20 6e 6f 74 0d 0a 20 20 2a 2a 20 69 6e 76  do not..  ** inv
32101 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
32102 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 20 20 2a 2f   function...  */
32103 0d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  ..  if( (db->fla
32104 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  gs&SQLITE_CountR
32105 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d  ows) && !pParse-
32106 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50 61 72  >nested && !pPar
32107 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
32108 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ){..    sqlite3V
32109 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
3210a 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f  ResultRow, regRo
3210b 77 43 6f 75 6e 74 2c 20 31 29 3b 0d 0a 20 20 20  wCount, 1);..   
3210c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
3210d 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0d 0a 20  umCols(v, 1);.. 
3210e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
3210f 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
32110 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f  OLNAME_NAME, "ro
32111 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20 53 51  ws inserted", SQ
32112 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20  LITE_STATIC);.. 
32113 20 7d 0d 0a 0d 0a 69 6e 73 65 72 74 5f 63 6c 65   }....insert_cle
32114 61 6e 75 70 3a 0d 0a 20 20 73 71 6c 69 74 65 33  anup:..  sqlite3
32115 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
32116 2c 20 70 54 61 62 4c 69 73 74 29 3b 0d 0a 20 20  , pTabList);..  
32117 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
32118 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
32119 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  ;..  sqlite3Sele
3211a 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
3211b 6c 65 63 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65  lect);..  sqlite
3211c 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
3211d 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 73  , pColumn);..  s
3211e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3211f 20 61 52 65 67 49 64 78 29 3b 0d 0a 7d 0d 0a 0d   aRegIdx);..}...
32120 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69  ./* Make sure "i
32121 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68 65 72  sView" and other
32122 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20   macros defined 
32123 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65 66 69  above are undefi
32124 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 0d 0a  ned. Otherwise..
32125 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e 74  ** thely may int
32126 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d 70  erfere with comp
32127 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72  ilation of other
32128 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68   functions in th
32129 69 73 20 66 69 6c 65 0d 0a 2a 2a 20 28 6f 72 20  is file..** (or 
3212a 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c  in another file,
3212b 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65   if this file be
3212c 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68  comes part of th
3212d 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e  e amalgamation).
3212e 20 20 2a 2f 0d 0a 23 69 66 64 65 66 20 69 73 56    */..#ifdef isV
3212f 69 65 77 0d 0a 20 23 75 6e 64 65 66 20 69 73 56  iew.. #undef isV
32130 69 65 77 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  iew..#endif..#if
32131 64 65 66 20 70 54 72 69 67 67 65 72 0d 0a 20 23  def pTrigger.. #
32132 75 6e 64 65 66 20 70 54 72 69 67 67 65 72 0d 0a  undef pTrigger..
32133 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 74  #endif..#ifdef t
32134 6d 61 73 6b 0d 0a 20 23 75 6e 64 65 66 20 74 6d  mask.. #undef tm
32135 61 73 6b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  ask..#endif.....
32136 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ./*..** Generate
32137 20 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73   code to do cons
32138 74 72 61 69 6e 74 20 63 68 65 63 6b 73 20 70 72  traint checks pr
32139 69 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45 52 54  ior to an INSERT
3213a 20 6f 72 20 61 6e 20 55 50 44 41 54 45 2e 0d 0a   or an UPDATE...
3213b 2a 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  **..** The input
3213c 20 69 73 20 61 20 72 61 6e 67 65 20 6f 66 20 63   is a range of c
3213d 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69 73  onsecutive regis
3213e 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ters as follows:
3213f 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 31 2e 20 20  ..**..**    1.  
32140 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  The rowid of the
32141 20 72 6f 77 20 61 66 74 65 72 20 74 68 65 20 75   row after the u
32142 70 64 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  pdate...**..**  
32143 20 20 32 2e 20 20 54 68 65 20 64 61 74 61 20 69    2.  The data i
32144 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  n the first colu
32145 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20  mn of the entry 
32146 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
32147 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 69 2e 20  ...**..**    i. 
32148 20 44 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c   Data from middl
32149 65 20 63 6f 6c 75 6d 6e 73 2e 2e 2e 0d 0a 2a 2a  e columns.....**
3214a 0d 0a 2a 2a 20 20 20 20 4e 2e 20 20 54 68 65 20  ..**    N.  The 
3214b 64 61 74 61 20 69 6e 20 74 68 65 20 6c 61 73 74  data in the last
3214c 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 65   column of the e
3214d 6e 74 72 79 20 61 66 74 65 72 20 74 68 65 20 75  ntry after the u
3214e 70 64 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  pdate...**..** T
3214f 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72 61  he regRowid para
32150 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e 64  meter is the ind
32151 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73 74  ex of the regist
32152 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28 31  er containing (1
32153 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69 73  )...**..** If is
32154 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20 61  Update is true a
32155 6e 64 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20  nd rowidChng is 
32156 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 72  non-zero, then r
32157 6f 77 69 64 43 68 6e 67 20 63 6f 6e 74 61 69 6e  owidChng contain
32158 73 0d 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73  s..** the addres
32159 73 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20  s of a register 
3215a 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
3215b 6f 77 69 64 20 62 65 66 6f 72 65 20 74 68 65 20  owid before the 
3215c 75 70 64 61 74 65 20 74 61 6b 65 73 0d 0a 2a 2a  update takes..**
3215d 20 70 6c 61 63 65 2e 20 69 73 55 70 64 61 74 65   place. isUpdate
3215e 20 69 73 20 74 72 75 65 20 66 6f 72 20 55 50 44   is true for UPD
3215f 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65 20 66  ATEs and false f
32160 6f 72 20 49 4e 53 45 52 54 73 2e 20 49 66 20 69  or INSERTs. If i
32161 73 55 70 64 61 74 65 0d 0a 2a 2a 20 69 73 20 66  sUpdate..** is f
32162 61 6c 73 65 2c 20 69 6e 64 69 63 61 74 69 6e 67  alse, indicating
32163 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
32164 6d 65 6e 74 2c 20 74 68 65 6e 20 61 20 6e 6f 6e  ment, then a non
32165 2d 7a 65 72 6f 20 72 6f 77 69 64 43 68 6e 67 20  -zero rowidChng 
32166 0d 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74  ..** indicates t
32167 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 77 61  hat the rowid wa
32168 73 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65  s explicitly spe
32169 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
3216a 66 20 74 68 65 0d 0a 2a 2a 20 49 4e 53 45 52 54  f the..** INSERT
3216b 20 73 74 61 74 65 6d 65 6e 74 2e 20 49 66 20 72   statement. If r
3216c 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c 73  owidChng is fals
3216d 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
3216e 20 20 74 68 65 20 72 6f 77 69 64 20 69 73 0d 0a    the rowid is..
3216f 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f  ** computed auto
32170 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20  matically in an 
32171 69 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74  insert or that t
32172 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69  he rowid value i
32173 73 20 6e 6f 74 20 0d 0a 2a 2a 20 6d 6f 64 69 66  s not ..** modif
32174 69 65 64 20 62 79 20 61 6e 20 75 70 64 61 74 65  ied by an update
32175 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f  ...**..** The co
32176 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
32177 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f  this routine sto
32178 72 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  re new index ent
32179 72 69 65 73 20 69 6e 74 6f 0d 0a 2a 2a 20 72 65  ries into..** re
3217a 67 69 73 74 65 72 73 20 69 64 65 6e 74 69 66 69  gisters identifi
3217b 65 64 20 62 79 20 61 52 65 67 49 64 78 5b 5d 2e  ed by aRegIdx[].
3217c 20 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74 72 79    No index entry
3217d 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 0d   is created for.
3217e 0a 2a 2a 20 69 6e 64 69 63 65 73 20 77 68 65 72  .** indices wher
3217f 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e  e aRegIdx[i]==0.
32180 20 20 54 68 65 20 6f 72 64 65 72 20 6f 66 20 69    The order of i
32181 6e 64 69 63 65 73 20 69 6e 20 61 52 65 67 49 64  ndices in aRegId
32182 78 5b 5d 20 69 73 0d 0a 2a 2a 20 74 68 65 20 73  x[] is..** the s
32183 61 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72  ame as the order
32184 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74   of indices on t
32185 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  he linked list o
32186 66 20 69 6e 64 69 63 65 73 0d 0a 2a 2a 20 61 74  f indices..** at
32187 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61  tached to the ta
32188 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ble...**..** Thi
32189 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67  s routine also g
3218a 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
3218b 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
3218c 74 73 2e 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0d 0a  ts.  NOT NULL,..
3218d 2a 2a 20 43 48 45 43 4b 2c 20 61 6e 64 20 55 4e  ** CHECK, and UN
3218e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 73  IQUE constraints
3218f 20 61 72 65 20 61 6c 6c 20 63 68 65 63 6b 65 64   are all checked
32190 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69  .  If a constrai
32191 6e 74 20 66 61 69 6c 73 2c 0d 0a 2a 2a 20 74 68  nt fails,..** th
32192 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
32193 74 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  te action is per
32194 66 6f 72 6d 65 64 2e 20 20 54 68 65 72 65 20 61  formed.  There a
32195 72 65 20 66 69 76 65 20 70 6f 73 73 69 62 6c 65  re five possible
32196 0d 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a 20 52 4f  ..** actions: RO
32197 4c 4c 42 41 43 4b 2c 20 41 42 4f 52 54 2c 20 46  LLBACK, ABORT, F
32198 41 49 4c 2c 20 52 45 50 4c 41 43 45 2c 20 61 6e  AIL, REPLACE, an
32199 64 20 49 47 4e 4f 52 45 2e 0d 0a 2a 2a 0d 0a 2a  d IGNORE...**..*
3219a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20 74 79  *  Constraint ty
3219b 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20 20 20  pe  Action      
3219c 20 57 68 61 74 20 48 61 70 70 65 6e 73 0d 0a 2a   What Happens..*
3219d 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
3219e 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --  ----------  
3219f 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
321a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
321a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20 20 61  ---------..**  a
321a2 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
321a3 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65  ROLLBACK     The
321a4 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
321a5 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62  tion is rolled b
321a6 61 63 6b 20 61 6e 64 0d 0a 2a 2a 20 20 20 20 20  ack and..**     
321a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321a8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
321a9 65 33 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e  e3_exec() return
321aa 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69  s immediately wi
321ab 74 68 20 61 0d 0a 2a 2a 20 20 20 20 20 20 20 20  th a..**        
321ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321ad 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 63          return c
321ae 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f  ode of SQLITE_CO
321af 4e 53 54 52 41 49 4e 54 2e 0d 0a 2a 2a 0d 0a 2a  NSTRAINT...**..*
321b0 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20  *  any          
321b1 20 20 20 20 41 42 4f 52 54 20 20 20 20 20 20 20      ABORT       
321b2 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65   Back out change
321b3 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65  s from the curre
321b4 6e 74 20 63 6f 6d 6d 61 6e 64 0d 0a 2a 2a 20 20  nt command..**  
321b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e                on
321b7 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20  ly (do not do a 
321b8 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63  complete rollbac
321b9 6b 29 20 74 68 65 6e 0d 0a 2a 2a 20 20 20 20 20  k) then..**     
321ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321bb 20 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65             cause
321bc 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
321bd 74 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69  to return immedi
321be 61 74 65 6c 79 0d 0a 2a 2a 20 20 20 20 20 20 20  ately..**       
321bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321c0 20 20 20 20 20 20 20 20 20 77 69 74 68 20 53 51           with SQ
321c1 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e  LITE_CONSTRAINT.
321c2 0d 0a 2a 2a 0d 0a 2a 2a 20 20 61 6e 79 20 20 20  ..**..**  any   
321c3 20 20 20 20 20 20 20 20 20 20 20 46 41 49 4c 20             FAIL 
321c4 20 20 20 20 20 20 20 20 53 71 6c 69 74 65 33 5f          Sqlite3_
321c5 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20 69  exec() returns i
321c6 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68 20  mmediately with 
321c7 61 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  a..**           
321c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321c9 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65       return code
321ca 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   of SQLITE_CONST
321cb 52 41 49 4e 54 2e 20 20 54 68 65 0d 0a 2a 2a 20  RAINT.  The..** 
321cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
321ce 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f  ransaction is no
321cf 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e  t rolled back an
321d0 64 20 61 6e 79 0d 0a 2a 2a 20 20 20 20 20 20 20  d any..**       
321d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321d2 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 20 63           prior c
321d3 68 61 6e 67 65 73 20 61 72 65 20 72 65 74 61 69  hanges are retai
321d4 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 61 6e  ned...**..**  an
321d5 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49  y              I
321d6 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65 20  GNORE       The 
321d7 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61 6e  record number an
321d8 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65 64  d data is popped
321d9 20 66 72 6f 6d 0d 0a 2a 2a 20 20 20 20 20 20 20   from..**       
321da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321db 20 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61           the sta
321dc 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ck and there is 
321dd 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
321de 70 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  p..**           
321df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321e0 20 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67       to label ig
321e1 6e 6f 72 65 44 65 73 74 2e 0d 0a 2a 2a 0d 0a 2a  noreDest...**..*
321e2 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20  *  NOT NULL     
321e3 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20      REPLACE     
321e4 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20   The NULL value 
321e5 69 73 20 72 65 70 6c 61 63 65 20 62 79 20 74 68  is replace by th
321e6 65 20 64 65 66 61 75 6c 74 0d 0a 2a 2a 20 20 20  e default..**   
321e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321e8 20 20 20 20 20 20 20 20 20 20 20 20 20 76 61 6c               val
321e9 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f 6c 75  ue for that colu
321ea 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65 66 61  mn.  If the defa
321eb 75 6c 74 20 76 61 6c 75 65 0d 0a 2a 2a 20 20 20  ult value..**   
321ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321ed 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
321ee 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e  NULL, the action
321ef 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
321f0 41 42 4f 52 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ABORT...**..**  
321f1 55 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20  UNIQUE          
321f2 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68   REPLACE      Th
321f3 65 20 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74  e other row that
321f4 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20   conflicts with 
321f5 74 68 65 20 72 6f 77 0d 0a 2a 2a 20 20 20 20 20  the row..**     
321f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321f7 20 20 20 20 20 20 20 20 20 20 20 62 65 69 6e 67             being
321f8 20 69 6e 73 65 72 74 65 64 20 69 73 20 72 65 6d   inserted is rem
321f9 6f 76 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 43  oved...**..**  C
321fa 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 20  HECK            
321fb 52 45 50 4c 41 43 45 20 20 20 20 20 20 49 6c 6c  REPLACE      Ill
321fc 65 67 61 6c 2e 20 20 54 68 65 20 72 65 73 75 6c  egal.  The resul
321fd 74 73 20 69 6e 20 61 6e 20 65 78 63 65 70 74 69  ts in an excepti
321fe 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 69 63  on...**..** Whic
321ff 68 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  h action to take
32200 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
32201 79 20 74 68 65 20 6f 76 65 72 72 69 64 65 45 72  y the overrideEr
32202 72 6f 72 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a  ror parameter...
32203 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72 72 69 64  ** Or if overrid
32204 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  eError==OE_Defau
32205 6c 74 2c 20 74 68 65 6e 20 74 68 65 20 70 50 61  lt, then the pPa
32206 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20 70 61 72  rse->onError par
32207 61 6d 65 74 65 72 0d 0a 2a 2a 20 69 73 20 75 73  ameter..** is us
32208 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73  ed.  Or if pPars
32209 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  e->onError==OE_D
3220a 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68 65 20  efault then the 
3220b 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0d 0a 2a  onError value..*
3220c 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72  * for the constr
3220d 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0d 0a 2a  aint is used...*
3220e 2a 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e  *..** The callin
3220f 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f  g routine must o
32210 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65  pen a read/write
32211 20 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62   cursor for pTab
32212 20 77 69 74 68 0d 0a 2a 2a 20 63 75 72 73 6f 72   with..** cursor
32213 20 6e 75 6d 62 65 72 20 22 62 61 73 65 43 75 72   number "baseCur
32214 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20  ".  All indices 
32215 6f 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73  of pTab must als
32216 6f 20 68 61 76 65 20 6f 70 65 6e 0d 0a 2a 2a 20  o have open..** 
32217 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
32218 72 73 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e  rs with cursor n
32219 75 6d 62 65 72 20 62 61 73 65 43 75 72 2b 69 20  umber baseCur+i 
3221a 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 75 72  for the i-th cur
3221b 73 6f 72 2e 0d 0a 2a 2a 20 45 78 63 65 70 74 2c  sor...** Except,
3221c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
3221d 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 61  possibility of a
3221e 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f 6e 20   REPLACE action 
3221f 74 68 65 6e 0d 0a 2a 2a 20 63 75 72 73 6f 72 73  then..** cursors
32220 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
32221 62 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69  be open for indi
32222 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49 64  ces where aRegId
32223 78 5b 69 5d 3d 3d 30 2e 0d 0a 2a 2f 0d 0a 53 51  x[i]==0...*/..SQ
32224 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
32225 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
32226 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b  eConstraintCheck
32227 73 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  s(..  Parse *pPa
32228 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  rse,      /* The
32229 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
3222a 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  */..  Table *pTa
3222b 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  b,        /* the
3222c 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
3222d 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
3222e 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 62 61 73  ng */..  int bas
3222f 65 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  eCur,        /* 
32230 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64 2f  Index of a read/
32231 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69  write cursor poi
32232 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a 2f  nting at pTab */
32233 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  ..  int regRowid
32234 2c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ,       /* Index
32235 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
32236 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
32237 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 52 65 67   */..  int *aReg
32238 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65  Idx,       /* Re
32239 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65  gister used by e
3223a 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f  ach index.  0 fo
3223b 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73  r unused indices
3223c 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 6f 77 69 64   */..  int rowid
3223d 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a 20 54 72  Chng,      /* Tr
3223e 75 65 20 69 66 20 74 68 65 20 72 6f 77 69 64 20  ue if the rowid 
3223f 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 20 77 69  might collide wi
32240 74 68 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72  th existing entr
32241 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 55 70  y */..  int isUp
32242 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  date,       /* T
32243 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20  rue for UPDATE, 
32244 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54  False for INSERT
32245 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 76 65 72 72   */..  int overr
32246 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f 76  ideError,  /* Ov
32247 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20 74  erride onError t
32248 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f 45  o this if not OE
32249 5f 44 65 66 61 75 6c 74 20 2a 2f 0d 0a 20 20 69  _Default */..  i
3224a 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 2c 20 20  nt ignoreDest,  
3224b 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68     /* Jump to th
3224c 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f  is label on an O
3224d 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74  E_Ignore resolut
3224e 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70  ion */..  int *p
3224f 62 4d 61 79 52 65 70 6c 61 63 65 20 20 20 2f 2a  bMayReplace   /*
32250 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72 75   OUT: Set to tru
32251 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20  e if constraint 
32252 6d 61 79 20 63 61 75 73 65 20 61 20 72 65 70 6c  may cause a repl
32253 61 63 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ace */..){..  in
32254 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
32255 20 20 2f 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65    /* loop counte
32256 72 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76 3b  r */..  Vdbe *v;
32257 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
32258 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  DBE under constr
32259 75 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ution */..  int 
3225a 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
3225b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
3225c 75 6d 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f  umns */..  int o
3225d 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 2f  nError;        /
3225e 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  * Conflict resol
3225f 75 74 69 6f 6e 20 73 74 72 61 74 65 67 79 20 2a  ution strategy *
32260 2f 0d 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20  /..  int j1;    
32261 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
32262 65 73 73 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73  esss of jump ins
32263 74 72 75 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69  truction */..  i
32264 6e 74 20 6a 32 20 3d 20 30 2c 20 6a 33 3b 20 20  nt j2 = 0, j3;  
32265 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20     /* Addresses 
32266 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74  of jump instruct
32267 69 6f 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  ions */..  int r
32268 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f  egData;        /
32269 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
3226a 69 6e 69 6e 67 20 66 69 72 73 74 20 64 61 74 61  ining first data
3226b 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 69 6e   column */..  in
3226c 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20  t iCur;         
3226d 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f    /* Table curso
3226e 72 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 49  r number */..  I
3226f 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
32270 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
32271 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64  o one of the ind
32272 69 63 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 73  ices */..  int s
32273 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 30 3b 20  eenReplace = 0; 
32274 2f 2a 20 54 72 75 65 20 69 66 20 52 45 50 4c 41  /* True if REPLA
32275 43 45 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  CE is used to re
32276 73 6f 6c 76 65 20 49 4e 54 20 50 4b 20 63 6f 6e  solve INT PK con
32277 66 6c 69 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  flict */..  int 
32278 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 28 72  regOldRowid = (r
32279 6f 77 69 64 43 68 6e 67 20 26 26 20 69 73 55 70  owidChng && isUp
3227a 64 61 74 65 29 20 3f 20 72 6f 77 69 64 43 68 6e  date) ? rowidChn
3227b 67 20 3a 20 72 65 67 52 6f 77 69 64 3b 0d 0a 0d  g : regRowid;...
3227c 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
3227d 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a  tVdbe(pParse);..
3227e 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
3227f 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  ;..  assert( pTa
32280 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  b->pSelect==0 );
32281 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20    /* This table 
32282 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f  is not a VIEW */
32283 0d 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d  ..  nCol = pTab-
32284 3e 6e 43 6f 6c 3b 0d 0a 20 20 72 65 67 44 61 74  >nCol;..  regDat
32285 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31  a = regRowid + 1
32286 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 65 73 74 20 61  ;....  /* Test a
32287 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  ll NOT NULL cons
32288 74 72 61 69 6e 74 73 2e 0d 0a 20 20 2a 2f 0d 0a  traints...  */..
32289 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
3228a 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  l; i++){..    if
3228b 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79  ( i==pTab->iPKey
3228c 20 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 74 69   ){..      conti
3228d 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  nue;..    }..   
3228e 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d   onError = pTab-
3228f 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c  >aCol[i].notNull
32290 3b 0d 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72  ;..    if( onErr
32291 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f  or==OE_None ) co
32292 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28  ntinue;..    if(
32293 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d   overrideError!=
32294 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0d 0a 20  OE_Default ){.. 
32295 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
32296 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0d 0a 20  verrideError;.. 
32297 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45     }else if( onE
32298 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
32299 20 29 7b 0d 0a 20 20 20 20 20 20 6f 6e 45 72 72   ){..      onErr
3229a 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a  or = OE_Abort;..
3229b 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 6f      }..    if( o
3229c 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
3229d 63 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c  ce && pTab->aCol
3229e 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b 0d  [i].pDflt==0 ){.
3229f 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
322a0 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20   OE_Abort;..    
322a1 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f  }..    assert( o
322a2 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62  nError==OE_Rollb
322a3 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ack || onError==
322a4 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72  OE_Abort || onEr
322a5 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0d 0a 20 20  ror==OE_Fail..  
322a6 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72        || onError
322a7 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f  ==OE_Ignore || o
322a8 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
322a9 63 65 20 29 3b 0d 0a 20 20 20 20 73 77 69 74 63  ce );..    switc
322aa 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0d 0a 20  h( onError ){.. 
322ab 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f       case OE_Abo
322ac 72 74 3a 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  rt:..        sql
322ad 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
322ae 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 63 61 73  rse);..      cas
322af 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0d 0a  e OE_Rollback:..
322b0 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
322b1 69 6c 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 63  il: {..        c
322b2 68 61 72 20 2a 7a 4d 73 67 3b 0d 0a 20 20 20 20  har *zMsg;..    
322b3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
322b4 64 64 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp3(v, OP_Halt
322b5 49 66 4e 75 6c 6c 2c 0d 0a 20 20 20 20 20 20 20  IfNull,..       
322b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322b7 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
322b8 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
322b9 45 72 72 6f 72 2c 20 72 65 67 44 61 74 61 2b 69  Error, regData+i
322ba 29 3b 0d 0a 20 20 20 20 20 20 20 20 7a 4d 73 67  );..        zMsg
322bb 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
322bc 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25  f(pParse->db, "%
322bd 73 2e 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  s.%s may not be 
322be 4e 55 4c 4c 22 2c 0d 0a 20 20 20 20 20 20 20 20  NULL",..        
322bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322c0 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
322c1 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
322c2 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20  .zName);..      
322c3 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
322c4 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 7a 4d 73  ngeP4(v, -1, zMs
322c5 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0d  g, P4_DYNAMIC);.
322c6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
322c7 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
322c8 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20  case OE_Ignore: 
322c9 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
322ca 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
322cb 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 44 61  OP_IsNull, regDa
322cc 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44 65 73 74  ta+i, ignoreDest
322cd 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );..        brea
322ce 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
322cf 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20     default: {.. 
322d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f         assert( o
322d1 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
322d2 63 65 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 6a  ce );..        j
322d3 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
322d4 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
322d5 75 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b  ull, regData+i);
322d6 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
322d7 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
322d8 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pTab->aCol[i].
322d9 70 44 66 6c 74 2c 20 72 65 67 44 61 74 61 2b 69  pDflt, regData+i
322da 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
322db 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
322dc 76 2c 20 6a 31 29 3b 0d 0a 20 20 20 20 20 20 20  v, j1);..       
322dd 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
322de 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
322df 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48    /* Test all CH
322e0 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0d  ECK constraints.
322e1 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  .  */..#ifndef S
322e2 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
322e3 0d 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 43  ..  if( pTab->pC
322e4 68 65 63 6b 20 26 26 20 28 70 50 61 72 73 65 2d  heck && (pParse-
322e5 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
322e6 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
322e7 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74  )==0 ){..    int
322e8 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33   allOk = sqlite3
322e9 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
322ea 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63  ;..    pParse->c
322eb 6b 42 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b  kBase = regData;
322ec 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
322ed 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
322ee 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c  pTab->pCheck, al
322ef 6c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  lOk, SQLITE_JUMP
322f0 49 46 4e 55 4c 4c 29 3b 0d 0a 20 20 20 20 6f 6e  IFNULL);..    on
322f1 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65  Error = override
322f2 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
322f3 74 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72 6f  t ? overrideErro
322f4 72 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20  r : OE_Abort;.. 
322f5 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d     if( onError==
322f6 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0d 0a 20 20  OE_Ignore ){..  
322f7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
322f8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
322f9 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29  , 0, ignoreDest)
322fa 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
322fb 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72       if( onError
322fc 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 20 6f  ==OE_Replace ) o
322fd 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
322fe 74 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 31 35 35  t; /* IMP: R-155
322ff 36 39 2d 36 33 36 32 35 20 2a 2f 0d 0a 20 20 20  69-63625 */..   
32300 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f     sqlite3HaltCo
32301 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c  nstraint(pParse,
32302 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 29 3b   onError, 0, 0);
32303 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
32304 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
32305 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0d  abel(v, allOk);.
32306 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  .  }..#endif /* 
32307 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
32308 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0d 0a  OMIT_CHECK) */..
32309 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76  ..  /* If we hav
3230a 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  e an INTEGER PRI
3230b 4d 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65 20 73  MARY KEY, make s
3230c 75 72 65 20 74 68 65 20 70 72 69 6d 61 72 79 20  ure the primary 
3230d 6b 65 79 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65  key..  ** of the
3230e 20 6e 65 77 20 72 65 63 6f 72 64 20 64 6f 65 73   new record does
3230f 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
32310 65 78 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20  exist.  Except, 
32311 69 66 20 74 68 69 73 0d 0a 20 20 2a 2a 20 69 73  if this..  ** is
32312 20 61 6e 20 55 50 44 41 54 45 20 61 6e 64 20 74   an UPDATE and t
32313 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
32314 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20  s not changing, 
32315 74 68 61 74 20 69 73 20 4f 4b 2e 0d 0a 20 20 2a  that is OK...  *
32316 2f 0d 0a 20 20 69 66 28 20 72 6f 77 69 64 43 68  /..  if( rowidCh
32317 6e 67 20 29 7b 0d 0a 20 20 20 20 6f 6e 45 72 72  ng ){..    onErr
32318 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f  or = pTab->keyCo
32319 6e 66 3b 0d 0a 20 20 20 20 69 66 28 20 6f 76 65  nf;..    if( ove
3231a 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
3231b 65 66 61 75 6c 74 20 29 7b 0d 0a 20 20 20 20 20  efault ){..     
3231c 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
3231d 69 64 65 45 72 72 6f 72 3b 0d 0a 20 20 20 20 7d  ideError;..    }
3231e 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72  else if( onError
3231f 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0d  ==OE_Default ){.
32320 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
32321 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20   OE_Abort;..    
32322 7d 0d 0a 20 20 20 20 0d 0a 20 20 20 20 69 66 28  }..    ..    if(
32323 20 69 73 55 70 64 61 74 65 20 29 7b 0d 0a 20 20   isUpdate ){..  
32324 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33      j2 = sqlite3
32325 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
32326 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c 20 30  _Eq, regRowid, 0
32327 2c 20 72 6f 77 69 64 43 68 6e 67 29 3b 0d 0a 20  , rowidChng);.. 
32328 20 20 20 7d 0d 0a 20 20 20 20 6a 33 20 3d 20 73     }..    j3 = s
32329 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
3232a 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
3232b 2c 20 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65  , baseCur, 0, re
3232c 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 73 77  gRowid);..    sw
3232d 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
3232e 0d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ..      default:
3232f 20 7b 0d 0a 20 20 20 20 20 20 20 20 6f 6e 45 72   {..        onEr
32330 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0d  ror = OE_Abort;.
32331 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c  .        /* Fall
32332 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e   thru into the n
32333 65 78 74 20 63 61 73 65 20 2a 2f 0d 0a 20 20 20  ext case */..   
32334 20 20 20 7d 0d 0a 20 20 20 20 20 20 63 61 73 65     }..      case
32335 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0d 0a 20   OE_Rollback:.. 
32336 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f       case OE_Abo
32337 72 74 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20  rt:..      case 
32338 4f 45 5f 46 61 69 6c 3a 20 7b 0d 0a 20 20 20 20  OE_Fail: {..    
32339 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43      sqlite3HaltC
3233a 6f 6e 73 74 72 61 69 6e 74 28 0d 0a 20 20 20 20  onstraint(..    
3233b 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e        pParse, on
3233c 45 72 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20  Error, "PRIMARY 
3233d 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71  KEY must be uniq
3233e 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ue", P4_STATIC);
3233f 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
32340 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
32341 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65   case OE_Replace
32342 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20  : {..        /* 
32343 49 66 20 74 68 65 72 65 20 61 72 65 20 44 45 4c  If there are DEL
32344 45 54 45 20 74 72 69 67 67 65 72 73 20 6f 6e 20  ETE triggers on 
32345 74 68 69 73 20 74 61 62 6c 65 20 61 6e 64 20 74  this table and t
32346 68 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  he..        ** r
32347 65 63 75 72 73 69 76 65 2d 74 72 69 67 67 65 72  ecursive-trigger
32348 73 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 63  s flag is set, c
32349 61 6c 6c 20 47 65 6e 65 72 61 74 65 52 6f 77 44  all GenerateRowD
3234a 65 6c 65 74 65 28 29 20 74 6f 0d 0a 20 20 20 20  elete() to..    
3234b 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68      ** remove th
3234c 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 6f  e conflicting ro
3234d 77 20 66 72 6f 6d 20 74 68 65 20 74 68 65 20 74  w from the the t
3234e 61 62 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20  able. This will 
3234f 66 69 72 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a  fire..        **
32350 20 74 68 65 20 74 72 69 67 67 65 72 73 20 61 6e   the triggers an
32351 64 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68  d remove both th
32352 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  e table and inde
32353 78 20 62 2d 74 72 65 65 20 65 6e 74 72 69 65 73  x b-tree entries
32354 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20  ...        **.. 
32355 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77         ** Otherw
32356 69 73 65 2c 20 69 66 20 74 68 65 72 65 20 61 72  ise, if there ar
32357 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 72  e no triggers or
32358 20 74 68 65 20 72 65 63 75 72 73 69 76 65 2d 74   the recursive-t
32359 72 69 67 67 65 72 73 0d 0a 20 20 20 20 20 20 20  riggers..       
3235a 20 2a 2a 20 66 6c 61 67 20 69 73 20 6e 6f 74 20   ** flag is not 
3235b 73 65 74 2c 20 62 75 74 20 74 68 65 20 74 61 62  set, but the tab
3235c 6c 65 20 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f  le has one or mo
3235d 72 65 20 69 6e 64 65 78 65 73 2c 20 63 61 6c 6c  re indexes, call
3235e 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65   ..        ** Ge
3235f 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65  nerateRowIndexDe
32360 6c 65 74 65 28 29 2e 20 54 68 69 73 20 72 65 6d  lete(). This rem
32361 6f 76 65 73 20 74 68 65 20 69 6e 64 65 78 20 62  oves the index b
32362 2d 74 72 65 65 20 65 6e 74 72 69 65 73 20 0d 0a  -tree entries ..
32363 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79 2e          ** only.
32364 20 54 68 65 20 74 61 62 6c 65 20 62 2d 74 72 65   The table b-tre
32365 65 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20  e entry will be 
32366 72 65 70 6c 61 63 65 64 20 62 79 20 74 68 65 20  replaced by the 
32367 6e 65 77 20 65 6e 74 72 79 20 0d 0a 20 20 20 20  new entry ..    
32368 20 20 20 20 2a 2a 20 77 68 65 6e 20 69 74 20 69      ** when it i
32369 73 20 69 6e 73 65 72 74 65 64 2e 20 20 0d 0a 20  s inserted.  .. 
3236a 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20         **..     
3236b 20 20 20 2a 2a 20 49 66 20 65 69 74 68 65 72 20     ** If either 
3236c 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
3236d 65 28 29 20 6f 72 20 47 65 6e 65 72 61 74 65 52  e() or GenerateR
3236e 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 29 20  owIndexDelete() 
3236f 69 73 20 63 61 6c 6c 65 64 2c 0d 0a 20 20 20 20  is called,..    
32370 20 20 20 20 2a 2a 20 61 6c 73 6f 20 69 6e 76 6f      ** also invo
32371 6b 65 20 4d 75 6c 74 69 57 72 69 74 65 28 29 20  ke MultiWrite() 
32372 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
32373 20 74 68 69 73 20 56 44 42 45 20 6d 61 79 20 72   this VDBE may r
32374 65 71 75 69 72 65 0d 0a 20 20 20 20 20 20 20 20  equire..        
32375 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c  ** statement rol
32376 6c 62 61 63 6b 20 28 69 66 20 74 68 65 20 73 74  lback (if the st
32377 61 74 65 6d 65 6e 74 20 69 73 20 61 62 6f 72 74  atement is abort
32378 65 64 20 61 66 74 65 72 20 74 68 65 20 64 65 6c  ed after the del
32379 65 74 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ete..        ** 
3237a 74 61 6b 65 73 20 70 6c 61 63 65 29 2e 20 45 61  takes place). Ea
3237b 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73 20 63  rlier versions c
3237c 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 4d 75 6c  alled sqlite3Mul
3237d 74 69 57 72 69 74 65 28 29 20 72 65 67 61 72 64  tiWrite() regard
3237e 6c 65 73 73 2c 0d 0a 20 20 20 20 20 20 20 20 2a  less,..        *
3237f 2a 20 62 75 74 20 62 65 69 6e 67 20 6d 6f 72 65  * but being more
32380 20 73 65 6c 65 63 74 69 76 65 20 68 65 72 65 20   selective here 
32381 61 6c 6c 6f 77 73 20 73 74 61 74 65 6d 65 6e 74  allows statement
32382 73 20 6c 69 6b 65 3a 0d 0a 20 20 20 20 20 20 20  s like:..       
32383 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20   **..        ** 
32384 20 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 74    REPLACE INTO t
32385 28 72 6f 77 69 64 29 20 56 41 4c 55 45 53 28 24  (rowid) VALUES($
32386 6e 65 77 72 6f 77 69 64 29 0d 0a 20 20 20 20 20  newrowid)..     
32387 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a     **..        *
32388 2a 20 74 6f 20 72 75 6e 20 77 69 74 68 6f 75 74  * to run without
32389 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75   a statement jou
3238a 72 6e 61 6c 20 69 66 20 74 68 65 72 65 20 61 72  rnal if there ar
3238b 65 20 6e 6f 20 69 6e 64 65 78 65 73 20 6f 6e 20  e no indexes on 
3238c 74 68 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  the..        ** 
3238d 74 61 62 6c 65 2e 0d 0a 20 20 20 20 20 20 20 20  table...        
3238e 2a 2f 0d 0a 20 20 20 20 20 20 20 20 54 72 69 67  */..        Trig
3238f 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d 20  ger *pTrigger = 
32390 30 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  0;..        if( 
32391 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
32392 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67  s&SQLITE_RecTrig
32393 67 65 72 73 20 29 7b 0d 0a 20 20 20 20 20 20 20  gers ){..       
32394 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71     pTrigger = sq
32395 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
32396 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  st(pParse, pTab,
32397 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30   TK_DELETE, 0, 0
32398 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
32399 20 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67         if( pTrig
3239a 67 65 72 20 7c 7c 20 73 71 6c 69 74 65 33 46 6b  ger || sqlite3Fk
3239b 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65 2c  Required(pParse,
3239c 20 70 54 61 62 2c 20 30 2c 20 30 29 20 29 7b 0d   pTab, 0, 0) ){.
3239d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
3239e 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50 61  e3MultiWrite(pPa
3239f 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  rse);..         
323a0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
323a1 52 6f 77 44 65 6c 65 74 65 28 0d 0a 20 20 20 20  RowDelete(..    
323a2 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
323a3 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
323a4 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 70 54   regRowid, 0, pT
323a5 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61  rigger, OE_Repla
323a6 63 65 0d 0a 20 20 20 20 20 20 20 20 20 20 29 3b  ce..          );
323a7 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ..        }else 
323a8 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  if( pTab->pIndex
323a9 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
323aa 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65  qlite3MultiWrite
323ab 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
323ac 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
323ad 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
323ae 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
323af 20 62 61 73 65 43 75 72 2c 20 30 29 3b 0d 0a 20   baseCur, 0);.. 
323b0 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
323b1 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20    seenReplace = 
323b2 31 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  1;..        brea
323b3 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
323b4 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
323b5 65 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 61 73  e: {..        as
323b6 73 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63  sert( seenReplac
323b7 65 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20  e==0 );..       
323b8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
323b9 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
323ba 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0d 0a  , ignoreDest);..
323bb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
323bc 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
323bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
323be 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0d  umpHere(v, j3);.
323bf 0a 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74  .    if( isUpdat
323c0 65 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  e ){..      sqli
323c1 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
323c2 76 2c 20 6a 32 29 3b 0d 0a 20 20 20 20 7d 0d 0a  v, j2);..    }..
323c3 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 65 73 74    }....  /* Test
323c4 20 61 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73   all UNIQUE cons
323c5 74 72 61 69 6e 74 73 20 62 79 20 63 72 65 61 74  traints by creat
323c6 69 6e 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20  ing entries for 
323c7 65 61 63 68 20 55 4e 49 51 55 45 0d 0a 20 20 2a  each UNIQUE..  *
323c8 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69  * index and maki
323c9 6e 67 20 73 75 72 65 20 74 68 61 74 20 64 75 70  ng sure that dup
323ca 6c 69 63 61 74 65 20 65 6e 74 72 69 65 73 20 64  licate entries d
323cb 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  o not already ex
323cc 69 73 74 2e 0d 0a 20 20 2a 2a 20 41 64 64 20 74  ist...  ** Add t
323cd 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73 20 74  he new records t
323ce 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20 61 73  o the indices as
323cf 20 77 65 20 67 6f 2e 0d 0a 20 20 2a 2f 0d 0a 20   we go...  */.. 
323d0 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64   for(iCur=0, pId
323d1 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
323d2 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
323d3 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b  >pNext, iCur++){
323d4 0d 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64 78  ..    int regIdx
323d5 3b 0d 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b  ;..    int regR;
323d6 0d 0a 0d 0a 20 20 20 20 69 66 28 20 61 52 65 67  ....    if( aReg
323d7 49 64 78 5b 69 43 75 72 5d 3d 3d 30 20 29 20 63  Idx[iCur]==0 ) c
323d8 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 53 6b 69  ontinue;  /* Ski
323d9 70 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73  p unused indices
323da 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 72   */....    /* Cr
323db 65 61 74 65 20 61 20 6b 65 79 20 66 6f 72 20 61  eate a key for a
323dc 63 63 65 73 73 69 6e 67 20 74 68 65 20 69 6e 64  ccessing the ind
323dd 65 78 20 65 6e 74 72 79 20 2a 2f 0d 0a 20 20 20  ex entry */..   
323de 20 72 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65   regIdx = sqlite
323df 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
323e0 61 72 73 65 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  arse, pIdx->nCol
323e1 75 6d 6e 2b 31 29 3b 0d 0a 20 20 20 20 66 6f 72  umn+1);..    for
323e2 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43  (i=0; i<pIdx->nC
323e3 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20  olumn; i++){..  
323e4 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 49      int idx = pI
323e5 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
323e6 0d 0a 20 20 20 20 20 20 69 66 28 20 69 64 78 3d  ..      if( idx=
323e7 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0d  =pTab->iPKey ){.
323e8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
323e9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
323ea 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64  _SCopy, regRowid
323eb 2c 20 72 65 67 49 64 78 2b 69 29 3b 0d 0a 20 20  , regIdx+i);..  
323ec 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
323ed 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
323ee 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
323ef 79 2c 20 72 65 67 44 61 74 61 2b 69 64 78 2c 20  y, regData+idx, 
323f0 72 65 67 49 64 78 2b 69 29 3b 0d 0a 20 20 20 20  regIdx+i);..    
323f1 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
323f2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
323f3 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
323f4 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b  egRowid, regIdx+
323f5 69 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  i);..    sqlite3
323f6 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
323f7 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
323f8 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  Idx, pIdx->nColu
323f9 6d 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43  mn+1, aRegIdx[iC
323fa 75 72 5d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  ur]);..    sqlit
323fb 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
323fc 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64  , -1, sqlite3Ind
323fd 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
323fe 20 70 49 64 78 29 2c 20 50 34 5f 54 52 41 4e 53   pIdx), P4_TRANS
323ff 49 45 4e 54 29 3b 0d 0a 20 20 20 20 73 71 6c 69  IENT);..    sqli
32400 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
32401 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
32402 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d  e, regIdx, pIdx-
32403 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0d 0a 0d 0a  >nColumn+1);....
32404 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20      /* Find out 
32405 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74  what action to t
32406 61 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72  ake in case ther
32407 65 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67  e is an indexing
32408 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0d 0a 20 20   conflict */..  
32409 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78    onError = pIdx
3240a 2d 3e 6f 6e 45 72 72 6f 72 3b 0d 0a 20 20 20 20  ->onError;..    
3240b 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
3240c 4e 6f 6e 65 20 29 7b 20 0d 0a 20 20 20 20 20 20  None ){ ..      
3240d 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
3240e 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
3240f 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
32410 6f 6c 75 6d 6e 2b 31 29 3b 0d 0a 20 20 20 20 20  olumn+1);..     
32411 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 70   continue;  /* p
32412 49 64 78 20 69 73 20 6e 6f 74 20 61 20 55 4e 49  Idx is not a UNI
32413 51 55 45 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  QUE index */..  
32414 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 6f 76 65    }..    if( ove
32415 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
32416 65 66 61 75 6c 74 20 29 7b 0d 0a 20 20 20 20 20  efault ){..     
32417 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
32418 69 64 65 45 72 72 6f 72 3b 0d 0a 20 20 20 20 7d  ideError;..    }
32419 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72  else if( onError
3241a 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0d  ==OE_Default ){.
3241b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
3241c 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20   OE_Abort;..    
3241d 7d 0d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52  }..    if( seenR
3241e 65 70 6c 61 63 65 20 29 7b 0d 0a 20 20 20 20 20  eplace ){..     
3241f 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
32420 5f 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f  _Ignore ) onErro
32421 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0d  r = OE_Replace;.
32422 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
32423 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c  onError==OE_Fail
32424 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f   ) onError = OE_
32425 41 62 6f 72 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Abort;..    }.. 
32426 20 20 20 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63     ..    /* Chec
32427 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
32428 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20  new index entry 
32429 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 2a  will be unique *
3242a 2f 0d 0a 20 20 20 20 72 65 67 52 20 3d 20 73 71  /..    regR = sq
3242b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
3242c 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 73 71  pParse);..    sq
3242d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3242e 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
3242f 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 52 29 3b  OldRowid, regR);
32430 0d 0a 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74  ..    j3 = sqlit
32431 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
32432 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20 62 61 73  OP_IsUnique, bas
32433 65 43 75 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0d  eCur+iCur+1, 0,.
32434 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32435 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 52              regR
32436 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
32437 50 54 52 28 72 65 67 49 64 78 29 2c 0d 0a 20 20  PTR(regIdx),..  
32438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32439 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e 54 33           P4_INT3
3243a 32 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  2);..    sqlite3
3243b 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
3243c 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c  (pParse, regIdx,
3243d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31   pIdx->nColumn+1
3243e 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47 65 6e  );....    /* Gen
3243f 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
32440 65 78 65 63 75 74 65 73 20 69 66 20 74 68 65 20  executes if the 
32441 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20  new index entry 
32442 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a 2f  is not unique */
32443 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  ..    assert( on
32444 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  Error==OE_Rollba
32445 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  ck || onError==O
32446 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72  E_Abort || onErr
32447 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0d 0a 20 20 20  or==OE_Fail..   
32448 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d       || onError=
32449 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e  =OE_Ignore || on
3244a 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
3244b 65 20 29 3b 0d 0a 20 20 20 20 73 77 69 74 63 68  e );..    switch
3244c 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0d 0a 20 20  ( onError ){..  
3244d 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c      case OE_Roll
3244e 62 61 63 6b 3a 0d 0a 20 20 20 20 20 20 63 61 73  back:..      cas
3244f 65 20 4f 45 5f 41 62 6f 72 74 3a 0d 0a 20 20 20  e OE_Abort:..   
32450 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a     case OE_Fail:
32451 20 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20   {..        int 
32452 6a 3b 0d 0a 20 20 20 20 20 20 20 20 53 74 72 41  j;..        StrA
32453 63 63 75 6d 20 65 72 72 4d 73 67 3b 0d 0a 20 20  ccum errMsg;..  
32454 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
32455 20 2a 7a 53 65 70 3b 0d 0a 20 20 20 20 20 20 20   *zSep;..       
32456 20 63 68 61 72 20 2a 7a 45 72 72 3b 0d 0a 0d 0a   char *zErr;....
32457 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
32458 74 72 41 63 63 75 6d 49 6e 69 74 28 26 65 72 72  trAccumInit(&err
32459 4d 73 67 2c 20 30 2c 20 30 2c 20 32 30 30 29 3b  Msg, 0, 0, 200);
3245a 0d 0a 20 20 20 20 20 20 20 20 65 72 72 4d 73 67  ..        errMsg
3245b 2e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  .db = pParse->db
3245c 3b 0d 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20  ;..        zSep 
3245d 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e  = pIdx->nColumn>
3245e 31 20 3f 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a  1 ? "columns " :
3245f 20 22 63 6f 6c 75 6d 6e 20 22 3b 0d 0a 20 20 20   "column ";..   
32460 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
32461 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
32462 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ++){..          
32463 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61  char *zCol = pTa
32464 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69  b->aCol[pIdx->ai
32465 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65  Column[j]].zName
32466 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
32467 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
32468 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a 53 65 70  nd(&errMsg, zSep
32469 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20 20 20  , -1);..        
3246a 20 20 7a 53 65 70 20 3d 20 22 2c 20 22 3b 0d 0a    zSep = ", ";..
3246b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3246c 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
3246d 26 65 72 72 4d 73 67 2c 20 7a 43 6f 6c 2c 20 2d  &errMsg, zCol, -
3246e 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  1);..        }..
3246f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
32470 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65  trAccumAppend(&e
32471 72 72 4d 73 67 2c 0d 0a 20 20 20 20 20 20 20 20  rrMsg,..        
32472 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d      pIdx->nColum
32473 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74 20  n>1 ? " are not 
32474 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20 6e  unique" : " is n
32475 6f 74 20 75 6e 69 71 75 65 22 2c 20 2d 31 29 3b  ot unique", -1);
32476 0d 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d  ..        zErr =
32477 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
32478 46 69 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b  Finish(&errMsg);
32479 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
3247a 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28  3HaltConstraint(
3247b 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c  pParse, onError,
3247c 20 7a 45 72 72 2c 20 30 29 3b 0d 0a 20 20 20 20   zErr, 0);..    
3247d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3247e 65 28 65 72 72 4d 73 67 2e 64 62 2c 20 7a 45 72  e(errMsg.db, zEr
3247f 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65  r);..        bre
32480 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
32481 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f      case OE_Igno
32482 72 65 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 61  re: {..        a
32483 73 73 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61  ssert( seenRepla
32484 63 65 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ce==0 );..      
32485 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
32486 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
32487 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0d  0, ignoreDest);.
32488 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
32489 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3248a 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20  default: {..    
3248b 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72      Trigger *pTr
3248c 69 67 67 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20  igger = 0;..    
3248d 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72      assert( onEr
3248e 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror==OE_Replace 
3248f 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
32490 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50  te3MultiWrite(pP
32491 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  arse);..        
32492 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  if( pParse->db->
32493 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63  flags&SQLITE_Rec
32494 54 72 69 67 67 65 72 73 20 29 7b 0d 0a 20 20 20  Triggers ){..   
32495 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20         pTrigger 
32496 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
32497 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
32498 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  Tab, TK_DELETE, 
32499 30 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  0, 0);..        
3249a 7d 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  }..        sqlit
3249b 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
3249c 65 74 65 28 0d 0a 20 20 20 20 20 20 20 20 20 20  ete(..          
3249d 20 20 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20    pParse, pTab, 
3249e 62 61 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30  baseCur, regR, 0
3249f 2c 20 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 52  , pTrigger, OE_R
324a0 65 70 6c 61 63 65 0d 0a 20 20 20 20 20 20 20 20  eplace..        
324a1 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 65 65 6e  );..        seen
324a2 52 65 70 6c 61 63 65 20 3d 20 31 3b 0d 0a 20 20  Replace = 1;..  
324a3 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
324a4 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
324a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
324a6 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0d 0a 20  pHere(v, j3);.. 
324a7 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
324a8 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
324a9 20 72 65 67 52 29 3b 0d 0a 20 20 7d 0d 0a 20 20   regR);..  }..  
324aa 0d 0a 20 20 69 66 28 20 70 62 4d 61 79 52 65 70  ..  if( pbMayRep
324ab 6c 61 63 65 20 29 7b 0d 0a 20 20 20 20 2a 70 62  lace ){..    *pb
324ac 4d 61 79 52 65 70 6c 61 63 65 20 3d 20 73 65 65  MayReplace = see
324ad 6e 52 65 70 6c 61 63 65 3b 0d 0a 20 20 7d 0d 0a  nReplace;..  }..
324ae 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
324af 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
324b0 65 73 20 63 6f 64 65 20 74 6f 20 66 69 6e 69 73  es code to finis
324b1 68 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20  h the INSERT or 
324b2 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
324b3 0d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 73 74  ..** that was st
324b4 61 72 74 65 64 20 62 79 20 61 20 70 72 69 6f 72  arted by a prior
324b5 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
324b6 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
324b7 6e 74 43 68 65 63 6b 73 2e 0d 0a 2a 2a 20 41 20  ntChecks...** A 
324b8 63 6f 6e 73 65 63 75 74 69 76 65 20 72 61 6e 67  consecutive rang
324b9 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 73  e of registers s
324ba 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 52 6f  tarting at regRo
324bb 77 69 64 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  wid contains the
324bc 0d 0a 2a 2a 20 72 6f 77 69 64 20 61 6e 64 20 74  ..** rowid and t
324bd 68 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 62 65  he content to be
324be 20 69 6e 73 65 72 74 65 64 2e 0d 0a 2a 2a 0d 0a   inserted...**..
324bf 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73  ** The arguments
324c0 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
324c1 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73   should be the s
324c2 61 6d 65 20 61 73 20 74 68 65 20 66 69 72 73 74  ame as the first
324c3 20 73 69 78 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e   six..** argumen
324c4 74 73 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ts to sqlite3Gen
324c5 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
324c6 68 65 63 6b 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  hecks...*/..SQLI
324c7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
324c8 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
324c9 6e 73 65 72 74 69 6f 6e 28 0d 0a 20 20 50 61 72  nsertion(..  Par
324ca 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
324cb 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
324cc 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62  ontext */..  Tab
324cd 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
324ce 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e   /* the table in
324cf 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
324d0 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0d 0a 20 20  inserting */..  
324d1 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
324d2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
324d3 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  a read/write cur
324d4 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
324d5 70 54 61 62 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  pTab */..  int r
324d6 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f  egRowid,       /
324d7 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65  * Range of conte
324d8 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 52  nt */..  int *aR
324d9 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20  egIdx,       /* 
324da 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79  Register used by
324db 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20   each index.  0 
324dc 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
324dd 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 55  es */..  int isU
324de 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20  pdate,       /* 
324df 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c  True for UPDATE,
324e0 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52   False for INSER
324e1 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 70 70 65  T */..  int appe
324e2 6e 64 42 69 61 73 2c 20 20 20 20 20 2f 2a 20 54  ndBias,     /* T
324e3 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c  rue if this is l
324e4 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61  ikely to be an a
324e5 70 70 65 6e 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  ppend */..  int 
324e6 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20 20 20  useSeekResult   
324e7 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 74  /* True to set t
324e8 68 65 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54  he USESEEKRESULT
324e9 20 66 6c 61 67 20 6f 6e 20 4f 50 5f 5b 49 64 78   flag on OP_[Idx
324ea 5d 49 6e 73 65 72 74 20 2a 2f 0d 0a 29 7b 0d 0a  ]Insert */..){..
324eb 20 20 69 6e 74 20 69 3b 0d 0a 20 20 56 64 62 65    int i;..  Vdbe
324ec 20 2a 76 3b 0d 0a 20 20 69 6e 74 20 6e 49 64 78   *v;..  int nIdx
324ed 3b 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  ;..  Index *pIdx
324ee 3b 0d 0a 20 20 75 38 20 70 69 6b 5f 66 6c 61 67  ;..  u8 pik_flag
324ef 73 3b 0d 0a 20 20 69 6e 74 20 72 65 67 44 61 74  s;..  int regDat
324f0 61 3b 0d 0a 20 20 69 6e 74 20 72 65 67 52 65 63  a;..  int regRec
324f1 3b 0d 0a 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74  ;....  v = sqlit
324f2 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
324f3 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 76 21  );..  assert( v!
324f4 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
324f5 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
324f6 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
324f7 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
324f8 57 20 2a 2f 0d 0a 20 20 66 6f 72 28 6e 49 64 78  W */..  for(nIdx
324f9 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
324fa 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
324fb 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e  x=pIdx->pNext, n
324fc 49 64 78 2b 2b 29 7b 7d 0d 0a 20 20 66 6f 72 28  Idx++){}..  for(
324fd 69 3d 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20  i=nIdx-1; i>=0; 
324fe 69 2d 2d 29 7b 0d 0a 20 20 20 20 69 66 28 20 61  i--){..    if( a
324ff 52 65 67 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63  RegIdx[i]==0 ) c
32500 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 73 71  ontinue;..    sq
32501 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
32502 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
32503 20 62 61 73 65 43 75 72 2b 69 2b 31 2c 20 61 52   baseCur+i+1, aR
32504 65 67 49 64 78 5b 69 5d 29 3b 0d 0a 20 20 20 20  egIdx[i]);..    
32505 69 66 28 20 75 73 65 53 65 65 6b 52 65 73 75 6c  if( useSeekResul
32506 74 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  t ){..      sqli
32507 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
32508 76 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45  v, OPFLAG_USESEE
32509 4b 52 45 53 55 4c 54 29 3b 0d 0a 20 20 20 20 7d  KRESULT);..    }
3250a 0d 0a 20 20 7d 0d 0a 20 20 72 65 67 44 61 74 61  ..  }..  regData
3250b 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b   = regRowid + 1;
3250c 0d 0a 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c  ..  regRec = sql
3250d 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
3250e 50 61 72 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74  Parse);..  sqlit
3250f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
32510 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
32511 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43  egData, pTab->nC
32512 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0d 0a 20 20  ol, regRec);..  
32513 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
32514 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29  nityStr(v, pTab)
32515 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
32516 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
32517 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 44  nge(pParse, regD
32518 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ata, pTab->nCol)
32519 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
3251a 3e 6e 65 73 74 65 64 20 29 7b 0d 0a 20 20 20 20  >nested ){..    
3251b 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0d 0a  pik_flags = 0;..
3251c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 69    }else{..    pi
3251d 6b 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47  k_flags = OPFLAG
3251e 5f 4e 43 48 41 4e 47 45 3b 0d 0a 20 20 20 20 70  _NCHANGE;..    p
3251f 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55  ik_flags |= (isU
32520 70 64 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55  pdate?OPFLAG_ISU
32521 50 44 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53  PDATE:OPFLAG_LAS
32522 54 52 4f 57 49 44 29 3b 0d 0a 20 20 7d 0d 0a 20  TROWID);..  }.. 
32523 20 69 66 28 20 61 70 70 65 6e 64 42 69 61 73 20   if( appendBias 
32524 29 7b 0d 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67  ){..    pik_flag
32525 73 20 7c 3d 20 4f 50 46 4c 41 47 5f 41 50 50 45  s |= OPFLAG_APPE
32526 4e 44 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  ND;..  }..  if( 
32527 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29 7b  useSeekResult ){
32528 0d 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20  ..    pik_flags 
32529 7c 3d 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45  |= OPFLAG_USESEE
3252a 4b 52 45 53 55 4c 54 3b 0d 0a 20 20 7d 0d 0a 20  KRESULT;..  }.. 
3252b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3252c 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
3252d 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 63   baseCur, regRec
3252e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20  , regRowid);..  
3252f 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73  if( !pParse->nes
32530 74 65 64 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ted ){..    sqli
32531 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
32532 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61  v, -1, pTab->zNa
32533 6d 65 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54  me, P4_TRANSIENT
32534 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
32535 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
32536 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0d 0a 7d  , pik_flags);..}
32537 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  ..../*..** Gener
32538 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
32539 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20  ll open cursors 
3253a 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20  for a table and 
3253b 66 6f 72 20 61 6c 6c 0d 0a 2a 2a 20 69 6e 64 69  for all..** indi
3253c 63 65 73 20 6f 66 20 74 68 61 74 20 74 61 62 6c  ces of that tabl
3253d 65 2e 20 20 54 68 65 20 22 62 61 73 65 43 75 72  e.  The "baseCur
3253e 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  " parameter is t
3253f 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
32540 20 75 73 65 64 0d 0a 2a 2a 20 66 6f 72 20 74 68   used..** for th
32541 65 20 74 61 62 6c 65 2e 20 20 49 6e 64 69 63 65  e table.  Indice
32542 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f 6e 20  s are opened on 
32543 73 75 62 73 65 71 75 65 6e 74 20 63 75 72 73 6f  subsequent curso
32544 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75  rs...**..** Retu
32545 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
32546 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
32547 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  table...*/..SQLI
32548 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
32549 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41  qlite3OpenTableA
3254a 6e 64 49 6e 64 69 63 65 73 28 0d 0a 20 20 50 61  ndIndices(..  Pa
3254b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f  rse *pParse,   /
3254c 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
3254d 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70  t */..  Table *p
3254e 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  Tab,     /* Tabl
3254f 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a  e to be opened *
32550 2f 0d 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  /..  int baseCur
32551 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  ,     /* Cursor 
32552 6e 75 6d 62 65 72 20 61 73 73 69 67 6e 65 64 20  number assigned 
32553 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d  to the table */.
32554 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20 20 20  .  int op       
32555 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65      /* OP_OpenRe
32556 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69  ad or OP_OpenWri
32557 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  te */..){..  int
32558 20 69 3b 0d 0a 20 20 69 6e 74 20 69 44 62 3b 0d   i;..  int iDb;.
32559 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d  .  Index *pIdx;.
3255a 0a 20 20 56 64 62 65 20 2a 76 3b 0d 0a 0d 0a 20  .  Vdbe *v;.... 
3255b 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
3255c 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b  Tab) ) return 0;
3255d 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
3255e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
3255f 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
32560 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20 76 20  >pSchema);..  v 
32561 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
32562 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 61 73 73  (pParse);..  ass
32563 65 72 74 28 20 76 21 3d 30 20 29 3b 0d 0a 20 20  ert( v!=0 );..  
32564 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
32565 28 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 72  (pParse, baseCur
32566 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29  , iDb, pTab, op)
32567 3b 0d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49  ;..  for(i=1, pI
32568 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
32569 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
3256a 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0d 0a  ->pNext, i++){..
3256b 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
3256c 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
3256d 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
3256e 70 49 64 78 29 3b 0d 0a 20 20 20 20 61 73 73 65  pIdx);..    asse
3256f 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  rt( pIdx->pSchem
32570 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
32571 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   );..    sqlite3
32572 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70  VdbeAddOp4(v, op
32573 2c 20 69 2b 62 61 73 65 43 75 72 2c 20 70 49 64  , i+baseCur, pId
32574 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0d 0a 20  x->tnum, iDb,.. 
32575 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32576 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
32577 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
32578 44 4f 46 46 29 3b 0d 0a 20 20 20 20 56 64 62 65  DOFF);..    Vdbe
32579 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
3257a 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
3257b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 50 61  ..  }..  if( pPa
3257c 72 73 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75  rse->nTab<baseCu
3257d 72 2b 69 20 29 7b 0d 0a 20 20 20 20 70 50 61 72  r+i ){..    pPar
3257e 73 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65 43  se->nTab = baseC
3257f 75 72 2b 69 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  ur+i;..  }..  re
32580 74 75 72 6e 20 69 2d 31 3b 0d 0a 7d 0d 0a 0d 0a  turn i-1;..}....
32581 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
32582 54 45 53 54 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  TEST../*..** The
32583 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61   following globa
32584 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e  l variable is in
32585 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76  cremented whenev
32586 65 72 20 74 68 65 0d 0a 2a 2a 20 74 72 61 6e 73  er the..** trans
32587 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
32588 20 69 73 20 75 73 65 64 2e 20 20 54 68 69 73 20   is used.  This 
32589 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
3258a 69 6e 67 0d 0a 2a 2a 20 70 75 72 70 6f 73 65 73  ing..** purposes
3258b 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20   only - to make 
3258c 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 66 65  sure the transfe
3258d 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72  r optimization r
3258e 65 61 6c 6c 79 0d 0a 2a 2a 20 69 73 20 68 61 70  eally..** is hap
3258f 70 65 6e 69 6e 67 20 77 68 65 6e 20 69 74 20 69  pening when it i
32590 73 20 73 75 70 70 6f 73 65 20 74 6f 2e 0d 0a 2a  s suppose to...*
32591 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
32592 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70  t sqlite3_xferop
32593 74 5f 63 6f 75 6e 74 3b 0d 0a 23 65 6e 64 69 66  t_count;..#endif
32594 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20   /* SQLITE_TEST 
32595 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  */......#ifndef 
32596 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
32597 5f 4f 50 54 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65  _OPT../*..** Che
32598 63 6b 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20  ck to collation 
32599 6e 61 6d 65 73 20 74 6f 20 73 65 65 20 69 66 20  names to see if 
3259a 74 68 65 79 20 61 72 65 20 63 6f 6d 70 61 74 69  they are compati
3259b 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ble...*/..static
3259c 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61 74 69   int xferCompati
3259d 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 63 6f 6e  bleCollation(con
3259e 73 74 20 63 68 61 72 20 2a 7a 31 2c 20 63 6f 6e  st char *z1, con
3259f 73 74 20 63 68 61 72 20 2a 7a 32 29 7b 0d 0a 20  st char *z2){.. 
325a0 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b 0d 0a 20   if( z1==0 ){.. 
325a1 20 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d 30 3b     return z2==0;
325a2 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 7a 32 3d  ..  }..  if( z2=
325a3 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
325a4 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  n 0;..  }..  ret
325a5 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 49 43  urn sqlite3StrIC
325a6 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0d 0a  mp(z1, z2)==0;..
325a7 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68  }....../*..** Ch
325a8 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 6e  eck to see if in
325a9 64 65 78 20 70 53 72 63 20 69 73 20 63 6f 6d 70  dex pSrc is comp
325aa 61 74 69 62 6c 65 20 61 73 20 61 20 73 6f 75 72  atible as a sour
325ab 63 65 20 6f 66 20 64 61 74 61 0d 0a 2a 2a 20 66  ce of data..** f
325ac 6f 72 20 69 6e 64 65 78 20 70 44 65 73 74 20 69  or index pDest i
325ad 6e 20 61 6e 20 69 6e 73 65 72 74 20 74 72 61 6e  n an insert tran
325ae 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
325af 6e 2e 20 20 54 68 65 20 72 75 6c 65 73 0d 0a 2a  n.  The rules..*
325b0 2a 20 66 6f 72 20 61 20 63 6f 6d 70 61 74 69 62  * for a compatib
325b1 6c 65 20 69 6e 64 65 78 3a 0d 0a 2a 2a 0d 0a 2a  le index:..**..*
325b2 2a 20 20 20 20 2a 20 20 20 54 68 65 20 69 6e 64  *    *   The ind
325b3 65 78 20 69 73 20 6f 76 65 72 20 74 68 65 20 73  ex is over the s
325b4 61 6d 65 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d  ame set of colum
325b5 6e 73 0d 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  ns..**    *   Th
325b6 65 20 73 61 6d 65 20 44 45 53 43 20 61 6e 64 20  e same DESC and 
325b7 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 6f 63 63  ASC markings occ
325b8 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  urs on all colum
325b9 6e 73 0d 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  ns..**    *   Th
325ba 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 20 70  e same onError p
325bb 72 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f 41 62  rocessing (OE_Ab
325bc 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
325bd 65 74 63 29 0d 0a 2a 2a 20 20 20 20 2a 20 20 20  etc)..**    *   
325be 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69  The same collati
325bf 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 65  ng sequence on e
325c0 61 63 68 20 63 6f 6c 75 6d 6e 0d 0a 2a 2f 0d 0a  ach column..*/..
325c1 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43  static int xferC
325c2 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49  ompatibleIndex(I
325c3 6e 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64  ndex *pDest, Ind
325c4 65 78 20 2a 70 53 72 63 29 7b 0d 0a 20 20 69 6e  ex *pSrc){..  in
325c5 74 20 69 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  t i;..  assert( 
325c6 70 44 65 73 74 20 26 26 20 70 53 72 63 20 29 3b  pDest && pSrc );
325c7 0d 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73  ..  assert( pDes
325c8 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63 2d  t->pTable!=pSrc-
325c9 3e 70 54 61 62 6c 65 20 29 3b 0d 0a 20 20 69 66  >pTable );..  if
325ca 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e  ( pDest->nColumn
325cb 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20  !=pSrc->nColumn 
325cc 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
325cd 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
325ce 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
325cf 6e 73 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66  ns */..  }..  if
325d0 28 20 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72  ( pDest->onError
325d1 21 3d 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20  !=pSrc->onError 
325d2 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
325d3 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
325d4 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
325d5 74 69 6f 6e 20 73 74 72 61 74 65 67 69 65 73 20  tion strategies 
325d6 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69  */..  }..  for(i
325d7 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 43 6f 6c  =0; i<pSrc->nCol
325d8 75 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  umn; i++){..    
325d9 69 66 28 20 70 53 72 63 2d 3e 61 69 43 6f 6c 75  if( pSrc->aiColu
325da 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 69  mn[i]!=pDest->ai
325db 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b 0d 0a 20 20  Column[i] ){..  
325dc 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
325dd 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c  /* Different col
325de 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 2a 2f 0d  umns indexed */.
325df 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
325e0 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pSrc->aSortOrder
325e1 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f 72  [i]!=pDest->aSor
325e2 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0d 0a 20 20  tOrder[i] ){..  
325e3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
325e4 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f 72  /* Different sor
325e5 74 20 6f 72 64 65 72 73 20 2a 2f 0d 0a 20 20 20  t orders */..   
325e6 20 7d 0d 0a 20 20 20 20 69 66 28 20 21 78 66 65   }..    if( !xfe
325e7 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
325e8 74 69 6f 6e 28 70 53 72 63 2d 3e 61 7a 43 6f 6c  tion(pSrc->azCol
325e9 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e 61 7a 43 6f  l[i],pDest->azCo
325ea 6c 6c 5b 69 5d 29 20 29 7b 0d 0a 20 20 20 20 20  ll[i]) ){..     
325eb 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
325ec 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61 74  Different collat
325ed 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f  ing sequences */
325ee 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
325ef 20 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20    /* If no test 
325f0 61 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e  above fails then
325f1 20 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73   the indices mus
325f2 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20  t be compatible 
325f3 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d  */..  return 1;.
325f4 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 74  .}..../*..** Att
325f5 65 6d 70 74 20 74 68 65 20 74 72 61 6e 73 66 65  empt the transfe
325f6 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f  r optimization o
325f7 6e 20 49 4e 53 45 52 54 73 20 6f 66 20 74 68 65  n INSERTs of the
325f8 20 66 6f 72 6d 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20   form..**..**   
325f9 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
325fa 62 31 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  b1 SELECT * FROM
325fb 20 74 61 62 32 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54   tab2;..**..** T
325fc 68 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61  he xfer optimiza
325fd 74 69 6f 6e 20 74 72 61 6e 73 66 65 72 73 20 72  tion transfers r
325fe 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20  aw records from 
325ff 74 61 62 32 20 6f 76 65 72 20 74 6f 20 74 61 62  tab2 over to tab
32600 31 2e 20 20 0d 0a 2a 2a 20 43 6f 6c 75 6d 6e 73  1.  ..** Columns
32601 20 61 72 65 20 6e 6f 74 20 64 65 63 6f 64 65 64   are not decoded
32602 20 61 6e 64 20 72 65 61 73 73 65 6d 62 6c 69 65   and reassemblie
32603 64 2c 20 77 68 69 63 68 20 67 72 65 61 74 6c 79  d, which greatly
32604 20 69 6d 70 72 6f 76 65 73 0d 0a 2a 2a 20 70 65   improves..** pe
32605 72 66 6f 72 6d 61 6e 63 65 2e 20 20 52 61 77 20  rformance.  Raw 
32606 69 6e 64 65 78 20 72 65 63 6f 72 64 73 20 61 72  index records ar
32607 65 20 74 72 61 6e 73 66 65 72 72 65 64 20 69 6e  e transferred in
32608 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e 0d 0a   the same way...
32609 2a 2a 0d 0a 2a 2a 20 54 68 65 20 78 66 65 72 20  **..** The xfer 
3260a 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
3260b 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69  only attempted i
3260c 66 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20  f tab1 and tab2 
3260d 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0d  are compatible..
3260e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6c 6f  .** There are lo
3260f 74 73 20 6f 66 20 72 75 6c 65 73 20 66 6f 72 20  ts of rules for 
32610 64 65 74 65 72 6d 69 6e 69 6e 67 20 63 6f 6d 70  determining comp
32611 61 74 69 62 69 6c 69 74 79 20 2d 20 73 65 65 20  atibility - see 
32612 63 6f 6d 6d 65 6e 74 73 0d 0a 2a 2a 20 65 6d 62  comments..** emb
32613 65 64 64 65 64 20 69 6e 20 74 68 65 20 63 6f 64  edded in the cod
32614 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0d 0a  e for details...
32615 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
32616 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52 55 45  ine returns TRUE
32617 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   if the optimiza
32618 74 69 6f 6e 20 69 73 20 67 75 61 72 61 6e 74 65  tion is guarante
32619 65 64 20 74 6f 20 62 65 20 75 73 65 64 2e 0d 0a  ed to be used...
3261a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 74 68 65  ** Sometimes the
3261b 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69   xfer optimizati
3261c 6f 6e 20 77 69 6c 6c 20 6f 6e 6c 79 20 77 6f 72  on will only wor
3261d 6b 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61  k if the destina
3261e 74 69 6f 6e 20 74 61 62 6c 65 0d 0a 2a 2a 20 69  tion table..** i
3261f 73 20 65 6d 70 74 79 20 2d 20 61 20 66 61 63 74  s empty - a fact
32620 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 6e 6c 79  or that can only
32621 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 20 61   be determined a
32622 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
32623 74 68 61 74 0d 0a 2a 2a 20 63 61 73 65 2c 20 74  that..** case, t
32624 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
32625 72 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20 74  rates code for t
32626 68 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61  he xfer optimiza
32627 74 69 6f 6e 20 62 75 74 20 61 6c 73 6f 0d 0a 2a  tion but also..*
32628 2a 20 64 6f 65 73 20 61 20 74 65 73 74 20 74 6f  * does a test to
32629 20 73 65 65 20 69 66 20 74 68 65 20 64 65 73 74   see if the dest
3262a 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65 20 69 73  ination table is
3262b 20 65 6d 70 74 79 20 61 6e 64 20 6a 75 6d 70 73   empty and jumps
3262c 20 6f 76 65 72 20 74 68 65 0d 0a 2a 2a 20 78 66   over the..** xf
3262d 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  er optimization 
3262e 63 6f 64 65 20 69 66 20 74 68 65 20 74 65 73 74  code if the test
3262f 20 66 61 69 6c 73 2e 20 20 49 6e 20 74 68 61 74   fails.  In that
32630 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74   case, this rout
32631 69 6e 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ine..** returns 
32632 46 41 4c 53 45 20 73 6f 20 74 68 61 74 20 74 68  FALSE so that th
32633 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 6e  e caller will kn
32634 6f 77 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61  ow to go ahead a
32635 6e 64 20 67 65 6e 65 72 61 74 65 0d 0a 2a 2a 20  nd generate..** 
32636 61 6e 20 75 6e 6f 70 74 69 6d 69 7a 65 64 20 74  an unoptimized t
32637 72 61 6e 73 66 65 72 2e 20 20 54 68 69 73 20 72  ransfer.  This r
32638 6f 75 74 69 6e 65 20 61 6c 73 6f 20 72 65 74 75  outine also retu
32639 72 6e 73 20 46 41 4c 53 45 20 69 66 20 74 68 65  rns FALSE if the
3263a 72 65 0d 0a 2a 2a 20 69 73 20 6e 6f 20 63 68 61  re..** is no cha
3263b 6e 63 65 20 74 68 61 74 20 74 68 65 20 78 66 65  nce that the xfe
3263c 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  r optimization c
3263d 61 6e 20 62 65 20 61 70 70 6c 69 65 64 2e 0d 0a  an be applied...
3263e 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69  **..** This opti
3263f 6d 69 7a 61 74 69 6f 6e 20 69 73 20 70 61 72 74  mization is part
32640 69 63 75 6c 61 72 6c 79 20 75 73 65 66 75 6c 20  icularly useful 
32641 61 74 20 6d 61 6b 69 6e 67 20 56 41 43 55 55 4d  at making VACUUM
32642 20 72 75 6e 20 66 61 73 74 65 72 2e 0d 0a 2a 2f   run faster...*/
32643 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65  ..static int xfe
32644 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0d 0a  rOptimization(..
32645 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
32646 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
32647 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  r context */..  
32648 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20 20  Table *pDest,   
32649 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
3264a 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  le we are insert
3264b 69 6e 67 20 69 6e 74 6f 20 2a 2f 0d 0a 20 20 53  ing into */..  S
3264c 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
3264d 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
3264e 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
3264f 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
32650 75 72 63 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f  urce */..  int o
32651 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  nError,         
32652 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
32653 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
32654 6f 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44  ors */..  int iD
32655 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20  bDest           
32656 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
32657 6f 66 20 70 44 65 73 74 20 2a 2f 0d 0a 29 7b 0d  of pDest */..){.
32658 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
32659 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
3265a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
3265b 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
3265c 45 43 54 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20  ECT */..  Table 
3265d 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20 20  *pSrc;          
3265e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3265f 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  e table in the F
32660 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 53 45  ROM clause of SE
32661 4c 45 43 54 20 2a 2f 0d 0a 20 20 49 6e 64 65 78  LECT */..  Index
32662 20 2a 70 53 72 63 49 64 78 2c 20 2a 70 44 65 73   *pSrcIdx, *pDes
32663 74 49 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 53  tIdx;       /* S
32664 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
32665 61 74 69 6f 6e 20 69 6e 64 69 63 65 73 20 2a 2f  ation indices */
32666 0d 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ..  struct SrcLi
32667 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20  st_item *pItem; 
32668 20 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65 6d 65       /* An eleme
32669 6e 74 20 6f 66 20 70 53 65 6c 65 63 74 2d 3e 70  nt of pSelect->p
3266a 53 72 63 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b  Src */..  int i;
3266b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3266c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
3266d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  op counter */.. 
3266e 20 69 6e 74 20 69 44 62 53 72 63 3b 20 20 20 20   int iDbSrc;    
3266f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32670 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
32671 65 20 6f 66 20 70 53 72 63 20 2a 2f 0d 0a 20 20  e of pSrc */..  
32672 69 6e 74 20 69 53 72 63 2c 20 69 44 65 73 74 3b  int iSrc, iDest;
32673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32674 20 2f 2a 20 43 75 72 73 6f 72 73 20 66 72 6f 6d   /* Cursors from
32675 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   source and dest
32676 69 6e 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e  ination */..  in
32677 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 20  t addr1, addr2; 
32678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32679 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73 73 65 73  * Loop addresses
3267a 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 6d 70 74 79   */..  int empty
3267b 44 65 73 74 54 65 73 74 3b 20 20 20 20 20 20 20  DestTest;       
3267c 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
3267d 73 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65  ss of test for e
3267e 6d 70 74 79 20 70 44 65 73 74 20 2a 2f 0d 0a 20  mpty pDest */.. 
3267f 20 69 6e 74 20 65 6d 70 74 79 53 72 63 54 65 73   int emptySrcTes
32680 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
32681 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
32682 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70  test for empty p
32683 53 72 63 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a  Src */..  Vdbe *
32684 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
32685 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
32686 65 20 56 44 42 45 20 77 65 20 61 72 65 20 62 75  e VDBE we are bu
32687 69 6c 64 69 6e 67 20 2a 2f 0d 0a 20 20 4b 65 79  ilding */..  Key
32688 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20  Info *pKey;     
32689 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3268a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Key information
3268b 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f   for an index */
3268c 0d 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69  ..  int regAutoi
3268d 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
3268e 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72       /* Memory r
3268f 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20  egister used by 
32690 41 55 54 4f 49 4e 43 20 2a 2f 0d 0a 20 20 69 6e  AUTOINC */..  in
32691 74 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  t destHasUniqueI
32692 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  dx = 0;        /
32693 2a 20 54 72 75 65 20 69 66 20 70 44 65 73 74 20  * True if pDest 
32694 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64  has a UNIQUE ind
32695 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  ex */..  int reg
32696 44 61 74 61 2c 20 72 65 67 52 6f 77 69 64 3b 20  Data, regRowid; 
32697 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
32698 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 64  isters holding d
32699 61 74 61 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f  ata and rowid */
3269a 0d 0a 0d 0a 20 20 69 66 28 20 70 53 65 6c 65 63  ....  if( pSelec
3269b 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74  t==0 ){..    ret
3269c 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d 75 73 74  urn 0;   /* Must
3269d 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   be of the form 
3269e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e   INSERT INTO ...
3269f 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0d 0a   SELECT ... */..
326a0 20 20 7d 0d 0a 20 20 69 66 28 20 73 71 6c 69 74    }..  if( sqlit
326a1 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50  e3TriggerList(pP
326a2 61 72 73 65 2c 20 70 44 65 73 74 29 20 29 7b 0d  arse, pDest) ){.
326a3 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
326a4 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f   /* tab1 must no
326a5 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20  t have triggers 
326a6 2a 2f 0d 0a 20 20 7d 0d 0a 23 69 66 6e 64 65 66  */..  }..#ifndef
326a7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
326a8 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 69 66 28  TUALTABLE..  if(
326a9 20 70 44 65 73 74 2d 3e 74 61 62 46 6c 61 67 73   pDest->tabFlags
326aa 20 26 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b   & TF_Virtual ){
326ab 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  ..    return 0; 
326ac 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e    /* tab1 must n
326ad 6f 74 20 62 65 20 61 20 76 69 72 74 75 61 6c 20  ot be a virtual 
326ae 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 7d 0d 0a 23  table */..  }..#
326af 65 6e 64 69 66 0d 0a 20 20 69 66 28 20 6f 6e 45  endif..  if( onE
326b0 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
326b1 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 44 65   ){..    if( pDe
326b2 73 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 20 6f  st->iPKey>=0 ) o
326b3 6e 45 72 72 6f 72 20 3d 20 70 44 65 73 74 2d 3e  nError = pDest->
326b4 6b 65 79 43 6f 6e 66 3b 0d 0a 20 20 20 20 69 66  keyConf;..    if
326b5 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
326b6 66 61 75 6c 74 20 29 20 6f 6e 45 72 72 6f 72 20  fault ) onError 
326b7 3d 20 4f 45 5f 41 62 6f 72 74 3b 0d 0a 20 20 7d  = OE_Abort;..  }
326b8 0d 0a 20 20 61 73 73 65 72 74 28 70 53 65 6c 65  ..  assert(pSele
326b9 63 74 2d 3e 70 53 72 63 29 3b 20 20 20 2f 2a 20  ct->pSrc);   /* 
326ba 61 6c 6c 6f 63 61 74 65 64 20 65 76 65 6e 20 69  allocated even i
326bb 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 46 52  f there is no FR
326bc 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20  OM clause */..  
326bd 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  if( pSelect->pSr
326be 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0d 0a 20  c->nSrc!=1 ){.. 
326bf 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
326c0 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d 75  * FROM clause mu
326c1 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20  st have exactly 
326c2 6f 6e 65 20 74 65 72 6d 20 2a 2f 0d 0a 20 20 7d  one term */..  }
326c3 0d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  ..  if( pSelect-
326c4 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c  >pSrc->a[0].pSel
326c5 65 63 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  ect ){..    retu
326c6 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20  rn 0;   /* FROM 
326c7 63 6c 61 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f  clause cannot co
326c8 6e 74 61 69 6e 20 61 20 73 75 62 71 75 65 72 79  ntain a subquery
326c9 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   */..  }..  if( 
326ca 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 20  pSelect->pWhere 
326cb 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
326cc 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
326cd 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48 45  y not have a WHE
326ce 52 45 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20  RE clause */..  
326cf 7d 0d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  }..  if( pSelect
326d0 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0d 0a 20  ->pOrderBy ){.. 
326d1 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
326d2 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
326d3 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42   have an ORDER B
326d4 59 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 7d  Y clause */..  }
326d5 0d 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65  ..  /* Do not ne
326d6 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 61  ed to test for a
326d7 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20   HAVING clause. 
326d8 20 49 66 20 48 41 56 49 4e 47 20 69 73 20 70 72   If HAVING is pr
326d9 65 73 65 6e 74 20 62 75 74 0d 0a 20 20 2a 2a 20  esent but..  ** 
326da 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45  there is no ORDE
326db 52 20 42 59 2c 20 77 65 20 77 69 6c 6c 20 67 65  R BY, we will ge
326dc 74 20 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0d 0a  t an error. */..
326dd 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
326de 47 72 6f 75 70 42 79 20 29 7b 0d 0a 20 20 20 20  GroupBy ){..    
326df 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
326e0 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61  ELECT may not ha
326e1 76 65 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c  ve a GROUP BY cl
326e2 61 75 73 65 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20  ause */..  }..  
326e3 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c 69  if( pSelect->pLi
326e4 6d 69 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  mit ){..    retu
326e5 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
326e6 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
326e7 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f   LIMIT clause */
326e8 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
326e9 20 70 53 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65   pSelect->pOffse
326ea 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74  t==0 );  /* Must
326eb 20 62 65 20 73 6f 20 69 66 20 70 4c 69 6d 69 74   be so if pLimit
326ec 3d 3d 30 20 2a 2f 0d 0a 20 20 69 66 28 20 70 53  ==0 */..  if( pS
326ed 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b  elect->pPrior ){
326ee 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  ..    return 0; 
326ef 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20    /* SELECT may 
326f0 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e  not be a compoun
326f1 64 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20 7d 0d  d query */..  }.
326f2 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
326f3 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
326f4 73 74 69 6e 63 74 20 29 7b 0d 0a 20 20 20 20 72  stinct ){..    r
326f5 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45  eturn 0;   /* SE
326f6 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20  LECT may not be 
326f7 44 49 53 54 49 4e 43 54 20 2a 2f 0d 0a 20 20 7d  DISTINCT */..  }
326f8 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ..  pEList = pSe
326f9 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0d 0a 20  lect->pEList;.. 
326fa 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
326fb 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 45 4c  =0 );..  if( pEL
326fc 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  ist->nExpr!=1 ){
326fd 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  ..    return 0; 
326fe 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20    /* The result 
326ff 73 65 74 20 6d 75 73 74 20 68 61 76 65 20 65 78  set must have ex
32700 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e  actly one column
32701 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65   */..  }..  asse
32702 72 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  rt( pEList->a[0]
32703 2e 70 45 78 70 72 20 29 3b 0d 0a 20 20 69 66 28  .pExpr );..  if(
32704 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
32705 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20  xpr->op!=TK_ALL 
32706 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
32707 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c  ;   /* The resul
32708 74 20 73 65 74 20 6d 75 73 74 20 62 65 20 74 68  t set must be th
32709 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74  e special operat
3270a 6f 72 20 22 2a 22 20 2a 2f 0d 0a 20 20 7d 0d 0a  or "*" */..  }..
3270b 0d 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
3270c 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65 73 74  oint we have est
3270d 61 62 6c 69 73 68 65 64 20 74 68 61 74 20 74 68  ablished that th
3270e 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
3270f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 63 6f 72 72  f the..  ** corr
32710 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66 6f  ect syntactic fo
32711 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61 74  rm to participat
32712 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d 69  e in this optimi
32713 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0d 0a 20 20  zation.  Now..  
32714 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68  ** we have to ch
32715 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63  eck the semantic
32716 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 49 74 65  s...  */..  pIte
32717 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  m = pSelect->pSr
32718 63 2d 3e 61 3b 0d 0a 20 20 70 53 72 63 20 3d 20  c->a;..  pSrc = 
32719 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
3271a 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49  le(pParse, 0, pI
3271b 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65  tem->zName, pIte
3271c 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0d 0a  m->zDatabase);..
3271d 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b    if( pSrc==0 ){
3271e 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  ..    return 0; 
3271f 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
32720 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
32721 6e 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 2a  n a real table *
32722 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 53  /..  }..  if( pS
32723 72 63 3d 3d 70 44 65 73 74 20 29 7b 0d 0a 20 20  rc==pDest ){..  
32724 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
32725 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 6d   tab1 and tab2 m
32726 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61  ay not be the sa
32727 6d 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 7d  me table */..  }
32728 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
32729 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
3272a 4c 45 0d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e  LE..  if( pSrc->
3272b 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
3272c 72 74 75 61 6c 20 29 7b 0d 0a 20 20 20 20 72 65  rtual ){..    re
3272d 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
3272e 32 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20  2 must not be a 
3272f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
32730 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20  ..  }..#endif.. 
32731 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65 6c 65   if( pSrc->pSele
32732 63 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ct ){..    retur
32733 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
32734 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65 77  ay not be a view
32735 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   */..  }..  if( 
32736 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72  pDest->nCol!=pSr
32737 63 2d 3e 6e 43 6f 6c 20 29 7b 0d 0a 20 20 20 20  c->nCol ){..    
32738 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e  return 0;   /* N
32739 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
3273a 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
3273b 65 20 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61  e in tab1 and ta
3273c 62 32 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66  b2 */..  }..  if
3273d 28 20 70 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d  ( pDest->iPKey!=
3273e 70 53 72 63 2d 3e 69 50 4b 65 79 20 29 7b 0d 0a  pSrc->iPKey ){..
3273f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
32740 2f 2a 20 42 6f 74 68 20 74 61 62 6c 65 73 20 6d  /* Both tables m
32741 75 73 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  ust have the sam
32742 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  e INTEGER PRIMAR
32743 59 20 4b 45 59 20 2a 2f 0d 0a 20 20 7d 0d 0a 20  Y KEY */..  }.. 
32744 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44 65 73   for(i=0; i<pDes
32745 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a  t->nCol; i++){..
32746 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61      if( pDest->a
32747 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 21  Col[i].affinity!
32748 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  =pSrc->aCol[i].a
32749 66 66 69 6e 69 74 79 20 29 7b 0d 0a 20 20 20 20  ffinity ){..    
3274a 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
3274b 2a 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20  * Affinity must 
3274c 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
3274d 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0d 0a 20  ll columns */.. 
3274e 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 21 78     }..    if( !x
3274f 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
32750 6c 61 74 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43  lation(pDest->aC
32751 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72  ol[i].zColl, pSr
32752 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c  c->aCol[i].zColl
32753 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  ) ){..      retu
32754 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c  rn 0;    /* Coll
32755 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d  ating sequence m
32756 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
32757 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a  on all columns *
32758 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  /..    }..    if
32759 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d  ( pDest->aCol[i]
3275a 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53 72  .notNull && !pSr
3275b 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  c->aCol[i].notNu
3275c 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  ll ){..      ret
3275d 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62  urn 0;    /* tab
3275e 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e 55  2 must be NOT NU
3275f 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a 2f  LL if tab1 is */
32760 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
32761 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65  for(pDestIdx=pDe
32762 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73  st->pIndex; pDes
32763 74 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70  tIdx; pDestIdx=p
32764 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b  DestIdx->pNext){
32765 0d 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 49  ..    if( pDestI
32766 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
32767 4e 6f 6e 65 20 29 7b 0d 0a 20 20 20 20 20 20 64  None ){..      d
32768 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20  estHasUniqueIdx 
32769 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  = 1;..    }..   
3276a 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70 53 72   for(pSrcIdx=pSr
3276b 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53 72 63 49  c->pIndex; pSrcI
3276c 64 78 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63  dx; pSrcIdx=pSrc
3276d 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20  Idx->pNext){..  
3276e 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d 70      if( xferComp
3276f 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65 73  atibleIndex(pDes
32770 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20 29  tIdx, pSrcIdx) )
32771 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
32772 20 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d      if( pSrcIdx=
32773 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  =0 ){..      ret
32774 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65  urn 0;    /* pDe
32775 73 74 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72  stIdx has no cor
32776 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78  responding index
32777 20 69 6e 20 70 53 72 63 20 2a 2f 0d 0a 20 20 20   in pSrc */..   
32778 20 7d 0d 0a 20 20 7d 0d 0a 23 69 66 6e 64 65 66   }..  }..#ifndef
32779 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
3277a 43 4b 0d 0a 20 20 69 66 28 20 70 44 65 73 74 2d  CK..  if( pDest-
3277b 3e 70 43 68 65 63 6b 20 26 26 20 73 71 6c 69 74  >pCheck && sqlit
3277c 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 53  e3ExprCompare(pS
3277d 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70 44 65 73  rc->pCheck, pDes
3277e 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0d 0a 20  t->pCheck) ){.. 
3277f 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
32780 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20 64 69  * Tables have di
32781 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20 63 6f  fferent CHECK co
32782 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69 63 6b  nstraints.  Tick
32783 65 74 20 23 32 32 35 32 20 2a 2f 0d 0a 20 20 7d  et #2252 */..  }
32784 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65  ..#endif..#ifnde
32785 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
32786 52 45 49 47 4e 5f 4b 45 59 0d 0a 20 20 2f 2a 20  REIGN_KEY..  /* 
32787 44 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61  Disallow the tra
32788 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
32789 6f 6e 20 69 66 20 74 68 65 20 64 65 73 74 69 6e  on if the destin
3278a 61 74 69 6f 6e 20 74 61 62 6c 65 20 63 6f 6e 73  ation table cons
3278b 74 61 69 6e 73 0d 0a 20 20 2a 2a 20 61 6e 79 20  tains..  ** any 
3278c 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
3278d 74 72 61 69 6e 74 73 2e 20 20 54 68 69 73 20 69  traints.  This i
3278e 73 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69  s more restricti
3278f 76 65 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72  ve than necessar
32790 79 2e 0d 0a 20 20 2a 2a 20 42 75 74 20 74 68 65  y...  ** But the
32791 20 6d 61 69 6e 20 62 65 6e 65 66 69 63 69 61 72   main beneficiar
32792 79 20 6f 66 20 74 68 65 20 74 72 61 6e 73 66 65  y of the transfe
32793 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  r optimization i
32794 73 20 74 68 65 20 56 41 43 55 55 4d 20 0d 0a 20  s the VACUUM .. 
32795 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2c 20 61 6e 64   ** command, and
32796 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d   the VACUUM comm
32797 61 6e 64 20 64 69 73 61 62 6c 65 73 20 66 6f 72  and disables for
32798 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
32799 69 6e 74 73 2e 20 20 53 6f 0d 0a 20 20 2a 2a 20  ints.  So..  ** 
3279a 74 68 65 20 65 78 74 72 61 20 63 6f 6d 70 6c 69  the extra compli
3279b 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 74  cation to make t
3279c 68 69 73 20 72 75 6c 65 20 6c 65 73 73 20 72 65  his rule less re
3279d 73 74 72 69 63 74 69 76 65 20 69 73 20 70 72 6f  strictive is pro
3279e 62 61 62 6c 79 0d 0a 20 20 2a 2a 20 6e 6f 74 20  bably..  ** not 
3279f 77 6f 72 74 68 20 74 68 65 20 65 66 66 6f 72 74  worth the effort
327a0 2e 20 20 54 69 63 6b 65 74 20 5b 36 32 38 34 64  .  Ticket [6284d
327a1 66 38 39 64 65 62 64 66 61 36 31 64 62 38 30 37  f89debdfa61db807
327a2 33 65 30 36 32 39 30 38 61 66 30 63 39 62 36 31  3e062908af0c9b61
327a3 31 38 65 5d 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  18e]..  */..  if
327a4 28 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  ( (pParse->db->f
327a5 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 6f  lags & SQLITE_Fo
327a6 72 65 69 67 6e 4b 65 79 73 29 21 3d 30 20 26 26  reignKeys)!=0 &&
327a7 20 70 44 65 73 74 2d 3e 70 46 4b 65 79 21 3d 30   pDest->pFKey!=0
327a8 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
327a9 30 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  0;..  }..#endif.
327aa 0a 20 20 69 66 28 20 28 70 50 61 72 73 65 2d 3e  .  if( (pParse->
327ab 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
327ac 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30  TE_CountRows)!=0
327ad 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
327ae 30 3b 20 20 2f 2a 20 78 66 65 72 20 6f 70 74 20  0;  /* xfer opt 
327af 64 6f 65 73 20 6e 6f 74 20 70 6c 61 79 20 77 65  does not play we
327b0 6c 6c 20 77 69 74 68 20 50 52 41 47 4d 41 20 63  ll with PRAGMA c
327b1 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 2a 2f 0d  ount_changes */.
327b2 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
327b3 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c  we get this far,
327b4 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
327b5 68 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61  he xfer optimiza
327b6 74 69 6f 6e 20 69 73 20 61 74 0d 0a 20 20 2a 2a  tion is at..  **
327b7 20 6c 65 61 73 74 20 61 20 70 6f 73 73 69 62 69   least a possibi
327b8 6c 69 74 79 2c 20 74 68 6f 75 67 68 20 69 74 20  lity, though it 
327b9 6d 69 67 68 74 20 6f 6e 6c 79 20 77 6f 72 6b 20  might only work 
327ba 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  if the destinati
327bb 6f 6e 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28  on..  ** table (
327bc 74 61 62 31 29 20 69 73 20 69 6e 69 74 69 61 6c  tab1) is initial
327bd 6c 79 20 65 6d 70 74 79 2e 0d 0a 20 20 2a 2f 0d  ly empty...  */.
327be 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
327bf 45 53 54 0d 0a 20 20 73 71 6c 69 74 65 33 5f 78  EST..  sqlite3_x
327c0 66 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0d  feropt_count++;.
327c1 0a 23 65 6e 64 69 66 0d 0a 20 20 69 44 62 53 72  .#endif..  iDbSr
327c2 63 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  c = sqlite3Schem
327c3 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
327c4 3e 64 62 2c 20 70 53 72 63 2d 3e 70 53 63 68 65  >db, pSrc->pSche
327c5 6d 61 29 3b 0d 0a 20 20 76 20 3d 20 73 71 6c 69  ma);..  v = sqli
327c6 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
327c7 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 43 6f  e);..  sqlite3Co
327c8 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70  deVerifySchema(p
327c9 50 61 72 73 65 2c 20 69 44 62 53 72 63 29 3b 0d  Parse, iDbSrc);.
327ca 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72 73 65  .  iSrc = pParse
327cb 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20 20 69 44 65  ->nTab++;..  iDe
327cc 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  st = pParse->nTa
327cd 62 2b 2b 3b 0d 0a 20 20 72 65 67 41 75 74 6f 69  b++;..  regAutoi
327ce 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69  nc = autoIncBegi
327cf 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73  n(pParse, iDbDes
327d0 74 2c 20 70 44 65 73 74 29 3b 0d 0a 20 20 73 71  t, pDest);..  sq
327d1 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
327d2 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44  Parse, iDest, iD
327d3 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50  bDest, pDest, OP
327d4 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0d 0a 20 20  _OpenWrite);..  
327d5 69 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65  if( (pDest->iPKe
327d6 79 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49  y<0 && pDest->pI
327d7 6e 64 65 78 21 3d 30 29 20 20 20 20 20 20 20 20  ndex!=0)        
327d8 20 20 2f 2a 20 28 31 29 20 2a 2f 0d 0a 20 20 20    /* (1) */..   
327d9 7c 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65  || destHasUnique
327da 49 64 78 20 20 20 20 20 20 20 20 20 20 20 20 20  Idx             
327db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327dc 20 2f 2a 20 28 32 29 20 2a 2f 0d 0a 20 20 20 7c   /* (2) */..   |
327dd 7c 20 28 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41  | (onError!=OE_A
327de 62 6f 72 74 20 26 26 20 6f 6e 45 72 72 6f 72 21  bort && onError!
327df 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 29 20 20 20  =OE_Rollback)   
327e0 2f 2a 20 28 33 29 20 2a 2f 0d 0a 20 20 29 7b 0d  /* (3) */..  ){.
327e1 0a 20 20 20 20 2f 2a 20 49 6e 20 73 6f 6d 65 20  .    /* In some 
327e2 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 77  circumstances, w
327e3 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 75  e are able to ru
327e4 6e 20 74 68 65 20 78 66 65 72 20 6f 70 74 69 6d  n the xfer optim
327e5 69 7a 61 74 69 6f 6e 0d 0a 20 20 20 20 2a 2a 20  ization..    ** 
327e6 6f 6e 6c 79 20 69 66 20 74 68 65 20 64 65 73 74  only if the dest
327e7 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65 20 69 73  ination table is
327e8 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79   initially empty
327e9 2e 20 20 54 68 69 73 20 63 6f 64 65 20 6d 61 6b  .  This code mak
327ea 65 73 0d 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  es..    ** that 
327eb 64 65 74 65 72 6d 69 6e 61 74 69 6f 6e 2e 20 20  determination.  
327ec 43 6f 6e 64 69 74 69 6f 6e 73 20 75 6e 64 65 72  Conditions under
327ed 20 77 68 69 63 68 20 74 68 65 20 64 65 73 74 69   which the desti
327ee 6e 61 74 69 6f 6e 20 6d 75 73 74 0d 0a 20 20 20  nation must..   
327ef 20 2a 2a 20 62 65 20 65 6d 70 74 79 3a 0d 0a 20   ** be empty:.. 
327f0 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 28 31     **..    ** (1
327f1 29 20 54 68 65 72 65 20 69 73 20 6e 6f 20 49 4e  ) There is no IN
327f2 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
327f3 59 20 62 75 74 20 74 68 65 72 65 20 61 72 65 20  Y but there are 
327f4 69 6e 64 69 63 65 73 2e 0d 0a 20 20 20 20 2a 2a  indices...    **
327f5 20 20 20 20 20 28 49 66 20 74 68 65 20 64 65 73       (If the des
327f6 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  tination is not 
327f7 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c  initially empty,
327f8 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64   the rowid field
327f9 73 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 6f 66  s..    **     of
327fa 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 6d   index entries m
327fb 69 67 68 74 20 6e 65 65 64 20 74 6f 20 63 68 61  ight need to cha
327fc 6e 67 65 2e 29 0d 0a 20 20 20 20 2a 2a 0d 0a 20  nge.)..    **.. 
327fd 20 20 20 2a 2a 20 28 32 29 20 54 68 65 20 64 65     ** (2) The de
327fe 73 74 69 6e 61 74 69 6f 6e 20 68 61 73 20 61 20  stination has a 
327ff 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 28  unique index.  (
32800 54 68 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a  The xfer optimiz
32801 61 74 69 6f 6e 20 0d 0a 20 20 20 20 2a 2a 20 20  ation ..    **  
32802 20 20 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20     is unable to 
32803 74 65 73 74 20 75 6e 69 71 75 65 6e 65 73 73 2e  test uniqueness.
32804 29 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  )..    **..    *
32805 2a 20 28 33 29 20 6f 6e 45 72 72 6f 72 20 69 73  * (3) onError is
32806 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72   something other
32807 20 74 68 61 6e 20 4f 45 5f 41 62 6f 72 74 20 61   than OE_Abort a
32808 6e 64 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 0d  nd OE_Rollback..
32809 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 64 64  .    */..    add
3280a 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
3280b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
3280c 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29 3b 0d  ind, iDest, 0);.
3280d 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65  .    emptyDestTe
3280e 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
3280f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
32810 6f 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 73  o, 0, 0);..    s
32811 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
32812 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0d 0a 20  re(v, addr1);.. 
32813 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 65 6d 70   }else{..    emp
32814 74 79 44 65 73 74 54 65 73 74 20 3d 20 30 3b 0d  tyDestTest = 0;.
32815 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 4f  .  }..  sqlite3O
32816 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
32817 20 69 53 72 63 2c 20 69 44 62 53 72 63 2c 20 70   iSrc, iDbSrc, p
32818 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  Src, OP_OpenRead
32819 29 3b 0d 0a 20 20 65 6d 70 74 79 53 72 63 54 65  );..  emptySrcTe
3281a 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
3281b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
3281c 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0d 0a  ind, iSrc, 0);..
3281d 20 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c 69    regData = sqli
3281e 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
3281f 61 72 73 65 29 3b 0d 0a 20 20 72 65 67 52 6f 77  arse);..  regRow
32820 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
32821 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
32822 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50  .  if( pDest->iP
32823 4b 65 79 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 61  Key>=0 ){..    a
32824 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
32825 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
32826 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52  owid, iSrc, regR
32827 6f 77 69 64 29 3b 0d 0a 20 20 20 20 61 64 64 72  owid);..    addr
32828 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
32829 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
3282a 78 69 73 74 73 2c 20 69 44 65 73 74 2c 20 30 2c  xists, iDest, 0,
3282b 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20   regRowid);..   
3282c 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
3282d 74 72 61 69 6e 74 28 0d 0a 20 20 20 20 20 20 20  traint(..       
3282e 20 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72   pParse, onError
3282f 2c 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d  , "PRIMARY KEY m
32830 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20  ust be unique", 
32831 50 34 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20  P4_STATIC);..   
32832 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
32833 48 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0d  Here(v, addr2);.
32834 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70  .    autoIncStep
32835 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f  (pParse, regAuto
32836 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d  inc, regRowid);.
32837 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65  .  }else if( pDe
32838 73 74 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b  st->pIndex==0 ){
32839 0d 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  ..    addr1 = sq
3283a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3283b 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
3283c 69 44 65 73 74 2c 20 72 65 67 52 6f 77 69 64 29  iDest, regRowid)
3283d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
3283e 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
3283f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
32840 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65  _Rowid, iSrc, re
32841 67 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 61 73  gRowid);..    as
32842 73 65 72 74 28 20 28 70 44 65 73 74 2d 3e 74 61  sert( (pDest->ta
32843 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
32844 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30 20 29 3b  increment)==0 );
32845 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
32846 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
32847 5f 52 6f 77 44 61 74 61 2c 20 69 53 72 63 2c 20  _RowData, iSrc, 
32848 72 65 67 44 61 74 61 29 3b 0d 0a 20 20 73 71 6c  regData);..  sql
32849 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
3284a 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65  , OP_Insert, iDe
3284b 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  st, regData, reg
3284c 52 6f 77 69 64 29 3b 0d 0a 20 20 73 71 6c 69 74  Rowid);..  sqlit
3284d 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
3284e 2c 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  , OPFLAG_NCHANGE
3284f 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49  |OPFLAG_LASTROWI
32850 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  D|OPFLAG_APPEND)
32851 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
32852 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
32853 70 44 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29  pDest->zName, 0)
32854 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
32855 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
32856 74 2c 20 69 53 72 63 2c 20 61 64 64 72 31 29 3b  t, iSrc, addr1);
32857 0d 0a 20 20 66 6f 72 28 70 44 65 73 74 49 64 78  ..  for(pDestIdx
32858 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20  =pDest->pIndex; 
32859 70 44 65 73 74 49 64 78 3b 20 70 44 65 73 74 49  pDestIdx; pDestI
3285a 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70 4e 65  dx=pDestIdx->pNe
3285b 78 74 29 7b 0d 0a 20 20 20 20 66 6f 72 28 70 53  xt){..    for(pS
3285c 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64  rcIdx=pSrc->pInd
3285d 65 78 3b 20 41 4c 57 41 59 53 28 70 53 72 63 49  ex; ALWAYS(pSrcI
3285e 64 78 29 3b 20 70 53 72 63 49 64 78 3d 70 53 72  dx); pSrcIdx=pSr
3285f 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20  cIdx->pNext){.. 
32860 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d       if( xferCom
32861 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65  patibleIndex(pDe
32862 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20  stIdx, pSrcIdx) 
32863 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d  ) break;..    }.
32864 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72  .    assert( pSr
32865 63 49 64 78 20 29 3b 0d 0a 20 20 20 20 73 71 6c  cIdx );..    sql
32866 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
32867 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63  , OP_Close, iSrc
32868 2c 20 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  , 0);..    sqlit
32869 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
3286a 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
3286b 20 30 29 3b 0d 0a 20 20 20 20 70 4b 65 79 20 3d   0);..    pKey =
3286c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
3286d 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 53 72  info(pParse, pSr
3286e 63 49 64 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69  cIdx);..    sqli
3286f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
32870 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53   OP_OpenRead, iS
32871 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74 6e 75  rc, pSrcIdx->tnu
32872 6d 2c 20 69 44 62 53 72 63 2c 0d 0a 20 20 20 20  m, iDbSrc,..    
32873 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32874 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50    (char*)pKey, P
32875 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
32876 46 29 3b 0d 0a 20 20 20 20 56 64 62 65 43 6f 6d  F);..    VdbeCom
32877 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
32878 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  SrcIdx->zName));
32879 0d 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c  ..    pKey = sql
3287a 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
3287b 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 49 64  (pParse, pDestId
3287c 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  x);..    sqlite3
3287d 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
3287e 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73  _OpenWrite, iDes
3287f 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75  t, pDestIdx->tnu
32880 6d 2c 20 69 44 62 44 65 73 74 2c 0d 0a 20 20 20  m, iDbDest,..   
32881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32882 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
32883 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
32884 46 46 29 3b 0d 0a 20 20 20 20 56 64 62 65 43 6f  FF);..    VdbeCo
32885 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
32886 70 44 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29  pDestIdx->zName)
32887 29 3b 0d 0a 20 20 20 20 61 64 64 72 31 20 3d 20  );..    addr1 = 
32888 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
32889 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
3288a 69 53 72 63 2c 20 30 29 3b 0d 0a 20 20 20 20 73  iSrc, 0);..    s
3288b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3288c 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 69  (v, OP_RowKey, i
3288d 53 72 63 2c 20 72 65 67 44 61 74 61 29 3b 0d 0a  Src, regData);..
3288e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3288f 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp3(v, OP_IdxI
32890 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 20 72 65  nsert, iDest, re
32891 67 44 61 74 61 2c 20 31 29 3b 0d 0a 20 20 20 20  gData, 1);..    
32892 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
32893 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 53  2(v, OP_Next, iS
32894 72 63 2c 20 61 64 64 72 31 2b 31 29 3b 0d 0a 20  rc, addr1+1);.. 
32895 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
32896 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
32897 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
32898 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
32899 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0d   emptySrcTest);.
3289a 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
3289b 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
3289c 20 72 65 67 52 6f 77 69 64 29 3b 0d 0a 20 20 73   regRowid);..  s
3289d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
3289e 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
3289f 44 61 74 61 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Data);..  sqlite
328a0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
328a1 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30  P_Close, iSrc, 0
328a2 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
328a3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
328a4 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b 0d  ose, iDest, 0);.
328a5 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65 73 74  .  if( emptyDest
328a6 54 65 73 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Test ){..    sql
328a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
328a8 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
328a9 45 5f 4f 4b 2c 20 30 29 3b 0d 0a 20 20 20 20 73  E_OK, 0);..    s
328aa 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
328ab 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54  re(v, emptyDestT
328ac 65 73 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  est);..    sqlit
328ad 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
328ae 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
328af 20 30 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e   0);..    return
328b0 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   0;..  }else{.. 
328b1 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
328b2 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  }..}..#endif /* 
328b3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
328b4 5f 4f 50 54 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a  _OPT */..../****
328b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
328b6 66 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a  f insert.c *****
328b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a  *********/../***
328ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
328bb 6e 20 66 69 6c 65 20 6c 65 67 61 63 79 2e 63 20  n file legacy.c 
328bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d  **********/../*.
328bf 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
328c0 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  er 15..**..** Th
328c1 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
328c2 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
328c3 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
328c4 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
328c5 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
328c6 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
328c7 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
328c8 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
328c9 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
328ca 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
328cb 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
328cc 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
328cd 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
328ce 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
328cf 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
328d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
328d1 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
328d2 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
328d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
328d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
328d7 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20  * Main file for 
328d8 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
328d9 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65  ry.  The routine
328da 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0d 0a  s in this file..
328db 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
328dc 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65   programmer inte
328dd 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62  rface to the lib
328de 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20  rary.  Routines 
328df 69 6e 0d 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c  in..** other fil
328e0 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
328e1 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74  nal use by SQLit
328e2 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74  e and should not
328e3 20 62 65 0d 0a 2a 2a 20 61 63 63 65 73 73 65 64   be..** accessed
328e4 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68 65   by users of the
328e5 20 6c 69 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 0d   library...*/...
328e6 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 65 63 75 74  .../*..** Execut
328e7 65 20 53 51 4c 20 63 6f 64 65 2e 20 20 52 65 74  e SQL code.  Ret
328e8 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53  urn one of the S
328e9 51 4c 49 54 45 5f 20 73 75 63 63 65 73 73 2f 66  QLITE_ success/f
328ea 61 69 6c 75 72 65 0d 0a 2a 2a 20 63 6f 64 65 73  ailure..** codes
328eb 2e 20 20 41 6c 73 6f 20 77 72 69 74 65 20 61 6e  .  Also write an
328ec 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
328ed 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
328ee 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 6d 61 6c  ned from..** mal
328ef 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
328f0 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
328f1 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65 2e 0d  o that message..
328f2 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 53  .**..** If the S
328f3 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c 20 74  QL is a query, t
328f4 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77  hen for each row
328f5 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72 65   in the query re
328f6 73 75 6c 74 0d 0a 2a 2a 20 74 68 65 20 78 43 61  sult..** the xCa
328f7 6c 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f  llback() functio
328f8 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70 41  n is called.  pA
328f9 72 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20 66  rg becomes the f
328fa 69 72 73 74 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e  irst..** argumen
328fb 74 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29  t to xCallback()
328fc 2e 20 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d  .  If xCallback=
328fd 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c  NULL then no cal
328fe 6c 62 61 63 6b 0d 0a 2a 2a 20 69 73 20 69 6e 76  lback..** is inv
328ff 6f 6b 65 64 2c 20 65 76 65 6e 20 66 6f 72 20 71  oked, even for q
32900 75 65 72 69 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ueries...*/..SQL
32901 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
32902 74 65 33 5f 65 78 65 63 28 0d 0a 20 20 73 71 6c  te3_exec(..  sql
32903 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
32904 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
32905 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63  database on whic
32906 68 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74  h the SQL execut
32907 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  es */..  const c
32908 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
32909 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20       /* The SQL 
3290a 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 2a  to be executed *
3290b 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 61 6c  /..  sqlite3_cal
3290c 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c  lback xCallback,
3290d 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73 20   /* Invoke this 
3290e 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
3290f 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72   */..  void *pAr
32910 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
32911 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
32912 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63  ment to xCallbac
32913 6b 28 29 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  k() */..  char *
32914 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
32915 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
32916 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
32917 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  re */..){..  int
32918 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
32919 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
3291a 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 63 6f  rn code */..  co
3291b 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 6f  nst char *zLefto
3291c 76 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 61 69  ver;      /* Tai
3291d 6c 20 6f 66 20 75 6e 70 72 6f 63 65 73 73 65 64  l of unprocessed
3291e 20 53 51 4c 20 2a 2f 0d 0a 20 20 73 71 6c 69 74   SQL */..  sqlit
3291f 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d  e3_stmt *pStmt =
32920 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75   0;    /* The cu
32921 72 72 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d  rrent SQL statem
32922 65 6e 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  ent */..  char *
32923 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20 20 20  *azCols = 0;    
32924 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 20 6f        /* Names o
32925 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
32926 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65 74 72   */..  int nRetr
32927 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  y = 0;          
32928 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
32929 72 65 74 72 79 20 61 74 74 65 6d 70 74 73 20 2a  retry attempts *
3292a 2f 0d 0a 20 20 69 6e 74 20 63 61 6c 6c 62 61 63  /..  int callbac
3292b 6b 49 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20  kIsInit;        
3292c 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 6c 6c   /* True if call
3292d 62 61 63 6b 20 64 61 74 61 20 69 73 20 69 6e 69  back data is ini
3292e 74 69 61 6c 69 7a 65 64 20 2a 2f 0d 0a 0d 0a 20  tialized */.... 
3292f 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
32930 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29  etyCheckOk(db) )
32931 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
32932 49 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20 69  ISUSE_BKPT;..  i
32933 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71  f( zSql==0 ) zSq
32934 6c 20 3d 20 22 22 3b 0d 0a 0d 0a 20 20 73 71 6c  l = "";....  sql
32935 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
32936 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
32937 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
32938 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0d   SQLITE_OK, 0);.
32939 0a 20 20 77 68 69 6c 65 28 20 28 72 63 3d 3d 53  .  while( (rc==S
3293a 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 72 63 3d  QLITE_OK || (rc=
3293b 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26  =SQLITE_SCHEMA &
3293c 26 20 28 2b 2b 6e 52 65 74 72 79 29 3c 32 29 29  & (++nRetry)<2))
3293d 20 26 26 20 7a 53 71 6c 5b 30 5d 20 29 7b 0d 0a   && zSql[0] ){..
3293e 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0d 0a 20      int nCol;.. 
3293f 20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 6c 73     char **azVals
32940 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 70 53 74   = 0;....    pSt
32941 6d 74 20 3d 20 30 3b 0d 0a 20 20 20 20 72 63 20  mt = 0;..    rc 
32942 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
32943 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  e(db, zSql, -1, 
32944 26 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f 76  &pStmt, &zLeftov
32945 65 72 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  er);..    assert
32946 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
32947 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0d 0a  || pStmt==0 );..
32948 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
32949 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
3294a 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 7d  continue;..    }
3294b 0d 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74  ..    if( !pStmt
3294c 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 74 68   ){..      /* th
3294d 69 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61  is happens for a
3294e 20 63 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74   comment or whit
3294f 65 2d 73 70 61 63 65 20 2a 2f 0d 0a 20 20 20 20  e-space */..    
32950 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76    zSql = zLeftov
32951 65 72 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 74 69  er;..      conti
32952 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  nue;..    }.... 
32953 20 20 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69     callbackIsIni
32954 74 20 3d 20 30 3b 0d 0a 20 20 20 20 6e 43 6f 6c  t = 0;..    nCol
32955 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
32956 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0d  n_count(pStmt);.
32957 0a 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 31 20  ...    while( 1 
32958 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  ){..      int i;
32959 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ..      rc = sql
3295a 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
3295b 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 6e  ;....      /* In
3295c 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
3295d 6b 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 72 65  k function if re
3295e 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 20 20 20  quired */..     
3295f 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 20 26   if( xCallback &
32960 26 20 28 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 72  & (SQLITE_ROW==r
32961 63 20 7c 7c 20 0d 0a 20 20 20 20 20 20 20 20 20  c || ..         
32962 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d 72   (SQLITE_DONE==r
32963 63 20 26 26 20 21 63 61 6c 6c 62 61 63 6b 49 73  c && !callbackIs
32964 49 6e 69 74 0d 0a 20 20 20 20 20 20 20 20 20 20  Init..          
32965 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32966 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51   && db->flags&SQ
32967 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63  LITE_NullCallbac
32968 6b 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  k)) ){..        
32969 69 66 28 20 21 63 61 6c 6c 62 61 63 6b 49 73 49  if( !callbackIsI
3296a 6e 69 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  nit ){..        
3296b 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74    azCols = sqlit
3296c 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
3296d 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66  b, 2*nCol*sizeof
3296e 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b 20  (const char*) + 
3296f 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  1);..          i
32970 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0d  f( azCols==0 ){.
32971 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
32972 6f 20 65 78 65 63 5f 6f 75 74 3b 0d 0a 20 20 20  o exec_out;..   
32973 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
32974 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
32975 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  Col; i++){..    
32976 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 73 5b 69          azCols[i
32977 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  ] = (char *)sqli
32978 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
32979 70 53 74 6d 74 2c 20 69 29 3b 0d 0a 20 20 20 20  pStmt, i);..    
3297a 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
3297b 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
3297c 28 29 20 69 6e 73 74 61 6c 6c 73 20 63 6f 6c 75  () installs colu
3297d 6d 6e 20 6e 61 6d 65 73 20 61 73 20 55 54 46 38  mn names as UTF8
3297e 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  ..            **
3297f 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 65 72   strings so ther
32980 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20  e is no way for 
32981 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
32982 61 6d 65 28 29 20 74 6f 20 66 61 69 6c 2e 20 2a  ame() to fail. *
32983 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  /..            a
32984 73 73 65 72 74 28 20 61 7a 43 6f 6c 73 5b 69 5d  ssert( azCols[i]
32985 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  !=0 );..        
32986 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 63    }..          c
32987 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20  allbackIsInit = 
32988 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  1;..        }.. 
32989 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
3298a 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20 20  QLITE_ROW ){..  
3298b 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
3298c 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0d   &azCols[nCol];.
3298d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
3298e 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
3298f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  {..            a
32990 7a 56 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72  zVals[i] = (char
32991 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
32992 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29  n_text(pStmt, i)
32993 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ;..            i
32994 66 28 20 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26  f( !azVals[i] &&
32995 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
32996 74 79 70 65 28 70 53 74 6d 74 2c 20 69 29 21 3d  type(pStmt, i)!=
32997 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0d 0a  SQLITE_NULL ){..
32998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
32999 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
3299a 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   1;..           
3299b 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74     goto exec_out
3299c 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
3299d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
3299e 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
3299f 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 28    if( xCallback(
329a0 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a 56 61  pArg, nCol, azVa
329a1 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29 7b 0d 0a  ls, azCols) ){..
329a2 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
329a3 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0d 0a 20 20  QLITE_ABORT;..  
329a4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
329a5 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
329a6 65 20 2a 29 70 53 74 6d 74 29 3b 0d 0a 20 20 20  e *)pStmt);..   
329a7 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30         pStmt = 0
329a8 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
329a9 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
329aa 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0d  LITE_ABORT, 0);.
329ab 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
329ac 65 78 65 63 5f 6f 75 74 3b 0d 0a 20 20 20 20 20  exec_out;..     
329ad 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d     }..      }...
329ae 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
329af 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20 20  QLITE_ROW ){..  
329b0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
329b1 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28  e3VdbeFinalize((
329b2 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0d 0a  Vdbe *)pStmt);..
329b3 20 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20          pStmt = 
329b4 30 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  0;..        if( 
329b5 72 63 21 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc!=SQLITE_SCHEM
329b6 41 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  A ){..          
329b7 6e 52 65 74 72 79 20 3d 20 30 3b 0d 0a 20 20 20  nRetry = 0;..   
329b8 20 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 7a 4c         zSql = zL
329b9 65 66 74 6f 76 65 72 3b 0d 0a 20 20 20 20 20 20  eftover;..      
329ba 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
329bb 65 33 49 73 73 70 61 63 65 28 7a 53 71 6c 5b 30  e3Isspace(zSql[0
329bc 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0d 0a 20 20  ]) ) zSql++;..  
329bd 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
329be 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
329bf 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73  ..    }....    s
329c0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
329c1 20 61 7a 43 6f 6c 73 29 3b 0d 0a 20 20 20 20 61   azCols);..    a
329c2 7a 43 6f 6c 73 20 3d 20 30 3b 0d 0a 20 20 7d 0d  zCols = 0;..  }.
329c3 0a 0d 0a 65 78 65 63 5f 6f 75 74 3a 0d 0a 20 20  ...exec_out:..  
329c4 69 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69  if( pStmt ) sqli
329c5 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
329c6 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0d  (Vdbe *)pStmt);.
329c7 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
329c8 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0d 0a 0d  (db, azCols);...
329c9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
329ca 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0d  piExit(db, rc);.
329cb 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
329cc 45 5f 4f 4b 20 26 26 20 41 4c 57 41 59 53 28 72  E_OK && ALWAYS(r
329cd 63 3d 3d 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  c==sqlite3_errco
329ce 64 65 28 64 62 29 29 20 26 26 20 70 7a 45 72 72  de(db)) && pzErr
329cf 4d 73 67 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  Msg ){..    int 
329d0 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b 20 73 71  nErrMsg = 1 + sq
329d1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71  lite3Strlen30(sq
329d2 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29  lite3_errmsg(db)
329d3 29 3b 0d 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73  );..    *pzErrMs
329d4 67 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  g = sqlite3Mallo
329d5 63 28 6e 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20  c(nErrMsg);..   
329d6 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20 29   if( *pzErrMsg )
329d7 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  {..      memcpy(
329d8 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74  *pzErrMsg, sqlit
329d9 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e  e3_errmsg(db), n
329da 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 7d 65  ErrMsg);..    }e
329db 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  lse{..      rc =
329dc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
329dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
329de 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f  or(db, SQLITE_NO
329df 4d 45 4d 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 0d  MEM, 0);..    }.
329e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45  .  }else if( pzE
329e1 72 72 4d 73 67 20 29 7b 0d 0a 20 20 20 20 2a 70  rrMsg ){..    *p
329e2 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d 0a 20 20  zErrMsg = 0;..  
329e3 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 28  }....  assert( (
329e4 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  rc&db->errMask)=
329e5 3d 72 63 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  =rc );..  sqlite
329e6 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
329e7 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74  ->mutex);..  ret
329e8 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
329e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
329ea 64 20 6f 66 20 6c 65 67 61 63 79 2e 63 20 2a 2a  d of legacy.c **
329eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
329ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
329ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
329ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
329ef 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61 64 65 78  egin file loadex
329f0 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
329f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
329f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
329f3 2f 2a 0d 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65  /*..** 2006 June
329f4 20 37 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61   7..**..** The a
329f5 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
329f6 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
329f7 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
329f8 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
329f9 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
329fa 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
329fb 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
329fc 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
329fd 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
329fe 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
329ff 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
32a00 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
32a01 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
32a02 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
32a03 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
32a04 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
32a05 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
32a06 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
32a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54  **********..** T
32a0b 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
32a0c 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 64  s code used to d
32a0d 79 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20  ynamically load 
32a0e 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0d  extensions into.
32a0f 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c  .** the SQLite l
32a10 69 62 72 61 72 79 2e 0d 0a 2a 2f 0d 0a 0d 0a 23  ibrary...*/....#
32a11 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
32a12 52 45 0d 0a 20 20 23 64 65 66 69 6e 65 20 53 51  RE..  #define SQ
32a13 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a 20  LITE_CORE 1  /* 
32a14 44 69 73 61 62 6c 65 20 74 68 65 20 41 50 49 20  Disable the API 
32a15 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e 20  redefinition in 
32a16 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f 0d  sqlite3ext.h */.
32a17 0a 23 65 6e 64 69 66 0d 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
32a18 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
32a19 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e   sqlite3ext.h in
32a1a 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c   the middle of l
32a1b 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
32a1c 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
32a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
32a1e 66 69 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e  file sqlite3ext.
32a1f 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
32a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a21 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
32a22 2a 20 32 30 30 36 20 4a 75 6e 65 20 37 0d 0a 2a  * 2006 June 7..*
32a23 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
32a24 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
32a25 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
32a26 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
32a27 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
32a28 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
32a29 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
32a2a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
32a2b 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
32a2c 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
32a2d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
32a2e 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
32a2f 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
32a30 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
32a31 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
32a32 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
32a33 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
32a34 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
32a35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a39 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 68  *****..** This h
32a3a 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e  eader file defin
32a3b 65 73 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e  es the SQLite in
32a3c 74 65 72 66 61 63 65 20 66 6f 72 20 75 73 65 20  terface for use 
32a3d 62 79 0d 0a 2a 2a 20 73 68 61 72 65 64 20 6c 69  by..** shared li
32a3e 62 72 61 72 69 65 73 20 74 68 61 74 20 77 61 6e  braries that wan
32a3f 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74 65 64  t to be imported
32a40 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20 69   as extensions i
32a41 6e 74 6f 0d 0a 2a 2a 20 61 6e 20 53 51 4c 69 74  nto..** an SQLit
32a42 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68 61  e instance.  Sha
32a43 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74 68  red libraries th
32a44 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65 20  at intend to be 
32a45 6c 6f 61 64 65 64 0d 0a 2a 2a 20 61 73 20 65 78  loaded..** as ex
32a46 74 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69  tensions by SQLi
32a47 74 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  te should #inclu
32a48 64 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73  de this file ins
32a49 74 65 61 64 20 6f 66 20 0d 0a 2a 2a 20 73 71 6c  tead of ..** sql
32a4a 69 74 65 33 2e 68 2e 0d 0a 2a 2f 0d 0a 23 69 66  ite3.h...*/..#if
32a4b 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58 54  ndef _SQLITE3EXT
32a4c 5f 48 5f 0d 0a 23 64 65 66 69 6e 65 20 5f 53 51  _H_..#define _SQ
32a4d 4c 49 54 45 33 45 58 54 5f 48 5f 0d 0a 0d 0a 74  LITE3EXT_H_....t
32a4e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
32a4f 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
32a50 65 73 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  es sqlite3_api_r
32a51 6f 75 74 69 6e 65 73 3b 0d 0a 0d 0a 2f 2a 0d 0a  outines;..../*..
32a52 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
32a53 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
32a54 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c   pointers to all
32a55 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 41   of the SQLite A
32a56 50 49 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e  PI..** routines.
32a57 0d 0a 2a 2a 0d 0a 2a 2a 20 57 41 52 4e 49 4e 47  ..**..** WARNING
32a58 3a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  :  In order to m
32a59 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64  aintain backward
32a5a 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c  s compatibility,
32a5b 20 61 64 64 20 6e 65 77 0d 0a 2a 2a 20 69 6e 74   add new..** int
32a5c 65 72 66 61 63 65 73 20 74 6f 20 74 68 65 20 65  erfaces to the e
32a5d 6e 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  nd of this struc
32a5e 74 75 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20 79  ture only.  If y
32a5f 6f 75 20 69 6e 73 65 72 74 20 6e 65 77 0d 0a 2a  ou insert new..*
32a60 2a 20 69 6e 74 65 72 66 61 63 65 73 20 69 6e 20  * interfaces in 
32a61 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68  the middle of th
32a62 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  is structure, th
32a63 65 6e 20 6f 6c 64 65 72 20 64 69 66 66 65 72 65  en older differe
32a64 6e 74 0d 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20  nt..** versions 
32a65 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  of SQLite will n
32a66 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 6c 6f  ot be able to lo
32a67 61 64 20 65 61 63 68 20 6f 74 68 65 72 73 27 20  ad each others' 
32a68 73 68 61 72 65 64 0d 0a 2a 2a 20 6c 69 62 72 61  shared..** libra
32a69 72 69 65 73 21 0d 0a 2a 2f 0d 0a 73 74 72 75 63  ries!..*/..struc
32a6a 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
32a6b 75 74 69 6e 65 73 20 7b 0d 0a 20 20 76 6f 69 64  utines {..  void
32a6c 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63   * (*aggregate_c
32a6d 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  ontext)(sqlite3_
32a6e 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79  context*,int nBy
32a6f 74 65 73 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a  tes);..  int  (*
32a70 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29  aggregate_count)
32a71 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
32a72 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 62 69  *);..  int  (*bi
32a73 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  nd_blob)(sqlite3
32a74 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
32a75 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69   void*,int n,voi
32a76 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 20  d(*)(void*));.. 
32a77 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75   int  (*bind_dou
32a78 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ble)(sqlite3_stm
32a79 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0d  t*,int,double);.
32a7a 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
32a7b 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
32a7c 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0d 0a 20 20 69  *,int,int);..  i
32a7d 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34  nt  (*bind_int64
32a7e 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
32a7f 69 6e 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  int,sqlite_int64
32a80 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  );..  int  (*bin
32a81 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f  d_null)(sqlite3_
32a82 73 74 6d 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 69  stmt*,int);..  i
32a83 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  nt  (*bind_param
32a84 65 74 65 72 5f 63 6f 75 6e 74 29 28 73 71 6c 69  eter_count)(sqli
32a85 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a 20 20 69  te3_stmt*);..  i
32a86 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  nt  (*bind_param
32a87 65 74 65 72 5f 69 6e 64 65 78 29 28 73 71 6c 69  eter_index)(sqli
32a88 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74 20  te3_stmt*,const 
32a89 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0d 0a 20 20  char*zName);..  
32a8a 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 62  const char * (*b
32a8b 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
32a8c 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
32a8d 2a 2c 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 20  *,int);..  int  
32a8e 28 2a 62 69 6e 64 5f 74 65 78 74 29 28 73 71 6c  (*bind_text)(sql
32a8f 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63  ite3_stmt*,int,c
32a90 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 20 6e  onst char*,int n
32a91 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
32a92 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  ;..  int  (*bind
32a93 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33  _text16)(sqlite3
32a94 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
32a95 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28   void*,int,void(
32a96 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 69  *)(void*));..  i
32a97 6e 74 20 20 28 2a 62 69 6e 64 5f 76 61 6c 75 65  nt  (*bind_value
32a98 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
32a99 69 6e 74 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  int,const sqlite
32a9a 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a 20 20 69 6e  3_value*);..  in
32a9b 74 20 20 28 2a 62 75 73 79 5f 68 61 6e 64 6c 65  t  (*busy_handle
32a9c 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28  r)(sqlite3*,int(
32a9d 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f  *)(void*,int),vo
32a9e 69 64 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a  id*);..  int  (*
32a9f 62 75 73 79 5f 74 69 6d 65 6f 75 74 29 28 73 71  busy_timeout)(sq
32aa0 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29 3b 0d  lite3*,int ms);.
32aa1 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e 67 65  .  int  (*change
32aa2 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 20  s)(sqlite3*);.. 
32aa3 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28 73   int  (*close)(s
32aa4 71 6c 69 74 65 33 2a 29 3b 0d 0a 20 20 69 6e 74  qlite3*);..  int
32aa5 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65    (*collation_ne
32aa6 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76  eded)(sqlite3*,v
32aa7 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  oid*,void(*)(voi
32aa8 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 0d 0a 20 20  d*,sqlite3*,..  
32aa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32aaa 20 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 65           int eTe
32aab 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
32aac 2a 29 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 63  *));..  int  (*c
32aad 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
32aae 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  6)(sqlite3*,void
32aaf 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
32ab0 73 71 6c 69 74 65 33 2a 2c 0d 0a 20 20 20 20 20  sqlite3*,..     
32ab1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ab2 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 65 78          int eTex
32ab3 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
32ab4 29 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69  ));..  const voi
32ab5 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c 6f  d * (*column_blo
32ab6 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  b)(sqlite3_stmt*
32ab7 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 20 20 69  ,int iCol);..  i
32ab8 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
32ab9 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  es)(sqlite3_stmt
32aba 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 20 20  *,int iCol);..  
32abb 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79  int  (*column_by
32abc 74 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73  tes16)(sqlite3_s
32abd 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d  tmt*,int iCol);.
32abe 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
32abf 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
32ac0 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0d 0a 20 20  stmt*pStmt);..  
32ac1 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
32ac2 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
32ac3 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
32ac4 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73  t*,int);..  cons
32ac5 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
32ac6 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
32ac7 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
32ac8 2c 69 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73 74 20  ,int);..  const 
32ac9 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
32aca 64 65 63 6c 74 79 70 65 29 28 73 71 6c 69 74 65  decltype)(sqlite
32acb 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 29 3b 0d  3_stmt*,int i);.
32acc 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
32acd 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  (*column_decltyp
32ace 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
32acf 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 64 6f 75 62  t*,int);..  doub
32ad0 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64 6f 75  le  (*column_dou
32ad1 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ble)(sqlite3_stm
32ad2 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 20  t*,int iCol);.. 
32ad3 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69   int  (*column_i
32ad4 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
32ad5 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 20 20  *,int iCol);..  
32ad6 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a  sqlite_int64  (*
32ad7 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71  column_int64)(sq
32ad8 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
32ad9 69 43 6f 6c 29 3b 0d 0a 20 20 63 6f 6e 73 74 20  iCol);..  const 
32ada 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
32adb 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74  name)(sqlite3_st
32adc 6d 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 63 6f 6e  mt*,int);..  con
32add 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
32ade 6d 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74  mn_name16)(sqlit
32adf 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0d 0a  e3_stmt*,int);..
32ae0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
32ae1 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  *column_origin_n
32ae2 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
32ae3 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73  t*,int);..  cons
32ae4 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
32ae5 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 29  n_origin_name16)
32ae6 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
32ae7 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  nt);..  const ch
32ae8 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61  ar * (*column_ta
32ae9 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  ble_name)(sqlite
32aea 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0d 0a 20  3_stmt*,int);.. 
32aeb 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
32aec 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
32aed 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
32aee 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73  t*,int);..  cons
32aef 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
32af0 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29  * (*column_text)
32af1 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
32af2 6e 74 20 69 43 6f 6c 29 3b 0d 0a 20 20 63 6f 6e  nt iCol);..  con
32af3 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
32af4 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  mn_text16)(sqlit
32af5 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
32af6 6c 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 63 6f  l);..  int  (*co
32af7 6c 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74  lumn_type)(sqlit
32af8 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
32af9 6c 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  l);..  sqlite3_v
32afa 61 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76  alue* (*column_v
32afb 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
32afc 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a  mt*,int iCol);..
32afd 20 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69    void * (*commi
32afe 74 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a  t_hook)(sqlite3*
32aff 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76  ,int(*)(void*),v
32b00 6f 69 64 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28  oid*);..  int  (
32b01 2a 63 6f 6d 70 6c 65 74 65 29 28 63 6f 6e 73 74  *complete)(const
32b02 20 63 68 61 72 2a 73 71 6c 29 3b 0d 0a 20 20 69   char*sql);..  i
32b03 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 31 36  nt  (*complete16
32b04 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 73 71 6c  )(const void*sql
32b05 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  );..  int  (*cre
32b06 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29 28 73  ate_collation)(s
32b07 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
32b08 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 0d 0a  ar*,int,void*,..
32b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b0a 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 28 2a             int(*
32b0b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
32b0c 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
32b0d 74 20 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 69 6e  t void*));..  in
32b0e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c  t  (*create_coll
32b0f 61 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33  ation16)(sqlite3
32b10 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
32b11 74 2c 76 6f 69 64 2a 2c 0d 0a 20 20 20 20 20 20  t,void*,..      
32b12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b13 20 20 20 20 20 20 20 69 6e 74 28 2a 29 28 76 6f         int(*)(vo
32b14 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
32b15 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
32b16 69 64 2a 29 29 3b 0d 0a 20 20 69 6e 74 20 20 28  id*));..  int  (
32b17 2a 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  *create_function
32b18 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
32b19 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76   char*,int,int,v
32b1a 6f 69 64 2a 2c 0d 0a 20 20 20 20 20 20 20 20 20  oid*,..         
32b1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b1c 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
32b1d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
32b1e 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
32b1f 65 2a 2a 29 2c 0d 0a 20 20 20 20 20 20 20 20 20  e**),..         
32b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b21 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
32b22 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
32b23 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
32b24 65 2a 2a 29 2c 0d 0a 20 20 20 20 20 20 20 20 20  e**),..         
32b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b26 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
32b27 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32b28 29 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 63 72  ));..  int  (*cr
32b29 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29  eate_function16)
32b2a 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
32b2b 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f  void*,int,int,vo
32b2c 69 64 2a 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  id*,..          
32b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b2e 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
32b2f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32b30 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
32b31 75 65 2a 2a 29 2c 0d 0a 20 20 20 20 20 20 20 20  ue**),..        
32b32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b33 20 20 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70      void (*xStep
32b34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
32b35 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
32b36 61 6c 75 65 2a 2a 29 2c 0d 0a 20 20 20 20 20 20  alue**),..      
32b37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b38 20 20 20 20 20 20 76 6f 69 64 20 28 2a 78 46 69        void (*xFi
32b39 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
32b3a 74 65 78 74 2a 29 29 3b 0d 0a 20 20 69 6e 74 20  text*));..  int 
32b3b 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 29  (*create_module)
32b3c 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
32b3d 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  char*,const sqli
32b3e 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64  te3_module*,void
32b3f 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 64 61  *);..  int  (*da
32b40 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  ta_count)(sqlite
32b41 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0d 0a  3_stmt*pStmt);..
32b42 20 20 73 71 6c 69 74 65 33 20 2a 20 28 2a 64 62    sqlite3 * (*db
32b43 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65 33  _handle)(sqlite3
32b44 5f 73 74 6d 74 2a 29 3b 0d 0a 20 20 69 6e 74 20  _stmt*);..  int 
32b45 28 2a 64 65 63 6c 61 72 65 5f 76 74 61 62 29 28  (*declare_vtab)(
32b46 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
32b47 68 61 72 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28  har*);..  int  (
32b48 2a 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  *enable_shared_c
32b49 61 63 68 65 29 28 69 6e 74 29 3b 0d 0a 20 20 69  ache)(int);..  i
32b4a 6e 74 20 20 28 2a 65 72 72 63 6f 64 65 29 28 73  nt  (*errcode)(s
32b4b 71 6c 69 74 65 33 2a 64 62 29 3b 0d 0a 20 20 63  qlite3*db);..  c
32b4c 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 65 72  onst char * (*er
32b4d 72 6d 73 67 29 28 73 71 6c 69 74 65 33 2a 29 3b  rmsg)(sqlite3*);
32b4e 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ..  const void *
32b4f 20 28 2a 65 72 72 6d 73 67 31 36 29 28 73 71 6c   (*errmsg16)(sql
32b50 69 74 65 33 2a 29 3b 0d 0a 20 20 69 6e 74 20 20  ite3*);..  int  
32b51 28 2a 65 78 65 63 29 28 73 71 6c 69 74 65 33 2a  (*exec)(sqlite3*
32b52 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
32b53 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f  ite3_callback,vo
32b54 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0d 0a 20 20  id*,char**);..  
32b55 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64 29 28  int  (*expired)(
32b56 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d  sqlite3_stmt*);.
32b57 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c 69  .  int  (*finali
32b58 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ze)(sqlite3_stmt
32b59 2a 70 53 74 6d 74 29 3b 0d 0a 20 20 76 6f 69 64  *pStmt);..  void
32b5a 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29    (*free)(void*)
32b5b 3b 0d 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  ;..  void  (*fre
32b5c 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72  e_table)(char**r
32b5d 65 73 75 6c 74 29 3b 0d 0a 20 20 69 6e 74 20 20  esult);..  int  
32b5e 28 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  (*get_autocommit
32b5f 29 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 20 20  )(sqlite3*);..  
32b60 76 6f 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78  void * (*get_aux
32b61 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f  data)(sqlite3_co
32b62 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20  ntext*,int);..  
32b63 69 6e 74 20 20 28 2a 67 65 74 5f 74 61 62 6c 65  int  (*get_table
32b64 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
32b65 20 63 68 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69   char*,char***,i
32b66 6e 74 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29  nt*,int*,char**)
32b67 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 67 6c 6f 62  ;..  int  (*glob
32b68 61 6c 5f 72 65 63 6f 76 65 72 29 28 76 6f 69 64  al_recover)(void
32b69 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28 2a 69 6e  );..  void  (*in
32b6a 74 65 72 72 75 70 74 78 29 28 73 71 6c 69 74 65  terruptx)(sqlite
32b6b 33 2a 29 3b 0d 0a 20 20 73 71 6c 69 74 65 5f 69  3*);..  sqlite_i
32b6c 6e 74 36 34 20 20 28 2a 6c 61 73 74 5f 69 6e 73  nt64  (*last_ins
32b6d 65 72 74 5f 72 6f 77 69 64 29 28 73 71 6c 69 74  ert_rowid)(sqlit
32b6e 65 33 2a 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  e3*);..  const c
32b6f 68 61 72 20 2a 20 28 2a 6c 69 62 76 65 72 73 69  har * (*libversi
32b70 6f 6e 29 28 76 6f 69 64 29 3b 0d 0a 20 20 69 6e  on)(void);..  in
32b71 74 20 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f  t  (*libversion_
32b72 6e 75 6d 62 65 72 29 28 76 6f 69 64 29 3b 0d 0a  number)(void);..
32b73 20 20 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63    void *(*malloc
32b74 29 28 69 6e 74 29 3b 0d 0a 20 20 63 68 61 72 20  )(int);..  char 
32b75 2a 20 28 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e  * (*mprintf)(con
32b76 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0d 0a  st char*,...);..
32b77 20 20 69 6e 74 20 20 28 2a 6f 70 65 6e 29 28 63    int  (*open)(c
32b78 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
32b79 65 33 2a 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28  e3**);..  int  (
32b7a 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73 74 20 76  *open16)(const v
32b7b 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b  oid*,sqlite3**);
32b7c 0d 0a 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61  ..  int  (*prepa
32b7d 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  re)(sqlite3*,con
32b7e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c  st char*,int,sql
32b7f 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
32b80 74 20 63 68 61 72 2a 2a 29 3b 0d 0a 20 20 69 6e  t char**);..  in
32b81 74 20 20 28 2a 70 72 65 70 61 72 65 31 36 29 28  t  (*prepare16)(
32b82 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
32b83 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  oid*,int,sqlite3
32b84 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f  _stmt**,const vo
32b85 69 64 2a 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 2a  id**);..  void *
32b86 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69   (*profile)(sqli
32b87 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
32b88 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  d*,const char*,s
32b89 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f  qlite_uint64),vo
32b8a 69 64 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28  id*);..  void  (
32b8b 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  *progress_handle
32b8c 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  r)(sqlite3*,int,
32b8d 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  int(*)(void*),vo
32b8e 69 64 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 2a 28  id*);..  void *(
32b8f 2a 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c  *realloc)(void*,
32b90 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a  int);..  int  (*
32b91 72 65 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73  reset)(sqlite3_s
32b92 74 6d 74 2a 70 53 74 6d 74 29 3b 0d 0a 20 20 76  tmt*pStmt);..  v
32b93 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c  oid  (*result_bl
32b94 6f 62 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ob)(sqlite3_cont
32b95 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
32b96 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
32b97 64 2a 29 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28  d*));..  void  (
32b98 2a 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 29 28  *result_double)(
32b99 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32b9a 2c 64 6f 75 62 6c 65 29 3b 0d 0a 20 20 76 6f 69  ,double);..  voi
32b9b 64 20 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f  d  (*result_erro
32b9c 72 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  r)(sqlite3_conte
32b9d 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
32b9e 69 6e 74 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28  int);..  void  (
32b9f 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 29  *result_error16)
32ba0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
32ba1 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
32ba2 74 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28 2a 72  t);..  void  (*r
32ba3 65 73 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69 74  esult_int)(sqlit
32ba4 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29  e3_context*,int)
32ba5 3b 0d 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  ;..  void  (*res
32ba6 75 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  ult_int64)(sqlit
32ba7 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
32ba8 74 65 5f 69 6e 74 36 34 29 3b 0d 0a 20 20 76 6f  te_int64);..  vo
32ba9 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75 6c  id  (*result_nul
32baa 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
32bab 78 74 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28  xt*);..  void  (
32bac 2a 72 65 73 75 6c 74 5f 74 65 78 74 29 28 73 71  *result_text)(sq
32bad 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
32bae 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76  onst char*,int,v
32baf 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0d  oid(*)(void*));.
32bb0 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
32bb1 74 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65  t_text16)(sqlite
32bb2 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
32bb3 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28   void*,int,void(
32bb4 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 76  *)(void*));..  v
32bb5 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
32bb6 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f  xt16be)(sqlite3_
32bb7 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
32bb8 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
32bb9 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 76 6f 69  (void*));..  voi
32bba 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74  d  (*result_text
32bbb 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f  16le)(sqlite3_co
32bbc 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69  ntext*,const voi
32bbd 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  d*,int,void(*)(v
32bbe 6f 69 64 2a 29 29 3b 0d 0a 20 20 76 6f 69 64 20  oid*));..  void 
32bbf 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65 29   (*result_value)
32bc0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
32bc1 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  *,sqlite3_value*
32bc2 29 3b 0d 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72  );..  void * (*r
32bc3 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71  ollback_hook)(sq
32bc4 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
32bc5 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0d 0a 20  oid*),void*);.. 
32bc6 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68   int  (*set_auth
32bc7 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a  orizer)(sqlite3*
32bc8 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
32bc9 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
32bca 6e 73 74 20 63 68 61 72 2a 2c 0d 0a 20 20 20 20  nst char*,..    
32bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bcc 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 2a       const char*
32bcd 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f  ,const char*),vo
32bce 69 64 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 20 28  id*);..  void  (
32bcf 2a 73 65 74 5f 61 75 78 64 61 74 61 29 28 73 71  *set_auxdata)(sq
32bd0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
32bd1 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a  nt,void*,void (*
32bd2 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 63 68  )(void*));..  ch
32bd3 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74 66 29  ar * (*snprintf)
32bd4 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74  (int,char*,const
32bd5 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0d 0a 20 20   char*,...);..  
32bd6 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73 71 6c  int  (*step)(sql
32bd7 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a 20 20  ite3_stmt*);..  
32bd8 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c  int  (*table_col
32bd9 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73 71  umn_metadata)(sq
32bda 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
32bdb 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
32bdc 6f 6e 73 74 20 63 68 61 72 2a 2c 0d 0a 20 20 20  onst char*,..   
32bdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bde 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61               cha
32bdf 72 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63  r const**,char c
32be0 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  onst**,int*,int*
32be1 2c 69 6e 74 2a 29 3b 0d 0a 20 20 76 6f 69 64 20  ,int*);..  void 
32be2 20 28 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75   (*thread_cleanu
32be3 70 29 28 76 6f 69 64 29 3b 0d 0a 20 20 69 6e 74  p)(void);..  int
32be4 20 20 28 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65    (*total_change
32be5 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 20  s)(sqlite3*);.. 
32be6 20 76 6f 69 64 20 2a 20 28 2a 74 72 61 63 65 29   void * (*trace)
32be7 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a  (sqlite3*,void(*
32be8 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f  xTrace)(void*,co
32be9 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a  nst char*),void*
32bea 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 74 72 61  );..  int  (*tra
32beb 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 29 28  nsfer_bindings)(
32bec 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 73 71  sqlite3_stmt*,sq
32bed 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a 20  lite3_stmt*);.. 
32bee 20 76 6f 69 64 20 2a 20 28 2a 75 70 64 61 74 65   void * (*update
32bef 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
32bf0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  void(*)(void*,in
32bf1 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 63  t ,char const*,c
32bf2 68 61 72 20 63 6f 6e 73 74 2a 2c 0d 0a 20 20 20  har const*,..   
32bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bf5 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
32bf6 36 34 29 2c 76 6f 69 64 2a 29 3b 0d 0a 20 20 76  64),void*);..  v
32bf7 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74  oid * (*user_dat
32bf8 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
32bf9 78 74 2a 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 76  xt*);..  const v
32bfa 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c  oid * (*value_bl
32bfb 6f 62 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob)(sqlite3_valu
32bfc 65 2a 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 76  e*);..  int  (*v
32bfd 61 6c 75 65 5f 62 79 74 65 73 29 28 73 71 6c 69  alue_bytes)(sqli
32bfe 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a 20 20  te3_value*);..  
32bff 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74  int  (*value_byt
32c00 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61  es16)(sqlite3_va
32c01 6c 75 65 2a 29 3b 0d 0a 20 20 64 6f 75 62 6c 65  lue*);..  double
32c02 20 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62 6c 65    (*value_double
32c03 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
32c04 29 3b 0d 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  );..  int  (*val
32c05 75 65 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f  ue_int)(sqlite3_
32c06 76 61 6c 75 65 2a 29 3b 0d 0a 20 20 73 71 6c 69  value*);..  sqli
32c07 74 65 5f 69 6e 74 36 34 20 20 28 2a 76 61 6c 75  te_int64  (*valu
32c08 65 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33  e_int64)(sqlite3
32c09 5f 76 61 6c 75 65 2a 29 3b 0d 0a 20 20 69 6e 74  _value*);..  int
32c0a 20 20 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72 69    (*value_numeri
32c0b 63 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  c_type)(sqlite3_
32c0c 76 61 6c 75 65 2a 29 3b 0d 0a 20 20 63 6f 6e 73  value*);..  cons
32c0d 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
32c0e 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 29 28  * (*value_text)(
32c0f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
32c10 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ..  const void *
32c11 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 29   (*value_text16)
32c12 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
32c13 3b 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  ;..  const void 
32c14 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36  * (*value_text16
32c15 62 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  be)(sqlite3_valu
32c16 65 2a 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 76 6f  e*);..  const vo
32c17 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
32c18 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 76  t16le)(sqlite3_v
32c19 61 6c 75 65 2a 29 3b 0d 0a 20 20 69 6e 74 20 20  alue*);..  int  
32c1a 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28 73 71  (*value_type)(sq
32c1b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0d 0a  lite3_value*);..
32c1c 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e    char *(*vmprin
32c1d 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  tf)(const char*,
32c1e 76 61 5f 6c 69 73 74 29 3b 0d 0a 20 20 2f 2a 20  va_list);..  /* 
32c1f 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0d 0a 20 20  Added ??? */..  
32c20 69 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66  int (*overload_f
32c21 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33  unction)(sqlite3
32c22 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
32c23 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41  FuncName, int nA
32c24 72 67 29 3b 0d 0a 20 20 2f 2a 20 41 64 64 65 64  rg);..  /* Added
32c25 20 62 79 20 33 2e 33 2e 31 33 20 2a 2f 0d 0a 20   by 3.3.13 */.. 
32c26 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 5f 76   int (*prepare_v
32c27 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  2)(sqlite3*,cons
32c28 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69  t char*,int,sqli
32c29 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74  te3_stmt**,const
32c2a 20 63 68 61 72 2a 2a 29 3b 0d 0a 20 20 69 6e 74   char**);..  int
32c2b 20 28 2a 70 72 65 70 61 72 65 31 36 5f 76 32 29   (*prepare16_v2)
32c2c 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
32c2d 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
32c2e 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
32c2f 6f 69 64 2a 2a 29 3b 0d 0a 20 20 69 6e 74 20 28  oid**);..  int (
32c30 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 29  *clear_bindings)
32c31 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
32c32 0d 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20  ..  /* Added by 
32c33 33 2e 34 2e 31 20 2a 2f 0d 0a 20 20 69 6e 74 20  3.4.1 */..  int 
32c34 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  (*create_module_
32c35 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  v2)(sqlite3*,con
32c36 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73  st char*,const s
32c37 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76  qlite3_module*,v
32c38 6f 69 64 2a 2c 0d 0a 20 20 20 20 20 20 20 20 20  oid*,..         
32c39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c3a 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
32c3b 29 28 76 6f 69 64 20 2a 29 29 3b 0d 0a 20 20 2f  )(void *));..  /
32c3c 2a 20 41 64 64 65 64 20 62 79 20 33 2e 35 2e 30  * Added by 3.5.0
32c3d 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 62 69 6e   */..  int (*bin
32c3e 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69  d_zeroblob)(sqli
32c3f 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e  te3_stmt*,int,in
32c40 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 62 6c 6f  t);..  int (*blo
32c41 62 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33  b_bytes)(sqlite3
32c42 5f 62 6c 6f 62 2a 29 3b 0d 0a 20 20 69 6e 74 20  _blob*);..  int 
32c43 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73 71  (*blob_close)(sq
32c44 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0d 0a 20  lite3_blob*);.. 
32c45 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e   int (*blob_open
32c46 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
32c47 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
32c48 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  r*,const char*,s
32c49 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 0d 0a 20  qlite3_int64,.. 
32c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c4b 20 20 69 6e 74 2c 73 71 6c 69 74 65 33 5f 62 6c    int,sqlite3_bl
32c4c 6f 62 2a 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a  ob**);..  int (*
32c4d 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c 69 74  blob_read)(sqlit
32c4e 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c 69  e3_blob*,void*,i
32c4f 6e 74 2c 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20  nt,int);..  int 
32c50 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73 71  (*blob_write)(sq
32c51 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73  lite3_blob*,cons
32c52 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  t void*,int,int)
32c53 3b 0d 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  ;..  int (*creat
32c54 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28  e_collation_v2)(
32c55 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
32c56 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 0d  har*,int,void*,.
32c57 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32c58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
32c59 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
32c5a 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
32c5b 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0d 0a 20 20  onst void*),..  
32c5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c5d 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28             void(
32c5e 2a 29 28 76 6f 69 64 2a 29 29 3b 0d 0a 20 20 69  *)(void*));..  i
32c5f 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f  nt (*file_contro
32c60 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  l)(sqlite3*,cons
32c61 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
32c62 2a 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69  *);..  sqlite3_i
32c63 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69  nt64 (*memory_hi
32c64 67 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0d 0a  ghwater)(int);..
32c65 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
32c66 28 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76  (*memory_used)(v
32c67 6f 69 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  oid);..  sqlite3
32c68 5f 6d 75 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f  _mutex *(*mutex_
32c69 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0d 0a 20 20  alloc)(int);..  
32c6a 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74  void (*mutex_ent
32c6b 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  er)(sqlite3_mute
32c6c 78 2a 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 6d  x*);..  void (*m
32c6d 75 74 65 78 5f 66 72 65 65 29 28 73 71 6c 69 74  utex_free)(sqlit
32c6e 65 33 5f 6d 75 74 65 78 2a 29 3b 0d 0a 20 20 76  e3_mutex*);..  v
32c6f 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c 65 61 76  oid (*mutex_leav
32c70 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e)(sqlite3_mutex
32c71 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 6d 75 74  *);..  int (*mut
32c72 65 78 5f 74 72 79 29 28 73 71 6c 69 74 65 33 5f  ex_try)(sqlite3_
32c73 6d 75 74 65 78 2a 29 3b 0d 0a 20 20 69 6e 74 20  mutex*);..  int 
32c74 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73 74  (*open_v2)(const
32c75 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a 2a   char*,sqlite3**
32c76 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
32c77 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65  );..  int (*rele
32c78 61 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29  ase_memory)(int)
32c79 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75  ;..  void (*resu
32c7a 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28  lt_error_nomem)(
32c7b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32c7c 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  );..  void (*res
32c7d 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
32c7e 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
32c7f 74 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 73 6c  t*);..  int (*sl
32c80 65 65 70 29 28 69 6e 74 29 3b 0d 0a 20 20 76 6f  eep)(int);..  vo
32c81 69 64 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f 6c  id (*soft_heap_l
32c82 69 6d 69 74 29 28 69 6e 74 29 3b 0d 0a 20 20 73  imit)(int);..  s
32c83 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a 76 66  qlite3_vfs *(*vf
32c84 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20 63 68  s_find)(const ch
32c85 61 72 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 76  ar*);..  int (*v
32c86 66 73 5f 72 65 67 69 73 74 65 72 29 28 73 71 6c  fs_register)(sql
32c87 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b 0d  ite3_vfs*,int);.
32c88 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72  .  int (*vfs_unr
32c89 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
32c8a 5f 76 66 73 2a 29 3b 0d 0a 20 20 69 6e 74 20 28  _vfs*);..  int (
32c8b 2a 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f  *xthreadsafe)(vo
32c8c 69 64 29 3b 0d 0a 20 20 76 6f 69 64 20 28 2a 72  id);..  void (*r
32c8d 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28  esult_zeroblob)(
32c8e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32c8f 2c 69 6e 74 29 3b 0d 0a 20 20 76 6f 69 64 20 28  ,int);..  void (
32c90 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  *result_error_co
32c91 64 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  de)(sqlite3_cont
32c92 65 78 74 2a 2c 69 6e 74 29 3b 0d 0a 20 20 69 6e  ext*,int);..  in
32c93 74 20 28 2a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  t (*test_control
32c94 29 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0d 0a 20 20  )(int, ...);..  
32c95 76 6f 69 64 20 28 2a 72 61 6e 64 6f 6d 6e 65 73  void (*randomnes
32c96 73 29 28 69 6e 74 2c 76 6f 69 64 2a 29 3b 0d 0a  s)(int,void*);..
32c97 20 20 73 71 6c 69 74 65 33 20 2a 28 2a 63 6f 6e    sqlite3 *(*con
32c98 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 29 28  text_db_handle)(
32c99 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32c9a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 65 78 74 65  );..  int (*exte
32c9b 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
32c9c 73 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 29  s)(sqlite3*,int)
32c9d 3b 0d 0a 20 20 69 6e 74 20 28 2a 6c 69 6d 69 74  ;..  int (*limit
32c9e 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
32c9f 6e 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  nt);..  sqlite3_
32ca0 73 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73 74 6d  stmt *(*next_stm
32ca1 74 29 28 73 71 6c 69 74 65 33 2a 2c 73 71 6c 69  t)(sqlite3*,sqli
32ca2 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a 20 20 63  te3_stmt*);..  c
32ca3 6f 6e 73 74 20 63 68 61 72 20 2a 28 2a 73 71 6c  onst char *(*sql
32ca4 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
32ca5 3b 0d 0a 20 20 69 6e 74 20 28 2a 73 74 61 74 75  ;..  int (*statu
32ca6 73 29 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a  s)(int,int*,int*
32ca7 2c 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a  ,int);..  int (*
32ca8 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 29 28 73  backup_finish)(s
32ca9 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 29 3b  qlite3_backup*);
32caa 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ..  sqlite3_back
32cab 75 70 20 2a 28 2a 62 61 63 6b 75 70 5f 69 6e 69  up *(*backup_ini
32cac 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  t)(sqlite3*,cons
32cad 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
32cae 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d 0a  ,const char*);..
32caf 20 20 69 6e 74 20 28 2a 62 61 63 6b 75 70 5f 70    int (*backup_p
32cb0 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  agecount)(sqlite
32cb1 33 5f 62 61 63 6b 75 70 2a 29 3b 0d 0a 20 20 69  3_backup*);..  i
32cb2 6e 74 20 28 2a 62 61 63 6b 75 70 5f 72 65 6d 61  nt (*backup_rema
32cb3 69 6e 69 6e 67 29 28 73 71 6c 69 74 65 33 5f 62  ining)(sqlite3_b
32cb4 61 63 6b 75 70 2a 29 3b 0d 0a 20 20 69 6e 74 20  ackup*);..  int 
32cb5 28 2a 62 61 63 6b 75 70 5f 73 74 65 70 29 28 73  (*backup_step)(s
32cb6 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 2c 69  qlite3_backup*,i
32cb7 6e 74 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  nt);..  const ch
32cb8 61 72 20 2a 28 2a 63 6f 6d 70 69 6c 65 6f 70 74  ar *(*compileopt
32cb9 69 6f 6e 5f 67 65 74 29 28 69 6e 74 29 3b 0d 0a  ion_get)(int);..
32cba 20 20 69 6e 74 20 28 2a 63 6f 6d 70 69 6c 65 6f    int (*compileo
32cbb 70 74 69 6f 6e 5f 75 73 65 64 29 28 63 6f 6e 73  ption_used)(cons
32cbc 74 20 63 68 61 72 2a 29 3b 0d 0a 20 20 69 6e 74  t char*);..  int
32cbd 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74 69   (*create_functi
32cbe 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c  on_v2)(sqlite3*,
32cbf 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
32cc0 69 6e 74 2c 76 6f 69 64 2a 2c 0d 0a 20 20 20 20  int,void*,..    
32cc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cc2 20 20 20 20 20 20 20 20 76 6f 69 64 20 28 2a 78          void (*x
32cc3 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
32cc4 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
32cc5 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a 20 20  e3_value**),..  
32cc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cc7 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 28            void (
32cc8 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
32cc9 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
32cca 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a  ite3_value**),..
32ccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ccc 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64              void
32ccd 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
32cce 65 33 5f 63 6f 6e 74 65 78 74 2a 29 2c 0d 0a 20  e3_context*),.. 
32ccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cd0 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28             void(
32cd1 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a  *xDestroy)(void*
32cd2 29 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 64 62 5f  ));..  int (*db_
32cd3 63 6f 6e 66 69 67 29 28 73 71 6c 69 74 65 33 2a  config)(sqlite3*
32cd4 2c 69 6e 74 2c 2e 2e 2e 29 3b 0d 0a 20 20 73 71  ,int,...);..  sq
32cd5 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 64  lite3_mutex *(*d
32cd6 62 5f 6d 75 74 65 78 29 28 73 71 6c 69 74 65 33  b_mutex)(sqlite3
32cd7 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 64 62 5f  *);..  int (*db_
32cd8 73 74 61 74 75 73 29 28 73 71 6c 69 74 65 33 2a  status)(sqlite3*
32cd9 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  ,int,int*,int*,i
32cda 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 65 78  nt);..  int (*ex
32cdb 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 29 28  tended_errcode)(
32cdc 73 71 6c 69 74 65 33 2a 29 3b 0d 0a 20 20 76 6f  sqlite3*);..  vo
32cdd 69 64 20 28 2a 6c 6f 67 29 28 69 6e 74 2c 63 6f  id (*log)(int,co
32cde 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0d  nst char*,...);.
32cdf 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
32ce0 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d   (*soft_heap_lim
32ce1 69 74 36 34 29 28 73 71 6c 69 74 65 33 5f 69 6e  it64)(sqlite3_in
32ce2 74 36 34 29 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  t64);..  const c
32ce3 68 61 72 20 2a 28 2a 73 6f 75 72 63 65 69 64 29  har *(*sourceid)
32ce4 28 76 6f 69 64 29 3b 0d 0a 20 20 69 6e 74 20 28  (void);..  int (
32ce5 2a 73 74 6d 74 5f 73 74 61 74 75 73 29 28 73 71  *stmt_status)(sq
32ce6 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
32ce7 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 73  int);..  int (*s
32ce8 74 72 6e 69 63 6d 70 29 28 63 6f 6e 73 74 20 63  trnicmp)(const c
32ce9 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
32cea 2c 69 6e 74 29 3b 0d 0a 20 20 69 6e 74 20 28 2a  ,int);..  int (*
32ceb 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 29 28 73  unlock_notify)(s
32cec 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28  qlite3*,void(*)(
32ced 76 6f 69 64 2a 2a 2c 69 6e 74 29 2c 76 6f 69 64  void**,int),void
32cee 2a 29 3b 0d 0a 20 20 69 6e 74 20 28 2a 77 61 6c  *);..  int (*wal
32cef 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 29  _autocheckpoint)
32cf0 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 29 3b 0d  (sqlite3*,int);.
32cf1 0a 20 20 69 6e 74 20 28 2a 77 61 6c 5f 63 68 65  .  int (*wal_che
32cf2 63 6b 70 6f 69 6e 74 29 28 73 71 6c 69 74 65 33  ckpoint)(sqlite3
32cf3 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0d  *,const char*);.
32cf4 0a 20 20 76 6f 69 64 20 2a 28 2a 77 61 6c 5f 68  .  void *(*wal_h
32cf5 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  ook)(sqlite3*,in
32cf6 74 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  t(*)(void*,sqlit
32cf7 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
32cf8 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0d 0a 20 20  int),void*);..  
32cf9 69 6e 74 20 28 2a 62 6c 6f 62 5f 72 65 6f 70 65  int (*blob_reope
32cfa 6e 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  n)(sqlite3_blob*
32cfb 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b  ,sqlite3_int64);
32cfc 0d 0a 20 20 69 6e 74 20 28 2a 76 74 61 62 5f 63  ..  int (*vtab_c
32cfd 6f 6e 66 69 67 29 28 73 71 6c 69 74 65 33 2a 2c  onfig)(sqlite3*,
32cfe 69 6e 74 20 6f 70 2c 2e 2e 2e 29 3b 0d 0a 20 20  int op,...);..  
32cff 69 6e 74 20 28 2a 76 74 61 62 5f 6f 6e 5f 63 6f  int (*vtab_on_co
32d00 6e 66 6c 69 63 74 29 28 73 71 6c 69 74 65 33 2a  nflict)(sqlite3*
32d01 29 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  );..};..../*..**
32d02 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
32d03 61 63 72 6f 73 20 72 65 64 65 66 69 6e 65 20 74  acros redefine t
32d04 68 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  he API routines 
32d05 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65  so that they are
32d06 0d 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 20  ..** redirected 
32d07 74 68 72 6f 75 67 68 74 20 74 68 65 20 67 6c 6f  throught the glo
32d08 62 61 6c 20 73 71 6c 69 74 65 33 5f 61 70 69 20  bal sqlite3_api 
32d09 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a  structure...**..
32d0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
32d0b 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ile is also used
32d0c 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78 74 2e   by the loadext.
32d0d 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0d 0a 2a  c source file..*
32d0e 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 6d  * (part of the m
32d0f 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61  ain SQLite libra
32d10 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74 65  ry - not an exte
32d11 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0d 0a  nsion) so that..
32d12 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63  ** it can get ac
32d13 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
32d14 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
32d15 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 64   structure..** d
32d16 65 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20  efinition.  But 
32d17 74 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79  the main library
32d18 20 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74   does not want t
32d19 6f 20 72 65 64 65 66 69 6e 65 0d 0a 2a 2a 20 74  o redefine..** t
32d1a 68 65 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20  he API.  So the 
32d1b 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63  redefinition mac
32d1c 72 6f 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c  ros are only val
32d1d 69 64 20 69 66 20 74 68 65 0d 0a 2a 2a 20 53 51  id if the..** SQ
32d1e 4c 49 54 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73  LITE_CORE macros
32d1f 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a   is undefined...
32d20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
32d21 54 45 5f 43 4f 52 45 0d 0a 23 64 65 66 69 6e 65  TE_CORE..#define
32d22 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
32d23 74 65 5f 63 6f 6e 74 65 78 74 20 20 20 20 20 20  te_context      
32d24 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67  sqlite3_api->agg
32d25 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 0d 0a  regate_context..
32d26 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
32d27 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0d 0a  MIT_DEPRECATED..
32d28 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32d29 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 20  aggregate_count 
32d2a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32d2b 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63 6f  pi->aggregate_co
32d2c 75 6e 74 0d 0a 23 65 6e 64 69 66 0d 0a 23 64 65  unt..#endif..#de
32d2d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
32d2e 64 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20  d_blob          
32d2f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32d30 3e 62 69 6e 64 5f 62 6c 6f 62 0d 0a 23 64 65 66  >bind_blob..#def
32d31 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
32d32 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
32d33 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32d34 62 69 6e 64 5f 64 6f 75 62 6c 65 0d 0a 23 64 65  bind_double..#de
32d35 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
32d36 64 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  d_int           
32d37 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32d38 3e 62 69 6e 64 5f 69 6e 74 0d 0a 23 64 65 66 69  >bind_int..#defi
32d39 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
32d3a 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20  int64           
32d3b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
32d3c 69 6e 64 5f 69 6e 74 36 34 0d 0a 23 64 65 66 69  ind_int64..#defi
32d3d 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
32d3e 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  null            
32d3f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
32d40 69 6e 64 5f 6e 75 6c 6c 0d 0a 23 64 65 66 69 6e  ind_null..#defin
32d41 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  e sqlite3_bind_p
32d42 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20 20  arameter_count  
32d43 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
32d44 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
32d45 6e 74 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  nt..#define sqli
32d46 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
32d47 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74  er_index   sqlit
32d48 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
32d49 61 6d 65 74 65 72 5f 69 6e 64 65 78 0d 0a 23 64  ameter_index..#d
32d4a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
32d4b 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
32d4c 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
32d4d 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  ->bind_parameter
32d4e 5f 6e 61 6d 65 0d 0a 23 64 65 66 69 6e 65 20 73  _name..#define s
32d4f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
32d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
32d51 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
32d52 74 65 78 74 0d 0a 23 64 65 66 69 6e 65 20 73 71  text..#define sq
32d53 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
32d54 36 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  6            sql
32d55 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74  ite3_api->bind_t
32d56 65 78 74 31 36 0d 0a 23 64 65 66 69 6e 65 20 73  ext16..#define s
32d57 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
32d58 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
32d59 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
32d5a 76 61 6c 75 65 0d 0a 23 64 65 66 69 6e 65 20 73  value..#define s
32d5b 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
32d5c 6c 65 72 20 20 20 20 20 20 20 20 20 20 20 73 71  ler           sq
32d5d 6c 69 74 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f  lite3_api->busy_
32d5e 68 61 6e 64 6c 65 72 0d 0a 23 64 65 66 69 6e 65  handler..#define
32d5f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69   sqlite3_busy_ti
32d60 6d 65 6f 75 74 20 20 20 20 20 20 20 20 20 20 20  meout           
32d61 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 75 73  sqlite3_api->bus
32d62 79 5f 74 69 6d 65 6f 75 74 0d 0a 23 64 65 66 69  y_timeout..#defi
32d63 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  ne sqlite3_chang
32d64 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  es              
32d65 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
32d66 68 61 6e 67 65 73 0d 0a 23 64 65 66 69 6e 65 20  hanges..#define 
32d67 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20 20  sqlite3_close   
32d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
32d69 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f 73  qlite3_api->clos
32d6a 65 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  e..#define sqlit
32d6b 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
32d6c 64 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65  ded       sqlite
32d6d 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
32d6e 5f 6e 65 65 64 65 64 0d 0a 23 64 65 66 69 6e 65  _needed..#define
32d6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
32d70 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20  on_needed16     
32d71 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
32d72 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0d  lation_needed16.
32d73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32d74 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20  _column_blob    
32d75 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32d76 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  api->column_blob
32d77 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32d78 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20  3_column_bytes  
32d79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32d7a 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74  _api->column_byt
32d7b 65 73 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  es..#define sqli
32d7c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
32d7d 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
32d7e 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62  e3_api->column_b
32d7f 79 74 65 73 31 36 0d 0a 23 64 65 66 69 6e 65 20  ytes16..#define 
32d80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
32d81 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 73  ount           s
32d82 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
32d83 6d 6e 5f 63 6f 75 6e 74 0d 0a 23 64 65 66 69 6e  mn_count..#defin
32d84 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
32d85 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20  _database_name  
32d86 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
32d87 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
32d88 6d 65 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  me..#define sqli
32d89 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
32d8a 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74  ase_name16 sqlit
32d8b 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
32d8c 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0d 0a  atabase_name16..
32d8d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32d8e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20  column_decltype 
32d8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32d90 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  pi->column_declt
32d91 79 70 65 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  ype..#define sql
32d92 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
32d93 74 79 70 65 31 36 20 20 20 20 20 20 73 71 6c 69  type16      sqli
32d94 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
32d95 64 65 63 6c 74 79 70 65 31 36 0d 0a 23 64 65 66  decltype16..#def
32d96 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
32d97 6d 6e 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20  mn_double       
32d98 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32d99 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0d 0a 23  column_double..#
32d9a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
32d9b 6f 6c 75 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20  olumn_int       
32d9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32d9d 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74 0d 0a 23  i->column_int..#
32d9e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
32d9f 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20 20 20 20 20  olumn_int64     
32da0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32da1 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0d  i->column_int64.
32da2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32da3 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20 20 20 20  _column_name    
32da4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32da5 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  api->column_name
32da6 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32da7 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20  3_column_name16 
32da8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32da9 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d  _api->column_nam
32daa 65 31 36 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  e16..#define sql
32dab 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
32dac 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c 69  in_name     sqli
32dad 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
32dae 6f 72 69 67 69 6e 5f 6e 61 6d 65 0d 0a 23 64 65  origin_name..#de
32daf 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
32db0 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
32db1 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  6   sqlite3_api-
32db2 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
32db3 61 6d 65 31 36 0d 0a 23 64 65 66 69 6e 65 20 73  ame16..#define s
32db4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
32db5 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71  ble_name      sq
32db6 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
32db7 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 0d 0a 23 64  n_table_name..#d
32db8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
32db9 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
32dba 36 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  6    sqlite3_api
32dbb 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  ->column_table_n
32dbc 61 6d 65 31 36 0d 0a 23 64 65 66 69 6e 65 20 73  ame16..#define s
32dbd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
32dbe 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71  xt            sq
32dbf 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
32dc0 6e 5f 74 65 78 74 0d 0a 23 64 65 66 69 6e 65 20  n_text..#define 
32dc1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
32dc2 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73  ext16          s
32dc3 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
32dc4 6d 6e 5f 74 65 78 74 31 36 0d 0a 23 64 65 66 69  mn_text16..#defi
32dc5 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
32dc6 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20 20 20  n_type          
32dc7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
32dc8 6f 6c 75 6d 6e 5f 74 79 70 65 0d 0a 23 64 65 66  olumn_type..#def
32dc9 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
32dca 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20  mn_value        
32dcb 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32dcc 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0d 0a 23 64  column_value..#d
32dcd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
32dce 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20  mmit_hook       
32dcf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32dd0 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0d 0a 23  ->commit_hook..#
32dd1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
32dd2 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20  omplete         
32dd3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32dd4 69 2d 3e 63 6f 6d 70 6c 65 74 65 0d 0a 23 64 65  i->complete..#de
32dd5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
32dd6 70 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20  plete16         
32dd7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32dd8 3e 63 6f 6d 70 6c 65 74 65 31 36 0d 0a 23 64 65  >complete16..#de
32dd9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
32dda 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20  ate_collation   
32ddb 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32ddc 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
32ddd 6e 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  n..#define sqlit
32dde 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
32ddf 69 6f 6e 31 36 20 20 20 20 20 73 71 6c 69 74 65  ion16     sqlite
32de0 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
32de1 6c 6c 61 74 69 6f 6e 31 36 0d 0a 23 64 65 66 69  llation16..#defi
32de2 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
32de3 65 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  e_function      
32de4 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
32de5 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 0d 0a  reate_function..
32de6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32de7 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
32de8 36 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  6      sqlite3_a
32de9 70 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74  pi->create_funct
32dea 69 6f 6e 31 36 0d 0a 23 64 65 66 69 6e 65 20 73  ion16..#define s
32deb 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
32dec 64 75 6c 65 20 20 20 20 20 20 20 20 20 20 73 71  dule          sq
32ded 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
32dee 65 5f 6d 6f 64 75 6c 65 0d 0a 23 64 65 66 69 6e  e_module..#defin
32def 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
32df0 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20 20  _module_v2      
32df1 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
32df2 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0d 0a  eate_module_v2..
32df3 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32df4 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20  data_count      
32df5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32df6 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0d 0a  pi->data_count..
32df7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32df8 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20  db_handle       
32df9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32dfa 70 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0d 0a 23  pi->db_handle..#
32dfb 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
32dfc 65 63 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20  eclare_vtab     
32dfd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32dfe 69 2d 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0d  i->declare_vtab.
32dff 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32e00 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
32e01 61 63 68 65 20 20 20 20 73 71 6c 69 74 65 33 5f  ache    sqlite3_
32e02 61 70 69 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72  api->enable_shar
32e03 65 64 5f 63 61 63 68 65 0d 0a 23 64 65 66 69 6e  ed_cache..#defin
32e04 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  e sqlite3_errcod
32e05 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
32e06 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72   sqlite3_api->er
32e07 72 63 6f 64 65 0d 0a 23 64 65 66 69 6e 65 20 73  rcode..#define s
32e08 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 20 20  qlite3_errmsg   
32e09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
32e0a 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73  lite3_api->errms
32e0b 67 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  g..#define sqlit
32e0c 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20  e3_errmsg16     
32e0d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32e0e 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36 0d  3_api->errmsg16.
32e0f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32e10 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20 20  _exec           
32e11 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32e12 61 70 69 2d 3e 65 78 65 63 0d 0a 23 69 66 6e 64  api->exec..#ifnd
32e13 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
32e14 45 50 52 45 43 41 54 45 44 0d 0a 23 64 65 66 69  EPRECATED..#defi
32e15 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  ne sqlite3_expir
32e16 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
32e17 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
32e18 78 70 69 72 65 64 0d 0a 23 65 6e 64 69 66 0d 0a  xpired..#endif..
32e19 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32e1a 66 69 6e 61 6c 69 7a 65 20 20 20 20 20 20 20 20  finalize        
32e1b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32e1c 70 69 2d 3e 66 69 6e 61 6c 69 7a 65 0d 0a 23 64  pi->finalize..#d
32e1d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72  efine sqlite3_fr
32e1e 65 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ee              
32e1f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32e20 2d 3e 66 72 65 65 0d 0a 23 64 65 66 69 6e 65 20  ->free..#define 
32e21 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
32e22 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73  le             s
32e23 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65 65  qlite3_api->free
32e24 5f 74 61 62 6c 65 0d 0a 23 64 65 66 69 6e 65 20  _table..#define 
32e25 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f  sqlite3_get_auto
32e26 63 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 73  commit         s
32e27 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f  qlite3_api->get_
32e28 61 75 74 6f 63 6f 6d 6d 69 74 0d 0a 23 64 65 66  autocommit..#def
32e29 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ine sqlite3_get_
32e2a 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20 20  auxdata         
32e2b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32e2c 67 65 74 5f 61 75 78 64 61 74 61 0d 0a 23 64 65  get_auxdata..#de
32e2d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
32e2e 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  _table          
32e2f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32e30 3e 67 65 74 5f 74 61 62 6c 65 0d 0a 23 69 66 6e  >get_table..#ifn
32e31 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
32e32 44 45 50 52 45 43 41 54 45 44 0d 0a 23 64 65 66  DEPRECATED..#def
32e33 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  ine sqlite3_glob
32e34 61 6c 5f 72 65 63 6f 76 65 72 20 20 20 20 20 20  al_recover      
32e35 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32e36 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0d 0a  global_recover..
32e37 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65 20  #endif..#define 
32e38 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
32e39 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  t              s
32e3a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74 65  qlite3_api->inte
32e3b 72 72 75 70 74 78 0d 0a 23 64 65 66 69 6e 65 20  rruptx..#define 
32e3c 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
32e3d 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20 20 73  ert_rowid      s
32e3e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74  qlite3_api->last
32e3f 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0d 0a 23  _insert_rowid..#
32e40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
32e41 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20 20  ibversion       
32e42 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32e43 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0d 0a 23  i->libversion..#
32e44 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
32e45 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
32e46 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32e47 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  i->libversion_nu
32e48 6d 62 65 72 0d 0a 23 64 65 66 69 6e 65 20 73 71  mber..#define sq
32e49 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20  lite3_malloc    
32e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32e4b 69 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63  ite3_api->malloc
32e4c 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32e4d 33 5f 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20  3_mprintf       
32e4e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32e4f 5f 61 70 69 2d 3e 6d 70 72 69 6e 74 66 0d 0a 23  _api->mprintf..#
32e50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
32e51 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  pen             
32e52 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32e53 69 2d 3e 6f 70 65 6e 0d 0a 23 64 65 66 69 6e 65  i->open..#define
32e54 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20   sqlite3_open16 
32e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e56 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
32e57 6e 31 36 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  n16..#define sql
32e58 69 74 65 33 5f 70 72 65 70 61 72 65 20 20 20 20  ite3_prepare    
32e59 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32e5a 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
32e5b 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32e5c 33 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20  3_prepare16     
32e5d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32e5e 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 0d  _api->prepare16.
32e5f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32e60 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20 20 20  _prepare_v2     
32e61 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32e62 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0d  api->prepare_v2.
32e63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32e64 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20  _prepare16_v2   
32e65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32e66 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f 76  api->prepare16_v
32e67 32 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  2..#define sqlit
32e68 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
32e69 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32e6a 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65 0d 0a  3_api->profile..
32e6b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32e6c 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
32e6d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32e6e 70 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61 6e  pi->progress_han
32e6f 64 6c 65 72 0d 0a 23 64 65 66 69 6e 65 20 73 71  dler..#define sq
32e70 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20  lite3_realloc   
32e71 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32e72 69 74 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f  ite3_api->reallo
32e73 63 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  c..#define sqlit
32e74 65 33 5f 72 65 73 65 74 20 20 20 20 20 20 20 20  e3_reset        
32e75 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32e76 33 5f 61 70 69 2d 3e 72 65 73 65 74 0d 0a 23 64  3_api->reset..#d
32e77 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
32e78 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20 20  sult_blob       
32e79 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32e7a 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62 0d 0a 23  ->result_blob..#
32e7b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
32e7c 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20  esult_double    
32e7d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32e7e 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  i->result_double
32e7f 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32e80 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20 20  3_result_error  
32e81 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32e82 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
32e83 6f 72 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  or..#define sqli
32e84 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
32e85 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
32e86 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
32e87 72 72 6f 72 31 36 0d 0a 23 64 65 66 69 6e 65 20  rror16..#define 
32e88 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
32e89 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
32e8a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
32e8b 6c 74 5f 69 6e 74 0d 0a 23 64 65 66 69 6e 65 20  lt_int..#define 
32e8c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
32e8d 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73  nt64           s
32e8e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
32e8f 6c 74 5f 69 6e 74 36 34 0d 0a 23 64 65 66 69 6e  lt_int64..#defin
32e90 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
32e91 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  _null           
32e92 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
32e93 73 75 6c 74 5f 6e 75 6c 6c 0d 0a 23 64 65 66 69  sult_null..#defi
32e94 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
32e95 74 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  t_text          
32e96 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
32e97 65 73 75 6c 74 5f 74 65 78 74 0d 0a 23 64 65 66  esult_text..#def
32e98 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
32e99 6c 74 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  lt_text16       
32e9a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32e9b 72 65 73 75 6c 74 5f 74 65 78 74 31 36 0d 0a 23  result_text16..#
32e9c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
32e9d 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20  esult_text16be  
32e9e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32e9f 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36  i->result_text16
32ea0 62 65 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  be..#define sqli
32ea1 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
32ea2 36 6c 65 20 20 20 20 20 20 20 20 73 71 6c 69 74  6le        sqlit
32ea3 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
32ea4 65 78 74 31 36 6c 65 0d 0a 23 64 65 66 69 6e 65  ext16le..#define
32ea5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
32ea6 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20 20  value           
32ea7 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
32ea8 75 6c 74 5f 76 61 6c 75 65 0d 0a 23 64 65 66 69  ult_value..#defi
32ea9 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  ne sqlite3_rollb
32eaa 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20  ack_hook        
32eab 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
32eac 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0d 0a 23 64  ollback_hook..#d
32ead 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65  efine sqlite3_se
32eae 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20  t_authorizer    
32eaf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32eb0 2d 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  ->set_authorizer
32eb1 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32eb2 33 5f 73 65 74 5f 61 75 78 64 61 74 61 20 20 20  3_set_auxdata   
32eb3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32eb4 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78 64 61 74  _api->set_auxdat
32eb5 61 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  a..#define sqlit
32eb6 65 33 5f 73 6e 70 72 69 6e 74 66 20 20 20 20 20  e3_snprintf     
32eb7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32eb8 33 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0d  3_api->snprintf.
32eb9 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32eba 5f 73 74 65 70 20 20 20 20 20 20 20 20 20 20 20  _step           
32ebb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32ebc 61 70 69 2d 3e 73 74 65 70 0d 0a 23 64 65 66 69  api->step..#defi
32ebd 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  ne sqlite3_table
32ebe 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
32ebf 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
32ec0 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
32ec1 64 61 74 61 0d 0a 23 64 65 66 69 6e 65 20 73 71  data..#define sq
32ec2 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65  lite3_thread_cle
32ec3 61 6e 75 70 20 20 20 20 20 20 20 20 20 73 71 6c  anup         sql
32ec4 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61 64  ite3_api->thread
32ec5 5f 63 6c 65 61 6e 75 70 0d 0a 23 64 65 66 69 6e  _cleanup..#defin
32ec6 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  e sqlite3_total_
32ec7 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  changes         
32ec8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f   sqlite3_api->to
32ec9 74 61 6c 5f 63 68 61 6e 67 65 73 0d 0a 23 64 65  tal_changes..#de
32eca 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61  fine sqlite3_tra
32ecb 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ce              
32ecc 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32ecd 3e 74 72 61 63 65 0d 0a 23 69 66 6e 64 65 66 20  >trace..#ifndef 
32ece 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
32ecf 45 43 41 54 45 44 0d 0a 23 64 65 66 69 6e 65 20  ECATED..#define 
32ed0 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72  sqlite3_transfer
32ed1 5f 62 69 6e 64 69 6e 67 73 20 20 20 20 20 20 73  _bindings      s
32ed2 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e  qlite3_api->tran
32ed3 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 0d 0a 23  sfer_bindings..#
32ed4 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65 20 73  endif..#define s
32ed5 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
32ed6 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ok            sq
32ed7 6c 69 74 65 33 5f 61 70 69 2d 3e 75 70 64 61 74  lite3_api->updat
32ed8 65 5f 68 6f 6f 6b 0d 0a 23 64 65 66 69 6e 65 20  e_hook..#define 
32ed9 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
32eda 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  a              s
32edb 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 73 65 72  qlite3_api->user
32edc 5f 64 61 74 61 0d 0a 23 64 65 66 69 6e 65 20 73  _data..#define s
32edd 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
32ede 62 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  b             sq
32edf 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
32ee0 5f 62 6c 6f 62 0d 0a 23 64 65 66 69 6e 65 20 73  _blob..#define s
32ee1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
32ee2 65 73 20 20 20 20 20 20 20 20 20 20 20 20 73 71  es            sq
32ee3 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
32ee4 5f 62 79 74 65 73 0d 0a 23 64 65 66 69 6e 65 20  _bytes..#define 
32ee5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
32ee6 74 65 73 31 36 20 20 20 20 20 20 20 20 20 20 73  tes16          s
32ee7 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
32ee8 65 5f 62 79 74 65 73 31 36 0d 0a 23 64 65 66 69  e_bytes16..#defi
32ee9 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
32eea 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
32eeb 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
32eec 61 6c 75 65 5f 64 6f 75 62 6c 65 0d 0a 23 64 65  alue_double..#de
32eed 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
32eee 75 65 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  ue_int          
32eef 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32ef0 3e 76 61 6c 75 65 5f 69 6e 74 0d 0a 23 64 65 66  >value_int..#def
32ef1 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
32ef2 65 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20  e_int64         
32ef3 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32ef4 76 61 6c 75 65 5f 69 6e 74 36 34 0d 0a 23 64 65  value_int64..#de
32ef5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
32ef6 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 20  ue_numeric_type 
32ef7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32ef8 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  >value_numeric_t
32ef9 79 70 65 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  ype..#define sql
32efa 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
32efb 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32efc 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
32efd 65 78 74 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  ext..#define sql
32efe 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
32eff 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  6           sqli
32f00 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
32f01 65 78 74 31 36 0d 0a 23 64 65 66 69 6e 65 20 73  ext16..#define s
32f02 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
32f03 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71  t16be         sq
32f04 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
32f05 5f 74 65 78 74 31 36 62 65 0d 0a 23 64 65 66 69  _text16be..#defi
32f06 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
32f07 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
32f08 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
32f09 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 0d 0a 23  alue_text16le..#
32f0a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
32f0b 61 6c 75 65 5f 74 79 70 65 20 20 20 20 20 20 20  alue_type       
32f0c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f0d 69 2d 3e 76 61 6c 75 65 5f 74 79 70 65 0d 0a 23  i->value_type..#
32f0e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
32f0f 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  mprintf         
32f10 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f11 69 2d 3e 76 6d 70 72 69 6e 74 66 0d 0a 23 64 65  i->vmprintf..#de
32f12 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 76 65  fine sqlite3_ove
32f13 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 20 20  rload_function  
32f14 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32f15 3e 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  >overload_functi
32f16 6f 6e 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  on..#define sqli
32f17 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20  te3_prepare_v2  
32f18 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32f19 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f  e3_api->prepare_
32f1a 76 32 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  v2..#define sqli
32f1b 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
32f1c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32f1d 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31  e3_api->prepare1
32f1e 36 5f 76 32 0d 0a 23 64 65 66 69 6e 65 20 73 71  6_v2..#define sq
32f1f 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
32f20 69 6e 67 73 20 20 20 20 20 20 20 20 20 73 71 6c  ings         sql
32f21 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61 72 5f  ite3_api->clear_
32f22 62 69 6e 64 69 6e 67 73 0d 0a 23 64 65 66 69 6e  bindings..#defin
32f23 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  e sqlite3_bind_z
32f24 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 20  eroblob         
32f25 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
32f26 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0d 0a 23 64 65  nd_zeroblob..#de
32f27 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
32f28 62 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20  b_bytes         
32f29 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32f2a 3e 62 6c 6f 62 5f 62 79 74 65 73 0d 0a 23 64 65  >blob_bytes..#de
32f2b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
32f2c 62 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20  b_close         
32f2d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32f2e 3e 62 6c 6f 62 5f 63 6c 6f 73 65 0d 0a 23 64 65  >blob_close..#de
32f2f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
32f30 62 5f 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20  b_open          
32f31 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32f32 3e 62 6c 6f 62 5f 6f 70 65 6e 0d 0a 23 64 65 66  >blob_open..#def
32f33 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
32f34 5f 72 65 61 64 20 20 20 20 20 20 20 20 20 20 20  _read           
32f35 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32f36 62 6c 6f 62 5f 72 65 61 64 0d 0a 23 64 65 66 69  blob_read..#defi
32f37 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
32f38 77 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20  write           
32f39 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
32f3a 6c 6f 62 5f 77 72 69 74 65 0d 0a 23 64 65 66 69  lob_write..#defi
32f3b 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
32f3c 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 20 20  e_collation_v2  
32f3d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
32f3e 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
32f3f 76 32 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  v2..#define sqli
32f40 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
32f41 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32f42 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f 6e  e3_api->file_con
32f43 74 72 6f 6c 0d 0a 23 64 65 66 69 6e 65 20 73 71  trol..#define sq
32f44 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
32f45 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c  hwater       sql
32f46 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
32f47 5f 68 69 67 68 77 61 74 65 72 0d 0a 23 64 65 66  _highwater..#def
32f48 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  ine sqlite3_memo
32f49 72 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20  ry_used         
32f4a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32f4b 6d 65 6d 6f 72 79 5f 75 73 65 64 0d 0a 23 64 65  memory_used..#de
32f4c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
32f4d 65 78 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20  ex_alloc        
32f4e 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32f4f 3e 6d 75 74 65 78 5f 61 6c 6c 6f 63 0d 0a 23 64  >mutex_alloc..#d
32f50 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
32f51 74 65 78 5f 65 6e 74 65 72 20 20 20 20 20 20 20  tex_enter       
32f52 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32f53 2d 3e 6d 75 74 65 78 5f 65 6e 74 65 72 0d 0a 23  ->mutex_enter..#
32f54 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
32f55 75 74 65 78 5f 66 72 65 65 20 20 20 20 20 20 20  utex_free       
32f56 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f57 69 2d 3e 6d 75 74 65 78 5f 66 72 65 65 0d 0a 23  i->mutex_free..#
32f58 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
32f59 75 74 65 78 5f 6c 65 61 76 65 20 20 20 20 20 20  utex_leave      
32f5a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f5b 69 2d 3e 6d 75 74 65 78 5f 6c 65 61 76 65 0d 0a  i->mutex_leave..
32f5c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32f5d 6d 75 74 65 78 5f 74 72 79 20 20 20 20 20 20 20  mutex_try       
32f5e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32f5f 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79 0d 0a 23  pi->mutex_try..#
32f60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
32f61 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20 20 20  pen_v2          
32f62 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f63 69 2d 3e 6f 70 65 6e 5f 76 32 0d 0a 23 64 65 66  i->open_v2..#def
32f64 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  ine sqlite3_rele
32f65 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20 20  ase_memory      
32f66 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32f67 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0d 0a  release_memory..
32f68 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32f69 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
32f6a 65 6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  em     sqlite3_a
32f6b 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
32f6c 5f 6e 6f 6d 65 6d 0d 0a 23 64 65 66 69 6e 65 20  _nomem..#define 
32f6d 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
32f6e 72 72 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20 73  rror_toobig    s
32f6f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
32f70 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0d  lt_error_toobig.
32f71 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32f72 5f 73 6c 65 65 70 20 20 20 20 20 20 20 20 20 20  _sleep          
32f73 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32f74 61 70 69 2d 3e 73 6c 65 65 70 0d 0a 23 64 65 66  api->sleep..#def
32f75 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  ine sqlite3_soft
32f76 5f 68 65 61 70 5f 6c 69 6d 69 74 20 20 20 20 20  _heap_limit     
32f77 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32f78 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 0d  soft_heap_limit.
32f79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32f7a 5f 76 66 73 5f 66 69 6e 64 20 20 20 20 20 20 20  _vfs_find       
32f7b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32f7c 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64 0d 0a 23  api->vfs_find..#
32f7d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
32f7e 66 73 5f 72 65 67 69 73 74 65 72 20 20 20 20 20  fs_register     
32f7f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32f80 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65 72 0d  i->vfs_register.
32f81 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32f82 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 20  _vfs_unregister 
32f83 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32f84 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69 73  api->vfs_unregis
32f85 74 65 72 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  ter..#define sql
32f86 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20  ite3_threadsafe 
32f87 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32f88 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64  te3_api->xthread
32f89 73 61 66 65 0d 0a 23 64 65 66 69 6e 65 20 73 71  safe..#define sq
32f8a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
32f8b 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c  oblob        sql
32f8c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
32f8d 5f 7a 65 72 6f 62 6c 6f 62 0d 0a 23 64 65 66 69  _zeroblob..#defi
32f8e 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
32f8f 74 5f 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20  t_error_code    
32f90 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
32f91 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
32f92 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
32f93 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20  3_test_control  
32f94 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32f95 5f 61 70 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72  _api->test_contr
32f96 6f 6c 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ol..#define sqli
32f97 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 20 20  te3_randomness  
32f98 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32f99 65 33 5f 61 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65  e3_api->randomne
32f9a 73 73 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ss..#define sqli
32f9b 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
32f9c 61 6e 64 6c 65 20 20 20 20 20 20 73 71 6c 69 74  andle      sqlit
32f9d 65 33 5f 61 70 69 2d 3e 63 6f 6e 74 65 78 74 5f  e3_api->context_
32f9e 64 62 5f 68 61 6e 64 6c 65 0d 0a 23 64 65 66 69  db_handle..#defi
32f9f 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  ne sqlite3_exten
32fa0 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
32fa1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
32fa2 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
32fa3 6f 64 65 73 0d 0a 23 64 65 66 69 6e 65 20 73 71  odes..#define sq
32fa4 6c 69 74 65 33 5f 6c 69 6d 69 74 20 20 20 20 20  lite3_limit     
32fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32fa6 69 74 65 33 5f 61 70 69 2d 3e 6c 69 6d 69 74 0d  ite3_api->limit.
32fa7 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
32fa8 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20 20 20 20  _next_stmt      
32fa9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32faa 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d 74 0d 0a  api->next_stmt..
32fab 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
32fac 73 71 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  sql             
32fad 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
32fae 70 69 2d 3e 73 71 6c 0d 0a 23 64 65 66 69 6e 65  pi->sql..#define
32faf 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 20   sqlite3_status 
32fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32fb1 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 61  sqlite3_api->sta
32fb2 74 75 73 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  tus..#define sql
32fb3 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
32fb4 73 68 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  sh          sqli
32fb5 74 65 33 5f 61 70 69 2d 3e 62 61 63 6b 75 70 5f  te3_api->backup_
32fb6 66 69 6e 69 73 68 0d 0a 23 64 65 66 69 6e 65 20  finish..#define 
32fb7 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
32fb8 6e 69 74 20 20 20 20 20 20 20 20 20 20 20 20 73  nit            s
32fb9 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 61 63 6b  qlite3_api->back
32fba 75 70 5f 69 6e 69 74 0d 0a 23 64 65 66 69 6e 65  up_init..#define
32fbb 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
32fbc 70 61 67 65 63 6f 75 6e 74 20 20 20 20 20 20 20  pagecount       
32fbd 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 61 63  sqlite3_api->bac
32fbe 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 0d 0a 23  kup_pagecount..#
32fbf 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
32fc0 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 20  ackup_remaining 
32fc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32fc2 69 2d 3e 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  i->backup_remain
32fc3 69 6e 67 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  ing..#define sql
32fc4 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
32fc5 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32fc6 74 65 33 5f 61 70 69 2d 3e 62 61 63 6b 75 70 5f  te3_api->backup_
32fc7 73 74 65 70 0d 0a 23 64 65 66 69 6e 65 20 73 71  step..#define sq
32fc8 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74  lite3_compileopt
32fc9 69 6f 6e 5f 67 65 74 20 20 20 20 20 20 73 71 6c  ion_get      sql
32fca 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 69 6c  ite3_api->compil
32fcb 65 6f 70 74 69 6f 6e 5f 67 65 74 0d 0a 23 64 65  eoption_get..#de
32fcc 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
32fcd 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 20  pileoption_used 
32fce 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32fcf 3e 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75  >compileoption_u
32fd0 73 65 64 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c  sed..#define sql
32fd1 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
32fd2 74 69 6f 6e 5f 76 32 20 20 20 20 20 73 71 6c 69  tion_v2     sqli
32fd3 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f  te3_api->create_
32fd4 66 75 6e 63 74 69 6f 6e 5f 76 32 0d 0a 23 64 65  function_v2..#de
32fd5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 62 5f  fine sqlite3_db_
32fd6 63 6f 6e 66 69 67 20 20 20 20 20 20 20 20 20 20  config          
32fd7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32fd8 3e 64 62 5f 63 6f 6e 66 69 67 0d 0a 23 64 65 66  >db_config..#def
32fd9 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 6d  ine sqlite3_db_m
32fda 75 74 65 78 20 20 20 20 20 20 20 20 20 20 20 20  utex            
32fdb 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32fdc 64 62 5f 6d 75 74 65 78 0d 0a 23 64 65 66 69 6e  db_mutex..#defin
32fdd 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61  e sqlite3_db_sta
32fde 74 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20  tus             
32fdf 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62   sqlite3_api->db
32fe0 5f 73 74 61 74 75 73 0d 0a 23 64 65 66 69 6e 65  _status..#define
32fe1 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
32fe2 64 5f 65 72 72 63 6f 64 65 20 20 20 20 20 20 20  d_errcode       
32fe3 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 74  sqlite3_api->ext
32fe4 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 0d 0a 23  ended_errcode..#
32fe5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
32fe6 6f 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  og              
32fe7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32fe8 69 2d 3e 6c 6f 67 0d 0a 23 64 65 66 69 6e 65 20  i->log..#define 
32fe9 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
32fea 70 5f 6c 69 6d 69 74 36 34 20 20 20 20 20 20 73  p_limit64      s
32feb 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66 74  qlite3_api->soft
32fec 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 0d 0a 23  _heap_limit64..#
32fed 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
32fee 6f 75 72 63 65 69 64 20 20 20 20 20 20 20 20 20  ourceid         
32fef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
32ff0 69 2d 3e 73 6f 75 72 63 65 69 64 0d 0a 23 64 65  i->sourceid..#de
32ff1 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 6d  fine sqlite3_stm
32ff2 74 5f 73 74 61 74 75 73 20 20 20 20 20 20 20 20  t_status        
32ff3 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
32ff4 3e 73 74 6d 74 5f 73 74 61 74 75 73 0d 0a 23 64  >stmt_status..#d
32ff5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74  efine sqlite3_st
32ff6 72 6e 69 63 6d 70 20 20 20 20 20 20 20 20 20 20  rnicmp          
32ff7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
32ff8 2d 3e 73 74 72 6e 69 63 6d 70 0d 0a 23 64 65 66  ->strnicmp..#def
32ff9 69 6e 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  ine sqlite3_unlo
32ffa 63 6b 5f 6e 6f 74 69 66 79 20 20 20 20 20 20 20  ck_notify       
32ffb 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
32ffc 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 0d 0a 23  unlock_notify..#
32ffd 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 77  define sqlite3_w
32ffe 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
32fff 74 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  t     sqlite3_ap
33000 69 2d 3e 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b  i->wal_autocheck
33001 70 6f 69 6e 74 0d 0a 23 64 65 66 69 6e 65 20 73  point..#define s
33002 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b  qlite3_wal_check
33003 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20 73 71  point         sq
33004 6c 69 74 65 33 5f 61 70 69 2d 3e 77 61 6c 5f 63  lite3_api->wal_c
33005 68 65 63 6b 70 6f 69 6e 74 0d 0a 23 64 65 66 69  heckpoint..#defi
33006 6e 65 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68  ne sqlite3_wal_h
33007 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ook             
33008 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 77    sqlite3_api->w
33009 61 6c 5f 68 6f 6f 6b 0d 0a 23 64 65 66 69 6e 65  al_hook..#define
3300a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
3300b 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
3300c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
3300d 62 5f 72 65 6f 70 65 6e 0d 0a 23 64 65 66 69 6e  b_reopen..#defin
3300e 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e sqlite3_vtab_c
3300f 6f 6e 66 69 67 20 20 20 20 20 20 20 20 20 20 20  onfig           
33010 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 74   sqlite3_api->vt
33011 61 62 5f 63 6f 6e 66 69 67 0d 0a 23 64 65 66 69  ab_config..#defi
33012 6e 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  ne sqlite3_vtab_
33013 6f 6e 5f 63 6f 6e 66 6c 69 63 74 20 20 20 20 20  on_conflict     
33014 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
33015 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 0d  tab_on_conflict.
33016 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
33017 45 5f 43 4f 52 45 20 2a 2f 0d 0a 0d 0a 23 64 65  E_CORE */....#de
33018 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
33019 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20 20  NSION_INIT1     
3301a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
3301b 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c 69  i_routines *sqli
3301c 74 65 33 5f 61 70 69 20 3d 20 30 3b 0d 0a 23 64  te3_api = 0;..#d
3301d 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54  efine SQLITE_EXT
3301e 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20  ENSION_INIT2(v) 
3301f 20 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76   sqlite3_api = v
33020 3b 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  ;....#endif /* _
33021 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f  SQLITE3EXT_H_ */
33022 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
33023 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
33024 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
33025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33027 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
33028 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
33029 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
3302a 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20 2a  f in loadext.c *
3302b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3302c 2a 2a 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  ***/../* #includ
3302d 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d  e <string.h> */.
3302e 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
3302f 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
33030 4e 53 49 4f 4e 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  NSION..../*..** 
33031 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e 65  Some API routine
33032 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68  s are omitted wh
33033 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74 75  en various featu
33034 72 65 73 20 61 72 65 0d 0a 2a 2a 20 65 78 63 6c  res are..** excl
33035 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c  uded from a buil
33036 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75  d of SQLite.  Su
33037 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20  bstitute a NULL 
33038 70 6f 69 6e 74 65 72 0d 0a 2a 2a 20 66 6f 72 20  pointer..** for 
33039 61 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73  any missing APIs
3303a 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  ...*/..#ifndef S
3303b 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
3303c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0d 0a 23 20  UMN_METADATA..# 
3303d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
3303e 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
3303f 61 6d 65 20 20 20 30 0d 0a 23 20 64 65 66 69 6e  ame   0..# defin
33040 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
33041 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
33042 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c   0..# define sql
33043 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
33044 65 5f 6e 61 6d 65 20 20 20 20 20 20 30 0d 0a 23  e_name      0..#
33045 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
33046 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
33047 65 31 36 20 20 20 20 30 0d 0a 23 20 64 65 66 69  e16    0..# defi
33048 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
33049 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20  n_origin_name   
3304a 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71    0..# define sq
3304b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
3304c 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0d 0a  gin_name16   0..
3304d 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
3304e 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
3304f 74 61 64 61 74 61 20 20 30 0d 0a 23 65 6e 64 69  tadata  0..#endi
33050 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  f....#ifdef SQLI
33051 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
33052 41 54 49 4f 4e 0d 0a 23 20 64 65 66 69 6e 65 20  ATION..# define 
33053 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
33054 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20 20 30  orizer         0
33055 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64  ..#endif....#ifd
33056 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
33057 54 46 31 36 0d 0a 23 20 64 65 66 69 6e 65 20 73  TF16..# define s
33058 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
33059 31 36 20 20 20 20 20 20 20 20 20 20 20 20 30 0d  16            0.
3305a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
3305b 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
3305c 65 64 31 36 20 20 20 20 20 30 0d 0a 23 20 64 65  ed16     0..# de
3305d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
3305e 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20  umn_decltype16  
3305f 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20      0..# define 
33060 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
33061 61 6d 65 31 36 20 20 20 20 20 20 20 20 20 20 30  ame16          0
33062 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
33063 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
33064 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23 20 64            0..# d
33065 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
33066 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20 20 20  mplete16        
33067 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65       0..# define
33068 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33069 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20  collation16     
3306a 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  0..# define sqli
3306b 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
3306c 69 6f 6e 31 36 20 20 20 20 20 20 30 0d 0a 23 20  ion16      0..# 
3306d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
3306e 72 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20  rrmsg16         
3306f 20 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e        0..# defin
33070 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  e sqlite3_open16
33071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33072 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c   0..# define sql
33073 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20  ite3_prepare16  
33074 20 20 20 20 20 20 20 20 20 20 20 20 30 0d 0a 23              0..#
33075 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
33076 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20 20  prepare16_v2    
33077 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65 66 69         0..# defi
33078 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
33079 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20 20 20  t_error16       
3307a 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73 71    0..# define sq
3307b 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3307c 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0d 0a  t16          0..
3307d 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
3307e 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
3307f 20 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65 66          0..# def
33080 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
33081 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20  lt_text16le     
33082 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20 73     0..# define s
33083 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
33084 74 31 36 20 20 20 20 20 20 20 20 20 20 20 30 0d  t16           0.
33085 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
33086 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65  3_value_text16be
33087 20 20 20 20 20 20 20 20 20 30 0d 0a 23 20 64 65           0..# de
33088 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
33089 75 65 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20  ue_text16le     
3308a 20 20 20 20 30 0d 0a 23 20 64 65 66 69 6e 65 20      0..# define 
3308b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
3308c 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 30  atabase_name16 0
3308d 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
3308e 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
3308f 6e 61 6d 65 31 36 20 20 20 20 30 0d 0a 23 20 64  name16    0..# d
33090 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
33091 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
33092 31 36 20 20 20 30 0d 0a 23 65 6e 64 69 66 0d 0a  16   0..#endif..
33093 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
33094 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0d 0a 23  OMIT_COMPLETE..#
33095 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
33096 63 6f 6d 70 6c 65 74 65 20 30 0d 0a 23 20 64 65  complete 0..# de
33097 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
33098 70 6c 65 74 65 31 36 20 30 0d 0a 23 65 6e 64 69  plete16 0..#endi
33099 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  f....#ifdef SQLI
3309a 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
3309b 0d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
3309c 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
3309d 70 65 31 36 20 20 20 20 20 20 30 0d 0a 23 20 64  pe16      0..# d
3309e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
3309f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20 20  lumn_decltype   
330a0 20 20 20 20 20 30 0d 0a 23 65 6e 64 69 66 0d 0a       0..#endif..
330a1 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
330a2 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
330a3 4c 4c 42 41 43 4b 0d 0a 23 20 64 65 66 69 6e 65  LLBACK..# define
330a4 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
330a5 73 5f 68 61 6e 64 6c 65 72 20 30 0d 0a 23 65 6e  s_handler 0..#en
330a6 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51  dif....#ifdef SQ
330a7 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
330a8 4c 54 41 42 4c 45 0d 0a 23 20 64 65 66 69 6e 65  LTABLE..# define
330a9 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
330aa 6d 6f 64 75 6c 65 20 30 0d 0a 23 20 64 65 66 69  module 0..# defi
330ab 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
330ac 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 30 0d 0a 23  e_module_v2 0..#
330ad 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
330ae 64 65 63 6c 61 72 65 5f 76 74 61 62 20 30 0d 0a  declare_vtab 0..
330af 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
330b0 5f 76 74 61 62 5f 63 6f 6e 66 69 67 20 30 0d 0a  _vtab_config 0..
330b1 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
330b2 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63  _vtab_on_conflic
330b3 74 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23  t 0..#endif....#
330b4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
330b5 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d 0a  T_SHARED_CACHE..
330b6 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
330b7 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
330b8 61 63 68 65 20 30 0d 0a 23 65 6e 64 69 66 0d 0a  ache 0..#endif..
330b9 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
330ba 4f 4d 49 54 5f 54 52 41 43 45 0d 0a 23 20 64 65  OMIT_TRACE..# de
330bb 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
330bc 66 69 6c 65 20 20 20 20 20 20 20 30 0d 0a 23 20  file       0..# 
330bd 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
330be 72 61 63 65 20 20 20 20 20 20 20 20 20 30 0d 0a  race         0..
330bf 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66  #endif....#ifdef
330c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
330c1 5f 54 41 42 4c 45 0d 0a 23 20 64 65 66 69 6e 65  _TABLE..# define
330c2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
330c3 62 6c 65 20 20 20 20 30 0d 0a 23 20 64 65 66 69  ble    0..# defi
330c4 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  ne sqlite3_get_t
330c5 61 62 6c 65 20 20 20 20 20 30 0d 0a 23 65 6e 64  able     0..#end
330c6 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  if....#ifdef SQL
330c7 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
330c8 42 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  B..#define sqlit
330c9 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
330ca 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 73 71 6c    0..#define sql
330cb 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20  ite3_blob_bytes 
330cc 20 20 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 73      0..#define s
330cd 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
330ce 65 20 20 20 20 20 30 0d 0a 23 64 65 66 69 6e 65  e     0..#define
330cf 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70   sqlite3_blob_op
330d0 65 6e 20 20 20 20 20 20 30 0d 0a 23 64 65 66 69  en      0..#defi
330d1 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
330d2 72 65 61 64 20 20 20 20 20 20 30 0d 0a 23 64 65  read      0..#de
330d3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
330d4 62 5f 77 72 69 74 65 20 20 20 20 20 30 0d 0a 23  b_write     0..#
330d5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
330d6 6c 6f 62 5f 72 65 6f 70 65 6e 20 20 20 20 30 0d  lob_reopen    0.
330d7 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a  .#endif..../*..*
330d8 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
330d9 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
330da 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61  ns pointers to a
330db 6c 6c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f  ll SQLite API ro
330dc 75 74 69 6e 65 73 2e 0d 0a 2a 2a 20 41 20 70 6f  utines...** A po
330dd 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
330de 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65  ructure is passe
330df 64 20 69 6e 74 6f 20 65 78 74 65 6e 73 69 6f 6e  d into extension
330e0 73 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 0d  s when they are.
330e1 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20 74 68  .** loaded so th
330e2 61 74 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  at the extension
330e3 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c 73 20   can make calls 
330e4 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 53 51  back into the SQ
330e5 4c 69 74 65 0d 0a 2a 2a 20 6c 69 62 72 61 72 79  Lite..** library
330e6 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 61  ...**..** When a
330e7 64 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20  dding new APIs, 
330e8 61 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20  add them to the 
330e9 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73  bottom of this s
330ea 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 69 6e 20  tructure..** in 
330eb 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 76  order to preserv
330ec 65 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  e backwards comp
330ed 61 74 69 62 69 6c 69 74 79 2e 0d 0a 2a 2a 0d 0a  atibility...**..
330ee 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73 20 74 68  ** Extensions th
330ef 61 74 20 75 73 65 20 6e 65 77 65 72 20 41 50 49  at use newer API
330f0 73 20 73 68 6f 75 6c 64 20 66 69 72 73 74 20 63  s should first c
330f1 61 6c 6c 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69  all the..** sqli
330f2 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
330f3 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20  umber() to make 
330f4 73 75 72 65 20 74 68 61 74 20 74 68 65 20 41 50  sure that the AP
330f5 49 20 74 68 65 79 0d 0a 2a 2a 20 69 6e 74 65 6e  I they..** inten
330f6 64 20 74 6f 20 75 73 65 20 69 73 20 73 75 70 70  d to use is supp
330f7 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c 69 62  orted by the lib
330f8 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f 6e  rary.  Extension
330f9 73 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 61 6c 73  s should..** als
330fa 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
330fb 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 6f  sure that the po
330fc 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
330fd 63 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 6e 6f 74  ction is..** not
330fe 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c   NULL before cal
330ff 6c 69 6e 67 20 69 74 2e 0d 0a 2a 2f 0d 0a 73 74  ling it...*/..st
33100 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
33101 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
33102 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 0d  sqlite3Apis = {.
33103 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  .  sqlite3_aggre
33104 67 61 74 65 5f 63 6f 6e 74 65 78 74 2c 0d 0a 23  gate_context,..#
33105 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
33106 49 54 5f 44 45 50 52 45 43 41 54 45 44 0d 0a 20  IT_DEPRECATED.. 
33107 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
33108 74 65 5f 63 6f 75 6e 74 2c 0d 0a 23 65 6c 73 65  te_count,..#else
33109 0d 0a 20 20 30 2c 0d 0a 23 65 6e 64 69 66 0d 0a  ..  0,..#endif..
3310a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
3310b 6c 6f 62 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  lob,..  sqlite3_
3310c 62 69 6e 64 5f 64 6f 75 62 6c 65 2c 0d 0a 20 20  bind_double,..  
3310d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3310e 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ,..  sqlite3_bin
3310f 64 5f 69 6e 74 36 34 2c 0d 0a 20 20 73 71 6c 69  d_int64,..  sqli
33110 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c 0d 0a  te3_bind_null,..
33111 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70    sqlite3_bind_p
33112 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c 0d  arameter_count,.
33113 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
33114 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c  parameter_index,
33115 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
33116 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c  _parameter_name,
33117 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
33118 5f 74 65 78 74 2c 0d 0a 20 20 73 71 6c 69 74 65  _text,..  sqlite
33119 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c 0d 0a  3_bind_text16,..
3311a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76    sqlite3_bind_v
3311b 61 6c 75 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33  alue,..  sqlite3
3311c 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 2c 0d 0a  _busy_handler,..
3311d 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74    sqlite3_busy_t
3311e 69 6d 65 6f 75 74 2c 0d 0a 20 20 73 71 6c 69 74  imeout,..  sqlit
3311f 65 33 5f 63 68 61 6e 67 65 73 2c 0d 0a 20 20 73  e3_changes,..  s
33120 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0d 0a 20  qlite3_close,.. 
33121 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
33122 6f 6e 5f 6e 65 65 64 65 64 2c 0d 0a 20 20 73 71  on_needed,..  sq
33123 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
33124 6e 65 65 64 65 64 31 36 2c 0d 0a 20 20 73 71 6c  needed16,..  sql
33125 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
33126 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ,..  sqlite3_col
33127 75 6d 6e 5f 62 79 74 65 73 2c 0d 0a 20 20 73 71  umn_bytes,..  sq
33128 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
33129 65 73 31 36 2c 0d 0a 20 20 73 71 6c 69 74 65 33  es16,..  sqlite3
3312a 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c 0d 0a  _column_count,..
3312b 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
3312c 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c 0d  _database_name,.
3312d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
3312e 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
3312f 36 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  6,..  sqlite3_co
33130 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0d 0a  lumn_decltype,..
33131 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
33132 5f 64 65 63 6c 74 79 70 65 31 36 2c 0d 0a 20 20  _decltype16,..  
33133 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
33134 6f 75 62 6c 65 2c 0d 0a 20 20 73 71 6c 69 74 65  ouble,..  sqlite
33135 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0d 0a 20  3_column_int,.. 
33136 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
33137 69 6e 74 36 34 2c 0d 0a 20 20 73 71 6c 69 74 65  int64,..  sqlite
33138 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 0d 0a  3_column_name,..
33139 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
3313a 5f 6e 61 6d 65 31 36 2c 0d 0a 20 20 73 71 6c 69  _name16,..  sqli
3313b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
3313c 6e 5f 6e 61 6d 65 2c 0d 0a 20 20 73 71 6c 69 74  n_name,..  sqlit
3313d 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
3313e 5f 6e 61 6d 65 31 36 2c 0d 0a 20 20 73 71 6c 69  _name16,..  sqli
3313f 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
33140 5f 6e 61 6d 65 2c 0d 0a 20 20 73 71 6c 69 74 65  _name,..  sqlite
33141 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
33142 61 6d 65 31 36 2c 0d 0a 20 20 73 71 6c 69 74 65  ame16,..  sqlite
33143 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0d 0a  3_column_text,..
33144 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
33145 5f 74 65 78 74 31 36 2c 0d 0a 20 20 73 71 6c 69  _text16,..  sqli
33146 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 2c  te3_column_type,
33147 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ..  sqlite3_colu
33148 6d 6e 5f 76 61 6c 75 65 2c 0d 0a 20 20 73 71 6c  mn_value,..  sql
33149 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3314a 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d  ,..  sqlite3_com
3314b 70 6c 65 74 65 2c 0d 0a 20 20 73 71 6c 69 74 65  plete,..  sqlite
3314c 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0d 0a 20  3_complete16,.. 
3314d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3314e 63 6f 6c 6c 61 74 69 6f 6e 2c 0d 0a 20 20 73 71  collation,..  sq
3314f 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
33150 6c 61 74 69 6f 6e 31 36 2c 0d 0a 20 20 73 71 6c  lation16,..  sql
33151 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
33152 74 69 6f 6e 2c 0d 0a 20 20 73 71 6c 69 74 65 33  tion,..  sqlite3
33153 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33154 31 36 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63  16,..  sqlite3_c
33155 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c 0d 0a 20  reate_module,.. 
33156 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
33157 75 6e 74 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  unt,..  sqlite3_
33158 64 62 5f 68 61 6e 64 6c 65 2c 0d 0a 20 20 73 71  db_handle,..  sq
33159 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
3315a 61 62 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 65  ab,..  sqlite3_e
3315b 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
3315c 68 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 65  he,..  sqlite3_e
3315d 72 72 63 6f 64 65 2c 0d 0a 20 20 73 71 6c 69 74  rrcode,..  sqlit
3315e 65 33 5f 65 72 72 6d 73 67 2c 0d 0a 20 20 73 71  e3_errmsg,..  sq
3315f 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 2c 0d  lite3_errmsg16,.
33160 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2c  .  sqlite3_exec,
33161 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
33162 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
33163 0d 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69  ..  sqlite3_expi
33164 72 65 64 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 30  red,..#else..  0
33165 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c  ,..#endif..  sql
33166 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 2c 0d 0a  ite3_finalize,..
33167 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 2c 0d    sqlite3_free,.
33168 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  .  sqlite3_free_
33169 74 61 62 6c 65 2c 0d 0a 20 20 73 71 6c 69 74 65  table,..  sqlite
3316a 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
3316b 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  ,..  sqlite3_get
3316c 5f 61 75 78 64 61 74 61 2c 0d 0a 20 20 73 71 6c  _auxdata,..  sql
3316d 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0d  ite3_get_table,.
3316e 0a 20 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73  .  0,     /* Was
3316f 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f   sqlite3_global_
33170 72 65 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74  recover(), but t
33171 68 61 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  hat function is 
33172 64 65 70 72 65 63 61 74 65 64 20 2a 2f 0d 0a 20  deprecated */.. 
33173 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
33174 70 74 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6c  pt,..  sqlite3_l
33175 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
33176 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 62  ,..  sqlite3_lib
33177 76 65 72 73 69 6f 6e 2c 0d 0a 20 20 73 71 6c 69  version,..  sqli
33178 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
33179 75 6d 62 65 72 2c 0d 0a 20 20 73 71 6c 69 74 65  umber,..  sqlite
3317a 33 5f 6d 61 6c 6c 6f 63 2c 0d 0a 20 20 73 71 6c  3_malloc,..  sql
3317b 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0d 0a 20  ite3_mprintf,.. 
3317c 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0d 0a   sqlite3_open,..
3317d 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36    sqlite3_open16
3317e 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  ,..  sqlite3_pre
3317f 70 61 72 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33  pare,..  sqlite3
33180 5f 70 72 65 70 61 72 65 31 36 2c 0d 0a 20 20 73  _prepare16,..  s
33181 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 2c 0d  qlite3_profile,.
33182 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  .  sqlite3_progr
33183 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0d 0a 20 20  ess_handler,..  
33184 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2c  sqlite3_realloc,
33185 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65  ..  sqlite3_rese
33186 74 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  t,..  sqlite3_re
33187 73 75 6c 74 5f 62 6c 6f 62 2c 0d 0a 20 20 73 71  sult_blob,..  sq
33188 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
33189 62 6c 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ble,..  sqlite3_
3318a 72 65 73 75 6c 74 5f 65 72 72 6f 72 2c 0d 0a 20  result_error,.. 
3318b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3318c 65 72 72 6f 72 31 36 2c 0d 0a 20 20 73 71 6c 69  error16,..  sqli
3318d 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 2c 0d  te3_result_int,.
3318e 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
3318f 74 5f 69 6e 74 36 34 2c 0d 0a 20 20 73 71 6c 69  t_int64,..  sqli
33190 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c  te3_result_null,
33191 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
33192 6c 74 5f 74 65 78 74 2c 0d 0a 20 20 73 71 6c 69  lt_text,..  sqli
33193 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
33194 36 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  6,..  sqlite3_re
33195 73 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0d 0a  sult_text16be,..
33196 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
33197 5f 74 65 78 74 31 36 6c 65 2c 0d 0a 20 20 73 71  _text16le,..  sq
33198 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
33199 75 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72  ue,..  sqlite3_r
3319a 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c 0d 0a 20  ollback_hook,.. 
3319b 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
3319c 68 6f 72 69 7a 65 72 2c 0d 0a 20 20 73 71 6c 69  horizer,..  sqli
3319d 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 2c  te3_set_auxdata,
3319e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ..  sqlite3_snpr
3319f 69 6e 74 66 2c 0d 0a 20 20 73 71 6c 69 74 65 33  intf,..  sqlite3
331a0 5f 73 74 65 70 2c 0d 0a 20 20 73 71 6c 69 74 65  _step,..  sqlite
331a1 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
331a2 65 74 61 64 61 74 61 2c 0d 0a 23 69 66 6e 64 65  etadata,..#ifnde
331a3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
331a4 50 52 45 43 41 54 45 44 0d 0a 20 20 73 71 6c 69  PRECATED..  sqli
331a5 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
331a6 75 70 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 30 2c  up,..#else..  0,
331a7 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c 69  ..#endif..  sqli
331a8 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
331a9 73 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 72  s,..  sqlite3_tr
331aa 61 63 65 2c 0d 0a 23 69 66 6e 64 65 66 20 53 51  ace,..#ifndef SQ
331ab 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
331ac 41 54 45 44 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ATED..  sqlite3_
331ad 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
331ae 73 2c 0d 0a 23 65 6c 73 65 0d 0a 20 20 30 2c 0d  s,..#else..  0,.
331af 0a 23 65 6e 64 69 66 0d 0a 20 20 73 71 6c 69 74  .#endif..  sqlit
331b0 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 2c 0d  e3_update_hook,.
331b1 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  .  sqlite3_user_
331b2 64 61 74 61 2c 0d 0a 20 20 73 71 6c 69 74 65 33  data,..  sqlite3
331b3 5f 76 61 6c 75 65 5f 62 6c 6f 62 2c 0d 0a 20 20  _value_blob,..  
331b4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
331b5 74 65 73 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  tes,..  sqlite3_
331b6 76 61 6c 75 65 5f 62 79 74 65 73 31 36 2c 0d 0a  value_bytes16,..
331b7 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
331b8 64 6f 75 62 6c 65 2c 0d 0a 20 20 73 71 6c 69 74  double,..  sqlit
331b9 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0d 0a 20  e3_value_int,.. 
331ba 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
331bb 6e 74 36 34 2c 0d 0a 20 20 73 71 6c 69 74 65 33  nt64,..  sqlite3
331bc 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
331bd 79 70 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ype,..  sqlite3_
331be 76 61 6c 75 65 5f 74 65 78 74 2c 0d 0a 20 20 73  value_text,..  s
331bf 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
331c0 74 31 36 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  t16,..  sqlite3_
331c1 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0d  value_text16be,.
331c2 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
331c3 5f 74 65 78 74 31 36 6c 65 2c 0d 0a 20 20 73 71  _text16le,..  sq
331c4 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
331c5 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 6d 70  ,..  sqlite3_vmp
331c6 72 69 6e 74 66 2c 0d 0a 20 20 2f 2a 0d 0a 20 20  rintf,..  /*..  
331c7 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** The original 
331c8 41 50 49 20 73 65 74 20 65 6e 64 73 20 68 65 72  API set ends her
331c9 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73 69 6f  e.  All extensio
331ca 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e 79 0d  ns can call any.
331cb 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50 49  .  ** of the API
331cc 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65 64  s above provided
331cd 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
331ce 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20  r is not NULL.  
331cf 42 75 74 0d 0a 20 20 2a 2a 20 62 65 66 6f 72 65  But..  ** before
331d0 20 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68   calling APIs th
331d1 61 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e  at follow, exten
331d2 73 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63  sion should chec
331d3 6b 20 74 68 65 0d 0a 20 20 2a 2a 20 73 71 6c 69  k the..  ** sqli
331d4 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
331d5 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20  umber() to make 
331d6 73 75 72 65 20 74 68 65 79 20 61 72 65 20 64 65  sure they are de
331d7 61 6c 69 6e 67 20 77 69 74 68 0d 0a 20 20 2a 2a  aling with..  **
331d8 20 61 20 6c 69 62 72 61 72 79 20 74 68 61 74 20   a library that 
331d9 69 73 20 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f  is new enough to
331da 20 73 75 70 70 6f 72 74 20 74 68 61 74 20 41 50   support that AP
331db 49 2e 0d 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  I...  **********
331dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
331e0 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  .  */..  sqlite3
331e1 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
331e2 6f 6e 2c 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  on,....  /*..  *
331e3 2a 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e  * Added after 3.
331e4 33 2e 31 33 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71  3.13..  */..  sq
331e5 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
331e6 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  ,..  sqlite3_pre
331e7 70 61 72 65 31 36 5f 76 32 2c 0d 0a 20 20 73 71  pare16_v2,..  sq
331e8 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
331e9 69 6e 67 73 2c 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20  ings,....  /*.. 
331ea 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
331eb 34 2e 31 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  4.1..  */..  sql
331ec 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
331ed 6c 65 5f 76 32 2c 0d 0a 0d 0a 20 20 2f 2a 0d 0a  le_v2,....  /*..
331ee 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
331ef 2e 35 2e 30 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71  .5.0..  */..  sq
331f0 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
331f1 6c 6f 62 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  lob,..  sqlite3_
331f2 62 6c 6f 62 5f 62 79 74 65 73 2c 0d 0a 20 20 73  blob_bytes,..  s
331f3 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
331f4 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  e,..  sqlite3_bl
331f5 6f 62 5f 6f 70 65 6e 2c 0d 0a 20 20 73 71 6c 69  ob_open,..  sqli
331f6 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 2c 0d 0a  te3_blob_read,..
331f7 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77    sqlite3_blob_w
331f8 72 69 74 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33  rite,..  sqlite3
331f9 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
331fa 6e 5f 76 32 2c 0d 0a 20 20 73 71 6c 69 74 65 33  n_v2,..  sqlite3
331fb 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 2c 0d 0a  _file_control,..
331fc 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79    sqlite3_memory
331fd 5f 68 69 67 68 77 61 74 65 72 2c 0d 0a 20 20 73  _highwater,..  s
331fe 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
331ff 65 64 2c 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  ed,..#ifdef SQLI
33200 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0d 0a 20  TE_MUTEX_OMIT.. 
33201 20 30 2c 20 0d 0a 20 20 30 2c 20 0d 0a 20 20 30   0, ..  0, ..  0
33202 2c 0d 0a 20 20 30 2c 0d 0a 20 20 30 2c 0d 0a 23  ,..  0,..  0,..#
33203 65 6c 73 65 0d 0a 20 20 73 71 6c 69 74 65 33 5f  else..  sqlite3_
33204 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0d 0a 20 20  mutex_alloc,..  
33205 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33206 74 65 72 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ter,..  sqlite3_
33207 6d 75 74 65 78 5f 66 72 65 65 2c 0d 0a 20 20 73  mutex_free,..  s
33208 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33209 76 65 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ve,..  sqlite3_m
3320a 75 74 65 78 5f 74 72 79 2c 0d 0a 23 65 6e 64 69  utex_try,..#endi
3320b 66 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  f..  sqlite3_ope
3320c 6e 5f 76 32 2c 0d 0a 20 20 73 71 6c 69 74 65 33  n_v2,..  sqlite3
3320d 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 2c  _release_memory,
3320e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
3320f 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c 0d  lt_error_nomem,.
33210 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
33211 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0d  t_error_toobig,.
33212 0a 20 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70  .  sqlite3_sleep
33213 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 66  ,..  sqlite3_sof
33214 74 5f 68 65 61 70 5f 6c 69 6d 69 74 2c 0d 0a 20  t_heap_limit,.. 
33215 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
33216 64 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  d,..  sqlite3_vf
33217 73 5f 72 65 67 69 73 74 65 72 2c 0d 0a 20 20 73  s_register,..  s
33218 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
33219 69 73 74 65 72 2c 0d 0a 0d 0a 20 20 2f 2a 0d 0a  ister,....  /*..
3321a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
3321b 2e 35 2e 38 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71  .5.8..  */..  sq
3321c 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65  lite3_threadsafe
3321d 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ,..  sqlite3_res
3321e 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 2c 0d 0a 20  ult_zeroblob,.. 
3321f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
33220 65 72 72 6f 72 5f 63 6f 64 65 2c 0d 0a 20 20 73  error_code,..  s
33221 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
33222 72 6f 6c 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  rol,..  sqlite3_
33223 72 61 6e 64 6f 6d 6e 65 73 73 2c 0d 0a 20 20 73  randomness,..  s
33224 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
33225 62 5f 68 61 6e 64 6c 65 2c 0d 0a 0d 0a 20 20 2f  b_handle,....  /
33226 2a 0d 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f  *..  ** Added fo
33227 72 20 33 2e 36 2e 30 0d 0a 20 20 2a 2f 0d 0a 20  r 3.6.0..  */.. 
33228 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
33229 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 2c 0d  d_result_codes,.
3322a 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  .  sqlite3_limit
3322b 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65 78  ,..  sqlite3_nex
3322c 74 5f 73 74 6d 74 2c 0d 0a 20 20 73 71 6c 69 74  t_stmt,..  sqlit
3322d 65 33 5f 73 71 6c 2c 0d 0a 20 20 73 71 6c 69 74  e3_sql,..  sqlit
3322e 65 33 5f 73 74 61 74 75 73 2c 0d 0a 0d 0a 20 20  e3_status,....  
3322f 2f 2a 0d 0a 20 20 2a 2a 20 41 64 64 65 64 20 66  /*..  ** Added f
33230 6f 72 20 33 2e 37 2e 34 0d 0a 20 20 2a 2f 0d 0a  or 3.7.4..  */..
33231 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
33232 5f 66 69 6e 69 73 68 2c 0d 0a 20 20 73 71 6c 69  _finish,..  sqli
33233 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 2c  te3_backup_init,
33234 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ..  sqlite3_back
33235 75 70 5f 70 61 67 65 63 6f 75 6e 74 2c 0d 0a 20  up_pagecount,.. 
33236 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
33237 72 65 6d 61 69 6e 69 6e 67 2c 0d 0a 20 20 73 71  remaining,..  sq
33238 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
33239 70 2c 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  p,..#ifndef SQLI
3323a 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f  TE_OMIT_COMPILEO
3323b 50 54 49 4f 4e 5f 44 49 41 47 53 0d 0a 20 20 73  PTION_DIAGS..  s
3323c 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70  qlite3_compileop
3323d 74 69 6f 6e 5f 67 65 74 2c 0d 0a 20 20 73 71 6c  tion_get,..  sql
3323e 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69  ite3_compileopti
3323f 6f 6e 5f 75 73 65 64 2c 0d 0a 23 65 6c 73 65 0d  on_used,..#else.
33240 0a 20 20 30 2c 0d 0a 20 20 30 2c 0d 0a 23 65 6e  .  0,..  0,..#en
33241 64 69 66 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63  dif..  sqlite3_c
33242 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76  reate_function_v
33243 32 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 64 62  2,..  sqlite3_db
33244 5f 63 6f 6e 66 69 67 2c 0d 0a 20 20 73 71 6c 69  _config,..  sqli
33245 74 65 33 5f 64 62 5f 6d 75 74 65 78 2c 0d 0a 20  te3_db_mutex,.. 
33246 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74   sqlite3_db_stat
33247 75 73 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 65  us,..  sqlite3_e
33248 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 2c  xtended_errcode,
33249 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 2c  ..  sqlite3_log,
3324a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  ..  sqlite3_soft
3324b 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 2c 0d 0a  _heap_limit64,..
3324c 20 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65    sqlite3_source
3324d 69 64 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73  id,..  sqlite3_s
3324e 74 6d 74 5f 73 74 61 74 75 73 2c 0d 0a 20 20 73  tmt_status,..  s
3324f 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 2c  qlite3_strnicmp,
33250 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
33251 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f  ENABLE_UNLOCK_NO
33252 54 49 46 59 0d 0a 20 20 73 71 6c 69 74 65 33 5f  TIFY..  sqlite3_
33253 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 2c 0d 0a  unlock_notify,..
33254 23 65 6c 73 65 0d 0a 20 20 30 2c 0d 0a 23 65 6e  #else..  0,..#en
33255 64 69 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
33256 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 20 20  ITE_OMIT_WAL..  
33257 73 71 6c 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f  sqlite3_wal_auto
33258 63 68 65 63 6b 70 6f 69 6e 74 2c 0d 0a 20 20 73  checkpoint,..  s
33259 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b  qlite3_wal_check
3325a 70 6f 69 6e 74 2c 0d 0a 20 20 73 71 6c 69 74 65  point,..  sqlite
3325b 33 5f 77 61 6c 5f 68 6f 6f 6b 2c 0d 0a 23 65 6c  3_wal_hook,..#el
3325c 73 65 0d 0a 20 20 30 2c 0d 0a 20 20 30 2c 0d 0a  se..  0,..  0,..
3325d 20 20 30 2c 0d 0a 23 65 6e 64 69 66 0d 0a 20 20    0,..#endif..  
3325e 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 6f  sqlite3_blob_reo
3325f 70 65 6e 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  pen,..  sqlite3_
33260 76 74 61 62 5f 63 6f 6e 66 69 67 2c 0d 0a 20 20  vtab_config,..  
33261 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f  sqlite3_vtab_on_
33262 63 6f 6e 66 6c 69 63 74 2c 0d 0a 7d 3b 0d 0a 0d  conflict,..};...
33263 0a 2f 2a 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ./*..** Attempt 
33264 74 6f 20 6c 6f 61 64 20 61 6e 20 53 51 4c 69 74  to load an SQLit
33265 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72  e extension libr
33266 61 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  ary contained in
33267 20 74 68 65 20 66 69 6c 65 0d 0a 2a 2a 20 7a 46   the file..** zF
33268 69 6c 65 2e 20 20 54 68 65 20 65 6e 74 72 79 20  ile.  The entry 
33269 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 20  point is zProc. 
3326a 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 20   zProc may be 0 
3326b 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 61 0d  in which case a.
3326c 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e 74 72  .** default entr
3326d 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28 73 71  y point name (sq
3326e 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
3326f 69 6e 69 74 29 20 69 73 20 75 73 65 64 2e 20 20  init) is used.  
33270 55 73 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 64  Use..** of the d
33271 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73 20 72  efault name is r
33272 65 63 6f 6d 6d 65 6e 64 65 64 2e 0d 0a 2a 2a 0d  ecommended...**.
33273 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
33274 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
33275 61 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  and SQLITE_ERROR
33276 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   if something go
33277 65 73 20 77 72 6f 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  es wrong...**..*
33278 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
33279 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73  curs and pzErrMs
3327a 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  g is not 0, then
3327b 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20   fill *pzErrMsg 
3327c 77 69 74 68 20 0d 0a 2a 2a 20 65 72 72 6f 72 20  with ..** error 
3327d 6d 65 73 73 61 67 65 20 74 65 78 74 2e 20 20 54  message text.  T
3327e 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
3327f 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72 65 65 20  ion should free 
33280 74 68 69 73 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20  this memory..** 
33281 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  by calling sqlit
33282 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 2e 0d  e3DbFree(db, )..
33283 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
33284 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e  sqlite3LoadExten
33285 73 69 6f 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33  sion(..  sqlite3
33286 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
33287 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
33288 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
33289 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3328a 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  on */..  const c
3328b 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f  har *zFile,    /
3328c 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68  * Name of the sh
3328d 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e  ared library con
3328e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f  taining extensio
3328f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  n */..  const ch
33290 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a  ar *zProc,    /*
33291 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55   Entry point.  U
33292 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65  se "sqlite3_exte
33293 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30  nsion_init" if 0
33294 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a   */..  char **pz
33295 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
33296 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
33297 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
33298 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
33299 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62  3_vfs *pVfs = db
3329a 2d 3e 70 56 66 73 3b 0d 0a 20 20 76 6f 69 64 20  ->pVfs;..  void 
3329b 2a 68 61 6e 64 6c 65 3b 0d 0a 20 20 69 6e 74 20  *handle;..  int 
3329c 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65 33  (*xInit)(sqlite3
3329d 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
3329e 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
3329f 6e 65 73 2a 29 3b 0d 0a 20 20 63 68 61 72 20 2a  nes*);..  char *
332a0 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0d 0a 20 20  zErrmsg = 0;..  
332a1 76 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b 0d  void **aHandle;.
332a2 0a 20 20 69 6e 74 20 6e 4d 73 67 20 3d 20 33 30  .  int nMsg = 30
332a3 30 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65  0 + sqlite3Strle
332a4 6e 33 30 28 7a 46 69 6c 65 29 3b 0d 0a 0d 0a 20  n30(zFile);.... 
332a5 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 20   if( pzErrMsg ) 
332a6 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d 0a  *pzErrMsg = 0;..
332a7 0d 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31  ..  /* Ticket #1
332a8 38 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61  863.  To avoid a
332a9 20 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69   creating securi
332aa 74 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ty problems for 
332ab 6f 6c 64 65 72 0d 0a 20 20 2a 2a 20 61 70 70 6c  older..  ** appl
332ac 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65  ications that re
332ad 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77  link against new
332ae 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  er versions of S
332af 51 4c 69 74 65 2c 20 74 68 65 0d 0a 20 20 2a 2a  QLite, the..  **
332b0 20 61 62 69 6c 69 74 79 20 74 6f 20 72 75 6e 20   ability to run 
332b1 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69  load_extension i
332b2 73 20 74 75 72 6e 65 64 20 6f 66 66 20 62 79 20  s turned off by 
332b3 64 65 66 61 75 6c 74 2e 20 20 4f 6e 65 0d 0a 20  default.  One.. 
332b4 20 2a 2a 20 6d 75 73 74 20 63 61 6c 6c 20 73 71   ** must call sq
332b5 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
332b6 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 74 6f  d_extension() to
332b7 20 74 75 72 6e 20 6f 6e 20 65 78 74 65 6e 73 69   turn on extensi
332b8 6f 6e 0d 0a 20 20 2a 2a 20 6c 6f 61 64 69 6e 67  on..  ** loading
332b9 2e 20 20 4f 74 68 65 72 77 69 73 65 20 79 6f 75  .  Otherwise you
332ba 20 67 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   get the followi
332bb 6e 67 20 65 72 72 6f 72 2e 0d 0a 20 20 2a 2f 0d  ng error...  */.
332bc 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
332bd 73 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45  s & SQLITE_LoadE
332be 78 74 65 6e 73 69 6f 6e 29 3d 3d 30 20 29 7b 0d  xtension)==0 ){.
332bf 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73  .    if( pzErrMs
332c0 67 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 7a 45  g ){..      *pzE
332c1 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
332c2 6d 70 72 69 6e 74 66 28 22 6e 6f 74 20 61 75 74  mprintf("not aut
332c3 68 6f 72 69 7a 65 64 22 29 3b 0d 0a 20 20 20 20  horized");..    
332c4 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  }..    return SQ
332c5 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d  LITE_ERROR;..  }
332c6 0d 0a 0d 0a 20 20 69 66 28 20 7a 50 72 6f 63 3d  ....  if( zProc=
332c7 3d 30 20 29 7b 0d 0a 20 20 20 20 7a 50 72 6f 63  =0 ){..    zProc
332c8 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65   = "sqlite3_exte
332c9 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0d 0a 20 20  nsion_init";..  
332ca 7d 0d 0a 0d 0a 20 20 68 61 6e 64 6c 65 20 3d 20  }....  handle = 
332cb 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28  sqlite3OsDlOpen(
332cc 70 56 66 73 2c 20 7a 46 69 6c 65 29 3b 0d 0a 20  pVfs, zFile);.. 
332cd 20 69 66 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29   if( handle==0 )
332ce 7b 0d 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72  {..    if( pzErr
332cf 4d 73 67 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70  Msg ){..      *p
332d0 7a 45 72 72 4d 73 67 20 3d 20 7a 45 72 72 6d 73  zErrMsg = zErrms
332d1 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  g = sqlite3_mall
332d2 6f 63 28 6e 4d 73 67 29 3b 0d 0a 20 20 20 20 20  oc(nMsg);..     
332d3 20 69 66 28 20 7a 45 72 72 6d 73 67 20 29 7b 0d   if( zErrmsg ){.
332d4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
332d5 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67 2c 20  _snprintf(nMsg, 
332d6 7a 45 72 72 6d 73 67 2c 20 0d 0a 20 20 20 20 20  zErrmsg, ..     
332d7 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
332d8 6f 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6c 69  o open shared li
332d9 62 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 46 69  brary [%s]", zFi
332da 6c 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  le);..        sq
332db 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70  lite3OsDlError(p
332dc 56 66 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72  Vfs, nMsg-1, zEr
332dd 72 6d 73 67 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  rmsg);..      }.
332de 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75  .    }..    retu
332df 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
332e0 0d 0a 20 20 7d 0d 0a 20 20 78 49 6e 69 74 20 3d  ..  }..  xInit =
332e1 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33   (int(*)(sqlite3
332e2 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
332e3 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
332e4 6e 65 73 2a 29 29 0d 0a 20 20 20 20 20 20 20 20  nes*))..        
332e5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
332e6 65 33 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c 20  e3OsDlSym(pVfs, 
332e7 68 61 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b 0d  handle, zProc);.
332e8 0a 20 20 69 66 28 20 78 49 6e 69 74 3d 3d 30 20  .  if( xInit==0 
332e9 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 7a 45 72  ){..    if( pzEr
332ea 72 4d 73 67 20 29 7b 0d 0a 20 20 20 20 20 20 6e  rMsg ){..      n
332eb 4d 73 67 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Msg += sqlite3St
332ec 72 6c 65 6e 33 30 28 7a 50 72 6f 63 29 3b 0d 0a  rlen30(zProc);..
332ed 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
332ee 3d 20 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69  = zErrmsg = sqli
332ef 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 73 67 29  te3_malloc(nMsg)
332f0 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 45 72  ;..      if( zEr
332f1 72 6d 73 67 20 29 7b 0d 0a 20 20 20 20 20 20 20  rmsg ){..       
332f2 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
332f3 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67 2c  f(nMsg, zErrmsg,
332f4 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 6e  ..            "n
332f5 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 5b 25  o entry point [%
332f6 73 5d 20 69 6e 20 73 68 61 72 65 64 20 6c 69 62  s] in shared lib
332f7 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 50 72 6f  rary [%s]", zPro
332f8 63 2c 7a 46 69 6c 65 29 3b 0d 0a 20 20 20 20 20  c,zFile);..     
332f9 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72     sqlite3OsDlEr
332fa 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d 31  ror(pVfs, nMsg-1
332fb 2c 20 7a 45 72 72 6d 73 67 29 3b 0d 0a 20 20 20  , zErrmsg);..   
332fc 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
332fd 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66  te3OsDlClose(pVf
332fe 73 2c 20 68 61 6e 64 6c 65 29 3b 0d 0a 20 20 20  s, handle);..   
332ff 20 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53   }..    return S
33300 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
33301 7d 65 6c 73 65 20 69 66 28 20 78 49 6e 69 74 28  }else if( xInit(
33302 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c 20 26 73  db, &zErrmsg, &s
33303 71 6c 69 74 65 33 41 70 69 73 29 20 29 7b 0d 0a  qlite3Apis) ){..
33304 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
33305 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 7a 45 72   ){..      *pzEr
33306 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d  rMsg = sqlite3_m
33307 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20 64 75  printf("error du
33308 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
33309 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73  ion: %s", zErrms
3330a 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  g);..    }..    
3330b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
3330c 72 6d 73 67 29 3b 0d 0a 20 20 20 20 73 71 6c 69  rmsg);..    sqli
3330d 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66  te3OsDlClose(pVf
3330e 73 2c 20 68 61 6e 64 6c 65 29 3b 0d 0a 20 20 20  s, handle);..   
3330f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
33310 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  RROR;..  }....  
33311 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65  /* Append the ne
33312 77 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  w shared library
33313 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 64   handle to the d
33314 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61 72  b->aExtension ar
33315 72 61 79 2e 20 2a 2f 0d 0a 20 20 61 48 61 6e 64  ray. */..  aHand
33316 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  le = sqlite3DbMa
33317 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
33318 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64 62 2d  eof(handle)*(db-
33319 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29 29 3b  >nExtension+1));
3331a 0d 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65 3d  ..  if( aHandle=
3331b 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
3331c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
3331d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
3331e 6e 45 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0d  nExtension>0 ){.
3331f 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 48 61 6e  .    memcpy(aHan
33320 64 6c 65 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73  dle, db->aExtens
33321 69 6f 6e 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64  ion, sizeof(hand
33322 6c 65 29 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69  le)*db->nExtensi
33323 6f 6e 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  on);..  }..  sql
33324 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 64  ite3DbFree(db, d
33325 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0d  b->aExtension);.
33326 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  .  db->aExtensio
33327 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0d 0a 0d 0a  n = aHandle;....
33328 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e    db->aExtension
33329 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b  [db->nExtension+
3332a 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0d 0a 20 20  +] = handle;..  
3332b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3332c 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ;..}..SQLITE_API
3332d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61   int sqlite3_loa
3332e 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0d 0a 20 20  d_extension(..  
3332f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
33330 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68        /* Load th
33331 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f  e extension into
33332 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63   this database c
33333 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20  onnection */..  
33334 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
33335 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
33336 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
33337 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
33338 78 74 65 6e 73 69 6f 6e 20 2a 2f 0d 0a 20 20 63  xtension */..  c
33339 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63  onst char *zProc
3333a 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f  ,    /* Entry po
3333b 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74  int.  Use "sqlit
3333c 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
3333d 74 22 20 69 66 20 30 20 2a 2f 0d 0a 20 20 63 68  t" if 0 */..  ch
3333e 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
3333f 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
33340 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
33341 20 6e 6f 74 20 30 20 2a 2f 0d 0a 29 7b 0d 0a 20   not 0 */..){.. 
33342 20 69 6e 74 20 72 63 3b 0d 0a 20 20 73 71 6c 69   int rc;..  sqli
33343 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
33344 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72  db->mutex);..  r
33345 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 61 64 45  c = sqlite3LoadE
33346 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69  xtension(db, zFi
33347 6c 65 2c 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72  le, zProc, pzErr
33348 4d 73 67 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71  Msg);..  rc = sq
33349 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
3334a 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33   rc);..  sqlite3
3334b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
3334c 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75  >mutex);..  retu
3334d 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
3334e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f  .** Call this ro
3334f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 20 64  utine when the d
33350 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33351 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e  on is closing in
33352 20 6f 72 64 65 72 0d 0a 2a 2a 20 74 6f 20 63 6c   order..** to cl
33353 65 61 6e 20 75 70 20 6c 6f 61 64 65 64 20 65 78  ean up loaded ex
33354 74 65 6e 73 69 6f 6e 73 0d 0a 2a 2f 0d 0a 53 51  tensions..*/..SQ
33355 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
33356 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78  d sqlite3CloseEx
33357 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33  tensions(sqlite3
33358 20 2a 64 62 29 7b 0d 0a 20 20 69 6e 74 20 69 3b   *db){..  int i;
33359 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
3335a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
3335b 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  b->mutex) );..  
3335c 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
3335d 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b  Extension; i++){
3335e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44  ..    sqlite3OsD
3335f 6c 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c  lClose(db->pVfs,
33360 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b   db->aExtension[
33361 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  i]);..  }..  sql
33362 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 64  ite3DbFree(db, d
33363 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0d  b->aExtension);.
33364 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 6e 61  .}..../*..** Ena
33365 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 65  ble or disable e
33366 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
33367 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  .  Extension loa
33368 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c 65 64  ding is disabled
33369 20 62 79 0d 0a 2a 2a 20 64 65 66 61 75 6c 74 20   by..** default 
3336a 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65  so as not to ope
3336b 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73  n security holes
3336c 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63   in older applic
3336d 61 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ations...*/..SQL
3336e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3336f 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
33370 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65  extension(sqlite
33371 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66  3 *db, int onoff
33372 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ){..  sqlite3_mu
33373 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
33374 74 65 78 29 3b 0d 0a 20 20 69 66 28 20 6f 6e 6f  tex);..  if( ono
33375 66 66 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 66  ff ){..    db->f
33376 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c  lags |= SQLITE_L
33377 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0d 0a 20  oadExtension;.. 
33378 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 64 62 2d   }else{..    db-
33379 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
3337a 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b  E_LoadExtension;
3337b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
3337c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
3337d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75  >mutex);..  retu
3337e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
3337f 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ....#endif /* SQ
33380 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
33381 58 54 45 4e 53 49 4f 4e 20 2a 2f 0d 0a 0d 0a 2f  XTENSION */..../
33382 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 6f 2d 65  *..** The auto-e
33383 78 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20 61 64  xtension code ad
33384 64 65 64 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ded regardless o
33385 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
33386 20 65 78 74 65 6e 73 69 6f 6e 0d 0a 2a 2a 20 6c   extension..** l
33387 6f 61 64 69 6e 67 20 69 73 20 73 75 70 70 6f 72  oading is suppor
33388 74 65 64 2e 20 20 57 65 20 6e 65 65 64 20 61 20  ted.  We need a 
33389 64 75 6d 6d 79 20 73 71 6c 69 74 65 33 41 70 69  dummy sqlite3Api
3338a 73 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  s pointer for th
3338b 61 74 0d 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72  at..** code if r
3338c 65 67 75 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e  egular extension
3338d 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e 6f 74 20   loading is not 
3338e 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73  available.  This
3338f 20 69 73 20 74 68 61 74 0d 0a 2a 2a 20 64 75 6d   is that..** dum
33390 6d 79 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d  my pointer...*/.
33391 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
33392 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
33393 4f 4e 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ON..static const
33394 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
33395 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69  tines sqlite3Api
33396 73 20 3d 20 7b 20 30 20 7d 3b 0d 0a 23 65 6e 64  s = { 0 };..#end
33397 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  if....../*..** T
33398 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a  he following obj
33399 65 63 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69  ect holds the li
3339a 73 74 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61  st of automatica
3339b 6c 6c 79 20 6c 6f 61 64 65 64 0d 0a 2a 2a 20 65  lly loaded..** e
3339c 78 74 65 6e 73 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a  xtensions...**..
3339d 2a 2a 20 54 68 69 73 20 6c 69 73 74 20 69 73 20  ** This list is 
3339e 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68  shared across th
3339f 72 65 61 64 73 2e 20 20 54 68 65 20 53 51 4c 49  reads.  The SQLI
333a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
333a1 4d 41 53 54 45 52 0d 0a 2a 2a 20 6d 75 74 65 78  MASTER..** mutex
333a2 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
333a3 69 6c 65 20 61 63 63 65 73 73 69 6e 67 20 74 68  ile accessing th
333a4 69 73 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 74 79  is list...*/..ty
333a5 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
333a6 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20  ite3AutoExtList 
333a7 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
333a8 73 74 3b 0d 0a 73 74 61 74 69 63 20 53 51 4c 49  st;..static SQLI
333a9 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71  TE_WSD struct sq
333aa 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
333ab 20 7b 0d 0a 20 20 69 6e 74 20 6e 45 78 74 3b 20   {..  int nExt; 
333ac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
333ad 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
333ae 73 20 69 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20  s in aExt[] */  
333af 20 20 20 20 20 20 20 20 0d 0a 20 20 76 6f 69 64          ..  void
333b0 20 28 2a 2a 61 45 78 74 29 28 76 6f 69 64 29 3b   (**aExt)(void);
333b1 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
333b2 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  o the extension 
333b3 69 6e 69 74 20 66 75 6e 63 74 69 6f 6e 73 20 2a  init functions *
333b4 2f 0d 0a 7d 20 73 71 6c 69 74 65 33 41 75 74 6f  /..} sqlite3Auto
333b5 65 78 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0d  ext = { 0, 0 };.
333b6 0a 0d 0a 2f 2a 20 54 68 65 20 22 77 73 64 41 75  .../* The "wsdAu
333b7 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77 69 6c  toext" macro wil
333b8 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
333b9 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e 0d 0a   autoextension..
333ba 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e  ** state vector.
333bb 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74    If writable st
333bc 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73  atic data is uns
333bd 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20  upported on the 
333be 74 61 72 67 65 74 2c 0d 0a 2a 2a 20 77 65 20 68  target,..** we h
333bf 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
333c0 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
333c1 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
333c2 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0d  the more common.
333c3 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77  .** case where w
333c4 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
333c5 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64  ata is supported
333c6 2c 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65  , wsdStat can re
333c7 66 65 72 20 64 69 72 65 63 74 6c 79 0d 0a 2a 2a  fer directly..**
333c8 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33   to the "sqlite3
333c9 41 75 74 6f 65 78 74 22 20 73 74 61 74 65 20 76  Autoext" state v
333ca 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61  ector declared a
333cb 62 6f 76 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65  bove...*/..#ifde
333cc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  f SQLITE_OMIT_WS
333cd 44 0d 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  D..# define wsdA
333ce 75 74 6f 65 78 74 49 6e 69 74 20 5c 0d 0a 20 20  utoextInit \..  
333cf 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
333d0 73 74 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28  st *x = &GLOBAL(
333d1 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
333d2 73 74 2c 73 71 6c 69 74 65 33 41 75 74 6f 65 78  st,sqlite3Autoex
333d3 74 29 0d 0a 23 20 64 65 66 69 6e 65 20 77 73 64  t)..# define wsd
333d4 41 75 74 6f 65 78 74 20 78 5b 30 5d 0d 0a 23 65  Autoext x[0]..#e
333d5 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 77 73  lse..# define ws
333d6 64 41 75 74 6f 65 78 74 49 6e 69 74 0d 0a 23 20  dAutoextInit..# 
333d7 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78  define wsdAutoex
333d8 74 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74  t sqlite3Autoext
333d9 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a  ..#endif....../*
333da 0d 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  ..** Register a 
333db 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65  statically linke
333dc 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61 74  d extension that
333dd 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
333de 79 0d 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  y..** loaded by 
333df 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
333e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d 0a  se connection...
333e1 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
333e2 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f  nt sqlite3_auto_
333e3 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28  extension(void (
333e4 2a 78 49 6e 69 74 29 28 76 6f 69 64 29 29 7b 0d  *xInit)(void)){.
333e5 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
333e6 54 45 5f 4f 4b 3b 0d 0a 23 69 66 6e 64 65 66 20  TE_OK;..#ifndef 
333e7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
333e8 49 4e 49 54 0d 0a 20 20 72 63 20 3d 20 73 71 6c  INIT..  rc = sql
333e9 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
333ea 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 7b 0d  );..  if( rc ){.
333eb 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  .    return rc;.
333ec 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66  .  }else..#endif
333ed 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 6e 74 20 69  ..  {..    int i
333ee 3b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ;..#if SQLITE_TH
333ef 52 45 41 44 53 41 46 45 0d 0a 20 20 20 20 73 71  READSAFE..    sq
333f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
333f1 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
333f2 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
333f3 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
333f4 52 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  R);..#endif..   
333f5 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b   wsdAutoextInit;
333f6 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ..    sqlite3_mu
333f7 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
333f8 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
333f9 69 3c 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  i<wsdAutoext.nEx
333fa 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  t; i++){..      
333fb 69 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 61  if( wsdAutoext.a
333fc 45 78 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20  Ext[i]==xInit ) 
333fd 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
333fe 20 20 20 69 66 28 20 69 3d 3d 77 73 64 41 75 74     if( i==wsdAut
333ff 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0d 0a 20 20  oext.nExt ){..  
33400 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
33401 28 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  (wsdAutoext.nExt
33402 2b 31 29 2a 73 69 7a 65 6f 66 28 77 73 64 41 75  +1)*sizeof(wsdAu
33403 74 6f 65 78 74 2e 61 45 78 74 5b 30 5d 29 3b 0d  toext.aExt[0]);.
33404 0a 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a 61  .      void (**a
33405 4e 65 77 29 28 76 6f 69 64 29 3b 0d 0a 20 20 20  New)(void);..   
33406 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
33407 33 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41 75 74  3_realloc(wsdAut
33408 6f 65 78 74 2e 61 45 78 74 2c 20 6e 42 79 74 65  oext.aExt, nByte
33409 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 61 4e  );..      if( aN
3340a 65 77 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ew==0 ){..      
3340b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3340c 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  MEM;..      }els
3340d 65 7b 0d 0a 20 20 20 20 20 20 20 20 77 73 64 41  e{..        wsdA
3340e 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e  utoext.aExt = aN
3340f 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 77 73 64  ew;..        wsd
33410 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 77 73 64  Autoext.aExt[wsd
33411 41 75 74 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20  Autoext.nExt] = 
33412 78 49 6e 69 74 3b 0d 0a 20 20 20 20 20 20 20 20  xInit;..        
33413 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b  wsdAutoext.nExt+
33414 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  +;..      }..   
33415 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
33416 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
33417 78 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  x);..    assert(
33418 20 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29   (rc&0xff)==rc )
33419 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ;..    return rc
3341a 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
3341b 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 75  .** Reset the au
3341c 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
3341d 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e  n loading mechan
3341e 69 73 6d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ism...*/..SQLITE
3341f 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
33420 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
33421 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b 0d 0a 23  ension(void){..#
33422 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
33423 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a 20 20 69  IT_AUTOINIT..  i
33424 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
33425 61 6c 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45 5f  alize()==SQLITE_
33426 4f 4b 20 29 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  OK )..#endif..  
33427 7b 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  {..#if SQLITE_TH
33428 52 45 41 44 53 41 46 45 0d 0a 20 20 20 20 73 71  READSAFE..    sq
33429 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
3342a 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
3342b 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
3342c 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
3342d 52 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  R);..#endif..   
3342e 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b   wsdAutoextInit;
3342f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ..    sqlite3_mu
33430 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
33431 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ;..    sqlite3_f
33432 72 65 65 28 77 73 64 41 75 74 6f 65 78 74 2e 61  ree(wsdAutoext.a
33433 45 78 74 29 3b 0d 0a 20 20 20 20 77 73 64 41 75  Ext);..    wsdAu
33434 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 30 3b 0d  toext.aExt = 0;.
33435 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e  .    wsdAutoext.
33436 6e 45 78 74 20 3d 20 30 3b 0d 0a 20 20 20 20 73  nExt = 0;..    s
33437 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33438 76 65 28 6d 75 74 65 78 29 3b 0d 0a 20 20 7d 0d  ve(mutex);..  }.
33439 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4c 6f 61  .}..../*..** Loa
3343a 64 20 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63 20  d all automatic 
3343b 65 78 74 65 6e 73 69 6f 6e 73 2e 0d 0a 2a 2a 0d  extensions...**.
3343c 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20  .** If anything 
3343d 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73 65 74 20  goes wrong, set 
3343e 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20  an error in the 
3343f 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33440 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ion...*/..SQLITE
33441 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
33442 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74  lite3AutoLoadExt
33443 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
33444 2a 64 62 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d  *db){..  int i;.
33445 0a 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0d 0a  .  int go = 1;..
33446 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74    int rc;..  int
33447 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65   (*xInit)(sqlite
33448 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20  3*,char**,const 
33449 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
3344a 69 6e 65 73 2a 29 3b 0d 0a 0d 0a 20 20 77 73 64  ines*);....  wsd
3344b 41 75 74 6f 65 78 74 49 6e 69 74 3b 0d 0a 20 20  AutoextInit;..  
3344c 69 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e  if( wsdAutoext.n
3344d 45 78 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f  Ext==0 ){..    /
3344e 2a 20 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65  * Common case: e
3344f 61 72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74  arly out without
33450 20 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f   every having to
33451 20 61 63 71 75 69 72 65 20 61 20 6d 75 74 65 78   acquire a mutex
33452 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b   */..    return;
33453 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69 3d 30  ..  }..  for(i=0
33454 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  ; go; i++){..   
33455 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 3b 0d   char *zErrmsg;.
33456 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
33457 41 44 53 41 46 45 0d 0a 20 20 20 20 73 71 6c 69  ADSAFE..    sqli
33458 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
33459 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
3345a 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
3345b 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
3345c 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 73  ;..#endif..    s
3345d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3345e 65 72 28 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20  er(mutex);..    
3345f 69 66 28 20 69 3e 3d 77 73 64 41 75 74 6f 65 78  if( i>=wsdAutoex
33460 74 2e 6e 45 78 74 20 29 7b 0d 0a 20 20 20 20 20  t.nExt ){..     
33461 20 78 49 6e 69 74 20 3d 20 30 3b 0d 0a 20 20 20   xInit = 0;..   
33462 20 20 20 67 6f 20 3d 20 30 3b 0d 0a 20 20 20 20     go = 0;..    
33463 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 78 49  }else{..      xI
33464 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71  nit = (int(*)(sq
33465 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
33466 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
33467 72 6f 75 74 69 6e 65 73 2a 29 29 0d 0a 20 20 20  routines*))..   
33468 20 20 20 20 20 20 20 20 20 20 20 77 73 64 41 75             wsdAu
33469 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0d 0a  toext.aExt[i];..
3346a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
3346b 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
3346c 75 74 65 78 29 3b 0d 0a 20 20 20 20 7a 45 72 72  utex);..    zErr
3346d 6d 73 67 20 3d 20 30 3b 0d 0a 20 20 20 20 69 66  msg = 0;..    if
3346e 28 20 78 49 6e 69 74 20 26 26 20 28 72 63 20 3d  ( xInit && (rc =
3346f 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72   xInit(db, &zErr
33470 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69  msg, &sqlite3Api
33471 73 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  s))!=0 ){..     
33472 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
33473 2c 20 72 63 2c 0d 0a 20 20 20 20 20 20 20 20 20  , rc,..         
33474 20 20 20 22 61 75 74 6f 6d 61 74 69 63 20 65 78     "automatic ex
33475 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
33476 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 45 72  failed: %s", zEr
33477 72 6d 73 67 29 3b 0d 0a 20 20 20 20 20 20 67 6f  rmsg);..      go
33478 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 0;..    }..  
33479 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3347a 45 72 72 6d 73 67 29 3b 0d 0a 20 20 7d 0d 0a 7d  Errmsg);..  }..}
3347b 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
3347c 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65  *** End of loade
3347d 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
3347e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33480 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
33481 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
33482 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pragma.c *******
33483 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33484 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33485 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
33486 33 20 41 70 72 69 6c 20 36 0d 0a 2a 2a 0d 0a 2a  3 April 6..**..*
33487 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
33488 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
33489 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3348a 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3348b 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
3348c 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3348d 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
3348e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
3348f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
33490 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
33491 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
33492 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
33493 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
33494 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
33495 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
33496 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
33497 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
33498 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
33499 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3349a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3349b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3349c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3349d 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
3349e 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
3349f 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
334a0 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61  the PRAGMA comma
334a1 6e 64 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  nd...*/..../*..*
334a2 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20  * Interpret the 
334a3 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20  given string as 
334a4 61 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2e 20  a safety level. 
334a5 20 52 65 74 75 72 6e 20 30 20 66 6f 72 20 4f 46   Return 0 for OF
334a6 46 2c 0d 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e 20  F,..** 1 for ON 
334a7 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32 20  or NORMAL and 2 
334a8 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74 75 72  for FULL.  Retur
334a9 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d 70 74 79  n 1 for an empty
334aa 20 6f 72 20 0d 0a 2a 2a 20 75 6e 72 65 63 6f 67   or ..** unrecog
334ab 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61 72 67  nized string arg
334ac 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e  ument...**..** N
334ad 6f 74 65 20 74 68 61 74 20 74 68 65 20 76 61 6c  ote that the val
334ae 75 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  ues returned are
334af 20 6f 6e 65 20 6c 65 73 73 20 74 68 61 74 20 74   one less that t
334b0 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 0d 0a  he values that..
334b1 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  ** should be pas
334b2 73 65 64 20 69 6e 74 6f 20 73 71 6c 69 74 65 33  sed into sqlite3
334b3 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65  BtreeSetSafetyLe
334b4 76 65 6c 28 29 2e 20 20 54 68 65 20 69 73 20 64  vel().  The is d
334b5 6f 6e 65 0d 0a 2a 2a 20 74 6f 20 73 75 70 70 6f  one..** to suppo
334b6 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20 63 6f  rt legacy SQL co
334b7 64 65 2e 20 20 54 68 65 20 73 61 66 65 74 79 20  de.  The safety 
334b8 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20 62 65  level used to be
334b9 20 62 6f 6f 6c 65 61 6e 0d 0a 2a 2a 20 61 6e 64   boolean..** and
334ba 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20 6d   older scripts m
334bb 61 79 20 68 61 76 65 20 75 73 65 64 20 6e 75 6d  ay have used num
334bc 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20 61  bers 0 for OFF a
334bd 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0d 0a 2a 2f  nd 1 for ON...*/
334be 0d 0a 73 74 61 74 69 63 20 75 38 20 67 65 74 53  ..static u8 getS
334bf 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74  afetyLevel(const
334c0 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 20 20 20 20   char *z){..    
334c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334c2 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34           /* 1234
334c3 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20  56789 123456789 
334c4 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  */..  static con
334c5 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20  st char zText[] 
334c6 3d 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74  = "onoffalseyest
334c7 72 75 65 66 75 6c 6c 22 3b 0d 0a 20 20 73 74 61  ruefull";..  sta
334c8 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66  tic const u8 iOf
334c9 66 73 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20  fset[] = {0, 1, 
334ca 32 2c 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d  2, 4, 9, 12, 16}
334cb 3b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ;..  static cons
334cc 74 20 75 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d  t u8 iLength[] =
334cd 20 7b 32 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c   {2, 2, 3, 5, 3,
334ce 20 34 2c 20 34 7d 3b 0d 0a 20 20 73 74 61 74 69   4, 4};..  stati
334cf 63 20 63 6f 6e 73 74 20 75 38 20 69 56 61 6c 75  c const u8 iValu
334d0 65 5b 5d 20 3d 20 20 7b 31 2c 20 30 2c 20 30 2c  e[] =  {1, 0, 0,
334d1 20 30 2c 20 31 2c 20 31 2c 20 32 7d 3b 0d 0a 20   0, 1, 1, 2};.. 
334d2 20 69 6e 74 20 69 2c 20 6e 3b 0d 0a 20 20 69 66   int i, n;..  if
334d3 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
334d4 28 2a 7a 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  (*z) ){..    ret
334d5 75 72 6e 20 28 75 38 29 73 71 6c 69 74 65 33 41  urn (u8)sqlite3A
334d6 74 6f 69 28 7a 29 3b 0d 0a 20 20 7d 0d 0a 20 20  toi(z);..  }..  
334d7 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
334d8 6e 33 30 28 7a 29 3b 0d 0a 20 20 66 6f 72 28 69  n30(z);..  for(i
334d9 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
334da 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0d  iLength); i++){.
334db 0a 20 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68  .    if( iLength
334dc 5b 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65  [i]==n && sqlite
334dd 33 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74  3StrNICmp(&zText
334de 5b 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e  [iOffset[i]],z,n
334df 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72  )==0 ){..      r
334e0 65 74 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b  eturn iValue[i];
334e1 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
334e2 72 65 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 0d 0a  return 1;..}....
334e3 2f 2a 0d 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  /*..** Interpret
334e4 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
334e5 67 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76  g as a boolean v
334e6 61 6c 75 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  alue...*/..SQLIT
334e7 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
334e8 69 74 65 33 47 65 74 42 6f 6f 6c 65 61 6e 28 63  ite3GetBoolean(c
334e9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0d 0a  onst char *z){..
334ea 20 20 72 65 74 75 72 6e 20 67 65 74 53 61 66 65    return getSafe
334eb 74 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0d 0a 7d  tyLevel(z)&1;..}
334ec 0d 0a 0d 0a 2f 2a 20 54 68 65 20 73 71 6c 69 74  ..../* The sqlit
334ed 65 33 47 65 74 42 6f 6f 6c 65 61 6e 28 29 20 66  e3GetBoolean() f
334ee 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
334ef 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73  by other modules
334f0 20 62 75 74 20 74 68 65 0d 0a 2a 2a 20 72 65 6d   but the..** rem
334f1 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66  ainder of this f
334f2 69 6c 65 20 69 73 20 73 70 65 63 69 66 69 63 20  ile is specific 
334f3 74 6f 20 50 52 41 47 4d 41 20 70 72 6f 63 65 73  to PRAGMA proces
334f4 73 69 6e 67 2e 20 20 53 6f 20 6f 6d 69 74 0d 0a  sing.  So omit..
334f5 2a 2a 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  ** the rest of t
334f6 68 65 20 66 69 6c 65 20 69 66 20 50 52 41 47 4d  he file if PRAGM
334f7 41 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 66  As are omitted f
334f8 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0d 0a  rom the build...
334f9 2a 2f 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
334fa 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41  (SQLITE_OMIT_PRA
334fb 47 4d 41 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  GMA)..../*..** I
334fc 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76  nterpret the giv
334fd 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 6c  en string as a l
334fe 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75  ocking mode valu
334ff 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
33500 6e 74 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64  nt getLockingMod
33501 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  e(const char *z)
33502 7b 0d 0a 20 20 69 66 28 20 7a 20 29 7b 0d 0a 20  {..  if( z ){.. 
33503 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
33504 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 65 78 63  3StrICmp(z, "exc
33505 6c 75 73 69 76 65 22 29 20 29 20 72 65 74 75 72  lusive") ) retur
33506 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  n PAGER_LOCKINGM
33507 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3b 0d 0a  ODE_EXCLUSIVE;..
33508 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
33509 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f  e3StrICmp(z, "no
3350a 72 6d 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20  rmal") ) return 
3350b 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
3350c 45 5f 4e 4f 52 4d 41 4c 3b 0d 0a 20 20 7d 0d 0a  E_NORMAL;..  }..
3350d 20 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c    return PAGER_L
3350e 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
3350f 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ;..}....#ifndef 
33510 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
33511 56 41 43 55 55 4d 0d 0a 2f 2a 0d 0a 2a 2a 20 49  VACUUM../*..** I
33512 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76  nterpret the giv
33513 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e 20  en string as an 
33514 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
33515 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   value...**..** 
33516 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
33517 72 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22  rings, "none", "
33518 66 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65  full" and "incre
33519 6d 65 6e 74 61 6c 22 20 61 72 65 20 0d 0a 2a 2a  mental" are ..**
3351a 20 61 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20   acceptable, as 
3351b 61 72 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69  are their numeri
3351c 63 20 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30  c equivalents: 0
3351d 2c 20 31 20 61 6e 64 20 32 20 72 65 73 70 65 63  , 1 and 2 respec
3351e 74 69 76 65 6c 79 2e 0d 0a 2a 2f 0d 0a 73 74 61  tively...*/..sta
3351f 74 69 63 20 69 6e 74 20 67 65 74 41 75 74 6f 56  tic int getAutoV
33520 61 63 75 75 6d 28 63 6f 6e 73 74 20 63 68 61 72  acuum(const char
33521 20 2a 7a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d   *z){..  int i;.
33522 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
33523 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 6e  3StrICmp(z, "non
33524 65 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52  e") ) return BTR
33525 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
33526 4e 45 3b 0d 0a 20 20 69 66 28 20 30 3d 3d 73 71  NE;..  if( 0==sq
33527 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
33528 22 66 75 6c 6c 22 29 20 29 20 72 65 74 75 72 6e  "full") ) return
33529 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
3352a 4d 5f 46 55 4c 4c 3b 0d 0a 20 20 69 66 28 20 30  M_FULL;..  if( 0
3352b 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
3352c 28 7a 2c 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c  (z, "incremental
3352d 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45  ") ) return BTRE
3352e 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
3352f 52 3b 0d 0a 20 20 69 20 3d 20 73 71 6c 69 74 65  R;..  i = sqlite
33530 33 41 74 6f 69 28 7a 29 3b 0d 0a 20 20 72 65 74  3Atoi(z);..  ret
33531 75 72 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26  urn (u8)((i>=0&&
33532 69 3c 3d 32 29 3f 69 3a 30 29 3b 0d 0a 7d 0d 0a  i<=2)?i:0);..}..
33533 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66  #endif /* ifndef
33534 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
33535 4f 56 41 43 55 55 4d 20 2a 2f 0d 0a 0d 0a 23 69  OVACUUM */....#i
33536 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33537 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0d  T_PAGER_PRAGMAS.
33538 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74 65 72 70 72 65  ./*..** Interpre
33539 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
3353a 6e 67 20 61 73 20 61 20 74 65 6d 70 20 64 62 20  ng as a temp db 
3353b 6c 6f 63 61 74 69 6f 6e 2e 20 52 65 74 75 72 6e  location. Return
3353c 20 31 20 66 6f 72 20 66 69 6c 65 0d 0a 2a 2a 20   1 for file..** 
3353d 62 61 63 6b 65 64 20 74 65 6d 70 6f 72 61 72 79  backed temporary
3353e 20 64 61 74 61 62 61 73 65 73 2c 20 32 20 66 6f   databases, 2 fo
3353f 72 20 74 68 65 20 52 65 64 2d 42 6c 61 63 6b 20  r the Red-Black 
33540 74 72 65 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64  tree in memory d
33541 61 74 61 62 61 73 65 0d 0a 2a 2a 20 61 6e 64 20  atabase..** and 
33542 30 20 74 6f 20 75 73 65 20 74 68 65 20 63 6f 6d  0 to use the com
33543 70 69 6c 65 2d 74 69 6d 65 20 64 65 66 61 75 6c  pile-time defaul
33544 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  t...*/..static i
33545 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28  nt getTempStore(
33546 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0d  const char *z){.
33547 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27  .  if( z[0]>='0'
33548 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b   && z[0]<='2' ){
33549 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30  ..    return z[0
3354a 5d 20 2d 20 27 30 27 3b 0d 0a 20 20 7d 65 6c 73  ] - '0';..  }els
3354b 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72  e if( sqlite3Str
3354c 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d  ICmp(z, "file")=
3354d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
3354e 6e 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  n 1;..  }else if
3354f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
33550 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
33551 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
33552 32 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  2;..  }else{..  
33553 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
33554 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ..}..#endif /* S
33555 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47  QLITE_PAGER_PRAG
33556 4d 41 53 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65  MAS */....#ifnde
33557 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
33558 47 45 52 5f 50 52 41 47 4d 41 53 0d 0a 2f 2a 0d  GER_PRAGMAS../*.
33559 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
3355a 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65 69 74  emp storage, eit
3355b 68 65 72 20 77 68 65 6e 20 74 68 65 20 74 65 6d  her when the tem
3355c 70 20 73 74 6f 72 61 67 65 20 69 73 20 63 68 61  p storage is cha
3355d 6e 67 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20 64 65  nged..** from de
3355e 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20 27  fault, or when '
3355f 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74 65  file' and the te
33560 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
33561 72 79 20 68 61 73 20 63 68 61 6e 67 65 64 0d 0a  ry has changed..
33562 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69  */..static int i
33563 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f  nvalidateTempSto
33564 72 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  rage(Parse *pPar
33565 73 65 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  se){..  sqlite3 
33566 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
33567 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  ;..  if( db->aDb
33568 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0d 0a 20  [1].pBt!=0 ){.. 
33569 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
3356a 43 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65  Commit || sqlite
3356b 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
3356c 61 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ans(db->aDb[1].p
3356d 42 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  Bt) ){..      sq
3356e 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
3356f 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
33570 20 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20   storage cannot 
33571 62 65 20 63 68 61 6e 67 65 64 20 22 0d 0a 20 20  be changed "..  
33572 20 20 20 20 20 20 22 66 72 6f 6d 20 77 69 74 68        "from with
33573 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
33574 22 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  ");..      retur
33575 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
33576 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
33577 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62  te3BtreeClose(db
33578 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0d 0a  ->aDb[1].pBt);..
33579 20 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70      db->aDb[1].p
3357a 42 74 20 3d 20 30 3b 0d 0a 20 20 20 20 73 71 6c  Bt = 0;..    sql
3357b 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
3357c 6c 53 63 68 65 6d 61 28 64 62 2c 20 2d 31 29 3b  lSchema(db, -1);
3357d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
3357e 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23  SQLITE_OK;..}..#
3357f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
33580 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
33581 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
33582 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
33583 41 47 4d 41 53 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66  AGMAS../*..** If
33584 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
33585 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73  se is open, clos
33586 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68  e it and mark th
33587 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
33588 61 0d 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67  a..** as needing
33589 20 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69   reloading.  Thi
3358a 73 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77  s must be done w
3358b 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51  hen using the SQ
3358c 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0d  LITE_TEMP_STORE.
3358d 0a 2a 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54  .** or DEFAULT_T
3358e 45 4d 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61  EMP_STORE pragma
3358f 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
33590 6e 74 20 63 68 61 6e 67 65 54 65 6d 70 53 74 6f  nt changeTempSto
33591 72 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  rage(Parse *pPar
33592 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
33593 7a 53 74 6f 72 61 67 65 54 79 70 65 29 7b 0d 0a  zStorageType){..
33594 20 20 69 6e 74 20 74 73 20 3d 20 67 65 74 54 65    int ts = getTe
33595 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72 61 67 65  mpStore(zStorage
33596 54 79 70 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Type);..  sqlite
33597 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
33598 64 62 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 74  db;..  if( db->t
33599 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29 20  emp_store==ts ) 
3359a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3359b 3b 0d 0a 20 20 69 66 28 20 69 6e 76 61 6c 69 64  ;..  if( invalid
3359c 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 20  ateTempStorage( 
3359d 70 50 61 72 73 65 20 29 20 21 3d 20 53 51 4c 49  pParse ) != SQLI
3359e 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 65  TE_OK ){..    re
3359f 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
335a0 52 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d 3e 74  R;..  }..  db->t
335a1 65 6d 70 5f 73 74 6f 72 65 20 3d 20 28 75 38 29  emp_store = (u8)
335a2 74 73 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  ts;..  return SQ
335a3 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e  LITE_OK;..}..#en
335a4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41  dif /* SQLITE_PA
335a5 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0d 0a  GER_PRAGMAS */..
335a6 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74  ../*..** Generat
335a7 65 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e  e code to return
335a8 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65   a single intege
335a9 72 20 76 61 6c 75 65 2e 0d 0a 2a 2f 0d 0a 73 74  r value...*/..st
335aa 61 74 69 63 20 76 6f 69 64 20 72 65 74 75 72 6e  atic void return
335ab 53 69 6e 67 6c 65 49 6e 74 28 50 61 72 73 65 20  SingleInt(Parse 
335ac 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
335ad 68 61 72 20 2a 7a 4c 61 62 65 6c 2c 20 69 36 34  har *zLabel, i64
335ae 20 76 61 6c 75 65 29 7b 0d 0a 20 20 56 64 62 65   value){..  Vdbe
335af 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
335b0 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a 20  Vdbe(pParse);.. 
335b1 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61   int mem = ++pPa
335b2 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 69 36  rse->nMem;..  i6
335b3 34 20 2a 70 49 36 34 20 3d 20 73 71 6c 69 74 65  4 *pI64 = sqlite
335b4 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 50 61  3DbMallocRaw(pPa
335b5 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28  rse->db, sizeof(
335b6 76 61 6c 75 65 29 29 3b 0d 0a 20 20 69 66 28 20  value));..  if( 
335b7 70 49 36 34 20 29 7b 0d 0a 20 20 20 20 6d 65 6d  pI64 ){..    mem
335b8 63 70 79 28 70 49 36 34 2c 20 26 76 61 6c 75 65  cpy(pI64, &value
335b9 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29  , sizeof(value))
335ba 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
335bb 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
335bc 50 5f 49 6e 74 36 34 2c 20 30 2c 20 6d 65 6d 2c  P_Int64, 0, mem,
335bd 20 30 2c 20 28 63 68 61 72 2a 29 70 49 36 34 2c   0, (char*)pI64,
335be 20 50 34 5f 49 4e 54 36 34 29 3b 0d 0a 20 20 73   P4_INT64);..  s
335bf 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
335c0 43 6f 6c 73 28 76 2c 20 31 29 3b 0d 0a 20 20 73  Cols(v, 1);..  s
335c1 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
335c2 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
335c3 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c 2c  ME_NAME, zLabel,
335c4 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
335c5 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
335c6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
335c7 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b 0d  ltRow, mem, 1);.
335c8 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  .}....#ifndef SQ
335c9 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
335ca 52 41 47 4d 41 53 0d 0a 2f 2a 0d 0a 2a 2a 20 43  RAGMAS../*..** C
335cb 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a  heck to see if z
335cc 52 69 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20  Right and zLeft 
335cd 72 65 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d  refer to a pragm
335ce 61 20 74 68 61 74 20 71 75 65 72 69 65 73 0d 0a  a that queries..
335cf 2a 2a 20 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e  ** or changes on
335d0 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 69  e of the flags i
335d1 6e 20 64 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65  n db->flags.  Re
335d2 74 75 72 6e 20 31 20 69 66 20 73 6f 20 61 6e 64  turn 1 if so and
335d3 20 30 20 69 66 20 6e 6f 74 2e 0d 0a 2a 2a 20 41   0 if not...** A
335d4 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 74  lso, implement t
335d5 68 65 20 70 72 61 67 6d 61 2e 0d 0a 2a 2f 0d 0a  he pragma...*/..
335d6 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 67 50  static int flagP
335d7 72 61 67 6d 61 28 50 61 72 73 65 20 2a 70 50 61  ragma(Parse *pPa
335d8 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
335d9 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68  *zLeft, const ch
335da 61 72 20 2a 7a 52 69 67 68 74 29 7b 0d 0a 20 20  ar *zRight){..  
335db 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
335dc 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65 20  uct sPragmaType 
335dd 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  {..    const cha
335de 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61  r *zName;  /* Na
335df 6d 65 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61  me of the pragma
335e0 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6d 61 73   */..    int mas
335e1 6b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  k;           /* 
335e2 4d 61 73 6b 20 66 6f 72 20 74 68 65 20 64 62 2d  Mask for the db-
335e3 3e 66 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f 0d  >flags value */.
335e4 0a 20 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d  .  } aPragma[] =
335e5 20 7b 0d 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f   {..    { "full_
335e6 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20  column_names",  
335e7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c        SQLITE_Ful
335e8 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0d 0a 20  lColNames  },.. 
335e9 20 20 20 7b 20 22 73 68 6f 72 74 5f 63 6f 6c 75     { "short_colu
335ea 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20  mn_names",      
335eb 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
335ec 4e 61 6d 65 73 20 7d 2c 0d 0a 20 20 20 20 7b 20  Names },..    { 
335ed 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 22 2c  "count_changes",
335ee 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
335ef 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20  TE_CountRows    
335f0 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 65 6d 70 74   },..    { "empt
335f1 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62 61 63  y_result_callbac
335f2 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f 4e 75  ks",   SQLITE_Nu
335f3 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c 0d 0a  llCallback  },..
335f4 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66 69      { "legacy_fi
335f5 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20 20  le_format",     
335f6 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46    SQLITE_LegacyF
335f7 69 6c 65 46 6d 74 20 7d 2c 0d 0a 20 20 20 20 7b  ileFmt },..    {
335f8 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20   "fullfsync",   
335f9 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
335fa 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20  ITE_FullFSync   
335fb 20 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 63 68 65    },..    { "che
335fc 63 6b 70 6f 69 6e 74 5f 66 75 6c 6c 66 73 79 6e  ckpoint_fullfsyn
335fd 63 22 2c 20 20 20 20 20 53 51 4c 49 54 45 5f 43  c",     SQLITE_C
335fe 6b 70 74 46 75 6c 6c 46 53 79 6e 63 20 7d 2c 0d  kptFullFSync },.
335ff 0a 20 20 20 20 7b 20 22 72 65 76 65 72 73 65 5f  .    { "reverse_
33600 75 6e 6f 72 64 65 72 65 64 5f 73 65 6c 65 63 74  unordered_select
33601 73 22 2c 20 53 51 4c 49 54 45 5f 52 65 76 65 72  s", SQLITE_Rever
33602 73 65 4f 72 64 65 72 20 20 7d 2c 0d 0a 23 69 66  seOrder  },..#if
33603 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33604 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58  _AUTOMATIC_INDEX
33605 0d 0a 20 20 20 20 7b 20 22 61 75 74 6f 6d 61 74  ..    { "automat
33606 69 63 5f 69 6e 64 65 78 22 2c 20 20 20 20 20 20  ic_index",      
33607 20 20 20 20 53 51 4c 49 54 45 5f 41 75 74 6f 49      SQLITE_AutoI
33608 6e 64 65 78 20 20 20 20 20 7d 2c 0d 0a 23 65 6e  ndex     },..#en
33609 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
3360a 54 45 5f 44 45 42 55 47 0d 0a 20 20 20 20 7b 20  TE_DEBUG..    { 
3360b 22 73 71 6c 5f 74 72 61 63 65 22 2c 20 20 20 20  "sql_trace",    
3360c 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
3360d 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20  TE_SqlTrace     
3360e 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 76 64 62 65   },..    { "vdbe
3360f 5f 6c 69 73 74 69 6e 67 22 2c 20 20 20 20 20 20  _listing",      
33610 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64         SQLITE_Vd
33611 62 65 4c 69 73 74 69 6e 67 20 20 20 7d 2c 0d 0a  beListing   },..
33612 20 20 20 20 7b 20 22 76 64 62 65 5f 74 72 61 63      { "vdbe_trac
33613 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
33614 20 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61    SQLITE_VdbeTra
33615 63 65 20 20 20 20 20 7d 2c 0d 0a 23 65 6e 64 69  ce     },..#endi
33616 66 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
33617 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0d 0a 20 20  E_OMIT_CHECK..  
33618 20 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68 65 63    { "ignore_chec
33619 6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22 2c 20  k_constraints", 
3361a 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
3361b 63 6b 73 20 20 7d 2c 0d 0a 23 65 6e 64 69 66 0d  cks  },..#endif.
3361c 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
3361d 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20 65 78  owing is VERY ex
3361e 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0d 0a 20  perimental */.. 
3361f 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f 73     { "writable_s
33620 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20 20  chema",         
33621 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
33622 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f 76  ema|SQLITE_Recov
33623 65 72 79 4d 6f 64 65 20 7d 2c 0d 0a 20 20 20 20  eryMode },..    
33624 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b  { "omit_readlock
33625 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ",            SQ
33626 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20  LITE_NoReadlock 
33627 20 20 20 7d 2c 0d 0a 0d 0a 20 20 20 20 2f 2a 20     },....    /* 
33628 54 4f 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73  TODO: Maybe it s
33629 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73  houldn't be poss
3362a 69 62 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74  ible to change t
3362b 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  he ReadUncommitt
3362c 65 64 0d 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20  ed..    ** flag 
3362d 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
3362e 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
3362f 74 73 2e 20 2a 2f 0d 0a 20 20 20 20 7b 20 22 72  ts. */..    { "r
33630 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74 65 64 22  ead_uncommitted"
33631 2c 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  ,         SQLITE
33632 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
33633 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 72 65 63 75   },..    { "recu
33634 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 22 2c  rsive_triggers",
33635 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65         SQLITE_Re
33636 63 54 72 69 67 67 65 72 73 20 7d 2c 0d 0a 0d 0a  cTriggers },....
33637 20 20 20 20 2f 2a 20 54 68 69 73 20 66 6c 61 67      /* This flag
33638 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 73 65 74   may only be set
33639 20 69 66 20 62 6f 74 68 20 66 6f 72 65 69 67 6e   if both foreign
3363a 2d 6b 65 79 20 61 6e 64 20 74 72 69 67 67 65 72  -key and trigger
3363b 20 73 75 70 70 6f 72 74 0d 0a 20 20 20 20 2a 2a   support..    **
3363c 20 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20   are present in 
3363d 74 68 65 20 62 75 69 6c 64 2e 20 20 2a 2f 0d 0a  the build.  */..
3363e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3363f 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
33640 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65  _KEY) && !define
33641 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
33642 49 47 47 45 52 29 0d 0a 20 20 20 20 7b 20 22 66  IGGER)..    { "f
33643 6f 72 65 69 67 6e 5f 6b 65 79 73 22 2c 20 20 20  oreign_keys",   
33644 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
33645 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 7d 2c 0d  _ForeignKeys },.
33646 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a 20  .#endif..  };.. 
33647 20 69 6e 74 20 69 3b 0d 0a 20 20 63 6f 6e 73 74   int i;..  const
33648 20 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54   struct sPragmaT
33649 79 70 65 20 2a 70 3b 0d 0a 20 20 66 6f 72 28 69  ype *p;..  for(i
3364a 3d 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b 20 69  =0, p=aPragma; i
3364b 3c 41 72 72 61 79 53 69 7a 65 28 61 50 72 61 67  <ArraySize(aPrag
3364c 6d 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0d  ma); i++, p++){.
3364d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
3364e 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 70  StrICmp(zLeft, p
3364f 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a  ->zName)==0 ){..
33650 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
33651 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d  b = pParse->db;.
33652 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b 0d  .      Vdbe *v;.
33653 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74  .      v = sqlit
33654 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
33655 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
33656 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 41 6c  ( v!=0 );  /* Al
33657 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
33658 62 79 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61  by sqlite3Pragma
33659 28 29 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28  () */..      if(
3365a 20 41 4c 57 41 59 53 28 76 29 20 29 7b 0d 0a 20   ALWAYS(v) ){.. 
3365b 20 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68         if( zRigh
3365c 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  t==0 ){..       
3365d 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
3365e 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 7a 4e  nt(pParse, p->zN
3365f 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67 73 20  ame, (db->flags 
33660 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20 29 3b  & p->mask)!=0 );
33661 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ..        }else{
33662 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ..          int 
33663 6d 61 73 6b 20 3d 20 70 2d 3e 6d 61 73 6b 3b 20  mask = p->mask; 
33664 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
33665 20 6f 66 20 62 69 74 73 20 74 6f 20 73 65 74 20   of bits to set 
33666 6f 72 20 63 6c 65 61 72 2e 20 2a 2f 0d 0a 20 20  or clear. */..  
33667 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
33668 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 7b  autoCommit==0 ){
33669 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ..            /*
3366a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70   Foreign key sup
3366b 70 6f 72 74 20 6d 61 79 20 6e 6f 74 20 62 65 20  port may not be 
3366c 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62  enabled or disab
3366d 6c 65 64 20 77 68 69 6c 65 20 6e 6f 74 0d 0a 20  led while not.. 
3366e 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e             ** in
3366f 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64   auto-commit mod
33670 65 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  e.  */..        
33671 20 20 20 20 6d 61 73 6b 20 26 3d 20 7e 28 53 51      mask &= ~(SQ
33672 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
33673 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d  );..          }.
33674 0a 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ...          if(
33675 20 73 71 6c 69 74 65 33 47 65 74 42 6f 6f 6c 65   sqlite3GetBoole
33676 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0d 0a 20  an(zRight) ){.. 
33677 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 66             db->f
33678 6c 61 67 73 20 7c 3d 20 6d 61 73 6b 3b 0d 0a 20  lags |= mask;.. 
33679 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d           }else{.
3367a 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d  .            db-
3367b 3e 66 6c 61 67 73 20 26 3d 20 7e 6d 61 73 6b 3b  >flags &= ~mask;
3367c 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 0d  ..          }...
3367d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  .          /* Ma
3367e 6e 79 20 6f 66 20 74 68 65 20 66 6c 61 67 2d 70  ny of the flag-p
3367f 72 61 67 6d 61 73 20 6d 6f 64 69 66 79 20 74 68  ragmas modify th
33680 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
33681 20 62 79 20 74 68 65 20 53 51 4c 20 0d 0a 20 20   by the SQL ..  
33682 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69          ** compi
33683 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63  ler (eg. count_c
33684 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64 64 20  hanges). So add 
33685 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65 78 70  an opcode to exp
33686 69 72 65 20 61 6c 6c 0d 0a 20 20 20 20 20 20 20  ire all..       
33687 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53     ** compiled S
33688 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 66  QL statements af
33689 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20  ter modifying a 
3368a 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0d 0a 20  pragma value... 
3368b 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20           */..   
3368c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3368d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
3368e 78 70 69 72 65 2c 20 30 2c 20 30 29 3b 0d 0a 20  xpire, 0, 0);.. 
3368f 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
33690 7d 0d 0a 0d 0a 20 20 20 20 20 20 72 65 74 75 72  }....      retur
33691 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  n 1;..    }..  }
33692 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
33693 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
33694 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41  TE_OMIT_FLAG_PRA
33695 47 4d 41 53 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  GMAS */..../*..*
33696 2a 20 52 65 74 75 72 6e 20 61 20 68 75 6d 61 6e  * Return a human
33697 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d 65 20 66  -readable name f
33698 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20  or a constraint 
33699 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63 74 69 6f  resolution actio
3369a 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20  n...*/..#ifndef 
3369b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
3369c 49 47 4e 5f 4b 45 59 0d 0a 73 74 61 74 69 63 20  IGN_KEY..static 
3369d 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69  const char *acti
3369e 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e  onName(u8 action
3369f 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ){..  const char
336a0 20 2a 7a 4e 61 6d 65 3b 0d 0a 20 20 73 77 69 74   *zName;..  swit
336a1 63 68 28 20 61 63 74 69 6f 6e 20 29 7b 0d 0a 20  ch( action ){.. 
336a2 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74 4e 75     case OE_SetNu
336a3 6c 6c 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45  ll:  zName = "SE
336a4 54 20 4e 55 4c 4c 22 3b 20 20 20 20 20 20 20 20  T NULL";        
336a5 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65  break;..    case
336a6 20 4f 45 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e   OE_SetDflt:  zN
336a7 61 6d 65 20 3d 20 22 53 45 54 20 44 45 46 41 55  ame = "SET DEFAU
336a8 4c 54 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0d  LT";     break;.
336a9 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61 73  .    case OE_Cas
336aa 63 61 64 65 3a 20 20 7a 4e 61 6d 65 20 3d 20 22  cade:  zName = "
336ab 43 41 53 43 41 44 45 22 3b 20 20 20 20 20 20 20  CASCADE";       
336ac 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61    break;..    ca
336ad 73 65 20 4f 45 5f 52 65 73 74 72 69 63 74 3a 20  se OE_Restrict: 
336ae 7a 4e 61 6d 65 20 3d 20 22 52 45 53 54 52 49 43  zName = "RESTRIC
336af 54 22 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b  T";        break
336b0 3b 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  ;..    default: 
336b1 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d           zName =
336b2 20 22 4e 4f 20 41 43 54 49 4f 4e 22 3b 20 20 0d   "NO ACTION";  .
336b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
336b4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61         assert( a
336b5 63 74 69 6f 6e 3d 3d 4f 45 5f 4e 6f 6e 65 20 29  ction==OE_None )
336b6 3b 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20  ; break;..  }.. 
336b7 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0d 0a   return zName;..
336b8 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f  }..#endif....../
336b9 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  *..** Parameter 
336ba 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e  eMode must be on
336bb 65 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a  e of the PAGER_J
336bc 4f 55 52 4e 41 4c 4d 4f 44 45 5f 58 58 58 20 63  OURNALMODE_XXX c
336bd 6f 6e 73 74 61 6e 74 73 0d 0a 2a 2a 20 64 65 66  onstants..** def
336be 69 6e 65 64 20 69 6e 20 70 61 67 65 72 2e 68 2e  ined in pager.h.
336bf 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
336c0 65 74 75 72 6e 73 20 74 68 65 20 61 73 73 6f 63  eturns the assoc
336c1 69 61 74 65 64 20 6c 6f 77 65 72 63 61 73 65 0d  iated lowercase.
336c2 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  .** journal-mode
336c3 20 6e 61 6d 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   name...*/..SQLI
336c4 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
336c5 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4a 6f   char *sqlite3Jo
336c6 75 72 6e 61 6c 4d 6f 64 65 6e 61 6d 65 28 69 6e  urnalModename(in
336c7 74 20 65 4d 6f 64 65 29 7b 0d 0a 20 20 73 74 61  t eMode){..  sta
336c8 74 69 63 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  tic char * const
336c9 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20   azModeName[] = 
336ca 7b 0d 0a 20 20 20 20 22 64 65 6c 65 74 65 22 2c  {..    "delete",
336cb 20 22 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66   "persist", "off
336cc 22 2c 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22  ", "truncate", "
336cd 6d 65 6d 6f 72 79 22 0d 0a 23 69 66 6e 64 65 66  memory"..#ifndef
336ce 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c   SQLITE_OMIT_WAL
336cf 0d 0a 20 20 20 20 20 2c 20 22 77 61 6c 22 0d 0a  ..     , "wal"..
336d0 23 65 6e 64 69 66 0d 0a 20 20 7d 3b 0d 0a 20 20  #endif..  };..  
336d1 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4a 4f  assert( PAGER_JO
336d2 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
336d3 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ==0 );..  assert
336d4 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ( PAGER_JOURNALM
336d5 4f 44 45 5f 50 45 52 53 49 53 54 3d 3d 31 20 29  ODE_PERSIST==1 )
336d6 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  ;..  assert( PAG
336d7 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
336d8 46 46 3d 3d 32 20 29 3b 0d 0a 20 20 61 73 73 65  FF==2 );..  asse
336d9 72 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  rt( PAGER_JOURNA
336da 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 3d 3d  LMODE_TRUNCATE==
336db 33 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  3 );..  assert( 
336dc 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
336dd 45 5f 4d 45 4d 4f 52 59 3d 3d 34 20 29 3b 0d 0a  E_MEMORY==4 );..
336de 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
336df 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 3d  JOURNALMODE_WAL=
336e0 3d 35 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  =5 );..  assert(
336e1 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 65 4d 6f   eMode>=0 && eMo
336e2 64 65 3c 3d 41 72 72 61 79 53 69 7a 65 28 61 7a  de<=ArraySize(az
336e3 4d 6f 64 65 4e 61 6d 65 29 20 29 3b 0d 0a 0d 0a  ModeName) );....
336e4 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 41 72 72    if( eMode==Arr
336e5 61 79 53 69 7a 65 28 61 7a 4d 6f 64 65 4e 61 6d  aySize(azModeNam
336e6 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a  e) ) return 0;..
336e7 20 20 72 65 74 75 72 6e 20 61 7a 4d 6f 64 65 4e    return azModeN
336e8 61 6d 65 5b 65 4d 6f 64 65 5d 3b 0d 0a 7d 0d 0a  ame[eMode];..}..
336e9 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 6f 63 65 73 73  ../*..** Process
336ea 20 61 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d   a pragma statem
336eb 65 6e 74 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 50  ent.  ..**..** P
336ec 72 61 67 6d 61 73 20 61 72 65 20 6f 66 20 74 68  ragmas are of th
336ed 69 73 20 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a  is form:..**..**
336ee 20 20 20 20 20 20 50 52 41 47 4d 41 20 5b 64 61        PRAGMA [da
336ef 74 61 62 61 73 65 2e 5d 69 64 20 5b 3d 20 76 61  tabase.]id [= va
336f0 6c 75 65 5d 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  lue]..**..** The
336f1 20 69 64 65 6e 74 69 66 69 65 72 20 6d 69 67 68   identifier migh
336f2 74 20 61 6c 73 6f 20 62 65 20 61 20 73 74 72 69  t also be a stri
336f3 6e 67 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  ng.  The value i
336f4 73 20 61 20 73 74 72 69 6e 67 2c 20 61 6e 64 0d  s a string, and.
336f5 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2c 20  .** identifier, 
336f6 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66  or a number.  If
336f7 20 6d 69 6e 75 73 46 6c 61 67 20 69 73 20 74 72   minusFlag is tr
336f8 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ue, then the val
336f9 75 65 20 69 73 0d 0a 2a 2a 20 61 20 6e 75 6d 62  ue is..** a numb
336fa 65 72 20 74 68 61 74 20 77 61 73 20 70 72 65 63  er that was prec
336fb 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75 73 20  eded by a minus 
336fc 73 69 67 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  sign...**..** If
336fd 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
336fe 73 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20  s "database.id" 
336ff 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
33700 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0d 0a   database name..
33701 2a 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20 74  ** and pId2 is t
33702 68 65 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c  he id.  If the l
33703 65 66 74 20 73 69 64 65 20 69 73 20 6a 75 73 74  eft side is just
33704 20 22 69 64 22 20 74 68 65 6e 20 70 49 64 31 20   "id" then pId1 
33705 69 73 20 74 68 65 0d 0a 2a 2a 20 69 64 20 61 6e  is the..** id an
33706 64 20 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d  d pId2 is any em
33707 70 74 79 20 73 74 72 69 6e 67 2e 0d 0a 2a 2f 0d  pty string...*/.
33708 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33709 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 61 67  void sqlite3Prag
3370a 6d 61 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  ma(..  Parse *pP
3370b 61 72 73 65 2c 20 0d 0a 20 20 54 6f 6b 65 6e 20  arse, ..  Token 
3370c 2a 70 49 64 31 2c 20 20 20 20 20 20 20 20 2f 2a  *pId1,        /*
3370d 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 5b   First part of [
3370e 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65  database.]id fie
3370f 6c 64 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a  ld */..  Token *
33710 70 49 64 32 2c 20 20 20 20 20 20 20 20 2f 2a 20  pId2,        /* 
33711 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 5b  Second part of [
33712 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65  database.]id fie
33713 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a  ld, or NULL */..
33714 20 20 54 6f 6b 65 6e 20 2a 70 56 61 6c 75 65 2c    Token *pValue,
33715 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 66        /* Token f
33716 6f 72 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e  or <value>, or N
33717 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 69  ULL */..  int mi
33718 6e 75 73 46 6c 61 67 20 20 20 20 20 20 20 2f 2a  nusFlag       /*
33719 20 54 72 75 65 20 69 66 20 61 20 27 2d 27 20 73   True if a '-' s
3371a 69 67 6e 20 70 72 65 63 65 64 65 64 20 3c 76 61  ign preceded <va
3371b 6c 75 65 3e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63  lue> */..){..  c
3371c 68 61 72 20 2a 7a 4c 65 66 74 20 3d 20 30 3b 20  har *zLeft = 0; 
3371d 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72        /* Nul-ter
3371e 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
3371f 72 69 6e 67 20 3c 69 64 3e 20 2a 2f 0d 0a 20 20  ring <id> */..  
33720 63 68 61 72 20 2a 7a 52 69 67 68 74 20 3d 20 30  char *zRight = 0
33721 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65  ;      /* Nul-te
33722 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
33723 74 72 69 6e 67 20 3c 76 61 6c 75 65 3e 2c 20 6f  tring <value>, o
33724 72 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 63 6f 6e  r NULL */..  con
33725 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 30  st char *zDb = 0
33726 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  ;   /* The datab
33727 61 73 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 54  ase name */..  T
33728 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20 20 20  oken *pId;      
33729 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
3372a 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a   to <id> token *
3372b 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  /..  int iDb;   
3372c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
3372d 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66 6f  atabase index fo
3372e 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f 0d  r <database> */.
3372f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
33730 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20   pParse->db;..  
33731 44 62 20 2a 70 44 62 3b 0d 0a 20 20 56 64 62 65  Db *pDb;..  Vdbe
33732 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
33733 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dbe = sqlite3Vdb
33734 65 43 72 65 61 74 65 28 64 62 29 3b 0d 0a 20 20  eCreate(db);..  
33735 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
33736 6e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  n;..  sqlite3Vdb
33737 65 52 75 6e 4f 6e 6c 79 4f 6e 63 65 28 76 29 3b  eRunOnlyOnce(v);
33738 0d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ..  pParse->nMem
33739 20 3d 20 32 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e   = 2;....  /* In
3373a 74 65 72 70 72 65 74 20 74 68 65 20 5b 64 61 74  terpret the [dat
3373b 61 62 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20  abase.] part of 
3373c 74 68 65 20 70 72 61 67 6d 61 20 73 74 61 74 65  the pragma state
3373d 6d 65 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65  ment. iDb is the
3373e 0d 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20  ..  ** index of 
3373f 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 69  the database thi
33740 73 20 70 72 61 67 6d 61 20 69 73 20 62 65 69 6e  s pragma is bein
33741 67 20 61 70 70 6c 69 65 64 20 74 6f 20 69 6e 20  g applied to in 
33742 64 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0d 0a 20 20  db.aDb[]. */..  
33743 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
33744 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
33745 20 70 49 64 31 2c 20 70 49 64 32 2c 20 26 70 49   pId1, pId2, &pI
33746 64 29 3b 0d 0a 20 20 69 66 28 20 69 44 62 3c 30  d);..  if( iDb<0
33747 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 70 44   ) return;..  pD
33748 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
33749 5d 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68  ];....  /* If th
3374a 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20  e temp database 
3374b 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69  has been explici
3374c 74 6c 79 20 6e 61 6d 65 64 20 61 73 20 70 61 72  tly named as par
3374d 74 20 6f 66 20 74 68 65 20 0d 0a 20 20 2a 2a 20  t of the ..  ** 
3374e 70 72 61 67 6d 61 2c 20 6d 61 6b 65 20 73 75 72  pragma, make sur
3374f 65 20 69 74 20 69 73 20 6f 70 65 6e 2e 20 0d 0a  e it is open. ..
33750 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69 44 62 3d    */..  if( iDb=
33751 3d 31 20 26 26 20 73 71 6c 69 74 65 33 4f 70 65  =1 && sqlite3Ope
33752 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50  nTempDatabase(pP
33753 61 72 73 65 29 20 29 7b 0d 0a 20 20 20 20 72 65  arse) ){..    re
33754 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  turn;..  }....  
33755 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e  zLeft = sqlite3N
33756 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
33757 20 70 49 64 29 3b 0d 0a 20 20 69 66 28 20 21 7a   pId);..  if( !z
33758 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0d 0a  Left ) return;..
33759 20 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20    if( minusFlag 
3375a 29 7b 0d 0a 20 20 20 20 7a 52 69 67 68 74 20 3d  ){..    zRight =
3375b 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
3375c 64 62 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75  db, "-%T", pValu
3375d 65 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  e);..  }else{.. 
3375e 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69     zRight = sqli
3375f 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
33760 28 64 62 2c 20 70 56 61 6c 75 65 29 3b 0d 0a 20  (db, pValue);.. 
33761 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20   }....  assert( 
33762 70 49 64 32 20 29 3b 0d 0a 20 20 7a 44 62 20 3d  pId2 );..  zDb =
33763 20 70 49 64 32 2d 3e 6e 3e 30 20 3f 20 70 44 62   pId2->n>0 ? pDb
33764 2d 3e 7a 4e 61 6d 65 20 3a 20 30 3b 0d 0a 20 20  ->zName : 0;..  
33765 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
33766 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
33767 49 54 45 5f 50 52 41 47 4d 41 2c 20 7a 4c 65 66  ITE_PRAGMA, zLef
33768 74 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 20  t, zRight, zDb) 
33769 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 70 72 61  ){..    goto pra
3376a 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 20  gma_out;..  }.. 
3376b 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
3376c 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
3376d 5f 50 52 41 47 4d 41 53 29 20 26 26 20 21 64 65  _PRAGMAS) && !de
3376e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
3376f 54 5f 44 45 50 52 45 43 41 54 45 44 29 0d 0a 20  T_DEPRECATED).. 
33770 20 2f 2a 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d   /*..  **  PRAGM
33771 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66  A [database.]def
33772 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 0d  ault_cache_size.
33773 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
33774 61 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74  atabase.]default
33775 5f 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0d 0a 20  _cache_size=N.. 
33776 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 66 69   **..  ** The fi
33777 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
33778 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 65 72   the current per
33779 73 69 73 74 65 6e 74 20 73 65 74 74 69 6e 67 20  sistent setting 
3377a 66 6f 72 20 74 68 65 0d 0a 20 20 2a 2a 20 70 61  for the..  ** pa
3377b 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20  ge cache size.  
3377c 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
3377d 65 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  ed is the maximu
3377e 6d 20 6e 75 6d 62 65 72 20 6f 66 0d 0a 20 20 2a  m number of..  *
3377f 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70  * pages in the p
33780 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65 20  age cache.  The 
33781 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73  second form sets
33782 20 62 6f 74 68 20 74 68 65 20 63 75 72 72 65 6e   both the curren
33783 74 0d 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63  t..  ** page cac
33784 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61 6e  he size value an
33785 64 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  d the persistent
33786 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
33787 20 76 61 6c 75 65 0d 0a 20 20 2a 2a 20 73 74 6f   value..  ** sto
33788 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  red in the datab
33789 61 73 65 20 66 69 6c 65 2e 0d 0a 20 20 2a 2a 0d  ase file...  **.
3378a 0a 20 20 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73  .  ** Older vers
3378b 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77  ions of SQLite w
3378c 6f 75 6c 64 20 73 65 74 20 74 68 65 20 64 65 66  ould set the def
3378d 61 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65 20  ault cache size 
3378e 74 6f 20 61 0d 0a 20 20 2a 2a 20 6e 65 67 61 74  to a..  ** negat
3378f 69 76 65 20 6e 75 6d 62 65 72 20 74 6f 20 69 6e  ive number to in
33790 64 69 63 61 74 65 20 73 79 6e 63 68 72 6f 6e 6f  dicate synchrono
33791 75 73 3d 4f 46 46 2e 20 20 54 68 65 73 65 20 64  us=OFF.  These d
33792 61 79 73 2c 20 73 79 6e 63 68 72 6f 6e 6f 75 73  ays, synchronous
33793 0d 0a 20 20 2a 2a 20 69 73 20 61 6c 77 61 79 73  ..  ** is always
33794 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 72   on by default r
33795 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
33796 20 73 69 67 6e 20 6f 66 20 74 68 65 20 64 65 66   sign of the def
33797 61 75 6c 74 20 63 61 63 68 65 0d 0a 20 20 2a 2a  ault cache..  **
33798 20 73 69 7a 65 2e 20 20 42 75 74 20 63 6f 6e 74   size.  But cont
33799 69 6e 75 65 20 74 6f 20 74 61 6b 65 20 74 68 65  inue to take the
3379a 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20   absolute value 
3379b 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  of the default c
3379c 61 63 68 65 0d 0a 20 20 2a 2a 20 73 69 7a 65 20  ache..  ** size 
3379d 6f 66 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f  of historical co
3379e 6d 70 61 74 69 62 69 6c 69 74 79 2e 0d 0a 20 20  mpatibility...  
3379f 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
337a0 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
337a1 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69  default_cache_si
337a2 7a 65 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ze")==0 ){..    
337a3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
337a4 65 4f 70 4c 69 73 74 20 67 65 74 43 61 63 68 65  eOpList getCache
337a5 53 69 7a 65 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20  Size[] = {..    
337a6 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69    { OP_Transacti
337a7 6f 6e 2c 20 30 2c 20 30 2c 20 20 20 20 20 20 20  on, 0, 0,       
337a8 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20   0},            
337a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
337aa 30 20 2a 2f 0d 0a 20 20 20 20 20 20 7b 20 4f 50  0 */..      { OP
337ab 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30 2c  _ReadCookie,  0,
337ac 20 31 2c 20 20 20 20 20 20 20 20 42 54 52 45 45   1,        BTREE
337ad 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
337ae 49 5a 45 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0d 0a  IZE},  /* 1 */..
337af 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73        { OP_IfPos
337b0 2c 20 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20  ,       1, 7,   
337b1 20 20 20 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20       0},..      
337b2 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20  { OP_Integer,   
337b3 20 20 30 2c 20 32 2c 20 20 20 20 20 20 20 20 30    0, 2,        0
337b4 7d 2c 0d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53  },..      { OP_S
337b5 75 62 74 72 61 63 74 2c 20 20 20 20 31 2c 20 32  ubtract,    1, 2
337b6 2c 20 20 20 20 20 20 20 20 31 7d 2c 0d 0a 20 20  ,        1},..  
337b7 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20      { OP_IfPos, 
337b8 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20        1, 7,     
337b9 20 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20 7b 20     0},..      { 
337ba 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20  OP_Integer,     
337bb 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 1,        0},
337bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
337bd 20 20 20 20 20 20 20 20 20 2f 2a 20 36 20 2a 2f           /* 6 */
337be 0d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73  ..      { OP_Res
337bf 75 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20  ultRow,   1, 1, 
337c0 20 20 20 20 20 20 20 30 7d 2c 0d 0a 20 20 20 20         0},..    
337c1 7d 3b 0d 0a 20 20 20 20 69 6e 74 20 61 64 64 72  };..    int addr
337c2 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ;..    if( sqlit
337c3 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
337c4 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
337c5 6d 61 5f 6f 75 74 3b 0d 0a 20 20 20 20 73 71 6c  ma_out;..    sql
337c6 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
337c7 65 28 76 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20  e(v, iDb);..    
337c8 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0d 0a  if( !zRight ){..
337c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
337ca 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
337cb 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
337cc 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
337cd 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
337ce 4d 45 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22  ME, "cache_size"
337cf 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
337d0 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ;..      pParse-
337d1 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0d 0a 20 20 20  >nMem += 2;..   
337d2 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
337d3 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
337d4 2c 20 41 72 72 61 79 53 69 7a 65 28 67 65 74 43  , ArraySize(getC
337d5 61 63 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61  acheSize), getCa
337d6 63 68 65 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20  cheSize);..     
337d7 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
337d8 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44  geP1(v, addr, iD
337d9 62 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  b);..      sqlit
337da 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
337db 2c 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0d  , addr+1, iDb);.
337dc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
337dd 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
337de 64 72 2b 36 2c 20 53 51 4c 49 54 45 5f 44 45 46  dr+6, SQLITE_DEF
337df 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29  AULT_CACHE_SIZE)
337e0 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
337e1 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
337e2 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32 28  sqlite3AbsInt32(
337e3 73 71 6c 69 74 65 33 41 74 6f 69 28 7a 52 69 67  sqlite3Atoi(zRig
337e4 68 74 29 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  ht));..      sql
337e5 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
337e6 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
337e7 30 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20  0, iDb);..      
337e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
337e9 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
337ea 20 73 69 7a 65 2c 20 31 29 3b 0d 0a 20 20 20 20   size, 1);..    
337eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
337ec 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f  Op3(v, OP_SetCoo
337ed 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f  kie, iDb, BTREE_
337ee 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
337ef 5a 45 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 61  ZE, 1);..      a
337f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
337f1 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
337f2 2c 20 69 44 62 2c 20 30 29 20 29 3b 0d 0a 20 20  , iDb, 0) );..  
337f3 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
337f4 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
337f5 69 7a 65 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ize;..      sqli
337f6 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
337f7 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70  Size(pDb->pBt, p
337f8 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
337f9 68 65 5f 73 69 7a 65 29 3b 0d 0a 20 20 20 20 7d  he_size);..    }
337fa 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69  ..  }else..#endi
337fb 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
337fc 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20  T_PAGER_PRAGMAS 
337fd 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  && !SQLITE_OMIT_
337fe 44 45 50 52 45 43 41 54 45 44 20 2a 2f 0d 0a 0d  DEPRECATED */...
337ff 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
33800 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
33801 50 52 41 47 4d 41 53 29 0d 0a 20 20 2f 2a 0d 0a  PRAGMAS)..  /*..
33802 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
33803 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a  tabase.]page_siz
33804 65 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  e..  **  PRAGMA 
33805 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f  [database.]page_
33806 73 69 7a 65 3d 4e 0d 0a 20 20 2a 2a 0d 0a 20 20  size=N..  **..  
33807 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
33808 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
33809 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f  rrent setting fo
3380a 72 20 74 68 65 0d 0a 20 20 2a 2a 20 64 61 74 61  r the..  ** data
3380b 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69  base page size i
3380c 6e 20 62 79 74 65 73 2e 20 20 54 68 65 20 73 65  n bytes.  The se
3380d 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74  cond form sets t
3380e 68 65 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  he..  ** databas
3380f 65 20 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75  e page size valu
33810 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 63 61  e.  The value ca
33811 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66  n only be set if
33812 0d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  ..  ** the datab
33813 61 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ase has not yet 
33814 62 65 65 6e 20 63 72 65 61 74 65 64 2e 0d 0a 20  been created... 
33815 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74   */..  if( sqlit
33816 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
33817 22 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20  "page_size")==0 
33818 29 7b 0d 0a 20 20 20 20 42 74 72 65 65 20 2a 70  ){..    Btree *p
33819 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0d 0a  Bt = pDb->pBt;..
3381a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21      assert( pBt!
3381b 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 21  =0 );..    if( !
3381c 7a 52 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20  zRight ){..     
3381d 20 69 6e 74 20 73 69 7a 65 20 3d 20 41 4c 57 41   int size = ALWA
3381e 59 53 28 70 42 74 29 20 3f 20 73 71 6c 69 74 65  YS(pBt) ? sqlite
3381f 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
33820 65 28 70 42 74 29 20 3a 20 30 3b 0d 0a 20 20 20  e(pBt) : 0;..   
33821 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
33822 6e 74 28 70 50 61 72 73 65 2c 20 22 70 61 67 65  nt(pParse, "page
33823 5f 73 69 7a 65 22 2c 20 73 69 7a 65 29 3b 0d 0a  _size", size);..
33824 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
33825 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79 20    /* Malloc may 
33826 66 61 69 6c 20 77 68 65 6e 20 73 65 74 74 69 6e  fail when settin
33827 67 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c  g the page-size,
33828 20 61 73 20 74 68 65 72 65 20 69 73 20 61 6e 20   as there is an 
33829 69 6e 74 65 72 6e 61 6c 0d 0a 20 20 20 20 20 20  internal..      
3382a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 74  ** buffer that t
3382b 68 65 20 70 61 67 65 72 20 6d 6f 64 75 6c 65 20  he pager module 
3382c 72 65 73 69 7a 65 73 20 75 73 69 6e 67 20 73 71  resizes using sq
3382d 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
3382e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
3382f 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69    db->nextPagesi
33830 7a 65 20 3d 20 73 71 6c 69 74 65 33 41 74 6f 69  ze = sqlite3Atoi
33831 28 7a 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 20  (zRight);..     
33832 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   if( SQLITE_NOME
33833 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 53  M==sqlite3BtreeS
33834 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 2c 20  etPageSize(pBt, 
33835 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
33836 2c 2d 31 2c 30 29 20 29 7b 0d 0a 20 20 20 20 20  ,-1,0) ){..     
33837 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
33838 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  led = 1;..      
33839 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  }..    }..  }els
3383a 65 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  e....  /*..  ** 
3383b 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
3383c 65 2e 5d 73 65 63 75 72 65 5f 64 65 6c 65 74 65  e.]secure_delete
3383d 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  ..  **  PRAGMA [
3383e 64 61 74 61 62 61 73 65 2e 5d 73 65 63 75 72 65  database.]secure
3383f 5f 64 65 6c 65 74 65 3d 4f 4e 2f 4f 46 46 0d 0a  _delete=ON/OFF..
33840 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 66    **..  ** The f
33841 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
33842 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  s the current se
33843 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0d 0a 20  tting for the.. 
33844 20 2a 2a 20 73 65 63 75 72 65 5f 64 65 6c 65 74   ** secure_delet
33845 65 20 66 6c 61 67 2e 20 20 54 68 65 20 73 65 63  e flag.  The sec
33846 6f 6e 64 20 66 6f 72 6d 20 63 68 61 6e 67 65 73  ond form changes
33847 20 74 68 65 20 73 65 63 75 72 65 5f 64 65 6c 65   the secure_dele
33848 74 65 0d 0a 20 20 2a 2a 20 66 6c 61 67 20 73 65  te..  ** flag se
33849 74 74 69 6e 67 20 61 6e 64 20 72 65 70 6f 72 74  tting and report
3384a 73 20 74 68 65 6e 65 77 20 76 61 6c 75 65 2e 0d  s thenew value..
3384b 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  .  */..  if( sql
3384c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
3384d 74 2c 22 73 65 63 75 72 65 5f 64 65 6c 65 74 65  t,"secure_delete
3384e 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 42 74  ")==0 ){..    Bt
3384f 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e  ree *pBt = pDb->
33850 70 42 74 3b 0d 0a 20 20 20 20 69 6e 74 20 62 20  pBt;..    int b 
33851 3d 20 2d 31 3b 0d 0a 20 20 20 20 61 73 73 65 72  = -1;..    asser
33852 74 28 20 70 42 74 21 3d 30 20 29 3b 0d 0a 20 20  t( pBt!=0 );..  
33853 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0d    if( zRight ){.
33854 0a 20 20 20 20 20 20 62 20 3d 20 73 71 6c 69 74  .      b = sqlit
33855 65 33 47 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69  e3GetBoolean(zRi
33856 67 68 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ght);..    }..  
33857 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30    if( pId2->n==0
33858 20 26 26 20 62 3e 3d 30 20 29 7b 0d 0a 20 20 20   && b>=0 ){..   
33859 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 20 20     int ii;..    
3385a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64    for(ii=0; ii<d
3385b 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0d 0a  b->nDb; ii++){..
3385c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
3385d 74 72 65 65 53 65 63 75 72 65 44 65 6c 65 74 65  treeSecureDelete
3385e 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74  (db->aDb[ii].pBt
3385f 2c 20 62 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  , b);..      }..
33860 20 20 20 20 7d 0d 0a 20 20 20 20 62 20 3d 20 73      }..    b = s
33861 71 6c 69 74 65 33 42 74 72 65 65 53 65 63 75 72  qlite3BtreeSecur
33862 65 44 65 6c 65 74 65 28 70 42 74 2c 20 62 29 3b  eDelete(pBt, b);
33863 0d 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67  ..    returnSing
33864 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 73  leInt(pParse, "s
33865 65 63 75 72 65 5f 64 65 6c 65 74 65 22 2c 20 62  ecure_delete", b
33866 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 20  );..  }else.... 
33867 20 2f 2a 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d   /*..  **  PRAGM
33868 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61 78  A [database.]max
33869 5f 70 61 67 65 5f 63 6f 75 6e 74 0d 0a 20 20 2a  _page_count..  *
3386a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
3386b 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f  ase.]max_page_co
3386c 75 6e 74 3d 4e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  unt=N..  **..  *
3386d 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
3386e 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
3386f 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72  rent setting for
33870 20 74 68 65 0d 0a 20 20 2a 2a 20 6d 61 78 69 6d   the..  ** maxim
33871 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  um number of pag
33872 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
33873 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 0d 0a  se file.  The ..
33874 20 20 2a 2a 20 73 65 63 6f 6e 64 20 66 6f 72 6d    ** second form
33875 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 68 61   attempts to cha
33876 6e 67 65 20 74 68 69 73 20 73 65 74 74 69 6e 67  nge this setting
33877 2e 20 20 42 6f 74 68 0d 0a 20 20 2a 2a 20 66 6f  .  Both..  ** fo
33878 72 6d 73 20 72 65 74 75 72 6e 20 74 68 65 20 63  rms return the c
33879 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2e 0d  urrent setting..
3387a 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20  .  **..  ** The 
3387b 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f  absolute value o
3387c 66 20 4e 20 69 73 20 75 73 65 64 2e 20 20 54 68  f N is used.  Th
3387d 69 73 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74  is is undocument
3387e 65 64 20 61 6e 64 20 6d 69 67 68 74 0d 0a 20 20  ed and might..  
3387f 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54 68 65 20  ** change.  The 
33880 6f 6e 6c 79 20 70 75 72 70 6f 73 65 20 69 73 20  only purpose is 
33881 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 65 61  to provide an ea
33882 73 79 20 77 61 79 20 74 6f 20 74 65 73 74 0d 0a  sy way to test..
33883 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33    ** the sqlite3
33884 41 62 73 49 6e 74 33 32 28 29 20 66 75 6e 63 74  AbsInt32() funct
33885 69 6f 6e 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ion...  **..  **
33886 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
33887 73 65 2e 5d 70 61 67 65 5f 63 6f 75 6e 74 0d 0a  se.]page_count..
33888 20 20 2a 2a 0d 0a 20 20 2a 2a 20 52 65 74 75 72    **..  ** Retur
33889 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
3388a 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 70 65  pages in the spe
3388b 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65 2e  cified database.
3388c 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71  ..  */..  if( sq
3388d 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
3388e 66 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29  ft,"page_count")
3388f 3d 3d 30 0d 0a 20 20 20 7c 7c 20 73 71 6c 69 74  ==0..   || sqlit
33890 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
33891 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22  "max_page_count"
33892 29 3d 3d 30 0d 0a 20 20 29 7b 0d 0a 20 20 20 20  )==0..  ){..    
33893 69 6e 74 20 69 52 65 67 3b 0d 0a 20 20 20 20 69  int iReg;..    i
33894 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
33895 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
33896 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d  oto pragma_out;.
33897 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
33898 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
33899 72 73 65 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20  rse, iDb);..    
3389a 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iReg = ++pParse-
3389b 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 69 66 28 20  >nMem;..    if( 
3389c 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 7a  sqlite3Tolower(z
3389d 4c 65 66 74 5b 30 5d 29 3d 3d 27 70 27 20 29 7b  Left[0])=='p' ){
3389e 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
3389f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
338a0 50 61 67 65 63 6f 75 6e 74 2c 20 69 44 62 2c 20  Pagecount, iDb, 
338a1 69 52 65 67 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  iReg);..    }els
338a2 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e{..      sqlite
338a3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
338a4 50 5f 4d 61 78 50 67 63 6e 74 2c 20 69 44 62 2c  P_MaxPgcnt, iDb,
338a5 20 69 52 65 67 2c 20 0d 0a 20 20 20 20 20 20 20   iReg, ..       
338a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
338a7 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32   sqlite3AbsInt32
338a8 28 73 71 6c 69 74 65 33 41 74 6f 69 28 7a 52 69  (sqlite3Atoi(zRi
338a9 67 68 74 29 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a  ght)));..    }..
338aa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
338ab 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
338ac 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b  ltRow, iReg, 1);
338ad 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
338ae 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
338af 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
338b0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
338b1 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
338b2 2c 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f  , zLeft, SQLITE_
338b3 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20 7d  TRANSIENT);..  }
338b4 65 6c 73 65 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20  else....  /*..  
338b5 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
338b6 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f  base.]locking_mo
338b7 64 65 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  de..  **  PRAGMA
338b8 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
338b9 69 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d  ing_mode = (norm
338ba 61 6c 7c 65 78 63 6c 75 73 69 76 65 29 0d 0a 20  al|exclusive).. 
338bb 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74   */..  if( sqlit
338bc 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
338bd 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d  "locking_mode")=
338be 3d 30 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74  =0 ){..    const
338bf 20 63 68 61 72 20 2a 7a 52 65 74 20 3d 20 22 6e   char *zRet = "n
338c0 6f 72 6d 61 6c 22 3b 0d 0a 20 20 20 20 69 6e 74  ormal";..    int
338c1 20 65 4d 6f 64 65 20 3d 20 67 65 74 4c 6f 63 6b   eMode = getLock
338c2 69 6e 67 4d 6f 64 65 28 7a 52 69 67 68 74 29 3b  ingMode(zRight);
338c3 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 49 64 32  ....    if( pId2
338c4 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d  ->n==0 && eMode=
338c5 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
338c6 44 45 5f 51 55 45 52 59 20 29 7b 0d 0a 20 20 20  DE_QUERY ){..   
338c7 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52     /* Simple "PR
338c8 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  AGMA locking_mod
338c9 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  e;" statement. T
338ca 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66  his is a query f
338cb 6f 72 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  or..      ** the
338cc 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74   current default
338cd 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28 77   locking mode (w
338ce 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66  hich may be diff
338cf 65 72 65 6e 74 20 74 6f 0d 0a 20 20 20 20 20 20  erent to..      
338d0 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  ** the locking-m
338d1 6f 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  ode of the main 
338d2 64 61 74 61 62 61 73 65 29 2e 0d 0a 20 20 20 20  database)...    
338d3 20 20 2a 2f 0d 0a 20 20 20 20 20 20 65 4d 6f 64    */..      eMod
338d4 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  e = db->dfltLock
338d5 4d 6f 64 65 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  Mode;..    }else
338d6 7b 0d 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  {..      Pager *
338d7 70 50 61 67 65 72 3b 0d 0a 20 20 20 20 20 20 69  pPager;..      i
338d8 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b  f( pId2->n==0 ){
338d9 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  ..        /* Thi
338da 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  s indicates that
338db 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d   no database nam
338dc 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20  e was specified 
338dd 61 73 20 70 61 72 74 0d 0a 20 20 20 20 20 20 20  as part..       
338de 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d   ** of the PRAGM
338df 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68  A command. In th
338e0 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b  is case the lock
338e1 69 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65  ing-mode must be
338e2 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74  ..        ** set
338e3 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64   on all attached
338e4 20 64 61 74 61 62 61 73 65 73 2c 20 61 73 20 77   databases, as w
338e5 65 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20  ell as the main 
338e6 64 62 20 66 69 6c 65 2e 0d 0a 20 20 20 20 20 20  db file...      
338e7 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a    **..        **
338e8 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69 74   Also, the sqlit
338e9 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20  e3.dfltLockMode 
338ea 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
338eb 73 6f 20 74 68 61 74 0d 0a 20 20 20 20 20 20 20  so that..       
338ec 20 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65   ** any subseque
338ed 6e 74 6c 79 20 61 74 74 61 63 68 65 64 20 64 61  ntly attached da
338ee 74 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65  tabases also use
338ef 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0d 0a   the specified..
338f0 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 69          ** locki
338f1 6e 67 20 6d 6f 64 65 2e 0d 0a 20 20 20 20 20 20  ng mode...      
338f2 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e    */..        in
338f3 74 20 69 69 3b 0d 0a 20 20 20 20 20 20 20 20 61  t ii;..        a
338f4 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e  ssert(pDb==&db->
338f5 61 44 62 5b 30 5d 29 3b 0d 0a 20 20 20 20 20 20  aDb[0]);..      
338f6 20 20 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64    for(ii=2; ii<d
338f7 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0d 0a  b->nDb; ii++){..
338f8 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
338f9 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
338fa 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d  ager(db->aDb[ii]
338fb 2e 70 42 74 29 3b 0d 0a 20 20 20 20 20 20 20 20  .pBt);..        
338fc 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f    sqlite3PagerLo
338fd 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72  ckingMode(pPager
338fe 2c 20 65 4d 6f 64 65 29 3b 0d 0a 20 20 20 20 20  , eMode);..     
338ff 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 64 62     }..        db
33900 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d  ->dfltLockMode =
33901 20 28 75 38 29 65 4d 6f 64 65 3b 0d 0a 20 20 20   (u8)eMode;..   
33902 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 50 61 67     }..      pPag
33903 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
33904 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
33905 3b 0d 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  ;..      eMode =
33906 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
33907 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
33908 20 65 4d 6f 64 65 29 3b 0d 0a 20 20 20 20 7d 0d   eMode);..    }.
33909 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28 65 4d  ...    assert(eM
3390a 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
3390b 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65  NGMODE_NORMAL||e
3390c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
3390d 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
3390e 45 29 3b 0d 0a 20 20 20 20 69 66 28 20 65 4d 6f  E);..    if( eMo
3390f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
33910 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
33911 29 7b 0d 0a 20 20 20 20 20 20 7a 52 65 74 20 3d  ){..      zRet =
33912 20 22 65 78 63 6c 75 73 69 76 65 22 3b 0d 0a 20   "exclusive";.. 
33913 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
33914 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
33915 76 2c 20 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69  v, 1);..    sqli
33916 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
33917 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
33918 4e 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d  NAME, "locking_m
33919 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ode", SQLITE_STA
3391a 54 49 43 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  TIC);..    sqlit
3391b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
3391c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
3391d 2c 20 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0d 0a  , 0, zRet, 0);..
3391e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3391f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
33920 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0d 0a 20  ltRow, 1, 1);.. 
33921 20 7d 65 6c 73 65 0d 0a 0d 0a 20 20 2f 2a 0d 0a   }else....  /*..
33922 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
33923 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
33924 6d 6f 64 65 0d 0a 20 20 2a 2a 20 20 50 52 41 47  mode..  **  PRAG
33925 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
33926 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 0d 0a 20 20  urnal_mode =..  
33927 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
33928 20 20 20 20 20 20 20 20 28 64 65 6c 65 74 65 7c          (delete|
33929 70 65 72 73 69 73 74 7c 6f 66 66 7c 74 72 75 6e  persist|off|trun
3392a 63 61 74 65 7c 6d 65 6d 6f 72 79 7c 77 61 6c 7c  cate|memory|wal|
3392b 6f 66 66 29 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  off)..  */..  if
3392c 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
3392d 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f  (zLeft,"journal_
3392e 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0d 0a 20 20  mode")==0 ){..  
3392f 20 20 69 6e 74 20 65 4d 6f 64 65 3b 20 20 20 20    int eMode;    
33930 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
33931 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
33932 4f 44 45 5f 58 58 58 20 73 79 6d 62 6f 6c 73 20  ODE_XXX symbols 
33933 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 69 3b 20  */..    int ii; 
33934 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
33935 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d 0a  p counter */....
33936 20 20 20 20 2f 2a 20 46 6f 72 63 65 20 74 68 65      /* Force the
33937 20 73 63 68 65 6d 61 20 74 6f 20 62 65 20 6c 6f   schema to be lo
33938 61 64 65 64 20 6f 6e 20 61 6c 6c 20 64 61 74 61  aded on all data
33939 62 61 73 65 73 2e 20 20 54 68 69 73 20 63 61 75  bases.  This cau
3393a 73 65 73 20 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20  ses all..    ** 
3393b 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 74  database files t
3393c 6f 20 62 65 20 6f 70 65 6e 65 64 20 61 6e 64 20  o be opened and 
3393d 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  the journal_mode
3393e 73 20 73 65 74 2e 20 20 54 68 69 73 20 69 73 0d  s set.  This is.
3393f 0a 20 20 20 20 2a 2a 20 6e 65 63 65 73 73 61 72  .    ** necessar
33940 79 20 62 65 63 61 75 73 65 20 73 75 62 73 65 71  y because subseq
33941 75 65 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 20  uent processing 
33942 6d 75 73 74 20 6b 6e 6f 77 20 69 66 20 74 68 65  must know if the
33943 20 64 61 74 61 62 61 73 65 73 0d 0a 20 20 20 20   databases..    
33944 2a 2a 20 61 72 65 20 69 6e 20 57 41 4c 20 6d 6f  ** are in WAL mo
33945 64 65 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  de. */..    if( 
33946 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
33947 61 28 70 50 61 72 73 65 29 20 29 7b 0d 0a 20 20  a(pParse) ){..  
33948 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
33949 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  out;..    }.... 
3394a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
3394b 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0d  tNumCols(v, 1);.
3394c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3394d 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
3394e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
3394f 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 2c 20 53  journal_mode", S
33950 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
33951 0d 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  ..    if( zRight
33952 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  ==0 ){..      /*
33953 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
33954 22 3d 4d 4f 44 45 22 20 70 61 72 74 20 6f 66 20  "=MODE" part of 
33955 74 68 65 20 70 72 61 67 6d 61 2c 20 64 6f 20 61  the pragma, do a
33956 20 71 75 65 72 79 20 66 6f 72 20 74 68 65 0d 0a   query for the..
33957 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74        ** current
33958 20 6d 6f 64 65 20 2a 2f 0d 0a 20 20 20 20 20 20   mode */..      
33959 65 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f  eMode = PAGER_JO
3395a 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 3b  URNALMODE_QUERY;
3395b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
3395c 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
3395d 7a 4d 6f 64 65 3b 0d 0a 20 20 20 20 20 20 69 6e  zMode;..      in
3395e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
3395f 6c 65 6e 33 30 28 7a 52 69 67 68 74 29 3b 0d 0a  len30(zRight);..
33960 20 20 20 20 20 20 66 6f 72 28 65 4d 6f 64 65 3d        for(eMode=
33961 30 3b 20 28 7a 4d 6f 64 65 20 3d 20 73 71 6c 69  0; (zMode = sqli
33962 74 65 33 4a 6f 75 72 6e 61 6c 4d 6f 64 65 6e 61  te3JournalModena
33963 6d 65 28 65 4d 6f 64 65 29 29 21 3d 30 3b 20 65  me(eMode))!=0; e
33964 4d 6f 64 65 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  Mode++){..      
33965 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
33966 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 7a 4d  NICmp(zRight, zM
33967 6f 64 65 2c 20 6e 29 3d 3d 30 20 29 20 62 72 65  ode, n)==0 ) bre
33968 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
33969 20 20 20 20 69 66 28 20 21 7a 4d 6f 64 65 20 29      if( !zMode )
3396a 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  {..        /* If
3396b 20 74 68 65 20 22 3d 4d 4f 44 45 22 20 70 61 72   the "=MODE" par
3396c 74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  t does not match
3396d 20 61 6e 79 20 6b 6e 6f 77 6e 20 6a 6f 75 72 6e   any known journ
3396e 61 6c 20 6d 6f 64 65 2c 0d 0a 20 20 20 20 20 20  al mode,..      
3396f 20 20 2a 2a 20 74 68 65 6e 20 64 6f 20 61 20 71    ** then do a q
33970 75 65 72 79 20 2a 2f 0d 0a 20 20 20 20 20 20 20  uery */..       
33971 20 65 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a   eMode = PAGER_J
33972 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59  OURNALMODE_QUERY
33973 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
33974 7d 0d 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65  }..    if( eMode
33975 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
33976 4f 44 45 5f 51 55 45 52 59 20 26 26 20 70 49 64  ODE_QUERY && pId
33977 32 2d 3e 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  2->n==0 ){..    
33978 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 22 50 52    /* Convert "PR
33979 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
3397a 65 22 20 69 6e 74 6f 20 22 50 52 41 47 4d 41 20  e" into "PRAGMA 
3397b 6d 61 69 6e 2e 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  main.journal_mod
3397c 65 22 20 2a 2f 0d 0a 20 20 20 20 20 20 69 44 62  e" */..      iDb
3397d 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 49 64   = 0;..      pId
3397e 32 2d 3e 6e 20 3d 20 31 3b 0d 0a 20 20 20 20 7d  2->n = 1;..    }
3397f 0d 0a 20 20 20 20 66 6f 72 28 69 69 3d 64 62 2d  ..    for(ii=db-
33980 3e 6e 44 62 2d 31 3b 20 69 69 3e 3d 30 3b 20 69  >nDb-1; ii>=0; i
33981 69 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  i--){..      if(
33982 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74   db->aDb[ii].pBt
33983 20 26 26 20 28 69 69 3d 3d 69 44 62 20 7c 7c 20   && (ii==iDb || 
33984 70 49 64 32 2d 3e 6e 3d 3d 30 29 20 29 7b 0d 0a  pId2->n==0) ){..
33985 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33986 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
33987 69 69 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  ii);..        sq
33988 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
33989 76 2c 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f 64  v, OP_JournalMod
3398a 65 2c 20 69 69 2c 20 31 2c 20 65 4d 6f 64 65 29  e, ii, 1, eMode)
3398b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
3398c 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  }..    sqlite3Vd
3398d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
3398e 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b  esultRow, 1, 1);
3398f 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 20 20 2f  ..  }else....  /
33990 2a 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  *..  **  PRAGMA 
33991 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e  [database.]journ
33992 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 0d 0a 20  al_size_limit.. 
33993 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
33994 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73  abase.]journal_s
33995 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0d 0a 20 20 2a  ize_limit=N..  *
33996 2a 0d 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73  *..  ** Get or s
33997 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69  et the size limi
33998 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  t on rollback jo
33999 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0d 0a 20 20  urnal files...  
3399a 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
3399b 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
3399c 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
3399d 69 74 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  it")==0 ){..    
3399e 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
3399f 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
339a0 72 28 70 44 62 2d 3e 70 42 74 29 3b 0d 0a 20 20  r(pDb->pBt);..  
339a1 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 2d    i64 iLimit = -
339a2 32 3b 0d 0a 20 20 20 20 69 66 28 20 7a 52 69 67  2;..    if( zRig
339a3 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  ht ){..      sql
339a4 69 74 65 33 41 74 6f 69 36 34 28 7a 52 69 67 68  ite3Atoi64(zRigh
339a5 74 2c 20 26 69 4c 69 6d 69 74 2c 20 31 30 30 30  t, &iLimit, 1000
339a6 30 30 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  000, SQLITE_UTF8
339a7 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 69 4c  );..      if( iL
339a8 69 6d 69 74 3c 2d 31 20 29 20 69 4c 69 6d 69 74  imit<-1 ) iLimit
339a9 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20   = -1;..    }.. 
339aa 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71 6c 69     iLimit = sqli
339ab 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53  te3PagerJournalS
339ac 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65 72 2c  izeLimit(pPager,
339ad 20 69 4c 69 6d 69 74 29 3b 0d 0a 20 20 20 20 72   iLimit);..    r
339ae 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
339af 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f  Parse, "journal_
339b0 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c 69  size_limit", iLi
339b1 6d 69 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a  mit);..  }else..
339b2 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
339b3 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
339b4 41 47 4d 41 53 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a  AGMAS */....  /*
339b5 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  ..  **  PRAGMA [
339b6 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
339b7 61 63 75 75 6d 0d 0a 20 20 2a 2a 20 20 50 52 41  acuum..  **  PRA
339b8 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61  GMA [database.]a
339b9 75 74 6f 5f 76 61 63 75 75 6d 3d 4e 0d 0a 20 20  uto_vacuum=N..  
339ba 2a 2a 0d 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20  **..  ** Get or 
339bb 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
339bc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27 61   the database 'a
339bd 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72 61  uto-vacuum' para
339be 6d 65 74 65 72 2e 0d 0a 20 20 2a 2a 20 54 68 65  meter...  ** The
339bf 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66   value is one of
339c0 3a 20 20 30 20 4e 4f 4e 45 20 31 20 46 55 4c 4c  :  0 NONE 1 FULL
339c1 20 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c 0d 0a   2 INCREMENTAL..
339c2 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51    */..#ifndef SQ
339c3 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
339c4 43 55 55 4d 0d 0a 20 20 69 66 28 20 73 71 6c 69  CUUM..  if( sqli
339c5 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
339c6 2c 22 61 75 74 6f 5f 76 61 63 75 75 6d 22 29 3d  ,"auto_vacuum")=
339c7 3d 30 20 29 7b 0d 0a 20 20 20 20 42 74 72 65 65  =0 ){..    Btree
339c8 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74   *pBt = pDb->pBt
339c9 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
339ca 42 74 21 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66  Bt!=0 );..    if
339cb 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
339cc 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0d 0a  ema(pParse) ){..
339cd 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d        goto pragm
339ce 61 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  a_out;..    }.. 
339cf 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
339d0 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74  {..      int aut
339d1 6f 5f 76 61 63 75 75 6d 3b 0d 0a 20 20 20 20 20  o_vacuum;..     
339d2 20 69 66 28 20 41 4c 57 41 59 53 28 70 42 74 29   if( ALWAYS(pBt)
339d3 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 61 75   ){..         au
339d4 74 6f 5f 76 61 63 75 75 6d 20 3d 20 73 71 6c 69  to_vacuum = sqli
339d5 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
339d6 61 63 75 75 6d 28 70 42 74 29 3b 0d 0a 20 20 20  acuum(pBt);..   
339d7 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
339d8 20 20 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20      auto_vacuum 
339d9 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  = SQLITE_DEFAULT
339da 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0d 0a 20 20  _AUTOVACUUM;..  
339db 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 65 74      }..      ret
339dc 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
339dd 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75 75  rse, "auto_vacuu
339de 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d 29  m", auto_vacuum)
339df 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
339e0 20 20 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d       int eAuto =
339e1 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a   getAutoVacuum(z
339e2 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 61  Right);..      a
339e3 73 73 65 72 74 28 20 65 41 75 74 6f 3e 3d 30 20  ssert( eAuto>=0 
339e4 26 26 20 65 41 75 74 6f 3c 3d 32 20 29 3b 0d 0a  && eAuto<=2 );..
339e5 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 41 75        db->nextAu
339e6 74 6f 76 61 63 20 3d 20 28 75 38 29 65 41 75 74  tovac = (u8)eAut
339e7 6f 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 41 4c  o;..      if( AL
339e8 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29 20 29  WAYS(eAuto>=0) )
339e9 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61  {..        /* Ca
339ea 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75 6d  ll SetAutoVacuum
339eb 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69 61  () to set initia
339ec 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e 61  lize the interna
339ed 6c 20 61 75 74 6f 20 61 6e 64 0d 0a 20 20 20 20  l auto and..    
339ee 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75      ** incr-vacu
339ef 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69  um flags. This i
339f0 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63 61  s required in ca
339f1 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  se this connecti
339f2 6f 6e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  on..        ** c
339f3 72 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62  reates the datab
339f4 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ase file. It is 
339f5 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69  important that i
339f6 74 20 69 73 20 63 72 65 61 74 65 64 0d 0a 20 20  t is created..  
339f7 20 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61        ** as an a
339f8 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62  uto-vacuum capab
339f9 6c 65 20 64 62 2e 0d 0a 20 20 20 20 20 20 20 20  le db...        
339fa 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  */..        int 
339fb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
339fc 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70  eSetAutoVacuum(p
339fd 42 74 2c 20 65 41 75 74 6f 29 3b 0d 0a 20 20 20  Bt, eAuto);..   
339fe 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
339ff 49 54 45 5f 4f 4b 20 26 26 20 28 65 41 75 74 6f  ITE_OK && (eAuto
33a00 3d 3d 31 20 7c 7c 20 65 41 75 74 6f 3d 3d 32 29  ==1 || eAuto==2)
33a01 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f   ){..          /
33a02 2a 20 57 68 65 6e 20 73 65 74 74 69 6e 67 20 74  * When setting t
33a03 68 65 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 6d  he auto_vacuum m
33a04 6f 64 65 20 74 6f 20 65 69 74 68 65 72 20 22 66  ode to either "f
33a05 75 6c 6c 22 20 6f 72 20 0d 0a 20 20 20 20 20 20  ull" or ..      
33a06 20 20 20 20 2a 2a 20 22 69 6e 63 72 65 6d 65 6e      ** "incremen
33a07 74 61 6c 22 2c 20 77 72 69 74 65 20 74 68 65 20  tal", write the 
33a08 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 36 5d  value of meta[6]
33a09 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
33a0a 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66  ..          ** f
33a0b 69 6c 65 2e 20 42 65 66 6f 72 65 20 77 72 69 74  ile. Before writ
33a0c 69 6e 67 20 74 6f 20 6d 65 74 61 5b 36 5d 2c 20  ing to meta[6], 
33a0d 63 68 65 63 6b 20 74 68 61 74 20 6d 65 74 61 5b  check that meta[
33a0e 33 5d 20 69 6e 64 69 63 61 74 65 73 0d 0a 20 20  3] indicates..  
33a0f 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
33a10 74 68 69 73 20 72 65 61 6c 6c 79 20 69 73 20 61  this really is a
33a11 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61  n auto-vacuum ca
33a12 70 61 62 6c 65 20 64 61 74 61 62 61 73 65 2e 0d  pable database..
33a13 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  .          */.. 
33a14 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
33a15 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
33a16 20 73 65 74 4d 65 74 61 36 5b 5d 20 3d 20 7b 0d   setMeta6[] = {.
33a17 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
33a18 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20  P_Transaction,  
33a19 20 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20    0,         1, 
33a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a1b 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0d 0a  0},    /* 0 */..
33a1c 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
33a1d 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20 20  _ReadCookie,    
33a1e 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20   0,         1,  
33a1f 20 20 20 20 20 20 20 42 54 52 45 45 5f 4c 41 52         BTREE_LAR
33a20 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 7d 2c  GEST_ROOT_PAGE},
33a21 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ..            { 
33a22 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20 20 20  OP_If,          
33a23 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 30 2c     1,         0,
33a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a25 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0d   0},    /* 2 */.
33a26 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
33a27 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 20  P_Halt,         
33a28 20 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f    SQLITE_OK, OE_
33a29 41 62 6f 72 74 2c 20 20 20 20 20 20 20 20 20 20  Abort,          
33a2a 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0d 0a  0},    /* 3 */..
33a2b 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
33a2c 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20  _Integer,       
33a2d 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20   0,         1,  
33a2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
33a2f 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0d 0a 20  },    /* 4 */.. 
33a30 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
33a31 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20  SetCookie,      
33a32 30 2c 20 20 20 20 20 20 20 20 20 42 54 52 45 45  0,         BTREE
33a33 5f 49 4e 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d  _INCR_VACUUM, 1}
33a34 2c 20 20 20 20 2f 2a 20 35 20 2a 2f 0d 0a 20 20  ,    /* 5 */..  
33a35 20 20 20 20 20 20 20 20 7d 3b 0d 0a 20 20 20 20          };..    
33a36 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b        int iAddr;
33a37 0d 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64  ..          iAdd
33a38 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
33a39 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
33a3a 79 53 69 7a 65 28 73 65 74 4d 65 74 61 36 29 2c  ySize(setMeta6),
33a3b 20 73 65 74 4d 65 74 61 36 29 3b 0d 0a 20 20 20   setMeta6);..   
33a3c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33a3d 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41  beChangeP1(v, iA
33a3e 64 64 72 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20  ddr, iDb);..    
33a3f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33a40 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
33a41 64 72 2b 31 2c 20 69 44 62 29 3b 0d 0a 20 20 20  dr+1, iDb);..   
33a42 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33a43 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 69 41  beChangeP2(v, iA
33a44 64 64 72 2b 32 2c 20 69 41 64 64 72 2b 34 29 3b  ddr+2, iAddr+4);
33a45 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
33a46 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
33a47 76 2c 20 69 41 64 64 72 2b 34 2c 20 65 41 75 74  v, iAddr+4, eAut
33a48 6f 2d 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  o-1);..         
33a49 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
33a4a 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 35 2c  geP1(v, iAddr+5,
33a4b 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 20 20   iDb);..        
33a4c 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
33a4d 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0d  sBtree(v, iDb);.
33a4e 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
33a4f 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65    }..    }..  }e
33a50 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  lse..#endif.... 
33a51 20 2f 2a 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d   /*..  **  PRAGM
33a52 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 6e 63  A [database.]inc
33a53 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28  remental_vacuum(
33a54 4e 29 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 44  N)..  **..  ** D
33a55 6f 20 4e 20 73 74 65 70 73 20 6f 66 20 69 6e 63  o N steps of inc
33a56 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 69  remental vacuumi
33a57 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ng on a database
33a58 2e 0d 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66  ...  */..#ifndef
33a59 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
33a5a 4f 56 41 43 55 55 4d 0d 0a 20 20 69 66 28 20 73  OVACUUM..  if( s
33a5b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
33a5c 65 66 74 2c 22 69 6e 63 72 65 6d 65 6e 74 61 6c  eft,"incremental
33a5d 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0d  _vacuum")==0 ){.
33a5e 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c  .    int iLimit,
33a5f 20 61 64 64 72 3b 0d 0a 20 20 20 20 69 66 28 20   addr;..    if( 
33a60 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
33a61 61 28 70 50 61 72 73 65 29 20 29 7b 0d 0a 20 20  a(pParse) ){..  
33a62 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
33a63 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  out;..    }..   
33a64 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 7c   if( zRight==0 |
33a65 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49 6e 74  | !sqlite3GetInt
33a66 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d  32(zRight, &iLim
33a67 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30  it) || iLimit<=0
33a68 20 29 7b 0d 0a 20 20 20 20 20 20 69 4c 69 6d 69   ){..      iLimi
33a69 74 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0d  t = 0x7fffffff;.
33a6a 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
33a6b 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
33a6c 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
33a6d 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20 73 71 6c  , iDb);..    sql
33a6e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
33a6f 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c  , OP_Integer, iL
33a70 69 6d 69 74 2c 20 31 29 3b 0d 0a 20 20 20 20 61  imit, 1);..    a
33a71 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
33a72 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e  eAddOp1(v, OP_In
33a73 63 72 56 61 63 75 75 6d 2c 20 69 44 62 29 3b 0d  crVacuum, iDb);.
33a74 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
33a75 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73  AddOp1(v, OP_Res
33a76 75 6c 74 52 6f 77 2c 20 31 29 3b 0d 0a 20 20 20  ultRow, 1);..   
33a77 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33a78 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
33a79 20 31 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 73 71   1, -1);..    sq
33a7a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33a7b 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 2c 20  v, OP_IfPos, 1, 
33a7c 61 64 64 72 29 3b 0d 0a 20 20 20 20 73 71 6c 69  addr);..    sqli
33a7d 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
33a7e 76 2c 20 61 64 64 72 29 3b 0d 0a 20 20 7d 65 6c  v, addr);..  }el
33a7f 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  se..#endif....#i
33a80 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33a81 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0d  T_PAGER_PRAGMAS.
33a82 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 20 50 52 41  .  /*..  **  PRA
33a83 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63  GMA [database.]c
33a84 61 63 68 65 5f 73 69 7a 65 0d 0a 20 20 2a 2a 20  ache_size..  ** 
33a85 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
33a86 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0d  e.]cache_size=N.
33a87 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20  .  **..  ** The 
33a88 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
33a89 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ts the current l
33a8a 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66 6f 72  ocal setting for
33a8b 20 74 68 65 0d 0a 20 20 2a 2a 20 70 61 67 65 20   the..  ** page 
33a8c 63 61 63 68 65 20 73 69 7a 65 2e 20 54 68 65 20  cache size. The 
33a8d 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73  second form sets
33a8e 20 74 68 65 20 6c 6f 63 61 6c 0d 0a 20 20 2a 2a   the local..  **
33a8f 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
33a90 20 76 61 6c 75 65 2e 20 20 49 66 20 4e 20 69 73   value.  If N is
33a91 20 70 6f 73 69 74 69 76 65 20 74 68 65 6e 20 74   positive then t
33a92 68 61 74 20 69 73 20 74 68 65 0d 0a 20 20 2a 2a  hat is the..  **
33a93 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
33a94 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 20 20   in the cache.  
33a95 49 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76 65  If N is negative
33a96 2c 20 74 68 65 6e 20 74 68 65 0d 0a 20 20 2a 2a  , then the..  **
33a97 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
33a98 20 69 73 20 61 64 6a 75 73 74 65 64 20 73 6f 20   is adjusted so 
33a99 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 75  that the cache u
33a9a 73 65 73 20 2d 4e 20 6b 69 62 69 62 79 74 65 73  ses -N kibibytes
33a9b 0d 0a 20 20 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  ..  ** of memory
33a9c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73  ...  */..  if( s
33a9d 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
33a9e 65 66 74 2c 22 63 61 63 68 65 5f 73 69 7a 65 22  eft,"cache_size"
33a9f 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66 28  )==0 ){..    if(
33aa0 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
33aa1 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
33aa2 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20  o pragma_out;.. 
33aa3 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
33aa4 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c  e3SchemaMutexHel
33aa5 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b  d(db, iDb, 0) );
33aa6 0d 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ..    if( !zRigh
33aa7 74 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  t ){..      retu
33aa8 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
33aa9 73 65 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22  se, "cache_size"
33aaa 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
33aab 63 61 63 68 65 5f 73 69 7a 65 29 3b 0d 0a 20 20  cache_size);..  
33aac 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
33aad 69 6e 74 20 73 69 7a 65 20 3d 20 73 71 6c 69 74  int size = sqlit
33aae 65 33 41 74 6f 69 28 7a 52 69 67 68 74 29 3b 0d  e3Atoi(zRight);.
33aaf 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68  .      pDb->pSch
33ab0 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20  ema->cache_size 
33ab1 3d 20 73 69 7a 65 3b 0d 0a 20 20 20 20 20 20 73  = size;..      s
33ab2 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
33ab3 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74  cheSize(pDb->pBt
33ab4 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
33ab5 63 61 63 68 65 5f 73 69 7a 65 29 3b 0d 0a 20 20  cache_size);..  
33ab6 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a    }..  }else....
33ab7 20 20 2f 2a 0d 0a 20 20 2a 2a 20 20 20 50 52 41    /*..  **   PRA
33ab8 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 0d 0a  GMA temp_store..
33ab9 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65    **   PRAGMA te
33aba 6d 70 5f 73 74 6f 72 65 20 3d 20 22 64 65 66 61  mp_store = "defa
33abb 75 6c 74 22 7c 22 6d 65 6d 6f 72 79 22 7c 22 66  ult"|"memory"|"f
33abc 69 6c 65 22 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ile"..  **..  **
33abd 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74   Return or set t
33abe 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f  he local value o
33abf 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72 65  f the temp_store
33ac0 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67   flag.  Changing
33ac1 0d 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c  ..  ** the local
33ac2 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20   value does not 
33ac3 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20  make changes to 
33ac4 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e  the disk file an
33ac5 64 20 74 68 65 20 64 65 66 61 75 6c 74 0d 0a 20  d the default.. 
33ac6 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62   ** value will b
33ac7 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
33ac8 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
33ac9 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e  abase is opened.
33aca 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f 74  ..  **..  ** Not
33acb 65 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  e that it is pos
33acc 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69  sible for the li
33acd 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69  brary compile-ti
33ace 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0d 0a 20  me options to.. 
33acf 20 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69   ** override thi
33ad0 73 20 73 65 74 74 69 6e 67 0d 0a 20 20 2a 2f 0d  s setting..  */.
33ad1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
33ad2 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65  rICmp(zLeft, "te
33ad3 6d 70 5f 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b  mp_store")==0 ){
33ad4 0d 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ..    if( !zRigh
33ad5 74 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  t ){..      retu
33ad6 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
33ad7 73 65 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 22  se, "temp_store"
33ad8 2c 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  , db->temp_store
33ad9 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
33ada 20 20 20 20 20 20 63 68 61 6e 67 65 54 65 6d 70        changeTemp
33adb 53 74 6f 72 61 67 65 28 70 50 61 72 73 65 2c 20  Storage(pParse, 
33adc 7a 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 7d 0d  zRight);..    }.
33add 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 20 20 2f 2a  .  }else....  /*
33ade 0d 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  ..  **   PRAGMA 
33adf 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
33ae0 74 6f 72 79 0d 0a 20 20 2a 2a 20 20 20 50 52 41  tory..  **   PRA
33ae1 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
33ae2 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22 64  irectory = ""|"d
33ae3 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0d 0a  irectory_name"..
33ae4 20 20 2a 2a 0d 0a 20 20 2a 2a 20 52 65 74 75 72    **..  ** Retur
33ae5 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63  n or set the loc
33ae6 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  al value of the 
33ae7 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
33ae8 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e  tory flag.  Chan
33ae9 67 69 6e 67 0d 0a 20 20 2a 2a 20 74 68 65 20 76  ging..  ** the v
33aea 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
33aeb 69 66 69 63 20 64 69 72 65 63 74 6f 72 79 20 74  ific directory t
33aec 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 65  o be used for te
33aed 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0d 0a  mporary files...
33aee 20 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20    ** Setting to 
33aef 61 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65  a null string re
33af0 76 65 72 74 73 20 74 6f 20 74 68 65 20 64 65 66  verts to the def
33af1 61 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64  ault temporary d
33af2 69 72 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e  irectory search.
33af3 0d 0a 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72  ..  ** If tempor
33af4 61 72 79 20 64 69 72 65 63 74 6f 72 79 20 69 73  ary directory is
33af5 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69   changed, then i
33af6 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f  nvalidateTempSto
33af7 72 61 67 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  rage...  **..  *
33af8 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
33af9 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
33afa 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
33afb 74 6f 72 79 22 29 3d 3d 30 20 29 7b 0d 0a 20 20  tory")==0 ){..  
33afc 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
33afd 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ..      if( sqli
33afe 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
33aff 72 79 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  ry ){..        s
33b00 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
33b01 43 6f 6c 73 28 76 2c 20 31 29 3b 0d 0a 20 20 20  Cols(v, 1);..   
33b02 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
33b03 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
33b04 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0d   COLNAME_NAME, .
33b05 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74 65  .            "te
33b06 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
33b07 72 79 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ry", SQLITE_STAT
33b08 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  IC);..        sq
33b09 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
33b0a 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
33b0b 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f  , 1, 0, sqlite3_
33b0c 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c 20  temp_directory, 
33b0d 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  0);..        sql
33b0e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
33b0f 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
33b10 31 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  1, 1);..      }.
33b11 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 23 69 66  .    }else{..#if
33b12 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33b13 5f 57 53 44 0d 0a 20 20 20 20 20 20 69 66 28 20  _WSD..      if( 
33b14 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0d 0a 20 20  zRight[0] ){..  
33b15 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20        int rc;.. 
33b16 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0d         int res;.
33b17 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
33b18 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62  lite3OsAccess(db
33b19 2d 3e 70 56 66 73 2c 20 7a 52 69 67 68 74 2c 20  ->pVfs, zRight, 
33b1a 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
33b1b 41 44 57 52 49 54 45 2c 20 26 72 65 73 29 3b 0d  ADWRITE, &res);.
33b1c 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
33b1d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 65  =SQLITE_OK || re
33b1e 73 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  s==0 ){..       
33b1f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
33b20 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20  sg(pParse, "not 
33b21 61 20 77 72 69 74 61 62 6c 65 20 64 69 72 65 63  a writable direc
33b22 74 6f 72 79 22 29 3b 0d 0a 20 20 20 20 20 20 20  tory");..       
33b23 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f     goto pragma_o
33b24 75 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ut;..        }..
33b25 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
33b26 66 28 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  f( SQLITE_TEMP_S
33b27 54 4f 52 45 3d 3d 30 0d 0a 20 20 20 20 20 20 20  TORE==0..       
33b28 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f  || (SQLITE_TEMP_
33b29 53 54 4f 52 45 3d 3d 31 20 26 26 20 64 62 2d 3e  STORE==1 && db->
33b2a 74 65 6d 70 5f 73 74 6f 72 65 3c 3d 31 29 0d 0a  temp_store<=1)..
33b2b 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54         || (SQLIT
33b2c 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20  E_TEMP_STORE==2 
33b2d 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  && db->temp_stor
33b2e 65 3d 3d 31 29 0d 0a 20 20 20 20 20 20 29 7b 0d  e==1)..      ){.
33b2f 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64  .        invalid
33b30 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70  ateTempStorage(p
33b31 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 7d  Parse);..      }
33b32 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
33b33 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65 6d  free(sqlite3_tem
33b34 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0d 0a 20  p_directory);.. 
33b35 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
33b36 30 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  0] ){..        s
33b37 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
33b38 63 74 6f 72 79 20 3d 20 73 71 6c 69 74 65 33 5f  ctory = sqlite3_
33b39 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 52  mprintf("%s", zR
33b3a 69 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 7d 65  ight);..      }e
33b3b 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
33b3c 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
33b3d 74 6f 72 79 20 3d 20 30 3b 0d 0a 20 20 20 20 20  tory = 0;..     
33b3e 20 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   }..#endif /* SQ
33b3f 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 2a 2f  LITE_OMIT_WSD */
33b40 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65  ..    }..  }else
33b41 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
33b42 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c  (SQLITE_ENABLE_L
33b43 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0d 0a 23  OCKING_STYLE)..#
33b44 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41    if defined(__A
33b45 50 50 4c 45 5f 5f 29 0d 0a 23 20 20 20 20 64 65  PPLE__)..#    de
33b46 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
33b47 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
33b48 20 31 0d 0a 23 20 20 65 6c 73 65 0d 0a 23 20 20   1..#  else..#  
33b49 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
33b4a 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
33b4b 54 59 4c 45 20 30 0d 0a 23 20 20 65 6e 64 69 66  TYLE 0..#  endif
33b4c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 53 51  ..#endif..#if SQ
33b4d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
33b4e 49 4e 47 5f 53 54 59 4c 45 0d 0a 20 20 2f 2a 0d  ING_STYLE..  /*.
33b4f 0a 20 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  .   **   PRAGMA 
33b50 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f  [database.]lock_
33b51 70 72 6f 78 79 5f 66 69 6c 65 0d 0a 20 20 20 2a  proxy_file..   *
33b52 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
33b53 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79  base.]lock_proxy
33b54 5f 66 69 6c 65 20 3d 20 22 3a 61 75 74 6f 3a 22  _file = ":auto:"
33b55 7c 22 6c 6f 63 6b 5f 66 69 6c 65 5f 70 61 74 68  |"lock_file_path
33b56 22 0d 0a 20 20 20 2a 2a 0d 0a 20 20 20 2a 2a 20  "..   **..   ** 
33b57 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
33b58 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c  e value of the l
33b59 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 66  ock_proxy_file f
33b5a 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0d 0a  lag.  Changing..
33b5b 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20     ** the value 
33b5c 73 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20  sets a specific 
33b5d 66 69 6c 65 20 74 6f 20 62 65 20 75 73 65 64 20  file to be used 
33b5e 66 6f 72 20 64 61 74 61 62 61 73 65 20 61 63 63  for database acc
33b5f 65 73 73 20 6c 6f 63 6b 73 2e 0d 0a 20 20 20 2a  ess locks...   *
33b60 2a 0d 0a 20 20 20 2a 2f 0d 0a 20 20 69 66 28 20  *..   */..  if( 
33b61 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
33b62 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78  Left, "lock_prox
33b63 79 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0d 0a  y_file")==0 ){..
33b64 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
33b65 29 7b 0d 0a 20 20 20 20 20 20 50 61 67 65 72 20  ){..      Pager 
33b66 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  *pPager = sqlite
33b67 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d  3BtreePager(pDb-
33b68 3e 70 42 74 29 3b 0d 0a 20 20 20 20 20 20 63 68  >pBt);..      ch
33b69 61 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f 70  ar *proxy_file_p
33b6a 61 74 68 20 3d 20 4e 55 4c 4c 3b 0d 0a 20 20 20  ath = NULL;..   
33b6b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
33b6c 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  *pFile = sqlite3
33b6d 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65 72  PagerFile(pPager
33b6e 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33b6f 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69  3OsFileControlHi
33b70 6e 74 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  nt(pFile, SQLITE
33b71 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
33b72 4c 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  LE, ..          
33b73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b74 20 26 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74   &proxy_file_pat
33b75 68 29 3b 0d 0a 20 20 20 20 20 20 0d 0a 20 20 20  h);..      ..   
33b76 20 20 20 69 66 28 20 70 72 6f 78 79 5f 66 69 6c     if( proxy_fil
33b77 65 5f 70 61 74 68 20 29 7b 0d 0a 20 20 20 20 20  e_path ){..     
33b78 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
33b79 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0d  tNumCols(v, 1);.
33b7a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
33b7b 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
33b7c 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
33b7d 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  E, ..           
33b7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b7f 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66     "lock_proxy_f
33b80 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ile", SQLITE_STA
33b81 54 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  TIC);..        s
33b82 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
33b83 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
33b84 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66  0, 1, 0, proxy_f
33b85 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0d 0a 20  ile_path, 0);.. 
33b86 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33b87 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
33b88 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b  esultRow, 1, 1);
33b89 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
33b8a 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 50 61 67  else{..      Pag
33b8b 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
33b8c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
33b8d 44 62 2d 3e 70 42 74 29 3b 0d 0a 20 20 20 20 20  Db->pBt);..     
33b8e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
33b8f 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61  File = sqlite3Pa
33b90 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
33b91 0d 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ..      int res;
33b92 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67  ..      if( zRig
33b93 68 74 5b 30 5d 20 29 7b 0d 0a 20 20 20 20 20 20  ht[0] ){..      
33b94 20 20 72 65 73 3d 73 71 6c 69 74 65 33 4f 73 46    res=sqlite3OsF
33b95 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65  ileControl(pFile
33b96 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  , SQLITE_SET_LOC
33b97 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0d 0a 20 20  KPROXYFILE, ..  
33b98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b9a 20 20 20 7a 52 69 67 68 74 29 3b 0d 0a 20 20 20     zRight);..   
33b9b 20 20 20 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20     } else {..   
33b9c 20 20 20 20 20 72 65 73 3d 73 71 6c 69 74 65 33       res=sqlite3
33b9d 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46  OsFileControl(pF
33b9e 69 6c 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f  ile, SQLITE_SET_
33b9f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0d  LOCKPROXYFILE, .
33ba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33ba1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ba2 20 20 20 20 20 20 4e 55 4c 4c 29 3b 0d 0a 20 20        NULL);..  
33ba3 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
33ba4 20 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   res!=SQLITE_OK 
33ba5 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
33ba6 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
33ba7 73 65 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 73  se, "failed to s
33ba8 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69  et lock proxy fi
33ba9 6c 65 22 29 3b 0d 0a 20 20 20 20 20 20 20 20 67  le");..        g
33baa 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d  oto pragma_out;.
33bab 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
33bac 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66  .  }else..#endif
33bad 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
33bae 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
33baf 2a 2f 20 20 20 20 20 20 0d 0a 20 20 20 20 0d 0a  */      ..    ..
33bb0 20 20 2f 2a 0d 0a 20 20 2a 2a 20 20 20 50 52 41    /*..  **   PRA
33bb1 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73  GMA [database.]s
33bb2 79 6e 63 68 72 6f 6e 6f 75 73 0d 0a 20 20 2a 2a  ynchronous..  **
33bb3 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
33bb4 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73  ase.]synchronous
33bb5 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46  =OFF|ON|NORMAL|F
33bb6 55 4c 4c 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  ULL..  **..  ** 
33bb7 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
33bb8 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66  e local value of
33bb9 20 74 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73   the synchronous
33bba 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67   flag.  Changing
33bbb 0d 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c  ..  ** the local
33bbc 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20   value does not 
33bbd 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20  make changes to 
33bbe 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e  the disk file an
33bbf 64 20 74 68 65 0d 0a 20 20 2a 2a 20 64 65 66 61  d the..  ** defa
33bc0 75 6c 74 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  ult value will b
33bc1 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
33bc2 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
33bc3 61 62 61 73 65 20 69 73 0d 0a 20 20 2a 2a 20 6f  abase is..  ** o
33bc4 70 65 6e 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  pened...  */..  
33bc5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
33bc6 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68 72  mp(zLeft,"synchr
33bc7 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0d 0a 20  onous")==0 ){.. 
33bc8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
33bc9 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
33bca 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
33bcb 75 74 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a 52  ut;..    if( !zR
33bcc 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 72  ight ){..      r
33bcd 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
33bce 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e  Parse, "synchron
33bcf 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74  ous", pDb->safet
33bd0 79 5f 6c 65 76 65 6c 2d 31 29 3b 0d 0a 20 20 20  y_level-1);..   
33bd1 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69   }else{..      i
33bd2 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
33bd3 69 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  it ){..        s
33bd4 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
33bd5 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20  Parse, ..       
33bd6 20 20 20 20 20 22 53 61 66 65 74 79 20 6c 65 76       "Safety lev
33bd7 65 6c 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68  el may not be ch
33bd8 61 6e 67 65 64 20 69 6e 73 69 64 65 20 61 20 74  anged inside a t
33bd9 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0d 0a 20  ransaction");.. 
33bda 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
33bdb 20 20 20 20 20 70 44 62 2d 3e 73 61 66 65 74 79       pDb->safety
33bdc 5f 6c 65 76 65 6c 20 3d 20 67 65 74 53 61 66 65  _level = getSafe
33bdd 74 79 4c 65 76 65 6c 28 7a 52 69 67 68 74 29 2b  tyLevel(zRight)+
33bde 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  1;..      }..   
33bdf 20 7d 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e   }..  }else..#en
33be0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
33be1 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
33be2 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   */....#ifndef S
33be3 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f  QLITE_OMIT_FLAG_
33be4 50 52 41 47 4d 41 53 0d 0a 20 20 69 66 28 20 66  PRAGMAS..  if( f
33be5 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72 73 65  lagPragma(pParse
33be6 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 29  , zLeft, zRight)
33be7 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20   ){..    /* The 
33be8 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75 62  flagPragma() sub
33be9 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e  routine also gen
33bea 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65 73  erates any neces
33beb 73 61 72 79 20 63 6f 64 65 0d 0a 20 20 20 20 2a  sary code..    *
33bec 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69  * there is nothi
33bed 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65  ng more to do he
33bee 72 65 20 2a 2f 0d 0a 20 20 7d 65 6c 73 65 0d 0a  re */..  }else..
33bef 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
33bf0 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
33bf1 41 53 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66  AS */....#ifndef
33bf2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
33bf3 45 4d 41 5f 50 52 41 47 4d 41 53 0d 0a 20 20 2f  EMA_PRAGMAS..  /
33bf4 2a 0d 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  *..  **   PRAGMA
33bf5 20 74 61 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62   table_info(<tab
33bf6 6c 65 3e 29 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  le>)..  **..  **
33bf7 20 52 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65   Return a single
33bf8 20 72 6f 77 20 66 6f 72 20 65 61 63 68 20 63 6f   row for each co
33bf9 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65  lumn of the name
33bfa 64 20 74 61 62 6c 65 2e 20 54 68 65 20 63 6f 6c  d table. The col
33bfb 75 6d 6e 73 20 6f 66 0d 0a 20 20 2a 2a 20 74 68  umns of..  ** th
33bfc 65 20 72 65 74 75 72 6e 65 64 20 64 61 74 61 20  e returned data 
33bfd 73 65 74 20 61 72 65 3a 0d 0a 20 20 2a 2a 0d 0a  set are:..  **..
33bfe 20 20 2a 2a 20 63 69 64 3a 20 20 20 20 20 20 20    ** cid:       
33bff 20 43 6f 6c 75 6d 6e 20 69 64 20 28 6e 75 6d 62   Column id (numb
33c00 65 72 65 64 20 66 72 6f 6d 20 6c 65 66 74 20 74  ered from left t
33c01 6f 20 72 69 67 68 74 2c 20 73 74 61 72 74 69 6e  o right, startin
33c02 67 20 61 74 20 30 29 0d 0a 20 20 2a 2a 20 6e 61  g at 0)..  ** na
33c03 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  me:       Column
33c04 20 6e 61 6d 65 0d 0a 20 20 2a 2a 20 74 79 70 65   name..  ** type
33c05 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 64  :       Column d
33c06 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 2e  eclaration type.
33c07 0d 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20  ..  ** notnull: 
33c08 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f 54 20     True if 'NOT 
33c09 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20 6f 66  NULL' is part of
33c0a 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
33c0b 69 6f 6e 0d 0a 20 20 2a 2a 20 64 66 6c 74 5f 76  ion..  ** dflt_v
33c0c 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75 6c  alue: The defaul
33c0d 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
33c0e 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0d  column, if any..
33c0f 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  .  */..  if( sql
33c10 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
33c11 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29  t, "table_info")
33c12 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
33c13 0d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ..    Table *pTa
33c14 62 3b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69  b;..    if( sqli
33c15 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
33c16 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
33c17 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20 20 20 70 54  gma_out;..    pT
33c18 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
33c19 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74  Table(db, zRight
33c1a 2c 20 7a 44 62 29 3b 0d 0a 20 20 20 20 69 66 28  , zDb);..    if(
33c1b 20 70 54 61 62 20 29 7b 0d 0a 20 20 20 20 20 20   pTab ){..      
33c1c 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20 69 6e  int i;..      in
33c1d 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0d 0a  t nHidden = 0;..
33c1e 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
33c1f 6f 6c 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  ol;..      sqlit
33c20 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
33c21 28 76 2c 20 36 29 3b 0d 0a 20 20 20 20 20 20 70  (v, 6);..      p
33c22 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b  Parse->nMem = 6;
33c23 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
33c24 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
33c25 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
33c26 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54 45 5f  , "cid", SQLITE_
33c27 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 20 20  STATIC);..      
33c28 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
33c29 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
33c2a 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
33c2b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
33c2c 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
33c2d 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
33c2e 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
33c2f 45 2c 20 22 74 79 70 65 22 2c 20 53 51 4c 49 54  E, "type", SQLIT
33c30 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20  E_STATIC);..    
33c31 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
33c32 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f  ColName(v, 3, CO
33c33 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74  LNAME_NAME, "not
33c34 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54  null", SQLITE_ST
33c35 41 54 49 43 29 3b 0d 0a 20 20 20 20 20 20 73 71  ATIC);..      sq
33c36 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33c37 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d  ame(v, 4, COLNAM
33c38 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61  E_NAME, "dflt_va
33c39 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  lue", SQLITE_STA
33c3a 54 49 43 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  TIC);..      sql
33c3b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
33c3c 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45  me(v, 5, COLNAME
33c3d 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51 4c  _NAME, "pk", SQL
33c3e 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20  ITE_STATIC);..  
33c3f 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47      sqlite3ViewG
33c40 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
33c41 61 72 73 65 2c 20 70 54 61 62 29 3b 0d 0a 20 20  arse, pTab);..  
33c42 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f      for(i=0, pCo
33c43 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c  l=pTab->aCol; i<
33c44 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c  pTab->nCol; i++,
33c45 20 70 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20   pCol++){..     
33c46 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43     if( IsHiddenC
33c47 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0d 0a  olumn(pCol) ){..
33c48 20 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65            nHidde
33c49 6e 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  n++;..          
33c4a 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20  continue;..     
33c4b 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73 71     }..        sq
33c4c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33c4d 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
33c4e 2d 6e 48 69 64 64 65 6e 2c 20 31 29 3b 0d 0a 20  -nHidden, 1);.. 
33c4f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33c50 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
33c51 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
33c52 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pCol->zName, 0)
33c53 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
33c54 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
33c55 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
33c56 2c 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  , 0,..          
33c57 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f 20 70   pCol->zType ? p
33c58 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22 22 2c  Col->zType : "",
33c59 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71   0);..        sq
33c5a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33c5b 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 28  v, OP_Integer, (
33c5c 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 3f 20  pCol->notNull ? 
33c5d 31 20 3a 20 30 29 2c 20 34 29 3b 0d 0a 20 20 20  1 : 0), 4);..   
33c5e 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 7a       if( pCol->z
33c5f 44 66 6c 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  Dflt ){..       
33c60 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
33c61 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
33c62 67 38 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63 68  g8, 0, 5, 0, (ch
33c63 61 72 2a 29 70 43 6f 6c 2d 3e 7a 44 66 6c 74 2c  ar*)pCol->zDflt,
33c64 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65   0);..        }e
33c65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
33c66 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33c67 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
33c68 20 35 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d   5);..        }.
33c69 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
33c6a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
33c6b 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c 2d 3e  _Integer, pCol->
33c6c 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b 0d 0a  isPrimKey, 6);..
33c6d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33c6e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
33c6f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36 29  ResultRow, 1, 6)
33c70 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
33c71 7d 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 20 20  }..  }else....  
33c72 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
33c73 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78  mp(zLeft, "index
33c74 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52  _info")==0 && zR
33c75 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 49 6e 64  ight ){..    Ind
33c76 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20 20 20 54  ex *pIdx;..    T
33c77 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a 20 20 20  able *pTab;..   
33c78 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
33c79 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
33c7a 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
33c7b 3b 0d 0a 20 20 20 20 70 49 64 78 20 3d 20 73 71  ;..    pIdx = sq
33c7c 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
33c7d 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b  b, zRight, zDb);
33c7e 0d 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29  ..    if( pIdx )
33c7f 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0d  {..      int i;.
33c80 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49  .      pTab = pI
33c81 64 78 2d 3e 70 54 61 62 6c 65 3b 0d 0a 20 20 20  dx->pTable;..   
33c82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
33c83 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0d  tNumCols(v, 3);.
33c84 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
33c85 4d 65 6d 20 3d 20 33 3b 0d 0a 20 20 20 20 20 20  Mem = 3;..      
33c86 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
33c87 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
33c88 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 6e 6f  AME_NAME, "seqno
33c89 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
33c8a 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33c8b 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
33c8c 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
33c8d 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54  ME, "cid", SQLIT
33c8e 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20  E_STATIC);..    
33c8f 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
33c90 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
33c91 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
33c92 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
33c93 43 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69  C);..      for(i
33c94 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
33c95 75 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  umn; i++){..    
33c96 20 20 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70      int cnum = p
33c97 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
33c98 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
33c99 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
33c9a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31  OP_Integer, i, 1
33c9b 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
33c9c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33c9d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 63 6e 75   OP_Integer, cnu
33c9e 6d 2c 20 32 29 3b 0d 0a 20 20 20 20 20 20 20 20  m, 2);..        
33c9f 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43  assert( pTab->nC
33ca0 6f 6c 3e 63 6e 75 6d 20 29 3b 0d 0a 20 20 20 20  ol>cnum );..    
33ca1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
33ca2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
33ca3 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 54  ng8, 0, 3, 0, pT
33ca4 61 62 2d 3e 61 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a  ab->aCol[cnum].z
33ca5 4e 61 6d 65 2c 20 30 29 3b 0d 0a 20 20 20 20 20  Name, 0);..     
33ca6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
33ca7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
33ca8 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0d 0a 20 20  tRow, 1, 3);..  
33ca9 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
33caa 7d 65 6c 73 65 0d 0a 0d 0a 20 20 69 66 28 20 73  }else....  if( s
33cab 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
33cac 65 66 74 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74  eft, "index_list
33cad 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
33cae 29 7b 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  ){..    Index *p
33caf 49 64 78 3b 0d 0a 20 20 20 20 54 61 62 6c 65 20  Idx;..    Table 
33cb0 2a 70 54 61 62 3b 0d 0a 20 20 20 20 69 66 28 20  *pTab;..    if( 
33cb1 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
33cb2 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
33cb3 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20   pragma_out;..  
33cb4 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
33cb5 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
33cb6 69 67 68 74 2c 20 7a 44 62 29 3b 0d 0a 20 20 20  ight, zDb);..   
33cb7 20 69 66 28 20 70 54 61 62 20 29 7b 0d 0a 20 20   if( pTab ){..  
33cb8 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
33cb9 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d  etVdbe(pParse);.
33cba 0a 20 20 20 20 20 20 70 49 64 78 20 3d 20 70 54  .      pIdx = pT
33cbb 61 62 2d 3e 70 49 6e 64 65 78 3b 0d 0a 20 20 20  ab->pIndex;..   
33cbc 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0d 0a     if( pIdx ){..
33cbd 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20          int i = 
33cbe 30 3b 20 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  0; ..        sql
33cbf 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
33cc0 6c 73 28 76 2c 20 33 29 3b 0d 0a 20 20 20 20 20  ls(v, 3);..     
33cc1 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
33cc2 3d 20 33 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  = 3;..        sq
33cc3 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33cc4 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
33cc5 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53  E_NAME, "seq", S
33cc6 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
33cc7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33cc8 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
33cc9 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
33cca 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
33ccb 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 20  _STATIC);..     
33ccc 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
33ccd 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
33cce 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75 6e  OLNAME_NAME, "un
33ccf 69 71 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  ique", SQLITE_ST
33cd0 41 54 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20  ATIC);..        
33cd1 77 68 69 6c 65 28 70 49 64 78 29 7b 0d 0a 20 20  while(pIdx){..  
33cd2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33cd3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
33cd4 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0d  Integer, i, 1);.
33cd5 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
33cd6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
33cd7 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
33cd8 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  , 0, pIdx->zName
33cd9 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  , 0);..         
33cda 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33cdb 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
33cdc 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  , pIdx->onError!
33cdd 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0d 0a 20  =OE_None, 3);.. 
33cde 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
33cdf 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
33ce0 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33  _ResultRow, 1, 3
33ce1 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2b 2b  );..          ++
33ce2 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 49  i;..          pI
33ce3 64 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74  dx = pIdx->pNext
33ce4 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
33ce5 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
33ce6 7d 65 6c 73 65 0d 0a 0d 0a 20 20 69 66 28 20 73  }else....  if( s
33ce7 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
33ce8 65 66 74 2c 20 22 64 61 74 61 62 61 73 65 5f 6c  eft, "database_l
33ce9 69 73 74 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  ist")==0 ){..   
33cea 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 69 66 28   int i;..    if(
33ceb 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
33cec 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
33ced 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20  o pragma_out;.. 
33cee 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
33cef 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0d  tNumCols(v, 3);.
33cf0 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
33cf1 6d 20 3d 20 33 3b 0d 0a 20 20 20 20 73 71 6c 69  m = 3;..    sqli
33cf2 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
33cf3 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
33cf4 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c  NAME, "seq", SQL
33cf5 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20  ITE_STATIC);..  
33cf6 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
33cf7 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
33cf8 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
33cf9 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
33cfa 43 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  C);..    sqlite3
33cfb 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
33cfc 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
33cfd 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c 49 54  E, "file", SQLIT
33cfe 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20  E_STATIC);..    
33cff 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
33d00 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  Db; i++){..     
33d01 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
33d02 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  pBt==0 ) continu
33d03 65 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  e;..      assert
33d04 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  ( db->aDb[i].zNa
33d05 6d 65 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  me!=0 );..      
33d06 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33d07 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
33d08 20 69 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 73   i, 1);..      s
33d09 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
33d0a 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
33d0b 30 2c 20 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62  0, 2, 0, db->aDb
33d0c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0d 0a  [i].zName, 0);..
33d0d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33d0e 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
33d0f 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0d  ring8, 0, 3, 0,.
33d10 0a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  .           sqli
33d11 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
33d12 61 6d 65 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  ame(db->aDb[i].p
33d13 42 74 29 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  Bt), 0);..      
33d14 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33d15 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
33d16 77 2c 20 31 2c 20 33 29 3b 0d 0a 20 20 20 20 7d  w, 1, 3);..    }
33d17 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 20 20 69  ..  }else....  i
33d18 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
33d19 70 28 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61 74  p(zLeft, "collat
33d1a 69 6f 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b  ion_list")==0 ){
33d1b 0d 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  ..    int i = 0;
33d1c 0d 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a  ..    HashElem *
33d1d 70 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p;..    sqlite3V
33d1e 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
33d1f 20 32 29 3b 0d 0a 20 20 20 20 70 50 61 72 73 65   2);..    pParse
33d20 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0d 0a 20 20 20  ->nMem = 2;..   
33d21 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
33d22 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
33d23 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
33d24 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
33d25 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
33d26 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
33d27 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
33d28 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
33d29 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 66 6f  STATIC);..    fo
33d2a 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
33d2b 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
33d2c 71 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  q); p; p=sqliteH
33d2d 61 73 68 4e 65 78 74 28 70 29 29 7b 0d 0a 20 20  ashNext(p)){..  
33d2e 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
33d2f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29  ll = (CollSeq *)
33d30 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70  sqliteHashData(p
33d31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33d32 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
33d33 50 5f 49 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20  P_Integer, i++, 
33d34 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  1);..      sqlit
33d35 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
33d36 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
33d37 2c 20 30 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  , 0, pColl->zNam
33d38 65 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 73 71  e, 0);..      sq
33d39 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33d3a 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
33d3b 20 31 2c 20 32 29 3b 0d 0a 20 20 20 20 7d 0d 0a   1, 2);..    }..
33d3c 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 20    }else..#endif 
33d3d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
33d3e 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20 2a 2f  CHEMA_PRAGMAS */
33d3f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
33d40 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
33d41 4b 45 59 0d 0a 20 20 69 66 28 20 73 71 6c 69 74  KEY..  if( sqlit
33d42 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
33d43 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69   "foreign_key_li
33d44 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  st")==0 && zRigh
33d45 74 20 29 7b 0d 0a 20 20 20 20 46 4b 65 79 20 2a  t ){..    FKey *
33d46 70 46 4b 3b 0d 0a 20 20 20 20 54 61 62 6c 65 20  pFK;..    Table 
33d47 2a 70 54 61 62 3b 0d 0a 20 20 20 20 69 66 28 20  *pTab;..    if( 
33d48 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
33d49 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
33d4a 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20   pragma_out;..  
33d4b 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
33d4c 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
33d4d 69 67 68 74 2c 20 7a 44 62 29 3b 0d 0a 20 20 20  ight, zDb);..   
33d4e 20 69 66 28 20 70 54 61 62 20 29 7b 0d 0a 20 20   if( pTab ){..  
33d4f 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
33d50 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d  etVdbe(pParse);.
33d51 0a 20 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61  .      pFK = pTa
33d52 62 2d 3e 70 46 4b 65 79 3b 0d 0a 20 20 20 20 20  b->pFKey;..     
33d53 20 69 66 28 20 70 46 4b 20 29 7b 0d 0a 20 20 20   if( pFK ){..   
33d54 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20       int i = 0; 
33d55 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
33d56 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
33d57 76 2c 20 38 29 3b 0d 0a 20 20 20 20 20 20 20 20  v, 8);..        
33d58 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 38  pParse->nMem = 8
33d59 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
33d5a 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
33d5b 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
33d5c 41 4d 45 2c 20 22 69 64 22 2c 20 53 51 4c 49 54  AME, "id", SQLIT
33d5d 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20  E_STATIC);..    
33d5e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
33d5f 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
33d60 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
33d61 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  eq", SQLITE_STAT
33d62 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  IC);..        sq
33d63 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33d64 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d  ame(v, 2, COLNAM
33d65 45 5f 4e 41 4d 45 2c 20 22 74 61 62 6c 65 22 2c  E_NAME, "table",
33d66 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
33d67 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
33d68 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
33d69 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 3, COLNAME_NA
33d6a 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49  ME, "from", SQLI
33d6b 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20  TE_STATIC);..   
33d6c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
33d6d 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c  SetColName(v, 4,
33d6e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
33d6f 74 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  to", SQLITE_STAT
33d70 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  IC);..        sq
33d71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33d72 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d  ame(v, 5, COLNAM
33d73 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75 70 64 61  E_NAME, "on_upda
33d74 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  te", SQLITE_STAT
33d75 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  IC);..        sq
33d76 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33d77 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c 4e 41 4d  ame(v, 6, COLNAM
33d78 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65 6c 65  E_NAME, "on_dele
33d79 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  te", SQLITE_STAT
33d7a 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  IC);..        sq
33d7b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
33d7c 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e 41 4d  ame(v, 7, COLNAM
33d7d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68 22 2c  E_NAME, "match",
33d7e 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
33d7f 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ..        while(
33d80 70 46 4b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  pFK){..         
33d81 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 20 20 20   int j;..       
33d82 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46     for(j=0; j<pF
33d83 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0d 0a  K->nCol; j++){..
33d84 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72              char
33d85 20 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43   *zCol = pFK->aC
33d86 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0d 0a 20 20 20  ol[j].zCol;..   
33d87 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
33d88 4f 6e 44 65 6c 65 74 65 20 3d 20 28 63 68 61 72  OnDelete = (char
33d89 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46   *)actionName(pF
33d8a 4b 2d 3e 61 41 63 74 69 6f 6e 5b 30 5d 29 3b 0d  K->aAction[0]);.
33d8b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
33d8c 72 20 2a 7a 4f 6e 55 70 64 61 74 65 20 3d 20 28  r *zOnUpdate = (
33d8d 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d  char *)actionNam
33d8e 65 28 70 46 4b 2d 3e 61 41 63 74 69 6f 6e 5b 31  e(pFK->aAction[1
33d8f 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ]);..           
33d90 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33d91 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
33d92 2c 20 69 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20  , i, 1);..      
33d93 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33d94 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
33d95 74 65 67 65 72 2c 20 6a 2c 20 32 29 3b 0d 0a 20  teger, j, 2);.. 
33d96 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
33d97 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
33d98 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
33d99 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c 20 30  , 0, pFK->zTo, 0
33d9a 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
33d9b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33d9c 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
33d9d 20 30 2c 20 34 2c 20 30 2c 0d 0a 20 20 20 20 20   0, 4, 0,..     
33d9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d9f 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61           pTab->a
33da0 43 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d  Col[pFK->aCol[j]
33da1 2e 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30  .iFrom].zName, 0
33da2 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
33da3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33da4 34 28 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53  4(v, zCol ? OP_S
33da5 74 72 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c  tring8 : OP_Null
33da6 2c 20 30 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c 2c  , 0, 5, 0, zCol,
33da7 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   0);..          
33da8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
33da9 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
33daa 38 2c 20 30 2c 20 36 2c 20 30 2c 20 7a 4f 6e 55  8, 0, 6, 0, zOnU
33dab 70 64 61 74 65 2c 20 30 29 3b 0d 0a 20 20 20 20  pdate, 0);..    
33dac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33dad 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
33dae 53 74 72 69 6e 67 38 2c 20 30 2c 20 37 2c 20 30  String8, 0, 7, 0
33daf 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c 20 30 29 3b  , zOnDelete, 0);
33db0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ..            sq
33db1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
33db2 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
33db3 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45 22 2c 20  , 8, 0, "NONE", 
33db4 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  0);..           
33db5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33db6 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
33db7 6f 77 2c 20 31 2c 20 38 29 3b 0d 0a 20 20 20 20  ow, 1, 8);..    
33db8 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
33db9 20 20 20 2b 2b 69 3b 0d 0a 20 20 20 20 20 20 20     ++i;..       
33dba 20 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e     pFK = pFK->pN
33dbb 65 78 74 46 72 6f 6d 3b 0d 0a 20 20 20 20 20 20  extFrom;..      
33dbc 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
33dbd 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65    }..  }else..#e
33dbe 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
33dbf 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
33dc0 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0d 0a 0d 0a  EIGN_KEY) */....
33dc1 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
33dc2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
33dc3 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70 61 72  ICmp(zLeft, "par
33dc4 73 65 72 5f 74 72 61 63 65 22 29 3d 3d 30 20 29  ser_trace")==0 )
33dc5 7b 0d 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  {..    if( zRigh
33dc6 74 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  t ){..      if( 
33dc7 73 71 6c 69 74 65 33 47 65 74 42 6f 6f 6c 65 61  sqlite3GetBoolea
33dc8 6e 28 7a 52 69 67 68 74 29 20 29 7b 0d 0a 20 20  n(zRight) ){..  
33dc9 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
33dca 73 65 72 54 72 61 63 65 28 73 74 64 65 72 72 2c  serTrace(stderr,
33dcb 20 22 70 61 72 73 65 72 3a 20 22 29 3b 0d 0a 20   "parser: ");.. 
33dcc 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
33dcd 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
33dce 65 72 54 72 61 63 65 28 30 2c 20 30 29 3b 0d 0a  erTrace(0, 0);..
33dcf 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
33dd0 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d    }else..#endif.
33dd1 0a 0d 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c  ...  /* Reinstal
33dd2 6c 20 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47  l the LIKE and G
33dd3 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  LOB functions.  
33dd4 54 68 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c  The variant of L
33dd5 49 4b 45 0d 0a 20 20 2a 2a 20 75 73 65 64 20 77  IKE..  ** used w
33dd6 69 6c 6c 20 62 65 20 63 61 73 65 20 73 65 6e 73  ill be case sens
33dd7 69 74 69 76 65 20 6f 72 20 6e 6f 74 20 64 65 70  itive or not dep
33dd8 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 52 48  ending on the RH
33dd9 53 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  S...  */..  if( 
33dda 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
33ddb 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65 6e 73  Left, "case_sens
33ddc 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d 30 20  itive_like")==0 
33ddd 29 7b 0d 0a 20 20 20 20 69 66 28 20 7a 52 69 67  ){..    if( zRig
33dde 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  ht ){..      sql
33ddf 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65  ite3RegisterLike
33de0 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 73 71  Functions(db, sq
33de1 6c 69 74 65 33 47 65 74 42 6f 6f 6c 65 61 6e 28  lite3GetBoolean(
33de2 7a 52 69 67 68 74 29 29 3b 0d 0a 20 20 20 20 7d  zRight));..    }
33de3 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 23 69 66  ..  }else....#if
33de4 6e 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 45  ndef SQLITE_INTE
33de5 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
33de6 52 5f 4d 41 58 0d 0a 23 20 64 65 66 69 6e 65 20  R_MAX..# define 
33de7 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59  SQLITE_INTEGRITY
33de8 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58  _CHECK_ERROR_MAX
33de9 20 31 30 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a   100..#endif....
33dea 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33deb 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
33dec 45 43 4b 0d 0a 20 20 2f 2a 20 50 72 61 67 6d 61  ECK..  /* Pragma
33ded 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69   "quick_check" i
33dee 73 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61  s an experimenta
33def 6c 20 72 65 64 75 63 65 64 20 76 65 72 73 69 6f  l reduced versio
33df0 6e 20 6f 66 20 0d 0a 20 20 2a 2a 20 69 6e 74 65  n of ..  ** inte
33df1 67 72 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69  grity_check desi
33df2 67 6e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d  gned to detect m
33df3 6f 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72  ost database cor
33df4 72 75 70 74 69 6f 6e 0d 0a 20 20 2a 2a 20 77 69  ruption..  ** wi
33df5 74 68 6f 75 74 20 6d 6f 73 74 20 6f 66 20 74 68  thout most of th
33df6 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 61 20  e overhead of a 
33df7 66 75 6c 6c 20 69 6e 74 65 67 72 69 74 79 2d 63  full integrity-c
33df8 68 65 63 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  heck...  */..  i
33df9 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
33dfa 70 28 7a 4c 65 66 74 2c 20 22 69 6e 74 65 67 72  p(zLeft, "integr
33dfb 69 74 79 5f 63 68 65 63 6b 22 29 3d 3d 30 0d 0a  ity_check")==0..
33dfc 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
33dfd 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 71 75 69  ICmp(zLeft, "qui
33dfe 63 6b 5f 63 68 65 63 6b 22 29 3d 3d 30 20 0d 0a  ck_check")==0 ..
33dff 20 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 2c    ){..    int i,
33e00 20 6a 2c 20 61 64 64 72 2c 20 6d 78 45 72 72 3b   j, addr, mxErr;
33e01 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  ....    /* Code 
33e02 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
33e03 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
33e04 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 20  ntegrity check. 
33e05 20 49 66 20 6e 6f 20 65 72 72 6f 72 0d 0a 20 20   If no error..  
33e06 20 20 2a 2a 20 6d 65 73 73 61 67 65 73 20 68 61    ** messages ha
33e07 76 65 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65  ve been generate
33e08 64 2c 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20 4f  d, output OK.  O
33e09 74 68 65 72 77 69 73 65 20 6f 75 74 70 75 74 20  therwise output 
33e0a 74 68 65 0d 0a 20 20 20 20 2a 2a 20 65 72 72 6f  the..    ** erro
33e0b 72 20 6d 65 73 73 61 67 65 0d 0a 20 20 20 20 2a  r message..    *
33e0c 2f 0d 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  /..    static co
33e0d 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 65  nst VdbeOpList e
33e0e 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0d 0a 20 20  ndCode[] = {..  
33e0f 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
33e10 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20        1, 0,     
33e11 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a     0},    /* 0 *
33e12 2f 0d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66  /..      { OP_If
33e13 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30 2c  Neg,       1, 0,
33e14 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
33e15 2a 20 31 20 2a 2f 0d 0a 20 20 20 20 20 20 7b 20  * 1 */..      { 
33e16 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
33e17 30 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 3,        0},
33e18 20 20 20 20 2f 2a 20 32 20 2a 2f 0d 0a 20 20 20      /* 2 */..   
33e19 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
33e1a 77 2c 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20  w,   3, 1,      
33e1b 20 20 30 7d 2c 0d 0a 20 20 20 20 7d 3b 0d 0a 0d    0},..    };...
33e1c 0a 20 20 20 20 69 6e 74 20 69 73 51 75 69 63 6b  .    int isQuick
33e1d 20 3d 20 28 73 71 6c 69 74 65 33 54 6f 6c 6f 77   = (sqlite3Tolow
33e1e 65 72 28 7a 4c 65 66 74 5b 30 5d 29 3d 3d 27 71  er(zLeft[0])=='q
33e1f 27 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 6e  ');....    /* In
33e20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 56 44 42  itialize the VDB
33e21 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20 20  E program */..  
33e22 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
33e23 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
33e24 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
33e25 74 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  t;..    pParse->
33e26 6e 4d 65 6d 20 3d 20 36 3b 0d 0a 20 20 20 20 73  nMem = 6;..    s
33e27 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
33e28 43 6f 6c 73 28 76 2c 20 31 29 3b 0d 0a 20 20 20  Cols(v, 1);..   
33e29 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
33e2a 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
33e2b 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74 65  NAME_NAME, "inte
33e2c 67 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 53 51  grity_check", SQ
33e2d 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 0d  LITE_STATIC);...
33e2e 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20  .    /* Set the 
33e2f 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72 20 63 6f  maximum error co
33e30 75 6e 74 20 2a 2f 0d 0a 20 20 20 20 6d 78 45 72  unt */..    mxEr
33e31 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  r = SQLITE_INTEG
33e32 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52  RITY_CHECK_ERROR
33e33 5f 4d 41 58 3b 0d 0a 20 20 20 20 69 66 28 20 7a  _MAX;..    if( z
33e34 52 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20 20  Right ){..      
33e35 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28  sqlite3GetInt32(
33e36 7a 52 69 67 68 74 2c 20 26 6d 78 45 72 72 29 3b  zRight, &mxErr);
33e37 0d 0a 20 20 20 20 20 20 69 66 28 20 6d 78 45 72  ..      if( mxEr
33e38 72 3c 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  r<=0 ){..       
33e39 20 6d 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f   mxErr = SQLITE_
33e3a 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f  INTEGRITY_CHECK_
33e3b 45 52 52 4f 52 5f 4d 41 58 3b 0d 0a 20 20 20 20  ERROR_MAX;..    
33e3c 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
33e3d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33e3e 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
33e3f 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a 20   mxErr, 1);  /* 
33e40 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72 72  reg[1] holds err
33e41 6f 72 73 20 6c 65 66 74 20 2a 2f 0d 0a 0d 0a 20  ors left */.... 
33e42 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
33e43 67 72 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65  grity check on e
33e44 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c  ach database fil
33e45 65 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69 3d  e */..    for(i=
33e46 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
33e47 2b 29 7b 0d 0a 20 20 20 20 20 20 48 61 73 68 45  +){..      HashE
33e48 6c 65 6d 20 2a 78 3b 0d 0a 20 20 20 20 20 20 48  lem *x;..      H
33e49 61 73 68 20 2a 70 54 62 6c 73 3b 0d 0a 20 20 20  ash *pTbls;..   
33e4a 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0d     int cnt = 0;.
33e4b 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 4f 4d 49  ...      if( OMI
33e4c 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d 3d 31  T_TEMPDB && i==1
33e4d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 0d 0a   ) continue;....
33e4e 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
33e4f 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
33e50 61 72 73 65 2c 20 69 29 3b 0d 0a 20 20 20 20 20  arse, i);..     
33e51 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
33e52 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
33e53 49 66 50 6f 73 2c 20 31 29 3b 20 2f 2a 20 48 61  IfPos, 1); /* Ha
33e54 6c 74 20 69 66 20 6f 75 74 20 6f 66 20 65 72 72  lt if out of err
33e55 6f 72 73 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71  ors */..      sq
33e56 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33e57 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
33e58 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33e59 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
33e5a 20 61 64 64 72 29 3b 0d 0a 0d 0a 20 20 20 20 20   addr);....     
33e5b 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72   /* Do an integr
33e5c 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ity check of the
33e5d 20 42 2d 54 72 65 65 0d 0a 20 20 20 20 20 20 2a   B-Tree..      *
33e5e 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69  *..      ** Begi
33e5f 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67  n by filling reg
33e60 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e  isters 2, 3, ...
33e61 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70   with the root p
33e62 61 67 65 73 20 6e 75 6d 62 65 72 73 0d 0a 20 20  ages numbers..  
33e63 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74      ** for all t
33e64 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
33e65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
33e66 65 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  e...      */..  
33e67 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
33e68 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
33e69 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29  ld(db, iDb, 0) )
33e6a 3b 0d 0a 20 20 20 20 20 20 70 54 62 6c 73 20 3d  ;..      pTbls =
33e6b 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63   &db->aDb[i].pSc
33e6c 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0d 0a  hema->tblHash;..
33e6d 20 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69        for(x=sqli
33e6e 74 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c  teHashFirst(pTbl
33e6f 73 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48  s); x; x=sqliteH
33e70 61 73 68 4e 65 78 74 28 78 29 29 7b 0d 0a 20 20  ashNext(x)){..  
33e71 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
33e72 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61  b = sqliteHashDa
33e73 74 61 28 78 29 3b 0d 0a 20 20 20 20 20 20 20 20  ta(x);..        
33e74 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20  Index *pIdx;..  
33e75 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33e76 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
33e77 74 65 67 65 72 2c 20 70 54 61 62 2d 3e 74 6e 75  teger, pTab->tnu
33e78 6d 2c 20 32 2b 63 6e 74 29 3b 0d 0a 20 20 20 20  m, 2+cnt);..    
33e79 20 20 20 20 63 6e 74 2b 2b 3b 0d 0a 20 20 20 20      cnt++;..    
33e7a 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
33e7b 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
33e7c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
33e7d 74 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  t){..          s
33e7e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
33e7f 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
33e80 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e  pIdx->tnum, 2+cn
33e81 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 63  t);..          c
33e82 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 7d  nt++;..        }
33e83 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  ..      }....   
33e84 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
33e85 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65  sufficient numbe
33e86 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 68  r of registers h
33e87 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ave been allocat
33e88 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28  ed */..      if(
33e89 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20   pParse->nMem < 
33e8a 63 6e 74 2b 34 20 29 7b 0d 0a 20 20 20 20 20 20  cnt+4 ){..      
33e8b 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
33e8c 20 63 6e 74 2b 34 3b 0d 0a 20 20 20 20 20 20 7d   cnt+4;..      }
33e8d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20  ....      /* Do 
33e8e 74 68 65 20 62 2d 74 72 65 65 20 69 6e 74 65 67  the b-tree integ
33e8f 72 69 74 79 20 63 68 65 63 6b 73 20 2a 2f 0d 0a  rity checks */..
33e90 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33e91 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
33e92 74 65 67 72 69 74 79 43 6b 2c 20 32 2c 20 63 6e  tegrityCk, 2, cn
33e93 74 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 73 71  t, 1);..      sq
33e94 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
33e95 35 28 76 2c 20 28 75 38 29 69 29 3b 0d 0a 20 20  5(v, (u8)i);..  
33e96 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
33e97 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
33e98 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b 0d 0a  OP_IsNull, 2);..
33e99 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33e9a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
33e9b 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0d  ring8, 0, 3, 0,.
33e9c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
33e9d 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a  3MPrintf(db, "**
33e9e 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73  * in database %s
33e9f 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62   ***\n", db->aDb
33ea0 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0d 0a 20 20 20  [i].zName),..   
33ea1 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43        P4_DYNAMIC
33ea2 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33ea3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
33ea4 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29  P_Move, 2, 4, 1)
33ea5 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
33ea6 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
33ea7 5f 43 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32  _Concat, 4, 3, 2
33ea8 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
33ea9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
33eaa 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20  P_ResultRow, 2, 
33eab 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  1);..      sqlit
33eac 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
33ead 2c 20 61 64 64 72 29 3b 0d 0a 0d 0a 20 20 20 20  , addr);....    
33eae 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
33eaf 6c 6c 20 74 68 65 20 69 6e 64 69 63 65 73 20 61  ll the indices a
33eb0 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 63  re constructed c
33eb1 6f 72 72 65 63 74 6c 79 2e 0d 0a 20 20 20 20 20  orrectly...     
33eb2 20 2a 2f 0d 0a 20 20 20 20 20 20 66 6f 72 28 78   */..      for(x
33eb3 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
33eb4 28 70 54 62 6c 73 29 3b 20 78 20 26 26 20 21 69  (pTbls); x && !i
33eb5 73 51 75 69 63 6b 3b 20 78 3d 73 71 6c 69 74 65  sQuick; x=sqlite
33eb6 48 61 73 68 4e 65 78 74 28 78 29 29 7b 0d 0a 20  HashNext(x)){.. 
33eb7 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54         Table *pT
33eb8 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
33eb9 61 74 61 28 78 29 3b 0d 0a 20 20 20 20 20 20 20  ata(x);..       
33eba 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20   Index *pIdx;.. 
33ebb 20 20 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70 54         int loopT
33ebc 6f 70 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69  op;....        i
33ebd 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  f( pTab->pIndex=
33ebe 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  =0 ) continue;..
33ebf 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
33ec0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
33ec1 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
33ec2 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f 75  ;  /* Stop if ou
33ec3 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0d 0a  t of errors */..
33ec4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33ec5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
33ec6 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  Halt, 0, 0);..  
33ec7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33ec8 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
33ec9 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  r);..        sql
33eca 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
33ecb 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
33ecc 70 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e  pTab, 1, OP_Open
33ecd 52 65 61 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  Read);..        
33ece 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33ecf 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
33ed0 20 30 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28   0, 2);  /* reg(
33ed1 32 29 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e  2) will count en
33ed2 74 72 69 65 73 20 2a 2f 0d 0a 20 20 20 20 20 20  tries */..      
33ed3 20 20 6c 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69    loopTop = sqli
33ed4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33ed5 20 4f 50 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30   OP_Rewind, 1, 0
33ed6 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
33ed7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33ed8 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 32 2c 20 31   OP_AddImm, 2, 1
33ed9 29 3b 20 20 20 2f 2a 20 69 6e 63 72 65 6d 65 6e  );   /* incremen
33eda 74 20 65 6e 74 72 79 20 63 6f 75 6e 74 20 2a 2f  t entry count */
33edb 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ..        for(j=
33edc 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
33edd 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
33ede 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
33edf 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  +){..          i
33ee0 6e 74 20 6a 6d 70 32 3b 0d 0a 20 20 20 20 20 20  nt jmp2;..      
33ee1 20 20 20 20 69 6e 74 20 72 31 3b 0d 0a 20 20 20      int r1;..   
33ee2 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
33ee3 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 69  nst VdbeOpList i
33ee4 64 78 45 72 72 5b 5d 20 3d 20 7b 0d 0a 20 20 20  dxErr[] = {..   
33ee5 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64           { OP_Ad
33ee6 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c 20 2d 31  dImm,      1, -1
33ee7 2c 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20 20 20  ,  0},..        
33ee8 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
33ee9 2c 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d  ,     0,  3,  0}
33eea 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0d 0a 20 20  ,    /* 1 */..  
33eeb 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52            { OP_R
33eec 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c 20 20  owid,       1,  
33eed 34 2c 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20 20  4,  0},..       
33eee 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
33eef 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20 30  8,     0,  5,  0
33ef0 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0d 0a 20  },    /* 3 */.. 
33ef1 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
33ef2 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20  String8,     0, 
33ef3 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34   6,  0},    /* 4
33ef4 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20   */..           
33ef5 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20   { OP_Concat,   
33ef6 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0d 0a     4,  3,  3},..
33ef7 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
33ef8 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c  _Concat,      5,
33ef9 20 20 33 2c 20 20 33 7d 2c 0d 0a 20 20 20 20 20    3,  3},..     
33efa 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
33efb 61 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20  at,      6,  3, 
33efc 20 33 7d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20   3},..          
33efd 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
33efe 2c 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0d  ,   3,  1,  0},.
33eff 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
33f00 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31  P_IfPos,       1
33f01 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  0,  0},    /*
33f02 20 39 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20   9 */..         
33f03 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20     { OP_Halt,   
33f04 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c       0,  0,  0},
33f05 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0d 0a  ..          };..
33f06 20 20 20 20 20 20 20 20 20 20 72 31 20 3d 20 73            r1 = s
33f07 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
33f08 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70  dexKey(pParse, p
33f09 49 64 78 2c 20 31 2c 20 33 2c 20 30 29 3b 0d 0a  Idx, 1, 3, 0);..
33f0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32 20 3d            jmp2 =
33f0b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33f0c 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e  p4Int(v, OP_Foun
33f0d 64 2c 20 6a 2b 32 2c 20 30 2c 20 72 31 2c 20 70  d, j+2, 0, r1, p
33f0e 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
33f0f 0d 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72  ..          addr
33f10 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
33f11 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
33f12 53 69 7a 65 28 69 64 78 45 72 72 29 2c 20 69 64  Size(idxErr), id
33f13 78 45 72 72 29 3b 0d 0a 20 20 20 20 20 20 20 20  xErr);..        
33f14 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
33f15 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 31 2c  ngeP4(v, addr+1,
33f16 20 22 72 6f 77 69 64 20 22 2c 20 50 34 5f 53 54   "rowid ", P4_ST
33f17 41 54 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20  ATIC);..        
33f18 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
33f19 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 33 2c  ngeP4(v, addr+3,
33f1a 20 22 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20   " missing from 
33f1b 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41 54  index ", P4_STAT
33f1c 49 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  IC);..          
33f1d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
33f1e 65 50 34 28 76 2c 20 61 64 64 72 2b 34 2c 20 70  eP4(v, addr+4, p
33f1f 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54  Idx->zName, P4_T
33f20 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20 20 20  RANSIENT);..    
33f21 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33f22 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
33f23 72 2b 39 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  r+9);..         
33f24 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
33f25 48 65 72 65 28 76 2c 20 6a 6d 70 32 29 3b 0d 0a  Here(v, jmp2);..
33f26 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
33f27 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
33f28 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
33f29 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0d   1, loopTop+1);.
33f2a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
33f2b 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
33f2c 6c 6f 6f 70 54 6f 70 29 3b 0d 0a 20 20 20 20 20  loopTop);..     
33f2d 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
33f2e 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
33f2f 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
33f30 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0d 0a 20 20  pNext, j++){..  
33f31 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63          static c
33f32 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
33f33 63 6e 74 49 64 78 5b 5d 20 3d 20 7b 0d 0a 20 20  cntIdx[] = {..  
33f34 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
33f35 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c  Integer,      0,
33f36 20 20 33 2c 20 20 30 7d 2c 0d 0a 20 20 20 20 20    3,  0},..     
33f37 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 77          { OP_Rew
33f38 69 6e 64 2c 20 20 20 20 20 20 20 30 2c 20 20 30  ind,       0,  0
33f39 2c 20 20 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0d  ,  0},  /* 1 */.
33f3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
33f3b 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20  OP_AddImm,      
33f3c 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0d 0a 20 20   3,  1,  0},..  
33f3d 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
33f3e 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 30 2c  Next,         0,
33f3f 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 33 20    0,  0},  /* 3 
33f40 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
33f41 20 7b 20 4f 50 5f 45 71 2c 20 20 20 20 20 20 20   { OP_Eq,       
33f42 20 20 20 20 32 2c 20 20 30 2c 20 20 33 7d 2c 20      2,  0,  3}, 
33f43 20 2f 2a 20 34 20 2a 2f 0d 0a 20 20 20 20 20 20   /* 4 */..      
33f44 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49         { OP_AddI
33f45 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20 2d 31 2c  mm,       1, -1,
33f46 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20 20 20 20    0},..         
33f47 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
33f48 2c 20 20 20 20 20 20 30 2c 20 20 32 2c 20 20 30  ,      0,  2,  0
33f49 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0d 0a 20 20 20  },  /* 6 */..   
33f4a 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
33f4b 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20  tring8,      0, 
33f4c 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20 2a   3,  0},  /* 7 *
33f4d 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  /..             
33f4e 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
33f4f 20 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0d 0a     3,  2,  2},..
33f50 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
33f51 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20  P_ResultRow,    
33f52 32 2c 20 20 31 2c 20 20 30 7d 2c 0d 0a 20 20 20  2,  1,  0},..   
33f53 20 20 20 20 20 20 20 7d 3b 0d 0a 20 20 20 20 20         };..     
33f54 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
33f55 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
33f56 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 0d 0a   OP_IfPos, 1);..
33f57 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33f58 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
33f59 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0d 0a  P_Halt, 0, 0);..
33f5a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33f5b 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
33f5c 20 61 64 64 72 29 3b 0d 0a 20 20 20 20 20 20 20   addr);..       
33f5d 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
33f5e 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
33f5f 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e 74 49  , ArraySize(cntI
33f60 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0d 0a 20  dx), cntIdx);.. 
33f61 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
33f62 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
33f63 61 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0d 0a 20  addr+1, j+2);.. 
33f64 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
33f65 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
33f66 61 64 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b  addr+1, addr+4);
33f67 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
33f68 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
33f69 76 2c 20 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b  v, addr+3, j+2);
33f6a 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
33f6b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
33f6c 76 2c 20 61 64 64 72 2b 33 2c 20 61 64 64 72 2b  v, addr+3, addr+
33f6d 32 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73  2);..          s
33f6e 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
33f6f 72 65 28 76 2c 20 61 64 64 72 2b 34 29 3b 0d 0a  re(v, addr+4);..
33f70 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33f71 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
33f72 20 61 64 64 72 2b 36 2c 20 0d 0a 20 20 20 20 20   addr+6, ..     
33f73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f74 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e 74 72  "wrong # of entr
33f75 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22 2c 20  ies in index ", 
33f76 50 34 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20  P4_STATIC);..   
33f77 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33f78 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
33f79 64 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  dr+7, pIdx->zNam
33f7a 65 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29  e, P4_TRANSIENT)
33f7b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
33f7c 20 20 20 20 7d 20 0d 0a 20 20 20 20 7d 0d 0a 20      } ..    }.. 
33f7d 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
33f7e 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
33f7f 2c 20 41 72 72 61 79 53 69 7a 65 28 65 6e 64 43  , ArraySize(endC
33f80 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29 3b 0d  ode), endCode);.
33f81 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
33f82 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
33f83 2c 20 2d 6d 78 45 72 72 29 3b 0d 0a 20 20 20 20  , -mxErr);..    
33f84 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
33f85 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b 0d  ere(v, addr+1);.
33f86 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
33f87 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
33f88 2b 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41  +2, "ok", P4_STA
33f89 54 49 43 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a  TIC);..  }else..
33f8a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
33f8b 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
33f8c 43 48 45 43 4b 20 2a 2f 0d 0a 0d 0a 23 69 66 6e  CHECK */....#ifn
33f8d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33f8e 55 54 46 31 36 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  UTF16..  /*..  *
33f8f 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64  *   PRAGMA encod
33f90 69 6e 67 0d 0a 20 20 2a 2a 20 20 20 50 52 41 47  ing..  **   PRAG
33f91 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75  MA encoding = "u
33f92 74 66 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22  tf-8"|"utf-16"|"
33f93 75 74 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31  utf-16le"|"utf-1
33f94 36 62 65 22 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  6be"..  **..  **
33f95 20 49 6e 20 69 74 73 20 66 69 72 73 74 20 66 6f   In its first fo
33f96 72 6d 2c 20 74 68 69 73 20 70 72 61 67 6d 61 20  rm, this pragma 
33f97 72 65 74 75 72 6e 73 20 74 68 65 20 65 6e 63 6f  returns the enco
33f98 64 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e  ding of the main
33f99 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  ..  ** database.
33f9a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
33f9b 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69   is not initiali
33f9c 7a 65 64 2c 20 69 74 20 69 73 20 69 6e 69 74 69  zed, it is initi
33f9d 61 6c 69 7a 65 64 20 6e 6f 77 2e 0d 0a 20 20 2a  alized now...  *
33f9e 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  *..  ** The seco
33f9f 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68 69 73 20  nd form of this 
33fa0 70 72 61 67 6d 61 20 69 73 20 61 20 6e 6f 2d 6f  pragma is a no-o
33fa1 70 20 69 66 20 74 68 65 20 6d 61 69 6e 20 64 61  p if the main da
33fa2 74 61 62 61 73 65 20 66 69 6c 65 0d 0a 20 20 2a  tabase file..  *
33fa3 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64  * has not alread
33fa4 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  y been initializ
33fa5 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
33fa6 20 69 74 20 73 65 74 73 20 74 68 65 20 64 65 66   it sets the def
33fa7 61 75 6c 74 0d 0a 20 20 2a 2a 20 65 6e 63 6f 64  ault..  ** encod
33fa8 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ing that will be
33fa9 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61   used for the ma
33faa 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
33fab 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65 0d 0a   if a new file..
33fac 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64 2e    ** is created.
33fad 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   If an existing 
33fae 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
33faf 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74 68  le is opened, th
33fb0 65 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 64 65 66  en the..  ** def
33fb1 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69  ault text encodi
33fb2 6e 67 20 66 6f 72 20 74 68 65 20 65 78 69 73 74  ng for the exist
33fb3 69 6e 67 20 64 61 74 61 62 61 73 65 20 69 73 20  ing database is 
33fb4 75 73 65 64 2e 0d 0a 20 20 2a 2a 20 0d 0a 20 20  used...  ** ..  
33fb5 2a 2a 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 20  ** In all cases 
33fb6 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 63 72  new databases cr
33fb7 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20  eated using the 
33fb8 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61  ATTACH command a
33fb9 72 65 0d 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  re..  ** created
33fba 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65   to use the same
33fbb 20 64 65 66 61 75 6c 74 20 74 65 78 74 20 65 6e   default text en
33fbc 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61  coding as the ma
33fbd 69 6e 20 64 61 74 61 62 61 73 65 2e 20 49 66 0d  in database. If.
33fbe 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  .  ** the main d
33fbf 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20  atabase has not 
33fc0 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
33fc1 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 20   and/or created 
33fc2 77 68 65 6e 20 41 54 54 41 43 48 0d 0a 20 20 2a  when ATTACH..  *
33fc3 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  * is executed, t
33fc4 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f  his is done befo
33fc5 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f 70  re the ATTACH op
33fc6 65 72 61 74 69 6f 6e 2e 0d 0a 20 20 2a 2a 0d 0a  eration...  **..
33fc7 20 20 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f    ** In the seco
33fc8 6e 64 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61  nd form this pra
33fc9 67 6d 61 20 73 65 74 73 20 74 68 65 20 74 65 78  gma sets the tex
33fca 74 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65  t encoding to be
33fcb 20 75 73 65 64 20 69 6e 0d 0a 20 20 2a 2a 20 6e   used in..  ** n
33fcc 65 77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ew database file
33fcd 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
33fce 74 68 69 73 20 64 61 74 61 62 61 73 65 20 68 61  this database ha
33fcf 6e 64 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79  ndle. It is only
33fd0 0d 0a 20 20 2a 2a 20 75 73 65 66 75 6c 20 69 66  ..  ** useful if
33fd1 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61   invoked immedia
33fd2 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6d  tely after the m
33fd3 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 0d 0a  ain database i..
33fd4 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69    */..  if( sqli
33fd5 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
33fd6 2c 20 22 65 6e 63 6f 64 69 6e 67 22 29 3d 3d 30  , "encoding")==0
33fd7 20 29 7b 0d 0a 20 20 20 20 73 74 61 74 69 63 20   ){..    static 
33fd8 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63  const struct Enc
33fd9 4e 61 6d 65 20 7b 0d 0a 20 20 20 20 20 20 63 68  Name {..      ch
33fda 61 72 20 2a 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20  ar *zName;..    
33fdb 20 20 75 38 20 65 6e 63 3b 0d 0a 20 20 20 20 7d    u8 enc;..    }
33fdc 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0d   encnames[] = {.
33fdd 0a 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c  .      { "UTF8",
33fde 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38       SQLITE_UTF8
33fdf 20 20 20 20 20 20 20 20 7d 2c 0d 0a 20 20 20 20          },..    
33fe0 20 20 7b 20 22 55 54 46 2d 38 22 2c 20 20 20 20    { "UTF-8",    
33fe1 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20  SQLITE_UTF8     
33fe2 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62     },  /* Must b
33fe3 65 20 65 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f  e element [1] */
33fe4 0d 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31  ..      { "UTF-1
33fe5 36 6c 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  6le", SQLITE_UTF
33fe6 31 36 4c 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20  16LE     },  /* 
33fe7 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20  Must be element 
33fe8 5b 32 5d 20 2a 2f 0d 0a 20 20 20 20 20 20 7b 20  [2] */..      { 
33fe9 22 55 54 46 2d 31 36 62 65 22 2c 20 53 51 4c 49  "UTF-16be", SQLI
33fea 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 7d  TE_UTF16BE     }
33feb 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c  ,  /* Must be el
33fec 65 6d 65 6e 74 20 5b 33 5d 20 2a 2f 0d 0a 20 20  ement [3] */..  
33fed 20 20 20 20 7b 20 22 55 54 46 31 36 6c 65 22 2c      { "UTF16le",
33fee 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45    SQLITE_UTF16LE
33fef 20 20 20 20 20 7d 2c 0d 0a 20 20 20 20 20 20 7b       },..      {
33ff0 20 22 55 54 46 31 36 62 65 22 2c 20 20 53 51 4c   "UTF16be",  SQL
33ff1 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
33ff2 7d 2c 0d 0a 20 20 20 20 20 20 7b 20 22 55 54 46  },..      { "UTF
33ff3 2d 31 36 22 2c 20 20 20 30 20 20 20 20 20 20 20  -16",   0       
33ff4 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a             }, /*
33ff5 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
33ff6 49 56 45 20 2a 2f 0d 0a 20 20 20 20 20 20 7b 20  IVE */..      { 
33ff7 22 55 54 46 31 36 22 2c 20 20 20 20 30 20 20 20  "UTF16",    0   
33ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
33ff9 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31  , /* SQLITE_UTF1
33ffa 36 4e 41 54 49 56 45 20 2a 2f 0d 0a 20 20 20 20  6NATIVE */..    
33ffb 20 20 7b 20 30 2c 20 30 20 7d 0d 0a 20 20 20 20    { 0, 0 }..    
33ffc 7d 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 73 74  };..    const st
33ffd 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 2a 70 45  ruct EncName *pE
33ffe 6e 63 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a 52  nc;..    if( !zR
33fff 69 67 68 74 20 29 7b 20 20 20 20 2f 2a 20 22 50  ight ){    /* "P
34000 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 22 20  RAGMA encoding" 
34001 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71  */..      if( sq
34002 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
34003 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
34004 72 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20 20 20  ragma_out;..    
34005 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
34006 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0d 0a  NumCols(v, 1);..
34007 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
34008 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
34009 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
3400a 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c 49  "encoding", SQLI
3400b 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20  TE_STATIC);..   
3400c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3400d 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp2(v, OP_Strin
3400e 67 38 2c 20 30 2c 20 31 29 3b 0d 0a 20 20 20 20  g8, 0, 1);..    
3400f 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d    assert( encnam
34010 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2e  es[SQLITE_UTF8].
34011 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
34012 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
34013 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49  t( encnames[SQLI
34014 54 45 5f 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d  TE_UTF16LE].enc=
34015 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
34016 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
34017 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54  ( encnames[SQLIT
34018 45 5f 55 54 46 31 36 42 45 5d 2e 65 6e 63 3d 3d  E_UTF16BE].enc==
34019 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
3401a 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3401b 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
3401c 2d 31 2c 20 65 6e 63 6e 61 6d 65 73 5b 45 4e 43  -1, encnames[ENC
3401d 28 70 50 61 72 73 65 2d 3e 64 62 29 5d 2e 7a 4e  (pParse->db)].zN
3401e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
3401f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
34020 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
34021 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
34022 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 20 20 20  ;..    }else{   
34023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34024 20 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20       /* "PRAGMA 
34025 65 6e 63 6f 64 69 6e 67 20 3d 20 58 58 58 22 20  encoding = XXX" 
34026 2a 2f 0d 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c  */..      /* Onl
34027 79 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  y change the val
34028 75 65 20 6f 66 20 73 71 6c 69 74 65 2e 65 6e 63  ue of sqlite.enc
34029 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
3402a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 0d 0a   handle is not..
3402b 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c        ** initial
3402c 69 7a 65 64 2e 20 49 66 20 74 68 65 20 6d 61 69  ized. If the mai
3402d 6e 20 64 61 74 61 62 61 73 65 20 65 78 69 73 74  n database exist
3402e 73 2c 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74  s, the new sqlit
3402f 65 2e 65 6e 63 20 76 61 6c 75 65 0d 0a 20 20 20  e.enc value..   
34030 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 6f 76     ** will be ov
34031 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 20 74  erwritten when t
34032 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 65 78  he schema is nex
34033 74 20 6c 6f 61 64 65 64 2e 20 49 66 20 69 74 20  t loaded. If it 
34034 64 6f 65 73 20 6e 6f 74 0d 0a 20 20 20 20 20 20  does not..      
34035 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ** already exist
34036 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 63 72  s, it will be cr
34037 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68 65  eated to use the
34038 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20 76 61   new encoding va
34039 6c 75 65 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  lue...      */..
3403a 20 20 20 20 20 20 69 66 28 20 0d 0a 20 20 20 20        if( ..    
3403b 20 20 20 20 21 28 44 62 48 61 73 50 72 6f 70 65      !(DbHasPrope
3403c 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63  rty(db, 0, DB_Sc
3403d 68 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20  hemaLoaded)) || 
3403e 0d 0a 20 20 20 20 20 20 20 20 44 62 48 61 73 50  ..        DbHasP
3403f 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44  roperty(db, 0, D
34040 42 5f 45 6d 70 74 79 29 20 0d 0a 20 20 20 20 20  B_Empty) ..     
34041 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72   ){..        for
34042 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b  (pEnc=&encnames[
34043 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b  0]; pEnc->zName;
34044 20 70 45 6e 63 2b 2b 29 7b 0d 0a 20 20 20 20 20   pEnc++){..     
34045 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69       if( 0==sqli
34046 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68  te3StrICmp(zRigh
34047 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20  t, pEnc->zName) 
34048 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
34049 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20  ENC(pParse->db) 
3404a 3d 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45  = pEnc->enc ? pE
3404b 6e 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45  nc->enc : SQLITE
3404c 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0d 0a 20  _UTF16NATIVE;.. 
3404d 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3404e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
3404f 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
34050 20 20 20 69 66 28 20 21 70 45 6e 63 2d 3e 7a 4e     if( !pEnc->zN
34051 61 6d 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ame ){..        
34052 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
34053 67 28 70 50 61 72 73 65 2c 20 22 75 6e 73 75 70  g(pParse, "unsup
34054 70 6f 72 74 65 64 20 65 6e 63 6f 64 69 6e 67 3a  ported encoding:
34055 20 25 73 22 2c 20 7a 52 69 67 68 74 29 3b 0d 0a   %s", zRight);..
34056 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
34057 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c   }..    }..  }el
34058 73 65 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  se..#endif /* SQ
34059 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
3405a 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  */....#ifndef SQ
3405b 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41  LITE_OMIT_SCHEMA
3405c 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53  _VERSION_PRAGMAS
3405d 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 20 20 50  ..  /*..  **   P
3405e 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
3405f 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0d  ]schema_version.
34060 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
34061 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61  database.]schema
34062 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65  _version = <inte
34063 67 65 72 3e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ger>..  **..  **
34064 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
34065 61 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f  ase.]user_versio
34066 6e 0d 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  n..  **   PRAGMA
34067 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72   [database.]user
34068 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65  _version = <inte
34069 67 65 72 3e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ger>..  **..  **
3406a 20 54 68 65 20 70 72 61 67 6d 61 27 73 20 73 63   The pragma's sc
3406b 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 61 6e 64  hema_version and
3406c 20 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 61 72   user_version ar
3406d 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 6f 72  e used to set or
3406e 20 67 65 74 0d 0a 20 20 2a 2a 20 74 68 65 20 76   get..  ** the v
3406f 61 6c 75 65 20 6f 66 20 74 68 65 20 73 63 68 65  alue of the sche
34070 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 75  ma-version and u
34071 73 65 72 2d 76 65 72 73 69 6f 6e 2c 20 72 65 73  ser-version, res
34072 70 65 63 74 69 76 65 6c 79 2e 20 42 6f 74 68 0d  pectively. Both.
34073 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
34074 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 74 68 65  -version and the
34075 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 61 72   user-version ar
34076 65 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20  e 32-bit signed 
34077 69 6e 74 65 67 65 72 73 0d 0a 20 20 2a 2a 20 73  integers..  ** s
34078 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74  tored in the dat
34079 61 62 61 73 65 20 68 65 61 64 65 72 2e 0d 0a 20  abase header... 
3407a 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 73 63   **..  ** The sc
3407b 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75  hema-cookie is u
3407c 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69  sually only mani
3407d 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e 61 6c  pulated internal
3407e 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74  ly by SQLite. It
3407f 0d 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d  ..  ** is increm
34080 65 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65 20  ented by SQLite 
34081 77 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74  whenever the dat
34082 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20  abase schema is 
34083 6d 6f 64 69 66 69 65 64 20 28 62 79 0d 0a 20 20  modified (by..  
34084 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64  ** creating or d
34085 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20  ropping a table 
34086 6f 72 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73  or index). The s
34087 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73  chema version is
34088 20 75 73 65 64 20 62 79 0d 0a 20 20 2a 2a 20 53   used by..  ** S
34089 51 4c 69 74 65 20 65 61 63 68 20 74 69 6d 65 20  QLite each time 
3408a 61 20 71 75 65 72 79 20 69 73 20 65 78 65 63 75  a query is execu
3408b 74 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68  ted to ensure th
3408c 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  at the internal 
3408d 63 61 63 68 65 0d 0a 20 20 2a 2a 20 6f 66 20 74  cache..  ** of t
3408e 68 65 20 73 63 68 65 6d 61 20 75 73 65 64 20 77  he schema used w
3408f 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68  hen compiling th
34090 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61 74 63  e SQL query matc
34091 68 65 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f  hes the schema o
34092 66 0d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  f..  ** the data
34093 62 61 73 65 20 61 67 61 69 6e 73 74 20 77 68 69  base against whi
34094 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20  ch the compiled 
34095 71 75 65 72 79 20 69 73 20 61 63 74 75 61 6c 6c  query is actuall
34096 79 20 65 78 65 63 75 74 65 64 2e 0d 0a 20 20 2a  y executed...  *
34097 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68 69  * Subverting thi
34098 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75  s mechanism by u
34099 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68  sing "PRAGMA sch
3409a 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20  ema_version" to 
3409b 6d 6f 64 69 66 79 0d 0a 20 20 2a 2a 20 74 68 65  modify..  ** the
3409c 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
3409d 69 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64  is potentially d
3409e 61 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79  angerous and may
3409f 20 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d   lead to program
340a0 0d 0a 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f  ..  ** crashes o
340a1 72 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  r database corru
340a2 70 74 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20  ption. Use with 
340a3 63 61 75 74 69 6f 6e 21 0d 0a 20 20 2a 2a 0d 0a  caution!..  **..
340a4 20 20 2a 2a 20 54 68 65 20 75 73 65 72 2d 76 65    ** The user-ve
340a5 72 73 69 6f 6e 20 69 73 20 6e 6f 74 20 75 73 65  rsion is not use
340a6 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  d internally by 
340a7 53 51 4c 69 74 65 2e 20 49 74 20 6d 61 79 20 62  SQLite. It may b
340a8 65 20 75 73 65 64 20 62 79 0d 0a 20 20 2a 2a 20  e used by..  ** 
340a9 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 66 6f 72  applications for
340aa 20 61 6e 79 20 70 75 72 70 6f 73 65 2e 0d 0a 20   any purpose... 
340ab 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74   */..  if( sqlit
340ac 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
340ad 20 22 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e   "schema_version
340ae 22 29 3d 3d 30 20 0d 0a 20 20 20 7c 7c 20 73 71  ")==0 ..   || sq
340af 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
340b0 66 74 2c 20 22 75 73 65 72 5f 76 65 72 73 69 6f  ft, "user_versio
340b1 6e 22 29 3d 3d 30 20 0d 0a 20 20 20 7c 7c 20 73  n")==0 ..   || s
340b2 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
340b3 65 66 74 2c 20 22 66 72 65 65 6c 69 73 74 5f 63  eft, "freelist_c
340b4 6f 75 6e 74 22 29 3d 3d 30 20 0d 0a 20 20 29 7b  ount")==0 ..  ){
340b5 0d 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69  ..    int iCooki
340b6 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69  e;   /* Cookie i
340b7 6e 64 65 78 2e 20 31 20 66 6f 72 20 73 63 68 65  ndex. 1 for sche
340b8 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72  ma-cookie, 6 for
340b9 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f   user-cookie. */
340ba 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
340bb 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
340bc 62 29 3b 0d 0a 20 20 20 20 73 77 69 74 63 68 28  b);..    switch(
340bd 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0d 0a 20 20   zLeft[0] ){..  
340be 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 63 61      case 'f': ca
340bf 73 65 20 27 46 27 3a 0d 0a 20 20 20 20 20 20 20  se 'F':..       
340c0 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45   iCookie = BTREE
340c1 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54  _FREE_PAGE_COUNT
340c2 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;..        break
340c3 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 27 73  ;..      case 's
340c4 27 3a 20 63 61 73 65 20 27 53 27 3a 0d 0a 20 20  ': case 'S':..  
340c5 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20        iCookie = 
340c6 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52  BTREE_SCHEMA_VER
340c7 53 49 4f 4e 3b 0d 0a 20 20 20 20 20 20 20 20 62  SION;..        b
340c8 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 64 65 66  reak;..      def
340c9 61 75 6c 74 3a 0d 0a 20 20 20 20 20 20 20 20 69  ault:..        i
340ca 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f 55  Cookie = BTREE_U
340cb 53 45 52 5f 56 45 52 53 49 4f 4e 3b 0d 0a 20 20  SER_VERSION;..  
340cc 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
340cd 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 7a    }....    if( z
340ce 52 69 67 68 74 20 26 26 20 69 43 6f 6f 6b 69 65  Right && iCookie
340cf 21 3d 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47  !=BTREE_FREE_PAG
340d0 45 5f 43 4f 55 4e 54 20 29 7b 0d 0a 20 20 20 20  E_COUNT ){..    
340d1 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73    /* Write the s
340d2 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20  pecified cookie 
340d3 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 20 20 20 20  value */..      
340d4 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
340d5 65 4f 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69  eOpList setCooki
340d6 65 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20  e[] = {..       
340d7 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
340d8 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d  n,    0,  1,  0}
340d9 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0d 0a 20 20  ,    /* 0 */..  
340da 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67        { OP_Integ
340db 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20 31  er,        0,  1
340dc 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a  ,  0},    /* 1 *
340dd 2f 0d 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  /..        { OP_
340de 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20  SetCookie,      
340df 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20 20 20 2f  0,  0,  1},    /
340e0 2a 20 32 20 2a 2f 0d 0a 20 20 20 20 20 20 7d 3b  * 2 */..      };
340e1 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ..      int addr
340e2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
340e3 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
340e4 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29 2c  Size(setCookie),
340e5 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0d 0a 20 20   setCookie);..  
340e6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
340e7 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
340e8 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 73 71   iDb);..      sq
340e9 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
340ea 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 73 71 6c  1(v, addr+1, sql
340eb 69 74 65 33 41 74 6f 69 28 7a 52 69 67 68 74 29  ite3Atoi(zRight)
340ec 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
340ed 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
340ee 20 61 64 64 72 2b 32 2c 20 69 44 62 29 3b 0d 0a   addr+2, iDb);..
340ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
340f0 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64  eChangeP2(v, add
340f1 72 2b 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0d 0a  r+2, iCookie);..
340f2 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
340f3 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 70    /* Read the sp
340f4 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76  ecified cookie v
340f5 61 6c 75 65 20 2a 2f 0d 0a 20 20 20 20 20 20 73  alue */..      s
340f6 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
340f7 4f 70 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b 69  OpList readCooki
340f8 65 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20  e[] = {..       
340f9 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
340fa 6e 2c 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30  n,     0,  0,  0
340fb 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0d 0a 20  },    /* 0 */.. 
340fc 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64         { OP_Read
340fd 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20  Cookie,      0, 
340fe 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31   1,  0},    /* 1
340ff 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7b 20 4f   */..        { O
34100 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20  P_ResultRow,    
34101 20 20 20 31 2c 20 20 31 2c 20 20 30 7d 0d 0a 20     1,  1,  0}.. 
34102 20 20 20 20 20 7d 3b 0d 0a 20 20 20 20 20 20 69       };..      i
34103 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  nt addr = sqlite
34104 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
34105 2c 20 41 72 72 61 79 53 69 7a 65 28 72 65 61 64  , ArraySize(read
34106 43 6f 6f 6b 69 65 29 2c 20 72 65 61 64 43 6f 6f  Cookie), readCoo
34107 6b 69 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  kie);..      sql
34108 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
34109 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0d  (v, addr, iDb);.
3410a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3410b 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
3410c 64 72 2b 31 2c 20 69 44 62 29 3b 0d 0a 20 20 20  dr+1, iDb);..   
3410d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
3410e 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72 2b 31  angeP3(v, addr+1
3410f 2c 20 69 43 6f 6f 6b 69 65 29 3b 0d 0a 20 20 20  , iCookie);..   
34110 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
34111 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0d  tNumCols(v, 1);.
34112 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
34113 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
34114 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
34115 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54   zLeft, SQLITE_T
34116 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20 20 20  RANSIENT);..    
34117 7d 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64  }..  }else..#end
34118 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
34119 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  T_SCHEMA_VERSION
3411a 5f 50 52 41 47 4d 41 53 20 2a 2f 0d 0a 0d 0a 23  _PRAGMAS */....#
3411b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3411c 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e  IT_COMPILEOPTION
3411d 5f 44 49 41 47 53 0d 0a 20 20 2f 2a 0d 0a 20 20  _DIAGS..  /*..  
3411e 2a 2a 20 20 20 50 52 41 47 4d 41 20 63 6f 6d 70  **   PRAGMA comp
3411f 69 6c 65 5f 6f 70 74 69 6f 6e 73 0d 0a 20 20 2a  ile_options..  *
34120 2a 0d 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74  *..  ** Return t
34121 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20  he names of all 
34122 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
34123 69 6f 6e 73 20 75 73 65 64 20 69 6e 20 74 68 69  ions used in thi
34124 73 20 62 75 69 6c 64 2c 0d 0a 20 20 2a 2a 20 6f  s build,..  ** o
34125 6e 65 20 6f 70 74 69 6f 6e 20 70 65 72 20 72 6f  ne option per ro
34126 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  w...  */..  if( 
34127 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
34128 4c 65 66 74 2c 20 22 63 6f 6d 70 69 6c 65 5f 6f  Left, "compile_o
34129 70 74 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0d 0a  ptions")==0 ){..
3412a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0d 0a      int i = 0;..
3412b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
3412c 7a 4f 70 74 3b 0d 0a 20 20 20 20 73 71 6c 69 74  zOpt;..    sqlit
3412d 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
3412e 28 76 2c 20 31 29 3b 0d 0a 20 20 20 20 70 50 61  (v, 1);..    pPa
3412f 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 31 3b 0d 0a  rse->nMem = 1;..
34130 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
34131 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
34132 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63  COLNAME_NAME, "c
34133 6f 6d 70 69 6c 65 5f 6f 70 74 69 6f 6e 22 2c 20  ompile_option", 
34134 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d  SQLITE_STATIC);.
34135 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4f 70  .    while( (zOp
34136 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  t = sqlite3_comp
34137 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 69 2b  ileoption_get(i+
34138 2b 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  +))!=0 ){..     
34139 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3413a 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
3413b 2c 20 30 2c 20 31 2c 20 30 2c 20 7a 4f 70 74 2c  , 0, 1, 0, zOpt,
3413c 20 30 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69   0);..      sqli
3413d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3413e 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
3413f 2c 20 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  , 1);..    }..  
34140 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 20 2f 2a  }else..#endif /*
34141 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
34142 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53  PILEOPTION_DIAGS
34143 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   */....#ifndef S
34144 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a  QLITE_OMIT_WAL..
34145 20 20 2f 2a 0d 0a 20 20 2a 2a 20 20 20 50 52 41    /*..  **   PRA
34146 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 77  GMA [database.]w
34147 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20 3d 20  al_checkpoint = 
34148 70 61 73 73 69 76 65 7c 66 75 6c 6c 7c 72 65 73  passive|full|res
34149 74 61 72 74 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  tart..  **..  **
3414a 20 43 68 65 63 6b 70 6f 69 6e 74 20 74 68 65 20   Checkpoint the 
3414b 64 61 74 61 62 61 73 65 2e 0d 0a 20 20 2a 2f 0d  database...  */.
3414c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
3414d 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 77 61  rICmp(zLeft, "wa
3414e 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 22 29 3d 3d  l_checkpoint")==
3414f 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 42  0 ){..    int iB
34150 74 20 3d 20 28 70 49 64 32 2d 3e 7a 3f 69 44 62  t = (pId2->z?iDb
34151 3a 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  :SQLITE_MAX_ATTA
34152 43 48 45 44 29 3b 0d 0a 20 20 20 20 69 6e 74 20  CHED);..    int 
34153 65 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43  eMode = SQLITE_C
34154 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56  HECKPOINT_PASSIV
34155 45 3b 0d 0a 20 20 20 20 69 66 28 20 7a 52 69 67  E;..    if( zRig
34156 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  ht ){..      if(
34157 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
34158 7a 52 69 67 68 74 2c 20 22 66 75 6c 6c 22 29 3d  zRight, "full")=
34159 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 65  =0 ){..        e
3415a 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 48  Mode = SQLITE_CH
3415b 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 3b 0d 0a  ECKPOINT_FULL;..
3415c 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
3415d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
3415e 52 69 67 68 74 2c 20 22 72 65 73 74 61 72 74 22  Right, "restart"
3415f 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )==0 ){..       
34160 20 65 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f   eMode = SQLITE_
34161 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41  CHECKPOINT_RESTA
34162 52 54 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  RT;..      }..  
34163 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73 71 6c    }..    if( sql
34164 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
34165 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
34166 61 67 6d 61 5f 6f 75 74 3b 0d 0a 20 20 20 20 73  agma_out;..    s
34167 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
34168 43 6f 6c 73 28 76 2c 20 33 29 3b 0d 0a 20 20 20  Cols(v, 3);..   
34169 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
3416a 33 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  3;..    sqlite3V
3416b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
3416c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
3416d 2c 20 22 62 75 73 79 22 2c 20 53 51 4c 49 54 45  , "busy", SQLITE
3416e 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 73  _STATIC);..    s
3416f 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
34170 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
34171 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 67 22 2c 20  ME_NAME, "log", 
34172 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d  SQLITE_STATIC);.
34173 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
34174 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c  SetColName(v, 2,
34175 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
34176 63 68 65 63 6b 70 6f 69 6e 74 65 64 22 2c 20 53  checkpointed", S
34177 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
34178 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
34179 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 68  eAddOp3(v, OP_Ch
3417a 65 63 6b 70 6f 69 6e 74 2c 20 69 42 74 2c 20 65  eckpoint, iBt, e
3417b 4d 6f 64 65 2c 20 31 29 3b 0d 0a 20 20 20 20 73  Mode, 1);..    s
3417c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3417d 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
3417e 2c 20 31 2c 20 33 29 3b 0d 0a 20 20 7d 65 6c 73  , 1, 3);..  }els
3417f 65 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  e....  /*..  ** 
34180 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 61 75 74    PRAGMA wal_aut
34181 6f 63 68 65 63 6b 70 6f 69 6e 74 0d 0a 20 20 2a  ocheckpoint..  *
34182 2a 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 61  *   PRAGMA wal_a
34183 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 20 3d 20  utocheckpoint = 
34184 4e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 43 6f  N..  **..  ** Co
34185 6e 66 69 67 75 72 65 20 61 20 64 61 74 61 62 61  nfigure a databa
34186 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  se connection to
34187 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
34188 68 65 63 6b 70 6f 69 6e 74 20 61 20 64 61 74 61  heckpoint a data
34189 62 61 73 65 0d 0a 20 20 2a 2a 20 61 66 74 65 72  base..  ** after
3418a 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 4e 20   accumulating N 
3418b 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f  frames in the lo
3418c 67 2e 20 4f 72 20 71 75 65 72 79 20 66 6f 72 20  g. Or query for 
3418d 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
3418e 65 0d 0a 20 20 2a 2a 20 6f 66 20 4e 2e 0d 0a 20  e..  ** of N... 
3418f 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74   */..  if( sqlit
34190 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
34191 20 22 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70   "wal_autocheckp
34192 6f 69 6e 74 22 29 3d 3d 30 20 29 7b 0d 0a 20 20  oint")==0 ){..  
34193 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0d    if( zRight ){.
34194 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 77  .      sqlite3_w
34195 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
34196 74 28 64 62 2c 20 73 71 6c 69 74 65 33 41 74 6f  t(db, sqlite3Ato
34197 69 28 7a 52 69 67 68 74 29 29 3b 0d 0a 20 20 20  i(zRight));..   
34198 20 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 53 69   }..    returnSi
34199 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
3419a 22 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f  "wal_autocheckpo
3419b 69 6e 74 22 2c 20 0d 0a 20 20 20 20 20 20 20 64  int", ..       d
3419c 62 2d 3e 78 57 61 6c 43 61 6c 6c 62 61 63 6b 3d  b->xWalCallback=
3419d 3d 73 71 6c 69 74 65 33 57 61 6c 44 65 66 61 75  =sqlite3WalDefau
3419e 6c 74 48 6f 6f 6b 20 3f 20 0d 0a 20 20 20 20 20  ltHook ? ..     
3419f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 54 52        SQLITE_PTR
341a0 5f 54 4f 5f 49 4e 54 28 64 62 2d 3e 70 57 61 6c  _TO_INT(db->pWal
341a1 41 72 67 29 20 3a 20 30 29 3b 0d 0a 20 20 7d 65  Arg) : 0);..  }e
341a2 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  lse..#endif.... 
341a3 20 2f 2a 0d 0a 20 20 2a 2a 20 20 50 52 41 47 4d   /*..  **  PRAGM
341a4 41 20 73 68 72 69 6e 6b 5f 6d 65 6d 6f 72 79 0d  A shrink_memory.
341a5 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69 73  .  **..  ** This
341a6 20 70 72 61 67 6d 61 20 61 74 74 65 6d 70 74 73   pragma attempts
341a7 20 74 6f 20 66 72 65 65 20 61 73 20 6d 75 63 68   to free as much
341a8 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69   memory as possi
341a9 62 6c 65 20 66 72 6f 6d 20 74 68 65 0d 0a 20 20  ble from the..  
341aa 2a 2a 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  ** current datab
341ab 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d  ase connection..
341ac 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  .  */..  if( sql
341ad 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
341ae 74 2c 20 22 73 68 72 69 6e 6b 5f 6d 65 6d 6f 72  t, "shrink_memor
341af 79 22 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73  y")==0 ){..    s
341b0 71 6c 69 74 65 33 5f 64 62 5f 72 65 6c 65 61 73  qlite3_db_releas
341b1 65 5f 6d 65 6d 6f 72 79 28 64 62 29 3b 0d 0a 20  e_memory(db);.. 
341b2 20 7d 65 6c 73 65 0d 0a 0d 0a 23 69 66 20 64 65   }else....#if de
341b3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
341b4 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
341b5 51 4c 49 54 45 5f 54 45 53 54 29 0d 0a 20 20 2f  QLITE_TEST)..  /
341b6 2a 0d 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74  *..  ** Report t
341b7 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
341b8 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f   of file logs fo
341b9 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0d  r all databases.
341ba 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  .  */..  if( sql
341bb 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
341bc 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22  t, "lock_status"
341bd 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 74 61  )==0 ){..    sta
341be 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
341bf 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65  const azLockName
341c0 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 22 75  [] = {..      "u
341c1 6e 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61 72 65  nlocked", "share
341c2 64 22 2c 20 22 72 65 73 65 72 76 65 64 22 2c 20  d", "reserved", 
341c3 22 70 65 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c  "pending", "excl
341c4 75 73 69 76 65 22 0d 0a 20 20 20 20 7d 3b 0d 0a  usive"..    };..
341c5 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20      int i;..    
341c6 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
341c7 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0d 0a 20 20  mCols(v, 2);..  
341c8 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
341c9 20 32 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   2;..    sqlite3
341ca 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
341cb 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
341cc 45 2c 20 22 64 61 74 61 62 61 73 65 22 2c 20 53  E, "database", S
341cd 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
341ce 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
341cf 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
341d0 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
341d1 74 61 74 75 73 22 2c 20 53 51 4c 49 54 45 5f 53  tatus", SQLITE_S
341d2 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 66 6f 72  TATIC);..    for
341d3 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
341d4 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 42 74   i++){..      Bt
341d5 72 65 65 20 2a 70 42 74 3b 0d 0a 20 20 20 20 20  ree *pBt;..     
341d6 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0d   Pager *pPager;.
341d7 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
341d8 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b  r *zState = "unk
341d9 6e 6f 77 6e 22 3b 0d 0a 20 20 20 20 20 20 69 6e  nown";..      in
341da 74 20 6a 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  t j;..      if( 
341db 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
341dc 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d  ==0 ) continue;.
341dd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
341de 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
341df 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c  tring8, 0, 1, 0,
341e0 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
341e1 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0d 0a  e, P4_STATIC);..
341e2 20 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e        pBt = db->
341e3 61 44 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20  aDb[i].pBt;..   
341e4 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 7c 7c     if( pBt==0 ||
341e5 20 28 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74   (pPager = sqlit
341e6 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74  e3BtreePager(pBt
341e7 29 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ))==0 ){..      
341e8 20 20 7a 53 74 61 74 65 20 3d 20 22 63 6c 6f 73    zState = "clos
341e9 65 64 22 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  ed";..      }els
341ea 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66 69  e if( sqlite3_fi
341eb 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 69  le_control(db, i
341ec 20 3f 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e   ? db->aDb[i].zN
341ed 61 6d 65 20 3a 20 30 2c 20 0d 0a 20 20 20 20 20  ame : 0, ..     
341ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341f0 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
341f1 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d 53 51 4c  KSTATE, &j)==SQL
341f2 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
341f3 20 20 20 20 7a 53 74 61 74 65 20 3d 20 61 7a 4c      zState = azL
341f4 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b 0d 0a 20 20 20  ockName[j];..   
341f5 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
341f6 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
341f7 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
341f8 32 2c 20 30 2c 20 7a 53 74 61 74 65 2c 20 50 34  2, 0, zState, P4
341f9 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 20  _STATIC);..     
341fa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
341fb 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
341fc 6f 77 2c 20 31 2c 20 32 29 3b 0d 0a 20 20 20 20  ow, 1, 2);..    
341fd 7d 0d 0a 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65  }....  }else..#e
341fe 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53  ndif....#ifdef S
341ff 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0d  QLITE_HAS_CODEC.
34200 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
34201 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b 65  rICmp(zLeft, "ke
34202 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  y")==0 && zRight
34203 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
34204 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c  _key(db, zRight,
34205 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
34206 28 7a 52 69 67 68 74 29 29 3b 0d 0a 20 20 7d 65  (zRight));..  }e
34207 6c 73 65 0d 0a 20 20 69 66 28 20 73 71 6c 69 74  lse..  if( sqlit
34208 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
34209 20 22 72 65 6b 65 79 22 29 3d 3d 30 20 26 26 20   "rekey")==0 && 
3420a 7a 52 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 73  zRight ){..    s
3420b 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 64 62 2c  qlite3_rekey(db,
3420c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74 65 33   zRight, sqlite3
3420d 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29  Strlen30(zRight)
3420e 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a 20 20 69  );..  }else..  i
3420f 66 28 20 7a 52 69 67 68 74 20 26 26 20 28 73 71  f( zRight && (sq
34210 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
34211 66 74 2c 20 22 68 65 78 6b 65 79 22 29 3d 3d 30  ft, "hexkey")==0
34212 20 7c 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ||..           
34213 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
34214 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68 65 78  ICmp(zLeft, "hex
34215 72 65 6b 65 79 22 29 3d 3d 30 29 20 29 7b 0d 0a  rekey")==0) ){..
34216 20 20 20 20 69 6e 74 20 69 2c 20 68 31 2c 20 68      int i, h1, h
34217 32 3b 0d 0a 20 20 20 20 63 68 61 72 20 7a 4b 65  2;..    char zKe
34218 79 5b 34 30 5d 3b 0d 0a 20 20 20 20 66 6f 72 28  y[40];..    for(
34219 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67 68  i=0; (h1 = zRigh
3421a 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32 20  t[i])!=0 && (h2 
3421b 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21 3d  = zRight[i+1])!=
3421c 30 3b 20 69 2b 3d 32 29 7b 0d 0a 20 20 20 20 20  0; i+=2){..     
3421d 20 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e   h1 += 9*(1&(h1>
3421e 3e 36 29 29 3b 0d 0a 20 20 20 20 20 20 68 32 20  >6));..      h2 
3421f 2b 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29  += 9*(1&(h2>>6))
34220 3b 0d 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f  ;..      zKey[i/
34221 32 5d 20 3d 20 28 68 32 20 26 20 30 78 30 66 29  2] = (h2 & 0x0f)
34222 20 7c 20 28 28 68 31 20 26 20 30 78 66 29 3c 3c   | ((h1 & 0xf)<<
34223 34 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  4);..    }..    
34224 69 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20 26 20  if( (zLeft[3] & 
34225 30 78 66 29 3d 3d 30 78 62 20 29 7b 0d 0a 20 20  0xf)==0xb ){..  
34226 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28      sqlite3_key(
34227 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0d  db, zKey, i/2);.
34228 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
34229 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79     sqlite3_rekey
3422a 28 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b  (db, zKey, i/2);
3422b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65  ..    }..  }else
3422c 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 64 65  ..#endif..#if de
3422d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53  fined(SQLITE_HAS
3422e 5f 43 4f 44 45 43 29 20 7c 7c 20 64 65 66 69 6e  _CODEC) || defin
3422f 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
34230 5f 43 45 52 4f 44 29 0d 0a 20 20 69 66 28 20 73  _CEROD)..  if( s
34231 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
34232 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65  eft, "activate_e
34233 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29  xtensions")==0 )
34234 7b 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  {..#ifdef SQLITE
34235 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 20 20 20 20  _HAS_CODEC..    
34236 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
34237 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 73 65 65  Cmp(zRight, "see
34238 2d 22 2c 20 34 29 3d 3d 30 20 29 7b 0d 0a 20 20  -", 4)==0 ){..  
34239 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69      sqlite3_acti
3423a 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74  vate_see(&zRight
3423b 5b 34 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65  [4]);..    }..#e
3423c 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
3423d 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44  ITE_ENABLE_CEROD
3423e 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
3423f 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74  3StrNICmp(zRight
34240 2c 20 22 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d  , "cerod-", 6)==
34241 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  0 ){..      sqli
34242 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72  te3_activate_cer
34243 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b 0d  od(&zRight[6]);.
34244 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
34245 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d    }else..#endif.
34246 0a 0d 0a 20 0d 0a 20 20 7b 2f 2a 20 45 6d 70 74  ... ..  {/* Empt
34247 79 20 45 4c 53 45 20 63 6c 61 75 73 65 20 2a 2f  y ELSE clause */
34248 7d 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20  }....  /*..  ** 
34249 52 65 73 65 74 20 74 68 65 20 73 61 66 65 74 79  Reset the safety
3424a 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20   level, in case 
3424b 74 68 65 20 66 75 6c 6c 66 73 79 6e 63 20 66 6c  the fullfsync fl
3424c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f 6e 6f 75  ag or synchronou
3424d 73 0d 0a 20 20 2a 2a 20 73 65 74 74 69 6e 67 20  s..  ** setting 
3424e 63 68 61 6e 67 65 64 2e 0d 0a 20 20 2a 2f 0d 0a  changed...  */..
3424f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
34250 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
34251 53 0d 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74  S..  if( db->aut
34252 6f 43 6f 6d 6d 69 74 20 29 7b 0d 0a 20 20 20 20  oCommit ){..    
34253 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53  sqlite3BtreeSetS
34254 61 66 65 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e  afetyLevel(pDb->
34255 70 42 74 2c 20 70 44 62 2d 3e 73 61 66 65 74 79  pBt, pDb->safety
34256 5f 6c 65 76 65 6c 2c 0d 0a 20 20 20 20 20 20 20  _level,..       
34257 20 20 20 20 20 20 20 20 28 64 62 2d 3e 66 6c 61          (db->fla
34258 67 73 26 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53  gs&SQLITE_FullFS
34259 79 6e 63 29 21 3d 30 2c 0d 0a 20 20 20 20 20 20  ync)!=0,..      
3425a 20 20 20 20 20 20 20 20 20 28 64 62 2d 3e 66 6c           (db->fl
3425b 61 67 73 26 53 51 4c 49 54 45 5f 43 6b 70 74 46  ags&SQLITE_CkptF
3425c 75 6c 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0d 0a  ullFSync)!=0);..
3425d 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 70 72 61    }..#endif..pra
3425e 67 6d 61 5f 6f 75 74 3a 0d 0a 20 20 73 71 6c 69  gma_out:..  sqli
3425f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4c  te3DbFree(db, zL
34260 65 66 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  eft);..  sqlite3
34261 44 62 46 72 65 65 28 64 62 2c 20 7a 52 69 67 68  DbFree(db, zRigh
34262 74 29 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66  t);..}....#endif
34263 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
34264 50 52 41 47 4d 41 20 2a 2f 0d 0a 0d 0a 2f 2a 2a  PRAGMA */..../**
34265 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
34266 20 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a   of pragma.c ***
34267 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34268 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34269 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
3426a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
3426b 67 69 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65  gin file prepare
3426c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3426d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3426e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
3426f 2a 0d 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32  *..** 2005 May 2
34270 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  5..**..** The au
34271 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
34272 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
34273 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
34274 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
34275 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
34276 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
34277 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
34278 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
34279 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
3427a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
3427b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
3427c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
3427d 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
3427e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
3427f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
34280 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
34281 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
34282 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34283 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34285 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34286 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
34287 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
34288 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
34289 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
3428a 65 33 5f 70 72 65 70 61 72 65 28 29 0d 0a 2a 2a  e3_prepare()..**
3428b 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 64 20   interface, and 
3428c 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 63 6f  routines that co
3428d 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f 61 64  ntribute to load
3428e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
3428f 20 73 63 68 65 6d 61 0d 0a 2a 2a 20 66 72 6f 6d   schema..** from
34290 20 64 69 73 6b 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a   disk...*/..../*
34291 0d 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e  ..** Fill the In
34292 69 74 44 61 74 61 20 73 74 72 75 63 74 75 72 65  itData structure
34293 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
34294 65 73 73 61 67 65 20 74 68 61 74 20 69 6e 64 69  essage that indi
34295 63 61 74 65 73 0d 0a 2a 2a 20 74 68 61 74 20 74  cates..** that t
34296 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
34297 6f 72 72 75 70 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  orrupt...*/..sta
34298 74 69 63 20 76 6f 69 64 20 63 6f 72 72 75 70 74  tic void corrupt
34299 53 63 68 65 6d 61 28 0d 0a 20 20 49 6e 69 74 44  Schema(..  InitD
3429a 61 74 61 20 2a 70 44 61 74 61 2c 20 20 20 20 20  ata *pData,     
3429b 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 61 74 69 6f  /* Initializatio
3429c 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  n context */..  
3429d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a  const char *zObj
3429e 2c 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 62  ,    /* Object b
3429f 65 69 6e 67 20 70 61 72 73 65 64 20 61 74 20 74  eing parsed at t
342a0 68 65 20 70 6f 69 6e 74 20 6f 66 20 65 72 72 6f  he point of erro
342a1 72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  r */..  const ch
342a2 61 72 20 2a 7a 45 78 74 72 61 20 20 20 2f 2a 20  ar *zExtra   /* 
342a3 45 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  Error informatio
342a4 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69  n */..){..  sqli
342a5 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74 61 2d  te3 *db = pData-
342a6 3e 64 62 3b 0d 0a 20 20 69 66 28 20 21 64 62 2d  >db;..  if( !db-
342a7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26  >mallocFailed &&
342a8 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
342a9 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
342aa 65 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66  e)==0 ){..    if
342ab 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a 4f 62 6a  ( zObj==0 ) zObj
342ac 20 3d 20 22 3f 22 3b 0d 0a 20 20 20 20 73 71 6c   = "?";..    sql
342ad 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 44  ite3SetString(pD
342ae 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 64  ata->pzErrMsg, d
342af 62 2c 0d 0a 20 20 20 20 20 20 22 6d 61 6c 66 6f  b,..      "malfo
342b0 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 73 63  rmed database sc
342b1 68 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a  hema (%s)", zObj
342b2 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a 45 78 74  );..    if( zExt
342b3 72 61 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 44  ra ){..      *pD
342b4 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20  ata->pzErrMsg = 
342b5 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
342b6 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  db, *pData->pzEr
342b7 72 4d 73 67 2c 20 0d 0a 20 20 20 20 20 20 20 20  rMsg, ..        
342b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342b9 20 20 20 20 20 20 20 20 20 22 25 73 20 2d 20 25           "%s - %
342ba 73 22 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  s", *pData->pzEr
342bb 72 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0d 0a  rMsg, zExtra);..
342bc 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70 44      }..  }..  pD
342bd 61 74 61 2d 3e 72 63 20 3d 20 64 62 2d 3e 6d 61  ata->rc = db->ma
342be 6c 6c 6f 63 46 61 69 6c 65 64 20 3f 20 53 51 4c  llocFailed ? SQL
342bf 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49  ITE_NOMEM : SQLI
342c0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
342c1 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
342c2 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c 62 61  is is the callba
342c3 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 74  ck routine for t
342c4 68 65 20 63 6f 64 65 20 74 68 61 74 20 69 6e 69  he code that ini
342c5 74 69 61 6c 69 7a 65 73 20 74 68 65 0d 0a 2a 2a  tializes the..**
342c6 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65 20   database.  See 
342c7 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62 65  sqlite3Init() be
342c8 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  low for addition
342c9 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d  al information..
342ca 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
342cb 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
342cc 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73  from the OP_Pars
342cd 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f  eSchema opcode o
342ce 66 20 74 68 65 20 56 44 42 45 2e 0d 0a 2a 2a 0d  f the VDBE...**.
342cf 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63  .** Each callbac
342d0 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66  k contains the f
342d1 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61  ollowing informa
342d2 74 69 6f 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  tion:..**..**   
342d3 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65    argv[0] = name
342d4 20 6f 66 20 74 68 69 6e 67 20 62 65 69 6e 67 20   of thing being 
342d5 63 72 65 61 74 65 64 0d 0a 2a 2a 20 20 20 20 20  created..**     
342d6 61 72 67 76 5b 31 5d 20 3d 20 72 6f 6f 74 20 70  argv[1] = root p
342d7 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
342d8 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 30  able or index. 0
342d9 20 66 6f 72 20 74 72 69 67 67 65 72 20 6f 72 20   for trigger or 
342da 76 69 65 77 2e 0d 0a 2a 2a 20 20 20 20 20 61 72  view...**     ar
342db 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74 65 78 74  gv[2] = SQL text
342dc 20 66 6f 72 20 74 68 65 20 43 52 45 41 54 45 20   for the CREATE 
342dd 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a  statement...**..
342de 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
342df 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
342e0 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20  itCallback(void 
342e1 2a 70 49 6e 69 74 2c 20 69 6e 74 20 61 72 67 63  *pInit, int argc
342e2 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63  , char **argv, c
342e3 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0d  har **NotUsed){.
342e4 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61  .  InitData *pDa
342e5 74 61 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29  ta = (InitData*)
342e6 70 49 6e 69 74 3b 0d 0a 20 20 73 71 6c 69 74 65  pInit;..  sqlite
342e7 33 20 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e 64  3 *db = pData->d
342e8 62 3b 0d 0a 20 20 69 6e 74 20 69 44 62 20 3d 20  b;..  int iDb = 
342e9 70 44 61 74 61 2d 3e 69 44 62 3b 0d 0a 0d 0a 20  pData->iDb;.... 
342ea 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33   assert( argc==3
342eb 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41   );..  UNUSED_PA
342ec 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
342ed 2c 20 61 72 67 63 29 3b 0d 0a 20 20 61 73 73 65  , argc);..  asse
342ee 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
342ef 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
342f0 29 20 29 3b 0d 0a 20 20 44 62 43 6c 65 61 72 50  ) );..  DbClearP
342f1 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c  roperty(db, iDb,
342f2 20 44 42 5f 45 6d 70 74 79 29 3b 0d 0a 20 20 69   DB_Empty);..  i
342f3 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
342f4 6c 65 64 20 29 7b 0d 0a 20 20 20 20 63 6f 72 72  led ){..    corr
342f5 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
342f6 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0d 0a 20   argv[0], 0);.. 
342f7 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
342f8 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 69  }....  assert( i
342f9 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
342fa 3e 6e 44 62 20 29 3b 0d 0a 20 20 69 66 28 20 61  >nDb );..  if( a
342fb 72 67 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rgv==0 ) return 
342fc 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20 68 61  0;   /* Might ha
342fd 70 70 65 6e 20 69 66 20 45 4d 50 54 59 5f 52 45  ppen if EMPTY_RE
342fe 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20 61  SULT_CALLBACKS a
342ff 72 65 20 6f 6e 20 2a 2f 0d 0a 20 20 69 66 28 20  re on */..  if( 
34300 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0d 0a 20  argv[1]==0 ){.. 
34301 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
34302 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
34303 20 30 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66   0);..  }else if
34304 28 20 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67  ( argv[2] && arg
34305 76 5b 32 5d 5b 30 5d 20 29 7b 0d 0a 20 20 20 20  v[2][0] ){..    
34306 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 61 72 73  /* Call the pars
34307 65 72 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20  er to process a 
34308 43 52 45 41 54 45 20 54 41 42 4c 45 2c 20 49 4e  CREATE TABLE, IN
34309 44 45 58 20 6f 72 20 56 49 45 57 2e 0d 0a 20 20  DEX or VIEW...  
3430a 20 20 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65    ** But because
3430b 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
3430c 73 20 73 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56  s set to 1, no V
3430d 44 42 45 20 63 6f 64 65 20 69 73 20 67 65 6e 65  DBE code is gene
3430e 72 61 74 65 64 0d 0a 20 20 20 20 2a 2a 20 6f 72  rated..    ** or
3430f 20 65 78 65 63 75 74 65 64 2e 20 20 41 6c 6c 20   executed.  All 
34310 74 68 65 20 70 61 72 73 65 72 20 64 6f 65 73 20  the parser does 
34311 69 73 20 62 75 69 6c 64 20 74 68 65 20 69 6e 74  is build the int
34312 65 72 6e 61 6c 20 64 61 74 61 0d 0a 20 20 20 20  ernal data..    
34313 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ** structures th
34314 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20  at describe the 
34315 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f 72  table, index, or
34316 20 76 69 65 77 2e 0d 0a 20 20 20 20 2a 2f 0d 0a   view...    */..
34317 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20      int rc;..   
34318 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
34319 53 74 6d 74 3b 0d 0a 20 20 20 20 54 45 53 54 4f  Stmt;..    TESTO
3431a 4e 4c 59 28 69 6e 74 20 72 63 70 29 3b 20 20 20  NLY(int rcp);   
3431b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
3431c 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c  rn code from sql
3431d 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a  ite3_prepare() *
3431e 2f 0d 0a 0d 0a 20 20 20 20 61 73 73 65 72 74 28  /....    assert(
3431f 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
34320 3b 0d 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  ;..    db->init.
34321 69 44 62 20 3d 20 69 44 62 3b 0d 0a 20 20 20 20  iDb = iDb;..    
34322 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
34323 20 3d 20 73 71 6c 69 74 65 33 41 74 6f 69 28 61   = sqlite3Atoi(a
34324 72 67 76 5b 31 5d 29 3b 0d 0a 20 20 20 20 64 62  rgv[1]);..    db
34325 2d 3e 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69  ->init.orphanTri
34326 67 67 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 54  gger = 0;..    T
34327 45 53 54 4f 4e 4c 59 28 72 63 70 20 3d 20 29 20  ESTONLY(rcp = ) 
34328 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
34329 64 62 2c 20 61 72 67 76 5b 32 5d 2c 20 2d 31 2c  db, argv[2], -1,
3432a 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20   &pStmt, 0);..  
3432b 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 43 6f    rc = db->errCo
3432c 64 65 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  de;..    assert(
3432d 20 28 72 63 26 30 78 46 46 29 3d 3d 28 72 63 70   (rc&0xFF)==(rcp
3432e 26 30 78 46 46 29 20 29 3b 0d 0a 20 20 20 20 64  &0xFF) );..    d
3432f 62 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 30 3b  b->init.iDb = 0;
34330 0d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ..    if( SQLITE
34331 5f 4f 4b 21 3d 72 63 20 29 7b 0d 0a 20 20 20 20  _OK!=rc ){..    
34332 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 6f    if( db->init.o
34333 72 70 68 61 6e 54 72 69 67 67 65 72 20 29 7b 0d  rphanTrigger ){.
34334 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
34335 20 69 44 62 3d 3d 31 20 29 3b 0d 0a 20 20 20 20   iDb==1 );..    
34336 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
34337 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63    pData->rc = rc
34338 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
34339 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
3433a 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 64 62  ){..          db
3433b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
3433c 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c   1;..        }el
3433d 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
3433e 45 5f 49 4e 54 45 52 52 55 50 54 20 26 26 20 28  E_INTERRUPT && (
3433f 72 63 26 30 78 46 46 29 21 3d 53 51 4c 49 54 45  rc&0xFF)!=SQLITE
34340 5f 4c 4f 43 4b 45 44 20 29 7b 0d 0a 20 20 20 20  _LOCKED ){..    
34341 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68        corruptSch
34342 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
34343 30 5d 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  0], sqlite3_errm
34344 73 67 28 64 62 29 29 3b 0d 0a 20 20 20 20 20 20  sg(db));..      
34345 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
34346 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
34347 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
34348 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61  ;..  }else if( a
34349 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0d 0a 20 20  rgv[0]==0 ){..  
3434a 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
3434b 70 44 61 74 61 2c 20 30 2c 20 30 29 3b 0d 0a 20  pData, 0, 0);.. 
3434c 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20   }else{..    /* 
3434d 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d  If the SQL colum
3434e 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65  n is blank it me
3434f 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20 69  ans this is an i
34350 6e 64 65 78 20 74 68 61 74 0d 0a 20 20 20 20 2a  ndex that..    *
34351 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74 6f  * was created to
34352 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59 20   be the PRIMARY 
34353 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c  KEY or to fulfil
34354 6c 20 61 20 55 4e 49 51 55 45 0d 0a 20 20 20 20  l a UNIQUE..    
34355 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ** constraint fo
34356 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  r a CREATE TABLE
34357 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f  .  The index sho
34358 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79  uld have already
34359 0d 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72  ..    ** been cr
3435a 65 61 74 65 64 20 77 68 65 6e 20 77 65 20 70 72  eated when we pr
3435b 6f 63 65 73 73 65 64 20 74 68 65 20 43 52 45 41  ocessed the CREA
3435c 54 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77  TE TABLE.  All w
3435d 65 20 68 61 76 65 0d 0a 20 20 20 20 2a 2a 20 74  e have..    ** t
3435e 6f 20 64 6f 20 68 65 72 65 20 69 73 20 72 65 63  o do here is rec
3435f 6f 72 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ord the root pag
34360 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61  e number for tha
34361 74 20 69 6e 64 65 78 2e 0d 0a 20 20 20 20 2a 2f  t index...    */
34362 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e  ..    Index *pIn
34363 64 65 78 3b 0d 0a 20 20 20 20 70 49 6e 64 65 78  dex;..    pIndex
34364 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
34365 64 65 78 28 64 62 2c 20 61 72 67 76 5b 30 5d 2c  dex(db, argv[0],
34366 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
34367 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  ame);..    if( p
34368 49 6e 64 65 78 3d 3d 30 20 29 7b 0d 0a 20 20 20  Index==0 ){..   
34369 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f     /* This can o
3436a 63 63 75 72 20 69 66 20 74 68 65 72 65 20 65 78  ccur if there ex
3436b 69 73 74 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  ists an index on
3436c 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 77 68   a TEMP table wh
3436d 69 63 68 0d 0a 20 20 20 20 20 20 2a 2a 20 68 61  ich..      ** ha
3436e 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  s the same name 
3436f 61 73 20 61 6e 6f 74 68 65 72 20 69 6e 64 65 78  as another index
34370 20 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20   on a permanent 
34371 69 6e 64 65 78 2e 20 20 53 69 6e 63 65 0d 0a 20  index.  Since.. 
34372 20 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 6d       ** the perm
34373 61 6e 65 6e 74 20 74 61 62 6c 65 20 69 73 20 68  anent table is h
34374 69 64 64 65 6e 20 62 79 20 74 68 65 20 54 45 4d  idden by the TEM
34375 50 20 74 61 62 6c 65 2c 20 77 65 20 63 61 6e 20  P table, we can 
34376 61 6c 73 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 73  also..      ** s
34377 61 66 65 6c 79 20 69 67 6e 6f 72 65 20 74 68 65  afely ignore the
34378 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 65   index on the pe
34379 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 2e 0d 0a  rmanent table...
3437a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
3437b 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20 2a 2f  /* Do Nothing */
3437c 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ;..    }else if(
3437d 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
3437e 28 61 72 67 76 5b 31 5d 2c 20 26 70 49 6e 64 65  (argv[1], &pInde
3437f 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29 7b 0d 0a  x->tnum)==0 ){..
34380 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68        corruptSch
34381 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
34382 30 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72 6f 6f  0], "invalid roo
34383 74 70 61 67 65 22 29 3b 0d 0a 20 20 20 20 7d 0d  tpage");..    }.
34384 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30  .  }..  return 0
34385 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ;..}..../*..** A
34386 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74  ttempt to read t
34387 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
34388 6d 61 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ma and initializ
34389 65 20 69 6e 74 65 72 6e 61 6c 0d 0a 2a 2a 20 64  e internal..** d
3438a 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 66  ata structures f
3438b 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61  or a single data
3438c 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
3438d 69 6e 64 65 78 20 6f 66 20 74 68 65 0d 0a 2a 2a  index of the..**
3438e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
3438f 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e 20  s given by iDb. 
34390 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64 20   iDb==0 is used 
34391 66 6f 72 20 74 68 65 20 6d 61 69 6e 0d 0a 2a 2a  for the main..**
34392 20 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d   database.  iDb=
34393 3d 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  =1 should never 
34394 62 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32  be used.  iDb>=2
34395 20 69 73 20 75 73 65 64 20 66 6f 72 0d 0a 2a 2a   is used for..**
34396 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
34397 61 73 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e  ases.  Return on
34398 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
34399 20 65 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0d   error codes to.
3439a 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20 73 75 63  .** indicate suc
3439b 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e  cess or failure.
3439c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
3439d 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
3439e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
3439f 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45   iDb, char **pzE
343a0 72 72 4d 73 67 29 7b 0d 0a 20 20 69 6e 74 20 72  rrMsg){..  int r
343a1 63 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  c;..  int i;..  
343a2 69 6e 74 20 73 69 7a 65 3b 0d 0a 20 20 54 61 62  int size;..  Tab
343a3 6c 65 20 2a 70 54 61 62 3b 0d 0a 20 20 44 62 20  le *pTab;..  Db 
343a4 2a 70 44 62 3b 0d 0a 20 20 63 68 61 72 20 63 6f  *pDb;..  char co
343a5 6e 73 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0d 0a  nst *azArg[4];..
343a6 20 20 69 6e 74 20 6d 65 74 61 5b 35 5d 3b 0d 0a    int meta[5];..
343a7 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44    InitData initD
343a8 61 74 61 3b 0d 0a 20 20 63 68 61 72 20 63 6f 6e  ata;..  char con
343a9 73 74 20 2a 7a 4d 61 73 74 65 72 53 63 68 65 6d  st *zMasterSchem
343aa 61 3b 0d 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  a;..  char const
343ab 20 2a 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0d 0a   *zMasterName;..
343ac 20 20 69 6e 74 20 6f 70 65 6e 65 64 54 72 61 6e    int openedTran
343ad 73 61 63 74 69 6f 6e 20 3d 20 30 3b 0d 0a 0d 0a  saction = 0;....
343ae 20 20 2f 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 6d    /*..  ** The m
343af 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74  aster database t
343b0 61 62 6c 65 20 68 61 73 20 61 20 73 74 72 75 63  able has a struc
343b1 74 75 72 65 20 6c 69 6b 65 20 74 68 69 73 0d 0a  ture like this..
343b2 20 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 63    */..  static c
343b3 6f 6e 73 74 20 63 68 61 72 20 6d 61 73 74 65 72  onst char master
343b4 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0d 0a 20 20  _schema[] = ..  
343b5 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
343b6 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c   sqlite_master(\
343b7 6e 22 0d 0a 20 20 20 20 20 22 20 20 74 79 70 65  n"..     "  type
343b8 20 74 65 78 74 2c 5c 6e 22 0d 0a 20 20 20 20 20   text,\n"..     
343b9 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  "  name text,\n"
343ba 0d 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61  ..     "  tbl_na
343bb 6d 65 20 74 65 78 74 2c 5c 6e 22 0d 0a 20 20 20  me text,\n"..   
343bc 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e    "  rootpage in
343bd 74 65 67 65 72 2c 5c 6e 22 0d 0a 20 20 20 20 20  teger,\n"..     
343be 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0d 0a  "  sql text\n"..
343bf 20 20 20 20 20 22 29 22 0d 0a 20 20 3b 0d 0a 23       ")"..  ;..#
343c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
343c1 49 54 5f 54 45 4d 50 44 42 0d 0a 20 20 73 74 61  IT_TEMPDB..  sta
343c2 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 74  tic const char t
343c3 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d  emp_master_schem
343c4 61 5b 5d 20 3d 20 0d 0a 20 20 20 20 20 22 43 52  a[] = ..     "CR
343c5 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20  EATE TEMP TABLE 
343c6 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
343c7 65 72 28 5c 6e 22 0d 0a 20 20 20 20 20 22 20 20  er(\n"..     "  
343c8 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0d 0a 20  type text,\n".. 
343c9 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74      "  name text
343ca 2c 5c 6e 22 0d 0a 20 20 20 20 20 22 20 20 74 62  ,\n"..     "  tb
343cb 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0d  l_name text,\n".
343cc 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67  .     "  rootpag
343cd 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0d 0a 20  e integer,\n".. 
343ce 20 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c      "  sql text\
343cf 6e 22 0d 0a 20 20 20 20 20 22 29 22 0d 0a 20 20  n"..     ")"..  
343d0 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 23 64 65 66  ;..#else..  #def
343d1 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  ine temp_master_
343d2 73 63 68 65 6d 61 20 30 0d 0a 23 65 6e 64 69 66  schema 0..#endif
343d3 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 69 44  ....  assert( iD
343d4 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
343d5 6e 44 62 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  nDb );..  assert
343d6 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
343d7 53 63 68 65 6d 61 20 29 3b 0d 0a 20 20 61 73 73  Schema );..  ass
343d8 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
343d9 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
343da 78 29 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  x) );..  assert(
343db 20 69 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74   iDb==1 || sqlit
343dc 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
343dd 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  x(db->aDb[iDb].p
343de 42 74 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 7a  Bt) );....  /* z
343df 4d 61 73 74 65 72 53 63 68 65 6d 61 20 61 6e 64  MasterSchema and
343e0 20 7a 49 6e 69 74 53 63 72 69 70 74 20 61 72 65   zInitScript are
343e1 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74   set to point at
343e2 20 74 68 65 20 6d 61 73 74 65 72 20 73 63 68 65   the master sche
343e3 6d 61 0d 0a 20 20 2a 2a 20 61 6e 64 20 69 6e 69  ma..  ** and ini
343e4 74 69 61 6c 69 73 61 74 69 6f 6e 20 73 63 72 69  tialisation scri
343e5 70 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  pt appropriate f
343e6 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
343e7 62 65 69 6e 67 0d 0a 20 20 2a 2a 20 69 6e 69 74  being..  ** init
343e8 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73 74 65 72  ialised. zMaster
343e9 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
343ea 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74   of the master t
343eb 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  able...  */..  i
343ec 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
343ed 26 26 20 69 44 62 3d 3d 31 20 29 7b 0d 0a 20 20  && iDb==1 ){..  
343ee 20 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20    zMasterSchema 
343ef 3d 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63  = temp_master_sc
343f0 68 65 6d 61 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  hema;..  }else{.
343f1 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68 65  .    zMasterSche
343f2 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73 63 68 65  ma = master_sche
343f3 6d 61 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 4d 61 73  ma;..  }..  zMas
343f4 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41  terName = SCHEMA
343f5 5f 54 41 42 4c 45 28 69 44 62 29 3b 0d 0a 0d 0a  _TABLE(iDb);....
343f6 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
343f7 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  he schema tables
343f8 2e 20 20 2a 2f 0d 0a 20 20 61 7a 41 72 67 5b 30  .  */..  azArg[0
343f9 5d 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b  ] = zMasterName;
343fa 0d 0a 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22  ..  azArg[1] = "
343fb 31 22 3b 0d 0a 20 20 61 7a 41 72 67 5b 32 5d 20  1";..  azArg[2] 
343fc 3d 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b  = zMasterSchema;
343fd 0d 0a 20 20 61 7a 41 72 67 5b 33 5d 20 3d 20 30  ..  azArg[3] = 0
343fe 3b 0d 0a 20 20 69 6e 69 74 44 61 74 61 2e 64 62  ;..  initData.db
343ff 20 3d 20 64 62 3b 0d 0a 20 20 69 6e 69 74 44 61   = db;..  initDa
34400 74 61 2e 69 44 62 20 3d 20 69 44 62 3b 0d 0a 20  ta.iDb = iDb;.. 
34401 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53   initData.rc = S
34402 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 69  QLITE_OK;..  ini
34403 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d  tData.pzErrMsg =
34404 20 70 7a 45 72 72 4d 73 67 3b 0d 0a 20 20 73 71   pzErrMsg;..  sq
34405 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
34406 6b 28 26 69 6e 69 74 44 61 74 61 2c 20 33 2c 20  k(&initData, 3, 
34407 28 63 68 61 72 20 2a 2a 29 61 7a 41 72 67 2c 20  (char **)azArg, 
34408 30 29 3b 0d 0a 20 20 69 66 28 20 69 6e 69 74 44  0);..  if( initD
34409 61 74 61 2e 72 63 20 29 7b 0d 0a 20 20 20 20 72  ata.rc ){..    r
3440a 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b  c = initData.rc;
3440b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
3440c 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54  _out;..  }..  pT
3440d 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
3440e 54 61 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65  Table(db, zMaste
3440f 72 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69  rName, db->aDb[i
34410 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 69  Db].zName);..  i
34411 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 29 20  f( ALWAYS(pTab) 
34412 29 7b 0d 0a 20 20 20 20 70 54 61 62 2d 3e 74 61  ){..    pTab->ta
34413 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 52 65 61  bFlags |= TF_Rea
34414 64 6f 6e 6c 79 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  donly;..  }.... 
34415 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75 72   /* Create a cur
34416 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sor to hold the 
34417 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0d 0a 20  database open.. 
34418 20 2a 2f 0d 0a 20 20 70 44 62 20 3d 20 26 64 62   */..  pDb = &db
34419 2d 3e 61 44 62 5b 69 44 62 5d 3b 0d 0a 20 20 69  ->aDb[iDb];..  i
3441a 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29  f( pDb->pBt==0 )
3441b 7b 0d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54  {..    if( !OMIT
3441c 5f 54 45 4d 50 44 42 20 26 26 20 41 4c 57 41 59  _TEMPDB && ALWAY
3441d 53 28 69 44 62 3d 3d 31 29 20 29 7b 0d 0a 20 20  S(iDb==1) ){..  
3441e 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74      DbSetPropert
3441f 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65  y(db, 1, DB_Sche
34420 6d 61 4c 6f 61 64 65 64 29 3b 0d 0a 20 20 20 20  maLoaded);..    
34421 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  }..    return SQ
34422 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d  LITE_OK;..  }...
34423 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
34424 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20  s not already a 
34425 72 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65  read-only (or re
34426 61 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61  ad-write) transa
34427 63 74 69 6f 6e 20 6f 70 65 6e 65 64 0d 0a 20 20  ction opened..  
34428 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65  ** on the b-tree
34429 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20   database, open 
3442a 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
3442b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
3442c 6e 65 64 2c 20 69 74 20 0d 0a 20 20 2a 2a 20 77  ned, it ..  ** w
3442d 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 20 62 65  ill be closed be
3442e 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
3442f 6f 6e 20 72 65 74 75 72 6e 73 2e 20 20 2a 2f 0d  on returns.  */.
34430 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
34431 6e 74 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0d  nter(pDb->pBt);.
34432 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42  .  if( !sqlite3B
34433 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e  treeIsInReadTran
34434 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0d 0a  s(pDb->pBt) ){..
34435 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34436 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
34437 70 44 62 2d 3e 70 42 74 2c 20 30 29 3b 0d 0a 20  pDb->pBt, 0);.. 
34438 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
34439 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 73  E_OK ){..      s
3443a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
3443b 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  pzErrMsg, db, "%
3443c 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
3443d 72 28 72 63 29 29 3b 0d 0a 20 20 20 20 20 20 67  r(rc));..      g
3443e 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f  oto initone_erro
3443f 72 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  r_out;..    }.. 
34440 20 20 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63     openedTransac
34441 74 69 6f 6e 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a  tion = 1;..  }..
34442 0d 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64  ..  /* Get the d
34443 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66  atabase meta inf
34444 6f 72 6d 61 74 69 6f 6e 2e 0d 0a 20 20 2a 2a 0d  ormation...  **.
34445 0a 20 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65  .  ** Meta value
34446 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
34447 3a 0d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  :..  **    meta[
34448 30 5d 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b  0]   Schema cook
34449 69 65 2e 20 20 43 68 61 6e 67 65 73 20 77 69 74  ie.  Changes wit
3444a 68 20 65 61 63 68 20 73 63 68 65 6d 61 20 63 68  h each schema ch
3444b 61 6e 67 65 2e 0d 0a 20 20 2a 2a 20 20 20 20 6d  ange...  **    m
3444c 65 74 61 5b 31 5d 20 20 20 46 69 6c 65 20 66 6f  eta[1]   File fo
3444d 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c  rmat of schema l
3444e 61 79 65 72 2e 0d 0a 20 20 2a 2a 20 20 20 20 6d  ayer...  **    m
3444f 65 74 61 5b 32 5d 20 20 20 53 69 7a 65 20 6f 66  eta[2]   Size of
34450 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e   the page cache.
34451 0d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 33  ..  **    meta[3
34452 5d 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f 74  ]   Largest root
34453 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f  page (auto/incr_
34454 76 61 63 75 75 6d 20 6d 6f 64 65 29 0d 0a 20 20  vacuum mode)..  
34455 2a 2a 20 20 20 20 6d 65 74 61 5b 34 5d 20 20 20  **    meta[4]   
34456 44 62 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  Db text encoding
34457 2e 20 31 3a 55 54 46 2d 38 20 32 3a 55 54 46 2d  . 1:UTF-8 2:UTF-
34458 31 36 4c 45 20 33 3a 55 54 46 2d 31 36 42 45 0d  16LE 3:UTF-16BE.
34459 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 35 5d  .  **    meta[5]
3445a 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e 0d     User version.
3445b 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36 5d  .  **    meta[6]
3445c 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76     Incremental v
3445d 61 63 75 75 6d 20 6d 6f 64 65 0d 0a 20 20 2a 2a  acuum mode..  **
3445e 20 20 20 20 6d 65 74 61 5b 37 5d 20 20 20 75 6e      meta[7]   un
3445f 75 73 65 64 0d 0a 20 20 2a 2a 20 20 20 20 6d 65  used..  **    me
34460 74 61 5b 38 5d 20 20 20 75 6e 75 73 65 64 0d 0a  ta[8]   unused..
34461 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 39 5d 20    **    meta[9] 
34462 20 20 75 6e 75 73 65 64 0d 0a 20 20 2a 2a 0d 0a    unused..  **..
34463 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20 23    ** Note: The #
34464 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 55  defined SQLITE_U
34465 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20 73  TF* symbols in s
34466 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72 65  qliteInt.h corre
34467 73 70 6f 6e 64 20 74 6f 0d 0a 20 20 2a 2a 20 74  spond to..  ** t
34468 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75  he possible valu
34469 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0d 0a  es of meta[4]...
3446a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b    */..  for(i=0;
3446b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74   i<ArraySize(met
3446c 61 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73  a); i++){..    s
3446d 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
3446e 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31  ta(pDb->pBt, i+1
3446f 2c 20 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69  , (u32 *)&meta[i
34470 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 44 62 2d  ]);..  }..  pDb-
34471 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  >pSchema->schema
34472 5f 63 6f 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 42  _cookie = meta[B
34473 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
34474 49 4f 4e 2d 31 5d 3b 0d 0a 0d 0a 20 20 2f 2a 20  ION-1];....  /* 
34475 49 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 6f 6e  If opening a non
34476 2d 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c  -empty database,
34477 20 63 68 65 63 6b 20 74 68 65 20 74 65 78 74 20   check the text 
34478 65 6e 63 6f 64 69 6e 67 2e 20 46 6f 72 20 74 68  encoding. For th
34479 65 0d 0a 20 20 2a 2a 20 6d 61 69 6e 20 64 61 74  e..  ** main dat
3447a 61 62 61 73 65 2c 20 73 65 74 20 73 71 6c 69 74  abase, set sqlit
3447b 65 33 2e 65 6e 63 20 74 6f 20 74 68 65 20 65 6e  e3.enc to the en
3447c 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 6d 61  coding of the ma
3447d 69 6e 20 64 61 74 61 62 61 73 65 2e 0d 0a 20 20  in database...  
3447e 2a 2a 20 46 6f 72 20 61 6e 20 61 74 74 61 63 68  ** For an attach
3447f 65 64 20 64 62 2c 20 69 74 20 69 73 20 61 6e 20  ed db, it is an 
34480 65 72 72 6f 72 20 69 66 20 74 68 65 20 65 6e 63  error if the enc
34481 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20 74 68 65  oding is not the
34482 20 73 61 6d 65 0d 0a 20 20 2a 2a 20 61 73 20 73   same..  ** as s
34483 71 6c 69 74 65 33 2e 65 6e 63 2e 0d 0a 20 20 2a  qlite3.enc...  *
34484 2f 0d 0a 20 20 69 66 28 20 6d 65 74 61 5b 42 54  /..  if( meta[BT
34485 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e  REE_TEXT_ENCODIN
34486 47 2d 31 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74  G-1] ){  /* text
34487 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0d 0a 20 20   encoding */..  
34488 20 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0d    if( iDb==0 ){.
34489 0a 20 20 20 20 20 20 75 38 20 65 6e 63 6f 64 69  .      u8 encodi
3448a 6e 67 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66  ng;..      /* If
3448b 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d 61 69   opening the mai
3448c 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20  n database, set 
3448d 45 4e 43 28 64 62 29 2e 20 2a 2f 0d 0a 20 20 20  ENC(db). */..   
3448e 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28 75     encoding = (u
3448f 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58  8)meta[BTREE_TEX
34490 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26 20  T_ENCODING-1] & 
34491 33 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 65 6e  3;..      if( en
34492 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f  coding==0 ) enco
34493 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f 55 54  ding = SQLITE_UT
34494 46 38 3b 0d 0a 20 20 20 20 20 20 45 4e 43 28 64  F8;..      ENC(d
34495 62 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0d 0a  b) = encoding;..
34496 20 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43        db->pDfltC
34497 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
34498 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
34499 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52  ITE_UTF8, "BINAR
3449a 59 22 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 65 6c  Y", 0);..    }el
3449b 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66  se{..      /* If
3449c 20 6f 70 65 6e 69 6e 67 20 61 6e 20 61 74 74 61   opening an atta
3449d 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  ched database, t
3449e 68 65 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63 68  he encoding much
3449f 20 6d 61 74 63 68 20 45 4e 43 28 64 62 29 20 2a   match ENC(db) *
344a0 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 6d 65 74  /..      if( met
344a1 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  a[BTREE_TEXT_ENC
344a2 4f 44 49 4e 47 2d 31 5d 21 3d 45 4e 43 28 64 62  ODING-1]!=ENC(db
344a3 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  ) ){..        sq
344a4 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
344a5 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 61 74  zErrMsg, db, "at
344a6 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
344a7 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 61   must use the sa
344a8 6d 65 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20  me"..           
344a9 20 22 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67   " text encoding
344aa 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73   as main databas
344ab 65 22 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63  e");..        rc
344ac 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
344ad 0d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69  ..        goto i
344ae 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74  nitone_error_out
344af 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
344b0 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  }..  }else{..   
344b1 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 64   DbSetProperty(d
344b2 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79  b, iDb, DB_Empty
344b3 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 44 62 2d 3e  );..  }..  pDb->
344b4 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20 45  pSchema->enc = E
344b5 4e 43 28 64 62 29 3b 0d 0a 0d 0a 20 20 69 66 28  NC(db);....  if(
344b6 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
344b7 61 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b 0d  ache_size==0 ){.
344b8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
344b9 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0d  OMIT_DEPRECATED.
344ba 0a 20 20 20 20 73 69 7a 65 20 3d 20 73 71 6c 69  .    size = sqli
344bb 74 65 33 41 62 73 49 6e 74 33 32 28 6d 65 74 61  te3AbsInt32(meta
344bc 5b 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43  [BTREE_DEFAULT_C
344bd 41 43 48 45 5f 53 49 5a 45 2d 31 5d 29 3b 0d 0a  ACHE_SIZE-1]);..
344be 20 20 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20      if( size==0 
344bf 29 7b 20 73 69 7a 65 20 3d 20 53 51 4c 49 54 45  ){ size = SQLITE
344c0 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
344c1 49 5a 45 3b 20 7d 0d 0a 20 20 20 20 70 44 62 2d  IZE; }..    pDb-
344c2 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
344c3 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0d 0a 23 65  size = size;..#e
344c4 6c 73 65 0d 0a 20 20 20 20 70 44 62 2d 3e 70 53  lse..    pDb->pS
344c5 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
344c6 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55  e = SQLITE_DEFAU
344c7 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b 0d 0a  LT_CACHE_SIZE;..
344c8 23 65 6e 64 69 66 0d 0a 20 20 20 20 73 71 6c 69  #endif..    sqli
344c9 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
344ca 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70  Size(pDb->pBt, p
344cb 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
344cc 68 65 5f 73 69 7a 65 29 3b 0d 0a 20 20 7d 0d 0a  he_size);..  }..
344cd 0d 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 66 69 6c  ..  /*..  ** fil
344ce 65 5f 66 6f 72 6d 61 74 3d 3d 31 20 20 20 20 56  e_format==1    V
344cf 65 72 73 69 6f 6e 20 33 2e 30 2e 30 2e 0d 0a 20  ersion 3.0.0... 
344d0 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d   ** file_format=
344d1 3d 32 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e  =2    Version 3.
344d2 31 2e 33 2e 20 20 2f 2f 20 41 4c 54 45 52 20 54  1.3.  // ALTER T
344d3 41 42 4c 45 20 41 44 44 20 43 4f 4c 55 4d 4e 0d  ABLE ADD COLUMN.
344d4 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61  .  ** file_forma
344d5 74 3d 3d 33 20 20 20 20 56 65 72 73 69 6f 6e 20  t==3    Version 
344d6 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69 74 74 6f  3.1.4.  // ditto
344d7 20 62 75 74 20 77 69 74 68 20 6e 6f 6e 2d 4e 55   but with non-NU
344d8 4c 4c 20 64 65 66 61 75 6c 74 73 0d 0a 20 20 2a  LL defaults..  *
344d9 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 34  * file_format==4
344da 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e      Version 3.3.
344db 30 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e 64 69  0.  // DESC indi
344dc 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20 63 6f  ces.  Boolean co
344dd 6e 73 74 61 6e 74 73 0d 0a 20 20 2a 2f 0d 0a 20  nstants..  */.. 
344de 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
344df 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38  ile_format = (u8
344e0 29 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45  )meta[BTREE_FILE
344e1 5f 46 4f 52 4d 41 54 2d 31 5d 3b 0d 0a 20 20 69  _FORMAT-1];..  i
344e2 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
344e3 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20  >file_format==0 
344e4 29 7b 0d 0a 20 20 20 20 70 44 62 2d 3e 70 53 63  ){..    pDb->pSc
344e5 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
344e6 74 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 69  t = 1;..  }..  i
344e7 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
344e8 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53 51 4c  >file_format>SQL
344e9 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52  ITE_MAX_FILE_FOR
344ea 4d 41 54 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  MAT ){..    sqli
344eb 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
344ec 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73 75  rrMsg, db, "unsu
344ed 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f 72  pported file for
344ee 6d 61 74 22 29 3b 0d 0a 20 20 20 20 72 63 20 3d  mat");..    rc =
344ef 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a   SQLITE_ERROR;..
344f0 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
344f1 5f 65 72 72 6f 72 5f 6f 75 74 3b 0d 0a 20 20 7d  _error_out;..  }
344f2 0d 0a 0d 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20  ....  /* Ticket 
344f3 23 32 38 30 34 3a 20 20 57 68 65 6e 20 77 65 20  #2804:  When we 
344f4 6f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20  open a database 
344f5 69 6e 20 74 68 65 20 6e 65 77 65 72 20 66 69 6c  in the newer fil
344f6 65 20 66 6f 72 6d 61 74 2c 0d 0a 20 20 2a 2a 20  e format,..  ** 
344f7 63 6c 65 61 72 20 74 68 65 20 6c 65 67 61 63 79  clear the legacy
344f8 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 70 72 61  _file_format pra
344f9 67 6d 61 20 66 6c 61 67 20 73 6f 20 74 68 61 74  gma flag so that
344fa 20 61 20 56 41 43 55 55 4d 20 77 69 6c 6c 0d 0a   a VACUUM will..
344fb 20 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e 67 72 61    ** not downgra
344fc 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  de the database 
344fd 61 6e 64 20 74 68 75 73 20 69 6e 76 61 6c 69 64  and thus invalid
344fe 61 74 65 20 61 6e 79 20 64 65 73 63 65 6e 64 69  ate any descendi
344ff 6e 67 0d 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73  ng..  ** indices
34500 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 6d   that the user m
34501 69 67 68 74 20 68 61 76 65 20 63 72 65 61 74 65  ight have create
34502 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  d...  */..  if( 
34503 69 44 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b 42  iDb==0 && meta[B
34504 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
34505 2d 31 5d 3e 3d 34 20 29 7b 0d 0a 20 20 20 20 64  -1]>=4 ){..    d
34506 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
34507 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d  ITE_LegacyFileFm
34508 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  t;..  }....  /* 
34509 52 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  Read the schema 
3450a 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20  information out 
3450b 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 74 61  of the schema ta
3450c 62 6c 65 73 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73  bles..  */..  as
3450d 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62  sert( db->init.b
3450e 75 73 79 20 29 3b 0d 0a 20 20 7b 0d 0a 20 20 20  usy );..  {..   
3450f 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0d 0a 20 20   char *zSql;..  
34510 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
34511 4d 50 72 69 6e 74 66 28 64 62 2c 20 0d 0a 20 20  MPrintf(db, ..  
34512 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61        "SELECT na
34513 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71  me, rootpage, sq
34514 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 4f  l FROM '%q'.%s O
34515 52 44 45 52 20 42 59 20 72 6f 77 69 64 22 2c 0d  RDER BY rowid",.
34516 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  .        db->aDb
34517 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61  [iDb].zName, zMa
34518 73 74 65 72 4e 61 6d 65 29 3b 0d 0a 23 69 66 6e  sterName);..#ifn
34519 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3451a 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0d 0a 20  AUTHORIZATION.. 
3451b 20 20 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20     {..      int 
3451c 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69  (*xAuth)(void*,i
3451d 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  nt,const char*,c
3451e 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
3451f 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
34520 72 2a 29 3b 0d 0a 20 20 20 20 20 20 78 41 75 74  r*);..      xAut
34521 68 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0d 0a  h = db->xAuth;..
34522 20 20 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20        db->xAuth 
34523 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  = 0;..#endif..  
34524 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34525 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20  _exec(db, zSql, 
34526 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62  sqlite3InitCallb
34527 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20  ack, &initData, 
34528 30 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  0);..#ifndef SQL
34529 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
3452a 5a 41 54 49 4f 4e 0d 0a 20 20 20 20 20 20 64 62  ZATION..      db
3452b 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b  ->xAuth = xAuth;
3452c 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
3452d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3452e 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e  ITE_OK ) rc = in
3452f 69 74 44 61 74 61 2e 72 63 3b 0d 0a 20 20 20 20  itData.rc;..    
34530 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
34531 2c 20 7a 53 71 6c 29 3b 0d 0a 23 69 66 6e 64 65  , zSql);..#ifnde
34532 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  f SQLITE_OMIT_AN
34533 41 4c 59 5a 45 0d 0a 20 20 20 20 69 66 28 20 72  ALYZE..    if( r
34534 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
34535 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 6e  .      sqlite3An
34536 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69  alysisLoad(db, i
34537 44 62 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  Db);..    }..#en
34538 64 69 66 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  dif..  }..  if( 
34539 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3453a 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51   ){..    rc = SQ
3453b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
3453c 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
3453d 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
3453e 2d 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  -1);..  }..  if(
3453f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
34540 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  | (db->flags&SQL
34541 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
34542 29 29 7b 0d 0a 20 20 20 20 2f 2a 20 42 6c 61 63  )){..    /* Blac
34543 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68 65 20  k magic: If the 
34544 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
34545 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c  ode flag is set,
34546 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 0d 0a   then consider..
34547 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d      ** the schem
34548 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20 69  a loaded, even i
34549 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72 65  f errors occurre
3454a 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61  d. In this situa
3454b 74 69 6f 6e 20 74 68 65 20 0d 0a 20 20 20 20 2a  tion the ..    *
3454c 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  * current sqlite
3454d 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72  3_prepare() oper
3454e 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c  ation will fail,
3454f 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   but the followi
34550 6e 67 20 6f 6e 65 0d 0a 20 20 20 20 2a 2a 20 77  ng one..    ** w
34551 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63  ill attempt to c
34552 6f 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c  ompile the suppl
34553 69 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67  ied statement ag
34554 61 69 6e 73 74 20 77 68 61 74 65 76 65 72 20 73  ainst whatever s
34555 75 62 73 65 74 0d 0a 20 20 20 20 2a 2a 20 6f 66  ubset..    ** of
34556 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73 20   the schema was 
34557 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 20 74 68  loaded before th
34558 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  e error occurred
34559 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0d 0a 20  . The primary.. 
3455a 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66     ** purpose of
3455b 20 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f   this is to allo
3455c 77 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  w access to the 
3455d 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
3455e 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 65 76 65 6e  ble..    ** even
3455f 20 77 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e   when its conten
34560 74 73 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72  ts have been cor
34561 72 75 70 74 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d  rupted...    */.
34562 0a 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72  .    DbSetProper
34563 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 53  ty(db, iDb, DB_S
34564 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0d 0a 20  chemaLoaded);.. 
34565 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
34566 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  K;..  }....  /* 
34567 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 61 6e  Jump here for an
34568 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75   error that occu
34569 72 73 20 61 66 74 65 72 20 73 75 63 63 65 73 73  rs after success
3456a 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74 69 6e 67  fully allocating
3456b 0d 0a 20 20 2a 2a 20 63 75 72 4d 61 69 6e 20 61  ..  ** curMain a
3456c 6e 64 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  nd calling sqlit
3456d 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 2e 20  e3BtreeEnter(). 
3456e 46 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61  For an error tha
3456f 74 20 6f 63 63 75 72 73 0d 0a 20 20 2a 2a 20 62  t occurs..  ** b
34570 65 66 6f 72 65 20 74 68 61 74 20 70 6f 69 6e 74  efore that point
34571 2c 20 6a 75 6d 70 20 74 6f 20 65 72 72 6f 72 5f  , jump to error_
34572 6f 75 74 2e 0d 0a 20 20 2a 2f 0d 0a 69 6e 69 74  out...  */..init
34573 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a 0d 0a  one_error_out:..
34574 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e    if( openedTran
34575 73 61 63 74 69 6f 6e 20 29 7b 0d 0a 20 20 20 20  saction ){..    
34576 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
34577 69 74 28 70 44 62 2d 3e 70 42 74 29 3b 0d 0a 20  it(pDb->pBt);.. 
34578 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72   }..  sqlite3Btr
34579 65 65 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74  eeLeave(pDb->pBt
3457a 29 3b 0d 0a 0d 0a 65 72 72 6f 72 5f 6f 75 74 3a  );....error_out:
3457b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
3457c 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d  TE_NOMEM || rc==
3457d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
3457e 45 4d 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d  EM ){..    db->m
3457f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
34580 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
34581 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
34582 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20   Initialize all 
34583 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2d  database files -
34584 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
34585 73 65 20 66 69 6c 65 2c 20 74 68 65 20 66 69 6c  se file, the fil
34586 65 0d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  e..** used to st
34587 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ore temporary ta
34588 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20 61 64  bles, and any ad
34589 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73  ditional databas
3458a 65 20 66 69 6c 65 73 0d 0a 2a 2a 20 63 72 65 61  e files..** crea
3458b 74 65 64 20 75 73 69 6e 67 20 41 54 54 41 43 48  ted using ATTACH
3458c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52 65   statements.  Re
3458d 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63  turn a success c
3458e 6f 64 65 2e 20 20 49 66 20 61 6e 0d 0a 2a 2a 20  ode.  If an..** 
3458f 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72  error occurs, wr
34590 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
34591 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72  sage into *pzErr
34592 4d 73 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 66 74  Msg...**..** Aft
34593 65 72 20 61 20 64 61 74 61 62 61 73 65 20 69 73  er a database is
34594 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
34595 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  e DB_SchemaLoade
34596 64 20 62 69 74 20 69 73 20 73 65 74 0d 0a 2a 2a  d bit is set..**
34597 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74   bit is set in t
34598 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f  he flags field o
34599 66 20 74 68 65 20 44 62 20 73 74 72 75 63 74 75  f the Db structu
3459a 72 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  re. If the datab
3459b 61 73 65 0d 0a 2a 2a 20 66 69 6c 65 20 77 61 73  ase..** file was
3459c 20 6f 66 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c   of zero-length,
3459d 20 74 68 65 6e 20 74 68 65 20 44 42 5f 45 6d 70   then the DB_Emp
3459e 74 79 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 20  ty flag is also 
3459f 73 65 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  set...*/..SQLITE
345a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
345a1 69 74 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33  ite3Init(sqlite3
345a2 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45   *db, char **pzE
345a3 72 72 4d 73 67 29 7b 0d 0a 20 20 69 6e 74 20 69  rrMsg){..  int i
345a4 2c 20 72 63 3b 0d 0a 20 20 69 6e 74 20 63 6f 6d  , rc;..  int com
345a5 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 3d 20 21  mit_internal = !
345a6 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
345a7 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29  E_InternChanges)
345a8 3b 0d 0a 20 20 0d 0a 20 20 61 73 73 65 72 74 28  ;..  ..  assert(
345a9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
345aa 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
345ab 3b 0d 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  ;..  rc = SQLITE
345ac 5f 4f 4b 3b 0d 0a 20 20 64 62 2d 3e 69 6e 69 74  _OK;..  db->init
345ad 2e 62 75 73 79 20 3d 20 31 3b 0d 0a 20 20 66 6f  .busy = 1;..  fo
345ae 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
345af 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
345b0 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  b; i++){..    if
345b1 28 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  ( DbHasProperty(
345b2 64 62 2c 20 69 2c 20 44 42 5f 53 63 68 65 6d 61  db, i, DB_Schema
345b3 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d 3d 31 20  Loaded) || i==1 
345b4 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20  ) continue;..   
345b5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69   rc = sqlite3Ini
345b6 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a 45 72  tOne(db, i, pzEr
345b7 72 4d 73 67 29 3b 0d 0a 20 20 20 20 69 66 28 20  rMsg);..    if( 
345b8 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  rc ){..      sql
345b9 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
345ba 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0d  lSchema(db, i);.
345bb 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
345bc 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65   /* Once all the
345bd 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 73   other databases
345be 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
345bf 61 6c 69 73 65 64 2c 20 6c 6f 61 64 20 74 68 65  alised, load the
345c0 20 73 63 68 65 6d 61 0d 0a 20 20 2a 2a 20 66 6f   schema..  ** fo
345c1 72 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  r the TEMP datab
345c2 61 73 65 2e 20 54 68 69 73 20 69 73 20 6c 6f 61  ase. This is loa
345c3 64 65 64 20 6c 61 73 74 2c 20 61 73 20 74 68 65  ded last, as the
345c4 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 0d 0a   TEMP database..
345c5 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d 61 79 20    ** schema may 
345c6 63 6f 6e 74 61 69 6e 20 72 65 66 65 72 65 6e 63  contain referenc
345c7 65 73 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e  es to objects in
345c8 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 73   other databases
345c9 2e 0d 0a 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66  ...  */..#ifndef
345ca 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
345cb 50 44 42 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  PDB..  if( rc==S
345cc 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41  QLITE_OK && ALWA
345cd 59 53 28 64 62 2d 3e 6e 44 62 3e 31 29 0d 0a 20  YS(db->nDb>1).. 
345ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
345cf 20 20 20 26 26 20 21 44 62 48 61 73 50 72 6f 70     && !DbHasProp
345d0 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53  erty(db, 1, DB_S
345d1 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0d  chemaLoaded) ){.
345d2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
345d3 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20  3InitOne(db, 1, 
345d4 70 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20  pzErrMsg);..    
345d5 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20 20  if( rc ){..     
345d6 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
345d7 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
345d8 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  1);..    }..  }.
345d9 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 64 62 2d  .#endif....  db-
345da 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0d  >init.busy = 0;.
345db 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
345dc 45 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69  E_OK && commit_i
345dd 6e 74 65 72 6e 61 6c 20 29 7b 0d 0a 20 20 20 20  nternal ){..    
345de 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
345df 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29  ernalChanges(db)
345e0 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75  ;..  }....  retu
345e1 72 6e 20 72 63 3b 20 0d 0a 7d 0d 0a 0d 0a 2f 2a  rn rc; ..}..../*
345e2 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
345e3 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  e is a no-op if 
345e4 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
345e5 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 69  ema is already i
345e6 6e 69 74 69 61 6c 69 73 65 64 2e 0d 0a 2a 2a 20  nitialised...** 
345e7 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73  Otherwise, the s
345e8 63 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e  chema is loaded.
345e9 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
345ea 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d  s returned...*/.
345eb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
345ec 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 53  int sqlite3ReadS
345ed 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61  chema(Parse *pPa
345ee 72 73 65 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20  rse){..  int rc 
345ef 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
345f0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
345f1 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 61 73 73  arse->db;..  ass
345f2 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
345f3 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
345f4 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 21 64 62  x) );..  if( !db
345f5 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0d 0a  ->init.busy ){..
345f6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
345f7 49 6e 69 74 28 64 62 2c 20 26 70 50 61 72 73 65  Init(db, &pParse
345f8 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 7d  ->zErrMsg);..  }
345f9 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
345fa 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 50  TE_OK ){..    pP
345fb 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0d 0a  arse->rc = rc;..
345fc 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
345fd 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  ++;..  }..  retu
345fe 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  rn rc;..}....../
345ff 2a 0d 0a 2a 2a 20 43 68 65 63 6b 20 73 63 68 65  *..** Check sche
34600 6d 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c  ma cookies in al
34601 6c 20 64 61 74 61 62 61 73 65 73 2e 20 20 49 66  l databases.  If
34602 20 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f   any cookie is o
34603 75 74 0d 0a 2a 2a 20 6f 66 20 64 61 74 65 20 73  ut..** of date s
34604 65 74 20 70 50 61 72 73 65 2d 3e 72 63 20 74 6f  et pParse->rc to
34605 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 20   SQLITE_SCHEMA. 
34606 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20 63   If all schema c
34607 6f 6f 6b 69 65 73 0d 0a 2a 2a 20 6d 61 6b 65 20  ookies..** make 
34608 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 70 50  no changes to pP
34609 61 72 73 65 2d 3e 72 63 2e 0d 0a 2a 2f 0d 0a 73  arse->rc...*/..s
3460a 74 61 74 69 63 20 76 6f 69 64 20 73 63 68 65 6d  tatic void schem
3460b 61 49 73 56 61 6c 69 64 28 50 61 72 73 65 20 2a  aIsValid(Parse *
3460c 70 50 61 72 73 65 29 7b 0d 0a 20 20 73 71 6c 69  pParse){..  sqli
3460d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
3460e 2d 3e 64 62 3b 0d 0a 20 20 69 6e 74 20 69 44 62  ->db;..  int iDb
3460f 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  ;..  int rc;..  
34610 69 6e 74 20 63 6f 6f 6b 69 65 3b 0d 0a 0d 0a 20  int cookie;.... 
34611 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
34612 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 29 3b 0d  >checkSchema );.
34613 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
34614 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
34615 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 66  ->mutex) );..  f
34616 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62  or(iDb=0; iDb<db
34617 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0d 0a  ->nDb; iDb++){..
34618 20 20 20 20 69 6e 74 20 6f 70 65 6e 65 64 54 72      int openedTr
34619 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30 3b 20 20  ansaction = 0;  
3461a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
3461b 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
3461c 69 73 20 6f 70 65 6e 65 64 20 2a 2f 0d 0a 20 20  is opened */..  
3461d 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
3461e 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b  b->aDb[iDb].pBt;
3461f 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 64 61       /* Btree da
34620 74 61 62 61 73 65 20 74 6f 20 72 65 61 64 20 63  tabase to read c
34621 6f 6f 6b 69 65 20 66 72 6f 6d 20 2a 2f 0d 0a 20  ookie from */.. 
34622 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 20     if( pBt==0 ) 
34623 63 6f 6e 74 69 6e 75 65 3b 0d 0a 0d 0a 20 20 20  continue;....   
34624 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
34625 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72 65  not already a re
34626 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64  ad-only (or read
34627 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74  -write) transact
34628 69 6f 6e 20 6f 70 65 6e 65 64 0d 0a 20 20 20 20  ion opened..    
34629 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65  ** on the b-tree
3462a 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20   database, open 
3462b 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
3462c 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
3462d 6e 65 64 2c 20 69 74 20 0d 0a 20 20 20 20 2a 2a  ned, it ..    **
3462e 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 20   will be closed 
3462f 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
34630 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 6d 65  r reading the me
34631 74 61 2d 76 61 6c 75 65 2e 20 2a 2f 0d 0a 20 20  ta-value. */..  
34632 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 74    if( !sqlite3Bt
34633 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
34634 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20 20 20  (pBt) ){..      
34635 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
34636 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c  eBeginTrans(pBt,
34637 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20   0);..      if( 
34638 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
34639 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
3463a 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0d 0a 20  OERR_NOMEM ){.. 
3463b 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
3463c 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20  cFailed = 1;..  
3463d 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
3463e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3463f 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20   return;..      
34640 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f  openedTransactio
34641 6e 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  n = 1;..    }...
34642 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
34643 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 66   schema cookie f
34644 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
34645 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
34646 20 6d 61 74 63 68 20 74 68 65 20 0d 0a 20 20 20   match the ..   
34647 20 2a 2a 20 76 61 6c 75 65 20 73 74 6f 72 65 64   ** value stored
34648 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
34649 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65 6d 61  in-memory schema
3464a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c   representation,
3464b 0d 0a 20 20 20 20 2a 2a 20 73 65 74 20 50 61 72  ..    ** set Par
3464c 73 65 2e 72 63 20 74 6f 20 53 51 4c 49 54 45 5f  se.rc to SQLITE_
3464d 53 43 48 45 4d 41 2e 20 2a 2f 0d 0a 20 20 20 20  SCHEMA. */..    
3464e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
3464f 65 74 61 28 70 42 74 2c 20 42 54 52 45 45 5f 53  eta(pBt, BTREE_S
34650 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28  CHEMA_VERSION, (
34651 75 33 32 20 2a 29 26 63 6f 6f 6b 69 65 29 3b 0d  u32 *)&cookie);.
34652 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
34653 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
34654 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20  eld(db, iDb, 0) 
34655 29 3b 0d 0a 20 20 20 20 69 66 28 20 63 6f 6f 6b  );..    if( cook
34656 69 65 21 3d 64 62 2d 3e 61 44 62 5b 69 44 62 5d  ie!=db->aDb[iDb]
34657 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
34658 5f 63 6f 6f 6b 69 65 20 29 7b 0d 0a 20 20 20 20  _cookie ){..    
34659 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
3465a 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
3465b 20 69 44 62 29 3b 0d 0a 20 20 20 20 20 20 70 50   iDb);..      pP
3465c 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
3465d 45 5f 53 43 48 45 4d 41 3b 0d 0a 20 20 20 20 7d  E_SCHEMA;..    }
3465e 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65  ....    /* Close
3465f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
34660 2c 20 69 66 20 6f 6e 65 20 77 61 73 20 6f 70 65  , if one was ope
34661 6e 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28  ned. */..    if(
34662 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
34663 6f 6e 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  on ){..      sql
34664 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
34665 70 42 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  pBt);..    }..  
34666 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  }..}..../*..** C
34667 6f 6e 76 65 72 74 20 61 20 73 63 68 65 6d 61 20  onvert a schema 
34668 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65  pointer into the
34669 20 69 44 62 20 69 6e 64 65 78 20 74 68 61 74 20   iDb index that 
3466a 69 6e 64 69 63 61 74 65 73 0d 0a 2a 2a 20 77 68  indicates..** wh
3466b 69 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c  ich database fil
3466c 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 74  e in db->aDb[] t
3466d 68 65 20 73 63 68 65 6d 61 20 72 65 66 65 72 73  he schema refers
3466e 20 74 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20   to...**..** If 
3466f 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
34670 65 20 69 73 20 61 74 74 61 63 68 65 64 20 6d 6f  e is attached mo
34671 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68  re than once, th
34672 65 20 66 69 72 73 74 0d 0a 2a 2a 20 61 74 74 61  e first..** atta
34673 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 73  ched database is
34674 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a   returned...*/..
34675 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
34676 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  nt sqlite3Schema
34677 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20  ToIndex(sqlite3 
34678 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70 53 63  *db, Schema *pSc
34679 68 65 6d 61 29 7b 0d 0a 20 20 69 6e 74 20 69 20  hema){..  int i 
3467a 3d 20 2d 31 30 30 30 30 30 30 3b 0d 0a 0d 0a 20  = -1000000;.... 
3467b 20 2f 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69   /* If pSchema i
3467c 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74  s NULL, then ret
3467d 75 72 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68  urn -1000000. Th
3467e 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
3467f 63 6f 64 65 20 69 6e 20 0d 0a 20 20 2a 2a 20 65  code in ..  ** e
34680 78 70 72 2e 63 20 69 73 20 74 72 79 69 6e 67 20  xpr.c is trying 
34681 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 72 65 66  to resolve a ref
34682 65 72 65 6e 63 65 20 74 6f 20 61 20 74 72 61 6e  erence to a tran
34683 73 69 65 6e 74 20 74 61 62 6c 65 20 28 69 2e 65  sient table (i.e
34684 2e 20 6f 6e 65 0d 0a 20 20 2a 2a 20 63 72 65 61  . one..  ** crea
34685 74 65 64 20 62 79 20 61 20 73 75 62 2d 73 65 6c  ted by a sub-sel
34686 65 63 74 29 2e 20 49 6e 20 74 68 69 73 20 63 61  ect). In this ca
34687 73 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  se the return va
34688 6c 75 65 20 6f 66 20 74 68 69 73 20 0d 0a 20 20  lue of this ..  
34689 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ** function shou
3468a 6c 64 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ld never be used
3468b 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57 65  ...  **..  ** We
3468c 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30   return -1000000
3468d 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
3468e 6d 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73 69  more usual -1 si
3468f 6d 70 6c 79 20 62 65 63 61 75 73 65 20 75 73 69  mply because usi
34690 6e 67 0d 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30  ng..  ** -100000
34691 30 20 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65  0 as the incorre
34692 63 74 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  ct index into db
34693 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
34694 0d 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65  ..  ** more like
34695 6c 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65  ly to cause a se
34696 67 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28  gfault than -1 (
34697 6f 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20  of course there 
34698 61 72 65 20 61 73 73 65 72 74 28 29 0d 0a 20 20  are assert()..  
34699 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
3469a 6f 2c 20 62 75 74 20 69 74 20 6e 65 76 65 72 20  o, but it never 
3469b 68 75 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68  hurts to play th
3469c 65 20 6f 64 64 73 29 2e 0d 0a 20 20 2a 2f 0d 0a  e odds)...  */..
3469d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
3469e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
3469f 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 69 66  >mutex) );..  if
346a0 28 20 70 53 63 68 65 6d 61 20 29 7b 0d 0a 20 20  ( pSchema ){..  
346a1 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 41 59    for(i=0; ALWAY
346a2 53 28 69 3c 64 62 2d 3e 6e 44 62 29 3b 20 69 2b  S(i<db->nDb); i+
346a3 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 64  +){..      if( d
346a4 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d  b->aDb[i].pSchem
346a5 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0d 0a 20  a==pSchema ){.. 
346a6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
346a7 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
346a8 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20     assert( i>=0 
346a9 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0d  && i<db->nDb );.
346aa 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 69  .  }..  return i
346ab 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
346ac 6f 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38  ompile the UTF-8
346ad 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
346ae 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f  tement zSql into
346af 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e   a statement han
346b0 64 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  dle...*/..static
346b1 20 69 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70   int sqlite3Prep
346b2 61 72 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  are(..  sqlite3 
346b3 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
346b4 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
346b5 6e 64 6c 65 2e 20 2a 2f 0d 0a 20 20 63 6f 6e 73  ndle. */..  cons
346b6 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
346b7 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
346b8 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
346b9 6d 65 6e 74 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ment. */..  int 
346ba 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
346bb 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
346bc 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
346bd 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 61 76 65  . */..  int save
346be 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20  SqlFlag,        
346bf 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70    /* True to cop
346c0 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20  y SQL text into 
346c1 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  the sqlite3_stmt
346c2 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 70 52 65   */..  Vdbe *pRe
346c3 70 72 65 70 61 72 65 2c 20 20 20 20 20 20 20 20  prepare,        
346c4 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20 72 65 70   /* VM being rep
346c5 72 65 70 61 72 65 64 20 2a 2f 0d 0a 20 20 73 71  repared */..  sq
346c6 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
346c7 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
346c8 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
346c9 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
346ca 65 6e 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  ent */..  const 
346cb 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
346cc 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
346cd 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
346ce 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 50 61 72 73 65   */..){..  Parse
346cf 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
346d0 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
346d1 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 63 68  context */..  ch
346d2 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ar *zErrMsg = 0;
346d3 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
346d4 20 6d 65 73 73 61 67 65 20 2a 2f 0d 0a 20 20 69   message */..  i
346d5 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
346d6 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75  K;       /* Resu
346d7 6c 74 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e  lt code */..  in
346d8 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
346d9 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
346da 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20  counter */....  
346db 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
346dc 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
346dd 2a 2f 0d 0a 20 20 70 50 61 72 73 65 20 3d 20 73  */..  pParse = s
346de 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
346df 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
346e0 2a 70 50 61 72 73 65 29 29 3b 0d 0a 20 20 69 66  *pParse));..  if
346e1 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0d 0a  ( pParse==0 ){..
346e2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
346e3 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 67 6f 74 6f  NOMEM;..    goto
346e4 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0d 0a 20   end_prepare;.. 
346e5 20 7d 0d 0a 20 20 70 50 61 72 73 65 2d 3e 70 52   }..  pParse->pR
346e6 65 70 72 65 70 61 72 65 20 3d 20 70 52 65 70 72  eprepare = pRepr
346e7 65 70 61 72 65 3b 0d 0a 20 20 61 73 73 65 72 74  epare;..  assert
346e8 28 20 70 70 53 74 6d 74 20 26 26 20 2a 70 70 53  ( ppStmt && *ppS
346e9 74 6d 74 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73  tmt==0 );..  ass
346ea 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
346eb 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 61 73 73  Failed );..  ass
346ec 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
346ed 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
346ee 78 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68  x) );....  /* Ch
346ef 65 63 6b 20 74 6f 20 76 65 72 69 66 79 20 74 68  eck to verify th
346f0 61 74 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  at it is possibl
346f1 65 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 20  e to get a read 
346f2 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0d 0a 20 20 2a  lock on all..  *
346f3 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
346f4 61 73 2e 20 20 54 68 65 20 69 6e 61 62 69 6c 69  as.  The inabili
346f5 74 79 20 74 6f 20 67 65 74 20 61 20 72 65 61 64  ty to get a read
346f6 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74 65 73 20   lock indicates 
346f7 74 68 61 74 0d 0a 20 20 2a 2a 20 73 6f 6d 65 20  that..  ** some 
346f8 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
346f9 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
346fa 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ding a write-loc
346fb 6b 2c 20 77 68 69 63 68 20 69 6e 0d 0a 20 20 2a  k, which in..  *
346fc 2a 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68 61  * turn means tha
346fd 74 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e  t the other conn
346fe 65 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20  ection has made 
346ff 75 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61 6e  uncommitted chan
34700 67 65 73 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65  ges..  ** to the
34701 20 73 63 68 65 6d 61 2e 0d 0a 20 20 2a 2a 0d 0a   schema...  **..
34702 20 20 2a 2a 20 57 65 72 65 20 77 65 20 74 6f 20    ** Were we to 
34703 70 72 6f 63 65 65 64 20 61 6e 64 20 70 72 65 70  proceed and prep
34704 61 72 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  are the statemen
34705 74 20 61 67 61 69 6e 73 74 20 74 68 65 20 75 6e  t against the un
34706 63 6f 6d 6d 69 74 74 65 64 0d 0a 20 20 2a 2a 20  committed..  ** 
34707 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61  schema changes a
34708 6e 64 20 69 66 20 74 68 6f 73 65 20 73 63 68 65  nd if those sche
34709 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20 73  ma changes are s
3470a 75 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c  ubsequently roll
3470b 65 64 0d 0a 20 20 2a 2a 20 62 61 63 6b 20 61 6e  ed..  ** back an
3470c 64 20 64 69 66 66 65 72 65 6e 74 20 63 68 61 6e  d different chan
3470d 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e 20  ges are made in 
3470e 74 68 65 69 72 20 70 6c 61 63 65 2c 20 74 68 65  their place, the
3470f 6e 20 77 68 65 6e 20 74 68 69 73 0d 0a 20 20 2a  n when this..  *
34710 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  * prepared state
34711 6d 65 6e 74 20 67 6f 65 73 20 74 6f 20 72 75 6e  ment goes to run
34712 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
34713 69 65 20 77 6f 75 6c 64 20 66 61 69 6c 20 74 6f  ie would fail to
34714 20 64 65 74 65 63 74 0d 0a 20 20 2a 2a 20 74 68   detect..  ** th
34715 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  e schema change.
34716 20 20 44 69 73 61 73 74 65 72 20 77 6f 75 6c 64    Disaster would
34717 20 66 6f 6c 6c 6f 77 2e 0d 0a 20 20 2a 2a 0d 0a   follow...  **..
34718 20 20 2a 2a 20 54 68 69 73 20 74 68 72 65 61 64    ** This thread
34719 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 6f   is currently ho
3471a 6c 64 69 6e 67 20 6d 75 74 65 78 65 73 20 6f 6e  lding mutexes on
3471b 20 61 6c 6c 20 42 74 72 65 65 73 20 28 62 65 63   all Btrees (bec
3471c 61 75 73 65 0d 0a 20 20 2a 2a 20 6f 66 20 74 68  ause..  ** of th
3471d 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  e sqlite3BtreeEn
3471e 74 65 72 41 6c 6c 28 29 20 69 6e 20 73 71 6c 69  terAll() in sqli
3471f 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72  te3LockAndPrepar
34720 65 28 29 29 20 73 6f 20 69 74 0d 0a 20 20 2a 2a  e()) so it..  **
34721 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
34722 20 66 6f 72 20 61 6e 6f 74 68 65 72 20 74 68 72   for another thr
34723 65 61 64 20 74 6f 20 73 74 61 72 74 20 61 20 6e  ead to start a n
34724 65 77 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ew schema change
34725 0d 0a 20 20 2a 2a 20 77 68 69 6c 65 20 74 68 69  ..  ** while thi
34726 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e  s routine is run
34727 6e 69 6e 67 2e 20 20 48 65 6e 63 65 2c 20 77 65  ning.  Hence, we
34728 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
34729 68 6f 6c 64 20 0d 0a 20 20 2a 2a 20 6c 6f 63 6b  hold ..  ** lock
3472a 73 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 2c  s on the schema,
3472b 20 77 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f   we just need to
3472c 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 62 6f 64   make sure nobod
3472d 79 20 65 6c 73 65 20 69 73 20 0d 0a 20 20 2a 2a  y else is ..  **
3472e 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d 2e 0d 0a   holding them...
3472f 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f 74 65 20    **..  ** Note 
34730 74 68 61 74 20 73 65 74 74 69 6e 67 20 52 45 41  that setting REA
34731 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f 76  D_UNCOMMITTED ov
34732 65 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f 63  errides most loc
34733 6b 20 64 65 74 65 63 74 69 6f 6e 2c 0d 0a 20 20  k detection,..  
34734 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a  ** but it does *
34735 6e 6f 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63  not* override sc
34736 68 65 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74  hema lock detect
34737 69 6f 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c  ion, so this all
34738 20 73 74 69 6c 6c 0d 0a 20 20 2a 2a 20 77 6f 72   still..  ** wor
34739 6b 73 20 65 76 65 6e 20 69 66 20 52 45 41 44 5f  ks even if READ_
3473a 55 4e 43 4f 4d 4d 49 54 54 45 44 20 69 73 20 73  UNCOMMITTED is s
3473b 65 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72  et...  */..  for
3473c 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
3473d 20 69 2b 2b 29 20 7b 0d 0a 20 20 20 20 42 74 72   i++) {..    Btr
3473e 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
3473f 62 5b 69 5d 2e 70 42 74 3b 0d 0a 20 20 20 20 69  b[i].pBt;..    i
34740 66 28 20 70 42 74 20 29 7b 0d 0a 20 20 20 20 20  f( pBt ){..     
34741 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
34742 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
34743 70 42 74 29 20 29 3b 0d 0a 20 20 20 20 20 20 72  pBt) );..      r
34744 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
34745 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74  SchemaLocked(pBt
34746 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
34747 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e   ){..        con
34748 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  st char *zDb = d
34749 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b  b->aDb[i].zName;
3474a 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
3474b 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
3474c 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
3474d 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20  is locked: %s", 
3474e 7a 44 62 29 3b 0d 0a 20 20 20 20 20 20 20 20 74  zDb);..        t
3474f 65 73 74 63 61 73 65 28 20 64 62 2d 3e 66 6c 61  estcase( db->fla
34750 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64  gs & SQLITE_Read
34751 55 6e 63 6f 6d 6d 69 74 74 65 64 20 29 3b 0d 0a  Uncommitted );..
34752 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
34753 5f 70 72 65 70 61 72 65 3b 0d 0a 20 20 20 20 20  _prepare;..     
34754 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
34755 0d 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55  ..  sqlite3VtabU
34756 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0d 0a  nlockList(db);..
34757 0d 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d  ..  pParse->db =
34758 20 64 62 3b 0d 0a 20 20 70 50 61 72 73 65 2d 3e   db;..  pParse->
34759 6e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 28 64 6f  nQueryLoop = (do
3475a 75 62 6c 65 29 31 3b 0d 0a 20 20 69 66 28 20 6e  uble)1;..  if( n
3475b 42 79 74 65 73 3e 3d 30 20 26 26 20 28 6e 42 79  Bytes>=0 && (nBy
3475c 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b 6e  tes==0 || zSql[n
3475d 42 79 74 65 73 2d 31 5d 21 3d 30 29 20 29 7b 0d  Bytes-1]!=0) ){.
3475e 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 43  .    char *zSqlC
3475f 6f 70 79 3b 0d 0a 20 20 20 20 69 6e 74 20 6d 78  opy;..    int mx
34760 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  Len = db->aLimit
34761 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  [SQLITE_LIMIT_SQ
34762 4c 5f 4c 45 4e 47 54 48 5d 3b 0d 0a 20 20 20 20  L_LENGTH];..    
34763 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 73  testcase( nBytes
34764 3d 3d 6d 78 4c 65 6e 20 29 3b 0d 0a 20 20 20 20  ==mxLen );..    
34765 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 73  testcase( nBytes
34766 3d 3d 6d 78 4c 65 6e 2b 31 20 29 3b 0d 0a 20 20  ==mxLen+1 );..  
34767 20 20 69 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c    if( nBytes>mxL
34768 65 6e 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  en ){..      sql
34769 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
3476a 4c 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74  LITE_TOOBIG, "st
3476b 61 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67  atement too long
3476c 22 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  ");..      rc = 
3476d 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
3476e 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
3476f 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65  );..      goto e
34770 6e 64 5f 70 72 65 70 61 72 65 3b 0d 0a 20 20 20  nd_prepare;..   
34771 20 7d 0d 0a 20 20 20 20 7a 53 71 6c 43 6f 70 79   }..    zSqlCopy
34772 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
34773 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42  Dup(db, zSql, nB
34774 79 74 65 73 29 3b 0d 0a 20 20 20 20 69 66 28 20  ytes);..    if( 
34775 7a 53 71 6c 43 6f 70 79 20 29 7b 0d 0a 20 20 20  zSqlCopy ){..   
34776 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72     sqlite3RunPar
34777 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c  ser(pParse, zSql
34778 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67 29 3b  Copy, &zErrMsg);
34779 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
3477a 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 43 6f  bFree(db, zSqlCo
3477b 70 79 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 72  py);..      pPar
3477c 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71  se->zTail = &zSq
3477d 6c 5b 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d  l[pParse->zTail-
3477e 7a 53 71 6c 43 6f 70 79 5d 3b 0d 0a 20 20 20 20  zSqlCopy];..    
3477f 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 50  }else{..      pP
34780 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a  arse->zTail = &z
34781 53 71 6c 5b 6e 42 79 74 65 73 5d 3b 0d 0a 20 20  Sql[nBytes];..  
34782 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20    }..  }else{.. 
34783 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72     sqlite3RunPar
34784 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c  ser(pParse, zSql
34785 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20  , &zErrMsg);..  
34786 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 31 3d 3d  }..  assert( 1==
34787 28 69 6e 74 29 70 50 61 72 73 65 2d 3e 6e 51 75  (int)pParse->nQu
34788 65 72 79 4c 6f 6f 70 20 29 3b 0d 0a 0d 0a 20 20  eryLoop );....  
34789 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
3478a 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 70 50 61  iled ){..    pPa
3478b 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
3478c 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20  _NOMEM;..  }..  
3478d 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d  if( pParse->rc==
3478e 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 70 50  SQLITE_DONE ) pP
3478f 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
34790 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 50 61  E_OK;..  if( pPa
34791 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61  rse->checkSchema
34792 20 29 7b 0d 0a 20 20 20 20 73 63 68 65 6d 61 49   ){..    schemaI
34793 73 56 61 6c 69 64 28 70 50 61 72 73 65 29 3b 0d  sValid(pParse);.
34794 0a 20 20 7d 0d 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
34795 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
34796 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
34797 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
34798 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 7a 54 61  .  }..  if( pzTa
34799 69 6c 20 29 7b 0d 0a 20 20 20 20 2a 70 7a 54 61  il ){..    *pzTa
3479a 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e 7a 54 61  il = pParse->zTa
3479b 69 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d  il;..  }..  rc =
3479c 20 70 50 61 72 73 65 2d 3e 72 63 3b 0d 0a 0d 0a   pParse->rc;....
3479d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3479e 4d 49 54 5f 45 58 50 4c 41 49 4e 0d 0a 20 20 69  MIT_EXPLAIN..  i
3479f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
347a0 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62   && pParse->pVdb
347a1 65 20 26 26 20 70 50 61 72 73 65 2d 3e 65 78 70  e && pParse->exp
347a2 6c 61 69 6e 20 29 7b 0d 0a 20 20 20 20 73 74 61  lain ){..    sta
347a3 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
347a4 20 63 6f 6e 73 74 20 61 7a 43 6f 6c 4e 61 6d 65   const azColName
347a5 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20 22  [] = {..       "
347a6 61 64 64 72 22 2c 20 22 6f 70 63 6f 64 65 22 2c  addr", "opcode",
347a7 20 22 70 31 22 2c 20 22 70 32 22 2c 20 22 70 33   "p1", "p2", "p3
347a8 22 2c 20 22 70 34 22 2c 20 22 70 35 22 2c 20 22  ", "p4", "p5", "
347a9 63 6f 6d 6d 65 6e 74 22 2c 0d 0a 20 20 20 20 20  comment",..     
347aa 20 20 22 73 65 6c 65 63 74 69 64 22 2c 20 22 6f    "selectid", "o
347ab 72 64 65 72 22 2c 20 22 66 72 6f 6d 22 2c 20 22  rder", "from", "
347ac 64 65 74 61 69 6c 22 0d 0a 20 20 20 20 7d 3b 0d  detail"..    };.
347ad 0a 20 20 20 20 69 6e 74 20 69 46 69 72 73 74 2c  .    int iFirst,
347ae 20 6d 78 3b 0d 0a 20 20 20 20 69 66 28 20 70 50   mx;..    if( pP
347af 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  arse->explain==2
347b0 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
347b1 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
347b2 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
347b3 34 29 3b 0d 0a 20 20 20 20 20 20 69 46 69 72 73  4);..      iFirs
347b4 74 20 3d 20 38 3b 0d 0a 20 20 20 20 20 20 6d 78  t = 8;..      mx
347b5 20 3d 20 31 32 3b 0d 0a 20 20 20 20 7d 65 6c 73   = 12;..    }els
347b6 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e{..      sqlite
347b7 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
347b8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 38  pParse->pVdbe, 8
347b9 29 3b 0d 0a 20 20 20 20 20 20 69 46 69 72 73 74  );..      iFirst
347ba 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 6d 78 20   = 0;..      mx 
347bb 3d 20 38 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  = 8;..    }..   
347bc 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69   for(i=iFirst; i
347bd 3c 6d 78 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  <mx; i++){..    
347be 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
347bf 43 6f 6c 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e  ColName(pParse->
347c0 70 56 64 62 65 2c 20 69 2d 69 46 69 72 73 74 2c  pVdbe, i-iFirst,
347c1 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 0d 0a   COLNAME_NAME,..
347c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
347c3 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f              azCo
347c4 6c 4e 61 6d 65 5b 69 5d 2c 20 53 51 4c 49 54 45  lName[i], SQLITE
347c5 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 7d  _STATIC);..    }
347c6 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
347c7 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69  .  assert( db->i
347c8 6e 69 74 2e 62 75 73 79 3d 3d 30 20 7c 7c 20 73  nit.busy==0 || s
347c9 61 76 65 53 71 6c 46 6c 61 67 3d 3d 30 20 29 3b  aveSqlFlag==0 );
347ca 0d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74  ..  if( db->init
347cb 2e 62 75 73 79 3d 3d 30 20 29 7b 0d 0a 20 20 20  .busy==0 ){..   
347cc 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 70   Vdbe *pVdbe = p
347cd 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d 0a 20  Parse->pVdbe;.. 
347ce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
347cf 74 53 71 6c 28 70 56 64 62 65 2c 20 7a 53 71 6c  tSql(pVdbe, zSql
347d0 2c 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d 3e  , (int)(pParse->
347d1 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73 61 76  zTail-zSql), sav
347d2 65 53 71 6c 46 6c 61 67 29 3b 0d 0a 20 20 7d 0d  eSqlFlag);..  }.
347d3 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70  .  if( pParse->p
347d4 56 64 62 65 20 26 26 20 28 72 63 21 3d 53 51 4c  Vdbe && (rc!=SQL
347d5 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61  ITE_OK || db->ma
347d6 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0d 0a  llocFailed) ){..
347d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
347d8 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e  inalize(pParse->
347d9 70 56 64 62 65 29 3b 0d 0a 20 20 20 20 61 73 73  pVdbe);..    ass
347da 65 72 74 28 21 28 2a 70 70 53 74 6d 74 29 29 3b  ert(!(*ppStmt));
347db 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
347dc 2a 70 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74  *ppStmt = (sqlit
347dd 65 33 5f 73 74 6d 74 2a 29 70 50 61 72 73 65 2d  e3_stmt*)pParse-
347de 3e 70 56 64 62 65 3b 0d 0a 20 20 7d 0d 0a 0d 0a  >pVdbe;..  }....
347df 20 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29 7b    if( zErrMsg ){
347e0 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
347e1 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c  or(db, rc, "%s",
347e2 20 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20   zErrMsg);..    
347e3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
347e4 2c 20 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 7d  , zErrMsg);..  }
347e5 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74  else{..    sqlit
347e6 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
347e7 30 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  0);..  }....  /*
347e8 20 44 65 6c 65 74 65 20 61 6e 79 20 54 72 69 67   Delete any Trig
347e9 67 65 72 50 72 67 20 73 74 72 75 63 74 75 72 65  gerPrg structure
347ea 73 20 61 6c 6c 6f 63 61 74 65 64 20 77 68 69 6c  s allocated whil
347eb 65 20 70 61 72 73 69 6e 67 20 74 68 69 73 20 73  e parsing this s
347ec 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 20 20  tatement. */..  
347ed 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70  while( pParse->p
347ee 54 72 69 67 67 65 72 50 72 67 20 29 7b 0d 0a 20  TriggerPrg ){.. 
347ef 20 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70     TriggerPrg *p
347f0 54 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69  T = pParse->pTri
347f1 67 67 65 72 50 72 67 3b 0d 0a 20 20 20 20 70 50  ggerPrg;..    pP
347f2 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72  arse->pTriggerPr
347f3 67 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b 0d 0a  g = pT->pNext;..
347f4 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
347f5 65 28 64 62 2c 20 70 54 29 3b 0d 0a 20 20 7d 0d  e(db, pT);..  }.
347f6 0a 0d 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0d  ...end_prepare:.
347f7 0a 0d 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63  ...  sqlite3Stac
347f8 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  kFree(db, pParse
347f9 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  );..  rc = sqlit
347fa 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
347fb 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28 72  );..  assert( (r
347fc 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
347fd 72 63 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  rc );..  return 
347fe 72 63 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69  rc;..}..static i
347ff 6e 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  nt sqlite3LockAn
34800 64 50 72 65 70 61 72 65 28 0d 0a 20 20 73 71 6c  dPrepare(..  sql
34801 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
34802 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
34803 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0d 0a 20  se handle. */.. 
34804 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
34805 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
34806 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
34807 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 20  statement. */.. 
34808 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
34809 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
3480a 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
3480b 62 79 74 65 73 2e 20 2a 2f 0d 0a 20 20 69 6e 74  bytes. */..  int
3480c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20   saveSqlFlag,   
3480d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
3480e 6f 20 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20  o copy SQL text 
3480f 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  into the sqlite3
34810 5f 73 74 6d 74 20 2a 2f 0d 0a 20 20 56 64 62 65  _stmt */..  Vdbe
34811 20 2a 70 4f 6c 64 2c 20 20 20 20 20 20 20 20 20   *pOld,         
34812 20 20 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e        /* VM bein
34813 67 20 72 65 70 72 65 70 61 72 65 64 20 2a 2f 0d  g reprepared */.
34814 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
34815 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
34816 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
34817 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
34818 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 63  tatement */..  c
34819 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
3481a 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
3481b 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
3481c 74 72 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  tring */..){..  
3481d 69 6e 74 20 72 63 3b 0d 0a 20 20 61 73 73 65 72  int rc;..  asser
3481e 74 28 20 70 70 53 74 6d 74 21 3d 30 20 29 3b 0d  t( ppStmt!=0 );.
3481f 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0d  .  *ppStmt = 0;.
34820 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
34821 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
34822 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
34823 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
34824 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  PT;..  }..  sqli
34825 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34826 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 73  db->mutex);..  s
34827 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
34828 41 6c 6c 28 64 62 29 3b 0d 0a 20 20 72 63 20 3d  All(db);..  rc =
34829 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28   sqlite3Prepare(
3482a 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73  db, zSql, nBytes
3482b 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 70  , saveSqlFlag, p
3482c 4f 6c 64 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54  Old, ppStmt, pzT
3482d 61 69 6c 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d  ail);..  if( rc=
3482e 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
3482f 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  {..    sqlite3_f
34830 69 6e 61 6c 69 7a 65 28 2a 70 70 53 74 6d 74 29  inalize(*ppStmt)
34831 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
34832 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
34833 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
34834 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c 64 2c 20  eSqlFlag, pOld, 
34835 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b  ppStmt, pzTail);
34836 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
34837 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
34838 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
34839 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
3483a 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  tex);..  return 
3483b 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
3483c 20 52 65 72 75 6e 20 74 68 65 20 63 6f 6d 70 69   Rerun the compi
3483d 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  lation of a stat
3483e 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63  ement after a sc
3483f 68 65 6d 61 20 63 68 61 6e 67 65 2e 0d 0a 2a 2a  hema change...**
34840 0d 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74  ..** If the stat
34841 65 6d 65 6e 74 20 69 73 20 73 75 63 63 65 73 73  ement is success
34842 66 75 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64  fully recompiled
34843 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
34844 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 0d 0a  OK. Otherwise,..
34845 2a 2a 20 69 66 20 74 68 65 20 73 74 61 74 65 6d  ** if the statem
34846 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ent cannot be re
34847 63 6f 6d 70 69 6c 65 64 20 62 65 63 61 75 73 65  compiled because
34848 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74   another connect
34849 69 6f 6e 20 68 61 73 0d 0a 2a 2a 20 6c 6f 63 6b  ion has..** lock
3484a 65 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d  ed the sqlite3_m
3484b 61 73 74 65 72 20 74 61 62 6c 65 2c 20 72 65 74  aster table, ret
3484c 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
3484d 44 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72 20  D. If any other 
3484e 65 72 72 6f 72 0d 0a 2a 2a 20 6f 63 63 75 72 73  error..** occurs
3484f 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
34850 53 43 48 45 4d 41 2e 0d 0a 2a 2f 0d 0a 53 51 4c  SCHEMA...*/..SQL
34851 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
34852 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
34853 28 56 64 62 65 20 2a 70 29 7b 0d 0a 20 20 69 6e  (Vdbe *p){..  in
34854 74 20 72 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33  t rc;..  sqlite3
34855 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0d 0a 20 20  _stmt *pNew;..  
34856 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
34857 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
34858 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73  ;....  assert( s
34859 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3485a 64 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  d(sqlite3VdbeDb(
3485b 70 29 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20  p)->mutex) );.. 
3485c 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
3485d 73 71 6c 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  sql((sqlite3_stm
3485e 74 20 2a 29 70 29 3b 0d 0a 20 20 61 73 73 65 72  t *)p);..  asser
3485f 74 28 20 7a 53 71 6c 21 3d 30 20 29 3b 20 20 2f  t( zSql!=0 );  /
34860 2a 20 52 65 70 72 65 70 61 72 65 20 6f 6e 6c 79  * Reprepare only
34861 20 63 61 6c 6c 65 64 20 66 6f 72 20 70 72 65 70   called for prep
34862 61 72 65 5f 76 32 28 29 20 73 74 61 74 65 6d 65  are_v2() stateme
34863 6e 74 73 20 2a 2f 0d 0a 20 20 64 62 20 3d 20 73  nts */..  db = s
34864 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 3b  qlite3VdbeDb(p);
34865 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
34866 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
34867 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20  b->mutex) );..  
34868 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
34869 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a  AndPrepare(db, z
3486a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 70 2c 20 26  Sql, -1, 0, p, &
3486b 70 4e 65 77 2c 20 30 29 3b 0d 0a 20 20 69 66 28  pNew, 0);..  if(
3486c 20 72 63 20 29 7b 0d 0a 20 20 20 20 69 66 28 20   rc ){..    if( 
3486d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
3486e 20 29 7b 0d 0a 20 20 20 20 20 20 64 62 2d 3e 6d   ){..      db->m
3486f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
34870 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73  ..    }..    ass
34871 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0d  ert( pNew==0 );.
34872 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  .    return rc;.
34873 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61  .  }else{..    a
34874 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29  ssert( pNew!=0 )
34875 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
34876 33 56 64 62 65 53 77 61 70 28 28 56 64 62 65 2a  3VdbeSwap((Vdbe*
34877 29 70 4e 65 77 2c 20 70 29 3b 0d 0a 20 20 73 71  )pNew, p);..  sq
34878 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
34879 64 69 6e 67 73 28 70 4e 65 77 2c 20 28 73 71 6c  dings(pNew, (sql
3487a 69 74 65 33 5f 73 74 6d 74 2a 29 70 29 3b 0d 0a  ite3_stmt*)p);..
3487b 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
3487c 65 74 53 74 65 70 52 65 73 75 6c 74 28 28 56 64  etStepResult((Vd
3487d 62 65 2a 29 70 4e 65 77 29 3b 0d 0a 20 20 73 71  be*)pNew);..  sq
3487e 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
3487f 65 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0d  e((Vdbe*)pNew);.
34880 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
34881 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  _OK;..}....../*.
34882 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73  .** Two versions
34883 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c   of the official
34884 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e   API.  Legacy an
34885 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74  d new use.  In t
34886 68 65 20 6c 65 67 61 63 79 0d 0a 2a 2a 20 76 65  he legacy..** ve
34887 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69  rsion, the origi
34888 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20  nal SQL text is 
34889 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68 65  not saved in the
3488a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
3488b 65 6e 74 0d 0a 2a 2a 20 61 6e 64 20 73 6f 20 69  ent..** and so i
3488c 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  f a schema chang
3488d 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45  e occurs, SQLITE
3488e 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75 72  _SCHEMA is retur
3488f 6e 65 64 20 62 79 0d 0a 2a 2a 20 73 71 6c 69 74  ned by..** sqlit
34890 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74  e3_step().  In t
34891 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20  he new version, 
34892 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
34893 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65   text is retaine
34894 64 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74  d..** and the st
34895 61 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d  atement is autom
34896 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
34897 6c 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61  led if an schema
34898 20 63 68 61 6e 67 65 0d 0a 2a 2a 20 6f 63 63 75   change..** occu
34899 72 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  rs...*/..SQLITE_
3489a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3489b 70 72 65 70 61 72 65 28 0d 0a 20 20 73 71 6c 69  prepare(..  sqli
3489c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
3489d 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
3489e 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0d 0a 20 20  e handle. */..  
3489f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
348a0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
348a1 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
348a2 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 20 20  tatement. */..  
348a3 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
348a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
348a5 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
348a6 79 74 65 73 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69  ytes. */..  sqli
348a7 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
348a8 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
348a9 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
348aa 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
348ab 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  t */..  const ch
348ac 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
348ad 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
348ae 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
348af 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  /..){..  int rc;
348b0 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
348b1 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
348b2 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c  b,zSql,nBytes,0,
348b3 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  0,ppStmt,pzTail)
348b4 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  ;..  assert( rc=
348b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70  =SQLITE_OK || pp
348b6 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74  Stmt==0 || *ppSt
348b7 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52  mt==0 );  /* VER
348b8 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0d 0a  IFY: F13021 */..
348b9 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
348ba 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
348bb 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
348bc 76 32 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  v2(..  sqlite3 *
348bd 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
348be 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
348bf 64 6c 65 2e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  dle. */..  const
348c0 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
348c1 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
348c2 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
348c3 65 6e 74 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ent. */..  int n
348c4 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
348c5 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
348c6 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
348c7 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73   */..  sqlite3_s
348c8 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
348c9 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
348ca 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
348cb 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d  ed statement */.
348cc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
348cd 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
348ce 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
348cf 65 64 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 29 7b  ed string */..){
348d0 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 72  ..  int rc;..  r
348d1 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
348d2 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71  ndPrepare(db,zSq
348d3 6c 2c 6e 42 79 74 65 73 2c 31 2c 30 2c 70 70 53  l,nBytes,1,0,ppS
348d4 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0d 0a 20 20  tmt,pzTail);..  
348d5 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
348d6 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
348d7 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
348d8 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
348d9 46 31 33 30 32 31 20 2a 2f 0d 0a 20 20 72 65 74  F13021 */..  ret
348da 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  urn rc;..}......
348db 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
348dc 4d 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a  MIT_UTF16../*..*
348dd 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55 54  * Compile the UT
348de 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51 4c  F-16 encoded SQL
348df 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20   statement zSql 
348e0 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  into a statement
348e1 20 68 61 6e 64 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74   handle...*/..st
348e2 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
348e3 50 72 65 70 61 72 65 31 36 28 0d 0a 20 20 73 71  Prepare16(..  sq
348e4 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
348e5 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
348e6 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0d  ase handle. */ .
348e7 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
348e8 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
348e9 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53  UTF-16 encoded S
348ea 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
348eb 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  ..  int nBytes, 
348ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
348ed 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
348ee 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0d 0a 20 20  in bytes. */..  
348ef 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
348f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
348f1 65 20 74 6f 20 73 61 76 65 20 53 51 4c 20 74 65  e to save SQL te
348f2 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
348f3 74 65 33 5f 73 74 6d 74 20 2a 2f 0d 0a 20 20 73  te3_stmt */..  s
348f4 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
348f5 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
348f6 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
348f7 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
348f8 6d 65 6e 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ment */..  const
348f9 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
348fa 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
348fb 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e   of parsed strin
348fc 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 2f 2a 20 54  g */..){..  /* T
348fd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 75 72  his function cur
348fe 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62 79 20  rently works by 
348ff 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72 6d 69  first transformi
34900 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0d 0a 20  ng the UTF-16.. 
34901 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72 69   ** encoded stri
34902 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68 65  ng to UTF-8, the
34903 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  n invoking sqlit
34904 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54 68  e3_prepare(). Th
34905 65 0d 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62  e..  ** tricky b
34906 69 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f  it is figuring o
34907 75 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ut the pointer t
34908 6f 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54  o return in *pzT
34909 61 69 6c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 63 68  ail...  */..  ch
3490a 61 72 20 2a 7a 53 71 6c 38 3b 0d 0a 20 20 63 6f  ar *zSql8;..  co
3490b 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69 6c 38  nst char *zTail8
3490c 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 72 63 20   = 0;..  int rc 
3490d 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a  = SQLITE_OK;....
3490e 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d 74    assert( ppStmt
3490f 20 29 3b 0d 0a 20 20 2a 70 70 53 74 6d 74 20 3d   );..  *ppStmt =
34910 20 30 3b 0d 0a 20 20 69 66 28 20 21 73 71 6c 69   0;..  if( !sqli
34911 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b  te3SafetyCheckOk
34912 28 64 62 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  (db) ){..    ret
34913 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
34914 45 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20  E_BKPT;..  }..  
34915 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
34916 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ter(db->mutex);.
34917 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74  .  zSql8 = sqlit
34918 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a  e3Utf16to8(db, z
34919 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 53 51 4c  Sql, nBytes, SQL
3491a 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
3491b 3b 0d 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29  ;..  if( zSql8 )
3491c 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  {..    rc = sqli
3491d 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72  te3LockAndPrepar
3491e 65 28 64 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c  e(db, zSql8, -1,
3491f 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 30 2c   saveSqlFlag, 0,
34920 20 70 70 53 74 6d 74 2c 20 26 7a 54 61 69 6c 38   ppStmt, &zTail8
34921 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  );..  }....  if(
34922 20 7a 54 61 69 6c 38 20 26 26 20 70 7a 54 61 69   zTail8 && pzTai
34923 6c 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49 66 20  l ){..    /* If 
34924 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20  sqlite3_prepare 
34925 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c 20 70  returns a tail p
34926 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c 63 75  ointer, we calcu
34927 6c 61 74 65 20 74 68 65 0d 0a 20 20 20 20 2a 2a  late the..    **
34928 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69 6e   equivalent poin
34929 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ter into the UTF
3492a 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63 6f  -16 string by co
3492b 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63 6f  unting the unico
3492c 64 65 0d 0a 20 20 20 20 2a 2a 20 63 68 61 72 61  de..    ** chara
3492d 63 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53  cters between zS
3492e 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20  ql8 and zTail8, 
3492f 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69  and then returni
34930 6e 67 20 61 20 70 6f 69 6e 74 65 72 0d 0a 20 20  ng a pointer..  
34931 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75    ** the same nu
34932 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
34933 72 73 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d  rs into the UTF-
34934 31 36 20 73 74 72 69 6e 67 2e 0d 0a 20 20 20 20  16 string...    
34935 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 63 68 61 72  */..    int char
34936 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c 69 74  s_parsed = sqlit
34937 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 53  e3Utf8CharLen(zS
34938 71 6c 38 2c 20 28 69 6e 74 29 28 7a 54 61 69 6c  ql8, (int)(zTail
34939 38 2d 7a 53 71 6c 38 29 29 3b 0d 0a 20 20 20 20  8-zSql8));..    
3493a 2a 70 7a 54 61 69 6c 20 3d 20 28 75 38 20 2a 29  *pzTail = (u8 *)
3493b 7a 53 71 6c 20 2b 20 73 71 6c 69 74 65 33 55 74  zSql + sqlite3Ut
3493c 66 31 36 42 79 74 65 4c 65 6e 28 7a 53 71 6c 2c  f16ByteLen(zSql,
3493d 20 63 68 61 72 73 5f 70 61 72 73 65 64 29 3b 0d   chars_parsed);.
3493e 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 44  .  }..  sqlite3D
3493f 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 38 29  bFree(db, zSql8)
34940 3b 20 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ; ..  rc = sqlit
34941 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
34942 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
34943 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
34944 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  tex);..  return 
34945 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
34946 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66   Two versions of
34947 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50   the official AP
34948 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e  I.  Legacy and n
34949 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20  ew use.  In the 
3494a 6c 65 67 61 63 79 0d 0a 2a 2a 20 76 65 72 73 69  legacy..** versi
3494b 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
3494c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74   SQL text is not
3494d 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70 72   saved in the pr
3494e 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3494f 0d 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  ..** and so if a
34950 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
34951 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
34952 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
34953 20 62 79 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   by..** sqlite3_
34954 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20  step().  In the 
34955 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  new version, the
34956 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
34957 78 74 20 69 73 20 72 65 74 61 69 6e 65 64 0d 0a  xt is retained..
34958 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74 65  ** and the state
34959 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69  ment is automati
3495a 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64  cally recompiled
3495b 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68   if an schema ch
3495c 61 6e 67 65 0d 0a 2a 2a 20 6f 63 63 75 72 73 2e  ange..** occurs.
3495d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
3495e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
3495f 70 61 72 65 31 36 28 0d 0a 20 20 73 71 6c 69 74  pare16(..  sqlit
34960 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
34961 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
34962 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0d 0a 20 20   handle. */ ..  
34963 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
34964 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
34965 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  -16 encoded SQL 
34966 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0d 0a 20  statement. */.. 
34967 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
34968 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
34969 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
3496a 62 79 74 65 73 2e 20 2a 2f 0d 0a 20 20 73 71 6c  bytes. */..  sql
3496b 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
3496c 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
3496d 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3496e 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3496f 6e 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 76  nt */..  const v
34970 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
34971 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
34972 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
34973 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
34974 3b 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
34975 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53  3Prepare16(db,zS
34976 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74  ql,nBytes,0,ppSt
34977 6d 74 2c 70 7a 54 61 69 6c 29 3b 0d 0a 20 20 61  mt,pzTail);..  a
34978 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
34979 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d  E_OK || ppStmt==
3497a 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20  0 || *ppStmt==0 
3497b 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46  );  /* VERIFY: F
3497c 31 33 30 32 31 20 2a 2f 0d 0a 20 20 72 65 74 75  13021 */..  retu
3497d 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 53 51 4c 49 54  rn rc;..}..SQLIT
3497e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3497f 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 0d  3_prepare16_v2(.
34980 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
34981 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34982 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
34983 20 2a 2f 20 0d 0a 20 20 63 6f 6e 73 74 20 76 6f   */ ..  const vo
34984 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  id *zSql,       
34985 20 20 2f 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f    /* UTF-16 enco
34986 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
34987 74 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79  t. */..  int nBy
34988 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
34989 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
3498a 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
3498b 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  /..  sqlite3_stm
3498c 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
3498d 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
3498e 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
3498f 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20   statement */.. 
34990 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
34991 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
34992 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
34993 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a   string */..){..
34994 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 72 63 20    int rc;..  rc 
34995 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  = sqlite3Prepare
34996 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  16(db,zSql,nByte
34997 73 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  s,1,ppStmt,pzTai
34998 6c 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 72  l);..  assert( r
34999 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
3499a 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70  ppStmt==0 || *pp
3499b 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56  Stmt==0 );  /* V
3499c 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f  ERIFY: F13021 */
3499d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
3499e 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  }....#endif /* S
3499f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
349a0 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   */..../********
349a1 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72  ****** End of pr
349a2 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  epare.c ********
349a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a5 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
349a6 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
349a7 6c 65 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a  le select.c ****
349a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349aa 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20  ******/../*..** 
349ab 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
349ac 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  5..**..** The au
349ad 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
349ae 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
349af 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
349b0 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
349b1 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
349b2 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
349b3 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
349b4 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
349b5 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
349b6 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
349b7 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
349b8 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
349b9 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
349ba 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
349bb 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
349bc 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
349bd 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
349be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
349c3 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
349c4 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
349c5 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
349c6 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0d 0a   by the parser..
349c7 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c  ** to handle SEL
349c8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
349c9 6e 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d 0a 0d  n SQLite...*/...
349ca 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65  .../*..** Delete
349cb 20 61 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74   all the content
349cc 20 6f 66 20 61 20 53 65 6c 65 63 74 20 73 74 72   of a Select str
349cd 75 63 74 75 72 65 20 62 75 74 20 64 6f 20 6e 6f  ucture but do no
349ce 74 20 64 65 61 6c 6c 6f 63 61 74 65 0d 0a 2a 2a  t deallocate..**
349cf 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 72 75   the select stru
349d0 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0d 0a 2a  cture itself...*
349d1 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  /..static void c
349d2 6c 65 61 72 53 65 6c 65 63 74 28 73 71 6c 69 74  learSelect(sqlit
349d3 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
349d4 70 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78  p){..  sqlite3Ex
349d5 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
349d6 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a 20 20   p->pEList);..  
349d7 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
349d8 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 72 63  lete(db, p->pSrc
349d9 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70  );..  sqlite3Exp
349da 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
349db 57 68 65 72 65 29 3b 0d 0a 20 20 73 71 6c 69 74  Where);..  sqlit
349dc 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
349dd 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  (db, p->pGroupBy
349de 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70  );..  sqlite3Exp
349df 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
349e0 48 61 76 69 6e 67 29 3b 0d 0a 20 20 73 71 6c 69  Having);..  sqli
349e1 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
349e2 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  e(db, p->pOrderB
349e3 79 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  y);..  sqlite3Se
349e4 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
349e5 2d 3e 70 50 72 69 6f 72 29 3b 0d 0a 20 20 73 71  ->pPrior);..  sq
349e6 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
349e7 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0d  db, p->pLimit);.
349e8 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
349e9 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66  lete(db, p->pOff
349ea 73 65 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  set);..}..../*..
349eb 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
349ec 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63  SelectDest struc
349ed 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ture...*/..SQLIT
349ee 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
349ef 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
349f0 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 20  Init(SelectDest 
349f1 2a 70 44 65 73 74 2c 20 69 6e 74 20 65 44 65 73  *pDest, int eDes
349f2 74 2c 20 69 6e 74 20 69 50 61 72 6d 29 7b 0d 0a  t, int iParm){..
349f3 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 3d    pDest->eDest =
349f4 20 28 75 38 29 65 44 65 73 74 3b 0d 0a 20 20 70   (u8)eDest;..  p
349f5 44 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69 50  Dest->iParm = iP
349f6 61 72 6d 3b 0d 0a 20 20 70 44 65 73 74 2d 3e 61  arm;..  pDest->a
349f7 66 66 69 6e 69 74 79 20 3d 20 30 3b 0d 0a 20 20  ffinity = 0;..  
349f8 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b  pDest->iMem = 0;
349f9 0d 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20  ..  pDest->nMem 
349fa 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  = 0;..}....../*.
349fb 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
349fc 65 77 20 53 65 6c 65 63 74 20 73 74 72 75 63 74  ew Select struct
349fd 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ure and return a
349fe 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
349ff 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0d  ..** structure..
34a00 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
34a01 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
34a02 74 65 33 53 65 6c 65 63 74 4e 65 77 28 0d 0a 20  te3SelectNew(.. 
34a03 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
34a04 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
34a05 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  g context */..  
34a06 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
34a07 2c 20 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63  ,     /* which c
34a08 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64  olumns to includ
34a09 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  e in the result 
34a0a 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  */..  SrcList *p
34a0b 53 72 63 2c 20 20 20 20 20 20 20 20 2f 2a 20 74  Src,        /* t
34a0c 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d  he FROM clause -
34a0d 2d 20 77 68 69 63 68 20 74 61 62 6c 65 73 20 74  - which tables t
34a0e 6f 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 45 78 70  o scan */..  Exp
34a0f 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
34a10 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20     /* the WHERE 
34a11 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 45 78 70  clause */..  Exp
34a12 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 2c  rList *pGroupBy,
34a13 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50 20     /* the GROUP 
34a14 42 59 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20  BY clause */..  
34a15 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20  Expr *pHaving,  
34a16 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56        /* the HAV
34a17 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20  ING clause */.. 
34a18 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
34a19 72 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52  rBy,   /* the OR
34a1a 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
34a1b 0d 0a 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e  ..  int isDistin
34a1c 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 72 75  ct,       /* tru
34a1d 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43  e if the DISTINC
34a1e 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  T keyword is pre
34a1f 73 65 6e 74 20 2a 2f 0d 0a 20 20 45 78 70 72 20  sent */..  Expr 
34a20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 20  *pLimit,        
34a21 20 2f 2a 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e   /* LIMIT value.
34a22 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74    NULL means not
34a23 20 75 73 65 64 20 2a 2f 0d 0a 20 20 45 78 70 72   used */..  Expr
34a24 20 2a 70 4f 66 66 73 65 74 20 20 20 20 20 20 20   *pOffset       
34a25 20 20 2f 2a 20 4f 46 46 53 45 54 20 76 61 6c 75    /* OFFSET valu
34a26 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e  e.  NULL means n
34a27 6f 20 6f 66 66 73 65 74 20 2a 2f 0d 0a 29 7b 0d  o offset */..){.
34a28 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b  .  Select *pNew;
34a29 0d 0a 20 20 53 65 6c 65 63 74 20 73 74 61 6e 64  ..  Select stand
34a2a 69 6e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  in;..  sqlite3 *
34a2b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
34a2c 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ..  pNew = sqlit
34a2d 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
34a2e 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
34a2f 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 64   );..  assert( d
34a30 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34a31 7c 7c 20 21 70 4f 66 66 73 65 74 20 7c 7c 20 70  || !pOffset || p
34a32 4c 69 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46 53  Limit ); /* OFFS
34a33 45 54 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49 54  ET implies LIMIT
34a34 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4e 65 77 3d   */..  if( pNew=
34a35 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72  =0 ){..    asser
34a36 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
34a37 6c 65 64 20 29 3b 0d 0a 20 20 20 20 70 4e 65 77  led );..    pNew
34a38 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0d 0a 20 20   = &standin;..  
34a39 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30    memset(pNew, 0
34a3a 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
34a3b 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 45  ;..  }..  if( pE
34a3c 4c 69 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  List==0 ){..    
34a3d 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
34a3e 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
34a3f 50 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65  Parse, 0, sqlite
34a40 33 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c  3Expr(db,TK_ALL,
34a41 30 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4e 65  0));..  }..  pNe
34a42 77 2d 3e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69  w->pEList = pELi
34a43 73 74 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70 53 72  st;..  pNew->pSr
34a44 63 20 3d 20 70 53 72 63 3b 0d 0a 20 20 70 4e 65  c = pSrc;..  pNe
34a45 77 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65  w->pWhere = pWhe
34a46 72 65 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70 47 72  re;..  pNew->pGr
34a47 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79  oupBy = pGroupBy
34a48 3b 0d 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69  ;..  pNew->pHavi
34a49 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b 0d 0a 20  ng = pHaving;.. 
34a4a 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20   pNew->pOrderBy 
34a4b 3d 20 70 4f 72 64 65 72 42 79 3b 0d 0a 20 20 70  = pOrderBy;..  p
34a4c 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20  New->selFlags = 
34a4d 69 73 44 69 73 74 69 6e 63 74 20 3f 20 53 46 5f  isDistinct ? SF_
34a4e 44 69 73 74 69 6e 63 74 20 3a 20 30 3b 0d 0a 20  Distinct : 0;.. 
34a4f 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53   pNew->op = TK_S
34a50 45 4c 45 43 54 3b 0d 0a 20 20 70 4e 65 77 2d 3e  ELECT;..  pNew->
34a51 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b  pLimit = pLimit;
34a52 0d 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65  ..  pNew->pOffse
34a53 74 20 3d 20 70 4f 66 66 73 65 74 3b 0d 0a 20 20  t = pOffset;..  
34a54 61 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 3d  assert( pOffset=
34a55 3d 30 20 7c 7c 20 70 4c 69 6d 69 74 21 3d 30 20  =0 || pLimit!=0 
34a56 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  );..  pNew->addr
34a57 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31  OpenEphm[0] = -1
34a58 3b 0d 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  ;..  pNew->addrO
34a59 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b  penEphm[1] = -1;
34a5a 0d 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ..  pNew->addrOp
34a5b 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0d  enEphm[2] = -1;.
34a5c 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
34a5d 63 46 61 69 6c 65 64 20 29 20 7b 0d 0a 20 20 20  cFailed ) {..   
34a5e 20 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c   clearSelect(db,
34a5f 20 70 4e 65 77 29 3b 0d 0a 20 20 20 20 69 66 28   pNew);..    if(
34a60 20 70 4e 65 77 21 3d 26 73 74 61 6e 64 69 6e 20   pNew!=&standin 
34a61 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  ) sqlite3DbFree(
34a62 64 62 2c 20 70 4e 65 77 29 3b 0d 0a 20 20 20 20  db, pNew);..    
34a63 70 4e 65 77 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  pNew = 0;..  }el
34a64 73 65 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  se{..    assert(
34a65 20 70 4e 65 77 2d 3e 70 53 72 63 21 3d 30 20 7c   pNew->pSrc!=0 |
34a66 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  | pParse->nErr>0
34a67 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65   );..  }..  asse
34a68 72 74 28 20 70 4e 65 77 21 3d 26 73 74 61 6e 64  rt( pNew!=&stand
34a69 69 6e 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  in );..  return 
34a6a 70 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  pNew;..}..../*..
34a6b 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67 69  ** Delete the gi
34a6c 76 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63  ven Select struc
34a6d 74 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20  ture and all of 
34a6e 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65  its substructure
34a6f 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  s...*/..SQLITE_P
34a70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
34a71 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
34a72 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
34a73 65 63 74 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  ect *p){..  if( 
34a74 70 20 29 7b 0d 0a 20 20 20 20 63 6c 65 61 72 53  p ){..    clearS
34a75 65 6c 65 63 74 28 64 62 2c 20 70 29 3b 0d 0a 20  elect(db, p);.. 
34a76 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
34a77 28 64 62 2c 20 70 29 3b 0d 0a 20 20 7d 0d 0a 7d  (db, p);..  }..}
34a78 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65 6e  ..../*..** Given
34a79 20 31 20 74 6f 20 33 20 69 64 65 6e 74 69 66 69   1 to 3 identifi
34a7a 65 72 73 20 70 72 65 63 65 65 64 69 6e 67 20 74  ers preceeding t
34a7b 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c  he JOIN keyword,
34a7c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 0d 0a   determine the..
34a7d 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e  ** type of join.
34a7e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65    Return an inte
34a7f 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 61  ger constant tha
34a80 74 20 65 78 70 72 65 73 73 65 73 20 74 68 61 74  t expresses that
34a81 20 74 79 70 65 0d 0a 2a 2a 20 69 6e 20 74 65 72   type..** in ter
34a82 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ms of the follow
34a83 69 6e 67 20 62 69 74 20 76 61 6c 75 65 73 3a 0d  ing bit values:.
34a84 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 4a 54 5f 49  .**..**     JT_I
34a85 4e 4e 45 52 0d 0a 2a 2a 20 20 20 20 20 4a 54 5f  NNER..**     JT_
34a86 43 52 4f 53 53 0d 0a 2a 2a 20 20 20 20 20 4a 54  CROSS..**     JT
34a87 5f 4f 55 54 45 52 0d 0a 2a 2a 20 20 20 20 20 4a  _OUTER..**     J
34a88 54 5f 4e 41 54 55 52 41 4c 0d 0a 2a 2a 20 20 20  T_NATURAL..**   
34a89 20 20 4a 54 5f 4c 45 46 54 0d 0a 2a 2a 20 20 20    JT_LEFT..**   
34a8a 20 20 4a 54 5f 52 49 47 48 54 0d 0a 2a 2a 0d 0a    JT_RIGHT..**..
34a8b 2a 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65 72 20  ** A full outer 
34a8c 6a 6f 69 6e 20 69 73 20 74 68 65 20 63 6f 6d 62  join is the comb
34a8d 69 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f 4c 45  ination of JT_LE
34a8e 46 54 20 61 6e 64 20 4a 54 5f 52 49 47 48 54 2e  FT and JT_RIGHT.
34a8f 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 69  ..**..** If an i
34a90 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75 70 70  llegal or unsupp
34a91 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
34a92 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20 73 74  is seen, then st
34a93 69 6c 6c 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 61  ill return..** a
34a94 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74 20   join type, but 
34a95 70 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  put an error in 
34a96 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
34a97 74 75 72 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ture...*/..SQLIT
34a98 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
34a99 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 50 61  lite3JoinType(Pa
34a9a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
34a9b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70  en *pA, Token *p
34a9c 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0d 0a  B, Token *pC){..
34a9d 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20 3d    int jointype =
34a9e 20 30 3b 0d 0a 20 20 54 6f 6b 65 6e 20 2a 61 70   0;..  Token *ap
34a9f 41 6c 6c 5b 33 5d 3b 0d 0a 20 20 54 6f 6b 65 6e  All[3];..  Token
34aa0 20 2a 70 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   *p;..          
34aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34aa2 20 20 20 2f 2a 20 20 20 30 31 32 33 34 35 36 37     /*   01234567
34aa3 38 39 20 31 32 33 34 35 36 37 38 39 20 31 32 33  89 123456789 123
34aa4 34 35 36 37 38 39 20 31 32 33 20 2a 2f 0d 0a 20  456789 123 */.. 
34aa5 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
34aa6 61 72 20 7a 4b 65 79 54 65 78 74 5b 5d 20 3d 20  ar zKeyText[] = 
34aa7 22 6e 61 74 75 72 61 6c 65 66 74 6f 75 74 65 72  "naturaleftouter
34aa8 69 67 68 74 66 75 6c 6c 69 6e 6e 65 72 63 72 6f  ightfullinnercro
34aa9 73 73 22 3b 0d 0a 20 20 73 74 61 74 69 63 20 63  ss";..  static c
34aaa 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0d 0a 20  onst struct {.. 
34aab 20 20 20 75 38 20 69 3b 20 20 20 20 20 20 20 20     u8 i;        
34aac 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20  /* Beginning of 
34aad 6b 65 79 77 6f 72 64 20 74 65 78 74 20 69 6e 20  keyword text in 
34aae 7a 4b 65 79 54 65 78 74 5b 5d 20 2a 2f 0d 0a 20  zKeyText[] */.. 
34aaf 20 20 20 75 38 20 6e 43 68 61 72 3b 20 20 20 20     u8 nChar;    
34ab0 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
34ab1 20 6b 65 79 77 6f 72 64 20 69 6e 20 63 68 61 72   keyword in char
34ab2 61 63 74 65 72 73 20 2a 2f 0d 0a 20 20 20 20 75  acters */..    u
34ab3 38 20 63 6f 64 65 3b 20 20 20 20 20 2f 2a 20 4a  8 code;     /* J
34ab4 6f 69 6e 20 74 79 70 65 20 6d 61 73 6b 20 2a 2f  oin type mask */
34ab5 0d 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d  ..  } aKeyword[]
34ab6 20 3d 20 7b 0d 0a 20 20 20 20 2f 2a 20 6e 61 74   = {..    /* nat
34ab7 75 72 61 6c 20 2a 2f 20 7b 20 30 2c 20 20 37 2c  ural */ { 0,  7,
34ab8 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20 20 20   JT_NATURAL     
34ab9 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0d 0a 20             },.. 
34aba 20 20 20 2f 2a 20 6c 65 66 74 20 20 20 20 2a 2f     /* left    */
34abb 20 7b 20 36 2c 20 20 34 2c 20 4a 54 5f 4c 45 46   { 6,  4, JT_LEF
34abc 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 20  T|JT_OUTER      
34abd 20 20 20 20 7d 2c 0d 0a 20 20 20 20 2f 2a 20 6f      },..    /* o
34abe 75 74 65 72 20 20 20 2a 2f 20 7b 20 31 30 2c 20  uter   */ { 10, 
34abf 35 2c 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20  5, JT_OUTER     
34ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0d               },.
34ac1 0a 20 20 20 20 2f 2a 20 72 69 67 68 74 20 20 20  .    /* right   
34ac2 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 4a 54 5f 52  */ { 14, 5, JT_R
34ac3 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20  IGHT|JT_OUTER   
34ac4 20 20 20 20 20 20 7d 2c 0d 0a 20 20 20 20 2f 2a        },..    /*
34ac5 20 66 75 6c 6c 20 20 20 20 2a 2f 20 7b 20 31 39   full    */ { 19
34ac6 2c 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f  , 4, JT_LEFT|JT_
34ac7 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d  RIGHT|JT_OUTER }
34ac8 2c 0d 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 72 20  ,..    /* inner 
34ac9 20 20 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 4a 54    */ { 23, 5, JT
34aca 5f 49 4e 4e 45 52 20 20 20 20 20 20 20 20 20 20  _INNER          
34acb 20 20 20 20 20 20 20 20 7d 2c 0d 0a 20 20 20 20          },..    
34acc 2f 2a 20 63 72 6f 73 73 20 20 20 2a 2f 20 7b 20  /* cross   */ { 
34acd 32 38 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c  28, 5, JT_INNER|
34ace 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 20 20 20  JT_CROSS        
34acf 20 7d 2c 0d 0a 20 20 7d 3b 0d 0a 20 20 69 6e 74   },..  };..  int
34ad0 20 69 2c 20 6a 3b 0d 0a 20 20 61 70 41 6c 6c 5b   i, j;..  apAll[
34ad1 30 5d 20 3d 20 70 41 3b 0d 0a 20 20 61 70 41 6c  0] = pA;..  apAl
34ad2 6c 5b 31 5d 20 3d 20 70 42 3b 0d 0a 20 20 61 70  l[1] = pB;..  ap
34ad3 41 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0d 0a 20 20  All[2] = pC;..  
34ad4 66 6f 72 28 69 3d 30 3b 20 69 3c 33 20 26 26 20  for(i=0; i<3 && 
34ad5 61 70 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0d  apAll[i]; i++){.
34ad6 0a 20 20 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69  .    p = apAll[i
34ad7 5d 3b 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ];..    for(j=0;
34ad8 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 61 4b 65   j<ArraySize(aKe
34ad9 79 77 6f 72 64 29 3b 20 6a 2b 2b 29 7b 0d 0a 20  yword); j++){.. 
34ada 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 3d 3d 61       if( p->n==a
34adb 4b 65 79 77 6f 72 64 5b 6a 5d 2e 6e 43 68 61 72  Keyword[j].nChar
34adc 20 0d 0a 20 20 20 20 20 20 20 20 20 20 26 26 20   ..          && 
34add 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
34ade 28 63 68 61 72 2a 29 70 2d 3e 7a 2c 20 26 7a 4b  (char*)p->z, &zK
34adf 65 79 54 65 78 74 5b 61 4b 65 79 77 6f 72 64 5b  eyText[aKeyword[
34ae0 6a 5d 2e 69 5d 2c 20 70 2d 3e 6e 29 3d 3d 30 20  j].i], p->n)==0 
34ae1 29 7b 0d 0a 20 20 20 20 20 20 20 20 6a 6f 69 6e  ){..        join
34ae2 74 79 70 65 20 7c 3d 20 61 4b 65 79 77 6f 72 64  type |= aKeyword
34ae3 5b 6a 5d 2e 63 6f 64 65 3b 0d 0a 20 20 20 20 20  [j].code;..     
34ae4 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
34ae5 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 74   }..    }..    t
34ae6 65 73 74 63 61 73 65 28 20 6a 3d 3d 30 20 7c 7c  estcase( j==0 ||
34ae7 20 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c 7c   j==1 || j==2 ||
34ae8 20 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c 7c   j==3 || j==4 ||
34ae9 20 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29 3b   j==5 || j==6 );
34aea 0d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 72  ..    if( j>=Arr
34aeb 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29  aySize(aKeyword)
34aec 20 29 7b 0d 0a 20 20 20 20 20 20 6a 6f 69 6e 74   ){..      joint
34aed 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b  ype |= JT_ERROR;
34aee 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
34aef 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
34af0 28 0d 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70  (..     (jointyp
34af1 65 20 26 20 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54  e & (JT_INNER|JT
34af2 5f 4f 55 54 45 52 29 29 3d 3d 28 4a 54 5f 49 4e  _OUTER))==(JT_IN
34af3 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c  NER|JT_OUTER) ||
34af4 0d 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65  ..     (jointype
34af5 20 26 20 4a 54 5f 45 52 52 4f 52 29 21 3d 30 0d   & JT_ERROR)!=0.
34af6 0a 20 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74  .  ){..    const
34af7 20 63 68 61 72 20 2a 7a 53 70 20 3d 20 22 20 22   char *zSp = " "
34af8 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
34af9 42 21 3d 30 20 29 3b 0d 0a 20 20 20 20 69 66 28  B!=0 );..    if(
34afa 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70 2b 2b 3b   pC==0 ){ zSp++;
34afb 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45   }..    sqlite3E
34afc 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
34afd 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75  "unknown or unsu
34afe 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70  pported join typ
34aff 65 3a 20 22 0d 0a 20 20 20 20 20 20 20 22 25 54  e: "..       "%T
34b00 20 25 54 25 73 25 54 22 2c 20 70 41 2c 20 70 42   %T%s%T", pA, pB
34b01 2c 20 7a 53 70 2c 20 70 43 29 3b 0d 0a 20 20 20  , zSp, pC);..   
34b02 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49   jointype = JT_I
34b03 4e 4e 45 52 3b 0d 0a 20 20 7d 65 6c 73 65 20 69  NNER;..  }else i
34b04 66 28 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a  f( (jointype & J
34b05 54 5f 4f 55 54 45 52 29 21 3d 30 20 0d 0a 20 20  T_OUTER)!=0 ..  
34b06 20 20 20 20 20 20 20 26 26 20 28 6a 6f 69 6e 74         && (joint
34b07 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a  ype & (JT_LEFT|J
34b08 54 5f 52 49 47 48 54 29 29 21 3d 4a 54 5f 4c 45  T_RIGHT))!=JT_LE
34b09 46 54 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  FT ){..    sqlit
34b0a 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
34b0b 65 2c 20 0d 0a 20 20 20 20 20 20 22 52 49 47 48  e, ..      "RIGH
34b0c 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52  T and FULL OUTER
34b0d 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63   JOINs are not c
34b0e 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74  urrently support
34b0f 65 64 22 29 3b 0d 0a 20 20 20 20 6a 6f 69 6e 74  ed");..    joint
34b10 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0d  ype = JT_INNER;.
34b11 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6a  .  }..  return j
34b12 6f 69 6e 74 79 70 65 3b 0d 0a 7d 0d 0a 0d 0a 2f  ointype;..}..../
34b13 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
34b14 20 69 6e 64 65 78 20 6f 66 20 61 20 63 6f 6c 75   index of a colu
34b15 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 20 20  mn in a table.  
34b16 52 65 74 75 72 6e 20 2d 31 20 69 66 20 74 68 65  Return -1 if the
34b17 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 69 73 20 6e   column..** is n
34b18 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ot contained in 
34b19 74 68 65 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a  the table...*/..
34b1a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 75 6d  static int colum
34b1b 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70 54  nIndex(Table *pT
34b1c 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
34b1d 7a 43 6f 6c 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  zCol){..  int i;
34b1e 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
34b1f 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
34b20 0d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
34b21 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61  3StrICmp(pTab->a
34b22 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  Col[i].zName, zC
34b23 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ol)==0 ) return 
34b24 69 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  i;..  }..  retur
34b25 6e 20 2d 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n -1;..}..../*..
34b26 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 69  ** Search the fi
34b27 72 73 74 20 4e 20 74 61 62 6c 65 73 20 69 6e 20  rst N tables in 
34b28 70 53 72 63 2c 20 66 72 6f 6d 20 6c 65 66 74 20  pSrc, from left 
34b29 74 6f 20 72 69 67 68 74 2c 20 6c 6f 6f 6b 69 6e  to right, lookin
34b2a 67 20 66 6f 72 20 61 0d 0a 2a 2a 20 74 61 62 6c  g for a..** tabl
34b2b 65 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c  e that has a col
34b2c 75 6d 6e 20 6e 61 6d 65 64 20 7a 43 6f 6c 2e 20  umn named zCol. 
34b2d 20 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 66   ..**..** When f
34b2e 6f 75 6e 64 2c 20 73 65 74 20 2a 70 69 54 61 62  ound, set *piTab
34b2f 20 61 6e 64 20 2a 70 69 43 6f 6c 20 74 6f 20 74   and *piCol to t
34b30 68 65 20 74 61 62 6c 65 20 69 6e 64 65 78 20 61  he table index a
34b31 6e 64 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0d  nd column index.
34b32 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 61 74 63 68  .** of the match
34b33 69 6e 67 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 72  ing column and r
34b34 65 74 75 72 6e 20 54 52 55 45 2e 0d 0a 2a 2a 0d  eturn TRUE...**.
34b35 0a 2a 2a 20 49 66 20 6e 6f 74 20 66 6f 75 6e 64  .** If not found
34b36 2c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0d  , return FALSE..
34b37 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
34b38 74 61 62 6c 65 41 6e 64 43 6f 6c 75 6d 6e 49 6e  tableAndColumnIn
34b39 64 65 78 28 0d 0a 20 20 53 72 63 4c 69 73 74 20  dex(..  SrcList 
34b3a 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20  *pSrc,       /* 
34b3b 41 72 72 61 79 20 6f 66 20 74 61 62 6c 65 73 20  Array of tables 
34b3c 74 6f 20 73 65 61 72 63 68 20 2a 2f 0d 0a 20 20  to search */..  
34b3d 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20  int N,          
34b3e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
34b3f 66 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63  f tables in pSrc
34b40 2d 3e 61 5b 5d 20 74 6f 20 73 65 61 72 63 68 20  ->a[] to search 
34b41 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
34b42 20 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61   *zCol,    /* Na
34b43 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
34b44 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
34b45 66 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70  for */..  int *p
34b46 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  iTab,          /
34b47 2a 20 57 72 69 74 65 20 69 6e 64 65 78 20 6f 66  * Write index of
34b48 20 70 53 72 63 2d 3e 61 5b 5d 20 68 65 72 65 20   pSrc->a[] here 
34b49 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 69 43 6f 6c  */..  int *piCol
34b4a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
34b4b 69 74 65 20 69 6e 64 65 78 20 6f 66 20 70 53 72  ite index of pSr
34b4c 63 2d 3e 61 5b 2a 70 69 54 61 62 5d 2e 70 54 61  c->a[*piTab].pTa
34b4d 62 2d 3e 61 43 6f 6c 5b 5d 20 68 65 72 65 20 2a  b->aCol[] here *
34b4e 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 20  /..){..  int i; 
34b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34b50 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
34b51 72 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63  r tables in pSrc
34b52 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b   */..  int iCol;
34b53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
34b54 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 6d  ndex of column m
34b55 61 74 63 68 69 6e 67 20 7a 43 6f 6c 20 2a 2f 0d  atching zCol */.
34b56 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 69  ...  assert( (pi
34b57 54 61 62 3d 3d 30 29 3d 3d 28 70 69 43 6f 6c 3d  Tab==0)==(piCol=
34b58 3d 30 29 20 29 3b 20 20 2f 2a 20 42 6f 74 68 20  =0) );  /* Both 
34b59 6f 72 20 6e 65 69 74 68 65 72 20 61 72 65 20 4e  or neither are N
34b5a 55 4c 4c 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d  ULL */..  for(i=
34b5b 30 3b 20 69 3c 4e 3b 20 69 2b 2b 29 7b 0d 0a 20  0; i<N; i++){.. 
34b5c 20 20 20 69 43 6f 6c 20 3d 20 63 6f 6c 75 6d 6e     iCol = column
34b5d 49 6e 64 65 78 28 70 53 72 63 2d 3e 61 5b 69 5d  Index(pSrc->a[i]
34b5e 2e 70 54 61 62 2c 20 7a 43 6f 6c 29 3b 0d 0a 20  .pTab, zCol);.. 
34b5f 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29     if( iCol>=0 )
34b60 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 69 54  {..      if( piT
34b61 61 62 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a  ab ){..        *
34b62 70 69 54 61 62 20 3d 20 69 3b 0d 0a 20 20 20 20  piTab = i;..    
34b63 20 20 20 20 2a 70 69 43 6f 6c 20 3d 20 69 43 6f      *piCol = iCo
34b64 6c 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  l;..      }..   
34b65 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
34b66 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75    }..  }..  retu
34b67 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn 0;..}..../*..
34b68 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
34b69 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
34b6a 74 65 72 6d 73 20 69 6d 70 6c 69 65 64 20 62 79  terms implied by
34b6b 20 4a 4f 49 4e 20 73 79 6e 74 61 78 20 74 6f 20   JOIN syntax to 
34b6c 74 68 65 0d 0a 2a 2a 20 57 48 45 52 45 20 63 6c  the..** WHERE cl
34b6d 61 75 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20  ause expression 
34b6e 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
34b6f 65 6d 65 6e 74 2e 20 54 68 65 20 6e 65 77 20 74  ement. The new t
34b70 65 72 6d 2c 20 77 68 69 63 68 0d 0a 2a 2a 20 69  erm, which..** i
34b71 73 20 41 4e 44 65 64 20 77 69 74 68 20 74 68 65  s ANDed with the
34b72 20 65 78 69 73 74 69 6e 67 20 57 48 45 52 45 20   existing WHERE 
34b73 63 6c 61 75 73 65 2c 20 69 73 20 6f 66 20 74 68  clause, is of th
34b74 65 20 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  e form:..**..** 
34b75 20 20 20 28 74 61 62 31 2e 63 6f 6c 31 20 3d 20     (tab1.col1 = 
34b76 74 61 62 32 2e 63 6f 6c 32 29 0d 0a 2a 2a 0d 0a  tab2.col2)..**..
34b77 2a 2a 20 77 68 65 72 65 20 74 61 62 31 20 69 73  ** where tab1 is
34b78 20 74 68 65 20 69 53 72 63 27 74 68 20 74 61 62   the iSrc'th tab
34b79 6c 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70 53  le in SrcList pS
34b7a 72 63 20 61 6e 64 20 74 61 62 32 20 69 73 20 74  rc and tab2 is t
34b7b 68 65 20 0d 0a 2a 2a 20 28 69 53 72 63 2b 31 29  he ..** (iSrc+1)
34b7c 27 74 68 2e 20 43 6f 6c 75 6d 6e 20 63 6f 6c 31  'th. Column col1
34b7d 20 69 73 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 4c   is column iColL
34b7e 65 66 74 20 6f 66 20 74 61 62 31 2c 20 61 6e 64  eft of tab1, and
34b7f 20 63 6f 6c 32 20 69 73 0d 0a 2a 2a 20 63 6f 6c   col2 is..** col
34b80 75 6d 6e 20 69 43 6f 6c 52 69 67 68 74 20 6f 66  umn iColRight of
34b81 20 74 61 62 32 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   tab2...*/..stat
34b82 69 63 20 76 6f 69 64 20 61 64 64 57 68 65 72 65  ic void addWhere
34b83 54 65 72 6d 28 0d 0a 20 20 50 61 72 73 65 20 2a  Term(..  Parse *
34b84 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
34b85 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
34b86 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  ing context */..
34b87 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
34b88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b89 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
34b8a 6c 65 73 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75  les in FROM clau
34b8b 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c 65  se */..  int iLe
34b8c 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ft,             
34b8d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
34b8e 78 20 6f 66 20 66 69 72 73 74 20 74 61 62 6c 65  x of first table
34b8f 20 74 6f 20 6a 6f 69 6e 20 69 6e 20 70 53 72 63   to join in pSrc
34b90 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 4c   */..  int iColL
34b91 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  eft,            
34b92 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
34b93 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 66 69 72  of column in fir
34b94 73 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69  st table */..  i
34b95 6e 74 20 69 52 69 67 68 74 2c 20 20 20 20 20 20  nt iRight,      
34b96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34b97 2a 20 49 6e 64 65 78 20 6f 66 20 73 65 63 6f 6e  * Index of secon
34b98 64 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 20  d table in pSrc 
34b99 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 52 69  */..  int iColRi
34b9a 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ght,            
34b9b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
34b9c 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 73 65 63 6f  f column in seco
34b9d 6e 64 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69  nd table */..  i
34b9e 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e 2c 20  nt isOuterJoin, 
34b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34ba0 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
34ba1 73 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20  s an OUTER join 
34ba2 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 2a 70 70 57  */..  Expr **ppW
34ba3 68 65 72 65 20 20 20 20 20 20 20 20 20 20 20 20  here            
34ba4 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
34ba5 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
34ba6 65 20 74 6f 20 61 64 64 20 74 6f 20 2a 2f 0d 0a  e to add to */..
34ba7 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ){..  sqlite3 *d
34ba8 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d  b = pParse->db;.
34ba9 0a 20 20 45 78 70 72 20 2a 70 45 31 3b 0d 0a 20  .  Expr *pE1;.. 
34baa 20 45 78 70 72 20 2a 70 45 32 3b 0d 0a 20 20 45   Expr *pE2;..  E
34bab 78 70 72 20 2a 70 45 71 3b 0d 0a 0d 0a 20 20 61  xpr *pEq;....  a
34bac 73 73 65 72 74 28 20 69 4c 65 66 74 3c 69 52 69  ssert( iLeft<iRi
34bad 67 68 74 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ght );..  assert
34bae 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 69 52 69  ( pSrc->nSrc>iRi
34baf 67 68 74 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ght );..  assert
34bb0 28 20 70 53 72 63 2d 3e 61 5b 69 4c 65 66 74 5d  ( pSrc->a[iLeft]
34bb1 2e 70 54 61 62 20 29 3b 0d 0a 20 20 61 73 73 65  .pTab );..  asse
34bb2 72 74 28 20 70 53 72 63 2d 3e 61 5b 69 52 69 67  rt( pSrc->a[iRig
34bb3 68 74 5d 2e 70 54 61 62 20 29 3b 0d 0a 0d 0a 20  ht].pTab );.... 
34bb4 20 70 45 31 20 3d 20 73 71 6c 69 74 65 33 43 72   pE1 = sqlite3Cr
34bb5 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64  eateColumnExpr(d
34bb6 62 2c 20 70 53 72 63 2c 20 69 4c 65 66 74 2c 20  b, pSrc, iLeft, 
34bb7 69 43 6f 6c 4c 65 66 74 29 3b 0d 0a 20 20 70 45  iColLeft);..  pE
34bb8 32 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  2 = sqlite3Creat
34bb9 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20  eColumnExpr(db, 
34bba 70 53 72 63 2c 20 69 52 69 67 68 74 2c 20 69 43  pSrc, iRight, iC
34bbb 6f 6c 52 69 67 68 74 29 3b 0d 0a 0d 0a 20 20 70  olRight);....  p
34bbc 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  Eq = sqlite3PExp
34bbd 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c  r(pParse, TK_EQ,
34bbe 20 70 45 31 2c 20 70 45 32 2c 20 30 29 3b 0d 0a   pE1, pE2, 0);..
34bbf 20 20 69 66 28 20 70 45 71 20 26 26 20 69 73 4f    if( pEq && isO
34bc0 75 74 65 72 4a 6f 69 6e 20 29 7b 0d 0a 20 20 20  uterJoin ){..   
34bc1 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
34bc2 28 70 45 71 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  (pEq, EP_FromJoi
34bc3 6e 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  n);..    assert(
34bc4 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
34bc5 65 72 74 79 28 70 45 71 2c 20 45 50 5f 54 6f 6b  erty(pEq, EP_Tok
34bc6 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65  enOnly|EP_Reduce
34bc7 64 29 20 29 3b 0d 0a 20 20 20 20 45 78 70 72 53  d) );..    ExprS
34bc8 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45  etIrreducible(pE
34bc9 71 29 3b 0d 0a 20 20 20 20 70 45 71 2d 3e 69 52  q);..    pEq->iR
34bca 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20  ightJoinTable = 
34bcb 28 69 31 36 29 70 45 32 2d 3e 69 54 61 62 6c 65  (i16)pE2->iTable
34bcc 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 70 57 68 65  ;..  }..  *ppWhe
34bcd 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
34bce 41 6e 64 28 64 62 2c 20 2a 70 70 57 68 65 72 65  And(db, *ppWhere
34bcf 2c 20 70 45 71 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  , pEq);..}..../*
34bd0 0d 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50 5f  ..** Set the EP_
34bd1 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74  FromJoin propert
34bd2 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f  y on all terms o
34bd3 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  f the given expr
34bd4 65 73 73 69 6f 6e 2e 0d 0a 2a 2a 20 41 6e 64 20  ession...** And 
34bd5 73 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69  set the Expr.iRi
34bd6 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20  ghtJoinTable to 
34bd7 69 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79  iTable for every
34bd8 20 74 65 72 6d 20 69 6e 20 74 68 65 0d 0a 2a 2a   term in the..**
34bd9 20 65 78 70 72 65 73 73 69 6f 6e 2e 0d 0a 2a 2a   expression...**
34bda 0d 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d  ..** The EP_From
34bdb 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 69 73  Join property is
34bdc 20 75 73 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f   used on terms o
34bdd 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
34bde 74 6f 20 74 65 6c 6c 0d 0a 2a 2a 20 74 68 65 20  to tell..** the 
34bdf 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
34be0 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63  processing logic
34be1 20 74 68 61 74 20 74 68 69 73 20 74 65 72 6d 20   that this term 
34be2 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 0d 0a  is part of the..
34be3 2a 2a 20 6a 6f 69 6e 20 72 65 73 74 72 69 63 74  ** join restrict
34be4 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e  ion specified in
34be5 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47   the ON or USING
34be6 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 74 20   clause and not 
34be7 61 20 70 61 72 74 0d 0a 2a 2a 20 6f 66 20 74 68  a part..** of th
34be8 65 20 6d 6f 72 65 20 67 65 6e 65 72 61 6c 20 57  e more general W
34be9 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 54 68  HERE clause.  Th
34bea 65 73 65 20 74 65 72 6d 73 20 61 72 65 20 6d 6f  ese terms are mo
34beb 76 65 64 20 6f 76 65 72 20 74 6f 20 74 68 65 0d  ved over to the.
34bec 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65  .** WHERE clause
34bed 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20 70 72 6f   during join pro
34bee 63 65 73 73 69 6e 67 20 62 75 74 20 77 65 20 6e  cessing but we n
34bef 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20  eed to remember 
34bf0 74 68 61 74 20 74 68 65 79 0d 0a 2a 2a 20 6f 72  that they..** or
34bf1 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20  iginated in the 
34bf2 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
34bf3 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  se...**..** The 
34bf4 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54  Expr.iRightJoinT
34bf5 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65 20 57  able tells the W
34bf6 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
34bf7 65 73 73 69 6e 67 20 74 68 61 74 20 74 68 65 0d  essing that the.
34bf8 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64  .** expression d
34bf9 65 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20  epends on table 
34bfa 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
34bfb 65 76 65 6e 20 69 66 20 74 68 61 74 20 74 61 62  even if that tab
34bfc 6c 65 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 65 78  le is not..** ex
34bfd 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e  plicitly mention
34bfe 65 64 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ed in the expres
34bff 73 69 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f  sion.  That info
34c00 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65  rmation is neede
34c01 64 0d 0a 2a 2a 20 66 6f 72 20 63 61 73 65 73 20  d..** for cases 
34c02 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 2a 2a 0d 0a  like this:..**..
34c03 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  **    SELECT * F
34c04 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e  ROM t1 LEFT JOIN
34c05 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62   t2 ON t1.a=t2.b
34c06 20 41 4e 44 20 74 31 2e 78 3d 35 0d 0a 2a 2a 0d   AND t1.x=5..**.
34c07 0a 2a 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c  .** The where cl
34c08 61 75 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65  ause needs to de
34c09 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  fer the handling
34c0a 20 6f 66 20 74 68 65 20 74 31 2e 78 3d 35 0d 0a   of the t1.x=5..
34c0b 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c 20 61 66  ** term until af
34c0c 74 65 72 20 74 68 65 20 74 32 20 6c 6f 6f 70 20  ter the t2 loop 
34c0d 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e  of the join.  In
34c0e 20 74 68 61 74 20 77 61 79 2c 20 61 0d 0a 2a 2a   that way, a..**
34c0f 20 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c   NULL t2 row wil
34c10 6c 20 62 65 20 69 6e 73 65 72 74 65 64 20 77 68  l be inserted wh
34c11 65 6e 65 76 65 72 20 74 31 2e 78 21 3d 35 2e 20  enever t1.x!=5. 
34c12 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 0d 0a 2a   If we do not..*
34c13 2a 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64  * defer the hand
34c14 6c 69 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c 20  ling of t1.x=5, 
34c15 69 74 20 77 69 6c 6c 20 62 65 20 70 72 6f 63 65  it will be proce
34c16 73 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  ssed immediately
34c17 0d 0a 2a 2a 20 61 66 74 65 72 20 74 68 65 20 74  ..** after the t
34c18 31 20 6c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20  1 loop and rows 
34c19 77 69 74 68 20 74 31 2e 78 21 3d 35 20 77 69 6c  with t1.x!=5 wil
34c1a 6c 20 6e 65 76 65 72 20 61 70 70 65 61 72 20 69  l never appear i
34c1b 6e 0d 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75 74  n..** the output
34c1c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 63 6f 72  , which is incor
34c1d 72 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rect...*/..stati
34c1e 63 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e 45 78  c void setJoinEx
34c1f 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  pr(Expr *p, int 
34c20 69 54 61 62 6c 65 29 7b 0d 0a 20 20 77 68 69 6c  iTable){..  whil
34c21 65 28 20 70 20 29 7b 0d 0a 20 20 20 20 45 78 70  e( p ){..    Exp
34c22 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20  rSetProperty(p, 
34c23 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0d 0a 20  EP_FromJoin);.. 
34c24 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
34c25 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
34c26 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45  , EP_TokenOnly|E
34c27 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0d 0a 20  P_Reduced) );.. 
34c28 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75     ExprSetIrredu
34c29 63 69 62 6c 65 28 70 29 3b 0d 0a 20 20 20 20 70  cible(p);..    p
34c2a 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
34c2b 65 20 3d 20 28 69 31 36 29 69 54 61 62 6c 65 3b  e = (i16)iTable;
34c2c 0d 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45 78 70  ..    setJoinExp
34c2d 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62  r(p->pLeft, iTab
34c2e 6c 65 29 3b 0d 0a 20 20 20 20 70 20 3d 20 70 2d  le);..    p = p-
34c2f 3e 70 52 69 67 68 74 3b 0d 0a 20 20 7d 20 0d 0a  >pRight;..  } ..
34c30 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
34c31 20 72 6f 75 74 69 6e 65 20 70 72 6f 63 65 73 73   routine process
34c32 65 73 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66 6f  es the join info
34c33 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 53 45  rmation for a SE
34c34 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0d  LECT statement..
34c35 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  .** ON and USING
34c36 20 63 6c 61 75 73 65 73 20 61 72 65 20 63 6f 6e   clauses are con
34c37 76 65 72 74 65 64 20 69 6e 74 6f 20 65 78 74 72  verted into extr
34c38 61 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  a terms of the W
34c39 48 45 52 45 20 63 6c 61 75 73 65 2e 0d 0a 2a 2a  HERE clause...**
34c3a 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 73 20 61   NATURAL joins a
34c3b 6c 73 6f 20 63 72 65 61 74 65 20 65 78 74 72 61  lso create extra
34c3c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
34c3d 72 6d 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  rms...**..** The
34c3e 20 74 65 72 6d 73 20 6f 66 20 61 20 46 52 4f 4d   terms of a FROM
34c3f 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f 6e 74   clause are cont
34c40 61 69 6e 65 64 20 69 6e 20 74 68 65 20 53 65 6c  ained in the Sel
34c41 65 63 74 2e 70 53 72 63 20 73 74 72 75 63 74 75  ect.pSrc structu
34c42 72 65 2e 0d 0a 2a 2a 20 54 68 65 20 6c 65 66 74  re...** The left
34c43 20 6d 6f 73 74 20 74 61 62 6c 65 20 69 73 20 74   most table is t
34c44 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69  he first entry i
34c45 6e 20 53 65 6c 65 63 74 2e 70 53 72 63 2e 20 20  n Select.pSrc.  
34c46 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0d 0a  The right-most..
34c47 2a 2a 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  ** table is the 
34c48 6c 61 73 74 20 65 6e 74 72 79 2e 20 20 54 68 65  last entry.  The
34c49 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 69   join operator i
34c4a 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 65 6e  s held in the en
34c4b 74 72 79 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 6c  try to..** the l
34c4c 65 66 74 2e 20 20 54 68 75 73 20 65 6e 74 72 79  eft.  Thus entry
34c4d 20 30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20   0 contains the 
34c4e 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f  join operator fo
34c4f 72 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65  r the join betwe
34c50 65 6e 0d 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30  en..** entries 0
34c51 20 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20   and 1.  Any ON 
34c52 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  or USING clauses
34c53 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
34c54 20 74 68 65 20 6a 6f 69 6e 20 61 72 65 0d 0a 2a   the join are..*
34c55 2a 20 61 6c 73 6f 20 61 74 74 61 63 68 65 64 20  * also attached 
34c56 74 6f 20 74 68 65 20 6c 65 66 74 20 65 6e 74 72  to the left entr
34c57 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  y...**..** This 
34c58 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
34c59 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
34c5a 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  rors encountered
34c5b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
34c5c 74 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  t sqliteProcessJ
34c5d 6f 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  oin(Parse *pPars
34c5e 65 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0d 0a  e, Select *p){..
34c5f 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
34c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c61 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20    /* All tables 
34c62 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
34c63 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 2c 20  se */..  int i, 
34c64 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
34c65 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
34c66 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d 0a 20 20   counters */..  
34c67 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
34c68 74 65 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20 20  tem *pLeft;     
34c69 2f 2a 20 4c 65 66 74 20 74 61 62 6c 65 20 62 65  /* Left table be
34c6a 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0d 0a 20  ing joined */.. 
34c6b 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
34c6c 69 74 65 6d 20 2a 70 52 69 67 68 74 3b 20 20 20  item *pRight;   
34c6d 20 2f 2a 20 52 69 67 68 74 20 74 61 62 6c 65 20   /* Right table 
34c6e 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0d  being joined */.
34c6f 0a 0d 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70  ...  pSrc = p->p
34c70 53 72 63 3b 0d 0a 20 20 70 4c 65 66 74 20 3d 20  Src;..  pLeft = 
34c71 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0d 0a 20 20  &pSrc->a[0];..  
34c72 70 52 69 67 68 74 20 3d 20 26 70 4c 65 66 74 5b  pRight = &pLeft[
34c73 31 5d 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  1];..  for(i=0; 
34c74 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20  i<pSrc->nSrc-1; 
34c75 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20 70  i++, pRight++, p
34c76 4c 65 66 74 2b 2b 29 7b 0d 0a 20 20 20 20 54 61  Left++){..    Ta
34c77 62 6c 65 20 2a 70 4c 65 66 74 54 61 62 20 3d 20  ble *pLeftTab = 
34c78 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0d 0a 20 20  pLeft->pTab;..  
34c79 20 20 54 61 62 6c 65 20 2a 70 52 69 67 68 74 54    Table *pRightT
34c7a 61 62 20 3d 20 70 52 69 67 68 74 2d 3e 70 54 61  ab = pRight->pTa
34c7b 62 3b 0d 0a 20 20 20 20 69 6e 74 20 69 73 4f 75  b;..    int isOu
34c7c 74 65 72 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20  ter;....    if( 
34c7d 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d 3d  NEVER(pLeftTab==
34c7e 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d 3d  0 || pRightTab==
34c7f 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  0) ) continue;..
34c80 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70      isOuter = (p
34c81 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
34c82 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0d  & JT_OUTER)!=0;.
34c83 0a 0d 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74  ...    /* When t
34c84 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f  he NATURAL keywo
34c85 72 64 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61  rd is present, a
34c86 64 64 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  dd WHERE clause 
34c87 74 65 72 6d 73 20 66 6f 72 0d 0a 20 20 20 20 2a  terms for..    *
34c88 2a 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74  * every column t
34c89 68 61 74 20 74 68 65 20 74 77 6f 20 74 61 62 6c  hat the two tabl
34c8a 65 73 20 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f  es have in commo
34c8b 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  n...    */..    
34c8c 69 66 28 20 70 52 69 67 68 74 2d 3e 6a 6f 69 6e  if( pRight->join
34c8d 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41  type & JT_NATURA
34c8e 4c 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  L ){..      if( 
34c8f 70 52 69 67 68 74 2d 3e 70 4f 6e 20 7c 7c 20 70  pRight->pOn || p
34c90 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b  Right->pUsing ){
34c91 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
34c92 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
34c93 2c 20 22 61 20 4e 41 54 55 52 41 4c 20 6a 6f 69  , "a NATURAL joi
34c94 6e 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 22  n may not have "
34c95 0d 0a 20 20 20 20 20 20 20 20 20 20 20 22 61 6e  ..           "an
34c96 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
34c97 75 73 65 22 2c 20 30 29 3b 0d 0a 20 20 20 20 20  use", 0);..     
34c98 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
34c99 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 66 6f 72      }..      for
34c9a 28 6a 3d 30 3b 20 6a 3c 70 52 69 67 68 74 54 61  (j=0; j<pRightTa
34c9b 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0d 0a  b->nCol; j++){..
34c9c 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
34c9d 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame;   /* Name o
34c9e 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  f column in the 
34c9f 72 69 67 68 74 20 74 61 62 6c 65 20 2a 2f 0d 0a  right table */..
34ca0 20 20 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66          int iLef
34ca1 74 3b 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69  t;     /* Matchi
34ca2 6e 67 20 6c 65 66 74 20 74 61 62 6c 65 20 2a 2f  ng left table */
34ca3 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 4c  ..        int iL
34ca4 65 66 74 43 6f 6c 3b 20 20 2f 2a 20 4d 61 74 63  eftCol;  /* Matc
34ca5 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  hing column in t
34ca6 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 2a 2f  he left table */
34ca7 0d 0a 0d 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  ....        zNam
34ca8 65 20 3d 20 70 52 69 67 68 74 54 61 62 2d 3e 61  e = pRightTab->a
34ca9 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  Col[j].zName;.. 
34caa 20 20 20 20 20 20 20 69 66 28 20 74 61 62 6c 65         if( table
34cab 41 6e 64 43 6f 6c 75 6d 6e 49 6e 64 65 78 28 70  AndColumnIndex(p
34cac 53 72 63 2c 20 69 2b 31 2c 20 7a 4e 61 6d 65 2c  Src, i+1, zName,
34cad 20 26 69 4c 65 66 74 2c 20 26 69 4c 65 66 74 43   &iLeft, &iLeftC
34cae 6f 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ol) ){..        
34caf 20 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70    addWhereTerm(p
34cb0 50 61 72 73 65 2c 20 70 53 72 63 2c 20 69 4c 65  Parse, pSrc, iLe
34cb1 66 74 2c 20 69 4c 65 66 74 43 6f 6c 2c 20 69 2b  ft, iLeftCol, i+
34cb2 31 2c 20 6a 2c 0d 0a 20 20 20 20 20 20 20 20 20  1, j,..         
34cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
34cb4 4f 75 74 65 72 2c 20 26 70 2d 3e 70 57 68 65 72  Outer, &p->pWher
34cb5 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  e);..        }..
34cb6 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
34cb7 0d 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c 6c 6f  ..    /* Disallo
34cb8 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53  w both ON and US
34cb9 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e 20 74  ING clauses in t
34cba 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0d 0a 20 20  he same join..  
34cbb 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 52    */..    if( pR
34cbc 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69  ight->pOn && pRi
34cbd 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0d 0a  ght->pUsing ){..
34cbe 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
34cbf 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
34cc0 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20  annot have both 
34cc1 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0d 0a  ON and USING "..
34cc2 20 20 20 20 20 20 20 20 22 63 6c 61 75 73 65 73          "clauses
34cc3 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69   in the same joi
34cc4 6e 22 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  n");..      retu
34cc5 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  rn 1;..    }....
34cc6 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f      /* Add the O
34cc7 4e 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  N clause to the 
34cc8 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45  end of the WHERE
34cc9 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74   clause, connect
34cca 65 64 20 62 79 0d 0a 20 20 20 20 2a 2a 20 61 6e  ed by..    ** an
34ccb 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0d 0a   AND operator...
34ccc 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
34ccd 70 52 69 67 68 74 2d 3e 70 4f 6e 20 29 7b 0d 0a  pRight->pOn ){..
34cce 20 20 20 20 20 20 69 66 28 20 69 73 4f 75 74 65        if( isOute
34ccf 72 20 29 20 73 65 74 4a 6f 69 6e 45 78 70 72 28  r ) setJoinExpr(
34cd0 70 52 69 67 68 74 2d 3e 70 4f 6e 2c 20 70 52 69  pRight->pOn, pRi
34cd1 67 68 74 2d 3e 69 43 75 72 73 6f 72 29 3b 0d 0a  ght->iCursor);..
34cd2 20 20 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20        p->pWhere 
34cd3 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
34cd4 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e  (pParse->db, p->
34cd5 70 57 68 65 72 65 2c 20 70 52 69 67 68 74 2d 3e  pWhere, pRight->
34cd6 70 4f 6e 29 3b 0d 0a 20 20 20 20 20 20 70 52 69  pOn);..      pRi
34cd7 67 68 74 2d 3e 70 4f 6e 20 3d 20 30 3b 0d 0a 20  ght->pOn = 0;.. 
34cd8 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43     }....    /* C
34cd9 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72 6d  reate extra term
34cda 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20 63  s on the WHERE c
34cdb 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20 63  lause for each c
34cdc 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0d 0a 20 20 20  olumn named..   
34cdd 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47   ** in the USING
34cde 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c   clause.  Exampl
34cdf 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61  e: If the two ta
34ce0 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
34ce1 64 20 61 72 65 20 0d 0a 20 20 20 20 2a 2a 20 41  d are ..    ** A
34ce2 20 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55   and B and the U
34ce3 53 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65  SING clause name
34ce4 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74  s X, Y, and Z, t
34ce5 68 65 6e 20 61 64 64 20 74 68 69 73 0d 0a 20 20  hen add this..  
34ce6 20 20 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52    ** to the WHER
34ce7 45 20 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58  E clause:    A.X
34ce8 3d 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59  =B.X AND A.Y=B.Y
34ce9 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0d 0a 20 20   AND A.Z=B.Z..  
34cea 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65    ** Report an e
34ceb 72 72 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c 75  rror if any colu
34cec 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20  mn mentioned in 
34ced 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
34cee 20 69 73 0d 0a 20 20 20 20 2a 2a 20 6e 6f 74 20   is..    ** not 
34cef 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62 6f 74  contained in bot
34cf0 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a  h tables to be j
34cf1 6f 69 6e 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  oined...    */..
34cf2 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
34cf3 70 55 73 69 6e 67 20 29 7b 0d 0a 20 20 20 20 20  pUsing ){..     
34cf4 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d   IdList *pList =
34cf5 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b   pRight->pUsing;
34cf6 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ..      for(j=0;
34cf7 20 6a 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a   j<pList->nId; j
34cf8 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68  ++){..        ch
34cf9 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f  ar *zName;     /
34cfa 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 65  * Name of the te
34cfb 72 6d 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  rm in the USING 
34cfc 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20 20  clause */..     
34cfd 20 20 20 69 6e 74 20 69 4c 65 66 74 3b 20 20 20     int iLeft;   
34cfe 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f 6e 20      /* Table on 
34cff 74 68 65 20 6c 65 66 74 20 77 69 74 68 20 6d 61  the left with ma
34d00 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61  tching column na
34d01 6d 65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69  me */..        i
34d02 6e 74 20 69 4c 65 66 74 43 6f 6c 3b 20 20 20 20  nt iLeftCol;    
34d03 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
34d04 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c   of matching col
34d05 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74 20  umn on the left 
34d06 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  */..        int 
34d07 69 52 69 67 68 74 43 6f 6c 3b 20 20 20 2f 2a 20  iRightCol;   /* 
34d08 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66  Column number of
34d09 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
34d0a 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a 2f   on the right */
34d0b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  ....        zNam
34d0c 65 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e  e = pList->a[j].
34d0d 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20 20 20  zName;..        
34d0e 69 52 69 67 68 74 43 6f 6c 20 3d 20 63 6f 6c 75  iRightCol = colu
34d0f 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54 61  mnIndex(pRightTa
34d10 62 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  b, zName);..    
34d11 20 20 20 20 69 66 28 20 69 52 69 67 68 74 43 6f      if( iRightCo
34d12 6c 3c 30 0d 0a 20 20 20 20 20 20 20 20 20 7c 7c  l<0..         ||
34d13 20 21 74 61 62 6c 65 41 6e 64 43 6f 6c 75 6d 6e   !tableAndColumn
34d14 49 6e 64 65 78 28 70 53 72 63 2c 20 69 2b 31 2c  Index(pSrc, i+1,
34d15 20 7a 4e 61 6d 65 2c 20 26 69 4c 65 66 74 2c 20   zName, &iLeft, 
34d16 26 69 4c 65 66 74 43 6f 6c 29 0d 0a 20 20 20 20  &iLeftCol)..    
34d17 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20      ){..        
34d18 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
34d19 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
34d1a 74 20 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c  t join using col
34d1b 75 6d 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20  umn %s - column 
34d1c 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  "..            "
34d1d 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 62  not present in b
34d1e 6f 74 68 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61  oth tables", zNa
34d1f 6d 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  me);..          
34d20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20  return 1;..     
34d21 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 61 64     }..        ad
34d22 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73  dWhereTerm(pPars
34d23 65 2c 20 70 53 72 63 2c 20 69 4c 65 66 74 2c 20  e, pSrc, iLeft, 
34d24 69 4c 65 66 74 43 6f 6c 2c 20 69 2b 31 2c 20 69  iLeftCol, i+1, i
34d25 52 69 67 68 74 43 6f 6c 2c 0d 0a 20 20 20 20 20  RightCol,..     
34d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34d27 69 73 4f 75 74 65 72 2c 20 26 70 2d 3e 70 57 68  isOuter, &p->pWh
34d28 65 72 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ere);..      }..
34d29 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
34d2a 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn 0;..}..../*
34d2b 0d 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64 65  ..** Insert code
34d2c 20 69 6e 74 6f 20 22 76 22 20 74 68 61 74 20 77   into "v" that w
34d2d 69 6c 6c 20 70 75 73 68 20 74 68 65 20 72 65 63  ill push the rec
34d2e 6f 72 64 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f  ord on the top o
34d2f 66 20 74 68 65 0d 0a 2a 2a 20 73 74 61 63 6b 20  f the..** stack 
34d30 69 6e 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e  into the sorter.
34d31 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
34d32 64 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72  d pushOntoSorter
34d33 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
34d34 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  se,         /* P
34d35 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
34d36 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  ..  ExprList *pO
34d37 72 64 65 72 42 79 2c 20 20 20 20 2f 2a 20 54 68  rderBy,    /* Th
34d38 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
34d39 65 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a  e */..  Select *
34d3a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 20 2f  pSelect,       /
34d3b 2a 20 54 68 65 20 77 68 6f 6c 65 20 53 45 4c 45  * The whole SELE
34d3c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d  CT statement */.
34d3d 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 20 20  .  int regData  
34d3e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
34d3f 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 64 61  ister holding da
34d40 74 61 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20  ta to be sorted 
34d41 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62 65 20 2a  */..){..  Vdbe *
34d42 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
34d43 65 3b 0d 0a 20 20 69 6e 74 20 6e 45 78 70 72 20  e;..  int nExpr 
34d44 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  = pOrderBy->nExp
34d45 72 3b 0d 0a 20 20 69 6e 74 20 72 65 67 42 61 73  r;..  int regBas
34d46 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
34d47 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
34d48 6e 45 78 70 72 2b 32 29 3b 0d 0a 20 20 69 6e 74  nExpr+2);..  int
34d49 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
34d4a 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
34d4b 50 61 72 73 65 29 3b 0d 0a 20 20 69 6e 74 20 6f  Parse);..  int o
34d4c 70 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70  p;..  sqlite3Exp
34d4d 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
34d4e 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45  se);..  sqlite3E
34d4f 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
34d50 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
34d51 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0d 0a  , regBase, 0);..
34d52 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
34d53 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e  Op2(v, OP_Sequen
34d54 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  ce, pOrderBy->iE
34d55 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b  Cursor, regBase+
34d56 6e 45 78 70 72 29 3b 0d 0a 20 20 73 71 6c 69 74  nExpr);..  sqlit
34d57 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70  e3ExprCodeMove(p
34d58 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20  Parse, regData, 
34d59 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c  regBase+nExpr+1,
34d5a 20 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56   1);..  sqlite3V
34d5b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
34d5c 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
34d5d 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20  ase, nExpr + 2, 
34d5e 72 65 67 52 65 63 6f 72 64 29 3b 0d 0a 20 20 69  regRecord);..  i
34d5f 66 28 20 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46  f( pSelect->selF
34d60 6c 61 67 73 20 26 20 53 46 5f 55 73 65 53 6f 72  lags & SF_UseSor
34d61 74 65 72 20 29 7b 0d 0a 20 20 20 20 6f 70 20 3d  ter ){..    op =
34d62 20 4f 50 5f 53 6f 72 74 65 72 49 6e 73 65 72 74   OP_SorterInsert
34d63 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
34d64 20 6f 70 20 3d 20 4f 50 5f 49 64 78 49 6e 73 65   op = OP_IdxInse
34d65 72 74 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  rt;..  }..  sqli
34d66 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
34d67 20 6f 70 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69   op, pOrderBy->i
34d68 45 43 75 72 73 6f 72 2c 20 72 65 67 52 65 63 6f  ECursor, regReco
34d69 72 64 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 52  rd);..  sqlite3R
34d6a 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
34d6b 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29  arse, regRecord)
34d6c 3b 0d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  ;..  sqlite3Rele
34d6d 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
34d6e 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45  rse, regBase, nE
34d6f 78 70 72 2b 32 29 3b 0d 0a 20 20 69 66 28 20 70  xpr+2);..  if( p
34d70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20 29  Select->iLimit )
34d71 7b 0d 0a 20 20 20 20 69 6e 74 20 61 64 64 72 31  {..    int addr1
34d72 2c 20 61 64 64 72 32 3b 0d 0a 20 20 20 20 69 6e  , addr2;..    in
34d73 74 20 69 4c 69 6d 69 74 3b 0d 0a 20 20 20 20 69  t iLimit;..    i
34d74 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4f 66 66  f( pSelect->iOff
34d75 73 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 69 4c  set ){..      iL
34d76 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  imit = pSelect->
34d77 69 4f 66 66 73 65 74 2b 31 3b 0d 0a 20 20 20 20  iOffset+1;..    
34d78 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69 4c  }else{..      iL
34d79 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  imit = pSelect->
34d7a 69 4c 69 6d 69 74 3b 0d 0a 20 20 20 20 7d 0d 0a  iLimit;..    }..
34d7b 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
34d7c 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
34d7d 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
34d7e 69 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  it);..    sqlite
34d7f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
34d80 50 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74  P_AddImm, iLimit
34d81 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 61 64 64 72  , -1);..    addr
34d82 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
34d83 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
34d84 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
34d85 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
34d86 64 64 72 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69  ddr1);..    sqli
34d87 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
34d88 20 4f 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65 72   OP_Last, pOrder
34d89 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0d 0a  By->iECursor);..
34d8a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
34d8b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65  ddOp1(v, OP_Dele
34d8c 74 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  te, pOrderBy->iE
34d8d 43 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20 73 71  Cursor);..    sq
34d8e 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
34d8f 65 28 76 2c 20 61 64 64 72 32 29 3b 0d 0a 20 20  e(v, addr2);..  
34d90 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  }..}..../*..** A
34d91 64 64 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  dd code to imple
34d92 6d 65 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0d  ment the OFFSET.
34d93 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
34d94 20 63 6f 64 65 4f 66 66 73 65 74 28 0d 0a 20 20   codeOffset(..  
34d95 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20  Vdbe *v,        
34d96 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
34d97 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d 20  de into this VM 
34d98 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c  */..  Select *p,
34d99 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
34d9a 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
34d9b 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0d 0a  being coded */..
34d9c 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 20    int iContinue 
34d9d 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
34d9e 20 74 6f 20 73 6b 69 70 20 74 68 65 20 63 75 72   to skip the cur
34d9f 72 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f 0d 0a  rent record */..
34da0 29 7b 0d 0a 20 20 69 66 28 20 70 2d 3e 69 4f 66  ){..  if( p->iOf
34da1 66 73 65 74 20 26 26 20 69 43 6f 6e 74 69 6e 75  fset && iContinu
34da2 65 21 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74  e!=0 ){..    int
34da3 20 61 64 64 72 3b 0d 0a 20 20 20 20 73 71 6c 69   addr;..    sqli
34da4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
34da5 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69   OP_AddImm, p->i
34da6 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0d 0a 20 20  Offset, -1);..  
34da7 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
34da8 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
34da9 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66 66 73  _IfNeg, p->iOffs
34daa 65 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  et);..    sqlite
34dab 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
34dac 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e 74  P_Goto, 0, iCont
34dad 69 6e 75 65 29 3b 0d 0a 20 20 20 20 56 64 62 65  inue);..    Vdbe
34dae 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69  Comment((v, "ski
34daf 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64 73  p OFFSET records
34db0 22 29 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  "));..    sqlite
34db1 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
34db2 20 61 64 64 72 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d   addr);..  }..}.
34db3 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 63 6f  .../*..** Add co
34db4 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 68 65  de that will che
34db5 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
34db6 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20  the N registers 
34db7 73 74 61 72 74 69 6e 67 20 61 74 20 69 4d 65 6d  starting at iMem
34db8 0d 0a 2a 2a 20 66 6f 72 6d 20 61 20 64 69 73 74  ..** form a dist
34db9 69 6e 63 74 20 65 6e 74 72 79 2e 20 20 69 54 61  inct entry.  iTa
34dba 62 20 69 73 20 61 20 73 6f 72 74 69 6e 67 20 69  b is a sorting i
34dbb 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20  ndex that holds 
34dbc 70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 73  previously..** s
34dbd 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  een combinations
34dbe 20 6f 66 20 74 68 65 20 4e 20 76 61 6c 75 65 73   of the N values
34dbf 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
34dc0 73 20 6d 61 64 65 20 69 6e 20 69 54 61 62 0d 0a  s made in iTab..
34dc1 2a 2a 20 69 66 20 74 68 65 20 63 75 72 72 65 6e  ** if the curren
34dc2 74 20 4e 20 76 61 6c 75 65 73 20 61 72 65 20 6e  t N values are n
34dc3 65 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 6a 75  ew...**..** A ju
34dc4 6d 70 20 74 6f 20 61 64 64 72 52 65 70 65 61 74  mp to addrRepeat
34dc5 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65   is made and the
34dc6 20 4e 2b 31 20 76 61 6c 75 65 73 20 61 72 65 20   N+1 values are 
34dc7 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 0d  popped from the.
34dc8 0a 2a 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65  .** stack if the
34dc9 20 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20   top N elements 
34dca 61 72 65 20 6e 6f 74 20 64 69 73 74 69 6e 63 74  are not distinct
34dcb 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
34dcc 69 64 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28  id codeDistinct(
34dcd 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
34dce 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
34dcf 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
34dd0 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
34dd1 0d 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20  ..  int iTab,   
34dd2 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72 74         /* A sort
34dd3 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20 74  ing index used t
34dd4 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73 74 69  o test for disti
34dd5 6e 63 74 6e 65 73 73 20 2a 2f 0d 0a 20 20 69 6e  nctness */..  in
34dd6 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20 20  t addrRepeat,   
34dd7 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
34dd8 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74   if not distinct
34dd9 20 2a 2f 0d 0a 20 20 69 6e 74 20 4e 2c 20 20 20   */..  int N,   
34dda 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
34ddb 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
34ddc 2a 2f 0d 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20  */..  int iMem  
34ddd 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
34dde 74 20 65 6c 65 6d 65 6e 74 20 2a 2f 0d 0a 29 7b  t element */..){
34ddf 0d 0a 20 20 56 64 62 65 20 2a 76 3b 0d 0a 20 20  ..  Vdbe *v;..  
34de0 69 6e 74 20 72 31 3b 0d 0a 0d 0a 20 20 76 20 3d  int r1;....  v =
34de1 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d   pParse->pVdbe;.
34de2 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  .  r1 = sqlite3G
34de3 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
34de4 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
34de5 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50  eAddOp4Int(v, OP
34de6 5f 46 6f 75 6e 64 2c 20 69 54 61 62 2c 20 61 64  _Found, iTab, ad
34de7 64 72 52 65 70 65 61 74 2c 20 69 4d 65 6d 2c 20  drRepeat, iMem, 
34de8 4e 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  N);..  sqlite3Vd
34de9 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
34dea 61 6b 65 52 65 63 6f 72 64 2c 20 69 4d 65 6d 2c  akeRecord, iMem,
34deb 20 4e 2c 20 72 31 29 3b 0d 0a 20 20 73 71 6c 69   N, r1);..  sqli
34dec 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
34ded 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
34dee 54 61 62 2c 20 72 31 29 3b 0d 0a 20 20 73 71 6c  Tab, r1);..  sql
34def 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
34df0 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0d  eg(pParse, r1);.
34df1 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  .}....#ifndef SQ
34df2 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
34df3 52 59 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  RY../*..** Gener
34df4 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ate an error mes
34df5 73 61 67 65 20 77 68 65 6e 20 61 20 53 45 4c 45  sage when a SELE
34df6 43 54 20 69 73 20 75 73 65 64 20 77 69 74 68 69  CT is used withi
34df7 6e 20 61 20 73 75 62 65 78 70 72 65 73 73 69 6f  n a subexpressio
34df8 6e 0d 0a 2a 2a 20 28 65 78 61 6d 70 6c 65 3a 20  n..** (example: 
34df9 20 22 61 20 49 4e 20 28 53 45 4c 45 43 54 20 2a   "a IN (SELECT *
34dfa 20 46 52 4f 4d 20 74 61 62 6c 65 29 22 29 20 62   FROM table)") b
34dfb 75 74 20 69 74 20 68 61 73 20 6d 6f 72 65 20 74  ut it has more t
34dfc 68 61 6e 20 31 20 72 65 73 75 6c 74 0d 0a 2a 2a  han 1 result..**
34dfd 20 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64 6f 20   column.  We do 
34dfe 74 68 69 73 20 69 6e 20 61 20 73 75 62 72 6f 75  this in a subrou
34dff 74 69 6e 65 20 62 65 63 61 75 73 65 20 74 68 65  tine because the
34e00 20 65 72 72 6f 72 20 75 73 65 64 20 74 6f 20 6f   error used to o
34e01 63 63 75 72 0d 0a 2a 2a 20 69 6e 20 6d 75 6c 74  ccur..** in mult
34e02 69 70 6c 65 20 70 6c 61 63 65 73 2e 20 20 28 54  iple places.  (T
34e03 68 65 20 65 72 72 6f 72 20 6f 6e 6c 79 20 6f 63  he error only oc
34e04 63 75 72 73 20 69 6e 20 6f 6e 65 20 70 6c 61 63  curs in one plac
34e05 65 20 6e 6f 77 2c 20 62 75 74 20 77 65 0d 0a 2a  e now, but we..*
34e06 2a 20 72 65 74 61 69 6e 20 74 68 65 20 73 75 62  * retain the sub
34e07 72 6f 75 74 69 6e 65 20 74 6f 20 6d 69 6e 69 6d  routine to minim
34e08 69 7a 65 20 63 6f 64 65 20 64 69 73 72 75 70 74  ize code disrupt
34e09 69 6f 6e 2e 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ion.)..*/..stati
34e0a 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d 75  c int checkForMu
34e0b 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45  ltiColumnSelectE
34e0c 72 72 6f 72 28 0d 0a 20 20 50 61 72 73 65 20 2a  rror(..  Parse *
34e0d 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
34e0e 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20   Parse context. 
34e0f 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 44 65 73 74  */..  SelectDest
34e10 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44 65   *pDest,   /* De
34e11 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c  stination of SEL
34e12 45 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0d 0a  ECT results */..
34e13 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20    int nExpr     
34e14 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
34e15 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
34e16 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  ns returned by S
34e17 45 4c 45 43 54 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ELECT */..){..  
34e18 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
34e19 74 2d 3e 65 44 65 73 74 3b 0d 0a 20 20 69 66 28  t->eDest;..  if(
34e1a 20 6e 45 78 70 72 3e 31 20 26 26 20 28 65 44 65   nExpr>1 && (eDe
34e1b 73 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65  st==SRT_Mem || e
34e1c 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29  Dest==SRT_Set) )
34e1d 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  {..    sqlite3Er
34e1e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
34e1f 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 65  only a single re
34e20 73 75 6c 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72  sult allowed for
34e21 20 22 0d 0a 20 20 20 20 20 20 20 22 61 20 53 45   "..       "a SE
34e22 4c 45 43 54 20 74 68 61 74 20 69 73 20 70 61 72  LECT that is par
34e23 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
34e24 6f 6e 22 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  on");..    retur
34e25 6e 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  n 1;..  }else{..
34e26 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
34e27 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d   }..}..#endif...
34e28 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./*..** This rou
34e29 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74  tine generates t
34e2a 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
34e2b 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e  inside of the in
34e2c 6e 65 72 20 6c 6f 6f 70 0d 0a 2a 2a 20 6f 66 20  ner loop..** of 
34e2d 61 20 53 45 4c 45 43 54 2e 0d 0a 2a 2a 0d 0a 2a  a SELECT...**..*
34e2e 2a 20 49 66 20 73 72 63 54 61 62 20 61 6e 64 20  * If srcTab and 
34e2f 6e 43 6f 6c 75 6d 6e 20 61 72 65 20 62 6f 74 68  nColumn are both
34e30 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
34e31 70 45 4c 69 73 74 20 65 78 70 72 65 73 73 69 6f  pEList expressio
34e32 6e 73 0d 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75  ns..** are evalu
34e33 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ated in order to
34e34 20 67 65 74 20 74 68 65 20 64 61 74 61 20 66 6f   get the data fo
34e35 72 20 74 68 69 73 20 72 6f 77 2e 20 20 49 66 20  r this row.  If 
34e36 6e 43 6f 6c 75 6d 6e 3e 30 0d 0a 2a 2a 20 74 68  nColumn>0..** th
34e37 65 6e 20 64 61 74 61 20 69 73 20 70 75 6c 6c 65  en data is pulle
34e38 64 20 66 72 6f 6d 20 73 72 63 54 61 62 20 61 6e  d from srcTab an
34e39 64 20 70 45 4c 69 73 74 20 69 73 20 75 73 65 64  d pEList is used
34e3a 20 6f 6e 6c 79 20 74 6f 20 67 65 74 20 74 68 65   only to get the
34e3b 0d 0a 2a 2a 20 64 61 74 61 74 79 70 65 73 20 66  ..** datatypes f
34e3c 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0d  or each column..
34e3d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
34e3e 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
34e3f 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
34e40 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
34e41 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
34e42 78 74 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20  xt */..  Select 
34e43 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
34e44 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
34e45 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
34e46 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  t being coded */
34e47 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
34e48 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c  List,       /* L
34e49 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65  ist of values be
34e4a 69 6e 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f  ing extracted */
34e4b 0d 0a 20 20 69 6e 74 20 73 72 63 54 61 62 2c 20  ..  int srcTab, 
34e4c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
34e4d 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74 68  ull data from th
34e4e 69 73 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69  is table */..  i
34e4f 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt nColumn,     
34e50 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
34e51 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
34e52 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
34e53 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  */..  ExprList *
34e54 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
34e55 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f   If not NULL, so
34e56 72 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67  rt results using
34e57 20 74 68 69 73 20 6b 65 79 20 2a 2f 0d 0a 20 20   this key */..  
34e58 69 6e 74 20 64 69 73 74 69 6e 63 74 2c 20 20 20  int distinct,   
34e59 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d          /* If >=
34e5a 30 2c 20 6d 61 6b 65 20 73 75 72 65 20 72 65 73  0, make sure res
34e5b 75 6c 74 73 20 61 72 65 20 64 69 73 74 69 6e 63  ults are distinc
34e5c 74 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 44 65  t */..  SelectDe
34e5d 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20  st *pDest,      
34e5e 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
34e5f 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73  e of the results
34e60 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6e 74   */..  int iCont
34e61 69 6e 75 65 2c 20 20 20 20 20 20 20 20 20 20 2f  inue,          /
34e62 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
34e63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78  ontinue with nex
34e64 74 20 72 6f 77 20 2a 2f 0d 0a 20 20 69 6e 74 20  t row */..  int 
34e65 69 42 72 65 61 6b 20 20 20 20 20 20 20 20 20 20  iBreak          
34e66 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
34e67 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
34e68 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20   the inner loop 
34e69 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62 65 20 2a  */..){..  Vdbe *
34e6a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
34e6b 65 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20  e;..  int i;..  
34e6c 69 6e 74 20 68 61 73 44 69 73 74 69 6e 63 74 3b  int hasDistinct;
34e6d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
34e6e 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
34e6f 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
34e70 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  nt */..  int reg
34e71 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  Result;         
34e72 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66       /* Start of
34e73 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e 67 20   memory holding 
34e74 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0d 0a 20  result set */.. 
34e75 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
34e76 73 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a 20  st->eDest;   /* 
34e77 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  How to dispose o
34e78 66 20 72 65 73 75 6c 74 73 20 2a 2f 0d 0a 20 20  f results */..  
34e79 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73  int iParm = pDes
34e7a 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f 2a 20 46  t->iParm;   /* F
34e7b 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
34e7c 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64   disposal method
34e7d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65 73 75   */..  int nResu
34e7e 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  ltCol;          
34e7f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
34e80 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a  result columns *
34e81 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 76  /....  assert( v
34e82 20 29 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45 52   );..  if( NEVER
34e83 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
34e84 0d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  ..  assert( pELi
34e85 73 74 21 3d 30 20 29 3b 0d 0a 20 20 68 61 73 44  st!=0 );..  hasD
34e86 69 73 74 69 6e 63 74 20 3d 20 64 69 73 74 69 6e  istinct = distin
34e87 63 74 3e 3d 30 3b 0d 0a 20 20 69 66 28 20 70 4f  ct>=0;..  if( pO
34e88 72 64 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61  rderBy==0 && !ha
34e89 73 44 69 73 74 69 6e 63 74 20 29 7b 0d 0a 20 20  sDistinct ){..  
34e8a 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20    codeOffset(v, 
34e8b 70 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0d 0a  p, iContinue);..
34e8c 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 50 75 6c 6c    }....  /* Pull
34e8d 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
34e8e 6f 6c 75 6d 6e 73 2e 0d 0a 20 20 2a 2f 0d 0a 20  olumns...  */.. 
34e8f 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29   if( nColumn>0 )
34e90 7b 0d 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f  {..    nResultCo
34e91 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0d 0a 20 20  l = nColumn;..  
34e92 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6e 52 65 73  }else{..    nRes
34e93 75 6c 74 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d  ultCol = pEList-
34e94 3e 6e 45 78 70 72 3b 0d 0a 20 20 7d 0d 0a 20 20  >nExpr;..  }..  
34e95 69 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d  if( pDest->iMem=
34e96 3d 30 20 29 7b 0d 0a 20 20 20 20 70 44 65 73 74  =0 ){..    pDest
34e97 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d  ->iMem = pParse-
34e98 3e 6e 4d 65 6d 2b 31 3b 0d 0a 20 20 20 20 70 44  >nMem+1;..    pD
34e99 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65 73  est->nMem = nRes
34e9a 75 6c 74 43 6f 6c 3b 0d 0a 20 20 20 20 70 50 61  ultCol;..    pPa
34e9b 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65  rse->nMem += nRe
34e9c 73 75 6c 74 43 6f 6c 3b 0d 0a 20 20 7d 65 6c 73  sultCol;..  }els
34e9d 65 7b 20 0d 0a 20 20 20 20 61 73 73 65 72 74 28  e{ ..    assert(
34e9e 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52   pDest->nMem==nR
34e9f 65 73 75 6c 74 43 6f 6c 20 29 3b 0d 0a 20 20 7d  esultCol );..  }
34ea0 0d 0a 20 20 72 65 67 52 65 73 75 6c 74 20 3d 20  ..  regResult = 
34ea1 70 44 65 73 74 2d 3e 69 4d 65 6d 3b 0d 0a 20 20  pDest->iMem;..  
34ea2 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b  if( nColumn>0 ){
34ea3 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
34ea4 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0d  <nColumn; i++){.
34ea5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
34ea6 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
34ea7 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 69  olumn, srcTab, i
34ea8 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29 3b 0d  , regResult+i);.
34ea9 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 20  .    }..  }else 
34eaa 69 66 28 20 65 44 65 73 74 21 3d 53 52 54 5f 45  if( eDest!=SRT_E
34eab 78 69 73 74 73 20 29 7b 0d 0a 20 20 20 20 2f 2a  xists ){..    /*
34eac 20 49 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   If the destinat
34ead 69 6f 6e 20 69 73 20 61 6e 20 45 58 49 53 54 53  ion is an EXISTS
34eae 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e  (...) expression
34eaf 2c 20 74 68 65 20 61 63 74 75 61 6c 0d 0a 20 20  , the actual..  
34eb0 20 20 2a 2a 20 76 61 6c 75 65 73 20 72 65 74 75    ** values retu
34eb1 72 6e 65 64 20 62 79 20 74 68 65 20 53 45 4c 45  rned by the SELE
34eb2 43 54 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69  CT are not requi
34eb3 72 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  red...    */..  
34eb4 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
34eb5 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
34eb6 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
34eb7 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50  rCodeExprList(pP
34eb8 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 72 65  arse, pEList, re
34eb9 67 52 65 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d  gResult, eDest==
34eba 53 52 54 5f 4f 75 74 70 75 74 29 3b 0d 0a 20 20  SRT_Output);..  
34ebb 7d 0d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e  }..  nColumn = n
34ebc 52 65 73 75 6c 74 43 6f 6c 3b 0d 0a 0d 0a 20 20  ResultCol;....  
34ebd 2f 2a 20 49 66 20 74 68 65 20 44 49 53 54 49 4e  /* If the DISTIN
34ebe 43 54 20 6b 65 79 77 6f 72 64 20 77 61 73 20 70  CT keyword was p
34ebf 72 65 73 65 6e 74 20 6f 6e 20 74 68 65 20 53 45  resent on the SE
34ec0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0d 0a  LECT statement..
34ec1 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f    ** and this ro
34ec2 77 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20  w has been seen 
34ec3 62 65 66 6f 72 65 2c 20 74 68 65 6e 20 64 6f 20  before, then do 
34ec4 6e 6f 74 20 6d 61 6b 65 20 74 68 69 73 20 72 6f  not make this ro
34ec5 77 0d 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 20  w..  ** part of 
34ec6 74 68 65 20 72 65 73 75 6c 74 2e 0d 0a 20 20 2a  the result...  *
34ec7 2f 0d 0a 20 20 69 66 28 20 68 61 73 44 69 73 74  /..  if( hasDist
34ec8 69 6e 63 74 20 29 7b 0d 0a 20 20 20 20 61 73 73  inct ){..    ass
34ec9 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
34eca 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
34ecb 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43  EList->nExpr==nC
34ecc 6f 6c 75 6d 6e 20 29 3b 0d 0a 20 20 20 20 63 6f  olumn );..    co
34ecd 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72 73  deDistinct(pPars
34ece 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69 43 6f  e, distinct, iCo
34ecf 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c  ntinue, nColumn,
34ed0 20 72 65 67 52 65 73 75 6c 74 29 3b 0d 0a 20 20   regResult);..  
34ed1 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d    if( pOrderBy==
34ed2 30 20 29 7b 0d 0a 20 20 20 20 20 20 63 6f 64 65  0 ){..      code
34ed3 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f  Offset(v, p, iCo
34ed4 6e 74 69 6e 75 65 29 3b 0d 0a 20 20 20 20 7d 0d  ntinue);..    }.
34ed5 0a 20 20 7d 0d 0a 0d 0a 20 20 73 77 69 74 63 68  .  }....  switch
34ed6 28 20 65 44 65 73 74 20 29 7b 0d 0a 20 20 20 20  ( eDest ){..    
34ed7 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  /* In this mode,
34ed8 20 77 72 69 74 65 20 65 61 63 68 20 71 75 65 72   write each quer
34ed9 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68 65 20  y result to the 
34eda 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  key of the tempo
34edb 72 61 72 79 0d 0a 20 20 20 20 2a 2a 20 74 61 62  rary..    ** tab
34edc 6c 65 20 69 50 61 72 6d 2e 0d 0a 20 20 20 20 2a  le iParm...    *
34edd 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
34ede 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
34edf 53 45 4c 45 43 54 0d 0a 20 20 20 20 63 61 73 65  SELECT..    case
34ee0 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0d 0a 20   SRT_Union: {.. 
34ee1 20 20 20 20 20 69 6e 74 20 72 31 3b 0d 0a 20 20       int r1;..  
34ee2 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
34ee3 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
34ee4 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  e);..      sqlit
34ee5 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
34ee6 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
34ee7 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
34ee8 6e 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 73  n, r1);..      s
34ee9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
34eea 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
34eeb 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b 0d 0a 20  , iParm, r1);.. 
34eec 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
34eed 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
34eee 65 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 62  e, r1);..      b
34eef 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  reak;..    }....
34ef0 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74      /* Construct
34ef1 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74   a record from t
34ef2 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2c  he query result,
34ef3 20 62 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0d   but instead of.
34ef4 0a 20 20 20 20 2a 2a 20 73 61 76 69 6e 67 20 74  .    ** saving t
34ef5 68 61 74 20 72 65 63 6f 72 64 2c 20 75 73 65 20  hat record, use 
34ef6 69 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 64  it as a key to d
34ef7 65 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73 20 66  elete elements f
34ef8 72 6f 6d 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20  rom..    ** the 
34ef9 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
34efa 69 50 61 72 6d 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  iParm...    */..
34efb 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 63      case SRT_Exc
34efc 65 70 74 3a 20 7b 0d 0a 20 20 20 20 20 20 73 71  ept: {..      sq
34efd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
34efe 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c  v, OP_IdxDelete,
34eff 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c   iParm, regResul
34f00 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20  t, nColumn);..  
34f01 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
34f02 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20  }..#endif....   
34f03 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65   /* Store the re
34f04 73 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69  sult as data usi
34f05 6e 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e  ng a unique key.
34f06 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61  ..    */..    ca
34f07 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0d 0a 20  se SRT_Table:.. 
34f08 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65     case SRT_Ephe
34f09 6d 54 61 62 3a 20 7b 0d 0a 20 20 20 20 20 20 69  mTab: {..      i
34f0a 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
34f0b 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
34f0c 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
34f0d 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 54  se( eDest==SRT_T
34f0e 61 62 6c 65 20 29 3b 0d 0a 20 20 20 20 20 20 74  able );..      t
34f0f 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
34f10 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b 0d  SRT_EphemTab );.
34f11 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
34f12 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
34f13 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65  akeRecord, regRe
34f14 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72  sult, nColumn, r
34f15 31 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  1);..      if( p
34f16 4f 72 64 65 72 42 79 20 29 7b 0d 0a 20 20 20 20  OrderBy ){..    
34f17 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
34f18 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
34f19 72 42 79 2c 20 70 2c 20 72 31 29 3b 0d 0a 20 20  rBy, p, r1);..  
34f1a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
34f1b 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c      int r2 = sql
34f1c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
34f1d 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 20  Parse);..       
34f1e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34f1f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
34f20 64 2c 20 69 50 61 72 6d 2c 20 72 32 29 3b 0d 0a  d, iParm, r2);..
34f21 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
34f22 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
34f23 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72  Insert, iParm, r
34f24 31 2c 20 72 32 29 3b 0d 0a 20 20 20 20 20 20 20  1, r2);..       
34f25 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
34f26 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
34f27 50 50 45 4e 44 29 3b 0d 0a 20 20 20 20 20 20 20  PPEND);..       
34f28 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
34f29 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
34f2a 32 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  2);..      }..  
34f2b 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
34f2c 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
34f2d 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 62 72  , r1);..      br
34f2e 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23  eak;..    }....#
34f2f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34f30 49 54 5f 53 55 42 51 55 45 52 59 0d 0a 20 20 20  IT_SUBQUERY..   
34f31 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72   /* If we are cr
34f32 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72  eating a set for
34f33 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45   an "expr IN (SE
34f34 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74  LECT ...)" const
34f35 72 75 63 74 2c 0d 0a 20 20 20 20 2a 2a 20 74 68  ruct,..    ** th
34f36 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20  en there should 
34f37 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d  be a single item
34f38 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20   on the stack.  
34f39 57 72 69 74 65 20 74 68 69 73 0d 0a 20 20 20 20  Write this..    
34f3a 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65  ** item into the
34f3b 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20   set table with 
34f3c 62 6f 67 75 73 20 64 61 74 61 2e 0d 0a 20 20 20  bogus data...   
34f3d 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 52   */..    case SR
34f3e 54 5f 53 65 74 3a 20 7b 0d 0a 20 20 20 20 20 20  T_Set: {..      
34f3f 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
34f40 3d 31 20 29 3b 0d 0a 20 20 20 20 20 20 70 2d 3e  =1 );..      p->
34f41 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74  affinity = sqlit
34f42 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
34f43 79 28 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  y(pEList->a[0].p
34f44 45 78 70 72 2c 20 70 44 65 73 74 2d 3e 61 66 66  Expr, pDest->aff
34f45 69 6e 69 74 79 29 3b 0d 0a 20 20 20 20 20 20 69  inity);..      i
34f46 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0d 0a  f( pOrderBy ){..
34f47 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20 66 69          /* At fi
34f48 72 73 74 20 67 6c 61 6e 63 65 20 79 6f 75 20 77  rst glance you w
34f49 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65 20 63 6f  ould think we co
34f4a 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f 75 74  uld optimize out
34f4b 20 74 68 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a   the..        **
34f4c 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74 68 69   ORDER BY in thi
34f4d 73 20 63 61 73 65 20 73 69 6e 63 65 20 74 68 65  s case since the
34f4e 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72 69 65   order of entrie
34f4f 73 20 69 6e 20 74 68 65 20 73 65 74 0d 0a 20 20  s in the set..  
34f50 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f        ** does no
34f51 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20 74  t matter.  But t
34f52 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 61 20  here might be a 
34f53 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69 6e  LIMIT clause, in
34f54 20 77 68 69 63 68 0d 0a 20 20 20 20 20 20 20 20   which..        
34f55 2a 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64 65  ** case the orde
34f56 72 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f  r does matter */
34f57 0d 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  ..        pushOn
34f58 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
34f59 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65   pOrderBy, p, re
34f5a 67 52 65 73 75 6c 74 29 3b 0d 0a 20 20 20 20 20  gResult);..     
34f5b 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
34f5c 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
34f5d 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
34f5e 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  se);..        sq
34f5f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
34f60 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
34f61 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c 20  , regResult, 1, 
34f62 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79  r1, &p->affinity
34f63 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  , 1);..        s
34f64 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
34f65 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
34f66 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c  arse, regResult,
34f67 20 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71   1);..        sq
34f68 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
34f69 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
34f6a 20 69 50 61 72 6d 2c 20 72 31 29 3b 0d 0a 20 20   iParm, r1);..  
34f6b 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
34f6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
34f6d 73 65 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20 20  se, r1);..      
34f6e 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  }..      break;.
34f6f 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
34f70 20 49 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73   If any row exis
34f71 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  t in the result 
34f72 73 65 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74  set, record that
34f73 20 66 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e   fact and abort.
34f74 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61  ..    */..    ca
34f75 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20 7b  se SRT_Exists: {
34f76 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
34f77 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
34f78 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61 72  Integer, 1, iPar
34f79 6d 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68  m);..      /* Th
34f7a 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
34f7b 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ill terminate th
34f7c 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
34f7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
34f7e 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
34f7f 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
34f80 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
34f81 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
34f82 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0d 0a  pression, then..
34f83 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65      ** store the
34f84 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
34f85 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f  appropriate memo
34f86 72 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61  ry cell and brea
34f87 6b 20 6f 75 74 0d 0a 20 20 20 20 2a 2a 20 6f 66  k out..    ** of
34f88 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0d   the scan loop..
34f89 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73  .    */..    cas
34f8a 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0d 0a 20 20  e SRT_Mem: {..  
34f8b 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
34f8c 75 6d 6e 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 20  umn==1 );..     
34f8d 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
34f8e 0d 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  ..        pushOn
34f8f 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
34f90 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65   pOrderBy, p, re
34f91 67 52 65 73 75 6c 74 29 3b 0d 0a 20 20 20 20 20  gResult);..     
34f92 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
34f93 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
34f94 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67  Move(pParse, reg
34f95 52 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c 20 31  Result, iParm, 1
34f96 29 3b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  );..        /* T
34f97 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
34f98 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
34f99 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
34f9a 20 2a 2f 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   */..      }..  
34f9b 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
34f9c 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66  }..#endif /* #if
34f9d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
34f9e 5f 53 55 42 51 55 45 52 59 20 2a 2f 0d 0a 0d 0a  _SUBQUERY */....
34f9f 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65 20      /* Send the 
34fa0 64 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c 6c  data to the call
34fa1 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72  back function or
34fa2 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
34fa3 2e 20 20 49 6e 20 74 68 65 0d 0a 20 20 20 20 2a  .  In the..    *
34fa4 2a 20 63 61 73 65 20 6f 66 20 61 20 73 75 62 72  * case of a subr
34fa5 6f 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72  outine, the subr
34fa6 6f 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73  outine itself is
34fa7 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
34fa8 0d 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67  ..    ** popping
34fa9 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74   the data from t
34faa 68 65 20 73 74 61 63 6b 2e 0d 0a 20 20 20 20 2a  he stack...    *
34fab 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  /..    case SRT_
34fac 43 6f 72 6f 75 74 69 6e 65 3a 0d 0a 20 20 20 20  Coroutine:..    
34fad 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a  case SRT_Output:
34fae 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61   {..      testca
34faf 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43  se( eDest==SRT_C
34fb0 6f 72 6f 75 74 69 6e 65 20 29 3b 0d 0a 20 20 20  oroutine );..   
34fb1 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
34fb2 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29  st==SRT_Output )
34fb3 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  ;..      if( pOr
34fb4 64 65 72 42 79 20 29 7b 0d 0a 20 20 20 20 20 20  derBy ){..      
34fb5 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
34fb6 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
34fb7 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  rse);..        s
34fb8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
34fb9 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
34fba 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  d, regResult, nC
34fbb 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0d 0a 20 20 20  olumn, r1);..   
34fbc 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72       pushOntoSor
34fbd 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64  ter(pParse, pOrd
34fbe 65 72 42 79 2c 20 70 2c 20 72 31 29 3b 0d 0a 20  erBy, p, r1);.. 
34fbf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
34fc0 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
34fc1 72 73 65 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20  rse, r1);..     
34fc2 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74   }else if( eDest
34fc3 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20  ==SRT_Coroutine 
34fc4 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
34fc5 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
34fc6 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74   OP_Yield, pDest
34fc7 2d 3e 69 50 61 72 6d 29 3b 0d 0a 20 20 20 20 20  ->iParm);..     
34fc8 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
34fc9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34fca 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
34fcb 6f 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  ow, regResult, n
34fcc 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 20 20 20 20  Column);..      
34fcd 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
34fce 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
34fcf 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
34fd0 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0d 0a 20  lt, nColumn);.. 
34fd1 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72       }..      br
34fd2 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23  eak;..    }....#
34fd3 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
34fd4 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
34fd5 0d 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72 64  ..    /* Discard
34fd6 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54   the results.  T
34fd7 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
34fd8 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
34fd9 73 20 69 6e 73 69 64 65 0d 0a 20 20 20 20 2a 2a  s inside..    **
34fda 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 54   the body of a T
34fdb 52 49 47 47 45 52 2e 20 20 54 68 65 20 70 75 72  RIGGER.  The pur
34fdc 70 6f 73 65 20 6f 66 20 73 75 63 68 20 73 65 6c  pose of such sel
34fdd 65 63 74 73 20 69 73 20 74 6f 20 63 61 6c 6c 0d  ects is to call.
34fde 0a 20 20 20 20 2a 2a 20 75 73 65 72 2d 64 65 66  .    ** user-def
34fdf 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74  ined functions t
34fe0 68 61 74 20 68 61 76 65 20 73 69 64 65 20 65 66  hat have side ef
34fe1 66 65 63 74 73 2e 20 20 57 65 20 64 6f 20 6e 6f  fects.  We do no
34fe2 74 20 63 61 72 65 0d 0a 20 20 20 20 2a 2a 20 61  t care..    ** a
34fe3 62 6f 75 74 20 74 68 65 20 61 63 74 75 61 6c 20  bout the actual 
34fe4 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73  results of the s
34fe5 65 6c 65 63 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  elect...    */..
34fe6 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a      default: {..
34fe7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 44        assert( eD
34fe8 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64  est==SRT_Discard
34fe9 20 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b   );..      break
34fea 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  ;..    }..#endif
34feb 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4a 75  ..  }....  /* Ju
34fec 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
34fed 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65   the loop if the
34fee 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
34fef 64 2e 20 20 45 78 63 65 70 74 2c 20 69 66 0d 0a  d.  Except, if..
34ff0 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20    ** there is a 
34ff1 73 6f 72 74 65 72 2c 20 69 6e 20 77 68 69 63 68  sorter, in which
34ff2 20 63 61 73 65 20 74 68 65 20 73 6f 72 74 65 72   case the sorter
34ff3 20 68 61 73 20 61 6c 72 65 61 64 79 20 6c 69 6d   has already lim
34ff4 69 74 65 64 0d 0a 20 20 2a 2a 20 74 68 65 20 6f  ited..  ** the o
34ff5 75 74 70 75 74 20 66 6f 72 20 75 73 2e 0d 0a 20  utput for us... 
34ff6 20 2a 2f 0d 0a 20 20 69 66 28 20 70 4f 72 64 65   */..  if( pOrde
34ff7 72 42 79 3d 3d 30 20 26 26 20 70 2d 3e 69 4c 69  rBy==0 && p->iLi
34ff8 6d 69 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  mit ){..    sqli
34ff9 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
34ffa 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69   OP_IfZero, p->i
34ffb 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 2c 20 2d  Limit, iBreak, -
34ffc 31 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  1);..  }..}..../
34ffd 2a 0d 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65  *..** Given an e
34ffe 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20  xpression list, 
34fff 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79 49 6e  generate a KeyIn
35000 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
35001 74 20 72 65 63 6f 72 64 73 0d 0a 2a 2a 20 74 68  t records..** th
35002 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
35003 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65 78  ence for each ex
35004 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61 74  pression in that
35005 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
35006 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
35007 20 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20   ExprList is an 
35008 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
35009 50 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e  P BY clause then
3500a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0d 0a   the resulting..
3500b 2a 2a 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  ** KeyInfo struc
3500c 74 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69  ture is appropri
3500d 61 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69  ate for initiali
3500e 7a 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69  zing a virtual i
3500f 6e 64 65 78 20 74 6f 0d 0a 2a 2a 20 69 6d 70 6c  ndex to..** impl
35010 65 6d 65 6e 74 20 74 68 61 74 20 63 6c 61 75 73  ement that claus
35011 65 2e 20 20 49 66 20 74 68 65 20 45 78 70 72 4c  e.  If the ExprL
35012 69 73 74 20 69 73 20 74 68 65 20 72 65 73 75 6c  ist is the resul
35013 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t set of a SELEC
35014 54 0d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 4b  T..** then the K
35015 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
35016 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
35017 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  for initializing
35018 20 61 20 76 69 72 74 75 61 6c 0d 0a 2a 2a 20 69   a virtual..** i
35019 6e 64 65 78 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ndex to implemen
3501a 74 20 61 20 44 49 53 54 49 4e 43 54 20 74 65 73  t a DISTINCT tes
3501b 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 70 61 63 65  t...**..** Space
3501c 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 4b 65 79   to hold the Key
3501d 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
3501e 73 20 6f 62 74 61 69 6e 20 66 72 6f 6d 20 6d 61  s obtain from ma
3501f 6c 6c 6f 63 2e 20 20 54 68 65 20 63 61 6c 6c 69  lloc.  The calli
35020 6e 67 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ng..** function 
35021 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
35022 6f 72 20 73 65 65 69 6e 67 20 74 68 61 74 20 74  or seeing that t
35023 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
35024 20 65 76 65 6e 74 75 61 6c 6c 79 0d 0a 2a 2a 20   eventually..** 
35025 66 72 65 65 64 2e 20 20 41 64 64 20 74 68 65 20  freed.  Add the 
35026 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
35027 65 20 74 6f 20 74 68 65 20 50 34 20 66 69 65 6c  e to the P4 fiel
35028 64 20 6f 66 20 61 6e 20 6f 70 63 6f 64 65 20 75  d of an opcode u
35029 73 69 6e 67 0d 0a 2a 2a 20 50 34 5f 4b 45 59 49  sing..** P4_KEYI
3502a 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 73 20 74  NFO_HANDOFF is t
3502b 68 65 20 75 73 75 61 6c 20 77 61 79 20 6f 66 20  he usual way of 
3502c 64 65 61 6c 69 6e 67 20 77 69 74 68 20 74 68 69  dealing with thi
3502d 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 4b  s...*/..static K
3502e 65 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f 46  eyInfo *keyInfoF
3502f 72 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72 73  romExprList(Pars
35030 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c  e *pParse, ExprL
35031 69 73 74 20 2a 70 4c 69 73 74 29 7b 0d 0a 20 20  ist *pList){..  
35032 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
35033 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 69 6e 74  arse->db;..  int
35034 20 6e 45 78 70 72 3b 0d 0a 20 20 4b 65 79 49 6e   nExpr;..  KeyIn
35035 66 6f 20 2a 70 49 6e 66 6f 3b 0d 0a 20 20 73 74  fo *pInfo;..  st
35036 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
35037 65 6d 20 2a 70 49 74 65 6d 3b 0d 0a 20 20 69 6e  em *pItem;..  in
35038 74 20 69 3b 0d 0a 0d 0a 20 20 6e 45 78 70 72 20  t i;....  nExpr 
35039 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0d  = pList->nExpr;.
3503a 0a 20 20 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74  .  pInfo = sqlit
3503b 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
3503c 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f  b, sizeof(*pInfo
3503d 29 20 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f  ) + nExpr*(sizeo
3503e 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29  f(CollSeq*)+1) )
3503f 3b 0d 0a 20 20 69 66 28 20 70 49 6e 66 6f 20 29  ;..  if( pInfo )
35040 7b 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53  {..    pInfo->aS
35041 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29  ortOrder = (u8*)
35042 26 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45  &pInfo->aColl[nE
35043 78 70 72 5d 3b 0d 0a 20 20 20 20 70 49 6e 66 6f  xpr];..    pInfo
35044 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
35045 6e 45 78 70 72 3b 0d 0a 20 20 20 20 70 49 6e 66  nExpr;..    pInf
35046 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  o->enc = ENC(db)
35047 3b 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 64 62  ;..    pInfo->db
35048 20 3d 20 64 62 3b 0d 0a 20 20 20 20 66 6f 72 28   = db;..    for(
35049 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  i=0, pItem=pList
3504a 2d 3e 61 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b  ->a; i<nExpr; i+
3504b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a 20 20  +, pItem++){..  
3504c 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
3504d 6c 6c 3b 0d 0a 20 20 20 20 20 20 70 43 6f 6c 6c  ll;..      pColl
3504e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
3504f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49  llSeq(pParse, pI
35050 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0d 0a 20 20  tem->pExpr);..  
35051 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
35052 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  {..        pColl
35053 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
35054 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
35055 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69    pInfo->aColl[i
35056 5d 20 3d 20 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20  ] = pColl;..    
35057 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72    pInfo->aSortOr
35058 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e  der[i] = pItem->
35059 73 6f 72 74 4f 72 64 65 72 3b 0d 0a 20 20 20 20  sortOrder;..    
3505a 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
3505b 20 70 49 6e 66 6f 3b 0d 0a 7d 0d 0a 0d 0a 23 69   pInfo;..}....#i
3505c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3505d 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
3505e 54 0d 0a 2f 2a 0d 0a 2a 2a 20 4e 61 6d 65 20 6f  T../*..** Name o
3505f 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  f the connection
35060 20 6f 70 65 72 61 74 6f 72 2c 20 75 73 65 64 20   operator, used 
35061 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
35062 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
35063 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 65 6c 65  const char *sele
35064 63 74 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64 29  ctOpName(int id)
35065 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 20  {..  char *z;.. 
35066 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0d 0a   switch( id ){..
35067 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a      case TK_ALL:
35068 20 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f         z = "UNIO
35069 4e 20 41 4c 4c 22 3b 20 20 20 62 72 65 61 6b 3b  N ALL";   break;
3506a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  ..    case TK_IN
3506b 54 45 52 53 45 43 54 3a 20 7a 20 3d 20 22 49 4e  TERSECT: z = "IN
3506c 54 45 52 53 45 43 54 22 3b 20 20 20 62 72 65 61  TERSECT";   brea
3506d 6b 3b 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  k;..    case TK_
3506e 45 58 43 45 50 54 3a 20 20 20 20 7a 20 3d 20 22  EXCEPT:    z = "
3506f 45 58 43 45 50 54 22 3b 20 20 20 20 20 20 62 72  EXCEPT";      br
35070 65 61 6b 3b 0d 0a 20 20 20 20 64 65 66 61 75 6c  eak;..    defaul
35071 74 3a 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d  t:           z =
35072 20 22 55 4e 49 4f 4e 22 3b 20 20 20 20 20 20 20   "UNION";       
35073 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 72  break;..  }..  r
35074 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 23 65 6e  eturn z;..}..#en
35075 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
35076 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
35077 43 54 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66  CT */....#ifndef
35078 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
35079 4c 41 49 4e 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 6c  LAIN../*..** Unl
3507a 65 73 73 20 61 6e 20 22 45 58 50 4c 41 49 4e 20  ess an "EXPLAIN 
3507b 51 55 45 52 59 20 50 4c 41 4e 22 20 63 6f 6d 6d  QUERY PLAN" comm
3507c 61 6e 64 20 69 73 20 62 65 69 6e 67 20 70 72 6f  and is being pro
3507d 63 65 73 73 65 64 2c 20 74 68 69 73 20 66 75 6e  cessed, this fun
3507e 63 74 69 6f 6e 0d 0a 2a 2a 20 69 73 20 61 20 6e  ction..** is a n
3507f 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
35080 20 69 74 20 61 64 64 73 20 61 20 73 69 6e 67 6c   it adds a singl
35081 65 20 72 6f 77 20 6f 66 20 6f 75 74 70 75 74 20  e row of output 
35082 74 6f 20 74 68 65 20 45 51 50 20 72 65 73 75 6c  to the EQP resul
35083 74 2c 0d 0a 2a 2a 20 77 68 65 72 65 20 74 68 65  t,..** where the
35084 20 63 61 70 74 69 6f 6e 20 69 73 20 6f 66 20 74   caption is of t
35085 68 65 20 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a  he form:..**..**
35086 20 20 20 22 55 53 45 20 54 45 4d 50 20 42 2d 54     "USE TEMP B-T
35087 52 45 45 20 46 4f 52 20 78 78 78 22 0d 0a 2a 2a  REE FOR xxx"..**
35088 0d 0a 2a 2a 20 77 68 65 72 65 20 78 78 78 20 69  ..** where xxx i
35089 73 20 6f 6e 65 20 6f 66 20 22 44 49 53 54 49 4e  s one of "DISTIN
3508a 43 54 22 2c 20 22 4f 52 44 45 52 20 42 59 22 20  CT", "ORDER BY" 
3508b 6f 72 20 22 47 52 4f 55 50 20 42 59 22 2e 20 45  or "GROUP BY". E
3508c 78 61 63 74 6c 79 20 77 68 69 63 68 0d 0a 2a 2a  xactly which..**
3508d 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
3508e 79 20 74 68 65 20 7a 55 73 61 67 65 20 61 72 67  y the zUsage arg
3508f 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ument...*/..stat
35090 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 54  ic void explainT
35091 65 6d 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a  empTable(Parse *
35092 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
35093 61 72 20 2a 7a 55 73 61 67 65 29 7b 0d 0a 20 20  ar *zUsage){..  
35094 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  if( pParse->expl
35095 61 69 6e 3d 3d 32 20 29 7b 0d 0a 20 20 20 20 56  ain==2 ){..    V
35096 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
35097 3e 70 56 64 62 65 3b 0d 0a 20 20 20 20 63 68 61  >pVdbe;..    cha
35098 72 20 2a 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65  r *zMsg = sqlite
35099 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d  3MPrintf(pParse-
3509a 3e 64 62 2c 20 22 55 53 45 20 54 45 4d 50 20 42  >db, "USE TEMP B
3509b 2d 54 52 45 45 20 46 4f 52 20 25 73 22 2c 20 7a  -TREE FOR %s", z
3509c 55 73 61 67 65 29 3b 0d 0a 20 20 20 20 73 71 6c  Usage);..    sql
3509d 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
3509e 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20 70 50  , OP_Explain, pP
3509f 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 2c  arse->iSelectId,
350a0 20 30 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f   0, 0, zMsg, P4_
350a1 44 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 7d 0d 0a  DYNAMIC);..  }..
350a2 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 73 73 69  }..../*..** Assi
350a3 67 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 62 20  gn expression b 
350a4 74 6f 20 6c 76 61 6c 75 65 20 61 2e 20 41 20 73  to lvalue a. A s
350a5 65 63 6f 6e 64 2c 20 6e 6f 2d 6f 70 2c 20 76 65  econd, no-op, ve
350a6 72 73 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 61  rsion of this ma
350a7 63 72 6f 0d 0a 2a 2a 20 69 73 20 70 72 6f 76 69  cro..** is provi
350a8 64 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ded when SQLITE_
350a9 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 69 73 20  OMIT_EXPLAIN is 
350aa 64 65 66 69 6e 65 64 2e 20 54 68 69 73 20 61 6c  defined. This al
350ab 6c 6f 77 73 20 74 68 65 20 63 6f 64 65 0d 0a 2a  lows the code..*
350ac 2a 20 69 6e 20 73 71 6c 69 74 65 33 53 65 6c 65  * in sqlite3Sele
350ad 63 74 28 29 20 74 6f 20 61 73 73 69 67 6e 20 76  ct() to assign v
350ae 61 6c 75 65 73 20 74 6f 20 73 74 72 75 63 74 75  alues to structu
350af 72 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62  re member variab
350b0 6c 65 73 20 74 68 61 74 0d 0a 2a 2a 20 6f 6e 6c  les that..** onl
350b1 79 20 65 78 69 73 74 20 69 66 20 53 51 4c 49 54  y exist if SQLIT
350b2 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 69  E_OMIT_EXPLAIN i
350b3 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 77 69  s not defined wi
350b4 74 68 6f 75 74 20 70 6f 6c 6c 75 74 69 6e 67 20  thout polluting 
350b5 74 68 65 0d 0a 2a 2a 20 63 6f 64 65 20 77 69 74  the..** code wit
350b6 68 20 23 69 66 6e 64 65 66 20 64 69 72 65 63 74  h #ifndef direct
350b7 69 76 65 73 2e 0d 0a 2a 2f 0d 0a 23 20 64 65 66  ives...*/..# def
350b8 69 6e 65 20 65 78 70 6c 61 69 6e 53 65 74 49 6e  ine explainSetIn
350b9 74 65 67 65 72 28 61 2c 20 62 29 20 61 20 3d 20  teger(a, b) a = 
350ba 62 0d 0a 0d 0a 23 65 6c 73 65 0d 0a 2f 2a 20 4e  b....#else../* N
350bb 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66  o-op versions of
350bc 20 74 68 65 20 65 78 70 6c 61 69 6e 58 58 58 28   the explainXXX(
350bd 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  ) functions and 
350be 6d 61 63 72 6f 73 2e 20 2a 2f 0d 0a 23 20 64 65  macros. */..# de
350bf 66 69 6e 65 20 65 78 70 6c 61 69 6e 54 65 6d 70  fine explainTemp
350c0 54 61 62 6c 65 28 79 2c 7a 29 0d 0a 23 20 64 65  Table(y,z)..# de
350c1 66 69 6e 65 20 65 78 70 6c 61 69 6e 53 65 74 49  fine explainSetI
350c2 6e 74 65 67 65 72 28 79 2c 7a 29 0d 0a 23 65 6e  nteger(y,z)..#en
350c3 64 69 66 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69  dif....#if !defi
350c4 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
350c5 45 58 50 4c 41 49 4e 29 20 26 26 20 21 64 65 66  EXPLAIN) && !def
350c6 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
350c7 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
350c8 29 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 6c 65 73 73  )../*..** Unless
350c9 20 61 6e 20 22 45 58 50 4c 41 49 4e 20 51 55 45   an "EXPLAIN QUE
350ca 52 59 20 50 4c 41 4e 22 20 63 6f 6d 6d 61 6e 64  RY PLAN" command
350cb 20 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 73   is being proces
350cc 73 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69  sed, this functi
350cd 6f 6e 0d 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  on..** is a no-o
350ce 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74  p. Otherwise, it
350cf 20 61 64 64 73 20 61 20 73 69 6e 67 6c 65 20 72   adds a single r
350d0 6f 77 20 6f 66 20 6f 75 74 70 75 74 20 74 6f 20  ow of output to 
350d1 74 68 65 20 45 51 50 20 72 65 73 75 6c 74 2c 0d  the EQP result,.
350d2 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 63 61  .** where the ca
350d3 70 74 69 6f 6e 20 69 73 20 6f 66 20 6f 6e 65 20  ption is of one 
350d4 6f 66 20 74 68 65 20 74 77 6f 20 66 6f 72 6d 73  of the two forms
350d5 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 22 43 4f 4d  :..**..**   "COM
350d6 50 4f 53 49 54 45 20 53 55 42 51 55 45 52 49 45  POSITE SUBQUERIE
350d7 53 20 69 53 75 62 31 20 61 6e 64 20 69 53 75 62  S iSub1 and iSub
350d8 32 20 28 6f 70 29 22 0d 0a 2a 2a 20 20 20 22 43  2 (op)"..**   "C
350d9 4f 4d 50 4f 53 49 54 45 20 53 55 42 51 55 45 52  OMPOSITE SUBQUER
350da 49 45 53 20 69 53 75 62 31 20 61 6e 64 20 69 53  IES iSub1 and iS
350db 75 62 32 20 55 53 49 4e 47 20 54 45 4d 50 20 42  ub2 USING TEMP B
350dc 2d 54 52 45 45 20 28 6f 70 29 22 0d 0a 2a 2a 0d  -TREE (op)"..**.
350dd 0a 2a 2a 20 77 68 65 72 65 20 69 53 75 62 31 20  .** where iSub1 
350de 61 6e 64 20 69 53 75 62 32 20 61 72 65 20 74 68  and iSub2 are th
350df 65 20 69 6e 74 65 67 65 72 73 20 70 61 73 73 65  e integers passe
350e0 64 20 61 73 20 74 68 65 20 63 6f 72 72 65 73 70  d as the corresp
350e1 6f 6e 64 69 6e 67 0d 0a 2a 2a 20 66 75 6e 63 74  onding..** funct
350e2 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2c 20  ion parameters, 
350e3 61 6e 64 20 6f 70 20 69 73 20 74 68 65 20 74 65  and op is the te
350e4 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  xt representatio
350e5 6e 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74  n of the paramet
350e6 65 72 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 61  er..** of the sa
350e7 6d 65 20 6e 61 6d 65 2e 20 54 68 65 20 70 61 72  me name. The par
350e8 61 6d 65 74 65 72 20 22 6f 70 22 20 6d 75 73 74  ameter "op" must
350e9 20 62 65 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 4e   be one of TK_UN
350ea 49 4f 4e 2c 20 54 4b 5f 45 58 43 45 50 54 2c 0d  ION, TK_EXCEPT,.
350eb 0a 2a 2a 20 54 4b 5f 49 4e 54 45 52 53 45 43 54  .** TK_INTERSECT
350ec 20 6f 72 20 54 4b 5f 41 4c 4c 2e 20 54 68 65 20   or TK_ALL. The 
350ed 66 69 72 73 74 20 66 6f 72 6d 20 69 73 20 75 73  first form is us
350ee 65 64 20 69 66 20 61 72 67 75 6d 65 6e 74 20 62  ed if argument b
350ef 55 73 65 54 6d 70 20 69 73 20 0d 0a 2a 2a 20 66  UseTmp is ..** f
350f0 61 6c 73 65 2c 20 6f 72 20 74 68 65 20 73 65 63  alse, or the sec
350f1 6f 6e 64 20 66 6f 72 6d 20 69 66 20 69 74 20 69  ond form if it i
350f2 73 20 74 72 75 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  s true...*/..sta
350f3 74 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e  tic void explain
350f4 43 6f 6d 70 6f 73 69 74 65 28 0d 0a 20 20 50 61  Composite(..  Pa
350f5 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
350f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
350f7 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
350f8 2f 0d 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  /..  int op,    
350f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
350fa 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
350fb 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f 45 58 43 45  K_UNION, TK_EXCE
350fc 50 54 20 65 74 63 2e 20 2a 2f 0d 0a 20 20 69 6e  PT etc. */..  in
350fd 74 20 69 53 75 62 31 2c 20 20 20 20 20 20 20 20  t iSub1,        
350fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
350ff 20 53 75 62 71 75 65 72 79 20 69 64 20 31 20 2a   Subquery id 1 *
35100 2f 0d 0a 20 20 69 6e 74 20 69 53 75 62 32 2c 20  /..  int iSub2, 
35101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35102 20 20 20 20 20 2f 2a 20 53 75 62 71 75 65 72 79       /* Subquery
35103 20 69 64 20 32 20 2a 2f 0d 0a 20 20 69 6e 74 20   id 2 */..  int 
35104 62 55 73 65 54 6d 70 20 20 20 20 20 20 20 20 20  bUseTmp         
35105 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
35106 72 75 65 20 69 66 20 61 20 74 65 6d 70 20 74 61  rue if a temp ta
35107 62 6c 65 20 77 61 73 20 75 73 65 64 20 2a 2f 0d  ble was used */.
35108 0a 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 6f  .){..  assert( o
35109 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 7c 7c 20 6f  p==TK_UNION || o
3510a 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20  p==TK_EXCEPT || 
3510b 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
3510c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29   || op==TK_ALL )
3510d 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
3510e 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0d 0a  >explain==2 ){..
3510f 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50      Vdbe *v = pP
35110 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20  arse->pVdbe;..  
35111 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 73    char *zMsg = s
35112 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 0d 0a  qlite3MPrintf(..
35113 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
35114 64 62 2c 20 22 43 4f 4d 50 4f 55 4e 44 20 53 55  db, "COMPOUND SU
35115 42 51 55 45 52 49 45 53 20 25 64 20 41 4e 44 20  BQUERIES %d AND 
35116 25 64 20 25 73 28 25 73 29 22 2c 20 69 53 75 62  %d %s(%s)", iSub
35117 31 2c 20 69 53 75 62 32 2c 0d 0a 20 20 20 20 20  1, iSub2,..     
35118 20 20 20 62 55 73 65 54 6d 70 3f 22 55 53 49 4e     bUseTmp?"USIN
35119 47 20 54 45 4d 50 20 42 2d 54 52 45 45 20 22 3a  G TEMP B-TREE ":
3511a 22 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65  "", selectOpName
3511b 28 6f 70 29 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  (op)..    );..  
3511c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3511d 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69  Op4(v, OP_Explai
3511e 6e 2c 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65  n, pParse->iSele
3511f 63 74 49 64 2c 20 30 2c 20 30 2c 20 7a 4d 73 67  ctId, 0, 0, zMsg
35120 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a  , P4_DYNAMIC);..
35121 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 2f    }..}..#else../
35122 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73  * No-op versions
35123 20 6f 66 20 74 68 65 20 65 78 70 6c 61 69 6e 58   of the explainX
35124 58 58 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61  XX() functions a
35125 6e 64 20 6d 61 63 72 6f 73 2e 20 2a 2f 0d 0a 23  nd macros. */..#
35126 20 64 65 66 69 6e 65 20 65 78 70 6c 61 69 6e 43   define explainC
35127 6f 6d 70 6f 73 69 74 65 28 76 2c 77 2c 78 2c 79  omposite(v,w,x,y
35128 2c 7a 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  ,z)..#endif..../
35129 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e  *..** If the inn
3512a 65 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65  er loop was gene
3512b 72 61 74 65 64 20 75 73 69 6e 67 20 61 20 6e 6f  rated using a no
3512c 6e 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 79 20  n-null pOrderBy 
3512d 61 72 67 75 6d 65 6e 74 2c 0d 0a 2a 2a 20 74 68  argument,..** th
3512e 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 77  en the results w
3512f 65 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20  ere placed in a 
35130 73 6f 72 74 65 72 2e 20 20 41 66 74 65 72 20 74  sorter.  After t
35131 68 65 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d 69  he loop is termi
35132 6e 61 74 65 64 0d 0a 2a 2a 20 77 65 20 6e 65 65  nated..** we nee
35133 64 20 74 6f 20 72 75 6e 20 74 68 65 20 73 6f 72  d to run the sor
35134 74 65 72 20 61 6e 64 20 6f 75 74 70 75 74 20 74  ter and output t
35135 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68 65  he results.  The
35136 20 66 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 72   following..** r
35137 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
35138 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65 64   the code needed
35139 20 74 6f 20 64 6f 20 74 68 61 74 2e 0d 0a 2a 2f   to do that...*/
3513a 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  ..static void ge
3513b 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28 0d  nerateSortTail(.
3513c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3513d 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
3513e 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 53 65  context */..  Se
3513f 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
35140 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
35141 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 56 64  atement */..  Vd
35142 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20  be *v,          
35143 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
35144 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
35145 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d  */..  int nColum
35146 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  n,      /* Numbe
35147 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
35148 64 61 74 61 20 2a 2f 0d 0a 20 20 53 65 6c 65 63  data */..  Selec
35149 74 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20  tDest *pDest /* 
3514a 57 72 69 74 65 20 74 68 65 20 73 6f 72 74 65 64  Write the sorted
3514b 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
3514c 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 61 64 64 72  ..){..  int addr
3514d 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56  Break = sqlite3V
3514e 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
3514f 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
35150 65 20 74 6f 20 65 78 69 74 20 6c 6f 6f 70 20 2a  e to exit loop *
35151 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e  /..  int addrCon
35152 74 69 6e 75 65 20 3d 20 73 71 6c 69 74 65 33 56  tinue = sqlite3V
35153 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
35154 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66    /* Jump here f
35155 6f 72 20 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f  or next cycle */
35156 0d 0a 20 20 69 6e 74 20 61 64 64 72 3b 0d 0a 20  ..  int addr;.. 
35157 20 69 6e 74 20 69 54 61 62 3b 0d 0a 20 20 69 6e   int iTab;..  in
35158 74 20 70 73 65 75 64 6f 54 61 62 20 3d 20 30 3b  t pseudoTab = 0;
35159 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  ..  ExprList *pO
3515a 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64  rderBy = p->pOrd
3515b 65 72 42 79 3b 0d 0a 0d 0a 20 20 69 6e 74 20 65  erBy;....  int e
3515c 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44  Dest = pDest->eD
3515d 65 73 74 3b 0d 0a 20 20 69 6e 74 20 69 50 61 72  est;..  int iPar
3515e 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  m = pDest->iParm
3515f 3b 0d 0a 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f  ;....  int regRo
35160 77 3b 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f 77  w;..  int regRow
35161 69 64 3b 0d 0a 0d 0a 20 20 69 54 61 62 20 3d 20  id;....  iTab = 
35162 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
35163 6f 72 3b 0d 0a 20 20 72 65 67 52 6f 77 20 3d 20  or;..  regRow = 
35164 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
35165 67 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 69 66  g(pParse);..  if
35166 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ( eDest==SRT_Out
35167 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52  put || eDest==SR
35168 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0d 0a  T_Coroutine ){..
35169 20 20 20 20 70 73 65 75 64 6f 54 61 62 20 3d 20      pseudoTab = 
3516a 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d  pParse->nTab++;.
3516b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3516c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
3516d 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f 54  nPseudo, pseudoT
3516e 61 62 2c 20 72 65 67 52 6f 77 2c 20 6e 43 6f 6c  ab, regRow, nCol
3516f 75 6d 6e 29 3b 0d 0a 20 20 20 20 72 65 67 52 6f  umn);..    regRo
35170 77 69 64 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73  wid = 0;..  }els
35171 65 7b 0d 0a 20 20 20 20 72 65 67 52 6f 77 69 64  e{..    regRowid
35172 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
35173 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d 0a 20  pReg(pParse);.. 
35174 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c   }..  if( p->sel
35175 46 6c 61 67 73 20 26 20 53 46 5f 55 73 65 53 6f  Flags & SF_UseSo
35176 72 74 65 72 20 29 7b 0d 0a 20 20 20 20 69 6e 74  rter ){..    int
35177 20 72 65 67 53 6f 72 74 4f 75 74 20 3d 20 2b 2b   regSortOut = ++
35178 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20  pParse->nMem;.. 
35179 20 20 20 69 6e 74 20 70 74 61 62 32 20 3d 20 70     int ptab2 = p
3517a 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a  Parse->nTab++;..
3517b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3517c 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
3517d 50 73 65 75 64 6f 2c 20 70 74 61 62 32 2c 20 72  Pseudo, ptab2, r
3517e 65 67 53 6f 72 74 4f 75 74 2c 20 70 4f 72 64 65  egSortOut, pOrde
3517f 72 42 79 2d 3e 6e 45 78 70 72 2b 32 29 3b 0d 0a  rBy->nExpr+2);..
35180 20 20 20 20 61 64 64 72 20 3d 20 31 20 2b 20 73      addr = 1 + s
35181 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
35182 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 6f 72  (v, OP_SorterSor
35183 74 2c 20 69 54 61 62 2c 20 61 64 64 72 42 72 65  t, iTab, addrBre
35184 61 6b 29 3b 0d 0a 20 20 20 20 63 6f 64 65 4f 66  ak);..    codeOf
35185 66 73 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43  fset(v, p, addrC
35186 6f 6e 74 69 6e 75 65 29 3b 0d 0a 20 20 20 20 73  ontinue);..    s
35187 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
35188 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 44 61 74  (v, OP_SorterDat
35189 61 2c 20 69 54 61 62 2c 20 72 65 67 53 6f 72 74  a, iTab, regSort
3518a 4f 75 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Out);..    sqlit
3518b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
3518c 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 74 61 62 32  OP_Column, ptab2
3518d 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  , pOrderBy->nExp
3518e 72 2b 31 2c 20 72 65 67 52 6f 77 29 3b 0d 0a 20  r+1, regRow);.. 
3518f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
35190 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
35191 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0d 0a 20  _CLEARCACHE);.. 
35192 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 64 64   }else{..    add
35193 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56  r = 1 + sqlite3V
35194 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
35195 53 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 72  Sort, iTab, addr
35196 42 72 65 61 6b 29 3b 0d 0a 20 20 20 20 63 6f 64  Break);..    cod
35197 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 61 64  eOffset(v, p, ad
35198 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0d 0a 20 20  drContinue);..  
35199 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3519a 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
3519b 2c 20 69 54 61 62 2c 20 70 4f 72 64 65 72 42 79  , iTab, pOrderBy
3519c 2d 3e 6e 45 78 70 72 2b 31 2c 20 72 65 67 52 6f  ->nExpr+1, regRo
3519d 77 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 77 69 74  w);..  }..  swit
3519e 63 68 28 20 65 44 65 73 74 20 29 7b 0d 0a 20 20  ch( eDest ){..  
3519f 20 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65    case SRT_Table
351a0 3a 0d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  :..    case SRT_
351a1 45 70 68 65 6d 54 61 62 3a 20 7b 0d 0a 20 20 20  EphemTab: {..   
351a2 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
351a3 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b  st==SRT_Table );
351a4 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
351a5 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  ( eDest==SRT_Eph
351a6 65 6d 54 61 62 20 29 3b 0d 0a 20 20 20 20 20 20  emTab );..      
351a7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
351a8 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
351a9 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69  , iParm, regRowi
351aa 64 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  d);..      sqlit
351ab 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
351ac 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
351ad 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77  , regRow, regRow
351ae 69 64 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  id);..      sqli
351af 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
351b0 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
351b1 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
351b2 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66  ..    }..#ifndef
351b3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
351b4 51 55 45 52 59 0d 0a 20 20 20 20 63 61 73 65 20  QUERY..    case 
351b5 53 52 54 5f 53 65 74 3a 20 7b 0d 0a 20 20 20 20  SRT_Set: {..    
351b6 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d    assert( nColum
351b7 6e 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 20 20 73  n==1 );..      s
351b8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
351b9 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
351ba 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65  d, regRow, 1, re
351bb 67 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66 69  gRowid, &p->affi
351bc 6e 69 74 79 2c 20 31 29 3b 0d 0a 20 20 20 20 20  nity, 1);..     
351bd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
351be 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
351bf 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20  pParse, regRow, 
351c0 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  1);..      sqlit
351c1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
351c2 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50  OP_IdxInsert, iP
351c3 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0d  arm, regRowid);.
351c4 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
351c5 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53     }..    case S
351c6 52 54 5f 4d 65 6d 3a 20 7b 0d 0a 20 20 20 20 20  RT_Mem: {..     
351c7 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e   assert( nColumn
351c8 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 20 20 73 71  ==1 );..      sq
351c9 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
351ca 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  e(pParse, regRow
351cb 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0d 0a 20 20  , iParm, 1);..  
351cc 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
351cd 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72   clause will ter
351ce 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20  minate the loop 
351cf 66 6f 72 20 75 73 20 2a 2f 0d 0a 20 20 20 20 20  for us */..     
351d0 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
351d1 23 65 6e 64 69 66 0d 0a 20 20 20 20 64 65 66 61  #endif..    defa
351d2 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e  ult: {..      in
351d3 74 20 69 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  t i;..      asse
351d4 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  rt( eDest==SRT_O
351d5 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d  utput || eDest==
351d6 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b  SRT_Coroutine );
351d7 20 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   ..      testcas
351d8 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  e( eDest==SRT_Ou
351d9 74 70 75 74 20 29 3b 0d 0a 20 20 20 20 20 20 74  tput );..      t
351da 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
351db 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b  SRT_Coroutine );
351dc 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ..      for(i=0;
351dd 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29   i<nColumn; i++)
351de 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  {..        asser
351df 74 28 20 72 65 67 52 6f 77 21 3d 70 44 65 73 74  t( regRow!=pDest
351e0 2d 3e 69 4d 65 6d 2b 69 20 29 3b 0d 0a 20 20 20  ->iMem+i );..   
351e1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
351e2 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
351e3 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  umn, pseudoTab, 
351e4 69 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69  i, pDest->iMem+i
351e5 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
351e6 69 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  i==0 ){..       
351e7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
351e8 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
351e9 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0d 0a 20  _CLEARCACHE);.. 
351ea 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
351eb 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 65 44 65  }..      if( eDe
351ec 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29  st==SRT_Output )
351ed 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
351ee 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
351ef 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 70 44  OP_ResultRow, pD
351f0 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75  est->iMem, nColu
351f1 6d 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  mn);..        sq
351f2 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
351f3 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
351f4 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d  rse, pDest->iMem
351f5 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 20  , nColumn);..   
351f6 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
351f7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
351f8 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
351f9 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b  , pDest->iParm);
351fa 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
351fb 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a   break;..    }..
351fc 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65    }..  sqlite3Re
351fd 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
351fe 72 73 65 2c 20 72 65 67 52 6f 77 29 3b 0d 0a 20  rse, regRow);.. 
351ff 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
35200 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
35201 65 67 52 6f 77 69 64 29 3b 0d 0a 0d 0a 20 20 2f  egRowid);....  /
35202 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
35203 74 68 65 20 6c 6f 6f 70 0d 0a 20 20 2a 2f 0d 0a  the loop..  */..
35204 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
35205 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
35206 72 43 6f 6e 74 69 6e 75 65 29 3b 0d 0a 20 20 69  rContinue);..  i
35207 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  f( p->selFlags &
35208 20 53 46 5f 55 73 65 53 6f 72 74 65 72 20 29 7b   SF_UseSorter ){
35209 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
3520a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f  eAddOp2(v, OP_So
3520b 72 74 65 72 4e 65 78 74 2c 20 69 54 61 62 2c 20  rterNext, iTab, 
3520c 61 64 64 72 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b  addr);..  }else{
3520d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
3520e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
3520f 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b  xt, iTab, addr);
35210 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
35211 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
35212 28 76 2c 20 61 64 64 72 42 72 65 61 6b 29 3b 0d  (v, addrBreak);.
35213 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52  .  if( eDest==SR
35214 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73  T_Output || eDes
35215 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
35216 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
35217 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
35218 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54 61  _Close, pseudoTa
35219 62 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  b, 0);..  }..}..
3521a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
3521b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
3521c 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
3521d 20 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f   the 'declaratio
3521e 6e 20 74 79 70 65 27 20 6f 66 20 74 68 65 0d 0a  n type' of the..
3521f 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  ** expression pE
35220 78 70 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20  xpr. The string 
35221 6d 61 79 20 62 65 20 74 72 65 61 74 65 64 20 61  may be treated a
35222 73 20 73 74 61 74 69 63 20 62 79 20 74 68 65 20  s static by the 
35223 63 61 6c 6c 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  caller...**..** 
35224 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
35225 74 79 70 65 20 69 73 20 74 68 65 20 65 78 61 63  type is the exac
35226 74 20 64 61 74 61 74 79 70 65 20 64 65 66 69 6e  t datatype defin
35227 69 74 69 6f 6e 20 65 78 74 72 61 63 74 65 64 20  ition extracted 
35228 66 72 6f 6d 20 74 68 65 0d 0a 2a 2a 20 6f 72 69  from the..** ori
35229 67 69 6e 61 6c 20 43 52 45 41 54 45 20 54 41 42  ginal CREATE TAB
3522a 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  LE statement if 
3522b 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
3522c 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 54 68 65 0d  s a column. The.
3522d 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  .** declaration 
3522e 74 79 70 65 20 66 6f 72 20 61 20 52 4f 57 49 44  type for a ROWID
3522f 20 66 69 65 6c 64 20 69 73 20 49 4e 54 45 47 45   field is INTEGE
35230 52 2e 20 45 78 61 63 74 6c 79 20 77 68 65 6e 20  R. Exactly when 
35231 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0d 0a 2a  an expression..*
35232 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
35233 61 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20  a column can be 
35234 63 6f 6d 70 6c 65 78 20 69 6e 20 74 68 65 20 70  complex in the p
35235 72 65 73 65 6e 63 65 20 6f 66 20 73 75 62 71 75  resence of subqu
35236 65 72 69 65 73 2e 20 54 68 65 0d 0a 2a 2a 20 72  eries. The..** r
35237 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
35238 73 69 6f 6e 20 69 6e 20 61 6c 6c 20 6f 66 20 74  sion in all of t
35239 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 45 4c  he following SEL
3523a 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
3523b 73 20 0d 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65  s ..** considere
3523c 64 20 61 20 63 6f 6c 75 6d 6e 20 62 79 20 74 68  d a column by th
3523d 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a  is function...**
3523e 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63 6f  ..**   SELECT co
3523f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0d 0a 2a 2a 20  l FROM tbl;..** 
35240 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43 54    SELECT (SELECT
35241 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0d 0a   col FROM tbl;..
35242 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c  **   SELECT (SEL
35243 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c  ECT col FROM tbl
35244 29 3b 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  );..**   SELECT 
35245 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  abc FROM (SELECT
35246 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52 4f 4d   col AS abc FROM
35247 20 74 62 6c 29 3b 0d 0a 2a 2a 20 0d 0a 2a 2a 20   tbl);..** ..** 
35248 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
35249 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
3524a 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
3524b 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
3524c 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ULL...*/..static
3524d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c   const char *col
3524e 75 6d 6e 54 79 70 65 28 0d 0a 20 20 4e 61 6d 65  umnType(..  Name
3524f 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 0d 0a  Context *pNC, ..
35250 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 0d 0a    Expr *pExpr,..
35251 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
35252 7a 4f 72 69 67 69 6e 44 62 2c 0d 0a 20 20 63 6f  zOriginDb,..  co
35253 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69  nst char **pzOri
35254 67 69 6e 54 61 62 2c 0d 0a 20 20 63 6f 6e 73 74  ginTab,..  const
35255 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e   char **pzOrigin
35256 43 6f 6c 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20  Col..){..  char 
35257 63 6f 6e 73 74 20 2a 7a 54 79 70 65 20 3d 20 30  const *zType = 0
35258 3b 0d 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  ;..  char const 
35259 2a 7a 4f 72 69 67 69 6e 44 62 20 3d 20 30 3b 0d  *zOriginDb = 0;.
3525a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
3525b 4f 72 69 67 69 6e 54 61 62 20 3d 20 30 3b 0d 0a  OriginTab = 0;..
3525c 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f    char const *zO
3525d 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0d 0a 20  riginCol = 0;.. 
3525e 20 69 6e 74 20 6a 3b 0d 0a 20 20 69 66 28 20 4e   int j;..  if( N
3525f 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29 20 7c  EVER(pExpr==0) |
35260 7c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3d  | pNC->pSrcList=
35261 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a  =0 ) return 0;..
35262 0d 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ..  switch( pExp
35263 72 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 63 61  r->op ){..    ca
35264 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
35265 3a 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  :..    case TK_C
35266 4f 4c 55 4d 4e 3a 20 7b 0d 0a 20 20 20 20 20 20  OLUMN: {..      
35267 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
35268 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 4c  n is a column. L
35269 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
3526a 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 62 65  the column is be
3526b 69 6e 67 0d 0a 20 20 20 20 20 20 2a 2a 20 65 78  ing..      ** ex
3526c 74 72 61 63 74 65 64 20 66 72 6f 6d 20 69 6e 20  tracted from in 
3526d 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63  NameContext.pSrc
3526e 4c 69 73 74 2e 20 54 68 69 73 20 74 61 62 6c 65  List. This table
3526f 20 6d 61 79 20 62 65 20 72 65 61 6c 0d 0a 20 20   may be real..  
35270 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
35271 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62 71 75  table or a subqu
35272 65 72 79 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ery...      */..
35273 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
35274 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
35275 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63    /* Table struc
35276 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65  ture column is e
35277 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f  xtracted from */
35278 0d 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  ..      Select *
35279 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  pS = 0;         
3527a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
3527b 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72  e column is extr
3527c 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20  acted from */.. 
3527d 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
3527e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20  pExpr->iColumn; 
3527f 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
35280 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0d 0a  umn in pTab */..
35281 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
35282 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47  pExpr->op==TK_AG
35283 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0d 0a 20 20 20  G_COLUMN );..   
35284 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
35285 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
35286 4e 20 29 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c  N );..      whil
35287 65 28 20 70 4e 43 20 26 26 20 21 70 54 61 62 20  e( pNC && !pTab 
35288 29 7b 0d 0a 20 20 20 20 20 20 20 20 53 72 63 4c  ){..        SrcL
35289 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20  ist *pTabList = 
3528a 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0d 0a  pNC->pSrcList;..
3528b 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
3528c 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
3528d 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b   && pTabList->a[
3528e 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45 78 70  j].iCursor!=pExp
3528f 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29 3b 0d  r->iTable;j++);.
35290 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c 70  .        if( j<p
35291 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b  TabList->nSrc ){
35292 0d 0a 20 20 20 20 20 20 20 20 20 20 70 54 61 62  ..          pTab
35293 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
35294 5d 2e 70 54 61 62 3b 0d 0a 20 20 20 20 20 20 20  ].pTab;..       
35295 20 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74     pS = pTabList
35296 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0d  ->a[j].pSelect;.
35297 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
35298 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43 20 3d  .          pNC =
35299 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20   pNC->pNext;..  
3529a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
3529b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54  ....      if( pT
3529c 61 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ab==0 ){..      
3529d 20 20 2f 2a 20 41 74 20 6f 6e 65 20 74 69 6d 65    /* At one time
3529e 2c 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 22  , code such as "
3529f 53 45 4c 45 43 54 20 6e 65 77 2e 78 22 20 77 69  SELECT new.x" wi
352a0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 77  thin a trigger w
352a1 6f 75 6c 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a  ould..        **
352a2 20 63 61 75 73 65 20 74 68 69 73 20 63 6f 6e 64   cause this cond
352a3 69 74 69 6f 6e 20 74 6f 20 72 75 6e 2e 20 20 53  ition to run.  S
352a4 69 6e 63 65 20 74 68 65 6e 2c 20 77 65 20 68 61  ince then, we ha
352a5 76 65 20 72 65 73 74 72 75 63 74 75 72 65 64 20  ve restructured 
352a6 68 6f 77 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  how..        ** 
352a7 74 72 69 67 67 65 72 20 63 6f 64 65 20 69 73 20  trigger code is 
352a8 67 65 6e 65 72 61 74 65 64 20 61 6e 64 20 73 6f  generated and so
352a9 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20   this condition 
352aa 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 0d 0a 20  is no longer .. 
352ab 20 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62         ** possib
352ac 6c 65 2e 20 48 6f 77 65 76 65 72 2c 20 69 74 20  le. However, it 
352ad 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 74 72 75  can still be tru
352ae 65 20 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73  e for statements
352af 20 6c 69 6b 65 0d 0a 20 20 20 20 20 20 20 20 2a   like..        *
352b0 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  * the following:
352b1 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ..        **..  
352b2 20 20 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54        **   CREAT
352b3 45 20 54 41 42 4c 45 20 74 31 28 63 6f 6c 20 49  E TABLE t1(col I
352b4 4e 54 45 47 45 52 29 3b 0d 0a 20 20 20 20 20 20  NTEGER);..      
352b5 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53    **   SELECT (S
352b6 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29 20 46 52  ELECT t1.col) FR
352b7 4f 4d 20 46 52 4f 4d 20 74 31 3b 0d 0a 20 20 20  OM FROM t1;..   
352b8 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20       **..       
352b9 20 2a 2a 20 77 68 65 6e 20 63 6f 6c 75 6d 6e 54   ** when columnT
352ba 79 70 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ype() is called 
352bb 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
352bc 6e 20 22 74 31 2e 63 6f 6c 22 20 69 6e 20 74 68  n "t1.col" in th
352bd 65 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  e ..        ** s
352be 75 62 2d 73 65 6c 65 63 74 2e 20 49 6e 20 74 68  ub-select. In th
352bf 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 65  is case, set the
352c0 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 74 6f 20   column type to 
352c1 4e 55 4c 4c 2c 20 65 76 65 6e 0d 0a 20 20 20 20  NULL, even..    
352c2 20 20 20 20 2a 2a 20 74 68 6f 75 67 68 20 69 74      ** though it
352c3 20 73 68 6f 75 6c 64 20 72 65 61 6c 6c 79 20 62   should really b
352c4 65 20 22 49 4e 54 45 47 45 52 22 2e 0d 0a 20 20  e "INTEGER"...  
352c5 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
352c6 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74    ** This is not
352c7 20 61 20 70 72 6f 62 6c 65 6d 2c 20 61 73 20 74   a problem, as t
352c8 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6f  he column type o
352c9 66 20 22 74 31 2e 63 6f 6c 22 20 69 73 20 6e 65  f "t1.col" is ne
352ca 76 65 72 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ver..        ** 
352cb 75 73 65 64 2e 20 57 68 65 6e 20 63 6f 6c 75 6d  used. When colum
352cc 6e 54 79 70 65 28 29 20 69 73 20 63 61 6c 6c 65  nType() is calle
352cd 64 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73  d on the express
352ce 69 6f 6e 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a  ion ..        **
352cf 20 22 28 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c   "(SELECT t1.col
352d0 29 22 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20  )", the correct 
352d1 74 79 70 65 20 69 73 20 72 65 74 75 72 6e 65 64  type is returned
352d2 20 28 73 65 65 20 74 68 65 20 54 4b 5f 53 45 4c   (see the TK_SEL
352d3 45 43 54 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ECT..        ** 
352d4 62 72 61 6e 63 68 20 62 65 6c 6f 77 2e 20 20 2a  branch below.  *
352d5 2f 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  /..        break
352d6 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  ;..      }....  
352d7 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
352d8 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 3d   && pExpr->pTab=
352d9 3d 70 54 61 62 20 29 3b 0d 0a 20 20 20 20 20 20  =pTab );..      
352da 69 66 28 20 70 53 20 29 7b 0d 0a 20 20 20 20 20  if( pS ){..     
352db 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65     /* The "table
352dc 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  " is actually a 
352dd 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20  sub-select or a 
352de 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d  view in the FROM
352df 20 63 6c 61 75 73 65 0d 0a 20 20 20 20 20 20 20   clause..       
352e0 20 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43   ** of the SELEC
352e1 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74  T statement. Ret
352e2 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74  urn the declarat
352e3 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69  ion type and ori
352e4 67 69 6e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  gin..        ** 
352e5 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 73  data for the res
352e6 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f  ult-set column o
352e7 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74  f the sub-select
352e8 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
352e9 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e         if( iCol>
352ea 3d 30 20 26 26 20 41 4c 57 41 59 53 28 69 43 6f  =0 && ALWAYS(iCo
352eb 6c 3c 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  l<pS->pEList->nE
352ec 78 70 72 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  xpr) ){..       
352ed 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c 20 69 73     /* If iCol is
352ee 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
352ef 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   then the expres
352f0 73 69 6f 6e 20 72 65 71 75 65 73 74 73 20 74 68  sion requests th
352f1 65 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e..          ** 
352f2 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73 75 62  rowid of the sub
352f3 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e  -select or view.
352f4 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   This expression
352f5 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65 20 0d   is legal (see .
352f6 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 65  .          ** te
352f7 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32 2e  st case misc2.2.
352f8 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20 65  2) - it always e
352f9 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
352fa 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d  ...          */.
352fb 0a 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43  .          NameC
352fc 6f 6e 74 65 78 74 20 73 4e 43 3b 0d 0a 20 20 20  ontext sNC;..   
352fd 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d         Expr *p =
352fe 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69   pS->pEList->a[i
352ff 43 6f 6c 5d 2e 70 45 78 70 72 3b 0d 0a 20 20 20  Col].pExpr;..   
35300 20 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c         sNC.pSrcL
35301 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0d  ist = pS->pSrc;.
35302 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70  .          sNC.p
35303 4e 65 78 74 20 3d 20 70 4e 43 3b 0d 0a 20 20 20  Next = pNC;..   
35304 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73         sNC.pPars
35305 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
35306 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  ..          zTyp
35307 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
35308 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
35309 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
3530a 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0d   &zOriginCol); .
3530b 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
3530c 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41    }else if( ALWA
3530d 59 53 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  YS(pTab->pSchema
3530e 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  ) ){..        /*
3530f 20 41 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f   A real table */
35310 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
35311 28 20 21 70 53 20 29 3b 0d 0a 20 20 20 20 20 20  ( !pS );..      
35312 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69    if( iCol<0 ) i
35313 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  Col = pTab->iPKe
35314 79 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  y;..        asse
35315 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20  rt( iCol==-1 || 
35316 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c  (iCol>=0 && iCol
35317 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0d  <pTab->nCol) );.
35318 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
35319 6c 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  l<0 ){..        
3531a 20 20 7a 54 79 70 65 20 3d 20 22 49 4e 54 45 47    zType = "INTEG
3531b 45 52 22 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ER";..          
3531c 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 22 72 6f  zOriginCol = "ro
3531d 77 69 64 22 3b 0d 0a 20 20 20 20 20 20 20 20 7d  wid";..        }
3531e 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
3531f 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61   zType = pTab->a
35320 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b  Col[iCol].zType;
35321 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69  ..          zOri
35322 67 69 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61  ginCol = pTab->a
35323 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
35324 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
35325 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61 62 20       zOriginTab 
35326 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0d 0a  = pTab->zName;..
35327 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d          if( pNC-
35328 3e 70 50 61 72 73 65 20 29 7b 0d 0a 20 20 20 20  >pParse ){..    
35329 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
3532a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
3532b 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
3532c 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
3532d 65 6d 61 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  ema);..         
3532e 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43   zOriginDb = pNC
3532f 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  ->pParse->db->aD
35330 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  b[iDb].zName;.. 
35331 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
35332 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  }..      break;.
35333 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
35334 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
35335 55 45 52 59 0d 0a 20 20 20 20 63 61 73 65 20 54  UERY..    case T
35336 4b 5f 53 45 4c 45 43 54 3a 20 7b 0d 0a 20 20 20  K_SELECT: {..   
35337 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
35338 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65  sion is a sub-se
35339 6c 65 63 74 2e 20 52 65 74 75 72 6e 20 74 68 65  lect. Return the
3533a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
3533b 65 20 61 6e 64 0d 0a 20 20 20 20 20 20 2a 2a 20  e and..      ** 
3533c 6f 72 69 67 69 6e 20 69 6e 66 6f 20 66 6f 72 20  origin info for 
3533d 74 68 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  the single colum
3533e 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  n in the result 
3533f 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
35340 54 0d 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  T..      ** stat
35341 65 6d 65 6e 74 2e 0d 0a 20 20 20 20 20 20 2a 2f  ement...      */
35342 0d 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74  ..      NameCont
35343 65 78 74 20 73 4e 43 3b 0d 0a 20 20 20 20 20 20  ext sNC;..      
35344 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 70 45 78  Select *pS = pEx
35345 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0d 0a  pr->x.pSelect;..
35346 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20        Expr *p = 
35347 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  pS->pEList->a[0]
35348 2e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 61  .pExpr;..      a
35349 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72  ssert( ExprHasPr
3534a 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
3534b 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0d 0a  _xIsSelect) );..
3534c 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69        sNC.pSrcLi
3534d 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0d 0a  st = pS->pSrc;..
3534e 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20        sNC.pNext 
3534f 3d 20 70 4e 43 3b 0d 0a 20 20 20 20 20 20 73 4e  = pNC;..      sN
35350 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e  C.pParse = pNC->
35351 70 50 61 72 73 65 3b 0d 0a 20 20 20 20 20 20 7a  pParse;..      z
35352 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70  Type = columnTyp
35353 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69  e(&sNC, p, &zOri
35354 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54  ginDb, &zOriginT
35355 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29  ab, &zOriginCol)
35356 3b 20 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ; ..      break;
35357 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
35358 0a 20 20 7d 0d 0a 20 20 0d 0a 20 20 69 66 28 20  .  }..  ..  if( 
35359 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0d 0a 20  pzOriginDb ){.. 
3535a 20 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69     assert( pzOri
3535b 67 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67  ginTab && pzOrig
3535c 69 6e 43 6f 6c 20 29 3b 0d 0a 20 20 20 20 2a 70  inCol );..    *p
3535d 7a 4f 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69  zOriginDb = zOri
3535e 67 69 6e 44 62 3b 0d 0a 20 20 20 20 2a 70 7a 4f  ginDb;..    *pzO
3535f 72 69 67 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67  riginTab = zOrig
35360 69 6e 54 61 62 3b 0d 0a 20 20 20 20 2a 70 7a 4f  inTab;..    *pzO
35361 72 69 67 69 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67  riginCol = zOrig
35362 69 6e 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 72  inCol;..  }..  r
35363 65 74 75 72 6e 20 7a 54 79 70 65 3b 0d 0a 7d 0d  eturn zType;..}.
35364 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
35365 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
35366 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20  l tell the VDBE 
35367 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  the declaration 
35368 74 79 70 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  types of columns
35369 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75  ..** in the resu
3536a 6c 74 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  lt set...*/..sta
3536b 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
3536c 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0d 0a 20  eColumnTypes(.. 
3536d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
3536e 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
3536f 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 53 72 63  ontext */..  Src
35370 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
35371 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
35372 65 73 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73  es */..  ExprLis
35373 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20  t *pEList    /* 
35374 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69  Expressions defi
35375 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
35376 73 65 74 20 2a 2f 0d 0a 29 7b 0d 0a 23 69 66 6e  set */..){..#ifn
35377 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
35378 44 45 43 4c 54 59 50 45 0d 0a 20 20 56 64 62 65  DECLTYPE..  Vdbe
35379 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
3537a 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  dbe;..  int i;..
3537b 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
3537c 43 3b 0d 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69  C;..  sNC.pSrcLi
3537d 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0d 0a  st = pTabList;..
3537e 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
3537f 50 61 72 73 65 3b 0d 0a 20 20 66 6f 72 28 69 3d  Parse;..  for(i=
35380 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
35381 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 45  pr; i++){..    E
35382 78 70 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d  xpr *p = pEList-
35383 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0d 0a 20 20  >a[i].pExpr;..  
35384 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
35385 79 70 65 3b 0d 0a 23 69 66 64 65 66 20 53 51 4c  ype;..#ifdef SQL
35386 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
35387 4e 5f 4d 45 54 41 44 41 54 41 0d 0a 20 20 20 20  N_METADATA..    
35388 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69  const char *zOri
35389 67 44 62 20 3d 20 30 3b 0d 0a 20 20 20 20 63 6f  gDb = 0;..    co
3538a 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 54  nst char *zOrigT
3538b 61 62 20 3d 20 30 3b 0d 0a 20 20 20 20 63 6f 6e  ab = 0;..    con
3538c 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f  st char *zOrigCo
3538d 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 7a 54 79 70  l = 0;..    zTyp
3538e 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
3538f 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44 62  sNC, p, &zOrigDb
35390 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a 4f  , &zOrigTab, &zO
35391 72 69 67 43 6f 6c 29 3b 0d 0a 0d 0a 20 20 20 20  rigCol);....    
35392 2f 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73 74  /* The vdbe must
35393 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f   make its own co
35394 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  py of the column
35395 2d 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20  -type and other 
35396 0d 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  ..    ** column 
35397 73 70 65 63 69 66 69 63 20 73 74 72 69 6e 67 73  specific strings
35398 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 63  , in case the sc
35399 68 65 6d 61 20 69 73 20 72 65 73 65 74 20 62 65  hema is reset be
3539a 66 6f 72 65 20 74 68 69 73 0d 0a 20 20 20 20 2a  fore this..    *
3539b 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * virtual machin
3539c 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0d 0a 20  e is deleted... 
3539d 20 20 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74     */..    sqlit
3539e 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
3539f 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44  (v, i, COLNAME_D
353a0 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67 44 62  ATABASE, zOrigDb
353a1 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
353a2 4e 54 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  NT);..    sqlite
353a3 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
353a4 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41  v, i, COLNAME_TA
353a5 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20 53  BLE, zOrigTab, S
353a6 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
353a7 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
353a8 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
353a9 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  i, COLNAME_COLUM
353aa 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c  N, zOrigCol, SQL
353ab 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d  ITE_TRANSIENT);.
353ac 0a 23 65 6c 73 65 0d 0a 20 20 20 20 7a 54 79 70  .#else..    zTyp
353ad 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
353ae 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29  sNC, p, 0, 0, 0)
353af 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 73  ;..#endif..    s
353b0 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
353b1 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
353b2 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20 7a 54 79  ME_DECLTYPE, zTy
353b3 70 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  pe, SQLITE_TRANS
353b4 49 45 4e 54 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  IENT);..  }..#en
353b5 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
353b6 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0d 0a  IT_DECLTYPE */..
353b7 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65  }..../*..** Gene
353b8 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
353b9 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42  ill tell the VDB
353ba 45 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63  E the names of c
353bb 6f 6c 75 6d 6e 73 0d 0a 2a 2a 20 69 6e 20 74 68  olumns..** in th
353bc 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54  e result set.  T
353bd 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
353be 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69  is used to provi
353bf 64 65 20 74 68 65 0d 0a 2a 2a 20 61 7a 43 6f 6c  de the..** azCol
353c0 5b 5d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65  [] values in the
353c1 20 63 61 6c 6c 62 61 63 6b 2e 0d 0a 2a 2f 0d 0a   callback...*/..
353c2 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
353c3 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  rateColumnNames(
353c4 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
353c5 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65  e,      /* Parse
353c6 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  r context */..  
353c7 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
353c8 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
353c9 61 62 6c 65 73 20 2a 2f 0d 0a 20 20 45 78 70 72  ables */..  Expr
353ca 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20  List *pEList    
353cb 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64  /* Expressions d
353cc 65 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75  efining the resu
353cd 6c 74 20 73 65 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  lt set */..){.. 
353ce 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
353cf 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74  e->pVdbe;..  int
353d0 20 69 2c 20 6a 3b 0d 0a 20 20 73 71 6c 69 74 65   i, j;..  sqlite
353d1 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
353d2 64 62 3b 0d 0a 20 20 69 6e 74 20 66 75 6c 6c 4e  db;..  int fullN
353d3 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73  ames, shortNames
353d4 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;....#ifndef SQL
353d5 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
353d6 0d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
353d7 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b  s an EXPLAIN, sk
353d8 69 70 20 74 68 69 73 20 73 74 65 70 20 2a 2f 0d  ip this step */.
353d9 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65  .  if( pParse->e
353da 78 70 6c 61 69 6e 20 29 7b 0d 0a 20 20 20 20 72  xplain ){..    r
353db 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  eturn;..  }..#en
353dc 64 69 66 0d 0a 0d 0a 20 20 69 66 28 20 70 50 61  dif....  if( pPa
353dd 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74  rse->colNamesSet
353de 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d 30 29 20   || NEVER(v==0) 
353df 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
353e0 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  led ) return;.. 
353e1 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
353e2 73 53 65 74 20 3d 20 31 3b 0d 0a 20 20 66 75 6c  sSet = 1;..  ful
353e3 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c  lNames = (db->fl
353e4 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c  ags & SQLITE_Ful
353e5 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0d 0a  lColNames)!=0;..
353e6 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28    shortNames = (
353e7 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
353e8 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
353e9 29 21 3d 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33  )!=0;..  sqlite3
353ea 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
353eb 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
353ec 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
353ed 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
353ee 2b 2b 29 7b 0d 0a 20 20 20 20 45 78 70 72 20 2a  ++){..    Expr *
353ef 70 3b 0d 0a 20 20 20 20 70 20 3d 20 70 45 4c 69  p;..    p = pELi
353f0 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0d  st->a[i].pExpr;.
353f1 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70  .    if( NEVER(p
353f2 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ==0) ) continue;
353f3 0d 0a 20 20 20 20 69 66 28 20 70 45 4c 69 73 74  ..    if( pEList
353f4 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0d  ->a[i].zName ){.
353f5 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  .      char *zNa
353f6 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  me = pEList->a[i
353f7 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20  ].zName;..      
353f8 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
353f9 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
353fa 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c  AME_NAME, zName,
353fb 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
353fc 54 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69  T);..    }else i
353fd 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( (p->op==TK_CO
353fe 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54  LUMN || p->op==T
353ff 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26  K_AGG_COLUMN) &&
35400 20 70 54 61 62 4c 69 73 74 20 29 7b 0d 0a 20 20   pTabList ){..  
35401 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
35402 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43  ..      char *zC
35403 6f 6c 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69  ol;..      int i
35404 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e  Col = p->iColumn
35405 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  ;..      for(j=0
35406 3b 20 41 4c 57 41 59 53 28 6a 3c 70 54 61 62 4c  ; ALWAYS(j<pTabL
35407 69 73 74 2d 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29  ist->nSrc); j++)
35408 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  {..        if( p
35409 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43  TabList->a[j].iC
3540a 75 72 73 6f 72 3d 3d 70 2d 3e 69 54 61 62 6c 65  ursor==p->iTable
3540b 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
3540c 20 7d 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74   }..      assert
3540d 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ( j<pTabList->nS
3540e 72 63 20 29 3b 0d 0a 20 20 20 20 20 20 70 54 61  rc );..      pTa
3540f 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
35410 6a 5d 2e 70 54 61 62 3b 0d 0a 20 20 20 20 20 20  j].pTab;..      
35411 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f  if( iCol<0 ) iCo
35412 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b  l = pTab->iPKey;
35413 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
35414 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f  iCol==-1 || (iCo
35415 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61  l>=0 && iCol<pTa
35416 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0d 0a 20 20 20  b->nCol) );..   
35417 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
35418 0d 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d  ..        zCol =
35419 20 22 72 6f 77 69 64 22 3b 0d 0a 20 20 20 20 20   "rowid";..     
3541a 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
3541b 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43   zCol = pTab->aC
3541c 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0d  ol[iCol].zName;.
3541d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3541e 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20  if( !shortNames 
3541f 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  && !fullNames ){
35420 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
35421 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
35422 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
35423 4d 45 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ME, ..          
35424 20 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75    sqlite3DbStrDu
35425 70 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b  p(db, pEList->a[
35426 69 5d 2e 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54  i].zSpan), SQLIT
35427 45 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 20  E_DYNAMIC);..   
35428 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 75 6c     }else if( ful
35429 6c 4e 61 6d 65 73 20 29 7b 0d 0a 20 20 20 20 20  lNames ){..     
3542a 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d     char *zName =
3542b 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7a 4e 61   0;..        zNa
3542c 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
3542d 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c  ntf(db, "%s.%s",
3542e 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
3542f 6f 6c 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  ol);..        sq
35430 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
35431 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
35432 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53  E_NAME, zName, S
35433 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0d  QLITE_DYNAMIC);.
35434 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
35435 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
35436 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
35437 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  i, COLNAME_NAME,
35438 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52   zCol, SQLITE_TR
35439 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20 20 20 20  ANSIENT);..     
3543a 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a   }..    }else{..
3543b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3543c 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
3543d 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
3543e 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
3543f 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
35440 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70  pEList->a[i].zSp
35441 61 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41  an), SQLITE_DYNA
35442 4d 49 43 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  MIC);..    }..  
35443 7d 0d 0a 20 20 67 65 6e 65 72 61 74 65 43 6f 6c  }..  generateCol
35444 75 6d 6e 54 79 70 65 73 28 70 50 61 72 73 65 2c  umnTypes(pParse,
35445 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73   pTabList, pELis
35446 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  t);..}..../*..**
35447 20 47 69 76 65 6e 20 61 20 61 6e 20 65 78 70 72   Given a an expr
35448 65 73 73 69 6f 6e 20 6c 69 73 74 20 28 77 68 69  ession list (whi
35449 63 68 20 69 73 20 72 65 61 6c 6c 79 20 74 68 65  ch is really the
3544a 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
3544b 69 6f 6e 73 0d 0a 2a 2a 20 74 68 61 74 20 66 6f  ions..** that fo
3544c 72 6d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  rm the result se
3544d 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
3544e 61 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75 74 65  atement) compute
3544f 20 61 70 70 72 6f 70 72 69 61 74 65 0d 0a 2a 2a   appropriate..**
35450 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   column names fo
35451 72 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 77  r a table that w
35452 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 78  ould hold the ex
35453 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0d 0a  pression list...
35454 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d  **..** All colum
35455 6e 20 6e 61 6d 65 73 20 77 69 6c 6c 20 62 65 20  n names will be 
35456 75 6e 69 71 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  unique...**..** 
35457 4f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  Only the column 
35458 6e 61 6d 65 73 20 61 72 65 20 63 6f 6d 70 75 74  names are comput
35459 65 64 2e 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70  ed.  Column.zTyp
3545a 65 2c 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c  e, Column.zColl,
3545b 0d 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 66  ..** and other f
3545c 69 65 6c 64 73 20 6f 66 20 43 6f 6c 75 6d 6e 20  ields of Column 
3545d 61 72 65 20 7a 65 72 6f 65 64 2e 0d 0a 2a 2a 0d  are zeroed...**.
3545e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
3545f 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
35460 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c    If a memory al
35461 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
35462 63 63 75 72 73 2c 0d 0a 2a 2a 20 73 74 6f 72 65  ccurs,..** store
35463 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43 6f 6c 20   NULL in *paCol 
35464 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43 6f 6c 20  and 0 in *pnCol 
35465 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
35466 45 5f 4e 4f 4d 45 4d 2e 0d 0a 2a 2f 0d 0a 73 74  E_NOMEM...*/..st
35467 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 43  atic int selectC
35468 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
35469 73 74 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  st(..  Parse *pP
3546a 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
3546b 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
3546c 74 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74  t */..  ExprList
3546d 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20   *pEList,       
3546e 2f 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f  /* Expr list fro
3546f 6d 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76  m which to deriv
35470 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  e column names *
35471 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c  /..  int *pnCol,
35472 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35473 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  Write the number
35474 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65   of columns here
35475 20 2a 2f 0d 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a   */..  Column **
35476 70 61 43 6f 6c 20 20 20 20 20 20 20 20 20 20 2f  paCol          /
35477 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
35478 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 65 72 65  column list here
35479 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74   */..){..  sqlit
3547a 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
3547b 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61  >db;   /* Databa
3547c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
3547d 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  ..  int i, j;   
3547e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3547f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
35480 20 2a 2f 0d 0a 20 20 69 6e 74 20 63 6e 74 3b 20   */..  int cnt; 
35481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35482 20 20 20 2f 2a 20 49 6e 64 65 78 20 61 64 64 65     /* Index adde
35483 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e 61  d to make the na
35484 6d 65 20 75 6e 69 71 75 65 20 2a 2f 0d 0a 20 20  me unique */..  
35485 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70  Column *aCol, *p
35486 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  Col;        /* F
35487 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
35488 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a  result columns *
35489 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  /..  int nCol;  
3548a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3548b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
3548c 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73  lumns in the res
3548d 75 6c 74 20 73 65 74 20 2a 2f 0d 0a 20 20 45 78  ult set */..  Ex
3548e 70 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  pr *p;          
3548f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
35490 72 65 73 73 69 6f 6e 20 66 6f 72 20 61 20 73 69  ression for a si
35491 6e 67 6c 65 20 72 65 73 75 6c 74 20 63 6f 6c 75  ngle result colu
35492 6d 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  mn */..  char *z
35493 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
35494 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
35495 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e  ame */..  int nN
35496 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
35497 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
35498 20 6e 61 6d 65 20 69 6e 20 7a 4e 61 6d 65 5b 5d   name in zName[]
35499 20 2a 2f 0d 0a 0d 0a 20 20 2a 70 6e 43 6f 6c 20   */....  *pnCol 
3549a 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d  = nCol = pEList-
3549b 3e 6e 45 78 70 72 3b 0d 0a 20 20 61 43 6f 6c 20  >nExpr;..  aCol 
3549c 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c 69 74  = *paCol = sqlit
3549d 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
3549e 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c 5b 30  b, sizeof(aCol[0
3549f 5d 29 2a 6e 43 6f 6c 29 3b 0d 0a 20 20 69 66 28  ])*nCol);..  if(
354a0 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72   aCol==0 ) retur
354a1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
354a2 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c  .  for(i=0, pCol
354a3 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69  =aCol; i<nCol; i
354a4 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0d 0a 20 20  ++, pCol++){..  
354a5 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72    /* Get an appr
354a6 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72  opriate name for
354a7 20 74 68 65 20 63 6f 6c 75 6d 6e 0d 0a 20 20 20   the column..   
354a8 20 2a 2f 0d 0a 20 20 20 20 70 20 3d 20 70 45 4c   */..    p = pEL
354a9 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
354aa 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
354ab 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 45 78  >pRight==0 || Ex
354ac 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d  prHasProperty(p-
354ad 3e 70 52 69 67 68 74 2c 20 45 50 5f 49 6e 74 56  >pRight, EP_IntV
354ae 61 6c 75 65 29 0d 0a 20 20 20 20 20 20 20 20 20  alue)..         
354af 20 20 20 20 20 20 7c 7c 20 70 2d 3e 70 52 69 67        || p->pRig
354b0 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20  ht->u.zToken==0 
354b1 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e  || p->pRight->u.
354b2 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0d  zToken[0]!=0 );.
354b3 0a 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20  .    if( (zName 
354b4 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
354b5 4e 61 6d 65 29 21 3d 30 20 29 7b 0d 0a 20 20 20  Name)!=0 ){..   
354b6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
354b7 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  umn contains an 
354b8 22 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61  "AS <name>" phra
354b9 73 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61  se, use <name> a
354ba 73 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20  s the name */.. 
354bb 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c       zName = sql
354bc 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
354bd 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 65   zName);..    }e
354be 6c 73 65 7b 0d 0a 20 20 20 20 20 20 45 78 70 72  lse{..      Expr
354bf 20 2a 70 43 6f 6c 45 78 70 72 20 3d 20 70 3b 20   *pColExpr = p; 
354c0 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
354c1 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 72  on that is the r
354c2 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d  esult column nam
354c3 65 20 2a 2f 0d 0a 20 20 20 20 20 20 54 61 62 6c  e */..      Tabl
354c4 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
354c5 20 2f 2a 20 54 61 62 6c 65 20 61 73 73 6f 63 69   /* Table associ
354c6 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
354c7 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20  xpression */..  
354c8 20 20 20 20 77 68 69 6c 65 28 20 70 43 6f 6c 45      while( pColE
354c9 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20  xpr->op==TK_DOT 
354ca 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f 6c  ){..        pCol
354cb 45 78 70 72 20 3d 20 70 43 6f 6c 45 78 70 72 2d  Expr = pColExpr-
354cc 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20  >pRight;..      
354cd 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 45 78    assert( pColEx
354ce 70 72 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  pr!=0 );..      
354cf 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  }..      if( pCo
354d0 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  lExpr->op==TK_CO
354d1 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 28 70  LUMN && ALWAYS(p
354d2 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 3d 30  ColExpr->pTab!=0
354d3 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a  ) ){..        /*
354d4 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65   For columns use
354d5 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
354d6 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 20 20   name */..      
354d7 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f    int iCol = pCo
354d8 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d  lExpr->iColumn;.
354d9 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
354da 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0d  pColExpr->pTab;.
354db 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
354dc 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61  l<0 ) iCol = pTa
354dd 62 2d 3e 69 50 4b 65 79 3b 0d 0a 20 20 20 20 20  b->iPKey;..     
354de 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
354df 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
354e0 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  s",..           
354e1 20 20 20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20        iCol>=0 ? 
354e2 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
354e3 2e 7a 4e 61 6d 65 20 3a 20 22 72 6f 77 69 64 22  .zName : "rowid"
354e4 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  );..      }else 
354e5 69 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70  if( pColExpr->op
354e6 3d 3d 54 4b 5f 49 44 20 29 7b 0d 0a 20 20 20 20  ==TK_ID ){..    
354e7 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
354e8 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 43 6f  rHasProperty(pCo
354e9 6c 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  lExpr, EP_IntVal
354ea 75 65 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ue) );..        
354eb 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
354ec 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
354ed 20 70 43 6f 6c 45 78 70 72 2d 3e 75 2e 7a 54 6f   pColExpr->u.zTo
354ee 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ken);..      }el
354ef 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20  se{..        /* 
354f0 55 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  Use the original
354f1 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6c   text of the col
354f2 75 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61  umn expression a
354f3 73 20 69 74 73 20 6e 61 6d 65 20 2a 2f 0d 0a 20  s its name */.. 
354f4 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73         zName = s
354f5 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
354f6 2c 20 22 25 73 22 2c 20 70 45 4c 69 73 74 2d 3e  , "%s", pEList->
354f7 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b 0d 0a 20 20  a[i].zSpan);..  
354f8 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
354f9 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
354fa 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 20  Failed ){..     
354fb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
354fc 62 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  b, zName);..    
354fd 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
354fe 0a 0d 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  ...    /* Make s
354ff 75 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  ure the column n
35500 61 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20  ame is unique.  
35501 49 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e  If the name is n
35502 6f 74 20 75 6e 69 71 75 65 2c 0d 0a 20 20 20 20  ot unique,..    
35503 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65  ** append a inte
35504 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ger to the name 
35505 73 6f 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d  so that it becom
35506 65 73 20 75 6e 69 71 75 65 2e 0d 0a 20 20 20 20  es unique...    
35507 2a 2f 0d 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20  */..    nName = 
35508 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
35509 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 66 6f 72  zName);..    for
3550a 28 6a 3d 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20 6a  (j=cnt=0; j<i; j
3550b 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  ++){..      if( 
3550c 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 61  sqlite3StrICmp(a
3550d 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e  Col[j].zName, zN
3550e 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ame)==0 ){..    
3550f 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61      char *zNewNa
35510 6d 65 3b 0d 0a 20 20 20 20 20 20 20 20 7a 4e 61  me;..        zNa
35511 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0d 0a  me[nName] = 0;..
35512 20 20 20 20 20 20 20 20 7a 4e 65 77 4e 61 6d 65          zNewName
35513 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
35514 66 28 64 62 2c 20 22 25 73 3a 25 64 22 2c 20 7a  f(db, "%s:%d", z
35515 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b 0d 0a 20  Name, ++cnt);.. 
35516 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
35517 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  Free(db, zName);
35518 0d 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  ..        zName 
35519 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0d 0a 20 20 20  = zNewName;..   
3551a 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0d 0a 20 20       j = -1;..  
3551b 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d        if( zName=
3551c 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20  =0 ) break;..   
3551d 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
3551e 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a   pCol->zName = z
3551f 4e 61 6d 65 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  Name;..  }..  if
35520 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
35521 65 64 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 6a  ed ){..    for(j
35522 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0d 0a  =0; j<i; j++){..
35523 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
35524 72 65 65 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e  ree(db, aCol[j].
35525 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a  zName);..    }..
35526 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
35527 65 28 64 62 2c 20 61 43 6f 6c 29 3b 0d 0a 20 20  e(db, aCol);..  
35528 20 20 2a 70 61 43 6f 6c 20 3d 20 30 3b 0d 0a 20    *paCol = 0;.. 
35529 20 20 20 2a 70 6e 43 6f 6c 20 3d 20 30 3b 0d 0a     *pnCol = 0;..
3552a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3552b 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20  E_NOMEM;..  }.. 
3552c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3552d 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
3552e 41 64 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c  Add type and col
3552f 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  lation informati
35530 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c  on to a column l
35531 69 73 74 20 62 61 73 65 64 20 6f 6e 0d 0a 2a 2a  ist based on..**
35532 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
35533 65 6e 74 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20 54 68  ent...** ..** Th
35534 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72  e column list pr
35535 65 73 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72  esumably came fr
35536 6f 6d 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e  om selectColumnN
35537 61 6d 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  amesFromExprList
35538 28 29 2e 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 75  ()...** The colu
35539 6d 6e 20 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79  mn list has only
3553a 20 6e 61 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65   names, not type
3553b 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e  s or collations.
3553c 20 20 54 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69    This..** routi
3553d 6e 65 20 67 6f 65 73 20 74 68 72 6f 75 67 68 20  ne goes through 
3553e 61 6e 64 20 61 64 64 73 20 74 68 65 20 74 79 70  and adds the typ
3553f 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  es and collation
35540 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  s...**..** This 
35541 72 6f 75 74 69 6e 65 20 72 65 71 75 69 72 65 73  routine requires
35542 20 74 68 61 74 20 61 6c 6c 20 69 64 65 6e 74 69   that all identi
35543 66 69 65 72 73 20 69 6e 20 74 68 65 20 53 45 4c  fiers in the SEL
35544 45 43 54 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ECT..** statemen
35545 74 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0d 0a  t be resolved...
35546 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
35547 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54  selectAddColumnT
35548 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28  ypeAndCollation(
35549 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
3554a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
3554b 73 69 6e 67 20 63 6f 6e 74 65 78 74 73 20 2a 2f  sing contexts */
3554c 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20  ..  int nCol,   
3554d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3554e 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ber of columns *
3554f 2f 0d 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f  /..  Column *aCo
35550 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  l,         /* Li
35551 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  st of columns */
35552 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  ..  Select *pSel
35553 65 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c  ect       /* SEL
35554 45 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65  ECT used to dete
35555 72 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20  rmine types and 
35556 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0d 0a 29  collations */..)
35557 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  {..  sqlite3 *db
35558 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a   = pParse->db;..
35559 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
3555a 43 3b 0d 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  C;..  Column *pC
3555b 6f 6c 3b 0d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  ol;..  CollSeq *
3555c 70 43 6f 6c 6c 3b 0d 0a 20 20 69 6e 74 20 69 3b  pColl;..  int i;
3555d 0d 0a 20 20 45 78 70 72 20 2a 70 3b 0d 0a 20 20  ..  Expr *p;..  
3555e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
3555f 69 74 65 6d 20 2a 61 3b 0d 0a 0d 0a 20 20 61 73  item *a;....  as
35560 73 65 72 74 28 20 70 53 65 6c 65 63 74 21 3d 30  sert( pSelect!=0
35561 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 28   );..  assert( (
35562 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67  pSelect->selFlag
35563 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29  s & SF_Resolved)
35564 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  !=0 );..  assert
35565 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c 65 63 74 2d  ( nCol==pSelect-
35566 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 7c  >pEList->nExpr |
35567 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
35568 65 64 20 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d  ed );..  if( db-
35569 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
3556a 72 65 74 75 72 6e 3b 0d 0a 20 20 6d 65 6d 73 65  return;..  memse
3556b 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
3556c 66 28 73 4e 43 29 29 3b 0d 0a 20 20 73 4e 43 2e  f(sNC));..  sNC.
3556d 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65  pSrcList = pSele
3556e 63 74 2d 3e 70 53 72 63 3b 0d 0a 20 20 61 20 3d  ct->pSrc;..  a =
3556f 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
35570 2d 3e 61 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 2c  ->a;..  for(i=0,
35571 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43   pCol=aCol; i<nC
35572 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
35573 7b 0d 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e  {..    p = a[i].
35574 70 45 78 70 72 3b 0d 0a 20 20 20 20 70 43 6f 6c  pExpr;..    pCol
35575 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65  ->zType = sqlite
35576 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 63 6f  3DbStrDup(db, co
35577 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70  lumnType(&sNC, p
35578 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0d 0a 20 20  , 0, 0, 0));..  
35579 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79    pCol->affinity
3557a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66   = sqlite3ExprAf
3557b 66 69 6e 69 74 79 28 70 29 3b 0d 0a 20 20 20 20  finity(p);..    
3557c 69 66 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  if( pCol->affini
3557d 74 79 3d 3d 30 20 29 20 70 43 6f 6c 2d 3e 61 66  ty==0 ) pCol->af
3557e 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
3557f 41 46 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 20 20 70  AFF_NONE;..    p
35580 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
35581 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
35582 2c 20 70 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  , p);..    if( p
35583 43 6f 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 70  Coll ){..      p
35584 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c  Col->zColl = sql
35585 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
35586 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0d   pColl->zName);.
35587 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a  .    }..  }..}..
35588 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65 6e 20 61  ../*..** Given a
35589 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
3558a 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 54 61  t, generate a Ta
3558b 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68  ble structure th
3558c 61 74 20 64 65 73 63 72 69 62 65 73 0d 0a 2a 2a  at describes..**
3558d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
3558e 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54 2e 0d  of that SELECT..
3558f 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
35590 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
35591 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
35592 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ect(Parse *pPars
35593 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  e, Select *pSele
35594 63 74 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70  ct){..  Table *p
35595 54 61 62 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20  Tab;..  sqlite3 
35596 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
35597 3b 0d 0a 20 20 69 6e 74 20 73 61 76 65 64 46 6c  ;..  int savedFl
35598 61 67 73 3b 0d 0a 0d 0a 20 20 73 61 76 65 64 46  ags;....  savedF
35599 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73  lags = db->flags
3559a 3b 0d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  ;..  db->flags &
3559b 3d 20 7e 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  = ~SQLITE_FullCo
3559c 6c 4e 61 6d 65 73 3b 0d 0a 20 20 64 62 2d 3e 66  lNames;..  db->f
3559d 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53  lags |= SQLITE_S
3559e 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b 0d 0a 20  hortColNames;.. 
3559f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
355a0 65 70 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ep(pParse, pSele
355a1 63 74 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 70  ct, 0);..  if( p
355a2 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65  Parse->nErr ) re
355a3 74 75 72 6e 20 30 3b 0d 0a 20 20 77 68 69 6c 65  turn 0;..  while
355a4 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
355a5 72 20 29 20 70 53 65 6c 65 63 74 20 3d 20 70 53  r ) pSelect = pS
355a6 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0d 0a  elect->pPrior;..
355a7 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61    db->flags = sa
355a8 76 65 64 46 6c 61 67 73 3b 0d 0a 20 20 70 54 61  vedFlags;..  pTa
355a9 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  b = sqlite3DbMal
355aa 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
355ab 6f 66 28 54 61 62 6c 65 29 20 29 3b 0d 0a 20 20  of(Table) );..  
355ac 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0d 0a  if( pTab==0 ){..
355ad 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
355ae 20 7d 0d 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c   }..  /* The sql
355af 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
355b0 65 6c 65 63 74 28 29 20 69 73 20 6f 6e 6c 79 20  elect() is only 
355b1 75 73 65 64 20 6e 20 63 6f 6e 74 65 78 74 73 20  used n contexts 
355b2 77 68 65 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0d  where lookaside.
355b3 0a 20 20 2a 2a 20 69 73 20 64 69 73 61 62 6c 65  .  ** is disable
355b4 64 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  d */..  assert( 
355b5 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
355b6 6e 61 62 6c 65 64 3d 3d 30 20 29 3b 0d 0a 20 20  nabled==0 );..  
355b7 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0d  pTab->nRef = 1;.
355b8 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d  .  pTab->zName =
355b9 20 30 3b 0d 0a 20 20 70 54 61 62 2d 3e 6e 52 6f   0;..  pTab->nRo
355ba 77 45 73 74 20 3d 20 31 30 30 30 30 30 30 3b 0d  wEst = 1000000;.
355bb 0a 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73  .  selectColumns
355bc 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
355bd 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45  rse, pSelect->pE
355be 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f  List, &pTab->nCo
355bf 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b  l, &pTab->aCol);
355c0 0d 0a 20 20 73 65 6c 65 63 74 41 64 64 43 6f 6c  ..  selectAddCol
355c1 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74  umnTypeAndCollat
355c2 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  ion(pParse, pTab
355c3 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43  ->nCol, pTab->aC
355c4 6f 6c 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a 20  ol, pSelect);.. 
355c5 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d   pTab->iPKey = -
355c6 31 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  1;..  if( db->ma
355c7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20  llocFailed ){.. 
355c8 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
355c9 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b  Table(db, pTab);
355ca 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
355cb 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
355cc 54 61 62 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  Tab;..}..../*..*
355cd 2a 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72  * Get a VDBE for
355ce 20 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65   the given parse
355cf 72 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61  r context.  Crea
355d0 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20  te a new one if 
355d1 6e 65 63 65 73 73 61 72 79 2e 0d 0a 2a 2a 20 49  necessary...** I
355d2 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
355d3 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61  s, return NULL a
355d4 6e 64 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61  nd leave a messa
355d5 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0d 0a 2a  ge in pParse...*
355d6 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
355d7 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47  E Vdbe *sqlite3G
355d8 65 74 56 64 62 65 28 50 61 72 73 65 20 2a 70 50  etVdbe(Parse *pP
355d9 61 72 73 65 29 7b 0d 0a 20 20 56 64 62 65 20 2a  arse){..  Vdbe *
355da 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
355db 65 3b 0d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e;..  if( v==0 )
355dc 7b 0d 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73  {..    v = pPars
355dd 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74  e->pVdbe = sqlit
355de 65 33 56 64 62 65 43 72 65 61 74 65 28 70 50 61  e3VdbeCreate(pPa
355df 72 73 65 2d 3e 64 62 29 3b 0d 0a 23 69 66 6e 64  rse->db);..#ifnd
355e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
355e1 52 41 43 45 0d 0a 20 20 20 20 69 66 28 20 76 20  RACE..    if( v 
355e2 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
355e3 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
355e4 50 5f 54 72 61 63 65 29 3b 0d 0a 20 20 20 20 7d  P_Trace);..    }
355e5 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 20  ..#endif..  }.. 
355e6 20 72 65 74 75 72 6e 20 76 3b 0d 0a 7d 0d 0a 0d   return v;..}...
355e7 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74  .../*..** Comput
355e8 65 20 74 68 65 20 69 4c 69 6d 69 74 20 61 6e 64  e the iLimit and
355e9 20 69 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20   iOffset fields 
355ea 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 62 61  of the SELECT ba
355eb 73 65 64 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20 70  sed on the..** p
355ec 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65  Limit and pOffse
355ed 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20  t expressions.  
355ee 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73  pLimit and pOffs
355ef 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78 70 72  et hold the expr
355f0 65 73 73 69 6f 6e 73 0d 0a 2a 2a 20 74 68 61 74  essions..** that
355f1 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6f   appear in the o
355f2 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
355f3 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65 20  ement after the 
355f4 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54  LIMIT and OFFSET
355f5 0d 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20  ..** keywords.  
355f6 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65  Or NULL if those
355f7 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d   keywords are om
355f8 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e  itted. iLimit an
355f9 64 20 69 4f 66 66 73 65 74 20 0d 0a 2a 2a 20 61  d iOffset ..** a
355fa 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d  re the integer m
355fb 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e  emory register n
355fc 75 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74  umbers for count
355fd 65 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  ers used to comp
355fe 75 74 65 20 0d 0a 2a 2a 20 74 68 65 20 6c 69 6d  ute ..** the lim
355ff 69 74 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20  it and offset.  
35600 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c  If there is no l
35601 69 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73  imit and/or offs
35602 65 74 2c 20 74 68 65 6e 20 0d 0a 2a 2a 20 69 4c  et, then ..** iL
35603 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74  imit and iOffset
35604 20 61 72 65 20 6e 65 67 61 74 69 76 65 2e 0d 0a   are negative...
35605 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  **..** This rout
35606 69 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65 20  ine changes the 
35607 76 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69 74  values of iLimit
35608 20 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e 6c   and iOffset onl
35609 79 20 69 66 0d 0a 2a 2a 20 61 20 6c 69 6d 69 74  y if..** a limit
3560a 20 6f 72 20 6f 66 66 73 65 74 20 69 73 20 64 65   or offset is de
3560b 66 69 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20  fined by pLimit 
3560c 61 6e 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c  and pOffset.  iL
3560d 69 6d 69 74 20 61 6e 64 0d 0a 2a 2a 20 69 4f 66  imit and..** iOf
3560e 66 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65  fset should have
3560f 20 62 65 65 6e 20 70 72 65 73 65 74 20 74 6f 20   been preset to 
35610 61 70 70 72 6f 70 72 69 61 74 65 20 64 65 66 61  appropriate defa
35611 75 6c 74 20 76 61 6c 75 65 73 0d 0a 2a 2a 20 28  ult values..** (
35612 75 73 75 61 6c 6c 79 20 62 75 74 20 6e 6f 74 20  usually but not 
35613 61 6c 77 61 79 73 20 2d 31 29 20 70 72 69 6f 72  always -1) prior
35614 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   to calling this
35615 20 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2a 20 4f 6e   routine...** On
35616 6c 79 20 69 66 20 70 4c 69 6d 69 74 21 3d 30 20  ly if pLimit!=0 
35617 6f 72 20 70 4f 66 66 73 65 74 21 3d 30 20 64 6f  or pOffset!=0 do
35618 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69 73   the limit regis
35619 74 65 72 73 20 67 65 74 0d 0a 2a 2a 20 72 65 64  ters get..** red
3561a 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55 4e 49  efined.  The UNI
3561b 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20  ON ALL operator 
3561c 75 73 65 73 20 74 68 69 73 20 70 72 6f 70 65 72  uses this proper
3561d 74 79 20 74 6f 20 66 6f 72 63 65 0d 0a 2a 2a 20  ty to force..** 
3561e 74 68 65 20 72 65 75 73 65 20 6f 66 20 74 68 65  the reuse of the
3561f 20 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20   same limit and 
35620 6f 66 66 73 65 74 20 72 65 67 69 73 74 65 72 73  offset registers
35621 20 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65   across multiple
35622 0d 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  ..** SELECT stat
35623 65 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  ements...*/..sta
35624 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
35625 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 50  LimitRegisters(P
35626 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
35627 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 42 72  lect *p, int iBr
35628 65 61 6b 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76  eak){..  Vdbe *v
35629 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69 4c 69   = 0;..  int iLi
3562a 6d 69 74 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20  mit = 0;..  int 
3562b 69 4f 66 66 73 65 74 3b 0d 0a 20 20 69 6e 74 20  iOffset;..  int 
3562c 61 64 64 72 31 2c 20 6e 3b 0d 0a 20 20 69 66 28  addr1, n;..  if(
3562d 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65 74   p->iLimit ) ret
3562e 75 72 6e 3b 0d 0a 0d 0a 20 20 2f 2a 20 0d 0a 20  urn;....  /* .. 
3562f 20 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22 20 61   ** "LIMIT -1" a
35630 6c 77 61 79 73 20 73 68 6f 77 73 20 61 6c 6c 20  lways shows all 
35631 72 6f 77 73 2e 20 20 54 68 65 72 65 20 69 73 20  rows.  There is 
35632 73 6f 6d 65 0d 0a 20 20 2a 2a 20 63 6f 6e 74 72  some..  ** contr
35633 61 76 65 72 73 79 20 61 62 6f 75 74 20 77 68 61  aversy about wha
35634 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65  t the correct be
35635 68 61 76 69 6f 72 20 73 68 6f 75 6c 64 20 62 65  havior should be
35636 2e 0d 0a 20 20 2a 2a 20 54 68 65 20 63 75 72 72  ...  ** The curr
35637 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ent implementati
35638 6f 6e 20 69 6e 74 65 72 70 72 65 74 73 20 22 4c  on interprets "L
35639 49 4d 49 54 20 30 22 20 74 6f 20 6d 65 61 6e 0d  IMIT 0" to mean.
3563a 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0d 0a  .  ** no rows...
3563b 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 45    */..  sqlite3E
3563c 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50  xprCacheClear(pP
3563d 61 72 73 65 29 3b 0d 0a 20 20 61 73 73 65 72 74  arse);..  assert
3563e 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20  ( p->pOffset==0 
3563f 7c 7c 20 70 2d 3e 70 4c 69 6d 69 74 21 3d 30 20  || p->pLimit!=0 
35640 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69  );..  if( p->pLi
35641 6d 69 74 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 69  mit ){..    p->i
35642 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d  Limit = iLimit =
35643 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
35644 0d 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ..    v = sqlite
35645 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
35646 3b 0d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  ;..    if( NEVER
35647 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
35648 20 20 2f 2a 20 56 44 42 45 20 73 68 6f 75 6c 64    /* VDBE should
35649 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
3564a 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0d  en allocated */.
3564b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
3564c 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d  ExprIsInteger(p-
3564d 3e 70 4c 69 6d 69 74 2c 20 26 6e 29 20 29 7b 0d  >pLimit, &n) ){.
3564e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3564f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
35650 6e 74 65 67 65 72 2c 20 6e 2c 20 69 4c 69 6d 69  nteger, n, iLimi
35651 74 29 3b 0d 0a 20 20 20 20 20 20 56 64 62 65 43  t);..      VdbeC
35652 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49  omment((v, "LIMI
35653 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0d 0a 20  T counter"));.. 
35654 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b       if( n==0 ){
35655 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
35656 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35657 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 72 65 61  P_Goto, 0, iBrea
35658 6b 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  k);..      }else
35659 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  {..        if( p
3565a 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3e 20 28  ->nSelectRow > (
3565b 64 6f 75 62 6c 65 29 6e 20 29 20 70 2d 3e 6e 53  double)n ) p->nS
3565c 65 6c 65 63 74 52 6f 77 20 3d 20 28 64 6f 75 62  electRow = (doub
3565d 6c 65 29 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  le)n;..      }..
3565e 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
3565f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
35660 65 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69  e(pParse, p->pLi
35661 6d 69 74 2c 20 69 4c 69 6d 69 74 29 3b 0d 0a 20  mit, iLimit);.. 
35662 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35663 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
35664 74 42 65 49 6e 74 2c 20 69 4c 69 6d 69 74 29 3b  tBeInt, iLimit);
35665 0d 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ..      VdbeComm
35666 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 20 63  ent((v, "LIMIT c
35667 6f 75 6e 74 65 72 22 29 29 3b 0d 0a 20 20 20 20  ounter"));..    
35668 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
35669 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op2(v, OP_IfZero
3566a 2c 20 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b  , iLimit, iBreak
3566b 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
3566c 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b  f( p->pOffset ){
3566d 0d 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ..      p->iOffs
3566e 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d 20 2b  et = iOffset = +
3566f 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
35670 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
35671 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63  em++;   /* Alloc
35672 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 65 67  ate an extra reg
35673 69 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 2b  ister for limit+
35674 6f 66 66 73 65 74 20 2a 2f 0d 0a 20 20 20 20 20  offset */..     
35675 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
35676 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66  (pParse, p->pOff
35677 73 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0d 0a  set, iOffset);..
35678 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
35679 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
3567a 73 74 42 65 49 6e 74 2c 20 69 4f 66 66 73 65 74  stBeInt, iOffset
3567b 29 3b 0d 0a 20 20 20 20 20 20 56 64 62 65 43 6f  );..      VdbeCo
3567c 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46 53 45  mment((v, "OFFSE
3567d 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0d 0a 20  T counter"));.. 
3567e 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c       addr1 = sql
3567f 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
35680 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66  , OP_IfPos, iOff
35681 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  set);..      sql
35682 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
35683 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
35684 20 69 4f 66 66 73 65 74 29 3b 0d 0a 20 20 20 20   iOffset);..    
35685 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
35686 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
35687 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
35688 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
35689 41 64 64 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66  Add, iLimit, iOf
3568a 66 73 65 74 2c 20 69 4f 66 66 73 65 74 2b 31 29  fset, iOffset+1)
3568b 3b 0d 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  ;..      VdbeCom
3568c 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 2b  ment((v, "LIMIT+
3568d 4f 46 46 53 45 54 22 29 29 3b 0d 0a 20 20 20 20  OFFSET"));..    
3568e 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
3568f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
35690 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d 69 74 29  P_IfPos, iLimit)
35691 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
35692 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
35693 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 69 4f  _Integer, -1, iO
35694 66 66 73 65 74 2b 31 29 3b 0d 0a 20 20 20 20 20  ffset+1);..     
35695 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
35696 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0d  Here(v, addr1);.
35697 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a  .    }..  }..}..
35698 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
35699 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
3569a 45 4c 45 43 54 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ELECT../*..** Re
3569b 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72  turn the appropr
3569c 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73  iate collating s
3569d 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
3569e 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f  iCol-th column o
3569f 66 0d 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  f..** the result
356a0 20 73 65 74 20 66 6f 72 20 74 68 65 20 63 6f 6d   set for the com
356a1 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73 74 61  pound-select sta
356a2 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52 65 74  tement "p".  Ret
356a3 75 72 6e 20 4e 55 4c 4c 20 69 66 0d 0a 2a 2a 20  urn NULL if..** 
356a4 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e  the column has n
356a5 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
356a6 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0d 0a 2a  ing sequence...*
356a7 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74  *..** The collat
356a8 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
356a9 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
356aa 6c 65 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72  lect is taken fr
356ab 6f 6d 20 74 68 65 0d 0a 2a 2a 20 6c 65 66 74 2d  om the..** left-
356ac 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65  most term of the
356ad 20 73 65 6c 65 63 74 20 74 68 61 74 20 68 61 73   select that has
356ae 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71   a collating seq
356af 75 65 6e 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  uence...*/..stat
356b0 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74  ic CollSeq *mult
356b1 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 50  iSelectCollSeq(P
356b2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
356b3 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 43 6f  lect *p, int iCo
356b4 6c 29 7b 0d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  l){..  CollSeq *
356b5 70 52 65 74 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  pRet;..  if( p->
356b6 70 50 72 69 6f 72 20 29 7b 0d 0a 20 20 20 20 70  pPrior ){..    p
356b7 52 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  Ret = multiSelec
356b8 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
356b9 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c   p->pPrior, iCol
356ba 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  );..  }else{..  
356bb 20 20 70 52 65 74 20 3d 20 30 3b 0d 0a 20 20 7d    pRet = 0;..  }
356bc 0d 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  ..  assert( iCol
356bd 3e 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 52  >=0 );..  if( pR
356be 65 74 3d 3d 30 20 26 26 20 69 43 6f 6c 3c 70 2d  et==0 && iCol<p-
356bf 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
356c0 7b 0d 0a 20 20 20 20 70 52 65 74 20 3d 20 73 71  {..    pRet = sq
356c1 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
356c2 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69  (pParse, p->pELi
356c3 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70  st->a[iCol].pExp
356c4 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  r);..  }..  retu
356c5 72 6e 20 70 52 65 74 3b 0d 0a 7d 0d 0a 23 65 6e  rn pRet;..}..#en
356c6 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
356c7 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
356c8 43 54 20 2a 2f 0d 0a 0d 0a 2f 2a 20 46 6f 72 77  CT */..../* Forw
356c9 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f  ard reference */
356ca 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c  ..static int mul
356cb 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28  tiSelectOrderBy(
356cc 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
356cd 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
356ce 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d  sing context */.
356cf 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
356d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
356d1 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45  right-most of SE
356d2 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65  LECTs to be code
356d3 64 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 44 65  d */..  SelectDe
356d4 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a  st *pDest     /*
356d5 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
356d6 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
356d7 2f 0d 0a 29 3b 0d 0a 0d 0a 0d 0a 23 69 66 6e 64  /..);......#ifnd
356d8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
356d9 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0d 0a  OMPOUND_SELECT..
356da 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
356db 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
356dc 20 70 72 6f 63 65 73 73 20 61 20 63 6f 6d 70 6f   process a compo
356dd 75 6e 64 20 71 75 65 72 79 20 66 6f 72 6d 20 66  und query form f
356de 72 6f 6d 0d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d  rom..** two or m
356df 6f 72 65 20 73 65 70 61 72 61 74 65 20 71 75 65  ore separate que
356e0 72 69 65 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e  ries using UNION
356e1 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43  , UNION ALL, EXC
356e2 45 50 54 2c 20 6f 72 0d 0a 2a 2a 20 49 4e 54 45  EPT, or..** INTE
356e3 52 53 45 43 54 0d 0a 2a 2a 0d 0a 2a 2a 20 22 70  RSECT..**..** "p
356e4 22 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  " points to the 
356e5 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68  right-most of th
356e6 65 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20  e two queries.  
356e7 74 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65  the query on the
356e8 0d 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e  ..** left is p->
356e9 70 50 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66  pPrior.  The lef
356ea 74 20 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c  t query could al
356eb 73 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64  so be a compound
356ec 20 71 75 65 72 79 0d 0a 2a 2a 20 69 6e 20 77 68   query..** in wh
356ed 69 63 68 20 63 61 73 65 20 74 68 69 73 20 72 6f  ich case this ro
356ee 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61  utine will be ca
356ef 6c 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c 79  lled recursively
356f0 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  . ..**..** The r
356f1 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 74 6f  esults of the to
356f2 74 61 6c 20 71 75 65 72 79 20 61 72 65 20 74 6f  tal query are to
356f3 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
356f4 20 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 0d 0a   a destination..
356f5 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65 73 74  ** of type eDest
356f6 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72 20   with parameter 
356f7 69 50 61 72 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45  iParm...**..** E
356f8 78 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69  xample 1:  Consi
356f9 64 65 72 20 61 20 74 68 72 65 65 2d 77 61 79 20  der a three-way 
356fa 63 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61  compound SQL sta
356fb 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tement...**..** 
356fc 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f      SELECT a FRO
356fd 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43  M t1 UNION SELEC
356fe 54 20 62 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f  T b FROM t2 UNIO
356ff 4e 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20  N SELECT c FROM 
35700 74 33 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  t3..**..** This 
35701 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72  statement is par
35702 73 65 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f 77  sed up as follow
35703 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53  s:..**..**     S
35704 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0d  ELECT c FROM t3.
35705 0a 2a 2a 20 20 20 20 20 20 7c 0d 0a 2a 2a 20 20  .**      |..**  
35706 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c      `----->  SEL
35707 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0d 0a 2a  ECT b FROM t2..*
35708 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
35709 20 7c 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   |..**          
3570a 20 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20        `------>  
3570b 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31  SELECT a FROM t1
3570c 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 72 72  ..**..** The arr
3570d 6f 77 73 20 69 6e 20 74 68 65 20 64 69 61 67 72  ows in the diagr
3570e 61 6d 20 61 62 6f 76 65 20 72 65 70 72 65 73 65  am above represe
3570f 6e 74 20 74 68 65 20 53 65 6c 65 63 74 2e 70 50  nt the Select.pP
35710 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a  rior pointer...*
35711 2a 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75  * So if this rou
35712 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
35713 69 74 68 20 70 20 65 71 75 61 6c 20 74 6f 20 74  ith p equal to t
35714 68 65 20 74 33 20 71 75 65 72 79 2c 20 74 68 65  he t3 query, the
35715 6e 0d 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69 6c  n..** pPrior wil
35716 6c 20 62 65 20 74 68 65 20 74 32 20 71 75 65 72  l be the t2 quer
35717 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20 62  y.  p->op will b
35718 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74 68  e TK_UNION in th
35719 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  is case...**..**
3571a 20 4e 6f 74 69 63 65 20 74 68 61 74 20 62 65 63   Notice that bec
3571b 61 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 20  ause of the way 
3571c 53 51 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f  SQLite parses co
3571d 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20  mpound SELECTs, 
3571e 74 68 65 0d 0a 2a 2a 20 69 6e 64 69 76 69 64 75  the..** individu
3571f 61 6c 20 73 65 6c 65 63 74 73 20 61 6c 77 61 79  al selects alway
35720 73 20 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66  s group from lef
35721 74 20 74 6f 20 72 69 67 68 74 2e 0d 0a 2a 2f 0d  t to right...*/.
35722 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74  .static int mult
35723 69 53 65 6c 65 63 74 28 0d 0a 20 20 50 61 72 73  iSelect(..  Pars
35724 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
35725 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
35726 74 65 78 74 20 2a 2f 0d 0a 20 20 53 65 6c 65 63  text */..  Selec
35727 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
35728 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f   /* The right-mo
35729 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f  st of SELECTs to
3572a 20 62 65 20 63 6f 64 65 64 20 2a 2f 0d 0a 20 20   be coded */..  
3572b 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
3572c 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f  t     /* What to
3572d 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72   do with query r
3572e 65 73 75 6c 74 73 20 2a 2f 0d 0a 29 7b 0d 0a 20  esults */..){.. 
3572f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
35730 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 73  _OK;   /* Succes
35731 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 75  s code from a su
35732 62 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 53  broutine */..  S
35733 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
35734 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
35735 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
35736 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
35737 2f 0d 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  /..  Vdbe *v;   
35738 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65             /* Ge
35739 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74  nerate code to t
3573a 68 69 73 20 56 44 42 45 20 2a 2f 0d 0a 20 20 53  his VDBE */..  S
3573b 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20  electDest dest; 
3573c 20 20 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74       /* Alternat
3573d 69 76 65 20 64 61 74 61 20 64 65 73 74 69 6e 61  ive data destina
3573e 74 69 6f 6e 20 2a 2f 0d 0a 20 20 53 65 6c 65 63  tion */..  Selec
3573f 74 20 2a 70 44 65 6c 65 74 65 20 3d 20 30 3b 20  t *pDelete = 0; 
35740 20 2f 2a 20 43 68 61 69 6e 20 6f 66 20 73 69 6d   /* Chain of sim
35741 70 6c 65 20 73 65 6c 65 63 74 73 20 74 6f 20 64  ple selects to d
35742 65 6c 65 74 65 20 2a 2f 0d 0a 20 20 73 71 6c 69  elete */..  sqli
35743 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
35744 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
35745 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 23 69 66  nnection */..#if
35746 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
35747 5f 45 58 50 4c 41 49 4e 0d 0a 20 20 69 6e 74 20  _EXPLAIN..  int 
35748 69 53 75 62 31 3b 20 20 20 20 20 20 20 20 20 20  iSub1;          
35749 20 20 2f 2a 20 45 51 50 20 69 64 20 6f 66 20 6c    /* EQP id of l
3574a 65 66 74 2d 68 61 6e 64 20 71 75 65 72 79 20 2a  eft-hand query *
3574b 2f 0d 0a 20 20 69 6e 74 20 69 53 75 62 32 3b 20  /..  int iSub2; 
3574c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 51             /* EQ
3574d 50 20 69 64 20 6f 66 20 72 69 67 68 74 2d 68 61  P id of right-ha
3574e 6e 64 20 71 75 65 72 79 20 2a 2f 0d 0a 23 65 6e  nd query */..#en
3574f 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65  dif....  /* Make
35750 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 6e   sure there is n
35751 6f 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49  o ORDER BY or LI
35752 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20 70 72  MIT clause on pr
35753 69 6f 72 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e  ior SELECTs.  On
35754 6c 79 0d 0a 20 20 2a 2a 20 74 68 65 20 6c 61 73  ly..  ** the las
35755 74 20 28 72 69 67 68 74 2d 6d 6f 73 74 29 20 53  t (right-most) S
35756 45 4c 45 43 54 20 69 6e 20 74 68 65 20 73 65 72  ELECT in the ser
35757 69 65 73 20 6d 61 79 20 68 61 76 65 20 61 6e 20  ies may have an 
35758 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49  ORDER BY or LIMI
35759 54 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65  T...  */..  asse
3575a 72 74 28 20 70 20 26 26 20 70 2d 3e 70 50 72 69  rt( p && p->pPri
3575b 6f 72 20 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e  or );  /* Callin
3575c 67 20 66 75 6e 63 74 69 6f 6e 20 67 75 61 72 61  g function guara
3575d 6e 74 65 65 73 20 74 68 69 73 20 6d 75 63 68 20  ntees this much 
3575e 2a 2f 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73  */..  db = pPars
3575f 65 2d 3e 64 62 3b 0d 0a 20 20 70 50 72 69 6f 72  e->db;..  pPrior
35760 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0d 0a 20   = p->pPrior;.. 
35761 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d   assert( pPrior-
35762 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70 50 72  >pRightmost!=pPr
35763 69 6f 72 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ior );..  assert
35764 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74  ( pPrior->pRight
35765 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68 74 6d  most==p->pRightm
35766 6f 73 74 20 29 3b 0d 0a 20 20 64 65 73 74 20 3d  ost );..  dest =
35767 20 2a 70 44 65 73 74 3b 0d 0a 20 20 69 66 28 20   *pDest;..  if( 
35768 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
35769 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
3576a 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
3576b 22 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65  "ORDER BY clause
3576c 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74   should come aft
3576d 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65  er %s not before
3576e 22 2c 0d 0a 20 20 20 20 20 20 73 65 6c 65 63 74  ",..      select
3576f 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0d  OpName(p->op));.
35770 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a 20 20  .    rc = 1;..  
35771 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
35772 65 63 74 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a 20  ect_end;..  }.. 
35773 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69   if( pPrior->pLi
35774 6d 69 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  mit ){..    sqli
35775 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
35776 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65  se,"LIMIT clause
35777 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74   should come aft
35778 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65  er %s not before
35779 22 2c 0d 0a 20 20 20 20 20 20 73 65 6c 65 63 74  ",..      select
3577a 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0d  OpName(p->op));.
3577b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a 20 20  .    rc = 1;..  
3577c 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
3577d 65 63 74 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a 0d  ect_end;..  }...
3577e 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
3577f 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a  tVdbe(pParse);..
35780 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
35781 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 61  ;  /* The VDBE a
35782 6c 72 65 61 64 79 20 63 72 65 61 74 65 64 20 62  lready created b
35783 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  y calling functi
35784 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 43 72  on */....  /* Cr
35785 65 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61  eate the destina
35786 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  tion temporary t
35787 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
35788 79 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64  y..  */..  if( d
35789 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45  est.eDest==SRT_E
3578a 70 68 65 6d 54 61 62 20 29 7b 0d 0a 20 20 20 20  phemTab ){..    
3578b 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
3578c 74 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  t );..    sqlite
3578d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3578e 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
3578f 20 64 65 73 74 2e 69 50 61 72 6d 2c 20 70 2d 3e   dest.iParm, p->
35790 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0d  pEList->nExpr);.
35791 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
35792 43 68 61 6e 67 65 50 35 28 76 2c 20 42 54 52 45  ChangeP5(v, BTRE
35793 45 5f 55 4e 4f 52 44 45 52 45 44 29 3b 0d 0a 20  E_UNORDERED);.. 
35794 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20     dest.eDest = 
35795 53 52 54 5f 54 61 62 6c 65 3b 0d 0a 20 20 7d 0d  SRT_Table;..  }.
35796 0a 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ...  /* Make sur
35797 65 20 61 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e  e all SELECTs in
35798 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68   the statement h
35799 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ave the same num
3579a 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0d  ber of elements.
3579b 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69 72 20 72  .  ** in their r
3579c 65 73 75 6c 74 20 73 65 74 73 2e 0d 0a 20 20 2a  esult sets...  *
3579d 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  /..  assert( p->
3579e 70 45 4c 69 73 74 20 26 26 20 70 50 72 69 6f 72  pEList && pPrior
3579f 2d 3e 70 45 4c 69 73 74 20 29 3b 0d 0a 20 20 69  ->pEList );..  i
357a0 66 28 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  f( p->pEList->nE
357a1 78 70 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c  xpr!=pPrior->pEL
357a2 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0d 0a 20  ist->nExpr ){.. 
357a3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
357a4 73 67 28 70 50 61 72 73 65 2c 20 22 53 45 4c 45  sg(pParse, "SELE
357a5 43 54 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  CTs to the left 
357a6 61 6e 64 20 72 69 67 68 74 20 6f 66 20 25 73 22  and right of %s"
357a7 0d 0a 20 20 20 20 20 20 22 20 64 6f 20 6e 6f 74  ..      " do not
357a8 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e   have the same n
357a9 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
357aa 63 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c 65 63 74  columns", select
357ab 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0d  OpName(p->op));.
357ac 0a 20 20 20 20 72 63 20 3d 20 31 3b 0d 0a 20 20  .    rc = 1;..  
357ad 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
357ae 65 63 74 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a 0d  ect_end;..  }...
357af 0a 20 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64 20 53  .  /* Compound S
357b0 45 4c 45 43 54 73 20 74 68 61 74 20 68 61 76 65  ELECTs that have
357b1 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
357b2 75 73 65 20 61 72 65 20 68 61 6e 64 6c 65 64 20  use are handled 
357b3 73 65 70 61 72 61 74 65 6c 79 2e 0d 0a 20 20 2a  separately...  *
357b4 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64  /..  if( p->pOrd
357b5 65 72 42 79 20 29 7b 0d 0a 20 20 20 20 72 65 74  erBy ){..    ret
357b6 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f  urn multiSelectO
357b7 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70  rderBy(pParse, p
357b8 2c 20 70 44 65 73 74 29 3b 0d 0a 20 20 7d 0d 0a  , pDest);..  }..
357b9 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
357ba 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 66  code for the lef
357bb 74 20 61 6e 64 20 72 69 67 68 74 20 53 45 4c 45  t and right SELE
357bc 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0d 0a  CT statements...
357bd 20 20 2a 2f 0d 0a 20 20 73 77 69 74 63 68 28 20    */..  switch( 
357be 70 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 63 61  p->op ){..    ca
357bf 73 65 20 54 4b 5f 41 4c 4c 3a 20 7b 0d 0a 20 20  se TK_ALL: {..  
357c0 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30      int addr = 0
357c1 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 4c 69  ;..      int nLi
357c2 6d 69 74 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  mit;..      asse
357c3 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c 69  rt( !pPrior->pLi
357c4 6d 69 74 20 29 3b 0d 0a 20 20 20 20 20 20 70 50  mit );..      pP
357c5 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70  rior->pLimit = p
357c6 2d 3e 70 4c 69 6d 69 74 3b 0d 0a 20 20 20 20 20  ->pLimit;..     
357c7 20 70 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74   pPrior->pOffset
357c8 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0d 0a   = p->pOffset;..
357c9 20 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74        explainSet
357ca 49 6e 74 65 67 65 72 28 69 53 75 62 31 2c 20 70  Integer(iSub1, p
357cb 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65  Parse->iNextSele
357cc 63 74 49 64 29 3b 0d 0a 20 20 20 20 20 20 72 63  ctId);..      rc
357cd 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
357ce 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
357cf 20 26 64 65 73 74 29 3b 0d 0a 20 20 20 20 20 20   &dest);..      
357d0 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0d 0a  p->pLimit = 0;..
357d1 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74        p->pOffset
357d2 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66 28   = 0;..      if(
357d3 20 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20   rc ){..        
357d4 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
357d5 74 5f 65 6e 64 3b 0d 0a 20 20 20 20 20 20 7d 0d  t_end;..      }.
357d6 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
357d7 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 2d 3e   = 0;..      p->
357d8 69 4c 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d  iLimit = pPrior-
357d9 3e 69 4c 69 6d 69 74 3b 0d 0a 20 20 20 20 20 20  >iLimit;..      
357da 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 70 50 72  p->iOffset = pPr
357db 69 6f 72 2d 3e 69 4f 66 66 73 65 74 3b 0d 0a 20  ior->iOffset;.. 
357dc 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d       if( p->iLim
357dd 69 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61  it ){..        a
357de 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
357df 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
357e0 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 29  Zero, p->iLimit)
357e1 3b 0d 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  ;..        VdbeC
357e2 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70  omment((v, "Jump
357e3 20 61 68 65 61 64 20 69 66 20 4c 49 4d 49 54 20   ahead if LIMIT 
357e4 72 65 61 63 68 65 64 22 29 29 3b 0d 0a 20 20 20  reached"));..   
357e5 20 20 20 7d 0d 0a 20 20 20 20 20 20 65 78 70 6c     }..      expl
357e6 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53  ainSetInteger(iS
357e7 75 62 32 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65  ub2, pParse->iNe
357e8 78 74 53 65 6c 65 63 74 49 64 29 3b 0d 0a 20 20  xtSelectId);..  
357e9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
357ea 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
357eb 2c 20 26 64 65 73 74 29 3b 0d 0a 20 20 20 20 20  , &dest);..     
357ec 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
357ed 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20  QLITE_OK );..   
357ee 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e     pDelete = p->
357ef 70 50 72 69 6f 72 3b 0d 0a 20 20 20 20 20 20 70  pPrior;..      p
357f0 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
357f1 72 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 53 65  r;..      p->nSe
357f2 6c 65 63 74 52 6f 77 20 2b 3d 20 70 50 72 69 6f  lectRow += pPrio
357f3 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 3b 0d 0a  r->nSelectRow;..
357f4 20 20 20 20 20 20 69 66 28 20 70 50 72 69 6f 72        if( pPrior
357f5 2d 3e 70 4c 69 6d 69 74 0d 0a 20 20 20 20 20 20  ->pLimit..      
357f6 20 26 26 20 73 71 6c 69 74 65 33 45 78 70 72 49   && sqlite3ExprI
357f7 73 49 6e 74 65 67 65 72 28 70 50 72 69 6f 72 2d  sInteger(pPrior-
357f8 3e 70 4c 69 6d 69 74 2c 20 26 6e 4c 69 6d 69 74  >pLimit, &nLimit
357f9 29 0d 0a 20 20 20 20 20 20 20 26 26 20 70 2d 3e  )..       && p->
357fa 6e 53 65 6c 65 63 74 52 6f 77 20 3e 20 28 64 6f  nSelectRow > (do
357fb 75 62 6c 65 29 6e 4c 69 6d 69 74 20 0d 0a 20 20  uble)nLimit ..  
357fc 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20      ){..        
357fd 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20  p->nSelectRow = 
357fe 28 64 6f 75 62 6c 65 29 6e 4c 69 6d 69 74 3b 0d  (double)nLimit;.
357ff 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
35800 69 66 28 20 61 64 64 72 20 29 7b 0d 0a 20 20 20  if( addr ){..   
35801 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35802 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
35803 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
35804 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
35805 0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  ..    case TK_EX
35806 43 45 50 54 3a 0d 0a 20 20 20 20 63 61 73 65 20  CEPT:..    case 
35807 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0d 0a 20 20 20  TK_UNION: {..   
35808 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b     int unionTab;
35809 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
3580a 6d 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70  mber of the temp
3580b 6f 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64  orary table hold
3580c 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0d 0a 20  ing result */.. 
3580d 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 20       u8 op = 0; 
3580e 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
3580f 74 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69  the SRT_ operati
35810 6f 6e 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20  ons to apply to 
35811 73 65 6c 66 20 2a 2f 0d 0a 20 20 20 20 20 20 69  self */..      i
35812 6e 74 20 70 72 69 6f 72 4f 70 3b 20 20 20 20 20  nt priorOp;     
35813 2f 2a 20 54 68 65 20 53 52 54 5f 20 6f 70 65 72  /* The SRT_ oper
35814 61 74 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74  ation to apply t
35815 6f 20 70 72 69 6f 72 20 73 65 6c 65 63 74 73 20  o prior selects 
35816 2a 2f 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a  */..      Expr *
35817 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74  pLimit, *pOffset
35818 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65  ; /* Saved value
35819 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61  s of p->nLimit a
3581a 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f  nd p->nOffset */
3581b 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ..      int addr
3581c 3b 0d 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44  ;..      SelectD
3581d 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0d 0a  est uniondest;..
3581e 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3581f 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45  ( p->op==TK_EXCE
35820 50 54 20 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  PT );..      tes
35821 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
35822 5f 55 4e 49 4f 4e 20 29 3b 0d 0a 20 20 20 20 20  _UNION );..     
35823 20 70 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55   priorOp = SRT_U
35824 6e 69 6f 6e 3b 0d 0a 20 20 20 20 20 20 69 66 28  nion;..      if(
35825 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 70 72 69   dest.eDest==pri
35826 6f 72 4f 70 20 26 26 20 41 4c 57 41 59 53 28 21  orOp && ALWAYS(!
35827 70 2d 3e 70 4c 69 6d 69 74 20 26 26 21 70 2d 3e  p->pLimit &&!p->
35828 70 4f 66 66 73 65 74 29 20 29 7b 0d 0a 20 20 20  pOffset) ){..   
35829 20 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 20 72       /* We can r
3582a 65 75 73 65 20 61 20 74 65 6d 70 6f 72 61 72 79  euse a temporary
3582b 20 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65 64   table generated
3582c 20 62 79 20 61 20 53 45 4c 45 43 54 20 74 6f 20   by a SELECT to 
3582d 6f 75 72 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  our..        ** 
3582e 72 69 67 68 74 2e 0d 0a 20 20 20 20 20 20 20 20  right...        
3582f 2a 2f 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  */..        asse
35830 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73  rt( p->pRightmos
35831 74 21 3d 70 20 29 3b 20 20 2f 2a 20 43 61 6e 20  t!=p );  /* Can 
35832 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72 20  only happen for 
35833 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e 74  leftward element
35834 73 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s..             
35835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35836 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
35837 33 2d 77 61 79 20 6f 72 20 6d 6f 72 65 20 63 6f  3-way or more co
35838 6d 70 6f 75 6e 64 20 2a 2f 0d 0a 20 20 20 20 20  mpound */..     
35839 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c     assert( p->pL
3583a 69 6d 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20  imit==0 );      
3583b 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  /* Not allowed o
3583c 6e 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65  n leftward eleme
3583d 6e 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  nts */..        
3583e 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73  assert( p->pOffs
3583f 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20  et==0 );     /* 
35840 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c  Not allowed on l
35841 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e 74 73  eftward elements
35842 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 75 6e 69   */..        uni
35843 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69 50 61  onTab = dest.iPa
35844 72 6d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  rm;..      }else
35845 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65  {..        /* We
35846 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72   will need to cr
35847 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d  eate our own tem
35848 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20  porary table to 
35849 68 6f 6c 64 20 74 68 65 0d 0a 20 20 20 20 20 20  hold the..      
3584a 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74    ** intermediat
3584b 65 20 72 65 73 75 6c 74 73 2e 0d 0a 20 20 20 20  e results...    
3584c 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
3584d 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72 73  unionTab = pPars
3584e 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20 20 20 20  e->nTab++;..    
3584f 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
35850 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0d 0a 20  OrderBy==0 );.. 
35851 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71         addr = sq
35852 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35853 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
35854 72 61 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30  ral, unionTab, 0
35855 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  );..        asse
35856 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45  rt( p->addrOpenE
35857 70 68 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0d  phm[0] == -1 );.
35858 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 64 64 72  .        p->addr
35859 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 61 64  OpenEphm[0] = ad
3585a 64 72 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e  dr;..        p->
3585b 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65 6c 46  pRightmost->selF
3585c 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65 73 45  lags |= SF_UsesE
3585d 70 68 65 6d 65 72 61 6c 3b 0d 0a 20 20 20 20 20  phemeral;..     
3585e 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45     assert( p->pE
3585f 4c 69 73 74 20 29 3b 0d 0a 20 20 20 20 20 20 7d  List );..      }
35860 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64  ....      /* Cod
35861 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
35862 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75 72 20 6c  tements to our l
35863 65 66 74 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  eft..      */.. 
35864 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50       assert( !pP
35865 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 29  rior->pOrderBy )
35866 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
35867 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
35868 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f 72  uniondest, prior
35869 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0d 0a  Op, unionTab);..
3586a 20 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74        explainSet
3586b 49 6e 74 65 67 65 72 28 69 53 75 62 31 2c 20 70  Integer(iSub1, p
3586c 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65  Parse->iNextSele
3586d 63 74 49 64 29 3b 0d 0a 20 20 20 20 20 20 72 63  ctId);..      rc
3586e 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
3586f 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
35870 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0d 0a 20   &uniondest);.. 
35871 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0d 0a       if( rc ){..
35872 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
35873 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0d 0a  ti_select_end;..
35874 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20        }....     
35875 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72   /* Code the cur
35876 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74  rent SELECT stat
35877 65 6d 65 6e 74 0d 0a 20 20 20 20 20 20 2a 2f 0d  ement..      */.
35878 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  .      if( p->op
35879 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0d 0a  ==TK_EXCEPT ){..
3587a 20 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54          op = SRT
3587b 5f 45 78 63 65 70 74 3b 0d 0a 20 20 20 20 20 20  _Except;..      
3587c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
3587d 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54  assert( p->op==T
3587e 4b 5f 55 4e 49 4f 4e 20 29 3b 0d 0a 20 20 20 20  K_UNION );..    
3587f 20 20 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69      op = SRT_Uni
35880 6f 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  on;..      }..  
35881 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
35882 30 3b 0d 0a 20 20 20 20 20 20 70 4c 69 6d 69 74  0;..      pLimit
35883 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0d 0a 20   = p->pLimit;.. 
35884 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
35885 20 30 3b 0d 0a 20 20 20 20 20 20 70 4f 66 66 73   0;..      pOffs
35886 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b  et = p->pOffset;
35887 0d 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73  ..      p->pOffs
35888 65 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 75  et = 0;..      u
35889 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20 3d  niondest.eDest =
3588a 20 6f 70 3b 0d 0a 20 20 20 20 20 20 65 78 70 6c   op;..      expl
3588b 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53  ainSetInteger(iS
3588c 75 62 32 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65  ub2, pParse->iNe
3588d 78 74 53 65 6c 65 63 74 49 64 29 3b 0d 0a 20 20  xtSelectId);..  
3588e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3588f 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
35890 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0d 0a  , &uniondest);..
35891 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
35892 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
35893 0d 0a 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79  ..      /* Query
35894 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 6e 20 73   flattening in s
35895 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 6d  qlite3Select() m
35896 69 67 68 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70  ight refill p->p
35897 4f 72 64 65 72 42 79 2e 0d 0a 20 20 20 20 20 20  OrderBy...      
35898 2a 2a 20 42 65 20 73 75 72 65 20 74 6f 20 64 65  ** Be sure to de
35899 6c 65 74 65 20 70 2d 3e 70 4f 72 64 65 72 42 79  lete p->pOrderBy
3589a 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 74 6f 20  , therefore, to 
3589b 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c  avoid a memory l
3589c 65 61 6b 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 73  eak. */..      s
3589d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
3589e 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64  lete(db, p->pOrd
3589f 65 72 42 79 29 3b 0d 0a 20 20 20 20 20 20 70 44  erBy);..      pD
358a0 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f  elete = p->pPrio
358a1 72 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72  r;..      p->pPr
358a2 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0d 0a 20  ior = pPrior;.. 
358a3 20 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79       p->pOrderBy
358a4 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66 28   = 0;..      if(
358a5 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   p->op==TK_UNION
358a6 20 29 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77   ) p->nSelectRow
358a7 20 2b 3d 20 70 50 72 69 6f 72 2d 3e 6e 53 65 6c   += pPrior->nSel
358a8 65 63 74 52 6f 77 3b 0d 0a 20 20 20 20 20 20 73  ectRow;..      s
358a9 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
358aa 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
358ab 0d 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ..      p->pLimi
358ac 74 20 3d 20 70 4c 69 6d 69 74 3b 0d 0a 20 20 20  t = pLimit;..   
358ad 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
358ae 70 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 20 20  pOffset;..      
358af 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0d 0a  p->iLimit = 0;..
358b0 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74        p->iOffset
358b1 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f   = 0;....      /
358b2 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61  * Convert the da
358b3 74 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72  ta in the tempor
358b4 61 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  ary table into w
358b5 68 61 74 65 76 65 72 20 66 6f 72 6d 0d 0a 20 20  hatever form..  
358b6 20 20 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61      ** it is tha
358b7 74 20 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e  t we currently n
358b8 65 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  eed...      */..
358b9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 6e        assert( un
358ba 69 6f 6e 54 61 62 3d 3d 64 65 73 74 2e 69 50 61  ionTab==dest.iPa
358bb 72 6d 20 7c 7c 20 64 65 73 74 2e 65 44 65 73 74  rm || dest.eDest
358bc 21 3d 70 72 69 6f 72 4f 70 20 29 3b 0d 0a 20 20  !=priorOp );..  
358bd 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44 65      if( dest.eDe
358be 73 74 21 3d 70 72 69 6f 72 4f 70 20 29 7b 0d 0a  st!=priorOp ){..
358bf 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e          int iCon
358c0 74 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72  t, iBreak, iStar
358c1 74 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  t;..        asse
358c2 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
358c3 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 65  ..        if( de
358c4 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  st.eDest==SRT_Ou
358c5 74 70 75 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  tput ){..       
358c6 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73     Select *pFirs
358c7 74 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20  t = p;..        
358c8 20 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d    while( pFirst-
358c9 3e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74  >pPrior ) pFirst
358ca 20 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f   = pFirst->pPrio
358cb 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 65  r;..          ge
358cc 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
358cd 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69  s(pParse, 0, pFi
358ce 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a 20  rst->pEList);.. 
358cf 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
358d0 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74    iBreak = sqlit
358d1 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
358d2 76 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 43 6f  v);..        iCo
358d3 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
358d4 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20  MakeLabel(v);.. 
358d5 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69         computeLi
358d6 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61  mitRegisters(pPa
358d7 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b  rse, p, iBreak);
358d8 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
358d9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
358da 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54  P_Rewind, unionT
358db 61 62 2c 20 69 42 72 65 61 6b 29 3b 0d 0a 20 20  ab, iBreak);..  
358dc 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73        iStart = s
358dd 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
358de 74 41 64 64 72 28 76 29 3b 0d 0a 20 20 20 20 20  tAddr(v);..     
358df 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
358e0 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
358e1 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61  >pEList, unionTa
358e2 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  b, p->pEList->nE
358e3 78 70 72 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  xpr,..          
358e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
358e5 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e   -1, &dest, iCon
358e6 74 2c 20 69 42 72 65 61 6b 29 3b 0d 0a 20 20 20  t, iBreak);..   
358e7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
358e8 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
358e9 69 43 6f 6e 74 29 3b 0d 0a 20 20 20 20 20 20 20  iCont);..       
358ea 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
358eb 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75  p2(v, OP_Next, u
358ec 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61 72 74 29  nionTab, iStart)
358ed 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
358ee 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
358ef 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0d 0a  el(v, iBreak);..
358f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
358f1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
358f2 43 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c  Close, unionTab,
358f3 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20   0);..      }.. 
358f4 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
358f5 20 7d 0d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   }..    default:
358f6 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d   assert( p->op==
358f7 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 3b 20  TK_INTERSECT ); 
358f8 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 74 61 62  {..      int tab
358f9 31 2c 20 74 61 62 32 3b 0d 0a 20 20 20 20 20 20  1, tab2;..      
358fa 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61  int iCont, iBrea
358fb 6b 2c 20 69 53 74 61 72 74 3b 0d 0a 20 20 20 20  k, iStart;..    
358fc 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20    Expr *pLimit, 
358fd 2a 70 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 20  *pOffset;..     
358fe 20 69 6e 74 20 61 64 64 72 3b 0d 0a 20 20 20 20   int addr;..    
358ff 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e 74    SelectDest int
35900 65 72 73 65 63 74 64 65 73 74 3b 0d 0a 20 20 20  ersectdest;..   
35901 20 20 20 69 6e 74 20 72 31 3b 0d 0a 0d 0a 20 20     int r1;....  
35902 20 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54      /* INTERSECT
35903 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72   is different fr
35904 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73 69  om the others si
35905 6e 63 65 20 69 74 20 72 65 71 75 69 72 65 73 0d  nce it requires.
35906 0a 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65  .      ** two te
35907 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
35908 20 48 65 6e 63 65 20 69 74 20 68 61 73 20 69 74   Hence it has it
35909 73 20 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67  s own case.  Beg
3590a 69 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  in..      ** by 
3590b 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 74  allocating the t
3590c 61 62 6c 65 73 20 77 65 20 77 69 6c 6c 20 6e 65  ables we will ne
3590d 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  ed...      */.. 
3590e 20 20 20 20 20 74 61 62 31 20 3d 20 70 50 61 72       tab1 = pPar
3590f 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20 20 20  se->nTab++;..   
35910 20 20 20 74 61 62 32 20 3d 20 70 50 61 72 73 65     tab2 = pParse
35911 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20 20 20 20 20  ->nTab++;..     
35912 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64   assert( p->pOrd
35913 65 72 42 79 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20  erBy==0 );....  
35914 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
35915 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
35916 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
35917 2c 20 74 61 62 31 2c 20 30 29 3b 0d 0a 20 20 20  , tab1, 0);..   
35918 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64     assert( p->ad
35919 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d  drOpenEphm[0] ==
3591a 20 2d 31 20 29 3b 0d 0a 20 20 20 20 20 20 70 2d   -1 );..      p-
3591b 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
3591c 20 3d 20 61 64 64 72 3b 0d 0a 20 20 20 20 20 20   = addr;..      
3591d 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73  p->pRightmost->s
3591e 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73  elFlags |= SF_Us
3591f 65 73 45 70 68 65 6d 65 72 61 6c 3b 0d 0a 20 20  esEphemeral;..  
35920 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
35921 45 4c 69 73 74 20 29 3b 0d 0a 0d 0a 20 20 20 20  EList );....    
35922 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
35923 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66  LECTs to our lef
35924 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79  t into temporary
35925 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0d 0a   table "tab1"...
35926 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
35927 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
35928 74 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74  tInit(&intersect
35929 64 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c  dest, SRT_Union,
3592a 20 74 61 62 31 29 3b 0d 0a 20 20 20 20 20 20 65   tab1);..      e
3592b 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72  xplainSetInteger
3592c 28 69 53 75 62 31 2c 20 70 50 61 72 73 65 2d 3e  (iSub1, pParse->
3592d 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0d  iNextSelectId);.
3592e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3592f 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
35930 2c 20 70 50 72 69 6f 72 2c 20 26 69 6e 74 65 72  , pPrior, &inter
35931 73 65 63 74 64 65 73 74 29 3b 0d 0a 20 20 20 20  sectdest);..    
35932 20 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20    if( rc ){..   
35933 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
35934 73 65 6c 65 63 74 5f 65 6e 64 3b 0d 0a 20 20 20  select_end;..   
35935 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
35936 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
35937 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65  t SELECT into te
35938 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74  mporary table "t
35939 61 62 32 22 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ab2"..      */..
3593a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
3593b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3593c 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
3593d 61 6c 2c 20 74 61 62 32 2c 20 30 29 3b 0d 0a 20  al, tab2, 0);.. 
3593e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
3593f 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20  addrOpenEphm[1] 
35940 3d 3d 20 2d 31 20 29 3b 0d 0a 20 20 20 20 20 20  == -1 );..      
35941 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
35942 31 5d 20 3d 20 61 64 64 72 3b 0d 0a 20 20 20 20  1] = addr;..    
35943 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b    p->pPrior = 0;
35944 0d 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 20 3d  ..      pLimit =
35945 20 70 2d 3e 70 4c 69 6d 69 74 3b 0d 0a 20 20 20   p->pLimit;..   
35946 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30     p->pLimit = 0
35947 3b 0d 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74  ;..      pOffset
35948 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0d 0a   = p->pOffset;..
35949 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74        p->pOffset
3594a 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 6e 74   = 0;..      int
3594b 65 72 73 65 63 74 64 65 73 74 2e 69 50 61 72 6d  ersectdest.iParm
3594c 20 3d 20 74 61 62 32 3b 0d 0a 20 20 20 20 20 20   = tab2;..      
3594d 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65  explainSetIntege
3594e 72 28 69 53 75 62 32 2c 20 70 50 61 72 73 65 2d  r(iSub2, pParse-
3594f 3e 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b  >iNextSelectId);
35950 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ..      rc = sql
35951 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
35952 65 2c 20 70 2c 20 26 69 6e 74 65 72 73 65 63 74  e, p, &intersect
35953 64 65 73 74 29 3b 0d 0a 20 20 20 20 20 20 74 65  dest);..      te
35954 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49  stcase( rc!=SQLI
35955 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 20 20  TE_OK );..      
35956 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72  pDelete = p->pPr
35957 69 6f 72 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 70  ior;..      p->p
35958 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0d  Prior = pPrior;.
35959 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 53  .      if( p->nS
3595a 65 6c 65 63 74 52 6f 77 3e 70 50 72 69 6f 72 2d  electRow>pPrior-
3595b 3e 6e 53 65 6c 65 63 74 52 6f 77 20 29 20 70 2d  >nSelectRow ) p-
3595c 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 70 50  >nSelectRow = pP
3595d 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77  rior->nSelectRow
3595e 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3595f 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
35960 2d 3e 70 4c 69 6d 69 74 29 3b 0d 0a 20 20 20 20  ->pLimit);..    
35961 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c    p->pLimit = pL
35962 69 6d 69 74 3b 0d 0a 20 20 20 20 20 20 70 2d 3e  imit;..      p->
35963 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65  pOffset = pOffse
35964 74 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 47  t;....      /* G
35965 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
35966 74 61 6b 65 20 74 68 65 20 69 6e 74 65 72 73 65  take the interse
35967 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f  ction of the two
35968 20 74 65 6d 70 6f 72 61 72 79 0d 0a 20 20 20 20   temporary..    
35969 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0d 0a 20 20    ** tables...  
3596a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73      */..      as
3596b 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
3596c 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 64 65  );..      if( de
3596d 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  st.eDest==SRT_Ou
3596e 74 70 75 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  tput ){..       
3596f 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
35970 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20 77 68  = p;..        wh
35971 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72  ile( pFirst->pPr
35972 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70  ior ) pFirst = p
35973 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0d 0a  First->pPrior;..
35974 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65          generate
35975 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
35976 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70  se, 0, pFirst->p
35977 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 7d  EList);..      }
35978 0d 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d  ..      iBreak =
35979 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
3597a 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20 20 20 20  Label(v);..     
3597b 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33   iCont = sqlite3
3597c 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
3597d 3b 0d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65  ;..      compute
3597e 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
3597f 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b  Parse, p, iBreak
35980 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
35981 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35982 50 5f 52 65 77 69 6e 64 2c 20 74 61 62 31 2c 20  P_Rewind, tab1, 
35983 69 42 72 65 61 6b 29 3b 0d 0a 20 20 20 20 20 20  iBreak);..      
35984 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
35985 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
35986 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20  .      iStart = 
35987 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35988 32 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20  2(v, OP_RowKey, 
35989 74 61 62 31 2c 20 72 31 29 3b 0d 0a 20 20 20 20  tab1, r1);..    
3598a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3598b 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4e 6f 74  Op4Int(v, OP_Not
3598c 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20 69 43 6f  Found, tab2, iCo
3598d 6e 74 2c 20 72 31 2c 20 30 29 3b 0d 0a 20 20 20  nt, r1, 0);..   
3598e 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
3598f 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
35990 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 73 65 6c   r1);..      sel
35991 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61  ectInnerLoop(pPa
35992 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73  rse, p, p->pELis
35993 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70 45 4c 69  t, tab1, p->pELi
35994 73 74 2d 3e 6e 45 78 70 72 2c 0d 0a 20 20 20 20  st->nExpr,..    
35995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35996 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20    0, -1, &dest, 
35997 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0d  iCont, iBreak);.
35998 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
35999 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
3599a 2c 20 69 43 6f 6e 74 29 3b 0d 0a 20 20 20 20 20  , iCont);..     
3599b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3599c 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74  p2(v, OP_Next, t
3599d 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0d 0a 20  ab1, iStart);.. 
3599e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3599f 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
359a0 69 42 72 65 61 6b 29 3b 0d 0a 20 20 20 20 20 20  iBreak);..      
359a1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
359a2 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74  2(v, OP_Close, t
359a3 61 62 32 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  ab2, 0);..      
359a4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
359a5 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74  2(v, OP_Close, t
359a6 61 62 31 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  ab1, 0);..      
359a7 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
359a8 20 7d 0d 0a 0d 0a 20 20 65 78 70 6c 61 69 6e 43   }....  explainC
359a9 6f 6d 70 6f 73 69 74 65 28 70 50 61 72 73 65 2c  omposite(pParse,
359aa 20 70 2d 3e 6f 70 2c 20 69 53 75 62 31 2c 20 69   p->op, iSub1, i
359ab 53 75 62 32 2c 20 70 2d 3e 6f 70 21 3d 54 4b 5f  Sub2, p->op!=TK_
359ac 41 4c 4c 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 6f  ALL);....  /* Co
359ad 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20  mpute collating 
359ae 73 65 71 75 65 6e 63 65 73 20 75 73 65 64 20 62  sequences used b
359af 79 20 0d 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61  y ..  ** tempora
359b0 72 79 20 74 61 62 6c 65 73 20 6e 65 65 64 65 64  ry tables needed
359b1 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
359b2 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
359b3 74 2e 0d 0a 20 20 2a 2a 20 41 74 74 61 63 68 20  t...  ** Attach 
359b4 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
359b5 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 74 65 6d  cture to all tem
359b6 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0d 0a  porary tables...
359b7 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69 73 20    **..  ** This 
359b8 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62  section is run b
359b9 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  y the right-most
359ba 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
359bb 74 20 6f 6e 6c 79 2e 0d 0a 20 20 2a 2a 20 53 45  t only...  ** SE
359bc 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
359bd 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61  to the left alwa
359be 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  ys skip this par
359bf 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f  t.  The right-mo
359c0 73 74 0d 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  st..  ** SELECT 
359c1 6d 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20  might also skip 
359c2 74 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20  this part if it 
359c3 68 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20  has no ORDER BY 
359c4 63 6c 61 75 73 65 20 61 6e 64 0d 0a 20 20 2a 2a  clause and..  **
359c5 20 6e 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20   no temp tables 
359c6 61 72 65 20 72 65 71 75 69 72 65 64 2e 0d 0a 20  are required... 
359c7 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 73 65   */..  if( p->se
359c8 6c 46 6c 61 67 73 20 26 20 53 46 5f 55 73 65 73  lFlags & SF_Uses
359c9 45 70 68 65 6d 65 72 61 6c 20 29 7b 0d 0a 20 20  Ephemeral ){..  
359ca 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
359cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359cc 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
359cd 2a 2f 0d 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20  */..    KeyInfo 
359ce 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 20  *pKeyInfo;      
359cf 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
359d0 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
359d1 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
359d2 2f 0d 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  /..    Select *p
359d3 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  Loop;           
359d4 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
359d5 69 6e 67 20 74 68 72 6f 75 67 68 20 53 45 4c 45  ing through SELE
359d6 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  CT statements */
359d7 0d 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 2a  ..    CollSeq **
359d8 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  apColl;         
359d9 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
359da 6e 67 20 74 68 72 6f 75 67 68 20 70 4b 65 79 49  ng through pKeyI
359db 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f 0d  nfo->aColl[] */.
359dc 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  .    int nCol;  
359dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359de 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
359df 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75 6c  columns in resul
359e0 74 20 73 65 74 20 2a 2f 0d 0a 0d 0a 20 20 20 20  t set */....    
359e1 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
359e2 74 6d 6f 73 74 3d 3d 70 20 29 3b 0d 0a 20 20 20  tmost==p );..   
359e3 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73   nCol = p->pELis
359e4 74 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20 20 70  t->nExpr;..    p
359e5 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65  KeyInfo = sqlite
359e6 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
359e7 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
359e8 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
359e9 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c  (*pKeyInfo)+nCol
359ea 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
359eb 2a 29 20 2b 20 31 29 29 3b 0d 0a 20 20 20 20 69  *) + 1));..    i
359ec 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0d  f( !pKeyInfo ){.
359ed 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
359ee 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20  TE_NOMEM;..     
359ef 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
359f0 63 74 5f 65 6e 64 3b 0d 0a 20 20 20 20 7d 0d 0a  ct_end;..    }..
359f1 0d 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e  ..    pKeyInfo->
359f2 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0d 0a  enc = ENC(db);..
359f3 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46      pKeyInfo->nF
359f4 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c  ield = (u16)nCol
359f5 3b 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30  ;....    for(i=0
359f6 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79 49 6e 66  , apColl=pKeyInf
359f7 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e 43 6f 6c  o->aColl; i<nCol
359f8 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c 2b 2b 29  ; i++, apColl++)
359f9 7b 0d 0a 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c  {..      *apColl
359fa 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
359fb 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c  llSeq(pParse, p,
359fc 20 69 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20   i);..      if( 
359fd 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0d 0a 20  0==*apColl ){.. 
359fe 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d         *apColl =
359ff 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0d   db->pDfltColl;.
35a00 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
35a01 0a 0d 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70  ...    for(pLoop
35a02 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70  =p; pLoop; pLoop
35a03 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b  =pLoop->pPrior){
35a04 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ..      for(i=0;
35a05 20 69 3c 32 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20   i<2; i++){..   
35a06 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20       int addr = 
35a07 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
35a08 70 68 6d 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 20  phm[i];..       
35a09 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0d 0a   if( addr<0 ){..
35a0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
35a0b 5b 30 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68  [0] is unused th
35a0c 65 6e 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75  en [1] is also u
35a0d 6e 75 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61  nused.  So we ca
35a0e 6e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  n..          ** 
35a0f 61 6c 77 61 79 73 20 73 61 66 65 6c 79 20 61 62  always safely ab
35a10 6f 72 74 20 61 73 20 73 6f 6f 6e 20 61 73 20 74  ort as soon as t
35a11 68 65 20 66 69 72 73 74 20 75 6e 75 73 65 64 20  he first unused 
35a12 73 6c 6f 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f  slot is found */
35a13 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ..          asse
35a14 72 74 28 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f  rt( pLoop->addrO
35a15 70 65 6e 45 70 68 6d 5b 31 5d 3c 30 20 29 3b 0d  penEphm[1]<0 );.
35a16 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
35a17 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
35a18 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
35a19 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64  eChangeP2(v, add
35a1a 72 2c 20 6e 43 6f 6c 29 3b 0d 0a 20 20 20 20 20  r, nCol);..     
35a1b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
35a1c 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20  angeP4(v, addr, 
35a1d 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
35a1e 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0d 0a 20   P4_KEYINFO);.. 
35a1f 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 64         pLoop->ad
35a20 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d 20  drOpenEphm[i] = 
35a21 2d 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  -1;..      }..  
35a22 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
35a23 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49  DbFree(db, pKeyI
35a24 6e 66 6f 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 6d 75  nfo);..  }....mu
35a25 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a 0d  lti_select_end:.
35a26 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d  .  pDest->iMem =
35a27 20 64 65 73 74 2e 69 4d 65 6d 3b 0d 0a 20 20 70   dest.iMem;..  p
35a28 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73  Dest->nMem = des
35a29 74 2e 6e 4d 65 6d 3b 0d 0a 20 20 73 71 6c 69 74  t.nMem;..  sqlit
35a2a 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
35a2b 62 2c 20 70 44 65 6c 65 74 65 29 3b 0d 0a 20 20  b, pDelete);..  
35a2c 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 23  return rc;..}..#
35a2d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
35a2e 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
35a2f 4c 45 43 54 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  LECT */..../*..*
35a30 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74 70 75 74  * Code an output
35a31 20 73 75 62 72 6f 75 74 69 6e 65 20 66 6f 72 20   subroutine for 
35a32 61 20 63 6f 72 6f 75 74 69 6e 65 20 69 6d 70 6c  a coroutine impl
35a33 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 0d  ementation of a.
35a34 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d  .** SELECT statm
35a35 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ent...**..** The
35a36 20 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70   data to be outp
35a37 75 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20  ut is contained 
35a38 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54  in pIn->iMem.  T
35a39 68 65 72 65 20 61 72 65 0d 0a 2a 2a 20 70 49 6e  here are..** pIn
35a3a 2d 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74  ->nMem columns t
35a3b 6f 20 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44  o be output.  pD
35a3c 65 73 74 20 69 73 20 77 68 65 72 65 20 74 68 65  est is where the
35a3d 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 0d 0a   output should..
35a3e 2a 2a 20 62 65 20 73 65 6e 74 2e 0d 0a 2a 2a 0d  ** be sent...**.
35a3f 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69 73  .** regReturn is
35a40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
35a41 68 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  he register hold
35a42 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74 69  ing the subrouti
35a43 6e 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64  ne..** return ad
35a44 64 72 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  dress...**..** I
35a45 66 20 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e  f regPrev>0 then
35a46 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 74   it is the first
35a47 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 20 76   register in a v
35a48 65 63 74 6f 72 20 74 68 61 74 0d 0a 2a 2a 20 72  ector that..** r
35a49 65 63 6f 72 64 73 20 74 68 65 20 70 72 65 76 69  ecords the previ
35a4a 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d  ous output.  mem
35a4b 5b 72 65 67 50 72 65 76 5d 20 69 73 20 61 20 66  [regPrev] is a f
35a4c 6c 61 67 20 74 68 61 74 20 69 73 20 66 61 6c 73  lag that is fals
35a4d 65 0d 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 68  e..** if there h
35a4e 61 73 20 62 65 65 6e 20 6e 6f 20 70 72 65 76 69  as been no previ
35a4f 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 49 66 20  ous output.  If 
35a50 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 63  regPrev>0 then c
35a51 6f 64 65 20 69 73 0d 0a 2a 2a 20 67 65 6e 65 72  ode is..** gener
35a52 61 74 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  ated to suppress
35a53 20 64 75 70 6c 69 63 61 74 65 73 2e 20 20 70 4b   duplicates.  pK
35a54 65 79 49 6e 66 6f 20 69 73 20 75 73 65 64 20 66  eyInfo is used f
35a55 6f 72 20 63 6f 6d 70 61 72 69 6e 67 0d 0a 2a 2a  or comparing..**
35a56 20 6b 65 79 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   keys...**..** I
35a57 66 20 74 68 65 20 4c 49 4d 49 54 20 66 6f 75 6e  f the LIMIT foun
35a58 64 20 69 6e 20 70 2d 3e 69 4c 69 6d 69 74 20 69  d in p->iLimit i
35a59 73 20 72 65 61 63 68 65 64 2c 20 6a 75 6d 70 20  s reached, jump 
35a5a 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 0d 0a  immediately to..
35a5b 2a 2a 20 69 42 72 65 61 6b 2e 0d 0a 2a 2f 0d 0a  ** iBreak...*/..
35a5c 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65 72  static int gener
35a5d 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
35a5e 69 6e 65 28 0d 0a 20 20 50 61 72 73 65 20 2a 70  ine(..  Parse *p
35a5f 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
35a60 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
35a61 78 74 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20  xt */..  Select 
35a62 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
35a63 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
35a64 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 53  tatement */..  S
35a65 65 6c 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20  electDest *pIn, 
35a66 20 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74         /* Corout
35a67 69 6e 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61  ine supplying da
35a68 74 61 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 44  ta */..  SelectD
35a69 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20  est *pDest,     
35a6a 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 73 65 6e   /* Where to sen
35a6b 64 20 74 68 65 20 64 61 74 61 20 2a 2f 0d 0a 20  d the data */.. 
35a6c 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 2c 20   int regReturn, 
35a6d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
35a6e 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72  return address r
35a6f 65 67 69 73 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  egister */..  in
35a70 74 20 72 65 67 50 72 65 76 2c 20 20 20 20 20 20  t regPrev,      
35a71 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75        /* Previou
35a72 73 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65  s result registe
35a73 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65 73  r.  No uniquenes
35a74 73 20 69 66 20 30 20 2a 2f 0d 0a 20 20 4b 65 79  s if 0 */..  Key
35a75 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20  Info *pKeyInfo, 
35a76 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70       /* For comp
35a77 61 72 69 6e 67 20 77 69 74 68 20 70 72 65 76 69  aring with previ
35a78 6f 75 73 20 65 6e 74 72 79 20 2a 2f 0d 0a 20 20  ous entry */..  
35a79 69 6e 74 20 70 34 74 79 70 65 2c 20 20 20 20 20  int p4type,     
35a7a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
35a7b 34 20 74 79 70 65 20 66 6f 72 20 70 4b 65 79 49  4 type for pKeyI
35a7c 6e 66 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 42  nfo */..  int iB
35a7d 72 65 61 6b 20 20 20 20 20 20 20 20 20 20 20 20  reak            
35a7e 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
35a7f 66 20 77 65 20 68 69 74 20 74 68 65 20 4c 49 4d  f we hit the LIM
35a80 49 54 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 56 64 62  IT */..){..  Vdb
35a81 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
35a82 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69 43 6f  Vdbe;..  int iCo
35a83 6e 74 69 6e 75 65 3b 0d 0a 20 20 69 6e 74 20 61  ntinue;..  int a
35a84 64 64 72 3b 0d 0a 0d 0a 20 20 61 64 64 72 20 3d  ddr;....  addr =
35a85 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
35a86 65 6e 74 41 64 64 72 28 76 29 3b 0d 0a 20 20 69  entAddr(v);..  i
35a87 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74  Continue = sqlit
35a88 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
35a89 76 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 75 70 70  v);....  /* Supp
35a8a 72 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20  ress duplicates 
35a8b 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50  for UNION, EXCEP
35a8c 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54  T, and INTERSECT
35a8d 20 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72   ..  */..  if( r
35a8e 65 67 50 72 65 76 20 29 7b 0d 0a 20 20 20 20 69  egPrev ){..    i
35a8f 6e 74 20 6a 31 2c 20 6a 32 3b 0d 0a 20 20 20 20  nt j1, j2;..    
35a90 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
35a91 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e  AddOp1(v, OP_IfN
35a92 6f 74 2c 20 72 65 67 50 72 65 76 29 3b 0d 0a 20  ot, regPrev);.. 
35a93 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56     j2 = sqlite3V
35a94 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
35a95 43 6f 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69 4d  Compare, pIn->iM
35a96 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70  em, regPrev+1, p
35a97 49 6e 2d 3e 6e 4d 65 6d 2c 0d 0a 20 20 20 20 20  In->nMem,..     
35a98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a99 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
35a9a 70 4b 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65  pKeyInfo, p4type
35a9b 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
35a9c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
35a9d 4a 75 6d 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e  Jump, j2+2, iCon
35a9e 74 69 6e 75 65 2c 20 6a 32 2b 32 29 3b 0d 0a 20  tinue, j2+2);.. 
35a9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
35aa0 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0d 0a  mpHere(v, j1);..
35aa1 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
35aa2 6f 64 65 43 6f 70 79 28 70 50 61 72 73 65 2c 20  odeCopy(pParse, 
35aa3 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65 67 50 72  pIn->iMem, regPr
35aa4 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  ev+1, pIn->nMem)
35aa5 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
35aa6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
35aa7 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 50 72  nteger, 1, regPr
35aa8 65 76 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  ev);..  }..  if(
35aa9 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
35aaa 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
35aab 72 6e 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 75  rn 0;....  /* Su
35aac 70 70 72 65 73 73 20 74 68 65 20 74 68 65 20 66  ppress the the f
35aad 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e 74 72  irst OFFSET entr
35aae 69 65 73 20 69 66 20 74 68 65 72 65 20 69 73 20  ies if there is 
35aaf 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  an OFFSET clause
35ab0 0d 0a 20 20 2a 2f 0d 0a 20 20 63 6f 64 65 4f 66  ..  */..  codeOf
35ab1 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
35ab2 69 6e 75 65 29 3b 0d 0a 0d 0a 20 20 73 77 69 74  inue);....  swit
35ab3 63 68 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  ch( pDest->eDest
35ab4 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 53 74 6f 72   ){..    /* Stor
35ab5 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73 20  e the result as 
35ab6 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e 69  data using a uni
35ab7 71 75 65 20 6b 65 79 2e 0d 0a 20 20 20 20 2a 2f  que key...    */
35ab8 0d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54  ..    case SRT_T
35ab9 61 62 6c 65 3a 0d 0a 20 20 20 20 63 61 73 65 20  able:..    case 
35aba 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0d  SRT_EphemTab: {.
35abb 0a 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20  .      int r1 = 
35abc 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
35abd 67 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20  g(pParse);..    
35abe 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
35abf 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
35ac0 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 74 65 73  rse);..      tes
35ac1 74 63 61 73 65 28 20 70 44 65 73 74 2d 3e 65 44  tcase( pDest->eD
35ac2 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29  est==SRT_Table )
35ac3 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
35ac4 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  e( pDest->eDest=
35ac5 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b  =SRT_EphemTab );
35ac6 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
35ac7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
35ac8 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
35ac9 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  >iMem, pIn->nMem
35aca 2c 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 73 71  , r1);..      sq
35acb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35acc 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
35acd 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 32  pDest->iParm, r2
35ace 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
35acf 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
35ad0 50 5f 49 6e 73 65 72 74 2c 20 70 44 65 73 74 2d  P_Insert, pDest-
35ad1 3e 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b  >iParm, r1, r2);
35ad2 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
35ad3 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
35ad4 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0d 0a  PFLAG_APPEND);..
35ad5 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
35ad6 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
35ad7 73 65 2c 20 72 32 29 3b 0d 0a 20 20 20 20 20 20  se, r2);..      
35ad8 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
35ad9 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
35ada 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
35adb 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64  ..    }....#ifnd
35adc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
35add 55 42 51 55 45 52 59 0d 0a 20 20 20 20 2f 2a 20  UBQUERY..    /* 
35ade 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74 69  If we are creati
35adf 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20  ng a set for an 
35ae0 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54  "expr IN (SELECT
35ae1 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74   ...)" construct
35ae2 2c 0d 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  ,..    ** then t
35ae3 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61  here should be a
35ae4 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20   single item on 
35ae5 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74  the stack.  Writ
35ae6 65 20 74 68 69 73 0d 0a 20 20 20 20 2a 2a 20 69  e this..    ** i
35ae7 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74  tem into the set
35ae8 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75   table with bogu
35ae9 73 20 64 61 74 61 2e 0d 0a 20 20 20 20 2a 2f 0d  s data...    */.
35aea 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65  .    case SRT_Se
35aeb 74 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  t: {..      int 
35aec 72 31 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72  r1;..      asser
35aed 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20  t( pIn->nMem==1 
35aee 29 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 61 66 66  );..      p->aff
35aef 69 6e 69 74 79 20 3d 20 0d 0a 20 20 20 20 20 20  inity = ..      
35af0 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72     sqlite3Compar
35af1 65 41 66 66 69 6e 69 74 79 28 70 2d 3e 70 45 4c  eAffinity(p->pEL
35af2 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c  ist->a[0].pExpr,
35af3 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79   pDest->affinity
35af4 29 3b 0d 0a 20 20 20 20 20 20 72 31 20 3d 20 73  );..      r1 = s
35af5 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
35af6 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
35af7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
35af8 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p4(v, OP_MakeRec
35af9 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20  ord, pIn->iMem, 
35afa 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e  1, r1, &p->affin
35afb 69 74 79 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20  ity, 1);..      
35afc 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
35afd 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
35afe 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
35aff 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  , 1);..      sql
35b00 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
35b01 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
35b02 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31  pDest->iParm, r1
35b03 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
35b04 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
35b05 70 50 61 72 73 65 2c 20 72 31 29 3b 0d 0a 20 20  pParse, r1);..  
35b06 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
35b07 7d 0d 0a 0d 0a 23 69 66 20 30 20 20 2f 2a 20 4e  }....#if 0  /* N
35b08 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  ever occurs on a
35b09 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79  n ORDER BY query
35b0a 20 2a 2f 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61   */..    /* If a
35b0b 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20  ny row exist in 
35b0c 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20  the result set, 
35b0d 72 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74  record that fact
35b0e 20 61 6e 64 20 61 62 6f 72 74 2e 0d 0a 20 20 20   and abort...   
35b0f 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 52   */..    case SR
35b10 54 5f 45 78 69 73 74 73 3a 20 7b 0d 0a 20 20 20  T_Exists: {..   
35b11 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
35b12 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
35b13 65 72 2c 20 31 2c 20 70 44 65 73 74 2d 3e 69 50  er, 1, pDest->iP
35b14 61 72 6d 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20  arm);..      /* 
35b15 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
35b16 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20   will terminate 
35b17 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20  the loop for us 
35b18 2a 2f 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  */..      break;
35b19 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  ..    }..#endif.
35b1a 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ...    /* If thi
35b1b 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73 65  s is a scalar se
35b1c 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61 72  lect that is par
35b1d 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
35b1e 6f 6e 2c 20 74 68 65 6e 0d 0a 20 20 20 20 2a 2a  on, then..    **
35b1f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
35b20 74 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70  ts in the approp
35b21 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c  riate memory cel
35b22 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0d  l and break out.
35b23 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73  .    ** of the s
35b24 63 61 6e 20 6c 6f 6f 70 2e 0d 0a 20 20 20 20 2a  can loop...    *
35b25 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  /..    case SRT_
35b26 4d 65 6d 3a 20 7b 0d 0a 20 20 20 20 20 20 61 73  Mem: {..      as
35b27 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d  sert( pIn->nMem=
35b28 3d 31 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  =1 );..      sql
35b29 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
35b2a 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
35b2b 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  em, pDest->iParm
35b2c 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20  , 1);..      /* 
35b2d 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
35b2e 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f   will jump out o
35b2f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  f the loop for u
35b30 73 20 2a 2f 0d 0a 20 20 20 20 20 20 62 72 65 61  s */..      brea
35b31 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69  k;..    }..#endi
35b32 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
35b33 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
35b34 59 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54  Y */....    /* T
35b35 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 73  he results are s
35b36 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65  tored in a seque
35b37 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nce of registers
35b38 0d 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e  ..    ** startin
35b39 67 20 61 74 20 70 44 65 73 74 2d 3e 69 4d 65 6d  g at pDest->iMem
35b3a 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 2d 72  .  Then the co-r
35b3b 6f 75 74 69 6e 65 20 79 69 65 6c 64 73 2e 0d 0a  outine yields...
35b3c 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65      */..    case
35b3d 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 20   SRT_Coroutine: 
35b3e 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  {..      if( pDe
35b3f 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0d 0a  st->iMem==0 ){..
35b40 20 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 69          pDest->i
35b41 4d 65 6d 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Mem = sqlite3Get
35b42 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
35b43 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0d 0a 20  , pIn->nMem);.. 
35b44 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d         pDest->nM
35b45 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d 65 6d 3b 0d  em = pIn->nMem;.
35b46 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
35b47 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
35b48 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  ove(pParse, pIn-
35b49 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69 4d  >iMem, pDest->iM
35b4a 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 29  em, pDest->nMem)
35b4b 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
35b4c 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
35b4d 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69  _Yield, pDest->i
35b4e 50 61 72 6d 29 3b 0d 0a 20 20 20 20 20 20 62 72  Parm);..      br
35b4f 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  eak;..    }.... 
35b50 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66     /* If none of
35b51 20 74 68 65 20 61 62 6f 76 65 2c 20 74 68 65 6e   the above, then
35b52 20 74 68 65 20 72 65 73 75 6c 74 20 64 65 73 74   the result dest
35b53 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 0d  ination must be.
35b54 0a 20 20 20 20 2a 2a 20 53 52 54 5f 4f 75 74 70  .    ** SRT_Outp
35b55 75 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ut.  This routin
35b56 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
35b57 64 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72  d with any other
35b58 0d 0a 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61  ..    ** destina
35b59 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20  tion other than 
35b5a 74 68 65 20 6f 6e 65 73 20 68 61 6e 64 6c 65 64  the ones handled
35b5b 20 61 62 6f 76 65 20 6f 72 20 53 52 54 5f 4f 75   above or SRT_Ou
35b5c 74 70 75 74 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  tput...    **.. 
35b5d 20 20 20 2a 2a 20 46 6f 72 20 53 52 54 5f 4f 75     ** For SRT_Ou
35b5e 74 70 75 74 2c 20 72 65 73 75 6c 74 73 20 61 72  tput, results ar
35b5f 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 65  e stored in a se
35b60 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73 74  quence of regist
35b61 65 72 73 2e 20 20 0d 0a 20 20 20 20 2a 2a 20 54  ers.  ..    ** T
35b62 68 65 6e 20 74 68 65 20 4f 50 5f 52 65 73 75 6c  hen the OP_Resul
35b63 74 52 6f 77 20 6f 70 63 6f 64 65 20 69 73 20 75  tRow opcode is u
35b64 73 65 64 20 74 6f 20 63 61 75 73 65 20 73 71 6c  sed to cause sql
35b65 69 74 65 33 5f 73 74 65 70 28 29 20 74 6f 0d 0a  ite3_step() to..
35b66 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68      ** return th
35b67 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 72 65  e next row of re
35b68 73 75 6c 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  sult...    */.. 
35b69 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 20     default: {.. 
35b6a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44 65       assert( pDe
35b6b 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  st->eDest==SRT_O
35b6c 75 74 70 75 74 20 29 3b 0d 0a 20 20 20 20 20 20  utput );..      
35b6d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35b6e 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
35b6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49  w, pIn->iMem, pI
35b70 6e 2d 3e 6e 4d 65 6d 29 3b 0d 0a 20 20 20 20 20  n->nMem);..     
35b71 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
35b72 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
35b73 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65  pParse, pIn->iMe
35b74 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0d 0a  m, pIn->nMem);..
35b75 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
35b76 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
35b77 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64   Jump to the end
35b78 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20   of the loop if 
35b79 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61  the LIMIT is rea
35b7a 63 68 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  ched...  */..  i
35b7b 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0d  f( p->iLimit ){.
35b7c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
35b7d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 5a  AddOp3(v, OP_IfZ
35b7e 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20  ero, p->iLimit, 
35b7f 69 42 72 65 61 6b 2c 20 2d 31 29 3b 0d 0a 20 20  iBreak, -1);..  
35b80 7d 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61  }....  /* Genera
35b81 74 65 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  te the subroutin
35b82 65 20 72 65 74 75 72 6e 0d 0a 20 20 2a 2f 0d 0a  e return..  */..
35b83 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
35b84 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
35b85 6e 74 69 6e 75 65 29 3b 0d 0a 20 20 73 71 6c 69  ntinue);..  sqli
35b86 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
35b87 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52   OP_Return, regR
35b88 65 74 75 72 6e 29 3b 0d 0a 0d 0a 20 20 72 65 74  eturn);....  ret
35b89 75 72 6e 20 61 64 64 72 3b 0d 0a 7d 0d 0a 0d 0a  urn addr;..}....
35b8a 2f 2a 0d 0a 2a 2a 20 41 6c 74 65 72 6e 61 74 69  /*..** Alternati
35b8b 76 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  ve compound sele
35b8c 63 74 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  ct code generato
35b8d 72 20 66 6f 72 20 63 61 73 65 73 20 77 68 65 6e  r for cases when
35b8e 20 74 68 65 72 65 0d 0a 2a 2a 20 69 73 20 61 6e   there..** is an
35b8f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
35b90 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 65 20 61 73 73  ...**..** We ass
35b91 75 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20 74  ume a query of t
35b92 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
35b93 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  m:..**..**      
35b94 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72  <selectA>  <oper
35b95 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e  ator>  <selectB>
35b96 20 20 4f 52 44 45 52 20 42 59 20 3c 6f 72 64 65    ORDER BY <orde
35b97 72 62 79 6c 69 73 74 3e 0d 0a 2a 2a 0d 0a 2a 2a  rbylist>..**..**
35b98 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 6f   <operator> is o
35b99 6e 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c  ne of UNION ALL,
35b9a 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20   UNION, EXCEPT, 
35b9b 6f 72 20 49 4e 54 45 52 53 45 43 54 2e 20 20 54  or INTERSECT.  T
35b9c 68 65 20 69 64 65 61 0d 0a 2a 2a 20 69 73 20 74  he idea..** is t
35b9d 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c  o code both <sel
35b9e 65 63 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63  ectA> and <selec
35b9f 74 42 3e 20 77 69 74 68 20 74 68 65 20 4f 52 44  tB> with the ORD
35ba0 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 73 0d  ER BY clause as.
35ba1 0a 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e  .** co-routines.
35ba2 20 20 54 68 65 6e 20 72 75 6e 20 74 68 65 20 63    Then run the c
35ba3 6f 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20 70 61  o-routines in pa
35ba4 72 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65  rallel and merge
35ba5 20 74 68 65 20 72 65 73 75 6c 74 73 0d 0a 2a 2a   the results..**
35ba6 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74   into the output
35ba7 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74  .  In addition t
35ba8 6f 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74  o the two corout
35ba9 69 6e 65 73 20 28 63 61 6c 6c 65 64 20 73 65 6c  ines (called sel
35baa 65 63 74 41 20 61 6e 64 0d 0a 2a 2a 20 73 65 6c  ectA and..** sel
35bab 65 63 74 42 29 20 74 68 65 72 65 20 61 72 65 20  ectB) there are 
35bac 37 20 73 75 62 72 6f 75 74 69 6e 65 73 3a 0d 0a  7 subroutines:..
35bad 2a 2a 0d 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20  **..**    outA: 
35bae 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74 70     Move the outp
35baf 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ut of the select
35bb0 41 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74 6f  A coroutine into
35bb1 20 74 68 65 20 6f 75 74 70 75 74 0d 0a 2a 2a 20   the output..** 
35bb2 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
35bb3 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
35bb4 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 6f 75  y...**..**    ou
35bb5 74 42 3a 20 20 20 20 4d 6f 76 65 20 74 68 65 20  tB:    Move the 
35bb6 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 73 65  output of the se
35bb7 6c 65 63 74 42 20 63 6f 72 6f 75 74 69 6e 65 20  lectB coroutine 
35bb8 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0d  into the output.
35bb9 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
35bba 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  of the compound 
35bbb 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79 20 67 65  query.  (Only ge
35bbc 6e 65 72 61 74 65 64 20 66 6f 72 20 55 4e 49 4f  nerated for UNIO
35bbd 4e 20 61 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 20  N and..**       
35bbe 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e        UNION ALL.
35bbf 20 20 45 58 43 45 50 54 20 61 6e 64 20 49 4e 53    EXCEPT and INS
35bc0 45 52 54 53 45 43 54 20 6e 65 76 65 72 20 6f 75  ERTSECT never ou
35bc1 74 70 75 74 20 61 20 72 6f 77 20 74 68 61 74 0d  tput a row that.
35bc2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
35bc3 61 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e 20  appears only in 
35bc4 42 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 41  B.)..**..**    A
35bc5 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ltB:    Called w
35bc6 68 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 74  hen there is dat
35bc7 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f  a from both coro
35bc8 75 74 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0d  utines and A<B..
35bc9 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 41 65 71 42 3a  .**..**    AeqB:
35bca 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20      Called when 
35bcb 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66 72  there is data fr
35bcc 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e  om both coroutin
35bcd 65 73 20 61 6e 64 20 41 3d 3d 42 2e 0d 0a 2a 2a  es and A==B...**
35bce 0d 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 20 20  ..**    AgtB:   
35bcf 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
35bd0 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
35bd1 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
35bd2 61 6e 64 20 41 3e 42 2e 0d 0a 2a 2a 0d 0a 2a 2a  and A>B...**..**
35bd3 20 20 20 20 45 6f 66 41 3a 20 20 20 20 43 61 6c      EofA:    Cal
35bd4 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69 73  led when data is
35bd5 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20   exhausted from 
35bd6 73 65 6c 65 63 74 41 2e 0d 0a 2a 2a 0d 0a 2a 2a  selectA...**..**
35bd7 20 20 20 20 45 6f 66 42 3a 20 20 20 20 43 61 6c      EofB:    Cal
35bd8 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69 73  led when data is
35bd9 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20   exhausted from 
35bda 73 65 6c 65 63 74 42 2e 0d 0a 2a 2a 0d 0a 2a 2a  selectB...**..**
35bdb 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
35bdc 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 74 74 65  ion of the latte
35bdd 72 20 66 69 76 65 20 73 75 62 72 6f 75 74 69 6e  r five subroutin
35bde 65 73 20 64 65 70 65 6e 64 20 6f 6e 20 77 68 69  es depend on whi
35bdf 63 68 20 0d 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f  ch ..** <operato
35be0 72 3e 20 69 73 20 75 73 65 64 3a 0d 0a 2a 2a 0d  r> is used:..**.
35be1 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  .**..**         
35be2 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20 20      UNION ALL   
35be3 20 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20 20        UNION     
35be4 20 20 20 20 20 20 20 45 58 43 45 50 54 20 20 20         EXCEPT   
35be5 20 20 20 20 20 20 20 49 4e 54 45 52 53 45 43 54         INTERSECT
35be6 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d  ..**          --
35be7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d  -----------  ---
35be8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
35be9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
35bea 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
35beb 2d 0d 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20  -..**   AltB:   
35bec 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
35bed 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
35bee 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
35bef 20 20 20 20 20 20 20 6e 65 78 74 41 0d 0a 2a 2a         nextA..**
35bf0 0d 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20 20 6f  ..**   AeqB:   o
35bf1 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
35bf2 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20 20     nextA        
35bf3 20 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20       nextA      
35bf4 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 0d 0a     outA, nextA..
35bf5 2a 2a 0d 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20  **..**   AgtB:  
35bf6 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
35bf7 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20    outB, nextB   
35bf8 20 20 20 20 20 20 20 6e 65 78 74 42 20 20 20 20         nextB    
35bf9 20 20 20 20 20 20 20 20 6e 65 78 74 42 0d 0a 2a          nextB..*
35bfa 2a 0d 0a 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20  *..**   EofA:   
35bfb 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20  outB, nextB     
35bfc 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
35bfd 20 20 20 20 20 20 68 61 6c 74 20 20 20 20 20 20        halt      
35bfe 20 20 20 20 20 20 20 68 61 6c 74 0d 0a 2a 2a 0d         halt..**.
35bff 0a 2a 2a 20 20 20 45 6f 66 42 3a 20 20 20 6f 75  .**   EofB:   ou
35c00 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f  tA, nextA      o
35c01 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
35c02 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
35c03 20 20 20 20 20 68 61 6c 74 0d 0a 2a 2a 0d 0a 2a       halt..**..*
35c04 2a 20 49 6e 20 74 68 65 20 41 6c 74 42 2c 20 41  * In the AltB, A
35c05 65 71 42 2c 20 61 6e 64 20 41 67 74 42 20 73 75  eqB, and AgtB su
35c06 62 72 6f 75 74 69 6e 65 73 2c 20 61 6e 20 45 4f  broutines, an EO
35c07 46 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77 69 6e 67  F on A following
35c08 20 6e 65 78 74 41 0d 0a 2a 2a 20 63 61 75 73 65   nextA..** cause
35c09 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a  s an immediate j
35c0a 75 6d 70 20 74 6f 20 45 6f 66 41 20 61 6e 64 20  ump to EofA and 
35c0b 61 6e 20 45 4f 46 20 6f 6e 20 42 20 66 6f 6c 6c  an EOF on B foll
35c0c 6f 77 69 6e 67 20 6e 65 78 74 42 20 63 61 75 73  owing nextB caus
35c0d 65 73 0d 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64 69  es..** an immedi
35c0e 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 42  ate jump to EofB
35c0f 2e 20 20 57 69 74 68 69 6e 20 45 6f 66 41 20 61  .  Within EofA a
35c10 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20 45 4f 46  nd EofB, and EOF
35c11 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0d 0a 2a 2a   on entry or..**
35c12 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 58   following nextX
35c13 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70 20 74   causes a jump t
35c14 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
35c15 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73 69   select processi
35c16 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 44 75 70 6c  ng...**..** Dupl
35c17 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e  icate removal in
35c18 20 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45   the UNION, EXCE
35c19 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43  PT, and INTERSEC
35c1a 54 20 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c  T cases is handl
35c1b 65 64 0d 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  ed..** within th
35c1c 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74  e output subrout
35c1d 69 6e 65 2e 20 20 54 68 65 20 72 65 67 50 72 65  ine.  The regPre
35c1e 76 20 72 65 67 69 73 74 65 72 20 73 65 74 20 68  v register set h
35c1f 6f 6c 64 73 20 74 68 65 20 70 72 65 76 69 6f 75  olds the previou
35c20 73 6c 79 0d 0a 2a 2a 20 6f 75 74 70 75 74 20 76  sly..** output v
35c21 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61 72 69  alue.  A compari
35c22 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 67 61 69  son is made agai
35c23 6e 73 74 20 74 68 69 73 20 76 61 6c 75 65 20 61  nst this value a
35c24 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0d 0a 2a  nd the output..*
35c25 2a 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 20  * is skipped if 
35c26 74 68 65 20 6e 65 78 74 20 72 65 73 75 6c 74 73  the next results
35c27 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20 73 61   would be the sa
35c28 6d 65 20 61 73 20 74 68 65 20 70 72 65 76 69 6f  me as the previo
35c29 75 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  us...**..** The 
35c2a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70  implementation p
35c2b 6c 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d  lan is to implem
35c2c 65 6e 74 20 74 68 65 20 74 77 6f 20 63 6f 72 6f  ent the two coro
35c2d 75 74 69 6e 65 73 20 61 6e 64 20 73 65 76 65 6e  utines and seven
35c2e 0d 0a 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65 73  ..** subroutines
35c2f 20 66 69 72 73 74 2c 20 74 68 65 6e 20 70 75 74   first, then put
35c30 20 74 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67   the control log
35c31 69 63 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  ic at the bottom
35c32 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0d 0a 2a  .  Like this:..*
35c33 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 67  *..**          g
35c34 6f 74 6f 20 49 6e 69 74 0d 0a 2a 2a 20 20 20 20  oto Init..**    
35c35 20 63 6f 41 3a 20 63 6f 72 6f 75 74 69 6e 65 20   coA: coroutine 
35c36 66 6f 72 20 6c 65 66 74 20 71 75 65 72 79 20 28  for left query (
35c37 41 29 0d 0a 2a 2a 20 20 20 20 20 63 6f 42 3a 20  A)..**     coB: 
35c38 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69  coroutine for ri
35c39 67 68 74 20 71 75 65 72 79 20 28 42 29 0d 0a 2a  ght query (B)..*
35c3a 2a 20 20 20 20 6f 75 74 41 3a 20 6f 75 74 70 75  *    outA: outpu
35c3b 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 41 0d 0a  t one row of A..
35c3c 2a 2a 20 20 20 20 6f 75 74 42 3a 20 6f 75 74 70  **    outB: outp
35c3d 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 42 20  ut one row of B 
35c3e 28 55 4e 49 4f 4e 20 61 6e 64 20 55 4e 49 4f 4e  (UNION and UNION
35c3f 20 41 4c 4c 20 6f 6e 6c 79 29 0d 0a 2a 2a 20 20   ALL only)..**  
35c40 20 20 45 6f 66 41 3a 20 2e 2e 2e 0d 0a 2a 2a 20    EofA: .....** 
35c41 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0d 0a 2a 2a     EofB: .....**
35c42 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0d 0a 2a      AltB: .....*
35c43 2a 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0d 0a  *    AeqB: .....
35c44 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0d  **    AgtB: ....
35c45 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69 6e 69  .**    Init: ini
35c46 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e  tialize coroutin
35c47 65 20 72 65 67 69 73 74 65 72 73 0d 0a 2a 2a 20  e registers..** 
35c48 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20 63           yield c
35c49 6f 41 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  oA..**          
35c4a 69 66 20 65 6f 66 28 41 29 20 67 6f 74 6f 20 45  if eof(A) goto E
35c4b 6f 66 41 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ofA..**         
35c4c 20 79 69 65 6c 64 20 63 6f 42 0d 0a 2a 2a 20 20   yield coB..**  
35c4d 20 20 20 20 20 20 20 20 69 66 20 65 6f 66 28 42          if eof(B
35c4e 29 20 67 6f 74 6f 20 45 6f 66 42 0d 0a 2a 2a 20  ) goto EofB..** 
35c4f 20 20 20 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65     Cmpr: Compare
35c50 20 41 2c 20 42 0d 0a 2a 2a 20 20 20 20 20 20 20   A, B..**       
35c51 20 20 20 4a 75 6d 70 20 41 6c 74 42 2c 20 41 65     Jump AltB, Ae
35c52 71 42 2c 20 41 67 74 42 0d 0a 2a 2a 20 20 20 20  qB, AgtB..**    
35c53 20 45 6e 64 3a 20 2e 2e 2e 0d 0a 2a 2a 0d 0a 2a   End: .....**..*
35c54 2a 20 57 65 20 63 61 6c 6c 20 41 6c 74 42 2c 20  * We call AltB, 
35c55 41 65 71 42 2c 20 41 67 74 42 2c 20 45 6f 66 41  AeqB, AgtB, EofA
35c56 2c 20 61 6e 64 20 45 6f 66 42 20 22 73 75 62 72  , and EofB "subr
35c57 6f 75 74 69 6e 65 73 22 20 62 75 74 20 74 68 65  outines" but the
35c58 79 20 61 72 65 20 6e 6f 74 0d 0a 2a 2a 20 61 63  y are not..** ac
35c59 74 75 61 6c 6c 79 20 63 61 6c 6c 65 64 20 75 73  tually called us
35c5a 69 6e 67 20 47 6f 73 75 62 20 61 6e 64 20 74 68  ing Gosub and th
35c5b 65 79 20 64 6f 20 6e 6f 74 20 52 65 74 75 72 6e  ey do not Return
35c5c 2e 20 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42  .  EofA and EofB
35c5d 20 6c 6f 6f 70 0d 0a 2a 2a 20 75 6e 74 69 6c 20   loop..** until 
35c5e 61 6c 6c 20 64 61 74 61 20 69 73 20 65 78 68 61  all data is exha
35c5f 75 73 74 65 64 20 74 68 65 6e 20 6a 75 6d 70 20  usted then jump 
35c60 74 6f 20 74 68 65 20 22 65 6e 64 22 20 6c 61 62  to the "end" lab
35c61 65 2e 20 20 41 6c 74 42 2c 20 41 65 71 42 2c 0d  e.  AltB, AeqB,.
35c62 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a 75 6d  .** and AgtB jum
35c63 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32 20 6f  p to either L2 o
35c64 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f 66 41  r to one of EofA
35c65 20 6f 72 20 45 6f 66 42 2e 0d 0a 2a 2f 0d 0a 23   or EofB...*/..#
35c66 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
35c67 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
35c68 43 54 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  CT..static int m
35c69 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42  ultiSelectOrderB
35c6a 79 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  y(..  Parse *pPa
35c6b 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
35c6c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
35c6d 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  /..  Select *p, 
35c6e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
35c6f 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20  e right-most of 
35c70 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f  SELECTs to be co
35c71 64 65 64 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74  ded */..  Select
35c72 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20  Dest *pDest     
35c73 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
35c74 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  th query results
35c75 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69   */..){..  int i
35c76 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
35c77 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
35c78 73 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a  s */..  Select *
35c79 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a  pPrior;       /*
35c7a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43 54 20   Another SELECT 
35c7b 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6f  immediately to o
35c7c 75 72 20 6c 65 66 74 20 2a 2f 0d 0a 20 20 56 64  ur left */..  Vd
35c7d 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
35c7e 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
35c7f 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44 42  code to this VDB
35c80 45 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 44 65  E */..  SelectDe
35c81 73 74 20 64 65 73 74 41 3b 20 20 20 20 20 2f 2a  st destA;     /*
35c82 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
35c83 20 63 6f 72 6f 75 74 69 6e 65 20 41 20 2a 2f 0d   coroutine A */.
35c84 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
35c85 73 74 42 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stB;     /* Dest
35c86 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
35c87 75 74 69 6e 65 20 42 20 2a 2f 0d 0a 20 20 69 6e  utine B */..  in
35c88 74 20 72 65 67 41 64 64 72 41 3b 20 20 20 20 20  t regAddrA;     
35c89 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72      /* Address r
35c8a 65 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65  egister for sele
35c8b 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a  ct-A coroutine *
35c8c 2f 0d 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 41  /..  int regEofA
35c8d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c  ;          /* Fl
35c8e 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77  ag to indicate w
35c8f 68 65 6e 20 73 65 6c 65 63 74 2d 41 20 69 73 20  hen select-A is 
35c90 63 6f 6d 70 6c 65 74 65 20 2a 2f 0d 0a 20 20 69  complete */..  i
35c91 6e 74 20 72 65 67 41 64 64 72 42 3b 20 20 20 20  nt regAddrB;    
35c92 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
35c93 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c  register for sel
35c94 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20  ect-B coroutine 
35c95 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67 45 6f 66  */..  int regEof
35c96 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  B;          /* F
35c97 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20  lag to indicate 
35c98 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20 69 73  when select-B is
35c99 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0d 0a 20 20   complete */..  
35c9a 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41 3b  int addrSelectA;
35c9b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
35c9c 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41   of the select-A
35c9d 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20   coroutine */.. 
35c9e 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42   int addrSelectB
35c9f 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
35ca0 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d  s of the select-
35ca1 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a  B coroutine */..
35ca2 20 20 69 6e 74 20 72 65 67 4f 75 74 41 3b 20 20    int regOutA;  
35ca3 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
35ca4 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
35ca5 74 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62  the output-A sub
35ca6 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e  routine */..  in
35ca7 74 20 72 65 67 4f 75 74 42 3b 20 20 20 20 20 20  t regOutB;      
35ca8 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72      /* Address r
35ca9 65 67 69 73 74 65 72 20 66 6f 72 20 74 68 65 20  egister for the 
35caa 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74  output-B subrout
35cab 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64  ine */..  int ad
35cac 64 72 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20  drOutA;         
35cad 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
35cae 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f  e output-A subro
35caf 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  utine */..  int 
35cb0 61 64 64 72 4f 75 74 42 20 3d 20 30 3b 20 20 20  addrOutB = 0;   
35cb1 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
35cb2 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62  the output-B sub
35cb3 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e  routine */..  in
35cb4 74 20 61 64 64 72 45 6f 66 41 3b 20 20 20 20 20  t addrEofA;     
35cb5 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
35cb6 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41 2d 65  f the select-A-e
35cb7 78 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74  xhausted subrout
35cb8 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64  ine */..  int ad
35cb9 64 72 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20  drEofB;         
35cba 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
35cbb 65 20 73 65 6c 65 63 74 2d 42 2d 65 78 68 61 75  e select-B-exhau
35cbc 73 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20  sted subroutine 
35cbd 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72 41 6c  */..  int addrAl
35cbe 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  tB;         /* A
35cbf 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3c  ddress of the A<
35cc0 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0d  B subroutine */.
35cc1 0a 20 20 69 6e 74 20 61 64 64 72 41 65 71 42 3b  .  int addrAeqB;
35cc2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
35cc3 65 73 73 20 6f 66 20 74 68 65 20 41 3d 3d 42 20  ess of the A==B 
35cc4 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20  subroutine */.. 
35cc5 20 69 6e 74 20 61 64 64 72 41 67 74 42 3b 20 20   int addrAgtB;  
35cc6 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
35cc7 73 20 6f 66 20 74 68 65 20 41 3e 42 20 73 75 62  s of the A>B sub
35cc8 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 69 6e  routine */..  in
35cc9 74 20 72 65 67 4c 69 6d 69 74 41 3b 20 20 20 20  t regLimitA;    
35cca 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67      /* Limit reg
35ccb 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
35ccc 2d 41 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  -A */..  int reg
35ccd 4c 69 6d 69 74 42 3b 20 20 20 20 20 20 20 20 2f  LimitB;        /
35cce 2a 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72  * Limit register
35ccf 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f   for select-A */
35cd0 0d 0a 20 20 69 6e 74 20 72 65 67 50 72 65 76 3b  ..  int regPrev;
35cd1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72            /* A r
35cd2 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65 72  ange of register
35cd3 73 20 74 6f 20 68 6f 6c 64 20 70 72 65 76 69 6f  s to hold previo
35cd4 75 73 20 6f 75 74 70 75 74 20 2a 2f 0d 0a 20 20  us output */..  
35cd5 69 6e 74 20 73 61 76 65 64 4c 69 6d 69 74 3b 20  int savedLimit; 
35cd6 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76        /* Saved v
35cd7 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c 69 6d 69  alue of p->iLimi
35cd8 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 61 76 65  t */..  int save
35cd9 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  dOffset;      /*
35cda 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
35cdb 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0d 0a 20  p->iOffset */.. 
35cdc 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b 20   int labelCmpr; 
35cdd 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20         /* Label 
35cde 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66  for the start of
35cdf 20 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f 72   the merge algor
35ce0 69 74 68 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c  ithm */..  int l
35ce1 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20  abelEnd;        
35ce2 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68   /* Label for th
35ce3 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65  e end of the ove
35ce4 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74  rall SELECT stmt
35ce5 20 2a 2f 0d 0a 20 20 69 6e 74 20 6a 31 3b 20 20   */..  int j1;  
35ce6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35ce7 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e  Jump instruction
35ce8 73 20 74 68 61 74 20 67 65 74 20 72 65 74 61 72  s that get retar
35ce9 67 65 74 74 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  getted */..  int
35cea 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
35ceb 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f     /* One of TK_
35cec 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54  ALL, TK_UNION, T
35ced 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54  K_EXCEPT, TK_INT
35cee 45 52 53 45 43 54 20 2a 2f 0d 0a 20 20 4b 65 79  ERSECT */..  Key
35cef 49 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20  Info *pKeyDup = 
35cf0 30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e  0; /* Comparison
35cf1 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
35cf2 20 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76   duplicate remov
35cf3 61 6c 20 2a 2f 0d 0a 20 20 4b 65 79 49 6e 66 6f  al */..  KeyInfo
35cf4 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f   *pKeyMerge;   /
35cf5 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66  * Comparison inf
35cf6 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72  ormation for mer
35cf7 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0d 0a 20 20  ging rows */..  
35cf8 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
35cf9 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
35cfa 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
35cfb 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
35cfc 64 65 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20  derBy;   /* The 
35cfd 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
35cfe 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72  */..  int nOrder
35cff 42 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  By;         /* N
35d00 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
35d01 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  n the ORDER BY c
35d02 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  lause */..  int 
35d03 2a 61 50 65 72 6d 75 74 65 3b 20 20 20 20 20 20  *aPermute;      
35d04 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
35d05 6d 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73  m ORDER BY terms
35d06 20 74 6f 20 72 65 73 75 6c 74 20 73 65 74 20 63   to result set c
35d07 6f 6c 75 6d 6e 73 20 2a 2f 0d 0a 23 69 66 6e 64  olumns */..#ifnd
35d08 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
35d09 58 50 4c 41 49 4e 0d 0a 20 20 69 6e 74 20 69 53  XPLAIN..  int iS
35d0a 75 62 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  ub1;            
35d0b 2f 2a 20 45 51 50 20 69 64 20 6f 66 20 6c 65 66  /* EQP id of lef
35d0c 74 2d 68 61 6e 64 20 71 75 65 72 79 20 2a 2f 0d  t-hand query */.
35d0d 0a 20 20 69 6e 74 20 69 53 75 62 32 3b 20 20 20  .  int iSub2;   
35d0e 20 20 20 20 20 20 20 20 20 2f 2a 20 45 51 50 20           /* EQP 
35d0f 69 64 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64  id of right-hand
35d10 20 71 75 65 72 79 20 2a 2f 0d 0a 23 65 6e 64 69   query */..#endi
35d11 66 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  f....  assert( p
35d12 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b  ->pOrderBy!=0 );
35d13 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ..  assert( pKey
35d14 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61  Dup==0 ); /* "Ma
35d15 6e 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65 64  naged" code need
35d16 73 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74 20  s this.  Ticket 
35d17 23 33 33 38 32 2e 20 2a 2f 0d 0a 20 20 64 62 20  #3382. */..  db 
35d18 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20  = pParse->db;.. 
35d19 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
35d1a 62 65 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 76  be;..  assert( v
35d1b 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20  !=0 );       /* 
35d1c 41 6c 72 65 61 64 79 20 74 68 72 6f 77 6e 20 74  Already thrown t
35d1d 68 65 20 65 72 72 6f 72 20 69 66 20 56 44 42 45  he error if VDBE
35d1e 20 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f   alloc failed */
35d1f 0d 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73  ..  labelEnd = s
35d20 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
35d21 62 65 6c 28 76 29 3b 0d 0a 20 20 6c 61 62 65 6c  bel(v);..  label
35d22 43 6d 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Cmpr = sqlite3Vd
35d23 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d  beMakeLabel(v);.
35d24 0a 0d 0a 0d 0a 20 20 2f 2a 20 50 61 74 63 68 20  .....  /* Patch 
35d25 75 70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  up the ORDER BY 
35d26 63 6c 61 75 73 65 0d 0a 20 20 2a 2f 0d 0a 20 20  clause..  */..  
35d27 6f 70 20 3d 20 70 2d 3e 6f 70 3b 20 20 0d 0a 20  op = p->op;  .. 
35d28 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72   pPrior = p->pPr
35d29 69 6f 72 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ior;..  assert( 
35d2a 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
35d2b 3d 3d 30 20 29 3b 0d 0a 20 20 70 4f 72 64 65 72  ==0 );..  pOrder
35d2c 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79  By = p->pOrderBy
35d2d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72  ;..  assert( pOr
35d2e 64 65 72 42 79 20 29 3b 0d 0a 20 20 6e 4f 72 64  derBy );..  nOrd
35d2f 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
35d30 3e 6e 45 78 70 72 3b 0d 0a 0d 0a 20 20 2f 2a 20  >nExpr;....  /* 
35d31 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20 6f 74  For operators ot
35d32 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20 41  her than UNION A
35d33 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61  LL we have to ma
35d34 6b 65 20 73 75 72 65 20 74 68 61 74 0d 0a 20 20  ke sure that..  
35d35 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ** the ORDER BY 
35d36 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65 76  clause covers ev
35d37 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ery term of the 
35d38 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64 64  result set.  Add
35d39 0d 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20  ..  ** terms to 
35d3a 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
35d3b 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72 79  use as necessary
35d3c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6f  ...  */..  if( o
35d3d 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0d 0a 20 20  p!=TK_ALL ){..  
35d3e 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d    for(i=1; db->m
35d3f 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26  allocFailed==0 &
35d40 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e  & i<=p->pEList->
35d41 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nExpr; i++){..  
35d42 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
35d43 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
35d44 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c  ..      for(j=0,
35d45 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d   pItem=pOrderBy-
35d46 3e 61 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20  >a; j<nOrderBy; 
35d47 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a  j++, pItem++){..
35d48 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
35d49 70 49 74 65 6d 2d 3e 69 4f 72 64 65 72 42 79 43  pItem->iOrderByC
35d4a 6f 6c 3e 30 20 29 3b 0d 0a 20 20 20 20 20 20 20  ol>0 );..       
35d4b 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 4f 72 64   if( pItem->iOrd
35d4c 65 72 42 79 43 6f 6c 3d 3d 69 20 29 20 62 72 65  erByCol==i ) bre
35d4d 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ak;..      }..  
35d4e 20 20 20 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65      if( j==nOrde
35d4f 72 42 79 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  rBy ){..        
35d50 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c  Expr *pNew = sql
35d51 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
35d52 49 4e 54 45 47 45 52 2c 20 30 29 3b 0d 0a 20 20  INTEGER, 0);..  
35d53 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d        if( pNew==
35d54 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
35d55 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20  E_NOMEM;..      
35d56 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d    pNew->flags |=
35d57 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0d 0a 20   EP_IntValue;.. 
35d58 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69         pNew->u.i
35d59 56 61 6c 75 65 20 3d 20 69 3b 0d 0a 20 20 20 20  Value = i;..    
35d5a 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 73      pOrderBy = s
35d5b 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
35d5c 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4f 72  pend(pParse, pOr
35d5d 64 65 72 42 79 2c 20 70 4e 65 77 29 3b 0d 0a 20  derBy, pNew);.. 
35d5e 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d         pOrderBy-
35d5f 3e 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69  >a[nOrderBy++].i
35d60 4f 72 64 65 72 42 79 43 6f 6c 20 3d 20 28 75 31  OrderByCol = (u1
35d61 36 29 69 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  6)i;..      }.. 
35d62 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
35d63 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f  * Compute the co
35d64 6d 70 61 72 69 73 6f 6e 20 70 65 72 6d 75 74 61  mparison permuta
35d65 74 69 6f 6e 20 61 6e 64 20 6b 65 79 69 6e 66 6f  tion and keyinfo
35d66 20 74 68 61 74 20 69 73 20 75 73 65 64 20 77 69   that is used wi
35d67 74 68 0d 0a 20 20 2a 2a 20 74 68 65 20 70 65 72  th..  ** the per
35d68 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 74 6f  mutation used to
35d69 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
35d6a 65 20 6e 65 78 74 0d 0a 20 20 2a 2a 20 72 6f 77  e next..  ** row
35d6b 20 6f 66 20 72 65 73 75 6c 74 73 20 63 6f 6d 65   of results come
35d6c 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 20 6f  s from selectA o
35d6d 72 20 73 65 6c 65 63 74 42 2e 20 20 41 6c 73 6f  r selectB.  Also
35d6e 20 61 64 64 20 65 78 70 6c 69 63 69 74 0d 0a 20   add explicit.. 
35d6f 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 74   ** collations t
35d70 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  o the ORDER BY c
35d71 6c 61 75 73 65 20 74 65 72 6d 73 20 73 6f 20 74  lause terms so t
35d72 68 61 74 20 77 68 65 6e 20 74 68 65 20 73 75 62  hat when the sub
35d73 71 75 65 72 69 65 73 0d 0a 20 20 2a 2a 20 74 6f  queries..  ** to
35d74 20 74 68 65 20 72 69 67 68 74 20 61 6e 64 20 74   the right and t
35d75 68 65 20 6c 65 66 74 20 61 72 65 20 65 76 61 6c  he left are eval
35d76 75 61 74 65 64 2c 20 74 68 65 79 20 75 73 65 20  uated, they use 
35d77 74 68 65 20 63 6f 72 72 65 63 74 0d 0a 20 20 2a  the correct..  *
35d78 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0d 0a 20 20  * collation...  
35d79 2a 2f 0d 0a 20 20 61 50 65 72 6d 75 74 65 20 3d  */..  aPermute =
35d7a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
35d7b 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
35d7c 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0d 0a  nt)*nOrderBy);..
35d7d 20 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29    if( aPermute )
35d7e 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  {..    struct Ex
35d7f 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
35d80 65 6d 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30  em;..    for(i=0
35d81 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
35d82 2d 3e 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b  ->a; i<nOrderBy;
35d83 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d   i++, pItem++){.
35d84 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
35d85 49 74 65 6d 2d 3e 69 4f 72 64 65 72 42 79 43 6f  Item->iOrderByCo
35d86 6c 3e 30 20 20 26 26 20 70 49 74 65 6d 2d 3e 69  l>0  && pItem->i
35d87 4f 72 64 65 72 42 79 43 6f 6c 3c 3d 70 2d 3e 70  OrderByCol<=p->p
35d88 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0d  EList->nExpr );.
35d89 0a 20 20 20 20 20 20 61 50 65 72 6d 75 74 65 5b  .      aPermute[
35d8a 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 69 4f 72 64  i] = pItem->iOrd
35d8b 65 72 42 79 43 6f 6c 20 2d 20 31 3b 0d 0a 20 20  erByCol - 1;..  
35d8c 20 20 7d 0d 0a 20 20 20 20 70 4b 65 79 4d 65 72    }..    pKeyMer
35d8d 67 65 20 3d 0d 0a 20 20 20 20 20 20 73 71 6c 69  ge =..      sqli
35d8e 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
35d8f 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 4d  b, sizeof(*pKeyM
35d90 65 72 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a 28  erge)+nOrderBy*(
35d91 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29  sizeof(CollSeq*)
35d92 2b 31 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  +1));..    if( p
35d93 4b 65 79 4d 65 72 67 65 20 29 7b 0d 0a 20 20 20  KeyMerge ){..   
35d94 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53     pKeyMerge->aS
35d95 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29  ortOrder = (u8*)
35d96 26 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c  &pKeyMerge->aCol
35d97 6c 5b 6e 4f 72 64 65 72 42 79 5d 3b 0d 0a 20 20  l[nOrderBy];..  
35d98 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 6e      pKeyMerge->n
35d99 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f 72  Field = (u16)nOr
35d9a 64 65 72 42 79 3b 0d 0a 20 20 20 20 20 20 70 4b  derBy;..      pK
35d9b 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45  eyMerge->enc = E
35d9c 4e 43 28 64 62 29 3b 0d 0a 20 20 20 20 20 20 66  NC(db);..      f
35d9d 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72  or(i=0; i<nOrder
35d9e 42 79 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  By; i++){..     
35d9f 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
35da0 6c 3b 0d 0a 20 20 20 20 20 20 20 20 45 78 70 72  l;..        Expr
35da1 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65 72   *pTerm = pOrder
35da2 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0d  By->a[i].pExpr;.
35da3 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65  .        if( pTe
35da4 72 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  rm->flags & EP_E
35da5 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0d 0a 20 20  xpCollate ){..  
35da6 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
35da7 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0d 0a 20  pTerm->pColl;.. 
35da8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
35da9 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d           pColl =
35daa 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c   multiSelectColl
35dab 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 61  Seq(pParse, p, a
35dac 50 65 72 6d 75 74 65 5b 69 5d 29 3b 0d 0a 20 20  Permute[i]);..  
35dad 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66          pTerm->f
35dae 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
35daf 6c 6c 61 74 65 3b 0d 0a 20 20 20 20 20 20 20 20  llate;..        
35db0 20 20 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 20 3d    pTerm->pColl =
35db1 20 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20 20   pColl;..       
35db2 20 7d 0d 0a 20 20 20 20 20 20 20 20 70 4b 65 79   }..        pKey
35db3 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 69 5d 20  Merge->aColl[i] 
35db4 3d 20 70 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20 20  = pColl;..      
35db5 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f    pKeyMerge->aSo
35db6 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 4f 72  rtOrder[i] = pOr
35db7 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74  derBy->a[i].sort
35db8 4f 72 64 65 72 3b 0d 0a 20 20 20 20 20 20 7d 0d  Order;..      }.
35db9 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
35dba 0d 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65 20  ..    pKeyMerge 
35dbb 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  = 0;..  }....  /
35dbc 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f  * Reattach the O
35dbd 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74  RDER BY clause t
35dbe 6f 20 74 68 65 20 71 75 65 72 79 2e 0d 0a 20 20  o the query...  
35dbf 2a 2f 0d 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42  */..  p->pOrderB
35dc0 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0d 0a 20  y = pOrderBy;.. 
35dc1 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42   pPrior->pOrderB
35dc2 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
35dc3 69 73 74 44 75 70 28 70 50 61 72 73 65 2d 3e 64  istDup(pParse->d
35dc4 62 2c 20 70 4f 72 64 65 72 42 79 2c 20 30 29 3b  b, pOrderBy, 0);
35dc5 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ....  /* Allocat
35dc6 65 20 61 20 72 61 6e 67 65 20 6f 66 20 74 65 6d  e a range of tem
35dc7 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73  porary registers
35dc8 20 61 6e 64 20 74 68 65 20 4b 65 79 49 6e 66 6f   and the KeyInfo
35dc9 20 6e 65 65 64 65 64 0d 0a 20 20 2a 2a 20 66 6f   needed..  ** fo
35dca 72 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74  r the logic that
35dcb 20 72 65 6d 6f 76 65 73 20 64 75 70 6c 69 63 61   removes duplica
35dcc 74 65 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77  te result rows w
35dcd 68 65 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 6f 70  hen the..  ** op
35dce 65 72 61 74 6f 72 20 69 73 20 55 4e 49 4f 4e 2c  erator is UNION,
35dcf 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e 54 45   EXCEPT, or INTE
35dd0 52 53 45 43 54 20 28 62 75 74 20 6e 6f 74 20 55  RSECT (but not U
35dd1 4e 49 4f 4e 20 41 4c 4c 29 2e 0d 0a 20 20 2a 2f  NION ALL)...  */
35dd2 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41  ..  if( op==TK_A
35dd3 4c 4c 20 29 7b 0d 0a 20 20 20 20 72 65 67 50 72  LL ){..    regPr
35dd4 65 76 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65  ev = 0;..  }else
35dd5 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 45 78 70 72  {..    int nExpr
35dd6 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45   = p->pEList->nE
35dd7 78 70 72 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  xpr;..    assert
35dd8 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70  ( nOrderBy>=nExp
35dd9 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
35dda 61 69 6c 65 64 20 29 3b 0d 0a 20 20 20 20 72 65  ailed );..    re
35ddb 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 47  gPrev = sqlite3G
35ddc 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
35ddd 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0d 0a 20  se, nExpr+1);.. 
35dde 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
35ddf 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
35de0 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b  er, 0, regPrev);
35de1 0d 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20  ..    pKeyDup = 
35de2 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
35de3 65 72 6f 28 64 62 2c 0d 0a 20 20 20 20 20 20 20  ero(db,..       
35de4 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
35de5 66 28 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45  f(*pKeyDup) + nE
35de6 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  xpr*(sizeof(Coll
35de7 53 65 71 2a 29 2b 31 29 20 29 3b 0d 0a 20 20 20  Seq*)+1) );..   
35de8 20 69 66 28 20 70 4b 65 79 44 75 70 20 29 7b 0d   if( pKeyDup ){.
35de9 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e  .      pKeyDup->
35dea 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
35deb 2a 29 26 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c  *)&pKeyDup->aCol
35dec 6c 5b 6e 45 78 70 72 5d 3b 0d 0a 20 20 20 20 20  l[nExpr];..     
35ded 20 70 4b 65 79 44 75 70 2d 3e 6e 46 69 65 6c 64   pKeyDup->nField
35dee 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b 0d 0a   = (u16)nExpr;..
35def 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 65        pKeyDup->e
35df0 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0d 0a 20  nc = ENC(db);.. 
35df1 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
35df2 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nExpr; i++){..  
35df3 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61        pKeyDup->a
35df4 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c 74 69 53  Coll[i] = multiS
35df5 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
35df6 72 73 65 2c 20 70 2c 20 69 29 3b 0d 0a 20 20 20  rse, p, i);..   
35df7 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53       pKeyDup->aS
35df8 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 30 3b  ortOrder[i] = 0;
35df9 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
35dfa 0d 0a 20 20 7d 0d 0a 20 0d 0a 20 20 2f 2a 20 53  ..  }.. ..  /* S
35dfb 65 70 61 72 61 74 65 20 74 68 65 20 6c 65 66 74  eparate the left
35dfc 20 61 6e 64 20 74 68 65 20 72 69 67 68 74 20 71   and the right q
35dfd 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e  uery from one an
35dfe 6f 74 68 65 72 0d 0a 20 20 2a 2f 0d 0a 20 20 70  other..  */..  p
35dff 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0d 0a 20  ->pPrior = 0;.. 
35e00 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
35e01 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72  rderGroupBy(pPar
35e02 73 65 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72  se, p, p->pOrder
35e03 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0d 0a 20  By, "ORDER");.. 
35e04 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 50 72   if( pPrior->pPr
35e05 69 6f 72 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73  ior==0 ){..    s
35e06 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
35e07 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65  erGroupBy(pParse
35e08 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69 6f 72  , pPrior, pPrior
35e09 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44  ->pOrderBy, "ORD
35e0a 45 52 22 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  ER");..  }....  
35e0b 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c  /* Compute the l
35e0c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20 2a  imit registers *
35e0d 2f 0d 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69  /..  computeLimi
35e0e 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72 73  tRegisters(pPars
35e0f 65 2c 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  e, p, labelEnd);
35e10 0d 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  ..  if( p->iLimi
35e11 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20  t && op==TK_ALL 
35e12 29 7b 0d 0a 20 20 20 20 72 65 67 4c 69 6d 69 74  ){..    regLimit
35e13 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
35e14 65 6d 3b 0d 0a 20 20 20 20 72 65 67 4c 69 6d 69  em;..    regLimi
35e15 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  tB = ++pParse->n
35e16 4d 65 6d 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  Mem;..    sqlite
35e17 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35e18 50 5f 43 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73  P_Copy, p->iOffs
35e19 65 74 20 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b  et ? p->iOffset+
35e1a 31 20 3a 20 70 2d 3e 69 4c 69 6d 69 74 2c 0d 0a  1 : p->iLimit,..
35e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e1d 20 20 72 65 67 4c 69 6d 69 74 41 29 3b 0d 0a 20    regLimitA);.. 
35e1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
35e1f 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
35e20 20 72 65 67 4c 69 6d 69 74 41 2c 20 72 65 67 4c   regLimitA, regL
35e21 69 6d 69 74 42 29 3b 0d 0a 20 20 7d 65 6c 73 65  imitB);..  }else
35e22 7b 0d 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41  {..    regLimitA
35e23 20 3d 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 30   = regLimitB = 0
35e24 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
35e25 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
35e26 70 2d 3e 70 4c 69 6d 69 74 29 3b 0d 0a 20 20 70  p->pLimit);..  p
35e27 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0d 0a 20  ->pLimit = 0;.. 
35e28 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
35e29 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65  te(db, p->pOffse
35e2a 74 29 3b 0d 0a 20 20 70 2d 3e 70 4f 66 66 73 65  t);..  p->pOffse
35e2b 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 72 65 67 41  t = 0;....  regA
35e2c 64 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  ddrA = ++pParse-
35e2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 72 65 67 45 6f 66  >nMem;..  regEof
35e2e 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
35e2f 65 6d 3b 0d 0a 20 20 72 65 67 41 64 64 72 42 20  em;..  regAddrB 
35e30 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
35e31 3b 0d 0a 20 20 72 65 67 45 6f 66 42 20 3d 20 2b  ;..  regEofB = +
35e32 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
35e33 20 20 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70 50    regOutA = ++pP
35e34 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 72  arse->nMem;..  r
35e35 65 67 4f 75 74 42 20 3d 20 2b 2b 70 50 61 72 73  egOutB = ++pPars
35e36 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 73 71 6c 69  e->nMem;..  sqli
35e37 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
35e38 74 28 26 64 65 73 74 41 2c 20 53 52 54 5f 43 6f  t(&destA, SRT_Co
35e39 72 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72  routine, regAddr
35e3a 41 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  A);..  sqlite3Se
35e3b 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
35e3c 73 74 42 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69  stB, SRT_Corouti
35e3d 6e 65 2c 20 72 65 67 41 64 64 72 42 29 3b 0d 0a  ne, regAddrB);..
35e3e 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 70 61 73 74  ..  /* Jump past
35e3f 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 75 62   the various sub
35e40 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 63 6f 72  routines and cor
35e41 6f 75 74 69 6e 65 73 20 74 6f 20 74 68 65 20 6d  outines to the m
35e42 61 69 6e 0d 0a 20 20 2a 2a 20 6d 65 72 67 65 20  ain..  ** merge 
35e43 6c 6f 6f 70 0d 0a 20 20 2a 2f 0d 0a 20 20 6a 31  loop..  */..  j1
35e44 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
35e45 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
35e46 3b 0d 0a 20 20 61 64 64 72 53 65 6c 65 63 74 41  ;..  addrSelectA
35e47 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
35e48 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0d 0a 0d  rrentAddr(v);...
35e49 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ...  /* Generate
35e4a 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20   a coroutine to 
35e4b 65 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c  evaluate the SEL
35e4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
35e4d 20 74 68 65 0d 0a 20 20 2a 2a 20 6c 65 66 74 20   the..  ** left 
35e4e 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  of the compound 
35e4f 6f 70 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22  operator - the "
35e50 41 22 20 73 65 6c 65 63 74 2e 0d 0a 20 20 2a 2f  A" select...  */
35e51 0d 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ..  VdbeNoopComm
35e52 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20 63  ent((v, "Begin c
35e53 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66  oroutine for lef
35e54 74 20 53 45 4c 45 43 54 22 29 29 3b 0d 0a 20 20  t SELECT"));..  
35e55 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d  pPrior->iLimit =
35e56 20 72 65 67 4c 69 6d 69 74 41 3b 0d 0a 20 20 65   regLimitA;..  e
35e57 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72  xplainSetInteger
35e58 28 69 53 75 62 31 2c 20 70 50 61 72 73 65 2d 3e  (iSub1, pParse->
35e59 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0d  iNextSelectId);.
35e5a 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
35e5b 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
35e5c 20 26 64 65 73 74 41 29 3b 0d 0a 20 20 73 71 6c   &destA);..  sql
35e5d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
35e5e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
35e5f 20 72 65 67 45 6f 66 41 29 3b 0d 0a 20 20 73 71   regEofA);..  sq
35e60 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
35e61 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
35e62 41 64 64 72 41 29 3b 0d 0a 20 20 56 64 62 65 4e  AddrA);..  VdbeN
35e63 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
35e64 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  End coroutine fo
35e65 72 20 6c 65 66 74 20 53 45 4c 45 43 54 22 29 29  r left SELECT"))
35e66 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ;....  /* Genera
35e67 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74  te a coroutine t
35e68 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53  o evaluate the S
35e69 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
35e6a 6f 6e 20 0d 0a 20 20 2a 2a 20 74 68 65 20 72 69  on ..  ** the ri
35e6b 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65  ght - the "B" se
35e6c 6c 65 63 74 0d 0a 20 20 2a 2f 0d 0a 20 20 61 64  lect..  */..  ad
35e6d 64 72 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69  drSelectB = sqli
35e6e 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
35e6f 64 72 28 76 29 3b 0d 0a 20 20 56 64 62 65 4e 6f  dr(v);..  VdbeNo
35e70 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42  opComment((v, "B
35e71 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66  egin coroutine f
35e72 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
35e73 29 29 3b 0d 0a 20 20 73 61 76 65 64 4c 69 6d 69  ));..  savedLimi
35e74 74 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b 0d 0a  t = p->iLimit;..
35e75 20 20 73 61 76 65 64 4f 66 66 73 65 74 20 3d 20    savedOffset = 
35e76 70 2d 3e 69 4f 66 66 73 65 74 3b 0d 0a 20 20 70  p->iOffset;..  p
35e77 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69  ->iLimit = regLi
35e78 6d 69 74 42 3b 0d 0a 20 20 70 2d 3e 69 4f 66 66  mitB;..  p->iOff
35e79 73 65 74 20 3d 20 30 3b 20 20 0d 0a 20 20 65 78  set = 0;  ..  ex
35e7a 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28  plainSetInteger(
35e7b 69 53 75 62 32 2c 20 70 50 61 72 73 65 2d 3e 69  iSub2, pParse->i
35e7c 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0d 0a  NextSelectId);..
35e7d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
35e7e 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74  pParse, p, &dest
35e7f 42 29 3b 0d 0a 20 20 70 2d 3e 69 4c 69 6d 69 74  B);..  p->iLimit
35e80 20 3d 20 73 61 76 65 64 4c 69 6d 69 74 3b 0d 0a   = savedLimit;..
35e81 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 73    p->iOffset = s
35e82 61 76 65 64 4f 66 66 73 65 74 3b 0d 0a 20 20 73  avedOffset;..  s
35e83 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
35e84 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
35e85 31 2c 20 72 65 67 45 6f 66 42 29 3b 0d 0a 20 20  1, regEofB);..  
35e86 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35e87 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
35e88 65 67 41 64 64 72 42 29 3b 0d 0a 20 20 56 64 62  egAddrB);..  Vdb
35e89 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
35e8a 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20   "End coroutine 
35e8b 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54  for right SELECT
35e8c 22 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e  "));....  /* Gen
35e8d 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
35e8e 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
35e8f 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
35e90 6f 66 20 74 68 65 20 41 0d 0a 20 20 2a 2a 20 73  of the A..  ** s
35e91 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78  elect as the nex
35e92 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20  t output row of 
35e93 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
35e94 65 63 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 56 64  ect...  */..  Vd
35e95 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
35e96 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
35e97 65 20 66 6f 72 20 41 22 29 29 3b 0d 0a 20 20 61  e for A"));..  a
35e98 64 64 72 4f 75 74 41 20 3d 20 67 65 6e 65 72 61  ddrOutA = genera
35e99 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69  teOutputSubrouti
35e9a 6e 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20 20  ne(pParse,..    
35e9b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20               p, 
35e9c 26 64 65 73 74 41 2c 20 70 44 65 73 74 2c 20 72  &destA, pDest, r
35e9d 65 67 4f 75 74 41 2c 0d 0a 20 20 20 20 20 20 20  egOutA,..       
35e9e 20 20 20 20 20 20 20 20 20 20 72 65 67 50 72 65            regPre
35e9f 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b  v, pKeyDup, P4_K
35ea0 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20  EYINFO_HANDOFF, 
35ea1 6c 61 62 65 6c 45 6e 64 29 3b 0d 0a 20 20 0d 0a  labelEnd);..  ..
35ea2 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
35ea3 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
35ea4 6f 75 74 70 75 74 73 20 74 68 65 20 63 75 72 72  outputs the curr
35ea5 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 42  ent row of the B
35ea6 0d 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 61 73  ..  ** select as
35ea7 20 74 68 65 20 6e 65 78 74 20 6f 75 74 70 75 74   the next output
35ea8 20 72 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d 70   row of the comp
35ea9 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0d 0a 20 20  ound select...  
35eaa 2a 2f 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  */..  if( op==TK
35eab 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55  _ALL || op==TK_U
35eac 4e 49 4f 4e 20 29 7b 0d 0a 20 20 20 20 56 64 62  NION ){..    Vdb
35ead 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
35eae 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65   "Output routine
35eaf 20 66 6f 72 20 42 22 29 29 3b 0d 0a 20 20 20 20   for B"));..    
35eb0 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72  addrOutB = gener
35eb1 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
35eb2 69 6e 65 28 70 50 61 72 73 65 2c 0d 0a 20 20 20  ine(pParse,..   
35eb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c                p,
35eb4 20 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20   &destB, pDest, 
35eb5 72 65 67 4f 75 74 42 2c 0d 0a 20 20 20 20 20 20  regOutB,..      
35eb6 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50 72             regPr
35eb7 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f  ev, pKeyDup, P4_
35eb8 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20  KEYINFO_STATIC, 
35eb9 6c 61 62 65 6c 45 6e 64 29 3b 0d 0a 20 20 7d 0d  labelEnd);..  }.
35eba 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ...  /* Generate
35ebb 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f   a subroutine to
35ebc 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65   run when the re
35ebd 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63  sults from selec
35ebe 74 20 41 0d 0a 20 20 2a 2a 20 61 72 65 20 65 78  t A..  ** are ex
35ebf 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79  hausted and only
35ec0 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20   data in select 
35ec1 42 20 72 65 6d 61 69 6e 73 2e 0d 0a 20 20 2a 2f  B remains...  */
35ec2 0d 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ..  VdbeNoopComm
35ec3 65 6e 74 28 28 76 2c 20 22 65 6f 66 2d 41 20 73  ent((v, "eof-A s
35ec4 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0d 0a 20  ubroutine"));.. 
35ec5 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 45 58 43 45   if( op==TK_EXCE
35ec6 50 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54  PT || op==TK_INT
35ec7 45 52 53 45 43 54 20 29 7b 0d 0a 20 20 20 20 61  ERSECT ){..    a
35ec8 64 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65  ddrEofA = sqlite
35ec9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35eca 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
35ecb 45 6e 64 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 20  End);..  }else{ 
35ecc 20 0d 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20   ..    addrEofA 
35ecd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
35ece 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65  Op2(v, OP_If, re
35ecf 67 45 6f 66 42 2c 20 6c 61 62 65 6c 45 6e 64 29  gEofB, labelEnd)
35ed0 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
35ed1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
35ed2 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61  osub, regOutB, a
35ed3 64 64 72 4f 75 74 42 29 3b 0d 0a 20 20 20 20 73  ddrOutB);..    s
35ed4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
35ed5 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
35ed6 67 41 64 64 72 42 29 3b 0d 0a 20 20 20 20 73 71  gAddrB);..    sq
35ed7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35ed8 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
35ed9 64 64 72 45 6f 66 41 29 3b 0d 0a 20 20 20 20 70  ddrEofA);..    p
35eda 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 2b 3d 20  ->nSelectRow += 
35edb 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52  pPrior->nSelectR
35edc 6f 77 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ow;..  }....  /*
35edd 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
35ede 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77 68  outine to run wh
35edf 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 66  en the results f
35ee0 72 6f 6d 20 73 65 6c 65 63 74 20 42 0d 0a 20 20  rom select B..  
35ee1 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65 64  ** are exhausted
35ee2 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69   and only data i
35ee3 6e 20 73 65 6c 65 63 74 20 41 20 72 65 6d 61 69  n select A remai
35ee4 6e 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ns...  */..  if(
35ee5 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43   op==TK_INTERSEC
35ee6 54 20 29 7b 0d 0a 20 20 20 20 61 64 64 72 45 6f  T ){..    addrEo
35ee7 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0d 0a  fB = addrEofA;..
35ee8 20 20 20 20 69 66 28 20 70 2d 3e 6e 53 65 6c 65      if( p->nSele
35ee9 63 74 52 6f 77 20 3e 20 70 50 72 69 6f 72 2d 3e  ctRow > pPrior->
35eea 6e 53 65 6c 65 63 74 52 6f 77 20 29 20 70 2d 3e  nSelectRow ) p->
35eeb 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 70 50 72  nSelectRow = pPr
35eec 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 3b  ior->nSelectRow;
35eed 0d 0a 20 20 7d 65 6c 73 65 7b 20 20 0d 0a 20 20  ..  }else{  ..  
35eee 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
35eef 74 28 28 76 2c 20 22 65 6f 66 2d 42 20 73 75 62  t((v, "eof-B sub
35ef0 72 6f 75 74 69 6e 65 22 29 29 3b 0d 0a 20 20 20  routine"));..   
35ef1 20 61 64 64 72 45 6f 66 42 20 3d 20 73 71 6c 69   addrEofB = sqli
35ef2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
35ef3 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c   OP_If, regEofA,
35ef4 20 6c 61 62 65 6c 45 6e 64 29 3b 0d 0a 20 20 20   labelEnd);..   
35ef5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
35ef6 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
35ef7 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74  regOutA, addrOut
35ef8 41 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  A);..    sqlite3
35ef9 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
35efa 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41  _Yield, regAddrA
35efb 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
35efc 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
35efd 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66  Goto, 0, addrEof
35efe 42 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  B);..  }....  /*
35eff 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
35f00 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73  o handle the cas
35f01 65 20 6f 66 20 41 3c 42 0d 0a 20 20 2a 2f 0d 0a  e of A<B..  */..
35f02 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
35f03 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20 73 75  t((v, "A-lt-B su
35f04 62 72 6f 75 74 69 6e 65 22 29 29 3b 0d 0a 20 20  broutine"));..  
35f05 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69 74  addrAltB = sqlit
35f06 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
35f07 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
35f08 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0d 0a 20  A, addrOutA);.. 
35f09 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
35f0a 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
35f0b 72 65 67 41 64 64 72 41 29 3b 0d 0a 20 20 73 71  regAddrA);..  sq
35f0c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35f0d 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
35f0e 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0d 0a 20  A, addrEofA);.. 
35f0f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
35f10 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
35f11 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0d 0a 0d  , labelCmpr);...
35f12 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
35f13 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
35f14 65 20 63 61 73 65 20 6f 66 20 41 3d 3d 42 0d 0a  e case of A==B..
35f15 20 20 2a 2f 0d 0a 20 20 69 66 28 20 6f 70 3d 3d    */..  if( op==
35f16 54 4b 5f 41 4c 4c 20 29 7b 0d 0a 20 20 20 20 61  TK_ALL ){..    a
35f17 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c  ddrAeqB = addrAl
35f18 74 42 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  tB;..  }else if(
35f19 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43   op==TK_INTERSEC
35f1a 54 20 29 7b 0d 0a 20 20 20 20 61 64 64 72 41 65  T ){..    addrAe
35f1b 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0d 0a  qB = addrAltB;..
35f1c 20 20 20 20 61 64 64 72 41 6c 74 42 2b 2b 3b 0d      addrAltB++;.
35f1d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 56  .  }else{..    V
35f1e 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
35f1f 76 2c 20 22 41 2d 65 71 2d 42 20 73 75 62 72 6f  v, "A-eq-B subro
35f20 75 74 69 6e 65 22 29 29 3b 0d 0a 20 20 20 20 61  utine"));..    a
35f21 64 64 72 41 65 71 42 20 3d 0d 0a 20 20 20 20 73  ddrAeqB =..    s
35f22 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
35f23 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
35f24 67 41 64 64 72 41 29 3b 0d 0a 20 20 20 20 73 71  gAddrA);..    sq
35f25 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35f26 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
35f27 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0d 0a 20  A, addrEofA);.. 
35f28 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
35f29 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
35f2a 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0d   0, labelCmpr);.
35f2b 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e  .  }....  /* Gen
35f2c 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61  erate code to ha
35f2d 6e 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66  ndle the case of
35f2e 20 41 3e 42 0d 0a 20 20 2a 2f 0d 0a 20 20 56 64   A>B..  */..  Vd
35f2f 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
35f30 2c 20 22 41 2d 67 74 2d 42 20 73 75 62 72 6f 75  , "A-gt-B subrou
35f31 74 69 6e 65 22 29 29 3b 0d 0a 20 20 61 64 64 72  tine"));..  addr
35f32 41 67 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64  AgtB = sqlite3Vd
35f33 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
35f34 3b 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  ;..  if( op==TK_
35f35 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e  ALL || op==TK_UN
35f36 49 4f 4e 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ION ){..    sqli
35f37 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
35f38 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
35f39 74 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0d 0a  tB, addrOutB);..
35f3a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 56 64    }..  sqlite3Vd
35f3b 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
35f3c 69 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b  ield, regAddrB);
35f3d 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
35f3e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
35f3f 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f 66  regEofB, addrEof
35f40 42 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  B);..  sqlite3Vd
35f41 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
35f42 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70  oto, 0, labelCmp
35f43 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73  r);....  /* This
35f44 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e 63 65 20   code runs once 
35f45 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 65 76  to initialize ev
35f46 65 72 79 74 68 69 6e 67 2e 0d 0a 20 20 2a 2f 0d  erything...  */.
35f47 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
35f48 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0d 0a  mpHere(v, j1);..
35f49 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
35f4a 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
35f4b 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0d  r, 0, regEofA);.
35f4c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
35f4d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
35f4e 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b  er, 0, regEofB);
35f4f 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
35f50 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
35f51 62 2c 20 72 65 67 41 64 64 72 41 2c 20 61 64 64  b, regAddrA, add
35f52 72 53 65 6c 65 63 74 41 29 3b 0d 0a 20 20 73 71  rSelectA);..  sq
35f53 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35f54 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
35f55 41 64 64 72 42 2c 20 61 64 64 72 53 65 6c 65 63  AddrB, addrSelec
35f56 74 42 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56  tB);..  sqlite3V
35f57 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
35f58 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64  If, regEofA, add
35f59 72 45 6f 66 41 29 3b 0d 0a 20 20 73 71 6c 69 74  rEofA);..  sqlit
35f5a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
35f5b 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20  OP_If, regEofB, 
35f5c 61 64 64 72 45 6f 66 42 29 3b 0d 0a 0d 0a 20 20  addrEofB);....  
35f5d 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 74 68 65  /* Implement the
35f5e 20 6d 61 69 6e 20 6d 65 72 67 65 20 6c 6f 6f 70   main merge loop
35f5f 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
35f60 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
35f61 6c 28 76 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b  l(v, labelCmpr);
35f62 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
35f63 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 65 72 6d  ddOp4(v, OP_Perm
35f64 75 74 61 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30  utation, 0, 0, 0
35f65 2c 20 28 63 68 61 72 2a 29 61 50 65 72 6d 75 74  , (char*)aPermut
35f66 65 2c 20 50 34 5f 49 4e 54 41 52 52 41 59 29 3b  e, P4_INTARRAY);
35f67 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
35f68 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70  ddOp4(v, OP_Comp
35f69 61 72 65 2c 20 64 65 73 74 41 2e 69 4d 65 6d 2c  are, destA.iMem,
35f6a 20 64 65 73 74 42 2e 69 4d 65 6d 2c 20 6e 4f 72   destB.iMem, nOr
35f6b 64 65 72 42 79 2c 0d 0a 20 20 20 20 20 20 20 20  derBy,..        
35f6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f6d 20 28 63 68 61 72 2a 29 70 4b 65 79 4d 65 72 67   (char*)pKeyMerg
35f6e 65 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  e, P4_KEYINFO_HA
35f6f 4e 44 4f 46 46 29 3b 0d 0a 20 20 73 71 6c 69 74  NDOFF);..  sqlit
35f70 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
35f71 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72 41 6c 74  OP_Jump, addrAlt
35f72 42 2c 20 61 64 64 72 41 65 71 42 2c 20 61 64 64  B, addrAeqB, add
35f73 72 41 67 74 42 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  rAgtB);....  /* 
35f74 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61 72  Release temporar
35f75 79 20 72 65 67 69 73 74 65 72 73 0d 0a 20 20 2a  y registers..  *
35f76 2f 0d 0a 20 20 69 66 28 20 72 65 67 50 72 65 76  /..  if( regPrev
35f77 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
35f78 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
35f79 28 70 50 61 72 73 65 2c 20 72 65 67 50 72 65 76  (pParse, regPrev
35f7a 2c 20 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0d 0a  , nOrderBy+1);..
35f7b 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70    }....  /* Jump
35f7c 20 74 6f 20 74 68 65 20 74 68 69 73 20 70 6f 69   to the this poi
35f7d 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74  nt in order to t
35f7e 65 72 6d 69 6e 61 74 65 20 74 68 65 20 71 75 65  erminate the que
35f7f 72 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  ry...  */..  sql
35f80 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
35f81 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c 45 6e 64  abel(v, labelEnd
35f82 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 65 74 20 74  );....  /* Set t
35f83 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  he number of out
35f84 70 75 74 20 63 6f 6c 75 6d 6e 73 0d 0a 20 20 2a  put columns..  *
35f85 2f 0d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e  /..  if( pDest->
35f86 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
35f87 74 20 29 7b 0d 0a 20 20 20 20 53 65 6c 65 63 74  t ){..    Select
35f88 20 2a 70 46 69 72 73 74 20 3d 20 70 50 72 69 6f   *pFirst = pPrio
35f89 72 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 70  r;..    while( p
35f8a 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20  First->pPrior ) 
35f8b 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d  pFirst = pFirst-
35f8c 3e 70 50 72 69 6f 72 3b 0d 0a 20 20 20 20 67 65  >pPrior;..    ge
35f8d 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
35f8e 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69  s(pParse, 0, pFi
35f8f 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a 20  rst->pEList);.. 
35f90 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 61 73 73   }....  /* Reass
35f91 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75  embly the compou
35f92 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68 61 74  nd query so that
35f93 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65   it will be free
35f94 64 20 63 6f 72 72 65 63 74 6c 79 0d 0a 20 20 2a  d correctly..  *
35f95 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  * by the calling
35f96 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20   function */..  
35f97 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b  if( p->pPrior ){
35f98 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ..    sqlite3Sel
35f99 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  ectDelete(db, p-
35f9a 3e 70 50 72 69 6f 72 29 3b 0d 0a 20 20 7d 0d 0a  >pPrior);..  }..
35f9b 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50    p->pPrior = pP
35f9c 72 69 6f 72 3b 0d 0a 0d 0a 20 20 2f 2a 2a 2a 20  rior;....  /*** 
35f9d 54 42 44 3a 20 20 49 6e 73 65 72 74 20 73 75 62  TBD:  Insert sub
35f9e 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 6f  routine calls to
35f9f 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 20 6f   close cursors o
35fa0 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 0d 0a 20 20  n incomplete..  
35fa1 2a 2a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20  **** subqueries 
35fa2 2a 2a 2a 2a 2f 0d 0a 20 20 65 78 70 6c 61 69 6e  ****/..  explain
35fa3 43 6f 6d 70 6f 73 69 74 65 28 70 50 61 72 73 65  Composite(pParse
35fa4 2c 20 70 2d 3e 6f 70 2c 20 69 53 75 62 31 2c 20  , p->op, iSub1, 
35fa5 69 53 75 62 32 2c 20 30 29 3b 0d 0a 20 20 72 65  iSub2, 0);..  re
35fa6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
35fa7 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69  .}..#endif....#i
35fa8 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
35fa9 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
35faa 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
35fab 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0d 0a  ITE_OMIT_VIEW)..
35fac 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61  /* Forward Decla
35fad 72 61 74 69 6f 6e 73 20 2a 2f 0d 0a 73 74 61 74  rations */..stat
35fae 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70  ic void substExp
35faf 72 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 2c 20  rList(sqlite3*, 
35fb0 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20  ExprList*, int, 
35fb1 45 78 70 72 4c 69 73 74 2a 29 3b 0d 0a 73 74 61  ExprList*);..sta
35fb2 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65  tic void substSe
35fb3 6c 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 20 53  lect(sqlite3*, S
35fb4 65 6c 65 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78  elect *, int, Ex
35fb5 70 72 4c 69 73 74 20 2a 29 3b 0d 0a 0d 0a 2f 2a  prList *);..../*
35fb6 0d 0a 2a 2a 20 53 63 61 6e 20 74 68 72 6f 75 67  ..** Scan throug
35fb7 68 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  h the expression
35fb8 20 70 45 78 70 72 2e 20 20 52 65 70 6c 61 63 65   pExpr.  Replace
35fb9 20 65 76 65 72 79 20 72 65 66 65 72 65 6e 63 65   every reference
35fba 20 74 6f 0d 0a 2a 2a 20 61 20 63 6f 6c 75 6d 6e   to..** a column
35fbb 20 69 6e 20 74 61 62 6c 65 20 6e 75 6d 62 65 72   in table number
35fbc 20 69 54 61 62 6c 65 20 77 69 74 68 20 61 20 63   iTable with a c
35fbd 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 75  opy of the iColu
35fbe 6d 6e 2d 74 68 0d 0a 2a 2a 20 65 6e 74 72 79 20  mn-th..** entry 
35fbf 69 6e 20 70 45 4c 69 73 74 2e 20 20 28 42 75 74  in pEList.  (But
35fc0 20 6c 65 61 76 65 20 72 65 66 65 72 65 6e 63 65   leave reference
35fc1 73 20 74 6f 20 74 68 65 20 52 4f 57 49 44 20 63  s to the ROWID c
35fc2 6f 6c 75 6d 6e 20 0d 0a 2a 2a 20 75 6e 63 68 61  olumn ..** uncha
35fc3 6e 67 65 64 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54  nged.)..**..** T
35fc4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70  his routine is p
35fc5 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74  art of the flatt
35fc6 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e  ening procedure.
35fc7 20 20 41 20 73 75 62 71 75 65 72 79 0d 0a 2a 2a    A subquery..**
35fc8 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65   whose result se
35fc9 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  t is defined by 
35fca 70 45 4c 69 73 74 20 61 70 70 65 61 72 73 20 61  pEList appears a
35fcb 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0d 0a  s entry in the..
35fcc 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ** FROM clause o
35fcd 66 20 61 20 53 45 4c 45 43 54 20 73 75 63 68 20  f a SELECT such 
35fce 74 68 61 74 20 74 68 65 20 56 44 42 45 20 63 75  that the VDBE cu
35fcf 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
35fd0 20 74 68 61 74 0d 0a 2a 2a 20 46 4f 52 4d 20 63   that..** FORM c
35fd1 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73 20 69  lause entry is i
35fd2 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75  Table.  This rou
35fd3 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65  tine make the ne
35fd4 63 65 73 73 61 72 79 20 0d 0a 2a 2a 20 63 68 61  cessary ..** cha
35fd5 6e 67 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f  nges to pExpr so
35fd6 20 74 68 61 74 20 69 74 20 72 65 66 65 72 73 20   that it refers 
35fd7 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20  directly to the 
35fd8 73 6f 75 72 63 65 20 74 61 62 6c 65 0d 0a 2a 2a  source table..**
35fd9 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
35fda 20 72 61 74 68 65 72 20 74 68 65 20 72 65 73 75   rather the resu
35fdb 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 73 75  lt set of the su
35fdc 62 71 75 65 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61  bquery...*/..sta
35fdd 74 69 63 20 45 78 70 72 20 2a 73 75 62 73 74 45  tic Expr *substE
35fde 78 70 72 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  xpr(..  sqlite3 
35fdf 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 52  *db,        /* R
35fe0 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
35fe1 6f 72 73 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e  ors to this conn
35fe2 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 45 78 70  ection */..  Exp
35fe3 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
35fe4 20 2f 2a 20 45 78 70 72 20 69 6e 20 77 68 69 63   /* Expr in whic
35fe5 68 20 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f  h substitution o
35fe6 63 63 75 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  ccurs */..  int 
35fe7 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
35fe8 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73  /* Table to be s
35fe9 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0d 0a 20  ubstituted */.. 
35fea 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
35feb 74 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75  t    /* Substitu
35fec 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a  te expressions *
35fed 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 70 45 78  /..){..  if( pEx
35fee 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  pr==0 ) return 0
35fef 3b 0d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  ;..  if( pExpr->
35ff0 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
35ff1 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
35ff2 69 54 61 62 6c 65 20 29 7b 0d 0a 20 20 20 20 69  iTable ){..    i
35ff3 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
35ff4 6e 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 45  n<0 ){..      pE
35ff5 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c  xpr->op = TK_NUL
35ff6 4c 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  L;..    }else{..
35ff7 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
35ff8 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
35ff9 20 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45   pEList!=0 && pE
35ffa 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c  xpr->iColumn<pEL
35ffb 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0d 0a 20  ist->nExpr );.. 
35ffc 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
35ffd 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20  pr->pLeft==0 && 
35ffe 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30  pExpr->pRight==0
35fff 20 29 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77 20   );..      pNew 
36000 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
36001 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 70  (db, pEList->a[p
36002 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70  Expr->iColumn].p
36003 45 78 70 72 2c 20 30 29 3b 0d 0a 20 20 20 20 20  Expr, 0);..     
36004 20 69 66 28 20 70 4e 65 77 20 26 26 20 70 45 78   if( pNew && pEx
36005 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0d 0a 20 20  pr->pColl ){..  
36006 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c        pNew->pCol
36007 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  l = pExpr->pColl
36008 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
36009 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
3600a 65 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0d  ete(db, pExpr);.
3600b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70  .      pExpr = p
3600c 4e 65 77 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  New;..    }..  }
3600d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 45 78 70 72  else{..    pExpr
3600e 2d 3e 70 4c 65 66 74 20 3d 20 73 75 62 73 74 45  ->pLeft = substE
3600f 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e 70  xpr(db, pExpr->p
36010 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  Left, iTable, pE
36011 4c 69 73 74 29 3b 0d 0a 20 20 20 20 70 45 78 70  List);..    pExp
36012 72 2d 3e 70 52 69 67 68 74 20 3d 20 73 75 62 73  r->pRight = subs
36013 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d  tExpr(db, pExpr-
36014 3e 70 52 69 67 68 74 2c 20 69 54 61 62 6c 65 2c  >pRight, iTable,
36015 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 69   pEList);..    i
36016 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
36017 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
36018 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20 20 20 20  Select) ){..    
36019 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62    substSelect(db
3601a 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
3601b 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  ct, iTable, pELi
3601c 73 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  st);..    }else{
3601d 0d 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  ..      substExp
3601e 72 4c 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d  rList(db, pExpr-
3601f 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65  >x.pList, iTable
36020 2c 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20  , pEList);..    
36021 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
36022 20 70 45 78 70 72 3b 0d 0a 7d 0d 0a 73 74 61 74   pExpr;..}..stat
36023 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70  ic void substExp
36024 72 4c 69 73 74 28 0d 0a 20 20 73 71 6c 69 74 65  rList(..  sqlite
36025 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
36026 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20  * Report malloc 
36027 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0d 0a  errors here */..
36028 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
36029 74 2c 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74  t,     /* List t
3602a 6f 20 73 63 61 6e 20 61 6e 64 20 69 6e 20 77 68  o scan and in wh
3602b 69 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73  ich to make subs
3602c 74 69 74 75 74 65 73 20 2a 2f 0d 0a 20 20 69 6e  titutes */..  in
3602d 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
3602e 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62     /* Table to b
3602f 65 20 73 75 62 73 74 69 74 75 74 65 64 20 2a 2f  e substituted */
36030 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
36031 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73  List     /* Subs
36032 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f  titute values */
36033 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  ..){..  int i;..
36034 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
36035 20 72 65 74 75 72 6e 3b 0d 0a 20 20 66 6f 72 28   return;..  for(
36036 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
36037 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  xpr; i++){..    
36038 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
36039 72 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  r = substExpr(db
3603a 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
3603b 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  xpr, iTable, pEL
3603c 69 73 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73  ist);..  }..}..s
3603d 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74  tatic void subst
3603e 53 65 6c 65 63 74 28 0d 0a 20 20 73 71 6c 69 74  Select(..  sqlit
3603f 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
36040 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63  /* Report malloc
36041 20 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0d   errors here */.
36042 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
36043 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
36044 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 77  T statement in w
36045 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62  hich to make sub
36046 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f 0d 0a 20  stitutions */.. 
36047 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
36048 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
36049 6f 20 62 65 20 72 65 70 6c 61 63 65 64 20 2a 2f  o be replaced */
3604a 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
3604b 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73  List     /* Subs
3604c 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f  titute values */
3604d 0d 0a 29 7b 0d 0a 20 20 53 72 63 4c 69 73 74 20  ..){..  SrcList 
3604e 2a 70 53 72 63 3b 0d 0a 20 20 73 74 72 75 63 74  *pSrc;..  struct
3604f 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
36050 49 74 65 6d 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d  Item;..  int i;.
36051 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75  .  if( !p ) retu
36052 72 6e 3b 0d 0a 20 20 73 75 62 73 74 45 78 70 72  rn;..  substExpr
36053 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 45 4c 69  List(db, p->pELi
36054 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  st, iTable, pELi
36055 73 74 29 3b 0d 0a 20 20 73 75 62 73 74 45 78 70  st);..  substExp
36056 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 47 72  rList(db, p->pGr
36057 6f 75 70 42 79 2c 20 69 54 61 62 6c 65 2c 20 70  oupBy, iTable, p
36058 45 4c 69 73 74 29 3b 0d 0a 20 20 73 75 62 73 74  EList);..  subst
36059 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e  ExprList(db, p->
3605a 70 4f 72 64 65 72 42 79 2c 20 69 54 61 62 6c 65  pOrderBy, iTable
3605b 2c 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 70 2d  , pEList);..  p-
3605c 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74  >pHaving = subst
3605d 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48 61 76  Expr(db, p->pHav
3605e 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ing, iTable, pEL
3605f 69 73 74 29 3b 0d 0a 20 20 70 2d 3e 70 57 68 65  ist);..  p->pWhe
36060 72 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64  re = substExpr(d
36061 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69 54  b, p->pWhere, iT
36062 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0d 0a  able, pEList);..
36063 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62    substSelect(db
36064 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61  , p->pPrior, iTa
36065 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0d 0a 20  ble, pEList);.. 
36066 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b   pSrc = p->pSrc;
36067 0d 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63  ..  assert( pSrc
36068 20 29 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f 72   );  /* Even for
36069 20 28 53 45 4c 45 43 54 20 31 29 20 77 65 20 68   (SELECT 1) we h
3606a 61 76 65 3a 20 70 53 72 63 21 3d 30 20 62 75 74  ave: pSrc!=0 but
3606b 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 2a   pSrc->nSrc==0 *
3606c 2f 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  /..  if( ALWAYS(
3606d 70 53 72 63 29 20 29 7b 0d 0a 20 20 20 20 66 6f  pSrc) ){..    fo
3606e 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20  r(i=pSrc->nSrc, 
3606f 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69  pItem=pSrc->a; i
36070 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
36071 29 7b 0d 0a 20 20 20 20 20 20 73 75 62 73 74 53  ){..      substS
36072 65 6c 65 63 74 28 64 62 2c 20 70 49 74 65 6d 2d  elect(db, pItem-
36073 3e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65  >pSelect, iTable
36074 2c 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20  , pEList);..    
36075 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69  }..  }..}..#endi
36076 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
36077 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
36078 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
36079 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
3607a 29 20 2a 2f 0d 0a 0d 0a 23 69 66 20 21 64 65 66  ) */....#if !def
3607b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
3607c 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
3607d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
3607e 49 54 5f 56 49 45 57 29 0d 0a 2f 2a 0d 0a 2a 2a  IT_VIEW)../*..**
3607f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74   This routine at
36080 74 65 6d 70 74 73 20 74 6f 20 66 6c 61 74 74 65  tempts to flatte
36081 6e 20 73 75 62 71 75 65 72 69 65 73 20 61 73 20  n subqueries as 
36082 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 70  a performance op
36083 74 69 6d 69 7a 61 74 69 6f 6e 2e 0d 0a 2a 2a 20  timization...** 
36084 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
36085 75 72 6e 73 20 31 20 69 66 20 69 74 20 6d 61 6b  urns 1 if it mak
36086 65 73 20 63 68 61 6e 67 65 73 20 61 6e 64 20 30  es changes and 0
36087 20 69 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e   if no flattenin
36088 67 20 6f 63 63 75 72 73 2e 0d 0a 2a 2a 0d 0a 2a  g occurs...**..*
36089 2a 20 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20  * To understand 
3608a 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66  the concept of f
3608b 6c 61 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69  lattening, consi
3608c 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  der the followin
3608d 67 0d 0a 2a 2a 20 71 75 65 72 79 3a 0d 0a 2a 2a  g..** query:..**
3608e 0d 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  ..**     SELECT 
3608f 61 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78  a FROM (SELECT x
36090 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20  +y AS a FROM t1 
36091 57 48 45 52 45 20 7a 3c 31 30 30 29 20 57 48 45  WHERE z<100) WHE
36092 52 45 20 61 3e 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54  RE a>5..**..** T
36093 68 65 20 64 65 66 61 75 6c 74 20 77 61 79 20 6f  he default way o
36094 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74  f implementing t
36095 68 69 73 20 71 75 65 72 79 20 69 73 20 74 6f 20  his query is to 
36096 65 78 65 63 75 74 65 20 74 68 65 0d 0a 2a 2a 20  execute the..** 
36097 73 75 62 71 75 65 72 79 20 66 69 72 73 74 20 61  subquery first a
36098 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
36099 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f 72  ults in a tempor
3609a 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 0d  ary table, then.
3609b 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74 65  .** run the oute
3609c 72 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74 20  r query on that 
3609d 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
3609e 20 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20    This requires 
3609f 74 77 6f 0d 0a 2a 2a 20 70 61 73 73 65 73 20 6f  two..** passes o
360a0 76 65 72 20 74 68 65 20 64 61 74 61 2e 20 20 46  ver the data.  F
360a1 75 72 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61  urthermore, beca
360a2 75 73 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  use the temporar
360a3 79 20 74 61 62 6c 65 0d 0a 2a 2a 20 68 61 73 20  y table..** has 
360a4 6e 6f 20 69 6e 64 69 63 65 73 2c 20 74 68 65 20  no indices, the 
360a5 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 6e 20  WHERE clause on 
360a6 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
360a7 63 61 6e 6e 6f 74 20 62 65 0d 0a 2a 2a 20 6f 70  cannot be..** op
360a8 74 69 6d 69 7a 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  timized...**..**
360a9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74   This routine at
360aa 74 65 6d 70 74 73 20 74 6f 20 72 65 77 72 69 74  tempts to rewrit
360ab 65 20 71 75 65 72 69 65 73 20 73 75 63 68 20 61  e queries such a
360ac 73 20 74 68 65 20 61 62 6f 76 65 20 69 6e 74 6f  s the above into
360ad 0d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66 6c  ..** a single fl
360ae 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65 20  at select, like 
360af 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  this:..**..**   
360b0 20 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20    SELECT x+y AS 
360b1 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  a FROM t1 WHERE 
360b2 7a 3c 31 30 30 20 41 4e 44 20 61 3e 35 0d 0a 2a  z<100 AND a>5..*
360b3 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  *..** The code g
360b4 65 6e 65 72 61 74 65 64 20 66 6f 72 20 74 68 69  enerated for thi
360b5 73 20 73 69 6d 70 69 66 69 63 61 74 69 6f 6e 20  s simpification 
360b6 67 69 76 65 73 20 74 68 65 20 73 61 6d 65 20 72  gives the same r
360b7 65 73 75 6c 74 0d 0a 2a 2a 20 62 75 74 20 6f 6e  esult..** but on
360b8 6c 79 20 68 61 73 20 74 6f 20 73 63 61 6e 20 74  ly has to scan t
360b9 68 65 20 64 61 74 61 20 6f 6e 63 65 2e 20 20 41  he data once.  A
360ba 6e 64 20 62 65 63 61 75 73 65 20 69 6e 64 69 63  nd because indic
360bb 65 73 20 6d 69 67 68 74 20 0d 0a 2a 2a 20 65 78  es might ..** ex
360bc 69 73 74 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ist on the table
360bd 20 74 31 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20   t1, a complete 
360be 73 63 61 6e 20 6f 66 20 74 68 65 20 64 61 74 61  scan of the data
360bf 20 6d 69 67 68 74 20 62 65 0d 0a 2a 2a 20 61 76   might be..** av
360c0 6f 69 64 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46  oided...**..** F
360c1 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f 6e 6c  lattening is onl
360c2 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 61  y attempted if a
360c3 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
360c4 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0d 0a 2a  ing are true:..*
360c5 2a 0d 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65  *..**   (1)  The
360c6 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68   subquery and th
360c7 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
360c8 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67   not both use ag
360c9 67 72 65 67 61 74 65 73 2e 0d 0a 2a 2a 0d 0a 2a  gregates...**..*
360ca 2a 20 20 20 28 32 29 20 20 54 68 65 20 73 75 62  *   (2)  The sub
360cb 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20  query is not an 
360cc 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65  aggregate or the
360cd 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
360ce 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0d 0a 2a 2a 0d  not a join...**.
360cf 0a 2a 2a 20 20 20 28 33 29 20 20 54 68 65 20 73  .**   (3)  The s
360d0 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 74  ubquery is not t
360d1 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  he right operand
360d2 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72   of a left outer
360d3 20 6a 6f 69 6e 0d 0a 2a 2a 20 20 20 20 20 20 20   join..**       
360d4 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69 63   (Originally tic
360d5 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72 65 6e  ket #306.  Stren
360d6 67 74 68 65 6e 65 64 20 62 79 20 74 69 63 6b 65  gthened by ticke
360d7 74 20 23 33 33 30 30 29 0d 0a 2a 2a 0d 0a 2a 2a  t #3300)..**..**
360d8 20 20 20 28 34 29 20 20 54 68 65 20 73 75 62 71     (4)  The subq
360d9 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54  uery is not DIST
360da 49 4e 43 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 28  INCT...**..**  (
360db 2a 2a 29 20 20 41 74 20 6f 6e 65 20 70 6f 69 6e  **)  At one poin
360dc 74 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 28  t restrictions (
360dd 34 29 20 61 6e 64 20 28 35 29 20 64 65 66 69 6e  4) and (5) defin
360de 65 64 20 61 20 73 75 62 73 65 74 20 6f 66 20 44  ed a subset of D
360df 49 53 54 49 4e 43 54 0d 0a 2a 2a 20 20 20 20 20  ISTINCT..**     
360e0 20 20 20 73 75 62 2d 71 75 65 72 69 65 73 20 74     sub-queries t
360e1 68 61 74 20 77 65 72 65 20 65 78 63 6c 75 64 65  hat were exclude
360e2 64 20 66 72 6f 6d 20 74 68 69 73 20 6f 70 74 69  d from this opti
360e3 6d 69 7a 61 74 69 6f 6e 2e 20 52 65 73 74 72 69  mization. Restri
360e4 63 74 69 6f 6e 20 0d 0a 2a 2a 20 20 20 20 20 20  ction ..**      
360e5 20 20 28 34 29 20 68 61 73 20 73 69 6e 63 65 20    (4) has since 
360e6 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 74 6f  been expanded to
360e7 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 44 49 53   exclude all DIS
360e8 54 49 4e 43 54 20 73 75 62 71 75 65 72 69 65 73  TINCT subqueries
360e9 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28 36 29 20  ...**..**   (6) 
360ea 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
360eb 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65  es not use aggre
360ec 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74  gates or the out
360ed 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 0d  er query is not.
360ee 0a 2a 2a 20 20 20 20 20 20 20 20 44 49 53 54 49  .**        DISTI
360ef 4e 43 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28  NCT...**..**   (
360f0 37 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  7)  The subquery
360f1 20 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61 75   has a FROM clau
360f2 73 65 2e 20 20 54 4f 44 4f 3a 20 20 46 6f 72 20  se.  TODO:  For 
360f3 73 75 62 71 75 65 72 69 65 73 20 77 69 74 68 6f  subqueries witho
360f4 75 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 41 20  ut..**        A 
360f5 46 52 4f 4d 20 63 6c 61 75 73 65 2c 20 63 6f 6e  FROM clause, con
360f6 73 69 64 65 72 20 61 64 64 69 6e 67 20 61 20 46  sider adding a F
360f7 52 4f 4d 20 63 6c 6f 73 65 20 77 69 74 68 20 74  ROM close with t
360f8 68 65 20 73 70 65 63 69 61 6c 0d 0a 2a 2a 20 20  he special..**  
360f9 20 20 20 20 20 20 74 61 62 6c 65 20 73 71 6c 69        table sqli
360fa 74 65 5f 6f 6e 63 65 20 74 68 61 74 20 63 6f 6e  te_once that con
360fb 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c  sists of a singl
360fc 65 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  e row containing
360fd 20 61 0d 0a 2a 2a 20 20 20 20 20 20 20 20 73 69   a..**        si
360fe 6e 67 6c 65 20 4e 55 4c 4c 2e 0d 0a 2a 2a 0d 0a  ngle NULL...**..
360ff 2a 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75  **   (8)  The su
36100 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
36101 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
36102 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
36103 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0d 0a 2a 2a 0d  not a join...**.
36104 0a 2a 2a 20 20 20 28 39 29 20 20 54 68 65 20 73  .**   (9)  The s
36105 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
36106 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68   use LIMIT or th
36107 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
36108 65 73 20 6e 6f 74 20 75 73 65 0d 0a 2a 2a 20 20  es not use..**  
36109 20 20 20 20 20 20 61 67 67 72 65 67 61 74 65 73        aggregates
3610a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 28 31 30 29 20  ...**..**  (10) 
3610b 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
3610c 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65  es not use aggre
3610d 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74  gates or the out
3610e 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  er query does no
3610f 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 65  t..**        use
36110 20 4c 49 4d 49 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   LIMIT...**..** 
36111 20 28 31 31 29 20 20 54 68 65 20 73 75 62 71 75   (11)  The subqu
36112 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65  ery and the oute
36113 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
36114 6f 74 68 20 68 61 76 65 20 4f 52 44 45 52 20 42  oth have ORDER B
36115 59 20 63 6c 61 75 73 65 73 2e 0d 0a 2a 2a 0d 0a  Y clauses...**..
36116 2a 2a 20 20 28 2a 2a 29 20 20 4e 6f 74 20 69 6d  **  (**)  Not im
36117 70 6c 65 6d 65 6e 74 65 64 2e 20 20 53 75 62 73  plemented.  Subs
36118 75 6d 65 64 20 69 6e 74 6f 20 72 65 73 74 72 69  umed into restri
36119 63 74 69 6f 6e 20 28 33 29 2e 20 20 57 61 73 20  ction (3).  Was 
3611a 70 72 65 76 69 6f 75 73 6c 79 0d 0a 2a 2a 20 20  previously..**  
3611b 20 20 20 20 20 20 61 20 73 65 70 61 72 61 74 65        a separate
3611c 20 72 65 73 74 72 69 63 74 69 6f 6e 20 64 65 72   restriction der
3611d 69 76 69 6e 67 20 66 72 6f 6d 20 74 69 63 6b 65  iving from ticke
3611e 74 20 23 33 35 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  t #350...**..** 
3611f 20 28 31 33 29 20 20 54 68 65 20 73 75 62 71 75   (13)  The subqu
36120 65 72 79 20 61 6e 64 20 6f 75 74 65 72 20 71 75  ery and outer qu
36121 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20  ery do not both 
36122 75 73 65 20 4c 49 4d 49 54 2e 0d 0a 2a 2a 0d 0a  use LIMIT...**..
36123 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20 73 75  **  (14)  The su
36124 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
36125 75 73 65 20 4f 46 46 53 45 54 2e 0d 0a 2a 2a 0d  use OFFSET...**.
36126 0a 2a 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f  .**  (15)  The o
36127 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
36128 74 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  t part of a comp
36129 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74  ound select or t
3612a 68 65 0d 0a 2a 2a 20 20 20 20 20 20 20 20 73 75  he..**        su
3612b 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
3612c 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61  have a LIMIT cla
3612d 75 73 65 2e 0d 0a 2a 2a 20 20 20 20 20 20 20 20  use...**        
3612e 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 33 33  (See ticket #233
3612f 39 20 61 6e 64 20 74 69 63 6b 65 74 20 5b 30 32  9 and ticket [02
36130 61 38 65 38 31 64 34 34 5d 29 2e 0d 0a 2a 2a 0d  a8e81d44])...**.
36131 0a 2a 2a 20 20 28 31 36 29 20 20 54 68 65 20 6f  .**  (16)  The o
36132 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
36133 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f  t an aggregate o
36134 72 20 74 68 65 20 73 75 62 71 75 65 72 79 20 64  r the subquery d
36135 6f 65 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 6e  oes..**        n
36136 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52  ot contain ORDER
36137 20 42 59 2e 20 20 28 54 69 63 6b 65 74 20 23 32   BY.  (Ticket #2
36138 39 34 32 29 20 20 54 68 69 73 20 75 73 65 64 20  942)  This used 
36139 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72 0d 0a 2a  to not matter..*
3613a 2a 20 20 20 20 20 20 20 20 75 6e 74 69 6c 20 77  *        until w
3613b 65 20 69 6e 74 72 6f 64 75 63 65 64 20 74 68 65  e introduced the
3613c 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 29 20   group_concat() 
3613d 66 75 6e 63 74 69 6f 6e 2e 20 20 0d 0a 2a 2a 0d  function.  ..**.
3613e 0a 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20 73  .**  (17)  The s
3613f 75 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74 20  ub-query is not 
36140 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
36141 74 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55 4e  t, or it is a UN
36142 49 4f 4e 20 41 4c 4c 20 0d 0a 2a 2a 20 20 20 20  ION ALL ..**    
36143 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61      compound cla
36144 75 73 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69  use made up enti
36145 72 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72  rely of non-aggr
36146 65 67 61 74 65 20 71 75 65 72 69 65 73 2c 20 61  egate queries, a
36147 6e 64 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20 74  nd ..**        t
36148 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 3a  he parent query:
36149 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ..**..**        
3614a 20 20 2a 20 69 73 20 6e 6f 74 20 69 74 73 65 6c    * is not itsel
3614b 66 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  f part of a comp
3614c 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0d 0a 2a 2a  ound select,..**
3614d 20 20 20 20 20 20 20 20 20 20 2a 20 69 73 20 6e            * is n
3614e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  ot an aggregate 
3614f 6f 72 20 44 49 53 54 49 4e 43 54 20 71 75 65 72  or DISTINCT quer
36150 79 2c 20 61 6e 64 0d 0a 2a 2a 20 20 20 20 20 20  y, and..**      
36151 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61 20 6a      * is not a j
36152 6f 69 6e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  oin..**..**     
36153 20 20 20 54 68 65 20 70 61 72 65 6e 74 20 61 6e     The parent an
36154 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61 79 20  d sub-query may 
36155 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20 63 6c  contain WHERE cl
36156 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74 20 74  auses. Subject t
36157 6f 0d 0a 2a 2a 20 20 20 20 20 20 20 20 72 75 6c  o..**        rul
36158 65 73 20 28 31 31 29 2c 20 28 31 33 29 20 61 6e  es (11), (13) an
36159 64 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61 79  d (14), they may
3615a 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52   also contain OR
3615b 44 45 52 20 42 59 2c 0d 0a 2a 2a 20 20 20 20 20  DER BY,..**     
3615c 20 20 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46     LIMIT and OFF
3615d 53 45 54 20 63 6c 61 75 73 65 73 2e 20 20 54 68  SET clauses.  Th
3615e 65 20 73 75 62 71 75 65 72 79 20 63 61 6e 6e 6f  e subquery canno
3615f 74 20 75 73 65 20 61 6e 79 20 63 6f 6d 70 6f 75  t use any compou
36160 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 20 20 6f 70  nd..**        op
36161 65 72 61 74 6f 72 20 6f 74 68 65 72 20 74 68 61  erator other tha
36162 6e 20 55 4e 49 4f 4e 20 41 4c 4c 20 62 65 63 61  n UNION ALL beca
36163 75 73 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65  use all the othe
36164 72 20 63 6f 6d 70 6f 75 6e 64 0d 0a 2a 2a 20 20  r compound..**  
36165 20 20 20 20 20 20 6f 70 65 72 61 74 6f 72 73 20        operators 
36166 68 61 76 65 20 61 6e 20 69 6d 70 6c 69 65 64 20  have an implied 
36167 44 49 53 54 49 4e 43 54 20 77 68 69 63 68 20 69  DISTINCT which i
36168 73 20 64 69 73 61 6c 6c 6f 77 65 64 20 62 79 0d  s disallowed by.
36169 0a 2a 2a 20 20 20 20 20 20 20 20 72 65 73 74 72  .**        restr
3616a 69 63 74 69 6f 6e 20 28 34 29 2e 0d 0a 2a 2a 0d  iction (4)...**.
3616b 0a 2a 2a 20 20 28 31 38 29 20 20 49 66 20 74 68  .**  (18)  If th
3616c 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61  e sub-query is a
3616d 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
3616e 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73  , then all terms
3616f 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20   of the..**     
36170 20 20 20 4f 52 44 45 52 20 62 79 20 63 6c 61 75     ORDER by clau
36171 73 65 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  se of the parent
36172 20 6d 75 73 74 20 62 65 20 73 69 6d 70 6c 65 20   must be simple 
36173 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 0d 0a  references to ..
36174 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e  **        column
36175 73 20 6f 66 20 74 68 65 20 73 75 62 2d 71 75 65  s of the sub-que
36176 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 28 31 39  ry...**..**  (19
36177 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
36178 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d  does not use LIM
36179 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20  IT or the outer 
3617a 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0d 0a  query does not..
3617b 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20 61  **        have a
3617c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0d 0a   WHERE clause...
3617d 2a 2a 0d 0a 2a 2a 20 20 28 32 30 29 20 20 49 66  **..**  (20)  If
3617e 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
3617f 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  s a compound sel
36180 65 63 74 2c 20 74 68 65 6e 20 69 74 20 6d 75 73  ect, then it mus
36181 74 20 6e 6f 74 20 75 73 65 0d 0a 2a 2a 20 20 20  t not use..**   
36182 20 20 20 20 20 61 6e 20 4f 52 44 45 52 20 42 59       an ORDER BY
36183 20 63 6c 61 75 73 65 2e 20 20 54 69 63 6b 65 74   clause.  Ticket
36184 20 23 33 37 37 33 2e 20 20 57 65 20 63 6f 75 6c   #3773.  We coul
36185 64 20 72 65 6c 61 78 20 74 68 69 73 20 63 6f 6e  d relax this con
36186 73 74 72 61 69 6e 74 0d 0a 2a 2a 20 20 20 20 20  straint..**     
36187 20 20 20 73 6f 6d 65 77 68 61 74 20 62 79 20 73     somewhat by s
36188 61 79 69 6e 67 20 74 68 61 74 20 74 68 65 20 74  aying that the t
36189 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45  erms of the ORDE
3618a 52 20 42 59 20 63 6c 61 75 73 65 20 6d 75 73 74  R BY clause must
3618b 0d 0a 2a 2a 20 20 20 20 20 20 20 20 61 70 70 65  ..**        appe
3618c 61 72 20 61 73 20 75 6e 6d 6f 64 69 66 69 65 64  ar as unmodified
3618d 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
3618e 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  in the outer que
3618f 72 79 2e 20 20 42 75 74 20 77 65 0d 0a 2a 2a 20  ry.  But we..** 
36190 20 20 20 20 20 20 20 68 61 76 65 20 6f 74 68 65         have othe
36191 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  r optimizations 
36192 69 6e 20 6d 69 6e 64 20 74 6f 20 64 65 61 6c 20  in mind to deal 
36193 77 69 74 68 20 74 68 61 74 20 63 61 73 65 2e 0d  with that case..
36194 0a 2a 2a 0d 0a 2a 2a 20 20 28 32 31 29 20 20 54  .**..**  (21)  T
36195 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
36196 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f   not use LIMIT o
36197 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
36198 79 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 20 20 20  y is not..**    
36199 20 20 20 20 44 49 53 54 49 4e 43 54 2e 20 20 28      DISTINCT.  (
3619a 53 65 65 20 74 69 63 6b 65 74 20 5b 37 35 32 65  See ticket [752e
3619b 31 36 34 36 66 63 5d 29 2e 0d 0a 2a 2a 0d 0a 2a  1646fc])...**..*
3619c 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  * In this routin
3619d 65 2c 20 74 68 65 20 22 70 22 20 70 61 72 61 6d  e, the "p" param
3619e 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65  eter is a pointe
3619f 72 20 74 6f 20 74 68 65 20 6f 75 74 65 72 20 71  r to the outer q
361a0 75 65 72 79 2e 0d 0a 2a 2a 20 54 68 65 20 73 75  uery...** The su
361a1 62 71 75 65 72 79 20 69 73 20 70 2d 3e 70 53 72  bquery is p->pSr
361a2 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20 20 69 73  c->a[iFrom].  is
361a3 41 67 67 20 69 73 20 74 72 75 65 20 69 66 20 74  Agg is true if t
361a4 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 0d 0a  he outer query..
361a5 2a 2a 20 75 73 65 73 20 61 67 67 72 65 67 61 74  ** uses aggregat
361a6 65 73 20 61 6e 64 20 73 75 62 71 75 65 72 79 49  es and subqueryI
361a7 73 41 67 67 20 69 73 20 74 72 75 65 20 69 66 20  sAgg is true if 
361a8 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73 65  the subquery use
361a9 73 20 61 67 67 72 65 67 61 74 65 73 2e 0d 0a 2a  s aggregates...*
361aa 2a 0d 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e  *..** If flatten
361ab 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65 6d  ing is not attem
361ac 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  pted, this routi
361ad 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e  ne is a no-op an
361ae 64 20 72 65 74 75 72 6e 73 20 30 2e 0d 0a 2a 2a  d returns 0...**
361af 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   If flattening i
361b0 73 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73  s attempted this
361b1 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
361b2 20 31 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20   1...**..** All 
361b3 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
361b4 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73 74 20  n analysis must 
361b5 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20 74 68  occur on both th
361b6 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e  e outer query an
361b7 64 0d 0a 2a 2a 20 74 68 65 20 73 75 62 71 75 65  d..** the subque
361b8 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ry before this r
361b9 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0d 0a 2a 2f  outine runs...*/
361ba 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61  ..static int fla
361bb 74 74 65 6e 53 75 62 71 75 65 72 79 28 0d 0a 20  ttenSubquery(.. 
361bc 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
361bd 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
361be 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 53   context */..  S
361bf 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
361c0 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e      /* The paren
361c1 74 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43  t or outer SELEC
361c2 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a  T statement */..
361c3 20 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20    int iFrom,    
361c4 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
361c5 69 6e 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20  in p->pSrc->a[] 
361c6 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62  of the inner sub
361c7 71 75 65 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20  query */..  int 
361c8 69 73 41 67 67 2c 20 20 20 20 20 20 20 20 20 20  isAgg,          
361c9 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 65   /* True if oute
361ca 72 20 53 45 4c 45 43 54 20 75 73 65 73 20 61 67  r SELECT uses ag
361cb 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
361cc 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 73 75 62 71  s */..  int subq
361cd 75 65 72 79 49 73 41 67 67 20 20 20 20 2f 2a 20  ueryIsAgg    /* 
361ce 54 72 75 65 20 69 66 20 74 68 65 20 73 75 62 71  True if the subq
361cf 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65 67  uery uses aggreg
361d0 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f  ate functions */
361d1 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  ..){..  const ch
361d2 61 72 20 2a 7a 53 61 76 65 64 41 75 74 68 43 6f  ar *zSavedAuthCo
361d3 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e  ntext = pParse->
361d4 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0d 0a 20  zAuthContext;.. 
361d5 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65 6e 74   Select *pParent
361d6 3b 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75  ;..  Select *pSu
361d7 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
361d8 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72 20 22  inner query or "
361d9 73 75 62 71 75 65 72 79 22 20 2a 2f 0d 0a 20 20  subquery" */..  
361da 53 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20 20  Select *pSub1;  
361db 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
361dc 6f 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20  o the rightmost 
361dd 73 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71 75  select in sub-qu
361de 65 72 79 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73  ery */..  SrcLis
361df 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a  t *pSrc;      /*
361e0 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
361e1 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
361e2 65 72 79 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73  ery */..  SrcLis
361e3 74 20 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a  t *pSubSrc;   /*
361e4 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
361e5 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
361e6 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20   */..  ExprList 
361e7 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68  *pList;    /* Th
361e8 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
361e9 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
361ea 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 61 72 65 6e  */..  int iParen
361eb 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42  t;        /* VDB
361ec 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
361ed 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73 75  of the pSub resu
361ee 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62 6c  lt set temp tabl
361ef 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b 20 20  e */..  int i;  
361f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
361f1 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a  oop counter */..
361f2 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
361f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
361f4 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
361f5 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 73 74 72  clause */..  str
361f6 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
361f7 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a   *pSubitem;   /*
361f8 20 54 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f   The subquery */
361f9 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
361fa 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d  = pParse->db;...
361fb 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
361fc 65 65 20 69 66 20 66 6c 61 74 74 65 6e 69 6e 67  ee if flattening
361fd 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20   is permitted.  
361fe 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e  Return 0 if not.
361ff 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  ..  */..  assert
36200 28 20 70 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  ( p!=0 );..  ass
36201 65 72 74 28 20 70 2d 3e 70 50 72 69 6f 72 3d 3d  ert( p->pPrior==
36202 30 20 29 3b 20 20 2f 2a 20 55 6e 61 62 6c 65 20  0 );  /* Unable 
36203 74 6f 20 66 6c 61 74 74 65 6e 20 63 6f 6d 70 6f  to flatten compo
36204 75 6e 64 20 71 75 65 72 69 65 73 20 2a 2f 0d 0a  und queries */..
36205 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
36206 26 20 53 51 4c 49 54 45 5f 51 75 65 72 79 46 6c  & SQLITE_QueryFl
36207 61 74 74 65 6e 65 72 20 29 20 72 65 74 75 72 6e  attener ) return
36208 20 30 3b 0d 0a 20 20 70 53 72 63 20 3d 20 70 2d   0;..  pSrc = p-
36209 3e 70 53 72 63 3b 0d 0a 20 20 61 73 73 65 72 74  >pSrc;..  assert
3620a 28 20 70 53 72 63 20 26 26 20 69 46 72 6f 6d 3e  ( pSrc && iFrom>
3620b 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53 72 63  =0 && iFrom<pSrc
3620c 2d 3e 6e 53 72 63 20 29 3b 0d 0a 20 20 70 53 75  ->nSrc );..  pSu
3620d 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61  bitem = &pSrc->a
3620e 5b 69 46 72 6f 6d 5d 3b 0d 0a 20 20 69 50 61 72  [iFrom];..  iPar
3620f 65 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ent = pSubitem->
36210 69 43 75 72 73 6f 72 3b 0d 0a 20 20 70 53 75 62  iCursor;..  pSub
36211 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65   = pSubitem->pSe
36212 6c 65 63 74 3b 0d 0a 20 20 61 73 73 65 72 74 28  lect;..  assert(
36213 20 70 53 75 62 21 3d 30 20 29 3b 0d 0a 20 20 69   pSub!=0 );..  i
36214 66 28 20 69 73 41 67 67 20 26 26 20 73 75 62 71  f( isAgg && subq
36215 75 65 72 79 49 73 41 67 67 20 29 20 72 65 74 75  ueryIsAgg ) retu
36216 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
36217 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
36218 74 69 6f 6e 20 28 31 29 20 20 2a 2f 0d 0a 20 20  tion (1)  */..  
36219 69 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67  if( subqueryIsAg
3621a 67 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e  g && pSrc->nSrc>
3621b 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  1 ) return 0;   
3621c 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
3621d 63 74 69 6f 6e 20 28 32 29 20 20 2a 2f 0d 0a 20  ction (2)  */.. 
3621e 20 70 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d   pSubSrc = pSub-
3621f 3e 70 53 72 63 3b 0d 0a 20 20 61 73 73 65 72 74  >pSrc;..  assert
36220 28 20 70 53 75 62 53 72 63 20 29 3b 0d 0a 20 20  ( pSubSrc );..  
36221 2f 2a 20 50 72 69 6f 72 20 74 6f 20 76 65 72 73  /* Prior to vers
36222 69 6f 6e 20 33 2e 31 2e 32 2c 20 77 68 65 6e 20  ion 3.1.2, when 
36223 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54  LIMIT and OFFSET
36224 20 68 61 64 20 74 6f 20 62 65 20 73 69 6d 70 6c   had to be simpl
36225 65 20 63 6f 6e 73 74 61 6e 74 73 2c 0d 0a 20 20  e constants,..  
36226 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72 61 72 79  ** not arbitrary
36227 20 65 78 70 72 65 73 73 73 69 6f 6e 73 2c 20 77   expresssions, w
36228 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d 65 20 63  e allowed some c
36229 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c 49 4d 49  ombining of LIMI
3622a 54 20 61 6e 64 20 4f 46 46 53 45 54 0d 0a 20 20  T and OFFSET..  
3622b 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 79 20  ** because they 
3622c 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  could be compute
3622d 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
3622e 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c 49 4d  e.  But when LIM
3622f 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0d 0a 20  IT and OFFSET.. 
36230 20 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69 74   ** became arbit
36231 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 73  rary expressions
36232 2c 20 77 65 20 77 65 72 65 20 66 6f 72 63 65 64  , we were forced
36233 20 74 6f 20 61 64 64 20 72 65 73 74 72 69 63 74   to add restrict
36234 69 6f 6e 73 20 28 31 33 29 0d 0a 20 20 2a 2a 20  ions (13)..  ** 
36235 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0d 0a 20 20  and (14). */..  
36236 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74  if( pSub->pLimit
36237 20 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20   && p->pLimit ) 
36238 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
36239 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
3623a 63 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0d 0a 20  ction (13) */.. 
3623b 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 66 66 73   if( pSub->pOffs
3623c 65 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  et ) return 0;  
3623d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3623e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
3623f 69 63 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0d 0a  iction (14) */..
36240 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d    if( p->pRightm
36241 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70 4c 69  ost && pSub->pLi
36242 6d 69 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  mit ){..    retu
36243 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
36244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36246 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
36247 28 31 35 29 20 2a 2f 0d 0a 20 20 7d 0d 0a 20 20  (15) */..  }..  
36248 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72  if( pSubSrc->nSr
36249 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  c==0 ) return 0;
3624a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3624b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
3624c 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0d 0a 20  ction (7)  */.. 
3624d 20 69 66 28 20 70 53 75 62 2d 3e 73 65 6c 46 6c   if( pSub->selFl
3624e 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
3624f 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  t ) return 0;   
36250 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
36251 69 63 74 69 6f 6e 20 28 35 29 20 20 2a 2f 0d 0a  iction (5)  */..
36252 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d    if( pSub->pLim
36253 69 74 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72  it && (pSrc->nSr
36254 63 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b  c>1 || isAgg) ){
36255 0d 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ..     return 0;
36256 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
36257 72 69 63 74 69 6f 6e 73 20 28 38 29 28 39 29 20  rictions (8)(9) 
36258 2a 2f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 28  */..  }..  if( (
36259 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
3625a 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20 26 26  _Distinct)!=0 &&
3625b 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29   subqueryIsAgg )
3625c 7b 0d 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30  {..     return 0
3625d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73  ;         /* Res
3625e 74 72 69 63 74 69 6f 6e 20 28 36 29 20 20 2a 2f  triction (6)  */
3625f 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e  ..  }..  if( p->
36260 70 4f 72 64 65 72 42 79 20 26 26 20 70 53 75 62  pOrderBy && pSub
36261 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0d 0a 20  ->pOrderBy ){.. 
36262 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
36263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36265 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
36266 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f 0d 0a  iction (11) */..
36267 20 20 7d 0d 0a 20 20 69 66 28 20 69 73 41 67 67    }..  if( isAgg
36268 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
36269 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  By ) return 0;  
3626a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3626b 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36   Restriction (16
3626c 29 20 2a 2f 0d 0a 20 20 69 66 28 20 70 53 75 62  ) */..  if( pSub
3626d 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70  ->pLimit && p->p
3626e 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30  Where ) return 0
3626f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
36270 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
36271 39 29 20 2a 2f 0d 0a 20 20 69 66 28 20 70 53 75  9) */..  if( pSu
36272 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 28 70 2d  b->pLimit && (p-
36273 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
36274 69 73 74 69 6e 63 74 29 21 3d 30 20 29 7b 0d 0a  istinct)!=0 ){..
36275 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
36276 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
36277 63 74 69 6f 6e 20 28 32 31 29 20 2a 2f 0d 0a 20  ction (21) */.. 
36278 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4f 42 53 4f 4c   }....  /* OBSOL
36279 45 54 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0d 0a  ETE COMMENT 1:..
3627a 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    ** Restriction
3627b 20 33 3a 20 20 49 66 20 74 68 65 20 73 75 62 71   3:  If the subq
3627c 75 65 72 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20  uery is a join, 
3627d 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75  make sure the su
3627e 62 71 75 65 72 79 20 69 73 20 0d 0a 20 20 2a 2a  bquery is ..  **
3627f 20 6e 6f 74 20 75 73 65 64 20 61 73 20 74 68 65   not used as the
36280 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f   right operand o
36281 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f 69 6e 2e  f an outer join.
36282 20 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 77 68    Examples of wh
36283 79 20 74 68 69 73 0d 0a 20 20 2a 2a 20 69 73 20  y this..  ** is 
36284 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0d 0a 20 20  not allowed:..  
36285 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  **..  **        
36286 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a   t1 LEFT OUTER J
36287 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29  OIN (t2 JOIN t3)
36288 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20  ..  **..  ** If 
36289 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61  we flatten the a
3628a 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67  bove, we would g
3628b 65 74 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20  et..  **..  **  
3628c 20 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20         (t1 LEFT 
3628d 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a  OUTER JOIN t2) J
3628e 4f 49 4e 20 74 33 0d 0a 20 20 2a 2a 0d 0a 20 20  OIN t3..  **..  
3628f 2a 2a 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  ** which is not 
36290 61 74 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20  at all the same 
36291 74 68 69 6e 67 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  thing...  **..  
36292 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  ** OBSOLETE COMM
36293 45 4e 54 20 32 3a 0d 0a 20 20 2a 2a 20 52 65 73  ENT 2:..  ** Res
36294 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66  triction 12:  If
36295 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
36296 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
36297 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
36298 65 72 0d 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d  er..  ** join, m
36299 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62  ake sure the sub
3629a 71 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45  query has no WHE
3629b 52 45 20 63 6c 61 75 73 65 2e 0d 0a 20 20 2a 2a  RE clause...  **
3629c 20 41 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20   An examples of 
3629d 77 68 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20  why this is not 
3629e 61 6c 6c 6f 77 65 64 3a 0d 0a 20 20 2a 2a 0d 0a  allowed:..  **..
3629f 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20    **         t1 
362a0 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
362a1 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  (SELECT * FROM t
362a2 32 20 57 48 45 52 45 20 74 32 2e 78 3e 30 29 0d  2 WHERE t2.x>0).
362a3 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49 66 20 77  .  **..  ** If w
362a4 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62  e flatten the ab
362a5 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65  ove, we would ge
362a6 74 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20  t..  **..  **   
362a7 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
362a8 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48  UTER JOIN t2) WH
362a9 45 52 45 20 74 32 2e 78 3e 30 0d 0a 20 20 2a 2a  ERE t2.x>0..  **
362aa 0d 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 74  ..  ** But the t
362ab 32 2e 78 3e 30 20 74 65 73 74 20 77 69 6c 6c 20  2.x>0 test will 
362ac 61 6c 77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61  always fail on a
362ad 20 4e 55 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c   NULL row of t2,
362ae 20 77 68 69 63 68 0d 0a 20 20 2a 2a 20 65 66 66   which..  ** eff
362af 65 63 74 69 76 65 6c 79 20 63 6f 6e 76 65 72 74  ectively convert
362b0 73 20 74 68 65 20 4f 55 54 45 52 20 4a 4f 49 4e  s the OUTER JOIN
362b1 20 69 6e 74 6f 20 61 6e 20 49 4e 4e 45 52 20 4a   into an INNER J
362b2 4f 49 4e 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  OIN...  **..  **
362b3 20 54 48 49 53 20 4f 56 45 52 52 49 44 45 53 20   THIS OVERRIDES 
362b4 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54  OBSOLETE COMMENT
362b5 53 20 31 20 41 4e 44 20 32 20 41 42 4f 56 45 3a  S 1 AND 2 ABOVE:
362b6 0d 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 33  ..  ** Ticket #3
362b7 33 30 30 20 73 68 6f 77 73 20 74 68 61 74 20 66  300 shows that f
362b8 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 72 69  lattening the ri
362b9 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45  ght term of a LE
362ba 46 54 20 4a 4f 49 4e 0d 0a 20 20 2a 2a 20 69 73  FT JOIN..  ** is
362bb 20 66 72 61 75 67 68 74 20 77 69 74 68 20 64 61   fraught with da
362bc 6e 67 65 72 2e 20 20 42 65 73 74 20 74 6f 20 61  nger.  Best to a
362bd 76 6f 69 64 20 74 68 65 20 77 68 6f 6c 65 20 74  void the whole t
362be 68 69 6e 67 2e 20 20 49 66 20 74 68 65 0d 0a 20  hing.  If the.. 
362bf 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69 73 20   ** subquery is 
362c0 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f  the right term o
362c1 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 74  f a LEFT JOIN, t
362c2 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74 74  hen do not flatt
362c3 65 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  en...  */..  if(
362c4 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e   (pSubitem->join
362c5 74 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29  type & JT_OUTER)
362c6 21 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  !=0 ){..    retu
362c7 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 0;..  }....  
362c8 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31  /* Restriction 1
362c9 37 3a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75  7: If the sub-qu
362ca 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  ery is a compoun
362cb 64 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 69  d SELECT, then i
362cc 74 20 6d 75 73 74 0d 0a 20 20 2a 2a 20 75 73 65  t must..  ** use
362cd 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e 20   only the UNION 
362ce 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41 6e  ALL operator. An
362cf 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 69  d none of the si
362d0 6d 70 6c 65 20 73 65 6c 65 63 74 20 71 75 65 72  mple select quer
362d1 69 65 73 0d 0a 20 20 2a 2a 20 74 68 61 74 20 6d  ies..  ** that m
362d2 61 6b 65 20 75 70 20 74 68 65 20 63 6f 6d 70 6f  ake up the compo
362d3 75 6e 64 20 53 45 4c 45 43 54 20 61 72 65 20 61  und SELECT are a
362d4 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 61 67 67  llowed to be agg
362d5 72 65 67 61 74 65 20 6f 72 20 64 69 73 74 69 6e  regate or distin
362d6 63 74 0d 0a 20 20 2a 2a 20 71 75 65 72 69 65 73  ct..  ** queries
362d7 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70  ...  */..  if( p
362d8 53 75 62 2d 3e 70 50 72 69 6f 72 20 29 7b 0d 0a  Sub->pPrior ){..
362d9 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f      if( pSub->pO
362da 72 64 65 72 42 79 20 29 7b 0d 0a 20 20 20 20 20  rderBy ){..     
362db 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 52   return 0;  /* R
362dc 65 73 74 72 69 63 74 69 6f 6e 20 32 30 20 2a 2f  estriction 20 */
362dd 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
362de 20 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e 73 65   isAgg || (p->se
362df 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
362e0 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 72 63  inct)!=0 || pSrc
362e1 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0d 0a 20 20  ->nSrc!=1 ){..  
362e2 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20      return 0;.. 
362e3 20 20 20 7d 0d 0a 20 20 20 20 66 6f 72 28 70 53     }..    for(pS
362e4 75 62 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b  ub1=pSub; pSub1;
362e5 20 70 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50   pSub1=pSub1->pP
362e6 72 69 6f 72 29 7b 0d 0a 20 20 20 20 20 20 74 65  rior){..      te
362e7 73 74 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e  stcase( (pSub1->
362e8 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44  selFlags & (SF_D
362e9 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65  istinct|SF_Aggre
362ea 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69  gate))==SF_Disti
362eb 6e 63 74 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  nct );..      te
362ec 73 74 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e  stcase( (pSub1->
362ed 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44  selFlags & (SF_D
362ee 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65  istinct|SF_Aggre
362ef 67 61 74 65 29 29 3d 3d 53 46 5f 41 67 67 72 65  gate))==SF_Aggre
362f0 67 61 74 65 20 29 3b 0d 0a 20 20 20 20 20 20 61  gate );..      a
362f1 73 73 65 72 74 28 20 70 53 75 62 2d 3e 70 53 72  ssert( pSub->pSr
362f2 63 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 69  c!=0 );..      i
362f3 66 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c  f( (pSub1->selFl
362f4 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
362f5 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
362f6 29 21 3d 30 0d 0a 20 20 20 20 20 20 20 7c 7c 20  )!=0..       || 
362f7 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 20 26  (pSub1->pPrior &
362f8 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f  & pSub1->op!=TK_
362f9 41 4c 4c 29 20 0d 0a 20 20 20 20 20 20 20 7c 7c  ALL) ..       ||
362fa 20 70 53 75 62 31 2d 3e 70 53 72 63 2d 3e 6e 53   pSub1->pSrc->nS
362fb 72 63 3c 31 0d 0a 20 20 20 20 20 20 29 7b 0d 0a  rc<1..      ){..
362fc 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
362fd 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
362fe 20 20 74 65 73 74 63 61 73 65 28 20 70 53 75 62    testcase( pSub
362ff 31 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20  1->pSrc->nSrc>1 
36300 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  );..    }....   
36301 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
36302 31 38 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  18. */..    if( 
36303 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0d 0a  p->pOrderBy ){..
36304 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20        int ii;.. 
36305 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
36306 69 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e  i<p->pOrderBy->n
36307 45 78 70 72 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20  Expr; ii++){..  
36308 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72        if( p->pOr
36309 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e 69 4f 72  derBy->a[ii].iOr
3630a 64 65 72 42 79 43 6f 6c 3d 3d 30 20 29 20 72 65  derByCol==0 ) re
3630b 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20 7d  turn 0;..      }
3630c 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
3630d 20 20 2f 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72    /***** If we r
3630e 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
3630f 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70   flattening is p
36310 65 72 6d 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f  ermitted. *****/
36311 0d 0a 0d 0a 20 20 2f 2a 20 41 75 74 68 6f 72 69  ....  /* Authori
36312 7a 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20  ze the subquery 
36313 2a 2f 0d 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41  */..  pParse->zA
36314 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 53 75  uthContext = pSu
36315 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20  bitem->zName;.. 
36316 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
36317 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
36318 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30  _SELECT, 0, 0, 0
36319 29 3b 0d 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41  );..  pParse->zA
3631a 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61  uthContext = zSa
3631b 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 3b 0d  vedAuthContext;.
3631c 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ...  /* If the s
3631d 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
3631e 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74  mpound SELECT st
3631f 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62  atement, then (b
36320 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0d 0a  y restrictions..
36321 20 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61    ** 17 and 18 a
36322 62 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65  bove) it must be
36323 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64   a UNION ALL and
36324 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
36325 79 20 6d 75 73 74 20 0d 0a 20 20 2a 2a 20 62 65  y must ..  ** be
36326 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0d 0a 20   of the form:.. 
36327 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 53 45   **..  **     SE
36328 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69 73 74 3e  LECT <expr-list>
36329 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75 65 72   FROM (<sub-quer
3632a 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61 75 73  y>) <where-claus
3632b 65 3e 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  e> ..  **..  ** 
3632c 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79 20  followed by any 
3632d 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20  ORDER BY, LIMIT 
3632e 61 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c  and/or OFFSET cl
3632f 61 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f 63  auses. This bloc
36330 6b 0d 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20  k..  ** creates 
36331 4e 2d 31 20 63 6f 70 69 65 73 20 6f 66 20 74 68  N-1 copies of th
36332 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 77  e parent query w
36333 69 74 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52  ithout any ORDER
36334 20 42 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0d 0a   BY, LIMIT or ..
36335 20 20 2a 2a 20 4f 46 46 53 45 54 20 63 6c 61 75    ** OFFSET clau
36336 73 65 73 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68  ses and joins th
36337 65 6d 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 68  em to the left-h
36338 61 6e 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20  and-side of the 
36339 6f 72 69 67 69 6e 61 6c 0d 0a 20 20 2a 2a 20 75  original..  ** u
3633a 73 69 6e 67 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f  sing UNION ALL o
3633b 70 65 72 61 74 6f 72 73 2e 20 49 6e 20 74 68 69  perators. In thi
3633c 73 20 63 61 73 65 20 4e 20 69 73 20 74 68 65 20  s case N is the 
3633d 6e 75 6d 62 65 72 20 6f 66 20 73 69 6d 70 6c 65  number of simple
3633e 0d 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 73 74  ..  ** select st
3633f 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  atements in the 
36340 63 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71 75 65  compound sub-que
36341 72 79 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  ry...  **..  ** 
36342 45 78 61 6d 70 6c 65 3a 0d 0a 20 20 2a 2a 0d 0a  Example:..  **..
36343 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
36344 61 2b 31 20 46 52 4f 4d 20 28 0d 0a 20 20 2a 2a  a+1 FROM (..  **
36345 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 78          SELECT x
36346 20 46 52 4f 4d 20 74 61 62 0d 0a 20 20 2a 2a 20   FROM tab..  ** 
36347 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
36348 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45  ..  **        SE
36349 4c 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0d  LECT y FROM tab.
3634a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49  .  **        UNI
3634b 4f 4e 20 41 4c 4c 0d 0a 20 20 2a 2a 20 20 20 20  ON ALL..  **    
3634c 20 20 20 20 53 45 4c 45 43 54 20 61 62 73 28 7a      SELECT abs(z
3634d 2a 32 29 20 46 52 4f 4d 20 74 61 62 32 0d 0a 20  *2) FROM tab2.. 
3634e 20 2a 2a 20 20 20 20 20 29 20 57 48 45 52 45 20   **     ) WHERE 
3634f 61 21 3d 35 20 4f 52 44 45 52 20 42 59 20 31 0d  a!=5 ORDER BY 1.
36350 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 72 61 6e  .  **..  ** Tran
36351 73 66 6f 72 6d 65 64 20 69 6e 74 6f 3a 0d 0a 20  sformed into:.. 
36352 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 53 45   **..  **     SE
36353 4c 45 43 54 20 78 2b 31 20 46 52 4f 4d 20 74 61  LECT x+1 FROM ta
36354 62 20 57 48 45 52 45 20 78 2b 31 21 3d 35 0d 0a  b WHERE x+1!=5..
36355 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41    **     UNION A
36356 4c 4c 0d 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c  LL..  **     SEL
36357 45 43 54 20 79 2b 31 20 46 52 4f 4d 20 74 61 62  ECT y+1 FROM tab
36358 20 57 48 45 52 45 20 79 2b 31 21 3d 35 0d 0a 20   WHERE y+1!=5.. 
36359 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c   **     UNION AL
3635a 4c 0d 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  L..  **     SELE
3635b 43 54 20 61 62 73 28 7a 2a 32 29 2b 31 20 46 52  CT abs(z*2)+1 FR
3635c 4f 4d 20 74 61 62 32 20 57 48 45 52 45 20 61 62  OM tab2 WHERE ab
3635d 73 28 7a 2a 32 29 2b 31 21 3d 35 0d 0a 20 20 2a  s(z*2)+1!=5..  *
3635e 2a 20 20 20 20 20 4f 52 44 45 52 20 42 59 20 31  *     ORDER BY 1
3635f 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57 65 20  ..  **..  ** We 
36360 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22 63  call this the "c
36361 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79  ompound-subquery
36362 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0d 0a 20   flattening"... 
36363 20 2a 2f 0d 0a 20 20 66 6f 72 28 70 53 75 62 3d   */..  for(pSub=
36364 70 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53  pSub->pPrior; pS
36365 75 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  ub; pSub=pSub->p
36366 50 72 69 6f 72 29 7b 0d 0a 20 20 20 20 53 65 6c  Prior){..    Sel
36367 65 63 74 20 2a 70 4e 65 77 3b 0d 0a 20 20 20 20  ect *pNew;..    
36368 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
36369 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79  By = p->pOrderBy
3636a 3b 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 69  ;..    Expr *pLi
3636b 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b  mit = p->pLimit;
3636c 0d 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 50  ..    Select *pP
3636d 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72  rior = p->pPrior
3636e 3b 0d 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  ;..    p->pOrder
3636f 42 79 20 3d 20 30 3b 0d 0a 20 20 20 20 70 2d 3e  By = 0;..    p->
36370 70 53 72 63 20 3d 20 30 3b 0d 0a 20 20 20 20 70  pSrc = 0;..    p
36371 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0d 0a 20  ->pPrior = 0;.. 
36372 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30     p->pLimit = 0
36373 3b 0d 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  ;..    pNew = sq
36374 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
36375 62 2c 20 70 2c 20 30 29 3b 0d 0a 20 20 20 20 70  b, p, 0);..    p
36376 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69  ->pLimit = pLimi
36377 74 3b 0d 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65  t;..    p->pOrde
36378 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0d  rBy = pOrderBy;.
36379 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 70  .    p->pSrc = p
3637a 53 72 63 3b 0d 0a 20 20 20 20 70 2d 3e 6f 70 20  Src;..    p->op 
3637b 3d 20 54 4b 5f 41 4c 4c 3b 0d 0a 20 20 20 20 70  = TK_ALL;..    p
3637c 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
3637d 3b 0d 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  ;..    if( pNew=
3637e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 4e 65  =0 ){..      pNe
3637f 77 20 3d 20 70 50 72 69 6f 72 3b 0d 0a 20 20 20  w = pPrior;..   
36380 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70   }else{..      p
36381 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50  New->pPrior = pP
36382 72 69 6f 72 3b 0d 0a 20 20 20 20 20 20 70 4e 65  rior;..      pNe
36383 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  w->pRightmost = 
36384 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70  0;..    }..    p
36385 2d 3e 70 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b  ->pPrior = pNew;
36386 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ..    if( db->ma
36387 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
36388 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  urn 1;..  }.... 
36389 20 2f 2a 20 42 65 67 69 6e 20 66 6c 61 74 74 65   /* Begin flatte
3638a 6e 69 6e 67 20 74 68 65 20 69 46 72 6f 6d 2d 74  ning the iFrom-t
3638b 68 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 46  h entry of the F
3638c 52 4f 4d 20 63 6c 61 75 73 65 20 0d 0a 20 20 2a  ROM clause ..  *
3638d 2a 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71  * in the outer q
3638e 75 65 72 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70  uery...  */..  p
3638f 53 75 62 20 3d 20 70 53 75 62 31 20 3d 20 70 53  Sub = pSub1 = pS
36390 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b  ubitem->pSelect;
36391 0d 0a 0d 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20  ....  /* Delete 
36392 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 74 61  the transient ta
36393 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 73  ble structure as
36394 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
36395 65 0d 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79  e..  ** subquery
36396 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
36397 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62  3DbFree(db, pSub
36398 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  item->zDatabase)
36399 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  ;..  sqlite3DbFr
3639a 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d  ee(db, pSubitem-
3639b 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 73 71 6c 69  >zName);..  sqli
3639c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53  te3DbFree(db, pS
3639d 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b  ubitem->zAlias);
3639e 0d 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44  ..  pSubitem->zD
3639f 61 74 61 62 61 73 65 20 3d 20 30 3b 0d 0a 20 20  atabase = 0;..  
363a0 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pSubitem->zName 
363a1 3d 20 30 3b 0d 0a 20 20 70 53 75 62 69 74 65 6d  = 0;..  pSubitem
363a2 2d 3e 7a 41 6c 69 61 73 20 3d 20 30 3b 0d 0a 20  ->zAlias = 0;.. 
363a3 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65   pSubitem->pSele
363a4 63 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20  ct = 0;....  /* 
363a5 44 65 66 65 72 20 64 65 6c 65 74 69 6e 67 20 74  Defer deleting t
363a6 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
363a7 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
363a8 74 68 65 0d 0a 20 20 2a 2a 20 73 75 62 71 75 65  the..  ** subque
363a9 72 79 20 75 6e 74 69 6c 20 63 6f 64 65 20 67 65  ry until code ge
363aa 6e 65 72 61 74 69 6f 6e 20 69 73 0d 0a 20 20 2a  neration is..  *
363ab 2a 20 63 6f 6d 70 6c 65 74 65 2c 20 73 69 6e 63  * complete, sinc
363ac 65 20 74 68 65 72 65 20 6d 61 79 20 73 74 69 6c  e there may stil
363ad 6c 20 65 78 69 73 74 20 45 78 70 72 2e 70 54 61  l exist Expr.pTa
363ae 62 20 65 6e 74 72 69 65 73 20 74 68 61 74 0d 0a  b entries that..
363af 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 68    ** refer to th
363b0 65 20 73 75 62 71 75 65 72 79 20 65 76 65 6e 20  e subquery even 
363b1 61 66 74 65 72 20 66 6c 61 74 74 65 6e 69 6e 67  after flattening
363b2 2e 20 20 54 69 63 6b 65 74 20 23 33 33 34 36 2e  .  Ticket #3346.
363b3 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 70 53 75  ..  **..  ** pSu
363b4 62 69 74 65 6d 2d 3e 70 54 61 62 20 69 73 20 61  bitem->pTab is a
363b5 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c 20 62  lways non-NULL b
363b6 79 20 74 65 73 74 20 72 65 73 74 72 69 63 74 69  y test restricti
363b7 6f 6e 73 20 61 6e 64 20 74 65 73 74 73 20 61 62  ons and tests ab
363b8 6f 76 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ove...  */..  if
363b9 28 20 41 4c 57 41 59 53 28 70 53 75 62 69 74 65  ( ALWAYS(pSubite
363ba 6d 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0d 0a  m->pTab!=0) ){..
363bb 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54      Table *pTabT
363bc 6f 44 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d  oDel = pSubitem-
363bd 3e 70 54 61 62 3b 0d 0a 20 20 20 20 69 66 28 20  >pTab;..    if( 
363be 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d  pTabToDel->nRef=
363bf 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20 50 61 72  =1 ){..      Par
363c0 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20  se *pToplevel = 
363c1 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
363c2 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0d 0a 20  evel(pParse);.. 
363c3 20 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e       pTabToDel->
363c4 70 4e 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70 54  pNextZombie = pT
363c5 6f 70 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65  oplevel->pZombie
363c6 54 61 62 3b 0d 0a 20 20 20 20 20 20 70 54 6f 70  Tab;..      pTop
363c7 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61  level->pZombieTa
363c8 62 20 3d 20 70 54 61 62 54 6f 44 65 6c 3b 0d 0a  b = pTabToDel;..
363c9 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
363ca 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65    pTabToDel->nRe
363cb 66 2d 2d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  f--;..    }..   
363cc 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20   pSubitem->pTab 
363cd 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  = 0;..  }....  /
363ce 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
363cf 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66  loop runs once f
363d0 6f 72 20 65 61 63 68 20 74 65 72 6d 20 69 6e 20  or each term in 
363d1 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75  a compound-subqu
363d2 65 72 79 0d 0a 20 20 2a 2a 20 66 6c 61 74 74 65  ery..  ** flatte
363d3 6e 69 6e 67 20 28 61 73 20 64 65 73 63 72 69 62  ning (as describ
363d4 65 64 20 61 62 6f 76 65 29 2e 20 20 49 66 20 77  ed above).  If w
363d5 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20 64 69  e are doing a di
363d6 66 66 65 72 65 6e 74 20 6b 69 6e 64 0d 0a 20 20  fferent kind..  
363d7 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67  ** of flattening
363d8 20 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e 67 20   - a flattening 
363d9 6f 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f 6d  other than a com
363da 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20 66  pound-subquery f
363db 6c 61 74 74 65 6e 69 6e 67 20 2d 0d 0a 20 20 2a  lattening -..  *
363dc 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f 70  * then this loop
363dd 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65 2e   only runs once.
363de 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 69  ..  **..  ** Thi
363df 73 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c  s loop moves all
363e0 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65   of the FROM ele
363e1 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62  ments of the sub
363e2 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0d 0a  query into the..
363e3 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c    ** the FROM cl
363e4 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65  ause of the oute
363e5 72 20 71 75 65 72 79 2e 20 20 42 65 66 6f 72 65  r query.  Before
363e6 20 64 6f 69 6e 67 20 74 68 69 73 2c 20 72 65 6d   doing this, rem
363e7 65 6d 62 65 72 0d 0a 20 20 2a 2a 20 74 68 65 20  ember..  ** the 
363e8 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
363e9 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f  r the original o
363ea 75 74 65 72 20 71 75 65 72 79 20 46 52 4f 4d 20  uter query FROM 
363eb 65 6c 65 6d 65 6e 74 20 69 6e 0d 0a 20 20 2a 2a  element in..  **
363ec 20 69 50 61 72 65 6e 74 2e 20 20 54 68 65 20 69   iParent.  The i
363ed 50 61 72 65 6e 74 20 63 75 72 73 6f 72 20 77 69  Parent cursor wi
363ee 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ll never be used
363ef 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 6f  .  Subsequent co
363f0 64 65 0d 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63  de..  ** will sc
363f1 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c  an expressions l
363f2 6f 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65  ooking for iPare
363f3 6e 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e  nt references an
363f4 64 20 72 65 70 6c 61 63 65 0d 0a 20 20 2a 2a 20  d replace..  ** 
363f5 74 68 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73  those references
363f6 20 77 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e   with expression
363f7 73 20 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74  s that resolve t
363f8 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 46  o the subquery F
363f9 52 4f 4d 0d 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e  ROM..  ** elemen
363fa 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20 63 6f  ts we are now co
363fb 70 79 69 6e 67 20 69 6e 2e 0d 0a 20 20 2a 2f 0d  pying in...  */.
363fc 0a 20 20 66 6f 72 28 70 50 61 72 65 6e 74 3d 70  .  for(pParent=p
363fd 3b 20 70 50 61 72 65 6e 74 3b 20 70 50 61 72 65  ; pParent; pPare
363fe 6e 74 3d 70 50 61 72 65 6e 74 2d 3e 70 50 72 69  nt=pParent->pPri
363ff 6f 72 2c 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  or, pSub=pSub->p
36400 50 72 69 6f 72 29 7b 0d 0a 20 20 20 20 69 6e 74  Prior){..    int
36401 20 6e 53 75 62 53 72 63 3b 0d 0a 20 20 20 20 75   nSubSrc;..    u
36402 38 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0d  8 jointype = 0;.
36403 0a 20 20 20 20 70 53 75 62 53 72 63 20 3d 20 70  .    pSubSrc = p
36404 53 75 62 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f  Sub->pSrc;     /
36405 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  * FROM clause of
36406 20 73 75 62 71 75 65 72 79 20 2a 2f 0d 0a 20 20   subquery */..  
36407 20 20 6e 53 75 62 53 72 63 20 3d 20 70 53 75 62    nSubSrc = pSub
36408 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f 2a 20 4e  Src->nSrc;  /* N
36409 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
3640a 6e 20 73 75 62 71 75 65 72 79 20 46 52 4f 4d 20  n subquery FROM 
3640b 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20 70  clause */..    p
3640c 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70  Src = pParent->p
3640d 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d  Src;     /* FROM
3640e 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f   clause of the o
3640f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0d 0a 0d  uter query */...
36410 0a 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b  .    if( pSrc ){
36411 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
36412 70 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20 2f  pParent==p );  /
36413 2a 20 46 69 72 73 74 20 74 69 6d 65 20 74 68 72  * First time thr
36414 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  ough the loop */
36415 0d 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65  ..      jointype
36416 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69   = pSubitem->joi
36417 6e 74 79 70 65 3b 0d 0a 20 20 20 20 7d 65 6c 73  ntype;..    }els
36418 65 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  e{..      assert
36419 28 20 70 50 61 72 65 6e 74 21 3d 70 20 29 3b 20  ( pParent!=p ); 
3641a 20 2f 2a 20 32 6e 64 20 61 6e 64 20 73 75 62 73   /* 2nd and subs
3641b 65 71 75 65 6e 74 20 74 69 6d 65 73 20 74 68 72  equent times thr
3641c 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  ough the loop */
3641d 0d 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ..      pSrc = p
3641e 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73  Parent->pSrc = s
3641f 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
36420 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29  end(db, 0, 0, 0)
36421 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 53 72  ;..      if( pSr
36422 63 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  c==0 ){..       
36423 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
36424 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20  locFailed );..  
36425 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
36426 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a      }..    }....
36427 20 20 20 20 2f 2a 20 54 68 65 20 73 75 62 71 75      /* The subqu
36428 65 72 79 20 75 73 65 73 20 61 20 73 69 6e 67 6c  ery uses a singl
36429 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 20 46 52  e slot of the FR
3642a 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
3642b 20 6f 75 74 65 72 0d 0a 20 20 20 20 2a 2a 20 71   outer..    ** q
3642c 75 65 72 79 2e 20 20 49 66 20 74 68 65 20 73 75  uery.  If the su
3642d 62 71 75 65 72 79 20 68 61 73 20 6d 6f 72 65 20  bquery has more 
3642e 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e 74  than one element
3642f 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61   in its FROM cla
36430 75 73 65 2c 0d 0a 20 20 20 20 2a 2a 20 74 68 65  use,..    ** the
36431 6e 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75 74  n expand the out
36432 65 72 20 71 75 65 72 79 20 74 6f 20 6d 61 6b 65  er query to make
36433 20 73 70 61 63 65 20 66 6f 72 20 69 74 20 74 6f   space for it to
36434 20 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d 65 6e   hold all elemen
36435 74 73 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  ts..    ** of th
36436 65 20 73 75 62 71 75 65 72 79 2e 0d 0a 20 20 20  e subquery...   
36437 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 45 78 61 6d   **..    ** Exam
36438 70 6c 65 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  ple:..    **..  
36439 20 20 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a    **    SELECT *
3643a 20 46 52 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c   FROM tabA, (SEL
3643b 45 43 54 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c  ECT * FROM sub1,
3643c 20 73 75 62 32 29 2c 20 74 61 62 42 3b 0d 0a 20   sub2), tabB;.. 
3643d 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68     **..    ** Th
3643e 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 68 61  e outer query ha
3643f 73 20 33 20 73 6c 6f 74 73 20 69 6e 20 69 74 73  s 3 slots in its
36440 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f   FROM clause.  O
36441 6e 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 0d 0a  ne slot of the..
36442 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65      ** outer que
36443 72 79 20 28 74 68 65 20 6d 69 64 64 6c 65 20 73  ry (the middle s
36444 6c 6f 74 29 20 69 73 20 75 73 65 64 20 62 79 20  lot) is used by 
36445 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 54  the subquery.  T
36446 68 65 20 6e 65 78 74 0d 0a 20 20 20 20 2a 2a 20  he next..    ** 
36447 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 77 69  block of code wi
36448 6c 6c 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75  ll expand the ou
36449 74 20 71 75 65 72 79 20 74 6f 20 34 20 73 6c 6f  t query to 4 slo
3644a 74 73 2e 20 20 54 68 65 20 6d 69 64 64 6c 65 0d  ts.  The middle.
3644b 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69 73 20  .    ** slot is 
3644c 65 78 70 61 6e 64 65 64 20 74 6f 20 74 77 6f 20  expanded to two 
3644d 73 6c 6f 74 73 20 69 6e 20 6f 72 64 65 72 20 74  slots in order t
3644e 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72  o make space for
3644f 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 74 77 6f   the..    ** two
36450 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
36451 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
36452 74 68 65 20 73 75 62 71 75 65 72 79 2e 0d 0a 20  the subquery... 
36453 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 6e     */..    if( n
36454 53 75 62 53 72 63 3e 31 20 29 7b 0d 0a 20 20 20  SubSrc>1 ){..   
36455 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63     pParent->pSrc
36456 20 3d 20 70 53 72 63 20 3d 20 73 71 6c 69 74 65   = pSrc = sqlite
36457 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28  3SrcListEnlarge(
36458 64 62 2c 20 70 53 72 63 2c 20 6e 53 75 62 53 72  db, pSrc, nSubSr
36459 63 2d 31 2c 69 46 72 6f 6d 2b 31 29 3b 0d 0a 20  c-1,iFrom+1);.. 
3645a 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c       if( db->mal
3645b 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
3645c 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3645d 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a      }..    }....
3645e 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20      /* Transfer 
3645f 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
36460 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 73  terms from the s
36461 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65  ubquery into the
36462 0d 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71  ..    ** outer q
36463 75 65 72 79 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  uery...    */.. 
36464 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53     for(i=0; i<nS
36465 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20  ubSrc; i++){..  
36466 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73      sqlite3IdLis
36467 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
36468 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d 2e 70 55 73  ->a[i+iFrom].pUs
36469 69 6e 67 29 3b 0d 0a 20 20 20 20 20 20 70 53 72  ing);..      pSr
3646a 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20  c->a[i+iFrom] = 
3646b 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 3b 0d 0a  pSubSrc->a[i];..
3646c 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70 53        memset(&pS
3646d 75 62 53 72 63 2d 3e 61 5b 69 5d 2c 20 30 2c 20  ubSrc->a[i], 0, 
3646e 73 69 7a 65 6f 66 28 70 53 75 62 53 72 63 2d 3e  sizeof(pSubSrc->
3646f 61 5b 69 5d 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a  a[i]));..    }..
36470 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 46 72 6f      pSrc->a[iFro
36471 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 6a 6f  m].jointype = jo
36472 69 6e 74 79 70 65 3b 0d 0a 20 20 0d 0a 20 20 20  intype;..  ..   
36473 20 2f 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73 75   /* Now begin su
36474 62 73 74 69 74 75 74 69 6e 67 20 73 75 62 71 75  bstituting subqu
36475 65 72 79 20 72 65 73 75 6c 74 20 73 65 74 20 65  ery result set e
36476 78 70 72 65 73 73 69 6f 6e 73 20 66 6f 72 20 0d  xpressions for .
36477 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  .    ** referenc
36478 65 73 20 74 6f 20 74 68 65 20 69 50 61 72 65 6e  es to the iParen
36479 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71  t in the outer q
3647a 75 65 72 79 2e 0d 0a 20 20 20 20 2a 2a 20 0d 0a  uery...    ** ..
3647b 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0d      ** Example:.
3647c 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3647d 20 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a    SELECT a+5, b*
3647e 31 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20  10 FROM (SELECT 
3647f 78 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41  x*3 AS a, y+10 A
36480 53 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45  S b FROM t1) WHE
36481 52 45 20 61 3e 62 3b 0d 0a 20 20 20 20 2a 2a 20  RE a>b;..    ** 
36482 20 20 5c 20 20 20 20 20 20 20 20 20 20 20 20 20    \             
36483 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f          \_______
36484 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20  ______ subquery 
36485 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20  __________/     
36486 20 20 20 20 20 2f 0d 0a 20 20 20 20 2a 2a 20 20       /..    **  
36487 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
36488 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71  ________ outer q
36489 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  uery ___________
3648a 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
3648b 5f 5f 5f 2f 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  ___/..    **..  
3648c 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20    ** We look at 
3648d 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
3648e 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
3648f 65 72 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c  ery and every pl
36490 61 63 65 20 77 65 20 73 65 65 0d 0a 20 20 20 20  ace we see..    
36491 2a 2a 20 22 61 22 20 77 65 20 73 75 62 73 74 69  ** "a" we substi
36492 74 75 74 65 20 22 78 2a 33 22 20 61 6e 64 20 65  tute "x*3" and e
36493 76 65 72 79 20 70 6c 61 63 65 20 77 65 20 73 65  very place we se
36494 65 20 22 62 22 20 77 65 20 73 75 62 73 74 69 74  e "b" we substit
36495 75 74 65 20 22 79 2b 31 30 22 2e 0d 0a 20 20 20  ute "y+10"...   
36496 20 2a 2f 0d 0a 20 20 20 20 70 4c 69 73 74 20 3d   */..    pList =
36497 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73 74   pParent->pEList
36498 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
36499 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
3649a 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  i++){..      if(
3649b 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61   pList->a[i].zNa
3649c 6d 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  me==0 ){..      
3649d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
3649e 70 61 6e 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69  pan = pList->a[i
3649f 5d 2e 7a 53 70 61 6e 3b 0d 0a 20 20 20 20 20 20  ].zSpan;..      
364a0 20 20 69 66 28 20 41 4c 57 41 59 53 28 7a 53 70    if( ALWAYS(zSp
364a1 61 6e 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  an) ){..        
364a2 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e    pList->a[i].zN
364a3 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
364a4 74 72 44 75 70 28 64 62 2c 20 7a 53 70 61 6e 29  trDup(db, zSpan)
364a5 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
364a6 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
364a7 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
364a8 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c  db, pParent->pEL
364a9 69 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ist, iParent, pS
364aa 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a 20 20  ub->pEList);..  
364ab 20 20 69 66 28 20 69 73 41 67 67 20 29 7b 0d 0a    if( isAgg ){..
364ac 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c        substExprL
364ad 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
364ae 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61 72 65  >pGroupBy, iPare
364af 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
364b0 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 65 6e  );..      pParen
364b1 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62  t->pHaving = sub
364b2 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65  stExpr(db, pPare
364b3 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61  nt->pHaving, iPa
364b4 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
364b5 73 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  st);..    }..   
364b6 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65   if( pSub->pOrde
364b7 72 42 79 20 29 7b 0d 0a 20 20 20 20 20 20 61 73  rBy ){..      as
364b8 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70  sert( pParent->p
364b9 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0d 0a 20  OrderBy==0 );.. 
364ba 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4f       pParent->pO
364bb 72 64 65 72 42 79 20 3d 20 70 53 75 62 2d 3e 70  rderBy = pSub->p
364bc 4f 72 64 65 72 42 79 3b 0d 0a 20 20 20 20 20 20  OrderBy;..      
364bd 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 3d  pSub->pOrderBy =
364be 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69   0;..    }else i
364bf 66 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64  f( pParent->pOrd
364c0 65 72 42 79 20 29 7b 0d 0a 20 20 20 20 20 20 73  erBy ){..      s
364c1 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
364c2 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
364c3 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  By, iParent, pSu
364c4 62 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20  b->pEList);..   
364c5 20 7d 0d 0a 20 20 20 20 69 66 28 20 70 53 75 62   }..    if( pSub
364c6 2d 3e 70 57 68 65 72 65 20 29 7b 0d 0a 20 20 20  ->pWhere ){..   
364c7 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69     pWhere = sqli
364c8 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
364c9 53 75 62 2d 3e 70 57 68 65 72 65 2c 20 30 29 3b  Sub->pWhere, 0);
364ca 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
364cb 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0d      pWhere = 0;.
364cc 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
364cd 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b  subqueryIsAgg ){
364ce 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
364cf 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67  pParent->pHaving
364d0 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70 50  ==0 );..      pP
364d1 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d  arent->pHaving =
364d2 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65   pParent->pWhere
364d3 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  ;..      pParent
364d4 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72  ->pWhere = pWher
364d5 65 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 65 6e  e;..      pParen
364d6 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62  t->pHaving = sub
364d7 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65  stExpr(db, pPare
364d8 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61  nt->pHaving, iPa
364d9 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
364da 73 74 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 72  st);..      pPar
364db 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73  ent->pHaving = s
364dc 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
364dd 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69  , pParent->pHavi
364de 6e 67 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ng, ..          
364df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364e0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
364e1 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 62 2d  xprDup(db, pSub-
364e2 3e 70 48 61 76 69 6e 67 2c 20 30 29 29 3b 0d 0a  >pHaving, 0));..
364e3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
364e4 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 3d  arent->pGroupBy=
364e5 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70 50 61  =0 );..      pPa
364e6 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 20 3d  rent->pGroupBy =
364e7 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
364e8 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 47  Dup(db, pSub->pG
364e9 72 6f 75 70 42 79 2c 20 30 29 3b 0d 0a 20 20 20  roupBy, 0);..   
364ea 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70   }else{..      p
364eb 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
364ec 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70   substExpr(db, p
364ed 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20  Parent->pWhere, 
364ee 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70  iParent, pSub->p
364ef 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 70  EList);..      p
364f0 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
364f1 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
364f2 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68  db, pParent->pWh
364f3 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0d 0a 20  ere, pWhere);.. 
364f4 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a     }..  ..    /*
364f5 20 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71   The flattened q
364f6 75 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74  uery is distinct
364f7 20 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69   if either the i
364f8 6e 6e 65 72 20 6f 72 20 74 68 65 0d 0a 20 20 20  nner or the..   
364f9 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20   ** outer query 
364fa 69 73 20 64 69 73 74 69 6e 63 74 2e 20 0d 0a 20  is distinct. .. 
364fb 20 20 20 2a 2f 0d 0a 20 20 20 20 70 50 61 72 65     */..    pPare
364fc 6e 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20  nt->selFlags |= 
364fd 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26  pSub->selFlags &
364fe 20 53 46 5f 44 69 73 74 69 6e 63 74 3b 0d 0a 20   SF_Distinct;.. 
364ff 20 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20 2a   ..    /*..    *
36500 2a 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f  * SELECT ... FRO
36501 4d 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49  M (SELECT ... LI
36502 4d 49 54 20 61 20 4f 46 46 53 45 54 20 62 29 20  MIT a OFFSET b) 
36503 4c 49 4d 49 54 20 78 20 4f 46 46 53 45 54 20 79  LIMIT x OFFSET y
36504 3b 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  ;..    **..    *
36505 2a 20 4f 6e 65 20 69 73 20 74 65 6d 70 74 65 64  * One is tempted
36506 20 74 6f 20 74 72 79 20 74 6f 20 61 64 64 20 61   to try to add a
36507 20 61 6e 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e   and b to combin
36508 65 20 74 68 65 20 6c 69 6d 69 74 73 2e 20 20 42  e the limits.  B
36509 75 74 20 74 68 69 73 0d 0a 20 20 20 20 2a 2a 20  ut this..    ** 
3650a 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 69 66  does not work if
3650b 20 65 69 74 68 65 72 20 6c 69 6d 69 74 20 69 73   either limit is
3650c 20 6e 65 67 61 74 69 76 65 2e 0d 0a 20 20 20 20   negative...    
3650d 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 53 75 62  */..    if( pSub
3650e 2d 3e 70 4c 69 6d 69 74 20 29 7b 0d 0a 20 20 20  ->pLimit ){..   
3650f 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69 6d     pParent->pLim
36510 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d 69  it = pSub->pLimi
36511 74 3b 0d 0a 20 20 20 20 20 20 70 53 75 62 2d 3e  t;..      pSub->
36512 70 4c 69 6d 69 74 20 3d 20 30 3b 0d 0a 20 20 20  pLimit = 0;..   
36513 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20   }..  }....  /* 
36514 46 69 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65  Finially, delete
36515 20 77 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66   what is left of
36516 20 74 68 65 20 73 75 62 71 75 65 72 79 20 61 6e   the subquery an
36517 64 20 72 65 74 75 72 6e 0d 0a 20 20 2a 2a 20 73  d return..  ** s
36518 75 63 63 65 73 73 2e 0d 0a 20 20 2a 2f 0d 0a 20  uccess...  */.. 
36519 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
3651a 6c 65 74 65 28 64 62 2c 20 70 53 75 62 31 29 3b  lete(db, pSub1);
3651b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d  ....  return 1;.
3651c 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  .}..#endif /* !d
3651d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
3651e 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20  IT_SUBQUERY) || 
3651f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
36520 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0d 0a 0d  OMIT_VIEW) */...
36521 0a 2f 2a 0d 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20  ./*..** Analyze 
36522 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
36523 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73 20 61  ment passed as a
36524 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 65  n argument to se
36525 65 20 69 66 20 69 74 0d 0a 2a 2a 20 69 73 20 61  e if it..** is a
36526 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20   min() or max() 
36527 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20 57 48  query. Return WH
36528 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20  ERE_ORDERBY_MIN 
36529 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  or WHERE_ORDERBY
3652a 5f 4d 41 58 20 69 66 20 0d 0a 2a 2a 20 69 74 20  _MAX if ..** it 
3652b 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69  is, or 0 otherwi
3652c 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c 20  se. At present, 
3652d 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73 69  a query is consi
3652e 64 65 72 65 64 20 74 6f 20 62 65 0d 0a 2a 2a 20  dered to be..** 
3652f 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75  a min()/max() qu
36530 65 72 79 20 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ery if:..**..** 
36531 20 20 31 2e 20 54 68 65 72 65 20 69 73 20 61 20    1. There is a 
36532 73 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e  single object in
36533 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
36534 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 2e 20 54  ...**..**   2. T
36535 68 65 72 65 20 69 73 20 61 20 73 69 6e 67 6c 65  here is a single
36536 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
36537 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 61  he result set, a
36538 6e 64 20 69 74 20 69 73 0d 0a 2a 2a 20 20 20 20  nd it is..**    
36539 20 20 65 69 74 68 65 72 20 6d 69 6e 28 78 29 20    either min(x) 
3653a 6f 72 20 6d 61 78 28 78 29 2c 20 77 68 65 72 65  or max(x), where
3653b 20 78 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 72   x is a column r
3653c 65 66 65 72 65 6e 63 65 2e 0d 0a 2a 2f 0d 0a 73  eference...*/..s
3653d 74 61 74 69 63 20 75 38 20 6d 69 6e 4d 61 78 51  tatic u8 minMaxQ
3653e 75 65 72 79 28 53 65 6c 65 63 74 20 2a 70 29 7b  uery(Select *p){
3653f 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  ..  Expr *pExpr;
36540 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
36541 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74  List = p->pEList
36542 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 45 4c 69 73  ;....  if( pELis
36543 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65  t->nExpr!=1 ) re
36544 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
36545 42 59 5f 4e 4f 52 4d 41 4c 3b 0d 0a 20 20 70 45  BY_NORMAL;..  pE
36546 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  xpr = pEList->a[
36547 30 5d 2e 70 45 78 70 72 3b 0d 0a 20 20 69 66 28  0].pExpr;..  if(
36548 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41   pExpr->op!=TK_A
36549 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65  GG_FUNCTION ) re
3654a 74 75 72 6e 20 30 3b 0d 0a 20 20 69 66 28 20 4e  turn 0;..  if( N
3654b 45 56 45 52 28 45 78 70 72 48 61 73 50 72 6f 70  EVER(ExprHasProp
3654c 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
3654d 49 73 53 65 6c 65 63 74 29 29 20 29 20 72 65 74  IsSelect)) ) ret
3654e 75 72 6e 20 30 3b 0d 0a 20 20 70 45 4c 69 73 74  urn 0;..  pEList
3654f 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
36550 74 3b 0d 0a 20 20 69 66 28 20 70 45 4c 69 73 74  t;..  if( pEList
36551 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e 6e  ==0 || pEList->n
36552 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e  Expr!=1 ) return
36553 20 30 3b 0d 0a 20 20 69 66 28 20 70 45 4c 69 73   0;..  if( pELis
36554 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
36555 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p!=TK_AGG_COLUMN
36556 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
36557 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0d  ORDERBY_NORMAL;.
36558 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
36559 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
3655a 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
3655b 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
3655c 33 53 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e  3StrICmp(pExpr->
3655d 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 69 6e 22 29 3d  u.zToken,"min")=
3655e 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
3655f 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
36560 4d 49 4e 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  MIN;..  }else if
36561 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
36562 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pExpr->u.zToken
36563 2c 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0d 0a 20  ,"max")==0 ){.. 
36564 20 20 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f     return WHERE_
36565 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0d 0a 20 20  ORDERBY_MAX;..  
36566 7d 0d 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52  }..  return WHER
36567 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
36568 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
36569 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  he select statem
3656a 65 6e 74 20 70 61 73 73 65 64 20 61 73 20 74 68  ent passed as th
3656b 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3656c 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65   is an aggregate
3656d 20 71 75 65 72 79 2e 0d 0a 2a 2a 20 54 68 65 20   query...** The 
3656e 73 65 63 6f 6e 64 20 61 72 67 6d 65 6e 74 20 69  second argment i
3656f 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  s the associated
36570 20 61 67 67 72 65 67 61 74 65 2d 69 6e 66 6f 20   aggregate-info 
36571 6f 62 6a 65 63 74 2e 20 54 68 69 73 20 0d 0a 2a  object. This ..*
36572 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65 73 74 73  * function tests
36573 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69   if the SELECT i
36574 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0d 0a  s of the form:..
36575 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  **..**   SELECT 
36576 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74  count(*) FROM <t
36577 62 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 77 68 65 72  bl>..**..** wher
36578 65 20 74 61 62 6c 65 20 69 73 20 61 20 64 61 74  e table is a dat
36579 61 62 61 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74  abase table, not
3657a 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72   a sub-select or
3657b 20 76 69 65 77 2e 20 49 66 20 74 68 65 20 71 75   view. If the qu
3657c 65 72 79 0d 0a 2a 2a 20 64 6f 65 73 20 6d 61 74  ery..** does mat
3657d 63 68 20 74 68 69 73 20 70 61 74 74 65 72 6e 2c  ch this pattern,
3657e 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20   then a pointer 
3657f 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a  to the Table obj
36580 65 63 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67  ect representing
36581 0d 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73 20 72 65  ..** <tbl> is re
36582 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
36583 65 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  e, 0 is returned
36584 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 54 61  ...*/..static Ta
36585 62 6c 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f 75  ble *isSimpleCou
36586 6e 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 41 67  nt(Select *p, Ag
36587 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
36588 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  {..  Table *pTab
36589 3b 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  ;..  Expr *pExpr
3658a 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 21  ;....  assert( !
3658b 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 3b 0d 0a  p->pGroupBy );..
3658c 0d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72  ..  if( p->pWher
3658d 65 20 7c 7c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  e || p->pEList->
3658e 6e 45 78 70 72 21 3d 31 20 0d 0a 20 20 20 7c 7c  nExpr!=1 ..   ||
3658f 20 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d   p->pSrc->nSrc!=
36590 31 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  1 || p->pSrc->a[
36591 30 5d 2e 70 53 65 6c 65 63 74 0d 0a 20 20 29 7b  0].pSelect..  ){
36592 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
36593 0a 20 20 7d 0d 0a 20 20 70 54 61 62 20 3d 20 70  .  }..  pTab = p
36594 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
36595 62 3b 0d 0a 20 20 70 45 78 70 72 20 3d 20 70 2d  b;..  pExpr = p-
36596 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
36597 78 70 72 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  xpr;..  assert( 
36598 70 54 61 62 20 26 26 20 21 70 54 61 62 2d 3e 70  pTab && !pTab->p
36599 53 65 6c 65 63 74 20 26 26 20 70 45 78 70 72 20  Select && pExpr 
3659a 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 49 73 56 69  );....  if( IsVi
3659b 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65  rtual(pTab) ) re
3659c 74 75 72 6e 20 30 3b 0d 0a 20 20 69 66 28 20 70  turn 0;..  if( p
3659d 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
3659e 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74 75  _FUNCTION ) retu
3659f 72 6e 20 30 3b 0d 0a 20 20 69 66 28 20 28 70 41  rn 0;..  if( (pA
365a0 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d  ggInfo->aFunc[0]
365a1 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 26 53 51  .pFunc->flags&SQ
365a2 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29  LITE_FUNC_COUNT)
365a3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ==0 ) return 0;.
365a4 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c  .  if( pExpr->fl
365a5 61 67 73 26 45 50 5f 44 69 73 74 69 6e 63 74 20  ags&EP_Distinct 
365a6 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 0d 0a 20  ) return 0;.... 
365a7 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0d 0a 7d   return pTab;..}
365a8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  ..../*..** If th
365a9 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 69 74  e source-list it
365aa 65 6d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  em passed as an 
365ab 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61 75 67  argument was aug
365ac 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e 0d 0a  mented with an..
365ad 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  ** INDEXED BY cl
365ae 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20 74  ause, then try t
365af 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70 65  o locate the spe
365b0 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49 66  cified index. If
365b1 20 74 68 65 72 65 0d 0a 2a 2a 20 77 61 73 20 73   there..** was s
365b2 75 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64  uch a clause and
365b3 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78   the named index
365b4 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
365b5 2c 20 72 65 74 75 72 6e 20 0d 0a 2a 2a 20 53 51  , return ..** SQ
365b6 4c 49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c  LITE_ERROR and l
365b7 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
365b8 20 70 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69   pParse. Otherwi
365b9 73 65 2c 20 70 6f 70 75 6c 61 74 65 20 0d 0a 2a  se, populate ..*
365ba 2a 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20  * pFrom->pIndex 
365bb 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
365bc 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  E_OK...*/..SQLIT
365bd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
365be 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f  lite3IndexedByLo
365bf 6f 6b 75 70 28 50 61 72 73 65 20 2a 70 50 61 72  okup(Parse *pPar
365c0 73 65 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69  se, struct SrcLi
365c1 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 29 7b  st_item *pFrom){
365c2 0d 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70  ..  if( pFrom->p
365c3 54 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e 7a 49  Tab && pFrom->zI
365c4 6e 64 65 78 20 29 7b 0d 0a 20 20 20 20 54 61 62  ndex ){..    Tab
365c5 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d  le *pTab = pFrom
365c6 2d 3e 70 54 61 62 3b 0d 0a 20 20 20 20 63 68 61  ->pTab;..    cha
365c7 72 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f  r *zIndex = pFro
365c8 6d 2d 3e 7a 49 6e 64 65 78 3b 0d 0a 20 20 20 20  m->zIndex;..    
365c9 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a 20 20  Index *pIdx;..  
365ca 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
365cb 3e 70 49 6e 64 65 78 3b 20 0d 0a 20 20 20 20 20  >pIndex; ..     
365cc 20 20 20 70 49 64 78 20 26 26 20 73 71 6c 69 74     pIdx && sqlit
365cd 65 33 53 74 72 49 43 6d 70 28 70 49 64 78 2d 3e  e3StrICmp(pIdx->
365ce 7a 4e 61 6d 65 2c 20 7a 49 6e 64 65 78 29 3b 20  zName, zIndex); 
365cf 0d 0a 20 20 20 20 20 20 20 20 70 49 64 78 3d 70  ..        pIdx=p
365d0 49 64 78 2d 3e 70 4e 65 78 74 0d 0a 20 20 20 20  Idx->pNext..    
365d1 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 49 64  );..    if( !pId
365d2 78 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  x ){..      sqli
365d3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
365d4 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e 64  se, "no such ind
365d5 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65 78 2c  ex: %s", zIndex,
365d6 20 30 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 72   0);..      pPar
365d7 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20  se->checkSchema 
365d8 3d 20 31 3b 0d 0a 20 20 20 20 20 20 72 65 74 75  = 1;..      retu
365d9 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
365da 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 46 72  ..    }..    pFr
365db 6f 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64  om->pIndex = pId
365dc 78 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  x;..  }..  retur
365dd 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
365de 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  .../*..** This r
365df 6f 75 74 69 6e 65 20 69 73 20 61 20 57 61 6c 6b  outine is a Walk
365e0 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  er callback for 
365e1 22 65 78 70 61 6e 64 69 6e 67 22 20 61 20 53 45  "expanding" a SE
365e2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0d  LECT statement..
365e3 0a 2a 2a 20 22 45 78 70 61 6e 64 69 6e 67 22 20  .** "Expanding" 
365e4 6d 65 61 6e 73 20 74 6f 20 64 6f 20 74 68 65 20  means to do the 
365e5 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a  following:..**..
365e6 2a 2a 20 20 20 20 28 31 29 20 20 4d 61 6b 65 20  **    (1)  Make 
365e7 73 75 72 65 20 56 44 42 45 20 63 75 72 73 6f 72  sure VDBE cursor
365e8 20 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62 65   numbers have be
365e9 65 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65  en assigned to e
365ea 76 65 72 79 0d 0a 2a 2a 20 20 20 20 20 20 20 20  very..**        
365eb 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
365ec 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0d 0a 2a 2a  FROM clause...**
365ed 0d 0a 2a 2a 20 20 20 20 28 32 29 20 20 46 69 6c  ..**    (2)  Fil
365ee 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69 73  l in the pTabLis
365ef 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65 6c  t->a[].pTab fiel
365f0 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73  ds in the SrcLis
365f1 74 20 74 68 61 74 20 0d 0a 2a 2a 20 20 20 20 20  t that ..**     
365f2 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f 4d      defines FROM
365f3 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20 76   clause.  When v
365f4 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20 74  iews appear in t
365f5 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0d  he FROM clause,.
365f6 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c  .**         fill
365f7 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70   pTabList->a[].p
365f8 53 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f  Select with a co
365f9 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  py of the SELECT
365fa 20 73 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 20   statement..**  
365fb 20 20 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c         that impl
365fc 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e  ements the view.
365fd 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65    A copy is made
365fe 20 6f 66 20 74 68 65 20 76 69 65 77 27 73 20 53   of the view's S
365ff 45 4c 45 43 54 0d 0a 2a 2a 20 20 20 20 20 20 20  ELECT..**       
36600 20 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74    statement so t
36601 68 61 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c  hat we can freel
36602 79 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65  y modify or dele
36603 74 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  te that statemen
36604 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69  t..**         wi
36605 74 68 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61  thout worrying a
36606 62 6f 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20  bout messing up 
36607 74 68 65 20 70 72 65 73 69 73 74 65 6e 74 20 72  the presistent r
36608 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0d 0a 2a  epresentation..*
36609 2a 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65  *         of the
3660a 20 76 69 65 77 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20   view...**..**  
3660b 20 20 28 33 29 20 20 41 64 64 20 74 65 72 6d 73    (3)  Add terms
3660c 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
3660d 61 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f 64 61  ause to accomoda
3660e 74 65 20 74 68 65 20 4e 41 54 55 52 41 4c 20 6b  te the NATURAL k
3660f 65 79 77 6f 72 64 0d 0a 2a 2a 20 20 20 20 20 20  eyword..**      
36610 20 20 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64 20     on joins and 
36611 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  the ON and USING
36612 20 63 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e 73   clause of joins
36613 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 28 34 29  ...**..**    (4)
36614 20 20 53 63 61 6e 20 74 68 65 20 6c 69 73 74 20    Scan the list 
36615 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
36616 65 20 72 65 73 75 6c 74 20 73 65 74 20 28 70 45  e result set (pE
36617 4c 69 73 74 29 20 6c 6f 6f 6b 69 6e 67 0d 0a 2a  List) looking..*
36618 2a 20 20 20 20 20 20 20 20 20 66 6f 72 20 69 6e  *         for in
36619 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 22  stances of the "
3661a 2a 22 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 74  *" operator or t
3661b 68 65 20 54 41 42 4c 45 2e 2a 20 6f 70 65 72 61  he TABLE.* opera
3661c 74 6f 72 2e 0d 0a 2a 2a 20 20 20 20 20 20 20 20  tor...**        
3661d 20 49 66 20 66 6f 75 6e 64 2c 20 65 78 70 61 6e   If found, expan
3661e 64 20 65 61 63 68 20 22 2a 22 20 74 6f 20 62 65  d each "*" to be
3661f 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e   every column in
36620 20 65 76 65 72 79 20 74 61 62 6c 65 0d 0a 2a 2a   every table..**
36621 20 20 20 20 20 20 20 20 20 61 6e 64 20 54 41 42           and TAB
36622 4c 45 2e 2a 20 74 6f 20 62 65 20 65 76 65 72 79  LE.* to be every
36623 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 41 42 4c 45   column in TABLE
36624 2e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ...**..*/..stati
36625 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70 61  c int selectExpa
36626 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  nder(Walker *pWa
36627 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
36628 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  {..  Parse *pPar
36629 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
3662a 61 72 73 65 3b 0d 0a 20 20 69 6e 74 20 69 2c 20  arse;..  int i, 
3662b 6a 2c 20 6b 3b 0d 0a 20 20 53 72 63 4c 69 73 74  j, k;..  SrcList
3662c 20 2a 70 54 61 62 4c 69 73 74 3b 0d 0a 20 20 45   *pTabList;..  E
3662d 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
3662e 0d 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ..  struct SrcLi
3662f 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0d  st_item *pFrom;.
36630 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
36631 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a   pParse->db;....
36632 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
36633 46 61 69 6c 65 64 20 20 29 7b 0d 0a 20 20 20 20  Failed  ){..    
36634 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
36635 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 4e 45  ;..  }..  if( NE
36636 56 45 52 28 70 2d 3e 70 53 72 63 3d 3d 30 29 20  VER(p->pSrc==0) 
36637 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
36638 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d  & SF_Expanded)!=
36639 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  0 ){..    return
3663a 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a 20 20 7d   WRC_Prune;..  }
3663b 0d 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  ..  p->selFlags 
3663c 7c 3d 20 53 46 5f 45 78 70 61 6e 64 65 64 3b 0d  |= SF_Expanded;.
3663d 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d  .  pTabList = p-
3663e 3e 70 53 72 63 3b 0d 0a 20 20 70 45 4c 69 73 74  >pSrc;..  pEList
3663f 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0d 0a 0d   = p->pEList;...
36640 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
36641 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68  cursor numbers h
36642 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65  ave been assigne
36643 64 20 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73  d to all entries
36644 20 69 6e 0d 0a 20 20 2a 2a 20 74 68 65 20 46 52   in..  ** the FR
36645 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
36646 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
36647 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69  t...  */..  sqli
36648 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
36649 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20  Cursors(pParse, 
3664a 70 54 61 62 4c 69 73 74 29 3b 0d 0a 0d 0a 20 20  pTabList);....  
3664b 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79  /* Look up every
3664c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 69 6e 20   table named in 
3664d 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
3664e 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20  of the select.  
3664f 49 66 0d 0a 20 20 2a 2a 20 61 6e 20 65 6e 74 72  If..  ** an entr
36650 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  y of the FROM cl
36651 61 75 73 65 20 69 73 20 61 20 73 75 62 71 75 65  ause is a subque
36652 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  ry instead of a 
36653 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c 0d 0a  table or view,..
36654 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65    ** then create
36655 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
36656 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  le structure to 
36657 64 65 73 63 72 69 62 65 20 74 68 65 20 73 75 62  describe the sub
36658 71 75 65 72 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  query...  */..  
36659 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70  for(i=0, pFrom=p
3665a 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54  TabList->a; i<pT
3665b 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
3665c 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0d 0a 20 20  +, pFrom++){..  
3665d 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a    Table *pTab;..
3665e 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70      if( pFrom->p
3665f 54 61 62 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Tab!=0 ){..     
36660 20 2f 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65   /* This stateme
36661 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  nt has already b
36662 65 65 6e 20 70 72 65 70 61 72 65 64 2e 20 20 54  een prepared.  T
36663 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0d  here is no need.
36664 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67 6f 20  .      ** to go 
36665 66 75 72 74 68 65 72 2e 20 2a 2f 0d 0a 20 20 20  further. */..   
36666 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20     assert( i==0 
36667 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  );..      return
36668 20 57 52 43 5f 50 72 75 6e 65 3b 0d 0a 20 20 20   WRC_Prune;..   
36669 20 7d 0d 0a 20 20 20 20 69 66 28 20 70 46 72 6f   }..    if( pFro
3666a 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0d 0a  m->zName==0 ){..
3666b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3666c 4d 49 54 5f 53 55 42 51 55 45 52 59 0d 0a 20 20  MIT_SUBQUERY..  
3666d 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
3666e 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63   = pFrom->pSelec
3666f 74 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 41 20 73  t;..      /* A s
36670 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20  ub-query in the 
36671 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
36672 20 53 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 20 20   SELECT */..    
36673 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 21 3d    assert( pSel!=
36674 30 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  0 );..      asse
36675 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d  rt( pFrom->pTab=
36676 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 );..      sql
36677 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
36678 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0d 0a  Walker, pSel);..
36679 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61        pFrom->pTa
3667a 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74  b = pTab = sqlit
3667b 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
3667c 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29  b, sizeof(Table)
3667d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54  );..      if( pT
3667e 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ab==0 ) return W
3667f 52 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20 20  RC_Abort;..     
36680 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b   pTab->nRef = 1;
36681 0d 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e  ..      pTab->zN
36682 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
36683 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65  intf(db, "sqlite
36684 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22 2c 20  _subquery_%p_", 
36685 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0d 0a 20  (void*)pTab);.. 
36686 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c       while( pSel
36687 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65 6c  ->pPrior ){ pSel
36688 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
36689 20 7d 0d 0a 20 20 20 20 20 20 73 65 6c 65 63 74   }..      select
3668a 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
3668b 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ist(pParse, pSel
3668c 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
3668d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
3668e 6f 6c 29 3b 0d 0a 20 20 20 20 20 20 70 54 61 62  ol);..      pTab
3668f 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0d 0a 20  ->iPKey = -1;.. 
36690 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 6f 77 45       pTab->nRowE
36691 73 74 20 3d 20 31 30 30 30 30 30 30 3b 0d 0a 20  st = 1000000;.. 
36692 20 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c       pTab->tabFl
36693 61 67 73 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65  ags |= TF_Epheme
36694 72 61 6c 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  ral;..#endif..  
36695 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
36696 2f 2a 20 41 6e 20 6f 72 64 69 6e 61 72 79 20 74  /* An ordinary t
36697 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d  able or view nam
36698 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
36699 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20 20 20 61  ause */..      a
3669a 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54  ssert( pFrom->pT
3669b 61 62 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ab==0 );..      
3669c 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54  pFrom->pTab = pT
3669d 61 62 20 3d 20 0d 0a 20 20 20 20 20 20 20 20 73  ab = ..        s
3669e 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
3669f 65 28 70 50 61 72 73 65 2c 30 2c 70 46 72 6f 6d  e(pParse,0,pFrom
366a0 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d 2d 3e 7a  ->zName,pFrom->z
366a1 44 61 74 61 62 61 73 65 29 3b 0d 0a 20 20 20 20  Database);..    
366a2 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
366a3 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
366a4 3b 0d 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e  ;..      pTab->n
366a5 52 65 66 2b 2b 3b 0d 0a 23 69 66 20 21 64 65 66  Ref++;..#if !def
366a6 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
366a7 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
366a8 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ed (SQLITE_OMIT_
366a9 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0d 0a 20  VIRTUALTABLE).. 
366aa 20 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70       if( pTab->p
366ab 53 65 6c 65 63 74 20 7c 7c 20 49 73 56 69 72 74  Select || IsVirt
366ac 75 61 6c 28 70 54 61 62 29 20 29 7b 0d 0a 20 20  ual(pTab) ){..  
366ad 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65 61 63        /* We reac
366ae 68 20 68 65 72 65 20 69 66 20 74 68 65 20 6e 61  h here if the na
366af 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61 20 72  med table is a r
366b0 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f 0d  eally a view */.
366b1 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
366b2 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
366b3 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
366b4 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 57 52  Tab) ) return WR
366b5 43 5f 41 62 6f 72 74 3b 0d 0a 20 20 20 20 20 20  C_Abort;..      
366b6 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
366b7 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0d 0a  >pSelect==0 );..
366b8 20 20 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70          pFrom->p
366b9 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
366ba 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
366bb 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b  ab->pSelect, 0);
366bc 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
366bd 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
366be 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c  ker, pFrom->pSel
366bf 65 63 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  ect);..      }..
366c0 23 65 6e 64 69 66 0d 0a 20 20 20 20 7d 0d 0a 0d  #endif..    }...
366c1 0a 20 20 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  .    /* Locate t
366c2 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 62  he index named b
366c3 79 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59  y the INDEXED BY
366c4 20 63 6c 61 75 73 65 2c 20 69 66 20 61 6e 79 2e   clause, if any.
366c5 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   */..    if( sql
366c6 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f  ite3IndexedByLoo
366c7 6b 75 70 28 70 50 61 72 73 65 2c 20 70 46 72 6f  kup(pParse, pFro
366c8 6d 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74  m) ){..      ret
366c9 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0d 0a  urn WRC_Abort;..
366ca 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
366cb 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41 54 55 52  /* Process NATUR
366cc 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64  AL keywords, and
366cd 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
366ce 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0d  auses of joins..
366cf 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d  .  */..  if( db-
366d0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
366d1 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f   sqliteProcessJo
366d2 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b  in(pParse, p) ){
366d3 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  ..    return WRC
366d4 5f 41 62 6f 72 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a  _Abort;..  }....
366d5 20 20 2f 2a 20 46 6f 72 20 65 76 65 72 79 20 22    /* For every "
366d6 2a 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 69  *" that occurs i
366d7 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  n the column lis
366d8 74 2c 20 69 6e 73 65 72 74 20 74 68 65 20 6e 61  t, insert the na
366d9 6d 65 73 20 6f 66 0d 0a 20 20 2a 2a 20 61 6c 6c  mes of..  ** all
366da 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20   columns in all 
366db 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66 6f 72  tables.  And for
366dc 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a 20 69   every TABLE.* i
366dd 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 0d  nsert the names.
366de 0a 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c  .  ** of all col
366df 75 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20 20  umns in TABLE.  
366e0 54 68 65 20 70 61 72 73 65 72 20 69 6e 73 65 72  The parser inser
366e1 74 65 64 20 61 20 73 70 65 63 69 61 6c 20 65 78  ted a special ex
366e2 70 72 65 73 73 69 6f 6e 0d 0a 20 20 2a 2a 20 77  pression..  ** w
366e3 69 74 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f  ith the TK_ALL o
366e4 70 65 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68  perator for each
366e5 20 22 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75   "*" that it fou
366e6 6e 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  nd in the column
366e7 20 6c 69 73 74 2e 0d 0a 20 20 2a 2a 20 54 68 65   list...  ** The
366e8 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
366e9 6a 75 73 74 20 68 61 73 20 74 6f 20 6c 6f 63 61  just has to loca
366ea 74 65 20 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78  te the TK_ALL ex
366eb 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 20 65 78  pressions and ex
366ec 70 61 6e 64 0d 0a 20 20 2a 2a 20 65 61 63 68 20  pand..  ** each 
366ed 6f 6e 65 20 74 6f 20 74 68 65 20 6c 69 73 74 20  one to the list 
366ee 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  of all columns i
366ef 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 0d 0a 20  n all tables... 
366f0 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 66 69   **..  ** The fi
366f1 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74 20 63 68  rst loop just ch
366f2 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74  ecks to see if t
366f3 68 65 72 65 20 61 72 65 20 61 6e 79 20 22 2a 22  here are any "*"
366f4 20 6f 70 65 72 61 74 6f 72 73 0d 0a 20 20 2a 2a   operators..  **
366f5 20 74 68 61 74 20 6e 65 65 64 20 65 78 70 61 6e   that need expan
366f6 64 69 6e 67 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66  ding...  */..  f
366f7 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74  or(k=0; k<pEList
366f8 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0d 0a  ->nExpr; k++){..
366f9 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
366fa 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70  EList->a[k].pExp
366fb 72 3b 0d 0a 20 20 20 20 69 66 28 20 70 45 2d 3e  r;..    if( pE->
366fc 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65  op==TK_ALL ) bre
366fd 61 6b 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  ak;..    assert(
366fe 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20   pE->op!=TK_DOT 
366ff 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30  || pE->pRight!=0
36700 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
36701 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20   pE->op!=TK_DOT 
36702 7c 7c 20 28 70 45 2d 3e 70 4c 65 66 74 21 3d 30  || (pE->pLeft!=0
36703 20 26 26 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f   && pE->pLeft->o
36704 70 3d 3d 54 4b 5f 49 44 29 20 29 3b 0d 0a 20 20  p==TK_ID) );..  
36705 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b    if( pE->op==TK
36706 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70 52 69 67  _DOT && pE->pRig
36707 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29  ht->op==TK_ALL )
36708 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20 20   break;..  }..  
36709 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45  if( k<pEList->nE
3670a 78 70 72 20 29 7b 0d 0a 20 20 20 20 2f 2a 0d 0a  xpr ){..    /*..
3670b 20 20 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74      ** If we get
3670c 20 68 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74   here it means t
3670d 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  he result set co
3670e 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f  ntains one or mo
3670f 72 65 20 22 2a 22 0d 0a 20 20 20 20 2a 2a 20 6f  re "*"..    ** o
36710 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 6e 65  perators that ne
36711 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65  ed to be expande
36712 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68  d.  Loop through
36713 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
36714 0d 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  ..    ** in the 
36715 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20 65  result set and e
36716 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62  xpand them one b
36717 79 20 6f 6e 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  y one...    */..
36718 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
36719 69 73 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45  ist_item *a = pE
3671a 4c 69 73 74 2d 3e 61 3b 0d 0a 20 20 20 20 45 78  List->a;..    Ex
3671b 70 72 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 30  prList *pNew = 0
3671c 3b 0d 0a 20 20 20 20 69 6e 74 20 66 6c 61 67 73  ;..    int flags
3671d 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66   = pParse->db->f
3671e 6c 61 67 73 3b 0d 0a 20 20 20 20 69 6e 74 20 6c  lags;..    int l
3671f 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66 6c 61 67  ongNames = (flag
36720 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43  s & SQLITE_FullC
36721 6f 6c 4e 61 6d 65 73 29 21 3d 30 0d 0a 20 20 20  olNames)!=0..   
36722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36723 20 20 20 26 26 20 28 66 6c 61 67 73 20 26 20 53     && (flags & S
36724 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
36725 6d 65 73 29 3d 3d 30 3b 0d 0a 0d 0a 20 20 20 20  mes)==0;....    
36726 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73  for(k=0; k<pELis
36727 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0d  t->nExpr; k++){.
36728 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20  .      Expr *pE 
36729 3d 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0d 0a 20  = a[k].pExpr;.. 
3672a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 2d       assert( pE-
3672b 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70  >op!=TK_DOT || p
3672c 45 2d 3e 70 52 69 67 68 74 21 3d 30 20 29 3b 0d  E->pRight!=0 );.
3672d 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f  .      if( pE->o
3672e 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 20 28 70 45  p!=TK_ALL && (pE
3672f 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20  ->op!=TK_DOT || 
36730 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d  pE->pRight->op!=
36731 54 4b 5f 41 4c 4c 29 20 29 7b 0d 0a 20 20 20 20  TK_ALL) ){..    
36732 20 20 20 20 2f 2a 20 54 68 69 73 20 70 61 72 74      /* This part
36733 69 63 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  icular expressio
36734 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
36735 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 0d  to be expanded..
36736 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  .        */..   
36737 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
36738 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
36739 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20  d(pParse, pNew, 
3673a 61 5b 6b 5d 2e 70 45 78 70 72 29 3b 0d 0a 20 20  a[k].pExpr);..  
3673b 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
3673c 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65  {..          pNe
3673d 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72  w->a[pNew->nExpr
3673e 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d  -1].zName = a[k]
3673f 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20 20  .zName;..       
36740 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d     pNew->a[pNew-
36741 3e 6e 45 78 70 72 2d 31 5d 2e 7a 53 70 61 6e 20  >nExpr-1].zSpan 
36742 3d 20 61 5b 6b 5d 2e 7a 53 70 61 6e 3b 0d 0a 20  = a[k].zSpan;.. 
36743 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e           a[k].zN
36744 61 6d 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ame = 0;..      
36745 20 20 20 20 61 5b 6b 5d 2e 7a 53 70 61 6e 20 3d      a[k].zSpan =
36746 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a   0;..        }..
36747 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78          a[k].pEx
36748 70 72 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d  pr = 0;..      }
36749 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2f  else{..        /
3674a 2a 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f  * This expressio
3674b 6e 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61 20  n is a "*" or a 
3674c 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65  "TABLE.*" and ne
3674d 65 64 73 20 74 6f 20 62 65 0d 0a 20 20 20 20 20  eds to be..     
3674e 20 20 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20     ** expanded. 
3674f 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  */..        int 
36750 74 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20  tableSeen = 0;  
36751 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20      /* Set to 1 
36752 77 68 65 6e 20 54 41 42 4c 45 20 6d 61 74 63 68  when TABLE match
36753 65 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 63  es */..        c
36754 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20  har *zTName;    
36755 20 20 20 20 20 20 20 20 2f 2a 20 74 65 78 74 20          /* text 
36756 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45  of name of TABLE
36757 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28   */..        if(
36758 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
36759 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73  ){..          as
3675a 73 65 72 74 28 20 70 45 2d 3e 70 4c 65 66 74 21  sert( pE->pLeft!
3675b 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  =0 );..         
3675c 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
3675d 73 50 72 6f 70 65 72 74 79 28 70 45 2d 3e 70 4c  sProperty(pE->pL
3675e 65 66 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  eft, EP_IntValue
3675f 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) );..          
36760 7a 54 4e 61 6d 65 20 3d 20 70 45 2d 3e 70 4c 65  zTName = pE->pLe
36761 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20  ft->u.zToken;.. 
36762 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
36763 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
36764 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  = 0;..        }.
36765 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
36766 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74  , pFrom=pTabList
36767 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
36768 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f  >nSrc; i++, pFro
36769 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  m++){..         
3676a 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
3676b 46 72 6f 6d 2d 3e 70 54 61 62 3b 0d 0a 20 20 20  From->pTab;..   
3676c 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61         char *zTa
3676d 62 4e 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a  bName = pFrom->z
3676e 41 6c 69 61 73 3b 0d 0a 20 20 20 20 20 20 20 20  Alias;..        
3676f 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d    if( zTabName==
36770 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  0 ){..          
36771 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61    zTabName = pTa
36772 62 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20  b->zName;..     
36773 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
36774 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
36775 46 61 69 6c 65 64 20 29 20 62 72 65 61 6b 3b 0d  Failed ) break;.
36776 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
36777 54 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33  TName && sqlite3
36778 53 74 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20  StrICmp(zTName, 
36779 7a 54 61 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0d  zTabName)!=0 ){.
3677a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .            con
3677b 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 20 20  tinue;..        
3677c 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 74    }..          t
3677d 61 62 6c 65 53 65 65 6e 20 3d 20 31 3b 0d 0a 20  ableSeen = 1;.. 
3677e 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
3677f 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
36780 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  j++){..         
36781 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20     Expr *pExpr, 
36782 2a 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20  *pRight;..      
36783 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
36784 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a  e = pTab->aCol[j
36785 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 20 20  ].zName;..      
36786 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
36787 6e 61 6d 65 3b 20 20 2f 2a 20 54 68 65 20 63 6f  name;  /* The co
36788 6d 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  mputed column na
36789 6d 65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  me */..         
3678a 20 20 20 63 68 61 72 20 2a 7a 54 6f 46 72 65 65     char *zToFree
3678b 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64 20  ;   /* Malloced 
3678c 73 74 72 69 6e 67 20 74 68 61 74 20 6e 65 65 64  string that need
3678d 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f  s to be freed */
3678e 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 54 6f  ..            To
3678f 6b 65 6e 20 73 43 6f 6c 6e 61 6d 65 3b 20 20 2f  ken sColname;  /
36790 2a 20 43 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d  * Computed colum
36791 6e 20 6e 61 6d 65 20 61 73 20 61 20 74 6f 6b 65  n name as a toke
36792 6e 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 20 20  n */....        
36793 20 20 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75      /* If a colu
36794 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  mn is marked as 
36795 27 68 69 64 64 65 6e 27 20 28 63 75 72 72 65 6e  'hidden' (curren
36796 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c  tly only possibl
36797 65 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e..            *
36798 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20 74 61  * for virtual ta
36799 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e  bles), do not in
3679a 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68 65 20  clude it in the 
3679b 65 78 70 61 6e 64 65 64 0d 0a 20 20 20 20 20 20  expanded..      
3679c 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 2d        ** result-
3679d 73 65 74 20 6c 69 73 74 2e 0d 0a 20 20 20 20 20  set list...     
3679e 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
3679f 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64         if( IsHid
367a0 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d  denColumn(&pTab-
367a1 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0d 0a 20 20  >aCol[j]) ){..  
367a2 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
367a3 72 74 28 49 73 56 69 72 74 75 61 6c 28 70 54 61  rt(IsVirtual(pTa
367a4 62 29 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  b));..          
367a5 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20      continue;.. 
367a6 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a             }....
367a7 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
367a8 69 3e 30 20 26 26 20 7a 54 4e 61 6d 65 3d 3d 30  i>0 && zTName==0
367a9 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
367aa 20 20 20 69 66 28 20 28 70 46 72 6f 6d 2d 3e 6a     if( (pFrom->j
367ab 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54  ointype & JT_NAT
367ac 55 52 41 4c 29 21 3d 30 0d 0a 20 20 20 20 20 20  URAL)!=0..      
367ad 20 20 20 20 20 20 20 20 20 20 26 26 20 74 61 62            && tab
367ae 6c 65 41 6e 64 43 6f 6c 75 6d 6e 49 6e 64 65 78  leAndColumnIndex
367af 28 70 54 61 62 4c 69 73 74 2c 20 69 2c 20 7a 4e  (pTabList, i, zN
367b0 61 6d 65 2c 20 30 2c 20 30 29 0d 0a 20 20 20 20  ame, 0, 0)..    
367b1 20 20 20 20 20 20 20 20 20 20 29 7b 0d 0a 20 20            ){..  
367b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
367b3 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a 6f   In a NATURAL jo
367b4 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f 69  in, omit the joi
367b5 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 74  n columns from t
367b6 68 65 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  he ..           
367b7 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 74 6f       ** table to
367b8 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68   the right of th
367b9 65 20 6a 6f 69 6e 20 2a 2f 0d 0a 20 20 20 20 20  e join */..     
367ba 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
367bb 6e 75 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  nue;..          
367bc 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
367bd 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
367be 49 64 4c 69 73 74 49 6e 64 65 78 28 70 46 72 6f  IdListIndex(pFro
367bf 6d 2d 3e 70 55 73 69 6e 67 2c 20 7a 4e 61 6d 65  m->pUsing, zName
367c0 29 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )>=0 ){..       
367c1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 61           /* In a
367c2 20 6a 6f 69 6e 20 77 69 74 68 20 61 20 55 53 49   join with a USI
367c3 4e 47 20 63 6c 61 75 73 65 2c 20 6f 6d 69 74 20  NG clause, omit 
367c4 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0d 0a  columns in the..
367c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367c6 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75 73 65 20  ** using clause 
367c7 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 6f  from the table o
367c8 6e 20 74 68 65 20 72 69 67 68 74 2e 20 2a 2f 0d  n the right. */.
367c9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
367ca 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20   continue;..    
367cb 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
367cc 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
367cd 20 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d          pRight =
367ce 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
367cf 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29 3b 0d   TK_ID, zName);.
367d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f  .            zCo
367d1 6c 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0d 0a  lname = zName;..
367d2 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46              zToF
367d3 72 65 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ree = 0;..      
367d4 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61        if( longNa
367d5 6d 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d  mes || pTabList-
367d6 3e 6e 53 72 63 3e 31 20 29 7b 0d 0a 20 20 20 20  >nSrc>1 ){..    
367d7 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
367d8 70 4c 65 66 74 3b 0d 0a 20 20 20 20 20 20 20 20  pLeft;..        
367d9 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 73 71        pLeft = sq
367da 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
367db 5f 49 44 2c 20 7a 54 61 62 4e 61 6d 65 29 3b 0d  _ID, zTabName);.
367dc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
367dd 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
367de 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
367df 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  OT, pLeft, pRigh
367e0 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  t, 0);..        
367e1 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61        if( longNa
367e2 6d 65 73 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  mes ){..        
367e3 20 20 20 20 20 20 20 20 7a 43 6f 6c 6e 61 6d 65          zColname
367e4 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
367e5 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a  f(db, "%s.%s", z
367e6 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b  TabName, zName);
367e7 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
367e8 20 20 7a 54 6f 46 72 65 65 20 3d 20 7a 43 6f 6c    zToFree = zCol
367e9 6e 61 6d 65 3b 0d 0a 20 20 20 20 20 20 20 20 20  name;..         
367ea 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
367eb 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
367ec 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
367ed 3d 20 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20  = pRight;..     
367ee 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
367ef 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
367f0 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
367f1 6e 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c  nd(pParse, pNew,
367f2 20 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 20 20   pExpr);..      
367f3 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 7a        sColname.z
367f4 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0d 0a 20 20   = zColname;..  
367f5 20 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61            sColna
367f6 6d 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  me.n = sqlite3St
367f7 72 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d 65 29  rlen30(zColname)
367f8 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ;..            s
367f9 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65  qlite3ExprListSe
367fa 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
367fb 65 77 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20 30  ew, &sColname, 0
367fc 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
367fd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
367fe 2c 20 7a 54 6f 46 72 65 65 29 3b 0d 0a 20 20 20  , zToFree);..   
367ff 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
36800 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
36801 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0d 0a   !tableSeen ){..
36802 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
36803 4e 61 6d 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  Name ){..       
36804 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
36805 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
36806 20 73 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22   such table: %s"
36807 2c 20 7a 54 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  , zTName);..    
36808 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
36809 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3680a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
3680b 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20 73 70 65  , "no tables spe
3680c 63 69 66 69 65 64 22 29 3b 0d 0a 20 20 20 20 20  cified");..     
3680d 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
3680e 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
3680f 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  }..    sqlite3Ex
36810 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
36811 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 70   pEList);..    p
36812 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b  ->pEList = pNew;
36813 0d 0a 20 20 7d 0d 0a 23 69 66 20 53 51 4c 49 54  ..  }..#if SQLIT
36814 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0d 0a 20 20  E_MAX_COLUMN..  
36815 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26  if( p->pEList &&
36816 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
36817 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
36818 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
36819 5d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ] ){..    sqlite
3681a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
3681b 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75  , "too many colu
3681c 6d 6e 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65  mns in result se
3681d 74 22 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69  t");..  }..#endi
3681e 66 0d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  f..  return WRC_
3681f 43 6f 6e 74 69 6e 75 65 3b 0d 0a 7d 0d 0a 0d 0a  Continue;..}....
36820 2f 2a 0d 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75  /*..** No-op rou
36821 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72  tine for the par
36822 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0d  se-tree walker..
36823 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 69  .**..** When thi
36824 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
36825 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c   Walker.xExprCal
36826 6c 62 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65  lback then expre
36827 73 73 69 6f 6e 20 74 72 65 65 73 0d 0a 2a 2a 20  ssion trees..** 
36828 61 72 65 20 77 61 6c 6b 65 64 20 77 69 74 68 6f  are walked witho
36829 75 74 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 62  ut any actions b
3682a 65 69 6e 67 20 74 61 6b 65 6e 20 61 74 20 65 61  eing taken at ea
3682b 63 68 20 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d  ch node.  Presum
3682c 61 62 6c 79 2c 0d 0a 2a 2a 20 77 68 65 6e 20 74  ably,..** when t
3682d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
3682e 73 65 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e 78  sed for Walker.x
3682f 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65  ExprCallback the
36830 6e 20 0d 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53  n ..** Walker.xS
36831 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 69 73  electCallback is
36832 20 73 65 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74   set to do somet
36833 68 69 6e 67 20 75 73 65 66 75 6c 20 66 6f 72 20  hing useful for 
36834 65 76 65 72 79 20 0d 0a 2a 2a 20 73 75 62 71 75  every ..** subqu
36835 65 72 79 20 69 6e 20 74 68 65 20 70 61 72 73 65  ery in the parse
36836 72 20 74 72 65 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  r tree...*/..sta
36837 74 69 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b  tic int exprWalk
36838 4e 6f 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74  Noop(Walker *Not
36839 55 73 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55  Used, Expr *NotU
3683a 73 65 64 32 29 7b 0d 0a 20 20 55 4e 55 53 45 44  sed2){..  UNUSED
3683b 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
3683c 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0d  sed, NotUsed2);.
3683d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f  .  return WRC_Co
3683e 6e 74 69 6e 75 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ntinue;..}..../*
3683f 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
36840 65 20 22 65 78 70 61 6e 64 73 22 20 61 20 53 45  e "expands" a SE
36841 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
36842 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75  nd all of its su
36843 62 71 75 65 72 69 65 73 2e 0d 0a 2a 2a 20 46 6f  bqueries...** Fo
36844 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
36845 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 77 68 61 74  ormation on what
36846 20 69 74 20 6d 65 61 6e 73 20 74 6f 20 22 65 78   it means to "ex
36847 70 61 6e 64 22 20 61 20 53 45 4c 45 43 54 0d 0a  pand" a SELECT..
36848 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65  ** statement, se
36849 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e  e the comment on
3684a 20 74 68 65 20 73 65 6c 65 63 74 45 78 70 61 6e   the selectExpan
3684b 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c 62 61 63  d worker callbac
3684c 6b 20 61 62 6f 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  k above...**..**
3684d 20 45 78 70 61 6e 64 69 6e 67 20 61 20 53 45 4c   Expanding a SEL
3684e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ECT statement is
3684f 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20   the first step 
36850 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 0d  in processing a.
36851 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
36852 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c 45 43  ment.  The SELEC
36853 54 20 73 74 61 74 65 6d 65 6e 74 20 6d 75 73 74  T statement must
36854 20 62 65 20 65 78 70 61 6e 64 65 64 20 62 65 66   be expanded bef
36855 6f 72 65 0d 0a 2a 2a 20 6e 61 6d 65 20 72 65 73  ore..** name res
36856 6f 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 66 6f  olution is perfo
36857 72 6d 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  rmed...**..** If
36858 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
36859 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d  rong, an error m
3685a 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65  essage is writte
3685b 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 0d 0a  n into pParse...
3685c 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  ** The calling f
3685d 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74 65  unction can dete
3685e 63 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 62  ct the problem b
3685f 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50 61  y looking at pPa
36860 72 73 65 2d 3e 6e 45 72 72 0d 0a 2a 2a 20 61 6e  rse->nErr..** an
36861 64 2f 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d  d/or pParse->db-
36862 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0d 0a  >mallocFailed...
36863 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
36864 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
36865 61 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73  and(Parse *pPars
36866 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  e, Select *pSele
36867 63 74 29 7b 0d 0a 20 20 57 61 6c 6b 65 72 20 77  ct){..  Walker w
36868 3b 0d 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  ;..  w.xSelectCa
36869 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 45  llback = selectE
3686a 78 70 61 6e 64 65 72 3b 0d 0a 20 20 77 2e 78 45  xpander;..  w.xE
3686b 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78  xprCallback = ex
3686c 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0d 0a 20 20 77  prWalkNoop;..  w
3686d 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
3686e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  ;..  sqlite3Walk
3686f 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c 65  Select(&w, pSele
36870 63 74 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 23 69 66  ct);..}......#if
36871 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
36872 5f 53 55 42 51 55 45 52 59 0d 0a 2f 2a 0d 0a 2a  _SUBQUERY../*..*
36873 2a 20 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b  * This is a Walk
36874 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  er.xSelectCallba
36875 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ck callback for 
36876 74 68 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  the sqlite3Selec
36877 74 54 79 70 65 49 6e 66 6f 28 29 0d 0a 2a 2a 20  tTypeInfo()..** 
36878 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2a 0d 0a  interface...**..
36879 2a 2a 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d  ** For each FROM
3687a 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79  -clause subquery
3687b 2c 20 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79  , add Column.zTy
3687c 70 65 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43  pe and Column.zC
3687d 6f 6c 6c 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  oll..** informat
3687e 69 6f 6e 20 74 6f 20 74 68 65 20 54 61 62 6c 65  ion to the Table
3687f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
36880 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 72  represents the r
36881 65 73 75 6c 74 20 73 65 74 0d 0a 2a 2a 20 6f 66  esult set..** of
36882 20 74 68 61 74 20 73 75 62 71 75 65 72 79 2e 0d   that subquery..
36883 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 54 61 62 6c  .**..** The Tabl
36884 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
36885 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
36886 72 65 73 75 6c 74 20 73 65 74 20 77 61 73 20 63  result set was c
36887 6f 6e 73 74 72 75 63 74 65 64 0d 0a 2a 2a 20 62  onstructed..** b
36888 79 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72  y selectExpander
36889 28 29 20 62 75 74 20 74 68 65 20 74 79 70 65 20  () but the type 
3688a 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e  and collation in
3688b 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 6f 6d  formation was om
3688c 69 74 74 65 64 0d 0a 2a 2a 20 61 74 20 74 68 61  itted..** at tha
3688d 74 20 70 6f 69 6e 74 20 62 65 63 61 75 73 65 20  t point because 
3688e 69 64 65 6e 74 69 66 69 65 72 73 20 68 61 64 20  identifiers had 
3688f 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 73  not yet been res
36890 6f 6c 76 65 64 2e 20 20 54 68 69 73 0d 0a 2a 2a  olved.  This..**
36891 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
36892 65 64 20 61 66 74 65 72 20 69 64 65 6e 74 69 66  ed after identif
36893 69 65 72 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0d  ier resolution..
36894 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
36895 73 65 6c 65 63 74 41 64 64 53 75 62 71 75 65 72  selectAddSubquer
36896 79 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b 65 72  yTypeInfo(Walker
36897 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
36898 74 20 2a 70 29 7b 0d 0a 20 20 50 61 72 73 65 20  t *p){..  Parse 
36899 2a 70 50 61 72 73 65 3b 0d 0a 20 20 69 6e 74 20  *pParse;..  int 
3689a 69 3b 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  i;..  SrcList *p
3689b 54 61 62 4c 69 73 74 3b 0d 0a 20 20 73 74 72 75  TabList;..  stru
3689c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
3689d 2a 70 46 72 6f 6d 3b 0d 0a 0d 0a 20 20 61 73 73  *pFrom;....  ass
3689e 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73  ert( p->selFlags
3689f 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29   & SF_Resolved )
368a0 3b 0d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c  ;..  if( (p->sel
368a1 46 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79  Flags & SF_HasTy
368a2 70 65 49 6e 66 6f 29 3d 3d 30 20 29 7b 0d 0a 20  peInfo)==0 ){.. 
368a3 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c     p->selFlags |
368a4 3d 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f  = SF_HasTypeInfo
368a5 3b 0d 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20  ;..    pParse = 
368a6 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b  pWalker->pParse;
368a7 0d 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ..    pTabList =
368a8 20 70 2d 3e 70 53 72 63 3b 0d 0a 20 20 20 20 66   p->pSrc;..    f
368a9 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54  or(i=0, pFrom=pT
368aa 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61  abList->a; i<pTa
368ab 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
368ac 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0d 0a 20 20 20  , pFrom++){..   
368ad 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
368ae 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0d 0a 20   pFrom->pTab;.. 
368af 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
368b0 70 54 61 62 21 3d 30 29 20 26 26 20 28 70 54 61  pTab!=0) && (pTa
368b1 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
368b2 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 29  _Ephemeral)!=0 )
368b3 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  {..        /* A 
368b4 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65  sub-query in the
368b5 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
368b6 61 20 53 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 20  a SELECT */..   
368b7 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65       Select *pSe
368b8 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  l = pFrom->pSele
368b9 63 74 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73  ct;..        ass
368ba 65 72 74 28 20 70 53 65 6c 20 29 3b 0d 0a 20 20  ert( pSel );..  
368bb 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65        while( pSe
368bc 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c  l->pPrior ) pSel
368bd 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
368be 0d 0a 20 20 20 20 20 20 20 20 73 65 6c 65 63 74  ..        select
368bf 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64  AddColumnTypeAnd
368c0 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73 65  Collation(pParse
368c1 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54  , pTab->nCol, pT
368c2 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b  ab->aCol, pSel);
368c3 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
368c4 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
368c5 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0d 0a 7d  WRC_Continue;..}
368c6 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a  ..#endif....../*
368c7 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
368c8 65 20 61 64 64 73 20 64 61 74 61 74 79 70 65 20  e adds datatype 
368c9 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  and collating se
368ca 71 75 65 6e 63 65 20 69 6e 66 6f 72 6d 61 74 69  quence informati
368cb 6f 6e 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 54 61  on to..** the Ta
368cc 62 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 6f  ble structures o
368cd 66 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73  f all FROM-claus
368ce 65 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  e subqueries in 
368cf 61 0d 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61  a..** SELECT sta
368d0 74 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tement...**..** 
368d1 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  Use this routine
368d2 20 61 66 74 65 72 20 6e 61 6d 65 20 72 65 73 6f   after name reso
368d3 6c 75 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61  lution...*/..sta
368d4 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
368d5 53 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66  SelectAddTypeInf
368d6 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  o(Parse *pParse,
368d7 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
368d8 29 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){..#ifndef SQLI
368d9 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
368da 0d 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0d 0a 20  ..  Walker w;.. 
368db 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
368dc 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64 53 75  ck = selectAddSu
368dd 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f 3b 0d  bqueryTypeInfo;.
368de 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61  .  w.xExprCallba
368df 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f  ck = exprWalkNoo
368e0 70 3b 0d 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  p;..  w.pParse =
368e1 20 70 50 61 72 73 65 3b 0d 0a 20 20 73 71 6c 69   pParse;..  sqli
368e2 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77  te3WalkSelect(&w
368e3 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a 23 65 6e  , pSelect);..#en
368e4 64 69 66 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  dif..}....../*..
368e5 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
368e6 73 65 74 73 20 6f 66 20 61 20 53 45 4c 45 43 54  sets of a SELECT
368e7 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 70   statement for p
368e8 72 6f 63 65 73 73 69 6e 67 2e 20 20 54 68 65 0d  rocessing.  The.
368e9 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  .** following is
368ea 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 3a 0d 0a   accomplished:..
368eb 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 20 56 44  **..**     *  VD
368ec 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE Cursor number
368ed 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  s are assigned t
368ee 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73  o all FROM-claus
368ef 65 20 74 65 72 6d 73 2e 0d 0a 2a 2a 20 20 20 20  e terms...**    
368f0 20 2a 20 20 45 70 68 65 6d 65 72 61 6c 20 54 61   *  Ephemeral Ta
368f1 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20  ble objects are 
368f2 63 72 65 61 74 65 64 20 66 6f 72 20 61 6c 6c 20  created for all 
368f3 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
368f4 75 65 72 69 65 73 2e 0d 0a 2a 2a 20 20 20 20 20  ueries...**     
368f5 2a 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  *  ON and USING 
368f6 63 6c 61 75 73 65 73 20 61 72 65 20 73 68 69 66  clauses are shif
368f7 74 65 64 20 69 6e 74 6f 20 57 48 45 52 45 20 73  ted into WHERE s
368f8 74 61 74 65 6d 65 6e 74 73 0d 0a 2a 2a 20 20 20  tatements..**   
368f9 20 20 2a 20 20 57 69 6c 64 63 61 72 64 73 20 22    *  Wildcards "
368fa 2a 22 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22  *" and "TABLE.*"
368fb 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 73 20   in result sets 
368fc 61 72 65 20 65 78 70 61 6e 64 65 64 2e 0d 0a 2a  are expanded...*
368fd 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74 69 66  *     *  Identif
368fe 69 65 72 73 20 69 6e 20 65 78 70 72 65 73 73 69  iers in expressi
368ff 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64 20 74  on are matched t
36900 6f 20 74 61 62 6c 65 73 2e 0d 0a 2a 2a 0d 0a 2a  o tables...**..*
36901 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
36902 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
36903 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
36904 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
36905 45 43 54 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ECT...*/..SQLITE
36906 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
36907 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
36908 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
36909 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
3690a 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
3690b 20 2a 2f 0d 0a 20 20 53 65 6c 65 63 74 20 2a 70   */..  Select *p
3690c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
3690d 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
3690e 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
3690f 64 2e 20 2a 2f 0d 0a 20 20 4e 61 6d 65 43 6f 6e  d. */..  NameCon
36910 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20  text *pOuterNC  
36911 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
36912 66 6f 72 20 63 6f 6e 74 61 69 6e 65 72 20 2a 2f  for container */
36913 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20  ..){..  sqlite3 
36914 2a 64 62 3b 0d 0a 20 20 69 66 28 20 4e 45 56 45  *db;..  if( NEVE
36915 52 28 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(p==0) ) return
36916 3b 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ;..  db = pParse
36917 2d 3e 64 62 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  ->db;..  if( p->
36918 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61  selFlags & SF_Ha
36919 73 54 79 70 65 49 6e 66 6f 20 29 20 72 65 74 75  sTypeInfo ) retu
3691a 72 6e 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65  rn;..  sqlite3Se
3691b 6c 65 63 74 45 78 70 61 6e 64 28 70 50 61 72 73  lectExpand(pPars
3691c 65 2c 20 70 29 3b 0d 0a 20 20 69 66 28 20 70 50  e, p);..  if( pP
3691d 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
3691e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
3691f 20 72 65 74 75 72 6e 3b 0d 0a 20 20 73 71 6c 69   return;..  sqli
36920 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74  te3ResolveSelect
36921 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 2c  Names(pParse, p,
36922 20 70 4f 75 74 65 72 4e 43 29 3b 0d 0a 20 20 69   pOuterNC);..  i
36923 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
36924 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
36925 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  led ) return;.. 
36926 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41 64   sqlite3SelectAd
36927 64 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73 65  dTypeInfo(pParse
36928 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  , p);..}..../*..
36929 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 67 67  ** Reset the agg
3692a 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74  regate accumulat
3692b 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  or...**..** The 
3692c 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
3692d 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20 6f  lator is a set o
3692e 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74  f memory cells t
3692f 68 61 74 20 68 6f 6c 64 0d 0a 2a 2a 20 69 6e 74  hat hold..** int
36930 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74  ermediate result
36931 73 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74  s while calculat
36932 69 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65  ing an aggregate
36933 2e 20 20 54 68 69 73 0d 0a 2a 2a 20 72 6f 75 74  .  This..** rout
36934 69 6e 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65  ine simply store
36935 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f  s NULLs in all o
36936 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63  f those memory c
36937 65 6c 6c 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ells...*/..stati
36938 63 20 76 6f 69 64 20 72 65 73 65 74 41 63 63 75  c void resetAccu
36939 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70  mulator(Parse *p
3693a 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a  Parse, AggInfo *
3693b 70 41 67 67 49 6e 66 6f 29 7b 0d 0a 20 20 56 64  pAggInfo){..  Vd
3693c 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
3693d 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69 3b  pVdbe;..  int i;
3693e 0d 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  ..  struct AggIn
3693f 66 6f 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0d  fo_func *pFunc;.
36940 0a 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d  .  if( pAggInfo-
36941 3e 6e 46 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d  >nFunc+pAggInfo-
36942 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0d 0a  >nColumn==0 ){..
36943 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
36944 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
36945 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
36946 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c  ; i++){..    sql
36947 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
36948 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 41  , OP_Null, 0, pA
36949 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e  ggInfo->aCol[i].
3694a 69 4d 65 6d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 66  iMem);..  }..  f
3694b 6f 72 28 70 46 75 6e 63 3d 70 41 67 67 49 6e 66  or(pFunc=pAggInf
3694c 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69  o->aFunc, i=0; i
3694d 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
3694e 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b  ; i++, pFunc++){
3694f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
36950 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
36951 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e 69 4d  ll, 0, pFunc->iM
36952 65 6d 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 46  em);..    if( pF
36953 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d  unc->iDistinct>=
36954 30 20 29 7b 0d 0a 20 20 20 20 20 20 45 78 70 72  0 ){..      Expr
36955 20 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45   *pE = pFunc->pE
36956 78 70 72 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  xpr;..      asse
36957 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
36958 65 72 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53  erty(pE, EP_xIsS
36959 65 6c 65 63 74 29 20 29 3b 0d 0a 20 20 20 20 20  elect) );..     
3695a 20 69 66 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74   if( pE->x.pList
3695b 3d 3d 30 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69  ==0 || pE->x.pLi
3695c 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0d  st->nExpr!=1 ){.
3695d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3695e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
3695f 20 22 44 49 53 54 49 4e 43 54 20 61 67 67 72 65   "DISTINCT aggre
36960 67 61 74 65 73 20 6d 75 73 74 20 68 61 76 65 20  gates must have 
36961 65 78 61 63 74 6c 79 20 6f 6e 65 20 22 0d 0a 20  exactly one ".. 
36962 20 20 20 20 20 20 20 20 20 20 22 61 72 67 75 6d            "argum
36963 65 6e 74 22 29 3b 0d 0a 20 20 20 20 20 20 20 20  ent");..        
36964 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74  pFunc->iDistinct
36965 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 20 20 7d 65   = -1;..      }e
36966 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 4b 65  lse{..        Ke
36967 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20  yInfo *pKeyInfo 
36968 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
36969 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45  rList(pParse, pE
3696a 2d 3e 78 2e 70 4c 69 73 74 29 3b 0d 0a 20 20 20  ->x.pList);..   
3696b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3696c 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
3696d 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46 75 6e  nEphemeral, pFun
3696e 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20 30 2c  c->iDistinct, 0,
3696f 20 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20   0,..           
36970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
36971 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  char*)pKeyInfo, 
36972 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
36973 46 46 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  FF);..      }.. 
36974 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a     }..  }..}....
36975 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  /*..** Invoke th
36976 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65  e OP_AggFinalize
36977 20 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72   opcode for ever
36978 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  y aggregate func
36979 74 69 6f 6e 0d 0a 2a 2a 20 69 6e 20 74 68 65 20  tion..** in the 
3697a 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72  AggInfo structur
3697b 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  e...*/..static v
3697c 6f 69 64 20 66 69 6e 61 6c 69 7a 65 41 67 67 46  oid finalizeAggF
3697d 75 6e 63 74 69 6f 6e 73 28 50 61 72 73 65 20 2a  unctions(Parse *
3697e 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20  pParse, AggInfo 
3697f 2a 70 41 67 67 49 6e 66 6f 29 7b 0d 0a 20 20 56  *pAggInfo){..  V
36980 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
36981 3e 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69  >pVdbe;..  int i
36982 3b 0d 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  ;..  struct AggI
36983 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0d 0a 20  nfo_func *pF;.. 
36984 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67   for(i=0, pF=pAg
36985 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c  gInfo->aFunc; i<
36986 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b  pAggInfo->nFunc;
36987 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0d 0a 20 20   i++, pF++){..  
36988 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
36989 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78  t = pF->pExpr->x
3698a 2e 70 4c 69 73 74 3b 0d 0a 20 20 20 20 61 73 73  .pList;..    ass
3698b 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
3698c 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72 2c  perty(pF->pExpr,
3698d 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
3698e 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
3698f 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41  beAddOp4(v, OP_A
36990 67 67 46 69 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65  ggFinal, pF->iMe
36991 6d 2c 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74  m, pList ? pList
36992 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 30 2c 0d  ->nExpr : 0, 0,.
36993 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36994 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46         (void*)pF
36995 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43  ->pFunc, P4_FUNC
36996 44 45 46 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  DEF);..  }..}...
36997 0a 2f 2a 0d 0a 2a 2a 20 55 70 64 61 74 65 20 74  ./*..** Update t
36998 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d  he accumulator m
36999 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20  emory cells for 
3699a 61 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73  an aggregate bas
3699b 65 64 20 6f 6e 0d 0a 2a 2a 20 74 68 65 20 63 75  ed on..** the cu
3699c 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73  rrent cursor pos
3699d 69 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ition...*/..stat
3699e 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 41 63  ic void updateAc
3699f 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20  cumulator(Parse 
369a0 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f  *pParse, AggInfo
369a1 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0d 0a 20 20   *pAggInfo){..  
369a2 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
369a3 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20  ->pVdbe;..  int 
369a4 69 3b 0d 0a 20 20 73 74 72 75 63 74 20 41 67 67  i;..  struct Agg
369a5 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0d 0a  Info_func *pF;..
369a6 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
369a7 5f 63 6f 6c 20 2a 70 43 3b 0d 0a 0d 0a 20 20 70  _col *pC;....  p
369a8 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d  AggInfo->directM
369a9 6f 64 65 20 3d 20 31 3b 0d 0a 20 20 73 71 6c 69  ode = 1;..  sqli
369aa 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
369ab 72 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 66 6f  r(pParse);..  fo
369ac 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e  r(i=0, pF=pAggIn
369ad 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67  fo->aFunc; i<pAg
369ae 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b  gInfo->nFunc; i+
369af 2b 2c 20 70 46 2b 2b 29 7b 0d 0a 20 20 20 20 69  +, pF++){..    i
369b0 6e 74 20 6e 41 72 67 3b 0d 0a 20 20 20 20 69 6e  nt nArg;..    in
369b1 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b 0d  t addrNext = 0;.
369b2 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67 67 3b  .    int regAgg;
369b3 0d 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  ..    ExprList *
369b4 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70  pList = pF->pExp
369b5 72 2d 3e 78 2e 70 4c 69 73 74 3b 0d 0a 20 20 20  r->x.pList;..   
369b6 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
369b7 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45  sProperty(pF->pE
369b8 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
369b9 74 29 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  t) );..    if( p
369ba 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 6e  List ){..      n
369bb 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  Arg = pList->nEx
369bc 70 72 3b 0d 0a 20 20 20 20 20 20 72 65 67 41 67  pr;..      regAg
369bd 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
369be 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
369bf 6e 41 72 67 29 3b 0d 0a 20 20 20 20 20 20 73 71  nArg);..      sq
369c0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
369c1 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c  rList(pParse, pL
369c2 69 73 74 2c 20 72 65 67 41 67 67 2c 20 31 29 3b  ist, regAgg, 1);
369c3 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
369c4 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0d 0a 20      nArg = 0;.. 
369c5 20 20 20 20 20 72 65 67 41 67 67 20 3d 20 30 3b       regAgg = 0;
369c6 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
369c7 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d   pF->iDistinct>=
369c8 30 20 29 7b 0d 0a 20 20 20 20 20 20 61 64 64 72  0 ){..      addr
369c9 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Next = sqlite3Vd
369ca 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d  beMakeLabel(v);.
369cb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
369cc 41 72 67 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 20  Arg==1 );..     
369cd 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50   codeDistinct(pP
369ce 61 72 73 65 2c 20 70 46 2d 3e 69 44 69 73 74 69  arse, pF->iDisti
369cf 6e 63 74 2c 20 61 64 64 72 4e 65 78 74 2c 20 31  nct, addrNext, 1
369d0 2c 20 72 65 67 41 67 67 29 3b 0d 0a 20 20 20 20  , regAgg);..    
369d1 7d 0d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 70  }..    if( pF->p
369d2 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51  Func->flags & SQ
369d3 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
369d4 4c 4c 20 29 7b 0d 0a 20 20 20 20 20 20 43 6f 6c  LL ){..      Col
369d5 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b  lSeq *pColl = 0;
369d6 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ..      struct E
369d7 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
369d8 74 65 6d 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20  tem;..      int 
369d9 6a 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  j;..      assert
369da 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f  ( pList!=0 );  /
369db 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20 70 46  * pList!=0 if pF
369dc 2d 3e 70 46 75 6e 63 20 68 61 73 20 4e 45 45 44  ->pFunc has NEED
369dd 43 4f 4c 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 66  COLL */..      f
369de 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(j=0, pItem=pL
369df 69 73 74 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26  ist->a; !pColl &
369e0 26 20 6a 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70  & j<nArg; j++, p
369e1 49 74 65 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  Item++){..      
369e2 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
369e3 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
369e4 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  rse, pItem->pExp
369e5 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  r);..      }..  
369e6 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
369e7 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  {..        pColl
369e8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70   = pParse->db->p
369e9 44 66 6c 74 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20  DfltColl;..     
369ea 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65   }..      sqlite
369eb 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
369ec 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c  P_CollSeq, 0, 0,
369ed 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c   0, (char *)pCol
369ee 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0d  l, P4_COLLSEQ);.
369ef 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
369f0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
369f1 20 4f 50 5f 41 67 67 53 74 65 70 2c 20 30 2c 20   OP_AggStep, 0, 
369f2 72 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d  regAgg, pF->iMem
369f3 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
369f4 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29           (void*)
369f5 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55  pF->pFunc, P4_FU
369f6 4e 43 44 45 46 29 3b 0d 0a 20 20 20 20 73 71 6c  NCDEF);..    sql
369f7 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
369f8 28 76 2c 20 28 75 38 29 6e 41 72 67 29 3b 0d 0a  (v, (u8)nArg);..
369f9 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
369fa 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
369fb 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67  ge(pParse, regAg
369fc 67 2c 20 6e 41 72 67 29 3b 0d 0a 20 20 20 20 73  g, nArg);..    s
369fd 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
369fe 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
369ff 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0d 0a 20  egAgg, nArg);.. 
36a00 20 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20     if( addrNext 
36a01 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
36a02 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
36a03 6c 28 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0d  l(v, addrNext);.
36a04 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
36a05 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
36a06 72 73 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  rse);..    }..  
36a07 7d 0d 0a 0d 0a 20 20 2f 2a 20 42 65 66 6f 72 65  }....  /* Before
36a08 20 70 6f 70 75 6c 61 74 69 6e 67 20 74 68 65 20   populating the 
36a09 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67 69  accumulator regi
36a0a 73 74 65 72 73 2c 20 63 6c 65 61 72 20 74 68 65  sters, clear the
36a0b 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0d 0a   column cache...
36a0c 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    ** Otherwise, 
36a0d 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 72 65  if any of the re
36a0e 71 75 69 72 65 64 20 63 6f 6c 75 6d 6e 20 76 61  quired column va
36a0f 6c 75 65 73 20 61 72 65 20 61 6c 72 65 61 64 79  lues are already
36a10 20 70 72 65 73 65 6e 74 20 0d 0a 20 20 2a 2a 20   present ..  ** 
36a11 69 6e 20 72 65 67 69 73 74 65 72 73 2c 20 73 71  in registers, sq
36a12 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 29 20  lite3ExprCode() 
36a13 6d 61 79 20 75 73 65 20 4f 50 5f 53 43 6f 70 79  may use OP_SCopy
36a14 20 74 6f 20 63 6f 70 79 20 74 68 65 20 76 61 6c   to copy the val
36a15 75 65 0d 0a 20 20 2a 2a 20 74 6f 20 70 43 2d 3e  ue..  ** to pC->
36a16 69 4d 65 6d 2e 20 42 75 74 20 62 79 20 74 68 65  iMem. But by the
36a17 20 74 69 6d 65 20 74 68 65 20 76 61 6c 75 65 20   time the value 
36a18 69 73 20 75 73 65 64 2c 20 74 68 65 20 6f 72 69  is used, the ori
36a19 67 69 6e 61 6c 20 72 65 67 69 73 74 65 72 0d 0a  ginal register..
36a1a 20 20 2a 2a 20 6d 61 79 20 68 61 76 65 20 62 65    ** may have be
36a1b 65 6e 20 75 73 65 64 2c 20 69 6e 76 61 6c 69 64  en used, invalid
36a1c 61 74 69 6e 67 20 74 68 65 20 75 6e 64 65 72 6c  ating the underl
36a1d 79 69 6e 67 20 62 75 66 66 65 72 20 68 6f 6c 64  ying buffer hold
36a1e 69 6e 67 20 74 68 65 0d 0a 20 20 2a 2a 20 74 65  ing the..  ** te
36a1f 78 74 20 6f 72 20 62 6c 6f 62 20 76 61 6c 75 65  xt or blob value
36a20 2e 20 53 65 65 20 74 69 63 6b 65 74 20 5b 38 38  . See ticket [88
36a21 33 30 33 34 64 63 62 35 5d 2e 0d 0a 20 20 2a 2a  3034dcb5]...  **
36a22 0d 0a 20 20 2a 2a 20 41 6e 6f 74 68 65 72 20 73  ..  ** Another s
36a23 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65  olution would be
36a24 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 4f   to change the O
36a25 50 5f 53 43 6f 70 79 20 75 73 65 64 20 74 6f 20  P_SCopy used to 
36a26 63 6f 70 79 20 63 61 63 68 65 64 0d 0a 20 20 2a  copy cached..  *
36a27 2a 20 76 61 6c 75 65 73 20 74 6f 20 61 6e 20 4f  * values to an O
36a28 50 5f 43 6f 70 79 2e 0d 0a 20 20 2a 2f 0d 0a 20  P_Copy...  */.. 
36a29 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
36a2a 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0d  eClear(pParse);.
36a2b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70  .  for(i=0, pC=p
36a2c 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69  AggInfo->aCol; i
36a2d 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75  <pAggInfo->nAccu
36a2e 6d 75 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43  mulator; i++, pC
36a2f 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ++){..    sqlite
36a30 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
36a31 2c 20 70 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d  , pC->pExpr, pC-
36a32 3e 69 4d 65 6d 29 3b 0d 0a 20 20 7d 0d 0a 20 20  >iMem);..  }..  
36a33 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74  pAggInfo->direct
36a34 4d 6f 64 65 20 3d 20 30 3b 0d 0a 20 20 73 71 6c  Mode = 0;..  sql
36a35 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
36a36 61 72 28 70 50 61 72 73 65 29 3b 0d 0a 7d 0d 0a  ar(pParse);..}..
36a37 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61 20 73  ../*..** Add a s
36a38 69 6e 67 6c 65 20 4f 50 5f 45 78 70 6c 61 69 6e  ingle OP_Explain
36a39 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
36a3a 74 68 65 20 56 44 42 45 20 74 6f 20 65 78 70 6c  the VDBE to expl
36a3b 61 69 6e 20 61 20 73 69 6d 70 6c 65 0d 0a 2a 2a  ain a simple..**
36a3c 20 63 6f 75 6e 74 28 2a 29 20 71 75 65 72 79 20   count(*) query 
36a3d 28 22 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a  ("SELECT count(*
36a3e 29 20 46 52 4f 4d 20 70 54 61 62 22 29 2e 0d 0a  ) FROM pTab")...
36a3f 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
36a40 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0d  TE_OMIT_EXPLAIN.
36a41 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
36a42 6c 61 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74 28  lainSimpleCount(
36a43 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
36a44 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
36a45 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
36a46 74 65 78 74 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  text */..  Table
36a47 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
36a48 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
36a49 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69 65  ble being querie
36a4a 64 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a 70  d */..  Index *p
36a4b 49 64 78 20 20 20 20 20 20 20 20 20 20 20 20 20  Idx             
36a4c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
36a4d 20 75 73 65 64 20 74 6f 20 6f 70 74 69 6d 69 7a   used to optimiz
36a4e 65 20 73 63 61 6e 2c 20 6f 72 20 4e 55 4c 4c 20  e scan, or NULL 
36a4f 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 70 50  */..){..  if( pP
36a50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  arse->explain==2
36a51 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a   ){..    char *z
36a52 45 71 70 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Eqp = sqlite3MPr
36a53 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c  intf(pParse->db,
36a54 20 22 53 43 41 4e 20 54 41 42 4c 45 20 25 73 20   "SCAN TABLE %s 
36a55 25 73 25 73 28 7e 25 64 20 72 6f 77 73 29 22 2c  %s%s(~%d rows)",
36a56 0d 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e  ..        pTab->
36a57 7a 4e 61 6d 65 2c 20 0d 0a 20 20 20 20 20 20 20  zName, ..       
36a58 20 70 49 64 78 20 3f 20 22 55 53 49 4e 47 20 43   pIdx ? "USING C
36a59 4f 56 45 52 49 4e 47 20 49 4e 44 45 58 20 22 20  OVERING INDEX " 
36a5a 3a 20 22 22 2c 0d 0a 20 20 20 20 20 20 20 20 70  : "",..        p
36a5b 49 64 78 20 3f 20 70 49 64 78 2d 3e 7a 4e 61 6d  Idx ? pIdx->zNam
36a5c 65 20 3a 20 22 22 2c 0d 0a 20 20 20 20 20 20 20  e : "",..       
36a5d 20 70 54 61 62 2d 3e 6e 52 6f 77 45 73 74 0d 0a   pTab->nRowEst..
36a5e 20 20 20 20 29 3b 0d 0a 20 20 20 20 73 71 6c 69      );..    sqli
36a5f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0d 0a  te3VdbeAddOp4(..
36a60 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
36a61 70 56 64 62 65 2c 20 4f 50 5f 45 78 70 6c 61 69  pVdbe, OP_Explai
36a62 6e 2c 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65  n, pParse->iSele
36a63 63 74 49 64 2c 20 30 2c 20 30 2c 20 7a 45 71 70  ctId, 0, 0, zEqp
36a64 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0d 0a 20 20  , P4_DYNAMIC..  
36a65 20 20 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65    );..  }..}..#e
36a66 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 65 78  lse..# define ex
36a67 70 6c 61 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74  plainSimpleCount
36a68 28 61 2c 62 2c 63 29 0d 0a 23 65 6e 64 69 66 0d  (a,b,c)..#endif.
36a69 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
36a6a 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  te code for the 
36a6b 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
36a6c 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 70 20   given in the p 
36a6d 61 72 67 75 6d 65 6e 74 2e 20 20 0d 0a 2a 2a 0d  argument.  ..**.
36a6e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
36a6f 61 72 65 20 64 69 73 74 72 69 62 75 74 65 64 20  are distributed 
36a70 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79 73 20  in various ways 
36a71 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
36a72 0d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  ..** contents of
36a73 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74 20   the SelectDest 
36a74 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65  structure pointe
36a75 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74  d to by argument
36a76 20 70 44 65 73 74 0d 0a 2a 2a 20 61 73 20 66 6f   pDest..** as fo
36a77 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  llows:..**..**  
36a78 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20     pDest->eDest 
36a79 20 20 20 52 65 73 75 6c 74 0d 0a 2a 2a 20 20 20     Result..**   
36a7a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20    ------------  
36a7b 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
36a7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
36a7d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a  -------------..*
36a7e 2a 20 20 20 20 20 53 52 54 5f 4f 75 74 70 75 74  *     SRT_Output
36a7f 20 20 20 20 20 20 47 65 6e 65 72 61 74 65 20 61        Generate a
36a80 20 72 6f 77 20 6f 66 20 6f 75 74 70 75 74 20 28   row of output (
36a81 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 52 65 73  using the OP_Res
36a82 75 6c 74 52 6f 77 0d 0a 2a 2a 20 20 20 20 20 20  ultRow..**      
36a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
36a84 70 63 6f 64 65 29 20 66 6f 72 20 65 61 63 68 20  pcode) for each 
36a85 72 6f 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c  row in the resul
36a86 74 20 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  t set...**..**  
36a87 20 20 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 20     SRT_Mem      
36a88 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66     Only valid if
36a89 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
36a8a 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 0d   single column..
36a8b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
36a8c 20 20 20 20 20 20 20 20 53 74 6f 72 65 20 74 68          Store th
36a8d 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  e first column o
36a8e 66 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  f the first resu
36a8f 6c 74 20 72 6f 77 0d 0a 2a 2a 20 20 20 20 20 20  lt row..**      
36a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
36a91 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73 74  n register pDest
36a92 2d 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62 61  ->iParm then aba
36a93 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0d 0a 2a  ndon the rest..*
36a94 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
36a95 20 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65        of the que
36a96 72 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e  ry.  This destin
36a97 61 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c  ation implies "L
36a98 49 4d 49 54 20 31 22 2e 0d 0a 2a 2a 0d 0a 2a 2a  IMIT 1"...**..**
36a99 20 20 20 20 20 53 52 54 5f 53 65 74 20 20 20 20       SRT_Set    
36a9a 20 20 20 20 20 54 68 65 20 72 65 73 75 6c 74 20       The result 
36a9b 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65  must be a single
36a9c 20 63 6f 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20   column.  Store 
36a9d 65 61 63 68 0d 0a 2a 2a 20 20 20 20 20 20 20 20  each..**        
36a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 72 6f 77               row
36a9f 20 6f 66 20 72 65 73 75 6c 74 20 61 73 20 74 68   of result as th
36aa0 65 20 6b 65 79 20 69 6e 20 74 61 62 6c 65 20 70  e key in table p
36aa1 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20 0d 0a 2a  Dest->iParm. ..*
36aa2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
36aa3 20 20 20 20 20 20 41 70 70 6c 79 20 74 68 65 20        Apply the 
36aa4 61 66 66 69 6e 69 74 79 20 70 44 65 73 74 2d 3e  affinity pDest->
36aa5 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20  affinity before 
36aa6 73 74 6f 72 69 6e 67 0d 0a 2a 2a 20 20 20 20 20  storing..**     
36aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36aa8 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20 74  results.  Used t
36aa9 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e 20  o implement "IN 
36aaa 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0d 0a  (SELECT ...)"...
36aab 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55  **..**     SRT_U
36aac 6e 69 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65  nion       Store
36aad 20 72 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65   results as a ke
36aae 79 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  y in a temporary
36aaf 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50   table pDest->iP
36ab0 61 72 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  arm...**..**    
36ab1 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20   SRT_Except     
36ab2 20 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 73 20   Remove results 
36ab3 66 72 6f 6d 20 74 68 65 20 74 65 6d 70 6f 72 61  from the tempora
36ab4 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e  ry table pDest->
36ab5 69 50 61 72 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  iParm...**..**  
36ab6 20 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20     SRT_Table    
36ab7 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73     Store results
36ab8 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61   in temporary ta
36ab9 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ble pDest->iParm
36aba 2e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ...**           
36abb 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20 69            This i
36abc 73 20 6c 69 6b 65 20 53 52 54 5f 45 70 68 65 6d  s like SRT_Ephem
36abd 54 61 62 20 65 78 63 65 70 74 20 74 68 61 74 20  Tab except that 
36abe 74 68 65 20 74 61 62 6c 65 0d 0a 2a 2a 20 20 20  the table..**   
36abf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ac0 20 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20    is assumed to 
36ac1 61 6c 72 65 61 64 79 20 62 65 20 6f 70 65 6e 2e  already be open.
36ac2 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53 52 54  ..**..**     SRT
36ac3 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43 72 65  _EphemTab    Cre
36ac4 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72 79  ate an temporary
36ac5 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50   table pDest->iP
36ac6 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0d 0a 2a  arm and store..*
36ac7 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
36ac8 20 20 20 20 20 20 74 68 65 20 72 65 73 75 6c 74        the result
36ac9 20 74 68 65 72 65 2e 20 54 68 65 20 63 75 72 73   there. The curs
36aca 6f 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20  or is left open 
36acb 61 66 74 65 72 0d 0a 2a 2a 20 20 20 20 20 20 20  after..**       
36acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
36acd 74 75 72 6e 69 6e 67 2e 20 20 54 68 69 73 20 69  turning.  This i
36ace 73 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65  s like SRT_Table
36acf 20 65 78 63 65 70 74 20 74 68 61 74 0d 0a 2a 2a   except that..**
36ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ad1 20 20 20 20 20 74 68 69 73 20 64 65 73 74 69 6e       this destin
36ad2 61 74 69 6f 6e 20 75 73 65 73 20 4f 50 5f 4f 70  ation uses OP_Op
36ad3 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 6f 20 63  enEphemeral to c
36ad4 72 65 61 74 65 0d 0a 2a 2a 20 20 20 20 20 20 20  reate..**       
36ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
36ad6 65 20 74 61 62 6c 65 20 66 69 72 73 74 2e 0d 0a  e table first...
36ad7 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 43  **..**     SRT_C
36ad8 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65 72  oroutine   Gener
36ad9 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65  ate a co-routine
36ada 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61 20   that returns a 
36adb 6e 65 77 20 72 6f 77 20 6f 66 0d 0a 2a 2a 20 20  new row of..**  
36adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36add 20 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20     results each 
36ade 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b  time it is invok
36adf 65 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70  ed.  The entry p
36ae0 6f 69 6e 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20  oint..**        
36ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
36ae2 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69  the co-routine i
36ae3 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  s stored in regi
36ae4 73 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72  ster pDest->iPar
36ae5 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53  m...**..**     S
36ae6 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 53  RT_Exists      S
36ae7 74 6f 72 65 20 61 20 31 20 69 6e 20 6d 65 6d 6f  tore a 1 in memo
36ae8 72 79 20 63 65 6c 6c 20 70 44 65 73 74 2d 3e 69  ry cell pDest->i
36ae9 50 61 72 6d 20 69 66 20 74 68 65 20 72 65 73 75  Parm if the resu
36aea 6c 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  lt..**          
36aeb 20 20 20 20 20 20 20 20 20 20 20 73 65 74 20 69             set i
36aec 73 20 6e 6f 74 20 65 6d 70 74 79 2e 0d 0a 2a 2a  s not empty...**
36aed 0d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 44 69 73  ..**     SRT_Dis
36aee 63 61 72 64 20 20 20 20 20 54 68 72 6f 77 20 74  card     Throw t
36aef 68 65 20 72 65 73 75 6c 74 73 20 61 77 61 79 2e  he results away.
36af0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62    This is used b
36af1 79 20 53 45 4c 45 43 54 0d 0a 2a 2a 20 20 20 20  y SELECT..**    
36af2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36af3 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68   statements with
36af4 69 6e 20 74 72 69 67 67 65 72 73 20 77 68 6f 73  in triggers whos
36af5 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73 65 20 69  e only purpose i
36af6 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  s..**           
36af7 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 69            the si
36af8 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20 66 75  de-effects of fu
36af9 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  nctions...**..**
36afa 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
36afb 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
36afc 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
36afd 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0d 0a  any errors are..
36afe 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  ** encountered, 
36aff 74 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69  then an appropri
36b00 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ate error messag
36b01 65 20 69 73 20 6c 65 66 74 20 69 6e 0d 0a 2a 2a  e is left in..**
36b02 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
36b03 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
36b04 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54 20  outine does NOT 
36b05 66 72 65 65 20 74 68 65 20 53 65 6c 65 63 74 20  free the Select 
36b06 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
36b07 20 69 6e 2e 20 20 54 68 65 0d 0a 2a 2a 20 63 61   in.  The..** ca
36b08 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e  lling function n
36b09 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74 2e  eeds to do that.
36b0a 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
36b0b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
36b0c 53 65 6c 65 63 74 28 0d 0a 20 20 50 61 72 73 65  Select(..  Parse
36b0d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
36b0e 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
36b0f 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 53 65  context */..  Se
36b10 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
36b11 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
36b12 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
36b13 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0d 0a 20 20  ng coded. */..  
36b14 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
36b15 74 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  t      /* What t
36b16 6f 20 64 6f 20 77 69 74 68 20 74 68 65 20 71 75  o do with the qu
36b17 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0d 0a  ery results */..
36b18 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20  ){..  int i, j; 
36b19 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36b1a 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
36b1b 0d 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  ..  WhereInfo *p
36b1c 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 52 65  WInfo;     /* Re
36b1d 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65  turn from sqlite
36b1e 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f  3WhereBegin() */
36b1f 0d 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  ..  Vdbe *v;    
36b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
36b21 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
36b22 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
36b23 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  tion */..  int i
36b24 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20  sAgg;           
36b25 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73 65    /* True for se
36b26 6c 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65 20  lect lists like 
36b27 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0d 0a 20  "count(*)" */.. 
36b28 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
36b29 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  t;      /* List 
36b2a 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78  of columns to ex
36b2b 74 72 61 63 74 2e 20 2a 2f 0d 0a 20 20 53 72 63  tract. */..  Src
36b2c 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20  List *pTabList; 
36b2d 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
36b2e 61 62 6c 65 73 20 74 6f 20 73 65 6c 65 63 74 20  ables to select 
36b2f 66 72 6f 6d 20 2a 2f 0d 0a 20 20 45 78 70 72 20  from */..  Expr 
36b30 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
36b31 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
36b32 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e  lause.  May be N
36b33 55 4c 4c 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69  ULL */..  ExprLi
36b34 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20  st *pOrderBy;   
36b35 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
36b36 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
36b37 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 45 78 70 72   NULL */..  Expr
36b38 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
36b39 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20     /* The GROUP 
36b3a 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  BY clause.  May 
36b3b 62 65 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 45 78  be NULL */..  Ex
36b3c 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20  pr *pHaving;    
36b3d 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49       /* The HAVI
36b3e 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  NG clause.  May 
36b3f 62 65 20 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 69 6e  be NULL */..  in
36b40 74 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20  t isDistinct;   
36b41 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
36b42 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
36b43 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
36b44 2a 2f 0d 0a 20 20 69 6e 74 20 64 69 73 74 69 6e  */..  int distin
36b45 63 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ct;          /* 
36b46 54 61 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72  Table to use for
36b47 20 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65   the distinct se
36b48 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20 3d  t */..  int rc =
36b49 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   1;            /
36b4a 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
36b4b 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63  n from this func
36b4c 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 61  tion */..  int a
36b4d 64 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20 20  ddrSortIndex;   
36b4e 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
36b4f 61 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  an OP_OpenEpheme
36b50 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ral instruction 
36b51 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64 72 44 69  */..  int addrDi
36b52 73 74 69 6e 63 74 49 6e 64 65 78 3b 20 2f 2a 20  stinctIndex; /* 
36b53 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20 4f 50  Address of an OP
36b54 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69  _OpenEphemeral i
36b55 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0d 0a 20  nstruction */.. 
36b56 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e 66   AggInfo sAggInf
36b57 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  o;      /* Infor
36b58 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 61  mation used by a
36b59 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
36b5a 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 45 6e 64 3b   */..  int iEnd;
36b5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36b5c 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
36b5d 65 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79  end of the query
36b5e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a   */..  sqlite3 *
36b5f 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  db;           /*
36b60 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
36b61 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 23  nnection */....#
36b62 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
36b63 49 54 5f 45 58 50 4c 41 49 4e 0d 0a 20 20 69 6e  IT_EXPLAIN..  in
36b64 74 20 69 52 65 73 74 6f 72 65 53 65 6c 65 63 74  t iRestoreSelect
36b65 49 64 20 3d 20 70 50 61 72 73 65 2d 3e 69 53 65  Id = pParse->iSe
36b66 6c 65 63 74 49 64 3b 0d 0a 20 20 70 50 61 72 73  lectId;..  pPars
36b67 65 2d 3e 69 53 65 6c 65 63 74 49 64 20 3d 20 70  e->iSelectId = p
36b68 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65  Parse->iNextSele
36b69 63 74 49 64 2b 2b 3b 0d 0a 23 65 6e 64 69 66 0d  ctId++;..#endif.
36b6a 0a 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ...  db = pParse
36b6b 2d 3e 64 62 3b 0d 0a 20 20 69 66 28 20 70 3d 3d  ->db;..  if( p==
36b6c 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
36b6d 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d  ailed || pParse-
36b6e 3e 6e 45 72 72 20 29 7b 0d 0a 20 20 20 20 72 65  >nErr ){..    re
36b6f 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 1;..  }..  
36b70 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
36b71 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
36b72 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30  ITE_SELECT, 0, 0
36b73 2c 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b  , 0) ) return 1;
36b74 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67  ..  memset(&sAgg
36b75 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
36b76 73 41 67 67 49 6e 66 6f 29 29 3b 0d 0a 0d 0a 20  sAggInfo));.... 
36b77 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72   if( IgnorableOr
36b78 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0d  derby(pDest) ){.
36b79 0a 20 20 20 20 61 73 73 65 72 74 28 70 44 65 73  .    assert(pDes
36b7a 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78  t->eDest==SRT_Ex
36b7b 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e 65  ists || pDest->e
36b7c 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e 20  Dest==SRT_Union 
36b7d 7c 7c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  || ..           
36b7e 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
36b7f 54 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65 73  T_Except || pDes
36b80 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69  t->eDest==SRT_Di
36b81 73 63 61 72 64 29 3b 0d 0a 20 20 20 20 2f 2a 20  scard);..    /* 
36b82 49 66 20 4f 52 44 45 52 20 42 59 20 6d 61 6b 65  If ORDER BY make
36b83 73 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20  s no difference 
36b84 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 74 68  in the output th
36b85 65 6e 20 6e 65 69 74 68 65 72 20 64 6f 65 73 0d  en neither does.
36b86 0a 20 20 20 20 2a 2a 20 44 49 53 54 49 4e 43 54  .    ** DISTINCT
36b87 20 73 6f 20 69 74 20 63 61 6e 20 62 65 20 72 65   so it can be re
36b88 6d 6f 76 65 64 20 74 6f 6f 2e 20 2a 2f 0d 0a 20  moved too. */.. 
36b89 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
36b8a 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
36b8b 70 4f 72 64 65 72 42 79 29 3b 0d 0a 20 20 20 20  pOrderBy);..    
36b8c 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  p->pOrderBy = 0;
36b8d 0d 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67  ..    p->selFlag
36b8e 73 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63  s &= ~SF_Distinc
36b8f 74 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  t;..  }..  sqlit
36b90 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
36b91 72 73 65 2c 20 70 2c 20 30 29 3b 0d 0a 20 20 70  rse, p, 0);..  p
36b92 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72  OrderBy = p->pOr
36b93 64 65 72 42 79 3b 0d 0a 20 20 70 54 61 62 4c 69  derBy;..  pTabLi
36b94 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0d 0a 20  st = p->pSrc;.. 
36b95 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
36b96 69 73 74 3b 0d 0a 20 20 69 66 28 20 70 50 61 72  ist;..  if( pPar
36b97 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
36b98 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
36b99 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74  .    goto select
36b9a 5f 65 6e 64 3b 0d 0a 20 20 7d 0d 0a 20 20 69 73  _end;..  }..  is
36b9b 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61  Agg = (p->selFla
36b9c 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
36b9d 65 29 21 3d 30 3b 0d 0a 20 20 61 73 73 65 72 74  e)!=0;..  assert
36b9e 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0d 0a  ( pEList!=0 );..
36b9f 0d 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e  ..  /* Begin gen
36ba0 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0d 0a 20  erating code... 
36ba1 20 2a 2f 0d 0a 20 20 76 20 3d 20 73 71 6c 69 74   */..  v = sqlit
36ba2 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
36ba3 29 3b 0d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  );..  if( v==0 )
36ba4 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
36ba5 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 77 72 69  ;....  /* If wri
36ba6 74 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  ting to memory o
36ba7 72 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 73  r generating a s
36ba8 65 74 0d 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61 20  et..  ** only a 
36ba9 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61  single column ma
36baa 79 20 62 65 20 6f 75 74 70 75 74 2e 0d 0a 20 20  y be output...  
36bab 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
36bac 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
36bad 0d 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72  ..  if( checkFor
36bae 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63  MultiColumnSelec
36baf 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70  tError(pParse, p
36bb0 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  Dest, pEList->nE
36bb1 78 70 72 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74  xpr) ){..    got
36bb2 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0d 0a 20  o select_end;.. 
36bb3 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20   }..#endif....  
36bb4 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
36bb5 20 66 6f 72 20 61 6c 6c 20 73 75 62 2d 71 75 65   for all sub-que
36bb6 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
36bb7 20 63 6c 61 75 73 65 0d 0a 20 20 2a 2f 0d 0a 23   clause..  */..#
36bb8 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
36bb9 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
36bba 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
36bbb 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0d  LITE_OMIT_VIEW).
36bbc 0a 20 20 66 6f 72 28 69 3d 30 3b 20 21 70 2d 3e  .  for(i=0; !p->
36bbd 70 50 72 69 6f 72 20 26 26 20 69 3c 70 54 61 62  pPrior && i<pTab
36bbe 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  List->nSrc; i++)
36bbf 7b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 53 72  {..    struct Sr
36bc0 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
36bc1 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
36bc2 5b 69 5d 3b 0d 0a 20 20 20 20 53 65 6c 65 63 74  [i];..    Select
36bc3 44 65 73 74 20 64 65 73 74 3b 0d 0a 20 20 20 20  Dest dest;..    
36bc4 53 65 6c 65 63 74 20 2a 70 53 75 62 20 3d 20 70  Select *pSub = p
36bc5 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0d 0a  Item->pSelect;..
36bc6 20 20 20 20 69 6e 74 20 69 73 41 67 67 53 75 62      int isAggSub
36bc7 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 53 75  ;....    if( pSu
36bc8 62 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  b==0 ) continue;
36bc9 0d 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d  ..    if( pItem-
36bca 3e 61 64 64 72 46 69 6c 6c 53 75 62 20 29 7b 0d  >addrFillSub ){.
36bcb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
36bcc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
36bcd 6f 73 75 62 2c 20 70 49 74 65 6d 2d 3e 72 65 67  osub, pItem->reg
36bce 52 65 74 75 72 6e 2c 20 70 49 74 65 6d 2d 3e 61  Return, pItem->a
36bcf 64 64 72 46 69 6c 6c 53 75 62 29 3b 0d 0a 20 20  ddrFillSub);..  
36bd0 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20      continue;.. 
36bd1 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49     }....    /* I
36bd2 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65 2e 6e  ncrement Parse.n
36bd3 48 65 69 67 68 74 20 62 79 20 74 68 65 20 68 65  Height by the he
36bd4 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61 72 67  ight of the larg
36bd5 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e 0d 0a  est expression..
36bd6 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66 65      ** tree refe
36bd7 72 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c 20  red to by this, 
36bd8 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65 63  the parent selec
36bd9 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65 6c  t. The child sel
36bda 65 63 74 0d 0a 20 20 20 20 2a 2a 20 6d 61 79 20  ect..    ** may 
36bdb 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69  contain expressi
36bdc 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d  on trees of at m
36bdd 6f 73 74 0d 0a 20 20 20 20 2a 2a 20 28 53 51 4c  ost..    ** (SQL
36bde 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
36bdf 54 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74  TH-Parse.nHeight
36be0 29 20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69  ) height. This i
36be1 73 20 61 20 62 69 74 0d 0a 20 20 20 20 2a 2a 20  s a bit..    ** 
36be2 6d 6f 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76  more conservativ
36be3 65 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79  e than necessary
36be4 2c 20 62 75 74 20 6d 75 63 68 20 65 61 73 69 65  , but much easie
36be5 72 20 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67  r than enforcing
36be6 0d 0a 20 20 20 20 2a 2a 20 61 6e 20 65 78 61 63  ..    ** an exac
36be7 74 20 6c 69 6d 69 74 2e 0d 0a 20 20 20 20 2a 2f  t limit...    */
36be8 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48  ..    pParse->nH
36be9 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65 33  eight += sqlite3
36bea 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74  SelectExprHeight
36beb 28 70 29 3b 0d 0a 0d 0a 20 20 20 20 69 73 41 67  (p);....    isAg
36bec 67 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73 65  gSub = (pSub->se
36bed 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72  lFlags & SF_Aggr
36bee 65 67 61 74 65 29 21 3d 30 3b 0d 0a 20 20 20 20  egate)!=0;..    
36bef 69 66 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75  if( flattenSubqu
36bf0 65 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ery(pParse, p, i
36bf1 2c 20 69 73 41 67 67 2c 20 69 73 41 67 67 53 75  , isAgg, isAggSu
36bf2 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  b) ){..      /* 
36bf3 54 68 69 73 20 73 75 62 71 75 65 72 79 20 63 61  This subquery ca
36bf4 6e 20 62 65 20 61 62 73 6f 72 62 65 64 20 69 6e  n be absorbed in
36bf5 74 6f 20 69 74 73 20 70 61 72 65 6e 74 2e 20 2a  to its parent. *
36bf6 2f 0d 0a 20 20 20 20 20 20 69 66 28 20 69 73 41  /..      if( isA
36bf7 67 67 53 75 62 20 29 7b 0d 0a 20 20 20 20 20 20  ggSub ){..      
36bf8 20 20 69 73 41 67 67 20 3d 20 31 3b 0d 0a 20 20    isAgg = 1;..  
36bf9 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67        p->selFlag
36bfa 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74  s |= SF_Aggregat
36bfb 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  e;..      }..   
36bfc 20 20 20 69 20 3d 20 2d 31 3b 0d 0a 20 20 20 20     i = -1;..    
36bfd 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a  }else{..      /*
36bfe 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
36bff 6f 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c  outine that will
36c00 20 66 69 6c 6c 20 61 6e 20 65 70 68 65 6d 65 72   fill an ephemer
36c01 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0d 0a 20  al table with.. 
36c02 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74       ** the cont
36c03 65 6e 74 20 6f 66 20 74 68 69 73 20 73 75 62 71  ent of this subq
36c04 75 65 72 79 2e 20 20 70 49 74 65 6d 2d 3e 61 64  uery.  pItem->ad
36c05 64 72 46 69 6c 6c 53 75 62 20 77 69 6c 6c 20 70  drFillSub will p
36c06 6f 69 6e 74 0d 0a 20 20 20 20 20 20 2a 2a 20 74  oint..      ** t
36c07 6f 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  o the address of
36c08 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 73   the generated s
36c09 75 62 72 6f 75 74 69 6e 65 2e 20 20 70 49 74 65  ubroutine.  pIte
36c0a 6d 2d 3e 72 65 67 52 65 74 75 72 6e 0d 0a 20 20  m->regReturn..  
36c0b 20 20 20 20 2a 2a 20 69 73 20 61 20 72 65 67 69      ** is a regi
36c0c 73 74 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74  ster allocated t
36c0d 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 72 6f  o hold the subro
36c0e 75 74 69 6e 65 20 72 65 74 75 72 6e 20 61 64 64  utine return add
36c0f 72 65 73 73 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ress..      */..
36c10 20 20 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64        int topAdd
36c11 72 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6f 6e  r;..      int on
36c12 63 65 41 64 64 72 20 3d 20 30 3b 0d 0a 20 20 20  ceAddr = 0;..   
36c13 20 20 20 69 6e 74 20 72 65 74 41 64 64 72 3b 0d     int retAddr;.
36c14 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
36c15 49 74 65 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75  Item->addrFillSu
36c16 62 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70  b==0 );..      p
36c17 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e 20  Item->regReturn 
36c18 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
36c19 3b 0d 0a 20 20 20 20 20 20 74 6f 70 41 64 64 72  ;..      topAddr
36c1a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
36c1b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
36c1c 65 72 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 72 65  er, 0, pItem->re
36c1d 67 52 65 74 75 72 6e 29 3b 0d 0a 20 20 20 20 20  gReturn);..     
36c1e 20 70 49 74 65 6d 2d 3e 61 64 64 72 46 69 6c 6c   pItem->addrFill
36c1f 53 75 62 20 3d 20 74 6f 70 41 64 64 72 2b 31 3b  Sub = topAddr+1;
36c20 0d 0a 20 20 20 20 20 20 56 64 62 65 4e 6f 6f 70  ..      VdbeNoop
36c21 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6d 61 74  Comment((v, "mat
36c22 65 72 69 61 6c 69 7a 65 20 25 73 22 2c 20 70 49  erialize %s", pI
36c23 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  tem->pTab->zName
36c24 29 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  ));..      if( p
36c25 49 74 65 6d 2d 3e 69 73 43 6f 72 72 65 6c 61 74  Item->isCorrelat
36c26 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ed==0 ){..      
36c27 20 20 2f 2a 20 49 66 20 74 68 65 20 73 75 62 71    /* If the subq
36c28 75 65 72 79 20 69 73 20 6e 6f 20 63 6f 72 72 65  uery is no corre
36c29 6c 61 74 65 64 20 61 6e 64 20 69 66 20 77 65 20  lated and if we 
36c2a 61 72 65 20 6e 6f 74 20 69 6e 73 69 64 65 20 6f  are not inside o
36c2b 66 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  f..        ** a 
36c2c 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20 77 65  trigger, then we
36c2d 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 63 6f   only need to co
36c2e 6d 70 75 74 65 20 74 68 65 20 76 61 6c 75 65 20  mpute the value 
36c2f 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0d  of the subquery.
36c30 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 63 65  .        ** once
36c31 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 6f 6e  . */..        on
36c32 63 65 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33  ceAddr = sqlite3
36c33 43 6f 64 65 4f 6e 63 65 28 70 50 61 72 73 65 29  CodeOnce(pParse)
36c34 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
36c35 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
36c36 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
36c37 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74  RT_EphemTab, pIt
36c38 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0d 0a 20  em->iCursor);.. 
36c39 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49       explainSetI
36c3a 6e 74 65 67 65 72 28 70 49 74 65 6d 2d 3e 69 53  nteger(pItem->iS
36c3b 65 6c 65 63 74 49 64 2c 20 28 75 38 29 70 50 61  electId, (u8)pPa
36c3c 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65 63 74  rse->iNextSelect
36c3d 49 64 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  Id);..      sqli
36c3e 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
36c3f 2c 20 70 53 75 62 2c 20 26 64 65 73 74 29 3b 0d  , pSub, &dest);.
36c40 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54  .      pItem->pT
36c41 61 62 2d 3e 6e 52 6f 77 45 73 74 20 3d 20 28 75  ab->nRowEst = (u
36c42 6e 73 69 67 6e 65 64 29 70 53 75 62 2d 3e 6e 53  nsigned)pSub->nS
36c43 65 6c 65 63 74 52 6f 77 3b 0d 0a 20 20 20 20 20  electRow;..     
36c44 20 69 66 28 20 6f 6e 63 65 41 64 64 72 20 29 20   if( onceAddr ) 
36c45 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
36c46 65 72 65 28 76 2c 20 6f 6e 63 65 41 64 64 72 29  ere(v, onceAddr)
36c47 3b 0d 0a 20 20 20 20 20 20 72 65 74 41 64 64 72  ;..      retAddr
36c48 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
36c49 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72  dOp1(v, OP_Retur
36c4a 6e 2c 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74  n, pItem->regRet
36c4b 75 72 6e 29 3b 0d 0a 20 20 20 20 20 20 56 64 62  urn);..      Vdb
36c4c 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e  eComment((v, "en
36c4d 64 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 70 54  d %s", pItem->pT
36c4e 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 20 20  ab->zName));..  
36c4f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
36c50 68 61 6e 67 65 50 31 28 76 2c 20 74 6f 70 41 64  hangeP1(v, topAd
36c51 64 72 2c 20 72 65 74 41 64 64 72 29 3b 0d 0a 20  dr, retAddr);.. 
36c52 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 65 61       sqlite3Clea
36c53 72 54 65 6d 70 52 65 67 43 61 63 68 65 28 70 50  rTempRegCache(pP
36c54 61 72 73 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  arse);..    }.. 
36c55 20 20 20 69 66 28 20 2f 2a 70 50 61 72 73 65 2d     if( /*pParse-
36c56 3e 6e 45 72 72 20 7c 7c 2a 2f 20 64 62 2d 3e 6d  >nErr ||*/ db->m
36c57 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a  allocFailed ){..
36c58 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63        goto selec
36c59 74 5f 65 6e 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20  t_end;..    }.. 
36c5a 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67     pParse->nHeig
36c5b 68 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c  ht -= sqlite3Sel
36c5c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29  ectExprHeight(p)
36c5d 3b 0d 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20  ;..    pTabList 
36c5e 3d 20 70 2d 3e 70 53 72 63 3b 0d 0a 20 20 20 20  = p->pSrc;..    
36c5f 69 66 28 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72  if( !IgnorableOr
36c60 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0d  derby(pDest) ){.
36c61 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20  .      pOrderBy 
36c62 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0d 0a  = p->pOrderBy;..
36c63 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 70 45      }..  }..  pE
36c64 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74  List = p->pEList
36c65 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70 57 68  ;..#endif..  pWh
36c66 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b  ere = p->pWhere;
36c67 0d 0a 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70  ..  pGroupBy = p
36c68 2d 3e 70 47 72 6f 75 70 42 79 3b 0d 0a 20 20 70  ->pGroupBy;..  p
36c69 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76  Having = p->pHav
36c6a 69 6e 67 3b 0d 0a 20 20 69 73 44 69 73 74 69 6e  ing;..  isDistin
36c6b 63 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ct = (p->selFlag
36c6c 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29  s & SF_Distinct)
36c6d 21 3d 30 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  !=0;....#ifndef 
36c6e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
36c6f 4f 55 4e 44 5f 53 45 4c 45 43 54 0d 0a 20 20 2f  OUND_SELECT..  /
36c70 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 72  * If there is ar
36c71 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  e a sequence of 
36c72 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20  queries, do the 
36c73 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72  earlier ones fir
36c74 73 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  st...  */..  if(
36c75 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0d 0a 20   p->pPrior ){.. 
36c76 20 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74     if( p->pRight
36c77 6d 6f 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  most==0 ){..    
36c78 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c    Select *pLoop,
36c79 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0d 0a 20   *pRight = 0;.. 
36c7a 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30       int cnt = 0
36c7b 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6d 78 53  ;..      int mxS
36c7c 65 6c 65 63 74 3b 0d 0a 20 20 20 20 20 20 66 6f  elect;..      fo
36c7d 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70  r(pLoop=p; pLoop
36c7e 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70  ; pLoop=pLoop->p
36c7f 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29 7b 0d 0a  Prior, cnt++){..
36c80 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70          pLoop->p
36c81 52 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b 0d 0a  Rightmost = p;..
36c82 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70          pLoop->p
36c83 4e 65 78 74 20 3d 20 70 52 69 67 68 74 3b 0d 0a  Next = pRight;..
36c84 20 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d          pRight =
36c85 20 70 4c 6f 6f 70 3b 0d 0a 20 20 20 20 20 20 7d   pLoop;..      }
36c86 0d 0a 20 20 20 20 20 20 6d 78 53 65 6c 65 63 74  ..      mxSelect
36c87 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
36c88 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
36c89 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0d 0a 20 20  UND_SELECT];..  
36c8a 20 20 20 20 69 66 28 20 6d 78 53 65 6c 65 63 74      if( mxSelect
36c8b 20 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65 63 74   && cnt>mxSelect
36c8c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c   ){..        sql
36c8d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
36c8e 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
36c8f 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e 64  erms in compound
36c90 20 53 45 4c 45 43 54 22 29 3b 0d 0a 20 20 20 20   SELECT");..    
36c91 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
36c92 65 6e 64 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  end;..      }.. 
36c93 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 6d     }..    rc = m
36c94 75 6c 74 69 53 65 6c 65 63 74 28 70 50 61 72 73  ultiSelect(pPars
36c95 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0d 0a 20  e, p, pDest);.. 
36c96 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49 6e 74     explainSetInt
36c97 65 67 65 72 28 70 50 61 72 73 65 2d 3e 69 53 65  eger(pParse->iSe
36c98 6c 65 63 74 49 64 2c 20 69 52 65 73 74 6f 72 65  lectId, iRestore
36c99 53 65 6c 65 63 74 49 64 29 3b 0d 0a 20 20 20 20  SelectId);..    
36c9a 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d  return rc;..  }.
36c9b 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20  .#endif....  /* 
36c9c 49 66 20 74 68 65 72 65 20 69 73 20 62 6f 74 68  If there is both
36c9d 20 61 20 47 52 4f 55 50 20 42 59 20 61 6e 64 20   a GROUP BY and 
36c9e 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
36c9f 73 65 20 61 6e 64 20 74 68 65 79 20 61 72 65 0d  se and they are.
36ca0 0a 20 20 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c  .  ** identical,
36ca1 20 74 68 65 6e 20 64 69 73 61 62 6c 65 20 74 68   then disable th
36ca2 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
36ca3 65 20 73 69 6e 63 65 20 74 68 65 20 47 52 4f 55  e since the GROU
36ca4 50 20 42 59 0d 0a 20 20 2a 2a 20 77 69 6c 6c 20  P BY..  ** will 
36ca5 63 61 75 73 65 20 65 6c 65 6d 65 6e 74 73 20 74  cause elements t
36ca6 6f 20 63 6f 6d 65 20 6f 75 74 20 69 6e 20 74 68  o come out in th
36ca7 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 2e  e correct order.
36ca8 20 20 54 68 69 73 20 69 73 0d 0a 20 20 2a 2a 20    This is..  ** 
36ca9 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  an optimization 
36caa 2d 20 74 68 65 20 63 6f 72 72 65 63 74 20 61 6e  - the correct an
36cab 73 77 65 72 20 73 68 6f 75 6c 64 20 72 65 73 75  swer should resu
36cac 6c 74 20 72 65 67 61 72 64 6c 65 73 73 2e 0d 0a  lt regardless...
36cad 20 20 2a 2a 20 55 73 65 20 74 68 65 20 53 51 4c    ** Use the SQL
36cae 49 54 45 5f 47 72 6f 75 70 42 79 4f 72 64 65 72  ITE_GroupByOrder
36caf 20 66 6c 61 67 20 77 69 74 68 20 53 51 4c 49 54   flag with SQLIT
36cb0 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49 4d  E_TESTCTRL_OPTIM
36cb1 49 5a 45 52 0d 0a 20 20 2a 2a 20 74 6f 20 64 69  IZER..  ** to di
36cb2 73 61 62 6c 65 20 74 68 69 73 20 6f 70 74 69 6d  sable this optim
36cb3 69 7a 61 74 69 6f 6e 20 66 6f 72 20 74 65 73 74  ization for test
36cb4 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 0d 0a 20  ing purposes... 
36cb5 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74   */..  if( sqlit
36cb6 65 33 45 78 70 72 4c 69 73 74 43 6f 6d 70 61 72  e3ExprListCompar
36cb7 65 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70  e(p->pGroupBy, p
36cb8 4f 72 64 65 72 42 79 29 3d 3d 30 0d 0a 20 20 20  OrderBy)==0..   
36cb9 20 20 20 20 20 20 26 26 20 28 64 62 2d 3e 66 6c        && (db->fl
36cba 61 67 73 20 26 20 53 51 4c 49 54 45 5f 47 72 6f  ags & SQLITE_Gro
36cbb 75 70 42 79 4f 72 64 65 72 29 3d 3d 30 20 29 7b  upByOrder)==0 ){
36cbc 0d 0a 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d  ..    pOrderBy =
36cbd 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   0;..  }....  /*
36cbe 20 49 66 20 74 68 65 20 71 75 65 72 79 20 69 73   If the query is
36cbf 20 44 49 53 54 49 4e 43 54 20 77 69 74 68 20 61   DISTINCT with a
36cc0 6e 20 4f 52 44 45 52 20 42 59 20 62 75 74 20 69  n ORDER BY but i
36cc1 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61  s not an aggrega
36cc2 74 65 2c 20 61 6e 64 20 0d 0a 20 20 2a 2a 20 69  te, and ..  ** i
36cc3 66 20 74 68 65 20 73 65 6c 65 63 74 2d 6c 69 73  f the select-lis
36cc4 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  t is the same as
36cc5 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6c 69   the ORDER BY li
36cc6 73 74 2c 20 74 68 65 6e 20 74 68 69 73 20 71 75  st, then this qu
36cc7 65 72 79 0d 0a 20 20 2a 2a 20 63 61 6e 20 62 65  ery..  ** can be
36cc8 20 72 65 77 72 69 74 74 65 6e 20 61 73 20 61 20   rewritten as a 
36cc9 47 52 4f 55 50 20 42 59 2e 20 49 6e 20 6f 74 68  GROUP BY. In oth
36cca 65 72 20 77 6f 72 64 73 2c 20 74 68 69 73 3a 0d  er words, this:.
36ccb 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20  .  **..  **     
36ccc 53 45 4c 45 43 54 20 44 49 53 54 49 4e 43 54 20  SELECT DISTINCT 
36ccd 78 79 7a 20 46 52 4f 4d 20 2e 2e 2e 20 4f 52 44  xyz FROM ... ORD
36cce 45 52 20 42 59 20 78 79 7a 0d 0a 20 20 2a 2a 0d  ER BY xyz..  **.
36ccf 0a 20 20 2a 2a 20 69 73 20 74 72 61 6e 73 66 6f  .  ** is transfo
36cd0 72 6d 65 64 20 74 6f 3a 0d 0a 20 20 2a 2a 0d 0a  rmed to:..  **..
36cd1 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
36cd2 78 79 7a 20 46 52 4f 4d 20 2e 2e 2e 20 47 52 4f  xyz FROM ... GRO
36cd3 55 50 20 42 59 20 78 79 7a 0d 0a 20 20 2a 2a 0d  UP BY xyz..  **.
36cd4 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  .  ** The second
36cd5 20 66 6f 72 6d 20 69 73 20 70 72 65 66 65 72 72   form is preferr
36cd6 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 69  ed as a single i
36cd7 6e 64 65 78 20 28 6f 72 20 74 65 6d 70 2d 74 61  ndex (or temp-ta
36cd8 62 6c 65 29 20 6d 61 79 20 62 65 20 0d 0a 20 20  ble) may be ..  
36cd9 2a 2a 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68  ** used for both
36cda 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 61 6e   the ORDER BY an
36cdb 64 20 44 49 53 54 49 4e 43 54 20 70 72 6f 63 65  d DISTINCT proce
36cdc 73 73 69 6e 67 2e 20 41 73 20 6f 72 69 67 69 6e  ssing. As origin
36cdd 61 6c 6c 79 20 0d 0a 20 20 2a 2a 20 77 72 69 74  ally ..  ** writ
36cde 74 65 6e 20 74 68 65 20 71 75 65 72 79 20 6d 75  ten the query mu
36cdf 73 74 20 75 73 65 20 61 20 74 65 6d 70 2d 74 61  st use a temp-ta
36ce0 62 6c 65 20 66 6f 72 20 61 74 20 6c 65 61 73 74  ble for at least
36ce1 20 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45   one of the ORDE
36ce2 52 20 0d 0a 20 20 2a 2a 20 42 59 20 61 6e 64 20  R ..  ** BY and 
36ce3 44 49 53 54 49 4e 43 54 2c 20 61 6e 64 20 61 6e  DISTINCT, and an
36ce4 20 69 6e 64 65 78 20 6f 72 20 73 65 70 61 72 61   index or separa
36ce5 74 65 20 74 65 6d 70 2d 74 61 62 6c 65 20 66 6f  te temp-table fo
36ce6 72 20 74 68 65 20 6f 74 68 65 72 2e 0d 0a 20 20  r the other...  
36ce7 2a 2f 0d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65  */..  if( (p->se
36ce8 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
36ce9 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
36cea 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63  te))==SF_Distinc
36ceb 74 20 0d 0a 20 20 20 26 26 20 73 71 6c 69 74 65  t ..   && sqlite
36cec 33 45 78 70 72 4c 69 73 74 43 6f 6d 70 61 72 65  3ExprListCompare
36ced 28 70 4f 72 64 65 72 42 79 2c 20 70 2d 3e 70 45  (pOrderBy, p->pE
36cee 4c 69 73 74 29 3d 3d 30 0d 0a 20 20 29 7b 0d 0a  List)==0..  ){..
36cef 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20      p->selFlags 
36cf0 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b  &= ~SF_Distinct;
36cf1 0d 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75 70 42  ..    p->pGroupB
36cf2 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
36cf3 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
36cf4 4c 69 73 74 2c 20 30 29 3b 0d 0a 20 20 20 20 70  List, 0);..    p
36cf5 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72  GroupBy = p->pGr
36cf6 6f 75 70 42 79 3b 0d 0a 20 20 20 20 70 4f 72 64  oupBy;..    pOrd
36cf7 65 72 42 79 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  erBy = 0;..  }..
36cf8 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
36cf9 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
36cfa 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73  lause, then this
36cfb 20 73 6f 72 74 69 6e 67 0d 0a 20 20 2a 2a 20 69   sorting..  ** i
36cfc 6e 64 65 78 20 6d 69 67 68 74 20 65 6e 64 20 75  ndex might end u
36cfd 70 20 62 65 69 6e 67 20 75 6e 75 73 65 64 20 69  p being unused i
36cfe 66 20 74 68 65 20 64 61 74 61 20 63 61 6e 20 62  f the data can b
36cff 65 20 0d 0a 20 20 2a 2a 20 65 78 74 72 61 63 74  e ..  ** extract
36d00 65 64 20 69 6e 20 70 72 65 2d 73 6f 72 74 65 64  ed in pre-sorted
36d01 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 61 74   order.  If that
36d02 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
36d03 65 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 4f 50 5f  en the..  ** OP_
36d04 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
36d05 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62  struction will b
36d06 65 20 63 68 61 6e 67 65 64 20 74 6f 20 61 6e 20  e changed to an 
36d07 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0d 0a 20 20  OP_Noop once..  
36d08 2a 2a 20 77 65 20 66 69 67 75 72 65 20 6f 75 74  ** we figure out
36d09 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 69 6e   that the sortin
36d0a 67 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e  g index is not n
36d0b 65 65 64 65 64 2e 20 20 54 68 65 20 61 64 64 72  eeded.  The addr
36d0c 53 6f 72 74 49 6e 64 65 78 0d 0a 20 20 2a 2a 20  SortIndex..  ** 
36d0d 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64  variable is used
36d0e 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74   to facilitate t
36d0f 68 61 74 20 63 68 61 6e 67 65 2e 0d 0a 20 20 2a  hat change...  *
36d10 2f 0d 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  /..  if( pOrderB
36d11 79 20 29 7b 0d 0a 20 20 20 20 4b 65 79 49 6e 66  y ){..    KeyInf
36d12 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0d 0a 20 20  o *pKeyInfo;..  
36d13 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79    pKeyInfo = key
36d14 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
36d15 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
36d16 79 29 3b 0d 0a 20 20 20 20 70 4f 72 64 65 72 42  y);..    pOrderB
36d17 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 70 50  y->iECursor = pP
36d18 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a 20  arse->nTab++;.. 
36d19 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70     p->addrOpenEp
36d1a 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72 74  hm[2] = addrSort
36d1b 49 6e 64 65 78 20 3d 0d 0a 20 20 20 20 20 20 73  Index =..      s
36d1c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
36d1d 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
36d1e 65 72 61 6c 2c 0d 0a 20 20 20 20 20 20 20 20 20  eral,..         
36d1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75    pOrderBy->iECu
36d21 72 73 6f 72 2c 20 70 4f 72 64 65 72 42 79 2d 3e  rsor, pOrderBy->
36d22 6e 45 78 70 72 2b 32 2c 20 30 2c 0d 0a 20 20 20  nExpr+2, 0,..   
36d23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d24 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
36d25 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
36d26 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0d 0a 20  NFO_HANDOFF);.. 
36d27 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61 64 64   }else{..    add
36d28 72 53 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b  rSortIndex = -1;
36d29 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ..  }....  /* If
36d2a 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 64   the output is d
36d2b 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20 74 65  estined for a te
36d2c 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 6f  mporary table, o
36d2d 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 0d  pen that table..
36d2e 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 44 65  .  */..  if( pDe
36d2f 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
36d30 70 68 65 6d 54 61 62 20 29 7b 0d 0a 20 20 20 20  phemTab ){..    
36d31 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
36d32 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
36d33 6d 65 72 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50  meral, pDest->iP
36d34 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  arm, pEList->nEx
36d35 70 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  pr);..  }....  /
36d36 2a 20 53 65 74 20 74 68 65 20 6c 69 6d 69 74 65  * Set the limite
36d37 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 45 6e 64  r...  */..  iEnd
36d38 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
36d39 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20 70  keLabel(v);..  p
36d3a 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 28  ->nSelectRow = (
36d3b 64 6f 75 62 6c 65 29 4c 41 52 47 45 53 54 5f 49  double)LARGEST_I
36d3c 4e 54 36 34 3b 0d 0a 20 20 63 6f 6d 70 75 74 65  NT64;..  compute
36d3d 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
36d3e 50 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b  Parse, p, iEnd);
36d3f 0d 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  ..  if( p->iLimi
36d40 74 3d 3d 30 20 26 26 20 61 64 64 72 53 6f 72 74  t==0 && addrSort
36d41 49 6e 64 65 78 3e 3d 30 20 29 7b 0d 0a 20 20 20  Index>=0 ){..   
36d42 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f   sqlite3VdbeGetO
36d43 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64  p(v, addrSortInd
36d44 65 78 29 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  ex)->opcode = OP
36d45 5f 53 6f 72 74 65 72 4f 70 65 6e 3b 0d 0a 20 20  _SorterOpen;..  
36d46 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d    p->selFlags |=
36d47 20 53 46 5f 55 73 65 53 6f 72 74 65 72 3b 0d 0a   SF_UseSorter;..
36d48 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4f 70 65 6e    }....  /* Open
36d49 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78   a virtual index
36d4a 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20   to use for the 
36d4b 64 69 73 74 69 6e 63 74 20 73 65 74 2e 0d 0a 20  distinct set... 
36d4c 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 73 65   */..  if( p->se
36d4d 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
36d4e 69 6e 63 74 20 29 7b 0d 0a 20 20 20 20 4b 65 79  inct ){..    Key
36d4f 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0d  Info *pKeyInfo;.
36d50 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20  .    distinct = 
36d51 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d  pParse->nTab++;.
36d52 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
36d53 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
36d54 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  ist(pParse, p->p
36d55 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 61 64 64  EList);..    add
36d56 72 44 69 73 74 69 6e 63 74 49 6e 64 65 78 20 3d  rDistinctIndex =
36d57 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36d58 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
36d59 65 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e 63 74  emeral, distinct
36d5a 2c 20 30 2c 20 30 2c 0d 0a 20 20 20 20 20 20 20  , 0, 0,..       
36d5b 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
36d5c 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
36d5d 44 4f 46 46 29 3b 0d 0a 20 20 20 20 73 71 6c 69  DOFF);..    sqli
36d5e 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
36d5f 76 2c 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52  v, BTREE_UNORDER
36d60 45 44 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ED);..  }else{..
36d61 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20 61      distinct = a
36d62 64 64 72 44 69 73 74 69 6e 63 74 49 6e 64 65 78  ddrDistinctIndex
36d63 20 3d 20 2d 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20   = -1;..  }.... 
36d64 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e   /* Aggregate an
36d65 64 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20  d non-aggregate 
36d66 71 75 65 72 69 65 73 20 61 72 65 20 68 61 6e 64  queries are hand
36d67 6c 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20  led differently 
36d68 2a 2f 0d 0a 20 20 69 66 28 20 21 69 73 41 67 67  */..  if( !isAgg
36d69 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d 30 20   && pGroupBy==0 
36d6a 29 7b 0d 0a 20 20 20 20 45 78 70 72 4c 69 73 74  ){..    ExprList
36d6b 20 2a 70 44 69 73 74 20 3d 20 28 69 73 44 69 73   *pDist = (isDis
36d6c 74 69 6e 63 74 20 3f 20 70 2d 3e 70 45 4c 69 73  tinct ? p->pELis
36d6d 74 20 3a 20 30 29 3b 0d 0a 0d 0a 20 20 20 20 2f  t : 0);....    /
36d6e 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61  * Begin the data
36d6f 62 61 73 65 20 73 63 61 6e 2e 20 2a 2f 0d 0a 20  base scan. */.. 
36d70 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69     pWInfo = sqli
36d71 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
36d72 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
36d73 70 57 68 65 72 65 2c 20 26 70 4f 72 64 65 72 42  pWhere, &pOrderB
36d74 79 2c 20 70 44 69 73 74 2c 20 30 29 3b 0d 0a 20  y, pDist, 0);.. 
36d75 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
36d76 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
36d77 6e 64 3b 0d 0a 20 20 20 20 69 66 28 20 70 57 49  nd;..    if( pWI
36d78 6e 66 6f 2d 3e 6e 52 6f 77 4f 75 74 20 3c 20 70  nfo->nRowOut < p
36d79 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 29 20 70  ->nSelectRow ) p
36d7a 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 70  ->nSelectRow = p
36d7b 57 49 6e 66 6f 2d 3e 6e 52 6f 77 4f 75 74 3b 0d  WInfo->nRowOut;.
36d7c 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 73 6f 72  ...    /* If sor
36d7d 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20  ting index that 
36d7e 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61  was created by a
36d7f 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70   prior OP_OpenEp
36d80 68 65 6d 65 72 61 6c 20 0d 0a 20 20 20 20 2a 2a  hemeral ..    **
36d81 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e 64   instruction end
36d82 65 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67 20  ed up not being 
36d83 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68 61  needed, then cha
36d84 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45  nge the OP_OpenE
36d85 70 68 65 6d 65 72 61 6c 0d 0a 20 20 20 20 2a 2a  phemeral..    **
36d86 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70   into an OP_Noop
36d87 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  ...    */..    i
36d88 66 28 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78  f( addrSortIndex
36d89 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d  >=0 && pOrderBy=
36d8a 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 ){..      sql
36d8b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
36d8c 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74  Noop(v, addrSort
36d8d 49 6e 64 65 78 29 3b 0d 0a 20 20 20 20 20 20 70  Index);..      p
36d8e 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
36d8f 5d 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a  ] = -1;..    }..
36d90 0d 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f  ..    if( pWInfo
36d91 2d 3e 65 44 69 73 74 69 6e 63 74 20 29 7b 0d 0a  ->eDistinct ){..
36d92 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f        VdbeOp *pO
36d93 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
36d94 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 72    /* No longer r
36d95 65 71 75 69 72 65 64 20 4f 70 65 6e 45 70 68 65  equired OpenEphe
36d96 6d 65 72 61 6c 20 69 6e 73 74 72 2e 20 2a 2f 0d  meral instr. */.
36d97 0a 20 20 20 20 20 0d 0a 20 20 20 20 20 20 61 73  .     ..      as
36d98 73 65 72 74 28 20 61 64 64 72 44 69 73 74 69 6e  sert( addrDistin
36d99 63 74 49 6e 64 65 78 3e 3d 30 20 29 3b 0d 0a 20  ctIndex>=0 );.. 
36d9a 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74       pOp = sqlit
36d9b 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 61  e3VdbeGetOp(v, a
36d9c 64 64 72 44 69 73 74 69 6e 63 74 49 6e 64 65 78  ddrDistinctIndex
36d9d 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 61 73 73 65  );....      asse
36d9e 72 74 28 20 69 73 44 69 73 74 69 6e 63 74 20 29  rt( isDistinct )
36d9f 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
36da0 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e   pWInfo->eDistin
36da1 63 74 3d 3d 57 48 45 52 45 5f 44 49 53 54 49 4e  ct==WHERE_DISTIN
36da2 43 54 5f 4f 52 44 45 52 45 44 20 0d 0a 20 20 20  CT_ORDERED ..   
36da3 20 20 20 20 20 20 20 20 7c 7c 20 70 57 49 6e 66          || pWInf
36da4 6f 2d 3e 65 44 69 73 74 69 6e 63 74 3d 3d 57 48  o->eDistinct==WH
36da5 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e 49  ERE_DISTINCT_UNI
36da6 51 55 45 20 0d 0a 20 20 20 20 20 20 29 3b 0d 0a  QUE ..      );..
36da7 20 20 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d        distinct =
36da8 20 2d 31 3b 0d 0a 20 20 20 20 20 20 69 66 28 20   -1;..      if( 
36da9 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e 63  pWInfo->eDistinc
36daa 74 3d 3d 57 48 45 52 45 5f 44 49 53 54 49 4e 43  t==WHERE_DISTINC
36dab 54 5f 4f 52 44 45 52 45 44 20 29 7b 0d 0a 20 20  T_ORDERED ){..  
36dac 20 20 20 20 20 20 69 6e 74 20 69 4a 75 6d 70 3b        int iJump;
36dad 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 45  ..        int iE
36dae 78 70 72 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e  xpr;..        in
36daf 74 20 69 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72  t iFlag = ++pPar
36db0 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 20  se->nMem;..     
36db1 20 20 20 69 6e 74 20 69 42 61 73 65 20 3d 20 70     int iBase = p
36db2 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0d 0a  Parse->nMem+1;..
36db3 20 20 20 20 20 20 20 20 69 6e 74 20 69 42 61 73          int iBas
36db4 65 32 20 3d 20 69 42 61 73 65 20 2b 20 70 45 4c  e2 = iBase + pEL
36db5 69 73 74 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20  ist->nExpr;..   
36db6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
36db7 6d 20 2b 3d 20 28 70 45 4c 69 73 74 2d 3e 6e 45  m += (pEList->nE
36db8 78 70 72 2a 32 29 3b 0d 0a 0d 0a 20 20 20 20 20  xpr*2);....     
36db9 20 20 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65     /* Change the
36dba 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
36dbb 6c 20 63 6f 64 65 64 20 65 61 72 6c 69 65 72 20  l coded earlier 
36dbc 74 6f 20 61 6e 20 4f 50 5f 49 6e 74 65 67 65 72  to an OP_Integer
36dbd 2e 20 54 68 65 0d 0a 20 20 20 20 20 20 20 20 2a  . The..        *
36dbe 2a 20 4f 50 5f 49 6e 74 65 67 65 72 20 69 6e 69  * OP_Integer ini
36dbf 74 69 61 6c 69 7a 65 73 20 74 68 65 20 22 66 69  tializes the "fi
36dc0 72 73 74 20 72 6f 77 22 20 66 6c 61 67 2e 20 20  rst row" flag.  
36dc1 2a 2f 0d 0a 20 20 20 20 20 20 20 20 70 4f 70 2d  */..        pOp-
36dc2 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 49 6e 74  >opcode = OP_Int
36dc3 65 67 65 72 3b 0d 0a 20 20 20 20 20 20 20 20 70  eger;..        p
36dc4 4f 70 2d 3e 70 31 20 3d 20 31 3b 0d 0a 20 20 20  Op->p1 = 1;..   
36dc5 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 69       pOp->p2 = i
36dc6 46 6c 61 67 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  Flag;....       
36dc7 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
36dc8 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
36dc9 20 70 45 4c 69 73 74 2c 20 69 42 61 73 65 2c 20   pEList, iBase, 
36dca 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 4a 75  1);..        iJu
36dcb 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  mp = sqlite3Vdbe
36dcc 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b  CurrentAddr(v) +
36dcd 20 31 20 2b 20 70 45 4c 69 73 74 2d 3e 6e 45 78   1 + pEList->nEx
36dce 70 72 20 2b 20 31 20 2b 20 31 3b 0d 0a 20 20 20  pr + 1 + 1;..   
36dcf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
36dd0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
36dd1 20 69 46 6c 61 67 2c 20 69 4a 75 6d 70 2d 31 29   iFlag, iJump-1)
36dd2 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
36dd3 45 78 70 72 3d 30 3b 20 69 45 78 70 72 3c 70 45  Expr=0; iExpr<pE
36dd4 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 45 78  List->nExpr; iEx
36dd5 70 72 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  pr++){..        
36dd6 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
36dd7 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
36dd8 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
36dd9 4c 69 73 74 2d 3e 61 5b 69 45 78 70 72 5d 2e 70  List->a[iExpr].p
36dda 45 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 20 20  Expr);..        
36ddb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
36ddc 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 69 42  Op3(v, OP_Ne, iB
36ddd 61 73 65 2b 69 45 78 70 72 2c 20 69 4a 75 6d 70  ase+iExpr, iJump
36dde 2c 20 69 42 61 73 65 32 2b 69 45 78 70 72 29 3b  , iBase2+iExpr);
36ddf 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
36de0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
36de1 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68  v, -1, (const ch
36de2 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43  ar *)pColl, P4_C
36de3 4f 4c 4c 53 45 51 29 3b 0d 0a 20 20 20 20 20 20  OLLSEQ);..      
36de4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
36de5 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
36de6 45 5f 4e 55 4c 4c 45 51 29 3b 0d 0a 20 20 20 20  E_NULLEQ);..    
36de7 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73      }..        s
36de8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
36de9 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
36dea 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
36deb 65 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 73  e);....        s
36dec 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
36ded 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
36dee 30 2c 20 69 46 6c 61 67 29 3b 0d 0a 20 20 20 20  0, iFlag);..    
36def 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
36df0 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
36df1 64 72 28 76 29 3d 3d 69 4a 75 6d 70 20 29 3b 0d  dr(v)==iJump );.
36df2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
36df3 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
36df4 5f 4d 6f 76 65 2c 20 69 42 61 73 65 2c 20 69 42  _Move, iBase, iB
36df5 61 73 65 32 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  ase2, pEList->nE
36df6 78 70 72 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  xpr);..      }el
36df7 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 4f 70  se{..        pOp
36df8 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f  ->opcode = OP_No
36df9 6f 70 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  op;..      }..  
36dfa 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 55 73    }....    /* Us
36dfb 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 69  e the standard i
36dfc 6e 6e 65 72 20 6c 6f 6f 70 2e 20 2a 2f 0d 0a 20  nner loop. */.. 
36dfd 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
36dfe 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 45  op(pParse, p, pE
36dff 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64  List, 0, 0, pOrd
36e00 65 72 42 79 2c 20 64 69 73 74 69 6e 63 74 2c 20  erBy, distinct, 
36e01 70 44 65 73 74 2c 0d 0a 20 20 20 20 20 20 20 20  pDest,..        
36e02 20 20 20 20 20 20 20 20 20 20 20 20 70 57 49 6e              pWIn
36e03 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70  fo->iContinue, p
36e04 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0d  WInfo->iBreak);.
36e05 0a 0d 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68  ...    /* End th
36e06 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
36e07 6c 6f 6f 70 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  loop...    */.. 
36e08 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
36e09 6e 64 28 70 57 49 6e 66 6f 29 3b 0d 0a 20 20 7d  nd(pWInfo);..  }
36e0a 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  else{..    /* Th
36e0b 69 73 20 69 73 20 74 68 65 20 70 72 6f 63 65 73  is is the proces
36e0c 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61  sing for aggrega
36e0d 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0d 0a 20  te queries */.. 
36e0e 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
36e0f 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63  NC;    /* Name c
36e10 6f 6e 74 65 78 74 20 66 6f 72 20 70 72 6f 63 65  ontext for proce
36e11 73 73 69 6e 67 20 61 67 67 72 65 67 61 74 65 20  ssing aggregate 
36e12 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0d 0a  information */..
36e13 20 20 20 20 69 6e 74 20 69 41 4d 65 6d 3b 20 20      int iAMem;  
36e14 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
36e15 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72   Mem address for
36e16 20 73 74 6f 72 69 6e 67 20 63 75 72 72 65 6e 74   storing current
36e17 20 47 52 4f 55 50 20 42 59 20 2a 2f 0d 0a 20 20   GROUP BY */..  
36e18 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 20 20    int iBMem;    
36e19 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d        /* First M
36e1a 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 70  em address for p
36e1b 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20 42 59  revious GROUP BY
36e1c 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 55 73   */..    int iUs
36e1d 65 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20  eFlag;       /* 
36e1e 4d 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c 64  Mem address hold
36e1f 69 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61 74  ing flag indicat
36e20 69 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61 73  ing that at leas
36e21 74 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
36e22 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
36e23 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70  e row of the inp
36e24 75 74 20 74 6f 20 74 68 65 20 61 67 67 72 65 67  ut to the aggreg
36e25 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0d 0a 20  ator has been.. 
36e26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e27 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73         ** proces
36e28 73 65 64 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  sed */..    int 
36e29 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20  iAbortFlag;     
36e2a 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77  /* Mem address w
36e2b 68 69 63 68 20 63 61 75 73 65 73 20 71 75 65 72  hich causes quer
36e2c 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74  y abort if posit
36e2d 69 76 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  ive */..    int 
36e2e 67 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20 20  groupBySort;    
36e2f 2f 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f  /* Rows come fro
36e30 6d 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f 55  m source in GROU
36e31 50 20 42 59 20 6f 72 64 65 72 20 2a 2f 0d 0a 20  P BY order */.. 
36e32 20 20 20 69 6e 74 20 61 64 64 72 45 6e 64 3b 20     int addrEnd; 
36e33 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
36e34 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
36e35 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0d 0a  this SELECT */..
36e36 20 20 20 20 69 6e 74 20 73 6f 72 74 50 54 61 62      int sortPTab
36e37 20 3d 20 30 3b 20 20 20 2f 2a 20 50 73 65 75 64   = 0;   /* Pseud
36e38 6f 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 64  otable used to d
36e39 65 63 6f 64 65 20 73 6f 72 74 69 6e 67 20 72 65  ecode sorting re
36e3a 73 75 6c 74 73 20 2a 2f 0d 0a 20 20 20 20 69 6e  sults */..    in
36e3b 74 20 73 6f 72 74 4f 75 74 20 3d 20 30 3b 20 20  t sortOut = 0;  
36e3c 20 20 2f 2a 20 4f 75 74 70 75 74 20 72 65 67 69    /* Output regi
36e3d 73 74 65 72 20 66 72 6f 6d 20 74 68 65 20 73 6f  ster from the so
36e3e 72 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f  rter */....    /
36e3f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 61 6e 64  * Remove any and
36e40 20 61 6c 6c 20 61 6c 69 61 73 65 73 20 62 65 74   all aliases bet
36e41 77 65 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  ween the result 
36e42 73 65 74 20 61 6e 64 20 74 68 65 0d 0a 20 20 20  set and the..   
36e43 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 63 6c 61   ** GROUP BY cla
36e44 75 73 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  use...    */..  
36e45 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29    if( pGroupBy )
36e46 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 20  {..      int k; 
36e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e48 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
36e49 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20  ounter */..     
36e4a 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
36e4b 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f  _item *pItem;  /
36e4c 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
36e4d 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  er expression in
36e4e 20 61 20 6c 69 73 74 20 2a 2f 0d 0a 0d 0a 20 20   a list */....  
36e4f 20 20 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c      for(k=p->pEL
36e50 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65  ist->nExpr, pIte
36e51 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 20  m=p->pEList->a; 
36e52 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b  k>0; k--, pItem+
36e53 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 49 74  +){..        pIt
36e54 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0d  em->iAlias = 0;.
36e55 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
36e56 66 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d 3e  for(k=pGroupBy->
36e57 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 47 72  nExpr, pItem=pGr
36e58 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20 6b  oupBy->a; k>0; k
36e59 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0d 0a 20  --, pItem++){.. 
36e5a 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41         pItem->iA
36e5b 6c 69 61 73 20 3d 20 30 3b 0d 0a 20 20 20 20 20  lias = 0;..     
36e5c 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 2d   }..      if( p-
36e5d 3e 6e 53 65 6c 65 63 74 52 6f 77 3e 28 64 6f 75  >nSelectRow>(dou
36e5e 62 6c 65 29 31 30 30 20 29 20 70 2d 3e 6e 53 65  ble)100 ) p->nSe
36e5f 6c 65 63 74 52 6f 77 20 3d 20 28 64 6f 75 62 6c  lectRow = (doubl
36e60 65 29 31 30 30 3b 0d 0a 20 20 20 20 7d 65 6c 73  e)100;..    }els
36e61 65 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 6e 53 65  e{..      p->nSe
36e62 6c 65 63 74 52 6f 77 20 3d 20 28 64 6f 75 62 6c  lectRow = (doubl
36e63 65 29 31 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  e)1;..    }.... 
36e64 0d 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
36e65 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20  a label to jump 
36e66 74 6f 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20  to when we want 
36e67 74 6f 20 61 62 6f 72 74 20 74 68 65 20 71 75 65  to abort the que
36e68 72 79 20 2a 2f 0d 0a 20 20 20 20 61 64 64 72 45  ry */..    addrE
36e69 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nd = sqlite3Vdbe
36e6a 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 0d  MakeLabel(v);...
36e6b 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20  .    /* Convert 
36e6c 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20  TK_COLUMN nodes 
36e6d 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  into TK_AGG_COLU
36e6e 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72  MN and make entr
36e6f 69 65 73 20 69 6e 0d 0a 20 20 20 20 2a 2a 20 73  ies in..    ** s
36e70 41 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20  AggInfo for all 
36e71 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
36e72 6e 6f 64 65 73 20 69 6e 20 65 78 70 72 65 73 73  nodes in express
36e73 69 6f 6e 73 20 6f 66 20 74 68 65 0d 0a 20 20 20  ions of the..   
36e74 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65   ** SELECT state
36e75 6d 65 6e 74 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  ment...    */.. 
36e76 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20     memset(&sNC, 
36e77 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b  0, sizeof(sNC));
36e78 0d 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ..    sNC.pParse
36e79 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 20 20   = pParse;..    
36e7a 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
36e7b 54 61 62 4c 69 73 74 3b 0d 0a 20 20 20 20 73 4e  TabList;..    sN
36e7c 43 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26 73 41  C.pAggInfo = &sA
36e7d 67 67 49 6e 66 6f 3b 0d 0a 20 20 20 20 73 41 67  ggInfo;..    sAg
36e7e 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f  gInfo.nSortingCo
36e7f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79 20  lumn = pGroupBy 
36e80 3f 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  ? pGroupBy->nExp
36e81 72 2b 31 20 3a 20 30 3b 0d 0a 20 20 20 20 73 41  r+1 : 0;..    sA
36e82 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20  ggInfo.pGroupBy 
36e83 3d 20 70 47 72 6f 75 70 42 79 3b 0d 0a 20 20 20  = pGroupBy;..   
36e84 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
36e85 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c  yzeAggList(&sNC,
36e86 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 20 20 73   pEList);..    s
36e87 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
36e88 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70  eAggList(&sNC, p
36e89 4f 72 64 65 72 42 79 29 3b 0d 0a 20 20 20 20 69  OrderBy);..    i
36e8a 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0d 0a 20  f( pHaving ){.. 
36e8b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
36e8c 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
36e8d 73 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67 29  s(&sNC, pHaving)
36e8e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 41  ;..    }..    sA
36e8f 67 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61  ggInfo.nAccumula
36e90 74 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e  tor = sAggInfo.n
36e91 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 66 6f 72  Column;..    for
36e92 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f  (i=0; i<sAggInfo
36e93 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b 0d 0a 20  .nFunc; i++){.. 
36e94 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
36e95 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 73 41  prHasProperty(sA
36e96 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e  ggInfo.aFunc[i].
36e97 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
36e98 65 63 74 29 20 29 3b 0d 0a 20 20 20 20 20 20 73  ect) );..      s
36e99 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
36e9a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 73  eAggList(&sNC, s
36e9b 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d  AggInfo.aFunc[i]
36e9c 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29  .pExpr->x.pList)
36e9d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
36e9e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
36e9f 65 64 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  ed ) goto select
36ea0 5f 65 6e 64 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  _end;....    /* 
36ea1 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61  Processing for a
36ea2 67 67 72 65 67 61 74 65 73 20 77 69 74 68 20 47  ggregates with G
36ea3 52 4f 55 50 20 42 59 20 69 73 20 76 65 72 79 20  ROUP BY is very 
36ea4 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0d 0a 20  different and.. 
36ea5 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20     ** much more 
36ea6 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67 67  complex than agg
36ea7 72 65 67 61 74 65 73 20 77 69 74 68 6f 75 74 20  regates without 
36ea8 61 20 47 52 4f 55 50 20 42 59 2e 0d 0a 20 20 20  a GROUP BY...   
36ea9 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 47 72   */..    if( pGr
36eaa 6f 75 70 42 79 20 29 7b 0d 0a 20 20 20 20 20 20  oupBy ){..      
36eab 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
36eac 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e  o;  /* Keying in
36ead 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
36eae 65 20 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73  e group by claus
36eaf 65 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  e */..      int 
36eb0 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  j1;             
36eb1 2f 2a 20 41 2d 76 73 2d 42 20 63 6f 6d 70 61 72  /* A-vs-B compar
36eb2 69 73 69 6f 6e 20 6a 75 6d 70 20 2a 2f 0d 0a 20  ision jump */.. 
36eb3 20 20 20 20 20 69 6e 74 20 61 64 64 72 4f 75 74       int addrOut
36eb4 70 75 74 52 6f 77 3b 20 20 2f 2a 20 53 74 61 72  putRow;  /* Star
36eb5 74 20 6f 66 20 73 75 62 72 6f 75 74 69 6e 65 20  t of subroutine 
36eb6 74 68 61 74 20 6f 75 74 70 75 74 73 20 61 20 72  that outputs a r
36eb7 65 73 75 6c 74 20 72 6f 77 20 2a 2f 0d 0a 20 20  esult row */..  
36eb8 20 20 20 20 69 6e 74 20 72 65 67 4f 75 74 70 75      int regOutpu
36eb9 74 52 6f 77 3b 20 20 20 2f 2a 20 52 65 74 75 72  tRow;   /* Retur
36eba 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74  n address regist
36ebb 65 72 20 66 6f 72 20 6f 75 74 70 75 74 20 73 75  er for output su
36ebc 62 72 6f 75 74 69 6e 65 20 2a 2f 0d 0a 20 20 20  broutine */..   
36ebd 20 20 20 69 6e 74 20 61 64 64 72 53 65 74 41 62     int addrSetAb
36ebe 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74 20 74 68  ort;   /* Set th
36ebf 65 20 61 62 6f 72 74 20 66 6c 61 67 20 61 6e 64  e abort flag and
36ec0 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 20 20 20 20   return */..    
36ec1 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66 4c    int addrTopOfL
36ec2 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f 66 20  oop;  /* Top of 
36ec3 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20 2a  the input loop *
36ec4 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  /..      int add
36ec5 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a 20  rSortingIdx; /* 
36ec6 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  The OP_OpenEphem
36ec7 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 6f 72  eral for the sor
36ec8 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0d 0a 20  ting index */.. 
36ec9 20 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73       int addrRes
36eca 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 72  et;      /* Subr
36ecb 6f 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 74  outine for reset
36ecc 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c  ting the accumul
36ecd 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 20 20 69  ator */..      i
36ece 6e 74 20 72 65 67 52 65 73 65 74 3b 20 20 20 20  nt regReset;    
36ecf 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64     /* Return add
36ed0 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f  ress register fo
36ed1 72 20 72 65 73 65 74 20 73 75 62 72 6f 75 74 69  r reset subrouti
36ed2 6e 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 2f  ne */....      /
36ed3 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20  * If there is a 
36ed4 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
36ed5 77 65 20 6d 69 67 68 74 20 6e 65 65 64 20 61 20  we might need a 
36ed6 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 6f  sorting index to
36ed7 0d 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c 65  ..      ** imple
36ed8 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c 6f 63 61  ment it.  Alloca
36ed9 74 65 20 74 68 61 74 20 73 6f 72 74 69 6e 67 20  te that sorting 
36eda 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49 66 20 69  index now.  If i
36edb 74 20 74 75 72 6e 73 20 6f 75 74 0d 0a 20 20 20  t turns out..   
36edc 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f     ** that we do
36edd 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61 66 74   not need it aft
36ede 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 50 5f 53  er all, the OP_S
36edf 6f 72 74 65 72 4f 70 65 6e 20 69 6e 73 74 72 75  orterOpen instru
36ee0 63 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  ction..      ** 
36ee1 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65  will be converte
36ee2 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20 20  d into a Noop.  
36ee3 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
36ee4 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69    sAggInfo.sorti
36ee5 6e 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ngIdx = pParse->
36ee6 6e 54 61 62 2b 2b 3b 0d 0a 20 20 20 20 20 20 70  nTab++;..      p
36ee7 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
36ee8 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
36ee9 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b  arse, pGroupBy);
36eea 0d 0a 20 20 20 20 20 20 61 64 64 72 53 6f 72 74  ..      addrSort
36eeb 69 6e 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33  ingIdx = sqlite3
36eec 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
36eed 5f 53 6f 72 74 65 72 4f 70 65 6e 2c 20 0d 0a 20  _SorterOpen, .. 
36eee 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66           sAggInf
36eef 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 73 41  o.sortingIdx, sA
36ef0 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43  ggInfo.nSortingC
36ef1 6f 6c 75 6d 6e 2c 20 0d 0a 20 20 20 20 20 20 20  olumn, ..       
36ef2 20 20 20 30 2c 20 28 63 68 61 72 2a 29 70 4b 65     0, (char*)pKe
36ef3 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
36ef4 4f 5f 48 41 4e 44 4f 46 46 29 3b 0d 0a 0d 0a 20  O_HANDOFF);.... 
36ef5 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69       /* Initiali
36ef6 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  ze memory locati
36ef7 6f 6e 73 20 75 73 65 64 20 62 79 20 47 52 4f 55  ons used by GROU
36ef8 50 20 42 59 20 61 67 67 72 65 67 61 74 65 20 70  P BY aggregate p
36ef9 72 6f 63 65 73 73 69 6e 67 0d 0a 20 20 20 20 20  rocessing..     
36efa 20 2a 2f 0d 0a 20 20 20 20 20 20 69 55 73 65 46   */..      iUseF
36efb 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  lag = ++pParse->
36efc 6e 4d 65 6d 3b 0d 0a 20 20 20 20 20 20 69 41 62  nMem;..      iAb
36efd 6f 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72  ortFlag = ++pPar
36efe 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 20  se->nMem;..     
36eff 20 72 65 67 4f 75 74 70 75 74 52 6f 77 20 3d 20   regOutputRow = 
36f00 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d  ++pParse->nMem;.
36f01 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
36f02 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
36f03 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d  beMakeLabel(v);.
36f04 0a 20 20 20 20 20 20 72 65 67 52 65 73 65 74 20  .      regReset 
36f05 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
36f06 3b 0d 0a 20 20 20 20 20 20 61 64 64 72 52 65 73  ;..      addrRes
36f07 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  et = sqlite3Vdbe
36f08 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20  MakeLabel(v);.. 
36f09 20 20 20 20 20 69 41 4d 65 6d 20 3d 20 70 50 61       iAMem = pPa
36f0a 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0d 0a  rse->nMem + 1;..
36f0b 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
36f0c 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
36f0d 6e 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 69 42  nExpr;..      iB
36f0e 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
36f0f 65 6d 20 2b 20 31 3b 0d 0a 20 20 20 20 20 20 70  em + 1;..      p
36f10 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
36f11 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0d  GroupBy->nExpr;.
36f12 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
36f13 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
36f14 6e 74 65 67 65 72 2c 20 30 2c 20 69 41 62 6f 72  nteger, 0, iAbor
36f15 74 46 6c 61 67 29 3b 0d 0a 20 20 20 20 20 20 56  tFlag);..      V
36f16 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
36f17 63 6c 65 61 72 20 61 62 6f 72 74 20 66 6c 61 67  clear abort flag
36f18 22 29 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  "));..      sqli
36f19 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
36f1a 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
36f1b 69 55 73 65 46 6c 61 67 29 3b 0d 0a 20 20 20 20  iUseFlag);..    
36f1c 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
36f1d 2c 20 22 69 6e 64 69 63 61 74 65 20 61 63 63 75  , "indicate accu
36f1e 6d 75 6c 61 74 6f 72 20 65 6d 70 74 79 22 29 29  mulator empty"))
36f1f 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
36f20 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
36f21 5f 4e 75 6c 6c 2c 20 30 2c 20 69 41 4d 65 6d 2c  _Null, 0, iAMem,
36f22 20 69 41 4d 65 6d 2b 70 47 72 6f 75 70 42 79 2d   iAMem+pGroupBy-
36f23 3e 6e 45 78 70 72 2d 31 29 3b 0d 0a 0d 0a 20 20  >nExpr-1);....  
36f24 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c      /* Begin a l
36f25 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78  oop that will ex
36f26 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65  tract all source
36f27 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42   rows in GROUP B
36f28 59 20 6f 72 64 65 72 2e 0d 0a 20 20 20 20 20 20  Y order...      
36f29 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e  ** This might in
36f2a 76 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61  volve two separa
36f2b 74 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e  te loops with an
36f2c 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77   OP_Sort in betw
36f2d 65 65 6e 2c 20 6f 72 0d 0a 20 20 20 20 20 20 2a  een, or..      *
36f2e 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20  * it might be a 
36f2f 73 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74  single loop that
36f30 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74   uses an index t
36f31 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d  o extract inform
36f32 61 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  ation..      ** 
36f33 69 6e 20 74 68 65 20 72 69 67 68 74 20 6f 72 64  in the right ord
36f34 65 72 20 74 6f 20 62 65 67 69 6e 20 77 69 74 68  er to begin with
36f35 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
36f36 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
36f37 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
36f38 2c 20 72 65 67 52 65 73 65 74 2c 20 61 64 64 72  , regReset, addr
36f39 52 65 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 70  Reset);..      p
36f3a 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
36f3b 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
36f3c 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
36f3d 72 65 2c 20 26 70 47 72 6f 75 70 42 79 2c 20 30  re, &pGroupBy, 0
36f3e 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  , 0);..      if(
36f3f 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
36f40 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0d 0a 20  o select_end;.. 
36f41 20 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42       if( pGroupB
36f42 79 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  y==0 ){..       
36f43 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65   /* The optimize
36f44 72 20 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c  r is able to del
36f45 69 76 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f  iver rows in gro
36f46 75 70 20 62 79 20 6f 72 64 65 72 20 73 6f 0d 0a  up by order so..
36f47 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20 64 6f          ** we do
36f48 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73 6f 72   not have to sor
36f49 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45  t.  The OP_OpenE
36f4a 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77  phemeral table w
36f4b 69 6c 6c 20 62 65 0d 0a 20 20 20 20 20 20 20 20  ill be..        
36f4c 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74  ** cancelled lat
36f4d 65 72 20 62 65 63 61 75 73 65 20 77 65 20 73 74  er because we st
36f4e 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65 20  ill need to use 
36f4f 74 68 65 20 70 4b 65 79 49 6e 66 6f 0d 0a 20 20  the pKeyInfo..  
36f50 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
36f51 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
36f52 70 47 72 6f 75 70 42 79 3b 0d 0a 20 20 20 20 20  pGroupBy;..     
36f53 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d     groupBySort =
36f54 20 30 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65   0;..      }else
36f55 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  {..        /* Ro
36f56 77 73 20 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75  ws are coming ou
36f57 74 20 69 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65  t in undetermine
36f58 64 20 6f 72 64 65 72 2e 20 20 57 65 20 68 61 76  d order.  We hav
36f59 65 20 74 6f 20 70 75 73 68 0d 0a 20 20 20 20 20  e to push..     
36f5a 20 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 69     ** each row i
36f5b 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  nto a sorting in
36f5c 64 65 78 2c 20 74 65 72 6d 69 6e 61 74 65 20 74  dex, terminate t
36f5d 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 2c 0d 0a  he first loop,..
36f5e 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
36f5f 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 73 6f  loop over the so
36f60 72 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 6f  rting index in o
36f61 72 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20  rder to get the 
36f62 6f 75 74 70 75 74 0d 0a 20 20 20 20 20 20 20 20  output..        
36f63 2a 2a 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  ** in sorted ord
36f64 65 72 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a  er..        */..
36f65 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67 42          int regB
36f66 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e  ase;..        in
36f67 74 20 72 65 67 52 65 63 6f 72 64 3b 0d 0a 20 20  t regRecord;..  
36f68 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0d        int nCol;.
36f69 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 47 72  .        int nGr
36f6a 6f 75 70 42 79 3b 0d 0a 0d 0a 20 20 20 20 20 20  oupBy;....      
36f6b 20 20 65 78 70 6c 61 69 6e 54 65 6d 70 54 61 62    explainTempTab
36f6c 6c 65 28 70 50 61 72 73 65 2c 20 0d 0a 20 20 20  le(pParse, ..   
36f6d 20 20 20 20 20 20 20 20 20 69 73 44 69 73 74 69           isDisti
36f6e 6e 63 74 20 26 26 20 21 28 70 2d 3e 73 65 6c 46  nct && !(p->selF
36f6f 6c 61 67 73 26 53 46 5f 44 69 73 74 69 6e 63 74  lags&SF_Distinct
36f70 29 3f 22 44 49 53 54 49 4e 43 54 22 3a 22 47 52  )?"DISTINCT":"GR
36f71 4f 55 50 20 42 59 22 29 3b 0d 0a 0d 0a 20 20 20  OUP BY");....   
36f72 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74       groupBySort
36f73 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 6e   = 1;..        n
36f74 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70  GroupBy = pGroup
36f75 42 79 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20 20  By->nExpr;..    
36f76 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75      nCol = nGrou
36f77 70 42 79 20 2b 20 31 3b 0d 0a 20 20 20 20 20 20  pBy + 1;..      
36f78 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31    j = nGroupBy+1
36f79 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
36f7a 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
36f7b 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20  Column; i++){.. 
36f7c 20 20 20 20 20 20 20 20 20 69 66 28 20 73 41 67           if( sAg
36f7d 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53  gInfo.aCol[i].iS
36f7e 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29  orterColumn>=j )
36f7f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  {..            n
36f80 43 6f 6c 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20  Col++;..        
36f81 20 20 20 20 6a 2b 2b 3b 0d 0a 20 20 20 20 20 20      j++;..      
36f82 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
36f83 0d 0a 20 20 20 20 20 20 20 20 72 65 67 42 61 73  ..        regBas
36f84 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
36f85 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
36f86 6e 43 6f 6c 29 3b 0d 0a 20 20 20 20 20 20 20 20  nCol);..        
36f87 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
36f88 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0d 0a  Clear(pParse);..
36f89 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
36f8a 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
36f8b 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79  pParse, pGroupBy
36f8c 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0d 0a  , regBase, 0);..
36f8d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
36f8e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
36f8f 53 65 71 75 65 6e 63 65 2c 20 73 41 67 67 49 6e  Sequence, sAggIn
36f90 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 72 65  fo.sortingIdx,re
36f91 67 42 61 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b  gBase+nGroupBy);
36f92 0d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47  ..        j = nG
36f93 72 6f 75 70 42 79 2b 31 3b 0d 0a 20 20 20 20 20  roupBy+1;..     
36f94 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41     for(i=0; i<sA
36f95 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20  ggInfo.nColumn; 
36f96 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  i++){..         
36f97 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
36f98 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67  col *pCol = &sAg
36f99 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0d 0a  gInfo.aCol[i];..
36f9a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
36f9b 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
36f9c 6e 3e 3d 6a 20 29 7b 0d 0a 20 20 20 20 20 20 20  n>=j ){..       
36f9d 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 6a 20       int r1 = j 
36f9e 2b 20 72 65 67 42 61 73 65 3b 0d 0a 20 20 20 20  + regBase;..    
36f9f 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b 0d          int r2;.
36fa0 0a 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ...            r
36fa1 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
36fa2 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61  odeGetColumn(pPa
36fa3 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  rse, ..         
36fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fa5 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62        pCol->pTab
36fa6 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c  , pCol->iColumn,
36fa7 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20 72   pCol->iTable, r
36fa8 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  1);..           
36fa9 20 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0d 0a   if( r1!=r2 ){..
36faa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
36fab 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
36fac 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c  v, OP_SCopy, r2,
36fad 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 20   r1);..         
36fae 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
36faf 20 20 6a 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20    j++;..        
36fb0 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a    }..        }..
36fb1 20 20 20 20 20 20 20 20 72 65 67 52 65 63 6f 72          regRecor
36fb2 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
36fb3 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d 0a  mpReg(pParse);..
36fb4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
36fb5 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
36fb6 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
36fb7 61 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65  ase, nCol, regRe
36fb8 63 6f 72 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  cord);..        
36fb9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
36fba 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 49 6e  2(v, OP_SorterIn
36fbb 73 65 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73  sert, sAggInfo.s
36fbc 6f 72 74 69 6e 67 49 64 78 2c 20 72 65 67 52 65  ortingIdx, regRe
36fbd 63 6f 72 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  cord);..        
36fbe 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
36fbf 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
36fc0 67 52 65 63 6f 72 64 29 3b 0d 0a 20 20 20 20 20  gRecord);..     
36fc1 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
36fc2 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
36fc3 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c  e, regBase, nCol
36fc4 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
36fc5 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
36fc6 66 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 41  fo);..        sA
36fc7 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
36fc8 78 50 54 61 62 20 3d 20 73 6f 72 74 50 54 61 62  xPTab = sortPTab
36fc9 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
36fca 2b 3b 0d 0a 20 20 20 20 20 20 20 20 73 6f 72 74  +;..        sort
36fcb 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Out = sqlite3Get
36fcc 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
36fcd 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
36fce 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
36fcf 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 73 6f  P_OpenPseudo, so
36fd0 72 74 50 54 61 62 2c 20 73 6f 72 74 4f 75 74 2c  rtPTab, sortOut,
36fd1 20 6e 43 6f 6c 29 3b 0d 0a 20 20 20 20 20 20 20   nCol);..       
36fd2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36fd3 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53  p2(v, OP_SorterS
36fd4 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  ort, sAggInfo.so
36fd5 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45 6e  rtingIdx, addrEn
36fd6 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 56 64 62  d);..        Vdb
36fd7 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52  eComment((v, "GR
36fd8 4f 55 50 20 42 59 20 73 6f 72 74 22 29 29 3b 0d  OUP BY sort"));.
36fd9 0a 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66  .        sAggInf
36fda 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20  o.useSortingIdx 
36fdb 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  = 1;..        sq
36fdc 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
36fdd 65 61 72 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  ear(pParse);..  
36fde 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f      }....      /
36fdf 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 63  * Evaluate the c
36fe0 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
36fe1 74 65 72 6d 73 20 61 6e 64 20 73 74 6f 72 65 20  terms and store 
36fe2 69 6e 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e  in b0, b1, b2...
36fe3 0d 0a 20 20 20 20 20 20 2a 2a 20 28 62 30 20 69  ..      ** (b0 i
36fe4 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  s memory locatio
36fe5 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73  n iBMem+0, b1 is
36fe6 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f   iBMem+1, and so
36fe7 20 66 6f 72 74 68 29 0d 0a 20 20 20 20 20 20 2a   forth)..      *
36fe8 2a 20 54 68 65 6e 20 63 6f 6d 70 61 72 65 20 74  * Then compare t
36fe9 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  he current GROUP
36fea 20 42 59 20 74 65 72 6d 73 20 61 67 61 69 6e 73   BY terms agains
36feb 74 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 74  t the GROUP BY t
36fec 65 72 6d 73 0d 0a 20 20 20 20 20 20 2a 2a 20 66  erms..      ** f
36fed 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73  rom the previous
36fee 20 72 6f 77 20 63 75 72 72 65 6e 74 6c 79 20 73   row currently s
36fef 74 6f 72 65 64 20 69 6e 20 61 30 2c 20 61 31 2c  tored in a0, a1,
36ff0 20 61 32 2e 2e 2e 0d 0a 20 20 20 20 20 20 2a 2f   a2.....      */
36ff1 0d 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70 4f  ..      addrTopO
36ff2 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
36ff3 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
36ff4 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
36ff5 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
36ff6 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20  pParse);..      
36ff7 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20  if( groupBySort 
36ff8 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
36ff9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
36ffa 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 2c 20   OP_SorterData, 
36ffb 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
36ffc 49 64 78 2c 20 73 6f 72 74 4f 75 74 29 3b 0d 0a  Idx, sortOut);..
36ffd 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 66        }..      f
36ffe 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70  or(j=0; j<pGroup
36fff 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b  By->nExpr; j++){
37000 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 72  ..        if( gr
37001 6f 75 70 42 79 53 6f 72 74 20 29 7b 0d 0a 20 20  oupBySort ){..  
37002 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
37003 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
37004 43 6f 6c 75 6d 6e 2c 20 73 6f 72 74 50 54 61 62  Column, sortPTab
37005 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0d 0a  , j, iBMem+j);..
37006 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d            if( j=
37007 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  =0 ) sqlite3Vdbe
37008 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
37009 41 47 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0d  AG_CLEARCACHE);.
3700a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
3700b 0a 20 20 20 20 20 20 20 20 20 20 73 41 67 67 49  .          sAggI
3700c 6e 66 6f 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d  nfo.directMode =
3700d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   1;..          s
3700e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
3700f 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2d  Parse, pGroupBy-
37010 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 42 4d  >a[j].pExpr, iBM
37011 65 6d 2b 6a 29 3b 0d 0a 20 20 20 20 20 20 20 20  em+j);..        
37012 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
37013 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
37014 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72  Op4(v, OP_Compar
37015 65 2c 20 69 41 4d 65 6d 2c 20 69 42 4d 65 6d 2c  e, iAMem, iBMem,
37016 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
37017 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
37018 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
37019 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
3701a 5f 4b 45 59 49 4e 46 4f 29 3b 0d 0a 20 20 20 20  _KEYINFO);..    
3701b 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
3701c 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
3701d 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3701e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
3701f 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c 20  _Jump, j1+1, 0, 
37020 6a 31 2b 31 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  j1+1);....      
37021 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
37022 20 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 65   that runs whene
37023 76 65 72 20 74 68 65 20 47 52 4f 55 50 20 42 59  ver the GROUP BY
37024 20 63 68 61 6e 67 65 73 2e 0d 0a 20 20 20 20 20   changes...     
37025 20 2a 2a 20 43 68 61 6e 67 65 73 20 69 6e 20 74   ** Changes in t
37026 68 65 20 47 52 4f 55 50 20 42 59 20 61 72 65 20  he GROUP BY are 
37027 64 65 74 65 63 74 65 64 20 62 79 20 74 68 65 20  detected by the 
37028 70 72 65 76 69 6f 75 73 20 63 6f 64 65 0d 0a 20  previous code.. 
37029 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20       ** block.  
3702a 49 66 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f  If there were no
3702b 20 63 68 61 6e 67 65 73 2c 20 74 68 69 73 20 62   changes, this b
3702c 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70 65 64 2e  lock is skipped.
3702d 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20  ..      **..    
3702e 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 63    ** This code c
3702f 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20 67 72  opies current gr
37030 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69 6e 20  oup by terms in 
37031 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0d 0a 20 20  b0,b1,b2,.....  
37032 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61      ** over to a
37033 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68 65  0,a1,a2.  It the
37034 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74 70  n calls the outp
37035 75 74 20 73 75 62 72 6f 75 74 69 6e 65 0d 0a 20  ut subroutine.. 
37036 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65       ** and rese
37037 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65  ts the aggregate
37038 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67   accumulator reg
37039 69 73 74 65 72 73 20 69 6e 20 70 72 65 70 61 72  isters in prepar
3703a 61 74 69 6f 6e 0d 0a 20 20 20 20 20 20 2a 2a 20  ation..      ** 
3703b 66 6f 72 20 74 68 65 20 6e 65 78 74 20 47 52 4f  for the next GRO
3703c 55 50 20 42 59 20 62 61 74 63 68 2e 0d 0a 20 20  UP BY batch...  
3703d 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71      */..      sq
3703e 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
3703f 65 28 70 50 61 72 73 65 2c 20 69 42 4d 65 6d 2c  e(pParse, iBMem,
37040 20 69 41 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79   iAMem, pGroupBy
37041 2d 3e 6e 45 78 70 72 29 3b 0d 0a 20 20 20 20 20  ->nExpr);..     
37042 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
37043 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
37044 72 65 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64  regOutputRow, ad
37045 64 72 4f 75 74 70 75 74 52 6f 77 29 3b 0d 0a 20  drOutputRow);.. 
37046 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
37047 28 28 76 2c 20 22 6f 75 74 70 75 74 20 6f 6e 65  ((v, "output one
37048 20 72 6f 77 22 29 29 3b 0d 0a 20 20 20 20 20 20   row"));..      
37049 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3704a 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69  2(v, OP_IfPos, i
3704b 41 62 6f 72 74 46 6c 61 67 2c 20 61 64 64 72 45  AbortFlag, addrE
3704c 6e 64 29 3b 0d 0a 20 20 20 20 20 20 56 64 62 65  nd);..      Vdbe
3704d 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 68 65  Comment((v, "che
3704e 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ck abort flag"))
3704f 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
37050 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
37051 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74  _Gosub, regReset
37052 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0d 0a 20  , addrReset);.. 
37053 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
37054 28 28 76 2c 20 22 72 65 73 65 74 20 61 63 63 75  ((v, "reset accu
37055 6d 75 6c 61 74 6f 72 22 29 29 3b 0d 0a 0d 0a 20  mulator"));.... 
37056 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74       /* Update t
37057 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63  he aggregate acc
37058 75 6d 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20  umulators based 
37059 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  on the content o
3705a 66 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  f..      ** the 
3705b 63 75 72 72 65 6e 74 20 72 6f 77 0d 0a 20 20 20  current row..   
3705c 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c     */..      sql
3705d 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
3705e 28 76 2c 20 6a 31 29 3b 0d 0a 20 20 20 20 20 20  (v, j1);..      
3705f 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f  updateAccumulato
37060 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
37061 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  nfo);..      sql
37062 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
37063 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
37064 20 69 55 73 65 46 6c 61 67 29 3b 0d 0a 20 20 20   iUseFlag);..   
37065 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
37066 76 2c 20 22 69 6e 64 69 63 61 74 65 20 64 61 74  v, "indicate dat
37067 61 20 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72  a in accumulator
37068 22 29 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  "));....      /*
37069 20 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70   End of the loop
3706a 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
3706b 20 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72    if( groupBySor
3706c 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  t ){..        sq
3706d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3706e 76 2c 20 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74  v, OP_SorterNext
3706f 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69  , sAggInfo.sorti
37070 6e 67 49 64 78 2c 20 61 64 64 72 54 6f 70 4f 66  ngIdx, addrTopOf
37071 4c 6f 6f 70 29 3b 0d 0a 20 20 20 20 20 20 7d 65  Loop);..      }e
37072 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
37073 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
37074 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20  Info);..        
37075 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
37076 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53  eToNoop(v, addrS
37077 6f 72 74 69 6e 67 49 64 78 29 3b 0d 0a 20 20 20  ortingIdx);..   
37078 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
37079 20 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61   Output the fina
3707a 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0d  l row of result.
3707b 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
3707c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3707d 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
3707e 72 65 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64  regOutputRow, ad
3707f 64 72 4f 75 74 70 75 74 52 6f 77 29 3b 0d 0a 20  drOutputRow);.. 
37080 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
37081 28 28 76 2c 20 22 6f 75 74 70 75 74 20 66 69 6e  ((v, "output fin
37082 61 6c 20 72 6f 77 22 29 29 3b 0d 0a 0d 0a 20 20  al row"));....  
37083 20 20 20 20 2f 2a 20 4a 75 6d 70 20 6f 76 65 72      /* Jump over
37084 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 73   the subroutines
37085 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  ..      */..    
37086 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
37087 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
37088 30 2c 20 61 64 64 72 45 6e 64 29 3b 0d 0a 0d 0a  0, addrEnd);....
37089 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
3708a 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74  e a subroutine t
3708b 68 61 74 20 6f 75 74 70 75 74 73 20 61 20 73 69  hat outputs a si
3708c 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
3708d 72 65 73 75 6c 74 0d 0a 20 20 20 20 20 20 2a 2a  result..      **
3708e 20 73 65 74 2e 20 20 54 68 69 73 20 73 75 62 72   set.  This subr
3708f 6f 75 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f  outine first loo
37090 6b 73 20 61 74 20 74 68 65 20 69 55 73 65 46 6c  ks at the iUseFl
37091 61 67 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67  ag.  If iUseFlag
37092 0d 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6c 65  ..      ** is le
37093 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
37094 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 73 75   to zero, the su
37095 62 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f  broutine is a no
37096 2d 6f 70 2e 20 20 49 66 0d 0a 20 20 20 20 20 20  -op.  If..      
37097 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  ** the processin
37098 67 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20  g calls for the 
37099 71 75 65 72 79 20 74 6f 20 61 62 6f 72 74 2c 20  query to abort, 
3709a 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 0d  this subroutine.
3709b 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d  .      ** increm
3709c 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72 74 46  ents the iAbortF
3709d 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  lag memory locat
3709e 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72  ion before retur
3709f 6e 69 6e 67 20 69 6e 0d 0a 20 20 20 20 20 20 2a  ning in..      *
370a0 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e 61  * order to signa
370a1 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20  l the caller to 
370a2 61 62 6f 72 74 2e 0d 0a 20 20 20 20 20 20 2a 2f  abort...      */
370a3 0d 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41  ..      addrSetA
370a4 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  bort = sqlite3Vd
370a5 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
370a6 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
370a7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
370a8 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62  _Integer, 1, iAb
370a9 6f 72 74 46 6c 61 67 29 3b 0d 0a 20 20 20 20 20  ortFlag);..     
370aa 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
370ab 20 22 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67   "set abort flag
370ac 22 29 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  "));..      sqli
370ad 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
370ae 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f   OP_Return, regO
370af 75 74 70 75 74 52 6f 77 29 3b 0d 0a 20 20 20 20  utputRow);..    
370b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
370b1 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
370b2 72 4f 75 74 70 75 74 52 6f 77 29 3b 0d 0a 20 20  rOutputRow);..  
370b3 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f      addrOutputRo
370b4 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  w = sqlite3VdbeC
370b5 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0d 0a  urrentAddr(v);..
370b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
370b7 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
370b8 50 6f 73 2c 20 69 55 73 65 46 6c 61 67 2c 20 61  Pos, iUseFlag, a
370b9 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 32 29 3b  ddrOutputRow+2);
370ba 0d 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ..      VdbeComm
370bb 65 6e 74 28 28 76 2c 20 22 47 72 6f 75 70 62 79  ent((v, "Groupby
370bc 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f   result generato
370bd 72 20 65 6e 74 72 79 20 70 6f 69 6e 74 22 29 29  r entry point"))
370be 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
370bf 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
370c0 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
370c1 75 74 52 6f 77 29 3b 0d 0a 20 20 20 20 20 20 66  utRow);..      f
370c2 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69  inalizeAggFuncti
370c3 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41 67  ons(pParse, &sAg
370c4 67 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 73  gInfo);..      s
370c5 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
370c6 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e  e(pParse, pHavin
370c7 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
370c8 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  +1, SQLITE_JUMPI
370c9 46 4e 55 4c 4c 29 3b 0d 0a 20 20 20 20 20 20 73  FNULL);..      s
370ca 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
370cb 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
370cc 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65  ist, 0, 0, pOrde
370cd 72 42 79 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  rBy,..          
370ce 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74              dist
370cf 69 6e 63 74 2c 20 70 44 65 73 74 2c 0d 0a 20 20  inct, pDest,..  
370d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370d1 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f      addrOutputRo
370d2 77 2b 31 2c 20 61 64 64 72 53 65 74 41 62 6f 72  w+1, addrSetAbor
370d3 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  t);..      sqlit
370d4 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
370d5 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75  OP_Return, regOu
370d6 74 70 75 74 52 6f 77 29 3b 0d 0a 20 20 20 20 20  tputRow);..     
370d7 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
370d8 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72 65   "end groupby re
370d9 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22 29  sult generator")
370da 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 47  );....      /* G
370db 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
370dc 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72  tine that will r
370dd 65 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62  eset the group-b
370de 79 20 61 63 63 75 6d 75 6c 61 74 6f 72 0d 0a 20  y accumulator.. 
370df 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 73       */..      s
370e0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
370e1 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 52 65  eLabel(v, addrRe
370e2 73 65 74 29 3b 0d 0a 20 20 20 20 20 20 72 65 73  set);..      res
370e3 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  etAccumulator(pP
370e4 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
370e5 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
370e6 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
370e7 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65 73 65  _Return, regRese
370e8 74 29 3b 0d 0a 20 20 20 20 20 0d 0a 20 20 20 20  t);..     ..    
370e9 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47 72 6f 75  } /* endif pGrou
370ea 70 42 79 2e 20 20 42 65 67 69 6e 20 61 67 67 72  pBy.  Begin aggr
370eb 65 67 61 74 65 20 71 75 65 72 69 65 73 20 77 69  egate queries wi
370ec 74 68 6f 75 74 20 47 52 4f 55 50 20 42 59 3a 20  thout GROUP BY: 
370ed 2a 2f 0d 0a 20 20 20 20 65 6c 73 65 20 7b 0d 0a  */..    else {..
370ee 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
370ef 70 44 65 6c 20 3d 20 30 3b 0d 0a 23 69 66 6e 64  pDel = 0;..#ifnd
370f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
370f1 54 52 45 45 43 4f 55 4e 54 0d 0a 20 20 20 20 20  TREECOUNT..     
370f2 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a 20   Table *pTab;.. 
370f3 20 20 20 20 20 69 66 28 20 28 70 54 61 62 20 3d       if( (pTab =
370f4 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70   isSimpleCount(p
370f5 2c 20 26 73 41 67 67 49 6e 66 6f 29 29 21 3d 30  , &sAggInfo))!=0
370f6 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20   ){..        /* 
370f7 49 66 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74  If isSimpleCount
370f8 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  () returns a poi
370f9 6e 74 65 72 20 74 6f 20 61 20 54 61 62 6c 65 20  nter to a Table 
370fa 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 0d  structure, then.
370fb 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
370fc 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73  SQL statement is
370fd 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0d 0a 20   of the form:.. 
370fe 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20         **..     
370ff 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 63     **   SELECT c
37100 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62  ount(*) FROM <tb
37101 6c 3e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  l>..        **..
37102 20 20 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65          ** where
37103 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
37104 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 72 65  ture returned re
37105 70 72 65 73 65 6e 74 73 20 74 61 62 6c 65 20 3c  presents table <
37106 74 62 6c 3e 2e 0d 0a 20 20 20 20 20 20 20 20 2a  tbl>...        *
37107 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68  *..        ** Th
37108 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  is statement is 
37109 73 6f 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 69  so common that i
3710a 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 73  t is optimized s
3710b 70 65 63 69 61 6c 6c 79 2e 20 54 68 65 0d 0a 20  pecially. The.. 
3710c 20 20 20 20 20 20 20 2a 2a 20 4f 50 5f 43 6f 75         ** OP_Cou
3710d 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  nt instruction i
3710e 73 20 65 78 65 63 75 74 65 64 20 65 69 74 68 65  s executed eithe
3710f 72 20 6f 6e 20 74 68 65 20 69 6e 74 6b 65 79 20  r on the intkey 
37110 74 61 62 6c 65 20 74 68 61 74 0d 0a 20 20 20 20  table that..    
37111 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20      ** contains 
37112 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 61 62  the data for tab
37113 6c 65 20 3c 74 62 6c 3e 20 6f 72 20 6f 6e 20 6f  le <tbl> or on o
37114 6e 65 20 6f 66 20 69 74 73 20 69 6e 64 65 78 65  ne of its indexe
37115 73 2e 20 49 74 0d 0a 20 20 20 20 20 20 20 20 2a  s. It..        *
37116 2a 20 69 73 20 62 65 74 74 65 72 20 74 6f 20 65  * is better to e
37117 78 65 63 75 74 65 20 74 68 65 20 6f 70 20 6f 6e  xecute the op on
37118 20 61 6e 20 69 6e 64 65 78 2c 20 61 73 20 69 6e   an index, as in
37119 64 65 78 65 73 20 61 72 65 20 61 6c 6d 6f 73 74  dexes are almost
3711a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77  ..        ** alw
3711b 61 79 73 20 73 70 72 65 61 64 20 61 63 72 6f 73  ays spread acros
3711c 73 20 6c 65 73 73 20 70 61 67 65 73 20 74 68 61  s less pages tha
3711d 6e 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f  n their correspo
3711e 6e 64 69 6e 67 20 74 61 62 6c 65 73 2e 0d 0a 20  nding tables... 
3711f 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
37120 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 62     const int iDb
37121 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
37122 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
37123 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
37124 61 29 3b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e  a);..        con
37125 73 74 20 69 6e 74 20 69 43 73 72 20 3d 20 70 50  st int iCsr = pP
37126 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20  arse->nTab++;   
37127 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 73    /* Cursor to s
37128 63 61 6e 20 62 2d 74 72 65 65 20 2a 2f 0d 0a 20  can b-tree */.. 
37129 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
3712a 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
3712b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
3712c 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
3712d 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 4b 65 79   */..        Key
3712e 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d  Info *pKeyInfo =
3712f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
37130 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72    /* Keyinfo for
37131 20 73 63 61 6e 6e 65 64 20 69 6e 64 65 78 20 2a   scanned index *
37132 2f 0d 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78  /..        Index
37133 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 20 20 20   *pBest = 0;    
37134 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37135 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20 66 6f  /* Best index fo
37136 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20  und so far */.. 
37137 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6f 74         int iRoot
37138 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20 20   = pTab->tnum;  
37139 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
3713a 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63 61 6e  oot page of scan
3713b 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0d 0a 0d  ned b-tree */...
3713c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3713d 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
3713e 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a  (pParse, iDb);..
3713f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 54          sqlite3T
37140 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
37141 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
37142 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
37143 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a  );....        /*
37144 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20   Search for the 
37145 69 6e 64 65 78 20 74 68 61 74 20 68 61 73 20 74  index that has t
37146 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20  he least amount 
37147 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0d 0a  of columns. If..
37148 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65          ** there
37149 20 69 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65   is such an inde
3714a 78 2c 20 61 6e 64 20 69 74 20 68 61 73 20 6c 65  x, and it has le
3714b 73 73 20 63 6f 6c 75 6d 6e 73 20 74 68 61 6e 20  ss columns than 
3714c 74 68 65 20 74 61 62 6c 65 0d 0a 20 20 20 20 20  the table..     
3714d 20 20 20 2a 2a 20 64 6f 65 73 2c 20 74 68 65 6e     ** does, then
3714e 20 77 65 20 63 61 6e 20 61 73 73 75 6d 65 20 74   we can assume t
3714f 68 61 74 20 69 74 20 63 6f 6e 73 75 6d 65 73 20  hat it consumes 
37150 6c 65 73 73 20 73 70 61 63 65 20 6f 6e 20 64 69  less space on di
37151 73 6b 20 61 6e 64 0d 0a 20 20 20 20 20 20 20 20  sk and..        
37152 2a 2a 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72  ** will therefor
37153 65 20 62 65 20 63 68 65 61 70 65 72 20 74 6f 20  e be cheaper to 
37154 73 63 61 6e 20 74 6f 20 64 65 74 65 72 6d 69 6e  scan to determin
37155 65 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  e the query resu
37156 6c 74 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  lt...        ** 
37157 49 6e 20 74 68 69 73 20 63 61 73 65 20 73 65 74  In this case set
37158 20 69 52 6f 6f 74 20 74 6f 20 74 68 65 20 72 6f   iRoot to the ro
37159 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
3715a 66 20 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72  f the index b-tr
3715b 65 65 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ee..        ** a
3715c 6e 64 20 70 4b 65 79 49 6e 66 6f 20 74 6f 20 74  nd pKeyInfo to t
3715d 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
3715e 74 75 72 65 20 72 65 71 75 69 72 65 64 20 74 6f  ture required to
3715f 20 6e 61 76 69 67 61 74 65 20 74 68 65 0d 0a 20   navigate the.. 
37160 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e         ** index.
37161 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ..        **..  
37162 20 20 20 20 20 20 2a 2a 20 28 32 30 31 31 2d 30        ** (2011-0
37163 34 2d 31 35 29 20 44 6f 20 6e 6f 74 20 64 6f 20  4-15) Do not do 
37164 61 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61  a full scan of a
37165 6e 20 75 6e 6f 72 64 65 72 65 64 20 69 6e 64 65  n unordered inde
37166 78 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  x...        **..
37167 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 70 72          ** In pr
37168 61 63 74 69 63 65 20 74 68 65 20 4b 65 79 49 6e  actice the KeyIn
37169 66 6f 20 73 74 72 75 63 74 75 72 65 20 77 69 6c  fo structure wil
3716a 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 49  l not be used. I
3716b 74 20 69 73 20 6f 6e 6c 79 20 0d 0a 20 20 20 20  t is only ..    
3716c 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 74 6f      ** passed to
3716d 20 6b 65 65 70 20 4f 50 5f 4f 70 65 6e 52 65 61   keep OP_OpenRea
3716e 64 20 68 61 70 70 79 2e 0d 0a 20 20 20 20 20 20  d happy...      
3716f 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 66 6f    */..        fo
37170 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
37171 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
37172 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20  pIdx->pNext){.. 
37173 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
37174 78 2d 3e 62 55 6e 6f 72 64 65 72 65 64 3d 3d 30  x->bUnordered==0
37175 20 26 26 20 28 21 70 42 65 73 74 20 7c 7c 20 70   && (!pBest || p
37176 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65  Idx->nColumn<pBe
37177 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 7b 0d  st->nColumn) ){.
37178 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 42 65  .            pBe
37179 73 74 20 3d 20 70 49 64 78 3b 0d 0a 20 20 20 20  st = pIdx;..    
3717a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
3717b 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
3717c 70 42 65 73 74 20 26 26 20 70 42 65 73 74 2d 3e  pBest && pBest->
3717d 6e 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43  nColumn<pTab->nC
3717e 6f 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  ol ){..         
3717f 20 69 52 6f 6f 74 20 3d 20 70 42 65 73 74 2d 3e   iRoot = pBest->
37180 74 6e 75 6d 3b 0d 0a 20 20 20 20 20 20 20 20 20  tnum;..         
37181 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69   pKeyInfo = sqli
37182 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
37183 70 50 61 72 73 65 2c 20 70 42 65 73 74 29 3b 0d  pParse, pBest);.
37184 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  .        }....  
37185 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20        /* Open a 
37186 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72  read-only cursor
37187 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 4f 50  , execute the OP
37188 5f 43 6f 75 6e 74 2c 20 63 6c 6f 73 65 20 74 68  _Count, close th
37189 65 20 63 75 72 73 6f 72 2e 20 2a 2f 0d 0a 20 20  e cursor. */..  
3718a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3718b 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
3718c 65 6e 52 65 61 64 2c 20 69 43 73 72 2c 20 69 52  enRead, iCsr, iR
3718d 6f 6f 74 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20  oot, iDb);..    
3718e 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f      if( pKeyInfo
3718f 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
37190 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
37191 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20  P4(v, -1, (char 
37192 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b  *)pKeyInfo, P4_K
37193 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
37194 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
37195 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
37196 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75  AddOp2(v, OP_Cou
37197 6e 74 2c 20 69 43 73 72 2c 20 73 41 67 67 49 6e  nt, iCsr, sAggIn
37198 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d 65 6d  fo.aFunc[0].iMem
37199 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  );..        sqli
3719a 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
3719b 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72 29   OP_Close, iCsr)
3719c 3b 0d 0a 20 20 20 20 20 20 20 20 65 78 70 6c 61  ;..        expla
3719d 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70 50  inSimpleCount(pP
3719e 61 72 73 65 2c 20 70 54 61 62 2c 20 70 42 65 73  arse, pTab, pBes
3719f 74 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  t);..      }else
371a0 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
371a1 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55  TE_OMIT_BTREECOU
371a2 4e 54 20 2a 2f 0d 0a 20 20 20 20 20 20 7b 0d 0a  NT */..      {..
371a3 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
371a4 20 69 66 20 74 68 65 20 71 75 65 72 79 20 69 73   if the query is
371a5 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   of one of the f
371a6 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0d  ollowing forms:.
371a7 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20  .        **..   
371a8 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54       **   SELECT
371a9 20 6d 69 6e 28 78 29 20 46 52 4f 4d 20 2e 2e 2e   min(x) FROM ...
371aa 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53  ..        **   S
371ab 45 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52 4f  ELECT max(x) FRO
371ac 4d 20 2e 2e 2e 0d 0a 20 20 20 20 20 20 20 20 2a  M .....        *
371ad 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66  *..        ** If
371ae 20 69 74 20 69 73 2c 20 74 68 65 6e 20 61 73 6b   it is, then ask
371af 20 74 68 65 20 63 6f 64 65 20 69 6e 20 77 68 65   the code in whe
371b0 72 65 2e 63 20 74 6f 20 61 74 74 65 6d 70 74 20  re.c to attempt 
371b1 74 6f 20 73 6f 72 74 20 72 65 73 75 6c 74 73 0d  to sort results.
371b2 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 69  .        ** as i
371b3 66 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 22  f there was an "
371b4 4f 52 44 45 52 20 4f 4e 20 78 22 20 6f 72 20 22  ORDER ON x" or "
371b5 4f 52 44 45 52 20 4f 4e 20 78 20 44 45 53 43 22  ORDER ON x DESC"
371b6 20 63 6c 61 75 73 65 2e 20 0d 0a 20 20 20 20 20   clause. ..     
371b7 20 20 20 2a 2a 20 49 66 20 77 68 65 72 65 2e 63     ** If where.c
371b8 20 69 73 20 61 62 6c 65 20 74 6f 20 70 72 6f 64   is able to prod
371b9 75 63 65 20 72 65 73 75 6c 74 73 20 73 6f 72 74  uce results sort
371ba 65 64 20 69 6e 20 74 68 69 73 20 6f 72 64 65 72  ed in this order
371bb 2c 20 74 68 65 6e 0d 0a 20 20 20 20 20 20 20 20  , then..        
371bc 2a 2a 20 61 64 64 20 76 64 62 65 20 63 6f 64 65  ** add vdbe code
371bd 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
371be 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
371bf 6c 6f 6f 70 20 61 66 74 65 72 20 74 68 65 20 0d  loop after the .
371c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 72 73  .        ** firs
371c1 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69 6e  t iteration (sin
371c2 63 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65  ce the first ite
371c3 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f  ration of the lo
371c4 6f 70 20 69 73 20 0d 0a 20 20 20 20 20 20 20 20  op is ..        
371c5 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ** guaranteed to
371c6 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65 20   operate on the 
371c7 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69 6e  row with the min
371c8 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20  imum or maximum 
371c9 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c  ..        ** val
371ca 75 65 20 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c  ue of x, the onl
371cb 79 20 72 6f 77 20 72 65 71 75 69 72 65 64 29 2e  y row required).
371cc 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ..        **..  
371cd 20 20 20 20 20 20 2a 2a 20 41 20 73 70 65 63 69        ** A speci
371ce 61 6c 20 66 6c 61 67 20 6d 75 73 74 20 62 65 20  al flag must be 
371cf 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
371d0 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 74 6f  3WhereBegin() to
371d1 20 73 6c 69 67 68 74 6c 79 0d 0a 20 20 20 20 20   slightly..     
371d2 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68     ** modify beh
371d3 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77  aviour as follow
371d4 73 3a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  s:..        **..
371d5 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 49          **   + I
371d6 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20 61  f the query is a
371d7 20 22 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 22   "SELECT min(x)"
371d8 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20  , then the loop 
371d9 63 6f 64 65 64 20 62 79 0d 0a 20 20 20 20 20 20  coded by..      
371da 20 20 2a 2a 20 20 20 20 20 77 68 65 72 65 2e 63    **     where.c
371db 20 73 68 6f 75 6c 64 20 6e 6f 74 20 69 74 65 72   should not iter
371dc 61 74 65 20 6f 76 65 72 20 61 6e 79 20 76 61 6c  ate over any val
371dd 75 65 73 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  ues with a NULL 
371de 76 61 6c 75 65 0d 0a 20 20 20 20 20 20 20 20 2a  value..        *
371df 2a 20 20 20 20 20 66 6f 72 20 78 2e 0d 0a 20 20  *     for x...  
371e0 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
371e1 20 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f 70 74    **   + The opt
371e2 69 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e 20 77  imizer code in w
371e3 68 65 72 65 2e 63 20 28 74 68 65 20 74 68 69 6e  here.c (the thin
371e4 67 20 74 68 61 74 20 64 65 63 69 64 65 73 20 77  g that decides w
371e5 68 69 63 68 0d 0a 20 20 20 20 20 20 20 20 2a 2a  hich..        **
371e6 20 20 20 20 20 69 6e 64 65 78 20 6f 72 20 69 6e       index or in
371e7 64 69 63 65 73 20 74 6f 20 75 73 65 29 20 73 68  dices to use) sh
371e8 6f 75 6c 64 20 70 6c 61 63 65 20 61 20 64 69 66  ould place a dif
371e9 66 65 72 65 6e 74 20 70 72 69 6f 72 69 74 79 20  ferent priority 
371ea 6f 6e 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  on ..        ** 
371eb 20 20 20 20 73 61 74 69 73 66 79 69 6e 67 20 74      satisfying t
371ec 68 65 20 27 4f 52 44 45 52 20 42 59 27 20 63 6c  he 'ORDER BY' cl
371ed 61 75 73 65 20 74 68 61 6e 20 69 74 20 64 6f 65  ause than it doe
371ee 73 20 69 6e 20 6f 74 68 65 72 20 63 61 73 65 73  s in other cases
371ef 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  ...        **   
371f0 20 20 52 65 66 65 72 20 74 6f 20 63 6f 64 65 20    Refer to code 
371f1 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  and comments in 
371f2 77 68 65 72 65 2e 63 20 66 6f 72 20 64 65 74 61  where.c for deta
371f3 69 6c 73 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f  ils...        */
371f4 0d 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69  ..        ExprLi
371f5 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30 3b  st *pMinMax = 0;
371f6 0d 0a 20 20 20 20 20 20 20 20 75 38 20 66 6c 61  ..        u8 fla
371f7 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 28  g = minMaxQuery(
371f8 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  p);..        if(
371f9 20 66 6c 61 67 20 29 7b 0d 0a 20 20 20 20 20 20   flag ){..      
371fa 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
371fb 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e  rHasProperty(p->
371fc 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
371fd 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
371fe 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) );..          
371ff 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65  pMinMax = sqlite
37200 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
37201 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d   p->pEList->a[0]
37202 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c  .pExpr->x.pList,
37203 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  0);..          p
37204 44 65 6c 20 3d 20 70 4d 69 6e 4d 61 78 3b 0d 0a  Del = pMinMax;..
37205 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4d            if( pM
37206 69 6e 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d 61  inMax && !db->ma
37207 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20  llocFailed ){.. 
37208 20 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d             pMinM
37209 61 78 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  ax->a[0].sortOrd
3720a 65 72 20 3d 20 66 6c 61 67 21 3d 57 48 45 52 45  er = flag!=WHERE
3720b 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 3f 31 3a  _ORDERBY_MIN ?1:
3720c 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  0;..            
3720d 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45  pMinMax->a[0].pE
3720e 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c  xpr->op = TK_COL
3720f 55 4d 4e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  UMN;..          
37210 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  }..        }..  
37211 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  ..        /* Thi
37212 73 20 63 61 73 65 20 72 75 6e 73 20 69 66 20 74  s case runs if t
37213 68 65 20 61 67 67 72 65 67 61 74 65 20 68 61 73  he aggregate has
37214 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61   no GROUP BY cla
37215 75 73 65 2e 20 20 54 68 65 0d 0a 20 20 20 20 20  use.  The..     
37216 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67     ** processing
37217 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 65 72   is much simpler
37218 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20   since there is 
37219 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f  only a single ro
3721a 77 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  w..        ** of
3721b 20 6f 75 74 70 75 74 2e 0d 0a 20 20 20 20 20 20   output...      
3721c 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 65    */..        re
3721d 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70  setAccumulator(p
3721e 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
3721f 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 57 49 6e  );..        pWIn
37220 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
37221 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
37222 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c  TabList, pWhere,
37223 20 26 70 4d 69 6e 4d 61 78 2c 20 30 2c 20 66 6c   &pMinMax, 0, fl
37224 61 67 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  ag);..        if
37225 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b 0d 0a  ( pWInfo==0 ){..
37226 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
37227 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
37228 64 62 2c 20 70 44 65 6c 29 3b 0d 0a 20 20 20 20  db, pDel);..    
37229 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63        goto selec
3722a 74 5f 65 6e 64 3b 0d 0a 20 20 20 20 20 20 20 20  t_end;..        
3722b 7d 0d 0a 20 20 20 20 20 20 20 20 75 70 64 61 74  }..        updat
3722c 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61  eAccumulator(pPa
3722d 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b  rse, &sAggInfo);
3722e 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ..        if( !p
3722f 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61 67 20 29  MinMax && flag )
37230 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
37231 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
37232 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 57  , OP_Goto, 0, pW
37233 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0d 0a  Info->iBreak);..
37234 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43 6f            VdbeCo
37235 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 28 29 20  mment((v, "%s() 
37236 62 79 20 69 6e 64 65 78 22 2c 0d 0a 20 20 20 20  by index",..    
37237 20 20 20 20 20 20 20 20 20 20 20 20 28 66 6c 61              (fla
37238 67 3d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59  g==WHERE_ORDERBY
37239 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22 6d 61 78 22  _MIN?"min":"max"
3723a 29 29 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  )));..        }.
3723b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3723c 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
3723d 3b 0d 0a 20 20 20 20 20 20 20 20 66 69 6e 61 6c  ;..        final
3723e 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
3723f 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
37240 6f 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a  o);..      }....
37241 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
37242 20 30 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   0;..      sqlit
37243 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
37244 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61  arse, pHaving, a
37245 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45 5f 4a  ddrEnd, SQLITE_J
37246 55 4d 50 49 46 4e 55 4c 4c 29 3b 0d 0a 20 20 20  UMPIFNULL);..   
37247 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
37248 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
37249 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30  >pEList, 0, 0, 0
3724a 2c 20 2d 31 2c 20 0d 0a 20 20 20 20 20 20 20 20  , -1, ..        
3724b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
3724c 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64  est, addrEnd, ad
3724d 64 72 45 6e 64 29 3b 0d 0a 20 20 20 20 20 20 73  drEnd);..      s
3724e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
3724f 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0d  lete(db, pDel);.
37250 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
37251 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
37252 62 65 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b  bel(v, addrEnd);
37253 0d 0a 20 20 20 20 0d 0a 20 20 7d 20 2f 2a 20 65  ..    ..  } /* e
37254 6e 64 69 66 20 61 67 67 72 65 67 61 74 65 20 71  ndif aggregate q
37255 75 65 72 79 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28  uery */....  if(
37256 20 64 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0d   distinct>=0 ){.
37257 0a 20 20 20 20 65 78 70 6c 61 69 6e 54 65 6d 70  .    explainTemp
37258 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 22 44  Table(pParse, "D
37259 49 53 54 49 4e 43 54 22 29 3b 0d 0a 20 20 7d 0d  ISTINCT");..  }.
3725a 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ...  /* If there
3725b 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
3725c 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77 65 20  clause, then we 
3725d 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74 68 65  need to sort the
3725e 20 72 65 73 75 6c 74 73 0d 0a 20 20 2a 2a 20 61   results..  ** a
3725f 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f 20  nd send them to 
37260 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 65  the callback one
37261 20 62 79 20 6f 6e 65 2e 0d 0a 20 20 2a 2f 0d 0a   by one...  */..
37262 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
37263 7b 0d 0a 20 20 20 20 65 78 70 6c 61 69 6e 54 65  {..    explainTe
37264 6d 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  mpTable(pParse, 
37265 22 4f 52 44 45 52 20 42 59 22 29 3b 0d 0a 20 20  "ORDER BY");..  
37266 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61    generateSortTa
37267 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 76 2c  il(pParse, p, v,
37268 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20   pEList->nExpr, 
37269 70 44 65 73 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  pDest);..  }....
3726a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
3726b 6f 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72  o skip this quer
3726c 79 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  y..  */..  sqlit
3726d 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
3726e 65 6c 28 76 2c 20 69 45 6e 64 29 3b 0d 0a 0d 0a  el(v, iEnd);....
3726f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
37270 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
37271 20 63 6f 64 65 64 2e 20 20 20 53 65 74 20 74 68   coded.   Set th
37272 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f  e return code to
37273 20 30 0d 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69   0..  ** to indi
37274 63 61 74 65 20 6e 6f 20 65 72 72 6f 72 73 2e 0d  cate no errors..
37275 0a 20 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 30 3b  .  */..  rc = 0;
37276 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f 6c  ....  /* Control
37277 20 6a 75 6d 70 73 20 74 6f 20 68 65 72 65 20 69   jumps to here i
37278 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e  f an error is en
37279 63 6f 75 6e 74 65 72 65 64 20 61 62 6f 76 65 2c  countered above,
3727a 20 6f 72 20 75 70 6f 6e 0d 0a 20 20 2a 2a 20 73   or upon..  ** s
3727b 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69 6e 67  uccessful coding
3727c 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0d   of the SELECT..
3727d 0a 20 20 2a 2f 0d 0a 73 65 6c 65 63 74 5f 65 6e  .  */..select_en
3727e 64 3a 0d 0a 20 20 65 78 70 6c 61 69 6e 53 65 74  d:..  explainSet
3727f 49 6e 74 65 67 65 72 28 70 50 61 72 73 65 2d 3e  Integer(pParse->
37280 69 53 65 6c 65 63 74 49 64 2c 20 69 52 65 73 74  iSelectId, iRest
37281 6f 72 65 53 65 6c 65 63 74 49 64 29 3b 0d 0a 0d  oreSelectId);...
37282 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 63  .  /* Identify c
37283 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20 72  olumn names if r
37284 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45  esults of the SE
37285 4c 45 43 54 20 61 72 65 20 74 6f 20 62 65 20 6f  LECT are to be o
37286 75 74 70 75 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  utput...  */..  
37287 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37288 4b 20 26 26 20 70 44 65 73 74 2d 3e 65 44 65 73  K && pDest->eDes
37289 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b  t==SRT_Output ){
3728a 0d 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f  ..    generateCo
3728b 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
3728c 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69  , pTabList, pELi
3728d 73 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73  st);..  }....  s
3728e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3728f 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b   sAggInfo.aCol);
37290 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ..  sqlite3DbFre
37291 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61  e(db, sAggInfo.a
37292 46 75 6e 63 29 3b 0d 0a 20 20 72 65 74 75 72 6e  Func);..  return
37293 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20 64   rc;..}....#if d
37294 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
37295 41 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49  ABLE_TREE_EXPLAI
37296 4e 29 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  N)../*..** Gener
37297 61 74 65 20 61 20 68 75 6d 61 6e 2d 72 65 61 64  ate a human-read
37298 61 62 6c 65 20 64 65 73 63 72 69 70 74 69 6f 6e  able description
37299 20 6f 66 20 61 20 74 68 65 20 53 65 6c 65 63 74   of a the Select
3729a 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73 74   object...*/..st
3729b 61 74 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69  atic void explai
3729c 6e 4f 6e 65 53 65 6c 65 63 74 28 56 64 62 65 20  nOneSelect(Vdbe 
3729d 2a 70 56 64 62 65 2c 20 53 65 6c 65 63 74 20 2a  *pVdbe, Select *
3729e 70 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78  p){..  sqlite3Ex
3729f 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62  plainPrintf(pVdb
372a0 65 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0d 0a  e, "SELECT ");..
372a1 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
372a2 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
372a3 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29  |SF_Aggregate) )
372a4 7b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 65  {..    if( p->se
372a5 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
372a6 69 6e 63 74 20 29 7b 0d 0a 20 20 20 20 20 20 73  inct ){..      s
372a7 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69  qlite3ExplainPri
372a8 6e 74 66 28 70 56 64 62 65 2c 20 22 44 49 53 54  ntf(pVdbe, "DIST
372a9 49 4e 43 54 20 22 29 3b 0d 0a 20 20 20 20 7d 0d  INCT ");..    }.
372aa 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 65 6c 46  .    if( p->selF
372ab 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67  lags & SF_Aggreg
372ac 61 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  ate ){..      sq
372ad 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
372ae 74 66 28 70 56 64 62 65 2c 20 22 61 67 67 5f 66  tf(pVdbe, "agg_f
372af 6c 61 67 20 22 29 3b 0d 0a 20 20 20 20 7d 0d 0a  lag ");..    }..
372b0 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
372b1 69 6e 4e 4c 28 70 56 64 62 65 29 3b 0d 0a 20 20  inNL(pVdbe);..  
372b2 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
372b3 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 20  Printf(pVdbe, " 
372b4 20 20 22 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71    ");..  }..  sq
372b5 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
372b6 4c 69 73 74 28 70 56 64 62 65 2c 20 70 2d 3e 70  List(pVdbe, p->p
372b7 45 4c 69 73 74 29 3b 0d 0a 20 20 73 71 6c 69 74  EList);..  sqlit
372b8 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62  e3ExplainNL(pVdb
372b9 65 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 70 53  e);..  if( p->pS
372ba 72 63 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 6e  rc && p->pSrc->n
372bb 53 72 63 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  Src ){..    int 
372bc 69 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  i;..    sqlite3E
372bd 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64  xplainPrintf(pVd
372be 62 65 2c 20 22 46 52 4f 4d 20 22 29 3b 0d 0a 20  be, "FROM ");.. 
372bf 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
372c0 6e 50 75 73 68 28 70 56 64 62 65 29 3b 0d 0a 20  nPush(pVdbe);.. 
372c1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
372c2 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b  >pSrc->nSrc; i++
372c3 29 7b 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74  ){..      struct
372c4 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
372c5 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d  Item = &p->pSrc-
372c6 3e 61 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 73 71  >a[i];..      sq
372c7 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
372c8 74 66 28 70 56 64 62 65 2c 20 22 7b 25 64 2c 2a  tf(pVdbe, "{%d,*
372c9 7d 20 3d 20 22 2c 20 70 49 74 65 6d 2d 3e 69 43  } = ", pItem->iC
372ca 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20 20 20 69  ursor);..      i
372cb 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
372cc 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  t ){..        sq
372cd 6c 69 74 65 33 45 78 70 6c 61 69 6e 53 65 6c 65  lite3ExplainSele
372ce 63 74 28 70 56 64 62 65 2c 20 70 49 74 65 6d 2d  ct(pVdbe, pItem-
372cf 3e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20 20  >pSelect);..    
372d0 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
372d1 54 61 62 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Tab ){..        
372d2 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
372d3 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 20  Printf(pVdbe, " 
372d4 28 74 61 62 6e 61 6d 65 3d 25 73 29 22 2c 20 70  (tabname=%s)", p
372d5 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  Item->pTab->zNam
372d6 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  e);..        }..
372d7 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
372d8 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0d  pItem->zName ){.
372d9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
372da 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
372db 64 62 65 2c 20 22 25 73 22 2c 20 70 49 74 65 6d  dbe, "%s", pItem
372dc 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20  ->zName);..     
372dd 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49   }..      if( pI
372de 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0d 0a  tem->zAlias ){..
372df 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
372e0 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64  xplainPrintf(pVd
372e1 62 65 2c 20 22 20 28 41 53 20 25 73 29 22 2c 20  be, " (AS %s)", 
372e2 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0d  pItem->zAlias);.
372e3 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
372e4 69 66 28 20 70 49 74 65 6d 2d 3e 6a 6f 69 6e 74  if( pItem->joint
372e5 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 20 29 7b  ype & JT_LEFT ){
372e6 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
372e7 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70  3ExplainPrintf(p
372e8 56 64 62 65 2c 20 22 20 4c 45 46 54 2d 4a 4f 49  Vdbe, " LEFT-JOI
372e9 4e 22 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  N");..      }.. 
372ea 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
372eb 61 69 6e 4e 4c 28 70 56 64 62 65 29 3b 0d 0a 20  ainNL(pVdbe);.. 
372ec 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
372ed 33 45 78 70 6c 61 69 6e 50 6f 70 28 70 56 64 62  3ExplainPop(pVdb
372ee 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  e);..  }..  if( 
372ef 70 2d 3e 70 57 68 65 72 65 20 29 7b 0d 0a 20 20  p->pWhere ){..  
372f0 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
372f1 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 57  Printf(pVdbe, "W
372f2 48 45 52 45 20 22 29 3b 0d 0a 20 20 20 20 73 71  HERE ");..    sq
372f3 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72  lite3ExplainExpr
372f4 28 70 56 64 62 65 2c 20 70 2d 3e 70 57 68 65 72  (pVdbe, p->pWher
372f5 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
372f6 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65 29  ExplainNL(pVdbe)
372f7 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d  ;..  }..  if( p-
372f8 3e 70 47 72 6f 75 70 42 79 20 29 7b 0d 0a 20 20  >pGroupBy ){..  
372f9 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
372fa 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 47  Printf(pVdbe, "G
372fb 52 4f 55 50 42 59 20 22 29 3b 0d 0a 20 20 20 20  ROUPBY ");..    
372fc 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
372fd 70 72 4c 69 73 74 28 70 56 64 62 65 2c 20 70 2d  prList(pVdbe, p-
372fe 3e 70 47 72 6f 75 70 42 79 29 3b 0d 0a 20 20 20  >pGroupBy);..   
372ff 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e   sqlite3ExplainN
37300 4c 28 70 56 64 62 65 29 3b 0d 0a 20 20 7d 0d 0a  L(pVdbe);..  }..
37301 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67    if( p->pHaving
37302 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
37303 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
37304 64 62 65 2c 20 22 48 41 56 49 4e 47 20 22 29 3b  dbe, "HAVING ");
37305 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
37306 6c 61 69 6e 45 78 70 72 28 70 56 64 62 65 2c 20  lainExpr(pVdbe, 
37307 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0d 0a 20 20  p->pHaving);..  
37308 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
37309 4e 4c 28 70 56 64 62 65 29 3b 0d 0a 20 20 7d 0d  NL(pVdbe);..  }.
3730a 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72  .  if( p->pOrder
3730b 42 79 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  By ){..    sqlit
3730c 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
3730d 70 56 64 62 65 2c 20 22 4f 52 44 45 52 42 59 20  pVdbe, "ORDERBY 
3730e 22 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ");..    sqlite3
3730f 45 78 70 6c 61 69 6e 45 78 70 72 4c 69 73 74 28  ExplainExprList(
37310 70 56 64 62 65 2c 20 70 2d 3e 70 4f 72 64 65 72  pVdbe, p->pOrder
37311 42 79 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  By);..    sqlite
37312 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65  3ExplainNL(pVdbe
37313 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  );..  }..  if( p
37314 2d 3e 70 4c 69 6d 69 74 20 29 7b 0d 0a 20 20 20  ->pLimit ){..   
37315 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
37316 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 4c 49  rintf(pVdbe, "LI
37317 4d 49 54 20 22 29 3b 0d 0a 20 20 20 20 73 71 6c  MIT ");..    sql
37318 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28  ite3ExplainExpr(
37319 70 56 64 62 65 2c 20 70 2d 3e 70 4c 69 6d 69 74  pVdbe, p->pLimit
3731a 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  );..    sqlite3E
3731b 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65 29 3b  xplainNL(pVdbe);
3731c 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 2d 3e  ..  }..  if( p->
3731d 70 4f 66 66 73 65 74 20 29 7b 0d 0a 20 20 20 20  pOffset ){..    
3731e 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
3731f 69 6e 74 66 28 70 56 64 62 65 2c 20 22 4f 46 46  intf(pVdbe, "OFF
37320 53 45 54 20 22 29 3b 0d 0a 20 20 20 20 73 71 6c  SET ");..    sql
37321 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28  ite3ExplainExpr(
37322 70 56 64 62 65 2c 20 70 2d 3e 70 4f 66 66 73 65  pVdbe, p->pOffse
37323 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  t);..    sqlite3
37324 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65 29  ExplainNL(pVdbe)
37325 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 53 51 4c 49 54  ;..  }..}..SQLIT
37326 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37327 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 53 65 6c  qlite3ExplainSel
37328 65 63 74 28 56 64 62 65 20 2a 70 56 64 62 65 2c  ect(Vdbe *pVdbe,
37329 20 53 65 6c 65 63 74 20 2a 70 29 7b 0d 0a 20 20   Select *p){..  
3732a 69 66 28 20 70 3d 3d 30 20 29 7b 0d 0a 20 20 20  if( p==0 ){..   
3732b 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
3732c 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 28 6e  rintf(pVdbe, "(n
3732d 75 6c 6c 2d 73 65 6c 65 63 74 29 22 29 3b 0d 0a  ull-select)");..
3732e 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
3732f 0d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50  ..  while( p->pP
37330 72 69 6f 72 20 29 20 70 20 3d 20 70 2d 3e 70 50  rior ) p = p->pP
37331 72 69 6f 72 3b 0d 0a 20 20 73 71 6c 69 74 65 33  rior;..  sqlite3
37332 45 78 70 6c 61 69 6e 50 75 73 68 28 70 56 64 62  ExplainPush(pVdb
37333 65 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 20  e);..  while( p 
37334 29 7b 0d 0a 20 20 20 20 65 78 70 6c 61 69 6e 4f  ){..    explainO
37335 6e 65 53 65 6c 65 63 74 28 70 56 64 62 65 2c 20  neSelect(pVdbe, 
37336 70 29 3b 0d 0a 20 20 20 20 70 20 3d 20 70 2d 3e  p);..    p = p->
37337 70 4e 65 78 74 3b 0d 0a 20 20 20 20 69 66 28 20  pNext;..    if( 
37338 70 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20  p==0 ) break;.. 
37339 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
3733a 6e 4e 4c 28 70 56 64 62 65 29 3b 0d 0a 20 20 20  nNL(pVdbe);..   
3733b 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
3733c 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22 25 73  rintf(pVdbe, "%s
3733d 5c 6e 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d  \n", selectOpNam
3733e 65 28 70 2d 3e 6f 70 29 29 3b 0d 0a 20 20 7d 0d  e(p->op));..  }.
3733f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  .  sqlite3Explai
37340 6e 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22  nPrintf(pVdbe, "
37341 45 4e 44 22 29 3b 0d 0a 20 20 73 71 6c 69 74 65  END");..  sqlite
37342 33 45 78 70 6c 61 69 6e 50 6f 70 28 70 56 64 62  3ExplainPop(pVdb
37343 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 45 6e 64  e);..}..../* End
37344 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72   of the structur
37345 65 20 64 65 62 75 67 20 70 72 69 6e 74 69 6e 67  e debug printing
37346 20 63 6f 64 65 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a   code..*********
37347 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37348 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37349 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3734a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3734b 2a 2a 2a 2a 2f 0d 0a 23 65 6e 64 69 66 20 2f 2a  ****/..#endif /*
3734c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3734d 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64  TEST) || defined
3734e 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a  (SQLITE_DEBUG) *
3734f 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
37350 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c 65  **** End of sele
37351 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ct.c ***********
37352 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37353 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37354 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
37355 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
37356 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   table.c *******
37357 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37359 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
3735a 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0d  01 September 15.
3735b 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68  .**..** The auth
3735c 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3735d 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3735e 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3735f 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c  place of..** a l
37360 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
37361 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
37362 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ..**..**    May 
37363 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
37364 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20  not evil...**   
37365 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
37366 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
37367 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
37368 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20  ve others...**  
37369 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
3736a 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
3736b 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
3736c 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  ou give...**..**
3736d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3736e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3736f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37371 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  *******..** This
37372 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
37373 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
37374 61 62 6c 65 28 29 20 61 6e 64 20 73 71 6c 69 74  able() and sqlit
37375 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 0d  e3_free_table().
37376 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f  .** interface ro
37377 75 74 69 6e 65 73 2e 20 20 54 68 65 73 65 20 61  utines.  These a
37378 72 65 20 6a 75 73 74 20 77 72 61 70 70 65 72 73  re just wrappers
37379 20 61 72 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e   around the main
3737a 0d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72  ..** interface r
3737b 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c 69 74 65  outine of sqlite
3737c 33 5f 65 78 65 63 28 29 2e 0d 0a 2a 2a 0d 0a 2a  3_exec()...**..*
3737d 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
3737e 20 61 72 65 20 69 6e 20 61 20 73 65 70 61 72 61   are in a separa
3737f 74 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74  te files so that
37380 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62   they will not b
37381 65 20 6c 69 6e 6b 65 64 0d 0a 2a 2a 20 69 66 20  e linked..** if 
37382 74 68 65 79 20 61 72 65 20 6e 6f 74 20 75 73 65  they are not use
37383 64 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c  d...*/../* #incl
37384 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a  ude <stdlib.h> *
37385 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  /../* #include <
37386 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d 0a 0d 0a  string.h> */....
37387 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
37388 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0d 0a 0d  MIT_GET_TABLE...
37389 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 73 74 72  ./*..** This str
3738a 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
3738b 6f 20 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d  o pass data from
3738c 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
3738d 6c 65 28 29 20 74 68 72 6f 75 67 68 0d 0a 2a 2a  le() through..**
3738e 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
3738f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
37390 73 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72  s to build the r
37391 65 73 75 6c 74 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  esult...*/..type
37392 64 65 66 20 73 74 72 75 63 74 20 54 61 62 52 65  def struct TabRe
37393 73 75 6c 74 20 7b 0d 0a 20 20 63 68 61 72 20 2a  sult {..  char *
37394 2a 61 7a 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20  *azResult;   /* 
37395 41 63 63 75 6d 75 6c 61 74 65 64 20 6f 75 74 70  Accumulated outp
37396 75 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  ut */..  char *z
37397 45 72 72 4d 73 67 3b 20 20 20 20 20 2f 2a 20 45  ErrMsg;     /* E
37398 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
37399 74 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  t, if an error o
3739a 63 63 75 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  ccurs */..  int 
3739b 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f  nAlloc;        /
3739c 2a 20 53 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65  * Slots allocate
3739d 64 20 66 6f 72 20 61 7a 52 65 73 75 6c 74 5b 5d  d for azResult[]
3739e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 6f 77 3b   */..  int nRow;
3739f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
373a0 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74  ber of rows in t
373a1 68 65 20 72 65 73 75 6c 74 20 2a 2f 0d 0a 20 20  he result */..  
373a2 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
373a3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
373a4 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
373a5 65 73 75 6c 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  esult */..  int 
373a6 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f  nData;         /
373a7 2a 20 53 6c 6f 74 73 20 75 73 65 64 20 69 6e 20  * Slots used in 
373a8 61 7a 52 65 73 75 6c 74 5b 5d 2e 20 20 28 6e 52  azResult[].  (nR
373a9 6f 77 2b 31 29 2a 6e 43 6f 6c 75 6d 6e 20 2a 2f  ow+1)*nColumn */
373aa 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
373ab 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
373ac 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74   code from sqlit
373ad 65 33 5f 65 78 65 63 28 29 20 2a 2f 0d 0a 7d 20  e3_exec() */..} 
373ae 54 61 62 52 65 73 75 6c 74 3b 0d 0a 0d 0a 2f 2a  TabResult;..../*
373af 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
373b0 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  e is called once
373b1 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e   for each row in
373b2 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
373b3 65 2e 20 20 49 74 73 20 6a 6f 62 0d 0a 2a 2a 20  e.  Its job..** 
373b4 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68  is to fill in th
373b5 65 20 54 61 62 52 65 73 75 6c 74 20 73 74 72 75  e TabResult stru
373b6 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61 74  cture appropriat
373b7 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20  ely, allocating 
373b8 6e 65 77 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  new..** memory a
373b9 73 20 6e 65 63 65 73 73 61 72 79 2e 0d 0a 2a 2f  s necessary...*/
373ba 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  ..static int sql
373bb 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63  ite3_get_table_c
373bc 62 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e  b(void *pArg, in
373bd 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61  t nCol, char **a
373be 72 67 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76  rgv, char **colv
373bf 29 7b 0d 0a 20 20 54 61 62 52 65 73 75 6c 74 20  ){..  TabResult 
373c0 2a 70 20 3d 20 28 54 61 62 52 65 73 75 6c 74 2a  *p = (TabResult*
373c1 29 70 41 72 67 3b 20 20 2f 2a 20 52 65 73 75 6c  )pArg;  /* Resul
373c2 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f  t accumulator */
373c3 0d 0a 20 20 69 6e 74 20 6e 65 65 64 3b 20 20 20  ..  int need;   
373c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
373c5 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 6e        /* Slots n
373c6 65 65 64 65 64 20 69 6e 20 70 2d 3e 61 7a 52 65  eeded in p->azRe
373c7 73 75 6c 74 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74  sult[] */..  int
373c8 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
373c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
373ca 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
373cb 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  /..  char *z;   
373cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
373cd 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
373ce 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 72 65 73  le column of res
373cf 75 6c 74 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 4d  ult */....  /* M
373d0 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69  ake sure there i
373d1 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69  s enough space i
373d2 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 74 6f  n p->azResult to
373d3 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67   hold everything
373d4 0d 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 74  ..  ** we need t
373d5 6f 20 72 65 6d 65 6d 62 65 72 20 66 72 6f 6d 20  o remember from 
373d6 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20  this invocation 
373d7 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e  of the callback.
373d8 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d  ..  */..  if( p-
373d9 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67 76  >nRow==0 && argv
373da 21 3d 30 20 29 7b 0d 0a 20 20 20 20 6e 65 65 64  !=0 ){..    need
373db 20 3d 20 6e 43 6f 6c 2a 32 3b 0d 0a 20 20 7d 65   = nCol*2;..  }e
373dc 6c 73 65 7b 0d 0a 20 20 20 20 6e 65 65 64 20 3d  lse{..    need =
373dd 20 6e 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 20 20 69   nCol;..  }..  i
373de 66 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65  f( p->nData + ne
373df 65 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29  ed > p->nAlloc )
373e0 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a  {..    char **az
373e1 4e 65 77 3b 0d 0a 20 20 20 20 70 2d 3e 6e 41 6c  New;..    p->nAl
373e2 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a  loc = p->nAlloc*
373e3 32 20 2b 20 6e 65 65 64 3b 0d 0a 20 20 20 20 61  2 + need;..    a
373e4 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  zNew = sqlite3_r
373e5 65 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73  ealloc( p->azRes
373e6 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72  ult, sizeof(char
373e7 2a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0d  *)*p->nAlloc );.
373e8 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d  .    if( azNew==
373e9 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f  0 ) goto malloc_
373ea 66 61 69 6c 65 64 3b 0d 0a 20 20 20 20 70 2d 3e  failed;..    p->
373eb 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77  azResult = azNew
373ec 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49  ;..  }....  /* I
373ed 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
373ee 72 73 74 20 72 6f 77 2c 20 74 68 65 6e 20 67 65  rst row, then ge
373ef 6e 65 72 61 74 65 20 61 6e 20 65 78 74 72 61 20  nerate an extra 
373f0 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 0d 0a  row containing..
373f1 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f    ** the names o
373f2 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e 0d 0a  f all columns...
373f3 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 2d 3e 6e    */..  if( p->n
373f4 52 6f 77 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70  Row==0 ){..    p
373f5 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c  ->nColumn = nCol
373f6 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
373f7 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20  i<nCol; i++){.. 
373f8 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
373f9 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 63  _mprintf("%s", c
373fa 6f 6c 76 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20  olv[i]);..      
373fb 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20  if( z==0 ) goto 
373fc 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0d 0a  malloc_failed;..
373fd 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
373fe 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
373ff 7a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c  z;..    }..  }el
37400 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d  se if( p->nColum
37401 6e 21 3d 6e 43 6f 6c 20 29 7b 0d 0a 20 20 20 20  n!=nCol ){..    
37402 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
37403 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 70  zErrMsg);..    p
37404 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  ->zErrMsg = sqli
37405 74 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a 20 20  te3_mprintf(..  
37406 20 20 20 20 20 22 73 71 6c 69 74 65 33 5f 67 65       "sqlite3_ge
37407 74 5f 74 61 62 6c 65 28 29 20 63 61 6c 6c 65 64  t_table() called
37408 20 77 69 74 68 20 74 77 6f 20 6f 72 20 6d 6f 72   with two or mor
37409 65 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 71  e incompatible q
3740a 75 65 72 69 65 73 22 0d 0a 20 20 20 20 29 3b 0d  ueries"..    );.
3740b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  .    p->rc = SQL
3740c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20  ITE_ERROR;..    
3740d 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a  return 1;..  }..
3740e 0d 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
3740f 20 74 68 65 20 72 6f 77 20 64 61 74 61 0d 0a 20   the row data.. 
37410 20 2a 2f 0d 0a 20 20 69 66 28 20 61 72 67 76 21   */..  if( argv!
37411 3d 30 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69  =0 ){..    for(i
37412 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
37413 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 61 72 67  {..      if( arg
37414 76 5b 69 5d 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  v[i]==0 ){..    
37415 20 20 20 20 7a 20 3d 20 30 3b 0d 0a 20 20 20 20      z = 0;..    
37416 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
37417 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
37418 33 53 74 72 6c 65 6e 33 30 28 61 72 67 76 5b 69  3Strlen30(argv[i
37419 5d 29 2b 31 3b 0d 0a 20 20 20 20 20 20 20 20 7a  ])+1;..        z
3741a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
3741b 63 28 20 6e 20 29 3b 0d 0a 20 20 20 20 20 20 20  c( n );..       
3741c 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f   if( z==0 ) goto
3741d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0d   malloc_failed;.
3741e 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
3741f 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0d  z, argv[i], n);.
37420 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
37421 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e  p->azResult[p->n
37422 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0d 0a 20 20  Data++] = z;..  
37423 20 20 7d 0d 0a 20 20 20 20 70 2d 3e 6e 52 6f 77    }..    p->nRow
37424 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  ++;..  }..  retu
37425 72 6e 20 30 3b 0d 0a 0d 0a 6d 61 6c 6c 6f 63 5f  rn 0;....malloc_
37426 66 61 69 6c 65 64 3a 0d 0a 20 20 70 2d 3e 72 63  failed:..  p->rc
37427 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
37428 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 7d  ..  return 1;..}
37429 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 51 75 65 72 79  ..../*..** Query
3742a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
3742b 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69  But instead of i
3742c 6e 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c 62 61  nvoking a callba
3742d 63 6b 20 66 6f 72 20 65 61 63 68 20 72 6f 77 2c  ck for each row,
3742e 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 6f  ..** malloc() fo
3742f 72 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  r space to hold 
37430 74 68 65 20 72 65 73 75 6c 74 20 61 6e 64 20 72  the result and r
37431 65 74 75 72 6e 20 74 68 65 20 65 6e 74 69 72 65  eturn the entire
37432 20 72 65 73 75 6c 74 73 0d 0a 2a 2a 20 61 74 20   results..** at 
37433 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
37434 66 20 74 68 65 20 63 61 6c 6c 2e 0d 0a 2a 2a 0d  f the call...**.
37435 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 74  .** The result t
37436 68 61 74 20 69 73 20 77 72 69 74 74 65 6e 20 74  hat is written t
37437 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20 69  o ***pazResult i
37438 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
37439 20 6f 62 74 61 69 6e 65 64 0d 0a 2a 2a 20 66 72   obtained..** fr
3743a 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75  om malloc().  Bu
3743b 74 20 74 68 65 20 63 61 6c 6c 65 72 20 63 61 6e  t the caller can
3743c 6e 6f 74 20 66 72 65 65 20 74 68 69 73 20 6d 65  not free this me
3743d 6d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e 20 20  mory directly.  
3743e 0d 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74 68  ..** Instead, th
3743f 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20 73  e entire table s
37440 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20  hould be passed 
37441 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  to sqlite3_free_
37442 74 61 62 6c 65 28 29 20 77 68 65 6e 0d 0a 2a 2a  table() when..**
37443 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
37444 63 65 64 75 72 65 20 69 73 20 66 69 6e 69 73 68  cedure is finish
37445 65 64 20 75 73 69 6e 67 20 69 74 2e 0d 0a 2a 2f  ed using it...*/
37446 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
37447 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
37448 6c 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  le(..  sqlite3 *
37449 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
3744a 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
3744b 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  se on which the 
3744c 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0d  SQL executes */.
3744d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3744e 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Sql,           /
3744f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20  * The SQL to be 
37450 65 78 65 63 75 74 65 64 20 2a 2f 0d 0a 20 20 63  executed */..  c
37451 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
37452 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ,          /* Wr
37453 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 74  ite the result t
37454 61 62 6c 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20  able here */..  
37455 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20  int *pnRow,     
37456 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
37457 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
37458 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72  of rows in the r
37459 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0d 0a 20  esult here */.. 
3745a 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20   int *pnColumn, 
3745b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3745c 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  Write the number
3745d 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 72   of columns of r
3745e 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0d 0a 20  esult here */.. 
3745f 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
37460 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37461 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  Write error mess
37462 61 67 65 73 20 68 65 72 65 20 2a 2f 0d 0a 29 7b  ages here */..){
37463 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 54  ..  int rc;..  T
37464 61 62 52 65 73 75 6c 74 20 72 65 73 3b 0d 0a 0d  abResult res;...
37465 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20  .  *pazResult = 
37466 30 3b 0d 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75  0;..  if( pnColu
37467 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d  mn ) *pnColumn =
37468 20 30 3b 0d 0a 20 20 69 66 28 20 70 6e 52 6f 77   0;..  if( pnRow
37469 20 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0d 0a   ) *pnRow = 0;..
3746a 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
3746b 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0d   *pzErrMsg = 0;.
3746c 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d  .  res.zErrMsg =
3746d 20 30 3b 0d 0a 20 20 72 65 73 2e 6e 52 6f 77 20   0;..  res.nRow 
3746e 3d 20 30 3b 0d 0a 20 20 72 65 73 2e 6e 43 6f 6c  = 0;..  res.nCol
3746f 75 6d 6e 20 3d 20 30 3b 0d 0a 20 20 72 65 73 2e  umn = 0;..  res.
37470 6e 44 61 74 61 20 3d 20 31 3b 0d 0a 20 20 72 65  nData = 1;..  re
37471 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0d 0a  s.nAlloc = 20;..
37472 20 20 72 65 73 2e 72 63 20 3d 20 53 51 4c 49 54    res.rc = SQLIT
37473 45 5f 4f 4b 3b 0d 0a 20 20 72 65 73 2e 61 7a 52  E_OK;..  res.azR
37474 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f  esult = sqlite3_
37475 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68  malloc(sizeof(ch
37476 61 72 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20  ar*)*res.nAlloc 
37477 29 3b 0d 0a 20 20 69 66 28 20 72 65 73 2e 61 7a  );..  if( res.az
37478 52 65 73 75 6c 74 3d 3d 30 20 29 7b 0d 0a 20 20  Result==0 ){..  
37479 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
3747a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
3747b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3747c 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a  TE_NOMEM;..  }..
3747d 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30    res.azResult[0
3747e 5d 20 3d 20 30 3b 0d 0a 20 20 72 63 20 3d 20 73  ] = 0;..  rc = s
3747f 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
37480 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 5f 67 65  zSql, sqlite3_ge
37481 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26 72 65 73  t_table_cb, &res
37482 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20  , pzErrMsg);..  
37483 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 72  assert( sizeof(r
37484 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3e  es.azResult[0])>
37485 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e 44 61  = sizeof(res.nDa
37486 74 61 29 20 29 3b 0d 0a 20 20 72 65 73 2e 61 7a  ta) );..  res.az
37487 52 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51 4c 49  Result[0] = SQLI
37488 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65  TE_INT_TO_PTR(re
37489 73 2e 6e 44 61 74 61 29 3b 0d 0a 20 20 69 66 28  s.nData);..  if(
3748a 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49   (rc&0xff)==SQLI
3748b 54 45 5f 41 42 4f 52 54 20 29 7b 0d 0a 20 20 20  TE_ABORT ){..   
3748c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
3748d 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
3748e 74 5b 31 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20  t[1]);..    if( 
3748f 72 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0d 0a  res.zErrMsg ){..
37490 20 20 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d        if( pzErrM
37491 73 67 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  sg ){..        s
37492 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a 45  qlite3_free(*pzE
37493 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 20 20 20  rrMsg);..       
37494 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
37495 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
37496 22 2c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0d  ",res.zErrMsg);.
37497 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
37498 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73  sqlite3_free(res
37499 2e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20  .zErrMsg);..    
3749a 7d 0d 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  }..    db->errCo
3749b 64 65 20 3d 20 72 65 73 2e 72 63 3b 20 20 2f 2a  de = res.rc;  /*
3749c 20 41 73 73 75 6d 65 20 33 32 2d 62 69 74 20 61   Assume 32-bit a
3749d 73 73 69 67 6e 6d 65 6e 74 20 69 73 20 61 74 6f  ssignment is ato
3749e 6d 69 63 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75  mic */..    retu
3749f 72 6e 20 72 65 73 2e 72 63 3b 0d 0a 20 20 7d 0d  rn res.rc;..  }.
374a0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
374a1 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0d 0a 20  res.zErrMsg);.. 
374a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
374a3 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  OK ){..    sqlit
374a4 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
374a5 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
374a6 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ..    return rc;
374a7 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 65 73  ..  }..  if( res
374a8 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74  .nAlloc>res.nDat
374a9 61 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a  a ){..    char *
374aa 2a 61 7a 4e 65 77 3b 0d 0a 20 20 20 20 61 7a 4e  *azNew;..    azN
374ab 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
374ac 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75  lloc( res.azResu
374ad 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
374ae 29 2a 72 65 73 2e 6e 44 61 74 61 20 29 3b 0d 0a  )*res.nData );..
374af 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30      if( azNew==0
374b0 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
374b1 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
374b2 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
374b3 0d 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43  ..      db->errC
374b4 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ode = SQLITE_NOM
374b5 45 4d 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  EM;..      retur
374b6 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
374b7 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 73 2e  .    }..    res.
374b8 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77  azResult = azNew
374b9 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 61 7a 52 65  ;..  }..  *pazRe
374ba 73 75 6c 74 20 3d 20 26 72 65 73 2e 61 7a 52 65  sult = &res.azRe
374bb 73 75 6c 74 5b 31 5d 3b 0d 0a 20 20 69 66 28 20  sult[1];..  if( 
374bc 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f  pnColumn ) *pnCo
374bd 6c 75 6d 6e 20 3d 20 72 65 73 2e 6e 43 6f 6c 75  lumn = res.nColu
374be 6d 6e 3b 0d 0a 20 20 69 66 28 20 70 6e 52 6f 77  mn;..  if( pnRow
374bf 20 29 20 2a 70 6e 52 6f 77 20 3d 20 72 65 73 2e   ) *pnRow = res.
374c0 6e 52 6f 77 3b 0d 0a 20 20 72 65 74 75 72 6e 20  nRow;..  return 
374c1 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  rc;..}..../*..**
374c2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   This routine fr
374c3 65 65 73 20 74 68 65 20 73 70 61 63 65 20 74 68  ees the space th
374c4 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
374c5 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e 0d  ble() malloced..
374c6 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
374c7 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65  void sqlite3_fre
374c8 65 5f 74 61 62 6c 65 28 0d 0a 20 20 63 68 61 72  e_table(..  char
374c9 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20   **azResult     
374ca 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
374cb 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66   returned from f
374cc 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  rom sqlite3_get_
374cd 74 61 62 6c 65 28 29 20 2a 2f 0d 0a 29 7b 0d 0a  table() */..){..
374ce 20 20 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29    if( azResult )
374cf 7b 0d 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b  {..    int i, n;
374d0 0d 0a 20 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d  ..    azResult--
374d1 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  ;..    assert( a
374d2 7a 52 65 73 75 6c 74 21 3d 30 20 29 3b 0d 0a 20  zResult!=0 );.. 
374d3 20 20 20 6e 20 3d 20 53 51 4c 49 54 45 5f 50 54     n = SQLITE_PT
374d4 52 5f 54 4f 5f 49 4e 54 28 61 7a 52 65 73 75 6c  R_TO_INT(azResul
374d5 74 5b 30 5d 29 3b 0d 0a 20 20 20 20 66 6f 72 28  t[0]);..    for(
374d6 69 3d 31 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20  i=1; i<n; i++){ 
374d7 69 66 28 20 61 7a 52 65 73 75 6c 74 5b 69 5d 20  if( azResult[i] 
374d8 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61  ) sqlite3_free(a
374d9 7a 52 65 73 75 6c 74 5b 69 5d 29 3b 20 7d 0d 0a  zResult[i]); }..
374da 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
374db 28 61 7a 52 65 73 75 6c 74 29 3b 0d 0a 20 20 7d  (azResult);..  }
374dc 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a  ..}....#endif /*
374dd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
374de 5f 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 2f 2a 2a  _TABLE */..../**
374df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
374e0 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a   of table.c ****
374e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
374e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
374e5 67 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72  gin file trigger
374e6 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
374e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
374e9 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75  *..**..** The au
374ea 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
374eb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
374ec 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
374ed 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61  n place of..** a
374ee 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
374ef 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
374f0 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61  g:..**..**    Ma
374f1 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
374f2 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20  d not evil...** 
374f3 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
374f4 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
374f5 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
374f6 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a  give others...**
374f7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
374f8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
374f9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
374fa 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a   you give...**..
374fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68  *********..** Th
37500 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
37501 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
37502 69 6f 6e 20 66 6f 72 20 54 52 49 47 47 45 52 73  ion for TRIGGERs
37503 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ..*/....#ifndef 
37504 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
37505 47 45 52 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65  GER../*..** Dele
37506 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  te a linked list
37507 20 6f 66 20 54 72 69 67 67 65 72 53 74 65 70 20   of TriggerStep 
37508 73 74 72 75 63 74 75 72 65 73 2e 0d 0a 2a 2f 0d  structures...*/.
37509 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3750a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
3750b 74 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71  teTriggerStep(sq
3750c 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67  lite3 *db, Trigg
3750d 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
3750e 53 74 65 70 29 7b 0d 0a 20 20 77 68 69 6c 65 28  Step){..  while(
3750f 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b   pTriggerStep ){
37510 0d 0a 20 20 20 20 54 72 69 67 67 65 72 53 74 65  ..    TriggerSte
37511 70 20 2a 20 70 54 6d 70 20 3d 20 70 54 72 69 67  p * pTmp = pTrig
37512 67 65 72 53 74 65 70 3b 0d 0a 20 20 20 20 70 54  gerStep;..    pT
37513 72 69 67 67 65 72 53 74 65 70 20 3d 20 70 54 72  riggerStep = pTr
37514 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65 78 74  iggerStep->pNext
37515 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ;....    sqlite3
37516 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
37517 54 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0d 0a 20  Tmp->pWhere);.. 
37518 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
37519 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d  stDelete(db, pTm
3751a 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0d 0a  p->pExprList);..
3751b 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
3751c 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  tDelete(db, pTmp
3751d 2d 3e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  ->pSelect);..   
3751e 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
3751f 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
37520 49 64 4c 69 73 74 29 3b 0d 0a 0d 0a 20 20 20 20  IdList);....    
37521 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
37522 2c 20 70 54 6d 70 29 3b 0d 0a 20 20 7d 0d 0a 7d  , pTmp);..  }..}
37523 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 69 76 65 6e  ..../*..** Given
37524 20 74 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74   table pTab, ret
37525 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
37526 6c 20 74 68 65 20 74 72 69 67 67 65 72 73 20 61  l the triggers a
37527 74 74 61 63 68 65 64 20 74 6f 20 0d 0a 2a 2a 20  ttached to ..** 
37528 74 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 6c  the table. The l
37529 69 73 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64  ist is connected
3752a 20 62 79 20 54 72 69 67 67 65 72 2e 70 4e 65 78   by Trigger.pNex
3752b 74 20 70 6f 69 6e 74 65 72 73 2e 0d 0a 2a 2a 0d  t pointers...**.
3752c 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 74  .** All of the t
3752d 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20  riggers on pTab 
3752e 74 68 61 74 20 61 72 65 20 69 6e 20 74 68 65 20  that are in the 
3752f 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 61 73  same database as
37530 20 70 54 61 62 0d 0a 2a 2a 20 61 72 65 20 61 6c   pTab..** are al
37531 72 65 61 64 79 20 61 74 74 61 63 68 65 64 20 74  ready attached t
37532 6f 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  o pTab->pTrigger
37533 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d 69 67  .  But there mig
37534 68 74 20 62 65 20 61 64 64 69 74 69 6f 6e 61 6c  ht be additional
37535 0d 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 6f 6e  ..** triggers on
37536 20 70 54 61 62 20 69 6e 20 74 68 65 20 54 45 4d   pTab in the TEM
37537 50 20 73 63 68 65 6d 61 2e 20 20 54 68 69 73 20  P schema.  This 
37538 72 6f 75 74 69 6e 65 20 70 72 65 70 65 6e 64 73  routine prepends
37539 20 61 6c 6c 0d 0a 2a 2a 20 54 45 4d 50 20 74 72   all..** TEMP tr
3753a 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74  iggers on pTab t
3753b 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
3753c 6f 66 20 74 68 65 20 70 54 61 62 2d 3e 70 54 72  of the pTab->pTr
3753d 69 67 67 65 72 20 6c 69 73 74 0d 0a 2a 2a 20 61  igger list..** a
3753e 6e 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 63  nd returns the c
3753f 6f 6d 62 69 6e 65 64 20 6c 69 73 74 2e 0d 0a 2a  ombined list...*
37540 2a 0d 0a 2a 2a 20 54 6f 20 73 74 61 74 65 20 69  *..** To state i
37541 74 20 61 6e 6f 74 68 65 72 20 77 61 79 3a 20 20  t another way:  
37542 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
37543 75 72 6e 73 20 61 20 6c 69 73 74 20 6f 66 20 61  urns a list of a
37544 6c 6c 20 74 72 69 67 67 65 72 73 0d 0a 2a 2a 20  ll triggers..** 
37545 74 68 61 74 20 66 69 72 65 20 6f 66 66 20 6f 66  that fire off of
37546 20 70 54 61 62 2e 20 20 54 68 65 20 6c 69 73 74   pTab.  The list
37547 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 61 6e   will include an
37548 79 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20  y TEMP triggers 
37549 6f 6e 0d 0a 2a 2a 20 70 54 61 62 20 61 73 20 77  on..** pTab as w
3754a 65 6c 6c 20 61 73 20 74 68 65 20 74 72 69 67 67  ell as the trigg
3754b 65 72 73 20 6c 69 73 65 64 20 69 6e 20 70 54 61  ers lised in pTa
3754c 62 2d 3e 70 54 72 69 67 67 65 72 2e 0d 0a 2a 2f  b->pTrigger...*/
3754d 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
3754e 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65   Trigger *sqlite
3754f 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72  3TriggerList(Par
37550 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
37551 65 20 2a 70 54 61 62 29 7b 0d 0a 20 20 53 63 68  e *pTab){..  Sch
37552 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54 6d 70  ema * const pTmp
37553 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73 65 2d  Schema = pParse-
37554 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  >db->aDb[1].pSch
37555 65 6d 61 3b 0d 0a 20 20 54 72 69 67 67 65 72 20  ema;..  Trigger 
37556 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20  *pList = 0;     
37557 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37558 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
37559 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 0d   to return */...
3755a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64  .  if( pParse->d
3755b 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29  isableTriggers )
3755c 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {..    return 0;
3755d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 70  ..  }....  if( p
3755e 54 6d 70 53 63 68 65 6d 61 21 3d 70 54 61 62 2d  TmpSchema!=pTab-
3755f 3e 70 53 63 68 65 6d 61 20 29 7b 0d 0a 20 20 20  >pSchema ){..   
37560 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0d 0a 20   HashElem *p;.. 
37561 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
37562 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c  e3SchemaMutexHel
37563 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c  d(pParse->db, 0,
37564 20 70 54 6d 70 53 63 68 65 6d 61 29 20 29 3b 0d   pTmpSchema) );.
37565 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  .    for(p=sqlit
37566 65 48 61 73 68 46 69 72 73 74 28 26 70 54 6d 70  eHashFirst(&pTmp
37567 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68  Schema->trigHash
37568 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61  ); p; p=sqliteHa
37569 73 68 4e 65 78 74 28 70 29 29 7b 0d 0a 20 20 20  shNext(p)){..   
3756a 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69     Trigger *pTri
3756b 67 20 3d 20 28 54 72 69 67 67 65 72 20 2a 29 73  g = (Trigger *)s
3756c 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29  qliteHashData(p)
3756d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54 72  ;..      if( pTr
3756e 69 67 2d 3e 70 54 61 62 53 63 68 65 6d 61 3d 3d  ig->pTabSchema==
3756f 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 0d 0a 20  pTab->pSchema.. 
37570 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69        && 0==sqli
37571 74 65 33 53 74 72 49 43 6d 70 28 70 54 72 69 67  te3StrICmp(pTrig
37572 2d 3e 74 61 62 6c 65 2c 20 70 54 61 62 2d 3e 7a  ->table, pTab->z
37573 4e 61 6d 65 29 20 0d 0a 20 20 20 20 20 20 29 7b  Name) ..      ){
37574 0d 0a 20 20 20 20 20 20 20 20 70 54 72 69 67 2d  ..        pTrig-
37575 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69 73 74 20  >pNext = (pList 
37576 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e  ? pList : pTab->
37577 70 54 72 69 67 67 65 72 29 3b 0d 0a 20 20 20 20  pTrigger);..    
37578 20 20 20 20 70 4c 69 73 74 20 3d 20 70 54 72 69      pList = pTri
37579 67 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  g;..      }..   
3757a 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74   }..  }....  ret
3757b 75 72 6e 20 28 70 4c 69 73 74 20 3f 20 70 4c 69  urn (pList ? pLi
3757c 73 74 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67  st : pTab->pTrig
3757d 67 65 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ger);..}..../*..
3757e 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  ** This is calle
3757f 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
37580 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 20 43  when it sees a C
37581 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74  REATE TRIGGER st
37582 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 75 70 20 74  atement..** up t
37583 6f 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  o the point of t
37584 68 65 20 42 45 47 49 4e 20 62 65 66 6f 72 65 20  he BEGIN before 
37585 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69  the trigger acti
37586 6f 6e 73 2e 20 20 41 20 54 72 69 67 67 65 72 0d  ons.  A Trigger.
37587 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
37588 20 67 65 6e 65 72 61 74 65 64 20 62 61 73 65 64   generated based
37589 20 6f 6e 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   on the informat
3758a 69 6f 6e 20 61 76 61 69 6c 61 62 6c 65 20 61 6e  ion available an
3758b 64 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 69 6e 20  d stored..** in 
3758c 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
3758d 67 65 72 2e 20 20 41 66 74 65 72 20 74 68 65 20  ger.  After the 
3758e 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20  trigger actions 
3758f 68 61 76 65 20 62 65 65 6e 20 70 61 72 73 65 64  have been parsed
37590 2c 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74 65  , the..** sqlite
37591 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 29  3FinishTrigger()
37592 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
37593 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  led to complete 
37594 74 68 65 20 74 72 69 67 67 65 72 0d 0a 2a 2a 20  the trigger..** 
37595 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 70 72 6f  construction pro
37596 63 65 73 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  cess...*/..SQLIT
37597 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37598 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67  qlite3BeginTrigg
37599 65 72 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  er(..  Parse *pP
3759a 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
3759b 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  e parse context 
3759c 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 52  of the CREATE TR
3759d 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20  IGGER statement 
3759e 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61  */..  Token *pNa
3759f 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me1,      /* The
375a0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
375a1 67 67 65 72 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e  gger */..  Token
375a2 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f   *pName2,      /
375a3 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
375a4 65 20 74 72 69 67 67 65 72 20 2a 2f 0d 0a 20 20  e trigger */..  
375a5 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20  int tr_tm,      
375a6 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
375a7 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45  _BEFORE, TK_AFTE
375a8 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f  R, TK_INSTEAD */
375a9 0d 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  ..  int op,     
375aa 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
375ab 66 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f  f TK_INSERT, TK_
375ac 55 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54  UPDATE, TK_DELET
375ad 45 20 2a 2f 0d 0a 20 20 49 64 4c 69 73 74 20 2a  E */..  IdList *
375ae 70 43 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63  pColumns,   /* c
375af 6f 6c 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68  olumn list if th
375b0 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  is is an UPDATE 
375b1 4f 46 20 74 72 69 67 67 65 72 20 2a 2f 0d 0a 20  OF trigger */.. 
375b2 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 6c 65   SrcList *pTable
375b3 4e 61 6d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65  Name,/* The name
375b4 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2f 76 69   of the table/vi
375b5 65 77 20 74 68 65 20 74 72 69 67 67 65 72 20 61  ew the trigger a
375b6 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0d 0a 20 20  pplies to */..  
375b7 45 78 70 72 20 2a 70 57 68 65 6e 2c 20 20 20 20  Expr *pWhen,    
375b8 20 20 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75      /* WHEN clau
375b9 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 54  se */..  int isT
375ba 65 6d 70 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  emp,         /* 
375bb 54 72 75 65 20 69 66 20 74 68 65 20 54 45 4d 50  True if the TEMP
375bc 4f 52 41 52 59 20 6b 65 79 77 6f 72 64 20 69 73  ORARY keyword is
375bd 20 70 72 65 73 65 6e 74 20 2a 2f 0d 0a 20 20 69   present */..  i
375be 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20  nt noErr        
375bf 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65     /* Suppress e
375c0 72 72 6f 72 73 20 69 66 20 74 68 65 20 74 72 69  rrors if the tri
375c1 67 67 65 72 20 61 6c 72 65 61 64 79 20 65 78 69  gger already exi
375c2 73 74 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 72  sts */..){..  Tr
375c3 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20  igger *pTrigger 
375c4 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77  = 0;  /* The new
375c5 20 74 72 69 67 67 65 72 20 2a 2f 0d 0a 20 20 54   trigger */..  T
375c6 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
375c7 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
375c8 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65 72  that the trigger
375c9 20 66 69 72 65 73 20 6f 66 66 20 6f 66 20 2a 2f   fires off of */
375ca 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  ..  char *zName 
375cb 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  = 0;        /* N
375cc 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67  ame of the trigg
375cd 65 72 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  er */..  sqlite3
375ce 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
375cf 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
375d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
375d1 2f 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  /..  int iDb;   
375d2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
375d3 54 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  The database to 
375d4 73 74 6f 72 65 20 74 68 65 20 74 72 69 67 67 65  store the trigge
375d5 72 20 69 6e 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e  r in */..  Token
375d6 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *pName;        
375d7 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c     /* The unqual
375d8 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f  ified db name */
375d9 0d 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  ..  DbFixer sFix
375da 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
375db 74 61 74 65 20 76 65 63 74 6f 72 20 66 6f 72 20  tate vector for 
375dc 74 68 65 20 44 42 20 66 69 78 65 72 20 2a 2f 0d  the DB fixer */.
375dd 0a 20 20 69 6e 74 20 69 54 61 62 44 62 3b 20 20  .  int iTabDb;  
375de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
375df 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
375e0 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
375e1 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   */....  assert(
375e2 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20 20   pName1!=0 );   
375e3 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67  /* pName1->z mig
375e4 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20  ht be NULL, but 
375e5 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74 73 65 6c  not pName1 itsel
375e6 66 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20  f */..  assert( 
375e7 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0d 0a 20 20  pName2!=0 );..  
375e8 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 49  assert( op==TK_I
375e9 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  NSERT || op==TK_
375ea 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b  UPDATE || op==TK
375eb 5f 44 45 4c 45 54 45 20 29 3b 0d 0a 20 20 61 73  _DELETE );..  as
375ec 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70  sert( op>0 && op
375ed 3c 30 78 66 66 20 29 3b 0d 0a 20 20 69 66 28 20  <0xff );..  if( 
375ee 69 73 54 65 6d 70 20 29 7b 0d 0a 20 20 20 20 2f  isTemp ){..    /
375ef 2a 20 49 66 20 54 45 4d 50 20 77 61 73 20 73 70  * If TEMP was sp
375f0 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68  ecified, then th
375f1 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 6d  e trigger name m
375f2 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66  ay not be qualif
375f3 69 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28  ied. */..    if(
375f4 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0d   pName2->n>0 ){.
375f5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
375f6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
375f7 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65  temporary trigge
375f8 72 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 71  r may not have q
375f9 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 22 29 3b  ualified name");
375fa 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  ..      goto tri
375fb 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20  gger_cleanup;.. 
375fc 20 20 20 7d 0d 0a 20 20 20 20 69 44 62 20 3d 20     }..    iDb = 
375fd 31 3b 0d 0a 20 20 20 20 70 4e 61 6d 65 20 3d 20  1;..    pName = 
375fe 70 4e 61 6d 65 31 3b 0d 0a 20 20 7d 65 6c 73 65  pName1;..  }else
375ff 7b 0d 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65  {..    /* Figure
37600 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61 74   out the db that
37601 20 74 68 65 20 74 68 65 20 74 72 69 67 67 65 72   the the trigger
37602 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64   will be created
37603 20 69 6e 20 2a 2f 0d 0a 20 20 20 20 69 44 62 20   in */..    iDb 
37604 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
37605 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
37606 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
37607 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 69  ame);..    if( i
37608 44 62 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20 67  Db<0 ){..      g
37609 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
3760a 6e 75 70 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  nup;..    }..  }
3760b 0d 0a 20 20 69 66 28 20 21 70 54 61 62 6c 65 4e  ..  if( !pTableN
3760c 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ame || db->mallo
3760d 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20  cFailed ){..    
3760e 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
3760f 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  anup;..  }....  
37610 2f 2a 20 41 20 6c 6f 6e 67 2d 73 74 61 6e 64 69  /* A long-standi
37611 6e 67 20 70 61 72 73 65 72 20 62 75 67 20 69 73  ng parser bug is
37612 20 74 68 61 74 20 74 68 69 73 20 73 79 6e 74 61   that this synta
37613 78 20 77 61 73 20 61 6c 6c 6f 77 65 64 3a 0d 0a  x was allowed:..
37614 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 43 52    **..  **    CR
37615 45 41 54 45 20 54 52 49 47 47 45 52 20 61 74 74  EATE TRIGGER att
37616 61 63 68 65 64 2e 64 65 6d 6f 20 41 46 54 45 52  ached.demo AFTER
37617 20 49 4e 53 45 52 54 20 4f 4e 20 61 74 74 61 63   INSERT ON attac
37618 68 65 64 2e 74 61 62 20 2e 2e 2e 2e 0d 0a 20 20  hed.tab ......  
37619 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3761a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3761b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3761c 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e 0d 0a 20 20 2a     ^^^^^^^^..  *
3761d 2a 0d 0a 20 20 2a 2a 20 54 6f 20 6d 61 69 6e 74  *..  ** To maint
3761e 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f  ain backwards co
3761f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 69 67 6e  mpatibility, ign
37620 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
37621 0d 0a 20 20 2a 2a 20 6e 61 6d 65 20 6f 6e 20 70  ..  ** name on p
37622 54 61 62 6c 65 4e 61 6d 65 20 69 66 20 77 65 20  TableName if we 
37623 61 72 65 20 72 65 70 61 72 73 69 6e 67 20 6f 75  are reparsing ou
37624 72 20 6f 66 20 53 51 4c 49 54 45 5f 4d 41 53 54  r of SQLITE_MAST
37625 45 52 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ER...  */..  if(
37626 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26   db->init.busy &
37627 26 20 69 44 62 21 3d 31 20 29 7b 0d 0a 20 20 20  & iDb!=1 ){..   
37628 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
37629 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61  b, pTableName->a
3762a 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0d  [0].zDatabase);.
3762b 0a 20 20 20 20 70 54 61 62 6c 65 4e 61 6d 65 2d  .    pTableName-
3762c 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 20  >a[0].zDatabase 
3762d 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  = 0;..  }....  /
3762e 2a 20 49 66 20 74 68 65 20 74 72 69 67 67 65 72  * If the trigger
3762f 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c   name was unqual
37630 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20 74  ified, and the t
37631 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20 74  able is a temp t
37632 61 62 6c 65 2c 0d 0a 20 20 2a 2a 20 74 68 65 6e  able,..  ** then
37633 20 73 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f   set iDb to 1 to
37634 20 63 72 65 61 74 65 20 74 68 65 20 74 72 69 67   create the trig
37635 67 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f  ger in the tempo
37636 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0d 0a  rary database...
37637 20 20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53    ** If sqlite3S
37638 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72  rcListLookup() r
37639 65 74 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61  eturns 0, indica
3763a 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64  ting the table d
3763b 6f 65 73 20 6e 6f 74 0d 0a 20 20 2a 2a 20 65 78  oes not..  ** ex
3763c 69 73 74 2c 20 74 68 65 20 65 72 72 6f 72 20 69  ist, the error i
3763d 73 20 63 61 75 67 68 74 20 62 79 20 74 68 65 20  s caught by the 
3763e 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0d 0a 20 20  block below...  
3763f 2a 2f 0d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  */..  pTab = sql
37640 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
37641 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65  p(pParse, pTable
37642 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28 20 64 62  Name);..  if( db
37643 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 26  ->init.busy==0 &
37644 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26  & pName2->n==0 &
37645 26 20 70 54 61 62 0d 0a 20 20 20 20 20 20 20 20  & pTab..        
37646 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  && pTab->pSchema
37647 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ==db->aDb[1].pSc
37648 68 65 6d 61 20 29 7b 0d 0a 20 20 20 20 69 44 62  hema ){..    iDb
37649 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20   = 1;..  }....  
3764a 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74 61  /* Ensure the ta
3764b 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 73  ble name matches
3764c 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61   database name a
3764d 6e 64 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  nd that the tabl
3764e 65 20 65 78 69 73 74 73 20 2a 2f 0d 0a 20 20 69  e exists */..  i
3764f 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
37650 6c 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67  led ) goto trigg
37651 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 61  er_cleanup;..  a
37652 73 73 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d  ssert( pTableNam
37653 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0d 0a 20  e->nSrc==1 );.. 
37654 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
37655 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
37656 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65 72  e, iDb, "trigger
37657 22 2c 20 70 4e 61 6d 65 29 20 26 26 20 0d 0a 20  ", pName) && .. 
37658 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78 53       sqlite3FixS
37659 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54  rcList(&sFix, pT
3765a 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0d 0a 20 20  ableName) ){..  
3765b 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
3765c 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 20 20  leanup;..  }..  
3765d 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
3765e 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
3765f 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  se, pTableName);
37660 0d 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 7b  ..  if( !pTab ){
37661 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ..    /* The tab
37662 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  le does not exis
37663 74 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 64  t. */..    if( d
37664 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29  b->init.iDb==1 )
37665 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 69 63 6b  {..      /* Tick
37666 65 74 20 23 33 38 31 30 2e 0d 0a 20 20 20 20 20  et #3810...     
37667 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 77 68   ** Normally, wh
37668 65 6e 65 76 65 72 20 61 20 74 61 62 6c 65 20 69  enever a table i
37669 73 20 64 72 6f 70 70 65 64 2c 20 61 6c 6c 20 61  s dropped, all a
3766a 73 73 6f 63 69 61 74 65 64 20 74 72 69 67 67 65  ssociated trigge
3766b 72 73 20 61 72 65 0d 0a 20 20 20 20 20 20 2a 2a  rs are..      **
3766c 20 64 72 6f 70 70 65 64 20 74 6f 6f 2e 20 20 42   dropped too.  B
3766d 75 74 20 69 66 20 61 20 54 45 4d 50 20 74 72 69  ut if a TEMP tri
3766e 67 67 65 72 20 69 73 20 63 72 65 61 74 65 64 20  gger is created 
3766f 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d 50 20 74 61  on a non-TEMP ta
37670 62 6c 65 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e  ble..      ** an
37671 64 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64  d the table is d
37672 72 6f 70 70 65 64 20 62 79 20 61 20 64 69 66 66  ropped by a diff
37673 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63  erent database c
37674 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 0d 0a  onnection, the..
37675 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72        ** trigger
37676 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 20   is not visible 
37677 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
37678 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
37679 64 6f 65 73 20 74 68 65 0d 0a 20 20 20 20 20 20  does the..      
3767a 2a 2a 20 64 72 6f 70 20 73 6f 20 74 68 65 20 74  ** drop so the t
3767b 72 69 67 67 65 72 20 63 61 6e 6e 6f 74 20 62 65  rigger cannot be
3767c 20 64 72 6f 70 70 65 64 2e 20 20 54 68 69 73 20   dropped.  This 
3767d 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0d 0a 20  results in an.. 
3767e 20 20 20 20 20 2a 2a 20 22 6f 72 70 68 61 6e 65       ** "orphane
3767f 64 20 74 72 69 67 67 65 72 22 20 2d 20 61 20 74  d trigger" - a t
37680 72 69 67 67 65 72 20 77 68 6f 73 65 20 61 73 73  rigger whose ass
37681 6f 63 69 61 74 65 64 20 74 61 62 6c 65 20 69 73  ociated table is
37682 20 6d 69 73 73 69 6e 67 2e 0d 0a 20 20 20 20 20   missing...     
37683 20 2a 2f 0d 0a 20 20 20 20 20 20 64 62 2d 3e 69   */..      db->i
37684 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65  nit.orphanTrigge
37685 72 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  r = 1;..    }.. 
37686 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
37687 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 20  cleanup;..  }.. 
37688 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
37689 54 61 62 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Tab) ){..    sql
3768a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
3768b 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65  rse, "cannot cre
3768c 61 74 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20  ate triggers on 
3768d 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 22 29  virtual tables")
3768e 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  ;..    goto trig
3768f 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20  ger_cleanup;..  
37690 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  }....  /* Check 
37691 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65 72  that the trigger
37692 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 72 65 73   name is not res
37693 65 72 76 65 64 20 61 6e 64 20 74 68 61 74 20 6e  erved and that n
37694 6f 20 74 72 69 67 67 65 72 20 6f 66 20 74 68 65  o trigger of the
37695 0d 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64  ..  ** specified
37696 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a 2f 0d   name exists */.
37697 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
37698 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
37699 64 62 2c 20 70 4e 61 6d 65 29 3b 0d 0a 20 20 69  db, pName);..  i
3769a 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c  f( !zName || SQL
3769b 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43  ITE_OK!=sqlite3C
3769c 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70  heckObjectName(p
3769d 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  Parse, zName) ){
3769e 0d 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  ..    goto trigg
3769f 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  er_cleanup;..  }
376a0 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
376a1 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
376a2 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29  ld(db, iDb, 0) )
376a3 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
376a4 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61  HashFind(&(db->a
376a5 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
376a6 3e 74 72 69 67 48 61 73 68 29 2c 0d 0a 20 20 20  >trigHash),..   
376a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376a8 20 20 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65     zName, sqlite
376a9 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
376aa 29 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 21 6e  ) ){..    if( !n
376ab 6f 45 72 72 20 29 7b 0d 0a 20 20 20 20 20 20 73  oErr ){..      s
376ac 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
376ad 50 61 72 73 65 2c 20 22 74 72 69 67 67 65 72 20  Parse, "trigger 
376ae 25 54 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  %T already exist
376af 73 22 2c 20 70 4e 61 6d 65 29 3b 0d 0a 20 20 20  s", pName);..   
376b0 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61   }else{..      a
376b1 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74  ssert( !db->init
376b2 2e 62 75 73 79 20 29 3b 0d 0a 20 20 20 20 20 20  .busy );..      
376b3 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
376b4 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
376b5 69 44 62 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  iDb);..    }..  
376b6 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
376b7 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 0d 0a  leanup;..  }....
376b8 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61    /* Do not crea
376b9 74 65 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20  te a trigger on 
376ba 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a  a system table *
376bb 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
376bc 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a  StrNICmp(pTab->z
376bd 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c  Name, "sqlite_",
376be 20 37 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73   7)==0 ){..    s
376bf 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
376c0 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
376c1 72 65 61 74 65 20 74 72 69 67 67 65 72 20 6f 6e  reate trigger on
376c2 20 73 79 73 74 65 6d 20 74 61 62 6c 65 22 29 3b   system table");
376c3 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45  ..    pParse->nE
376c4 72 72 2b 2b 3b 0d 0a 20 20 20 20 67 6f 74 6f 20  rr++;..    goto 
376c5 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
376c6 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 4e  ..  }....  /* IN
376c7 53 54 45 41 44 20 6f 66 20 74 72 69 67 67 65 72  STEAD of trigger
376c8 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f 72 20 76  s are only for v
376c9 69 65 77 73 20 61 6e 64 20 76 69 65 77 73 20 6f  iews and views o
376ca 6e 6c 79 20 73 75 70 70 6f 72 74 20 49 4e 53 54  nly support INST
376cb 45 41 44 0d 0a 20 20 2a 2a 20 6f 66 20 74 72 69  EAD..  ** of tri
376cc 67 67 65 72 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ggers...  */..  
376cd 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
376ce 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49  t && tr_tm!=TK_I
376cf 4e 53 54 45 41 44 20 29 7b 0d 0a 20 20 20 20 73  NSTEAD ){..    s
376d0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
376d1 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
376d2 72 65 61 74 65 20 25 73 20 74 72 69 67 67 65 72  reate %s trigger
376d3 20 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20 0d   on view: %S", .
376d4 0a 20 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20  .        (tr_tm 
376d5 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42  == TK_BEFORE)?"B
376d6 45 46 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20  EFORE":"AFTER", 
376d7 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0d  pTableName, 0);.
376d8 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
376d9 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d  r_cleanup;..  }.
376da 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53  .  if( !pTab->pS
376db 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d  elect && tr_tm==
376dc 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0d 0a 20  TK_INSTEAD ){.. 
376dd 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
376de 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
376df 6f 74 20 63 72 65 61 74 65 20 49 4e 53 54 45 41  ot create INSTEA
376e0 44 20 4f 46 22 0d 0a 20 20 20 20 20 20 20 20 22  D OF"..        "
376e1 20 74 72 69 67 67 65 72 20 6f 6e 20 74 61 62 6c   trigger on tabl
376e2 65 3a 20 25 53 22 2c 20 70 54 61 62 6c 65 4e 61  e: %S", pTableNa
376e3 6d 65 2c 20 30 29 3b 0d 0a 20 20 20 20 67 6f 74  me, 0);..    got
376e4 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
376e5 70 3b 0d 0a 20 20 7d 0d 0a 20 20 69 54 61 62 44  p;..  }..  iTabD
376e6 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
376e7 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
376e8 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a 0d 0a  b->pSchema);....
376e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
376ea 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
376eb 4e 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 6e 74 20  N..  {..    int 
376ec 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
376ed 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0d 0a 20  EATE_TRIGGER;.. 
376ee 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
376ef 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54 61  Db = db->aDb[iTa
376f0 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20  bDb].zName;..   
376f1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
376f2 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20  Trig = isTemp ? 
376f3 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65  db->aDb[1].zName
376f4 20 3a 20 7a 44 62 3b 0d 0a 20 20 20 20 69 66 28   : zDb;..    if(
376f5 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73   iTabDb==1 || is
376f6 54 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53 51  Temp ) code = SQ
376f7 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
376f8 5f 54 52 49 47 47 45 52 3b 0d 0a 20 20 20 20 69  _TRIGGER;..    i
376f9 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
376fa 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
376fb 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  , zName, pTab->z
376fc 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29  Name, zDbTrig) )
376fd 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  {..      goto tr
376fe 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a  igger_cleanup;..
376ff 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 73      }..    if( s
37700 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
37701 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
37702 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
37703 42 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a 44  BLE(iTabDb),0,zD
37704 62 29 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  b)){..      goto
37705 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
37706 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23  ;..    }..  }..#
37707 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 49 4e  endif....  /* IN
37708 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72  STEAD OF trigger
37709 73 20 63 61 6e 20 6f 6e 6c 79 20 61 70 70 65 61  s can only appea
3770a 72 20 6f 6e 20 76 69 65 77 73 20 61 6e 64 20 42  r on views and B
3770b 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 0d 0a  EFORE triggers..
3770c 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 61 70 70 65    ** cannot appe
3770d 61 72 20 6f 6e 20 76 69 65 77 73 2e 20 20 53 6f  ar on views.  So
3770e 20 77 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c   we might as wel
3770f 6c 20 74 72 61 6e 73 6c 61 74 65 20 65 76 65 72  l translate ever
37710 79 0d 0a 20 20 2a 2a 20 49 4e 53 54 45 41 44 20  y..  ** INSTEAD 
37711 4f 46 20 74 72 69 67 67 65 72 20 69 6e 74 6f 20  OF trigger into 
37712 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  a BEFORE trigger
37713 2e 20 20 49 74 20 73 69 6d 70 6c 69 66 69 65 73  .  It simplifies
37714 20 63 6f 64 65 0d 0a 20 20 2a 2a 20 65 6c 73 65   code..  ** else
37715 77 68 65 72 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  where...  */..  
37716 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f  if (tr_tm == TK_
37717 49 4e 53 54 45 41 44 29 7b 0d 0a 20 20 20 20 74  INSTEAD){..    t
37718 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45  r_tm = TK_BEFORE
37719 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 42  ;..  }....  /* B
3771a 75 69 6c 64 20 74 68 65 20 54 72 69 67 67 65 72  uild the Trigger
3771b 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 70 54   object */..  pT
3771c 72 69 67 67 65 72 20 3d 20 28 54 72 69 67 67 65  rigger = (Trigge
3771d 72 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  r*)sqlite3DbMall
3771e 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
3771f 66 28 54 72 69 67 67 65 72 29 29 3b 0d 0a 20 20  f(Trigger));..  
37720 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20  if( pTrigger==0 
37721 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63  ) goto trigger_c
37722 6c 65 61 6e 75 70 3b 0d 0a 20 20 70 54 72 69 67  leanup;..  pTrig
37723 67 65 72 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61  ger->zName = zNa
37724 6d 65 3b 0d 0a 20 20 7a 4e 61 6d 65 20 3d 20 30  me;..  zName = 0
37725 3b 0d 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74  ;..  pTrigger->t
37726 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  able = sqlite3Db
37727 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c  StrDup(db, pTabl
37728 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  eName->a[0].zNam
37729 65 29 3b 0d 0a 20 20 70 54 72 69 67 67 65 72 2d  e);..  pTrigger-
3772a 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
3772b 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
3772c 0d 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 54  ..  pTrigger->pT
3772d 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d  abSchema = pTab-
3772e 3e 70 53 63 68 65 6d 61 3b 0d 0a 20 20 70 54 72  >pSchema;..  pTr
3772f 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28 75 38 29  igger->op = (u8)
37730 6f 70 3b 0d 0a 20 20 70 54 72 69 67 67 65 72 2d  op;..  pTrigger-
37731 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74 6d 3d 3d  >tr_tm = tr_tm==
37732 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54 52 49 47  TK_BEFORE ? TRIG
37733 47 45 52 5f 42 45 46 4f 52 45 20 3a 20 54 52 49  GER_BEFORE : TRI
37734 47 47 45 52 5f 41 46 54 45 52 3b 0d 0a 20 20 70  GGER_AFTER;..  p
37735 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 3d  Trigger->pWhen =
37736 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
37737 64 62 2c 20 70 57 68 65 6e 2c 20 45 58 50 52 44  db, pWhen, EXPRD
37738 55 50 5f 52 45 44 55 43 45 29 3b 0d 0a 20 20 70  UP_REDUCE);..  p
37739 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e  Trigger->pColumn
3773a 73 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  s = sqlite3IdLis
3773b 74 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  tDup(db, pColumn
3773c 73 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  s);..  assert( p
3773d 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
3773e 65 72 3d 3d 30 20 29 3b 0d 0a 20 20 70 50 61 72  er==0 );..  pPar
3773f 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
37740 3d 20 70 54 72 69 67 67 65 72 3b 0d 0a 0d 0a 74  = pTrigger;....t
37741 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0d  rigger_cleanup:.
37742 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
37743 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20  (db, zName);..  
37744 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
37745 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 4e  lete(db, pTableN
37746 61 6d 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ame);..  sqlite3
37747 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
37748 20 70 43 6f 6c 75 6d 6e 73 29 3b 0d 0a 20 20 73   pColumns);..  s
37749 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
3774a 28 64 62 2c 20 70 57 68 65 6e 29 3b 0d 0a 20 20  (db, pWhen);..  
3774b 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65  if( !pParse->pNe
3774c 77 54 72 69 67 67 65 72 20 29 7b 0d 0a 20 20 20  wTrigger ){..   
3774d 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
3774e 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 67  igger(db, pTrigg
3774f 65 72 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  er);..  }else{..
37750 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
37751 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d  se->pNewTrigger=
37752 3d 70 54 72 69 67 67 65 72 20 29 3b 0d 0a 20 20  =pTrigger );..  
37753 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  }..}..../*..** T
37754 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
37755 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c 6c 20  alled after all 
37756 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61  of the trigger a
37757 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e  ctions have been
37758 20 70 61 72 73 65 64 0d 0a 2a 2a 20 69 6e 20 6f   parsed..** in o
37759 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65  rder to complete
3775a 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20   the process of 
3775b 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72 69  building the tri
3775c 67 67 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  gger...*/..SQLIT
3775d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3775e 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
3775f 67 65 72 28 0d 0a 20 20 50 61 72 73 65 20 2a 70  ger(..  Parse *p
37760 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
37761 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
37762 74 20 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 53  t */..  TriggerS
37763 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20  tep *pStepList, 
37764 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 65 64  /* The triggered
37765 20 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20 20 54   program */..  T
37766 6f 6b 65 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20  oken *pAll      
37767 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
37768 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
37769 68 65 20 63 6f 6d 70 6c 65 74 65 20 43 52 45 41  he complete CREA
3776a 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0d 0a 29  TE TRIGGER */..)
3776b 7b 0d 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  {..  Trigger *pT
3776c 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  rig = pParse->pN
3776d 65 77 54 72 69 67 67 65 72 3b 20 20 20 2f 2a 20  ewTrigger;   /* 
3776e 54 72 69 67 67 65 72 20 62 65 69 6e 67 20 66 69  Trigger being fi
3776f 6e 69 73 68 65 64 20 2a 2f 0d 0a 20 20 63 68 61  nished */..  cha
37770 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
37771 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37772 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
37773 74 72 69 67 67 65 72 20 2a 2f 0d 0a 20 20 73 71  trigger */..  sq
37774 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
37775 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20  se->db;         
37776 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
37777 61 62 61 73 65 20 2a 2f 0d 0a 20 20 44 62 46 69  abase */..  DbFi
37778 78 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20  xer sFix;       
37779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3777a 20 20 20 20 2f 2a 20 46 69 78 65 72 20 6f 62 6a      /* Fixer obj
3777b 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44  ect */..  int iD
3777c 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
3777d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3777e 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
3777f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69  ntaining the tri
37780 67 67 65 72 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e  gger */..  Token
37781 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20   nameToken;     
37782 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37783 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6e 61     /* Trigger na
37784 6d 65 20 66 6f 72 20 65 72 72 6f 72 20 72 65 70  me for error rep
37785 6f 72 74 69 6e 67 20 2a 2f 0d 0a 0d 0a 20 20 70  orting */....  p
37786 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
37787 65 72 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 4e  er = 0;..  if( N
37788 45 56 45 52 28 70 50 61 72 73 65 2d 3e 6e 45 72  EVER(pParse->nEr
37789 72 29 20 7c 7c 20 21 70 54 72 69 67 20 29 20 67  r) || !pTrig ) g
3778a 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
3778b 68 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7a 4e  h_cleanup;..  zN
3778c 61 6d 65 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61  ame = pTrig->zNa
3778d 6d 65 3b 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c  me;..  iDb = sql
3778e 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
3778f 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
37790 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0d 0a  rig->pSchema);..
37791 20 20 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69    pTrig->step_li
37792 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74 3b 0d  st = pStepList;.
37793 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 4c  .  while( pStepL
37794 69 73 74 20 29 7b 0d 0a 20 20 20 20 70 53 74 65  ist ){..    pSte
37795 70 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d 20 70  pList->pTrig = p
37796 54 72 69 67 3b 0d 0a 20 20 20 20 70 53 74 65 70  Trig;..    pStep
37797 4c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74  List = pStepList
37798 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 20  ->pNext;..  }.. 
37799 20 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20 3d 20 70   nameToken.z = p
3779a 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20 20  Trig->zName;..  
3779b 6e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71  nameToken.n = sq
3779c 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 6e 61  lite3Strlen30(na
3779d 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0d 0a 20 20 69  meToken.z);..  i
3779e 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
3779f 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
377a0 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
377a1 20 26 6e 61 6d 65 54 6f 6b 65 6e 29 20 0d 0a 20   &nameToken) .. 
377a2 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
377a3 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
377a4 70 28 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e  p(&sFix, pTrig->
377a5 73 74 65 70 5f 6c 69 73 74 29 20 29 7b 0d 0a 20  step_list) ){.. 
377a6 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66     goto triggerf
377a7 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0d 0a  inish_cleanup;..
377a8 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 69 66 20 77    }....  /* if w
377a9 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74 69 61  e are not initia
377aa 6c 69 7a 69 6e 67 2c 0d 0a 20 20 2a 2a 20 62 75  lizing,..  ** bu
377ab 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ild the sqlite_m
377ac 61 73 74 65 72 20 65 6e 74 72 79 0d 0a 20 20 2a  aster entry..  *
377ad 2f 0d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  /..  if( !db->in
377ae 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20 20 20  it.busy ){..    
377af 56 64 62 65 20 2a 76 3b 0d 0a 20 20 20 20 63 68  Vdbe *v;..    ch
377b0 61 72 20 2a 7a 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  ar *z;....    /*
377b1 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 69   Make an entry i
377b2 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
377b3 74 65 72 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  ter table */..  
377b4 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
377b5 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a 20  Vdbe(pParse);.. 
377b6 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f     if( v==0 ) go
377b7 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
377b8 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20 20 73  _cleanup;..    s
377b9 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
377ba 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
377bb 2c 20 30 2c 20 69 44 62 29 3b 0d 0a 20 20 20 20  , 0, iDb);..    
377bc 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  z = sqlite3DbStr
377bd 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
377be 70 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e  pAll->z, pAll->n
377bf 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4e  );..    sqlite3N
377c0 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
377c1 65 2c 0d 0a 20 20 20 20 20 20 20 22 49 4e 53 45  e,..       "INSE
377c2 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41  RT INTO %Q.%s VA
377c3 4c 55 45 53 28 27 74 72 69 67 67 65 72 27 2c 25  LUES('trigger',%
377c4 51 2c 25 51 2c 30 2c 27 43 52 45 41 54 45 20 54  Q,%Q,0,'CREATE T
377c5 52 49 47 47 45 52 20 25 71 27 29 22 2c 0d 0a 20  RIGGER %q')",.. 
377c6 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
377c7 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
377c8 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61  _TABLE(iDb), zNa
377c9 6d 65 2c 0d 0a 20 20 20 20 20 20 20 70 54 72 69  me,..       pTri
377ca 67 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b 0d 0a 20  g->table, z);.. 
377cb 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
377cc 28 64 62 2c 20 7a 29 3b 0d 0a 20 20 20 20 73 71  (db, z);..    sq
377cd 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
377ce 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d  e(pParse, iDb);.
377cf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
377d0 41 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70  AddParseSchemaOp
377d1 28 76 2c 20 69 44 62 2c 0d 0a 20 20 20 20 20 20  (v, iDb,..      
377d2 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66    sqlite3MPrintf
377d3 28 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67  (db, "type='trig
377d4 67 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25  ger' AND name='%
377d5 71 27 22 2c 20 7a 4e 61 6d 65 29 29 3b 0d 0a 20  q'", zName));.. 
377d6 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 64 62 2d 3e   }....  if( db->
377d7 69 6e 69 74 2e 62 75 73 79 20 29 7b 0d 0a 20 20  init.busy ){..  
377d8 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 6e 6b    Trigger *pLink
377d9 20 3d 20 70 54 72 69 67 3b 0d 0a 20 20 20 20 48   = pTrig;..    H
377da 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62  ash *pHash = &db
377db 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
377dc 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0d 0a 20  ma->trigHash;.. 
377dd 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
377de 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c  e3SchemaMutexHel
377df 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b  d(db, iDb, 0) );
377e0 0d 0a 20 20 20 20 70 54 72 69 67 20 3d 20 73 71  ..    pTrig = sq
377e1 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
377e2 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71  pHash, zName, sq
377e3 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
377e4 61 6d 65 29 2c 20 70 54 72 69 67 29 3b 0d 0a 20  ame), pTrig);.. 
377e5 20 20 20 69 66 28 20 70 54 72 69 67 20 29 7b 0d     if( pTrig ){.
377e6 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
377e7 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20  cFailed = 1;..  
377e8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 69 6e    }else if( pLin
377e9 6b 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 4c 69 6e  k->pSchema==pLin
377ea 6b 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29 7b  k->pTabSchema ){
377eb 0d 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  ..      Table *p
377ec 54 61 62 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20  Tab;..      int 
377ed 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
377ee 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65  n30(pLink->table
377ef 29 3b 0d 0a 20 20 20 20 20 20 70 54 61 62 20 3d  );..      pTab =
377f0 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
377f1 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68  (&pLink->pTabSch
377f2 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 4c  ema->tblHash, pL
377f3 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0d  ink->table, n);.
377f4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
377f5 54 61 62 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20  Tab!=0 );..     
377f6 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20   pLink->pNext = 
377f7 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0d  pTab->pTrigger;.
377f8 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72  .      pTab->pTr
377f9 69 67 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0d 0a  igger = pLink;..
377fa 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 74 72      }..  }....tr
377fb 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
377fc 6e 75 70 3a 0d 0a 20 20 73 71 6c 69 74 65 33 44  nup:..  sqlite3D
377fd 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c  eleteTrigger(db,
377fe 20 70 54 72 69 67 29 3b 0d 0a 20 20 61 73 73 65   pTrig);..  asse
377ff 72 74 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65  rt( !pParse->pNe
37800 77 54 72 69 67 67 65 72 20 29 3b 0d 0a 20 20 73  wTrigger );..  s
37801 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
37802 67 65 72 53 74 65 70 28 64 62 2c 20 70 53 74 65  gerStep(db, pSte
37803 70 4c 69 73 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  pList);..}..../*
37804 0d 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45  ..** Turn a SELE
37805 43 54 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68  CT statement (th
37806 61 74 20 74 68 65 20 70 53 65 6c 65 63 74 20 70  at the pSelect p
37807 61 72 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20  arameter points 
37808 74 6f 29 20 69 6e 74 6f 0d 0a 2a 2a 20 61 20 74  to) into..** a t
37809 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 52 65  rigger step.  Re
3780a 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
3780b 6f 20 61 20 54 72 69 67 67 65 72 53 74 65 70 20  o a TriggerStep 
3780c 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2a 0d 0a  structure...**..
3780d 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
3780e 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
3780f 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20 61   when it finds a
37810 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
37811 74 20 69 6e 0d 0a 2a 2a 20 62 6f 64 79 20 6f 66  t in..** body of
37812 20 61 20 54 52 49 47 47 45 52 2e 20 20 0d 0a 2a   a TRIGGER.  ..*
37813 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
37814 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
37815 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c  qlite3TriggerSel
37816 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 20  ectStep(sqlite3 
37817 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 53 65  *db, Select *pSe
37818 6c 65 63 74 29 7b 0d 0a 20 20 54 72 69 67 67 65  lect){..  Trigge
37819 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
3781a 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  tep = sqlite3DbM
3781b 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
3781c 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
3781d 29 29 3b 0d 0a 20 20 69 66 28 20 70 54 72 69 67  ));..  if( pTrig
3781e 67 65 72 53 74 65 70 3d 3d 30 20 29 20 7b 0d 0a  gerStep==0 ) {..
3781f 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
37820 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
37821 65 63 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  ect);..    retur
37822 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54 72  n 0;..  }..  pTr
37823 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
37824 54 4b 5f 53 45 4c 45 43 54 3b 0d 0a 20 20 70 54  TK_SELECT;..  pT
37825 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
37826 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0d 0a  ect = pSelect;..
37827 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
37828 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61  orconf = OE_Defa
37829 75 6c 74 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70  ult;..  return p
3782a 54 72 69 67 67 65 72 53 74 65 70 3b 0d 0a 7d 0d  TriggerStep;..}.
3782b 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61  .../*..** Alloca
3782c 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  te space to hold
3782d 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 73   a new trigger s
3782e 74 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61  tep.  The alloca
3782f 74 65 64 20 73 70 61 63 65 0d 0a 2a 2a 20 68 6f  ted space..** ho
37830 6c 64 73 20 62 6f 74 68 20 74 68 65 20 54 72 69  lds both the Tri
37831 67 67 65 72 53 74 65 70 20 6f 62 6a 65 63 74 20  ggerStep object 
37832 61 6e 64 20 74 68 65 20 54 72 69 67 67 65 72 53  and the TriggerS
37833 74 65 70 2e 74 61 72 67 65 74 2e 7a 20 73 74 72  tep.target.z str
37834 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ing...**..** If 
37835 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63  an OOM error occ
37836 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  urs, NULL is ret
37837 75 72 6e 65 64 20 61 6e 64 20 64 62 2d 3e 6d 61  urned and db->ma
37838 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 73 65  llocFailed is se
37839 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 54  t...*/..static T
3783a 72 69 67 67 65 72 53 74 65 70 20 2a 74 72 69 67  riggerStep *trig
3783b 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
3783c 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
3783d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3783e 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
3783f 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 75 38 20  ection */..  u8 
37840 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
37841 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67           /* Trig
37842 67 65 72 20 6f 70 63 6f 64 65 20 2a 2f 0d 0a 20  ger opcode */.. 
37843 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20   Token *pName   
37844 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37845 54 68 65 20 74 61 72 67 65 74 20 6e 61 6d 65 20  The target name 
37846 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 72 69 67 67 65  */..){..  Trigge
37847 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
37848 74 65 70 3b 0d 0a 0d 0a 20 20 70 54 72 69 67 67  tep;....  pTrigg
37849 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33  erStep = sqlite3
3784a 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
3784b 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
3784c 74 65 70 29 20 2b 20 70 4e 61 6d 65 2d 3e 6e 29  tep) + pName->n)
3784d 3b 0d 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  ;..  if( pTrigge
3784e 72 53 74 65 70 20 29 7b 0d 0a 20 20 20 20 63 68  rStep ){..    ch
3784f 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 26  ar *z = (char*)&
37850 70 54 72 69 67 67 65 72 53 74 65 70 5b 31 5d 3b  pTriggerStep[1];
37851 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20  ..    memcpy(z, 
37852 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d  pName->z, pName-
37853 3e 6e 29 3b 0d 0a 20 20 20 20 70 54 72 69 67 67  >n);..    pTrigg
37854 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a  erStep->target.z
37855 20 3d 20 7a 3b 0d 0a 20 20 20 20 70 54 72 69 67   = z;..    pTrig
37856 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e  gerStep->target.
37857 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0d 0a 20  n = pName->n;.. 
37858 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
37859 3e 6f 70 20 3d 20 6f 70 3b 0d 0a 20 20 7d 0d 0a  >op = op;..  }..
3785a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65    return pTrigge
3785b 72 53 74 65 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rStep;..}..../*.
3785c 0a 2a 2a 20 42 75 69 6c 64 20 61 20 74 72 69 67  .** Build a trig
3785d 67 65 72 20 73 74 65 70 20 6f 75 74 20 6f 66 20  ger step out of 
3785e 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  an INSERT statem
3785f 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ent.  Return a p
37860 6f 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68  ointer..** to th
37861 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 74  e new trigger st
37862 65 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ep...**..** The 
37863 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
37864 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
37865 74 20 73 65 65 73 20 61 6e 20 49 4e 53 45 52 54  t sees an INSERT
37866 20 69 6e 73 69 64 65 20 74 68 65 0d 0a 2a 2a 20   inside the..** 
37867 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 65  body of a trigge
37868 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  r...*/..SQLITE_P
37869 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74  RIVATE TriggerSt
3786a 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
3786b 65 72 49 6e 73 65 72 74 53 74 65 70 28 0d 0a 20  erInsertStep(.. 
3786c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
3786d 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
3786e 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3786f 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61  */..  Token *pTa
37870 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d  bleName,  /* Nam
37871 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
37872 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69 6e 73  nto which we ins
37873 65 72 74 20 2a 2f 0d 0a 20 20 49 64 4c 69 73 74  ert */..  IdList
37874 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a   *pColumn,    /*
37875 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
37876 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20 74   in pTableName t
37877 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f  o insert into */
37878 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ..  ExprList *pE
37879 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56  List,   /* The V
3787a 41 4c 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c  ALUE clause: a l
3787b 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
3787c 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0d   be inserted */.
3787d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
3787e 63 74 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c 45  ct,    /* A SELE
3787f 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  CT statement tha
37880 74 20 73 75 70 70 6c 69 65 73 20 76 61 6c 75 65  t supplies value
37881 73 20 2a 2f 0d 0a 20 20 75 38 20 6f 72 63 6f 6e  s */..  u8 orcon
37882 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  f           /* T
37883 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f  he conflict algo
37884 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c  rithm (OE_Abort,
37885 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 65 74 63   OE_Replace, etc
37886 2e 29 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 72 69  .) */..){..  Tri
37887 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
37888 65 72 53 74 65 70 3b 0d 0a 0d 0a 20 20 61 73 73  erStep;....  ass
37889 65 72 74 28 70 45 4c 69 73 74 20 3d 3d 20 30 20  ert(pEList == 0 
3788a 7c 7c 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29  || pSelect == 0)
3788b 3b 0d 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69  ;..  assert(pELi
3788c 73 74 20 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65  st != 0 || pSele
3788d 63 74 20 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d  ct != 0 || db->m
3788e 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0d 0a 0d  allocFailed);...
3788f 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20  .  pTriggerStep 
37890 3d 20 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c  = triggerStepAll
37891 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 49 4e 53  ocate(db, TK_INS
37892 45 52 54 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29  ERT, pTableName)
37893 3b 0d 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  ;..  if( pTrigge
37894 72 53 74 65 70 20 29 7b 0d 0a 20 20 20 20 70 54  rStep ){..    pT
37895 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
37896 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
37897 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65  ectDup(db, pSele
37898 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ct, EXPRDUP_REDU
37899 43 45 29 3b 0d 0a 20 20 20 20 70 54 72 69 67 67  CE);..    pTrigg
3789a 65 72 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20  erStep->pIdList 
3789b 3d 20 70 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20  = pColumn;..    
3789c 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
3789d 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  xprList = sqlite
3789e 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
3789f 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50   pEList, EXPRDUP
378a0 5f 52 45 44 55 43 45 29 3b 0d 0a 20 20 20 20 70  _REDUCE);..    p
378a1 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63  TriggerStep->orc
378a2 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0d 0a 20  onf = orconf;.. 
378a3 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
378a4 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
378a5 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0d 0a  (db, pColumn);..
378a6 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 45 78    }..  sqlite3Ex
378a7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
378a8 20 70 45 4c 69 73 74 29 3b 0d 0a 20 20 73 71 6c   pEList);..  sql
378a9 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
378aa 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a  (db, pSelect);..
378ab 0d 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67  ..  return pTrig
378ac 67 65 72 53 74 65 70 3b 0d 0a 7d 0d 0a 0d 0a 2f  gerStep;..}..../
378ad 2a 0d 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  *..** Construct 
378ae 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20 74  a trigger step t
378af 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  hat implements a
378b0 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  n UPDATE stateme
378b1 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0d 0a 2a  nt and return..*
378b2 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
378b3 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65 70  hat trigger step
378b4 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61  .  The parser ca
378b5 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
378b6 20 77 68 65 6e 20 69 74 0d 0a 2a 2a 20 73 65 65   when it..** see
378b7 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  s an UPDATE stat
378b8 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65  ement inside the
378b9 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54   body of a CREAT
378ba 45 20 54 52 49 47 47 45 52 2e 0d 0a 2a 2f 0d 0a  E TRIGGER...*/..
378bb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
378bc 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
378bd 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65  te3TriggerUpdate
378be 53 74 65 70 28 0d 0a 20 20 73 71 6c 69 74 65 33  Step(..  sqlite3
378bf 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
378c0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
378c1 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 54  nnection */..  T
378c2 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
378c3 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ,   /* Name of t
378c4 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75  he table to be u
378c5 70 64 61 74 65 64 20 2a 2f 0d 0a 20 20 45 78 70  pdated */..  Exp
378c6 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
378c7 20 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61    /* The SET cla
378c8 75 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c  use: list of col
378c9 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75  umn and new valu
378ca 65 73 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  es */..  Expr *p
378cb 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a  Where,        /*
378cc 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
378cd 65 20 2a 2f 0d 0a 20 20 75 38 20 6f 72 63 6f 6e  e */..  u8 orcon
378ce 66 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f            /* 
378cf 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67  The conflict alg
378d0 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72  orithm. (OE_Abor
378d1 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74  t, OE_Ignore, et
378d2 63 29 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54 72 69  c) */..){..  Tri
378d3 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
378d4 65 72 53 74 65 70 3b 0d 0a 0d 0a 20 20 70 54 72  erStep;....  pTr
378d5 69 67 67 65 72 53 74 65 70 20 3d 20 74 72 69 67  iggerStep = trig
378d6 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
378d7 64 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70  db, TK_UPDATE, p
378d8 54 61 62 6c 65 4e 61 6d 65 29 3b 0d 0a 20 20 69  TableName);..  i
378d9 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20  f( pTriggerStep 
378da 29 7b 0d 0a 20 20 20 20 70 54 72 69 67 67 65 72  ){..    pTrigger
378db 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20  Step->pExprList 
378dc 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
378dd 74 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2c  tDup(db, pEList,
378de 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
378df 3b 0d 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  ;..    pTriggerS
378e0 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71  tep->pWhere = sq
378e1 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
378e2 20 70 57 68 65 72 65 2c 20 45 58 50 52 44 55 50   pWhere, EXPRDUP
378e3 5f 52 45 44 55 43 45 29 3b 0d 0a 20 20 20 20 70  _REDUCE);..    p
378e4 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63  TriggerStep->orc
378e5 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0d 0a 20  onf = orconf;.. 
378e6 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70   }..  sqlite3Exp
378e7 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
378e8 70 45 4c 69 73 74 29 3b 0d 0a 20 20 73 71 6c 69  pEList);..  sqli
378e9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
378ea 2c 20 70 57 68 65 72 65 29 3b 0d 0a 20 20 72 65  , pWhere);..  re
378eb 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
378ec 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  p;..}..../*..** 
378ed 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69 67  Construct a trig
378ee 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69 6d  ger step that im
378ef 70 6c 65 6d 65 6e 74 73 20 61 20 44 45 4c 45 54  plements a DELET
378f0 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  E statement and 
378f1 72 65 74 75 72 6e 0d 0a 2a 2a 20 61 20 70 6f 69  return..** a poi
378f2 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69  nter to that tri
378f3 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20  gger step.  The 
378f4 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
378f5 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
378f6 74 0d 0a 2a 2a 20 73 65 65 73 20 61 20 44 45 4c  t..** sees a DEL
378f7 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ETE statement in
378f8 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66  side the body of
378f9 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45   a CREATE TRIGGE
378fa 52 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  R...*/..SQLITE_P
378fb 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74  RIVATE TriggerSt
378fc 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
378fd 65 72 44 65 6c 65 74 65 53 74 65 70 28 0d 0a 20  erDeleteStep(.. 
378fe 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
378ff 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
37900 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
37901 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61  */..  Token *pTa
37902 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 20 2f 2a  bleName,      /*
37903 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   The table from 
37904 77 68 69 63 68 20 72 6f 77 73 20 61 72 65 20 64  which rows are d
37905 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 45 78 70  eleted */..  Exp
37906 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
37907 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
37908 45 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 29 7b 0d  E clause */..){.
37909 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
3790a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0d 0a 0d  pTriggerStep;...
3790b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20  .  pTriggerStep 
3790c 3d 20 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c  = triggerStepAll
3790d 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 44 45 4c  ocate(db, TK_DEL
3790e 45 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29  ETE, pTableName)
3790f 3b 0d 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  ;..  if( pTrigge
37910 72 53 74 65 70 20 29 7b 0d 0a 20 20 20 20 70 54  rStep ){..    pT
37911 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65  riggerStep->pWhe
37912 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
37913 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20  Dup(db, pWhere, 
37914 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
37915 0d 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ..    pTriggerSt
37916 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
37917 44 65 66 61 75 6c 74 3b 0d 0a 20 20 7d 0d 0a 20  Default;..  }.. 
37918 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
37919 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0d  te(db, pWhere);.
3791a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
3791b 65 72 53 74 65 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  erStep;..}..../*
3791c 20 0d 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 6c   ..** Recursivel
3791d 79 20 64 65 6c 65 74 65 20 61 20 54 72 69 67 67  y delete a Trigg
3791e 65 72 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2f  er structure..*/
3791f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
37920 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c   void sqlite3Del
37921 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74  eteTrigger(sqlit
37922 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 20  e3 *db, Trigger 
37923 2a 70 54 72 69 67 67 65 72 29 7b 0d 0a 20 20 69  *pTrigger){..  i
37924 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29  f( pTrigger==0 )
37925 20 72 65 74 75 72 6e 3b 0d 0a 20 20 73 71 6c 69   return;..  sqli
37926 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
37927 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65  Step(db, pTrigge
37928 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b 0d 0a  r->step_list);..
37929 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
3792a 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  db, pTrigger->zN
3792b 61 6d 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ame);..  sqlite3
3792c 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67  DbFree(db, pTrig
3792d 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0d 0a 20 20  ger->table);..  
3792e 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
3792f 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  e(db, pTrigger->
37930 70 57 68 65 6e 29 3b 0d 0a 20 20 73 71 6c 69 74  pWhen);..  sqlit
37931 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
37932 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f  b, pTrigger->pCo
37933 6c 75 6d 6e 73 29 3b 0d 0a 20 20 73 71 6c 69 74  lumns);..  sqlit
37934 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72  e3DbFree(db, pTr
37935 69 67 67 65 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  igger);..}..../*
37936 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
37937 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
37938 64 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 66  drop a trigger f
37939 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
3793a 20 73 63 68 65 6d 61 2e 20 0d 0a 2a 2a 0d 0a 2a   schema. ..**..*
3793b 2a 20 54 68 69 73 20 6d 61 79 20 62 65 20 63 61  * This may be ca
3793c 6c 6c 65 64 20 64 69 72 65 63 74 6c 79 20 66 72  lled directly fr
3793d 6f 6d 20 74 68 65 20 70 61 72 73 65 72 20 61 6e  om the parser an
3793e 64 20 74 68 65 72 65 66 6f 72 65 20 69 64 65 6e  d therefore iden
3793f 74 69 66 69 65 73 0d 0a 2a 2a 20 74 68 65 20 74  tifies..** the t
37940 72 69 67 67 65 72 20 62 79 20 6e 61 6d 65 2e 20  rigger by name. 
37941 20 54 68 65 20 73 71 6c 69 74 65 33 44 72 6f 70   The sqlite3Drop
37942 54 72 69 67 67 65 72 50 74 72 28 29 20 72 6f 75  TriggerPtr() rou
37943 74 69 6e 65 20 64 6f 65 73 20 74 68 65 0d 0a 2a  tine does the..*
37944 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73 20 74 68  * same job as th
37945 69 73 20 72 6f 75 74 69 6e 65 20 65 78 63 65 70  is routine excep
37946 74 20 69 74 20 74 61 6b 65 73 20 61 20 70 6f 69  t it takes a poi
37947 6e 74 65 72 20 74 6f 20 74 68 65 20 74 72 69 67  nter to the trig
37948 67 65 72 0d 0a 2a 2a 20 69 6e 73 74 65 61 64 20  ger..** instead 
37949 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  of the trigger n
3794a 61 6d 65 2e 0d 0a 2a 2a 2f 0d 0a 53 51 4c 49 54  ame...**/..SQLIT
3794b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3794c 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
3794d 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
3794e 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
3794f 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0d 0a 20 20   int noErr){..  
37950 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
37951 72 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69 3b  r = 0;..  int i;
37952 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
37953 7a 44 62 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  zDb;..  const ch
37954 61 72 20 2a 7a 4e 61 6d 65 3b 0d 0a 20 20 69 6e  ar *zName;..  in
37955 74 20 6e 4e 61 6d 65 3b 0d 0a 20 20 73 71 6c 69  t nName;..  sqli
37956 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
37957 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 69 66 28 20 64  ->db;....  if( d
37958 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
37959 29 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67  ) goto drop_trig
3795a 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20  ger_cleanup;..  
3795b 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
3795c 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
3795d 28 70 50 61 72 73 65 29 20 29 7b 0d 0a 20 20 20  (pParse) ){..   
3795e 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67   goto drop_trigg
3795f 65 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  er_cleanup;..  }
37960 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4e  ....  assert( pN
37961 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0d  ame->nSrc==1 );.
37962 0a 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65 2d 3e  .  zDb = pName->
37963 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 3b 0d  a[0].zDatabase;.
37964 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65  .  zName = pName
37965 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20  ->a[0].zName;.. 
37966 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
37967 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
37968 0d 0a 20 20 61 73 73 65 72 74 28 20 7a 44 62 21  ..  assert( zDb!
37969 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  =0 || sqlite3Btr
3796a 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
3796b 73 28 64 62 29 20 29 3b 0d 0a 20 20 66 6f 72 28  s(db) );..  for(
3796c 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
3796d 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d  <db->nDb; i++){.
3796e 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c  .    int j = (i<
3796f 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f  2) ? i^1 : i;  /
37970 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
37971 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0d 0a 20 20  fore MAIN */..  
37972 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c    if( zDb && sql
37973 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e  ite3StrICmp(db->
37974 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 44  aDb[j].zName, zD
37975 62 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  b) ) continue;..
37976 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
37977 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
37978 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0d  ld(db, j, 0) );.
37979 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
3797a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
3797b 26 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63  &(db->aDb[j].pSc
3797c 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 2c  hema->trigHash),
3797d 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0d   zName, nName);.
3797e 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
3797f 72 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d  r ) break;..  }.
37980 0a 20 20 69 66 28 20 21 70 54 72 69 67 67 65 72  .  if( !pTrigger
37981 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 21 6e 6f   ){..    if( !no
37982 45 72 72 20 29 7b 0d 0a 20 20 20 20 20 20 73 71  Err ){..      sq
37983 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
37984 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74  arse, "no such t
37985 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61  rigger: %S", pNa
37986 6d 65 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 65 6c  me, 0);..    }el
37987 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
37988 65 33 43 6f 64 65 56 65 72 69 66 79 4e 61 6d 65  e3CodeVerifyName
37989 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  dSchema(pParse, 
3798a 7a 44 62 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  zDb);..    }..  
3798b 20 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53    pParse->checkS
3798c 63 68 65 6d 61 20 3d 20 31 3b 0d 0a 20 20 20 20  chema = 1;..    
3798d 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65  goto drop_trigge
3798e 72 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d  r_cleanup;..  }.
3798f 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  .  sqlite3DropTr
37990 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c  iggerPtr(pParse,
37991 20 70 54 72 69 67 67 65 72 29 3b 0d 0a 0d 0a 64   pTrigger);....d
37992 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
37993 6e 75 70 3a 0d 0a 20 20 73 71 6c 69 74 65 33 53  nup:..  sqlite3S
37994 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
37995 20 70 4e 61 6d 65 29 3b 0d 0a 7d 0d 0a 0d 0a 2f   pName);..}..../
37996 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  *..** Return a p
37997 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54 61  ointer to the Ta
37998 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  ble structure fo
37999 72 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  r the table that
3799a 20 61 20 74 72 69 67 67 65 72 0d 0a 2a 2a 20 69   a trigger..** i
3799b 73 20 73 65 74 20 6f 6e 2e 0d 0a 2a 2f 0d 0a 73  s set on...*/..s
3799c 74 61 74 69 63 20 54 61 62 6c 65 20 2a 74 61 62  tatic Table *tab
3799d 6c 65 4f 66 54 72 69 67 67 65 72 28 54 72 69 67  leOfTrigger(Trig
3799e 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0d  ger *pTrigger){.
3799f 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74  .  int n = sqlit
379a0 65 33 53 74 72 6c 65 6e 33 30 28 70 54 72 69 67  e3Strlen30(pTrig
379a1 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0d 0a 20 20  ger->table);..  
379a2 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 48 61  return sqlite3Ha
379a3 73 68 46 69 6e 64 28 26 70 54 72 69 67 67 65 72  shFind(&pTrigger
379a4 2d 3e 70 54 61 62 53 63 68 65 6d 61 2d 3e 74 62  ->pTabSchema->tb
379a5 6c 48 61 73 68 2c 20 70 54 72 69 67 67 65 72 2d  lHash, pTrigger-
379a6 3e 74 61 62 6c 65 2c 20 6e 29 3b 0d 0a 7d 0d 0a  >table, n);..}..
379a7 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 72 6f 70 20  ..../*..** Drop 
379a8 61 20 74 72 69 67 67 65 72 20 67 69 76 65 6e 20  a trigger given 
379a9 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
379aa 74 20 74 72 69 67 67 65 72 2e 20 0d 0a 2a 2f 0d  t trigger. ..*/.
379ab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
379ac 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
379ad 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65  TriggerPtr(Parse
379ae 20 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67 65   *pParse, Trigge
379af 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0d 0a 20  r *pTrigger){.. 
379b0 20 54 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65   Table   *pTable
379b1 3b 0d 0a 20 20 56 64 62 65 20 2a 76 3b 0d 0a 20  ;..  Vdbe *v;.. 
379b2 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
379b3 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 69 6e  Parse->db;..  in
379b4 74 20 69 44 62 3b 0d 0a 0d 0a 20 20 69 44 62 20  t iDb;....  iDb 
379b5 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
379b6 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
379b7 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  b, pTrigger->pSc
379b8 68 65 6d 61 29 3b 0d 0a 20 20 61 73 73 65 72 74  hema);..  assert
379b9 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
379ba 64 62 2d 3e 6e 44 62 20 29 3b 0d 0a 20 20 70 54  db->nDb );..  pT
379bb 61 62 6c 65 20 3d 20 74 61 62 6c 65 4f 66 54 72  able = tableOfTr
379bc 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29 3b  igger(pTrigger);
379bd 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
379be 6c 65 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  le );..  assert(
379bf 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
379c0 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68  ==pTrigger->pSch
379c1 65 6d 61 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b  ema || iDb==1 );
379c2 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
379c3 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
379c4 49 4f 4e 0d 0a 20 20 7b 0d 0a 20 20 20 20 69 6e  ION..  {..    in
379c5 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  t code = SQLITE_
379c6 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0d 0a 20  DROP_TRIGGER;.. 
379c7 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
379c8 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
379c9 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20 20 20 63 6f  ].zName;..    co
379ca 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
379cb 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
379cc 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 69 44 62  b);..    if( iDb
379cd 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  ==1 ) code = SQL
379ce 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52  ITE_DROP_TEMP_TR
379cf 49 47 47 45 52 3b 0d 0a 20 20 20 20 69 66 28 20  IGGER;..    if( 
379d0 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
379d1 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70  (pParse, code, p
379d2 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
379d3 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a  pTable->zName, z
379d4 44 62 29 20 7c 7c 0d 0a 20 20 20 20 20 20 73 71  Db) ||..      sq
379d5 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
379d6 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
379d7 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a  LETE, zTab, 0, z
379d8 44 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  Db) ){..      re
379d9 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  turn;..    }..  
379da 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f  }..#endif....  /
379db 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
379dc 74 6f 20 64 65 73 74 72 6f 79 20 74 68 65 20 64  to destroy the d
379dd 61 74 61 62 61 73 65 20 72 65 63 6f 72 64 20 6f  atabase record o
379de 66 20 74 68 65 20 74 72 69 67 67 65 72 2e 0d 0a  f the trigger...
379df 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20    */..  assert( 
379e0 70 54 61 62 6c 65 21 3d 30 20 29 3b 0d 0a 20 20  pTable!=0 );..  
379e1 69 66 28 20 28 76 20 3d 20 73 71 6c 69 74 65 33  if( (v = sqlite3
379e2 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29  GetVdbe(pParse))
379e3 21 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  !=0 ){..    int 
379e4 62 61 73 65 3b 0d 0a 20 20 20 20 73 74 61 74 69  base;..    stati
379e5 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
379e6 73 74 20 64 72 6f 70 54 72 69 67 67 65 72 5b 5d  st dropTrigger[]
379e7 20 3d 20 7b 0d 0a 20 20 20 20 20 20 7b 20 4f 50   = {..      { OP
379e8 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c 20  _Rewind,     0, 
379e9 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0d 0a 20  ADDR(9),  0},.. 
379ea 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
379eb 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20  8,    0, 1,     
379ec 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0d 0a     0}, /* 1 */..
379ed 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d        { OP_Colum
379ee 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  n,     0, 1,    
379ef 20 20 20 20 32 7d 2c 0d 0a 20 20 20 20 20 20 7b      2},..      {
379f0 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20   OP_Ne,         
379f1 32 2c 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c  2, ADDR(8),  1},
379f2 0d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72  ..      { OP_Str
379f3 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20  ing8,    0, 1,  
379f4 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20        0}, /* 4: 
379f5 22 74 72 69 67 67 65 72 22 20 2a 2f 0d 0a 20 20  "trigger" */..  
379f6 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
379f7 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
379f8 20 20 32 7d 2c 0d 0a 20 20 20 20 20 20 7b 20 4f    2},..      { O
379f9 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c  P_Ne,         2,
379fa 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0d 0a   ADDR(8),  1},..
379fb 20 20 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74        { OP_Delet
379fc 65 2c 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20  e,     0, 0,    
379fd 20 20 20 20 30 7d 2c 0d 0a 20 20 20 20 20 20 7b      0},..      {
379fe 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20 20 20   OP_Next,       
379ff 30 2c 20 41 44 44 52 28 31 29 2c 20 20 30 7d 2c  0, ADDR(1),  0},
37a00 20 2f 2a 20 38 20 2a 2f 0d 0a 20 20 20 20 7d 3b   /* 8 */..    };
37a01 0d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ....    sqlite3B
37a02 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
37a03 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
37a04 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  b);..    sqlite3
37a05 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
37a06 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a 20  pParse, iDb);.. 
37a07 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74 65     base = sqlite
37a08 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
37a09 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f  ,  ArraySize(dro
37a0a 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f 70 54  pTrigger), dropT
37a0b 72 69 67 67 65 72 29 3b 0d 0a 20 20 20 20 73 71  rigger);..    sq
37a0c 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
37a0d 34 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54 72  4(v, base+1, pTr
37a0e 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 50 34  igger->zName, P4
37a0f 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a 20 20  _TRANSIENT);..  
37a10 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
37a11 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 34 2c  ngeP4(v, base+4,
37a12 20 22 74 72 69 67 67 65 72 22 2c 20 50 34 5f 53   "trigger", P4_S
37a13 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 73 71 6c  TATIC);..    sql
37a14 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
37a15 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0d 0a  (pParse, iDb);..
37a16 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
37a17 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
37a18 65 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 73  e, 0, 0);..    s
37a19 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
37a1a 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
37a1b 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  er, iDb, 0, 0, p
37a1c 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
37a1d 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 50 61  0);..    if( pPa
37a1e 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0d 0a  rse->nMem<3 ){..
37a1f 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
37a20 65 6d 20 3d 20 33 3b 0d 0a 20 20 20 20 7d 0d 0a  em = 3;..    }..
37a21 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
37a22 20 52 65 6d 6f 76 65 20 61 20 74 72 69 67 67 65   Remove a trigge
37a23 72 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  r from the hash 
37a24 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 73 71  tables of the sq
37a25 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 2e 0d 0a  lite* pointer...
37a26 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
37a27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
37a28 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72  nlinkAndDeleteTr
37a29 69 67 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64  igger(sqlite3 *d
37a2a 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
37a2b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0d  t char *zName){.
37a2c 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
37a2d 67 67 65 72 3b 0d 0a 20 20 48 61 73 68 20 2a 70  gger;..  Hash *p
37a2e 48 61 73 68 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  Hash;....  asser
37a2f 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  t( sqlite3Schema
37a30 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44  MutexHeld(db, iD
37a31 62 2c 20 30 29 20 29 3b 0d 0a 20 20 70 48 61 73  b, 0) );..  pHas
37a32 68 20 3d 20 26 28 64 62 2d 3e 61 44 62 5b 69 44  h = &(db->aDb[iD
37a33 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  b].pSchema->trig
37a34 48 61 73 68 29 3b 0d 0a 20 20 70 54 72 69 67 67  Hash);..  pTrigg
37a35 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  er = sqlite3Hash
37a36 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e  Insert(pHash, zN
37a37 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
37a38 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b  en30(zName), 0);
37a39 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ..  if( ALWAYS(p
37a3a 54 72 69 67 67 65 72 29 20 29 7b 0d 0a 20 20 20  Trigger) ){..   
37a3b 20 69 66 28 20 70 54 72 69 67 67 65 72 2d 3e 70   if( pTrigger->p
37a3c 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72  Schema==pTrigger
37a3d 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29 7b 0d  ->pTabSchema ){.
37a3e 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
37a3f 61 62 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67  ab = tableOfTrig
37a40 67 65 72 28 70 54 72 69 67 67 65 72 29 3b 0d 0a  ger(pTrigger);..
37a41 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 2a        Trigger **
37a42 70 70 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 70  pp;..      for(p
37a43 70 3d 26 70 54 61 62 2d 3e 70 54 72 69 67 67 65  p=&pTab->pTrigge
37a44 72 3b 20 2a 70 70 21 3d 70 54 72 69 67 67 65 72  r; *pp!=pTrigger
37a45 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70 4e  ; pp=&((*pp)->pN
37a46 65 78 74 29 29 3b 0d 0a 20 20 20 20 20 20 2a 70  ext));..      *p
37a47 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
37a48 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71  ;..    }..    sq
37a49 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
37a4a 65 72 28 64 62 2c 20 70 54 72 69 67 67 65 72 29  er(db, pTrigger)
37a4b 3b 0d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  ;..    db->flags
37a4c 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
37a4d 6e 43 68 61 6e 67 65 73 3b 0d 0a 20 20 7d 0d 0a  nChanges;..  }..
37a4e 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 70 45 4c 69  }..../*..** pELi
37a4f 73 74 20 69 73 20 74 68 65 20 53 45 54 20 63 6c  st is the SET cl
37a50 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54  ause of an UPDAT
37a51 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 45 61  E statement.  Ea
37a52 63 68 20 65 6e 74 72 79 0d 0a 2a 2a 20 69 6e 20  ch entry..** in 
37a53 70 45 4c 69 73 74 20 69 73 20 6f 66 20 74 68 65  pEList is of the
37a54 20 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65 78   format <id>=<ex
37a55 70 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66 20  pr>.  If any of 
37a56 74 68 65 20 65 6e 74 72 69 65 73 0d 0a 2a 2a 20  the entries..** 
37a57 69 6e 20 70 45 4c 69 73 74 20 68 61 76 65 20 61  in pEList have a
37a58 6e 20 3c 69 64 3e 20 77 68 69 63 68 20 6d 61 74  n <id> which mat
37a59 63 68 65 73 20 61 6e 20 69 64 65 6e 74 69 66 69  ches an identifi
37a5a 65 72 20 69 6e 20 70 49 64 4c 69 73 74 2c 0d 0a  er in pIdList,..
37a5b 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54  ** then return T
37a5c 52 55 45 2e 20 20 49 66 20 70 49 64 4c 69 73 74  RUE.  If pIdList
37a5d 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  ==NULL, then it 
37a5e 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 0d  is considered a.
37a5f 0a 2a 2a 20 77 69 6c 64 63 61 72 64 20 74 68 61  .** wildcard tha
37a60 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69  t matches anythi
37a61 6e 67 2e 20 20 4c 69 6b 65 77 69 73 65 20 69 66  ng.  Likewise if
37a62 20 70 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20 74 68   pEList==NULL th
37a63 65 6e 0d 0a 2a 2a 20 69 74 20 6d 61 74 63 68 65  en..** it matche
37a64 73 20 61 6e 79 74 68 69 6e 67 20 73 6f 20 61 6c  s anything so al
37a65 77 61 79 73 20 72 65 74 75 72 6e 20 74 72 75 65  ways return true
37a66 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20  .  Return false 
37a67 6f 6e 6c 79 0d 0a 2a 2a 20 69 66 20 74 68 65 72  only..** if ther
37a68 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0d 0a  e is no match...
37a69 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */..static int c
37a6a 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61  heckColumnOverla
37a6b 70 28 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73  p(IdList *pIdLis
37a6c 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  t, ExprList *pEL
37a6d 69 73 74 29 7b 0d 0a 20 20 69 6e 74 20 65 3b 0d  ist){..  int e;.
37a6e 0a 20 20 69 66 28 20 70 49 64 4c 69 73 74 3d 3d  .  if( pIdList==
37a6f 30 20 7c 7c 20 4e 45 56 45 52 28 70 45 4c 69 73  0 || NEVER(pELis
37a70 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 31  t==0) ) return 1
37a71 3b 0d 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c  ;..  for(e=0; e<
37a72 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65  pEList->nExpr; e
37a73 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 73 71  ++){..    if( sq
37a74 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78  lite3IdListIndex
37a75 28 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74  (pIdList, pEList
37a76 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30  ->a[e].zName)>=0
37a77 20 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20   ) return 1;..  
37a78 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0d  }..  return 0; .
37a79 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
37a7a 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
37a7b 6c 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61  l triggers on ta
37a7c 62 6c 65 20 70 54 61 62 20 69 66 20 74 68 65 72  ble pTab if ther
37a7d 65 20 65 78 69 73 74 73 20 61 74 20 6c 65 61 73  e exists at leas
37a7e 74 0d 0a 2a 2a 20 6f 6e 65 20 74 72 69 67 67 65  t..** one trigge
37a7f 72 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 66  r that must be f
37a80 69 72 65 64 20 77 68 65 6e 20 61 6e 20 6f 70 65  ired when an ope
37a81 72 61 74 69 6f 6e 20 6f 66 20 74 79 70 65 20 27  ration of type '
37a82 6f 70 27 20 69 73 20 0d 0a 2a 2a 20 70 65 72 66  op' is ..** perf
37a83 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 74 61 62  ormed on the tab
37a84 6c 65 2c 20 61 6e 64 2c 20 69 66 20 74 68 61 74  le, and, if that
37a85 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e   operation is an
37a86 20 55 50 44 41 54 45 2c 20 69 66 20 61 74 0d 0a   UPDATE, if at..
37a87 2a 2a 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20  ** least one of 
37a88 74 68 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70  the columns in p
37a89 43 68 61 6e 67 65 73 20 69 73 20 62 65 69 6e 67  Changes is being
37a8a 20 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2f 0d 0a   modified...*/..
37a8b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
37a8c 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54  rigger *sqlite3T
37a8d 72 69 67 67 65 72 73 45 78 69 73 74 28 0d 0a 20  riggersExist(.. 
37a8e 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
37a8f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
37a90 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  e context */..  
37a91 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
37a92 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
37a93 61 62 6c 65 20 74 68 65 20 63 6f 6e 74 61 69 6e  able the contain
37a94 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 2a  s the triggers *
37a95 2f 0d 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  /..  int op,    
37a96 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37a97 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45  one of TK_DELETE
37a98 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f  , TK_INSERT, TK_
37a99 55 50 44 41 54 45 20 2a 2f 0d 0a 20 20 45 78 70  UPDATE */..  Exp
37a9a 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
37a9b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20       /* Columns 
37a9c 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20 61  that change in a
37a9d 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  n UPDATE stateme
37a9e 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 4d  nt */..  int *pM
37a9f 61 73 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ask             
37aa0 20 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66   /* OUT: Mask of
37aa1 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c   TRIGGER_BEFORE|
37aa2 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f  TRIGGER_AFTER */
37aa3 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 6d 61 73 6b  ..){..  int mask
37aa4 20 3d 20 30 3b 0d 0a 20 20 54 72 69 67 67 65 72   = 0;..  Trigger
37aa5 20 2a 70 4c 69 73 74 20 3d 20 30 3b 0d 0a 20 20   *pList = 0;..  
37aa6 54 72 69 67 67 65 72 20 2a 70 3b 0d 0a 0d 0a 20  Trigger *p;.... 
37aa7 20 69 66 28 20 28 70 50 61 72 73 65 2d 3e 64 62   if( (pParse->db
37aa8 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
37aa9 5f 45 6e 61 62 6c 65 54 72 69 67 67 65 72 29 21  _EnableTrigger)!
37aaa 3d 30 20 29 7b 0d 0a 20 20 20 20 70 4c 69 73 74  =0 ){..    pList
37aab 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
37aac 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
37aad 61 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73  ab);..  }..  ass
37aae 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 7c 7c  ert( pList==0 ||
37aaf 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
37ab0 3d 3d 30 20 29 3b 0d 0a 20 20 66 6f 72 28 70 3d  ==0 );..  for(p=
37ab1 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70  pList; p; p=p->p
37ab2 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20  Next){..    if( 
37ab3 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65  p->op==op && che
37ab4 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28  ckColumnOverlap(
37ab5 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68  p->pColumns, pCh
37ab6 61 6e 67 65 73 29 20 29 7b 0d 0a 20 20 20 20 20  anges) ){..     
37ab7 20 6d 61 73 6b 20 7c 3d 20 70 2d 3e 74 72 5f 74   mask |= p->tr_t
37ab8 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  m;..    }..  }..
37ab9 20 20 69 66 28 20 70 4d 61 73 6b 20 29 7b 0d 0a    if( pMask ){..
37aba 20 20 20 20 2a 70 4d 61 73 6b 20 3d 20 6d 61 73      *pMask = mas
37abb 6b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  k;..  }..  retur
37abc 6e 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73 74 20  n (mask ? pList 
37abd 3a 20 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  : 0);..}..../*..
37abe 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70  ** Convert the p
37abf 53 74 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b  Step->target tok
37ac0 65 6e 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73  en into a SrcLis
37ac1 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
37ac2 6f 69 6e 74 65 72 0d 0a 2a 2a 20 74 6f 20 74 68  ointer..** to th
37ac3 61 74 20 53 72 63 4c 69 73 74 2e 0d 0a 2a 2a 0d  at SrcList...**.
37ac4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
37ac5 20 61 64 64 73 20 61 20 73 70 65 63 69 66 69 63   adds a specific
37ac6 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20   database name, 
37ac7 69 66 20 6e 65 65 64 65 64 2c 20 74 6f 20 74 68  if needed, to th
37ac8 65 20 74 61 72 67 65 74 20 77 68 65 6e 0d 0a 2a  e target when..*
37ac9 2a 20 66 6f 72 6d 69 6e 67 20 74 68 65 20 53 72  * forming the Sr
37aca 63 4c 69 73 74 2e 20 20 54 68 69 73 20 70 72 65  cList.  This pre
37acb 76 65 6e 74 73 20 61 20 74 72 69 67 67 65 72 20  vents a trigger 
37acc 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  in one database 
37acd 66 72 6f 6d 0d 0a 2a 2a 20 72 65 66 65 72 72 69  from..** referri
37ace 6e 67 20 74 6f 20 61 20 74 61 72 67 65 74 20 69  ng to a target i
37acf 6e 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61  n another databa
37ad0 73 65 2e 20 20 41 6e 20 65 78 63 65 70 74 69 6f  se.  An exceptio
37ad1 6e 20 69 73 20 77 68 65 6e 20 74 68 65 0d 0a 2a  n is when the..*
37ad2 2a 20 74 72 69 67 67 65 72 20 69 73 20 69 6e 20  * trigger is in 
37ad3 54 45 4d 50 20 69 6e 20 77 68 69 63 68 20 63 61  TEMP in which ca
37ad4 73 65 20 69 74 20 63 61 6e 20 72 65 66 65 72 20  se it can refer 
37ad5 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74  to any other dat
37ad6 61 62 61 73 65 20 69 74 0d 0a 2a 2a 20 77 61 6e  abase it..** wan
37ad7 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ts...*/..static 
37ad8 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65 74 53  SrcList *targetS
37ad9 72 63 4c 69 73 74 28 0d 0a 20 20 50 61 72 73 65  rcList(..  Parse
37ada 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
37adb 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
37adc 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 72 69  ontext */..  Tri
37add 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20  ggerStep *pStep 
37ade 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
37adf 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
37ae0 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0d  target token */.
37ae1 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20  .){..  int iDb; 
37ae2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
37ae3 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
37ae4 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0d 0a  base to use */..
37ae5 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
37ae6 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73         /* SrcLis
37ae7 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  t to be returned
37ae8 20 2a 2f 0d 0a 0d 0a 20 20 70 53 72 63 20 3d 20   */....  pSrc = 
37ae9 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
37aea 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
37aeb 20 30 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67   0, &pStep->targ
37aec 65 74 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 70  et, 0);..  if( p
37aed 53 72 63 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  Src ){..    asse
37aee 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 30  rt( pSrc->nSrc>0
37aef 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
37af0 20 70 53 72 63 2d 3e 61 21 3d 30 20 29 3b 0d 0a   pSrc->a!=0 );..
37af1 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
37af2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
37af3 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 74 65 70  Parse->db, pStep
37af4 2d 3e 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61  ->pTrig->pSchema
37af5 29 3b 0d 0a 20 20 20 20 69 66 28 20 69 44 62 3d  );..    if( iDb=
37af6 3d 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0d  =0 || iDb>=2 ){.
37af7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a  .      sqlite3 *
37af8 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
37af9 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
37afa 69 44 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e  iDb<pParse->db->
37afb 6e 44 62 20 29 3b 0d 0a 20 20 20 20 20 20 70 53  nDb );..      pS
37afc 72 63 2d 3e 61 5b 70 53 72 63 2d 3e 6e 53 72 63  rc->a[pSrc->nSrc
37afd 2d 31 5d 2e 7a 44 61 74 61 62 61 73 65 20 3d 20  -1].zDatabase = 
37afe 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
37aff 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  db, db->aDb[iDb]
37b00 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 0d  .zName);..    }.
37b01 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
37b02 53 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  Src;..}..../*..*
37b03 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20  * Generate VDBE 
37b04 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61  code for the sta
37b05 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74  tements inside t
37b06 68 65 20 62 6f 64 79 20 6f 66 20 61 20 73 69 6e  he body of a sin
37b07 67 6c 65 20 0d 0a 2a 2a 20 74 72 69 67 67 65 72  gle ..** trigger
37b08 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
37b09 74 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f  t codeTriggerPro
37b0a 67 72 61 6d 28 0d 0a 20 20 50 61 72 73 65 20 2a  gram(..  Parse *
37b0b 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
37b0c 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
37b0d 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54   context */..  T
37b0e 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
37b0f 70 4c 69 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74  pList,   /* List
37b10 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 69   of statements i
37b11 6e 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65  nside the trigge
37b12 72 20 62 6f 64 79 20 2a 2f 0d 0a 20 20 69 6e 74  r body */..  int
37b13 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
37b14 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69         /* Confli
37b15 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f  ct algorithm. (O
37b16 45 5f 41 62 6f 72 74 2c 20 65 74 63 29 20 2a 2f  E_Abort, etc) */
37b17 20 20 0d 0a 29 7b 0d 0a 20 20 54 72 69 67 67 65    ..){..  Trigge
37b18 72 53 74 65 70 20 2a 70 53 74 65 70 3b 0d 0a 20  rStep *pStep;.. 
37b19 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
37b1a 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 73 71 6c  e->pVdbe;..  sql
37b1b 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
37b1c 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 61 73 73 65  e->db;....  asse
37b1d 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69  rt( pParse->pTri
37b1e 67 67 65 72 54 61 62 20 26 26 20 70 50 61 72 73  ggerTab && pPars
37b1f 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 29 3b 0d  e->pToplevel );.
37b20 0a 20 20 61 73 73 65 72 74 28 20 70 53 74 65 70  .  assert( pStep
37b21 4c 69 73 74 20 29 3b 0d 0a 20 20 61 73 73 65 72  List );..  asser
37b22 74 28 20 76 21 3d 30 20 29 3b 0d 0a 20 20 66 6f  t( v!=0 );..  fo
37b23 72 28 70 53 74 65 70 3d 70 53 74 65 70 4c 69 73  r(pStep=pStepLis
37b24 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d  t; pStep; pStep=
37b25 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0d 0a  pStep->pNext){..
37b26 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
37b27 74 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  t the ON CONFLIC
37b28 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 69  T policy that wi
37b29 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ll be used for t
37b2a 68 69 73 20 73 74 65 70 0d 0a 20 20 20 20 2a 2a  his step..    **
37b2b 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
37b2c 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 65 20  program. If the 
37b2d 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
37b2e 61 75 73 65 64 20 74 68 69 73 20 74 72 69 67 67  aused this trigg
37b2f 65 72 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 66 69  er..    ** to fi
37b30 72 65 20 68 61 64 20 61 6e 20 65 78 70 6c 69 63  re had an explic
37b31 69 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 2c 20  it ON CONFLICT, 
37b32 74 68 65 6e 20 75 73 65 20 69 74 2e 20 4f 74 68  then use it. Oth
37b33 65 72 77 69 73 65 2c 20 75 73 65 0d 0a 20 20 20  erwise, use..   
37b34 20 2a 2a 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c   ** the ON CONFL
37b35 49 43 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20  ICT policy that 
37b36 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61 73  was specified as
37b37 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 72 69   part of the tri
37b38 67 67 65 72 0d 0a 20 20 20 20 2a 2a 20 73 74 65  gger..    ** ste
37b39 70 20 73 74 61 74 65 6d 65 6e 74 2e 20 45 78 61  p statement. Exa
37b3a 6d 70 6c 65 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20  mple:..    **.. 
37b3b 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54     **   CREATE T
37b3c 52 49 47 47 45 52 20 41 46 54 45 52 20 49 4e 53  RIGGER AFTER INS
37b3d 45 52 54 20 4f 4e 20 74 31 20 42 45 47 49 4e 3b  ERT ON t1 BEGIN;
37b3e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 49 4e 53  ..    **     INS
37b3f 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49  ERT OR REPLACE I
37b40 4e 54 4f 20 74 32 20 56 41 4c 55 45 53 28 6e 65  NTO t2 VALUES(ne
37b41 77 2e 61 2c 20 6e 65 77 2e 62 29 3b 0d 0a 20 20  w.a, new.b);..  
37b42 20 20 2a 2a 20 20 20 45 4e 44 3b 0d 0a 20 20 20    **   END;..   
37b43 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 49 4e   **..    **   IN
37b44 53 45 52 54 20 49 4e 54 4f 20 74 31 20 2e 2e 2e  SERT INTO t1 ...
37b45 20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d   ;            --
37b46 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74 32 20   insert into t2 
37b47 75 73 65 73 20 52 45 50 4c 41 43 45 20 70 6f 6c  uses REPLACE pol
37b48 69 63 79 0d 0a 20 20 20 20 2a 2a 20 20 20 49 4e  icy..    **   IN
37b49 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
37b4a 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20 20 2d 2d  NTO t1 ... ;  --
37b4b 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74 32 20   insert into t2 
37b4c 75 73 65 73 20 49 47 4e 4f 52 45 20 70 6f 6c 69  uses IGNORE poli
37b4d 63 79 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  cy..    */..    
37b4e 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 20  pParse->eOrconf 
37b4f 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45 5f 44 65  = (orconf==OE_De
37b50 66 61 75 6c 74 29 3f 70 53 74 65 70 2d 3e 6f 72  fault)?pStep->or
37b51 63 6f 6e 66 3a 28 75 38 29 6f 72 63 6f 6e 66 3b  conf:(u8)orconf;
37b52 0d 0a 0d 0a 20 20 20 20 73 77 69 74 63 68 28 20  ....    switch( 
37b53 70 53 74 65 70 2d 3e 6f 70 20 29 7b 0d 0a 20 20  pStep->op ){..  
37b54 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41      case TK_UPDA
37b55 54 45 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 73  TE: {..        s
37b56 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61  qlite3Update(pPa
37b57 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20 20 20  rse, ..         
37b58 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70   targetSrcList(p
37b59 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0d 0a  Parse, pStep),..
37b5a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
37b5b 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
37b5c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73   pStep->pExprLis
37b5d 74 2c 20 30 29 2c 20 0d 0a 20 20 20 20 20 20 20  t, 0), ..       
37b5e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
37b5f 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 57 68  p(db, pStep->pWh
37b60 65 72 65 2c 20 30 29 2c 20 0d 0a 20 20 20 20 20  ere, 0), ..     
37b61 20 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72       pParse->eOr
37b62 63 6f 6e 66 0d 0a 20 20 20 20 20 20 20 20 29 3b  conf..        );
37b63 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
37b64 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
37b65 20 63 61 73 65 20 54 4b 5f 49 4e 53 45 52 54 3a   case TK_INSERT:
37b66 20 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69   {..        sqli
37b67 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73 65  te3Insert(pParse
37b68 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 74 61  , ..          ta
37b69 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72  rgetSrcList(pPar
37b6a 73 65 2c 20 70 53 74 65 70 29 2c 0d 0a 20 20 20  se, pStep),..   
37b6b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
37b6c 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53  prListDup(db, pS
37b6d 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20  tep->pExprList, 
37b6e 30 29 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  0), ..          
37b6f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
37b70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c  (db, pStep->pSel
37b71 65 63 74 2c 20 30 29 2c 20 0d 0a 20 20 20 20 20  ect, 0), ..     
37b72 20 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69       sqlite3IdLi
37b73 73 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d  stDup(db, pStep-
37b74 3e 70 49 64 4c 69 73 74 29 2c 20 0d 0a 20 20 20  >pIdList), ..   
37b75 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 65         pParse->e
37b76 4f 72 63 6f 6e 66 0d 0a 20 20 20 20 20 20 20 20  Orconf..        
37b77 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );..        brea
37b78 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
37b79 20 20 20 63 61 73 65 20 54 4b 5f 44 45 4c 45 54     case TK_DELET
37b7a 45 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  E: {..        sq
37b7b 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28  lite3DeleteFrom(
37b7c 70 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20  pParse, ..      
37b7d 20 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73      targetSrcLis
37b7e 74 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29  t(pParse, pStep)
37b7f 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ,..          sql
37b80 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
37b81 70 53 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30  pStep->pWhere, 0
37b82 29 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d 0a 20  )..        );.. 
37b83 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
37b84 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 64 65       }..      de
37b85 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 70  fault: assert( p
37b86 53 74 65 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Step->op==TK_SEL
37b87 45 43 54 20 29 3b 20 7b 0d 0a 20 20 20 20 20 20  ECT ); {..      
37b88 20 20 53 65 6c 65 63 74 44 65 73 74 20 73 44 65    SelectDest sDe
37b89 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 53 65 6c  st;..        Sel
37b8a 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 73  ect *pSelect = s
37b8b 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
37b8c 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  db, pStep->pSele
37b8d 63 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  ct, 0);..       
37b8e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
37b8f 73 74 49 6e 69 74 28 26 73 44 65 73 74 2c 20 53  stInit(&sDest, S
37b90 52 54 5f 44 69 73 63 61 72 64 2c 20 30 29 3b 0d  RT_Discard, 0);.
37b91 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
37b92 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
37b93 53 65 6c 65 63 74 2c 20 26 73 44 65 73 74 29 3b  Select, &sDest);
37b94 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
37b95 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
37b96 2c 20 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  , pSelect);..   
37b97 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
37b98 20 20 20 7d 0d 0a 20 20 20 20 7d 20 0d 0a 20 20     }..    } ..  
37b99 20 20 69 66 28 20 70 53 74 65 70 2d 3e 6f 70 21    if( pStep->op!
37b9a 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0d 0a 20  =TK_SELECT ){.. 
37b9b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
37b9c 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 52 65 73  AddOp0(v, OP_Res
37b9d 65 74 43 6f 75 6e 74 29 3b 0d 0a 20 20 20 20 7d  etCount);..    }
37b9e 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
37b9f 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2f 23 69 66  n 0;..}....//#if
37ba0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
37ba1 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
37ba2 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
37ba3 6f 20 61 64 64 20 56 64 62 65 43 6f 6d 6d 65 6e  o add VdbeCommen
37ba4 74 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e 73 20  t() annotations 
37ba5 74 6f 20 61 20 56 44 42 45 0d 0a 2a 2a 20 70 72  to a VDBE..** pr
37ba6 6f 67 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f 74  ogram. It is not
37ba7 20 75 73 65 64 20 69 6e 20 70 72 6f 64 75 63 74   used in product
37ba8 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20 66  ion code, only f
37ba9 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0d 0a 2a  or debugging...*
37baa 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  /..static const 
37bab 63 68 61 72 20 2a 6f 6e 45 72 72 6f 72 54 65 78  char *onErrorTex
37bac 74 28 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0d  t(int onError){.
37bad 0a 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72  .  switch( onErr
37bae 6f 72 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20  or ){..    case 
37baf 4f 45 5f 41 62 6f 72 74 3a 20 20 20 20 72 65 74  OE_Abort:    ret
37bb0 75 72 6e 20 22 61 62 6f 72 74 22 3b 0d 0a 20 20  urn "abort";..  
37bb1 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61    case OE_Rollba
37bb2 63 6b 3a 20 72 65 74 75 72 6e 20 22 72 6f 6c 6c  ck: return "roll
37bb3 62 61 63 6b 22 3b 0d 0a 20 20 20 20 63 61 73 65  back";..    case
37bb4 20 4f 45 5f 46 61 69 6c 3a 20 20 20 20 20 72 65   OE_Fail:     re
37bb5 74 75 72 6e 20 22 66 61 69 6c 22 3b 0d 0a 20 20  turn "fail";..  
37bb6 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63    case OE_Replac
37bb7 65 3a 20 20 72 65 74 75 72 6e 20 22 72 65 70 6c  e:  return "repl
37bb8 61 63 65 22 3b 0d 0a 20 20 20 20 63 61 73 65 20  ace";..    case 
37bb9 4f 45 5f 49 67 6e 6f 72 65 3a 20 20 20 72 65 74  OE_Ignore:   ret
37bba 75 72 6e 20 22 69 67 6e 6f 72 65 22 3b 0d 0a 20  urn "ignore";.. 
37bbb 20 20 20 63 61 73 65 20 4f 45 5f 44 65 66 61 75     case OE_Defau
37bbc 6c 74 3a 20 20 72 65 74 75 72 6e 20 22 64 65 66  lt:  return "def
37bbd 61 75 6c 74 22 3b 0d 0a 20 20 7d 0d 0a 20 20 72  ault";..  }..  r
37bbe 65 74 75 72 6e 20 22 6e 2f 61 22 3b 0d 0a 7d 0d  eturn "n/a";..}.
37bbf 0a 2f 2f 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  .//#endif..../*.
37bc0 0a 2a 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78  .** Parse contex
37bc1 74 20 73 74 72 75 63 74 75 72 65 20 70 46 72 6f  t structure pFro
37bc2 6d 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  m has just been 
37bc3 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  used to create a
37bc4 20 73 75 62 2d 76 64 62 65 0d 0a 2a 2a 20 28 74   sub-vdbe..** (t
37bc5 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 29 2e  rigger program).
37bc6 20 49 66 20 61 6e 20 65 72 72 6f 72 20 68 61 73   If an error has
37bc7 20 6f 63 63 75 72 72 65 64 2c 20 74 72 61 6e 73   occurred, trans
37bc8 66 65 72 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d  fer error inform
37bc9 61 74 69 6f 6e 0d 0a 2a 2a 20 66 72 6f 6d 20 70  ation..** from p
37bca 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 0d 0a 2a 2f  From to pTo...*/
37bcb 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72  ..static void tr
37bcc 61 6e 73 66 65 72 50 61 72 73 65 45 72 72 6f 72  ansferParseError
37bcd 28 50 61 72 73 65 20 2a 70 54 6f 2c 20 50 61 72  (Parse *pTo, Par
37bce 73 65 20 2a 70 46 72 6f 6d 29 7b 0d 0a 20 20 61  se *pFrom){..  a
37bcf 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 7a 45  ssert( pFrom->zE
37bd0 72 72 4d 73 67 3d 3d 30 20 7c 7c 20 70 46 72 6f  rrMsg==0 || pFro
37bd1 6d 2d 3e 6e 45 72 72 20 29 3b 0d 0a 20 20 61 73  m->nErr );..  as
37bd2 73 65 72 74 28 20 70 54 6f 2d 3e 7a 45 72 72 4d  sert( pTo->zErrM
37bd3 73 67 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 6e 45  sg==0 || pTo->nE
37bd4 72 72 20 29 3b 0d 0a 20 20 69 66 28 20 70 54 6f  rr );..  if( pTo
37bd5 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0d 0a 20 20  ->nErr==0 ){..  
37bd6 20 20 70 54 6f 2d 3e 7a 45 72 72 4d 73 67 20 3d    pTo->zErrMsg =
37bd7 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3b   pFrom->zErrMsg;
37bd8 0d 0a 20 20 20 20 70 54 6f 2d 3e 6e 45 72 72 20  ..    pTo->nErr 
37bd9 3d 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 3b 0d 0a  = pFrom->nErr;..
37bda 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71    }else{..    sq
37bdb 6c 69 74 65 33 44 62 46 72 65 65 28 70 46 72 6f  lite3DbFree(pFro
37bdc 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d 3e 7a 45  m->db, pFrom->zE
37bdd 72 72 4d 73 67 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d  rrMsg);..  }..}.
37bde 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72 65 61 74 65  .../*..** Create
37bdf 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20   and populate a 
37be0 6e 65 77 20 54 72 69 67 67 65 72 50 72 67 20 6f  new TriggerPrg o
37be1 62 6a 65 63 74 20 77 69 74 68 20 61 20 73 75 62  bject with a sub
37be2 2d 70 72 6f 67 72 61 6d 20 0d 0a 2a 2a 20 69 6d  -program ..** im
37be3 70 6c 65 6d 65 6e 74 69 6e 67 20 74 72 69 67 67  plementing trigg
37be4 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74 68  er pTrigger with
37be5 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
37be6 69 63 79 20 6f 72 63 6f 6e 66 2e 0d 0a 2a 2f 0d  icy orconf...*/.
37be7 0a 73 74 61 74 69 63 20 54 72 69 67 67 65 72 50  .static TriggerP
37be8 72 67 20 2a 63 6f 64 65 52 6f 77 54 72 69 67 67  rg *codeRowTrigg
37be9 65 72 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  er(..  Parse *pP
37bea 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 43  arse,       /* C
37beb 75 72 72 65 6e 74 20 70 61 72 73 65 20 63 6f 6e  urrent parse con
37bec 74 65 78 74 20 2a 2f 0d 0a 20 20 54 72 69 67 67  text */..  Trigg
37bed 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20  er *pTrigger,   
37bee 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f  /* Trigger to co
37bef 64 65 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a  de */..  Table *
37bf0 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a  pTab,         /*
37bf1 20 54 68 65 20 74 61 62 6c 65 20 70 54 72 69 67   The table pTrig
37bf2 67 65 72 20 69 73 20 61 74 74 61 63 68 65 64 20  ger is attached 
37bf3 74 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 72 63  to */..  int orc
37bf4 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a  onf           /*
37bf5 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
37bf6 69 63 79 20 74 6f 20 63 6f 64 65 20 74 72 69 67  icy to code trig
37bf7 67 65 72 20 70 72 6f 67 72 61 6d 20 77 69 74 68  ger program with
37bf8 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 50 61 72 73 65   */..){..  Parse
37bf9 20 2a 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33   *pTop = sqlite3
37bfa 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
37bfb 61 72 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65  arse);..  sqlite
37bfc 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
37bfd 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73  db;   /* Databas
37bfe 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 54  e handle */..  T
37bff 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b  riggerPrg *pPrg;
37c00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
37c01 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
37c02 0d 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 20  ..  Expr *pWhen 
37c03 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
37c04 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 6f 66 20  /* Duplicate of 
37c05 74 72 69 67 67 65 72 20 57 48 45 4e 20 65 78 70  trigger WHEN exp
37c06 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 56 64  ression */..  Vd
37c07 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
37c08 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
37c09 70 6f 72 61 72 79 20 56 4d 20 2a 2f 0d 0a 20 20  porary VM */..  
37c0a 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
37c0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
37c0c 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
37c0d 73 75 62 2d 76 64 62 65 20 2a 2f 0d 0a 20 20 53  sub-vdbe */..  S
37c0e 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
37c0f 72 61 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75  ram = 0;   /* Su
37c10 62 2d 76 64 62 65 20 66 6f 72 20 74 72 69 67 67  b-vdbe for trigg
37c11 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0d 0a 20  er program */.. 
37c12 20 50 61 72 73 65 20 2a 70 53 75 62 50 61 72 73   Parse *pSubPars
37c13 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
37c14 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 66 6f  Parse context fo
37c15 72 20 73 75 62 2d 76 64 62 65 20 2a 2f 0d 0a 20  r sub-vdbe */.. 
37c16 20 69 6e 74 20 69 45 6e 64 54 72 69 67 67 65 72   int iEndTrigger
37c17 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
37c18 4c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f  Label to jump to
37c19 20 69 66 20 57 48 45 4e 20 69 73 20 66 61 6c 73   if WHEN is fals
37c1a 65 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  e */....  assert
37c1b 28 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  ( pTrigger->zNam
37c1c 65 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61  e==0 || pTab==ta
37c1d 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72  bleOfTrigger(pTr
37c1e 69 67 67 65 72 29 20 29 3b 0d 0a 20 20 61 73 73  igger) );..  ass
37c1f 65 72 74 28 20 70 54 6f 70 2d 3e 70 56 64 62 65  ert( pTop->pVdbe
37c20 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f   );....  /* Allo
37c21 63 61 74 65 20 74 68 65 20 54 72 69 67 67 65 72  cate the Trigger
37c22 50 72 67 20 61 6e 64 20 53 75 62 50 72 6f 67 72  Prg and SubProgr
37c23 61 6d 20 6f 62 6a 65 63 74 73 2e 20 54 6f 20 65  am objects. To e
37c24 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 79 0d  nsure that they.
37c25 0a 20 20 2a 2a 20 61 72 65 20 66 72 65 65 64 20  .  ** are freed 
37c26 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
37c27 72 73 2c 20 6c 69 6e 6b 20 74 68 65 6d 20 69 6e  rs, link them in
37c28 74 6f 20 74 68 65 20 50 61 72 73 65 2e 70 54 72  to the Parse.pTr
37c29 69 67 67 65 72 50 72 67 20 0d 0a 20 20 2a 2a 20  iggerPrg ..  ** 
37c2a 6c 69 73 74 20 6f 66 20 74 68 65 20 74 6f 70 2d  list of the top-
37c2b 6c 65 76 65 6c 20 50 61 72 73 65 20 6f 62 6a 65  level Parse obje
37c2c 63 74 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72  ct sooner rather
37c2d 20 74 68 61 6e 20 6c 61 74 65 72 2e 20 20 2a 2f   than later.  */
37c2e 0d 0a 20 20 70 50 72 67 20 3d 20 73 71 6c 69 74  ..  pPrg = sqlit
37c2f 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
37c30 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
37c31 72 50 72 67 29 29 3b 0d 0a 20 20 69 66 28 20 21  rPrg));..  if( !
37c32 70 50 72 67 20 29 20 72 65 74 75 72 6e 20 30 3b  pPrg ) return 0;
37c33 0d 0a 20 20 70 50 72 67 2d 3e 70 4e 65 78 74 20  ..  pPrg->pNext 
37c34 3d 20 70 54 6f 70 2d 3e 70 54 72 69 67 67 65 72  = pTop->pTrigger
37c35 50 72 67 3b 0d 0a 20 20 70 54 6f 70 2d 3e 70 54  Prg;..  pTop->pT
37c36 72 69 67 67 65 72 50 72 67 20 3d 20 70 50 72 67  riggerPrg = pPrg
37c37 3b 0d 0a 20 20 70 50 72 67 2d 3e 70 50 72 6f 67  ;..  pPrg->pProg
37c38 72 61 6d 20 3d 20 70 50 72 6f 67 72 61 6d 20 3d  ram = pProgram =
37c39 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
37c3a 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
37c3b 53 75 62 50 72 6f 67 72 61 6d 29 29 3b 0d 0a 20  SubProgram));.. 
37c3c 20 69 66 28 20 21 70 50 72 6f 67 72 61 6d 20 29   if( !pProgram )
37c3d 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 73 71   return 0;..  sq
37c3e 6c 69 74 65 33 56 64 62 65 4c 69 6e 6b 53 75 62  lite3VdbeLinkSub
37c3f 50 72 6f 67 72 61 6d 28 70 54 6f 70 2d 3e 70 56  Program(pTop->pV
37c40 64 62 65 2c 20 70 50 72 6f 67 72 61 6d 29 3b 0d  dbe, pProgram);.
37c41 0a 20 20 70 50 72 67 2d 3e 70 54 72 69 67 67 65  .  pPrg->pTrigge
37c42 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0d 0a 20  r = pTrigger;.. 
37c43 20 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 20 3d 20   pPrg->orconf = 
37c44 6f 72 63 6f 6e 66 3b 0d 0a 20 20 70 50 72 67 2d  orconf;..  pPrg-
37c45 3e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20 3d 20 30  >aColmask[0] = 0
37c46 78 66 66 66 66 66 66 66 66 3b 0d 0a 20 20 70 50  xffffffff;..  pP
37c47 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20  rg->aColmask[1] 
37c48 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0d 0a 0d  = 0xffffffff;...
37c49 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
37c4a 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65  nd populate a ne
37c4b 77 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  w Parse context 
37c4c 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 64 69 6e  to use for codin
37c4d 67 20 74 68 65 20 0d 0a 20 20 2a 2a 20 74 72 69  g the ..  ** tri
37c4e 67 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61 6d  gger sub-program
37c4f 2e 20 20 2a 2f 0d 0a 20 20 70 53 75 62 50 61 72  .  */..  pSubPar
37c50 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
37c51 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  kAllocZero(db, s
37c52 69 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0d 0a  izeof(Parse));..
37c53 20 20 69 66 28 20 21 70 53 75 62 50 61 72 73 65    if( !pSubParse
37c54 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20   ) return 0;..  
37c55 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
37c56 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0d 0a 20  sizeof(sNC));.. 
37c57 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 53   sNC.pParse = pS
37c58 75 62 50 61 72 73 65 3b 0d 0a 20 20 70 53 75 62  ubParse;..  pSub
37c59 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0d  Parse->db = db;.
37c5a 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 70 54  .  pSubParse->pT
37c5b 72 69 67 67 65 72 54 61 62 20 3d 20 70 54 61 62  riggerTab = pTab
37c5c 3b 0d 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e  ;..  pSubParse->
37c5d 70 54 6f 70 6c 65 76 65 6c 20 3d 20 70 54 6f 70  pToplevel = pTop
37c5e 3b 0d 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e  ;..  pSubParse->
37c5f 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  zAuthContext = p
37c60 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3b 0d  Trigger->zName;.
37c61 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 65 54  .  pSubParse->eT
37c62 72 69 67 67 65 72 4f 70 20 3d 20 70 54 72 69 67  riggerOp = pTrig
37c63 67 65 72 2d 3e 6f 70 3b 0d 0a 20 20 70 53 75 62  ger->op;..  pSub
37c64 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f  Parse->nQueryLoo
37c65 70 20 3d 20 70 50 61 72 73 65 2d 3e 6e 51 75 65  p = pParse->nQue
37c66 72 79 4c 6f 6f 70 3b 0d 0a 0d 0a 20 20 76 20 3d  ryLoop;....  v =
37c67 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
37c68 70 53 75 62 50 61 72 73 65 29 3b 0d 0a 20 20 69  pSubParse);..  i
37c69 66 28 20 76 20 29 7b 0d 0a 20 20 20 20 56 64 62  f( v ){..    Vdb
37c6a 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53 74  eComment((v, "St
37c6b 61 72 74 3a 20 25 73 2e 25 73 20 28 25 73 20 25  art: %s.%s (%s %
37c6c 73 25 73 25 73 20 4f 4e 20 25 73 29 22 2c 20 0d  s%s%s ON %s)", .
37c6d 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d  .      pTrigger-
37c6e 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f 72 54  >zName, onErrorT
37c6f 65 78 74 28 6f 72 63 6f 6e 66 29 2c 0d 0a 20 20  ext(orconf),..  
37c70 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 74      (pTrigger->t
37c71 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45  r_tm==TRIGGER_BE
37c72 46 4f 52 45 20 3f 20 22 42 45 46 4f 52 45 22 20  FORE ? "BEFORE" 
37c73 3a 20 22 41 46 54 45 52 22 29 2c 0d 0a 20 20 20  : "AFTER"),..   
37c74 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e       (pTrigger->
37c75 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 3f 20  op==TK_UPDATE ? 
37c76 22 55 50 44 41 54 45 22 20 3a 20 22 22 29 2c 0d  "UPDATE" : ""),.
37c77 0a 20 20 20 20 20 20 20 20 28 70 54 72 69 67 67  .        (pTrigg
37c78 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52  er->op==TK_INSER
37c79 54 20 3f 20 22 49 4e 53 45 52 54 22 20 3a 20 22  T ? "INSERT" : "
37c7a 22 29 2c 0d 0a 20 20 20 20 20 20 20 20 28 70 54  "),..        (pT
37c7b 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 44  rigger->op==TK_D
37c7c 45 4c 45 54 45 20 3f 20 22 44 45 4c 45 54 45 22  ELETE ? "DELETE"
37c7d 20 3a 20 22 22 29 2c 0d 0a 20 20 20 20 20 20 70   : ""),..      p
37c7e 54 61 62 2d 3e 7a 4e 61 6d 65 0d 0a 20 20 20 20  Tab->zName..    
37c7f 29 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ));..#ifndef SQL
37c80 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0d 0a  ITE_OMIT_TRACE..
37c81 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
37c82 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 0d  hangeP4(v, -1, .
37c83 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50  .      sqlite3MP
37c84 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54 52  rintf(db, "-- TR
37c85 49 47 47 45 52 20 25 73 22 2c 20 70 54 72 69 67  IGGER %s", pTrig
37c86 67 65 72 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f  ger->zName), P4_
37c87 44 59 4e 41 4d 49 43 0d 0a 20 20 20 20 29 3b 0d  DYNAMIC..    );.
37c88 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 20 20 2f  .#endif....    /
37c89 2a 20 49 66 20 6f 6e 65 20 77 61 73 20 73 70 65  * If one was spe
37c8a 63 69 66 69 65 64 2c 20 63 6f 64 65 20 74 68 65  cified, code the
37c8b 20 57 48 45 4e 20 63 6c 61 75 73 65 2e 20 49 66   WHEN clause. If
37c8c 20 69 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f   it evaluates to
37c8d 20 66 61 6c 73 65 0d 0a 20 20 20 20 2a 2a 20 28   false..    ** (
37c8e 6f 72 20 4e 55 4c 4c 29 20 74 68 65 20 73 75 62  or NULL) the sub
37c8f 2d 76 64 62 65 20 69 73 20 69 6d 6d 65 64 69 61  -vdbe is immedia
37c90 74 65 6c 79 20 68 61 6c 74 65 64 20 62 79 20 6a  tely halted by j
37c91 75 6d 70 69 6e 67 20 74 6f 20 74 68 65 20 0d 0a  umping to the ..
37c92 20 20 20 20 2a 2a 20 4f 50 5f 48 61 6c 74 20 69      ** OP_Halt i
37c93 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 20 65  nserted at the e
37c94 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61  nd of the progra
37c95 6d 2e 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20  m.  */..    if( 
37c96 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20  pTrigger->pWhen 
37c97 29 7b 0d 0a 20 20 20 20 20 20 70 57 68 65 6e 20  ){..      pWhen 
37c98 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
37c99 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70  (db, pTrigger->p
37c9a 57 68 65 6e 2c 20 30 29 3b 0d 0a 20 20 20 20 20  When, 0);..     
37c9b 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
37c9c 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
37c9d 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57  prNames(&sNC, pW
37c9e 68 65 6e 29 20 0d 0a 20 20 20 20 20 20 20 26 26  hen) ..       &&
37c9f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
37ca0 64 3d 3d 30 20 0d 0a 20 20 20 20 20 20 29 7b 0d  d==0 ..      ){.
37ca1 0a 20 20 20 20 20 20 20 20 69 45 6e 64 54 72 69  .        iEndTri
37ca2 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64  gger = sqlite3Vd
37ca3 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d  beMakeLabel(v);.
37ca4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
37ca5 45 78 70 72 49 66 46 61 6c 73 65 28 70 53 75 62  ExprIfFalse(pSub
37ca6 50 61 72 73 65 2c 20 70 57 68 65 6e 2c 20 69 45  Parse, pWhen, iE
37ca7 6e 64 54 72 69 67 67 65 72 2c 20 53 51 4c 49 54  ndTrigger, SQLIT
37ca8 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0d 0a  E_JUMPIFNULL);..
37ca9 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73        }..      s
37caa 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
37cab 28 64 62 2c 20 70 57 68 65 6e 29 3b 0d 0a 20 20  (db, pWhen);..  
37cac 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f    }....    /* Co
37cad 64 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70  de the trigger p
37cae 72 6f 67 72 61 6d 20 69 6e 74 6f 20 74 68 65 20  rogram into the 
37caf 73 75 62 2d 76 64 62 65 2e 20 2a 2f 0d 0a 20 20  sub-vdbe. */..  
37cb0 20 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f    codeTriggerPro
37cb1 67 72 61 6d 28 70 53 75 62 50 61 72 73 65 2c 20  gram(pSubParse, 
37cb2 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c  pTrigger->step_l
37cb3 69 73 74 2c 20 6f 72 63 6f 6e 66 29 3b 0d 0a 0d  ist, orconf);...
37cb4 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61  .    /* Insert a
37cb5 6e 20 4f 50 5f 48 61 6c 74 20 61 74 20 74 68 65  n OP_Halt at the
37cb6 20 65 6e 64 20 6f 66 20 74 68 65 20 73 75 62 2d   end of the sub-
37cb7 70 72 6f 67 72 61 6d 2e 20 2a 2f 0d 0a 20 20 20  program. */..   
37cb8 20 69 66 28 20 69 45 6e 64 54 72 69 67 67 65 72   if( iEndTrigger
37cb9 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
37cba 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
37cbb 65 6c 28 76 2c 20 69 45 6e 64 54 72 69 67 67 65  el(v, iEndTrigge
37cbc 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  r);..    }..    
37cbd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
37cbe 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0d 0a  0(v, OP_Halt);..
37cbf 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
37cc0 28 76 2c 20 22 45 6e 64 3a 20 25 73 2e 25 73 22  (v, "End: %s.%s"
37cc1 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  , pTrigger->zNam
37cc2 65 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f  e, onErrorText(o
37cc3 72 63 6f 6e 66 29 29 29 3b 0d 0a 0d 0a 20 20 20  rconf)));....   
37cc4 20 74 72 61 6e 73 66 65 72 50 61 72 73 65 45 72   transferParseEr
37cc5 72 6f 72 28 70 50 61 72 73 65 2c 20 70 53 75 62  ror(pParse, pSub
37cc6 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 69 66 28  Parse);..    if(
37cc7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
37cc8 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 70  d==0 ){..      p
37cc9 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 20 3d 20 73  Program->aOp = s
37cca 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70  qlite3VdbeTakeOp
37ccb 41 72 72 61 79 28 76 2c 20 26 70 50 72 6f 67 72  Array(v, &pProgr
37ccc 61 6d 2d 3e 6e 4f 70 2c 20 26 70 54 6f 70 2d 3e  am->nOp, &pTop->
37ccd 6e 4d 61 78 41 72 67 29 3b 0d 0a 20 20 20 20 7d  nMaxArg);..    }
37cce 0d 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e  ..    pProgram->
37ccf 6e 4d 65 6d 20 3d 20 70 53 75 62 50 61 72 73 65  nMem = pSubParse
37cd0 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 70 50 72  ->nMem;..    pPr
37cd1 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 3d 20 70 53  ogram->nCsr = pS
37cd2 75 62 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0d 0a  ubParse->nTab;..
37cd3 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f      pProgram->nO
37cd4 6e 63 65 20 3d 20 70 53 75 62 50 61 72 73 65 2d  nce = pSubParse-
37cd5 3e 6e 4f 6e 63 65 3b 0d 0a 20 20 20 20 70 50 72  >nOnce;..    pPr
37cd6 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e 20 3d 20 28  ogram->token = (
37cd7 76 6f 69 64 20 2a 29 70 54 72 69 67 67 65 72 3b  void *)pTrigger;
37cd8 0d 0a 20 20 20 20 70 50 72 67 2d 3e 61 43 6f 6c  ..    pPrg->aCol
37cd9 6d 61 73 6b 5b 30 5d 20 3d 20 70 53 75 62 50 61  mask[0] = pSubPa
37cda 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 3b 0d 0a 20  rse->oldmask;.. 
37cdb 20 20 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73     pPrg->aColmas
37cdc 6b 5b 31 5d 20 3d 20 70 53 75 62 50 61 72 73 65  k[1] = pSubParse
37cdd 2d 3e 6e 65 77 6d 61 73 6b 3b 0d 0a 20 20 20 20  ->newmask;..    
37cde 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
37cdf 65 28 76 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  e(v);..  }....  
37ce0 61 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72  assert( !pSubPar
37ce1 73 65 2d 3e 70 41 69 6e 63 20 20 20 20 20 20 20  se->pAinc       
37ce2 26 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 70  && !pSubParse->p
37ce3 5a 6f 6d 62 69 65 54 61 62 20 29 3b 0d 0a 20 20  ZombieTab );..  
37ce4 61 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72  assert( !pSubPar
37ce5 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20  se->pTriggerPrg 
37ce6 26 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 6e  && !pSubParse->n
37ce7 4d 61 78 41 72 67 20 29 3b 0d 0a 20 20 73 71 6c  MaxArg );..  sql
37ce8 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62  ite3StackFree(db
37ce9 2c 20 70 53 75 62 50 61 72 73 65 29 3b 0d 0a 0d  , pSubParse);...
37cea 0a 20 20 72 65 74 75 72 6e 20 70 50 72 67 3b 0d  .  return pPrg;.
37ceb 0a 7d 0d 0a 20 20 20 20 0d 0a 2f 2a 0d 0a 2a 2a  .}..    ../*..**
37cec 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
37ced 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 50 72  r to a TriggerPr
37cee 67 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e  g object contain
37cef 69 6e 67 20 74 68 65 20 73 75 62 2d 70 72 6f 67  ing the sub-prog
37cf0 72 61 6d 20 66 6f 72 0d 0a 2a 2a 20 74 72 69 67  ram for..** trig
37cf1 67 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74  ger pTrigger wit
37cf2 68 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e  h default ON CON
37cf3 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 20  FLICT algorithm 
37cf4 6f 72 63 6f 6e 66 2e 20 49 66 20 6e 6f 20 73 75  orconf. If no su
37cf5 63 68 0d 0a 2a 2a 20 54 72 69 67 67 65 72 50 72  ch..** TriggerPr
37cf6 67 20 6f 62 6a 65 63 74 20 65 78 69 73 74 73 2c  g object exists,
37cf7 20 61 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 73   a new object is
37cf8 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70   allocated and p
37cf9 6f 70 75 6c 61 74 65 64 20 62 65 66 6f 72 65 0d  opulated before.
37cfa 0a 2a 2a 20 62 65 69 6e 67 20 72 65 74 75 72 6e  .** being return
37cfb 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ed...*/..static 
37cfc 54 72 69 67 67 65 72 50 72 67 20 2a 67 65 74 52  TriggerPrg *getR
37cfd 6f 77 54 72 69 67 67 65 72 28 0d 0a 20 20 50 61  owTrigger(..  Pa
37cfe 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
37cff 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 61     /* Current pa
37d00 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  rse context */..
37d01 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
37d02 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67 67 65  ger,   /* Trigge
37d03 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  r to code */..  
37d04 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
37d05 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
37d06 65 20 74 72 69 67 67 65 72 20 70 54 72 69 67 67  e trigger pTrigg
37d07 65 72 20 69 73 20 61 74 74 61 63 68 65 64 20 74  er is attached t
37d08 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 72 63 6f  o */..  int orco
37d09 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf           /* 
37d0a 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f  ON CONFLICT algo
37d0b 72 69 74 68 6d 2e 20 2a 2f 0d 0a 29 7b 0d 0a 20  rithm. */..){.. 
37d0c 20 50 61 72 73 65 20 2a 70 52 6f 6f 74 20 3d 20   Parse *pRoot = 
37d0d 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
37d0e 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0d 0a 20  evel(pParse);.. 
37d0f 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72   TriggerPrg *pPr
37d10 67 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  g;....  assert( 
37d11 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d  pTrigger->zName=
37d12 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62 6c  =0 || pTab==tabl
37d13 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67  eOfTrigger(pTrig
37d14 67 65 72 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  ger) );....  /* 
37d15 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 20 74  It may be that t
37d16 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73 20  his trigger has 
37d17 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 64  already been cod
37d18 65 64 20 28 6f 72 20 69 73 20 69 6e 20 74 68 65  ed (or is in the
37d19 0d 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f  ..  ** process o
37d1a 66 20 62 65 69 6e 67 20 63 6f 64 65 64 29 2e 20  f being coded). 
37d1b 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
37d1c 61 73 65 2c 20 74 68 65 6e 20 61 6e 20 65 6e 74  ase, then an ent
37d1d 72 79 20 77 69 74 68 0d 0a 20 20 2a 2a 20 61 20  ry with..  ** a 
37d1e 6d 61 74 63 68 69 6e 67 20 54 72 69 67 67 65 72  matching Trigger
37d1f 50 72 67 2e 70 54 72 69 67 67 65 72 20 66 69 65  Prg.pTrigger fie
37d20 6c 64 20 77 69 6c 6c 20 62 65 20 70 72 65 73 65  ld will be prese
37d21 6e 74 20 73 6f 6d 65 77 68 65 72 65 0d 0a 20 20  nt somewhere..  
37d22 2a 2a 20 69 6e 20 74 68 65 20 50 61 72 73 65 2e  ** in the Parse.
37d23 70 54 72 69 67 67 65 72 50 72 67 20 6c 69 73 74  pTriggerPrg list
37d24 2e 20 53 65 61 72 63 68 20 66 6f 72 20 73 75 63  . Search for suc
37d25 68 20 61 6e 20 65 6e 74 72 79 2e 20 20 2a 2f 0d  h an entry.  */.
37d26 0a 20 20 66 6f 72 28 70 50 72 67 3d 70 52 6f 6f  .  for(pPrg=pRoo
37d27 74 2d 3e 70 54 72 69 67 67 65 72 50 72 67 3b 20  t->pTriggerPrg; 
37d28 0d 0a 20 20 20 20 20 20 70 50 72 67 20 26 26 20  ..      pPrg && 
37d29 28 70 50 72 67 2d 3e 70 54 72 69 67 67 65 72 21  (pPrg->pTrigger!
37d2a 3d 70 54 72 69 67 67 65 72 20 7c 7c 20 70 50 72  =pTrigger || pPr
37d2b 67 2d 3e 6f 72 63 6f 6e 66 21 3d 6f 72 63 6f 6e  g->orconf!=orcon
37d2c 66 29 3b 20 0d 0a 20 20 20 20 20 20 70 50 72 67  f); ..      pPrg
37d2d 3d 70 50 72 67 2d 3e 70 4e 65 78 74 0d 0a 20 20  =pPrg->pNext..  
37d2e 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 6e  );....  /* If an
37d2f 20 65 78 69 73 74 69 6e 67 20 54 72 69 67 67 65   existing Trigge
37d30 72 50 72 67 20 63 6f 75 6c 64 20 6e 6f 74 20 62  rPrg could not b
37d31 65 20 6c 6f 63 61 74 65 64 2c 20 63 72 65 61 74  e located, creat
37d32 65 20 61 20 6e 65 77 20 6f 6e 65 2e 20 2a 2f 0d  e a new one. */.
37d33 0a 20 20 69 66 28 20 21 70 50 72 67 20 29 7b 0d  .  if( !pPrg ){.
37d34 0a 20 20 20 20 70 50 72 67 20 3d 20 63 6f 64 65  .    pPrg = code
37d35 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
37d36 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70 54 61  e, pTrigger, pTa
37d37 62 2c 20 6f 72 63 6f 6e 66 29 3b 0d 0a 20 20 7d  b, orconf);..  }
37d38 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 72  ....  return pPr
37d39 67 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  g;..}..../*..** 
37d3a 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
37d3b 72 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  r the trigger pr
37d3c 6f 67 72 61 6d 20 61 73 73 6f 63 69 61 74 65 64  ogram associated
37d3d 20 77 69 74 68 20 74 72 69 67 67 65 72 20 70 20   with trigger p 
37d3e 6f 6e 20 0d 0a 2a 2a 20 74 61 62 6c 65 20 70 54  on ..** table pT
37d3f 61 62 2e 20 54 68 65 20 72 65 67 2c 20 6f 72 63  ab. The reg, orc
37d40 6f 6e 66 20 61 6e 64 20 69 67 6e 6f 72 65 4a 75  onf and ignoreJu
37d41 6d 70 20 70 61 72 61 6d 65 74 65 72 73 20 70 61  mp parameters pa
37d42 73 73 65 64 20 74 6f 20 74 68 69 73 0d 0a 2a 2a  ssed to this..**
37d43 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
37d44 65 20 73 61 6d 65 20 61 73 20 74 68 6f 73 65 20  e same as those 
37d45 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65  described in the
37d46 20 68 65 61 64 65 72 20 66 75 6e 63 74 69 6f 6e   header function
37d47 20 66 6f 72 0d 0a 2a 2a 20 73 71 6c 69 74 65 33   for..** sqlite3
37d48 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 29  CodeRowTrigger()
37d49 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
37d4a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37d4b 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
37d4c 69 72 65 63 74 28 0d 0a 20 20 50 61 72 73 65 20  irect(..  Parse 
37d4d 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
37d4e 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
37d4f 2a 2f 0d 0a 20 20 54 72 69 67 67 65 72 20 2a 70  */..  Trigger *p
37d50 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
37d51 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f  igger to code */
37d52 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  ..  Table *pTab,
37d53 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
37d54 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72  table to code tr
37d55 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0d 0a  iggers from */..
37d56 20 20 69 6e 74 20 72 65 67 2c 20 20 20 20 20 20    int reg,      
37d57 20 20 20 20 20 20 20 2f 2a 20 52 65 67 20 61 72         /* Reg ar
37d58 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 4f  ray containing O
37d59 4c 44 2e 2a 20 61 6e 64 20 4e 45 57 2e 2a 20 76  LD.* and NEW.* v
37d5a 61 6c 75 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  alues */..  int 
37d5b 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20  orconf,         
37d5c 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   /* ON CONFLICT 
37d5d 70 6f 6c 69 63 79 20 2a 2f 0d 0a 20 20 69 6e 74  policy */..  int
37d5e 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20   ignoreJump     
37d5f 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e    /* Instruction
37d60 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20   to jump to for 
37d61 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f  RAISE(IGNORE) */
37d62 0d 0a 29 7b 0d 0a 20 20 56 64 62 65 20 2a 76 20  ..){..  Vdbe *v 
37d63 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
37d64 28 70 50 61 72 73 65 29 3b 20 2f 2a 20 4d 61 69  (pParse); /* Mai
37d65 6e 20 56 4d 20 2a 2f 0d 0a 20 20 54 72 69 67 67  n VM */..  Trigg
37d66 65 72 50 72 67 20 2a 70 50 72 67 3b 0d 0a 20 20  erPrg *pPrg;..  
37d67 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69  pPrg = getRowTri
37d68 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20  gger(pParse, p, 
37d69 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0d 0a  pTab, orconf);..
37d6a 20 20 61 73 73 65 72 74 28 20 70 50 72 67 20 7c    assert( pPrg |
37d6b 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  | pParse->nErr |
37d6c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
37d6d 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 0d  llocFailed );...
37d6e 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4f  .  /* Code the O
37d6f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65  P_Program opcode
37d70 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 56   in the parent V
37d71 44 42 45 2e 20 50 34 20 6f 66 20 74 68 65 20 4f  DBE. P4 of the O
37d72 50 5f 50 72 6f 67 72 61 6d 20 0d 0a 20 20 2a 2a  P_Program ..  **
37d73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
37d74 20 74 68 65 20 73 75 62 2d 76 64 62 65 20 63 6f   the sub-vdbe co
37d75 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69  ntaining the tri
37d76 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 20 2a  gger program.  *
37d77 2f 0d 0a 20 20 69 66 28 20 70 50 72 67 20 29 7b  /..  if( pPrg ){
37d78 0d 0a 20 20 20 20 69 6e 74 20 62 52 65 63 75 72  ..    int bRecur
37d79 73 69 76 65 20 3d 20 28 70 2d 3e 7a 4e 61 6d 65  sive = (p->zName
37d7a 20 26 26 20 30 3d 3d 28 70 50 61 72 73 65 2d 3e   && 0==(pParse->
37d7b 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
37d7c 5f 52 65 63 54 72 69 67 67 65 72 73 29 29 3b 0d  _RecTriggers));.
37d7d 0a 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ...    sqlite3Vd
37d7e 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 50  beAddOp3(v, OP_P
37d7f 72 6f 67 72 61 6d 2c 20 72 65 67 2c 20 69 67 6e  rogram, reg, ign
37d80 6f 72 65 4a 75 6d 70 2c 20 2b 2b 70 50 61 72 73  oreJump, ++pPars
37d81 65 2d 3e 6e 4d 65 6d 29 3b 0d 0a 20 20 20 20 73  e->nMem);..    s
37d82 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
37d83 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
37d84 20 63 68 61 72 20 2a 29 70 50 72 67 2d 3e 70 50   char *)pPrg->pP
37d85 72 6f 67 72 61 6d 2c 20 50 34 5f 53 55 42 50 52  rogram, P4_SUBPR
37d86 4f 47 52 41 4d 29 3b 0d 0a 20 20 20 20 56 64 62  OGRAM);..    Vdb
37d87 65 43 6f 6d 6d 65 6e 74 28 0d 0a 20 20 20 20 20  eComment(..     
37d88 20 20 20 28 76 2c 20 22 43 61 6c 6c 3a 20 25 73     (v, "Call: %s
37d89 2e 25 73 22 2c 20 28 70 2d 3e 7a 4e 61 6d 65 3f  .%s", (p->zName?
37d8a 70 2d 3e 7a 4e 61 6d 65 3a 22 66 6b 65 79 22 29  p->zName:"fkey")
37d8b 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72  , onErrorText(or
37d8c 63 6f 6e 66 29 29 29 3b 0d 0a 0d 0a 20 20 20 20  conf)));....    
37d8d 2f 2a 20 53 65 74 20 74 68 65 20 50 35 20 6f 70  /* Set the P5 op
37d8e 65 72 61 6e 64 20 6f 66 20 74 68 65 20 4f 50 5f  erand of the OP_
37d8f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74  Program instruct
37d90 69 6f 6e 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20  ion to non-zero 
37d91 69 66 0d 0a 20 20 20 20 2a 2a 20 72 65 63 75 72  if..    ** recur
37d92 73 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20  sive invocation 
37d93 6f 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20  of this trigger 
37d94 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 61 6c  program is disal
37d95 6c 6f 77 65 64 2e 20 52 65 63 75 72 73 69 76 65  lowed. Recursive
37d96 0d 0a 20 20 20 20 2a 2a 20 69 6e 76 6f 63 61 74  ..    ** invocat
37d97 69 6f 6e 20 69 73 20 64 69 73 61 6c 6c 6f 77 65  ion is disallowe
37d98 64 20 69 66 20 28 61 29 20 74 68 65 20 73 75 62  d if (a) the sub
37d99 2d 70 72 6f 67 72 61 6d 20 69 73 20 72 65 61 6c  -program is real
37d9a 6c 79 20 61 20 74 72 69 67 67 65 72 2c 0d 0a 20  ly a trigger,.. 
37d9b 20 20 20 2a 2a 20 6e 6f 74 20 61 20 66 6f 72 65     ** not a fore
37d9c 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20  ign key action, 
37d9d 61 6e 64 20 28 62 29 20 74 68 65 20 66 6c 61 67  and (b) the flag
37d9e 20 74 6f 20 65 6e 61 62 6c 65 20 72 65 63 75 72   to enable recur
37d9f 73 69 76 65 20 74 72 69 67 67 65 72 73 0d 0a 20  sive triggers.. 
37da0 20 20 20 2a 2a 20 69 73 20 63 6c 65 61 72 2e 20     ** is clear. 
37da1 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   */..    sqlite3
37da2 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
37da3 28 75 38 29 62 52 65 63 75 72 73 69 76 65 29 3b  (u8)bRecursive);
37da4 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
37da5 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  ** This is calle
37da6 64 20 74 6f 20 63 6f 64 65 20 74 68 65 20 72 65  d to code the re
37da7 71 75 69 72 65 64 20 46 4f 52 20 45 41 43 48 20  quired FOR EACH 
37da8 52 4f 57 20 74 72 69 67 67 65 72 73 20 66 6f 72  ROW triggers for
37da9 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 0d 0a 2a   an operation..*
37daa 2a 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 2e  * on table pTab.
37dab 20 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74   The operation t
37dac 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20  o code triggers 
37dad 66 6f 72 20 28 49 4e 53 45 52 54 2c 20 55 50 44  for (INSERT, UPD
37dae 41 54 45 20 6f 72 20 44 45 4c 45 54 45 29 0d 0a  ATE or DELETE)..
37daf 2a 2a 20 69 73 20 67 69 76 65 6e 20 62 79 20 74  ** is given by t
37db0 68 65 20 6f 70 20 70 61 72 61 6d 61 74 65 72 2e  he op paramater.
37db1 20 54 68 65 20 74 72 5f 74 6d 20 70 61 72 61 6d   The tr_tm param
37db2 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20  eter determines 
37db3 77 68 65 74 68 65 72 20 74 68 65 0d 0a 2a 2a 20  whether the..** 
37db4 42 45 46 4f 52 45 20 6f 72 20 41 46 54 45 52 20  BEFORE or AFTER 
37db5 74 72 69 67 67 65 72 73 20 61 72 65 20 63 6f 64  triggers are cod
37db6 65 64 2e 20 49 66 20 74 68 65 20 6f 70 65 72 61  ed. If the opera
37db7 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44 41 54  tion is an UPDAT
37db8 45 2c 20 74 68 65 6e 0d 0a 2a 2a 20 70 61 72 61  E, then..** para
37db9 6d 65 74 65 72 20 70 43 68 61 6e 67 65 73 20 69  meter pChanges i
37dba 73 20 70 61 73 73 65 64 20 74 68 65 20 6c 69 73  s passed the lis
37dbb 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69  t of columns bei
37dbc 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2a  ng modified...**
37dbd 0d 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  ..** If there ar
37dbe 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20 74 68  e no triggers th
37dbf 61 74 20 66 69 72 65 20 61 74 20 74 68 65 20 73  at fire at the s
37dc0 70 65 63 69 66 69 65 64 20 74 69 6d 65 20 66 6f  pecified time fo
37dc1 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0d  r the specified.
37dc2 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e  .** operation on
37dc3 20 70 54 61 62 2c 20 74 68 69 73 20 66 75 6e 63   pTab, this func
37dc4 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
37dc5 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 65 67  ..**..** The reg
37dc6 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
37dc7 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   address of the 
37dc8 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61  first in an arra
37dc9 79 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 0d  y of registers .
37dca 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  .** that contain
37dcb 20 74 68 65 20 76 61 6c 75 65 73 20 73 75 62 73   the values subs
37dcc 74 69 74 75 74 65 64 20 66 6f 72 20 74 68 65 20  tituted for the 
37dcd 6e 65 77 2e 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20  new.* and old.* 
37dce 72 65 66 65 72 65 6e 63 65 73 0d 0a 2a 2a 20 69  references..** i
37dcf 6e 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  n the trigger pr
37dd0 6f 67 72 61 6d 2e 20 49 66 20 4e 20 69 73 20 74  ogram. If N is t
37dd1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
37dd2 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70 54  umns in table pT
37dd3 61 62 0d 0a 2a 2a 20 28 61 20 63 6f 70 79 20 6f  ab..** (a copy o
37dd4 66 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20 74  f pTab->nCol), t
37dd5 68 65 6e 20 72 65 67 69 73 74 65 72 73 20 61 72  hen registers ar
37dd6 65 20 70 6f 70 75 6c 61 74 65 64 20 61 73 20 66  e populated as f
37dd7 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  ollows:..**..** 
37dd8 20 20 52 65 67 69 73 74 65 72 20 20 20 20 20 20    Register      
37dd9 20 43 6f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 20 20   Contains..**   
37dda 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
37ddb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
37ddc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
37ddd 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20 20 20 72 65 67  ------..**   reg
37dde 2b 30 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e  +0          OLD.
37ddf 72 6f 77 69 64 0d 0a 2a 2a 20 20 20 72 65 67 2b  rowid..**   reg+
37de0 31 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a  1          OLD.*
37de1 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d   value of left-m
37de2 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54  ost column of pT
37de3 61 62 0d 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20 20  ab..**   ...    
37de4 20 20 20 20 20 20 20 20 2e 2e 2e 0d 0a 2a 2a 20          .....** 
37de5 20 20 72 65 67 2b 4e 20 20 20 20 20 20 20 20 20    reg+N         
37de6 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20   OLD.* value of 
37de7 72 69 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d  right-most colum
37de8 6e 20 6f 66 20 70 54 61 62 0d 0a 2a 2a 20 20 20  n of pTab..**   
37de9 72 65 67 2b 4e 2b 31 20 20 20 20 20 20 20 20 4e  reg+N+1        N
37dea 45 57 2e 72 6f 77 69 64 0d 0a 2a 2a 20 20 20 72  EW.rowid..**   r
37deb 65 67 2b 4e 2b 32 20 20 20 20 20 20 20 20 4f 4c  eg+N+2        OL
37dec 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20 6c 65 66  D.* value of lef
37ded 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66  t-most column of
37dee 20 70 54 61 62 0d 0a 2a 2a 20 20 20 2e 2e 2e 20   pTab..**   ... 
37def 20 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0d 0a             .....
37df0 2a 2a 20 20 20 72 65 67 2b 4e 2b 4e 2b 31 20 20  **   reg+N+N+1  
37df1 20 20 20 20 4e 45 57 2e 2a 20 76 61 6c 75 65 20      NEW.* value 
37df2 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 6f  of right-most co
37df3 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0d 0a 2a 2a  lumn of pTab..**
37df4 0d 0a 2a 2a 20 46 6f 72 20 4f 4e 20 44 45 4c 45  ..** For ON DELE
37df5 54 45 20 74 72 69 67 67 65 72 73 2c 20 74 68 65  TE triggers, the
37df6 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61   registers conta
37df7 69 6e 69 6e 67 20 74 68 65 20 4e 45 57 2e 2a 20  ining the NEW.* 
37df8 76 61 6c 75 65 73 20 77 69 6c 6c 0d 0a 2a 2a 20  values will..** 
37df9 6e 65 76 65 72 20 62 65 20 61 63 63 65 73 73 65  never be accesse
37dfa 64 20 62 79 20 74 68 65 20 74 72 69 67 67 65 72  d by the trigger
37dfb 20 70 72 6f 67 72 61 6d 2c 20 73 6f 20 74 68 65   program, so the
37dfc 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 63 61  y are not alloca
37dfd 74 65 64 20 6f 72 20 0d 0a 2a 2a 20 70 6f 70 75  ted or ..** popu
37dfe 6c 61 74 65 64 20 62 79 20 74 68 65 20 63 61 6c  lated by the cal
37dff 6c 65 72 20 28 74 68 65 72 65 20 69 73 20 6e 6f  ler (there is no
37e00 20 64 61 74 61 20 74 6f 20 70 6f 70 75 6c 61 74   data to populat
37e01 65 20 74 68 65 6d 20 77 69 74 68 20 61 6e 79 77  e them with anyw
37e02 61 79 29 2e 20 0d 0a 2a 2a 20 53 69 6d 69 6c 61  ay). ..** Simila
37e03 72 6c 79 2c 20 66 6f 72 20 4f 4e 20 49 4e 53 45  rly, for ON INSE
37e04 52 54 20 74 72 69 67 67 65 72 73 20 74 68 65 20  RT triggers the 
37e05 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e  values stored in
37e06 20 74 68 65 20 4f 4c 44 2e 2a 20 72 65 67 69 73   the OLD.* regis
37e07 74 65 72 73 0d 0a 2a 2a 20 61 72 65 20 6e 65 76  ters..** are nev
37e08 65 72 20 61 63 63 65 73 73 65 64 2c 20 61 6e 64  er accessed, and
37e09 20 73 6f 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f   so are not allo
37e0a 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61 6c  cated by the cal
37e0b 6c 65 72 2e 20 53 6f 2c 20 66 6f 72 20 61 6e 0d  ler. So, for an.
37e0c 0a 2a 2a 20 4f 4e 20 49 4e 53 45 52 54 20 74 72  .** ON INSERT tr
37e0d 69 67 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65  igger, the value
37e0e 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
37e0f 66 75 6e 63 74 69 6f 6e 20 61 73 20 70 61 72 61  function as para
37e10 6d 65 74 65 72 20 72 65 67 0d 0a 2a 2a 20 69 73  meter reg..** is
37e11 20 6e 6f 74 20 61 20 72 65 61 64 61 62 6c 65 20   not a readable 
37e12 72 65 67 69 73 74 65 72 2c 20 61 6c 74 68 6f 75  register, althou
37e13 67 68 20 72 65 67 69 73 74 65 72 73 20 28 72 65  gh registers (re
37e14 67 2b 4e 29 20 74 68 72 6f 75 67 68 20 0d 0a 2a  g+N) through ..*
37e15 2a 20 28 72 65 67 2b 4e 2b 4e 2b 31 29 20 61 72  * (reg+N+N+1) ar
37e16 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 50 61 72 61 6d  e...**..** Param
37e17 65 74 65 72 20 6f 72 63 6f 6e 66 20 69 73 20 74  eter orconf is t
37e18 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 6c  he default confl
37e19 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61  ict resolution a
37e1a 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 74 68 65  lgorithm for the
37e1b 0d 0a 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f  ..** trigger pro
37e1c 67 72 61 6d 20 74 6f 20 75 73 65 20 28 52 45 50  gram to use (REP
37e1d 4c 41 43 45 2c 20 49 47 4e 4f 52 45 20 65 74 63  LACE, IGNORE etc
37e1e 2e 29 2e 20 50 61 72 61 6d 65 74 65 72 20 69 67  .). Parameter ig
37e1f 6e 6f 72 65 4a 75 6d 70 0d 0a 2a 2a 20 69 73 20  noreJump..** is 
37e20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
37e21 74 68 61 74 20 63 6f 6e 74 72 6f 6c 20 73 68 6f  that control sho
37e22 75 6c 64 20 6a 75 6d 70 20 74 6f 20 69 66 20 61  uld jump to if a
37e23 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
37e24 0d 0a 2a 2a 20 72 61 69 73 65 73 20 61 6e 20 49  ..** raises an I
37e25 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e  GNORE exception.
37e26 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
37e27 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37e28 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
37e29 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
37e2a 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
37e2b 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  e context */..  
37e2c 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
37e2d 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  r,   /* List of 
37e2e 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
37e2f 65 20 70 54 61 62 20 2a 2f 0d 0a 20 20 69 6e 74  e pTab */..  int
37e30 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
37e31 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55    /* One of TK_U
37e32 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
37e33 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0d 0a  , TK_DELETE */..
37e34 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
37e35 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65  nges,  /* Change
37e36 73 20 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55  s list for any U
37e37 50 44 41 54 45 20 4f 46 20 74 72 69 67 67 65 72  PDATE OF trigger
37e38 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 74 72 5f 74  s */..  int tr_t
37e39 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  m,           /* 
37e3a 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42  One of TRIGGER_B
37e3b 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41  EFORE, TRIGGER_A
37e3c 46 54 45 52 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  FTER */..  Table
37e3d 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
37e3e 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
37e3f 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72  code triggers fr
37e40 6f 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  om */..  int reg
37e41 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
37e42 20 54 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   The first in an
37e43 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74   array of regist
37e44 65 72 73 20 28 73 65 65 20 61 62 6f 76 65 29 20  ers (see above) 
37e45 2a 2f 0d 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66  */..  int orconf
37e46 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e  ,          /* ON
37e47 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79   CONFLICT policy
37e48 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 67 6e 6f 72   */..  int ignor
37e49 65 4a 75 6d 70 20 20 20 20 20 20 20 2f 2a 20 49  eJump       /* I
37e4a 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6a 75  nstruction to ju
37e4b 6d 70 20 74 6f 20 66 6f 72 20 52 41 49 53 45 28  mp to for RAISE(
37e4c 49 47 4e 4f 52 45 29 20 2a 2f 0d 0a 29 7b 0d 0a  IGNORE) */..){..
37e4d 20 20 54 72 69 67 67 65 72 20 2a 70 3b 20 20 20    Trigger *p;   
37e4e 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
37e4f 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
37e50 68 20 70 54 72 69 67 67 65 72 20 6c 69 73 74 20  h pTrigger list 
37e51 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
37e52 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c  op==TK_UPDATE ||
37e53 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c   op==TK_INSERT |
37e54 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20  | op==TK_DELETE 
37e55 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 74 72  );..  assert( tr
37e56 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45 46  _tm==TRIGGER_BEF
37e57 4f 52 45 20 7c 7c 20 74 72 5f 74 6d 3d 3d 54 52  ORE || tr_tm==TR
37e58 49 47 47 45 52 5f 41 46 54 45 52 20 29 3b 0d 0a  IGGER_AFTER );..
37e59 20 20 61 73 73 65 72 74 28 20 28 6f 70 3d 3d 54    assert( (op==T
37e5a 4b 5f 55 50 44 41 54 45 29 3d 3d 28 70 43 68 61  K_UPDATE)==(pCha
37e5b 6e 67 65 73 21 3d 30 29 20 29 3b 0d 0a 0d 0a 20  nges!=0) );.... 
37e5c 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72 3b   for(p=pTrigger;
37e5d 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
37e5e 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 61 6e 69 74  ....    /* Sanit
37e5f 79 20 63 68 65 63 6b 69 6e 67 3a 20 20 54 68 65  y checking:  The
37e60 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20   schema for the 
37e61 74 72 69 67 67 65 72 20 61 6e 64 20 66 6f 72 20  trigger and for 
37e62 74 68 65 20 74 61 62 6c 65 20 61 72 65 0d 0a 20  the table are.. 
37e63 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 64 65 66     ** always def
37e64 69 6e 65 64 2e 20 20 54 68 65 20 74 72 69 67 67  ined.  The trigg
37e65 65 72 20 6d 75 73 74 20 62 65 20 69 6e 20 74 68  er must be in th
37e66 65 20 73 61 6d 65 20 73 63 68 65 6d 61 20 61 73  e same schema as
37e67 20 74 68 65 20 74 61 62 6c 65 0d 0a 20 20 20 20   the table..    
37e68 2a 2a 20 6f 72 20 65 6c 73 65 20 69 74 20 6d 75  ** or else it mu
37e69 73 74 20 62 65 20 61 20 54 45 4d 50 20 74 72 69  st be a TEMP tri
37e6a 67 67 65 72 2e 20 2a 2f 0d 0a 20 20 20 20 61 73  gger. */..    as
37e6b 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d 61  sert( p->pSchema
37e6c 21 3d 30 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  !=0 );..    asse
37e6d 72 74 28 20 70 2d 3e 70 54 61 62 53 63 68 65 6d  rt( p->pTabSchem
37e6e 61 21 3d 30 20 29 3b 0d 0a 20 20 20 20 61 73 73  a!=0 );..    ass
37e6f 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d 61 3d  ert( p->pSchema=
37e70 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 0d  =p->pTabSchema .
37e71 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e  .         || p->
37e72 70 53 63 68 65 6d 61 3d 3d 70 50 61 72 73 65 2d  pSchema==pParse-
37e73 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  >db->aDb[1].pSch
37e74 65 6d 61 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  ema );....    /*
37e75 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   Determine wheth
37e76 65 72 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 64  er we should cod
37e77 65 20 74 68 69 73 20 74 72 69 67 67 65 72 20 2a  e this trigger *
37e78 2f 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  /..    if( p->op
37e79 3d 3d 6f 70 20 0d 0a 20 20 20 20 20 26 26 20 70  ==op ..     && p
37e7a 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 0d  ->tr_tm==tr_tm .
37e7b 0a 20 20 20 20 20 26 26 20 63 68 65 63 6b 43 6f  .     && checkCo
37e7c 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70  lumnOverlap(p->p
37e7d 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65  Columns, pChange
37e7e 73 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20  s)..    ){..    
37e7f 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
37e80 54 72 69 67 67 65 72 44 69 72 65 63 74 28 70 50  TriggerDirect(pP
37e81 61 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 72  arse, p, pTab, r
37e82 65 67 2c 20 6f 72 63 6f 6e 66 2c 20 69 67 6e 6f  eg, orconf, igno
37e83 72 65 4a 75 6d 70 29 3b 0d 0a 20 20 20 20 7d 0d  reJump);..    }.
37e84 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
37e85 2a 20 54 72 69 67 67 65 72 73 20 6d 61 79 20 61  * Triggers may a
37e86 63 63 65 73 73 20 76 61 6c 75 65 73 20 73 74 6f  ccess values sto
37e87 72 65 64 20 69 6e 20 74 68 65 20 6f 6c 64 2e 2a  red in the old.*
37e88 20 6f 72 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f   or new.* pseudo
37e89 2d 74 61 62 6c 65 2e 20 0d 0a 2a 2a 20 54 68 69  -table. ..** Thi
37e8a 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
37e8b 6e 73 20 61 20 33 32 2d 62 69 74 20 62 69 74 6d  ns a 32-bit bitm
37e8c 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77  ask indicating w
37e8d 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  hich columns of 
37e8e 74 68 65 20 0d 0a 2a 2a 20 6f 6c 64 2e 2a 20 6f  the ..** old.* o
37e8f 72 20 6e 65 77 2e 2a 20 74 61 62 6c 65 73 20 61  r new.* tables a
37e90 63 74 75 61 6c 6c 79 20 61 72 65 20 75 73 65 64  ctually are used
37e91 20 62 79 20 74 72 69 67 67 65 72 73 2e 20 54 68   by triggers. Th
37e92 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0d  is information .
37e93 0a 2a 2a 20 6d 61 79 20 62 65 20 75 73 65 64 20  .** may be used 
37e94 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2c 20 66  by the caller, f
37e95 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61  or example, to a
37e96 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 6c  void having to l
37e97 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 0d 0a  oad the entire..
37e98 2a 2a 20 6f 6c 64 2e 2a 20 72 65 63 6f 72 64 20  ** old.* record 
37e99 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 77 68 65 6e  into memory when
37e9a 20 65 78 65 63 75 74 69 6e 67 20 61 6e 20 55 50   executing an UP
37e9b 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 63  DATE or DELETE c
37e9c 6f 6d 6d 61 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ommand...**..** 
37e9d 42 69 74 20 30 20 6f 66 20 74 68 65 20 72 65 74  Bit 0 of the ret
37e9e 75 72 6e 65 64 20 6d 61 73 6b 20 69 73 20 73 65  urned mask is se
37e9f 74 20 69 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f  t if the left-mo
37ea0 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
37ea1 0d 0a 2a 2a 20 74 61 62 6c 65 20 6d 61 79 20 62  ..** table may b
37ea2 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67  e accessed using
37ea3 20 61 6e 20 5b 6f 6c 64 7c 6e 65 77 5d 2e 3c 63   an [old|new].<c
37ea4 6f 6c 3e 20 72 65 66 65 72 65 6e 63 65 2e 20 42  ol> reference. B
37ea5 69 74 20 31 20 69 73 20 73 65 74 20 69 66 0d 0a  it 1 is set if..
37ea6 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65  ** the second le
37ea7 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 76 61  ftmost column va
37ea8 6c 75 65 20 69 73 20 72 65 71 75 69 72 65 64 2c  lue is required,
37ea9 20 61 6e 64 20 73 6f 20 6f 6e 2e 20 49 66 20 74   and so on. If t
37eaa 68 65 72 65 0d 0a 2a 2a 20 61 72 65 20 6d 6f 72  here..** are mor
37eab 65 20 74 68 61 6e 20 33 32 20 63 6f 6c 75 6d 6e  e than 32 column
37eac 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  s in the table, 
37ead 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  and at least one
37eae 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0d   of the columns.
37eaf 0a 2a 2a 20 77 69 74 68 20 61 6e 20 69 6e 64 65  .** with an inde
37eb0 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 33  x greater than 3
37eb1 32 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  2 may be accesse
37eb2 64 2c 20 30 78 66 66 66 66 66 66 66 66 20 69 73  d, 0xffffffff is
37eb3 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a   returned...**..
37eb4 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** It is not pos
37eb5 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69  sible to determi
37eb6 6e 65 20 69 66 20 74 68 65 20 6f 6c 64 2e 72 6f  ne if the old.ro
37eb7 77 69 64 20 6f 72 20 6e 65 77 2e 72 6f 77 69 64  wid or new.rowid
37eb8 20 63 6f 6c 75 6d 6e 20 69 73 20 0d 0a 2a 2a 20   column is ..** 
37eb9 61 63 63 65 73 73 65 64 20 62 79 20 74 72 69 67  accessed by trig
37eba 67 65 72 73 2e 20 54 68 65 20 63 61 6c 6c 65 72  gers. The caller
37ebb 20 6d 75 73 74 20 61 6c 77 61 79 73 20 61 73 73   must always ass
37ebc 75 6d 65 20 74 68 61 74 20 69 74 20 69 73 2e 0d  ume that it is..
37ebd 0a 2a 2a 0d 0a 2a 2a 20 50 61 72 61 6d 65 74 65  .**..** Paramete
37ebe 72 20 69 73 4e 65 77 20 6d 75 73 74 20 62 65 20  r isNew must be 
37ebf 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e 20 49  either 1 or 0. I
37ec0 66 20 69 74 20 69 73 20 30 2c 20 74 68 65 6e 20  f it is 0, then 
37ec1 74 68 65 20 6d 61 73 6b 20 72 65 74 75 72 6e 65  the mask returne
37ec2 64 0d 0a 2a 2a 20 61 70 70 6c 69 65 73 20 74 6f  d..** applies to
37ec3 20 74 68 65 20 6f 6c 64 2e 2a 20 74 61 62 6c 65   the old.* table
37ec4 2e 20 49 66 20 31 2c 20 74 68 65 20 6e 65 77 2e  . If 1, the new.
37ec5 2a 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  * table...**..**
37ec6 20 50 61 72 61 6d 65 74 65 72 20 74 72 5f 74 6d   Parameter tr_tm
37ec7 20 6d 75 73 74 20 62 65 20 61 20 6d 61 73 6b 20   must be a mask 
37ec8 77 69 74 68 20 6f 6e 65 20 6f 72 20 62 6f 74 68  with one or both
37ec9 20 6f 66 20 74 68 65 20 54 52 49 47 47 45 52 5f   of the TRIGGER_
37eca 42 45 46 4f 52 45 0d 0a 2a 2a 20 61 6e 64 20 54  BEFORE..** and T
37ecb 52 49 47 47 45 52 5f 41 46 54 45 52 20 62 69 74  RIGGER_AFTER bit
37ecc 73 20 73 65 74 2e 20 56 61 6c 75 65 73 20 61 63  s set. Values ac
37ecd 63 65 73 73 65 64 20 62 79 20 42 45 46 4f 52 45  cessed by BEFORE
37ece 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e   triggers are on
37ecf 6c 79 0d 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20  ly..** included 
37ed0 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  in the returned 
37ed1 6d 61 73 6b 20 69 66 20 74 68 65 20 54 52 49 47  mask if the TRIG
37ed2 47 45 52 5f 42 45 46 4f 52 45 20 62 69 74 20 69  GER_BEFORE bit i
37ed3 73 20 73 65 74 20 69 6e 20 74 68 65 0d 0a 2a 2a  s set in the..**
37ed4 20 74 72 5f 74 6d 20 70 61 72 61 6d 65 74 65 72   tr_tm parameter
37ed5 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 76 61 6c  . Similarly, val
37ed6 75 65 73 20 61 63 63 65 73 73 65 64 20 62 79 20  ues accessed by 
37ed7 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 61  AFTER triggers a
37ed8 72 65 20 6f 6e 6c 79 0d 0a 2a 2a 20 69 6e 63 6c  re only..** incl
37ed9 75 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75  uded in the retu
37eda 72 6e 65 64 20 6d 61 73 6b 20 69 66 20 74 68 65  rned mask if the
37edb 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 62   TRIGGER_AFTER b
37edc 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 72 5f  it is set in tr_
37edd 74 6d 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  tm...*/..SQLITE_
37ede 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
37edf 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73  te3TriggerColmas
37ee0 6b 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61  k(..  Parse *pPa
37ee1 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61  rse,       /* Pa
37ee2 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  rse context */..
37ee3 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
37ee4 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ger,   /* List o
37ee5 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61  f triggers on ta
37ee6 62 6c 65 20 70 54 61 62 20 2a 2f 0d 0a 20 20 45  ble pTab */..  E
37ee7 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
37ee8 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c  s,  /* Changes l
37ee9 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44 41  ist for any UPDA
37eea 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  TE OF triggers *
37eeb 2f 0d 0a 20 20 69 6e 74 20 69 73 4e 65 77 2c 20  /..  int isNew, 
37eec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
37eed 6f 72 20 6e 65 77 2e 2a 20 72 65 66 20 6d 61 73  or new.* ref mas
37eee 6b 2c 20 30 20 66 6f 72 20 6f 6c 64 2e 2a 20 72  k, 0 for old.* r
37eef 65 66 20 6d 61 73 6b 20 2a 2f 0d 0a 20 20 69 6e  ef mask */..  in
37ef0 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20  t tr_tm,        
37ef1 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 52     /* Mask of TR
37ef2 49 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49  IGGER_BEFORE|TRI
37ef3 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0d 0a 20  GGER_AFTER */.. 
37ef4 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
37ef5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
37ef6 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67  le to code trigg
37ef7 65 72 73 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69  ers from */..  i
37ef8 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20  nt orconf       
37ef9 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f      /* Default O
37efa 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
37efb 79 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74  y for trigger st
37efc 65 70 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63 6f  eps */..){..  co
37efd 6e 73 74 20 69 6e 74 20 6f 70 20 3d 20 70 43 68  nst int op = pCh
37efe 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41 54  anges ? TK_UPDAT
37eff 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45 3b 0d 0a  E : TK_DELETE;..
37f00 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30 3b 0d    u32 mask = 0;.
37f01 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0d 0a  .  Trigger *p;..
37f02 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73 4e 65  ..  assert( isNe
37f03 77 3d 3d 31 20 7c 7c 20 69 73 4e 65 77 3d 3d 30  w==1 || isNew==0
37f04 20 29 3b 0d 0a 20 20 66 6f 72 28 70 3d 70 54 72   );..  for(p=pTr
37f05 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  igger; p; p=p->p
37f06 4e 65 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20  Next){..    if( 
37f07 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26 20 28 74 72  p->op==op && (tr
37f08 5f 74 6d 26 70 2d 3e 74 72 5f 74 6d 29 0d 0a 20  _tm&p->tr_tm).. 
37f09 20 20 20 20 26 26 20 63 68 65 63 6b 43 6f 6c 75      && checkColu
37f0a 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f  mnOverlap(p->pCo
37f0b 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65 73 29 0d  lumns,pChanges).
37f0c 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 54  .    ){..      T
37f0d 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b  riggerPrg *pPrg;
37f0e 0d 0a 20 20 20 20 20 20 70 50 72 67 20 3d 20 67  ..      pPrg = g
37f0f 65 74 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  etRowTrigger(pPa
37f10 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72  rse, p, pTab, or
37f11 63 6f 6e 66 29 3b 0d 0a 20 20 20 20 20 20 69 66  conf);..      if
37f12 28 20 70 50 72 67 20 29 7b 0d 0a 20 20 20 20 20  ( pPrg ){..     
37f13 20 20 20 6d 61 73 6b 20 7c 3d 20 70 50 72 67 2d     mask |= pPrg-
37f14 3e 61 43 6f 6c 6d 61 73 6b 5b 69 73 4e 65 77 5d  >aColmask[isNew]
37f15 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
37f16 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75  }..  }....  retu
37f17 72 6e 20 6d 61 73 6b 3b 0d 0a 7d 0d 0a 0d 0a 23  rn mask;..}....#
37f18 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
37f19 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
37f1a 49 47 47 45 52 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a  IGGER) */..../**
37f1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
37f1c 20 6f 66 20 74 72 69 67 67 65 72 2e 63 20 2a 2a   of trigger.c **
37f1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
37f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
37f21 67 69 6e 20 66 69 6c 65 20 75 70 64 61 74 65 2e  gin file update.
37f22 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
37f23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
37f25 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  *..** 2001 Septe
37f26 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20  mber 15..**..** 
37f27 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
37f28 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
37f29 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
37f2a 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
37f2b 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  ..** a legal not
37f2c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
37f2d 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a  lessing:..**..**
37f2e 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
37f2f 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
37f30 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
37f31 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
37f32 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
37f33 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
37f34 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  s...**    May yo
37f35 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
37f36 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
37f37 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
37f38 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**..**********
37f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
37f3d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
37f3e 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
37f3f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
37f40 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
37f41 72 73 65 72 0d 0a 2a 2a 20 74 6f 20 68 61 6e 64  rser..** to hand
37f42 6c 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  le UPDATE statem
37f43 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66  ents...*/....#if
37f44 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
37f45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 2f  _VIRTUALTABLE../
37f46 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
37f47 61 74 69 6f 6e 20 2a 2f 0d 0a 73 74 61 74 69 63  ation */..static
37f48 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74   void updateVirt
37f49 75 61 6c 54 61 62 6c 65 28 0d 0a 20 20 50 61 72  ualTable(..  Par
37f4a 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
37f4b 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
37f4c 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 53   context */..  S
37f4d 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
37f4e 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
37f4f 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d  al table to be m
37f50 6f 64 69 66 69 65 64 20 2a 2f 0d 0a 20 20 54 61  odified */..  Ta
37f51 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
37f52 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
37f53 6c 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 45 78  l table */..  Ex
37f54 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
37f55 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  ,  /* The column
37f56 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74  s to change in t
37f57 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
37f58 65 6e 74 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  ent */..  Expr *
37f59 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20 20 2f  pRowidExpr,    /
37f5a 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65  * Expression use
37f5b 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74  d to recompute t
37f5c 68 65 20 72 6f 77 69 64 20 2a 2f 0d 0a 20 20 69  he rowid */..  i
37f5d 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
37f5e 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
37f5f 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
37f60 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
37f61 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0d 0a 20  n pChanges */.. 
37f62 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
37f63 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
37f64 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
37f65 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
37f66 0d 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20  ..  int onError 
37f67 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43           /* ON C
37f68 4f 4e 46 4c 49 43 54 20 73 74 72 61 74 65 67 79  ONFLICT strategy
37f69 20 2a 2f 0d 0a 29 3b 0d 0a 23 65 6e 64 69 66 20   */..);..#endif 
37f6a 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
37f6b 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a  IRTUALTABLE */..
37f6c 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 6d 6f 73  ../*..** The mos
37f6d 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 64  t recently coded
37f6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73   instruction was
37f6f 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f   an OP_Column to
37f70 20 72 65 74 72 69 65 76 65 20 74 68 65 0d 0a 2a   retrieve the..*
37f71 2a 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  * i-th column of
37f72 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68 69   table pTab. Thi
37f73 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
37f74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
37f75 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 4f 50 5f 43  of the ..** OP_C
37f76 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 64 65 66  olumn to the def
37f77 61 75 6c 74 20 76 61 6c 75 65 2c 20 69 66 20 61  ault value, if a
37f78 6e 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ny...**..** The 
37f79 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
37f7a 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65   a column is spe
37f7b 63 69 66 69 65 64 20 62 79 20 61 20 44 45 46 41  cified by a DEFA
37f7c 55 4c 54 20 63 6c 61 75 73 65 20 69 6e 20 74 68  ULT clause in th
37f7d 65 20 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65  e ..** column de
37f7e 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73 20 77  finition. This w
37f7f 61 73 20 65 69 74 68 65 72 20 73 75 70 70 6c 69  as either suppli
37f80 65 64 20 62 79 20 74 68 65 20 75 73 65 72 20 77  ed by the user w
37f81 68 65 6e 20 74 68 65 20 74 61 62 6c 65 0d 0a 2a  hen the table..*
37f82 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c 20 6f  * was created, o
37f83 72 20 61 64 64 65 64 20 6c 61 74 65 72 20 74 6f  r added later to
37f84 20 74 68 65 20 74 61 62 6c 65 20 64 65 66 69 6e   the table defin
37f85 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54 45  ition by an ALTE
37f86 52 20 54 41 42 4c 45 0d 0a 2a 2a 20 63 6f 6d 6d  R TABLE..** comm
37f87 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74 74  and. If the latt
37f88 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  er, then the row
37f89 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20  -records in the 
37f8a 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20 64  table btree on d
37f8b 69 73 6b 0d 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20  isk..** may not 
37f8c 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20  contain a value 
37f8d 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
37f8e 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  nd the default v
37f8f 61 6c 75 65 2c 20 74 61 6b 65 6e 0d 0a 2a 2a 20  alue, taken..** 
37f90 66 72 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61  from the P4 para
37f91 6d 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f  meter of the OP_
37f92 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69  Column instructi
37f93 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20  on, is returned 
37f94 69 6e 73 74 65 61 64 2e 0d 0a 2a 2a 20 49 66 20  instead...** If 
37f95 74 68 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e  the former, then
37f96 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73   all row-records
37f97 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20   are guaranteed 
37f98 74 6f 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c  to include a val
37f99 75 65 0d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63  ue..** for the c
37f9a 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 50 34  olumn and the P4
37f9b 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 72 65   value is not re
37f9c 71 75 69 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  quired...**..** 
37f9d 43 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  Column definitio
37f9e 6e 73 20 63 72 65 61 74 65 64 20 62 79 20 61 6e  ns created by an
37f9f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d   ALTER TABLE com
37fa0 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61  mand may only ha
37fa1 76 65 20 0d 0a 2a 2a 20 6c 69 74 65 72 61 6c 20  ve ..** literal 
37fa2 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 73  default values s
37fa3 70 65 63 69 66 69 65 64 3a 20 61 20 6e 75 6d 62  pecified: a numb
37fa4 65 72 2c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74  er, null or a st
37fa5 72 69 6e 67 2e 20 28 49 66 20 61 20 6d 6f 72 65  ring. (If a more
37fa6 0d 0a 2a 2a 20 63 6f 6d 70 6c 69 63 61 74 65 64  ..** complicated
37fa7 20 64 65 66 61 75 6c 74 20 65 78 70 72 65 73 73   default express
37fa8 69 6f 6e 20 76 61 6c 75 65 20 77 61 73 20 70 72  ion value was pr
37fa9 6f 76 69 64 65 64 2c 20 69 74 20 69 73 20 65 76  ovided, it is ev
37faa 61 6c 75 61 74 65 64 20 0d 0a 2a 2a 20 77 68 65  aluated ..** whe
37fab 6e 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c  n the ALTER TABL
37fac 45 20 69 73 20 65 78 65 63 75 74 65 64 20 61 6e  E is executed an
37fad 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 69 74  d one of the lit
37fae 65 72 61 6c 20 76 61 6c 75 65 73 20 77 72 69 74  eral values writ
37faf 74 65 6e 0d 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  ten..** into the
37fb0 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
37fb1 61 62 6c 65 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54  able.)..**..** T
37fb2 68 65 72 65 66 6f 72 65 2c 20 74 68 65 20 50 34  herefore, the P4
37fb3 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e   parameter is on
37fb4 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20 74  ly required if t
37fb5 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
37fb6 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 63 6f 6c   for..** the col
37fb7 75 6d 6e 20 69 73 20 61 20 6c 69 74 65 72 61 6c  umn is a literal
37fb8 20 6e 75 6d 62 65 72 2c 20 73 74 72 69 6e 67 20   number, string 
37fb9 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20 73 71 6c  or null. The sql
37fba 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
37fbb 72 28 29 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  r()..** function
37fbc 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 74   is capable of t
37fbd 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 73  ransforming thes
37fbe 65 20 74 79 70 65 73 20 6f 66 20 65 78 70 72 65  e types of expre
37fbf 73 73 69 6f 6e 73 20 69 6e 74 6f 0d 0a 2a 2a 20  ssions into..** 
37fc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
37fc1 6a 65 63 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  jects...**..** I
37fc2 66 20 70 61 72 61 6d 65 74 65 72 20 69 52 65 67  f parameter iReg
37fc3 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   is not negative
37fc4 2c 20 63 6f 64 65 20 61 6e 20 4f 50 5f 52 65 61  , code an OP_Rea
37fc5 6c 41 66 66 69 6e 69 74 79 20 69 6e 73 74 72 75  lAffinity instru
37fc6 63 74 69 6f 6e 0d 0a 2a 2a 20 6f 6e 20 72 65 67  ction..** on reg
37fc7 69 73 74 65 72 20 69 52 65 67 2e 20 54 68 69 73  ister iReg. This
37fc8 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 61 6e   is used when an
37fc9 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65   equivalent inte
37fca 67 65 72 20 76 61 6c 75 65 20 69 73 20 0d 0a 2a  ger value is ..*
37fcb 2a 20 73 74 6f 72 65 64 20 69 6e 20 70 6c 61 63  * stored in plac
37fcc 65 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 66  e of an 8-byte f
37fcd 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
37fce 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  lue in order to 
37fcf 73 61 76 65 20 0d 0a 2a 2a 20 73 70 61 63 65 2e  save ..** space.
37fd0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
37fd1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37fd2 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56  3ColumnDefault(V
37fd3 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70  dbe *v, Table *p
37fd4 54 61 62 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20  Tab, int i, int 
37fd5 69 52 65 67 29 7b 0d 0a 20 20 61 73 73 65 72 74  iReg){..  assert
37fd6 28 20 70 54 61 62 21 3d 30 20 29 3b 0d 0a 20 20  ( pTab!=0 );..  
37fd7 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65  if( !pTab->pSele
37fd8 63 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ct ){..    sqlit
37fd9 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
37fda 3b 0d 0a 20 20 20 20 75 38 20 65 6e 63 20 3d 20  ;..    u8 enc = 
37fdb 45 4e 43 28 73 71 6c 69 74 65 33 56 64 62 65 44  ENC(sqlite3VdbeD
37fdc 62 28 76 29 29 3b 0d 0a 20 20 20 20 43 6f 6c 75  b(v));..    Colu
37fdd 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62  mn *pCol = &pTab
37fde 2d 3e 61 43 6f 6c 5b 69 5d 3b 0d 0a 20 20 20 20  ->aCol[i];..    
37fdf 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
37fe0 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a  "%s.%s", pTab->z
37fe1 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  Name, pCol->zNam
37fe2 65 29 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  e));..    assert
37fe3 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ( i<pTab->nCol )
37fe4 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  ;..    sqlite3Va
37fe5 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69  lueFromExpr(sqli
37fe6 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 70 43  te3VdbeDb(v), pC
37fe7 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20  ol->pDflt, enc, 
37fe8 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
37fe9 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
37fea 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56 61 6c  >affinity, &pVal
37feb 75 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 56  ue);..    if( pV
37fec 61 6c 75 65 20 29 7b 0d 0a 20 20 20 20 20 20 73  alue ){..      s
37fed 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
37fee 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
37fef 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20   char *)pValue, 
37ff0 50 34 5f 4d 45 4d 29 3b 0d 0a 20 20 20 20 7d 0d  P4_MEM);..    }.
37ff1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
37ff2 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
37ff3 49 4e 54 0d 0a 20 20 20 20 69 66 28 20 69 52 65  INT..    if( iRe
37ff4 67 3e 3d 30 20 26 26 20 70 54 61 62 2d 3e 61 43  g>=0 && pTab->aC
37ff5 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3d 3d  ol[i].affinity==
37ff6 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
37ff7 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
37ff8 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
37ff9 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20  P_RealAffinity, 
37ffa 69 52 65 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23  iReg);..    }..#
37ffb 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  endif..  }..}...
37ffc 0a 2f 2a 0d 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ./*..** Process 
37ffd 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
37ffe 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 55  ent...**..**   U
37fff 50 44 41 54 45 20 4f 52 20 49 47 4e 4f 52 45 20  PDATE OR IGNORE 
38000 74 61 62 6c 65 5f 77 78 79 7a 20 53 45 54 20 61  table_wxyz SET a
38001 3d 62 2c 20 63 3d 64 20 57 48 45 52 45 20 65 3c  =b, c=d WHERE e<
38002 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c 4c  5 AND f NOT NULL
38003 3b 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5c  ;..**          \
38004 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f  _______/ \______
38005 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f  __/     \______/
38006 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
38007 5f 5f 5f 5f 5f 5f 5f 5f 2f 0d 0a 2a 20 20 20 20  ________/..*    
38008 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20          onError 
38009 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20    pTabList      
3800a 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  pChanges        
3800b 20 20 20 20 20 70 57 68 65 72 65 0d 0a 2a 2f 0d       pWhere..*/.
3800c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3800d 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61  void sqlite3Upda
3800e 74 65 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  te(..  Parse *pP
3800f 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
38010 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
38011 65 78 74 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73  ext */..  SrcLis
38012 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
38013 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
38014 20 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c 64   which we should
38015 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20 2a   change things *
38016 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  /..  ExprList *p
38017 43 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54  Changes,    /* T
38018 68 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e  hings to be chan
38019 67 65 64 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  ged */..  Expr *
3801a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
3801b 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
3801c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
3801d 6c 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6e 45  ll */..  int onE
3801e 72 72 6f 72 20 20 20 20 20 20 20 20 20 20 20 20  rror            
3801f 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65  /* How to handle
38020 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f   constraint erro
38021 72 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  rs */..){..  int
38022 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
38023 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
38024 74 65 72 73 20 2a 2f 0d 0a 20 20 54 61 62 6c 65  ters */..  Table
38025 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
38026 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
38027 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0d  o be updated */.
38028 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b  .  int addr = 0;
38029 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
3802a 45 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 64  E instruction ad
3802b 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 74 61  dress of the sta
3802c 72 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a  rt of the loop *
3802d 2f 0d 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  /..  WhereInfo *
3802e 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49  pWInfo;     /* I
3802f 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
38030 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
38031 65 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76 3b  e */..  Vdbe *v;
38032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38033 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61  * The virtual da
38034 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f  tabase engine */
38035 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ..  Index *pIdx;
38036 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
38037 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69  r looping over i
38038 6e 64 69 63 65 73 20 2a 2f 0d 0a 20 20 69 6e 74  ndices */..  int
38039 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20 20 20   nIdx;          
3803a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3803b 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 6e 65   indices that ne
3803c 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f 0d 0a  ed updating */..
3803d 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
3803e 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
3803f 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
38040 66 20 70 54 61 62 20 2a 2f 0d 0a 20 20 73 71 6c  f pTab */..  sql
38041 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
38042 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
38043 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
38044 0d 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  ..  int *aRegIdx
38045 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e   = 0;      /* On
38046 65 20 72 65 67 69 73 74 65 72 20 61 73 73 69 67  e register assig
38047 6e 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65  ned to each inde
38048 78 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  x to be updated 
38049 2a 2f 0d 0a 20 20 69 6e 74 20 2a 61 58 52 65 66  */..  int *aXRef
3804a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
3804b 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68 65 20  aXRef[i] is the 
3804c 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e 67 65  index in pChange
3804d 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0d 0a 20  s->a[] of the.. 
3804e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3804f 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78          ** an ex
38050 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68 65  pression for the
38051 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   i-th column of 
38052 74 68 65 20 74 61 62 6c 65 2e 0d 0a 20 20 20 20  the table...    
38053 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38054 20 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d       ** aXRef[i]
38055 3d 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68  ==-1 if the i-th
38056 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63   column is not c
38057 68 61 6e 67 65 64 2e 20 2a 2f 0d 0a 20 20 69 6e  hanged. */..  in
38058 74 20 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20  t chngRowid;    
38059 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3805a 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
3805b 72 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67  r is being chang
3805c 65 64 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  ed */..  Expr *p
3805d 52 6f 77 69 64 45 78 70 72 20 3d 20 30 3b 20 20  RowidExpr = 0;  
3805e 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 64 65  /* Expression de
3805f 66 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 72  fining the new r
38060 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0d  ecord number */.
38061 0a 20 20 69 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d  .  int openAll =
38062 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75   0;       /* Tru
38063 65 20 69 66 20 61 6c 6c 20 69 6e 64 69 63 65 73  e if all indices
38064 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e   need to be open
38065 65 64 20 2a 2f 0d 0a 20 20 41 75 74 68 43 6f 6e  ed */..  AuthCon
38066 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20  text sContext;  
38067 2f 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 61  /* The authoriza
38068 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0d  tion context */.
38069 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73  .  NameContext s
3806a 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  NC;       /* The
3806b 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f   name-context to
3806c 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73   resolve express
3806d 69 6f 6e 73 20 69 6e 20 2a 2f 0d 0a 20 20 69 6e  ions in */..  in
3806e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
3806f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
38070 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
38071 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  table being upda
38072 74 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6b  ted */..  int ok
38073 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20  OnePass;        
38074 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f 6e 65   /* True for one
38075 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
38076 77 69 74 68 6f 75 74 20 74 68 65 20 46 49 46 4f  without the FIFO
38077 20 2a 2f 0d 0a 20 20 69 6e 74 20 68 61 73 46 4b   */..  int hasFK
38078 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
38079 20 54 72 75 65 20 69 66 20 66 6f 72 65 69 67 6e   True if foreign
3807a 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20   key processing 
3807b 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0d 0a  is required */..
3807c 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3807d 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20  _OMIT_TRIGGER.. 
3807e 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20   int isView;    
3807f 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
38080 77 68 65 6e 20 75 70 64 61 74 69 6e 67 20 61 20  when updating a 
38081 76 69 65 77 20 28 49 4e 53 54 45 41 44 20 4f 46  view (INSTEAD OF
38082 20 74 72 69 67 67 65 72 29 20 2a 2f 0d 0a 20 20   trigger) */..  
38083 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
38084 72 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  r;     /* List o
38085 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54  f triggers on pT
38086 61 62 2c 20 69 66 20 72 65 71 75 69 72 65 64 20  ab, if required 
38087 2a 2f 0d 0a 20 20 69 6e 74 20 74 6d 61 73 6b 3b  */..  int tmask;
38088 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38089 4d 61 73 6b 20 6f 66 20 54 52 49 47 47 45 52 5f  Mask of TRIGGER_
3808a 42 45 46 4f 52 45 7c 54 52 49 47 47 45 52 5f 41  BEFORE|TRIGGER_A
3808b 46 54 45 52 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d  FTER */..#endif.
3808c 0a 20 20 69 6e 74 20 6e 65 77 6d 61 73 6b 3b 20  .  int newmask; 
3808d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
3808e 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f 6c 75 6d  k of NEW.* colum
3808f 6e 73 20 61 63 63 65 73 73 65 64 20 62 79 20 42  ns accessed by B
38090 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 2a  EFORE triggers *
38091 2f 0d 0a 0d 0a 20 20 2f 2a 20 52 65 67 69 73 74  /....  /* Regist
38092 65 72 20 41 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a  er Allocations *
38093 2f 0d 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43  /..  int regRowC
38094 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41  ount = 0;   /* A
38095 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 63   count of rows c
38096 68 61 6e 67 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  hanged */..  int
38097 20 72 65 67 4f 6c 64 52 6f 77 69 64 3b 20 20 20   regOldRowid;   
38098 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c 64 20 72      /* The old r
38099 6f 77 69 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  owid */..  int r
3809a 65 67 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20  egNewRowid;     
3809b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77    /* The new row
3809c 69 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 65 67  id */..  int reg
3809d 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  New;            
3809e 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
3809f 65 20 4e 45 57 2e 2a 20 74 61 62 6c 65 20 69 6e  e NEW.* table in
380a0 20 74 72 69 67 67 65 72 73 20 2a 2f 0d 0a 20 20   triggers */..  
380a1 69 6e 74 20 72 65 67 4f 6c 64 20 3d 20 30 3b 20  int regOld = 0; 
380a2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e         /* Conten
380a3 74 20 6f 66 20 4f 4c 44 2e 2a 20 74 61 62 6c 65  t of OLD.* table
380a4 20 69 6e 20 74 72 69 67 67 65 72 73 20 2a 2f 0d   in triggers */.
380a5 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74  .  int regRowSet
380a6 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77   = 0;     /* Row
380a7 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62  set of rows to b
380a8 65 20 75 70 64 61 74 65 64 20 2a 2f 0d 0a 0d 0a  e updated */....
380a9 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65    memset(&sConte
380aa 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43  xt, 0, sizeof(sC
380ab 6f 6e 74 65 78 74 29 29 3b 0d 0a 20 20 64 62 20  ontext));..  db 
380ac 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20  = pParse->db;.. 
380ad 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
380ae 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
380af 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 67 6f  ailed ){..    go
380b0 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
380b1 70 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  p;..  }..  asser
380b2 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  t( pTabList->nSr
380b3 63 3d 3d 31 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  c==1 );....  /* 
380b4 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
380b5 20 77 68 69 63 68 20 77 65 20 77 61 6e 74 20 74   which we want t
380b6 6f 20 75 70 64 61 74 65 2e 20 0d 0a 20 20 2a 2f  o update. ..  */
380b7 0d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ..  pTab = sqlit
380b8 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
380b9 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
380ba 29 3b 0d 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  );..  if( pTab==
380bb 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
380bc 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 69 44 62 20  cleanup;..  iDb 
380bd 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
380be 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
380bf 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
380c0 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 46 69 67 75 72  );....  /* Figur
380c1 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
380c2 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
380c3 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
380c4 65 69 6e 67 0d 0a 20 20 2a 2a 20 75 70 64 61 74  eing..  ** updat
380c5 65 64 20 69 73 20 61 20 76 69 65 77 2e 0d 0a 20  ed is a view... 
380c6 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
380c7 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
380c8 0d 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73  ..  pTrigger = s
380c9 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
380ca 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
380cb 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
380cc 61 6e 67 65 73 2c 20 26 74 6d 61 73 6b 29 3b 0d  anges, &tmask);.
380cd 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62  .  isView = pTab
380ce 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0d 0a 20  ->pSelect!=0;.. 
380cf 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
380d0 72 20 7c 7c 20 74 6d 61 73 6b 3d 3d 30 20 29 3b  r || tmask==0 );
380d1 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66 69 6e  ..#else..# defin
380d2 65 20 70 54 72 69 67 67 65 72 20 30 0d 0a 23 20  e pTrigger 0..# 
380d3 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0d  define isView 0.
380d4 0a 23 20 64 65 66 69 6e 65 20 74 6d 61 73 6b 20  .# define tmask 
380d5 30 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 64 65  0..#endif..#ifde
380d6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
380d7 45 57 0d 0a 23 20 75 6e 64 65 66 20 69 73 56 69  EW..# undef isVi
380d8 65 77 0d 0a 23 20 64 65 66 69 6e 65 20 69 73 56  ew..# define isV
380d9 69 65 77 20 30 0d 0a 23 65 6e 64 69 66 0d 0a 0d  iew 0..#endif...
380da 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69  .  if( sqlite3Vi
380db 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
380dc 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29  (pParse, pTab) )
380dd 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  {..    goto upda
380de 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  te_cleanup;..  }
380df 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  ..  if( sqlite3I
380e0 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65  sReadOnly(pParse
380e1 2c 20 70 54 61 62 2c 20 74 6d 61 73 6b 29 20 29  , pTab, tmask) )
380e2 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  {..    goto upda
380e3 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d  te_cleanup;..  }
380e4 0d 0a 20 20 61 58 52 65 66 20 3d 20 73 71 6c 69  ..  aXRef = sqli
380e5 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
380e6 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2a  b, sizeof(int) *
380e7 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0d 0a   pTab->nCol );..
380e8 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30 20 29    if( aXRef==0 )
380e9 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
380ea 61 6e 75 70 3b 0d 0a 20 20 66 6f 72 28 69 3d 30  anup;..  for(i=0
380eb 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
380ec 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d 20  i++) aXRef[i] = 
380ed 2d 31 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f  -1;....  /* Allo
380ee 63 61 74 65 20 61 20 63 75 72 73 6f 72 73 20 66  cate a cursors f
380ef 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
380f0 62 61 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66  base table and f
380f1 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0d  or all indices..
380f2 0a 20 20 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  .  ** The index 
380f3 63 75 72 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f  cursors might no
380f4 74 20 62 65 20 75 73 65 64 2c 20 62 75 74 20 69  t be used, but i
380f5 66 20 74 68 65 79 20 61 72 65 20 75 73 65 64 20  f they are used 
380f6 74 68 65 79 0d 0a 20 20 2a 2a 20 6e 65 65 64 20  they..  ** need 
380f7 74 6f 20 6f 63 63 75 72 20 72 69 67 68 74 20 61  to occur right a
380f8 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
380f9 65 20 63 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f  e cursor.  So go
380fa 20 61 68 65 61 64 20 61 6e 64 0d 0a 20 20 2a 2a   ahead and..  **
380fb 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68   allocate enough
380fc 20 73 70 61 63 65 2c 20 6a 75 73 74 20 69 6e 20   space, just in 
380fd 63 61 73 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70  case...  */..  p
380fe 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43  TabList->a[0].iC
380ff 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d 20 70  ursor = iCur = p
38100 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0d 0a  Parse->nTab++;..
38101 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
38102 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
38103 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
38104 7b 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  {..    pParse->n
38105 54 61 62 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Tab++;..  }.... 
38106 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
38107 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  he name-context 
38108 2a 2f 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e  */..  memset(&sN
38109 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
3810a 29 29 3b 0d 0a 20 20 73 4e 43 2e 70 50 61 72 73  ));..  sNC.pPars
3810b 65 20 3d 20 70 50 61 72 73 65 3b 0d 0a 20 20 73  e = pParse;..  s
3810c 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
3810d 61 62 4c 69 73 74 3b 0d 0a 0d 0a 20 20 2f 2a 20  abList;....  /* 
3810e 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75  Resolve the colu
3810f 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  mn names in all 
38110 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
38111 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 6f 66 20  of the..  ** of 
38112 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65  the UPDATE state
38113 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64  ment.  Also find
38114 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65   the column inde
38115 78 0d 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63 68  x..  ** for each
38116 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70   column to be up
38117 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 43 68  dated in the pCh
38118 61 6e 67 65 73 20 61 72 72 61 79 2e 20 20 46 6f  anges array.  Fo
38119 72 20 65 61 63 68 0d 0a 20 20 2a 2a 20 63 6f 6c  r each..  ** col
3811a 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65  umn to be update
3811b 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  d, make sure we 
3811c 68 61 76 65 20 61 75 74 68 6f 72 69 7a 61 74 69  have authorizati
3811d 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0d 0a 20 20  on to change..  
3811e 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0d  ** that column..
3811f 0a 20 20 2a 2f 0d 0a 20 20 63 68 6e 67 52 6f 77  .  */..  chngRow
38120 69 64 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28 69  id = 0;..  for(i
38121 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d 3e  =0; i<pChanges->
38122 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nExpr; i++){..  
38123 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
38124 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
38125 4e 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  NC, pChanges->a[
38126 69 5d 2e 70 45 78 70 72 29 20 29 7b 0d 0a 20 20  i].pExpr) ){..  
38127 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
38128 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 20 20 7d 0d  cleanup;..    }.
38129 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
3812a 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
3812b 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  {..      if( sql
3812c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
3812d 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
3812e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
3812f 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a 20 20  zName)==0 ){..  
38130 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61        if( j==pTa
38131 62 2d 3e 69 50 4b 65 79 20 29 7b 0d 0a 20 20 20  b->iPKey ){..   
38132 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64         chngRowid
38133 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 1;..         
38134 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43   pRowidExpr = pC
38135 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78  hanges->a[i].pEx
38136 70 72 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  pr;..        }..
38137 20 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d          aXRef[j]
38138 20 3d 20 69 3b 0d 0a 20 20 20 20 20 20 20 20 62   = i;..        b
38139 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
3813a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 6a      }..    if( j
3813b 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0d  >=pTab->nCol ){.
3813c 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
3813d 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e 67  e3IsRowid(pChang
3813e 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20  es->a[i].zName) 
3813f 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 6e 67  ){..        chng
38140 52 6f 77 69 64 20 3d 20 31 3b 0d 0a 20 20 20 20  Rowid = 1;..    
38141 20 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d      pRowidExpr =
38142 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
38143 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 7d 65  pExpr;..      }e
38144 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
38145 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
38146 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63  arse, "no such c
38147 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61  olumn: %s", pCha
38148 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
38149 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72  );..        pPar
3814a 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20  se->checkSchema 
3814b 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f  = 1;..        go
3814c 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
3814d 70 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  p;..      }..   
3814e 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
3814f 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
38150 41 54 49 4f 4e 0d 0a 20 20 20 20 7b 0d 0a 20 20  ATION..    {..  
38151 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20      int rc;..   
38152 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
38153 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
38154 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20   SQLITE_UPDATE, 
38155 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0d 0a 20 20  pTab->zName,..  
38156 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38157 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61           pTab->a
38158 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62  Col[j].zName, db
38159 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
3815a 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  );..      if( rc
3815b 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
3815c 0d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75  ..        goto u
3815d 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a  pdate_cleanup;..
3815e 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
3815f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc==SQLITE_IGNOR
38160 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 58  E ){..        aX
38161 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0d 0a 20 20  Ref[j] = -1;..  
38162 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65      }..    }..#e
38163 6e 64 69 66 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 68  ndif..  }....  h
38164 61 73 46 4b 20 3d 20 73 71 6c 69 74 65 33 46 6b  asFK = sqlite3Fk
38165 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65 2c  Required(pParse,
38166 20 70 54 61 62 2c 20 61 58 52 65 66 2c 20 63 68   pTab, aXRef, ch
38167 6e 67 52 6f 77 69 64 29 3b 0d 0a 0d 0a 20 20 2f  ngRowid);....  /
38168 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
38169 79 20 66 6f 72 20 74 68 65 20 61 72 72 61 79 20  y for the array 
3816a 61 52 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72  aRegIdx[].  Ther
3816b 65 20 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69  e is one entry i
3816c 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 61 72 72 61  n the..  ** arra
3816d 79 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78  y for each index
3816e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
3816f 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64   table being upd
38170 61 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e 0d 0a  ated.  Fill in..
38171 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 77    ** the value w
38172 69 74 68 20 61 20 72 65 67 69 73 74 65 72 20 6e  ith a register n
38173 75 6d 62 65 72 20 66 6f 72 20 69 6e 64 69 63 65  umber for indice
38174 73 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65  s that are to be
38175 20 75 73 65 64 0d 0a 20 20 2a 2a 20 61 6e 64 20   used..  ** and 
38176 77 69 74 68 20 7a 65 72 6f 20 66 6f 72 20 75 6e  with zero for un
38177 75 73 65 64 20 69 6e 64 69 63 65 73 2e 0d 0a 20  used indices... 
38178 20 2a 2f 0d 0a 20 20 66 6f 72 28 6e 49 64 78 3d   */..  for(nIdx=
38179 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
3817a 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
3817b 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
3817c 64 78 2b 2b 29 7b 7d 0d 0a 20 20 69 66 28 20 6e  dx++){}..  if( n
3817d 49 64 78 3e 30 20 29 7b 0d 0a 20 20 20 20 61 52  Idx>0 ){..    aR
3817e 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 44  egIdx = sqlite3D
3817f 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
38180 69 7a 65 6f 66 28 49 6e 64 65 78 2a 29 20 2a 20  izeof(Index*) * 
38181 6e 49 64 78 20 29 3b 0d 0a 20 20 20 20 69 66 28  nIdx );..    if(
38182 20 61 52 65 67 49 64 78 3d 3d 30 20 29 20 67 6f   aRegIdx==0 ) go
38183 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
38184 70 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 6a  p;..  }..  for(j
38185 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
38186 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
38187 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a  x=pIdx->pNext, j
38188 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20 72 65  ++){..    int re
38189 67 3b 0d 0a 20 20 20 20 69 66 28 20 68 61 73 46  g;..    if( hasF
3818a 4b 20 7c 7c 20 63 68 6e 67 52 6f 77 69 64 20 29  K || chngRowid )
3818b 7b 0d 0a 20 20 20 20 20 20 72 65 67 20 3d 20 2b  {..      reg = +
3818c 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
3818d 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
3818e 20 20 72 65 67 20 3d 20 30 3b 0d 0a 20 20 20 20    reg = 0;..    
3818f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
38190 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
38191 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  {..        if( a
38192 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  XRef[pIdx->aiCol
38193 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0d 0a 20  umn[i]]>=0 ){.. 
38194 20 20 20 20 20 20 20 20 20 72 65 67 20 3d 20 2b           reg = +
38195 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a  +pParse->nMem;..
38196 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
38197 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
38198 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20     }..    }..   
38199 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65   aRegIdx[j] = re
3819a 67 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  g;..  }....  /* 
3819b 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
3819c 20 63 6f 64 65 2e 20 2a 2f 0d 0a 20 20 76 20 3d   code. */..  v =
3819d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
3819e 70 50 61 72 73 65 29 3b 0d 0a 20 20 69 66 28 20  pParse);..  if( 
3819f 76 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  v==0 ) goto upda
381a0 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20 69  te_cleanup;..  i
381a1 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
381a2 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64  d==0 ) sqlite3Vd
381a3 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76  beCountChanges(v
381a4 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 42 65 67  );..  sqlite3Beg
381a5 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
381a6 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29  (pParse, 1, iDb)
381a7 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;....#ifndef SQL
381a8 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
381a9 54 41 42 4c 45 0d 0a 20 20 2f 2a 20 56 69 72 74  TABLE..  /* Virt
381aa 75 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74 20  ual tables must 
381ab 62 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72  be handled separ
381ac 61 74 65 6c 79 20 2a 2f 0d 0a 20 20 69 66 28 20  ately */..  if( 
381ad 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
381ae 29 7b 0d 0a 20 20 20 20 75 70 64 61 74 65 56 69  ){..    updateVi
381af 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61 72 73  rtualTable(pPars
381b0 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 54 61  e, pTabList, pTa
381b1 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 52 6f  b, pChanges, pRo
381b2 77 69 64 45 78 70 72 2c 20 61 58 52 65 66 2c 0d  widExpr, aXRef,.
381b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
381b4 20 20 20 20 20 20 20 20 70 57 68 65 72 65 2c 20          pWhere, 
381b5 6f 6e 45 72 72 6f 72 29 3b 0d 0a 20 20 20 20 70  onError);..    p
381b6 57 68 65 72 65 20 3d 20 30 3b 0d 0a 20 20 20 20  Where = 0;..    
381b7 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 0d 0a 20  pTabList = 0;.. 
381b8 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
381b9 6c 65 61 6e 75 70 3b 0d 0a 20 20 7d 0d 0a 23 65  leanup;..  }..#e
381ba 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c  ndif....  /* All
381bb 6f 63 61 74 65 20 72 65 71 75 69 72 65 64 20 72  ocate required r
381bc 65 67 69 73 74 65 72 73 2e 20 2a 2f 0d 0a 20 20  egisters. */..  
381bd 72 65 67 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50  regRowSet = ++pP
381be 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 72  arse->nMem;..  r
381bf 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 72 65 67  egOldRowid = reg
381c0 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61  NewRowid = ++pPa
381c1 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 69 66  rse->nMem;..  if
381c2 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  ( pTrigger || ha
381c3 73 46 4b 20 29 7b 0d 0a 20 20 20 20 72 65 67 4f  sFK ){..    regO
381c4 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ld = pParse->nMe
381c5 6d 20 2b 20 31 3b 0d 0a 20 20 20 20 70 50 61 72  m + 1;..    pPar
381c6 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
381c7 2d 3e 6e 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 20 20  ->nCol;..  }..  
381c8 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c  if( chngRowid ||
381c9 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61 73   pTrigger || has
381ca 46 4b 20 29 7b 0d 0a 20 20 20 20 72 65 67 4e 65  FK ){..    regNe
381cb 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  wRowid = ++pPars
381cc 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20  e->nMem;..  }.. 
381cd 20 72 65 67 4e 65 77 20 3d 20 70 50 61 72 73 65   regNew = pParse
381ce 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0d 0a 20 20 70  ->nMem + 1;..  p
381cf 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
381d0 54 61 62 2d 3e 6e 43 6f 6c 3b 0d 0a 0d 0a 20 20  Tab->nCol;....  
381d1 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76 69 65  /* Start the vie
381d2 77 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0d 0a 20  w context. */.. 
381d3 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0d 0a   if( isView ){..
381d4 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43      sqlite3AuthC
381d5 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73  ontextPush(pPars
381d6 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54  e, &sContext, pT
381d7 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 7d  ab->zName);..  }
381d8 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 77 65 20 61  ....  /* If we a
381d9 72 65 20 74 72 79 69 6e 67 20 74 6f 20 75 70 64  re trying to upd
381da 61 74 65 20 61 20 76 69 65 77 2c 20 72 65 61 6c  ate a view, real
381db 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69 6e  ize that view in
381dc 74 6f 0d 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d  to..  ** a ephem
381dd 65 72 61 6c 20 74 61 62 6c 65 2e 0d 0a 20 20 2a  eral table...  *
381de 2f 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
381df 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
381e0 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
381e1 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
381e2 52 29 0d 0a 20 20 69 66 28 20 69 73 56 69 65 77  R)..  if( isView
381e3 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
381e4 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28  MaterializeView(
381e5 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 57  pParse, pTab, pW
381e6 68 65 72 65 2c 20 69 43 75 72 29 3b 0d 0a 20 20  here, iCur);..  
381e7 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f  }..#endif....  /
381e8 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f  * Resolve the co
381e9 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c  lumn names in al
381ea 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  l the expression
381eb 73 20 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 57  s in the..  ** W
381ec 48 45 52 45 20 63 6c 61 75 73 65 2e 0d 0a 20 20  HERE clause...  
381ed 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
381ee 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
381ef 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20  s(&sNC, pWhere) 
381f0 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 75 70 64  ){..    goto upd
381f1 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a 20 20  ate_cleanup;..  
381f2 7d 0d 0a 0d 0a 20 20 2f 2a 20 42 65 67 69 6e 20  }....  /* Begin 
381f3 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
381f4 6e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  n..  */..  sqlit
381f5 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
381f6 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
381f7 6f 77 53 65 74 2c 20 72 65 67 4f 6c 64 52 6f 77  owSet, regOldRow
381f8 69 64 29 3b 0d 0a 20 20 70 57 49 6e 66 6f 20 3d  id);..  pWInfo =
381f9 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
381fa 69 6e 28 0d 0a 20 20 20 20 20 20 70 50 61 72 73  in(..      pPars
381fb 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68  e, pTabList, pWh
381fc 65 72 65 2c 20 30 2c 20 30 2c 20 57 48 45 52 45  ere, 0, 0, WHERE
381fd 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44  _ONEPASS_DESIRED
381fe 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20 70 57  ..  );..  if( pW
381ff 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75  Info==0 ) goto u
38200 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0d 0a  pdate_cleanup;..
38201 20 20 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 70 57    okOnePass = pW
38202 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b  Info->okOnePass;
38203 0d 0a 0d 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65  ....  /* Remembe
38204 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 65  r the rowid of e
38205 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65 20  very item to be 
38206 75 70 64 61 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a  updated...  */..
38207 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
38208 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
38209 20 69 43 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77   iCur, regOldRow
3820a 69 64 29 3b 0d 0a 20 20 69 66 28 20 21 6f 6b 4f  id);..  if( !okO
3820b 6e 65 50 61 73 73 20 29 7b 0d 0a 20 20 20 20 73  nePass ){..    s
3820c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3820d 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  (v, OP_RowSetAdd
3820e 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 72 65 67  , regRowSet, reg
3820f 4f 6c 64 52 6f 77 69 64 29 3b 0d 0a 20 20 7d 0d  OldRowid);..  }.
38210 0a 0d 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20  ...  /* End the 
38211 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f  database scan lo
38212 6f 70 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  op...  */..  sql
38213 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
38214 6e 66 6f 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e  nfo);....  /* In
38215 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75  itialize the cou
38216 6e 74 20 6f 66 20 75 70 64 61 74 65 64 20 72 6f  nt of updated ro
38217 77 73 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  ws..  */..  if( 
38218 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
38219 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26  ITE_CountRows) &
3821a 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67  & !pParse->pTrig
3821b 67 65 72 54 61 62 20 29 7b 0d 0a 20 20 20 20 72  gerTab ){..    r
3821c 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70  egRowCount = ++p
3821d 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20  Parse->nMem;..  
3821e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3821f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
38220 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e  r, 0, regRowCoun
38221 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  t);..  }....  if
38222 28 20 21 69 73 56 69 65 77 20 29 7b 0d 0a 20 20  ( !isView ){..  
38223 20 20 2f 2a 20 0d 0a 20 20 20 20 2a 2a 20 4f 70    /* ..    ** Op
38224 65 6e 20 65 76 65 72 79 20 69 6e 64 65 78 20 74  en every index t
38225 68 61 74 20 6e 65 65 64 73 20 75 70 64 61 74 69  hat needs updati
38226 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69  ng.  Note that i
38227 66 20 61 6e 79 0d 0a 20 20 20 20 2a 2a 20 69 6e  f any..    ** in
38228 64 65 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74  dex could potent
38229 69 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52  ially invoke a R
3822a 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 20  EPLACE conflict 
3822b 72 65 73 6f 6c 75 74 69 6f 6e 20 0d 0a 20 20 20  resolution ..   
3822c 20 2a 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e   ** action, then
3822d 20 77 65 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e   we need to open
3822e 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 62 65 63   all indices bec
3822f 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65  ause we might ne
38230 65 64 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65  ed..    ** to be
38231 20 64 65 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72   deleting some r
38232 65 63 6f 72 64 73 2e 0d 0a 20 20 20 20 2a 2f 0d  ecords...    */.
38233 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50  .    if( !okOneP
38234 61 73 73 20 29 20 73 71 6c 69 74 65 33 4f 70 65  ass ) sqlite3Ope
38235 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
38236 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  Cur, iDb, pTab, 
38237 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 20 0d  OP_OpenWrite); .
38238 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  .    if( onError
38239 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0d  ==OE_Replace ){.
3823a 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d  .      openAll =
3823b 20 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   1;..    }else{.
3823c 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d  .      openAll =
3823d 20 30 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 70   0;..      for(p
3823e 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
3823f 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
38240 78 2d 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20  x->pNext){..    
38241 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e      if( pIdx->on
38242 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
38243 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  e ){..          
38244 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0d 0a 20 20  openAll = 1;..  
38245 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
38246 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
38247 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 66   }..    }..    f
38248 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(i=0, pIdx=pTa
38249 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
3824a 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
3824b 74 2c 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  t, i++){..      
3824c 61 73 73 65 72 74 28 20 61 52 65 67 49 64 78 20  assert( aRegIdx 
3824d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6f 70  );..      if( op
3824e 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78  enAll || aRegIdx
3824f 5b 69 5d 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  [i]>0 ){..      
38250 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20    KeyInfo *pKey 
38251 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
38252 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
38253 64 78 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  dx);..        sq
38254 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
38255 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
38256 20 69 43 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d   iCur+i+1, pIdx-
38257 3e 74 6e 75 6d 2c 20 69 44 62 2c 0d 0a 20 20 20  >tnum, iDb,..   
38258 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38259 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
3825a 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
3825b 4f 46 46 29 3b 0d 0a 20 20 20 20 20 20 20 20 61  OFF);..        a
3825c 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
3825d 54 61 62 3e 69 43 75 72 2b 69 2b 31 20 29 3b 0d  Tab>iCur+i+1 );.
3825e 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
3825f 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 6f 70  .  }....  /* Top
38260 20 6f 66 20 74 68 65 20 75 70 64 61 74 65 20 6c   of the update l
38261 6f 6f 70 20 2a 2f 0d 0a 20 20 69 66 28 20 6f 6b  oop */..  if( ok
38262 4f 6e 65 50 61 73 73 20 29 7b 0d 0a 20 20 20 20  OnePass ){..    
38263 69 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33  int a1 = sqlite3
38264 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
38265 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64  _NotNull, regOld
38266 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 61 64 64  Rowid);..    add
38267 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
38268 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
38269 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
3826a 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
3826b 31 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  1);..  }else{.. 
3826c 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
3826d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
3826e 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 72 65  P_RowSetRead, re
3826f 67 52 6f 77 53 65 74 2c 20 30 2c 20 72 65 67 4f  gRowSet, 0, regO
38270 6c 64 52 6f 77 69 64 29 3b 0d 0a 20 20 7d 0d 0a  ldRowid);..  }..
38271 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 63 75 72 73  ..  /* Make curs
38272 6f 72 20 69 43 75 72 20 70 6f 69 6e 74 20 74 6f  or iCur point to
38273 20 74 68 65 20 72 65 63 6f 72 64 20 74 68 61 74   the record that
38274 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 74 65   is being update
38275 64 2e 20 49 66 0d 0a 20 20 2a 2a 20 74 68 69 73  d. If..  ** this
38276 20 72 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74   record does not
38277 20 65 78 69 73 74 20 66 6f 72 20 73 6f 6d 65 20   exist for some 
38278 72 65 61 73 6f 6e 20 28 64 65 6c 65 74 65 64 20  reason (deleted 
38279 62 79 20 61 20 74 72 69 67 67 65 72 2c 0d 0a 20  by a trigger,.. 
3827a 20 2a 2a 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c   ** for example,
3827b 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68   then jump to th
3827c 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
3827d 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 6c   of the RowSet l
3827e 6f 6f 70 2e 20 20 2a 2f 0d 0a 20 20 73 71 6c 69  oop.  */..  sqli
3827f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
38280 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
38281 43 75 72 2c 20 61 64 64 72 2c 20 72 65 67 4f 6c  Cur, addr, regOl
38282 64 52 6f 77 69 64 29 3b 0d 0a 0d 0a 20 20 2f 2a  dRowid);....  /*
38283 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e   If the record n
38284 75 6d 62 65 72 20 77 69 6c 6c 20 63 68 61 6e 67  umber will chang
38285 65 2c 20 73 65 74 20 72 65 67 69 73 74 65 72 20  e, set register 
38286 72 65 67 4e 65 77 52 6f 77 69 64 20 74 6f 0d 0a  regNewRowid to..
38287 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 74 68 65    ** contain the
38288 20 6e 65 77 20 76 61 6c 75 65 2e 20 49 66 20 74   new value. If t
38289 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
3828a 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 6d 6f   is not being mo
3828b 64 69 66 69 65 64 2c 0d 0a 20 20 2a 2a 20 74 68  dified,..  ** th
3828c 65 6e 20 72 65 67 4e 65 77 52 6f 77 69 64 20 69  en regNewRowid i
3828d 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73  s the same regis
3828e 74 65 72 20 61 73 20 72 65 67 4f 6c 64 52 6f 77  ter as regOldRow
3828f 69 64 2c 20 77 68 69 63 68 20 69 73 0d 0a 20 20  id, which is..  
38290 2a 2a 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c  ** already popul
38291 61 74 65 64 2e 20 20 2a 2f 0d 0a 20 20 61 73 73  ated.  */..  ass
38292 65 72 74 28 20 63 68 6e 67 52 6f 77 69 64 20 7c  ert( chngRowid |
38293 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  | pTrigger || ha
38294 73 46 4b 20 7c 7c 20 72 65 67 4f 6c 64 52 6f 77  sFK || regOldRow
38295 69 64 3d 3d 72 65 67 4e 65 77 52 6f 77 69 64 20  id==regNewRowid 
38296 29 3b 0d 0a 20 20 69 66 28 20 63 68 6e 67 52 6f  );..  if( chngRo
38297 77 69 64 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  wid ){..    sqli
38298 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
38299 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20  se, pRowidExpr, 
3829a 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0d 0a 20  regNewRowid);.. 
3829b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3829c 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42  dOp1(v, OP_MustB
3829d 65 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77 69  eInt, regNewRowi
3829e 64 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  d);..  }....  /*
3829f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 74 72   If there are tr
382a0 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74  iggers on this t
382a1 61 62 6c 65 2c 20 70 6f 70 75 6c 61 74 65 20 61  able, populate a
382a2 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73  n array of regis
382a3 74 65 72 73 20 0d 0a 20 20 2a 2a 20 77 69 74 68  ters ..  ** with
382a4 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6f 6c   the required ol
382a5 64 2e 2a 20 63 6f 6c 75 6d 6e 20 64 61 74 61 2e  d.* column data.
382a6 20 20 2a 2f 0d 0a 20 20 69 66 28 20 68 61 73 46    */..  if( hasF
382a7 4b 20 7c 7c 20 70 54 72 69 67 67 65 72 20 29 7b  K || pTrigger ){
382a8 0d 0a 20 20 20 20 75 33 32 20 6f 6c 64 6d 61 73  ..    u32 oldmas
382a9 6b 20 3d 20 28 68 61 73 46 4b 20 3f 20 73 71 6c  k = (hasFK ? sql
382aa 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 70 50  ite3FkOldmask(pP
382ab 61 72 73 65 2c 20 70 54 61 62 29 20 3a 20 30 29  arse, pTab) : 0)
382ac 3b 0d 0a 20 20 20 20 6f 6c 64 6d 61 73 6b 20 7c  ;..    oldmask |
382ad 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
382ae 43 6f 6c 6d 61 73 6b 28 70 50 61 72 73 65 2c 20  Colmask(pParse, 
382af 0d 0a 20 20 20 20 20 20 20 20 70 54 72 69 67 67  ..        pTrigg
382b0 65 72 2c 20 70 43 68 61 6e 67 65 73 2c 20 30 2c  er, pChanges, 0,
382b1 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c   TRIGGER_BEFORE|
382b2 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
382b3 54 61 62 2c 20 6f 6e 45 72 72 6f 72 0d 0a 20 20  Tab, onError..  
382b4 20 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d    );..    for(i=
382b5 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
382b6 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66   i++){..      if
382b7 28 20 61 58 52 65 66 5b 69 5d 3c 30 20 7c 7c 20  ( aXRef[i]<0 || 
382b8 6f 6c 64 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66  oldmask==0xfffff
382b9 66 66 66 20 7c 7c 20 28 69 3c 33 32 20 26 26 20  fff || (i<32 && 
382ba 28 6f 6c 64 6d 61 73 6b 20 26 20 28 31 3c 3c 69  (oldmask & (1<<i
382bb 29 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ))) ){..        
382bc 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
382bd 65 74 43 6f 6c 75 6d 6e 4f 66 54 61 62 6c 65 28  etColumnOfTable(
382be 76 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69  v, pTab, iCur, i
382bf 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0d 0a 20 20  , regOld+i);..  
382c0 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
382c1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
382c2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
382c3 2c 20 30 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0d  , 0, regOld+i);.
382c4 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
382c5 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
382c6 69 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  id==0 ){..      
382c7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
382c8 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65  2(v, OP_Copy, re
382c9 67 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 4e 65  gOldRowid, regNe
382ca 77 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 7d 0d  wRowid);..    }.
382cb 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 50 6f 70  .  }....  /* Pop
382cc 75 6c 61 74 65 20 74 68 65 20 61 72 72 61 79 20  ulate the array 
382cd 6f 66 20 72 65 67 69 73 74 65 72 73 20 62 65 67  of registers beg
382ce 69 6e 6e 69 6e 67 20 61 74 20 72 65 67 4e 65 77  inning at regNew
382cf 20 77 69 74 68 20 74 68 65 20 6e 65 77 0d 0a 20   with the new.. 
382d0 20 2a 2a 20 72 6f 77 20 64 61 74 61 2e 20 54 68   ** row data. Th
382d1 69 73 20 61 72 72 61 79 20 69 73 20 75 73 65 64  is array is used
382d2 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 61   to check consta
382d3 69 6e 74 73 2c 20 63 72 65 61 74 65 20 74 68 65  ints, create the
382d4 20 6e 65 77 0d 0a 20 20 2a 2a 20 74 61 62 6c 65   new..  ** table
382d5 20 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72   and index recor
382d6 64 73 2c 20 61 6e 64 20 61 73 20 74 68 65 20 76  ds, and as the v
382d7 61 6c 75 65 73 20 66 6f 72 20 61 6e 79 20 6e 65  alues for any ne
382d8 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 0d 0a  w.* references..
382d9 20 20 2a 2a 20 6d 61 64 65 20 62 79 20 74 72 69    ** made by tri
382da 67 67 65 72 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  ggers...  **..  
382db 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
382dc 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 42 45 46 4f  one or more BEFO
382dd 52 45 20 74 72 69 67 67 65 72 73 2c 20 74 68 65  RE triggers, the
382de 6e 20 64 6f 20 6e 6f 74 20 70 6f 70 75 6c 61 74  n do not populat
382df 65 20 74 68 65 0d 0a 20 20 2a 2a 20 72 65 67 69  e the..  ** regi
382e0 73 74 65 72 73 20 61 73 73 6f 63 69 61 74 65 64  sters associated
382e1 20 77 69 74 68 20 63 6f 6c 75 6d 6e 73 20 74 68   with columns th
382e2 61 74 20 61 72 65 20 28 61 29 20 6e 6f 74 20 6d  at are (a) not m
382e3 6f 64 69 66 69 65 64 20 62 79 0d 0a 20 20 2a 2a  odified by..  **
382e4 20 74 68 69 73 20 55 50 44 41 54 45 20 73 74 61   this UPDATE sta
382e5 74 65 6d 65 6e 74 20 61 6e 64 20 28 62 29 20 6e  tement and (b) n
382e6 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 20 6e  ot accessed by n
382e7 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e  ew.* references.
382e8 20 54 68 65 0d 0a 20 20 2a 2a 20 76 61 6c 75 65   The..  ** value
382e9 73 20 66 6f 72 20 72 65 67 69 73 74 65 72 73 20  s for registers 
382ea 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79 20  not modified by 
382eb 74 68 65 20 55 50 44 41 54 45 20 6d 75 73 74 20  the UPDATE must 
382ec 62 65 20 72 65 6c 6f 61 64 65 64 20 66 72 6f 6d  be reloaded from
382ed 20 0d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61   ..  ** the data
382ee 62 61 73 65 20 61 66 74 65 72 20 74 68 65 20 42  base after the B
382ef 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 61  EFORE triggers a
382f0 72 65 20 66 69 72 65 64 20 61 6e 79 77 61 79 20  re fired anyway 
382f1 28 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20  (as the trigger 
382f2 0d 0a 20 20 2a 2a 20 6d 61 79 20 68 61 76 65 20  ..  ** may have 
382f3 6d 6f 64 69 66 69 65 64 20 74 68 65 6d 29 2e 20  modified them). 
382f4 53 6f 20 6e 6f 74 20 6c 6f 61 64 69 6e 67 20 74  So not loading t
382f5 68 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e 6f  hose that are no
382f6 74 20 67 6f 69 6e 67 20 74 6f 0d 0a 20 20 2a 2a  t going to..  **
382f7 20 62 65 20 75 73 65 64 20 65 6c 69 6d 69 6e 61   be used elimina
382f8 74 65 73 20 73 6f 6d 65 20 72 65 64 75 6e 64 61  tes some redunda
382f9 6e 74 20 6f 70 63 6f 64 65 73 2e 0d 0a 20 20 2a  nt opcodes...  *
382fa 2f 0d 0a 20 20 6e 65 77 6d 61 73 6b 20 3d 20 73  /..  newmask = s
382fb 71 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c  qlite3TriggerCol
382fc 6d 61 73 6b 28 0d 0a 20 20 20 20 20 20 70 50 61  mask(..      pPa
382fd 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70  rse, pTrigger, p
382fe 43 68 61 6e 67 65 73 2c 20 31 2c 20 54 52 49 47  Changes, 1, TRIG
382ff 47 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62  GER_BEFORE, pTab
38300 2c 20 6f 6e 45 72 72 6f 72 0d 0a 20 20 29 3b 0d  , onError..  );.
38301 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
38302 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp3(v, OP_Null,
38303 20 30 2c 20 72 65 67 4e 65 77 2c 20 72 65 67 4e   0, regNew, regN
38304 65 77 2b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 29  ew+pTab->nCol-1)
38305 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
38306 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
38307 7b 0d 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 54  {..    if( i==pT
38308 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0d 0a 20 20  ab->iPKey ){..  
38309 20 20 20 20 2f 2a 73 71 6c 69 74 65 33 56 64 62      /*sqlite3Vdb
3830a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
3830b 6c 6c 2c 20 30 2c 20 72 65 67 4e 65 77 2b 69 29  ll, 0, regNew+i)
3830c 3b 2a 2f 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  ;*/..    }else{.
3830d 0a 20 20 20 20 20 20 6a 20 3d 20 61 58 52 65 66  .      j = aXRef
3830e 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  [i];..      if( 
3830f 6a 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  j>=0 ){..       
38310 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
38311 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65  (pParse, pChange
38312 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72  s->a[j].pExpr, r
38313 65 67 4e 65 77 2b 69 29 3b 0d 0a 20 20 20 20 20  egNew+i);..     
38314 20 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 28 74   }else if( 0==(t
38315 6d 61 73 6b 26 54 52 49 47 47 45 52 5f 42 45 46  mask&TRIGGER_BEF
38316 4f 52 45 29 20 7c 7c 20 69 3e 33 31 20 7c 7c 20  ORE) || i>31 || 
38317 28 6e 65 77 6d 61 73 6b 26 28 31 3c 3c 69 29 29  (newmask&(1<<i))
38318 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20   ){..        /* 
38319 54 68 69 73 20 62 72 61 6e 63 68 20 6c 6f 61 64  This branch load
3831a 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  s the value of a
3831b 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 69 6c   column that wil
3831c 6c 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64  l not be changed
3831d 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e   ..        ** in
3831e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 54  to a register. T
3831f 68 69 73 20 69 73 20 64 6f 6e 65 20 69 66 20 74  his is done if t
38320 68 65 72 65 20 61 72 65 20 6e 6f 20 42 45 46 4f  here are no BEFO
38321 52 45 20 74 72 69 67 67 65 72 73 2c 20 6f 72 0d  RE triggers, or.
38322 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 74  .        ** if t
38323 68 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20  here are one or 
38324 6d 6f 72 65 20 42 45 46 4f 52 45 20 74 72 69 67  more BEFORE trig
38325 67 65 72 73 20 74 68 61 74 20 75 73 65 20 74 68  gers that use th
38326 69 73 20 76 61 6c 75 65 20 76 69 61 0d 0a 20 20  is value via..  
38327 20 20 20 20 20 20 2a 2a 20 61 20 6e 65 77 2e 2a        ** a new.*
38328 20 72 65 66 65 72 65 6e 63 65 20 69 6e 20 61 20   reference in a 
38329 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
3832a 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20  ..        */..  
3832b 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3832c 69 3d 3d 33 31 20 29 3b 0d 0a 20 20 20 20 20 20  i==31 );..      
3832d 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
3832e 32 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  2 );..        sq
3832f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
38330 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43  v, OP_Column, iC
38331 75 72 2c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29  ur, i, regNew+i)
38332 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
38333 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
38334 76 2c 20 70 54 61 62 2c 20 69 2c 20 72 65 67 4e  v, pTab, i, regN
38335 65 77 2b 69 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  ew+i);..      }.
38336 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
38337 20 2f 2a 20 46 69 72 65 20 61 6e 79 20 42 45 46   /* Fire any BEF
38338 4f 52 45 20 55 50 44 41 54 45 20 74 72 69 67 67  ORE UPDATE trigg
38339 65 72 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e  ers. This happen
3833a 73 20 62 65 66 6f 72 65 20 63 6f 6e 73 74 72 61  s before constra
3833b 69 6e 74 73 20 61 72 65 0d 0a 20 20 2a 2a 20 76  ints are..  ** v
3833c 65 72 69 66 69 65 64 2e 20 4f 6e 65 20 63 6f 75  erified. One cou
3833d 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 74 68  ld argue that th
3833e 69 73 20 69 73 20 77 72 6f 6e 67 2e 0d 0a 20 20  is is wrong...  
3833f 2a 2f 0d 0a 20 20 69 66 28 20 74 6d 61 73 6b 26  */..  if( tmask&
38340 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 29  TRIGGER_BEFORE )
38341 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
38342 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
38343 66 66 69 6e 69 74 79 2c 20 72 65 67 4e 65 77 2c  ffinity, regNew,
38344 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0d 0a 20   pTab->nCol);.. 
38345 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41     sqlite3TableA
38346 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54  ffinityStr(v, pT
38347 61 62 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ab);..    sqlite
38348 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
38349 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
3834a 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
3834b 61 6e 67 65 73 2c 20 0d 0a 20 20 20 20 20 20 20  anges, ..       
3834c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
3834d 20 70 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77   pTab, regOldRow
3834e 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64  id, onError, add
3834f 72 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68  r);....    /* Th
38350 65 20 72 6f 77 2d 74 72 69 67 67 65 72 20 6d 61  e row-trigger ma
38351 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74  y have deleted t
38352 68 65 20 72 6f 77 20 62 65 69 6e 67 20 75 70 64  he row being upd
38353 61 74 65 64 2e 20 49 6e 20 74 68 69 73 0d 0a 20  ated. In this.. 
38354 20 20 20 2a 2a 20 63 61 73 65 2c 20 6a 75 6d 70     ** case, jump
38355 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77   to the next row
38356 2e 20 4e 6f 20 75 70 64 61 74 65 73 20 6f 72 20  . No updates or 
38357 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 61  AFTER triggers a
38358 72 65 20 0d 0a 20 20 20 20 2a 2a 20 72 65 71 75  re ..    ** requ
38359 69 72 65 64 2e 20 54 68 69 73 20 62 65 68 61 76  ired. This behav
3835a 69 6f 75 72 20 2d 20 77 68 61 74 20 68 61 70 70  iour - what happ
3835b 65 6e 73 20 77 68 65 6e 20 74 68 65 20 72 6f 77  ens when the row
3835c 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 0d 0a   being updated..
3835d 20 20 20 20 2a 2a 20 69 73 20 64 65 6c 65 74 65      ** is delete
3835e 64 20 6f 72 20 72 65 6e 61 6d 65 64 20 62 79 20  d or renamed by 
3835f 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  a BEFORE trigger
38360 20 2d 20 69 73 20 6c 65 66 74 20 75 6e 64 65 66   - is left undef
38361 69 6e 65 64 20 69 6e 20 74 68 65 0d 0a 20 20 20  ined in the..   
38362 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f   ** documentatio
38363 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  n...    */..    
38364 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
38365 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
38366 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72  s, iCur, addr, r
38367 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0d 0a 0d 0a  egOldRowid);....
38368 20 20 20 20 2f 2a 20 49 66 20 69 74 20 64 69 64      /* If it did
38369 20 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 20   not delete it, 
3836a 74 68 65 20 72 6f 77 2d 74 72 69 67 67 65 72 20  the row-trigger 
3836b 6d 61 79 20 73 74 69 6c 6c 20 68 61 76 65 20 6d  may still have m
3836c 6f 64 69 66 69 65 64 20 0d 0a 20 20 20 20 2a 2a  odified ..    **
3836d 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   some of the col
3836e 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 6f 77 20  umns of the row 
3836f 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 4c  being updated. L
38370 6f 61 64 20 74 68 65 20 76 61 6c 75 65 73 20 66  oad the values f
38371 6f 72 20 0d 0a 20 20 20 20 2a 2a 20 61 6c 6c 20  or ..    ** all 
38372 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 6d 6f 64 69  columns not modi
38373 66 69 65 64 20 62 79 20 74 68 65 20 75 70 64 61  fied by the upda
38374 74 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74  te statement int
38375 6f 20 74 68 65 69 72 20 0d 0a 20 20 20 20 2a 2a  o their ..    **
38376 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 63 61   registers in ca
38377 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70  se this has happ
38378 65 6e 65 64 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  ened...    */.. 
38379 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
3837a 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d  ab->nCol; i++){.
3837b 0a 20 20 20 20 20 20 69 66 28 20 61 58 52 65 66  .      if( aXRef
3837c 5b 69 5d 3c 30 20 26 26 20 69 21 3d 70 54 61 62  [i]<0 && i!=pTab
3837d 2d 3e 69 50 4b 65 79 20 29 7b 0d 0a 20 20 20 20  ->iPKey ){..    
3837e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3837f 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
38380 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
38381 4e 65 77 2b 69 29 3b 0d 0a 20 20 20 20 20 20 20  New+i);..       
38382 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
38383 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
38384 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0d 0a 20 20  , regNew+i);..  
38385 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
38386 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 69 73 56 69  }....  if( !isVi
38387 65 77 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6a  ew ){..    int j
38388 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
38389 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
3838a 65 73 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74  ess of jump inst
3838b 72 75 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20  ruction */....  
3838c 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69    /* Do constrai
3838d 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0d 0a 20  nt checks. */.. 
3838e 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
3838f 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
38390 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ks(pParse, pTab,
38391 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
38392 69 64 2c 0d 0a 20 20 20 20 20 20 20 20 61 52 65  id,..        aRe
38393 67 49 64 78 2c 20 28 63 68 6e 67 52 6f 77 69 64  gIdx, (chngRowid
38394 3f 72 65 67 4f 6c 64 52 6f 77 69 64 3a 30 29 2c  ?regOldRowid:0),
38395 20 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64   1, onError, add
38396 72 2c 20 30 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  r, 0);....    /*
38397 20 44 6f 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e   Do FK constrain
38398 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0d 0a 20 20  t checks. */..  
38399 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0d 0a    if( hasFK ){..
3839a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43        sqlite3FkC
3839b 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61  heck(pParse, pTa
3839c 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20  b, regOldRowid, 
3839d 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  0);..    }....  
3839e 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
3839f 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
383a0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
383a1 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64  e current record
383a2 2e 20 20 2a 2f 0d 0a 20 20 20 20 6a 31 20 3d 20  .  */..    j1 = 
383a3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
383a4 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
383a5 73 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 4f  s, iCur, 0, regO
383a6 6c 64 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 73  ldRowid);..    s
383a7 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
383a8 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61  wIndexDelete(pPa
383a9 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  rse, pTab, iCur,
383aa 20 61 52 65 67 49 64 78 29 3b 0d 0a 20 20 0d 0a   aRegIdx);..  ..
383ab 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67 69      /* If changi
383ac 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ng the record nu
383ad 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68 65  mber, delete the
383ae 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20 20 2a 2f   old record.  */
383af 0d 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20  ..    if( hasFK 
383b0 7c 7c 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0d  || chngRowid ){.
383b1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
383b2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44  beAddOp2(v, OP_D
383b3 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 30 29 3b  elete, iCur, 0);
383b4 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
383b5 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
383b6 28 76 2c 20 6a 31 29 3b 0d 0a 0d 0a 20 20 20 20  (v, j1);....    
383b7 69 66 28 20 68 61 73 46 4b 20 29 7b 0d 0a 20 20  if( hasFK ){..  
383b8 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
383b9 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
383ba 20 30 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29   0, regNewRowid)
383bb 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20  ;..    }..  ..  
383bc 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20    /* Insert the 
383bd 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65  new index entrie
383be 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20 72 65  s and the new re
383bf 63 6f 72 64 2e 20 2a 2f 0d 0a 20 20 20 20 73 71  cord. */..    sq
383c0 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
383c1 65 72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ertion(pParse, p
383c2 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65  Tab, iCur, regNe
383c3 77 52 6f 77 69 64 2c 20 61 52 65 67 49 64 78 2c  wRowid, aRegIdx,
383c4 20 31 2c 20 30 2c 20 30 29 3b 0d 0a 0d 0a 20 20   1, 0, 0);....  
383c5 20 20 2f 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43    /* Do any ON C
383c6 41 53 43 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c  ASCADE, SET NULL
383c7 20 6f 72 20 53 45 54 20 44 45 46 41 55 4c 54 20   or SET DEFAULT 
383c8 6f 70 65 72 61 74 69 6f 6e 73 20 72 65 71 75 69  operations requi
383c9 72 65 64 20 74 6f 0d 0a 20 20 20 20 2a 2a 20 68  red to..    ** h
383ca 61 6e 64 6c 65 20 72 6f 77 73 20 28 70 6f 73 73  andle rows (poss
383cb 69 62 6c 79 20 69 6e 20 6f 74 68 65 72 20 74 61  ibly in other ta
383cc 62 6c 65 73 29 20 74 68 61 74 20 72 65 66 65 72  bles) that refer
383cd 20 76 69 61 20 61 20 66 6f 72 65 69 67 6e 20 6b   via a foreign k
383ce 65 79 0d 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  ey..    ** to th
383cf 65 20 72 6f 77 20 6a 75 73 74 20 75 70 64 61 74  e row just updat
383d0 65 64 2e 20 2a 2f 20 0d 0a 20 20 20 20 69 66 28  ed. */ ..    if(
383d1 20 68 61 73 46 4b 20 29 7b 0d 0a 20 20 20 20 20   hasFK ){..     
383d2 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e   sqlite3FkAction
383d3 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
383d4 70 43 68 61 6e 67 65 73 2c 20 72 65 67 4f 6c 64  pChanges, regOld
383d5 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a  Rowid);..    }..
383d6 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 63 72    }....  /* Incr
383d7 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20 63 6f  ement the row co
383d8 75 6e 74 65 72 20 0d 0a 20 20 2a 2f 0d 0a 20 20  unter ..  */..  
383d9 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
383da 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
383db 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70  s) && !pParse->p
383dc 54 72 69 67 67 65 72 54 61 62 29 7b 0d 0a 20 20  TriggerTab){..  
383dd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
383de 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
383df 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31  , regRowCount, 1
383e0 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c  );..  }....  sql
383e1 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
383e2 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  er(pParse, pTrig
383e3 67 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  ger, TK_UPDATE, 
383e4 70 43 68 61 6e 67 65 73 2c 20 0d 0a 20 20 20 20  pChanges, ..    
383e5 20 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c    TRIGGER_AFTER,
383e6 20 70 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77   pTab, regOldRow
383e7 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64  id, onError, add
383e8 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 65 70 65  r);....  /* Repe
383e9 61 74 20 74 68 65 20 61 62 6f 76 65 20 77 69 74  at the above wit
383ea 68 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72  h the next recor
383eb 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c  d to be updated,
383ec 20 75 6e 74 69 6c 0d 0a 20 20 2a 2a 20 61 6c 6c   until..  ** all
383ed 20 72 65 63 6f 72 64 20 73 65 6c 65 63 74 65 64   record selected
383ee 20 62 79 20 74 68 65 20 57 48 45 52 45 20 63 6c   by the WHERE cl
383ef 61 75 73 65 20 68 61 76 65 20 62 65 65 6e 20 75  ause have been u
383f0 70 64 61 74 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20  pdated...  */.. 
383f1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
383f2 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
383f3 2c 20 61 64 64 72 29 3b 0d 0a 20 20 73 71 6c 69  , addr);..  sqli
383f4 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
383f5 76 2c 20 61 64 64 72 29 3b 0d 0a 0d 0a 20 20 2f  v, addr);....  /
383f6 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c  * Close all tabl
383f7 65 73 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30  es */..  for(i=0
383f8 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
383f9 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
383fa 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
383fb 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
383fc 61 52 65 67 49 64 78 20 29 3b 0d 0a 20 20 20 20  aRegIdx );..    
383fd 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61  if( openAll || a
383fe 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0d 0a  RegIdx[i]>0 ){..
383ff 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
38400 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
38401 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 30  ose, iCur+i+1, 0
38402 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
38403 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
38404 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
38405 20 69 43 75 72 2c 20 30 29 3b 0d 0a 0d 0a 20 20   iCur, 0);....  
38406 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71  /* Update the sq
38407 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
38408 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74  ble by storing t
38409 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
3840a 65 0d 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20  e..  ** maximum 
3840b 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61  rowid counter va
3840c 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20 77 68  lues recorded wh
3840d 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e  ile inserting in
3840e 74 6f 0d 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63  to..  ** autoinc
3840f 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0d 0a  rement tables...
38410 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 50 61 72    */..  if( pPar
38411 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
38412 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
38413 72 54 61 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  rTab==0 ){..    
38414 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65  sqlite3Autoincre
38415 6d 65 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b  mentEnd(pParse);
38416 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20  ..  }....  /*.. 
38417 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e   ** Return the n
38418 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68  umber of rows th
38419 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 2e  at were changed.
3841a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
3841b 20 69 73 20 0d 0a 20 20 2a 2a 20 67 65 6e 65 72   is ..  ** gener
3841c 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75  ating code becau
3841d 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20  se of a call to 
3841e 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
3841f 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0d 0a 20 20  se(), do not..  
38420 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61  ** invoke the ca
38421 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
38422 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 64  ..  */..  if( (d
38423 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
38424 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70  CountRows) && !p
38425 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54  Parse->pTriggerT
38426 61 62 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e  ab && !pParse->n
38427 65 73 74 65 64 20 29 7b 0d 0a 20 20 20 20 73 71  ested ){..    sq
38428 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
38429 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
3842a 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29   regRowCount, 1)
3842b 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
3842c 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
3842d 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  1);..    sqlite3
3842e 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
3842f 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
38430 45 2c 20 22 72 6f 77 73 20 75 70 64 61 74 65 64  E, "rows updated
38431 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
38432 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 75 70 64 61 74  );..  }....updat
38433 65 5f 63 6c 65 61 6e 75 70 3a 0d 0a 20 20 73 71  e_cleanup:..  sq
38434 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
38435 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0d  Pop(&sContext);.
38436 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
38437 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0d 0a  (db, aRegIdx);..
38438 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
38439 64 62 2c 20 61 58 52 65 66 29 3b 0d 0a 20 20 73  db, aXRef);..  s
3843a 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
3843b 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74  ete(db, pTabList
3843c 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70  );..  sqlite3Exp
3843d 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
3843e 70 43 68 61 6e 67 65 73 29 3b 0d 0a 20 20 73 71  pChanges);..  sq
3843f 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
38440 64 62 2c 20 70 57 68 65 72 65 29 3b 0d 0a 20 20  db, pWhere);..  
38441 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 2f 2a 20 4d  return;..}../* M
38442 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77  ake sure "isView
38443 22 20 61 6e 64 20 6f 74 68 65 72 20 6d 61 63 72  " and other macr
38444 6f 73 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65  os defined above
38445 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20   are undefined. 
38446 4f 74 68 65 72 77 69 73 65 0d 0a 2a 2a 20 74 68  Otherwise..** th
38447 65 6c 79 20 6d 61 79 20 69 6e 74 65 72 66 65 72  ely may interfer
38448 65 20 77 69 74 68 20 63 6f 6d 70 69 6c 61 74 69  e with compilati
38449 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66 75 6e 63  on of other func
3844a 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69  tions in this fi
3844b 6c 65 0d 0a 2a 2a 20 28 6f 72 20 69 6e 20 61 6e  le..** (or in an
3844c 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 66 20 74  other file, if t
3844d 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73  his file becomes
3844e 20 70 61 72 74 20 6f 66 20 74 68 65 20 61 6d 61   part of the ama
3844f 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f 0d  lgamation).  */.
38450 0a 23 69 66 64 65 66 20 69 73 56 69 65 77 0d 0a  .#ifdef isView..
38451 20 23 75 6e 64 65 66 20 69 73 56 69 65 77 0d 0a   #undef isView..
38452 23 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 70  #endif..#ifdef p
38453 54 72 69 67 67 65 72 0d 0a 20 23 75 6e 64 65 66  Trigger.. #undef
38454 20 70 54 72 69 67 67 65 72 0d 0a 23 65 6e 64 69   pTrigger..#endi
38455 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  f....#ifndef SQL
38456 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
38457 54 41 42 4c 45 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65  TABLE../*..** Ge
38458 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
38459 61 6e 20 55 50 44 41 54 45 20 6f 66 20 61 20 76  an UPDATE of a v
3845a 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0d 0a 2a  irtual table...*
3845b 2a 0d 0a 2a 2a 20 54 68 65 20 73 74 72 61 74 65  *..** The strate
3845c 67 79 20 69 73 20 74 68 61 74 20 77 65 20 63 72  gy is that we cr
3845d 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72 69  eate an ephemeri
3845e 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  al table that co
3845f 6e 74 61 69 6e 73 0d 0a 2a 2a 20 66 6f 72 20 65  ntains..** for e
38460 61 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63 68  ach row to be ch
38461 61 6e 67 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  anged:..**..**  
38462 20 28 41 29 20 20 54 68 65 20 6f 72 69 67 69 6e   (A)  The origin
38463 61 6c 20 72 6f 77 69 64 20 6f 66 20 74 68 61 74  al rowid of that
38464 20 72 6f 77 2e 0d 0a 2a 2a 20 20 20 28 42 29 20   row...**   (B) 
38465 20 54 68 65 20 72 65 76 69 73 65 64 20 72 6f 77   The revised row
38466 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20  id for the row. 
38467 28 6e 6f 74 65 31 29 0d 0a 2a 2a 20 20 20 28 43  (note1)..**   (C
38468 29 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  )  The content o
38469 66 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69  f every column i
3846a 6e 20 74 68 65 20 72 6f 77 2e 0d 0a 2a 2a 0d 0a  n the row...**..
3846b 2a 2a 20 54 68 65 6e 20 77 65 20 6c 6f 6f 70 20  ** Then we loop 
3846c 6f 76 65 72 20 74 68 69 73 20 65 70 68 65 6d 65  over this epheme
3846d 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 66 6f  ral table and fo
3846e 72 20 65 61 63 68 20 72 6f 77 20 69 6e 0d 0a 2a  r each row in..*
3846f 2a 20 74 68 65 20 65 70 68 65 72 6d 65 72 61 6c  * the ephermeral
38470 20 74 61 62 6c 65 20 63 61 6c 6c 20 56 55 70 64   table call VUpd
38471 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65  ate...**..** Whe
38472 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f 70  n finished, drop
38473 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74   the ephemeral t
38474 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 28 6e  able...**..** (n
38475 6f 74 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20  ote1) Actually, 
38476 69 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64  if we know in ad
38477 76 61 6e 63 65 20 74 68 61 74 20 28 41 29 20 69  vance that (A) i
38478 73 20 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d  s always the sam
38479 65 0d 0a 2a 2a 20 61 73 20 28 42 29 20 77 65 20  e..** as (B) we 
3847a 6f 6e 6c 79 20 73 74 6f 72 65 20 28 41 29 2c 20  only store (A), 
3847b 74 68 65 6e 20 64 75 70 6c 69 63 61 74 65 20 28  then duplicate (
3847c 41 29 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0d  A) when pulling.
3847d 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66 20 74 68  .** it out of th
3847e 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
3847f 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  e before calling
38480 20 56 55 70 64 61 74 65 2e 0d 0a 2a 2f 0d 0a 73   VUpdate...*/..s
38481 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74  tatic void updat
38482 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0d 0a  eVirtualTable(..
38483 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
38484 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
38485 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
38486 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ..  SrcList *pSr
38487 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
38488 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
38489 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0d   be modified */.
3848a 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
3848b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
3848c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0d  irtual table */.
3848d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68  .  ExprList *pCh
3848e 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63  anges,  /* The c
3848f 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65  olumns to change
38490 20 69 6e 20 74 68 65 20 55 50 44 41 54 45 20 73   in the UPDATE s
38491 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 45  tatement */..  E
38492 78 70 72 20 2a 70 52 6f 77 69 64 2c 20 20 20 20  xpr *pRowid,    
38493 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
38494 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d 70  n used to recomp
38495 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a 2f  ute the rowid */
38496 0d 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20  ..  int *aXRef, 
38497 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70           /* Mapp
38498 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73  ing from columns
38499 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72   of pTab to entr
3849a 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  ies in pChanges 
3849b 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 57 68 65  */..  Expr *pWhe
3849c 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 48  re,        /* WH
3849d 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ERE clause of th
3849e 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
3849f 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f 6e 45  nt */..  int onE
384a0 72 72 6f 72 20 20 20 20 20 20 20 20 20 20 2f 2a  rror          /*
384a1 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 74 72   ON CONFLICT str
384a2 61 74 65 67 79 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ategy */..){..  
384a3 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
384a4 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72  ->pVdbe;  /* Vir
384a5 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64  tual machine und
384a6 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
384a7 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  */..  ExprList *
384a8 70 45 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20  pEList = 0;     
384a9 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  /* The result se
384aa 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
384ab 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20  statement */..  
384ac 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
384ad 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  = 0;      /* The
384ae 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
384af 74 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45  t */..  Expr *pE
384b0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
384b1 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 65    /* Temporary e
384b2 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20  xpression */..  
384b3 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20 20 20  int ephemTab;   
384b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
384b5 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72  le holding the r
384b6 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c  esult of the SEL
384b7 45 43 54 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b  ECT */..  int i;
384b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
384b9 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
384ba 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64  ter */..  int ad
384bb 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
384bc 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
384bd 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
384be 0d 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20  ..  int iReg;   
384bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
384c0 20 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20   First register 
384c1 69 6e 20 73 65 74 20 70 61 73 73 65 64 20 74 6f  in set passed to
384c2 20 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f 0d 0a   OP_VUpdate */..
384c3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
384c4 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44  pParse->db; /* D
384c5 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
384c6 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  on */..  const c
384c7 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f  har *pVTab = (co
384c8 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
384c9 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
384ca 54 61 62 29 3b 0d 0a 20 20 53 65 6c 65 63 74 44  Tab);..  SelectD
384cb 65 73 74 20 64 65 73 74 3b 0d 0a 0d 0a 20 20 2f  est dest;....  /
384cc 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * Construct the 
384cd 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
384ce 20 74 68 61 74 20 77 69 6c 6c 20 66 69 6e 64 20   that will find 
384cf 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66  the new values f
384d0 6f 72 0d 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64  or..  ** all upd
384d1 61 74 65 64 20 72 6f 77 73 2e 20 0d 0a 20 20 2a  ated rows. ..  *
384d2 2f 0d 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71  /..  pEList = sq
384d3 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
384d4 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 73  end(pParse, 0, s
384d5 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
384d6 4b 5f 49 44 2c 20 22 5f 72 6f 77 69 64 5f 22 29  K_ID, "_rowid_")
384d7 29 3b 0d 0a 20 20 69 66 28 20 70 52 6f 77 69 64  );..  if( pRowid
384d8 20 29 7b 0d 0a 20 20 20 20 70 45 4c 69 73 74 20   ){..    pEList 
384d9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
384da 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
384db 70 45 4c 69 73 74 2c 0d 0a 20 20 20 20 20 20 20  pEList,..       
384dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
384dd 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
384de 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
384df 52 6f 77 69 64 2c 20 30 29 29 3b 0d 0a 20 20 7d  Rowid, 0));..  }
384e0 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
384e1 2d 3e 69 50 4b 65 79 3c 30 20 29 3b 0d 0a 20 20  ->iPKey<0 );..  
384e2 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
384e3 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20  >nCol; i++){..  
384e4 20 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d    if( aXRef[i]>=
384e5 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 45 78 70  0 ){..      pExp
384e6 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  r = sqlite3ExprD
384e7 75 70 28 64 62 2c 20 70 43 68 61 6e 67 65 73 2d  up(db, pChanges-
384e8 3e 61 5b 61 58 52 65 66 5b 69 5d 5d 2e 70 45 78  >a[aXRef[i]].pEx
384e9 70 72 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 65 6c  pr, 0);..    }el
384ea 73 65 7b 0d 0a 20 20 20 20 20 20 70 45 78 70 72  se{..      pExpr
384eb 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
384ec 62 2c 20 54 4b 5f 49 44 2c 20 70 54 61 62 2d 3e  b, TK_ID, pTab->
384ed 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0d  aCol[i].zName);.
384ee 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 45 4c 69  .    }..    pELi
384ef 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
384f0 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
384f1 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 78 70 72  e, pEList, pExpr
384f2 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 53 65 6c 65  );..  }..  pSele
384f3 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
384f4 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 70 45  ctNew(pParse, pE
384f5 4c 69 73 74 2c 20 70 53 72 63 2c 20 70 57 68 65  List, pSrc, pWhe
384f6 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  re, 0, 0, 0, 0, 
384f7 30 2c 20 30 29 3b 0d 0a 20 20 0d 0a 20 20 2f 2a  0, 0);..  ..  /*
384f8 20 43 72 65 61 74 65 20 74 68 65 20 65 70 68 65   Create the ephe
384f9 6d 65 72 61 6c 20 74 61 62 6c 65 20 69 6e 74 6f  meral table into
384fa 20 77 68 69 63 68 20 74 68 65 20 75 70 64 61 74   which the updat
384fb 65 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0d 0a  e results will..
384fc 20 20 2a 2a 20 62 65 20 73 74 6f 72 65 64 2e 0d    ** be stored..
384fd 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
384fe 20 76 20 29 3b 0d 0a 20 20 65 70 68 65 6d 54 61   v );..  ephemTa
384ff 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
38500 2b 2b 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  ++;..  sqlite3Vd
38501 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f  beAddOp2(v, OP_O
38502 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65 70  penEphemeral, ep
38503 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43  hemTab, pTab->nC
38504 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29  ol+1+(pRowid!=0)
38505 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
38506 65 43 68 61 6e 67 65 50 35 28 76 2c 20 42 54 52  eChangeP5(v, BTR
38507 45 45 5f 55 4e 4f 52 44 45 52 45 44 29 3b 0d 0a  EE_UNORDERED);..
38508 0d 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20  ..  /* fill the 
38509 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
3850a 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ..  */..  sqlite
3850b 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
3850c 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65  &dest, SRT_Table
3850d 2c 20 65 70 68 65 6d 54 61 62 29 3b 0d 0a 20 20  , ephemTab);..  
3850e 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
3850f 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26  arse, pSelect, &
38510 64 65 73 74 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 47  dest);....  /* G
38511 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
38512 73 63 61 6e 20 74 68 65 20 65 70 68 65 6d 65 72  scan the ephemer
38513 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 63 61 6c  al table and cal
38514 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f 0d 0a 20  l VUpdate. */.. 
38515 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65   iReg = ++pParse
38516 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 70 50 61 72 73  ->nMem;..  pPars
38517 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d  e->nMem += pTab-
38518 3e 6e 43 6f 6c 2b 31 3b 0d 0a 20 20 61 64 64 72  >nCol+1;..  addr
38519 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
3851a 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
3851b 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  d, ephemTab, 0);
3851c 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ..  sqlite3VdbeA
3851d 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
3851e 6d 6e 2c 20 20 65 70 68 65 6d 54 61 62 2c 20 30  mn,  ephemTab, 0
3851f 2c 20 69 52 65 67 29 3b 0d 0a 20 20 73 71 6c 69  , iReg);..  sqli
38520 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
38521 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65   OP_Column, ephe
38522 6d 54 61 62 2c 20 28 70 52 6f 77 69 64 3f 31 3a  mTab, (pRowid?1:
38523 30 29 2c 20 69 52 65 67 2b 31 29 3b 0d 0a 20 20  0), iReg+1);..  
38524 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
38525 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20  >nCol; i++){..  
38526 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
38527 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
38528 2c 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b  , ephemTab, i+1+
38529 28 70 52 6f 77 69 64 21 3d 30 29 2c 20 69 52 65  (pRowid!=0), iRe
3852a 67 2b 32 2b 69 29 3b 0d 0a 20 20 7d 0d 0a 20 20  g+2+i);..  }..  
3852b 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
3852c 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ritable(pParse, 
3852d 70 54 61 62 29 3b 0d 0a 20 20 73 71 6c 69 74 65  pTab);..  sqlite
3852e 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
3852f 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70 54  P_VUpdate, 0, pT
38530 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65 67  ab->nCol+2, iReg
38531 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42  , pVTab, P4_VTAB
38532 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
38533 65 43 68 61 6e 67 65 50 35 28 76 2c 20 6f 6e 45  eChangeP5(v, onE
38534 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
38535 20 3f 20 4f 45 5f 41 62 6f 72 74 20 3a 20 6f 6e   ? OE_Abort : on
38536 45 72 72 6f 72 29 3b 0d 0a 20 20 73 71 6c 69 74  Error);..  sqlit
38537 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73  e3MayAbort(pPars
38538 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64  e);..  sqlite3Vd
38539 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
3853a 65 78 74 2c 20 65 70 68 65 6d 54 61 62 2c 20 61  ext, ephemTab, a
3853b 64 64 72 2b 31 29 3b 0d 0a 20 20 73 71 6c 69 74  ddr+1);..  sqlit
3853c 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
3853d 2c 20 61 64 64 72 29 3b 0d 0a 20 20 73 71 6c 69  , addr);..  sqli
3853e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3853f 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d   OP_Close, ephem
38540 54 61 62 2c 20 30 29 3b 0d 0a 0d 0a 20 20 2f 2a  Tab, 0);....  /*
38541 20 43 6c 65 61 6e 75 70 20 2a 2f 0d 0a 20 20 73   Cleanup */..  s
38542 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
38543 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
38544 20 20 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a    ..}..#endif /*
38545 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
38546 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a  TUALTABLE */....
38547 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
38548 45 6e 64 20 6f 66 20 75 70 64 61 74 65 2e 63 20  End of update.c 
38549 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3854a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3854b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
3854c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3854d 20 42 65 67 69 6e 20 66 69 6c 65 20 76 61 63 75   Begin file vacu
3854e 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
3854f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
38551 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 33 20 41 70  ../*..** 2003 Ap
38552 72 69 6c 20 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ril 6..**..** Th
38553 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
38554 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
38555 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
38556 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
38557 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
38558 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
38559 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
3855a 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
3855b 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
3855c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3855d 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3855e 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3855f 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
38560 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
38561 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
38562 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
38563 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
38564 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
38565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38566 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38567 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a  *************..*
38569 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
3856a 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
3856b 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
3856c 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0d  VACUUM command..
3856d 0a 2a 2a 0d 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20  .**..** Most of 
3856e 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
3856f 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69   file may be omi
38570 74 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67  tted by defining
38571 20 74 68 65 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f   the..** SQLITE_
38572 4f 4d 49 54 5f 56 41 43 55 55 4d 20 6d 61 63 72  OMIT_VACUUM macr
38573 6f 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 20 21 64  o...*/....#if !d
38574 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
38575 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64  IT_VACUUM) && !d
38576 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
38577 49 54 5f 41 54 54 41 43 48 29 0d 0a 2f 2a 0d 0a  IT_ATTACH)../*..
38578 2a 2a 20 46 69 6e 61 6c 69 7a 65 20 61 20 70 72  ** Finalize a pr
38579 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3857a 2e 20 20 49 66 20 74 68 65 72 65 20 77 61 73 20  .  If there was 
3857b 61 6e 20 65 72 72 6f 72 2c 20 73 74 6f 72 65 20  an error, store 
3857c 74 68 65 0d 0a 2a 2a 20 74 65 78 74 20 6f 66 20  the..** text of 
3857d 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
3857e 65 20 69 6e 20 2a 70 7a 45 72 72 4d 73 67 2e 20  e in *pzErrMsg. 
3857f 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75   Return the resu
38580 6c 74 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74  lt code...*/..st
38581 61 74 69 63 20 69 6e 74 20 76 61 63 75 75 6d 46  atic int vacuumF
38582 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 20  inalize(sqlite3 
38583 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  *db, sqlite3_stm
38584 74 20 2a 70 53 74 6d 74 2c 20 63 68 61 72 20 2a  t *pStmt, char *
38585 2a 70 7a 45 72 72 4d 73 67 29 7b 0d 0a 20 20 69  *pzErrMsg){..  i
38586 6e 74 20 72 63 3b 0d 0a 20 20 72 63 20 3d 20 73  nt rc;..  rc = s
38587 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
38588 7a 65 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29  ze((Vdbe*)pStmt)
38589 3b 0d 0a 20 20 69 66 28 20 72 63 20 29 7b 0d 0a  ;..  if( rc ){..
3858a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
3858b 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64  ring(pzErrMsg, d
3858c 62 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  b, sqlite3_errms
3858d 67 28 64 62 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20  g(db));..  }..  
3858e 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
3858f 0a 2f 2a 0d 0a 2a 2a 20 45 78 65 63 75 74 65 20  ./*..** Execute 
38590 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65  zSql on database
38591 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e 20 65   db. Return an e
38592 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a  rror code...*/..
38593 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53  static int execS
38594 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
38595 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
38596 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
38597 6c 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73  l){..  sqlite3_s
38598 74 6d 74 20 2a 70 53 74 6d 74 3b 0d 0a 20 20 56  tmt *pStmt;..  V
38599 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 72 63 3b  VA_ONLY( int rc;
3859a 20 29 0d 0a 20 20 69 66 28 20 21 7a 53 71 6c 20   )..  if( !zSql 
3859b 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ){..    return S
3859c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
3859d 7d 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  }..  if( SQLITE_
3859e 4f 4b 21 3d 73 71 6c 69 74 65 33 5f 70 72 65 70  OK!=sqlite3_prep
3859f 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  are(db, zSql, -1
385a0 2c 20 26 70 53 74 6d 74 2c 20 30 29 20 29 7b 0d  , &pStmt, 0) ){.
385a1 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
385a2 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
385a3 64 62 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  db, sqlite3_errm
385a4 73 67 28 64 62 29 29 3b 0d 0a 20 20 20 20 72 65  sg(db));..    re
385a5 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72  turn sqlite3_err
385a6 63 6f 64 65 28 64 62 29 3b 0d 0a 20 20 7d 0d 0a  code(db);..  }..
385a7 20 20 56 56 41 5f 4f 4e 4c 59 28 20 72 63 20 3d    VVA_ONLY( rc =
385a8 20 29 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   ) sqlite3_step(
385a9 70 53 74 6d 74 29 3b 0d 0a 20 20 61 73 73 65 72  pStmt);..  asser
385aa 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  t( rc!=SQLITE_RO
385ab 57 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26  W || (db->flags&
385ac 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
385ad 29 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 76  ) );..  return v
385ae 61 63 75 75 6d 46 69 6e 61 6c 69 7a 65 28 64 62  acuumFinalize(db
385af 2c 20 70 53 74 6d 74 2c 20 70 7a 45 72 72 4d 73  , pStmt, pzErrMs
385b0 67 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  g);..}..../*..**
385b1 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e   Execute zSql on
385b2 20 64 61 74 61 62 61 73 65 20 64 62 2e 20 54 68   database db. Th
385b3 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75  e statement retu
385b4 72 6e 73 20 65 78 61 63 74 6c 79 0d 0a 2a 2a 20  rns exactly..** 
385b5 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45 78 65 63  one column. Exec
385b6 75 74 65 20 74 68 69 73 20 61 73 20 53 51 4c 20  ute this as SQL 
385b7 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  on the same data
385b8 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  base...*/..stati
385b9 63 20 69 6e 74 20 65 78 65 63 45 78 65 63 53 71  c int execExecSq
385ba 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  l(sqlite3 *db, c
385bb 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 20  har **pzErrMsg, 
385bc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
385bd 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  ){..  sqlite3_st
385be 6d 74 20 2a 70 53 74 6d 74 3b 0d 0a 20 20 69 6e  mt *pStmt;..  in
385bf 74 20 72 63 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20  t rc;....  rc = 
385c0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
385c1 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
385c2 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 69 66 28  Stmt, 0);..  if(
385c3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
385c4 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20   return rc;.... 
385c5 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52   while( SQLITE_R
385c6 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
385c7 28 70 53 74 6d 74 29 20 29 7b 0d 0a 20 20 20 20  (pStmt) ){..    
385c8 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
385c9 20 70 7a 45 72 72 4d 73 67 2c 20 28 63 68 61 72   pzErrMsg, (char
385ca 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
385cb 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29  _text(pStmt, 0))
385cc 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  ;..    if( rc!=S
385cd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
385ce 20 20 20 76 61 63 75 75 6d 46 69 6e 61 6c 69 7a     vacuumFinaliz
385cf 65 28 64 62 2c 20 70 53 74 6d 74 2c 20 70 7a 45  e(db, pStmt, pzE
385d0 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 20 20 72  rrMsg);..      r
385d1 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d  eturn rc;..    }
385d2 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
385d3 6e 20 76 61 63 75 75 6d 46 69 6e 61 6c 69 7a 65  n vacuumFinalize
385d4 28 64 62 2c 20 70 53 74 6d 74 2c 20 70 7a 45 72  (db, pStmt, pzEr
385d5 72 4d 73 67 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rMsg);..}..../*.
385d6 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e  .** The non-stan
385d7 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d  dard VACUUM comm
385d8 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63  and is used to c
385d9 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61 74 61  lean up the data
385da 62 61 73 65 2c 0d 0a 2a 2a 20 63 6f 6c 6c 61 70  base,..** collap
385db 73 65 20 66 72 65 65 20 73 70 61 63 65 2c 20 65  se free space, e
385dc 74 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65 6c  tc.  It is model
385dd 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 56 41  led after the VA
385de 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 0d 0a 2a 2a  CUUM command..**
385df 20 69 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0d   in PostgreSQL..
385e0 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 76 65 72 73 69  .**..** In versi
385e1 6f 6e 20 31 2e 30 2e 78 20 6f 66 20 53 51 4c 69  on 1.0.x of SQLi
385e2 74 65 2c 20 74 68 65 20 56 41 43 55 55 4d 20 63  te, the VACUUM c
385e3 6f 6d 6d 61 6e 64 20 77 6f 75 6c 64 20 63 61 6c  ommand would cal
385e4 6c 0d 0a 2a 2a 20 67 64 62 6d 5f 72 65 6f 72 67  l..** gdbm_reorg
385e5 61 6e 69 7a 65 28 29 20 6f 6e 20 61 6c 6c 20 74  anize() on all t
385e6 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
385e7 65 73 2e 20 20 42 75 74 20 62 65 67 69 6e 6e 69  es.  But beginni
385e8 6e 67 0d 0a 2a 2a 20 77 69 74 68 20 32 2e 30 2e  ng..** with 2.0.
385e9 30 2c 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e  0, SQLite no lon
385ea 67 65 72 20 75 73 65 73 20 47 44 42 4d 20 73 6f  ger uses GDBM so
385eb 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20 68 61   this command ha
385ec 73 0d 0a 2a 2a 20 62 65 63 6f 6d 65 20 61 20 6e  s..** become a n
385ed 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  o-op...*/..SQLIT
385ee 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
385ef 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
385f0 73 65 20 2a 70 50 61 72 73 65 29 7b 0d 0a 20 20  se *pParse){..  
385f1 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
385f2 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
385f3 3b 0d 0a 20 20 69 66 28 20 76 20 29 7b 0d 0a 20  ;..  if( v ){.. 
385f4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
385f5 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63 75 75  dOp2(v, OP_Vacuu
385f6 6d 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a  m, 0, 0);..  }..
385f7 20 20 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 0d 0a    return;..}....
385f8 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
385f9 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ine implements t
385fa 68 65 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63  he OP_Vacuum opc
385fb 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
385fc 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
385fd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
385fe 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 20 2a  RunVacuum(char *
385ff 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74  *pzErrMsg, sqlit
38600 65 33 20 2a 64 62 29 7b 0d 0a 20 20 69 6e 74 20  e3 *db){..  int 
38601 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
38602 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
38603 64 65 20 66 72 6f 6d 20 73 65 72 76 69 63 65 20  de from service 
38604 72 6f 75 74 69 6e 65 73 20 2a 2f 0d 0a 20 20 42  routines */..  B
38605 74 72 65 65 20 2a 70 4d 61 69 6e 3b 20 20 20 20  tree *pMain;    
38606 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
38607 74 61 62 61 73 65 20 62 65 69 6e 67 20 76 61 63  tabase being vac
38608 75 75 6d 65 64 20 2a 2f 0d 0a 20 20 42 74 72 65  uumed */..  Btre
38609 65 20 2a 70 54 65 6d 70 3b 20 20 20 20 20 20 20  e *pTemp;       
3860a 20 20 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 6f      /* The tempo
3860b 72 61 72 79 20 64 61 74 61 62 61 73 65 20 77 65  rary database we
3860c 20 76 61 63 75 75 6d 20 69 6e 74 6f 20 2a 2f 0d   vacuum into */.
3860d 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20  .  char *zSql = 
3860e 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  0;         /* SQ
3860f 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0d  L statements */.
38610 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66 6c 61  .  int saved_fla
38611 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 61  gs;        /* Sa
38612 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ved value of the
38613 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0d 0a 20   db->flags */.. 
38614 20 69 6e 74 20 73 61 76 65 64 5f 6e 43 68 61 6e   int saved_nChan
38615 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65  ge;      /* Save
38616 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e  d value of db->n
38617 43 68 61 6e 67 65 20 2a 2f 0d 0a 20 20 69 6e 74  Change */..  int
38618 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61   saved_nTotalCha
38619 6e 67 65 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  nge; /* Saved va
3861a 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 54 6f 74 61  lue of db->nTota
3861b 6c 43 68 61 6e 67 65 20 2a 2f 0d 0a 20 20 76 6f  lChange */..  vo
3861c 69 64 20 28 2a 73 61 76 65 64 5f 78 54 72 61 63  id (*saved_xTrac
3861d 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
3861e 68 61 72 2a 29 3b 20 20 2f 2a 20 53 61 76 65 64  har*);  /* Saved
3861f 20 64 62 2d 3e 78 54 72 61 63 65 20 2a 2f 0d 0a   db->xTrace */..
38620 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20    Db *pDb = 0;  
38621 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
38622 61 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20  abase to detach 
38623 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d  at end of vacuum
38624 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 4d 65 6d   */..  int isMem
38625 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db;            /
38626 2a 20 54 72 75 65 20 69 66 20 76 61 63 75 75 6d  * True if vacuum
38627 69 6e 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64  ing a :memory: d
38628 61 74 61 62 61 73 65 20 2a 2f 0d 0a 20 20 69 6e  atabase */..  in
38629 74 20 6e 52 65 73 3b 20 20 20 20 20 20 20 20 20  t nRes;         
3862a 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
3862b 66 20 72 65 73 65 72 76 65 64 20 73 70 61 63 65  f reserved space
3862c 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65   at the end of e
3862d 61 63 68 20 70 61 67 65 20 2a 2f 0d 0a 20 20 69  ach page */..  i
3862e 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20  nt nDb;         
3862f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
38630 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74   of attached dat
38631 61 62 61 73 65 73 20 2a 2f 0d 0a 0d 0a 20 20 69  abases */....  i
38632 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
38633 69 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  it ){..    sqlit
38634 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
38635 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f  rMsg, db, "canno
38636 74 20 56 41 43 55 55 4d 20 66 72 6f 6d 20 77 69  t VACUUM from wi
38637 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  thin a transacti
38638 6f 6e 22 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  on");..    retur
38639 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
3863a 0a 20 20 7d 0d 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
3863b 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20  activeVdbeCnt>1 
3863c 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 53  ){..    sqlite3S
3863d 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
3863e 67 2c 20 64 62 2c 22 63 61 6e 6e 6f 74 20 56 41  g, db,"cannot VA
3863f 43 55 55 4d 20 2d 20 53 51 4c 20 73 74 61 74 65  CUUM - SQL state
38640 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73  ments in progres
38641 73 22 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  s");..    return
38642 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a   SQLITE_ERROR;..
38643 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 61 76 65    }....  /* Save
38644 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
38645 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ue of the databa
38646 73 65 20 66 6c 61 67 73 20 73 6f 20 74 68 61 74  se flags so that
38647 20 69 74 20 63 61 6e 20 62 65 20 0d 0a 20 20 2a   it can be ..  *
38648 2a 20 72 65 73 74 6f 72 65 64 20 62 65 66 6f 72  * restored befor
38649 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 54 68 65  e returning. The
3864a 6e 20 73 65 74 20 74 68 65 20 77 72 69 74 61 62  n set the writab
3864b 6c 65 2d 73 63 68 65 6d 61 20 66 6c 61 67 2c 20  le-schema flag, 
3864c 61 6e 64 0d 0a 20 20 2a 2a 20 64 69 73 61 62 6c  and..  ** disabl
3864d 65 20 43 48 45 43 4b 20 61 6e 64 20 66 6f 72 65  e CHECK and fore
3864e 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
3864f 6e 74 73 2e 20 20 2a 2f 0d 0a 20 20 73 61 76 65  nts.  */..  save
38650 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c  d_flags = db->fl
38651 61 67 73 3b 0d 0a 20 20 73 61 76 65 64 5f 6e 43  ags;..  saved_nC
38652 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43 68 61  hange = db->nCha
38653 6e 67 65 3b 0d 0a 20 20 73 61 76 65 64 5f 6e 54  nge;..  saved_nT
38654 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62 2d  otalChange = db-
38655 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0d 0a  >nTotalChange;..
38656 20 20 73 61 76 65 64 5f 78 54 72 61 63 65 20 3d    saved_xTrace =
38657 20 64 62 2d 3e 78 54 72 61 63 65 3b 0d 0a 20 20   db->xTrace;..  
38658 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
38659 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20  ITE_WriteSchema 
3865a 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  | SQLITE_IgnoreC
3865b 68 65 63 6b 73 20 7c 20 53 51 4c 49 54 45 5f 50  hecks | SQLITE_P
3865c 72 65 66 65 72 42 75 69 6c 74 69 6e 3b 0d 0a 20  referBuiltin;.. 
3865d 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28   db->flags &= ~(
3865e 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
3865f 79 73 20 7c 20 53 51 4c 49 54 45 5f 52 65 76 65  ys | SQLITE_Reve
38660 72 73 65 4f 72 64 65 72 29 3b 0d 0a 20 20 64 62  rseOrder);..  db
38661 2d 3e 78 54 72 61 63 65 20 3d 20 30 3b 0d 0a 0d  ->xTrace = 0;...
38662 0a 20 20 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61  .  pMain = db->a
38663 44 62 5b 30 5d 2e 70 42 74 3b 0d 0a 20 20 69 73  Db[0].pBt;..  is
38664 4d 65 6d 44 62 20 3d 20 73 71 6c 69 74 65 33 50  MemDb = sqlite3P
38665 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69  agerIsMemdb(sqli
38666 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 4d  te3BtreePager(pM
38667 61 69 6e 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41  ain));....  /* A
38668 74 74 61 63 68 20 74 68 65 20 74 65 6d 70 6f 72  ttach the tempor
38669 61 72 79 20 64 61 74 61 62 61 73 65 20 61 73 20  ary database as 
3866a 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54 68 65  'vacuum_db'. The
3866b 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70 72 61   synchronous pra
3866c 67 6d 61 0d 0a 20 20 2a 2a 20 63 61 6e 20 62 65  gma..  ** can be
3866d 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66 6f   set to 'off' fo
3866e 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20  r this file, as 
3866f 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76 65  it is not recove
38670 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0d 0a  red if a crash..
38671 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77    ** occurs anyw
38672 61 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74  ay. The integrit
38673 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
38674 65 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20  e is maintained 
38675 62 79 20 61 0d 0a 20 20 2a 2a 20 28 70 6f 73 73  by a..  ** (poss
38676 69 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73  ibly synchronous
38677 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  ) transaction op
38678 65 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e  ened on the main
38679 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65   database before
3867a 0d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74  ..  ** sqlite3Bt
3867b 72 65 65 43 6f 70 79 46 69 6c 65 28 29 20 69 73  reeCopyFile() is
3867c 20 63 61 6c 6c 65 64 2e 0d 0a 20 20 2a 2a 0d 0a   called...  **..
3867d 20 20 2a 2a 20 41 6e 20 6f 70 74 69 6d 69 73 61    ** An optimisa
3867e 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 74 6f  tion would be to
3867f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e   use a non-journ
38680 61 6c 65 64 20 70 61 67 65 72 2e 0d 0a 20 20 2a  aled pager...  *
38681 2a 20 28 4c 61 74 65 72 3a 29 20 49 20 74 72 69  * (Later:) I tri
38682 65 64 20 73 65 74 74 69 6e 67 20 22 50 52 41 47  ed setting "PRAG
38683 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 6a 6f 75  MA vacuum_db.jou
38684 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22 20 62  rnal_mode=OFF" b
38685 75 74 0d 0a 20 20 2a 2a 20 74 68 61 74 20 61 63  ut..  ** that ac
38686 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68 65 20  tually made the 
38687 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f 77 65  VACUUM run slowe
38688 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c 65 20  r.  Very little 
38689 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0d 0a 20 20 2a  journalling..  *
3868a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72  * actually occur
3868b 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 76  s when doing a v
3868c 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65 20  acuum since the 
3868d 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e 69  vacuum_db is ini
3868e 74 69 61 6c 6c 79 0d 0a 20 20 2a 2a 20 65 6d 70  tially..  ** emp
3868f 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f  ty.  Only the jo
38690 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
38691 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65  written.  Appare
38692 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f  ntly it takes mo
38693 72 65 0d 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f  re..  ** time to
38694 20 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74   parse and run t
38695 68 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72  he PRAGMA to tur
38696 6e 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66  n journalling of
38697 66 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0d 0a  f than it does..
38698 20 20 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68    ** to write th
38699 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
3869a 20 66 69 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   file...  */..  
3869b 6e 44 62 20 3d 20 64 62 2d 3e 6e 44 62 3b 0d 0a  nDb = db->nDb;..
3869c 20 20 69 66 28 20 73 71 6c 69 74 65 33 54 65 6d    if( sqlite3Tem
3869d 70 49 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b  pInMemory(db) ){
3869e 0d 0a 20 20 20 20 7a 53 71 6c 20 3d 20 22 41 54  ..    zSql = "AT
3869f 54 41 43 48 20 27 3a 6d 65 6d 6f 72 79 3a 27 20  TACH ':memory:' 
386a0 41 53 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0d  AS vacuum_db;";.
386a1 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a  .  }else{..    z
386a2 53 71 6c 20 3d 20 22 41 54 54 41 43 48 20 27 27  Sql = "ATTACH ''
386a3 20 41 53 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b   AS vacuum_db;";
386a4 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 65 78  ..  }..  rc = ex
386a5 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d  ecSql(db, pzErrM
386a6 73 67 2c 20 7a 53 71 6c 29 3b 0d 0a 20 20 69 66  sg, zSql);..  if
386a7 28 20 64 62 2d 3e 6e 44 62 3e 6e 44 62 20 29 7b  ( db->nDb>nDb ){
386a8 0d 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d  ..    pDb = &db-
386a9 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b  >aDb[db->nDb-1];
386aa 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74  ..    assert( st
386ab 72 63 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c  rcmp(pDb->zName,
386ac 22 76 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30 20  "vacuum_db")==0 
386ad 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72  );..  }..  if( r
386ae 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
386af 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
386b0 6d 3b 0d 0a 20 20 70 54 65 6d 70 20 3d 20 64 62  m;..  pTemp = db
386b1 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d  ->aDb[db->nDb-1]
386b2 2e 70 42 74 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68  .pBt;....  /* Th
386b3 65 20 63 61 6c 6c 20 74 6f 20 65 78 65 63 53 71  e call to execSq
386b4 6c 28 29 20 74 6f 20 61 74 74 61 63 68 20 74 68  l() to attach th
386b5 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20  e temp database 
386b6 68 61 73 20 6c 65 66 74 20 74 68 65 20 66 69 6c  has left the fil
386b7 65 0d 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 20 28  e..  ** locked (
386b8 61 73 20 74 68 65 72 65 20 77 61 73 20 6d 6f 72  as there was mor
386b9 65 20 74 68 61 6e 20 6f 6e 65 20 61 63 74 69 76  e than one activ
386ba 65 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 6e  e statement when
386bb 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
386bc 0d 0a 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 74  ..  ** to read t
386bd 68 65 20 73 63 68 65 6d 61 20 77 61 73 20 63 6f  he schema was co
386be 6e 63 6c 75 64 65 64 2e 20 55 6e 6c 6f 63 6b 20  ncluded. Unlock 
386bf 69 74 20 68 65 72 65 20 73 6f 20 74 68 61 74 20  it here so that 
386c0 74 68 69 73 20 64 6f 65 73 6e 27 74 0d 0a 20 20  this doesn't..  
386c1 2a 2a 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d  ** cause problem
386c2 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c 20 74  s for the call t
386c3 6f 20 42 74 72 65 65 53 65 74 50 61 67 65 53 69  o BtreeSetPageSi
386c4 7a 65 28 29 20 62 65 6c 6f 77 2e 20 20 2a 2f 0d  ze() below.  */.
386c5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  .  sqlite3BtreeC
386c6 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0d 0a 0d  ommit(pTemp);...
386c7 0a 20 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65  .  nRes = sqlite
386c8 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65  3BtreeGetReserve
386c9 28 70 4d 61 69 6e 29 3b 0d 0a 0d 0a 20 20 2f 2a  (pMain);....  /*
386ca 20 41 20 56 41 43 55 55 4d 20 63 61 6e 6e 6f 74   A VACUUM cannot
386cb 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 67 65   change the page
386cc 73 69 7a 65 20 6f 66 20 61 6e 20 65 6e 63 72 79  size of an encry
386cd 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 2a  pted database. *
386ce 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
386cf 5f 48 41 53 5f 43 4f 44 45 43 0d 0a 20 20 69 66  _HAS_CODEC..  if
386d0 28 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69  ( db->nextPagesi
386d1 7a 65 20 29 7b 0d 0a 20 20 20 20 65 78 74 65 72  ze ){..    exter
386d2 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  n void sqlite3Co
386d3 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65  decGetKey(sqlite
386d4 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c  3*, int, void**,
386d5 20 69 6e 74 2a 29 3b 0d 0a 20 20 20 20 69 6e 74   int*);..    int
386d6 20 6e 4b 65 79 3b 0d 0a 20 20 20 20 63 68 61 72   nKey;..    char
386d7 20 2a 7a 4b 65 79 3b 0d 0a 20 20 20 20 73 71 6c   *zKey;..    sql
386d8 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28  ite3CodecGetKey(
386d9 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26  db, 0, (void**)&
386da 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0d 0a 20  zKey, &nKey);.. 
386db 20 20 20 69 66 28 20 6e 4b 65 79 20 29 20 64 62     if( nKey ) db
386dc 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d  ->nextPagesize =
386dd 20 30 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66   0;..  }..#endif
386de 0d 0a 0d 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  ....  /* Do not 
386df 61 74 74 65 6d 70 74 20 74 6f 20 63 68 61 6e 67  attempt to chang
386e0 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  e the page size 
386e1 66 6f 72 20 61 20 57 41 4c 20 64 61 74 61 62 61  for a WAL databa
386e2 73 65 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  se */..  if( sql
386e3 69 74 65 33 50 61 67 65 72 47 65 74 4a 6f 75 72  ite3PagerGetJour
386e4 6e 61 6c 4d 6f 64 65 28 73 71 6c 69 74 65 33 42  nalMode(sqlite3B
386e5 74 72 65 65 50 61 67 65 72 28 70 4d 61 69 6e 29  treePager(pMain)
386e6 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )..             
386e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386e9 20 20 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41    ==PAGER_JOURNA
386ea 4c 4d 4f 44 45 5f 57 41 4c 20 29 7b 0d 0a 20 20  LMODE_WAL ){..  
386eb 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69    db->nextPagesi
386ec 7a 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ze = 0;..  }....
386ed 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
386ee 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeSetPageSize(pT
386ef 65 6d 70 2c 20 73 71 6c 69 74 65 33 42 74 72 65  emp, sqlite3Btre
386f0 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61  eGetPageSize(pMa
386f1 69 6e 29 2c 20 6e 52 65 73 2c 20 30 29 0d 0a 20  in), nRes, 0).. 
386f2 20 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20 26    || (!isMemDb &
386f3 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  & sqlite3BtreeSe
386f4 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c  tPageSize(pTemp,
386f5 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a   db->nextPagesiz
386f6 65 2c 20 6e 52 65 73 2c 20 30 29 29 0d 0a 20 20  e, nRes, 0))..  
386f7 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61   || NEVER(db->ma
386f8 6c 6c 6f 63 46 61 69 6c 65 64 29 0d 0a 20 20 29  llocFailed)..  )
386f9 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  {..    rc = SQLI
386fa 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 67  TE_NOMEM;..    g
386fb 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
386fc 6d 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20  m;..  }..  rc = 
386fd 65 78 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72  execSql(db, pzEr
386fe 72 4d 73 67 2c 20 22 50 52 41 47 4d 41 20 76 61  rMsg, "PRAGMA va
386ff 63 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e  cuum_db.synchron
38700 6f 75 73 3d 4f 46 46 22 29 3b 0d 0a 20 20 69 66  ous=OFF");..  if
38701 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
38702 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64  ){..    goto end
38703 5f 6f 66 5f 76 61 63 75 75 6d 3b 0d 0a 20 20 7d  _of_vacuum;..  }
38704 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
38705 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
38706 55 4d 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  UM..  sqlite3Btr
38707 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
38708 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 41  pTemp, db->nextA
38709 75 74 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d 3e  utovac>=0 ? db->
3870a 6e 65 78 74 41 75 74 6f 76 61 63 20 3a 0d 0a 20  nextAutovac :.. 
3870b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3870c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3870d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3870e 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
3870f 75 75 6d 28 70 4d 61 69 6e 29 29 3b 0d 0a 23 65  uum(pMain));..#e
38710 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 42 65 67  ndif....  /* Beg
38711 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
38712 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 65 78 65 63   */..  rc = exec
38713 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67  Sql(db, pzErrMsg
38714 2c 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53 49  , "BEGIN EXCLUSI
38715 56 45 3b 22 29 3b 0d 0a 20 20 69 66 28 20 72 63  VE;");..  if( rc
38716 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
38717 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
38718 3b 0d 0a 0d 0a 20 20 2f 2a 20 51 75 65 72 79 20  ;....  /* Query 
38719 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68  the schema of th
3871a 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
3871b 20 43 72 65 61 74 65 20 61 20 6d 69 72 72 6f 72   Create a mirror
3871c 20 73 63 68 65 6d 61 0d 0a 20 20 2a 2a 20 69 6e   schema..  ** in
3871d 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64   the temporary d
3871e 61 74 61 62 61 73 65 2e 0d 0a 20 20 2a 2f 0d 0a  atabase...  */..
3871f 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53    rc = execExecS
38720 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c  ql(db, pzErrMsg,
38721 0d 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20  ..      "SELECT 
38722 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 76 61  'CREATE TABLE va
38723 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62  cuum_db.' || sub
38724 73 74 72 28 73 71 6c 2c 31 34 29 20 22 0d 0a 20  str(sql,14) ".. 
38725 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c       "  FROM sql
38726 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
38727 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e   type='table' AN
38728 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
38729 73 65 71 75 65 6e 63 65 27 22 0d 0a 20 20 20 20  sequence'"..    
3872a 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61    "   AND rootpa
3872b 67 65 3e 30 22 0d 0a 20 20 29 3b 0d 0a 20 20 69  ge>0"..  );..  i
3872c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3872d 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
3872e 61 63 75 75 6d 3b 0d 0a 20 20 72 63 20 3d 20 65  acuum;..  rc = e
3872f 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 70  xecExecSql(db, p
38730 7a 45 72 72 4d 73 67 2c 0d 0a 20 20 20 20 20 20  zErrMsg,..      
38731 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
38732 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
38733 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
38734 31 34 29 22 0d 0a 20 20 20 20 20 20 22 20 20 46  14)"..      "  F
38735 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
38736 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45  r WHERE sql LIKE
38737 20 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 25   'CREATE INDEX %
38738 27 20 22 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  ' ");..  if( rc!
38739 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
3873a 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
3873b 0d 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ..  rc = execExe
3873c 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73  cSql(db, pzErrMs
3873d 67 2c 0d 0a 20 20 20 20 20 20 22 53 45 4c 45 43  g,..      "SELEC
3873e 54 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45  T 'CREATE UNIQUE
3873f 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62   INDEX vacuum_db
38740 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  .' || substr(sql
38741 2c 32 31 29 20 22 0d 0a 20 20 20 20 20 20 22 20  ,21) "..      " 
38742 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
38743 74 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49  ter WHERE sql LI
38744 4b 45 20 27 43 52 45 41 54 45 20 55 4e 49 51 55  KE 'CREATE UNIQU
38745 45 20 49 4e 44 45 58 20 25 27 22 29 3b 0d 0a 20  E INDEX %'");.. 
38746 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
38747 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
38748 5f 76 61 63 75 75 6d 3b 0d 0a 0d 0a 20 20 2f 2a  _vacuum;....  /*
38749 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
3874a 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  e tables in the 
3874b 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 46  main database. F
3874c 6f 72 20 65 61 63 68 2c 20 64 6f 0d 0a 20 20 2a  or each, do..  *
3874d 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54  * an "INSERT INT
3874e 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78 78 20  O vacuum_db.xxx 
3874f 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61  SELECT * FROM ma
38750 69 6e 2e 78 78 78 3b 22 20 74 6f 20 63 6f 70 79  in.xxx;" to copy
38751 0d 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ..  ** the conte
38752 6e 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f  nts to the tempo
38753 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0d 0a  rary database...
38754 20 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 65 78 65    */..  rc = exe
38755 63 45 78 65 63 53 71 6c 28 64 62 2c 20 70 7a 45  cExecSql(db, pzE
38756 72 72 4d 73 67 2c 0d 0a 20 20 20 20 20 20 22 53  rrMsg,..      "S
38757 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e  ELECT 'INSERT IN
38758 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c  TO vacuum_db.' |
38759 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0d  | quote(name) ".
3875a 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c  .      "|| ' SEL
3875b 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e  ECT * FROM main.
3875c 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
3875d 20 7c 7c 20 27 3b 27 22 0d 0a 20 20 20 20 20 20   || ';'"..      
3875e 22 46 52 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69 74  "FROM main.sqlit
3875f 65 5f 6d 61 73 74 65 72 20 22 0d 0a 20 20 20 20  e_master "..    
38760 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d 20    "WHERE type = 
38761 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65  'table' AND name
38762 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  !='sqlite_sequen
38763 63 65 27 20 22 0d 0a 20 20 20 20 20 20 22 20 20  ce' "..      "  
38764 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0d  AND rootpage>0".
38765 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  .  );..  if( rc!
38766 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
38767 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
38768 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76  ....  /* Copy ov
38769 65 72 20 74 68 65 20 73 65 71 75 65 6e 63 65 20  er the sequence 
3876a 74 61 62 6c 65 0d 0a 20 20 2a 2f 0d 0a 20 20 72  table..  */..  r
3876b 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
3876c 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c 0d 0a 20  db, pzErrMsg,.. 
3876d 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 44 45       "SELECT 'DE
3876e 4c 45 54 45 20 46 52 4f 4d 20 76 61 63 75 75 6d  LETE FROM vacuum
3876f 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  _db.' || quote(n
38770 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0d 0a 20  ame) || ';' ".. 
38771 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75       "FROM vacuu
38772 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74  m_db.sqlite_mast
38773 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 27 73  er WHERE name='s
38774 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20  qlite_sequence' 
38775 22 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20 72  "..  );..  if( r
38776 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
38777 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
38778 6d 3b 0d 0a 20 20 72 63 20 3d 20 65 78 65 63 45  m;..  rc = execE
38779 78 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72  xecSql(db, pzErr
3877a 4d 73 67 2c 0d 0a 20 20 20 20 20 20 22 53 45 4c  Msg,..      "SEL
3877b 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f  ECT 'INSERT INTO
3877c 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
3877d 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0d 0a 20  quote(name) ".. 
3877e 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43       "|| ' SELEC
3877f 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 27 20  T * FROM main.' 
38780 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c  || quote(name) |
38781 7c 20 27 3b 27 20 22 0d 0a 20 20 20 20 20 20 22  | ';' "..      "
38782 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73  FROM vacuum_db.s
38783 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
38784 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74 65  RE name=='sqlite
38785 5f 73 65 71 75 65 6e 63 65 27 3b 22 0d 0a 20 20  _sequence';"..  
38786 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
38787 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
38788 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0d 0a 0d  nd_of_vacuum;...
38789 0a 0d 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  ...  /* Copy the
3878a 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73   triggers, views
3878b 2c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74 61  , and virtual ta
3878c 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6d 61  bles from the ma
3878d 69 6e 20 64 61 74 61 62 61 73 65 0d 0a 20 20 2a  in database..  *
3878e 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 74 65  * over to the te
3878f 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
38790 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 73 65  .  None of these
38791 20 6f 62 6a 65 63 74 73 20 68 61 73 20 61 6e 79   objects has any
38792 0d 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65  ..  ** associate
38793 64 20 73 74 6f 72 61 67 65 2c 20 73 6f 20 61 6c  d storage, so al
38794 6c 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20  l we have to do 
38795 69 73 20 63 6f 70 79 20 74 68 65 69 72 20 65 6e  is copy their en
38796 74 72 69 65 73 0d 0a 20 20 2a 2a 20 66 72 6f 6d  tries..  ** from
38797 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
38798 45 52 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d  ER table...  */.
38799 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28  .  rc = execSql(
3879a 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c 0d 0a 20  db, pzErrMsg,.. 
3879b 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
3879c 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69  O vacuum_db.sqli
3879d 74 65 5f 6d 61 73 74 65 72 20 22 0d 0a 20 20 20  te_master "..   
3879e 20 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70     "  SELECT typ
3879f 65 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d  e, name, tbl_nam
387a0 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
387a1 22 0d 0a 20 20 20 20 20 20 22 20 20 20 20 46 52  "..      "    FR
387a2 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65 5f 6d  OM main.sqlite_m
387a3 61 73 74 65 72 22 0d 0a 20 20 20 20 20 20 22 20  aster"..      " 
387a4 20 20 57 48 45 52 45 20 74 79 70 65 3d 27 76 69    WHERE type='vi
387a5 65 77 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69  ew' OR type='tri
387a6 67 67 65 72 27 22 0d 0a 20 20 20 20 20 20 22 20  gger'"..      " 
387a7 20 20 20 20 20 4f 52 20 28 74 79 70 65 3d 27 74       OR (type='t
387a8 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f 74 70 61  able' AND rootpa
387a9 67 65 3d 30 29 22 0d 0a 20 20 29 3b 0d 0a 20 20  ge=0)"..  );..  
387aa 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
387ab 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0d 0a 0d 0a  d_of_vacuum;....
387ac 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
387ad 6e 74 2c 20 74 68 65 72 65 20 69 73 20 61 20 77  nt, there is a w
387ae 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
387af 20 6f 70 65 6e 20 6f 6e 20 62 6f 74 68 20 74 68   open on both th
387b0 65 20 0d 0a 20 20 2a 2a 20 76 61 63 75 75 6d 20  e ..  ** vacuum 
387b1 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65  database and the
387b2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
387b3 41 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f  Assuming no erro
387b4 72 20 6f 63 63 75 72 73 2c 0d 0a 20 20 2a 2a 20  r occurs,..  ** 
387b5 62 6f 74 68 20 74 72 61 6e 73 61 63 74 69 6f 6e  both transaction
387b6 73 20 61 72 65 20 63 6c 6f 73 65 64 20 62 79 20  s are closed by 
387b7 74 68 69 73 20 62 6c 6f 63 6b 20 2d 20 74 68 65  this block - the
387b8 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0d 0a   main database..
387b9 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
387ba 20 62 79 20 73 71 6c 69 74 65 33 42 74 72 65 65   by sqlite3Btree
387bb 43 6f 70 79 46 69 6c 65 28 29 20 61 6e 64 20 74  CopyFile() and t
387bc 68 65 20 6f 74 68 65 72 20 62 79 20 61 6e 20 65  he other by an e
387bd 78 70 6c 69 63 69 74 0d 0a 20 20 2a 2a 20 63 61  xplicit..  ** ca
387be 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ll to sqlite3Btr
387bf 65 65 43 6f 6d 6d 69 74 28 29 2e 0d 0a 20 20 2a  eeCommit()...  *
387c0 2f 0d 0a 20 20 7b 0d 0a 20 20 20 20 75 33 32 20  /..  {..    u32 
387c1 6d 65 74 61 3b 0d 0a 20 20 20 20 69 6e 74 20 69  meta;..    int i
387c2 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 69 73  ;....    /* This
387c3 20 61 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65   array determine
387c4 73 20 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74  s which meta met
387c5 61 20 76 61 6c 75 65 73 20 61 72 65 20 70 72 65  a values are pre
387c6 73 65 72 76 65 64 20 69 6e 20 74 68 65 0d 0a 20  served in the.. 
387c7 20 20 20 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45     ** vacuum.  E
387c8 76 65 6e 20 65 6e 74 72 69 65 73 20 61 72 65 20  ven entries are 
387c9 74 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6e  the meta value n
387ca 75 6d 62 65 72 20 61 6e 64 20 6f 64 64 20 65 6e  umber and odd en
387cb 74 72 69 65 73 0d 0a 20 20 20 20 2a 2a 20 61 72  tries..    ** ar
387cc 65 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74  e an increment t
387cd 6f 20 61 70 70 6c 79 20 74 6f 20 74 68 65 20 6d  o apply to the m
387ce 65 74 61 20 76 61 6c 75 65 20 61 66 74 65 72 20  eta value after 
387cf 74 68 65 20 76 61 63 75 75 6d 2e 0d 0a 20 20 20  the vacuum...   
387d0 20 2a 2a 20 54 68 65 20 69 6e 63 72 65 6d 65 6e   ** The incremen
387d1 74 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 63  t is used to inc
387d2 72 65 61 73 65 20 74 68 65 20 73 63 68 65 6d 61  rease the schema
387d3 20 63 6f 6f 6b 69 65 20 73 6f 20 74 68 61 74 20   cookie so that 
387d4 6f 74 68 65 72 0d 0a 20 20 20 20 2a 2a 20 63 6f  other..    ** co
387d5 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65  nnections to the
387d6 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 77   same database w
387d7 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20 72 65 72 65  ill know to rere
387d8 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0d 0a  ad the schema...
387d9 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 74 61 74      */..    stat
387da 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
387db 64 20 63 68 61 72 20 61 43 6f 70 79 5b 5d 20 3d  d char aCopy[] =
387dc 20 7b 0d 0a 20 20 20 20 20 20 20 42 54 52 45 45   {..       BTREE
387dd 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c  _SCHEMA_VERSION,
387de 20 20 20 20 20 31 2c 20 20 2f 2a 20 41 64 64 20       1,  /* Add 
387df 6f 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73  one to the old s
387e0 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0d  chema cookie */.
387e1 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 44 45  .       BTREE_DE
387e2 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
387e3 2c 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76  , 0,  /* Preserv
387e4 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  e the default pa
387e5 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  ge cache size */
387e6 0d 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 54  ..       BTREE_T
387e7 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 20 20  EXT_ENCODING,   
387e8 20 20 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72     0,  /* Preser
387e9 76 65 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ve the text enco
387ea 64 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 20 20 20  ding */..       
387eb 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49  BTREE_USER_VERSI
387ec 4f 4e 2c 20 20 20 20 20 20 20 30 2c 20 20 2f 2a  ON,       0,  /*
387ed 20 50 72 65 73 65 72 76 65 20 74 68 65 20 75 73   Preserve the us
387ee 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0d 0a 20  er version */.. 
387ef 20 20 20 7d 3b 0d 0a 0d 0a 20 20 20 20 61 73 73     };....    ass
387f0 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
387f1 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54  treeIsInTrans(pT
387f2 65 6d 70 29 20 29 3b 0d 0a 20 20 20 20 61 73 73  emp) );..    ass
387f3 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
387f4 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d  treeIsInTrans(pM
387f5 61 69 6e 29 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f  ain) );....    /
387f6 2a 20 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74  * Copy Btree met
387f7 61 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 20 20 20  a values */..   
387f8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
387f9 79 53 69 7a 65 28 61 43 6f 70 79 29 3b 20 69 2b  ySize(aCopy); i+
387fa 3d 32 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 47  =2){..      /* G
387fb 65 74 4d 65 74 61 28 29 20 61 6e 64 20 55 70 64  etMeta() and Upd
387fc 61 74 65 4d 65 74 61 28 29 20 63 61 6e 6e 6f 74  ateMeta() cannot
387fd 20 66 61 69 6c 20 69 6e 20 74 68 69 73 20 63 6f   fail in this co
387fe 6e 74 65 78 74 20 62 65 63 61 75 73 65 0d 0a 20  ntext because.. 
387ff 20 20 20 20 20 2a 2a 20 77 65 20 61 6c 72 65 61       ** we alrea
38800 64 79 20 68 61 76 65 20 70 61 67 65 20 31 20 6c  dy have page 1 l
38801 6f 61 64 65 64 20 69 6e 74 6f 20 63 61 63 68 65  oaded into cache
38802 20 61 6e 64 20 6d 61 72 6b 65 64 20 64 69 72 74   and marked dirt
38803 79 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c  y. */..      sql
38804 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
38805 28 70 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d  (pMain, aCopy[i]
38806 2c 20 26 6d 65 74 61 29 3b 0d 0a 20 20 20 20 20  , &meta);..     
38807 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
38808 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 54 65  eeUpdateMeta(pTe
38809 6d 70 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 6d 65  mp, aCopy[i], me
3880a 74 61 2b 61 43 6f 70 79 5b 69 2b 31 5d 29 3b 0d  ta+aCopy[i+1]);.
3880b 0a 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 52  .      if( NEVER
3880c 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
3880d 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
3880e 63 75 75 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  cuum;..    }....
3880f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
38810 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
38811 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0d 0a 20 20  ain, pTemp);..  
38812 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
38813 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
38814 66 5f 76 61 63 75 75 6d 3b 0d 0a 20 20 20 20 72  f_vacuum;..    r
38815 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
38816 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0d 0a  Commit(pTemp);..
38817 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
38818 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
38819 5f 6f 66 5f 76 61 63 75 75 6d 3b 0d 0a 23 69 66  _of_vacuum;..#if
3881a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3881b 5f 41 55 54 4f 56 41 43 55 55 4d 0d 0a 20 20 20  _AUTOVACUUM..   
3881c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
3881d 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e  AutoVacuum(pMain
3881e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  , sqlite3BtreeGe
3881f 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d  tAutoVacuum(pTem
38820 70 29 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  p));..#endif..  
38821 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 72  }....  assert( r
38822 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d  c==SQLITE_OK );.
38823 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
38824 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
38825 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65 33 42 74  pMain, sqlite3Bt
38826 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
38827 54 65 6d 70 29 2c 20 6e 52 65 73 2c 31 29 3b 0d  Temp), nRes,1);.
38828 0a 0d 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  ...end_of_vacuum
38829 3a 0d 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20  :..  /* Restore 
3882a 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c  the original val
3882b 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20  ue of db->flags 
3882c 2a 2f 0d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  */..  db->flags 
3882d 3d 20 73 61 76 65 64 5f 66 6c 61 67 73 3b 0d 0a  = saved_flags;..
3882e 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20    db->nChange = 
3882f 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0d 0a  saved_nChange;..
38830 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e    db->nTotalChan
38831 67 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61  ge = saved_nTota
38832 6c 43 68 61 6e 67 65 3b 0d 0a 20 20 64 62 2d 3e  lChange;..  db->
38833 78 54 72 61 63 65 20 3d 20 73 61 76 65 64 5f 78  xTrace = saved_x
38834 54 72 61 63 65 3b 0d 0a 20 20 73 71 6c 69 74 65  Trace;..  sqlite
38835 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a  3BtreeSetPageSiz
38836 65 28 70 4d 61 69 6e 2c 20 2d 31 2c 20 2d 31 2c  e(pMain, -1, -1,
38837 20 31 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 75 72   1);....  /* Cur
38838 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69 73 20  rently there is 
38839 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74 72 61  an SQL level tra
3883a 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e  nsaction open on
3883b 20 74 68 65 20 76 61 63 75 75 6d 0d 0a 20 20 2a   the vacuum..  *
3883c 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20 6c  * database. No l
3883d 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e  ocks are held on
3883e 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 73   any other files
3883f 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69 6e   (since the main
38840 20 66 69 6c 65 0d 0a 20 20 2a 2a 20 77 61 73 20   file..  ** was 
38841 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65  committed at the
38842 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53   btree level). S
38843 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64  o it safe to end
38844 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
38845 0d 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c  ..  ** by manual
38846 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 61  ly setting the a
38847 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74  utoCommit flag t
38848 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74 61 63  o true and detac
38849 68 69 6e 67 20 74 68 65 0d 0a 20 20 2a 2a 20 76  hing the..  ** v
3884a 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20  acuum database. 
3884b 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f  The vacuum_db jo
3884c 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65  urnal file is de
3884d 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  leted when the p
3884e 61 67 65 72 0d 0a 20 20 2a 2a 20 69 73 20 63 6c  ager..  ** is cl
3884f 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41  osed by the DETA
38850 43 48 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 64 62 2d  CH...  */..  db-
38851 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
38852 0d 0a 0d 0a 20 20 69 66 28 20 70 44 62 20 29 7b  ....  if( pDb ){
38853 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ..    sqlite3Btr
38854 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74  eeClose(pDb->pBt
38855 29 3b 0d 0a 20 20 20 20 70 44 62 2d 3e 70 42 74  );..    pDb->pBt
38856 20 3d 20 30 3b 0d 0a 20 20 20 20 70 44 62 2d 3e   = 0;..    pDb->
38857 70 53 63 68 65 6d 61 20 3d 20 30 3b 0d 0a 20 20  pSchema = 0;..  
38858 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73 20 62  }....  /* This b
38859 6f 74 68 20 63 6c 65 61 72 73 20 74 68 65 20 73  oth clears the s
3885a 63 68 65 6d 61 73 20 61 6e 64 20 72 65 64 75 63  chemas and reduc
3885b 65 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  es the size of t
3885c 68 65 20 64 62 2d 3e 61 44 62 5b 5d 0d 0a 20 20  he db->aDb[]..  
3885d 2a 2a 20 61 72 72 61 79 2e 20 2a 2f 20 0d 0a 20  ** array. */ .. 
3885e 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
3885f 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
38860 2d 31 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  -1);....  return
38861 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69   rc;..}....#endi
38862 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  f  /* SQLITE_OMI
38863 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51 4c 49  T_VACUUM && SQLI
38864 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a  TE_OMIT_ATTACH *
38865 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /..../**********
38866 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75  **** End of vacu
38867 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
38868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38869 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3886a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
3886b 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3886c 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vtab.c ********
3886d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3886e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3886f 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
38870 30 36 20 4a 75 6e 65 20 31 30 0d 0a 2a 2a 0d 0a  06 June 10..**..
38871 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
38872 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
38873 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
38874 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
38875 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
38876 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
38877 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
38878 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
38879 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
3887a 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
3887b 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
3887c 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
3887d 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
3887e 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
3887f 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
38880 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
38881 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
38882 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
38883 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38884 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38885 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38886 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38887 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  **..** This file
38888 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
38889 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c  sed to help impl
3888a 65 6d 65 6e 74 20 76 69 72 74 75 61 6c 20 74 61  ement virtual ta
3888b 62 6c 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  bles...*/..#ifnd
3888c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
3888d 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 0d 0a 2f  IRTUALTABLE..../
3888e 2a 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 61 20 76  *..** Before a v
3888f 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 43 72  irtual table xCr
38890 65 61 74 65 28 29 20 6f 72 20 78 43 6f 6e 6e 65  eate() or xConne
38891 63 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 69  ct() method is i
38892 6e 76 6f 6b 65 64 2c 20 74 68 65 0d 0a 2a 2a 20  nvoked, the..** 
38893 73 71 6c 69 74 65 33 2e 70 56 74 61 62 43 74 78  sqlite3.pVtabCtx
38894 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
38895 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
38896 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
38897 6f 66 0d 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  of..** this stru
38898 63 74 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  ct allocated on 
38899 74 68 65 20 73 74 61 63 6b 2e 20 49 74 20 69 73  the stack. It is
3889a 20 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70   used by the imp
3889b 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 0d  lementation of .
3889c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f  .** the sqlite3_
3889d 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 61  declare_vtab() a
3889e 6e 64 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  nd sqlite3_vtab_
3889f 63 6f 6e 66 69 67 28 29 20 41 50 49 73 2c 20 62  config() APIs, b
388a0 6f 74 68 20 6f 66 20 77 68 69 63 68 0d 0a 2a 2a  oth of which..**
388a1 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c   are invoked onl
388a2 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 43  y from within xC
388a3 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65  reate and xConne
388a4 63 74 20 6d 65 74 68 6f 64 73 2e 0d 0a 2a 2f 0d  ct methods...*/.
388a5 0a 73 74 72 75 63 74 20 56 74 61 62 43 74 78 20  .struct VtabCtx 
388a6 7b 0d 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  {..  Table *pTab
388a7 3b 0d 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54  ;..  VTable *pVT
388a8 61 62 6c 65 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d  able;..};..../*.
388a9 0a 2a 2a 20 54 68 65 20 61 63 74 75 61 6c 20 66  .** The actual f
388aa 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  unction that doe
388ab 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 63 72  s the work of cr
388ac 65 61 74 69 6e 67 20 61 20 6e 65 77 20 6d 6f 64  eating a new mod
388ad 75 6c 65 2e 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ule...** This fu
388ae 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
388af 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72  s the sqlite3_cr
388b0 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 61 6e  eate_module() an
388b1 64 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72  d..** sqlite3_cr
388b2 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29  eate_module_v2()
388b3 20 69 6e 74 65 72 66 61 63 65 73 2e 0d 0a 2a 2f   interfaces...*/
388b4 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65  ..static int cre
388b5 61 74 65 4d 6f 64 75 6c 65 28 0d 0a 20 20 73 71  ateModule(..  sq
388b6 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
388b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
388b8 20 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69   Database in whi
388b9 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67  ch module is reg
388ba 69 73 74 65 72 65 64 20 2a 2f 0d 0a 20 20 63 6f  istered */..  co
388bb 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
388bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
388bd 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   Name assigned t
388be 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f  o this module */
388bf 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ..  const sqlite
388c0 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
388c1 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e  e,  /* The defin
388c2 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ition of the mod
388c3 75 6c 65 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a  ule */..  void *
388c4 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20  pAux,           
388c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
388c6 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72  text pointer for
388c7 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
388c8 74 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a 78  t */..  void (*x
388c9 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29  Destroy)(void *)
388ca 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
388cb 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
388cc 63 74 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ction */..){..  
388cd 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b 0d 0a  int rc, nName;..
388ce 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0d    Module *pMod;.
388cf 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ...  sqlite3_mut
388d0 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
388d1 65 78 29 3b 0d 0a 20 20 6e 4e 61 6d 65 20 3d 20  ex);..  nName = 
388d2 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
388d3 7a 4e 61 6d 65 29 3b 0d 0a 20 20 70 4d 6f 64 20  zName);..  pMod 
388d4 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
388d5 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
388d6 62 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65  b, sizeof(Module
388d7 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0d  ) + nName + 1);.
388d8 0a 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0d 0a  .  if( pMod ){..
388d9 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 44 65 6c      Module *pDel
388da 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f  ;..    char *zCo
388db 70 79 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  py = (char *)(&p
388dc 4d 6f 64 5b 31 5d 29 3b 0d 0a 20 20 20 20 6d 65  Mod[1]);..    me
388dd 6d 63 70 79 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d  mcpy(zCopy, zNam
388de 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0d 0a 20 20  e, nName+1);..  
388df 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 20 3d 20    pMod->zName = 
388e0 7a 43 6f 70 79 3b 0d 0a 20 20 20 20 70 4d 6f 64  zCopy;..    pMod
388e1 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
388e2 75 6c 65 3b 0d 0a 20 20 20 20 70 4d 6f 64 2d 3e  ule;..    pMod->
388e3 70 41 75 78 20 3d 20 70 41 75 78 3b 0d 0a 20 20  pAux = pAux;..  
388e4 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79    pMod->xDestroy
388e5 20 3d 20 78 44 65 73 74 72 6f 79 3b 0d 0a 20 20   = xDestroy;..  
388e6 20 20 70 44 65 6c 20 3d 20 28 4d 6f 64 75 6c 65    pDel = (Module
388e7 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e   *)sqlite3HashIn
388e8 73 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  sert(&db->aModul
388e9 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65 2c  e, zCopy, nName,
388ea 20 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b 0d 0a   (void*)pMod);..
388eb 20 20 20 20 69 66 28 20 70 44 65 6c 20 26 26 20      if( pDel && 
388ec 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 20 29  pDel->xDestroy )
388ed 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
388ee 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
388ef 65 6d 61 28 64 62 2c 20 2d 31 29 3b 0d 0a 20 20  ema(db, -1);..  
388f0 20 20 20 20 70 44 65 6c 2d 3e 78 44 65 73 74 72      pDel->xDestr
388f1 6f 79 28 70 44 65 6c 2d 3e 70 41 75 78 29 3b 0d  oy(pDel->pAux);.
388f2 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
388f3 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44  te3DbFree(db, pD
388f4 65 6c 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 44  el);..    if( pD
388f5 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0d 0a 20 20 20  el==pMod ){..   
388f6 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
388f7 6c 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  led = 1;..    }.
388f8 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65  .  }else if( xDe
388f9 73 74 72 6f 79 20 29 7b 0d 0a 20 20 20 20 78 44  stroy ){..    xD
388fa 65 73 74 72 6f 79 28 70 41 75 78 29 3b 0d 0a 20  estroy(pAux);.. 
388fb 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
388fc 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 53 51  e3ApiExit(db, SQ
388fd 4c 49 54 45 5f 4f 4b 29 3b 0d 0a 20 20 73 71 6c  LITE_OK);..  sql
388fe 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
388ff 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
38900 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
38901 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 74 65 72 6e  .../*..** Extern
38902 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  al API function 
38903 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  used to create a
38904 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62   new virtual-tab
38905 6c 65 20 6d 6f 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a  le module...*/..
38906 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
38907 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
38908 64 75 6c 65 28 0d 0a 20 20 73 71 6c 69 74 65 33  dule(..  sqlite3
38909 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
3890a 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
3890b 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f  base in which mo
3890c 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72  dule is register
3890d 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  ed */..  const c
3890e 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
3890f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
38910 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69   assigned to thi
38911 73 20 6d 6f 64 75 6c 65 20 2a 2f 0d 0a 20 20 63  s module */..  c
38912 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
38913 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f  ule *pModule,  /
38914 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  * The definition
38915 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   of the module *
38916 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 20  /..  void *pAux 
38917 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38918 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
38919 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65  pointer for xCre
3891a 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0d  ate/xConnect */.
3891b 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 63 72  .){..  return cr
3891c 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a  eateModule(db, z
3891d 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70  Name, pModule, p
3891e 41 75 78 2c 20 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  Aux, 0);..}..../
3891f 2a 0d 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41  *..** External A
38920 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  PI function used
38921 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
38922 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
38923 6f 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  odule...*/..SQLI
38924 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
38925 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
38926 5f 76 32 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  _v2(..  sqlite3 
38927 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
38928 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
38929 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64  ase in which mod
3892a 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ule is registere
3892b 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  d */..  const ch
3892c 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
3892d 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
3892e 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73  assigned to this
3892f 20 6d 6f 64 75 6c 65 20 2a 2f 0d 0a 20 20 63 6f   module */..  co
38930 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
38931 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a  le *pModule,  /*
38932 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20   The definition 
38933 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
38934 0d 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  ..  void *pAux, 
38935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38936 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70      /* Context p
38937 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61  ointer for xCrea
38938 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0d 0a  te/xConnect */..
38939 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f    void (*xDestro
3893a 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20  y)(void *)      
3893b 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
3893c 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
3893d 2a 2f 0d 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e  */..){..  return
3893e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62   createModule(db
3893f 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65  , zName, pModule
38940 2c 20 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79  , pAux, xDestroy
38941 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
38942 4c 6f 63 6b 20 74 68 65 20 76 69 72 74 75 61 6c  Lock the virtual
38943 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69   table so that i
38944 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 69 73 63  t cannot be disc
38945 6f 6e 6e 65 63 74 65 64 2e 0d 0a 2a 2a 20 4c 6f  onnected...** Lo
38946 63 6b 73 20 6e 65 73 74 2e 20 20 45 76 65 72 79  cks nest.  Every
38947 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 68 61 76   lock should hav
38948 65 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  e a correspondin
38949 67 20 75 6e 6c 6f 63 6b 2e 0d 0a 2a 2a 20 49 66  g unlock...** If
3894a 20 61 6e 20 75 6e 6c 6f 63 6b 20 69 73 20 6f 6d   an unlock is om
3894b 69 74 74 65 64 2c 20 72 65 73 6f 75 72 63 65 73  itted, resources
3894c 20 6c 65 61 6b 73 20 77 69 6c 6c 20 6f 63 63 75   leaks will occu
3894d 72 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  r.  ..**..** If 
3894e 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73 20  a disconnect is 
3894f 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c 65 20  attempted while 
38950 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
38951 69 73 20 6c 6f 63 6b 65 64 2c 0d 0a 2a 2a 20 74  is locked,..** t
38952 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73  he disconnect is
38953 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
38954 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20 62  all locks have b
38955 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0d 0a 2a 2f  een removed...*/
38956 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
38957 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
38958 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70 56  bLock(VTable *pV
38959 54 61 62 29 7b 0d 0a 20 20 70 56 54 61 62 2d 3e  Tab){..  pVTab->
3895a 6e 52 65 66 2b 2b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a  nRef++;..}......
3895b 2f 2a 0d 0a 2a 2a 20 70 54 61 62 20 69 73 20 61  /*..** pTab is a
3895c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61   pointer to a Ta
3895d 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 72 65  ble structure re
3895e 70 72 65 73 65 6e 74 69 6e 67 20 61 20 76 69 72  presenting a vir
3895f 74 75 61 6c 2d 74 61 62 6c 65 2e 0d 0a 2a 2a 20  tual-table...** 
38960 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
38961 20 74 6f 20 74 68 65 20 56 54 61 62 6c 65 20 6f   to the VTable o
38962 62 6a 65 63 74 20 75 73 65 64 20 62 79 20 63 6f  bject used by co
38963 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 61  nnection db to a
38964 63 63 65 73 73 20 0d 0a 2a 2a 20 74 68 69 73 20  ccess ..** this 
38965 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c 20 69  virtual-table, i
38966 66 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20 63  f one has been c
38967 72 65 61 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20  reated, or NULL 
38968 6f 74 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a  otherwise...*/..
38969 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
3896a 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 47 65  Table *sqlite3Ge
3896b 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  tVTable(sqlite3 
3896c 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  *db, Table *pTab
3896d 29 7b 0d 0a 20 20 56 54 61 62 6c 65 20 2a 70 56  ){..  VTable *pV
3896e 74 61 62 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  tab;..  assert( 
3896f 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
38970 29 3b 0d 0a 20 20 66 6f 72 28 70 56 74 61 62 3d  );..  for(pVtab=
38971 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b 20 70  pTab->pVTable; p
38972 56 74 61 62 20 26 26 20 70 56 74 61 62 2d 3e 64  Vtab && pVtab->d
38973 62 21 3d 64 62 3b 20 70 56 74 61 62 3d 70 56 74  b!=db; pVtab=pVt
38974 61 62 2d 3e 70 4e 65 78 74 29 3b 0d 0a 20 20 72  ab->pNext);..  r
38975 65 74 75 72 6e 20 70 56 74 61 62 3b 0d 0a 7d 0d  eturn pVtab;..}.
38976 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 63 72 65 6d  .../*..** Decrem
38977 65 6e 74 20 74 68 65 20 72 65 66 2d 63 6f 75 6e  ent the ref-coun
38978 74 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74  t on a virtual t
38979 61 62 6c 65 20 6f 62 6a 65 63 74 2e 20 57 68 65  able object. Whe
3897a 6e 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 0d  n the ref-count.
3897b 0a 2a 2a 20 72 65 61 63 68 65 73 20 7a 65 72 6f  .** reaches zero
3897c 2c 20 63 61 6c 6c 20 74 68 65 20 78 44 69 73 63  , call the xDisc
3897d 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20  onnect() method 
3897e 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6f 62  to delete the ob
3897f 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ject...*/..SQLIT
38980 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
38981 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
38982 28 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b  (VTable *pVTab){
38983 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
38984 3d 20 70 56 54 61 62 2d 3e 64 62 3b 0d 0a 0d 0a  = pVTab->db;....
38985 20 20 61 73 73 65 72 74 28 20 64 62 20 29 3b 0d    assert( db );.
38986 0a 20 20 61 73 73 65 72 74 28 20 70 56 54 61 62  .  assert( pVTab
38987 2d 3e 6e 52 65 66 3e 30 20 29 3b 0d 0a 20 20 61  ->nRef>0 );..  a
38988 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 61  ssert( sqlite3Sa
38989 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20  fetyCheckOk(db) 
3898a 29 3b 0d 0a 0d 0a 20 20 70 56 54 61 62 2d 3e 6e  );....  pVTab->n
3898b 52 65 66 2d 2d 3b 0d 0a 20 20 69 66 28 20 70 56  Ref--;..  if( pV
3898c 54 61 62 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0d  Tab->nRef==0 ){.
3898d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
3898e 62 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70 56  b *p = pVTab->pV
3898f 74 61 62 3b 0d 0a 20 20 20 20 69 66 28 20 70 20  tab;..    if( p 
38990 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 70 4d 6f  ){..      p->pMo
38991 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63  dule->xDisconnec
38992 74 28 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  t(p);..    }..  
38993 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
38994 64 62 2c 20 70 56 54 61 62 29 3b 0d 0a 20 20 7d  db, pVTab);..  }
38995 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 61  ..}..../*..** Ta
38996 62 6c 65 20 70 20 69 73 20 61 20 76 69 72 74 75  ble p is a virtu
38997 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  al table. This f
38998 75 6e 63 74 69 6f 6e 20 6d 6f 76 65 73 20 61 6c  unction moves al
38999 6c 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  l elements in th
3899a 65 0d 0a 2a 2a 20 70 2d 3e 70 56 54 61 62 6c 65  e..** p->pVTable
3899b 20 6c 69 73 74 20 74 6f 20 74 68 65 20 73 71 6c   list to the sql
3899c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
3899d 20 6c 69 73 74 73 20 6f 66 20 74 68 65 69 72 20   lists of their 
3899e 61 73 73 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 64  associated..** d
3899f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
389a0 6f 6e 73 20 74 6f 20 62 65 20 64 69 73 63 6f 6e  ons to be discon
389a1 6e 65 63 74 65 64 20 61 74 20 74 68 65 20 6e 65  nected at the ne
389a2 78 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20  xt opportunity. 
389a3 0d 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20  ..** Except, if 
389a4 61 72 67 75 6d 65 6e 74 20 64 62 20 69 73 20 6e  argument db is n
389a5 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  ot NULL, then th
389a6 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74  e entry associat
389a7 65 64 20 77 69 74 68 0d 0a 2a 2a 20 63 6f 6e 6e  ed with..** conn
389a8 65 63 74 69 6f 6e 20 64 62 20 69 73 20 6c 65 66  ection db is lef
389a9 74 20 69 6e 20 74 68 65 20 70 2d 3e 70 56 54 61  t in the p->pVTa
389aa 62 6c 65 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73  ble list...*/..s
389ab 74 61 74 69 63 20 56 54 61 62 6c 65 20 2a 76 74  tatic VTable *vt
389ac 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28  abDisconnectAll(
389ad 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62  sqlite3 *db, Tab
389ae 6c 65 20 2a 70 29 7b 0d 0a 20 20 56 54 61 62 6c  le *p){..  VTabl
389af 65 20 2a 70 52 65 74 20 3d 20 30 3b 0d 0a 20 20  e *pRet = 0;..  
389b0 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 20  VTable *pVTable 
389b1 3d 20 70 2d 3e 70 56 54 61 62 6c 65 3b 0d 0a 20  = p->pVTable;.. 
389b2 20 70 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30 3b   p->pVTable = 0;
389b3 0d 0a 0d 0a 20 20 2f 2a 20 41 73 73 65 72 74 20  ....  /* Assert 
389b4 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 28  that the mutex (
389b5 69 66 20 61 6e 79 29 20 61 73 73 6f 63 69 61 74  if any) associat
389b6 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68  ed with the BtSh
389b7 61 72 65 64 20 64 61 74 61 62 61 73 65 20 0d 0a  ared database ..
389b8 20 20 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69    ** that contai
389b9 6e 73 20 74 61 62 6c 65 20 70 20 69 73 20 68 65  ns table p is he
389ba 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ld by the caller
389bb 2e 20 53 65 65 20 68 65 61 64 65 72 20 63 6f 6d  . See header com
389bc 6d 65 6e 74 73 20 0d 0a 20 20 2a 2a 20 61 62 6f  ments ..  ** abo
389bd 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  ve function sqli
389be 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73  te3VtabUnlockLis
389bf 74 28 29 20 66 6f 72 20 61 6e 20 65 78 70 6c 61  t() for an expla
389c0 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 0d 0a 20  nation of why.. 
389c1 20 2a 2a 20 74 68 69 73 20 6d 61 6b 65 73 20 69   ** this makes i
389c2 74 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73  t safe to access
389c3 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
389c4 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66  sconnect list of
389c5 20 61 6e 79 0d 0a 20 20 2a 2a 20 64 61 74 61 62   any..  ** datab
389c6 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
389c7 68 61 74 20 6d 61 79 20 68 61 76 65 20 61 6e 20  hat may have an 
389c8 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 2d 3e  entry in the p->
389c9 70 56 54 61 62 6c 65 20 6c 69 73 74 2e 0d 0a 20  pVTable list... 
389ca 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 64   */..  assert( d
389cb 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53  b==0 || sqlite3S
389cc 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
389cd 62 2c 20 30 2c 20 70 2d 3e 70 53 63 68 65 6d 61  b, 0, p->pSchema
389ce 29 20 29 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65 28  ) );....  while(
389cf 20 70 56 54 61 62 6c 65 20 29 7b 0d 0a 20 20 20   pVTable ){..   
389d0 20 73 71 6c 69 74 65 33 20 2a 64 62 32 20 3d 20   sqlite3 *db2 = 
389d1 70 56 54 61 62 6c 65 2d 3e 64 62 3b 0d 0a 20 20  pVTable->db;..  
389d2 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20    VTable *pNext 
389d3 3d 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74  = pVTable->pNext
389d4 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ;..    assert( d
389d5 62 32 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 64  b2 );..    if( d
389d6 62 32 3d 3d 64 62 20 29 7b 0d 0a 20 20 20 20 20  b2==db ){..     
389d7 20 70 52 65 74 20 3d 20 70 56 54 61 62 6c 65 3b   pRet = pVTable;
389d8 0d 0a 20 20 20 20 20 20 70 2d 3e 70 56 54 61 62  ..      p->pVTab
389d9 6c 65 20 3d 20 70 52 65 74 3b 0d 0a 20 20 20 20  le = pRet;..    
389da 20 20 70 52 65 74 2d 3e 70 4e 65 78 74 20 3d 20    pRet->pNext = 
389db 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  0;..    }else{..
389dc 20 20 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70        pVTable->p
389dd 4e 65 78 74 20 3d 20 64 62 32 2d 3e 70 44 69 73  Next = db2->pDis
389de 63 6f 6e 6e 65 63 74 3b 0d 0a 20 20 20 20 20 20  connect;..      
389df 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74  db2->pDisconnect
389e0 20 3d 20 70 56 54 61 62 6c 65 3b 0d 0a 20 20 20   = pVTable;..   
389e1 20 7d 0d 0a 20 20 20 20 70 56 54 61 62 6c 65 20   }..    pVTable 
389e2 3d 20 70 4e 65 78 74 3b 0d 0a 20 20 7d 0d 0a 0d  = pNext;..  }...
389e3 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c  .  assert( !db |
389e4 7c 20 70 52 65 74 20 29 3b 0d 0a 20 20 72 65 74  | pRet );..  ret
389e5 75 72 6e 20 70 52 65 74 3b 0d 0a 7d 0d 0a 0d 0a  urn pRet;..}....
389e6 0d 0a 2f 2a 0d 0a 2a 2a 20 44 69 73 63 6f 6e 6e  ../*..** Disconn
389e7 65 63 74 20 61 6c 6c 20 74 68 65 20 76 69 72 74  ect all the virt
389e8 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
389e9 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  s in the sqlite3
389ea 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
389eb 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  t...**..** This 
389ec 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c  function may onl
389ed 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
389ee 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 73 73   the mutexes ass
389ef 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c  ociated with all
389f0 0d 0a 2a 2a 20 73 68 61 72 65 64 20 62 2d 74 72  ..** shared b-tr
389f1 65 65 20 64 61 74 61 62 61 73 65 73 20 6f 70 65  ee databases ope
389f2 6e 65 64 20 75 73 69 6e 67 20 63 6f 6e 6e 65 63  ned using connec
389f3 74 69 6f 6e 20 64 62 20 61 72 65 20 68 65 6c 64  tion db are held
389f4 20 62 79 20 74 68 65 20 0d 0a 2a 2a 20 63 61 6c   by the ..** cal
389f5 6c 65 72 2e 20 54 68 69 73 20 69 73 20 64 6f 6e  ler. This is don
389f6 65 20 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65  e to protect the
389f7 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
389f8 6e 65 63 74 20 6c 69 73 74 2e 20 54 68 65 0d 0a  nect list. The..
389f9 2a 2a 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63  ** sqlite3.pDisc
389fa 6f 6e 6e 65 63 74 20 6c 69 73 74 20 69 73 20 61  onnect list is a
389fb 63 63 65 73 73 65 64 20 6f 6e 6c 79 20 61 73 20  ccessed only as 
389fc 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  follows:..**..**
389fd 20 20 20 31 29 20 42 79 20 74 68 69 73 20 66 75     1) By this fu
389fe 6e 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20  nction. In this 
389ff 63 61 73 65 2c 20 61 6c 6c 20 42 74 53 68 61 72  case, all BtShar
38a00 65 64 20 6d 75 74 65 78 65 73 20 61 6e 64 20 74  ed mutexes and t
38a01 68 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 20 20 20  he mutex..**    
38a02 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74    associated wit
38a03 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
38a04 61 6e 64 6c 65 20 69 74 73 65 6c 66 20 6d 75 73  andle itself mus
38a05 74 20 62 65 20 68 65 6c 64 2e 0d 0a 2a 2a 0d 0a  t be held...**..
38a06 2a 2a 20 20 20 32 29 20 42 79 20 66 75 6e 63 74  **   2) By funct
38a07 69 6f 6e 20 76 74 61 62 44 69 73 63 6f 6e 6e 65  ion vtabDisconne
38a08 63 74 41 6c 6c 28 29 2c 20 77 68 65 6e 20 69 74  ctAll(), when it
38a09 20 61 64 64 73 20 61 20 56 54 61 62 6c 65 20 65   adds a VTable e
38a0a 6e 74 72 79 20 74 6f 0d 0a 2a 2a 20 20 20 20 20  ntry to..**     
38a0b 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
38a0c 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 20 49  sconnect list. I
38a0d 6e 20 74 68 69 73 20 63 61 73 65 20 65 69 74 68  n this case eith
38a0e 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 20  er the BtShared 
38a0f 6d 75 74 65 78 0d 0a 2a 2a 20 20 20 20 20 20 61  mutex..**      a
38a10 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
38a11 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
38a12 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73  virtual table is
38a13 20 73 74 6f 72 65 64 20 69 6e 20 69 73 20 68 65   stored in is he
38a14 6c 64 0d 0a 2a 2a 20 20 20 20 20 20 6f 72 2c 20  ld..**      or, 
38a15 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  if the virtual t
38a16 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69  able is stored i
38a17 6e 20 61 20 6e 6f 6e 2d 73 68 61 72 61 62 6c 65  n a non-sharable
38a18 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0d   database, then.
38a19 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 64 61 74  .**      the dat
38a1a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74  abase handle mut
38a1b 65 78 20 69 73 20 68 65 6c 64 2e 0d 0a 2a 2a 0d  ex is held...**.
38a1c 0a 2a 2a 20 41 73 20 61 20 72 65 73 75 6c 74 2c  .** As a result,
38a1d 20 61 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63   a sqlite3.pDisc
38a1e 6f 6e 6e 65 63 74 20 63 61 6e 6e 6f 74 20 62 65  onnect cannot be
38a1f 20 61 63 63 65 73 73 65 64 20 73 69 6d 75 6c 74   accessed simult
38a20 61 6e 65 6f 75 73 6c 79 20 0d 0a 2a 2a 20 62 79  aneously ..** by
38a21 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64   multiple thread
38a22 73 2e 20 49 74 20 69 73 20 74 68 72 65 61 64 2d  s. It is thread-
38a23 73 61 66 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  safe...*/..SQLIT
38a24 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
38a25 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
38a26 4c 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64 62  List(sqlite3 *db
38a27 29 7b 0d 0a 20 20 56 54 61 62 6c 65 20 2a 70 20  ){..  VTable *p 
38a28 3d 20 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63  = db->pDisconnec
38a29 74 3b 0d 0a 20 20 64 62 2d 3e 70 44 69 73 63 6f  t;..  db->pDisco
38a2a 6e 6e 65 63 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20  nnect = 0;....  
38a2b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
38a2c 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
38a2d 78 65 73 28 64 62 29 20 29 3b 0d 0a 20 20 61 73  xes(db) );..  as
38a2e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
38a2f 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
38a30 65 78 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20  ex) );....  if( 
38a31 70 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  p ){..    sqlite
38a32 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
38a33 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0d 0a  tatements(db);..
38a34 20 20 20 20 64 6f 20 7b 0d 0a 20 20 20 20 20 20      do {..      
38a35 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d 20  VTable *pNext = 
38a36 70 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 20  p->pNext;..     
38a37 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
38a38 63 6b 28 70 29 3b 0d 0a 20 20 20 20 20 20 70 20  ck(p);..      p 
38a39 3d 20 70 4e 65 78 74 3b 0d 0a 20 20 20 20 7d 77  = pNext;..    }w
38a3a 68 69 6c 65 28 20 70 20 29 3b 0d 0a 20 20 7d 0d  hile( p );..  }.
38a3b 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 65  .}..../*..** Cle
38a3c 61 72 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76  ar any and all v
38a3d 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66  irtual-table inf
38a3e 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
38a3f 65 20 54 61 62 6c 65 20 72 65 63 6f 72 64 2e 0d  e Table record..
38a40 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
38a41 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 6f 72 20   is called, for 
38a42 65 78 61 6d 70 6c 65 2c 20 6a 75 73 74 20 62 65  example, just be
38a43 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20 74 68  fore deleting th
38a44 65 20 54 61 62 6c 65 0d 0a 2a 2a 20 72 65 63 6f  e Table..** reco
38a45 72 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 69 6e 63  rd...**..** Sinc
38a46 65 20 69 74 20 69 73 20 61 20 76 69 72 74 75 61  e it is a virtua
38a47 6c 2d 74 61 62 6c 65 2c 20 74 68 65 20 54 61 62  l-table, the Tab
38a48 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  le structure con
38a49 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0d  tains a pointer.
38a4a 0a 2a 2a 20 74 6f 20 74 68 65 20 68 65 61 64 20  .** to the head 
38a4b 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  of a linked list
38a4c 20 6f 66 20 56 54 61 62 6c 65 20 73 74 72 75 63   of VTable struc
38a4d 74 75 72 65 73 2e 20 45 61 63 68 20 56 54 61 62  tures. Each VTab
38a4e 6c 65 20 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72  le ..** structur
38a4f 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  e is associated 
38a50 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 73 71  with a single sq
38a51 6c 69 74 65 33 2a 20 75 73 65 72 20 6f 66 20 74  lite3* user of t
38a52 68 65 20 73 63 68 65 6d 61 2e 0d 0a 2a 2a 20 54  he schema...** T
38a53 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
38a54 6e 74 20 6f 66 20 74 68 65 20 56 54 61 62 6c 65  nt of the VTable
38a55 20 73 74 72 75 63 74 75 72 65 20 61 73 73 6f 63   structure assoc
38a56 69 61 74 65 64 20 77 69 74 68 20 64 61 74 61 62  iated with datab
38a57 61 73 65 20 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74  ase ..** connect
38a58 69 6f 6e 20 64 62 20 69 73 20 64 65 63 72 65 6d  ion db is decrem
38a59 65 6e 74 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  ented immediatel
38a5a 79 20 28 77 68 69 63 68 20 6d 61 79 20 6c 65 61  y (which may lea
38a5b 64 20 74 6f 20 74 68 65 20 0d 0a 2a 2a 20 73 74  d to the ..** st
38a5c 72 75 63 74 75 72 65 20 62 65 69 6e 67 20 78 44  ructure being xD
38a5d 69 73 63 6f 6e 6e 65 63 74 65 64 20 61 6e 64 20  isconnected and 
38a5e 66 72 65 65 29 2e 20 41 6e 79 20 6f 74 68 65 72  free). Any other
38a5f 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75 72   VTable structur
38a60 65 73 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69  es..** in the li
38a61 73 74 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20  st are moved to 
38a62 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
38a63 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66 20  connect list of 
38a64 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0d  the associated .
38a65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
38a66 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51  nection...*/..SQ
38a67 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
38a68 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  d sqlite3VtabCle
38a69 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ar(sqlite3 *db, 
38a6a 54 61 62 6c 65 20 2a 70 29 7b 0d 0a 20 20 69 66  Table *p){..  if
38a6b 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 70 6e 42  ( !db || db->pnB
38a6c 79 74 65 73 46 72 65 65 64 3d 3d 30 20 29 20 76  ytesFreed==0 ) v
38a6d 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c  tabDisconnectAll
38a6e 28 30 2c 20 70 29 3b 0d 0a 20 20 69 66 28 20 70  (0, p);..  if( p
38a6f 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b  ->azModuleArg ){
38a70 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ..    int i;..  
38a71 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
38a72 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69 2b 2b 29  nModuleArg; i++)
38a73 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
38a74 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 7a  DbFree(db, p->az
38a75 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0d 0a  ModuleArg[i]);..
38a76 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
38a77 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
38a78 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0d 0a 20  azModuleArg);.. 
38a79 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
38a7a 41 64 64 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65  Add a new module
38a7b 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 54 61   argument to pTa
38a7c 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  ble->azModuleArg
38a7d 5b 5d 2e 0d 0a 2a 2a 20 54 68 65 20 73 74 72 69  []...** The stri
38a7e 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64  ng is not copied
38a7f 20 2d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69   - the pointer i
38a80 73 20 73 74 6f 72 65 64 2e 20 20 54 68 65 0d 0a  s stored.  The..
38a81 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62  ** string will b
38a82 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74 69  e freed automati
38a83 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 74  cally when the t
38a84 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 64 65 6c 65  able is..** dele
38a85 74 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ted...*/..static
38a86 20 76 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41   void addModuleA
38a87 72 67 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20  rgument(sqlite3 
38a88 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  *db, Table *pTab
38a89 6c 65 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b  le, char *zArg){
38a8a 0d 0a 20 20 69 6e 74 20 69 20 3d 20 70 54 61 62  ..  int i = pTab
38a8b 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b  le->nModuleArg++
38a8c 3b 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20  ;..  int nBytes 
38a8d 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29  = sizeof(char *)
38a8e 2a 28 31 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64  *(1+pTable->nMod
38a8f 75 6c 65 41 72 67 29 3b 0d 0a 20 20 63 68 61 72  uleArg);..  char
38a90 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0d   **azModuleArg;.
38a91 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d  .  azModuleArg =
38a92 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
38a93 63 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a  c(db, pTable->az
38a94 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79 74 65  ModuleArg, nByte
38a95 73 29 3b 0d 0a 20 20 69 66 28 20 61 7a 4d 6f 64  s);..  if( azMod
38a96 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0d 0a 20 20  uleArg==0 ){..  
38a97 20 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 66 6f    int j;..    fo
38a98 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  r(j=0; j<i; j++)
38a99 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
38a9a 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
38a9b 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a  e->azModuleArg[j
38a9c 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ]);..    }..    
38a9d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
38a9e 2c 20 7a 41 72 67 29 3b 0d 0a 20 20 20 20 73 71  , zArg);..    sq
38a9f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
38aa0 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
38aa1 41 72 67 29 3b 0d 0a 20 20 20 20 70 54 61 62 6c  Arg);..    pTabl
38aa2 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20  e->nModuleArg = 
38aa3 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  0;..  }else{..  
38aa4 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d    azModuleArg[i]
38aa5 20 3d 20 7a 41 72 67 3b 0d 0a 20 20 20 20 61 7a   = zArg;..    az
38aa6 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d  ModuleArg[i+1] =
38aa7 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54 61 62   0;..  }..  pTab
38aa8 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20  le->azModuleArg 
38aa9 3d 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0d 0a  = azModuleArg;..
38aaa 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  }..../*..** The 
38aab 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
38aac 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
38aad 74 20 66 69 72 73 74 20 73 65 65 73 20 61 20 43  t first sees a C
38aae 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
38aaf 42 4c 45 0d 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  BLE..** statemen
38ab0 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e  t.  The module n
38ab1 61 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61 72  ame has been par
38ab2 73 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74  sed, but the opt
38ab3 69 6f 6e 61 6c 20 6c 69 73 74 0d 0a 2a 2a 20 6f  ional list..** o
38ab4 66 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  f parameters tha
38ab5 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64  t follow the mod
38ab6 75 6c 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69  ule name are sti
38ab7 6c 6c 20 70 65 6e 64 69 6e 67 2e 0d 0a 2a 2f 0d  ll pending...*/.
38ab8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
38ab9 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
38aba 42 65 67 69 6e 50 61 72 73 65 28 0d 0a 20 20 50  BeginParse(..  P
38abb 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
38abc 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
38abd 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 54 6f  context */..  To
38abe 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
38abf 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e      /* Name of n
38ac0 65 77 20 74 61 62 6c 65 2c 20 6f 72 20 64 61 74  ew table, or dat
38ac1 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20  abase name */.. 
38ac2 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20   Token *pName2, 
38ac3 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
38ac4 66 20 6e 65 77 20 74 61 62 6c 65 20 6f 72 20 4e  f new table or N
38ac5 55 4c 4c 20 2a 2f 0d 0a 20 20 54 6f 6b 65 6e 20  ULL */..  Token 
38ac6 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20 20 20  *pModuleName    
38ac7 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d  /* Name of the m
38ac8 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20 76 69  odule for the vi
38ac9 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0d 0a  rtual table */..
38aca 29 7b 0d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  ){..  int iDb;  
38acb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
38acc 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
38acd 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63  table is being c
38ace 72 65 61 74 65 64 20 69 6e 20 2a 2f 0d 0a 20 20  reated in */..  
38acf 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20  Table *pTable;  
38ad0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
38ad1 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
38ad2 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  /..  sqlite3 *db
38ad3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
38ad4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
38ad5 6e 20 2a 2f 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  n */....  sqlite
38ad6 33 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72  3StartTable(pPar
38ad7 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
38ad8 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20 30 29 3b  e2, 0, 0, 1, 0);
38ad9 0d 0a 20 20 70 54 61 62 6c 65 20 3d 20 70 50 61  ..  pTable = pPa
38ada 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0d  rse->pNewTable;.
38adb 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
38adc 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 61 73   ) return;..  as
38add 73 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d  sert( 0==pTable-
38ade 3e 70 49 6e 64 65 78 20 29 3b 0d 0a 0d 0a 20 20  >pIndex );....  
38adf 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
38ae0 0d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
38ae1 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
38ae2 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65  b, pTable->pSche
38ae3 6d 61 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ma);..  assert( 
38ae4 69 44 62 3e 3d 30 20 29 3b 0d 0a 0d 0a 20 20 70  iDb>=0 );....  p
38ae5 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20  Table->tabFlags 
38ae6 7c 3d 20 54 46 5f 56 69 72 74 75 61 6c 3b 0d 0a  |= TF_Virtual;..
38ae7 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c    pTable->nModul
38ae8 65 41 72 67 20 3d 20 30 3b 0d 0a 20 20 61 64 64  eArg = 0;..  add
38ae9 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
38aea 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  b, pTable, sqlit
38aeb 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
38aec 64 62 2c 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 29  db, pModuleName)
38aed 29 3b 0d 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41  );..  addModuleA
38aee 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
38aef 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72  le, sqlite3DbStr
38af0 44 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  Dup(db, db->aDb[
38af1 69 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0d 0a 20  iDb].zName));.. 
38af2 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65   addModuleArgume
38af3 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73  nt(db, pTable, s
38af4 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
38af5 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65  b, pTable->zName
38af6 29 29 3b 0d 0a 20 20 70 50 61 72 73 65 2d 3e 73  ));..  pParse->s
38af7 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69  NameToken.n = (i
38af8 6e 74 29 28 26 70 4d 6f 64 75 6c 65 4e 61 6d 65  nt)(&pModuleName
38af9 2d 3e 7a 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d  ->z[pModuleName-
38afa 3e 6e 5d 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29  >n] - pName1->z)
38afb 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;....#ifndef SQL
38afc 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
38afd 5a 41 54 49 4f 4e 0d 0a 20 20 2f 2a 20 43 72 65  ZATION..  /* Cre
38afe 61 74 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20  ating a virtual 
38aff 74 61 62 6c 65 20 69 6e 76 6f 6b 65 73 20 74 68  table invokes th
38b00 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
38b01 63 61 6c 6c 62 61 63 6b 20 74 77 69 63 65 2e 0d  callback twice..
38b02 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
38b03 69 6e 76 6f 63 61 74 69 6f 6e 2c 20 74 6f 20 6f  invocation, to o
38b04 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e  btain permission
38b05 20 74 6f 20 49 4e 53 45 52 54 20 61 20 72 6f 77   to INSERT a row
38b06 20 69 6e 74 6f 20 74 68 65 0d 0a 20 20 2a 2a 20   into the..  ** 
38b07 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
38b08 62 6c 65 2c 20 68 61 73 20 61 6c 72 65 61 64 79  ble, has already
38b09 20 62 65 65 6e 20 6d 61 64 65 20 62 79 20 73 71   been made by sq
38b0a 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
38b0b 29 2e 0d 0a 20 20 2a 2a 20 54 68 65 20 73 65 63  )...  ** The sec
38b0c 6f 6e 64 20 63 61 6c 6c 2c 20 74 6f 20 6f 62 74  ond call, to obt
38b0d 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74  ain permission t
38b0e 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62  o create the tab
38b0f 6c 65 2c 20 69 73 20 6d 61 64 65 20 6e 6f 77 2e  le, is made now.
38b10 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54  ..  */..  if( pT
38b11 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
38b12 67 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  g ){..    sqlite
38b13 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
38b14 65 2c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  e, SQLITE_CREATE
38b15 5f 56 54 41 42 4c 45 2c 20 70 54 61 62 6c 65 2d  _VTABLE, pTable-
38b16 3e 7a 4e 61 6d 65 2c 20 0d 0a 20 20 20 20 20 20  >zName, ..      
38b17 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 7a        pTable->az
38b18 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 50  ModuleArg[0], pP
38b19 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44  arse->db->aDb[iD
38b1a 62 5d 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 7d 0d  b].zName);..  }.
38b1b 0a 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a  .#endif..}..../*
38b1c 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
38b1d 65 20 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75  e takes the modu
38b1e 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74  le argument that
38b1f 20 68 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75   has been accumu
38b20 6c 61 74 69 6e 67 0d 0a 2a 2a 20 69 6e 20 70 50  lating..** in pP
38b21 61 72 73 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64  arse->zArg[] and
38b22 20 61 70 70 65 6e 64 73 20 69 74 20 74 6f 20 74   appends it to t
38b23 68 65 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d  he list of argum
38b24 65 6e 74 73 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20  ents on the..** 
38b25 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
38b26 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
38b27 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 50  nstruction in pP
38b28 61 72 73 65 2d 3e 70 54 61 62 6c 65 2e 0d 0a 2a  arse->pTable...*
38b29 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  /..static void a
38b2a 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62  ddArgumentToVtab
38b2b 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
38b2c 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
38b2d 73 41 72 67 2e 7a 20 26 26 20 41 4c 57 41 59 53  sArg.z && ALWAYS
38b2e 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62  (pParse->pNewTab
38b2f 6c 65 29 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73  le) ){..    cons
38b30 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
38b31 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
38b32 3e 73 41 72 67 2e 7a 3b 0d 0a 20 20 20 20 69 6e  >sArg.z;..    in
38b33 74 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41  t n = pParse->sA
38b34 72 67 2e 6e 3b 0d 0a 20 20 20 20 73 71 6c 69 74  rg.n;..    sqlit
38b35 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
38b36 3e 64 62 3b 0d 0a 20 20 20 20 61 64 64 4d 6f 64  >db;..    addMod
38b37 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20  uleArgument(db, 
38b38 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
38b39 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e  e, sqlite3DbStrN
38b3a 44 75 70 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0d  Dup(db, z, n));.
38b3b 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
38b3c 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
38b3d 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
38b3e 61 66 74 65 72 20 74 68 65 20 43 52 45 41 54 45  after the CREATE
38b3f 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
38b40 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 68 61 73  tatement..** has
38b41 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79   been completely
38b42 20 70 61 72 73 65 64 2e 0d 0a 2a 2f 0d 0a 53 51   parsed...*/..SQ
38b43 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
38b44 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  d sqlite3VtabFin
38b45 69 73 68 50 61 72 73 65 28 50 61 72 73 65 20 2a  ishParse(Parse *
38b46 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
38b47 45 6e 64 29 7b 0d 0a 20 20 54 61 62 6c 65 20 2a  End){..  Table *
38b48 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
38b49 4e 65 77 54 61 62 6c 65 3b 20 20 2f 2a 20 54 68  NewTable;  /* Th
38b4a 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f  e table being co
38b4b 6e 73 74 72 75 63 74 65 64 20 2a 2f 0d 0a 20 20  nstructed */..  
38b4c 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
38b4d 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20  arse->db;       
38b4e 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
38b4f 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
38b50 0a 0d 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  ...  if( pTab==0
38b51 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 61 64   ) return;..  ad
38b52 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28  dArgumentToVtab(
38b53 70 50 61 72 73 65 29 3b 0d 0a 20 20 70 50 61 72  pParse);..  pPar
38b54 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0d  se->sArg.z = 0;.
38b55 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f  .  if( pTab->nMo
38b56 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74 75  duleArg<1 ) retu
38b57 72 6e 3b 0d 0a 20 20 0d 0a 20 20 2f 2a 20 49 66  rn;..  ..  /* If
38b58 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
38b59 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
38b5a 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65 6e 74  ent is being ent
38b5b 65 72 65 64 20 66 6f 72 20 74 68 65 0d 0a 20 20  ered for the..  
38b5c 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28 69  ** first time (i
38b5d 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66  n other words if
38b5e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
38b5f 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62  le is actually b
38b60 65 69 6e 67 0d 0a 20 20 2a 2a 20 63 72 65 61 74  eing..  ** creat
38b61 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f  ed now instead o
38b62 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61  f just being rea
38b63 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f  d out of sqlite_
38b64 6d 61 73 74 65 72 29 20 74 68 65 6e 0d 0a 20 20  master) then..  
38b65 2a 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c  ** do additional
38b66 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
38b67 77 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74  work and store t
38b68 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  he statement tex
38b69 74 0d 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73  t..  ** in the s
38b6a 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
38b6b 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  le...  */..  if(
38b6c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
38b6d 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  ){..    char *zS
38b6e 74 6d 74 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a  tmt;..    char *
38b6f 7a 57 68 65 72 65 3b 0d 0a 20 20 20 20 69 6e 74  zWhere;..    int
38b70 20 69 44 62 3b 0d 0a 20 20 20 20 56 64 62 65 20   iDb;..    Vdbe 
38b71 2a 76 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f  *v;....    /* Co
38b72 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65  mpute the comple
38b73 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43  te text of the C
38b74 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
38b75 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  BLE statement */
38b76 0d 0a 20 20 20 20 69 66 28 20 70 45 6e 64 20 29  ..    if( pEnd )
38b77 7b 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  {..      pParse-
38b78 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20  >sNameToken.n = 
38b79 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20  (int)(pEnd->z - 
38b7a 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
38b7b 65 6e 2e 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b  en.z) + pEnd->n;
38b7c 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 7a 53 74  ..    }..    zSt
38b7d 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  mt = sqlite3MPri
38b7e 6e 74 66 28 64 62 2c 20 22 43 52 45 41 54 45 20  ntf(db, "CREATE 
38b7f 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 25 54  VIRTUAL TABLE %T
38b80 22 2c 20 26 70 50 61 72 73 65 2d 3e 73 4e 61 6d  ", &pParse->sNam
38b81 65 54 6f 6b 65 6e 29 3b 0d 0a 0d 0a 20 20 20 20  eToken);....    
38b82 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
38b83 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
38b84 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
38b85 74 65 64 20 69 6e 20 74 68 65 20 0d 0a 20 20 20  ted in the ..   
38b86 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45   ** SQLITE_MASTE
38b87 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73  R table.  We jus
38b88 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65  t need to update
38b89 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20   that slot with 
38b8a 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20  all..    ** the 
38b8b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76  information we'v
38b8c 65 20 63 6f 6c 6c 65 63 74 65 64 2e 20 20 0d 0a  e collected.  ..
38b8d 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54      **..    ** T
38b8e 68 65 20 56 4d 20 72 65 67 69 73 74 65 72 20 6e  he VM register n
38b8f 75 6d 62 65 72 20 70 50 61 72 73 65 2d 3e 72 65  umber pParse->re
38b90 67 52 6f 77 69 64 20 68 6f 6c 64 73 20 74 68 65  gRowid holds the
38b91 20 72 6f 77 69 64 20 6f 66 20 61 6e 0d 0a 20 20   rowid of an..  
38b92 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68    ** entry in th
38b93 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
38b94 74 61 62 6c 65 20 74 68 74 20 77 61 73 20 63 72  table tht was cr
38b95 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 76  eated for this v
38b96 74 61 62 0d 0a 20 20 20 20 2a 2a 20 62 79 20 73  tab..    ** by s
38b97 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
38b98 28 29 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ()...    */..   
38b99 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
38b9a 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
38b9b 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0d  pTab->pSchema);.
38b9c 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
38b9d 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0d  edParse(pParse,.
38b9e 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
38b9f 51 2e 25 73 20 22 0d 0a 20 20 20 20 20 20 20 20  Q.%s "..        
38ba0 20 22 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c   "SET type='tabl
38ba1 65 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c  e', name=%Q, tbl
38ba2 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61  _name=%Q, rootpa
38ba3 67 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0d 0a  ge=0, sql=%Q "..
38ba4 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72 6f         "WHERE ro
38ba5 77 69 64 3d 23 25 64 22 2c 0d 0a 20 20 20 20 20  wid=#%d",..     
38ba6 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
38ba7 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
38ba8 45 28 69 44 62 29 2c 0d 0a 20 20 20 20 20 20 70  E(iDb),..      p
38ba9 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0d 0a 20 20 20  Tab->zName,..   
38baa 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0d     pTab->zName,.
38bab 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0d 0a 20  .      zStmt,.. 
38bac 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67       pParse->reg
38bad 52 6f 77 69 64 0d 0a 20 20 20 20 29 3b 0d 0a 20  Rowid..    );.. 
38bae 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
38baf 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0d 0a 20 20  (db, zStmt);..  
38bb0 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
38bb1 56 64 62 65 28 70 50 61 72 73 65 29 3b 0d 0a 20  Vdbe(pParse);.. 
38bb2 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
38bb3 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
38bb4 44 62 29 3b 0d 0a 0d 0a 20 20 20 20 73 71 6c 69  Db);....    sqli
38bb5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
38bb6 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30   OP_Expire, 0, 0
38bb7 29 3b 0d 0a 20 20 20 20 7a 57 68 65 72 65 20 3d  );..    zWhere =
38bb8 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
38bb9 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 20 41  db, "name='%q' A
38bba 4e 44 20 74 79 70 65 3d 27 74 61 62 6c 65 27 22  ND type='table'"
38bbb 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d  , pTab->zName);.
38bbc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
38bbd 41 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70  AddParseSchemaOp
38bbe 28 76 2c 20 69 44 62 2c 20 7a 57 68 65 72 65 29  (v, iDb, zWhere)
38bbf 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
38bc0 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
38bc1 43 72 65 61 74 65 2c 20 69 44 62 2c 20 30 2c 20  Create, iDb, 0, 
38bc2 30 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  0, ..           
38bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
38bc4 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74  ab->zName, sqlit
38bc5 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d  e3Strlen30(pTab-
38bc6 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0d 0a 20  >zName) + 1);.. 
38bc7 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 77 65   }....  /* If we
38bc8 20 61 72 65 20 72 65 72 65 61 64 69 6e 67 20 74   are rereading t
38bc9 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
38bca 20 74 61 62 6c 65 20 63 72 65 61 74 65 20 74 68   table create th
38bcb 65 20 69 6e 2d 6d 65 6d 6f 72 79 0d 0a 20 20 2a  e in-memory..  *
38bcc 2a 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20  * record of the 
38bcd 74 61 62 6c 65 2e 20 54 68 65 20 78 43 6f 6e 6e  table. The xConn
38bce 65 63 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20  ect() method is 
38bcf 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 74 69 6c  not called until
38bd0 0d 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  ..  ** the first
38bd1 20 74 69 6d 65 20 74 68 65 20 76 69 72 74 75 61   time the virtua
38bd2 6c 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20  l table is used 
38bd3 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  in an SQL statem
38bd4 65 6e 74 2e 20 54 68 69 73 0d 0a 20 20 2a 2a 20  ent. This..  ** 
38bd5 61 6c 6c 6f 77 73 20 61 20 73 63 68 65 6d 61 20  allows a schema 
38bd6 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 76 69  that contains vi
38bd7 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20  rtual tables to 
38bd8 62 65 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65  be loaded before
38bd9 0d 0a 20 20 2a 2a 20 74 68 65 20 72 65 71 75 69  ..  ** the requi
38bda 72 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c  red virtual tabl
38bdb 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
38bdc 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  s are registered
38bdd 2e 20 20 2a 2f 0d 0a 20 20 65 6c 73 65 20 7b 0d  .  */..  else {.
38bde 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64  .    Table *pOld
38bdf 3b 0d 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70  ;..    Schema *p
38be0 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70  Schema = pTab->p
38be1 53 63 68 65 6d 61 3b 0d 0a 20 20 20 20 63 6f 6e  Schema;..    con
38be2 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
38be3 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0d 0a 20   pTab->zName;.. 
38be4 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73     int nName = s
38be5 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
38be6 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 61 73 73 65  Name);..    asse
38be7 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
38be8 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30  aMutexHeld(db, 0
38be9 2c 20 70 53 63 68 65 6d 61 29 20 29 3b 0d 0a 20  , pSchema) );.. 
38bea 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65     pOld = sqlite
38beb 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63  3HashInsert(&pSc
38bec 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a  hema->tblHash, z
38bed 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61  Name, nName, pTa
38bee 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 4f 6c  b);..    if( pOl
38bef 64 20 29 7b 0d 0a 20 20 20 20 20 20 64 62 2d 3e  d ){..      db->
38bf0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
38bf1 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
38bf2 20 70 54 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20   pTab==pOld );  
38bf3 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68  /* Malloc must h
38bf4 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64  ave failed insid
38bf5 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a  e HashInsert() *
38bf6 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  /..      return;
38bf7 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50 61  ..    }..    pPa
38bf8 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d  rse->pNewTable =
38bf9 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f   0;..  }..}..../
38bfa 2a 0d 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  *..** The parser
38bfb 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
38bfc 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73  ine when it sees
38bfd 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
38bfe 0d 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75 6d  ..** of an argum
38bff 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c  ent to the modul
38c00 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41  e name in a CREA
38c01 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
38c02 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2f 0d   statement...*/.
38c03 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
38c04 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
38c05 41 72 67 49 6e 69 74 28 50 61 72 73 65 20 2a 70  ArgInit(Parse *p
38c06 50 61 72 73 65 29 7b 0d 0a 20 20 61 64 64 41 72  Parse){..  addAr
38c07 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61  gumentToVtab(pPa
38c08 72 73 65 29 3b 0d 0a 20 20 70 50 61 72 73 65 2d  rse);..  pParse-
38c09 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0d 0a 20 20  >sArg.z = 0;..  
38c0a 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d  pParse->sArg.n =
38c0b 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
38c0c 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
38c0d 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  s this routine f
38c0e 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 61 66  or each token af
38c0f 74 65 72 20 74 68 65 20 66 69 72 73 74 20 74 6f  ter the first to
38c10 6b 65 6e 0d 0a 2a 2a 20 69 6e 20 61 6e 20 61 72  ken..** in an ar
38c11 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f  gument to the mo
38c12 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43  dule name in a C
38c13 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
38c14 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a  BLE statement...
38c15 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
38c16 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
38c17 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72  tabArgExtend(Par
38c18 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
38c19 6e 20 2a 70 29 7b 0d 0a 20 20 54 6f 6b 65 6e 20  n *p){..  Token 
38c1a 2a 70 41 72 67 20 3d 20 26 70 50 61 72 73 65 2d  *pArg = &pParse-
38c1b 3e 73 41 72 67 3b 0d 0a 20 20 69 66 28 20 70 41  >sArg;..  if( pA
38c1c 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0d 0a 20 20 20  rg->z==0 ){..   
38c1d 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b   pArg->z = p->z;
38c1e 0d 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20  ..    pArg->n = 
38c1f 70 2d 3e 6e 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  p->n;..  }else{.
38c20 0a 20 20 20 20 61 73 73 65 72 74 28 70 41 72 67  .    assert(pArg
38c21 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0d 0a 20 20  ->z < p->z);..  
38c22 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e 74    pArg->n = (int
38c23 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d 20  )(&p->z[p->n] - 
38c24 70 41 72 67 2d 3e 7a 29 3b 0d 0a 20 20 7d 0d 0a  pArg->z);..  }..
38c25 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f  }..../*..** Invo
38c26 6b 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ke a virtual tab
38c27 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 28  le constructor (
38c28 65 69 74 68 65 72 20 78 43 72 65 61 74 65 20 6f  either xCreate o
38c29 72 20 78 43 6f 6e 6e 65 63 74 29 2e 20 54 68 65  r xConnect). The
38c2a 0d 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20  ..** pointer to 
38c2b 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  the function to 
38c2c 69 6e 76 6f 6b 65 20 69 73 20 70 61 73 73 65 64  invoke is passed
38c2d 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 20 70   as the fourth p
38c2e 61 72 61 6d 65 74 65 72 0d 0a 2a 2a 20 74 6f 20  arameter..** to 
38c2f 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0d  this procedure..
38c30 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
38c31 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
38c32 74 6f 72 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  tor(..  sqlite3 
38c33 2a 64 62 2c 20 0d 0a 20 20 54 61 62 6c 65 20 2a  *db, ..  Table *
38c34 70 54 61 62 2c 0d 0a 20 20 4d 6f 64 75 6c 65 20  pTab,..  Module 
38c35 2a 70 4d 6f 64 2c 0d 0a 20 20 69 6e 74 20 28 2a  *pMod,..  int (*
38c36 78 43 6f 6e 73 74 72 75 63 74 29 28 73 71 6c 69  xConstruct)(sqli
38c37 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63  te3*,void*,int,c
38c38 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a  onst char*const*
38c39 2c 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 2a 2c  ,sqlite3_vtab**,
38c3a 63 68 61 72 2a 2a 29 2c 0d 0a 20 20 63 68 61 72  char**),..  char
38c3b 20 2a 2a 70 7a 45 72 72 0d 0a 29 7b 0d 0a 20 20   **pzErr..){..  
38c3c 56 74 61 62 43 74 78 20 73 43 74 78 3b 0d 0a 20  VtabCtx sCtx;.. 
38c3d 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65   VTable *pVTable
38c3e 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  ;..  int rc;..  
38c3f 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
38c40 74 2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74  t*azArg = (const
38c41 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54   char *const*)pT
38c42 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b  ab->azModuleArg;
38c43 0d 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ..  int nArg = p
38c44 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b  Tab->nModuleArg;
38c45 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  ..  char *zErr =
38c46 20 30 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 4d 6f   0;..  char *zMo
38c47 64 75 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74  duleName = sqlit
38c48 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
38c49 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  s", pTab->zName)
38c4a 3b 0d 0a 0d 0a 20 20 69 66 28 20 21 7a 4d 6f 64  ;....  if( !zMod
38c4b 75 6c 65 4e 61 6d 65 20 29 7b 0d 0a 20 20 20 20  uleName ){..    
38c4c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
38c4d 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70  MEM;..  }....  p
38c4e 56 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  VTable = sqlite3
38c4f 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
38c50 20 73 69 7a 65 6f 66 28 56 54 61 62 6c 65 29 29   sizeof(VTable))
38c51 3b 0d 0a 20 20 69 66 28 20 21 70 56 54 61 62 6c  ;..  if( !pVTabl
38c52 65 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  e ){..    sqlite
38c53 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f 64  3DbFree(db, zMod
38c54 75 6c 65 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 72  uleName);..    r
38c55 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
38c56 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 70 56 54 61  EM;..  }..  pVTa
38c57 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0d 0a 20  ble->db = db;.. 
38c58 20 70 56 54 61 62 6c 65 2d 3e 70 4d 6f 64 20 3d   pVTable->pMod =
38c59 20 70 4d 6f 64 3b 0d 0a 0d 0a 20 20 2f 2a 20 49   pMod;....  /* I
38c5a 6e 76 6f 6b 65 20 74 68 65 20 76 69 72 74 75 61  nvoke the virtua
38c5b 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63  l table construc
38c5c 74 6f 72 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74  tor */..  assert
38c5d 28 20 26 64 62 2d 3e 70 56 74 61 62 43 74 78 20  ( &db->pVtabCtx 
38c5e 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 78 43  );..  assert( xC
38c5f 6f 6e 73 74 72 75 63 74 20 29 3b 0d 0a 20 20 73  onstruct );..  s
38c60 43 74 78 2e 70 54 61 62 20 3d 20 70 54 61 62 3b  Ctx.pTab = pTab;
38c61 0d 0a 20 20 73 43 74 78 2e 70 56 54 61 62 6c 65  ..  sCtx.pVTable
38c62 20 3d 20 70 56 54 61 62 6c 65 3b 0d 0a 20 20 64   = pVTable;..  d
38c63 62 2d 3e 70 56 74 61 62 43 74 78 20 3d 20 26 73  b->pVtabCtx = &s
38c64 43 74 78 3b 0d 0a 20 20 72 63 20 3d 20 78 43 6f  Ctx;..  rc = xCo
38c65 6e 73 74 72 75 63 74 28 64 62 2c 20 70 4d 6f 64  nstruct(db, pMod
38c66 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c 20 61 7a  ->pAux, nArg, az
38c67 41 72 67 2c 20 26 70 56 54 61 62 6c 65 2d 3e 70  Arg, &pVTable->p
38c68 56 74 61 62 2c 20 26 7a 45 72 72 29 3b 0d 0a 20  Vtab, &zErr);.. 
38c69 20 64 62 2d 3e 70 56 74 61 62 43 74 78 20 3d 20   db->pVtabCtx = 
38c6a 30 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0;..  if( rc==SQ
38c6b 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d  LITE_NOMEM ) db-
38c6c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
38c6d 31 3b 0d 0a 0d 0a 20 20 69 66 28 20 53 51 4c 49  1;....  if( SQLI
38c6e 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0d 0a 20 20  TE_OK!=rc ){..  
38c6f 20 20 69 66 28 20 7a 45 72 72 3d 3d 30 20 29 7b    if( zErr==0 ){
38c70 0d 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d  ..      *pzErr =
38c71 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
38c72 64 62 2c 20 22 76 74 61 62 6c 65 20 63 6f 6e 73  db, "vtable cons
38c73 74 72 75 63 74 6f 72 20 66 61 69 6c 65 64 3a 20  tructor failed: 
38c74 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65  %s", zModuleName
38c75 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 7b 0d  );..    }else {.
38c76 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  .      *pzErr = 
38c77 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
38c78 62 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0d  b, "%s", zErr);.
38c79 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
38c7a 72 65 65 28 7a 45 72 72 29 3b 0d 0a 20 20 20 20  ree(zErr);..    
38c7b 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  }..    sqlite3Db
38c7c 46 72 65 65 28 64 62 2c 20 70 56 54 61 62 6c 65  Free(db, pVTable
38c7d 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  );..  }else if( 
38c7e 41 4c 57 41 59 53 28 70 56 54 61 62 6c 65 2d 3e  ALWAYS(pVTable->
38c7f 70 56 74 61 62 29 20 29 7b 0d 0a 20 20 20 20 2f  pVtab) ){..    /
38c80 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20  * Justification 
38c81 6f 66 20 41 4c 57 41 59 53 28 29 3a 20 20 41 20  of ALWAYS():  A 
38c82 63 6f 72 72 65 63 74 20 76 74 61 62 20 63 6f 6e  correct vtab con
38c83 73 74 72 75 63 74 6f 72 20 6d 75 73 74 20 61 6c  structor must al
38c84 6c 6f 63 61 74 65 0d 0a 20 20 20 20 2a 2a 20 74  locate..    ** t
38c85 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
38c86 6f 62 6a 65 63 74 20 69 66 20 73 75 63 63 65 73  object if succes
38c87 73 66 75 6c 2e 20 20 2a 2f 0d 0a 20 20 20 20 70  sful.  */..    p
38c88 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2d 3e 70  VTable->pVtab->p
38c89 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70  Module = pMod->p
38c8a 4d 6f 64 75 6c 65 3b 0d 0a 20 20 20 20 70 56 54  Module;..    pVT
38c8b 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0d  able->nRef = 1;.
38c8c 0a 20 20 20 20 69 66 28 20 73 43 74 78 2e 70 54  .    if( sCtx.pT
38c8d 61 62 20 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e  ab ){..      con
38c8e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
38c8f 20 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74   = "vtable const
38c90 72 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64  ructor did not d
38c91 65 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25  eclare schema: %
38c92 73 22 3b 0d 0a 20 20 20 20 20 20 2a 70 7a 45 72  s";..      *pzEr
38c93 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
38c94 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
38c95 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  pTab->zName);.. 
38c96 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
38c97 55 6e 6c 6f 63 6b 28 70 56 54 61 62 6c 65 29 3b  Unlock(pVTable);
38c98 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
38c99 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20  ITE_ERROR;..    
38c9a 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69 6e  }else{..      in
38c9b 74 20 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 2f  t iCol;..      /
38c9c 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20  * If everything 
38c9d 77 65 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74  went according t
38c9e 6f 20 70 6c 61 6e 2c 20 6c 69 6e 6b 20 74 68 65  o plan, link the
38c9f 20 6e 65 77 20 56 54 61 62 6c 65 20 73 74 72 75   new VTable stru
38ca0 63 74 75 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20  cture..      ** 
38ca1 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20  into the linked 
38ca2 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 70  list headed by p
38ca3 54 61 62 2d 3e 70 56 54 61 62 6c 65 2e 20 54 68  Tab->pVTable. Th
38ca4 65 6e 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20  en loop through 
38ca5 74 68 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63  the ..      ** c
38ca6 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
38ca7 62 6c 65 20 74 6f 20 73 65 65 20 69 66 20 61 6e  ble to see if an
38ca8 79 20 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61 69  y of them contai
38ca9 6e 20 74 68 65 20 74 6f 6b 65 6e 20 22 68 69 64  n the token "hid
38caa 64 65 6e 22 2e 0d 0a 20 20 20 20 20 20 2a 2a 20  den"...      ** 
38cab 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20 43  If so, set the C
38cac 6f 6c 75 6d 6e 2e 69 73 48 69 64 64 65 6e 20 66  olumn.isHidden f
38cad 6c 61 67 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  lag and remove t
38cae 68 65 20 74 6f 6b 65 6e 20 66 72 6f 6d 0d 0a 20  he token from.. 
38caf 20 20 20 20 20 2a 2a 20 74 68 65 20 74 79 70 65       ** the type
38cb0 20 73 74 72 69 6e 67 2e 20 20 2a 2f 0d 0a 20 20   string.  */..  
38cb1 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65      pVTable->pNe
38cb2 78 74 20 3d 20 70 54 61 62 2d 3e 70 56 54 61 62  xt = pTab->pVTab
38cb3 6c 65 3b 0d 0a 20 20 20 20 20 20 70 54 61 62 2d  le;..      pTab-
38cb4 3e 70 56 54 61 62 6c 65 20 3d 20 70 56 54 61 62  >pVTable = pVTab
38cb5 6c 65 3b 0d 0a 0d 0a 20 20 20 20 20 20 66 6f 72  le;....      for
38cb6 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54  (iCol=0; iCol<pT
38cb7 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  ab->nCol; iCol++
38cb8 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61 72  ){..        char
38cb9 20 2a 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e   *zType = pTab->
38cba 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65  aCol[iCol].zType
38cbb 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  ;..        int n
38cbc 54 79 70 65 3b 0d 0a 20 20 20 20 20 20 20 20 69  Type;..        i
38cbd 6e 74 20 69 20 3d 20 30 3b 0d 0a 20 20 20 20 20  nt i = 0;..     
38cbe 20 20 20 69 66 28 20 21 7a 54 79 70 65 20 29 20     if( !zType ) 
38cbf 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20  continue;..     
38cc0 20 20 20 6e 54 79 70 65 20 3d 20 73 71 6c 69 74     nType = sqlit
38cc1 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65  e3Strlen30(zType
38cc2 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
38cc3 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
38cc4 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65 2c  "hidden", zType,
38cc5 20 36 29 7c 7c 28 7a 54 79 70 65 5b 36 5d 20 26   6)||(zType[6] &
38cc6 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27 20 27 29  & zType[6]!=' ')
38cc7 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66   ){..          f
38cc8 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b  or(i=0; i<nType;
38cc9 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20   i++){..        
38cca 20 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69      if( (0==sqli
38ccb 74 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69  te3StrNICmp(" hi
38ccc 64 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d  dden", &zType[i]
38ccd 2c 20 37 29 29 0d 0a 20 20 20 20 20 20 20 20 20  , 7))..         
38cce 20 20 20 20 26 26 20 28 7a 54 79 70 65 5b 69 2b      && (zType[i+
38ccf 37 5d 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70  7]=='\0' || zTyp
38cd0 65 5b 69 2b 37 5d 3d 3d 27 20 27 29 0d 0a 20 20  e[i+7]==' ')..  
38cd1 20 20 20 20 20 20 20 20 20 20 29 7b 0d 0a 20 20            ){..  
38cd2 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
38cd3 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
38cd4 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
38cd5 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
38cd6 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20   }..        }.. 
38cd7 20 20 20 20 20 20 20 69 66 28 20 69 3c 6e 54 79         if( i<nTy
38cd8 70 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  pe ){..         
38cd9 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 20 20 20   int j;..       
38cda 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20     int nDel = 6 
38cdb 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20  + (zType[i+6] ? 
38cdc 31 20 3a 20 30 29 3b 0d 0a 20 20 20 20 20 20 20  1 : 0);..       
38cdd 20 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e     for(j=i; (j+n
38cde 44 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b  Del)<=nType; j++
38cdf 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
38ce0 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a 54 79 70 65  zType[j] = zType
38ce1 5b 6a 2b 6e 44 65 6c 5d 3b 0d 0a 20 20 20 20 20  [j+nDel];..     
38ce2 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
38ce3 20 20 69 66 28 20 7a 54 79 70 65 5b 69 5d 3d 3d    if( zType[i]==
38ce4 27 5c 30 27 20 26 26 20 69 3e 30 20 29 7b 0d 0a  '\0' && i>0 ){..
38ce5 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
38ce6 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27  rt(zType[i-1]=='
38ce7 20 27 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   ');..          
38ce8 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27    zType[i-1] = '
38ce9 5c 30 27 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  \0';..          
38cea 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 70 54 61  }..          pTa
38ceb 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
38cec 48 69 64 64 65 6e 20 3d 20 31 3b 0d 0a 20 20 20  Hidden = 1;..   
38ced 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
38cee 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
38cef 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
38cf0 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b  b, zModuleName);
38cf1 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
38cf2 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
38cf3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
38cf4 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73  oked by the pars
38cf5 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78  er to call the x
38cf6 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64  Connect() method
38cf7 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74  ..** of the virt
38cf8 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20  ual table pTab. 
38cf9 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
38cfa 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rs, an error cod
38cfb 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 0d 0a  e is returned ..
38cfc 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ** and an error 
38cfd 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0d  left in pParse..
38cfe 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 63 61 6c  .**..** This cal
38cff 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  l is a no-op if 
38d00 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 6e 6f  table pTab is no
38d01 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  t a virtual tabl
38d02 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  e...*/..SQLITE_P
38d03 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
38d04 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63  e3VtabCallConnec
38d05 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
38d06 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0d 0a   Table *pTab){..
38d07 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
38d08 70 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 63  pParse->db;..  c
38d09 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 3b  onst char *zMod;
38d0a 0d 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  ..  Module *pMod
38d0b 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a  ;..  int rc;....
38d0c 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29    assert( pTab )
38d0d 3b 0d 0a 20 20 69 66 28 20 28 70 54 61 62 2d 3e  ;..  if( (pTab->
38d0e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
38d0f 72 74 75 61 6c 29 3d 3d 30 20 7c 7c 20 73 71 6c  rtual)==0 || sql
38d10 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
38d11 2c 20 70 54 61 62 29 20 29 7b 0d 0a 20 20 20 20  , pTab) ){..    
38d12 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38d13 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c  ;..  }....  /* L
38d14 6f 63 61 74 65 20 74 68 65 20 72 65 71 75 69 72  ocate the requir
38d15 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ed virtual table
38d16 20 6d 6f 64 75 6c 65 20 2a 2f 0d 0a 20 20 7a 4d   module */..  zM
38d17 6f 64 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  od = pTab->azMod
38d18 75 6c 65 41 72 67 5b 30 5d 3b 0d 0a 20 20 70 4d  uleArg[0];..  pM
38d19 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71  od = (Module*)sq
38d1a 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64  lite3HashFind(&d
38d1b 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64  b->aModule, zMod
38d1c 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
38d1d 30 28 7a 4d 6f 64 29 29 3b 0d 0a 0d 0a 20 20 69  0(zMod));....  i
38d1e 66 28 20 21 70 4d 6f 64 20 29 7b 0d 0a 20 20 20  f( !pMod ){..   
38d1f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f   const char *zMo
38d20 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d  dule = pTab->azM
38d21 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0d 0a 20 20  oduleArg[0];..  
38d22 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
38d23 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
38d24 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20  ch module: %s", 
38d25 7a 4d 6f 64 75 6c 65 29 3b 0d 0a 20 20 20 20 72  zModule);..    r
38d26 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
38d27 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
38d28 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b   char *zErr = 0;
38d29 0d 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 43  ..    rc = vtabC
38d2a 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64  allConstructor(d
38d2b 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70  b, pTab, pMod, p
38d2c 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43  Mod->pModule->xC
38d2d 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0d  onnect, &zErr);.
38d2e 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
38d2f 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
38d30 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
38d31 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a  (pParse, "%s", z
38d32 45 72 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Err);..    }..  
38d33 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
38d34 64 62 2c 20 7a 45 72 72 29 3b 0d 0a 20 20 7d 0d  db, zErr);..  }.
38d35 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
38d36 0a 7d 0d 0a 2f 2a 0d 0a 2a 2a 20 47 72 6f 77 20  .}../*..** Grow 
38d37 74 68 65 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  the db->aVTrans[
38d38 5d 20 61 72 72 61 79 20 73 6f 20 74 68 61 74 20  ] array so that 
38d39 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 66 6f  there is room fo
38d3a 72 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 0d 0a  r at least one..
38d3b 2a 2a 20 6d 6f 72 65 20 76 2d 74 61 62 6c 65 2e  ** more v-table.
38d3c 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   Return SQLITE_N
38d3d 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63  OMEM if a malloc
38d3e 20 66 61 69 6c 73 2c 20 6f 72 20 53 51 4c 49 54   fails, or SQLIT
38d3f 45 5f 4f 4b 20 6f 74 68 65 72 77 69 73 65 2e 0d  E_OK otherwise..
38d40 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
38d41 67 72 6f 77 56 54 72 61 6e 73 28 73 71 6c 69 74  growVTrans(sqlit
38d42 65 33 20 2a 64 62 29 7b 0d 0a 20 20 63 6f 6e 73  e3 *db){..  cons
38d43 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e 43 52  t int ARRAY_INCR
38d44 20 3d 20 35 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 72   = 5;....  /* Gr
38d45 6f 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  ow the sqlite3.a
38d46 56 54 72 61 6e 73 20 61 72 72 61 79 20 69 66 20  VTrans array if 
38d47 72 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 69  required */..  i
38d48 66 28 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25  f( (db->nVTrans%
38d49 41 52 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29  ARRAY_INCR)==0 )
38d4a 7b 0d 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 2a  {..    VTable **
38d4b 61 56 54 72 61 6e 73 3b 0d 0a 20 20 20 20 69 6e  aVTrans;..    in
38d4c 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f  t nBytes = sizeo
38d4d 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  f(sqlite3_vtab *
38d4e 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73  ) * (db->nVTrans
38d4f 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29 3b 0d   + ARRAY_INCR);.
38d50 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20 73  .    aVTrans = s
38d51 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
38d52 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d 3e  db, (void *)db->
38d53 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73 29  aVTrans, nBytes)
38d54 3b 0d 0a 20 20 20 20 69 66 28 20 21 61 56 54 72  ;..    if( !aVTr
38d55 61 6e 73 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  ans ){..      re
38d56 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
38d57 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 6d  M;..    }..    m
38d58 65 6d 73 65 74 28 26 61 56 54 72 61 6e 73 5b 64  emset(&aVTrans[d
38d59 62 2d 3e 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20  b->nVTrans], 0, 
38d5a 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
38d5b 74 61 62 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43  tab *)*ARRAY_INC
38d5c 52 29 3b 0d 0a 20 20 20 20 64 62 2d 3e 61 56 54  R);..    db->aVT
38d5d 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0d  rans = aVTrans;.
38d5e 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
38d5f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
38d60 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 74 68 65  ../*..** Add the
38d61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
38d62 56 54 61 62 20 74 6f 20 74 68 65 20 61 72 72 61  VTab to the arra
38d63 79 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  y sqlite3.aVTran
38d64 73 5b 5d 2e 20 53 70 61 63 65 20 73 68 6f 75 6c  s[]. Space shoul
38d65 64 0d 0a 2a 2a 20 68 61 76 65 20 61 6c 72 65 61  d..** have alrea
38d66 64 79 20 62 65 65 6e 20 72 65 73 65 72 76 65 64  dy been reserved
38d67 20 75 73 69 6e 67 20 67 72 6f 77 56 54 72 61 6e   using growVTran
38d68 73 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  s()...*/..static
38d69 20 76 6f 69 64 20 61 64 64 54 6f 56 54 72 61 6e   void addToVTran
38d6a 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56  s(sqlite3 *db, V
38d6b 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0d 0a  Table *pVTab){..
38d6c 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20 74    /* Add pVtab t
38d6d 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c  o the end of sql
38d6e 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0d  ite3.aVTrans */.
38d6f 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64  .  db->aVTrans[d
38d70 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20  b->nVTrans++] = 
38d71 70 56 54 61 62 3b 0d 0a 20 20 73 71 6c 69 74 65  pVTab;..  sqlite
38d72 33 56 74 61 62 4c 6f 63 6b 28 70 56 54 61 62 29  3VtabLock(pVTab)
38d73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
38d74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
38d75 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76  invoked by the v
38d76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  dbe to call the 
38d77 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0d 0a  xCreate method..
38d78 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ** of the virtua
38d79 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54  l table named zT
38d7a 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ab in database i
38d7b 44 62 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  Db. ..**..** If 
38d7c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
38d7d 20 2a 70 7a 45 72 72 20 69 73 20 73 65 74 20 74   *pzErr is set t
38d7e 6f 20 70 6f 69 6e 74 20 61 6e 20 61 6e 20 45 6e  o point an an En
38d7f 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 0d 0a  glish language..
38d80 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f  ** description o
38d81 66 20 74 68 65 20 65 72 72 6f 72 20 61 6e 64 20  f the error and 
38d82 61 6e 20 53 51 4c 49 54 45 5f 58 58 58 20 65 72  an SQLITE_XXX er
38d83 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
38d84 72 6e 65 64 2e 0d 0a 2a 2a 20 49 6e 20 74 68 69  rned...** In thi
38d85 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65  s case the calle
38d86 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69  r must call sqli
38d87 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
38d88 6f 6e 20 2a 70 7a 45 72 72 2e 0d 0a 2a 2f 0d 0a  on *pzErr...*/..
38d89 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
38d8a 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
38d8b 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
38d8c 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
38d8d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
38d8e 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0d   char **pzErr){.
38d8f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
38d90 54 45 5f 4f 4b 3b 0d 0a 20 20 54 61 62 6c 65 20  TE_OK;..  Table 
38d91 2a 70 54 61 62 3b 0d 0a 20 20 4d 6f 64 75 6c 65  *pTab;..  Module
38d92 20 2a 70 4d 6f 64 3b 0d 0a 20 20 63 6f 6e 73 74   *pMod;..  const
38d93 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0d 0a 0d 0a   char *zMod;....
38d94 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
38d95 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54  FindTable(db, zT
38d96 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  ab, db->aDb[iDb]
38d97 2e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 61 73 73 65  .zName);..  asse
38d98 72 74 28 20 70 54 61 62 20 26 26 20 28 70 54 61  rt( pTab && (pTa
38d99 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
38d9a 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 26 26 20  _Virtual)!=0 && 
38d9b 21 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 29  !pTab->pVTable )
38d9c 3b 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 63 61 74 65  ;....  /* Locate
38d9d 20 74 68 65 20 72 65 71 75 69 72 65 64 20 76 69   the required vi
38d9e 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
38d9f 6c 65 20 2a 2f 0d 0a 20 20 7a 4d 6f 64 20 3d 20  le */..  zMod = 
38da0 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
38da1 67 5b 30 5d 3b 0d 0a 20 20 70 4d 6f 64 20 3d 20  g[0];..  pMod = 
38da2 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33  (Module*)sqlite3
38da3 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d  HashFind(&db->aM
38da4 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c  odule, zMod, sql
38da5 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f  ite3Strlen30(zMo
38da6 64 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  d));....  /* If 
38da7 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 62  the module has b
38da8 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 20 61  een registered a
38da9 6e 64 20 69 6e 63 6c 75 64 65 73 20 61 20 43 72  nd includes a Cr
38daa 65 61 74 65 20 6d 65 74 68 6f 64 2c 20 0d 0a 20  eate method, .. 
38dab 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e 6f   ** invoke it no
38dac 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65  w. If the module
38dad 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 72 65   has not been re
38dae 67 69 73 74 65 72 65 64 2c 20 72 65 74 75 72 6e  gistered, return
38daf 20 61 6e 20 0d 0a 20 20 2a 2a 20 65 72 72 6f 72   an ..  ** error
38db0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f 20  . Otherwise, do 
38db1 6e 6f 74 68 69 6e 67 2e 0d 0a 20 20 2a 2f 0d 0a  nothing...  */..
38db2 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0d 0a    if( !pMod ){..
38db3 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
38db4 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
38db5 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a  "no such module:
38db6 20 25 73 22 2c 20 7a 4d 6f 64 29 3b 0d 0a 20 20   %s", zMod);..  
38db7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
38db8 52 4f 52 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ROR;..  }else{..
38db9 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c      rc = vtabCal
38dba 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c  lConstructor(db,
38dbb 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f   pTab, pMod, pMo
38dbc 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65  d->pModule->xCre
38dbd 61 74 65 2c 20 70 7a 45 72 72 29 3b 0d 0a 20 20  ate, pzErr);..  
38dbe 7d 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 73 74 69 66  }....  /* Justif
38dbf 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59  ication of ALWAY
38dc0 53 28 29 3a 20 20 54 68 65 20 78 43 6f 6e 73 74  S():  The xConst
38dc1 72 75 63 74 6f 72 20 6d 65 74 68 6f 64 20 69 73  ructor method is
38dc2 20 72 65 71 75 69 72 65 64 20 74 6f 0d 0a 20 20   required to..  
38dc3 2a 2a 20 63 72 65 61 74 65 20 61 20 76 61 6c 69  ** create a vali
38dc4 64 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 69  d sqlite3_vtab i
38dc5 66 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c  f it returns SQL
38dc6 49 54 45 5f 4f 4b 2e 20 2a 2f 0d 0a 20 20 69 66  ITE_OK. */..  if
38dc7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
38dc8 26 26 20 41 4c 57 41 59 53 28 73 71 6c 69 74 65  && ALWAYS(sqlite
38dc9 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
38dca 54 61 62 29 29 20 29 7b 0d 0a 20 20 20 20 72 63  Tab)) ){..    rc
38dcb 20 3d 20 67 72 6f 77 56 54 72 61 6e 73 28 64 62   = growVTrans(db
38dcc 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  );..    if( rc==
38dcd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
38dce 20 20 20 20 61 64 64 54 6f 56 54 72 61 6e 73 28      addToVTrans(
38dcf 64 62 2c 20 73 71 6c 69 74 65 33 47 65 74 56 54  db, sqlite3GetVT
38dd0 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 29 3b  able(db, pTab));
38dd1 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
38dd2 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
38dd3 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
38dd4 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
38dd5 74 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d  to set the schem
38dd6 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  a of a virtual t
38dd7 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c  able.  It is onl
38dd8 79 0d 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63  y..** valid to c
38dd9 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
38dda 6e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  n from within th
38ddb 65 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78  e xCreate() or x
38ddc 43 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0d 0a  Connect() of a..
38ddd 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
38dde 20 6d 6f 64 75 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51   module...*/..SQ
38ddf 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
38de0 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
38de1 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  b(sqlite3 *db, c
38de2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 72 65 61  onst char *zCrea
38de3 74 65 54 61 62 6c 65 29 7b 0d 0a 20 20 50 61 72  teTable){..  Par
38de4 73 65 20 2a 70 50 61 72 73 65 3b 0d 0a 0d 0a 20  se *pParse;.... 
38de5 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
38de6 5f 4f 4b 3b 0d 0a 20 20 54 61 62 6c 65 20 2a 70  _OK;..  Table *p
38de7 54 61 62 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 45  Tab;..  char *zE
38de8 72 72 20 3d 20 30 3b 0d 0a 0d 0a 20 20 73 71 6c  rr = 0;....  sql
38de9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
38dea 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
38deb 69 66 28 20 21 64 62 2d 3e 70 56 74 61 62 43 74  if( !db->pVtabCt
38dec 78 20 7c 7c 20 21 28 70 54 61 62 20 3d 20 64 62  x || !(pTab = db
38ded 2d 3e 70 56 74 61 62 43 74 78 2d 3e 70 54 61 62  ->pVtabCtx->pTab
38dee 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  ) ){..    sqlite
38def 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
38df0 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0d 0a 20  E_MISUSE, 0);.. 
38df1 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
38df2 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
38df3 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  );..    return S
38df4 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50  QLITE_MISUSE_BKP
38df5 54 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  T;..  }..  asser
38df6 74 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  t( (pTab->tabFla
38df7 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
38df8 21 3d 30 20 29 3b 0d 0a 0d 0a 20 20 70 50 61 72  !=0 );....  pPar
38df9 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
38dfa 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  kAllocZero(db, s
38dfb 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
38dfc 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d  ..  if( pParse==
38dfd 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  0 ){..    rc = S
38dfe 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
38dff 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 50 61 72  }else{..    pPar
38e00 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20  se->declareVtab 
38e01 3d 20 31 3b 0d 0a 20 20 20 20 70 50 61 72 73 65  = 1;..    pParse
38e02 2d 3e 64 62 20 3d 20 64 62 3b 0d 0a 20 20 20 20  ->db = db;..    
38e03 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f  pParse->nQueryLo
38e04 6f 70 20 3d 20 31 3b 0d 0a 20 20 0d 0a 20 20 20  op = 1;..  ..   
38e05 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
38e06 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
38e07 28 70 50 61 72 73 65 2c 20 7a 43 72 65 61 74 65  (pParse, zCreate
38e08 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20 0d 0a  Table, &zErr) ..
38e09 20 20 20 20 20 26 26 20 70 50 61 72 73 65 2d 3e       && pParse->
38e0a 70 4e 65 77 54 61 62 6c 65 0d 0a 20 20 20 20 20  pNewTable..     
38e0b 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  && !db->mallocFa
38e0c 69 6c 65 64 0d 0a 20 20 20 20 20 26 26 20 21 70  iled..     && !p
38e0d 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
38e0e 2d 3e 70 53 65 6c 65 63 74 0d 0a 20 20 20 20 20  ->pSelect..     
38e0f 26 26 20 28 70 50 61 72 73 65 2d 3e 70 4e 65 77  && (pParse->pNew
38e10 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20  Table->tabFlags 
38e11 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30  & TF_Virtual)==0
38e12 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20  ..    ){..      
38e13 69 66 28 20 21 70 54 61 62 2d 3e 61 43 6f 6c 20  if( !pTab->aCol 
38e14 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 54 61 62  ){..        pTab
38e15 2d 3e 61 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d  ->aCol = pParse-
38e16 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c  >pNewTable->aCol
38e17 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ;..        pTab-
38e18 3e 6e 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e  >nCol = pParse->
38e19 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b  pNewTable->nCol;
38e1a 0d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ..        pParse
38e1b 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f  ->pNewTable->nCo
38e1c 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  l = 0;..        
38e1d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
38e1e 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0d 0a 20 20  e->aCol = 0;..  
38e1f 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 64 62 2d      }..      db-
38e20 3e 70 56 74 61 62 43 74 78 2d 3e 70 54 61 62 20  >pVtabCtx->pTab 
38e21 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  = 0;..    }else{
38e22 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
38e23 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
38e24 45 52 52 4f 52 2c 20 28 7a 45 72 72 20 3f 20 22  ERROR, (zErr ? "
38e25 25 73 22 20 3a 20 30 29 2c 20 7a 45 72 72 29 3b  %s" : 0), zErr);
38e26 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ..      sqlite3D
38e27 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
38e28 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
38e29 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20  ITE_ERROR;..    
38e2a 7d 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  }..    pParse->d
38e2b 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0d  eclareVtab = 0;.
38e2c 0a 20 20 0d 0a 20 20 20 20 69 66 28 20 70 50 61  .  ..    if( pPa
38e2d 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0d 0a 20  rse->pVdbe ){.. 
38e2e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
38e2f 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d  Finalize(pParse-
38e30 3e 70 56 64 62 65 29 3b 0d 0a 20 20 20 20 7d 0d  >pVdbe);..    }.
38e31 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
38e32 74 65 54 61 62 6c 65 28 64 62 2c 20 70 50 61 72  teTable(db, pPar
38e33 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0d  se->pNewTable);.
38e34 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63  .    sqlite3Stac
38e35 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  kFree(db, pParse
38e36 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73  );..  }....  ass
38e37 65 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d  ert( (rc&0xff)==
38e38 72 63 20 29 3b 0d 0a 20 20 72 63 20 3d 20 73 71  rc );..  rc = sq
38e39 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
38e3a 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33   rc);..  sqlite3
38e3b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
38e3c 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75  >mutex);..  retu
38e3d 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
38e3e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
38e3f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20  n is invoked by 
38e40 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c  the vdbe to call
38e41 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65   the xDestroy me
38e42 74 68 6f 64 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  thod..** of the 
38e43 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61  virtual table na
38e44 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61  med zTab in data
38e45 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f  base iDb. This o
38e46 63 63 75 72 73 0d 0a 2a 2a 20 77 68 65 6e 20 61  ccurs..** when a
38e47 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d   DROP TABLE is m
38e48 65 6e 74 69 6f 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a  entioned...**..*
38e49 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * This call is a
38e4a 20 6e 6f 2d 6f 70 20 69 66 20 7a 54 61 62 20 69   no-op if zTab i
38e4b 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20  s not a virtual 
38e4c 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  table...*/..SQLI
38e4d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
38e4e 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65  qlite3VtabCallDe
38e4f 73 74 72 6f 79 28 73 71 6c 69 74 65 33 20 2a 64  stroy(sqlite3 *d
38e50 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
38e51 74 20 63 68 61 72 20 2a 7a 54 61 62 29 7b 0d 0a  t char *zTab){..
38e52 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
38e53 45 5f 4f 4b 3b 0d 0a 20 20 54 61 62 6c 65 20 2a  E_OK;..  Table *
38e54 70 54 61 62 3b 0d 0a 0d 0a 20 20 70 54 61 62 20  pTab;....  pTab 
38e55 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
38e56 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d  le(db, zTab, db-
38e57 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
38e58 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ;..  if( ALWAYS(
38e59 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d  pTab!=0 && pTab-
38e5a 3e 70 56 54 61 62 6c 65 21 3d 30 29 20 29 7b 0d  >pVTable!=0) ){.
38e5b 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 70 20 3d  .    VTable *p =
38e5c 20 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41   vtabDisconnectA
38e5d 6c 6c 28 64 62 2c 20 70 54 61 62 29 3b 0d 0a 0d  ll(db, pTab);...
38e5e 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
38e5f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20  =SQLITE_OK );.. 
38e60 20 20 20 72 63 20 3d 20 70 2d 3e 70 4d 6f 64 2d     rc = p->pMod-
38e61 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72  >pModule->xDestr
38e62 6f 79 28 70 2d 3e 70 56 74 61 62 29 3b 0d 0a 0d  oy(p->pVtab);...
38e63 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  .    /* Remove t
38e64 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a  he sqlite3_vtab*
38e65 20 66 72 6f 6d 20 74 68 65 20 61 56 54 72 61 6e   from the aVTran
38e66 73 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 61 70  s[] array, if ap
38e67 70 6c 69 63 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  plicable */..   
38e68 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
38e69 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  OK ){..      ass
38e6a 65 72 74 28 20 70 54 61 62 2d 3e 70 56 54 61 62  ert( pTab->pVTab
38e6b 6c 65 3d 3d 70 20 26 26 20 70 2d 3e 70 4e 65 78  le==p && p->pNex
38e6c 74 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70  t==0 );..      p
38e6d 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0d 0a 20 20  ->pVtab = 0;..  
38e6e 20 20 20 20 70 54 61 62 2d 3e 70 56 54 61 62 6c      pTab->pVTabl
38e6f 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71  e = 0;..      sq
38e70 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
38e71 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  p);..    }..  }.
38e72 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
38e73 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
38e74 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b  s function invok
38e75 65 73 20 65 69 74 68 65 72 20 74 68 65 20 78 52  es either the xR
38e76 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f 6d 6d  ollback or xComm
38e77 69 74 20 6d 65 74 68 6f 64 0d 0a 2a 2a 20 6f 66  it method..** of
38e78 20 65 61 63 68 20 6f 66 20 74 68 65 20 76 69 72   each of the vir
38e79 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74  tual tables in t
38e7a 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
38e7b 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20 6d 65  ns array. The me
38e7c 74 68 6f 64 0d 0a 2a 2a 20 63 61 6c 6c 65 64 20  thod..** called 
38e7d 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
38e7e 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
38e7f 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c 20  ment, "offset", 
38e80 77 68 69 63 68 20 69 73 0d 0a 2a 2a 20 74 68 65  which is..** the
38e81 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d   offset of the m
38e82 65 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e  ethod to call in
38e83 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64   the sqlite3_mod
38e84 75 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0d 0a  ule structure...
38e85 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 72 72 61 79  **..** The array
38e86 20 69 73 20 63 6c 65 61 72 65 64 20 61 66 74 65   is cleared afte
38e87 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63  r invoking the c
38e88 61 6c 6c 62 61 63 6b 73 2e 20 0d 0a 2a 2f 0d 0a  allbacks. ..*/..
38e89 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c  static void call
38e8a 46 69 6e 61 6c 69 73 65 72 28 73 71 6c 69 74 65  Finaliser(sqlite
38e8b 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66 66 73 65  3 *db, int offse
38e8c 74 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20  t){..  int i;.. 
38e8d 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73   if( db->aVTrans
38e8e 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   ){..    for(i=0
38e8f 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b  ; i<db->nVTrans;
38e90 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 56 54   i++){..      VT
38e91 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 64 62  able *pVTab = db
38e92 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0d 0a 20  ->aVTrans[i];.. 
38e93 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
38e94 62 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70 56  b *p = pVTab->pV
38e95 74 61 62 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  tab;..      if( 
38e96 70 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  p ){..        in
38e97 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76  t (*x)(sqlite3_v
38e98 74 61 62 20 2a 29 3b 0d 0a 20 20 20 20 20 20 20  tab *);..       
38e99 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a 2a 29 28   x = *(int (**)(
38e9a 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 29  sqlite3_vtab *))
38e9b 28 28 63 68 61 72 20 2a 29 70 2d 3e 70 4d 6f 64  ((char *)p->pMod
38e9c 75 6c 65 20 2b 20 6f 66 66 73 65 74 29 3b 0d 0a  ule + offset);..
38e9d 20 20 20 20 20 20 20 20 69 66 28 20 78 20 29 20          if( x ) 
38e9e 78 28 70 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  x(p);..      }..
38e9f 20 20 20 20 20 20 70 56 54 61 62 2d 3e 69 53 61        pVTab->iSa
38ea0 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0d 0a 20 20  vepoint = 0;..  
38ea1 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55      sqlite3VtabU
38ea2 6e 6c 6f 63 6b 28 70 56 54 61 62 29 3b 0d 0a 20  nlock(pVTab);.. 
38ea3 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
38ea4 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e  3DbFree(db, db->
38ea5 61 56 54 72 61 6e 73 29 3b 0d 0a 20 20 20 20 64  aVTrans);..    d
38ea6 62 2d 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0d  b->nVTrans = 0;.
38ea7 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73  .    db->aVTrans
38ea8 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d   = 0;..  }..}...
38ea9 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ./*..** Invoke t
38eaa 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20  he xSync method 
38eab 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74  of all virtual t
38eac 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c  ables in the sql
38ead 69 74 65 33 2e 61 56 54 72 61 6e 73 0d 0a 2a 2a  ite3.aVTrans..**
38eae 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74   array. Return t
38eaf 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f  he error code fo
38eb0 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f  r the first erro
38eb1 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c 20 6f  r that occurs, o
38eb2 72 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  r..** SQLITE_OK 
38eb3 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65  if all xSync ope
38eb4 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
38eb5 65 73 73 66 75 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  essful...**..** 
38eb6 53 65 74 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f  Set *pzErrmsg to
38eb7 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
38eb8 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  er that should b
38eb9 65 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67  e released using
38eba 20 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 46   ..** sqlite3DbF
38ebb 72 65 65 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67  ree() containing
38ebc 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
38ebd 65 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61  e, if one is ava
38ebe 69 6c 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  ilable...*/..SQL
38ebf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
38ec0 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
38ec1 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61  sqlite3 *db, cha
38ec2 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b 0d 0a  r **pzErrmsg){..
38ec3 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74 20    int i;..  int 
38ec4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
38ec5 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72  .  VTable **aVTr
38ec6 61 6e 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e  ans = db->aVTran
38ec7 73 3b 0d 0a 0d 0a 20 20 64 62 2d 3e 61 56 54 72  s;....  db->aVTr
38ec8 61 6e 73 20 3d 20 30 3b 0d 0a 20 20 66 6f 72 28  ans = 0;..  for(
38ec9 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
38eca 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72  OK && i<db->nVTr
38ecb 61 6e 73 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  ans; i++){..    
38ecc 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33  int (*x)(sqlite3
38ecd 5f 76 74 61 62 20 2a 29 3b 0d 0a 20 20 20 20 73  _vtab *);..    s
38ece 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
38ecf 61 62 20 3d 20 61 56 54 72 61 6e 73 5b 69 5d 2d  ab = aVTrans[i]-
38ed0 3e 70 56 74 61 62 3b 0d 0a 20 20 20 20 69 66 28  >pVtab;..    if(
38ed1 20 70 56 74 61 62 20 26 26 20 28 78 20 3d 20 70   pVtab && (x = p
38ed2 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
38ed3 53 79 6e 63 29 21 3d 30 20 29 7b 0d 0a 20 20 20  Sync)!=0 ){..   
38ed4 20 20 20 72 63 20 3d 20 78 28 70 56 74 61 62 29     rc = x(pVtab)
38ed5 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
38ed6 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 45 72  DbFree(db, *pzEr
38ed7 72 6d 73 67 29 3b 0d 0a 20 20 20 20 20 20 2a 70  rmsg);..      *p
38ed8 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74 65  zErrmsg = sqlite
38ed9 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 56  3DbStrDup(db, pV
38eda 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d 0a  tab->zErrMsg);..
38edb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
38edc 65 65 28 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ee(pVtab->zErrMs
38edd 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  g);..    }..  }.
38ede 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d  .  db->aVTrans =
38edf 20 61 56 54 72 61 6e 73 3b 0d 0a 20 20 72 65 74   aVTrans;..  ret
38ee0 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
38ee1 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  ..** Invoke the 
38ee2 78 52 6f 6c 6c 62 61 63 6b 20 6d 65 74 68 6f 64  xRollback method
38ee3 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20   of all virtual 
38ee4 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0d 0a  tables in the ..
38ee5 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  ** sqlite3.aVTra
38ee6 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63  ns array. Then c
38ee7 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20 69  lear the array i
38ee8 74 73 65 6c 66 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  tself...*/..SQLI
38ee9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
38eea 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
38eeb 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ck(sqlite3 *db){
38eec 0d 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65  ..  callFinalise
38eed 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73  r(db, offsetof(s
38eee 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 52  qlite3_module,xR
38eef 6f 6c 6c 62 61 63 6b 29 29 3b 0d 0a 20 20 72 65  ollback));..  re
38ef0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
38ef1 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76  .}..../*..** Inv
38ef2 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74 20  oke the xCommit 
38ef3 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69  method of all vi
38ef4 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
38ef5 74 68 65 20 0d 0a 2a 2a 20 73 71 6c 69 74 65 33  the ..** sqlite3
38ef6 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20  .aVTrans array. 
38ef7 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61  Then clear the a
38ef8 72 72 61 79 20 69 74 73 65 6c 66 2e 0d 0a 2a 2f  rray itself...*/
38ef9 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
38efa 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
38efb 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
38efc 64 62 29 7b 0d 0a 20 20 63 61 6c 6c 46 69 6e 61  db){..  callFina
38efd 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74  liser(db, offset
38efe 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  of(sqlite3_modul
38eff 65 2c 78 43 6f 6d 6d 69 74 29 29 3b 0d 0a 20 20  e,xCommit));..  
38f00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38f01 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
38f02 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
38f03 62 6c 65 20 70 56 74 61 62 20 73 75 70 70 6f 72  ble pVtab suppor
38f04 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ts the transacti
38f05 6f 6e 20 69 6e 74 65 72 66 61 63 65 0d 0a 2a 2a  on interface..**
38f06 20 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61   (xBegin/xRollba
38f07 63 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f  ck/xCommit and o
38f08 70 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29  ptionally xSync)
38f09 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69   and a transacti
38f0a 6f 6e 20 69 73 0d 0a 2a 2a 20 6e 6f 74 20 63 75  on is..** not cu
38f0b 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e  rrently open, in
38f0c 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20  voke the xBegin 
38f0d 6d 65 74 68 6f 64 20 6e 6f 77 2e 0d 0a 2a 2a 0d  method now...**.
38f0e 0a 2a 2a 20 49 66 20 74 68 65 20 78 42 65 67 69  .** If the xBegi
38f0f 6e 20 63 61 6c 6c 20 69 73 20 73 75 63 63 65 73  n call is succes
38f10 73 66 75 6c 2c 20 70 6c 61 63 65 20 74 68 65 20  sful, place the 
38f11 73 71 6c 69 74 65 33 5f 76 74 61 62 20 70 6f 69  sqlite3_vtab poi
38f12 6e 74 65 72 0d 0a 2a 2a 20 69 6e 20 74 68 65 20  nter..** in the 
38f13 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20  sqlite3.aVTrans 
38f14 61 72 72 61 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  array...*/..SQLI
38f15 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
38f16 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
38f17 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 54 61  sqlite3 *db, VTa
38f18 62 6c 65 20 2a 70 56 54 61 62 29 7b 0d 0a 20 20  ble *pVTab){..  
38f19 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
38f1a 4f 4b 3b 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c  OK;..  const sql
38f1b 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
38f1c 64 75 6c 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 70  dule;....  /* Sp
38f1d 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 64  ecial case: If d
38f1e 62 2d 3e 61 56 54 72 61 6e 73 20 69 73 20 4e 55  b->aVTrans is NU
38f1f 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54 72 61  LL and db->nVTra
38f20 6e 73 20 69 73 20 67 72 65 61 74 65 72 0d 0a 20  ns is greater.. 
38f21 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74   ** than zero, t
38f22 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
38f23 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65  n is being calle
38f24 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 0d  d from within a.
38f25 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
38f26 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61 6c  dule xSync() cal
38f27 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c  lback. It is ill
38f28 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74 6f  egal to write to
38f29 20 0d 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20   ..  ** virtual 
38f2a 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e  module tables in
38f2b 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72   this case, so r
38f2c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
38f2d 4b 45 44 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  KED...  */..  if
38f2e 28 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53  ( sqlite3VtabInS
38f2f 79 6e 63 28 64 62 29 20 29 7b 0d 0a 20 20 20 20  ync(db) ){..    
38f30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
38f31 43 4b 45 44 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  CKED;..  }..  if
38f32 28 20 21 70 56 54 61 62 20 29 7b 0d 0a 20 20 20  ( !pVTab ){..   
38f33 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
38f34 4b 3b 0d 0a 20 20 7d 20 0d 0a 20 20 70 4d 6f 64  K;..  } ..  pMod
38f35 75 6c 65 20 3d 20 70 56 54 61 62 2d 3e 70 56 74  ule = pVTab->pVt
38f36 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a 0d 0a  ab->pModule;....
38f37 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78    if( pModule->x
38f38 42 65 67 69 6e 20 29 7b 0d 0a 20 20 20 20 69 6e  Begin ){..    in
38f39 74 20 69 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49  t i;....    /* I
38f3a 66 20 70 56 74 61 62 20 69 73 20 61 6c 72 65 61  f pVtab is alrea
38f3b 64 79 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e  dy in the aVTran
38f3c 73 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20  s array, return 
38f3d 65 61 72 6c 79 20 2a 2f 0d 0a 20 20 20 20 66 6f  early */..    fo
38f3e 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54  r(i=0; i<db->nVT
38f3f 72 61 6e 73 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  rans; i++){..   
38f40 20 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61     if( db->aVTra
38f41 6e 73 5b 69 5d 3d 3d 70 56 54 61 62 20 29 7b 0d  ns[i]==pVTab ){.
38f42 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
38f43 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20  SQLITE_OK;..    
38f44 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20    }..    }....  
38f45 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
38f46 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 2e 20 49  xBegin method. I
38f47 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 64  f successful, ad
38f48 64 20 74 68 65 20 76 74 61 62 20 74 6f 20 74 68  d the vtab to th
38f49 65 20 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  e ..    ** sqlit
38f4a 65 33 2e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  e3.aVTrans[] arr
38f4b 61 79 2e 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d  ay. */..    rc =
38f4c 20 67 72 6f 77 56 54 72 61 6e 73 28 64 62 29 3b   growVTrans(db);
38f4d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
38f4e 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
38f4f 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
38f50 78 42 65 67 69 6e 28 70 56 54 61 62 2d 3e 70 56  xBegin(pVTab->pV
38f51 74 61 62 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  tab);..      if(
38f52 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
38f53 7b 0d 0a 20 20 20 20 20 20 20 20 61 64 64 54 6f  {..        addTo
38f54 56 54 72 61 6e 73 28 64 62 2c 20 70 56 54 61 62  VTrans(db, pVTab
38f55 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
38f56 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72   }..  }..  retur
38f57 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
38f58 2a 2a 20 49 6e 76 6f 6b 65 20 65 69 74 68 65 72  ** Invoke either
38f59 20 74 68 65 20 78 53 61 76 65 70 6f 69 6e 74 2c   the xSavepoint,
38f5a 20 78 52 6f 6c 6c 62 61 63 6b 54 6f 20 6f 72 20   xRollbackTo or 
38f5b 78 52 65 6c 65 61 73 65 20 6d 65 74 68 6f 64 20  xRelease method 
38f5c 6f 66 20 61 6c 6c 0d 0a 2a 2a 20 76 69 72 74 75  of all..** virtu
38f5d 61 6c 20 74 61 62 6c 65 73 20 74 68 61 74 20 63  al tables that c
38f5e 75 72 72 65 6e 74 6c 79 20 68 61 76 65 20 61 6e  urrently have an
38f5f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f   open transactio
38f60 6e 2e 20 50 61 73 73 20 69 53 61 76 65 70 6f 69  n. Pass iSavepoi
38f61 6e 74 0d 0a 2a 2a 20 61 73 20 74 68 65 20 73 65  nt..** as the se
38f62 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
38f63 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
38f64 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 6b 65  le method invoke
38f65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 6f 70  d...**..** If op
38f66 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 42 45   is SAVEPOINT_BE
38f67 47 49 4e 2c 20 74 68 65 20 78 53 61 76 65 70 6f  GIN, the xSavepo
38f68 69 6e 74 20 6d 65 74 68 6f 64 20 69 73 20 69 6e  int method is in
38f69 76 6f 6b 65 64 2e 20 49 66 20 69 74 20 69 73 0d  voked. If it is.
38f6a 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  .** SAVEPOINT_RO
38f6b 4c 4c 42 41 43 4b 2c 20 74 68 65 20 78 52 6f 6c  LLBACK, the xRol
38f6c 6c 62 61 63 6b 54 6f 20 6d 65 74 68 6f 64 2e 20  lbackTo method. 
38f6d 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 6f 70  Otherwise, if op
38f6e 20 69 73 20 0d 0a 2a 2a 20 53 41 56 45 50 4f 49   is ..** SAVEPOI
38f6f 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e  NT_RELEASE, then
38f70 20 74 68 65 20 78 52 65 6c 65 61 73 65 20 6d 65   the xRelease me
38f71 74 68 6f 64 20 6f 66 20 65 61 63 68 20 76 69 72  thod of each vir
38f72 74 75 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0d  tual table with.
38f73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 74 72 61 6e  .** an open tran
38f74 73 61 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  saction is invok
38f75 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  ed...**..** If a
38f76 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ny virtual table
38f77 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
38f78 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74  an error code ot
38f79 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
38f7a 4f 4b 2c 20 0d 0a 2a 2a 20 70 72 6f 63 65 73 73  OK, ..** process
38f7b 69 6e 67 20 69 73 20 61 62 61 6e 64 6f 6e 65 64  ing is abandoned
38f7c 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 72   and the error r
38f7d 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
38f7e 61 6c 6c 65 72 20 6f 66 20 74 68 69 73 0d 0a 2a  aller of this..*
38f7f 2a 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64  * function immed
38f80 69 61 74 65 6c 79 2e 20 49 66 20 61 6c 6c 20 63  iately. If all c
38f81 61 6c 6c 73 20 74 6f 20 76 69 72 74 75 61 6c 20  alls to virtual 
38f82 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 61 72  table methods ar
38f83 65 20 73 75 63 63 65 73 73 66 75 6c 2c 0d 0a 2a  e successful,..*
38f84 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
38f85 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  eturned...*/..SQ
38f86 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
38f87 20 73 71 6c 69 74 65 33 56 74 61 62 53 61 76 65   sqlite3VtabSave
38f88 70 6f 69 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  point(sqlite3 *d
38f89 62 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69  b, int op, int i
38f8a 53 61 76 65 70 6f 69 6e 74 29 7b 0d 0a 20 20 69  Savepoint){..  i
38f8b 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
38f8c 4b 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  K;....  assert( 
38f8d 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
38f8e 4c 45 41 53 45 7c 7c 6f 70 3d 3d 53 41 56 45 50  LEASE||op==SAVEP
38f8f 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 7c 7c 6f  OINT_ROLLBACK||o
38f90 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  p==SAVEPOINT_BEG
38f91 49 4e 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  IN );..  assert(
38f92 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29   iSavepoint>=0 )
38f93 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 61 56 54  ;..  if( db->aVT
38f94 72 61 6e 73 20 29 7b 0d 0a 20 20 20 20 69 6e 74  rans ){..    int
38f95 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30   i;..    for(i=0
38f96 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
38f97 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73  && i<db->nVTrans
38f98 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 56  ; i++){..      V
38f99 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 64  Table *pVTab = d
38f9a 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0d 0a  b->aVTrans[i];..
38f9b 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69        const sqli
38f9c 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
38f9d 20 3d 20 70 56 54 61 62 2d 3e 70 4d 6f 64 2d 3e   = pVTab->pMod->
38f9e 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 20 20 20 20  pModule;..      
38f9f 69 66 28 20 70 56 54 61 62 2d 3e 70 56 74 61 62  if( pVTab->pVtab
38fa0 20 26 26 20 70 4d 6f 64 2d 3e 69 56 65 72 73 69   && pMod->iVersi
38fa1 6f 6e 3e 3d 32 20 29 7b 0d 0a 20 20 20 20 20 20  on>=2 ){..      
38fa2 20 20 69 6e 74 20 28 2a 78 4d 65 74 68 6f 64 29    int (*xMethod)
38fa3 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c  (sqlite3_vtab *,
38fa4 20 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 20 20   int);..        
38fa5 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0d 0a 20  switch( op ){.. 
38fa6 20 20 20 20 20 20 20 20 20 63 61 73 65 20 53 41           case SA
38fa7 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 3a 0d 0a  VEPOINT_BEGIN:..
38fa8 20 20 20 20 20 20 20 20 20 20 20 20 78 4d 65 74              xMet
38fa9 68 6f 64 20 3d 20 70 4d 6f 64 2d 3e 78 53 61 76  hod = pMod->xSav
38faa 65 70 6f 69 6e 74 3b 0d 0a 20 20 20 20 20 20 20  epoint;..       
38fab 20 20 20 20 20 70 56 54 61 62 2d 3e 69 53 61 76       pVTab->iSav
38fac 65 70 6f 69 6e 74 20 3d 20 69 53 61 76 65 70 6f  epoint = iSavepo
38fad 69 6e 74 2b 31 3b 0d 0a 20 20 20 20 20 20 20 20  int+1;..        
38fae 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
38faf 20 20 20 20 20 20 63 61 73 65 20 53 41 56 45 50        case SAVEP
38fb0 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3a 0d 0a  OINT_ROLLBACK:..
38fb1 20 20 20 20 20 20 20 20 20 20 20 20 78 4d 65 74              xMet
38fb2 68 6f 64 20 3d 20 70 4d 6f 64 2d 3e 78 52 6f 6c  hod = pMod->xRol
38fb3 6c 62 61 63 6b 54 6f 3b 0d 0a 20 20 20 20 20 20  lbackTo;..      
38fb4 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
38fb5 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a          default:
38fb6 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 4d  ..            xM
38fb7 65 74 68 6f 64 20 3d 20 70 4d 6f 64 2d 3e 78 52  ethod = pMod->xR
38fb8 65 6c 65 61 73 65 3b 0d 0a 20 20 20 20 20 20 20  elease;..       
38fb9 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
38fba 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
38fbb 69 66 28 20 78 4d 65 74 68 6f 64 20 26 26 20 70  if( xMethod && p
38fbc 56 54 61 62 2d 3e 69 53 61 76 65 70 6f 69 6e 74  VTab->iSavepoint
38fbd 3e 69 53 61 76 65 70 6f 69 6e 74 20 29 7b 0d 0a  >iSavepoint ){..
38fbe 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 78            rc = x
38fbf 4d 65 74 68 6f 64 28 70 56 54 61 62 2d 3e 70 56  Method(pVTab->pV
38fc0 74 61 62 2c 20 69 53 61 76 65 70 6f 69 6e 74 29  tab, iSavepoint)
38fc1 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
38fc2 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
38fc3 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
38fc4 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  .}..../*..** The
38fc5 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
38fc6 20 28 70 44 65 66 29 20 69 73 20 61 20 66 75 6e   (pDef) is a fun
38fc7 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
38fc8 74 69 6f 6e 2e 20 20 54 68 65 0d 0a 2a 2a 20 73  tion.  The..** s
38fc9 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
38fca 28 70 45 78 70 72 29 20 69 73 20 74 68 65 20 66  (pExpr) is the f
38fcb 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
38fcc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d   this function..
38fcd 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20  .** If pExpr is 
38fce 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69  a column in a vi
38fcf 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 74 68 65  rtual table, the
38fd0 6e 20 6c 65 74 20 74 68 65 20 76 69 72 74 75 61  n let the virtua
38fd1 6c 0d 0a 2a 2a 20 74 61 62 6c 65 20 69 6d 70 6c  l..** table impl
38fd2 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 76 65 20  ementation have 
38fd3 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  an opportunity t
38fd4 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66  o overload the f
38fd5 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a  unction...**..**
38fd6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
38fd7 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77 20 76   used to allow v
38fd8 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
38fd9 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 0d  lementations to.
38fda 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41 54  .** overload MAT
38fdb 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20  CH, LIKE, GLOB, 
38fdc 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72 61  and REGEXP opera
38fdd 74 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65  tors...**..** Re
38fde 74 75 72 6e 20 65 69 74 68 65 72 20 74 68 65 20  turn either the 
38fdf 70 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69  pDef argument (i
38fe0 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61  ndicating no cha
38fe1 6e 67 65 29 20 6f 72 20 61 20 0d 0a 2a 2a 20 6e  nge) or a ..** n
38fe2 65 77 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ew FuncDef struc
38fe3 74 75 72 65 20 74 68 61 74 20 69 73 20 6d 61 72  ture that is mar
38fe4 6b 65 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c  ked as ephemeral
38fe5 20 75 73 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 53   using the..** S
38fe6 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
38fe7 20 66 6c 61 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49   flag...*/..SQLI
38fe8 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44  TE_PRIVATE FuncD
38fe9 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f  ef *sqlite3VtabO
38fea 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
38feb 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
38fec 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
38fed 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 72  connection for r
38fee 65 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f 63 20  eporting malloc 
38fef 70 72 6f 62 6c 65 6d 73 20 2a 2f 0d 0a 20 20 46  problems */..  F
38ff0 75 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20 2f  uncDef *pDef,  /
38ff1 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f  * Function to po
38ff2 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20  ssibly overload 
38ff3 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20  */..  int nArg, 
38ff4 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
38ff5 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
38ff6 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d  the function */.
38ff7 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
38ff8 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
38ff9 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63  ment to the func
38ffa 74 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54  tion */..){..  T
38ffb 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a 20 20 73  able *pTab;..  s
38ffc 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
38ffd 61 62 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ab;..  sqlite3_m
38ffe 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0d 0a 20 20  odule *pMod;..  
38fff 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
39000 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
39001 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
39002 2a 2a 29 20 3d 20 30 3b 0d 0a 20 20 76 6f 69 64  **) = 0;..  void
39003 20 2a 70 41 72 67 20 3d 20 30 3b 0d 0a 20 20 46   *pArg = 0;..  F
39004 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0d 0a 20  uncDef *pNew;.. 
39005 20 69 6e 74 20 72 63 20 3d 20 30 3b 0d 0a 20 20   int rc = 0;..  
39006 63 68 61 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65  char *zLowerName
39007 3b 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
39008 61 72 20 2a 7a 3b 0d 0a 0d 0a 0d 0a 20 20 2f 2a  ar *z;......  /*
39009 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 74 68   Check to see th
3900a 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69  e left operand i
3900b 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20  s a column in a 
3900c 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
3900d 0d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45  ..  if( NEVER(pE
3900e 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  xpr==0) ) return
3900f 20 70 44 65 66 3b 0d 0a 20 20 69 66 28 20 70 45   pDef;..  if( pE
39010 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
39011 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  MN ) return pDef
39012 3b 0d 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70  ;..  pTab = pExp
39013 72 2d 3e 70 54 61 62 3b 0d 0a 20 20 69 66 28 20  r->pTab;..  if( 
39014 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29  NEVER(pTab==0) )
39015 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0d 0a 20   return pDef;.. 
39016 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46   if( (pTab->tabF
39017 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
39018 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70  l)==0 ) return p
39019 44 65 66 3b 0d 0a 20 20 70 56 74 61 62 20 3d 20  Def;..  pVtab = 
3901a 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65  sqlite3GetVTable
3901b 28 64 62 2c 20 70 54 61 62 29 2d 3e 70 56 74 61  (db, pTab)->pVta
3901c 62 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 56  b;..  assert( pV
3901d 74 61 62 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73  tab!=0 );..  ass
3901e 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64  ert( pVtab->pMod
3901f 75 6c 65 21 3d 30 20 29 3b 0d 0a 20 20 70 4d 6f  ule!=0 );..  pMo
39020 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64  d = (sqlite3_mod
39021 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f  ule *)pVtab->pMo
39022 64 75 6c 65 3b 0d 0a 20 20 69 66 28 20 70 4d 6f  dule;..  if( pMo
39023 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  d->xFindFunction
39024 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44 65  ==0 ) return pDe
39025 66 3b 0d 0a 20 0d 0a 20 20 2f 2a 20 43 61 6c 6c  f;.. ..  /* Call
39026 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69   the xFindFuncti
39027 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65  on method on the
39028 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
39029 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a 20  mplementation.. 
3902a 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68   ** to see if th
3902b 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3902c 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c 6f   wants to overlo
3902d 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ad this function
3902e 20 0d 0a 20 20 2a 2f 0d 0a 20 20 7a 4c 6f 77 65   ..  */..  zLowe
3902f 72 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  rName = sqlite3D
39030 62 53 74 72 44 75 70 28 64 62 2c 20 70 44 65 66  bStrDup(db, pDef
39031 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 69 66 28  ->zName);..  if(
39032 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0d 0a   zLowerName ){..
39033 20 20 20 20 2f 2f 66 6f 72 28 7a 3d 28 75 6e 73      //for(z=(uns
39034 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f 77  igned char*)zLow
39035 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b 29  erName; *z; z++)
39036 7b 0d 0a 20 20 20 20 2f 2f 20 20 2a 7a 20 3d 20  {..    //  *z = 
39037 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
39038 77 65 72 5b 2a 7a 5d 3b 0d 0a 20 20 20 20 2f 2f  wer[*z];..    //
39039 7d 0d 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  }..    rc = pMod
3903a 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  ->xFindFunction(
3903b 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 7a 4c 6f  pVtab, nArg, zLo
3903c 77 65 72 4e 61 6d 65 2c 20 26 78 46 75 6e 63 2c  werName, &xFunc,
3903d 20 26 70 41 72 67 29 3b 0d 0a 20 20 20 20 73 71   &pArg);..    sq
3903e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3903f 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0d 0a 20 20  zLowerName);..  
39040 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  }..  if( rc==0 )
39041 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44  {..    return pD
39042 65 66 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ef;..  }....  /*
39043 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 70   Create a new ep
39044 68 65 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e  hemeral function
39045 20 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20   definition for 
39046 74 68 65 20 6f 76 65 72 6c 6f 61 64 65 64 0d 0a  the overloaded..
39047 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f    ** function */
39048 0d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ..  pNew = sqlit
39049 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
3904a 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
3904b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3904c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b                 +
3904d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
3904e 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 2b 20  (pDef->zName) + 
3904f 31 29 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 3d  1);..  if( pNew=
39050 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
39051 6e 20 70 44 65 66 3b 0d 0a 20 20 7d 0d 0a 20 20  n pDef;..  }..  
39052 2a 70 4e 65 77 20 3d 20 2a 70 44 65 66 3b 0d 0a  *pNew = *pDef;..
39053 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20    pNew->zName = 
39054 28 63 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d  (char *)&pNew[1]
39055 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77  ;..  memcpy(pNew
39056 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 7a  ->zName, pDef->z
39057 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72  Name, sqlite3Str
39058 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d  len30(pDef->zNam
39059 65 29 2b 31 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e  e)+1);..  pNew->
3905a 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0d 0a  xFunc = xFunc;..
3905b 20 20 70 4e 65 77 2d 3e 70 55 73 65 72 44 61 74    pNew->pUserDat
3905c 61 20 3d 20 70 41 72 67 3b 0d 0a 20 20 70 4e 65  a = pArg;..  pNe
3905d 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  w->flags |= SQLI
3905e 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0d 0a  TE_FUNC_EPHEM;..
3905f 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0d 0a    return pNew;..
39060 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65  }..../*..** Make
39061 20 73 75 72 65 20 76 69 72 74 75 61 6c 20 74 61   sure virtual ta
39062 62 6c 65 20 70 54 61 62 20 69 73 20 63 6f 6e 74  ble pTab is cont
39063 61 69 6e 65 64 20 69 6e 20 74 68 65 20 70 50 61  ained in the pPa
39064 72 73 65 2d 3e 61 70 56 69 72 74 75 61 6c 4c 6f  rse->apVirtualLo
39065 63 6b 5b 5d 0d 0a 2a 2a 20 61 72 72 61 79 20 73  ck[]..** array s
39066 6f 20 74 68 61 74 20 61 6e 20 4f 50 5f 56 42 65  o that an OP_VBe
39067 67 69 6e 20 77 69 6c 6c 20 67 65 74 20 67 65 6e  gin will get gen
39068 65 72 61 74 65 64 20 66 6f 72 20 69 74 2e 20 20  erated for it.  
39069 41 64 64 20 70 54 61 62 20 74 6f 20 74 68 65 0d  Add pTab to the.
3906a 0a 2a 2a 20 61 72 72 61 79 20 69 66 20 69 74 20  .** array if it 
3906b 69 73 20 6d 69 73 73 69 6e 67 2e 20 20 49 66 20  is missing.  If 
3906c 70 54 61 62 20 69 73 20 61 6c 72 65 61 64 79 20  pTab is already 
3906d 69 6e 20 74 68 65 20 61 72 72 61 79 2c 20 74 68  in the array, th
3906e 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 69  is routine..** i
3906f 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a  s a no-op...*/..
39070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
39071 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d  oid sqlite3VtabM
39072 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73  akeWritable(Pars
39073 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
39074 20 2a 70 54 61 62 29 7b 0d 0a 20 20 50 61 72 73   *pTab){..  Pars
39075 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
39076 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
39077 76 65 6c 28 70 50 61 72 73 65 29 3b 0d 0a 20 20  vel(pParse);..  
39078 69 6e 74 20 69 2c 20 6e 3b 0d 0a 20 20 54 61 62  int i, n;..  Tab
39079 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b  le **apVtabLock;
3907a 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 49 73  ....  assert( Is
3907b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b  Virtual(pTab) );
3907c 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
3907d 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c  Toplevel->nVtabL
3907e 6f 63 6b 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  ock; i++){..    
3907f 69 66 28 20 70 54 61 62 3d 3d 70 54 6f 70 6c 65  if( pTab==pTople
39080 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b  vel->apVtabLock[
39081 69 5d 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  i] ) return;..  
39082 7d 0d 0a 20 20 6e 20 3d 20 28 70 54 6f 70 6c 65  }..  n = (pTople
39083 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31  vel->nVtabLock+1
39084 29 2a 73 69 7a 65 6f 66 28 70 54 6f 70 6c 65 76  )*sizeof(pToplev
39085 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30  el->apVtabLock[0
39086 5d 29 3b 0d 0a 20 20 61 70 56 74 61 62 4c 6f 63  ]);..  apVtabLoc
39087 6b 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  k = sqlite3_real
39088 6c 6f 63 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  loc(pToplevel->a
39089 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0d 0a  pVtabLock, n);..
3908a 20 20 69 66 28 20 61 70 56 74 61 62 4c 6f 63 6b    if( apVtabLock
3908b 20 29 7b 0d 0a 20 20 20 20 70 54 6f 70 6c 65 76   ){..    pToplev
3908c 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 3d  el->apVtabLock =
3908d 20 61 70 56 74 61 62 4c 6f 63 6b 3b 0d 0a 20 20   apVtabLock;..  
3908e 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56    pToplevel->apV
3908f 74 61 62 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65  tabLock[pTopleve
39090 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20  l->nVtabLock++] 
39091 3d 20 70 54 61 62 3b 0d 0a 20 20 7d 65 6c 73 65  = pTab;..  }else
39092 7b 0d 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c  {..    pToplevel
39093 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
39094 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 7d 0d  ed = 1;..  }..}.
39095 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
39096 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
39097 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6d 6f 64 65   resolution mode
39098 20 69 6e 20 65 66 66 65 63 74 20 66 6f 72 20 74   in effect for t
39099 68 65 20 76 69 72 74 75 61 6c 0d 0a 2a 2a 20 74  he virtual..** t
3909a 61 62 6c 65 20 75 70 64 61 74 65 20 6f 70 65 72  able update oper
3909b 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ation currently 
3909c 69 6e 20 70 72 6f 67 72 65 73 73 2e 0d 0a 2a 2a  in progress...**
3909d 0d 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73  ..** The results
3909e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
3909f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 75   are undefined u
390a0 6e 6c 65 73 73 20 69 74 20 69 73 20 63 61 6c 6c  nless it is call
390a1 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 77 69 74 68  ed from..** with
390a2 69 6e 20 61 6e 20 78 55 70 64 61 74 65 20 6d 65  in an xUpdate me
390a3 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  thod...*/..SQLIT
390a4 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
390a5 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69  3_vtab_on_confli
390a6 63 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ct(sqlite3 *db){
390a7 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ..  static const
390a8 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
390a9 4d 61 70 5b 5d 20 3d 20 7b 20 0d 0a 20 20 20 20  Map[] = { ..    
390aa 53 51 4c 49 54 45 5f 52 4f 4c 4c 42 41 43 4b 2c  SQLITE_ROLLBACK,
390ab 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 53   SQLITE_ABORT, S
390ac 51 4c 49 54 45 5f 46 41 49 4c 2c 20 53 51 4c 49  QLITE_FAIL, SQLI
390ad 54 45 5f 49 47 4e 4f 52 45 2c 20 53 51 4c 49 54  TE_IGNORE, SQLIT
390ae 45 5f 52 45 50 4c 41 43 45 20 0d 0a 20 20 7d 3b  E_REPLACE ..  };
390af 0d 0a 20 20 61 73 73 65 72 74 28 20 4f 45 5f 52  ..  assert( OE_R
390b0 6f 6c 6c 62 61 63 6b 3d 3d 31 20 26 26 20 4f 45  ollback==1 && OE
390b1 5f 41 62 6f 72 74 3d 3d 32 20 26 26 20 4f 45 5f  _Abort==2 && OE_
390b2 46 61 69 6c 3d 3d 33 20 29 3b 0d 0a 20 20 61 73  Fail==3 );..  as
390b3 73 65 72 74 28 20 4f 45 5f 49 67 6e 6f 72 65 3d  sert( OE_Ignore=
390b4 3d 34 20 26 26 20 4f 45 5f 52 65 70 6c 61 63 65  =4 && OE_Replace
390b5 3d 3d 35 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ==5 );..  assert
390b6 28 20 64 62 2d 3e 76 74 61 62 4f 6e 43 6f 6e 66  ( db->vtabOnConf
390b7 6c 69 63 74 3e 3d 31 20 26 26 20 64 62 2d 3e 76  lict>=1 && db->v
390b8 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 3c 3d 35  tabOnConflict<=5
390b9 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 69   );..  return (i
390ba 6e 74 29 61 4d 61 70 5b 64 62 2d 3e 76 74 61 62  nt)aMap[db->vtab
390bb 4f 6e 43 6f 6e 66 6c 69 63 74 2d 31 5d 3b 0d 0a  OnConflict-1];..
390bc 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6c 6c  }..../*..** Call
390bd 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
390be 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43   xCreate() or xC
390bf 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73  onnect() methods
390c0 20 74 6f 20 70 72 6f 76 69 64 65 20 0d 0a 2a 2a   to provide ..**
390c1 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65   the SQLite core
390c2 20 77 69 74 68 20 61 64 64 69 74 69 6f 6e 61 6c   with additional
390c3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
390c4 75 74 20 74 68 65 20 62 65 68 61 76 69 6f 72 0d  ut the behavior.
390c5 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75  .** of the virtu
390c6 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69  al table being i
390c7 6d 70 6c 65 6d 65 6e 74 65 64 2e 0d 0a 2a 2f 0d  mplemented...*/.
390c8 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
390c9 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e  sqlite3_vtab_con
390ca 66 69 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  fig(sqlite3 *db,
390cb 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0d 0a   int op, ...){..
390cc 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20    va_list ap;.. 
390cd 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
390ce 5f 4f 4b 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  _OK;....  sqlite
390cf 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
390d0 2d 3e 6d 75 74 65 78 29 3b 0d 0a 0d 0a 20 20 76  ->mutex);....  v
390d1 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
390d2 0d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ..  switch( op )
390d3 7b 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  {..    case SQLI
390d4 54 45 5f 56 54 41 42 5f 43 4f 4e 53 54 52 41 49  TE_VTAB_CONSTRAI
390d5 4e 54 5f 53 55 50 50 4f 52 54 3a 20 7b 0d 0a 20  NT_SUPPORT: {.. 
390d6 20 20 20 20 20 56 74 61 62 43 74 78 20 2a 70 20       VtabCtx *p 
390d7 3d 20 64 62 2d 3e 70 56 74 61 62 43 74 78 3b 0d  = db->pVtabCtx;.
390d8 0a 20 20 20 20 20 20 69 66 28 20 21 70 20 29 7b  .      if( !p ){
390d9 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ..        rc = S
390da 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50  QLITE_MISUSE_BKP
390db 54 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  T;..      }else{
390dc 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
390dd 28 20 70 2d 3e 70 54 61 62 3d 3d 30 20 7c 7c 20  ( p->pTab==0 || 
390de 28 70 2d 3e 70 54 61 62 2d 3e 74 61 62 46 6c 61  (p->pTab->tabFla
390df 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
390e0 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  !=0 );..        
390e1 70 2d 3e 70 56 54 61 62 6c 65 2d 3e 62 43 6f 6e  p->pVTable->bCon
390e2 73 74 72 61 69 6e 74 20 3d 20 28 75 38 29 76 61  straint = (u8)va
390e3 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d 0a  _arg(ap, int);..
390e4 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62        }..      b
390e5 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
390e6 20 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20    default:..    
390e7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
390e8 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20 20 20  SUSE_BKPT;..    
390e9 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 20    break;..  }.. 
390ea 20 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 0d 0a   va_end(ap);....
390eb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
390ec 5f 4f 4b 20 29 20 73 71 6c 69 74 65 33 45 72 72  _OK ) sqlite3Err
390ed 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0d 0a  or(db, rc, 0);..
390ee 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
390ef 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
390f0 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
390f1 0a 7d 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  .}....#endif /* 
390f2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
390f3 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a 2f  UALTABLE */..../
390f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
390f5 6e 64 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a  nd of vtab.c ***
390f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
390f9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
390fa 42 65 67 69 6e 20 66 69 6c 65 20 77 68 65 72 65  Begin file where
390fb 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
390fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
390fe 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70  ./*..** 2001 Sep
390ff 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a  tember 15..**..*
39100 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
39101 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
39102 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
39103 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
39104 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
39105 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
39106 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
39107 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
39108 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
39109 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
3910a 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
3910b 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
3910c 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
3910d 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
3910e 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
3910f 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
39110 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
39111 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
39112 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39113 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39116 2a 0d 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c  *..** This modul
39117 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
39118 65 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 73  e that generates
39119 20 56 44 42 45 20 63 6f 64 65 20 75 73 65 64 20   VDBE code used 
3911a 74 6f 20 70 72 6f 63 65 73 73 0d 0a 2a 2a 20 74  to process..** t
3911b 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
3911c 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  of SQL statement
3911d 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  s.  This module 
3911e 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
3911f 6f 72 0d 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  or..** generatin
39120 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  g the code that 
39121 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20  loops through a 
39122 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  table looking fo
39123 72 20 61 70 70 6c 69 63 61 62 6c 65 0d 0a 2a 2a  r applicable..**
39124 20 72 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20   rows.  Indices 
39125 61 72 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64  are selected and
39126 20 75 73 65 64 20 74 6f 20 73 70 65 65 64 20 74   used to speed t
39127 68 65 20 73 65 61 72 63 68 20 77 68 65 6e 20 64  he search when d
39128 6f 69 6e 67 0d 0a 2a 2a 20 73 6f 20 69 73 20 61  oing..** so is a
39129 70 70 6c 69 63 61 62 6c 65 2e 20 20 42 65 63 61  pplicable.  Beca
3912a 75 73 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  use this module 
3912b 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
3912c 6f 72 20 73 65 6c 65 63 74 69 6e 67 0d 0a 2a 2a  or selecting..**
3912d 20 69 6e 64 69 63 65 73 2c 20 79 6f 75 20 6d 69   indices, you mi
3912e 67 68 74 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f  ght also think o
3912f 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 73  f this module as
39130 20 74 68 65 20 22 71 75 65 72 79 20 6f 70 74 69   the "query opti
39131 6d 69 7a 65 72 22 2e 0d 0a 2a 2f 0d 0a 0d 0a 0d  mizer"...*/.....
39132 0a 2f 2a 0d 0a 2a 2a 20 54 72 61 63 65 20 6f 75  ./*..** Trace ou
39133 74 70 75 74 20 6d 61 63 72 6f 73 0d 0a 2a 2f 0d  tput macros..*/.
39134 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
39135 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66  ITE_TEST) || def
39136 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
39137 47 29 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  G)..SQLITE_PRIVA
39138 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 68  TE int sqlite3Wh
39139 65 72 65 54 72 61 63 65 20 3d 20 30 3b 0d 0a 23  ereTrace = 0;..#
3913a 65 6e 64 69 66 0d 0a 23 69 66 20 64 65 66 69 6e  endif..#if defin
3913b 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
3913c 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
3913d 45 5f 44 45 42 55 47 29 0d 0a 23 20 64 65 66 69  E_DEBUG)..# defi
3913e 6e 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29  ne WHERETRACE(X)
3913f 20 20 69 66 28 73 71 6c 69 74 65 33 57 68 65 72    if(sqlite3Wher
39140 65 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44  eTrace) sqlite3D
39141 65 62 75 67 50 72 69 6e 74 66 20 58 0d 0a 23 65  ebugPrintf X..#e
39142 6c 73 65 0d 0a 23 20 64 65 66 69 6e 65 20 57 48  lse..# define WH
39143 45 52 45 54 52 41 43 45 28 58 29 0d 0a 23 65 6e  ERETRACE(X)..#en
39144 64 69 66 0d 0a 0d 0a 2f 2a 20 46 6f 72 77 61 72  dif..../* Forwar
39145 64 20 72 65 66 65 72 65 6e 63 65 0d 0a 2a 2f 0d  d reference..*/.
39146 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
39147 57 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72  WhereClause Wher
39148 65 43 6c 61 75 73 65 3b 0d 0a 74 79 70 65 64 65  eClause;..typede
39149 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4d 61  f struct WhereMa
3914a 73 6b 53 65 74 20 57 68 65 72 65 4d 61 73 6b 53  skSet WhereMaskS
3914b 65 74 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72  et;..typedef str
3914c 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20  uct WhereOrInfo 
3914d 57 68 65 72 65 4f 72 49 6e 66 6f 3b 0d 0a 74 79  WhereOrInfo;..ty
3914e 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
3914f 72 65 41 6e 64 49 6e 66 6f 20 57 68 65 72 65 41  reAndInfo WhereA
39150 6e 64 49 6e 66 6f 3b 0d 0a 74 79 70 65 64 65 66  ndInfo;..typedef
39151 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73   struct WhereCos
39152 74 20 57 68 65 72 65 43 6f 73 74 3b 0d 0a 0d 0a  t WhereCost;....
39153 2f 2a 0d 0a 2a 2a 20 54 68 65 20 71 75 65 72 79  /*..** The query
39154 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 20   generator uses 
39155 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 74  an array of inst
39156 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 74  ances of this st
39157 72 75 63 74 75 72 65 20 74 6f 0d 0a 2a 2a 20 68  ructure to..** h
39158 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74  elp it analyze t
39159 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  he subexpression
3915a 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
3915b 6c 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45  lause.  Each WHE
3915c 52 45 0d 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75  RE..** clause su
3915d 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73  bexpression is s
3915e 65 70 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68  eparated from th
3915f 65 20 6f 74 68 65 72 73 20 62 79 20 41 4e 44 20  e others by AND 
39160 6f 70 65 72 61 74 6f 72 73 2c 0d 0a 2a 2a 20 75  operators,..** u
39161 73 75 61 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65 74  sually, or somet
39162 69 6d 65 73 20 73 75 62 65 78 70 72 65 73 73 69  imes subexpressi
39163 6f 6e 73 20 73 65 70 61 72 61 74 65 64 20 62 79  ons separated by
39164 20 4f 52 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c   OR...**..** All
39165 20 57 68 65 72 65 54 65 72 6d 73 20 61 72 65 20   WhereTerms are 
39166 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61  collected into a
39167 20 73 69 6e 67 6c 65 20 57 68 65 72 65 43 6c 61   single WhereCla
39168 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  use structure.  
39169 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
3916a 6e 67 20 69 64 65 6e 74 69 74 79 20 68 6f 6c 64  ng identity hold
3916b 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  s:..**..**      
3916c 20 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43 2d    WhereTerm.pWC-
3916d 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64 78  >a[WhereTerm.idx
3916e 5d 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0d 0a  ] == WhereTerm..
3916f 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65  **..** When a te
39170 72 6d 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  rm is of the for
39171 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  m:..**..**      
39172 20 20 20 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c          X <op> <
39173 65 78 70 72 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 77 68  expr>..**..** wh
39174 65 72 65 20 58 20 69 73 20 61 20 63 6f 6c 75 6d  ere X is a colum
39175 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f 70 3e 20  n name and <op> 
39176 69 73 20 6f 6e 65 20 6f 66 20 63 65 72 74 61 69  is one of certai
39177 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0d 0a 2a 2a  n operators,..**
39178 20 74 68 65 6e 20 57 68 65 72 65 54 65 72 6d 2e   then WhereTerm.
39179 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64 20 57  leftCursor and W
3917a 68 65 72 65 54 65 72 6d 2e 75 2e 6c 65 66 74 43  hereTerm.u.leftC
3917b 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74 68 65  olumn record the
3917c 0d 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62  ..** cursor numb
3917d 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75  er and column nu
3917e 6d 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68 65  mber for X.  Whe
3917f 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72  reTerm.eOperator
39180 20 72 65 63 6f 72 64 73 0d 0a 2a 2a 20 74 68 65   records..** the
39181 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69   <op> using a bi
39182 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64  tmask encoding d
39183 65 66 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78  efined by WO_xxx
39184 20 62 65 6c 6f 77 2e 20 20 54 68 65 0d 0a 2a 2a   below.  The..**
39185 20 75 73 65 20 6f 66 20 61 20 62 69 74 6d 61 73   use of a bitmas
39186 6b 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  k encoding for t
39187 68 65 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f  he operator allo
39188 77 73 20 75 73 20 74 6f 20 73 65 61 72 63 68 0d  ws us to search.
39189 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72 20  .** quickly for 
3918a 74 65 72 6d 73 20 74 68 61 74 20 6d 61 74 63 68  terms that match
3918b 20 61 6e 79 20 6f 66 20 73 65 76 65 72 61 6c 20   any of several 
3918c 64 69 66 66 65 72 65 6e 74 20 6f 70 65 72 61 74  different operat
3918d 6f 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 57  ors...**..** A W
3918e 68 65 72 65 54 65 72 6d 20 6d 69 67 68 74 20 61  hereTerm might a
3918f 6c 73 6f 20 62 65 20 74 77 6f 20 6f 72 20 6d 6f  lso be two or mo
39190 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e 6e  re subterms conn
39191 65 63 74 65 64 20 62 79 20 4f 52 3a 0d 0a 2a 2a  ected by OR:..**
39192 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31  ..**         (t1
39193 2e 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20  .X <op> <expr>) 
39194 4f 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20 3c 65  OR (t1.Y <op> <e
39195 78 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0d 0a 2a  xpr>) OR ......*
39196 2a 0d 0a 2a 2a 20 49 6e 20 74 68 69 73 20 73 65  *..** In this se
39197 63 6f 6e 64 20 63 61 73 65 2c 20 77 74 46 6c 61  cond case, wtFla
39198 67 20 61 73 20 74 68 65 20 54 45 52 4d 5f 4f 52  g as the TERM_OR
39199 49 4e 46 4f 20 73 65 74 20 61 6e 64 20 65 4f 70  INFO set and eOp
3919a 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 0d 0a 2a  erator==WO_OR..*
3919b 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 54  * and the WhereT
3919c 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 66 69  erm.u.pOrInfo fi
3919d 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 75  eld points to au
3919e 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74  xiliary informat
3919f 69 6f 6e 20 74 68 61 74 0d 0a 2a 2a 20 69 73 20  ion that..** is 
391a0 63 6f 6c 6c 65 63 74 65 64 20 61 62 6f 75 74 20  collected about 
391a1 74 68 65 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  the..**..** If a
391a2 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45   term in the WHE
391a3 52 45 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  RE clause does n
391a4 6f 74 20 6d 61 74 63 68 20 65 69 74 68 65 72 20  ot match either 
391a5 6f 66 20 74 68 65 20 74 77 6f 20 70 72 65 76 69  of the two previ
391a6 6f 75 73 0d 0a 2a 2a 20 63 61 74 65 67 6f 72 69  ous..** categori
391a7 65 73 2c 20 74 68 65 6e 20 65 4f 70 65 72 61 74  es, then eOperat
391a8 6f 72 3d 3d 30 2e 20 20 54 68 65 20 57 68 65 72  or==0.  The Wher
391a9 65 54 65 72 6d 2e 70 45 78 70 72 20 66 69 65 6c  eTerm.pExpr fiel
391aa 64 20 69 73 20 73 74 69 6c 6c 20 73 65 74 0d 0a  d is still set..
391ab 2a 2a 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e  ** to the origin
391ac 61 6c 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  al subexpression
391ad 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 77 74 46   content and wtF
391ae 6c 61 67 73 20 69 73 20 73 65 74 20 75 70 20 61  lags is set up a
391af 70 70 72 6f 70 72 69 61 74 65 6c 79 0d 0a 2a 2a  ppropriately..**
391b0 20 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 69   but no other fi
391b1 65 6c 64 73 20 69 6e 20 74 68 65 20 57 68 65 72  elds in the Wher
391b2 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 61 72 65  eTerm object are
391b3 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0d 0a 2a 2a   meaningful...**
391b4 0d 0a 2a 2a 20 57 68 65 6e 20 65 4f 70 65 72 61  ..** When eOpera
391b5 74 6f 72 21 3d 30 2c 20 70 72 65 72 65 71 52 69  tor!=0, prereqRi
391b6 67 68 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c  ght and prereqAl
391b7 6c 20 72 65 63 6f 72 64 20 73 65 74 73 20 6f 66  l record sets of
391b8 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c   cursor numbers,
391b9 0d 0a 2a 2a 20 62 75 74 20 74 68 65 79 20 64 6f  ..** but they do
391ba 20 73 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e 20   so indirectly. 
391bb 20 41 20 73 69 6e 67 6c 65 20 57 68 65 72 65 4d   A single WhereM
391bc 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65  askSet structure
391bd 20 74 72 61 6e 73 6c 61 74 65 73 0d 0a 2a 2a 20   translates..** 
391be 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e  cursor number in
391bf 74 6f 20 62 69 74 73 20 61 6e 64 20 74 68 65 20  to bits and the 
391c0 74 72 61 6e 73 6c 61 74 65 64 20 62 69 74 20 69  translated bit i
391c1 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
391c2 70 72 65 72 65 71 0d 0a 2a 2a 20 66 69 65 6c 64  prereq..** field
391c3 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c 61 74  s.  The translat
391c4 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 6f  ion is used in o
391c5 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69 7a 65  rder to maximize
391c6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a   the number of..
391c7 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69 6c  ** bits that wil
391c8 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d 61  l fit in a Bitma
391c9 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63 75  sk.  The VDBE cu
391ca 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67  rsor numbers mig
391cb 68 74 20 62 65 0d 0a 2a 2a 20 73 70 72 65 61 64  ht be..** spread
391cc 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f   out over the no
391cd 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  n-negative integ
391ce 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ers.  For exampl
391cf 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0d 0a 2a  e, the cursor..*
391d0 2a 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20  * numbers might 
391d1 62 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20  be 3, 8, 9, 10, 
391d2 32 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20  20, 23, 41, and 
391d3 34 35 2e 20 20 54 68 65 20 57 68 65 72 65 4d 61  45.  The WhereMa
391d4 73 6b 53 65 74 0d 0a 2a 2a 20 74 72 61 6e 73 6c  skSet..** transl
391d5 61 74 65 73 20 74 68 65 73 65 20 73 70 61 72 73  ates these spars
391d6 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
391d7 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76   into consecutiv
391d8 65 20 69 6e 74 65 67 65 72 73 0d 0a 2a 2a 20 62  e integers..** b
391d9 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 20  eginning with 0 
391da 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65  in order to make
391db 20 74 68 65 20 62 65 73 74 20 70 6f 73 73 69 62   the best possib
391dc 6c 65 20 75 73 65 20 6f 66 20 74 68 65 20 61 76  le use of the av
391dd 61 69 6c 61 62 6c 65 0d 0a 2a 2a 20 62 69 74 73  ailable..** bits
391de 20 69 6e 20 74 68 65 20 42 69 74 6d 61 73 6b 2e   in the Bitmask.
391df 20 20 53 6f 2c 20 69 6e 20 74 68 65 20 65 78 61    So, in the exa
391e0 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20  mple above, the 
391e1 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 0d 0a  cursor numbers..
391e2 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 61 70 70  ** would be mapp
391e3 65 64 20 69 6e 74 6f 20 69 6e 74 65 67 65 72 73  ed into integers
391e4 20 30 20 74 68 72 6f 75 67 68 20 37 2e 0d 0a 2a   0 through 7...*
391e5 2a 0d 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  *..** The number
391e6 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 6a   of terms in a j
391e7 6f 69 6e 20 69 73 20 6c 69 6d 69 74 65 64 20 62  oin is limited b
391e8 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
391e9 62 69 74 73 0d 0a 2a 2a 20 69 6e 20 70 72 65 72  bits..** in prer
391ea 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65 72  eqRight and prer
391eb 65 71 41 6c 6c 2e 20 20 54 68 65 20 64 65 66 61  eqAll.  The defa
391ec 75 6c 74 20 69 73 20 36 34 20 62 69 74 73 2c 20  ult is 64 bits, 
391ed 68 65 6e 63 65 20 53 51 4c 69 74 65 0d 0a 2a 2a  hence SQLite..**
391ee 20 69 73 20 6f 6e 6c 79 20 61 62 6c 65 20 74 6f   is only able to
391ef 20 70 72 6f 63 65 73 73 20 6a 6f 69 6e 73 20 77   process joins w
391f0 69 74 68 20 36 34 20 6f 72 20 66 65 77 65 72 20  ith 64 or fewer 
391f1 74 61 62 6c 65 73 2e 0d 0a 2a 2f 0d 0a 74 79 70  tables...*/..typ
391f2 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
391f3 65 54 65 72 6d 20 57 68 65 72 65 54 65 72 6d 3b  eTerm WhereTerm;
391f4 0d 0a 73 74 72 75 63 74 20 57 68 65 72 65 54 65  ..struct WhereTe
391f5 72 6d 20 7b 0d 0a 20 20 45 78 70 72 20 2a 70 45  rm {..  Expr *pE
391f6 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
391f7 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
391f8 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  e subexpression 
391f9 74 68 61 74 20 69 73 20 74 68 69 73 20 74 65 72  that is this ter
391fa 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 61 72  m */..  int iPar
391fb 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
391fc 2f 2a 20 44 69 73 61 62 6c 65 20 70 57 43 2d 3e  /* Disable pWC->
391fd 61 5b 69 50 61 72 65 6e 74 5d 20 77 68 65 6e 20  a[iParent] when 
391fe 74 68 69 73 20 74 65 72 6d 20 64 69 73 61 62 6c  this term disabl
391ff 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6c 65 66  ed */..  int lef
39200 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  tCursor;        
39201 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
39202 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70  r of X in "X <op
39203 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0d 0a 20 20  > <expr>" */..  
39204 75 6e 69 6f 6e 20 7b 0d 0a 20 20 20 20 69 6e 74  union {..    int
39205 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20   leftColumn;    
39206 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
39207 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58  umber of X in "X
39208 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f   <op> <expr>" */
39209 0d 0a 20 20 20 20 57 68 65 72 65 4f 72 49 6e 66  ..    WhereOrInf
3920a 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a  o *pOrInfo;   /*
3920b 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   Extra informati
3920c 6f 6e 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d  on if eOperator=
3920d 3d 57 4f 5f 4f 52 20 2a 2f 0d 0a 20 20 20 20 57  =WO_OR */..    W
3920e 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e  hereAndInfo *pAn
3920f 64 49 6e 66 6f 3b 20 2f 2a 20 45 78 74 72 61 20  dInfo; /* Extra 
39210 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65  information if e
39211 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44  Operator==WO_AND
39212 20 2a 2f 0d 0a 20 20 7d 20 75 3b 0d 0a 20 20 75   */..  } u;..  u
39213 31 36 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 20  16 eOperator;   
39214 20 20 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78         /* A WO_x
39215 78 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 69  x value describi
39216 6e 67 20 3c 6f 70 3e 20 2a 2f 0d 0a 20 20 75 38  ng <op> */..  u8
39217 20 77 74 46 6c 61 67 73 3b 20 20 20 20 20 20 20   wtFlags;       
39218 20 20 20 20 20 20 2f 2a 20 54 45 52 4d 5f 78 78        /* TERM_xx
39219 78 20 62 69 74 20 66 6c 61 67 73 2e 20 20 53 65  x bit flags.  Se
3921a 65 20 62 65 6c 6f 77 20 2a 2f 0d 0a 20 20 75 38  e below */..  u8
3921b 20 6e 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20   nChild;        
3921c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3921d 6f 66 20 63 68 69 6c 64 72 65 6e 20 74 68 61 74  of children that
3921e 20 6d 75 73 74 20 64 69 73 61 62 6c 65 20 75 73   must disable us
3921f 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61 75   */..  WhereClau
39220 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f  se *pWC;       /
39221 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74 68 69  * The clause thi
39222 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f  s term is part o
39223 66 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b 20  f */..  Bitmask 
39224 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20 20  prereqRight;    
39225 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61  /* Bitmask of ta
39226 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 45 78  bles used by pEx
39227 70 72 2d 3e 70 52 69 67 68 74 20 2a 2f 0d 0a 20  pr->pRight */.. 
39228 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41   Bitmask prereqA
39229 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  ll;      /* Bitm
3922a 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 72 65  ask of tables re
3922b 66 65 72 65 6e 63 65 64 20 62 79 20 70 45 78 70  ferenced by pExp
3922c 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  r */..};..../*..
3922d 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
3922e 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e 77  s of WhereTerm.w
3922f 74 46 6c 61 67 73 0d 0a 2a 2f 0d 0a 23 64 65 66  tFlags..*/..#def
39230 69 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  ine TERM_DYNAMIC
39231 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e 65      0x01   /* Ne
39232 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  ed to call sqlit
39233 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
39234 20 70 45 78 70 72 29 20 2a 2f 0d 0a 23 64 65 66   pExpr) */..#def
39235 69 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  ine TERM_VIRTUAL
39236 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64      0x02   /* Ad
39237 64 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d  ded by the optim
39238 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f  izer.  Do not co
39239 64 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 54  de */..#define T
3923a 45 52 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30  ERM_CODED      0
3923b 78 30 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65  x04   /* This te
3923c 72 6d 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f  rm is already co
3923d 64 65 64 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  ded */..#define 
3923e 54 45 52 4d 5f 43 4f 50 49 45 44 20 20 20 20 20  TERM_COPIED     
3923f 30 78 30 38 20 20 20 2f 2a 20 48 61 73 20 61 20  0x08   /* Has a 
39240 63 68 69 6c 64 20 2a 2f 0d 0a 23 64 65 66 69 6e  child */..#defin
39241 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 20 20  e TERM_ORINFO   
39242 20 20 30 78 31 30 20 20 20 2f 2a 20 4e 65 65 64    0x10   /* Need
39243 20 74 6f 20 66 72 65 65 20 74 68 65 20 57 68 65   to free the Whe
39244 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f  reTerm.u.pOrInfo
39245 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 23 64 65 66   object */..#def
39246 69 6e 65 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f  ine TERM_ANDINFO
39247 20 20 20 20 30 78 32 30 20 20 20 2f 2a 20 4e 65      0x20   /* Ne
39248 65 64 20 74 6f 20 66 72 65 65 20 74 68 65 20 57  ed to free the W
39249 68 65 72 65 54 65 72 6d 2e 75 2e 70 41 6e 64 49  hereTerm.u.pAndI
3924a 6e 66 6f 20 6f 62 6a 20 2a 2f 0d 0a 23 64 65 66  nfo obj */..#def
3924b 69 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20  ine TERM_OR_OK  
3924c 20 20 20 20 30 78 34 30 20 20 20 2f 2a 20 55 73      0x40   /* Us
3924d 65 64 20 64 75 72 69 6e 67 20 4f 52 2d 63 6c 61  ed during OR-cla
3924e 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a  use processing *
3924f 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
39250 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 23  _ENABLE_STAT3..#
39251 20 20 64 65 66 69 6e 65 20 54 45 52 4d 5f 56 4e    define TERM_VN
39252 55 4c 4c 20 20 20 20 30 78 38 30 20 20 20 2f 2a  ULL    0x80   /*
39253 20 4d 61 6e 75 66 61 63 74 75 72 65 64 20 78 3e   Manufactured x>
39254 4e 55 4c 4c 20 6f 72 20 78 3c 3d 4e 55 4c 4c 20  NULL or x<=NULL 
39255 74 65 72 6d 20 2a 2f 0d 0a 23 65 6c 73 65 0d 0a  term */..#else..
39256 23 20 20 64 65 66 69 6e 65 20 54 45 52 4d 5f 56  #  define TERM_V
39257 4e 55 4c 4c 20 20 20 20 30 78 30 30 20 20 20 2f  NULL    0x00   /
39258 2a 20 44 69 73 61 62 6c 65 64 20 69 66 20 6e 6f  * Disabled if no
39259 74 20 75 73 69 6e 67 20 73 74 61 74 33 20 2a 2f  t using stat3 */
3925a 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
3925b 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
3925c 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3925d 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
3925e 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
3925f 61 62 6f 75 74 20 61 0d 0a 2a 2a 20 57 48 45 52  about a..** WHER
39260 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c  E clause.  Mostl
39261 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74  y this is a cont
39262 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72  ainer for one or
39263 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73   more WhereTerms
39264 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 70 6c 61 6e  ...**..** Explan
39265 61 74 69 6f 6e 20 6f 66 20 70 4f 75 74 65 72 3a  ation of pOuter:
39266 20 20 46 6f 72 20 61 20 57 48 45 52 45 20 63 6c    For a WHERE cl
39267 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  ause of the form
39268 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 20  ..**..**        
39269 20 20 20 61 20 41 4e 44 20 28 28 62 20 41 4e 44     a AND ((b AND
3926a 20 63 29 20 4f 52 20 28 64 20 41 4e 44 20 65 29   c) OR (d AND e)
3926b 29 20 41 4e 44 20 66 0d 0a 2a 2a 0d 0a 2a 2a 20  ) AND f..**..** 
3926c 54 68 65 72 65 20 61 72 65 20 73 65 70 61 72 61  There are separa
3926d 74 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f  te WhereClause o
3926e 62 6a 65 63 74 73 20 66 6f 72 20 74 68 65 20 77  bjects for the w
3926f 68 6f 6c 65 20 63 6c 61 75 73 65 20 61 6e 64 20  hole clause and 
39270 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 73 75 62 63  for..** the subc
39271 6c 61 75 73 65 73 20 22 28 62 20 41 4e 44 20 63  lauses "(b AND c
39272 29 22 20 61 6e 64 20 22 28 64 20 41 4e 44 20 65  )" and "(d AND e
39273 29 22 2e 20 20 54 68 65 20 70 4f 75 74 65 72 20  )".  The pOuter 
39274 66 69 65 6c 64 20 6f 66 20 74 68 65 0d 0a 2a 2a  field of the..**
39275 20 73 75 62 63 6c 61 75 73 65 73 20 70 6f 69 6e   subclauses poin
39276 74 73 20 74 6f 20 74 68 65 20 57 68 65 72 65 43  ts to the WhereC
39277 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 66 6f 72  lause object for
39278 20 74 68 65 20 77 68 6f 6c 65 20 63 6c 61 75 73   the whole claus
39279 65 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 57  e...*/..struct W
3927a 68 65 72 65 43 6c 61 75 73 65 20 7b 0d 0a 20 20  hereClause {..  
3927b 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
3927c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3927d 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
3927e 2f 0d 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  /..  WhereMaskSe
3927f 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 2f 2a  t *pMaskSet;  /*
39280 20 4d 61 70 70 69 6e 67 20 6f 66 20 74 61 62 6c   Mapping of tabl
39281 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
39282 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0d   to bitmasks */.
39283 0a 20 20 42 69 74 6d 61 73 6b 20 76 6d 61 73 6b  .  Bitmask vmask
39284 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ;           /* B
39285 69 74 6d 61 73 6b 20 69 64 65 6e 74 69 66 79 69  itmask identifyi
39286 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ng virtual table
39287 20 63 75 72 73 6f 72 73 20 2a 2f 0d 0a 20 20 57   cursors */..  W
39288 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 75 74  hereClause *pOut
39289 65 72 3b 20 20 20 20 20 2f 2a 20 4f 75 74 65 72  er;     /* Outer
3928a 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 20 2a 2f 0d   conjunction */.
3928b 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
3928c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
3928d 70 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e 20 20  plit operator.  
3928e 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52 20  TK_AND or TK_OR 
3928f 2a 2f 0d 0a 20 20 75 31 36 20 77 63 74 72 6c 46  */..  u16 wctrlF
39290 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lags;          /
39291 2a 20 4d 69 67 68 74 20 69 6e 63 6c 75 64 65 20  * Might include 
39292 57 48 45 52 45 5f 41 4e 44 5f 4f 4e 4c 59 20 2a  WHERE_AND_ONLY *
39293 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20  /..  int nTerm; 
39294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39295 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
39296 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 6c 6f 74   */..  int nSlot
39297 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39298 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
39299 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0d 0a  ries in a[] */..
3929a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20    WhereTerm *a; 
3929b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61             /* Ea
3929c 63 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73  ch a[] describes
3929d 20 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57   a term of the W
3929e 48 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0d 0a  HERE cluase */..
3929f 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
392a0 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0d  TE_SMALL_STACK).
392a1 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74  .  WhereTerm aSt
392a2 61 74 69 63 5b 31 5d 3b 20 20 20 20 2f 2a 20 49  atic[1];    /* I
392a3 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70  nitial static sp
392a4 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0d 0a  ace for a[] */..
392a5 23 65 6c 73 65 0d 0a 20 20 57 68 65 72 65 54 65  #else..  WhereTe
392a6 72 6d 20 61 53 74 61 74 69 63 5b 38 5d 3b 20 20  rm aStatic[8];  
392a7 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61    /* Initial sta
392a8 74 69 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b  tic space for a[
392a9 5d 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b  ] */..#endif..};
392aa 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 57 68 65  ..../*..** A Whe
392ab 72 65 54 65 72 6d 20 77 69 74 68 20 65 4f 70 65  reTerm with eOpe
392ac 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 68 61 73  rator==WO_OR has
392ad 20 69 74 73 20 75 2e 70 4f 72 49 6e 66 6f 20 70   its u.pOrInfo p
392ae 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0d 0a 2a  ointer set to..*
392af 2a 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  * a dynamically 
392b0 61 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61 6e  allocated instan
392b1 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
392b2 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0d 0a  ing structure...
392b3 2a 2f 0d 0a 73 74 72 75 63 74 20 57 68 65 72 65  */..struct Where
392b4 4f 72 49 6e 66 6f 20 7b 0d 0a 20 20 57 68 65 72  OrInfo {..  Wher
392b5 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20  eClause wc;     
392b6 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73       /* Decompos
392b7 69 74 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74 65  ition into subte
392b8 72 6d 73 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73  rms */..  Bitmas
392b9 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20  k indexable;    
392ba 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
392bb 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c 65 20 74   all indexable t
392bc 61 62 6c 65 73 20 69 6e 20 74 68 65 20 63 6c 61  ables in the cla
392bd 75 73 65 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  use */..};..../*
392be 0d 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d  ..** A WhereTerm
392bf 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d   with eOperator=
392c0 3d 57 4f 5f 41 4e 44 20 68 61 73 20 69 74 73 20  =WO_AND has its 
392c1 75 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69 6e 74  u.pAndInfo point
392c2 65 72 20 73 65 74 20 74 6f 0d 0a 2a 2a 20 61 20  er set to..** a 
392c3 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
392c4 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f  cated instance o
392c5 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
392c6 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a  structure...*/..
392c7 73 74 72 75 63 74 20 57 68 65 72 65 41 6e 64 49  struct WhereAndI
392c8 6e 66 6f 20 7b 0d 0a 20 20 57 68 65 72 65 43 6c  nfo {..  WhereCl
392c9 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20 20  ause wc;        
392ca 20 20 2f 2a 20 54 68 65 20 73 75 62 65 78 70 72    /* The subexpr
392cb 65 73 73 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75  ession broken ou
392cc 74 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  t */..};..../*..
392cd 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
392ce 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
392cf 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20  structure keeps 
392d0 74 72 61 63 6b 20 6f 66 20 61 20 6d 61 70 70 69  track of a mappi
392d1 6e 67 0d 0a 2a 2a 20 62 65 74 77 65 65 6e 20 56  ng..** between V
392d2 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
392d3 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66 20 74  rs and bits of t
392d4 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e 20 57  he bitmasks in W
392d5 68 65 72 65 54 65 72 6d 2e 0d 0a 2a 2a 0d 0a 2a  hereTerm...**..*
392d6 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
392d7 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d  r numbers are sm
392d8 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e  all integers con
392d9 74 61 69 6e 65 64 20 69 6e 20 0d 0a 2a 2a 20 53  tained in ..** S
392da 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72  rcList_item.iCur
392db 73 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61  sor and Expr.iTa
392dc 62 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72  ble fields.  For
392dd 20 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45   any given WHERE
392de 20 0d 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68   ..** clause, th
392df 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
392e0 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e   might not begin
392e1 20 77 69 74 68 20 30 20 61 6e 64 20 74 68 65 79   with 0 and they
392e2 20 6d 69 67 68 74 0d 0a 2a 2a 20 63 6f 6e 74 61   might..** conta
392e3 69 6e 20 67 61 70 73 20 69 6e 20 74 68 65 20 6e  in gaps in the n
392e4 75 6d 62 65 72 69 6e 67 20 73 65 71 75 65 6e 63  umbering sequenc
392e5 65 2e 20 20 42 75 74 20 77 65 20 77 61 6e 74 20  e.  But we want 
392e6 74 6f 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0d  to make maximum.
392e7 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68 65 20 62  .** use of the b
392e8 69 74 73 20 69 6e 20 6f 75 72 20 62 69 74 6d 61  its in our bitma
392e9 73 6b 73 2e 20 20 54 68 69 73 20 73 74 72 75 63  sks.  This struc
392ea 74 75 72 65 20 70 72 6f 76 69 64 65 73 20 61 20  ture provides a 
392eb 6d 61 70 70 69 6e 67 0d 0a 2a 2a 20 66 72 6f 6d  mapping..** from
392ec 20 74 68 65 20 73 70 61 72 73 65 20 63 75 72 73   the sparse curs
392ed 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
392ee 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65  consecutive inte
392ef 67 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 0d 0a  gers beginning..
392f0 2a 2a 20 77 69 74 68 20 30 2e 0d 0a 2a 2a 0d 0a  ** with 0...**..
392f1 2a 2a 20 49 66 20 57 68 65 72 65 4d 61 73 6b 53  ** If WhereMaskS
392f2 65 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20 6d  et.ix[A]==B it m
392f3 65 61 6e 73 20 74 68 61 74 20 54 68 65 20 41 2d  eans that The A-
392f4 74 68 20 62 69 74 20 6f 66 20 61 20 42 69 74 6d  th bit of a Bitm
392f5 61 73 6b 0d 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  ask..** correspo
392f6 6e 64 73 20 56 44 42 45 20 63 75 72 73 6f 72 20  nds VDBE cursor 
392f7 6e 75 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41  number B.  The A
392f8 2d 74 68 20 62 69 74 20 6f 66 20 61 20 62 69 74  -th bit of a bit
392f9 6d 61 73 6b 20 69 73 20 31 3c 3c 41 2e 0d 0a 2a  mask is 1<<A...*
392fa 2a 0d 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  *..** For exampl
392fb 65 2c 20 69 66 20 74 68 65 20 57 48 45 52 45 20  e, if the WHERE 
392fc 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 69 6f  clause expressio
392fd 6e 20 75 73 65 64 20 74 68 65 73 65 20 56 44 42  n used these VDB
392fe 45 0d 0a 2a 2a 20 63 75 72 73 6f 72 73 3a 20 20  E..** cursors:  
392ff 34 2c 20 35 2c 20 38 2c 20 32 39 2c 20 35 37 2c  4, 5, 8, 29, 57,
39300 20 37 33 2e 20 20 54 68 65 6e 20 74 68 65 20 20   73.  Then the  
39301 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72  WhereMaskSet str
39302 75 63 74 75 72 65 0d 0a 2a 2a 20 77 6f 75 6c 64  ucture..** would
39303 20 6d 61 70 20 74 68 6f 73 65 20 63 75 72 73 6f   map those curso
39304 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 62  r numbers into b
39305 69 74 73 20 30 20 74 68 72 6f 75 67 68 20 35 2e  its 0 through 5.
39306 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68  ..**..** Note th
39307 61 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 69  at the mapping i
39308 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
39309 79 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 74  y ordered.  In t
3930a 68 65 20 65 78 61 6d 70 6c 65 0d 0a 2a 2a 20 61  he example..** a
3930b 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e  bove, the mappin
3930c 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20  g might go like 
3930d 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e  this:  4->3, 5->
3930e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0d  1, 8->2, 29->0,.
3930f 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34  .** 57->5, 73->4
39310 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39  .  Or one of 719
39311 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74 69   other combinati
39312 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73 65  ons might be use
39313 64 2e 20 49 74 0d 0a 2a 2a 20 64 6f 65 73 20 6e  d. It..** does n
39314 6f 74 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72  ot really matter
39315 2e 20 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72  .  What is impor
39316 74 61 6e 74 20 69 73 20 74 68 61 74 20 73 70 61  tant is that spa
39317 72 73 65 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 6e  rse cursor..** n
39318 75 6d 62 65 72 73 20 61 6c 6c 20 67 65 74 20 6d  umbers all get m
39319 61 70 70 65 64 20 69 6e 74 6f 20 62 69 74 20 6e  apped into bit n
3931a 75 6d 62 65 72 73 20 74 68 61 74 20 62 65 67 69  umbers that begi
3931b 6e 20 77 69 74 68 20 30 20 61 6e 64 20 63 6f 6e  n with 0 and con
3931c 74 61 69 6e 0d 0a 2a 2a 20 6e 6f 20 67 61 70 73  tain..** no gaps
3931d 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 57 68  ...*/..struct Wh
3931e 65 72 65 4d 61 73 6b 53 65 74 20 7b 0d 0a 20 20  ereMaskSet {..  
3931f 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
39320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39321 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73 69 67   Number of assig
39322 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c 75 65  ned cursor value
39323 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 78 5b 42  s */..  int ix[B
39324 4d 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  MS];            
39325 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
39326 61 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68  assigned to each
39327 20 62 69 74 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f   bit */..};..../
39328 2a 0d 0a 2a 2a 20 41 20 57 68 65 72 65 43 6f 73  *..** A WhereCos
39329 74 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73  t object records
3932a 20 61 20 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65   a lookup strate
3932b 67 79 20 61 6e 64 20 74 68 65 20 65 73 74 69 6d  gy and the estim
3932c 61 74 65 64 0d 0a 2a 2a 20 63 6f 73 74 20 6f 66  ated..** cost of
3932d 20 70 75 72 73 75 69 6e 67 20 74 68 61 74 20 73   pursuing that s
3932e 74 72 61 74 65 67 79 2e 0d 0a 2a 2f 0d 0a 73 74  trategy...*/..st
3932f 72 75 63 74 20 57 68 65 72 65 43 6f 73 74 20 7b  ruct WhereCost {
39330 0d 0a 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c  ..  WherePlan pl
39331 61 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  an;    /* The lo
39332 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20 2a 2f  okup strategy */
39333 0d 0a 20 20 64 6f 75 62 6c 65 20 72 43 6f 73 74  ..  double rCost
39334 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 61 6c  ;      /* Overal
39335 6c 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69  l cost of pursui
39336 6e 67 20 74 68 69 73 20 73 65 61 72 63 68 20 73  ng this search s
39337 74 72 61 74 65 67 79 20 2a 2f 0d 0a 20 20 42 69  trategy */..  Bi
39338 74 6d 61 73 6b 20 75 73 65 64 3b 20 20 20 20 20  tmask used;     
39339 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 63   /* Bitmask of c
3933a 75 72 73 6f 72 73 20 75 73 65 64 20 62 79 20 74  ursors used by t
3933b 68 69 73 20 70 6c 61 6e 20 2a 2f 0d 0a 7d 3b 0d  his plan */..};.
3933c 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 42 69 74 6d 61 73  .../*..** Bitmas
3933d 6b 73 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61  ks for the opera
3933e 74 6f 72 73 20 74 68 61 74 20 69 6e 64 69 63 65  tors that indice
3933f 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78  s are able to ex
39340 70 6c 6f 69 74 2e 20 20 41 6e 0d 0a 2a 2a 20 4f  ploit.  An..** O
39341 52 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  R-ed combination
39342 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73   of these values
39343 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 68 65   can be used whe
39344 6e 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 0d  n searching for.
39345 0a 2a 2a 20 74 65 72 6d 73 20 69 6e 20 74 68 65  .** terms in the
39346 20 77 68 65 72 65 20 63 6c 61 75 73 65 2e 0d 0a   where clause...
39347 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49  */..#define WO_I
39348 4e 20 20 20 20 20 30 78 30 30 31 0d 0a 23 64 65  N     0x001..#de
39349 66 69 6e 65 20 57 4f 5f 45 51 20 20 20 20 20 30  fine WO_EQ     0
3934a 78 30 30 32 0d 0a 23 64 65 66 69 6e 65 20 57 4f  x002..#define WO
3934b 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _LT     (WO_EQ<<
3934c 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0d 0a  (TK_LT-TK_EQ))..
3934d 23 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20  #define WO_LE   
3934e 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45    (WO_EQ<<(TK_LE
3934f 2d 54 4b 5f 45 51 29 29 0d 0a 23 64 65 66 69 6e  -TK_EQ))..#defin
39350 65 20 57 4f 5f 47 54 20 20 20 20 20 28 57 4f 5f  e WO_GT     (WO_
39351 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45 51  EQ<<(TK_GT-TK_EQ
39352 29 29 0d 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47  ))..#define WO_G
39353 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
39354 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0d 0a 23 64  K_GE-TK_EQ))..#d
39355 65 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20 20  efine WO_MATCH  
39356 30 78 30 34 30 0d 0a 23 64 65 66 69 6e 65 20 57  0x040..#define W
39357 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38 30 0d 0a  O_ISNULL 0x080..
39358 23 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20  #define WO_OR   
39359 20 20 30 78 31 30 30 20 20 20 20 20 20 20 2f 2a    0x100       /*
3935a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d   Two or more OR-
3935b 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20  connected terms 
3935c 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41  */..#define WO_A
3935d 4e 44 20 20 20 20 30 78 32 30 30 20 20 20 20 20  ND    0x200     
3935e 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65    /* Two or more
3935f 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 74   AND-connected t
39360 65 72 6d 73 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  erms */..#define
39361 20 57 4f 5f 4e 4f 4f 50 20 20 20 30 78 38 30 30   WO_NOOP   0x800
39362 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74         /* This t
39363 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73  erm does not res
39364 74 72 69 63 74 20 73 65 61 72 63 68 20 73 70 61  trict search spa
39365 63 65 20 2a 2f 0d 0a 0d 0a 23 64 65 66 69 6e 65  ce */....#define
39366 20 57 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66 66   WO_ALL    0xfff
39367 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
39368 66 20 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20 57  f all possible W
39369 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 23  O_* values */..#
3936a 64 65 66 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45  define WO_SINGLE
3936b 20 30 78 30 66 66 20 20 20 20 20 20 20 2f 2a 20   0x0ff       /* 
3936c 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d  Mask of all non-
3936d 63 6f 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61  compound WO_* va
3936e 6c 75 65 73 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  lues */..../*..*
3936f 2a 20 56 61 6c 75 65 20 66 6f 72 20 77 73 46 6c  * Value for wsFl
39370 61 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ags returned by 
39371 62 65 73 74 49 6e 64 65 78 28 29 20 61 6e 64 20  bestIndex() and 
39372 73 74 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 57 68  stored in..** Wh
39373 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73  ereLevel.wsFlags
39374 2e 20 20 54 68 65 73 65 20 66 6c 61 67 73 20 64  .  These flags d
39375 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 20 73  etermine which s
39376 65 61 72 63 68 0d 0a 2a 2a 20 73 74 72 61 74 65  earch..** strate
39377 67 69 65 73 20 61 72 65 20 61 70 70 72 6f 70 72  gies are appropr
39378 69 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  iate...**..** Th
39379 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63  e least signific
3937a 61 6e 74 20 31 32 20 62 69 74 73 20 69 73 20 72  ant 12 bits is r
3937b 65 73 65 72 76 65 64 20 61 73 20 61 20 6d 61 73  eserved as a mas
3937c 6b 20 66 6f 72 20 57 4f 5f 20 76 61 6c 75 65 73  k for WO_ values
3937d 20 61 62 6f 76 65 2e 0d 0a 2a 2a 20 54 68 65 20   above...** The 
3937e 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61  WhereLevel.wsFla
3937f 67 73 20 66 69 65 6c 64 20 69 73 20 75 73 75 61  gs field is usua
39380 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e  lly set to WO_IN
39381 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c  |WO_EQ|WO_ISNULL
39382 2e 0d 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65  ...** But if the
39383 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69   table is the ri
39384 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
39385 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72 65 4c  eft join, WhereL
39386 65 76 65 6c 2e 77 73 46 6c 61 67 73 0d 0a 2a 2a  evel.wsFlags..**
39387 20 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e   is set to WO_IN
39388 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68 65  |WO_EQ.  The Whe
39389 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20  reLevel.wsFlags 
3938a 66 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20 62  field can then b
3938b 65 20 75 73 65 64 20 61 73 0d 0a 2a 2a 20 74 68  e used as..** th
3938c 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72  e "op" parameter
3938d 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65   to findTerm whe
3938e 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69  n we are resolvi
3938f 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  ng equality cons
39390 74 72 61 69 6e 74 73 2e 0d 0a 2a 2a 20 49 53 4e  traints...** ISN
39391 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ULL constraints 
39392 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65  will then not be
39393 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69 67   used on the rig
39394 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c 65  ht table of a le
39395 66 74 0d 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69  ft..** join.  Ti
39396 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64 20  ckets #2177 and 
39397 23 32 31 38 39 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  #2189...*/..#def
39398 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  ine WHERE_ROWID_
39399 45 51 20 20 20 20 20 30 78 30 30 30 30 31 30 30  EQ     0x0000100
3939a 30 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52  0  /* rowid=EXPR
3939b 20 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e   or rowid IN (..
3939c 2e 29 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57  .) */..#define W
3939d 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
3939e 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a    0x00002000  /*
3939f 20 72 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f   rowid<EXPR and/
393a0 6f 72 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f  or rowid>EXPR */
393a1 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  ..#define WHERE_
393a2 43 4f 4c 55 4d 4e 5f 45 51 20 20 20 20 30 78 30  COLUMN_EQ    0x0
393a3 30 30 31 30 30 30 30 20 20 2f 2a 20 78 3d 45 58  0010000  /* x=EX
393a4 50 52 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29  PR or x IN (...)
393a5 20 6f 72 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f   or x IS NULL */
393a6 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  ..#define WHERE_
393a7 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30  COLUMN_RANGE 0x0
393a8 30 30 32 30 30 30 30 20 20 2f 2a 20 78 3c 45 58  0020000  /* x<EX
393a9 50 52 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52  PR and/or x>EXPR
393aa 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48 45   */..#define WHE
393ab 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20  RE_COLUMN_IN    
393ac 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 78  0x00040000  /* x
393ad 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0d 0a 23 64   IN (...) */..#d
393ae 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55  efine WHERE_COLU
393af 4d 4e 5f 4e 55 4c 4c 20 20 30 78 30 30 30 38 30  MN_NULL  0x00080
393b0 30 30 30 20 20 2f 2a 20 78 20 49 53 20 4e 55 4c  000  /* x IS NUL
393b1 4c 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48  L */..#define WH
393b2 45 52 45 5f 49 4e 44 45 58 45 44 20 20 20 20 20  ERE_INDEXED     
393b3 20 30 78 30 30 30 66 30 30 30 30 20 20 2f 2a 20   0x000f0000  /* 
393b4 41 6e 79 74 68 69 6e 67 20 74 68 61 74 20 75 73  Anything that us
393b5 65 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0d 0a  es an index */..
393b6 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4e 4f  #define WHERE_NO
393b7 54 5f 46 55 4c 4c 53 43 41 4e 20 30 78 31 30 30  T_FULLSCAN 0x100
393b8 66 33 30 30 30 20 20 2f 2a 20 44 6f 65 73 20 6e  f3000  /* Does n
393b9 6f 74 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62  ot do a full tab
393ba 6c 65 20 73 63 61 6e 20 2a 2f 0d 0a 23 64 65 66  le scan */..#def
393bb 69 6e 65 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c  ine WHERE_IN_ABL
393bc 45 20 20 20 20 20 20 30 78 30 30 30 66 31 30 30  E      0x000f100
393bd 30 20 20 2f 2a 20 41 62 6c 65 20 74 6f 20 73 75  0  /* Able to su
393be 70 70 6f 72 74 20 61 6e 20 49 4e 20 6f 70 65 72  pport an IN oper
393bf 61 74 6f 72 20 2a 2f 0d 0a 23 64 65 66 69 6e 65  ator */..#define
393c0 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54   WHERE_TOP_LIMIT
393c1 20 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20      0x00100000  
393c2 2f 2a 20 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d  /* x<EXPR or x<=
393c3 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20  EXPR constraint 
393c4 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52  */..#define WHER
393c5 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 20 20 20 30  E_BTM_LIMIT    0
393c6 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 78 3e  x00200000  /* x>
393c7 45 58 50 52 20 6f 72 20 78 3e 3d 45 58 50 52 20  EXPR or x>=EXPR 
393c8 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a 23  constraint */..#
393c9 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42 4f 54  define WHERE_BOT
393ca 48 5f 4c 49 4d 49 54 20 20 20 30 78 30 30 33 30  H_LIMIT   0x0030
393cb 30 30 30 30 20 20 2f 2a 20 42 6f 74 68 20 78 3e  0000  /* Both x>
393cc 45 58 50 52 20 61 6e 64 20 78 3c 45 58 50 52 20  EXPR and x<EXPR 
393cd 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52  */..#define WHER
393ce 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30  E_IDX_ONLY     0
393cf 78 30 30 38 30 30 30 30 30 20 20 2f 2a 20 55 73  x00800000  /* Us
393d0 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f  e index only - o
393d1 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 23 64  mit table */..#d
393d2 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
393d3 52 42 59 20 20 20 20 20 20 30 78 30 31 30 30 30  RBY      0x01000
393d4 30 30 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77  000  /* Output w
393d5 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f  ill appear in co
393d6 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0d 0a  rrect order */..
393d7 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45  #define WHERE_RE
393d8 56 45 52 53 45 20 20 20 20 20 20 30 78 30 32 30  VERSE      0x020
393d9 30 30 30 30 30 20 20 2f 2a 20 53 63 61 6e 20 69  00000  /* Scan i
393da 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  n reverse order 
393db 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52  */..#define WHER
393dc 45 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 30  E_UNIQUE       0
393dd 78 30 34 30 30 30 30 30 30 20 20 2f 2a 20 53 65  x04000000  /* Se
393de 6c 65 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68  lects no more th
393df 61 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f 0d 0a 23  an one row */..#
393e0 64 65 66 69 6e 65 20 57 48 45 52 45 5f 56 49 52  define WHERE_VIR
393e1 54 55 41 4c 54 41 42 4c 45 20 30 78 30 38 30 30  TUALTABLE 0x0800
393e2 30 30 30 30 20 20 2f 2a 20 55 73 65 20 76 69 72  0000  /* Use vir
393e3 74 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65  tual-table proce
393e4 73 73 69 6e 67 20 2a 2f 0d 0a 23 64 65 66 69 6e  ssing */..#defin
393e5 65 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  e WHERE_MULTI_OR
393e6 20 20 20 20 20 30 78 31 30 30 30 30 30 30 30 20       0x10000000 
393e7 20 2f 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75 6c   /* OR using mul
393e8 74 69 70 6c 65 20 69 6e 64 69 63 65 73 20 2a 2f  tiple indices */
393e9 0d 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  ..#define WHERE_
393ea 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 30 78 32  TEMP_INDEX   0x2
393eb 30 30 30 30 30 30 30 20 20 2f 2a 20 55 73 65 73  0000000  /* Uses
393ec 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 69 6e   an ephemeral in
393ed 64 65 78 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  dex */..#define 
393ee 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 20 20  WHERE_DISTINCT  
393ef 20 20 20 30 78 34 30 30 30 30 30 30 30 20 20 2f     0x40000000  /
393f0 2a 20 43 6f 72 72 65 63 74 20 6f 72 64 65 72 20  * Correct order 
393f1 66 6f 72 20 44 49 53 54 49 4e 43 54 20 2a 2f 0d  for DISTINCT */.
393f2 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69 61  .../*..** Initia
393f3 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61  lize a prealloca
393f4 74 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20  ted WhereClause 
393f5 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a  structure...*/..
393f6 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
393f7 65 43 6c 61 75 73 65 49 6e 69 74 28 0d 0a 20 20  eClauseInit(..  
393f8 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
393f9 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
393fa 57 68 65 72 65 43 6c 61 75 73 65 20 74 6f 20 62  WhereClause to b
393fb 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  e initialized */
393fc 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
393fd 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
393fe 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
393ff 65 78 74 20 2a 2f 0d 0a 20 20 57 68 65 72 65 4d  ext */..  WhereM
39400 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
39401 2c 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72  ,  /* Mapping fr
39402 6f 6d 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20  om table cursor 
39403 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61  numbers to bitma
39404 73 6b 73 20 2a 2f 0d 0a 20 20 75 31 36 20 77 63  sks */..  u16 wc
39405 74 72 6c 46 6c 61 67 73 20 20 20 20 20 20 20 20  trlFlags        
39406 20 20 20 2f 2a 20 4d 69 67 68 74 20 69 6e 63 6c     /* Might incl
39407 75 64 65 20 57 48 45 52 45 5f 41 4e 44 5f 4f 4e  ude WHERE_AND_ON
39408 4c 59 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 70 57 43  LY */..){..  pWC
39409 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  ->pParse = pPars
3940a 65 3b 0d 0a 20 20 70 57 43 2d 3e 70 4d 61 73 6b  e;..  pWC->pMask
3940b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74 3b 0d  Set = pMaskSet;.
3940c 0a 20 20 70 57 43 2d 3e 70 4f 75 74 65 72 20 3d  .  pWC->pOuter =
3940d 20 30 3b 0d 0a 20 20 70 57 43 2d 3e 6e 54 65 72   0;..  pWC->nTer
3940e 6d 20 3d 20 30 3b 0d 0a 20 20 70 57 43 2d 3e 6e  m = 0;..  pWC->n
3940f 53 6c 6f 74 20 3d 20 41 72 72 61 79 53 69 7a 65  Slot = ArraySize
39410 28 70 57 43 2d 3e 61 53 74 61 74 69 63 29 3b 0d  (pWC->aStatic);.
39411 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70 57 43 2d  .  pWC->a = pWC-
39412 3e 61 53 74 61 74 69 63 3b 0d 0a 20 20 70 57 43  >aStatic;..  pWC
39413 2d 3e 76 6d 61 73 6b 20 3d 20 30 3b 0d 0a 20 20  ->vmask = 0;..  
39414 70 57 43 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20  pWC->wctrlFlags 
39415 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b 0d 0a 7d  = wctrlFlags;..}
39416 0d 0a 0d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  ..../* Forward r
39417 65 66 65 72 65 6e 63 65 20 2a 2f 0d 0a 73 74 61  eference */..sta
39418 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c  tic void whereCl
39419 61 75 73 65 43 6c 65 61 72 28 57 68 65 72 65 43  auseClear(WhereC
3941a 6c 61 75 73 65 2a 29 3b 0d 0a 0d 0a 2f 2a 0d 0a  lause*);..../*..
3941b 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
3941c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
3941d 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65  ted with a Where
3941e 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0d 0a  OrInfo object...
3941f 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
39420 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74  whereOrInfoDelet
39421 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57  e(sqlite3 *db, W
39422 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b 0d  hereOrInfo *p){.
39423 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  .  whereClauseCl
39424 65 61 72 28 26 70 2d 3e 77 63 29 3b 0d 0a 20 20  ear(&p->wc);..  
39425 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
39426 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  , p);..}..../*..
39427 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
39428 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
39429 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65  ted with a Where
3942a 41 6e 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0d  AndInfo object..
3942b 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
3942c 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c   whereAndInfoDel
3942d 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
3942e 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70   WhereAndInfo *p
3942f 29 7b 0d 0a 20 20 77 68 65 72 65 43 6c 61 75 73  ){..  whereClaus
39430 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0d  eClear(&p->wc);.
39431 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
39432 28 64 62 2c 20 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  (db, p);..}..../
39433 2a 0d 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  *..** Deallocate
39434 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
39435 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 57  tructure.  The W
39436 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
39437 74 75 72 65 0d 0a 2a 2a 20 69 74 73 65 6c 66 20  ture..** itself 
39438 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54  is not freed.  T
39439 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
3943a 68 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68  he inverse of wh
3943b 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e  ereClauseInit().
3943c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
3943d 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65  d whereClauseCle
3943e 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  ar(WhereClause *
3943f 70 57 43 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d  pWC){..  int i;.
39440 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b  .  WhereTerm *a;
39441 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
39442 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64  = pWC->pParse->d
39443 62 3b 0d 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d  b;..  for(i=pWC-
39444 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d  >nTerm-1, a=pWC-
39445 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61  >a; i>=0; i--, a
39446 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 61 2d  ++){..    if( a-
39447 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
39448 44 59 4e 41 4d 49 43 20 29 7b 0d 0a 20 20 20 20  DYNAMIC ){..    
39449 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
3944a 65 74 65 28 64 62 2c 20 61 2d 3e 70 45 78 70 72  ete(db, a->pExpr
3944b 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
3944c 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20  f( a->wtFlags & 
3944d 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 29 7b 0d 0a  TERM_ORINFO ){..
3944e 20 20 20 20 20 20 77 68 65 72 65 4f 72 49 6e 66        whereOrInf
3944f 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 75  oDelete(db, a->u
39450 2e 70 4f 72 49 6e 66 6f 29 3b 0d 0a 20 20 20 20  .pOrInfo);..    
39451 7d 65 6c 73 65 20 69 66 28 20 61 2d 3e 77 74 46  }else if( a->wtF
39452 6c 61 67 73 20 26 20 54 45 52 4d 5f 41 4e 44 49  lags & TERM_ANDI
39453 4e 46 4f 20 29 7b 0d 0a 20 20 20 20 20 20 77 68  NFO ){..      wh
39454 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65  ereAndInfoDelete
39455 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e 64 49 6e  (db, a->u.pAndIn
39456 66 6f 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  fo);..    }..  }
39457 0d 0a 20 20 69 66 28 20 70 57 43 2d 3e 61 21 3d  ..  if( pWC->a!=
39458 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0d  pWC->aStatic ){.
39459 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
3945a 65 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b 0d  ee(db, pWC->a);.
3945b 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
3945c 2a 20 41 64 64 20 61 20 73 69 6e 67 6c 65 20 6e  * Add a single n
3945d 65 77 20 57 68 65 72 65 54 65 72 6d 20 65 6e 74  ew WhereTerm ent
3945e 72 79 20 74 6f 20 74 68 65 20 57 68 65 72 65 43  ry to the WhereC
3945f 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43  lause object pWC
39460 2e 0d 0a 2a 2a 20 54 68 65 20 6e 65 77 20 57 68  ...** The new Wh
39461 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 69  ereTerm object i
39462 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  s constructed fr
39463 6f 6d 20 45 78 70 72 20 70 20 61 6e 64 20 77 69  om Expr p and wi
39464 74 68 20 77 74 46 6c 61 67 73 2e 0d 0a 2a 2a 20  th wtFlags...** 
39465 54 68 65 20 69 6e 64 65 78 20 69 6e 20 70 57 43  The index in pWC
39466 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 6e 65 77  ->a[] of the new
39467 20 57 68 65 72 65 54 65 72 6d 20 69 73 20 72 65   WhereTerm is re
39468 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73  turned on succes
39469 73 2e 0d 0a 2a 2a 20 30 20 69 73 20 72 65 74 75  s...** 0 is retu
3946a 72 6e 65 64 20 69 66 20 74 68 65 20 6e 65 77 20  rned if the new 
3946b 57 68 65 72 65 54 65 72 6d 20 63 6f 75 6c 64 20  WhereTerm could 
3946c 6e 6f 74 20 62 65 20 61 64 64 65 64 20 64 75 65  not be added due
3946d 20 74 6f 20 61 20 6d 65 6d 6f 72 79 0d 0a 2a 2a   to a memory..**
3946e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
3946f 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  r.  The memory a
39470 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72  llocation failur
39471 65 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72 64  e will be record
39472 65 64 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 64 62  ed in..** the db
39473 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  ->mallocFailed f
39474 6c 61 67 20 73 6f 20 74 68 61 74 20 68 69 67 68  lag so that high
39475 65 72 2d 6c 65 76 65 6c 20 66 75 6e 63 74 69 6f  er-level functio
39476 6e 73 20 63 61 6e 20 64 65 74 65 63 74 20 69 74  ns can detect it
39477 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
39478 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69 6e 63 72  outine will incr
39479 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
3947a 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72   the pWC->a[] ar
3947b 72 61 79 20 61 73 20 6e 65 63 65 73 73 61 72 79  ray as necessary
3947c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
3947d 20 77 74 46 6c 61 67 73 20 61 72 67 75 6d 65 6e   wtFlags argumen
3947e 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f  t includes TERM_
3947f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72 65  DYNAMIC, then re
39480 73 70 6f 6e 73 69 62 69 6c 69 74 79 0d 0a 2a 2a  sponsibility..**
39481 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
39482 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
39483 20 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20   assumed by the 
39484 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
39485 63 74 20 70 57 43 2e 0d 0a 2a 2a 20 54 68 69 73  ct pWC...** This
39486 20 69 73 20 74 72 75 65 20 65 76 65 6e 20 69 66   is true even if
39487 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61   this routine fa
39488 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ils to allocate 
39489 61 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 2e  a new WhereTerm.
3948a 0d 0a 2a 2a 0d 0a 2a 2a 20 57 41 52 4e 49 4e 47  ..**..** WARNING
3948b 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  :  This routine 
3948c 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63 61 74 65  might reallocate
3948d 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20   the space used 
3948e 74 6f 20 73 74 6f 72 65 0d 0a 2a 2a 20 57 68 65  to store..** Whe
3948f 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70 6f  reTerms.  All po
39490 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72 65 54  inters to WhereT
39491 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65 20 69  erms should be i
39492 6e 76 61 6c 69 64 61 74 65 64 20 61 66 74 65 72  nvalidated after
39493 0d 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69  ..** calling thi
39494 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75 63 68  s routine.  Such
39495 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20 62 65   pointers may be
39496 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 62   reinitialized b
39497 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0d 0a 2a  y referencing..*
39498 2a 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61  * the pWC->a[] a
39499 72 72 61 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rray...*/..stati
3949a 63 20 69 6e 74 20 77 68 65 72 65 43 6c 61 75 73  c int whereClaus
3949b 65 49 6e 73 65 72 74 28 57 68 65 72 65 43 6c 61  eInsert(WhereCla
3949c 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a  use *pWC, Expr *
3949d 70 2c 20 75 38 20 77 74 46 6c 61 67 73 29 7b 0d  p, u8 wtFlags){.
3949e 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
3949f 65 72 6d 3b 0d 0a 20 20 69 6e 74 20 69 64 78 3b  erm;..  int idx;
394a0 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 77 74  ..  testcase( wt
394a1 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52  Flags & TERM_VIR
394a2 54 55 41 4c 20 29 3b 20 20 2f 2a 20 45 56 3a 20  TUAL );  /* EV: 
394a3 52 2d 30 30 32 31 31 2d 31 35 31 30 30 20 2a 2f  R-00211-15100 */
394a4 0d 0a 20 20 69 66 28 20 70 57 43 2d 3e 6e 54 65  ..  if( pWC->nTe
394a5 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c 6f 74 20 29  rm>=pWC->nSlot )
394a6 7b 0d 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  {..    WhereTerm
394a7 20 2a 70 4f 6c 64 20 3d 20 70 57 43 2d 3e 61 3b   *pOld = pWC->a;
394a8 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ..    sqlite3 *d
394a9 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d  b = pWC->pParse-
394aa 3e 64 62 3b 0d 0a 20 20 20 20 70 57 43 2d 3e 61  >db;..    pWC->a
394ab 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
394ac 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
394ad 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d  (pWC->a[0])*pWC-
394ae 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0d 0a 20 20 20  >nSlot*2 );..   
394af 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29   if( pWC->a==0 )
394b0 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 77 74 46  {..      if( wtF
394b1 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41  lags & TERM_DYNA
394b2 4d 49 43 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  MIC ){..        
394b3 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
394b4 65 28 64 62 2c 20 70 29 3b 0d 0a 20 20 20 20 20  e(db, p);..     
394b5 20 7d 0d 0a 20 20 20 20 20 20 70 57 43 2d 3e 61   }..      pWC->a
394b6 20 3d 20 70 4f 6c 64 3b 0d 0a 20 20 20 20 20 20   = pOld;..      
394b7 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d  return 0;..    }
394b8 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43  ..    memcpy(pWC
394b9 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f  ->a, pOld, sizeo
394ba 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43  f(pWC->a[0])*pWC
394bb 2d 3e 6e 54 65 72 6d 29 3b 0d 0a 20 20 20 20 69  ->nTerm);..    i
394bc 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53  f( pOld!=pWC->aS
394bd 74 61 74 69 63 20 29 7b 0d 0a 20 20 20 20 20 20  tatic ){..      
394be 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
394bf 2c 20 70 4f 6c 64 29 3b 0d 0a 20 20 20 20 7d 0d  , pOld);..    }.
394c0 0a 20 20 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20  .    pWC->nSlot 
394c1 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
394c2 63 53 69 7a 65 28 64 62 2c 20 70 57 43 2d 3e 61  cSize(db, pWC->a
394c3 29 2f 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b  )/sizeof(pWC->a[
394c4 30 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54 65  0]);..  }..  pTe
394c5 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
394c6 20 3d 20 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b 5d   = pWC->nTerm++]
394c7 3b 0d 0a 20 20 70 54 65 72 6d 2d 3e 70 45 78 70  ;..  pTerm->pExp
394c8 72 20 3d 20 70 3b 0d 0a 20 20 70 54 65 72 6d 2d  r = p;..  pTerm-
394c9 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46 6c 61  >wtFlags = wtFla
394ca 67 73 3b 0d 0a 20 20 70 54 65 72 6d 2d 3e 70 57  gs;..  pTerm->pW
394cb 43 20 3d 20 70 57 43 3b 0d 0a 20 20 70 54 65 72  C = pWC;..  pTer
394cc 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
394cd 0d 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0d  ..  return idx;.
394ce 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
394cf 73 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74 69  s routine identi
394d0 66 69 65 73 20 73 75 62 65 78 70 72 65 73 73 69  fies subexpressi
394d1 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  ons in the WHERE
394d2 20 63 6c 61 75 73 65 20 77 68 65 72 65 0d 0a 2a   clause where..*
394d3 2a 20 65 61 63 68 20 73 75 62 65 78 70 72 65 73  * each subexpres
394d4 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65  sion is separate
394d5 64 20 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65  d by the AND ope
394d6 72 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74  rator or some ot
394d7 68 65 72 0d 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  her..** operator
394d8 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
394d9 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 20  e op parameter. 
394da 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
394db 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a 20 69   structure..** i
394dc 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 70 6f  s filled with po
394dd 69 6e 74 65 72 73 20 74 6f 20 73 75 62 65 78 70  inters to subexp
394de 72 65 73 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65  ressions.  For e
394df 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  xample:..**..** 
394e0 20 20 20 57 48 45 52 45 20 20 61 3d 3d 27 68 65     WHERE  a=='he
394e1 6c 6c 6f 27 20 41 4e 44 20 63 6f 61 6c 65 73 63  llo' AND coalesc
394e2 65 28 62 2c 31 31 29 3c 31 30 20 41 4e 44 20 28  e(b,11)<10 AND (
394e3 63 2b 31 32 21 3d 64 20 4f 52 20 63 3d 3d 32 32  c+12!=d OR c==22
394e4 29 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  )..**           
394e5 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c  \________/     \
394e6 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
394e7 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
394e8 5f 5f 5f 5f 5f 5f 2f 0d 0a 2a 2a 20 20 20 20 20  ______/..**     
394e9 20 20 20 20 20 20 20 73 6c 6f 74 5b 30 5d 20 20         slot[0]  
394ea 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 31            slot[1
394eb 5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ]               
394ec 73 6c 6f 74 5b 32 5d 0d 0a 2a 2a 0d 0a 2a 2a 20  slot[2]..**..** 
394ed 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48 45  The original WHE
394ee 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70 45 78  RE clause in pEx
394ef 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65 64 2e  pr is unaltered.
394f0 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69    All this routi
394f1 6e 65 0d 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d  ne..** does is m
394f2 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69  ake slot[] entri
394f3 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62 73  es point to subs
394f4 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e 20  tructure within 
394f5 70 45 78 70 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  pExpr...**..** I
394f6 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  n the previous s
394f7 65 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74  entence and in t
394f8 68 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f  he diagram, "slo
394f9 74 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0d 0a  t[]" refers to..
394fa 2a 2a 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  ** the WhereClau
394fb 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54  se.a[] array.  T
394fc 68 65 20 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20  he slot[] array 
394fd 67 72 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20  grows as needed 
394fe 74 6f 20 63 6f 6e 74 61 69 6e 0d 0a 2a 2a 20 61  to contain..** a
394ff 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  ll terms of the 
39500 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0d 0a 2a  WHERE clause...*
39501 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  /..static void w
39502 68 65 72 65 53 70 6c 69 74 28 57 68 65 72 65 43  hereSplit(WhereC
39503 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72  lause *pWC, Expr
39504 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70 29   *pExpr, int op)
39505 7b 0d 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20 28  {..  pWC->op = (
39506 75 38 29 6f 70 3b 0d 0a 20 20 69 66 28 20 70 45  u8)op;..  if( pE
39507 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  xpr==0 ) return;
39508 0d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ..  if( pExpr->o
39509 70 21 3d 6f 70 20 29 7b 0d 0a 20 20 20 20 77 68  p!=op ){..    wh
3950a 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
3950b 70 57 43 2c 20 70 45 78 70 72 2c 20 30 29 3b 0d  pWC, pExpr, 0);.
3950c 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 77  .  }else{..    w
3950d 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70  hereSplit(pWC, p
3950e 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 6f 70 29  Expr->pLeft, op)
3950f 3b 0d 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69  ;..    whereSpli
39510 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 52  t(pWC, pExpr->pR
39511 69 67 68 74 2c 20 6f 70 29 3b 0d 0a 20 20 7d 0d  ight, op);..  }.
39512 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69  .}..../*..** Ini
39513 74 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65  tialize an expre
39514 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 28  ssion mask set (
39515 61 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 6f  a WhereMaskSet o
39516 62 6a 65 63 74 29 0d 0a 2a 2f 0d 0a 23 64 65 66  bject)..*/..#def
39517 69 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65 74 28  ine initMaskSet(
39518 50 29 20 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c  P)  memset(P, 0,
39519 20 73 69 7a 65 6f 66 28 2a 50 29 29 0d 0a 0d 0a   sizeof(*P))....
3951a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
3951b 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68  e bitmask for th
3951c 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20 6e  e given cursor n
3951d 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20 30  umber.  Return 0
3951e 20 69 66 0d 0a 2a 2a 20 69 43 75 72 73 6f 72 20   if..** iCursor 
3951f 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65  is not in the se
39520 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 42  t...*/..static B
39521 69 74 6d 61 73 6b 20 67 65 74 4d 61 73 6b 28 57  itmask getMask(W
39522 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
39523 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73  skSet, int iCurs
39524 6f 72 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  or){..  int i;..
39525 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53    assert( pMaskS
39526 65 74 2d 3e 6e 3c 3d 28 69 6e 74 29 73 69 7a 65  et->n<=(int)size
39527 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29 3b  of(Bitmask)*8 );
39528 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
39529 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29  MaskSet->n; i++)
3952a 7b 0d 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b  {..    if( pMask
3952b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72  Set->ix[i]==iCur
3952c 73 6f 72 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  sor ){..      re
3952d 74 75 72 6e 20 28 28 42 69 74 6d 61 73 6b 29 31  turn ((Bitmask)1
3952e 29 3c 3c 69 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  )<<i;..    }..  
3952f 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  }..  return 0;..
39530 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72 65 61  }..../*..** Crea
39531 74 65 20 61 20 6e 65 77 20 6d 61 73 6b 20 66 6f  te a new mask fo
39532 72 20 63 75 72 73 6f 72 20 69 43 75 72 73 6f 72  r cursor iCursor
39533 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 72 65 20  ...**..** There 
39534 69 73 20 6f 6e 65 20 63 75 72 73 6f 72 20 70 65  is one cursor pe
39535 72 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  r table in the F
39536 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65  ROM clause.  The
39537 20 6e 75 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 74   number of..** t
39538 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f  ables in the FRO
39539 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69  M clause is limi
3953a 74 65 64 20 62 79 20 61 20 74 65 73 74 20 65 61  ted by a test ea
3953b 72 6c 79 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 73  rly in the..** s
3953c 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
3953d 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 6f 20  () routine.  So 
3953e 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  we know that the
3953f 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 5d 0d   pMaskSet->ix[].
39540 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c 20 6e  .** array will n
39541 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e 0d 0a  ever overflow...
39542 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
39543 63 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72 65  createMask(Where
39544 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
39545 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b  t, int iCursor){
39546 0d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73  ..  assert( pMas
39547 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53  kSet->n < ArrayS
39548 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78  ize(pMaskSet->ix
39549 29 20 29 3b 0d 0a 20 20 70 4d 61 73 6b 53 65 74  ) );..  pMaskSet
3954a 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e  ->ix[pMaskSet->n
3954b 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0d 0a  ++] = iCursor;..
3954c 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
3954d 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 28   routine walks (
3954e 72 65 63 75 72 73 69 76 65 6c 79 29 20 61 6e 20  recursively) an 
3954f 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
39550 61 6e 64 20 67 65 6e 65 72 61 74 65 73 0d 0a 2a  and generates..*
39551 2a 20 61 20 62 69 74 6d 61 73 6b 20 69 6e 64 69  * a bitmask indi
39552 63 61 74 69 6e 67 20 77 68 69 63 68 20 74 61 62  cating which tab
39553 6c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20  les are used in 
39554 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 0d  that expression.
39555 0a 2a 2a 20 74 72 65 65 2e 0d 0a 2a 2a 0d 0a 2a  .** tree...**..*
39556 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74  * In order for t
39557 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 77  his routine to w
39558 6f 72 6b 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  ork, the calling
39559 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 68   function must h
3955a 61 76 65 0d 0a 2a 2a 20 70 72 65 76 69 6f 75 73  ave..** previous
3955b 6c 79 20 69 6e 76 6f 6b 65 64 20 73 71 6c 69 74  ly invoked sqlit
3955c 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
3955d 65 73 28 29 20 6f 6e 20 74 68 65 20 65 78 70 72  es() on the expr
3955e 65 73 73 69 6f 6e 2e 20 20 53 65 65 0d 0a 2a 2a  ession.  See..**
3955f 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d   the header comm
39560 65 6e 74 20 6f 6e 20 74 68 61 74 20 72 6f 75 74  ent on that rout
39561 69 6e 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ine for addition
39562 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0d  al information..
39563 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 52  .** The sqlite3R
39564 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
39565 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f 6b 73  ) routines looks
39566 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   for column name
39567 73 20 61 6e 64 0d 0a 2a 2a 20 73 65 74 73 20 74  s and..** sets t
39568 68 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f 20  heir opcodes to 
39569 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74 68  TK_COLUMN and th
3956a 65 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65 20  eir Expr.iTable 
3956b 66 69 65 6c 64 73 20 74 6f 0d 0a 2a 2a 20 74 68  fields to..** th
3956c 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
3956d 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
3956e 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
3956f 20 6a 75 73 74 20 68 61 73 20 74 6f 0d 0a 2a 2a   just has to..**
39570 20 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20 63   translate the c
39571 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
39572 74 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65  to bitmask value
39573 73 20 61 6e 64 20 4f 52 20 61 6c 6c 0d 0a 2a 2a  s and OR all..**
39574 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f   the bitmasks to
39575 67 65 74 68 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61  gether...*/..sta
39576 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
39577 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 57  ListTableUsage(W
39578 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45 78  hereMaskSet*, Ex
39579 70 72 4c 69 73 74 2a 29 3b 0d 0a 73 74 61 74 69  prList*);..stati
3957a 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
3957b 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
3957c 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65  hereMaskSet*, Se
3957d 6c 65 63 74 2a 29 3b 0d 0a 73 74 61 74 69 63 20  lect*);..static 
3957e 42 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c  Bitmask exprTabl
3957f 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b  eUsage(WhereMask
39580 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45  Set *pMaskSet, E
39581 78 70 72 20 2a 70 29 7b 0d 0a 20 20 42 69 74 6d  xpr *p){..  Bitm
39582 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0d 0a 20  ask mask = 0;.. 
39583 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
39584 72 6e 20 30 3b 0d 0a 20 20 69 66 28 20 70 2d 3e  rn 0;..  if( p->
39585 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
39586 0d 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67 65 74  ..    mask = get
39587 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
39588 2d 3e 69 54 61 62 6c 65 29 3b 0d 0a 20 20 20 20  ->iTable);..    
39589 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0d 0a 20 20  return mask;..  
3958a 7d 0d 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72  }..  mask = expr
3958b 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
3958c 53 65 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b  Set, p->pRight);
3958d 0d 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72  ..  mask |= expr
3958e 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
3958f 53 65 74 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0d  Set, p->pLeft);.
39590 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72  .  if( ExprHasPr
39591 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73  operty(p, EP_xIs
39592 53 65 6c 65 63 74 29 20 29 7b 0d 0a 20 20 20 20  Select) ){..    
39593 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65  mask |= exprSele
39594 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  ctTableUsage(pMa
39595 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53 65 6c  skSet, p->x.pSel
39596 65 63 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  ect);..  }else{.
39597 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
39598 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
39599 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70  pMaskSet, p->x.p
3959a 4c 69 73 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  List);..  }..  r
3959b 65 74 75 72 6e 20 6d 61 73 6b 3b 0d 0a 7d 0d 0a  eturn mask;..}..
3959c 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
3959d 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
3959e 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a  e(WhereMaskSet *
3959f 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69  pMaskSet, ExprLi
395a0 73 74 20 2a 70 4c 69 73 74 29 7b 0d 0a 20 20 69  st *pList){..  i
395a1 6e 74 20 69 3b 0d 0a 20 20 42 69 74 6d 61 73 6b  nt i;..  Bitmask
395a2 20 6d 61 73 6b 20 3d 20 30 3b 0d 0a 20 20 69 66   mask = 0;..  if
395a3 28 20 70 4c 69 73 74 20 29 7b 0d 0a 20 20 20 20  ( pList ){..    
395a4 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
395a5 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a  ->nExpr; i++){..
395a6 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78        mask |= ex
395a7 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
395a8 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b  skSet, pList->a[
395a9 69 5d 2e 70 45 78 70 72 29 3b 0d 0a 20 20 20 20  i].pExpr);..    
395aa 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
395ab 20 6d 61 73 6b 3b 0d 0a 7d 0d 0a 73 74 61 74 69   mask;..}..stati
395ac 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
395ad 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
395ae 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
395af 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20 2a 70  skSet, Select *p
395b0 53 29 7b 0d 0a 20 20 42 69 74 6d 61 73 6b 20 6d  S){..  Bitmask m
395b1 61 73 6b 20 3d 20 30 3b 0d 0a 20 20 77 68 69 6c  ask = 0;..  whil
395b2 65 28 20 70 53 20 29 7b 0d 0a 20 20 20 20 53 72  e( pS ){..    Sr
395b3 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 70 53  cList *pSrc = pS
395b4 2d 3e 70 53 72 63 3b 0d 0a 20 20 20 20 6d 61 73  ->pSrc;..    mas
395b5 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
395b6 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
395b7 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0d 0a  , pS->pEList);..
395b8 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72      mask |= expr
395b9 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70  ListTableUsage(p
395ba 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72  MaskSet, pS->pGr
395bb 6f 75 70 42 79 29 3b 0d 0a 20 20 20 20 6d 61 73  oupBy);..    mas
395bc 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
395bd 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
395be 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b  , pS->pOrderBy);
395bf 0d 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  ..    mask |= ex
395c0 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
395c1 73 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72  skSet, pS->pWher
395c2 65 29 3b 0d 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  e);..    mask |=
395c3 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
395c4 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48  pMaskSet, pS->pH
395c5 61 76 69 6e 67 29 3b 0d 0a 20 20 20 20 69 66 28  aving);..    if(
395c6 20 41 4c 57 41 59 53 28 70 53 72 63 21 3d 30 29   ALWAYS(pSrc!=0)
395c7 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69   ){..      int i
395c8 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ;..      for(i=0
395c9 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 3b 20  ; i<pSrc->nSrc; 
395ca 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 6d  i++){..        m
395cb 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65 63  ask |= exprSelec
395cc 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
395cd 6b 53 65 74 2c 20 70 53 72 63 2d 3e 61 5b 69 5d  kSet, pSrc->a[i]
395ce 2e 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20 20  .pSelect);..    
395cf 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72      mask |= expr
395d0 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
395d1 53 65 74 2c 20 70 53 72 63 2d 3e 61 5b 69 5d 2e  Set, pSrc->a[i].
395d2 70 4f 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  pOn);..      }..
395d3 20 20 20 20 7d 0d 0a 20 20 20 20 70 53 20 3d 20      }..    pS = 
395d4 70 53 2d 3e 70 50 72 69 6f 72 3b 0d 0a 20 20 7d  pS->pPrior;..  }
395d5 0d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  ..  return mask;
395d6 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
395d7 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
395d8 20 67 69 76 65 6e 20 6f 70 65 72 61 74 6f 72 20   given operator 
395d9 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f 70  is one of the op
395da 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 73 0d  erators that is.
395db 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  .** allowed for 
395dc 61 6e 20 69 6e 64 65 78 61 62 6c 65 20 57 48 45  an indexable WHE
395dd 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 20  RE clause term. 
395de 20 54 68 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65   The allowed ope
395df 72 61 74 6f 72 73 20 61 72 65 0d 0a 2a 2a 20 22  rators are..** "
395e0 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 2c 20 22 3c  =", "<", ">", "<
395e1 3d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64 20 22 49  =", ">=", and "I
395e2 4e 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 4d 50 4c  N"...**..** IMPL
395e3 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52  EMENTATION-OF: R
395e4 2d 35 39 39 32 36 2d 32 36 33 39 33 20 54 6f 20  -59926-26393 To 
395e5 62 65 20 75 73 61 62 6c 65 20 62 79 20 61 6e 20  be usable by an 
395e6 69 6e 64 65 78 20 61 20 74 65 72 6d 20 6d 75 73  index a term mus
395e7 74 20 62 65 0d 0a 2a 2a 20 6f 66 20 6f 6e 65 20  t be..** of one 
395e8 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
395e9 20 66 6f 72 6d 73 3a 20 63 6f 6c 75 6d 6e 20 3d   forms: column =
395ea 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6c 75   expression colu
395eb 6d 6e 20 3e 20 65 78 70 72 65 73 73 69 6f 6e 0d  mn > expression.
395ec 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 3e 3d 20 65 78  .** column >= ex
395ed 70 72 65 73 73 69 6f 6e 20 63 6f 6c 75 6d 6e 20  pression column 
395ee 3c 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6c  < expression col
395ef 75 6d 6e 20 3c 3d 20 65 78 70 72 65 73 73 69 6f  umn <= expressio
395f0 6e 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  n..** expression
395f1 20 3d 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73   = column expres
395f2 73 69 6f 6e 20 3e 20 63 6f 6c 75 6d 6e 20 65 78  sion > column ex
395f3 70 72 65 73 73 69 6f 6e 20 3e 3d 20 63 6f 6c 75  pression >= colu
395f4 6d 6e 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  mn..** expressio
395f5 6e 20 3c 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65  n < column expre
395f6 73 73 69 6f 6e 20 3c 3d 20 63 6f 6c 75 6d 6e 20  ssion <= column 
395f7 63 6f 6c 75 6d 6e 20 49 4e 0d 0a 2a 2a 20 28 65  column IN..** (e
395f8 78 70 72 65 73 73 69 6f 6e 2d 6c 69 73 74 29 20  xpression-list) 
395f9 63 6f 6c 75 6d 6e 20 49 4e 20 28 73 75 62 71 75  column IN (subqu
395fa 65 72 79 29 20 63 6f 6c 75 6d 6e 20 49 53 20 4e  ery) column IS N
395fb 55 4c 4c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ULL..*/..static 
395fc 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70 28 69 6e  int allowedOp(in
395fd 74 20 6f 70 29 7b 0d 0a 20 20 61 73 73 65 72 74  t op){..  assert
395fe 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 26 26  ( TK_GT>TK_EQ &&
395ff 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29 3b 0d   TK_GT<TK_GE );.
39600 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54  .  assert( TK_LT
39601 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54 3c  >TK_EQ && TK_LT<
39602 54 4b 5f 47 45 20 29 3b 0d 0a 20 20 61 73 73 65  TK_GE );..  asse
39603 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20  rt( TK_LE>TK_EQ 
39604 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29  && TK_LE<TK_GE )
39605 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f  ;..  assert( TK_
39606 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0d 0a  GE==TK_EQ+4 );..
39607 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f    return op==TK_
39608 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51  IN || (op>=TK_EQ
39609 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c   && op<=TK_GE) |
3960a 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b  | op==TK_ISNULL;
3960b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 77  ..}..../*..** Sw
3960c 61 70 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f  ap two objects o
3960d 66 20 74 79 70 65 20 54 59 50 45 2e 0d 0a 2a 2f  f type TYPE...*/
3960e 0d 0a 23 64 65 66 69 6e 65 20 53 57 41 50 28 54  ..#define SWAP(T
3960f 59 50 45 2c 41 2c 42 29 20 7b 54 59 50 45 20 74  YPE,A,B) {TYPE t
39610 3d 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d 0d 0a  =A; A=B; B=t;}..
39611 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 6d 75 74 65  ../*..** Commute
39612 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
39613 65 72 61 74 6f 72 2e 20 20 45 78 70 72 65 73 73  erator.  Express
39614 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ions of the form
39615 20 22 58 20 6f 70 20 59 22 0d 0a 2a 2a 20 61 72   "X op Y"..** ar
39616 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
39617 20 22 59 20 6f 70 20 58 22 2e 0d 0a 2a 2a 0d 0a   "Y op X"...**..
39618 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6f  ** If a collatio
39619 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 73  n sequence is as
3961a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 69  sociated with ei
3961b 74 68 65 72 20 74 68 65 20 6c 65 66 74 20 6f 72  ther the left or
3961c 20 72 69 67 68 74 0d 0a 2a 2a 20 73 69 64 65 20   right..** side 
3961d 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
3961e 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61 73  n, it remains as
3961f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
39620 65 20 73 61 6d 65 20 73 69 64 65 20 61 66 74 65  e same side afte
39621 72 0d 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74  r..** the commut
39622 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c  ation. So "Y col
39623 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58  late NOCASE op X
39624 22 20 62 65 63 6f 6d 65 73 20 0d 0a 2a 2a 20 22  " becomes ..** "
39625 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45  X collate NOCASE
39626 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73 20   op Y". This is 
39627 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c  because any coll
39628 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f  ation sequence o
39629 6e 0d 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68  n..** the left h
3962a 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f  and side of a co
3962b 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64  mparison overrid
3962c 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e  es any collation
3962d 20 73 65 71 75 65 6e 63 65 20 0d 0a 2a 2a 20 61   sequence ..** a
3962e 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72  ttached to the r
3962f 69 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61  ight. For the sa
39630 6d 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50  me reason the EP
39631 5f 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67  _ExpCollate flag
39632 0d 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d  ..** is not comm
39633 75 74 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  uted...*/..stati
39634 63 20 76 6f 69 64 20 65 78 70 72 43 6f 6d 6d 75  c void exprCommu
39635 74 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  te(Parse *pParse
39636 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0d  , Expr *pExpr){.
39637 0a 20 20 75 31 36 20 65 78 70 52 69 67 68 74 20  .  u16 expRight 
39638 3d 20 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74  = (pExpr->pRight
39639 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
3963a 43 6f 6c 6c 61 74 65 29 3b 0d 0a 20 20 75 31 36  Collate);..  u16
3963b 20 65 78 70 4c 65 66 74 20 3d 20 28 70 45 78 70   expLeft = (pExp
3963c 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20  r->pLeft->flags 
3963d 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29  & EP_ExpCollate)
3963e 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c  ;..  assert( all
3963f 6f 77 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f 70  owedOp(pExpr->op
39640 29 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ) && pExpr->op!=
39641 54 4b 5f 49 4e 20 29 3b 0d 0a 20 20 70 45 78 70  TK_IN );..  pExp
39642 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c  r->pRight->pColl
39643 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
39644 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
39645 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0d 0a 20  xpr->pRight);.. 
39646 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70   pExpr->pLeft->p
39647 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
39648 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
39649 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
3964a 0d 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53 65 71  ..  SWAP(CollSeq
3964b 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d  *,pExpr->pRight-
3964c 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70 4c  >pColl,pExpr->pL
3964d 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0d 0a 20 20  eft->pColl);..  
3964e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66  pExpr->pRight->f
3964f 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70  lags = (pExpr->p
39650 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e  Right->flags & ~
39651 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
39652 20 65 78 70 4c 65 66 74 3b 0d 0a 20 20 70 45 78   expLeft;..  pEx
39653 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73  pr->pLeft->flags
39654 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74   = (pExpr->pLeft
39655 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78  ->flags & ~EP_Ex
39656 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 52  pCollate) | expR
39657 69 67 68 74 3b 0d 0a 20 20 53 57 41 50 28 45 78  ight;..  SWAP(Ex
39658 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68  pr*,pExpr->pRigh
39659 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  t,pExpr->pLeft);
3965a 0d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ..  if( pExpr->o
3965b 70 3e 3d 54 4b 5f 47 54 20 29 7b 0d 0a 20 20 20  p>=TK_GT ){..   
3965c 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d   assert( TK_LT==
3965d 54 4b 5f 47 54 2b 32 20 29 3b 0d 0a 20 20 20 20  TK_GT+2 );..    
3965e 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54  assert( TK_GE==T
3965f 4b 5f 4c 45 2b 32 20 29 3b 0d 0a 20 20 20 20 61  K_LE+2 );..    a
39660 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
39661 45 51 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  EQ );..    asser
39662 74 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29  t( TK_GT<TK_LE )
39663 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
39664 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20  Expr->op>=TK_GT 
39665 26 26 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b  && pExpr->op<=TK
39666 5f 47 45 20 29 3b 0d 0a 20 20 20 20 70 45 78 70  _GE );..    pExp
39667 72 2d 3e 6f 70 20 3d 20 28 28 70 45 78 70 72 2d  r->op = ((pExpr-
39668 3e 6f 70 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b  >op-TK_GT)^2)+TK
39669 5f 47 54 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  _GT;..  }..}....
3966a 2f 2a 0d 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65  /*..** Translate
3966b 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70 65 72   from TK_xx oper
3966c 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20 62 69  ator to WO_xx bi
3966d 74 6d 61 73 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  tmask...*/..stat
3966e 69 63 20 75 31 36 20 6f 70 65 72 61 74 6f 72 4d  ic u16 operatorM
3966f 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0d 0a 20 20  ask(int op){..  
39670 75 31 36 20 63 3b 0d 0a 20 20 61 73 73 65 72 74  u16 c;..  assert
39671 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20  ( allowedOp(op) 
39672 29 3b 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  );..  if( op==TK
39673 5f 49 4e 20 29 7b 0d 0a 20 20 20 20 63 20 3d 20  _IN ){..    c = 
39674 57 4f 5f 49 4e 3b 0d 0a 20 20 7d 65 6c 73 65 20  WO_IN;..  }else 
39675 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  if( op==TK_ISNUL
39676 4c 20 29 7b 0d 0a 20 20 20 20 63 20 3d 20 57 4f  L ){..    c = WO
39677 5f 49 53 4e 55 4c 4c 3b 0d 0a 20 20 7d 65 6c 73  _ISNULL;..  }els
39678 65 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  e{..    assert( 
39679 28 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45  (WO_EQ<<(op-TK_E
3967a 51 29 29 20 3c 20 30 78 37 66 66 66 20 29 3b 0d  Q)) < 0x7fff );.
3967b 0a 20 20 20 20 63 20 3d 20 28 75 31 36 29 28 57  .    c = (u16)(W
3967c 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29  O_EQ<<(op-TK_EQ)
3967d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72  );..  }..  asser
3967e 74 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c  t( op!=TK_ISNULL
3967f 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55 4c 4c   || c==WO_ISNULL
39680 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6f   );..  assert( o
39681 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d 57  p!=TK_IN || c==W
39682 4f 5f 49 4e 20 29 3b 0d 0a 20 20 61 73 73 65 72  O_IN );..  asser
39683 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20  t( op!=TK_EQ || 
39684 63 3d 3d 57 4f 5f 45 51 20 29 3b 0d 0a 20 20 61  c==WO_EQ );..  a
39685 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 54  ssert( op!=TK_LT
39686 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0d   || c==WO_LT );.
39687 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
39688 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45  K_LE || c==WO_LE
39689 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 6f   );..  assert( o
3968a 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57  p!=TK_GT || c==W
3968b 4f 5f 47 54 20 29 3b 0d 0a 20 20 61 73 73 65 72  O_GT );..  asser
3968c 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20  t( op!=TK_GE || 
3968d 63 3d 3d 57 4f 5f 47 45 20 29 3b 0d 0a 20 20 72  c==WO_GE );..  r
3968e 65 74 75 72 6e 20 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn c;..}..../
3968f 2a 0d 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72  *..** Search for
39690 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
39691 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
39692 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
39693 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0d  "X <op> <expr>".
39694 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61  .** where X is a
39695 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
39696 65 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62  e iColumn of tab
39697 6c 65 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e  le iCur and <op>
39698 20 69 73 20 6f 6e 65 20 6f 66 0d 0a 2a 2a 20 74   is one of..** t
39699 68 65 20 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f  he WO_xx operato
3969a 72 20 63 6f 64 65 73 20 73 70 65 63 69 66 69 65  r codes specifie
3969b 64 20 62 79 20 74 68 65 20 6f 70 20 70 61 72 61  d by the op para
3969c 6d 65 74 65 72 2e 0d 0a 2a 2a 20 52 65 74 75 72  meter...** Retur
3969d 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
3969e 68 65 20 74 65 72 6d 2e 20 20 52 65 74 75 72 6e  he term.  Return
3969f 20 30 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e   0 if not found.
396a0 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 57 68 65  ..*/..static Whe
396a1 72 65 54 65 72 6d 20 2a 66 69 6e 64 54 65 72 6d  reTerm *findTerm
396a2 28 0d 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  (..  WhereClause
396a3 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68   *pWC,     /* Th
396a4 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
396a5 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f  o be searched */
396a6 0d 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  ..  int iCur,   
396a7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
396a8 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48  sor number of LH
396a9 53 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c  S */..  int iCol
396aa 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  umn,          /*
396ab 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f   Column number o
396ac 66 20 4c 48 53 20 2a 2f 0d 0a 20 20 42 69 74 6d  f LHS */..  Bitm
396ad 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
396ae 20 20 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e 6f    /* RHS must no
396af 74 20 6f 76 65 72 6c 61 70 20 77 69 74 68 20 74  t overlap with t
396b0 68 69 73 20 6d 61 73 6b 20 2a 2f 0d 0a 20 20 75  his mask */..  u
396b1 33 32 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  32 op,          
396b2 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
396b3 57 4f 5f 78 78 20 76 61 6c 75 65 73 20 64 65 73  WO_xx values des
396b4 63 72 69 62 69 6e 67 20 6f 70 65 72 61 74 6f 72  cribing operator
396b5 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49   */..  Index *pI
396b6 64 78 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx           /* 
396b7 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62  Must be compatib
396b8 6c 65 20 77 69 74 68 20 74 68 69 73 20 69 6e 64  le with this ind
396b9 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20  ex, if not NULL 
396ba 2a 2f 0d 0a 29 7b 0d 0a 20 20 57 68 65 72 65 54  */..){..  WhereT
396bb 65 72 6d 20 2a 70 54 65 72 6d 3b 0d 0a 20 20 69  erm *pTerm;..  i
396bc 6e 74 20 6b 3b 0d 0a 20 20 61 73 73 65 72 74 28  nt k;..  assert(
396bd 20 69 43 75 72 3e 3d 30 20 29 3b 0d 0a 20 20 6f   iCur>=0 );..  o
396be 70 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0d 0a 20 20  p &= WO_ALL;..  
396bf 66 6f 72 28 3b 20 70 57 43 3b 20 70 57 43 3d 70  for(; pWC; pWC=p
396c0 57 43 2d 3e 70 4f 75 74 65 72 29 7b 0d 0a 20 20  WC->pOuter){..  
396c1 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d    for(pTerm=pWC-
396c2 3e 61 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d  >a, k=pWC->nTerm
396c3 3b 20 6b 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b  ; k; k--, pTerm+
396c4 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  +){..      if( p
396c5 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
396c6 3d 3d 69 43 75 72 0d 0a 20 20 20 20 20 20 20 20  ==iCur..        
396c7 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72   && (pTerm->prer
396c8 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61  eqRight & notRea
396c9 64 79 29 3d 3d 30 0d 0a 20 20 20 20 20 20 20 20  dy)==0..        
396ca 20 26 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66   && pTerm->u.lef
396cb 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e  tColumn==iColumn
396cc 0d 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70  ..         && (p
396cd 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
396ce 26 20 6f 70 29 21 3d 30 0d 0a 20 20 20 20 20 20  & op)!=0..      
396cf 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
396d0 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 49  iColumn>=0 && pI
396d1 64 78 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70  dx && pTerm->eOp
396d2 65 72 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c  erator!=WO_ISNUL
396d3 4c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  L ){..          
396d4 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d  Expr *pX = pTerm
396d5 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20  ->pExpr;..      
396d6 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
396d7 6c 6c 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 63  ll;..          c
396d8 68 61 72 20 69 64 78 61 66 66 3b 0d 0a 20 20 20  har idxaff;..   
396d9 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20         int j;.. 
396da 20 20 20 20 20 20 20 20 20 50 61 72 73 65 20 2a           Parse *
396db 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50  pParse = pWC->pP
396dc 61 72 73 65 3b 0d 0a 20 20 0d 0a 20 20 20 20 20  arse;..  ..     
396dd 20 20 20 20 20 69 64 78 61 66 66 20 3d 20 70 49       idxaff = pI
396de 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  dx->pTable->aCol
396df 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69  [iColumn].affini
396e0 74 79 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69  ty;..          i
396e1 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
396e2 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69  AffinityOk(pX, i
396e3 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75  dxaff) ) continu
396e4 65 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20 20 20  e;..  ..        
396e5 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
396e6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
396e7 71 75 65 6e 63 65 20 72 65 71 75 69 72 65 64 20  quence required 
396e8 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f  from an index fo
396e9 72 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  r..          ** 
396ea 69 74 20 74 6f 20 62 65 20 75 73 65 66 75 6c 20  it to be useful 
396eb 66 6f 72 20 6f 70 74 69 6d 69 73 69 6e 67 20 65  for optimising e
396ec 78 70 72 65 73 73 69 6f 6e 20 70 58 2e 20 53 74  xpression pX. St
396ed 6f 72 65 20 74 68 69 73 0d 0a 20 20 20 20 20 20  ore this..      
396ee 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20      ** value in 
396ef 76 61 72 69 61 62 6c 65 20 70 43 6f 6c 6c 2e 0d  variable pColl..
396f0 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  .          */.. 
396f1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
396f2 70 58 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20 20  pX->pLeft);..   
396f3 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
396f4 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
396f5 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  areCollSeq(pPars
396f6 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58  e, pX->pLeft, pX
396f7 2d 3e 70 52 69 67 68 74 29 3b 0d 0a 20 20 20 20  ->pRight);..    
396f8 20 20 20 20 20 20 61 73 73 65 72 74 28 70 43 6f        assert(pCo
396f9 6c 6c 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45  ll || pParse->nE
396fa 72 72 29 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20  rr);..  ..      
396fb 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 70 49 64      for(j=0; pId
396fc 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d  x->aiColumn[j]!=
396fd 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0d 0a  iColumn; j++){..
396fe 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
396ff 4e 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e 6e  NEVER(j>=pIdx->n
39700 43 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72 6e  Column) ) return
39701 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d   0;..          }
39702 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
39703 70 43 6f 6c 6c 20 26 26 20 73 71 6c 69 74 65 33  pColl && sqlite3
39704 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a  StrICmp(pColl->z
39705 4e 61 6d 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f  Name, pIdx->azCo
39706 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75  ll[j]) ) continu
39707 65 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  e;..        }.. 
39708 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 54         return pT
39709 65 72 6d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  erm;..      }.. 
3970a 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
3970b 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20  urn 0;..}..../* 
3970c 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
3970d 65 20 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  e */..static voi
3970e 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 53 72  d exprAnalyze(Sr
3970f 63 4c 69 73 74 2a 2c 20 57 68 65 72 65 43 6c 61  cList*, WhereCla
39710 75 73 65 2a 2c 20 69 6e 74 29 3b 0d 0a 0d 0a 2f  use*, int);..../
39711 2a 0d 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72 41  *..** Call exprA
39712 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74 65  nalyze on all te
39713 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20 63  rms in a WHERE c
39714 6c 61 75 73 65 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a  lause.  ..**..**
39715 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
39716 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c  d exprAnalyzeAll
39717 28 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  (..  SrcList *pT
39718 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  abList,       /*
39719 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
3971a 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61 75   */..  WhereClau
3971b 73 65 20 2a 70 57 43 20 20 20 20 20 20 20 20 20  se *pWC         
3971c 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  /* the WHERE cla
3971d 75 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  use to be analyz
3971e 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  ed */..){..  int
3971f 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d 70 57 43   i;..  for(i=pWC
39720 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b  ->nTerm-1; i>=0;
39721 20 69 2d 2d 29 7b 0d 0a 20 20 20 20 65 78 70 72   i--){..    expr
39722 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69 73 74  Analyze(pTabList
39723 2c 20 70 57 43 2c 20 69 29 3b 0d 0a 20 20 7d 0d  , pWC, i);..  }.
39724 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  .}....#ifndef SQ
39725 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f  LITE_OMIT_LIKE_O
39726 50 54 49 4d 49 5a 41 54 49 4f 4e 0d 0a 2f 2a 0d  PTIMIZATION../*.
39727 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
39728 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78   if the given ex
39729 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 4c 49  pression is a LI
3972a 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61  KE or GLOB opera
3972b 74 6f 72 20 74 68 61 74 0d 0a 2a 2a 20 63 61 6e  tor that..** can
3972c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75 73   be optimized us
3972d 69 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20 63  ing inequality c
3972e 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65 74  onstraints.  Ret
3972f 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69  urn TRUE if it i
39730 73 0d 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c  s..** so and fal
39731 73 65 20 69 66 20 6e 6f 74 2e 0d 0a 2a 2a 0d 0a  se if not...**..
39732 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20  ** In order for 
39733 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  the operator to 
39734 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20  be optimizible, 
39735 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65 20  the RHS must be 
39736 61 20 73 74 72 69 6e 67 0d 0a 2a 2a 20 6c 69 74  a string..** lit
39737 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e  eral that does n
39738 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
39739 77 69 6c 64 63 61 72 64 2e 20 20 0d 0a 2a 2f 0d  wildcard.  ..*/.
3973a 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69  .static int isLi
3973b 6b 65 4f 72 47 6c 6f 62 28 0d 0a 20 20 50 61 72  keOrGlob(..  Par
3973c 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
3973d 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
3973e 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
3973f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 45 78 70 72  ntext */..  Expr
39740 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 2f 2a   *pExpr,      /*
39741 20 54 65 73 74 20 74 68 69 73 20 65 78 70 72 65   Test this expre
39742 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 45 78 70 72  ssion */..  Expr
39743 20 2a 2a 70 70 50 72 65 66 69 78 2c 20 20 2f 2a   **ppPrefix,  /*
39744 20 50 6f 69 6e 74 65 72 20 74 6f 20 54 4b 5f 53   Pointer to TK_S
39745 54 52 49 4e 47 20 65 78 70 72 65 73 73 69 6f 6e  TRING expression
39746 20 77 69 74 68 20 70 61 74 74 65 72 6e 20 70 72   with pattern pr
39747 65 66 69 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  efix */..  int *
39748 70 69 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20  pisComplete, /* 
39749 54 72 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79  True if the only
3974a 20 77 69 6c 64 63 61 72 64 20 69 73 20 25 20 69   wildcard is % i
3974b 6e 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61  n the last chara
3974c 63 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  cter */..  int *
3974d 70 6e 6f 43 61 73 65 20 20 20 20 20 20 2f 2a 20  pnoCase      /* 
3974e 54 72 75 65 20 69 66 20 75 70 70 65 72 63 61 73  True if uppercas
3974f 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  e is equivalent 
39750 74 6f 20 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0d  to lowercase */.
39751 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  .){..  const cha
39752 72 20 2a 7a 20 3d 20 30 3b 20 20 20 20 20 20 20  r *z = 0;       
39753 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 6e 20 52    /* String on R
39754 48 53 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72 61  HS of LIKE opera
39755 74 6f 72 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  tor */..  Expr *
39756 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 20  pRight, *pLeft; 
39757 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 61 6e       /* Right an
39758 64 20 6c 65 66 74 20 73 69 7a 65 20 6f 66 20 4c  d left size of L
39759 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0d  IKE operator */.
3975a 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
3975b 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  st;           /*
3975c 20 4c 69 73 74 20 6f 66 20 6f 70 65 72 61 6e 64   List of operand
3975d 73 20 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f 70  s to the LIKE op
3975e 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74  erator */..  int
3975f 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   c;             
39760 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63          /* One c
39761 68 61 72 61 63 74 65 72 20 69 6e 20 7a 5b 5d 20  haracter in z[] 
39762 2a 2f 0d 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20  */..  int cnt;  
39763 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39764 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
39765 6e 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69  n-wildcard prefi
39766 78 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0d  x characters */.
39767 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b 20 20  .  char wc[3];  
39768 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39769 20 57 69 6c 64 63 61 72 64 20 63 68 61 72 61 63   Wildcard charac
3976a 74 65 72 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ters */..  sqlit
3976b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
3976c 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73  >db;  /* Databas
3976d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
3976e 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
3976f 20 2a 70 56 61 6c 20 3d 20 30 3b 0d 0a 20 20 69   *pVal = 0;..  i
39770 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
39771 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
39772 6f 64 65 20 6f 66 20 70 52 69 67 68 74 20 2a 2f  ode of pRight */
39773 0d 0a 0d 0a 20 20 69 66 28 20 21 73 71 6c 69 74  ....  if( !sqlit
39774 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
39775 28 64 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43  (db, pExpr, pnoC
39776 61 73 65 2c 20 77 63 29 20 29 7b 0d 0a 20 20 20  ase, wc) ){..   
39777 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d   return 0;..  }.
39778 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
39779 42 43 44 49 43 0d 0a 20 20 69 66 28 20 2a 70 6e  BCDIC..  if( *pn
3977a 6f 43 61 73 65 20 29 20 72 65 74 75 72 6e 20 30  oCase ) return 0
3977b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70 4c 69  ;..#endif..  pLi
3977c 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
3977d 69 73 74 3b 0d 0a 20 20 70 4c 65 66 74 20 3d 20  ist;..  pLeft = 
3977e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70  pList->a[1].pExp
3977f 72 3b 0d 0a 20 20 69 66 28 20 70 4c 65 66 74 2d  r;..  if( pLeft-
39780 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c  >op!=TK_COLUMN |
39781 7c 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  | sqlite3ExprAff
39782 69 6e 69 74 79 28 70 4c 65 66 74 29 21 3d 53 51  inity(pLeft)!=SQ
39783 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b  LITE_AFF_TEXT ){
39784 0d 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d  ..    /* IMP: R-
39785 30 32 30 36 35 2d 34 39 34 36 35 20 54 68 65 20  02065-49465 The 
39786 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
39787 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
39788 4f 42 20 6f 70 65 72 61 74 6f 72 20 6d 75 73 74  OB operator must
39789 0d 0a 20 20 20 20 2a 2a 20 62 65 20 74 68 65 20  ..    ** be the 
3978a 6e 61 6d 65 20 6f 66 20 61 6e 20 69 6e 64 65 78  name of an index
3978b 65 64 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 54  ed column with T
3978c 45 58 54 20 61 66 66 69 6e 69 74 79 2e 20 2a 2f  EXT affinity. */
3978d 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d  ..    return 0;.
3978e 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
3978f 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  pLeft->iColumn!=
39790 28 2d 31 29 20 29 3b 20 2f 2a 20 42 65 63 61 75  (-1) ); /* Becau
39791 73 65 20 49 50 4b 20 6e 65 76 65 72 20 68 61 73  se IPK never has
39792 20 41 46 46 5f 54 45 58 54 20 2a 2f 0d 0a 0d 0a   AFF_TEXT */....
39793 20 20 70 52 69 67 68 74 20 3d 20 70 4c 69 73 74    pRight = pList
39794 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0d 0a 20  ->a[0].pExpr;.. 
39795 20 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f 70   op = pRight->op
39796 3b 0d 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  ;..  if( op==TK_
39797 52 45 47 49 53 54 45 52 20 29 7b 0d 0a 20 20 20  REGISTER ){..   
39798 20 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f 70   op = pRight->op
39799 32 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 6f  2;..  }..  if( o
3979a 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29  p==TK_VARIABLE )
3979b 7b 0d 0a 20 20 20 20 56 64 62 65 20 2a 70 52 65  {..    Vdbe *pRe
3979c 70 72 65 70 61 72 65 20 3d 20 70 50 61 72 73 65  prepare = pParse
3979d 2d 3e 70 52 65 70 72 65 70 61 72 65 3b 0d 0a 20  ->pReprepare;.. 
3979e 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 52     int iCol = pR
3979f 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a  ight->iColumn;..
397a0 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74      pVal = sqlit
397a1 65 33 56 64 62 65 47 65 74 56 61 6c 75 65 28 70  e3VdbeGetValue(p
397a2 52 65 70 72 65 70 61 72 65 2c 20 69 43 6f 6c 2c  Reprepare, iCol,
397a3 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
397a4 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 56 61 6c  );..    if( pVal
397a5 20 26 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   && sqlite3_valu
397a6 65 5f 74 79 70 65 28 70 56 61 6c 29 3d 3d 53 51  e_type(pVal)==SQ
397a7 4c 49 54 45 5f 54 45 58 54 20 29 7b 0d 0a 20 20  LITE_TEXT ){..  
397a8 20 20 20 20 7a 20 3d 20 28 63 68 61 72 20 2a 29      z = (char *)
397a9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
397aa 78 74 28 70 56 61 6c 29 3b 0d 0a 20 20 20 20 7d  xt(pVal);..    }
397ab 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
397ac 65 53 65 74 56 61 72 6d 61 73 6b 28 70 50 61 72  eSetVarmask(pPar
397ad 73 65 2d 3e 70 56 64 62 65 2c 20 69 43 6f 6c 29  se->pVdbe, iCol)
397ae 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
397af 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 56 41  Right->op==TK_VA
397b0 52 49 41 42 4c 45 20 7c 7c 20 70 52 69 67 68 74  RIABLE || pRight
397b1 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ->op==TK_REGISTE
397b2 52 20 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  R );..  }else if
397b3 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20  ( op==TK_STRING 
397b4 29 7b 0d 0a 20 20 20 20 7a 20 3d 20 70 52 69 67  ){..    z = pRig
397b5 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0d 0a 20  ht->u.zToken;.. 
397b6 20 7d 0d 0a 20 20 69 66 28 20 7a 20 29 7b 0d 0a   }..  if( z ){..
397b7 20 20 20 20 63 6e 74 20 3d 20 30 3b 0d 0a 20 20      cnt = 0;..  
397b8 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e    while( (c=z[cn
397b9 74 5d 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b  t])!=0 && c!=wc[
397ba 30 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26  0] && c!=wc[1] &
397bb 26 20 63 21 3d 77 63 5b 32 5d 20 29 7b 0d 0a 20  & c!=wc[2] ){.. 
397bc 20 20 20 20 20 63 6e 74 2b 2b 3b 0d 0a 20 20 20       cnt++;..   
397bd 20 7d 0d 0a 20 20 20 20 69 66 28 20 63 6e 74 21   }..    if( cnt!
397be 3d 30 20 26 26 20 32 35 35 21 3d 28 75 38 29 7a  =0 && 255!=(u8)z
397bf 5b 63 6e 74 2d 31 5d 20 29 7b 0d 0a 20 20 20 20  [cnt-1] ){..    
397c0 20 20 45 78 70 72 20 2a 70 50 72 65 66 69 78 3b    Expr *pPrefix;
397c1 0d 0a 20 20 20 20 20 20 2a 70 69 73 43 6f 6d 70  ..      *pisComp
397c2 6c 65 74 65 20 3d 20 63 3d 3d 77 63 5b 30 5d 20  lete = c==wc[0] 
397c3 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b 0d  && z[cnt+1]==0;.
397c4 0a 20 20 20 20 20 20 70 50 72 65 66 69 78 20 3d  .      pPrefix =
397c5 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
397c6 20 54 4b 5f 53 54 52 49 4e 47 2c 20 7a 29 3b 0d   TK_STRING, z);.
397c7 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 66  .      if( pPref
397c8 69 78 20 29 20 70 50 72 65 66 69 78 2d 3e 75 2e  ix ) pPrefix->u.
397c9 7a 54 6f 6b 65 6e 5b 63 6e 74 5d 20 3d 20 30 3b  zToken[cnt] = 0;
397ca 0d 0a 20 20 20 20 20 20 2a 70 70 50 72 65 66 69  ..      *ppPrefi
397cb 78 20 3d 20 70 50 72 65 66 69 78 3b 0d 0a 20 20  x = pPrefix;..  
397cc 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 56      if( op==TK_V
397cd 41 52 49 41 42 4c 45 20 29 7b 0d 0a 20 20 20 20  ARIABLE ){..    
397ce 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50      Vdbe *v = pP
397cf 61 72 73 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20  arse->pVdbe;..  
397d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
397d1 65 53 65 74 56 61 72 6d 61 73 6b 28 76 2c 20 70  eSetVarmask(v, p
397d2 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b  Right->iColumn);
397d3 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 70  ..        if( *p
397d4 69 73 43 6f 6d 70 6c 65 74 65 20 26 26 20 70 52  isComplete && pR
397d5 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  ight->u.zToken[1
397d6 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ] ){..          
397d7 2f 2a 20 49 66 20 74 68 65 20 72 68 73 20 6f 66  /* If the rhs of
397d8 20 74 68 65 20 4c 49 4b 45 20 65 78 70 72 65 73   the LIKE expres
397d9 73 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62  sion is a variab
397da 6c 65 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72  le, and the curr
397db 65 6e 74 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  ent..          *
397dc 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 76  * value of the v
397dd 61 72 69 61 62 6c 65 20 6d 65 61 6e 73 20 74 68  ariable means th
397de 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
397df 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 4c 49 4b  o invoke the LIK
397e0 45 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  E..          ** 
397e1 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e  function, then n
397e2 6f 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 77 69  o OP_Variable wi
397e3 6c 6c 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  ll be added to t
397e4 68 65 20 70 72 6f 67 72 61 6d 2e 0d 0a 20 20 20  he program...   
397e5 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63         ** This c
397e6 61 75 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66  auses problems f
397e7 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  or the sqlite3_b
397e8 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
397e9 6d 65 28 29 0d 0a 20 20 20 20 20 20 20 20 20 20  me()..          
397ea 2a 2a 20 41 50 49 2e 20 54 6f 20 77 6f 72 6b 61  ** API. To worka
397eb 72 6f 75 6e 64 20 74 68 65 6d 2c 20 61 64 64 20  round them, add 
397ec 61 20 64 75 6d 6d 79 20 4f 50 5f 56 61 72 69 61  a dummy OP_Varia
397ed 62 6c 65 20 68 65 72 65 2e 0d 0a 20 20 20 20 20  ble here...     
397ee 20 20 20 20 20 2a 2f 20 0d 0a 20 20 20 20 20 20       */ ..      
397ef 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
397f0 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
397f1 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20 20 20  Parse);..       
397f2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
397f3 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
397f4 20 70 52 69 67 68 74 2c 20 72 31 29 3b 0d 0a 20   pRight, r1);.. 
397f5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
397f6 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
397f7 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
397f8 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 30 29 3b  ntAddr(v)-1, 0);
397f9 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
397fa 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
397fb 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0d 0a  g(pParse, r1);..
397fc 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
397fd 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a   }..    }else{..
397fe 20 20 20 20 20 20 7a 20 3d 20 30 3b 0d 0a 20 20        z = 0;..  
397ff 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71    }..  }....  sq
39800 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
39801 56 61 6c 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  Val);..  return 
39802 28 7a 21 3d 30 29 3b 0d 0a 7d 0d 0a 23 65 6e 64  (z!=0);..}..#end
39803 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
39804 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54  T_LIKE_OPTIMIZAT
39805 49 4f 4e 20 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 6e  ION */......#ifn
39806 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
39807 56 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 2f 2a  VIRTUALTABLE../*
39808 0d 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  ..** Check to se
39809 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65  e if the given e
3980a 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20  xpression is of 
3980b 74 68 65 20 66 6f 72 6d 0d 0a 2a 2a 0d 0a 2a 2a  the form..**..**
3980c 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
3980d 4d 41 54 43 48 20 65 78 70 72 0d 0a 2a 2a 0d 0a  MATCH expr..**..
3980e 2a 2a 20 49 66 20 69 74 20 69 73 20 74 68 65 6e  ** If it is then
3980f 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
39810 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41  f not, return FA
39811 4c 53 45 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  LSE...*/..static
39812 20 69 6e 74 20 69 73 4d 61 74 63 68 4f 66 43 6f   int isMatchOfCo
39813 6c 75 6d 6e 28 0d 0a 20 20 45 78 70 72 20 2a 70  lumn(..  Expr *p
39814 45 78 70 72 20 20 20 20 20 20 2f 2a 20 54 65 73  Expr      /* Tes
39815 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
39816 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 45 78 70 72  n */..){..  Expr
39817 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0d 0a 0d 0a  List *pList;....
39818 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
39819 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b 0d  =TK_FUNCTION ){.
3981a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  .    return 0;..
3981b 20 20 7d 0d 0a 20 20 69 66 28 20 73 71 6c 69 74    }..  if( sqlit
3981c 65 33 53 74 72 49 43 6d 70 28 70 45 78 70 72 2d  e3StrICmp(pExpr-
3981d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 61 74 63 68  >u.zToken,"match
3981e 22 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65  ")!=0 ){..    re
3981f 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 0;..  }..  
39820 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  pList = pExpr->x
39821 2e 70 4c 69 73 74 3b 0d 0a 20 20 69 66 28 20 70  .pList;..  if( p
39822 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29  List->nExpr!=2 )
39823 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {..    return 0;
39824 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 4c 69  ..  }..  if( pLi
39825 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e  st->a[1].pExpr->
39826 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20  op != TK_COLUMN 
39827 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30  ){..    return 0
39828 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
39829 20 31 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f   1;..}..#endif /
3982a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
3982b 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d  RTUALTABLE */...
3982c 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70  ./*..** If the p
3982d 42 61 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20  Base expression 
3982e 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68  originated in th
3982f 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
39830 61 75 73 65 20 6f 66 0d 0a 2a 2a 20 61 20 6a 6f  ause of..** a jo
39831 69 6e 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65  in, then transfe
39832 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  r the appropriat
39833 65 20 6d 61 72 6b 69 6e 67 73 20 6f 76 65 72 20  e markings over 
39834 74 6f 20 64 65 72 69 76 65 64 2e 0d 0a 2a 2f 0d  to derived...*/.
39835 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 61  .static void tra
39836 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67  nsferJoinMarking
39837 73 28 45 78 70 72 20 2a 70 44 65 72 69 76 65 64  s(Expr *pDerived
39838 2c 20 45 78 70 72 20 2a 70 42 61 73 65 29 7b 0d  , Expr *pBase){.
39839 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66 6c 61  .  pDerived->fla
3983a 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c 61  gs |= pBase->fla
3983b 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  gs & EP_FromJoin
3983c 3b 0d 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 69  ;..  pDerived->i
3983d 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
3983e 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f   pBase->iRightJo
3983f 69 6e 54 61 62 6c 65 3b 0d 0a 7d 0d 0a 0d 0a 23  inTable;..}....#
39840 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
39841 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
39842 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66  IZATION) && !def
39843 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
39844 5f 53 55 42 51 55 45 52 59 29 0d 0a 2f 2a 0d 0a  _SUBQUERY)../*..
39845 2a 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72  ** Analyze a ter
39846 6d 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20  m that consists 
39847 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 4f  of two or more O
39848 52 2d 63 6f 6e 6e 65 63 74 65 64 0d 0a 2a 2a 20  R-connected..** 
39849 73 75 62 74 65 72 6d 73 2e 20 20 53 6f 20 69 6e  subterms.  So in
3984a 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2e 2e  :..**..**     ..
3984b 2e 20 57 48 45 52 45 20 20 28 61 3d 35 29 20 41  . WHERE  (a=5) A
3984c 4e 44 20 28 62 3d 37 20 4f 52 20 63 3d 39 20 4f  ND (b=7 OR c=9 O
3984d 52 20 64 3d 31 33 29 20 41 4e 44 20 28 64 3d 31  R d=13) AND (d=1
3984e 33 29 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  3)..**          
3984f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39850 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e  ^^^^^^^^^^^^^^^^
39851 5e 5e 5e 5e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ^^^^..**..** Thi
39852 73 20 72 6f 75 74 69 6e 65 20 61 6e 61 6c 79 7a  s routine analyz
39853 65 73 20 74 65 72 6d 73 20 73 75 63 68 20 61 73  es terms such as
39854 20 74 68 65 20 6d 69 64 64 6c 65 20 74 65 72 6d   the middle term
39855 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 65 78   in the above ex
39856 61 6d 70 6c 65 2e 0d 0a 2a 2a 20 41 20 57 68 65  ample...** A Whe
39857 72 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 20  reOrTerm object 
39858 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20  is computed and 
39859 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
3985a 74 65 72 6d 20 75 6e 64 65 72 0d 0a 2a 2a 20 61  term under..** a
3985b 6e 61 6c 79 73 69 73 2c 20 72 65 67 61 72 64 6c  nalysis, regardl
3985c 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f  ess of the outco
3985d 6d 65 20 6f 66 20 74 68 65 20 61 6e 61 6c 79 73  me of the analys
3985e 69 73 2e 20 20 48 65 6e 63 65 3a 0d 0a 2a 2a 0d  is.  Hence:..**.
3985f 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72  .**     WhereTer
39860 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c 3d 20 20  m.wtFlags   |=  
39861 54 45 52 4d 5f 4f 52 49 4e 46 4f 0d 0a 2a 2a 20  TERM_ORINFO..** 
39862 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
39863 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20 64 79  pOrInfo  =  a dy
39864 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
39865 74 65 64 20 57 68 65 72 65 4f 72 54 65 72 6d 20  ted WhereOrTerm 
39866 6f 62 6a 65 63 74 0d 0a 2a 2a 0d 0a 2a 2a 20 54  object..**..** T
39867 68 65 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e  he term being an
39868 61 6c 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65  alyzed must have
39869 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20   two or more of 
3986a 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62  OR-connected sub
3986b 74 65 72 6d 73 2e 0d 0a 2a 2a 20 41 20 73 69 6e  terms...** A sin
3986c 67 6c 65 20 73 75 62 74 65 72 6d 20 6d 69 67 68  gle subterm migh
3986d 74 20 62 65 20 61 20 73 65 74 20 6f 66 20 41 4e  t be a set of AN
3986e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d  D-connected sub-
3986f 73 75 62 74 65 72 6d 73 2e 0d 0a 2a 2a 20 45 78  subterms...** Ex
39870 61 6d 70 6c 65 73 20 6f 66 20 74 65 72 6d 73 20  amples of terms 
39871 75 6e 64 65 72 20 61 6e 61 6c 79 73 69 73 3a 0d  under analysis:.
39872 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 28 41 29 20  .**..**     (A) 
39873 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52      t1.x=t2.y OR
39874 20 74 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74 31   t1.x=t2.z OR t1
39875 2e 79 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74 33  .y=15 OR t1.z=t3
39876 2e 61 2b 35 0d 0a 2a 2a 20 20 20 20 20 28 42 29  .a+5..**     (B)
39877 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20       x=expr1 OR 
39878 65 78 70 72 32 3d 78 20 4f 52 20 78 3d 65 78 70  expr2=x OR x=exp
39879 72 33 0d 0a 2a 2a 20 20 20 20 20 28 43 29 20 20  r3..**     (C)  
3987a 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20     t1.x=t2.y OR 
3987b 28 74 31 2e 78 3d 74 32 2e 7a 20 41 4e 44 20 74  (t1.x=t2.z AND t
3987c 31 2e 79 3d 31 35 29 0d 0a 2a 2a 20 20 20 20 20  1.y=15)..**     
3987d 28 44 29 20 20 20 20 20 78 3d 65 78 70 72 31 20  (D)     x=expr1 
3987e 4f 52 20 28 79 3e 31 31 20 41 4e 44 20 79 3c 32  OR (y>11 AND y<2
3987f 32 20 41 4e 44 20 7a 20 4c 49 4b 45 20 27 2a 68  2 AND z LIKE '*h
39880 65 6c 6c 6f 2a 27 29 0d 0a 2a 2a 20 20 20 20 20  ello*')..**     
39881 28 45 29 20 20 20 20 20 28 70 2e 61 3d 31 20 41  (E)     (p.a=1 A
39882 4e 44 20 71 2e 62 3d 32 20 41 4e 44 20 72 2e 63  ND q.b=2 AND r.c
39883 3d 33 29 20 4f 52 20 28 70 2e 78 3d 34 20 41 4e  =3) OR (p.x=4 AN
39884 44 20 71 2e 79 3d 35 20 41 4e 44 20 72 2e 7a 3d  D q.y=5 AND r.z=
39885 36 29 0d 0a 2a 2a 0d 0a 2a 2a 20 43 41 53 45 20  6)..**..** CASE 
39886 31 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6c  1:..**..** If al
39887 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65 20 6f  l subterms are o
39888 66 20 74 68 65 20 66 6f 72 6d 20 54 2e 43 3d 65  f the form T.C=e
39889 78 70 72 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e  xpr for some sin
3988a 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 43 0d  gle column of C.
3988b 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62  .** a single tab
3988c 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e 20 69  le T (as shown i
3988d 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f 76  n example B abov
3988e 65 29 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  e) then create a
3988f 20 6e 65 77 20 76 69 72 74 75 61 6c 0d 0a 2a 2a   new virtual..**
39890 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 61 6e   term that is an
39891 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20 65   equivalent IN e
39892 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f  xpression.  In o
39893 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74  ther words, if t
39894 68 65 20 74 65 72 6d 0d 0a 2a 2a 20 62 65 69 6e  he term..** bein
39895 67 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0d 0a  g analyzed is:..
39896 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20  **..**      x = 
39897 65 78 70 72 31 20 20 4f 52 20 20 65 78 70 72 32  expr1  OR  expr2
39898 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20 65 78   = x  OR  x = ex
39899 70 72 33 0d 0a 2a 2a 0d 0a 2a 2a 20 74 68 65 6e  pr3..**..** then
3989a 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
3989b 72 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b 65 20  rtual term like 
3989c 74 68 69 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  this:..**..**   
3989d 20 20 20 78 20 49 4e 20 28 65 78 70 72 31 2c 65     x IN (expr1,e
3989e 78 70 72 32 2c 65 78 70 72 33 29 0d 0a 2a 2a 0d  xpr2,expr3)..**.
3989f 0a 2a 2a 20 43 41 53 45 20 32 3a 0d 0a 2a 2a 0d  .** CASE 2:..**.
398a0 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62 74 65  .** If all subte
398a1 72 6d 73 20 61 72 65 20 69 6e 64 65 78 61 62 6c  rms are indexabl
398a2 65 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 61  e by a single ta
398a3 62 6c 65 20 54 2c 20 74 68 65 6e 20 73 65 74 0d  ble T, then set.
398a4 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 57 68 65 72  .**..**     Wher
398a5 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20  eTerm.eOperator 
398a6 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 20 20               =  
398a7 57 4f 5f 4f 52 0d 0a 2a 2a 20 20 20 20 20 57 68  WO_OR..**     Wh
398a8 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66  ereTerm.u.pOrInf
398a9 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20 7c 3d  o->indexable  |=
398aa 20 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d    the cursor num
398ab 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20 54 0d  ber for table T.
398ac 0a 2a 2a 0d 0a 2a 2a 20 41 20 73 75 62 74 65 72  .**..** A subter
398ad 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65 22  m is "indexable"
398ae 20 69 66 20 69 74 20 69 73 20 6f 66 20 74 68 65   if it is of the
398af 20 66 6f 72 6d 0d 0a 2a 2a 20 22 54 2e 43 20 3c   form..** "T.C <
398b0 6f 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72  op> <expr>" wher
398b1 65 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d  e C is any colum
398b2 6e 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64  n of table T and
398b3 20 0d 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e   ..** <op> is on
398b4 65 20 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22  e of "=", "<", "
398b5 3c 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20  <=", ">", ">=", 
398b6 22 49 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49  "IS NULL", or "I
398b7 4e 22 2e 0d 0a 2a 2a 20 41 20 73 75 62 74 65 72  N"...** A subter
398b8 6d 20 69 73 20 61 6c 73 6f 20 69 6e 64 65 78 61  m is also indexa
398b9 62 6c 65 20 69 66 20 69 74 20 69 73 20 61 6e 20  ble if it is an 
398ba 41 4e 44 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f  AND of two or mo
398bb 72 65 0d 0a 2a 2a 20 73 75 62 73 75 62 74 65 72  re..** subsubter
398bc 6d 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ms at least one 
398bd 6f 66 20 77 68 69 63 68 20 69 73 20 69 6e 64 65  of which is inde
398be 78 61 62 6c 65 2e 20 20 49 6e 64 65 78 61 62 6c  xable.  Indexabl
398bf 65 20 41 4e 44 20 0d 0a 2a 2a 20 73 75 62 74 65  e AND ..** subte
398c0 72 6d 73 20 68 61 76 65 20 74 68 65 69 72 20 65  rms have their e
398c1 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f 20  Operator set to 
398c2 57 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65 79 20  WO_AND and they 
398c3 68 61 76 65 0d 0a 2a 2a 20 75 2e 70 41 6e 64 49  have..** u.pAndI
398c4 6e 66 6f 20 73 65 74 20 74 6f 20 61 20 64 79 6e  nfo set to a dyn
398c5 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
398c6 65 64 20 57 68 65 72 65 41 6e 64 54 65 72 6d 20  ed WhereAndTerm 
398c7 6f 62 6a 65 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  object...**..** 
398c8 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20 70 6f 69  From another poi
398c9 6e 74 20 6f 66 20 76 69 65 77 2c 20 22 69 6e 64  nt of view, "ind
398ca 65 78 61 62 6c 65 22 20 6d 65 61 6e 73 20 74 68  exable" means th
398cb 61 74 20 74 68 65 20 73 75 62 74 65 72 6d 20 63  at the subterm c
398cc 6f 75 6c 64 0d 0a 2a 2a 20 70 6f 74 65 6e 74 69  ould..** potenti
398cd 61 6c 6c 79 20 62 65 20 75 73 65 64 20 77 69 74  ally be used wit
398ce 68 20 61 6e 20 69 6e 64 65 78 20 69 66 20 61 6e  h an index if an
398cf 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 64   appropriate ind
398d0 65 78 20 65 78 69 73 74 73 2e 0d 0a 2a 2a 20 54  ex exists...** T
398d1 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 6f 65  his analysis doe
398d2 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 20 77  s not consider w
398d3 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
398d4 65 20 69 6e 64 65 78 20 65 78 69 73 74 73 3b 20  e index exists; 
398d5 74 68 61 74 0d 0a 2a 2a 20 69 73 20 73 6f 6d 65  that..** is some
398d6 74 68 69 6e 67 20 74 68 65 20 62 65 73 74 49 6e  thing the bestIn
398d7 64 65 78 28 29 20 72 6f 75 74 69 6e 65 20 77 69  dex() routine wi
398d8 6c 6c 20 64 65 74 65 72 6d 69 6e 65 2e 20 20 54  ll determine.  T
398d9 68 69 73 20 61 6e 61 6c 79 73 69 73 0d 0a 2a 2a  his analysis..**
398da 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20 61 74 20 77   only looks at w
398db 68 65 74 68 65 72 20 73 75 62 74 65 72 6d 73 20  hether subterms 
398dc 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
398dd 69 6e 64 65 78 69 6e 67 20 65 78 69 73 74 2e 0d  indexing exist..
398de 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 65 78 61 6d  .**..** All exam
398df 70 6c 65 73 20 41 20 74 68 72 6f 75 67 68 20 45  ples A through E
398e0 20 61 62 6f 76 65 20 61 6c 6c 20 73 61 74 69 73   above all satis
398e1 66 79 20 63 61 73 65 20 32 2e 20 20 42 75 74 20  fy case 2.  But 
398e2 69 66 20 61 20 74 65 72 6d 0d 0a 2a 2a 20 61 6c  if a term..** al
398e3 73 6f 20 73 74 61 74 69 73 66 69 65 73 20 63 61  so statisfies ca
398e4 73 65 20 31 20 28 73 75 63 68 20 61 73 20 42 29  se 1 (such as B)
398e5 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68   we know that th
398e6 65 20 6f 70 74 69 6d 69 7a 65 72 20 77 69 6c 6c  e optimizer will
398e7 0d 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72 65 66  ..** always pref
398e8 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20 69 6e  er case 1, so in
398e9 20 74 68 61 74 20 63 61 73 65 20 77 65 20 70 72   that case we pr
398ea 65 74 65 6e 64 20 74 68 61 74 20 63 61 73 65 20  etend that case 
398eb 32 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 73 61 74  2 is not..** sat
398ec 69 73 66 69 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  isfied...**..** 
398ed 49 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20  It might be the 
398ee 63 61 73 65 20 74 68 61 74 20 6d 75 6c 74 69 70  case that multip
398ef 6c 65 20 74 61 62 6c 65 73 20 61 72 65 20 69 6e  le tables are in
398f0 64 65 78 61 62 6c 65 2e 20 20 46 6f 72 20 65 78  dexable.  For ex
398f1 61 6d 70 6c 65 2c 0d 0a 2a 2a 20 28 45 29 20 61  ample,..** (E) a
398f2 62 6f 76 65 20 69 73 20 69 6e 64 65 78 61 62 6c  bove is indexabl
398f3 65 20 6f 6e 20 74 61 62 6c 65 73 20 50 2c 20 51  e on tables P, Q
398f4 2c 20 61 6e 64 20 52 2e 0d 0a 2a 2a 0d 0a 2a 2a  , and R...**..**
398f5 20 54 65 72 6d 73 20 74 68 61 74 20 73 61 74 69   Terms that sati
398f6 73 66 79 20 63 61 73 65 20 32 20 61 72 65 20 63  sfy case 2 are c
398f7 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6c 6f  andidates for lo
398f8 6f 6b 75 70 20 62 79 20 75 73 69 6e 67 0d 0a 2a  okup by using..*
398f9 2a 20 73 65 70 61 72 61 74 65 20 69 6e 64 69 63  * separate indic
398fa 65 73 20 74 6f 20 66 69 6e 64 20 72 6f 77 69 64  es to find rowid
398fb 73 20 66 6f 72 20 65 61 63 68 20 73 75 62 74 65  s for each subte
398fc 72 6d 20 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67  rm and composing
398fd 0d 0a 2a 2a 20 74 68 65 20 75 6e 69 6f 6e 20 6f  ..** the union o
398fe 66 20 61 6c 6c 20 72 6f 77 69 64 73 20 75 73 69  f all rowids usi
398ff 6e 67 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65  ng a RowSet obje
39900 63 74 2e 20 20 54 68 69 73 20 69 73 20 73 69 6d  ct.  This is sim
39901 69 6c 61 72 0d 0a 2a 2a 20 74 6f 20 22 62 69 74  ilar..** to "bit
39902 6d 61 70 20 69 6e 64 69 63 65 73 22 20 69 6e 20  map indices" in 
39903 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 65  other database e
39904 6e 67 69 6e 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ngines...**..** 
39905 4f 54 48 45 52 57 49 53 45 3a 0d 0a 2a 2a 0d 0a  OTHERWISE:..**..
39906 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 63 61  ** If neither ca
39907 73 65 20 31 20 6e 6f 72 20 63 61 73 65 20 32 20  se 1 nor case 2 
39908 61 70 70 6c 79 2c 20 74 68 65 6e 20 6c 65 61 76  apply, then leav
39909 65 20 74 68 65 20 65 4f 70 65 72 61 74 6f 72 20  e the eOperator 
3990a 73 65 74 20 74 6f 0d 0a 2a 2a 20 7a 65 72 6f 2e  set to..** zero.
3990b 20 20 54 68 69 73 20 74 65 72 6d 20 69 73 20 6e    This term is n
3990c 6f 74 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65  ot useful for se
3990d 61 72 63 68 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  arch...*/..stati
3990e 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
3990f 7a 65 4f 72 54 65 72 6d 28 0d 0a 20 20 53 72 63  zeOrTerm(..  Src
39910 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
39911 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52         /* the FR
39912 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20  OM clause */..  
39913 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
39914 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  ,         /* the
39915 20 63 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20   complete WHERE 
39916 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69 6e 74  clause */..  int
39917 20 69 64 78 54 65 72 6d 20 20 20 20 20 20 20 20   idxTerm        
39918 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
39919 6f 66 20 74 68 65 20 4f 52 2d 74 65 72 6d 20 74  of the OR-term t
3991a 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
3991b 0d 0a 29 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70  ..){..  Parse *p
3991c 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61  Parse = pWC->pPa
3991d 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
3991e 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
3991f 74 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20  t */..  sqlite3 
39920 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
39921 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39922 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
39923 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 57 68 65  ection */..  Whe
39924 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20  reTerm *pTerm = 
39925 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
39926 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d  ;    /* The term
39927 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
39928 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70 45 78 70  */..  Expr *pExp
39929 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  r = pTerm->pExpr
3992a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
3992b 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
3992c 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0d 0a  of the term */..
3992d 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
3992e 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e  pMaskSet = pWC->
3992f 70 4d 61 73 6b 53 65 74 3b 20 2f 2a 20 54 61 62  pMaskSet; /* Tab
39930 6c 65 20 75 73 65 20 6d 61 73 6b 73 20 2a 2f 0d  le use masks */.
39931 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
39932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39933 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
39934 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0d 0a  op counters */..
39935 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
39936 4f 72 57 63 3b 20 20 20 20 20 20 20 2f 2a 20 42  OrWc;       /* B
39937 72 65 61 6b 75 70 20 6f 66 20 70 54 65 72 6d 20  reakup of pTerm 
39938 69 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f  into subterms */
39939 0d 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ..  WhereTerm *p
3993a 4f 72 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a  OrTerm;       /*
3993b 20 41 20 53 75 62 2d 74 65 72 6d 20 77 69 74 68   A Sub-term with
3993c 69 6e 20 74 68 65 20 70 4f 72 57 63 20 2a 2f 0d  in the pOrWc */.
3993d 0a 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a  .  WhereOrInfo *
3993e 70 4f 72 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  pOrInfo;     /* 
3993f 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  Additional infor
39940 6d 61 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65  mation associate
39941 64 20 77 69 74 68 20 70 54 65 72 6d 20 2a 2f 0d  d with pTerm */.
39942 0a 20 20 42 69 74 6d 61 73 6b 20 63 68 6e 67 54  .  Bitmask chngT
39943 6f 49 4e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  oIN;         /* 
39944 54 61 62 6c 65 73 20 74 68 61 74 20 6d 69 67 68  Tables that migh
39945 74 20 73 61 74 69 73 66 79 20 63 61 73 65 20 31  t satisfy case 1
39946 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b 20 69   */..  Bitmask i
39947 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20 20  ndexable;       
39948 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20   /* Tables that 
39949 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2c 20 73  are indexable, s
3994a 61 74 69 73 66 79 69 6e 67 20 63 61 73 65 20 32  atisfying case 2
3994b 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a   */....  /*..  *
3994c 2a 20 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63  * Break the OR c
3994d 6c 61 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73  lause into its s
3994e 65 70 61 72 61 74 65 20 73 75 62 74 65 72 6d 73  eparate subterms
3994f 2e 20 20 54 68 65 20 73 75 62 74 65 72 6d 73 20  .  The subterms 
39950 61 72 65 0d 0a 20 20 2a 2a 20 73 74 6f 72 65 64  are..  ** stored
39951 20 69 6e 20 61 20 57 68 65 72 65 43 6c 61 75 73   in a WhereClaus
39952 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
39953 61 69 6e 69 6e 67 20 77 69 74 68 69 6e 20 74 68  aining within th
39954 65 20 57 68 65 72 65 4f 72 49 6e 66 6f 0d 0a 20  e WhereOrInfo.. 
39955 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20   ** object that 
39956 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
39957 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f 52 20 63  he original OR c
39958 6c 61 75 73 65 20 74 65 72 6d 2e 0d 0a 20 20 2a  lause term...  *
39959 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 54  /..  assert( (pT
3995a 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
3995b 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45 52  TERM_DYNAMIC|TER
3995c 4d 5f 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41 4e  M_ORINFO|TERM_AN
3995d 44 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0d 0a 20  DINFO))==0 );.. 
3995e 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
3995f 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0d 0a 20 20  op==TK_OR );..  
39960 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f  pTerm->u.pOrInfo
39961 20 3d 20 70 4f 72 49 6e 66 6f 20 3d 20 73 71 6c   = pOrInfo = sql
39962 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
39963 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 72  (db, sizeof(*pOr
39964 49 6e 66 6f 29 29 3b 0d 0a 20 20 69 66 28 20 70  Info));..  if( p
39965 4f 72 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75  OrInfo==0 ) retu
39966 72 6e 3b 0d 0a 20 20 70 54 65 72 6d 2d 3e 77 74  rn;..  pTerm->wt
39967 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52  Flags |= TERM_OR
39968 49 4e 46 4f 3b 0d 0a 20 20 70 4f 72 57 63 20 3d  INFO;..  pOrWc =
39969 20 26 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0d 0a   &pOrInfo->wc;..
3996a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69    whereClauseIni
3996b 74 28 70 4f 72 57 63 2c 20 70 57 43 2d 3e 70 50  t(pOrWc, pWC->pP
3996c 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 2c 20  arse, pMaskSet, 
3996d 70 57 43 2d 3e 77 63 74 72 6c 46 6c 61 67 73 29  pWC->wctrlFlags)
3996e 3b 0d 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28  ;..  whereSplit(
3996f 70 4f 72 57 63 2c 20 70 45 78 70 72 2c 20 54 4b  pOrWc, pExpr, TK
39970 5f 4f 52 29 3b 0d 0a 20 20 65 78 70 72 41 6e 61  _OR);..  exprAna
39971 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 4f  lyzeAll(pSrc, pO
39972 72 57 63 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d  rWc);..  if( db-
39973 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
39974 72 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73 65 72  return;..  asser
39975 74 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e  t( pOrWc->nTerm>
39976 3d 32 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20  =2 );....  /*.. 
39977 20 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   ** Compute the 
39978 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68  set of tables th
39979 61 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79  at might satisfy
3997a 20 63 61 73 65 73 20 31 20 6f 72 20 32 2e 0d 0a   cases 1 or 2...
3997b 20 20 2a 2f 0d 0a 20 20 69 6e 64 65 78 61 62 6c    */..  indexabl
3997c 65 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b  e = ~(Bitmask)0;
3997d 0d 0a 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 7e  ..  chngToIN = ~
3997e 28 70 57 43 2d 3e 76 6d 61 73 6b 29 3b 0d 0a 20  (pWC->vmask);.. 
3997f 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54   for(i=pOrWc->nT
39980 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70  erm-1, pOrTerm=p
39981 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 20 26 26  OrWc->a; i>=0 &&
39982 20 69 6e 64 65 78 61 62 6c 65 3b 20 69 2d 2d 2c   indexable; i--,
39983 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20   pOrTerm++){..  
39984 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e    if( (pOrTerm->
39985 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 53  eOperator & WO_S
39986 49 4e 47 4c 45 29 3d 3d 30 20 29 7b 0d 0a 20 20  INGLE)==0 ){..  
39987 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f      WhereAndInfo
39988 20 2a 70 41 6e 64 49 6e 66 6f 3b 0d 0a 20 20 20   *pAndInfo;..   
39989 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65     assert( pOrTe
3998a 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 30  rm->eOperator==0
3998b 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
3998c 74 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46  t( (pOrTerm->wtF
3998d 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 41 4e 44  lags & (TERM_AND
3998e 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f  INFO|TERM_ORINFO
3998f 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ))==0 );..      
39990 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0d 0a 20  chngToIN = 0;.. 
39991 20 20 20 20 20 70 41 6e 64 49 6e 66 6f 20 3d 20       pAndInfo = 
39992 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
39993 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  aw(db, sizeof(*p
39994 41 6e 64 49 6e 66 6f 29 29 3b 0d 0a 20 20 20 20  AndInfo));..    
39995 20 20 69 66 28 20 70 41 6e 64 49 6e 66 6f 20 29    if( pAndInfo )
39996 7b 0d 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  {..        Where
39997 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 3b 0d  Clause *pAndWC;.
39998 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
39999 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b 0d 0a 20  rm *pAndTerm;.. 
3999a 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20         int j;.. 
3999b 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62         Bitmask b
3999c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 70   = 0;..        p
3999d 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
3999e 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0d 0a  fo = pAndInfo;..
3999f 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
399a0 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
399a1 5f 41 4e 44 49 4e 46 4f 3b 0d 0a 20 20 20 20 20  _ANDINFO;..     
399a2 20 20 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65     pOrTerm->eOpe
399a3 72 61 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0d  rator = WO_AND;.
399a4 0a 20 20 20 20 20 20 20 20 70 41 6e 64 57 43 20  .        pAndWC 
399a5 3d 20 26 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b  = &pAndInfo->wc;
399a6 0d 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 43  ..        whereC
399a7 6c 61 75 73 65 49 6e 69 74 28 70 41 6e 64 57 43  lauseInit(pAndWC
399a8 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  , pWC->pParse, p
399a9 4d 61 73 6b 53 65 74 2c 20 70 57 43 2d 3e 77 63  MaskSet, pWC->wc
399aa 74 72 6c 46 6c 61 67 73 29 3b 0d 0a 20 20 20 20  trlFlags);..    
399ab 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
399ac 41 6e 64 57 43 2c 20 70 4f 72 54 65 72 6d 2d 3e  AndWC, pOrTerm->
399ad 70 45 78 70 72 2c 20 54 4b 5f 41 4e 44 29 3b 0d  pExpr, TK_AND);.
399ae 0a 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61  .        exprAna
399af 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 41  lyzeAll(pSrc, pA
399b0 6e 64 57 43 29 3b 0d 0a 20 20 20 20 20 20 20 20  ndWC);..        
399b1 70 41 6e 64 57 43 2d 3e 70 4f 75 74 65 72 20 3d  pAndWC->pOuter =
399b2 20 70 57 43 3b 0d 0a 20 20 20 20 20 20 20 20 74   pWC;..        t
399b3 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c  estcase( db->mal
399b4 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20  locFailed );..  
399b5 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
399b6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a  allocFailed ){..
399b7 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
399b8 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70 41 6e 64  0, pAndTerm=pAnd
399b9 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64 57 43 2d  WC->a; j<pAndWC-
399ba 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 41 6e  >nTerm; j++, pAn
399bb 64 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20  dTerm++){..     
399bc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
399bd 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 20 29  AndTerm->pExpr )
399be 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ;..            i
399bf 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41 6e  f( allowedOp(pAn
399c0 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f 70  dTerm->pExpr->op
399c1 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) ){..          
399c2 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b      b |= getMask
399c3 28 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 54  (pMaskSet, pAndT
399c4 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  erm->leftCursor)
399c5 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
399c6 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
399c7 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
399c8 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62    indexable &= b
399c9 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
399ca 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72  }else if( pOrTer
399cb 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
399cc 4d 5f 43 4f 50 49 45 44 20 29 7b 0d 0a 20 20 20  M_COPIED ){..   
399cd 20 20 20 2f 2a 20 53 6b 69 70 20 74 68 69 73 20     /* Skip this 
399ce 74 65 72 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57  term for now.  W
399cf 65 20 72 65 76 69 73 69 74 20 69 74 20 77 68 65  e revisit it whe
399d0 6e 20 77 65 20 70 72 6f 63 65 73 73 20 74 68 65  n we process the
399d1 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65  ..      ** corre
399d2 73 70 6f 6e 64 69 6e 67 20 54 45 52 4d 5f 56 49  sponding TERM_VI
399d3 52 54 55 41 4c 20 74 65 72 6d 20 2a 2f 0d 0a 20  RTUAL term */.. 
399d4 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
399d5 20 42 69 74 6d 61 73 6b 20 62 3b 0d 0a 20 20 20   Bitmask b;..   
399d6 20 20 20 62 20 3d 20 67 65 74 4d 61 73 6b 28 70     b = getMask(p
399d7 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d  MaskSet, pOrTerm
399d8 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0d 0a  ->leftCursor);..
399d9 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
399da 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
399db 4d 5f 56 49 52 54 55 41 4c 20 29 7b 0d 0a 20 20  M_VIRTUAL ){..  
399dc 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
399dd 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f 72 57 63  *pOther = &pOrWc
399de 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e 69 50 61  ->a[pOrTerm->iPa
399df 72 65 6e 74 5d 3b 0d 0a 20 20 20 20 20 20 20 20  rent];..        
399e0 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61  b |= getMask(pMa
399e1 73 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d 3e 6c  skSet, pOther->l
399e2 65 66 74 43 75 72 73 6f 72 29 3b 0d 0a 20 20 20  eftCursor);..   
399e3 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 6e 64 65     }..      inde
399e4 78 61 62 6c 65 20 26 3d 20 62 3b 0d 0a 20 20 20  xable &= b;..   
399e5 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e     if( pOrTerm->
399e6 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51  eOperator!=WO_EQ
399e7 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 6e   ){..        chn
399e8 67 54 6f 49 4e 20 3d 20 30 3b 0d 0a 20 20 20 20  gToIN = 0;..    
399e9 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
399ea 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d 20 62 3b    chngToIN &= b;
399eb 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
399ec 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20  ..  }....  /*.. 
399ed 20 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 73   ** Record the s
399ee 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61  et of tables tha
399ef 74 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32  t satisfy case 2
399f0 2e 20 20 54 68 65 20 73 65 74 20 6d 69 67 68 74  .  The set might
399f1 20 62 65 0d 0a 20 20 2a 2a 20 65 6d 70 74 79 2e   be..  ** empty.
399f2 0d 0a 20 20 2a 2f 0d 0a 20 20 70 4f 72 49 6e 66  ..  */..  pOrInf
399f3 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 3d 20 69  o->indexable = i
399f4 6e 64 65 78 61 62 6c 65 3b 0d 0a 20 20 70 54 65  ndexable;..  pTe
399f5 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20  rm->eOperator = 
399f6 69 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f 20 30  indexable==0 ? 0
399f7 20 3a 20 57 4f 5f 4f 52 3b 0d 0a 0d 0a 20 20 2f   : WO_OR;....  /
399f8 2a 0d 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e  *..  ** chngToIN
399f9 20 68 6f 6c 64 73 20 61 20 73 65 74 20 6f 66 20   holds a set of 
399fa 74 61 62 6c 65 73 20 74 68 61 74 20 2a 6d 69 67  tables that *mig
399fb 68 74 2a 20 73 61 74 69 73 66 79 20 63 61 73 65  ht* satisfy case
399fc 20 31 2e 20 20 42 75 74 0d 0a 20 20 2a 2a 20 77   1.  But..  ** w
399fd 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d  e have to do som
399fe 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 68 65  e additional che
399ff 63 6b 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20  cking to see if 
39a00 63 61 73 65 20 31 20 72 65 61 6c 6c 79 0d 0a 20  case 1 really.. 
39a01 20 2a 2a 20 69 73 20 73 61 74 69 73 66 69 65 64   ** is satisfied
39a02 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 63 68  ...  **..  ** ch
39a03 6e 67 54 6f 49 4e 20 77 69 6c 6c 20 68 6f 6c 64  ngToIN will hold
39a04 20 65 69 74 68 65 72 20 30 2c 20 31 2c 20 6f 72   either 0, 1, or
39a05 20 32 20 62 69 74 73 2e 20 20 54 68 65 20 30 2d   2 bits.  The 0-
39a06 62 69 74 20 63 61 73 65 20 6d 65 61 6e 73 0d 0a  bit case means..
39a07 20 20 2a 2a 20 74 68 61 74 20 74 68 65 72 65 20    ** that there 
39a08 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69 74  is no possibilit
39a09 79 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e  y of transformin
39a0a 67 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20  g the OR clause 
39a0b 69 6e 74 6f 20 61 6e 0d 0a 20 20 2a 2a 20 49 4e  into an..  ** IN
39a0c 20 6f 70 65 72 61 74 6f 72 20 62 65 63 61 75 73   operator becaus
39a0d 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 65  e one or more te
39a0e 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 20 63 6c  rms in the OR cl
39a0f 61 75 73 65 20 63 6f 6e 74 61 69 6e 0d 0a 20 20  ause contain..  
39a10 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68  ** something oth
39a11 65 72 20 74 68 61 6e 20 3d 3d 20 6f 6e 20 61 20  er than == on a 
39a12 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 73 69  column in the si
39a13 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20 54 68 65  ngle table.  The
39a14 20 31 2d 62 69 74 0d 0a 20 20 2a 2a 20 63 61 73   1-bit..  ** cas
39a15 65 20 6d 65 61 6e 73 20 74 68 61 74 20 65 76 65  e means that eve
39a16 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f  ry term of the O
39a17 52 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74  R clause is of t
39a18 68 65 20 66 6f 72 6d 0d 0a 20 20 2a 2a 20 22 74  he form..  ** "t
39a19 61 62 6c 65 2e 63 6f 6c 75 6d 6e 3d 65 78 70 72  able.column=expr
39a1a 22 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c  " for some singl
39a1b 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6f 6e  e table.  The on
39a1c 65 20 62 69 74 20 74 68 61 74 20 69 73 20 73 65  e bit that is se
39a1d 74 0d 0a 20 20 2a 2a 20 77 69 6c 6c 20 63 6f 72  t..  ** will cor
39a1e 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 63  respond to the c
39a1f 6f 6d 6d 6f 6e 20 74 61 62 6c 65 2e 20 20 57 65  ommon table.  We
39a20 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63   still need to c
39a21 68 65 63 6b 20 74 6f 20 6d 61 6b 65 0d 0a 20 20  heck to make..  
39a22 2a 2a 20 73 75 72 65 20 74 68 65 20 73 61 6d 65  ** sure the same
39a23 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20   column is used 
39a24 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 2e 20 20 54  on all terms.  T
39a25 68 65 20 32 2d 62 69 74 20 63 61 73 65 20 69 73  he 2-bit case is
39a26 20 77 68 65 6e 0d 0a 20 20 2a 2a 20 74 68 65 20   when..  ** the 
39a27 61 6c 6c 20 74 65 72 6d 73 20 61 72 65 20 6f 66  all terms are of
39a28 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65   the form "table
39a29 31 2e 63 6f 6c 75 6d 6e 3d 74 61 62 6c 65 32 2e  1.column=table2.
39a2a 63 6f 6c 75 6d 6e 22 2e 20 20 49 74 0d 0a 20 20  column".  It..  
39a2b 2a 2a 20 6d 69 67 68 74 20 62 65 20 70 6f 73 73  ** might be poss
39a2c 69 62 6c 65 20 74 6f 20 66 6f 72 6d 20 61 6e 20  ible to form an 
39a2d 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  IN operator with
39a2e 20 65 69 74 68 65 72 20 74 61 62 6c 65 31 2e 63   either table1.c
39a2f 6f 6c 75 6d 6e 0d 0a 20 20 2a 2a 20 6f 72 20 74  olumn..  ** or t
39a30 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 20 61 73 20  able2.column as 
39a31 74 68 65 20 4c 48 53 20 69 66 20 65 69 74 68 65  the LHS if eithe
39a32 72 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65  r is common to e
39a33 76 65 72 79 20 74 65 72 6d 20 6f 66 0d 0a 20 20  very term of..  
39a34 2a 2a 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  ** the OR clause
39a35 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f  ...  **..  ** No
39a36 74 65 20 74 68 61 74 20 74 65 72 6d 73 20 6f 66  te that terms of
39a37 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65   the form "table
39a38 2e 63 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65 2e 63  .column1=table.c
39a39 6f 6c 75 6d 6e 32 22 20 28 74 68 65 0d 0a 20 20  olumn2" (the..  
39a3a 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 6f 6e  ** same table on
39a3b 20 62 6f 74 68 20 73 69 7a 65 73 20 6f 66 20 74   both sizes of t
39a3c 68 65 20 3d 3d 29 20 63 61 6e 6e 6f 74 20 62 65  he ==) cannot be
39a3d 20 6f 70 74 69 6d 69 7a 65 64 2e 0d 0a 20 20 2a   optimized...  *
39a3e 2f 0d 0a 20 20 69 66 28 20 63 68 6e 67 54 6f 49  /..  if( chngToI
39a3f 4e 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6f 6b  N ){..    int ok
39a40 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 20  ToChngToIN = 0; 
39a41 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
39a42 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f  he conversion to
39a43 20 49 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0d   IN is valid */.
39a44 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  .    int iColumn
39a45 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f   = -1;         /
39a46 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f  * Column index o
39a47 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f 70 65 72  n lhs of IN oper
39a48 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  ator */..    int
39a49 20 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 20 20   iCursor = -1;  
39a4a 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
39a4b 63 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f  cursor common to
39a4c 20 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0d 0a 20   all terms */.. 
39a4d 20 20 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20 20     int j = 0;   
39a4e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39a4f 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d  Loop counter */.
39a50 0a 0d 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68  ...    /* Search
39a51 20 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64   for a table and
39a52 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70   column that app
39a53 65 61 72 73 20 6f 6e 20 6f 6e 65 20 73 69 64 65  ears on one side
39a54 20 6f 72 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20   or the..    ** 
39a55 6f 74 68 65 72 20 6f 66 20 74 68 65 20 3d 3d 20  other of the == 
39a56 6f 70 65 72 61 74 6f 72 20 69 6e 20 65 76 65 72  operator in ever
39a57 79 20 73 75 62 74 65 72 6d 2e 20 20 54 68 61 74  y subterm.  That
39a58 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
39a59 6e 0d 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  n..    ** will b
39a5a 65 20 72 65 63 6f 72 64 65 64 20 69 6e 20 69 43  e recorded in iC
39a5b 75 72 73 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d  ursor and iColum
39a5c 6e 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20  n.  There might 
39a5d 6e 6f 74 20 62 65 20 61 6e 79 0d 0a 20 20 20 20  not be any..    
39a5e 2a 2a 20 73 75 63 68 20 74 61 62 6c 65 20 61 6e  ** such table an
39a5f 64 20 63 6f 6c 75 6d 6e 2e 20 20 53 65 74 20 6f  d column.  Set o
39a60 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69 66 20 61  kToChngToIN if a
39a61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 61  n appropriate ta
39a62 62 6c 65 0d 0a 20 20 20 20 2a 2a 20 61 6e 64 20  ble..    ** and 
39a63 63 6f 6c 75 6d 6e 20 69 73 20 66 6f 75 6e 64 20  column is found 
39a64 62 75 74 20 6c 65 61 76 65 20 6f 6b 54 6f 43 68  but leave okToCh
39a65 6e 67 54 6f 49 4e 20 66 61 6c 73 65 20 69 66 20  ngToIN false if 
39a66 6e 6f 74 20 66 6f 75 6e 64 2e 0d 0a 20 20 20 20  not found...    
39a67 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  */..    for(j=0;
39a68 20 6a 3c 32 20 26 26 20 21 6f 6b 54 6f 43 68 6e   j<2 && !okToChn
39a69 67 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0d 0a 20 20  gToIN; j++){..  
39a6a 20 20 20 20 70 4f 72 54 65 72 6d 20 3d 20 70 4f      pOrTerm = pO
39a6b 72 57 63 2d 3e 61 3b 0d 0a 20 20 20 20 20 20 66  rWc->a;..      f
39a6c 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72  or(i=pOrWc->nTer
39a6d 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  m-1; i>=0; i--, 
39a6e 70 4f 72 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20  pOrTerm++){..   
39a6f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
39a70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
39a71 3d 57 4f 5f 45 51 20 29 3b 0d 0a 20 20 20 20 20  =WO_EQ );..     
39a72 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
39a73 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f  ags &= ~TERM_OR_
39a74 4f 4b 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  OK;..        if(
39a75 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75   pOrTerm->leftCu
39a76 72 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29 7b  rsor==iCursor ){
39a77 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ..          /* T
39a78 68 69 73 20 69 73 20 74 68 65 20 32 2d 62 69 74  his is the 2-bit
39a79 20 63 61 73 65 20 61 6e 64 20 77 65 20 61 72 65   case and we are
39a7a 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69   on the second i
39a7b 74 65 72 61 74 69 6f 6e 20 61 6e 64 0d 0a 20 20  teration and..  
39a7c 20 20 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65          ** curre
39a7d 6e 74 20 74 65 72 6d 20 69 73 20 66 72 6f 6d 20  nt term is from 
39a7e 74 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74  the first iterat
39a7f 69 6f 6e 2e 20 20 53 6f 20 73 6b 69 70 20 74 68  ion.  So skip th
39a80 69 73 20 74 65 72 6d 2e 20 2a 2f 0d 0a 20 20 20  is term. */..   
39a81 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a         assert( j
39a82 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ==1 );..        
39a83 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20    continue;..   
39a84 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
39a85 69 66 28 20 28 63 68 6e 67 54 6f 49 4e 20 26 20  if( (chngToIN & 
39a86 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
39a87 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  , pOrTerm->leftC
39a88 75 72 73 6f 72 29 29 3d 3d 30 20 29 7b 0d 0a 20  ursor))==0 ){.. 
39a89 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
39a8a 20 74 65 72 6d 20 6d 75 73 74 20 62 65 20 6f 66   term must be of
39a8b 20 74 68 65 20 66 6f 72 6d 20 74 31 2e 61 3d 3d   the form t1.a==
39a8c 74 32 2e 62 20 77 68 65 72 65 20 74 32 20 69 73  t2.b where t2 is
39a8d 20 69 6e 20 74 68 65 0d 0a 20 20 20 20 20 20 20   in the..       
39a8e 20 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 73     ** chngToIN s
39a8f 65 74 20 62 75 74 20 74 31 20 69 73 20 6e 6f 74  et but t1 is not
39a90 2e 20 20 54 68 69 73 20 74 65 72 6d 20 77 69 6c  .  This term wil
39a91 6c 20 62 65 20 65 69 74 68 65 72 20 70 72 65 63  l be either prec
39a92 65 65 64 65 64 0d 0a 20 20 20 20 20 20 20 20 20  eeded..         
39a93 20 2a 2a 20 6f 72 20 66 6f 6c 6c 77 65 64 20 62   ** or follwed b
39a94 79 20 61 6e 20 69 6e 76 65 72 74 65 64 20 63 6f  y an inverted co
39a95 70 79 20 28 74 32 2e 62 3d 3d 74 31 2e 61 29 2e  py (t2.b==t1.a).
39a96 20 20 53 6b 69 70 20 74 68 69 73 20 74 65 72 6d    Skip this term
39a97 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   ..          ** 
39a98 61 6e 64 20 75 73 65 20 69 74 73 20 69 6e 76 65  and use its inve
39a99 72 73 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 20 20 20  rsion. */..     
39a9a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
39a9b 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  OrTerm->wtFlags 
39a9c 26 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29 3b  & TERM_COPIED );
39a9d 0d 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  ..          test
39a9e 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77  case( pOrTerm->w
39a9f 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49  tFlags & TERM_VI
39aa0 52 54 55 41 4c 20 29 3b 0d 0a 20 20 20 20 20 20  RTUAL );..      
39aa1 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
39aa2 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
39aa3 54 45 52 4d 5f 43 4f 50 49 45 44 7c 54 45 52 4d  TERM_COPIED|TERM
39aa4 5f 56 49 52 54 55 41 4c 29 20 29 3b 0d 0a 20 20  _VIRTUAL) );..  
39aa5 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
39aa6 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
39aa7 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20        iColumn = 
39aa8 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43  pOrTerm->u.leftC
39aa9 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 20 20  olumn;..        
39aaa 69 43 75 72 73 6f 72 20 3d 20 70 4f 72 54 65 72  iCursor = pOrTer
39aab 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3b 0d 0a  m->leftCursor;..
39aac 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
39aad 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
39aae 66 28 20 69 3c 30 20 29 7b 0d 0a 20 20 20 20 20  f( i<0 ){..     
39aaf 20 20 20 2f 2a 20 4e 6f 20 63 61 6e 64 69 64 61     /* No candida
39ab0 74 65 20 74 61 62 6c 65 2b 63 6f 6c 75 6d 6e 20  te table+column 
39ab1 77 61 73 20 66 6f 75 6e 64 2e 20 20 54 68 69 73  was found.  This
39ab2 20 63 61 6e 20 6f 6e 6c 79 20 6f 63 63 75 72 0d   can only occur.
39ab3 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74  .        ** on t
39ab4 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74  he second iterat
39ab5 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ion */..        
39ab6 61 73 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0d  assert( j==1 );.
39ab7 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39ab8 20 28 63 68 6e 67 54 6f 49 4e 26 28 63 68 6e 67   (chngToIN&(chng
39ab9 54 6f 49 4e 2d 31 29 29 3d 3d 30 20 29 3b 0d 0a  ToIN-1))==0 );..
39aba 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39abb 63 68 6e 67 54 6f 49 4e 3d 3d 67 65 74 4d 61 73  chngToIN==getMas
39abc 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72  k(pMaskSet, iCur
39abd 73 6f 72 29 20 29 3b 0d 0a 20 20 20 20 20 20 20  sor) );..       
39abe 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
39abf 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
39ac0 28 20 6a 3d 3d 31 20 29 3b 0d 0a 0d 0a 20 20 20  ( j==1 );....   
39ac1 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 66 6f     /* We have fo
39ac2 75 6e 64 20 61 20 63 61 6e 64 69 64 61 74 65 20  und a candidate 
39ac3 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
39ac4 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  .  Check to see 
39ac5 69 66 20 74 68 61 74 0d 0a 20 20 20 20 20 20 2a  if that..      *
39ac6 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  * table and colu
39ac7 6d 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  mn is common to 
39ac8 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68  every term in th
39ac9 65 20 4f 52 20 63 6c 61 75 73 65 20 2a 2f 0d 0a  e OR clause */..
39aca 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f        okToChngTo
39acb 49 4e 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 66  IN = 1;..      f
39acc 6f 72 28 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 54  or(; i>=0 && okT
39acd 6f 43 68 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c 20  oChngToIN; i--, 
39ace 70 4f 72 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20  pOrTerm++){..   
39acf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
39ad0 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
39ad1 3d 57 4f 5f 45 51 20 29 3b 0d 0a 20 20 20 20 20  =WO_EQ );..     
39ad2 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e     if( pOrTerm->
39ad3 6c 65 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72  leftCursor!=iCur
39ad4 73 6f 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  sor ){..        
39ad5 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
39ad6 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f  gs &= ~TERM_OR_O
39ad7 4b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  K;..        }els
39ad8 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 75  e if( pOrTerm->u
39ad9 2e 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f  .leftColumn!=iCo
39ada 6c 75 6d 6e 20 29 7b 0d 0a 20 20 20 20 20 20 20  lumn ){..       
39adb 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20     okToChngToIN 
39adc 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  = 0;..        }e
39add 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
39ade 69 6e 74 20 61 66 66 4c 65 66 74 2c 20 61 66 66  int affLeft, aff
39adf 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 20 20  Right;..        
39ae0 20 20 2f 2a 20 49 66 20 74 68 65 20 72 69 67 68    /* If the righ
39ae1 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61  t-hand side is a
39ae2 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68  lso a column, th
39ae3 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 69 65  en the affinitie
39ae4 73 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  s..          ** 
39ae5 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20 61 6e  of both right an
39ae6 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d 75 73  d left sides mus
39ae7 74 20 62 65 20 73 75 63 68 20 74 68 61 74 20 6e  t be such that n
39ae8 6f 20 74 79 70 65 0d 0a 20 20 20 20 20 20 20 20  o type..        
39ae9 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73    ** conversions
39aea 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e   are required on
39aeb 20 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69   the right.  (Ti
39aec 63 6b 65 74 20 23 32 32 34 39 29 0d 0a 20 20 20  cket #2249)..   
39aed 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20         */..     
39aee 20 20 20 20 20 61 66 66 52 69 67 68 74 20 3d 20       affRight = 
39aef 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
39af0 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78  ity(pOrTerm->pEx
39af1 70 72 2d 3e 70 52 69 67 68 74 29 3b 0d 0a 20 20  pr->pRight);..  
39af2 20 20 20 20 20 20 20 20 61 66 66 4c 65 66 74 20          affLeft 
39af3 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
39af4 69 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70  inity(pOrTerm->p
39af5 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20  Expr->pLeft);.. 
39af6 20 20 20 20 20 20 20 20 20 69 66 28 20 61 66 66           if( aff
39af7 52 69 67 68 74 21 3d 30 20 26 26 20 61 66 66 52  Right!=0 && affR
39af8 69 67 68 74 21 3d 61 66 66 4c 65 66 74 20 29 7b  ight!=affLeft ){
39af9 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f 6b  ..            ok
39afa 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0d  ToChngToIN = 0;.
39afb 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
39afc 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  {..            p
39afd 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  OrTerm->wtFlags 
39afe 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0d 0a  |= TERM_OR_OK;..
39aff 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
39b00 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
39b01 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
39b02 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
39b03 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69 73 20  okToChngToIN is 
39b04 74 72 75 65 20 69 66 20 6f 72 69 67 69 6e 61 6c  true if original
39b05 20 70 54 65 72 6d 20 73 61 74 69 73 66 69 65 73   pTerm satisfies
39b06 0d 0a 20 20 20 20 2a 2a 20 63 61 73 65 20 31 2e  ..    ** case 1.
39b07 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
39b08 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20  construct a new 
39b09 76 69 72 74 75 61 6c 20 74 65 72 6d 20 74 68 61  virtual term tha
39b0a 74 20 69 73 20 0d 0a 20 20 20 20 2a 2a 20 70 54  t is ..    ** pT
39b0b 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  erm converted in
39b0c 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  to an IN operato
39b0d 72 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  r...    **..    
39b0e 2a 2a 20 45 56 3a 20 52 2d 30 30 32 31 31 2d 31  ** EV: R-00211-1
39b0f 35 31 30 30 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  5100..    */..  
39b10 20 20 69 66 28 20 6f 6b 54 6f 43 68 6e 67 54 6f    if( okToChngTo
39b11 49 4e 20 29 7b 0d 0a 20 20 20 20 20 20 45 78 70  IN ){..      Exp
39b12 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20  r *pDup;        
39b13 20 20 20 20 2f 2a 20 41 20 74 72 61 6e 73 69 65      /* A transie
39b14 6e 74 20 64 75 70 6c 69 63 61 74 65 20 65 78 70  nt duplicate exp
39b15 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20  ression */..    
39b16 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
39b17 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  t = 0;   /* The 
39b18 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70  RHS of the IN op
39b19 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 20  erator */..     
39b1a 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 30   Expr *pLeft = 0
39b1b 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;       /* The L
39b1c 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65  HS of the IN ope
39b1d 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 20 20  rator */..      
39b1e 45 78 70 72 20 2a 70 4e 65 77 3b 20 20 20 20 20  Expr *pNew;     
39b1f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
39b20 6d 70 6c 65 74 65 20 49 4e 20 6f 70 65 72 61 74  mplete IN operat
39b21 6f 72 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 66  or */....      f
39b22 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72  or(i=pOrWc->nTer
39b23 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72  m-1, pOrTerm=pOr
39b24 57 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Wc->a; i>=0; i--
39b25 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0d 0a 20  , pOrTerm++){.. 
39b26 20 20 20 20 20 20 20 69 66 28 20 28 70 4f 72 54         if( (pOrT
39b27 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
39b28 45 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20  ERM_OR_OK)==0 ) 
39b29 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20  continue;..     
39b2a 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65     assert( pOrTe
39b2b 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
39b2c 4f 5f 45 51 20 29 3b 0d 0a 20 20 20 20 20 20 20  O_EQ );..       
39b2d 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
39b2e 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
39b2f 75 72 73 6f 72 20 29 3b 0d 0a 20 20 20 20 20 20  ursor );..      
39b30 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72    assert( pOrTer
39b31 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d  m->u.leftColumn=
39b32 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0d 0a 20 20 20  =iColumn );..   
39b33 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69       pDup = sqli
39b34 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
39b35 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  OrTerm->pExpr->p
39b36 52 69 67 68 74 2c 20 30 29 3b 0d 0a 20 20 20 20  Right, 0);..    
39b37 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
39b38 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
39b39 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  d(pWC->pParse, p
39b3a 4c 69 73 74 2c 20 70 44 75 70 29 3b 0d 0a 20 20  List, pDup);..  
39b3b 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4f        pLeft = pO
39b3c 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c  rTerm->pExpr->pL
39b3d 65 66 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eft;..      }.. 
39b3e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
39b3f 66 74 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  ft!=0 );..      
39b40 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
39b41 70 72 44 75 70 28 64 62 2c 20 70 4c 65 66 74 2c  prDup(db, pLeft,
39b42 20 30 29 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77   0);..      pNew
39b43 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
39b44 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70  pParse, TK_IN, p
39b45 44 75 70 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  Dup, 0, 0);..   
39b46 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0d 0a     if( pNew ){..
39b47 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e          int idxN
39b48 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 74 72 61  ew;..        tra
39b49 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67  nsferJoinMarking
39b4a 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29 3b 0d  s(pNew, pExpr);.
39b4b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39b4c 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
39b4d 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65  y(pNew, EP_xIsSe
39b4e 6c 65 63 74 29 20 29 3b 0d 0a 20 20 20 20 20 20  lect) );..      
39b4f 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20    pNew->x.pList 
39b50 3d 20 70 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20  = pList;..      
39b51 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
39b52 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
39b53 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52  , pNew, TERM_VIR
39b54 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
39b55 43 29 3b 0d 0a 20 20 20 20 20 20 20 20 74 65 73  C);..        tes
39b56 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30  tcase( idxNew==0
39b57 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 65 78 70   );..        exp
39b58 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
39b59 57 43 2c 20 69 64 78 4e 65 77 29 3b 0d 0a 20 20  WC, idxNew);..  
39b5a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70        pTerm = &p
39b5b 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0d  WC->a[idxTerm];.
39b5c 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  .        pWC->a[
39b5d 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20  idxNew].iParent 
39b5e 3d 20 69 64 78 54 65 72 6d 3b 0d 0a 20 20 20 20  = idxTerm;..    
39b5f 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
39b60 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 65  d = 1;..      }e
39b61 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  lse{..        sq
39b62 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
39b63 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0d  ete(db, pList);.
39b64 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
39b65 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
39b66 20 3d 20 57 4f 5f 4e 4f 4f 50 3b 20 20 2f 2a 20   = WO_NOOP;  /* 
39b67 63 61 73 65 20 31 20 74 72 75 6d 70 73 20 63 61  case 1 trumps ca
39b68 73 65 20 32 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a  se 2 */..    }..
39b69 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f    }..}..#endif /
39b6a 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  * !SQLITE_OMIT_O
39b6b 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 26  R_OPTIMIZATION &
39b6c 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  & !SQLITE_OMIT_S
39b6d 55 42 51 55 45 52 59 20 2a 2f 0d 0a 0d 0a 0d 0a  UBQUERY */......
39b6e 2f 2a 0d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  /*..** The input
39b6f 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
39b70 20 69 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d   is an WhereTerm
39b71 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20   structure with 
39b72 6f 6e 6c 79 20 74 68 65 0d 0a 2a 2a 20 22 70 45  only the..** "pE
39b73 78 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c 65  xpr" field fille
39b74 64 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f  d in.  The job o
39b75 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
39b76 73 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65  s to analyze the
39b77 0d 0a 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69  ..** subexpressi
39b78 6f 6e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  on and populate 
39b79 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69  all the other fi
39b7a 65 6c 64 73 20 6f 66 20 74 68 65 20 57 68 65 72  elds of the Wher
39b7b 65 54 65 72 6d 0d 0a 2a 2a 20 73 74 72 75 63 74  eTerm..** struct
39b7c 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ure...**..** If 
39b7d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
39b7e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  s of the form "<
39b7f 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20 69 74  expr> <op> X" it
39b80 20 67 65 74 73 20 63 6f 6d 6d 75 74 65 64 0d 0a   gets commuted..
39b81 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61  ** to the standa
39b82 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c 6f  rd form of "X <o
39b83 70 3e 20 3c 65 78 70 72 3e 22 2e 0d 0a 2a 2a 0d  p> <expr>"...**.
39b84 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
39b85 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
39b86 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20  form "X <op> Y" 
39b87 77 68 65 72 65 20 62 6f 74 68 20 58 20 61 6e 64  where both X and
39b88 20 59 20 61 72 65 0d 0a 2a 2a 20 63 6f 6c 75 6d   Y are..** colum
39b89 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69  ns, then the ori
39b8a 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  ginal expression
39b8b 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e   is unchanged an
39b8c 64 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0d  d a new virtual.
39b8d 0a 2a 2a 20 74 65 72 6d 20 6f 66 20 74 68 65 20  .** term of the 
39b8e 66 6f 72 6d 20 22 59 20 3c 6f 70 3e 20 58 22 20  form "Y <op> X" 
39b8f 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
39b90 57 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64  WHERE clause and
39b91 0d 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 73 65  ..** analyzed se
39b92 70 61 72 61 74 65 6c 79 2e 20 20 54 68 65 20 6f  parately.  The o
39b93 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 69 73 20  riginal term is 
39b94 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d  marked with TERM
39b95 5f 43 4f 50 49 45 44 0d 0a 2a 2a 20 61 6e 64 20  _COPIED..** and 
39b96 74 68 65 20 6e 65 77 20 74 65 72 6d 20 69 73 20  the new term is 
39b97 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d  marked with TERM
39b98 5f 44 59 4e 41 4d 49 43 20 28 62 65 63 61 75 73  _DYNAMIC (becaus
39b99 65 20 69 74 27 73 20 70 45 78 70 72 0d 0a 2a 2a  e it's pExpr..**
39b9a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65   needs to be fre
39b9b 65 64 20 77 69 74 68 20 74 68 65 20 57 68 65 72  ed with the Wher
39b9c 65 43 6c 61 75 73 65 29 20 61 6e 64 20 54 45 52  eClause) and TER
39b9d 4d 5f 56 49 52 54 55 41 4c 20 28 62 65 63 61 75  M_VIRTUAL (becau
39b9e 73 65 20 69 74 0d 0a 2a 2a 20 69 73 20 61 20 63  se it..** is a c
39b9f 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f 66 20  ommuted copy of 
39ba0 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29 20 20  a prior term.)  
39ba1 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72  The original ter
39ba2 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31 0d 0a  m has nChild=1..
39ba3 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79 20  ** and the copy 
39ba4 68 61 73 20 69 64 78 50 61 72 65 6e 74 20 73 65  has idxParent se
39ba5 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  t to the index o
39ba6 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74  f the original t
39ba7 65 72 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  erm...*/..static
39ba8 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a   void exprAnalyz
39ba9 65 28 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  e(..  SrcList *p
39baa 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
39bab 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
39bac 73 65 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c  se */..  WhereCl
39bad 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
39bae 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20     /* the WHERE 
39baf 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69 6e 74  clause */..  int
39bb0 20 69 64 78 54 65 72 6d 20 20 20 20 20 20 20 20   idxTerm        
39bb1 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
39bb2 6f 66 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62  of the term to b
39bb3 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0d 0a 29  e analyzed */..)
39bb4 7b 0d 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  {..  WhereTerm *
39bb5 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
39bb6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72        /* The ter
39bb7 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  m to be analyzed
39bb8 20 2a 2f 0d 0a 20 20 57 68 65 72 65 4d 61 73 6b   */..  WhereMask
39bb9 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20  Set *pMaskSet;  
39bba 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f          /* Set o
39bbb 66 20 74 61 62 6c 65 20 69 6e 64 65 78 20 6d 61  f table index ma
39bbc 73 6b 73 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a  sks */..  Expr *
39bbd 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
39bbe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
39bbf 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
39bc0 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0d 0a  be analyzed */..
39bc1 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
39bc2 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20  Left;           
39bc3 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69     /* Prerequesi
39bc4 74 65 73 20 6f 66 20 74 68 65 20 70 45 78 70 72  tes of the pExpr
39bc5 2d 3e 70 4c 65 66 74 20 2a 2f 0d 0a 20 20 42 69  ->pLeft */..  Bi
39bc6 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b  tmask prereqAll;
39bc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39bc8 2a 20 50 72 65 72 65 71 75 65 73 69 74 65 73 20  * Prerequesites 
39bc9 6f 66 20 70 45 78 70 72 20 2a 2f 0d 0a 20 20 42  of pExpr */..  B
39bca 69 74 6d 61 73 6b 20 65 78 74 72 61 52 69 67 68  itmask extraRigh
39bcb 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
39bcc 2f 2a 20 45 78 74 72 61 20 64 65 70 65 6e 64 65  /* Extra depende
39bcd 6e 63 69 65 73 20 6f 6e 20 4c 45 46 54 20 4a 4f  ncies on LEFT JO
39bce 49 4e 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  IN */..  Expr *p
39bcf 53 74 72 31 20 3d 20 30 3b 20 20 20 20 20 20 20  Str1 = 0;       
39bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 48 53            /* RHS
39bd1 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70   of LIKE/GLOB op
39bd2 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74  erator */..  int
39bd3 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30 3b   isComplete = 0;
39bd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39bd5 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f   RHS of LIKE/GLO
39bd6 42 20 65 6e 64 73 20 77 69 74 68 20 77 69 6c 64  B ends with wild
39bd7 63 61 72 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  card */..  int n
39bd8 6f 43 61 73 65 20 3d 20 30 3b 20 20 20 20 20 20  oCase = 0;      
39bd9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
39bda 49 4b 45 2f 47 4c 4f 42 20 64 69 73 74 69 6e 67  IKE/GLOB disting
39bdb 75 69 73 68 65 73 20 63 61 73 65 20 2a 2f 0d 0a  uishes case */..
39bdc 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
39bdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39bde 20 20 20 2f 2a 20 54 6f 70 2d 6c 65 76 65 6c 20     /* Top-level 
39bdf 6f 70 65 72 61 74 6f 72 2e 20 20 70 45 78 70 72  operator.  pExpr
39be0 2d 3e 6f 70 20 2a 2f 0d 0a 20 20 50 61 72 73 65  ->op */..  Parse
39be1 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e   *pParse = pWC->
39be2 70 50 61 72 73 65 3b 20 20 20 20 20 2f 2a 20 50  pParse;     /* P
39be3 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
39be4 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  /..  sqlite3 *db
39be5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
39be6 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
39be7 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
39be8 0a 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  ...  if( db->mal
39be9 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
39bea 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
39beb 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
39bec 61 5b 69 64 78 54 65 72 6d 5d 3b 0d 0a 20 20 70  a[idxTerm];..  p
39bed 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70  MaskSet = pWC->p
39bee 4d 61 73 6b 53 65 74 3b 0d 0a 20 20 70 45 78 70  MaskSet;..  pExp
39bef 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  r = pTerm->pExpr
39bf0 3b 0d 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20  ;..  prereqLeft 
39bf1 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
39bf2 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
39bf3 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20 6f 70 20  ->pLeft);..  op 
39bf4 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0d 0a 20 20  = pExpr->op;..  
39bf5 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b  if( op==TK_IN ){
39bf6 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ..    assert( pE
39bf7 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  xpr->pRight==0 )
39bf8 3b 0d 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  ;..    if( ExprH
39bf9 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
39bfa 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
39bfb 29 7b 0d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  ){..      pTerm-
39bfc 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65  >prereqRight = e
39bfd 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
39bfe 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
39bff 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b  xpr->x.pSelect);
39c00 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
39c01 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65      pTerm->prere
39c02 71 52 69 67 68 74 20 3d 20 65 78 70 72 4c 69 73  qRight = exprLis
39c03 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
39c04 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70  kSet, pExpr->x.p
39c05 4c 69 73 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  List);..    }.. 
39c06 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54   }else if( op==T
39c07 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20  K_ISNULL ){..   
39c08 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69   pTerm->prereqRi
39c09 67 68 74 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73  ght = 0;..  }els
39c0a 65 7b 0d 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70  e{..    pTerm->p
39c0b 72 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70  rereqRight = exp
39c0c 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
39c0d 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  kSet, pExpr->pRi
39c0e 67 68 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 72  ght);..  }..  pr
39c0f 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72 54 61  ereqAll = exprTa
39c10 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
39c11 74 2c 20 70 45 78 70 72 29 3b 0d 0a 20 20 69 66  t, pExpr);..  if
39c12 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
39c13 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d  y(pExpr, EP_From
39c14 4a 6f 69 6e 29 20 29 7b 0d 0a 20 20 20 20 42 69  Join) ){..    Bi
39c15 74 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73  tmask x = getMas
39c16 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  k(pMaskSet, pExp
39c17 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  r->iRightJoinTab
39c18 6c 65 29 3b 0d 0a 20 20 20 20 70 72 65 72 65 71  le);..    prereq
39c19 41 6c 6c 20 7c 3d 20 78 3b 0d 0a 20 20 20 20 65  All |= x;..    e
39c1a 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d 31 3b  xtraRight = x-1;
39c1b 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74    /* ON clause t
39c1c 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  erms may not be 
39c1d 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64  used with an ind
39c1e 65 78 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ex..            
39c1f 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
39c20 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61   left table of a
39c21 20 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63   LEFT JOIN.  Tic
39c22 6b 65 74 20 23 33 30 31 35 20 2a 2f 0d 0a 20 20  ket #3015 */..  
39c23 7d 0d 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  }..  pTerm->prer
39c24 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c  eqAll = prereqAl
39c25 6c 3b 0d 0a 20 20 70 54 65 72 6d 2d 3e 6c 65 66  l;..  pTerm->lef
39c26 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0d 0a 20  tCursor = -1;.. 
39c27 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20   pTerm->iParent 
39c28 3d 20 2d 31 3b 0d 0a 20 20 70 54 65 72 6d 2d 3e  = -1;..  pTerm->
39c29 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0d 0a  eOperator = 0;..
39c2a 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
39c2b 6f 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70  op) && (pTerm->p
39c2c 72 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65  rereqRight & pre
39c2d 72 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0d 0a  reqLeft)==0 ){..
39c2e 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20      Expr *pLeft 
39c2f 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0d  = pExpr->pLeft;.
39c30 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68  .    Expr *pRigh
39c31 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68  t = pExpr->pRigh
39c32 74 3b 0d 0a 20 20 20 20 69 66 28 20 70 4c 65 66  t;..    if( pLef
39c33 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  t->op==TK_COLUMN
39c34 20 29 7b 0d 0a 20 20 20 20 20 20 70 54 65 72 6d   ){..      pTerm
39c35 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70  ->leftCursor = p
39c36 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0d 0a 20  Left->iTable;.. 
39c37 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e 6c 65       pTerm->u.le
39c38 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74  ftColumn = pLeft
39c39 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20  ->iColumn;..    
39c3a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74    pTerm->eOperat
39c3b 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73  or = operatorMas
39c3c 6b 28 6f 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  k(op);..    }.. 
39c3d 20 20 20 69 66 28 20 70 52 69 67 68 74 20 26 26     if( pRight &&
39c3e 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f   pRight->op==TK_
39c3f 43 4f 4c 55 4d 4e 20 29 7b 0d 0a 20 20 20 20 20  COLUMN ){..     
39c40 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77   WhereTerm *pNew
39c41 3b 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ;..      Expr *p
39c42 44 75 70 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  Dup;..      if( 
39c43 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
39c44 72 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  r>=0 ){..       
39c45 20 69 6e 74 20 69 64 78 4e 65 77 3b 0d 0a 20 20   int idxNew;..  
39c46 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
39c47 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
39c48 70 45 78 70 72 2c 20 30 29 3b 0d 0a 20 20 20 20  pExpr, 0);..    
39c49 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
39c4a 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20 20  ocFailed ){..   
39c4b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
39c4c 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75  prDelete(db, pDu
39c4d 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72  p);..          r
39c4e 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 20 20  eturn;..        
39c4f 7d 0d 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65  }..        idxNe
39c50 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
39c51 6e 73 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c  nsert(pWC, pDup,
39c52 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
39c53 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a 20 20  RM_DYNAMIC);..  
39c54 20 20 20 20 20 20 69 66 28 20 69 64 78 4e 65 77        if( idxNew
39c55 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  ==0 ) return;.. 
39c56 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 70         pNew = &p
39c57 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0d 0a  WC->a[idxNew];..
39c58 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 50          pNew->iP
39c59 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b  arent = idxTerm;
39c5a 0d 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20  ..        pTerm 
39c5b 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
39c5c 6d 5d 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 65  m];..        pTe
39c5d 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0d  rm->nChild = 1;.
39c5e 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  .        pTerm->
39c5f 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
39c60 43 4f 50 49 45 44 3b 0d 0a 20 20 20 20 20 20 7d  COPIED;..      }
39c61 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70  else{..        p
39c62 44 75 70 20 3d 20 70 45 78 70 72 3b 0d 0a 20 20  Dup = pExpr;..  
39c63 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 54 65        pNew = pTe
39c64 72 6d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  rm;..      }..  
39c65 20 20 20 20 65 78 70 72 43 6f 6d 6d 75 74 65 28      exprCommute(
39c66 70 50 61 72 73 65 2c 20 70 44 75 70 29 3b 0d 0a  pParse, pDup);..
39c67 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 44        pLeft = pD
39c68 75 70 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20 20 20  up->pLeft;..    
39c69 20 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73    pNew->leftCurs
39c6a 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
39c6b 6c 65 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77 2d  le;..      pNew-
39c6c 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20  >u.leftColumn = 
39c6d 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d  pLeft->iColumn;.
39c6e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
39c6f 20 28 70 72 65 72 65 71 4c 65 66 74 20 7c 20 65   (prereqLeft | e
39c70 78 74 72 61 52 69 67 68 74 29 20 21 3d 20 70 72  xtraRight) != pr
39c71 65 72 65 71 4c 65 66 74 20 29 3b 0d 0a 20 20 20  ereqLeft );..   
39c72 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52     pNew->prereqR
39c73 69 67 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66  ight = prereqLef
39c74 74 20 7c 20 65 78 74 72 61 52 69 67 68 74 3b 0d  t | extraRight;.
39c75 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65  .      pNew->pre
39c76 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41  reqAll = prereqA
39c77 6c 6c 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77 2d  ll;..      pNew-
39c78 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
39c79 72 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e  ratorMask(pDup->
39c7a 6f 70 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  op);..    }..  }
39c7b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
39c7c 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f  TE_OMIT_BETWEEN_
39c7d 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0d 0a 20 20  OPTIMIZATION..  
39c7e 2f 2a 20 49 66 20 61 20 74 65 72 6d 20 69 73 20  /* If a term is 
39c7f 74 68 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72  the BETWEEN oper
39c80 61 74 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f  ator, create two
39c81 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72   new virtual ter
39c82 6d 73 0d 0a 20 20 2a 2a 20 74 68 61 74 20 64 65  ms..  ** that de
39c83 66 69 6e 65 20 74 68 65 20 72 61 6e 67 65 20 74  fine the range t
39c84 68 61 74 20 74 68 65 20 42 45 54 57 45 45 4e 20  hat the BETWEEN 
39c85 69 6d 70 6c 65 6d 65 6e 74 73 2e 20 20 46 6f 72  implements.  For
39c86 20 65 78 61 6d 70 6c 65 3a 0d 0a 20 20 2a 2a 0d   example:..  **.
39c87 0a 20 20 2a 2a 20 20 20 20 20 20 61 20 42 45 54  .  **      a BET
39c88 57 45 45 4e 20 62 20 41 4e 44 20 63 0d 0a 20 20  WEEN b AND c..  
39c89 2a 2a 0d 0a 20 20 2a 2a 20 69 73 20 63 6f 6e 76  **..  ** is conv
39c8a 65 72 74 65 64 20 69 6e 74 6f 3a 0d 0a 20 20 2a  erted into:..  *
39c8b 2a 0d 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 20  *..  **      (a 
39c8c 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 29  BETWEEN b AND c)
39c8d 20 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 20   AND (a>=b) AND 
39c8e 28 61 3c 3d 63 29 0d 0a 20 20 2a 2a 0d 0a 20 20  (a<=c)..  **..  
39c8f 2a 2a 20 54 68 65 20 74 77 6f 20 6e 65 77 20 74  ** The two new t
39c90 65 72 6d 73 20 61 72 65 20 61 64 64 65 64 20 6f  erms are added o
39c91 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
39c92 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f  he WhereClause o
39c93 62 6a 65 63 74 2e 0d 0a 20 20 2a 2a 20 54 68 65  bject...  ** The
39c94 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20 22   new terms are "
39c95 64 79 6e 61 6d 69 63 22 20 61 6e 64 20 61 72 65  dynamic" and are
39c96 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 65   children of the
39c97 20 6f 72 69 67 69 6e 61 6c 20 42 45 54 57 45 45   original BETWEE
39c98 4e 0d 0a 20 20 2a 2a 20 74 65 72 6d 2e 20 20 54  N..  ** term.  T
39c99 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69  hat means that i
39c9a 66 20 74 68 65 20 42 45 54 57 45 45 4e 20 74 65  f the BETWEEN te
39c9b 72 6d 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65  rm is coded, the
39c9c 20 63 68 69 6c 64 72 65 6e 20 61 72 65 0d 0a 20   children are.. 
39c9d 20 2a 2a 20 73 6b 69 70 70 65 64 2e 20 20 4f 72   ** skipped.  Or
39c9e 2c 20 69 66 20 74 68 65 20 63 68 69 6c 64 72 65  , if the childre
39c9f 6e 20 61 72 65 20 73 61 74 69 73 66 69 65 64 20  n are satisfied 
39ca0 62 79 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65  by an index, the
39ca1 20 6f 72 69 67 69 6e 61 6c 0d 0a 20 20 2a 2a 20   original..  ** 
39ca2 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73 20  BETWEEN term is 
39ca3 73 6b 69 70 70 65 64 2e 0d 0a 20 20 2a 2f 0d 0a  skipped...  */..
39ca4 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
39ca5 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e  ->op==TK_BETWEEN
39ca6 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f   && pWC->op==TK_
39ca7 41 4e 44 20 29 7b 0d 0a 20 20 20 20 45 78 70 72  AND ){..    Expr
39ca8 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
39ca9 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0d 0a 20  xpr->x.pList;.. 
39caa 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 73     int i;..    s
39cab 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 6f  tatic const u8 o
39cac 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45 2c 20 54  ps[] = {TK_GE, T
39cad 4b 5f 4c 45 7d 3b 0d 0a 20 20 20 20 61 73 73 65  K_LE};..    asse
39cae 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 0d  rt( pList!=0 );.
39caf 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
39cb0 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b 0d  st->nExpr==2 );.
39cb1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
39cb2 32 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  2; i++){..      
39cb3 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0d  Expr *pNewExpr;.
39cb4 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65  .      int idxNe
39cb5 77 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77 45 78  w;..      pNewEx
39cb6 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
39cb7 72 28 70 50 61 72 73 65 2c 20 6f 70 73 5b 69 5d  r(pParse, ops[i]
39cb8 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
39cb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39cba 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
39cbb 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  db, pExpr->pLeft
39cbc 2c 20 30 29 2c 0d 0a 20 20 20 20 20 20 20 20 20  , 0),..         
39cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39cbe 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
39cbf 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  up(db, pList->a[
39cc0 69 5d 2e 70 45 78 70 72 2c 20 30 29 2c 20 30 29  i].pExpr, 0), 0)
39cc1 3b 0d 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20  ;..      idxNew 
39cc2 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
39cc3 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
39cc4 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  r, TERM_VIRTUAL|
39cc5 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a  TERM_DYNAMIC);..
39cc6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
39cc7 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0d 0a 20 20  idxNew==0 );..  
39cc8 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
39cc9 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
39cca 77 29 3b 0d 0a 20 20 20 20 20 20 70 54 65 72 6d  w);..      pTerm
39ccb 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
39ccc 72 6d 5d 3b 0d 0a 20 20 20 20 20 20 70 57 43 2d  rm];..      pWC-
39ccd 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65  >a[idxNew].iPare
39cce 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0d 0a 20  nt = idxTerm;.. 
39ccf 20 20 20 7d 0d 0a 20 20 20 20 70 54 65 72 6d 2d     }..    pTerm-
39cd0 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0d 0a 20 20  >nChild = 2;..  
39cd1 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
39cd2 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e  ITE_OMIT_BETWEEN
39cd3 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
39cd4 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
39cd5 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f  (SQLITE_OMIT_OR_
39cd6 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26  OPTIMIZATION) &&
39cd7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
39cd8 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0d  _OMIT_SUBQUERY).
39cd9 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 20  .  /* Analyze a 
39cda 74 65 72 6d 20 74 68 61 74 20 69 73 20 63 6f 6d  term that is com
39cdb 70 6f 73 65 64 20 6f 66 20 74 77 6f 20 6f 72 20  posed of two or 
39cdc 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f  more subterms co
39cdd 6e 6e 65 63 74 65 64 20 62 79 0d 0a 20 20 2a 2a  nnected by..  **
39cde 20 61 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72 2e   an OR operator.
39cdf 0d 0a 20 20 2a 2f 0d 0a 20 20 65 6c 73 65 20 69  ..  */..  else i
39ce0 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  f( pExpr->op==TK
39ce1 5f 4f 52 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  _OR ){..    asse
39ce2 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f  rt( pWC->op==TK_
39ce3 41 4e 44 20 29 3b 0d 0a 20 20 20 20 65 78 70 72  AND );..    expr
39ce4 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 70 53  AnalyzeOrTerm(pS
39ce5 72 63 2c 20 70 57 43 2c 20 69 64 78 54 65 72 6d  rc, pWC, idxTerm
39ce6 29 3b 0d 0a 20 20 20 20 70 54 65 72 6d 20 3d 20  );..    pTerm = 
39ce7 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
39ce8 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f  ;..  }..#endif /
39ce9 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  * SQLITE_OMIT_OR
39cea 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
39ceb 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
39cec 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
39ced 49 4d 49 5a 41 54 49 4f 4e 0d 0a 20 20 2f 2a 20  IMIZATION..  /* 
39cee 41 64 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  Add constraints 
39cef 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65  to reduce the se
39cf0 61 72 63 68 20 73 70 61 63 65 20 6f 6e 20 61 20  arch space on a 
39cf1 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0d 0a 20 20  LIKE or GLOB..  
39cf2 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0d 0a 20 20  ** operator...  
39cf3 2a 2a 0d 0a 20 20 2a 2a 20 41 20 6c 69 6b 65 20  **..  ** A like 
39cf4 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20 66  pattern of the f
39cf5 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62 63  orm "x LIKE 'abc
39cf6 25 27 22 20 69 73 20 63 68 61 6e 67 65 64 20 69  %'" is changed i
39cf7 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73 0d  nto constraints.
39cf8 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20  .  **..  **     
39cf9 20 20 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e       x>='abc' AN
39cfa 44 20 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20  D x<'abd' AND x 
39cfb 4c 49 4b 45 20 27 61 62 63 25 27 0d 0a 20 20 2a  LIKE 'abc%'..  *
39cfc 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 6c 61 73 74  *..  ** The last
39cfd 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
39cfe 65 20 70 72 65 66 69 78 20 22 61 62 63 22 20 69  e prefix "abc" i
39cff 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f  s incremented to
39d00 20 66 6f 72 6d 20 74 68 65 0d 0a 20 20 2a 2a 20   form the..  ** 
39d01 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 6e 64  termination cond
39d02 69 74 69 6f 6e 20 22 61 62 64 22 2e 0d 0a 20 20  ition "abd"...  
39d03 2a 2f 0d 0a 20 20 69 66 28 20 70 57 43 2d 3e 6f  */..  if( pWC->o
39d04 70 3d 3d 54 4b 5f 41 4e 44 20 0d 0a 20 20 20 26  p==TK_AND ..   &
39d05 26 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 70  & isLikeOrGlob(p
39d06 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 70  Parse, pExpr, &p
39d07 53 74 72 31 2c 20 26 69 73 43 6f 6d 70 6c 65 74  Str1, &isComplet
39d08 65 2c 20 26 6e 6f 43 61 73 65 29 0d 0a 20 20 29  e, &noCase)..  )
39d09 7b 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65  {..    Expr *pLe
39d0a 66 74 3b 20 20 20 20 20 20 20 2f 2a 20 4c 48 53  ft;       /* LHS
39d0b 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70   of LIKE/GLOB op
39d0c 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 45  erator */..    E
39d0d 78 70 72 20 2a 70 53 74 72 32 3b 20 20 20 20 20  xpr *pStr2;     
39d0e 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 53 74    /* Copy of pSt
39d0f 72 31 20 2d 20 52 48 53 20 6f 66 20 4c 49 4b 45  r1 - RHS of LIKE
39d10 2f 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a  /GLOB operator *
39d11 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65  /..    Expr *pNe
39d12 77 45 78 70 72 31 3b 0d 0a 20 20 20 20 45 78 70  wExpr1;..    Exp
39d13 72 20 2a 70 4e 65 77 45 78 70 72 32 3b 0d 0a 20  r *pNewExpr2;.. 
39d14 20 20 20 69 6e 74 20 69 64 78 4e 65 77 31 3b 0d     int idxNew1;.
39d15 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 32  .    int idxNew2
39d16 3b 0d 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  ;..    CollSeq *
39d17 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c  pColl;    /* Col
39d18 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
39d19 74 6f 20 75 73 65 20 2a 2f 0d 0a 0d 0a 20 20 20  to use */....   
39d1a 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
39d1b 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  x.pList->a[1].pE
39d1c 78 70 72 3b 0d 0a 20 20 20 20 70 53 74 72 32 20  xpr;..    pStr2 
39d1d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
39d1e 28 64 62 2c 20 70 53 74 72 31 2c 20 30 29 3b 0d  (db, pStr1, 0);.
39d1f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  .    if( !db->ma
39d20 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20  llocFailed ){.. 
39d21 20 20 20 20 20 75 38 20 63 2c 20 2a 70 43 3b 20       u8 c, *pC; 
39d22 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 63 68        /* Last ch
39d23 61 72 61 63 74 65 72 20 62 65 66 6f 72 65 20 74  aracter before t
39d24 68 65 20 66 69 72 73 74 20 77 69 6c 64 63 61 72  he first wildcar
39d25 64 20 2a 2f 0d 0a 20 20 20 20 20 20 70 43 20 3d  d */..      pC =
39d26 20 28 75 38 2a 29 26 70 53 74 72 32 2d 3e 75 2e   (u8*)&pStr2->u.
39d27 7a 54 6f 6b 65 6e 5b 73 71 6c 69 74 65 33 53 74  zToken[sqlite3St
39d28 72 6c 65 6e 33 30 28 70 53 74 72 32 2d 3e 75 2e  rlen30(pStr2->u.
39d29 7a 54 6f 6b 65 6e 29 2d 31 5d 3b 0d 0a 20 20 20  zToken)-1];..   
39d2a 20 20 20 63 20 3d 20 2a 70 43 3b 0d 0a 20 20 20     c = *pC;..   
39d2b 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b     if( noCase ){
39d2c 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ..        /* The
39d2d 20 70 6f 69 6e 74 20 69 73 20 74 6f 20 69 6e 63   point is to inc
39d2e 72 65 6d 65 6e 74 20 74 68 65 20 6c 61 73 74 20  rement the last 
39d2f 63 68 61 72 61 63 74 65 72 20 62 65 66 6f 72 65  character before
39d30 20 74 68 65 20 66 69 72 73 74 0d 0a 20 20 20 20   the first..    
39d31 20 20 20 20 2a 2a 20 77 69 6c 64 63 61 72 64 2e      ** wildcard.
39d32 20 20 42 75 74 20 69 66 20 77 65 20 69 6e 63 72    But if we incr
39d33 65 6d 65 6e 74 20 27 40 27 2c 20 74 68 61 74 20  ement '@', that 
39d34 77 69 6c 6c 20 70 75 73 68 20 69 74 20 69 6e 74  will push it int
39d35 6f 20 74 68 65 0d 0a 20 20 20 20 20 20 20 20 2a  o the..        *
39d36 2a 20 61 6c 70 68 61 62 65 74 69 63 20 72 61 6e  * alphabetic ran
39d37 67 65 20 77 68 65 72 65 20 63 61 73 65 20 63 6f  ge where case co
39d38 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 6d  nversions will m
39d39 65 73 73 20 75 70 20 74 68 65 20 0d 0a 20 20 20  ess up the ..   
39d3a 20 20 20 20 20 2a 2a 20 69 6e 65 71 75 61 6c 69       ** inequali
39d3b 74 79 2e 20 20 54 6f 20 61 76 6f 69 64 20 74 68  ty.  To avoid th
39d3c 69 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 6f  is, make sure to
39d3d 20 61 6c 73 6f 20 72 75 6e 20 74 68 65 20 66 75   also run the fu
39d3e 6c 6c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 4c  ll..        ** L
39d3f 49 4b 45 20 6f 6e 20 61 6c 6c 20 63 61 6e 64 69  IKE on all candi
39d40 64 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  date expressions
39d41 20 62 79 20 63 6c 65 61 72 69 6e 67 20 74 68 65   by clearing the
39d42 20 69 73 43 6f 6d 70 6c 65 74 65 20 66 6c 61 67   isComplete flag
39d43 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20  ..        */..  
39d44 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 41 27        if( c=='A'
39d45 2d 31 20 29 20 69 73 43 6f 6d 70 6c 65 74 65 20  -1 ) isComplete 
39d46 3d 20 30 3b 20 20 20 2f 2a 20 45 56 3a 20 52 2d  = 0;   /* EV: R-
39d47 36 34 33 33 39 2d 30 38 32 30 37 20 2a 2f 0d 0a  64339-08207 */..
39d48 20 20 20 20 20 20 20 20 63 20 3d 20 6f 61 73 55          c = oasU
39d49 70 70 65 72 32 4c 6f 77 65 72 5b 63 5d 3b 0d 0a  pper2Lower[c];..
39d4a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 2a        }..      *
39d4b 70 43 20 3d 20 63 20 2b 20 31 3b 0d 0a 20 20 20  pC = c + 1;..   
39d4c 20 7d 0d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   }..    pColl = 
39d4d 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
39d4e 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54  eq(db, SQLITE_UT
39d4f 46 38 2c 20 6e 6f 43 61 73 65 20 3f 20 22 4e 4f  F8, noCase ? "NO
39d50 43 41 53 45 22 20 3a 20 22 42 49 4e 41 52 59 22  CASE" : "BINARY"
39d51 2c 30 29 3b 0d 0a 20 20 20 20 70 4e 65 77 45 78  ,0);..    pNewEx
39d52 70 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78  pr1 = sqlite3PEx
39d53 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 47 45  pr(pParse, TK_GE
39d54 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
39d55 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
39d56 45 78 70 72 53 65 74 43 6f 6c 6c 28 73 71 6c 69  ExprSetColl(sqli
39d57 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c  te3ExprDup(db,pL
39d58 65 66 74 2c 30 29 2c 20 70 43 6f 6c 6c 29 2c 0d  eft,0), pColl),.
39d59 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
39d5a 20 20 20 20 20 20 70 53 74 72 31 2c 20 30 29 3b        pStr1, 0);
39d5b 0d 0a 20 20 20 20 69 64 78 4e 65 77 31 20 3d 20  ..    idxNew1 = 
39d5c 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
39d5d 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 31  t(pWC, pNewExpr1
39d5e 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
39d5f 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a 20  ERM_DYNAMIC);.. 
39d60 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78     testcase( idx
39d61 4e 65 77 31 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  New1==0 );..    
39d62 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
39d63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 31 29 3b  , pWC, idxNew1);
39d64 0d 0a 20 20 20 20 70 4e 65 77 45 78 70 72 32 20  ..    pNewExpr2 
39d65 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
39d66 50 61 72 73 65 2c 20 54 4b 5f 4c 54 2c 0d 0a 20  Parse, TK_LT,.. 
39d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39d68 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
39d69 65 74 43 6f 6c 6c 28 73 71 6c 69 74 65 33 45 78  etColl(sqlite3Ex
39d6a 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30  prDup(db,pLeft,0
39d6b 29 2c 20 70 43 6f 6c 6c 29 2c 0d 0a 20 20 20 20  ), pColl),..    
39d6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39d6d 20 70 53 74 72 32 2c 20 30 29 3b 0d 0a 20 20 20   pStr2, 0);..   
39d6e 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72 65   idxNew2 = where
39d6f 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
39d70 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45 52  , pNewExpr2, TER
39d71 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
39d72 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 20 20 74 65  YNAMIC);..    te
39d73 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 32 3d  stcase( idxNew2=
39d74 3d 30 20 29 3b 0d 0a 20 20 20 20 65 78 70 72 41  =0 );..    exprA
39d75 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43  nalyze(pSrc, pWC
39d76 2c 20 69 64 78 4e 65 77 32 29 3b 0d 0a 20 20 20  , idxNew2);..   
39d77 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
39d78 5b 69 64 78 54 65 72 6d 5d 3b 0d 0a 20 20 20 20  [idxTerm];..    
39d79 69 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20 29  if( isComplete )
39d7a 7b 0d 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  {..      pWC->a[
39d7b 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74  idxNew1].iParent
39d7c 20 3d 20 69 64 78 54 65 72 6d 3b 0d 0a 20 20 20   = idxTerm;..   
39d7d 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77     pWC->a[idxNew
39d7e 32 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78  2].iParent = idx
39d7f 54 65 72 6d 3b 0d 0a 20 20 20 20 20 20 70 54 65  Term;..      pTe
39d80 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0d  rm->nChild = 2;.
39d81 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e  .    }..  }..#en
39d82 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
39d83 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
39d84 54 49 4f 4e 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64  TION */....#ifnd
39d85 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
39d86 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 2f  IRTUALTABLE..  /
39d87 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41 54 43 48  * Add a WO_MATCH
39d88 20 61 75 78 69 6c 69 61 72 79 20 74 65 72 6d 20   auxiliary term 
39d89 74 6f 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  to the constrain
39d8a 74 20 73 65 74 20 69 66 20 74 68 65 0d 0a 20 20  t set if the..  
39d8b 2a 2a 20 63 75 72 72 65 6e 74 20 65 78 70 72 65  ** current expre
39d8c 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
39d8d 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41  form:  column MA
39d8e 54 43 48 20 65 78 70 72 2e 0d 0a 20 20 2a 2a 20  TCH expr...  ** 
39d8f 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
39d90 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
39d91 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f  xBestIndex metho
39d92 64 73 20 6f 66 0d 0a 20 20 2a 2a 20 76 69 72 74  ds of..  ** virt
39d93 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54 68 65  ual tables.  The
39d94 20 6e 61 74 69 76 65 20 71 75 65 72 79 20 6f 70   native query op
39d95 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74  timizer does not
39d96 20 61 74 74 65 6d 70 74 0d 0a 20 20 2a 2a 20 74   attempt..  ** t
39d97 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69  o do anything wi
39d98 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69 6f  th MATCH functio
39d99 6e 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ns...  */..  if(
39d9a 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
39d9b 28 70 45 78 70 72 29 20 29 7b 0d 0a 20 20 20 20  (pExpr) ){..    
39d9c 69 6e 74 20 69 64 78 4e 65 77 3b 0d 0a 20 20 20  int idxNew;..   
39d9d 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a   Expr *pRight, *
39d9e 70 4c 65 66 74 3b 0d 0a 20 20 20 20 57 68 65 72  pLeft;..    Wher
39d9f 65 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b  eTerm *pNewTerm;
39da0 0d 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72  ..    Bitmask pr
39da1 65 72 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72  ereqColumn, prer
39da2 65 71 45 78 70 72 3b 0d 0a 0d 0a 20 20 20 20 70  eqExpr;....    p
39da3 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78  Right = pExpr->x
39da4 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  .pList->a[0].pEx
39da5 70 72 3b 0d 0a 20 20 20 20 70 4c 65 66 74 20 3d  pr;..    pLeft =
39da6 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
39da7 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0d 0a 20 20  >a[1].pExpr;..  
39da8 20 20 70 72 65 72 65 71 45 78 70 72 20 3d 20 65    prereqExpr = e
39da9 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
39daa 61 73 6b 53 65 74 2c 20 70 52 69 67 68 74 29 3b  askSet, pRight);
39dab 0d 0a 20 20 20 20 70 72 65 72 65 71 43 6f 6c 75  ..    prereqColu
39dac 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  mn = exprTableUs
39dad 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c  age(pMaskSet, pL
39dae 65 66 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 28  eft);..    if( (
39daf 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72 65  prereqExpr & pre
39db0 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b  reqColumn)==0 ){
39db1 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  ..      Expr *pN
39db2 65 77 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 70  ewExpr;..      p
39db3 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65  NewExpr = sqlite
39db4 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
39db5 4b 5f 4d 41 54 43 48 2c 20 0d 0a 20 20 20 20 20  K_MATCH, ..     
39db6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39db7 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69           0, sqli
39db8 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
39db9 52 69 67 68 74 2c 20 30 29 2c 20 30 29 3b 0d 0a  Right, 0), 0);..
39dba 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77        idxNew = w
39dbb 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
39dbc 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20  (pWC, pNewExpr, 
39dbd 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
39dbe 4d 5f 44 59 4e 41 4d 49 43 29 3b 0d 0a 20 20 20  M_DYNAMIC);..   
39dbf 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78     testcase( idx
39dc0 4e 65 77 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20  New==0 );..     
39dc1 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70 57 43   pNewTerm = &pWC
39dc2 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0d 0a 20 20  ->a[idxNew];..  
39dc3 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72      pNewTerm->pr
39dc4 65 72 65 71 52 69 67 68 74 20 3d 20 70 72 65 72  ereqRight = prer
39dc5 65 71 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 70  eqExpr;..      p
39dc6 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  NewTerm->leftCur
39dc7 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
39dc8 62 6c 65 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77  ble;..      pNew
39dc9 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
39dca 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c  mn = pLeft->iCol
39dcb 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 70 4e 65 77  umn;..      pNew
39dcc 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
39dcd 3d 20 57 4f 5f 4d 41 54 43 48 3b 0d 0a 20 20 20  = WO_MATCH;..   
39dce 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61     pNewTerm->iPa
39dcf 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0d  rent = idxTerm;.
39dd0 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26  .      pTerm = &
39dd1 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b  pWC->a[idxTerm];
39dd2 0d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e  ..      pTerm->n
39dd3 43 68 69 6c 64 20 3d 20 31 3b 0d 0a 20 20 20 20  Child = 1;..    
39dd4 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
39dd5 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b   |= TERM_COPIED;
39dd6 0d 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ..      pNewTerm
39dd7 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54  ->prereqAll = pT
39dd8 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0d  erm->prereqAll;.
39dd9 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e  .    }..  }..#en
39dda 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
39ddb 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
39ddc 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  */....#ifdef SQL
39ddd 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
39dde 0d 0a 20 20 2f 2a 20 57 68 65 6e 20 73 71 6c 69  ..  /* When sqli
39ddf 74 65 5f 73 74 61 74 33 20 68 69 73 74 6f 67 72  te_stat3 histogr
39de0 61 6d 20 64 61 74 61 20 69 73 20 61 76 61 69 6c  am data is avail
39de1 61 62 6c 65 20 61 6e 20 6f 70 65 72 61 74 6f 72  able an operator
39de2 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 66 6f   of the..  ** fo
39de3 72 6d 20 22 78 20 49 53 20 4e 4f 54 20 4e 55 4c  rm "x IS NOT NUL
39de4 4c 22 20 63 61 6e 20 73 6f 6d 65 74 69 6d 65 73  L" can sometimes
39de5 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 6d 6f   be evaluated mo
39de6 72 65 20 65 66 66 69 63 69 65 6e 74 6c 79 0d 0a  re efficiently..
39de7 20 20 2a 2a 20 61 73 20 22 78 3e 4e 55 4c 4c 22    ** as "x>NULL"
39de8 20 69 66 20 78 20 69 73 20 6e 6f 74 20 61 6e 20   if x is not an 
39de9 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
39dea 4b 45 59 2e 20 20 53 6f 20 63 6f 6e 73 74 72 75  KEY.  So constru
39deb 63 74 20 61 0d 0a 20 20 2a 2a 20 76 69 72 74 75  ct a..  ** virtu
39dec 61 6c 20 74 65 72 6d 20 6f 66 20 74 68 61 74 20  al term of that 
39ded 66 6f 72 6d 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  form...  **..  *
39dee 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
39def 76 69 72 74 75 61 6c 20 74 65 72 6d 20 6d 75 73  virtual term mus
39df0 74 20 62 65 20 74 61 67 67 65 64 20 77 69 74 68  t be tagged with
39df1 20 54 45 52 4d 5f 56 4e 55 4c 4c 2e 20 20 54 68   TERM_VNULL.  Th
39df2 69 73 0d 0a 20 20 2a 2a 20 54 45 52 4d 5f 56 4e  is..  ** TERM_VN
39df3 55 4c 4c 20 74 61 67 20 77 69 6c 6c 20 73 75 70  ULL tag will sup
39df4 70 72 65 73 73 20 74 68 65 20 6e 6f 74 2d 6e 75  press the not-nu
39df5 6c 6c 20 63 68 65 63 6b 20 61 74 20 74 68 65 20  ll check at the 
39df6 62 65 67 69 6e 6e 69 6e 67 0d 0a 20 20 2a 2a 20  beginning..  ** 
39df7 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 57 69  of the loop.  Wi
39df8 74 68 6f 75 74 20 74 68 65 20 54 45 52 4d 5f 56  thout the TERM_V
39df9 4e 55 4c 4c 20 66 6c 61 67 2c 20 74 68 65 20 6e  NULL flag, the n
39dfa 6f 74 2d 6e 75 6c 6c 20 63 68 65 63 6b 20 61 74  ot-null check at
39dfb 0d 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 72 74  ..  ** the start
39dfc 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 77 69 6c   of the loop wil
39dfd 6c 20 70 72 65 76 65 6e 74 20 61 6e 79 20 72 65  l prevent any re
39dfe 73 75 6c 74 73 20 66 72 6f 6d 20 62 65 69 6e 67  sults from being
39dff 20 72 65 74 75 72 6e 65 64 2e 0d 0a 20 20 2a 2f   returned...  */
39e00 0d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ..  if( pExpr->o
39e01 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 0d 0a 20  p==TK_NOTNULL.. 
39e02 20 20 26 26 20 70 45 78 70 72 2d 3e 70 4c 65 66    && pExpr->pLef
39e03 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  t->op==TK_COLUMN
39e04 0d 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e 70  ..   && pExpr->p
39e05 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  Left->iColumn>=0
39e06 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 45 78 70 72  ..  ){..    Expr
39e07 20 2a 70 4e 65 77 45 78 70 72 3b 0d 0a 20 20 20   *pNewExpr;..   
39e08 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70   Expr *pLeft = p
39e09 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20  Expr->pLeft;..  
39e0a 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0d 0a 20    int idxNew;.. 
39e0b 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e     WhereTerm *pN
39e0c 65 77 54 65 72 6d 3b 0d 0a 0d 0a 20 20 20 20 70  ewTerm;....    p
39e0d 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65  NewExpr = sqlite
39e0e 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
39e0f 4b 5f 47 54 2c 0d 0a 20 20 20 20 20 20 20 20 20  K_GT,..         
39e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39e11 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
39e12 70 28 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 2c  p(db, pLeft, 0),
39e13 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
39e14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
39e15 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
39e16 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30  e, TK_NULL, 0, 0
39e17 2c 20 30 29 2c 20 30 29 3b 0d 0a 0d 0a 20 20 20  , 0), 0);....   
39e18 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
39e19 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
39e1a 20 70 4e 65 77 45 78 70 72 2c 0d 0a 20 20 20 20   pNewExpr,..    
39e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39e1c 20 20 20 20 20 20 20 20 20 20 54 45 52 4d 5f 56            TERM_V
39e1d 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
39e1e 4d 49 43 7c 54 45 52 4d 5f 56 4e 55 4c 4c 29 3b  MIC|TERM_VNULL);
39e1f 0d 0a 20 20 20 20 69 66 28 20 69 64 78 4e 65 77  ..    if( idxNew
39e20 20 29 7b 0d 0a 20 20 20 20 20 20 70 4e 65 77 54   ){..      pNewT
39e21 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
39e22 78 4e 65 77 5d 3b 0d 0a 20 20 20 20 20 20 70 4e  xNew];..      pN
39e23 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  ewTerm->prereqRi
39e24 67 68 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ght = 0;..      
39e25 70 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75  pNewTerm->leftCu
39e26 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54  rsor = pLeft->iT
39e27 61 62 6c 65 3b 0d 0a 20 20 20 20 20 20 70 4e 65  able;..      pNe
39e28 77 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  wTerm->u.leftCol
39e29 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
39e2a 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 70 4e 65  lumn;..      pNe
39e2b 77 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  wTerm->eOperator
39e2c 20 3d 20 57 4f 5f 47 54 3b 0d 0a 20 20 20 20 20   = WO_GT;..     
39e2d 20 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61 72 65   pNewTerm->iPare
39e2e 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0d 0a 20  nt = idxTerm;.. 
39e2f 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
39e30 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0d 0a  C->a[idxTerm];..
39e31 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
39e32 69 6c 64 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  ild = 1;..      
39e33 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c  pTerm->wtFlags |
39e34 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0d 0a  = TERM_COPIED;..
39e35 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
39e36 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 72  prereqAll = pTer
39e37 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0d 0a 20  m->prereqAll;.. 
39e38 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69     }..  }..#endi
39e39 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
39e3a 4c 45 5f 53 54 41 54 20 2a 2f 0d 0a 0d 0a 20 20  LE_STAT */....  
39e3b 2f 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20 63 6c  /* Prevent ON cl
39e3c 61 75 73 65 20 74 65 72 6d 73 20 6f 66 20 61 20  ause terms of a 
39e3d 4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d 20 62  LEFT JOIN from b
39e3e 65 69 6e 67 20 75 73 65 64 20 74 6f 20 64 72 69  eing used to dri
39e3f 76 65 0d 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65  ve..  ** an inde
39e40 78 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  x for tables to 
39e41 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20  the left of the 
39e42 6a 6f 69 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70  join...  */..  p
39e43 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
39e44 74 20 7c 3d 20 65 78 74 72 61 52 69 67 68 74 3b  t |= extraRight;
39e45 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
39e46 74 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79  turn TRUE if any
39e47 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
39e48 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b  ons in pList->a[
39e49 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61  iFirst...] conta
39e4a 69 6e 0d 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e  in..** a referen
39e4b 63 65 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20  ce to any table 
39e4c 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 69  other than the i
39e4d 42 61 73 65 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  Base table...*/.
39e4e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 66 65  .static int refe
39e4f 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
39e50 73 28 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  s(..  ExprList *
39e51 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20  pList,          
39e52 2f 2a 20 53 65 61 72 63 68 20 65 78 70 72 65 73  /* Search expres
39e53 73 69 6f 6e 73 20 69 6e 20 74 68 73 20 6c 69 73  sions in ths lis
39e54 74 20 2a 2f 0d 0a 20 20 57 68 65 72 65 4d 61 73  t */..  WhereMas
39e55 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
39e56 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
39e57 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d  m tables to bitm
39e58 61 70 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 46  aps */..  int iF
39e59 69 72 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  irst,           
39e5a 20 20 20 20 2f 2a 20 42 65 20 73 65 61 72 63 68      /* Be search
39e5b 69 6e 67 20 77 69 74 68 20 74 68 65 20 69 46 69  ing with the iFi
39e5c 72 73 74 2d 74 68 20 65 78 70 72 65 73 73 69 6f  rst-th expressio
39e5d 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 42 61 73  n */..  int iBas
39e5e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
39e5f 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 65 66 65    /* Ignore refe
39e60 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 74  rences to this t
39e61 61 62 6c 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 42  able */..){..  B
39e62 69 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64 20 3d  itmask allowed =
39e63 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53   ~getMask(pMaskS
39e64 65 74 2c 20 69 42 61 73 65 29 3b 0d 0a 20 20 77  et, iBase);..  w
39e65 68 69 6c 65 28 20 69 46 69 72 73 74 3c 70 4c 69  hile( iFirst<pLi
39e66 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0d 0a 20 20  st->nExpr ){..  
39e67 20 20 69 66 28 20 28 65 78 70 72 54 61 62 6c 65    if( (exprTable
39e68 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
39e69 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b  pList->a[iFirst+
39e6a 2b 5d 2e 70 45 78 70 72 29 26 61 6c 6c 6f 77 65  +].pExpr)&allowe
39e6b 64 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  d)!=0 ){..      
39e6c 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d  return 1;..    }
39e6d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
39e6e 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  0;..}..../*..** 
39e6f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  This function se
39e70 61 72 63 68 65 73 20 74 68 65 20 65 78 70 72 65  arches the expre
39e71 73 73 69 6f 6e 20 6c 69 73 74 20 70 61 73 73 65  ssion list passe
39e72 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
39e73 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 66 6f 72  argument..** for
39e74 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
39e75 66 20 74 79 70 65 20 54 4b 5f 43 4f 4c 55 4d 4e  f type TK_COLUMN
39e76 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20   that refers to 
39e77 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20  the same column 
39e78 61 6e 64 0d 0a 2a 2a 20 75 73 65 73 20 74 68 65  and..** uses the
39e79 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6f 6e 20   same collation 
39e7a 73 65 71 75 65 6e 63 65 20 61 73 20 74 68 65 20  sequence as the 
39e7b 69 43 6f 6c 27 74 68 20 63 6f 6c 75 6d 6e 20 6f  iCol'th column o
39e7c 66 20 69 6e 64 65 78 20 70 49 64 78 2e 0d 0a 2a  f index pIdx...*
39e7d 2a 20 41 72 67 75 6d 65 6e 74 20 69 42 61 73 65  * Argument iBase
39e7e 20 69 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e   is the cursor n
39e7f 75 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20 74  umber used for t
39e80 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 70 49  he table that pI
39e81 64 78 20 72 65 66 65 72 73 0d 0a 2a 2a 20 74 6f  dx refers..** to
39e82 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63  ...**..** If suc
39e83 68 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  h an expression 
39e84 69 73 20 66 6f 75 6e 64 2c 20 69 74 73 20 69 6e  is found, its in
39e85 64 65 78 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b  dex in pList->a[
39e86 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  ] is returned. I
39e87 66 0d 0a 2a 2a 20 6e 6f 20 65 78 70 72 65 73 73  f..** no express
39e88 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 2d 31  ion is found, -1
39e89 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   is returned...*
39e8a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  /..static int fi
39e8b 6e 64 49 6e 64 65 78 43 6f 6c 28 0d 0a 20 20 50  ndIndexCol(..  P
39e8c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
39e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39e8e 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
39e8f 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  */..  ExprList *
39e90 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20  pList,          
39e91 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
39e92 69 6f 6e 20 6c 69 73 74 20 74 6f 20 73 65 61 72  ion list to sear
39e93 63 68 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 42 61  ch */..  int iBa
39e94 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
39e95 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
39e96 6f 72 20 66 6f 72 20 74 61 62 6c 65 20 61 73 73  or for table ass
39e97 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49 64  ociated with pId
39e98 78 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a 70  x */..  Index *p
39e99 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
39e9a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
39e9b 20 74 6f 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e   to match column
39e9c 20 6f 66 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43   of */..  int iC
39e9d 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ol              
39e9e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
39e9f 75 6d 6e 20 6f 66 20 69 6e 64 65 78 20 74 6f 20  umn of index to 
39ea0 6d 61 74 63 68 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  match */..){..  
39ea1 69 6e 74 20 69 3b 0d 0a 20 20 63 6f 6e 73 74 20  int i;..  const 
39ea2 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49  char *zColl = pI
39ea3 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 43 6f 6c 5d  dx->azColl[iCol]
39ea4 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ;....  for(i=0; 
39ea5 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
39ea6 69 2b 2b 29 7b 0d 0a 20 20 20 20 45 78 70 72 20  i++){..    Expr 
39ea7 2a 70 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  *p = pList->a[i]
39ea8 2e 70 45 78 70 72 3b 0d 0a 20 20 20 20 69 66 28  .pExpr;..    if(
39ea9 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   p->op==TK_COLUM
39eaa 4e 0d 0a 20 20 20 20 20 26 26 20 70 2d 3e 69 43  N..     && p->iC
39eab 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43  olumn==pIdx->aiC
39eac 6f 6c 75 6d 6e 5b 69 43 6f 6c 5d 0d 0a 20 20 20  olumn[iCol]..   
39ead 20 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d    && p->iTable==
39eae 69 42 61 73 65 0d 0a 20 20 20 20 29 7b 0d 0a 20  iBase..    ){.. 
39eaf 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
39eb0 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
39eb1 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
39eb2 20 70 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20   p);..      if( 
39eb3 41 4c 57 41 59 53 28 70 43 6f 6c 6c 29 20 26 26  ALWAYS(pColl) &&
39eb4 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
39eb5 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  mp(pColl->zName,
39eb6 20 7a 43 6f 6c 6c 29 20 29 7b 0d 0a 20 20 20 20   zColl) ){..    
39eb7 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a 20      return i;.. 
39eb8 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
39eb9 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 2d   }....  return -
39eba 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  1;..}..../*..** 
39ebb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 74  This routine det
39ebc 65 72 6d 69 6e 65 73 20 69 66 20 70 49 64 78 20  ermines if pIdx 
39ebd 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61  can be used to a
39ebe 73 73 69 73 74 20 69 6e 20 70 72 6f 63 65 73 73  ssist in process
39ebf 69 6e 67 20 61 0d 0a 2a 2a 20 44 49 53 54 49 4e  ing a..** DISTIN
39ec0 43 54 20 71 75 61 6c 69 66 69 65 72 2e 20 49 6e  CT qualifier. In
39ec1 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74   other words, it
39ec2 20 74 65 73 74 73 20 77 68 65 74 68 65 72 20 6f   tests whether o
39ec3 72 20 6e 6f 74 20 75 73 69 6e 67 20 74 68 69 73  r not using this
39ec4 0d 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74  ..** index for t
39ec5 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70 20 67 75  he outer loop gu
39ec6 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 72 6f  arantees that ro
39ec7 77 73 20 77 69 74 68 20 65 71 75 61 6c 20 76 61  ws with equal va
39ec8 6c 75 65 73 20 66 6f 72 0d 0a 2a 2a 20 61 6c 6c  lues for..** all
39ec9 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
39eca 74 68 65 20 70 44 69 73 74 69 6e 63 74 20 6c 69  the pDistinct li
39ecb 73 74 20 61 72 65 20 64 65 6c 69 76 65 72 65 64  st are delivered
39ecc 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65   grouped togethe
39ecd 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f 72 20 65  r...**..** For e
39ece 78 61 6d 70 6c 65 2c 20 74 68 65 20 71 75 65 72  xample, the quer
39ecf 79 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c  y ..**..**   SEL
39ed0 45 43 54 20 44 49 53 54 49 4e 43 54 20 61 2c 20  ECT DISTINCT a, 
39ed1 62 2c 20 63 20 46 52 4f 4d 20 74 62 6c 20 57 48  b, c FROM tbl WH
39ed2 45 52 45 20 61 20 3d 20 3f 0d 0a 2a 2a 0d 0a 2a  ERE a = ?..**..*
39ed3 2a 20 63 61 6e 20 62 65 6e 65 66 69 74 20 66 72  * can benefit fr
39ed4 6f 6d 20 61 6e 79 20 69 6e 64 65 78 20 6f 6e 20  om any index on 
39ed5 63 6f 6c 75 6d 6e 73 20 22 62 22 20 61 6e 64 20  columns "b" and 
39ed6 22 63 22 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  "c"...*/..static
39ed7 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74 49   int isDistinctI
39ed8 6e 64 65 78 28 0d 0a 20 20 50 61 72 73 65 20 2a  ndex(..  Parse *
39ed9 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
39eda 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
39edb 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  ing context */..
39edc 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
39edd 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  WC,             
39ede 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
39edf 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 49 6e 64 65  lause */..  Inde
39ee0 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20  x *pIdx,        
39ee1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
39ee2 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 63  he index being c
39ee3 6f 6e 73 69 64 65 72 65 64 20 2a 2f 0d 0a 20 20  onsidered */..  
39ee4 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20 20  int base,       
39ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39ee6 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
39ee7 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 70   for the table p
39ee8 49 64 78 20 69 73 20 6f 6e 20 2a 2f 0d 0a 20 20  Idx is on */..  
39ee9 45 78 70 72 4c 69 73 74 20 2a 70 44 69 73 74 69  ExprList *pDisti
39eea 6e 63 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  nct,            
39eeb 2f 2a 20 54 68 65 20 44 49 53 54 49 4e 43 54 20  /* The DISTINCT 
39eec 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0d 0a  expressions */..
39eed 20 20 69 6e 74 20 6e 45 71 43 6f 6c 20 20 20 20    int nEqCol    
39eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39eef 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
39ef0 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74  ndex columns wit
39ef1 68 20 3d 3d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 42  h == */..){..  B
39ef2 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b  itmask mask = 0;
39ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39ef4 2a 20 4d 61 73 6b 20 6f 66 20 75 6e 61 63 63 6f  * Mask of unacco
39ef5 75 6e 74 65 64 20 66 6f 72 20 70 44 69 73 74 69  unted for pDisti
39ef6 6e 63 74 20 65 78 70 72 73 20 2a 2f 0d 0a 20 20  nct exprs */..  
39ef7 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
39ef8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39ef9 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
39efa 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28  able */....  if(
39efb 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 3d 3d 30 20   pIdx->zName==0 
39efc 7c 7c 20 70 44 69 73 74 69 6e 63 74 3d 3d 30 20  || pDistinct==0 
39efd 7c 7c 20 70 44 69 73 74 69 6e 63 74 2d 3e 6e 45  || pDistinct->nE
39efe 78 70 72 3e 3d 42 4d 53 20 29 20 72 65 74 75 72  xpr>=BMS ) retur
39eff 6e 20 30 3b 0d 0a 20 20 74 65 73 74 63 61 73 65  n 0;..  testcase
39f00 28 20 70 44 69 73 74 69 6e 63 74 2d 3e 6e 45 78  ( pDistinct->nEx
39f01 70 72 3d 3d 42 4d 53 2d 31 20 29 3b 0d 0a 0d 0a  pr==BMS-1 );....
39f02 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    /* Loop throug
39f03 68 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73  h all the expres
39f04 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 64 69 73  sions in the dis
39f05 74 69 6e 63 74 20 6c 69 73 74 2e 20 49 66 20 61  tinct list. If a
39f06 6e 79 20 6f 66 20 74 68 65 6d 0d 0a 20 20 2a 2a  ny of them..  **
39f07 20 61 72 65 20 6e 6f 74 20 73 69 6d 70 6c 65 20   are not simple 
39f08 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
39f09 73 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e  s, return early.
39f0a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 65 73 74   Otherwise, test
39f0b 20 69 66 20 74 68 65 0d 0a 20 20 2a 2a 20 57 48   if the..  ** WH
39f0c 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 74 61  ERE clause conta
39f0d 69 6e 73 20 61 20 22 63 6f 6c 3d 58 22 20 63 6c  ins a "col=X" cl
39f0e 61 75 73 65 2e 20 49 66 20 69 74 20 64 6f 65 73  ause. If it does
39f0f 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  , the expression
39f10 0d 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 69 67  ..  ** can be ig
39f11 6e 6f 72 65 64 2e 20 49 66 20 69 74 20 64 6f 65  nored. If it doe
39f12 73 20 6e 6f 74 2c 20 61 6e 64 20 74 68 65 20 63  s not, and the c
39f13 6f 6c 75 6d 6e 20 64 6f 65 73 20 6e 6f 74 20 62  olumn does not b
39f14 65 6c 6f 6e 67 20 74 6f 20 74 68 65 0d 0a 20 20  elong to the..  
39f15 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 61 73  ** same table as
39f16 20 69 6e 64 65 78 20 70 49 64 78 2c 20 72 65 74   index pIdx, ret
39f17 75 72 6e 20 65 61 72 6c 79 2e 20 46 69 6e 61 6c  urn early. Final
39f18 6c 79 2c 20 69 66 20 74 68 65 72 65 20 69 73 20  ly, if there is 
39f19 6e 6f 0d 0a 20 20 2a 2a 20 6d 61 74 63 68 69 6e  no..  ** matchin
39f1a 67 20 22 63 6f 6c 3d 58 22 20 65 78 70 72 65 73  g "col=X" expres
39f1b 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 63 6f 6c  sion and the col
39f1c 75 6d 6e 20 69 73 20 6f 6e 20 74 68 65 20 73 61  umn is on the sa
39f1d 6d 65 20 74 61 62 6c 65 20 61 73 20 70 49 64 78  me table as pIdx
39f1e 2c 0d 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20  ,..  ** set the 
39f1f 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
39f20 74 20 69 6e 20 76 61 72 69 61 62 6c 65 20 6d 61  t in variable ma
39f21 73 6b 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72  sk...  */..  for
39f22 28 69 3d 30 3b 20 69 3c 70 44 69 73 74 69 6e 63  (i=0; i<pDistinc
39f23 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0d  t->nExpr; i++){.
39f24 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a  .    WhereTerm *
39f25 70 54 65 72 6d 3b 0d 0a 20 20 20 20 45 78 70 72  pTerm;..    Expr
39f26 20 2a 70 20 3d 20 70 44 69 73 74 69 6e 63 74 2d   *p = pDistinct-
39f27 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0d 0a 20 20  >a[i].pExpr;..  
39f28 20 20 69 66 28 20 70 2d 3e 6f 70 21 3d 54 4b 5f    if( p->op!=TK_
39f29 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20  COLUMN ) return 
39f2a 30 3b 0d 0a 20 20 20 20 70 54 65 72 6d 20 3d 20  0;..    pTerm = 
39f2b 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 70 2d  findTerm(pWC, p-
39f2c 3e 69 54 61 62 6c 65 2c 20 70 2d 3e 69 43 6f 6c  >iTable, p->iCol
39f2d 75 6d 6e 2c 20 7e 28 42 69 74 6d 61 73 6b 29 30  umn, ~(Bitmask)0
39f2e 2c 20 57 4f 5f 45 51 2c 20 30 29 3b 0d 0a 20 20  , WO_EQ, 0);..  
39f2f 20 20 69 66 28 20 70 54 65 72 6d 20 29 7b 0d 0a    if( pTerm ){..
39f30 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d        Expr *pX =
39f31 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0d 0a   pTerm->pExpr;..
39f32 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
39f33 31 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72  1 = sqlite3Binar
39f34 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
39f35 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66  pParse, pX->pLef
39f36 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29 3b 0d  t, pX->pRight);.
39f37 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
39f38 70 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  p2 = sqlite3Expr
39f39 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
39f3a 70 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  p);..      if( p
39f3b 31 3d 3d 70 32 20 29 20 63 6f 6e 74 69 6e 75 65  1==p2 ) continue
39f3c 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
39f3d 28 20 70 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73  ( p->iTable!=bas
39f3e 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20  e ) return 0;.. 
39f3f 20 20 20 6d 61 73 6b 20 7c 3d 20 28 28 28 42 69     mask |= (((Bi
39f40 74 6d 61 73 6b 29 31 29 20 3c 3c 20 69 29 3b 0d  tmask)1) << i);.
39f41 0a 20 20 7d 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d  .  }....  for(i=
39f42 6e 45 71 43 6f 6c 3b 20 6d 61 73 6b 20 26 26 20  nEqCol; mask && 
39f43 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
39f44 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20   i++){..    int 
39f45 69 45 78 70 72 20 3d 20 66 69 6e 64 49 6e 64 65  iExpr = findInde
39f46 78 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 44 69  xCol(pParse, pDi
39f47 73 74 69 6e 63 74 2c 20 62 61 73 65 2c 20 70 49  stinct, base, pI
39f48 64 78 2c 20 69 29 3b 0d 0a 20 20 20 20 69 66 28  dx, i);..    if(
39f49 20 69 45 78 70 72 3c 30 20 29 20 62 72 65 61 6b   iExpr<0 ) break
39f4a 3b 0d 0a 20 20 20 20 6d 61 73 6b 20 26 3d 20 7e  ;..    mask &= ~
39f4b 28 28 28 42 69 74 6d 61 73 6b 29 31 29 20 3c 3c  (((Bitmask)1) <<
39f4c 20 69 45 78 70 72 29 3b 0d 0a 20 20 7d 0d 0a 0d   iExpr);..  }...
39f4d 0a 20 20 72 65 74 75 72 6e 20 28 6d 61 73 6b 3d  .  return (mask=
39f4e 3d 30 29 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d  =0);..}....../*.
39f4f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
39f50 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
39f51 65 78 70 72 65 73 73 69 6f 6e 2d 6c 69 73 74 20  expression-list 
39f52 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 68  passed as the th
39f53 69 72 64 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a  ird argument..**
39f54 20 69 73 20 72 65 64 75 6e 64 61 6e 74 2e 20 41   is redundant. A
39f55 20 44 49 53 54 49 4e 43 54 20 6c 69 73 74 20 69   DISTINCT list i
39f56 73 20 72 65 64 75 6e 64 61 6e 74 20 69 66 20 74  s redundant if t
39f57 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
39f58 61 69 6e 73 20 61 0d 0a 2a 2a 20 55 4e 49 51 55  ains a..** UNIQU
39f59 45 20 69 6e 64 65 78 20 74 68 61 74 20 67 75 61  E index that gua
39f5a 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65  rantees that the
39f5b 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 71   result of the q
39f5c 75 65 72 79 20 77 69 6c 6c 20 62 65 20 64 69 73  uery will be dis
39f5d 74 69 6e 63 74 0d 0a 2a 2a 20 61 6e 79 77 61 79  tinct..** anyway
39f5e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
39f5f 74 20 69 73 44 69 73 74 69 6e 63 74 52 65 64 75  t isDistinctRedu
39f60 6e 64 61 6e 74 28 0d 0a 20 20 50 61 72 73 65 20  ndant(..  Parse 
39f61 2a 70 50 61 72 73 65 2c 0d 0a 20 20 53 72 63 4c  *pParse,..  SrcL
39f62 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 0d 0a  ist *pTabList,..
39f63 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
39f64 57 43 2c 0d 0a 20 20 45 78 70 72 4c 69 73 74 20  WC,..  ExprList 
39f65 2a 70 44 69 73 74 69 6e 63 74 0d 0a 29 7b 0d 0a  *pDistinct..){..
39f66 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0d 0a    Table *pTab;..
39f67 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0d 0a    Index *pIdx;..
39f68 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
39f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f6a 20 20 0d 0a 20 20 69 6e 74 20 69 42 61 73 65 3b    ..  int iBase;
39f6b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  ....  /* If ther
39f6c 65 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f  e is more than o
39f6d 6e 65 20 74 61 62 6c 65 20 6f 72 20 73 75 62 2d  ne table or sub-
39f6e 73 65 6c 65 63 74 20 69 6e 20 74 68 65 20 46 52  select in the FR
39f6f 4f 4d 20 63 6c 61 75 73 65 20 6f 66 0d 0a 20 20  OM clause of..  
39f70 2a 2a 20 74 68 69 73 20 71 75 65 72 79 2c 20 74  ** this query, t
39f71 68 65 6e 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20  hen it will not 
39f72 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 73  be possible to s
39f73 68 6f 77 20 74 68 61 74 20 74 68 65 20 44 49 53  how that the DIS
39f74 54 49 4e 43 54 20 0d 0a 20 20 2a 2a 20 63 6c 61  TINCT ..  ** cla
39f75 75 73 65 20 69 73 20 72 65 64 75 6e 64 61 6e 74  use is redundant
39f76 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54 61 62  . */..  if( pTab
39f77 4c 69 73 74 2d 3e 6e 53 72 63 21 3d 31 20 29 20  List->nSrc!=1 ) 
39f78 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 69 42 61  return 0;..  iBa
39f79 73 65 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  se = pTabList->a
39f7a 5b 30 5d 2e 69 43 75 72 73 6f 72 3b 0d 0a 20 20  [0].iCursor;..  
39f7b 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
39f7c 3e 61 5b 30 5d 2e 70 54 61 62 3b 0d 0a 0d 0a 20  >a[0].pTab;.... 
39f7d 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68   /* If any of th
39f7e 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 73  e expressions is
39f7f 20 61 6e 20 49 50 4b 20 63 6f 6c 75 6d 6e 20 6f   an IPK column o
39f80 6e 20 74 61 62 6c 65 20 69 42 61 73 65 2c 20 74  n table iBase, t
39f81 68 65 6e 20 72 65 74 75 72 6e 20 0d 0a 20 20 2a  hen return ..  *
39f82 2a 20 74 72 75 65 2e 20 4e 6f 74 65 3a 20 54 68  * true. Note: Th
39f83 65 20 28 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 42  e (p->iTable==iB
39f84 61 73 65 29 20 70 61 72 74 20 6f 66 20 74 68 69  ase) part of thi
39f85 73 20 74 65 73 74 20 6d 61 79 20 62 65 20 66 61  s test may be fa
39f86 6c 73 65 20 69 66 20 74 68 65 0d 0a 20 20 2a 2a  lse if the..  **
39f87 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20   current SELECT 
39f88 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20  is a correlated 
39f89 73 75 62 2d 71 75 65 72 79 2e 0d 0a 20 20 2a 2f  sub-query...  */
39f8a 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
39f8b 44 69 73 74 69 6e 63 74 2d 3e 6e 45 78 70 72 3b  Distinct->nExpr;
39f8c 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 45 78 70 72   i++){..    Expr
39f8d 20 2a 70 20 3d 20 70 44 69 73 74 69 6e 63 74 2d   *p = pDistinct-
39f8e 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0d 0a 20 20  >a[i].pExpr;..  
39f8f 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f    if( p->op==TK_
39f90 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e 69 54 61  COLUMN && p->iTa
39f91 62 6c 65 3d 3d 69 42 61 73 65 20 26 26 20 70 2d  ble==iBase && p-
39f92 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 20 72 65 74  >iColumn<0 ) ret
39f93 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  urn 1;..  }.... 
39f94 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
39f95 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 6e 20   all indices on 
39f96 74 68 65 20 74 61 62 6c 65 2c 20 63 68 65 63 6b  the table, check
39f97 69 6e 67 20 65 61 63 68 20 74 6f 20 73 65 65 20  ing each to see 
39f98 69 66 20 69 74 20 6d 61 6b 65 73 0d 0a 20 20 2a  if it makes..  *
39f99 2a 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 71  * the DISTINCT q
39f9a 75 61 6c 69 66 69 65 72 20 72 65 64 75 6e 64 61  ualifier redunda
39f9b 6e 74 2e 20 49 74 20 64 6f 65 73 20 73 6f 20 69  nt. It does so i
39f9c 66 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20  f:..  **..  **  
39f9d 20 31 2e 20 54 68 65 20 69 6e 64 65 78 20 69 73   1. The index is
39f9e 20 69 74 73 65 6c 66 20 55 4e 49 51 55 45 2c 20   itself UNIQUE, 
39f9f 61 6e 64 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  and..  **..  ** 
39fa0 20 20 32 2e 20 41 6c 6c 20 6f 66 20 74 68 65 20    2. All of the 
39fa1 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69  columns in the i
39fa2 6e 64 65 78 20 61 72 65 20 65 69 74 68 65 72 20  ndex are either 
39fa3 70 61 72 74 20 6f 66 20 74 68 65 20 70 44 69 73  part of the pDis
39fa4 74 69 6e 63 74 0d 0a 20 20 2a 2a 20 20 20 20 20  tinct..  **     
39fa5 20 6c 69 73 74 2c 20 6f 72 20 65 6c 73 65 20 74   list, or else t
39fa6 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
39fa7 63 6f 6e 74 61 69 6e 73 20 61 20 74 65 72 6d 20  contains a term 
39fa8 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 63 6f 6c  of the form "col
39fa9 3d 58 22 2c 0d 0a 20 20 2a 2a 20 20 20 20 20 20  =X",..  **      
39faa 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f 6e  where X is a con
39fab 73 74 61 6e 74 20 76 61 6c 75 65 2e 20 54 68 65  stant value. The
39fac 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
39fad 6e 63 65 73 20 6f 66 20 74 68 65 0d 0a 20 20 2a  nces of the..  *
39fae 2a 20 20 20 20 20 20 63 6f 6d 70 61 72 69 73 6f  *      compariso
39faf 6e 20 61 6e 64 20 73 65 6c 65 63 74 2d 6c 69 73  n and select-lis
39fb0 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75  t expressions mu
39fb1 73 74 20 6d 61 74 63 68 20 74 68 6f 73 65 20 6f  st match those o
39fb2 66 20 74 68 65 20 69 6e 64 65 78 2e 0d 0a 20 20  f the index...  
39fb3 2a 2f 0d 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  */..  for(pIdx=p
39fb4 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
39fb5 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
39fb6 65 78 74 29 7b 0d 0a 20 20 20 20 69 66 28 20 70  ext){..    if( p
39fb7 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  Idx->onError==OE
39fb8 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65  _None ) continue
39fb9 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
39fba 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
39fbb 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 6e   i++){..      in
39fbc 74 20 69 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 61  t iCol = pIdx->a
39fbd 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0d 0a 20 20 20  iColumn[i];..   
39fbe 20 20 20 69 66 28 20 30 3d 3d 66 69 6e 64 54 65     if( 0==findTe
39fbf 72 6d 28 70 57 43 2c 20 69 42 61 73 65 2c 20 69  rm(pWC, iBase, i
39fc0 43 6f 6c 2c 20 7e 28 42 69 74 6d 61 73 6b 29 30  Col, ~(Bitmask)0
39fc1 2c 20 57 4f 5f 45 51 2c 20 70 49 64 78 29 20 0d  , WO_EQ, pIdx) .
39fc2 0a 20 20 20 20 20 20 20 26 26 20 30 3e 66 69 6e  .       && 0>fin
39fc3 64 49 6e 64 65 78 43 6f 6c 28 70 50 61 72 73 65  dIndexCol(pParse
39fc4 2c 20 70 44 69 73 74 69 6e 63 74 2c 20 69 42 61  , pDistinct, iBa
39fc5 73 65 2c 20 70 49 64 78 2c 20 69 29 0d 0a 20 20  se, pIdx, i)..  
39fc6 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20      ){..        
39fc7 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d  break;..      }.
39fc8 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
39fc9 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  i==pIdx->nColumn
39fca 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68   ){..      /* Th
39fcb 69 73 20 69 6e 64 65 78 20 69 6d 70 6c 69 65 73  is index implies
39fcc 20 74 68 61 74 20 74 68 65 20 44 49 53 54 49 4e   that the DISTIN
39fcd 43 54 20 71 75 61 6c 69 66 69 65 72 20 69 73 20  CT qualifier is 
39fce 72 65 64 75 6e 64 61 6e 74 2e 20 2a 2f 0d 0a 20  redundant. */.. 
39fcf 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a       return 1;..
39fd0 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
39fd1 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a  return 0;..}....
39fd2 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
39fd3 69 6e 65 20 64 65 63 69 64 65 73 20 69 66 20 70  ine decides if p
39fd4 49 64 78 20 63 61 6e 20 62 65 20 75 73 65 64 20  Idx can be used 
39fd5 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  to satisfy the O
39fd6 52 44 45 52 20 42 59 0d 0a 2a 2a 20 63 6c 61 75  RDER BY..** clau
39fd7 73 65 2e 20 20 49 66 20 69 74 20 63 61 6e 2c 20  se.  If it can, 
39fd8 69 74 20 72 65 74 75 72 6e 73 20 31 2e 20 20 49  it returns 1.  I
39fd9 66 20 70 49 64 78 20 63 61 6e 6e 6f 74 20 73 61  f pIdx cannot sa
39fda 74 69 73 66 79 20 74 68 65 0d 0a 2a 2a 20 4f 52  tisfy the..** OR
39fdb 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
39fdc 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
39fdd 72 6e 73 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 70  rns 0...**..** p
39fde 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52  OrderBy is an OR
39fdf 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 72  DER BY clause fr
39fe0 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  om a SELECT stat
39fe1 65 6d 65 6e 74 2e 20 20 70 54 61 62 20 69 73 20  ement.  pTab is 
39fe2 74 68 65 0d 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73  the..** left-mos
39fe3 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  t table in the F
39fe4 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
39fe5 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54 20 73  at same SELECT s
39fe6 74 61 74 65 6d 65 6e 74 20 61 6e 64 0d 0a 2a 2a  tatement and..**
39fe7 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
39fe8 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   cursor number o
39fe9 66 20 22 62 61 73 65 22 2e 20 20 70 49 64 78 20  f "base".  pIdx 
39fea 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70  is an index on p
39feb 54 61 62 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 6e 45 71  Tab...**..** nEq
39fec 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65  Col is the numbe
39fed 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
39fee 70 49 64 78 20 74 68 61 74 20 61 72 65 20 75 73  pIdx that are us
39fef 65 64 20 61 73 20 65 71 75 61 6c 69 74 79 0d 0a  ed as equality..
39ff0 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ** constraints. 
39ff1 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 63 6f   Any of these co
39ff2 6c 75 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69 73  lumns may be mis
39ff3 73 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f 52  sing from the OR
39ff4 44 45 52 20 42 59 0d 0a 2a 2a 20 63 6c 61 75 73  DER BY..** claus
39ff5 65 20 61 6e 64 20 74 68 65 20 6d 61 74 63 68 20  e and the match 
39ff6 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 61 20 73  can still be a s
39ff7 75 63 63 65 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  uccess...**..** 
39ff8 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  All terms of the
39ff9 20 4f 52 44 45 52 20 42 59 20 74 68 61 74 20 6d   ORDER BY that m
39ffa 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65  atch against the
39ffb 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 65   index must be e
39ffc 69 74 68 65 72 0d 0a 2a 2a 20 41 53 43 20 6f 72  ither..** ASC or
39ffd 20 44 45 53 43 2e 20 20 28 54 65 72 6d 73 20 6f   DESC.  (Terms o
39ffe 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
39fff 6c 61 75 73 65 20 70 61 73 74 20 74 68 65 20 65  lause past the e
3a000 6e 64 20 6f 66 20 61 20 55 4e 49 51 55 45 0d 0a  nd of a UNIQUE..
3a001 2a 2a 20 69 6e 64 65 78 20 64 6f 20 6e 6f 74 20  ** index do not 
3a002 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66 79 20  need to satisfy 
3a003 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e  this constraint.
3a004 29 20 20 54 68 65 20 2a 70 62 52 65 76 20 76 61  )  The *pbRev va
3a005 6c 75 65 20 69 73 0d 0a 2a 2a 20 73 65 74 20 74  lue is..** set t
3a006 6f 20 31 20 69 66 20 74 68 65 20 4f 52 44 45 52  o 1 if the ORDER
3a007 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c   BY clause is al
3a008 6c 20 44 45 53 43 20 61 6e 64 20 69 74 20 69 73  l DESC and it is
3a009 20 73 65 74 20 74 6f 20 30 20 69 66 0d 0a 2a 2a   set to 0 if..**
3a00a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
3a00b 61 75 73 65 20 69 73 20 61 6c 6c 20 41 53 43 2e  ause is all ASC.
3a00c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
3a00d 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28   isSortingIndex(
3a00e 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ..  Parse *pPars
3a00f 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
3a010 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
3a011 2f 0d 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  /..  WhereMaskSe
3a012 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 2f 2a 20  t *pMaskSet, /* 
3a013 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
3a014 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  le cursor number
3a015 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0d  s to bitmaps */.
3a016 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
3a017 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3a018 65 20 69 6e 64 65 78 20 77 65 20 61 72 65 20 74  e index we are t
3a019 65 73 74 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74  esting */..  int
3a01a 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20   base,          
3a01b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
3a01c 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
3a01d 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64  ble to be sorted
3a01e 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20   */..  ExprList 
3a01f 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f  *pOrderBy,     /
3a020 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
3a021 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  lause */..  int 
3a022 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  nEqCol,         
3a023 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3a024 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77   index columns w
3a025 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e  ith == constrain
3a026 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 77 73 46  ts */..  int wsF
3a027 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
3a028 20 2f 2a 20 49 6e 64 65 78 20 75 73 61 67 65 73   /* Index usages
3a029 20 66 6c 61 67 73 20 2a 2f 0d 0a 20 20 69 6e 74   flags */..  int
3a02a 20 2a 70 62 52 65 76 20 20 20 20 20 20 20 20 20   *pbRev         
3a02b 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31       /* Set to 1
3a02c 20 69 66 20 4f 52 44 45 52 20 42 59 20 69 73 20   if ORDER BY is 
3a02d 44 45 53 43 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  DESC */..){..  i
3a02e 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
3a02f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a030 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
3a031 2a 2f 0d 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72  */..  int sortOr
3a032 64 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  der = 0;        
3a033 20 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66 20        /* XOR of 
3a034 69 6e 64 65 78 20 61 6e 64 20 4f 52 44 45 52 20  index and ORDER 
3a035 42 59 20 73 6f 72 74 20 64 69 72 65 63 74 69 6f  BY sort directio
3a036 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72  n */..  int nTer
3a037 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
3a038 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3a039 72 20 6f 66 20 4f 52 44 45 52 20 42 59 20 74 65  r of ORDER BY te
3a03a 72 6d 73 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  rms */..  struct
3a03b 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
3a03c 70 54 65 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74  pTerm;    /* A t
3a03d 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52  erm of the ORDER
3a03e 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20   BY clause */.. 
3a03f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
3a040 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20  Parse->db;....  
3a041 69 66 28 20 21 70 4f 72 64 65 72 42 79 20 29 20  if( !pOrderBy ) 
3a042 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 69 66 28  return 0;..  if(
3a043 20 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45   wsFlags & WHERE
3a044 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 20 72 65 74  _COLUMN_IN ) ret
3a045 75 72 6e 20 30 3b 0d 0a 20 20 69 66 28 20 70 49  urn 0;..  if( pI
3a046 64 78 2d 3e 62 55 6e 6f 72 64 65 72 65 64 20 29  dx->bUnordered )
3a047 20 72 65 74 75 72 6e 20 30 3b 0d 0a 0d 0a 20 20   return 0;....  
3a048 6e 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79  nTerm = pOrderBy
3a049 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 61 73 73 65  ->nExpr;..  asse
3a04a 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0d 0a  rt( nTerm>0 );..
3a04b 0d 0a 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20  ..  /* Argument 
3a04c 70 49 64 78 20 6d 75 73 74 20 65 69 74 68 65 72  pIdx must either
3a04d 20 70 6f 69 6e 74 20 74 6f 20 61 20 27 72 65 61   point to a 'rea
3a04e 6c 27 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 73  l' named index s
3a04f 74 72 75 63 74 75 72 65 2c 20 0d 0a 20 20 2a 2a  tructure, ..  **
3a050 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 73 74 72   or an index str
3a051 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64  ucture allocated
3a052 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 62 79   on the stack by
3a053 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78 28   bestBtreeIndex(
3a054 29 20 74 6f 0d 0a 20 20 2a 2a 20 72 65 70 72 65  ) to..  ** repre
3a055 73 65 6e 74 20 74 68 65 20 72 6f 77 69 64 20 69  sent the rowid i
3a056 6e 64 65 78 20 74 68 61 74 20 69 73 20 70 61 72  ndex that is par
3a057 74 20 6f 66 20 65 76 65 72 79 20 74 61 62 6c 65  t of every table
3a058 2e 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28  .  */..  assert(
3a059 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 7c 7c 20   pIdx->zName || 
3a05a 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d  (pIdx->nColumn==
3a05b 31 20 26 26 20 70 49 64 78 2d 3e 61 69 43 6f 6c  1 && pIdx->aiCol
3a05c 75 6d 6e 5b 30 5d 3d 3d 2d 31 29 20 29 3b 0d 0a  umn[0]==-1) );..
3a05d 0d 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65 72  ..  /* Match ter
3a05e 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
3a05f 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e 73  BY clause agains
3a060 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0d 0a 20 20  t columns of..  
3a061 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0d 0a 20  ** the index... 
3a062 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f 74 65 20 74   **..  ** Note t
3a063 68 61 74 20 69 6e 64 69 63 65 73 20 68 61 76 65  hat indices have
3a064 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72   pIdx->nColumn r
3a065 65 67 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70  egular columns p
3a066 6c 75 73 0d 0a 20 20 2a 2a 20 6f 6e 65 20 61 64  lus..  ** one ad
3a067 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20  ditional column 
3a068 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
3a069 6f 77 69 64 2e 20 20 54 68 65 20 72 6f 77 69 64  owid.  The rowid
3a06a 20 63 6f 6c 75 6d 6e 0d 0a 20 20 2a 2a 20 6f 66   column..  ** of
3a06b 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 61 6c   the index is al
3a06c 73 6f 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  so allowed to ma
3a06d 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65 20  tch against the 
3a06e 4f 52 44 45 52 20 42 59 0d 0a 20 20 2a 2a 20 63  ORDER BY..  ** c
3a06f 6c 61 75 73 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  lause...  */..  
3a070 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d  for(i=j=0, pTerm
3a071 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c  =pOrderBy->a; j<
3a072 6e 54 65 72 6d 20 26 26 20 69 3c 3d 70 49 64 78  nTerm && i<=pIdx
3a073 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
3a074 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  ..    Expr *pExp
3a075 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  r;       /* The 
3a076 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68  expression of th
3a077 65 20 4f 52 44 45 52 20 42 59 20 70 54 65 72 6d  e ORDER BY pTerm
3a078 20 2a 2f 0d 0a 20 20 20 20 43 6f 6c 6c 53 65 71   */..    CollSeq
3a079 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 54   *pColl;    /* T
3a07a 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
3a07b 75 65 6e 63 65 20 6f 66 20 70 45 78 70 72 20 2a  uence of pExpr *
3a07c 2f 0d 0a 20 20 20 20 69 6e 74 20 74 65 72 6d 53  /..    int termS
3a07d 6f 72 74 4f 72 64 65 72 3b 20 2f 2a 20 53 6f 72  ortOrder; /* Sor
3a07e 74 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  t order for this
3a07f 20 74 65 72 6d 20 2a 2f 0d 0a 20 20 20 20 69 6e   term */..    in
3a080 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
3a081 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 6f 6c   /* The i-th col
3a082 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
3a083 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 20  .  -1 for rowid 
3a084 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 53 6f 72  */..    int iSor
3a085 74 4f 72 64 65 72 3b 20 20 20 20 2f 2a 20 31 20  tOrder;    /* 1 
3a086 66 6f 72 20 44 45 53 43 2c 20 30 20 66 6f 72 20  for DESC, 0 for 
3a087 41 53 43 20 6f 6e 20 74 68 65 20 69 2d 74 68 20  ASC on the i-th 
3a088 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0d 0a 20  index term */.. 
3a089 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3a08a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Coll; /* Name of
3a08b 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
3a08c 65 71 75 65 6e 63 65 20 66 6f 72 20 69 2d 74 68  equence for i-th
3a08d 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0d 0a   index term */..
3a08e 0d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 54  ..    pExpr = pT
3a08f 65 72 6d 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20  erm->pExpr;..   
3a090 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
3a091 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78  TK_COLUMN || pEx
3a092 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65  pr->iTable!=base
3a093 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 43 61   ){..      /* Ca
3a094 6e 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64  n not use an ind
3a095 65 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68  ex sort on anyth
3a096 69 6e 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20  ing that is not 
3a097 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0d  a column in the.
3a098 0a 20 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d  .      ** left-m
3a099 6f 73 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  ost table of the
3a09a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0d   FROM clause */.
3a09b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
3a09c 20 20 20 7d 0d 0a 20 20 20 20 70 43 6f 6c 6c 20     }..    pColl 
3a09d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
3a09e 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
3a09f 70 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 70  pr);..    if( !p
3a0a0 43 6f 6c 6c 20 29 7b 0d 0a 20 20 20 20 20 20 70  Coll ){..      p
3a0a1 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
3a0a2 43 6f 6c 6c 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Coll;..    }..  
3a0a3 20 20 69 66 28 20 70 49 64 78 2d 3e 7a 4e 61 6d    if( pIdx->zNam
3a0a4 65 20 26 26 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  e && i<pIdx->nCo
3a0a5 6c 75 6d 6e 20 29 7b 0d 0a 20 20 20 20 20 20 69  lumn ){..      i
3a0a6 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 61  Column = pIdx->a
3a0a7 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0d 0a 20 20 20  iColumn[i];..   
3a0a8 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d     if( iColumn==
3a0a9 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 69 50  pIdx->pTable->iP
3a0aa 4b 65 79 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Key ){..        
3a0ab 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0d 0a 20  iColumn = -1;.. 
3a0ac 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 53       }..      iS
3a0ad 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64 78 2d  ortOrder = pIdx-
3a0ae 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0d  >aSortOrder[i];.
3a0af 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
3a0b0 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0d  Idx->azColl[i];.
3a0b1 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
3a0b2 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b     iColumn = -1;
3a0b3 0d 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64  ..      iSortOrd
3a0b4 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7a  er = 0;..      z
3a0b5 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e  Coll = pColl->zN
3a0b6 61 6d 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  ame;..    }..   
3a0b7 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c   if( pExpr->iCol
3a0b8 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20  umn!=iColumn || 
3a0b9 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
3a0ba 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  Coll->zName, zCo
3a0bb 6c 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a  ll) ){..      /*
3a0bc 20 54 65 72 6d 20 6a 20 6f 66 20 74 68 65 20 4f   Term j of the O
3a0bd 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 64  RDER BY clause d
3a0be 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 63 6f  oes not match co
3a0bf 6c 75 6d 6e 20 69 20 6f 66 20 74 68 65 20 69 6e  lumn i of the in
3a0c0 64 65 78 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66  dex */..      if
3a0c1 28 20 69 3c 6e 45 71 43 6f 6c 20 29 7b 0d 0a 20  ( i<nEqCol ){.. 
3a0c2 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20         /* If an 
3a0c3 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 74 68 61  index column tha
3a0c4 74 20 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64  t is constrained
3a0c5 20 62 79 20 3d 3d 20 66 61 69 6c 73 20 74 6f 20   by == fails to 
3a0c6 6d 61 74 63 68 20 61 6e 0d 0a 20 20 20 20 20 20  match an..      
3a0c7 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65    ** ORDER BY te
3a0c8 72 6d 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 20  rm, that is OK. 
3a0c9 20 4a 75 73 74 20 69 67 6e 6f 72 65 20 74 68 61   Just ignore tha
3a0ca 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
3a0cb 69 6e 64 65 78 0d 0a 20 20 20 20 20 20 20 20 2a  index..        *
3a0cc 2f 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  /..        conti
3a0cd 6e 75 65 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  nue;..      }els
3a0ce 65 20 69 66 28 20 69 3d 3d 70 49 64 78 2d 3e 6e  e if( i==pIdx->n
3a0cf 43 6f 6c 75 6d 6e 20 29 7b 0d 0a 20 20 20 20 20  Column ){..     
3a0d0 20 20 20 2f 2a 20 49 6e 64 65 78 20 63 6f 6c 75     /* Index colu
3a0d1 6d 6e 20 69 20 69 73 20 74 68 65 20 72 6f 77 69  mn i is the rowi
3a0d2 64 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 74 65  d.  All other te
3a0d3 72 6d 73 20 6d 61 74 63 68 2e 20 2a 2f 0d 0a 20  rms match. */.. 
3a0d4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3a0d5 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
3a0d6 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e       /* If an in
3a0d7 64 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69 6c 73  dex column fails
3a0d8 20 74 6f 20 6d 61 74 63 68 20 61 6e 64 20 69 73   to match and is
3a0d9 20 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e 65 64   not constrained
3a0da 20 62 79 20 3d 3d 0d 0a 20 20 20 20 20 20 20 20   by ==..        
3a0db 2a 2a 20 74 68 65 6e 20 74 68 65 20 69 6e 64 65  ** then the inde
3a0dc 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79  x cannot satisfy
3a0dd 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6f   the ORDER BY co
3a0de 6e 73 74 72 61 69 6e 74 2e 0d 0a 20 20 20 20 20  nstraint...     
3a0df 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72     */..        r
3a0e0 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20  eturn 0;..      
3a0e1 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73  }..    }..    as
3a0e2 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53 6f 72  sert( pIdx->aSor
3a0e3 74 4f 72 64 65 72 21 3d 30 20 7c 7c 20 69 43 6f  tOrder!=0 || iCo
3a0e4 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0d 0a 20 20 20  lumn==-1 );..   
3a0e5 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
3a0e6 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  sortOrder==0 || 
3a0e7 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  pTerm->sortOrder
3a0e8 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  ==1 );..    asse
3a0e9 72 74 28 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d  rt( iSortOrder==
3a0ea 30 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65 72 3d  0 || iSortOrder=
3a0eb 3d 31 20 29 3b 0d 0a 20 20 20 20 74 65 72 6d 53  =1 );..    termS
3a0ec 6f 72 74 4f 72 64 65 72 20 3d 20 69 53 6f 72 74  ortOrder = iSort
3a0ed 4f 72 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e 73  Order ^ pTerm->s
3a0ee 6f 72 74 4f 72 64 65 72 3b 0d 0a 20 20 20 20 69  ortOrder;..    i
3a0ef 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b 0d 0a  f( i>nEqCol ){..
3a0f0 20 20 20 20 20 20 69 66 28 20 74 65 72 6d 53 6f        if( termSo
3a0f1 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72 64  rtOrder!=sortOrd
3a0f2 65 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f  er ){..        /
3a0f3 2a 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e  * Indices can on
3a0f4 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 61 6c  ly be used if al
3a0f5 6c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73  l ORDER BY terms
3a0f6 20 70 61 73 74 20 74 68 65 0d 0a 20 20 20 20 20   past the..     
3a0f7 20 20 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63     ** equality c
3a0f8 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61  onstraints are a
3a0f9 6c 6c 20 65 69 74 68 65 72 20 44 45 53 43 20 6f  ll either DESC o
3a0fa 72 20 41 53 43 2e 20 2a 2f 0d 0a 20 20 20 20 20  r ASC. */..     
3a0fb 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20     return 0;..  
3a0fc 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65      }..    }else
3a0fd 7b 0d 0a 20 20 20 20 20 20 73 6f 72 74 4f 72 64  {..      sortOrd
3a0fe 65 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f 72 64  er = termSortOrd
3a0ff 65 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  er;..    }..    
3a100 6a 2b 2b 3b 0d 0a 20 20 20 20 70 54 65 72 6d 2b  j++;..    pTerm+
3a101 2b 3b 0d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  +;..    if( iCol
3a102 75 6d 6e 3c 30 20 26 26 20 21 72 65 66 65 72 65  umn<0 && !refere
3a103 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
3a104 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53  pOrderBy, pMaskS
3a105 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0d  et, j, base) ){.
3a106 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
3a107 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20   indexed column 
3a108 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  is the primary k
3a109 65 79 20 61 6e 64 20 65 76 65 72 79 74 68 69 6e  ey and everythin
3a10a 67 20 6d 61 74 63 68 65 73 0d 0a 20 20 20 20 20  g matches..     
3a10b 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e 64 20 6e   ** so far and n
3a10c 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45 52  one of the ORDER
3a10d 20 42 59 20 74 65 72 6d 73 20 74 6f 20 74 68 65   BY terms to the
3a10e 20 72 69 67 68 74 20 72 65 66 65 72 65 6e 63 65   right reference
3a10f 20 6f 74 68 65 72 0d 0a 20 20 20 20 20 20 2a 2a   other..      **
3a110 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a   tables in the j
3a111 6f 69 6e 2c 20 74 68 65 6e 20 77 65 20 61 72 65  oin, then we are
3a112 20 61 73 73 75 72 65 64 20 74 68 61 74 20 74 68   assured that th
3a113 65 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75  e index can be u
3a114 73 65 64 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74  sed ..      ** t
3a115 6f 20 73 6f 72 74 20 62 65 63 61 75 73 65 20 74  o sort because t
3a116 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
3a117 73 20 75 6e 69 71 75 65 20 61 6e 64 20 73 6f 20  s unique and so 
3a118 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f 74 68 65  none of the othe
3a119 72 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  r..      ** colu
3a11a 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e  mns will make an
3a11b 79 20 64 69 66 66 65 72 65 6e 63 65 0d 0a 20 20  y difference..  
3a11c 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 6a 20      */..      j 
3a11d 3d 20 6e 54 65 72 6d 3b 0d 0a 20 20 20 20 7d 0d  = nTerm;..    }.
3a11e 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 62 52 65 76  .  }....  *pbRev
3a11f 20 3d 20 73 6f 72 74 4f 72 64 65 72 21 3d 30 3b   = sortOrder!=0;
3a120 0d 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65 72 6d  ..  if( j>=nTerm
3a121 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41 6c 6c 20   ){..    /* All 
3a122 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44  terms of the ORD
3a123 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 72 65  ER BY clause are
3a124 20 63 6f 76 65 72 65 64 20 62 79 20 74 68 69 73   covered by this
3a125 20 69 6e 64 65 78 20 73 6f 0d 0a 20 20 20 20 2a   index so..    *
3a126 2a 20 74 68 69 73 20 69 6e 64 65 78 20 63 61 6e  * this index can
3a127 20 62 65 20 75 73 65 64 20 66 6f 72 20 73 6f 72   be used for sor
3a128 74 69 6e 67 2e 20 2a 2f 0d 0a 20 20 20 20 72 65  ting. */..    re
3a129 74 75 72 6e 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20  turn 1;..  }..  
3a12a 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  if( pIdx->onErro
3a12b 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 69 3d  r!=OE_None && i=
3a12c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0d 0a  =pIdx->nColumn..
3a12d 20 20 20 20 20 20 26 26 20 28 77 73 46 6c 61 67        && (wsFlag
3a12e 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s & WHERE_COLUMN
3a12f 5f 4e 55 4c 4c 29 3d 3d 30 0d 0a 20 20 20 20 20  _NULL)==0..     
3a130 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f   && !referencesO
3a131 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65  therTables(pOrde
3a132 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a  rBy, pMaskSet, j
3a133 2c 20 62 61 73 65 29 20 29 7b 0d 0a 20 20 20 20  , base) ){..    
3a134 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20  /* All terms of 
3a135 74 68 69 73 20 69 6e 64 65 78 20 6d 61 74 63 68  this index match
3a136 20 73 6f 6d 65 20 70 72 65 66 69 78 20 6f 66 20   some prefix of 
3a137 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
3a138 75 73 65 0d 0a 20 20 20 20 2a 2a 20 61 6e 64 20  use..    ** and 
3a139 74 68 65 20 69 6e 64 65 78 20 69 73 20 55 4e 49  the index is UNI
3a13a 51 55 45 20 61 6e 64 20 6e 6f 20 74 65 72 6d 73  QUE and no terms
3a13b 20 6f 6e 20 74 68 65 20 74 61 69 6c 20 6f 66 20   on the tail of 
3a13c 74 68 65 20 4f 52 44 45 52 20 42 59 0d 0a 20 20  the ORDER BY..  
3a13d 20 20 2a 2a 20 63 6c 61 75 73 65 20 72 65 66 65    ** clause refe
3a13e 72 65 6e 63 65 20 6f 74 68 65 72 20 74 61 62 6c  rence other tabl
3a13f 65 73 20 69 6e 20 61 20 6a 6f 69 6e 2e 20 20 49  es in a join.  I
3a140 66 20 74 68 69 73 20 69 73 20 61 6c 6c 20 74 72  f this is all tr
3a141 75 65 20 74 68 65 6e 0d 0a 20 20 20 20 2a 2a 20  ue then..    ** 
3a142 74 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c 61  the order by cla
3a143 75 73 65 20 69 73 20 73 75 70 65 72 66 6c 75 6f  use is superfluo
3a144 75 73 2e 20 20 4e 6f 74 20 74 68 61 74 20 69 66  us.  Not that if
3a145 20 74 68 65 20 6d 61 74 63 68 69 6e 67 0d 0a 20   the matching.. 
3a146 20 20 20 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20     ** condition 
3a147 69 73 20 49 53 20 4e 55 4c 4c 20 74 68 65 6e 20  is IS NULL then 
3a148 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f  the result is no
3a149 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 75 6e  t necessarily un
3a14a 69 71 75 65 0d 0a 20 20 20 20 2a 2a 20 65 76 65  ique..    ** eve
3a14b 6e 20 6f 6e 20 61 20 55 4e 49 51 55 45 20 69 6e  n on a UNIQUE in
3a14c 64 65 78 2c 20 73 6f 20 64 69 73 61 6c 6c 6f 77  dex, so disallow
3a14d 20 74 68 6f 73 65 20 63 61 73 65 73 2e 20 2a 2f   those cases. */
3a14e 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d  ..    return 1;.
3a14f 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30  .  }..  return 0
3a150 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50  ;..}..../*..** P
3a151 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20 65  repare a crude e
3a152 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6c  stimate of the l
3a153 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65 20  ogarithm of the 
3a154 69 6e 70 75 74 20 76 61 6c 75 65 2e 0d 0a 2a 2a  input value...**
3a155 20 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65   The results nee
3a156 64 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20  d not be exact. 
3a157 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73   This is only us
3a158 65 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e  ed for estimatin
3a159 67 0d 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20  g..** the total 
3a15a 63 6f 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69  cost of performi
3a15b 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 69  ng operations wi
3a15c 74 68 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28  th O(logN) or O(
3a15d 4e 6c 6f 67 4e 29 0d 0a 2a 2a 20 63 6f 6d 70 6c  NlogN)..** compl
3a15e 65 78 69 74 79 2e 20 20 42 65 63 61 75 73 65 20  exity.  Because 
3a15f 4e 20 69 73 20 6a 75 73 74 20 61 20 67 75 65 73  N is just a gues
3a160 73 2c 20 69 74 20 69 73 20 6e 6f 20 67 72 65 61  s, it is no grea
3a161 74 20 74 72 61 67 65 64 79 20 69 66 0d 0a 2a 2a  t tragedy if..**
3a162 20 6c 6f 67 4e 20 69 73 20 61 20 6c 69 74 74 6c   logN is a littl
3a163 65 20 6f 66 66 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  e off...*/..stat
3a164 69 63 20 64 6f 75 62 6c 65 20 65 73 74 4c 6f 67  ic double estLog
3a165 28 64 6f 75 62 6c 65 20 4e 29 7b 0d 0a 20 20 64  (double N){..  d
3a166 6f 75 62 6c 65 20 6c 6f 67 4e 20 3d 20 31 3b 0d  ouble logN = 1;.
3a167 0a 20 20 64 6f 75 62 6c 65 20 78 20 3d 20 31 30  .  double x = 10
3a168 3b 0d 0a 20 20 77 68 69 6c 65 28 20 4e 3e 78 20  ;..  while( N>x 
3a169 29 7b 0d 0a 20 20 20 20 6c 6f 67 4e 20 2b 3d 20  ){..    logN += 
3a16a 31 3b 0d 0a 20 20 20 20 78 20 2a 3d 20 31 30 3b  1;..    x *= 10;
3a16b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
3a16c 6c 6f 67 4e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  logN;..}..../*..
3a16d 2a 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20  ** Two routines 
3a16e 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65  for printing the
3a16f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73   content of an s
3a170 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3a171 6f 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  o..** structure.
3a172 20 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69    Used for testi
3a173 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
3a174 20 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68   only.  If neith
3a175 65 72 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45  er..** SQLITE_TE
3a176 53 54 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 42  ST or SQLITE_DEB
3a177 55 47 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  UG are defined, 
3a178 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69  then these routi
3a179 6e 65 73 0d 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f  nes..** are no-o
3a17a 70 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65  ps...*/..#if !de
3a17b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
3a17c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20  T_VIRTUALTABLE) 
3a17d 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
3a17e 45 5f 44 45 42 55 47 29 0d 0a 73 74 61 74 69 63  E_DEBUG)..static
3a17f 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58 5f   void TRACE_IDX_
3a180 49 4e 50 55 54 53 28 73 71 6c 69 74 65 33 5f 69  INPUTS(sqlite3_i
3a181 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0d 0a  ndex_info *p){..
3a182 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 66 28 20    int i;..  if( 
3a183 21 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61  !sqlite3WhereTra
3a184 63 65 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20  ce ) return;..  
3a185 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
3a186 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b  onstraint; i++){
3a187 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ..    sqlite3Deb
3a188 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e 73  ugPrintf("  cons
3a189 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d  traint[%d]: col=
3a18a 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d  %d termid=%d op=
3a18b 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22  %d usabled=%d\n"
3a18c 2c 0d 0a 20 20 20 20 20 20 20 69 2c 0d 0a 20 20  ,..       i,..  
3a18d 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
3a18e 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0d  int[i].iColumn,.
3a18f 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
3a190 74 72 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f  traint[i].iTermO
3a191 66 66 73 65 74 2c 0d 0a 20 20 20 20 20 20 20 70  ffset,..       p
3a192 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d  ->aConstraint[i]
3a193 2e 6f 70 2c 0d 0a 20 20 20 20 20 20 20 70 2d 3e  .op,..       p->
3a194 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75  aConstraint[i].u
3a195 73 61 62 6c 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20  sable);..  }..  
3a196 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
3a197 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0d 0a 20  rderBy; i++){.. 
3a198 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
3a199 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72 62 79  rintf("  orderby
3a19a 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73  [%d]: col=%d des
3a19b 63 3d 25 64 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  c=%d\n",..      
3a19c 20 69 2c 0d 0a 20 20 20 20 20 20 20 70 2d 3e 61   i,..       p->a
3a19d 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75  OrderBy[i].iColu
3a19e 6d 6e 2c 0d 0a 20 20 20 20 20 20 20 70 2d 3e 61  mn,..       p->a
3a19f 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 29  OrderBy[i].desc)
3a1a0 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 73 74 61 74 69  ;..  }..}..stati
3a1a1 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58  c void TRACE_IDX
3a1a2 5f 4f 55 54 50 55 54 53 28 73 71 6c 69 74 65 33  _OUTPUTS(sqlite3
3a1a3 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b  _index_info *p){
3a1a4 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 66  ..  int i;..  if
3a1a5 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65 54  ( !sqlite3WhereT
3a1a6 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0d 0a  race ) return;..
3a1a7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
3a1a8 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
3a1a9 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ){..    sqlite3D
3a1aa 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73  ebugPrintf("  us
3a1ab 61 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78  age[%d]: argvIdx
3a1ac 3d 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0d  =%d omit=%d\n",.
3a1ad 0a 20 20 20 20 20 20 20 69 2c 0d 0a 20 20 20 20  .       i,..    
3a1ae 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
3a1af 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e  tUsage[i].argvIn
3a1b0 64 65 78 2c 0d 0a 20 20 20 20 20 20 20 70 2d 3e  dex,..       p->
3a1b1 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
3a1b2 5b 69 5d 2e 6f 6d 69 74 29 3b 0d 0a 20 20 7d 0d  [i].omit);..  }.
3a1b3 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
3a1b4 72 69 6e 74 66 28 22 20 20 69 64 78 4e 75 6d 3d  rintf("  idxNum=
3a1b5 25 64 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e 75 6d  %d\n", p->idxNum
3a1b6 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44 65 62  );..  sqlite3Deb
3a1b7 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78 53  ugPrintf("  idxS
3a1b8 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69 64 78  tr=%s\n", p->idx
3a1b9 53 74 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  Str);..  sqlite3
3a1ba 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f  DebugPrintf("  o
3a1bb 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3d 25  rderByConsumed=%
3a1bc 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72 42 79  d\n", p->orderBy
3a1bd 43 6f 6e 73 75 6d 65 64 29 3b 0d 0a 20 20 73 71  Consumed);..  sq
3a1be 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
3a1bf 28 22 20 20 65 73 74 69 6d 61 74 65 64 43 6f 73  ("  estimatedCos
3a1c0 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74 69  t=%g\n", p->esti
3a1c1 6d 61 74 65 64 43 6f 73 74 29 3b 0d 0a 7d 0d 0a  matedCost);..}..
3a1c2 23 65 6c 73 65 0d 0a 23 64 65 66 69 6e 65 20 54  #else..#define T
3a1c3 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
3a1c4 41 29 0d 0a 23 64 65 66 69 6e 65 20 54 52 41 43  A)..#define TRAC
3a1c5 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 41 29  E_IDX_OUTPUTS(A)
3a1c6 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 0d  ..#endif..../* .
3a1c7 0a 2a 2a 20 52 65 71 75 69 72 65 64 20 62 65 63  .** Required bec
3a1c8 61 75 73 65 20 62 65 73 74 49 6e 64 65 78 28 29  ause bestIndex()
3a1c9 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 62 65   is called by be
3a1ca 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28  stOrClauseIndex(
3a1cb 29 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  ) ..*/..static v
3a1cc 6f 69 64 20 62 65 73 74 49 6e 64 65 78 28 0d 0a  oid bestIndex(..
3a1cd 20 20 20 20 50 61 72 73 65 2a 2c 20 57 68 65 72      Parse*, Wher
3a1ce 65 43 6c 61 75 73 65 2a 2c 20 73 74 72 75 63 74  eClause*, struct
3a1cf 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2a 2c 0d   SrcList_item*,.
3a1d0 0a 20 20 20 20 42 69 74 6d 61 73 6b 2c 20 42 69  .    Bitmask, Bi
3a1d1 74 6d 61 73 6b 2c 20 45 78 70 72 4c 69 73 74 2a  tmask, ExprList*
3a1d2 2c 20 57 68 65 72 65 43 6f 73 74 2a 29 3b 0d 0a  , WhereCost*);..
3a1d3 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f  ../*..** This ro
3a1d4 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74  utine attempts t
3a1d5 6f 20 66 69 6e 64 20 61 6e 20 73 63 61 6e 6e 69  o find an scanni
3a1d6 6e 67 20 73 74 72 61 74 65 67 79 20 74 68 61 74  ng strategy that
3a1d7 20 63 61 6e 20 62 65 20 75 73 65 64 20 0d 0a 2a   can be used ..*
3a1d8 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e  * to optimize an
3a1d9 20 27 4f 52 27 20 65 78 70 72 65 73 73 69 6f 6e   'OR' expression
3a1da 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66   that is part of
3a1db 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e   a WHERE clause.
3a1dc 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 61   ..**..** The ta
3a1dd 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
3a1de 69 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  ith FROM clause 
3a1df 74 65 72 6d 20 70 53 72 63 20 6d 61 79 20 62 65  term pSrc may be
3a1e0 20 65 69 74 68 65 72 20 61 0d 0a 2a 2a 20 72 65   either a..** re
3a1e1 67 75 6c 61 72 20 42 2d 54 72 65 65 20 74 61 62  gular B-Tree tab
3a1e2 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20  le or a virtual 
3a1e3 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  table...*/..stat
3a1e4 69 63 20 76 6f 69 64 20 62 65 73 74 4f 72 43 6c  ic void bestOrCl
3a1e5 61 75 73 65 49 6e 64 65 78 28 0d 0a 20 20 50 61  auseIndex(..  Pa
3a1e6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
3a1e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3a1e8 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
3a1e9 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61 75   */..  WhereClau
3a1ea 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
3a1eb 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
3a1ec 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 73 74 72  clause */..  str
3a1ed 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
3a1ee 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20   *pSrc,  /* The 
3a1ef 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
3a1f0 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0d 0a 20   to search */.. 
3a1f1 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
3a1f2 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
3a1f3 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
3a1f4 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  not available fo
3a1f5 72 20 69 6e 64 65 78 69 6e 67 20 2a 2f 0d 0a 20  r indexing */.. 
3a1f6 20 42 69 74 6d 61 73 6b 20 6e 6f 74 56 61 6c 69   Bitmask notVali
3a1f7 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
3a1f8 43 75 72 73 6f 72 73 20 6e 6f 74 20 61 76 61 69  Cursors not avai
3a1f9 6c 61 62 6c 65 20 66 6f 72 20 61 6e 79 20 70 75  lable for any pu
3a1fa 72 70 6f 73 65 20 2a 2f 0d 0a 20 20 45 78 70 72  rpose */..  Expr
3a1fb 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
3a1fc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
3a1fd 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
3a1fe 2f 0d 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a  /..  WhereCost *
3a1ff 70 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20  pCost           
3a200 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20   /* Lowest cost 
3a201 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0d 0a 29  query plan */..)
3a202 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  {..#ifndef SQLIT
3a203 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49  E_OMIT_OR_OPTIMI
3a204 5a 41 54 49 4f 4e 0d 0a 20 20 63 6f 6e 73 74 20  ZATION..  const 
3a205 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63 2d  int iCur = pSrc-
3a206 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54  >iCursor;   /* T
3a207 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65  he cursor of the
3a208 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
3a209 65 73 73 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73  essed */..  cons
3a20a 74 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 53 72  t Bitmask maskSr
3a20b 63 20 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d  c = getMask(pWC-
3a20c 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29  >pMaskSet, iCur)
3a20d 3b 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f  ;  /* Bitmask fo
3a20e 72 20 70 53 72 63 20 2a 2f 0d 0a 20 20 57 68 65  r pSrc */..  Whe
3a20f 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70  reTerm * const p
3a210 57 43 45 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b  WCEnd = &pWC->a[
3a211 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b 20 20 20 20  pWC->nTerm];    
3a212 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57      /* End of pW
3a213 43 2d 3e 61 5b 5d 20 2a 2f 0d 0a 20 20 57 68 65  C->a[] */..  Whe
3a214 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
3a215 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a216 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  * A single term 
3a217 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
3a218 75 73 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54  use */....  /* T
3a219 68 65 20 4f 52 2d 63 6c 61 75 73 65 20 6f 70 74  he OR-clause opt
3a21a 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 64 69 73  imization is dis
3a21b 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20 49  allowed if the I
3a21c 4e 44 45 58 45 44 20 42 59 20 6f 72 0d 0a 20 20  NDEXED BY or..  
3a21d 2a 2a 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63  ** NOT INDEXED c
3a21e 6c 61 75 73 65 73 20 61 72 65 20 75 73 65 64 20  lauses are used 
3a21f 6f 72 20 69 66 20 74 68 65 20 57 48 45 52 45 5f  or if the WHERE_
3a220 41 4e 44 5f 4f 4e 4c 59 20 62 69 74 20 69 73 20  AND_ONLY bit is 
3a221 73 65 74 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 70  set. */..  if( p
3a222 53 72 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20  Src->notIndexed 
3a223 7c 7c 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 21  || pSrc->pIndex!
3a224 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
3a225 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  n;..  }..  if( p
3a226 57 43 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26  WC->wctrlFlags &
3a227 20 57 48 45 52 45 5f 41 4e 44 5f 4f 4e 4c 59 20   WHERE_AND_ONLY 
3a228 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d  ){..    return;.
3a229 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 61  .  }....  /* Sea
3a22a 72 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c  rch the WHERE cl
3a22b 61 75 73 65 20 74 65 72 6d 73 20 66 6f 72 20 61  ause terms for a
3a22c 20 75 73 61 62 6c 65 20 57 4f 5f 4f 52 20 74 65   usable WO_OR te
3a22d 72 6d 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 70 54  rm. */..  for(pT
3a22e 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72  erm=pWC->a; pTer
3a22f 6d 3c 70 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b  m<pWCEnd; pTerm+
3a230 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 54 65  +){..    if( pTe
3a231 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
3a232 4f 5f 4f 52 20 0d 0a 20 20 20 20 20 26 26 20 28  O_OR ..     && (
3a233 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
3a234 6c 20 26 20 7e 6d 61 73 6b 53 72 63 29 20 26 20  l & ~maskSrc) & 
3a235 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0d 0a 20 20  notReady)==0..  
3a236 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e     && (pTerm->u.
3a237 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62  pOrInfo->indexab
3a238 6c 65 20 26 20 6d 61 73 6b 53 72 63 29 21 3d 30  le & maskSrc)!=0
3a239 20 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20   ..    ){..     
3a23a 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 20 63   WhereClause * c
3a23b 6f 6e 73 74 20 70 4f 72 57 43 20 3d 20 26 70 54  onst pOrWC = &pT
3a23c 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e  erm->u.pOrInfo->
3a23d 77 63 3b 0d 0a 20 20 20 20 20 20 57 68 65 72 65  wc;..      Where
3a23e 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 4f 72  Term * const pOr
3a23f 57 43 45 6e 64 20 3d 20 26 70 4f 72 57 43 2d 3e  WCEnd = &pOrWC->
3a240 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 5d 3b  a[pOrWC->nTerm];
3a241 0d 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  ..      WhereTer
3a242 6d 20 2a 70 4f 72 54 65 72 6d 3b 0d 0a 20 20 20  m *pOrTerm;..   
3a243 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 57     int flags = W
3a244 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0d 0a  HERE_MULTI_OR;..
3a245 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 54 6f        double rTo
3a246 74 61 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  tal = 0;..      
3a247 64 6f 75 62 6c 65 20 6e 52 6f 77 20 3d 20 30 3b  double nRow = 0;
3a248 0d 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20  ..      Bitmask 
3a249 75 73 65 64 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20  used = 0;....   
3a24a 20 20 20 66 6f 72 28 70 4f 72 54 65 72 6d 3d 70     for(pOrTerm=p
3a24b 4f 72 57 43 2d 3e 61 3b 20 70 4f 72 54 65 72 6d  OrWC->a; pOrTerm
3a24c 3c 70 4f 72 57 43 45 6e 64 3b 20 70 4f 72 54 65  <pOrWCEnd; pOrTe
3a24d 72 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  rm++){..        
3a24e 57 68 65 72 65 43 6f 73 74 20 73 54 65 72 6d 43  WhereCost sTermC
3a24f 6f 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 57 48  ost;..        WH
3a250 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 4d  ERETRACE(("... M
3a251 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 74 65  ulti-index OR te
3a252 73 74 69 6e 67 20 66 6f 72 20 74 65 72 6d 20 25  sting for term %
3a253 64 20 6f 66 20 25 64 2e 2e 2e 2e 5c 6e 22 2c 20  d of %d....\n", 
3a254 0d 0a 20 20 20 20 20 20 20 20 20 20 28 70 4f 72  ..          (pOr
3a255 54 65 72 6d 20 2d 20 70 4f 72 57 43 2d 3e 61 29  Term - pOrWC->a)
3a256 2c 20 28 70 54 65 72 6d 20 2d 20 70 57 43 2d 3e  , (pTerm - pWC->
3a257 61 29 0d 0a 20 20 20 20 20 20 20 20 29 29 3b 0d  a)..        ));.
3a258 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 72  .        if( pOr
3a259 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
3a25a 3d 57 4f 5f 41 4e 44 20 29 7b 0d 0a 20 20 20 20  =WO_AND ){..    
3a25b 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73        WhereClaus
3a25c 65 20 2a 70 41 6e 64 57 43 20 3d 20 26 70 4f 72  e *pAndWC = &pOr
3a25d 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f  Term->u.pAndInfo
3a25e 2d 3e 77 63 3b 0d 0a 20 20 20 20 20 20 20 20 20  ->wc;..         
3a25f 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73   bestIndex(pPars
3a260 65 2c 20 70 41 6e 64 57 43 2c 20 70 53 72 63 2c  e, pAndWC, pSrc,
3a261 20 6e 6f 74 52 65 61 64 79 2c 20 6e 6f 74 56 61   notReady, notVa
3a262 6c 69 64 2c 20 30 2c 20 26 73 54 65 72 6d 43 6f  lid, 0, &sTermCo
3a263 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  st);..        }e
3a264 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
3a265 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
3a266 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  r ){..          
3a267 57 68 65 72 65 43 6c 61 75 73 65 20 74 65 6d 70  WhereClause temp
3a268 57 43 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 74  WC;..          t
3a269 65 6d 70 57 43 2e 70 50 61 72 73 65 20 3d 20 70  empWC.pParse = p
3a26a 57 43 2d 3e 70 50 61 72 73 65 3b 0d 0a 20 20 20  WC->pParse;..   
3a26b 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70 4d         tempWC.pM
3a26c 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d  askSet = pWC->pM
3a26d 61 73 6b 53 65 74 3b 0d 0a 20 20 20 20 20 20 20  askSet;..       
3a26e 20 20 20 74 65 6d 70 57 43 2e 70 4f 75 74 65 72     tempWC.pOuter
3a26f 20 3d 20 70 57 43 3b 0d 0a 20 20 20 20 20 20 20   = pWC;..       
3a270 20 20 20 74 65 6d 70 57 43 2e 6f 70 20 3d 20 54     tempWC.op = T
3a271 4b 5f 41 4e 44 3b 0d 0a 20 20 20 20 20 20 20 20  K_AND;..        
3a272 20 20 74 65 6d 70 57 43 2e 61 20 3d 20 70 4f 72    tempWC.a = pOr
3a273 54 65 72 6d 3b 0d 0a 20 20 20 20 20 20 20 20 20  Term;..         
3a274 20 74 65 6d 70 57 43 2e 77 63 74 72 6c 46 6c 61   tempWC.wctrlFla
3a275 67 73 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  gs = 0;..       
3a276 20 20 20 74 65 6d 70 57 43 2e 6e 54 65 72 6d 20     tempWC.nTerm 
3a277 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  = 1;..          
3a278 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65  bestIndex(pParse
3a279 2c 20 26 74 65 6d 70 57 43 2c 20 70 53 72 63 2c  , &tempWC, pSrc,
3a27a 20 6e 6f 74 52 65 61 64 79 2c 20 6e 6f 74 56 61   notReady, notVa
3a27b 6c 69 64 2c 20 30 2c 20 26 73 54 65 72 6d 43 6f  lid, 0, &sTermCo
3a27c 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  st);..        }e
3a27d 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
3a27e 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20  continue;..     
3a27f 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 72 54     }..        rT
3a280 6f 74 61 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73  otal += sTermCos
3a281 74 2e 72 43 6f 73 74 3b 0d 0a 20 20 20 20 20 20  t.rCost;..      
3a282 20 20 6e 52 6f 77 20 2b 3d 20 73 54 65 72 6d 43    nRow += sTermC
3a283 6f 73 74 2e 70 6c 61 6e 2e 6e 52 6f 77 3b 0d 0a  ost.plan.nRow;..
3a284 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20          used |= 
3a285 73 54 65 72 6d 43 6f 73 74 2e 75 73 65 64 3b 0d  sTermCost.used;.
3a286 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 54 6f  .        if( rTo
3a287 74 61 6c 3e 3d 70 43 6f 73 74 2d 3e 72 43 6f 73  tal>=pCost->rCos
3a288 74 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20  t ) break;..    
3a289 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20    }....      /* 
3a28a 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  If there is an O
3a28b 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
3a28c 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 63 61  increase the sca
3a28d 6e 20 63 6f 73 74 20 74 6f 20 61 63 63 6f 75 6e  n cost to accoun
3a28e 74 20 0d 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  t ..      ** for
3a28f 20 74 68 65 20 63 6f 73 74 20 6f 66 20 74 68 65   the cost of the
3a290 20 73 6f 72 74 2e 20 2a 2f 0d 0a 20 20 20 20 20   sort. */..     
3a291 20 69 66 28 20 70 4f 72 64 65 72 42 79 21 3d 30   if( pOrderBy!=0
3a292 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 57 48 45   ){..        WHE
3a293 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 73 6f  RETRACE(("... so
3a294 72 74 69 6e 67 20 69 6e 63 72 65 61 73 65 73 20  rting increases 
3a295 4f 52 20 63 6f 73 74 20 25 2e 39 67 20 74 6f 20  OR cost %.9g to 
3a296 25 2e 39 67 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  %.9g\n",..      
3a297 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 54                rT
3a298 6f 74 61 6c 2c 20 72 54 6f 74 61 6c 2b 6e 52 6f  otal, rTotal+nRo
3a299 77 2a 65 73 74 4c 6f 67 28 6e 52 6f 77 29 29 29  w*estLog(nRow)))
3a29a 3b 0d 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61  ;..        rTota
3a29b 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c 6f 67  l += nRow*estLog
3a29c 28 6e 52 6f 77 29 3b 0d 0a 20 20 20 20 20 20 7d  (nRow);..      }
3a29d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  ....      /* If 
3a29e 74 68 65 20 63 6f 73 74 20 6f 66 20 73 63 61 6e  the cost of scan
3a29f 6e 69 6e 67 20 75 73 69 6e 67 20 74 68 69 73 20  ning using this 
3a2a0 4f 52 20 74 65 72 6d 20 66 6f 72 20 6f 70 74 69  OR term for opti
3a2a1 6d 69 7a 61 74 69 6f 6e 20 69 73 0d 0a 20 20 20  mization is..   
3a2a2 20 20 20 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20     ** less than 
3a2a3 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 73 74  the current cost
3a2a4 20 73 74 6f 72 65 64 20 69 6e 20 70 43 6f 73 74   stored in pCost
3a2a5 2c 20 72 65 70 6c 61 63 65 20 74 68 65 20 63 6f  , replace the co
3a2a6 6e 74 65 6e 74 73 0d 0a 20 20 20 20 20 20 2a 2a  ntents..      **
3a2a7 20 6f 66 20 70 43 6f 73 74 2e 20 2a 2f 0d 0a 20   of pCost. */.. 
3a2a8 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
3a2a9 28 22 2e 2e 2e 20 6d 75 6c 74 69 2d 69 6e 64 65  ("... multi-inde
3a2aa 78 20 4f 52 20 63 6f 73 74 3d 25 2e 39 67 20 6e  x OR cost=%.9g n
3a2ab 72 6f 77 3d 25 2e 39 67 5c 6e 22 2c 20 72 54 6f  row=%.9g\n", rTo
3a2ac 74 61 6c 2c 20 6e 52 6f 77 29 29 3b 0d 0a 20 20  tal, nRow));..  
3a2ad 20 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3c 70      if( rTotal<p
3a2ae 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0d 0a  Cost->rCost ){..
3a2af 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72          pCost->r
3a2b0 43 6f 73 74 20 3d 20 72 54 6f 74 61 6c 3b 0d 0a  Cost = rTotal;..
3a2b1 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75          pCost->u
3a2b2 73 65 64 20 3d 20 75 73 65 64 3b 0d 0a 20 20 20  sed = used;..   
3a2b3 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e       pCost->plan
3a2b4 2e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0d 0a 20  .nRow = nRow;.. 
3a2b5 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c         pCost->pl
3a2b6 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 66 6c 61  an.wsFlags = fla
3a2b7 67 73 3b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f  gs;..        pCo
3a2b8 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d  st->plan.u.pTerm
3a2b9 20 3d 20 70 54 65 72 6d 3b 0d 0a 20 20 20 20 20   = pTerm;..     
3a2ba 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
3a2bb 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
3a2bc 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
3a2bd 41 54 49 4f 4e 20 2a 2f 0d 0a 7d 0d 0a 0d 0a 23  ATION */..}....#
3a2be 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3a2bf 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44  IT_AUTOMATIC_IND
3a2c0 45 58 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  EX../*..** Retur
3a2c1 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 57 48  n TRUE if the WH
3a2c2 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ERE clause term 
3a2c3 70 54 65 72 6d 20 69 73 20 6f 66 20 61 20 66 6f  pTerm is of a fo
3a2c4 72 6d 20 77 68 65 72 65 20 69 74 0d 0a 2a 2a 20  rm where it..** 
3a2c5 63 6f 75 6c 64 20 62 65 20 75 73 65 64 20 77 69  could be used wi
3a2c6 74 68 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 61  th an index to a
3a2c7 63 63 65 73 73 20 70 53 72 63 2c 20 61 73 73 75  ccess pSrc, assu
3a2c8 6d 69 6e 67 20 61 6e 20 61 70 70 72 6f 70 72 69  ming an appropri
3a2c9 61 74 65 0d 0a 2a 2a 20 69 6e 64 65 78 20 65 78  ate..** index ex
3a2ca 69 73 74 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  isted...*/..stat
3a2cb 69 63 20 69 6e 74 20 74 65 72 6d 43 61 6e 44 72  ic int termCanDr
3a2cc 69 76 65 49 6e 64 65 78 28 0d 0a 20 20 57 68 65  iveIndex(..  Whe
3a2cd 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20  reTerm *pTerm,  
3a2ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
3a2cf 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
3a2d0 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0d 0a 20 20   to check */..  
3a2d1 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
3a2d2 74 65 6d 20 2a 70 53 72 63 2c 20 20 20 20 20 2f  tem *pSrc,     /
3a2d3 2a 20 54 61 62 6c 65 20 77 65 20 61 72 65 20 74  * Table we are t
3a2d4 72 79 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20  rying to access 
3a2d5 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f  */..  Bitmask no
3a2d6 74 52 65 61 64 79 20 20 20 20 20 20 20 20 20 20  tReady          
3a2d7 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 69       /* Tables i
3a2d8 6e 20 6f 75 74 65 72 20 6c 6f 6f 70 73 20 6f 66  n outer loops of
3a2d9 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0d 0a 29 7b   the join */..){
3a2da 0d 0a 20 20 63 68 61 72 20 61 66 66 3b 0d 0a 20  ..  char aff;.. 
3a2db 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
3a2dc 43 75 72 73 6f 72 21 3d 70 53 72 63 2d 3e 69 43  Cursor!=pSrc->iC
3a2dd 75 72 73 6f 72 20 29 20 72 65 74 75 72 6e 20 30  ursor ) return 0
3a2de 3b 0d 0a 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  ;..  if( pTerm->
3a2df 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51  eOperator!=WO_EQ
3a2e0 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20   ) return 0;..  
3a2e1 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
3a2e2 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61  eqRight & notRea
3a2e3 64 79 29 21 3d 30 20 29 20 72 65 74 75 72 6e 20  dy)!=0 ) return 
3a2e4 30 3b 0d 0a 20 20 61 66 66 20 3d 20 70 53 72 63  0;..  aff = pSrc
3a2e5 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 65  ->pTab->aCol[pTe
3a2e6 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e  rm->u.leftColumn
3a2e7 5d 2e 61 66 66 69 6e 69 74 79 3b 0d 0a 20 20 69  ].affinity;..  i
3a2e8 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
3a2e9 41 66 66 69 6e 69 74 79 4f 6b 28 70 54 65 72 6d  AffinityOk(pTerm
3a2ea 2d 3e 70 45 78 70 72 2c 20 61 66 66 29 20 29 20  ->pExpr, aff) ) 
3a2eb 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 72 65 74  return 0;..  ret
3a2ec 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  urn 1;..}..#endi
3a2ed 66 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  f....#ifndef SQL
3a2ee 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 4d 41 54  ITE_OMIT_AUTOMAT
3a2ef 49 43 5f 49 4e 44 45 58 0d 0a 2f 2a 0d 0a 2a 2a  IC_INDEX../*..**
3a2f0 20 49 66 20 74 68 65 20 71 75 65 72 79 20 70 6c   If the query pl
3a2f1 61 6e 20 66 6f 72 20 70 53 72 63 20 73 70 65 63  an for pSrc spec
3a2f2 69 66 69 65 64 20 69 6e 20 70 43 6f 73 74 20 69  ified in pCost i
3a2f3 73 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  s a full table s
3a2f4 63 61 6e 0d 0a 2a 2a 20 61 6e 64 20 69 6e 64 65  can..** and inde
3a2f5 78 69 6e 67 20 69 73 20 61 6c 6c 6f 77 73 20 28  xing is allows (
3a2f6 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4e  if there is no N
3a2f7 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73  OT INDEXED claus
3a2f8 65 29 20 61 6e 64 20 69 74 0d 0a 2a 2a 20 70 6f  e) and it..** po
3a2f9 73 73 69 62 6c 65 20 74 6f 20 63 6f 6e 73 74 72  ssible to constr
3a2fa 75 63 74 20 61 20 74 72 61 6e 73 69 65 6e 74 20  uct a transient 
3a2fb 69 6e 64 65 78 20 74 68 61 74 20 77 6f 75 6c 64  index that would
3a2fc 20 70 65 72 66 6f 72 6d 20 62 65 74 74 65 72 0d   perform better.
3a2fd 0a 2a 2a 20 74 68 61 6e 20 61 20 66 75 6c 6c 20  .** than a full 
3a2fe 74 61 62 6c 65 20 73 63 61 6e 20 65 76 65 6e 20  table scan even 
3a2ff 77 68 65 6e 20 74 68 65 20 63 6f 73 74 20 6f 66  when the cost of
3a300 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74 68   constructing th
3a301 65 20 69 6e 64 65 78 0d 0a 2a 2a 20 69 73 20 74  e index..** is t
3a302 61 6b 65 6e 20 69 6e 74 6f 20 61 63 63 6f 75 6e  aken into accoun
3a303 74 2c 20 74 68 65 6e 20 61 6c 74 65 72 20 74 68  t, then alter th
3a304 65 20 71 75 65 72 79 20 70 6c 61 6e 20 74 6f 20  e query plan to 
3a305 75 73 65 20 74 68 65 0d 0a 2a 2a 20 74 72 61 6e  use the..** tran
3a306 73 69 65 6e 74 20 69 6e 64 65 78 2e 0d 0a 2a 2f  sient index...*/
3a307 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  ..static void be
3a308 73 74 41 75 74 6f 6d 61 74 69 63 49 6e 64 65 78  stAutomaticIndex
3a309 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
3a30a 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
3a30b 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
3a30c 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 57 68  context */..  Wh
3a30d 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
3a30e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3a30f 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
3a310 0d 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ..  struct SrcLi
3a311 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20  st_item *pSrc,  
3a312 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
3a313 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63  se term to searc
3a314 68 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b 20  h */..  Bitmask 
3a315 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20  notReady,       
3a316 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63      /* Mask of c
3a317 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
3a318 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f  not available */
3a319 0d 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70  ..  WhereCost *p
3a31a 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Cost            
3a31b 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71  /* Lowest cost q
3a31c 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0d 0a 29 7b  uery plan */..){
3a31d 0d 0a 20 20 64 6f 75 62 6c 65 20 6e 54 61 62 6c  ..  double nTabl
3a31e 65 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  eRow;           
3a31f 2f 2a 20 52 6f 77 73 20 69 6e 20 74 68 65 20 69  /* Rows in the i
3a320 6e 70 75 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  nput table */.. 
3a321 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 3b 20 20 20   double logN;   
3a322 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a323 6c 6f 67 28 6e 54 61 62 6c 65 52 6f 77 29 20 2a  log(nTableRow) *
3a324 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 63 6f 73 74  /..  double cost
3a325 54 65 6d 70 49 64 78 3b 20 20 20 20 20 20 20 20  TempIdx;        
3a326 20 2f 2a 20 70 65 72 2d 71 75 65 72 79 20 63 6f   /* per-query co
3a327 73 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69  st of the transi
3a328 65 6e 74 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  ent index */..  
3a329 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
3a32a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
3a32b 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20   single term of 
3a32c 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3a32d 20 2a 2f 0d 0a 20 20 57 68 65 72 65 54 65 72 6d   */..  WhereTerm
3a32e 20 2a 70 57 43 45 6e 64 3b 20 20 20 20 20 20 20   *pWCEnd;       
3a32f 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57 43     /* End of pWC
3a330 2d 3e 61 5b 5d 20 2a 2f 0d 0a 20 20 54 61 62 6c  ->a[] */..  Tabl
3a331 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 20 20 20  e *pTable;      
3a332 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
3a333 20 74 68 74 20 6d 69 67 68 74 20 62 65 20 69 6e   tht might be in
3a334 64 65 78 65 64 20 2a 2f 0d 0a 0d 0a 20 20 69 66  dexed */....  if
3a335 28 20 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79  ( pParse->nQuery
3a336 4c 6f 6f 70 3c 3d 28 64 6f 75 62 6c 65 29 31 20  Loop<=(double)1 
3a337 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 72 65  ){..    /* There
3a338 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20   is no point in 
3a339 62 75 69 6c 64 69 6e 67 20 61 6e 20 61 75 74 6f  building an auto
3a33a 6d 61 74 69 63 20 69 6e 64 65 78 20 66 6f 72 20  matic index for 
3a33b 61 20 73 69 6e 67 6c 65 20 73 63 61 6e 20 2a 2f  a single scan */
3a33c 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20  ..    return;.. 
3a33d 20 7d 0d 0a 20 20 69 66 28 20 28 70 50 61 72 73   }..  if( (pPars
3a33e 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
3a33f 51 4c 49 54 45 5f 41 75 74 6f 49 6e 64 65 78 29  QLITE_AutoIndex)
3a340 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41  ==0 ){..    /* A
3a341 75 74 6f 6d 61 74 69 63 20 69 6e 64 69 63 65 73  utomatic indices
3a342 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 61 74   are disabled at
3a343 20 72 75 6e 2d 74 69 6d 65 20 2a 2f 0d 0a 20 20   run-time */..  
3a344 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
3a345 20 20 69 66 28 20 28 70 43 6f 73 74 2d 3e 70 6c    if( (pCost->pl
3a346 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3a347 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29  RE_NOT_FULLSCAN)
3a348 21 3d 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 57  !=0 ){..    /* W
3a349 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 73  e already have s
3a34a 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 64 65  ome kind of inde
3a34b 78 20 69 6e 20 75 73 65 20 66 6f 72 20 74 68 69  x in use for thi
3a34c 73 20 71 75 65 72 79 2e 20 2a 2f 0d 0a 20 20 20  s query. */..   
3a34d 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
3a34e 20 69 66 28 20 70 53 72 63 2d 3e 6e 6f 74 49 6e   if( pSrc->notIn
3a34f 64 65 78 65 64 20 29 7b 0d 0a 20 20 20 20 2f 2a  dexed ){..    /*
3a350 20 54 68 65 20 4e 4f 54 20 49 4e 44 45 58 45 44   The NOT INDEXED
3a351 20 63 6c 61 75 73 65 20 61 70 70 65 61 72 73 20   clause appears 
3a352 69 6e 20 74 68 65 20 53 51 4c 2e 20 2a 2f 0d 0a  in the SQL. */..
3a353 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
3a354 0d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 69 73  ..  if( pSrc->is
3a355 43 6f 72 72 65 6c 61 74 65 64 20 29 7b 0d 0a 20  Correlated ){.. 
3a356 20 20 20 2f 2a 20 54 68 65 20 73 6f 75 72 63 65     /* The source
3a357 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64   is a correlated
3a358 20 73 75 62 2d 71 75 65 72 79 2e 20 4e 6f 20 70   sub-query. No p
3a359 6f 69 6e 74 20 69 6e 20 69 6e 64 65 78 69 6e 67  oint in indexing
3a35a 20 69 74 2e 20 2a 2f 0d 0a 20 20 20 20 72 65 74   it. */..    ret
3a35b 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61  urn;..  }....  a
3a35c 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
3a35d 51 75 65 72 79 4c 6f 6f 70 20 3e 3d 20 28 64 6f  QueryLoop >= (do
3a35e 75 62 6c 65 29 31 20 29 3b 0d 0a 20 20 70 54 61  uble)1 );..  pTa
3a35f 62 6c 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  ble = pSrc->pTab
3a360 3b 0d 0a 20 20 6e 54 61 62 6c 65 52 6f 77 20 3d  ;..  nTableRow =
3a361 20 70 54 61 62 6c 65 2d 3e 6e 52 6f 77 45 73 74   pTable->nRowEst
3a362 3b 0d 0a 20 20 6c 6f 67 4e 20 3d 20 65 73 74 4c  ;..  logN = estL
3a363 6f 67 28 6e 54 61 62 6c 65 52 6f 77 29 3b 0d 0a  og(nTableRow);..
3a364 20 20 63 6f 73 74 54 65 6d 70 49 64 78 20 3d 20    costTempIdx = 
3a365 32 2a 6c 6f 67 4e 2a 28 6e 54 61 62 6c 65 52 6f  2*logN*(nTableRo
3a366 77 2f 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79  w/pParse->nQuery
3a367 4c 6f 6f 70 20 2b 20 31 29 3b 0d 0a 20 20 69 66  Loop + 1);..  if
3a368 28 20 63 6f 73 74 54 65 6d 70 49 64 78 3e 3d 70  ( costTempIdx>=p
3a369 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0d 0a  Cost->rCost ){..
3a36a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73 74 20      /* The cost 
3a36b 6f 66 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  of creating the 
3a36c 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
3a36d 77 6f 75 6c 64 20 62 65 20 67 72 65 61 74 65 72  would be greater
3a36e 20 74 68 61 6e 0d 0a 20 20 20 20 2a 2a 20 64 6f   than..    ** do
3a36f 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 74 61 62  ing the full tab
3a370 6c 65 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 20 20  le scan */..    
3a371 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a  return;..  }....
3a372 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20    /* Search for 
3a373 61 6e 79 20 65 71 75 61 6c 69 74 79 20 63 6f 6d  any equality com
3a374 70 61 72 69 73 6f 6e 20 74 65 72 6d 20 2a 2f 0d  parison term */.
3a375 0a 20 20 70 57 43 45 6e 64 20 3d 20 26 70 57 43  .  pWCEnd = &pWC
3a376 2d 3e 61 5b 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b  ->a[pWC->nTerm];
3a377 0d 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57  ..  for(pTerm=pW
3a378 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 57 43 45  C->a; pTerm<pWCE
3a379 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b 0d 0a 20  nd; pTerm++){.. 
3a37a 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e 44 72     if( termCanDr
3a37b 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d 2c 20  iveIndex(pTerm, 
3a37c 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 29 20  pSrc, notReady) 
3a37d 29 7b 0d 0a 20 20 20 20 20 20 57 48 45 52 45 54  ){..      WHERET
3a37e 52 41 43 45 28 28 22 61 75 74 6f 2d 69 6e 64 65  RACE(("auto-inde
3a37f 78 20 72 65 64 75 63 65 73 20 63 6f 73 74 20 66  x reduces cost f
3a380 72 6f 6d 20 25 2e 31 66 20 74 6f 20 25 2e 31 66  rom %.1f to %.1f
3a381 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  \n",..          
3a382 20 20 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d            pCost-
3a383 3e 72 43 6f 73 74 2c 20 63 6f 73 74 54 65 6d 70  >rCost, costTemp
3a384 49 64 78 29 29 3b 0d 0a 20 20 20 20 20 20 70 43  Idx));..      pC
3a385 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73  ost->rCost = cos
3a386 74 54 65 6d 70 49 64 78 3b 0d 0a 20 20 20 20 20  tTempIdx;..     
3a387 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 52 6f   pCost->plan.nRo
3a388 77 20 3d 20 6c 6f 67 4e 20 2b 20 31 3b 0d 0a 20  w = logN + 1;.. 
3a389 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e       pCost->plan
3a38a 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45  .wsFlags = WHERE
3a38b 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0d 0a 20 20  _TEMP_INDEX;..  
3a38c 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20      pCost->used 
3a38d 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52  = pTerm->prereqR
3a38e 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 62 72 65  ight;..      bre
3a38f 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ak;..    }..  }.
3a390 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65 66  .}..#else..# def
3a391 69 6e 65 20 62 65 73 74 41 75 74 6f 6d 61 74 69  ine bestAutomati
3a392 63 49 6e 64 65 78 28 41 2c 42 2c 43 2c 44 2c 45  cIndex(A,B,C,D,E
3a393 29 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0d 0a  )  /* no-op */..
3a394 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
3a395 5f 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f  _OMIT_AUTOMATIC_
3a396 49 4e 44 45 58 20 2a 2f 0d 0a 0d 0a 0d 0a 23 69  INDEX */......#i
3a397 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3a398 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45  T_AUTOMATIC_INDE
3a399 58 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  X../*..** Genera
3a39a 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 6e 73 74  te code to const
3a39b 72 75 63 74 20 74 68 65 20 49 6e 64 65 78 20 6f  ruct the Index o
3a39c 62 6a 65 63 74 20 66 6f 72 20 61 6e 20 61 75 74  bject for an aut
3a39d 6f 6d 61 74 69 63 20 69 6e 64 65 78 0d 0a 2a 2a  omatic index..**
3a39e 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74   and to set up t
3a39f 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62  he WhereLevel ob
3a3a0 6a 65 63 74 20 70 4c 65 76 65 6c 20 73 6f 20 74  ject pLevel so t
3a3a1 68 61 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e  hat the code gen
3a3a2 65 72 61 74 6f 72 0d 0a 2a 2a 20 6d 61 6b 65 73  erator..** makes
3a3a3 20 75 73 65 20 6f 66 20 74 68 65 20 61 75 74 6f   use of the auto
3a3a4 6d 61 74 69 63 20 69 6e 64 65 78 2e 0d 0a 2a 2f  matic index...*/
3a3a5 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  ..static void co
3a3a6 6e 73 74 72 75 63 74 41 75 74 6f 6d 61 74 69 63  nstructAutomatic
3a3a7 49 6e 64 65 78 28 0d 0a 20 20 50 61 72 73 65 20  Index(..  Parse 
3a3a8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
3a3a9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
3a3aa 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d  sing context */.
3a3ab 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
3a3ac 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pWC,           /
3a3ad 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
3a3ae 73 65 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  se */..  struct 
3a3af 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53  SrcList_item *pS
3a3b0 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d  rc,  /* The FROM
3a3b1 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20   clause term to 
3a3b2 67 65 74 20 74 68 65 20 6e 65 78 74 20 69 6e 64  get the next ind
3a3b3 65 78 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b  ex */..  Bitmask
3a3b4 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
3a3b5 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
3a3b6 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65  cursors that are
3a3b7 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a   not available *
3a3b8 2f 0d 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  /..  WhereLevel 
3a3b9 2a 70 4c 65 76 65 6c 20 20 20 20 20 20 20 20 20  *pLevel         
3a3ba 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 69 6e   /* Write new in
3a3bb 64 65 78 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d  dex here */..){.
3a3bc 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20  .  int nColumn; 
3a3bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a3be 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
3a3bf 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74  mns in the const
3a3c0 72 75 63 74 65 64 20 69 6e 64 65 78 20 2a 2f 0d  ructed index */.
3a3c1 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
3a3c2 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  erm;           /
3a3c3 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  * A single term 
3a3c4 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
3a3c5 75 73 65 20 2a 2f 0d 0a 20 20 57 68 65 72 65 54  use */..  WhereT
3a3c6 65 72 6d 20 2a 70 57 43 45 6e 64 3b 20 20 20 20  erm *pWCEnd;    
3a3c7 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
3a3c8 70 57 43 2d 3e 61 5b 5d 20 2a 2f 0d 0a 20 20 69  pWC->a[] */..  i
3a3c9 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
3a3ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
3a3cb 74 65 20 6f 66 20 6d 65 6d 6f 72 79 20 6e 65 65  te of memory nee
3a3cc 64 65 64 20 66 6f 72 20 70 49 64 78 20 2a 2f 0d  ded for pIdx */.
3a3cd 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
3a3ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a3cf 2a 20 4f 62 6a 65 63 74 20 64 65 73 63 72 69 62  * Object describ
3a3d0 69 6e 67 20 74 68 65 20 74 72 61 6e 73 69 65 6e  ing the transien
3a3d1 74 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 56 64  t index */..  Vd
3a3d2 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
3a3d3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
3a3d4 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
3a3d5 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
3a3d6 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64 64  on */..  int add
3a3d7 72 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20  rInit;          
3a3d8 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
3a3d9 6f 66 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  of the initializ
3a3da 61 74 69 6f 6e 20 62 79 70 61 73 73 20 6a 75 6d  ation bypass jum
3a3db 70 20 2a 2f 0d 0a 20 20 54 61 62 6c 65 20 2a 70  p */..  Table *p
3a3dc 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
3a3dd 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
3a3de 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a   being indexed *
3a3df 2f 0d 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  /..  KeyInfo *pK
3a3e0 65 79 69 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  eyinfo;         
3a3e1 20 2f 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74   /* Key informat
3a3e2 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6e 64 65  ion for the inde
3a3e3 78 20 2a 2f 20 20 20 0d 0a 20 20 69 6e 74 20 61  x */   ..  int a
3a3e4 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20 20 20  ddrTop;         
3a3e5 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66         /* Top of
3a3e6 20 74 68 65 20 69 6e 64 65 78 20 66 69 6c 6c 20   the index fill 
3a3e7 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  loop */..  int r
3a3e8 65 67 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20  egRecord;       
3a3e9 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
3a3ea 65 72 20 68 6f 6c 64 69 6e 67 20 61 6e 20 69 6e  er holding an in
3a3eb 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0d 0a 20  dex record */.. 
3a3ec 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
3a3ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a3ee 43 6f 6c 75 6d 6e 20 63 6f 75 6e 74 65 72 20 2a  Column counter *
3a3ef 2f 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  /..  int i;     
3a3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a3f1 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
3a3f2 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 42 69 74   */..  int mxBit
3a3f3 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
3a3f4 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 63 6f     /* Maximum co
3a3f5 6c 75 6d 6e 20 69 6e 20 70 53 72 63 2d 3e 63 6f  lumn in pSrc->co
3a3f6 6c 55 73 65 64 20 2a 2f 0d 0a 20 20 43 6f 6c 6c  lUsed */..  Coll
3a3f7 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
3a3f8 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61          /* Colla
3a3f9 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f  ting sequence to
3a3fa 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0d   on a column */.
3a3fb 0a 20 20 42 69 74 6d 61 73 6b 20 69 64 78 43 6f  .  Bitmask idxCo
3a3fc 6c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ls;            /
3a3fd 2a 20 42 69 74 6d 61 70 20 6f 66 20 63 6f 6c 75  * Bitmap of colu
3a3fe 6d 6e 73 20 75 73 65 64 20 66 6f 72 20 69 6e 64  mns used for ind
3a3ff 65 78 69 6e 67 20 2a 2f 0d 0a 20 20 42 69 74 6d  exing */..  Bitm
3a400 61 73 6b 20 65 78 74 72 61 43 6f 6c 73 3b 20 20  ask extraCols;  
3a401 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61          /* Bitma
3a402 70 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  p of additional 
3a403 63 6f 6c 75 6d 6e 73 20 2a 2f 0d 0a 0d 0a 20 20  columns */....  
3a404 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
3a405 20 74 6f 20 73 6b 69 70 20 6f 76 65 72 20 74 68   to skip over th
3a406 65 20 63 72 65 61 74 69 6f 6e 20 61 6e 64 20 69  e creation and i
3a407 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
3a408 20 74 68 65 0d 0a 20 20 2a 2a 20 74 72 61 6e 73   the..  ** trans
3a409 69 65 6e 74 20 69 6e 64 65 78 20 6f 6e 20 32 6e  ient index on 2n
3a40a 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
3a40b 20 69 74 65 72 61 74 69 6f 6e 73 20 6f 66 20 74   iterations of t
3a40c 68 65 20 6c 6f 6f 70 2e 20 2a 2f 0d 0a 20 20 76  he loop. */..  v
3a40d 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
3a40e 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  ;..  assert( v!=
3a40f 30 20 29 3b 0d 0a 20 20 61 64 64 72 49 6e 69 74  0 );..  addrInit
3a410 20 3d 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e   = sqlite3CodeOn
3a411 63 65 28 70 50 61 72 73 65 29 3b 0d 0a 0d 0a 20  ce(pParse);.... 
3a412 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75   /* Count the nu
3a413 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
3a414 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 64 64  that will be add
3a415 65 64 20 74 6f 20 74 68 65 20 69 6e 64 65 78 0d  ed to the index.
3a416 0a 20 20 2a 2a 20 61 6e 64 20 75 73 65 64 20 74  .  ** and used t
3a417 6f 20 6d 61 74 63 68 20 57 48 45 52 45 20 63 6c  o match WHERE cl
3a418 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
3a419 20 2a 2f 0d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d   */..  nColumn =
3a41a 20 30 3b 0d 0a 20 20 70 54 61 62 6c 65 20 3d 20   0;..  pTable = 
3a41b 70 53 72 63 2d 3e 70 54 61 62 3b 0d 0a 20 20 70  pSrc->pTab;..  p
3a41c 57 43 45 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b  WCEnd = &pWC->a[
3a41d 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0d 0a 20 20  pWC->nTerm];..  
3a41e 69 64 78 43 6f 6c 73 20 3d 20 30 3b 0d 0a 20 20  idxCols = 0;..  
3a41f 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61  for(pTerm=pWC->a
3a420 3b 20 70 54 65 72 6d 3c 70 57 43 45 6e 64 3b 20  ; pTerm<pWCEnd; 
3a421 70 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20 20 69  pTerm++){..    i
3a422 66 28 20 74 65 72 6d 43 61 6e 44 72 69 76 65 49  f( termCanDriveI
3a423 6e 64 65 78 28 70 54 65 72 6d 2c 20 70 53 72 63  ndex(pTerm, pSrc
3a424 2c 20 6e 6f 74 52 65 61 64 79 29 20 29 7b 0d 0a  , notReady) ){..
3a425 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
3a426 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f   pTerm->u.leftCo
3a427 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 42 69 74  lumn;..      Bit
3a428 6d 61 73 6b 20 63 4d 61 73 6b 20 3d 20 69 43 6f  mask cMask = iCo
3a429 6c 3e 3d 42 4d 53 20 3f 20 28 28 42 69 74 6d 61  l>=BMS ? ((Bitma
3a42a 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31 29 20 3a  sk)1)<<(BMS-1) :
3a42b 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 69   ((Bitmask)1)<<i
3a42c 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 74 65 73 74  Col;..      test
3a42d 63 61 73 65 28 20 69 43 6f 6c 3d 3d 42 4d 53 20  case( iCol==BMS 
3a42e 29 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  );..      testca
3a42f 73 65 28 20 69 43 6f 6c 3d 3d 42 4d 53 2d 31 20  se( iCol==BMS-1 
3a430 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 69  );..      if( (i
3a431 64 78 43 6f 6c 73 20 26 20 63 4d 61 73 6b 29 3d  dxCols & cMask)=
3a432 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 6e  =0 ){..        n
3a433 43 6f 6c 75 6d 6e 2b 2b 3b 0d 0a 20 20 20 20 20  Column++;..     
3a434 20 20 20 69 64 78 43 6f 6c 73 20 7c 3d 20 63 4d     idxCols |= cM
3a435 61 73 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ask;..      }.. 
3a436 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 61 73 73     }..  }..  ass
3a437 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29  ert( nColumn>0 )
3a438 3b 0d 0a 20 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ;..  pLevel->pla
3a439 6e 2e 6e 45 71 20 3d 20 6e 43 6f 6c 75 6d 6e 3b  n.nEq = nColumn;
3a43a 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74  ....  /* Count t
3a43b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 64 64  he number of add
3a43c 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20  itional columns 
3a43d 6e 65 65 64 65 64 20 74 6f 20 63 72 65 61 74 65  needed to create
3a43e 20 61 0d 0a 20 20 2a 2a 20 63 6f 76 65 72 69 6e   a..  ** coverin
3a43f 67 20 69 6e 64 65 78 2e 20 20 41 20 22 63 6f 76  g index.  A "cov
3a440 65 72 69 6e 67 20 69 6e 64 65 78 22 20 69 73 20  ering index" is 
3a441 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f  an index that co
3a442 6e 74 61 69 6e 73 20 61 6c 6c 0d 0a 20 20 2a 2a  ntains all..  **
3a443 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 61 72   columns that ar
3a444 65 20 6e 65 65 64 65 64 20 62 79 20 74 68 65 20  e needed by the 
3a445 71 75 65 72 79 2e 20 20 57 69 74 68 20 61 20 63  query.  With a c
3a446 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 2c 20 74  overing index, t
3a447 68 65 0d 0a 20 20 2a 2a 20 6f 72 69 67 69 6e 61  he..  ** origina
3a448 6c 20 74 61 62 6c 65 20 6e 65 76 65 72 20 6e 65  l table never ne
3a449 65 64 73 20 74 6f 20 62 65 20 61 63 63 65 73 73  eds to be access
3a44a 65 64 2e 20 20 41 75 74 6f 6d 61 74 69 63 20 69  ed.  Automatic i
3a44b 6e 64 69 63 65 73 20 6d 75 73 74 0d 0a 20 20 2a  ndices must..  *
3a44c 2a 20 62 65 20 61 20 63 6f 76 65 72 69 6e 67 20  * be a covering 
3a44d 69 6e 64 65 78 20 62 65 63 61 75 73 65 20 74 68  index because th
3a44e 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 6e 6f 74  e index will not
3a44f 20 62 65 20 75 70 64 61 74 65 64 20 69 66 20 74   be updated if t
3a450 68 65 0d 0a 20 20 2a 2a 20 6f 72 69 67 69 6e 61  he..  ** origina
3a451 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20  l table changes 
3a452 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 61 6e  and the index an
3a453 64 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62  d table cannot b
3a454 6f 74 68 20 62 65 20 75 73 65 64 0d 0a 20 20 2a  oth be used..  *
3a455 2a 20 69 66 20 74 68 65 79 20 67 6f 20 6f 75 74  * if they go out
3a456 20 6f 66 20 73 79 6e 63 2e 0d 0a 20 20 2a 2f 0d   of sync...  */.
3a457 0a 20 20 65 78 74 72 61 43 6f 6c 73 20 3d 20 70  .  extraCols = p
3a458 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20 26 20 28  Src->colUsed & (
3a459 7e 69 64 78 43 6f 6c 73 20 7c 20 28 28 28 42 69  ~idxCols | (((Bi
3a45a 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31  tmask)1)<<(BMS-1
3a45b 29 29 29 3b 0d 0a 20 20 6d 78 42 69 74 43 6f 6c  )));..  mxBitCol
3a45c 20 3d 20 28 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c   = (pTable->nCol
3a45d 20 3e 3d 20 42 4d 53 2d 31 29 20 3f 20 42 4d 53   >= BMS-1) ? BMS
3a45e 2d 31 20 3a 20 70 54 61 62 6c 65 2d 3e 6e 43 6f  -1 : pTable->nCo
3a45f 6c 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20  l;..  testcase( 
3a460 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3d 3d 42 4d  pTable->nCol==BM
3a461 53 2d 31 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  S-1 );..  testca
3a462 73 65 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  se( pTable->nCol
3a463 3d 3d 42 4d 53 2d 32 20 29 3b 0d 0a 20 20 66 6f  ==BMS-2 );..  fo
3a464 72 28 69 3d 30 3b 20 69 3c 6d 78 42 69 74 43 6f  r(i=0; i<mxBitCo
3a465 6c 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  l; i++){..    if
3a466 28 20 65 78 74 72 61 43 6f 6c 73 20 26 20 28 28  ( extraCols & ((
3a467 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 69 29 20  (Bitmask)1)<<i) 
3a468 29 20 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0d 0a 20 20  ) nColumn++;..  
3a469 7d 0d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 63  }..  if( pSrc->c
3a46a 6f 6c 55 73 65 64 20 26 20 28 28 28 42 69 74 6d  olUsed & (((Bitm
3a46b 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31 29 29  ask)1)<<(BMS-1))
3a46c 20 29 7b 0d 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e   ){..    nColumn
3a46d 20 2b 3d 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c   += pTable->nCol
3a46e 20 2d 20 42 4d 53 20 2b 20 31 3b 0d 0a 20 20 7d   - BMS + 1;..  }
3a46f 0d 0a 20 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  ..  pLevel->plan
3a470 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52  .wsFlags |= WHER
3a471 45 5f 43 4f 4c 55 4d 4e 5f 45 51 20 7c 20 57 48  E_COLUMN_EQ | WH
3a472 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 7c 20 57  ERE_IDX_ONLY | W
3a473 4f 5f 45 51 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 6f  O_EQ;....  /* Co
3a474 6e 73 74 72 75 63 74 20 74 68 65 20 49 6e 64 65  nstruct the Inde
3a475 78 20 6f 62 6a 65 63 74 20 74 6f 20 64 65 73 63  x object to desc
3a476 72 69 62 65 20 74 68 69 73 20 69 6e 64 65 78 20  ribe this index 
3a477 2a 2f 0d 0a 20 20 6e 42 79 74 65 20 3d 20 73 69  */..  nByte = si
3a478 7a 65 6f 66 28 49 6e 64 65 78 29 3b 0d 0a 20 20  zeof(Index);..  
3a479 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75 6d 6e  nByte += nColumn
3a47a 2a 73 69 7a 65 6f 66 28 69 6e 74 29 3b 20 20 20  *sizeof(int);   
3a47b 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c    /* Index.aiCol
3a47c 75 6d 6e 20 2a 2f 0d 0a 20 20 6e 42 79 74 65 20  umn */..  nByte 
3a47d 2b 3d 20 6e 43 6f 6c 75 6d 6e 2a 73 69 7a 65 6f  += nColumn*sizeo
3a47e 66 28 63 68 61 72 2a 29 3b 20 20 20 2f 2a 20 49  f(char*);   /* I
3a47f 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 2a 2f 0d 0a  ndex.azColl */..
3a480 20 20 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75    nByte += nColu
3a481 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
3a482 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f      /* Index.aSo
3a483 72 74 4f 72 64 65 72 20 2a 2f 0d 0a 20 20 70 49  rtOrder */..  pI
3a484 64 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  dx = sqlite3DbMa
3a485 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d  llocZero(pParse-
3a486 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20  >db, nByte);..  
3a487 69 66 28 20 70 49 64 78 3d 3d 30 20 29 20 72 65  if( pIdx==0 ) re
3a488 74 75 72 6e 3b 0d 0a 20 20 70 4c 65 76 65 6c 2d  turn;..  pLevel-
3a489 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70  >plan.u.pIdx = p
3a48a 49 64 78 3b 0d 0a 20 20 70 49 64 78 2d 3e 61 7a  Idx;..  pIdx->az
3a48b 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a 2a 29 26  Coll = (char**)&
3a48c 70 49 64 78 5b 31 5d 3b 0d 0a 20 20 70 49 64 78  pIdx[1];..  pIdx
3a48d 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e  ->aiColumn = (in
3a48e 74 2a 29 26 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  t*)&pIdx->azColl
3a48f 5b 6e 43 6f 6c 75 6d 6e 5d 3b 0d 0a 20 20 70 49  [nColumn];..  pI
3a490 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  dx->aSortOrder =
3a491 20 28 75 38 2a 29 26 70 49 64 78 2d 3e 61 69 43   (u8*)&pIdx->aiC
3a492 6f 6c 75 6d 6e 5b 6e 43 6f 6c 75 6d 6e 5d 3b 0d  olumn[nColumn];.
3a493 0a 20 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 3d  .  pIdx->zName =
3a494 20 22 61 75 74 6f 2d 69 6e 64 65 78 22 3b 0d 0a   "auto-index";..
3a495 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20    pIdx->nColumn 
3a496 3d 20 6e 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 70 49  = nColumn;..  pI
3a497 64 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61  dx->pTable = pTa
3a498 62 6c 65 3b 0d 0a 20 20 6e 20 3d 20 30 3b 0d 0a  ble;..  n = 0;..
3a499 20 20 69 64 78 43 6f 6c 73 20 3d 20 30 3b 0d 0a    idxCols = 0;..
3a49a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d    for(pTerm=pWC-
3a49b 3e 61 3b 20 70 54 65 72 6d 3c 70 57 43 45 6e 64  >a; pTerm<pWCEnd
3a49c 3b 20 70 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20  ; pTerm++){..   
3a49d 20 69 66 28 20 74 65 72 6d 43 61 6e 44 72 69 76   if( termCanDriv
3a49e 65 49 6e 64 65 78 28 70 54 65 72 6d 2c 20 70 53  eIndex(pTerm, pS
3a49f 72 63 2c 20 6e 6f 74 52 65 61 64 79 29 20 29 7b  rc, notReady) ){
3a4a0 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ..      int iCol
3a4a1 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74   = pTerm->u.left
3a4a2 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 42  Column;..      B
3a4a3 69 74 6d 61 73 6b 20 63 4d 61 73 6b 20 3d 20 69  itmask cMask = i
3a4a4 43 6f 6c 3e 3d 42 4d 53 20 3f 20 28 28 42 69 74  Col>=BMS ? ((Bit
3a4a5 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31 29  mask)1)<<(BMS-1)
3a4a6 20 3a 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c   : ((Bitmask)1)<
3a4a7 3c 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 69 66  <iCol;..      if
3a4a8 28 20 28 69 64 78 43 6f 6c 73 20 26 20 63 4d 61  ( (idxCols & cMa
3a4a9 73 6b 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  sk)==0 ){..     
3a4aa 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54     Expr *pX = pT
3a4ab 65 72 6d 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20  erm->pExpr;..   
3a4ac 20 20 20 20 20 69 64 78 43 6f 6c 73 20 7c 3d 20       idxCols |= 
3a4ad 63 4d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 20 20  cMask;..        
3a4ae 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  pIdx->aiColumn[n
3a4af 5d 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66  ] = pTerm->u.lef
3a4b0 74 43 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20  tColumn;..      
3a4b1 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
3a4b2 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
3a4b3 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58  llSeq(pParse, pX
3a4b4 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69  ->pLeft, pX->pRi
3a4b5 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  ght);..        p
3a4b6 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 5d 20 3d  Idx->azColl[n] =
3a4b7 20 41 4c 57 41 59 53 28 70 43 6f 6c 6c 29 20 3f   ALWAYS(pColl) ?
3a4b8 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 20 3a 20   pColl->zName : 
3a4b9 22 42 49 4e 41 52 59 22 3b 0d 0a 20 20 20 20 20  "BINARY";..     
3a4ba 20 20 20 6e 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d     n++;..      }
3a4bb 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
3a4bc 61 73 73 65 72 74 28 20 28 75 33 32 29 6e 3d 3d  assert( (u32)n==
3a4bd 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71  pLevel->plan.nEq
3a4be 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 64 64 20   );....  /* Add 
3a4bf 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  additional colum
3a4c0 6e 73 20 6e 65 65 64 65 64 20 74 6f 20 6d 61 6b  ns needed to mak
3a4c1 65 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20  e the automatic 
3a4c2 69 6e 64 65 78 20 69 6e 74 6f 0d 0a 20 20 2a 2a  index into..  **
3a4c3 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65   a covering inde
3a4c4 78 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b  x */..  for(i=0;
3a4c5 20 69 3c 6d 78 42 69 74 43 6f 6c 3b 20 69 2b 2b   i<mxBitCol; i++
3a4c6 29 7b 0d 0a 20 20 20 20 69 66 28 20 65 78 74 72  ){..    if( extr
3a4c7 61 43 6f 6c 73 20 26 20 28 28 28 42 69 74 6d 61  aCols & (((Bitma
3a4c8 73 6b 29 31 29 3c 3c 69 29 20 29 7b 0d 0a 20 20  sk)1)<<i) ){..  
3a4c9 20 20 20 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75      pIdx->aiColu
3a4ca 6d 6e 5b 6e 5d 20 3d 20 69 3b 0d 0a 20 20 20 20  mn[n] = i;..    
3a4cb 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e    pIdx->azColl[n
3a4cc 5d 20 3d 20 22 42 49 4e 41 52 59 22 3b 0d 0a 20  ] = "BINARY";.. 
3a4cd 20 20 20 20 20 6e 2b 2b 3b 0d 0a 20 20 20 20 7d       n++;..    }
3a4ce 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 53 72  ..  }..  if( pSr
3a4cf 63 2d 3e 63 6f 6c 55 73 65 64 20 26 20 28 28 28  c->colUsed & (((
3a4d0 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53  Bitmask)1)<<(BMS
3a4d1 2d 31 29 29 20 29 7b 0d 0a 20 20 20 20 66 6f 72  -1)) ){..    for
3a4d2 28 69 3d 42 4d 53 2d 31 3b 20 69 3c 70 54 61 62  (i=BMS-1; i<pTab
3a4d3 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0d  le->nCol; i++){.
3a4d4 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 61 69 43  .      pIdx->aiC
3a4d5 6f 6c 75 6d 6e 5b 6e 5d 20 3d 20 69 3b 0d 0a 20  olumn[n] = i;.. 
3a4d6 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c       pIdx->azCol
3a4d7 6c 5b 6e 5d 20 3d 20 22 42 49 4e 41 52 59 22 3b  l[n] = "BINARY";
3a4d8 0d 0a 20 20 20 20 20 20 6e 2b 2b 3b 0d 0a 20 20  ..      n++;..  
3a4d9 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65    }..  }..  asse
3a4da 72 74 28 20 6e 3d 3d 6e 43 6f 6c 75 6d 6e 20 29  rt( n==nColumn )
3a4db 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65  ;....  /* Create
3a4dc 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 69   the automatic i
3a4dd 6e 64 65 78 20 2a 2f 0d 0a 20 20 70 4b 65 79 69  ndex */..  pKeyi
3a4de 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  nfo = sqlite3Ind
3a4df 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
3a4e0 2c 20 70 49 64 78 29 3b 0d 0a 20 20 61 73 73 65  , pIdx);..  asse
3a4e1 72 74 28 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  rt( pLevel->iIdx
3a4e2 43 75 72 3e 3d 30 20 29 3b 0d 0a 20 20 73 71 6c  Cur>=0 );..  sql
3a4e3 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
3a4e4 2c 20 4f 50 5f 4f 70 65 6e 41 75 74 6f 69 6e 64  , OP_OpenAutoind
3a4e5 65 78 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  ex, pLevel->iIdx
3a4e6 43 75 72 2c 20 6e 43 6f 6c 75 6d 6e 2b 31 2c 20  Cur, nColumn+1, 
3a4e7 30 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  0,..            
3a4e8 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
3a4e9 4b 65 79 69 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  Keyinfo, P4_KEYI
3a4ea 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0d 0a 20  NFO_HANDOFF);.. 
3a4eb 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
3a4ec 20 22 66 6f 72 20 25 73 22 2c 20 70 54 61 62 6c   "for %s", pTabl
3a4ed 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 0d 0a 20  e->zName));.... 
3a4ee 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 61 75 74   /* Fill the aut
3a4ef 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 77 69 74  omatic index wit
3a4f0 68 20 63 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 20 20  h content */..  
3a4f1 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65  addrTop = sqlite
3a4f2 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
3a4f3 50 5f 52 65 77 69 6e 64 2c 20 70 4c 65 76 65 6c  P_Rewind, pLevel
3a4f4 2d 3e 69 54 61 62 43 75 72 29 3b 0d 0a 20 20 72  ->iTabCur);..  r
3a4f5 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74  egRecord = sqlit
3a4f6 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
3a4f7 72 73 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  rse);..  sqlite3
3a4f8 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
3a4f9 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 70  (pParse, pIdx, p
3a4fa 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 2c 20  Level->iTabCur, 
3a4fb 72 65 67 52 65 63 6f 72 64 2c 20 31 29 3b 0d 0a  regRecord, 1);..
3a4fc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3a4fd 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
3a4fe 65 72 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ert, pLevel->iId
3a4ff 78 43 75 72 2c 20 72 65 67 52 65 63 6f 72 64 29  xCur, regRecord)
3a500 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ;..  sqlite3Vdbe
3a501 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
3a502 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
3a503 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
3a504 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
3a505 78 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62  xt, pLevel->iTab
3a506 43 75 72 2c 20 61 64 64 72 54 6f 70 2b 31 29 3b  Cur, addrTop+1);
3a507 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ..  sqlite3VdbeC
3a508 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
3a509 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54  E_STMTSTATUS_AUT
3a50a 4f 49 4e 44 45 58 29 3b 0d 0a 20 20 73 71 6c 69  OINDEX);..  sqli
3a50b 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
3a50c 76 2c 20 61 64 64 72 54 6f 70 29 3b 0d 0a 20 20  v, addrTop);..  
3a50d 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
3a50e 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
3a50f 67 52 65 63 6f 72 64 29 3b 0d 0a 20 20 0d 0a 20  gRecord);..  .. 
3a510 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 77 68   /* Jump here wh
3a511 65 6e 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20  en skipping the 
3a512 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a  initialization *
3a513 2f 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  /..  sqlite3Vdbe
3a514 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
3a515 49 6e 69 74 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69  Init);..}..#endi
3a516 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
3a517 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58  _AUTOMATIC_INDEX
3a518 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   */....#ifndef S
3a519 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
3a51a 41 4c 54 41 42 4c 45 0d 0a 2f 2a 0d 0a 2a 2a 20  ALTABLE../*..** 
3a51b 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70  Allocate and pop
3a51c 75 6c 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33  ulate an sqlite3
3a51d 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
3a51e 63 74 75 72 65 2e 20 49 74 20 69 73 20 74 68 65  cture. It is the
3a51f 20 0d 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69   ..** responsibi
3a520 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c  lity of the call
3a521 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79  er to eventually
3a522 20 72 65 6c 65 61 73 65 20 74 68 65 20 73 74 72   release the str
3a523 75 63 74 75 72 65 0d 0a 2a 2a 20 62 79 20 70 61  ucture..** by pa
3a524 73 73 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ssing the pointe
3a525 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  r returned by th
3a526 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 73  is function to s
3a527 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0d 0a  qlite3_free()...
3a528 2a 2f 0d 0a 73 74 61 74 69 63 20 73 71 6c 69 74  */..static sqlit
3a529 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 61  e3_index_info *a
3a52a 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f  llocateIndexInfo
3a52b 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
3a52c 73 65 2c 20 0d 0a 20 20 57 68 65 72 65 43 6c 61  se, ..  WhereCla
3a52d 75 73 65 20 2a 70 57 43 2c 0d 0a 20 20 73 74 72  use *pWC,..  str
3a52e 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
3a52f 20 2a 70 53 72 63 2c 0d 0a 20 20 45 78 70 72 4c   *pSrc,..  ExprL
3a530 69 73 74 20 2a 70 4f 72 64 65 72 42 79 0d 0a 29  ist *pOrderBy..)
3a531 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a  {..  int i, j;..
3a532 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0d 0a 20 20    int nTerm;..  
3a533 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
3a534 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
3a535 2a 70 49 64 78 43 6f 6e 73 3b 0d 0a 20 20 73 74  *pIdxCons;..  st
3a536 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
3a537 65 78 5f 6f 72 64 65 72 62 79 20 2a 70 49 64 78  ex_orderby *pIdx
3a538 4f 72 64 65 72 42 79 3b 0d 0a 20 20 73 74 72 75  OrderBy;..  stru
3a539 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
3a53a 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
3a53b 65 20 2a 70 55 73 61 67 65 3b 0d 0a 20 20 57 68  e *pUsage;..  Wh
3a53c 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0d  ereTerm *pTerm;.
3a53d 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
3a53e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ..  sqlite3_inde
3a53f 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f  x_info *pIdxInfo
3a540 3b 0d 0a 0d 0a 20 20 57 48 45 52 45 54 52 41 43  ;....  WHERETRAC
3a541 45 28 28 22 52 65 63 6f 6d 70 75 74 69 6e 67 20  E(("Recomputing 
3a542 69 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 25  index info for %
3a543 73 2e 2e 2e 5c 6e 22 2c 20 70 53 72 63 2d 3e 70  s...\n", pSrc->p
3a544 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 0d  Tab->zName));...
3a545 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20  .  /* Count the 
3a546 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69 62  number of possib
3a547 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
3a548 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66 65  constraints refe
3a549 72 72 69 6e 67 0d 0a 20 20 2a 2a 20 74 6f 20 74  rring..  ** to t
3a54a 68 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c  his virtual tabl
3a54b 65 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 6e 54  e */..  for(i=nT
3a54c 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43  erm=0, pTerm=pWC
3a54d 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72  ->a; i<pWC->nTer
3a54e 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29  m; i++, pTerm++)
3a54f 7b 0d 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  {..    if( pTerm
3a550 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20  ->leftCursor != 
3a551 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
3a552 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 61  continue;..    a
3a553 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 65  ssert( (pTerm->e
3a554 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d 2d  Operator&(pTerm-
3a555 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d 3d  >eOperator-1))==
3a556 30 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61  0 );..    testca
3a557 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  se( pTerm->eOper
3a558 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0d 0a  ator==WO_IN );..
3a559 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
3a55a 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
3a55b 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0d 0a 20 20  WO_ISNULL );..  
3a55c 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70    if( pTerm->eOp
3a55d 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c  erator & (WO_IN|
3a55e 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e  WO_ISNULL) ) con
3a55f 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28 20  tinue;..    if( 
3a560 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
3a561 20 54 45 52 4d 5f 56 4e 55 4c 4c 20 29 20 63 6f   TERM_VNULL ) co
3a562 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 6e 54 65  ntinue;..    nTe
3a563 72 6d 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rm++;..  }....  
3a564 2f 2a 20 49 66 20 74 68 65 20 4f 52 44 45 52 20  /* If the ORDER 
3a565 42 59 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69  BY clause contai
3a566 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20  ns only columns 
3a567 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 0d  in the current .
3a568 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  .  ** virtual ta
3a569 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  ble then allocat
3a56a 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
3a56b 61 4f 72 64 65 72 42 79 20 70 61 72 74 20 6f 66  aOrderBy part of
3a56c 0d 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74  ..  ** the sqlit
3a56d 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
3a56e 72 75 63 74 75 72 65 2e 0d 0a 20 20 2a 2f 0d 0a  ructure...  */..
3a56f 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0d    nOrderBy = 0;.
3a570 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20  .  if( pOrderBy 
3a571 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ){..    for(i=0;
3a572 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
3a573 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  pr; i++){..     
3a574 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
3a575 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
3a576 78 70 72 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  xpr;..      if( 
3a577 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pExpr->op!=TK_CO
3a578 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69  LUMN || pExpr->i
3a579 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 69 43 75  Table!=pSrc->iCu
3a57a 72 73 6f 72 20 29 20 62 72 65 61 6b 3b 0d 0a 20  rsor ) break;.. 
3a57b 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 69 3d     }..    if( i=
3a57c 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  =pOrderBy->nExpr
3a57d 20 29 7b 0d 0a 20 20 20 20 20 20 6e 4f 72 64 65   ){..      nOrde
3a57e 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  rBy = pOrderBy->
3a57f 6e 45 78 70 72 3b 0d 0a 20 20 20 20 7d 0d 0a 20  nExpr;..    }.. 
3a580 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63   }....  /* Alloc
3a581 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ate the sqlite3_
3a582 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
3a583 74 75 72 65 0d 0a 20 20 2a 2f 0d 0a 20 20 70 49  ture..  */..  pI
3a584 64 78 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  dxInfo = sqlite3
3a585 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61  DbMallocZero(pPa
3a586 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28  rse->db, sizeof(
3a587 2a 70 49 64 78 49 6e 66 6f 29 0d 0a 20 20 20 20  *pIdxInfo)..    
3a588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a589 20 20 20 20 20 20 20 2b 20 28 73 69 7a 65 6f 66         + (sizeof
3a58a 28 2a 70 49 64 78 43 6f 6e 73 29 20 2b 20 73 69  (*pIdxCons) + si
3a58b 7a 65 6f 66 28 2a 70 55 73 61 67 65 29 29 2a 6e  zeof(*pUsage))*n
3a58c 54 65 72 6d 0d 0a 20 20 20 20 20 20 20 20 20 20  Term..          
3a58d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a58e 20 2b 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 4f   + sizeof(*pIdxO
3a58f 72 64 65 72 42 79 29 2a 6e 4f 72 64 65 72 42 79  rderBy)*nOrderBy
3a590 20 29 3b 0d 0a 20 20 69 66 28 20 70 49 64 78 49   );..  if( pIdxI
3a591 6e 66 6f 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73  nfo==0 ){..    s
3a592 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
3a593 50 61 72 73 65 2c 20 22 6f 75 74 20 6f 66 20 6d  Parse, "out of m
3a594 65 6d 6f 72 79 22 29 3b 0d 0a 20 20 20 20 2f 2a  emory");..    /*
3a595 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61   (double)0 In ca
3a596 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
3a597 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
3a598 2e 2e 2e 20 2a 2f 0d 0a 20 20 20 20 72 65 74 75  ... */..    retu
3a599 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  rn 0;..  }....  
3a59a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
3a59b 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  e structure.  Th
3a59c 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
3a59d 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 63  info structure c
3a59e 6f 6e 74 61 69 6e 73 0d 0a 20 20 2a 2a 20 6d 61  ontains..  ** ma
3a59f 6e 79 20 66 69 65 6c 64 73 20 74 68 61 74 20 61  ny fields that a
3a5a0 72 65 20 64 65 63 6c 61 72 65 64 20 22 63 6f 6e  re declared "con
3a5a1 73 74 22 20 74 6f 20 70 72 65 76 65 6e 74 20 78  st" to prevent x
3a5a2 42 65 73 74 49 6e 64 65 78 20 66 72 6f 6d 0d 0a  BestIndex from..
3a5a3 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68    ** changing th
3a5a4 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  em.  We have to 
3a5a5 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61  do some funky ca
3a5a6 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 74  sting in order t
3a5a7 6f 0d 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  o..  ** initiali
3a5a8 7a 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73 2e  ze those fields.
3a5a9 0d 0a 20 20 2a 2f 0d 0a 20 20 70 49 64 78 43 6f  ..  */..  pIdxCo
3a5aa 6e 73 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c  ns = (struct sql
3a5ab 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
3a5ac 72 61 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  raint*)&pIdxInfo
3a5ad 5b 31 5d 3b 0d 0a 20 20 70 49 64 78 4f 72 64 65  [1];..  pIdxOrde
3a5ae 72 42 79 20 3d 20 28 73 74 72 75 63 74 20 73 71  rBy = (struct sq
3a5af 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65  lite3_index_orde
3a5b0 72 62 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e  rby*)&pIdxCons[n
3a5b1 54 65 72 6d 5d 3b 0d 0a 20 20 70 55 73 61 67 65  Term];..  pUsage
3a5b2 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
3a5b3 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
3a5b4 69 6e 74 5f 75 73 61 67 65 2a 29 26 70 49 64 78  int_usage*)&pIdx
3a5b5 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65 72 42 79  OrderBy[nOrderBy
3a5b6 5d 3b 0d 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49  ];..  *(int*)&pI
3a5b7 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  dxInfo->nConstra
3a5b8 69 6e 74 20 3d 20 6e 54 65 72 6d 3b 0d 0a 20 20  int = nTerm;..  
3a5b9 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  *(int*)&pIdxInfo
3a5ba 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72  ->nOrderBy = nOr
3a5bb 64 65 72 42 79 3b 0d 0a 20 20 2a 28 73 74 72 75  derBy;..  *(stru
3a5bc 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
3a5bd 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70  _constraint**)&p
3a5be 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
3a5bf 61 69 6e 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b  aint = pIdxCons;
3a5c0 0d 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c  ..  *(struct sql
3a5c1 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
3a5c2 62 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  by**)&pIdxInfo->
3a5c3 61 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78 4f  aOrderBy = pIdxO
3a5c4 72 64 65 72 42 79 3b 0d 0a 20 20 2a 28 73 74 72  rderBy;..  *(str
3a5c5 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
3a5c6 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
3a5c7 67 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  ge**)&pIdxInfo->
3a5c8 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
3a5c9 20 3d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   =..            
3a5ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5cd 20 20 20 20 20 20 20 70 55 73 61 67 65 3b 0d 0a         pUsage;..
3a5ce 0d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70  ..  for(i=j=0, p
3a5cf 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70  Term=pWC->a; i<p
3a5d0 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20  WC->nTerm; i++, 
3a5d1 70 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20 20 69  pTerm++){..    i
3a5d2 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
3a5d3 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43  rsor != pSrc->iC
3a5d4 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65  ursor ) continue
3a5d5 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ;..    assert( (
3a5d6 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
3a5d7 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  &(pTerm->eOperat
3a5d8 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0d 0a 20 20  or-1))==0 );..  
3a5d9 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
3a5da 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
3a5db 5f 49 4e 20 29 3b 0d 0a 20 20 20 20 74 65 73 74  _IN );..    test
3a5dc 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70  case( pTerm->eOp
3a5dd 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c  erator==WO_ISNUL
3a5de 4c 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 54  L );..    if( pT
3a5df 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
3a5e0 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c   (WO_IN|WO_ISNUL
3a5e1 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a  L) ) continue;..
3a5e2 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77      if( pTerm->w
3a5e3 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e  tFlags & TERM_VN
3a5e4 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d  ULL ) continue;.
3a5e5 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d  .    pIdxCons[j]
3a5e6 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d  .iColumn = pTerm
3a5e7 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0d  ->u.leftColumn;.
3a5e8 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d  .    pIdxCons[j]
3a5e9 2e 69 54 65 72 6d 4f 66 66 73 65 74 20 3d 20 69  .iTermOffset = i
3a5ea 3b 0d 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b  ;..    pIdxCons[
3a5eb 6a 5d 2e 6f 70 20 3d 20 28 75 38 29 70 54 65 72  j].op = (u8)pTer
3a5ec 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3b 0d 0a 20  m->eOperator;.. 
3a5ed 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 63 74     /* The direct
3a5ee 20 61 73 73 69 67 6e 6d 65 6e 74 20 69 6e 20 74   assignment in t
3a5ef 68 65 20 70 72 65 76 69 6f 75 73 20 6c 69 6e 65  he previous line
3a5f0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 6f 6e 6c   is possible onl
3a5f1 79 20 62 65 63 61 75 73 65 0d 0a 20 20 20 20 2a  y because..    *
3a5f2 2a 20 74 68 65 20 57 4f 5f 20 61 6e 64 20 53 51  * the WO_ and SQ
3a5f3 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
3a5f4 52 41 49 4e 54 5f 20 63 6f 64 65 73 20 61 72 65  RAINT_ codes are
3a5f5 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 54 68 65   identical.  The
3a5f6 0d 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69  ..    ** followi
3a5f7 6e 67 20 61 73 73 65 72 74 73 20 76 65 72 69 66  ng asserts verif
3a5f8 79 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0d  y this fact. */.
3a5f9 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f  .    assert( WO_
3a5fa 45 51 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  EQ==SQLITE_INDEX
3a5fb 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29  _CONSTRAINT_EQ )
3a5fc 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 57  ;..    assert( W
3a5fd 4f 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_LT==SQLITE_IND
3a5fe 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
3a5ff 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
3a600 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49   WO_LE==SQLITE_I
3a601 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
3a602 4c 45 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  LE );..    asser
3a603 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45  t( WO_GT==SQLITE
3a604 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
3a605 54 5f 47 54 20 29 3b 0d 0a 20 20 20 20 61 73 73  T_GT );..    ass
3a606 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49  ert( WO_GE==SQLI
3a607 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
3a608 49 4e 54 5f 47 45 20 29 3b 0d 0a 20 20 20 20 61  INT_GE );..    a
3a609 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d  ssert( WO_MATCH=
3a60a 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
3a60b 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29  NSTRAINT_MATCH )
3a60c 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
3a60d 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
3a60e 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57  & (WO_EQ|WO_LT|W
3a60f 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45  O_LE|WO_GT|WO_GE
3a610 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0d 0a 20  |WO_MATCH) );.. 
3a611 20 20 20 6a 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20     j++;..  }..  
3a612 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65  for(i=0; i<nOrde
3a613 72 42 79 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  rBy; i++){..    
3a614 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4f  Expr *pExpr = pO
3a615 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78  rderBy->a[i].pEx
3a616 70 72 3b 0d 0a 20 20 20 20 70 49 64 78 4f 72 64  pr;..    pIdxOrd
3a617 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 20  erBy[i].iColumn 
3a618 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
3a619 3b 0d 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72  ;..    pIdxOrder
3a61a 42 79 5b 69 5d 2e 64 65 73 63 20 3d 20 70 4f 72  By[i].desc = pOr
3a61b 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74  derBy->a[i].sort
3a61c 4f 72 64 65 72 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  Order;..  }.... 
3a61d 20 72 65 74 75 72 6e 20 70 49 64 78 49 6e 66 6f   return pIdxInfo
3a61e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
3a61f 68 65 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20  he table object 
3a620 72 65 66 65 72 65 6e 63 65 20 70 61 73 73 65 64  reference passed
3a621 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
3a622 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
3a623 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75 73  function..** mus
3a624 74 20 72 65 70 72 65 73 65 6e 74 20 61 20 76 69  t represent a vi
3a625 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 69  rtual table. Thi
3a626 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b  s function invok
3a627 65 73 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  es the xBestInde
3a628 78 28 29 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f  x()..** method o
3a629 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
3a62a 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 71 6c  ble with the sql
3a62b 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
3a62c 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 0d 0a  pointer passed..
3a62d 2a 2a 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ** as the argume
3a62e 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61  nt...**..** If a
3a62f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
3a630 70 50 61 72 73 65 20 69 73 20 70 6f 70 75 6c 61  pParse is popula
3a631 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ted with an erro
3a632 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 61 0d  r message and a.
3a633 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  .** non-zero val
3a634 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ue is returned. 
3a635 4f 74 68 65 72 77 69 73 65 2c 20 30 20 69 73 20  Otherwise, 0 is 
3a636 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
3a637 20 6f 75 74 70 75 74 0d 0a 2a 2a 20 70 61 72 74   output..** part
3a638 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
3a639 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
3a63a 74 75 72 65 20 69 73 20 6c 65 66 74 20 70 6f 70  ture is left pop
3a63b 75 6c 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ulated...**..** 
3a63c 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  Whether or not a
3a63d 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
3a63e 6e 65 64 2c 20 69 74 20 69 73 20 74 68 65 20 72  ned, it is the r
3a63f 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
3a640 20 74 68 65 0d 0a 2a 2a 20 63 61 6c 6c 65 72 20   the..** caller 
3a641 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72  to eventually fr
3a642 65 65 20 70 2d 3e 69 64 78 53 74 72 20 69 66 20  ee p->idxStr if 
3a643 70 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  p->needToFreeIdx
3a644 53 74 72 20 69 6e 64 69 63 61 74 65 73 0d 0a 2a  Str indicates..*
3a645 2a 20 74 68 61 74 20 74 68 69 73 20 69 73 20 72  * that this is r
3a646 65 71 75 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 73 74  equired...*/..st
3a647 61 74 69 63 20 69 6e 74 20 76 74 61 62 42 65 73  atic int vtabBes
3a648 74 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  tIndex(Parse *pP
3a649 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
3a64a 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  b, sqlite3_index
3a64b 5f 69 6e 66 6f 20 2a 70 29 7b 0d 0a 20 20 73 71  _info *p){..  sq
3a64c 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
3a64d 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54  b = sqlite3GetVT
3a64e 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
3a64f 20 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0d 0a   pTab)->pVtab;..
3a650 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74 20    int i;..  int 
3a651 72 63 3b 0d 0a 0d 0a 20 20 57 48 45 52 45 54 52  rc;....  WHERETR
3a652 41 43 45 28 28 22 78 42 65 73 74 49 6e 64 65 78  ACE(("xBestIndex
3a653 20 66 6f 72 20 25 73 5c 6e 22 2c 20 70 54 61 62   for %s\n", pTab
3a654 2d 3e 7a 4e 61 6d 65 29 29 3b 0d 0a 20 20 54 52  ->zName));..  TR
3a655 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 70  ACE_IDX_INPUTS(p
3a656 29 3b 0d 0a 20 20 72 63 20 3d 20 70 56 74 61 62  );..  rc = pVtab
3a657 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 73 74  ->pModule->xBest
3a658 49 6e 64 65 78 28 70 56 74 61 62 2c 20 70 29 3b  Index(pVtab, p);
3a659 0d 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 4f 55  ..  TRACE_IDX_OU
3a65a 54 50 55 54 53 28 70 29 3b 0d 0a 0d 0a 20 20 69  TPUTS(p);....  i
3a65b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3a65c 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d   ){..    if( rc=
3a65d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
3a65e 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ..      pParse->
3a65f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3a660 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65   = 1;..    }else
3a661 20 69 66 28 20 21 70 56 74 61 62 2d 3e 7a 45 72   if( !pVtab->zEr
3a662 72 4d 73 67 20 29 7b 0d 0a 20 20 20 20 20 20 73  rMsg ){..      s
3a663 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
3a664 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73 71 6c  Parse, "%s", sql
3a665 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
3a666 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
3a667 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
3a668 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
3a669 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
3a66a 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
3a66b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a66c 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0d  Vtab->zErrMsg);.
3a66d 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  .  pVtab->zErrMs
3a66e 67 20 3d 20 30 3b 0d 0a 0d 0a 20 20 66 6f 72 28  g = 0;....  for(
3a66f 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74  i=0; i<p->nConst
3a670 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20  raint; i++){..  
3a671 20 20 69 66 28 20 21 70 2d 3e 61 43 6f 6e 73 74    if( !p->aConst
3a672 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 20  raint[i].usable 
3a673 26 26 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  && p->aConstrain
3a674 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e  tUsage[i].argvIn
3a675 64 65 78 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  dex>0 ){..      
3a676 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
3a677 70 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20  pParse, ..      
3a678 20 20 20 20 22 74 61 62 6c 65 20 25 73 3a 20 78      "table %s: x
3a679 42 65 73 74 49 6e 64 65 78 20 72 65 74 75 72 6e  BestIndex return
3a67a 65 64 20 61 6e 20 69 6e 76 61 6c 69 64 20 70 6c  ed an invalid pl
3a67b 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  an", pTab->zName
3a67c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
3a67d 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  ..  return pPars
3a67e 65 2d 3e 6e 45 72 72 3b 0d 0a 7d 0d 0a 0d 0a 0d  e->nErr;..}.....
3a67f 0a 2f 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ./*..** Compute 
3a680 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 66  the best index f
3a681 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  or a virtual tab
3a682 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  le...**..** The 
3a683 62 65 73 74 20 69 6e 64 65 78 20 69 73 20 63 6f  best index is co
3a684 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 78 42  mputed by the xB
3a685 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 20  estIndex method 
3a686 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 0d 0a  of the virtual..
3a687 2a 2a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  ** table module.
3a688 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
3a689 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20  s really just a 
3a68a 77 72 61 70 70 65 72 20 74 68 61 74 20 73 65 74  wrapper that set
3a68b 73 20 75 70 0d 0a 2a 2a 20 74 68 65 20 73 71 6c  s up..** the sql
3a68c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
3a68d 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69  structure that i
3a68e 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e  s used to commun
3a68f 69 63 61 74 65 20 77 69 74 68 0d 0a 2a 2a 20 78  icate with..** x
3a690 42 65 73 74 49 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a  BestIndex...**..
3a691 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e 2c 20 74 68  ** In a join, th
3a692 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
3a693 20 62 65 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69   be called multi
3a694 70 6c 65 20 74 69 6d 65 73 20 66 6f 72 20 74 68  ple times for th
3a695 65 0d 0a 2a 2a 20 73 61 6d 65 20 76 69 72 74 75  e..** same virtu
3a696 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 73  al table.  The s
3a697 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3a698 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  o structure is c
3a699 72 65 61 74 65 64 0d 0a 2a 2a 20 61 6e 64 20 69  reated..** and i
3a69a 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20 74 68  nitialized on th
3a69b 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69  e first invocati
3a69c 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20 6f 6e  on and reused on
3a69d 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0d   all subsequent.
3a69e 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e  .** invocations.
3a69f 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e    The sqlite3_in
3a6a0 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
3a6a1 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  re is also used 
3a6a2 77 68 65 6e 0d 0a 2a 2a 20 63 6f 64 65 20 69 73  when..** code is
3a6a3 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63   generated to ac
3a6a4 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
3a6a5 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65   table.  The whe
3a6a6 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0d  reInfoDelete() .
3a6a7 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65  .** routine take
3a6a8 73 20 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e  s care of freein
3a6a9 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  g the sqlite3_in
3a6aa 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
3a6ab 72 65 20 61 66 74 65 72 0d 0a 2a 2a 20 65 76 65  re after..** eve
3a6ac 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73  rybody has finis
3a6ad 68 65 64 20 77 69 74 68 20 69 74 2e 0d 0a 2a 2f  hed with it...*/
3a6ae 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  ..static void be
3a6af 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0d  stVirtualIndex(.
3a6b0 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3a6b1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a6b2 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
3a6b3 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20  g context */..  
3a6b4 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
3a6b5 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a6b6 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
3a6b7 75 73 65 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74  use */..  struct
3a6b8 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
3a6b9 53 72 63 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  Src,      /* The
3a6ba 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
3a6bb 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0d 0a  m to search */..
3a6bc 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
3a6bd 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dy,             
3a6be 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
3a6bf 73 6f 72 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  sors not availab
3a6c0 6c 65 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0d  le for index */.
3a6c1 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 56 61  .  Bitmask notVa
3a6c2 6c 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  lid,            
3a6c3 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 6e 6f     /* Cursors no
3a6c4 74 20 76 61 6c 69 64 20 66 6f 72 20 61 6e 79 20  t valid for any 
3a6c5 70 75 72 70 6f 73 65 20 2a 2f 0d 0a 20 20 45 78  purpose */..  Ex
3a6c6 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
3a6c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
3a6c8 20 54 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   The order by cl
3a6c9 61 75 73 65 20 2a 2f 0d 0a 20 20 57 68 65 72 65  ause */..  Where
3a6ca 43 6f 73 74 20 2a 70 43 6f 73 74 2c 20 20 20 20  Cost *pCost,    
3a6cb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
3a6cc 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79 20  west cost query 
3a6cd 70 6c 61 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  plan */..  sqlit
3a6ce 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
3a6cf 70 70 49 64 78 49 6e 66 6f 20 20 2f 2a 20 49 6e  ppIdxInfo  /* In
3a6d0 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  dex information 
3a6d1 70 61 73 73 65 64 20 74 6f 20 78 42 65 73 74 49  passed to xBestI
3a6d2 6e 64 65 78 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 54  ndex */..){..  T
3a6d3 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 53 72  able *pTab = pSr
3a6d4 63 2d 3e 70 54 61 62 3b 0d 0a 20 20 73 71 6c 69  c->pTab;..  sqli
3a6d5 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3a6d6 70 49 64 78 49 6e 66 6f 3b 0d 0a 20 20 73 74 72  pIdxInfo;..  str
3a6d7 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
3a6d8 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49  x_constraint *pI
3a6d9 64 78 43 6f 6e 73 3b 0d 0a 20 20 73 74 72 75 63  dxCons;..  struc
3a6da 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
3a6db 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
3a6dc 20 2a 70 55 73 61 67 65 3b 0d 0a 20 20 57 68 65   *pUsage;..  Whe
3a6dd 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0d 0a  reTerm *pTerm;..
3a6de 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 69    int i, j;..  i
3a6df 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0d 0a 20 20  nt nOrderBy;..  
3a6e0 64 6f 75 62 6c 65 20 72 43 6f 73 74 3b 0d 0a 0d  double rCost;...
3a6e1 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
3a6e2 77 73 46 6c 61 67 73 20 69 73 20 69 6e 69 74 69  wsFlags is initi
3a6e3 61 6c 69 7a 65 64 20 74 6f 20 73 6f 6d 65 20 73  alized to some s
3a6e4 61 6e 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72  ane value. Other
3a6e5 77 69 73 65 2c 20 69 66 20 74 68 65 20 0d 0a 20  wise, if the .. 
3a6e6 20 2a 2a 20 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c   ** malloc in al
3a6e7 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28  locateIndexInfo(
3a6e8 29 20 66 61 69 6c 73 20 61 6e 64 20 74 68 69 73  ) fails and this
3a6e9 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
3a6ea 73 20 6c 65 61 76 69 6e 67 0d 0a 20 20 2a 2a 20  s leaving..  ** 
3a6eb 77 73 46 6c 61 67 73 20 69 6e 20 61 6e 20 75 6e  wsFlags in an un
3a6ec 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74  initialized stat
3a6ed 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61  e, the caller ma
3a6ee 79 20 62 65 68 61 76 65 20 75 6e 70 72 65 64 69  y behave unpredi
3a6ef 63 74 61 62 6c 79 2e 0d 0a 20 20 2a 2f 0d 0a 20  ctably...  */.. 
3a6f0 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30   memset(pCost, 0
3a6f1 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29  , sizeof(*pCost)
3a6f2 29 3b 0d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61  );..  pCost->pla
3a6f3 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52  n.wsFlags = WHER
3a6f4 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b 0d  E_VIRTUALTABLE;.
3a6f5 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ...  /* If the s
3a6f6 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3a6f7 6f 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20  o structure has 
3a6f8 6e 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75  not been previou
3a6f9 73 6c 79 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  sly..  ** alloca
3a6fa 74 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ted and initiali
3a6fb 7a 65 64 2c 20 74 68 65 6e 20 61 6c 6c 6f 63 61  zed, then alloca
3a6fc 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  te and initializ
3a6fd 65 20 69 74 20 6e 6f 77 2e 0d 0a 20 20 2a 2f 0d  e it now...  */.
3a6fe 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 2a 70  .  pIdxInfo = *p
3a6ff 70 49 64 78 49 6e 66 6f 3b 0d 0a 20 20 69 66 28  pIdxInfo;..  if(
3a700 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0d   pIdxInfo==0 ){.
3a701 0a 20 20 20 20 2a 70 70 49 64 78 49 6e 66 6f 20  .    *ppIdxInfo 
3a702 3d 20 70 49 64 78 49 6e 66 6f 20 3d 20 61 6c 6c  = pIdxInfo = all
3a703 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 70  ocateIndexInfo(p
3a704 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63  Parse, pWC, pSrc
3a705 2c 20 70 4f 72 64 65 72 42 79 29 3b 0d 0a 20 20  , pOrderBy);..  
3a706 7d 0d 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66  }..  if( pIdxInf
3a707 6f 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74  o==0 ){..    ret
3a708 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  urn;..  }....  /
3a709 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  * At this point,
3a70a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
3a70b 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
3a70c 65 20 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20  e that pIdxInfo 
3a70d 70 6f 69 6e 74 73 0d 0a 20 20 2a 2a 20 74 6f 20  points..  ** to 
3a70e 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69  will have been i
3a70f 6e 69 74 69 61 6c 69 7a 65 64 2c 20 65 69 74 68  nitialized, eith
3a710 65 72 20 64 75 72 69 6e 67 20 74 68 65 20 63 75  er during the cu
3a711 72 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e  rrent invocation
3a712 20 6f 72 0d 0a 20 20 2a 2a 20 64 75 72 69 6e 67   or..  ** during
3a713 20 73 6f 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f   some prior invo
3a714 63 61 74 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20  cation.  Now we 
3a715 6a 75 73 74 20 68 61 76 65 20 74 6f 20 63 75 73  just have to cus
3a716 74 6f 6d 69 7a 65 20 74 68 65 0d 0a 20 20 2a 2a  tomize the..  **
3a717 20 64 65 74 61 69 6c 73 20 6f 66 20 70 49 64 78   details of pIdx
3a718 49 6e 66 6f 20 66 6f 72 20 74 68 65 20 63 75 72  Info for the cur
3a719 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20  rent invocation 
3a71a 61 6e 64 20 70 61 73 73 20 69 74 20 74 6f 0d 0a  and pass it to..
3a71b 20 20 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e    ** xBestIndex.
3a71c 0d 0a 20 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54  ..  */....  /* T
3a71d 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6d  he module name m
3a71e 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 2e 20  ust be defined. 
3a71f 41 6c 73 6f 2c 20 62 79 20 74 68 69 73 20 70 6f  Also, by this po
3a720 69 6e 74 20 74 68 65 72 65 20 6d 75 73 74 0d 0a  int there must..
3a721 20 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e 74 65    ** be a pointe
3a722 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
3a723 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20  vtab structure. 
3a724 4f 74 68 65 72 77 69 73 65 0d 0a 20 20 2a 2a 20  Otherwise..  ** 
3a725 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
3a726 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75 6c  lumnNames() woul
3a727 64 20 68 61 76 65 20 70 69 63 6b 65 64 20 75 70  d have picked up
3a728 20 74 68 65 20 65 72 72 6f 72 2e 20 0d 0a 20 20   the error. ..  
3a729 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 54  */..  assert( pT
3a72a 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20  ab->azModuleArg 
3a72b 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c  && pTab->azModul
3a72c 65 41 72 67 5b 30 5d 20 29 3b 0d 0a 20 20 61 73  eArg[0] );..  as
3a72d 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 65 74  sert( sqlite3Get
3a72e 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64  VTable(pParse->d
3a72f 62 2c 20 70 54 61 62 29 20 29 3b 0d 0a 0d 0a 20  b, pTab) );.... 
3a730 20 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f 6e   /* Set the aCon
3a731 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
3a732 20 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69 74   fields and init
3a733 69 61 6c 69 7a 65 20 61 6c 6c 20 0d 0a 20 20 2a  ialize all ..  *
3a734 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
3a735 65 73 20 74 6f 20 7a 65 72 6f 2e 0d 0a 20 20 2a  es to zero...  *
3a736 2a 0d 0a 20 20 2a 2a 20 61 43 6f 6e 73 74 72 61  *..  ** aConstra
3a737 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20  int[].usable is 
3a738 74 72 75 65 20 66 6f 72 20 63 6f 6e 73 74 72 61  true for constra
3a739 69 6e 74 73 20 77 68 65 72 65 20 74 68 65 20 72  ints where the r
3a73a 69 67 68 74 2d 68 61 6e 64 0d 0a 20 20 2a 2a 20  ight-hand..  ** 
3a73b 73 69 64 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  side contains on
3a73c 6c 79 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ly references to
3a73d 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
3a73e 65 66 74 20 6f 66 20 74 68 65 20 63 75 72 72 65  eft of the curre
3a73f 6e 74 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20  nt..  ** table. 
3a740 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
3a741 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69   if the constrai
3a742 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  nt is of the for
3a743 6d 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20  m:..  **..  **  
3a744 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
3a745 3d 20 65 78 70 72 0d 0a 20 20 2a 2a 0d 0a 20 20  = expr..  **..  
3a746 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 65 76  ** and we are ev
3a747 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c  aluating a join,
3a748 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72   then the constr
3a749 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69  aint on column i
3a74a 73 20 0d 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61  s ..  ** only va
3a74b 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62 6c 65  lid if all table
3a74c 73 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20  s referenced in 
3a74d 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20 74 68  expr occur to th
3a74e 65 20 6c 65 66 74 0d 0a 20 20 2a 2a 20 6f 66 20  e left..  ** of 
3a74f 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  the table contai
3a750 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0d 0a 20 20  ning column...  
3a751 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f  **..  ** The aCo
3a752 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61 72 72 61  nstraints[] arra
3a753 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69  y contains entri
3a754 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74  es for all const
3a755 72 61 69 6e 74 73 0d 0a 20 20 2a 2a 20 6f 6e 20  raints..  ** on 
3a756 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
3a757 65 2e 20 20 54 68 61 74 20 77 61 79 20 77 65 20  e.  That way we 
3a758 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20 63 6f 6d  only have to com
3a759 70 75 74 65 20 69 74 20 6f 6e 63 65 0d 0a 20 20  pute it once..  
3a75a 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 77  ** even though w
3a75b 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f 20 70  e might try to p
3a75c 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
3a75d 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ex multiple time
3a75e 73 2e 0d 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63  s...  ** For eac
3a75f 68 20 61 74 74 65 6d 70 74 20 61 74 20 70 69 63  h attempt at pic
3a760 6b 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74  king an index, t
3a761 68 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c  he order of tabl
3a762 65 73 20 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20  es in the..  ** 
3a763 6a 6f 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69  join might be di
3a764 66 66 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61  fferent so we ha
3a765 76 65 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ve to recompute 
3a766 74 68 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0d  the usable flag.
3a767 0a 20 20 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e  .  ** each time.
3a768 0d 0a 20 20 2a 2f 0d 0a 20 20 70 49 64 78 43 6f  ..  */..  pIdxCo
3a769 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20 73 71  ns = *(struct sq
3a76a 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
3a76b 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e  traint**)&pIdxIn
3a76c 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->aConstraint;
3a76d 0d 0a 20 20 70 55 73 61 67 65 20 3d 20 70 49 64  ..  pUsage = pId
3a76e 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
3a76f 6e 74 55 73 61 67 65 3b 0d 0a 20 20 66 6f 72 28  ntUsage;..  for(
3a770 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d  i=0; i<pIdxInfo-
3a771 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
3a772 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b 0d  +, pIdxCons++){.
3a773 0a 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e  .    j = pIdxCon
3a774 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0d  s->iTermOffset;.
3a775 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57  .    pTerm = &pW
3a776 43 2d 3e 61 5b 6a 5d 3b 0d 0a 20 20 20 20 70 49  C->a[j];..    pI
3a777 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d  dxCons->usable =
3a778 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52   (pTerm->prereqR
3a779 69 67 68 74 26 6e 6f 74 52 65 61 64 79 29 20 3f  ight&notReady) ?
3a77a 20 30 20 3a 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20   0 : 1;..  }..  
3a77b 6d 65 6d 73 65 74 28 70 55 73 61 67 65 2c 20 30  memset(pUsage, 0
3a77c 2c 20 73 69 7a 65 6f 66 28 70 55 73 61 67 65 5b  , sizeof(pUsage[
3a77d 30 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43  0])*pIdxInfo->nC
3a77e 6f 6e 73 74 72 61 69 6e 74 29 3b 0d 0a 20 20 69  onstraint);..  i
3a77f 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65  f( pIdxInfo->nee
3a780 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b  dToFreeIdxStr ){
3a781 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
3a782 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  ee(pIdxInfo->idx
3a783 53 74 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 49  Str);..  }..  pI
3a784 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d  dxInfo->idxStr =
3a785 20 30 3b 0d 0a 20 20 70 49 64 78 49 6e 66 6f 2d   0;..  pIdxInfo-
3a786 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0d 0a 20 20  >idxNum = 0;..  
3a787 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
3a788 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0d  FreeIdxStr = 0;.
3a789 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64  .  pIdxInfo->ord
3a78a 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30  erByConsumed = 0
3a78b 3b 0d 0a 20 20 2f 2a 20 28 28 64 6f 75 62 6c 65  ;..  /* ((double
3a78c 29 32 29 20 49 6e 20 63 61 73 65 20 6f 66 20 53  )2) In case of S
3a78d 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
3a78e 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0d  ING_POINT... */.
3a78f 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
3a790 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 53 51 4c  imatedCost = SQL
3a791 49 54 45 5f 42 49 47 5f 44 42 4c 20 2f 20 28 28  ITE_BIG_DBL / ((
3a792 64 6f 75 62 6c 65 29 32 29 3b 0d 0a 20 20 6e 4f  double)2);..  nO
3a793 72 64 65 72 42 79 20 3d 20 70 49 64 78 49 6e 66  rderBy = pIdxInf
3a794 6f 2d 3e 6e 4f 72 64 65 72 42 79 3b 0d 0a 20 20  o->nOrderBy;..  
3a795 69 66 28 20 21 70 4f 72 64 65 72 42 79 20 29 7b  if( !pOrderBy ){
3a796 0d 0a 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e  ..    pIdxInfo->
3a797 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0d 0a 20  nOrderBy = 0;.. 
3a798 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 76 74 61 62   }....  if( vtab
3a799 42 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65  BestIndex(pParse
3a79a 2c 20 70 54 61 62 2c 20 70 49 64 78 49 6e 66 6f  , pTab, pIdxInfo
3a79b 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  ) ){..    return
3a79c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 49 64 78  ;..  }....  pIdx
3a79d 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20  Cons = *(struct 
3a79e 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
3a79f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
3a7a0 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
3a7a1 74 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  t;..  for(i=0; i
3a7a2 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  <pIdxInfo->nCons
3a7a3 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0d 0a 20  traint; i++){.. 
3a7a4 20 20 20 69 66 28 20 70 55 73 61 67 65 5b 69 5d     if( pUsage[i]
3a7a5 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0d  .argvIndex>0 ){.
3a7a6 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73  .      pCost->us
3a7a7 65 64 20 7c 3d 20 70 57 43 2d 3e 61 5b 70 49 64  ed |= pWC->a[pId
3a7a8 78 43 6f 6e 73 5b 69 5d 2e 69 54 65 72 6d 4f 66  xCons[i].iTermOf
3a7a9 66 73 65 74 5d 2e 70 72 65 72 65 71 52 69 67 68  fset].prereqRigh
3a7aa 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  t;..    }..  }..
3a7ab 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
3a7ac 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
3a7ad 6c 61 75 73 65 2c 20 61 6e 64 20 74 68 65 20 73  lause, and the s
3a7ae 65 6c 65 63 74 65 64 20 76 69 72 74 75 61 6c 20  elected virtual 
3a7af 74 61 62 6c 65 20 69 6e 64 65 78 0d 0a 20 20 2a  table index..  *
3a7b0 2a 20 64 6f 65 73 20 6e 6f 74 20 73 61 74 69 73  * does not satis
3a7b1 66 79 20 69 74 2c 20 69 6e 63 72 65 61 73 65 20  fy it, increase 
3a7b2 74 68 65 20 63 6f 73 74 20 6f 66 20 74 68 65 20  the cost of the 
3a7b3 73 63 61 6e 20 61 63 63 6f 72 64 69 6e 67 6c 79  scan accordingly
3a7b4 2e 20 54 68 69 73 0d 0a 20 20 2a 2a 20 6d 61 74  . This..  ** mat
3a7b5 63 68 65 73 20 74 68 65 20 70 72 6f 63 65 73 73  ches the process
3a7b6 69 6e 67 20 66 6f 72 20 6e 6f 6e 2d 76 69 72 74  ing for non-virt
3a7b7 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 62 65  ual tables in be
3a7b8 73 74 42 74 72 65 65 49 6e 64 65 78 28 29 2e 0d  stBtreeIndex()..
3a7b9 0a 20 20 2a 2f 0d 0a 20 20 72 43 6f 73 74 20 3d  .  */..  rCost =
3a7ba 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d   pIdxInfo->estim
3a7bb 61 74 65 64 43 6f 73 74 3b 0d 0a 20 20 69 66 28  atedCost;..  if(
3a7bc 20 70 4f 72 64 65 72 42 79 20 26 26 20 70 49 64   pOrderBy && pId
3a7bd 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f  xInfo->orderByCo
3a7be 6e 73 75 6d 65 64 3d 3d 30 20 29 7b 0d 0a 20 20  nsumed==0 ){..  
3a7bf 20 20 72 43 6f 73 74 20 2b 3d 20 65 73 74 4c 6f    rCost += estLo
3a7c0 67 28 72 43 6f 73 74 29 2a 72 43 6f 73 74 3b 0d  g(rCost)*rCost;.
3a7c1 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65  .  }....  /* The
3a7c2 20 63 6f 73 74 20 69 73 20 6e 6f 74 20 61 6c 6c   cost is not all
3a7c3 6f 77 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65  owed to be large
3a7c4 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42 49  r than SQLITE_BI
3a7c5 47 5f 44 42 4c 20 28 74 68 65 0d 0a 20 20 2a 2a  G_DBL (the..  **
3a7c6 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f 66   inital value of
3a7c7 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e 20 74   lowestCost in t
3a7c8 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20  his loop. If it 
3a7c9 69 73 2c 20 74 68 65 6e 20 74 68 65 0d 0a 20 20  is, then the..  
3a7ca 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43  ** (cost<lowestC
3a7cb 6f 73 74 29 20 74 65 73 74 20 62 65 6c 6f 77 20  ost) test below 
3a7cc 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 74 72  will never be tr
3a7cd 75 65 2e 0d 0a 20 20 2a 2a 20 0d 0a 20 20 2a 2a  ue...  ** ..  **
3a7ce 20 55 73 65 20 22 28 64 6f 75 62 6c 65 29 32 22   Use "(double)2"
3a7cf 20 69 6e 73 74 65 61 64 20 6f 66 20 22 32 2e 30   instead of "2.0
3a7d0 22 20 69 6e 20 63 61 73 65 20 4f 4d 49 54 5f 46  " in case OMIT_F
3a7d1 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 0d 0a  LOATING_POINT ..
3a7d2 20 20 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e    ** is defined.
3a7d3 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28 53  ..  */..  if( (S
3a7d4 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28  QLITE_BIG_DBL/((
3a7d5 64 6f 75 62 6c 65 29 32 29 29 3c 72 43 6f 73 74  double)2))<rCost
3a7d6 20 29 7b 0d 0a 20 20 20 20 70 43 6f 73 74 2d 3e   ){..    pCost->
3a7d7 72 43 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f  rCost = (SQLITE_
3a7d8 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65  BIG_DBL/((double
3a7d9 29 32 29 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  )2));..  }else{.
3a7da 0a 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73  .    pCost->rCos
3a7db 74 20 3d 20 72 43 6f 73 74 3b 0d 0a 20 20 7d 0d  t = rCost;..  }.
3a7dc 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75  .  pCost->plan.u
3a7dd 2e 70 56 74 61 62 49 64 78 20 3d 20 70 49 64 78  .pVtabIdx = pIdx
3a7de 49 6e 66 6f 3b 0d 0a 20 20 69 66 28 20 70 49 64  Info;..  if( pId
3a7df 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f  xInfo->orderByCo
3a7e0 6e 73 75 6d 65 64 20 29 7b 0d 0a 20 20 20 20 70  nsumed ){..    p
3a7e1 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
3a7e2 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45  gs |= WHERE_ORDE
3a7e3 52 42 59 3b 0d 0a 20 20 7d 0d 0a 20 20 70 43 6f  RBY;..  }..  pCo
3a7e4 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 30  st->plan.nEq = 0
3a7e5 3b 0d 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  ;..  pIdxInfo->n
3a7e6 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72  OrderBy = nOrder
3a7e7 42 79 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 72 79 20  By;....  /* Try 
3a7e8 74 6f 20 66 69 6e 64 20 61 20 6d 6f 72 65 20 65  to find a more e
3a7e9 66 66 69 63 69 65 6e 74 20 61 63 63 65 73 73 20  fficient access 
3a7ea 70 61 74 74 65 72 6e 20 62 79 20 75 73 69 6e 67  pattern by using
3a7eb 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64 65 78 65   multiple indexe
3a7ec 73 0d 0a 20 20 2a 2a 20 74 6f 20 6f 70 74 69 6d  s..  ** to optim
3a7ed 69 7a 65 20 61 6e 20 4f 52 20 65 78 70 72 65 73  ize an OR expres
3a7ee 73 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20  sion within the 
3a7ef 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0d 0a  WHERE clause. ..
3a7f0 20 20 2a 2f 0d 0a 20 20 62 65 73 74 4f 72 43 6c    */..  bestOrCl
3a7f1 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65  auseIndex(pParse
3a7f2 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
3a7f3 52 65 61 64 79 2c 20 6e 6f 74 56 61 6c 69 64 2c  Ready, notValid,
3a7f4 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74   pOrderBy, pCost
3a7f5 29 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  );..}..#endif /*
3a7f6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
3a7f7 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0d 0a 0d 0a  TUALTABLE */....
3a7f8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
3a7f9 41 42 4c 45 5f 53 54 41 54 33 0d 0a 2f 2a 0d 0a  ABLE_STAT3../*..
3a7fa 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  ** Estimate the 
3a7fb 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 70 61  location of a pa
3a7fc 72 74 69 63 75 6c 61 72 20 6b 65 79 20 61 6d 6f  rticular key amo
3a7fd 6e 67 20 61 6c 6c 20 6b 65 79 73 20 69 6e 20 61  ng all keys in a
3a7fe 6e 0d 0a 2a 2a 20 69 6e 64 65 78 2e 20 20 53 74  n..** index.  St
3a7ff 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
3a800 69 6e 20 61 53 74 61 74 20 61 73 20 66 6f 6c 6c  in aStat as foll
3a801 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ows:..**..**    
3a802 61 53 74 61 74 5b 30 5d 20 20 20 20 20 20 45 73  aStat[0]      Es
3a803 74 2e 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  t. number of row
3a804 73 20 6c 65 73 73 20 74 68 61 6e 20 70 56 61 6c  s less than pVal
3a805 0d 0a 2a 2a 20 20 20 20 61 53 74 61 74 5b 31 5d  ..**    aStat[1]
3a806 20 20 20 20 20 20 45 73 74 2e 20 6e 75 6d 62 65        Est. numbe
3a807 72 20 6f 66 20 72 6f 77 73 20 65 71 75 61 6c 20  r of rows equal 
3a808 74 6f 20 70 56 61 6c 0d 0a 2a 2a 0d 0a 2a 2a 20  to pVal..**..** 
3a809 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
3a80a 20 6f 6e 20 73 75 63 63 65 73 73 2e 0d 0a 2a 2f   on success...*/
3a80b 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  ..static int whe
3a80c 72 65 4b 65 79 53 74 61 74 73 28 0d 0a 20 20 50  reKeyStats(..  P
3a80d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
3a80e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
3a80f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3a810 6e 20 2a 2f 0d 0a 20 20 49 6e 64 65 78 20 2a 70  n */..  Index *p
3a811 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
3a812 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 74 6f 20      /* Index to 
3a813 63 6f 6e 73 69 64 65 72 20 64 6f 6d 61 69 6e 20  consider domain 
3a814 6f 66 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  of */..  sqlite3
3a815 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 20 20  _value *pVal,   
3a816 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
3a817 20 63 6f 6e 73 69 64 65 72 20 2a 2f 0d 0a 20 20   consider */..  
3a818 69 6e 74 20 72 6f 75 6e 64 55 70 2c 20 20 20 20  int roundUp,    
3a819 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
3a81a 6f 75 6e 64 20 75 70 20 69 66 20 74 72 75 65 2e  ound up if true.
3a81b 20 20 52 6f 75 6e 64 20 64 6f 77 6e 20 69 66 20    Round down if 
3a81c 66 61 6c 73 65 20 2a 2f 0d 0a 20 20 74 52 6f 77  false */..  tRow
3a81d 63 6e 74 20 2a 61 53 74 61 74 20 20 20 20 20 20  cnt *aStat      
3a81e 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3a81f 73 74 61 74 73 20 77 72 69 74 74 65 6e 20 68 65  stats written he
3a820 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 74 52 6f  re */..){..  tRo
3a821 77 63 6e 74 20 6e 3b 0d 0a 20 20 49 6e 64 65 78  wcnt n;..  Index
3a822 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b  Sample *aSample;
3a823 0d 0a 20 20 69 6e 74 20 69 2c 20 65 54 79 70 65  ..  int i, eType
3a824 3b 0d 0a 20 20 69 6e 74 20 69 73 45 71 20 3d 20  ;..  int isEq = 
3a825 30 3b 0d 0a 20 20 69 36 34 20 76 3b 0d 0a 20 20  0;..  i64 v;..  
3a826 64 6f 75 62 6c 65 20 72 2c 20 72 53 3b 0d 0a 0d  double r, rS;...
3a827 0a 20 20 61 73 73 65 72 74 28 20 72 6f 75 6e 64  .  assert( round
3a828 55 70 3d 3d 30 20 7c 7c 20 72 6f 75 6e 64 55 70  Up==0 || roundUp
3a829 3d 3d 31 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ==1 );..  assert
3a82a 28 20 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 3e  ( pIdx->nSample>
3a82b 30 20 29 3b 0d 0a 20 20 69 66 28 20 70 56 61 6c  0 );..  if( pVal
3a82c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
3a82d 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 6e 20  ITE_ERROR;..  n 
3a82e 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 74  = pIdx->aiRowEst
3a82f 5b 30 5d 3b 0d 0a 20 20 61 53 61 6d 70 6c 65 20  [0];..  aSample 
3a830 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3b  = pIdx->aSample;
3a831 0d 0a 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69  ..  eType = sqli
3a832 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70  te3_value_type(p
3a833 56 61 6c 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 65  Val);....  if( e
3a834 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  Type==SQLITE_INT
3a835 45 47 45 52 20 29 7b 0d 0a 20 20 20 20 76 20 3d  EGER ){..    v =
3a836 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
3a837 6e 74 36 34 28 70 56 61 6c 29 3b 0d 0a 20 20 20  nt64(pVal);..   
3a838 20 72 20 3d 20 28 69 36 34 29 76 3b 0d 0a 20 20   r = (i64)v;..  
3a839 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
3a83a 78 2d 3e 6e 53 61 6d 70 6c 65 3b 20 69 2b 2b 29  x->nSample; i++)
3a83b 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 61 53 61  {..      if( aSa
3a83c 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53  mple[i].eType==S
3a83d 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e  QLITE_NULL ) con
3a83e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 69 66  tinue;..      if
3a83f 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79  ( aSample[i].eTy
3a840 70 65 3e 3d 53 51 4c 49 54 45 5f 54 45 58 54 20  pe>=SQLITE_TEXT 
3a841 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
3a842 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65  if( aSample[i].e
3a843 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  Type==SQLITE_INT
3a844 45 47 45 52 20 29 7b 0d 0a 20 20 20 20 20 20 20  EGER ){..       
3a845 20 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e   if( aSample[i].
3a846 75 2e 69 3e 3d 76 20 29 7b 0d 0a 20 20 20 20 20  u.i>=v ){..     
3a847 20 20 20 20 20 69 73 45 71 20 3d 20 61 53 61 6d       isEq = aSam
3a848 70 6c 65 5b 69 5d 2e 75 2e 69 3d 3d 76 3b 0d 0a  ple[i].u.i==v;..
3a849 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3a84a 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
3a84b 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
3a84c 20 20 20 61 73 73 65 72 74 28 20 61 53 61 6d 70     assert( aSamp
3a84d 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c  le[i].eType==SQL
3a84e 49 54 45 5f 46 4c 4f 41 54 20 29 3b 0d 0a 20 20  ITE_FLOAT );..  
3a84f 20 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c        if( aSampl
3a850 65 5b 69 5d 2e 75 2e 72 3e 3d 72 20 29 7b 0d 0a  e[i].u.r>=r ){..
3a851 20 20 20 20 20 20 20 20 20 20 69 73 45 71 20 3d            isEq =
3a852 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3d   aSample[i].u.r=
3a853 3d 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62  =r;..          b
3a854 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d  reak;..        }
3a855 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
3a856 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 54  ..  }else if( eT
3a857 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41  ype==SQLITE_FLOA
3a858 54 20 29 7b 0d 0a 20 20 20 20 72 20 3d 20 73 71  T ){..    r = sq
3a859 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3a85a 6c 65 28 70 56 61 6c 29 3b 0d 0a 20 20 20 20 66  le(pVal);..    f
3a85b 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
3a85c 6e 53 61 6d 70 6c 65 3b 20 69 2b 2b 29 7b 0d 0a  nSample; i++){..
3a85d 20 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c        if( aSampl
3a85e 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49  e[i].eType==SQLI
3a85f 54 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e 74 69 6e  TE_NULL ) contin
3a860 75 65 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 61  ue;..      if( a
3a861 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3e  Sample[i].eType>
3a862 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 29 20 62  =SQLITE_TEXT ) b
3a863 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 69 66 28  reak;..      if(
3a864 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70   aSample[i].eTyp
3a865 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  e==SQLITE_FLOAT 
3a866 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 53 20 3d  ){..        rS =
3a867 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3b   aSample[i].u.r;
3a868 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
3a869 20 20 20 20 20 20 20 20 72 53 20 3d 20 61 53 61          rS = aSa
3a86a 6d 70 6c 65 5b 69 5d 2e 75 2e 69 3b 0d 0a 20 20  mple[i].u.i;..  
3a86b 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
3a86c 20 72 53 3e 3d 72 20 29 7b 0d 0a 20 20 20 20 20   rS>=r ){..     
3a86d 20 20 20 69 73 45 71 20 3d 20 72 53 3d 3d 72 3b     isEq = rS==r;
3a86e 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3a86f 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
3a870 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 54  ..  }else if( eT
3a871 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  ype==SQLITE_NULL
3a872 20 29 7b 0d 0a 20 20 20 20 69 20 3d 20 30 3b 0d   ){..    i = 0;.
3a873 0a 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65  .    if( aSample
3a874 5b 30 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54  [0].eType==SQLIT
3a875 45 5f 4e 55 4c 4c 20 29 20 69 73 45 71 20 3d 20  E_NULL ) isEq = 
3a876 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  1;..  }else{..  
3a877 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d    assert( eType=
3a878 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20  =SQLITE_TEXT || 
3a879 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c  eType==SQLITE_BL
3a87a 4f 42 20 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69  OB );..    for(i
3a87b 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 53 61 6d  =0; i<pIdx->nSam
3a87c 70 6c 65 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20  ple; i++){..    
3a87d 20 20 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d    if( aSample[i]
3a87e 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54  .eType==SQLITE_T
3a87f 45 58 54 20 7c 7c 20 61 53 61 6d 70 6c 65 5b 69  EXT || aSample[i
3a880 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ].eType==SQLITE_
3a881 42 4c 4f 42 20 29 7b 0d 0a 20 20 20 20 20 20 20  BLOB ){..       
3a882 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d   break;..      }
3a883 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
3a884 20 69 3c 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65   i<pIdx->nSample
3a885 20 29 7b 20 20 20 20 20 20 0d 0a 20 20 20 20 20   ){      ..     
3a886 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
3a887 50 61 72 73 65 2d 3e 64 62 3b 0d 0a 20 20 20 20  Parse->db;..    
3a888 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
3a889 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  ;..      const u
3a88a 38 20 2a 7a 3b 0d 0a 20 20 20 20 20 20 69 66 28  8 *z;..      if(
3a88b 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42   eType==SQLITE_B
3a88c 4c 4f 42 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  LOB ){..        
3a88d 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 20 2a 29  z = (const u8 *)
3a88e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
3a88f 6f 62 28 70 56 61 6c 29 3b 0d 0a 20 20 20 20 20  ob(pVal);..     
3a890 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70     pColl = db->p
3a891 44 66 6c 74 43 6f 6c 6c 3b 0d 0a 20 20 20 20 20  DfltColl;..     
3a892 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c     assert( pColl
3a893 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
3a894 46 38 20 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  F8 );..      }el
3a895 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 43 6f  se{..        pCo
3a896 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43  ll = sqlite3GetC
3a897 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
3a898 45 5f 55 54 46 38 2c 20 30 2c 20 2a 70 49 64 78  E_UTF8, 0, *pIdx
3a899 2d 3e 61 7a 43 6f 6c 6c 29 3b 0d 0a 20 20 20 20  ->azColl);..    
3a89a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30      if( pColl==0
3a89b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
3a89c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
3a89d 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
3a89e 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3a89f 63 65 3a 20 25 73 22 2c 0d 0a 20 20 20 20 20 20  ce: %s",..      
3a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a8a1 20 20 20 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c      *pIdx->azCol
3a8a2 6c 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72  l);..          r
3a8a3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3a8a4 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  OR;..        }..
3a8a5 20 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e          z = (con
3a8a6 73 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33 56  st u8 *)sqlite3V
3a8a7 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 70  alueText(pVal, p
3a8a8 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0d 0a 20 20 20  Coll->enc);..   
3a8a9 20 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0d 0a       if( !z ){..
3a8aa 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a8ab 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
3a8ac 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3a8ad 20 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20     assert( z && 
3a8ae 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e  pColl && pColl->
3a8af 78 43 6d 70 20 29 3b 0d 0a 20 20 20 20 20 20 7d  xCmp );..      }
3a8b0 0d 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69  ..      n = sqli
3a8b1 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56  te3ValueBytes(pV
3a8b2 61 6c 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b  al, pColl->enc);
3a8b3 0d 0a 20 20 0d 0a 20 20 20 20 20 20 66 6f 72 28  ..  ..      for(
3a8b4 3b 20 69 3c 70 49 64 78 2d 3e 6e 53 61 6d 70 6c  ; i<pIdx->nSampl
3a8b5 65 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  e; i++){..      
3a8b6 20 20 69 6e 74 20 63 3b 0d 0a 20 20 20 20 20 20    int c;..      
3a8b7 20 20 69 6e 74 20 65 53 61 6d 70 6c 65 74 79 70    int eSampletyp
3a8b8 65 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65  e = aSample[i].e
3a8b9 54 79 70 65 3b 0d 0a 20 20 20 20 20 20 20 20 69  Type;..        i
3a8ba 66 28 20 65 53 61 6d 70 6c 65 74 79 70 65 3c 65  f( eSampletype<e
3a8bb 54 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Type ) continue;
3a8bc 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 53  ..        if( eS
3a8bd 61 6d 70 6c 65 74 79 70 65 21 3d 65 54 79 70 65  ampletype!=eType
3a8be 20 29 20 62 72 65 61 6b 3b 0d 0a 23 69 66 6e 64   ) break;..#ifnd
3a8bf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
3a8c0 54 46 31 36 0d 0a 20 20 20 20 20 20 20 20 69 66  TF16..        if
3a8c1 28 20 70 43 6f 6c 6c 2d 3e 65 6e 63 21 3d 53 51  ( pColl->enc!=SQ
3a8c2 4c 49 54 45 5f 55 54 46 38 20 29 7b 0d 0a 20 20  LITE_UTF8 ){..  
3a8c3 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 61 6d          int nSam
3a8c4 70 6c 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ple;..          
3a8c5 63 68 61 72 20 2a 7a 53 61 6d 70 6c 65 20 3d 20  char *zSample = 
3a8c6 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 36 28  sqlite3Utf8to16(
3a8c7 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3a8c8 64 62 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 2c 20  db, pColl->enc, 
3a8c9 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c 20  aSample[i].u.z, 
3a8ca 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65  aSample[i].nByte
3a8cb 2c 20 26 6e 53 61 6d 70 6c 65 0d 0a 20 20 20 20  , &nSample..    
3a8cc 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
3a8cd 20 20 20 20 69 66 28 20 21 7a 53 61 6d 70 6c 65      if( !zSample
3a8ce 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
3a8cf 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
3a8d0 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20  locFailed );..  
3a8d1 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a8d2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
3a8d3 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
3a8d4 20 20 20 20 20 20 20 63 20 3d 20 70 43 6f 6c 6c         c = pColl
3a8d5 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55  ->xCmp(pColl->pU
3a8d6 73 65 72 2c 20 6e 53 61 6d 70 6c 65 2c 20 7a 53  ser, nSample, zS
3a8d7 61 6d 70 6c 65 2c 20 6e 2c 20 7a 29 3b 0d 0a 20  ample, n, z);.. 
3a8d8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3a8d9 44 62 46 72 65 65 28 64 62 2c 20 7a 53 61 6d 70  DbFree(db, zSamp
3a8da 6c 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65  le);..        }e
3a8db 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  lse..#endif..   
3a8dc 20 20 20 20 20 7b 0d 0a 20 20 20 20 20 20 20 20       {..        
3a8dd 20 20 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d    c = pColl->xCm
3a8de 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20  p(pColl->pUser, 
3a8df 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65  aSample[i].nByte
3a8e0 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a  , aSample[i].u.z
3a8e1 2c 20 6e 2c 20 7a 29 3b 0d 0a 20 20 20 20 20 20  , n, z);..      
3a8e2 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
3a8e3 20 63 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20   c>=0 ){..      
3a8e4 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 69      if( c==0 ) i
3a8e5 73 45 71 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  sEq = 1;..      
3a8e6 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3a8e7 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
3a8e8 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
3a8e9 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
3a8ea 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 20 69 73 20  , aSample[i] is 
3a8eb 74 68 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65  the first sample
3a8ec 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65 72   that is greater
3a8ed 20 74 68 61 6e 0d 0a 20 20 2a 2a 20 6f 72 20 65   than..  ** or e
3a8ee 71 75 61 6c 20 74 6f 20 70 56 61 6c 2e 20 20 4f  qual to pVal.  O
3a8ef 72 20 69 66 20 69 3d 3d 70 49 64 78 2d 3e 6e 53  r if i==pIdx->nS
3a8f0 61 6d 70 6c 65 2c 20 74 68 65 6e 20 61 6c 6c 20  ample, then all 
3a8f1 73 61 6d 70 6c 65 73 20 61 72 65 20 6c 65 73 73  samples are less
3a8f2 0d 0a 20 20 2a 2a 20 74 68 61 6e 20 70 56 61 6c  ..  ** than pVal
3a8f3 2e 20 20 49 66 20 61 53 61 6d 70 6c 65 5b 69 5d  .  If aSample[i]
3a8f4 3d 3d 70 56 61 6c 2c 20 74 68 65 6e 20 69 73 45  ==pVal, then isE
3a8f5 71 3d 3d 31 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  q==1...  */..  i
3a8f6 66 28 20 69 73 45 71 20 29 7b 0d 0a 20 20 20 20  f( isEq ){..    
3a8f7 61 73 73 65 72 74 28 20 69 3c 70 49 64 78 2d 3e  assert( i<pIdx->
3a8f8 6e 53 61 6d 70 6c 65 20 29 3b 0d 0a 20 20 20 20  nSample );..    
3a8f9 61 53 74 61 74 5b 30 5d 20 3d 20 61 53 61 6d 70  aStat[0] = aSamp
3a8fa 6c 65 5b 69 5d 2e 6e 4c 74 3b 0d 0a 20 20 20 20  le[i].nLt;..    
3a8fb 61 53 74 61 74 5b 31 5d 20 3d 20 61 53 61 6d 70  aStat[1] = aSamp
3a8fc 6c 65 5b 69 5d 2e 6e 45 71 3b 0d 0a 20 20 7d 65  le[i].nEq;..  }e
3a8fd 6c 73 65 7b 0d 0a 20 20 20 20 74 52 6f 77 63 6e  lse{..    tRowcn
3a8fe 74 20 69 4c 6f 77 65 72 2c 20 69 55 70 70 65 72  t iLower, iUpper
3a8ff 2c 20 69 47 61 70 3b 0d 0a 20 20 20 20 69 66 28  , iGap;..    if(
3a900 20 69 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20   i==0 ){..      
3a901 69 4c 6f 77 65 72 20 3d 20 30 3b 0d 0a 20 20 20  iLower = 0;..   
3a902 20 20 20 69 55 70 70 65 72 20 3d 20 61 53 61 6d     iUpper = aSam
3a903 70 6c 65 5b 30 5d 2e 6e 4c 74 3b 0d 0a 20 20 20  ple[0].nLt;..   
3a904 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69   }else{..      i
3a905 55 70 70 65 72 20 3d 20 69 3e 3d 70 49 64 78 2d  Upper = i>=pIdx-
3a906 3e 6e 53 61 6d 70 6c 65 20 3f 20 6e 20 3a 20 61  >nSample ? n : a
3a907 53 61 6d 70 6c 65 5b 69 5d 2e 6e 4c 74 3b 0d 0a  Sample[i].nLt;..
3a908 20 20 20 20 20 20 69 4c 6f 77 65 72 20 3d 20 61        iLower = a
3a909 53 61 6d 70 6c 65 5b 69 2d 31 5d 2e 6e 45 71 20  Sample[i-1].nEq 
3a90a 2b 20 61 53 61 6d 70 6c 65 5b 69 2d 31 5d 2e 6e  + aSample[i-1].n
3a90b 4c 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  Lt;..    }..    
3a90c 61 53 74 61 74 5b 31 5d 20 3d 20 70 49 64 78 2d  aStat[1] = pIdx-
3a90d 3e 61 76 67 45 71 3b 0d 0a 20 20 20 20 69 66 28  >avgEq;..    if(
3a90e 20 69 4c 6f 77 65 72 3e 3d 69 55 70 70 65 72 20   iLower>=iUpper 
3a90f 29 7b 0d 0a 20 20 20 20 20 20 69 47 61 70 20 3d  ){..      iGap =
3a910 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   0;..    }else{.
3a911 0a 20 20 20 20 20 20 69 47 61 70 20 3d 20 69 55  .      iGap = iU
3a912 70 70 65 72 20 2d 20 69 4c 6f 77 65 72 3b 0d 0a  pper - iLower;..
3a913 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
3a914 6f 75 6e 64 55 70 20 29 7b 0d 0a 20 20 20 20 20  oundUp ){..     
3a915 20 69 47 61 70 20 3d 20 28 69 47 61 70 2a 32 29   iGap = (iGap*2)
3a916 2f 33 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  /3;..    }else{.
3a917 0a 20 20 20 20 20 20 69 47 61 70 20 3d 20 69 47  .      iGap = iG
3a918 61 70 2f 33 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ap/3;..    }..  
3a919 20 20 61 53 74 61 74 5b 30 5d 20 3d 20 69 4c 6f    aStat[0] = iLo
3a91a 77 65 72 20 2b 20 69 47 61 70 3b 0d 0a 20 20 7d  wer + iGap;..  }
3a91b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
3a91c 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  E_OK;..}..#endif
3a91d 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
3a91e 45 5f 53 54 41 54 33 20 2a 2f 0d 0a 0d 0a 2f 2a  E_STAT3 */..../*
3a91f 0d 0a 2a 2a 20 49 66 20 65 78 70 72 65 73 73 69  ..** If expressi
3a920 6f 6e 20 70 45 78 70 72 20 72 65 70 72 65 73 65  on pExpr represe
3a921 6e 74 73 20 61 20 6c 69 74 65 72 61 6c 20 76 61  nts a literal va
3a922 6c 75 65 2c 20 73 65 74 20 2a 70 70 20 74 6f 20  lue, set *pp to 
3a923 70 6f 69 6e 74 20 74 6f 0d 0a 2a 2a 20 61 6e 20  point to..** an 
3a924 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73 74  sqlite3_value st
3a925 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69  ructure containi
3a926 6e 67 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  ng the same valu
3a927 65 2c 20 77 69 74 68 20 61 66 66 69 6e 69 74 79  e, with affinity
3a928 0d 0a 2a 2a 20 61 66 66 20 61 70 70 6c 69 65 64  ..** aff applied
3a929 20 74 6f 20 69 74 2c 20 62 65 66 6f 72 65 20 72   to it, before r
3a92a 65 74 75 72 6e 69 6e 67 2e 20 49 74 20 69 73 20  eturning. It is 
3a92b 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
3a92c 74 79 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 63  ty of the ..** c
3a92d 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61  aller to eventua
3a92e 6c 6c 79 20 72 65 6c 65 61 73 65 20 74 68 69 73  lly release this
3a92f 20 73 74 72 75 63 74 75 72 65 20 62 79 20 70 61   structure by pa
3a930 73 73 69 6e 67 20 69 74 20 74 6f 20 0d 0a 2a 2a  ssing it to ..**
3a931 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
3a932 65 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  e()...**..** If 
3a933 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 72 73  the current pars
3a934 65 20 69 73 20 61 20 72 65 63 6f 6d 70 69 6c 65  e is a recompile
3a935 20 28 73 71 6c 69 74 65 33 52 65 70 72 65 70 61   (sqlite3Reprepa
3a936 72 65 28 29 29 20 61 6e 64 20 70 45 78 70 72 0d  re()) and pExpr.
3a937 0a 2a 2a 20 69 73 20 61 6e 20 53 51 4c 20 76 61  .** is an SQL va
3a938 72 69 61 62 6c 65 20 74 68 61 74 20 63 75 72 72  riable that curr
3a939 65 6e 74 6c 79 20 68 61 73 20 61 20 6e 6f 6e 2d  ently has a non-
3a93a 4e 55 4c 4c 20 76 61 6c 75 65 20 62 6f 75 6e 64  NULL value bound
3a93b 20 74 6f 20 69 74 2c 0d 0a 2a 2a 20 63 72 65 61   to it,..** crea
3a93c 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  te an sqlite3_va
3a93d 6c 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  lue structure co
3a93e 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 76 61  ntaining this va
3a93f 6c 75 65 2c 20 61 67 61 69 6e 20 77 69 74 68 0d  lue, again with.
3a940 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 61 66 66  .** affinity aff
3a941 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74 2c 20   applied to it, 
3a942 69 6e 73 74 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  instead...**..**
3a943 20 49 66 20 6e 65 69 74 68 65 72 20 6f 66 20 74   If neither of t
3a944 68 65 20 61 62 6f 76 65 20 61 70 70 6c 79 2c 20  he above apply, 
3a945 73 65 74 20 2a 70 70 20 74 6f 20 4e 55 4c 4c 2e  set *pp to NULL.
3a946 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 65  ..**..** If an e
3a947 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
3a948 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  urn an error cod
3a949 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51  e. Otherwise, SQ
3a94a 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 23 69  LITE_OK...*/..#i
3a94b 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3a94c 4c 45 5f 53 54 41 54 33 0d 0a 73 74 61 74 69 63  LE_STAT3..static
3a94d 20 69 6e 74 20 76 61 6c 75 65 46 72 6f 6d 45 78   int valueFromEx
3a94e 70 72 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50  pr(..  Parse *pP
3a94f 61 72 73 65 2c 20 0d 0a 20 20 45 78 70 72 20 2a  arse, ..  Expr *
3a950 70 45 78 70 72 2c 20 0d 0a 20 20 75 38 20 61 66  pExpr, ..  u8 af
3a951 66 2c 20 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  f, ..  sqlite3_v
3a952 61 6c 75 65 20 2a 2a 70 70 0d 0a 29 7b 0d 0a 20  alue **pp..){.. 
3a953 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d   if( pExpr->op==
3a954 54 4b 5f 56 41 52 49 41 42 4c 45 0d 0a 20 20 20  TK_VARIABLE..   
3a955 7c 7c 20 28 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  || (pExpr->op==T
3a956 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 45  K_REGISTER && pE
3a957 78 70 72 2d 3e 6f 70 32 3d 3d 54 4b 5f 56 41 52  xpr->op2==TK_VAR
3a958 49 41 42 4c 45 29 0d 0a 20 20 29 7b 0d 0a 20 20  IABLE)..  ){..  
3a959 20 20 69 6e 74 20 69 56 61 72 20 3d 20 70 45 78    int iVar = pEx
3a95a 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a 20 20  pr->iColumn;..  
3a95b 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
3a95c 56 61 72 6d 61 73 6b 28 70 50 61 72 73 65 2d 3e  Varmask(pParse->
3a95d 70 56 64 62 65 2c 20 69 56 61 72 29 3b 0d 0a 20  pVdbe, iVar);.. 
3a95e 20 20 20 2a 70 70 20 3d 20 73 71 6c 69 74 65 33     *pp = sqlite3
3a95f 56 64 62 65 47 65 74 56 61 6c 75 65 28 70 50 61  VdbeGetValue(pPa
3a960 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72 65 2c  rse->pReprepare,
3a961 20 69 56 61 72 2c 20 61 66 66 29 3b 0d 0a 20 20   iVar, aff);..  
3a962 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a963 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  OK;..  }..  retu
3a964 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  rn sqlite3ValueF
3a965 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65 2d 3e  romExpr(pParse->
3a966 64 62 2c 20 70 45 78 70 72 2c 20 53 51 4c 49 54  db, pExpr, SQLIT
3a967 45 5f 55 54 46 38 2c 20 61 66 66 2c 20 70 70 29  E_UTF8, aff, pp)
3a968 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
3a969 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
3a96a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
3a96b 65 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d  estimate the num
3a96c 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74  ber of rows that
3a96d 20 77 69 6c 6c 20 62 65 20 76 69 73 69 74 65 64   will be visited
3a96e 0d 0a 2a 2a 20 62 79 20 73 63 61 6e 6e 69 6e 67  ..** by scanning
3a96f 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20 61 20   an index for a 
3a970 72 61 6e 67 65 20 6f 66 20 76 61 6c 75 65 73 2e  range of values.
3a971 20 54 68 65 20 72 61 6e 67 65 20 6d 61 79 20 68   The range may h
3a972 61 76 65 20 61 6e 20 75 70 70 65 72 0d 0a 2a 2a  ave an upper..**
3a973 20 62 6f 75 6e 64 2c 20 61 20 6c 6f 77 65 72 20   bound, a lower 
3a974 62 6f 75 6e 64 2c 20 6f 72 20 62 6f 74 68 2e 20  bound, or both. 
3a975 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
3a976 20 74 65 72 6d 73 20 74 68 61 74 20 73 65 74 20   terms that set 
3a977 74 68 65 20 75 70 70 65 72 0d 0a 2a 2a 20 61 6e  the upper..** an
3a978 64 20 6c 6f 77 65 72 20 62 6f 75 6e 64 73 20 61  d lower bounds a
3a979 72 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  re represented b
3a97a 79 20 70 4c 6f 77 65 72 20 61 6e 64 20 70 55 70  y pLower and pUp
3a97b 70 65 72 20 72 65 73 70 65 63 74 69 76 65 6c 79  per respectively
3a97c 2e 20 46 6f 72 0d 0a 2a 2a 20 65 78 61 6d 70 6c  . For..** exampl
3a97d 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74  e, assuming that
3a97e 20 69 6e 64 65 78 20 70 20 69 73 20 6f 6e 20 74   index p is on t
3a97f 31 28 61 29 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  1(a):..**..**   
3a980 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52  ... FROM t1 WHER
3a981 45 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20  E a > ? AND a < 
3a982 3f 20 2e 2e 2e 0d 0a 2a 2a 20 20 20 20 20 20 20  ? .....**       
3a983 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 5f 5f               |__
3a984 5f 5f 5f 7c 20 20 20 7c 5f 5f 5f 5f 5f 7c 0d 0a  ___|   |_____|..
3a985 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3a986 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20           |      
3a987 20 20 20 7c 0d 0a 2a 2a 20 20 20 20 20 20 20 20     |..**        
3a988 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 6f               pLo
3a989 77 65 72 20 20 20 20 70 55 70 70 65 72 0d 0a 2a  wer    pUpper..*
3a98a 2a 0d 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  *..** If either 
3a98b 6f 66 20 74 68 65 20 75 70 70 65 72 20 6f 72 20  of the upper or 
3a98c 6c 6f 77 65 72 20 62 6f 75 6e 64 20 69 73 20 6e  lower bound is n
3a98d 6f 74 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e  ot present, then
3a98e 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20   NULL is passed 
3a98f 69 6e 0d 0a 2a 2a 20 70 6c 61 63 65 20 6f 66 20  in..** place of 
3a990 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
3a991 67 20 57 68 65 72 65 54 65 72 6d 2e 0d 0a 2a 2a  g WhereTerm...**
3a992 0d 0a 2a 2a 20 54 68 65 20 6e 45 71 20 70 61 72  ..** The nEq par
3a993 61 6d 65 74 65 72 20 69 73 20 70 61 73 73 65 64  ameter is passed
3a994 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
3a995 65 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 73  e index column s
3a996 75 62 6a 65 63 74 20 74 6f 20 74 68 65 0d 0a 2a  ubject to the..*
3a997 2a 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  * range constrai
3a998 6e 74 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65  nt. Or, equivale
3a999 6e 74 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72  ntly, the number
3a99a 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   of equality con
3a99b 73 74 72 61 69 6e 74 73 0d 0a 2a 2a 20 6f 70 74  straints..** opt
3a99c 69 6d 69 7a 65 64 20 62 79 20 74 68 65 20 70 72  imized by the pr
3a99d 6f 70 6f 73 65 64 20 69 6e 64 65 78 20 73 63 61  oposed index sca
3a99e 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  n. For example, 
3a99f 61 73 73 75 6d 69 6e 67 20 69 6e 64 65 78 20 70  assuming index p
3a9a0 20 69 73 0d 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c   is..** on t1(a,
3a9a1 20 62 29 2c 20 61 6e 64 20 74 68 65 20 53 51 4c   b), and the SQL
3a9a2 20 71 75 65 72 79 20 69 73 3a 0d 0a 2a 2a 0d 0a   query is:..**..
3a9a3 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31  **   ... FROM t1
3a9a4 20 57 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44   WHERE a = ? AND
3a9a5 20 62 20 3e 20 3f 20 41 4e 44 20 62 20 3c 20 3f   b > ? AND b < ?
3a9a6 20 2e 2e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 74 68 65   .....**..** the
3a9a7 6e 20 6e 45 71 20 73 68 6f 75 6c 64 20 62 65 20  n nEq should be 
3a9a8 70 61 73 73 65 64 20 74 68 65 20 76 61 6c 75 65  passed the value
3a9a9 20 31 20 28 61 73 20 74 68 65 20 72 61 6e 67 65   1 (as the range
3a9aa 20 72 65 73 74 72 69 63 74 65 64 20 63 6f 6c 75   restricted colu
3a9ab 6d 6e 2c 0d 0a 2a 2a 20 62 2c 20 69 73 20 74 68  mn,..** b, is th
3a9ac 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 2d 6d 6f  e second left-mo
3a9ad 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
3a9ae 20 69 6e 64 65 78 29 2e 20 4f 72 2c 20 69 66 20   index). Or, if 
3a9af 74 68 65 20 71 75 65 72 79 20 69 73 3a 0d 0a 2a  the query is:..*
3a9b0 2a 0d 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d  *..**   ... FROM
3a9b1 20 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f 20   t1 WHERE a > ? 
3a9b2 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0d 0a 2a  AND a < ? .....*
3a9b3 2a 0d 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20 73  *..** then nEq s
3a9b4 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20  hould be passed 
3a9b5 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  0...**..** The r
3a9b6 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
3a9b7 20 61 6e 20 69 6e 74 65 67 65 72 20 64 69 76 69   an integer divi
3a9b8 73 6f 72 20 74 6f 20 72 65 64 75 63 65 20 74 68  sor to reduce th
3a9b9 65 20 65 73 74 69 6d 61 74 65 64 0d 0a 2a 2a 20  e estimated..** 
3a9ba 73 65 61 72 63 68 20 73 70 61 63 65 2e 20 20 41  search space.  A
3a9bb 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
3a9bc 20 31 20 6d 65 61 6e 73 20 74 68 61 74 20 72 61   1 means that ra
3a9bd 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  nge constraints 
3a9be 61 72 65 0d 0a 2a 2a 20 6e 6f 20 68 65 6c 70 20  are..** no help 
3a9bf 61 74 20 61 6c 6c 2e 20 20 41 20 72 65 74 75 72  at all.  A retur
3a9c0 6e 20 76 61 6c 75 65 20 6f 66 20 32 20 6d 65 61  n value of 2 mea
3a9c1 6e 73 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61  ns range constra
3a9c2 69 6e 74 73 20 61 72 65 0d 0a 2a 2a 20 65 78 70  ints are..** exp
3a9c3 65 63 74 65 64 20 74 6f 20 72 65 64 75 63 65 20  ected to reduce 
3a9c4 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63 65  the search space
3a9c5 20 62 79 20 68 61 6c 66 2e 20 20 41 6e 64 20 73   by half.  And s
3a9c6 6f 20 66 6f 72 74 68 2e 2e 2e 0d 0a 2a 2a 0d 0a  o forth.....**..
3a9c7 2a 2a 20 49 6e 20 74 68 65 20 61 62 73 65 6e 63  ** In the absenc
3a9c8 65 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74  e of sqlite_stat
3a9c9 33 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20  3 ANALYZE data, 
3a9ca 65 61 63 68 20 72 61 6e 67 65 20 69 6e 65 71 75  each range inequ
3a9cb 61 6c 69 74 79 0d 0a 2a 2a 20 72 65 64 75 63 65  ality..** reduce
3a9cc 73 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61  s the search spa
3a9cd 63 65 20 62 79 20 61 20 66 61 63 74 6f 72 20 6f  ce by a factor o
3a9ce 66 20 34 2e 20 20 48 65 6e 63 65 20 61 20 73 69  f 4.  Hence a si
3a9cf 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ngle constraint 
3a9d0 28 78 3e 3f 29 0d 0a 2a 2a 20 72 65 73 75 6c 74  (x>?)..** result
3a9d1 73 20 69 6e 20 61 20 72 65 74 75 72 6e 20 6f 66  s in a return of
3a9d2 20 34 20 61 6e 64 20 61 20 72 61 6e 67 65 20 63   4 and a range c
3a9d3 6f 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f 20 41  onstraint (x>? A
3a9d4 4e 44 20 78 3c 3f 29 20 72 65 73 75 6c 74 73 0d  ND x<?) results.
3a9d5 0a 2a 2a 20 69 6e 20 61 20 72 65 74 75 72 6e 20  .** in a return 
3a9d6 6f 66 20 31 36 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  of 16...*/..stat
3a9d7 69 63 20 69 6e 74 20 77 68 65 72 65 52 61 6e 67  ic int whereRang
3a9d8 65 53 63 61 6e 45 73 74 28 0d 0a 20 20 50 61 72  eScanEst(..  Par
3a9d9 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
3a9da 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 26 20 63    /* Parsing & c
3a9db 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
3a9dc 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 49 6e 64  ontext */..  Ind
3a9dd 65 78 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ex *p,          
3a9de 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 63    /* The index c
3a9df 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 61  ontaining the ra
3a9e0 6e 67 65 2d 63 6f 6d 70 61 72 65 64 20 63 6f 6c  nge-compared col
3a9e1 75 6d 6e 3b 20 22 78 22 20 2a 2f 0d 0a 20 20 69  umn; "x" */..  i
3a9e2 6e 74 20 6e 45 71 2c 20 20 20 20 20 20 20 20 20  nt nEq,         
3a9e3 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 69 6e 74      /* index int
3a9e4 6f 20 70 2d 3e 61 43 6f 6c 5b 5d 20 6f 66 20 74  o p->aCol[] of t
3a9e5 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72 65  he range-compare
3a9e6 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 57  d column */..  W
3a9e7 68 65 72 65 54 65 72 6d 20 2a 70 4c 6f 77 65 72  hereTerm *pLower
3a9e8 2c 20 20 20 2f 2a 20 4c 6f 77 65 72 20 62 6f 75  ,   /* Lower bou
3a9e9 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e  nd on the range.
3a9ea 20 65 78 3a 20 22 78 3e 31 32 33 22 20 4d 69 67   ex: "x>123" Mig
3a9eb 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0d 0a 20  ht be NULL */.. 
3a9ec 20 57 68 65 72 65 54 65 72 6d 20 2a 70 55 70 70   WhereTerm *pUpp
3a9ed 65 72 2c 20 20 20 2f 2a 20 55 70 70 65 72 20 62  er,   /* Upper b
3a9ee 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67  ound on the rang
3a9ef 65 2e 20 65 78 3a 20 22 78 3c 34 35 35 22 20 4d  e. ex: "x<455" M
3a9f0 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0d  ight be NULL */.
3a9f1 0a 20 20 64 6f 75 62 6c 65 20 2a 70 52 61 6e 67  .  double *pRang
3a9f2 65 44 69 76 20 20 20 2f 2a 20 4f 55 54 3a 20 52  eDiv   /* OUT: R
3a9f3 65 64 75 63 65 20 73 65 61 72 63 68 20 73 70 61  educe search spa
3a9f4 63 65 20 62 79 20 74 68 69 73 20 64 69 76 69 73  ce by this divis
3a9f5 6f 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  or */..){..  int
3a9f6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3a9f7 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
3a9f8 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a  E_ENABLE_STAT3..
3a9f9 0d 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30 20 26  ..  if( nEq==0 &
3a9fa 26 20 70 2d 3e 6e 53 61 6d 70 6c 65 20 29 7b 0d  & p->nSample ){.
3a9fb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
3a9fc 75 65 20 2a 70 52 61 6e 67 65 56 61 6c 3b 0d 0a  ue *pRangeVal;..
3a9fd 20 20 20 20 74 52 6f 77 63 6e 74 20 69 4c 6f 77      tRowcnt iLow
3a9fe 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20 74 52 6f  er = 0;..    tRo
3a9ff 77 63 6e 74 20 69 55 70 70 65 72 20 3d 20 70 2d  wcnt iUpper = p-
3aa00 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0d 0a 20  >aiRowEst[0];.. 
3aa01 20 20 20 74 52 6f 77 63 6e 74 20 61 5b 32 5d 3b     tRowcnt a[2];
3aa02 0d 0a 20 20 20 20 75 38 20 61 66 66 20 3d 20 70  ..    u8 aff = p
3aa03 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70  ->pTable->aCol[p
3aa04 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 5d 2e 61  ->aiColumn[0]].a
3aa05 66 66 69 6e 69 74 79 3b 0d 0a 0d 0a 20 20 20 20  ffinity;....    
3aa06 69 66 28 20 70 4c 6f 77 65 72 20 29 7b 0d 0a 20  if( pLower ){.. 
3aa07 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
3aa08 20 3d 20 70 4c 6f 77 65 72 2d 3e 70 45 78 70 72   = pLower->pExpr
3aa09 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20  ->pRight;..     
3aa0a 20 72 63 20 3d 20 76 61 6c 75 65 46 72 6f 6d 45   rc = valueFromE
3aa0b 78 70 72 28 70 50 61 72 73 65 2c 20 70 45 78 70  xpr(pParse, pExp
3aa0c 72 2c 20 61 66 66 2c 20 26 70 52 61 6e 67 65 56  r, aff, &pRangeV
3aa0d 61 6c 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65  al);..      asse
3aa0e 72 74 28 20 70 4c 6f 77 65 72 2d 3e 65 4f 70 65  rt( pLower->eOpe
3aa0f 72 61 74 6f 72 3d 3d 57 4f 5f 47 54 20 7c 7c 20  rator==WO_GT || 
3aa10 70 4c 6f 77 65 72 2d 3e 65 4f 70 65 72 61 74 6f  pLower->eOperato
3aa11 72 3d 3d 57 4f 5f 47 45 20 29 3b 0d 0a 20 20 20  r==WO_GE );..   
3aa12 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3aa13 45 5f 4f 4b 0d 0a 20 20 20 20 20 20 20 26 26 20  E_OK..       && 
3aa14 77 68 65 72 65 4b 65 79 53 74 61 74 73 28 70 50  whereKeyStats(pP
3aa15 61 72 73 65 2c 20 70 2c 20 70 52 61 6e 67 65 56  arse, p, pRangeV
3aa16 61 6c 2c 20 30 2c 20 61 29 3d 3d 53 51 4c 49 54  al, 0, a)==SQLIT
3aa17 45 5f 4f 4b 0d 0a 20 20 20 20 20 20 29 7b 0d 0a  E_OK..      ){..
3aa18 20 20 20 20 20 20 20 20 69 4c 6f 77 65 72 20 3d          iLower =
3aa19 20 61 5b 30 5d 3b 0d 0a 20 20 20 20 20 20 20 20   a[0];..        
3aa1a 69 66 28 20 70 4c 6f 77 65 72 2d 3e 65 4f 70 65  if( pLower->eOpe
3aa1b 72 61 74 6f 72 3d 3d 57 4f 5f 47 54 20 29 20 69  rator==WO_GT ) i
3aa1c 4c 6f 77 65 72 20 2b 3d 20 61 5b 31 5d 3b 0d 0a  Lower += a[1];..
3aa1d 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73        }..      s
3aa1e 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
3aa1f 70 52 61 6e 67 65 56 61 6c 29 3b 0d 0a 20 20 20  pRangeVal);..   
3aa20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   }..    if( rc==
3aa21 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 55 70  SQLITE_OK && pUp
3aa22 70 65 72 20 29 7b 0d 0a 20 20 20 20 20 20 45 78  per ){..      Ex
3aa23 70 72 20 2a 70 45 78 70 72 20 3d 20 70 55 70 70  pr *pExpr = pUpp
3aa24 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  er->pExpr->pRigh
3aa25 74 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 76  t;..      rc = v
3aa26 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61  alueFromExpr(pPa
3aa27 72 73 65 2c 20 70 45 78 70 72 2c 20 61 66 66 2c  rse, pExpr, aff,
3aa28 20 26 70 52 61 6e 67 65 56 61 6c 29 3b 0d 0a 20   &pRangeVal);.. 
3aa29 20 20 20 20 20 61 73 73 65 72 74 28 20 70 55 70       assert( pUp
3aa2a 70 65 72 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  per->eOperator==
3aa2b 57 4f 5f 4c 54 20 7c 7c 20 70 55 70 70 65 72 2d  WO_LT || pUpper-
3aa2c 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4c  >eOperator==WO_L
3aa2d 45 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  E );..      if( 
3aa2e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20  rc==SQLITE_OK.. 
3aa2f 20 20 20 20 20 20 26 26 20 77 68 65 72 65 4b 65        && whereKe
3aa30 79 53 74 61 74 73 28 70 50 61 72 73 65 2c 20 70  yStats(pParse, p
3aa31 2c 20 70 52 61 6e 67 65 56 61 6c 2c 20 31 2c 20  , pRangeVal, 1, 
3aa32 61 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0d 0a 20  a)==SQLITE_OK.. 
3aa33 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20       ){..       
3aa34 20 69 55 70 70 65 72 20 3d 20 61 5b 30 5d 3b 0d   iUpper = a[0];.
3aa35 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 55 70  .        if( pUp
3aa36 70 65 72 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  per->eOperator==
3aa37 57 4f 5f 4c 45 20 29 20 69 55 70 70 65 72 20 2b  WO_LE ) iUpper +
3aa38 3d 20 61 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 7d  = a[1];..      }
3aa39 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
3aa3a 61 6c 75 65 46 72 65 65 28 70 52 61 6e 67 65 56  alueFree(pRangeV
3aa3b 61 6c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  al);..    }..   
3aa3c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3aa3d 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  OK ){..      if(
3aa3e 20 69 55 70 70 65 72 3c 3d 69 4c 6f 77 65 72 20   iUpper<=iLower 
3aa3f 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 70 52 61  ){..        *pRa
3aa40 6e 67 65 44 69 76 20 3d 20 28 64 6f 75 62 6c 65  ngeDiv = (double
3aa41 29 70 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b  )p->aiRowEst[0];
3aa42 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
3aa43 20 20 20 20 20 20 20 20 2a 70 52 61 6e 67 65 44          *pRangeD
3aa44 69 76 20 3d 20 28 64 6f 75 62 6c 65 29 70 2d 3e  iv = (double)p->
3aa45 61 69 52 6f 77 45 73 74 5b 30 5d 2f 28 64 6f 75  aiRowEst[0]/(dou
3aa46 62 6c 65 29 28 69 55 70 70 65 72 20 2d 20 69 4c  ble)(iUpper - iL
3aa47 6f 77 65 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  ower);..      }.
3aa48 0a 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43  .      WHERETRAC
3aa49 45 28 28 22 72 61 6e 67 65 20 73 63 61 6e 20 72  E(("range scan r
3aa4a 65 67 69 6f 6e 73 3a 20 25 75 2e 2e 25 75 20 20  egions: %u..%u  
3aa4b 64 69 76 3d 25 67 5c 6e 22 2c 0d 0a 20 20 20 20  div=%g\n",..    
3aa4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75                (u
3aa4d 33 32 29 69 4c 6f 77 65 72 2c 20 28 75 33 32 29  32)iLower, (u32)
3aa4e 69 55 70 70 65 72 2c 20 2a 70 52 61 6e 67 65 44  iUpper, *pRangeD
3aa4f 69 76 29 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  iv));..      ret
3aa50 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
3aa51 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65 6c 73      }..  }..#els
3aa52 65 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  e..  UNUSED_PARA
3aa53 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b 0d 0a  METER(pParse);..
3aa54 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
3aa55 45 52 28 70 29 3b 0d 0a 20 20 55 4e 55 53 45 44  ER(p);..  UNUSED
3aa56 5f 50 41 52 41 4d 45 54 45 52 28 6e 45 71 29 3b  _PARAMETER(nEq);
3aa57 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 61 73 73 65  ..#endif..  asse
3aa58 72 74 28 20 70 4c 6f 77 65 72 20 7c 7c 20 70 55  rt( pLower || pU
3aa59 70 70 65 72 20 29 3b 0d 0a 20 20 2a 70 52 61 6e  pper );..  *pRan
3aa5a 67 65 44 69 76 20 3d 20 28 64 6f 75 62 6c 65 29  geDiv = (double)
3aa5b 31 3b 0d 0a 20 20 69 66 28 20 70 4c 6f 77 65 72  1;..  if( pLower
3aa5c 20 26 26 20 28 70 4c 6f 77 65 72 2d 3e 77 74 46   && (pLower->wtF
3aa5d 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e 55 4c  lags & TERM_VNUL
3aa5e 4c 29 3d 3d 30 20 29 20 2a 70 52 61 6e 67 65 44  L)==0 ) *pRangeD
3aa5f 69 76 20 2a 3d 20 28 64 6f 75 62 6c 65 29 34 3b  iv *= (double)4;
3aa60 0d 0a 20 20 69 66 28 20 70 55 70 70 65 72 20 29  ..  if( pUpper )
3aa61 20 2a 70 52 61 6e 67 65 44 69 76 20 2a 3d 20 28   *pRangeDiv *= (
3aa62 64 6f 75 62 6c 65 29 34 3b 0d 0a 20 20 72 65 74  double)4;..  ret
3aa63 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 69  urn rc;..}....#i
3aa64 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3aa65 4c 45 5f 53 54 41 54 33 0d 0a 2f 2a 0d 0a 2a 2a  LE_STAT3../*..**
3aa66 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75   Estimate the nu
3aa67 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
3aa68 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  t will be return
3aa69 65 64 20 62 61 73 65 64 20 6f 6e 0d 0a 2a 2a 20  ed based on..** 
3aa6a 61 6e 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  an equality cons
3aa6b 74 72 61 69 6e 74 20 78 3d 56 41 4c 55 45 20 61  traint x=VALUE a
3aa6c 6e 64 20 77 68 65 72 65 20 74 68 61 74 20 56 41  nd where that VA
3aa6d 4c 55 45 20 6f 63 63 75 72 73 20 69 6e 0d 0a 2a  LUE occurs in..*
3aa6e 2a 20 74 68 65 20 68 69 73 74 6f 67 72 61 6d 20  * the histogram 
3aa6f 64 61 74 61 2e 20 20 54 68 69 73 20 6f 6e 6c 79  data.  This only
3aa70 20 77 6f 72 6b 73 20 77 68 65 6e 20 78 20 69 73   works when x is
3aa71 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0d 0a   the left-most..
3aa72 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 20  ** column of an 
3aa73 69 6e 64 65 78 20 61 6e 64 20 73 71 6c 69 74 65  index and sqlite
3aa74 5f 73 74 61 74 33 20 68 69 73 74 6f 67 72 61 6d  _stat3 histogram
3aa75 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62   data is availab
3aa76 6c 65 0d 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20  le..** for that 
3aa77 69 6e 64 65 78 2e 20 20 57 68 65 6e 20 70 45 78  index.  When pEx
3aa78 70 72 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65  pr==NULL that me
3aa79 61 6e 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69  ans the constrai
3aa7a 6e 74 20 69 73 0d 0a 2a 2a 20 22 78 20 49 53 20  nt is..** "x IS 
3aa7b 4e 55 4c 4c 22 20 69 6e 73 74 65 61 64 20 6f 66  NULL" instead of
3aa7c 20 22 78 3d 56 41 4c 55 45 22 2e 0d 0a 2a 2a 0d   "x=VALUE"...**.
3aa7d 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 73  .** Write the es
3aa7e 74 69 6d 61 74 65 64 20 72 6f 77 20 63 6f 75 6e  timated row coun
3aa7f 74 20 69 6e 74 6f 20 2a 70 6e 52 6f 77 20 61 6e  t into *pnRow an
3aa80 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
3aa81 4f 4b 2e 20 0d 0a 2a 2a 20 49 66 20 75 6e 61 62  OK. ..** If unab
3aa82 6c 65 20 74 6f 20 6d 61 6b 65 20 61 6e 20 65 73  le to make an es
3aa83 74 69 6d 61 74 65 2c 20 6c 65 61 76 65 20 2a 70  timate, leave *p
3aa84 6e 52 6f 77 20 75 6e 63 68 61 6e 67 65 64 20 61  nRow unchanged a
3aa85 6e 64 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 6e 6f  nd return..** no
3aa86 6e 2d 7a 65 72 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  n-zero...**..** 
3aa87 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
3aa88 20 66 61 69 6c 20 69 66 20 69 74 20 69 73 20 75   fail if it is u
3aa89 6e 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20 61 20  nable to load a 
3aa8a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
3aa8b 63 65 0d 0a 2a 2a 20 72 65 71 75 69 72 65 64 20  ce..** required 
3aa8c 66 6f 72 20 73 74 72 69 6e 67 20 63 6f 6d 70 61  for string compa
3aa8d 72 69 73 6f 6e 2c 20 6f 72 20 69 66 20 75 6e 61  rison, or if una
3aa8e 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
3aa8f 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 66 6f 72 20 61  memory..** for a
3aa90 20 55 54 46 20 63 6f 6e 76 65 72 73 69 6f 6e 20   UTF conversion 
3aa91 72 65 71 75 69 72 65 64 20 66 6f 72 20 63 6f 6d  required for com
3aa92 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 65 72  parison.  The er
3aa93 72 6f 72 20 69 73 20 73 74 6f 72 65 64 0d 0a 2a  ror is stored..*
3aa94 2a 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 20  * in the pParse 
3aa95 73 74 72 75 63 74 75 72 65 2e 0d 0a 2a 2f 0d 0a  structure...*/..
3aa96 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65  static int where
3aa97 45 71 75 61 6c 53 63 61 6e 45 73 74 28 0d 0a 20  EqualScanEst(.. 
3aa98 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
3aa99 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
3aa9a 20 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69   & code generati
3aa9b 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20  ng context */.. 
3aa9c 20 49 6e 64 65 78 20 2a 70 2c 20 20 20 20 20 20   Index *p,      
3aa9d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
3aa9e 65 78 20 77 68 6f 73 65 20 6c 65 66 74 2d 6d 6f  ex whose left-mo
3aa9f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 70 54 65  st column is pTe
3aaa0 72 6d 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  rm */..  Expr *p
3aaa1 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  Expr,         /*
3aaa2 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f 72 20   Expression for 
3aaa3 56 41 4c 55 45 20 69 6e 20 74 68 65 20 78 3d 56  VALUE in the x=V
3aaa4 41 4c 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20  ALUE constraint 
3aaa5 2a 2f 0d 0a 20 20 64 6f 75 62 6c 65 20 2a 70 6e  */..  double *pn
3aaa6 52 6f 77 20 20 20 20 20 20 20 20 2f 2a 20 57 72  Row        /* Wr
3aaa7 69 74 65 20 74 68 65 20 72 65 76 69 73 65 64 20  ite the revised 
3aaa8 72 6f 77 20 65 73 74 69 6d 61 74 65 20 68 65 72  row estimate her
3aaa9 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69  e */..){..  sqli
3aaaa 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 68 73 20  te3_value *pRhs 
3aaab 3d 20 30 3b 20 20 2f 2a 20 56 41 4c 55 45 20 6f  = 0;  /* VALUE o
3aaac 6e 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  n right-hand sid
3aaad 65 20 6f 66 20 70 54 65 72 6d 20 2a 2f 0d 0a 20  e of pTerm */.. 
3aaae 20 75 38 20 61 66 66 3b 20 20 20 20 20 20 20 20   u8 aff;        
3aaaf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
3aab0 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 2a 2f  lumn affinity */
3aab1 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
3aab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3aab3 20 53 75 62 66 75 6e 63 74 69 6f 6e 20 72 65 74   Subfunction ret
3aab4 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 74  urn code */..  t
3aab5 52 6f 77 63 6e 74 20 61 5b 32 5d 3b 20 20 20 20  Rowcnt a[2];    
3aab6 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
3aab7 69 73 74 69 63 73 20 2a 2f 0d 0a 0d 0a 20 20 61  istics */....  a
3aab8 73 73 65 72 74 28 20 70 2d 3e 61 53 61 6d 70 6c  ssert( p->aSampl
3aab9 65 21 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  e!=0 );..  asser
3aaba 74 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3e 30 20  t( p->nSample>0 
3aabb 29 3b 0d 0a 20 20 61 66 66 20 3d 20 70 2d 3e 70  );..  aff = p->p
3aabc 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d 3e 61  Table->aCol[p->a
3aabd 69 43 6f 6c 75 6d 6e 5b 30 5d 5d 2e 61 66 66 69  iColumn[0]].affi
3aabe 6e 69 74 79 3b 0d 0a 20 20 69 66 28 20 70 45 78  nity;..  if( pEx
3aabf 70 72 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  pr ){..    rc = 
3aac0 76 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 70 50  valueFromExpr(pP
3aac1 61 72 73 65 2c 20 70 45 78 70 72 2c 20 61 66 66  arse, pExpr, aff
3aac2 2c 20 26 70 52 68 73 29 3b 0d 0a 20 20 20 20 69  , &pRhs);..    i
3aac3 66 28 20 72 63 20 29 20 67 6f 74 6f 20 77 68 65  f( rc ) goto whe
3aac4 72 65 45 71 75 61 6c 53 63 61 6e 45 73 74 5f 63  reEqualScanEst_c
3aac5 61 6e 63 65 6c 3b 0d 0a 20 20 7d 65 6c 73 65 7b  ancel;..  }else{
3aac6 0d 0a 20 20 20 20 70 52 68 73 20 3d 20 73 71 6c  ..    pRhs = sql
3aac7 69 74 65 33 56 61 6c 75 65 4e 65 77 28 70 50 61  ite3ValueNew(pPa
3aac8 72 73 65 2d 3e 64 62 29 3b 0d 0a 20 20 7d 0d 0a  rse->db);..  }..
3aac9 20 20 69 66 28 20 70 52 68 73 3d 3d 30 20 29 20    if( pRhs==0 ) 
3aaca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3aacb 54 46 4f 55 4e 44 3b 0d 0a 20 20 72 63 20 3d 20  TFOUND;..  rc = 
3aacc 77 68 65 72 65 4b 65 79 53 74 61 74 73 28 70 50  whereKeyStats(pP
3aacd 61 72 73 65 2c 20 70 2c 20 70 52 68 73 2c 20 30  arse, p, pRhs, 0
3aace 2c 20 61 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d  , a);..  if( rc=
3aacf 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
3aad0 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
3aad1 65 71 75 61 6c 69 74 79 20 73 63 61 6e 20 72 65  equality scan re
3aad2 67 69 6f 6e 73 3a 20 25 64 5c 6e 22 2c 20 28 69  gions: %d\n", (i
3aad3 6e 74 29 61 5b 31 5d 29 29 3b 0d 0a 20 20 20 20  nt)a[1]));..    
3aad4 2a 70 6e 52 6f 77 20 3d 20 61 5b 31 5d 3b 0d 0a  *pnRow = a[1];..
3aad5 20 20 7d 0d 0a 77 68 65 72 65 45 71 75 61 6c 53    }..whereEqualS
3aad6 63 61 6e 45 73 74 5f 63 61 6e 63 65 6c 3a 0d 0a  canEst_cancel:..
3aad7 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
3aad8 65 65 28 70 52 68 73 29 3b 0d 0a 20 20 72 65 74  ee(pRhs);..  ret
3aad9 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 23 65 6e 64  urn rc;..}..#end
3aada 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
3aadb 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
3aadc 33 29 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20  3) */....#ifdef 
3aadd 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
3aade 41 54 33 0d 0a 2f 2a 0d 0a 2a 2a 20 45 73 74 69  AT3../*..** Esti
3aadf 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  mate the number 
3aae0 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c  of rows that wil
3aae1 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 61  l be returned ba
3aae2 73 65 64 20 6f 6e 0d 0a 2a 2a 20 61 6e 20 49 4e  sed on..** an IN
3aae3 20 63 6f 6e 73 74 72 61 69 6e 74 20 77 68 65 72   constraint wher
3aae4 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
3aae5 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e 20   side of the IN 
3aae6 6f 70 65 72 61 74 6f 72 0d 0a 2a 2a 20 69 73 20  operator..** is 
3aae7 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  a list of values
3aae8 2e 20 20 45 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d  .  Example:..**.
3aae9 0a 2a 2a 20 20 20 20 20 20 20 20 57 48 45 52 45  .**        WHERE
3aaea 20 78 20 49 4e 20 28 31 2c 32 2c 33 2c 34 29 0d   x IN (1,2,3,4).
3aaeb 0a 2a 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 74 68  .**..** Write th
3aaec 65 20 65 73 74 69 6d 61 74 65 64 20 72 6f 77 20  e estimated row 
3aaed 63 6f 75 6e 74 20 69 6e 74 6f 20 2a 70 6e 52 6f  count into *pnRo
3aaee 77 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  w and return SQL
3aaef 49 54 45 5f 4f 4b 2e 20 0d 0a 2a 2a 20 49 66 20  ITE_OK. ..** If 
3aaf0 75 6e 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61  unable to make a
3aaf1 6e 20 65 73 74 69 6d 61 74 65 2c 20 6c 65 61 76  n estimate, leav
3aaf2 65 20 2a 70 6e 52 6f 77 20 75 6e 63 68 61 6e 67  e *pnRow unchang
3aaf3 65 64 20 61 6e 64 20 72 65 74 75 72 6e 0d 0a 2a  ed and return..*
3aaf4 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 0d 0a 2a 2a 0d  * non-zero...**.
3aaf5 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3aaf6 20 63 61 6e 20 66 61 69 6c 20 69 66 20 69 74 20   can fail if it 
3aaf7 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 6c 6f 61  is unable to loa
3aaf8 64 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  d a collating se
3aaf9 71 75 65 6e 63 65 0d 0a 2a 2a 20 72 65 71 75 69  quence..** requi
3aafa 72 65 64 20 66 6f 72 20 73 74 72 69 6e 67 20 63  red for string c
3aafb 6f 6d 70 61 72 69 73 6f 6e 2c 20 6f 72 20 69 66  omparison, or if
3aafc 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63   unable to alloc
3aafd 61 74 65 20 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 66  ate memory..** f
3aafe 6f 72 20 61 20 55 54 46 20 63 6f 6e 76 65 72 73  or a UTF convers
3aaff 69 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72  ion required for
3ab00 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68   comparison.  Th
3ab01 65 20 65 72 72 6f 72 20 69 73 20 73 74 6f 72 65  e error is store
3ab02 64 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 70 50 61  d..** in the pPa
3ab03 72 73 65 20 73 74 72 75 63 74 75 72 65 2e 0d 0a  rse structure...
3ab04 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  */..static int w
3ab05 68 65 72 65 49 6e 53 63 61 6e 45 73 74 28 0d 0a  hereInScanEst(..
3ab06 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
3ab07 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
3ab08 67 20 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74  g & code generat
3ab09 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  ing context */..
3ab0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20 20 20 20    Index *p,     
3ab0b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
3ab0c 64 65 78 20 77 68 6f 73 65 20 6c 65 66 74 2d 6d  dex whose left-m
3ab0d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 70 54  ost column is pT
3ab0e 65 72 6d 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69  erm */..  ExprLi
3ab0f 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f  st *pList,     /
3ab10 2a 20 54 68 65 20 76 61 6c 75 65 20 6c 69 73 74  * The value list
3ab11 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20 22   on the RHS of "
3ab12 78 20 49 4e 20 28 76 31 2c 76 32 2c 76 33 2c 2e  x IN (v1,v2,v3,.
3ab13 2e 2e 29 22 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c  ..)" */..  doubl
3ab14 65 20 2a 70 6e 52 6f 77 20 20 20 20 20 20 20 20  e *pnRow        
3ab15 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 76  /* Write the rev
3ab16 69 73 65 64 20 72 6f 77 20 65 73 74 69 6d 61 74  ised row estimat
3ab17 65 20 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  e here */..){.. 
3ab18 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
3ab19 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  _OK;         /* 
3ab1a 53 75 62 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  Subfunction retu
3ab1b 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 64 6f  rn code */..  do
3ab1c 75 62 6c 65 20 6e 45 73 74 3b 20 20 20 20 20 20  uble nEst;      
3ab1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3ab1e 62 65 72 20 6f 66 20 72 6f 77 73 20 66 6f 72 20  ber of rows for 
3ab1f 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 2a 2f  a single term */
3ab20 0d 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 45  ..  double nRowE
3ab21 73 74 20 3d 20 28 64 6f 75 62 6c 65 29 30 3b 20  st = (double)0; 
3ab22 2f 2a 20 4e 65 77 20 65 73 74 69 6d 61 74 65 20  /* New estimate 
3ab23 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
3ab24 20 72 6f 77 73 20 2a 2f 0d 0a 20 20 69 6e 74 20   rows */..  int 
3ab25 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
3ab26 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
3ab27 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20  counter */....  
3ab28 61 73 73 65 72 74 28 20 70 2d 3e 61 53 61 6d 70  assert( p->aSamp
3ab29 6c 65 21 3d 30 20 29 3b 0d 0a 20 20 66 6f 72 28  le!=0 );..  for(
3ab2a 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
3ab2b 4f 4b 20 26 26 20 69 3c 70 4c 69 73 74 2d 3e 6e  OK && i<pList->n
3ab2c 45 78 70 72 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  Expr; i++){..   
3ab2d 20 6e 45 73 74 20 3d 20 70 2d 3e 61 69 52 6f 77   nEst = p->aiRow
3ab2e 45 73 74 5b 30 5d 3b 0d 0a 20 20 20 20 72 63 20  Est[0];..    rc 
3ab2f 3d 20 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e  = whereEqualScan
3ab30 45 73 74 28 70 50 61 72 73 65 2c 20 70 2c 20 70  Est(pParse, p, p
3ab31 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
3ab32 2c 20 26 6e 45 73 74 29 3b 0d 0a 20 20 20 20 6e  , &nEst);..    n
3ab33 52 6f 77 45 73 74 20 2b 3d 20 6e 45 73 74 3b 0d  RowEst += nEst;.
3ab34 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
3ab35 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
3ab36 20 20 69 66 28 20 6e 52 6f 77 45 73 74 20 3e 20    if( nRowEst > 
3ab37 70 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20 29  p->aiRowEst[0] )
3ab38 20 6e 52 6f 77 45 73 74 20 3d 20 70 2d 3e 61 69   nRowEst = p->ai
3ab39 52 6f 77 45 73 74 5b 30 5d 3b 0d 0a 20 20 20 20  RowEst[0];..    
3ab3a 2a 70 6e 52 6f 77 20 3d 20 6e 52 6f 77 45 73 74  *pnRow = nRowEst
3ab3b 3b 0d 0a 20 20 20 20 57 48 45 52 45 54 52 41 43  ;..    WHERETRAC
3ab3c 45 28 28 22 49 4e 20 72 6f 77 20 65 73 74 69 6d  E(("IN row estim
3ab3d 61 74 65 3a 20 65 73 74 3d 25 67 5c 6e 22 2c 20  ate: est=%g\n", 
3ab3e 6e 52 6f 77 45 73 74 29 29 3b 0d 0a 20 20 7d 0d  nRowEst));..  }.
3ab3f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
3ab40 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  ..#endif /* defi
3ab41 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3ab42 45 5f 53 54 41 54 33 29 20 2a 2f 0d 0a 0d 0a 0d  E_STAT3) */.....
3ab43 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ./*..** Find the
3ab44 20 62 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e   best query plan
3ab45 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 61   for accessing a
3ab46 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c   particular tabl
3ab47 65 2e 20 20 57 72 69 74 65 20 74 68 65 0d 0a 2a  e.  Write the..*
3ab48 2a 20 62 65 73 74 20 71 75 65 72 79 20 70 6c 61  * best query pla
3ab49 6e 20 61 6e 64 20 69 74 73 20 63 6f 73 74 20 69  n and its cost i
3ab4a 6e 74 6f 20 74 68 65 20 57 68 65 72 65 43 6f 73  nto the WhereCos
3ab4b 74 20 6f 62 6a 65 63 74 20 73 75 70 70 6c 69 65  t object supplie
3ab4c 64 20 61 73 20 74 68 65 0d 0a 2a 2a 20 6c 61 73  d as the..** las
3ab4d 74 20 70 61 72 61 6d 65 74 65 72 2e 0d 0a 2a 2a  t parameter...**
3ab4e 0d 0a 2a 2a 20 54 68 65 20 6c 6f 77 65 73 74 20  ..** The lowest 
3ab4f 63 6f 73 74 20 70 6c 61 6e 20 77 69 6e 73 2e 20  cost plan wins. 
3ab50 20 54 68 65 20 63 6f 73 74 20 69 73 20 61 6e 20   The cost is an 
3ab51 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
3ab52 61 6d 6f 75 6e 74 20 6f 66 0d 0a 2a 2a 20 43 50  amount of..** CP
3ab53 55 20 61 6e 64 20 64 69 73 6b 20 49 2f 4f 20 6e  U and disk I/O n
3ab54 65 65 64 65 64 20 74 6f 20 70 72 6f 63 65 73 73  eeded to process
3ab55 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72   the requested r
3ab56 65 73 75 6c 74 2e 0d 0a 2a 2a 20 46 61 63 74 6f  esult...** Facto
3ab57 72 73 20 74 68 61 74 20 69 6e 66 6c 75 65 6e 63  rs that influenc
3ab58 65 20 63 6f 73 74 20 69 6e 63 6c 75 64 65 3a 0d  e cost include:.
3ab59 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 2a 20 20 54 68  .**..**    *  Th
3ab5a 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d 62  e estimated numb
3ab5b 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
3ab5c 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76 65  will be retrieve
3ab5d 64 2e 20 20 28 54 68 65 0d 0a 2a 2a 20 20 20 20  d.  (The..**    
3ab5e 20 20 20 66 65 77 65 72 20 74 68 65 20 62 65 74     fewer the bet
3ab5f 74 65 72 2e 29 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ter.)..**..**   
3ab60 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e   *  Whether or n
3ab61 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75 73 74 20  ot sorting must 
3ab62 6f 63 63 75 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  occur...**..**  
3ab63 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20    *  Whether or 
3ab64 6e 6f 74 20 74 68 65 72 65 20 6d 75 73 74 20 62  not there must b
3ab65 65 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75  e separate looku
3ab66 70 73 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 20 20  ps in the..**   
3ab67 20 20 20 20 69 6e 64 65 78 20 61 6e 64 20 69 6e      index and in
3ab68 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e   the main table.
3ab69 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 72  ..**..** If ther
3ab6a 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44  e was an INDEXED
3ab6b 20 42 59 20 63 6c 61 75 73 65 20 28 70 53 72 63   BY clause (pSrc
3ab6c 2d 3e 70 49 6e 64 65 78 29 20 61 74 74 61 63 68  ->pIndex) attach
3ab6d 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
3ab6e 69 6e 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73  in..** the SQL s
3ab6f 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74  tatement, then t
3ab70 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c  his function onl
3ab71 79 20 63 6f 6e 73 69 64 65 72 73 20 70 6c 61 6e  y considers plan
3ab72 73 20 75 73 69 6e 67 20 74 68 65 20 0d 0a 2a 2a  s using the ..**
3ab73 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 49 66   named index. If
3ab74 20 6e 6f 20 73 75 63 68 20 70 6c 61 6e 20 69 73   no such plan is
3ab75 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65   found, then the
3ab76 20 72 65 74 75 72 6e 65 64 20 63 6f 73 74 20 69   returned cost i
3ab77 73 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 49 47  s..** SQLITE_BIG
3ab78 5f 44 42 4c 2e 20 49 66 20 61 20 70 6c 61 6e 20  _DBL. If a plan 
3ab79 69 73 20 66 6f 75 6e 64 20 74 68 61 74 20 75 73  is found that us
3ab7a 65 73 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64  es the named ind
3ab7b 65 78 2c 20 0d 0a 2a 2a 20 74 68 65 6e 20 74 68  ex, ..** then th
3ab7c 65 20 63 6f 73 74 20 69 73 20 63 61 6c 63 75 6c  e cost is calcul
3ab7d 61 74 65 64 20 69 6e 20 74 68 65 20 75 73 75 61  ated in the usua
3ab7e 6c 20 77 61 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  l way...**..** I
3ab7f 66 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20  f a NOT INDEXED 
3ab80 63 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 6e 6f  clause (pSrc->no
3ab81 74 49 6e 64 65 78 65 64 21 3d 30 29 20 77 61 73  tIndexed!=0) was
3ab82 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
3ab83 20 74 61 62 6c 65 20 0d 0a 2a 2a 20 69 6e 20 74   table ..** in t
3ab84 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
3ab85 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69 6e 64  ent, then no ind
3ab86 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65  exes are conside
3ab87 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68  red. However, th
3ab88 65 20 0d 0a 2a 2a 20 73 65 6c 65 63 74 65 64 20  e ..** selected 
3ab89 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c 20 74  plan may still t
3ab8a 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
3ab8b 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 6f   the built-in ro
3ab8c 77 69 64 20 70 72 69 6d 61 72 79 20 6b 65 79 0d  wid primary key.
3ab8d 0a 2a 2a 20 69 6e 64 65 78 2e 0d 0a 2a 2f 0d 0a  .** index...*/..
3ab8e 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74  static void best
3ab8f 42 74 72 65 65 49 6e 64 65 78 28 0d 0a 20 20 50  BtreeIndex(..  P
3ab90 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
3ab91 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3ab92 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
3ab93 74 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61  t */..  WhereCla
3ab94 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
3ab95 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
3ab96 20 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 73 74   clause */..  st
3ab97 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
3ab98 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65  m *pSrc,  /* The
3ab99 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
3ab9a 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0d 0a  m to search */..
3ab9b 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
3ab9c 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dy,           /*
3ab9d 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73   Mask of cursors
3ab9e 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 66   not available f
3ab9f 6f 72 20 69 6e 64 65 78 69 6e 67 20 2a 2f 0d 0a  or indexing */..
3aba0 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 56 61 6c    Bitmask notVal
3aba1 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  id,           /*
3aba2 20 43 75 72 73 6f 72 73 20 6e 6f 74 20 61 76 61   Cursors not ava
3aba3 69 6c 61 62 6c 65 20 66 6f 72 20 61 6e 79 20 70  ilable for any p
3aba4 75 72 70 6f 73 65 20 2a 2f 0d 0a 20 20 45 78 70  urpose */..  Exp
3aba5 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
3aba6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3aba7 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
3aba8 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  */..  ExprList *
3aba9 70 44 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20  pDistinct,      
3abaa 20 20 2f 2a 20 54 68 65 20 73 65 6c 65 63 74 2d    /* The select-
3abab 6c 69 73 74 20 69 66 20 71 75 65 72 79 20 69 73  list if query is
3abac 20 44 49 53 54 49 4e 43 54 20 2a 2f 0d 0a 20 20   DISTINCT */..  
3abad 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74  WhereCost *pCost
3abae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
3abaf 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79  owest cost query
3abb0 20 70 6c 61 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   plan */..){..  
3abb1 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63 2d  int iCur = pSrc-
3abb2 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54  >iCursor;   /* T
3abb3 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65  he cursor of the
3abb4 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
3abb5 65 73 73 65 64 20 2a 2f 0d 0a 20 20 49 6e 64 65  essed */..  Inde
3abb6 78 20 2a 70 50 72 6f 62 65 3b 20 20 20 20 20 20  x *pProbe;      
3abb7 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
3abb8 64 65 78 20 77 65 20 61 72 65 20 65 76 61 6c 75  dex we are evalu
3abb9 61 74 69 6e 67 20 2a 2f 0d 0a 20 20 49 6e 64 65  ating */..  Inde
3abba 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
3abbb 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
3abbc 6f 66 20 70 50 72 6f 62 65 2c 20 6f 72 20 7a 65  of pProbe, or ze
3abbd 72 6f 20 66 6f 72 20 49 50 4b 20 69 6e 64 65 78  ro for IPK index
3abbe 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 71 54 65 72   */..  int eqTer
3abbf 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  mMask;          
3abc0 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6d 61     /* Current ma
3abc1 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61  sk of valid equa
3abc2 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a  lity operators *
3abc3 2f 0d 0a 20 20 69 6e 74 20 69 64 78 45 71 54 65  /..  int idxEqTe
3abc4 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rmMask;         
3abc5 20 2f 2a 20 49 6e 64 65 78 20 6d 61 73 6b 20 6f   /* Index mask o
3abc6 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69 74 79  f valid equality
3abc7 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0d 0a 20   operators */.. 
3abc8 20 49 6e 64 65 78 20 73 50 6b 3b 20 20 20 20 20   Index sPk;     
3abc9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3abca 41 20 66 61 6b 65 20 69 6e 64 65 78 20 6f 62 6a  A fake index obj
3abcb 65 63 74 20 66 6f 72 20 74 68 65 20 70 72 69 6d  ect for the prim
3abcc 61 72 79 20 6b 65 79 20 2a 2f 0d 0a 20 20 74 52  ary key */..  tR
3abcd 6f 77 63 6e 74 20 61 69 52 6f 77 45 73 74 50 6b  owcnt aiRowEstPk
3abce 5b 32 5d 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  [2];      /* The
3abcf 20 61 69 52 6f 77 45 73 74 5b 5d 20 76 61 6c 75   aiRowEst[] valu
3abd0 65 20 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e  e for the sPk in
3abd1 64 65 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 69  dex */..  int ai
3abd2 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20 20  ColumnPk = -1;  
3abd3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 43 6f        /* The aCo
3abd4 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66 6f 72  lumn[] value for
3abd5 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20 2a   the sPk index *
3abd6 2f 0d 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 4d  /..  int wsFlagM
3abd7 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
3abd8 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61 67   /* Allowed flag
3abd9 73 20 69 6e 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  s in pCost->plan
3abda 2e 77 73 46 6c 61 67 20 2a 2f 0d 0a 0d 0a 20 20  .wsFlag */....  
3abdb 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
3abdc 65 20 63 6f 73 74 20 74 6f 20 61 20 77 6f 72 73  e cost to a wors
3abdd 74 2d 63 61 73 65 20 76 61 6c 75 65 20 2a 2f 0d  t-case value */.
3abde 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c  .  memset(pCost,
3abdf 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73   0, sizeof(*pCos
3abe0 74 29 29 3b 0d 0a 20 20 70 43 6f 73 74 2d 3e 72  t));..  pCost->r
3abe1 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49  Cost = SQLITE_BI
3abe2 47 5f 44 42 4c 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  G_DBL;....  /* I
3abe3 66 20 74 68 65 20 70 53 72 63 20 74 61 62 6c 65  f the pSrc table
3abe4 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61   is the right ta
3abe5 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  ble of a LEFT JO
3abe6 49 4e 20 74 68 65 6e 20 77 65 20 6d 61 79 20 6e  IN then we may n
3abe7 6f 74 0d 0a 20 20 2a 2a 20 75 73 65 20 61 6e 20  ot..  ** use an 
3abe8 69 6e 64 65 78 20 74 6f 20 73 61 74 69 73 66 79  index to satisfy
3abe9 20 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61   IS NULL constra
3abea 69 6e 74 73 20 6f 6e 20 74 68 61 74 20 74 61 62  ints on that tab
3abeb 6c 65 2e 20 20 54 68 69 73 20 69 73 0d 0a 20 20  le.  This is..  
3abec 2a 2a 20 62 65 63 61 75 73 65 20 63 6f 6c 75 6d  ** because colum
3abed 6e 73 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20  ns might end up 
3abee 62 65 69 6e 67 20 4e 55 4c 4c 20 69 66 20 74 68  being NULL if th
3abef 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74  e table does not
3abf0 20 6d 61 74 63 68 20 2d 0d 0a 20 20 2a 2a 20 61   match -..  ** a
3abf1 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 77 68   circumstance wh
3abf2 69 63 68 20 74 68 65 20 69 6e 64 65 78 20 63 61  ich the index ca
3abf3 6e 6e 6f 74 20 68 65 6c 70 20 75 73 20 64 69 73  nnot help us dis
3abf4 63 6f 76 65 72 2e 20 20 54 69 63 6b 65 74 20 23  cover.  Ticket #
3abf5 32 31 37 37 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  2177...  */..  i
3abf6 66 28 20 70 53 72 63 2d 3e 6a 6f 69 6e 74 79 70  f( pSrc->jointyp
3abf7 65 20 26 20 4a 54 5f 4c 45 46 54 20 29 7b 0d 0a  e & JT_LEFT ){..
3abf8 20 20 20 20 69 64 78 45 71 54 65 72 6d 4d 61 73      idxEqTermMas
3abf9 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b  k = WO_EQ|WO_IN;
3abfa 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
3abfb 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 20 3d 20  idxEqTermMask = 
3abfc 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49  WO_EQ|WO_IN|WO_I
3abfd 53 4e 55 4c 4c 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  SNULL;..  }.... 
3abfe 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65   if( pSrc->pInde
3abff 78 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 41 6e 20  x ){..    /* An 
3ac00 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3ac01 65 20 73 70 65 63 69 66 69 65 73 20 61 20 70 61  e specifies a pa
3ac02 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 74  rticular index t
3ac03 6f 20 75 73 65 20 2a 2f 0d 0a 20 20 20 20 70 49  o use */..    pI
3ac04 64 78 20 3d 20 70 50 72 6f 62 65 20 3d 20 70 53  dx = pProbe = pS
3ac05 72 63 2d 3e 70 49 6e 64 65 78 3b 0d 0a 20 20 20  rc->pIndex;..   
3ac06 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28   wsFlagMask = ~(
3ac07 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57  WHERE_ROWID_EQ|W
3ac08 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
3ac09 29 3b 0d 0a 20 20 20 20 65 71 54 65 72 6d 4d 61  );..    eqTermMa
3ac0a 73 6b 20 3d 20 69 64 78 45 71 54 65 72 6d 4d 61  sk = idxEqTermMa
3ac0b 73 6b 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  sk;..  }else{.. 
3ac0c 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e     /* There is n
3ac0d 6f 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  o INDEXED BY cla
3ac0e 75 73 65 2e 20 20 43 72 65 61 74 65 20 61 20 66  use.  Create a f
3ac0f 61 6b 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 74  ake Index object
3ac10 20 69 6e 20 6c 6f 63 61 6c 0d 0a 20 20 20 20 2a   in local..    *
3ac11 2a 20 76 61 72 69 61 62 6c 65 20 73 50 6b 20 74  * variable sPk t
3ac12 6f 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  o represent the 
3ac13 72 6f 77 69 64 20 70 72 69 6d 61 72 79 20 6b 65  rowid primary ke
3ac14 79 20 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20 74  y index.  Make t
3ac15 68 69 73 0d 0a 20 20 20 20 2a 2a 20 66 61 6b 65  his..    ** fake
3ac16 20 69 6e 64 65 78 20 74 68 65 20 66 69 72 73 74   index the first
3ac17 20 69 6e 20 61 20 63 68 61 69 6e 20 6f 66 20 49   in a chain of I
3ac18 6e 64 65 78 20 6f 62 6a 65 63 74 73 20 77 69 74  ndex objects wit
3ac19 68 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 61  h all of the rea
3ac1a 6c 0d 0a 20 20 20 20 2a 2a 20 69 6e 64 69 63 65  l..    ** indice
3ac1b 73 20 74 6f 20 66 6f 6c 6c 6f 77 20 2a 2f 0d 0a  s to follow */..
3ac1c 20 20 20 20 49 6e 64 65 78 20 2a 70 46 69 72 73      Index *pFirs
3ac1d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3ac1e 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 66 20      /* First of 
3ac1f 72 65 61 6c 20 69 6e 64 69 63 65 73 20 6f 6e 20  real indices on 
3ac20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  the table */..  
3ac21 20 20 6d 65 6d 73 65 74 28 26 73 50 6b 2c 20 30    memset(&sPk, 0
3ac22 2c 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 29  , sizeof(Index))
3ac23 3b 0d 0a 20 20 20 20 73 50 6b 2e 6e 43 6f 6c 75  ;..    sPk.nColu
3ac24 6d 6e 20 3d 20 31 3b 0d 0a 20 20 20 20 73 50 6b  mn = 1;..    sPk
3ac25 2e 61 69 43 6f 6c 75 6d 6e 20 3d 20 26 61 69 43  .aiColumn = &aiC
3ac26 6f 6c 75 6d 6e 50 6b 3b 0d 0a 20 20 20 20 73 50  olumnPk;..    sP
3ac27 6b 2e 61 69 52 6f 77 45 73 74 20 3d 20 61 69 52  k.aiRowEst = aiR
3ac28 6f 77 45 73 74 50 6b 3b 0d 0a 20 20 20 20 73 50  owEstPk;..    sP
3ac29 6b 2e 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52  k.onError = OE_R
3ac2a 65 70 6c 61 63 65 3b 0d 0a 20 20 20 20 73 50 6b  eplace;..    sPk
3ac2b 2e 70 54 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e  .pTable = pSrc->
3ac2c 70 54 61 62 3b 0d 0a 20 20 20 20 61 69 52 6f 77  pTab;..    aiRow
3ac2d 45 73 74 50 6b 5b 30 5d 20 3d 20 70 53 72 63 2d  EstPk[0] = pSrc-
3ac2e 3e 70 54 61 62 2d 3e 6e 52 6f 77 45 73 74 3b 0d  >pTab->nRowEst;.
3ac2f 0a 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b  .    aiRowEstPk[
3ac30 31 5d 20 3d 20 31 3b 0d 0a 20 20 20 20 70 46 69  1] = 1;..    pFi
3ac31 72 73 74 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  rst = pSrc->pTab
3ac32 2d 3e 70 49 6e 64 65 78 3b 0d 0a 20 20 20 20 69  ->pIndex;..    i
3ac33 66 28 20 70 53 72 63 2d 3e 6e 6f 74 49 6e 64 65  f( pSrc->notInde
3ac34 78 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  xed==0 ){..     
3ac35 20 2f 2a 20 54 68 65 20 72 65 61 6c 20 69 6e 64   /* The real ind
3ac36 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
3ac37 65 20 61 72 65 20 6f 6e 6c 79 20 63 6f 6e 73 69  e are only consi
3ac38 64 65 72 65 64 20 69 66 20 74 68 65 0d 0a 20 20  dered if the..  
3ac39 20 20 20 20 2a 2a 20 4e 4f 54 20 49 4e 44 45 58      ** NOT INDEX
3ac3a 45 44 20 71 75 61 6c 69 66 69 65 72 20 69 73 20  ED qualifier is 
3ac3b 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  omitted from the
3ac3c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0d   FROM clause */.
3ac3d 0a 20 20 20 20 20 20 73 50 6b 2e 70 4e 65 78 74  .      sPk.pNext
3ac3e 20 3d 20 70 46 69 72 73 74 3b 0d 0a 20 20 20 20   = pFirst;..    
3ac3f 7d 0d 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20  }..    pProbe = 
3ac40 26 73 50 6b 3b 0d 0a 20 20 20 20 77 73 46 6c 61  &sPk;..    wsFla
3ac41 67 4d 61 73 6b 20 3d 20 7e 28 0d 0a 20 20 20 20  gMask = ~(..    
3ac42 20 20 20 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e      WHERE_COLUMN
3ac43 5f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  _IN|WHERE_COLUMN
3ac44 5f 45 51 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  _EQ|WHERE_COLUMN
3ac45 5f 4e 55 4c 4c 7c 57 48 45 52 45 5f 43 4f 4c 55  _NULL|WHERE_COLU
3ac46 4d 4e 5f 52 41 4e 47 45 0d 0a 20 20 20 20 29 3b  MN_RANGE..    );
3ac47 0d 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b  ..    eqTermMask
3ac48 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0d   = WO_EQ|WO_IN;.
3ac49 0a 20 20 20 20 70 49 64 78 20 3d 20 30 3b 0d 0a  .    pIdx = 0;..
3ac4a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 6f 70    }....  /* Loop
3ac4b 20 6f 76 65 72 20 61 6c 6c 20 69 6e 64 69 63 65   over all indice
3ac4c 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 74 68  s looking for th
3ac4d 65 20 62 65 73 74 20 6f 6e 65 20 74 6f 20 75 73  e best one to us
3ac4e 65 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 3b  e..  */..  for(;
3ac4f 20 70 50 72 6f 62 65 3b 20 70 49 64 78 3d 70 50   pProbe; pIdx=pP
3ac50 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e 70 4e 65  robe=pProbe->pNe
3ac51 78 74 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  xt){..    const 
3ac52 74 52 6f 77 63 6e 74 20 2a 20 63 6f 6e 73 74 20  tRowcnt * const 
3ac53 61 69 52 6f 77 45 73 74 20 3d 20 70 50 72 6f 62  aiRowEst = pProb
3ac54 65 2d 3e 61 69 52 6f 77 45 73 74 3b 0d 0a 20 20  e->aiRowEst;..  
3ac55 20 20 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20    double cost;  
3ac56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ac57 20 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70   Cost of using p
3ac58 50 72 6f 62 65 20 2a 2f 0d 0a 20 20 20 20 64 6f  Probe */..    do
3ac59 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 20  uble nRow;      
3ac5a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74            /* Est
3ac5b 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  imated number of
3ac5c 20 72 6f 77 73 20 69 6e 20 72 65 73 75 6c 74 20   rows in result 
3ac5d 73 65 74 20 2a 2f 0d 0a 20 20 20 20 64 6f 75 62  set */..    doub
3ac5e 6c 65 20 6c 6f 67 31 30 4e 20 3d 20 28 64 6f 75  le log10N = (dou
3ac5f 62 6c 65 29 31 3b 20 20 2f 2a 20 62 61 73 65 2d  ble)1;  /* base-
3ac60 31 30 20 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20  10 logarithm of 
3ac61 6e 52 6f 77 20 28 69 6e 65 78 61 63 74 29 20 2a  nRow (inexact) *
3ac62 2f 0d 0a 20 20 20 20 69 6e 74 20 72 65 76 3b 20  /..    int rev; 
3ac63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac64 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 63     /* True to sc
3ac65 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  an in reverse or
3ac66 64 65 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  der */..    int 
3ac67 77 73 46 6c 61 67 73 20 3d 20 30 3b 0d 0a 20 20  wsFlags = 0;..  
3ac68 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 20 3d    Bitmask used =
3ac69 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 54 68   0;....    /* Th
3ac6a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
3ac6b 61 62 6c 65 73 20 61 72 65 20 70 6f 70 75 6c 61  ables are popula
3ac6c 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
3ac6d 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66 0d 0a   properties of..
3ac6e 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 62 65 69      ** index bei
3ac6f 6e 67 20 65 76 61 6c 75 61 74 65 64 2e 20 54 68  ng evaluated. Th
3ac70 65 79 20 61 72 65 20 74 68 65 6e 20 75 73 65 64  ey are then used
3ac71 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
3ac72 65 20 65 78 70 65 63 74 65 64 0d 0a 20 20 20 20  e expected..    
3ac73 2a 2a 20 63 6f 73 74 20 61 6e 64 20 6e 75 6d 62  ** cost and numb
3ac74 65 72 20 6f 66 20 72 6f 77 73 20 72 65 74 75 72  er of rows retur
3ac75 6e 65 64 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  ned...    **..  
3ac76 20 20 2a 2a 20 20 6e 45 71 3a 20 0d 0a 20 20 20    **  nEq: ..   
3ac77 20 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f 66   **    Number of
3ac78 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20   equality terms 
3ac79 74 68 61 74 20 63 61 6e 20 62 65 20 69 6d 70 6c  that can be impl
3ac7a 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68  emented using th
3ac7b 65 20 69 6e 64 65 78 2e 0d 0a 20 20 20 20 2a 2a  e index...    **
3ac7c 20 20 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72      In other wor
3ac7d 64 73 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ds, the number o
3ac7e 66 20 69 6e 69 74 69 61 6c 20 66 69 65 6c 64 73  f initial fields
3ac7f 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 74 68   in the index th
3ac80 61 74 0d 0a 20 20 20 20 2a 2a 20 20 20 20 61 72  at..    **    ar
3ac81 65 20 75 73 65 64 20 69 6e 20 3d 3d 20 6f 72 20  e used in == or 
3ac82 49 4e 20 6f 72 20 4e 4f 54 20 4e 55 4c 4c 20 63  IN or NOT NULL c
3ac83 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68  onstraints of th
3ac84 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0d  e WHERE clause..
3ac85 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3ac86 20 6e 49 6e 4d 75 6c 3a 20 20 0d 0a 20 20 20 20   nInMul:  ..    
3ac87 2a 2a 20 20 20 20 54 68 65 20 22 69 6e 2d 6d 75  **    The "in-mu
3ac88 6c 74 69 70 6c 69 65 72 22 2e 20 54 68 69 73 20  ltiplier". This 
3ac89 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
3ac8a 66 20 68 6f 77 20 6d 61 6e 79 20 73 65 65 6b 20  f how many seek 
3ac8b 6f 70 65 72 61 74 69 6f 6e 73 20 0d 0a 20 20 20  operations ..   
3ac8c 20 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75   **    SQLite mu
3ac8d 73 74 20 70 65 72 66 6f 72 6d 20 6f 6e 20 74 68  st perform on th
3ac8e 65 20 69 6e 64 65 78 20 69 6e 20 71 75 65 73 74  e index in quest
3ac8f 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ion. For example
3ac90 2c 20 69 66 20 74 68 65 20 0d 0a 20 20 20 20 2a  , if the ..    *
3ac91 2a 20 20 20 20 57 48 45 52 45 20 63 6c 61 75 73  *    WHERE claus
3ac92 65 20 69 73 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20  e is:..    **.. 
3ac93 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45     **      WHERE
3ac94 20 61 20 49 4e 20 28 31 2c 20 32 2c 20 33 29 20   a IN (1, 2, 3) 
3ac95 41 4e 44 20 62 20 49 4e 20 28 34 2c 20 35 2c 20  AND b IN (4, 5, 
3ac96 36 29 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  6)..    **..    
3ac97 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75 73  **    SQLite mus
3ac98 74 20 70 65 72 66 6f 72 6d 20 39 20 6c 6f 6f 6b  t perform 9 look
3ac99 75 70 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20  ups on an index 
3ac9a 6f 6e 20 28 61 2c 20 62 29 2c 20 73 6f 20 6e 49  on (a, b), so nI
3ac9b 6e 4d 75 6c 20 69 73 20 0d 0a 20 20 20 20 2a 2a  nMul is ..    **
3ac9c 20 20 20 20 73 65 74 20 74 6f 20 39 2e 20 47 69      set to 9. Gi
3ac9d 76 65 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68  ven the same sch
3ac9e 65 6d 61 20 61 6e 64 20 65 69 74 68 65 72 20 6f  ema and either o
3ac9f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3aca0 57 48 45 52 45 20 0d 0a 20 20 20 20 2a 2a 20 20  WHERE ..    **  
3aca1 20 20 63 6c 61 75 73 65 73 3a 0d 0a 20 20 20 20    clauses:..    
3aca2 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  **..    **      
3aca3 57 48 45 52 45 20 61 20 3d 20 20 31 0d 0a 20 20  WHERE a =  1..  
3aca4 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20    **      WHERE 
3aca5 61 20 3e 3d 20 32 0d 0a 20 20 20 20 2a 2a 0d 0a  a >= 2..    **..
3aca6 20 20 20 20 2a 2a 20 20 20 20 6e 49 6e 4d 75 6c      **    nInMul
3aca7 20 69 73 20 73 65 74 20 74 6f 20 31 2e 0d 0a 20   is set to 1... 
3aca8 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20     **..    **   
3aca9 20 49 66 20 74 68 65 72 65 20 65 78 69 73 74 73   If there exists
3acaa 20 61 20 57 48 45 52 45 20 74 65 72 6d 20 6f 66   a WHERE term of
3acab 20 74 68 65 20 66 6f 72 6d 20 22 78 20 49 4e 20   the form "x IN 
3acac 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2c 20 74  (SELECT ...)", t
3acad 68 65 6e 20 0d 0a 20 20 20 20 2a 2a 20 20 20 20  hen ..    **    
3acae 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 20 69  the sub-select i
3acaf 73 20 61 73 73 75 6d 65 64 20 74 6f 20 72 65 74  s assumed to ret
3acb0 75 72 6e 20 32 35 20 72 6f 77 73 20 66 6f 72 20  urn 25 rows for 
3acb1 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
3acb2 0d 0a 20 20 20 20 2a 2a 20 20 20 20 64 65 74 65  ..    **    dete
3acb3 72 6d 69 6e 69 6e 67 20 6e 49 6e 4d 75 6c 2e 0d  rmining nInMul..
3acb4 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3acb5 20 62 49 6e 45 73 74 3a 20 20 0d 0a 20 20 20 20   bInEst:  ..    
3acb6 2a 2a 20 20 20 20 53 65 74 20 74 6f 20 74 72 75  **    Set to tru
3acb7 65 20 69 66 20 74 68 65 72 65 20 77 61 73 20 61  e if there was a
3acb8 74 20 6c 65 61 73 74 20 6f 6e 65 20 22 78 20 49  t least one "x I
3acb9 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20  N (SELECT ...)" 
3acba 74 65 72 6d 20 75 73 65 64 20 0d 0a 20 20 20 20  term used ..    
3acbb 2a 2a 20 20 20 20 69 6e 20 64 65 74 65 72 6d 69  **    in determi
3acbc 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
3acbd 66 20 6e 49 6e 4d 75 6c 2e 20 20 4e 6f 74 65 20  f nInMul.  Note 
3acbe 74 68 61 74 20 74 68 65 20 52 48 53 20 6f 66 20  that the RHS of 
3acbf 74 68 65 0d 0a 20 20 20 20 2a 2a 20 20 20 20 49  the..    **    I
3acc0 4e 20 6f 70 65 72 61 74 6f 72 20 6d 75 73 74 20  N operator must 
3acc1 62 65 20 61 20 53 45 4c 45 43 54 2c 20 6e 6f 74  be a SELECT, not
3acc2 20 61 20 76 61 6c 75 65 20 6c 69 73 74 2c 20 66   a value list, f
3acc3 6f 72 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  or this variable
3acc4 0d 0a 20 20 20 20 2a 2a 20 20 20 20 74 6f 20 62  ..    **    to b
3acc5 65 20 74 72 75 65 2e 0d 0a 20 20 20 20 2a 2a 0d  e true...    **.
3acc6 0a 20 20 20 20 2a 2a 20 20 72 61 6e 67 65 44 69  .    **  rangeDi
3acc7 76 3a 0d 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e  v:..    **    An
3acc8 20 65 73 74 69 6d 61 74 65 20 6f 66 20 61 20 64   estimate of a d
3acc9 69 76 69 73 6f 72 20 62 79 20 77 68 69 63 68 20  ivisor by which 
3acca 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65  to reduce the se
3accb 61 72 63 68 20 73 70 61 63 65 20 64 75 65 0d 0a  arch space due..
3accc 20 20 20 20 2a 2a 20 20 20 20 74 6f 20 69 6e 65      **    to ine
3accd 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
3acce 6e 74 73 2e 20 20 49 6e 20 74 68 65 20 61 62 73  nts.  In the abs
3accf 65 6e 63 65 20 6f 66 20 73 71 6c 69 74 65 5f 73  ence of sqlite_s
3acd0 74 61 74 33 20 41 4e 41 4c 59 5a 45 0d 0a 20 20  tat3 ANALYZE..  
3acd1 20 20 2a 2a 20 20 20 20 64 61 74 61 2c 20 61 20    **    data, a 
3acd2 73 69 6e 67 6c 65 20 69 6e 65 71 75 61 6c 69 74  single inequalit
3acd3 79 20 72 65 64 75 63 65 73 20 74 68 65 20 73 65  y reduces the se
3acd4 61 72 63 68 20 73 70 61 63 65 20 74 6f 20 31 2f  arch space to 1/
3acd5 34 72 64 20 69 74 73 0d 0a 20 20 20 20 2a 2a 20  4rd its..    ** 
3acd6 20 20 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65     original size
3acd7 20 28 72 61 6e 67 65 44 69 76 3d 3d 34 29 2e 20   (rangeDiv==4). 
3acd8 20 54 77 6f 20 69 6e 65 71 75 61 6c 69 74 69 65   Two inequalitie
3acd9 73 20 72 65 64 75 63 65 20 74 68 65 20 73 65 61  s reduce the sea
3acda 72 63 68 0d 0a 20 20 20 20 2a 2a 20 20 20 20 73  rch..    **    s
3acdb 70 61 63 65 20 74 6f 20 31 2f 31 36 74 68 20 6f  pace to 1/16th o
3acdc 66 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73  f its original s
3acdd 69 7a 65 20 28 72 61 6e 67 65 44 69 76 3d 3d 31  ize (rangeDiv==1
3acde 36 29 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  6)...    **..   
3acdf 20 2a 2a 20 20 62 53 6f 72 74 3a 20 20 20 0d 0a   **  bSort:   ..
3ace0 20 20 20 20 2a 2a 20 20 20 20 42 6f 6f 6c 65 61      **    Boolea
3ace1 6e 2e 20 54 72 75 65 20 69 66 20 74 68 65 72 65  n. True if there
3ace2 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
3ace3 63 6c 61 75 73 65 20 74 68 61 74 20 77 69 6c 6c  clause that will
3ace4 20 72 65 71 75 69 72 65 20 61 6e 20 0d 0a 20 20   require an ..  
3ace5 20 20 2a 2a 20 20 20 20 65 78 74 65 72 6e 61 6c    **    external
3ace6 20 73 6f 72 74 20 28 69 2e 65 2e 20 73 63 61 6e   sort (i.e. scan
3ace7 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 62  ning the index b
3ace8 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 77  eing evaluated w
3ace9 69 6c 6c 20 6e 6f 74 20 0d 0a 20 20 20 20 2a 2a  ill not ..    **
3acea 20 20 20 20 63 6f 72 72 65 63 74 6c 79 20 6f 72      correctly or
3aceb 64 65 72 20 72 65 63 6f 72 64 73 29 2e 0d 0a 20  der records)... 
3acec 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 62     **..    **  b
3aced 4c 6f 6f 6b 75 70 3a 20 0d 0a 20 20 20 20 2a 2a  Lookup: ..    **
3acee 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75      Boolean. Tru
3acef 65 20 69 66 20 61 20 74 61 62 6c 65 20 6c 6f 6f  e if a table loo
3acf0 6b 75 70 20 69 73 20 72 65 71 75 69 72 65 64 20  kup is required 
3acf1 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 65  for each index e
3acf2 6e 74 72 79 0d 0a 20 20 20 20 2a 2a 20 20 20 20  ntry..    **    
3acf3 76 69 73 69 74 65 64 2e 20 20 49 6e 20 6f 74 68  visited.  In oth
3acf4 65 72 20 77 6f 72 64 73 2c 20 74 72 75 65 20 69  er words, true i
3acf5 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  f this is not a 
3acf6 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 2e 0d  covering index..
3acf7 0a 20 20 20 20 2a 2a 20 20 20 20 54 68 69 73 20  .    **    This 
3acf8 69 73 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20  is always false 
3acf9 66 6f 72 20 74 68 65 20 72 6f 77 69 64 20 70 72  for the rowid pr
3acfa 69 6d 61 72 79 20 6b 65 79 20 69 6e 64 65 78 20  imary key index 
3acfb 6f 66 20 61 20 74 61 62 6c 65 2e 0d 0a 20 20 20  of a table...   
3acfc 20 2a 2a 20 20 20 20 46 6f 72 20 6f 74 68 65 72   **    For other
3acfd 20 69 6e 64 65 78 65 73 2c 20 69 74 20 69 73 20   indexes, it is 
3acfe 74 72 75 65 20 75 6e 6c 65 73 73 20 61 6c 6c 20  true unless all 
3acff 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  the columns of t
3ad00 68 65 20 74 61 62 6c 65 0d 0a 20 20 20 20 2a 2a  he table..    **
3ad01 20 20 20 20 75 73 65 64 20 62 79 20 74 68 65 20      used by the 
3ad02 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
3ad03 20 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20   are present in 
3ad04 74 68 65 20 69 6e 64 65 78 20 28 73 75 63 68 20  the index (such 
3ad05 61 6e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 69 6e  an..    **    in
3ad06 64 65 78 20 69 73 20 73 6f 6d 65 74 69 6d 65 73  dex is sometimes
3ad07 20 64 65 73 63 72 69 62 65 64 20 61 73 20 61 20   described as a 
3ad08 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 29 2e  covering index).
3ad09 0d 0a 20 20 20 20 2a 2a 20 20 20 20 46 6f 72 20  ..    **    For 
3ad0a 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
3ad0b 68 65 20 69 6e 64 65 78 20 6f 6e 20 28 61 2c 20  he index on (a, 
3ad0c 62 29 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 6f  b), the second o
3ad0d 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3ad0e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 74 77 6f 20  ..    **    two 
3ad0f 71 75 65 72 69 65 73 20 72 65 71 75 69 72 65 73  queries requires
3ad10 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 6c 6f   table b-tree lo
3ad11 6f 6b 75 70 73 20 69 6e 20 6f 72 64 65 72 20 74  okups in order t
3ad12 6f 20 66 69 6e 64 20 74 68 65 20 76 61 6c 75 65  o find the value
3ad13 0d 0a 20 20 20 20 2a 2a 20 20 20 20 6f 66 20 63  ..    **    of c
3ad14 6f 6c 75 6d 6e 20 63 2c 20 62 75 74 20 74 68 65  olumn c, but the
3ad15 20 66 69 72 73 74 20 64 6f 65 73 20 6e 6f 74 20   first does not 
3ad16 62 65 63 61 75 73 65 20 63 6f 6c 75 6d 6e 73 20  because columns 
3ad17 61 20 61 6e 64 20 62 20 61 72 65 0d 0a 20 20 20  a and b are..   
3ad18 20 2a 2a 20 20 20 20 62 6f 74 68 20 61 76 61 69   **    both avai
3ad19 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 69 6e 64  lable in the ind
3ad1a 65 78 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  ex...    **..   
3ad1b 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
3ad1c 53 45 4c 45 43 54 20 61 2c 20 62 20 20 20 20 46  SELECT a, b    F
3ad1d 52 4f 4d 20 74 62 6c 20 57 48 45 52 45 20 61 20  ROM tbl WHERE a 
3ad1e 3d 20 31 3b 0d 0a 20 20 20 20 2a 2a 20 20 20 20  = 1;..    **    
3ad1f 20 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20           SELECT 
3ad20 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 62 6c  a, b, c FROM tbl
3ad21 20 57 48 45 52 45 20 61 20 3d 20 31 3b 0d 0a 20   WHERE a = 1;.. 
3ad22 20 20 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e     */..    int n
3ad23 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq;             
3ad24 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3ad25 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 74  er of == or IN t
3ad26 65 72 6d 73 20 6d 61 74 63 68 69 6e 67 20 69 6e  erms matching in
3ad27 64 65 78 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  dex */..    int 
3ad28 62 49 6e 45 73 74 20 3d 20 30 3b 20 20 20 20 20  bInEst = 0;     
3ad29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
3ad2a 65 20 69 66 20 22 78 20 49 4e 20 28 53 45 4c 45  e if "x IN (SELE
3ad2b 43 54 2e 2e 2e 29 22 20 73 65 65 6e 20 2a 2f 0d  CT...)" seen */.
3ad2c 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c 20  .    int nInMul 
3ad2d 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
3ad2e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3ad2f 64 69 73 74 69 6e 63 74 20 65 71 75 61 6c 69 74  distinct equalit
3ad30 69 65 73 20 74 6f 20 6c 6f 6f 6b 75 70 20 2a 2f  ies to lookup */
3ad31 0d 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 61 6e  ..    double ran
3ad32 67 65 44 69 76 20 3d 20 28 64 6f 75 62 6c 65 29  geDiv = (double)
3ad33 31 3b 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64  1;  /* Estimated
3ad34 20 72 65 64 75 63 74 69 6f 6e 20 69 6e 20 73 65   reduction in se
3ad35 61 72 63 68 20 73 70 61 63 65 20 2a 2f 0d 0a 20  arch space */.. 
3ad36 20 20 20 69 6e 74 20 6e 42 6f 75 6e 64 20 3d 20     int nBound = 
3ad37 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
3ad38 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 61   /* Number of ra
3ad39 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  nge constraints 
3ad3a 73 65 65 6e 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  seen */..    int
3ad3b 20 62 53 6f 72 74 20 3d 20 21 21 70 4f 72 64 65   bSort = !!pOrde
3ad3c 72 42 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  rBy;       /* Tr
3ad3d 75 65 20 69 66 20 65 78 74 65 72 6e 61 6c 20 73  ue if external s
3ad3e 6f 72 74 20 72 65 71 75 69 72 65 64 20 2a 2f 0d  ort required */.
3ad3f 0a 20 20 20 20 69 6e 74 20 62 44 69 73 74 20 3d  .    int bDist =
3ad40 20 21 21 70 44 69 73 74 69 6e 63 74 3b 20 20 20   !!pDistinct;   
3ad41 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
3ad42 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20  dex cannot help 
3ad43 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 2a 2f  with DISTINCT */
3ad44 0d 0a 20 20 20 20 69 6e 74 20 62 4c 6f 6f 6b 75  ..    int bLooku
3ad45 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  p = 0;          
3ad46 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e      /* True if n
3ad47 6f 74 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e  ot a covering in
3ad48 64 65 78 20 2a 2f 0d 0a 20 20 20 20 57 68 65 72  dex */..    Wher
3ad49 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
3ad4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
3ad4b 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20 74 68  ingle term of th
3ad4c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
3ad4d 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
3ad4e 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0d 0a 20  _ENABLE_STAT3.. 
3ad4f 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46     WhereTerm *pF
3ad50 69 72 73 74 54 65 72 6d 20 3d 20 30 3b 20 20 20  irstTerm = 0;   
3ad51 20 2f 2a 20 46 69 72 73 74 20 74 65 72 6d 20 6d   /* First term m
3ad52 61 74 63 68 69 6e 67 20 74 68 65 20 69 6e 64 65  atching the inde
3ad53 78 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  x */..#endif....
3ad54 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
3ad55 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 6e   the values of n
3ad56 45 71 20 61 6e 64 20 6e 49 6e 4d 75 6c 20 2a 2f  Eq and nInMul */
3ad57 0d 0a 20 20 20 20 66 6f 72 28 6e 45 71 3d 30 3b  ..    for(nEq=0;
3ad58 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f   nEq<pProbe->nCo
3ad59 6c 75 6d 6e 3b 20 6e 45 71 2b 2b 29 7b 0d 0a 20  lumn; nEq++){.. 
3ad5a 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72       int j = pPr
3ad5b 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  obe->aiColumn[nE
3ad5c 71 5d 3b 0d 0a 20 20 20 20 20 20 70 54 65 72 6d  q];..      pTerm
3ad5d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c   = findTerm(pWC,
3ad5e 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61   iCur, j, notRea
3ad5f 64 79 2c 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20  dy, eqTermMask, 
3ad60 70 49 64 78 29 3b 0d 0a 20 20 20 20 20 20 69 66  pIdx);..      if
3ad61 28 20 70 54 65 72 6d 3d 3d 30 20 29 20 62 72 65  ( pTerm==0 ) bre
3ad62 61 6b 3b 0d 0a 20 20 20 20 20 20 77 73 46 6c 61  ak;..      wsFla
3ad63 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43 4f 4c  gs |= (WHERE_COL
3ad64 55 4d 4e 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57  UMN_EQ|WHERE_ROW
3ad65 49 44 5f 45 51 29 3b 0d 0a 20 20 20 20 20 20 74  ID_EQ);..      t
3ad66 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
3ad67 70 57 43 21 3d 70 57 43 20 29 3b 0d 0a 20 20 20  pWC!=pWC );..   
3ad68 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
3ad69 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20  perator & WO_IN 
3ad6a 29 7b 0d 0a 20 20 20 20 20 20 20 20 45 78 70 72  ){..        Expr
3ad6b 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d   *pExpr = pTerm-
3ad6c 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 20  >pExpr;..       
3ad6d 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
3ad6e 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0d 0a 20 20  E_COLUMN_IN;..  
3ad6f 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
3ad70 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
3ad71 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
3ad72 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {..          /* 
3ad73 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  "x IN (SELECT ..
3ad74 2e 29 22 3a 20 20 41 73 73 75 6d 65 20 74 68 65  .)":  Assume the
3ad75 20 53 45 4c 45 43 54 20 72 65 74 75 72 6e 73 20   SELECT returns 
3ad76 32 35 20 72 6f 77 73 20 2a 2f 0d 0a 20 20 20 20  25 rows */..    
3ad77 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 2a 3d 20        nInMul *= 
3ad78 32 35 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62  25;..          b
3ad79 49 6e 45 73 74 20 3d 20 31 3b 0d 0a 20 20 20 20  InEst = 1;..    
3ad7a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c      }else if( AL
3ad7b 57 41 59 53 28 70 45 78 70 72 2d 3e 78 2e 70 4c  WAYS(pExpr->x.pL
3ad7c 69 73 74 20 26 26 20 70 45 78 70 72 2d 3e 78 2e  ist && pExpr->x.
3ad7d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b  pList->nExpr) ){
3ad7e 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22  ..          /* "
3ad7f 78 20 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c  x IN (value, val
3ad80 75 65 2c 20 2e 2e 2e 29 22 20 2a 2f 0d 0a 20 20  ue, ...)" */..  
3ad81 20 20 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 2a          nInMul *
3ad82 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
3ad83 2d 3e 6e 45 78 70 72 3b 0d 0a 20 20 20 20 20 20  ->nExpr;..      
3ad84 20 20 7d 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65    }..      }else
3ad85 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65   if( pTerm->eOpe
3ad86 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c  rator & WO_ISNUL
3ad87 4c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 77 73  L ){..        ws
3ad88 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  Flags |= WHERE_C
3ad89 4f 4c 55 4d 4e 5f 4e 55 4c 4c 3b 0d 0a 20 20 20  OLUMN_NULL;..   
3ad8a 20 20 20 7d 0d 0a 23 69 66 64 65 66 20 53 51 4c     }..#ifdef SQL
3ad8b 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
3ad8c 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 45 71 3d  ..      if( nEq=
3ad8d 3d 30 20 26 26 20 70 50 72 6f 62 65 2d 3e 61 53  =0 && pProbe->aS
3ad8e 61 6d 70 6c 65 20 29 20 70 46 69 72 73 74 54 65  ample ) pFirstTe
3ad8f 72 6d 20 3d 20 70 54 65 72 6d 3b 0d 0a 23 65 6e  rm = pTerm;..#en
3ad90 64 69 66 0d 0a 20 20 20 20 20 20 75 73 65 64 20  dif..      used 
3ad91 7c 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  |= pTerm->prereq
3ad92 52 69 67 68 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20  Right;..    }.. 
3ad93 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
3ad94 69 6e 64 65 78 20 62 65 69 6e 67 20 63 6f 6e 73  index being cons
3ad95 69 64 65 72 65 64 20 69 73 20 55 4e 49 51 55 45  idered is UNIQUE
3ad96 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 61  , and there is a
3ad97 6e 20 65 71 75 61 6c 69 74 79 20 0d 0a 20 20 20  n equality ..   
3ad98 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66   ** constraint f
3ad99 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  or all columns i
3ad9a 6e 20 74 68 65 20 69 6e 64 65 78 2c 20 74 68 65  n the index, the
3ad9b 6e 20 74 68 69 73 20 73 65 61 72 63 68 20 77 69  n this search wi
3ad9c 6c 6c 20 66 69 6e 64 0d 0a 20 20 20 20 2a 2a 20  ll find..    ** 
3ad9d 61 74 20 6d 6f 73 74 20 61 20 73 69 6e 67 6c 65  at most a single
3ad9e 20 72 6f 77 2e 20 49 6e 20 74 68 69 73 20 63 61   row. In this ca
3ad9f 73 65 20 73 65 74 20 74 68 65 20 57 48 45 52 45  se set the WHERE
3ada0 5f 55 4e 49 51 55 45 20 66 6c 61 67 20 74 6f 20  _UNIQUE flag to 
3ada1 0d 0a 20 20 20 20 2a 2a 20 69 6e 64 69 63 61 74  ..    ** indicat
3ada2 65 20 74 68 69 73 20 74 6f 20 74 68 65 20 63 61  e this to the ca
3ada3 6c 6c 65 72 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  ller...    **.. 
3ada4 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
3ada5 20 69 66 20 74 68 65 20 73 65 61 72 63 68 20 6d   if the search m
3ada6 61 79 20 66 69 6e 64 20 6d 6f 72 65 20 74 68 61  ay find more tha
3ada7 6e 20 6f 6e 65 20 72 6f 77 2c 20 74 65 73 74 20  n one row, test 
3ada8 74 6f 20 73 65 65 20 69 66 0d 0a 20 20 20 20 2a  to see if..    *
3ada9 2a 20 74 68 65 72 65 20 69 73 20 61 20 72 61 6e  * there is a ran
3adaa 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e  ge constraint on
3adab 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20   indexed column 
3adac 28 6e 45 71 2b 31 29 20 74 68 61 74 20 63 61 6e  (nEq+1) that can
3adad 20 62 65 20 0d 0a 20 20 20 20 2a 2a 20 6f 70 74   be ..    ** opt
3adae 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 74 68 65  imized using the
3adaf 20 69 6e 64 65 78 2e 20 0d 0a 20 20 20 20 2a 2f   index. ..    */
3adb0 0d 0a 20 20 20 20 69 66 28 20 6e 45 71 3d 3d 70  ..    if( nEq==p
3adb1 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 26  Probe->nColumn &
3adb2 26 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f  & pProbe->onErro
3adb3 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0d 0a 20  r!=OE_None ){.. 
3adb4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 77       testcase( w
3adb5 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43  sFlags & WHERE_C
3adb6 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0d 0a 20 20 20  OLUMN_IN );..   
3adb7 20 20 20 74 65 73 74 63 61 73 65 28 20 77 73 46     testcase( wsF
3adb8 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c  lags & WHERE_COL
3adb9 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0d 0a 20 20 20  UMN_NULL );..   
3adba 20 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20     if( (wsFlags 
3adbb 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  & (WHERE_COLUMN_
3adbc 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  IN|WHERE_COLUMN_
3adbd 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b 0d 0a 20 20  NULL))==0 ){..  
3adbe 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
3adbf 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0d 0a   WHERE_UNIQUE;..
3adc0 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c        }..    }el
3adc1 73 65 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 62  se if( pProbe->b
3adc2 55 6e 6f 72 64 65 72 65 64 3d 3d 30 20 29 7b 0d  Unordered==0 ){.
3adc3 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 28  .      int j = (
3adc4 6e 45 71 3d 3d 70 50 72 6f 62 65 2d 3e 6e 43 6f  nEq==pProbe->nCo
3adc5 6c 75 6d 6e 20 3f 20 2d 31 20 3a 20 70 50 72 6f  lumn ? -1 : pPro
3adc6 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71  be->aiColumn[nEq
3adc7 5d 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 66  ]);..      if( f
3adc8 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
3adc9 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
3adca 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47  WO_LT|WO_LE|WO_G
3adcb 54 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29 20 29  T|WO_GE, pIdx) )
3adcc 7b 0d 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  {..        Where
3adcd 54 65 72 6d 20 2a 70 54 6f 70 20 3d 20 66 69 6e  Term *pTop = fin
3adce 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
3adcf 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f   j, notReady, WO
3add0 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 70 49 64 78 29  _LT|WO_LE, pIdx)
3add1 3b 0d 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ;..        Where
3add2 54 65 72 6d 20 2a 70 42 74 6d 20 3d 20 66 69 6e  Term *pBtm = fin
3add3 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
3add4 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f   j, notReady, WO
3add5 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29  _GT|WO_GE, pIdx)
3add6 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 65 72 65  ;..        where
3add7 52 61 6e 67 65 53 63 61 6e 45 73 74 28 70 50 61  RangeScanEst(pPa
3add8 72 73 65 2c 20 70 50 72 6f 62 65 2c 20 6e 45 71  rse, pProbe, nEq
3add9 2c 20 70 42 74 6d 2c 20 70 54 6f 70 2c 20 26 72  , pBtm, pTop, &r
3adda 61 6e 67 65 44 69 76 29 3b 0d 0a 20 20 20 20 20  angeDiv);..     
3addb 20 20 20 69 66 28 20 70 54 6f 70 20 29 7b 0d 0a     if( pTop ){..
3addc 20 20 20 20 20 20 20 20 20 20 6e 42 6f 75 6e 64            nBound
3addd 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 1;..         
3adde 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
3addf 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0d 0a 20 20  E_TOP_LIMIT;..  
3ade0 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20          used |= 
3ade1 70 54 6f 70 2d 3e 70 72 65 72 65 71 52 69 67 68  pTop->prereqRigh
3ade2 74 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 74 65  t;..          te
3ade3 73 74 63 61 73 65 28 20 70 54 6f 70 2d 3e 70 57  stcase( pTop->pW
3ade4 43 21 3d 70 57 43 20 29 3b 0d 0a 20 20 20 20 20  C!=pWC );..     
3ade5 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66     }..        if
3ade6 28 20 70 42 74 6d 20 29 7b 0d 0a 20 20 20 20 20  ( pBtm ){..     
3ade7 20 20 20 20 20 6e 42 6f 75 6e 64 2b 2b 3b 0d 0a       nBound++;..
3ade8 20 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67            wsFlag
3ade9 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c  s |= WHERE_BTM_L
3adea 49 4d 49 54 3b 0d 0a 20 20 20 20 20 20 20 20 20  IMIT;..         
3adeb 20 75 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70   used |= pBtm->p
3adec 72 65 72 65 71 52 69 67 68 74 3b 0d 0a 20 20 20  rereqRight;..   
3aded 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
3adee 20 70 42 74 6d 2d 3e 70 57 43 21 3d 70 57 43 20   pBtm->pWC!=pWC 
3adef 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
3adf0 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
3adf1 3d 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  = (WHERE_COLUMN_
3adf2 52 41 4e 47 45 7c 57 48 45 52 45 5f 52 4f 57 49  RANGE|WHERE_ROWI
3adf3 44 5f 52 41 4e 47 45 29 3b 0d 0a 20 20 20 20 20  D_RANGE);..     
3adf4 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
3adf5 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
3adf6 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
3adf7 73 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  se and the index
3adf8 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65   being considere
3adf9 64 20 77 69 6c 6c 0d 0a 20 20 20 20 2a 2a 20 6e  d will..    ** n
3adfa 61 74 75 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f  aturally scan ro
3adfb 77 73 20 69 6e 20 74 68 65 20 72 65 71 75 69 72  ws in the requir
3adfc 65 64 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68  ed order, set th
3adfd 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6c  e appropriate fl
3adfe 61 67 73 0d 0a 20 20 20 20 2a 2a 20 69 6e 20 77  ags..    ** in w
3adff 73 46 6c 61 67 73 2e 20 4f 74 68 65 72 77 69 73  sFlags. Otherwis
3ae00 65 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61  e, if there is a
3ae01 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
3ae02 65 20 62 75 74 20 74 68 65 20 69 6e 64 65 78 0d  e but the index.
3ae03 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61  .    ** will sca
3ae04 6e 20 72 6f 77 73 20 69 6e 20 61 20 64 69 66 66  n rows in a diff
3ae05 65 72 65 6e 74 20 6f 72 64 65 72 2c 20 73 65 74  erent order, set
3ae06 20 74 68 65 20 62 53 6f 72 74 20 76 61 72 69 61   the bSort varia
3ae07 62 6c 65 2e 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ble.  */..    if
3ae08 28 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78  ( isSortingIndex
3ae09 28 0d 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  (..          pPa
3ae0a 72 73 65 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53  rse, pWC->pMaskS
3ae0b 65 74 2c 20 70 50 72 6f 62 65 2c 20 69 43 75 72  et, pProbe, iCur
3ae0c 2c 20 70 4f 72 64 65 72 42 79 2c 20 6e 45 71 2c  , pOrderBy, nEq,
3ae0d 20 77 73 46 6c 61 67 73 2c 20 26 72 65 76 29 0d   wsFlags, &rev).
3ae0e 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 62  .    ){..      b
3ae0f 53 6f 72 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20  Sort = 0;..     
3ae10 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
3ae11 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 7c 57 48  E_ROWID_RANGE|WH
3ae12 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45  ERE_COLUMN_RANGE
3ae13 7c 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0d  |WHERE_ORDERBY;.
3ae14 0a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c  .      wsFlags |
3ae15 3d 20 28 72 65 76 20 3f 20 57 48 45 52 45 5f 52  = (rev ? WHERE_R
3ae16 45 56 45 52 53 45 20 3a 20 30 29 3b 0d 0a 20 20  EVERSE : 0);..  
3ae17 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66    }....    /* If
3ae18 20 74 68 65 72 65 20 69 73 20 61 20 44 49 53 54   there is a DIST
3ae19 49 4e 43 54 20 71 75 61 6c 69 66 69 65 72 20 61  INCT qualifier a
3ae1a 6e 64 20 74 68 69 73 20 69 6e 64 65 78 20 77 69  nd this index wi
3ae1b 6c 6c 20 73 63 61 6e 20 72 6f 77 73 20 69 6e 0d  ll scan rows in.
3ae1c 0a 20 20 20 20 2a 2a 20 6f 72 64 65 72 20 6f 66  .    ** order of
3ae1d 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 65 78   the DISTINCT ex
3ae1e 70 72 65 73 73 69 6f 6e 73 2c 20 63 6c 65 61 72  pressions, clear
3ae1f 20 62 44 69 73 74 20 61 6e 64 20 73 65 74 20 74   bDist and set t
3ae20 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 0d 0a  he appropriate..
3ae21 20 20 20 20 2a 2a 20 66 6c 61 67 73 20 69 6e 20      ** flags in 
3ae22 77 73 46 6c 61 67 73 2e 20 2a 2f 0d 0a 20 20 20  wsFlags. */..   
3ae23 20 69 66 28 20 69 73 44 69 73 74 69 6e 63 74 49   if( isDistinctI
3ae24 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43  ndex(pParse, pWC
3ae25 2c 20 70 50 72 6f 62 65 2c 20 69 43 75 72 2c 20  , pProbe, iCur, 
3ae26 70 44 69 73 74 69 6e 63 74 2c 20 6e 45 71 29 20  pDistinct, nEq) 
3ae27 29 7b 0d 0a 20 20 20 20 20 20 62 44 69 73 74 20  ){..      bDist 
3ae28 3d 20 30 3b 0d 0a 20 20 20 20 20 20 77 73 46 6c  = 0;..      wsFl
3ae29 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 4f 57  ags |= WHERE_ROW
3ae2a 49 44 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 43  ID_RANGE|WHERE_C
3ae2b 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52  OLUMN_RANGE|WHER
3ae2c 45 5f 44 49 53 54 49 4e 43 54 3b 0d 0a 20 20 20  E_DISTINCT;..   
3ae2d 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20   }....    /* If 
3ae2e 63 75 72 72 65 6e 74 6c 79 20 63 61 6c 63 75 6c  currently calcul
3ae2f 61 74 69 6e 67 20 74 68 65 20 63 6f 73 74 20 6f  ating the cost o
3ae30 66 20 75 73 69 6e 67 20 61 6e 20 69 6e 64 65 78  f using an index
3ae31 20 28 6e 6f 74 20 74 68 65 20 49 50 4b 0d 0a 20   (not the IPK.. 
3ae32 20 20 20 2a 2a 20 69 6e 64 65 78 29 2c 20 64 65     ** index), de
3ae33 74 65 72 6d 69 6e 65 20 69 66 20 61 6c 6c 20 72  termine if all r
3ae34 65 71 75 69 72 65 64 20 63 6f 6c 75 6d 6e 20 64  equired column d
3ae35 61 74 61 20 6d 61 79 20 62 65 20 6f 62 74 61 69  ata may be obtai
3ae36 6e 65 64 20 77 69 74 68 6f 75 74 20 0d 0a 20 20  ned without ..  
3ae37 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 6d    ** using the m
3ae38 61 69 6e 20 74 61 62 6c 65 20 28 69 2e 65 2e 20  ain table (i.e. 
3ae39 69 66 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  if the index is 
3ae3a 61 20 63 6f 76 65 72 69 6e 67 0d 0a 20 20 20 20  a covering..    
3ae3b 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 69  ** index for thi
3ae3c 73 20 71 75 65 72 79 29 2e 20 49 66 20 69 74 20  s query). If it 
3ae3d 69 73 2c 20 73 65 74 20 74 68 65 20 57 48 45 52  is, set the WHER
3ae3e 45 5f 49 44 58 5f 4f 4e 4c 59 20 66 6c 61 67 20  E_IDX_ONLY flag 
3ae3f 69 6e 0d 0a 20 20 20 20 2a 2a 20 77 73 46 6c 61  in..    ** wsFla
3ae40 67 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 73  gs. Otherwise, s
3ae41 65 74 20 74 68 65 20 62 4c 6f 6f 6b 75 70 20 76  et the bLookup v
3ae42 61 72 69 61 62 6c 65 20 74 6f 20 74 72 75 65 2e  ariable to true.
3ae43 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 49    */..    if( pI
3ae44 64 78 20 26 26 20 77 73 46 6c 61 67 73 20 29 7b  dx && wsFlags ){
3ae45 0d 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20  ..      Bitmask 
3ae46 6d 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65  m = pSrc->colUse
3ae47 64 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  d;..      int j;
3ae48 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ..      for(j=0;
3ae49 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
3ae4a 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; j++){..       
3ae4b 20 69 6e 74 20 78 20 3d 20 70 49 64 78 2d 3e 61   int x = pIdx->a
3ae4c 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0d 0a 20 20 20  iColumn[j];..   
3ae4d 20 20 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31       if( x<BMS-1
3ae4e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 6d   ){..          m
3ae4f 20 26 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b 29   &= ~(((Bitmask)
3ae50 31 29 3c 3c 78 29 3b 0d 0a 20 20 20 20 20 20 20  1)<<x);..       
3ae51 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
3ae52 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0d 0a     if( m==0 ){..
3ae53 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
3ae54 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  |= WHERE_IDX_ONL
3ae55 59 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  Y;..      }else{
3ae56 0d 0a 20 20 20 20 20 20 20 20 62 4c 6f 6f 6b 75  ..        bLooku
3ae57 70 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d  p = 1;..      }.
3ae58 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
3ae59 0d 0a 20 20 20 20 2a 2a 20 45 73 74 69 6d 61 74  ..    ** Estimat
3ae5a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
3ae5b 72 6f 77 73 20 6f 66 20 6f 75 74 70 75 74 2e 20  rows of output. 
3ae5c 20 46 6f 72 20 61 6e 20 22 78 20 49 4e 20 28 53   For an "x IN (S
3ae5d 45 4c 45 43 54 2e 2e 2e 29 22 0d 0a 20 20 20 20  ELECT...)"..    
3ae5e 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 64  ** constraint, d
3ae5f 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 65 73  o not let the es
3ae60 74 69 6d 61 74 65 20 65 78 63 65 65 64 20 68 61  timate exceed ha
3ae61 6c 66 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74  lf the rows in t
3ae62 68 65 20 74 61 62 6c 65 2e 0d 0a 20 20 20 20 2a  he table...    *
3ae63 2f 0d 0a 20 20 20 20 6e 52 6f 77 20 3d 20 28 64  /..    nRow = (d
3ae64 6f 75 62 6c 65 29 28 61 69 52 6f 77 45 73 74 5b  ouble)(aiRowEst[
3ae65 6e 45 71 5d 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0d  nEq] * nInMul);.
3ae66 0a 20 20 20 20 69 66 28 20 62 49 6e 45 73 74 20  .    if( bInEst 
3ae67 26 26 20 6e 52 6f 77 2a 32 3e 61 69 52 6f 77 45  && nRow*2>aiRowE
3ae68 73 74 5b 30 5d 20 29 7b 0d 0a 20 20 20 20 20 20  st[0] ){..      
3ae69 6e 52 6f 77 20 3d 20 61 69 52 6f 77 45 73 74 5b  nRow = aiRowEst[
3ae6a 30 5d 2f 32 3b 0d 0a 20 20 20 20 20 20 6e 49 6e  0]/2;..      nIn
3ae6b 4d 75 6c 20 3d 20 28 69 6e 74 29 28 6e 52 6f 77  Mul = (int)(nRow
3ae6c 20 2f 20 61 69 52 6f 77 45 73 74 5b 6e 45 71 5d   / aiRowEst[nEq]
3ae6d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69 66  );..    }....#if
3ae6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
3ae6f 45 5f 53 54 41 54 33 0d 0a 20 20 20 20 2f 2a 20  E_STAT3..    /* 
3ae70 49 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  If the constrain
3ae71 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
3ae72 20 78 3d 56 41 4c 55 45 20 6f 72 20 78 20 49 4e   x=VALUE or x IN
3ae73 20 28 45 31 2c 45 32 2c 2e 2e 2e 29 0d 0a 20 20   (E1,E2,...)..  
3ae74 20 20 2a 2a 20 61 6e 64 20 77 65 20 64 6f 20 6e    ** and we do n
3ae75 6f 74 20 74 68 69 6e 6b 20 74 68 61 74 20 76 61  ot think that va
3ae76 6c 75 65 73 20 6f 66 20 78 20 61 72 65 20 75 6e  lues of x are un
3ae77 69 71 75 65 20 61 6e 64 20 69 66 20 68 69 73 74  ique and if hist
3ae78 6f 67 72 61 6d 0d 0a 20 20 20 20 2a 2a 20 64 61  ogram..    ** da
3ae79 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  ta is available 
3ae7a 66 6f 72 20 63 6f 6c 75 6d 6e 20 78 2c 20 74 68  for column x, th
3ae7b 65 6e 20 69 74 20 6d 69 67 68 74 20 62 65 20 70  en it might be p
3ae7c 6f 73 73 69 62 6c 65 0d 0a 20 20 20 20 2a 2a 20  ossible..    ** 
3ae7d 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20  to get a better 
3ae7e 65 73 74 69 6d 61 74 65 20 6f 6e 20 74 68 65 20  estimate on the 
3ae7f 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 62  number of rows b
3ae80 61 73 65 64 20 6f 6e 0d 0a 20 20 20 20 2a 2a 20  ased on..    ** 
3ae81 56 41 4c 55 45 20 61 6e 64 20 68 6f 77 20 63 6f  VALUE and how co
3ae82 6d 6d 6f 6e 20 74 68 61 74 20 76 61 6c 75 65 20  mmon that value 
3ae83 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  is according to 
3ae84 74 68 65 20 68 69 73 74 6f 67 72 61 6d 2e 0d 0a  the histogram...
3ae85 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
3ae86 6e 52 6f 77 3e 28 64 6f 75 62 6c 65 29 31 20 26  nRow>(double)1 &
3ae87 26 20 6e 45 71 3d 3d 31 20 26 26 20 70 46 69 72  & nEq==1 && pFir
3ae88 73 74 54 65 72 6d 21 3d 30 20 26 26 20 61 69 52  stTerm!=0 && aiR
3ae89 6f 77 45 73 74 5b 31 5d 3e 31 20 29 7b 0d 0a 20  owEst[1]>1 ){.. 
3ae8a 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 46       assert( (pF
3ae8b 69 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61  irstTerm->eOpera
3ae8c 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f  tor & (WO_EQ|WO_
3ae8d 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 21 3d  ISNULL|WO_IN))!=
3ae8e 30 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  0 );..      if( 
3ae8f 70 46 69 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65  pFirstTerm->eOpe
3ae90 72 61 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57  rator & (WO_EQ|W
3ae91 4f 5f 49 53 4e 55 4c 4c 29 20 29 7b 0d 0a 20 20  O_ISNULL) ){..  
3ae92 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3ae93 70 46 69 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65  pFirstTerm->eOpe
3ae94 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0d  rator==WO_EQ );.
3ae95 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
3ae96 65 28 20 70 46 69 72 73 74 54 65 72 6d 2d 3e 65  e( pFirstTerm->e
3ae97 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e  Operator==WO_ISN
3ae98 55 4c 4c 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ULL );..        
3ae99 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e 45 73  whereEqualScanEs
3ae9a 74 28 70 50 61 72 73 65 2c 20 70 50 72 6f 62 65  t(pParse, pProbe
3ae9b 2c 20 70 46 69 72 73 74 54 65 72 6d 2d 3e 70 45  , pFirstTerm->pE
3ae9c 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 6e 52  xpr->pRight, &nR
3ae9d 6f 77 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  ow);..      }els
3ae9e 65 20 69 66 28 20 62 49 6e 45 73 74 3d 3d 30 20  e if( bInEst==0 
3ae9f 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  ){..        asse
3aea0 72 74 28 20 70 46 69 72 73 74 54 65 72 6d 2d 3e  rt( pFirstTerm->
3aea1 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e  eOperator==WO_IN
3aea2 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 65   );..        whe
3aea3 72 65 49 6e 53 63 61 6e 45 73 74 28 70 50 61 72  reInScanEst(pPar
3aea4 73 65 2c 20 70 50 72 6f 62 65 2c 20 70 46 69 72  se, pProbe, pFir
3aea5 73 74 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 78  stTerm->pExpr->x
3aea6 2e 70 4c 69 73 74 2c 20 26 6e 52 6f 77 29 3b 0d  .pList, &nRow);.
3aea7 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
3aea8 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
3aea9 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 20 2a  E_ENABLE_STAT3 *
3aeaa 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 64 6a 75  /....    /* Adju
3aeab 73 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  st the number of
3aeac 20 6f 75 74 70 75 74 20 72 6f 77 73 20 61 6e 64   output rows and
3aead 20 64 6f 77 6e 77 61 72 64 20 74 6f 20 72 65 66   downward to ref
3aeae 6c 65 63 74 20 72 6f 77 73 0d 0a 20 20 20 20 2a  lect rows..    *
3aeaf 2a 20 74 68 61 74 20 61 72 65 20 65 78 63 6c 75  * that are exclu
3aeb0 64 65 64 20 62 79 20 72 61 6e 67 65 20 63 6f 6e  ded by range con
3aeb1 73 74 72 61 69 6e 74 73 2e 0d 0a 20 20 20 20 2a  straints...    *
3aeb2 2f 0d 0a 20 20 20 20 6e 52 6f 77 20 3d 20 6e 52  /..    nRow = nR
3aeb3 6f 77 2f 72 61 6e 67 65 44 69 76 3b 0d 0a 20 20  ow/rangeDiv;..  
3aeb4 20 20 69 66 28 20 6e 52 6f 77 3c 31 20 29 20 6e    if( nRow<1 ) n
3aeb5 52 6f 77 20 3d 20 31 3b 0d 0a 0d 0a 20 20 20 20  Row = 1;....    
3aeb6 2f 2a 20 45 78 70 65 72 69 6d 65 6e 74 73 20 72  /* Experiments r
3aeb7 75 6e 20 6f 6e 20 72 65 61 6c 20 53 51 4c 69 74  un on real SQLit
3aeb8 65 20 64 61 74 61 62 61 73 65 73 20 73 68 6f 77  e databases show
3aeb9 20 74 68 61 74 20 74 68 65 20 74 69 6d 65 20 6e   that the time n
3aeba 65 65 64 65 64 0d 0a 20 20 20 20 2a 2a 20 74 6f  eeded..    ** to
3aebb 20 64 6f 20 61 20 62 69 6e 61 72 79 20 73 65 61   do a binary sea
3aebc 72 63 68 20 74 6f 20 6c 6f 63 61 74 65 20 61 20  rch to locate a 
3aebd 72 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f  row in a table o
3aebe 72 20 69 6e 64 65 78 20 69 73 20 72 6f 75 67 68  r index is rough
3aebf 6c 79 0d 0a 20 20 20 20 2a 2a 20 6c 6f 67 31 30  ly..    ** log10
3aec0 28 4e 29 20 74 69 6d 65 73 20 74 68 65 20 74 69  (N) times the ti
3aec1 6d 65 20 74 6f 20 6d 6f 76 65 20 66 72 6f 6d 20  me to move from 
3aec2 6f 6e 65 20 72 6f 77 20 74 6f 20 74 68 65 20 6e  one row to the n
3aec3 65 78 74 20 72 6f 77 20 77 69 74 68 69 6e 0d 0a  ext row within..
3aec4 20 20 20 20 2a 2a 20 61 20 74 61 62 6c 65 20 6f      ** a table o
3aec5 72 20 69 6e 64 65 78 2e 20 20 54 68 65 20 61 63  r index.  The ac
3aec6 74 75 61 6c 20 74 69 6d 65 73 20 63 61 6e 20 76  tual times can v
3aec7 61 72 79 2c 20 77 69 74 68 20 74 68 65 20 73 69  ary, with the si
3aec8 7a 65 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 72 65  ze of..    ** re
3aec9 63 6f 72 64 73 20 62 65 69 6e 67 20 61 6e 20 69  cords being an i
3aeca 6d 70 6f 72 74 61 6e 74 20 66 61 63 74 6f 72 2e  mportant factor.
3aecb 20 20 42 6f 74 68 20 6d 6f 76 65 73 20 61 6e 64    Both moves and
3aecc 20 73 65 61 72 63 68 65 73 20 61 72 65 0d 0a 20   searches are.. 
3aecd 20 20 20 2a 2a 20 73 6c 6f 77 65 72 20 77 69 74     ** slower wit
3aece 68 20 6c 61 72 67 65 72 20 72 65 63 6f 72 64 73  h larger records
3aecf 2c 20 70 72 65 73 75 6d 61 62 6c 79 20 62 65 63  , presumably bec
3aed0 61 75 73 65 20 66 65 77 65 72 20 72 65 63 6f 72  ause fewer recor
3aed1 64 73 20 66 69 74 0d 0a 20 20 20 20 2a 2a 20 6f  ds fit..    ** o
3aed2 6e 20 6f 6e 65 20 70 61 67 65 20 61 6e 64 20 68  n one page and h
3aed3 65 6e 63 65 20 6d 6f 72 65 20 70 61 67 65 73 20  ence more pages 
3aed4 68 61 76 65 20 74 6f 20 62 65 20 66 65 74 63 68  have to be fetch
3aed5 65 64 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  ed...    **..   
3aed6 20 2a 2a 20 54 68 65 20 41 4e 41 4c 59 5a 45 20   ** The ANALYZE 
3aed7 63 6f 6d 6d 61 6e 64 20 61 6e 64 20 74 68 65 20  command and the 
3aed8 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64  sqlite_stat1 and
3aed9 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61   sqlite_stat3 ta
3aeda 62 6c 65 73 20 64 6f 0d 0a 20 20 20 20 2a 2a 20  bles do..    ** 
3aedb 6e 6f 74 20 67 69 76 65 20 75 73 20 64 61 74 61  not give us data
3aedc 20 6f 6e 20 74 68 65 20 72 65 6c 61 74 69 76 65   on the relative
3aedd 20 73 69 7a 65 73 20 6f 66 20 74 61 62 6c 65 20   sizes of table 
3aede 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64  and index record
3aedf 73 2e 0d 0a 20 20 20 20 2a 2a 20 53 6f 20 74 68  s...    ** So th
3aee0 69 73 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 61  is computation a
3aee1 73 73 75 6d 65 73 20 74 61 62 6c 65 20 72 65 63  ssumes table rec
3aee2 6f 72 64 73 20 61 72 65 20 61 62 6f 75 74 20 74  ords are about t
3aee3 77 69 63 65 20 61 73 20 62 69 67 0d 0a 20 20 20  wice as big..   
3aee4 20 2a 2a 20 61 73 20 69 6e 64 65 78 20 72 65 63   ** as index rec
3aee5 6f 72 64 73 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ords..    */..  
3aee6 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20 26    if( (wsFlags &
3aee7 20 57 48 45 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53   WHERE_NOT_FULLS
3aee8 43 41 4e 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  CAN)==0 ){..    
3aee9 20 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 6f 66    /* The cost of
3aeea 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63   a full table sc
3aeeb 61 6e 20 69 73 20 61 20 6e 75 6d 62 65 72 20 6f  an is a number o
3aeec 66 20 6d 6f 76 65 20 6f 70 65 72 61 74 69 6f 6e  f move operation
3aeed 73 20 65 71 75 61 6c 0d 0a 20 20 20 20 20 20 2a  s equal..      *
3aeee 2a 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  * to the number 
3aeef 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74  of rows in the t
3aef0 61 62 6c 65 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d  able...      **.
3aef1 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 61 64 64  .      ** We add
3aef2 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 34   an additional 4
3aef3 78 20 70 65 6e 61 6c 74 79 20 74 6f 20 66 75 6c  x penalty to ful
3aef4 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 2e 20 20  l table scans.  
3aef5 54 68 69 73 20 63 61 75 73 65 73 0d 0a 20 20 20  This causes..   
3aef6 20 20 20 2a 2a 20 74 68 65 20 63 6f 73 74 20 66     ** the cost f
3aef7 75 6e 63 74 69 6f 6e 20 74 6f 20 65 72 72 20 6f  unction to err o
3aef8 6e 20 74 68 65 20 73 69 64 65 20 6f 66 20 63 68  n the side of ch
3aef9 6f 6f 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 20  oosing an index 
3aefa 6f 76 65 72 0d 0a 20 20 20 20 20 20 2a 2a 20 63  over..      ** c
3aefb 68 6f 6f 73 69 6e 67 20 61 20 66 75 6c 6c 20 73  hoosing a full s
3aefc 63 61 6e 2e 20 20 54 68 69 73 20 34 78 20 66 75  can.  This 4x fu
3aefd 6c 6c 2d 73 63 61 6e 20 70 65 6e 61 6c 74 79 20  ll-scan penalty 
3aefe 69 73 20 61 6e 20 61 72 67 75 61 62 6c 65 0d 0a  is an arguable..
3aeff 20 20 20 20 20 20 2a 2a 20 64 65 63 69 73 69 6f        ** decisio
3af00 6e 20 61 6e 64 20 6f 6e 65 20 77 68 69 63 68 20  n and one which 
3af01 77 65 20 65 78 70 65 63 74 20 74 6f 20 72 65 76  we expect to rev
3af02 69 73 69 74 20 69 6e 20 74 68 65 20 66 75 74 75  isit in the futu
3af03 72 65 2e 20 20 42 75 74 0d 0a 20 20 20 20 20 20  re.  But..      
3af04 2a 2a 20 69 74 20 73 65 65 6d 73 20 74 6f 20 62  ** it seems to b
3af05 65 20 77 6f 72 6b 69 6e 67 20 77 65 6c 6c 20 65  e working well e
3af06 6e 6f 75 67 68 20 61 74 20 74 68 65 20 6d 6f 6d  nough at the mom
3af07 65 6e 74 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a  ent...      */..
3af08 20 20 20 20 20 20 63 6f 73 74 20 3d 20 61 69 52        cost = aiR
3af09 6f 77 45 73 74 5b 30 5d 2a 34 3b 0d 0a 20 20 20  owEst[0]*4;..   
3af0a 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6c   }else{..      l
3af0b 6f 67 31 30 4e 20 3d 20 65 73 74 4c 6f 67 28 61  og10N = estLog(a
3af0c 69 52 6f 77 45 73 74 5b 30 5d 29 3b 0d 0a 20 20  iRowEst[0]);..  
3af0d 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77 3b      cost = nRow;
3af0e 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ..      if( pIdx
3af0f 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28   ){..        if(
3af10 20 62 4c 6f 6f 6b 75 70 20 29 7b 0d 0a 20 20 20   bLookup ){..   
3af11 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e         /* For an
3af12 20 69 6e 64 65 78 20 6c 6f 6f 6b 75 70 20 66 6f   index lookup fo
3af13 6c 6c 6f 77 65 64 20 62 79 20 61 20 74 61 62 6c  llowed by a tabl
3af14 65 20 6c 6f 6f 6b 75 70 3a 0d 0a 20 20 20 20 20  e lookup:..     
3af15 20 20 20 20 20 2a 2a 20 20 20 20 6e 49 6e 4d 75       **    nInMu
3af16 6c 20 69 6e 64 65 78 20 73 65 61 72 63 68 65 73  l index searches
3af17 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 74 61   to find the sta
3af18 72 74 20 6f 66 20 65 61 63 68 20 69 6e 64 65 78  rt of each index
3af19 20 72 61 6e 67 65 0d 0a 20 20 20 20 20 20 20 20   range..        
3af1a 20 20 2a 2a 20 20 2b 20 6e 52 6f 77 20 73 74 65    **  + nRow ste
3af1b 70 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 69  ps through the i
3af1c 6e 64 65 78 0d 0a 20 20 20 20 20 20 20 20 20 20  ndex..          
3af1d 2a 2a 20 20 2b 20 6e 52 6f 77 20 74 61 62 6c 65  **  + nRow table
3af1e 20 73 65 61 72 63 68 65 73 20 74 6f 20 6c 6f 6f   searches to loo
3af1f 6b 75 70 20 74 68 65 20 74 61 62 6c 65 20 65 6e  kup the table en
3af20 74 72 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f  try using the ro
3af21 77 69 64 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  wid..          *
3af22 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73  /..          cos
3af23 74 20 2b 3d 20 28 6e 49 6e 4d 75 6c 20 2b 20 6e  t += (nInMul + n
3af24 52 6f 77 29 2a 6c 6f 67 31 30 4e 3b 0d 0a 20 20  Row)*log10N;..  
3af25 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3af26 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61          /* For a
3af27 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 3a   covering index:
3af28 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 20  ..          **  
3af29 20 20 20 6e 49 6e 4d 75 6c 20 69 6e 64 65 78 20     nInMul index 
3af2a 73 65 61 72 63 68 65 73 20 74 6f 20 66 69 6e 64  searches to find
3af2b 20 74 68 65 20 69 6e 69 74 69 61 6c 20 65 6e 74   the initial ent
3af2c 72 79 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  ry ..          *
3af2d 2a 20 20 20 2b 20 6e 52 6f 77 20 73 74 65 70 73  *   + nRow steps
3af2e 20 74 68 72 6f 75 67 68 20 74 68 65 20 69 6e 64   through the ind
3af2f 65 78 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ex..          */
3af30 0d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73 74  ..          cost
3af31 20 2b 3d 20 6e 49 6e 4d 75 6c 2a 6c 6f 67 31 30   += nInMul*log10
3af32 4e 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  N;..        }.. 
3af33 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
3af34 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 72 6f       /* For a ro
3af35 77 69 64 20 70 72 69 6d 61 72 79 20 6b 65 79 20  wid primary key 
3af36 6c 6f 6f 6b 75 70 3a 0d 0a 20 20 20 20 20 20 20  lookup:..       
3af37 20 2a 2a 20 20 20 20 6e 49 6e 4d 75 6c 74 20 74   **    nInMult t
3af38 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 74 6f  able searches to
3af39 20 66 69 6e 64 20 74 68 65 20 69 6e 69 74 69 61   find the initia
3af3a 6c 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  l entry for each
3af3b 20 72 61 6e 67 65 0d 0a 20 20 20 20 20 20 20 20   range..        
3af3c 2a 2a 20 20 2b 20 6e 52 6f 77 20 73 74 65 70 73  **  + nRow steps
3af3d 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62   through the tab
3af3e 6c 65 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a  le..        */..
3af3f 20 20 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20          cost += 
3af40 6e 49 6e 4d 75 6c 2a 6c 6f 67 31 30 4e 3b 0d 0a  nInMul*log10N;..
3af41 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
3af42 0d 0a 20 20 20 20 2f 2a 20 41 64 64 20 69 6e 20  ..    /* Add in 
3af43 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 63 6f  the estimated co
3af44 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 74 68  st of sorting th
3af45 65 20 72 65 73 75 6c 74 2e 20 20 41 63 74 75 61  e result.  Actua
3af46 6c 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 0d 0a  l experimental..
3af47 20 20 20 20 2a 2a 20 6d 65 61 73 75 72 65 6d 65      ** measureme
3af48 6e 74 73 20 6f 66 20 73 6f 72 74 69 6e 67 20 70  nts of sorting p
3af49 65 72 66 6f 72 6d 61 6e 63 65 20 69 6e 20 53 51  erformance in SQ
3af4a 4c 69 74 65 20 73 68 6f 77 20 74 68 61 74 20 73  Lite show that s
3af4b 6f 72 74 69 6e 67 20 74 69 6d 65 0d 0a 20 20 20  orting time..   
3af4c 20 2a 2a 20 61 64 64 73 20 43 2a 4e 2a 6c 6f 67   ** adds C*N*log
3af4d 31 30 28 4e 29 20 74 6f 20 74 68 65 20 63 6f 73  10(N) to the cos
3af4e 74 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  t, where N is th
3af4f 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
3af50 20 74 6f 20 62 65 20 0d 0a 20 20 20 20 2a 2a 20   to be ..    ** 
3af51 73 6f 72 74 65 64 20 61 6e 64 20 43 20 69 73 20  sorted and C is 
3af52 61 20 66 61 63 74 6f 72 20 62 65 74 77 65 65 6e  a factor between
3af53 20 31 2e 39 35 20 61 6e 64 20 34 2e 33 2e 20 20   1.95 and 4.3.  
3af54 57 65 20 77 69 6c 6c 20 73 70 6c 69 74 20 74 68  We will split th
3af55 65 0d 0a 20 20 20 20 2a 2a 20 64 69 66 66 65 72  e..    ** differ
3af56 65 6e 63 65 20 61 6e 64 20 73 65 6c 65 63 74 20  ence and select 
3af57 43 20 6f 66 20 33 2e 30 2e 0d 0a 20 20 20 20 2a  C of 3.0...    *
3af58 2f 0d 0a 20 20 20 20 69 66 28 20 62 53 6f 72 74  /..    if( bSort
3af59 20 29 7b 0d 0a 20 20 20 20 20 20 63 6f 73 74 20   ){..      cost 
3af5a 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c 6f 67 28 6e  += nRow*estLog(n
3af5b 52 6f 77 29 2a 33 3b 0d 0a 20 20 20 20 7d 0d 0a  Row)*3;..    }..
3af5c 20 20 20 20 69 66 28 20 62 44 69 73 74 20 29 7b      if( bDist ){
3af5d 0d 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20  ..      cost += 
3af5e 6e 52 6f 77 2a 65 73 74 4c 6f 67 28 6e 52 6f 77  nRow*estLog(nRow
3af5f 29 2a 33 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  )*3;..    }.... 
3af60 20 20 20 2f 2a 2a 2a 2a 20 43 6f 73 74 20 6f 66     /**** Cost of
3af61 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65   using this inde
3af62 78 20 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 63  x has now been c
3af63 6f 6d 70 75 74 65 64 20 2a 2a 2a 2a 2f 0d 0a 0d  omputed ****/...
3af64 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
3af65 20 61 72 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   are additional 
3af66 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74  constraints on t
3af67 68 69 73 20 74 61 62 6c 65 20 74 68 61 74 20 63  his table that c
3af68 61 6e 6e 6f 74 0d 0a 20 20 20 20 2a 2a 20 62 65  annot..    ** be
3af69 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20 63   used with the c
3af6a 75 72 72 65 6e 74 20 69 6e 64 65 78 2c 20 62 75  urrent index, bu
3af6b 74 20 77 68 69 63 68 20 6d 69 67 68 74 20 6c 6f  t which might lo
3af6c 77 65 72 20 74 68 65 20 6e 75 6d 62 65 72 0d 0a  wer the number..
3af6d 20 20 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74      ** of output
3af6e 20 72 6f 77 73 2c 20 61 64 6a 75 73 74 20 74 68   rows, adjust th
3af6f 65 20 6e 52 6f 77 20 76 61 6c 75 65 20 61 63 63  e nRow value acc
3af70 6f 72 64 69 6e 67 6c 79 2e 20 20 54 68 69 73 20  ordingly.  This 
3af71 6f 6e 6c 79 20 0d 0a 20 20 20 20 2a 2a 20 6d 61  only ..    ** ma
3af72 74 74 65 72 73 20 69 66 20 74 68 65 20 63 75 72  tters if the cur
3af73 72 65 6e 74 20 69 6e 64 65 78 20 69 73 20 74 68  rent index is th
3af74 65 20 6c 65 61 73 74 20 63 6f 73 74 6c 79 2c 20  e least costly, 
3af75 73 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72  so do not bother
3af76 0d 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68  ..    ** with th
3af77 69 73 20 73 74 65 70 20 69 66 20 77 65 20 61 6c  is step if we al
3af78 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 69 73 20  ready know this 
3af79 69 6e 64 65 78 20 77 69 6c 6c 20 6e 6f 74 20 62  index will not b
3af7a 65 20 63 68 6f 73 65 6e 2e 0d 0a 20 20 20 20 2a  e chosen...    *
3af7b 2a 20 41 6c 73 6f 2c 20 6e 65 76 65 72 20 72 65  * Also, never re
3af7c 64 75 63 65 20 74 68 65 20 6f 75 74 70 75 74 20  duce the output 
3af7d 72 6f 77 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20  row count below 
3af7e 32 20 75 73 69 6e 67 20 74 68 69 73 20 73 74 65  2 using this ste
3af7f 70 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  p...    **..    
3af80 2a 2a 20 49 74 20 69 73 20 63 72 69 74 69 63 61  ** It is critica
3af81 6c 20 74 68 61 74 20 74 68 65 20 6e 6f 74 56 61  l that the notVa
3af82 6c 69 64 20 6d 61 73 6b 20 62 65 20 75 73 65 64  lid mask be used
3af83 20 68 65 72 65 20 69 6e 73 74 65 61 64 20 6f 66   here instead of
3af84 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 6f 74  ..    ** the not
3af85 52 65 61 64 79 20 6d 61 73 6b 2e 20 20 57 68 65  Ready mask.  Whe
3af86 6e 20 63 6f 6d 70 75 74 69 6e 67 20 61 6e 20 22  n computing an "
3af87 6f 70 74 69 6d 61 6c 22 20 69 6e 64 65 78 2c 20  optimal" index, 
3af88 74 68 65 20 6e 6f 74 52 65 61 64 79 0d 0a 20 20  the notReady..  
3af89 20 20 2a 2a 20 6d 61 73 6b 20 77 69 6c 6c 20 6f    ** mask will o
3af8a 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 62 69 74  nly have one bit
3af8b 20 73 65 74 20 2d 20 74 68 65 20 62 69 74 20 66   set - the bit f
3af8c 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
3af8d 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2a 20 54 68  able...    ** Th
3af8e 65 20 6e 6f 74 56 61 6c 69 64 20 6d 61 73 6b 2c  e notValid mask,
3af8f 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   on the other ha
3af90 6e 64 2c 20 61 6c 77 61 79 73 20 68 61 73 20 61  nd, always has a
3af91 6c 6c 20 62 69 74 73 20 73 65 74 20 66 6f 72 0d  ll bits set for.
3af92 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20 74  .    ** tables t
3af93 68 61 74 20 61 72 65 20 6e 6f 74 20 69 6e 20 6f  hat are not in o
3af94 75 74 65 72 20 6c 6f 6f 70 73 2e 20 20 49 66 20  uter loops.  If 
3af95 6e 6f 74 52 65 61 64 79 20 69 73 20 75 73 65 64  notReady is used
3af96 20 68 65 72 65 20 69 6e 73 74 65 61 64 0d 0a 20   here instead.. 
3af97 20 20 20 2a 2a 20 6f 66 20 6e 6f 74 56 61 6c 69     ** of notVali
3af98 64 2c 20 74 68 65 6e 20 61 20 6f 70 74 69 6d 61  d, then a optima
3af99 6c 20 69 6e 64 65 78 20 74 68 61 74 20 64 65 70  l index that dep
3af9a 65 6e 64 73 20 6f 6e 20 69 6e 6e 65 72 20 6a 6f  ends on inner jo
3af9b 69 6e 73 20 6c 6f 6f 70 73 0d 0a 20 20 20 20 2a  ins loops..    *
3af9c 2a 20 6d 69 67 68 74 20 62 65 20 73 65 6c 65 63  * might be selec
3af9d 74 65 64 20 65 76 65 6e 20 77 68 65 6e 20 74 68  ted even when th
3af9e 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 6f 70  ere exists an op
3af9f 74 69 6d 61 6c 20 69 6e 64 65 78 20 74 68 61 74  timal index that
3afa0 20 68 61 73 0d 0a 20 20 20 20 2a 2a 20 6e 6f 20   has..    ** no 
3afa1 73 75 63 68 20 64 65 70 65 6e 64 65 6e 63 79 2e  such dependency.
3afa2 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
3afa3 28 20 6e 52 6f 77 3e 32 20 26 26 20 63 6f 73 74  ( nRow>2 && cost
3afa4 3c 3d 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29  <=pCost->rCost )
3afa5 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 20  {..      int k; 
3afa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afa7 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
3afa8 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20 20  unter */..      
3afa9 69 6e 74 20 6e 53 6b 69 70 45 71 20 3d 20 6e 45  int nSkipEq = nE
3afaa 71 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  q;           /* 
3afab 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f 6e  Number of == con
3afac 73 74 72 61 69 6e 74 73 20 74 6f 20 73 6b 69 70  straints to skip
3afad 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   */..      int n
3afae 53 6b 69 70 52 61 6e 67 65 20 3d 20 6e 42 6f 75  SkipRange = nBou
3afaf 6e 64 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  nd;     /* Numbe
3afb0 72 20 6f 66 20 3c 20 63 6f 6e 73 74 72 61 69 6e  r of < constrain
3afb1 74 73 20 74 6f 20 73 6b 69 70 20 2a 2f 0d 0a 20  ts to skip */.. 
3afb2 20 20 20 20 20 42 69 74 6d 61 73 6b 20 74 68 69       Bitmask thi
3afb3 73 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  sTab;           
3afb4 20 20 2f 2a 20 42 69 74 6d 61 70 20 66 6f 72 20    /* Bitmap for 
3afb5 70 53 72 63 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20  pSrc */....     
3afb6 20 74 68 69 73 54 61 62 20 3d 20 67 65 74 4d 61   thisTab = getMa
3afb7 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74  sk(pWC->pMaskSet
3afb8 2c 20 69 43 75 72 29 3b 0d 0a 20 20 20 20 20 20  , iCur);..      
3afb9 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61  for(pTerm=pWC->a
3afba 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20  , k=pWC->nTerm; 
3afbb 6e 52 6f 77 3e 32 20 26 26 20 6b 3b 20 6b 2d 2d  nRow>2 && k; k--
3afbc 2c 20 70 54 65 72 6d 2b 2b 29 7b 0d 0a 20 20 20  , pTerm++){..   
3afbd 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
3afbe 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  wtFlags & TERM_V
3afbf 49 52 54 55 41 4c 20 29 20 63 6f 6e 74 69 6e 75  IRTUAL ) continu
3afc0 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  e;..        if( 
3afc1 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
3afc2 6c 20 26 20 6e 6f 74 56 61 6c 69 64 29 21 3d 74  l & notValid)!=t
3afc3 68 69 73 54 61 62 20 29 20 63 6f 6e 74 69 6e 75  hisTab ) continu
3afc4 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  e;..        if( 
3afc5 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
3afc6 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c   & (WO_EQ|WO_IN|
3afc7 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 7b 0d 0a 20  WO_ISNULL) ){.. 
3afc8 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 53 6b           if( nSk
3afc9 69 70 45 71 20 29 7b 0d 0a 20 20 20 20 20 20 20  ipEq ){..       
3afca 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74       /* Ignore t
3afcb 68 65 20 66 69 72 73 74 20 6e 45 71 20 65 71 75  he first nEq equ
3afcc 61 6c 69 74 79 20 6d 61 74 63 68 65 73 20 73 69  ality matches si
3afcd 6e 63 65 20 74 68 65 20 69 6e 64 65 78 0d 0a 20  nce the index.. 
3afce 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61             ** ha
3afcf 73 20 61 6c 72 65 61 64 79 20 61 63 63 6f 75 6e  s already accoun
3afd0 74 65 64 20 66 6f 72 20 74 68 65 73 65 20 2a 2f  ted for these */
3afd1 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 53  ..            nS
3afd2 6b 69 70 45 71 2d 2d 3b 0d 0a 20 20 20 20 20 20  kipEq--;..      
3afd3 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
3afd4 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d          /* Assum
3afd5 65 20 65 61 63 68 20 61 64 64 69 74 69 6f 6e 61  e each additiona
3afd6 6c 20 65 71 75 61 6c 69 74 79 20 6d 61 74 63 68  l equality match
3afd7 20 72 65 64 75 63 65 73 20 74 68 65 20 72 65 73   reduces the res
3afd8 75 6c 74 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ult..           
3afd9 20 2a 2a 20 73 65 74 20 73 69 7a 65 20 62 79 20   ** set size by 
3afda 61 20 66 61 63 74 6f 72 20 6f 66 20 31 30 20 2a  a factor of 10 *
3afdb 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  /..            n
3afdc 52 6f 77 20 2f 3d 20 31 30 3b 0d 0a 20 20 20 20  Row /= 10;..    
3afdd 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
3afde 20 7d 65 6c 73 65 20 69 66 28 20 70 54 65 72 6d   }else if( pTerm
3afdf 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57  ->eOperator & (W
3afe0 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54  O_LT|WO_LE|WO_GT
3afe1 7c 57 4f 5f 47 45 29 20 29 7b 0d 0a 20 20 20 20  |WO_GE) ){..    
3afe2 20 20 20 20 20 20 69 66 28 20 6e 53 6b 69 70 52        if( nSkipR
3afe3 61 6e 67 65 20 29 7b 0d 0a 20 20 20 20 20 20 20  ange ){..       
3afe4 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74       /* Ignore t
3afe5 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 52 61  he first nSkipRa
3afe6 6e 67 65 20 72 61 6e 67 65 20 63 6f 6e 73 74 72  nge range constr
3afe7 61 69 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20  aints since the 
3afe8 69 6e 64 65 78 0d 0a 20 20 20 20 20 20 20 20 20  index..         
3afe9 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64     ** has alread
3afea 79 20 61 63 63 6f 75 6e 74 65 64 20 66 6f 72 20  y accounted for 
3afeb 74 68 65 73 65 20 2a 2f 0d 0a 20 20 20 20 20 20  these */..      
3afec 20 20 20 20 20 20 6e 53 6b 69 70 52 61 6e 67 65        nSkipRange
3afed 2d 2d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  --;..          }
3afee 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
3afef 20 20 20 2f 2a 20 41 73 73 75 6d 65 20 65 61 63     /* Assume eac
3aff0 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 61 6e  h additional ran
3aff1 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65  ge constraint re
3aff2 64 75 63 65 73 20 74 68 65 20 72 65 73 75 6c 74  duces the result
3aff3 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  ..            **
3aff4 20 73 65 74 20 73 69 7a 65 20 62 79 20 61 20 66   set size by a f
3aff5 61 63 74 6f 72 20 6f 66 20 33 2e 20 20 49 6e 64  actor of 3.  Ind
3aff6 65 78 65 64 20 72 61 6e 67 65 20 63 6f 6e 73 74  exed range const
3aff7 72 61 69 6e 74 73 20 72 65 64 75 63 65 0d 0a 20  raints reduce.. 
3aff8 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
3aff9 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20 62  e search space b
3affa 79 20 61 20 6c 61 72 67 65 72 20 66 61 63 74 6f  y a larger facto
3affb 72 3a 20 34 2e 20 20 57 65 20 6d 61 6b 65 20 69  r: 4.  We make i
3affc 6e 64 65 78 65 64 20 72 61 6e 67 65 0d 0a 20 20  ndexed range..  
3affd 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 72            ** mor
3affe 65 20 73 65 6c 65 63 74 69 76 65 20 69 6e 74 65  e selective inte
3afff 6e 74 69 6f 6e 61 6c 6c 79 20 62 65 63 61 75 73  ntionally becaus
3b000 65 20 6f 66 20 74 68 65 20 73 75 62 6a 65 63 74  e of the subject
3b001 69 76 65 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ive ..          
3b002 20 20 2a 2a 20 6f 62 73 65 72 76 61 74 69 6f 6e    ** observation
3b003 20 74 68 61 74 20 69 6e 64 65 78 65 64 20 72 61   that indexed ra
3b004 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  nge constraints 
3b005 72 65 61 6c 6c 79 20 61 72 65 20 6d 6f 72 65 0d  really are more.
3b006 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
3b007 73 65 6c 65 63 74 69 76 65 20 69 6e 20 70 72 61  selective in pra
3b008 63 74 69 63 65 2c 20 6f 6e 20 61 76 65 72 61 67  ctice, on averag
3b009 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  e. */..         
3b00a 20 20 20 6e 52 6f 77 20 2f 3d 20 33 3b 0d 0a 20     nRow /= 3;.. 
3b00b 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
3b00c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54      }else if( pT
3b00d 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d  erm->eOperator!=
3b00e 57 4f 5f 4e 4f 4f 50 20 29 7b 0d 0a 20 20 20 20  WO_NOOP ){..    
3b00f 20 20 20 20 20 20 2f 2a 20 41 6e 79 20 6f 74 68        /* Any oth
3b010 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f  er expression lo
3b011 77 65 72 73 20 74 68 65 20 6f 75 74 70 75 74 20  wers the output 
3b012 72 6f 77 20 63 6f 75 6e 74 20 62 79 20 68 61 6c  row count by hal
3b013 66 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20  f */..          
3b014 6e 52 6f 77 20 2f 3d 20 32 3b 0d 0a 20 20 20 20  nRow /= 2;..    
3b015 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
3b016 20 20 20 20 20 20 69 66 28 20 6e 52 6f 77 3c 32        if( nRow<2
3b017 20 29 20 6e 52 6f 77 20 3d 20 32 3b 0d 0a 20 20   ) nRow = 2;..  
3b018 20 20 7d 0d 0a 0d 0a 0d 0a 20 20 20 20 57 48 45    }......    WHE
3b019 52 45 54 52 41 43 45 28 28 0d 0a 20 20 20 20 20  RETRACE((..     
3b01a 20 22 25 73 28 25 73 29 3a 20 6e 45 71 3d 25 64   "%s(%s): nEq=%d
3b01b 20 6e 49 6e 4d 75 6c 3d 25 64 20 72 61 6e 67 65   nInMul=%d range
3b01c 44 69 76 3d 25 64 20 62 53 6f 72 74 3d 25 64 20  Div=%d bSort=%d 
3b01d 62 4c 6f 6f 6b 75 70 3d 25 64 20 77 73 46 6c 61  bLookup=%d wsFla
3b01e 67 73 3d 30 78 25 78 5c 6e 22 0d 0a 20 20 20 20  gs=0x%x\n"..    
3b01f 20 20 22 20 20 20 20 20 20 20 20 20 6e 6f 74 52    "         notR
3b020 65 61 64 79 3d 30 78 25 6c 6c 78 20 6c 6f 67 31  eady=0x%llx log1
3b021 30 4e 3d 25 2e 31 66 20 6e 52 6f 77 3d 25 2e 31  0N=%.1f nRow=%.1
3b022 66 20 63 6f 73 74 3d 25 2e 31 66 20 75 73 65 64  f cost=%.1f used
3b023 3d 30 78 25 6c 6c 78 5c 6e 22 2c 0d 0a 20 20 20  =0x%llx\n",..   
3b024 20 20 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a     pSrc->pTab->z
3b025 4e 61 6d 65 2c 20 28 70 49 64 78 20 3f 20 70 49  Name, (pIdx ? pI
3b026 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 69 70 6b  dx->zName : "ipk
3b027 22 29 2c 20 0d 0a 20 20 20 20 20 20 6e 45 71 2c  "), ..      nEq,
3b028 20 6e 49 6e 4d 75 6c 2c 20 28 69 6e 74 29 72 61   nInMul, (int)ra
3b029 6e 67 65 44 69 76 2c 20 62 53 6f 72 74 2c 20 62  ngeDiv, bSort, b
3b02a 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c 61 67 73 2c  Lookup, wsFlags,
3b02b 0d 0a 20 20 20 20 20 20 6e 6f 74 52 65 61 64 79  ..      notReady
3b02c 2c 20 6c 6f 67 31 30 4e 2c 20 6e 52 6f 77 2c 20  , log10N, nRow, 
3b02d 63 6f 73 74 2c 20 75 73 65 64 0d 0a 20 20 20 20  cost, used..    
3b02e 29 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66  ));....    /* If
3b02f 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 74   this index is t
3b030 68 65 20 62 65 73 74 20 77 65 20 68 61 76 65 20  he best we have 
3b031 73 65 65 6e 20 73 6f 20 66 61 72 2c 20 74 68 65  seen so far, the
3b032 6e 20 72 65 63 6f 72 64 20 74 68 69 73 0d 0a 20  n record this.. 
3b033 20 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20     ** index and 
3b034 69 74 73 20 63 6f 73 74 20 69 6e 20 74 68 65 20  its cost in the 
3b035 70 43 6f 73 74 20 73 74 72 75 63 74 75 72 65 2e  pCost structure.
3b036 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
3b037 28 20 28 21 70 49 64 78 20 7c 7c 20 77 73 46 6c  ( (!pIdx || wsFl
3b038 61 67 73 29 0d 0a 20 20 20 20 20 26 26 20 28 63  ags)..     && (c
3b039 6f 73 74 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74  ost<pCost->rCost
3b03a 20 7c 7c 20 28 63 6f 73 74 3c 3d 70 43 6f 73 74   || (cost<=pCost
3b03b 2d 3e 72 43 6f 73 74 20 26 26 20 6e 52 6f 77 3c  ->rCost && nRow<
3b03c 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 52 6f 77  pCost->plan.nRow
3b03d 29 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20  ))..    ){..    
3b03e 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
3b03f 20 63 6f 73 74 3b 0d 0a 20 20 20 20 20 20 70 43   cost;..      pC
3b040 6f 73 74 2d 3e 75 73 65 64 20 3d 20 75 73 65 64  ost->used = used
3b041 3b 0d 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  ;..      pCost->
3b042 70 6c 61 6e 2e 6e 52 6f 77 20 3d 20 6e 52 6f 77  plan.nRow = nRow
3b043 3b 0d 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  ;..      pCost->
3b044 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 28  plan.wsFlags = (
3b045 77 73 46 6c 61 67 73 26 77 73 46 6c 61 67 4d 61  wsFlags&wsFlagMa
3b046 73 6b 29 3b 0d 0a 20 20 20 20 20 20 70 43 6f 73  sk);..      pCos
3b047 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 6e 45  t->plan.nEq = nE
3b048 71 3b 0d 0a 20 20 20 20 20 20 70 43 6f 73 74 2d  q;..      pCost-
3b049 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70  >plan.u.pIdx = p
3b04a 49 64 78 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  Idx;..    }.... 
3b04b 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77     /* If there w
3b04c 61 73 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59  as an INDEXED BY
3b04d 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 6f 6e   clause, then on
3b04e 6c 79 20 74 68 61 74 20 6f 6e 65 20 69 6e 64 65  ly that one inde
3b04f 78 20 69 73 0d 0a 20 20 20 20 2a 2a 20 63 6f 6e  x is..    ** con
3b050 73 69 64 65 72 65 64 2e 20 2a 2f 0d 0a 20 20 20  sidered. */..   
3b051 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65   if( pSrc->pInde
3b052 78 20 29 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20  x ) break;....  
3b053 20 20 2f 2a 20 52 65 73 65 74 20 6d 61 73 6b 73    /* Reset masks
3b054 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 69 6e   for the next in
3b055 64 65 78 20 69 6e 20 74 68 65 20 6c 6f 6f 70 20  dex in the loop 
3b056 2a 2f 0d 0a 20 20 20 20 77 73 46 6c 61 67 4d 61  */..    wsFlagMa
3b057 73 6b 20 3d 20 7e 28 57 48 45 52 45 5f 52 4f 57  sk = ~(WHERE_ROW
3b058 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49  ID_EQ|WHERE_ROWI
3b059 44 5f 52 41 4e 47 45 29 3b 0d 0a 20 20 20 20 65  D_RANGE);..    e
3b05a 71 54 65 72 6d 4d 61 73 6b 20 3d 20 69 64 78 45  qTermMask = idxE
3b05b 71 54 65 72 6d 4d 61 73 6b 3b 0d 0a 20 20 7d 0d  qTermMask;..  }.
3b05c 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ...  /* If there
3b05d 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20   is no ORDER BY 
3b05e 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 53  clause and the S
3b05f 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64  QLITE_ReverseOrd
3b060 65 72 20 66 6c 61 67 0d 0a 20 20 2a 2a 20 69 73  er flag..  ** is
3b061 20 73 65 74 2c 20 74 68 65 6e 20 72 65 76 65 72   set, then rever
3b062 73 65 20 74 68 65 20 6f 72 64 65 72 20 74 68 61  se the order tha
3b063 74 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c  t the index will
3b064 20 62 65 20 73 63 61 6e 6e 65 64 0d 0a 20 20 2a   be scanned..  *
3b065 2a 20 69 6e 2e 20 54 68 69 73 20 69 73 20 75 73  * in. This is us
3b066 65 64 20 66 6f 72 20 61 70 70 6c 69 63 61 74 69  ed for applicati
3b067 6f 6e 20 74 65 73 74 69 6e 67 2c 20 74 6f 20 68  on testing, to h
3b068 65 6c 70 20 66 69 6e 64 20 63 61 73 65 73 0d 0a  elp find cases..
3b069 20 20 2a 2a 20 77 68 65 72 65 20 61 70 70 6c 69    ** where appli
3b06a 63 61 74 69 6f 6e 20 62 65 68 61 76 69 6f 75 72  cation behaviour
3b06b 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20   depends on the 
3b06c 28 75 6e 64 65 66 69 6e 65 64 29 20 6f 72 64 65  (undefined) orde
3b06d 72 20 74 68 61 74 0d 0a 20 20 2a 2a 20 53 51 4c  r that..  ** SQL
3b06e 69 74 65 20 6f 75 74 70 75 74 73 20 72 6f 77 73  ite outputs rows
3b06f 20 69 6e 20 69 6e 20 74 68 65 20 61 62 73 65 6e   in in the absen
3b070 63 65 20 6f 66 20 61 6e 20 4f 52 44 45 52 20 42  ce of an ORDER B
3b071 59 20 63 6c 61 75 73 65 2e 20 20 2a 2f 0d 0a 20  Y clause.  */.. 
3b072 20 69 66 28 20 21 70 4f 72 64 65 72 42 79 20 26   if( !pOrderBy &
3b073 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c  & pParse->db->fl
3b074 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 76  ags & SQLITE_Rev
3b075 65 72 73 65 4f 72 64 65 72 20 29 7b 0d 0a 20 20  erseOrder ){..  
3b076 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
3b077 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52  Flags |= WHERE_R
3b078 45 56 45 52 53 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a  EVERSE;..  }....
3b079 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
3b07a 42 79 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c  By || (pCost->pl
3b07b 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45  an.wsFlags&WHERE
3b07c 5f 4f 52 44 45 52 42 59 29 3d 3d 30 20 29 3b 0d  _ORDERBY)==0 );.
3b07d 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 73 74  .  assert( pCost
3b07e 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30  ->plan.u.pIdx==0
3b07f 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e   || (pCost->plan
3b080 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 52  .wsFlags&WHERE_R
3b081 4f 57 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0d 0a  OWID_EQ)==0 );..
3b082 20 20 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e    assert( pSrc->
3b083 70 49 6e 64 65 78 3d 3d 30 20 0d 0a 20 20 20 20  pIndex==0 ..    
3b084 20 20 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61     || pCost->pla
3b085 6e 2e 75 2e 70 49 64 78 3d 3d 30 20 0d 0a 20 20  n.u.pIdx==0 ..  
3b086 20 20 20 20 20 7c 7c 20 70 43 6f 73 74 2d 3e 70       || pCost->p
3b087 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70 53 72 63  lan.u.pIdx==pSrc
3b088 2d 3e 70 49 6e 64 65 78 20 0d 0a 20 20 29 3b 0d  ->pIndex ..  );.
3b089 0a 0d 0a 20 20 57 48 45 52 45 54 52 41 43 45 28  ...  WHERETRACE(
3b08a 28 22 62 65 73 74 20 69 6e 64 65 78 20 69 73 3a  ("best index is:
3b08b 20 25 73 5c 6e 22 2c 20 0d 0a 20 20 20 20 28 28   %s\n", ..    ((
3b08c 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
3b08d 61 67 73 20 26 20 57 48 45 52 45 5f 4e 4f 54 5f  ags & WHERE_NOT_
3b08e 46 55 4c 4c 53 43 41 4e 29 3d 3d 30 20 3f 20 22  FULLSCAN)==0 ? "
3b08f 6e 6f 6e 65 22 20 3a 20 0d 0a 20 20 20 20 20 20  none" : ..      
3b090 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75     pCost->plan.u
3b091 2e 70 49 64 78 20 3f 20 70 43 6f 73 74 2d 3e 70  .pIdx ? pCost->p
3b092 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d  lan.u.pIdx->zNam
3b093 65 20 3a 20 22 69 70 6b 22 29 0d 0a 20 20 29 29  e : "ipk")..  ))
3b094 3b 0d 0a 20 20 0d 0a 20 20 62 65 73 74 4f 72 43  ;..  ..  bestOrC
3b095 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73  lauseIndex(pPars
3b096 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f  e, pWC, pSrc, no
3b097 74 52 65 61 64 79 2c 20 6e 6f 74 56 61 6c 69 64  tReady, notValid
3b098 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73  , pOrderBy, pCos
3b099 74 29 3b 0d 0a 20 20 62 65 73 74 41 75 74 6f 6d  t);..  bestAutom
3b09a 61 74 69 63 49 6e 64 65 78 28 70 50 61 72 73 65  aticIndex(pParse
3b09b 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
3b09c 52 65 61 64 79 2c 20 70 43 6f 73 74 29 3b 0d 0a  Ready, pCost);..
3b09d 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
3b09e 46 6c 61 67 73 20 7c 3d 20 65 71 54 65 72 6d 4d  Flags |= eqTermM
3b09f 61 73 6b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ask;..}..../*..*
3b0a0 2a 20 46 69 6e 64 20 74 68 65 20 71 75 65 72 79  * Find the query
3b0a1 20 70 6c 61 6e 20 66 6f 72 20 61 63 63 65 73 73   plan for access
3b0a2 69 6e 67 20 74 61 62 6c 65 20 70 53 72 63 2d 3e  ing table pSrc->
3b0a3 70 54 61 62 2e 20 57 72 69 74 65 20 74 68 65 0d  pTab. Write the.
3b0a4 0a 2a 2a 20 62 65 73 74 20 71 75 65 72 79 20 70  .** best query p
3b0a5 6c 61 6e 20 61 6e 64 20 69 74 73 20 63 6f 73 74  lan and its cost
3b0a6 20 69 6e 74 6f 20 74 68 65 20 57 68 65 72 65 43   into the WhereC
3b0a7 6f 73 74 20 6f 62 6a 65 63 74 20 73 75 70 70 6c  ost object suppl
3b0a8 69 65 64 20 0d 0a 2a 2a 20 61 73 20 74 68 65 20  ied ..** as the 
3b0a9 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20  last parameter. 
3b0aa 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
3b0ab 79 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 20  y calculate the 
3b0ac 63 6f 73 74 20 6f 66 0d 0a 2a 2a 20 62 6f 74 68  cost of..** both
3b0ad 20 72 65 61 6c 20 61 6e 64 20 76 69 72 74 75 61   real and virtua
3b0ae 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 2e 0d 0a  l table scans...
3b0af 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
3b0b0 62 65 73 74 49 6e 64 65 78 28 0d 0a 20 20 50 61  bestIndex(..  Pa
3b0b1 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
3b0b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3b0b3 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
3b0b4 20 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61 75   */..  WhereClau
3b0b5 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
3b0b6 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
3b0b7 63 6c 61 75 73 65 20 2a 2f 0d 0a 20 20 73 74 72  clause */..  str
3b0b8 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
3b0b9 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20   *pSrc,  /* The 
3b0ba 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
3b0bb 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0d 0a 20   to search */.. 
3b0bc 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
3b0bd 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
3b0be 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
3b0bf 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  not available fo
3b0c0 72 20 69 6e 64 65 78 69 6e 67 20 2a 2f 0d 0a 20  r indexing */.. 
3b0c1 20 42 69 74 6d 61 73 6b 20 6e 6f 74 56 61 6c 69   Bitmask notVali
3b0c2 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
3b0c3 43 75 72 73 6f 72 73 20 6e 6f 74 20 61 76 61 69  Cursors not avai
3b0c4 6c 61 62 6c 65 20 66 6f 72 20 61 6e 79 20 70 75  lable for any pu
3b0c5 72 70 6f 73 65 20 2a 2f 0d 0a 20 20 45 78 70 72  rpose */..  Expr
3b0c6 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
3b0c7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
3b0c8 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
3b0c9 2f 0d 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a  /..  WhereCost *
3b0ca 70 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20  pCost           
3b0cb 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20   /* Lowest cost 
3b0cc 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0d 0a 29  query plan */..)
3b0cd 7b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  {..#ifndef SQLIT
3b0ce 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
3b0cf 42 4c 45 0d 0a 20 20 69 66 28 20 49 73 56 69 72  BLE..  if( IsVir
3b0d0 74 75 61 6c 28 70 53 72 63 2d 3e 70 54 61 62 29  tual(pSrc->pTab)
3b0d1 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
3b0d2 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 20 3d  _index_info *p =
3b0d3 20 30 3b 0d 0a 20 20 20 20 62 65 73 74 56 69 72   0;..    bestVir
3b0d4 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65  tualIndex(pParse
3b0d5 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
3b0d6 52 65 61 64 79 2c 20 6e 6f 74 56 61 6c 69 64 2c  Ready, notValid,
3b0d7 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74   pOrderBy, pCost
3b0d8 2c 26 70 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  ,&p);..    if( p
3b0d9 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
3b0da 74 72 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  tr ){..      sql
3b0db 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 69 64 78  ite3_free(p->idx
3b0dc 53 74 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Str);..    }..  
3b0dd 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
3b0de 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 29 3b 0d  pParse->db, p);.
3b0df 0a 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66  .  }else..#endif
3b0e0 0d 0a 20 20 7b 0d 0a 20 20 20 20 62 65 73 74 42  ..  {..    bestB
3b0e1 74 72 65 65 49 6e 64 65 78 28 70 50 61 72 73 65  treeIndex(pParse
3b0e2 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
3b0e3 52 65 61 64 79 2c 20 6e 6f 74 56 61 6c 69 64 2c  Ready, notValid,
3b0e4 20 70 4f 72 64 65 72 42 79 2c 20 30 2c 20 70 43   pOrderBy, 0, pC
3b0e5 6f 73 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ost);..  }..}...
3b0e6 0a 2f 2a 0d 0a 2a 2a 20 44 69 73 61 62 6c 65 20  ./*..** Disable 
3b0e7 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  a term in the WH
3b0e8 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78 63  ERE clause.  Exc
3b0e9 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73 61  ept, do not disa
3b0ea 62 6c 65 20 74 68 65 20 74 65 72 6d 0d 0a 2a 2a  ble the term..**
3b0eb 20 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20   if it controls 
3b0ec 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
3b0ed 4e 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74  N and it did not
3b0ee 20 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68   originate in th
3b0ef 65 20 4f 4e 0d 0a 2a 2a 20 6f 72 20 55 53 49 4e  e ON..** or USIN
3b0f0 47 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74  G clause of that
3b0f1 20 6a 6f 69 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 43   join...**..** C
3b0f2 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 72 6d  onsider the term
3b0f3 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68   t2.z='ok' in th
3b0f4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72  e following quer
3b0f5 69 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28  ies:..**..**   (
3b0f6 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  1)  SELECT * FRO
3b0f7 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
3b0f8 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57  2 ON t1.a=t2.x W
3b0f9 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0d 0a  HERE t2.z='ok'..
3b0fa 2a 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54  **   (2)  SELECT
3b0fb 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20   * FROM t1 LEFT 
3b0fc 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d  JOIN t2 ON t1.a=
3b0fd 74 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f  t2.x AND t2.z='o
3b0fe 6b 27 0d 0a 2a 2a 20 20 20 28 33 29 20 20 53 45  k'..**   (3)  SE
3b0ff 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20  LECT * FROM t1, 
3b100 74 32 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32  t2 WHERE t1.a=t2
3b101 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27  .x AND t2.z='ok'
3b102 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 32 2e  ..**..** The t2.
3b103 7a 3d 27 6f 6b 27 20 69 73 20 64 69 73 61 62 6c  z='ok' is disabl
3b104 65 64 20 69 6e 20 74 68 65 20 69 6e 20 28 32 29  ed in the in (2)
3b105 20 62 65 63 61 75 73 65 20 69 74 20 6f 72 69 67   because it orig
3b106 69 6e 61 74 65 73 0d 0a 2a 2a 20 69 6e 20 74 68  inates..** in th
3b107 65 20 4f 4e 20 63 6c 61 75 73 65 2e 20 20 54 68  e ON clause.  Th
3b108 65 20 74 65 72 6d 20 69 73 20 64 69 73 61 62 6c  e term is disabl
3b109 65 64 20 69 6e 20 28 33 29 20 62 65 63 61 75 73  ed in (3) becaus
3b10a 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74  e it is not part
3b10b 0d 0a 2a 2a 20 6f 66 20 61 20 4c 45 46 54 20 4f  ..** of a LEFT O
3b10c 55 54 45 52 20 4a 4f 49 4e 2e 20 20 49 6e 20 28  UTER JOIN.  In (
3b10d 31 29 2c 20 74 68 65 20 74 65 72 6d 20 69 73 20  1), the term is 
3b10e 6e 6f 74 20 64 69 73 61 62 6c 65 64 2e 0d 0a 2a  not disabled...*
3b10f 2a 0d 0a 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54 41  *..** IMPLEMENTA
3b110 54 49 4f 4e 2d 4f 46 3a 20 52 2d 32 34 35 39 37  TION-OF: R-24597
3b111 2d 35 38 36 35 35 20 4e 6f 20 74 65 73 74 73 20  -58655 No tests 
3b112 61 72 65 20 64 6f 6e 65 20 66 6f 72 20 74 65 72  are done for ter
3b113 6d 73 20 74 68 61 74 20 61 72 65 0d 0a 2a 2a 20  ms that are..** 
3b114 63 6f 6d 70 6c 65 74 65 6c 79 20 73 61 74 69 73  completely satis
3b115 66 69 65 64 20 62 79 20 69 6e 64 69 63 65 73 2e  fied by indices.
3b116 0d 0a 2a 2a 0d 0a 2a 2a 20 44 69 73 61 62 6c 69  ..**..** Disabli
3b117 6e 67 20 61 20 74 65 72 6d 20 63 61 75 73 65 73  ng a term causes
3b118 20 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f   that term to no
3b119 74 20 62 65 20 74 65 73 74 65 64 20 69 6e 20 74  t be tested in t
3b11a 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0d 0a 2a  he inner loop..*
3b11b 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  * of the join.  
3b11c 44 69 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20  Disabling is an 
3b11d 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57  optimization.  W
3b11e 68 65 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61  hen terms are sa
3b11f 74 69 73 66 69 65 64 0d 0a 2a 2a 20 62 79 20 69  tisfied..** by i
3b120 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73 61 62  ndices, we disab
3b121 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65 76 65  le them to preve
3b122 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74 65 73  nt redundant tes
3b123 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0d  ts in the inner.
3b124 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f  .** loop.  We wo
3b125 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72 72  uld get the corr
3b126 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 20 6e  ect results if n
3b127 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 65 72  othing were ever
3b128 20 64 69 73 61 62 6c 65 64 2c 0d 0a 2a 2a 20 62   disabled,..** b
3b129 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72  ut joins might r
3b12a 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  un a little slow
3b12b 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69  er.  The trick i
3b12c 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20  s to disable as 
3b12d 6d 75 63 68 0d 0a 2a 2a 20 61 73 20 77 65 20 63  much..** as we c
3b12e 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62  an without disab
3b12f 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20  ling too much.  
3b130 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69  If we disabled i
3b131 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0d  n (1), we'd get.
3b132 0a 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e  .** the wrong an
3b133 73 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65  swer.  See ticke
3b134 74 20 23 38 31 33 2e 0d 0a 2a 2f 0d 0a 73 74 61  t #813...*/..sta
3b135 74 69 63 20 76 6f 69 64 20 64 69 73 61 62 6c 65  tic void disable
3b136 54 65 72 6d 28 57 68 65 72 65 4c 65 76 65 6c 20  Term(WhereLevel 
3b137 2a 70 4c 65 76 65 6c 2c 20 57 68 65 72 65 54 65  *pLevel, WhereTe
3b138 72 6d 20 2a 70 54 65 72 6d 29 7b 0d 0a 20 20 69  rm *pTerm){..  i
3b139 66 28 20 70 54 65 72 6d 0d 0a 20 20 20 20 20 20  f( pTerm..      
3b13a 26 26 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61  && (pTerm->wtFla
3b13b 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29  gs & TERM_CODED)
3b13c 3d 3d 30 0d 0a 20 20 20 20 20 20 26 26 20 28 70  ==0..      && (p
3b13d 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
3b13e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73 50 72  ==0 || ExprHasPr
3b13f 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45  operty(pTerm->pE
3b140 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  xpr, EP_FromJoin
3b141 29 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 70 54  ))..  ){..    pT
3b142 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
3b143 54 45 52 4d 5f 43 4f 44 45 44 3b 0d 0a 20 20 20  TERM_CODED;..   
3b144 20 69 66 28 20 70 54 65 72 6d 2d 3e 69 50 61 72   if( pTerm->iPar
3b145 65 6e 74 3e 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ent>=0 ){..     
3b146 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68   WhereTerm *pOth
3b147 65 72 20 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43  er = &pTerm->pWC
3b148 2d 3e 61 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65  ->a[pTerm->iPare
3b149 6e 74 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  nt];..      if( 
3b14a 28 2d 2d 70 4f 74 68 65 72 2d 3e 6e 43 68 69 6c  (--pOther->nChil
3b14b 64 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  d)==0 ){..      
3b14c 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
3b14d 65 76 65 6c 2c 20 70 4f 74 68 65 72 29 3b 0d 0a  evel, pOther);..
3b14e 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
3b14f 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
3b150 20 43 6f 64 65 20 61 6e 20 4f 50 5f 41 66 66 69   Code an OP_Affi
3b151 6e 69 74 79 20 6f 70 63 6f 64 65 20 74 6f 20 61  nity opcode to a
3b152 70 70 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  pply the column 
3b153 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
3b154 7a 41 66 66 0d 0a 2a 2a 20 74 6f 20 74 68 65 20  zAff..** to the 
3b155 6e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  n registers star
3b156 74 69 6e 67 20 61 74 20 62 61 73 65 2e 20 0d 0a  ting at base. ..
3b157 2a 2a 0d 0a 2a 2a 20 41 73 20 61 6e 20 6f 70 74  **..** As an opt
3b158 69 6d 69 7a 61 74 69 6f 6e 2c 20 53 51 4c 49 54  imization, SQLIT
3b159 45 5f 41 46 46 5f 4e 4f 4e 45 20 65 6e 74 72 69  E_AFF_NONE entri
3b15a 65 73 20 28 77 68 69 63 68 20 61 72 65 20 6e 6f  es (which are no
3b15b 2d 6f 70 73 29 20 61 74 20 74 68 65 0d 0a 2a 2a  -ops) at the..**
3b15c 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65   beginning and e
3b15d 6e 64 20 6f 66 20 7a 41 66 66 20 61 72 65 20 69  nd of zAff are i
3b15e 67 6e 6f 72 65 64 2e 20 20 49 66 20 61 6c 6c 20  gnored.  If all 
3b15f 65 6e 74 72 69 65 73 20 69 6e 20 7a 41 66 66 20  entries in zAff 
3b160 61 72 65 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  are..** SQLITE_A
3b161 46 46 5f 4e 4f 4e 45 2c 20 74 68 65 6e 20 6e 6f  FF_NONE, then no
3b162 20 63 6f 64 65 20 67 65 74 73 20 67 65 6e 65 72   code gets gener
3b163 61 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ated...**..** Th
3b164 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73  is routine makes
3b165 20 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66   its own copy of
3b166 20 7a 41 66 66 20 73 6f 20 74 68 61 74 20 74 68   zAff so that th
3b167 65 20 63 61 6c 6c 65 72 20 69 73 20 66 72 65 65  e caller is free
3b168 0d 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 7a  ..** to modify z
3b169 41 66 66 20 61 66 74 65 72 20 74 68 69 73 20 72  Aff after this r
3b16a 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0d  outine returns..
3b16b 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
3b16c 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e 69   codeApplyAffini
3b16d 74 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ty(Parse *pParse
3b16e 2c 20 69 6e 74 20 62 61 73 65 2c 20 69 6e 74 20  , int base, int 
3b16f 6e 2c 20 63 68 61 72 20 2a 7a 41 66 66 29 7b 0d  n, char *zAff){.
3b170 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
3b171 72 73 65 2d 3e 70 56 64 62 65 3b 0d 0a 20 20 69  rse->pVdbe;..  i
3b172 66 28 20 7a 41 66 66 3d 3d 30 20 29 7b 0d 0a 20  f( zAff==0 ){.. 
3b173 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
3b174 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
3b175 6c 65 64 20 29 3b 0d 0a 20 20 20 20 72 65 74 75  led );..    retu
3b176 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65  rn;..  }..  asse
3b177 72 74 28 20 76 21 3d 30 20 29 3b 0d 0a 0d 0a 20  rt( v!=0 );.... 
3b178 20 2f 2a 20 41 64 6a 75 73 74 20 62 61 73 65 20   /* Adjust base 
3b179 61 6e 64 20 6e 20 74 6f 20 73 6b 69 70 20 6f 76  and n to skip ov
3b17a 65 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  er SQLITE_AFF_NO
3b17b 4e 45 20 65 6e 74 72 69 65 73 20 61 74 20 74 68  NE entries at th
3b17c 65 20 62 65 67 69 6e 6e 69 6e 67 0d 0a 20 20 2a  e beginning..  *
3b17d 2a 20 61 6e 64 20 65 6e 64 20 6f 66 20 74 68 65  * and end of the
3b17e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
3b17f 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 77 68 69 6c 65  ...  */..  while
3b180 28 20 6e 3e 30 20 26 26 20 7a 41 66 66 5b 30 5d  ( n>0 && zAff[0]
3b181 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
3b182 45 20 29 7b 0d 0a 20 20 20 20 6e 2d 2d 3b 0d 0a  E ){..    n--;..
3b183 20 20 20 20 62 61 73 65 2b 2b 3b 0d 0a 20 20 20      base++;..   
3b184 20 7a 41 66 66 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20   zAff++;..  }.. 
3b185 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a   while( n>1 && z
3b186 41 66 66 5b 6e 2d 31 5d 3d 3d 53 51 4c 49 54 45  Aff[n-1]==SQLITE
3b187 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0d 0a 20 20  _AFF_NONE ){..  
3b188 20 20 6e 2d 2d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20    n--;..  }.... 
3b189 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4f 50 5f   /* Code the OP_
3b18a 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65 20  Affinity opcode 
3b18b 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 74  if there is anyt
3b18c 68 69 6e 67 20 6c 65 66 74 20 74 6f 20 64 6f 2e  hing left to do.
3b18d 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 3e 30 20 29   */..  if( n>0 )
3b18e 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  {..    sqlite3Vd
3b18f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
3b190 66 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e  ffinity, base, n
3b191 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
3b192 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
3b193 31 2c 20 7a 41 66 66 2c 20 6e 29 3b 0d 0a 20 20  1, zAff, n);..  
3b194 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3b195 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
3b196 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c 20 6e  (pParse, base, n
3b197 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 0d 0a  );..  }..}......
3b198 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  /*..** Generate 
3b199 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c  code for a singl
3b19a 65 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 20  e equality term 
3b19b 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
3b19c 75 73 65 2e 20 20 41 6e 20 65 71 75 61 6c 69 74  use.  An equalit
3b19d 79 0d 0a 2a 2a 20 74 65 72 6d 20 63 61 6e 20 62  y..** term can b
3b19e 65 20 65 69 74 68 65 72 20 58 3d 65 78 70 72 20  e either X=expr 
3b19f 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29 2e 20 20  or X IN (...).  
3b1a0 20 70 54 65 72 6d 20 69 73 20 74 68 65 20 74 65   pTerm is the te
3b1a1 72 6d 20 74 6f 20 62 65 20 0d 0a 2a 2a 20 63 6f  rm to be ..** co
3b1a2 64 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ded...**..** The
3b1a3 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66   current value f
3b1a4 6f 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  or the constrain
3b1a5 74 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67  t is left in reg
3b1a6 69 73 74 65 72 20 69 52 65 67 2e 0d 0a 2a 2a 0d  ister iReg...**.
3b1a7 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e 73 74 72  .** For a constr
3b1a8 61 69 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  aint of the form
3b1a9 20 58 3d 65 78 70 72 2c 20 74 68 65 20 65 78 70   X=expr, the exp
3b1aa 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75  ression is evalu
3b1ab 61 74 65 64 20 61 6e 64 20 69 74 73 0d 0a 2a 2a  ated and its..**
3b1ac 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66 74 20   result is left 
3b1ad 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 46  on the stack.  F
3b1ae 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  or constraints o
3b1af 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49 4e 20  f the form X IN 
3b1b0 28 2e 2e 2e 29 0d 0a 2a 2a 20 74 68 69 73 20 72  (...)..** this r
3b1b1 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70 20 61  outine sets up a
3b1b2 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20   loop that will 
3b1b3 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c 6c  iterate over all
3b1b4 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0d 0a 2a   values of X...*
3b1b5 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  /..static int co
3b1b6 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0d  deEqualityTerm(.
3b1b7 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3b1b8 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
3b1b9 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
3b1ba 0d 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ..  WhereTerm *p
3b1bb 54 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74  Term,   /* The t
3b1bc 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
3b1bd 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f   clause to be co
3b1be 64 65 64 20 2a 2f 0d 0a 20 20 57 68 65 72 65 4c  ded */..  WhereL
3b1bf 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a  evel *pLevel, /*
3b1c0 20 57 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74   When level of t
3b1c1 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77  he FROM clause w
3b1c2 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e  e are working on
3b1c3 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 61 72 67   */..  int iTarg
3b1c4 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74  et         /* At
3b1c5 74 65 6d 70 74 20 74 6f 20 6c 65 61 76 65 20 72  tempt to leave r
3b1c6 65 73 75 6c 74 73 20 69 6e 20 74 68 69 73 20 72  esults in this r
3b1c7 65 67 69 73 74 65 72 20 2a 2f 0d 0a 29 7b 0d 0a  egister */..){..
3b1c8 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65    Expr *pX = pTe
3b1c9 72 6d 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 56 64  rm->pExpr;..  Vd
3b1ca 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
3b1cb 70 56 64 62 65 3b 0d 0a 20 20 69 6e 74 20 69 52  pVdbe;..  int iR
3b1cc 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eg;             
3b1cd 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
3b1ce 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 73   holding results
3b1cf 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28   */....  assert(
3b1d0 20 69 54 61 72 67 65 74 3e 30 20 29 3b 0d 0a 20   iTarget>0 );.. 
3b1d1 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f   if( pX->op==TK_
3b1d2 45 51 20 29 7b 0d 0a 20 20 20 20 69 52 65 67 20  EQ ){..    iReg 
3b1d3 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
3b1d4 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
3b1d5 70 58 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 72  pX->pRight, iTar
3b1d6 67 65 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69  get);..  }else i
3b1d7 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  f( pX->op==TK_IS
3b1d8 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 69 52 65  NULL ){..    iRe
3b1d9 67 20 3d 20 69 54 61 72 67 65 74 3b 0d 0a 20 20  g = iTarget;..  
3b1da 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3b1db 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
3b1dc 30 2c 20 69 52 65 67 29 3b 0d 0a 23 69 66 6e 64  0, iReg);..#ifnd
3b1dd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
3b1de 55 42 51 55 45 52 59 0d 0a 20 20 7d 65 6c 73 65  UBQUERY..  }else
3b1df 7b 0d 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65  {..    int eType
3b1e0 3b 0d 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b  ;..    int iTab;
3b1e1 0d 0a 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c  ..    struct InL
3b1e2 6f 6f 70 20 2a 70 49 6e 3b 0d 0a 0d 0a 20 20 20  oop *pIn;....   
3b1e3 20 61 73 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d   assert( pX->op=
3b1e4 3d 54 4b 5f 49 4e 20 29 3b 0d 0a 20 20 20 20 69  =TK_IN );..    i
3b1e5 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0d 0a  Reg = iTarget;..
3b1e6 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69      eType = sqli
3b1e7 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70  te3FindInIndex(p
3b1e8 50 61 72 73 65 2c 20 70 58 2c 20 30 29 3b 0d 0a  Parse, pX, 0);..
3b1e9 20 20 20 20 69 54 61 62 20 3d 20 70 58 2d 3e 69      iTab = pX->i
3b1ea 54 61 62 6c 65 3b 0d 0a 20 20 20 20 73 71 6c 69  Table;..    sqli
3b1eb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3b1ec 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62   OP_Rewind, iTab
3b1ed 2c 20 30 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  , 0);..    asser
3b1ee 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  t( pLevel->plan.
3b1ef 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
3b1f0 49 4e 5f 41 42 4c 45 20 29 3b 0d 0a 20 20 20 20  IN_ABLE );..    
3b1f1 69 66 28 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  if( pLevel->u.in
3b1f2 2e 6e 49 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  .nIn==0 ){..    
3b1f3 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78    pLevel->addrNx
3b1f4 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
3b1f5 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20  akeLabel(v);..  
3b1f6 20 20 7d 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d    }..    pLevel-
3b1f7 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0d 0a 20 20  >u.in.nIn++;..  
3b1f8 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61    pLevel->u.in.a
3b1f9 49 6e 4c 6f 6f 70 20 3d 0d 0a 20 20 20 20 20 20  InLoop =..      
3b1fa 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
3b1fb 63 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e  cOrFree(pParse->
3b1fc 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  db, pLevel->u.in
3b1fd 2e 61 49 6e 4c 6f 6f 70 2c 0d 0a 20 20 20 20 20  .aInLoop,..     
3b1fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b1ff 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
3b200 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
3b201 4c 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d  Loop[0])*pLevel-
3b202 3e 75 2e 69 6e 2e 6e 49 6e 29 3b 0d 0a 20 20 20  >u.in.nIn);..   
3b203 20 70 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75   pIn = pLevel->u
3b204 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 3b 0d 0a 20 20  .in.aInLoop;..  
3b205 20 20 69 66 28 20 70 49 6e 20 29 7b 0d 0a 20 20    if( pIn ){..  
3b206 20 20 20 20 70 49 6e 20 2b 3d 20 70 4c 65 76 65      pIn += pLeve
3b207 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 2d 20 31 3b  l->u.in.nIn - 1;
3b208 0d 0a 20 20 20 20 20 20 70 49 6e 2d 3e 69 43 75  ..      pIn->iCu
3b209 72 20 3d 20 69 54 61 62 3b 0d 0a 20 20 20 20 20  r = iTab;..     
3b20a 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49   if( eType==IN_I
3b20b 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0d 0a 20  NDEX_ROWID ){.. 
3b20c 20 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64 72         pIn->addr
3b20d 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  InTop = sqlite3V
3b20e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
3b20f 52 6f 77 69 64 2c 20 69 54 61 62 2c 20 69 52 65  Rowid, iTab, iRe
3b210 67 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  g);..      }else
3b211 7b 0d 0a 20 20 20 20 20 20 20 20 70 49 6e 2d 3e  {..        pIn->
3b212 61 64 64 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69  addrInTop = sqli
3b213 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
3b214 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62   OP_Column, iTab
3b215 2c 20 30 2c 20 69 52 65 67 29 3b 0d 0a 20 20 20  , 0, iReg);..   
3b216 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
3b217 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
3b218 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67   OP_IsNull, iReg
3b219 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
3b21a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e        pLevel->u.
3b21b 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0d 0a 20 20 20  in.nIn = 0;..   
3b21c 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d   }..#endif..  }.
3b21d 0a 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70  .  disableTerm(p
3b21e 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0d 0a  Level, pTerm);..
3b21f 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0d 0a    return iReg;..
3b220 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65  }..../*..** Gene
3b221 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
3b222 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 61 6c 6c  ill evaluate all
3b223 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74   == and IN const
3b224 72 61 69 6e 74 73 20 66 6f 72 20 61 6e 0d 0a 2a  raints for an..*
3b225 2a 20 69 6e 64 65 78 2e 0d 0a 2a 2a 0d 0a 2a 2a  * index...**..**
3b226 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f   For example, co
3b227 6e 73 69 64 65 72 20 74 61 62 6c 65 20 74 31 28  nsider table t1(
3b228 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74  a,b,c,d,e,f) wit
3b229 68 20 69 6e 64 65 78 20 69 31 28 61 2c 62 2c 63  h index i1(a,b,c
3b22a 29 2e 0d 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74  )...** Suppose t
3b22b 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
3b22c 69 73 20 74 68 69 73 3a 20 20 61 3d 3d 35 20 41  is this:  a==5 A
3b22d 4e 44 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 20  ND b IN (1,2,3) 
3b22e 41 4e 44 20 63 3e 35 20 41 4e 44 20 63 3c 31 30  AND c>5 AND c<10
3b22f 0d 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 68  ..** The index h
3b230 61 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74 68  as as many as th
3b231 72 65 65 20 65 71 75 61 6c 69 74 79 20 63 6f 6e  ree equality con
3b232 73 74 72 61 69 6e 74 73 2c 20 62 75 74 20 69 6e  straints, but in
3b233 20 74 68 69 73 0d 0a 2a 2a 20 65 78 61 6d 70 6c   this..** exampl
3b234 65 2c 20 74 68 65 20 74 68 69 72 64 20 22 63 22  e, the third "c"
3b235 20 76 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 65   value is an ine
3b236 71 75 61 6c 69 74 79 2e 20 20 53 6f 20 6f 6e 6c  quality.  So onl
3b237 79 20 74 77 6f 20 0d 0a 2a 2a 20 63 6f 6e 73 74  y two ..** const
3b238 72 61 69 6e 74 73 20 61 72 65 20 63 6f 64 65 64  raints are coded
3b239 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
3b23a 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 63 6f  will generate co
3b23b 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 0d 0a  de to evaluate..
3b23c 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 62 20 49 4e  ** a==5 and b IN
3b23d 20 28 31 2c 32 2c 33 29 2e 20 20 54 68 65 20 63   (1,2,3).  The c
3b23e 75 72 72 65 6e 74 20 76 61 6c 75 65 73 20 66 6f  urrent values fo
3b23f 72 20 61 20 61 6e 64 20 62 20 77 69 6c 6c 20 62  r a and b will b
3b240 65 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 69 6e 20  e stored..** in 
3b241 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
3b242 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 69 6e  sters and the in
3b243 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
3b244 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74   register is ret
3b245 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  urned...**..** I
3b246 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62  n the example ab
3b247 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74  ove nEq==2.  But
3b248 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
3b249 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76   works for any v
3b24a 61 6c 75 65 0d 0a 2a 2a 20 6f 66 20 6e 45 71 20  alue..** of nEq 
3b24b 69 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20 49 66  including 0.  If
3b24c 20 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f   nEq==0, this ro
3b24d 75 74 69 6e 65 20 69 73 20 6e 65 61 72 6c 79 20  utine is nearly 
3b24e 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 20 54 68 65  a no-op...** The
3b24f 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69 74 20 64   only thing it d
3b250 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 20  oes is allocate 
3b251 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  the pLevel->iMem
3b252 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64   memory cell and
3b253 0d 0a 2a 2a 20 63 6f 6d 70 75 74 65 20 74 68 65  ..** compute the
3b254 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
3b255 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  ...**..** This r
3b256 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 61 6c  outine always al
3b257 6c 6f 63 61 74 65 73 20 61 74 20 6c 65 61 73 74  locates at least
3b258 20 6f 6e 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   one memory cell
3b259 20 61 6e 64 20 72 65 74 75 72 6e 73 0d 0a 2a 2a   and returns..**
3b25a 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
3b25b 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20  at memory cell. 
3b25c 54 68 65 20 63 6f 64 65 20 74 68 61 74 0d 0a 2a  The code that..*
3b25d 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
3b25e 74 69 6e 65 20 77 69 6c 6c 20 75 73 65 20 74 68  tine will use th
3b25f 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  at memory cell t
3b260 6f 20 73 74 6f 72 65 20 74 68 65 20 74 65 72 6d  o store the term
3b261 69 6e 61 74 69 6f 6e 0d 0a 2a 2a 20 6b 65 79 20  ination..** key 
3b262 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f 6f  value of the loo
3b263 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f  p.  If one or mo
3b264 72 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20  re IN operators 
3b265 61 70 70 65 61 72 2c 20 74 68 65 6e 0d 0a 2a 2a  appear, then..**
3b266 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   this routine al
3b267 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64 69 74  locates an addit
3b268 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79  ional nEq memory
3b269 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72   cells for inter
3b26a 6e 61 6c 0d 0a 2a 2a 20 75 73 65 2e 0d 0a 2a 2a  nal..** use...**
3b26b 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75  ..** Before retu
3b26c 72 6e 69 6e 67 2c 20 2a 70 7a 41 66 66 20 69 73  rning, *pzAff is
3b26d 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
3b26e 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69   a buffer contai
3b26f 6e 69 6e 67 20 61 0d 0a 2a 2a 20 63 6f 70 79 20  ning a..** copy 
3b270 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66  of the column af
3b271 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 6f 66  finity string of
3b272 20 74 68 65 20 69 6e 64 65 78 20 61 6c 6c 6f 63   the index alloc
3b273 61 74 65 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 73  ated using..** s
3b274 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29  qlite3DbMalloc()
3b275 2e 20 45 78 63 65 70 74 2c 20 65 6e 74 72 69 65  . Except, entrie
3b276 73 20 69 6e 20 74 68 65 20 63 6f 70 79 20 6f 66  s in the copy of
3b277 20 74 68 65 20 73 74 72 69 6e 67 20 61 73 73 6f   the string asso
3b278 63 69 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68 20  ciated..** with 
3b279 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
3b27a 69 6e 74 73 20 74 68 61 74 20 75 73 65 20 4e 4f  ints that use NO
3b27b 4e 45 20 61 66 66 69 6e 69 74 79 20 61 72 65 20  NE affinity are 
3b27c 73 65 74 20 74 6f 0d 0a 2a 2a 20 53 51 4c 49 54  set to..** SQLIT
3b27d 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54 68 69 73  E_AFF_NONE. This
3b27e 20 69 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68   is to deal with
3b27f 20 53 51 4c 20 73 75 63 68 20 61 73 20 74 68 65   SQL such as the
3b280 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d   following:..**.
3b281 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42  .**   CREATE TAB
3b282 4c 45 20 74 31 28 61 20 54 45 58 54 20 50 52 49  LE t1(a TEXT PRI
3b283 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0d 0a 2a  MARY KEY, b);..*
3b284 2a 20 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46  *   SELECT ... F
3b285 52 4f 4d 20 74 31 20 41 53 20 74 32 2c 20 74 31  ROM t1 AS t2, t1
3b286 20 57 48 45 52 45 20 74 31 2e 61 20 3d 20 74 32   WHERE t1.a = t2
3b287 2e 62 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74  .b;..**..** In t
3b288 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65  he example above
3b289 2c 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74  , the index on t
3b28a 31 28 61 29 20 68 61 73 20 54 45 58 54 20 61 66  1(a) has TEXT af
3b28b 66 69 6e 69 74 79 2e 20 42 75 74 20 73 69 6e 63  finity. But sinc
3b28c 65 0d 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 20  e..** the right 
3b28d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
3b28e 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
3b28f 61 69 6e 74 20 28 74 32 2e 62 29 20 68 61 73 20  aint (t2.b) has 
3b290 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 2c 0d 0a  NONE affinity,..
3b291 2a 2a 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e  ** no conversion
3b292 20 73 68 6f 75 6c 64 20 62 65 20 61 74 74 65 6d   should be attem
3b293 70 74 65 64 20 62 65 66 6f 72 65 20 75 73 69 6e  pted before usin
3b294 67 20 61 20 74 32 2e 62 20 76 61 6c 75 65 20 61  g a t2.b value a
3b295 73 20 70 61 72 74 20 6f 66 0d 0a 2a 2a 20 61 20  s part of..** a 
3b296 6b 65 79 20 74 6f 20 73 65 61 72 63 68 20 74 68  key to search th
3b297 65 20 69 6e 64 65 78 2e 20 48 65 6e 63 65 20 74  e index. Hence t
3b298 68 65 20 66 69 72 73 74 20 62 79 74 65 20 69 6e  he first byte in
3b299 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 66   the returned af
3b29a 66 69 6e 69 74 79 0d 0a 2a 2a 20 73 74 72 69 6e  finity..** strin
3b29b 67 20 69 6e 20 74 68 69 73 20 65 78 61 6d 70 6c  g in this exampl
3b29c 65 20 77 6f 75 6c 64 20 62 65 20 73 65 74 20 74  e would be set t
3b29d 6f 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  o SQLITE_AFF_NON
3b29e 45 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  E...*/..static i
3b29f 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69  nt codeAllEquali
3b2a0 74 79 54 65 72 6d 73 28 0d 0a 20 20 50 61 72 73  tyTerms(..  Pars
3b2a1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
3b2a2 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
3b2a3 74 65 78 74 20 2a 2f 0d 0a 20 20 57 68 65 72 65  text */..  Where
3b2a4 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20  Level *pLevel,  
3b2a5 20 2f 2a 20 57 68 69 63 68 20 6e 65 73 74 65 64   /* Which nested
3b2a6 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 46 52 4f   loop of the FRO
3b2a7 4d 20 77 65 20 61 72 65 20 63 6f 64 69 6e 67 20  M we are coding 
3b2a8 2a 2f 0d 0a 20 20 57 68 65 72 65 43 6c 61 75 73  */..  WhereClaus
3b2a9 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54  e *pWC,     /* T
3b2aa 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
3b2ab 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f  */..  Bitmask no
3b2ac 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20 57  tReady,     /* W
3b2ad 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 46 52  hich parts of FR
3b2ae 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  OM have not yet 
3b2af 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f 0d 0a 20  been coded */.. 
3b2b0 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67 2c 20   int nExtraReg, 
3b2b1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3b2b2 20 6f 66 20 65 78 74 72 61 20 72 65 67 69 73 74   of extra regist
3b2b3 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ers to allocate 
3b2b4 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 41  */..  char **pzA
3b2b5 66 66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ff          /* O
3b2b6 55 54 3a 20 53 65 74 20 74 6f 20 70 6f 69 6e 74  UT: Set to point
3b2b7 20 74 6f 20 61 66 66 69 6e 69 74 79 20 73 74 72   to affinity str
3b2b8 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ing */..){..  in
3b2b9 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e  t nEq = pLevel->
3b2ba 70 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54  plan.nEq;   /* T
3b2bb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  he number of == 
3b2bc 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74  or IN constraint
3b2bd 73 20 74 6f 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  s to code */..  
3b2be 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
3b2bf 2d 3e 70 56 64 62 65 3b 20 20 20 20 20 20 2f 2a  ->pVdbe;      /*
3b2c0 20 54 68 65 20 76 6d 20 75 6e 64 65 72 20 63 6f   The vm under co
3b2c1 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0d 0a 20  nstruction */.. 
3b2c2 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
3b2c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3b2c4 2a 20 54 68 65 20 69 6e 64 65 78 20 62 65 69 6e  * The index bein
3b2c5 67 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  g used for this 
3b2c6 6c 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  loop */..  int i
3b2c7 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54  Cur = pLevel->iT
3b2c8 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68 65 20  abCur;   /* The 
3b2c9 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
3b2ca 62 6c 65 20 2a 2f 0d 0a 20 20 57 68 65 72 65 54  ble */..  WhereT
3b2cb 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
3b2cc 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
3b2cd 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  gle constraint t
3b2ce 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6a 3b  erm */..  int j;
3b2cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b2d0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
3b2d1 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  counter */..  in
3b2d2 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20  t regBase;      
3b2d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
3b2d4 61 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0d  ase register */.
3b2d5 0a 20 20 69 6e 74 20 6e 52 65 67 3b 20 20 20 20  .  int nReg;    
3b2d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b2d7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
3b2d8 67 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63  gisters to alloc
3b2d9 61 74 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  ate */..  char *
3b2da 7a 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  zAff;           
3b2db 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e          /* Affin
3b2dc 69 74 79 20 73 74 72 69 6e 67 20 74 6f 20 72 65  ity string to re
3b2dd 74 75 72 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  turn */....  /* 
3b2de 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 6f  This module is o
3b2df 6e 6c 79 20 63 61 6c 6c 65 64 20 6f 6e 20 71 75  nly called on qu
3b2e0 65 72 79 20 70 6c 61 6e 73 20 74 68 61 74 20 75  ery plans that u
3b2e1 73 65 20 61 6e 20 69 6e 64 65 78 2e 20 2a 2f 0d  se an index. */.
3b2e2 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 76 65  .  assert( pLeve
3b2e3 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3b2e4 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  & WHERE_INDEXED 
3b2e5 29 3b 0d 0a 20 20 70 49 64 78 20 3d 20 70 4c 65  );..  pIdx = pLe
3b2e6 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  vel->plan.u.pIdx
3b2e7 3b 0d 0a 0d 0a 20 20 2f 2a 20 46 69 67 75 72 65  ;....  /* Figure
3b2e8 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65   out how many me
3b2e9 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20 77 69  mory cells we wi
3b2ea 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c  ll need then all
3b2eb 6f 63 61 74 65 20 74 68 65 6d 2e 0d 0a 20 20 2a  ocate them...  *
3b2ec 2f 0d 0a 20 20 72 65 67 42 61 73 65 20 3d 20 70  /..  regBase = p
3b2ed 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
3b2ee 0d 0a 20 20 6e 52 65 67 20 3d 20 70 4c 65 76 65  ..  nReg = pLeve
3b2ef 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45  l->plan.nEq + nE
3b2f0 78 74 72 61 52 65 67 3b 0d 0a 20 20 70 50 61 72  xtraReg;..  pPar
3b2f1 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67  se->nMem += nReg
3b2f2 3b 0d 0a 0d 0a 20 20 7a 41 66 66 20 3d 20 73 71  ;....  zAff = sq
3b2f3 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 50  lite3DbStrDup(pP
3b2f4 61 72 73 65 2d 3e 64 62 2c 20 73 71 6c 69 74 65  arse->db, sqlite
3b2f5 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74  3IndexAffinitySt
3b2f6 72 28 76 2c 20 70 49 64 78 29 29 3b 0d 0a 20 20  r(v, pIdx));..  
3b2f7 69 66 28 20 21 7a 41 66 66 20 29 7b 0d 0a 20 20  if( !zAff ){..  
3b2f8 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61    pParse->db->ma
3b2f9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d  llocFailed = 1;.
3b2fa 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 45 76 61  .  }....  /* Eva
3b2fb 6c 75 61 74 65 20 74 68 65 20 65 71 75 61 6c 69  luate the equali
3b2fc 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0d 0a  ty constraints..
3b2fd 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20    */..  assert( 
3b2fe 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e  pIdx->nColumn>=n
3b2ff 45 71 20 29 3b 0d 0a 20 20 66 6f 72 28 6a 3d 30  Eq );..  for(j=0
3b300 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0d 0a  ; j<nEq; j++){..
3b301 20 20 20 20 69 6e 74 20 72 31 3b 0d 0a 20 20 20      int r1;..   
3b302 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61   int k = pIdx->a
3b303 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0d 0a 20 20 20  iColumn[j];..   
3b304 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72   pTerm = findTer
3b305 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20  m(pWC, iCur, k, 
3b306 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c  notReady, pLevel
3b307 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 20  ->plan.wsFlags, 
3b308 70 49 64 78 29 3b 0d 0a 20 20 20 20 69 66 28 20  pIdx);..    if( 
3b309 4e 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20  NEVER(pTerm==0) 
3b30a 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 2f 2a  ) break;..    /*
3b30b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
3b30c 72 75 65 20 66 6f 72 20 69 6e 64 69 63 65 73 20  rue for indices 
3b30d 77 69 74 68 20 72 65 64 75 6e 64 61 6e 74 20 63  with redundant c
3b30e 6f 6c 75 6d 6e 73 2e 20 0d 0a 20 20 20 20 2a 2a  olumns. ..    **
3b30f 20 45 78 3a 20 43 52 45 41 54 45 20 49 4e 44 45   Ex: CREATE INDE
3b310 58 20 69 31 20 4f 4e 20 74 31 28 61 2c 62 2c 61  X i1 ON t1(a,b,a
3b311 29 3b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  ); SELECT * FROM
3b312 20 74 31 20 57 48 45 52 45 20 61 3d 30 20 41 4e   t1 WHERE a=0 AN
3b313 44 20 62 3d 30 3b 20 2a 2f 0d 0a 20 20 20 20 74  D b=0; */..    t
3b314 65 73 74 63 61 73 65 28 20 28 70 54 65 72 6d 2d  estcase( (pTerm-
3b315 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
3b316 43 4f 44 45 44 29 21 3d 30 20 29 3b 0d 0a 20 20  CODED)!=0 );..  
3b317 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
3b318 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
3b319 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a 20  M_VIRTUAL ); /* 
3b31a 45 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36 36  EV: R-30575-1166
3b31b 32 20 2a 2f 0d 0a 20 20 20 20 72 31 20 3d 20 63  2 */..    r1 = c
3b31c 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
3b31d 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70  pParse, pTerm, p
3b31e 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65 2b 6a  Level, regBase+j
3b31f 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 31 21 3d  );..    if( r1!=
3b320 72 65 67 42 61 73 65 2b 6a 20 29 7b 0d 0a 20 20  regBase+j ){..  
3b321 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31 20      if( nReg==1 
3b322 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
3b323 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
3b324 67 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  g(pParse, regBas
3b325 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 67  e);..        reg
3b326 42 61 73 65 20 3d 20 72 31 3b 0d 0a 20 20 20 20  Base = r1;..    
3b327 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
3b328 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3b329 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
3b32a 20 72 31 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b   r1, regBase+j);
3b32b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
3b32c 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
3b32d 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
3b32e 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0d   & WO_ISNULL );.
3b32f 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
3b330 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
3b331 26 20 57 4f 5f 49 4e 20 29 3b 0d 0a 20 20 20 20  & WO_IN );..    
3b332 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65  if( (pTerm->eOpe
3b333 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55  rator & (WO_ISNU
3b334 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b  LL|WO_IN))==0 ){
3b335 0d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52  ..      Expr *pR
3b336 69 67 68 74 20 3d 20 70 54 65 72 6d 2d 3e 70 45  ight = pTerm->pE
3b337 78 70 72 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20  xpr->pRight;..  
3b338 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3b339 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c  odeIsNullJump(v,
3b33a 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65   pRight, regBase
3b33b 2b 6a 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72  +j, pLevel->addr
3b33c 42 72 6b 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  Brk);..      if(
3b33d 20 7a 41 66 66 20 29 7b 0d 0a 20 20 20 20 20 20   zAff ){..      
3b33e 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 6d    if( sqlite3Com
3b33f 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 52 69  pareAffinity(pRi
3b340 67 68 74 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53  ght, zAff[j])==S
3b341 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
3b342 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 41 66  {..          zAf
3b343 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46  f[j] = SQLITE_AF
3b344 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 20 20 20 20 20  F_NONE;..       
3b345 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
3b346 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73  sqlite3ExprNeeds
3b347 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  NoAffinityChange
3b348 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6a 5d  (pRight, zAff[j]
3b349 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) ){..          
3b34a 7a 41 66 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45  zAff[j] = SQLITE
3b34b 5f 41 46 46 5f 4e 4f 4e 45 3b 0d 0a 20 20 20 20  _AFF_NONE;..    
3b34c 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a      }..      }..
3b34d 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 2a 70      }..  }..  *p
3b34e 7a 41 66 66 20 3d 20 7a 41 66 66 3b 0d 0a 20 20  zAff = zAff;..  
3b34f 72 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0d  return regBase;.
3b350 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  .}....#ifndef SQ
3b351 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
3b352 4e 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72  N../*..** This r
3b353 6f 75 74 69 6e 65 20 69 73 20 61 20 68 65 6c 70  outine is a help
3b354 65 72 20 66 6f 72 20 65 78 70 6c 61 69 6e 49 6e  er for explainIn
3b355 64 65 78 52 61 6e 67 65 28 29 20 62 65 6c 6f 77  dexRange() below
3b356 0d 0a 2a 2a 0d 0a 2a 2a 20 70 53 74 72 20 68 6f  ..**..** pStr ho
3b357 6c 64 73 20 74 68 65 20 74 65 78 74 20 6f 66 20  lds the text of 
3b358 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  an expression th
3b359 61 74 20 77 65 20 61 72 65 20 62 75 69 6c 64 69  at we are buildi
3b35a 6e 67 20 75 70 20 6f 6e 65 20 74 65 72 6d 0d 0a  ng up one term..
3b35b 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20 54  ** at a time.  T
3b35c 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73  his routine adds
3b35d 20 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74   a new term to t
3b35e 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 65 78  he end of the ex
3b35f 70 72 65 73 73 69 6f 6e 2e 0d 0a 2a 2a 20 54 65  pression...** Te
3b360 72 6d 73 20 61 72 65 20 73 65 70 61 72 61 74 65  rms are separate
3b361 64 20 62 79 20 41 4e 44 20 73 6f 20 61 64 64 20  d by AND so add 
3b362 74 68 65 20 22 41 4e 44 22 20 74 65 78 74 20 66  the "AND" text f
3b363 6f 72 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75  or second and su
3b364 62 73 65 71 75 65 6e 74 0d 0a 2a 2a 20 74 65 72  bsequent..** ter
3b365 6d 73 20 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 73 74  ms only...*/..st
3b366 61 74 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69  atic void explai
3b367 6e 41 70 70 65 6e 64 54 65 72 6d 28 0d 0a 20 20  nAppendTerm(..  
3b368 53 74 72 41 63 63 75 6d 20 2a 70 53 74 72 2c 20  StrAccum *pStr, 
3b369 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3b36a 68 65 20 74 65 78 74 20 65 78 70 72 65 73 73 69  he text expressi
3b36b 6f 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20 2a  on being built *
3b36c 2f 0d 0a 20 20 69 6e 74 20 69 54 65 72 6d 2c 20  /..  int iTerm, 
3b36d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b36e 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 69   /* Index of thi
3b36f 73 20 74 65 72 6d 2e 20 20 46 69 72 73 74 20 69  s term.  First i
3b370 73 20 7a 65 72 6f 20 2a 2f 0d 0a 20 20 63 6f 6e  s zero */..  con
3b371 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
3b372 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
3b373 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a   of the column *
3b374 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
3b375 2a 7a 4f 70 20 20 20 20 20 20 20 20 20 20 20 20  *zOp            
3b376 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
3b377 6f 70 65 72 61 74 6f 72 20 2a 2f 0d 0a 29 7b 0d  operator */..){.
3b378 0a 20 20 69 66 28 20 69 54 65 72 6d 20 29 20 73  .  if( iTerm ) s
3b379 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
3b37a 70 65 6e 64 28 70 53 74 72 2c 20 22 20 41 4e 44  pend(pStr, " AND
3b37b 20 22 2c 20 35 29 3b 0d 0a 20 20 73 71 6c 69 74   ", 5);..  sqlit
3b37c 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
3b37d 28 70 53 74 72 2c 20 7a 43 6f 6c 75 6d 6e 2c 20  (pStr, zColumn, 
3b37e 2d 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53  -1);..  sqlite3S
3b37f 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 53  trAccumAppend(pS
3b380 74 72 2c 20 7a 4f 70 2c 20 31 29 3b 0d 0a 20 20  tr, zOp, 1);..  
3b381 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
3b382 70 70 65 6e 64 28 70 53 74 72 2c 20 22 3f 22 2c  ppend(pStr, "?",
3b383 20 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   1);..}..../*..*
3b384 2a 20 41 72 67 75 6d 65 6e 74 20 70 4c 65 76 65  * Argument pLeve
3b385 6c 20 64 65 73 63 72 69 62 65 73 20 61 20 73 74  l describes a st
3b386 72 61 74 65 67 79 20 66 6f 72 20 73 63 61 6e 6e  rategy for scann
3b387 69 6e 67 20 74 61 62 6c 65 20 70 54 61 62 2e 20  ing table pTab. 
3b388 54 68 69 73 20 0d 0a 2a 2a 20 66 75 6e 63 74 69  This ..** functi
3b389 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  on returns a poi
3b38a 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67  nter to a string
3b38b 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
3b38c 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e  ng a description
3b38d 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 73  ..** of the subs
3b38e 65 74 20 6f 66 20 74 61 62 6c 65 20 72 6f 77 73  et of table rows
3b38f 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68 65 20   scanned by the 
3b390 73 74 72 61 74 65 67 79 20 69 6e 20 74 68 65 20  strategy in the 
3b391 66 6f 72 6d 20 6f 66 20 61 6e 0d 0a 2a 2a 20 53  form of an..** S
3b392 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4f  QL expression. O
3b393 72 2c 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61  r, if all rows a
3b394 72 65 20 73 63 61 6e 6e 65 64 2c 20 4e 55 4c 4c  re scanned, NULL
3b395 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   is returned...*
3b396 2a 0d 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  *..** For exampl
3b397 65 2c 20 69 66 20 74 68 65 20 71 75 65 72 79 3a  e, if the query:
3b398 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c 45 43  ..**..**   SELEC
3b399 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52  T * FROM t1 WHER
3b39a 45 20 61 3d 31 20 41 4e 44 20 62 3e 32 3b 0d 0a  E a=1 AND b>2;..
3b39b 2a 2a 0d 0a 2a 2a 20 69 73 20 72 75 6e 20 61 6e  **..** is run an
3b39c 64 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  d there is an in
3b39d 64 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 74  dex on (a, b), t
3b39e 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
3b39f 6e 20 72 65 74 75 72 6e 73 20 61 0d 0a 2a 2a 20  n returns a..** 
3b3a0 73 74 72 69 6e 67 20 73 69 6d 69 6c 61 72 20 74  string similar t
3b3a1 6f 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 22 61 3d  o:..**..**   "a=
3b3a2 3f 20 41 4e 44 20 62 3e 3f 22 0d 0a 2a 2a 0d 0a  ? AND b>?"..**..
3b3a3 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
3b3a4 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74  pointer points t
3b3a5 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
3b3a6 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62  d from sqlite3Db
3b3a7 4d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a 2a 20 49 74  Malloc()...** It
3b3a8 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
3b3a9 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
3b3aa 6c 6c 65 72 20 74 6f 20 66 72 65 65 20 74 68 65  ller to free the
3b3ab 20 62 75 66 66 65 72 20 77 68 65 6e 20 69 74 20   buffer when it 
3b3ac 69 73 0d 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72  is..** no longer
3b3ad 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a 2f 0d 0a   required...*/..
3b3ae 73 74 61 74 69 63 20 63 68 61 72 20 2a 65 78 70  static char *exp
3b3af 6c 61 69 6e 49 6e 64 65 78 52 61 6e 67 65 28 73  lainIndexRange(s
3b3b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72  qlite3 *db, Wher
3b3b1 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20  eLevel *pLevel, 
3b3b2 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0d 0a 20  Table *pTab){.. 
3b3b3 20 57 68 65 72 65 50 6c 61 6e 20 2a 70 50 6c 61   WherePlan *pPla
3b3b4 6e 20 3d 20 26 70 4c 65 76 65 6c 2d 3e 70 6c 61  n = &pLevel->pla
3b3b5 6e 3b 0d 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e  n;..  Index *pIn
3b3b6 64 65 78 20 3d 20 70 50 6c 61 6e 2d 3e 75 2e 70  dex = pPlan->u.p
3b3b7 49 64 78 3b 0d 0a 20 20 69 6e 74 20 6e 45 71 20  Idx;..  int nEq 
3b3b8 3d 20 70 50 6c 61 6e 2d 3e 6e 45 71 3b 0d 0a 20  = pPlan->nEq;.. 
3b3b9 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 43 6f   int i, j;..  Co
3b3ba 6c 75 6d 6e 20 2a 61 43 6f 6c 20 3d 20 70 54 61  lumn *aCol = pTa
3b3bb 62 2d 3e 61 43 6f 6c 3b 0d 0a 20 20 69 6e 74 20  b->aCol;..  int 
3b3bc 2a 61 69 43 6f 6c 75 6d 6e 20 3d 20 70 49 6e 64  *aiColumn = pInd
3b3bd 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 3b 0d 0a 20  ex->aiColumn;.. 
3b3be 20 53 74 72 41 63 63 75 6d 20 74 78 74 3b 0d 0a   StrAccum txt;..
3b3bf 0d 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30 20 26  ..  if( nEq==0 &
3b3c0 26 20 28 70 50 6c 61 6e 2d 3e 77 73 46 6c 61 67  & (pPlan->wsFlag
3b3c1 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f 4c  s & (WHERE_BTM_L
3b3c2 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c  IMIT|WHERE_TOP_L
3b3c3 49 4d 49 54 29 29 3d 3d 30 20 29 7b 0d 0a 20 20  IMIT))==0 ){..  
3b3c4 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d    return 0;..  }
3b3c5 0d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  ..  sqlite3StrAc
3b3c6 63 75 6d 49 6e 69 74 28 26 74 78 74 2c 20 30 2c  cumInit(&txt, 0,
3b3c7 20 30 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c   0, SQLITE_MAX_L
3b3c8 45 4e 47 54 48 29 3b 0d 0a 20 20 74 78 74 2e 64  ENGTH);..  txt.d
3b3c9 62 20 3d 20 64 62 3b 0d 0a 20 20 73 71 6c 69 74  b = db;..  sqlit
3b3ca 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
3b3cb 28 26 74 78 74 2c 20 22 20 28 22 2c 20 32 29 3b  (&txt, " (", 2);
3b3cc 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ..  for(i=0; i<n
3b3cd 45 71 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 65  Eq; i++){..    e
3b3ce 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 72 6d  xplainAppendTerm
3b3cf 28 26 74 78 74 2c 20 69 2c 20 61 43 6f 6c 5b 61  (&txt, i, aCol[a
3b3d0 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e 7a 4e 61 6d  iColumn[i]].zNam
3b3d1 65 2c 20 22 3d 22 29 3b 0d 0a 20 20 7d 0d 0a 0d  e, "=");..  }...
3b3d2 0a 20 20 6a 20 3d 20 69 3b 0d 0a 20 20 69 66 28  .  j = i;..  if(
3b3d3 20 70 50 6c 61 6e 2d 3e 77 73 46 6c 61 67 73 26   pPlan->wsFlags&
3b3d4 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
3b3d5 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 20  ){..    char *z 
3b3d6 3d 20 28 6a 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43  = (j==pIndex->nC
3b3d7 6f 6c 75 6d 6e 20 29 20 3f 20 22 72 6f 77 69 64  olumn ) ? "rowid
3b3d8 22 20 3a 20 61 43 6f 6c 5b 61 69 43 6f 6c 75 6d  " : aCol[aiColum
3b3d9 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0d 0a 20 20  n[j]].zName;..  
3b3da 20 20 65 78 70 6c 61 69 6e 41 70 70 65 6e 64 54    explainAppendT
3b3db 65 72 6d 28 26 74 78 74 2c 20 69 2b 2b 2c 20 7a  erm(&txt, i++, z
3b3dc 2c 20 22 3e 22 29 3b 0d 0a 20 20 7d 0d 0a 20 20  , ">");..  }..  
3b3dd 69 66 28 20 70 50 6c 61 6e 2d 3e 77 73 46 6c 61  if( pPlan->wsFla
3b3de 67 73 26 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  gs&WHERE_TOP_LIM
3b3df 49 54 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20  IT ){..    char 
3b3e0 2a 7a 20 3d 20 28 6a 3d 3d 70 49 6e 64 65 78 2d  *z = (j==pIndex-
3b3e1 3e 6e 43 6f 6c 75 6d 6e 20 29 20 3f 20 22 72 6f  >nColumn ) ? "ro
3b3e2 77 69 64 22 20 3a 20 61 43 6f 6c 5b 61 69 43 6f  wid" : aCol[aiCo
3b3e3 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0d  lumn[j]].zName;.
3b3e4 0a 20 20 20 20 65 78 70 6c 61 69 6e 41 70 70 65  .    explainAppe
3b3e5 6e 64 54 65 72 6d 28 26 74 78 74 2c 20 69 2c 20  ndTerm(&txt, i, 
3b3e6 7a 2c 20 22 3c 22 29 3b 0d 0a 20 20 7d 0d 0a 20  z, "<");..  }.. 
3b3e7 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
3b3e8 41 70 70 65 6e 64 28 26 74 78 74 2c 20 22 29 22  Append(&txt, ")"
3b3e9 2c 20 31 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  , 1);..  return 
3b3ea 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
3b3eb 69 6e 69 73 68 28 26 74 78 74 29 3b 0d 0a 7d 0d  inish(&txt);..}.
3b3ec 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
3b3ed 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
3b3ee 6f 70 20 75 6e 6c 65 73 73 20 63 75 72 72 65 6e  op unless curren
3b3ef 74 6c 79 20 70 72 6f 63 65 73 73 69 6e 67 20 61  tly processing a
3b3f0 6e 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  n EXPLAIN QUERY 
3b3f1 50 4c 41 4e 0d 0a 2a 2a 20 63 6f 6d 6d 61 6e 64  PLAN..** command
3b3f2 2e 20 49 66 20 74 68 65 20 71 75 65 72 79 20 62  . If the query b
3b3f3 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 20 69 73  eing compiled is
3b3f4 20 61 6e 20 45 58 50 4c 41 49 4e 20 51 55 45 52   an EXPLAIN QUER
3b3f5 59 20 50 4c 41 4e 2c 20 61 20 73 69 6e 67 6c 65  Y PLAN, a single
3b3f6 0d 0a 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 61  ..** record is a
3b3f7 64 64 65 64 20 74 6f 20 74 68 65 20 6f 75 74 70  dded to the outp
3b3f8 75 74 20 74 6f 20 64 65 73 63 72 69 62 65 20 74  ut to describe t
3b3f9 68 65 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74  he table scan st
3b3fa 72 61 74 65 67 79 20 69 6e 20 0d 0a 2a 2a 20 70  rategy in ..** p
3b3fb 4c 65 76 65 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  Level...*/..stat
3b3fc 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 4f  ic void explainO
3b3fd 6e 65 53 63 61 6e 28 0d 0a 20 20 50 61 72 73 65  neScan(..  Parse
3b3fe 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
3b3ff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
3b400 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  rse context */..
3b401 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
3b402 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ist,            
3b403 20 20 2f 2a 20 54 61 62 6c 65 20 6c 69 73 74 20    /* Table list 
3b404 74 68 69 73 20 6c 6f 6f 70 20 72 65 66 65 72 73  this loop refers
3b405 20 74 6f 20 2a 2f 0d 0a 20 20 57 68 65 72 65 4c   to */..  WhereL
3b406 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20  evel *pLevel,   
3b407 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 61            /* Sca
3b408 6e 20 74 6f 20 77 72 69 74 65 20 4f 50 5f 45 78  n to write OP_Ex
3b409 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 66 6f 72  plain opcode for
3b40a 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c 65 76 65   */..  int iLeve
3b40b 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
3b40c 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
3b40d 66 6f 72 20 22 6c 65 76 65 6c 22 20 63 6f 6c 75  for "level" colu
3b40e 6d 6e 20 6f 66 20 6f 75 74 70 75 74 20 2a 2f 0d  mn of output */.
3b40f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20  .  int iFrom,   
3b410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b411 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20     /* Value for 
3b412 22 66 72 6f 6d 22 20 63 6f 6c 75 6d 6e 20 6f 66  "from" column of
3b413 20 6f 75 74 70 75 74 20 2a 2f 0d 0a 20 20 75 31   output */..  u1
3b414 36 20 77 63 74 72 6c 46 6c 61 67 73 20 20 20 20  6 wctrlFlags    
3b415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3b416 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f   Flags passed to
3b417 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
3b418 69 6e 28 29 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  in() */..){..  i
3b419 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  f( pParse->expla
3b41a 69 6e 3d 3d 32 20 29 7b 0d 0a 20 20 20 20 75 33  in==2 ){..    u3
3b41b 32 20 66 6c 61 67 73 20 3d 20 70 4c 65 76 65 6c  2 flags = pLevel
3b41c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 3b 0d  ->plan.wsFlags;.
3b41d 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
3b41e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
3b41f 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
3b420 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0d 0a  Level->iFrom];..
3b421 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50      Vdbe *v = pP
3b422 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20  arse->pVdbe;    
3b423 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20 63 6f    /* VM being co
3b424 6e 73 74 72 75 63 74 65 64 20 2a 2f 0d 0a 20 20  nstructed */..  
3b425 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
3b426 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
3b427 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
3b428 6c 65 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20  le */..    char 
3b429 2a 7a 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20  *zMsg;          
3b42a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
3b42b 20 74 6f 20 61 64 64 20 74 6f 20 45 51 50 20 6f   to add to EQP o
3b42c 75 74 70 75 74 20 2a 2f 0d 0a 20 20 20 20 73 71  utput */..    sq
3b42d 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 52 6f 77  lite3_int64 nRow
3b42e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  ;           /* E
3b42f 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f  xpected number o
3b430 66 20 72 6f 77 73 20 76 69 73 69 74 65 64 20 62  f rows visited b
3b431 79 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 20 20 69  y scan */..    i
3b432 6e 74 20 69 49 64 20 3d 20 70 50 61 72 73 65 2d  nt iId = pParse-
3b433 3e 69 53 65 6c 65 63 74 49 64 3b 20 20 2f 2a 20  >iSelectId;  /* 
3b434 53 65 6c 65 63 74 20 69 64 20 28 6c 65 66 74 2d  Select id (left-
3b435 6d 6f 73 74 20 6f 75 74 70 75 74 20 63 6f 6c 75  most output colu
3b436 6d 6e 29 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  mn) */..    int 
3b437 69 73 53 65 61 72 63 68 3b 20 20 20 20 20 20 20  isSearch;       
3b438 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
3b439 65 20 66 6f 72 20 61 20 53 45 41 52 43 48 2e 20  e for a SEARCH. 
3b43a 46 61 6c 73 65 20 66 6f 72 20 53 43 41 4e 2e 20  False for SCAN. 
3b43b 2a 2f 0d 0a 0d 0a 20 20 20 20 69 66 28 20 28 66  */....    if( (f
3b43c 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49  lags&WHERE_MULTI
3b43d 5f 4f 52 29 20 7c 7c 20 28 77 63 74 72 6c 46 6c  _OR) || (wctrlFl
3b43e 61 67 73 26 57 48 45 52 45 5f 4f 4e 45 54 41 42  ags&WHERE_ONETAB
3b43f 4c 45 5f 4f 4e 4c 59 29 20 29 20 72 65 74 75 72  LE_ONLY) ) retur
3b440 6e 3b 0d 0a 0d 0a 20 20 20 20 69 73 53 65 61 72  n;....    isSear
3b441 63 68 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  ch = (pLevel->pl
3b442 61 6e 2e 6e 45 71 3e 30 29 0d 0a 20 20 20 20 20  an.nEq>0)..     
3b443 20 20 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67          || (flag
3b444 73 26 28 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  s&(WHERE_BTM_LIM
3b445 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  IT|WHERE_TOP_LIM
3b446 49 54 29 29 21 3d 30 0d 0a 20 20 20 20 20 20 20  IT))!=0..       
3b447 20 20 20 20 20 20 7c 7c 20 28 77 63 74 72 6c 46        || (wctrlF
3b448 6c 61 67 73 26 28 57 48 45 52 45 5f 4f 52 44 45  lags&(WHERE_ORDE
3b449 52 42 59 5f 4d 49 4e 7c 57 48 45 52 45 5f 4f 52  RBY_MIN|WHERE_OR
3b44a 44 45 52 42 59 5f 4d 41 58 29 29 3b 0d 0a 0d 0a  DERBY_MAX));....
3b44b 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
3b44c 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
3b44d 73 22 2c 20 69 73 53 65 61 72 63 68 3f 22 53 45  s", isSearch?"SE
3b44e 41 52 43 48 22 3a 22 53 43 41 4e 22 29 3b 0d 0a  ARCH":"SCAN");..
3b44f 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
3b450 53 65 6c 65 63 74 20 29 7b 0d 0a 20 20 20 20 20  Select ){..     
3b451 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
3b452 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
3b453 2c 20 22 25 73 20 53 55 42 51 55 45 52 59 20 25  , "%s SUBQUERY %
3b454 64 22 2c 20 7a 4d 73 67 2c 70 49 74 65 6d 2d 3e  d", zMsg,pItem->
3b455 69 53 65 6c 65 63 74 49 64 29 3b 0d 0a 20 20 20  iSelectId);..   
3b456 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 7a   }else{..      z
3b457 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
3b458 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
3b459 22 25 73 20 54 41 42 4c 45 20 25 73 22 2c 20 7a  "%s TABLE %s", z
3b45a 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  Msg, pItem->zNam
3b45b 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  e);..    }....  
3b45c 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c    if( pItem->zAl
3b45d 69 61 73 20 29 7b 0d 0a 20 20 20 20 20 20 7a 4d  ias ){..      zM
3b45e 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
3b45f 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
3b460 25 73 20 41 53 20 25 73 22 2c 20 7a 4d 73 67 2c  %s AS %s", zMsg,
3b461 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b   pItem->zAlias);
3b462 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
3b463 20 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f   (flags & WHERE_
3b464 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0d 0a  INDEXED)!=0 ){..
3b465 20 20 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65        char *zWhe
3b466 72 65 20 3d 20 65 78 70 6c 61 69 6e 49 6e 64 65  re = explainInde
3b467 78 52 61 6e 67 65 28 64 62 2c 20 70 4c 65 76 65  xRange(db, pLeve
3b468 6c 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b  l, pItem->pTab);
3b469 0d 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73  ..      zMsg = s
3b46a 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
3b46b 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55 53 49  b, zMsg, "%s USI
3b46c 4e 47 20 25 73 25 73 49 4e 44 45 58 25 73 25 73  NG %s%sINDEX%s%s
3b46d 25 73 22 2c 20 7a 4d 73 67 2c 20 0d 0a 20 20 20  %s", zMsg, ..   
3b46e 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 20 26         ((flags &
3b46f 20 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45   WHERE_TEMP_INDE
3b470 58 29 3f 22 41 55 54 4f 4d 41 54 49 43 20 22 3a  X)?"AUTOMATIC ":
3b471 22 22 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  ""),..          
3b472 28 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f  ((flags & WHERE_
3b473 49 44 58 5f 4f 4e 4c 59 29 3f 22 43 4f 56 45 52  IDX_ONLY)?"COVER
3b474 49 4e 47 20 22 3a 22 22 29 2c 0d 0a 20 20 20 20  ING ":""),..    
3b475 20 20 20 20 20 20 28 28 66 6c 61 67 73 20 26 20        ((flags & 
3b476 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58  WHERE_TEMP_INDEX
3b477 29 3f 22 22 3a 22 20 22 29 2c 0d 0a 20 20 20 20  )?"":" "),..    
3b478 20 20 20 20 20 20 28 28 66 6c 61 67 73 20 26 20        ((flags & 
3b479 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58  WHERE_TEMP_INDEX
3b47a 29 3f 22 22 3a 20 70 4c 65 76 65 6c 2d 3e 70 6c  )?"": pLevel->pl
3b47b 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65  an.u.pIdx->zName
3b47c 29 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 57  ),..          zW
3b47d 68 65 72 65 0d 0a 20 20 20 20 20 20 29 3b 0d 0a  here..      );..
3b47e 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
3b47f 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b  ree(db, zWhere);
3b480 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
3b481 66 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52  flags & (WHERE_R
3b482 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f  OWID_EQ|WHERE_RO
3b483 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0d 0a 20  WID_RANGE) ){.. 
3b484 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
3b485 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
3b486 7a 4d 73 67 2c 20 22 25 73 20 55 53 49 4e 47 20  zMsg, "%s USING 
3b487 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
3b488 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0d 0a 0d 0a  KEY", zMsg);....
3b489 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 73 26        if( flags&
3b48a 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29  WHERE_ROWID_EQ )
3b48b 7b 0d 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  {..        zMsg 
3b48c 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
3b48d 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
3b48e 28 72 6f 77 69 64 3d 3f 29 22 2c 20 7a 4d 73 67  (rowid=?)", zMsg
3b48f 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  );..      }else 
3b490 69 66 28 20 28 66 6c 61 67 73 26 57 48 45 52 45  if( (flags&WHERE
3b491 5f 42 4f 54 48 5f 4c 49 4d 49 54 29 3d 3d 57 48  _BOTH_LIMIT)==WH
3b492 45 52 45 5f 42 4f 54 48 5f 4c 49 4d 49 54 20 29  ERE_BOTH_LIMIT )
3b493 7b 0d 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  {..        zMsg 
3b494 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
3b495 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
3b496 28 72 6f 77 69 64 3e 3f 20 41 4e 44 20 72 6f 77  (rowid>? AND row
3b497 69 64 3c 3f 29 22 2c 20 7a 4d 73 67 29 3b 0d 0a  id<?)", zMsg);..
3b498 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
3b499 66 6c 61 67 73 26 57 48 45 52 45 5f 42 54 4d 5f  flags&WHERE_BTM_
3b49a 4c 49 4d 49 54 20 29 7b 0d 0a 20 20 20 20 20 20  LIMIT ){..      
3b49b 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
3b49c 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
3b49d 67 2c 20 22 25 73 20 28 72 6f 77 69 64 3e 3f 29  g, "%s (rowid>?)
3b49e 22 2c 20 7a 4d 73 67 29 3b 0d 0a 20 20 20 20 20  ", zMsg);..     
3b49f 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
3b4a0 26 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54  &WHERE_TOP_LIMIT
3b4a1 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 7a 4d 73   ){..        zMs
3b4a2 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
3b4a3 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
3b4a4 73 20 28 72 6f 77 69 64 3c 3f 29 22 2c 20 7a 4d  s (rowid<?)", zM
3b4a5 73 67 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  sg);..      }.. 
3b4a6 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51     }..#ifndef SQ
3b4a7 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
3b4a8 4c 54 41 42 4c 45 0d 0a 20 20 20 20 65 6c 73 65  LTABLE..    else
3b4a9 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 57 48   if( (flags & WH
3b4aa 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
3b4ab 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73  )!=0 ){..      s
3b4ac 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3b4ad 6f 20 2a 70 56 74 61 62 49 64 78 20 3d 20 70 4c  o *pVtabIdx = pL
3b4ae 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74  evel->plan.u.pVt
3b4af 61 62 49 64 78 3b 0d 0a 20 20 20 20 20 20 7a 4d  abIdx;..      zM
3b4b0 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
3b4b1 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
3b4b2 25 73 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  %s VIRTUAL TABLE
3b4b3 20 49 4e 44 45 58 20 25 64 3a 25 73 22 2c 20 7a   INDEX %d:%s", z
3b4b4 4d 73 67 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  Msg,..          
3b4b5 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78          pVtabIdx
3b4b6 2d 3e 69 64 78 4e 75 6d 2c 20 70 56 74 61 62 49  ->idxNum, pVtabI
3b4b7 64 78 2d 3e 69 64 78 53 74 72 29 3b 0d 0a 20 20  dx->idxStr);..  
3b4b8 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20    }..#endif..   
3b4b9 20 69 66 28 20 77 63 74 72 6c 46 6c 61 67 73 26   if( wctrlFlags&
3b4ba 28 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  (WHERE_ORDERBY_M
3b4bb 49 4e 7c 57 48 45 52 45 5f 4f 52 44 45 52 42 59  IN|WHERE_ORDERBY
3b4bc 5f 4d 41 58 29 20 29 7b 0d 0a 20 20 20 20 20 20  _MAX) ){..      
3b4bd 74 65 73 74 63 61 73 65 28 20 77 63 74 72 6c 46  testcase( wctrlF
3b4be 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44  lags & WHERE_ORD
3b4bf 45 52 42 59 5f 4d 49 4e 20 29 3b 0d 0a 20 20 20  ERBY_MIN );..   
3b4c0 20 20 20 6e 52 6f 77 20 3d 20 31 3b 0d 0a 20 20     nRow = 1;..  
3b4c1 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
3b4c2 6e 52 6f 77 20 3d 20 28 73 71 6c 69 74 65 33 5f  nRow = (sqlite3_
3b4c3 69 6e 74 36 34 29 70 4c 65 76 65 6c 2d 3e 70 6c  int64)pLevel->pl
3b4c4 61 6e 2e 6e 52 6f 77 3b 0d 0a 20 20 20 20 7d 0d  an.nRow;..    }.
3b4c5 0a 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69  .    zMsg = sqli
3b4c6 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
3b4c7 7a 4d 73 67 2c 20 22 25 73 20 28 7e 25 6c 6c 64  zMsg, "%s (~%lld
3b4c8 20 72 6f 77 73 29 22 2c 20 7a 4d 73 67 2c 20 6e   rows)", zMsg, n
3b4c9 52 6f 77 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Row);..    sqlit
3b4ca 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
3b4cb 4f 50 5f 45 78 70 6c 61 69 6e 2c 20 69 49 64 2c  OP_Explain, iId,
3b4cc 20 69 4c 65 76 65 6c 2c 20 69 46 72 6f 6d 2c 20   iLevel, iFrom, 
3b4cd 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43  zMsg, P4_DYNAMIC
3b4ce 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73  );..  }..}..#els
3b4cf 65 0d 0a 23 20 64 65 66 69 6e 65 20 65 78 70 6c  e..# define expl
3b4d0 61 69 6e 4f 6e 65 53 63 61 6e 28 75 2c 76 2c 77  ainOneScan(u,v,w
3b4d1 2c 78 2c 79 2c 7a 29 0d 0a 23 65 6e 64 69 66 20  ,x,y,z)..#endif 
3b4d2 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  /* SQLITE_OMIT_E
3b4d3 58 50 4c 41 49 4e 20 2a 2f 0d 0a 0d 0a 0d 0a 2f  XPLAIN */....../
3b4d4 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  *..** Generate c
3b4d5 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 72  ode for the star
3b4d6 74 20 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d  t of the iLevel-
3b4d7 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57  th loop in the W
3b4d8 48 45 52 45 20 63 6c 61 75 73 65 0d 0a 2a 2a 20  HERE clause..** 
3b4d9 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
3b4da 65 73 63 72 69 62 65 64 20 62 79 20 70 57 49 6e  escribed by pWIn
3b4db 66 6f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  fo...*/..static 
3b4dc 42 69 74 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c  Bitmask codeOneL
3b4dd 6f 6f 70 53 74 61 72 74 28 0d 0a 20 20 57 68 65  oopStart(..  Whe
3b4de 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 2c 20  reInfo *pWInfo, 
3b4df 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 69 6e    /* Complete in
3b4e0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
3b4e1 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3b4e2 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c 65 76 65   */..  int iLeve
3b4e3 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  l,          /* W
3b4e4 68 69 63 68 20 6c 65 76 65 6c 20 6f 66 20 70 57  hich level of pW
3b4e5 49 6e 66 6f 2d 3e 61 5b 5d 20 73 68 6f 75 6c 64  Info->a[] should
3b4e6 20 62 65 20 63 6f 64 65 64 20 2a 2f 0d 0a 20 20   be coded */..  
3b4e7 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73 2c 20  u16 wctrlFlags, 
3b4e8 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
3b4e9 68 65 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73  he WHERE_* flags
3b4ea 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69   defined in sqli
3b4eb 74 65 49 6e 74 2e 68 20 2a 2f 0d 0a 20 20 42 69  teInt.h */..  Bi
3b4ec 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
3b4ed 20 20 20 2f 2a 20 57 68 69 63 68 20 74 61 62 6c     /* Which tabl
3b4ee 65 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  es are currently
3b4ef 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0d 0a 20   available */.. 
3b4f0 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
3b4f1 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74        /* Complet
3b4f2 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
3b4f3 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 6a 2c 20  /..){..  int j, 
3b4f4 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k;            /*
3b4f5 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
3b4f6 2f 0d 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20  /..  int iCur;  
3b4f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3b4f8 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
3b4f9 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a 20   the table */.. 
3b4fa 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20   int addrNxt;   
3b4fb 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74        /* Where t
3b4fc 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74 69 6e  o jump to contin
3b4fd 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74  ue with the next
3b4fe 20 49 4e 20 63 61 73 65 20 2a 2f 0d 0a 20 20 69   IN case */..  i
3b4ff 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20 20  nt omitTable;   
3b500 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
3b501 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78 20  e use the index 
3b502 6f 6e 6c 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 62  only */..  int b
3b503 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rev;            
3b504 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65  /* True if we ne
3b505 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65  ed to scan in re
3b506 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0d 0a  verse order */..
3b507 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
3b508 65 76 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68  evel;  /* The wh
3b509 65 72 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20  ere level to be 
3b50a 63 6f 64 65 64 20 2a 2f 0d 0a 20 20 57 68 65 72  coded */..  Wher
3b50b 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20  eClause *pWC;   
3b50c 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f   /* Decompositio
3b50d 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20  n of the entire 
3b50e 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0d  WHERE clause */.
3b50f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
3b510 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
3b511 20 20 20 2f 2a 20 41 20 57 48 45 52 45 20 63 6c     /* A WHERE cl
3b512 61 75 73 65 20 74 65 72 6d 20 2a 2f 0d 0a 20 20  ause term */..  
3b513 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
3b514 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b515 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
3b516 78 74 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76  xt */..  Vdbe *v
3b517 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3b518 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3b519 70 72 65 70 61 72 65 64 20 73 74 6d 74 20 75 6e  prepared stmt un
3b51a 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
3b51b 73 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 53  s */..  struct S
3b51c 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61  rcList_item *pTa
3b51d 62 49 74 65 6d 3b 20 20 2f 2a 20 46 52 4f 4d 20  bItem;  /* FROM 
3b51e 63 6c 61 75 73 65 20 74 65 72 6d 20 62 65 69 6e  clause term bein
3b51f 67 20 63 6f 64 65 64 20 2a 2f 0d 0a 20 20 69 6e  g coded */..  in
3b520 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20  t addrBrk;      
3b521 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3b522 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72   Jump here to br
3b523 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  eak out of the l
3b524 6f 6f 70 20 2a 2f 0d 0a 20 20 69 6e 74 20 61 64  oop */..  int ad
3b525 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20  drCont;         
3b526 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
3b527 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e  p here to contin
3b528 75 65 20 77 69 74 68 20 6e 65 78 74 20 63 79 63  ue with next cyc
3b529 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 52 6f  le */..  int iRo
3b52a 77 69 64 52 65 67 20 3d 20 30 3b 20 20 20 20 20  widReg = 0;     
3b52b 20 20 20 2f 2a 20 52 6f 77 69 64 20 69 73 20 73     /* Rowid is s
3b52c 74 6f 72 65 64 20 69 6e 20 74 68 69 73 20 72 65  tored in this re
3b52d 67 69 73 74 65 72 2c 20 69 66 20 6e 6f 74 20 7a  gister, if not z
3b52e 65 72 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 52  ero */..  int iR
3b52f 65 6c 65 61 73 65 52 65 67 20 3d 20 30 3b 20 20  eleaseReg = 0;  
3b530 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69      /* Temp regi
3b531 73 74 65 72 20 74 6f 20 66 72 65 65 20 62 65 66  ster to free bef
3b532 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f  ore returning */
3b533 0d 0a 0d 0a 20 20 70 50 61 72 73 65 20 3d 20 70  ....  pParse = p
3b534 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0d 0a  WInfo->pParse;..
3b535 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
3b536 64 62 65 3b 0d 0a 20 20 70 57 43 20 3d 20 70 57  dbe;..  pWC = pW
3b537 49 6e 66 6f 2d 3e 70 57 43 3b 0d 0a 20 20 70 4c  Info->pWC;..  pL
3b538 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  evel = &pWInfo->
3b539 61 5b 69 4c 65 76 65 6c 5d 3b 0d 0a 20 20 70 54  a[iLevel];..  pT
3b53a 61 62 49 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f  abItem = &pWInfo
3b53b 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c  ->pTabList->a[pL
3b53c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0d 0a 20  evel->iFrom];.. 
3b53d 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d   iCur = pTabItem
3b53e 2d 3e 69 43 75 72 73 6f 72 3b 0d 0a 20 20 62 52  ->iCursor;..  bR
3b53f 65 76 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  ev = (pLevel->pl
3b540 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3b541 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30 3b 0d  RE_REVERSE)!=0;.
3b542 0a 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d 20 28  .  omitTable = (
3b543 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3b544 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
3b545 5f 4f 4e 4c 59 29 21 3d 30 20 0d 0a 20 20 20 20  _ONLY)!=0 ..    
3b546 20 20 20 20 20 20 20 26 26 20 28 77 63 74 72 6c         && (wctrl
3b547 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 46 4f  Flags & WHERE_FO
3b548 52 43 45 5f 54 41 42 4c 45 29 3d 3d 30 3b 0d 0a  RCE_TABLE)==0;..
3b549 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 6c 61  ..  /* Create la
3b54a 62 65 6c 73 20 66 6f 72 20 74 68 65 20 22 62 72  bels for the "br
3b54b 65 61 6b 22 20 61 6e 64 20 22 63 6f 6e 74 69 6e  eak" and "contin
3b54c 75 65 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ue" instructions
3b54d 0d 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63  ..  ** for the c
3b54e 75 72 72 65 6e 74 20 6c 6f 6f 70 2e 20 20 4a 75  urrent loop.  Ju
3b54f 6d 70 20 74 6f 20 61 64 64 72 42 72 6b 20 74 6f  mp to addrBrk to
3b550 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 61 20   break out of a 
3b551 6c 6f 6f 70 2e 0d 0a 20 20 2a 2a 20 4a 75 6d 70  loop...  ** Jump
3b552 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67 6f 20 69   to cont to go i
3b553 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 74 68  mmediately to th
3b554 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
3b555 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 6c 6f   of the..  ** lo
3b556 6f 70 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  op...  **..  ** 
3b557 57 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e  When there is an
3b558 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 77 65   IN operator, we
3b559 20 61 6c 73 6f 20 68 61 76 65 20 61 20 22 61 64   also have a "ad
3b55a 64 72 4e 78 74 22 20 6c 61 62 65 6c 20 74 68 61  drNxt" label tha
3b55b 74 0d 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74 6f  t..  ** means to
3b55c 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74   continue with t
3b55d 68 65 20 6e 65 78 74 20 49 4e 20 76 61 6c 75 65  he next IN value
3b55e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20 57   combination.  W
3b55f 68 65 6e 0d 0a 20 20 2a 2a 20 74 68 65 72 65 20  hen..  ** there 
3b560 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74  are no IN operat
3b561 6f 72 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74  ors in the const
3b562 72 61 69 6e 74 73 2c 20 74 68 65 20 22 61 64 64  raints, the "add
3b563 72 4e 78 74 22 20 6c 61 62 65 6c 0d 0a 20 20 2a  rNxt" label..  *
3b564 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  * is the same as
3b565 20 22 61 64 64 72 42 72 6b 22 2e 0d 0a 20 20 2a   "addrBrk"...  *
3b566 2f 0d 0a 20 20 61 64 64 72 42 72 6b 20 3d 20 70  /..  addrBrk = p
3b567 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d  Level->addrBrk =
3b568 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74   pLevel->addrNxt
3b569 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
3b56a 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20 20 61  keLabel(v);..  a
3b56b 64 64 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c  ddrCont = pLevel
3b56c 2d 3e 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c  ->addrCont = sql
3b56d 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
3b56e 6c 28 76 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66  l(v);....  /* If
3b56f 20 74 68 69 73 20 69 73 20 74 68 65 20 72 69 67   this is the rig
3b570 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45  ht table of a LE
3b571 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61  FT OUTER JOIN, a
3b572 6c 6c 6f 63 61 74 65 20 61 6e 64 0d 0a 20 20 2a  llocate and..  *
3b573 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d  * initialize a m
3b574 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
3b575 72 65 63 6f 72 64 73 20 69 66 20 74 68 69 73 20  records if this 
3b576 74 61 62 6c 65 20 6d 61 74 63 68 65 73 20 61 6e  table matches an
3b577 79 0d 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 74  y..  ** row of t
3b578 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66  he left table of
3b579 20 74 68 65 20 6a 6f 69 6e 2e 0d 0a 20 20 2a 2f   the join...  */
3b57a 0d 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  ..  if( pLevel->
3b57b 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61 62  iFrom>0 && (pTab
3b57c 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65  Item[0].jointype
3b57d 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29   & JT_LEFT)!=0 )
3b57e 7b 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69  {..    pLevel->i
3b57f 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70 50 61  LeftJoin = ++pPa
3b580 72 73 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20  rse->nMem;..    
3b581 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3b582 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
3b583 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66   0, pLevel->iLef
3b584 74 4a 6f 69 6e 29 3b 0d 0a 20 20 20 20 56 64 62  tJoin);..    Vdb
3b585 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
3b586 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d  it LEFT JOIN no-
3b587 6d 61 74 63 68 20 66 6c 61 67 22 29 29 3b 0d 0a  match flag"));..
3b588 20 20 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53    }....#ifndef S
3b589 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
3b58a 41 4c 54 41 42 4c 45 0d 0a 20 20 69 66 28 20 20  ALTABLE..  if(  
3b58b 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
3b58c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49  Flags & WHERE_VI
3b58d 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29  RTUALTABLE)!=0 )
3b58e 7b 0d 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 30  {..    /* Case 0
3b58f 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  :  The table is 
3b590 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e  a virtual-table.
3b591 20 20 55 73 65 20 74 68 65 20 56 46 69 6c 74 65    Use the VFilte
3b592 72 20 61 6e 64 20 56 4e 65 78 74 0d 0a 20 20 20  r and VNext..   
3b593 20 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20   **          to 
3b594 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 2e  access the data.
3b595 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 6e  ..    */..    in
3b596 74 20 69 52 65 67 3b 20 20 20 2f 2a 20 50 33 20  t iReg;   /* P3 
3b597 56 61 6c 75 65 20 66 6f 72 20 4f 50 5f 56 46 69  Value for OP_VFi
3b598 6c 74 65 72 20 2a 2f 0d 0a 20 20 20 20 73 71 6c  lter */..    sql
3b599 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
3b59a 2a 70 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76  *pVtabIdx = pLev
3b59b 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62  el->plan.u.pVtab
3b59c 49 64 78 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 43  Idx;..    int nC
3b59d 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 56 74 61  onstraint = pVta
3b59e 62 49 64 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  bIdx->nConstrain
3b59f 74 3b 0d 0a 20 20 20 20 73 74 72 75 63 74 20 73  t;..    struct s
3b5a0 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
3b5a1 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 61  straint_usage *a
3b5a2 55 73 61 67 65 20 3d 0d 0a 20 20 20 20 20 20 20  Usage =..       
3b5a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b5a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b5a5 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
3b5a6 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  x->aConstraintUs
3b5a7 61 67 65 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20  age;..    const 
3b5a8 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
3b5a9 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
3b5aa 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0d 0a  *aConstraint =..
3b5ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b5ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b5ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b5ae 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74  pVtabIdx->aConst
3b5af 72 61 69 6e 74 3b 0d 0a 0d 0a 20 20 20 20 73 71  raint;....    sq
3b5b0 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
3b5b1 73 68 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20  sh(pParse);..   
3b5b2 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47   iReg = sqlite3G
3b5b3 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
3b5b4 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b  se, nConstraint+
3b5b5 32 29 3b 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 31  2);..    for(j=1
3b5b6 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69 6e 74  ; j<=nConstraint
3b5b7 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 66  ; j++){..      f
3b5b8 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73 74  or(k=0; k<nConst
3b5b9 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0d 0a 20 20  raint; k++){..  
3b5ba 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65        if( aUsage
3b5bb 5b 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a  [k].argvIndex==j
3b5bc 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69   ){..          i
3b5bd 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73  nt iTerm = aCons
3b5be 74 72 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f  traint[k].iTermO
3b5bf 66 66 73 65 74 3b 0d 0a 20 20 20 20 20 20 20 20  ffset;..        
3b5c0 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
3b5c1 65 28 70 50 61 72 73 65 2c 20 70 57 43 2d 3e 61  e(pParse, pWC->a
3b5c2 5b 69 54 65 72 6d 5d 2e 70 45 78 70 72 2d 3e 70  [iTerm].pExpr->p
3b5c3 52 69 67 68 74 2c 20 69 52 65 67 2b 6a 2b 31 29  Right, iReg+j+1)
3b5c4 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ;..          bre
3b5c5 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ak;..        }..
3b5c6 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
3b5c7 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69 6e  f( k==nConstrain
3b5c8 74 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20  t ) break;..    
3b5c9 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  }..    sqlite3Vd
3b5ca 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
3b5cb 6e 74 65 67 65 72 2c 20 70 56 74 61 62 49 64 78  nteger, pVtabIdx
3b5cc 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65 67 29 3b  ->idxNum, iReg);
3b5cd 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
3b5ce 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
3b5cf 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52 65 67  teger, j-1, iReg
3b5d0 2b 31 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  +1);..    sqlite
3b5d1 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
3b5d2 50 5f 56 46 69 6c 74 65 72 2c 20 69 43 75 72 2c  P_VFilter, iCur,
3b5d3 20 61 64 64 72 42 72 6b 2c 20 69 52 65 67 2c 20   addrBrk, iReg, 
3b5d4 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72  pVtabIdx->idxStr
3b5d5 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
3b5d6 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
3b5d7 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  x->needToFreeIdx
3b5d8 53 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46  Str ? P4_MPRINTF
3b5d9 20 3a 20 50 34 5f 53 54 41 54 49 43 29 3b 0d 0a   : P4_STATIC);..
3b5da 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65      pVtabIdx->ne
3b5db 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d  edToFreeIdxStr =
3b5dc 20 30 3b 0d 0a 20 20 20 20 66 6f 72 28 6a 3d 30   0;..    for(j=0
3b5dd 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  ; j<nConstraint;
3b5de 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66   j++){..      if
3b5df 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74  ( aUsage[j].omit
3b5e0 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74   ){..        int
3b5e1 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72   iTerm = aConstr
3b5e2 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66  aint[j].iTermOff
3b5e3 73 65 74 3b 0d 0a 20 20 20 20 20 20 20 20 64 69  set;..        di
3b5e4 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
3b5e5 2c 20 26 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d  , &pWC->a[iTerm]
3b5e6 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
3b5e7 20 7d 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e   }..    pLevel->
3b5e8 6f 70 20 3d 20 4f 50 5f 56 4e 65 78 74 3b 0d 0a  op = OP_VNext;..
3b5e9 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d      pLevel->p1 =
3b5ea 20 69 43 75 72 3b 0d 0a 20 20 20 20 70 4c 65 76   iCur;..    pLev
3b5eb 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33  el->p2 = sqlite3
3b5ec 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
3b5ed 76 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  v);..    sqlite3
3b5ee 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
3b5ef 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c 20 6e  (pParse, iReg, n
3b5f0 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0d 0a  Constraint+2);..
3b5f1 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3b5f2 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20  achePop(pParse, 
3b5f3 31 29 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a 23 65  1);..  }else..#e
3b5f4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
3b5f5 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
3b5f6 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 70 4c 65   */....  if( pLe
3b5f7 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
3b5f8 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  s & WHERE_ROWID_
3b5f9 45 51 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 43 61  EQ ){..    /* Ca
3b5fa 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69  se 1:  We can di
3b5fb 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65  rectly reference
3b5fc 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73   a single row us
3b5fd 69 6e 67 20 61 6e 0d 0a 20 20 20 20 2a 2a 20 20  ing an..    **  
3b5fe 20 20 20 20 20 20 20 20 65 71 75 61 6c 69 74 79          equality
3b5ff 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69   comparison agai
3b600 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69  nst the ROWID fi
3b601 65 6c 64 2e 20 20 4f 72 0d 0a 20 20 20 20 2a 2a  eld.  Or..    **
3b602 20 20 20 20 20 20 20 20 20 20 77 65 20 72 65 66            we ref
3b603 65 72 65 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20  erence multiple 
3b604 72 6f 77 73 20 75 73 69 6e 67 20 61 20 22 72 6f  rows using a "ro
3b605 77 69 64 20 49 4e 20 28 2e 2e 2e 29 22 0d 0a 20  wid IN (...)".. 
3b606 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 63     **          c
3b607 6f 6e 73 74 72 75 63 74 2e 0d 0a 20 20 20 20 2a  onstruct...    *
3b608 2f 0d 0a 20 20 20 20 69 52 65 6c 65 61 73 65 52  /..    iReleaseR
3b609 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  eg = sqlite3GetT
3b60a 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
3b60b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e  .    pTerm = fin
3b60c 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
3b60d 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57   -1, notReady, W
3b60e 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0d  O_EQ|WO_IN, 0);.
3b60f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
3b610 72 6d 21 3d 30 20 29 3b 0d 0a 20 20 20 20 61 73  rm!=0 );..    as
3b611 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78  sert( pTerm->pEx
3b612 70 72 21 3d 30 20 29 3b 0d 0a 20 20 20 20 61 73  pr!=0 );..    as
3b613 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65 66  sert( pTerm->lef
3b614 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b  tCursor==iCur );
3b615 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d  ..    assert( om
3b616 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20  itTable==0 );.. 
3b617 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
3b618 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
3b619 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a  RM_VIRTUAL ); /*
3b61a 20 45 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36   EV: R-30575-116
3b61b 36 32 20 2a 2f 0d 0a 20 20 20 20 69 52 6f 77 69  62 */..    iRowi
3b61c 64 52 65 67 20 3d 20 63 6f 64 65 45 71 75 61 6c  dReg = codeEqual
3b61d 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ityTerm(pParse, 
3b61e 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 69  pTerm, pLevel, i
3b61f 52 65 6c 65 61 73 65 52 65 67 29 3b 0d 0a 20 20  ReleaseReg);..  
3b620 20 20 61 64 64 72 4e 78 74 20 3d 20 70 4c 65 76    addrNxt = pLev
3b621 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0d 0a 20 20  el->addrNxt;..  
3b622 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3b623 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op2(v, OP_MustBe
3b624 49 6e 74 2c 20 69 52 6f 77 69 64 52 65 67 2c 20  Int, iRowidReg, 
3b625 61 64 64 72 4e 78 74 29 3b 0d 0a 20 20 20 20 73  addrNxt);..    s
3b626 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
3b627 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
3b628 2c 20 69 43 75 72 2c 20 61 64 64 72 4e 78 74 2c  , iCur, addrNxt,
3b629 20 69 52 6f 77 69 64 52 65 67 29 3b 0d 0a 20 20   iRowidReg);..  
3b62a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3b62b 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20  heStore(pParse, 
3b62c 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64  iCur, -1, iRowid
3b62d 52 65 67 29 3b 0d 0a 20 20 20 20 56 64 62 65 43  Reg);..    VdbeC
3b62e 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29  omment((v, "pk")
3b62f 29 3b 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  );..    pLevel->
3b630 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0d 0a 20  op = OP_Noop;.. 
3b631 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
3b632 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3b633 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  & WHERE_ROWID_RA
3b634 4e 47 45 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 43  NGE ){..    /* C
3b635 61 73 65 20 32 3a 20 20 57 65 20 68 61 76 65 20  ase 2:  We have 
3b636 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  an inequality co
3b637 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74  mparison against
3b638 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64   the ROWID field
3b639 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  ...    */..    i
3b63a 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e  nt testOp = OP_N
3b63b 6f 6f 70 3b 0d 0a 20 20 20 20 69 6e 74 20 73 74  oop;..    int st
3b63c 61 72 74 3b 0d 0a 20 20 20 20 69 6e 74 20 6d 65  art;..    int me
3b63d 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0d 0a  mEndValue = 0;..
3b63e 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
3b63f 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0d 0a 0d  Start, *pEnd;...
3b640 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69  .    assert( omi
3b641 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a 20 20  tTable==0 );..  
3b642 20 20 70 53 74 61 72 74 20 3d 20 66 69 6e 64 54    pStart = findT
3b643 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d  erm(pWC, iCur, -
3b644 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  1, notReady, WO_
3b645 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0d 0a 20  GT|WO_GE, 0);.. 
3b646 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64 54 65     pEnd = findTe
3b647 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31  rm(pWC, iCur, -1
3b648 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c  , notReady, WO_L
3b649 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0d 0a 20 20  T|WO_LE, 0);..  
3b64a 20 20 69 66 28 20 62 52 65 76 20 29 7b 0d 0a 20    if( bRev ){.. 
3b64b 20 20 20 20 20 70 54 65 72 6d 20 3d 20 70 53 74       pTerm = pSt
3b64c 61 72 74 3b 0d 0a 20 20 20 20 20 20 70 53 74 61  art;..      pSta
3b64d 72 74 20 3d 20 70 45 6e 64 3b 0d 0a 20 20 20 20  rt = pEnd;..    
3b64e 20 20 70 45 6e 64 20 3d 20 70 54 65 72 6d 3b 0d    pEnd = pTerm;.
3b64f 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
3b650 70 53 74 61 72 74 20 29 7b 0d 0a 20 20 20 20 20  pStart ){..     
3b651 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20   Expr *pX;      
3b652 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
3b653 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 64 65  pression that de
3b654 66 69 6e 65 73 20 74 68 65 20 73 74 61 72 74 20  fines the start 
3b655 62 6f 75 6e 64 20 2a 2f 0d 0a 20 20 20 20 20 20  bound */..      
3b656 69 6e 74 20 72 31 2c 20 72 54 65 6d 70 3b 20 20  int r1, rTemp;  
3b657 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
3b658 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74  rs for holding t
3b659 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 61 72  he start boundar
3b65a 79 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  y */....      /*
3b65b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
3b65c 6f 6e 73 74 61 6e 74 20 6d 61 70 73 20 54 4b 5f  onstant maps TK_
3b65d 78 78 20 63 6f 64 65 73 20 69 6e 74 6f 20 63 6f  xx codes into co
3b65e 72 72 65 73 70 6f 6e 64 69 6e 67 20 0d 0a 20 20  rresponding ..  
3b65f 20 20 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f      ** seek opco
3b660 64 65 73 2e 20 20 49 74 20 64 65 70 65 6e 64 73  des.  It depends
3b661 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
3b662 20 6f 72 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f   ordering of TK_
3b663 78 78 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  xx..      */..  
3b664 20 20 20 20 63 6f 6e 73 74 20 75 38 20 61 4d 6f      const u8 aMo
3b665 76 65 4f 70 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20  veOp[] = {..    
3b666 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47 54 20         /* TK_GT 
3b667 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 74 2c 0d 0a  */  OP_SeekGt,..
3b668 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b             /* TK
3b669 5f 4c 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c  _LE */  OP_SeekL
3b66a 65 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 2f  e,..           /
3b66b 2a 20 54 4b 5f 4c 54 20 2a 2f 20 20 4f 50 5f 53  * TK_LT */  OP_S
3b66c 65 65 6b 4c 74 2c 0d 0a 20 20 20 20 20 20 20 20  eekLt,..        
3b66d 20 20 20 2f 2a 20 54 4b 5f 47 45 20 2a 2f 20 20     /* TK_GE */  
3b66e 4f 50 5f 53 65 65 6b 47 65 0d 0a 20 20 20 20 20  OP_SeekGe..     
3b66f 20 7d 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   };..      asser
3b670 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b 5f 47 54 2b  t( TK_LE==TK_GT+
3b671 31 20 29 3b 20 20 20 20 20 20 2f 2a 20 4d 61 6b  1 );      /* Mak
3b672 65 20 73 75 72 65 20 74 68 65 20 6f 72 64 65 72  e sure the order
3b673 69 6e 67 2e 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  ing.. */..      
3b674 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54  assert( TK_LT==T
3b675 4b 5f 47 54 2b 32 20 29 3b 20 20 20 20 20 20 2f  K_GT+2 );      /
3b676 2a 20 20 2e 2e 2e 20 6f 66 20 74 68 65 20 54 4b  *  ... of the TK
3b677 5f 78 78 20 76 61 6c 75 65 73 2e 2e 2e 20 2a 2f  _xx values... */
3b678 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
3b679 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33 20 29  TK_GE==TK_GT+3 )
3b67a 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 69  ;      /*  ... i
3b67b 73 20 63 6f 72 72 65 63 63 74 2e 20 2a 2f 0d 0a  s correcct. */..
3b67c 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3b67d 28 20 70 53 74 61 72 74 2d 3e 77 74 46 6c 61 67  ( pStart->wtFlag
3b67e 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
3b67f 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 35   ); /* EV: R-305
3b680 37 35 2d 31 31 36 36 32 20 2a 2f 0d 0a 20 20 20  75-11662 */..   
3b681 20 20 20 70 58 20 3d 20 70 53 74 61 72 74 2d 3e     pX = pStart->
3b682 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20 61 73  pExpr;..      as
3b683 73 65 72 74 28 20 70 58 21 3d 30 20 29 3b 0d 0a  sert( pX!=0 );..
3b684 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
3b685 74 61 72 74 2d 3e 6c 65 66 74 43 75 72 73 6f 72  tart->leftCursor
3b686 3d 3d 69 43 75 72 20 29 3b 0d 0a 20 20 20 20 20  ==iCur );..     
3b687 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
3b688 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
3b689 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 26 72  , pX->pRight, &r
3b68a 54 65 6d 70 29 3b 0d 0a 20 20 20 20 20 20 73 71  Temp);..      sq
3b68b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
3b68c 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e 6f  v, aMoveOp[pX->o
3b68d 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c 20  p-TK_GT], iCur, 
3b68e 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0d 0a 20  addrBrk, r1);.. 
3b68f 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
3b690 28 28 76 2c 20 22 70 6b 22 29 29 3b 0d 0a 20 20  ((v, "pk"));..  
3b691 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3b692 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
3b693 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31  ge(pParse, r1, 1
3b694 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
3b695 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
3b696 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0d  pParse, rTemp);.
3b697 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65  .      disableTe
3b698 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61 72  rm(pLevel, pStar
3b699 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  t);..    }else{.
3b69a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3b69b 62 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65 76  beAddOp2(v, bRev
3b69c 20 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f   ? OP_Last : OP_
3b69d 52 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64  Rewind, iCur, ad
3b69e 64 72 42 72 6b 29 3b 0d 0a 20 20 20 20 7d 0d 0a  drBrk);..    }..
3b69f 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0d      if( pEnd ){.
3b6a0 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b  .      Expr *pX;
3b6a1 0d 0a 20 20 20 20 20 20 70 58 20 3d 20 70 45 6e  ..      pX = pEn
3b6a2 64 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20  d->pExpr;..     
3b6a3 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29   assert( pX!=0 )
3b6a4 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
3b6a5 20 70 45 6e 64 2d 3e 6c 65 66 74 43 75 72 73 6f   pEnd->leftCurso
3b6a6 72 3d 3d 69 43 75 72 20 29 3b 0d 0a 20 20 20 20  r==iCur );..    
3b6a7 20 20 74 65 73 74 63 61 73 65 28 20 70 45 6e 64    testcase( pEnd
3b6a8 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
3b6a9 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a 20 45  _VIRTUAL ); /* E
3b6aa 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36 36 32  V: R-30575-11662
3b6ab 20 2a 2f 0d 0a 20 20 20 20 20 20 6d 65 6d 45 6e   */..      memEn
3b6ac 64 56 61 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73  dValue = ++pPars
3b6ad 65 2d 3e 6e 4d 65 6d 3b 0d 0a 20 20 20 20 20 20  e->nMem;..      
3b6ae 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
3b6af 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
3b6b0 68 74 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 29  ht, memEndValue)
3b6b1 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 58 2d  ;..      if( pX-
3b6b2 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70 58  >op==TK_LT || pX
3b6b3 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29 7b 0d 0a  ->op==TK_GT ){..
3b6b4 20 20 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d          testOp =
3b6b5 20 62 52 65 76 20 3f 20 4f 50 5f 4c 65 20 3a 20   bRev ? OP_Le : 
3b6b6 4f 50 5f 47 65 3b 0d 0a 20 20 20 20 20 20 7d 65  OP_Ge;..      }e
3b6b7 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 74 65  lse{..        te
3b6b8 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50  stOp = bRev ? OP
3b6b9 5f 4c 74 20 3a 20 4f 50 5f 47 74 3b 0d 0a 20 20  _Lt : OP_Gt;..  
3b6ba 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 64 69 73      }..      dis
3b6bb 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
3b6bc 20 70 45 6e 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a   pEnd);..    }..
3b6bd 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69      start = sqli
3b6be 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
3b6bf 64 72 28 76 29 3b 0d 0a 20 20 20 20 70 4c 65 76  dr(v);..    pLev
3b6c0 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20  el->op = bRev ? 
3b6c1 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78  OP_Prev : OP_Nex
3b6c2 74 3b 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  t;..    pLevel->
3b6c3 70 31 20 3d 20 69 43 75 72 3b 0d 0a 20 20 20 20  p1 = iCur;..    
3b6c4 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61  pLevel->p2 = sta
3b6c5 72 74 3b 0d 0a 20 20 20 20 69 66 28 20 70 53 74  rt;..    if( pSt
3b6c6 61 72 74 3d 3d 30 20 26 26 20 70 45 6e 64 3d 3d  art==0 && pEnd==
3b6c7 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 4c 65 76  0 ){..      pLev
3b6c8 65 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f  el->p5 = SQLITE_
3b6c9 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53  STMTSTATUS_FULLS
3b6ca 43 41 4e 5f 53 54 45 50 3b 0d 0a 20 20 20 20 7d  CAN_STEP;..    }
3b6cb 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61 73 73  else{..      ass
3b6cc 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 35 3d  ert( pLevel->p5=
3b6cd 3d 30 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  =0 );..    }..  
3b6ce 20 20 69 66 28 20 74 65 73 74 4f 70 21 3d 4f 50    if( testOp!=OP
3b6cf 5f 4e 6f 6f 70 20 29 7b 0d 0a 20 20 20 20 20 20  _Noop ){..      
3b6d0 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65 6c  iRowidReg = iRel
3b6d1 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65  easeReg = sqlite
3b6d2 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
3b6d3 73 65 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  se);..      sqli
3b6d4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3b6d5 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
3b6d6 20 69 52 6f 77 69 64 52 65 67 29 3b 0d 0a 20 20   iRowidReg);..  
3b6d7 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3b6d8 61 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65  acheStore(pParse
3b6d9 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77  , iCur, -1, iRow
3b6da 69 64 52 65 67 29 3b 0d 0a 20 20 20 20 20 20 73  idReg);..      s
3b6db 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
3b6dc 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45  (v, testOp, memE
3b6dd 6e 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b  ndValue, addrBrk
3b6de 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0d 0a 20  , iRowidReg);.. 
3b6df 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3b6e0 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
3b6e1 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c  TE_AFF_NUMERIC |
3b6e2 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
3b6e3 4c 4c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  LL);..    }..  }
3b6e4 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
3b6e5 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
3b6e6 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  (WHERE_COLUMN_RA
3b6e7 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  NGE|WHERE_COLUMN
3b6e8 5f 45 51 29 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  _EQ) ){..    /* 
3b6e9 43 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75  Case 3: A scan u
3b6ea 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0d 0a  sing an index...
3b6eb 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20      **..    **  
3b6ec 20 20 20 20 20 20 20 54 68 65 20 57 48 45 52 45         The WHERE
3b6ed 20 63 6c 61 75 73 65 20 6d 61 79 20 63 6f 6e 74   clause may cont
3b6ee 61 69 6e 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65  ain zero or more
3b6ef 20 65 71 75 61 6c 69 74 79 20 0d 0a 20 20 20 20   equality ..    
3b6f0 2a 2a 20 20 20 20 20 20 20 20 20 74 65 72 6d 73  **         terms
3b6f1 20 28 22 3d 3d 22 20 6f 72 20 22 49 4e 22 20 6f   ("==" or "IN" o
3b6f2 70 65 72 61 74 6f 72 73 29 20 74 68 61 74 20 72  perators) that r
3b6f3 65 66 65 72 20 74 6f 20 74 68 65 20 4e 0d 0a 20  efer to the N.. 
3b6f4 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65     **         le
3b6f5 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 73 20  ft-most columns 
3b6f6 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 49 74  of the index. It
3b6f7 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69   may also contai
3b6f8 6e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  n..    **       
3b6f9 20 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e    inequality con
3b6fa 73 74 72 61 69 6e 74 73 20 28 3e 2c 20 3c 2c 20  straints (>, <, 
3b6fb 3e 3d 20 6f 72 20 3c 3d 29 20 6f 6e 20 74 68 65  >= or <=) on the
3b6fc 20 69 6e 64 65 78 65 64 0d 0a 20 20 20 20 2a 2a   indexed..    **
3b6fd 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
3b6fe 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79  that immediately
3b6ff 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 4e 20 65   follows the N e
3b700 71 75 61 6c 69 74 69 65 73 2e 20 4f 6e 6c 79 20  qualities. Only 
3b701 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
3b702 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
3b703 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 61 6e  column can be an
3b704 20 69 6e 65 71 75 61 6c 69 74 79 20 2d 20 74 68   inequality - th
3b705 65 20 72 65 73 74 20 6d 75 73 74 0d 0a 20 20 20  e rest must..   
3b706 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73 65 20   **         use 
3b707 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22 49 4e  the "==" and "IN
3b708 22 20 6f 70 65 72 61 74 6f 72 73 2e 20 46 6f 72  " operators. For
3b709 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
3b70a 20 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   ..    **       
3b70b 20 20 69 6e 64 65 78 20 69 73 20 6f 6e 20 28 78    index is on (x
3b70c 2c 79 2c 7a 29 2c 20 74 68 65 6e 20 74 68 65 20  ,y,z), then the 
3b70d 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c 61 75 73 65  following clause
3b70e 73 20 61 72 65 20 61 6c 6c 20 0d 0a 20 20 20 20  s are all ..    
3b70f 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 74 69 6d  **         optim
3b710 69 7a 65 64 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20  ized:..    **.. 
3b711 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
3b712 20 78 3d 35 0d 0a 20 20 20 20 2a 2a 20 20 20 20   x=5..    **    
3b713 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
3b714 79 3d 31 30 0d 0a 20 20 20 20 2a 2a 20 20 20 20  y=10..    **    
3b715 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
3b716 79 3c 31 30 0d 0a 20 20 20 20 2a 2a 20 20 20 20  y<10..    **    
3b717 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
3b718 79 3e 35 20 41 4e 44 20 79 3c 31 30 0d 0a 20 20  y>5 AND y<10..  
3b719 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
3b71a 78 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20  x=5 AND y=5 AND 
3b71b 7a 3c 3d 31 30 0d 0a 20 20 20 20 2a 2a 0d 0a 20  z<=10..    **.. 
3b71c 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68     **         Th
3b71d 65 20 7a 3c 31 30 20 74 65 72 6d 20 6f 66 20 74  e z<10 term of t
3b71e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e  he following can
3b71f 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 6f 6e 6c  not be used, onl
3b720 79 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  y..    **       
3b721 20 20 74 68 65 20 78 3d 35 20 74 65 72 6d 3a 0d    the x=5 term:.
3b722 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3b723 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41             x=5 A
3b724 4e 44 20 7a 3c 31 30 0d 0a 20 20 20 20 2a 2a 0d  ND z<10..    **.
3b725 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3b726 4e 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 69 66  N may be zero if
3b727 20 74 68 65 72 65 20 61 72 65 20 69 6e 65 71 75   there are inequ
3b728 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
3b729 73 2e 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  s...    **      
3b72a 20 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20     If there are 
3b72b 6e 6f 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  no inequality co
3b72c 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 6e 20  nstraints, then 
3b72d 4e 20 69 73 20 61 74 0d 0a 20 20 20 20 2a 2a 20  N is at..    ** 
3b72e 20 20 20 20 20 20 20 20 6c 65 61 73 74 20 6f 6e          least on
3b72f 65 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  e...    **..    
3b730 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69 73 20  **         This 
3b731 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65  case is also use
3b732 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  d when there are
3b733 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65   no WHERE clause
3b734 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
3b735 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74   constraints but
3b736 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c   an index is sel
3b737 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e  ected anyway, in
3b738 20 6f 72 64 65 72 0d 0a 20 20 20 20 2a 2a 20 20   order..    **  
3b739 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20         to force 
3b73a 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
3b73b 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61   to conform to a
3b73c 6e 20 4f 52 44 45 52 20 42 59 2e 0d 0a 20 20 20  n ORDER BY...   
3b73d 20 2a 2f 20 20 0d 0a 20 20 20 20 73 74 61 74 69   */  ..    stati
3b73e 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72  c const u8 aStar
3b73f 74 4f 70 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20  tOp[] = {..     
3b740 20 30 2c 0d 0a 20 20 20 20 20 20 30 2c 0d 0a 20   0,..      0,.. 
3b741 20 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c 20       OP_Rewind, 
3b742 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20            /* 2: 
3b743 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  (!start_constrai
3b744 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26  nts && startEq &
3b745 26 20 20 21 62 52 65 76 29 20 2a 2f 0d 0a 20 20  &  !bRev) */..  
3b746 20 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20      OP_Last,    
3b747 20 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28           /* 3: (
3b748 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  !start_constrain
3b749 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26  ts && startEq &&
3b74a 20 20 20 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20     bRev) */..   
3b74b 20 20 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20     OP_SeekGt,   
3b74c 20 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 28 73          /* 4: (s
3b74d 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
3b74e 20 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26    && !startEq &&
3b74f 20 21 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20   !bRev) */..    
3b750 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20    OP_SeekLt,    
3b751 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 28 73 74         /* 5: (st
3b752 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
3b753 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20   && !startEq && 
3b754 20 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20 20   bRev) */..     
3b755 20 4f 50 5f 53 65 65 6b 47 65 2c 20 20 20 20 20   OP_SeekGe,     
3b756 20 20 20 20 20 20 2f 2a 20 36 3a 20 28 73 74 61        /* 6: (sta
3b757 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20  rt_constraints  
3b758 26 26 20 20 73 74 61 72 74 45 71 20 26 26 20 21  &&  startEq && !
3b759 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20 20 20  bRev) */..      
3b75a 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20  OP_SeekLe       
3b75b 20 20 20 20 20 2f 2a 20 37 3a 20 28 73 74 61 72       /* 7: (star
3b75c 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
3b75d 26 20 20 73 74 61 72 74 45 71 20 26 26 20 20 62  &  startEq &&  b
3b75e 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20 7d 3b 0d  Rev) */..    };.
3b75f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
3b760 74 20 75 38 20 61 45 6e 64 4f 70 5b 5d 20 3d 20  t u8 aEndOp[] = 
3b761 7b 0d 0a 20 20 20 20 20 20 4f 50 5f 4e 6f 6f 70  {..      OP_Noop
3b762 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
3b763 20 30 3a 20 28 21 65 6e 64 5f 63 6f 6e 73 74 72   0: (!end_constr
3b764 61 69 6e 74 73 29 20 2a 2f 0d 0a 20 20 20 20 20  aints) */..     
3b765 20 4f 50 5f 49 64 78 47 45 2c 20 20 20 20 20 20   OP_IdxGE,      
3b766 20 20 20 20 20 20 2f 2a 20 31 3a 20 28 65 6e 64        /* 1: (end
3b767 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20  _constraints && 
3b768 21 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20 20  !bRev) */..     
3b769 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20   OP_IdxLT       
3b76a 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 65 6e 64        /* 2: (end
3b76b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20  _constraints && 
3b76c 62 52 65 76 29 20 2a 2f 0d 0a 20 20 20 20 7d 3b  bRev) */..    };
3b76d 0d 0a 20 20 20 20 69 6e 74 20 6e 45 71 20 3d 20  ..    int nEq = 
3b76e 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71  pLevel->plan.nEq
3b76f 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ;  /* Number of 
3b770 3d 3d 20 6f 72 20 49 4e 20 74 65 72 6d 73 20 2a  == or IN terms *
3b771 2f 0d 0a 20 20 20 20 69 6e 74 20 69 73 4d 69 6e  /..    int isMin
3b772 51 75 65 72 79 20 3d 20 30 3b 20 20 20 20 20 20  Query = 0;      
3b773 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
3b774 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65 64 20 53  s an optimized S
3b775 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e 2e 20 2a  ELECT min(x).. *
3b776 2f 0d 0a 20 20 20 20 69 6e 74 20 72 65 67 42 61  /..    int regBa
3b777 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
3b778 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69      /* Base regi
3b779 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6e  ster holding con
3b77a 73 74 72 61 69 6e 74 20 76 61 6c 75 65 73 20 2a  straint values *
3b77b 2f 0d 0a 20 20 20 20 69 6e 74 20 72 31 3b 20 20  /..    int r1;  
3b77c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b77d 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69      /* Temp regi
3b77e 73 74 65 72 20 2a 2f 0d 0a 20 20 20 20 57 68 65  ster */..    Whe
3b77f 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 53 74  reTerm *pRangeSt
3b780 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65  art = 0;  /* Ine
3b781 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
3b782 6e 74 20 61 74 20 72 61 6e 67 65 20 73 74 61 72  nt at range star
3b783 74 20 2a 2f 0d 0a 20 20 20 20 57 68 65 72 65 54  t */..    WhereT
3b784 65 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d  erm *pRangeEnd =
3b785 20 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71 75 61   0;    /* Inequa
3b786 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20  lity constraint 
3b787 61 74 20 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0d  at range end */.
3b788 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 45 71  .    int startEq
3b789 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3b78a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e    /* True if ran
3b78b 67 65 20 73 74 61 72 74 20 75 73 65 73 20 3d 3d  ge start uses ==
3b78c 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0d 0a 20  , >= or <= */.. 
3b78d 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20 20 20     int endEq;   
3b78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b78f 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65  /* True if range
3b790 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d   end uses ==, >=
3b791 20 6f 72 20 3c 3d 20 2a 2f 0d 0a 20 20 20 20 69   or <= */..    i
3b792 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61  nt start_constra
3b793 69 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20 53  ints;       /* S
3b794 74 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69 73  tart of range is
3b795 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0d   constrained */.
3b796 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  .    int nConstr
3b797 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  aint;           
3b798 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
3b799 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
3b79a 2a 2f 0d 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  */..    Index *p
3b79b 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
3b79c 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
3b79d 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69  x we will be usi
3b79e 6e 67 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69  ng */..    int i
3b79f 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20  IdxCur;         
3b7a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
3b7a1 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
3b7a2 68 65 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20 20  he index */..   
3b7a3 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67 20 3d   int nExtraReg =
3b7a4 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
3b7a5 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61   Number of extra
3b7a6 20 72 65 67 69 73 74 65 72 73 20 6e 65 65 64 65   registers neede
3b7a7 64 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6f 70  d */..    int op
3b7a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3b7a9 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75         /* Instru
3b7aa 63 74 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0d  ction opcode */.
3b7ab 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 61 72  .    char *zStar
3b7ac 74 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  tAff;           
3b7ad 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 66 6f    /* Affinity fo
3b7ae 72 20 73 74 61 72 74 20 6f 66 20 72 61 6e 67 65  r start of range
3b7af 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a   constraint */..
3b7b0 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 41 66      char *zEndAf
3b7b1 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
3b7b2 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 66 6f 72   /* Affinity for
3b7b3 20 65 6e 64 20 6f 66 20 72 61 6e 67 65 20 63 6f   end of range co
3b7b4 6e 73 74 72 61 69 6e 74 20 2a 2f 0d 0a 0d 0a 20  nstraint */.... 
3b7b5 20 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c     pIdx = pLevel
3b7b6 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0d 0a  ->plan.u.pIdx;..
3b7b7 20 20 20 20 69 49 64 78 43 75 72 20 3d 20 70 4c      iIdxCur = pL
3b7b8 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0d 0a  evel->iIdxCur;..
3b7b9 20 20 20 20 6b 20 3d 20 28 6e 45 71 3d 3d 70 49      k = (nEq==pI
3b7ba 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3f 20 2d 31  dx->nColumn ? -1
3b7bb 20 3a 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   : pIdx->aiColum
3b7bc 6e 5b 6e 45 71 5d 29 3b 0d 0a 0d 0a 20 20 20 20  n[nEq]);....    
3b7bd 2f 2a 20 49 66 20 74 68 69 73 20 6c 6f 6f 70 20  /* If this loop 
3b7be 73 61 74 69 73 66 69 65 73 20 61 20 73 6f 72 74  satisfies a sort
3b7bf 20 6f 72 64 65 72 20 28 70 4f 72 64 65 72 42 79   order (pOrderBy
3b7c0 29 20 72 65 71 75 65 73 74 20 74 68 61 74 20 0d  ) request that .
3b7c1 0a 20 20 20 20 2a 2a 20 77 61 73 20 70 61 73 73  .    ** was pass
3b7c2 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
3b7c3 69 6f 6e 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ion to implement
3b7c4 20 61 20 22 53 45 4c 45 43 54 20 6d 69 6e 28 78   a "SELECT min(x
3b7c5 29 20 2e 2e 2e 22 20 0d 0a 20 20 20 20 2a 2a 20  ) ..." ..    ** 
3b7c6 71 75 65 72 79 2c 20 74 68 65 6e 20 74 68 65 20  query, then the 
3b7c7 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 6e 6c 79  caller will only
3b7c8 20 61 6c 6c 6f 77 20 74 68 65 20 6c 6f 6f 70 20   allow the loop 
3b7c9 74 6f 20 72 75 6e 20 66 6f 72 0d 0a 20 20 20 20  to run for..    
3b7ca 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 74 65 72  ** a single iter
3b7cb 61 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e  ation. This mean
3b7cc 73 20 74 68 61 74 20 74 68 65 20 66 69 72 73 74  s that the first
3b7cd 20 72 6f 77 20 72 65 74 75 72 6e 65 64 0d 0a 20   row returned.. 
3b7ce 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74     ** should not
3b7cf 20 68 61 76 65 20 61 20 4e 55 4c 4c 20 76 61 6c   have a NULL val
3b7d0 75 65 20 73 74 6f 72 65 64 20 69 6e 20 27 78 27  ue stored in 'x'
3b7d1 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 27 78 27 20  . If column 'x' 
3b7d2 69 73 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 66  is..    ** the f
3b7d3 69 72 73 74 20 6f 6e 65 20 61 66 74 65 72 20 74  irst one after t
3b7d4 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 79 20  he nEq equality 
3b7d5 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74  constraints in t
3b7d6 68 65 20 69 6e 64 65 78 2c 0d 0a 20 20 20 20 2a  he index,..    *
3b7d7 2a 20 74 68 69 73 20 72 65 71 75 69 72 65 73 20  * this requires 
3b7d8 73 6f 6d 65 20 73 70 65 63 69 61 6c 20 68 61 6e  some special han
3b7d9 64 6c 69 6e 67 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  dling...    */..
3b7da 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c      if( (wctrlFl
3b7db 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42  ags&WHERE_ORDERB
3b7dc 59 5f 4d 49 4e 29 21 3d 30 0d 0a 20 20 20 20 20  Y_MIN)!=0..     
3b7dd 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  && (pLevel->plan
3b7de 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f  .wsFlags&WHERE_O
3b7df 52 44 45 52 42 59 29 0d 0a 20 20 20 20 20 26 26  RDERBY)..     &&
3b7e0 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e   (pIdx->nColumn>
3b7e1 6e 45 71 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20  nEq)..    ){..  
3b7e2 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70      /* assert( p
3b7e3 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d  OrderBy->nExpr==
3b7e4 31 20 29 3b 20 2a 2f 0d 0a 20 20 20 20 20 20 2f  1 ); */..      /
3b7e5 2a 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72  * assert( pOrder
3b7e6 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  By->a[0].pExpr->
3b7e7 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61  iColumn==pIdx->a
3b7e8 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20  iColumn[nEq] ); 
3b7e9 2a 2f 0d 0a 20 20 20 20 20 20 69 73 4d 69 6e 51  */..      isMinQ
3b7ea 75 65 72 79 20 3d 20 31 3b 0d 0a 20 20 20 20 20  uery = 1;..     
3b7eb 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0d   nExtraReg = 1;.
3b7ec 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
3b7ed 20 46 69 6e 64 20 61 6e 79 20 69 6e 65 71 75 61   Find any inequa
3b7ee 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20  lity constraint 
3b7ef 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 74  terms for the st
3b7f0 61 72 74 20 61 6e 64 20 65 6e 64 20 0d 0a 20 20  art and end ..  
3b7f1 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 61 6e 67    ** of the rang
3b7f2 65 2e 20 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  e. ..    */..   
3b7f3 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
3b7f4 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
3b7f5 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0d 0a  E_TOP_LIMIT ){..
3b7f6 20 20 20 20 20 20 70 52 61 6e 67 65 45 6e 64 20        pRangeEnd 
3b7f7 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
3b7f8 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
3b7f9 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29  y, (WO_LT|WO_LE)
3b7fa 2c 20 70 49 64 78 29 3b 0d 0a 20 20 20 20 20 20  , pIdx);..      
3b7fb 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0d 0a  nExtraReg = 1;..
3b7fc 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 70      }..    if( p
3b7fd 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3b7fe 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f  ags & WHERE_BTM_
3b7ff 4c 49 4d 49 54 20 29 7b 0d 0a 20 20 20 20 20 20  LIMIT ){..      
3b800 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69  pRangeStart = fi
3b801 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
3b802 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28  , k, notReady, (
3b803 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49  WO_GT|WO_GE), pI
3b804 64 78 29 3b 0d 0a 20 20 20 20 20 20 6e 45 78 74  dx);..      nExt
3b805 72 61 52 65 67 20 3d 20 31 3b 0d 0a 20 20 20 20  raReg = 1;..    
3b806 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 47 65 6e 65  }....    /* Gene
3b807 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
3b808 6c 75 61 74 65 20 61 6c 6c 20 63 6f 6e 73 74 72  luate all constr
3b809 61 69 6e 74 20 74 65 72 6d 73 20 75 73 69 6e 67  aint terms using
3b80a 20 3d 3d 20 6f 72 20 49 4e 0d 0a 20 20 20 20 2a   == or IN..    *
3b80b 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
3b80c 76 61 6c 75 65 73 20 6f 66 20 74 68 6f 73 65 20  values of those 
3b80d 74 65 72 6d 73 20 69 6e 20 61 6e 20 61 72 72 61  terms in an arra
3b80e 79 20 6f 66 20 72 65 67 69 73 74 65 72 73 0d 0a  y of registers..
3b80f 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
3b810 61 74 20 72 65 67 42 61 73 65 2e 0d 0a 20 20 20  at regBase...   
3b811 20 2a 2f 0d 0a 20 20 20 20 72 65 67 42 61 73 65   */..    regBase
3b812 20 3d 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69   = codeAllEquali
3b813 74 79 54 65 72 6d 73 28 0d 0a 20 20 20 20 20 20  tyTerms(..      
3b814 20 20 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c    pParse, pLevel
3b815 2c 20 70 57 43 2c 20 6e 6f 74 52 65 61 64 79 2c  , pWC, notReady,
3b816 20 6e 45 78 74 72 61 52 65 67 2c 20 26 7a 53 74   nExtraReg, &zSt
3b817 61 72 74 41 66 66 0d 0a 20 20 20 20 29 3b 0d 0a  artAff..    );..
3b818 20 20 20 20 7a 45 6e 64 41 66 66 20 3d 20 73 71      zEndAff = sq
3b819 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 50  lite3DbStrDup(pP
3b81a 61 72 73 65 2d 3e 64 62 2c 20 7a 53 74 61 72 74  arse->db, zStart
3b81b 41 66 66 29 3b 0d 0a 20 20 20 20 61 64 64 72 4e  Aff);..    addrN
3b81c 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64  xt = pLevel->add
3b81d 72 4e 78 74 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  rNxt;....    /* 
3b81e 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20  If we are doing 
3b81f 61 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  a reverse order 
3b820 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63 65 6e  scan on an ascen
3b821 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72 0d 0a  ding index, or..
3b822 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72 64      ** a forward
3b823 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61   order scan on a
3b824 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64 65   descending inde
3b825 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20 74  x, interchange t
3b826 68 65 20 0d 0a 20 20 20 20 2a 2a 20 73 74 61 72  he ..    ** star
3b827 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73 20  t and end terms 
3b828 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e 64  (pRangeStart and
3b829 20 70 52 61 6e 67 65 45 6e 64 29 2e 0d 0a 20 20   pRangeEnd)...  
3b82a 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 28 6e    */..    if( (n
3b82b 45 71 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  Eq<pIdx->nColumn
3b82c 20 26 26 20 62 52 65 76 3d 3d 28 70 49 64 78 2d   && bRev==(pIdx-
3b82d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d  >aSortOrder[nEq]
3b82e 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29  ==SQLITE_SO_ASC)
3b82f 29 0d 0a 20 20 20 20 20 7c 7c 20 28 62 52 65 76  )..     || (bRev
3b830 20 26 26 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   && pIdx->nColum
3b831 6e 3d 3d 6e 45 71 29 0d 0a 20 20 20 20 29 7b 0d  n==nEq)..    ){.
3b832 0a 20 20 20 20 20 20 53 57 41 50 28 57 68 65 72  .      SWAP(Wher
3b833 65 54 65 72 6d 20 2a 2c 20 70 52 61 6e 67 65 45  eTerm *, pRangeE
3b834 6e 64 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29  nd, pRangeStart)
3b835 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
3b836 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65  testcase( pRange
3b837 53 74 61 72 74 20 26 26 20 70 52 61 6e 67 65 53  Start && pRangeS
3b838 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
3b839 26 20 57 4f 5f 4c 45 20 29 3b 0d 0a 20 20 20 20  & WO_LE );..    
3b83a 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65  testcase( pRange
3b83b 53 74 61 72 74 20 26 26 20 70 52 61 6e 67 65 53  Start && pRangeS
3b83c 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
3b83d 26 20 57 4f 5f 47 45 20 29 3b 0d 0a 20 20 20 20  & WO_GE );..    
3b83e 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65  testcase( pRange
3b83f 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e 64  End && pRangeEnd
3b840 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f  ->eOperator & WO
3b841 5f 4c 45 20 29 3b 0d 0a 20 20 20 20 74 65 73 74  _LE );..    test
3b842 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20  case( pRangeEnd 
3b843 26 26 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f  && pRangeEnd->eO
3b844 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20  perator & WO_GE 
3b845 29 3b 0d 0a 20 20 20 20 73 74 61 72 74 45 71 20  );..    startEq 
3b846 3d 20 21 70 52 61 6e 67 65 53 74 61 72 74 20 7c  = !pRangeStart |
3b847 7c 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65  | pRangeStart->e
3b848 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c  Operator & (WO_L
3b849 45 7c 57 4f 5f 47 45 29 3b 0d 0a 20 20 20 20 65  E|WO_GE);..    e
3b84a 6e 64 45 71 20 3d 20 20 20 21 70 52 61 6e 67 65  ndEq =   !pRange
3b84b 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65 45 6e 64  End || pRangeEnd
3b84c 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57  ->eOperator & (W
3b84d 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0d 0a 20 20  O_LE|WO_GE);..  
3b84e 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69    start_constrai
3b84f 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74 61 72  nts = pRangeStar
3b850 74 20 7c 7c 20 6e 45 71 3e 30 3b 0d 0a 0d 0a 20  t || nEq>0;.... 
3b851 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69     /* Seek the i
3b852 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74  ndex cursor to t
3b853 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
3b854 72 61 6e 67 65 2e 20 2a 2f 0d 0a 20 20 20 20 6e  range. */..    n
3b855 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71  Constraint = nEq
3b856 3b 0d 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67  ;..    if( pRang
3b857 65 53 74 61 72 74 20 29 7b 0d 0a 20 20 20 20 20  eStart ){..     
3b858 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
3b859 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70 45 78  pRangeStart->pEx
3b85a 70 72 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20  pr->pRight;..   
3b85b 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
3b85c 64 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68  de(pParse, pRigh
3b85d 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  t, regBase+nEq);
3b85e 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70 52 61  ..      if( (pRa
3b85f 6e 67 65 53 74 61 72 74 2d 3e 77 74 46 6c 61 67  ngeStart->wtFlag
3b860 73 20 26 20 54 45 52 4d 5f 56 4e 55 4c 4c 29 3d  s & TERM_VNULL)=
3b861 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73  =0 ){..        s
3b862 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 73  qlite3ExprCodeIs
3b863 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20 70 52 69 67  NullJump(v, pRig
3b864 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 2c  ht, regBase+nEq,
3b865 20 61 64 64 72 4e 78 74 29 3b 0d 0a 20 20 20 20   addrNxt);..    
3b866 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 7a    }..      if( z
3b867 53 74 61 72 74 41 66 66 20 29 7b 0d 0a 20 20 20  StartAff ){..   
3b868 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
3b869 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
3b86a 70 52 69 67 68 74 2c 20 7a 53 74 61 72 74 41 66  pRight, zStartAf
3b86b 66 5b 6e 45 71 5d 29 3d 3d 53 51 4c 49 54 45 5f  f[nEq])==SQLITE_
3b86c 41 46 46 5f 4e 4f 4e 45 29 7b 0d 0a 20 20 20 20  AFF_NONE){..    
3b86d 20 20 20 20 20 20 2f 2a 20 53 69 6e 63 65 20 74        /* Since t
3b86e 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
3b86f 20 74 6f 20 62 65 20 70 65 72 66 6f 72 6d 65 64   to be performed
3b870 20 77 69 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73   with no convers
3b871 69 6f 6e 73 0d 0a 20 20 20 20 20 20 20 20 20 20  ions..          
3b872 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f 20 74 68  ** applied to th
3b873 65 20 6f 70 65 72 61 6e 64 73 2c 20 73 65 74 20  e operands, set 
3b874 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
3b875 61 70 70 6c 79 20 74 6f 20 70 52 69 67 68 74 20  apply to pRight 
3b876 74 6f 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  to ..          *
3b877 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
3b878 45 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  E.  */..        
3b879 20 20 7a 53 74 61 72 74 41 66 66 5b 6e 45 71 5d    zStartAff[nEq]
3b87a 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
3b87b 4e 45 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  NE;..        }..
3b87c 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
3b87d 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66  te3ExprNeedsNoAf
3b87e 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 52 69  finityChange(pRi
3b87f 67 68 74 2c 20 7a 53 74 61 72 74 41 66 66 5b 6e  ght, zStartAff[n
3b880 45 71 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  Eq]) ){..       
3b881 20 20 20 7a 53 74 61 72 74 41 66 66 5b 6e 45 71     zStartAff[nEq
3b882 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ] = SQLITE_AFF_N
3b883 4f 4e 45 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  ONE;..        }.
3b884 0a 20 20 20 20 20 20 7d 20 20 0d 0a 20 20 20 20  .      }  ..    
3b885 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
3b886 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3b887 28 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 77  ( pRangeStart->w
3b888 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49  tFlags & TERM_VI
3b889 52 54 55 41 4c 20 29 3b 20 2f 2a 20 45 56 3a 20  RTUAL ); /* EV: 
3b88a 52 2d 33 30 35 37 35 2d 31 31 36 36 32 20 2a 2f  R-30575-11662 */
3b88b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
3b88c 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0d 0a 20  isMinQuery ){.. 
3b88d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3b88e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
3b88f 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45  l, 0, regBase+nE
3b890 71 29 3b 0d 0a 20 20 20 20 20 20 6e 43 6f 6e 73  q);..      nCons
3b891 74 72 61 69 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20  traint++;..     
3b892 20 73 74 61 72 74 45 71 20 3d 20 30 3b 0d 0a 20   startEq = 0;.. 
3b893 20 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74       start_const
3b894 72 61 69 6e 74 73 20 3d 20 31 3b 0d 0a 20 20 20  raints = 1;..   
3b895 20 7d 0d 0a 20 20 20 20 63 6f 64 65 41 70 70 6c   }..    codeAppl
3b896 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65  yAffinity(pParse
3b897 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73  , regBase, nCons
3b898 74 72 61 69 6e 74 2c 20 7a 53 74 61 72 74 41 66  traint, zStartAf
3b899 66 29 3b 0d 0a 20 20 20 20 6f 70 20 3d 20 61 53  f);..    op = aS
3b89a 74 61 72 74 4f 70 5b 28 73 74 61 72 74 5f 63 6f  tartOp[(start_co
3b89b 6e 73 74 72 61 69 6e 74 73 3c 3c 32 29 20 2b 20  nstraints<<2) + 
3b89c 28 73 74 61 72 74 45 71 3c 3c 31 29 20 2b 20 62  (startEq<<1) + b
3b89d 52 65 76 5d 3b 0d 0a 20 20 20 20 61 73 73 65 72  Rev];..    asser
3b89e 74 28 20 6f 70 21 3d 30 20 29 3b 0d 0a 20 20 20  t( op!=0 );..   
3b89f 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
3b8a0 50 5f 52 65 77 69 6e 64 20 29 3b 0d 0a 20 20 20  P_Rewind );..   
3b8a1 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
3b8a2 50 5f 4c 61 73 74 20 29 3b 0d 0a 20 20 20 20 74  P_Last );..    t
3b8a3 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
3b8a4 53 65 65 6b 47 74 20 29 3b 0d 0a 20 20 20 20 74  SeekGt );..    t
3b8a5 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
3b8a6 53 65 65 6b 47 65 20 29 3b 0d 0a 20 20 20 20 74  SeekGe );..    t
3b8a7 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
3b8a8 53 65 65 6b 4c 65 20 29 3b 0d 0a 20 20 20 20 74  SeekLe );..    t
3b8a9 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
3b8aa 53 65 65 6b 4c 74 20 29 3b 0d 0a 20 20 20 20 73  SeekLt );..    s
3b8ab 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
3b8ac 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64 78 43  Int(v, op, iIdxC
3b8ad 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72 65 67  ur, addrNxt, reg
3b8ae 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  Base, nConstrain
3b8af 74 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4c 6f  t);....    /* Lo
3b8b0 61 64 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72  ad the value for
3b8b1 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20   the inequality 
3b8b2 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 74 68  constraint at th
3b8b3 65 20 65 6e 64 20 6f 66 20 74 68 65 0d 0a 20 20  e end of the..  
3b8b4 20 20 2a 2a 20 72 61 6e 67 65 20 28 69 66 20 61    ** range (if a
3b8b5 6e 79 29 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ny)...    */..  
3b8b6 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20    nConstraint = 
3b8b7 6e 45 71 3b 0d 0a 20 20 20 20 69 66 28 20 70 52  nEq;..    if( pR
3b8b8 61 6e 67 65 45 6e 64 20 29 7b 0d 0a 20 20 20 20  angeEnd ){..    
3b8b9 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d    Expr *pRight =
3b8ba 20 70 52 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70   pRangeEnd->pExp
3b8bb 72 2d 3e 70 52 69 67 68 74 3b 0d 0a 20 20 20 20  r->pRight;..    
3b8bc 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3b8bd 68 65 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c  heRemove(pParse,
3b8be 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 31 29   regBase+nEq, 1)
3b8bf 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3b8c0 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
3b8c1 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65   pRight, regBase
3b8c2 2b 6e 45 71 29 3b 0d 0a 20 20 20 20 20 20 69 66  +nEq);..      if
3b8c3 28 20 28 70 52 61 6e 67 65 45 6e 64 2d 3e 77 74  ( (pRangeEnd->wt
3b8c4 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e 55  Flags & TERM_VNU
3b8c5 4c 4c 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  LL)==0 ){..     
3b8c6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
3b8c7 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20  deIsNullJump(v, 
3b8c8 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
3b8c9 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0d 0a  nEq, addrNxt);..
3b8ca 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69        }..      i
3b8cb 66 28 20 7a 45 6e 64 41 66 66 20 29 7b 0d 0a 20  f( zEndAff ){.. 
3b8cc 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
3b8cd 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
3b8ce 79 28 70 52 69 67 68 74 2c 20 7a 45 6e 64 41 66  y(pRight, zEndAf
3b8cf 66 5b 6e 45 71 5d 29 3d 3d 53 51 4c 49 54 45 5f  f[nEq])==SQLITE_
3b8d0 41 46 46 5f 4e 4f 4e 45 29 7b 0d 0a 20 20 20 20  AFF_NONE){..    
3b8d1 20 20 20 20 20 20 2f 2a 20 53 69 6e 63 65 20 74        /* Since t
3b8d2 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
3b8d3 20 74 6f 20 62 65 20 70 65 72 66 6f 72 6d 65 64   to be performed
3b8d4 20 77 69 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73   with no convers
3b8d5 69 6f 6e 73 0d 0a 20 20 20 20 20 20 20 20 20 20  ions..          
3b8d6 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f 20 74 68  ** applied to th
3b8d7 65 20 6f 70 65 72 61 6e 64 73 2c 20 73 65 74 20  e operands, set 
3b8d8 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
3b8d9 61 70 70 6c 79 20 74 6f 20 70 52 69 67 68 74 20  apply to pRight 
3b8da 74 6f 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  to ..          *
3b8db 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
3b8dc 45 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  E.  */..        
3b8dd 20 20 7a 45 6e 64 41 66 66 5b 6e 45 71 5d 20 3d    zEndAff[nEq] =
3b8de 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
3b8df 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
3b8e0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
3b8e1 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69  3ExprNeedsNoAffi
3b8e2 6e 69 74 79 43 68 61 6e 67 65 28 70 52 69 67 68  nityChange(pRigh
3b8e3 74 2c 20 7a 45 6e 64 41 66 66 5b 6e 45 71 5d 29  t, zEndAff[nEq])
3b8e4 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a   ){..          z
3b8e5 45 6e 64 41 66 66 5b 6e 45 71 5d 20 3d 20 53 51  EndAff[nEq] = SQ
3b8e6 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0d 0a  LITE_AFF_NONE;..
3b8e7 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3b8e8 20 7d 20 20 0d 0a 20 20 20 20 20 20 63 6f 64 65   }  ..      code
3b8e9 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50  ApplyAffinity(pP
3b8ea 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
3b8eb 45 71 2b 31 2c 20 7a 45 6e 64 41 66 66 29 3b 0d  Eq+1, zEndAff);.
3b8ec 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69  .      nConstrai
3b8ed 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 74 65 73  nt++;..      tes
3b8ee 74 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64  tcase( pRangeEnd
3b8ef 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
3b8f0 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a 20 45  _VIRTUAL ); /* E
3b8f1 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36 36 32  V: R-30575-11662
3b8f2 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   */..    }..    
3b8f3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
3b8f4 61 72 73 65 2d 3e 64 62 2c 20 7a 53 74 61 72 74  arse->db, zStart
3b8f5 41 66 66 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Aff);..    sqlit
3b8f6 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d  e3DbFree(pParse-
3b8f7 3e 64 62 2c 20 7a 45 6e 64 41 66 66 29 3b 0d 0a  >db, zEndAff);..
3b8f8 0d 0a 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20  ..    /* Top of 
3b8f9 74 68 65 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f  the loop body */
3b8fa 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32  ..    pLevel->p2
3b8fb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
3b8fc 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0d 0a 0d  rrentAddr(v);...
3b8fd 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  .    /* Check if
3b8fe 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f   the index curso
3b8ff 72 20 69 73 20 70 61 73 74 20 74 68 65 20 65 6e  r is past the en
3b900 64 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20  d of the range. 
3b901 2a 2f 0d 0a 20 20 20 20 6f 70 20 3d 20 61 45 6e  */..    op = aEn
3b902 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64 20 7c  dOp[(pRangeEnd |
3b903 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20 62 52  | nEq) * (1 + bR
3b904 65 76 29 5d 3b 0d 0a 20 20 20 20 74 65 73 74 63  ev)];..    testc
3b905 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70  ase( op==OP_Noop
3b906 20 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73   );..    testcas
3b907 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20  e( op==OP_IdxGE 
3b908 29 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65  );..    testcase
3b909 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29  ( op==OP_IdxLT )
3b90a 3b 0d 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 4f  ;..    if( op!=O
3b90b 50 5f 4e 6f 6f 70 20 29 7b 0d 0a 20 20 20 20 20  P_Noop ){..     
3b90c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3b90d 70 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64  p4Int(v, op, iId
3b90e 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72  xCur, addrNxt, r
3b90f 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61  egBase, nConstra
3b910 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  int);..      sql
3b911 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
3b912 28 76 2c 20 65 6e 64 45 71 21 3d 62 52 65 76 20  (v, endEq!=bRev 
3b913 3f 31 3a 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  ?1:0);..    }...
3b914 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
3b915 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20   are inequality 
3b916 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63 68 65  constraints, che
3b917 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  ck that the valu
3b918 65 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  e..    ** of the
3b919 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68   table column th
3b91a 61 74 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74  at the inequalit
3b91b 79 20 63 6f 6e 74 72 61 69 6e 73 20 69 73 20 6e  y contrains is n
3b91c 6f 74 20 4e 55 4c 4c 2e 0d 0a 20 20 20 20 2a 2a  ot NULL...    **
3b91d 20 49 66 20 69 74 20 69 73 2c 20 6a 75 6d 70 20   If it is, jump 
3b91e 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
3b91f 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
3b920 70 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  p...    */..    
3b921 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
3b922 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0d  empReg(pParse);.
3b923 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
3b924 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3b925 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f  ags & WHERE_BTM_
3b926 4c 49 4d 49 54 20 29 3b 0d 0a 20 20 20 20 74 65  LIMIT );..    te
3b927 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e  stcase( pLevel->
3b928 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3b929 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29  HERE_TOP_LIMIT )
3b92a 3b 0d 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76  ;..    if( (pLev
3b92b 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3b92c 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f 4c 49   & (WHERE_BTM_LI
3b92d 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c 49  MIT|WHERE_TOP_LI
3b92e 4d 49 54 29 29 21 3d 30 20 29 7b 0d 0a 20 20 20  MIT))!=0 ){..   
3b92f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3b930 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
3b931 6e 2c 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c  n, iIdxCur, nEq,
3b932 20 72 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c   r1);..      sql
3b933 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3b934 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c  , OP_IsNull, r1,
3b935 20 61 64 64 72 43 6f 6e 74 29 3b 0d 0a 20 20 20   addrCont);..   
3b936 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 52   }..    sqlite3R
3b937 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
3b938 61 72 73 65 2c 20 72 31 29 3b 0d 0a 0d 0a 20 20  arse, r1);....  
3b939 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 74 61    /* Seek the ta
3b93a 62 6c 65 20 63 75 72 73 6f 72 2c 20 69 66 20 72  ble cursor, if r
3b93b 65 71 75 69 72 65 64 20 2a 2f 0d 0a 20 20 20 20  equired */..    
3b93c 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
3b93d 65 6c 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29  el, pRangeStart)
3b93e 3b 0d 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  ;..    disableTe
3b93f 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67  rm(pLevel, pRang
3b940 65 45 6e 64 29 3b 0d 0a 20 20 20 20 69 66 28 20  eEnd);..    if( 
3b941 21 6f 6d 69 74 54 61 62 6c 65 20 29 7b 0d 0a 20  !omitTable ){.. 
3b942 20 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d       iRowidReg =
3b943 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73   iReleaseReg = s
3b944 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
3b945 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20  (pParse);..     
3b946 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3b947 70 32 28 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69  p2(v, OP_IdxRowi
3b948 64 2c 20 69 49 64 78 43 75 72 2c 20 69 52 6f 77  d, iIdxCur, iRow
3b949 69 64 52 65 67 29 3b 0d 0a 20 20 20 20 20 20 73  idReg);..      s
3b94a 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53  qlite3ExprCacheS
3b94b 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 43 75  tore(pParse, iCu
3b94c 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52 65 67  r, -1, iRowidReg
3b94d 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
3b94e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3b94f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69 52  P_Seek, iCur, iR
3b950 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20 44 65  owidReg);  /* De
3b951 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0d 0a  ferred seek */..
3b952 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
3b953 52 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72  Record the instr
3b954 75 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74  uction used to t
3b955 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f  erminate the loo
3b956 70 2e 20 44 69 73 61 62 6c 65 20 0d 0a 20 20 20  p. Disable ..   
3b957 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   ** WHERE clause
3b958 20 74 65 72 6d 73 20 6d 61 64 65 20 72 65 64 75   terms made redu
3b959 6e 64 61 6e 74 20 62 79 20 74 68 65 20 69 6e 64  ndant by the ind
3b95a 65 78 20 72 61 6e 67 65 20 73 63 61 6e 2e 0d 0a  ex range scan...
3b95b 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
3b95c 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3b95d 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55 4e 49  lags & WHERE_UNI
3b95e 51 55 45 20 29 7b 0d 0a 20 20 20 20 20 20 70 4c  QUE ){..      pL
3b95f 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f  evel->op = OP_No
3b960 6f 70 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69  op;..    }else i
3b961 66 28 20 62 52 65 76 20 29 7b 0d 0a 20 20 20 20  f( bRev ){..    
3b962 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f    pLevel->op = O
3b963 50 5f 50 72 65 76 3b 0d 0a 20 20 20 20 7d 65 6c  P_Prev;..    }el
3b964 73 65 7b 0d 0a 20 20 20 20 20 20 70 4c 65 76 65  se{..      pLeve
3b965 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 65 78 74 3b  l->op = OP_Next;
3b966 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 4c 65  ..    }..    pLe
3b967 76 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75  vel->p1 = iIdxCu
3b968 72 3b 0d 0a 20 20 7d 65 6c 73 65 0d 0a 0d 0a 23  r;..  }else....#
3b969 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3b96a 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
3b96b 4f 4e 0d 0a 20 20 69 66 28 20 70 4c 65 76 65 6c  ON..  if( pLevel
3b96c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3b96d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
3b96e 29 7b 0d 0a 20 20 20 20 2f 2a 20 43 61 73 65 20  ){..    /* Case 
3b96f 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20  4:  Two or more 
3b970 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78  separately index
3b971 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  ed terms connect
3b972 65 64 20 62 79 20 4f 52 0d 0a 20 20 20 20 2a 2a  ed by OR..    **
3b973 0d 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65  ..    ** Example
3b974 3a 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  :..    **..    *
3b975 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
3b976 20 74 31 28 61 2c 62 2c 63 2c 64 29 3b 0d 0a 20   t1(a,b,c,d);.. 
3b977 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49     **   CREATE I
3b978 4e 44 45 58 20 69 31 20 4f 4e 20 74 31 28 61 29  NDEX i1 ON t1(a)
3b979 3b 0d 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41  ;..    **   CREA
3b97a 54 45 20 49 4e 44 45 58 20 69 32 20 4f 4e 20 74  TE INDEX i2 ON t
3b97b 31 28 62 29 3b 0d 0a 20 20 20 20 2a 2a 20 20 20  1(b);..    **   
3b97c 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 33 20  CREATE INDEX i3 
3b97d 4f 4e 20 74 31 28 63 29 3b 0d 0a 20 20 20 20 2a  ON t1(c);..    *
3b97e 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45  *..    **   SELE
3b97f 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
3b980 52 45 20 61 3d 35 20 4f 52 20 62 3d 37 20 4f 52  RE a=5 OR b=7 OR
3b981 20 28 63 3d 31 31 20 41 4e 44 20 64 3d 31 33 29   (c=11 AND d=13)
3b982 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
3b983 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 2c   In the example,
3b984 20 74 68 65 72 65 20 61 72 65 20 74 68 72 65 65   there are three
3b985 20 69 6e 64 65 78 65 64 20 74 65 72 6d 73 20 63   indexed terms c
3b986 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 2e 0d  onnected by OR..
3b987 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 6f 70 20  .    ** The top 
3b988 6f 66 20 74 68 65 20 6c 6f 6f 70 20 6c 6f 6f 6b  of the loop look
3b989 73 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a 20 20  s like this:..  
3b98a 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 20    **..    **    
3b98b 20 20 20 20 20 20 4e 75 6c 6c 20 20 20 20 20 20        Null      
3b98c 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20   1              
3b98d 20 20 23 20 5a 65 72 6f 20 74 68 65 20 72 6f 77    # Zero the row
3b98e 73 65 74 20 69 6e 20 72 65 67 20 31 0d 0a 20 20  set in reg 1..  
3b98f 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65    **..    ** The
3b990 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  n, for each inde
3b991 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66 6f  xed term, the fo
3b992 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72 67  llowing. The arg
3b993 75 6d 65 6e 74 73 20 74 6f 0d 0a 20 20 20 20 2a  uments to..    *
3b994 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65  * RowSetTest are
3b995 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72   such that the r
3b996 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72  owid of the curr
3b997 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72  ent row is inser
3b998 74 65 64 0d 0a 20 20 20 20 2a 2a 20 69 6e 74 6f  ted..    ** into
3b999 20 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20   the RowSet. If 
3b99a 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 70 72  it is already pr
3b99b 65 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73  esent, control s
3b99c 6b 69 70 73 20 74 68 65 0d 0a 20 20 20 20 2a 2a  kips the..    **
3b99d 20 47 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e   Gosub opcode an
3b99e 64 20 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74  d jumps straight
3b99f 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
3b9a0 65 72 61 74 65 64 20 62 79 20 57 68 65 72 65 45  erated by WhereE
3b9a1 6e 64 28 29 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  nd()...    **.. 
3b9a2 20 20 20 2a 2a 20 20 20 20 20 20 20 20 73 71 6c     **        sql
3b9a3 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 3c  ite3WhereBegin(<
3b9a4 74 65 72 6d 3e 29 0d 0a 20 20 20 20 2a 2a 20 20  term>)..    **  
3b9a5 20 20 20 20 20 20 20 20 52 6f 77 53 65 74 54 65          RowSetTe
3b9a6 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
3b9a7 20 20 20 20 23 20 49 6e 73 65 72 74 20 72 6f 77      # Insert row
3b9a8 69 64 20 69 6e 74 6f 20 72 6f 77 73 65 74 0d 0a  id into rowset..
3b9a9 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
3b9aa 47 6f 73 75 62 20 20 20 20 20 20 32 20 41 0d 0a  Gosub      2 A..
3b9ab 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 73 71      **        sq
3b9ac 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 0d  lite3WhereEnd().
3b9ad 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3b9ae 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 61 62  Following the ab
3b9af 6f 76 65 2c 20 63 6f 64 65 20 74 6f 20 74 65 72  ove, code to ter
3b9b0 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
3b9b1 20 4c 61 62 65 6c 20 41 2c 20 74 68 65 20 74 61   Label A, the ta
3b9b2 72 67 65 74 0d 0a 20 20 20 20 2a 2a 20 6f 66 20  rget..    ** of 
3b9b3 74 68 65 20 47 6f 73 75 62 20 61 62 6f 76 65 2c  the Gosub above,
3b9b4 20 6a 75 6d 70 73 20 74 6f 20 74 68 65 20 69 6e   jumps to the in
3b9b5 73 74 72 75 63 74 69 6f 6e 20 72 69 67 68 74 20  struction right 
3b9b6 61 66 74 65 72 20 74 68 65 20 47 6f 74 6f 2e 0d  after the Goto..
3b9b7 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20  .    **..    ** 
3b9b8 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20           Null   
3b9b9 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
3b9ba 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20       # Zero the 
3b9bb 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0d  rowset in reg 1.
3b9bc 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3b9bd 20 47 6f 74 6f 20 20 20 20 20 20 20 42 20 20 20   Goto       B   
3b9be 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20 54               # T
3b9bf 68 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e 69 73  he loop is finis
3b9c0 68 65 64 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  hed...    **..  
3b9c1 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c    **       A: <l
3b9c2 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20 20  oop body>       
3b9c3 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74 75            # Retu
3b9c4 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76 65  rn data, whateve
3b9c5 72 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  r...    **..    
3b9c6 2a 2a 20 20 20 20 20 20 20 20 20 20 52 65 74 75  **          Retu
3b9c7 72 6e 20 20 20 20 20 32 20 20 20 20 20 20 20 20  rn     2        
3b9c8 20 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62          # Jump b
3b9c9 61 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62  ack to the Gosub
3b9ca 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a  ..    **..    **
3b9cb 20 20 20 20 20 20 20 42 3a 20 3c 61 66 74 65 72         B: <after
3b9cc 20 74 68 65 20 6c 6f 6f 70 3e 0d 0a 20 20 20 20   the loop>..    
3b9cd 2a 2a 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  **..    */..    
3b9ce 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72  WhereClause *pOr
3b9cf 57 63 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  Wc;    /* The OR
3b9d0 2d 63 6c 61 75 73 65 20 62 72 6f 6b 65 6e 20 6f  -clause broken o
3b9d1 75 74 20 69 6e 74 6f 20 73 75 62 74 65 72 6d 73  ut into subterms
3b9d2 20 2a 2f 0d 0a 20 20 20 20 53 72 63 4c 69 73 74   */..    SrcList
3b9d3 20 2a 70 4f 72 54 61 62 3b 20 20 20 20 20 20 20   *pOrTab;       
3b9d4 2f 2a 20 53 68 6f 72 74 65 6e 65 64 20 74 61 62  /* Shortened tab
3b9d5 6c 65 20 6c 69 73 74 20 6f 72 20 4f 52 2d 63 6c  le list or OR-cl
3b9d6 61 75 73 65 20 67 65 6e 65 72 61 74 69 6f 6e 20  ause generation 
3b9d7 2a 2f 0d 0a 0d 0a 20 20 20 20 69 6e 74 20 72 65  */....    int re
3b9d8 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72  gReturn = ++pPar
3b9d9 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 20 20 20  se->nMem;       
3b9da 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
3b9db 75 73 65 64 20 77 69 74 68 20 4f 50 5f 47 6f 73  used with OP_Gos
3b9dc 75 62 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 72  ub */..    int r
3b9dd 65 67 52 6f 77 73 65 74 20 3d 20 30 3b 20 20 20  egRowset = 0;   
3b9de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b9df 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
3b9e0 20 66 6f 72 20 52 6f 77 53 65 74 20 6f 62 6a 65   for RowSet obje
3b9e1 63 74 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 72  ct */..    int r
3b9e2 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20 20 20  egRowid = 0;    
3b9e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b9e4 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
3b9e5 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20 2a   holding rowid *
3b9e6 2f 0d 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70  /..    int iLoop
3b9e7 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64  Body = sqlite3Vd
3b9e8 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20  beMakeLabel(v); 
3b9e9 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f   /* Start of loo
3b9ea 70 20 62 6f 64 79 20 2a 2f 0d 0a 20 20 20 20 69  p body */..    i
3b9eb 6e 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20  nt iRetInit;    
3b9ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b9ed 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
3b9ee 65 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e  ess of regReturn
3b9ef 20 69 6e 69 74 20 2a 2f 0d 0a 20 20 20 20 69 6e   init */..    in
3b9f0 74 20 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20  t untestedTerms 
3b9f1 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
3b9f2 20 2f 2a 20 53 6f 6d 65 20 74 65 72 6d 73 20 6e   /* Some terms n
3b9f3 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 74 65  ot completely te
3b9f4 73 74 65 64 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  sted */..    int
3b9f5 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
3b9f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b9f7 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
3b9f8 2a 2f 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 41  */..    Expr *pA
3b9f9 6e 64 45 78 70 72 20 3d 20 30 3b 20 20 20 20 20  ndExpr = 0;     
3b9fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
3b9fb 20 22 2e 2e 20 41 4e 44 20 28 2e 2e 2e 29 22 20   ".. AND (...)" 
3b9fc 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20  expression */.. 
3b9fd 20 20 0d 0a 20 20 20 20 70 54 65 72 6d 20 3d 20    ..    pTerm = 
3b9fe 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
3b9ff 54 65 72 6d 3b 0d 0a 20 20 20 20 61 73 73 65 72  Term;..    asser
3ba00 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0d 0a  t( pTerm!=0 );..
3ba01 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
3ba02 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
3ba03 5f 4f 52 20 29 3b 0d 0a 20 20 20 20 61 73 73 65  _OR );..    asse
3ba04 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  rt( (pTerm->wtFl
3ba05 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46  ags & TERM_ORINF
3ba06 4f 29 21 3d 30 20 29 3b 0d 0a 20 20 20 20 70 4f  O)!=0 );..    pO
3ba07 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e  rWc = &pTerm->u.
3ba08 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0d 0a 20 20  pOrInfo->wc;..  
3ba09 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f    pLevel->op = O
3ba0a 50 5f 52 65 74 75 72 6e 3b 0d 0a 20 20 20 20 70  P_Return;..    p
3ba0b 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52  Level->p1 = regR
3ba0c 65 74 75 72 6e 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  eturn;....    /*
3ba0d 20 53 65 74 20 75 70 20 61 20 6e 65 77 20 53 72   Set up a new Sr
3ba0e 63 4c 69 73 74 20 6e 69 20 70 4f 72 54 61 62 20  cList ni pOrTab 
3ba0f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
3ba10 61 62 6c 65 20 62 65 69 6e 67 20 73 63 61 6e 6e  able being scann
3ba11 65 64 0d 0a 20 20 20 20 2a 2a 20 62 79 20 74 68  ed..    ** by th
3ba12 69 73 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 61  is loop in the a
3ba13 5b 30 5d 20 73 6c 6f 74 20 61 6e 64 20 61 6c 6c  [0] slot and all
3ba14 20 6e 6f 74 52 65 61 64 79 20 74 61 62 6c 65 73   notReady tables
3ba15 20 69 6e 20 61 5b 31 2e 2e 5d 20 73 6c 6f 74 73   in a[1..] slots
3ba16 2e 0d 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62  ...    ** This b
3ba17 65 63 6f 6d 65 73 20 74 68 65 20 53 72 63 4c 69  ecomes the SrcLi
3ba18 73 74 20 69 6e 20 74 68 65 20 72 65 63 75 72 73  st in the recurs
3ba19 69 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ive call to sqli
3ba1a 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e  te3WhereBegin().
3ba1b 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66  ..    */..    if
3ba1c 28 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c  ( pWInfo->nLevel
3ba1d 3e 31 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74  >1 ){..      int
3ba1e 20 6e 4e 6f 74 52 65 61 64 79 3b 20 20 20 20 20   nNotReady;     
3ba1f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3ba20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 74  he number of not
3ba21 52 65 61 64 79 20 74 61 62 6c 65 73 20 2a 2f 0d  Ready tables */.
3ba22 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
3ba23 63 4c 69 73 74 5f 69 74 65 6d 20 2a 6f 72 69 67  cList_item *orig
3ba24 53 72 63 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  Src;     /* Orig
3ba25 69 6e 61 6c 20 6c 69 73 74 20 6f 66 20 74 61 62  inal list of tab
3ba26 6c 65 73 20 2a 2f 0d 0a 20 20 20 20 20 20 6e 4e  les */..      nN
3ba27 6f 74 52 65 61 64 79 20 3d 20 70 57 49 6e 66 6f  otReady = pWInfo
3ba28 2d 3e 6e 4c 65 76 65 6c 20 2d 20 69 4c 65 76 65  ->nLevel - iLeve
3ba29 6c 20 2d 20 31 3b 0d 0a 20 20 20 20 20 20 70 4f  l - 1;..      pO
3ba2a 72 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 74  rTab = sqlite3St
3ba2b 61 63 6b 41 6c 6c 6f 63 52 61 77 28 70 50 61 72  ackAllocRaw(pPar
3ba2c 73 65 2d 3e 64 62 2c 0d 0a 20 20 20 20 20 20 20  se->db,..       
3ba2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ba2e 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4f 72       sizeof(*pOr
3ba2f 54 61 62 29 2b 20 6e 4e 6f 74 52 65 61 64 79 2a  Tab)+ nNotReady*
3ba30 73 69 7a 65 6f 66 28 70 4f 72 54 61 62 2d 3e 61  sizeof(pOrTab->a
3ba31 5b 30 5d 29 29 3b 0d 0a 20 20 20 20 20 20 69 66  [0]));..      if
3ba32 28 20 70 4f 72 54 61 62 3d 3d 30 20 29 20 72 65  ( pOrTab==0 ) re
3ba33 74 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0d 0a  turn notReady;..
3ba34 20 20 20 20 20 20 70 4f 72 54 61 62 2d 3e 6e 41        pOrTab->nA
3ba35 6c 6c 6f 63 20 3d 20 28 69 31 36 29 28 6e 4e 6f  lloc = (i16)(nNo
3ba36 74 52 65 61 64 79 20 2b 20 31 29 3b 0d 0a 20 20  tReady + 1);..  
3ba37 20 20 20 20 70 4f 72 54 61 62 2d 3e 6e 53 72 63      pOrTab->nSrc
3ba38 20 3d 20 70 4f 72 54 61 62 2d 3e 6e 41 6c 6c 6f   = pOrTab->nAllo
3ba39 63 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  c;..      memcpy
3ba3a 28 70 4f 72 54 61 62 2d 3e 61 2c 20 70 54 61 62  (pOrTab->a, pTab
3ba3b 49 74 65 6d 2c 20 73 69 7a 65 6f 66 28 2a 70 54  Item, sizeof(*pT
3ba3c 61 62 49 74 65 6d 29 29 3b 0d 0a 20 20 20 20 20  abItem));..     
3ba3d 20 6f 72 69 67 53 72 63 20 3d 20 70 57 49 6e 66   origSrc = pWInf
3ba3e 6f 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0d  o->pTabList->a;.
3ba3f 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 31 3b 20  .      for(k=1; 
3ba40 6b 3c 3d 6e 4e 6f 74 52 65 61 64 79 3b 20 6b 2b  k<=nNotReady; k+
3ba41 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d  +){..        mem
3ba42 63 70 79 28 26 70 4f 72 54 61 62 2d 3e 61 5b 6b  cpy(&pOrTab->a[k
3ba43 5d 2c 20 26 6f 72 69 67 53 72 63 5b 70 4c 65 76  ], &origSrc[pLev
3ba44 65 6c 5b 6b 5d 2e 69 46 72 6f 6d 5d 2c 20 73 69  el[k].iFrom], si
3ba45 7a 65 6f 66 28 70 4f 72 54 61 62 2d 3e 61 5b 6b  zeof(pOrTab->a[k
3ba46 5d 29 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ]));..      }.. 
3ba47 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
3ba48 20 70 4f 72 54 61 62 20 3d 20 70 57 49 6e 66 6f   pOrTab = pWInfo
3ba49 2d 3e 70 54 61 62 4c 69 73 74 3b 0d 0a 20 20 20  ->pTabList;..   
3ba4a 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 6e 69   }....    /* Ini
3ba4b 74 69 61 6c 69 7a 65 20 74 68 65 20 72 6f 77 73  tialize the rows
3ba4c 65 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 63  et register to c
3ba4d 6f 6e 74 61 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20  ontain NULL. An 
3ba4e 53 51 4c 20 4e 55 4c 4c 20 69 73 20 0d 0a 20 20  SQL NULL is ..  
3ba4f 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20    ** equivalent 
3ba50 74 6f 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73  to an empty rows
3ba51 65 74 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  et...    **..   
3ba52 20 2a 2a 20 41 6c 73 6f 20 69 6e 69 74 69 61 6c   ** Also initial
3ba53 69 7a 65 20 72 65 67 52 65 74 75 72 6e 20 74 6f  ize regReturn to
3ba54 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 61 64 64   contain the add
3ba55 72 65 73 73 20 6f 66 20 74 68 65 20 69 6e 73 74  ress of the inst
3ba56 72 75 63 74 69 6f 6e 20 0d 0a 20 20 20 20 2a 2a  ruction ..    **
3ba57 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c   immediately fol
3ba58 6c 6f 77 69 6e 67 20 74 68 65 20 4f 50 5f 52 65  lowing the OP_Re
3ba59 74 75 72 6e 20 61 74 20 74 68 65 20 62 6f 74 74  turn at the bott
3ba5a 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20  om of the loop. 
3ba5b 54 68 69 73 0d 0a 20 20 20 20 2a 2a 20 69 73 20  This..    ** is 
3ba5c 72 65 71 75 69 72 65 64 20 69 6e 20 61 20 66 65  required in a fe
3ba5d 77 20 6f 62 73 63 75 72 65 20 4c 45 46 54 20 4a  w obscure LEFT J
3ba5e 4f 49 4e 20 63 61 73 65 73 20 77 68 65 72 65 20  OIN cases where 
3ba5f 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0d 0a 20  control jumps.. 
3ba60 20 20 20 2a 2a 20 6f 76 65 72 20 74 68 65 20 74     ** over the t
3ba61 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69  op of the loop i
3ba62 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20  nto the body of 
3ba63 69 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  it. In this case
3ba64 20 74 68 65 20 0d 0a 20 20 20 20 2a 2a 20 63 6f   the ..    ** co
3ba65 72 72 65 63 74 20 72 65 73 70 6f 6e 73 65 20 66  rrect response f
3ba66 6f 72 20 74 68 65 20 65 6e 64 2d 6f 66 2d 6c 6f  or the end-of-lo
3ba67 6f 70 20 63 6f 64 65 20 28 74 68 65 20 4f 50 5f  op code (the OP_
3ba68 52 65 74 75 72 6e 29 20 69 73 20 74 6f 20 0d 0a  Return) is to ..
3ba69 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74 68 72 6f      ** fall thro
3ba6a 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
3ba6b 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 6a 75 73  instruction, jus
3ba6c 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65 78 74 20  t as an OP_Next 
3ba6d 64 6f 65 73 20 69 66 0d 0a 20 20 20 20 2a 2a 20  does if..    ** 
3ba6e 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75 6e 69  called on an uni
3ba6f 6e 69 74 69 61 6c 69 7a 65 64 20 63 75 72 73 6f  nitialized curso
3ba70 72 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  r...    */..    
3ba71 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  if( (wctrlFlags 
3ba72 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  & WHERE_DUPLICAT
3ba73 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0d 0a 20 20  ES_OK)==0 ){..  
3ba74 20 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20      regRowset = 
3ba75 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0d  ++pParse->nMem;.
3ba76 0a 20 20 20 20 20 20 72 65 67 52 6f 77 69 64 20  .      regRowid 
3ba77 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
3ba78 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3ba79 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3ba7a 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77  _Null, 0, regRow
3ba7b 73 65 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  set);..    }..  
3ba7c 20 20 69 52 65 74 49 6e 69 74 20 3d 20 73 71 6c    iRetInit = sql
3ba7d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3ba7e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
3ba7f 20 72 65 67 52 65 74 75 72 6e 29 3b 0d 0a 0d 0a   regReturn);....
3ba80 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 72      /* If the or
3ba81 69 67 69 6e 61 6c 20 57 48 45 52 45 20 63 6c 61  iginal WHERE cla
3ba82 75 73 65 20 69 73 20 7a 20 6f 66 20 74 68 65 20  use is z of the 
3ba83 66 6f 72 6d 3a 20 20 28 78 31 20 4f 52 20 78 32  form:  (x1 OR x2
3ba84 20 4f 52 20 2e 2e 2e 29 20 41 4e 44 20 79 0d 0a   OR ...) AND y..
3ba85 20 20 20 20 2a 2a 20 54 68 65 6e 20 66 6f 72 20      ** Then for 
3ba86 65 76 65 72 79 20 74 65 72 6d 20 78 4e 2c 20 65  every term xN, e
3ba87 76 61 6c 75 61 74 65 20 61 73 20 74 68 65 20 73  valuate as the s
3ba88 75 62 65 78 70 72 65 73 73 69 6f 6e 3a 20 78 4e  ubexpression: xN
3ba89 20 41 4e 44 20 7a 0d 0a 20 20 20 20 2a 2a 20 54   AND z..    ** T
3ba8a 68 61 74 20 77 61 79 2c 20 74 65 72 6d 73 20 69  hat way, terms i
3ba8b 6e 20 79 20 74 68 61 74 20 61 72 65 20 66 61 63  n y that are fac
3ba8c 74 6f 72 65 64 20 69 6e 74 6f 20 74 68 65 20 64  tored into the d
3ba8d 69 73 6a 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 0d  isjunction will.
3ba8e 0a 20 20 20 20 2a 2a 20 62 65 20 70 69 63 6b 65  .    ** be picke
3ba8f 64 20 75 70 20 62 79 20 74 68 65 20 72 65 63 75  d up by the recu
3ba90 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 73  rsive calls to s
3ba91 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
3ba92 28 29 20 62 65 6c 6f 77 2e 0d 0a 20 20 20 20 2a  () below...    *
3ba93 2f 0d 0a 20 20 20 20 69 66 28 20 70 57 43 2d 3e  /..    if( pWC->
3ba94 6e 54 65 72 6d 3e 31 20 29 7b 0d 0a 20 20 20 20  nTerm>1 ){..    
3ba95 20 20 70 41 6e 64 45 78 70 72 20 3d 20 73 71 6c    pAndExpr = sql
3ba96 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 70 50  ite3ExprAlloc(pP
3ba97 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 41 4e 44  arse->db, TK_AND
3ba98 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  , 0, 0);..      
3ba99 70 41 6e 64 45 78 70 72 2d 3e 70 52 69 67 68 74  pAndExpr->pRight
3ba9a 20 3d 20 70 57 68 65 72 65 3b 0d 0a 20 20 20 20   = pWhere;..    
3ba9b 7d 0d 0a 0d 0a 20 20 20 20 66 6f 72 28 69 69 3d  }....    for(ii=
3ba9c 30 3b 20 69 69 3c 70 4f 72 57 63 2d 3e 6e 54 65  0; ii<pOrWc->nTe
3ba9d 72 6d 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20  rm; ii++){..    
3ba9e 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72    WhereTerm *pOr
3ba9f 54 65 72 6d 20 3d 20 26 70 4f 72 57 63 2d 3e 61  Term = &pOrWc->a
3baa0 5b 69 69 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28  [ii];..      if(
3baa1 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75   pOrTerm->leftCu
3baa2 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20 70 4f  rsor==iCur || pO
3baa3 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
3baa4 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0d 0a 20 20 20  ==WO_AND ){..   
3baa5 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f 20 2a       WhereInfo *
3baa6 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20 20 20  pSubWInfo;      
3baa7 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72 20      /* Info for 
3baa8 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20 73  single OR-term s
3baa9 63 61 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  can */..        
3baaa 45 78 70 72 20 2a 70 4f 72 45 78 70 72 20 3d 20  Expr *pOrExpr = 
3baab 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0d  pOrTerm->pExpr;.
3baac 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 41 6e  .        if( pAn
3baad 64 45 78 70 72 20 29 7b 0d 0a 20 20 20 20 20 20  dExpr ){..      
3baae 20 20 20 20 70 41 6e 64 45 78 70 72 2d 3e 70 4c      pAndExpr->pL
3baaf 65 66 74 20 3d 20 70 4f 72 45 78 70 72 3b 0d 0a  eft = pOrExpr;..
3bab0 20 20 20 20 20 20 20 20 20 20 70 4f 72 45 78 70            pOrExp
3bab1 72 20 3d 20 70 41 6e 64 45 78 70 72 3b 0d 0a 20  r = pAndExpr;.. 
3bab2 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
3bab3 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    /* Loop throug
3bab4 68 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20  h table entries 
3bab5 74 68 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20  that match term 
3bab6 70 4f 72 54 65 72 6d 2e 20 2a 2f 0d 0a 20 20 20  pOrTerm. */..   
3bab7 20 20 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d       pSubWInfo =
3bab8 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
3bab9 69 6e 28 70 50 61 72 73 65 2c 20 70 4f 72 54 61  in(pParse, pOrTa
3baba 62 2c 20 70 4f 72 45 78 70 72 2c 20 30 2c 20 30  b, pOrExpr, 0, 0
3babb 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
3babc 20 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45             WHERE
3babd 5f 4f 4d 49 54 5f 4f 50 45 4e 5f 43 4c 4f 53 45  _OMIT_OPEN_CLOSE
3babe 20 7c 20 57 48 45 52 45 5f 41 4e 44 5f 4f 4e 4c   | WHERE_AND_ONL
3babf 59 20 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  Y |..           
3bac0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45               WHE
3bac1 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 7c  RE_FORCE_TABLE |
3bac2 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f   WHERE_ONETABLE_
3bac3 4f 4e 4c 59 29 3b 0d 0a 20 20 20 20 20 20 20 20  ONLY);..        
3bac4 69 66 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b  if( pSubWInfo ){
3bac5 0d 0a 20 20 20 20 20 20 20 20 20 20 65 78 70 6c  ..          expl
3bac6 61 69 6e 4f 6e 65 53 63 61 6e 28 0d 0a 20 20 20  ainOneScan(..   
3bac7 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
3bac8 65 2c 20 70 4f 72 54 61 62 2c 20 26 70 53 75 62  e, pOrTab, &pSub
3bac9 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2c 20 69 4c 65  WInfo->a[0], iLe
3baca 76 65 6c 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72  vel, pLevel->iFr
3bacb 6f 6d 2c 20 30 0d 0a 20 20 20 20 20 20 20 20 20  om, 0..         
3bacc 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69   );..          i
3bacd 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  f( (wctrlFlags &
3bace 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45   WHERE_DUPLICATE
3bacf 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0d 0a 20 20 20  S_OK)==0 ){..   
3bad0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 65           int iSe
3bad1 74 20 3d 20 28 28 69 69 3d 3d 70 4f 72 57 63 2d  t = ((ii==pOrWc-
3bad2 3e 6e 54 65 72 6d 2d 31 29 3f 2d 31 3a 69 69 29  >nTerm-1)?-1:ii)
3bad3 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ;..            i
3bad4 6e 74 20 72 3b 0d 0a 20 20 20 20 20 20 20 20 20  nt r;..         
3bad5 20 20 20 72 20 3d 20 73 71 6c 69 74 65 33 45 78     r = sqlite3Ex
3bad6 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
3bad7 70 50 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d  pParse, pTabItem
3bad8 2d 3e 70 54 61 62 2c 20 2d 31 2c 20 69 43 75 72  ->pTab, -1, iCur
3bad9 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ..            
3bada 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3badb 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67               reg
3badc 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 20 20 20  Rowid);..       
3badd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3bade 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f  AddOp4Int(v, OP_
3badf 52 6f 77 53 65 74 54 65 73 74 2c 20 72 65 67 52  RowSetTest, regR
3bae0 6f 77 73 65 74 2c 0d 0a 20 20 20 20 20 20 20 20  owset,..        
3bae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bae2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3bae3 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
3bae4 76 29 2b 32 2c 20 72 2c 20 69 53 65 74 29 3b 0d  v)+2, r, iSet);.
3bae5 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
3bae6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
3bae7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
3bae8 47 6f 73 75 62 2c 20 72 65 67 52 65 74 75 72 6e  Gosub, regReturn
3bae9 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0d 0a 0d  , iLoopBody);...
3baea 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
3baeb 65 20 70 53 75 62 57 49 6e 66 6f 2d 3e 75 6e 74  e pSubWInfo->unt
3baec 65 73 74 65 64 54 65 72 6d 73 20 66 6c 61 67 20  estedTerms flag 
3baed 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
3baee 4f 52 20 74 65 72 6d 0d 0a 20 20 20 20 20 20 20  OR term..       
3baef 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20     ** contained 
3baf0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 41 4e 44 20  one or more AND 
3baf1 74 65 72 6d 20 66 72 6f 6d 20 61 20 6e 6f 74 52  term from a notR
3baf2 65 61 64 79 20 74 61 62 6c 65 2e 20 20 54 68 65  eady table.  The
3baf3 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  ..          ** t
3baf4 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 6e 6f  erms from the no
3baf5 74 52 65 61 64 79 20 74 61 62 6c 65 20 63 6f 75  tReady table cou
3baf6 6c 64 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64  ld not be tested
3baf7 20 61 6e 64 20 77 69 6c 6c 0d 0a 20 20 20 20 20   and will..     
3baf8 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20       ** need to 
3baf9 62 65 20 74 65 73 74 65 64 20 6c 61 74 65 72 2e  be tested later.
3bafa 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a  ..          */..
3bafb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 53            if( pS
3bafc 75 62 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65  ubWInfo->unteste
3bafd 64 54 65 72 6d 73 20 29 20 75 6e 74 65 73 74 65  dTerms ) unteste
3bafe 64 54 65 72 6d 73 20 3d 20 31 3b 0d 0a 0d 0a 20  dTerms = 1;.... 
3baff 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 69           /* Fini
3bb00 73 68 20 74 68 65 20 6c 6f 6f 70 20 74 68 72 6f  sh the loop thro
3bb01 75 67 68 20 74 61 62 6c 65 20 65 6e 74 72 69 65  ugh table entrie
3bb02 73 20 74 68 61 74 20 6d 61 74 63 68 20 74 65 72  s that match ter
3bb03 6d 20 70 4f 72 54 65 72 6d 2e 20 2a 2f 0d 0a 20  m pOrTerm. */.. 
3bb04 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3bb05 57 68 65 72 65 45 6e 64 28 70 53 75 62 57 49 6e  WhereEnd(pSubWIn
3bb06 66 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  fo);..        }.
3bb07 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
3bb08 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
3bb09 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ee(pParse->db, p
3bb0a 41 6e 64 45 78 70 72 29 3b 0d 0a 20 20 20 20 73  AndExpr);..    s
3bb0b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
3bb0c 50 31 28 76 2c 20 69 52 65 74 49 6e 69 74 2c 20  P1(v, iRetInit, 
3bb0d 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
3bb0e 6e 74 41 64 64 72 28 76 29 29 3b 0d 0a 20 20 20  ntAddr(v));..   
3bb0f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3bb10 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
3bb11 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
3bb12 6b 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  k);..    sqlite3
3bb13 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
3bb14 28 76 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0d  (v, iLoopBody);.
3bb15 0a 0d 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66  ...    if( pWInf
3bb16 6f 2d 3e 6e 4c 65 76 65 6c 3e 31 20 29 20 73 71  o->nLevel>1 ) sq
3bb17 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 70  lite3StackFree(p
3bb18 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 54 61  Parse->db, pOrTa
3bb19 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 75 6e  b);..    if( !un
3bb1a 74 65 73 74 65 64 54 65 72 6d 73 20 29 20 64 69  testedTerms ) di
3bb1b 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
3bb1c 2c 20 70 54 65 72 6d 29 3b 0d 0a 20 20 7d 65 6c  , pTerm);..  }el
3bb1d 73 65 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  se..#endif /* SQ
3bb1e 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
3bb1f 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0d 0a 0d 0a  IMIZATION */....
3bb20 20 20 7b 0d 0a 20 20 20 20 2f 2a 20 43 61 73 65    {..    /* Case
3bb21 20 35 3a 20 20 54 68 65 72 65 20 69 73 20 6e 6f   5:  There is no
3bb22 20 75 73 61 62 6c 65 20 69 6e 64 65 78 2e 20 20   usable index.  
3bb23 57 65 20 6d 75 73 74 20 64 6f 20 61 20 63 6f 6d  We must do a com
3bb24 70 6c 65 74 65 0d 0a 20 20 20 20 2a 2a 20 20 20  plete..    **   
3bb25 20 20 20 20 20 20 20 73 63 61 6e 20 6f 66 20 74         scan of t
3bb26 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 2e  he entire table.
3bb27 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 73 74  ..    */..    st
3bb28 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53  atic const u8 aS
3bb29 74 65 70 5b 5d 20 3d 20 7b 20 4f 50 5f 4e 65 78  tep[] = { OP_Nex
3bb2a 74 2c 20 4f 50 5f 50 72 65 76 20 7d 3b 0d 0a 20  t, OP_Prev };.. 
3bb2b 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
3bb2c 75 38 20 61 53 74 61 72 74 5b 5d 20 3d 20 7b 20  u8 aStart[] = { 
3bb2d 4f 50 5f 52 65 77 69 6e 64 2c 20 4f 50 5f 4c 61  OP_Rewind, OP_La
3bb2e 73 74 20 7d 3b 0d 0a 20 20 20 20 61 73 73 65 72  st };..    asser
3bb2f 74 28 20 62 52 65 76 3d 3d 30 20 7c 7c 20 62 52  t( bRev==0 || bR
3bb30 65 76 3d 3d 31 20 29 3b 0d 0a 20 20 20 20 61 73  ev==1 );..    as
3bb31 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d  sert( omitTable=
3bb32 3d 30 20 29 3b 0d 0a 20 20 20 20 70 4c 65 76 65  =0 );..    pLeve
3bb33 6c 2d 3e 6f 70 20 3d 20 61 53 74 65 70 5b 62 52  l->op = aStep[bR
3bb34 65 76 5d 3b 0d 0a 20 20 20 20 70 4c 65 76 65 6c  ev];..    pLevel
3bb35 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0d 0a 20 20  ->p1 = iCur;..  
3bb36 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31    pLevel->p2 = 1
3bb37 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   + sqlite3VdbeAd
3bb38 64 4f 70 32 28 76 2c 20 61 53 74 61 72 74 5b 62  dOp2(v, aStart[b
3bb39 52 65 76 5d 2c 20 69 43 75 72 2c 20 61 64 64 72  Rev], iCur, addr
3bb3a 42 72 6b 29 3b 0d 0a 20 20 20 20 70 4c 65 76 65  Brk);..    pLeve
3bb3b 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53  l->p5 = SQLITE_S
3bb3c 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43  TMTSTATUS_FULLSC
3bb3d 41 4e 5f 53 54 45 50 3b 0d 0a 20 20 7d 0d 0a 20  AN_STEP;..  }.. 
3bb3e 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
3bb3f 74 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b  tMask(pWC->pMask
3bb40 53 65 74 2c 20 69 43 75 72 29 3b 0d 0a 0d 0a 20  Set, iCur);.... 
3bb41 20 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20   /* Insert code 
3bb42 74 6f 20 74 65 73 74 20 65 76 65 72 79 20 73 75  to test every su
3bb43 62 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74  bexpression that
3bb44 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65   can be complete
3bb45 6c 79 0d 0a 20 20 2a 2a 20 63 6f 6d 70 75 74 65  ly..  ** compute
3bb46 64 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72  d using the curr
3bb47 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62 6c 65  ent set of table
3bb48 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 49  s...  **..  ** I
3bb49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46  MPLEMENTATION-OF
3bb4a 3a 20 52 2d 34 39 35 32 35 2d 35 30 39 33 35 20  : R-49525-50935 
3bb4b 54 65 72 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f  Terms that canno
3bb4c 74 20 62 65 20 73 61 74 69 73 66 69 65 64 20 74  t be satisfied t
3bb4d 68 72 6f 75 67 68 0d 0a 20 20 2a 2a 20 74 68 65  hrough..  ** the
3bb4e 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 20   use of indices 
3bb4f 62 65 63 6f 6d 65 20 74 65 73 74 73 20 74 68 61  become tests tha
3bb50 74 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  t are evaluated 
3bb51 61 67 61 69 6e 73 74 20 65 61 63 68 20 72 6f 77  against each row
3bb52 20 6f 66 0d 0a 20 20 2a 2a 20 74 68 65 20 72 65   of..  ** the re
3bb53 6c 65 76 61 6e 74 20 69 6e 70 75 74 20 74 61 62  levant input tab
3bb54 6c 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f  les...  */..  fo
3bb55 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20  r(pTerm=pWC->a, 
3bb56 6a 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e  j=pWC->nTerm; j>
3bb57 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29  0; j--, pTerm++)
3bb58 7b 0d 0a 20 20 20 20 45 78 70 72 20 2a 70 45 3b  {..    Expr *pE;
3bb59 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
3bb5a 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
3bb5b 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b   TERM_VIRTUAL );
3bb5c 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 30 35 37 35   /* IMP: R-30575
3bb5d 2d 31 31 36 36 32 20 2a 2f 0d 0a 20 20 20 20 74  -11662 */..    t
3bb5e 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
3bb5f 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43  wtFlags & TERM_C
3bb60 4f 44 45 44 20 29 3b 0d 0a 20 20 20 20 69 66 28  ODED );..    if(
3bb61 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
3bb62 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  & (TERM_VIRTUAL|
3bb63 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f  TERM_CODED) ) co
3bb64 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 69 66 28  ntinue;..    if(
3bb65 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41   (pTerm->prereqA
3bb66 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d  ll & notReady)!=
3bb67 30 20 29 7b 0d 0a 20 20 20 20 20 20 74 65 73 74  0 ){..      test
3bb68 63 61 73 65 28 20 70 57 49 6e 66 6f 2d 3e 75 6e  case( pWInfo->un
3bb69 74 65 73 74 65 64 54 65 72 6d 73 3d 3d 30 0d 0a  testedTerms==0..
3bb6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
3bb6b 26 20 28 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c  & (pWInfo->wctrl
3bb6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e  Flags & WHERE_ON
3bb6d 45 54 41 42 4c 45 5f 4f 4e 4c 59 29 21 3d 30 20  ETABLE_ONLY)!=0 
3bb6e 29 3b 0d 0a 20 20 20 20 20 20 70 57 49 6e 66 6f  );..      pWInfo
3bb6f 2d 3e 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20  ->untestedTerms 
3bb70 3d 20 31 3b 0d 0a 20 20 20 20 20 20 63 6f 6e 74  = 1;..      cont
3bb71 69 6e 75 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  inue;..    }..  
3bb72 20 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45    pE = pTerm->pE
3bb73 78 70 72 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  xpr;..    assert
3bb74 28 20 70 45 21 3d 30 20 29 3b 0d 0a 20 20 20 20  ( pE!=0 );..    
3bb75 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66  if( pLevel->iLef
3bb76 74 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 48 61  tJoin && !ExprHa
3bb77 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50  sProperty(pE, EP
3bb78 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0d 0a 20  _FromJoin) ){.. 
3bb79 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a       continue;..
3bb7a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
3bb7b 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
3bb7c 61 72 73 65 2c 20 70 45 2c 20 61 64 64 72 43 6f  arse, pE, addrCo
3bb7d 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  nt, SQLITE_JUMPI
3bb7e 46 4e 55 4c 4c 29 3b 0d 0a 20 20 20 20 70 54 65  FNULL);..    pTe
3bb7f 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
3bb80 45 52 4d 5f 43 4f 44 45 44 3b 0d 0a 20 20 7d 0d  ERM_CODED;..  }.
3bb81 0a 0d 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c 45  ...  /* For a LE
3bb82 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 67  FT OUTER JOIN, g
3bb83 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
3bb84 74 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74 68  t will record th
3bb85 65 20 66 61 63 74 20 74 68 61 74 0d 0a 20 20 2a  e fact that..  *
3bb86 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72  * at least one r
3bb87 6f 77 20 6f 66 20 74 68 65 20 72 69 67 68 74 20  ow of the right 
3bb88 74 61 62 6c 65 20 68 61 73 20 6d 61 74 63 68 65  table has matche
3bb89 64 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65  d the left table
3bb8a 2e 20 20 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  .  ..  */..  if(
3bb8b 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
3bb8c 69 6e 20 29 7b 0d 0a 20 20 20 20 70 4c 65 76 65  in ){..    pLeve
3bb8d 6c 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20 73  l->addrFirst = s
3bb8e 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
3bb8f 74 41 64 64 72 28 76 29 3b 0d 0a 20 20 20 20 73  tAddr(v);..    s
3bb90 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3bb91 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
3bb92 31 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  1, pLevel->iLeft
3bb93 4a 6f 69 6e 29 3b 0d 0a 20 20 20 20 56 64 62 65  Join);..    Vdbe
3bb94 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 63  Comment((v, "rec
3bb95 6f 72 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69  ord LEFT JOIN hi
3bb96 74 22 29 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74  t"));..    sqlit
3bb97 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72  e3ExprCacheClear
3bb98 28 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 66  (pParse);..    f
3bb99 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c  or(pTerm=pWC->a,
3bb9a 20 6a 3d 30 3b 20 6a 3c 70 57 43 2d 3e 6e 54 65   j=0; j<pWC->nTe
3bb9b 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b  rm; j++, pTerm++
3bb9c 29 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  ){..      testca
3bb9d 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  se( pTerm->wtFla
3bb9e 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
3bb9f 4c 20 29 3b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d  L );  /* IMP: R-
3bba0 33 30 35 37 35 2d 31 31 36 36 32 20 2a 2f 0d 0a  30575-11662 */..
3bba1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3bba2 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
3bba3 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0d 0a   TERM_CODED );..
3bba4 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
3bba5 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
3bba6 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f  _VIRTUAL|TERM_CO
3bba7 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  DED) ) continue;
3bba8 0d 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 65  ..      if( (pTe
3bba9 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20  rm->prereqAll & 
3bbaa 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29 7b 0d  notReady)!=0 ){.
3bbab 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3bbac 20 70 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65   pWInfo->unteste
3bbad 64 54 65 72 6d 73 20 29 3b 0d 0a 20 20 20 20 20  dTerms );..     
3bbae 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20     continue;..  
3bbaf 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73      }..      ass
3bbb0 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 70  ert( pTerm->pExp
3bbb1 72 20 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  r );..      sqli
3bbb2 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
3bbb3 50 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45  Parse, pTerm->pE
3bbb4 78 70 72 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53  xpr, addrCont, S
3bbb5 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
3bbb6 29 3b 0d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  );..      pTerm-
3bbb7 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
3bbb8 5f 43 4f 44 45 44 3b 0d 0a 20 20 20 20 7d 0d 0a  _CODED;..    }..
3bbb9 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 52 65    }..  sqlite3Re
3bbba 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
3bbbb 72 73 65 2c 20 69 52 65 6c 65 61 73 65 52 65 67  rse, iReleaseReg
3bbbc 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 6e  );....  return n
3bbbd 6f 74 52 65 61 64 79 3b 0d 0a 7d 0d 0a 0d 0a 23  otReady;..}....#
3bbbe 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
3bbbf 45 5f 54 45 53 54 29 0d 0a 2f 2a 0d 0a 2a 2a 20  E_TEST)../*..** 
3bbc0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
3bbc1 72 69 61 62 6c 65 20 68 6f 6c 64 73 20 61 20 74  riable holds a t
3bbc2 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20  ext description 
3bbc3 6f 66 20 71 75 65 72 79 20 70 6c 61 6e 20 67 65  of query plan ge
3bbc4 6e 65 72 61 74 65 64 0d 0a 2a 2a 20 62 79 20 74  nerated..** by t
3bbc5 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
3bbc6 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68  all to sqlite3Wh
3bbc7 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45 61 63  ereBegin().  Eac
3bbc8 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72 65 42  h call to WhereB
3bbc9 65 67 69 6e 0d 0a 2a 2a 20 6f 76 65 72 77 72 69  egin..** overwri
3bbca 74 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  tes the previous
3bbcb 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  .  This informat
3bbcc 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20  ion is used for 
3bbcd 74 65 73 74 69 6e 67 20 61 6e 64 0d 0a 2a 2a 20  testing and..** 
3bbce 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0d 0a  analysis only...
3bbcf 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  */..SQLITE_API c
3bbd0 68 61 72 20 73 71 6c 69 74 65 33 5f 71 75 65 72  har sqlite3_quer
3bbd1 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d  y_plan[BMS*2*40]
3bbd2 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68  ;  /* Text of th
3bbd3 65 20 6a 6f 69 6e 20 2a 2f 0d 0a 73 74 61 74 69  e join */..stati
3bbd4 63 20 69 6e 74 20 6e 51 50 6c 61 6e 20 3d 20 30  c int nQPlan = 0
3bbd5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3bbd6 2a 20 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 77  * Next free slow
3bbd7 20 69 6e 20 5f 71 75 65 72 79 5f 70 6c 61 6e 5b   in _query_plan[
3bbd8 5d 20 2a 2f 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f  ] */....#endif /
3bbd9 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
3bbda 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65  ....../*..** Fre
3bbdb 65 20 61 20 57 68 65 72 65 49 6e 66 6f 20 73 74  e a WhereInfo st
3bbdc 72 75 63 74 75 72 65 0d 0a 2a 2f 0d 0a 73 74 61  ructure..*/..sta
3bbdd 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 49 6e  tic void whereIn
3bbde 66 6f 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a  foFree(sqlite3 *
3bbdf 64 62 2c 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  db, WhereInfo *p
3bbe0 57 49 6e 66 6f 29 7b 0d 0a 20 20 69 66 28 20 41  WInfo){..  if( A
3bbe1 4c 57 41 59 53 28 70 57 49 6e 66 6f 29 20 29 7b  LWAYS(pWInfo) ){
3bbe2 0d 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20  ..    int i;..  
3bbe3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 57 49    for(i=0; i<pWI
3bbe4 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20 69 2b 2b  nfo->nLevel; i++
3bbe5 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
3bbe6 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49  3_index_info *pI
3bbe7 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b  nfo = pWInfo->a[
3bbe8 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0d 0a 20 20  i].pIdxInfo;..  
3bbe9 20 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29 7b      if( pInfo ){
3bbea 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 61 73 73  ..        /* ass
3bbeb 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64  ert( pInfo->need
3bbec 54 6f 46 72 65 65 49 64 78 53 74 72 3d 3d 30 20  ToFreeIdxStr==0 
3bbed 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
3bbee 6c 65 64 20 29 3b 20 2a 2f 0d 0a 20 20 20 20 20  led ); */..     
3bbef 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 65     if( pInfo->ne
3bbf0 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29  edToFreeIdxStr )
3bbf1 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
3bbf2 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d  ite3_free(pInfo-
3bbf3 3e 69 64 78 53 74 72 29 3b 0d 0a 20 20 20 20 20  >idxStr);..     
3bbf4 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 73 71     }..        sq
3bbf5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3bbf6 70 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 7d  pInfo);..      }
3bbf7 0d 0a 20 20 20 20 20 20 69 66 28 20 70 57 49 6e  ..      if( pWIn
3bbf8 66 6f 2d 3e 61 5b 69 5d 2e 70 6c 61 6e 2e 77 73  fo->a[i].plan.ws
3bbf9 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54 45  Flags & WHERE_TE
3bbfa 4d 50 5f 49 4e 44 45 58 20 29 7b 0d 0a 20 20 20  MP_INDEX ){..   
3bbfb 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
3bbfc 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e   = pWInfo->a[i].
3bbfd 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0d 0a 20 20  plan.u.pIdx;..  
3bbfe 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20 29        if( pIdx )
3bbff 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
3bc00 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
3bc01 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0d 0a  Idx->zColAff);..
3bc02 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3bc03 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 64 78  3DbFree(db, pIdx
3bc04 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  );..        }.. 
3bc05 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
3bc06 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c     whereClauseCl
3bc07 65 61 72 28 70 57 49 6e 66 6f 2d 3e 70 57 43 29  ear(pWInfo->pWC)
3bc08 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ;..    sqlite3Db
3bc09 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29  Free(db, pWInfo)
3bc0a 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  ;..  }..}....../
3bc0b 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  *..** Generate t
3bc0c 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
3bc0d 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f  the loop used fo
3bc0e 72 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  r WHERE clause p
3bc0f 72 6f 63 65 73 73 69 6e 67 2e 0d 0a 2a 2a 20 54  rocessing...** T
3bc10 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
3bc11 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3bc12 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74  an opaque struct
3bc13 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ure that contain
3bc14 73 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  s..** informatio
3bc15 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72 6d  n needed to term
3bc16 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20  inate the loop. 
3bc17 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c 6c   Later, the call
3bc18 69 6e 67 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20  ing routine..** 
3bc19 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71  should invoke sq
3bc1a 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20  lite3WhereEnd() 
3bc1b 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20  with the return 
3bc1c 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
3bc1d 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 69 6e 20 6f 72  nction..** in or
3bc1e 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  der to complete 
3bc1f 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3bc20 20 70 72 6f 63 65 73 73 69 6e 67 2e 0d 0a 2a 2a   processing...**
3bc21 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  ..** If an error
3bc22 20 6f 63 63 75 72 73 2c 20 74 68 69 73 20 72 6f   occurs, this ro
3bc23 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 4e 55  utine returns NU
3bc24 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  LL...**..** The 
3bc25 62 61 73 69 63 20 69 64 65 61 20 69 73 20 74 6f  basic idea is to
3bc26 20 64 6f 20 61 20 6e 65 73 74 65 64 20 6c 6f 6f   do a nested loo
3bc27 70 2c 20 6f 6e 65 20 6c 6f 6f 70 20 66 6f 72 20  p, one loop for 
3bc28 65 61 63 68 20 74 61 62 6c 65 20 69 6e 0d 0a 2a  each table in..*
3bc29 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
3bc2a 65 20 6f 66 20 61 20 73 65 6c 65 63 74 2e 20 20  e of a select.  
3bc2b 28 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41  (INSERT and UPDA
3bc2c 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  TE statements ar
3bc2d 65 20 74 68 65 0d 0a 2a 2a 20 73 61 6d 65 20 61  e the..** same a
3bc2e 73 20 61 20 53 45 4c 45 43 54 20 77 69 74 68 20  s a SELECT with 
3bc2f 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74 61  only a single ta
3bc30 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
3bc31 63 6c 61 75 73 65 2e 29 20 20 46 6f 72 0d 0a 2a  clause.)  For..*
3bc32 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68  * example, if th
3bc33 65 20 53 51 4c 20 69 73 20 74 68 69 73 3a 0d 0a  e SQL is this:..
3bc34 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20 20 53 45 4c  **..**       SEL
3bc35 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74  ECT * FROM t1, t
3bc36 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e 2e 3b  2, t3 WHERE ...;
3bc37 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 6e 20 74 68  ..**..** Then th
3bc38 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
3bc39 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79   is conceptually
3bc3a 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77   like the follow
3bc3b 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
3bc3c 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69    foreach row1 i
3bc3d 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20  n t1 do       \ 
3bc3e 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65     Code generate
3bc3f 64 0d 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72  d..**        for
3bc40 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32 20  each row2 in t2 
3bc41 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73  do      |-- by s
3bc42 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
3bc43 28 29 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ()..**          
3bc44 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20  foreach row3 in 
3bc45 74 33 20 64 6f 20 20 20 2f 0d 0a 2a 2a 20 20 20  t3 do   /..**   
3bc46 20 20 20 20 20 20 20 20 20 2e 2e 2e 0d 0a 2a 2a           .....**
3bc47 20 20 20 20 20 20 20 20 20 20 65 6e 64 20 20 20            end   
3bc48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bc49 20 20 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65    \    Code gene
3bc4a 72 61 74 65 64 0d 0a 2a 2a 20 20 20 20 20 20 20  rated..**       
3bc4b 20 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20   end            
3bc4c 20 20 20 20 20 20 20 20 20 20 20 20 7c 2d 2d 20              |-- 
3bc4d 62 79 20 73 71 6c 69 74 65 33 57 68 65 72 65 45  by sqlite3WhereE
3bc4e 6e 64 28 29 0d 0a 2a 2a 20 20 20 20 20 20 65 6e  nd()..**      en
3bc4f 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
3bc50 20 20 20 20 20 20 20 20 20 20 2f 0d 0a 2a 2a 0d            /..**.
3bc51 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
3bc52 65 20 6c 6f 6f 70 73 20 6d 69 67 68 74 20 6e 6f  e loops might no
3bc53 74 20 62 65 20 6e 65 73 74 65 64 20 69 6e 20 74  t be nested in t
3bc54 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68 69 63  he order in whic
3bc55 68 20 74 68 65 79 0d 0a 2a 2a 20 61 70 70 65 61  h they..** appea
3bc56 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  r in the FROM cl
3bc57 61 75 73 65 20 69 66 20 61 20 64 69 66 66 65 72  ause if a differ
3bc58 65 6e 74 20 6f 72 64 65 72 20 69 73 20 62 65 74  ent order is bet
3bc59 74 65 72 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65  ter able to make
3bc5a 0d 0a 2a 2a 20 75 73 65 20 6f 66 20 69 6e 64 69  ..** use of indi
3bc5b 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20  ces.  Note also 
3bc5c 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 49 4e  that when the IN
3bc5d 20 6f 70 65 72 61 74 6f 72 20 61 70 70 65 61 72   operator appear
3bc5e 73 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 57 48 45  s in..** the WHE
3bc5f 52 45 20 63 6c 61 75 73 65 2c 20 69 74 20 6d 69  RE clause, it mi
3bc60 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 64  ght result in ad
3bc61 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65 64 20  ditional nested 
3bc62 6c 6f 6f 70 73 20 66 6f 72 0d 0a 2a 2a 20 73 63  loops for..** sc
3bc63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20 61  anning through a
3bc64 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74 68 65  ll values on the
3bc65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
3bc66 20 6f 66 20 74 68 65 20 49 4e 2e 0d 0a 2a 2a 0d   of the IN...**.
3bc67 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74  .** There are Bt
3bc68 72 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f  ree cursors asso
3bc69 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
3bc6a 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73   table.  t1 uses
3bc6b 20 63 75 72 73 6f 72 0d 0a 2a 2a 20 6e 75 6d 62   cursor..** numb
3bc6c 65 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30  er pTabList->a[0
3bc6d 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75  ].iCursor.  t2 u
3bc6e 73 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 70  ses the cursor p
3bc6f 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43  TabList->a[1].iC
3bc70 75 72 73 6f 72 2e 0d 0a 2a 2a 20 41 6e 64 20 73  ursor...** And s
3bc71 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72  o forth.  This r
3bc72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
3bc73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68   code to open th
3bc74 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f 72 73  ose VDBE cursors
3bc75 0d 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  ..** and sqlite3
3bc76 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65 72  WhereEnd() gener
3bc77 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 6f  ates the code to
3bc78 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0d 0a 2a 2a   close them...**
3bc79 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68  ..** The code th
3bc7a 61 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  at sqlite3WhereB
3bc7b 65 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73  egin() generates
3bc7c 20 6c 65 61 76 65 73 20 74 68 65 20 63 75 72 73   leaves the curs
3bc7d 6f 72 73 20 6e 61 6d 65 64 0d 0a 2a 2a 20 69 6e  ors named..** in
3bc7e 20 70 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69   pTabList pointi
3bc7f 6e 67 20 61 74 20 74 68 65 69 72 20 61 70 70 72  ng at their appr
3bc80 6f 70 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e  opriate entries.
3bc81 20 20 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65    The [...] code
3bc82 0d 0a 2a 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f  ..** can use OP_
3bc83 43 6f 6c 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f  Column and OP_Ro
3bc84 77 69 64 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74  wid opcodes on t
3bc85 68 65 73 65 20 63 75 72 73 6f 72 73 20 74 6f 20  hese cursors to 
3bc86 65 78 74 72 61 63 74 0d 0a 2a 2a 20 64 61 74 61  extract..** data
3bc87 20 66 72 6f 6d 20 74 68 65 20 76 61 72 69 6f 75   from the variou
3bc88 73 20 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20  s tables of the 
3bc89 6c 6f 6f 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  loop...**..** If
3bc8a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
3bc8b 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20  e is empty, the 
3bc8c 66 6f 72 65 61 63 68 20 6c 6f 6f 70 73 20 6d 75  foreach loops mu
3bc8d 73 74 20 65 61 63 68 20 73 63 61 6e 20 74 68 65  st each scan the
3bc8e 69 72 0d 0a 2a 2a 20 65 6e 74 69 72 65 20 74 61  ir..** entire ta
3bc8f 62 6c 65 73 2e 20 20 54 68 75 73 20 61 20 74 68  bles.  Thus a th
3bc90 72 65 65 2d 77 61 79 20 6a 6f 69 6e 20 69 73 20  ree-way join is 
3bc91 61 6e 20 4f 28 4e 5e 33 29 20 6f 70 65 72 61 74  an O(N^3) operat
3bc92 69 6f 6e 2e 20 20 42 75 74 20 69 66 0d 0a 2a 2a  ion.  But if..**
3bc93 20 74 68 65 20 74 61 62 6c 65 73 20 68 61 76 65   the tables have
3bc94 20 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65   indices and the
3bc95 72 65 20 61 72 65 20 74 65 72 6d 73 20 69 6e 20  re are terms in 
3bc96 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3bc97 20 74 68 61 74 0d 0a 2a 2a 20 72 65 66 65 72 20   that..** refer 
3bc98 74 6f 20 74 68 6f 73 65 20 69 6e 64 69 63 65 73  to those indices
3bc99 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 74 61 62  , a complete tab
3bc9a 6c 65 20 73 63 61 6e 20 63 61 6e 20 62 65 20 61  le scan can be a
3bc9b 76 6f 69 64 65 64 20 61 6e 64 20 74 68 65 0d 0a  voided and the..
3bc9c 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72 75 6e  ** code will run
3bc9d 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20 20 4d   much faster.  M
3bc9e 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 20  ost of the work 
3bc9f 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
3bca0 69 73 20 63 68 65 63 6b 69 6e 67 0d 0a 2a 2a 20  is checking..** 
3bca1 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20  to see if there 
3bca2 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61 74  are indices that
3bca3 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
3bca4 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f 6f  speed up the loo
3bca5 70 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 65 72 6d 73  p...**..** Terms
3bca6 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
3bca7 61 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73  ause are also us
3bca8 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63  ed to limit whic
3bca9 68 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0d  h rows actually.
3bcaa 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74  .** make it to t
3bcab 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20  he "..." in the 
3bcac 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f  middle of the lo
3bcad 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63 68 20  op.  After each 
3bcae 22 66 6f 72 65 61 63 68 22 2c 0d 0a 2a 2a 20 74  "foreach",..** t
3bcaf 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
3bcb0 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75 73  E clause that us
3bcb1 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20  e only terms in 
3bcb2 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75  that loop and ou
3bcb3 74 65 72 0d 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72  ter..** loops ar
3bcb4 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
3bcb5 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20  if false a jump 
3bcb6 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61  is made around a
3bcb7 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0d 0a 2a  ll subsequent..*
3bcb8 2a 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f  * inner loops (o
3bcb9 72 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e  r around the "..
3bcba 2e 22 20 69 66 20 74 68 65 20 74 65 73 74 20 6f  ." if the test o
3bcbb 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65  ccurs within the
3bcbc 20 69 6e 6e 65 72 2d 0d 0a 2a 2a 20 6d 6f 73 74   inner-..** most
3bcbd 20 6c 6f 6f 70 29 0d 0a 2a 2a 0d 0a 2a 2a 20 4f   loop)..**..** O
3bcbe 55 54 45 52 20 4a 4f 49 4e 53 0d 0a 2a 2a 0d 0a  UTER JOINS..**..
3bcbf 2a 2a 20 41 6e 20 6f 75 74 65 72 20 6a 6f 69 6e  ** An outer join
3bcc0 20 6f 66 20 74 61 62 6c 65 73 20 74 31 20 61 6e   of tables t1 an
3bcc1 64 20 74 32 20 69 73 20 63 6f 6e 63 65 70 74 61  d t2 is concepta
3bcc2 6c 6c 79 20 63 6f 64 65 64 20 61 73 20 66 6f 6c  lly coded as fol
3bcc3 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  lows:..**..**   
3bcc4 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e   foreach row1 in
3bcc5 20 74 31 20 64 6f 0d 0a 2a 2a 20 20 20 20 20 20   t1 do..**      
3bcc6 66 6c 61 67 20 3d 20 30 0d 0a 2a 2a 20 20 20 20  flag = 0..**    
3bcc7 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32 20 69    foreach row2 i
3bcc8 6e 20 74 32 20 64 6f 0d 0a 2a 2a 20 20 20 20 20  n t2 do..**     
3bcc9 20 20 20 73 74 61 72 74 3a 0d 0a 2a 2a 20 20 20     start:..**   
3bcca 20 20 20 20 20 20 20 2e 2e 2e 0d 0a 2a 2a 20 20         .....**  
3bccb 20 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31          flag = 1
3bccc 0d 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0d 0a 2a  ..**      end..*
3bccd 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d 3d  *      if flag==
3bcce 30 20 74 68 65 6e 0d 0a 2a 2a 20 20 20 20 20 20  0 then..**      
3bccf 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20    move the row2 
3bcd0 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c  cursor to a null
3bcd1 20 72 6f 77 0d 0a 2a 2a 20 20 20 20 20 20 20 20   row..**        
3bcd2 67 6f 74 6f 20 73 74 61 72 74 0d 0a 2a 2a 20 20  goto start..**  
3bcd3 20 20 20 20 66 69 0d 0a 2a 2a 20 20 20 20 65 6e      fi..**    en
3bcd4 64 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 52 44 45 52 20  d..**..** ORDER 
3bcd5 42 59 20 43 4c 41 55 53 45 20 50 52 4f 43 45 53  BY CLAUSE PROCES
3bcd6 53 49 4e 47 0d 0a 2a 2a 0d 0a 2a 2a 20 2a 70 70  SING..**..** *pp
3bcd7 4f 72 64 65 72 42 79 20 69 73 20 61 20 70 6f 69  OrderBy is a poi
3bcd8 6e 74 65 72 20 74 6f 20 74 68 65 20 4f 52 44 45  nter to the ORDE
3bcd9 52 20 42 59 20 63 6c 61 75 73 65 20 6f 66 20 61  R BY clause of a
3bcda 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
3bcdb 74 2c 0d 0a 2a 2a 20 69 66 20 74 68 65 72 65 20  t,..** if there 
3bcdc 69 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65 72  is one.  If ther
3bcdd 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59  e is no ORDER BY
3bcde 20 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74 68   clause or if th
3bcdf 69 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 69  is routine..** i
3bce0 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e  s called from an
3bce1 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
3bce2 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  E statement, the
3bce3 6e 20 70 70 4f 72 64 65 72 42 79 20 69 73 20 4e  n ppOrderBy is N
3bce4 55 4c 4c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ULL...**..** If 
3bce5 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  an index can be 
3bce6 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  used so that the
3bce7 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20   natural output 
3bce8 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61 62  order of the tab
3bce9 6c 65 0d 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63  le..** scan is c
3bcea 6f 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f  orrect for the O
3bceb 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
3bcec 74 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20  then that index 
3bced 69 73 20 75 73 65 64 20 61 6e 64 0d 0a 2a 2a 20  is used and..** 
3bcee 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 73 65  *ppOrderBy is se
3bcef 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73  t to NULL.  This
3bcf0 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74   is an optimizat
3bcf1 69 6f 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74  ion that prevent
3bcf2 73 20 61 6e 0d 0a 2a 2a 20 75 6e 6e 65 63 65 73  s an..** unneces
3bcf3 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65  sary sort of the
3bcf4 20 72 65 73 75 6c 74 20 73 65 74 20 69 66 20 61   result set if a
3bcf5 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70 72 69  n index appropri
3bcf6 61 74 65 20 66 6f 72 20 74 68 65 0d 0a 2a 2a 20  ate for the..** 
3bcf7 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
3bcf8 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0d  already exists..
3bcf9 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 77  .**..** If the w
3bcfa 68 65 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70  here clause loop
3bcfb 73 20 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61  s cannot be arra
3bcfc 6e 67 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20  nged to provide 
3bcfd 74 68 65 20 63 6f 72 72 65 63 74 0d 0a 2a 2a 20  the correct..** 
3bcfe 6f 75 74 70 75 74 20 6f 72 64 65 72 2c 20 74 68  output order, th
3bcff 65 6e 20 74 68 65 20 2a 70 70 4f 72 64 65 72 42  en the *ppOrderB
3bd00 79 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0d  y is unchanged..
3bd01 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
3bd02 41 54 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73  ATE WhereInfo *s
3bd03 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
3bd04 28 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  (..  Parse *pPar
3bd05 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  se,        /* Th
3bd06 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
3bd07 20 2a 2f 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a   */..  SrcList *
3bd08 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a 20  pTabList,    /* 
3bd09 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 61  A list of all ta
3bd0a 62 6c 65 73 20 74 6f 20 62 65 20 73 63 61 6e 6e  bles to be scann
3bd0b 65 64 20 2a 2f 0d 0a 20 20 45 78 70 72 20 2a 70  ed */..  Expr *p
3bd0c 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f  Where,         /
3bd0d 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
3bd0e 73 65 20 2a 2f 0d 0a 20 20 45 78 70 72 4c 69 73  se */..  ExprLis
3bd0f 74 20 2a 2a 70 70 4f 72 64 65 72 42 79 2c 20 2f  t **ppOrderBy, /
3bd10 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  * An ORDER BY cl
3bd11 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  ause, or NULL */
3bd12 0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 44  ..  ExprList *pD
3bd13 69 73 74 69 6e 63 74 2c 20 20 2f 2a 20 54 68 65  istinct,  /* The
3bd14 20 73 65 6c 65 63 74 2d 6c 69 73 74 20 66 6f 72   select-list for
3bd15 20 44 49 53 54 49 4e 43 54 20 71 75 65 72 69 65   DISTINCT querie
3bd16 73 20 2d 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0d 0a  s - or NULL */..
3bd17 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73    u16 wctrlFlags
3bd18 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
3bd19 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20 66 6c  f the WHERE_* fl
3bd1a 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73  ags defined in s
3bd1b 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0d 0a 29  qliteInt.h */..)
3bd1c 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  {..  int i;     
3bd1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bd1e 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
3bd1f 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 57  */..  int nByteW
3bd20 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Info;           
3bd21 20 2f 2a 20 4e 75 6d 2e 20 62 79 74 65 73 20 61   /* Num. bytes a
3bd22 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 57 68 65  llocated for Whe
3bd23 72 65 49 6e 66 6f 20 73 74 72 75 63 74 20 2a 2f  reInfo struct */
3bd24 0d 0a 20 20 69 6e 74 20 6e 54 61 62 4c 69 73 74  ..  int nTabList
3bd25 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3bd26 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
3bd27 65 6e 74 73 20 69 6e 20 70 54 61 62 4c 69 73 74  ents in pTabList
3bd28 20 2a 2f 0d 0a 20 20 57 68 65 72 65 49 6e 66 6f   */..  WhereInfo
3bd29 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20   *pWInfo;       
3bd2a 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65    /* Will become
3bd2b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3bd2c 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
3bd2d 6f 6e 20 2a 2f 0d 0a 20 20 56 64 62 65 20 2a 76  on */..  Vdbe *v
3bd2e 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
3bd2f 3b 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75  ;   /* The virtu
3bd30 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
3bd31 6e 65 20 2a 2f 0d 0a 20 20 42 69 74 6d 61 73 6b  ne */..  Bitmask
3bd32 20 6e 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20   notReady;      
3bd33 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 74      /* Cursors t
3bd34 68 61 74 20 61 72 65 20 6e 6f 74 20 79 65 74 20  hat are not yet 
3bd35 70 6f 73 69 74 69 6f 6e 65 64 20 2a 2f 0d 0a 20  positioned */.. 
3bd36 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
3bd37 4d 61 73 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54  MaskSet;    /* T
3bd38 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61  he expression ma
3bd39 73 6b 20 73 65 74 20 2a 2f 0d 0a 20 20 57 68 65  sk set */..  Whe
3bd3a 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20  reClause *pWC;  
3bd3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3bd3c 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66  Decomposition of
3bd3d 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
3bd3e 65 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 53  e */..  struct S
3bd3f 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61  rcList_item *pTa
3bd40 62 49 74 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e  bItem;  /* A sin
3bd41 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 70  gle entry from p
3bd42 54 61 62 4c 69 73 74 20 2a 2f 0d 0a 20 20 57 68  TabList */..  Wh
3bd43 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
3bd44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
3bd45 20 41 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20   A single level 
3bd46 69 6e 20 74 68 65 20 70 57 49 6e 66 6f 20 6c 69  in the pWInfo li
3bd47 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 46 72  st */..  int iFr
3bd48 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  om;             
3bd49 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
3bd4a 74 20 75 6e 75 73 65 64 20 46 52 4f 4d 20 63 6c  t unused FROM cl
3bd4b 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0d  ause element */.
3bd4c 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67 73 3b  .  int andFlags;
3bd4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3bd4e 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61 74   AND-ed combinat
3bd4f 69 6f 6e 20 6f 66 20 61 6c 6c 20 70 57 43 2d 3e  ion of all pWC->
3bd50 61 5b 5d 2e 77 74 46 6c 61 67 73 20 2a 2f 0d 0a  a[].wtFlags */..
3bd51 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
3bd52 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3bd53 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
3bd54 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54  ion */....  /* T
3bd55 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62  he number of tab
3bd56 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  les in the FROM 
3bd57 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65  clause is limite
3bd58 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
3bd59 6f 66 0d 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e  of..  ** bits in
3bd5a 20 61 20 42 69 74 6d 61 73 6b 20 0d 0a 20 20 2a   a Bitmask ..  *
3bd5b 2f 0d 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  /..  testcase( p
3bd5c 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 42  TabList->nSrc==B
3bd5d 4d 53 20 29 3b 0d 0a 20 20 69 66 28 20 70 54 61  MS );..  if( pTa
3bd5e 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20  bList->nSrc>BMS 
3bd5f 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ){..    sqlite3E
3bd60 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
3bd61 22 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62 6c  "at most %d tabl
3bd62 65 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42  es in a join", B
3bd63 4d 53 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  MS);..    return
3bd64 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a   0;..  }....  /*
3bd65 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e   This function n
3bd66 6f 72 6d 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  ormally generate
3bd67 73 20 61 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20  s a nested loop 
3bd68 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 69  for all tables i
3bd69 6e 20 0d 0a 20 20 2a 2a 20 70 54 61 62 4c 69 73  n ..  ** pTabLis
3bd6a 74 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 57  t.  But if the W
3bd6b 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e  HERE_ONETABLE_ON
3bd6c 4c 59 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  LY flag is set, 
3bd6d 74 68 65 6e 20 77 65 20 73 68 6f 75 6c 64 0d 0a  then we should..
3bd6e 20 20 2a 2a 20 6f 6e 6c 79 20 67 65 6e 65 72 61    ** only genera
3bd6f 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  te code for the 
3bd70 66 69 72 73 74 20 74 61 62 6c 65 20 69 6e 20 70  first table in p
3bd71 54 61 62 4c 69 73 74 20 61 6e 64 20 61 73 73 75  TabList and assu
3bd72 6d 65 20 74 68 61 74 0d 0a 20 20 2a 2a 20 61 6e  me that..  ** an
3bd73 79 20 63 75 72 73 6f 72 73 20 61 73 73 6f 63 69  y cursors associ
3bd74 61 74 65 64 20 77 69 74 68 20 73 75 62 73 65 71  ated with subseq
3bd75 75 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20  uent tables are 
3bd76 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0d 0a  uninitialized...
3bd77 20 20 2a 2f 0d 0a 20 20 6e 54 61 62 4c 69 73 74    */..  nTabList
3bd78 20 3d 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26   = (wctrlFlags &
3bd79 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f   WHERE_ONETABLE_
3bd7a 4f 4e 4c 59 29 20 3f 20 31 20 3a 20 70 54 61 62  ONLY) ? 1 : pTab
3bd7b 4c 69 73 74 2d 3e 6e 53 72 63 3b 0d 0a 0d 0a 20  List->nSrc;.... 
3bd7c 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
3bd7d 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
3bd7e 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
3bd7f 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ure that will be
3bd80 63 6f 6d 65 20 74 68 65 0d 0a 20 20 2a 2a 20 72  come the..  ** r
3bd81 65 74 75 72 6e 20 76 61 6c 75 65 2e 20 41 20 73  eturn value. A s
3bd82 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  ingle allocation
3bd83 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
3bd84 65 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0d  e the WhereInfo.
3bd85 0a 20 20 2a 2a 20 73 74 72 75 63 74 2c 20 74 68  .  ** struct, th
3bd86 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 57 68  e contents of Wh
3bd87 65 72 65 49 6e 66 6f 2e 61 5b 5d 2c 20 74 68 65  ereInfo.a[], the
3bd88 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72   WhereClause str
3bd89 75 63 74 75 72 65 0d 0a 20 20 2a 2a 20 61 6e 64  ucture..  ** and
3bd8a 20 74 68 65 20 57 68 65 72 65 4d 61 73 6b 53 65   the WhereMaskSe
3bd8b 74 20 73 74 72 75 63 74 75 72 65 2e 20 53 69 6e  t structure. Sin
3bd8c 63 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 63  ce WhereClause c
3bd8d 6f 6e 74 61 69 6e 73 20 61 6e 20 38 2d 62 79 74  ontains an 8-byt
3bd8e 65 0d 0a 20 20 2a 2a 20 66 69 65 6c 64 20 28 74  e..  ** field (t
3bd8f 79 70 65 20 42 69 74 6d 61 73 6b 29 20 69 74 20  ype Bitmask) it 
3bd90 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20  must be aligned 
3bd91 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75  on an 8-byte bou
3bd92 6e 64 61 72 79 20 6f 6e 0d 0a 20 20 2a 2a 20 73  ndary on..  ** s
3bd93 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65  ome architecture
3bd94 73 2e 20 48 65 6e 63 65 20 74 68 65 20 52 4f 55  s. Hence the ROU
3bd95 4e 44 38 28 29 20 62 65 6c 6f 77 2e 0d 0a 20 20  ND8() below...  
3bd96 2a 2f 0d 0a 20 20 64 62 20 3d 20 70 50 61 72 73  */..  db = pPars
3bd97 65 2d 3e 64 62 3b 0d 0a 20 20 6e 42 79 74 65 57  e->db;..  nByteW
3bd98 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28 73 69  Info = ROUND8(si
3bd99 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29 2b  zeof(WhereInfo)+
3bd9a 28 6e 54 61 62 4c 69 73 74 2d 31 29 2a 73 69 7a  (nTabList-1)*siz
3bd9b 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c 29 29  eof(WhereLevel))
3bd9c 3b 0d 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  ;..  pWInfo = sq
3bd9d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
3bd9e 6f 28 64 62 2c 20 0d 0a 20 20 20 20 20 20 6e 42  o(db, ..      nB
3bd9f 79 74 65 57 49 6e 66 6f 20 2b 20 0d 0a 20 20 20  yteWInfo + ..   
3bda0 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 43     sizeof(WhereC
3bda1 6c 61 75 73 65 29 20 2b 0d 0a 20 20 20 20 20 20  lause) +..      
3bda2 73 69 7a 65 6f 66 28 57 68 65 72 65 4d 61 73 6b  sizeof(WhereMask
3bda3 53 65 74 29 0d 0a 20 20 29 3b 0d 0a 20 20 69 66  Set)..  );..  if
3bda4 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
3bda5 65 64 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ed ){..    sqlit
3bda6 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57 49  e3DbFree(db, pWI
3bda7 6e 66 6f 29 3b 0d 0a 20 20 20 20 70 57 49 6e 66  nfo);..    pWInf
3bda8 6f 20 3d 20 30 3b 0d 0a 20 20 20 20 67 6f 74 6f  o = 0;..    goto
3bda9 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
3bdaa 3b 0d 0a 20 20 7d 0d 0a 20 20 70 57 49 6e 66 6f  ;..  }..  pWInfo
3bdab 2d 3e 6e 4c 65 76 65 6c 20 3d 20 6e 54 61 62 4c  ->nLevel = nTabL
3bdac 69 73 74 3b 0d 0a 20 20 70 57 49 6e 66 6f 2d 3e  ist;..  pWInfo->
3bdad 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
3bdae 0d 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62  ..  pWInfo->pTab
3bdaf 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
3bdb0 0d 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  ..  pWInfo->iBre
3bdb1 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ak = sqlite3Vdbe
3bdb2 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0d 0a 20  MakeLabel(v);.. 
3bdb3 20 70 57 49 6e 66 6f 2d 3e 70 57 43 20 3d 20 70   pWInfo->pWC = p
3bdb4 57 43 20 3d 20 28 57 68 65 72 65 43 6c 61 75 73  WC = (WhereClaus
3bdb5 65 20 2a 29 26 28 28 75 38 20 2a 29 70 57 49 6e  e *)&((u8 *)pWIn
3bdb6 66 6f 29 5b 6e 42 79 74 65 57 49 6e 66 6f 5d 3b  fo)[nByteWInfo];
3bdb7 0d 0a 20 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72  ..  pWInfo->wctr
3bdb8 6c 46 6c 61 67 73 20 3d 20 77 63 74 72 6c 46 6c  lFlags = wctrlFl
3bdb9 61 67 73 3b 0d 0a 20 20 70 57 49 6e 66 6f 2d 3e  ags;..  pWInfo->
3bdba 73 61 76 65 64 4e 51 75 65 72 79 4c 6f 6f 70 20  savedNQueryLoop 
3bdbb 3d 20 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79  = pParse->nQuery
3bdbc 4c 6f 6f 70 3b 0d 0a 20 20 70 4d 61 73 6b 53 65  Loop;..  pMaskSe
3bdbd 74 20 3d 20 28 57 68 65 72 65 4d 61 73 6b 53 65  t = (WhereMaskSe
3bdbe 74 2a 29 26 70 57 43 5b 31 5d 3b 0d 0a 0d 0a 20  t*)&pWC[1];.... 
3bdbf 20 2f 2a 20 44 69 73 61 62 6c 65 20 74 68 65 20   /* Disable the 
3bdc0 44 49 53 54 49 4e 43 54 20 6f 70 74 69 6d 69 7a  DISTINCT optimiz
3bdc1 61 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 45 5f  ation if SQLITE_
3bdc2 44 69 73 74 69 6e 63 74 4f 70 74 20 69 73 20 73  DistinctOpt is s
3bdc3 65 74 20 76 69 61 0d 0a 20 20 2a 2a 20 73 71 6c  et via..  ** sql
3bdc4 69 74 65 33 5f 74 65 73 74 5f 63 74 72 6c 28 53  ite3_test_ctrl(S
3bdc5 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f  QLITE_TESTCTRL_O
3bdc6 50 54 49 4d 49 5a 41 54 49 4f 4e 53 2c 2e 2e 2e  PTIMIZATIONS,...
3bdc7 29 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e  ) */..  if( db->
3bdc8 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 44  flags & SQLITE_D
3bdc9 69 73 74 69 6e 63 74 4f 70 74 20 29 20 70 44 69  istinctOpt ) pDi
3bdca 73 74 69 6e 63 74 20 3d 20 30 3b 0d 0a 0d 0a 20  stinct = 0;.... 
3bdcb 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 57 48   /* Split the WH
3bdcc 45 52 45 20 63 6c 61 75 73 65 20 69 6e 74 6f 20  ERE clause into 
3bdcd 73 65 70 61 72 61 74 65 20 73 75 62 65 78 70 72  separate subexpr
3bdce 65 73 73 69 6f 6e 73 20 77 68 65 72 65 20 65 61  essions where ea
3bdcf 63 68 0d 0a 20 20 2a 2a 20 73 75 62 65 78 70 72  ch..  ** subexpr
3bdd0 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61  ession is separa
3bdd1 74 65 64 20 62 79 20 61 6e 20 41 4e 44 20 6f 70  ted by an AND op
3bdd2 65 72 61 74 6f 72 2e 0d 0a 20 20 2a 2f 0d 0a 20  erator...  */.. 
3bdd3 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 70 4d 61   initMaskSet(pMa
3bdd4 73 6b 53 65 74 29 3b 0d 0a 20 20 77 68 65 72 65  skSet);..  where
3bdd5 43 6c 61 75 73 65 49 6e 69 74 28 70 57 43 2c 20  ClauseInit(pWC, 
3bdd6 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74  pParse, pMaskSet
3bdd7 2c 20 77 63 74 72 6c 46 6c 61 67 73 29 3b 0d 0a  , wctrlFlags);..
3bdd8 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
3bdd9 65 43 6f 6e 73 74 61 6e 74 73 28 70 50 61 72 73  eConstants(pPars
3bdda 65 2c 20 70 57 68 65 72 65 29 3b 0d 0a 20 20 77  e, pWhere);..  w
3bddb 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70  hereSplit(pWC, p
3bddc 57 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b 20  Where, TK_AND); 
3bddd 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 31 35 38 34    /* IMP: R-1584
3bdde 32 2d 35 33 32 39 36 20 2a 2f 0d 0a 20 20 20 20  2-53296 */..    
3bddf 0d 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63  ..  /* Special c
3bde0 61 73 65 3a 20 61 20 57 48 45 52 45 20 63 6c 61  ase: a WHERE cla
3bde1 75 73 65 20 74 68 61 74 20 69 73 20 63 6f 6e 73  use that is cons
3bde2 74 61 6e 74 2e 20 20 45 76 61 6c 75 61 74 65 20  tant.  Evaluate 
3bde3 74 68 65 0d 0a 20 20 2a 2a 20 65 78 70 72 65 73  the..  ** expres
3bde4 73 69 6f 6e 20 61 6e 64 20 65 69 74 68 65 72 20  sion and either 
3bde5 6a 75 6d 70 20 6f 76 65 72 20 61 6c 6c 20 6f 66  jump over all of
3bde6 20 74 68 65 20 63 6f 64 65 20 6f 72 20 66 61 6c   the code or fal
3bde7 6c 20 74 68 72 75 2e 0d 0a 20 20 2a 2f 0d 0a 20  l thru...  */.. 
3bde8 20 69 66 28 20 70 57 68 65 72 65 20 26 26 20 28   if( pWhere && (
3bde9 6e 54 61 62 4c 69 73 74 3d 3d 30 20 7c 7c 20 73  nTabList==0 || s
3bdea 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
3bdeb 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68 65  tantNotJoin(pWhe
3bdec 72 65 29 29 20 29 7b 0d 0a 20 20 20 20 73 71 6c  re)) ){..    sql
3bded 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
3bdee 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 2c 20  pParse, pWhere, 
3bdef 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20  pWInfo->iBreak, 
3bdf0 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
3bdf1 4c 29 3b 0d 0a 20 20 20 20 70 57 68 65 72 65 20  L);..    pWhere 
3bdf2 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  = 0;..  }....  /
3bdf3 2a 20 41 73 73 69 67 6e 20 61 20 62 69 74 20 66  * Assign a bit f
3bdf4 72 6f 6d 20 74 68 65 20 62 69 74 6d 61 73 6b 20  rom the bitmask 
3bdf5 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e  to every term in
3bdf6 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
3bdf7 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 57 68  ...  **..  ** Wh
3bdf8 65 6e 20 61 73 73 69 67 6e 69 6e 67 20 62 69 74  en assigning bit
3bdf9 6d 61 73 6b 20 76 61 6c 75 65 73 20 74 6f 20 46  mask values to F
3bdfa 52 4f 4d 20 63 6c 61 75 73 65 20 63 75 72 73 6f  ROM clause curso
3bdfb 72 73 2c 20 69 74 20 6d 75 73 74 20 62 65 0d 0a  rs, it must be..
3bdfc 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68    ** the case th
3bdfd 61 74 20 69 66 20 58 20 69 73 20 74 68 65 20 62  at if X is the b
3bdfe 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 4e  itmask for the N
3bdff 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  -th FROM clause 
3be00 74 65 72 6d 20 74 68 65 6e 0d 0a 20 20 2a 2a 20  term then..  ** 
3be01 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20  the bitmask for 
3be02 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  all FROM clause 
3be03 74 65 72 6d 73 20 74 6f 20 74 68 65 20 6c 65 66  terms to the lef
3be04 74 20 6f 66 20 74 68 65 20 4e 2d 74 68 20 74 65  t of the N-th te
3be05 72 6d 0d 0a 20 20 2a 2a 20 69 73 20 28 58 2d 31  rm..  ** is (X-1
3be06 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73 73 69  ).   An expressi
3be07 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 63  on from the ON c
3be08 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54 20  lause of a LEFT 
3be09 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0d 0a 20 20  JOIN can use..  
3be0a 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69 67  ** its Expr.iRig
3be0b 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75  htJoinTable valu
3be0c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62 69  e to find the bi
3be0d 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69 67  tmask of the rig
3be0e 68 74 20 74 61 62 6c 65 0d 0a 20 20 2a 2a 20 6f  ht table..  ** o
3be0f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62  f the join.  Sub
3be10 74 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f  tracting one fro
3be11 6d 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  m the right tabl
3be12 65 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20  e bitmask gives 
3be13 61 0d 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20  a..  ** bitmask 
3be14 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74  for all tables t
3be15 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68  o the left of th
3be16 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67  e join.  Knowing
3be17 20 74 68 65 20 62 69 74 6d 61 73 6b 0d 0a 20 20   the bitmask..  
3be18 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65  ** for all table
3be19 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  s to the left of
3be1a 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20   a left join is 
3be1b 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b  important.  Tick
3be1c 65 74 20 23 33 30 31 35 2e 0d 0a 20 20 2a 2a 0d  et #3015...  **.
3be1d 0a 20 20 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20  .  ** Configure 
3be1e 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e  the WhereClause.
3be1f 76 6d 61 73 6b 20 76 61 72 69 61 62 6c 65 20 73  vmask variable s
3be20 6f 20 74 68 61 74 20 62 69 74 73 20 74 68 61 74  o that bits that
3be21 20 63 6f 72 72 65 73 70 6f 6e 64 0d 0a 20 20 2a   correspond..  *
3be22 2a 20 74 6f 20 76 69 72 74 75 61 6c 20 74 61 62  * to virtual tab
3be23 6c 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 73  le cursors are s
3be24 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  et. This is used
3be25 20 74 6f 20 73 65 6c 65 63 74 69 76 65 6c 79 20   to selectively 
3be26 64 69 73 61 62 6c 65 20 0d 0a 20 20 2a 2a 20 74  disable ..  ** t
3be27 68 65 20 4f 52 2d 74 6f 2d 49 4e 20 74 72 61 6e  he OR-to-IN tran
3be28 73 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 65 78  sformation in ex
3be29 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28  prAnalyzeOrTerm(
3be2a 29 2e 20 49 74 20 69 73 20 6e 6f 74 20 68 65 6c  ). It is not hel
3be2b 70 66 75 6c 20 0d 0a 20 20 2a 2a 20 77 69 74 68  pful ..  ** with
3be2c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
3be2d 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 4e 6f 74  ..  **..  ** Not
3be2e 65 20 74 68 61 74 20 62 69 74 6d 61 73 6b 73 20  e that bitmasks 
3be2f 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72 20  are created for 
3be30 61 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  all pTabList->nS
3be31 72 63 20 74 61 62 6c 65 73 20 69 6e 0d 0a 20 20  rc tables in..  
3be32 2a 2a 20 70 54 61 62 4c 69 73 74 2c 20 6e 6f 74  ** pTabList, not
3be33 20 6a 75 73 74 20 74 68 65 20 66 69 72 73 74 20   just the first 
3be34 6e 54 61 62 4c 69 73 74 20 74 61 62 6c 65 73 2e  nTabList tables.
3be35 20 20 6e 54 61 62 4c 69 73 74 20 69 73 20 6e 6f    nTabList is no
3be36 72 6d 61 6c 6c 79 0d 0a 20 20 2a 2a 20 65 71 75  rmally..  ** equ
3be37 61 6c 20 74 6f 20 70 54 61 62 4c 69 73 74 2d 3e  al to pTabList->
3be38 6e 53 72 63 20 62 75 74 20 6d 69 67 68 74 20 62  nSrc but might b
3be39 65 20 73 68 6f 72 74 65 6e 65 64 20 74 6f 20 31  e shortened to 1
3be3a 20 69 66 20 74 68 65 0d 0a 20 20 2a 2a 20 57 48   if the..  ** WH
3be3b 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e 4c  ERE_ONETABLE_ONL
3be3c 59 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0d 0a  Y flag is set...
3be3d 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20    */..  assert( 
3be3e 70 57 43 2d 3e 76 6d 61 73 6b 3d 3d 30 20 26 26  pWC->vmask==0 &&
3be3f 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3d 3d 30 20   pMaskSet->n==0 
3be40 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
3be41 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
3be42 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 63 72 65 61   i++){..    crea
3be43 74 65 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  teMask(pMaskSet,
3be44 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pTabList->a[i].
3be45 69 43 75 72 73 6f 72 29 3b 0d 0a 23 69 66 6e 64  iCursor);..#ifnd
3be46 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
3be47 49 52 54 55 41 4c 54 41 42 4c 45 0d 0a 20 20 20  IRTUALTABLE..   
3be48 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
3be49 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 62 29  List->a[i].pTab)
3be4a 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70 54   && IsVirtual(pT
3be4b 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61  abList->a[i].pTa
3be4c 62 29 20 29 7b 0d 0a 20 20 20 20 20 20 70 57 43  b) ){..      pWC
3be4d 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28 28 42 69 74  ->vmask |= ((Bit
3be4e 6d 61 73 6b 29 31 20 3c 3c 20 69 29 3b 0d 0a 20  mask)1 << i);.. 
3be4f 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20     }..#endif..  
3be50 7d 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
3be51 47 0d 0a 20 20 7b 0d 0a 20 20 20 20 42 69 74 6d  G..  {..    Bitm
3be52 61 73 6b 20 74 6f 54 68 65 4c 65 66 74 20 3d 20  ask toTheLeft = 
3be53 30 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  0;..    for(i=0;
3be54 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
3be55 63 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  c; i++){..      
3be56 42 69 74 6d 61 73 6b 20 6d 20 3d 20 67 65 74 4d  Bitmask m = getM
3be57 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54  ask(pMaskSet, pT
3be58 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
3be59 72 73 6f 72 29 3b 0d 0a 20 20 20 20 20 20 61 73  rsor);..      as
3be5a 73 65 72 74 28 20 28 6d 2d 31 29 3d 3d 74 6f 54  sert( (m-1)==toT
3be5b 68 65 4c 65 66 74 20 29 3b 0d 0a 20 20 20 20 20  heLeft );..     
3be5c 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d 20 6d 3b   toTheLeft |= m;
3be5d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 23 65  ..    }..  }..#e
3be5e 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 41 6e 61  ndif....  /* Ana
3be5f 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 20  lyze all of the 
3be60 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  subexpressions. 
3be61 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70 72 41   Note that exprA
3be62 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74 0d 0a  nalyze() might..
3be63 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69 72    ** add new vir
3be64 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20  tual terms onto 
3be65 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57  the end of the W
3be66 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57 65  HERE clause.  We
3be67 20 64 6f 20 6e 6f 74 0d 0a 20 20 2a 2a 20 77 61   do not..  ** wa
3be68 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  nt to analyze th
3be69 65 73 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d  ese virtual term
3be6a 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c  s, so start anal
3be6b 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64  yzing at the end
3be6c 0d 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20  ..  ** and work 
3be6d 66 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74 20  forward so that 
3be6e 74 68 65 20 61 64 64 65 64 20 76 69 72 74 75 61  the added virtua
3be6f 6c 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65  l terms are neve
3be70 72 20 70 72 6f 63 65 73 73 65 64 2e 0d 0a 20 20  r processed...  
3be71 2a 2f 0d 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a  */..  exprAnalyz
3be72 65 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 70  eAll(pTabList, p
3be73 57 43 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e  WC);..  if( db->
3be74 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d  mallocFailed ){.
3be75 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
3be76 65 67 69 6e 45 72 72 6f 72 3b 0d 0a 20 20 7d 0d  eginError;..  }.
3be77 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ...  /* Check if
3be78 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 71 75   the DISTINCT qu
3be79 61 6c 69 66 69 65 72 2c 20 69 66 20 74 68 65 72  alifier, if ther
3be7a 65 20 69 73 20 6f 6e 65 2c 20 69 73 20 72 65 64  e is one, is red
3be7b 75 6e 64 61 6e 74 2e 20 0d 0a 20 20 2a 2a 20 49  undant. ..  ** I
3be7c 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 73 65  f it is, then se
3be7d 74 20 70 44 69 73 74 69 6e 63 74 20 74 6f 20 4e  t pDistinct to N
3be7e 55 4c 4c 20 61 6e 64 20 57 68 65 72 65 49 6e 66  ULL and WhereInf
3be7f 6f 2e 65 44 69 73 74 69 6e 63 74 20 74 6f 0d 0a  o.eDistinct to..
3be80 20 20 2a 2a 20 57 48 45 52 45 5f 44 49 53 54 49    ** WHERE_DISTI
3be81 4e 43 54 5f 55 4e 49 51 55 45 20 74 6f 20 74 65  NCT_UNIQUE to te
3be82 6c 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  ll the caller to
3be83 20 69 67 6e 6f 72 65 20 74 68 65 20 44 49 53 54   ignore the DIST
3be84 49 4e 43 54 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  INCT...  */..  i
3be85 66 28 20 70 44 69 73 74 69 6e 63 74 20 26 26 20  f( pDistinct && 
3be86 69 73 44 69 73 74 69 6e 63 74 52 65 64 75 6e 64  isDistinctRedund
3be87 61 6e 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ant(pParse, pTab
3be88 4c 69 73 74 2c 20 70 57 43 2c 20 70 44 69 73 74  List, pWC, pDist
3be89 69 6e 63 74 29 20 29 7b 0d 0a 20 20 20 20 70 44  inct) ){..    pD
3be8a 69 73 74 69 6e 63 74 20 3d 20 30 3b 0d 0a 20 20  istinct = 0;..  
3be8b 20 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69    pWInfo->eDisti
3be8c 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49 53 54  nct = WHERE_DIST
3be8d 49 4e 43 54 5f 55 4e 49 51 55 45 3b 0d 0a 20 20  INCT_UNIQUE;..  
3be8e 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 68 6f 73 65 20  }....  /* Chose 
3be8f 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 74  the best index t
3be90 6f 20 75 73 65 20 66 6f 72 20 65 61 63 68 20 74  o use for each t
3be91 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
3be92 20 63 6c 61 75 73 65 2e 0d 0a 20 20 2a 2a 0d 0a   clause...  **..
3be93 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66    ** This loop f
3be94 69 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ills in the foll
3be95 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0d 0a 20  owing fields:.. 
3be96 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 70 57 49 6e   **..  **   pWIn
3be97 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20  fo->a[].pIdx    
3be98 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 75    The index to u
3be99 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65  se for this leve
3be9a 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0d 0a  l of the loop...
3be9b 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
3be9c 5b 5d 2e 77 73 46 6c 61 67 73 20 20 20 57 48 45  [].wsFlags   WHE
3be9d 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73 73  RE_xxx flags ass
3be9e 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49 64  ociated with pId
3be9f 78 0d 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  x..  **   pWInfo
3bea0 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20  ->a[].nEq       
3bea1 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d  The number of ==
3bea2 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69   and IN constrai
3bea3 6e 74 73 0d 0a 20 20 2a 2a 20 20 20 70 57 49 6e  nts..  **   pWIn
3bea4 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20  fo->a[].iFrom   
3bea5 20 20 57 68 69 63 68 20 74 65 72 6d 20 6f 66 20    Which term of 
3bea6 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
3bea7 69 73 20 62 65 69 6e 67 20 63 6f 64 65 64 0d 0a  is being coded..
3bea8 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
3bea9 5b 5d 2e 69 54 61 62 43 75 72 20 20 20 54 68 65  [].iTabCur   The
3beaa 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
3beab 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
3beac 62 6c 65 0d 0a 20 20 2a 2a 20 20 20 70 57 49 6e  ble..  **   pWIn
3bead 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20  fo->a[].iIdxCur 
3beae 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f    The VDBE curso
3beaf 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0d  r for the index.
3beb0 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
3beb1 61 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57 68  a[].pTerm     Wh
3beb2 65 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f  en wsFlags==WO_O
3beb3 52 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65  R, the OR-clause
3beb4 20 74 65 72 6d 0d 0a 20 20 2a 2a 0d 0a 20 20 2a   term..  **..  *
3beb5 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f  * This loop also
3beb6 20 66 69 67 75 72 65 73 20 6f 75 74 20 74 68 65   figures out the
3beb7 20 6e 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f   nesting order o
3beb8 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
3beb9 46 52 4f 4d 0d 0a 20 20 2a 2a 20 63 6c 61 75 73  FROM..  ** claus
3beba 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6e 6f 74 52  e...  */..  notR
3bebb 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b  eady = ~(Bitmask
3bebc 29 30 3b 0d 0a 20 20 61 6e 64 46 6c 61 67 73 20  )0;..  andFlags 
3bebd 3d 20 7e 30 3b 0d 0a 20 20 57 48 45 52 45 54 52  = ~0;..  WHERETR
3bebe 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69  ACE(("*** Optimi
3bebf 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22  zer Start ***\n"
3bec0 29 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 69 46 72  ));..  for(i=iFr
3bec1 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49  om=0, pLevel=pWI
3bec2 6e 66 6f 2d 3e 61 3b 20 69 3c 6e 54 61 62 4c 69  nfo->a; i<nTabLi
3bec3 73 74 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b  st; i++, pLevel+
3bec4 2b 29 7b 0d 0a 20 20 20 20 57 68 65 72 65 43 6f  +){..    WhereCo
3bec5 73 74 20 62 65 73 74 50 6c 61 6e 3b 20 20 20 20  st bestPlan;    
3bec6 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 65 66 66       /* Most eff
3bec7 69 63 69 65 6e 74 20 70 6c 61 6e 20 73 65 65 6e  icient plan seen
3bec8 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20 20 20 20   so far */..    
3bec9 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
3beca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
3becb 6e 64 65 78 20 66 6f 72 20 46 52 4f 4d 20 74 61  ndex for FROM ta
3becc 62 6c 65 20 61 74 20 70 54 61 62 49 74 65 6d 20  ble at pTabItem 
3becd 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6a 3b 20 20  */..    int j;  
3bece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3becf 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
3bed0 6e 67 20 6f 76 65 72 20 46 52 4f 4d 20 74 61 62  ng over FROM tab
3bed1 6c 65 73 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  les */..    int 
3bed2 62 65 73 74 4a 20 3d 20 2d 31 3b 20 20 20 20 20  bestJ = -1;     
3bed3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
3bed4 61 6c 75 65 20 6f 66 20 6a 20 2a 2f 0d 0a 20 20  alue of j */..  
3bed5 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20 20    Bitmask m;    
3bed6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3bed7 20 42 69 74 6d 61 73 6b 20 76 61 6c 75 65 20 66   Bitmask value f
3bed8 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a 2f  or j or bestJ */
3bed9 0d 0a 20 20 20 20 69 6e 74 20 69 73 4f 70 74 69  ..    int isOpti
3beda 6d 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  mal;            
3bedb 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f    /* Iterator fo
3bedc 72 20 6f 70 74 69 6d 61 6c 2f 6e 6f 6e 2d 6f 70  r optimal/non-op
3bedd 74 69 6d 61 6c 20 73 65 61 72 63 68 20 2a 2f 0d  timal search */.
3bede 0a 20 20 20 20 69 6e 74 20 6e 55 6e 63 6f 6e 73  .    int nUncons
3bedf 74 72 61 69 6e 65 64 3b 20 20 20 20 20 20 20 20  trained;        
3bee0 20 2f 2a 20 4e 75 6d 62 65 72 20 74 61 62 6c 65   /* Number table
3bee1 73 20 77 69 74 68 6f 75 74 20 49 4e 44 45 58 45  s without INDEXE
3bee2 44 20 42 59 20 2a 2f 0d 0a 20 20 20 20 42 69 74  D BY */..    Bit
3bee3 6d 61 73 6b 20 6e 6f 74 49 6e 64 65 78 65 64 3b  mask notIndexed;
3bee4 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
3bee5 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20   of tables that 
3bee6 63 61 6e 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e  cannot use an in
3bee7 64 65 78 20 2a 2f 0d 0a 0d 0a 20 20 20 20 6d 65  dex */....    me
3bee8 6d 73 65 74 28 26 62 65 73 74 50 6c 61 6e 2c 20  mset(&bestPlan, 
3bee9 30 2c 20 73 69 7a 65 6f 66 28 62 65 73 74 50 6c  0, sizeof(bestPl
3beea 61 6e 29 29 3b 0d 0a 20 20 20 20 62 65 73 74 50  an));..    bestP
3beeb 6c 61 6e 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49  lan.rCost = SQLI
3beec 54 45 5f 42 49 47 5f 44 42 4c 3b 0d 0a 20 20 20  TE_BIG_DBL;..   
3beed 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a   WHERETRACE(("**
3beee 2a 20 42 65 67 69 6e 20 73 65 61 72 63 68 20 66  * Begin search f
3beef 6f 72 20 6c 6f 6f 70 20 25 64 20 2a 2a 2a 5c 6e  or loop %d ***\n
3bef0 22 2c 20 69 29 29 3b 0d 0a 0d 0a 20 20 20 20 2f  ", i));....    /
3bef1 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
3bef2 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74  he remaining ent
3bef3 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
3bef4 20 63 6c 61 75 73 65 20 74 6f 20 66 69 6e 64 20   clause to find 
3bef5 74 68 65 0d 0a 20 20 20 20 2a 2a 20 6e 65 78 74  the..    ** next
3bef6 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 54 68   nested loop. Th
3bef7 65 20 6c 6f 6f 70 20 74 65 73 74 73 20 61 6c 6c  e loop tests all
3bef8 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74   FROM clause ent
3bef9 72 69 65 73 0d 0a 20 20 20 20 2a 2a 20 65 69 74  ries..    ** eit
3befa 68 65 72 20 6f 6e 63 65 20 6f 72 20 74 77 69 63  her once or twic
3befb 65 2e 20 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  e. ..    **..   
3befc 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 65   ** The first te
3befd 73 74 20 69 73 20 61 6c 77 61 79 73 20 70 65 72  st is always per
3befe 66 6f 72 6d 65 64 20 69 66 20 74 68 65 72 65 20  formed if there 
3beff 61 72 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  are two or more 
3bf00 65 6e 74 72 69 65 73 0d 0a 20 20 20 20 2a 2a 20  entries..    ** 
3bf01 72 65 6d 61 69 6e 69 6e 67 20 61 6e 64 20 6e 65  remaining and ne
3bf02 76 65 72 20 70 65 72 66 6f 72 6d 65 64 20 69 66  ver performed if
3bf03 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f   there is only o
3bf04 6e 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65  ne FROM clause e
3bf05 6e 74 72 79 0d 0a 20 20 20 20 2a 2a 20 74 6f 20  ntry..    ** to 
3bf06 63 68 6f 6f 73 65 20 66 72 6f 6d 2e 20 20 54 68  choose from.  Th
3bf07 65 20 66 69 72 73 74 20 74 65 73 74 20 6c 6f 6f  e first test loo
3bf08 6b 73 20 66 6f 72 20 61 6e 20 22 6f 70 74 69 6d  ks for an "optim
3bf09 61 6c 22 20 73 63 61 6e 2e 20 20 49 6e 0d 0a 20  al" scan.  In.. 
3bf0a 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 74 65     ** this conte
3bf0b 78 74 20 61 6e 20 6f 70 74 69 6d 61 6c 20 73 63  xt an optimal sc
3bf0c 61 6e 20 69 73 20 6f 6e 65 20 74 68 61 74 20 75  an is one that u
3bf0d 73 65 73 20 74 68 65 20 73 61 6d 65 20 73 74 72  ses the same str
3bf0e 61 74 65 67 79 0d 0a 20 20 20 20 2a 2a 20 66 6f  ategy..    ** fo
3bf0f 72 20 74 68 65 20 67 69 76 65 6e 20 46 52 4f 4d  r the given FROM
3bf10 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 61 73   clause entry as
3bf11 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74   would be select
3bf12 65 64 20 69 66 20 74 68 65 20 65 6e 74 72 79 0d  ed if the entry.
3bf13 0a 20 20 20 20 2a 2a 20 77 65 72 65 20 75 73 65  .    ** were use
3bf14 64 20 61 73 20 74 68 65 20 69 6e 6e 65 72 6d 6f  d as the innermo
3bf15 73 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20  st nested loop. 
3bf16 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
3bf17 20 61 20 74 61 62 6c 65 0d 0a 20 20 20 20 2a 2a   a table..    **
3bf18 20 69 73 20 63 68 6f 73 65 6e 20 73 75 63 68 20   is chosen such 
3bf19 74 68 61 74 20 74 68 65 20 63 6f 73 74 20 6f 66  that the cost of
3bf1a 20 72 75 6e 6e 69 6e 67 20 74 68 61 74 20 74 61   running that ta
3bf1b 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ble cannot be re
3bf1c 64 75 63 65 64 0d 0a 20 20 20 20 2a 2a 20 62 79  duced..    ** by
3bf1d 20 77 61 69 74 69 6e 67 20 66 6f 72 20 6f 74 68   waiting for oth
3bf1e 65 72 20 74 61 62 6c 65 73 20 74 6f 20 72 75 6e  er tables to run
3bf1f 20 66 69 72 73 74 2e 20 20 54 68 69 73 20 22 6f   first.  This "o
3bf20 70 74 69 6d 61 6c 22 20 74 65 73 74 20 77 6f 72  ptimal" test wor
3bf21 6b 73 0d 0a 20 20 20 20 2a 2a 20 62 79 20 66 69  ks..    ** by fi
3bf22 72 73 74 20 61 73 73 75 6d 69 6e 67 20 74 68 61  rst assuming tha
3bf23 74 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  t the FROM claus
3bf24 65 20 69 73 20 6f 6e 20 74 68 65 20 69 6e 6e 65  e is on the inne
3bf25 72 20 6c 6f 6f 70 20 61 6e 64 20 66 69 6e 64 69  r loop and findi
3bf26 6e 67 0d 0a 20 20 20 20 2a 2a 20 69 74 73 20 71  ng..    ** its q
3bf27 75 65 72 79 20 70 6c 61 6e 2c 20 74 68 65 6e 20  uery plan, then 
3bf28 63 68 65 63 6b 69 6e 67 20 74 6f 20 73 65 65 20  checking to see 
3bf29 69 66 20 74 68 61 74 20 71 75 65 72 79 20 70 6c  if that query pl
3bf2a 61 6e 20 75 73 65 73 20 61 6e 79 0d 0a 20 20 20  an uses any..   
3bf2b 20 2a 2a 20 6f 74 68 65 72 20 46 52 4f 4d 20 63   ** other FROM c
3bf2c 6c 61 75 73 65 20 74 65 72 6d 73 20 74 68 61 74  lause terms that
3bf2d 20 61 72 65 20 6e 6f 74 52 65 61 64 79 2e 20 20   are notReady.  
3bf2e 49 66 20 6e 6f 20 6e 6f 74 52 65 61 64 79 20 74  If no notReady t
3bf2f 65 72 6d 73 20 61 72 65 0d 0a 20 20 20 20 2a 2a  erms are..    **
3bf30 20 75 73 65 64 20 74 68 65 6e 20 74 68 65 20 22   used then the "
3bf31 6f 70 74 69 6d 61 6c 22 20 71 75 65 72 79 20 70  optimal" query p
3bf32 6c 61 6e 20 77 6f 72 6b 73 2e 0d 0a 20 20 20 20  lan works...    
3bf33 2a 2a 0d 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20  **..    ** Note 
3bf34 74 68 61 74 20 74 68 65 20 57 68 65 72 65 43 6f  that the WhereCo
3bf35 73 74 2e 6e 52 6f 77 20 70 61 72 61 6d 65 74 65  st.nRow paramete
3bf36 72 20 66 6f 72 20 61 6e 20 6f 70 74 69 6d 61 6c  r for an optimal
3bf37 20 73 63 61 6e 20 6d 69 67 68 74 0d 0a 20 20 20   scan might..   
3bf38 20 2a 2a 20 6e 6f 74 20 62 65 20 61 73 20 73 6d   ** not be as sm
3bf39 61 6c 6c 20 61 73 20 69 74 20 77 6f 75 6c 64 20  all as it would 
3bf3a 62 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  be if the table 
3bf3b 72 65 61 6c 6c 79 20 77 65 72 65 20 74 68 65 20  really were the 
3bf3c 69 6e 6e 65 72 6d 6f 73 74 0d 0a 20 20 20 20 2a  innermost..    *
3bf3d 2a 20 6a 6f 69 6e 2e 20 20 54 68 65 20 6e 52 6f  * join.  The nRo
3bf3e 77 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 72  w value can be r
3bf3f 65 64 75 63 65 64 20 62 79 20 57 48 45 52 45 20  educed by WHERE 
3bf40 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
3bf41 74 73 0d 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ts..    ** that 
3bf42 64 6f 20 6e 6f 74 20 75 73 65 20 69 6e 64 69 63  do not use indic
3bf43 65 73 2e 20 20 42 75 74 20 74 68 69 73 20 6e 52  es.  But this nR
3bf44 6f 77 20 72 65 64 75 63 74 69 6f 6e 20 6f 6e 6c  ow reduction onl
3bf45 79 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  y happens if the
3bf46 0d 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 72  ..    ** table r
3bf47 65 61 6c 6c 79 20 69 73 20 74 68 65 20 69 6e 6e  eally is the inn
3bf48 65 72 6d 6f 73 74 20 6a 6f 69 6e 2e 20 20 0d 0a  ermost join.  ..
3bf49 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54      **..    ** T
3bf4a 68 65 20 73 65 63 6f 6e 64 20 6c 6f 6f 70 20 69  he second loop i
3bf4b 74 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  teration is only
3bf4c 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20 6e 6f   performed if no
3bf4d 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 0d 0a 20   optimal scan.. 
3bf4e 20 20 20 2a 2a 20 73 74 72 61 74 65 67 69 65 73     ** strategies
3bf4f 20 77 65 72 65 20 66 6f 75 6e 64 20 62 79 20 74   were found by t
3bf50 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69  he first iterati
3bf51 6f 6e 2e 20 54 68 69 73 20 73 65 63 6f 6e 64 20  on. This second 
3bf52 69 74 65 72 61 74 69 6f 6e 0d 0a 20 20 20 20 2a  iteration..    *
3bf53 2a 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 61  * is used to sea
3bf54 72 63 68 20 66 6f 72 20 74 68 65 20 6c 6f 77 65  rch for the lowe
3bf55 73 74 20 63 6f 73 74 20 73 63 61 6e 20 6f 76 65  st cost scan ove
3bf56 72 61 6c 6c 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20  rall...    **.. 
3bf57 20 20 20 2a 2a 20 50 72 65 76 69 6f 75 73 20 76     ** Previous v
3bf58 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
3bf59 65 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 6c 79  e performed only
3bf5a 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72   the second iter
3bf5b 61 74 69 6f 6e 20 2d 0d 0a 20 20 20 20 2a 2a 20  ation -..    ** 
3bf5c 74 68 65 20 6e 65 78 74 20 6f 75 74 65 72 6d 6f  the next outermo
3bf5d 73 74 20 6c 6f 6f 70 20 77 61 73 20 61 6c 77 61  st loop was alwa
3bf5e 79 73 20 74 68 61 74 20 77 69 74 68 20 74 68 65  ys that with the
3bf5f 20 6c 6f 77 65 73 74 20 6f 76 65 72 61 6c 6c 0d   lowest overall.
3bf60 0a 20 20 20 20 2a 2a 20 63 6f 73 74 2e 20 48 6f  .    ** cost. Ho
3bf61 77 65 76 65 72 2c 20 74 68 69 73 20 6d 65 61 6e  wever, this mean
3bf62 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 6f  t that SQLite co
3bf63 75 6c 64 20 73 65 6c 65 63 74 20 74 68 65 20 77  uld select the w
3bf64 72 6f 6e 67 20 70 6c 61 6e 0d 0a 20 20 20 20 2a  rong plan..    *
3bf65 2a 20 66 6f 72 20 73 63 72 69 70 74 73 20 73 75  * for scripts su
3bf66 63 68 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77  ch as the follow
3bf67 69 6e 67 3a 0d 0a 20 20 20 20 2a 2a 20 20 20 0d  ing:..    **   .
3bf68 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45  .    **   CREATE
3bf69 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b   TABLE t1(a, b);
3bf6a 20 0d 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41   ..    **   CREA
3bf6b 54 45 20 54 41 42 4c 45 20 74 32 28 63 2c 20 64  TE TABLE t2(c, d
3bf6c 29 3b 0d 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c  );..    **   SEL
3bf6d 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 2c 20 74  ECT * FROM t2, t
3bf6e 31 20 57 48 45 52 45 20 74 32 2e 72 6f 77 69 64  1 WHERE t2.rowid
3bf6f 20 3d 20 74 31 2e 61 3b 0d 0a 20 20 20 20 2a 2a   = t1.a;..    **
3bf70 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 62 65 73  ..    ** The bes
3bf71 74 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f  t strategy is to
3bf72 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
3bf73 20 74 61 62 6c 65 20 74 31 20 66 69 72 73 74 2e   table t1 first.
3bf74 20 48 6f 77 65 76 65 72 20 69 74 0d 0a 20 20 20   However it..   
3bf75 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   ** is not possi
3bf76 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ble to determine
3bf77 20 74 68 69 73 20 77 69 74 68 20 61 20 73 69 6d   this with a sim
3bf78 70 6c 65 20 67 72 65 65 64 79 20 61 6c 67 6f 72  ple greedy algor
3bf79 69 74 68 6d 2e 0d 0a 20 20 20 20 2a 2a 20 53 69  ithm...    ** Si
3bf7a 6e 63 65 20 74 68 65 20 63 6f 73 74 20 6f 66 20  nce the cost of 
3bf7b 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68  a linear scan th
3bf7c 72 6f 75 67 68 20 74 61 62 6c 65 20 74 32 20 69  rough table t2 i
3bf7d 73 20 74 68 65 20 73 61 6d 65 20 0d 0a 20 20 20  s the same ..   
3bf7e 20 2a 2a 20 61 73 20 74 68 65 20 63 6f 73 74 20   ** as the cost 
3bf7f 6f 66 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e  of a linear scan
3bf80 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20 74   through table t
3bf81 31 2c 20 61 20 73 69 6d 70 6c 65 20 67 72 65 65  1, a simple gree
3bf82 64 79 20 0d 0a 20 20 20 20 2a 2a 20 61 6c 67 6f  dy ..    ** algo
3bf83 72 69 74 68 6d 20 6d 61 79 20 63 68 6f 6f 73 65  rithm may choose
3bf84 20 74 6f 20 75 73 65 20 74 32 20 66 6f 72 20 74   to use t2 for t
3bf85 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70 2c 20 77  he outer loop, w
3bf86 68 69 63 68 20 69 73 20 61 20 6d 75 63 68 0d 0a  hich is a much..
3bf87 20 20 20 20 2a 2a 20 63 6f 73 74 6c 69 65 72 20      ** costlier 
3bf88 61 70 70 72 6f 61 63 68 2e 0d 0a 20 20 20 20 2a  approach...    *
3bf89 2f 0d 0a 20 20 20 20 6e 55 6e 63 6f 6e 73 74 72  /..    nUnconstr
3bf8a 61 69 6e 65 64 20 3d 20 30 3b 0d 0a 20 20 20 20  ained = 0;..    
3bf8b 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 30 3b 0d  notIndexed = 0;.
3bf8c 0a 20 20 20 20 66 6f 72 28 69 73 4f 70 74 69 6d  .    for(isOptim
3bf8d 61 6c 3d 28 69 46 72 6f 6d 3c 6e 54 61 62 4c 69  al=(iFrom<nTabLi
3bf8e 73 74 2d 31 29 3b 20 69 73 4f 70 74 69 6d 61 6c  st-1); isOptimal
3bf8f 3e 3d 30 20 26 26 20 62 65 73 74 4a 3c 30 3b 20  >=0 && bestJ<0; 
3bf90 69 73 4f 70 74 69 6d 61 6c 2d 2d 29 7b 0d 0a 20  isOptimal--){.. 
3bf91 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 61 73       Bitmask mas
3bf92 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
3bf93 2a 20 4d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73  * Mask of tables
3bf94 20 6e 6f 74 20 79 65 74 20 72 65 61 64 79 20 2a   not yet ready *
3bf95 2f 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 69  /..      for(j=i
3bf96 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d 3d 26  From, pTabItem=&
3bf97 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20  pTabList->a[j]; 
3bf98 6a 3c 6e 54 61 62 4c 69 73 74 3b 20 6a 2b 2b 2c  j<nTabList; j++,
3bf99 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0d 0a 20   pTabItem++){.. 
3bf9a 20 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74         int doNot
3bf9b 52 65 6f 72 64 65 72 3b 20 20 20 20 2f 2a 20 54  Reorder;    /* T
3bf9c 72 75 65 20 69 66 20 74 68 69 73 20 74 61 62 6c  rue if this tabl
3bf9d 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
3bf9e 72 65 6f 72 64 65 72 65 64 20 2a 2f 0d 0a 20 20  reordered */..  
3bf9f 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20        WhereCost 
3bfa0 73 43 6f 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f  sCost;     /* Co
3bfa1 73 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  st information f
3bfa2 72 6f 6d 20 62 65 73 74 5b 56 69 72 74 75 61 6c  rom best[Virtual
3bfa3 5d 49 6e 64 65 78 28 29 20 2a 2f 0d 0a 20 20 20  ]Index() */..   
3bfa4 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
3bfa5 4f 72 64 65 72 42 79 3b 20 20 2f 2a 20 4f 52 44  OrderBy;  /* ORD
3bfa6 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72  ER BY clause for
3bfa7 20 69 6e 64 65 78 20 74 6f 20 6f 70 74 69 6d 69   index to optimi
3bfa8 7a 65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 45  ze */..        E
3bfa9 78 70 72 4c 69 73 74 20 2a 70 44 69 73 74 3b 20  xprList *pDist; 
3bfaa 20 20 20 20 2f 2a 20 44 49 53 54 49 4e 43 54 20      /* DISTINCT 
3bfab 63 6c 61 75 73 65 20 66 6f 72 20 69 6e 64 65 78  clause for index
3bfac 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 2a 2f 0d   to optimize */.
3bfad 0a 20 20 0d 0a 20 20 20 20 20 20 20 20 64 6f 4e  .  ..        doN
3bfae 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28 70 54  otReorder =  (pT
3bfaf 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65  abItem->jointype
3bfb0 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43   & (JT_LEFT|JT_C
3bfb1 52 4f 53 53 29 29 21 3d 30 3b 0d 0a 20 20 20 20  ROSS))!=0;..    
3bfb2 20 20 20 20 69 66 28 20 6a 21 3d 69 46 72 6f 6d      if( j!=iFrom
3bfb3 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72   && doNotReorder
3bfb4 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
3bfb5 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70     m = getMask(p
3bfb6 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65  MaskSet, pTabIte
3bfb7 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0d 0a 20 20  m->iCursor);..  
3bfb8 20 20 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e        if( (m & n
3bfb9 6f 74 52 65 61 64 79 29 3d 3d 30 20 29 7b 0d 0a  otReady)==0 ){..
3bfba 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d            if( j=
3bfbb 3d 69 46 72 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b  =iFrom ) iFrom++
3bfbc 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  ;..          con
3bfbd 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 20 20  tinue;..        
3bfbe 7d 0d 0a 20 20 20 20 20 20 20 20 6d 61 73 6b 20  }..        mask 
3bfbf 3d 20 28 69 73 4f 70 74 69 6d 61 6c 20 3f 20 6d  = (isOptimal ? m
3bfc0 20 3a 20 6e 6f 74 52 65 61 64 79 29 3b 0d 0a 20   : notReady);.. 
3bfc1 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20         pOrderBy 
3bfc2 3d 20 28 28 69 3d 3d 30 20 26 26 20 70 70 4f 72  = ((i==0 && ppOr
3bfc3 64 65 72 42 79 20 29 3f 2a 70 70 4f 72 64 65 72  derBy )?*ppOrder
3bfc4 42 79 3a 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  By:0);..        
3bfc5 70 44 69 73 74 20 3d 20 28 69 3d 3d 30 20 3f 20  pDist = (i==0 ? 
3bfc6 70 44 69 73 74 69 6e 63 74 20 3a 20 30 29 3b 0d  pDistinct : 0);.
3bfc7 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61  .        if( pTa
3bfc8 62 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3d 3d 30  bItem->pIndex==0
3bfc9 20 29 20 6e 55 6e 63 6f 6e 73 74 72 61 69 6e 65   ) nUnconstraine
3bfca 64 2b 2b 3b 0d 0a 20 20 0d 0a 20 20 20 20 20 20  d++;..  ..      
3bfcb 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 3d    WHERETRACE(("=
3bfcc 3d 3d 20 74 72 79 69 6e 67 20 74 61 62 6c 65 20  == trying table 
3bfcd 25 64 20 77 69 74 68 20 69 73 4f 70 74 69 6d 61  %d with isOptima
3bfce 6c 3d 25 64 20 3d 3d 3d 5c 6e 22 2c 0d 0a 20 20  l=%d ===\n",..  
3bfcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bfd0 20 20 6a 2c 20 69 73 4f 70 74 69 6d 61 6c 29 29    j, isOptimal))
3bfd1 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ;..        asser
3bfd2 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61  t( pTabItem->pTa
3bfd3 62 20 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  b );..#ifndef SQ
3bfd4 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
3bfd5 4c 54 41 42 4c 45 0d 0a 20 20 20 20 20 20 20 20  LTABLE..        
3bfd6 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
3bfd7 61 62 49 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b  abItem->pTab) ){
3bfd8 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
3bfd9 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3bfda 2a 70 70 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61  *pp = &pWInfo->a
3bfdb 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0d 0a 20  [j].pIdxInfo;.. 
3bfdc 20 20 20 20 20 20 20 20 20 62 65 73 74 56 69 72           bestVir
3bfdd 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65  tualIndex(pParse
3bfde 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c  , pWC, pTabItem,
3bfdf 20 6d 61 73 6b 2c 20 6e 6f 74 52 65 61 64 79 2c   mask, notReady,
3bfe0 20 70 4f 72 64 65 72 42 79 2c 0d 0a 20 20 20 20   pOrderBy,..    
3bfe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bfe2 20 20 20 20 20 20 20 26 73 43 6f 73 74 2c 20 70         &sCost, p
3bfe3 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  p);..        }el
3bfe4 73 65 20 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20  se ..#endif..   
3bfe5 20 20 20 20 20 7b 0d 0a 20 20 20 20 20 20 20 20       {..        
3bfe6 20 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78    bestBtreeIndex
3bfe7 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 54  (pParse, pWC, pT
3bfe8 61 62 49 74 65 6d 2c 20 6d 61 73 6b 2c 20 6e 6f  abItem, mask, no
3bfe9 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79  tReady, pOrderBy
3bfea 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
3bfeb 20 70 44 69 73 74 2c 20 26 73 43 6f 73 74 29 3b   pDist, &sCost);
3bfec 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
3bfed 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f       assert( isO
3bfee 70 74 69 6d 61 6c 20 7c 7c 20 28 73 43 6f 73 74  ptimal || (sCost
3bfef 2e 75 73 65 64 26 6e 6f 74 52 65 61 64 79 29 3d  .used&notReady)=
3bff0 3d 30 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  =0 );....       
3bff1 20 2f 2a 20 49 66 20 61 6e 20 49 4e 44 45 58 45   /* If an INDEXE
3bff2 44 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 70  D BY clause is p
3bff3 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65  resent, then the
3bff4 20 70 6c 61 6e 20 6d 75 73 74 20 75 73 65 20 74   plan must use t
3bff5 68 61 74 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  hat..        ** 
3bff6 69 6e 64 65 78 20 69 66 20 69 74 20 75 73 65 73  index if it uses
3bff7 20 61 6e 79 20 69 6e 64 65 78 20 61 74 20 61 6c   any index at al
3bff8 6c 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 61 73  l */..        as
3bff9 73 65 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e  sert( pTabItem->
3bffa 70 49 6e 64 65 78 3d 3d 30 20 0d 0a 20 20 20 20  pIndex==0 ..    
3bffb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
3bffc 20 28 73 43 6f 73 74 2e 70 6c 61 6e 2e 77 73 46   (sCost.plan.wsF
3bffd 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4e 4f 54  lags & WHERE_NOT
3bffe 5f 46 55 4c 4c 53 43 41 4e 29 3d 3d 30 0d 0a 20  _FULLSCAN)==0.. 
3bfff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c000 20 7c 7c 20 73 43 6f 73 74 2e 70 6c 61 6e 2e 75   || sCost.plan.u
3c001 2e 70 49 64 78 3d 3d 70 54 61 62 49 74 65 6d 2d  .pIdx==pTabItem-
3c002 3e 70 49 6e 64 65 78 20 29 3b 0d 0a 0d 0a 20 20  >pIndex );....  
3c003 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 74 69        if( isOpti
3c004 6d 61 6c 20 26 26 20 28 73 43 6f 73 74 2e 70 6c  mal && (sCost.pl
3c005 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3c006 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29  RE_NOT_FULLSCAN)
3c007 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
3c008 20 20 6e 6f 74 49 6e 64 65 78 65 64 20 7c 3d 20    notIndexed |= 
3c009 6d 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d  m;..        }...
3c00a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 64  .        /* Cond
3c00b 69 74 69 6f 6e 73 20 75 6e 64 65 72 20 77 68 69  itions under whi
3c00c 63 68 20 74 68 69 73 20 74 61 62 6c 65 20 62 65  ch this table be
3c00d 63 6f 6d 65 73 20 74 68 65 20 62 65 73 74 20 73  comes the best s
3c00e 6f 20 66 61 72 3a 0d 0a 20 20 20 20 20 20 20 20  o far:..        
3c00f 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  **..        **  
3c010 20 28 31 29 20 54 68 65 20 74 61 62 6c 65 20 6d   (1) The table m
3c011 75 73 74 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f  ust not depend o
3c012 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20 74  n other tables t
3c013 68 61 74 20 68 61 76 65 20 6e 6f 74 0d 0a 20 20  hat have not..  
3c014 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 79        **       y
3c015 65 74 20 72 75 6e 2e 0d 0a 20 20 20 20 20 20 20  et run...       
3c016 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20   **..        ** 
3c017 20 20 28 32 29 20 41 20 66 75 6c 6c 2d 74 61 62    (2) A full-tab
3c018 6c 65 2d 73 63 61 6e 20 70 6c 61 6e 20 63 61 6e  le-scan plan can
3c019 6e 6f 74 20 73 75 70 65 72 63 65 64 65 20 69 6e  not supercede in
3c01a 64 65 78 65 64 20 70 6c 61 6e 20 75 6e 6c 65 73  dexed plan unles
3c01b 73 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  s..        **   
3c01c 20 20 20 20 74 68 65 20 66 75 6c 6c 2d 74 61 62      the full-tab
3c01d 6c 65 2d 73 63 61 6e 20 69 73 20 61 6e 20 22 6f  le-scan is an "o
3c01e 70 74 69 6d 61 6c 22 20 70 6c 61 6e 20 61 73 20  ptimal" plan as 
3c01f 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2e 0d 0a  defined above...
3c020 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20          **..    
3c021 20 20 20 20 2a 2a 20 20 20 28 33 29 20 41 6c 6c      **   (3) All
3c022 20 74 61 62 6c 65 73 20 68 61 76 65 20 61 6e 20   tables have an 
3c023 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3c024 65 20 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20  e or this table 
3c025 6c 61 63 6b 73 20 61 6e 0d 0a 20 20 20 20 20 20  lacks an..      
3c026 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 44 45 58    **       INDEX
3c027 45 44 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20  ED BY clause or 
3c028 74 68 69 73 20 74 61 62 6c 65 20 75 73 65 73 20  this table uses 
3c029 74 68 65 20 73 70 65 63 69 66 69 63 0d 0a 20 20  the specific..  
3c02a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 69        **       i
3c02b 6e 64 65 78 20 73 70 65 63 69 66 69 65 64 20 62  ndex specified b
3c02c 79 20 69 74 73 20 49 4e 44 45 58 45 44 20 42 59  y its INDEXED BY
3c02d 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 72   clause.  This r
3c02e 75 6c 65 20 65 6e 73 75 72 65 73 0d 0a 20 20 20  ule ensures..   
3c02f 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 74 68       **       th
3c030 61 74 20 61 20 62 65 73 74 2d 73 6f 2d 66 61 72  at a best-so-far
3c031 20 69 73 20 61 6c 77 61 79 73 20 73 65 6c 65 63   is always selec
3c032 74 65 64 20 65 76 65 6e 20 69 66 20 61 6e 20 69  ted even if an i
3c033 6d 70 6f 73 73 69 62 6c 65 0d 0a 20 20 20 20 20  mpossible..     
3c034 20 20 20 2a 2a 20 20 20 20 20 20 20 63 6f 6d 62     **       comb
3c035 69 6e 61 74 69 6f 6e 20 6f 66 20 49 4e 44 45 58  ination of INDEX
3c036 45 44 20 42 59 20 63 6c 61 75 73 65 73 20 61 72  ED BY clauses ar
3c037 65 20 67 69 76 65 6e 2e 20 20 54 68 65 20 65 72  e given.  The er
3c038 72 6f 72 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ror..        ** 
3c039 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 64 65        will be de
3c03a 74 65 63 74 65 64 20 61 6e 64 20 72 65 6c 61 79  tected and relay
3c03b 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 61  ed back to the a
3c03c 70 70 6c 69 63 61 74 69 6f 6e 20 6c 61 74 65 72  pplication later
3c03d 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  ...        **   
3c03e 20 20 20 20 54 68 65 20 4e 45 56 45 52 28 29 20      The NEVER() 
3c03f 63 6f 6d 65 73 20 61 62 6f 75 74 20 62 65 63 61  comes about beca
3c040 75 73 65 20 72 75 6c 65 20 28 32 29 20 61 62 6f  use rule (2) abo
3c041 76 65 20 70 72 65 76 65 6e 74 73 0d 0a 20 20 20  ve prevents..   
3c042 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 41 6e       **       An
3c043 20 69 6e 64 65 78 61 62 6c 65 20 66 75 6c 6c 2d   indexable full-
3c044 74 61 62 6c 65 2d 73 63 61 6e 20 66 72 6f 6d 20  table-scan from 
3c045 72 65 61 63 68 69 6e 67 20 72 75 6c 65 20 28 33  reaching rule (3
3c046 29 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a  )...        **..
3c047 20 20 20 20 20 20 20 20 2a 2a 20 20 20 28 34 29          **   (4)
3c048 20 54 68 65 20 70 6c 61 6e 20 63 6f 73 74 20 6d   The plan cost m
3c049 75 73 74 20 62 65 20 6c 6f 77 65 72 20 74 68 61  ust be lower tha
3c04a 6e 20 70 72 69 6f 72 20 70 6c 61 6e 73 20 6f 72  n prior plans or
3c04b 20 65 6c 73 65 20 74 68 65 0d 0a 20 20 20 20 20   else the..     
3c04c 20 20 20 2a 2a 20 20 20 20 20 20 20 63 6f 73 74     **       cost
3c04d 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
3c04e 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72  e and the number
3c04f 20 6f 66 20 72 6f 77 73 20 6d 75 73 74 20 62 65   of rows must be
3c050 20 6c 6f 77 65 72 2e 0d 0a 20 20 20 20 20 20 20   lower...       
3c051 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28   */..        if(
3c052 20 28 73 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74   (sCost.used&not
3c053 52 65 61 64 79 29 3d 3d 30 20 20 20 20 20 20 20  Ready)==0       
3c054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c055 2f 2a 20 28 31 29 20 2a 2f 0d 0a 20 20 20 20 20  /* (1) */..     
3c056 20 20 20 20 20 20 20 26 26 20 28 62 65 73 74 4a         && (bestJ
3c057 3c 30 20 7c 7c 20 28 6e 6f 74 49 6e 64 65 78 65  <0 || (notIndexe
3c058 64 26 6d 29 21 3d 30 20 20 20 20 20 20 20 20 20  d&m)!=0         
3c059 20 20 20 20 20 20 2f 2a 20 28 32 29 20 2a 2f 0d        /* (2) */.
3c05a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3c05b 20 7c 7c 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c   || (bestPlan.pl
3c05c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3c05d 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29  RE_NOT_FULLSCAN)
3c05e 3d 3d 30 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ==0..           
3c05f 20 20 20 20 20 7c 7c 20 28 73 43 6f 73 74 2e 70       || (sCost.p
3c060 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3c061 45 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e  ERE_NOT_FULLSCAN
3c062 29 21 3d 30 29 0d 0a 20 20 20 20 20 20 20 20 20  )!=0)..         
3c063 20 20 20 26 26 20 28 6e 55 6e 63 6f 6e 73 74 72     && (nUnconstr
3c064 61 69 6e 65 64 3d 3d 30 20 7c 7c 20 70 54 61 62  ained==0 || pTab
3c065 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3d 3d 30 20  Item->pIndex==0 
3c066 20 20 2f 2a 20 28 33 29 20 2a 2f 0d 0a 20 20 20    /* (3) */..   
3c067 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
3c068 4e 45 56 45 52 28 28 73 43 6f 73 74 2e 70 6c 61  NEVER((sCost.pla
3c069 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
3c06a 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29 21  E_NOT_FULLSCAN)!
3c06b 3d 30 29 29 0d 0a 20 20 20 20 20 20 20 20 20 20  =0))..          
3c06c 20 20 26 26 20 28 62 65 73 74 4a 3c 30 20 7c 7c    && (bestJ<0 ||
3c06d 20 73 43 6f 73 74 2e 72 43 6f 73 74 3c 62 65 73   sCost.rCost<bes
3c06e 74 50 6c 61 6e 2e 72 43 6f 73 74 20 20 20 20 20  tPlan.rCost     
3c06f 20 2f 2a 20 28 34 29 20 2a 2f 0d 0a 20 20 20 20   /* (4) */..    
3c070 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28              || (
3c071 73 43 6f 73 74 2e 72 43 6f 73 74 3c 3d 62 65 73  sCost.rCost<=bes
3c072 74 50 6c 61 6e 2e 72 43 6f 73 74 20 0d 0a 20 20  tPlan.rCost ..  
3c073 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
3c074 26 20 73 43 6f 73 74 2e 70 6c 61 6e 2e 6e 52 6f  & sCost.plan.nRo
3c075 77 3c 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e  w<bestPlan.plan.
3c076 6e 52 6f 77 29 29 0d 0a 20 20 20 20 20 20 20 20  nRow))..        
3c077 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 57 48  ){..          WH
3c078 45 52 45 54 52 41 43 45 28 28 22 3d 3d 3d 20 74  ERETRACE(("=== t
3c079 61 62 6c 65 20 25 64 20 69 73 20 62 65 73 74 20  able %d is best 
3c07a 73 6f 20 66 61 72 22 0d 0a 20 20 20 20 20 20 20  so far"..       
3c07b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
3c07c 20 77 69 74 68 20 63 6f 73 74 3d 25 67 20 61 6e   with cost=%g an
3c07d 64 20 6e 52 6f 77 3d 25 67 5c 6e 22 2c 0d 0a 20  d nRow=%g\n",.. 
3c07e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c07f 20 20 20 20 20 6a 2c 20 73 43 6f 73 74 2e 72 43       j, sCost.rC
3c080 6f 73 74 2c 20 73 43 6f 73 74 2e 70 6c 61 6e 2e  ost, sCost.plan.
3c081 6e 52 6f 77 29 29 3b 0d 0a 20 20 20 20 20 20 20  nRow));..       
3c082 20 20 20 62 65 73 74 50 6c 61 6e 20 3d 20 73 43     bestPlan = sC
3c083 6f 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ost;..          
3c084 62 65 73 74 4a 20 3d 20 6a 3b 0d 0a 20 20 20 20  bestJ = j;..    
3c085 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
3c086 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20  f( doNotReorder 
3c087 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
3c088 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 73  }..    }..    as
3c089 73 65 72 74 28 20 62 65 73 74 4a 3e 3d 30 20 29  sert( bestJ>=0 )
3c08a 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  ;..    assert( n
3c08b 6f 74 52 65 61 64 79 20 26 20 67 65 74 4d 61 73  otReady & getMas
3c08c 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
3c08d 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69  List->a[bestJ].i
3c08e 43 75 72 73 6f 72 29 20 29 3b 0d 0a 20 20 20 20  Cursor) );..    
3c08f 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a  WHERETRACE(("***
3c090 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c 65 63   Optimizer selec
3c091 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f 72 20  ts table %d for 
3c092 6c 6f 6f 70 20 25 64 22 0d 0a 20 20 20 20 20 20  loop %d"..      
3c093 20 20 20 20 20 20 20 20 20 20 22 20 77 69 74 68            " with
3c094 20 63 6f 73 74 3d 25 67 20 61 6e 64 20 6e 52 6f   cost=%g and nRo
3c095 77 3d 25 67 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  w=%g\n",..      
3c096 20 20 20 20 20 20 20 20 20 20 62 65 73 74 4a 2c            bestJ,
3c097 20 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e   pLevel-pWInfo->
3c098 61 2c 20 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73  a, bestPlan.rCos
3c099 74 2c 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  t, bestPlan.plan
3c09a 2e 6e 52 6f 77 29 29 3b 0d 0a 20 20 20 20 2f 2a  .nRow));..    /*
3c09b 20 54 68 65 20 41 4c 57 41 59 53 28 29 20 74 68   The ALWAYS() th
3c09c 61 74 20 66 6f 6c 6c 6f 77 73 20 77 61 73 20 61  at follows was a
3c09d 64 64 65 64 20 74 6f 20 68 75 73 68 20 75 70 20  dded to hush up 
3c09e 63 6c 61 6e 67 20 73 63 61 6e 2d 62 75 69 6c 64  clang scan-build
3c09f 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 28 62 65   */..    if( (be
3c0a0 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
3c0a1 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45  ags & WHERE_ORDE
3c0a2 52 42 59 29 21 3d 30 20 26 26 20 41 4c 57 41 59  RBY)!=0 && ALWAY
3c0a3 53 28 70 70 4f 72 64 65 72 42 79 29 20 29 7b 0d  S(ppOrderBy) ){.
3c0a4 0a 20 20 20 20 20 20 2a 70 70 4f 72 64 65 72 42  .      *ppOrderB
3c0a5 79 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20  y = 0;..    }.. 
3c0a6 20 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e     if( (bestPlan
3c0a7 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  .plan.wsFlags & 
3c0a8 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 29 21  WHERE_DISTINCT)!
3c0a9 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73  =0 ){..      ass
3c0aa 65 72 74 28 20 70 57 49 6e 66 6f 2d 3e 65 44 69  ert( pWInfo->eDi
3c0ab 73 74 69 6e 63 74 3d 3d 30 20 29 3b 0d 0a 20 20  stinct==0 );..  
3c0ac 20 20 20 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73      pWInfo->eDis
3c0ad 74 69 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49  tinct = WHERE_DI
3c0ae 53 54 49 4e 43 54 5f 4f 52 44 45 52 45 44 3b 0d  STINCT_ORDERED;.
3c0af 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 6e 64 46  .    }..    andF
3c0b0 6c 61 67 73 20 26 3d 20 62 65 73 74 50 6c 61 6e  lags &= bestPlan
3c0b1 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 3b 0d 0a  .plan.wsFlags;..
3c0b2 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e      pLevel->plan
3c0b3 20 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e   = bestPlan.plan
3c0b4 3b 0d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  ;..    testcase(
3c0b5 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77   bestPlan.plan.w
3c0b6 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
3c0b7 4e 44 45 58 45 44 20 29 3b 0d 0a 20 20 20 20 74  NDEXED );..    t
3c0b8 65 73 74 63 61 73 65 28 20 62 65 73 74 50 6c 61  estcase( bestPla
3c0b9 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  n.plan.wsFlags &
3c0ba 20 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45   WHERE_TEMP_INDE
3c0bb 58 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 62 65  X );..    if( be
3c0bc 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
3c0bd 61 67 73 20 26 20 28 57 48 45 52 45 5f 49 4e 44  ags & (WHERE_IND
3c0be 45 58 45 44 7c 57 48 45 52 45 5f 54 45 4d 50 5f  EXED|WHERE_TEMP_
3c0bf 49 4e 44 45 58 29 20 29 7b 0d 0a 20 20 20 20 20  INDEX) ){..     
3c0c0 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
3c0c1 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
3c0c2 2b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  +;..    }else{..
3c0c3 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49        pLevel->iI
3c0c4 64 78 43 75 72 20 3d 20 2d 31 3b 0d 0a 20 20 20  dxCur = -1;..   
3c0c5 20 7d 0d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79   }..    notReady
3c0c6 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61   &= ~getMask(pMa
3c0c7 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
3c0c8 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f  >a[bestJ].iCurso
3c0c9 72 29 3b 0d 0a 20 20 20 20 70 4c 65 76 65 6c 2d  r);..    pLevel-
3c0ca 3e 69 46 72 6f 6d 20 3d 20 28 75 38 29 62 65 73  >iFrom = (u8)bes
3c0cb 74 4a 3b 0d 0a 20 20 20 20 69 66 28 20 62 65 73  tJ;..    if( bes
3c0cc 74 50 6c 61 6e 2e 70 6c 61 6e 2e 6e 52 6f 77 3e  tPlan.plan.nRow>
3c0cd 3d 28 64 6f 75 62 6c 65 29 31 20 29 7b 0d 0a 20  =(double)1 ){.. 
3c0ce 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 51 75       pParse->nQu
3c0cf 65 72 79 4c 6f 6f 70 20 2a 3d 20 62 65 73 74 50  eryLoop *= bestP
3c0d0 6c 61 6e 2e 70 6c 61 6e 2e 6e 52 6f 77 3b 0d 0a  lan.plan.nRow;..
3c0d1 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
3c0d2 43 68 65 63 6b 20 74 68 61 74 20 69 66 20 74 68  Check that if th
3c0d3 65 20 74 61 62 6c 65 20 73 63 61 6e 6e 65 64 20  e table scanned 
3c0d4 62 79 20 74 68 69 73 20 6c 6f 6f 70 20 69 74 65  by this loop ite
3c0d5 72 61 74 69 6f 6e 20 68 61 64 20 61 6e 0d 0a 20  ration had an.. 
3c0d6 20 20 20 2a 2a 20 49 4e 44 45 58 45 44 20 42 59     ** INDEXED BY
3c0d7 20 63 6c 61 75 73 65 20 61 74 74 61 63 68 65 64   clause attached
3c0d8 20 74 6f 20 69 74 2c 20 74 68 61 74 20 74 68 65   to it, that the
3c0d9 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 69 73 20   named index is 
3c0da 62 65 69 6e 67 0d 0a 20 20 20 20 2a 2a 20 75 73  being..    ** us
3c0db 65 64 20 66 6f 72 20 74 68 65 20 73 63 61 6e 2e  ed for the scan.
3c0dc 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 71 75   If not, then qu
3c0dd 65 72 79 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ery compilation 
3c0de 68 61 73 20 66 61 69 6c 65 64 2e 0d 0a 20 20 20  has failed...   
3c0df 20 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72   ** Return an er
3c0e0 72 6f 72 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ror...    */..  
3c0e1 20 20 70 49 64 78 20 3d 20 70 54 61 62 4c 69 73    pIdx = pTabLis
3c0e2 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70 49 6e 64  t->a[bestJ].pInd
3c0e3 65 78 3b 0d 0a 20 20 20 20 69 66 28 20 70 49 64  ex;..    if( pId
3c0e4 78 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  x ){..      if( 
3c0e5 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77  (bestPlan.plan.w
3c0e6 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
3c0e7 4e 44 45 58 45 44 29 3d 3d 30 20 29 7b 0d 0a 20  NDEXED)==0 ){.. 
3c0e8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
3c0e9 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
3c0ea 63 61 6e 6e 6f 74 20 75 73 65 20 69 6e 64 65 78  cannot use index
3c0eb 3a 20 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61  : %s", pIdx->zNa
3c0ec 6d 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f  me);..        go
3c0ed 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72  to whereBeginErr
3c0ee 6f 72 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65  or;..      }else
3c0ef 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  {..        /* If
3c0f0 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63   an INDEXED BY c
3c0f1 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c 20 74  lause is used, t
3c0f2 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 66  he bestIndex() f
3c0f3 75 6e 63 74 69 6f 6e 20 69 73 0d 0a 20 20 20 20  unction is..    
3c0f4 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65      ** guarantee
3c0f5 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69 6e  d to find the in
3c0f6 64 65 78 20 73 70 65 63 69 66 69 65 64 20 69 6e  dex specified in
3c0f7 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20   the INDEXED BY 
3c0f8 63 6c 61 75 73 65 0d 0a 20 20 20 20 20 20 20 20  clause..        
3c0f9 2a 2a 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e  ** if it find an
3c0fa 20 69 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a   index at all. *
3c0fb 2f 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  /..        asser
3c0fc 74 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  t( bestPlan.plan
3c0fd 2e 75 2e 70 49 64 78 3d 3d 70 49 64 78 20 29 3b  .u.pIdx==pIdx );
3c0fe 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
3c0ff 0d 0a 20 20 7d 0d 0a 20 20 57 48 45 52 45 54 52  ..  }..  WHERETR
3c100 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69  ACE(("*** Optimi
3c101 7a 65 72 20 46 69 6e 69 73 68 65 64 20 2a 2a 2a  zer Finished ***
3c102 5c 6e 22 29 29 3b 0d 0a 20 20 69 66 28 20 70 50  \n"));..  if( pP
3c103 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
3c104 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
3c105 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72  {..    goto wher
3c106 65 42 65 67 69 6e 45 72 72 6f 72 3b 0d 0a 20 20  eBeginError;..  
3c107 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65  }....  /* If the
3c108 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f 6e 6c   total query onl
3c109 79 20 73 65 6c 65 63 74 73 20 61 20 73 69 6e 67  y selects a sing
3c10a 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74 68 65  le row, then the
3c10b 20 4f 52 44 45 52 20 42 59 0d 0a 20 20 2a 2a 20   ORDER BY..  ** 
3c10c 63 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c 65  clause is irrele
3c10d 76 61 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  vant...  */..  i
3c10e 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57  f( (andFlags & W
3c10f 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20  HERE_UNIQUE)!=0 
3c110 26 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0d  && ppOrderBy ){.
3c111 0a 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20  .    *ppOrderBy 
3c112 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  = 0;..  }....  /
3c113 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
3c114 69 73 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20  is an UPDATE or 
3c115 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
3c116 20 74 68 61 74 20 69 73 20 72 65 71 75 65 73 74   that is request
3c117 69 6e 67 0d 0a 20 20 2a 2a 20 74 6f 20 75 73 65  ing..  ** to use
3c118 20 61 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f   a one-pass algo
3c119 72 69 74 68 6d 2c 20 64 65 74 65 72 6d 69 6e 65  rithm, determine
3c11a 20 69 66 20 74 68 69 73 20 69 73 20 61 70 70 72   if this is appr
3c11b 6f 70 72 69 61 74 65 2e 0d 0a 20 20 2a 2a 20 54  opriate...  ** T
3c11c 68 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f  he one-pass algo
3c11d 72 69 74 68 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73  rithm only works
3c11e 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   if the WHERE cl
3c11f 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
3c120 0d 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65  ..  ** the state
3c121 6d 65 6e 74 20 74 6f 20 75 70 64 61 74 65 20 61  ment to update a
3c122 20 73 69 6e 67 6c 65 20 72 6f 77 2e 0d 0a 20 20   single row...  
3c123 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 28 77  */..  assert( (w
3c124 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
3c125 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45  E_ONEPASS_DESIRE
3c126 44 29 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f 2d  D)==0 || pWInfo-
3c127 3e 6e 4c 65 76 65 6c 3d 3d 31 20 29 3b 0d 0a 20  >nLevel==1 );.. 
3c128 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
3c129 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53   & WHERE_ONEPASS
3c12a 5f 44 45 53 49 52 45 44 29 21 3d 30 20 26 26 20  _DESIRED)!=0 && 
3c12b 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52  (andFlags & WHER
3c12c 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 29 7b 0d  E_UNIQUE)!=0 ){.
3c12d 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f  .    pWInfo->okO
3c12e 6e 65 50 61 73 73 20 3d 20 31 3b 0d 0a 20 20 20  nePass = 1;..   
3c12f 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 6c   pWInfo->a[0].pl
3c130 61 6e 2e 77 73 46 6c 61 67 73 20 26 3d 20 7e 57  an.wsFlags &= ~W
3c131 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0d 0a  HERE_IDX_ONLY;..
3c132 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4f 70 65 6e    }....  /* Open
3c133 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74   all tables in t
3c134 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20  he pTabList and 
3c135 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c 65  any indices sele
3c136 63 74 65 64 20 66 6f 72 0d 0a 20 20 2a 2a 20 73  cted for..  ** s
3c137 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74  earching those t
3c138 61 62 6c 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  ables...  */..  
3c139 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
3c13a 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
3c13b 2d 31 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74  -1); /* Insert t
3c13c 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69  he cookie verifi
3c13d 65 72 20 47 6f 74 6f 20 2a 2f 0d 0a 20 20 6e 6f  er Goto */..  no
3c13e 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61  tReady = ~(Bitma
3c13f 73 6b 29 30 3b 0d 0a 20 20 70 57 49 6e 66 6f 2d  sk)0;..  pWInfo-
3c140 3e 6e 52 6f 77 4f 75 74 20 3d 20 28 64 6f 75 62  >nRowOut = (doub
3c141 6c 65 29 31 3b 0d 0a 20 20 66 6f 72 28 69 3d 30  le)1;..  for(i=0
3c142 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d  , pLevel=pWInfo-
3c143 3e 61 3b 20 69 3c 6e 54 61 62 4c 69 73 74 3b 20  >a; i<nTabList; 
3c144 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0d  i++, pLevel++){.
3c145 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
3c146 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ;     /* Table t
3c147 6f 20 6f 70 65 6e 20 2a 2f 0d 0a 20 20 20 20 69  o open */..    i
3c148 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
3c149 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
3c14a 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
3c14b 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0d 0a  table/index */..
3c14c 0d 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d  ..    pTabItem =
3c14d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c   &pTabList->a[pL
3c14e 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0d 0a 20  evel->iFrom];.. 
3c14f 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 49 74     pTab = pTabIt
3c150 65 6d 2d 3e 70 54 61 62 3b 0d 0a 20 20 20 20 70  em->pTab;..    p
3c151 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d  Level->iTabCur =
3c152 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
3c153 6f 72 3b 0d 0a 20 20 20 20 70 57 49 6e 66 6f 2d  or;..    pWInfo-
3c154 3e 6e 52 6f 77 4f 75 74 20 2a 3d 20 70 4c 65 76  >nRowOut *= pLev
3c155 65 6c 2d 3e 70 6c 61 6e 2e 6e 52 6f 77 3b 0d 0a  el->plan.nRow;..
3c156 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
3c157 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
3c158 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
3c159 29 3b 0d 0a 20 20 20 20 69 66 28 20 28 70 54 61  );..    if( (pTa
3c15a 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
3c15b 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c  _Ephemeral)!=0 |
3c15c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  | pTab->pSelect 
3c15d 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20  ){..      /* Do 
3c15e 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 20  nothing */..    
3c15f 7d 65 6c 73 65 0d 0a 23 69 66 6e 64 65 66 20 53  }else..#ifndef S
3c160 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
3c161 41 4c 54 41 42 4c 45 0d 0a 20 20 20 20 69 66 28  ALTABLE..    if(
3c162 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
3c163 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
3c164 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
3c165 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ){..      const 
3c166 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63  char *pVTab = (c
3c167 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
3c168 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
3c169 20 70 54 61 62 29 3b 0d 0a 20 20 20 20 20 20 69   pTab);..      i
3c16a 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62 49 74  nt iCur = pTabIt
3c16b 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0d 0a 20 20  em->iCursor;..  
3c16c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3c16d 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65  ddOp4(v, OP_VOpe
3c16e 6e 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20 70  n, iCur, 0, 0, p
3c16f 56 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0d  VTab, P4_VTAB);.
3c170 0a 20 20 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64  .    }else..#end
3c171 69 66 0d 0a 20 20 20 20 69 66 28 20 28 70 4c 65  if..    if( (pLe
3c172 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
3c173 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
3c174 4c 59 29 3d 3d 30 0d 0a 20 20 20 20 20 20 20 20  LY)==0..        
3c175 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73 20   && (wctrlFlags 
3c176 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45  & WHERE_OMIT_OPE
3c177 4e 5f 43 4c 4f 53 45 29 3d 3d 30 20 29 7b 0d 0a  N_CLOSE)==0 ){..
3c178 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70        int op = p
3c179 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
3c17a 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20   ? OP_OpenWrite 
3c17b 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3b 0d 0a  : OP_OpenRead;..
3c17c 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
3c17d 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  nTable(pParse, p
3c17e 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
3c17f 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29  , iDb, pTab, op)
3c180 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
3c181 65 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 42  e( pTab->nCol==B
3c182 4d 53 2d 31 20 29 3b 0d 0a 20 20 20 20 20 20 74  MS-1 );..      t
3c183 65 73 74 63 61 73 65 28 20 70 54 61 62 2d 3e 6e  estcase( pTab->n
3c184 43 6f 6c 3d 3d 42 4d 53 20 29 3b 0d 0a 20 20 20  Col==BMS );..   
3c185 20 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e     if( !pWInfo->
3c186 6f 6b 4f 6e 65 50 61 73 73 20 26 26 20 70 54 61  okOnePass && pTa
3c187 62 2d 3e 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0d 0a  b->nCol<BMS ){..
3c188 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20          Bitmask 
3c189 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f  b = pTabItem->co
3c18a 6c 55 73 65 64 3b 0d 0a 20 20 20 20 20 20 20 20  lUsed;..        
3c18b 69 6e 74 20 6e 20 3d 20 30 3b 0d 0a 20 20 20 20  int n = 0;..    
3c18c 20 20 20 20 66 6f 72 28 3b 20 62 3b 20 62 3d 62      for(; b; b=b
3c18d 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0d 0a 20 20 20  >>1, n++){}..   
3c18e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3c18f 43 68 61 6e 67 65 50 34 28 76 2c 20 73 71 6c 69  ChangeP4(v, sqli
3c190 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
3c191 64 72 28 76 29 2d 31 2c 20 0d 0a 20 20 20 20 20  dr(v)-1, ..     
3c192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c193 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e         SQLITE_IN
3c194 54 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50 34 5f  T_TO_PTR(n), P4_
3c195 49 4e 54 33 32 29 3b 0d 0a 20 20 20 20 20 20 20  INT32);..       
3c196 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62   assert( n<=pTab
3c197 2d 3e 6e 43 6f 6c 20 29 3b 0d 0a 20 20 20 20 20  ->nCol );..     
3c198 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a   }..    }else{..
3c199 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
3c19a 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
3c19b 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
3c19c 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
3c19d 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66  ..    }..#ifndef
3c19e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
3c19f 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0d 0a 20 20  OMATIC_INDEX..  
3c1a0 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70    if( (pLevel->p
3c1a1 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3c1a2 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58 29 21  ERE_TEMP_INDEX)!
3c1a3 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e  =0 ){..      con
3c1a4 73 74 72 75 63 74 41 75 74 6f 6d 61 74 69 63 49  structAutomaticI
3c1a5 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43  ndex(pParse, pWC
3c1a6 2c 20 70 54 61 62 49 74 65 6d 2c 20 6e 6f 74 52  , pTabItem, notR
3c1a7 65 61 64 79 2c 20 70 4c 65 76 65 6c 29 3b 0d 0a  eady, pLevel);..
3c1a8 20 20 20 20 7d 65 6c 73 65 0d 0a 23 65 6e 64 69      }else..#endi
3c1a9 66 0d 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76  f..    if( (pLev
3c1aa 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3c1ab 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
3c1ac 29 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 49  )!=0 ){..      I
3c1ad 6e 64 65 78 20 2a 70 49 78 20 3d 20 70 4c 65 76  ndex *pIx = pLev
3c1ae 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b  el->plan.u.pIdx;
3c1af 0d 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ..      KeyInfo 
3c1b0 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
3c1b1 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
3c1b2 73 65 2c 20 70 49 78 29 3b 0d 0a 20 20 20 20 20  se, pIx);..     
3c1b3 20 69 6e 74 20 69 49 64 78 43 75 72 20 3d 20 70   int iIdxCur = p
3c1b4 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0d  Level->iIdxCur;.
3c1b5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
3c1b6 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  Ix->pSchema==pTa
3c1b7 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0d 0a 20  b->pSchema );.. 
3c1b8 20 20 20 20 20 61 73 73 65 72 74 28 20 69 49 64       assert( iId
3c1b9 78 43 75 72 3e 3d 30 20 29 3b 0d 0a 20 20 20 20  xCur>=0 );..    
3c1ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3c1bb 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op4(v, OP_OpenRe
3c1bc 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 78  ad, iIdxCur, pIx
3c1bd 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0d 0a 20 20  ->tnum, iDb,..  
3c1be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c1bf 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
3c1c0 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
3c1c1 4e 44 4f 46 46 29 3b 0d 0a 20 20 20 20 20 20 56  NDOFF);..      V
3c1c2 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
3c1c3 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65 29  %s", pIx->zName)
3c1c4 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73  );..    }..    s
3c1c5 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
3c1c6 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
3c1c7 44 62 29 3b 0d 0a 20 20 20 20 6e 6f 74 52 65 61  Db);..    notRea
3c1c8 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70  dy &= ~getMask(p
3c1c9 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 70 54  WC->pMaskSet, pT
3c1ca 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29  abItem->iCursor)
3c1cb 3b 0d 0a 20 20 7d 0d 0a 20 20 70 57 49 6e 66 6f  ;..  }..  pWInfo
3c1cc 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  ->iTop = sqlite3
3c1cd 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
3c1ce 76 29 3b 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  v);..  if( db->m
3c1cf 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
3c1d0 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72  to whereBeginErr
3c1d1 6f 72 3b 0d 0a 0d 0a 20 20 2f 2a 20 47 65 6e 65  or;....  /* Gene
3c1d2 72 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f  rate the code to
3c1d3 20 64 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20   do the search. 
3c1d4 20 45 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20   Each iteration 
3c1d5 6f 66 20 74 68 65 20 66 6f 72 0d 0a 20 20 2a 2a  of the for..  **
3c1d6 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65   loop below gene
3c1d7 72 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20 61  rates code for a
3c1d8 20 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c   single nested l
3c1d9 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d 0d 0a 20  oop of the VM.. 
3c1da 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0d 0a 20 20   ** program...  
3c1db 2a 2f 0d 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d  */..  notReady =
3c1dc 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0d 0a 20   ~(Bitmask)0;.. 
3c1dd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 61 62   for(i=0; i<nTab
3c1de 4c 69 73 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  List; i++){..   
3c1df 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66   pLevel = &pWInf
3c1e0 6f 2d 3e 61 5b 69 5d 3b 0d 0a 20 20 20 20 65 78  o->a[i];..    ex
3c1e1 70 6c 61 69 6e 4f 6e 65 53 63 61 6e 28 70 50 61  plainOneScan(pPa
3c1e2 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
3c1e3 4c 65 76 65 6c 2c 20 69 2c 20 70 4c 65 76 65 6c  Level, i, pLevel
3c1e4 2d 3e 69 46 72 6f 6d 2c 20 77 63 74 72 6c 46 6c  ->iFrom, wctrlFl
3c1e5 61 67 73 29 3b 0d 0a 20 20 20 20 6e 6f 74 52 65  ags);..    notRe
3c1e6 61 64 79 20 3d 20 63 6f 64 65 4f 6e 65 4c 6f 6f  ady = codeOneLoo
3c1e7 70 53 74 61 72 74 28 70 57 49 6e 66 6f 2c 20 69  pStart(pWInfo, i
3c1e8 2c 20 77 63 74 72 6c 46 6c 61 67 73 2c 20 6e 6f  , wctrlFlags, no
3c1e9 74 52 65 61 64 79 2c 20 70 57 68 65 72 65 29 3b  tReady, pWhere);
3c1ea 0d 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43  ..    pWInfo->iC
3c1eb 6f 6e 74 69 6e 75 65 20 3d 20 70 4c 65 76 65 6c  ontinue = pLevel
3c1ec 2d 3e 61 64 64 72 43 6f 6e 74 3b 0d 0a 20 20 7d  ->addrCont;..  }
3c1ed 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
3c1ee 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20 74  E_TEST  /* For t
3c1ef 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
3c1f0 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  ging use only */
3c1f1 0d 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e  ..  /* Record in
3c1f2 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20   the query plan 
3c1f3 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
3c1f4 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  t the current ta
3c1f5 62 6c 65 0d 0a 20 20 2a 2a 20 61 6e 64 20 74 68  ble..  ** and th
3c1f6 65 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f 20  e index used to 
3c1f7 61 63 63 65 73 73 20 69 74 20 28 69 66 20 61 6e  access it (if an
3c1f8 79 29 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c  y).  If the tabl
3c1f9 65 20 69 74 73 65 6c 66 0d 0a 20 20 2a 2a 20 69  e itself..  ** i
3c1fa 73 20 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20  s not used, its 
3c1fb 6e 61 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d  name is just '{}
3c1fc 27 2e 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20  '.  If no index 
3c1fd 69 73 20 75 73 65 64 0d 0a 20 20 2a 2a 20 74 68  is used..  ** th
3c1fe 65 20 69 6e 64 65 78 20 69 73 20 6c 69 73 74 65  e index is liste
3c1ff 64 20 61 73 20 22 7b 7d 22 2e 20 20 49 66 20 74  d as "{}".  If t
3c200 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
3c201 73 20 75 73 65 64 20 74 68 65 0d 0a 20 20 2a 2a  s used the..  **
3c202 20 69 6e 64 65 78 20 6e 61 6d 65 20 69 73 20 27   index name is '
3c203 2a 27 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72  *'...  */..  for
3c204 28 69 3d 30 3b 20 69 3c 6e 54 61 62 4c 69 73 74  (i=0; i<nTabList
3c205 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 63 68 61  ; i++){..    cha
3c206 72 20 2a 7a 3b 0d 0a 20 20 20 20 69 6e 74 20 6e  r *z;..    int n
3c207 3b 0d 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20  ;..    pLevel = 
3c208 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0d 0a  &pWInfo->a[i];..
3c209 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26      pTabItem = &
3c20a 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
3c20b 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0d 0a 20 20 20  el->iFrom];..   
3c20c 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 7a   z = pTabItem->z
3c20d 41 6c 69 61 73 3b 0d 0a 20 20 20 20 69 66 28 20  Alias;..    if( 
3c20e 7a 3d 3d 30 20 29 20 7a 20 3d 20 70 54 61 62 49  z==0 ) z = pTabI
3c20f 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  tem->pTab->zName
3c210 3b 0d 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  ;..    n = sqlit
3c211 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0d 0a  e3Strlen30(z);..
3c212 20 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c 61 6e      if( n+nQPlan
3c213 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65   < sizeof(sqlite
3c214 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d 31 30  3_query_plan)-10
3c215 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70   ){..      if( p
3c216 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3c217 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f  ags & WHERE_IDX_
3c218 4f 4e 4c 59 20 29 7b 0d 0a 20 20 20 20 20 20 20  ONLY ){..       
3c219 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
3c21a 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
3c21b 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b 0d 0a  an], "{}", 2);..
3c21c 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b          nQPlan +
3c21d 3d 20 32 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  = 2;..      }els
3c21e 65 7b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  e{..        memc
3c21f 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72  py(&sqlite3_quer
3c220 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20  y_plan[nQPlan], 
3c221 7a 2c 20 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20  z, n);..        
3c222 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0d 0a 20 20  nQPlan += n;..  
3c223 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c      }..      sql
3c224 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
3c225 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b  nQPlan++] = ' ';
3c226 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 74 65 73  ..    }..    tes
3c227 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
3c228 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3c229 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b 0d  ERE_ROWID_EQ );.
3c22a 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
3c22b 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3c22c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49  ags & WHERE_ROWI
3c22d 44 5f 52 41 4e 47 45 20 29 3b 0d 0a 20 20 20 20  D_RANGE );..    
3c22e 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
3c22f 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
3c230 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
3c231 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b  _ROWID_RANGE) ){
3c232 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ..      memcpy(&
3c233 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
3c234 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22  an[nQPlan], "* "
3c235 2c 20 32 29 3b 0d 0a 20 20 20 20 20 20 6e 51 50  , 2);..      nQP
3c236 6c 61 6e 20 2b 3d 20 32 3b 0d 0a 20 20 20 20 7d  lan += 2;..    }
3c237 65 6c 73 65 20 69 66 28 20 28 70 4c 65 76 65 6c  else if( (pLevel
3c238 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3c239 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21   WHERE_INDEXED)!
3c23a 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 6e 20 3d  =0 ){..      n =
3c23b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
3c23c 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e  (pLevel->plan.u.
3c23d 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 20  pIdx->zName);.. 
3c23e 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c 61       if( n+nQPla
3c23f 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74  n < sizeof(sqlit
3c240 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d 32  e3_query_plan)-2
3c241 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d   ){..        mem
3c242 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
3c243 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
3c244 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
3c245 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b  pIdx->zName, n);
3c246 0d 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e  ..        nQPlan
3c247 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 20 20   += n;..        
3c248 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
3c249 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27  an[nQPlan++] = '
3c24a 20 27 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   ';..      }..  
3c24b 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
3c24c 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f  memcpy(&sqlite3_
3c24d 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
3c24e 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b 0d 0a  n], "{} ", 3);..
3c24f 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20        nQPlan += 
3c250 33 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  3;..    }..  }..
3c251 20 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e    while( nQPlan>
3c252 30 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65  0 && sqlite3_que
3c253 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31  ry_plan[nQPlan-1
3c254 5d 3d 3d 27 20 27 20 29 7b 0d 0a 20 20 20 20 73  ]==' ' ){..    s
3c255 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
3c256 6e 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b  n[--nQPlan] = 0;
3c257 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
3c258 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
3c259 61 6e 5d 20 3d 20 30 3b 0d 0a 20 20 6e 51 50 6c  an] = 0;..  nQPl
3c25a 61 6e 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 20  an = 0;..#endif 
3c25b 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2f  /* SQLITE_TEST /
3c25c 2f 20 54 65 73 74 69 6e 67 20 61 6e 64 20 64 65  / Testing and de
3c25d 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79  bugging use only
3c25e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 52 65 63 6f   */....  /* Reco
3c25f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74  rd the continuat
3c260 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20 74  ion address in t
3c261 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  he WhereInfo str
3c262 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0d 0a 20  ucture.  Then.. 
3c263 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64   ** clean up and
3c264 20 72 65 74 75 72 6e 2e 0d 0a 20 20 2a 2f 0d 0a   return...  */..
3c265 20 20 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b    return pWInfo;
3c266 0d 0a 0d 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65  ....  /* Jump he
3c267 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  re if malloc fai
3c268 6c 73 20 2a 2f 0d 0a 77 68 65 72 65 42 65 67 69  ls */..whereBegi
3c269 6e 45 72 72 6f 72 3a 0d 0a 20 20 69 66 28 20 70  nError:..  if( p
3c26a 57 49 6e 66 6f 20 29 7b 0d 0a 20 20 20 20 70 50  WInfo ){..    pP
3c26b 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70  arse->nQueryLoop
3c26c 20 3d 20 70 57 49 6e 66 6f 2d 3e 73 61 76 65 64   = pWInfo->saved
3c26d 4e 51 75 65 72 79 4c 6f 6f 70 3b 0d 0a 20 20 20  NQueryLoop;..   
3c26e 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 64   whereInfoFree(d
3c26f 62 2c 20 70 57 49 6e 66 6f 29 3b 0d 0a 20 20 7d  b, pWInfo);..  }
3c270 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
3c271 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72  ..../*..** Gener
3c272 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ate the end of t
3c273 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20  he WHERE loop.  
3c274 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20  See comments on 
3c275 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72  ..** sqlite3Wher
3c276 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61 64 64  eBegin() for add
3c277 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
3c278 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ion...*/..SQLITE
3c279 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
3c27a 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68  lite3WhereEnd(Wh
3c27b 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29  ereInfo *pWInfo)
3c27c 7b 0d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  {..  Parse *pPar
3c27d 73 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61  se = pWInfo->pPa
3c27e 72 73 65 3b 0d 0a 20 20 56 64 62 65 20 2a 76 20  rse;..  Vdbe *v 
3c27f 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
3c280 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 57 68  ..  int i;..  Wh
3c281 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
3c282 3b 0d 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  ;..  SrcList *pT
3c283 61 62 4c 69 73 74 20 3d 20 70 57 49 6e 66 6f 2d  abList = pWInfo-
3c284 3e 70 54 61 62 4c 69 73 74 3b 0d 0a 20 20 73 71  >pTabList;..  sq
3c285 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
3c286 73 65 2d 3e 64 62 3b 0d 0a 0d 0a 20 20 2f 2a 20  se->db;....  /* 
3c287 47 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65  Generate loop te
3c288 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0d  rmination code..
3c289 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  .  */..  sqlite3
3c28a 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
3c28b 50 61 72 73 65 29 3b 0d 0a 20 20 66 6f 72 28 69  Parse);..  for(i
3c28c 3d 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 2d  =pWInfo->nLevel-
3c28d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0d 0a  1; i>=0; i--){..
3c28e 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57      pLevel = &pW
3c28f 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0d 0a 20 20 20  Info->a[i];..   
3c290 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
3c291 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76  lveLabel(v, pLev
3c292 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b 0d 0a  el->addrCont);..
3c293 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
3c294 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0d 0a  op!=OP_Noop ){..
3c295 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3c296 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65  eAddOp2(v, pLeve
3c297 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->op, pLevel->p
3c298 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0d  1, pLevel->p2);.
3c299 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3c29a 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c  beChangeP5(v, pL
3c29b 65 76 65 6c 2d 3e 70 35 29 3b 0d 0a 20 20 20 20  evel->p5);..    
3c29c 7d 0d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  }..    if( pLeve
3c29d 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3c29e 26 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20  & WHERE_IN_ABLE 
3c29f 26 26 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  && pLevel->u.in.
3c2a0 6e 49 6e 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20  nIn>0 ){..      
3c2a1 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70  struct InLoop *p
3c2a2 49 6e 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a  In;..      int j
3c2a3 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3c2a4 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
3c2a5 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72  (v, pLevel->addr
3c2a6 4e 78 74 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72  Nxt);..      for
3c2a7 28 6a 3d 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  (j=pLevel->u.in.
3c2a8 6e 49 6e 2c 20 70 49 6e 3d 26 70 4c 65 76 65 6c  nIn, pIn=&pLevel
3c2a9 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 6a  ->u.in.aInLoop[j
3c2aa 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70  -1]; j>0; j--, p
3c2ab 49 6e 2d 2d 29 7b 0d 0a 20 20 20 20 20 20 20 20  In--){..        
3c2ac 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
3c2ad 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72  ere(v, pIn->addr
3c2ae 49 6e 54 6f 70 2b 31 29 3b 0d 0a 20 20 20 20 20  InTop+1);..     
3c2af 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3c2b0 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
3c2b1 20 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d   pIn->iCur, pIn-
3c2b2 3e 61 64 64 72 49 6e 54 6f 70 29 3b 0d 0a 20 20  >addrInTop);..  
3c2b3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3c2b4 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e  eJumpHere(v, pIn
3c2b5 2d 3e 61 64 64 72 49 6e 54 6f 70 2d 31 29 3b 0d  ->addrInTop-1);.
3c2b6 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3c2b7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
3c2b8 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  , pLevel->u.in.a
3c2b9 49 6e 4c 6f 6f 70 29 3b 0d 0a 20 20 20 20 7d 0d  InLoop);..    }.
3c2ba 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3c2bb 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
3c2bc 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29  pLevel->addrBrk)
3c2bd 3b 0d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  ;..    if( pLeve
3c2be 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0d  l->iLeftJoin ){.
3c2bf 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
3c2c0 0d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ..      addr = s
3c2c1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
3c2c2 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c  (v, OP_IfPos, pL
3c2c3 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29  evel->iLeftJoin)
3c2c4 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
3c2c5 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
3c2c6 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
3c2c7 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0d 0a 20 20 20  DX_ONLY)==0..   
3c2c8 20 20 20 20 20 20 20 20 7c 7c 20 28 70 4c 65 76          || (pLev
3c2c9 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3c2ca 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
3c2cb 29 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 69  )!=0 );..      i
3c2cc 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
3c2cd 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
3c2ce 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b  _IDX_ONLY)==0 ){
3c2cf 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
3c2d0 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
3c2d1 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54 61 62 4c  P_NullRow, pTabL
3c2d2 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
3c2d3 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  r);..      }..  
3c2d4 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
3c2d5 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0d 0a 20  iIdxCur>=0 ){.. 
3c2d6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3c2d7 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
3c2d8 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d 3e  ullRow, pLevel->
3c2d9 69 49 64 78 43 75 72 29 3b 0d 0a 20 20 20 20 20  iIdxCur);..     
3c2da 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4c   }..      if( pL
3c2db 65 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74  evel->op==OP_Ret
3c2dc 75 72 6e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  urn ){..        
3c2dd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3c2de 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70  2(v, OP_Gosub, p
3c2df 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65  Level->p1, pLeve
3c2e0 6c 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0d 0a  l->addrFirst);..
3c2e1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3c2e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3c2e3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
3c2e4 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 61  to, 0, pLevel->a
3c2e5 64 64 72 46 69 72 73 74 29 3b 0d 0a 20 20 20 20  ddrFirst);..    
3c2e6 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74    }..      sqlit
3c2e7 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
3c2e8 2c 20 61 64 64 72 29 3b 0d 0a 20 20 20 20 7d 0d  , addr);..    }.
3c2e9 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65  .  }....  /* The
3c2ea 20 22 62 72 65 61 6b 22 20 70 6f 69 6e 74 20 69   "break" point i
3c2eb 73 20 68 65 72 65 2c 20 6a 75 73 74 20 70 61 73  s here, just pas
3c2ec 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
3c2ed 20 6f 75 74 65 72 20 6c 6f 6f 70 2e 0d 0a 20 20   outer loop...  
3c2ee 2a 2a 20 53 65 74 20 69 74 2e 0d 0a 20 20 2a 2f  ** Set it...  */
3c2ef 0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  ..  sqlite3VdbeR
3c2f0 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
3c2f1 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0d  WInfo->iBreak);.
3c2f2 0a 0d 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c  ...  /* Close al
3c2f3 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73  l of the cursors
3c2f4 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e 65   that were opene
3c2f5 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72  d by sqlite3Wher
3c2f6 65 42 65 67 69 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20  eBegin...  */.. 
3c2f7 20 61 73 73 65 72 74 28 20 70 57 49 6e 66 6f 2d   assert( pWInfo-
3c2f8 3e 6e 4c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 57  >nLevel==1 || pW
3c2f9 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 70 54  Info->nLevel==pT
3c2fa 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0d  abList->nSrc );.
3c2fb 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76  .  for(i=0, pLev
3c2fc 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c  el=pWInfo->a; i<
3c2fd 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20  pWInfo->nLevel; 
3c2fe 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0d  i++, pLevel++){.
3c2ff 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
3c300 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74  ist_item *pTabIt
3c301 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
3c302 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
3c303 3b 0d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ;..    Table *pT
3c304 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70  ab = pTabItem->p
3c305 54 61 62 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  Tab;..    assert
3c306 28 20 70 54 61 62 21 3d 30 20 29 3b 0d 0a 20 20  ( pTab!=0 );..  
3c307 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62    if( (pTab->tab
3c308 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
3c309 65 72 61 6c 29 3d 3d 30 0d 0a 20 20 20 20 20 26  eral)==0..     &
3c30a 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d  & pTab->pSelect=
3c30b 3d 30 0d 0a 20 20 20 20 20 26 26 20 28 70 57 49  =0..     && (pWI
3c30c 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20  nfo->wctrlFlags 
3c30d 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45  & WHERE_OMIT_OPE
3c30e 4e 5f 43 4c 4f 53 45 29 3d 3d 30 0d 0a 20 20 20  N_CLOSE)==0..   
3c30f 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 77   ){..      int w
3c310 73 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  s = pLevel->plan
3c311 2e 77 73 46 6c 61 67 73 3b 0d 0a 20 20 20 20 20  .wsFlags;..     
3c312 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b   if( !pWInfo->ok
3c313 4f 6e 65 50 61 73 73 20 26 26 20 28 77 73 20 26  OnePass && (ws &
3c314 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
3c315 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
3c316 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3c317 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70  1(v, OP_Close, p
3c318 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
3c319 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
3c31a 20 20 20 69 66 28 20 28 77 73 20 26 20 57 48 45     if( (ws & WHE
3c31b 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26  RE_INDEXED)!=0 &
3c31c 26 20 28 77 73 20 26 20 57 48 45 52 45 5f 54 45  & (ws & WHERE_TE
3c31d 4d 50 5f 49 4e 44 45 58 29 3d 3d 30 20 29 7b 0d  MP_INDEX)==0 ){.
3c31e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3c31f 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
3c320 5f 43 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e  _Close, pLevel->
3c321 69 49 64 78 43 75 72 29 3b 0d 0a 20 20 20 20 20  iIdxCur);..     
3c322 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
3c323 20 2f 2a 20 49 66 20 74 68 69 73 20 73 63 61 6e   /* If this scan
3c324 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 2c 20   uses an index, 
3c325 6d 61 6b 65 20 63 6f 64 65 20 73 75 62 73 74 69  make code substi
3c326 74 75 74 69 6f 6e 73 20 74 6f 20 72 65 61 64 20  tutions to read 
3c327 64 61 74 61 0d 0a 20 20 20 20 2a 2a 20 66 72 6f  data..    ** fro
3c328 6d 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70  m the index in p
3c329 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
3c32a 20 74 61 62 6c 65 2e 20 53 6f 6d 65 74 69 6d 65   table. Sometime
3c32b 73 2c 20 74 68 69 73 20 6d 65 61 6e 73 0d 0a 20  s, this means.. 
3c32c 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20     ** the table 
3c32d 6e 65 65 64 20 6e 65 76 65 72 20 62 65 20 72 65  need never be re
3c32e 61 64 20 66 72 6f 6d 2e 20 54 68 69 73 20 69 73  ad from. This is
3c32f 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62   a performance b
3c330 6f 6f 73 74 2c 0d 0a 20 20 20 20 2a 2a 20 61 73  oost,..    ** as
3c331 20 74 68 65 20 76 64 62 65 20 6c 65 76 65 6c 20   the vdbe level 
3c332 77 61 69 74 73 20 75 6e 74 69 6c 20 74 68 65 20  waits until the 
3c333 74 61 62 6c 65 20 69 73 20 72 65 61 64 20 62 65  table is read be
3c334 66 6f 72 65 20 61 63 74 75 61 6c 6c 79 0d 0a 20  fore actually.. 
3c335 20 20 20 2a 2a 20 73 65 65 6b 69 6e 67 20 74 68     ** seeking th
3c336 65 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 74  e table cursor t
3c337 6f 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 72  o the record cor
3c338 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
3c339 65 20 63 75 72 72 65 6e 74 0d 0a 20 20 20 20 2a  e current..    *
3c33a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  * position in th
3c33b 65 20 69 6e 64 65 78 2e 0d 0a 20 20 20 20 2a 2a  e index...    **
3c33c 20 0d 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73 20   ..    ** Calls 
3c33d 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65  to the code gene
3c33e 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65 6e  rator in between
3c33f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
3c340 69 6e 20 61 6e 64 0d 0a 20 20 20 20 2a 2a 20 73  in and..    ** s
3c341 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77  qlite3WhereEnd w
3c342 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64  ill have created
3c343 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72   code that refer
3c344 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0d  ences the table.
3c345 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
3c346 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61  .  This loop sca
3c347 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65  ns all that code
3c348 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63   looking for opc
3c349 6f 64 65 73 0d 0a 20 20 20 20 2a 2a 20 74 68 61  odes..    ** tha
3c34a 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  t reference the 
3c34b 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72  table and conver
3c34c 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63  ts them into opc
3c34d 6f 64 65 73 20 74 68 61 74 0d 0a 20 20 20 20 2a  odes that..    *
3c34e 2a 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  * reference the 
3c34f 69 6e 64 65 78 2e 0d 0a 20 20 20 20 2a 2f 0d 0a  index...    */..
3c350 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
3c351 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
3c352 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d  WHERE_INDEXED)!=
3c353 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  0 && !db->malloc
3c354 46 61 69 6c 65 64 29 7b 0d 0a 20 20 20 20 20 20  Failed){..      
3c355 69 6e 74 20 6b 2c 20 6a 2c 20 6c 61 73 74 3b 0d  int k, j, last;.
3c356 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70  .      VdbeOp *p
3c357 4f 70 3b 0d 0a 20 20 20 20 20 20 49 6e 64 65 78  Op;..      Index
3c358 20 2a 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d   *pIdx = pLevel-
3c359 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0d 0a 0d  >plan.u.pIdx;...
3c35a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
3c35b 49 64 78 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20  Idx!=0 );..     
3c35c 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64   pOp = sqlite3Vd
3c35d 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49 6e 66  beGetOp(v, pWInf
3c35e 6f 2d 3e 69 54 6f 70 29 3b 0d 0a 20 20 20 20 20  o->iTop);..     
3c35f 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33 56   last = sqlite3V
3c360 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
3c361 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  );..      for(k=
3c362 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c  pWInfo->iTop; k<
3c363 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b  last; k++, pOp++
3c364 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
3c365 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d  pOp->p1!=pLevel-
3c366 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69  >iTabCur ) conti
3c367 6e 75 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  nue;..        if
3c368 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
3c369 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0d 0a 20 20 20  P_Column ){..   
3c36a 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
3c36b 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  j<pIdx->nColumn;
3c36c 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20   j++){..        
3c36d 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
3c36e 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
3c36f 6a 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  j] ){..         
3c370 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a       pOp->p2 = j
3c371 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
3c372 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65   pOp->p1 = pLeve
3c373 6c 2d 3e 69 49 64 78 43 75 72 3b 0d 0a 20 20 20  l->iIdxCur;..   
3c374 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3c375 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
3c376 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
3c377 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
3c378 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
3c379 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
3c37a 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0d 0a 20 20 20  DX_ONLY)==0..   
3c37b 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6a              || j
3c37c 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  <pIdx->nColumn )
3c37d 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ;..        }else
3c37e 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
3c37f 3d 3d 4f 50 5f 52 6f 77 69 64 20 29 7b 0d 0a 20  ==OP_Rowid ){.. 
3c380 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31           pOp->p1
3c381 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43   = pLevel->iIdxC
3c382 75 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  ur;..          p
3c383 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f  Op->opcode = OP_
3c384 49 64 78 52 6f 77 69 64 3b 0d 0a 20 20 20 20 20  IdxRowid;..     
3c385 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
3c386 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
3c387 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0d  * Final cleanup.
3c388 0a 20 20 2a 2f 0d 0a 20 20 70 50 61 72 73 65 2d  .  */..  pParse-
3c389 3e 6e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 70 57  >nQueryLoop = pW
3c38a 49 6e 66 6f 2d 3e 73 61 76 65 64 4e 51 75 65 72  Info->savedNQuer
3c38b 79 4c 6f 6f 70 3b 0d 0a 20 20 77 68 65 72 65 49  yLoop;..  whereI
3c38c 6e 66 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e  nfoFree(db, pWIn
3c38d 66 6f 29 3b 0d 0a 20 20 72 65 74 75 72 6e 3b 0d  fo);..  return;.
3c38e 0a 7d 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}..../*********
3c38f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 68 65  ***** End of whe
3c390 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  re.c ***********
3c391 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c393 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
3c394 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3c395 65 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a  e parse.c ******
3c396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c397 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c398 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 20 44 72 69 76 65  *****/../* Drive
3c399 72 20 74 65 6d 70 6c 61 74 65 20 66 6f 72 20 74  r template for t
3c39a 68 65 20 4c 45 4d 4f 4e 20 70 61 72 73 65 72 20  he LEMON parser 
3c39b 67 65 6e 65 72 61 74 6f 72 2e 0d 0a 2a 2a 20 54  generator...** T
3c39c 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
3c39d 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
3c39e 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
3c39f 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  e...**..** This 
3c3a0 76 65 72 73 69 6f 6e 20 6f 66 20 22 6c 65 6d 70  version of "lemp
3c3a1 61 72 2e 63 22 20 69 73 20 6d 6f 64 69 66 69 65  ar.c" is modifie
3c3a2 64 2c 20 73 6c 69 67 68 74 6c 79 2c 20 66 6f 72  d, slightly, for
3c3a3 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 0d   use by SQLite..
3c3a4 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 6d 6f 64  .** The only mod
3c3a5 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20 74  ifications are t
3c3a6 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 61  he addition of a
3c3a7 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45 56 45 52   couple of NEVER
3c3a8 28 29 0d 0a 2a 2a 20 6d 61 63 72 6f 73 20 74 6f  ()..** macros to
3c3a9 20 64 69 73 61 62 6c 65 20 74 65 73 74 73 20 74   disable tests t
3c3aa 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 69  hat are needed i
3c3ab 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61 20  n the case of a 
3c3ac 67 65 6e 65 72 61 6c 0d 0a 2a 2a 20 4c 41 4c 52  general..** LALR
3c3ad 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75 74 20  (1) grammar but 
3c3ae 77 68 69 63 68 20 61 72 65 20 61 6c 77 61 79 73  which are always
3c3af 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0d 0a 2a   false in the..*
3c3b0 2a 20 73 70 65 63 69 66 69 63 20 67 72 61 6d 6d  * specific gramm
3c3b1 61 72 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  ar used by SQLit
3c3b2 65 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 46 69 72 73 74  e...*/../* First
3c3b3 20 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e   off, code is in
3c3b4 63 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c  cluded that foll
3c3b5 6f 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65  ows the "include
3c3b6 22 20 64 65 63 6c 61 72 61 74 69 6f 6e 0d 0a 2a  " declaration..*
3c3b7 2a 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67  * in the input g
3c3b8 72 61 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0d  rammar file. */.
3c3b9 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
3c3ba 64 69 6f 2e 68 3e 20 2a 2f 0d 0a 0d 0a 0d 0a 2f  dio.h> */....../
3c3bb 2a 0d 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c  *..** Disable al
3c3bc 6c 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79  l error recovery
3c3bd 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74   processing in t
3c3be 68 65 20 70 61 72 73 65 72 20 70 75 73 68 2d 64  he parser push-d
3c3bf 6f 77 6e 0d 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f  own..** automato
3c3c0 6e 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  n...*/..#define 
3c3c1 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52  YYNOERRORRECOVER
3c3c2 59 20 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61  Y 1..../*..** Ma
3c3c3 6b 65 20 79 79 74 65 73 74 63 61 73 65 28 29 20  ke yytestcase() 
3c3c4 74 68 65 20 73 61 6d 65 20 61 73 20 74 65 73 74  the same as test
3c3c5 63 61 73 65 28 29 0d 0a 2a 2f 0d 0a 23 64 65 66  case()..*/..#def
3c3c6 69 6e 65 20 79 79 74 65 73 74 63 61 73 65 28 58  ine yytestcase(X
3c3c7 29 20 74 65 73 74 63 61 73 65 28 58 29 0d 0a 0d  ) testcase(X)...
3c3c8 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ./*..** An insta
3c3c9 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
3c3ca 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f  cture holds info
3c3cb 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
3c3cc 65 0d 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75  e..** LIMIT clau
3c3cd 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
3c3ce 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 73  tatement...*/..s
3c3cf 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b  truct LimitVal {
3c3d0 0d 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74  ..  Expr *pLimit
3c3d1 3b 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49  ;    /* The LIMI
3c3d2 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e  T expression.  N
3c3d3 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20  ULL if there is 
3c3d4 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0d 0a 20 20 45  no limit */..  E
3c3d5 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
3c3d6 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 65 78  /* The OFFSET ex
3c3d7 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20  pression.  NULL 
3c3d8 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65  if there is none
3c3d9 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
3c3da 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
3c3db 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
3c3dc 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
3c3dd 20 74 68 65 20 4c 49 4b 45 2c 0d 0a 2a 2a 20 47   the LIKE,..** G
3c3de 4c 4f 42 2c 20 4e 4f 54 20 4c 49 4b 45 2c 20 61  LOB, NOT LIKE, a
3c3df 6e 64 20 4e 4f 54 20 47 4c 4f 42 20 6f 70 65 72  nd NOT GLOB oper
3c3e0 61 74 6f 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  ators...*/..stru
3c3e1 63 74 20 4c 69 6b 65 4f 70 20 7b 0d 0a 20 20 54  ct LikeOp {..  T
3c3e2 6f 6b 65 6e 20 65 4f 70 65 72 61 74 6f 72 3b 20  oken eOperator; 
3c3e3 20 2f 2a 20 22 6c 69 6b 65 22 20 6f 72 20 22 67   /* "like" or "g
3c3e4 6c 6f 62 22 20 6f 72 20 22 72 65 67 65 78 70 22  lob" or "regexp"
3c3e5 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 6f 74 3b 20   */..  int not; 
3c3e6 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3c3e7 69 66 20 74 68 65 20 4e 4f 54 20 6b 65 79 77 6f  if the NOT keywo
3c3e8 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
3c3e9 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ..};..../*..** A
3c3ea 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3c3eb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
3c3ec 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20  cture describes 
3c3ed 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0d 0a  the event of a..
3c3ee 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22  ** TRIGGER.  "a"
3c3ef 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79   is the event ty
3c3f0 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50  pe, one of TK_UP
3c3f1 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
3c3f2 0d 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20  ..** TK_DELETE, 
3c3f3 6f 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20  or TK_INSTEAD.  
3c3f4 49 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20  If the event is 
3c3f5 6f 66 20 74 68 65 20 66 6f 72 6d 0d 0a 2a 2a 0d  of the form..**.
3c3f6 0a 2a 2a 20 20 20 20 20 20 55 50 44 41 54 45 20  .**      UPDATE 
3c3f7 4f 4e 20 28 61 2c 62 2c 63 29 0d 0a 2a 2a 0d 0a  ON (a,b,c)..**..
3c3f8 2a 2a 20 54 68 65 6e 20 74 68 65 20 22 62 22 20  ** Then the "b" 
3c3f9 49 64 4c 69 73 74 20 72 65 63 6f 72 64 73 20 74  IdList records t
3c3fa 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 2e  he list "a,b,c".
3c3fb 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 54 72 69  ..*/..struct Tri
3c3fc 67 45 76 65 6e 74 20 7b 20 69 6e 74 20 61 3b 20  gEvent { int a; 
3c3fd 49 64 4c 69 73 74 20 2a 20 62 3b 20 7d 3b 0d 0a  IdList * b; };..
3c3fe 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74  ../*..** An inst
3c3ff 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
3c400 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65  ucture holds the
3c401 20 41 54 54 41 43 48 20 6b 65 79 20 61 6e 64 20   ATTACH key and 
3c402 74 68 65 20 6b 65 79 20 74 79 70 65 2e 0d 0a 2a  the key type...*
3c403 2f 0d 0a 73 74 72 75 63 74 20 41 74 74 61 63 68  /..struct Attach
3c404 4b 65 79 20 7b 20 69 6e 74 20 74 79 70 65 3b 20  Key { int type; 
3c405 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b 0d 0a   Token key; };..
3c406 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  ....  /* This is
3c407 20 61 20 75 74 69 6c 69 74 79 20 72 6f 75 74 69   a utility routi
3c408 6e 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  ne used to set t
3c409 68 65 20 45 78 70 72 53 70 61 6e 2e 7a 53 74 61  he ExprSpan.zSta
3c40a 72 74 20 61 6e 64 0d 0a 20 20 2a 2a 20 45 78 70  rt and..  ** Exp
3c40b 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75 65  rSpan.zEnd value
3c40c 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74 68 61  s of pOut so tha
3c40d 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76 65 72  t the span cover
3c40e 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0d 0a  s the complete..
3c40f 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74 65    ** range of te
3c410 78 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  xt beginning wit
3c411 68 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f 69  h pStart and goi
3c412 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ng to the end of
3c413 20 70 45 6e 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   pEnd...  */..  
3c414 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3c415 53 65 74 28 45 78 70 72 53 70 61 6e 20 2a 70 4f  Set(ExprSpan *pO
3c416 75 74 2c 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72  ut, Token *pStar
3c417 74 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b  t, Token *pEnd){
3c418 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61  ..    pOut->zSta
3c419 72 74 20 3d 20 70 53 74 61 72 74 2d 3e 7a 3b 0d  rt = pStart->z;.
3c41a 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
3c41b 3d 20 26 70 45 6e 64 2d 3e 7a 5b 70 45 6e 64 2d  = &pEnd->z[pEnd-
3c41c 3e 6e 5d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  >n];..  }....  /
3c41d 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e 65  * Construct a ne
3c41e 77 20 45 78 70 72 20 6f 62 6a 65 63 74 20 66 72  w Expr object fr
3c41f 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e  om a single iden
3c420 74 69 66 69 65 72 2e 20 20 55 73 65 20 74 68 65  tifier.  Use the
3c421 0d 0a 20 20 2a 2a 20 6e 65 77 20 45 78 70 72 20  ..  ** new Expr 
3c422 74 6f 20 70 6f 70 75 6c 61 74 65 20 70 4f 75 74  to populate pOut
3c423 2e 20 20 53 65 74 20 74 68 65 20 73 70 61 6e 20  .  Set the span 
3c424 6f 66 20 70 4f 75 74 20 74 6f 20 62 65 20 74 68  of pOut to be th
3c425 65 20 69 64 65 6e 74 69 66 69 65 72 0d 0a 20 20  e identifier..  
3c426 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20  ** that created 
3c427 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0d  the expression..
3c428 0a 20 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20  .  */..  static 
3c429 76 6f 69 64 20 73 70 61 6e 45 78 70 72 28 45 78  void spanExpr(Ex
3c42a 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 50 61  prSpan *pOut, Pa
3c42b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
3c42c 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 56 61 6c   op, Token *pVal
3c42d 75 65 29 7b 0d 0a 20 20 20 20 70 4f 75 74 2d 3e  ue){..    pOut->
3c42e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
3c42f 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c  Expr(pParse, op,
3c430 20 30 2c 20 30 2c 20 70 56 61 6c 75 65 29 3b 0d   0, 0, pValue);.
3c431 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
3c432 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a 3b 0d 0a  t = pValue->z;..
3c433 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d      pOut->zEnd =
3c434 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56 61 6c   &pValue->z[pVal
3c435 75 65 2d 3e 6e 5d 3b 0d 0a 20 20 7d 0d 0a 0d 0a  ue->n];..  }....
3c436 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
3c437 65 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62  e constructs a b
3c438 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  inary expression
3c439 20 6e 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f   node out of two
3c43a 20 45 78 70 72 53 70 61 6e 0d 0a 20 20 2a 2a 20   ExprSpan..  ** 
3c43b 6f 62 6a 65 63 74 73 20 61 6e 64 20 75 73 65 73  objects and uses
3c43c 20 74 68 65 20 72 65 73 75 6c 74 20 74 6f 20 70   the result to p
3c43d 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 45 78  opulate a new Ex
3c43e 70 72 53 70 61 6e 20 6f 62 6a 65 63 74 2e 0d 0a  prSpan object...
3c43f 20 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 76    */..  static v
3c440 6f 69 64 20 73 70 61 6e 42 69 6e 61 72 79 45 78  oid spanBinaryEx
3c441 70 72 28 0d 0a 20 20 20 20 45 78 70 72 53 70 61  pr(..    ExprSpa
3c442 6e 20 2a 70 4f 75 74 2c 20 20 20 20 20 2f 2a 20  n *pOut,     /* 
3c443 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  Write the result
3c444 20 68 65 72 65 20 2a 2f 0d 0a 20 20 20 20 50 61   here */..    Pa
3c445 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
3c446 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
3c447 20 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72   context.  Error
3c448 73 20 61 63 63 75 6d 75 6c 61 74 65 20 68 65 72  s accumulate her
3c449 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6f 70  e */..    int op
3c44a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
3c44b 20 54 68 65 20 62 69 6e 61 72 79 20 6f 70 65 72   The binary oper
3c44c 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 45 78  ation */..    Ex
3c44d 70 72 53 70 61 6e 20 2a 70 4c 65 66 74 2c 20 20  prSpan *pLeft,  
3c44e 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
3c44f 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 20 20 45 78  erand */..    Ex
3c450 70 72 53 70 61 6e 20 2a 70 52 69 67 68 74 20 20  prSpan *pRight  
3c451 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f    /* The right o
3c452 70 65 72 61 6e 64 20 2a 2f 0d 0a 20 20 29 7b 0d  perand */..  ){.
3c453 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78 70 72  .    pOut->pExpr
3c454 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3c455 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4c 65 66  pParse, op, pLef
3c456 74 2d 3e 70 45 78 70 72 2c 20 70 52 69 67 68 74  t->pExpr, pRight
3c457 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0d 0a 20 20  ->pExpr, 0);..  
3c458 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
3c459 20 70 4c 65 66 74 2d 3e 7a 53 74 61 72 74 3b 0d   pLeft->zStart;.
3c45a 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
3c45b 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e 64 3b 0d  = pRight->zEnd;.
3c45c 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6e  .  }....  /* Con
3c45d 73 74 72 75 63 74 20 61 6e 20 65 78 70 72 65 73  struct an expres
3c45e 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20  sion node for a 
3c45f 75 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f 70  unary postfix op
3c460 65 72 61 74 6f 72 0d 0a 20 20 2a 2f 0d 0a 20 20  erator..  */..  
3c461 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3c462 55 6e 61 72 79 50 6f 73 74 66 69 78 28 0d 0a 20  UnaryPostfix(.. 
3c463 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75     ExprSpan *pOu
3c464 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69  t,        /* Wri
3c465 74 65 20 74 68 65 20 6e 65 77 20 65 78 70 72 65  te the new expre
3c466 73 73 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20  ssion node here 
3c467 2a 2f 0d 0a 20 20 20 20 50 61 72 73 65 20 2a 70  */..    Parse *p
3c468 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
3c469 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
3c46a 74 20 74 6f 20 72 65 63 6f 72 64 20 65 72 72 6f  t to record erro
3c46b 72 73 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6f  rs */..    int o
3c46c 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
3c46d 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 74 6f    /* The operato
3c46e 72 20 2a 2f 0d 0a 20 20 20 20 45 78 70 72 53 70  r */..    ExprSp
3c46f 61 6e 20 2a 70 4f 70 65 72 61 6e 64 2c 20 20 20  an *pOperand,   
3c470 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20   /* The operand 
3c471 2a 2f 0d 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70  */..    Token *p
3c472 50 6f 73 74 4f 70 20 20 20 20 20 20 20 20 20 2f  PostOp         /
3c473 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20 74 6f  * The operand to
3c474 6b 65 6e 20 66 6f 72 20 73 65 74 74 69 6e 67 20  ken for setting 
3c475 74 68 65 20 73 70 61 6e 20 2a 2f 0d 0a 20 20 29  the span */..  )
3c476 7b 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78  {..    pOut->pEx
3c477 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3c478 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4f  r(pParse, op, pO
3c479 70 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c 20 30  perand->pExpr, 0
3c47a 2c 20 30 29 3b 0d 0a 20 20 20 20 70 4f 75 74 2d  , 0);..    pOut-
3c47b 3e 7a 53 74 61 72 74 20 3d 20 70 4f 70 65 72 61  >zStart = pOpera
3c47c 6e 64 2d 3e 7a 53 74 61 72 74 3b 0d 0a 20 20 20  nd->zStart;..   
3c47d 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70   pOut->zEnd = &p
3c47e 50 6f 73 74 4f 70 2d 3e 7a 5b 70 50 6f 73 74 4f  PostOp->z[pPostO
3c47f 70 2d 3e 6e 5d 3b 0d 0a 20 20 7d 20 20 20 20 20  p->n];..  }     
3c480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c481 20 20 20 20 20 20 0d 0a 0d 0a 20 20 2f 2a 20 41        ....  /* A
3c482 20 72 6f 75 74 69 6e 65 20 74 6f 20 63 6f 6e 76   routine to conv
3c483 65 72 74 20 61 20 62 69 6e 61 72 79 20 54 4b 5f  ert a binary TK_
3c484 49 53 20 6f 72 20 54 4b 5f 49 53 4e 4f 54 20 65  IS or TK_ISNOT e
3c485 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61  xpression into a
3c486 0d 0a 20 20 2a 2a 20 75 6e 61 72 79 20 54 4b 5f  ..  ** unary TK_
3c487 49 53 4e 55 4c 4c 20 6f 72 20 54 4b 5f 4e 4f 54  ISNULL or TK_NOT
3c488 4e 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e  NULL expression.
3c489 20 2a 2f 0d 0a 20 20 73 74 61 74 69 63 20 76 6f   */..  static vo
3c48a 69 64 20 62 69 6e 61 72 79 54 6f 55 6e 61 72 79  id binaryToUnary
3c48b 49 66 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50  IfNull(Parse *pP
3c48c 61 72 73 65 2c 20 45 78 70 72 20 2a 70 59 2c 20  arse, Expr *pY, 
3c48d 45 78 70 72 20 2a 70 41 2c 20 69 6e 74 20 6f 70  Expr *pA, int op
3c48e 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 20  ){..    sqlite3 
3c48f 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
3c490 3b 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  ;..    if( db->m
3c491 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26  allocFailed==0 &
3c492 26 20 70 59 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c  & pY->op==TK_NUL
3c493 4c 20 29 7b 0d 0a 20 20 20 20 20 20 70 41 2d 3e  L ){..      pA->
3c494 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0d 0a 20 20  op = (u8)op;..  
3c495 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
3c496 65 6c 65 74 65 28 64 62 2c 20 70 41 2d 3e 70 52  elete(db, pA->pR
3c497 69 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 70 41  ight);..      pA
3c498 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0d 0a 20  ->pRight = 0;.. 
3c499 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
3c49a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 6e 20 65  * Construct an e
3c49b 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66  xpression node f
3c49c 6f 72 20 61 20 75 6e 61 72 79 20 70 72 65 66 69  or a unary prefi
3c49d 78 20 6f 70 65 72 61 74 6f 72 0d 0a 20 20 2a 2f  x operator..  */
3c49e 0d 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  ..  static void 
3c49f 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28  spanUnaryPrefix(
3c4a0 0d 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  ..    ExprSpan *
3c4a1 70 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  pOut,        /* 
3c4a2 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 65 78  Write the new ex
3c4a3 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 68 65  pression node he
3c4a4 72 65 20 2a 2f 0d 0a 20 20 20 20 50 61 72 73 65  re */..    Parse
3c4a5 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
3c4a6 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
3c4a7 74 65 78 74 20 74 6f 20 72 65 63 6f 72 64 20 65  text to record e
3c4a8 72 72 6f 72 73 20 2a 2f 0d 0a 20 20 20 20 69 6e  rrors */..    in
3c4a9 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
3c4aa 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72       /* The oper
3c4ab 61 74 6f 72 20 2a 2f 0d 0a 20 20 20 20 45 78 70  ator */..    Exp
3c4ac 72 53 70 61 6e 20 2a 70 4f 70 65 72 61 6e 64 2c  rSpan *pOperand,
3c4ad 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61      /* The opera
3c4ae 6e 64 20 2a 2f 0d 0a 20 20 20 20 54 6f 6b 65 6e  nd */..    Token
3c4af 20 2a 70 50 72 65 4f 70 20 20 20 20 20 20 20 20   *pPreOp        
3c4b0 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20   /* The operand 
3c4b1 74 6f 6b 65 6e 20 66 6f 72 20 73 65 74 74 69 6e  token for settin
3c4b2 67 20 74 68 65 20 73 70 61 6e 20 2a 2f 0d 0a 20  g the span */.. 
3c4b3 20 29 7b 0d 0a 20 20 20 20 70 4f 75 74 2d 3e 70   ){..    pOut->p
3c4b4 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
3c4b5 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20  xpr(pParse, op, 
3c4b6 70 4f 70 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c  pOperand->pExpr,
3c4b7 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 70 4f 75   0, 0);..    pOu
3c4b8 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70 50 72 65  t->zStart = pPre
3c4b9 4f 70 2d 3e 7a 3b 0d 0a 20 20 20 20 70 4f 75 74  Op->z;..    pOut
3c4ba 2d 3e 7a 45 6e 64 20 3d 20 70 4f 70 65 72 61 6e  ->zEnd = pOperan
3c4bb 64 2d 3e 7a 45 6e 64 3b 0d 0a 20 20 7d 0d 0a 2f  d->zEnd;..  }../
3c4bc 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74 6f  * Next is all to
3c4bd 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20 61  ken values, in a
3c4be 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20 66   form suitable f
3c4bf 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68 65  or use by makehe
3c4c0 61 64 65 72 73 2e 0d 0a 2a 2a 20 54 68 69 73 20  aders...** This 
3c4c1 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  section will be 
3c4c2 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f  null unless lemo
3c4c3 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20 74 68  n is run with th
3c4c4 65 20 2d 6d 20 73 77 69 74 63 68 2e 0d 0a 2a 2f  e -m switch...*/
3c4c5 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 65 73 65 20  ../* ..** These 
3c4c6 63 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20 67  constants (all g
3c4c7 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74  enerated automat
3c4c8 69 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70 61  ically by the pa
3c4c9 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29 0d  rser generator).
3c4ca 0a 2a 2a 20 73 70 65 63 69 66 79 20 74 68 65 20  .** specify the 
3c4cb 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66  various kinds of
3c4cc 20 74 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61   tokens (termina
3c4cd 6c 73 29 20 74 68 61 74 20 74 68 65 20 70 61 72  ls) that the par
3c4ce 73 65 72 0d 0a 2a 2a 20 75 6e 64 65 72 73 74 61  ser..** understa
3c4cf 6e 64 73 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 45 61  nds. ..**..** Ea
3c4d0 63 68 20 73 79 6d 62 6f 6c 20 68 65 72 65 20 69  ch symbol here i
3c4d1 73 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d  s a terminal sym
3c4d2 62 6f 6c 20 69 6e 20 74 68 65 20 67 72 61 6d 6d  bol in the gramm
3c4d3 61 72 2e 0d 0a 2a 2f 0d 0a 2f 2a 20 4d 61 6b 65  ar...*/../* Make
3c4d4 20 73 75 72 65 20 74 68 65 20 49 4e 54 45 52 46   sure the INTERF
3c4d5 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65 66  ACE macro is def
3c4d6 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  ined...*/..#ifnd
3c4d7 65 66 20 49 4e 54 45 52 46 41 43 45 0d 0a 23 20  ef INTERFACE..# 
3c4d8 64 65 66 69 6e 65 20 49 4e 54 45 52 46 41 43 45  define INTERFACE
3c4d9 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 2f 2a 20 54   1..#endif../* T
3c4da 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20 69 6e  he next thing in
3c4db 63 6c 75 64 65 64 20 69 73 20 73 65 72 69 65 73  cluded is series
3c4dc 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68 69 63   of defines whic
3c4dd 68 20 63 6f 6e 74 72 6f 6c 0d 0a 2a 2a 20 76 61  h control..** va
3c4de 72 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f 66  rious aspects of
3c4df 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 70   the generated p
3c4e0 61 72 73 65 72 2e 0d 0a 2a 2a 20 20 20 20 59 59  arser...**    YY
3c4e1 43 4f 44 45 54 59 50 45 20 20 20 20 20 20 20 20  CODETYPE        
3c4e2 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
3c4e3 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
3c4e4 6e 67 20 74 65 72 6d 69 6e 61 6c 0d 0a 2a 2a 20  ng terminal..** 
3c4e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c4e6 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72        and nonter
3c4e7 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20  minal numbers.  
3c4e8 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20  "unsigned char" 
3c4e9 69 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  is..**          
3c4ea 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
3c4eb 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  d if there are f
3c4ec 65 77 65 72 20 74 68 61 6e 20 32 35 30 20 74 65  ewer than 250 te
3c4ed 72 6d 69 6e 61 6c 73 0d 0a 2a 2a 20 20 20 20 20  rminals..**     
3c4ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c4ef 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61    and nontermina
3c4f0 6c 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73  ls.  "int" is us
3c4f1 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0d 0a 2a  ed otherwise...*
3c4f2 2a 20 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20  *    YYNOCODE   
3c4f3 20 20 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d          is a num
3c4f4 62 65 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f  ber of type YYCO
3c4f5 44 45 54 59 50 45 20 77 68 69 63 68 20 63 6f 72  DETYPE which cor
3c4f6 72 65 73 70 6f 6e 64 73 0d 0a 2a 2a 20 20 20 20  responds..**    
3c4f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c4f8 20 20 20 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74     to no legal t
3c4f9 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65  erminal or nonte
3c4fa 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20  rminal number.  
3c4fb 54 68 69 73 0d 0a 2a 2a 20 20 20 20 20 20 20 20  This..**        
3c4fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
3c4fd 75 6d 62 65 72 20 69 73 20 75 73 65 64 20 74 6f  umber is used to
3c4fe 20 66 69 6c 6c 20 69 6e 20 65 6d 70 74 79 20 73   fill in empty s
3c4ff 6c 6f 74 73 20 6f 66 20 74 68 65 20 68 61 73 68  lots of the hash
3c500 20 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   ..**           
3c501 20 20 20 20 20 20 20 20 20 20 20 20 74 61 62 6c              tabl
3c502 65 2e 0d 0a 2a 2a 20 20 20 20 59 59 46 41 4c 4c  e...**    YYFALL
3c503 42 41 43 4b 20 20 20 20 20 20 20 20 20 49 66 20  BACK         If 
3c504 64 65 66 69 6e 65 64 2c 20 74 68 69 73 20 69 6e  defined, this in
3c505 64 69 63 61 74 65 73 20 74 68 61 74 20 6f 6e 65  dicates that one
3c506 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 0d   or more tokens.
3c507 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3c508 20 20 20 20 20 20 20 20 20 20 68 61 76 65 20 66            have f
3c509 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65 73 20  all-back values 
3c50a 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20  which should be 
3c50b 75 73 65 64 20 69 66 20 74 68 65 0d 0a 2a 2a 20  used if the..** 
3c50c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c50d 20 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20 76        original v
3c50e 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  alue of the toke
3c50f 6e 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73 65  n will not parse
3c510 2e 0d 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f  ...**    YYACTIO
3c511 4e 54 59 50 45 20 20 20 20 20 20 20 69 73 20 74  NTYPE       is t
3c512 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
3c513 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
3c514 72 6d 69 6e 61 6c 0d 0a 2a 2a 20 20 20 20 20 20  rminal..**      
3c515 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c516 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c   and nonterminal
3c517 20 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69   numbers.  "unsi
3c518 67 6e 65 64 20 63 68 61 72 22 20 69 73 0d 0a 2a  gned char" is..*
3c519 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3c51a 20 20 20 20 20 20 20 20 75 73 65 64 20 69 66 20          used if 
3c51b 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72 20  there are fewer 
3c51c 74 68 61 6e 20 32 35 30 20 72 75 6c 65 73 20 61  than 250 rules a
3c51d 6e 64 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nd..**          
3c51e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
3c51f 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20 20 22  tes combined.  "
3c520 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f 74 68  int" is used oth
3c521 65 72 77 69 73 65 2e 0d 0a 2a 2a 20 20 20 20 73  erwise...**    s
3c522 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
3c523 4e 54 59 50 45 20 20 20 20 20 69 73 20 74 68 65  NTYPE     is the
3c524 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
3c525 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73  for minor tokens
3c526 20 67 69 76 65 6e 20 0d 0a 2a 2a 20 20 20 20 20   given ..**     
3c527 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c528 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68    directly to th
3c529 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68  e parser from th
3c52a 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0d 0a 2a 2a  e tokenizer...**
3c52b 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20      YYMINORTYPE 
3c52c 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64 61         is the da
3c52d 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72  ta type used for
3c52e 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   all minor token
3c52f 73 2e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  s...**          
3c530 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69               Thi
3c531 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61  s is typically a
3c532 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74   union of many t
3c533 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0d 0a 2a 2a  ypes, one of..**
3c534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c535 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20         which is 
3c536 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
3c537 45 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e 74  ENTYPE.  The ent
3c538 72 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0d  ry in the union.
3c539 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3c53a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 62 61            for ba
3c53b 73 65 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c  se tokens is cal
3c53c 6c 65 64 20 22 79 79 30 22 2e 0d 0a 2a 2a 20 20  led "yy0"...**  
3c53d 20 20 59 59 53 54 41 43 4b 44 45 50 54 48 20 20    YYSTACKDEPTH  
3c53e 20 20 20 20 20 69 73 20 74 68 65 20 6d 61 78 69       is the maxi
3c53f 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65  mum depth of the
3c540 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 2e   parser's stack.
3c541 20 20 49 66 0d 0a 2a 2a 20 20 20 20 20 20 20 20    If..**        
3c542 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
3c543 65 72 6f 20 74 68 65 20 73 74 61 63 6b 20 69 73  ero the stack is
3c544 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 73 69 7a   dynamically siz
3c545 65 64 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63  ed using realloc
3c546 28 29 0d 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ()..**    sqlite
3c547 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c  3ParserARG_SDECL
3c548 20 20 20 20 20 41 20 73 74 61 74 69 63 20 76 61       A static va
3c549 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
3c54a 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78 74 72  on for the %extr
3c54b 61 5f 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a 20 20  a_argument..**  
3c54c 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
3c54d 52 47 5f 50 44 45 43 4c 20 20 20 20 20 41 20 70  RG_PDECL     A p
3c54e 61 72 61 6d 65 74 65 72 20 64 65 63 6c 61 72 61  arameter declara
3c54f 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78  tion for the %ex
3c550 74 72 61 5f 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a  tra_argument..**
3c551 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
3c552 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20 20 43  rARG_STORE     C
3c553 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25 65 78  ode to store %ex
3c554 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69 6e 74  tra_argument int
3c555 6f 20 79 79 70 50 61 72 73 65 72 0d 0a 2a 2a 20  o yypParser..** 
3c556 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
3c557 41 52 47 5f 46 45 54 43 48 20 20 20 20 20 43 6f  ARG_FETCH     Co
3c558 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 25 65  de to extract %e
3c559 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66 72  xtra_argument fr
3c55a 6f 6d 20 79 79 70 50 61 72 73 65 72 0d 0a 2a 2a  om yypParser..**
3c55b 20 20 20 20 59 59 4e 53 54 41 54 45 20 20 20 20      YYNSTATE    
3c55c 20 20 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69         the combi
3c55d 6e 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74  ned number of st
3c55e 61 74 65 73 2e 0d 0a 2a 2a 20 20 20 20 59 59 4e  ates...**    YYN
3c55f 52 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  RULE            
3c560 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 75  the number of ru
3c561 6c 65 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d  les in the gramm
3c562 61 72 0d 0a 2a 2a 20 20 20 20 59 59 45 52 52 4f  ar..**    YYERRO
3c563 52 53 59 4d 42 4f 4c 20 20 20 20 20 20 69 73 20  RSYMBOL      is 
3c564 74 68 65 20 63 6f 64 65 20 6e 75 6d 62 65 72 20  the code number 
3c565 6f 66 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d  of the error sym
3c566 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0d 0a 2a 2a  bol.  If not..**
3c567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c568 20 20 20 20 20 20 20 64 65 66 69 6e 65 64 2c 20         defined, 
3c569 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72 6f 72  then do no error
3c56a 20 70 72 6f 63 65 73 73 69 6e 67 2e 0d 0a 2a 2f   processing...*/
3c56b 0d 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45  ..#define YYCODE
3c56c 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68  TYPE unsigned ch
3c56d 61 72 0d 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f  ar..#define YYNO
3c56e 43 4f 44 45 20 32 35 33 0d 0a 23 64 65 66 69 6e  CODE 253..#defin
3c56f 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75  e YYACTIONTYPE u
3c570 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
3c571 74 0d 0a 23 64 65 66 69 6e 65 20 59 59 57 49 4c  t..#define YYWIL
3c572 44 43 41 52 44 20 36 37 0d 0a 23 64 65 66 69 6e  DCARD 67..#defin
3c573 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  e sqlite3ParserT
3c574 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0d 0a  OKENTYPE Token..
3c575 74 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0d  typedef union {.
3c576 0a 20 20 69 6e 74 20 79 79 69 6e 69 74 3b 0d 0a  .  int yyinit;..
3c577 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
3c578 4f 4b 45 4e 54 59 50 45 20 79 79 30 3b 0d 0a 20  OKENTYPE yy0;.. 
3c579 20 69 6e 74 20 79 79 34 3b 0d 0a 20 20 73 74 72   int yy4;..  str
3c57a 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 79 79  uct TrigEvent yy
3c57b 39 30 3b 0d 0a 20 20 45 78 70 72 53 70 61 6e 20  90;..  ExprSpan 
3c57c 79 79 31 31 38 3b 0d 0a 20 20 54 72 69 67 67 65  yy118;..  Trigge
3c57d 72 53 74 65 70 2a 20 79 79 32 30 33 3b 0d 0a 20  rStep* yy203;.. 
3c57e 20 75 38 20 79 79 32 31 30 3b 0d 0a 20 20 73 74   u8 yy210;..  st
3c57f 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65 3b  ruct {int value;
3c580 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 32 31   int mask;} yy21
3c581 35 3b 0d 0a 20 20 53 72 63 4c 69 73 74 2a 20 79  5;..  SrcList* y
3c582 79 32 35 39 3b 0d 0a 20 20 73 74 72 75 63 74 20  y259;..  struct 
3c583 4c 69 6d 69 74 56 61 6c 20 79 79 32 39 32 3b 0d  LimitVal yy292;.
3c584 0a 20 20 45 78 70 72 2a 20 79 79 33 31 34 3b 0d  .  Expr* yy314;.
3c585 0a 20 20 45 78 70 72 4c 69 73 74 2a 20 79 79 33  .  ExprList* yy3
3c586 32 32 3b 0d 0a 20 20 73 74 72 75 63 74 20 4c 69  22;..  struct Li
3c587 6b 65 4f 70 20 79 79 33 34 32 3b 0d 0a 20 20 49  keOp yy342;..  I
3c588 64 4c 69 73 74 2a 20 79 79 33 38 34 3b 0d 0a 20  dList* yy384;.. 
3c589 20 53 65 6c 65 63 74 2a 20 79 79 33 38 37 3b 0d   Select* yy387;.
3c58a 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45 3b 0d  .} YYMINORTYPE;.
3c58b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41 43 4b  .#ifndef YYSTACK
3c58c 44 45 50 54 48 0d 0a 23 64 65 66 69 6e 65 20 59  DEPTH..#define Y
3c58d 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30 0d  YSTACKDEPTH 100.
3c58e 0a 23 65 6e 64 69 66 0d 0a 23 64 65 66 69 6e 65  .#endif..#define
3c58f 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
3c590 47 5f 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70  G_SDECL Parse *p
3c591 50 61 72 73 65 3b 0d 0a 23 64 65 66 69 6e 65 20  Parse;..#define 
3c592 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
3c593 5f 50 44 45 43 4c 20 2c 50 61 72 73 65 20 2a 70  _PDECL ,Parse *p
3c594 50 61 72 73 65 0d 0a 23 64 65 66 69 6e 65 20 73  Parse..#define s
3c595 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
3c596 46 45 54 43 48 20 50 61 72 73 65 20 2a 70 50 61  FETCH Parse *pPa
3c597 72 73 65 20 3d 20 79 79 70 50 61 72 73 65 72 2d  rse = yypParser-
3c598 3e 70 50 61 72 73 65 0d 0a 23 64 65 66 69 6e 65  >pParse..#define
3c599 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
3c59a 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73 65  G_STORE yypParse
3c59b 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  r->pParse = pPar
3c59c 73 65 0d 0a 23 64 65 66 69 6e 65 20 59 59 4e 53  se..#define YYNS
3c59d 54 41 54 45 20 36 33 30 0d 0a 23 64 65 66 69 6e  TATE 630..#defin
3c59e 65 20 59 59 4e 52 55 4c 45 20 33 32 39 0d 0a 23  e YYNRULE 329..#
3c59f 64 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43  define YYFALLBAC
3c5a0 4b 20 31 0d 0a 23 64 65 66 69 6e 65 20 59 59 5f  K 1..#define YY_
3c5a1 4e 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28  NO_ACTION      (
3c5a2 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
3c5a3 2b 32 29 0d 0a 23 64 65 66 69 6e 65 20 59 59 5f  +2)..#define YY_
3c5a4 41 43 43 45 50 54 5f 41 43 54 49 4f 4e 20 20 28  ACCEPT_ACTION  (
3c5a5 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
3c5a6 2b 31 29 0d 0a 23 64 65 66 69 6e 65 20 59 59 5f  +1)..#define YY_
3c5a7 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28  ERROR_ACTION   (
3c5a8 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
3c5a9 29 0d 0a 0d 0a 2f 2a 20 54 68 65 20 79 79 7a 65  )..../* The yyze
3c5aa 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74 61 6e 74  rominor constant
3c5ab 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 69 74   is used to init
3c5ac 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e 63 65 73  ialize instances
3c5ad 20 6f 66 0d 0a 2a 2a 20 59 59 4d 49 4e 4f 52 54   of..** YYMINORT
3c5ae 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f 20 7a  YPE objects to z
3c5af 65 72 6f 2e 20 2a 2f 0d 0a 73 74 61 74 69 63 20  ero. */..static 
3c5b0 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54 59 50  const YYMINORTYP
3c5b1 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 3d 20  E yyzerominor = 
3c5b2 7b 20 30 20 7d 3b 0d 0a 0d 0a 2f 2a 20 44 65 66  { 0 };..../* Def
3c5b3 69 6e 65 20 74 68 65 20 79 79 74 65 73 74 63 61  ine the yytestca
3c5b4 73 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 62 65  se() macro to be
3c5b5 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 6e   a no-op if is n
3c5b6 6f 74 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e  ot already defin
3c5b7 65 64 0d 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  ed..** otherwise
3c5b8 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 70 70 6c 69 63  ...**..** Applic
3c5b9 61 74 69 6f 6e 73 20 63 61 6e 20 63 68 6f 6f 73  ations can choos
3c5ba 65 20 74 6f 20 64 65 66 69 6e 65 20 79 79 74 65  e to define yyte
3c5bb 73 74 63 61 73 65 28 29 20 69 6e 20 74 68 65 20  stcase() in the 
3c5bc 25 69 6e 63 6c 75 64 65 20 73 65 63 74 69 6f 6e  %include section
3c5bd 0d 0a 2a 2a 20 74 6f 20 61 20 6d 61 63 72 6f 20  ..** to a macro 
3c5be 74 68 61 74 20 63 61 6e 20 61 73 73 69 73 74 20  that can assist 
3c5bf 69 6e 20 76 65 72 69 66 79 69 6e 67 20 63 6f 64  in verifying cod
3c5c0 65 20 63 6f 76 65 72 61 67 65 2e 20 20 46 6f 72  e coverage.  For
3c5c1 20 70 72 6f 64 75 63 74 69 6f 6e 0d 0a 2a 2a 20   production..** 
3c5c2 63 6f 64 65 20 74 68 65 20 79 79 74 65 73 74 63  code the yytestc
3c5c3 61 73 65 28 29 20 6d 61 63 72 6f 20 73 68 6f 75  ase() macro shou
3c5c4 6c 64 20 62 65 20 74 75 72 6e 65 64 20 6f 66 66  ld be turned off
3c5c5 2e 20 20 42 75 74 20 69 74 20 69 73 20 75 73 65  .  But it is use
3c5c6 66 75 6c 0d 0a 2a 2a 20 66 6f 72 20 74 65 73 74  ful..** for test
3c5c7 69 6e 67 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65  ing...*/..#ifnde
3c5c8 66 20 79 79 74 65 73 74 63 61 73 65 0d 0a 23 20  f yytestcase..# 
3c5c9 64 65 66 69 6e 65 20 79 79 74 65 73 74 63 61 73  define yytestcas
3c5ca 65 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  e(X)..#endif....
3c5cb 0d 0a 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68  ../* Next are th
3c5cc 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f  e tables used to
3c5cd 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
3c5ce 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62  action to take b
3c5cf 61 73 65 64 20 6f 6e 20 74 68 65 0d 0a 2a 2a 20  ased on the..** 
3c5d0 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 61 6e  current state an
3c5d1 64 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65  d lookahead toke
3c5d2 6e 2e 20 20 54 68 65 73 65 20 74 61 62 6c 65 73  n.  These tables
3c5d3 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70   are used to imp
3c5d4 6c 65 6d 65 6e 74 0d 0a 2a 2a 20 66 75 6e 63 74  lement..** funct
3c5d5 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 61  ions that take a
3c5d6 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 61 6e   state number an
3c5d7 64 20 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75  d lookahead valu
3c5d8 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 0d  e and return an.
3c5d9 0a 2a 2a 20 61 63 74 69 6f 6e 20 69 6e 74 65 67  .** action integ
3c5da 65 72 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 53 75  er.  ..**..** Su
3c5db 70 70 6f 73 65 20 74 68 65 20 61 63 74 69 6f 6e  ppose the action
3c5dc 20 69 6e 74 65 67 65 72 20 69 73 20 4e 2e 20 20   integer is N.  
3c5dd 54 68 65 6e 20 74 68 65 20 61 63 74 69 6f 6e 20  Then the action 
3c5de 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 61 73  is determined as
3c5df 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0d 0a 2a 2a  ..** follows..**
3c5e0 0d 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c 20  ..**   0 <= N < 
3c5e1 59 59 4e 53 54 41 54 45 20 20 20 20 20 20 20 20  YYNSTATE        
3c5e2 20 20 20 20 20 20 20 20 20 20 53 68 69 66 74 20            Shift 
3c5e3 4e 2e 20 20 54 68 61 74 20 69 73 2c 20 70 75 73  N.  That is, pus
3c5e4 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 0d  h the lookahead.
3c5e5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3c5e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c5e7 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f           token o
3c5e8 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e  nto the stack an
3c5e9 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0d  d goto state N..
3c5ea 0a 2a 2a 0d 0a 2a 2a 20 20 20 59 59 4e 53 54 41  .**..**   YYNSTA
3c5eb 54 45 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41  TE <= N < YYNSTA
3c5ec 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64  TE+YYNRULE   Red
3c5ed 75 63 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59  uce by rule N-YY
3c5ee 4e 53 54 41 54 45 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  NSTATE...**..** 
3c5ef 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b    N == YYNSTATE+
3c5f0 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20 20  YYNRULE         
3c5f1 20 20 20 20 20 41 20 73 79 6e 74 61 78 20 65 72       A syntax er
3c5f2 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
3c5f3 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 4e 20 3d 3d  ...**..**   N ==
3c5f4 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
3c5f5 45 2b 31 20 20 20 20 20 20 20 20 20 20 20 20 54  E+1            T
3c5f6 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74  he parser accept
3c5f7 73 20 69 74 73 20 69 6e 70 75 74 2e 0d 0a 2a 2a  s its input...**
3c5f8 0d 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  ..**   N == YYNS
3c5f9 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20  TATE+YYNRULE+2  
3c5fa 20 20 20 20 20 20 20 20 20 20 4e 6f 20 73 75 63            No suc
3c5fb 68 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74  h action.  Denot
3c5fc 65 73 20 75 6e 75 73 65 64 0d 0a 2a 2a 20 20 20  es unused..**   
3c5fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c5fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c5ff 20 20 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20     slots in the 
3c600 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c  yy_action[] tabl
3c601 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  e...**..** The a
3c602 63 74 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 63  ction table is c
3c603 6f 6e 73 74 72 75 63 74 65 64 20 61 73 20 61 20  onstructed as a 
3c604 73 69 6e 67 6c 65 20 6c 61 72 67 65 20 74 61 62  single large tab
3c605 6c 65 20 6e 61 6d 65 64 20 79 79 5f 61 63 74 69  le named yy_acti
3c606 6f 6e 5b 5d 2e 0d 0a 2a 2a 20 47 69 76 65 6e 20  on[]...** Given 
3c607 73 74 61 74 65 20 53 20 61 6e 64 20 6c 6f 6f 6b  state S and look
3c608 61 68 65 61 64 20 58 2c 20 74 68 65 20 61 63 74  ahead X, the act
3c609 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74 65 64 20  ion is computed 
3c60a 61 73 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 20  as..**..**      
3c60b 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f 73 68  yy_action[ yy_sh
3c60c 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20 58 20  ift_ofst[S] + X 
3c60d 5d 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ]..**..** If the
3c60e 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79 79 5f   index value yy_
3c60f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20  shift_ofst[S]+X 
3c610 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
3c611 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 65 0d  or if the value.
3c612 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  .** yy_lookahead
3c613 5b 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53  [yy_shift_ofst[S
3c614 5d 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75 61  ]+X] is not equa
3c615 6c 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79 5f  l to X or if yy_
3c616 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0d 0a 2a  shift_ofst[S]..*
3c617 2a 20 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59  * is equal to YY
3c618 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c  _SHIFT_USE_DFLT,
3c619 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
3c61a 68 65 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74  he action is not
3c61b 20 69 6e 20 74 68 65 20 74 61 62 6c 65 0d 0a 2a   in the table..*
3c61c 2a 20 61 6e 64 20 74 68 61 74 20 79 79 5f 64 65  * and that yy_de
3c61d 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20  fault[S] should 
3c61e 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e  be used instead.
3c61f 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66    ..**..** The f
3c620 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73 20  ormula above is 
3c621 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74 68  for computing th
3c622 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74 68  e action when th
3c623 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0d 0a  e lookahead is..
3c624 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79  ** a terminal sy
3c625 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f  mbol.  If the lo
3c626 6f 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e  okahead is a non
3c627 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63  -terminal (as oc
3c628 63 75 72 73 20 61 66 74 65 72 0d 0a 2a 2a 20 61  curs after..** a
3c629 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20   reduce action) 
3c62a 74 68 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75  then the yy_redu
3c62b 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20  ce_ofst[] array 
3c62c 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  is used in place
3c62d 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 79 79 5f 73   of..** the yy_s
3c62e 68 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61  hift_ofst[] arra
3c62f 79 20 61 6e 64 20 59 59 5f 52 45 44 55 43 45 5f  y and YY_REDUCE_
3c630 55 53 45 5f 44 46 4c 54 20 69 73 20 75 73 65 64  USE_DFLT is used
3c631 20 69 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   in place of..**
3c632 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
3c633 4c 54 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  LT...**..** The 
3c634 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68  following are th
3c635 65 20 74 61 62 6c 65 73 20 67 65 6e 65 72 61 74  e tables generat
3c636 65 64 20 69 6e 20 74 68 69 73 20 73 65 63 74 69  ed in this secti
3c637 6f 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 79 79 5f  on:..**..**  yy_
3c638 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20 20 20 20  action[]        
3c639 41 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 63  A single table c
3c63a 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 61 63  ontaining all ac
3c63b 74 69 6f 6e 73 2e 0d 0a 2a 2a 20 20 79 79 5f 6c  tions...**  yy_l
3c63c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20 20 41  ookahead[]     A
3c63d 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
3c63e 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  g the lookahead 
3c63f 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20 69  for each entry i
3c640 6e 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
3c641 20 20 20 20 20 20 20 20 20 20 79 79 5f 61 63 74            yy_act
3c642 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20 64 65  ion.  Used to de
3c643 74 65 63 74 20 68 61 73 68 20 63 6f 6c 6c 69 73  tect hash collis
3c644 69 6f 6e 73 2e 0d 0a 2a 2a 20 20 79 79 5f 73 68  ions...**  yy_sh
3c645 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f  ift_ofst[]    Fo
3c646 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74 68  r each state, th
3c647 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79  e offset into yy
3c648 5f 61 63 74 69 6f 6e 20 66 6f 72 0d 0a 2a 2a 20  _action for..** 
3c649 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c64a 20 20 20 20 73 68 69 66 74 69 6e 67 20 74 65 72      shifting ter
3c64b 6d 69 6e 61 6c 73 2e 0d 0a 2a 2a 20 20 79 79 5f  minals...**  yy_
3c64c 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20  reduce_ofst[]   
3c64d 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20  For each state, 
3c64e 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20  the offset into 
3c64f 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0d 0a 2a  yy_action for..*
3c650 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3c651 20 20 20 20 20 20 73 68 69 66 74 69 6e 67 20 6e        shifting n
3c652 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20 61 66 74  on-terminals aft
3c653 65 72 20 61 20 72 65 64 75 63 65 2e 0d 0a 2a 2a  er a reduce...**
3c654 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 20    yy_default[]  
3c655 20 20 20 20 20 44 65 66 61 75 6c 74 20 61 63 74       Default act
3c656 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73 74 61  ion for each sta
3c657 74 65 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  te...*/..#define
3c658 20 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54   YY_ACTTAB_COUNT
3c659 20 28 31 35 35 37 29 0d 0a 73 74 61 74 69 63 20   (1557)..static 
3c65a 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59  const YYACTIONTY
3c65b 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d  PE yy_action[] =
3c65c 20 7b 0d 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f   {.. /*     0 */
3c65d 20 20 20 33 31 33 2c 20 20 39 36 30 2c 20 20 31     313,  960,  1
3c65e 38 36 2c 20 20 34 31 39 2c 20 20 20 20 32 2c 20  86,  419,    2, 
3c65f 20 31 37 32 2c 20 20 36 32 37 2c 20 20 35 39 37   172,  627,  597
3c660 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0d 0a 20  ,   55,   55,.. 
3c661 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 35  /*    10 */    5
3c662 35 2c 20 20 20 35 35 2c 20 20 20 34 38 2c 20 20  5,   55,   48,  
3c663 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
3c664 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
3c665 32 2c 20 20 20 35 31 2c 0d 0a 20 2f 2a 20 20 20  2,   51,.. /*   
3c666 20 32 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20   20 */    51,   
3c667 35 31 2c 20 20 20 35 30 2c 20 20 32 33 38 2c 20  51,   50,  238, 
3c668 20 33 30 32 2c 20 20 32 38 33 2c 20 20 36 32 33   302,  283,  623
3c669 2c 20 20 36 32 32 2c 20 20 35 31 36 2c 20 20 35  ,  622,  516,  5
3c66a 31 35 2c 0d 0a 20 2f 2a 20 20 20 20 33 30 20 2a  15,.. /*    30 *
3c66b 2f 20 20 20 35 39 30 2c 20 20 35 38 34 2c 20 20  /   590,  584,  
3c66c 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
3c66d 20 20 20 35 35 2c 20 20 32 38 32 2c 20 20 20 35     55,  282,   5
3c66e 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 0d 0a  3,   53,   53,..
3c66f 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 20   /*    40 */    
3c670 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20  53,   52,   52, 
3c671 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31    51,   51,   51
3c672 2c 20 20 20 35 30 2c 20 20 32 33 38 2c 20 20 20  ,   50,  238,   
3c673 20 36 2c 20 20 20 35 36 2c 0d 0a 20 2f 2a 20 20   6,   56,.. /*  
3c674 20 20 35 30 20 2a 2f 20 20 20 20 35 37 2c 20 20    50 */    57,  
3c675 20 34 37 2c 20 20 35 38 32 2c 20 20 35 38 31 2c   47,  582,  581,
3c676 20 20 35 38 33 2c 20 20 35 38 33 2c 20 20 20 35    583,  583,   5
3c677 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
3c678 20 35 35 2c 0d 0a 20 2f 2a 20 20 20 20 36 30 20   55,.. /*    60 
3c679 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20  */    55,   55, 
3c67a 20 36 30 38 2c 20 20 20 35 33 2c 20 20 20 35 33   608,   53,   53
3c67b 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
3c67c 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0d  52,   52,   51,.
3c67d 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
3c67e 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c   51,   51,   50,
3c67f 20 20 32 33 38 2c 20 20 33 31 33 2c 20 20 35 39    238,  313,  59
3c680 37 2c 20 20 34 30 39 2c 20 20 33 33 30 2c 20 20  7,  409,  330,  
3c681 35 37 39 2c 20 20 35 37 39 2c 0d 0a 20 2f 2a 20  579,  579,.. /* 
3c682 20 20 20 38 30 20 2a 2f 20 20 20 20 33 32 2c 20     80 */    32, 
3c683 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
3c684 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20  ,   53,   52,   
3c685 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  52,   51,   51, 
3c686 20 20 35 31 2c 0d 0a 20 2f 2a 20 20 20 20 39 30    51,.. /*    90
3c687 20 2a 2f 20 20 20 20 35 30 2c 20 20 32 33 38 2c   */    50,  238,
3c688 20 20 33 33 30 2c 20 20 32 31 37 2c 20 20 36 32    330,  217,  62
3c689 30 2c 20 20 36 31 39 2c 20 20 31 36 36 2c 20 20  0,  619,  166,  
3c68a 34 31 31 2c 20 20 36 32 34 2c 20 20 33 38 32 2c  411,  624,  382,
3c68b 0d 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  .. /*   100 */  
3c68c 20 33 37 39 2c 20 20 33 37 38 2c 20 20 20 20 37   379,  378,    7
3c68d 2c 20 20 34 39 31 2c 20 20 35 39 30 2c 20 20 35  ,  491,  590,  5
3c68e 38 34 2c 20 20 32 30 30 2c 20 20 31 39 39 2c 20  84,  200,  199, 
3c68f 20 31 39 38 2c 20 20 20 35 38 2c 0d 0a 20 2f 2a   198,   58,.. /*
3c690 20 20 20 31 31 30 20 2a 2f 20 20 20 33 37 37 2c     110 */   377,
3c691 20 20 33 30 30 2c 20 20 34 31 34 2c 20 20 36 32    300,  414,  62
3c692 31 2c 20 20 34 38 31 2c 20 20 20 36 36 2c 20 20  1,  481,   66,  
3c693 36 32 33 2c 20 20 36 32 32 2c 20 20 36 32 31 2c  623,  622,  621,
3c694 20 20 35 38 30 2c 0d 0a 20 2f 2a 20 20 20 31 32    580,.. /*   12
3c695 30 20 2a 2f 20 20 20 32 35 34 2c 20 20 36 30 31  0 */   254,  601
3c696 2c 20 20 20 39 34 2c 20 20 20 35 36 2c 20 20 20  ,   94,   56,   
3c697 35 37 2c 20 20 20 34 37 2c 20 20 35 38 32 2c 20  57,   47,  582, 
3c698 20 35 38 31 2c 20 20 35 38 33 2c 20 20 35 38 33   581,  583,  583
3c699 2c 0d 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20  ,.. /*   130 */ 
3c69a 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3c69b 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
3c69c 20 35 35 2c 20 20 36 37 31 2c 20 20 20 35 33 2c   55,  671,   53,
3c69d 20 20 20 35 33 2c 20 20 20 35 33 2c 0d 0a 20 2f     53,   53,.. /
3c69e 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 35 33  *   140 */    53
3c69f 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
3c6a0 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
3c6a1 20 20 35 30 2c 20 20 32 33 38 2c 20 20 33 31 33    50,  238,  313
3c6a2 2c 20 20 35 33 32 2c 0d 0a 20 2f 2a 20 20 20 31  ,  532,.. /*   1
3c6a3 35 30 20 2a 2f 20 20 20 32 32 36 2c 20 20 35 30  50 */   226,  50
3c6a4 36 2c 20 20 35 30 37 2c 20 20 31 33 33 2c 20 20  6,  507,  133,  
3c6a5 31 37 37 2c 20 20 31 33 39 2c 20 20 32 38 34 2c  177,  139,  284,
3c6a6 20 20 33 38 35 2c 20 20 32 37 39 2c 20 20 33 38    385,  279,  38
3c6a7 34 2c 0d 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  4,.. /*   160 */
3c6a8 20 20 20 31 36 39 2c 20 20 31 39 37 2c 20 20 33     169,  197,  3
3c6a9 34 32 2c 20 20 33 39 38 2c 20 20 32 35 31 2c 20  42,  398,  251, 
3c6aa 20 32 32 36 2c 20 20 32 35 33 2c 20 20 32 37 35   226,  253,  275
3c6ab 2c 20 20 33 38 38 2c 20 20 31 36 37 2c 0d 0a 20  ,  388,  167,.. 
3c6ac 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 31 33  /*   170 */   13
3c6ad 39 2c 20 20 32 38 34 2c 20 20 33 38 35 2c 20 20  9,  284,  385,  
3c6ae 32 37 39 2c 20 20 33 38 34 2c 20 20 31 36 39 2c  279,  384,  169,
3c6af 20 20 35 37 30 2c 20 20 32 33 36 2c 20 20 35 39    570,  236,  59
3c6b0 30 2c 20 20 35 38 34 2c 0d 0a 20 2f 2a 20 20 20  0,  584,.. /*   
3c6b1 31 38 30 20 2a 2f 20 20 20 36 37 32 2c 20 20 32  180 */   672,  2
3c6b2 34 30 2c 20 20 32 37 35 2c 20 20 31 35 37 2c 20  40,  275,  157, 
3c6b3 20 36 32 30 2c 20 20 36 31 39 2c 20 20 35 35 34   620,  619,  554
3c6b4 2c 20 20 34 33 37 2c 20 20 20 35 31 2c 20 20 20  ,  437,   51,   
3c6b5 35 31 2c 0d 0a 20 2f 2a 20 20 20 31 39 30 20 2a  51,.. /*   190 *
3c6b6 2f 20 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20  /    51,   50,  
3c6b7 32 33 38 2c 20 20 33 34 33 2c 20 20 34 33 39 2c  238,  343,  439,
3c6b8 20 20 35 35 33 2c 20 20 34 33 38 2c 20 20 20 35    553,  438,   5
3c6b9 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 0d 0a  6,   57,   47,..
3c6ba 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 35   /*   200 */   5
3c6bb 38 32 2c 20 20 35 38 31 2c 20 20 35 38 33 2c 20  82,  581,  583, 
3c6bc 20 35 38 33 2c 20 20 20 35 34 2c 20 20 20 35 34   583,   54,   54
3c6bd 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
3c6be 35 35 2c 20 20 20 35 35 2c 0d 0a 20 2f 2a 20 20  55,   55,.. /*  
3c6bf 20 32 31 30 20 2a 2f 20 20 20 34 36 35 2c 20 20   210 */   465,  
3c6c0 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
3c6c1 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
3c6c2 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
3c6c3 20 35 31 2c 0d 0a 20 2f 2a 20 20 20 32 32 30 20   51,.. /*   220 
3c6c4 2a 2f 20 20 20 20 35 30 2c 20 20 32 33 38 2c 20  */    50,  238, 
3c6c5 20 33 31 33 2c 20 20 33 39 30 2c 20 20 20 35 32   313,  390,   52
3c6c6 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20  ,   52,   51,   
3c6c7 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 0d  51,   51,   50,.
3c6c8 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20  . /*   230 */   
3c6c9 32 33 38 2c 20 20 33 39 31 2c 20 20 31 36 36 2c  238,  391,  166,
3c6ca 20 20 34 39 31 2c 20 20 35 36 36 2c 20 20 33 38    491,  566,  38
3c6cb 32 2c 20 20 33 37 39 2c 20 20 33 37 38 2c 20 20  2,  379,  378,  
3c6cc 34 30 39 2c 20 20 34 34 30 2c 0d 0a 20 2f 2a 20  409,  440,.. /* 
3c6cd 20 20 32 34 30 20 2a 2f 20 20 20 35 37 39 2c 20    240 */   579, 
3c6ce 20 35 37 39 2c 20 20 32 35 32 2c 20 20 34 34 30   579,  252,  440
3c6cf 2c 20 20 36 30 37 2c 20 20 20 36 36 2c 20 20 33  ,  607,   66,  3
3c6d0 37 37 2c 20 20 35 31 33 2c 20 20 36 32 31 2c 20  77,  513,  621, 
3c6d1 20 20 34 39 2c 0d 0a 20 2f 2a 20 20 20 32 35 30    49,.. /*   250
3c6d2 20 2a 2f 20 20 20 20 34 36 2c 20 20 31 34 37 2c   */    46,  147,
3c6d3 20 20 35 39 30 2c 20 20 35 38 34 2c 20 20 36 32    590,  584,  62
3c6d4 31 2c 20 20 20 31 36 2c 20 20 34 36 36 2c 20 20  1,   16,  466,  
3c6d5 31 38 39 2c 20 20 36 32 31 2c 20 20 34 34 31 2c  189,  621,  441,
3c6d6 0d 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  .. /*   260 */  
3c6d7 20 34 34 32 2c 20 20 36 37 33 2c 20 20 35 32 36   442,  673,  526
3c6d8 2c 20 20 34 34 31 2c 20 20 33 34 30 2c 20 20 35  ,  441,  340,  5
3c6d9 37 37 2c 20 20 35 39 35 2c 20 20 20 36 34 2c 20  77,  595,   64, 
3c6da 20 31 39 34 2c 20 20 34 38 32 2c 0d 0a 20 2f 2a   194,  482,.. /*
3c6db 20 20 20 32 37 30 20 2a 2f 20 20 20 34 33 34 2c     270 */   434,
3c6dc 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34     56,   57,   4
3c6dd 37 2c 20 20 35 38 32 2c 20 20 35 38 31 2c 20 20  7,  582,  581,  
3c6de 35 38 33 2c 20 20 35 38 33 2c 20 20 20 35 34 2c  583,  583,   54,
3c6df 20 20 20 35 34 2c 0d 0a 20 2f 2a 20 20 20 32 38     54,.. /*   28
3c6e0 30 20 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35  0 */    55,   55
3c6e1 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
3c6e2 33 30 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  30,   53,   53, 
3c6e3 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
3c6e4 2c 0d 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20  ,.. /*   290 */ 
3c6e5 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35     52,   51,   5
3c6e6 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20  1,   51,   50,  
3c6e7 32 33 38 2c 20 20 33 31 33 2c 20 20 35 39 33 2c  238,  313,  593,
3c6e8 20 20 35 39 33 2c 20 20 35 39 33 2c 0d 0a 20 2f    593,  593,.. /
3c6e9 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 33 38 37  *   300 */   387
3c6ea 2c 20 20 35 37 38 2c 20 20 36 30 36 2c 20 20 34  ,  578,  606,  4
3c6eb 39 33 2c 20 20 32 35 39 2c 20 20 33 35 31 2c 20  93,  259,  351, 
3c6ec 20 32 35 38 2c 20 20 34 31 31 2c 20 20 20 20 31   258,  411,    1
3c6ed 2c 20 20 36 32 33 2c 0d 0a 20 2f 2a 20 20 20 33  ,  623,.. /*   3
3c6ee 31 30 20 2a 2f 20 20 20 36 32 32 2c 20 20 34 39  10 */   622,  49
3c6ef 36 2c 20 20 36 32 33 2c 20 20 36 32 32 2c 20 20  6,  623,  622,  
3c6f0 20 36 35 2c 20 20 32 34 30 2c 20 20 36 32 33 2c   65,  240,  623,
3c6f1 20 20 36 32 32 2c 20 20 35 39 37 2c 20 20 34 34    622,  597,  44
3c6f2 33 2c 0d 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f  3,.. /*   320 */
3c6f3 20 20 20 32 33 37 2c 20 20 32 33 39 2c 20 20 34     237,  239,  4
3c6f4 31 34 2c 20 20 33 34 31 2c 20 20 32 33 37 2c 20  14,  341,  237, 
3c6f5 20 36 30 32 2c 20 20 35 39 30 2c 20 20 35 38 34   602,  590,  584
3c6f6 2c 20 20 20 31 38 2c 20 20 36 30 33 2c 0d 0a 20  ,   18,  603,.. 
3c6f7 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 31 36  /*   330 */   16
3c6f8 36 2c 20 20 36 30 31 2c 20 20 20 38 37 2c 20 20  6,  601,   87,  
3c6f9 33 38 32 2c 20 20 33 37 39 2c 20 20 33 37 38 2c  382,  379,  378,
3c6fa 20 20 20 36 37 2c 20 20 36 32 33 2c 20 20 36 32     67,  623,  62
3c6fb 32 2c 20 20 20 33 38 2c 0d 0a 20 2f 2a 20 20 20  2,   38,.. /*   
3c6fc 33 34 30 20 2a 2f 20 20 20 36 32 33 2c 20 20 36  340 */   623,  6
3c6fd 32 32 2c 20 20 31 37 36 2c 20 20 32 37 30 2c 20  22,  176,  270, 
3c6fe 20 33 37 37 2c 20 20 20 35 36 2c 20 20 20 35 37   377,   56,   57
3c6ff 2c 20 20 20 34 37 2c 20 20 35 38 32 2c 20 20 35  ,   47,  582,  5
3c700 38 31 2c 0d 0a 20 2f 2a 20 20 20 33 35 30 20 2a  81,.. /*   350 *
3c701 2f 20 20 20 35 38 33 2c 20 20 35 38 33 2c 20 20  /   583,  583,  
3c702 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c   54,   54,   55,
3c703 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
3c704 35 2c 20 20 31 37 35 2c 20 20 20 35 33 2c 0d 0a  5,  175,   53,..
3c705 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 20   /*   360 */    
3c706 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
3c707 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31    52,   52,   51
3c708 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
3c709 35 30 2c 20 20 32 33 38 2c 0d 0a 20 2f 2a 20 20  50,  238,.. /*  
3c70a 20 33 37 30 20 2a 2f 20 20 20 33 31 33 2c 20 20   370 */   313,  
3c70b 33 39 36 2c 20 20 32 33 33 2c 20 20 34 31 31 2c  396,  233,  411,
3c70c 20 20 35 33 31 2c 20 20 35 36 35 2c 20 20 33 31    531,  565,  31
3c70d 37 2c 20 20 36 32 30 2c 20 20 36 31 39 2c 20 20  7,  620,  619,  
3c70e 20 34 34 2c 0d 0a 20 2f 2a 20 20 20 33 38 30 20   44,.. /*   380 
3c70f 2a 2f 20 20 20 36 32 30 2c 20 20 36 31 39 2c 20  */   620,  619, 
3c710 20 32 34 30 2c 20 20 32 30 36 2c 20 20 36 32 30   240,  206,  620
3c711 2c 20 20 36 31 39 2c 20 20 35 39 37 2c 20 20 32  ,  619,  597,  2
3c712 36 36 2c 20 20 34 31 34 2c 20 20 32 36 38 2c 0d  66,  414,  268,.
3c713 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20  . /*   390 */   
3c714 34 30 39 2c 20 20 35 39 37 2c 20 20 35 37 39 2c  409,  597,  579,
3c715 20 20 35 37 39 2c 20 20 33 35 32 2c 20 20 31 38    579,  352,  18
3c716 34 2c 20 20 35 30 35 2c 20 20 36 30 31 2c 20 20  4,  505,  601,  
3c717 20 37 33 2c 20 20 35 33 33 2c 0d 0a 20 2f 2a 20   73,  533,.. /* 
3c718 20 20 34 30 30 20 2a 2f 20 20 20 35 39 30 2c 20    400 */   590, 
3c719 20 35 38 34 2c 20 20 34 36 36 2c 20 20 35 34 38   584,  466,  548
3c71a 2c 20 20 31 39 30 2c 20 20 36 32 30 2c 20 20 36  ,  190,  620,  6
3c71b 31 39 2c 20 20 35 37 36 2c 20 20 36 32 30 2c 20  19,  576,  620, 
3c71c 20 36 31 39 2c 0d 0a 20 2f 2a 20 20 20 34 31 30   619,.. /*   410
3c71d 20 2a 2f 20 20 20 35 34 37 2c 20 20 33 38 33 2c   */   547,  383,
3c71e 20 20 35 35 31 2c 20 20 20 33 35 2c 20 20 33 33    551,   35,  33
3c71f 32 2c 20 20 35 37 35 2c 20 20 35 37 34 2c 20 20  2,  575,  574,  
3c720 36 30 30 2c 20 20 35 30 34 2c 20 20 20 35 36 2c  600,  504,   56,
3c721 0d 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  .. /*   420 */  
3c722 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 32    57,   47,  582
3c723 2c 20 20 35 38 31 2c 20 20 35 38 33 2c 20 20 35  ,  581,  583,  5
3c724 38 33 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20  83,   54,   54, 
3c725 20 20 35 35 2c 20 20 20 35 35 2c 0d 0a 20 2f 2a    55,   55,.. /*
3c726 20 20 20 34 33 30 20 2a 2f 20 20 20 20 35 35 2c     430 */    55,
3c727 20 20 20 35 35 2c 20 20 35 36 37 2c 20 20 20 35     55,  567,   5
3c728 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
3c729 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
3c72a 20 20 20 35 31 2c 0d 0a 20 2f 2a 20 20 20 34 34     51,.. /*   44
3c72b 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35 31  0 */    51,   51
3c72c 2c 20 20 20 35 30 2c 20 20 32 33 38 2c 20 20 33  ,   50,  238,  3
3c72d 31 33 2c 20 20 34 31 31 2c 20 20 35 36 31 2c 20  13,  411,  561, 
3c72e 20 35 36 31 2c 20 20 35 32 38 2c 20 20 33 36 34   561,  528,  364
3c72f 2c 0d 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20  ,.. /*   450 */ 
3c730 20 20 32 35 39 2c 20 20 33 35 31 2c 20 20 32 35    259,  351,  25
3c731 38 2c 20 20 31 38 33 2c 20 20 33 36 31 2c 20 20  8,  183,  361,  
3c732 35 34 39 2c 20 20 35 32 34 2c 20 20 33 37 34 2c  549,  524,  374,
3c733 20 20 34 31 31 2c 20 20 35 39 37 2c 0d 0a 20 2f    411,  597,.. /
3c734 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 34 31 34  *   460 */   414
3c735 2c 20 20 32 34 30 2c 20 20 35 36 30 2c 20 20 35  ,  240,  560,  5
3c736 36 30 2c 20 20 34 30 39 2c 20 20 36 30 34 2c 20  60,  409,  604, 
3c737 20 35 37 39 2c 20 20 35 37 39 2c 20 20 33 32 38   579,  579,  328
3c738 2c 20 20 36 30 31 2c 0d 0a 20 2f 2a 20 20 20 34  ,  601,.. /*   4
3c739 37 30 20 2a 2f 20 20 20 20 39 33 2c 20 20 36 32  70 */    93,  62
3c73a 33 2c 20 20 36 32 32 2c 20 20 34 31 34 2c 20 20  3,  622,  414,  
3c73b 35 39 30 2c 20 20 35 38 34 2c 20 20 32 33 37 2c  590,  584,  237,
3c73c 20 20 35 36 34 2c 20 20 35 35 39 2c 20 20 35 35    564,  559,  55
3c73d 39 2c 0d 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f  9,.. /*   480 */
3c73e 20 20 20 35 32 30 2c 20 20 34 30 32 2c 20 20 36     520,  402,  6
3c73f 30 31 2c 20 20 20 38 37 2c 20 20 34 30 39 2c 20  01,   87,  409, 
3c740 20 32 31 30 2c 20 20 35 37 39 2c 20 20 35 37 39   210,  579,  579
3c741 2c 20 20 31 36 38 2c 20 20 34 32 31 2c 0d 0a 20  ,  168,  421,.. 
3c742 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 39 35  /*   490 */   95
3c743 30 2c 20 20 35 31 39 2c 20 20 39 35 30 2c 20 20  0,  519,  950,  
3c744 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c   56,   57,   47,
3c745 20 20 35 38 32 2c 20 20 35 38 31 2c 20 20 35 38    582,  581,  58
3c746 33 2c 20 20 35 38 33 2c 0d 0a 20 2f 2a 20 20 20  3,  583,.. /*   
3c747 35 30 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 20  500 */    54,   
3c748 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  54,   55,   55, 
3c749 20 20 35 35 2c 20 20 20 35 35 2c 20 20 31 39 32    55,   55,  192
3c74a 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
3c74b 35 33 2c 0d 0a 20 2f 2a 20 20 20 35 31 30 20 2a  53,.. /*   510 *
3c74c 2f 20 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  /    53,   52,  
3c74d 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   52,   51,   51,
3c74e 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
3c74f 38 2c 20 20 33 31 33 2c 20 20 36 30 30 2c 0d 0a  8,  313,  600,..
3c750 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 32   /*   520 */   2
3c751 39 33 2c 20 20 35 36 33 2c 20 20 35 31 31 2c 20  93,  563,  511, 
3c752 20 32 33 34 2c 20 20 33 35 37 2c 20 20 31 34 36   234,  357,  146
3c753 2c 20 20 34 37 35 2c 20 20 34 37 35 2c 20 20 33  ,  475,  475,  3
3c754 36 37 2c 20 20 34 31 31 2c 0d 0a 20 2f 2a 20 20  67,  411,.. /*  
3c755 20 35 33 30 20 2a 2f 20 20 20 35 36 32 2c 20 20   530 */   562,  
3c756 34 31 31 2c 20 20 33 35 38 2c 20 20 35 34 32 2c  411,  358,  542,
3c757 20 20 34 32 35 2c 20 20 31 37 31 2c 20 20 34 31    425,  171,  41
3c758 31 2c 20 20 32 31 35 2c 20 20 31 34 34 2c 20 20  1,  215,  144,  
3c759 36 32 30 2c 0d 0a 20 2f 2a 20 20 20 35 34 30 20  620,.. /*   540 
3c75a 2a 2f 20 20 20 36 31 39 2c 20 20 35 34 34 2c 20  */   619,  544, 
3c75b 20 33 31 38 2c 20 20 33 35 33 2c 20 20 34 31 34   318,  353,  414
3c75c 2c 20 20 32 30 33 2c 20 20 34 31 34 2c 20 20 32  ,  203,  414,  2
3c75d 37 35 2c 20 20 35 39 30 2c 20 20 35 38 34 2c 0d  75,  590,  584,.
3c75e 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20  . /*   550 */   
3c75f 35 34 39 2c 20 20 34 31 34 2c 20 20 31 37 34 2c  549,  414,  174,
3c760 20 20 36 30 31 2c 20 20 20 39 34 2c 20 20 36 30    601,   94,  60
3c761 31 2c 20 20 20 37 39 2c 20 20 35 35 38 2c 20 20  1,   79,  558,  
3c762 34 37 31 2c 20 20 20 36 31 2c 0d 0a 20 2f 2a 20  471,   61,.. /* 
3c763 20 20 35 36 30 20 2a 2f 20 20 20 36 30 31 2c 20    560 */   601, 
3c764 20 20 37 39 2c 20 20 34 32 31 2c 20 20 39 34 39    79,  421,  949
3c765 2c 20 20 33 35 30 2c 20 20 39 34 39 2c 20 20 20  ,  350,  949,   
3c766 33 34 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20  34,   56,   57, 
3c767 20 20 34 37 2c 0d 0a 20 2f 2a 20 20 20 35 37 30    47,.. /*   570
3c768 20 2a 2f 20 20 20 35 38 32 2c 20 20 35 38 31 2c   */   582,  581,
3c769 20 20 35 38 33 2c 20 20 35 38 33 2c 20 20 20 35    583,  583,   5
3c76a 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
3c76b 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
3c76c 0d 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  .. /*   580 */  
3c76d 20 35 33 35 2c 20 20 20 35 33 2c 20 20 20 35 33   535,   53,   53
3c76e 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
3c76f 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20  52,   52,   51, 
3c770 20 20 35 31 2c 20 20 20 35 31 2c 0d 0a 20 2f 2a    51,   51,.. /*
3c771 20 20 20 35 39 30 20 2a 2f 20 20 20 20 35 30 2c     590 */    50,
3c772 20 20 32 33 38 2c 20 20 33 31 33 2c 20 20 33 30    238,  313,  30
3c773 37 2c 20 20 34 32 34 2c 20 20 33 39 34 2c 20 20  7,  424,  394,  
3c774 32 37 32 2c 20 20 20 34 39 2c 20 20 20 34 36 2c  272,   49,   46,
3c775 20 20 31 34 37 2c 0d 0a 20 2f 2a 20 20 20 36 30    147,.. /*   60
3c776 30 20 2a 2f 20 20 20 33 34 39 2c 20 20 33 32 32  0 */   349,  322
3c777 2c 20 20 20 20 34 2c 20 20 34 31 31 2c 20 20 34  ,    4,  411,  4
3c778 39 31 2c 20 20 33 31 32 2c 20 20 33 32 31 2c 20  91,  312,  321, 
3c779 20 34 32 35 2c 20 20 35 36 38 2c 20 20 34 39 32   425,  568,  492
3c77a 2c 0d 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20  ,.. /*   610 */ 
3c77b 20 20 32 31 36 2c 20 20 32 36 34 2c 20 20 34 30    216,  264,  40
3c77c 37 2c 20 20 35 37 35 2c 20 20 35 37 34 2c 20 20  7,  575,  574,  
3c77d 34 32 39 2c 20 20 20 36 36 2c 20 20 35 34 39 2c  429,   66,  549,
3c77e 20 20 34 31 34 2c 20 20 36 32 31 2c 0d 0a 20 2f    414,  621,.. /
3c77f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 35 34 30  *   620 */   540
3c780 2c 20 20 36 30 32 2c 20 20 35 39 30 2c 20 20 35  ,  602,  590,  5
3c781 38 34 2c 20 20 20 31 33 2c 20 20 36 30 33 2c 20  84,   13,  603, 
3c782 20 36 32 31 2c 20 20 36 30 31 2c 20 20 20 37 32   621,  601,   72
3c783 2c 20 20 20 31 32 2c 0d 0a 20 2f 2a 20 20 20 36  ,   12,.. /*   6
3c784 33 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 36 31  30 */   618,  61
3c785 37 2c 20 20 36 31 36 2c 20 20 32 30 32 2c 20 20  7,  616,  202,  
3c786 32 31 30 2c 20 20 36 32 31 2c 20 20 35 34 36 2c  210,  621,  546,
3c787 20 20 34 36 39 2c 20 20 34 32 32 2c 20 20 33 31    469,  422,  31
3c788 39 2c 0d 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f  9,.. /*   640 */
3c789 20 20 20 31 34 38 2c 20 20 20 35 36 2c 20 20 20     148,   56,   
3c78a 35 37 2c 20 20 20 34 37 2c 20 20 35 38 32 2c 20  57,   47,  582, 
3c78b 20 35 38 31 2c 20 20 35 38 33 2c 20 20 35 38 33   581,  583,  583
3c78c 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 0d 0a 20  ,   54,   54,.. 
3c78d 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 20 35  /*   650 */    5
3c78e 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
3c78f 20 35 35 2c 20 20 33 33 38 2c 20 20 20 35 33 2c   55,  338,   53,
3c790 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3c791 33 2c 20 20 20 35 32 2c 0d 0a 20 2f 2a 20 20 20  3,   52,.. /*   
3c792 36 36 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20  660 */    52,   
3c793 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
3c794 20 20 35 30 2c 20 20 32 33 38 2c 20 20 33 31 33    50,  238,  313
3c795 2c 20 20 36 30 30 2c 20 20 36 30 30 2c 20 20 34  ,  600,  600,  4
3c796 31 31 2c 0d 0a 20 2f 2a 20 20 20 36 37 30 20 2a  11,.. /*   670 *
3c797 2f 20 20 20 20 33 39 2c 20 20 20 32 31 2c 20 20  /    39,   21,  
3c798 20 33 37 2c 20 20 31 37 30 2c 20 20 32 33 37 2c   37,  170,  237,
3c799 20 20 38 37 35 2c 20 20 34 31 31 2c 20 20 35 37    875,  411,  57
3c79a 32 2c 20 20 35 37 32 2c 20 20 32 30 31 2c 0d 0a  2,  572,  201,..
3c79b 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 31   /*   680 */   1
3c79c 34 34 2c 20 20 34 37 33 2c 20 20 35 33 38 2c 20  44,  473,  538, 
3c79d 20 33 33 31 2c 20 20 34 31 34 2c 20 20 34 37 34   331,  414,  474
3c79e 2c 20 20 31 34 33 2c 20 20 31 34 36 2c 20 20 36  ,  143,  146,  6
3c79f 33 30 2c 20 20 36 32 38 2c 0d 0a 20 2f 2a 20 20  30,  628,.. /*  
3c7a0 20 36 39 30 20 2a 2f 20 20 20 33 33 34 2c 20 20   690 */   334,  
3c7a1 34 31 34 2c 20 20 33 35 33 2c 20 20 36 30 31 2c  414,  353,  601,
3c7a2 20 20 20 36 38 2c 20 20 31 36 38 2c 20 20 35 39     68,  168,  59
3c7a3 30 2c 20 20 35 38 34 2c 20 20 31 33 32 2c 20 20  0,  584,  132,  
3c7a4 33 36 35 2c 0d 0a 20 2f 2a 20 20 20 37 30 30 20  365,.. /*   700 
3c7a5 2a 2f 20 20 20 36 30 31 2c 20 20 20 39 36 2c 20  */   601,   96, 
3c7a6 20 33 30 37 2c 20 20 34 32 33 2c 20 20 35 33 30   307,  423,  530
3c7a7 2c 20 20 33 33 36 2c 20 20 20 34 39 2c 20 20 20  ,  336,   49,   
3c7a8 34 36 2c 20 20 31 34 37 2c 20 20 35 36 38 2c 0d  46,  147,  568,.
3c7a9 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20  . /*   710 */   
3c7aa 34 30 36 2c 20 20 32 31 36 2c 20 20 35 34 39 2c  406,  216,  549,
3c7ab 20 20 33 36 30 2c 20 20 35 32 39 2c 20 20 20 35    360,  529,   5
3c7ac 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20  6,   57,   47,  
3c7ad 35 38 32 2c 20 20 35 38 31 2c 0d 0a 20 2f 2a 20  582,  581,.. /* 
3c7ae 20 20 37 32 30 20 2a 2f 20 20 20 35 38 33 2c 20    720 */   583, 
3c7af 20 35 38 33 2c 20 20 20 35 34 2c 20 20 20 35 34   583,   54,   54
3c7b0 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
3c7b1 35 35 2c 20 20 20 35 35 2c 20 20 34 31 31 2c 20  55,   55,  411, 
3c7b2 20 20 35 33 2c 0d 0a 20 2f 2a 20 20 20 37 33 30    53,.. /*   730
3c7b3 20 2a 2f 20 20 20 20 35 33 2c 20 20 20 35 33 2c   */    53,   53,
3c7b4 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
3c7b5 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
3c7b6 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 38 2c   51,   50,  238,
3c7b7 0d 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20  .. /*   740 */  
3c7b8 20 33 31 33 2c 20 20 34 31 31 2c 20 20 36 30 35   313,  411,  605
3c7b9 2c 20 20 34 31 34 2c 20 20 34 38 34 2c 20 20 35  ,  414,  484,  5
3c7ba 31 30 2c 20 20 31 37 32 2c 20 20 34 32 32 2c 20  10,  172,  422, 
3c7bb 20 35 39 37 2c 20 20 33 31 38 2c 0d 0a 20 2f 2a   597,  318,.. /*
3c7bc 20 20 20 37 35 30 20 2a 2f 20 20 20 34 39 36 2c     750 */   496,
3c7bd 20 20 34 38 35 2c 20 20 36 30 31 2c 20 20 20 39    485,  601,   9
3c7be 39 2c 20 20 34 31 31 2c 20 20 31 34 32 2c 20 20  9,  411,  142,  
3c7bf 34 31 34 2c 20 20 34 31 31 2c 20 20 32 33 31 2c  414,  411,  231,
3c7c0 20 20 34 31 31 2c 0d 0a 20 2f 2a 20 20 20 37 36    411,.. /*   76
3c7c1 30 20 2a 2f 20 20 20 35 34 30 2c 20 20 34 31 31  0 */   540,  411
3c7c2 2c 20 20 33 35 39 2c 20 20 36 32 39 2c 20 20 20  ,  359,  629,   
3c7c3 20 32 2c 20 20 36 30 31 2c 20 20 20 39 37 2c 20   2,  601,   97, 
3c7c4 20 34 32 36 2c 20 20 33 30 38 2c 20 20 34 31 34   426,  308,  414
3c7c5 2c 0d 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20  ,.. /*   770 */ 
3c7c6 20 20 35 39 30 2c 20 20 35 38 34 2c 20 20 34 31    590,  584,  41
3c7c7 34 2c 20 20 20 32 30 2c 20 20 34 31 34 2c 20 20  4,   20,  414,  
3c7c8 36 32 31 2c 20 20 34 31 34 2c 20 20 36 32 31 2c  621,  414,  621,
3c7c9 20 20 36 30 31 2c 20 20 31 30 36 2c 0d 0a 20 2f    601,  106,.. /
3c7ca 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 35 30 33  *   780 */   503
3c7cb 2c 20 20 36 30 31 2c 20 20 31 30 35 2c 20 20 36  ,  601,  105,  6
3c7cc 30 31 2c 20 20 31 30 38 2c 20 20 36 30 31 2c 20  01,  108,  601, 
3c7cd 20 31 30 39 2c 20 20 32 30 34 2c 20 20 20 32 38   109,  204,   28
3c7ce 2c 20 20 20 35 36 2c 0d 0a 20 2f 2a 20 20 20 37  ,   56,.. /*   7
3c7cf 39 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34  90 */    57,   4
3c7d0 37 2c 20 20 35 38 32 2c 20 20 35 38 31 2c 20 20  7,  582,  581,  
3c7d1 35 38 33 2c 20 20 35 38 33 2c 20 20 20 35 34 2c  583,  583,   54,
3c7d2 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35     54,   55,   5
3c7d3 35 2c 0d 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f  5,.. /*   800 */
3c7d4 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34      55,   55,  4
3c7d5 31 31 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  11,   53,   53, 
3c7d6 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
3c7d7 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0d 0a 20  ,   52,   51,.. 
3c7d8 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20 35  /*   810 */    5
3c7d9 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20  1,   51,   50,  
3c7da 32 33 38 2c 20 20 33 31 33 2c 20 20 34 31 31 2c  238,  313,  411,
3c7db 20 20 35 39 37 2c 20 20 34 31 34 2c 20 20 34 31    597,  414,  41
3c7dc 31 2c 20 20 32 37 36 2c 0d 0a 20 2f 2a 20 20 20  1,  276,.. /*   
3c7dd 38 32 30 20 2a 2f 20 20 20 32 31 34 2c 20 20 36  820 */   214,  6
3c7de 30 30 2c 20 20 34 31 31 2c 20 20 33 36 36 2c 20  00,  411,  366, 
3c7df 20 32 31 33 2c 20 20 33 38 31 2c 20 20 36 30 31   213,  381,  601
3c7e0 2c 20 20 31 33 34 2c 20 20 32 37 34 2c 20 20 35  ,  134,  274,  5
3c7e1 30 30 2c 0d 0a 20 2f 2a 20 20 20 38 33 30 20 2a  00,.. /*   830 *
3c7e2 2f 20 20 20 34 31 34 2c 20 20 31 36 37 2c 20 20  /   414,  167,  
3c7e3 31 33 30 2c 20 20 34 31 34 2c 20 20 36 32 31 2c  130,  414,  621,
3c7e4 20 20 34 31 31 2c 20 20 33 35 34 2c 20 20 34 31    411,  354,  41
3c7e5 34 2c 20 20 33 37 36 2c 20 20 36 30 31 2c 0d 0a  4,  376,  601,..
3c7e6 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 31   /*   840 */   1
3c7e7 33 35 2c 20 20 31 32 39 2c 20 20 36 30 31 2c 20  35,  129,  601, 
3c7e8 20 31 30 30 2c 20 20 35 39 30 2c 20 20 35 38 34   100,  590,  584
3c7e9 2c 20 20 36 30 31 2c 20 20 31 30 34 2c 20 20 35  ,  601,  104,  5
3c7ea 32 32 2c 20 20 35 32 31 2c 0d 0a 20 2f 2a 20 20  22,  521,.. /*  
3c7eb 20 38 35 30 20 2a 2f 20 20 20 34 31 34 2c 20 20   850 */   414,  
3c7ec 36 32 31 2c 20 20 32 32 34 2c 20 20 32 37 33 2c  621,  224,  273,
3c7ed 20 20 36 30 30 2c 20 20 31 36 37 2c 20 20 33 32    600,  167,  32
3c7ee 37 2c 20 20 32 38 32 2c 20 20 36 30 30 2c 20 20  7,  282,  600,  
3c7ef 36 30 31 2c 0d 0a 20 2f 2a 20 20 20 38 36 30 20  601,.. /*   860 
3c7f0 2a 2f 20 20 20 31 30 33 2c 20 20 34 36 38 2c 20  */   103,  468, 
3c7f1 20 35 32 31 2c 20 20 20 35 36 2c 20 20 20 35 37   521,   56,   57
3c7f2 2c 20 20 20 34 37 2c 20 20 35 38 32 2c 20 20 35  ,   47,  582,  5
3c7f3 38 31 2c 20 20 35 38 33 2c 20 20 35 38 33 2c 0d  81,  583,  583,.
3c7f4 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20  . /*   870 */   
3c7f5 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c   54,   54,   55,
3c7f6 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
3c7f7 35 2c 20 20 34 31 31 2c 20 20 20 35 33 2c 20 20  5,  411,   53,  
3c7f8 20 35 33 2c 20 20 20 35 33 2c 0d 0a 20 2f 2a 20   53,   53,.. /* 
3c7f9 20 20 38 38 30 20 2a 2f 20 20 20 20 35 33 2c 20    880 */    53, 
3c7fa 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31    52,   52,   51
3c7fb 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
3c7fc 35 30 2c 20 20 32 33 38 2c 20 20 33 31 33 2c 20  50,  238,  313, 
3c7fd 20 34 31 31 2c 0d 0a 20 2f 2a 20 20 20 38 39 30   411,.. /*   890
3c7fe 20 2a 2f 20 20 20 20 32 37 2c 20 20 34 31 34 2c   */    27,  414,
3c7ff 20 20 34 31 31 2c 20 20 33 37 35 2c 20 20 32 37    411,  375,  27
3c800 36 2c 20 20 31 36 37 2c 20 20 33 35 39 2c 20 20  6,  167,  359,  
3c801 35 34 34 2c 20 20 20 35 30 2c 20 20 32 33 38 2c  544,   50,  238,
3c802 0d 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20  .. /*   900 */  
3c803 20 36 30 31 2c 20 20 20 39 35 2c 20 20 31 32 38   601,   95,  128
3c804 2c 20 20 32 32 33 2c 20 20 34 31 34 2c 20 20 34  ,  223,  414,  4
3c805 31 31 2c 20 20 31 36 35 2c 20 20 34 31 34 2c 20  11,  165,  414, 
3c806 20 34 31 31 2c 20 20 36 32 31 2c 0d 0a 20 2f 2a   411,  621,.. /*
3c807 20 20 20 39 31 30 20 2a 2f 20 20 20 34 31 31 2c     910 */   411,
3c808 20 20 36 32 31 2c 20 20 36 31 32 2c 20 20 36 30    621,  612,  60
3c809 31 2c 20 20 31 30 32 2c 20 20 33 37 32 2c 20 20  1,  102,  372,  
3c80a 36 30 31 2c 20 20 20 37 36 2c 20 20 35 39 30 2c  601,   76,  590,
3c80b 20 20 35 38 34 2c 0d 0a 20 2f 2a 20 20 20 39 32    584,.. /*   92
3c80c 30 20 2a 2f 20 20 20 34 31 34 2c 20 20 35 37 30  0 */   414,  570
3c80d 2c 20 20 32 33 36 2c 20 20 34 31 34 2c 20 20 34  ,  236,  414,  4
3c80e 37 30 2c 20 20 34 31 34 2c 20 20 31 36 37 2c 20  70,  414,  167, 
3c80f 20 36 32 31 2c 20 20 31 38 38 2c 20 20 36 30 31   621,  188,  601
3c810 2c 0d 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20  ,.. /*   930 */ 
3c811 20 20 20 39 38 2c 20 20 32 32 35 2c 20 20 36 30     98,  225,  60
3c812 31 2c 20 20 31 33 38 2c 20 20 36 30 31 2c 20 20  1,  138,  601,  
3c813 31 33 37 2c 20 20 32 33 32 2c 20 20 20 35 36 2c  137,  232,   56,
3c814 20 20 20 34 35 2c 20 20 20 34 37 2c 0d 0a 20 2f     45,   47,.. /
3c815 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 35 38 32  *   940 */   582
3c816 2c 20 20 35 38 31 2c 20 20 35 38 33 2c 20 20 35  ,  581,  583,  5
3c817 38 33 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20  83,   54,   54, 
3c818 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
3c819 2c 20 20 20 35 35 2c 0d 0a 20 2f 2a 20 20 20 39  ,   55,.. /*   9
3c81a 35 30 20 2a 2f 20 20 20 34 31 31 2c 20 20 20 35  50 */   411,   5
3c81b 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
3c81c 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
3c81d 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
3c81e 31 2c 0d 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f  1,.. /*   960 */
3c81f 20 20 20 20 35 30 2c 20 20 32 33 38 2c 20 20 33      50,  238,  3
3c820 31 33 2c 20 20 32 37 36 2c 20 20 32 37 36 2c 20  13,  276,  276, 
3c821 20 34 31 34 2c 20 20 34 31 31 2c 20 20 32 37 36   414,  411,  276
3c822 2c 20 20 35 34 34 2c 20 20 34 35 39 2c 0d 0a 20  ,  544,  459,.. 
3c823 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 33 35  /*   970 */   35
3c824 39 2c 20 20 31 37 31 2c 20 20 32 30 39 2c 20 20  9,  171,  209,  
3c825 34 37 39 2c 20 20 36 30 31 2c 20 20 31 33 36 2c  479,  601,  136,
3c826 20 20 36 32 38 2c 20 20 33 33 34 2c 20 20 36 32    628,  334,  62
3c827 31 2c 20 20 36 32 31 2c 0d 0a 20 2f 2a 20 20 20  1,  621,.. /*   
3c828 39 38 30 20 2a 2f 20 20 20 31 32 35 2c 20 20 34  980 */   125,  4
3c829 31 34 2c 20 20 36 32 31 2c 20 20 33 36 38 2c 20  14,  621,  368, 
3c82a 20 34 31 31 2c 20 20 36 32 31 2c 20 20 32 35 37   411,  621,  257
3c82b 2c 20 20 35 34 30 2c 20 20 35 38 39 2c 20 20 35  ,  540,  589,  5
3c82c 38 38 2c 0d 0a 20 2f 2a 20 20 20 39 39 30 20 2a  88,.. /*   990 *
3c82d 2f 20 20 20 36 30 31 2c 20 20 20 37 35 2c 20 20  /   601,   75,  
3c82e 35 39 30 2c 20 20 35 38 34 2c 20 20 34 35 38 2c  590,  584,  458,
3c82f 20 20 34 34 36 2c 20 20 20 32 33 2c 20 20 20 32    446,   23,   2
3c830 33 2c 20 20 31 32 34 2c 20 20 34 31 34 2c 0d 0a  3,  124,  414,..
3c831 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 33   /*  1000 */   3
3c832 32 36 2c 20 20 33 32 35 2c 20 20 36 32 31 2c 20  26,  325,  621, 
3c833 20 34 32 37 2c 20 20 33 32 34 2c 20 20 33 30 39   427,  324,  309
3c834 2c 20 20 36 30 30 2c 20 20 32 38 38 2c 20 20 36  ,  600,  288,  6
3c835 30 31 2c 20 20 20 39 32 2c 0d 0a 20 2f 2a 20 20  01,   92,.. /*  
3c836 31 30 31 30 20 2a 2f 20 20 20 35 38 36 2c 20 20  1010 */   586,  
3c837 35 38 35 2c 20 20 20 35 37 2c 20 20 20 34 37 2c  585,   57,   47,
3c838 20 20 35 38 32 2c 20 20 35 38 31 2c 20 20 35 38    582,  581,  58
3c839 33 2c 20 20 35 38 33 2c 20 20 20 35 34 2c 20 20  3,  583,   54,  
3c83a 20 35 34 2c 0d 0a 20 2f 2a 20 20 31 30 32 30 20   54,.. /*  1020 
3c83b 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20  */    55,   55, 
3c83c 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34 31 31    55,   55,  411
3c83d 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
3c83e 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 0d  53,   53,   52,.
3c83f 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20  . /*  1030 */   
3c840 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   52,   51,   51,
3c841 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
3c842 38 2c 20 20 33 31 33 2c 20 20 35 38 37 2c 20 20  8,  313,  587,  
3c843 34 31 31 2c 20 20 34 31 34 2c 0d 0a 20 2f 2a 20  411,  414,.. /* 
3c844 20 31 30 34 30 20 2a 2f 20 20 20 34 31 31 2c 20   1040 */   411, 
3c845 20 32 30 37 2c 20 20 36 31 31 2c 20 20 34 37 36   207,  611,  476
3c846 2c 20 20 31 37 31 2c 20 20 34 37 32 2c 20 20 31  ,  171,  472,  1
3c847 36 30 2c 20 20 31 32 33 2c 20 20 36 30 31 2c 20  60,  123,  601, 
3c848 20 20 39 31 2c 0d 0a 20 2f 2a 20 20 31 30 35 30    91,.. /*  1050
3c849 20 2a 2f 20 20 20 33 32 33 2c 20 20 32 36 31 2c   */   323,  261,
3c84a 20 20 20 31 35 2c 20 20 34 31 34 2c 20 20 34 36     15,  414,  46
3c84b 34 2c 20 20 34 31 34 2c 20 20 34 31 31 2c 20 20  4,  414,  411,  
3c84c 36 32 31 2c 20 20 34 31 31 2c 20 20 33 35 34 2c  621,  411,  354,
3c84d 0d 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20  .. /*  1060 */  
3c84e 20 32 32 32 2c 20 20 34 31 31 2c 20 20 36 30 31   222,  411,  601
3c84f 2c 20 20 20 37 34 2c 20 20 36 30 31 2c 20 20 20  ,   74,  601,   
3c850 39 30 2c 20 20 35 39 30 2c 20 20 35 38 34 2c 20  90,  590,  584, 
3c851 20 31 35 39 2c 20 20 32 36 34 2c 0d 0a 20 2f 2a   159,  264,.. /*
3c852 20 20 31 30 37 30 20 2a 2f 20 20 20 31 35 38 2c    1070 */   158,
3c853 20 20 34 31 34 2c 20 20 34 36 31 2c 20 20 34 31    414,  461,  41
3c854 34 2c 20 20 36 32 31 2c 20 20 36 30 30 2c 20 20  4,  621,  600,  
3c855 34 31 34 2c 20 20 31 32 31 2c 20 20 31 32 30 2c  414,  121,  120,
3c856 20 20 20 32 35 2c 0d 0a 20 2f 2a 20 20 31 30 38     25,.. /*  108
3c857 30 20 2a 2f 20 20 20 36 30 31 2c 20 20 20 38 39  0 */   601,   89
3c858 2c 20 20 36 30 31 2c 20 20 31 30 31 2c 20 20 36  ,  601,  101,  6
3c859 32 31 2c 20 20 36 30 31 2c 20 20 20 38 38 2c 20  21,  601,   88, 
3c85a 20 20 34 37 2c 20 20 35 38 32 2c 20 20 35 38 31    47,  582,  581
3c85b 2c 0d 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20  ,.. /*  1090 */ 
3c85c 20 20 35 38 33 2c 20 20 35 38 33 2c 20 20 20 35    583,  583,   5
3c85d 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
3c85e 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
3c85f 20 20 35 34 34 2c 20 20 20 35 33 2c 0d 0a 20 2f    544,   53,.. /
3c860 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 35 33  *  1100 */    53
3c861 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
3c862 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20  52,   52,   51, 
3c863 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30    51,   51,   50
3c864 2c 20 20 32 33 38 2c 0d 0a 20 2f 2a 20 20 31 31  ,  238,.. /*  11
3c865 31 30 20 2a 2f 20 20 20 20 34 33 2c 20 20 34 30  10 */    43,  40
3c866 35 2c 20 20 32 36 33 2c 20 20 20 20 33 2c 20 20  5,  263,    3,  
3c867 36 31 30 2c 20 20 32 36 34 2c 20 20 31 34 30 2c  610,  264,  140,
3c868 20 20 34 31 35 2c 20 20 36 32 32 2c 20 20 20 32    415,  622,   2
3c869 34 2c 0d 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f  4,.. /*  1120 */
3c86a 20 20 20 34 31 30 2c 20 20 20 31 31 2c 20 20 34     410,   11,  4
3c86b 35 36 2c 20 20 35 39 34 2c 20 20 31 31 38 2c 20  56,  594,  118, 
3c86c 20 31 35 35 2c 20 20 32 31 39 2c 20 20 34 35 32   155,  219,  452
3c86d 2c 20 20 34 30 38 2c 20 20 36 32 31 2c 0d 0a 20  ,  408,  621,.. 
3c86e 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 36 32  /*  1130 */   62
3c86f 31 2c 20 20 36 32 31 2c 20 20 31 35 36 2c 20 20  1,  621,  156,  
3c870 20 34 33 2c 20 20 34 30 35 2c 20 20 36 32 31 2c   43,  405,  621,
3c871 20 20 20 20 33 2c 20 20 32 38 36 2c 20 20 36 32      3,  286,  62
3c872 31 2c 20 20 31 31 33 2c 0d 0a 20 2f 2a 20 20 31  1,  113,.. /*  1
3c873 31 34 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 36  140 */   415,  6
3c874 32 32 2c 20 20 31 31 31 2c 20 20 34 34 35 2c 20  22,  111,  445, 
3c875 20 34 31 31 2c 20 20 34 30 30 2c 20 20 35 35 37   411,  400,  557
3c876 2c 20 20 34 30 33 2c 20 20 35 34 35 2c 20 20 20  ,  403,  545,   
3c877 31 30 2c 0d 0a 20 2f 2a 20 20 31 31 35 30 20 2a  10,.. /*  1150 *
3c878 2f 20 20 20 34 31 31 2c 20 20 34 30 38 2c 20 20  /   411,  408,  
3c879 32 36 34 2c 20 20 31 31 30 2c 20 20 32 30 35 2c  264,  110,  205,
3c87a 20 20 34 33 36 2c 20 20 35 34 31 2c 20 20 35 36    436,  541,  56
3c87b 36 2c 20 20 34 35 33 2c 20 20 34 31 34 2c 0d 0a  6,  453,  414,..
3c87c 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 36   /*  1160 */   6
3c87d 32 31 2c 20 20 36 32 31 2c 20 20 20 36 33 2c 20  21,  621,   63, 
3c87e 20 36 32 31 2c 20 20 34 33 35 2c 20 20 34 31 34   621,  435,  414
3c87f 2c 20 20 34 31 31 2c 20 20 36 32 31 2c 20 20 36  ,  411,  621,  6
3c880 30 31 2c 20 20 20 39 34 2c 0d 0a 20 2f 2a 20 20  01,   94,.. /*  
3c881 31 31 37 30 20 2a 2f 20 20 20 34 30 33 2c 20 20  1170 */   403,  
3c882 36 32 31 2c 20 20 34 31 31 2c 20 20 33 33 37 2c  621,  411,  337,
3c883 20 20 36 30 31 2c 20 20 20 38 36 2c 20 20 31 35    601,   86,  15
3c884 30 2c 20 20 20 34 30 2c 20 20 20 34 31 2c 20 20  0,   40,   41,  
3c885 35 33 34 2c 0d 0a 20 2f 2a 20 20 31 31 38 30 20  534,.. /*  1180 
3c886 2a 2f 20 20 20 35 36 36 2c 20 20 34 31 34 2c 20  */   566,  414, 
3c887 20 32 34 32 2c 20 20 32 36 34 2c 20 20 20 34 32   242,  264,   42
3c888 2c 20 20 34 31 33 2c 20 20 34 31 32 2c 20 20 34  ,  413,  412,  4
3c889 31 34 2c 20 20 36 30 30 2c 20 20 35 39 35 2c 0d  14,  600,  595,.
3c88a 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20  . /*  1190 */   
3c88b 36 30 31 2c 20 20 20 38 35 2c 20 20 31 39 31 2c  601,   85,  191,
3c88c 20 20 33 33 33 2c 20 20 31 30 37 2c 20 20 34 35    333,  107,  45
3c88d 31 2c 20 20 36 30 31 2c 20 20 20 38 34 2c 20 20  1,  601,   84,  
3c88e 36 32 31 2c 20 20 35 33 39 2c 0d 0a 20 2f 2a 20  621,  539,.. /* 
3c88f 20 31 32 30 30 20 2a 2f 20 20 20 20 34 30 2c 20   1200 */    40, 
3c890 20 20 34 31 2c 20 20 34 32 30 2c 20 20 32 33 30    41,  420,  230
3c891 2c 20 20 34 31 31 2c 20 20 31 34 39 2c 20 20 33  ,  411,  149,  3
3c892 31 36 2c 20 20 20 34 32 2c 20 20 34 31 33 2c 20  16,   42,  413, 
3c893 20 34 31 32 2c 0d 0a 20 2f 2a 20 20 31 32 31 30   412,.. /*  1210
3c894 20 2a 2f 20 20 20 33 39 38 2c 20 20 31 32 37 2c   */   398,  127,
3c895 20 20 35 39 35 2c 20 20 33 31 35 2c 20 20 36 32    595,  315,  62
3c896 31 2c 20 20 33 39 39 2c 20 20 32 37 38 2c 20 20  1,  399,  278,  
3c897 36 32 35 2c 20 20 31 38 31 2c 20 20 34 31 34 2c  625,  181,  414,
3c898 0d 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20  .. /*  1220 */  
3c899 20 35 39 33 2c 20 20 35 39 33 2c 20 20 35 39 33   593,  593,  593
3c89a 2c 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20 20  ,  592,  591,   
3c89b 31 34 2c 20 20 34 35 30 2c 20 20 34 31 31 2c 20  14,  450,  411, 
3c89c 20 36 30 31 2c 20 20 20 37 31 2c 0d 0a 20 2f 2a   601,   71,.. /*
3c89d 20 20 31 32 33 30 20 2a 2f 20 20 20 32 34 30 2c    1230 */   240,
3c89e 20 20 36 32 31 2c 20 20 20 34 33 2c 20 20 34 30    621,   43,  40
3c89f 35 2c 20 20 32 36 34 2c 20 20 20 20 33 2c 20 20  5,  264,    3,  
3c8a0 36 31 35 2c 20 20 31 38 30 2c 20 20 32 36 34 2c  615,  180,  264,
3c8a1 20 20 34 31 35 2c 0d 0a 20 2f 2a 20 20 31 32 34    415,.. /*  124
3c8a2 30 20 2a 2f 20 20 20 36 32 32 2c 20 20 36 31 34  0 */   622,  614
3c8a3 2c 20 20 34 31 34 2c 20 20 35 39 33 2c 20 20 35  ,  414,  593,  5
3c8a4 39 33 2c 20 20 35 39 33 2c 20 20 35 39 32 2c 20  93,  593,  592, 
3c8a5 20 35 39 31 2c 20 20 20 31 34 2c 20 20 36 32 31   591,   14,  621
3c8a6 2c 0d 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20  ,.. /*  1250 */ 
3c8a7 20 20 34 30 38 2c 20 20 36 30 31 2c 20 20 20 37    408,  601,   7
3c8a8 30 2c 20 20 36 32 31 2c 20 20 34 31 37 2c 20 20  0,  621,  417,  
3c8a9 20 33 33 2c 20 20 34 30 35 2c 20 20 36 31 33 2c   33,  405,  613,
3c8aa 20 20 20 20 33 2c 20 20 34 31 31 2c 0d 0a 20 2f      3,  411,.. /
3c8ab 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 32 36 34  *  1260 */   264
3c8ac 2c 20 20 34 31 31 2c 20 20 34 31 35 2c 20 20 36  ,  411,  415,  6
3c8ad 32 32 2c 20 20 34 31 38 2c 20 20 36 32 36 2c 20  22,  418,  626, 
3c8ae 20 31 37 38 2c 20 20 35 30 39 2c 20 20 20 20 38   178,  509,    8
3c8af 2c 20 20 34 30 33 2c 0d 0a 20 2f 2a 20 20 31 32  ,  403,.. /*  12
3c8b0 37 30 20 2a 2f 20 20 20 32 34 31 2c 20 20 34 31  70 */   241,  41
3c8b1 36 2c 20 20 31 32 36 2c 20 20 34 30 38 2c 20 20  6,  126,  408,  
3c8b2 34 31 34 2c 20 20 36 32 31 2c 20 20 34 31 34 2c  414,  621,  414,
3c8b3 20 20 34 34 39 2c 20 20 32 30 38 2c 20 20 35 36    449,  208,  56
3c8b4 36 2c 0d 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f  6,.. /*  1280 */
3c8b5 20 20 20 32 34 30 2c 20 20 32 32 31 2c 20 20 36     240,  221,  6
3c8b6 32 31 2c 20 20 36 30 31 2c 20 20 20 38 33 2c 20  21,  601,   83, 
3c8b7 20 36 30 31 2c 20 20 20 38 32 2c 20 20 35 39 39   601,   82,  599
3c8b8 2c 20 20 32 39 37 2c 20 20 32 37 37 2c 0d 0a 20  ,  297,  277,.. 
3c8b9 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 32 39  /*  1290 */   29
3c8ba 36 2c 20 20 20 33 30 2c 20 20 34 30 33 2c 20 20  6,   30,  403,  
3c8bb 20 33 31 2c 20 20 33 39 35 2c 20 20 32 36 34 2c   31,  395,  264,
3c8bc 20 20 32 39 35 2c 20 20 33 39 37 2c 20 20 34 38    295,  397,  48
3c8bd 39 2c 20 20 20 34 30 2c 0d 0a 20 2f 2a 20 20 31  9,   40,.. /*  1
3c8be 33 30 30 20 2a 2f 20 20 20 20 34 31 2c 20 20 34  300 */    41,  4
3c8bf 31 31 2c 20 20 35 36 36 2c 20 20 32 32 30 2c 20  11,  566,  220, 
3c8c0 20 36 32 31 2c 20 20 32 39 34 2c 20 20 20 34 32   621,  294,   42
3c8c1 2c 20 20 34 31 33 2c 20 20 34 31 32 2c 20 20 32  ,  413,  412,  2
3c8c2 37 31 2c 0d 0a 20 2f 2a 20 20 31 33 31 30 20 2a  71,.. /*  1310 *
3c8c3 2f 20 20 20 36 32 31 2c 20 20 35 39 35 2c 20 20  /   621,  595,  
3c8c4 36 30 30 2c 20 20 36 32 31 2c 20 20 20 35 39 2c  600,  621,   59,
3c8c5 20 20 20 36 30 2c 20 20 34 31 34 2c 20 20 32 36     60,  414,  26
3c8c6 39 2c 20 20 32 36 37 2c 20 20 36 32 33 2c 0d 0a  9,  267,  623,..
3c8c7 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 36   /*  1320 */   6
3c8c8 32 32 2c 20 20 20 33 36 2c 20 20 20 34 30 2c 20  22,   36,   40, 
3c8c9 20 20 34 31 2c 20 20 36 32 31 2c 20 20 36 30 31    41,  621,  601
3c8ca 2c 20 20 20 38 31 2c 20 20 35 39 38 2c 20 20 32  ,   81,  598,  2
3c8cb 33 35 2c 20 20 20 34 32 2c 0d 0a 20 2f 2a 20 20  35,   42,.. /*  
3c8cc 31 33 33 30 20 2a 2f 20 20 20 34 31 33 2c 20 20  1330 */   413,  
3c8cd 34 31 32 2c 20 20 36 32 31 2c 20 20 36 32 31 2c  412,  621,  621,
3c8ce 20 20 35 39 35 2c 20 20 32 36 35 2c 20 20 33 34    595,  265,  34
3c8cf 34 2c 20 20 34 31 31 2c 20 20 32 34 38 2c 20 20  4,  411,  248,  
3c8d0 35 35 36 2c 0d 0a 20 2f 2a 20 20 31 33 34 30 20  556,.. /*  1340 
3c8d1 2a 2f 20 20 20 31 37 33 2c 20 20 31 38 35 2c 20  */   173,  185, 
3c8d2 20 35 39 33 2c 20 20 35 39 33 2c 20 20 35 39 33   593,  593,  593
3c8d3 2c 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20 20  ,  592,  591,   
3c8d4 31 34 2c 20 20 32 31 38 2c 20 20 20 32 39 2c 0d  14,  218,   29,.
3c8d5 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20 20  . /*  1350 */   
3c8d6 36 32 31 2c 20 20 35 34 33 2c 20 20 34 31 34 2c  621,  543,  414,
3c8d7 20 20 33 30 35 2c 20 20 33 30 34 2c 20 20 33 30    305,  304,  30
3c8d8 33 2c 20 20 31 37 39 2c 20 20 33 30 31 2c 20 20  3,  179,  301,  
3c8d9 34 31 31 2c 20 20 35 36 36 2c 0d 0a 20 2f 2a 20  411,  566,.. /* 
3c8da 20 31 33 36 30 20 2a 2f 20 20 20 34 35 34 2c 20   1360 */   454, 
3c8db 20 36 30 31 2c 20 20 20 38 30 2c 20 20 32 38 39   601,   80,  289
3c8dc 2c 20 20 33 33 35 2c 20 20 35 39 33 2c 20 20 35  ,  335,  593,  5
3c8dd 39 33 2c 20 20 35 39 33 2c 20 20 35 39 32 2c 20  93,  593,  592, 
3c8de 20 35 39 31 2c 0d 0a 20 2f 2a 20 20 31 33 37 30   591,.. /*  1370
3c8df 20 2a 2f 20 20 20 20 31 34 2c 20 20 34 31 31 2c   */    14,  411,
3c8e0 20 20 32 38 37 2c 20 20 34 31 34 2c 20 20 31 35    287,  414,  15
3c8e1 31 2c 20 20 33 39 32 2c 20 20 32 34 36 2c 20 20  1,  392,  246,  
3c8e2 32 36 30 2c 20 20 34 31 31 2c 20 20 31 39 36 2c  260,  411,  196,
3c8e3 0d 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20  .. /*  1380 */  
3c8e4 20 31 39 35 2c 20 20 35 32 33 2c 20 20 36 30 31   195,  523,  601
3c8e5 2c 20 20 20 36 39 2c 20 20 34 31 31 2c 20 20 32  ,   69,  411,  2
3c8e6 34 35 2c 20 20 34 31 34 2c 20 20 35 32 36 2c 20  45,  414,  526, 
3c8e7 20 35 33 37 2c 20 20 32 38 35 2c 0d 0a 20 2f 2a   537,  285,.. /*
3c8e8 20 20 31 33 39 30 20 2a 2f 20 20 20 33 38 39 2c    1390 */   389,
3c8e9 20 20 35 39 35 2c 20 20 36 32 31 2c 20 20 34 31    595,  621,  41
3c8ea 34 2c 20 20 35 33 36 2c 20 20 36 30 31 2c 20 20  4,  536,  601,  
3c8eb 20 31 37 2c 20 20 33 36 32 2c 20 20 31 35 33 2c   17,  362,  153,
3c8ec 20 20 34 31 34 2c 0d 0a 20 2f 2a 20 20 31 34 30    414,.. /*  140
3c8ed 30 20 2a 2f 20 20 20 34 36 36 2c 20 20 34 36 33  0 */   466,  463
3c8ee 2c 20 20 36 30 31 2c 20 20 20 37 38 2c 20 20 31  ,  601,   78,  1
3c8ef 35 34 2c 20 20 34 31 34 2c 20 20 34 36 32 2c 20  54,  414,  462, 
3c8f0 20 31 35 32 2c 20 20 36 30 31 2c 20 20 20 37 37   152,  601,   77
3c8f1 2c 0d 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20  ,.. /*  1410 */ 
3c8f2 20 20 33 35 35 2c 20 20 32 35 35 2c 20 20 36 32    355,  255,  62
3c8f3 31 2c 20 20 34 35 35 2c 20 20 36 30 31 2c 20 20  1,  455,  601,  
3c8f4 20 20 39 2c 20 20 36 32 31 2c 20 20 33 38 36 2c    9,  621,  386,
3c8f5 20 20 34 34 34 2c 20 20 35 31 37 2c 0d 0a 20 2f    444,  517,.. /
3c8f6 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 32 34 37  *  1420 */   247
3c8f7 2c 20 20 36 32 31 2c 20 20 35 39 33 2c 20 20 35  ,  621,  593,  5
3c8f8 39 33 2c 20 20 35 39 33 2c 20 20 36 32 31 2c 20  93,  593,  621, 
3c8f9 20 36 32 31 2c 20 20 32 34 34 2c 20 20 36 32 31   621,  244,  621
3c8fa 2c 20 20 32 34 33 2c 0d 0a 20 2f 2a 20 20 31 34  ,  243,.. /*  14
3c8fb 33 30 20 2a 2f 20 20 20 34 33 30 2c 20 20 35 31  30 */   430,  51
3c8fc 38 2c 20 20 32 39 32 2c 20 20 36 32 31 2c 20 20  8,  292,  621,  
3c8fd 33 32 39 2c 20 20 36 32 31 2c 20 20 31 34 35 2c  329,  621,  145,
3c8fe 20 20 33 39 33 2c 20 20 32 38 30 2c 20 20 35 31    393,  280,  51
3c8ff 33 2c 0d 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f  3,.. /*  1440 */
3c900 20 20 20 32 39 31 2c 20 20 31 33 31 2c 20 20 36     291,  131,  6
3c901 32 31 2c 20 20 35 31 34 2c 20 20 36 32 31 2c 20  21,  514,  621, 
3c902 20 36 32 31 2c 20 20 33 31 31 2c 20 20 36 32 31   621,  311,  621
3c903 2c 20 20 32 35 39 2c 20 20 33 34 36 2c 0d 0a 20  ,  259,  346,.. 
3c904 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20 32 34  /*  1450 */   24
3c905 39 2c 20 20 36 32 31 2c 20 20 36 32 31 2c 20 20  9,  621,  621,  
3c906 32 32 39 2c 20 20 33 31 34 2c 20 20 36 32 31 2c  229,  314,  621,
3c907 20 20 32 32 38 2c 20 20 35 31 32 2c 20 20 32 32    228,  512,  22
3c908 37 2c 20 20 32 34 30 2c 0d 0a 20 2f 2a 20 20 31  7,  240,.. /*  1
3c909 34 36 30 20 2a 2f 20 20 20 34 39 34 2c 20 20 34  460 */   494,  4
3c90a 38 38 2c 20 20 33 31 30 2c 20 20 31 36 34 2c 20  88,  310,  164, 
3c90b 20 34 38 37 2c 20 20 34 38 36 2c 20 20 33 37 33   487,  486,  373
3c90c 2c 20 20 34 38 30 2c 20 20 31 36 33 2c 20 20 32  ,  480,  163,  2
3c90d 36 32 2c 0d 0a 20 2f 2a 20 20 31 34 37 30 20 2a  62,.. /*  1470 *
3c90e 2f 20 20 20 33 36 39 2c 20 20 33 37 31 2c 20 20  /   369,  371,  
3c90f 31 36 32 2c 20 20 20 32 36 2c 20 20 32 31 32 2c  162,   26,  212,
3c910 20 20 34 37 38 2c 20 20 34 37 37 2c 20 20 31 36    478,  477,  16
3c911 31 2c 20 20 31 34 31 2c 20 20 33 36 33 2c 0d 0a  1,  141,  363,..
3c912 20 2f 2a 20 20 31 34 38 30 20 2a 2f 20 20 20 34   /*  1480 */   4
3c913 36 37 2c 20 20 31 32 32 2c 20 20 33 33 39 2c 20  67,  122,  339, 
3c914 20 31 38 37 2c 20 20 31 31 39 2c 20 20 33 34 38   187,  119,  348
3c915 2c 20 20 33 34 37 2c 20 20 31 31 37 2c 20 20 31  ,  347,  117,  1
3c916 31 36 2c 20 20 31 31 35 2c 0d 0a 20 2f 2a 20 20  16,  115,.. /*  
3c917 31 34 39 30 20 2a 2f 20 20 20 31 31 34 2c 20 20  1490 */   114,  
3c918 31 31 32 2c 20 20 31 38 32 2c 20 20 34 35 37 2c  112,  182,  457,
3c919 20 20 33 32 30 2c 20 20 20 32 32 2c 20 20 34 33    320,   22,  43
3c91a 33 2c 20 20 34 33 32 2c 20 20 34 34 38 2c 20 20  3,  432,  448,  
3c91b 20 31 39 2c 0d 0a 20 2f 2a 20 20 31 35 30 30 20   19,.. /*  1500 
3c91c 2a 2f 20 20 20 36 30 39 2c 20 20 34 33 31 2c 20  */   609,  431, 
3c91d 20 34 32 38 2c 20 20 20 36 32 2c 20 20 31 39 33   428,   62,  193
3c91e 2c 20 20 35 39 36 2c 20 20 35 37 33 2c 20 20 32  ,  596,  573,  2
3c91f 39 38 2c 20 20 35 35 35 2c 20 20 35 35 32 2c 0d  98,  555,  552,.
3c920 0a 20 2f 2a 20 20 31 35 31 30 20 2a 2f 20 20 20  . /*  1510 */   
3c921 35 37 31 2c 20 20 34 30 34 2c 20 20 32 39 30 2c  571,  404,  290,
3c922 20 20 33 38 30 2c 20 20 34 39 38 2c 20 20 35 31    380,  498,  51
3c923 30 2c 20 20 34 39 35 2c 20 20 33 30 36 2c 20 20  0,  495,  306,  
3c924 32 38 31 2c 20 20 34 39 39 2c 0d 0a 20 2f 2a 20  281,  499,.. /* 
3c925 20 31 35 32 30 20 2a 2f 20 20 20 32 35 30 2c 20   1520 */   250, 
3c926 20 20 20 35 2c 20 20 34 39 37 2c 20 20 34 36 30     5,  497,  460
3c927 2c 20 20 33 34 35 2c 20 20 34 34 37 2c 20 20 35  ,  345,  447,  5
3c928 36 39 2c 20 20 35 35 30 2c 20 20 32 33 38 2c 20  69,  550,  238, 
3c929 20 32 39 39 2c 0d 0a 20 2f 2a 20 20 31 35 33 30   299,.. /*  1530
3c92a 20 2a 2f 20 20 20 35 32 37 2c 20 20 35 32 35 2c   */   527,  525,
3c92b 20 20 35 30 38 2c 20 20 39 36 31 2c 20 20 35 30    508,  961,  50
3c92c 32 2c 20 20 35 30 31 2c 20 20 39 36 31 2c 20 20  2,  501,  961,  
3c92d 34 30 31 2c 20 20 39 36 31 2c 20 20 32 31 31 2c  401,  961,  211,
3c92e 0d 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f 20 20  .. /*  1540 */  
3c92f 20 34 39 30 2c 20 20 33 35 36 2c 20 20 32 35 36   490,  356,  256
3c930 2c 20 20 39 36 31 2c 20 20 34 38 33 2c 20 20 39  ,  961,  483,  9
3c931 36 31 2c 20 20 39 36 31 2c 20 20 39 36 31 2c 20  61,  961,  961, 
3c932 20 39 36 31 2c 20 20 39 36 31 2c 0d 0a 20 2f 2a   961,  961,.. /*
3c933 20 20 31 35 35 30 20 2a 2f 20 20 20 39 36 31 2c    1550 */   961,
3c934 20 20 39 36 31 2c 20 20 39 36 31 2c 20 20 39 36    961,  961,  96
3c935 31 2c 20 20 39 36 31 2c 20 20 39 36 31 2c 20 20  1,  961,  961,  
3c936 33 37 30 2c 0d 0a 7d 3b 0d 0a 73 74 61 74 69 63  370,..};..static
3c937 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50   const YYCODETYP
3c938 45 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d  E yy_lookahead[]
3c939 20 3d 20 7b 0d 0a 20 2f 2a 20 20 20 20 20 30 20   = {.. /*     0 
3c93a 2a 2f 20 20 20 20 31 39 2c 20 20 31 34 32 2c 20  */    19,  142, 
3c93b 20 31 34 33 2c 20 20 31 34 34 2c 20 20 31 34 35   143,  144,  145
3c93c 2c 20 20 20 32 34 2c 20 20 20 20 31 2c 20 20 20  ,   24,    1,   
3c93d 32 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0d  26,   77,   78,.
3c93e 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
3c93f 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
3c940 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
3c941 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3c942 20 38 37 2c 20 20 20 38 38 2c 0d 0a 20 2f 2a 20   87,   88,.. /* 
3c943 20 20 20 32 30 20 2a 2f 20 20 20 20 38 39 2c 20     20 */    89, 
3c944 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32    90,   91,   92
3c945 2c 20 20 20 31 35 2c 20 20 20 39 38 2c 20 20 20  ,   15,   98,   
3c946 32 36 2c 20 20 20 32 37 2c 20 20 20 20 37 2c 20  26,   27,    7, 
3c947 20 20 20 38 2c 0d 0a 20 2f 2a 20 20 20 20 33 30     8,.. /*    30
3c948 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c   */    49,   50,
3c949 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
3c94a 39 2c 20 20 20 38 30 2c 20 20 31 30 39 2c 20 20  9,   80,  109,  
3c94b 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3c94c 0d 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20  .. /*    40 */  
3c94d 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
3c94e 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
3c94f 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
3c950 20 20 32 32 2c 20 20 20 36 38 2c 0d 0a 20 2f 2a    22,   68,.. /*
3c951 20 20 20 20 35 30 20 2a 2f 20 20 20 20 36 39 2c      50 */    69,
3c952 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
3c953 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
3c954 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
3c955 20 20 20 37 38 2c 0d 0a 20 2f 2a 20 20 20 20 36     78,.. /*    6
3c956 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30  0 */    79,   80
3c957 2c 20 20 20 32 33 2c 20 20 20 38 32 2c 20 20 20  ,   23,   82,   
3c958 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
3c959 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
3c95a 2c 0d 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  ,.. /*    70 */ 
3c95b 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
3c95c 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
3c95d 20 39 34 2c 20 20 31 31 32 2c 20 20 20 31 39 2c   94,  112,   19,
3c95e 20 20 31 31 34 2c 20 20 31 31 35 2c 0d 0a 20 2f    114,  115,.. /
3c95f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 32 35  *    80 */    25
3c960 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
3c961 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
3c962 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
3c963 2c 20 20 20 39 30 2c 0d 0a 20 2f 2a 20 20 20 20  ,   90,.. /*    
3c964 39 30 20 2a 2f 20 20 20 20 39 31 2c 20 20 20 39  90 */    91,   9
3c965 32 2c 20 20 20 31 39 2c 20 20 20 32 32 2c 20 20  2,   19,   22,  
3c966 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c   94,   95,   96,
3c967 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 39    150,  150,   9
3c968 39 2c 0d 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  9,.. /*   100 */
3c969 20 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 20     100,  101,   
3c96a 37 36 2c 20 20 31 35 30 2c 20 20 20 34 39 2c 20  76,  150,   49, 
3c96b 20 20 35 30 2c 20 20 31 30 35 2c 20 20 31 30 36    50,  105,  106
3c96c 2c 20 20 31 30 37 2c 20 20 20 35 34 2c 0d 0a 20  ,  107,   54,.. 
3c96d 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 31 31  /*   110 */   11
3c96e 30 2c 20 20 31 35 38 2c 20 20 31 36 35 2c 20 20  0,  158,  165,  
3c96f 31 36 35 2c 20 20 31 36 31 2c 20 20 31 36 32 2c  165,  161,  162,
3c970 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36     26,   27,  16
3c971 35 2c 20 20 31 31 33 2c 0d 0a 20 2f 2a 20 20 20  5,  113,.. /*   
3c972 31 32 30 20 2a 2f 20 20 20 20 31 36 2c 20 20 31  120 */    16,  1
3c973 37 34 2c 20 20 31 37 35 2c 20 20 20 36 38 2c 20  74,  175,   68, 
3c974 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
3c975 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
3c976 37 34 2c 0d 0a 20 2f 2a 20 20 20 31 33 30 20 2a  74,.. /*   130 *
3c977 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  /    75,   76,  
3c978 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
3c979 20 20 20 38 30 2c 20 20 31 31 38 2c 20 20 20 38     80,  118,   8
3c97a 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0d 0a  2,   83,   84,..
3c97b 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20   /*   140 */    
3c97c 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
3c97d 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
3c97e 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
3c97f 31 39 2c 20 20 20 32 33 2c 0d 0a 20 2f 2a 20 20  19,   23,.. /*  
3c980 20 31 35 30 20 2a 2f 20 20 20 20 39 32 2c 20 20   150 */    92,  
3c981 20 39 37 2c 20 20 20 39 38 2c 20 20 20 32 34 2c   97,   98,   24,
3c982 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20 20 39     96,   97,   9
3c983 38 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20  8,   99,  100,  
3c984 31 30 31 2c 0d 0a 20 2f 2a 20 20 20 31 36 30 20  101,.. /*   160 
3c985 2a 2f 20 20 20 31 30 32 2c 20 20 20 32 35 2c 20  */   102,   25, 
3c986 20 20 39 37 2c 20 20 32 31 36 2c 20 20 20 36 30    97,  216,   60
3c987 2c 20 20 20 39 32 2c 20 20 20 36 32 2c 20 20 31  ,   92,   62,  1
3c988 30 39 2c 20 20 32 32 31 2c 20 20 20 32 35 2c 0d  09,  221,   25,.
3c989 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20  . /*   170 */   
3c98a 20 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c   97,   98,   99,
3c98b 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30    100,  101,  10
3c98c 32 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  2,   86,   87,  
3c98d 20 34 39 2c 20 20 20 35 30 2c 0d 0a 20 2f 2a 20   49,   50,.. /* 
3c98e 20 20 31 38 30 20 2a 2f 20 20 20 31 31 38 2c 20    180 */   118, 
3c98f 20 31 31 36 2c 20 20 31 30 39 2c 20 20 20 32 35   116,  109,   25
3c990 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20  ,   94,   95,   
3c991 33 32 2c 20 20 20 39 37 2c 20 20 20 38 38 2c 20  32,   97,   88, 
3c992 20 20 38 39 2c 0d 0a 20 2f 2a 20 20 20 31 39 30    89,.. /*   190
3c993 20 2a 2f 20 20 20 20 39 30 2c 20 20 20 39 31 2c   */    90,   91,
3c994 20 20 20 39 32 2c 20 20 31 32 38 2c 20 20 31 30     92,  128,  10
3c995 34 2c 20 20 20 34 31 2c 20 20 31 30 36 2c 20 20  4,   41,  106,  
3c996 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
3c997 0d 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20  .. /*   200 */  
3c998 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
3c999 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
3c99a 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
3c99b 20 20 37 39 2c 20 20 20 38 30 2c 0d 0a 20 2f 2a    79,   80,.. /*
3c99c 20 20 20 32 31 30 20 2a 2f 20 20 20 20 31 31 2c     210 */    11,
3c99d 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
3c99e 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3c99f 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
3c9a0 20 20 20 39 30 2c 0d 0a 20 2f 2a 20 20 20 32 32     90,.. /*   22
3c9a1 30 20 2a 2f 20 20 20 20 39 31 2c 20 20 20 39 32  0 */    91,   92
3c9a2 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20  ,   19,   19,   
3c9a3 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
3c9a4 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31    89,   90,   91
3c9a5 2c 0d 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  ,.. /*   230 */ 
3c9a6 20 20 20 39 32 2c 20 20 20 32 37 2c 20 20 20 39     92,   27,   9
3c9a7 36 2c 20 20 31 35 30 2c 20 20 20 36 36 2c 20 20  6,  150,   66,  
3c9a8 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c   99,  100,  101,
3c9a9 20 20 31 31 32 2c 20 20 31 35 30 2c 0d 0a 20 2f    112,  150,.. /
3c9aa 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 31 31 34  *   240 */   114
3c9ab 2c 20 20 31 31 35 2c 20 20 31 33 38 2c 20 20 31  ,  115,  138,  1
3c9ac 35 30 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 20  50,  161,  162, 
3c9ad 20 31 31 30 2c 20 20 31 30 33 2c 20 20 31 36 35   110,  103,  165
3c9ae 2c 20 20 32 32 32 2c 0d 0a 20 2f 2a 20 20 20 32  ,  222,.. /*   2
3c9af 35 30 20 2a 2f 20 20 20 32 32 33 2c 20 20 32 32  50 */   223,  22
3c9b0 34 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  4,   49,   50,  
3c9b1 31 36 35 2c 20 20 20 32 32 2c 20 20 20 35 37 2c  165,   22,   57,
3c9b2 20 20 20 32 34 2c 20 20 31 36 35 2c 20 20 31 37     24,  165,  17
3c9b3 30 2c 0d 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  0,.. /*   260 */
3c9b4 20 20 20 31 37 31 2c 20 20 31 31 38 2c 20 20 20     171,  118,   
3c9b5 39 34 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  94,  170,  171, 
3c9b6 20 20 32 33 2c 20 20 20 39 38 2c 20 20 20 32 35    23,   98,   25
3c9b7 2c 20 20 31 38 35 2c 20 20 31 38 36 2c 0d 0a 20  ,  185,  186,.. 
3c9b8 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 32 34  /*   270 */   24
3c9b9 33 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  3,   68,   69,  
3c9ba 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
3c9bb 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
3c9bc 35 2c 20 20 20 37 36 2c 0d 0a 20 2f 2a 20 20 20  5,   76,.. /*   
3c9bd 32 38 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20  280 */    77,   
3c9be 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
3c9bf 20 31 32 36 2c 20 20 20 38 32 2c 20 20 20 38 33   126,   82,   83
3c9c0 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
3c9c1 38 36 2c 0d 0a 20 2f 2a 20 20 20 32 39 30 20 2a  86,.. /*   290 *
3c9c2 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  /    87,   88,  
3c9c3 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3c9c4 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 32     92,   19,  12
3c9c5 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 0d 0a  9,  130,  131,..
3c9c6 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 20   /*   300 */    
3c9c7 38 38 2c 20 20 20 32 33 2c 20 20 31 37 32 2c 20  88,   23,  172, 
3c9c8 20 31 37 33 2c 20 20 31 30 35 2c 20 20 31 30 36   173,  105,  106
3c9c9 2c 20 20 31 30 37 2c 20 20 31 35 30 2c 20 20 20  ,  107,  150,   
3c9ca 32 32 2c 20 20 20 32 36 2c 0d 0a 20 2f 2a 20 20  22,   26,.. /*  
3c9cb 20 33 31 30 20 2a 2f 20 20 20 20 32 37 2c 20 20   310 */    27,  
3c9cc 31 38 31 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  181,   26,   27,
3c9cd 20 20 20 32 32 2c 20 20 31 31 36 2c 20 20 20 32     22,  116,   2
3c9ce 36 2c 20 20 20 32 37 2c 20 20 20 32 36 2c 20 20  6,   27,   26,  
3c9cf 32 33 30 2c 0d 0a 20 2f 2a 20 20 20 33 32 30 20  230,.. /*   320 
3c9d0 2a 2f 20 20 20 32 33 31 2c 20 20 31 39 37 2c 20  */   231,  197, 
3c9d1 20 31 36 35 2c 20 20 32 33 30 2c 20 20 32 33 31   165,  230,  231
3c9d2 2c 20 20 31 31 33 2c 20 20 20 34 39 2c 20 20 20  ,  113,   49,   
3c9d3 35 30 2c 20 20 32 30 34 2c 20 20 31 31 37 2c 0d  50,  204,  117,.
3c9d4 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20  . /*   330 */   
3c9d5 20 39 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   96,  174,  175,
3c9d6 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30     99,  100,  10
3c9d7 31 2c 20 20 20 32 32 2c 20 20 20 32 36 2c 20 20  1,   22,   26,  
3c9d8 20 32 37 2c 20 20 31 33 36 2c 0d 0a 20 2f 2a 20   27,  136,.. /* 
3c9d9 20 20 33 34 30 20 2a 2f 20 20 20 20 32 36 2c 20    340 */    26, 
3c9da 20 20 32 37 2c 20 20 31 31 38 2c 20 20 20 31 36    27,  118,   16
3c9db 2c 20 20 31 31 30 2c 20 20 20 36 38 2c 20 20 20  ,  110,   68,   
3c9dc 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
3c9dd 20 20 37 32 2c 0d 0a 20 2f 2a 20 20 20 33 35 30    72,.. /*   350
3c9de 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c   */    73,   74,
3c9df 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
3c9e0 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
3c9e1 20 38 30 2c 20 20 31 31 38 2c 20 20 20 38 32 2c   80,  118,   82,
3c9e2 0d 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20  .. /*   360 */  
3c9e3 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
3c9e4 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
3c9e5 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
3c9e6 20 20 39 31 2c 20 20 20 39 32 2c 0d 0a 20 2f 2a    91,   92,.. /*
3c9e7 20 20 20 33 37 30 20 2a 2f 20 20 20 20 31 39 2c     370 */    19,
3c9e8 20 20 32 31 34 2c 20 20 32 31 35 2c 20 20 31 35    214,  215,  15
3c9e9 30 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20  0,   23,   23,  
3c9ea 31 35 35 2c 20 20 20 39 34 2c 20 20 20 39 35 2c  155,   94,   95,
3c9eb 20 20 20 32 32 2c 0d 0a 20 2f 2a 20 20 20 33 38     22,.. /*   38
3c9ec 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20 39 35  0 */    94,   95
3c9ed 2c 20 20 31 31 36 2c 20 20 31 36 30 2c 20 20 20  ,  116,  160,   
3c9ee 39 34 2c 20 20 20 39 35 2c 20 20 20 39 34 2c 20  94,   95,   94, 
3c9ef 20 20 36 30 2c 20 20 31 36 35 2c 20 20 20 36 32    60,  165,   62
3c9f0 2c 0d 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  ,.. /*   390 */ 
3c9f1 20 20 31 31 32 2c 20 20 20 32 36 2c 20 20 31 31    112,   26,  11
3c9f2 34 2c 20 20 31 31 35 2c 20 20 31 32 38 2c 20 20  4,  115,  128,  
3c9f3 20 32 33 2c 20 20 20 33 36 2c 20 20 31 37 34 2c   23,   36,  174,
3c9f4 20 20 31 37 35 2c 20 20 20 38 38 2c 0d 0a 20 2f    175,   88,.. /
3c9f5 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 20 34 39  *   400 */    49
3c9f6 2c 20 20 20 35 30 2c 20 20 20 35 37 2c 20 20 31  ,   50,   57,  1
3c9f7 32 30 2c 20 20 20 32 32 2c 20 20 20 39 34 2c 20  20,   22,   94, 
3c9f8 20 20 39 35 2c 20 20 20 32 33 2c 20 20 20 39 34    95,   23,   94
3c9f9 2c 20 20 20 39 35 2c 0d 0a 20 2f 2a 20 20 20 34  ,   95,.. /*   4
3c9fa 31 30 20 2a 2f 20 20 20 31 32 30 2c 20 20 20 35  10 */   120,   5
3c9fb 31 2c 20 20 20 32 35 2c 20 20 31 33 36 2c 20 20  1,   25,  136,  
3c9fc 31 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c  169,  170,  171,
3c9fd 20 20 31 39 34 2c 20 20 20 35 38 2c 20 20 20 36    194,   58,   6
3c9fe 38 2c 0d 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  8,.. /*   420 */
3c9ff 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20      69,   70,   
3ca00 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
3ca01 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
3ca02 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0d 0a 20  ,   77,   78,.. 
3ca03 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20 37  /*   430 */    7
3ca04 39 2c 20 20 20 38 30 2c 20 20 20 32 33 2c 20 20  9,   80,   23,  
3ca05 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3ca06 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
3ca07 37 2c 20 20 20 38 38 2c 0d 0a 20 2f 2a 20 20 20  7,   88,.. /*   
3ca08 34 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20  440 */    89,   
3ca09 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
3ca0a 20 20 31 39 2c 20 20 31 35 30 2c 20 20 20 31 32    19,  150,   12
3ca0b 2c 20 20 20 31 32 2c 20 20 20 32 33 2c 20 20 32  ,   12,   23,  2
3ca0c 32 38 2c 0d 0a 20 2f 2a 20 20 20 34 35 30 20 2a  28,.. /*   450 *
3ca0d 2f 20 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20  /   105,  106,  
3ca0e 31 30 37 2c 20 20 20 32 33 2c 20 20 32 33 33 2c  107,   23,  233,
3ca0f 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 20 31     25,  165,   1
3ca10 39 2c 20 20 31 35 30 2c 20 20 20 39 34 2c 0d 0a  9,  150,   94,..
3ca11 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 31   /*   460 */   1
3ca12 36 35 2c 20 20 31 31 36 2c 20 20 20 32 38 2c 20  65,  116,   28, 
3ca13 20 20 32 38 2c 20 20 31 31 32 2c 20 20 31 37 34    28,  112,  174
3ca14 2c 20 20 31 31 34 2c 20 20 31 31 35 2c 20 20 31  ,  114,  115,  1
3ca15 30 38 2c 20 20 31 37 34 2c 0d 0a 20 2f 2a 20 20  08,  174,.. /*  
3ca16 20 34 37 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   470 */   175,  
3ca17 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36 35 2c   26,   27,  165,
3ca18 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 32 33     49,   50,  23
3ca19 31 2c 20 20 20 31 31 2c 20 20 20 34 34 2c 20 20  1,   11,   44,  
3ca1a 20 34 34 2c 0d 0a 20 2f 2a 20 20 20 34 38 30 20   44,.. /*   480 
3ca1b 2a 2f 20 20 20 20 34 36 2c 20 20 20 34 36 2c 20  */    46,   46, 
3ca1c 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 31 32   174,  175,  112
3ca1d 2c 20 20 31 36 30 2c 20 20 31 31 34 2c 20 20 31  ,  160,  114,  1
3ca1e 31 35 2c 20 20 20 35 30 2c 20 20 20 32 32 2c 0d  15,   50,   22,.
3ca1f 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20  . /*   490 */   
3ca20 20 32 33 2c 20 20 20 35 37 2c 20 20 20 32 35 2c   23,   57,   25,
3ca21 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
3ca22 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
3ca23 20 37 33 2c 20 20 20 37 34 2c 0d 0a 20 2f 2a 20   73,   74,.. /* 
3ca24 20 20 35 30 30 20 2a 2f 20 20 20 20 37 35 2c 20    500 */    75, 
3ca25 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
3ca26 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
3ca27 31 39 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  19,   82,   83, 
3ca28 20 20 38 34 2c 0d 0a 20 2f 2a 20 20 20 35 31 30    84,.. /*   510
3ca29 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c   */    85,   86,
3ca2a 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
3ca2b 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
3ca2c 20 39 32 2c 20 20 20 31 39 2c 20 20 31 39 34 2c   92,   19,  194,
3ca2d 0d 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20  .. /*   520 */  
3ca2e 20 32 32 35 2c 20 20 20 32 33 2c 20 20 20 32 33   225,   23,   23
3ca2f 2c 20 20 32 31 35 2c 20 20 20 31 39 2c 20 20 20  ,  215,   19,   
3ca30 39 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20  95,  105,  106, 
3ca31 20 31 30 37 2c 20 20 31 35 30 2c 0d 0a 20 2f 2a   107,  150,.. /*
3ca32 20 20 20 35 33 30 20 2a 2f 20 20 20 20 32 33 2c     530 */    23,
3ca33 20 20 31 35 30 2c 20 20 20 32 37 2c 20 20 20 32    150,   27,   2
3ca34 33 2c 20 20 20 36 37 2c 20 20 20 32 35 2c 20 20  3,   67,   25,  
3ca35 31 35 30 2c 20 20 32 30 36 2c 20 20 32 30 37 2c  150,  206,  207,
3ca36 20 20 20 39 34 2c 0d 0a 20 2f 2a 20 20 20 35 34     94,.. /*   54
3ca37 30 20 2a 2f 20 20 20 20 39 35 2c 20 20 31 36 36  0 */    95,  166
3ca38 2c 20 20 31 30 34 2c 20 20 32 31 38 2c 20 20 31  ,  104,  218,  1
3ca39 36 35 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20  65,   22,  165, 
3ca3a 20 31 30 39 2c 20 20 20 34 39 2c 20 20 20 35 30   109,   49,   50
3ca3b 2c 0d 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20  ,.. /*   550 */ 
3ca3c 20 20 31 32 30 2c 20 20 31 36 35 2c 20 20 20 32    120,  165,   2
3ca3d 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
3ca3e 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c  174,  175,   23,
3ca3f 20 20 20 32 31 2c 20 20 32 33 34 2c 0d 0a 20 2f     21,  234,.. /
3ca40 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 31 37 34  *   560 */   174
3ca41 2c 20 20 31 37 35 2c 20 20 20 32 32 2c 20 20 20  ,  175,   22,   
3ca42 32 33 2c 20 20 32 33 39 2c 20 20 20 32 35 2c 20  23,  239,   25, 
3ca43 20 20 32 35 2c 20 20 20 36 38 2c 20 20 20 36 39    25,   68,   69
3ca44 2c 20 20 20 37 30 2c 0d 0a 20 2f 2a 20 20 20 35  ,   70,.. /*   5
3ca45 37 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37  70 */    71,   7
3ca46 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
3ca47 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
3ca48 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
3ca49 30 2c 0d 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  0,.. /*   580 */
3ca4a 20 20 20 32 30 35 2c 20 20 20 38 32 2c 20 20 20     205,   82,   
3ca4b 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
3ca4c 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
3ca4d 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0d 0a 20  ,   89,   90,.. 
3ca4e 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20 39  /*   590 */    9
3ca4f 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
3ca50 20 32 32 2c 20 20 20 32 33 2c 20 20 32 31 36 2c   22,   23,  216,
3ca51 20 20 20 32 33 2c 20 20 32 32 32 2c 20 20 32 32     23,  222,  22
3ca52 33 2c 20 20 32 32 34 2c 0d 0a 20 2f 2a 20 20 20  3,  224,.. /*   
3ca53 36 30 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 32  600 */    63,  2
3ca54 32 30 2c 20 20 20 33 35 2c 20 20 31 35 30 2c 20  20,   35,  150, 
3ca55 20 31 35 30 2c 20 20 31 36 33 2c 20 20 32 32 30   150,  163,  220
3ca56 2c 20 20 20 36 37 2c 20 20 31 36 36 2c 20 20 31  ,   67,  166,  1
3ca57 36 37 2c 0d 0a 20 2f 2a 20 20 20 36 31 30 20 2a  67,.. /*   610 *
3ca58 2f 20 20 20 31 36 38 2c 20 20 31 35 30 2c 20 20  /   168,  150,  
3ca59 31 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c  169,  170,  171,
3ca5a 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 20 32    161,  162,   2
3ca5b 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 0d 0a  5,  165,  165,..
3ca5c 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 31   /*   620 */   1
3ca5d 35 30 2c 20 20 31 31 33 2c 20 20 20 34 39 2c 20  50,  113,   49, 
3ca5e 20 20 35 30 2c 20 20 20 32 35 2c 20 20 31 31 37    50,   25,  117
3ca5f 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31  ,  165,  174,  1
3ca60 37 35 2c 20 20 20 33 35 2c 0d 0a 20 2f 2a 20 20  75,   35,.. /*  
3ca61 20 36 33 30 20 2a 2f 20 20 20 20 20 37 2c 20 20   630 */     7,  
3ca62 20 20 38 2c 20 20 20 20 39 2c 20 20 31 36 30 2c    8,    9,  160,
3ca63 20 20 31 36 30 2c 20 20 31 36 35 2c 20 20 31 32    160,  165,  12
3ca64 30 2c 20 20 31 30 30 2c 20 20 20 36 37 2c 20 20  0,  100,   67,  
3ca65 32 34 37 2c 0d 0a 20 2f 2a 20 20 20 36 34 30 20  247,.. /*   640 
3ca66 2a 2f 20 20 20 32 34 38 2c 20 20 20 36 38 2c 20  */   248,   68, 
3ca67 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
3ca68 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
3ca69 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0d  74,   75,   76,.
3ca6a 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20  . /*   650 */   
3ca6b 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
3ca6c 20 20 20 38 30 2c 20 20 31 39 33 2c 20 20 20 38     80,  193,   8
3ca6d 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
3ca6e 20 38 35 2c 20 20 20 38 36 2c 0d 0a 20 2f 2a 20   85,   86,.. /* 
3ca6f 20 20 36 36 30 20 2a 2f 20 20 20 20 38 37 2c 20    660 */    87, 
3ca70 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
3ca71 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
3ca72 31 39 2c 20 20 31 39 34 2c 20 20 31 39 34 2c 20  19,  194,  194, 
3ca73 20 31 35 30 2c 0d 0a 20 2f 2a 20 20 20 36 37 30   150,.. /*   670
3ca74 20 2a 2f 20 20 20 31 33 35 2c 20 20 20 32 34 2c   */   135,   24,
3ca75 20 20 31 33 37 2c 20 20 20 33 35 2c 20 20 32 33    137,   35,  23
3ca76 31 2c 20 20 31 33 38 2c 20 20 31 35 30 2c 20 20  1,  138,  150,  
3ca77 31 32 39 2c 20 20 31 33 30 2c 20 20 32 30 36 2c  129,  130,  206,
3ca78 0d 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20  .. /*   680 */  
3ca79 20 32 30 37 2c 20 20 20 33 30 2c 20 20 20 32 37   207,   30,   27
3ca7a 2c 20 20 32 31 33 2c 20 20 31 36 35 2c 20 20 20  ,  213,  165,   
3ca7b 33 34 2c 20 20 31 31 38 2c 20 20 20 39 35 2c 20  34,  118,   95, 
3ca7c 20 20 20 30 2c 20 20 20 20 31 2c 0d 0a 20 2f 2a     0,    1,.. /*
3ca7d 20 20 20 36 39 30 20 2a 2f 20 20 20 20 20 32 2c     690 */     2,
3ca7e 20 20 31 36 35 2c 20 20 32 31 38 2c 20 20 31 37    165,  218,  17
3ca7f 34 2c 20 20 31 37 35 2c 20 20 20 35 30 2c 20 20  4,  175,   50,  
3ca80 20 34 39 2c 20 20 20 35 30 2c 20 20 20 32 32 2c   49,   50,   22,
3ca81 20 20 20 34 38 2c 0d 0a 20 2f 2a 20 20 20 37 30     48,.. /*   70
3ca82 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35  0 */   174,  175
3ca83 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20 20  ,   22,   23,   
3ca84 32 33 2c 20 20 32 34 34 2c 20 20 32 32 32 2c 20  23,  244,  222, 
3ca85 20 32 32 33 2c 20 20 32 32 34 2c 20 20 31 36 36   223,  224,  166
3ca86 2c 0d 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20  ,.. /*   710 */ 
3ca87 20 20 31 36 37 2c 20 20 31 36 38 2c 20 20 31 32    167,  168,  12
3ca88 30 2c 20 20 32 33 39 2c 20 20 20 32 33 2c 20 20  0,  239,   23,  
3ca89 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
3ca8a 20 20 20 37 31 2c 20 20 20 37 32 2c 0d 0a 20 2f     71,   72,.. /
3ca8b 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 20 37 33  *   720 */    73
3ca8c 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
3ca8d 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
3ca8e 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30    79,   80,  150
3ca8f 2c 20 20 20 38 32 2c 0d 0a 20 2f 2a 20 20 20 37  ,   82,.. /*   7
3ca90 33 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38  30 */    83,   8
3ca91 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3ca92 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
3ca93 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
3ca94 32 2c 0d 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f  2,.. /*   740 */
3ca95 20 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31      19,  150,  1
3ca96 37 33 2c 20 20 31 36 35 2c 20 20 31 38 31 2c 20  73,  165,  181, 
3ca97 20 31 38 32 2c 20 20 20 32 34 2c 20 20 20 36 37   182,   24,   67
3ca98 2c 20 20 20 32 36 2c 20 20 31 30 34 2c 0d 0a 20  ,   26,  104,.. 
3ca99 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 31 38  /*   750 */   18
3ca9a 31 2c 20 20 31 38 38 2c 20 20 31 37 34 2c 20 20  1,  188,  174,  
3ca9b 31 37 35 2c 20 20 31 35 30 2c 20 20 20 33 39 2c  175,  150,   39,
3ca9c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 35    165,  150,   5
3ca9d 32 2c 20 20 31 35 30 2c 0d 0a 20 2f 2a 20 20 20  2,  150,.. /*   
3ca9e 37 36 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  760 */   150,  1
3ca9f 35 30 2c 20 20 31 35 30 2c 20 20 31 34 34 2c 20  50,  150,  144, 
3caa0 20 31 34 35 2c 20 20 31 37 34 2c 20 20 31 37 35   145,  174,  175
3caa1 2c 20 20 32 34 39 2c 20 20 32 35 30 2c 20 20 31  ,  249,  250,  1
3caa2 36 35 2c 0d 0a 20 2f 2a 20 20 20 37 37 30 20 2a  65,.. /*   770 *
3caa3 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  /    49,   50,  
3caa4 31 36 35 2c 20 20 20 35 32 2c 20 20 31 36 35 2c  165,   52,  165,
3caa5 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
3caa6 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0d 0a  5,  174,  175,..
3caa7 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 20   /*   780 */    
3caa8 32 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  29,  174,  175, 
3caa9 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34   174,  175,  174
3caaa 2c 20 20 31 37 35 2c 20 20 31 36 30 2c 20 20 20  ,  175,  160,   
3caab 32 32 2c 20 20 20 36 38 2c 0d 0a 20 2f 2a 20 20  22,   68,.. /*  
3caac 20 37 39 30 20 2a 2f 20 20 20 20 36 39 2c 20 20   790 */    69,  
3caad 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
3caae 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
3caaf 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
3cab0 20 37 38 2c 0d 0a 20 2f 2a 20 20 20 38 30 30 20   78,.. /*   800 
3cab1 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20  */    79,   80, 
3cab2 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33   150,   82,   83
3cab3 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
3cab4 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0d  86,   87,   88,.
3cab5 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20  . /*   810 */   
3cab6 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3cab7 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35     92,   19,  15
3cab8 30 2c 20 20 20 39 34 2c 20 20 31 36 35 2c 20 20  0,   94,  165,  
3cab9 31 35 30 2c 20 20 31 35 30 2c 0d 0a 20 2f 2a 20  150,  150,.. /* 
3caba 20 20 38 32 30 20 2a 2f 20 20 20 31 36 30 2c 20    820 */   160, 
3cabb 20 31 39 34 2c 20 20 31 35 30 2c 20 20 32 31 33   194,  150,  213
3cabc 2c 20 20 31 36 30 2c 20 20 20 35 32 2c 20 20 31  ,  160,   52,  1
3cabd 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c 20  74,  175,   23, 
3cabe 20 20 32 33 2c 0d 0a 20 2f 2a 20 20 20 38 33 30    23,.. /*   830
3cabf 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 32 35 2c   */   165,   25,
3cac0 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20 31 36     22,  165,  16
3cac1 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  5,  150,  150,  
3cac2 31 36 35 2c 20 20 20 35 32 2c 20 20 31 37 34 2c  165,   52,  174,
3cac3 0d 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20  .. /*   840 */  
3cac4 20 31 37 35 2c 20 20 20 32 32 2c 20 20 31 37 34   175,   22,  174
3cac5 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20  ,  175,   49,   
3cac6 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  50,  174,  175, 
3cac7 20 31 39 30 2c 20 20 31 39 31 2c 0d 0a 20 2f 2a   190,  191,.. /*
3cac8 20 20 20 38 35 30 20 2a 2f 20 20 20 31 36 35 2c     850 */   165,
3cac9 20 20 31 36 35 2c 20 20 32 34 30 2c 20 20 20 32    165,  240,   2
3caca 33 2c 20 20 31 39 34 2c 20 20 20 32 35 2c 20 20  3,  194,   25,  
3cacb 31 38 37 2c 20 20 31 30 39 2c 20 20 31 39 34 2c  187,  109,  194,
3cacc 20 20 31 37 34 2c 0d 0a 20 2f 2a 20 20 20 38 36    174,.. /*   86
3cacd 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 39 30  0 */   175,  190
3cace 2c 20 20 31 39 31 2c 20 20 20 36 38 2c 20 20 20  ,  191,   68,   
3cacf 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
3cad0 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
3cad1 2c 0d 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20  ,.. /*   870 */ 
3cad2 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
3cad3 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
3cad4 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c   80,  150,   82,
3cad5 20 20 20 38 33 2c 20 20 20 38 34 2c 0d 0a 20 2f     83,   84,.. /
3cad6 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 20 38 35  *   880 */    85
3cad7 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
3cad8 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
3cad9 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
3cada 2c 20 20 31 35 30 2c 0d 0a 20 2f 2a 20 20 20 38  ,  150,.. /*   8
3cadb 39 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 31 36  90 */    22,  16
3cadc 35 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20  5,  150,   23,  
3cadd 31 35 30 2c 20 20 20 32 35 2c 20 20 31 35 30 2c  150,   25,  150,
3cade 20 20 31 36 36 2c 20 20 20 39 31 2c 20 20 20 39    166,   91,   9
3cadf 32 2c 0d 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f  2,.. /*   900 */
3cae0 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20     174,  175,   
3cae1 32 32 2c 20 20 32 31 37 2c 20 20 31 36 35 2c 20  22,  217,  165, 
3cae2 20 31 35 30 2c 20 20 31 30 32 2c 20 20 31 36 35   150,  102,  165
3cae3 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0d 0a 20  ,  150,  165,.. 
3cae4 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 31 35  /*   910 */   15
3cae5 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  0,  165,  150,  
3cae6 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31 39 2c  174,  175,   19,
3cae7 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34    174,  175,   4
3cae8 39 2c 20 20 20 35 30 2c 0d 0a 20 2f 2a 20 20 20  9,   50,.. /*   
3cae9 39 32 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20  920 */   165,   
3caea 38 36 2c 20 20 20 38 37 2c 20 20 31 36 35 2c 20  86,   87,  165, 
3caeb 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35    23,  165,   25
3caec 2c 20 20 31 36 35 2c 20 20 20 32 34 2c 20 20 31  ,  165,   24,  1
3caed 37 34 2c 0d 0a 20 2f 2a 20 20 20 39 33 30 20 2a  74,.. /*   930 *
3caee 2f 20 20 20 31 37 35 2c 20 20 31 38 37 2c 20 20  /   175,  187,  
3caef 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c  174,  175,  174,
3caf0 20 20 31 37 35 2c 20 20 32 30 35 2c 20 20 20 36    175,  205,   6
3caf1 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0d 0a  8,   69,   70,..
3caf2 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 20   /*   940 */    
3caf3 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
3caf4 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
3caf5 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
3caf6 37 39 2c 20 20 20 38 30 2c 0d 0a 20 2f 2a 20 20  79,   80,.. /*  
3caf7 20 39 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   950 */   150,  
3caf8 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3caf9 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
3cafa 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
3cafb 20 39 30 2c 0d 0a 20 2f 2a 20 20 20 39 36 30 20   90,.. /*   960 
3cafc 2a 2f 20 20 20 20 39 31 2c 20 20 20 39 32 2c 20  */    91,   92, 
3cafd 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30    19,  150,  150
3cafe 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
3caff 35 30 2c 20 20 31 36 36 2c 20 20 20 32 33 2c 0d  50,  166,   23,.
3cb00 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20  . /*   970 */   
3cb01 31 35 30 2c 20 20 20 32 35 2c 20 20 31 36 30 2c  150,   25,  160,
3cb02 20 20 20 32 30 2c 20 20 31 37 34 2c 20 20 31 37     20,  174,  17
3cb03 35 2c 20 20 20 20 31 2c 20 20 20 20 32 2c 20 20  5,    1,    2,  
3cb04 31 36 35 2c 20 20 31 36 35 2c 0d 0a 20 2f 2a 20  165,  165,.. /* 
3cb05 20 20 39 38 30 20 2a 2f 20 20 20 31 30 34 2c 20    980 */   104, 
3cb06 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 34 33   165,  165,   43
3cb07 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 32  ,  150,  165,  2
3cb08 34 30 2c 20 20 31 35 30 2c 20 20 20 34 39 2c 20  40,  150,   49, 
3cb09 20 20 35 30 2c 0d 0a 20 2f 2a 20 20 20 39 39 30    50,.. /*   990
3cb0a 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c   */   174,  175,
3cb0b 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 32     49,   50,   2
3cb0c 33 2c 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20  3,   23,   25,  
3cb0d 20 32 35 2c 20 20 20 35 33 2c 20 20 31 36 35 2c   25,   53,  165,
3cb0e 0d 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20  .. /*  1000 */  
3cb0f 20 31 38 37 2c 20 20 31 38 37 2c 20 20 31 36 35   187,  187,  165
3cb10 2c 20 20 20 32 33 2c 20 20 31 38 37 2c 20 20 20  ,   23,  187,   
3cb11 32 35 2c 20 20 31 39 34 2c 20 20 32 30 35 2c 20  25,  194,  205, 
3cb12 20 31 37 34 2c 20 20 31 37 35 2c 0d 0a 20 2f 2a   174,  175,.. /*
3cb13 20 20 31 30 31 30 20 2a 2f 20 20 20 20 37 31 2c    1010 */    71,
3cb14 20 20 20 37 32 2c 20 20 20 36 39 2c 20 20 20 37     72,   69,   7
3cb15 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
3cb16 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
3cb17 20 20 20 37 36 2c 0d 0a 20 2f 2a 20 20 31 30 32     76,.. /*  102
3cb18 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37 38  0 */    77,   78
3cb19 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
3cb1a 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  50,   82,   83, 
3cb1b 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
3cb1c 2c 0d 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20  ,.. /*  1030 */ 
3cb1d 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
3cb1e 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
3cb1f 20 39 32 2c 20 20 20 31 39 2c 20 20 20 39 38 2c   92,   19,   98,
3cb20 20 20 31 35 30 2c 20 20 31 36 35 2c 0d 0a 20 2f    150,  165,.. /
3cb21 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 31 35 30  *  1040 */   150
3cb22 2c 20 20 31 36 30 2c 20 20 31 35 30 2c 20 20 20  ,  160,  150,   
3cb23 35 39 2c 20 20 20 32 35 2c 20 20 20 35 33 2c 20  59,   25,   53, 
3cb24 20 31 30 34 2c 20 20 20 32 32 2c 20 20 31 37 34   104,   22,  174
3cb25 2c 20 20 31 37 35 2c 0d 0a 20 2f 2a 20 20 31 30  ,  175,.. /*  10
3cb26 35 30 20 2a 2f 20 20 20 32 31 33 2c 20 20 31 33  50 */   213,  13
3cb27 38 2c 20 20 20 20 35 2c 20 20 31 36 35 2c 20 20  8,    5,  165,  
3cb28 20 20 31 2c 20 20 31 36 35 2c 20 20 31 35 30 2c    1,  165,  150,
3cb29 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35    165,  150,  15
3cb2a 30 2c 0d 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f  0,.. /*  1060 */
3cb2b 20 20 20 32 34 30 2c 20 20 31 35 30 2c 20 20 31     240,  150,  1
3cb2c 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
3cb2d 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30   175,   49,   50
3cb2e 2c 20 20 31 31 38 2c 20 20 31 35 30 2c 0d 0a 20  ,  118,  150,.. 
3cb2f 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 20 33  /*  1070 */    3
3cb30 35 2c 20 20 31 36 35 2c 20 20 20 32 37 2c 20 20  5,  165,   27,  
3cb31 31 36 35 2c 20 20 31 36 35 2c 20 20 31 39 34 2c  165,  165,  194,
3cb32 20 20 31 36 35 2c 20 20 31 30 38 2c 20 20 31 32    165,  108,  12
3cb33 37 2c 20 20 20 37 36 2c 0d 0a 20 2f 2a 20 20 31  7,   76,.. /*  1
3cb34 30 38 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31  080 */   174,  1
3cb35 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  75,  174,  175, 
3cb36 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
3cb37 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
3cb38 37 32 2c 0d 0a 20 2f 2a 20 20 31 30 39 30 20 2a  72,.. /*  1090 *
3cb39 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
3cb3a 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
3cb3b 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
3cb3c 30 2c 20 20 31 36 36 2c 20 20 20 38 32 2c 0d 0a  0,  166,   82,..
3cb3d 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20   /*  1100 */    
3cb3e 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
3cb3f 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
3cb40 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
3cb41 39 31 2c 20 20 20 39 32 2c 0d 0a 20 2f 2a 20 20  91,   92,.. /*  
3cb42 31 31 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20  1110 */    19,  
3cb43 20 32 30 2c 20 20 31 39 33 2c 20 20 20 32 32 2c   20,  193,   22,
3cb44 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
3cb45 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  0,   26,   27,  
3cb46 20 37 36 2c 0d 0a 20 2f 2a 20 20 31 31 32 30 20   76,.. /*  1120 
3cb47 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 32 2c 20  */   150,   22, 
3cb48 20 20 20 31 2c 20 20 31 35 30 2c 20 20 31 31 39     1,  150,  119
3cb49 2c 20 20 31 32 31 2c 20 20 32 31 37 2c 20 20 20  ,  121,  217,   
3cb4a 32 30 2c 20 20 20 33 37 2c 20 20 31 36 35 2c 0d  20,   37,  165,.
3cb4b 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20  . /*  1130 */   
3cb4c 31 36 35 2c 20 20 31 36 35 2c 20 20 20 31 36 2c  165,  165,   16,
3cb4d 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 31 36     19,   20,  16
3cb4e 35 2c 20 20 20 32 32 2c 20 20 32 30 35 2c 20 20  5,   22,  205,  
3cb4f 31 36 35 2c 20 20 31 31 39 2c 0d 0a 20 2f 2a 20  165,  119,.. /* 
3cb50 20 31 31 34 30 20 2a 2f 20 20 20 20 32 36 2c 20   1140 */    26, 
3cb51 20 20 32 37 2c 20 20 31 30 38 2c 20 20 31 32 38    27,  108,  128
3cb52 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
3cb53 35 30 2c 20 20 20 35 36 2c 20 20 31 35 30 2c 20  50,   56,  150, 
3cb54 20 20 32 32 2c 0d 0a 20 2f 2a 20 20 31 31 35 30    22,.. /*  1150
3cb55 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 33 37 2c   */   150,   37,
3cb56 20 20 31 35 30 2c 20 20 31 32 37 2c 20 20 31 36    150,  127,  16
3cb57 30 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20  0,   23,  150,  
3cb58 20 36 36 2c 20 20 31 39 33 2c 20 20 31 36 35 2c   66,  193,  165,
3cb59 0d 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20  .. /*  1160 */  
3cb5a 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 31 36   165,  165,   16
3cb5b 2c 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20 31  ,  165,   23,  1
3cb5c 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  65,  150,  165, 
3cb5d 20 31 37 34 2c 20 20 31 37 35 2c 0d 0a 20 2f 2a   174,  175,.. /*
3cb5e 20 20 31 31 37 30 20 2a 2f 20 20 20 20 35 36 2c    1170 */    56,
3cb5f 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 36    165,  150,   6
3cb60 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
3cb61 20 31 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   15,   86,   87,
3cb62 20 20 20 38 38 2c 0d 0a 20 2f 2a 20 20 31 31 38     88,.. /*  118
3cb63 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 31 36 35  0 */    66,  165
3cb64 2c 20 20 31 34 30 2c 20 20 31 35 30 2c 20 20 20  ,  140,  150,   
3cb65 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  93,   94,   95, 
3cb66 20 31 36 35 2c 20 20 31 39 34 2c 20 20 20 39 38   165,  194,   98
3cb67 2c 0d 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20  ,.. /*  1190 */ 
3cb68 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
3cb69 32 2c 20 20 20 20 33 2c 20 20 31 36 34 2c 20 20  2,    3,  164,  
3cb6a 31 39 33 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  193,  174,  175,
3cb6b 20 20 31 36 35 2c 20 20 31 35 30 2c 0d 0a 20 2f    165,  150,.. /
3cb6c 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 20 38 36  *  1200 */    86
3cb6d 2c 20 20 20 38 37 2c 20 20 20 20 34 2c 20 20 31  ,   87,    4,  1
3cb6e 38 30 2c 20 20 31 35 30 2c 20 20 32 34 38 2c 20  80,  150,  248, 
3cb6f 20 32 35 31 2c 20 20 20 39 33 2c 20 20 20 39 34   251,   93,   94
3cb70 2c 20 20 20 39 35 2c 0d 0a 20 2f 2a 20 20 31 32  ,   95,.. /*  12
3cb71 31 30 20 2a 2f 20 20 20 32 31 36 2c 20 20 31 38  10 */   216,  18
3cb72 30 2c 20 20 20 39 38 2c 20 20 32 35 31 2c 20 20  0,   98,  251,  
3cb73 31 36 35 2c 20 20 32 32 31 2c 20 20 31 35 30 2c  165,  221,  150,
3cb74 20 20 31 34 39 2c 20 20 20 20 36 2c 20 20 31 36    149,    6,  16
3cb75 35 2c 0d 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f  5,.. /*  1220 */
3cb76 20 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31     129,  130,  1
3cb77 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20  31,  132,  133, 
3cb78 20 31 33 34 2c 20 20 31 39 33 2c 20 20 31 35 30   134,  193,  150
3cb79 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0d 0a 20  ,  174,  175,.. 
3cb7a 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 31 31  /*  1230 */   11
3cb7b 36 2c 20 20 31 36 35 2c 20 20 20 31 39 2c 20 20  6,  165,   19,  
3cb7c 20 32 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c   20,  150,   22,
3cb7d 20 20 31 34 39 2c 20 20 31 35 31 2c 20 20 31 35    149,  151,  15
3cb7e 30 2c 20 20 20 32 36 2c 0d 0a 20 2f 2a 20 20 31  0,   26,.. /*  1
3cb7f 32 34 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 31  240 */    27,  1
3cb80 34 39 2c 20 20 31 36 35 2c 20 20 31 32 39 2c 20  49,  165,  129, 
3cb81 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32   130,  131,  132
3cb82 2c 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31  ,  133,  134,  1
3cb83 36 35 2c 0d 0a 20 2f 2a 20 20 31 32 35 30 20 2a  65,.. /*  1250 *
3cb84 2f 20 20 20 20 33 37 2c 20 20 31 37 34 2c 20 20  /    37,  174,  
3cb85 31 37 35 2c 20 20 31 36 35 2c 20 20 31 34 39 2c  175,  165,  149,
3cb86 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 20 31     19,   20,   1
3cb87 33 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 0d 0a  3,   22,  150,..
3cb88 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 31   /*  1260 */   1
3cb89 35 30 2c 20 20 31 35 30 2c 20 20 20 32 36 2c 20  50,  150,   26, 
3cb8a 20 20 32 37 2c 20 20 31 34 36 2c 20 20 31 34 37    27,  146,  147
3cb8b 2c 20 20 31 35 31 2c 20 20 31 35 30 2c 20 20 20  ,  151,  150,   
3cb8c 32 35 2c 20 20 20 35 36 2c 0d 0a 20 2f 2a 20 20  25,   56,.. /*  
3cb8d 31 32 37 30 20 2a 2f 20 20 20 31 35 32 2c 20 20  1270 */   152,  
3cb8e 31 35 39 2c 20 20 31 35 34 2c 20 20 20 33 37 2c  159,  154,   37,
3cb8f 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
3cb90 35 2c 20 20 31 39 33 2c 20 20 31 36 30 2c 20 20  5,  193,  160,  
3cb91 20 36 36 2c 0d 0a 20 2f 2a 20 20 31 32 38 30 20   66,.. /*  1280 
3cb92 2a 2f 20 20 20 31 31 36 2c 20 20 31 39 33 2c 20  */   116,  193, 
3cb93 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
3cb94 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
3cb95 39 34 2c 20 20 31 39 39 2c 20 20 31 35 30 2c 0d  94,  199,  150,.
3cb96 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20  . /*  1290 */   
3cb97 32 30 30 2c 20 20 31 32 36 2c 20 20 20 35 36 2c  200,  126,   56,
3cb98 20 20 31 32 34 2c 20 20 31 32 33 2c 20 20 31 35    124,  123,  15
3cb99 30 2c 20 20 32 30 31 2c 20 20 31 32 32 2c 20 20  0,  201,  122,  
3cb9a 31 35 30 2c 20 20 20 38 36 2c 0d 0a 20 2f 2a 20  150,   86,.. /* 
3cb9b 20 31 33 30 30 20 2a 2f 20 20 20 20 38 37 2c 20   1300 */    87, 
3cb9c 20 31 35 30 2c 20 20 20 36 36 2c 20 20 31 39 33   150,   66,  193
3cb9d 2c 20 20 31 36 35 2c 20 20 32 30 32 2c 20 20 20  ,  165,  202,   
3cb9e 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  93,   94,   95, 
3cb9f 20 31 35 30 2c 0d 0a 20 2f 2a 20 20 31 33 31 30   150,.. /*  1310
3cba0 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 39 38 2c   */   165,   98,
3cba1 20 20 31 39 34 2c 20 20 31 36 35 2c 20 20 31 32    194,  165,  12
3cba2 35 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20  5,   22,  165,  
3cba3 31 35 30 2c 20 20 31 35 30 2c 20 20 20 32 36 2c  150,  150,   26,
3cba4 0d 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20  .. /*  1320 */  
3cba5 20 20 32 37 2c 20 20 31 33 35 2c 20 20 20 38 36    27,  135,   86
3cba6 2c 20 20 20 38 37 2c 20 20 31 36 35 2c 20 20 31  ,   87,  165,  1
3cba7 37 34 2c 20 20 31 37 35 2c 20 20 32 30 33 2c 20  74,  175,  203, 
3cba8 20 32 32 36 2c 20 20 20 39 33 2c 0d 0a 20 2f 2a   226,   93,.. /*
3cba9 20 20 31 33 33 30 20 2a 2f 20 20 20 20 39 34 2c    1330 */    94,
3cbaa 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20 31 36     95,  165,  16
3cbab 35 2c 20 20 20 39 38 2c 20 20 31 35 30 2c 20 20  5,   98,  150,  
3cbac 32 31 38 2c 20 20 31 35 30 2c 20 20 31 39 33 2c  218,  150,  193,
3cbad 20 20 31 35 37 2c 0d 0a 20 2f 2a 20 20 31 33 34    157,.. /*  134
3cbae 30 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 35 37  0 */   118,  157
3cbaf 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31  ,  129,  130,  1
3cbb0 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20  31,  132,  133, 
3cbb1 20 31 33 34 2c 20 20 20 20 35 2c 20 20 31 30 34   134,    5,  104
3cbb2 2c 0d 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20  ,.. /*  1350 */ 
3cbb3 20 20 31 36 35 2c 20 20 32 31 31 2c 20 20 31 36    165,  211,  16
3cbb4 35 2c 20 20 20 31 30 2c 20 20 20 31 31 2c 20 20  5,   10,   11,  
3cbb5 20 31 32 2c 20 20 20 31 33 2c 20 20 20 31 34 2c   12,   13,   14,
3cbb6 20 20 31 35 30 2c 20 20 20 36 36 2c 0d 0a 20 2f    150,   66,.. /
3cbb7 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 20 31 37  *  1360 */    17
3cbb8 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32  ,  174,  175,  2
3cbb9 31 30 2c 20 20 32 34 36 2c 20 20 31 32 39 2c 20  10,  246,  129, 
3cbba 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32   130,  131,  132
3cbbb 2c 20 20 31 33 33 2c 0d 0a 20 2f 2a 20 20 31 33  ,  133,.. /*  13
3cbbc 37 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 31 35  70 */   134,  15
3cbbd 30 2c 20 20 32 31 30 2c 20 20 31 36 35 2c 20 20  0,  210,  165,  
3cbbe 20 33 31 2c 20 20 31 32 31 2c 20 20 20 33 33 2c   31,  121,   33,
3cbbf 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 38    150,  150,   8
3cbc0 36 2c 0d 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f  6,.. /*  1380 */
3cbc1 20 20 20 20 38 37 2c 20 20 31 37 36 2c 20 20 31      87,  176,  1
3cbc2 37 34 2c 20 20 31 37 35 2c 20 20 31 35 30 2c 20  74,  175,  150, 
3cbc3 20 20 34 32 2c 20 20 31 36 35 2c 20 20 20 39 34    42,  165,   94
3cbc4 2c 20 20 32 31 31 2c 20 20 32 31 30 2c 0d 0a 20  ,  211,  210,.. 
3cbc5 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 31 35  /*  1390 */   15
3cbc6 30 2c 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20  0,   98,  165,  
3cbc7 31 36 35 2c 20 20 32 31 31 2c 20 20 31 37 34 2c  165,  211,  174,
3cbc8 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20 20 35    175,  150,   5
3cbc9 35 2c 20 20 31 36 35 2c 0d 0a 20 2f 2a 20 20 31  5,  165,.. /*  1
3cbca 34 30 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 31  400 */    57,  1
3cbcb 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  50,  174,  175, 
3cbcc 20 20 36 31 2c 20 20 31 36 35 2c 20 20 31 35 30    61,  165,  150
3cbcd 2c 20 20 20 36 34 2c 20 20 31 37 34 2c 20 20 31  ,   64,  174,  1
3cbce 37 35 2c 0d 0a 20 2f 2a 20 20 31 34 31 30 20 2a  75,.. /*  1410 *
3cbcf 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  /   150,  150,  
3cbd0 31 36 35 2c 20 20 31 35 30 2c 20 20 31 37 34 2c  165,  150,  174,
3cbd1 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 31 30    175,  165,  10
3cbd2 34 2c 20 20 31 35 30 2c 20 20 31 38 34 2c 0d 0a  4,  150,  184,..
3cbd3 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 31   /*  1420 */   1
3cbd4 35 30 2c 20 20 31 36 35 2c 20 20 31 32 39 2c 20  50,  165,  129, 
3cbd5 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 36 35   130,  131,  165
3cbd6 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
3cbd7 36 35 2c 20 20 31 35 30 2c 0d 0a 20 2f 2a 20 20  65,  150,.. /*  
3cbd8 31 34 33 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1430 */   150,  
3cbd9 31 37 36 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  176,  150,  165,
3cbda 20 20 20 34 37 2c 20 20 31 36 35 2c 20 20 31 35     47,  165,  15
3cbdb 30 2c 20 20 31 35 30 2c 20 20 31 37 36 2c 20 20  0,  150,  176,  
3cbdc 31 30 33 2c 0d 0a 20 2f 2a 20 20 31 34 34 30 20  103,.. /*  1440 
3cbdd 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 32 2c 20  */   150,   22, 
3cbde 20 31 36 35 2c 20 20 31 37 38 2c 20 20 31 36 35   165,  178,  165
3cbdf 2c 20 20 31 36 35 2c 20 20 31 37 39 2c 20 20 31  ,  165,  179,  1
3cbe0 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 0d  65,  105,  106,.
3cbe1 0a 20 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20  . /*  1450 */   
3cbe2 31 30 37 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  107,  165,  165,
3cbe3 20 20 32 32 39 2c 20 20 31 31 31 2c 20 20 31 36    229,  111,  16
3cbe4 35 2c 20 20 20 39 32 2c 20 20 31 37 36 2c 20 20  5,   92,  176,  
3cbe5 32 32 39 2c 20 20 31 31 36 2c 0d 0a 20 2f 2a 20  229,  116,.. /* 
3cbe6 20 31 34 36 30 20 2a 2f 20 20 20 31 38 34 2c 20   1460 */   184, 
3cbe7 20 31 37 36 2c 20 20 31 37 39 2c 20 20 31 35 36   176,  179,  156
3cbe8 2c 20 20 31 37 36 2c 20 20 31 37 36 2c 20 20 20  ,  176,  176,   
3cbe9 31 38 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20  18,  157,  156, 
3cbea 20 32 33 37 2c 0d 0a 20 2f 2a 20 20 31 34 37 30   237,.. /*  1470
3cbeb 20 2a 2f 20 20 20 20 34 35 2c 20 20 31 35 37 2c   */    45,  157,
3cbec 20 20 31 35 36 2c 20 20 31 33 35 2c 20 20 31 35    156,  135,  15
3cbed 37 2c 20 20 31 35 37 2c 20 20 32 33 38 2c 20 20  7,  157,  238,  
3cbee 31 35 36 2c 20 20 20 36 38 2c 20 20 31 35 37 2c  156,   68,  157,
3cbef 0d 0a 20 2f 2a 20 20 31 34 38 30 20 2a 2f 20 20  .. /*  1480 */  
3cbf0 20 31 38 39 2c 20 20 31 38 39 2c 20 20 31 33 39   189,  189,  139
3cbf1 2c 20 20 32 31 39 2c 20 20 20 32 32 2c 20 20 31  ,  219,   22,  1
3cbf2 35 37 2c 20 20 20 31 38 2c 20 20 31 39 32 2c 20  57,   18,  192, 
3cbf3 20 31 39 32 2c 20 20 31 39 32 2c 0d 0a 20 2f 2a   192,  192,.. /*
3cbf4 20 20 31 34 39 30 20 2a 2f 20 20 20 31 39 32 2c    1490 */   192,
3cbf5 20 20 31 38 39 2c 20 20 32 31 39 2c 20 20 31 39    189,  219,  19
3cbf6 39 2c 20 20 31 35 37 2c 20 20 32 34 32 2c 20 20  9,  157,  242,  
3cbf7 20 34 30 2c 20 20 31 35 37 2c 20 20 31 39 39 2c   40,  157,  199,
3cbf8 20 20 32 34 32 2c 0d 0a 20 2f 2a 20 20 31 35 30    242,.. /*  150
3cbf9 30 20 2a 2f 20 20 20 31 35 33 2c 20 20 31 35 37  0 */   153,  157
3cbfa 2c 20 20 20 33 38 2c 20 20 32 34 35 2c 20 20 31  ,   38,  245,  1
3cbfb 39 36 2c 20 20 31 36 36 2c 20 20 32 33 32 2c 20  96,  166,  232, 
3cbfc 20 31 39 38 2c 20 20 31 37 37 2c 20 20 31 37 37   198,  177,  177
3cbfd 2c 0d 0a 20 2f 2a 20 20 31 35 31 30 20 2a 2f 20  ,.. /*  1510 */ 
3cbfe 20 20 32 33 32 2c 20 20 32 32 37 2c 20 20 32 30    232,  227,  20
3cbff 39 2c 20 20 31 37 38 2c 20 20 31 36 36 2c 20 20  9,  178,  166,  
3cc00 31 38 32 2c 20 20 31 36 36 2c 20 20 31 34 38 2c  182,  166,  148,
3cc01 20 20 31 37 37 2c 20 20 31 37 37 2c 0d 0a 20 2f    177,  177,.. /
3cc02 2a 20 20 31 35 32 30 20 2a 2f 20 20 20 32 30 39  *  1520 */   209
3cc03 2c 20 20 31 39 36 2c 20 20 31 37 37 2c 20 20 31  ,  196,  177,  1
3cc04 39 39 2c 20 20 32 30 39 2c 20 20 31 39 39 2c 20  99,  209,  199, 
3cc05 20 31 36 36 2c 20 20 32 30 38 2c 20 20 20 39 32   166,  208,   92
3cc06 2c 20 20 31 39 35 2c 0d 0a 20 2f 2a 20 20 31 35  ,  195,.. /*  15
3cc07 33 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37  30 */   174,  17
3cc08 34 2c 20 20 31 38 33 2c 20 20 32 35 32 2c 20 20  4,  183,  252,  
3cc09 31 38 33 2c 20 20 31 38 33 2c 20 20 32 35 32 2c  183,  183,  252,
3cc0a 20 20 31 39 31 2c 20 20 32 35 32 2c 20 20 32 33    191,  252,  23
3cc0b 35 2c 0d 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f  5,.. /*  1540 */
3cc0c 20 20 20 31 38 36 2c 20 20 32 34 31 2c 20 20 32     186,  241,  2
3cc0d 34 31 2c 20 20 32 35 32 2c 20 20 31 38 36 2c 20  41,  252,  186, 
3cc0e 20 32 35 32 2c 20 20 32 35 32 2c 20 20 32 35 32   252,  252,  252
3cc0f 2c 20 20 32 35 32 2c 20 20 32 35 32 2c 0d 0a 20  ,  252,  252,.. 
3cc10 2f 2a 20 20 31 35 35 30 20 2a 2f 20 20 20 32 35  /*  1550 */   25
3cc11 32 2c 20 20 32 35 32 2c 20 20 32 35 32 2c 20 20  2,  252,  252,  
3cc12 32 35 32 2c 20 20 32 35 32 2c 20 20 32 35 32 2c  252,  252,  252,
3cc13 20 20 32 33 36 2c 0d 0a 7d 3b 0d 0a 23 64 65 66    236,..};..#def
3cc14 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55 53 45  ine YY_SHIFT_USE
3cc15 5f 44 46 4c 54 20 28 2d 37 34 29 0d 0a 23 64 65  _DFLT (-74)..#de
3cc16 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 43 4f  fine YY_SHIFT_CO
3cc17 55 4e 54 20 28 34 31 38 29 0d 0a 23 64 65 66 69  UNT (418)..#defi
3cc18 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e 20  ne YY_SHIFT_MIN 
3cc19 20 20 28 2d 37 33 29 0d 0a 23 64 65 66 69 6e 65    (-73)..#define
3cc1a 20 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 20 20   YY_SHIFT_MAX   
3cc1b 28 31 34 36 38 29 0d 0a 73 74 61 74 69 63 20 63  (1468)..static c
3cc1c 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68  onst short yy_sh
3cc1d 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0d 0a  ift_ofst[] = {..
3cc1e 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 39   /*     0 */   9
3cc1f 37 35 2c 20 31 31 31 34 2c 20 31 33 34 33 2c 20  75, 1114, 1343, 
3cc20 31 31 31 34 2c 20 31 32 31 33 2c 20 31 32 31 33  1114, 1213, 1213
3cc21 2c 20 20 20 39 30 2c 20 20 20 39 30 2c 20 20 20  ,   90,   90,   
3cc22 20 30 2c 20 20 2d 31 39 2c 0d 0a 20 2f 2a 20 20   0,  -19,.. /*  
3cc23 20 20 31 30 20 2a 2f 20 20 31 32 31 33 2c 20 31    10 */  1213, 1
3cc24 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c  213, 1213, 1213,
3cc25 20 31 32 31 33 2c 20 20 33 34 35 2c 20 20 34 34   1213,  345,  44
3cc26 35 2c 20 20 37 32 31 2c 20 31 30 39 31 2c 20 31  5,  721, 1091, 1
3cc27 32 31 33 2c 0d 0a 20 2f 2a 20 20 20 20 32 30 20  213,.. /*    20 
3cc28 2a 2f 20 20 31 32 31 33 2c 20 31 32 31 33 2c 20  */  1213, 1213, 
3cc29 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33  1213, 1213, 1213
3cc2a 2c 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32  , 1213, 1213, 12
3cc2b 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c 0d  13, 1213, 1213,.
3cc2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 31  . /*    30 */  1
3cc2d 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c  213, 1213, 1213,
3cc2e 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31   1213, 1213, 121
3cc2f 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31  3, 1213, 1213, 1
3cc30 32 31 33 2c 20 31 32 31 33 2c 0d 0a 20 2f 2a 20  213, 1213,.. /* 
3cc31 20 20 20 34 30 20 2a 2f 20 20 31 32 31 33 2c 20     40 */  1213, 
3cc32 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33  1213, 1213, 1213
3cc33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32  , 1213, 1213, 12
3cc34 31 33 2c 20 31 32 33 36 2c 20 31 32 31 33 2c 20  13, 1236, 1213, 
3cc35 31 32 31 33 2c 0d 0a 20 2f 2a 20 20 20 20 35 30  1213,.. /*    50
3cc36 20 2a 2f 20 20 31 32 31 33 2c 20 31 32 31 33 2c   */  1213, 1213,
3cc37 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31   1213, 1213, 121
3cc38 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c 20 31  3, 1213, 1213, 1
3cc39 32 31 33 2c 20 31 32 31 33 2c 20 31 32 31 33 2c  213, 1213, 1213,
3cc3a 0d 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20  .. /*    60 */  
3cc3b 31 32 31 33 2c 20 20 31 39 39 2c 20 20 34 34 35  1213,  199,  445
3cc3c 2c 20 20 34 34 35 2c 20 20 38 33 35 2c 20 20 38  ,  445,  835,  8
3cc3d 33 35 2c 20 20 33 36 35 2c 20 31 31 36 34 2c 20  35,  365, 1164, 
3cc3e 20 20 35 35 2c 20 20 36 34 37 2c 0d 0a 20 2f 2a    55,  647,.. /*
3cc3f 20 20 20 20 37 30 20 2a 2f 20 20 20 35 37 33 2c      70 */   573,
3cc40 20 20 34 39 39 2c 20 20 34 32 35 2c 20 20 33 35    499,  425,  35
3cc41 31 2c 20 20 32 37 37 2c 20 20 32 30 33 2c 20 20  1,  277,  203,  
3cc42 31 32 39 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  129,  795,  795,
3cc43 20 20 37 39 35 2c 0d 0a 20 2f 2a 20 20 20 20 38    795,.. /*    8
3cc44 30 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39 35  0 */   795,  795
3cc45 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
3cc46 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20  95,  795,  795, 
3cc47 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
3cc48 2c 0d 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  ,.. /*    90 */ 
3cc49 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39    795,  795,  79
3cc4a 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  5,  795,  795,  
3cc4b 38 36 39 2c 20 20 37 39 35 2c 20 20 39 34 33 2c  869,  795,  943,
3cc4c 20 31 30 31 37 2c 20 31 30 31 37 2c 0d 0a 20 2f   1017, 1017,.. /
3cc4d 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 2d 36 39  *   100 */   -69
3cc4e 2c 20 20 2d 34 35 2c 20 20 2d 34 35 2c 20 20 2d  ,  -45,  -45,  -
3cc4f 34 35 2c 20 20 2d 34 35 2c 20 20 2d 34 35 2c 20  45,  -45,  -45, 
3cc50 20 20 2d 31 2c 20 20 20 35 38 2c 20 20 31 33 38    -1,   58,  138
3cc51 2c 20 20 31 30 30 2c 0d 0a 20 2f 2a 20 20 20 31  ,  100,.. /*   1
3cc52 31 30 20 2a 2f 20 20 20 34 34 35 2c 20 20 34 34  10 */   445,  44
3cc53 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20  5,  445,  445,  
3cc54 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c  445,  445,  445,
3cc55 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34    445,  445,  44
3cc56 35 2c 0d 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  5,.. /*   120 */
3cc57 20 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34     445,  445,  4
3cc58 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20  45,  445,  445, 
3cc59 20 34 34 35 2c 20 20 35 33 37 2c 20 20 34 33 38   445,  537,  438
3cc5a 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 0d 0a 20  ,  445,  445,.. 
3cc5b 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 34 34  /*   130 */   44
3cc5c 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20  5,  445,  445,  
3cc5d 33 36 35 2c 20 20 38 30 37 2c 20 31 34 33 36 2c  365,  807, 1436,
3cc5e 20 20 2d 37 34 2c 20 20 2d 37 34 2c 20 20 2d 37    -74,  -74,  -7
3cc5f 34 2c 20 31 32 39 33 2c 0d 0a 20 2f 2a 20 20 20  4, 1293,.. /*   
3cc60 31 34 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 34  140 */    73,  4
3cc61 33 34 2c 20 20 34 33 34 2c 20 20 33 31 31 2c 20  34,  434,  311, 
3cc62 20 33 31 34 2c 20 20 32 39 30 2c 20 20 32 38 33   314,  290,  283
3cc63 2c 20 20 32 38 36 2c 20 20 35 34 30 2c 20 20 34  ,  286,  540,  4
3cc64 36 37 2c 0d 0a 20 2f 2a 20 20 20 31 35 30 20 2a  67,.. /*   150 *
3cc65 2f 20 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20  /   445,  445,  
3cc66 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c  445,  445,  445,
3cc67 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34    445,  445,  44
3cc68 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 0d 0a  5,  445,  445,..
3cc69 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 34   /*   160 */   4
3cc6a 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20  45,  445,  445, 
3cc6b 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35   445,  445,  445
3cc6c 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34  ,  445,  445,  4
3cc6d 34 35 2c 20 20 34 34 35 2c 0d 0a 20 2f 2a 20 20  45,  445,.. /*  
3cc6e 20 31 37 30 20 2a 2f 20 20 20 34 34 35 2c 20 20   170 */   445,  
3cc6f 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c  445,  445,  445,
3cc70 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 34    445,  445,  44
3cc71 35 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20 20  5,  445,  445,  
3cc72 34 34 35 2c 0d 0a 20 2f 2a 20 20 20 31 38 30 20  445,.. /*   180 
3cc73 2a 2f 20 20 20 34 34 35 2c 20 20 34 34 35 2c 20  */   445,  445, 
3cc74 20 20 36 35 2c 20 20 37 32 32 2c 20 20 37 32 32    65,  722,  722
3cc75 2c 20 20 37 32 32 2c 20 20 36 38 38 2c 20 20 32  ,  722,  688,  2
3cc76 36 36 2c 20 31 31 36 34 2c 20 31 31 36 34 2c 0d  66, 1164, 1164,.
3cc77 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 31  . /*   190 */  1
3cc78 31 36 34 2c 20 20 2d 37 34 2c 20 20 2d 37 34 2c  164,  -74,  -74,
3cc79 20 20 2d 37 34 2c 20 20 31 33 36 2c 20 20 31 36    -74,  136,  16
3cc7a 38 2c 20 20 31 36 38 2c 20 20 32 33 34 2c 20 20  8,  168,  234,  
3cc7b 33 36 30 2c 20 20 33 36 30 2c 0d 0a 20 2f 2a 20  360,  360,.. /* 
3cc7c 20 20 32 30 30 20 2a 2f 20 20 20 33 36 30 2c 20    200 */   360, 
3cc7d 20 34 33 30 2c 20 20 33 37 32 2c 20 20 34 33 35   430,  372,  435
3cc7e 2c 20 20 33 35 32 2c 20 20 32 37 38 2c 20 20 31  ,  352,  278,  1
3cc7f 32 36 2c 20 20 2d 33 36 2c 20 20 2d 33 36 2c 20  26,  -36,  -36, 
3cc80 20 2d 33 36 2c 0d 0a 20 2f 2a 20 20 20 32 31 30   -36,.. /*   210
3cc81 20 2a 2f 20 20 20 2d 33 36 2c 20 20 34 32 31 2c   */   -36,  421,
3cc82 20 20 36 35 31 2c 20 20 2d 33 36 2c 20 20 2d 33    651,  -36,  -3
3cc83 36 2c 20 20 35 39 32 2c 20 20 32 39 32 2c 20 20  6,  592,  292,  
3cc84 32 31 32 2c 20 20 36 32 33 2c 20 20 31 35 38 2c  212,  623,  158,
3cc85 0d 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20  .. /*   220 */  
3cc86 20 32 30 34 2c 20 20 32 30 34 2c 20 20 35 30 35   204,  204,  505
3cc87 2c 20 20 31 35 38 2c 20 20 35 30 35 2c 20 20 31  ,  158,  505,  1
3cc88 34 34 2c 20 20 33 36 35 2c 20 20 31 35 34 2c 20  44,  365,  154, 
3cc89 20 33 36 35 2c 20 20 31 35 34 2c 0d 0a 20 2f 2a   365,  154,.. /*
3cc8a 20 20 20 32 33 30 20 2a 2f 20 20 20 36 34 35 2c     230 */   645,
3cc8b 20 20 31 35 34 2c 20 20 32 30 34 2c 20 20 31 35    154,  204,  15
3cc8c 34 2c 20 20 31 35 34 2c 20 20 35 33 35 2c 20 20  4,  154,  535,  
3cc8d 35 34 38 2c 20 20 35 34 38 2c 20 20 33 36 35 2c  548,  548,  365,
3cc8e 20 20 33 38 37 2c 0d 0a 20 2f 2a 20 20 20 32 34    387,.. /*   24
3cc8f 30 20 2a 2f 20 20 20 35 30 38 2c 20 20 32 33 33  0 */   508,  233
3cc90 2c 20 31 34 36 34 2c 20 31 32 32 32 2c 20 31 32  , 1464, 1222, 12
3cc91 32 32 2c 20 31 34 35 36 2c 20 31 34 35 36 2c 20  22, 1456, 1456, 
3cc92 31 32 32 32 2c 20 31 34 36 32 2c 20 31 34 31 30  1222, 1462, 1410
3cc93 2c 0d 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  ,.. /*   250 */ 
3cc94 20 31 31 36 35 2c 20 31 34 36 38 2c 20 31 34 36   1165, 1468, 146
3cc95 38 2c 20 31 34 36 38 2c 20 31 34 36 38 2c 20 31  8, 1468, 1468, 1
3cc96 32 32 32 2c 20 31 31 36 35 2c 20 31 34 36 32 2c  222, 1165, 1462,
3cc97 20 31 34 31 30 2c 20 31 34 31 30 2c 0d 0a 20 2f   1410, 1410,.. /
3cc98 2a 20 20 20 32 36 30 20 2a 2f 20 20 31 32 32 32  *   260 */  1222
3cc99 2c 20 31 34 34 38 2c 20 31 33 33 38 2c 20 31 34  , 1448, 1338, 14
3cc9a 32 35 2c 20 31 32 32 32 2c 20 31 32 32 32 2c 20  25, 1222, 1222, 
3cc9b 31 34 34 38 2c 20 31 32 32 32 2c 20 31 34 34 38  1448, 1222, 1448
3cc9c 2c 20 31 32 32 32 2c 0d 0a 20 2f 2a 20 20 20 32  , 1222,.. /*   2
3cc9d 37 30 20 2a 2f 20 20 31 34 34 38 2c 20 31 34 31  70 */  1448, 141
3cc9e 39 2c 20 31 33 31 33 2c 20 31 33 31 33 2c 20 31  9, 1313, 1313, 1
3cc9f 33 31 33 2c 20 31 33 38 37 2c 20 31 33 36 34 2c  313, 1387, 1364,
3cca0 20 31 33 36 34 2c 20 31 34 31 39 2c 20 31 33 31   1364, 1419, 131
3cca1 33 2c 0d 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  3,.. /*   280 */
3cca2 20 20 31 33 33 36 2c 20 31 33 31 33 2c 20 31 33    1336, 1313, 13
3cca3 38 37 2c 20 31 33 31 33 2c 20 31 33 31 33 2c 20  87, 1313, 1313, 
3cca4 31 32 35 34 2c 20 31 32 34 35 2c 20 31 32 35 34  1254, 1245, 1254
3cca5 2c 20 31 32 34 35 2c 20 31 32 35 34 2c 0d 0a 20  , 1245, 1254,.. 
3cca6 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32 34  /*   290 */  124
3cca7 35 2c 20 31 32 32 32 2c 20 31 32 32 32 2c 20 31  5, 1222, 1222, 1
3cca8 31 38 36 2c 20 31 31 38 39 2c 20 31 31 37 35 2c  186, 1189, 1175,
3cca9 20 31 31 36 39 2c 20 31 31 37 31 2c 20 31 31 36   1169, 1171, 116
3ccaa 35 2c 20 31 31 36 34 2c 0d 0a 20 2f 2a 20 20 20  5, 1164,.. /*   
3ccab 33 30 30 20 2a 2f 20 20 31 32 34 33 2c 20 31 32  300 */  1243, 12
3ccac 34 34 2c 20 31 32 34 34 2c 20 31 32 31 32 2c 20  44, 1244, 1212, 
3ccad 31 32 31 32 2c 20 31 32 31 32 2c 20 31 32 31 32  1212, 1212, 1212
3ccae 2c 20 20 2d 37 34 2c 20 20 2d 37 34 2c 20 20 2d  ,  -74,  -74,  -
3ccaf 37 34 2c 0d 0a 20 2f 2a 20 20 20 33 31 30 20 2a  74,.. /*   310 *
3ccb0 2f 20 20 20 2d 37 34 2c 20 20 2d 37 34 2c 20 20  /   -74,  -74,  
3ccb1 2d 37 34 2c 20 20 39 33 39 2c 20 20 31 30 34 2c  -74,  939,  104,
3ccb2 20 20 36 38 30 2c 20 20 35 37 31 2c 20 20 33 32    680,  571,  32
3ccb3 37 2c 20 20 20 20 31 2c 20 20 39 38 30 2c 0d 0a  7,    1,  980,..
3ccb4 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 20   /*   320 */    
3ccb5 32 36 2c 20 20 39 37 32 2c 20 20 39 37 31 2c 20  26,  972,  971, 
3ccb6 20 39 34 36 2c 20 20 39 30 31 2c 20 20 38 37 30   946,  901,  870
3ccb7 2c 20 20 38 33 30 2c 20 20 38 30 36 2c 20 20 20  ,  830,  806,   
3ccb8 35 34 2c 20 20 20 32 31 2c 0d 0a 20 2f 2a 20 20  54,   21,.. /*  
3ccb9 20 33 33 30 20 2a 2f 20 20 20 2d 37 33 2c 20 20   330 */   -73,  
3ccba 35 31 30 2c 20 20 32 34 32 2c 20 31 31 39 38 2c  510,  242, 1198,
3ccbb 20 31 31 39 30 2c 20 31 31 37 30 2c 20 31 30 34   1190, 1170, 104
3ccbc 32 2c 20 31 31 36 31 2c 20 31 31 30 38 2c 20 31  2, 1161, 1108, 1
3ccbd 31 34 36 2c 0d 0a 20 2f 2a 20 20 20 33 34 30 20  146,.. /*   340 
3ccbe 2a 2f 20 20 31 31 34 31 2c 20 31 31 33 32 2c 20  */  1141, 1132, 
3ccbf 31 30 31 35 2c 20 31 31 32 37 2c 20 31 30 32 36  1015, 1127, 1026
3ccc0 2c 20 31 30 33 34 2c 20 31 30 32 30 2c 20 31 31  , 1034, 1020, 11
3ccc1 30 37 2c 20 31 30 30 34 2c 20 31 31 31 36 2c 0d  07, 1004, 1116,.
3ccc2 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 31  . /*   350 */  1
3ccc3 31 32 31 2c 20 31 30 30 35 2c 20 31 30 39 39 2c  121, 1005, 1099,
3ccc4 20 20 39 35 31 2c 20 31 30 34 33 2c 20 31 30 30    951, 1043, 100
3ccc5 33 2c 20 20 39 36 39 2c 20 31 30 34 35 2c 20 31  3,  969, 1045, 1
3ccc6 30 33 35 2c 20 20 39 35 30 2c 0d 0a 20 2f 2a 20  035,  950,.. /* 
3ccc7 20 20 33 36 30 20 2a 2f 20 20 31 30 35 33 2c 20    360 */  1053, 
3ccc8 31 30 34 37 2c 20 31 30 32 35 2c 20 20 39 34 32  1047, 1025,  942
3ccc9 2c 20 20 39 31 33 2c 20 20 39 39 32 2c 20 31 30  ,  913,  992, 10
3ccca 31 39 2c 20 20 39 34 35 2c 20 20 39 38 34 2c 20  19,  945,  984, 
3cccb 20 39 34 30 2c 0d 0a 20 2f 2a 20 20 20 33 37 30   940,.. /*   370
3cccc 20 2a 2f 20 20 20 38 37 36 2c 20 20 39 30 34 2c   */   876,  904,
3cccd 20 20 39 35 33 2c 20 20 38 39 36 2c 20 20 37 34    953,  896,  74
3ccce 38 2c 20 20 38 30 34 2c 20 20 38 38 30 2c 20 20  8,  804,  880,  
3cccf 37 38 36 2c 20 20 38 36 38 2c 20 20 38 31 39 2c  786,  868,  819,
3ccd0 0d 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20  .. /*   380 */  
3ccd1 20 38 30 35 2c 20 20 38 31 30 2c 20 20 37 37 33   805,  810,  773
3ccd2 2c 20 20 37 35 31 2c 20 20 37 36 36 2c 20 20 37  ,  751,  766,  7
3ccd3 30 36 2c 20 20 37 31 36 2c 20 20 36 39 31 2c 20  06,  716,  691, 
3ccd4 20 36 38 31 2c 20 20 35 36 38 2c 0d 0a 20 2f 2a   681,  568,.. /*
3ccd5 20 20 20 33 39 30 20 2a 2f 20 20 20 36 35 35 2c     390 */   655,
3ccd6 20 20 36 33 38 2c 20 20 36 37 36 2c 20 20 35 31    638,  676,  51
3ccd7 36 2c 20 20 35 34 31 2c 20 20 35 39 34 2c 20 20  6,  541,  594,  
3ccd8 35 39 39 2c 20 20 35 36 37 2c 20 20 35 34 31 2c  599,  567,  541,
3ccd9 20 20 35 33 34 2c 0d 0a 20 2f 2a 20 20 20 34 30    534,.. /*   40
3ccda 30 20 2a 2f 20 20 20 35 30 37 2c 20 20 35 32 37  0 */   507,  527
3ccdb 2c 20 20 34 39 38 2c 20 20 35 32 33 2c 20 20 34  ,  498,  523,  4
3ccdc 36 36 2c 20 20 33 38 32 2c 20 20 34 30 39 2c 20  66,  382,  409, 
3ccdd 20 33 38 34 2c 20 20 33 35 37 2c 20 20 20 20 36   384,  357,    6
3ccde 2c 0d 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20  ,.. /*   410 */ 
3ccdf 20 20 32 34 30 2c 20 20 32 32 34 2c 20 20 31 34    240,  224,  14
3cce0 33 2c 20 20 20 36 32 2c 20 20 20 31 38 2c 20 20  3,   62,   18,  
3cce1 20 37 31 2c 20 20 20 33 39 2c 20 20 20 20 39 2c   71,   39,    9,
3cce2 20 20 20 20 35 2c 0d 0a 7d 3b 0d 0a 23 64 65 66      5,..};..#def
3cce3 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 55 53  ine YY_REDUCE_US
3cce4 45 5f 44 46 4c 54 20 28 2d 31 34 32 29 0d 0a 23  E_DFLT (-142)..#
3cce5 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45  define YY_REDUCE
3cce6 5f 43 4f 55 4e 54 20 28 33 31 32 29 0d 0a 23 64  _COUNT (312)..#d
3cce7 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f  efine YY_REDUCE_
3cce8 4d 49 4e 20 20 20 28 2d 31 34 31 29 0d 0a 23 64  MIN   (-141)..#d
3cce9 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f  efine YY_REDUCE_
3ccea 4d 41 58 20 20 20 28 31 33 36 39 29 0d 0a 73 74  MAX   (1369)..st
3cceb 61 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74  atic const short
3ccec 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b   yy_reduce_ofst[
3cced 5d 20 3d 20 7b 0d 0a 20 2f 2a 20 20 20 20 20 30  ] = {.. /*     0
3ccee 20 2a 2f 20 20 2d 31 34 31 2c 20 20 39 39 34 2c   */  -141,  994,
3ccef 20 31 31 31 38 2c 20 20 32 32 33 2c 20 20 31 35   1118,  223,  15
3ccf0 37 2c 20 20 2d 35 33 2c 20 20 20 39 33 2c 20 20  7,  -53,   93,  
3ccf1 20 38 39 2c 20 20 20 38 33 2c 20 20 33 37 35 2c   89,   83,  375,
3ccf2 0d 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20  .. /*    10 */  
3ccf3 20 33 38 36 2c 20 20 33 38 31 2c 20 20 33 37 39   386,  381,  379
3ccf4 2c 20 20 33 30 38 2c 20 20 32 39 35 2c 20 20 33  ,  308,  295,  3
3ccf5 32 35 2c 20 20 2d 34 37 2c 20 20 20 32 37 2c 20  25,  -47,   27, 
3ccf6 31 32 34 30 2c 20 31 32 33 34 2c 0d 0a 20 2f 2a  1240, 1234,.. /*
3ccf7 20 20 20 20 32 30 20 2a 2f 20 20 31 32 32 38 2c      20 */  1228,
3ccf8 20 31 32 32 31 2c 20 31 32 30 38 2c 20 31 31 38   1221, 1208, 118
3ccf9 37 2c 20 31 31 35 31 2c 20 31 31 31 31 2c 20 31  7, 1151, 1111, 1
3ccfa 31 30 39 2c 20 31 30 37 37 2c 20 31 30 35 34 2c  109, 1077, 1054,
3ccfb 20 31 30 32 32 2c 0d 0a 20 2f 2a 20 20 20 20 33   1022,.. /*    3
3ccfc 30 20 2a 2f 20 20 31 30 31 36 2c 20 31 30 30 30  0 */  1016, 1000
3ccfd 2c 20 20 39 31 31 2c 20 20 39 30 38 2c 20 20 39  ,  911,  908,  9
3ccfe 30 36 2c 20 20 38 39 30 2c 20 20 38 38 38 2c 20  06,  890,  888, 
3ccff 20 38 37 34 2c 20 20 38 33 34 2c 20 20 38 31 36   874,  834,  816
3cd00 2c 0d 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  ,.. /*    40 */ 
3cd01 20 20 38 30 30 2c 20 20 37 36 30 2c 20 20 37 35    800,  760,  75
3cd02 38 2c 20 20 37 35 35 2c 20 20 37 34 32 2c 20 20  8,  755,  742,  
3cd03 37 33 39 2c 20 20 37 32 36 2c 20 20 36 38 35 2c  739,  726,  685,
3cd04 20 20 36 37 32 2c 20 20 36 36 38 2c 0d 0a 20 2f    672,  668,.. /
3cd05 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 36 36 35  *    50 */   665
3cd06 2c 20 20 36 35 32 2c 20 20 36 31 31 2c 20 20 36  ,  652,  611,  6
3cd07 30 39 2c 20 20 36 30 37 2c 20 20 36 30 34 2c 20  09,  607,  604, 
3cd08 20 35 39 31 2c 20 20 35 37 38 2c 20 20 35 32 36   591,  578,  526
3cd09 2c 20 20 35 31 39 2c 0d 0a 20 2f 2a 20 20 20 20  ,  519,.. /*    
3cd0a 36 30 20 2a 2f 20 20 20 34 35 33 2c 20 20 34 37  60 */   453,  47
3cd0b 34 2c 20 20 34 35 34 2c 20 20 34 36 31 2c 20 20  4,  454,  461,  
3cd0c 34 34 33 2c 20 20 32 34 35 2c 20 20 34 34 32 2c  443,  245,  442,
3cd0d 20 20 34 37 33 2c 20 20 34 38 34 2c 20 20 34 38    473,  484,  48
3cd0e 34 2c 0d 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  4,.. /*    70 */
3cd0f 20 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34     484,  484,  4
3cd10 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 20  84,  484,  484, 
3cd11 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34   484,  484,  484
3cd12 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 0d 0a 20  ,  484,  484,.. 
3cd13 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 34 38  /*    80 */   48
3cd14 34 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20  4,  484,  484,  
3cd15 34 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34 2c  484,  484,  484,
3cd16 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34 38    484,  484,  48
3cd17 34 2c 20 20 34 38 34 2c 0d 0a 20 2f 2a 20 20 20  4,  484,.. /*   
3cd18 20 39 30 20 2a 2f 20 20 20 34 38 34 2c 20 20 34   90 */   484,  4
3cd19 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 20  84,  484,  484, 
3cd1a 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34   484,  484,  484
3cd1b 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34  ,  484,  484,  4
3cd1c 38 34 2c 0d 0a 20 2f 2a 20 20 20 31 30 30 20 2a  84,.. /*   100 *
3cd1d 2f 20 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20  /   484,  484,  
3cd1e 34 38 34 2c 20 20 34 38 34 2c 20 20 34 38 34 2c  484,  484,  484,
3cd1f 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20 31 33    484,  484,  13
3cd20 30 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 0d 0a  0,  484,  484,..
3cd21 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 31 31   /*   110 */  11
3cd22 34 35 2c 20 20 39 30 39 2c 20 31 31 31 30 2c 20  45,  909, 1110, 
3cd23 31 30 38 38 2c 20 31 30 38 34 2c 20 31 30 33 33  1088, 1084, 1033
3cd24 2c 20 31 30 30 32 2c 20 20 39 36 35 2c 20 20 38  , 1002,  965,  8
3cd25 32 30 2c 20 20 38 33 37 2c 0d 0a 20 2f 2a 20 20  20,  837,.. /*  
3cd26 20 31 32 30 20 2a 2f 20 20 20 37 34 36 2c 20 20   120 */   746,  
3cd27 36 38 36 2c 20 20 36 31 32 2c 20 20 38 31 37 2c  686,  612,  817,
3cd28 20 20 36 31 30 2c 20 20 39 31 39 2c 20 20 32 32    610,  919,  22
3cd29 31 2c 20 20 35 36 33 2c 20 20 38 31 34 2c 20 20  1,  563,  814,  
3cd2a 38 31 33 2c 0d 0a 20 2f 2a 20 20 20 31 33 30 20  813,.. /*   130 
3cd2b 2a 2f 20 20 20 37 34 34 2c 20 20 36 36 39 2c 20  */   744,  669, 
3cd2c 20 34 37 30 2c 20 20 35 34 33 2c 20 20 34 38 34   470,  543,  484
3cd2d 2c 20 20 34 38 34 2c 20 20 34 38 34 2c 20 20 34  ,  484,  484,  4
3cd2e 38 34 2c 20 20 34 38 34 2c 20 20 32 39 31 2c 0d  84,  484,  291,.
3cd2f 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
3cd30 35 36 39 2c 20 20 36 37 31 2c 20 20 36 35 38 2c  569,  671,  658,
3cd31 20 20 39 37 30 2c 20 31 32 39 30 2c 20 31 32 38    970, 1290, 128
3cd32 37 2c 20 31 32 38 36 2c 20 31 32 38 32 2c 20 20  7, 1286, 1282,  
3cd33 35 31 38 2c 20 20 35 31 38 2c 0d 0a 20 2f 2a 20  518,  518,.. /* 
3cd34 20 20 31 35 30 20 2a 2f 20 20 31 32 38 30 2c 20    150 */  1280, 
3cd35 31 32 37 39 2c 20 31 32 37 37 2c 20 31 32 37 30  1279, 1277, 1270
3cd36 2c 20 31 32 36 38 2c 20 31 32 36 33 2c 20 31 32  , 1268, 1263, 12
3cd37 36 31 2c 20 31 32 36 30 2c 20 31 32 35 36 2c 20  61, 1260, 1256, 
3cd38 31 32 35 31 2c 0d 0a 20 2f 2a 20 20 20 31 36 30  1251,.. /*   160
3cd39 20 2a 2f 20 20 31 32 34 37 2c 20 31 32 32 37 2c   */  1247, 1227,
3cd3a 20 31 31 38 35 2c 20 31 31 36 38 2c 20 31 31 36   1185, 1168, 116
3cd3b 37 2c 20 31 31 35 39 2c 20 31 31 34 38 2c 20 31  7, 1159, 1148, 1
3cd3c 31 33 39 2c 20 31 31 31 37 2c 20 31 30 36 36 2c  139, 1117, 1066,
3cd3d 0d 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20  .. /*   170 */  
3cd3e 31 30 34 39 2c 20 31 30 30 36 2c 20 20 39 39 38  1049, 1006,  998
3cd3f 2c 20 20 39 39 36 2c 20 20 39 39 35 2c 20 20 39  ,  996,  995,  9
3cd40 37 33 2c 20 20 39 37 30 2c 20 20 39 36 36 2c 20  73,  970,  966, 
3cd41 20 39 36 34 2c 20 20 38 39 32 2c 0d 0a 20 2f 2a   964,  892,.. /*
3cd42 20 20 20 31 38 30 20 2a 2f 20 20 20 37 36 32 2c     180 */   762,
3cd43 20 20 2d 35 32 2c 20 20 38 38 31 2c 20 20 39 33    -52,  881,  93
3cd44 32 2c 20 20 38 30 32 2c 20 20 37 33 31 2c 20 20  2,  802,  731,  
3cd45 36 31 39 2c 20 20 38 31 32 2c 20 20 36 36 34 2c  619,  812,  664,
3cd46 20 20 36 36 30 2c 0d 0a 20 2f 2a 20 20 20 31 39    660,.. /*   19
3cd47 30 20 2a 2f 20 20 20 36 32 37 2c 20 20 33 39 32  0 */   627,  392
3cd48 2c 20 20 33 33 31 2c 20 20 31 32 34 2c 20 31 33  ,  331,  124, 13
3cd49 35 38 2c 20 31 33 35 37 2c 20 31 33 35 36 2c 20  58, 1357, 1356, 
3cd4a 31 33 35 34 2c 20 31 33 35 32 2c 20 31 33 35 31  1354, 1352, 1351
3cd4b 2c 0d 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  ,.. /*   200 */ 
3cd4c 20 31 33 34 39 2c 20 31 33 31 39 2c 20 31 33 33   1349, 1319, 133
3cd4d 34 2c 20 31 33 34 36 2c 20 31 33 33 34 2c 20 31  4, 1346, 1334, 1
3cd4e 33 33 34 2c 20 31 33 33 34 2c 20 31 33 33 34 2c  334, 1334, 1334,
3cd4f 20 31 33 33 34 2c 20 31 33 33 34 2c 0d 0a 20 2f   1334, 1334,.. /
3cd50 2a 20 20 20 32 31 30 20 2a 2f 20 20 31 33 33 34  *   210 */  1334
3cd51 2c 20 31 33 32 30 2c 20 31 33 30 34 2c 20 31 33  , 1320, 1304, 13
3cd52 33 34 2c 20 31 33 33 34 2c 20 31 33 31 39 2c 20  34, 1334, 1319, 
3cd53 31 33 36 30 2c 20 31 33 32 35 2c 20 31 33 36 39  1360, 1325, 1369
3cd54 2c 20 31 33 32 36 2c 0d 0a 20 2f 2a 20 20 20 32  , 1326,.. /*   2
3cd55 32 30 20 2a 2f 20 20 31 33 31 35 2c 20 31 33 31  20 */  1315, 131
3cd56 31 2c 20 31 33 30 31 2c 20 31 33 32 34 2c 20 31  1, 1301, 1324, 1
3cd57 33 30 30 2c 20 31 33 33 35 2c 20 31 33 35 30 2c  300, 1335, 1350,
3cd58 20 31 33 34 35 2c 20 31 33 34 38 2c 20 31 33 34   1345, 1348, 134
3cd59 32 2c 0d 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  2,.. /*   230 */
3cd5a 20 20 31 33 33 33 2c 20 31 33 34 31 2c 20 31 33    1333, 1341, 13
3cd5b 30 33 2c 20 31 33 33 32 2c 20 31 33 33 31 2c 20  03, 1332, 1331, 
3cd5c 31 32 38 34 2c 20 31 32 37 38 2c 20 31 32 37 34  1284, 1278, 1274
3cd5d 2c 20 31 33 33 39 2c 20 31 33 30 39 2c 0d 0a 20  , 1339, 1309,.. 
3cd5e 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 33 30  /*   240 */  130
3cd5f 38 2c 20 31 33 34 37 2c 20 31 32 35 38 2c 20 31  8, 1347, 1258, 1
3cd60 33 34 34 2c 20 31 33 34 30 2c 20 31 32 35 37 2c  344, 1340, 1257,
3cd61 20 31 32 35 33 2c 20 31 33 33 37 2c 20 31 32 37   1253, 1337, 127
3cd62 33 2c 20 31 33 30 32 2c 0d 0a 20 2f 2a 20 20 20  3, 1302,.. /*   
3cd63 32 35 30 20 2a 2f 20 20 31 32 39 39 2c 20 31 32  250 */  1299, 12
3cd64 39 38 2c 20 31 32 39 37 2c 20 31 32 39 36 2c 20  98, 1297, 1296, 
3cd65 31 32 39 35 2c 20 31 33 32 38 2c 20 31 32 39 34  1295, 1328, 1294
3cd66 2c 20 31 32 36 34 2c 20 31 32 39 32 2c 20 31 32  , 1264, 1292, 12
3cd67 39 31 2c 0d 0a 20 2f 2a 20 20 20 32 36 30 20 2a  91,.. /*   260 *
3cd68 2f 20 20 31 33 32 32 2c 20 31 33 32 31 2c 20 31  /  1322, 1321, 1
3cd69 32 33 38 2c 20 31 32 33 32 2c 20 31 33 31 38 2c  238, 1232, 1318,
3cd6a 20 31 33 31 37 2c 20 31 33 31 36 2c 20 31 33 31   1317, 1316, 131
3cd6b 34 2c 20 31 33 31 32 2c 20 31 33 31 30 2c 0d 0a  4, 1312, 1310,..
3cd6c 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 33   /*   270 */  13
3cd6d 30 37 2c 20 31 32 38 33 2c 20 31 32 38 39 2c 20  07, 1283, 1289, 
3cd6e 31 32 38 38 2c 20 31 32 38 35 2c 20 31 32 37 36  1288, 1285, 1276
3cd6f 2c 20 31 32 32 39 2c 20 31 32 32 34 2c 20 31 32  , 1229, 1224, 12
3cd70 36 37 2c 20 31 32 38 31 2c 0d 0a 20 2f 2a 20 20  67, 1281,.. /*  
3cd71 20 32 38 30 20 2a 2f 20 20 31 32 36 35 2c 20 31   280 */  1265, 1
3cd72 32 36 32 2c 20 31 32 33 35 2c 20 31 32 35 35 2c  262, 1235, 1255,
3cd73 20 31 32 30 35 2c 20 31 31 38 33 2c 20 31 31 37   1205, 1183, 117
3cd74 39 2c 20 31 31 37 37 2c 20 31 31 36 32 2c 20 31  9, 1177, 1162, 1
3cd75 31 34 30 2c 0d 0a 20 2f 2a 20 20 20 32 39 30 20  140,.. /*   290 
3cd76 2a 2f 20 20 31 31 35 33 2c 20 31 31 38 34 2c 20  */  1153, 1184, 
3cd77 31 31 38 32 2c 20 31 31 30 32 2c 20 31 31 32 34  1182, 1102, 1124
3cd78 2c 20 31 31 30 33 2c 20 31 30 39 35 2c 20 31 30  , 1103, 1095, 10
3cd79 39 30 2c 20 31 30 38 39 2c 20 31 30 39 33 2c 0d  90, 1089, 1093,.
3cd7a 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 31  . /*   300 */  1
3cd7b 31 31 32 2c 20 31 31 31 35 2c 20 31 30 38 36 2c  112, 1115, 1086,
3cd7c 20 31 31 30 35 2c 20 31 30 39 32 2c 20 31 30 38   1105, 1092, 108
3cd7d 37 2c 20 31 30 36 38 2c 20 20 39 36 32 2c 20 20  7, 1068,  962,  
3cd7e 39 35 35 2c 20 20 39 35 37 2c 0d 0a 20 2f 2a 20  955,  957,.. /* 
3cd7f 20 20 33 31 30 20 2a 2f 20 20 31 30 33 31 2c 20    310 */  1031, 
3cd80 31 30 32 33 2c 20 31 30 33 30 2c 0d 0a 7d 3b 0d  1023, 1030,..};.
3cd81 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
3cd82 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 64 65  ACTIONTYPE yy_de
3cd83 66 61 75 6c 74 5b 5d 20 3d 20 7b 0d 0a 20 2f 2a  fault[] = {.. /*
3cd84 20 20 20 20 20 30 20 2a 2f 20 20 20 36 33 35 2c       0 */   635,
3cd85 20 20 38 37 30 2c 20 20 39 35 39 2c 20 20 39 35    870,  959,  95
3cd86 39 2c 20 20 39 35 39 2c 20 20 38 37 30 2c 20 20  9,  959,  870,  
3cd87 38 39 39 2c 20 20 38 39 39 2c 20 20 39 35 39 2c  899,  899,  959,
3cd88 20 20 37 35 39 2c 0d 0a 20 2f 2a 20 20 20 20 31    759,.. /*    1
3cd89 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35 39  0 */   959,  959
3cd8a 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 38  ,  959,  959,  8
3cd8b 36 38 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  68,  959,  959, 
3cd8c 20 39 33 33 2c 20 20 39 35 39 2c 20 20 39 35 39   933,  959,  959
3cd8d 2c 0d 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  ,.. /*    20 */ 
3cd8e 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3cd8f 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cd90 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cd91 20 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f    959,  959,.. /
3cd92 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 39 35 39  *    30 */   959
3cd93 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39  ,  959,  959,  9
3cd94 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3cd95 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cd96 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 20  ,  959,.. /*    
3cd97 34 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35  40 */   959,  95
3cd98 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cd99 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cd9a 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3cd9b 39 2c 0d 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  9,.. /*    50 */
3cd9c 20 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39     959,  959,  9
3cd9d 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3cd9e 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cd9f 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20  ,  959,  959,.. 
3cda0 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39 35  /*    60 */   95
3cda1 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cda2 39 35 39 2c 20 20 38 39 39 2c 20 20 38 39 39 2c  959,  899,  899,
3cda3 20 20 36 37 34 2c 20 20 37 36 33 2c 20 20 37 39    674,  763,  79
3cda4 34 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20  4,  959,.. /*   
3cda5 20 37 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39   70 */   959,  9
3cda6 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3cda7 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cda8 2c 20 20 39 33 32 2c 20 20 39 33 34 2c 20 20 38  ,  932,  934,  8
3cda9 30 39 2c 0d 0a 20 2f 2a 20 20 20 20 38 30 20 2a  09,.. /*    80 *
3cdaa 2f 20 20 20 38 30 38 2c 20 20 38 30 32 2c 20 20  /   808,  802,  
3cdab 38 30 31 2c 20 20 39 31 32 2c 20 20 37 37 34 2c  801,  912,  774,
3cdac 20 20 37 39 39 2c 20 20 37 39 32 2c 20 20 37 38    799,  792,  78
3cdad 35 2c 20 20 37 39 36 2c 20 20 38 37 31 2c 0d 0a  5,  796,  871,..
3cdae 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 38   /*    90 */   8
3cdaf 36 34 2c 20 20 38 36 35 2c 20 20 38 36 33 2c 20  64,  865,  863, 
3cdb0 20 38 36 37 2c 20 20 38 37 32 2c 20 20 39 35 39   867,  872,  959
3cdb1 2c 20 20 37 39 35 2c 20 20 38 33 31 2c 20 20 38  ,  795,  831,  8
3cdb2 34 38 2c 20 20 38 33 30 2c 0d 0a 20 2f 2a 20 20  48,  830,.. /*  
3cdb3 20 31 30 30 20 2a 2f 20 20 20 38 34 32 2c 20 20   100 */   842,  
3cdb4 38 34 37 2c 20 20 38 35 34 2c 20 20 38 34 36 2c  847,  854,  846,
3cdb5 20 20 38 34 33 2c 20 20 38 33 33 2c 20 20 38 33    843,  833,  83
3cdb6 32 2c 20 20 36 36 36 2c 20 20 38 33 34 2c 20 20  2,  666,  834,  
3cdb7 38 33 35 2c 0d 0a 20 2f 2a 20 20 20 31 31 30 20  835,.. /*   110 
3cdb8 2a 2f 20 20 20 39 35 39 2c 20 20 39 35 39 2c 20  */   959,  959, 
3cdb9 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cdba 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39  ,  959,  959,  9
3cdbb 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 0d  59,  959,  959,.
3cdbc 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
3cdbd 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cdbe 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3cdbf 39 2c 20 20 36 36 31 2c 20 20 37 32 38 2c 20 20  9,  661,  728,  
3cdc0 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a 20  959,  959,.. /* 
3cdc1 20 20 31 33 30 20 2a 2f 20 20 20 39 35 39 2c 20    130 */   959, 
3cdc2 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cdc3 2c 20 20 38 33 36 2c 20 20 38 33 37 2c 20 20 38  ,  836,  837,  8
3cdc4 35 31 2c 20 20 38 35 30 2c 20 20 38 34 39 2c 20  51,  850,  849, 
3cdc5 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 31 34 30   959,.. /*   140
3cdc6 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35 39 2c   */   959,  959,
3cdc7 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3cdc8 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cdc9 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cdca 0d 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  .. /*   150 */  
3cdcb 20 39 35 39 2c 20 20 39 33 39 2c 20 20 39 33 37   959,  939,  937
3cdcc 2c 20 20 39 35 39 2c 20 20 38 38 33 2c 20 20 39  ,  959,  883,  9
3cdcd 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3cdce 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a   959,  959,.. /*
3cdcf 20 20 20 31 36 30 20 2a 2f 20 20 20 39 35 39 2c     160 */   959,
3cdd0 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3cdd1 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cdd2 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cdd3 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 31 37    959,.. /*   17
3cdd4 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35 39  0 */   959,  959
3cdd5 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39  ,  959,  959,  9
3cdd6 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3cdd7 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cdd8 2c 0d 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  ,.. /*   180 */ 
3cdd9 20 20 39 35 39 2c 20 20 36 34 31 2c 20 20 39 35    959,  641,  95
3cdda 39 2c 20 20 37 35 39 2c 20 20 37 35 39 2c 20 20  9,  759,  759,  
3cddb 37 35 39 2c 20 20 36 33 35 2c 20 20 39 35 39 2c  759,  635,  959,
3cddc 20 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f    959,  959,.. /
3cddd 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 39 35 39  *   190 */   959
3cdde 2c 20 20 39 35 31 2c 20 20 37 36 33 2c 20 20 37  ,  951,  763,  7
3cddf 35 33 2c 20 20 37 31 39 2c 20 20 39 35 39 2c 20  53,  719,  959, 
3cde0 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3cde1 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 32  ,  959,.. /*   2
3cde2 30 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35  00 */   959,  95
3cde3 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3cde4 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3cde5 20 20 38 30 34 2c 20 20 37 34 32 2c 20 20 39 32    804,  742,  92
3cde6 32 2c 0d 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  2,.. /*   210 */
3cde7 20 20 20 39 32 34 2c 20 20 39 35 39 2c 20 20 39     924,  959,  9
3cde8 30 35 2c 20 20 37 34 30 2c 20 20 36 36 33 2c 20  05,  740,  663, 
3cde9 20 37 36 31 2c 20 20 36 37 36 2c 20 20 37 35 31   761,  676,  751
3cdea 2c 20 20 36 34 33 2c 20 20 37 39 38 2c 0d 0a 20  ,  643,  798,.. 
3cdeb 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 37 37  /*   220 */   77
3cdec 36 2c 20 20 37 37 36 2c 20 20 39 31 37 2c 20 20  6,  776,  917,  
3cded 37 39 38 2c 20 20 39 31 37 2c 20 20 37 30 30 2c  798,  917,  700,
3cdee 20 20 39 35 39 2c 20 20 37 38 38 2c 20 20 39 35    959,  788,  95
3cdef 39 2c 20 20 37 38 38 2c 0d 0a 20 2f 2a 20 20 20  9,  788,.. /*   
3cdf0 32 33 30 20 2a 2f 20 20 20 36 39 37 2c 20 20 37  230 */   697,  7
3cdf1 38 38 2c 20 20 37 37 36 2c 20 20 37 38 38 2c 20  88,  776,  788, 
3cdf2 20 37 38 38 2c 20 20 38 36 36 2c 20 20 39 35 39   788,  866,  959
3cdf3 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 37  ,  959,  959,  7
3cdf4 36 30 2c 0d 0a 20 2f 2a 20 20 20 32 34 30 20 2a  60,.. /*   240 *
3cdf5 2f 20 20 20 37 35 31 2c 20 20 39 35 39 2c 20 20  /   751,  959,  
3cdf6 39 34 34 2c 20 20 37 36 37 2c 20 20 37 36 37 2c  944,  767,  767,
3cdf7 20 20 39 33 36 2c 20 20 39 33 36 2c 20 20 37 36    936,  936,  76
3cdf8 37 2c 20 20 38 31 30 2c 20 20 37 33 32 2c 0d 0a  7,  810,  732,..
3cdf9 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 37   /*   250 */   7
3cdfa 39 38 2c 20 20 37 33 39 2c 20 20 37 33 39 2c 20  98,  739,  739, 
3cdfb 20 37 33 39 2c 20 20 37 33 39 2c 20 20 37 36 37   739,  739,  767
3cdfc 2c 20 20 37 39 38 2c 20 20 38 31 30 2c 20 20 37  ,  798,  810,  7
3cdfd 33 32 2c 20 20 37 33 32 2c 0d 0a 20 2f 2a 20 20  32,  732,.. /*  
3cdfe 20 32 36 30 20 2a 2f 20 20 20 37 36 37 2c 20 20   260 */   767,  
3cdff 36 35 38 2c 20 20 39 31 31 2c 20 20 39 30 39 2c  658,  911,  909,
3ce00 20 20 37 36 37 2c 20 20 37 36 37 2c 20 20 36 35    767,  767,  65
3ce01 38 2c 20 20 37 36 37 2c 20 20 36 35 38 2c 20 20  8,  767,  658,  
3ce02 37 36 37 2c 0d 0a 20 2f 2a 20 20 20 32 37 30 20  767,.. /*   270 
3ce03 2a 2f 20 20 20 36 35 38 2c 20 20 38 37 36 2c 20  */   658,  876, 
3ce04 20 37 33 30 2c 20 20 37 33 30 2c 20 20 37 33 30   730,  730,  730
3ce05 2c 20 20 37 31 35 2c 20 20 38 38 30 2c 20 20 38  ,  715,  880,  8
3ce06 38 30 2c 20 20 38 37 36 2c 20 20 37 33 30 2c 0d  80,  876,  730,.
3ce07 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20  . /*   280 */   
3ce08 37 30 30 2c 20 20 37 33 30 2c 20 20 37 31 35 2c  700,  730,  715,
3ce09 20 20 37 33 30 2c 20 20 37 33 30 2c 20 20 37 38    730,  730,  78
3ce0a 30 2c 20 20 37 37 35 2c 20 20 37 38 30 2c 20 20  0,  775,  780,  
3ce0b 37 37 35 2c 20 20 37 38 30 2c 0d 0a 20 2f 2a 20  775,  780,.. /* 
3ce0c 20 20 32 39 30 20 2a 2f 20 20 20 37 37 35 2c 20    290 */   775, 
3ce0d 20 37 36 37 2c 20 20 37 36 37 2c 20 20 39 35 39   767,  767,  959
3ce0e 2c 20 20 37 39 33 2c 20 20 37 38 31 2c 20 20 37  ,  793,  781,  7
3ce0f 39 31 2c 20 20 37 38 39 2c 20 20 37 39 38 2c 20  91,  789,  798, 
3ce10 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 33 30 30   959,.. /*   300
3ce11 20 2a 2f 20 20 20 37 31 38 2c 20 20 36 35 31 2c   */   718,  651,
3ce12 20 20 36 35 31 2c 20 20 36 34 30 2c 20 20 36 34    651,  640,  64
3ce13 30 2c 20 20 36 34 30 2c 20 20 36 34 30 2c 20 20  0,  640,  640,  
3ce14 39 35 36 2c 20 20 39 35 36 2c 20 20 39 35 31 2c  956,  956,  951,
3ce15 0d 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  .. /*   310 */  
3ce16 20 37 30 32 2c 20 20 37 30 32 2c 20 20 36 38 34   702,  702,  684
3ce17 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39  ,  959,  959,  9
3ce18 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3ce19 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a   959,  959,.. /*
3ce1a 20 20 20 33 32 30 20 2a 2f 20 20 20 38 38 35 2c     320 */   885,
3ce1b 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3ce1c 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3ce1d 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3ce1e 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20 33 33    959,.. /*   33
3ce1f 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35 39  0 */   959,  959
3ce20 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 36  ,  959,  959,  6
3ce21 33 36 2c 20 20 39 34 36 2c 20 20 39 35 39 2c 20  36,  946,  959, 
3ce22 20 39 35 39 2c 20 20 39 34 33 2c 20 20 39 35 39   959,  943,  959
3ce23 2c 0d 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20  ,.. /*   340 */ 
3ce24 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3ce25 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3ce26 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3ce27 20 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20 2f    959,  959,.. /
3ce28 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 39 35 39  *   350 */   959
3ce29 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39  ,  959,  959,  9
3ce2a 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3ce2b 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3ce2c 2c 20 20 39 31 35 2c 0d 0a 20 2f 2a 20 20 20 33  ,  915,.. /*   3
3ce2d 36 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39 35  60 */   959,  95
3ce2e 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3ce2f 39 35 39 2c 20 20 39 35 39 2c 20 20 39 30 38 2c  959,  959,  908,
3ce30 20 20 39 30 37 2c 20 20 39 35 39 2c 20 20 39 35    907,  959,  95
3ce31 39 2c 0d 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  9,.. /*   370 */
3ce32 20 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39     959,  959,  9
3ce33 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3ce34 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39   959,  959,  959
3ce35 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 0d 0a 20  ,  959,  959,.. 
3ce36 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 39 35  /*   380 */   95
3ce37 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  9,  959,  959,  
3ce38 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3ce39 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3ce3a 39 2c 20 20 39 35 39 2c 0d 0a 20 2f 2a 20 20 20  9,  959,.. /*   
3ce3b 33 39 30 20 2a 2f 20 20 20 39 35 39 2c 20 20 39  390 */   959,  9
3ce3c 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c 20  59,  959,  959, 
3ce3d 20 37 39 30 2c 20 20 39 35 39 2c 20 20 37 38 32   790,  959,  782
3ce3e 2c 20 20 39 35 39 2c 20 20 38 36 39 2c 20 20 39  ,  959,  869,  9
3ce3f 35 39 2c 0d 0a 20 2f 2a 20 20 20 34 30 30 20 2a  59,.. /*   400 *
3ce40 2f 20 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20  /   959,  959,  
3ce41 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35 39 2c  959,  959,  959,
3ce42 20 20 39 35 39 2c 20 20 39 35 39 2c 20 20 39 35    959,  959,  95
3ce43 39 2c 20 20 39 35 39 2c 20 20 37 34 35 2c 0d 0a  9,  959,  745,..
3ce44 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 38   /*   410 */   8
3ce45 31 39 2c 20 20 39 35 39 2c 20 20 38 31 38 2c 20  19,  959,  818, 
3ce46 20 38 32 32 2c 20 20 38 31 37 2c 20 20 36 36 38   822,  817,  668
3ce47 2c 20 20 39 35 39 2c 20 20 36 34 39 2c 20 20 39  ,  959,  649,  9
3ce48 35 39 2c 20 20 36 33 32 2c 0d 0a 20 2f 2a 20 20  59,  632,.. /*  
3ce49 20 34 32 30 20 2a 2f 20 20 20 36 33 37 2c 20 20   420 */   637,  
3ce4a 39 35 35 2c 20 20 39 35 38 2c 20 20 39 35 37 2c  955,  958,  957,
3ce4b 20 20 39 35 34 2c 20 20 39 35 33 2c 20 20 39 35    954,  953,  95
3ce4c 32 2c 20 20 39 34 37 2c 20 20 39 34 35 2c 20 20  2,  947,  945,  
3ce4d 39 34 32 2c 0d 0a 20 2f 2a 20 20 20 34 33 30 20  942,.. /*   430 
3ce4e 2a 2f 20 20 20 39 34 31 2c 20 20 39 34 30 2c 20  */   941,  940, 
3ce4f 20 39 33 38 2c 20 20 39 33 35 2c 20 20 39 33 31   938,  935,  931
3ce50 2c 20 20 38 38 39 2c 20 20 38 38 37 2c 20 20 38  ,  889,  887,  8
3ce51 39 34 2c 20 20 38 39 33 2c 20 20 38 39 32 2c 0d  94,  893,  892,.
3ce52 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20  . /*   440 */   
3ce53 38 39 31 2c 20 20 38 39 30 2c 20 20 38 38 38 2c  891,  890,  888,
3ce54 20 20 38 38 36 2c 20 20 38 38 34 2c 20 20 38 30    886,  884,  80
3ce55 35 2c 20 20 38 30 33 2c 20 20 38 30 30 2c 20 20  5,  803,  800,  
3ce56 37 39 37 2c 20 20 39 33 30 2c 0d 0a 20 2f 2a 20  797,  930,.. /* 
3ce57 20 20 34 35 30 20 2a 2f 20 20 20 38 38 32 2c 20    450 */   882, 
3ce58 20 37 34 31 2c 20 20 37 33 38 2c 20 20 37 33 37   741,  738,  737
3ce59 2c 20 20 36 35 37 2c 20 20 39 34 38 2c 20 20 39  ,  657,  948,  9
3ce5a 31 34 2c 20 20 39 32 33 2c 20 20 39 32 31 2c 20  14,  923,  921, 
3ce5b 20 38 31 31 2c 0d 0a 20 2f 2a 20 20 20 34 36 30   811,.. /*   460
3ce5c 20 2a 2f 20 20 20 39 32 30 2c 20 20 39 31 39 2c   */   920,  919,
3ce5d 20 20 39 31 38 2c 20 20 39 31 36 2c 20 20 39 31    918,  916,  91
3ce5e 33 2c 20 20 39 30 30 2c 20 20 38 30 37 2c 20 20  3,  900,  807,  
3ce5f 38 30 36 2c 20 20 37 33 33 2c 20 20 38 37 34 2c  806,  733,  874,
3ce60 0d 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20  .. /*   470 */  
3ce61 20 38 37 33 2c 20 20 36 36 30 2c 20 20 39 30 34   873,  660,  904
3ce62 2c 20 20 39 30 33 2c 20 20 39 30 32 2c 20 20 39  ,  903,  902,  9
3ce63 30 36 2c 20 20 39 31 30 2c 20 20 39 30 31 2c 20  06,  910,  901, 
3ce64 20 37 36 39 2c 20 20 36 35 39 2c 0d 0a 20 2f 2a   769,  659,.. /*
3ce65 20 20 20 34 38 30 20 2a 2f 20 20 20 36 35 36 2c     480 */   656,
3ce66 20 20 36 36 35 2c 20 20 37 32 32 2c 20 20 37 32    665,  722,  72
3ce67 31 2c 20 20 37 32 39 2c 20 20 37 32 37 2c 20 20  1,  729,  727,  
3ce68 37 32 36 2c 20 20 37 32 35 2c 20 20 37 32 34 2c  726,  725,  724,
3ce69 20 20 37 32 33 2c 0d 0a 20 2f 2a 20 20 20 34 39    723,.. /*   49
3ce6a 30 20 2a 2f 20 20 20 37 32 30 2c 20 20 36 36 37  0 */   720,  667
3ce6b 2c 20 20 36 37 35 2c 20 20 36 38 36 2c 20 20 37  ,  675,  686,  7
3ce6c 31 34 2c 20 20 36 39 39 2c 20 20 36 39 38 2c 20  14,  699,  698, 
3ce6d 20 38 37 39 2c 20 20 38 38 31 2c 20 20 38 37 38   879,  881,  878
3ce6e 2c 0d 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20  ,.. /*   500 */ 
3ce6f 20 20 38 37 37 2c 20 20 37 30 37 2c 20 20 37 30    877,  707,  70
3ce70 36 2c 20 20 37 31 32 2c 20 20 37 31 31 2c 20 20  6,  712,  711,  
3ce71 37 31 30 2c 20 20 37 30 39 2c 20 20 37 30 38 2c  710,  709,  708,
3ce72 20 20 37 30 35 2c 20 20 37 30 34 2c 0d 0a 20 2f    705,  704,.. /
3ce73 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 37 30 33  *   510 */   703
3ce74 2c 20 20 36 39 36 2c 20 20 36 39 35 2c 20 20 37  ,  696,  695,  7
3ce75 30 31 2c 20 20 36 39 34 2c 20 20 37 31 37 2c 20  01,  694,  717, 
3ce76 20 37 31 36 2c 20 20 37 31 33 2c 20 20 36 39 33   716,  713,  693
3ce77 2c 20 20 37 33 36 2c 0d 0a 20 2f 2a 20 20 20 35  ,  736,.. /*   5
3ce78 32 30 20 2a 2f 20 20 20 37 33 35 2c 20 20 37 33  20 */   735,  73
3ce79 34 2c 20 20 37 33 31 2c 20 20 36 39 32 2c 20 20  4,  731,  692,  
3ce7a 36 39 31 2c 20 20 36 39 30 2c 20 20 38 32 32 2c  691,  690,  822,
3ce7b 20 20 36 38 39 2c 20 20 36 38 38 2c 20 20 38 32    689,  688,  82
3ce7c 38 2c 0d 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  8,.. /*   530 */
3ce7d 20 20 20 38 32 37 2c 20 20 38 31 35 2c 20 20 38     827,  815,  8
3ce7e 35 38 2c 20 20 37 35 36 2c 20 20 37 35 35 2c 20  58,  756,  755, 
3ce7f 20 37 35 34 2c 20 20 37 36 36 2c 20 20 37 36 35   754,  766,  765
3ce80 2c 20 20 37 37 38 2c 20 20 37 37 37 2c 0d 0a 20  ,  778,  777,.. 
3ce81 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 38 31  /*   540 */   81
3ce82 33 2c 20 20 38 31 32 2c 20 20 37 37 39 2c 20 20  3,  812,  779,  
3ce83 37 36 34 2c 20 20 37 35 38 2c 20 20 37 35 37 2c  764,  758,  757,
3ce84 20 20 37 37 33 2c 20 20 37 37 32 2c 20 20 37 37    773,  772,  77
3ce85 31 2c 20 20 37 37 30 2c 0d 0a 20 2f 2a 20 20 20  1,  770,.. /*   
3ce86 35 35 30 20 2a 2f 20 20 20 37 36 32 2c 20 20 37  550 */   762,  7
3ce87 35 32 2c 20 20 37 38 34 2c 20 20 37 38 37 2c 20  52,  784,  787, 
3ce88 20 37 38 36 2c 20 20 37 38 33 2c 20 20 38 36 30   786,  783,  860
3ce89 2c 20 20 37 36 38 2c 20 20 38 35 37 2c 20 20 39  ,  768,  857,  9
3ce8a 32 39 2c 0d 0a 20 2f 2a 20 20 20 35 36 30 20 2a  29,.. /*   560 *
3ce8b 2f 20 20 20 39 32 38 2c 20 20 39 32 37 2c 20 20  /   928,  927,  
3ce8c 39 32 36 2c 20 20 39 32 35 2c 20 20 38 36 32 2c  926,  925,  862,
3ce8d 20 20 38 36 31 2c 20 20 38 32 39 2c 20 20 38 32    861,  829,  82
3ce8e 36 2c 20 20 36 37 39 2c 20 20 36 38 30 2c 0d 0a  6,  679,  680,..
3ce8f 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 38   /*   570 */   8
3ce90 39 38 2c 20 20 38 39 36 2c 20 20 38 39 37 2c 20  98,  896,  897, 
3ce91 20 38 39 35 2c 20 20 36 38 32 2c 20 20 36 38 31   895,  682,  681
3ce92 2c 20 20 36 37 38 2c 20 20 36 37 37 2c 20 20 38  ,  678,  677,  8
3ce93 35 39 2c 20 20 37 34 37 2c 0d 0a 20 2f 2a 20 20  59,  747,.. /*  
3ce94 20 35 38 30 20 2a 2f 20 20 20 37 34 36 2c 20 20   580 */   746,  
3ce95 38 35 35 2c 20 20 38 35 32 2c 20 20 38 34 34 2c  855,  852,  844,
3ce96 20 20 38 34 30 2c 20 20 38 35 36 2c 20 20 38 35    840,  856,  85
3ce97 33 2c 20 20 38 34 35 2c 20 20 38 34 31 2c 20 20  3,  845,  841,  
3ce98 38 33 39 2c 0d 0a 20 2f 2a 20 20 20 35 39 30 20  839,.. /*   590 
3ce99 2a 2f 20 20 20 38 33 38 2c 20 20 38 32 34 2c 20  */   838,  824, 
3ce9a 20 38 32 33 2c 20 20 38 32 31 2c 20 20 38 32 30   823,  821,  820
3ce9b 2c 20 20 38 31 36 2c 20 20 38 32 35 2c 20 20 36  ,  816,  825,  6
3ce9c 37 30 2c 20 20 37 34 38 2c 20 20 37 34 34 2c 0d  70,  748,  744,.
3ce9d 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20  . /*   600 */   
3ce9e 37 34 33 2c 20 20 38 31 34 2c 20 20 37 35 30 2c  743,  814,  750,
3ce9f 20 20 37 34 39 2c 20 20 36 38 37 2c 20 20 36 38    749,  687,  68
3cea0 35 2c 20 20 36 38 33 2c 20 20 36 36 34 2c 20 20  5,  683,  664,  
3cea1 36 36 32 2c 20 20 36 35 35 2c 0d 0a 20 2f 2a 20  662,  655,.. /* 
3cea2 20 20 36 31 30 20 2a 2f 20 20 20 36 35 33 2c 20    610 */   653, 
3cea3 20 36 35 32 2c 20 20 36 35 34 2c 20 20 36 35 30   652,  654,  650
3cea4 2c 20 20 36 34 38 2c 20 20 36 34 37 2c 20 20 36  ,  648,  647,  6
3cea5 34 36 2c 20 20 36 34 35 2c 20 20 36 34 34 2c 20  46,  645,  644, 
3cea6 20 36 37 33 2c 0d 0a 20 2f 2a 20 20 20 36 32 30   673,.. /*   620
3cea7 20 2a 2f 20 20 20 36 37 32 2c 20 20 36 37 31 2c   */   672,  671,
3cea8 20 20 36 36 39 2c 20 20 36 36 38 2c 20 20 36 34    669,  668,  64
3cea9 32 2c 20 20 36 33 39 2c 20 20 36 33 38 2c 20 20  2,  639,  638,  
3ceaa 36 33 34 2c 20 20 36 33 33 2c 20 20 36 33 31 2c  634,  633,  631,
3ceab 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 54 68 65 20 6e  ..};..../* The n
3ceac 65 78 74 20 74 61 62 6c 65 20 6d 61 70 73 20 74  ext table maps t
3cead 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61 6c 6c 62  okens into fallb
3ceae 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20 49 66 20  ack tokens.  If 
3ceaf 61 20 63 6f 6e 73 74 72 75 63 74 0d 0a 2a 2a 20  a construct..** 
3ceb0 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
3ceb1 6e 67 3a 0d 0a 2a 2a 20 0d 0a 2a 2a 20 20 20 20  ng:..** ..**    
3ceb2 20 20 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58    %fallback ID X
3ceb3 20 59 20 5a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 61 70   Y Z...**..** ap
3ceb4 70 65 61 72 73 20 69 6e 20 74 68 65 20 67 72 61  pears in the gra
3ceb5 6d 6d 61 72 2c 20 74 68 65 6e 20 49 44 20 62 65  mmar, then ID be
3ceb6 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63 6b  comes a fallback
3ceb7 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59 2c   token for X, Y,
3ceb8 0d 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65  ..** and Z.  Whe
3ceb9 6e 65 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65  never one of the
3ceba 20 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72   tokens X, Y, or
3cebb 20 5a 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74   Z is input to t
3cebc 68 65 20 70 61 72 73 65 72 0d 0a 2a 2a 20 62 75  he parser..** bu
3cebd 74 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 70 61  t it does not pa
3cebe 72 73 65 2c 20 74 68 65 20 74 79 70 65 20 6f 66  rse, the type of
3cebf 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 63 68   the token is ch
3cec0 61 6e 67 65 64 20 74 6f 20 49 44 20 61 6e 64 0d  anged to ID and.
3cec1 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 69 73  .** the parse is
3cec2 20 72 65 74 72 69 65 64 20 62 65 66 6f 72 65 20   retried before 
3cec3 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68 72 6f  an error is thro
3cec4 77 6e 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  wn...*/..#ifdef 
3cec5 59 59 46 41 4c 4c 42 41 43 4b 0d 0a 73 74 61 74  YYFALLBACK..stat
3cec6 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54  ic const YYCODET
3cec7 59 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d  YPE yyFallback[]
3cec8 20 3d 20 7b 0d 0a 20 20 20 20 30 2c 20 20 2f 2a   = {..    0,  /*
3cec9 20 20 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e            $ => n
3ceca 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 30  othing */..    0
3cecb 2c 20 20 2f 2a 20 20 20 20 20 20 20 53 45 4d 49  ,  /*       SEMI
3cecc 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a   => nothing */..
3cecd 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 45 58     26,  /*    EX
3cece 50 4c 41 49 4e 20 3d 3e 20 49 44 20 2a 2f 0d 0a  PLAIN => ID */..
3cecf 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3ced0 51 55 45 52 59 20 3d 3e 20 49 44 20 2a 2f 0d 0a  QUERY => ID */..
3ced1 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3ced2 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a 2f 0d 0a   PLAN => ID */..
3ced3 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3ced4 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f 0d 0a  BEGIN => ID */..
3ced5 20 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e 53      0,  /* TRANS
3ced6 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e  ACTION => nothin
3ced7 67 20 2a 2f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a  g */..   26,  /*
3ced8 20 20 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49     DEFERRED => I
3ced9 44 20 2a 2f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a  D */..   26,  /*
3ceda 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49    IMMEDIATE => I
3cedb 44 20 2a 2f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a  D */..   26,  /*
3cedc 20 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20 49    EXCLUSIVE => I
3cedd 44 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 2f 2a  D */..    0,  /*
3cede 20 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e       COMMIT => n
3cedf 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 32 36  othing */..   26
3cee0 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 45 4e 44  ,  /*        END
3cee1 20 3d 3e 20 49 44 20 2a 2f 0d 0a 20 20 20 32 36   => ID */..   26
3cee2 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b  ,  /*   ROLLBACK
3cee3 20 3d 3e 20 49 44 20 2a 2f 0d 0a 20 20 20 32 36   => ID */..   26
3cee4 2c 20 20 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54  ,  /*  SAVEPOINT
3cee5 20 3d 3e 20 49 44 20 2a 2f 0d 0a 20 20 20 32 36   => ID */..   26
3cee6 2c 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53 45  ,  /*    RELEASE
3cee7 20 3d 3e 20 49 44 20 2a 2f 0d 0a 20 20 20 20 30   => ID */..    0
3cee8 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f  ,  /*         TO
3cee9 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a   => nothing */..
3ceea 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
3ceeb 54 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  TABLE => nothing
3ceec 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 2f 2a 20   */..    0,  /* 
3ceed 20 20 20 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f      CREATE => no
3ceee 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 32 36 2c  thing */..   26,
3ceef 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 46 20    /*         IF 
3cef0 3d 3e 20 49 44 20 2a 2f 0d 0a 20 20 20 20 30 2c  => ID */..    0,
3cef1 20 20 2f 2a 20 20 20 20 20 20 20 20 4e 4f 54 20    /*        NOT 
3cef2 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20  => nothing */.. 
3cef3 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 58     0,  /*     EX
3cef4 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ISTS => nothing 
3cef5 2a 2f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20  */..   26,  /*  
3cef6 20 20 20 20 20 54 45 4d 50 20 3d 3e 20 49 44 20       TEMP => ID 
3cef7 2a 2f 0d 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20  */..    0,  /*  
3cef8 20 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f 74         LP => not
3cef9 68 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 30 2c 20  hing */..    0, 
3cefa 20 2f 2a 20 20 20 20 20 20 20 20 20 52 50 20 3d   /*         RP =
3cefb 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20 20  > nothing */..  
3cefc 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
3cefd 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   AS => nothing *
3cefe 2f 0d 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  /..    0,  /*   
3ceff 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68     COMMA => noth
3cf00 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 20  ing */..    0,  
3cf01 2f 2a 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e  /*         ID =>
3cf02 20 6e 6f 74 68 69 6e 67 20 2a 2f 0d 0a 20 20 20   nothing */..   
3cf03 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 44 45 58   0,  /*    INDEX
3cf04 45 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  ED => nothing */
3cf05 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf06 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a 2f    ABORT => ID */
3cf07 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf08 20 41 43 54 49 4f 4e 20 3d 3e 20 49 44 20 2a 2f   ACTION => ID */
3cf09 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf0a 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f    AFTER => ID */
3cf0b 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf0c 41 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f  ANALYZE => ID */
3cf0d 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf0e 20 20 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f      ASC => ID */
3cf0f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf10 20 41 54 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   ATTACH => ID */
3cf11 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf12 20 42 45 46 4f 52 45 20 3d 3e 20 49 44 20 2a 2f   BEFORE => ID */
3cf13 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf14 20 20 20 20 20 42 59 20 3d 3e 20 49 44 20 2a 2f       BY => ID */
3cf15 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf16 43 41 53 43 41 44 45 20 3d 3e 20 49 44 20 2a 2f  CASCADE => ID */
3cf17 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf18 20 20 20 43 41 53 54 20 3d 3e 20 49 44 20 2a 2f     CAST => ID */
3cf19 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  ..   26,  /*   C
3cf1a 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49 44 20 2a 2f  OLUMNKW => ID */
3cf1b 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  ..   26,  /*   C
3cf1c 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a 2f  ONFLICT => ID */
3cf1d 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 44  ..   26,  /*   D
3cf1e 41 54 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f  ATABASE => ID */
3cf1f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf20 20 20 20 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f     DESC => ID */
3cf21 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf22 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   DETACH => ID */
3cf23 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf24 20 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f     EACH => ID */
3cf25 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf26 20 20 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f     FAIL => ID */
3cf27 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf28 20 20 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f      FOR => ID */
3cf29 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf2a 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f   IGNORE => ID */
3cf2b 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e  ..   26,  /*  IN
3cf2c 49 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f  ITIALLY => ID */
3cf2d 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf2e 49 4e 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f  INSTEAD => ID */
3cf2f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf30 4c 49 4b 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f  LIKE_KW => ID */
3cf31 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf32 20 20 4d 41 54 43 48 20 3d 3e 20 49 44 20 2a 2f    MATCH => ID */
3cf33 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf34 20 20 20 20 20 4e 4f 20 3d 3e 20 49 44 20 2a 2f       NO => ID */
3cf35 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf36 20 20 20 20 4b 45 59 20 3d 3e 20 49 44 20 2a 2f      KEY => ID */
3cf37 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf38 20 20 20 20 20 4f 46 20 3d 3e 20 49 44 20 2a 2f       OF => ID */
3cf39 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf3a 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20 2a 2f   OFFSET => ID */
3cf3b 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf3c 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f   PRAGMA => ID */
3cf3d 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf3e 20 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f    RAISE => ID */
3cf3f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf40 52 45 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f  REPLACE => ID */
3cf41 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 52  ..   26,  /*   R
3cf42 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f  ESTRICT => ID */
3cf43 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf44 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a 2f      ROW => ID */
3cf45 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf46 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f  TRIGGER => ID */
3cf47 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf48 20 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f   VACUUM => ID */
3cf49 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf4a 20 20 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f     VIEW => ID */
3cf4b 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf4c 56 49 52 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f  VIRTUAL => ID */
3cf4d 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf4e 52 45 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a 2f  REINDEX => ID */
3cf4f 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  ..   26,  /*    
3cf50 20 52 45 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f   RENAME => ID */
3cf51 0d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  ..   26,  /*   C
3cf52 54 49 4d 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f  TIME_KW => ID */
3cf53 0d 0a 7d 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20  ..};..#endif /* 
3cf54 59 59 46 41 4c 4c 42 41 43 4b 20 2a 2f 0d 0a 0d  YYFALLBACK */...
3cf55 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
3cf56 67 20 73 74 72 75 63 74 75 72 65 20 72 65 70 72  g structure repr
3cf57 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20  esents a single 
3cf58 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0d 0a  element of the..
3cf59 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61 63  ** parser's stac
3cf5a 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  k.  Information 
3cf5b 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73 3a  stored includes:
3cf5c 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2b 20 20 54 68  ..**..**   +  Th
3cf5d 65 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66  e state number f
3cf5e 6f 72 20 74 68 65 20 70 61 72 73 65 72 20 61 74  or the parser at
3cf5f 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74   this level of t
3cf60 68 65 20 73 74 61 63 6b 2e 0d 0a 2a 2a 0d 0a 2a  he stack...**..*
3cf61 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c 75 65  *   +  The value
3cf62 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 73 74   of the token st
3cf63 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76  ored at this lev
3cf64 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
3cf65 0d 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f 74  ..**      (In ot
3cf66 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 22  her words, the "
3cf67 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0d 0a  major" token.)..
3cf68 2a 2a 0d 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  **..**   +  The 
3cf69 73 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73  semantic value s
3cf6a 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65  tored at this le
3cf6b 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b  vel of the stack
3cf6c 2e 20 20 54 68 69 73 20 69 73 0d 0a 2a 2a 20 20  .  This is..**  
3cf6d 20 20 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74      the informat
3cf6e 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20  ion used by the 
3cf6f 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20  action routines 
3cf70 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0d  in the grammar..
3cf71 0a 2a 2a 20 20 20 20 20 20 49 74 20 69 73 20 73  .**      It is s
3cf72 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20  ometimes called 
3cf73 74 68 65 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65  the "minor" toke
3cf74 6e 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 79  n...*/..struct y
3cf75 79 53 74 61 63 6b 45 6e 74 72 79 20 7b 0d 0a 20  yStackEntry {.. 
3cf76 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 73 74   YYACTIONTYPE st
3cf77 61 74 65 6e 6f 3b 20 20 2f 2a 20 54 68 65 20 73  ateno;  /* The s
3cf78 74 61 74 65 2d 6e 75 6d 62 65 72 20 2a 2f 0d 0a  tate-number */..
3cf79 20 20 59 59 43 4f 44 45 54 59 50 45 20 6d 61 6a    YYCODETYPE maj
3cf7a 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or;      /* The 
3cf7b 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75  major token valu
3cf7c 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  e.  This is the 
3cf7d 63 6f 64 65 0d 0a 20 20 20 20 20 20 20 20 20 20  code..          
3cf7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
3cf7f 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
3cf80 20 74 6f 6b 65 6e 20 61 74 20 74 68 69 73 20 73   token at this s
3cf81 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f 0d 0a 20  tack level */.. 
3cf82 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d 69 6e   YYMINORTYPE min
3cf83 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 75  or;     /* The u
3cf84 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d 69 6e  ser-supplied min
3cf85 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20  or token value. 
3cf86 20 54 68 69 73 0d 0a 20 20 20 20 20 20 20 20 20   This..         
3cf87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3cf88 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ** is the value 
3cf89 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f  of the token  */
3cf8a 0d 0a 7d 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  ..};..typedef st
3cf8b 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
3cf8c 79 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0d  y yyStackEntry;.
3cf8d 0a 0d 0a 2f 2a 20 54 68 65 20 73 74 61 74 65 20  .../* The state 
3cf8e 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73  of the parser is
3cf8f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74   completely cont
3cf90 61 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73 74  ained in an inst
3cf91 61 6e 63 65 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  ance of..** the 
3cf92 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
3cf93 75 72 65 20 2a 2f 0d 0a 73 74 72 75 63 74 20 79  ure */..struct y
3cf94 79 50 61 72 73 65 72 20 7b 0d 0a 20 20 69 6e 74  yParser {..  int
3cf95 20 79 79 69 64 78 3b 20 20 20 20 20 20 20 20 20   yyidx;         
3cf96 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3cf97 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65  dex of top eleme
3cf98 6e 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0d 0a  nt in stack */..
3cf99 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
3cf9a 58 53 54 41 43 4b 44 45 50 54 48 0d 0a 20 20 69  XSTACKDEPTH..  i
3cf9b 6e 74 20 79 79 69 64 78 4d 61 78 3b 20 20 20 20  nt yyidxMax;    
3cf9c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3cf9d 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  Maximum value of
3cf9e 20 79 79 69 64 78 20 2a 2f 0d 0a 23 65 6e 64 69   yyidx */..#endi
3cf9f 66 0d 0a 20 20 69 6e 74 20 79 79 65 72 72 63 6e  f..  int yyerrcn
3cfa0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3cfa1 20 20 20 2f 2a 20 53 68 69 66 74 73 20 6c 65 66     /* Shifts lef
3cfa2 74 20 62 65 66 6f 72 65 20 6f 75 74 20 6f 66 20  t before out of 
3cfa3 74 68 65 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20  the error */..  
3cfa4 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
3cfa5 5f 53 44 45 43 4c 20 20 20 20 20 20 20 20 20 20  _SDECL          
3cfa6 20 20 20 20 20 20 2f 2a 20 41 20 70 6c 61 63 65        /* A place
3cfa7 20 74 6f 20 68 6f 6c 64 20 25 65 78 74 72 61 5f   to hold %extra_
3cfa8 61 72 67 75 6d 65 6e 74 20 2a 2f 0d 0a 23 69 66  argument */..#if
3cfa9 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
3cfaa 0d 0a 20 20 69 6e 74 20 79 79 73 74 6b 73 7a 3b  ..  int yystksz;
3cfab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3cfac 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 64    /* Current sid
3cfad 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 2a  e of the stack *
3cfae 2f 0d 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  /..  yyStackEntr
3cfaf 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20 20 20  y *yystack;     
3cfb0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
3cfb1 27 73 20 73 74 61 63 6b 20 2a 2f 0d 0a 23 65 6c  's stack */..#el
3cfb2 73 65 0d 0a 20 20 79 79 53 74 61 63 6b 45 6e 74  se..  yyStackEnt
3cfb3 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41  ry yystack[YYSTA
3cfb4 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68  CKDEPTH];  /* Th
3cfb5 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
3cfb6 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d 3b 0d   */..#endif..};.
3cfb7 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3cfb8 79 79 50 61 72 73 65 72 20 79 79 50 61 72 73 65  yyParser yyParse
3cfb9 72 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44  r;....#ifndef ND
3cfba 45 42 55 47 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  EBUG../* #includ
3cfbb 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0d 0a  e <stdio.h> */..
3cfbc 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54  static FILE *yyT
3cfbd 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0d 0a 73  raceFILE = 0;..s
3cfbe 74 61 74 69 63 20 63 68 61 72 20 2a 79 79 54 72  tatic char *yyTr
3cfbf 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0d 0a  acePrompt = 0;..
3cfc0 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
3cfc1 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e   */....#ifndef N
3cfc2 44 45 42 55 47 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54  DEBUG../* ..** T
3cfc3 75 72 6e 20 70 61 72 73 65 72 20 74 72 61 63 69  urn parser traci
3cfc4 6e 67 20 6f 6e 20 62 79 20 67 69 76 69 6e 67 20  ng on by giving 
3cfc5 61 20 73 74 72 65 61 6d 20 74 6f 20 77 68 69 63  a stream to whic
3cfc6 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 74  h to write the t
3cfc7 72 61 63 65 0d 0a 2a 2a 20 61 6e 64 20 61 20 70  race..** and a p
3cfc8 72 6f 6d 70 74 20 74 6f 20 70 72 65 66 61 63 65  rompt to preface
3cfc9 20 65 61 63 68 20 74 72 61 63 65 20 6d 65 73 73   each trace mess
3cfca 61 67 65 2e 20 20 54 72 61 63 69 6e 67 20 69 73  age.  Tracing is
3cfcb 20 74 75 72 6e 65 64 20 6f 66 66 0d 0a 2a 2a 20   turned off..** 
3cfcc 62 79 20 6d 61 6b 69 6e 67 20 65 69 74 68 65 72  by making either
3cfcd 20 61 72 67 75 6d 65 6e 74 20 4e 55 4c 4c 20 0d   argument NULL .
3cfce 0a 2a 2a 0d 0a 2a 2a 20 49 6e 70 75 74 73 3a 0d  .**..** Inputs:.
3cfcf 0a 2a 2a 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69  .** <ul>..** <li
3cfd0 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20 77 68 69  > A FILE* to whi
3cfd1 63 68 20 74 72 61 63 65 20 6f 75 74 70 75 74 20  ch trace output 
3cfd2 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
3cfd3 6e 2e 0d 0a 2a 2a 20 20 20 20 20 20 49 66 20 4e  n...**      If N
3cfd4 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
3cfd5 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 2e  g is turned off.
3cfd6 0d 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65 66  ..** <li> A pref
3cfd7 69 78 20 73 74 72 69 6e 67 20 77 72 69 74 74 65  ix string writte
3cfd8 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  n at the beginni
3cfd9 6e 67 20 6f 66 20 65 76 65 72 79 0d 0a 2a 2a 20  ng of every..** 
3cfda 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72 61       line of tra
3cfdb 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20 4e  ce output.  If N
3cfdc 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
3cfdd 67 20 69 73 0d 0a 2a 2a 20 20 20 20 20 20 74 75  g is..**      tu
3cfde 72 6e 65 64 20 6f 66 66 2e 0d 0a 2a 2a 20 3c 2f  rned off...** </
3cfdf 75 6c 3e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 75 74 70  ul>..**..** Outp
3cfe0 75 74 73 3a 0d 0a 2a 2a 20 4e 6f 6e 65 2e 0d 0a  uts:..** None...
3cfe1 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
3cfe2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
3cfe3 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 20  arserTrace(FILE 
3cfe4 2a 54 72 61 63 65 46 49 4c 45 2c 20 63 68 61 72  *TraceFILE, char
3cfe5 20 2a 7a 54 72 61 63 65 50 72 6f 6d 70 74 29 7b   *zTracePrompt){
3cfe6 0d 0a 20 20 79 79 54 72 61 63 65 46 49 4c 45 20  ..  yyTraceFILE 
3cfe7 3d 20 54 72 61 63 65 46 49 4c 45 3b 0d 0a 20 20  = TraceFILE;..  
3cfe8 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20  yyTracePrompt = 
3cfe9 7a 54 72 61 63 65 50 72 6f 6d 70 74 3b 0d 0a 20  zTracePrompt;.. 
3cfea 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
3cfeb 3d 3d 30 20 29 20 79 79 54 72 61 63 65 50 72 6f  ==0 ) yyTracePro
3cfec 6d 70 74 20 3d 20 30 3b 0d 0a 20 20 65 6c 73 65  mpt = 0;..  else
3cfed 20 69 66 28 20 79 79 54 72 61 63 65 50 72 6f 6d   if( yyTraceProm
3cfee 70 74 3d 3d 30 20 29 20 79 79 54 72 61 63 65 46  pt==0 ) yyTraceF
3cfef 49 4c 45 20 3d 20 30 3b 0d 0a 7d 0d 0a 23 65 6e  ILE = 0;..}..#en
3cff0 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
3cff1 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ....#ifndef NDEB
3cff2 55 47 0d 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69  UG../* For traci
3cff3 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20 6e  ng shifts, the n
3cff4 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d  ames of all term
3cff5 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72  inals and nonter
3cff6 6d 69 6e 61 6c 73 0d 0a 2a 2a 20 61 72 65 20 72  minals..** are r
3cff7 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f  equired.  The fo
3cff8 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75  llowing table su
3cff9 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d  pplies these nam
3cffa 65 73 20 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f  es */..static co
3cffb 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
3cffc 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20  yyTokenName[] = 
3cffd 7b 20 0d 0a 20 20 22 24 22 2c 20 20 20 20 20 20  { ..  "$",      
3cffe 20 20 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20         "SEMI",  
3cfff 20 20 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e          "EXPLAIN
3d000 22 2c 20 20 20 20 20 20 20 22 51 55 45 52 59 22  ",       "QUERY"
3d001 2c 20 20 20 20 20 20 20 0d 0a 20 20 22 50 4c 41  ,       ..  "PLA
3d002 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 42 45  N",          "BE
3d003 47 49 4e 22 2c 20 20 20 20 20 20 20 20 20 22 54  GIN",         "T
3d004 52 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20 22  RANSACTION",   "
3d005 44 45 46 45 52 52 45 44 22 2c 20 20 20 20 0d 0a  DEFERRED",    ..
3d006 20 20 22 49 4d 4d 45 44 49 41 54 45 22 2c 20 20    "IMMEDIATE",  
3d007 20 20 20 22 45 58 43 4c 55 53 49 56 45 22 2c 20     "EXCLUSIVE", 
3d008 20 20 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20      "COMMIT",   
3d009 20 20 20 20 20 22 45 4e 44 22 2c 20 20 20 20 20       "END",     
3d00a 20 20 20 20 0d 0a 20 20 22 52 4f 4c 4c 42 41 43      ..  "ROLLBAC
3d00b 4b 22 2c 20 20 20 20 20 20 22 53 41 56 45 50 4f  K",      "SAVEPO
3d00c 49 4e 54 22 2c 20 20 20 20 20 22 52 45 4c 45 41  INT",     "RELEA
3d00d 53 45 22 2c 20 20 20 20 20 20 20 22 54 4f 22 2c  SE",       "TO",
3d00e 20 20 20 20 20 20 20 20 20 20 0d 0a 20 20 22 54            ..  "T
3d00f 41 42 4c 45 22 2c 20 20 20 20 20 20 20 20 20 22  ABLE",         "
3d010 43 52 45 41 54 45 22 2c 20 20 20 20 20 20 20 20  CREATE",        
3d011 22 49 46 22 2c 20 20 20 20 20 20 20 20 20 20 20  "IF",           
3d012 20 22 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 20   "NOT",         
3d013 0d 0a 20 20 22 45 58 49 53 54 53 22 2c 20 20 20  ..  "EXISTS",   
3d014 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20 20       "TEMP",    
3d015 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20 20        "LP",     
3d016 20 20 20 20 20 20 20 22 52 50 22 2c 20 20 20 20         "RP",    
3d017 20 20 20 20 20 20 0d 0a 20 20 22 41 53 22 2c 20        ..  "AS", 
3d018 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d             "COMM
3d019 41 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22  A",         "ID"
3d01a 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e  ,            "IN
3d01b 44 45 58 45 44 22 2c 20 20 20 20 20 0d 0a 20 20  DEXED",     ..  
3d01c 22 41 42 4f 52 54 22 2c 20 20 20 20 20 20 20 20  "ABORT",        
3d01d 20 22 41 43 54 49 4f 4e 22 2c 20 20 20 20 20 20   "ACTION",      
3d01e 20 20 22 41 46 54 45 52 22 2c 20 20 20 20 20 20    "AFTER",      
3d01f 20 20 20 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20     "ANALYZE",   
3d020 20 20 0d 0a 20 20 22 41 53 43 22 2c 20 20 20 20    ..  "ASC",    
3d021 20 20 20 20 20 20 20 22 41 54 54 41 43 48 22 2c         "ATTACH",
3d022 20 20 20 20 20 20 20 20 22 42 45 46 4f 52 45 22          "BEFORE"
3d023 2c 20 20 20 20 20 20 20 20 22 42 59 22 2c 20 20  ,        "BY",  
3d024 20 20 20 20 20 20 20 20 0d 0a 20 20 22 43 41 53          ..  "CAS
3d025 43 41 44 45 22 2c 20 20 20 20 20 20 20 22 43 41  CADE",       "CA
3d026 53 54 22 2c 20 20 20 20 20 20 20 20 20 20 22 43  ST",          "C
3d027 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20 20 20 22  OLUMNKW",      "
3d028 43 4f 4e 46 4c 49 43 54 22 2c 20 20 20 20 0d 0a  CONFLICT",    ..
3d029 20 20 22 44 41 54 41 42 41 53 45 22 2c 20 20 20    "DATABASE",   
3d02a 20 20 20 22 44 45 53 43 22 2c 20 20 20 20 20 20     "DESC",      
3d02b 20 20 20 20 22 44 45 54 41 43 48 22 2c 20 20 20      "DETACH",   
3d02c 20 20 20 20 20 22 45 41 43 48 22 2c 20 20 20 20       "EACH",    
3d02d 20 20 20 20 0d 0a 20 20 22 46 41 49 4c 22 2c 20      ..  "FAIL", 
3d02e 20 20 20 20 20 20 20 20 20 22 46 4f 52 22 2c 20           "FOR", 
3d02f 20 20 20 20 20 20 20 20 20 20 22 49 47 4e 4f 52            "IGNOR
3d030 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e 49 54  E",        "INIT
3d031 49 41 4c 4c 59 22 2c 20 20 20 0d 0a 20 20 22 49  IALLY",   ..  "I
3d032 4e 53 54 45 41 44 22 2c 20 20 20 20 20 20 20 22  NSTEAD",       "
3d033 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20 20 20  LIKE_KW",       
3d034 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20 20 20  "MATCH",        
3d035 20 22 4e 4f 22 2c 20 20 20 20 20 20 20 20 20 20   "NO",          
3d036 0d 0a 20 20 22 4b 45 59 22 2c 20 20 20 20 20 20  ..  "KEY",      
3d037 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20 20 20       "OF",      
3d038 20 20 20 20 20 20 22 4f 46 46 53 45 54 22 2c 20        "OFFSET", 
3d039 20 20 20 20 20 20 20 22 50 52 41 47 4d 41 22 2c         "PRAGMA",
3d03a 20 20 20 20 20 20 0d 0a 20 20 22 52 41 49 53 45        ..  "RAISE
3d03b 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 50 4c  ",         "REPL
3d03c 41 43 45 22 2c 20 20 20 20 20 20 20 22 52 45 53  ACE",       "RES
3d03d 54 52 49 43 54 22 2c 20 20 20 20 20 20 22 52 4f  TRICT",      "RO
3d03e 57 22 2c 20 20 20 20 20 20 20 20 20 0d 0a 20 20  W",         ..  
3d03f 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20 20  "TRIGGER",      
3d040 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20   "VACUUM",      
3d041 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20 20    "VIEW",       
3d042 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20 20     "VIRTUAL",   
3d043 20 20 0d 0a 20 20 22 52 45 49 4e 44 45 58 22 2c    ..  "REINDEX",
3d044 20 20 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c         "RENAME",
3d045 20 20 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b          "CTIME_K
3d046 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
3d047 20 20 20 20 20 20 20 20 0d 0a 20 20 22 4f 52 22          ..  "OR"
3d048 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 41 4e  ,            "AN
3d049 44 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49  D",           "I
3d04a 53 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  S",            "
3d04b 42 45 54 57 45 45 4e 22 2c 20 20 20 20 20 0d 0a  BETWEEN",     ..
3d04c 20 20 22 49 4e 22 2c 20 20 20 20 20 20 20 20 20    "IN",         
3d04d 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20     "ISNULL",    
3d04e 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20      "NOTNULL",  
3d04f 20 20 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20       "NE",      
3d050 20 20 20 20 0d 0a 20 20 22 45 51 22 2c 20 20 20      ..  "EQ",   
3d051 20 20 20 20 20 20 20 20 20 22 47 54 22 2c 20 20           "GT",  
3d052 20 20 20 20 20 20 20 20 20 20 22 4c 45 22 2c 20            "LE", 
3d053 20 20 20 20 20 20 20 20 20 20 20 22 4c 54 22 2c             "LT",
3d054 20 20 20 20 20 20 20 20 20 20 0d 0a 20 20 22 47            ..  "G
3d055 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  E",            "
3d056 45 53 43 41 50 45 22 2c 20 20 20 20 20 20 20 20  ESCAPE",        
3d057 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20 20  "BITAND",       
3d058 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20 20   "BITOR",       
3d059 0d 0a 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20  ..  "LSHIFT",   
3d05a 20 20 20 20 20 22 52 53 48 49 46 54 22 2c 20 20       "RSHIFT",  
3d05b 20 20 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20        "PLUS",   
3d05c 20 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20         "MINUS", 
3d05d 20 20 20 20 20 20 0d 0a 20 20 22 53 54 41 52 22        ..  "STAR"
3d05e 2c 20 20 20 20 20 20 20 20 20 20 22 53 4c 41 53  ,          "SLAS
3d05f 48 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 4d  H",         "REM
3d060 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 43 4f  ",           "CO
3d061 4e 43 41 54 22 2c 20 20 20 20 20 20 0d 0a 20 20  NCAT",      ..  
3d062 22 43 4f 4c 4c 41 54 45 22 2c 20 20 20 20 20 20  "COLLATE",      
3d063 20 22 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20   "BITNOT",      
3d064 20 20 22 53 54 52 49 4e 47 22 2c 20 20 20 20 20    "STRING",     
3d065 20 20 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20     "JOIN_KW",   
3d066 20 20 0d 0a 20 20 22 43 4f 4e 53 54 52 41 49 4e    ..  "CONSTRAIN
3d067 54 22 2c 20 20 20 20 22 44 45 46 41 55 4c 54 22  T",    "DEFAULT"
3d068 2c 20 20 20 20 20 20 20 22 4e 55 4c 4c 22 2c 20  ,       "NULL", 
3d069 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52           "PRIMAR
3d06a 59 22 2c 20 20 20 20 20 0d 0a 20 20 22 55 4e 49  Y",     ..  "UNI
3d06b 51 55 45 22 2c 20 20 20 20 20 20 20 20 22 43 48  QUE",        "CH
3d06c 45 43 4b 22 2c 20 20 20 20 20 20 20 20 20 22 52  ECK",         "R
3d06d 45 46 45 52 45 4e 43 45 53 22 2c 20 20 20 20 22  EFERENCES",    "
3d06e 41 55 54 4f 49 4e 43 52 22 2c 20 20 20 20 0d 0a  AUTOINCR",    ..
3d06f 20 20 22 4f 4e 22 2c 20 20 20 20 20 20 20 20 20    "ON",         
3d070 20 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20     "INSERT",    
3d071 20 20 20 20 22 44 45 4c 45 54 45 22 2c 20 20 20      "DELETE",   
3d072 20 20 20 20 20 22 55 50 44 41 54 45 22 2c 20 20       "UPDATE",  
3d073 20 20 20 20 0d 0a 20 20 22 53 45 54 22 2c 20 20      ..  "SET",  
3d074 20 20 20 20 20 20 20 20 20 22 44 45 46 45 52 52           "DEFERR
3d075 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52 45 49  ABLE",    "FOREI
3d076 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52 4f 50  GN",       "DROP
3d077 22 2c 20 20 20 20 20 20 20 20 0d 0a 20 20 22 55  ",        ..  "U
3d078 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 20 22  NION",         "
3d079 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 20  ALL",           
3d07a 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20 20  "EXCEPT",       
3d07b 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20 20   "INTERSECT",   
3d07c 0d 0a 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20  ..  "SELECT",   
3d07d 20 20 20 20 20 22 44 49 53 54 49 4e 43 54 22 2c       "DISTINCT",
3d07e 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20        "DOT",    
3d07f 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20         "FROM",  
3d080 20 20 20 20 20 20 0d 0a 20 20 22 4a 4f 49 4e 22        ..  "JOIN"
3d081 2c 20 20 20 20 20 20 20 20 20 20 22 55 53 49 4e  ,          "USIN
3d082 47 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44  G",         "ORD
3d083 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 47 52  ER",         "GR
3d084 4f 55 50 22 2c 20 20 20 20 20 20 20 0d 0a 20 20  OUP",       ..  
3d085 22 48 41 56 49 4e 47 22 2c 20 20 20 20 20 20 20  "HAVING",       
3d086 20 22 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20   "LIMIT",       
3d087 20 20 22 57 48 45 52 45 22 2c 20 20 20 20 20 20    "WHERE",      
3d088 20 20 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20     "INTO",      
3d089 20 20 0d 0a 20 20 22 56 41 4c 55 45 53 22 2c 20    ..  "VALUES", 
3d08a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52 22         "INTEGER"
3d08b 2c 20 20 20 20 20 20 20 22 46 4c 4f 41 54 22 2c  ,       "FLOAT",
3d08c 20 20 20 20 20 20 20 20 20 22 42 4c 4f 42 22 2c           "BLOB",
3d08d 20 20 20 20 20 20 20 20 0d 0a 20 20 22 52 45 47          ..  "REG
3d08e 49 53 54 45 52 22 2c 20 20 20 20 20 20 22 56 41  ISTER",      "VA
3d08f 52 49 41 42 4c 45 22 2c 20 20 20 20 20 20 22 43  RIABLE",      "C
3d090 41 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  ASE",          "
3d091 57 48 45 4e 22 2c 20 20 20 20 20 20 20 20 0d 0a  WHEN",        ..
3d092 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20 20    "THEN",       
3d093 20 20 20 22 45 4c 53 45 22 2c 20 20 20 20 20 20     "ELSE",      
3d094 20 20 20 20 22 49 4e 44 45 58 22 2c 20 20 20 20      "INDEX",    
3d095 20 20 20 20 20 22 41 4c 54 45 52 22 2c 20 20 20       "ALTER",   
3d096 20 20 20 20 0d 0a 20 20 22 41 44 44 22 2c 20 20      ..  "ADD",  
3d097 20 20 20 20 20 20 20 20 20 22 65 72 72 6f 72 22           "error"
3d098 2c 20 20 20 20 20 20 20 20 20 22 69 6e 70 75 74  ,         "input
3d099 22 2c 20 20 20 20 20 20 20 20 20 22 63 6d 64 6c  ",         "cmdl
3d09a 69 73 74 22 2c 20 20 20 20 20 0d 0a 20 20 22 65  ist",     ..  "e
3d09b 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20 20 22  cmd",          "
3d09c 65 78 70 6c 61 69 6e 22 2c 20 20 20 20 20 20 20  explain",       
3d09d 22 63 6d 64 78 22 2c 20 20 20 20 20 20 20 20 20  "cmdx",         
3d09e 20 22 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20   "cmd",         
3d09f 0d 0a 20 20 22 74 72 61 6e 73 74 79 70 65 22 2c  ..  "transtype",
3d0a0 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70 74 22       "trans_opt"
3d0a1 2c 20 20 20 20 20 22 6e 6d 22 2c 20 20 20 20 20  ,     "nm",     
3d0a2 20 20 20 20 20 20 20 22 73 61 76 65 70 6f 69 6e         "savepoin
3d0a3 74 5f 6f 70 74 22 2c 0d 0a 20 20 22 63 72 65 61  t_opt",..  "crea
3d0a4 74 65 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65  te_table",  "cre
3d0a5 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c  ate_table_args",
3d0a6 20 20 22 63 72 65 61 74 65 6b 77 22 2c 20 20 20    "createkw",   
3d0a7 20 20 20 22 74 65 6d 70 22 2c 20 20 20 20 20 20     "temp",      
3d0a8 20 20 0d 0a 20 20 22 69 66 6e 6f 74 65 78 69 73    ..  "ifnotexis
3d0a9 74 73 22 2c 20 20 20 22 64 62 6e 6d 22 2c 20 20  ts",   "dbnm",  
3d0aa 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 6c          "columnl
3d0ab 69 73 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c 69  ist",    "consli
3d0ac 73 74 5f 6f 70 74 22 2c 0d 0a 20 20 22 73 65 6c  st_opt",..  "sel
3d0ad 65 63 74 22 2c 20 20 20 20 20 20 20 20 22 63 6f  ect",        "co
3d0ae 6c 75 6d 6e 22 2c 20 20 20 20 20 20 20 20 22 63  lumn",        "c
3d0af 6f 6c 75 6d 6e 69 64 22 2c 20 20 20 20 20 20 22  olumnid",      "
3d0b0 74 79 70 65 22 2c 20 20 20 20 20 20 20 20 0d 0a  type",        ..
3d0b1 20 20 22 63 61 72 67 6c 69 73 74 22 2c 20 20 20    "carglist",   
3d0b2 20 20 20 22 69 64 22 2c 20 20 20 20 20 20 20 20     "id",        
3d0b3 20 20 20 20 22 69 64 73 22 2c 20 20 20 20 20 20      "ids",      
3d0b4 20 20 20 20 20 22 74 79 70 65 74 6f 6b 65 6e 22       "typetoken"
3d0b5 2c 20 20 20 0d 0a 20 20 22 74 79 70 65 6e 61 6d  ,   ..  "typenam
3d0b6 65 22 2c 20 20 20 20 20 20 22 73 69 67 6e 65 64  e",      "signed
3d0b7 22 2c 20 20 20 20 20 20 20 20 22 70 6c 75 73 5f  ",        "plus_
3d0b8 6e 75 6d 22 2c 20 20 20 20 20 20 22 6d 69 6e 75  num",      "minu
3d0b9 73 5f 6e 75 6d 22 2c 20 20 20 0d 0a 20 20 22 63  s_num",   ..  "c
3d0ba 61 72 67 22 2c 20 20 20 20 20 20 20 20 20 20 22  arg",          "
3d0bb 63 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20  ccons",         
3d0bc 22 74 65 72 6d 22 2c 20 20 20 20 20 20 20 20 20  "term",         
3d0bd 20 22 65 78 70 72 22 2c 20 20 20 20 20 20 20 20   "expr",        
3d0be 0d 0a 20 20 22 6f 6e 63 6f 6e 66 22 2c 20 20 20  ..  "onconf",   
3d0bf 20 20 20 20 20 22 73 6f 72 74 6f 72 64 65 72 22       "sortorder"
3d0c0 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63 22 2c  ,     "autoinc",
3d0c1 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74 5f         "idxlist_
3d0c2 6f 70 74 22 2c 20 0d 0a 20 20 22 72 65 66 61 72  opt", ..  "refar
3d0c3 67 73 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  gs",       "defe
3d0c4 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20 22  r_subclause",  "
3d0c5 72 65 66 61 72 67 22 2c 20 20 20 20 20 20 20 20  refarg",        
3d0c6 22 72 65 66 61 63 74 22 2c 20 20 20 20 20 20 0d  "refact",      .
3d0c7 0a 20 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65  .  "init_deferre
3d0c8 64 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20 22 63  d_pred_opt",  "c
3d0c9 6f 6e 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22  onslist",      "
3d0ca 74 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20  tcons",         
3d0cb 22 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0d  "idxlist",     .
3d0cc 0a 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  .  "defer_subcla
3d0cd 75 73 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f  use_opt",  "orco
3d0ce 6e 66 22 2c 20 20 20 20 20 20 20 20 22 72 65 73  nf",        "res
3d0cf 6f 6c 76 65 74 79 70 65 22 2c 20 20 20 22 72 61  olvetype",   "ra
3d0d0 69 73 65 74 79 70 65 22 2c 20 20 20 0d 0a 20 20  isetype",   ..  
3d0d1 22 69 66 65 78 69 73 74 73 22 2c 20 20 20 20 20  "ifexists",     
3d0d2 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20   "fullname",    
3d0d3 20 20 22 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20    "oneselect",  
3d0d4 20 20 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f     "multiselect_
3d0d5 6f 70 22 2c 0d 0a 20 20 22 64 69 73 74 69 6e 63  op",..  "distinc
3d0d6 74 22 2c 20 20 20 20 20 20 22 73 65 6c 63 6f 6c  t",      "selcol
3d0d7 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f 6d 22  list",    "from"
3d0d8 2c 20 20 20 20 20 20 20 20 20 20 22 77 68 65 72  ,          "wher
3d0d9 65 5f 6f 70 74 22 2c 20 20 20 0d 0a 20 20 22 67  e_opt",   ..  "g
3d0da 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20 20 22  roupby_opt",   "
3d0db 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20  having_opt",    
3d0dc 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20 20  "orderby_opt",  
3d0dd 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20 20   "limit_opt",   
3d0de 0d 0a 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20  ..  "sclp",     
3d0df 20 20 20 20 20 22 61 73 22 2c 20 20 20 20 20 20       "as",      
3d0e0 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73        "seltablis
3d0e1 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66  t",    "stl_pref
3d0e2 69 78 22 2c 20 20 0d 0a 20 20 22 6a 6f 69 6e 6f  ix",  ..  "joino
3d0e3 70 22 2c 20 20 20 20 20 20 20 20 22 69 6e 64 65  p",        "inde
3d0e4 78 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f  xed_opt",   "on_
3d0e5 6f 70 74 22 2c 20 20 20 20 20 20 20 20 22 75 73  opt",        "us
3d0e6 69 6e 67 5f 6f 70 74 22 2c 20 20 20 0d 0a 20 20  ing_opt",   ..  
3d0e7 22 6a 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20 20  "joinop2",      
3d0e8 20 22 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20   "inscollist",  
3d0e9 20 20 22 73 6f 72 74 6c 69 73 74 22 2c 20 20 20    "sortlist",   
3d0ea 20 20 20 22 73 6f 72 74 69 74 65 6d 22 2c 20 20     "sortitem",  
3d0eb 20 20 0d 0a 20 20 22 6e 65 78 70 72 6c 69 73 74    ..  "nexprlist
3d0ec 22 2c 20 20 20 20 20 22 73 65 74 6c 69 73 74 22  ",     "setlist"
3d0ed 2c 20 20 20 20 20 20 20 22 69 6e 73 65 72 74 5f  ,       "insert_
3d0ee 63 6d 64 22 2c 20 20 20 20 22 69 6e 73 63 6f 6c  cmd",    "inscol
3d0ef 6c 69 73 74 5f 6f 70 74 22 2c 0d 0a 20 20 22 69  list_opt",..  "i
3d0f0 74 65 6d 6c 69 73 74 22 2c 20 20 20 20 20 20 22  temlist",      "
3d0f1 65 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 20  exprlist",      
3d0f2 22 6c 69 6b 65 6f 70 22 2c 20 20 20 20 20 20 20  "likeop",       
3d0f3 20 22 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20   "between_op",  
3d0f4 0d 0a 20 20 22 69 6e 5f 6f 70 22 2c 20 20 20 20  ..  "in_op",    
3d0f5 20 20 20 20 20 22 63 61 73 65 5f 6f 70 65 72 61       "case_opera
3d0f6 6e 64 22 2c 20 20 22 63 61 73 65 5f 65 78 70 72  nd",  "case_expr
3d0f7 6c 69 73 74 22 2c 20 20 22 63 61 73 65 5f 65 6c  list",  "case_el
3d0f8 73 65 22 2c 20 20 20 0d 0a 20 20 22 75 6e 69 71  se",   ..  "uniq
3d0f9 75 65 66 6c 61 67 22 2c 20 20 20 20 22 63 6f 6c  ueflag",    "col
3d0fa 6c 61 74 65 22 2c 20 20 20 20 20 20 20 22 6e 6d  late",       "nm
3d0fb 6e 75 6d 22 2c 20 20 20 20 20 20 20 20 20 22 70  num",         "p
3d0fc 6c 75 73 5f 6f 70 74 22 2c 20 20 20 20 0d 0a 20  lus_opt",    .. 
3d0fd 20 22 6e 75 6d 62 65 72 22 2c 20 20 20 20 20 20   "number",      
3d0fe 20 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c 22    "trigger_decl"
3d0ff 2c 20 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f  ,  "trigger_cmd_
3d100 6c 69 73 74 22 2c 20 20 22 74 72 69 67 67 65 72  list",  "trigger
3d101 5f 74 69 6d 65 22 2c 0d 0a 20 20 22 74 72 69 67  _time",..  "trig
3d102 67 65 72 5f 65 76 65 6e 74 22 2c 20 20 22 66 6f  ger_event",  "fo
3d103 72 65 61 63 68 5f 63 6c 61 75 73 65 22 2c 20 20  reach_clause",  
3d104 22 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 20 20  "when_clause",  
3d105 20 22 74 72 69 67 67 65 72 5f 63 6d 64 22 2c 20   "trigger_cmd", 
3d106 0d 0a 20 20 22 74 72 6e 6d 22 2c 20 20 20 20 20  ..  "trnm",     
3d107 20 20 20 20 20 22 74 72 69 64 78 62 79 22 2c 20       "tridxby", 
3d108 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65 5f        "database_
3d109 6b 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f  kw_opt",  "key_o
3d10a 70 74 22 2c 20 20 20 20 20 0d 0a 20 20 22 61 64  pt",     ..  "ad
3d10b 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d  d_column_fullnam
3d10c 65 22 2c 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f  e",  "kwcolumn_o
3d10d 70 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76 74  pt",  "create_vt
3d10e 61 62 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c  ab",   "vtabargl
3d10f 69 73 74 22 2c 20 0d 0a 20 20 22 76 74 61 62 61  ist", ..  "vtaba
3d110 72 67 22 2c 20 20 20 20 20 20 20 22 76 74 61 62  rg",       "vtab
3d111 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22  argtoken",  "lp"
3d112 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 61 6e  ,            "an
3d113 79 6c 69 73 74 22 2c 20 20 20 20 20 0d 0a 7d 3b  ylist",     ..};
3d114 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  ..#endif /* NDEB
3d115 55 47 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66  UG */....#ifndef
3d116 20 4e 44 45 42 55 47 0d 0a 2f 2a 20 46 6f 72 20   NDEBUG../* For 
3d117 74 72 61 63 69 6e 67 20 72 65 64 75 63 65 20 61  tracing reduce a
3d118 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d 65  ctions, the name
3d119 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20 61  s of all rules a
3d11a 72 65 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a 2f  re required...*/
3d11b 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  ..static const c
3d11c 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c  har *const yyRul
3d11d 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0d 0a 20 2f 2a  eName[] = {.. /*
3d11e 20 20 20 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a     0 */ "input :
3d11f 3a 3d 20 63 6d 64 6c 69 73 74 22 2c 0d 0a 20 2f  := cmdlist",.. /
3d120 2a 20 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73  *   1 */ "cmdlis
3d121 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63  t ::= cmdlist ec
3d122 6d 64 22 2c 0d 0a 20 2f 2a 20 20 20 32 20 2a 2f  md",.. /*   2 */
3d123 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63   "cmdlist ::= ec
3d124 6d 64 22 2c 0d 0a 20 2f 2a 20 20 20 33 20 2a 2f  md",.. /*   3 */
3d125 20 22 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 22   "ecmd ::= SEMI"
3d126 2c 0d 0a 20 2f 2a 20 20 20 34 20 2a 2f 20 22 65  ,.. /*   4 */ "e
3d127 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20  cmd ::= explain 
3d128 63 6d 64 78 20 53 45 4d 49 22 2c 0d 0a 20 2f 2a  cmdx SEMI",.. /*
3d129 20 20 20 35 20 2a 2f 20 22 65 78 70 6c 61 69 6e     5 */ "explain
3d12a 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 20 20 36 20   ::=",.. /*   6 
3d12b 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20  */ "explain ::= 
3d12c 45 58 50 4c 41 49 4e 22 2c 0d 0a 20 2f 2a 20 20  EXPLAIN",.. /*  
3d12d 20 37 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a   7 */ "explain :
3d12e 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59  := EXPLAIN QUERY
3d12f 20 50 4c 41 4e 22 2c 0d 0a 20 2f 2a 20 20 20 38   PLAN",.. /*   8
3d130 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d   */ "cmdx ::= cm
3d131 64 22 2c 0d 0a 20 2f 2a 20 20 20 39 20 2a 2f 20  d",.. /*   9 */ 
3d132 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74  "cmd ::= BEGIN t
3d133 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f  ranstype trans_o
3d134 70 74 22 2c 0d 0a 20 2f 2a 20 20 31 30 20 2a 2f  pt",.. /*  10 */
3d135 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22   "trans_opt ::="
3d136 2c 0d 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22 74  ,.. /*  11 */ "t
3d137 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41  rans_opt ::= TRA
3d138 4e 53 41 43 54 49 4f 4e 22 2c 0d 0a 20 2f 2a 20  NSACTION",.. /* 
3d139 20 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70   12 */ "trans_op
3d13a 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f  t ::= TRANSACTIO
3d13b 4e 20 6e 6d 22 2c 0d 0a 20 2f 2a 20 20 31 33 20  N nm",.. /*  13 
3d13c 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
3d13d 3d 22 2c 0d 0a 20 2f 2a 20 20 31 34 20 2a 2f 20  =",.. /*  14 */ 
3d13e 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44  "transtype ::= D
3d13f 45 46 45 52 52 45 44 22 2c 0d 0a 20 2f 2a 20 20  EFERRED",.. /*  
3d140 31 35 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65  15 */ "transtype
3d141 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c   ::= IMMEDIATE",
3d142 0d 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72  .. /*  16 */ "tr
3d143 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c  anstype ::= EXCL
3d144 55 53 49 56 45 22 2c 0d 0a 20 2f 2a 20 20 31 37  USIVE",.. /*  17
3d145 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d   */ "cmd ::= COM
3d146 4d 49 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0d  MIT trans_opt",.
3d147 0a 20 2f 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64  . /*  18 */ "cmd
3d148 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f   ::= END trans_o
3d149 70 74 22 2c 0d 0a 20 2f 2a 20 20 31 39 20 2a 2f  pt",.. /*  19 */
3d14a 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41   "cmd ::= ROLLBA
3d14b 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0d 0a  CK trans_opt",..
3d14c 20 2f 2a 20 20 32 30 20 2a 2f 20 22 73 61 76 65   /*  20 */ "save
3d14d 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53 41  point_opt ::= SA
3d14e 56 45 50 4f 49 4e 54 22 2c 0d 0a 20 2f 2a 20 20  VEPOINT",.. /*  
3d14f 32 31 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74  21 */ "savepoint
3d150 5f 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20  _opt ::=",.. /* 
3d151 20 32 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20   22 */ "cmd ::= 
3d152 53 41 56 45 50 4f 49 4e 54 20 6e 6d 22 2c 0d 0a  SAVEPOINT nm",..
3d153 20 2f 2a 20 20 32 33 20 2a 2f 20 22 63 6d 64 20   /*  23 */ "cmd 
3d154 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65  ::= RELEASE save
3d155 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0d 0a  point_opt nm",..
3d156 20 2f 2a 20 20 32 34 20 2a 2f 20 22 63 6d 64 20   /*  24 */ "cmd 
3d157 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61  ::= ROLLBACK tra
3d158 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f  ns_opt TO savepo
3d159 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0d 0a 20 2f  int_opt nm",.. /
3d15a 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  25 */ "cmd ::
3d15b 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63  = create_table c
3d15c 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
3d15d 22 2c 0d 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22  ",.. /*  26 */ "
3d15e 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d  create_table ::=
3d15f 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54   createkw temp T
3d160 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73  ABLE ifnotexists
3d161 20 6e 6d 20 64 62 6e 6d 22 2c 0d 0a 20 2f 2a 20   nm dbnm",.. /* 
3d162 20 32 37 20 2a 2f 20 22 63 72 65 61 74 65 6b 77   27 */ "createkw
3d163 20 3a 3a 3d 20 43 52 45 41 54 45 22 2c 0d 0a 20   ::= CREATE",.. 
3d164 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74  /*  28 */ "ifnot
3d165 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0d 0a 20 2f  exists ::=",.. /
3d166 2a 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f 74 65  *  29 */ "ifnote
3d167 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54  xists ::= IF NOT
3d168 20 45 58 49 53 54 53 22 2c 0d 0a 20 2f 2a 20 20   EXISTS",.. /*  
3d169 33 30 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 20  30 */ "temp ::= 
3d16a 54 45 4d 50 22 2c 0d 0a 20 2f 2a 20 20 33 31 20  TEMP",.. /*  31 
3d16b 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 22 2c 0d 0a  */ "temp ::=",..
3d16c 20 2f 2a 20 20 33 32 20 2a 2f 20 22 63 72 65 61   /*  32 */ "crea
3d16d 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a  te_table_args ::
3d16e 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20  = LP columnlist 
3d16f 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 22  conslist_opt RP"
3d170 2c 0d 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22 63  ,.. /*  33 */ "c
3d171 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
3d172 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63 74 22 2c   ::= AS select",
3d173 0d 0a 20 2f 2a 20 20 33 34 20 2a 2f 20 22 63 6f  .. /*  34 */ "co
3d174 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c  lumnlist ::= col
3d175 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f  umnlist COMMA co
3d176 6c 75 6d 6e 22 2c 0d 0a 20 2f 2a 20 20 33 35 20  lumn",.. /*  35 
3d177 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a  */ "columnlist :
3d178 3a 3d 20 63 6f 6c 75 6d 6e 22 2c 0d 0a 20 2f 2a  := column",.. /*
3d179 20 20 33 36 20 2a 2f 20 22 63 6f 6c 75 6d 6e 20    36 */ "column 
3d17a 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70  ::= columnid typ
3d17b 65 20 63 61 72 67 6c 69 73 74 22 2c 0d 0a 20 2f  e carglist",.. /
3d17c 2a 20 20 33 37 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  37 */ "column
3d17d 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0d 0a 20 2f 2a  id ::= nm",.. /*
3d17e 20 20 33 38 20 2a 2f 20 22 69 64 20 3a 3a 3d 20    38 */ "id ::= 
3d17f 49 44 22 2c 0d 0a 20 2f 2a 20 20 33 39 20 2a 2f  ID",.. /*  39 */
3d180 20 22 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44   "id ::= INDEXED
3d181 22 2c 0d 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22  ",.. /*  40 */ "
3d182 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e  ids ::= ID|STRIN
3d183 47 22 2c 0d 0a 20 2f 2a 20 20 34 31 20 2a 2f 20  G",.. /*  41 */ 
3d184 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0d 0a 20 2f  "nm ::= id",.. /
3d185 2a 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a 3a 3d  *  42 */ "nm ::=
3d186 20 53 54 52 49 4e 47 22 2c 0d 0a 20 2f 2a 20 20   STRING",.. /*  
3d187 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f  43 */ "nm ::= JO
3d188 49 4e 5f 4b 57 22 2c 0d 0a 20 2f 2a 20 20 34 34  IN_KW",.. /*  44
3d189 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0d   */ "type ::=",.
3d18a 0a 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74 79 70  . /*  45 */ "typ
3d18b 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22  e ::= typetoken"
3d18c 2c 0d 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 74  ,.. /*  46 */ "t
3d18d 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
3d18e 65 6e 61 6d 65 22 2c 0d 0a 20 2f 2a 20 20 34 37  ename",.. /*  47
3d18f 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a   */ "typetoken :
3d190 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
3d191 69 67 6e 65 64 20 52 50 22 2c 0d 0a 20 2f 2a 20  igned RP",.. /* 
3d192 20 34 38 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65   48 */ "typetoke
3d193 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c  n ::= typename L
3d194 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73  P signed COMMA s
3d195 69 67 6e 65 64 20 52 50 22 2c 0d 0a 20 2f 2a 20  igned RP",.. /* 
3d196 20 34 39 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65   49 */ "typename
3d197 20 3a 3a 3d 20 69 64 73 22 2c 0d 0a 20 2f 2a 20   ::= ids",.. /* 
3d198 20 35 30 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65   50 */ "typename
3d199 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64   ::= typename id
3d19a 73 22 2c 0d 0a 20 2f 2a 20 20 35 31 20 2a 2f 20  s",.. /*  51 */ 
3d19b 22 73 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73  "signed ::= plus
3d19c 5f 6e 75 6d 22 2c 0d 0a 20 2f 2a 20 20 35 32 20  _num",.. /*  52 
3d19d 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d 20 6d  */ "signed ::= m
3d19e 69 6e 75 73 5f 6e 75 6d 22 2c 0d 0a 20 2f 2a 20  inus_num",.. /* 
3d19f 20 35 33 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   53 */ "carglist
3d1a0 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20 63 61   ::= carglist ca
3d1a1 72 67 22 2c 0d 0a 20 2f 2a 20 20 35 34 20 2a 2f  rg",.. /*  54 */
3d1a2 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 22 2c   "carglist ::=",
3d1a3 0d 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63 61  .. /*  55 */ "ca
3d1a4 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  rg ::= CONSTRAIN
3d1a5 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0d 0a 20 2f  T nm ccons",.. /
3d1a6 2a 20 20 35 36 20 2a 2f 20 22 63 61 72 67 20 3a  *  56 */ "carg :
3d1a7 3a 3d 20 63 63 6f 6e 73 22 2c 0d 0a 20 2f 2a 20  := ccons",.. /* 
3d1a8 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   57 */ "ccons ::
3d1a9 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 22 2c  = DEFAULT term",
3d1aa 0d 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63  .. /*  58 */ "cc
3d1ab 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
3d1ac 4c 50 20 65 78 70 72 20 52 50 22 2c 0d 0a 20 2f  LP expr RP",.. /
3d1ad 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73 20  *  59 */ "ccons 
3d1ae 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53  ::= DEFAULT PLUS
3d1af 20 74 65 72 6d 22 2c 0d 0a 20 2f 2a 20 20 36 30   term",.. /*  60
3d1b0 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
3d1b1 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
3d1b2 6d 22 2c 0d 0a 20 2f 2a 20 20 36 31 20 2a 2f 20  m",.. /*  61 */ 
3d1b3 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
3d1b4 4c 54 20 69 64 22 2c 0d 0a 20 2f 2a 20 20 36 32  LT id",.. /*  62
3d1b5 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e   */ "ccons ::= N
3d1b6 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0d 0a 20 2f  ULL onconf",.. /
3d1b7 2a 20 20 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20  *  63 */ "ccons 
3d1b8 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63  ::= NOT NULL onc
3d1b9 6f 6e 66 22 2c 0d 0a 20 2f 2a 20 20 36 34 20 2a  onf",.. /*  64 *
3d1ba 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49  / "ccons ::= PRI
3d1bb 4d 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64  MARY KEY sortord
3d1bc 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e  er onconf autoin
3d1bd 63 22 2c 0d 0a 20 2f 2a 20 20 36 35 20 2a 2f 20  c",.. /*  65 */ 
3d1be 22 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55  "ccons ::= UNIQU
3d1bf 45 20 6f 6e 63 6f 6e 66 22 2c 0d 0a 20 2f 2a 20  E onconf",.. /* 
3d1c0 20 36 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   66 */ "ccons ::
3d1c1 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20  = CHECK LP expr 
3d1c2 52 50 22 2c 0d 0a 20 2f 2a 20 20 36 37 20 2a 2f  RP",.. /*  67 */
3d1c3 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45   "ccons ::= REFE
3d1c4 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
3d1c5 74 5f 6f 70 74 20 72 65 66 61 72 67 73 22 2c 0d  t_opt refargs",.
3d1c6 0a 20 2f 2a 20 20 36 38 20 2a 2f 20 22 63 63 6f  . /*  68 */ "cco
3d1c7 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62  ns ::= defer_sub
3d1c8 63 6c 61 75 73 65 22 2c 0d 0a 20 2f 2a 20 20 36  clause",.. /*  6
3d1c9 39 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  9 */ "ccons ::= 
3d1ca 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0d 0a 20  COLLATE ids",.. 
3d1cb 2f 2a 20 20 37 30 20 2a 2f 20 22 61 75 74 6f 69  /*  70 */ "autoi
3d1cc 6e 63 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 20 37  nc ::=",.. /*  7
3d1cd 31 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a  1 */ "autoinc ::
3d1ce 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0d 0a 20 2f  = AUTOINCR",.. /
3d1cf 2a 20 20 37 32 20 2a 2f 20 22 72 65 66 61 72 67  *  72 */ "refarg
3d1d0 73 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 20 37 33  s ::=",.. /*  73
3d1d1 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d   */ "refargs ::=
3d1d2 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67 22   refargs refarg"
3d1d3 2c 0d 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22 72  ,.. /*  74 */ "r
3d1d4 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20  efarg ::= MATCH 
3d1d5 6e 6d 22 2c 0d 0a 20 2f 2a 20 20 37 35 20 2a 2f  nm",.. /*  75 */
3d1d6 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
3d1d7 49 4e 53 45 52 54 20 72 65 66 61 63 74 22 2c 0d  INSERT refact",.
3d1d8 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72 65 66  . /*  76 */ "ref
3d1d9 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54  arg ::= ON DELET
3d1da 45 20 72 65 66 61 63 74 22 2c 0d 0a 20 2f 2a 20  E refact",.. /* 
3d1db 20 37 37 20 2a 2f 20 22 72 65 66 61 72 67 20 3a   77 */ "refarg :
3d1dc 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66  := ON UPDATE ref
3d1dd 61 63 74 22 2c 0d 0a 20 2f 2a 20 20 37 38 20 2a  act",.. /*  78 *
3d1de 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  / "refact ::= SE
3d1df 54 20 4e 55 4c 4c 22 2c 0d 0a 20 2f 2a 20 20 37  T NULL",.. /*  7
3d1e0 39 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d  9 */ "refact ::=
3d1e1 20 53 45 54 20 44 45 46 41 55 4c 54 22 2c 0d 0a   SET DEFAULT",..
3d1e2 20 2f 2a 20 20 38 30 20 2a 2f 20 22 72 65 66 61   /*  80 */ "refa
3d1e3 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c  ct ::= CASCADE",
3d1e4 0d 0a 20 2f 2a 20 20 38 31 20 2a 2f 20 22 72 65  .. /*  81 */ "re
3d1e5 66 61 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43  fact ::= RESTRIC
3d1e6 54 22 2c 0d 0a 20 2f 2a 20 20 38 32 20 2a 2f 20  T",.. /*  82 */ 
3d1e7 22 72 65 66 61 63 74 20 3a 3a 3d 20 4e 4f 20 41  "refact ::= NO A
3d1e8 43 54 49 4f 4e 22 2c 0d 0a 20 2f 2a 20 20 38 33  CTION",.. /*  83
3d1e9 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
3d1ea 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46  ause ::= NOT DEF
3d1eb 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66  ERRABLE init_def
3d1ec 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
3d1ed 0d 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 64 65  .. /*  84 */ "de
3d1ee 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
3d1ef 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
3d1f0 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
3d1f1 6f 70 74 22 2c 0d 0a 20 2f 2a 20 20 38 35 20 2a  opt",.. /*  85 *
3d1f2 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64  / "init_deferred
3d1f3 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0d  _pred_opt ::=",.
3d1f4 0a 20 2f 2a 20 20 38 36 20 2a 2f 20 22 69 6e 69  . /*  86 */ "ini
3d1f5 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
3d1f6 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c  opt ::= INITIALL
3d1f7 59 20 44 45 46 45 52 52 45 44 22 2c 0d 0a 20 2f  Y DEFERRED",.. /
3d1f8 2a 20 20 38 37 20 2a 2f 20 22 69 6e 69 74 5f 64  *  87 */ "init_d
3d1f9 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
3d1fa 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49   ::= INITIALLY I
3d1fb 4d 4d 45 44 49 41 54 45 22 2c 0d 0a 20 2f 2a 20  MMEDIATE",.. /* 
3d1fc 20 38 38 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   88 */ "conslist
3d1fd 5f 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20  _opt ::=",.. /* 
3d1fe 20 38 39 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   89 */ "conslist
3d1ff 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63  _opt ::= COMMA c
3d200 6f 6e 73 6c 69 73 74 22 2c 0d 0a 20 2f 2a 20 20  onslist",.. /*  
3d201 39 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  90 */ "conslist 
3d202 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d  ::= conslist COM
3d203 4d 41 20 74 63 6f 6e 73 22 2c 0d 0a 20 2f 2a 20  MA tcons",.. /* 
3d204 20 39 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   91 */ "conslist
3d205 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63   ::= conslist tc
3d206 6f 6e 73 22 2c 0d 0a 20 2f 2a 20 20 39 32 20 2a  ons",.. /*  92 *
3d207 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  / "conslist ::= 
3d208 74 63 6f 6e 73 22 2c 0d 0a 20 2f 2a 20 20 39 33  tcons",.. /*  93
3d209 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43   */ "tcons ::= C
3d20a 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 22 2c 0d 0a  ONSTRAINT nm",..
3d20b 20 2f 2a 20 20 39 34 20 2a 2f 20 22 74 63 6f 6e   /*  94 */ "tcon
3d20c 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45  s ::= PRIMARY KE
3d20d 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74  Y LP idxlist aut
3d20e 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 22 2c  oinc RP onconf",
3d20f 0d 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 74 63  .. /*  95 */ "tc
3d210 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c  ons ::= UNIQUE L
3d211 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e 63  P idxlist RP onc
3d212 6f 6e 66 22 2c 0d 0a 20 2f 2a 20 20 39 36 20 2a  onf",.. /*  96 *
3d213 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  / "tcons ::= CHE
3d214 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e  CK LP expr RP on
3d215 63 6f 6e 66 22 2c 0d 0a 20 2f 2a 20 20 39 37 20  conf",.. /*  97 
3d216 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f  */ "tcons ::= FO
3d217 52 45 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78  REIGN KEY LP idx
3d218 6c 69 73 74 20 52 50 20 52 45 46 45 52 45 4e 43  list RP REFERENC
3d219 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70  ES nm idxlist_op
3d21a 74 20 72 65 66 61 72 67 73 20 64 65 66 65 72 5f  t refargs defer_
3d21b 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0d  subclause_opt",.
3d21c 0a 20 2f 2a 20 20 39 38 20 2a 2f 20 22 64 65 66  . /*  98 */ "def
3d21d 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
3d21e 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 20 39 39 20   ::=",.. /*  99 
3d21f 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
3d220 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65  use_opt ::= defe
3d221 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0d 0a 20  r_subclause",.. 
3d222 2f 2a 20 31 30 30 20 2a 2f 20 22 6f 6e 63 6f 6e  /* 100 */ "oncon
3d223 66 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 30 31  f ::=",.. /* 101
3d224 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20   */ "onconf ::= 
3d225 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f  ON CONFLICT reso
3d226 6c 76 65 74 79 70 65 22 2c 0d 0a 20 2f 2a 20 31  lvetype",.. /* 1
3d227 30 32 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a  02 */ "orconf ::
3d228 3d 22 2c 0d 0a 20 2f 2a 20 31 30 33 20 2a 2f 20  =",.. /* 103 */ 
3d229 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72  "orconf ::= OR r
3d22a 65 73 6f 6c 76 65 74 79 70 65 22 2c 0d 0a 20 2f  esolvetype",.. /
3d22b 2a 20 31 30 34 20 2a 2f 20 22 72 65 73 6f 6c 76  * 104 */ "resolv
3d22c 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74  etype ::= raiset
3d22d 79 70 65 22 2c 0d 0a 20 2f 2a 20 31 30 35 20 2a  ype",.. /* 105 *
3d22e 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  / "resolvetype :
3d22f 3a 3d 20 49 47 4e 4f 52 45 22 2c 0d 0a 20 2f 2a  := IGNORE",.. /*
3d230 20 31 30 36 20 2a 2f 20 22 72 65 73 6f 6c 76 65   106 */ "resolve
3d231 74 79 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45  type ::= REPLACE
3d232 22 2c 0d 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22  ",.. /* 107 */ "
3d233 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42  cmd ::= DROP TAB
3d234 4c 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  LE ifexists full
3d235 6e 61 6d 65 22 2c 0d 0a 20 2f 2a 20 31 30 38 20  name",.. /* 108 
3d236 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d  */ "ifexists ::=
3d237 20 49 46 20 45 58 49 53 54 53 22 2c 0d 0a 20 2f   IF EXISTS",.. /
3d238 2a 20 31 30 39 20 2a 2f 20 22 69 66 65 78 69 73  * 109 */ "ifexis
3d239 74 73 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 31  ts ::=",.. /* 11
3d23a 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  0 */ "cmd ::= cr
3d23b 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49 45 57  eatekw temp VIEW
3d23c 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
3d23d 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 22 2c  dbnm AS select",
3d23e 0d 0a 20 2f 2a 20 31 31 31 20 2a 2f 20 22 63 6d  .. /* 111 */ "cm
3d23f 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20  d ::= DROP VIEW 
3d240 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
3d241 65 22 2c 0d 0a 20 2f 2a 20 31 31 32 20 2a 2f 20  e",.. /* 112 */ 
3d242 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22  "cmd ::= select"
3d243 2c 0d 0a 20 2f 2a 20 31 31 33 20 2a 2f 20 22 73  ,.. /* 113 */ "s
3d244 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c  elect ::= onesel
3d245 65 63 74 22 2c 0d 0a 20 2f 2a 20 31 31 34 20 2a  ect",.. /* 114 *
3d246 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65  / "select ::= se
3d247 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74  lect multiselect
3d248 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0d  _op oneselect",.
3d249 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22 6d 75 6c  . /* 115 */ "mul
3d24a 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
3d24b 55 4e 49 4f 4e 22 2c 0d 0a 20 2f 2a 20 31 31 36  UNION",.. /* 116
3d24c 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74   */ "multiselect
3d24d 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c  _op ::= UNION AL
3d24e 4c 22 2c 0d 0a 20 2f 2a 20 31 31 37 20 2a 2f 20  L",.. /* 117 */ 
3d24f 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20  "multiselect_op 
3d250 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52  ::= EXCEPT|INTER
3d251 53 45 43 54 22 2c 0d 0a 20 2f 2a 20 31 31 38 20  SECT",.. /* 118 
3d252 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a  */ "oneselect ::
3d253 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63  = SELECT distinc
3d254 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f  t selcollist fro
3d255 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75  m where_opt grou
3d256 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f  pby_opt having_o
3d257 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c  pt orderby_opt l
3d258 69 6d 69 74 5f 6f 70 74 22 2c 0d 0a 20 2f 2a 20  imit_opt",.. /* 
3d259 31 31 39 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  119 */ "distinct
3d25a 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0d   ::= DISTINCT",.
3d25b 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 64 69 73  . /* 120 */ "dis
3d25c 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0d  tinct ::= ALL",.
3d25d 0a 20 2f 2a 20 31 32 31 20 2a 2f 20 22 64 69 73  . /* 121 */ "dis
3d25e 74 69 6e 63 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a  tinct ::=",.. /*
3d25f 20 31 32 32 20 2a 2f 20 22 73 63 6c 70 20 3a 3a   122 */ "sclp ::
3d260 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d  = selcollist COM
3d261 4d 41 22 2c 0d 0a 20 2f 2a 20 31 32 33 20 2a 2f  MA",.. /* 123 */
3d262 20 22 73 63 6c 70 20 3a 3a 3d 22 2c 0d 0a 20 2f   "sclp ::=",.. /
3d263 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c 63 6f 6c  * 124 */ "selcol
3d264 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78  list ::= sclp ex
3d265 70 72 20 61 73 22 2c 0d 0a 20 2f 2a 20 31 32 35  pr as",.. /* 125
3d266 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
3d267 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 22 2c 0d  ::= sclp STAR",.
3d268 0a 20 2f 2a 20 31 32 36 20 2a 2f 20 22 73 65 6c  . /* 126 */ "sel
3d269 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70  collist ::= sclp
3d26a 20 6e 6d 20 44 4f 54 20 53 54 41 52 22 2c 0d 0a   nm DOT STAR",..
3d26b 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73 20 3a   /* 127 */ "as :
3d26c 3a 3d 20 41 53 20 6e 6d 22 2c 0d 0a 20 2f 2a 20  := AS nm",.. /* 
3d26d 31 32 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 69  128 */ "as ::= i
3d26e 64 73 22 2c 0d 0a 20 2f 2a 20 31 32 39 20 2a 2f  ds",.. /* 129 */
3d26f 20 22 61 73 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20   "as ::=",.. /* 
3d270 31 33 30 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d  130 */ "from ::=
3d271 22 2c 0d 0a 20 2f 2a 20 31 33 31 20 2a 2f 20 22  ",.. /* 131 */ "
3d272 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65  from ::= FROM se
3d273 6c 74 61 62 6c 69 73 74 22 2c 0d 0a 20 2f 2a 20  ltablist",.. /* 
3d274 31 33 32 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66  132 */ "stl_pref
3d275 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73  ix ::= seltablis
3d276 74 20 6a 6f 69 6e 6f 70 22 2c 0d 0a 20 2f 2a 20  t joinop",.. /* 
3d277 31 33 33 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66  133 */ "stl_pref
3d278 69 78 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 33  ix ::=",.. /* 13
3d279 34 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  4 */ "seltablist
3d27a 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
3d27b 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78  nm dbnm as index
3d27c 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73  ed_opt on_opt us
3d27d 69 6e 67 5f 6f 70 74 22 2c 0d 0a 20 2f 2a 20 31  ing_opt",.. /* 1
3d27e 33 35 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  35 */ "seltablis
3d27f 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
3d280 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73   LP select RP as
3d281 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
3d282 74 22 2c 0d 0a 20 2f 2a 20 31 33 36 20 2a 2f 20  t",.. /* 136 */ 
3d283 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20  "seltablist ::= 
3d284 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
3d285 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f  ltablist RP as o
3d286 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22  n_opt using_opt"
3d287 2c 0d 0a 20 2f 2a 20 31 33 37 20 2a 2f 20 22 64  ,.. /* 137 */ "d
3d288 62 6e 6d 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31  bnm ::=",.. /* 1
3d289 33 38 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20  38 */ "dbnm ::= 
3d28a 44 4f 54 20 6e 6d 22 2c 0d 0a 20 2f 2a 20 31 33  DOT nm",.. /* 13
3d28b 39 20 2a 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a  9 */ "fullname :
3d28c 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c 0d 0a 20 2f  := nm dbnm",.. /
3d28d 2a 20 31 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 140 */ "joinop
3d28e 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22   ::= COMMA|JOIN"
3d28f 2c 0d 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22 6a  ,.. /* 141 */ "j
3d290 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
3d291 57 20 4a 4f 49 4e 22 2c 0d 0a 20 2f 2a 20 31 34  W JOIN",.. /* 14
3d292 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  2 */ "joinop ::=
3d293 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e   JOIN_KW nm JOIN
3d294 22 2c 0d 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22  ",.. /* 143 */ "
3d295 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
3d296 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0d  KW nm nm JOIN",.
3d297 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22 6f 6e 5f  . /* 144 */ "on_
3d298 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22  opt ::= ON expr"
3d299 2c 0d 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 6f  ,.. /* 145 */ "o
3d29a 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a  n_opt ::=",.. /*
3d29b 20 31 34 36 20 2a 2f 20 22 69 6e 64 65 78 65 64   146 */ "indexed
3d29c 5f 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20  _opt ::=",.. /* 
3d29d 31 34 37 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f  147 */ "indexed_
3d29e 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20  opt ::= INDEXED 
3d29f 42 59 20 6e 6d 22 2c 0d 0a 20 2f 2a 20 31 34 38  BY nm",.. /* 148
3d2a0 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74   */ "indexed_opt
3d2a1 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44   ::= NOT INDEXED
3d2a2 22 2c 0d 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22  ",.. /* 149 */ "
3d2a3 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53  using_opt ::= US
3d2a4 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ING LP inscollis
3d2a5 74 20 52 50 22 2c 0d 0a 20 2f 2a 20 31 35 30 20  t RP",.. /* 150 
3d2a6 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a  */ "using_opt ::
3d2a7 3d 22 2c 0d 0a 20 2f 2a 20 31 35 31 20 2a 2f 20  =",.. /* 151 */ 
3d2a8 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d  "orderby_opt ::=
3d2a9 22 2c 0d 0a 20 2f 2a 20 31 35 32 20 2a 2f 20 22  ",.. /* 152 */ "
3d2aa 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20  orderby_opt ::= 
3d2ab 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73  ORDER BY sortlis
3d2ac 74 22 2c 0d 0a 20 2f 2a 20 31 35 33 20 2a 2f 20  t",.. /* 153 */ 
3d2ad 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f  "sortlist ::= so
3d2ae 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72  rtlist COMMA sor
3d2af 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22  titem sortorder"
3d2b0 2c 0d 0a 20 2f 2a 20 31 35 34 20 2a 2f 20 22 73  ,.. /* 154 */ "s
3d2b1 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74  ortlist ::= sort
3d2b2 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c  item sortorder",
3d2b3 0d 0a 20 2f 2a 20 31 35 35 20 2a 2f 20 22 73 6f  .. /* 155 */ "so
3d2b4 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22  rtitem ::= expr"
3d2b5 2c 0d 0a 20 2f 2a 20 31 35 36 20 2a 2f 20 22 73  ,.. /* 156 */ "s
3d2b6 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43  ortorder ::= ASC
3d2b7 22 2c 0d 0a 20 2f 2a 20 31 35 37 20 2a 2f 20 22  ",.. /* 157 */ "
3d2b8 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45  sortorder ::= DE
3d2b9 53 43 22 2c 0d 0a 20 2f 2a 20 31 35 38 20 2a 2f  SC",.. /* 158 */
3d2ba 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 22   "sortorder ::="
3d2bb 2c 0d 0a 20 2f 2a 20 31 35 39 20 2a 2f 20 22 67  ,.. /* 159 */ "g
3d2bc 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c  roupby_opt ::=",
3d2bd 0d 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22 67 72  .. /* 160 */ "gr
3d2be 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52  oupby_opt ::= GR
3d2bf 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74  OUP BY nexprlist
3d2c0 22 2c 0d 0a 20 2f 2a 20 31 36 31 20 2a 2f 20 22  ",.. /* 161 */ "
3d2c1 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c  having_opt ::=",
3d2c2 0d 0a 20 2f 2a 20 31 36 32 20 2a 2f 20 22 68 61  .. /* 162 */ "ha
3d2c3 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56  ving_opt ::= HAV
3d2c4 49 4e 47 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20  ING expr",.. /* 
3d2c5 31 36 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  163 */ "limit_op
3d2c6 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 36 34  t ::=",.. /* 164
3d2c7 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a   */ "limit_opt :
3d2c8 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 22 2c 0d  := LIMIT expr",.
3d2c9 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22 6c 69 6d  . /* 165 */ "lim
3d2ca 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
3d2cb 20 65 78 70 72 20 4f 46 46 53 45 54 20 65 78 70   expr OFFSET exp
3d2cc 72 22 2c 0d 0a 20 2f 2a 20 31 36 36 20 2a 2f 20  r",.. /* 166 */ 
3d2cd 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c  "limit_opt ::= L
3d2ce 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
3d2cf 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 31 36 37 20  expr",.. /* 167 
3d2d0 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  */ "cmd ::= DELE
3d2d1 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65  TE FROM fullname
3d2d2 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65   indexed_opt whe
3d2d3 72 65 5f 6f 70 74 22 2c 0d 0a 20 2f 2a 20 31 36  re_opt",.. /* 16
3d2d4 38 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  8 */ "where_opt 
3d2d5 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 36 39 20 2a  ::=",.. /* 169 *
3d2d6 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  / "where_opt ::=
3d2d7 20 57 48 45 52 45 20 65 78 70 72 22 2c 0d 0a 20   WHERE expr",.. 
3d2d8 2f 2a 20 31 37 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 170 */ "cmd :
3d2d9 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66  := UPDATE orconf
3d2da 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65   fullname indexe
3d2db 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73  d_opt SET setlis
3d2dc 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0d 0a 20  t where_opt",.. 
3d2dd 2f 2a 20 31 37 31 20 2a 2f 20 22 73 65 74 6c 69  /* 171 */ "setli
3d2de 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43  st ::= setlist C
3d2df 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 22  OMMA nm EQ expr"
3d2e0 2c 0d 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 73  ,.. /* 172 */ "s
3d2e1 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51  etlist ::= nm EQ
3d2e2 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 31 37 33   expr",.. /* 173
3d2e3 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73   */ "cmd ::= ins
3d2e4 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
3d2e5 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
3d2e6 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
3d2e7 74 65 6d 6c 69 73 74 20 52 50 22 2c 0d 0a 20 2f  temlist RP",.. /
3d2e8 2a 20 31 37 34 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 174 */ "cmd ::
3d2e9 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
3d2ea 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
3d2eb 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74  llist_opt select
3d2ec 22 2c 0d 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22  ",.. /* 175 */ "
3d2ed 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
3d2ee 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
3d2ef 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
3d2f0 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 22 2c  DEFAULT VALUES",
3d2f1 0d 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e  .. /* 176 */ "in
3d2f2 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53  sert_cmd ::= INS
3d2f3 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0d 0a 20 2f  ERT orconf",.. /
3d2f4 2a 20 31 37 37 20 2a 2f 20 22 69 6e 73 65 72 74  * 177 */ "insert
3d2f5 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43 45  _cmd ::= REPLACE
3d2f6 22 2c 0d 0a 20 2f 2a 20 31 37 38 20 2a 2f 20 22  ",.. /* 178 */ "
3d2f7 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65  itemlist ::= ite
3d2f8 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72  mlist COMMA expr
3d2f9 22 2c 0d 0a 20 2f 2a 20 31 37 39 20 2a 2f 20 22  ",.. /* 179 */ "
3d2fa 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70  itemlist ::= exp
3d2fb 72 22 2c 0d 0a 20 2f 2a 20 31 38 30 20 2a 2f 20  r",.. /* 180 */ 
3d2fc 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20  "inscollist_opt 
3d2fd 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 31 38 31 20 2a  ::=",.. /* 181 *
3d2fe 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  / "inscollist_op
3d2ff 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c  t ::= LP inscoll
3d300 69 73 74 20 52 50 22 2c 0d 0a 20 2f 2a 20 31 38  ist RP",.. /* 18
3d301 32 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  2 */ "inscollist
3d302 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20   ::= inscollist 
3d303 43 4f 4d 4d 41 20 6e 6d 22 2c 0d 0a 20 2f 2a 20  COMMA nm",.. /* 
3d304 31 38 33 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  183 */ "inscolli
3d305 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0d 0a 20 2f 2a  st ::= nm",.. /*
3d306 20 31 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   184 */ "expr ::
3d307 3d 20 74 65 72 6d 22 2c 0d 0a 20 2f 2a 20 31 38  = term",.. /* 18
3d308 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c  5 */ "expr ::= L
3d309 50 20 65 78 70 72 20 52 50 22 2c 0d 0a 20 2f 2a  P expr RP",.. /*
3d30a 20 31 38 36 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   186 */ "term ::
3d30b 3d 20 4e 55 4c 4c 22 2c 0d 0a 20 2f 2a 20 31 38  = NULL",.. /* 18
3d30c 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 69  7 */ "expr ::= i
3d30d 64 22 2c 0d 0a 20 2f 2a 20 31 38 38 20 2a 2f 20  d",.. /* 188 */ 
3d30e 22 65 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  "expr ::= JOIN_K
3d30f 57 22 2c 0d 0a 20 2f 2a 20 31 38 39 20 2a 2f 20  W",.. /* 189 */ 
3d310 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54  "expr ::= nm DOT
3d311 20 6e 6d 22 2c 0d 0a 20 2f 2a 20 31 39 30 20 2a   nm",.. /* 190 *
3d312 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44  / "expr ::= nm D
3d313 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0d 0a  OT nm DOT nm",..
3d314 20 2f 2a 20 31 39 31 20 2a 2f 20 22 74 65 72 6d   /* 191 */ "term
3d315 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
3d316 41 54 7c 42 4c 4f 42 22 2c 0d 0a 20 2f 2a 20 31  AT|BLOB",.. /* 1
3d317 39 32 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  92 */ "term ::= 
3d318 53 54 52 49 4e 47 22 2c 0d 0a 20 2f 2a 20 31 39  STRING",.. /* 19
3d319 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52  3 */ "expr ::= R
3d31a 45 47 49 53 54 45 52 22 2c 0d 0a 20 2f 2a 20 31  EGISTER",.. /* 1
3d31b 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  94 */ "expr ::= 
3d31c 56 41 52 49 41 42 4c 45 22 2c 0d 0a 20 2f 2a 20  VARIABLE",.. /* 
3d31d 31 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  195 */ "expr ::=
3d31e 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
3d31f 73 22 2c 0d 0a 20 2f 2a 20 31 39 36 20 2a 2f 20  s",.. /* 196 */ 
3d320 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c  "expr ::= CAST L
3d321 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f  P expr AS typeto
3d322 6b 65 6e 20 52 50 22 2c 0d 0a 20 2f 2a 20 31 39  ken RP",.. /* 19
3d323 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49  7 */ "expr ::= I
3d324 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78  D LP distinct ex
3d325 70 72 6c 69 73 74 20 52 50 22 2c 0d 0a 20 2f 2a  prlist RP",.. /*
3d326 20 31 39 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   198 */ "expr ::
3d327 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 22  = ID LP STAR RP"
3d328 2c 0d 0a 20 2f 2a 20 31 39 39 20 2a 2f 20 22 74  ,.. /* 199 */ "t
3d329 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57  erm ::= CTIME_KW
3d32a 22 2c 0d 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22  ",.. /* 200 */ "
3d32b 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e  expr ::= expr AN
3d32c 44 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 30  D expr",.. /* 20
3d32d 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  1 */ "expr ::= e
3d32e 78 70 72 20 4f 52 20 65 78 70 72 22 2c 0d 0a 20  xpr OR expr",.. 
3d32f 2f 2a 20 32 30 32 20 2a 2f 20 22 65 78 70 72 20  /* 202 */ "expr 
3d330 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47  ::= expr LT|GT|G
3d331 45 7c 4c 45 20 65 78 70 72 22 2c 0d 0a 20 2f 2a  E|LE expr",.. /*
3d332 20 32 30 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a   203 */ "expr ::
3d333 3d 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78 70  = expr EQ|NE exp
3d334 72 22 2c 0d 0a 20 2f 2a 20 32 30 34 20 2a 2f 20  r",.. /* 204 */ 
3d335 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42  "expr ::= expr B
3d336 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49  ITAND|BITOR|LSHI
3d337 46 54 7c 52 53 48 49 46 54 20 65 78 70 72 22 2c  FT|RSHIFT expr",
3d338 0d 0a 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65 78  .. /* 205 */ "ex
3d339 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53  pr ::= expr PLUS
3d33a 7c 4d 49 4e 55 53 20 65 78 70 72 22 2c 0d 0a 20  |MINUS expr",.. 
3d33b 2f 2a 20 32 30 36 20 2a 2f 20 22 65 78 70 72 20  /* 206 */ "expr 
3d33c 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c  ::= expr STAR|SL
3d33d 41 53 48 7c 52 45 4d 20 65 78 70 72 22 2c 0d 0a  ASH|REM expr",..
3d33e 20 2f 2a 20 32 30 37 20 2a 2f 20 22 65 78 70 72   /* 207 */ "expr
3d33f 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
3d340 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 30 38   expr",.. /* 208
3d341 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
3d342 4c 49 4b 45 5f 4b 57 22 2c 0d 0a 20 2f 2a 20 32  LIKE_KW",.. /* 2
3d343 30 39 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  09 */ "likeop ::
3d344 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c 0d  = NOT LIKE_KW",.
3d345 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 6c 69 6b  . /* 210 */ "lik
3d346 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0d  eop ::= MATCH",.
3d347 0a 20 2f 2a 20 32 31 31 20 2a 2f 20 22 6c 69 6b  . /* 211 */ "lik
3d348 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43  eop ::= NOT MATC
3d349 48 22 2c 0d 0a 20 2f 2a 20 32 31 32 20 2a 2f 20  H",.. /* 212 */ 
3d34a 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c  "expr ::= expr l
3d34b 69 6b 65 6f 70 20 65 78 70 72 22 2c 0d 0a 20 2f  ikeop expr",.. /
3d34c 2a 20 32 31 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 213 */ "expr :
3d34d 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20 65  := expr likeop e
3d34e 78 70 72 20 45 53 43 41 50 45 20 65 78 70 72 22  xpr ESCAPE expr"
3d34f 2c 0d 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65  ,.. /* 214 */ "e
3d350 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e  xpr ::= expr ISN
3d351 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0d 0a 20  ULL|NOTNULL",.. 
3d352 2f 2a 20 32 31 35 20 2a 2f 20 22 65 78 70 72 20  /* 215 */ "expr 
3d353 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c  ::= expr NOT NUL
3d354 4c 22 2c 0d 0a 20 2f 2a 20 32 31 36 20 2a 2f 20  L",.. /* 216 */ 
3d355 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
3d356 53 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 31  S expr",.. /* 21
3d357 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  7 */ "expr ::= e
3d358 78 70 72 20 49 53 20 4e 4f 54 20 65 78 70 72 22  xpr IS NOT expr"
3d359 2c 0d 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22 65  ,.. /* 218 */ "e
3d35a 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72  xpr ::= NOT expr
3d35b 22 2c 0d 0a 20 2f 2a 20 32 31 39 20 2a 2f 20 22  ",.. /* 219 */ "
3d35c 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20  expr ::= BITNOT 
3d35d 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 32 30 20  expr",.. /* 220 
3d35e 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e  */ "expr ::= MIN
3d35f 55 53 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32  US expr",.. /* 2
3d360 32 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  21 */ "expr ::= 
3d361 50 4c 55 53 20 65 78 70 72 22 2c 0d 0a 20 2f 2a  PLUS expr",.. /*
3d362 20 32 32 32 20 2a 2f 20 22 62 65 74 77 65 65 6e   222 */ "between
3d363 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45 4e 22  _op ::= BETWEEN"
3d364 2c 0d 0a 20 2f 2a 20 32 32 33 20 2a 2f 20 22 62  ,.. /* 223 */ "b
3d365 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f  etween_op ::= NO
3d366 54 20 42 45 54 57 45 45 4e 22 2c 0d 0a 20 2f 2a  T BETWEEN",.. /*
3d367 20 32 32 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   224 */ "expr ::
3d368 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f 6f  = expr between_o
3d369 70 20 65 78 70 72 20 41 4e 44 20 65 78 70 72 22  p expr AND expr"
3d36a 2c 0d 0a 20 2f 2a 20 32 32 35 20 2a 2f 20 22 69  ,.. /* 225 */ "i
3d36b 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c 0d 0a 20  n_op ::= IN",.. 
3d36c 2f 2a 20 32 32 36 20 2a 2f 20 22 69 6e 5f 6f 70  /* 226 */ "in_op
3d36d 20 3a 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0d 0a 20   ::= NOT IN",.. 
3d36e 2f 2a 20 32 32 37 20 2a 2f 20 22 65 78 70 72 20  /* 227 */ "expr 
3d36f 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c  ::= expr in_op L
3d370 50 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0d  P exprlist RP",.
3d371 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22 65 78 70  . /* 228 */ "exp
3d372 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20  r ::= LP select 
3d373 52 50 22 2c 0d 0a 20 2f 2a 20 32 32 39 20 2a 2f  RP",.. /* 229 */
3d374 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
3d375 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74 20  in_op LP select 
3d376 52 50 22 2c 0d 0a 20 2f 2a 20 32 33 30 20 2a 2f  RP",.. /* 230 */
3d377 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
3d378 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c 0d  in_op nm dbnm",.
3d379 0a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78 70  . /* 231 */ "exp
3d37a 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20  r ::= EXISTS LP 
3d37b 73 65 6c 65 63 74 20 52 50 22 2c 0d 0a 20 2f 2a  select RP",.. /*
3d37c 20 32 33 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a   232 */ "expr ::
3d37d 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72  = CASE case_oper
3d37e 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73  and case_exprlis
3d37f 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22  t case_else END"
3d380 2c 0d 0a 20 2f 2a 20 32 33 33 20 2a 2f 20 22 63  ,.. /* 233 */ "c
3d381 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
3d382 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
3d383 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
3d384 70 72 22 2c 0d 0a 20 2f 2a 20 32 33 34 20 2a 2f  pr",.. /* 234 */
3d385 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20   "case_exprlist 
3d386 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48  ::= WHEN expr TH
3d387 45 4e 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32  EN expr",.. /* 2
3d388 33 35 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65  35 */ "case_else
3d389 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c   ::= ELSE expr",
3d38a 0d 0a 20 2f 2a 20 32 33 36 20 2a 2f 20 22 63 61  .. /* 236 */ "ca
3d38b 73 65 5f 65 6c 73 65 20 3a 3a 3d 22 2c 0d 0a 20  se_else ::=",.. 
3d38c 2f 2a 20 32 33 37 20 2a 2f 20 22 63 61 73 65 5f  /* 237 */ "case_
3d38d 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72  operand ::= expr
3d38e 22 2c 0d 0a 20 2f 2a 20 32 33 38 20 2a 2f 20 22  ",.. /* 238 */ "
3d38f 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d  case_operand ::=
3d390 22 2c 0d 0a 20 2f 2a 20 32 33 39 20 2a 2f 20 22  ",.. /* 239 */ "
3d391 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78  exprlist ::= nex
3d392 70 72 6c 69 73 74 22 2c 0d 0a 20 2f 2a 20 32 34  prlist",.. /* 24
3d393 30 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a  0 */ "exprlist :
3d394 3a 3d 22 2c 0d 0a 20 2f 2a 20 32 34 31 20 2a 2f  :=",.. /* 241 */
3d395 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20   "nexprlist ::= 
3d396 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20  nexprlist COMMA 
3d397 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 34 32 20  expr",.. /* 242 
3d398 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a  */ "nexprlist ::
3d399 3d 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 32 34  = expr",.. /* 24
3d39a 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  3 */ "cmd ::= cr
3d39b 65 61 74 65 6b 77 20 75 6e 69 71 75 65 66 6c 61  eatekw uniquefla
3d39c 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65 78 69  g INDEX ifnotexi
3d39d 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e  sts nm dbnm ON n
3d39e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22  m LP idxlist RP"
3d39f 2c 0d 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22 75  ,.. /* 244 */ "u
3d3a0 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e  niqueflag ::= UN
3d3a1 49 51 55 45 22 2c 0d 0a 20 2f 2a 20 32 34 35 20  IQUE",.. /* 245 
3d3a2 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a  */ "uniqueflag :
3d3a3 3a 3d 22 2c 0d 0a 20 2f 2a 20 32 34 36 20 2a 2f  :=",.. /* 246 */
3d3a4 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a   "idxlist_opt ::
3d3a5 3d 22 2c 0d 0a 20 2f 2a 20 32 34 37 20 2a 2f 20  =",.. /* 247 */ 
3d3a6 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
3d3a7 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c   LP idxlist RP",
3d3a8 0d 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64  .. /* 248 */ "id
3d3a9 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73  xlist ::= idxlis
3d3aa 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61  t COMMA nm colla
3d3ab 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0d 0a  te sortorder",..
3d3ac 20 2f 2a 20 32 34 39 20 2a 2f 20 22 69 64 78 6c   /* 249 */ "idxl
3d3ad 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61  ist ::= nm colla
3d3ae 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0d 0a  te sortorder",..
3d3af 20 2f 2a 20 32 35 30 20 2a 2f 20 22 63 6f 6c 6c   /* 250 */ "coll
3d3b0 61 74 65 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 32  ate ::=",.. /* 2
3d3b1 35 31 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a  51 */ "collate :
3d3b2 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c  := COLLATE ids",
3d3b3 0d 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 63 6d  .. /* 252 */ "cm
3d3b4 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58  d ::= DROP INDEX
3d3b5 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
3d3b6 6d 65 22 2c 0d 0a 20 2f 2a 20 32 35 33 20 2a 2f  me",.. /* 253 */
3d3b7 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d   "cmd ::= VACUUM
3d3b8 22 2c 0d 0a 20 2f 2a 20 32 35 34 20 2a 2f 20 22  ",.. /* 254 */ "
3d3b9 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e  cmd ::= VACUUM n
3d3ba 6d 22 2c 0d 0a 20 2f 2a 20 32 35 35 20 2a 2f 20  m",.. /* 255 */ 
3d3bb 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
3d3bc 6e 6d 20 64 62 6e 6d 22 2c 0d 0a 20 2f 2a 20 32  nm dbnm",.. /* 2
3d3bd 35 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  56 */ "cmd ::= P
3d3be 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
3d3bf 20 6e 6d 6e 75 6d 22 2c 0d 0a 20 2f 2a 20 32 35   nmnum",.. /* 25
3d3c0 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  7 */ "cmd ::= PR
3d3c1 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20  AGMA nm dbnm LP 
3d3c2 6e 6d 6e 75 6d 20 52 50 22 2c 0d 0a 20 2f 2a 20  nmnum RP",.. /* 
3d3c3 32 35 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  258 */ "cmd ::= 
3d3c4 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45  PRAGMA nm dbnm E
3d3c5 51 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0d 0a 20  Q minus_num",.. 
3d3c6 2f 2a 20 32 35 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 259 */ "cmd :
3d3c7 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
3d3c8 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52  m LP minus_num R
3d3c9 50 22 2c 0d 0a 20 2f 2a 20 32 36 30 20 2a 2f 20  P",.. /* 260 */ 
3d3ca 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f  "nmnum ::= plus_
3d3cb 6e 75 6d 22 2c 0d 0a 20 2f 2a 20 32 36 31 20 2a  num",.. /* 261 *
3d3cc 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22  / "nmnum ::= nm"
3d3cd 2c 0d 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e  ,.. /* 262 */ "n
3d3ce 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0d 0a 20  mnum ::= ON",.. 
3d3cf 2f 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d  /* 263 */ "nmnum
3d3d0 20 3a 3a 3d 20 44 45 4c 45 54 45 22 2c 0d 0a 20   ::= DELETE",.. 
3d3d1 2f 2a 20 32 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d  /* 264 */ "nmnum
3d3d2 20 3a 3a 3d 20 44 45 46 41 55 4c 54 22 2c 0d 0a   ::= DEFAULT",..
3d3d3 20 2f 2a 20 32 36 35 20 2a 2f 20 22 70 6c 75 73   /* 265 */ "plus
3d3d4 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70  _num ::= plus_op
3d3d5 74 20 6e 75 6d 62 65 72 22 2c 0d 0a 20 2f 2a 20  t number",.. /* 
3d3d6 32 36 36 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75  266 */ "minus_nu
3d3d7 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62  m ::= MINUS numb
3d3d8 65 72 22 2c 0d 0a 20 2f 2a 20 32 36 37 20 2a 2f  er",.. /* 267 */
3d3d9 20 22 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54   "number ::= INT
3d3da 45 47 45 52 7c 46 4c 4f 41 54 22 2c 0d 0a 20 2f  EGER|FLOAT",.. /
3d3db 2a 20 32 36 38 20 2a 2f 20 22 70 6c 75 73 5f 6f  * 268 */ "plus_o
3d3dc 70 74 20 3a 3a 3d 20 50 4c 55 53 22 2c 0d 0a 20  pt ::= PLUS",.. 
3d3dd 2f 2a 20 32 36 39 20 2a 2f 20 22 70 6c 75 73 5f  /* 269 */ "plus_
3d3de 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 32  opt ::=",.. /* 2
3d3df 37 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  70 */ "cmd ::= c
3d3e0 72 65 61 74 65 6b 77 20 74 72 69 67 67 65 72 5f  reatekw trigger_
3d3e1 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67  decl BEGIN trigg
3d3e2 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 22  er_cmd_list END"
3d3e3 2c 0d 0a 20 2f 2a 20 32 37 31 20 2a 2f 20 22 74  ,.. /* 271 */ "t
3d3e4 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20  rigger_decl ::= 
3d3e5 74 65 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e  temp TRIGGER ifn
3d3e6 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
3d3e7 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72   trigger_time tr
3d3e8 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66  igger_event ON f
3d3e9 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f  ullname foreach_
3d3ea 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75  clause when_clau
3d3eb 73 65 22 2c 0d 0a 20 2f 2a 20 32 37 32 20 2a 2f  se",.. /* 272 */
3d3ec 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
3d3ed 3a 3d 20 42 45 46 4f 52 45 22 2c 0d 0a 20 2f 2a  := BEFORE",.. /*
3d3ee 20 32 37 33 20 2a 2f 20 22 74 72 69 67 67 65 72   273 */ "trigger
3d3ef 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22  _time ::= AFTER"
3d3f0 2c 0d 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22 74  ,.. /* 274 */ "t
3d3f1 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
3d3f2 49 4e 53 54 45 41 44 20 4f 46 22 2c 0d 0a 20 2f  INSTEAD OF",.. /
3d3f3 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67 67 65  * 275 */ "trigge
3d3f4 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0d 0a 20 2f  r_time ::=",.. /
3d3f5 2a 20 32 37 36 20 2a 2f 20 22 74 72 69 67 67 65  * 276 */ "trigge
3d3f6 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45  r_event ::= DELE
3d3f7 54 45 7c 49 4e 53 45 52 54 22 2c 0d 0a 20 2f 2a  TE|INSERT",.. /*
3d3f8 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67 65 72   277 */ "trigger
3d3f9 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54  _event ::= UPDAT
3d3fa 45 22 2c 0d 0a 20 2f 2a 20 32 37 38 20 2a 2f 20  E",.. /* 278 */ 
3d3fb 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a  "trigger_event :
3d3fc 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73  := UPDATE OF ins
3d3fd 63 6f 6c 6c 69 73 74 22 2c 0d 0a 20 2f 2a 20 32  collist",.. /* 2
3d3fe 37 39 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63  79 */ "foreach_c
3d3ff 6c 61 75 73 65 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a  lause ::=",.. /*
3d400 20 32 38 30 20 2a 2f 20 22 66 6f 72 65 61 63 68   280 */ "foreach
3d401 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20  _clause ::= FOR 
3d402 45 41 43 48 20 52 4f 57 22 2c 0d 0a 20 2f 2a 20  EACH ROW",.. /* 
3d403 32 38 31 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61  281 */ "when_cla
3d404 75 73 65 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 32  use ::=",.. /* 2
3d405 38 32 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75  82 */ "when_clau
3d406 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72  se ::= WHEN expr
3d407 22 2c 0d 0a 20 2f 2a 20 32 38 33 20 2a 2f 20 22  ",.. /* 283 */ "
3d408 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
3d409 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
3d40a 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f 63 6d  _list trigger_cm
3d40b 64 20 53 45 4d 49 22 2c 0d 0a 20 2f 2a 20 32 38  d SEMI",.. /* 28
3d40c 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  4 */ "trigger_cm
3d40d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67  d_list ::= trigg
3d40e 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0d 0a 20  er_cmd SEMI",.. 
3d40f 2f 2a 20 32 38 35 20 2a 2f 20 22 74 72 6e 6d 20  /* 285 */ "trnm 
3d410 3a 3a 3d 20 6e 6d 22 2c 0d 0a 20 2f 2a 20 32 38  ::= nm",.. /* 28
3d411 36 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e  6 */ "trnm ::= n
3d412 6d 20 44 4f 54 20 6e 6d 22 2c 0d 0a 20 2f 2a 20  m DOT nm",.. /* 
3d413 32 38 37 20 2a 2f 20 22 74 72 69 64 78 62 79 20  287 */ "tridxby 
3d414 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 32 38 38 20 2a  ::=",.. /* 288 *
3d415 2f 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 20 49  / "tridxby ::= I
3d416 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c 0d 0a  NDEXED BY nm",..
3d417 20 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72 69 64   /* 289 */ "trid
3d418 78 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45  xby ::= NOT INDE
3d419 58 45 44 22 2c 0d 0a 20 2f 2a 20 32 39 30 20 2a  XED",.. /* 290 *
3d41a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
3d41b 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66  := UPDATE orconf
3d41c 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 53 45   trnm tridxby SE
3d41d 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f  T setlist where_
3d41e 6f 70 74 22 2c 0d 0a 20 2f 2a 20 32 39 31 20 2a  opt",.. /* 291 *
3d41f 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
3d420 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
3d421 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69  TO trnm inscolli
3d422 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50  st_opt VALUES LP
3d423 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c 0d 0a   itemlist RP",..
3d424 20 2f 2a 20 32 39 32 20 2a 2f 20 22 74 72 69 67   /* 292 */ "trig
3d425 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  ger_cmd ::= inse
3d426 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d  rt_cmd INTO trnm
3d427 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
3d428 73 65 6c 65 63 74 22 2c 0d 0a 20 2f 2a 20 32 39  select",.. /* 29
3d429 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  3 */ "trigger_cm
3d42a 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f  d ::= DELETE FRO
3d42b 4d 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 77  M trnm tridxby w
3d42c 68 65 72 65 5f 6f 70 74 22 2c 0d 0a 20 2f 2a 20  here_opt",.. /* 
3d42d 32 39 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  294 */ "trigger_
3d42e 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c  cmd ::= select",
3d42f 0d 0a 20 2f 2a 20 32 39 35 20 2a 2f 20 22 65 78  .. /* 295 */ "ex
3d430 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20  pr ::= RAISE LP 
3d431 49 47 4e 4f 52 45 20 52 50 22 2c 0d 0a 20 2f 2a  IGNORE RP",.. /*
3d432 20 32 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a   296 */ "expr ::
3d433 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65  = RAISE LP raise
3d434 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50  type COMMA nm RP
3d435 22 2c 0d 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22  ",.. /* 297 */ "
3d436 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f  raisetype ::= RO
3d437 4c 4c 42 41 43 4b 22 2c 0d 0a 20 2f 2a 20 32 39  LLBACK",.. /* 29
3d438 38 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20  8 */ "raisetype 
3d439 3a 3a 3d 20 41 42 4f 52 54 22 2c 0d 0a 20 2f 2a  ::= ABORT",.. /*
3d43a 20 32 39 39 20 2a 2f 20 22 72 61 69 73 65 74 79   299 */ "raisety
3d43b 70 65 20 3a 3a 3d 20 46 41 49 4c 22 2c 0d 0a 20  pe ::= FAIL",.. 
3d43c 2f 2a 20 33 30 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 300 */ "cmd :
3d43d 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45 52 20  := DROP TRIGGER 
3d43e 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
3d43f 65 22 2c 0d 0a 20 2f 2a 20 33 30 31 20 2a 2f 20  e",.. /* 301 */ 
3d440 22 63 6d 64 20 3a 3a 3d 20 41 54 54 41 43 48 20  "cmd ::= ATTACH 
3d441 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
3d442 65 78 70 72 20 41 53 20 65 78 70 72 20 6b 65 79  expr AS expr key
3d443 5f 6f 70 74 22 2c 0d 0a 20 2f 2a 20 33 30 32 20  _opt",.. /* 302 
3d444 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 54 41  */ "cmd ::= DETA
3d445 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
3d446 70 74 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 33  pt expr",.. /* 3
3d447 30 33 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a  03 */ "key_opt :
3d448 3a 3d 22 2c 0d 0a 20 2f 2a 20 33 30 34 20 2a 2f  :=",.. /* 304 */
3d449 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45   "key_opt ::= KE
3d44a 59 20 65 78 70 72 22 2c 0d 0a 20 2f 2a 20 33 30  Y expr",.. /* 30
3d44b 35 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b  5 */ "database_k
3d44c 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41  w_opt ::= DATABA
3d44d 53 45 22 2c 0d 0a 20 2f 2a 20 33 30 36 20 2a 2f  SE",.. /* 306 */
3d44e 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70   "database_kw_op
3d44f 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a 20 33 30 37  t ::=",.. /* 307
3d450 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49   */ "cmd ::= REI
3d451 4e 44 45 58 22 2c 0d 0a 20 2f 2a 20 33 30 38 20  NDEX",.. /* 308 
3d452 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e  */ "cmd ::= REIN
3d453 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0d 0a 20  DEX nm dbnm",.. 
3d454 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 309 */ "cmd :
3d455 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0d 0a 20 2f  := ANALYZE",.. /
3d456 2a 20 33 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 310 */ "cmd ::
3d457 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e  = ANALYZE nm dbn
3d458 6d 22 2c 0d 0a 20 2f 2a 20 33 31 31 20 2a 2f 20  m",.. /* 311 */ 
3d459 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54  "cmd ::= ALTER T
3d45a 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45  ABLE fullname RE
3d45b 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0d 0a 20 2f  NAME TO nm",.. /
3d45c 2a 20 33 31 32 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 312 */ "cmd ::
3d45d 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64  = ALTER TABLE ad
3d45e 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d  d_column_fullnam
3d45f 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f  e ADD kwcolumn_o
3d460 70 74 20 63 6f 6c 75 6d 6e 22 2c 0d 0a 20 2f 2a  pt column",.. /*
3d461 20 33 31 33 20 2a 2f 20 22 61 64 64 5f 63 6f 6c   313 */ "add_col
3d462 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d  umn_fullname ::=
3d463 20 66 75 6c 6c 6e 61 6d 65 22 2c 0d 0a 20 2f 2a   fullname",.. /*
3d464 20 33 31 34 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d   314 */ "kwcolum
3d465 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0d 0a 20 2f 2a  n_opt ::=",.. /*
3d466 20 33 31 35 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d   315 */ "kwcolum
3d467 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e  n_opt ::= COLUMN
3d468 4b 57 22 2c 0d 0a 20 2f 2a 20 33 31 36 20 2a 2f  KW",.. /* 316 */
3d469 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65   "cmd ::= create
3d46a 5f 76 74 61 62 22 2c 0d 0a 20 2f 2a 20 33 31 37  _vtab",.. /* 317
3d46b 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65   */ "cmd ::= cre
3d46c 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62  ate_vtab LP vtab
3d46d 61 72 67 6c 69 73 74 20 52 50 22 2c 0d 0a 20 2f  arglist RP",.. /
3d46e 2a 20 33 31 38 20 2a 2f 20 22 63 72 65 61 74 65  * 318 */ "create
3d46f 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65  _vtab ::= create
3d470 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  kw VIRTUAL TABLE
3d471 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e   nm dbnm USING n
3d472 6d 22 2c 0d 0a 20 2f 2a 20 33 31 39 20 2a 2f 20  m",.. /* 319 */ 
3d473 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d  "vtabarglist ::=
3d474 20 76 74 61 62 61 72 67 22 2c 0d 0a 20 2f 2a 20   vtabarg",.. /* 
3d475 33 32 30 20 2a 2f 20 22 76 74 61 62 61 72 67 6c  320 */ "vtabargl
3d476 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c  ist ::= vtabargl
3d477 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72  ist COMMA vtabar
3d478 67 22 2c 0d 0a 20 2f 2a 20 33 32 31 20 2a 2f 20  g",.. /* 321 */ 
3d479 22 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c 0d 0a  "vtabarg ::=",..
3d47a 20 2f 2a 20 33 32 32 20 2a 2f 20 22 76 74 61 62   /* 322 */ "vtab
3d47b 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20  arg ::= vtabarg 
3d47c 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0d 0a  vtabargtoken",..
3d47d 20 2f 2a 20 33 32 33 20 2a 2f 20 22 76 74 61 62   /* 323 */ "vtab
3d47e 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59  argtoken ::= ANY
3d47f 22 2c 0d 0a 20 2f 2a 20 33 32 34 20 2a 2f 20 22  ",.. /* 324 */ "
3d480 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
3d481 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 22 2c   lp anylist RP",
3d482 0d 0a 20 2f 2a 20 33 32 35 20 2a 2f 20 22 6c 70  .. /* 325 */ "lp
3d483 20 3a 3a 3d 20 4c 50 22 2c 0d 0a 20 2f 2a 20 33   ::= LP",.. /* 3
3d484 32 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a  26 */ "anylist :
3d485 3a 3d 22 2c 0d 0a 20 2f 2a 20 33 32 37 20 2a 2f  :=",.. /* 327 */
3d486 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
3d487 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74  ylist LP anylist
3d488 20 52 50 22 2c 0d 0a 20 2f 2a 20 33 32 38 20 2a   RP",.. /* 328 *
3d489 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61  / "anylist ::= a
3d48a 6e 79 6c 69 73 74 20 41 4e 59 22 2c 0d 0a 7d 3b  nylist ANY",..};
3d48b 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  ..#endif /* NDEB
3d48c 55 47 20 2a 2f 0d 0a 0d 0a 0d 0a 23 69 66 20 59  UG */......#if Y
3d48d 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0d 0a  YSTACKDEPTH<=0..
3d48e 2f 2a 0d 0a 2a 2a 20 54 72 79 20 74 6f 20 69 6e  /*..** Try to in
3d48f 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
3d490 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 73 74  of the parser st
3d491 61 63 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ack...*/..static
3d492 20 76 6f 69 64 20 79 79 47 72 6f 77 53 74 61 63   void yyGrowStac
3d493 6b 28 79 79 50 61 72 73 65 72 20 2a 70 29 7b 0d  k(yyParser *p){.
3d494 0a 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0d  .  int newSize;.
3d495 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
3d496 2a 70 4e 65 77 3b 0d 0a 0d 0a 20 20 6e 65 77 53  *pNew;....  newS
3d497 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73 7a  ize = p->yystksz
3d498 2a 32 20 2b 20 31 30 30 3b 0d 0a 20 20 70 4e 65  *2 + 100;..  pNe
3d499 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79  w = realloc(p->y
3d49a 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a  ystack, newSize*
3d49b 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29  sizeof(pNew[0]))
3d49c 3b 0d 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  ;..  if( pNew ){
3d49d 0d 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b  ..    p->yystack
3d49e 20 3d 20 70 4e 65 77 3b 0d 0a 20 20 20 20 70 2d   = pNew;..    p-
3d49f 3e 79 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69  >yystksz = newSi
3d4a0 7a 65 3b 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45  ze;..#ifndef NDE
3d4a1 42 55 47 0d 0a 20 20 20 20 69 66 28 20 79 79 54  BUG..    if( yyT
3d4a2 72 61 63 65 46 49 4c 45 20 29 7b 0d 0a 20 20 20  raceFILE ){..   
3d4a3 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
3d4a4 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20  ceFILE,"%sStack 
3d4a5 67 72 6f 77 73 20 74 6f 20 25 64 20 65 6e 74 72  grows to %d entr
3d4a6 69 65 73 21 5c 6e 22 2c 0d 0a 20 20 20 20 20 20  ies!\n",..      
3d4a7 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
3d4a8 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73 74 6b 73  rompt, p->yystks
3d4a9 7a 29 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  z);..    }..#end
3d4aa 69 66 0d 0a 20 20 7d 0d 0a 7d 0d 0a 23 65 6e 64  if..  }..}..#end
3d4ab 69 66 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68  if..../* ..** Th
3d4ac 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f  is function allo
3d4ad 63 61 74 65 73 20 61 20 6e 65 77 20 70 61 72 73  cates a new pars
3d4ae 65 72 2e 0d 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  er...** The only
3d4af 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
3d4b0 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
3d4b1 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b 73  tion which works
3d4b2 20 6c 69 6b 65 0d 0a 2a 2a 20 6d 61 6c 6c 6f 63   like..** malloc
3d4b3 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 70 75 74 73  ...**..** Inputs
3d4b4 3a 0d 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  :..** A pointer 
3d4b5 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
3d4b6 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
3d4b7 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a   memory...**..**
3d4b8 20 4f 75 74 70 75 74 73 3a 0d 0a 2a 2a 20 41 20   Outputs:..** A 
3d4b9 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72  pointer to a par
3d4ba 73 65 72 2e 20 20 54 68 69 73 20 70 6f 69 6e 74  ser.  This point
3d4bb 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 73 75  er is used in su
3d4bc 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 0d 0a  bsequent calls..
3d4bd 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 72  ** to sqlite3Par
3d4be 73 65 72 20 61 6e 64 20 73 71 6c 69 74 65 33 50  ser and sqlite3P
3d4bf 61 72 73 65 72 46 72 65 65 2e 0d 0a 2a 2f 0d 0a  arserFree...*/..
3d4c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3d4c1 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73  oid *sqlite3Pars
3d4c2 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a  erAlloc(void *(*
3d4c3 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65  mallocProc)(size
3d4c4 5f 74 29 29 7b 0d 0a 20 20 79 79 50 61 72 73 65  _t)){..  yyParse
3d4c5 72 20 2a 70 50 61 72 73 65 72 3b 0d 0a 20 20 70  r *pParser;..  p
3d4c6 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72 73  Parser = (yyPars
3d4c7 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63  er*)(*mallocProc
3d4c8 29 28 20 28 73 69 7a 65 5f 74 29 73 69 7a 65 6f  )( (size_t)sizeo
3d4c9 66 28 79 79 50 61 72 73 65 72 29 20 29 3b 0d 0a  f(yyParser) );..
3d4ca 20 20 69 66 28 20 70 50 61 72 73 65 72 20 29 7b    if( pParser ){
3d4cb 0d 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79  ..    pParser->y
3d4cc 79 69 64 78 20 3d 20 2d 31 3b 0d 0a 23 69 66 64  yidx = -1;..#ifd
3d4cd 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
3d4ce 43 4b 44 45 50 54 48 0d 0a 20 20 20 20 70 50 61  CKDEPTH..    pPa
3d4cf 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d  rser->yyidxMax =
3d4d0 20 30 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66   0;..#endif..#if
3d4d1 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
3d4d2 0d 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79  ..    pParser->y
3d4d3 79 73 74 61 63 6b 20 3d 20 4e 55 4c 4c 3b 0d 0a  ystack = NULL;..
3d4d4 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73      pParser->yys
3d4d5 74 6b 73 7a 20 3d 20 30 3b 0d 0a 20 20 20 20 79  tksz = 0;..    y
3d4d6 79 47 72 6f 77 53 74 61 63 6b 28 70 50 61 72 73  yGrowStack(pPars
3d4d7 65 72 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  er);..#endif..  
3d4d8 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72  }..  return pPar
3d4d9 73 65 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68  ser;..}..../* Th
3d4da 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
3d4db 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 65  tion deletes the
3d4dc 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65   value associate
3d4dd 64 20 77 69 74 68 20 61 0d 0a 2a 2a 20 73 79 6d  d with a..** sym
3d4de 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c  bol.  The symbol
3d4df 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
3d4e0 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e   terminal or non
3d4e1 74 65 72 6d 69 6e 61 6c 2e 0d 0a 2a 2a 20 22 79  terminal...** "y
3d4e2 79 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73  ymajor" is the s
3d4e3 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20  ymbol code, and 
3d4e4 22 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20  "yypminor" is a 
3d4e5 70 6f 69 6e 74 65 72 20 74 6f 0d 0a 2a 2a 20 74  pointer to..** t
3d4e6 68 65 20 76 61 6c 75 65 2e 0d 0a 2a 2f 0d 0a 73  he value...*/..s
3d4e7 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 64 65  tatic void yy_de
3d4e8 73 74 72 75 63 74 6f 72 28 0d 0a 20 20 79 79 50  structor(..  yyP
3d4e9 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
3d4ea 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73  ,    /* The pars
3d4eb 65 72 20 2a 2f 0d 0a 20 20 59 59 43 4f 44 45 54  er */..  YYCODET
3d4ec 59 50 45 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20  YPE yymajor,    
3d4ed 20 2f 2a 20 54 79 70 65 20 63 6f 64 65 20 66 6f   /* Type code fo
3d4ee 72 20 6f 62 6a 65 63 74 20 74 6f 20 64 65 73 74  r object to dest
3d4ef 72 6f 79 20 2a 2f 0d 0a 20 20 59 59 4d 49 4e 4f  roy */..  YYMINO
3d4f0 52 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 20  RTYPE *yypminor 
3d4f1 20 20 2f 2a 20 54 68 65 20 6f 62 6a 65 63 74 20    /* The object 
3d4f2 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20  to be destroyed 
3d4f3 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
3d4f4 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
3d4f5 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 79 79 6d  ;..  switch( yym
3d4f6 61 6a 6f 72 20 29 7b 0d 0a 20 20 20 20 2f 2a 20  ajor ){..    /* 
3d4f7 48 65 72 65 20 69 73 20 69 6e 73 65 72 74 65 64  Here is inserted
3d4f8 20 74 68 65 20 61 63 74 69 6f 6e 73 20 77 68 69   the actions whi
3d4f9 63 68 20 74 61 6b 65 20 70 6c 61 63 65 20 77 68  ch take place wh
3d4fa 65 6e 20 61 0d 0a 20 20 20 20 2a 2a 20 74 65 72  en a..    ** ter
3d4fb 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72  minal or non-ter
3d4fc 6d 69 6e 61 6c 20 69 73 20 64 65 73 74 72 6f 79  minal is destroy
3d4fd 65 64 2e 20 20 54 68 69 73 20 63 61 6e 20 68 61  ed.  This can ha
3d4fe 70 70 65 6e 0d 0a 20 20 20 20 2a 2a 20 77 68 65  ppen..    ** whe
3d4ff 6e 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 73 20  n the symbol is 
3d500 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20  popped from the 
3d501 73 74 61 63 6b 20 64 75 72 69 6e 67 20 61 0d 0a  stack during a..
3d502 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 6f 72      ** reduce or
3d503 20 64 75 72 69 6e 67 20 65 72 72 6f 72 20 70 72   during error pr
3d504 6f 63 65 73 73 69 6e 67 20 6f 72 20 77 68 65 6e  ocessing or when
3d505 20 61 20 70 61 72 73 65 72 20 69 73 20 0d 0a 20   a parser is .. 
3d506 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 73 74     ** being dest
3d507 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69 74 20  royed before it 
3d508 69 73 20 66 69 6e 69 73 68 65 64 20 70 61 72 73  is finished pars
3d509 69 6e 67 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20  ing...    **..  
3d50a 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e    ** Note: durin
3d50b 67 20 61 20 72 65 64 75 63 65 2c 20 74 68 65 20  g a reduce, the 
3d50c 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73  only symbols des
3d50d 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65  troyed are those
3d50e 0d 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 61  ..    ** which a
3d50f 70 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48 53  ppear on the RHS
3d510 20 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62 75   of the rule, bu
3d511 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20  t which are not 
3d512 75 73 65 64 0d 0a 20 20 20 20 2a 2a 20 69 6e 73  used..    ** ins
3d513 69 64 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0d  ide the C code..
3d514 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73  .    */..    cas
3d515 65 20 31 36 30 3a 20 2f 2a 20 73 65 6c 65 63 74  e 160: /* select
3d516 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 31 39   */..    case 19
3d517 34 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20  4: /* oneselect 
3d518 2a 2f 0d 0a 7b 0d 0a 73 71 6c 69 74 65 33 53 65  */..{..sqlite3Se
3d519 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73  lectDelete(pPars
3d51a 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
3d51b 2d 3e 79 79 33 38 37 29 29 3b 0d 0a 7d 0d 0a 20  ->yy387));..}.. 
3d51c 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3d51d 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 74 65   case 174: /* te
3d51e 72 6d 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20  rm */..    case 
3d51f 31 37 35 3a 20 2f 2a 20 65 78 70 72 20 2a 2f 0d  175: /* expr */.
3d520 0a 7b 0d 0a 73 71 6c 69 74 65 33 45 78 70 72 44  .{..sqlite3ExprD
3d521 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
3d522 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31  , (yypminor->yy1
3d523 31 38 29 2e 70 45 78 70 72 29 3b 0d 0a 7d 0d 0a  18).pExpr);..}..
3d524 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3d525 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69    case 179: /* i
3d526 64 78 6c 69 73 74 5f 6f 70 74 20 2a 2f 0d 0a 20  dxlist_opt */.. 
3d527 20 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20     case 187: /* 
3d528 69 64 78 6c 69 73 74 20 2a 2f 0d 0a 20 20 20 20  idxlist */..    
3d529 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 73 65 6c  case 197: /* sel
3d52a 63 6f 6c 6c 69 73 74 20 2a 2f 0d 0a 20 20 20 20  collist */..    
3d52b 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 67 72 6f  case 200: /* gro
3d52c 75 70 62 79 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20  upby_opt */..   
3d52d 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20 6f 72   case 202: /* or
3d52e 64 65 72 62 79 5f 6f 70 74 20 2a 2f 0d 0a 20 20  derby_opt */..  
3d52f 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 73    case 204: /* s
3d530 63 6c 70 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65  clp */..    case
3d531 20 32 31 34 3a 20 2f 2a 20 73 6f 72 74 6c 69 73   214: /* sortlis
3d532 74 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 32  t */..    case 2
3d533 31 36 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74  16: /* nexprlist
3d534 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 32 31   */..    case 21
3d535 37 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a 2f  7: /* setlist */
3d536 0d 0a 20 20 20 20 63 61 73 65 20 32 32 30 3a 20  ..    case 220: 
3d537 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f 0d 0a  /* itemlist */..
3d538 20 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a      case 221: /*
3d539 20 65 78 70 72 6c 69 73 74 20 2a 2f 0d 0a 20 20   exprlist */..  
3d53a 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 63    case 226: /* c
3d53b 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0d  ase_exprlist */.
3d53c 0a 7b 0d 0a 73 71 6c 69 74 65 33 45 78 70 72 4c  .{..sqlite3ExprL
3d53d 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
3d53e 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
3d53f 3e 79 79 33 32 32 29 29 3b 0d 0a 7d 0d 0a 20 20  >yy322));..}..  
3d540 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3d541 63 61 73 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c  case 193: /* ful
3d542 6c 6e 61 6d 65 20 2a 2f 0d 0a 20 20 20 20 63 61  lname */..    ca
3d543 73 65 20 31 39 38 3a 20 2f 2a 20 66 72 6f 6d 20  se 198: /* from 
3d544 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 32 30 36  */..    case 206
3d545 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
3d546 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 32 30 37  */..    case 207
3d547 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
3d548 2a 2f 0d 0a 7b 0d 0a 73 71 6c 69 74 65 33 53 72  */..{..sqlite3Sr
3d549 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  cListDelete(pPar
3d54a 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
3d54b 72 2d 3e 79 79 32 35 39 29 29 3b 0d 0a 7d 0d 0a  r->yy259));..}..
3d54c 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3d54d 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 77    case 199: /* w
3d54e 68 65 72 65 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20  here_opt */..   
3d54f 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 68 61   case 201: /* ha
3d550 76 69 6e 67 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20  ving_opt */..   
3d551 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e   case 210: /* on
3d552 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20 20 63 61 73  _opt */..    cas
3d553 65 20 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74  e 215: /* sortit
3d554 65 6d 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20  em */..    case 
3d555 32 32 35 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65  225: /* case_ope
3d556 72 61 6e 64 20 2a 2f 0d 0a 20 20 20 20 63 61 73  rand */..    cas
3d557 65 20 32 32 37 3a 20 2f 2a 20 63 61 73 65 5f 65  e 227: /* case_e
3d558 6c 73 65 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65  lse */..    case
3d559 20 32 33 38 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c   238: /* when_cl
3d55a 61 75 73 65 20 2a 2f 0d 0a 20 20 20 20 63 61 73  ause */..    cas
3d55b 65 20 32 34 33 3a 20 2f 2a 20 6b 65 79 5f 6f 70  e 243: /* key_op
3d55c 74 20 2a 2f 0d 0a 7b 0d 0a 73 71 6c 69 74 65 33  t */..{..sqlite3
3d55d 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
3d55e 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
3d55f 2d 3e 79 79 33 31 34 29 29 3b 0d 0a 7d 0d 0a 20  ->yy314));..}.. 
3d560 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3d561 20 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 75 73   case 211: /* us
3d562 69 6e 67 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20 20  ing_opt */..    
3d563 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 69 6e 73  case 213: /* ins
3d564 63 6f 6c 6c 69 73 74 20 2a 2f 0d 0a 20 20 20 20  collist */..    
3d565 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 69 6e 73  case 219: /* ins
3d566 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0d 0a  collist_opt */..
3d567 7b 0d 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  {..sqlite3IdList
3d568 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
3d569 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
3d56a 33 38 34 29 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  384));..}..     
3d56b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73   break;..    cas
3d56c 65 20 32 33 34 3a 20 2f 2a 20 74 72 69 67 67 65  e 234: /* trigge
3d56d 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0d 0a 20  r_cmd_list */.. 
3d56e 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20     case 239: /* 
3d56f 74 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0d 0a  trigger_cmd */..
3d570 7b 0d 0a 73 71 6c 69 74 65 33 44 65 6c 65 74 65  {..sqlite3Delete
3d571 54 72 69 67 67 65 72 53 74 65 70 28 70 50 61 72  TriggerStep(pPar
3d572 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
3d573 72 2d 3e 79 79 32 30 33 29 29 3b 0d 0a 7d 0d 0a  r->yy203));..}..
3d574 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3d575 20 20 63 61 73 65 20 32 33 36 3a 20 2f 2a 20 74    case 236: /* t
3d576 72 69 67 67 65 72 5f 65 76 65 6e 74 20 2a 2f 0d  rigger_event */.
3d577 0a 7b 0d 0a 73 71 6c 69 74 65 33 49 64 4c 69 73  .{..sqlite3IdLis
3d578 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3d579 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
3d57a 79 39 30 29 2e 62 29 3b 0d 0a 7d 0d 0a 20 20 20  y90).b);..}..   
3d57b 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 64     break;..    d
3d57c 65 66 61 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20  efault:  break; 
3d57d 20 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73 74 72    /* If no destr
3d57e 75 63 74 6f 72 20 61 63 74 69 6f 6e 20 73 70 65  uctor action spe
3d57f 63 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69  cified: do nothi
3d580 6e 67 20 2a 2f 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ng */..  }..}...
3d581 0a 2f 2a 0d 0a 2a 2a 20 50 6f 70 20 74 68 65 20  ./*..** Pop the 
3d582 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 6f  parser's stack o
3d583 6e 63 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  nce...**..** If 
3d584 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72  there is a destr
3d585 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73  uctor routine as
3d586 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3d587 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0d 0a 2a  e token which..*
3d588 2a 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d  * is popped from
3d589 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e   the stack, then
3d58a 20 63 61 6c 6c 20 69 74 2e 0d 0a 2a 2a 0d 0a 2a   call it...**..*
3d58b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 6a  * Return the maj
3d58c 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20  or token number 
3d58d 66 6f 72 20 74 68 65 20 73 79 6d 62 6f 6c 20 70  for the symbol p
3d58e 6f 70 70 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  opped...*/..stat
3d58f 69 63 20 69 6e 74 20 79 79 5f 70 6f 70 5f 70 61  ic int yy_pop_pa
3d590 72 73 65 72 5f 73 74 61 63 6b 28 79 79 50 61 72  rser_stack(yyPar
3d591 73 65 72 20 2a 70 50 61 72 73 65 72 29 7b 0d 0a  ser *pParser){..
3d592 20 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d    YYCODETYPE yym
3d593 61 6a 6f 72 3b 0d 0a 20 20 79 79 53 74 61 63 6b  ajor;..  yyStack
3d594 45 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d 20 26  Entry *yytos = &
3d595 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
3d596 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  [pParser->yyidx]
3d597 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 72 65 20  ;....  /* There 
3d598 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20  is no mechanism 
3d599 62 79 20 77 68 69 63 68 20 74 68 65 20 70 61 72  by which the par
3d59a 73 65 72 20 73 74 61 63 6b 20 63 61 6e 20 62 65  ser stack can be
3d59b 20 70 6f 70 70 65 64 20 62 65 6c 6f 77 0d 0a 20   popped below.. 
3d59c 20 2a 2a 20 65 6d 70 74 79 20 69 6e 20 53 51 4c   ** empty in SQL
3d59d 69 74 65 2e 20 20 2a 2f 0d 0a 20 20 69 66 28 20  ite.  */..  if( 
3d59e 4e 45 56 45 52 28 70 50 61 72 73 65 72 2d 3e 79  NEVER(pParser->y
3d59f 79 69 64 78 3c 30 29 20 29 20 72 65 74 75 72 6e  yidx<0) ) return
3d5a0 20 30 3b 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45   0;..#ifndef NDE
3d5a1 42 55 47 0d 0a 20 20 69 66 28 20 79 79 54 72 61  BUG..  if( yyTra
3d5a2 63 65 46 49 4c 45 20 26 26 20 70 50 61 72 73 65  ceFILE && pParse
3d5a3 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0d 0a  r->yyidx>=0 ){..
3d5a4 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
3d5a5 61 63 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69  aceFILE,"%sPoppi
3d5a6 6e 67 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20  ng %s\n",..     
3d5a7 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0d   yyTracePrompt,.
3d5a8 0a 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61  .      yyTokenNa
3d5a9 6d 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d  me[yytos->major]
3d5aa 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  );..  }..#endif.
3d5ab 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20 79 79 74  .  yymajor = yyt
3d5ac 6f 73 2d 3e 6d 61 6a 6f 72 3b 0d 0a 20 20 79 79  os->major;..  yy
3d5ad 5f 64 65 73 74 72 75 63 74 6f 72 28 70 50 61 72  _destructor(pPar
3d5ae 73 65 72 2c 20 79 79 6d 61 6a 6f 72 2c 20 26 79  ser, yymajor, &y
3d5af 79 74 6f 73 2d 3e 6d 69 6e 6f 72 29 3b 0d 0a 20  ytos->minor);.. 
3d5b0 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d   pParser->yyidx-
3d5b1 2d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 79 79 6d  -;..  return yym
3d5b2 61 6a 6f 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  ajor;..}..../* .
3d5b3 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61  .** Deallocate a
3d5b4 6e 64 20 64 65 73 74 72 6f 79 20 61 20 70 61 72  nd destroy a par
3d5b5 73 65 72 2e 20 20 44 65 73 74 72 75 63 74 6f 72  ser.  Destructor
3d5b6 73 20 61 72 65 20 61 6c 6c 20 63 61 6c 6c 65 64  s are all called
3d5b7 20 66 6f 72 0d 0a 2a 2a 20 61 6c 6c 20 73 74 61   for..** all sta
3d5b8 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62 65 66 6f  ck elements befo
3d5b9 72 65 20 73 68 75 74 74 69 6e 67 20 74 68 65 20  re shutting the 
3d5ba 70 61 72 73 65 72 20 64 6f 77 6e 2e 0d 0a 2a 2a  parser down...**
3d5bb 0d 0a 2a 2a 20 49 6e 70 75 74 73 3a 0d 0a 2a 2a  ..** Inputs:..**
3d5bc 20 3c 75 6c 3e 0d 0a 2a 2a 20 3c 6c 69 3e 20 20   <ul>..** <li>  
3d5bd 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
3d5be 20 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73   parser.  This s
3d5bf 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74  hould be a point
3d5c0 65 72 0d 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74  er..**       obt
3d5c1 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
3d5c2 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0d 0a  e3ParserAlloc...
3d5c3 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74  ** <li>  A point
3d5c4 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
3d5c5 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d   used to reclaim
3d5c6 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
3d5c7 0d 0a 2a 2a 20 20 20 20 20 20 20 66 72 6f 6d 20  ..**       from 
3d5c8 6d 61 6c 6c 6f 63 2e 0d 0a 2a 2a 20 3c 2f 75 6c  malloc...** </ul
3d5c9 3e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  >..*/..SQLITE_PR
3d5ca 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
3d5cb 65 33 50 61 72 73 65 72 46 72 65 65 28 0d 0a 20  e3ParserFree(.. 
3d5cc 20 76 6f 69 64 20 2a 70 2c 20 20 20 20 20 20 20   void *p,       
3d5cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3d5ce 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65  The parser to be
3d5cf 20 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 76   deleted */..  v
3d5d0 6f 69 64 20 28 2a 66 72 65 65 50 72 6f 63 29 28  oid (*freeProc)(
3d5d1 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 46 75  void*)     /* Fu
3d5d2 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72  nction used to r
3d5d3 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 2a 2f  eclaim memory */
3d5d4 0d 0a 29 7b 0d 0a 20 20 79 79 50 61 72 73 65 72  ..){..  yyParser
3d5d5 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79 50   *pParser = (yyP
3d5d6 61 72 73 65 72 2a 29 70 3b 0d 0a 20 20 2f 2a 20  arser*)p;..  /* 
3d5d7 49 6e 20 53 51 4c 69 74 65 2c 20 77 65 20 6e 65  In SQLite, we ne
3d5d8 76 65 72 20 74 72 79 20 74 6f 20 64 65 73 74 72  ver try to destr
3d5d9 6f 79 20 61 20 70 61 72 73 65 72 20 74 68 61 74  oy a parser that
3d5da 20 77 61 73 20 6e 6f 74 20 73 75 63 63 65 73 73   was not success
3d5db 66 75 6c 6c 79 0d 0a 20 20 2a 2a 20 63 72 65 61  fully..  ** crea
3d5dc 74 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ted in the first
3d5dd 20 70 6c 61 63 65 2e 20 2a 2f 0d 0a 20 20 69 66   place. */..  if
3d5de 28 20 4e 45 56 45 52 28 70 50 61 72 73 65 72 3d  ( NEVER(pParser=
3d5df 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20  =0) ) return;.. 
3d5e0 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 72 2d   while( pParser-
3d5e1 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
3d5e2 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
3d5e3 70 50 61 72 73 65 72 29 3b 0d 0a 23 69 66 20 59  pParser);..#if Y
3d5e4 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0d 0a  YSTACKDEPTH<=0..
3d5e5 20 20 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e    free(pParser->
3d5e6 79 79 73 74 61 63 6b 29 3b 0d 0a 23 65 6e 64 69  yystack);..#endi
3d5e7 66 0d 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29  f..  (*freeProc)
3d5e8 28 28 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29  ((void*)pParser)
3d5e9 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
3d5ea 65 74 75 72 6e 20 74 68 65 20 70 65 61 6b 20 64  eturn the peak d
3d5eb 65 70 74 68 20 6f 66 20 74 68 65 20 73 74 61 63  epth of the stac
3d5ec 6b 20 66 6f 72 20 61 20 70 61 72 73 65 72 2e 0d  k for a parser..
3d5ed 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 59 59 54 52  .*/..#ifdef YYTR
3d5ee 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48  ACKMAXSTACKDEPTH
3d5ef 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
3d5f0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73   int sqlite3Pars
3d5f1 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64  erStackPeak(void
3d5f2 20 2a 70 29 7b 0d 0a 20 20 79 79 50 61 72 73 65   *p){..  yyParse
3d5f3 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79  r *pParser = (yy
3d5f4 50 61 72 73 65 72 2a 29 70 3b 0d 0a 20 20 72 65  Parser*)p;..  re
3d5f5 74 75 72 6e 20 70 50 61 72 73 65 72 2d 3e 79 79  turn pParser->yy
3d5f6 69 64 78 4d 61 78 3b 0d 0a 7d 0d 0a 23 65 6e 64  idxMax;..}..#end
3d5f7 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 69 6e  if..../*..** Fin
3d5f8 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  d the appropriat
3d5f9 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70  e action for a p
3d5fa 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65 20  arser given the 
3d5fb 74 65 72 6d 69 6e 61 6c 0d 0a 2a 2a 20 6c 6f 6f  terminal..** loo
3d5fc 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c  k-ahead token iL
3d5fd 6f 6f 6b 41 68 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a  ookAhead...**..*
3d5fe 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68  * If the look-ah
3d5ff 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e  ead token is YYN
3d600 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63  OCODE, then chec
3d601 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
3d602 61 63 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 69 6e  action is..** in
3d603 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65  dependent of the
3d604 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66   look-ahead.  If
3d605 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74   it is, return t
3d606 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72  he action, other
3d607 77 69 73 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 20  wise..** return 
3d608 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0d 0a 2a  YY_NO_ACTION...*
3d609 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79  /..static int yy
3d60a 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69  _find_shift_acti
3d60b 6f 6e 28 0d 0a 20 20 79 79 50 61 72 73 65 72 20  on(..  yyParser 
3d60c 2a 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20  *pParser,       
3d60d 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
3d60e 2f 0d 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  /..  YYCODETYPE 
3d60f 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f  iLookAhead     /
3d610 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  * The look-ahead
3d611 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20   token */..){.. 
3d612 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74 20 73   int i;..  int s
3d613 74 61 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72  tateno = pParser
3d614 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
3d615 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e  r->yyidx].staten
3d616 6f 3b 0d 0a 20 0d 0a 20 20 69 66 28 20 73 74 61  o;.. ..  if( sta
3d617 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54 5f 43 4f  teno>YY_SHIFT_CO
3d618 55 4e 54 0d 0a 20 20 20 7c 7c 20 28 69 20 3d 20  UNT..   || (i = 
3d619 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73 74  yy_shift_ofst[st
3d61a 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46  ateno])==YY_SHIF
3d61b 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0d 0a 20  T_USE_DFLT ){.. 
3d61c 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
3d61d 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0d 0a  ault[stateno];..
3d61e 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28 20 69    }..  assert( i
3d61f 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
3d620 4f 44 45 20 29 3b 0d 0a 20 20 69 20 2b 3d 20 69  ODE );..  i += i
3d621 4c 6f 6f 6b 41 68 65 61 64 3b 0d 0a 20 20 69 66  LookAhead;..  if
3d622 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 41  ( i<0 || i>=YY_A
3d623 43 54 54 41 42 5f 43 4f 55 4e 54 20 7c 7c 20 79  CTTAB_COUNT || y
3d624 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
3d625 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0d 0a 20  iLookAhead ){.. 
3d626 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61     if( iLookAhea
3d627 64 3e 30 20 29 7b 0d 0a 23 69 66 64 65 66 20 59  d>0 ){..#ifdef Y
3d628 59 46 41 4c 4c 42 41 43 4b 0d 0a 20 20 20 20 20  YFALLBACK..     
3d629 20 59 59 43 4f 44 45 54 59 50 45 20 69 46 61 6c   YYCODETYPE iFal
3d62a 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  lback;          
3d62b 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20 74 6f    /* Fallback to
3d62c 6b 65 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66  ken */..      if
3d62d 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69 7a  ( iLookAhead<siz
3d62e 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29 2f  eof(yyFallback)/
3d62f 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63  sizeof(yyFallbac
3d630 6b 5b 30 5d 29 0d 0a 20 20 20 20 20 20 20 20 20  k[0])..         
3d631 20 20 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63      && (iFallbac
3d632 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69  k = yyFallback[i
3d633 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29  LookAhead])!=0 )
3d634 7b 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  {..#ifndef NDEBU
3d635 47 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 79  G..        if( y
3d636 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0d 0a 20  yTraceFILE ){.. 
3d637 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
3d638 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25  (yyTraceFILE, "%
3d639 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20  sFALLBACK %s => 
3d63a 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20  %s\n",..        
3d63b 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d       yyTraceProm
3d63c 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  pt, yyTokenName[
3d63d 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54  iLookAhead], yyT
3d63e 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62 61  okenName[iFallba
3d63f 63 6b 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d  ck]);..        }
3d640 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20 20  ..#endif..      
3d641 20 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64    return yy_find
3d642 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50  _shift_action(pP
3d643 61 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b  arser, iFallback
3d644 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e  );..      }..#en
3d645 64 69 66 0d 0a 23 69 66 64 65 66 20 59 59 57 49  dif..#ifdef YYWI
3d646 4c 44 43 41 52 44 0d 0a 20 20 20 20 20 20 7b 0d  LDCARD..      {.
3d647 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 20 3d  .        int j =
3d648 20 69 20 2d 20 69 4c 6f 6f 6b 41 68 65 61 64 20   i - iLookAhead 
3d649 2b 20 59 59 57 49 4c 44 43 41 52 44 3b 0d 0a 20  + YYWILDCARD;.. 
3d64a 20 20 20 20 20 20 20 69 66 28 20 0d 0a 23 69 66         if( ..#if
3d64b 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e 2b 59 59   YY_SHIFT_MIN+YY
3d64c 57 49 4c 44 43 41 52 44 3c 30 0d 0a 20 20 20 20  WILDCARD<0..    
3d64d 20 20 20 20 20 20 6a 3e 3d 30 20 26 26 0d 0a 23        j>=0 &&..#
3d64e 65 6e 64 69 66 0d 0a 23 69 66 20 59 59 5f 53 48  endif..#if YY_SH
3d64f 49 46 54 5f 4d 41 58 2b 59 59 57 49 4c 44 43 41  IFT_MAX+YYWILDCA
3d650 52 44 3e 3d 59 59 5f 41 43 54 54 41 42 5f 43 4f  RD>=YY_ACTTAB_CO
3d651 55 4e 54 0d 0a 20 20 20 20 20 20 20 20 20 20 6a  UNT..          j
3d652 3c 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54  <YY_ACTTAB_COUNT
3d653 20 26 26 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20   &&..#endif..   
3d654 20 20 20 20 20 20 20 79 79 5f 6c 6f 6f 6b 61 68         yy_lookah
3d655 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41  ead[j]==YYWILDCA
3d656 52 44 0d 0a 20 20 20 20 20 20 20 20 29 7b 0d 0a  RD..        ){..
3d657 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
3d658 20 20 20 20 20 20 20 20 20 20 69 66 28 20 79 79            if( yy
3d659 54 72 61 63 65 46 49 4c 45 20 29 7b 0d 0a 20 20  TraceFILE ){..  
3d65a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
3d65b 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22  f(yyTraceFILE, "
3d65c 25 73 57 49 4c 44 43 41 52 44 20 25 73 20 3d 3e  %sWILDCARD %s =>
3d65d 20 25 73 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20   %s\n",..       
3d65e 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
3d65f 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61  rompt, yyTokenNa
3d660 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20  me[iLookAhead], 
3d661 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59 59 57 49  yyTokenName[YYWI
3d662 4c 44 43 41 52 44 5d 29 3b 0d 0a 20 20 20 20 20  LDCARD]);..     
3d663 20 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 20 2f       }..#endif /
3d664 2a 20 4e 44 45 42 55 47 20 2a 2f 0d 0a 20 20 20  * NDEBUG */..   
3d665 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 79 79         return yy
3d666 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0d 0a 20 20 20  _action[j];..   
3d667 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d       }..      }.
3d668 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 57 49 4c  .#endif /* YYWIL
3d669 44 43 41 52 44 20 2a 2f 0d 0a 20 20 20 20 7d 0d  DCARD */..    }.
3d66a 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
3d66b 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
3d66c 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
3d66d 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
3d66e 5b 69 5d 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  [i];..  }..}....
3d66f 2f 2a 0d 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  /*..** Find the 
3d670 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69  appropriate acti
3d671 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72 20  on for a parser 
3d672 67 69 76 65 6e 20 74 68 65 20 6e 6f 6e 2d 74 65  given the non-te
3d673 72 6d 69 6e 61 6c 0d 0a 2a 2a 20 6c 6f 6f 6b 2d  rminal..** look-
3d674 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f  ahead token iLoo
3d675 6b 41 68 65 61 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  kAhead...**..** 
3d676 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  If the look-ahea
3d677 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43  d token is YYNOC
3d678 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20  ODE, then check 
3d679 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63  to see if the ac
3d67a 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20 69 6e 64 65  tion is..** inde
3d67b 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c  pendent of the l
3d67c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69  ook-ahead.  If i
3d67d 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65  t is, return the
3d67e 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69   action, otherwi
3d67f 73 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59  se..** return YY
3d680 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0d 0a 2a 2f 0d  _NO_ACTION...*/.
3d681 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66  .static int yy_f
3d682 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f  ind_reduce_actio
3d683 6e 28 0d 0a 20 20 69 6e 74 20 73 74 61 74 65 6e  n(..  int staten
3d684 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
3d685 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65  /* Current state
3d686 20 6e 75 6d 62 65 72 20 2a 2f 0d 0a 20 20 59 59   number */..  YY
3d687 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68  CODETYPE iLookAh
3d688 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c  ead     /* The l
3d689 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
3d68a 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  */..){..  int i;
3d68b 0d 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52  ..#ifdef YYERROR
3d68c 53 59 4d 42 4f 4c 0d 0a 20 20 69 66 28 20 73 74  SYMBOL..  if( st
3d68d 61 74 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f  ateno>YY_REDUCE_
3d68e 43 4f 55 4e 54 20 29 7b 0d 0a 20 20 20 20 72 65  COUNT ){..    re
3d68f 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
3d690 73 74 61 74 65 6e 6f 5d 3b 0d 0a 20 20 7d 0d 0a  stateno];..  }..
3d691 23 65 6c 73 65 0d 0a 20 20 61 73 73 65 72 74 28  #else..  assert(
3d692 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52 45 44   stateno<=YY_RED
3d693 55 43 45 5f 43 4f 55 4e 54 20 29 3b 0d 0a 23 65  UCE_COUNT );..#e
3d694 6e 64 69 66 0d 0a 20 20 69 20 3d 20 79 79 5f 72  ndif..  i = yy_r
3d695 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61 74 65  educe_ofst[state
3d696 6e 6f 5d 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  no];..  assert( 
3d697 69 21 3d 59 59 5f 52 45 44 55 43 45 5f 55 53 45  i!=YY_REDUCE_USE
3d698 5f 44 46 4c 54 20 29 3b 0d 0a 20 20 61 73 73 65  _DFLT );..  asse
3d699 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d  rt( iLookAhead!=
3d69a 59 59 4e 4f 43 4f 44 45 20 29 3b 0d 0a 20 20 69  YYNOCODE );..  i
3d69b 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0d   += iLookAhead;.
3d69c 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
3d69d 59 4d 42 4f 4c 0d 0a 20 20 69 66 28 20 69 3c 30  YMBOL..  if( i<0
3d69e 20 7c 7c 20 69 3e 3d 59 59 5f 41 43 54 54 41 42   || i>=YY_ACTTAB
3d69f 5f 43 4f 55 4e 54 20 7c 7c 20 79 79 5f 6c 6f 6f  _COUNT || yy_loo
3d6a0 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b  kahead[i]!=iLook
3d6a1 41 68 65 61 64 20 29 7b 0d 0a 20 20 20 20 72 65  Ahead ){..    re
3d6a2 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
3d6a3 73 74 61 74 65 6e 6f 5d 3b 0d 0a 20 20 7d 0d 0a  stateno];..  }..
3d6a4 23 65 6c 73 65 0d 0a 20 20 61 73 73 65 72 74 28  #else..  assert(
3d6a5 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 41 43   i>=0 && i<YY_AC
3d6a6 54 54 41 42 5f 43 4f 55 4e 54 20 29 3b 0d 0a 20  TTAB_COUNT );.. 
3d6a7 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b   assert( yy_look
3d6a8 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41  ahead[i]==iLookA
3d6a9 68 65 61 64 20 29 3b 0d 0a 23 65 6e 64 69 66 0d  head );..#endif.
3d6aa 0a 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74  .  return yy_act
3d6ab 69 6f 6e 5b 69 5d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ion[i];..}..../*
3d6ac 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
3d6ad 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  ng routine is ca
3d6ae 6c 6c 65 64 20 69 66 20 74 68 65 20 73 74 61 63  lled if the stac
3d6af 6b 20 6f 76 65 72 66 6c 6f 77 73 2e 0d 0a 2a 2f  k overflows...*/
3d6b0 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  ..static void yy
3d6b1 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79  StackOverflow(yy
3d6b2 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
3d6b3 72 2c 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a  r, YYMINORTYPE *
3d6b4 79 79 70 4d 69 6e 6f 72 29 7b 0d 0a 20 20 20 73  yypMinor){..   s
3d6b5 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
3d6b6 46 45 54 43 48 3b 0d 0a 20 20 20 79 79 70 50 61  FETCH;..   yypPa
3d6b7 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0d 0a  rser->yyidx--;..
3d6b8 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a  #ifndef NDEBUG..
3d6b9 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
3d6ba 4c 45 20 29 7b 0d 0a 20 20 20 20 20 66 70 72 69  LE ){..     fpri
3d6bb 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
3d6bc 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c 6f  "%sStack Overflo
3d6bd 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  w!\n",yyTracePro
3d6be 6d 70 74 29 3b 0d 0a 20 20 20 7d 0d 0a 23 65 6e  mpt);..   }..#en
3d6bf 64 69 66 0d 0a 20 20 20 77 68 69 6c 65 28 20 79  dif..   while( y
3d6c0 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
3d6c1 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73  =0 ) yy_pop_pars
3d6c2 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73  er_stack(yypPars
3d6c3 65 72 29 3b 0d 0a 20 20 20 2f 2a 20 48 65 72 65  er);..   /* Here
3d6c4 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65   code is inserte
3d6c5 64 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78 65  d which will exe
3d6c6 63 75 74 65 20 69 66 20 74 68 65 20 70 61 72 73  cute if the pars
3d6c7 65 72 0d 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20  er..   ** stack 
3d6c8 65 76 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20  every overflows 
3d6c9 2a 2f 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50  */....  UNUSED_P
3d6ca 41 52 41 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f  ARAMETER(yypMino
3d6cb 72 29 3b 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73  r); /* Silence s
3d6cc 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ome compiler war
3d6cd 6e 69 6e 67 73 20 2a 2f 0d 0a 20 20 73 71 6c 69  nings */..  sqli
3d6ce 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
3d6cf 73 65 2c 20 22 70 61 72 73 65 72 20 73 74 61 63  se, "parser stac
3d6d0 6b 20 6f 76 65 72 66 6c 6f 77 22 29 3b 0d 0a 20  k overflow");.. 
3d6d1 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
3d6d2 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
3d6d3 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
3d6d4 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
3d6d5 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a  a_argument var *
3d6d6 2f 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50  /..}..../*..** P
3d6d7 65 72 66 6f 72 6d 20 61 20 73 68 69 66 74 20 61  erform a shift a
3d6d8 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ction...*/..stat
3d6d9 69 63 20 76 6f 69 64 20 79 79 5f 73 68 69 66 74  ic void yy_shift
3d6da 28 0d 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79  (..  yyParser *y
3d6db 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20  ypParser,       
3d6dc 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
3d6dd 20 74 6f 20 62 65 20 73 68 69 66 74 65 64 20 2a   to be shifted *
3d6de 2f 0d 0a 20 20 69 6e 74 20 79 79 4e 65 77 53 74  /..  int yyNewSt
3d6df 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
3d6e0 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 73 74     /* The new st
3d6e1 61 74 65 20 74 6f 20 73 68 69 66 74 20 69 6e 20  ate to shift in 
3d6e2 2a 2f 0d 0a 20 20 69 6e 74 20 79 79 4d 61 6a 6f  */..  int yyMajo
3d6e3 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
3d6e4 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72      /* The major
3d6e5 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74 20   token to shift 
3d6e6 69 6e 20 2a 2f 0d 0a 20 20 59 59 4d 49 4e 4f 52  in */..  YYMINOR
3d6e7 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 20 20  TYPE *yypMinor  
3d6e8 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
3d6e9 72 20 74 6f 20 74 68 65 20 6d 69 6e 6f 72 20 74  r to the minor t
3d6ea 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e  oken to shift in
3d6eb 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 79 79 53 74 61   */..){..  yySta
3d6ec 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73 3b 0d  ckEntry *yytos;.
3d6ed 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  .  yypParser->yy
3d6ee 69 64 78 2b 2b 3b 0d 0a 23 69 66 64 65 66 20 59  idx++;..#ifdef Y
3d6ef 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
3d6f0 50 54 48 0d 0a 20 20 69 66 28 20 79 79 70 50 61  PTH..  if( yypPa
3d6f1 72 73 65 72 2d 3e 79 79 69 64 78 3e 79 79 70 50  rser->yyidx>yypP
3d6f2 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20  arser->yyidxMax 
3d6f3 29 7b 0d 0a 20 20 20 20 79 79 70 50 61 72 73 65  ){..    yypParse
3d6f4 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 79 79  r->yyidxMax = yy
3d6f5 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 0d  pParser->yyidx;.
3d6f6 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  .  }..#endif..#i
3d6f7 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30  f YYSTACKDEPTH>0
3d6f8 20 0d 0a 20 20 69 66 28 20 79 79 70 50 61 72 73   ..  if( yypPars
3d6f9 65 72 2d 3e 79 79 69 64 78 3e 3d 59 59 53 54 41  er->yyidx>=YYSTA
3d6fa 43 4b 44 45 50 54 48 20 29 7b 0d 0a 20 20 20 20  CKDEPTH ){..    
3d6fb 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28  yyStackOverflow(
3d6fc 79 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d 69  yypParser, yypMi
3d6fd 6e 6f 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  nor);..    retur
3d6fe 6e 3b 0d 0a 20 20 7d 0d 0a 23 65 6c 73 65 0d 0a  n;..  }..#else..
3d6ff 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
3d700 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73 65  >yyidx>=yypParse
3d701 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0d 0a 20  r->yystksz ){.. 
3d702 20 20 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79     yyGrowStack(y
3d703 79 70 50 61 72 73 65 72 29 3b 0d 0a 20 20 20 20  ypParser);..    
3d704 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
3d705 79 69 64 78 3e 3d 79 79 70 50 61 72 73 65 72 2d  yidx>=yypParser-
3d706 3e 79 79 73 74 6b 73 7a 20 29 7b 0d 0a 20 20 20  >yystksz ){..   
3d707 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
3d708 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 79 79  ow(yypParser, yy
3d709 70 4d 69 6e 6f 72 29 3b 0d 0a 20 20 20 20 20 20  pMinor);..      
3d70a 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a  return;..    }..
3d70b 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 79    }..#endif..  y
3d70c 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65  ytos = &yypParse
3d70d 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
3d70e 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0d 0a 20  rser->yyidx];.. 
3d70f 20 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20   yytos->stateno 
3d710 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29  = (YYACTIONTYPE)
3d711 79 79 4e 65 77 53 74 61 74 65 3b 0d 0a 20 20 79  yyNewState;..  y
3d712 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59  ytos->major = (Y
3d713 59 43 4f 44 45 54 59 50 45 29 79 79 4d 61 6a 6f  YCODETYPE)yyMajo
3d714 72 3b 0d 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e  r;..  yytos->min
3d715 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0d  or = *yypMinor;.
3d716 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d  .#ifndef NDEBUG.
3d717 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3d718 4c 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d  LE && yypParser-
3d719 3e 79 79 69 64 78 3e 30 20 29 7b 0d 0a 20 20 20  >yyidx>0 ){..   
3d71a 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 66 70 72   int i;..    fpr
3d71b 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
3d71c 2c 22 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c  ,"%sShift %d\n",
3d71d 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79  yyTracePrompt,yy
3d71e 4e 65 77 53 74 61 74 65 29 3b 0d 0a 20 20 20 20  NewState);..    
3d71f 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
3d720 49 4c 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79  ILE,"%sStack:",y
3d721 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0d 0a  yTracePrompt);..
3d722 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
3d723 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
3d724 3b 20 69 2b 2b 29 0d 0a 20 20 20 20 20 20 66 70  ; i++)..      fp
3d725 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
3d726 45 2c 22 20 25 73 22 2c 79 79 54 6f 6b 65 6e 4e  E," %s",yyTokenN
3d727 61 6d 65 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ame[yypParser->y
3d728 79 73 74 61 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d  ystack[i].major]
3d729 29 3b 0d 0a 20 20 20 20 66 70 72 69 6e 74 66 28  );..    fprintf(
3d72a 79 79 54 72 61 63 65 46 49 4c 45 2c 22 5c 6e 22  yyTraceFILE,"\n"
3d72b 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  );..  }..#endif.
3d72c 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68 65 20 66 6f 6c  .}..../* The fol
3d72d 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 63 6f 6e  lowing table con
3d72e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tains informatio
3d72f 6e 20 61 62 6f 75 74 20 65 76 65 72 79 20 72 75  n about every ru
3d730 6c 65 20 74 68 61 74 0d 0a 2a 2a 20 69 73 20 75  le that..** is u
3d731 73 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 72  sed during the r
3d732 65 64 75 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  educe...*/..stat
3d733 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
3d734 7b 0d 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  {..  YYCODETYPE 
3d735 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lhs;         /* 
3d736 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65  Symbol on the le
3d737 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
3d738 74 68 65 20 72 75 6c 65 20 2a 2f 0d 0a 20 20 75  the rule */..  u
3d739 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68  nsigned char nrh
3d73a 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  s;     /* Number
3d73b 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73   of right-hand s
3d73c 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74  ide symbols in t
3d73d 68 65 20 72 75 6c 65 20 2a 2f 0d 0a 7d 20 79 79  he rule */..} yy
3d73e 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0d 0a  RuleInfo[] = {..
3d73f 20 20 7b 20 31 34 32 2c 20 31 20 7d 2c 0d 0a 20    { 142, 1 },.. 
3d740 20 7b 20 31 34 33 2c 20 32 20 7d 2c 0d 0a 20 20   { 143, 2 },..  
3d741 7b 20 31 34 33 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 143, 1 },..  {
3d742 20 31 34 34 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20   144, 1 },..  { 
3d743 31 34 34 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31  144, 3 },..  { 1
3d744 34 35 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 34  45, 0 },..  { 14
3d745 35 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 35  5, 1 },..  { 145
3d746 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 34 36 2c  , 3 },..  { 146,
3d747 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20   1 },..  { 147, 
3d748 33 20 7d 2c 0d 0a 20 20 7b 20 31 34 39 2c 20 30  3 },..  { 149, 0
3d749 20 7d 2c 0d 0a 20 20 7b 20 31 34 39 2c 20 31 20   },..  { 149, 1 
3d74a 7d 2c 0d 0a 20 20 7b 20 31 34 39 2c 20 32 20 7d  },..  { 149, 2 }
3d74b 2c 0d 0a 20 20 7b 20 31 34 38 2c 20 30 20 7d 2c  ,..  { 148, 0 },
3d74c 0d 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0d  ..  { 148, 1 },.
3d74d 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0d 0a  .  { 148, 1 },..
3d74e 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0d 0a 20    { 148, 1 },.. 
3d74f 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0d 0a 20 20   { 147, 2 },..  
3d750 7b 20 31 34 37 2c 20 32 20 7d 2c 0d 0a 20 20 7b  { 147, 2 },..  {
3d751 20 31 34 37 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20   147, 2 },..  { 
3d752 31 35 31 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31  151, 1 },..  { 1
3d753 35 31 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 34  51, 0 },..  { 14
3d754 37 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 34 37  7, 2 },..  { 147
3d755 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c  , 3 },..  { 147,
3d756 20 35 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20   5 },..  { 147, 
3d757 32 20 7d 2c 0d 0a 20 20 7b 20 31 35 32 2c 20 36  2 },..  { 152, 6
3d758 20 7d 2c 0d 0a 20 20 7b 20 31 35 34 2c 20 31 20   },..  { 154, 1 
3d759 7d 2c 0d 0a 20 20 7b 20 31 35 36 2c 20 30 20 7d  },..  { 156, 0 }
3d75a 2c 0d 0a 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c  ,..  { 156, 3 },
3d75b 0d 0a 20 20 7b 20 31 35 35 2c 20 31 20 7d 2c 0d  ..  { 155, 1 },.
3d75c 0a 20 20 7b 20 31 35 35 2c 20 30 20 7d 2c 0d 0a  .  { 155, 0 },..
3d75d 20 20 7b 20 31 35 33 2c 20 34 20 7d 2c 0d 0a 20    { 153, 4 },.. 
3d75e 20 7b 20 31 35 33 2c 20 32 20 7d 2c 0d 0a 20 20   { 153, 2 },..  
3d75f 7b 20 31 35 38 2c 20 33 20 7d 2c 0d 0a 20 20 7b  { 158, 3 },..  {
3d760 20 31 35 38 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20   158, 1 },..  { 
3d761 31 36 31 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31  161, 3 },..  { 1
3d762 36 32 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 36  62, 1 },..  { 16
3d763 35 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 36 35  5, 1 },..  { 165
3d764 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 36 36 2c  , 1 },..  { 166,
3d765 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 35 30 2c 20   1 },..  { 150, 
3d766 31 20 7d 2c 0d 0a 20 20 7b 20 31 35 30 2c 20 31  1 },..  { 150, 1
3d767 20 7d 2c 0d 0a 20 20 7b 20 31 35 30 2c 20 31 20   },..  { 150, 1 
3d768 7d 2c 0d 0a 20 20 7b 20 31 36 33 2c 20 30 20 7d  },..  { 163, 0 }
3d769 2c 0d 0a 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c  ,..  { 163, 1 },
3d76a 0d 0a 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c 0d  ..  { 167, 1 },.
3d76b 0a 20 20 7b 20 31 36 37 2c 20 34 20 7d 2c 0d 0a  .  { 167, 4 },..
3d76c 20 20 7b 20 31 36 37 2c 20 36 20 7d 2c 0d 0a 20    { 167, 6 },.. 
3d76d 20 7b 20 31 36 38 2c 20 31 20 7d 2c 0d 0a 20 20   { 168, 1 },..  
3d76e 7b 20 31 36 38 2c 20 32 20 7d 2c 0d 0a 20 20 7b  { 168, 2 },..  {
3d76f 20 31 36 39 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20   169, 1 },..  { 
3d770 31 36 39 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31  169, 1 },..  { 1
3d771 36 34 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 36  64, 2 },..  { 16
3d772 34 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 37 32  4, 0 },..  { 172
3d773 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 32 2c  , 3 },..  { 172,
3d774 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 33 2c 20   1 },..  { 173, 
3d775 32 20 7d 2c 0d 0a 20 20 7b 20 31 37 33 2c 20 34  2 },..  { 173, 4
3d776 20 7d 2c 0d 0a 20 20 7b 20 31 37 33 2c 20 33 20   },..  { 173, 3 
3d777 7d 2c 0d 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d  },..  { 173, 3 }
3d778 2c 0d 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  ,..  { 173, 2 },
3d779 0d 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0d  ..  { 173, 2 },.
3d77a 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0d 0a  .  { 173, 3 },..
3d77b 20 20 7b 20 31 37 33 2c 20 35 20 7d 2c 0d 0a 20    { 173, 5 },.. 
3d77c 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0d 0a 20 20   { 173, 2 },..  
3d77d 7b 20 31 37 33 2c 20 34 20 7d 2c 0d 0a 20 20 7b  { 173, 4 },..  {
3d77e 20 31 37 33 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20   173, 4 },..  { 
3d77f 31 37 33 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31  173, 1 },..  { 1
3d780 37 33 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 37  73, 2 },..  { 17
3d781 38 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 37 38  8, 0 },..  { 178
3d782 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 38 30 2c  , 1 },..  { 180,
3d783 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 38 30 2c 20   0 },..  { 180, 
3d784 32 20 7d 2c 0d 0a 20 20 7b 20 31 38 32 2c 20 32  2 },..  { 182, 2
3d785 20 7d 2c 0d 0a 20 20 7b 20 31 38 32 2c 20 33 20   },..  { 182, 3 
3d786 7d 2c 0d 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d  },..  { 182, 3 }
3d787 2c 0d 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c  ,..  { 182, 3 },
3d788 0d 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0d  ..  { 183, 2 },.
3d789 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0d 0a  .  { 183, 2 },..
3d78a 20 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0d 0a 20    { 183, 1 },.. 
3d78b 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0d 0a 20 20   { 183, 1 },..  
3d78c 7b 20 31 38 33 2c 20 32 20 7d 2c 0d 0a 20 20 7b  { 183, 2 },..  {
3d78d 20 31 38 31 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20   181, 3 },..  { 
3d78e 31 38 31 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31  181, 2 },..  { 1
3d78f 38 34 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 38  84, 0 },..  { 18
3d790 34 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 38 34  4, 2 },..  { 184
3d791 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 35 39 2c  , 2 },..  { 159,
3d792 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 35 39 2c 20   0 },..  { 159, 
3d793 32 20 7d 2c 0d 0a 20 20 7b 20 31 38 35 2c 20 33  2 },..  { 185, 3
3d794 20 7d 2c 0d 0a 20 20 7b 20 31 38 35 2c 20 32 20   },..  { 185, 2 
3d795 7d 2c 0d 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d  },..  { 185, 1 }
3d796 2c 0d 0a 20 20 7b 20 31 38 36 2c 20 32 20 7d 2c  ,..  { 186, 2 },
3d797 0d 0a 20 20 7b 20 31 38 36 2c 20 37 20 7d 2c 0d  ..  { 186, 7 },.
3d798 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0d 0a  .  { 186, 5 },..
3d799 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0d 0a 20    { 186, 5 },.. 
3d79a 20 7b 20 31 38 36 2c 20 31 30 20 7d 2c 0d 0a 20   { 186, 10 },.. 
3d79b 20 7b 20 31 38 38 2c 20 30 20 7d 2c 0d 0a 20 20   { 188, 0 },..  
3d79c 7b 20 31 38 38 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 188, 1 },..  {
3d79d 20 31 37 36 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20   176, 0 },..  { 
3d79e 31 37 36 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31  176, 3 },..  { 1
3d79f 38 39 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 38  89, 0 },..  { 18
3d7a0 39 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 39 30  9, 2 },..  { 190
3d7a1 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 30 2c  , 1 },..  { 190,
3d7a2 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 30 2c 20   1 },..  { 190, 
3d7a3 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 34  1 },..  { 147, 4
3d7a4 20 7d 2c 0d 0a 20 20 7b 20 31 39 32 2c 20 32 20   },..  { 192, 2 
3d7a5 7d 2c 0d 0a 20 20 7b 20 31 39 32 2c 20 30 20 7d  },..  { 192, 0 }
3d7a6 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c  ,..  { 147, 8 },
3d7a7 0d 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0d  ..  { 147, 4 },.
3d7a8 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0d 0a  .  { 147, 1 },..
3d7a9 20 20 7b 20 31 36 30 2c 20 31 20 7d 2c 0d 0a 20    { 160, 1 },.. 
3d7aa 20 7b 20 31 36 30 2c 20 33 20 7d 2c 0d 0a 20 20   { 160, 3 },..  
3d7ab 7b 20 31 39 35 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 195, 1 },..  {
3d7ac 20 31 39 35 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20   195, 2 },..  { 
3d7ad 31 39 35 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31  195, 1 },..  { 1
3d7ae 39 34 2c 20 39 20 7d 2c 0d 0a 20 20 7b 20 31 39  94, 9 },..  { 19
3d7af 36 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 36  6, 1 },..  { 196
3d7b0 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 36 2c  , 1 },..  { 196,
3d7b1 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 30 34 2c 20   0 },..  { 204, 
3d7b2 32 20 7d 2c 0d 0a 20 20 7b 20 32 30 34 2c 20 30  2 },..  { 204, 0
3d7b3 20 7d 2c 0d 0a 20 20 7b 20 31 39 37 2c 20 33 20   },..  { 197, 3 
3d7b4 7d 2c 0d 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d  },..  { 197, 2 }
3d7b5 2c 0d 0a 20 20 7b 20 31 39 37 2c 20 34 20 7d 2c  ,..  { 197, 4 },
3d7b6 0d 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d 2c 0d  ..  { 205, 2 },.
3d7b7 0a 20 20 7b 20 32 30 35 2c 20 31 20 7d 2c 0d 0a  .  { 205, 1 },..
3d7b8 20 20 7b 20 32 30 35 2c 20 30 20 7d 2c 0d 0a 20    { 205, 0 },.. 
3d7b9 20 7b 20 31 39 38 2c 20 30 20 7d 2c 0d 0a 20 20   { 198, 0 },..  
3d7ba 7b 20 31 39 38 2c 20 32 20 7d 2c 0d 0a 20 20 7b  { 198, 2 },..  {
3d7bb 20 32 30 37 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20   207, 2 },..  { 
3d7bc 32 30 37 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32  207, 0 },..  { 2
3d7bd 30 36 2c 20 37 20 7d 2c 0d 0a 20 20 7b 20 32 30  06, 7 },..  { 20
3d7be 36 2c 20 37 20 7d 2c 0d 0a 20 20 7b 20 32 30 36  6, 7 },..  { 206
3d7bf 2c 20 37 20 7d 2c 0d 0a 20 20 7b 20 31 35 37 2c  , 7 },..  { 157,
3d7c0 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 35 37 2c 20   0 },..  { 157, 
3d7c1 32 20 7d 2c 0d 0a 20 20 7b 20 31 39 33 2c 20 32  2 },..  { 193, 2
3d7c2 20 7d 2c 0d 0a 20 20 7b 20 32 30 38 2c 20 31 20   },..  { 208, 1 
3d7c3 7d 2c 0d 0a 20 20 7b 20 32 30 38 2c 20 32 20 7d  },..  { 208, 2 }
3d7c4 2c 0d 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c  ,..  { 208, 3 },
3d7c5 0d 0a 20 20 7b 20 32 30 38 2c 20 34 20 7d 2c 0d  ..  { 208, 4 },.
3d7c6 0a 20 20 7b 20 32 31 30 2c 20 32 20 7d 2c 0d 0a  .  { 210, 2 },..
3d7c7 20 20 7b 20 32 31 30 2c 20 30 20 7d 2c 0d 0a 20    { 210, 0 },.. 
3d7c8 20 7b 20 32 30 39 2c 20 30 20 7d 2c 0d 0a 20 20   { 209, 0 },..  
3d7c9 7b 20 32 30 39 2c 20 33 20 7d 2c 0d 0a 20 20 7b  { 209, 3 },..  {
3d7ca 20 32 30 39 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20   209, 2 },..  { 
3d7cb 32 31 31 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 32  211, 4 },..  { 2
3d7cc 31 31 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 30  11, 0 },..  { 20
3d7cd 32 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 30 32  2, 0 },..  { 202
3d7ce 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 32 31 34 2c  , 3 },..  { 214,
3d7cf 20 34 20 7d 2c 0d 0a 20 20 7b 20 32 31 34 2c 20   4 },..  { 214, 
3d7d0 32 20 7d 2c 0d 0a 20 20 7b 20 32 31 35 2c 20 31  2 },..  { 215, 1
3d7d1 20 7d 2c 0d 0a 20 20 7b 20 31 37 37 2c 20 31 20   },..  { 177, 1 
3d7d2 7d 2c 0d 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d  },..  { 177, 1 }
3d7d3 2c 0d 0a 20 20 7b 20 31 37 37 2c 20 30 20 7d 2c  ,..  { 177, 0 },
3d7d4 0d 0a 20 20 7b 20 32 30 30 2c 20 30 20 7d 2c 0d  ..  { 200, 0 },.
3d7d5 0a 20 20 7b 20 32 30 30 2c 20 33 20 7d 2c 0d 0a  .  { 200, 3 },..
3d7d6 20 20 7b 20 32 30 31 2c 20 30 20 7d 2c 0d 0a 20    { 201, 0 },.. 
3d7d7 20 7b 20 32 30 31 2c 20 32 20 7d 2c 0d 0a 20 20   { 201, 2 },..  
3d7d8 7b 20 32 30 33 2c 20 30 20 7d 2c 0d 0a 20 20 7b  { 203, 0 },..  {
3d7d9 20 32 30 33 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20   203, 2 },..  { 
3d7da 32 30 33 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 32  203, 4 },..  { 2
3d7db 30 33 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 31 34  03, 4 },..  { 14
3d7dc 37 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 31 39 39  7, 5 },..  { 199
3d7dd 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31 39 39 2c  , 0 },..  { 199,
3d7de 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20   2 },..  { 147, 
3d7df 37 20 7d 2c 0d 0a 20 20 7b 20 32 31 37 2c 20 35  7 },..  { 217, 5
3d7e0 20 7d 2c 0d 0a 20 20 7b 20 32 31 37 2c 20 33 20   },..  { 217, 3 
3d7e1 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d  },..  { 147, 8 }
3d7e2 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  ,..  { 147, 5 },
3d7e3 0d 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0d  ..  { 147, 6 },.
3d7e4 0a 20 20 7b 20 32 31 38 2c 20 32 20 7d 2c 0d 0a  .  { 218, 2 },..
3d7e5 20 20 7b 20 32 31 38 2c 20 31 20 7d 2c 0d 0a 20    { 218, 1 },.. 
3d7e6 20 7b 20 32 32 30 2c 20 33 20 7d 2c 0d 0a 20 20   { 220, 3 },..  
3d7e7 7b 20 32 32 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 220, 1 },..  {
3d7e8 20 32 31 39 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20   219, 0 },..  { 
3d7e9 32 31 39 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 32  219, 3 },..  { 2
3d7ea 31 33 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 32 31  13, 3 },..  { 21
3d7eb 33 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 35  3, 1 },..  { 175
3d7ec 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c  , 1 },..  { 175,
3d7ed 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 34 2c 20   3 },..  { 174, 
3d7ee 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 31  1 },..  { 175, 1
3d7ef 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 31 20   },..  { 175, 1 
3d7f0 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d  },..  { 175, 3 }
3d7f1 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  ,..  { 175, 5 },
3d7f2 0d 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0d  ..  { 174, 1 },.
3d7f3 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0d 0a  .  { 174, 1 },..
3d7f4 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0d 0a 20    { 175, 1 },.. 
3d7f5 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0d 0a 20 20   { 175, 1 },..  
3d7f6 7b 20 31 37 35 2c 20 33 20 7d 2c 0d 0a 20 20 7b  { 175, 3 },..  {
3d7f7 20 31 37 35 2c 20 36 20 7d 2c 0d 0a 20 20 7b 20   175, 6 },..  { 
3d7f8 31 37 35 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 31  175, 5 },..  { 1
3d7f9 37 35 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 31 37  75, 4 },..  { 17
3d7fa 34 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 35  4, 1 },..  { 175
3d7fb 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c  , 3 },..  { 175,
3d7fc 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20   3 },..  { 175, 
3d7fd 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 33  3 },..  { 175, 3
3d7fe 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 33 20   },..  { 175, 3 
3d7ff 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d  },..  { 175, 3 }
3d800 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  ,..  { 175, 3 },
3d801 0d 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0d  ..  { 175, 3 },.
3d802 0a 20 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0d 0a  .  { 222, 1 },..
3d803 20 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0d 0a 20    { 222, 2 },.. 
3d804 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0d 0a 20 20   { 222, 1 },..  
3d805 7b 20 32 32 32 2c 20 32 20 7d 2c 0d 0a 20 20 7b  { 222, 2 },..  {
3d806 20 31 37 35 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20   175, 3 },..  { 
3d807 31 37 35 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 31  175, 5 },..  { 1
3d808 37 35 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 37  75, 2 },..  { 17
3d809 35 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 35  5, 3 },..  { 175
3d80a 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c  , 3 },..  { 175,
3d80b 20 34 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20   4 },..  { 175, 
3d80c 32 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 32  2 },..  { 175, 2
3d80d 20 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 32 20   },..  { 175, 2 
3d80e 7d 2c 0d 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d  },..  { 175, 2 }
3d80f 2c 0d 0a 20 20 7b 20 32 32 33 2c 20 31 20 7d 2c  ,..  { 223, 1 },
3d810 0d 0a 20 20 7b 20 32 32 33 2c 20 32 20 7d 2c 0d  ..  { 223, 2 },.
3d811 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0d 0a  .  { 175, 5 },..
3d812 20 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0d 0a 20    { 224, 1 },.. 
3d813 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0d 0a 20 20   { 224, 2 },..  
3d814 7b 20 31 37 35 2c 20 35 20 7d 2c 0d 0a 20 20 7b  { 175, 5 },..  {
3d815 20 31 37 35 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20   175, 3 },..  { 
3d816 31 37 35 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 31  175, 5 },..  { 1
3d817 37 35 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 31 37  75, 4 },..  { 17
3d818 35 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 31 37 35  5, 4 },..  { 175
3d819 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 32 32 36 2c  , 5 },..  { 226,
3d81a 20 35 20 7d 2c 0d 0a 20 20 7b 20 32 32 36 2c 20   5 },..  { 226, 
3d81b 34 20 7d 2c 0d 0a 20 20 7b 20 32 32 37 2c 20 32  4 },..  { 227, 2
3d81c 20 7d 2c 0d 0a 20 20 7b 20 32 32 37 2c 20 30 20   },..  { 227, 0 
3d81d 7d 2c 0d 0a 20 20 7b 20 32 32 35 2c 20 31 20 7d  },..  { 225, 1 }
3d81e 2c 0d 0a 20 20 7b 20 32 32 35 2c 20 30 20 7d 2c  ,..  { 225, 0 },
3d81f 0d 0a 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0d  ..  { 221, 1 },.
3d820 0a 20 20 7b 20 32 32 31 2c 20 30 20 7d 2c 0d 0a  .  { 221, 0 },..
3d821 20 20 7b 20 32 31 36 2c 20 33 20 7d 2c 0d 0a 20    { 216, 3 },.. 
3d822 20 7b 20 32 31 36 2c 20 31 20 7d 2c 0d 0a 20 20   { 216, 1 },..  
3d823 7b 20 31 34 37 2c 20 31 31 20 7d 2c 0d 0a 20 20  { 147, 11 },..  
3d824 7b 20 32 32 38 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 228, 1 },..  {
3d825 20 32 32 38 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20   228, 0 },..  { 
3d826 31 37 39 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 31  179, 0 },..  { 1
3d827 37 39 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 31 38  79, 3 },..  { 18
3d828 37 2c 20 35 20 7d 2c 0d 0a 20 20 7b 20 31 38 37  7, 5 },..  { 187
3d829 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 32 32 39 2c  , 3 },..  { 229,
3d82a 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 32 39 2c 20   0 },..  { 229, 
3d82b 32 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 34  2 },..  { 147, 4
3d82c 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 31 20   },..  { 147, 1 
3d82d 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d  },..  { 147, 2 }
3d82e 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  ,..  { 147, 3 },
3d82f 0d 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0d  ..  { 147, 5 },.
3d830 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0d 0a  .  { 147, 6 },..
3d831 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0d 0a 20    { 147, 5 },.. 
3d832 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0d 0a 20 20   { 147, 6 },..  
3d833 7b 20 32 33 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 230, 1 },..  {
3d834 20 32 33 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20   230, 1 },..  { 
3d835 32 33 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 32  230, 1 },..  { 2
3d836 33 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 32 33  30, 1 },..  { 23
3d837 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 37 30  0, 1 },..  { 170
3d838 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 31 37 31 2c  , 2 },..  { 171,
3d839 20 32 20 7d 2c 0d 0a 20 20 7b 20 32 33 32 2c 20   2 },..  { 232, 
3d83a 31 20 7d 2c 0d 0a 20 20 7b 20 32 33 31 2c 20 31  1 },..  { 231, 1
3d83b 20 7d 2c 0d 0a 20 20 7b 20 32 33 31 2c 20 30 20   },..  { 231, 0 
3d83c 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d  },..  { 147, 5 }
3d83d 2c 0d 0a 20 20 7b 20 32 33 33 2c 20 31 31 20 7d  ,..  { 233, 11 }
3d83e 2c 0d 0a 20 20 7b 20 32 33 35 2c 20 31 20 7d 2c  ,..  { 235, 1 },
3d83f 0d 0a 20 20 7b 20 32 33 35 2c 20 31 20 7d 2c 0d  ..  { 235, 1 },.
3d840 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c 0d 0a  .  { 235, 2 },..
3d841 20 20 7b 20 32 33 35 2c 20 30 20 7d 2c 0d 0a 20    { 235, 0 },.. 
3d842 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0d 0a 20 20   { 236, 1 },..  
3d843 7b 20 32 33 36 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 236, 1 },..  {
3d844 20 32 33 36 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20   236, 3 },..  { 
3d845 32 33 37 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32  237, 0 },..  { 2
3d846 33 37 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20 32 33  37, 3 },..  { 23
3d847 38 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 33 38  8, 0 },..  { 238
3d848 2c 20 32 20 7d 2c 0d 0a 20 20 7b 20 32 33 34 2c  , 2 },..  { 234,
3d849 20 33 20 7d 2c 0d 0a 20 20 7b 20 32 33 34 2c 20   3 },..  { 234, 
3d84a 32 20 7d 2c 0d 0a 20 20 7b 20 32 34 30 2c 20 31  2 },..  { 240, 1
3d84b 20 7d 2c 0d 0a 20 20 7b 20 32 34 30 2c 20 33 20   },..  { 240, 3 
3d84c 7d 2c 0d 0a 20 20 7b 20 32 34 31 2c 20 30 20 7d  },..  { 241, 0 }
3d84d 2c 0d 0a 20 20 7b 20 32 34 31 2c 20 33 20 7d 2c  ,..  { 241, 3 },
3d84e 0d 0a 20 20 7b 20 32 34 31 2c 20 32 20 7d 2c 0d  ..  { 241, 2 },.
3d84f 0a 20 20 7b 20 32 33 39 2c 20 37 20 7d 2c 0d 0a  .  { 239, 7 },..
3d850 20 20 7b 20 32 33 39 2c 20 38 20 7d 2c 0d 0a 20    { 239, 8 },.. 
3d851 20 7b 20 32 33 39 2c 20 35 20 7d 2c 0d 0a 20 20   { 239, 5 },..  
3d852 7b 20 32 33 39 2c 20 35 20 7d 2c 0d 0a 20 20 7b  { 239, 5 },..  {
3d853 20 32 33 39 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20   239, 1 },..  { 
3d854 31 37 35 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 31  175, 4 },..  { 1
3d855 37 35 2c 20 36 20 7d 2c 0d 0a 20 20 7b 20 31 39  75, 6 },..  { 19
3d856 31 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 31  1, 1 },..  { 191
3d857 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 39 31 2c  , 1 },..  { 191,
3d858 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20   1 },..  { 147, 
3d859 34 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 36  4 },..  { 147, 6
3d85a 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 33 20   },..  { 147, 3 
3d85b 7d 2c 0d 0a 20 20 7b 20 32 34 33 2c 20 30 20 7d  },..  { 243, 0 }
3d85c 2c 0d 0a 20 20 7b 20 32 34 33 2c 20 32 20 7d 2c  ,..  { 243, 2 },
3d85d 0d 0a 20 20 7b 20 32 34 32 2c 20 31 20 7d 2c 0d  ..  { 242, 1 },.
3d85e 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d 2c 0d 0a  .  { 242, 0 },..
3d85f 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0d 0a 20    { 147, 1 },.. 
3d860 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0d 0a 20 20   { 147, 3 },..  
3d861 7b 20 31 34 37 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 147, 1 },..  {
3d862 20 31 34 37 2c 20 33 20 7d 2c 0d 0a 20 20 7b 20   147, 3 },..  { 
3d863 31 34 37 2c 20 36 20 7d 2c 0d 0a 20 20 7b 20 31  147, 6 },..  { 1
3d864 34 37 2c 20 36 20 7d 2c 0d 0a 20 20 7b 20 32 34  47, 6 },..  { 24
3d865 34 2c 20 31 20 7d 2c 0d 0a 20 20 7b 20 32 34 35  4, 1 },..  { 245
3d866 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20 32 34 35 2c  , 0 },..  { 245,
3d867 20 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20   1 },..  { 147, 
3d868 31 20 7d 2c 0d 0a 20 20 7b 20 31 34 37 2c 20 34  1 },..  { 147, 4
3d869 20 7d 2c 0d 0a 20 20 7b 20 32 34 36 2c 20 37 20   },..  { 246, 7 
3d86a 7d 2c 0d 0a 20 20 7b 20 32 34 37 2c 20 31 20 7d  },..  { 247, 1 }
3d86b 2c 0d 0a 20 20 7b 20 32 34 37 2c 20 33 20 7d 2c  ,..  { 247, 3 },
3d86c 0d 0a 20 20 7b 20 32 34 38 2c 20 30 20 7d 2c 0d  ..  { 248, 0 },.
3d86d 0a 20 20 7b 20 32 34 38 2c 20 32 20 7d 2c 0d 0a  .  { 248, 2 },..
3d86e 20 20 7b 20 32 34 39 2c 20 31 20 7d 2c 0d 0a 20    { 249, 1 },.. 
3d86f 20 7b 20 32 34 39 2c 20 33 20 7d 2c 0d 0a 20 20   { 249, 3 },..  
3d870 7b 20 32 35 30 2c 20 31 20 7d 2c 0d 0a 20 20 7b  { 250, 1 },..  {
3d871 20 32 35 31 2c 20 30 20 7d 2c 0d 0a 20 20 7b 20   251, 0 },..  { 
3d872 32 35 31 2c 20 34 20 7d 2c 0d 0a 20 20 7b 20 32  251, 4 },..  { 2
3d873 35 31 2c 20 32 20 7d 2c 0d 0a 7d 3b 0d 0a 0d 0a  51, 2 },..};....
3d874 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61  static void yy_a
3d875 63 63 65 70 74 28 79 79 50 61 72 73 65 72 2a 29  ccept(yyParser*)
3d876 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65  ;  /* Forward De
3d877 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a  claration */....
3d878 2f 2a 0d 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  /*..** Perform a
3d879 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 61   reduce action a
3d87a 6e 64 20 74 68 65 20 73 68 69 66 74 20 74 68 61  nd the shift tha
3d87b 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61 74 65  t must immediate
3d87c 6c 79 0d 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68  ly..** follow th
3d87d 65 20 72 65 64 75 63 65 2e 0d 0a 2a 2f 0d 0a 73  e reduce...*/..s
3d87e 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65  tatic void yy_re
3d87f 64 75 63 65 28 0d 0a 20 20 79 79 50 61 72 73 65  duce(..  yyParse
3d880 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20  r *yypParser,   
3d881 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
3d882 73 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 79 79  ser */..  int yy
3d883 72 75 6c 65 6e 6f 20 20 20 20 20 20 20 20 20 20  ruleno          
3d884 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3d885 20 6f 66 20 74 68 65 20 72 75 6c 65 20 62 79 20   of the rule by 
3d886 77 68 69 63 68 20 74 6f 20 72 65 64 75 63 65 20  which to reduce 
3d887 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 79 79  */..){..  int yy
3d888 67 6f 74 6f 3b 20 20 20 20 20 20 20 20 20 20 20  goto;           
3d889 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3d88a 20 6e 65 78 74 20 73 74 61 74 65 20 2a 2f 0d 0a   next state */..
3d88b 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20 20    int yyact;    
3d88c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d88d 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 61 63    /* The next ac
3d88e 74 69 6f 6e 20 2a 2f 0d 0a 20 20 59 59 4d 49 4e  tion */..  YYMIN
3d88f 4f 52 54 59 50 45 20 79 79 67 6f 74 6f 6d 69 6e  ORTYPE yygotomin
3d890 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  or;        /* Th
3d891 65 20 4c 48 53 20 6f 66 20 74 68 65 20 72 75 6c  e LHS of the rul
3d892 65 20 72 65 64 75 63 65 64 20 2a 2f 0d 0a 20 20  e reduced */..  
3d893 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
3d894 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  msp;            
3d895 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  /* The top of th
3d896 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
3d897 20 2a 2f 0d 0a 20 20 69 6e 74 20 79 79 73 69 7a   */..  int yysiz
3d898 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
3d899 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74         /* Amount
3d89a 20 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61 63   to pop the stac
3d89b 6b 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 50  k */..  sqlite3P
3d89c 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0d  arserARG_FETCH;.
3d89d 0a 20 20 79 79 6d 73 70 20 3d 20 26 79 79 70 50  .  yymsp = &yypP
3d89e 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79  arser->yystack[y
3d89f 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  ypParser->yyidx]
3d8a0 3b 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ;..#ifndef NDEBU
3d8a1 47 0d 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  G..  if( yyTrace
3d8a2 46 49 4c 45 20 26 26 20 79 79 72 75 6c 65 6e 6f  FILE && yyruleno
3d8a3 3e 3d 30 20 0d 0a 20 20 20 20 20 20 20 20 26 26  >=0 ..        &&
3d8a4 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29 28   yyruleno<(int)(
3d8a5 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d  sizeof(yyRuleNam
3d8a6 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c 65  e)/sizeof(yyRule
3d8a7 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0d 0a 20 20  Name[0])) ){..  
3d8a8 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
3d8a9 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63 65  eFILE, "%sReduce
3d8aa 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61   [%s].\n", yyTra
3d8ab 63 65 50 72 6f 6d 70 74 2c 0d 0a 20 20 20 20 20  cePrompt,..     
3d8ac 20 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75   yyRuleName[yyru
3d8ad 6c 65 6e 6f 5d 29 3b 0d 0a 20 20 7d 0d 0a 23 65  leno]);..  }..#e
3d8ae 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
3d8af 2f 0d 0a 0d 0a 20 20 2f 2a 20 53 69 6c 65 6e 63  /....  /* Silenc
3d8b0 65 20 63 6f 6d 70 6c 61 69 6e 74 73 20 66 72 6f  e complaints fro
3d8b1 6d 20 70 75 72 69 66 79 20 61 62 6f 75 74 20 79  m purify about y
3d8b2 79 67 6f 74 6f 6d 69 6e 6f 72 20 62 65 69 6e 67  ygotominor being
3d8b3 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 0d 0a   uninitialized..
3d8b4 20 20 2a 2a 20 69 6e 20 73 6f 6d 65 20 63 61 73    ** in some cas
3d8b5 65 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f  es when it is co
3d8b6 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 73 74  pied into the st
3d8b7 61 63 6b 20 61 66 74 65 72 20 74 68 65 20 66 6f  ack after the fo
3d8b8 6c 6c 6f 77 69 6e 67 0d 0a 20 20 2a 2a 20 73 77  llowing..  ** sw
3d8b9 69 74 63 68 2e 20 20 79 79 67 6f 74 6f 6d 69 6e  itch.  yygotomin
3d8ba 6f 72 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69  or is uninitiali
3d8bb 7a 65 64 20 77 68 65 6e 20 61 20 72 75 6c 65 20  zed when a rule 
3d8bc 72 65 64 75 63 65 73 20 74 68 61 74 20 64 6f 65  reduces that doe
3d8bd 73 0d 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20  s..  ** not set 
3d8be 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69 74 73  the value of its
3d8bf 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
3d8c0 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20 4c 65  nonterminal.  Le
3d8c1 61 76 69 6e 67 20 74 68 65 0d 0a 20 20 2a 2a 20  aving the..  ** 
3d8c2 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f 6e  value of the non
3d8c3 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74 69  terminal uniniti
3d8c4 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65 72 6c  alized is utterl
3d8c5 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20 6c 6f  y harmless as lo
3d8c6 6e 67 0d 0a 20 20 2a 2a 20 61 73 20 74 68 65 20  ng..  ** as the 
3d8c7 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75  value is never u
3d8c8 73 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20  sed.  So really 
3d8c9 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74  the only thing t
3d8ca 68 69 73 20 63 6f 64 65 0d 0a 20 20 2a 2a 20 61  his code..  ** a
3d8cb 63 63 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74  ccomplishes is t
3d8cc 6f 20 71 75 69 65 74 65 6e 20 70 75 72 69 66 79  o quieten purify
3d8cd 2e 20 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  .  ..  **..  ** 
3d8ce 32 30 30 37 2d 30 31 2d 31 36 3a 20 20 54 68 65  2007-01-16:  The
3d8cf 20 77 69 72 65 73 68 61 72 6b 20 70 72 6f 6a 65   wireshark proje
3d8d0 63 74 20 28 77 77 77 2e 77 69 72 65 73 68 61 72  ct (www.wireshar
3d8d1 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74 73 20 74  k.org) reports t
3d8d2 68 61 74 0d 0a 20 20 2a 2a 20 77 69 74 68 6f 75  hat..  ** withou
3d8d3 74 20 74 68 69 73 20 63 6f 64 65 2c 20 74 68 65  t this code, the
3d8d4 69 72 20 70 61 72 73 65 72 20 73 65 67 66 61 75  ir parser segfau
3d8d5 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75  lts.  I'm not su
3d8d6 72 65 20 77 68 61 74 20 74 68 65 72 65 0d 0a 20  re what there.. 
3d8d7 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20 64 6f   ** parser is do
3d8d8 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 69 73  ing to make this
3d8d9 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73 20 69   happen.  This i
3d8da 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 67  s the second bug
3d8db 20 72 65 70 6f 72 74 0d 0a 20 20 2a 2a 20 66 72   report..  ** fr
3d8dc 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68 69  om wireshark thi
3d8dd 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79  s week.  Clearly
3d8de 20 74 68 65 79 20 61 72 65 20 73 74 72 65 73 73   they are stress
3d8df 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79  ing Lemon in way
3d8e0 73 0d 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20  s..  ** that it 
3d8e1 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65  has not been pre
3d8e2 76 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64  viously stressed
3d8e3 2e 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63  ...  (SQLite tic
3d8e4 6b 65 74 20 23 32 31 37 32 29 0d 0a 20 20 2a 2f  ket #2172)..  */
3d8e5 0d 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79  ..  /*memset(&yy
3d8e6 67 6f 74 6f 6d 69 6e 6f 72 2c 20 30 2c 20 73 69  gotominor, 0, si
3d8e7 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  zeof(yygotominor
3d8e8 29 29 3b 2a 2f 0d 0a 20 20 79 79 67 6f 74 6f 6d  ));*/..  yygotom
3d8e9 69 6e 6f 72 20 3d 20 79 79 7a 65 72 6f 6d 69 6e  inor = yyzeromin
3d8ea 6f 72 3b 0d 0a 0d 0a 0d 0a 20 20 73 77 69 74 63  or;......  switc
3d8eb 68 28 20 79 79 72 75 6c 65 6e 6f 20 29 7b 0d 0a  h( yyruleno ){..
3d8ec 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 68    /* Beginning h
3d8ed 65 72 65 20 61 72 65 20 74 68 65 20 72 65 64 75  ere are the redu
3d8ee 63 74 69 6f 6e 20 63 61 73 65 73 2e 20 20 41 20  ction cases.  A 
3d8ef 74 79 70 69 63 61 6c 20 65 78 61 6d 70 6c 65 0d  typical example.
3d8f0 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0d 0a  .  ** follows:..
3d8f1 20 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0d 0a    **   case 0:..
3d8f2 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e    **  #line <lin
3d8f3 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c  eno> <grammarfil
3d8f4 65 3e 0d 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e  e>..  **     { .
3d8f5 2e 2e 20 7d 20 20 20 20 20 20 20 20 20 20 20 2f  .. }           /
3d8f6 2f 20 55 73 65 72 20 73 75 70 70 6c 69 65 64 20  / User supplied 
3d8f7 63 6f 64 65 0d 0a 20 20 2a 2a 20 20 23 6c 69 6e  code..  **  #lin
3d8f8 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73  e <lineno> <this
3d8f9 66 69 6c 65 3e 0d 0a 20 20 2a 2a 20 20 20 20 20  file>..  **     
3d8fa 62 72 65 61 6b 3b 0d 0a 20 20 2a 2f 0d 0a 20 20  break;..  */..  
3d8fb 20 20 20 20 63 61 73 65 20 35 3a 20 2f 2a 20 65      case 5: /* e
3d8fc 78 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f 0d 0a 7b  xplain ::= */..{
3d8fd 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
3d8fe 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b 20 7d  se(pParse, 0); }
3d8ff 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3d900 0d 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a 20  ..      case 6: 
3d901 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45  /* explain ::= E
3d902 58 50 4c 41 49 4e 20 2a 2f 0d 0a 7b 20 73 71 6c  XPLAIN */..{ sql
3d903 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70  ite3BeginParse(p
3d904 50 61 72 73 65 2c 20 31 29 3b 20 7d 0d 0a 20 20  Parse, 1); }..  
3d905 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3d906 20 20 20 20 63 61 73 65 20 37 3a 20 2f 2a 20 65      case 7: /* e
3d907 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
3d908 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 2a 2f  IN QUERY PLAN */
3d909 0d 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ..{ sqlite3Begin
3d90a 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 32 29  Parse(pParse, 2)
3d90b 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ; }..        bre
3d90c 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3d90d 38 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d 20 63  8: /* cmdx ::= c
3d90e 6d 64 20 2a 2f 0d 0a 7b 20 73 71 6c 69 74 65 33  md */..{ sqlite3
3d90f 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50 61  FinishCoding(pPa
3d910 72 73 65 29 3b 20 7d 0d 0a 20 20 20 20 20 20 20  rse); }..       
3d911 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3d912 61 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  ase 9: /* cmd ::
3d913 3d 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70  = BEGIN transtyp
3d914 65 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0d 0a  e trans_opt */..
3d915 7b 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61  {sqlite3BeginTra
3d916 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  nsaction(pParse,
3d917 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3d918 2e 79 79 34 29 3b 7d 0d 0a 20 20 20 20 20 20 20  .yy4);}..       
3d919 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3d91a 61 73 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e 73  ase 13: /* trans
3d91b 74 79 70 65 20 3a 3a 3d 20 2a 2f 0d 0a 7b 79 79  type ::= */..{yy
3d91c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20  gotominor.yy4 = 
3d91d 54 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0d 0a 20  TK_DEFERRED;}.. 
3d91e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3d91f 20 20 20 20 20 63 61 73 65 20 31 34 3a 20 2f 2a       case 14: /*
3d920 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44   transtype ::= D
3d921 45 46 45 52 52 45 44 20 2a 2f 0d 0a 20 20 20 20  EFERRED */..    
3d922 20 20 63 61 73 65 20 31 35 3a 20 2f 2a 20 74 72    case 15: /* tr
3d923 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
3d924 44 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63  DIATE */ yytestc
3d925 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
3d926 29 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  );..      case 1
3d927 36 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  6: /* transtype 
3d928 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 20 2a 2f  ::= EXCLUSIVE */
3d929 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3d92a 6c 65 6e 6f 3d 3d 31 36 29 3b 0d 0a 20 20 20 20  leno==16);..    
3d92b 20 20 63 61 73 65 20 31 31 35 3a 20 2f 2a 20 6d    case 115: /* m
3d92c 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
3d92d 3d 20 55 4e 49 4f 4e 20 2a 2f 20 79 79 74 65 73  = UNION */ yytes
3d92e 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3d92f 31 31 35 29 3b 0d 0a 20 20 20 20 20 20 63 61 73  115);..      cas
3d930 65 20 31 31 37 3a 20 2f 2a 20 6d 75 6c 74 69 73  e 117: /* multis
3d931 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58 43  elect_op ::= EXC
3d932 45 50 54 7c 49 4e 54 45 52 53 45 43 54 20 2a 2f  EPT|INTERSECT */
3d933 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3d934 6c 65 6e 6f 3d 3d 31 31 37 29 3b 0d 0a 7b 79 79  leno==117);..{yy
3d935 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20  gotominor.yy4 = 
3d936 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d  yymsp[0].major;}
3d937 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3d938 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 3a  ..      case 17:
3d939 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d   /* cmd ::= COMM
3d93a 49 54 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0d  IT trans_opt */.
3d93b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 3a 20  .      case 18: 
3d93c 2f 2a 20 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74  /* cmd ::= END t
3d93d 72 61 6e 73 5f 6f 70 74 20 2a 2f 20 79 79 74 65  rans_opt */ yyte
3d93e 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3d93f 3d 31 38 29 3b 0d 0a 7b 73 71 6c 69 74 65 33 43  =18);..{sqlite3C
3d940 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e  ommitTransaction
3d941 28 70 50 61 72 73 65 29 3b 7d 0d 0a 20 20 20 20  (pParse);}..    
3d942 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3d943 20 20 63 61 73 65 20 31 39 3a 20 2f 2a 20 63 6d    case 19: /* cm
3d944 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74  d ::= ROLLBACK t
3d945 72 61 6e 73 5f 6f 70 74 20 2a 2f 0d 0a 7b 73 71  rans_opt */..{sq
3d946 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
3d947 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29  nsaction(pParse)
3d948 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3d949 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3d94a 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53 41  2: /* cmd ::= SA
3d94b 56 45 50 4f 49 4e 54 20 6e 6d 20 2a 2f 0d 0a 7b  VEPOINT nm */..{
3d94c 0d 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70  ..  sqlite3Savep
3d94d 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56  oint(pParse, SAV
3d94e 45 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 20 26 79  EPOINT_BEGIN, &y
3d94f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3d950 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20  0);..}..        
3d951 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3d952 73 65 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 23: /* cmd ::
3d953 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f  = RELEASE savepo
3d954 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0d 0a 7b  int_opt nm */..{
3d955 0d 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70  ..  sqlite3Savep
3d956 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56  oint(pParse, SAV
3d957 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20  EPOINT_RELEASE, 
3d958 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3d959 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  yy0);..}..      
3d95a 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3d95b 63 61 73 65 20 32 34 3a 20 2f 2a 20 63 6d 64 20  case 24: /* cmd 
3d95c 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61  ::= ROLLBACK tra
3d95d 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f  ns_opt TO savepo
3d95e 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0d 0a 7b  int_opt nm */..{
3d95f 0d 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70  ..  sqlite3Savep
3d960 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56  oint(pParse, SAV
3d961 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
3d962 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3d963 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  .yy0);..}..     
3d964 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3d965 20 63 61 73 65 20 32 36 3a 20 2f 2a 20 63 72 65   case 26: /* cre
3d966 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72  ate_table ::= cr
3d967 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c  eatekw temp TABL
3d968 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  E ifnotexists nm
3d969 20 64 62 6e 6d 20 2a 2f 0d 0a 7b 0d 0a 20 20 20   dbnm */..{..   
3d96a 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
3d96b 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
3d96c 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
3d96d 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3d96e 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  0,yymsp[-4].mino
3d96f 72 2e 79 79 34 2c 30 2c 30 2c 79 79 6d 73 70 5b  r.yy4,0,0,yymsp[
3d970 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 29 3b 0d  -2].minor.yy4);.
3d971 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  .}..        brea
3d972 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3d973 37 3a 20 2f 2a 20 63 72 65 61 74 65 6b 77 20 3a  7: /* createkw :
3d974 3a 3d 20 43 52 45 41 54 45 20 2a 2f 0d 0a 7b 0d  := CREATE */..{.
3d975 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6c  .  pParse->db->l
3d976 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
3d977 64 20 3d 20 30 3b 0d 0a 20 20 79 79 67 6f 74 6f  d = 0;..  yygoto
3d978 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73  minor.yy0 = yyms
3d979 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0d  p[0].minor.yy0;.
3d97a 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  .}..        brea
3d97b 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3d97c 38 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74  8: /* ifnotexist
3d97d 73 20 3a 3a 3d 20 2a 2f 0d 0a 20 20 20 20 20 20  s ::= */..      
3d97e 63 61 73 65 20 33 31 3a 20 2f 2a 20 74 65 6d 70  case 31: /* temp
3d97f 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3d980 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 29  se(yyruleno==31)
3d981 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 37 30  ;..      case 70
3d982 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d  : /* autoinc ::=
3d983 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3d984 79 72 75 6c 65 6e 6f 3d 3d 37 30 29 3b 0d 0a 20  yruleno==70);.. 
3d985 20 20 20 20 20 63 61 73 65 20 38 33 3a 20 2f 2a       case 83: /*
3d986 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
3d987 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52 41   ::= NOT DEFERRA
3d988 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65  BLE init_deferre
3d989 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 20 79 79  d_pred_opt */ yy
3d98a 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3d98b 6f 3d 3d 38 33 29 3b 0d 0a 20 20 20 20 20 20 63  o==83);..      c
3d98c 61 73 65 20 38 35 3a 20 2f 2a 20 69 6e 69 74 5f  ase 85: /* init_
3d98d 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
3d98e 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
3d98f 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 35  ase(yyruleno==85
3d990 29 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 38  );..      case 8
3d991 37 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72  7: /* init_defer
3d992 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
3d993 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
3d994 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61  IATE */ yytestca
3d995 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 37 29  se(yyruleno==87)
3d996 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 39 38  ;..      case 98
3d997 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c  : /* defer_subcl
3d998 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  ause_opt ::= */ 
3d999 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3d99a 65 6e 6f 3d 3d 39 38 29 3b 0d 0a 20 20 20 20 20  eno==98);..     
3d99b 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20 69 66   case 109: /* if
3d99c 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 20 79 79  exists ::= */ yy
3d99d 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3d99e 6f 3d 3d 31 30 39 29 3b 0d 0a 20 20 20 20 20 20  o==109);..      
3d99f 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 64 69 73  case 120: /* dis
3d9a0 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a 2f  tinct ::= ALL */
3d9a1 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3d9a2 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0d 0a 20 20 20  leno==120);..   
3d9a3 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20     case 121: /* 
3d9a4 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 20  distinct ::= */ 
3d9a5 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3d9a6 65 6e 6f 3d 3d 31 32 31 29 3b 0d 0a 20 20 20 20  eno==121);..    
3d9a7 20 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62    case 222: /* b
3d9a8 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45  etween_op ::= BE
3d9a9 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63  TWEEN */ yytestc
3d9aa 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32  ase(yyruleno==22
3d9ab 32 29 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  2);..      case 
3d9ac 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a  225: /* in_op ::
3d9ad 3d 20 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  = IN */ yytestca
3d9ae 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 35  se(yyruleno==225
3d9af 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  );..{yygotominor
3d9b0 2e 79 79 34 20 3d 20 30 3b 7d 0d 0a 20 20 20 20  .yy4 = 0;}..    
3d9b1 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3d9b2 20 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 69 66    case 29: /* if
3d9b3 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46  notexists ::= IF
3d9b4 20 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0d 0a   NOT EXISTS */..
3d9b5 20 20 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f        case 30: /
3d9b6 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20  * temp ::= TEMP 
3d9b7 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3d9b8 72 75 6c 65 6e 6f 3d 3d 33 30 29 3b 0d 0a 20 20  ruleno==30);..  
3d9b9 20 20 20 20 63 61 73 65 20 37 31 3a 20 2f 2a 20      case 71: /* 
3d9ba 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f  autoinc ::= AUTO
3d9bb 49 4e 43 52 20 2a 2f 20 79 79 74 65 73 74 63 61  INCR */ yytestca
3d9bc 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 37 31 29  se(yyruleno==71)
3d9bd 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 38 36  ;..      case 86
3d9be 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72  : /* init_deferr
3d9bf 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20  ed_pred_opt ::= 
3d9c0 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
3d9c1 45 44 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ED */ yytestcase
3d9c2 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 36 29 3b 0d  (yyruleno==86);.
3d9c3 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 38 3a  .      case 108:
3d9c4 20 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d   /* ifexists ::=
3d9c5 20 49 46 20 45 58 49 53 54 53 20 2a 2f 20 79 79   IF EXISTS */ yy
3d9c6 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3d9c7 6f 3d 3d 31 30 38 29 3b 0d 0a 20 20 20 20 20 20  o==108);..      
3d9c8 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 64 69 73  case 119: /* dis
3d9c9 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e  tinct ::= DISTIN
3d9ca 43 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  CT */ yytestcase
3d9cb 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 39 29 3b  (yyruleno==119);
3d9cc 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 33  ..      case 223
3d9cd 3a 20 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20  : /* between_op 
3d9ce 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 20  ::= NOT BETWEEN 
3d9cf 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3d9d0 72 75 6c 65 6e 6f 3d 3d 32 32 33 29 3b 0d 0a 20  ruleno==223);.. 
3d9d1 20 20 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f       case 226: /
3d9d2 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20  * in_op ::= NOT 
3d9d3 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  IN */ yytestcase
3d9d4 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 36 29 3b  (yyruleno==226);
3d9d5 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ..{yygotominor.y
3d9d6 79 34 20 3d 20 31 3b 7d 0d 0a 20 20 20 20 20 20  y4 = 1;}..      
3d9d7 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3d9d8 63 61 73 65 20 33 32 3a 20 2f 2a 20 63 72 65 61  case 32: /* crea
3d9d9 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a  te_table_args ::
3d9da 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20  = LP columnlist 
3d9db 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 20  conslist_opt RP 
3d9dc 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74 65 33  */..{..  sqlite3
3d9dd 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c  EndTable(pParse,
3d9de 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
3d9df 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
3d9e0 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 0d 0a 7d 0d  inor.yy0,0);..}.
3d9e1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3d9e2 0a 20 20 20 20 20 20 63 61 73 65 20 33 33 3a 20  .      case 33: 
3d9e3 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  /* create_table_
3d9e4 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65  args ::= AS sele
3d9e5 63 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69  ct */..{..  sqli
3d9e6 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72  te3EndTable(pPar
3d9e7 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  se,0,0,yymsp[0].
3d9e8 6d 69 6e 6f 72 2e 79 79 33 38 37 29 3b 0d 0a 20  minor.yy387);.. 
3d9e9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
3d9ea 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
3d9eb 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3d9ec 79 79 33 38 37 29 3b 0d 0a 7d 0d 0a 20 20 20 20  yy387);..}..    
3d9ed 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3d9ee 20 20 63 61 73 65 20 33 36 3a 20 2f 2a 20 63 6f    case 36: /* co
3d9ef 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
3d9f0 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 20  d type carglist 
3d9f1 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f 74 6f 6d  */..{..  yygotom
3d9f2 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d  inor.yy0.z = yym
3d9f3 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
3d9f4 2e 7a 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .z;..  yygotomin
3d9f5 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29  or.yy0.n = (int)
3d9f6 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f  (pParse->sLastTo
3d9f7 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32 5d 2e  ken.z-yymsp[-2].
3d9f8 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 70  minor.yy0.z) + p
3d9f9 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
3d9fa 6e 2e 6e 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20  n.n;..}..       
3d9fb 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3d9fc 61 73 65 20 33 37 3a 20 2f 2a 20 63 6f 6c 75 6d  ase 37: /* colum
3d9fd 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0d 0a 7b  nid ::= nm */..{
3d9fe 0d 0a 20 20 73 71 6c 69 74 65 33 41 64 64 43 6f  ..  sqlite3AddCo
3d9ff 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26 79 79 6d  lumn(pParse,&yym
3da00 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3da01 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ;..  yygotominor
3da02 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy0 = yymsp[0].
3da03 6d 69 6e 6f 72 2e 79 79 30 3b 0d 0a 7d 0d 0a 20  minor.yy0;..}.. 
3da04 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3da05 20 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a       case 38: /*
3da06 20 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0d 0a 20   id ::= ID */.. 
3da07 20 20 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a       case 39: /*
3da08 20 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20   id ::= INDEXED 
3da09 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3da0a 72 75 6c 65 6e 6f 3d 3d 33 39 29 3b 0d 0a 20 20  ruleno==39);..  
3da0b 20 20 20 20 63 61 73 65 20 34 30 3a 20 2f 2a 20      case 40: /* 
3da0c 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e  ids ::= ID|STRIN
3da0d 47 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  G */ yytestcase(
3da0e 79 79 72 75 6c 65 6e 6f 3d 3d 34 30 29 3b 0d 0a  yyruleno==40);..
3da0f 20 20 20 20 20 20 63 61 73 65 20 34 31 3a 20 2f        case 41: /
3da10 2a 20 6e 6d 20 3a 3a 3d 20 69 64 20 2a 2f 20 79  * nm ::= id */ y
3da11 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3da12 6e 6f 3d 3d 34 31 29 3b 0d 0a 20 20 20 20 20 20  no==41);..      
3da13 63 61 73 65 20 34 32 3a 20 2f 2a 20 6e 6d 20 3a  case 42: /* nm :
3da14 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 20 79 79 74  := STRING */ yyt
3da15 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3da16 3d 3d 34 32 29 3b 0d 0a 20 20 20 20 20 20 63 61  ==42);..      ca
3da17 73 65 20 34 33 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d  se 43: /* nm ::=
3da18 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79 74 65   JOIN_KW */ yyte
3da19 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3da1a 3d 34 33 29 3b 0d 0a 20 20 20 20 20 20 63 61 73  =43);..      cas
3da1b 65 20 34 36 3a 20 2f 2a 20 74 79 70 65 74 6f 6b  e 46: /* typetok
3da1c 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20  en ::= typename 
3da1d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3da1e 72 75 6c 65 6e 6f 3d 3d 34 36 29 3b 0d 0a 20 20  ruleno==46);..  
3da1f 20 20 20 20 63 61 73 65 20 34 39 3a 20 2f 2a 20      case 49: /* 
3da20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73  typename ::= ids
3da21 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3da22 79 72 75 6c 65 6e 6f 3d 3d 34 39 29 3b 0d 0a 20  yruleno==49);.. 
3da23 20 20 20 20 20 63 61 73 65 20 31 32 37 3a 20 2f       case 127: /
3da24 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 20 2a  * as ::= AS nm *
3da25 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3da26 75 6c 65 6e 6f 3d 3d 31 32 37 29 3b 0d 0a 20 20  uleno==127);..  
3da27 20 20 20 20 63 61 73 65 20 31 32 38 3a 20 2f 2a      case 128: /*
3da28 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79   as ::= ids */ y
3da29 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3da2a 6e 6f 3d 3d 31 32 38 29 3b 0d 0a 20 20 20 20 20  no==128);..     
3da2b 20 63 61 73 65 20 31 33 38 3a 20 2f 2a 20 64 62   case 138: /* db
3da2c 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a 2f  nm ::= DOT nm */
3da2d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3da2e 6c 65 6e 6f 3d 3d 31 33 38 29 3b 0d 0a 20 20 20  leno==138);..   
3da2f 20 20 20 63 61 73 65 20 31 34 37 3a 20 2f 2a 20     case 147: /* 
3da30 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
3da31 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f  INDEXED BY nm */
3da32 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3da33 6c 65 6e 6f 3d 3d 31 34 37 29 3b 0d 0a 20 20 20  leno==147);..   
3da34 20 20 20 63 61 73 65 20 32 35 31 3a 20 2f 2a 20     case 251: /* 
3da35 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c  collate ::= COLL
3da36 41 54 45 20 69 64 73 20 2a 2f 20 79 79 74 65 73  ATE ids */ yytes
3da37 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3da38 32 35 31 29 3b 0d 0a 20 20 20 20 20 20 63 61 73  251);..      cas
3da39 65 20 32 36 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20  e 260: /* nmnum 
3da3a 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20  ::= plus_num */ 
3da3b 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3da3c 65 6e 6f 3d 3d 32 36 30 29 3b 0d 0a 20 20 20 20  eno==260);..    
3da3d 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e    case 261: /* n
3da3e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79  mnum ::= nm */ y
3da3f 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3da40 6e 6f 3d 3d 32 36 31 29 3b 0d 0a 20 20 20 20 20  no==261);..     
3da41 20 63 61 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d   case 262: /* nm
3da42 6e 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 20 79 79  num ::= ON */ yy
3da43 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3da44 6f 3d 3d 32 36 32 29 3b 0d 0a 20 20 20 20 20 20  o==262);..      
3da45 63 61 73 65 20 32 36 33 3a 20 2f 2a 20 6e 6d 6e  case 263: /* nmn
3da46 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 20 2a 2f  um ::= DELETE */
3da47 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3da48 6c 65 6e 6f 3d 3d 32 36 33 29 3b 0d 0a 20 20 20  leno==263);..   
3da49 20 20 20 63 61 73 65 20 32 36 34 3a 20 2f 2a 20     case 264: /* 
3da4a 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c  nmnum ::= DEFAUL
3da4b 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  T */ yytestcase(
3da4c 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 34 29 3b 0d  yyruleno==264);.
3da4d 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 35 3a  .      case 265:
3da4e 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d   /* plus_num ::=
3da4f 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72   plus_opt number
3da50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3da51 79 72 75 6c 65 6e 6f 3d 3d 32 36 35 29 3b 0d 0a  yruleno==265);..
3da52 20 20 20 20 20 20 63 61 73 65 20 32 36 36 3a 20        case 266: 
3da53 2f 2a 20 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d  /* minus_num ::=
3da54 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 20 2a 2f   MINUS number */
3da55 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3da56 6c 65 6e 6f 3d 3d 32 36 36 29 3b 0d 0a 20 20 20  leno==266);..   
3da57 20 20 20 63 61 73 65 20 32 36 37 3a 20 2f 2a 20     case 267: /* 
3da58 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47  number ::= INTEG
3da59 45 52 7c 46 4c 4f 41 54 20 2a 2f 20 79 79 74 65  ER|FLOAT */ yyte
3da5a 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3da5b 3d 32 36 37 29 3b 0d 0a 20 20 20 20 20 20 63 61  =267);..      ca
3da5c 73 65 20 32 38 35 3a 20 2f 2a 20 74 72 6e 6d 20  se 285: /* trnm 
3da5d 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74  ::= nm */ yytest
3da5e 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3da5f 38 35 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e  85);..{yygotomin
3da60 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30  or.yy0 = yymsp[0
3da61 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0d 0a 20  ].minor.yy0;}.. 
3da62 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3da63 20 20 20 20 20 63 61 73 65 20 34 35 3a 20 2f 2a       case 45: /*
3da64 20 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f   type ::= typeto
3da65 6b 65 6e 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33  ken */..{sqlite3
3da66 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70 50  AddColumnType(pP
3da67 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
3da68 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20 20 20  inor.yy0);}..   
3da69 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3da6a 20 20 20 63 61 73 65 20 34 37 3a 20 2f 2a 20 74     case 47: /* t
3da6b 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
3da6c 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
3da6d 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f  RP */..{..  yygo
3da6e 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20  tominor.yy0.z = 
3da6f 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3da70 79 79 30 2e 7a 3b 0d 0a 20 20 79 79 67 6f 74 6f  yy0.z;..  yygoto
3da71 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69  minor.yy0.n = (i
3da72 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  nt)(&yymsp[0].mi
3da73 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3da74 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20  0].minor.yy0.n] 
3da75 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  - yymsp[-3].mino
3da76 72 2e 79 79 30 2e 7a 29 3b 0d 0a 7d 0d 0a 20 20  r.yy0.z);..}..  
3da77 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3da78 20 20 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20      case 48: /* 
3da79 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
3da7a 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
3da7b 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50   COMMA signed RP
3da7c 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f 74 6f   */..{..  yygoto
3da7d 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79  minor.yy0.z = yy
3da7e 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
3da7f 30 2e 7a 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69  0.z;..  yygotomi
3da80 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74  nor.yy0.n = (int
3da81 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  )(&yymsp[0].mino
3da82 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
3da83 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20  .minor.yy0.n] - 
3da84 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
3da85 79 79 30 2e 7a 29 3b 0d 0a 7d 0d 0a 20 20 20 20  yy0.z);..}..    
3da86 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3da87 20 20 63 61 73 65 20 35 30 3a 20 2f 2a 20 74 79    case 50: /* ty
3da88 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e  pename ::= typen
3da89 61 6d 65 20 69 64 73 20 2a 2f 0d 0a 7b 79 79 67  ame ids */..{yyg
3da8a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 79  otominor.yy0.z=y
3da8b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3da8c 79 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  y0.z; yygotomino
3da8d 72 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d  r.yy0.n=yymsp[0]
3da8e 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 69 6e  .minor.yy0.n+(in
3da8f 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t)(yymsp[0].mino
3da90 72 2e 79 79 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31  r.yy0.z-yymsp[-1
3da91 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 7d  ].minor.yy0.z);}
3da92 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3da93 0d 0a 20 20 20 20 20 20 63 61 73 65 20 35 37 3a  ..      case 57:
3da94 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
3da95 46 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0d 0a 20  FAULT term */.. 
3da96 20 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f 2a       case 59: /*
3da97 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
3da98 4c 54 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f 20  LT PLUS term */ 
3da99 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3da9a 65 6e 6f 3d 3d 35 39 29 3b 0d 0a 7b 73 71 6c 69  eno==59);..{sqli
3da9b 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
3da9c 75 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ue(pParse,&yymsp
3da9d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 29  [0].minor.yy118)
3da9e 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3da9f 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 35  k;..      case 5
3daa0 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  8: /* ccons ::= 
3daa1 44 45 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20  DEFAULT LP expr 
3daa2 52 50 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 41  RP */..{sqlite3A
3daa3 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
3daa4 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
3daa5 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 29 3b 7d 0d  .minor.yy118);}.
3daa6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3daa7 0a 20 20 20 20 20 20 63 61 73 65 20 36 30 3a 20  .      case 60: 
3daa8 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
3daa9 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 20  AULT MINUS term 
3daaa 2a 2f 0d 0a 7b 0d 0a 20 20 45 78 70 72 53 70 61  */..{..  ExprSpa
3daab 6e 20 76 3b 0d 0a 20 20 76 2e 70 45 78 70 72 20  n v;..  v.pExpr 
3daac 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3daad 50 61 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53  Parse, TK_UMINUS
3daae 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3daaf 2e 79 79 31 31 38 2e 70 45 78 70 72 2c 20 30 2c  .yy118.pExpr, 0,
3dab0 20 30 29 3b 0d 0a 20 20 76 2e 7a 53 74 61 72 74   0);..  v.zStart
3dab1 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
3dab2 6f 72 2e 79 79 30 2e 7a 3b 0d 0a 20 20 76 2e 7a  or.yy0.z;..  v.z
3dab3 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  End = yymsp[0].m
3dab4 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 45 6e 64 3b  inor.yy118.zEnd;
3dab5 0d 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65  ..  sqlite3AddDe
3dab6 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73  faultValue(pPars
3dab7 65 2c 26 76 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  e,&v);..}..     
3dab8 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3dab9 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f   case 61: /* cco
3daba 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69  ns ::= DEFAULT i
3dabb 64 20 2a 2f 0d 0a 7b 0d 0a 20 20 45 78 70 72 53  d */..{..  ExprS
3dabc 70 61 6e 20 76 3b 0d 0a 20 20 73 70 61 6e 45 78  pan v;..  spanEx
3dabd 70 72 28 26 76 2c 20 70 50 61 72 73 65 2c 20 54  pr(&v, pParse, T
3dabe 4b 5f 53 54 52 49 4e 47 2c 20 26 79 79 6d 73 70  K_STRING, &yymsp
3dabf 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d  [0].minor.yy0);.
3dac0 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  .  sqlite3AddDef
3dac1 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65  aultValue(pParse
3dac2 2c 26 76 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  ,&v);..}..      
3dac3 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dac4 63 61 73 65 20 36 33 3a 20 2f 2a 20 63 63 6f 6e  case 63: /* ccon
3dac5 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f  s ::= NOT NULL o
3dac6 6e 63 6f 6e 66 20 2a 2f 0d 0a 7b 73 71 6c 69 74  nconf */..{sqlit
3dac7 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 70 50 61  e3AddNotNull(pPa
3dac8 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
3dac9 6e 6f 72 2e 79 79 34 29 3b 7d 0d 0a 20 20 20 20  nor.yy4);}..    
3daca 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3dacb 20 20 63 61 73 65 20 36 34 3a 20 2f 2a 20 63 63    case 64: /* cc
3dacc 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20  ons ::= PRIMARY 
3dacd 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e  KEY sortorder on
3dace 63 6f 6e 66 20 61 75 74 6f 69 6e 63 20 2a 2f 0d  conf autoinc */.
3dacf 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d  .{sqlite3AddPrim
3dad0 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  aryKey(pParse,0,
3dad1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3dad2 79 79 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  yy4,yymsp[0].min
3dad3 6f 72 2e 79 79 34 2c 79 79 6d 73 70 5b 2d 32 5d  or.yy4,yymsp[-2]
3dad4 2e 6d 69 6e 6f 72 2e 79 79 34 29 3b 7d 0d 0a 20  .minor.yy4);}.. 
3dad5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3dad6 20 20 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a       case 65: /*
3dad7 20 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55   ccons ::= UNIQU
3dad8 45 20 6f 6e 63 6f 6e 66 20 2a 2f 0d 0a 7b 73 71  E onconf */..{sq
3dad9 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
3dada 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c  (pParse,0,0,0,0,
3dadb 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3dadc 79 34 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0d 0a 20  y4,0,0,0,0);}.. 
3dadd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3dade 20 20 20 20 20 63 61 73 65 20 36 36 3a 20 2f 2a       case 66: /*
3dadf 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b   ccons ::= CHECK
3dae0 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0d 0a   LP expr RP */..
3dae1 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b  {sqlite3AddCheck
3dae2 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
3dae3 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  e,yymsp[-1].mino
3dae4 72 2e 79 79 31 31 38 2e 70 45 78 70 72 29 3b 7d  r.yy118.pExpr);}
3dae5 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3dae6 0d 0a 20 20 20 20 20 20 63 61 73 65 20 36 37 3a  ..      case 67:
3dae7 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 52 45   /* ccons ::= RE
3dae8 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
3dae9 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
3daea 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 43 72 65 61  */..{sqlite3Crea
3daeb 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61  teForeignKey(pPa
3daec 72 73 65 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  rse,0,&yymsp[-2]
3daed 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70  .minor.yy0,yymsp
3daee 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32  [-1].minor.yy322
3daef 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
3daf0 79 79 34 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20  yy4);}..        
3daf1 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3daf2 73 65 20 36 38 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 68: /* ccons 
3daf3 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61  ::= defer_subcla
3daf4 75 73 65 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33  use */..{sqlite3
3daf5 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28  DeferForeignKey(
3daf6 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
3daf7 6d 69 6e 6f 72 2e 79 79 34 29 3b 7d 0d 0a 20 20  minor.yy4);}..  
3daf8 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3daf9 20 20 20 20 63 61 73 65 20 36 39 3a 20 2f 2a 20      case 69: /* 
3dafa 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54  ccons ::= COLLAT
3dafb 45 20 69 64 73 20 2a 2f 0d 0a 7b 73 71 6c 69 74  E ids */..{sqlit
3dafc 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65  e3AddCollateType
3dafd 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
3dafe 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d  0].minor.yy0);}.
3daff 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3db00 0a 20 20 20 20 20 20 63 61 73 65 20 37 32 3a 20  .      case 72: 
3db01 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a  /* refargs ::= *
3db02 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  /..{ yygotominor
3db03 2e 79 79 34 20 3d 20 4f 45 5f 4e 6f 6e 65 2a 30  .yy4 = OE_None*0
3db04 78 30 31 30 31 3b 20 2f 2a 20 45 56 3a 20 52 2d  x0101; /* EV: R-
3db05 31 39 38 30 33 2d 34 35 38 38 34 20 2a 2f 7d 0d  19803-45884 */}.
3db06 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3db07 0a 20 20 20 20 20 20 63 61 73 65 20 37 33 3a 20  .      case 73: 
3db08 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72  /* refargs ::= r
3db09 65 66 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f  efargs refarg */
3db0a 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ..{ yygotominor.
3db0b 79 79 34 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d  yy4 = (yymsp[-1]
3db0c 2e 6d 69 6e 6f 72 2e 79 79 34 20 26 20 7e 79 79  .minor.yy4 & ~yy
3db0d 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
3db0e 31 35 2e 6d 61 73 6b 29 20 7c 20 79 79 6d 73 70  15.mask) | yymsp
3db0f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 35 2e  [0].minor.yy215.
3db10 76 61 6c 75 65 3b 20 7d 0d 0a 20 20 20 20 20 20  value; }..      
3db11 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3db12 63 61 73 65 20 37 34 3a 20 2f 2a 20 72 65 66 61  case 74: /* refa
3db13 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 20  rg ::= MATCH nm 
3db14 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73 65 20 37  */..      case 7
3db15 35 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  5: /* refarg ::=
3db16 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63   ON INSERT refac
3db17 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  t */ yytestcase(
3db18 79 79 72 75 6c 65 6e 6f 3d 3d 37 35 29 3b 0d 0a  yyruleno==75);..
3db19 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3db1a 32 31 35 2e 76 61 6c 75 65 20 3d 20 30 3b 20 20  215.value = 0;  
3db1b 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3db1c 79 32 31 35 2e 6d 61 73 6b 20 3d 20 30 78 30 30  y215.mask = 0x00
3db1d 30 30 30 30 3b 20 7d 0d 0a 20 20 20 20 20 20 20  0000; }..       
3db1e 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3db1f 61 73 65 20 37 36 3a 20 2f 2a 20 72 65 66 61 72  ase 76: /* refar
3db20 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54 45 20  g ::= ON DELETE 
3db21 72 65 66 61 63 74 20 2a 2f 0d 0a 7b 20 79 79 67  refact */..{ yyg
3db22 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 35 2e 76  otominor.yy215.v
3db23 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  alue = yymsp[0].
3db24 6d 69 6e 6f 72 2e 79 79 34 3b 20 20 20 20 20 79  minor.yy4;     y
3db25 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 35  ygotominor.yy215
3db26 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30 66 66  .mask = 0x0000ff
3db27 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ; }..        bre
3db28 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3db29 37 37 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a  77: /* refarg ::
3db2a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66 61  = ON UPDATE refa
3db2b 63 74 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d  ct */..{ yygotom
3db2c 69 6e 6f 72 2e 79 79 32 31 35 2e 76 61 6c 75 65  inor.yy215.value
3db2d 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
3db2e 72 2e 79 79 34 3c 3c 38 3b 20 20 79 79 67 6f 74  r.yy4<<8;  yygot
3db2f 6f 6d 69 6e 6f 72 2e 79 79 32 31 35 2e 6d 61 73  ominor.yy215.mas
3db30 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d 0d  k = 0x00ff00; }.
3db31 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3db32 0a 20 20 20 20 20 20 63 61 73 65 20 37 38 3a 20  .      case 78: 
3db33 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  /* refact ::= SE
3db34 54 20 4e 55 4c 4c 20 2a 2f 0d 0a 7b 20 79 79 67  T NULL */..{ yyg
3db35 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20 4f  otominor.yy4 = O
3db36 45 5f 53 65 74 4e 75 6c 6c 3b 20 20 2f 2a 20 45  E_SetNull;  /* E
3db37 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32  V: R-33326-45252
3db38 20 2a 2f 7d 0d 0a 20 20 20 20 20 20 20 20 62 72   */}..        br
3db39 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3db3a 20 37 39 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   79: /* refact :
3db3b 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54 20 2a  := SET DEFAULT *
3db3c 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  /..{ yygotominor
3db3d 2e 79 79 34 20 3d 20 4f 45 5f 53 65 74 44 66 6c  .yy4 = OE_SetDfl
3db3e 74 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33  t;  /* EV: R-333
3db3f 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0d 0a 20 20  26-45252 */}..  
3db40 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3db41 20 20 20 20 63 61 73 65 20 38 30 3a 20 2f 2a 20      case 80: /* 
3db42 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41  refact ::= CASCA
3db43 44 45 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d  DE */..{ yygotom
3db44 69 6e 6f 72 2e 79 79 34 20 3d 20 4f 45 5f 43 61  inor.yy4 = OE_Ca
3db45 73 63 61 64 65 3b 20 20 2f 2a 20 45 56 3a 20 52  scade;  /* EV: R
3db46 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d  -33326-45252 */}
3db47 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3db48 0d 0a 20 20 20 20 20 20 63 61 73 65 20 38 31 3a  ..      case 81:
3db49 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 52   /* refact ::= R
3db4a 45 53 54 52 49 43 54 20 2a 2f 0d 0a 7b 20 79 79  ESTRICT */..{ yy
3db4b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20  gotominor.yy4 = 
3db4c 4f 45 5f 52 65 73 74 72 69 63 74 3b 20 2f 2a 20  OE_Restrict; /* 
3db4d 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35  EV: R-33326-4525
3db4e 32 20 2a 2f 7d 0d 0a 20 20 20 20 20 20 20 20 62  2 */}..        b
3db4f 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3db50 65 20 38 32 3a 20 2f 2a 20 72 65 66 61 63 74 20  e 82: /* refact 
3db51 3a 3a 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a 2f  ::= NO ACTION */
3db52 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ..{ yygotominor.
3db53 79 79 34 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 20 20  yy4 = OE_None;  
3db54 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32     /* EV: R-3332
3db55 36 2d 34 35 32 35 32 20 2a 2f 7d 0d 0a 20 20 20  6-45252 */}..   
3db56 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3db57 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a 20 64     case 84: /* d
3db58 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
3db59 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  := DEFERRABLE in
3db5a 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
3db5b 5f 6f 70 74 20 2a 2f 0d 0a 20 20 20 20 20 20 63  _opt */..      c
3db5c 61 73 65 20 39 39 3a 20 2f 2a 20 64 65 66 65 72  ase 99: /* defer
3db5d 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
3db5e 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  := defer_subclau
3db5f 73 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  se */ yytestcase
3db60 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 39 29 3b 0d  (yyruleno==99);.
3db61 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 31 3a  .      case 101:
3db62 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f   /* onconf ::= O
3db63 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c  N CONFLICT resol
3db64 76 65 74 79 70 65 20 2a 2f 20 79 79 74 65 73 74  vetype */ yytest
3db65 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3db66 30 31 29 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  01);..      case
3db67 20 31 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65   104: /* resolve
3db68 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79  type ::= raisety
3db69 70 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pe */ yytestcase
3db6a 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 34 29 3b  (yyruleno==104);
3db6b 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ..{yygotominor.y
3db6c 79 34 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  y4 = yymsp[0].mi
3db6d 6e 6f 72 2e 79 79 34 3b 7d 0d 0a 20 20 20 20 20  nor.yy4;}..     
3db6e 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3db6f 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e   case 88: /* con
3db70 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  slist_opt ::= */
3db71 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ..{yygotominor.y
3db72 79 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f  y0.n = 0; yygoto
3db73 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b  minor.yy0.z = 0;
3db74 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3db75 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 38 39  ;..      case 89
3db76 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  : /* conslist_op
3db77 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73  t ::= COMMA cons
3db78 6c 69 73 74 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f  list */..{yygoto
3db79 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73  minor.yy0 = yyms
3db7a 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b  p[-1].minor.yy0;
3db7b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3db7c 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 39 34  ;..      case 94
3db7d 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 50  : /* tcons ::= P
3db7e 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64  RIMARY KEY LP id
3db7f 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50  xlist autoinc RP
3db80 20 6f 6e 63 6f 6e 66 20 2a 2f 0d 0a 7b 73 71 6c   onconf */..{sql
3db81 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
3db82 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  y(pParse,yymsp[-
3db83 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 79  3].minor.yy322,y
3db84 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3db85 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  4,yymsp[-2].mino
3db86 72 2e 79 79 34 2c 30 29 3b 7d 0d 0a 20 20 20 20  r.yy4,0);}..    
3db87 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3db88 20 20 63 61 73 65 20 39 35 3a 20 2f 2a 20 74 63    case 95: /* tc
3db89 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c  ons ::= UNIQUE L
3db8a 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e 63  P idxlist RP onc
3db8b 6f 6e 66 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33  onf */..{sqlite3
3db8c 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
3db8d 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d  se,0,0,0,yymsp[-
3db8e 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 79  2].minor.yy322,y
3db8f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3db90 34 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0d 0a 20 20  4,0,0,0,0);}..  
3db91 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3db92 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f 2a 20      case 96: /* 
3db93 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20  tcons ::= CHECK 
3db94 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e  LP expr RP oncon
3db95 66 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 41 64  f */..{sqlite3Ad
3db96 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
3db97 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
3db98 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  ].minor.yy118.pE
3db99 78 70 72 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20  xpr);}..        
3db9a 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3db9b 73 65 20 39 37 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 97: /* tcons 
3db9c 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
3db9d 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
3db9e 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
3db9f 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
3dba0 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
3dba1 6f 70 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 73  opt */..{..    s
3dba2 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
3dba3 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79  ignKey(pParse, y
3dba4 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
3dba5 79 33 32 32 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  y322, &yymsp[-3]
3dba6 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73  .minor.yy0, yyms
3dba7 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-2].minor.yy32
3dba8 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  2, yymsp[-1].min
3dba9 6f 72 2e 79 79 34 29 3b 0d 0a 20 20 20 20 73 71  or.yy4);..    sq
3dbaa 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
3dbab 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
3dbac 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 29  sp[0].minor.yy4)
3dbad 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3dbae 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3dbaf 20 31 30 30 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20   100: /* onconf 
3dbb0 3a 3a 3d 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d  ::= */..{yygotom
3dbb1 69 6e 6f 72 2e 79 79 34 20 3d 20 4f 45 5f 44 65  inor.yy4 = OE_De
3dbb2 66 61 75 6c 74 3b 7d 0d 0a 20 20 20 20 20 20 20  fault;}..       
3dbb3 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3dbb4 61 73 65 20 31 30 32 3a 20 2f 2a 20 6f 72 63 6f  ase 102: /* orco
3dbb5 6e 66 20 3a 3a 3d 20 2a 2f 0d 0a 7b 79 79 67 6f  nf ::= */..{yygo
3dbb6 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 30 20 3d 20  tominor.yy210 = 
3dbb7 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0d 0a 20 20  OE_Default;}..  
3dbb8 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3dbb9 20 20 20 20 63 61 73 65 20 31 30 33 3a 20 2f 2a      case 103: /*
3dbba 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72   orconf ::= OR r
3dbbb 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0d 0a 7b  esolvetype */..{
3dbbc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31  yygotominor.yy21
3dbbd 30 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  0 = (u8)yymsp[0]
3dbbe 2e 6d 69 6e 6f 72 2e 79 79 34 3b 7d 0d 0a 20 20  .minor.yy4;}..  
3dbbf 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3dbc0 20 20 20 20 63 61 73 65 20 31 30 35 3a 20 2f 2a      case 105: /*
3dbc1 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d   resolvetype ::=
3dbc2 20 49 47 4e 4f 52 45 20 2a 2f 0d 0a 7b 79 79 67   IGNORE */..{yyg
3dbc3 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20 4f  otominor.yy4 = O
3dbc4 45 5f 49 67 6e 6f 72 65 3b 7d 0d 0a 20 20 20 20  E_Ignore;}..    
3dbc5 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3dbc6 20 20 63 61 73 65 20 31 30 36 3a 20 2f 2a 20 72    case 106: /* r
3dbc7 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52  esolvetype ::= R
3dbc8 45 50 4c 41 43 45 20 2a 2f 0d 0a 7b 79 79 67 6f  EPLACE */..{yygo
3dbc9 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20 4f 45  tominor.yy4 = OE
3dbca 5f 52 65 70 6c 61 63 65 3b 7d 0d 0a 20 20 20 20  _Replace;}..    
3dbcb 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3dbcc 20 20 63 61 73 65 20 31 30 37 3a 20 2f 2a 20 63    case 107: /* c
3dbcd 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c  md ::= DROP TABL
3dbce 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  E ifexists fulln
3dbcf 61 6d 65 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c  ame */..{..  sql
3dbd0 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50  ite3DropTable(pP
3dbd1 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
3dbd2 69 6e 6f 72 2e 79 79 32 35 39 2c 20 30 2c 20 79  inor.yy259, 0, y
3dbd3 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3dbd4 79 34 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20  y4);..}..       
3dbd5 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3dbd6 61 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d 64 20  ase 110: /* cmd 
3dbd7 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
3dbd8 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73  p VIEW ifnotexis
3dbd9 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65  ts nm dbnm AS se
3dbda 6c 65 63 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71  lect */..{..  sq
3dbdb 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
3dbdc 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
3dbdd 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  7].minor.yy0, &y
3dbde 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3dbdf 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y0, &yymsp[-2].m
3dbe0 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
3dbe1 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 2c 20  0].minor.yy387, 
3dbe2 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
3dbe3 79 79 34 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  yy4, yymsp[-4].m
3dbe4 69 6e 6f 72 2e 79 79 34 29 3b 0d 0a 7d 0d 0a 20  inor.yy4);..}.. 
3dbe5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3dbe6 20 20 20 20 20 63 61 73 65 20 31 31 31 3a 20 2f       case 111: /
3dbe7 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56  * cmd ::= DROP V
3dbe8 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c  IEW ifexists ful
3dbe9 6c 6e 61 6d 65 20 2a 2f 0d 0a 7b 0d 0a 20 20 73  lname */..{..  s
3dbea 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28  qlite3DropTable(
3dbeb 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
3dbec 2e 6d 69 6e 6f 72 2e 79 79 32 35 39 2c 20 31 2c  .minor.yy259, 1,
3dbed 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3dbee 2e 79 79 34 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  .yy4);..}..     
3dbef 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3dbf0 20 63 61 73 65 20 31 31 32 3a 20 2f 2a 20 63 6d   case 112: /* cm
3dbf1 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f 0d  d ::= select */.
3dbf2 0a 7b 0d 0a 20 20 53 65 6c 65 63 74 44 65 73 74  .{..  SelectDest
3dbf3 20 64 65 73 74 20 3d 20 7b 53 52 54 5f 4f 75 74   dest = {SRT_Out
3dbf4 70 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d  put, 0, 0, 0, 0}
3dbf5 3b 0d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  ;..  sqlite3Sele
3dbf6 63 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ct(pParse, yymsp
3dbf7 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 2c  [0].minor.yy387,
3dbf8 20 26 64 65 73 74 29 3b 0d 0a 20 20 73 71 6c 69   &dest);..  sqli
3dbf9 74 65 33 45 78 70 6c 61 69 6e 42 65 67 69 6e 28  te3ExplainBegin(
3dbfa 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0d  pParse->pVdbe);.
3dbfb 0a 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  .  sqlite3Explai
3dbfc 6e 53 65 6c 65 63 74 28 70 50 61 72 73 65 2d 3e  nSelect(pParse->
3dbfd 70 56 64 62 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  pVdbe, yymsp[0].
3dbfe 6d 69 6e 6f 72 2e 79 79 33 38 37 29 3b 0d 0a 20  minor.yy387);.. 
3dbff 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 46   sqlite3ExplainF
3dc00 69 6e 69 73 68 28 70 50 61 72 73 65 2d 3e 70 56  inish(pParse->pV
3dc01 64 62 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  dbe);..  sqlite3
3dc02 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
3dc03 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30  rse->db, yymsp[0
3dc04 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 29 3b 0d  ].minor.yy387);.
3dc05 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  .}..        brea
3dc06 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  k;..      case 1
3dc07 31 33 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a  13: /* select ::
3dc08 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0d 0a  = oneselect */..
3dc09 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
3dc0a 38 37 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  87 = yymsp[0].mi
3dc0b 6e 6f 72 2e 79 79 33 38 37 3b 7d 0d 0a 20 20 20  nor.yy387;}..   
3dc0c 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3dc0d 20 20 20 63 61 73 65 20 31 31 34 3a 20 2f 2a 20     case 114: /* 
3dc0e 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63  select ::= selec
3dc0f 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70  t multiselect_op
3dc10 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0d 0a 7b   oneselect */..{
3dc11 0d 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d  ..  if( yymsp[0]
3dc12 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 20 29 7b 0d  .minor.yy387 ){.
3dc13 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  .    yymsp[0].mi
3dc14 6e 6f 72 2e 79 79 33 38 37 2d 3e 6f 70 20 3d 20  nor.yy387->op = 
3dc15 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  (u8)yymsp[-1].mi
3dc16 6e 6f 72 2e 79 79 34 3b 0d 0a 20 20 20 20 79 79  nor.yy4;..    yy
3dc17 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3dc18 38 37 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d  87->pPrior = yym
3dc19 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
3dc1a 38 37 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  87;..  }else{.. 
3dc1b 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
3dc1c 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
3dc1d 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  b, yymsp[-2].min
3dc1e 6f 72 2e 79 79 33 38 37 29 3b 0d 0a 20 20 7d 0d  or.yy387);..  }.
3dc1f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3dc20 79 33 38 37 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y387 = yymsp[0].
3dc21 6d 69 6e 6f 72 2e 79 79 33 38 37 3b 0d 0a 7d 0d  minor.yy387;..}.
3dc22 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3dc23 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 36 3a  .      case 116:
3dc24 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f   /* multiselect_
3dc25 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c  op ::= UNION ALL
3dc26 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f   */..{yygotomino
3dc27 72 2e 79 79 34 20 3d 20 54 4b 5f 41 4c 4c 3b 7d  r.yy4 = TK_ALL;}
3dc28 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3dc29 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38  ..      case 118
3dc2a 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 3a  : /* oneselect :
3dc2b 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e  := SELECT distin
3dc2c 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72  ct selcollist fr
3dc2d 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f  om where_opt gro
3dc2e 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f  upby_opt having_
3dc2f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20  opt orderby_opt 
3dc30 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0d 0a 7b 0d  limit_opt */..{.
3dc31 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3dc32 79 33 38 37 20 3d 20 73 71 6c 69 74 65 33 53 65  y387 = sqlite3Se
3dc33 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79  lectNew(pParse,y
3dc34 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
3dc35 79 33 32 32 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d  y322,yymsp[-5].m
3dc36 69 6e 6f 72 2e 79 79 32 35 39 2c 79 79 6d 73 70  inor.yy259,yymsp
3dc37 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 34  [-4].minor.yy314
3dc38 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
3dc39 2e 79 79 33 32 32 2c 79 79 6d 73 70 5b 2d 32 5d  .yy322,yymsp[-2]
3dc3a 2e 6d 69 6e 6f 72 2e 79 79 33 31 34 2c 79 79 6d  .minor.yy314,yym
3dc3b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3dc3c 32 32 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  22,yymsp[-7].min
3dc3d 6f 72 2e 79 79 34 2c 79 79 6d 73 70 5b 30 5d 2e  or.yy4,yymsp[0].
3dc3e 6d 69 6e 6f 72 2e 79 79 32 39 32 2e 70 4c 69 6d  minor.yy292.pLim
3dc3f 69 74 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  it,yymsp[0].mino
3dc40 72 2e 79 79 32 39 32 2e 70 4f 66 66 73 65 74 29  r.yy292.pOffset)
3dc41 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3dc42 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3dc43 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a   122: /* sclp ::
3dc44 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d  = selcollist COM
3dc45 4d 41 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73  MA */..      cas
3dc46 65 20 32 34 37 3a 20 2f 2a 20 69 64 78 6c 69 73  e 247: /* idxlis
3dc47 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78  t_opt ::= LP idx
3dc48 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73  list RP */ yytes
3dc49 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3dc4a 32 34 37 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69  247);..{yygotomi
3dc4b 6e 6f 72 2e 79 79 33 32 32 20 3d 20 79 79 6d 73  nor.yy322 = yyms
3dc4c 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
3dc4d 32 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  2;}..        bre
3dc4e 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3dc4f 31 32 33 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d  123: /* sclp ::=
3dc50 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73 65 20   */..      case 
3dc51 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  151: /* orderby_
3dc52 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
3dc53 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3dc54 31 35 31 29 3b 0d 0a 20 20 20 20 20 20 63 61 73  151);..      cas
3dc55 65 20 31 35 39 3a 20 2f 2a 20 67 72 6f 75 70 62  e 159: /* groupb
3dc56 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  y_opt ::= */ yyt
3dc57 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3dc58 3d 3d 31 35 39 29 3b 0d 0a 20 20 20 20 20 20 63  ==159);..      c
3dc59 61 73 65 20 32 34 30 3a 20 2f 2a 20 65 78 70 72  ase 240: /* expr
3dc5a 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  list ::= */ yyte
3dc5b 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3dc5c 3d 32 34 30 29 3b 0d 0a 20 20 20 20 20 20 63 61  =240);..      ca
3dc5d 73 65 20 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69  se 246: /* idxli
3dc5e 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  st_opt ::= */ yy
3dc5f 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3dc60 6f 3d 3d 32 34 36 29 3b 0d 0a 7b 79 79 67 6f 74  o==246);..{yygot
3dc61 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20 30  ominor.yy322 = 0
3dc62 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3dc63 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  k;..      case 1
3dc64 32 34 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73  24: /* selcollis
3dc65 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20  t ::= sclp expr 
3dc66 61 73 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 79 79 67  as */..{..   yyg
3dc67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d  otominor.yy322 =
3dc68 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3dc69 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 79  Append(pParse, y
3dc6a 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3dc6b 79 33 32 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  y322, yymsp[-1].
3dc6c 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3dc6d 72 29 3b 0d 0a 20 20 20 69 66 28 20 79 79 6d 73  r);..   if( yyms
3dc6e 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
3dc6f 3e 30 20 29 20 73 71 6c 69 74 65 33 45 78 70 72  >0 ) sqlite3Expr
3dc70 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72  ListSetName(pPar
3dc71 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
3dc72 79 79 33 32 32 2c 20 26 79 79 6d 73 70 5b 30 5d  yy322, &yymsp[0]
3dc73 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0d  .minor.yy0, 1);.
3dc74 0a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  .   sqlite3ExprL
3dc75 69 73 74 53 65 74 53 70 61 6e 28 70 50 61 72 73  istSetSpan(pPars
3dc76 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
3dc77 33 32 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  322,&yymsp[-1].m
3dc78 69 6e 6f 72 2e 79 79 31 31 38 29 3b 0d 0a 7d 0d  inor.yy118);..}.
3dc79 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3dc7a 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a  .      case 125:
3dc7b 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
3dc7c 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0d  := sclp STAR */.
3dc7d 0a 7b 0d 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  .{..  Expr *p = 
3dc7e 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61 72  sqlite3Expr(pPar
3dc7f 73 65 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20  se->db, TK_ALL, 
3dc80 30 29 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0);..  yygotomin
3dc81 6f 72 2e 79 79 33 32 32 20 3d 20 73 71 6c 69 74  or.yy322 = sqlit
3dc82 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3dc83 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3dc84 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 20  1].minor.yy322, 
3dc85 70 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20  p);..}..        
3dc86 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3dc87 73 65 20 31 32 36 3a 20 2f 2a 20 73 65 6c 63 6f  se 126: /* selco
3dc88 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e  llist ::= sclp n
3dc89 6d 20 44 4f 54 20 53 54 41 52 20 2a 2f 0d 0a 7b  m DOT STAR */..{
3dc8a 0d 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  ..  Expr *pRight
3dc8b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3dc8c 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20  pParse, TK_ALL, 
3dc8d 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  0, 0, &yymsp[0].
3dc8e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 45  minor.yy0);..  E
3dc8f 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c  xpr *pLeft = sql
3dc90 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3dc91 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
3dc92 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3dc93 79 79 30 29 3b 0d 0a 20 20 45 78 70 72 20 2a 70  yy0);..  Expr *p
3dc94 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  Dot = sqlite3PEx
3dc95 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
3dc96 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  T, pLeft, pRight
3dc97 2c 20 30 29 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d  , 0);..  yygotom
3dc98 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20 73 71 6c  inor.yy322 = sql
3dc99 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
3dc9a 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
3dc9b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c  -3].minor.yy322,
3dc9c 20 70 44 6f 74 29 3b 0d 0a 7d 0d 0a 20 20 20 20   pDot);..}..    
3dc9d 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3dc9e 20 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 61    case 129: /* a
3dc9f 73 20 3a 3a 3d 20 2a 2f 0d 0a 7b 79 79 67 6f 74  s ::= */..{yygot
3dca0 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30  ominor.yy0.n = 0
3dca1 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3dca2 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  k;..      case 1
3dca3 33 30 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20  30: /* from ::= 
3dca4 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  */..{yygotominor
3dca5 2e 79 79 32 35 39 20 3d 20 73 71 6c 69 74 65 33  .yy259 = sqlite3
3dca6 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61  DbMallocZero(pPa
3dca7 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28  rse->db, sizeof(
3dca8 2a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32  *yygotominor.yy2
3dca9 35 39 29 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20  59));}..        
3dcaa 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3dcab 73 65 20 31 33 31 3a 20 2f 2a 20 66 72 6f 6d 20  se 131: /* from 
3dcac 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61 62 6c  ::= FROM seltabl
3dcad 69 73 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67  ist */..{..  yyg
3dcae 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 20 3d  otominor.yy259 =
3dcaf 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3dcb0 79 79 32 35 39 3b 0d 0a 20 20 73 71 6c 69 74 65  yy259;..  sqlite
3dcb1 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
3dcb2 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f  nType(yygotomino
3dcb3 72 2e 79 79 32 35 39 29 3b 0d 0a 7d 0d 0a 20 20  r.yy259);..}..  
3dcb4 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3dcb5 20 20 20 20 63 61 73 65 20 31 33 32 3a 20 2f 2a      case 132: /*
3dcb6 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20   stl_prefix ::= 
3dcb7 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f  seltablist joino
3dcb8 70 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 79 79 67 6f  p */..{..   yygo
3dcb9 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 20 3d 20  tominor.yy259 = 
3dcba 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3dcbb 79 79 32 35 39 3b 0d 0a 20 20 20 69 66 28 20 41  yy259;..   if( A
3dcbc 4c 57 41 59 53 28 79 79 67 6f 74 6f 6d 69 6e 6f  LWAYS(yygotomino
3dcbd 72 2e 79 79 32 35 39 20 26 26 20 79 79 67 6f 74  r.yy259 && yygot
3dcbe 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 2d 3e 6e 53  ominor.yy259->nS
3dcbf 72 63 3e 30 29 20 29 20 79 79 67 6f 74 6f 6d 69  rc>0) ) yygotomi
3dcc0 6e 6f 72 2e 79 79 32 35 39 2d 3e 61 5b 79 79 67  nor.yy259->a[yyg
3dcc1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 2d 3e  otominor.yy259->
3dcc2 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65  nSrc-1].jointype
3dcc3 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e   = (u8)yymsp[0].
3dcc4 6d 69 6e 6f 72 2e 79 79 34 3b 0d 0a 7d 0d 0a 20  minor.yy4;..}.. 
3dcc5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3dcc6 20 20 20 20 20 63 61 73 65 20 31 33 33 3a 20 2f       case 133: /
3dcc7 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d  * stl_prefix ::=
3dcc8 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f   */..{yygotomino
3dcc9 72 2e 79 79 32 35 39 20 3d 20 30 3b 7d 0d 0a 20  r.yy259 = 0;}.. 
3dcca 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3dccb 20 20 20 20 20 63 61 73 65 20 31 33 34 3a 20 2f       case 134: /
3dccc 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d  * seltablist ::=
3dccd 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64   stl_prefix nm d
3dcce 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f  bnm as indexed_o
3dccf 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  pt on_opt using_
3dcd0 6f 70 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67  opt */..{..  yyg
3dcd1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 20 3d  otominor.yy259 =
3dcd2 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
3dcd3 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
3dcd4 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
3dcd5 69 6e 6f 72 2e 79 79 32 35 39 2c 26 79 79 6d 73  inor.yy259,&yyms
3dcd6 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
3dcd7 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
3dcd8 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  .yy0,&yymsp[-3].
3dcd9 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73  minor.yy0,0,yyms
3dcda 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-1].minor.yy31
3dcdb 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
3dcdc 2e 79 79 33 38 34 29 3b 0d 0a 20 20 73 71 6c 69  .yy384);..  sqli
3dcdd 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
3dcde 64 42 79 28 70 50 61 72 73 65 2c 20 79 79 67 6f  dBy(pParse, yygo
3dcdf 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 39 2c 20 26  tominor.yy259, &
3dce0 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3dce1 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  yy0);..}..      
3dce2 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dce3 63 61 73 65 20 31 33 35 3a 20 2f 2a 20 73 65 6c  case 135: /* sel
3dce4 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f  tablist ::= stl_
3dce5 70 72 65 66 69 78 20 4c 50 20 73 65 6c 65 63 74  prefix LP select
3dce6 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73   RP as on_opt us
3dce7 69 6e 67 5f 6f 70 74 20 2a 2f 0d 0a 7b 0d 0a 20  ing_opt */..{.. 
3dce8 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3dce9 79 32 35 39 20 3d 20 73 71 6c 69 74 65 33 53 72  y259 = sqlite3Sr
3dcea 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
3dceb 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70  erm(pParse,yymsp
3dcec 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 39  [-6].minor.yy259
3dced 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  ,0,0,&yymsp[-2].
3dcee 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b  minor.yy0,yymsp[
3dcef 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 2c  -4].minor.yy387,
3dcf0 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3dcf1 79 79 33 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d  yy314,yymsp[0].m
3dcf2 69 6e 6f 72 2e 79 79 33 38 34 29 3b 0d 0a 20 20  inor.yy384);..  
3dcf3 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3dcf4 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  ;..      case 13
3dcf5 36 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74  6: /* seltablist
3dcf6 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
3dcf7 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 20 52 50  LP seltablist RP
3dcf8 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67   as on_opt using
3dcf9 5f 6f 70 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 20  _opt */..{..    
3dcfa 69 66 28 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  if( yymsp[-6].mi
3dcfb 6e 6f 72 2e 79 79 32 35 39 3d 3d 30 20 26 26 20  nor.yy259==0 && 
3dcfc 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3dcfd 79 79 30 2e 6e 3d 3d 30 20 26 26 20 79 79 6d 73  yy0.n==0 && yyms
3dcfe 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-1].minor.yy31
3dcff 34 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 30 5d  4==0 && yymsp[0]
3dd00 2e 6d 69 6e 6f 72 2e 79 79 33 38 34 3d 3d 30 20  .minor.yy384==0 
3dd01 29 7b 0d 0a 20 20 20 20 20 20 79 79 67 6f 74 6f  ){..      yygoto
3dd02 6d 69 6e 6f 72 2e 79 79 32 35 39 20 3d 20 79 79  minor.yy259 = yy
3dd03 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3dd04 32 35 39 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  259;..    }else{
3dd05 0d 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  ..      Select *
3dd06 70 53 75 62 71 75 65 72 79 3b 0d 0a 20 20 20 20  pSubquery;..    
3dd07 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
3dd08 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79  ShiftJoinType(yy
3dd09 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3dd0a 32 35 39 29 3b 0d 0a 20 20 20 20 20 20 70 53 75  259);..      pSu
3dd0b 62 71 75 65 72 79 20 3d 20 73 71 6c 69 74 65 33  bquery = sqlite3
3dd0c 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
3dd0d 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  ,0,yymsp[-4].min
3dd0e 6f 72 2e 79 79 32 35 39 2c 30 2c 30 2c 30 2c 30  or.yy259,0,0,0,0
3dd0f 2c 30 2c 30 2c 30 29 3b 0d 0a 20 20 20 20 20 20  ,0,0,0);..      
3dd10 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35  yygotominor.yy25
3dd11 39 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  9 = sqlite3SrcLi
3dd12 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d  stAppendFromTerm
3dd13 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36  (pParse,yymsp[-6
3dd14 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 39 2c 30 2c  ].minor.yy259,0,
3dd15 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
3dd16 6f 72 2e 79 79 30 2c 70 53 75 62 71 75 65 72 79  or.yy0,pSubquery
3dd17 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
3dd18 2e 79 79 33 31 34 2c 79 79 6d 73 70 5b 30 5d 2e  .yy314,yymsp[0].
3dd19 6d 69 6e 6f 72 2e 79 79 33 38 34 29 3b 0d 0a 20  minor.yy384);.. 
3dd1a 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 20 20 20     }..  }..     
3dd1b 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3dd1c 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 64 62   case 137: /* db
3dd1d 6e 6d 20 3a 3a 3d 20 2a 2f 0d 0a 20 20 20 20 20  nm ::= */..     
3dd1e 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20 69 6e   case 146: /* in
3dd1f 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  dexed_opt ::= */
3dd20 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3dd21 6c 65 6e 6f 3d 3d 31 34 36 29 3b 0d 0a 7b 79 79  leno==146);..{yy
3dd22 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
3dd23 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
3dd24 79 30 2e 6e 3d 30 3b 7d 0d 0a 20 20 20 20 20 20  y0.n=0;}..      
3dd25 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dd26 63 61 73 65 20 31 33 39 3a 20 2f 2a 20 66 75 6c  case 139: /* ful
3dd27 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e  lname ::= nm dbn
3dd28 6d 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e  m */..{yygotomin
3dd29 6f 72 2e 79 79 32 35 39 20 3d 20 73 71 6c 69 74  or.yy259 = sqlit
3dd2a 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
3dd2b 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79  pParse->db,0,&yy
3dd2c 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3dd2d 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
3dd2e 72 2e 79 79 30 29 3b 7d 0d 0a 20 20 20 20 20 20  r.yy0);}..      
3dd2f 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dd30 63 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f 69  case 140: /* joi
3dd31 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
3dd32 49 4e 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d  IN */..{ yygotom
3dd33 69 6e 6f 72 2e 79 79 34 20 3d 20 4a 54 5f 49 4e  inor.yy4 = JT_IN
3dd34 4e 45 52 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20  NER; }..        
3dd35 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3dd36 73 65 20 31 34 31 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 141: /* joino
3dd37 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f  p ::= JOIN_KW JO
3dd38 49 4e 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d  IN */..{ yygotom
3dd39 69 6e 6f 72 2e 79 79 34 20 3d 20 73 71 6c 69 74  inor.yy4 = sqlit
3dd3a 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73  e3JoinType(pPars
3dd3b 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
3dd3c 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0d 0a  or.yy0,0,0); }..
3dd3d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3dd3e 20 20 20 20 20 20 63 61 73 65 20 31 34 32 3a 20        case 142: 
3dd3f 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
3dd40 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f  IN_KW nm JOIN */
3dd41 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ..{ yygotominor.
3dd42 79 79 34 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  yy4 = sqlite3Joi
3dd43 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
3dd44 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3dd45 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
3dd46 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0d 0a 20 20  or.yy0,0); }..  
3dd47 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3dd48 20 20 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a      case 143: /*
3dd49 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e   joinop ::= JOIN
3dd4a 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a  _KW nm nm JOIN *
3dd4b 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  /..{ yygotominor
3dd4c 2e 79 79 34 20 3d 20 73 71 6c 69 74 65 33 4a 6f  .yy4 = sqlite3Jo
3dd4d 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  inType(pParse,&y
3dd4e 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3dd4f 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y0,&yymsp[-2].mi
3dd50 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
3dd51 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d  1].minor.yy0); }
3dd52 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3dd53 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 34  ..      case 144
3dd54 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20  : /* on_opt ::= 
3dd55 4f 4e 20 65 78 70 72 20 2a 2f 0d 0a 20 20 20 20  ON expr */..    
3dd56 20 20 63 61 73 65 20 31 35 35 3a 20 2f 2a 20 73    case 155: /* s
3dd57 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72  ortitem ::= expr
3dd58 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dd59 79 72 75 6c 65 6e 6f 3d 3d 31 35 35 29 3b 0d 0a  yruleno==155);..
3dd5a 20 20 20 20 20 20 63 61 73 65 20 31 36 32 3a 20        case 162: 
3dd5b 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  /* having_opt ::
3dd5c 3d 20 48 41 56 49 4e 47 20 65 78 70 72 20 2a 2f  = HAVING expr */
3dd5d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3dd5e 6c 65 6e 6f 3d 3d 31 36 32 29 3b 0d 0a 20 20 20  leno==162);..   
3dd5f 20 20 20 63 61 73 65 20 31 36 39 3a 20 2f 2a 20     case 169: /* 
3dd60 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48  where_opt ::= WH
3dd61 45 52 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65  ERE expr */ yyte
3dd62 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3dd63 3d 31 36 39 29 3b 0d 0a 20 20 20 20 20 20 63 61  =169);..      ca
3dd64 73 65 20 32 33 35 3a 20 2f 2a 20 63 61 73 65 5f  se 235: /* case_
3dd65 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78  else ::= ELSE ex
3dd66 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
3dd67 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 35 29 3b  (yyruleno==235);
3dd68 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 37  ..      case 237
3dd69 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e  : /* case_operan
3dd6a 64 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79  d ::= expr */ yy
3dd6b 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3dd6c 6f 3d 3d 32 33 37 29 3b 0d 0a 7b 79 79 67 6f 74  o==237);..{yygot
3dd6d 6f 6d 69 6e 6f 72 2e 79 79 33 31 34 20 3d 20 79  ominor.yy314 = y
3dd6e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3dd6f 31 31 38 2e 70 45 78 70 72 3b 7d 0d 0a 20 20 20  118.pExpr;}..   
3dd70 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3dd71 20 20 20 63 61 73 65 20 31 34 35 3a 20 2f 2a 20     case 145: /* 
3dd72 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0d 0a 20  on_opt ::= */.. 
3dd73 20 20 20 20 20 63 61 73 65 20 31 36 31 3a 20 2f       case 161: /
3dd74 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d  * having_opt ::=
3dd75 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dd76 79 72 75 6c 65 6e 6f 3d 3d 31 36 31 29 3b 0d 0a  yruleno==161);..
3dd77 20 20 20 20 20 20 63 61 73 65 20 31 36 38 3a 20        case 168: 
3dd78 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  /* where_opt ::=
3dd79 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dd7a 79 72 75 6c 65 6e 6f 3d 3d 31 36 38 29 3b 0d 0a  yruleno==168);..
3dd7b 20 20 20 20 20 20 63 61 73 65 20 32 33 36 3a 20        case 236: 
3dd7c 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  /* case_else ::=
3dd7d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dd7e 79 72 75 6c 65 6e 6f 3d 3d 32 33 36 29 3b 0d 0a  yruleno==236);..
3dd7f 20 20 20 20 20 20 63 61 73 65 20 32 33 38 3a 20        case 238: 
3dd80 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  /* case_operand 
3dd81 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
3dd82 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 38 29  e(yyruleno==238)
3dd83 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;..{yygotominor.
3dd84 79 79 33 31 34 20 3d 20 30 3b 7d 0d 0a 20 20 20  yy314 = 0;}..   
3dd85 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3dd86 20 20 20 63 61 73 65 20 31 34 38 3a 20 2f 2a 20     case 148: /* 
3dd87 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
3dd88 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0d 0a  NOT INDEXED */..
3dd89 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
3dd8a 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  .z=0; yygotomino
3dd8b 72 2e 79 79 30 2e 6e 3d 31 3b 7d 0d 0a 20 20 20  r.yy0.n=1;}..   
3dd8c 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3dd8d 20 20 20 63 61 73 65 20 31 34 39 3a 20 2f 2a 20     case 149: /* 
3dd8e 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53  using_opt ::= US
3dd8f 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ING LP inscollis
3dd90 74 20 52 50 20 2a 2f 0d 0a 20 20 20 20 20 20 63  t RP */..      c
3dd91 61 73 65 20 31 38 31 3a 20 2f 2a 20 69 6e 73 63  ase 181: /* insc
3dd92 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  ollist_opt ::= L
3dd93 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20  P inscollist RP 
3dd94 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3dd95 72 75 6c 65 6e 6f 3d 3d 31 38 31 29 3b 0d 0a 7b  ruleno==181);..{
3dd96 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 38  yygotominor.yy38
3dd97 34 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  4 = yymsp[-1].mi
3dd98 6e 6f 72 2e 79 79 33 38 34 3b 7d 0d 0a 20 20 20  nor.yy384;}..   
3dd99 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3dd9a 20 20 20 63 61 73 65 20 31 35 30 3a 20 2f 2a 20     case 150: /* 
3dd9b 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  using_opt ::= */
3dd9c 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 30  ..      case 180
3dd9d 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  : /* inscollist_
3dd9e 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
3dd9f 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3dda0 31 38 30 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69  180);..{yygotomi
3dda1 6e 6f 72 2e 79 79 33 38 34 20 3d 20 30 3b 7d 0d  nor.yy384 = 0;}.
3dda2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3dda3 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 32 3a  .      case 152:
3dda4 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20   /* orderby_opt 
3dda5 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72  ::= ORDER BY sor
3dda6 74 6c 69 73 74 20 2a 2f 0d 0a 20 20 20 20 20 20  tlist */..      
3dda7 63 61 73 65 20 31 36 30 3a 20 2f 2a 20 67 72 6f  case 160: /* gro
3dda8 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f  upby_opt ::= GRO
3dda9 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74 20  UP BY nexprlist 
3ddaa 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3ddab 72 75 6c 65 6e 6f 3d 3d 31 36 30 29 3b 0d 0a 20  ruleno==160);.. 
3ddac 20 20 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f       case 239: /
3ddad 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e  * exprlist ::= n
3ddae 65 78 70 72 6c 69 73 74 20 2a 2f 20 79 79 74 65  exprlist */ yyte
3ddaf 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3ddb0 3d 32 33 39 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d  =239);..{yygotom
3ddb1 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20 79 79 6d  inor.yy322 = yym
3ddb2 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
3ddb3 32 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  2;}..        bre
3ddb4 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3ddb5 31 35 33 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  153: /* sortlist
3ddb6 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f   ::= sortlist CO
3ddb7 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72  MMA sortitem sor
3ddb8 74 6f 72 64 65 72 20 2a 2f 0d 0a 7b 0d 0a 20 20  torder */..{..  
3ddb9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
3ddba 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
3ddbb 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3ddbc 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
3ddbd 2e 79 79 33 32 32 2c 79 79 6d 73 70 5b 2d 31 5d  .yy322,yymsp[-1]
3ddbe 2e 6d 69 6e 6f 72 2e 79 79 33 31 34 29 3b 0d 0a  .minor.yy314);..
3ddbf 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3ddc0 72 2e 79 79 33 32 32 20 29 20 79 79 67 6f 74 6f  r.yy322 ) yygoto
3ddc1 6d 69 6e 6f 72 2e 79 79 33 32 32 2d 3e 61 5b 79  minor.yy322->a[y
3ddc2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32  ygotominor.yy322
3ddc3 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f  ->nExpr-1].sortO
3ddc4 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70  rder = (u8)yymsp
3ddc5 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 3b 0d 0a  [0].minor.yy4;..
3ddc6 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3ddc7 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  ;..      case 15
3ddc8 34 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a  4: /* sortlist :
3ddc9 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74  := sortitem sort
3ddca 6f 72 64 65 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 79  order */..{..  y
3ddcb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32  ygotominor.yy322
3ddcc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
3ddcd 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
3ddce 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
3ddcf 72 2e 79 79 33 31 34 29 3b 0d 0a 20 20 69 66 28  r.yy314);..  if(
3ddd0 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3ddd1 32 32 20 26 26 20 41 4c 57 41 59 53 28 79 79 67  22 && ALWAYS(yyg
3ddd2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 2d 3e  otominor.yy322->
3ddd3 61 29 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  a) ) yygotominor
3ddd4 2e 79 79 33 32 32 2d 3e 61 5b 30 5d 2e 73 6f 72  .yy322->a[0].sor
3ddd5 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d  tOrder = (u8)yym
3ddd6 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 3b  sp[0].minor.yy4;
3ddd7 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ..}..        bre
3ddd8 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3ddd9 31 35 36 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65  156: /* sortorde
3ddda 72 20 3a 3a 3d 20 41 53 43 20 2a 2f 0d 0a 20 20  r ::= ASC */..  
3dddb 20 20 20 20 63 61 73 65 20 31 35 38 3a 20 2f 2a      case 158: /*
3dddc 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 2a   sortorder ::= *
3dddd 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3ddde 75 6c 65 6e 6f 3d 3d 31 35 38 29 3b 0d 0a 7b 79  uleno==158);..{y
3dddf 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d  ygotominor.yy4 =
3dde0 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 3b 7d   SQLITE_SO_ASC;}
3dde1 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3dde2 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 37  ..      case 157
3dde3 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a  : /* sortorder :
3dde4 3a 3d 20 44 45 53 43 20 2a 2f 0d 0a 7b 79 79 67  := DESC */..{yyg
3dde5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20 53  otominor.yy4 = S
3dde6 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 3b 7d 0d  QLITE_SO_DESC;}.
3dde7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3dde8 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 33 3a  .      case 163:
3dde9 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a   /* limit_opt ::
3ddea 3d 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e  = */..{yygotomin
3ddeb 6f 72 2e 79 79 32 39 32 2e 70 4c 69 6d 69 74 20  or.yy292.pLimit 
3ddec 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
3dded 2e 79 79 32 39 32 2e 70 4f 66 66 73 65 74 20 3d  .yy292.pOffset =
3ddee 20 30 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72   0;}..        br
3ddef 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3ddf0 20 31 36 34 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   164: /* limit_o
3ddf1 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
3ddf2 72 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e  r */..{yygotomin
3ddf3 6f 72 2e 79 79 32 39 32 2e 70 4c 69 6d 69 74 20  or.yy292.pLimit 
3ddf4 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3ddf5 2e 79 79 31 31 38 2e 70 45 78 70 72 3b 20 79 79  .yy118.pExpr; yy
3ddf6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 39 32 2e  gotominor.yy292.
3ddf7 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0d 0a 20  pOffset = 0;}.. 
3ddf8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3ddf9 20 20 20 20 20 63 61 73 65 20 31 36 35 3a 20 2f       case 165: /
3ddfa 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20  * limit_opt ::= 
3ddfb 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45  LIMIT expr OFFSE
3ddfc 54 20 65 78 70 72 20 2a 2f 0d 0a 7b 79 79 67 6f  T expr */..{yygo
3ddfd 74 6f 6d 69 6e 6f 72 2e 79 79 32 39 32 2e 70 4c  tominor.yy292.pL
3ddfe 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  imit = yymsp[-2]
3ddff 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  .minor.yy118.pEx
3de00 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr; yygotominor.
3de01 79 79 32 39 32 2e 70 4f 66 66 73 65 74 20 3d 20  yy292.pOffset = 
3de02 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3de03 79 31 31 38 2e 70 45 78 70 72 3b 7d 0d 0a 20 20  y118.pExpr;}..  
3de04 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3de05 20 20 20 20 63 61 73 65 20 31 36 36 3a 20 2f 2a      case 166: /*
3de06 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
3de07 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
3de08 65 78 70 72 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f  expr */..{yygoto
3de09 6d 69 6e 6f 72 2e 79 79 32 39 32 2e 70 4f 66 66  minor.yy292.pOff
3de0a 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  set = yymsp[-2].
3de0b 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3de0c 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
3de0d 79 32 39 32 2e 70 4c 69 6d 69 74 20 3d 20 79 79  y292.pLimit = yy
3de0e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
3de0f 31 38 2e 70 45 78 70 72 3b 7d 0d 0a 20 20 20 20  18.pExpr;}..    
3de10 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3de11 20 20 63 61 73 65 20 31 36 37 3a 20 2f 2a 20 63    case 167: /* c
3de12 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
3de13 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  OM fullname inde
3de14 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70  xed_opt where_op
3de15 74 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74  t */..{..  sqlit
3de16 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64  e3SrcListIndexed
3de17 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  By(pParse, yymsp
3de18 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 39  [-2].minor.yy259
3de19 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
3de1a 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 73 71 6c 69  or.yy0);..  sqli
3de1b 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50  te3DeleteFrom(pP
3de1c 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  arse,yymsp[-2].m
3de1d 69 6e 6f 72 2e 79 79 32 35 39 2c 79 79 6d 73 70  inor.yy259,yymsp
3de1e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 34 29  [0].minor.yy314)
3de1f 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3de20 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3de21 20 31 37 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   170: /* cmd ::=
3de22 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66   UPDATE orconf f
3de23 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f  ullname indexed_
3de24 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20  opt SET setlist 
3de25 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0d 0a 7b 0d  where_opt */..{.
3de26 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
3de27 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73  tIndexedBy(pPars
3de28 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  e, yymsp[-4].min
3de29 6f 72 2e 79 79 32 35 39 2c 20 26 79 79 6d 73 70  or.yy259, &yymsp
3de2a 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-3].minor.yy0);
3de2b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ..  sqlite3ExprL
3de2c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70  istCheckLength(p
3de2d 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e  Parse,yymsp[-1].
3de2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 22 73 65 74  minor.yy322,"set
3de2f 20 6c 69 73 74 22 29 3b 20 0d 0a 20 20 73 71 6c   list"); ..  sql
3de30 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73  ite3Update(pPars
3de31 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e,yymsp[-4].mino
3de32 72 2e 79 79 32 35 39 2c 79 79 6d 73 70 5b 2d 31  r.yy259,yymsp[-1
3de33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 79 79  ].minor.yy322,yy
3de34 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3de35 31 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  14,yymsp[-5].min
3de36 6f 72 2e 79 79 32 31 30 29 3b 0d 0a 7d 0d 0a 20  or.yy210);..}.. 
3de37 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3de38 20 20 20 20 20 63 61 73 65 20 31 37 31 3a 20 2f       case 171: /
3de39 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65  * setlist ::= se
3de3a 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45  tlist COMMA nm E
3de3b 51 20 65 78 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20  Q expr */..{..  
3de3c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
3de3d 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
3de3e 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3de3f 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
3de40 72 2e 79 79 33 32 32 2c 20 79 79 6d 73 70 5b 30  r.yy322, yymsp[0
3de41 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  ].minor.yy118.pE
3de42 78 70 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  xpr);..  sqlite3
3de43 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
3de44 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
3de45 6e 6f 72 2e 79 79 33 32 32 2c 20 26 79 79 6d 73  nor.yy322, &yyms
3de46 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3de47 20 31 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20   1);..}..       
3de48 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3de49 61 73 65 20 31 37 32 3a 20 2f 2a 20 73 65 74 6c  ase 172: /* setl
3de4a 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78  ist ::= nm EQ ex
3de4b 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f  pr */..{..  yygo
3de4c 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20  tominor.yy322 = 
3de4d 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3de4e 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
3de4f 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3de50 79 79 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a 20  yy118.pExpr);.. 
3de51 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3de52 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  SetName(pParse, 
3de53 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
3de54 32 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  2, &yymsp[-2].mi
3de55 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0d 0a 7d 0d  nor.yy0, 1);..}.
3de56 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3de57 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 33 3a  .      case 173:
3de58 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65   /* cmd ::= inse
3de59 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
3de5a 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
3de5b 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
3de5c 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0d 0a 7b 73  emlist RP */..{s
3de5d 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61  qlite3Insert(pPa
3de5e 72 73 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  rse, yymsp[-5].m
3de5f 69 6e 6f 72 2e 79 79 32 35 39 2c 20 79 79 6d 73  inor.yy259, yyms
3de60 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
3de61 32 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  2, 0, yymsp[-4].
3de62 6d 69 6e 6f 72 2e 79 79 33 38 34 2c 20 79 79 6d  minor.yy384, yym
3de63 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-7].minor.yy2
3de64 31 30 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62  10);}..        b
3de65 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3de66 65 20 31 37 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 174: /* cmd ::
3de67 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
3de68 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
3de69 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74  llist_opt select
3de6a 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 49 6e 73   */..{sqlite3Ins
3de6b 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ert(pParse, yyms
3de6c 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35  p[-2].minor.yy25
3de6d 39 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  9, 0, yymsp[0].m
3de6e 69 6e 6f 72 2e 79 79 33 38 37 2c 20 79 79 6d 73  inor.yy387, yyms
3de6f 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38  p[-1].minor.yy38
3de70 34 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  4, yymsp[-4].min
3de71 6f 72 2e 79 79 32 31 30 29 3b 7d 0d 0a 20 20 20  or.yy210);}..   
3de72 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3de73 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a 20     case 175: /* 
3de74 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
3de75 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
3de76 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
3de77 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 20 2a  DEFAULT VALUES *
3de78 2f 0d 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72  /..{sqlite3Inser
3de79 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  t(pParse, yymsp[
3de7a 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 39 2c  -3].minor.yy259,
3de7b 20 30 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 32 5d   0, 0, yymsp[-2]
3de7c 2e 6d 69 6e 6f 72 2e 79 79 33 38 34 2c 20 79 79  .minor.yy384, yy
3de7d 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
3de7e 32 31 30 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20  210);}..        
3de7f 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3de80 73 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73 65 72  se 176: /* inser
3de81 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54  t_cmd ::= INSERT
3de82 20 6f 72 63 6f 6e 66 20 2a 2f 0d 0a 7b 79 79 67   orconf */..{yyg
3de83 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 30 20 3d  otominor.yy210 =
3de84 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3de85 79 79 32 31 30 3b 7d 0d 0a 20 20 20 20 20 20 20  yy210;}..       
3de86 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3de87 61 73 65 20 31 37 37 3a 20 2f 2a 20 69 6e 73 65  ase 177: /* inse
3de88 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41  rt_cmd ::= REPLA
3de89 43 45 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69  CE */..{yygotomi
3de8a 6e 6f 72 2e 79 79 32 31 30 20 3d 20 4f 45 5f 52  nor.yy210 = OE_R
3de8b 65 70 6c 61 63 65 3b 7d 0d 0a 20 20 20 20 20 20  eplace;}..      
3de8c 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3de8d 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 69 74 65  case 178: /* ite
3de8e 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69  mlist ::= itemli
3de8f 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f  st COMMA expr */
3de90 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 31  ..      case 241
3de91 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a  : /* nexprlist :
3de92 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d  := nexprlist COM
3de93 4d 41 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  MA expr */ yytes
3de94 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3de95 32 34 31 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69  241);..{yygotomi
3de96 6e 6f 72 2e 79 79 33 32 32 20 3d 20 73 71 6c 69  nor.yy322 = sqli
3de97 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
3de98 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
3de99 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 79  2].minor.yy322,y
3de9a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3de9b 31 31 38 2e 70 45 78 70 72 29 3b 7d 0d 0a 20 20  118.pExpr);}..  
3de9c 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3de9d 20 20 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a      case 179: /*
3de9e 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78   itemlist ::= ex
3de9f 70 72 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73  pr */..      cas
3dea0 65 20 32 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 242: /* nexprl
3dea1 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20  ist ::= expr */ 
3dea2 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3dea3 65 6e 6f 3d 3d 32 34 32 29 3b 0d 0a 7b 79 79 67  eno==242);..{yyg
3dea4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d  otominor.yy322 =
3dea5 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3dea6 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
3dea7 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3dea8 79 31 31 38 2e 70 45 78 70 72 29 3b 7d 0d 0a 20  y118.pExpr);}.. 
3dea9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3deaa 20 20 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f       case 182: /
3deab 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * inscollist ::=
3deac 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d   inscollist COMM
3dead 41 20 6e 6d 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f  A nm */..{yygoto
3deae 6d 69 6e 6f 72 2e 79 79 33 38 34 20 3d 20 73 71  minor.yy384 = sq
3deaf 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
3deb0 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 79 79 6d  d(pParse->db,yym
3deb1 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
3deb2 38 34 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  84,&yymsp[0].min
3deb3 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20 20 20 20 20  or.yy0);}..     
3deb4 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3deb5 20 63 61 73 65 20 31 38 33 3a 20 2f 2a 20 69 6e   case 183: /* in
3deb6 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20  scollist ::= nm 
3deb7 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  */..{yygotominor
3deb8 2e 79 79 33 38 34 20 3d 20 73 71 6c 69 74 65 33  .yy384 = sqlite3
3deb9 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  IdListAppend(pPa
3deba 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70  rse->db,0,&yymsp
3debb 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
3debc 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3debd 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34  ..      case 184
3debe 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65  : /* expr ::= te
3debf 72 6d 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69  rm */..{yygotomi
3dec0 6e 6f 72 2e 79 79 31 31 38 20 3d 20 79 79 6d 73  nor.yy118 = yyms
3dec1 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38  p[0].minor.yy118
3dec2 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3dec3 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  k;..      case 1
3dec4 38 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  85: /* expr ::= 
3dec5 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0d 0a 7b  LP expr RP */..{
3dec6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3dec7 38 2e 70 45 78 70 72 20 3d 20 79 79 6d 73 70 5b  8.pExpr = yymsp[
3dec8 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  -1].minor.yy118.
3dec9 70 45 78 70 72 3b 20 73 70 61 6e 53 65 74 28 26  pExpr; spanSet(&
3deca 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3decb 38 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  8,&yymsp[-2].min
3decc 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
3decd 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20  .minor.yy0);}.. 
3dece 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3decf 20 20 20 20 20 63 61 73 65 20 31 38 36 3a 20 2f       case 186: /
3ded0 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 20  * term ::= NULL 
3ded1 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31  */..      case 1
3ded2 39 31 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20  91: /* term ::= 
3ded3 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c  INTEGER|FLOAT|BL
3ded4 4f 42 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  OB */ yytestcase
3ded5 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 31 29 3b  (yyruleno==191);
3ded6 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 32  ..      case 192
3ded7 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53 54  : /* term ::= ST
3ded8 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63 61  RING */ yytestca
3ded9 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 32  se(yyruleno==192
3deda 29 3b 0d 0a 7b 73 70 61 6e 45 78 70 72 28 26 79  );..{spanExpr(&y
3dedb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3dedc 2c 20 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  , pParse, yymsp[
3dedd 30 5d 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d 73 70  0].major, &yymsp
3dede 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
3dedf 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3dee0 0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 37  ..      case 187
3dee1 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 69 64  : /* expr ::= id
3dee2 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73 65 20   */..      case 
3dee3 31 38 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  188: /* expr ::=
3dee4 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79 74 65   JOIN_KW */ yyte
3dee5 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3dee6 3d 31 38 38 29 3b 0d 0a 7b 73 70 61 6e 45 78 70  =188);..{spanExp
3dee7 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
3dee8 79 31 31 38 2c 20 70 50 61 72 73 65 2c 20 54 4b  y118, pParse, TK
3dee9 5f 49 44 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  _ID, &yymsp[0].m
3deea 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20 20 20  inor.yy0);}..   
3deeb 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3deec 20 20 20 63 61 73 65 20 31 38 39 3a 20 2f 2a 20     case 189: /* 
3deed 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20  expr ::= nm DOT 
3deee 6e 6d 20 2a 2f 0d 0a 7b 0d 0a 20 20 45 78 70 72  nm */..{..  Expr
3deef 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65   *temp1 = sqlite
3def0 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3def1 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
3def2 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
3def3 29 3b 0d 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  );..  Expr *temp
3def4 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
3def5 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
3def6 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  0, 0, &yymsp[0].
3def7 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 79  minor.yy0);..  y
3def8 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3def9 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3defa 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3defb 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
3defc 70 32 2c 20 30 29 3b 0d 0a 20 20 73 70 61 6e 53  p2, 0);..  spanS
3defd 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  et(&yygotominor.
3defe 79 79 31 31 38 2c 26 79 79 6d 73 70 5b 2d 32 5d  yy118,&yymsp[-2]
3deff 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
3df00 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3df01 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ..}..        bre
3df02 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3df03 31 39 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  190: /* expr ::=
3df04 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e   nm DOT nm DOT n
3df05 6d 20 2a 2f 0d 0a 7b 0d 0a 20 20 45 78 70 72 20  m */..{..  Expr 
3df06 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33  *temp1 = sqlite3
3df07 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3df08 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
3df09 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-4].minor.yy0)
3df0a 3b 0d 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 32  ;..  Expr *temp2
3df0b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3df0c 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
3df0d 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  , 0, &yymsp[-2].
3df0e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 45  minor.yy0);..  E
3df0f 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
3df10 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3df11 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
3df12 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3df13 79 30 29 3b 0d 0a 20 20 45 78 70 72 20 2a 74 65  y0);..  Expr *te
3df14 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78  mp4 = sqlite3PEx
3df15 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
3df16 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c  T, temp2, temp3,
3df17 20 30 29 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69   0);..  yygotomi
3df18 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20  nor.yy118.pExpr 
3df19 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3df1a 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74  Parse, TK_DOT, t
3df1b 65 6d 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b  emp1, temp4, 0);
3df1c 0d 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ..  spanSet(&yyg
3df1d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 26  otominor.yy118,&
3df1e 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3df1f 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
3df20 6e 6f 72 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20  nor.yy0);..}..  
3df21 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3df22 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a      case 193: /*
3df23 20 65 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54   expr ::= REGIST
3df24 45 52 20 2a 2f 0d 0a 7b 0d 0a 20 20 2f 2a 20 57  ER */..{..  /* W
3df25 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74  hen doing a nest
3df26 65 64 20 70 61 72 73 65 2c 20 6f 6e 65 20 63 61  ed parse, one ca
3df27 6e 20 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20  n include terms 
3df28 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  in an expression
3df29 0d 0a 20 20 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b  ..  ** that look
3df2a 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 31   like this:   #1
3df2b 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74   #2 ...  These t
3df2c 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 65  erms refer to re
3df2d 67 69 73 74 65 72 73 0d 0a 20 20 2a 2a 20 69 6e  gisters..  ** in
3df2e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
3df2f 68 69 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65  hine.  #N is the
3df30 20 4e 2d 74 68 20 72 65 67 69 73 74 65 72 2e 20   N-th register. 
3df31 2a 2f 0d 0a 20 20 69 66 28 20 70 50 61 72 73 65  */..  if( pParse
3df32 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0d 0a  ->nested==0 ){..
3df33 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
3df34 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61  Msg(pParse, "nea
3df35 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78  r \"%T\": syntax
3df36 20 65 72 72 6f 72 22 2c 20 26 79 79 6d 73 70 5b   error", &yymsp[
3df37 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a  0].minor.yy0);..
3df38 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
3df39 79 79 31 31 38 2e 70 45 78 70 72 20 3d 20 30 3b  yy118.pExpr = 0;
3df3a 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
3df3b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3df3c 38 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  8.pExpr = sqlite
3df3d 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3df3e 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 2c 20 30  K_REGISTER, 0, 0
3df3f 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3df40 72 2e 79 79 30 29 3b 0d 0a 20 20 20 20 69 66 28  r.yy0);..    if(
3df41 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3df42 31 38 2e 70 45 78 70 72 20 29 20 73 71 6c 69 74  18.pExpr ) sqlit
3df43 65 33 47 65 74 49 6e 74 33 32 28 26 79 79 6d 73  e3GetInt32(&yyms
3df44 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
3df45 5b 31 5d 2c 20 26 79 79 67 6f 74 6f 6d 69 6e 6f  [1], &yygotomino
3df46 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2d 3e 69  r.yy118.pExpr->i
3df47 54 61 62 6c 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20  Table);..  }..  
3df48 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
3df49 69 6e 6f 72 2e 79 79 31 31 38 2c 20 26 79 79 6d  inor.yy118, &yym
3df4a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
3df4b 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3df4c 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  .yy0);..}..     
3df4d 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3df4e 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 65 78   case 194: /* ex
3df4f 70 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20  pr ::= VARIABLE 
3df50 2a 2f 0d 0a 7b 0d 0a 20 20 73 70 61 6e 45 78 70  */..{..  spanExp
3df51 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
3df52 79 31 31 38 2c 20 70 50 61 72 73 65 2c 20 54 4b  y118, pParse, TK
3df53 5f 56 41 52 49 41 42 4c 45 2c 20 26 79 79 6d 73  _VARIABLE, &yyms
3df54 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3df55 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 41  ..  sqlite3ExprA
3df56 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 70  ssignVarNumber(p
3df57 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3df58 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 29 3b  or.yy118.pExpr);
3df59 0d 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ..  spanSet(&yyg
3df5a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 20  otominor.yy118, 
3df5b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3df5c 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
3df5d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20  inor.yy0);..}.. 
3df5e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3df5f 20 20 20 20 20 63 61 73 65 20 31 39 35 3a 20 2f       case 195: /
3df60 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
3df61 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0d 0a  COLLATE ids */..
3df62 7b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  {..  yygotominor
3df63 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d 20 73  .yy118.pExpr = s
3df64 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c  qlite3ExprSetCol
3df65 6c 42 79 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c  lByToken(pParse,
3df66 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
3df67 2e 79 79 31 31 38 2e 70 45 78 70 72 2c 20 26 79  .yy118.pExpr, &y
3df68 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3df69 30 29 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0);..  yygotomin
3df6a 6f 72 2e 79 79 31 31 38 2e 7a 53 74 61 72 74 20  or.yy118.zStart 
3df6b 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
3df6c 72 2e 79 79 31 31 38 2e 7a 53 74 61 72 74 3b 0d  r.yy118.zStart;.
3df6d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3df6e 79 31 31 38 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y118.zEnd = &yym
3df6f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3df70 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
3df71 2e 79 79 30 2e 6e 5d 3b 0d 0a 7d 0d 0a 20 20 20  .yy0.n];..}..   
3df72 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3df73 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20     case 196: /* 
3df74 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50  expr ::= CAST LP
3df75 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b   expr AS typetok
3df76 65 6e 20 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 79  en RP */..{..  y
3df77 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3df78 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3df79 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3df7a 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b 2d 33 5d  _CAST, yymsp[-3]
3df7b 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  .minor.yy118.pEx
3df7c 70 72 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31  pr, 0, &yymsp[-1
3df7d 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20  ].minor.yy0);.. 
3df7e 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
3df7f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 26 79 79 6d  minor.yy118,&yym
3df80 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
3df81 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3df82 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  .yy0);..}..     
3df83 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3df84 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78   case 197: /* ex
3df85 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73  pr ::= ID LP dis
3df86 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52  tinct exprlist R
3df87 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 69 66 28 20 79  P */..{..  if( y
3df88 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3df89 79 33 32 32 20 26 26 20 79 79 6d 73 70 5b 2d 31  y322 && yymsp[-1
3df8a 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2d 3e 6e  ].minor.yy322->n
3df8b 45 78 70 72 3e 70 50 61 72 73 65 2d 3e 64 62 2d  Expr>pParse->db-
3df8c 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
3df8d 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
3df8e 47 5d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  G] ){..    sqlit
3df8f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
3df90 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67  e, "too many arg
3df91 75 6d 65 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69  uments on functi
3df92 6f 6e 20 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d  on %T", &yymsp[-
3df93 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a  4].minor.yy0);..
3df94 20 20 7d 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e    }..  yygotomin
3df95 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d  or.yy118.pExpr =
3df96 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63   sqlite3ExprFunc
3df97 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d  tion(pParse, yym
3df98 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3df99 32 32 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  22, &yymsp[-4].m
3df9a 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 73 70  inor.yy0);..  sp
3df9b 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
3df9c 6f 72 2e 79 79 31 31 38 2c 26 79 79 6d 73 70 5b  or.yy118,&yymsp[
3df9d 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -4].minor.yy0,&y
3df9e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3df9f 30 29 3b 0d 0a 20 20 69 66 28 20 79 79 6d 73 70  0);..  if( yymsp
3dfa0 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 20 26  [-2].minor.yy4 &
3dfa1 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  & yygotominor.yy
3dfa2 31 31 38 2e 70 45 78 70 72 20 29 7b 0d 0a 20 20  118.pExpr ){..  
3dfa3 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3dfa4 31 31 38 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73  118.pExpr->flags
3dfa5 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63 74 3b   |= EP_Distinct;
3dfa6 0d 0a 20 20 7d 0d 0a 7d 0d 0a 20 20 20 20 20 20  ..  }..}..      
3dfa7 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dfa8 63 61 73 65 20 31 39 38 3a 20 2f 2a 20 65 78 70  case 198: /* exp
3dfa9 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52  r ::= ID LP STAR
3dfaa 20 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67   RP */..{..  yyg
3dfab 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70  otominor.yy118.p
3dfac 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
3dfad 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  prFunction(pPars
3dfae 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  e, 0, &yymsp[-3]
3dfaf 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20 20  .minor.yy0);..  
3dfb0 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
3dfb1 69 6e 6f 72 2e 79 79 31 31 38 2c 26 79 79 6d 73  inor.yy118,&yyms
3dfb2 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
3dfb3 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3dfb4 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  yy0);..}..      
3dfb5 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dfb6 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 74 65 72  case 199: /* ter
3dfb7 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a  m ::= CTIME_KW *
3dfb8 2f 0d 0a 7b 0d 0a 20 20 2f 2a 20 54 68 65 20 43  /..{..  /* The C
3dfb9 55 52 52 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52  URRENT_TIME, CUR
3dfba 52 45 4e 54 5f 44 41 54 45 2c 20 61 6e 64 20 43  RENT_DATE, and C
3dfbb 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
3dfbc 20 76 61 6c 75 65 73 20 61 72 65 0d 0a 20 20 2a   values are..  *
3dfbd 2a 20 74 72 65 61 74 65 64 20 61 73 20 66 75 6e  * treated as fun
3dfbe 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75  ctions that retu
3dfbf 72 6e 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0d  rn constants */.
3dfc0 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3dfc1 79 31 31 38 2e 70 45 78 70 72 20 3d 20 73 71 6c  y118.pExpr = sql
3dfc2 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
3dfc3 28 70 50 61 72 73 65 2c 20 30 2c 26 79 79 6d 73  (pParse, 0,&yyms
3dfc4 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3dfc5 0d 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ..  if( yygotomi
3dfc6 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20  nor.yy118.pExpr 
3dfc7 29 7b 0d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  ){..    yygotomi
3dfc8 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2d  nor.yy118.pExpr-
3dfc9 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46  >op = TK_CONST_F
3dfca 55 4e 43 3b 20 20 0d 0a 20 20 7d 0d 0a 20 20 73  UNC;  ..  }..  s
3dfcb 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69  panSet(&yygotomi
3dfcc 6e 6f 72 2e 79 79 31 31 38 2c 20 26 79 79 6d 73  nor.yy118, &yyms
3dfcd 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  p[0].minor.yy0, 
3dfce 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3dfcf 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  yy0);..}..      
3dfd0 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3dfd1 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70  case 200: /* exp
3dfd2 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65  r ::= expr AND e
3dfd3 78 70 72 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61  xpr */..      ca
3dfd4 73 65 20 32 30 31 3a 20 2f 2a 20 65 78 70 72 20  se 201: /* expr 
3dfd5 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72  ::= expr OR expr
3dfd6 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dfd7 79 72 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0d 0a  yruleno==201);..
3dfd8 20 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20        case 202: 
3dfd9 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
3dfda 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
3dfdb 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
3dfdc 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 32 29 3b 0d  yyruleno==202);.
3dfdd 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 33 3a  .      case 203:
3dfde 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
3dfdf 72 20 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f 20  r EQ|NE expr */ 
3dfe0 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3dfe1 65 6e 6f 3d 3d 32 30 33 29 3b 0d 0a 20 20 20 20  eno==203);..    
3dfe2 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 65    case 204: /* e
3dfe3 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54  xpr ::= expr BIT
3dfe4 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54  AND|BITOR|LSHIFT
3dfe5 7c 52 53 48 49 46 54 20 65 78 70 72 20 2a 2f 20  |RSHIFT expr */ 
3dfe6 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3dfe7 65 6e 6f 3d 3d 32 30 34 29 3b 0d 0a 20 20 20 20  eno==204);..    
3dfe8 20 20 63 61 73 65 20 32 30 35 3a 20 2f 2a 20 65    case 205: /* e
3dfe9 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55  xpr ::= expr PLU
3dfea 53 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 20  S|MINUS expr */ 
3dfeb 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3dfec 65 6e 6f 3d 3d 32 30 35 29 3b 0d 0a 20 20 20 20  eno==205);..    
3dfed 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 65    case 206: /* e
3dfee 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41  xpr ::= expr STA
3dfef 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72  R|SLASH|REM expr
3dff0 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3dff1 79 72 75 6c 65 6e 6f 3d 3d 32 30 36 29 3b 0d 0a  yruleno==206);..
3dff2 20 20 20 20 20 20 63 61 73 65 20 32 30 37 3a 20        case 207: 
3dff3 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
3dff4 20 43 4f 4e 43 41 54 20 65 78 70 72 20 2a 2f 20   CONCAT expr */ 
3dff5 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3dff6 65 6e 6f 3d 3d 32 30 37 29 3b 0d 0a 7b 73 70 61  eno==207);..{spa
3dff7 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79 79 67  nBinaryExpr(&yyg
3dff8 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 70  otominor.yy118,p
3dff9 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e  Parse,yymsp[-1].
3dffa 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 2d 32 5d  major,&yymsp[-2]
3dffb 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 26 79 79  .minor.yy118,&yy
3dffc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
3dffd 31 38 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62  18);}..        b
3dffe 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3dfff 65 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f 70  e 208: /* likeop
3e000 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0d   ::= LIKE_KW */.
3e001 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 30 3a  .      case 210:
3e002 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d   /* likeop ::= M
3e003 41 54 43 48 20 2a 2f 20 79 79 74 65 73 74 63 61  ATCH */ yytestca
3e004 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 30  se(yyruleno==210
3e005 29 3b 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  );..{yygotominor
3e006 2e 79 79 33 34 32 2e 65 4f 70 65 72 61 74 6f 72  .yy342.eOperator
3e007 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
3e008 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  r.yy0; yygotomin
3e009 6f 72 2e 79 79 33 34 32 2e 6e 6f 74 20 3d 20 30  or.yy342.not = 0
3e00a 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3e00b 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3e00c 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a  09: /* likeop ::
3e00d 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f  = NOT LIKE_KW */
3e00e 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 31  ..      case 211
3e00f 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20  : /* likeop ::= 
3e010 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 20 79 79 74  NOT MATCH */ yyt
3e011 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e012 3d 3d 32 31 31 29 3b 0d 0a 7b 79 79 67 6f 74 6f  ==211);..{yygoto
3e013 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 65 4f 70 65  minor.yy342.eOpe
3e014 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d  rator = yymsp[0]
3e015 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f  .minor.yy0; yygo
3e016 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 6e 6f  tominor.yy342.no
3e017 74 20 3d 20 31 3b 7d 0d 0a 20 20 20 20 20 20 20  t = 1;}..       
3e018 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3e019 61 73 65 20 32 31 32 3a 20 2f 2a 20 65 78 70 72  ase 212: /* expr
3e01a 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70   ::= expr likeop
3e01b 20 65 78 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 45   expr */..{..  E
3e01c 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0d  xprList *pList;.
3e01d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
3e01e 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3e01f 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
3e020 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  [0].minor.yy118.
3e021 70 45 78 70 72 29 3b 0d 0a 20 20 70 4c 69 73 74  pExpr);..  pList
3e022 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
3e023 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
3e024 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 2d 32 5d  pList, yymsp[-2]
3e025 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  .minor.yy118.pEx
3e026 70 72 29 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69  pr);..  yygotomi
3e027 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20  nor.yy118.pExpr 
3e028 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
3e029 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c  ction(pParse, pL
3e02a 69 73 74 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  ist, &yymsp[-1].
3e02b 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 65 4f 70 65  minor.yy342.eOpe
3e02c 72 61 74 6f 72 29 3b 0d 0a 20 20 69 66 28 20 79  rator);..  if( y
3e02d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e02e 79 33 34 32 2e 6e 6f 74 20 29 20 79 79 67 6f 74  y342.not ) yygot
3e02f 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  ominor.yy118.pEx
3e030 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3e031 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
3e032 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
3e033 31 31 38 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  118.pExpr, 0, 0)
3e034 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ;..  yygotominor
3e035 2e 79 79 31 31 38 2e 7a 53 74 61 72 74 20 3d 20  .yy118.zStart = 
3e036 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3e037 79 79 31 31 38 2e 7a 53 74 61 72 74 3b 0d 0a 20  yy118.zStart;.. 
3e038 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e039 31 38 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b  18.zEnd = yymsp[
3e03a 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a  0].minor.yy118.z
3e03b 45 6e 64 3b 0d 0a 20 20 69 66 28 20 79 79 67 6f  End;..  if( yygo
3e03c 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e03d 78 70 72 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  xpr ) yygotomino
3e03e 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2d 3e 66  r.yy118.pExpr->f
3e03f 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 66 69 78  lags |= EP_Infix
3e040 46 75 6e 63 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  Func;..}..      
3e041 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3e042 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 65 78 70  case 213: /* exp
3e043 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f  r ::= expr likeo
3e044 70 20 65 78 70 72 20 45 53 43 41 50 45 20 65 78  p expr ESCAPE ex
3e045 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 45 78 70 72  pr */..{..  Expr
3e046 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0d 0a 20 20  List *pList;..  
3e047 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
3e048 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
3e049 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32  arse,0, yymsp[-2
3e04a 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  ].minor.yy118.pE
3e04b 78 70 72 29 3b 0d 0a 20 20 70 4c 69 73 74 20 3d  xpr);..  pList =
3e04c 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3e04d 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c  Append(pParse,pL
3e04e 69 73 74 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  ist, yymsp[-4].m
3e04f 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72  inor.yy118.pExpr
3e050 29 3b 0d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71  );..  pList = sq
3e051 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
3e052 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74  end(pParse,pList
3e053 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3e054 2e 79 79 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a  .yy118.pExpr);..
3e055 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3e056 31 31 38 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  118.pExpr = sqli
3e057 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
3e058 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26  pParse, pList, &
3e059 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3e05a 79 79 33 34 32 2e 65 4f 70 65 72 61 74 6f 72 29  yy342.eOperator)
3e05b 3b 0d 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  ;..  if( yymsp[-
3e05c 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 6e  3].minor.yy342.n
3e05d 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ot ) yygotominor
3e05e 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d 20 73  .yy118.pExpr = s
3e05f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
3e060 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
3e061 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e062 78 70 72 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 79  xpr, 0, 0);..  y
3e063 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3e064 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
3e065 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  -4].minor.yy118.
3e066 7a 53 74 61 72 74 3b 0d 0a 20 20 79 79 67 6f 74  zStart;..  yygot
3e067 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 45 6e  ominor.yy118.zEn
3e068 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  d = yymsp[0].min
3e069 6f 72 2e 79 79 31 31 38 2e 7a 45 6e 64 3b 0d 0a  or.yy118.zEnd;..
3e06a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3e06b 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20 29 20  r.yy118.pExpr ) 
3e06c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3e06d 38 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c  8.pExpr->flags |
3e06e 3d 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0d  = EP_InfixFunc;.
3e06f 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  .}..        brea
3e070 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3e071 31 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  14: /* expr ::= 
3e072 65 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e  expr ISNULL|NOTN
3e073 55 4c 4c 20 2a 2f 0d 0a 7b 73 70 61 6e 55 6e 61  ULL */..{spanUna
3e074 72 79 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74  ryPostfix(&yygot
3e075 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 70 50 61  ominor.yy118,pPa
3e076 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a  rse,yymsp[0].maj
3e077 6f 72 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  or,&yymsp[-1].mi
3e078 6e 6f 72 2e 79 79 31 31 38 2c 26 79 79 6d 73 70  nor.yy118,&yymsp
3e079 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
3e07a 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e07b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 35  ..      case 215
3e07c 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
3e07d 70 72 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0d 0a  pr NOT NULL */..
3e07e 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66 69  {spanUnaryPostfi
3e07f 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  x(&yygotominor.y
3e080 79 31 31 38 2c 70 50 61 72 73 65 2c 54 4b 5f 4e  y118,pParse,TK_N
3e081 4f 54 4e 55 4c 4c 2c 26 79 79 6d 73 70 5b 2d 32  OTNULL,&yymsp[-2
3e082 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 26 79  ].minor.yy118,&y
3e083 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3e084 30 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  0);}..        br
3e085 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e086 20 32 31 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   216: /* expr ::
3e087 3d 20 65 78 70 72 20 49 53 20 65 78 70 72 20 2a  = expr IS expr *
3e088 2f 0d 0a 7b 0d 0a 20 20 73 70 61 6e 42 69 6e 61  /..{..  spanBina
3e089 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69  ryExpr(&yygotomi
3e08a 6e 6f 72 2e 79 79 31 31 38 2c 70 50 61 72 73 65  nor.yy118,pParse
3e08b 2c 54 4b 5f 49 53 2c 26 79 79 6d 73 70 5b 2d 32  ,TK_IS,&yymsp[-2
3e08c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 26 79  ].minor.yy118,&y
3e08d 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3e08e 31 31 38 29 3b 0d 0a 20 20 62 69 6e 61 72 79 54  118);..  binaryT
3e08f 6f 55 6e 61 72 79 49 66 4e 75 6c 6c 28 70 50 61  oUnaryIfNull(pPa
3e090 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
3e091 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2c  nor.yy118.pExpr,
3e092 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e093 31 38 2e 70 45 78 70 72 2c 20 54 4b 5f 49 53 4e  18.pExpr, TK_ISN
3e094 55 4c 4c 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  ULL);..}..      
3e095 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3e096 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 65 78 70  case 217: /* exp
3e097 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f  r ::= expr IS NO
3e098 54 20 65 78 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20  T expr */..{..  
3e099 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26  spanBinaryExpr(&
3e09a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3e09b 38 2c 70 50 61 72 73 65 2c 54 4b 5f 49 53 4e 4f  8,pParse,TK_ISNO
3e09c 54 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  T,&yymsp[-3].min
3e09d 6f 72 2e 79 79 31 31 38 2c 26 79 79 6d 73 70 5b  or.yy118,&yymsp[
3e09e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 29 3b  0].minor.yy118);
3e09f 0d 0a 20 20 62 69 6e 61 72 79 54 6f 55 6e 61 72  ..  binaryToUnar
3e0a0 79 49 66 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20  yIfNull(pParse, 
3e0a1 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3e0a2 79 31 31 38 2e 70 45 78 70 72 2c 20 79 79 67 6f  y118.pExpr, yygo
3e0a3 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e0a4 78 70 72 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 29  xpr, TK_NOTNULL)
3e0a5 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3e0a6 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e0a7 20 32 31 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   218: /* expr ::
3e0a8 3d 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0d 0a 20  = NOT expr */.. 
3e0a9 20 20 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f       case 219: /
3e0aa 2a 20 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f  * expr ::= BITNO
3e0ab 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  T expr */ yytest
3e0ac 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3e0ad 31 39 29 3b 0d 0a 7b 73 70 61 6e 55 6e 61 72 79  19);..{spanUnary
3e0ae 50 72 65 66 69 78 28 26 79 79 67 6f 74 6f 6d 69  Prefix(&yygotomi
3e0af 6e 6f 72 2e 79 79 31 31 38 2c 70 50 61 72 73 65  nor.yy118,pParse
3e0b0 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72  ,yymsp[-1].major
3e0b1 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3e0b2 2e 79 79 31 31 38 2c 26 79 79 6d 73 70 5b 2d 31  .yy118,&yymsp[-1
3e0b3 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a  ].minor.yy0);}..
3e0b4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e0b5 20 20 20 20 20 20 63 61 73 65 20 32 32 30 3a 20        case 220: 
3e0b6 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55  /* expr ::= MINU
3e0b7 53 20 65 78 70 72 20 2a 2f 0d 0a 7b 73 70 61 6e  S expr */..{span
3e0b8 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79 67  UnaryPrefix(&yyg
3e0b9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 70  otominor.yy118,p
3e0ba 50 61 72 73 65 2c 54 4b 5f 55 4d 49 4e 55 53 2c  Parse,TK_UMINUS,
3e0bb 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3e0bc 79 79 31 31 38 2c 26 79 79 6d 73 70 5b 2d 31 5d  yy118,&yymsp[-1]
3e0bd 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20  .minor.yy0);}.. 
3e0be 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3e0bf 20 20 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f       case 221: /
3e0c0 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20  * expr ::= PLUS 
3e0c1 65 78 70 72 20 2a 2f 0d 0a 7b 73 70 61 6e 55 6e  expr */..{spanUn
3e0c2 61 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74  aryPrefix(&yygot
3e0c3 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2c 70 50 61  ominor.yy118,pPa
3e0c4 72 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26 79 79  rse,TK_UPLUS,&yy
3e0c5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
3e0c6 31 38 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  18,&yymsp[-1].mi
3e0c7 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a 20 20 20 20  nor.yy0);}..    
3e0c8 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3e0c9 20 20 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 65    case 224: /* e
3e0ca 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74  xpr ::= expr bet
3e0cb 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44  ween_op expr AND
3e0cc 20 65 78 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 45   expr */..{..  E
3e0cd 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
3e0ce 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3e0cf 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
3e0d0 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
3e0d1 2e 79 79 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a  .yy118.pExpr);..
3e0d2 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
3e0d3 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
3e0d4 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
3e0d5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
3e0d6 31 38 2e 70 45 78 70 72 29 3b 0d 0a 20 20 79 79  18.pExpr);..  yy
3e0d7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  gotominor.yy118.
3e0d8 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
3e0d9 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
3e0da 42 45 54 57 45 45 4e 2c 20 79 79 6d 73 70 5b 2d  BETWEEN, yymsp[-
3e0db 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70  4].minor.yy118.p
3e0dc 45 78 70 72 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  Expr, 0, 0);..  
3e0dd 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
3e0de 79 79 31 31 38 2e 70 45 78 70 72 20 29 7b 0d 0a  yy118.pExpr ){..
3e0df 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
3e0e0 79 79 31 31 38 2e 70 45 78 70 72 2d 3e 78 2e 70  yy118.pExpr->x.p
3e0e1 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0d 0a 20  List = pList;.. 
3e0e2 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
3e0e3 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
3e0e4 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
3e0e5 4c 69 73 74 29 3b 0d 0a 20 20 7d 20 0d 0a 20 20  List);..  } ..  
3e0e6 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  if( yymsp[-3].mi
3e0e7 6e 6f 72 2e 79 79 34 20 29 20 79 79 67 6f 74 6f  nor.yy4 ) yygoto
3e0e8 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3e0e9 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3e0ea 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
3e0eb 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e0ec 31 38 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  18.pExpr, 0, 0);
3e0ed 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ..  yygotominor.
3e0ee 79 79 31 31 38 2e 7a 53 74 61 72 74 20 3d 20 79  yy118.zStart = y
3e0ef 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3e0f0 79 31 31 38 2e 7a 53 74 61 72 74 3b 0d 0a 20 20  y118.zStart;..  
3e0f1 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3e0f2 38 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30  8.zEnd = yymsp[0
3e0f3 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 45  ].minor.yy118.zE
3e0f4 6e 64 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20  nd;..}..        
3e0f5 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3e0f6 73 65 20 32 32 37 3a 20 2f 2a 20 65 78 70 72 20  se 227: /* expr 
3e0f7 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c  ::= expr in_op L
3e0f8 50 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f  P exprlist RP */
3e0f9 0d 0a 7b 0d 0a 20 20 20 20 69 66 28 20 79 79 6d  ..{..    if( yym
3e0fa 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3e0fb 32 32 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  22==0 ){..      
3e0fc 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f  /* Expressions o
3e0fd 66 20 74 68 65 20 66 6f 72 6d 0d 0a 20 20 20 20  f the form..    
3e0fe 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 20    **..      **  
3e0ff 20 20 20 20 65 78 70 72 31 20 49 4e 20 28 29 0d      expr1 IN ().
3e100 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 65  .      **      e
3e101 78 70 72 31 20 4e 4f 54 20 49 4e 20 28 29 0d 0a  xpr1 NOT IN ()..
3e102 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
3e103 2a 2a 20 73 69 6d 70 6c 69 66 79 20 74 6f 20 63  ** simplify to c
3e104 6f 6e 73 74 61 6e 74 73 20 30 20 28 66 61 6c 73  onstants 0 (fals
3e105 65 29 20 61 6e 64 20 31 20 28 74 72 75 65 29 2c  e) and 1 (true),
3e106 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 0d 0a   respectively,..
3e107 20 20 20 20 20 20 2a 2a 20 72 65 67 61 72 64 6c        ** regardl
3e108 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ess of the value
3e109 20 6f 66 20 65 78 70 72 31 2e 0d 0a 20 20 20 20   of expr1...    
3e10a 20 20 2a 2f 0d 0a 20 20 20 20 20 20 79 79 67 6f    */..      yygo
3e10b 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e10c 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3e10d 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
3e10e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 26 73 71  TEGER, 0, 0, &sq
3e10f 6c 69 74 65 33 49 6e 74 54 6f 6b 65 6e 73 5b 79  lite3IntTokens[y
3e110 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3e111 79 34 5d 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  y4]);..      sql
3e112 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
3e113 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
3e114 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38  [-4].minor.yy118
3e115 2e 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 7d 65  .pExpr);..    }e
3e116 6c 73 65 7b 0d 0a 20 20 20 20 20 20 79 79 67 6f  lse{..      yygo
3e117 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e118 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3e119 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
3e11a 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
3e11b 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2c 20 30  r.yy118.pExpr, 0
3e11c 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66 28  , 0);..      if(
3e11d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e11e 31 38 2e 70 45 78 70 72 20 29 7b 0d 0a 20 20 20  18.pExpr ){..   
3e11f 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
3e120 2e 79 79 31 31 38 2e 70 45 78 70 72 2d 3e 78 2e  .yy118.pExpr->x.
3e121 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pList = yymsp[-1
3e122 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 3b 0d 0a  ].minor.yy322;..
3e123 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
3e124 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
3e125 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
3e126 2e 79 79 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a  .yy118.pExpr);..
3e127 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3e128 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3e129 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
3e12a 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
3e12b 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 29 3b 0d  ].minor.yy322);.
3e12c 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3e12d 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  if( yymsp[-3].mi
3e12e 6e 6f 72 2e 79 79 34 20 29 20 79 79 67 6f 74 6f  nor.yy4 ) yygoto
3e12f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3e130 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3e131 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
3e132 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e133 31 38 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  18.pExpr, 0, 0);
3e134 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 79 79 67  ..    }..    yyg
3e135 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a  otominor.yy118.z
3e136 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34  Start = yymsp[-4
3e137 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 53  ].minor.yy118.zS
3e138 74 61 72 74 3b 0d 0a 20 20 20 20 79 79 67 6f 74  tart;..    yygot
3e139 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 45 6e  ominor.yy118.zEn
3e13a 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
3e13b 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3e13c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
3e13d 0d 0a 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 62  ..  }..        b
3e13e 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e13f 65 20 32 32 38 3a 20 2f 2a 20 65 78 70 72 20 3a  e 228: /* expr :
3e140 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  := LP select RP 
3e141 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 79 79 67 6f 74  */..{..    yygot
3e142 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  ominor.yy118.pEx
3e143 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3e144 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c  r(pParse, TK_SEL
3e145 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a  ECT, 0, 0, 0);..
3e146 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69      if( yygotomi
3e147 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20  nor.yy118.pExpr 
3e148 29 7b 0d 0a 20 20 20 20 20 20 79 79 67 6f 74 6f  ){..      yygoto
3e149 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3e14a 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79  r->x.pSelect = y
3e14b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e14c 79 33 38 37 3b 0d 0a 20 20 20 20 20 20 45 78 70  y387;..      Exp
3e14d 72 53 65 74 50 72 6f 70 65 72 74 79 28 79 79 67  rSetProperty(yyg
3e14e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70  otominor.yy118.p
3e14f 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
3e150 63 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  ct);..      sqli
3e151 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
3e152 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
3e153 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72  inor.yy118.pExpr
3e154 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
3e155 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
3e156 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
3e157 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  ->db, yymsp[-1].
3e158 6d 69 6e 6f 72 2e 79 79 33 38 37 29 3b 0d 0a 20  minor.yy387);.. 
3e159 20 20 20 7d 0d 0a 20 20 20 20 79 79 67 6f 74 6f     }..    yygoto
3e15a 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 53 74 61  minor.yy118.zSta
3e15b 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  rt = yymsp[-2].m
3e15c 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0d 0a 20 20 20  inor.yy0.z;..   
3e15d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e15e 31 38 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70  18.zEnd = &yymsp
3e15f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
3e160 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3e161 79 30 2e 6e 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 20  y0.n];..  }..   
3e162 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3e163 20 20 20 63 61 73 65 20 32 32 39 3a 20 2f 2a 20     case 229: /* 
3e164 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e  expr ::= expr in
3e165 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50  _op LP select RP
3e166 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 79 79 67 6f   */..{..    yygo
3e167 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e168 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3e169 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
3e16a 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
3e16b 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2c 20 30  r.yy118.pExpr, 0
3e16c 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 79  , 0);..    if( y
3e16d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3e16e 2e 70 45 78 70 72 20 29 7b 0d 0a 20 20 20 20 20  .pExpr ){..     
3e16f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e170 31 38 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  18.pExpr->x.pSel
3e171 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  ect = yymsp[-1].
3e172 6d 69 6e 6f 72 2e 79 79 33 38 37 3b 0d 0a 20 20  minor.yy387;..  
3e173 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
3e174 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  rty(yygotominor.
3e175 79 79 31 31 38 2e 70 45 78 70 72 2c 20 45 50 5f  yy118.pExpr, EP_
3e176 78 49 73 53 65 6c 65 63 74 29 3b 0d 0a 20 20 20  xIsSelect);..   
3e177 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
3e178 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
3e179 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3e17a 38 2e 70 45 78 70 72 29 3b 0d 0a 20 20 20 20 7d  8.pExpr);..    }
3e17b 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c  else{..      sql
3e17c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
3e17d 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
3e17e 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3e17f 38 37 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  87);..    }..   
3e180 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
3e181 69 6e 6f 72 2e 79 79 34 20 29 20 79 79 67 6f 74  inor.yy4 ) yygot
3e182 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  ominor.yy118.pEx
3e183 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3e184 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
3e185 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
3e186 31 31 38 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  118.pExpr, 0, 0)
3e187 3b 0d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ;..    yygotomin
3e188 6f 72 2e 79 79 31 31 38 2e 7a 53 74 61 72 74 20  or.yy118.zStart 
3e189 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  = yymsp[-4].mino
3e18a 72 2e 79 79 31 31 38 2e 7a 53 74 61 72 74 3b 0d  r.yy118.zStart;.
3e18b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
3e18c 2e 79 79 31 31 38 2e 7a 45 6e 64 20 3d 20 26 79  .yy118.zEnd = &y
3e18d 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3e18e 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0.z[yymsp[0].min
3e18f 6f 72 2e 79 79 30 2e 6e 5d 3b 0d 0a 20 20 7d 0d  or.yy0.n];..  }.
3e190 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3e191 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 30 3a  .      case 230:
3e192 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
3e193 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20  r in_op nm dbnm 
3e194 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 53 72 63 4c 69  */..{..    SrcLi
3e195 73 74 20 2a 70 53 72 63 20 3d 20 73 71 6c 69 74  st *pSrc = sqlit
3e196 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
3e197 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79  pParse->db, 0,&y
3e198 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e199 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
3e19a 6f 72 2e 79 79 30 29 3b 0d 0a 20 20 20 20 79 79  or.yy0);..    yy
3e19b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  gotominor.yy118.
3e19c 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
3e19d 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
3e19e 49 4e 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  IN, yymsp[-3].mi
3e19f 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2c  nor.yy118.pExpr,
3e1a0 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66 28   0, 0);..    if(
3e1a1 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3e1a2 31 38 2e 70 45 78 70 72 20 29 7b 0d 0a 20 20 20  18.pExpr ){..   
3e1a3 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3e1a4 79 31 31 38 2e 70 45 78 70 72 2d 3e 78 2e 70 53  y118.pExpr->x.pS
3e1a5 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
3e1a6 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
3e1a7 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c   0,pSrc,0,0,0,0,
3e1a8 30 2c 30 2c 30 29 3b 0d 0a 20 20 20 20 20 20 45  0,0,0);..      E
3e1a9 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 79  xprSetProperty(y
3e1aa 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3e1ab 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  .pExpr, EP_xIsSe
3e1ac 6c 65 63 74 29 3b 0d 0a 20 20 20 20 20 20 73 71  lect);..      sq
3e1ad 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
3e1ae 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
3e1af 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  ominor.yy118.pEx
3e1b0 70 72 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  pr);..    }else{
3e1b1 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ..      sqlite3S
3e1b2 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  rcListDelete(pPa
3e1b3 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0d  rse->db, pSrc);.
3e1b4 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
3e1b5 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3e1b6 79 79 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  yy4 ) yygotomino
3e1b7 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d 20  r.yy118.pExpr = 
3e1b8 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3e1b9 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
3e1ba 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70  otominor.yy118.p
3e1bb 45 78 70 72 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  Expr, 0, 0);..  
3e1bc 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3e1bd 31 31 38 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  118.zStart = yym
3e1be 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
3e1bf 31 38 2e 7a 53 74 61 72 74 3b 0d 0a 20 20 20 20  18.zStart;..    
3e1c0 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31  yygotominor.yy11
3e1c1 38 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30  8.zEnd = yymsp[0
3e1c2 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3f 20  ].minor.yy0.z ? 
3e1c3 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3e1c4 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
3e1c5 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 3a 20 26 79  inor.yy0.n] : &y
3e1c6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e1c7 79 30 2e 7a 5b 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y0.z[yymsp[-1].m
3e1c8 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0d 0a 20 20  inor.yy0.n];..  
3e1c9 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3e1ca 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  ;..      case 23
3e1cb 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45  1: /* expr ::= E
3e1cc 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20  XISTS LP select 
3e1cd 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 45 78  RP */..{..    Ex
3e1ce 70 72 20 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69  pr *p = yygotomi
3e1cf 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20  nor.yy118.pExpr 
3e1d0 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3e1d1 50 61 72 73 65 2c 20 54 4b 5f 45 58 49 53 54 53  Parse, TK_EXISTS
3e1d2 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  , 0, 0, 0);..   
3e1d3 20 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 20   if( p ){..     
3e1d4 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20   p->x.pSelect = 
3e1d5 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3e1d6 79 79 33 38 37 3b 0d 0a 20 20 20 20 20 20 45 78  yy387;..      Ex
3e1d7 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c  prSetProperty(p,
3e1d8 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0d   EP_xIsSelect);.
3e1d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
3e1da 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
3e1db 73 65 2c 20 70 29 3b 0d 0a 20 20 20 20 7d 65 6c  se, p);..    }el
3e1dc 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
3e1dd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
3e1de 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
3e1df 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37  [-1].minor.yy387
3e1e0 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 79  );..    }..    y
3e1e1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38  ygotominor.yy118
3e1e2 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
3e1e3 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -3].minor.yy0.z;
3e1e4 0d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ..    yygotomino
3e1e5 72 2e 79 79 31 31 38 2e 7a 45 6e 64 20 3d 20 26  r.yy118.zEnd = &
3e1e6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3e1e7 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
3e1e8 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0d 0a 20 20 7d  nor.yy0.n];..  }
3e1e9 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e1ea 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 32  ..      case 232
3e1eb 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41  : /* expr ::= CA
3e1ec 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  SE case_operand 
3e1ed 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63 61  case_exprlist ca
3e1ee 73 65 5f 65 6c 73 65 20 45 4e 44 20 2a 2f 0d 0a  se_else END */..
3e1ef 7b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  {..  yygotominor
3e1f0 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d 20 73  .yy118.pExpr = s
3e1f1 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
3e1f2 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20 79 79 6d  se, TK_CASE, yym
3e1f3 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
3e1f4 31 34 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  14, yymsp[-1].mi
3e1f5 6e 6f 72 2e 79 79 33 31 34 2c 20 30 29 3b 0d 0a  nor.yy314, 0);..
3e1f6 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3e1f7 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20 29 7b  r.yy118.pExpr ){
3e1f8 0d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ..    yygotomino
3e1f9 72 2e 79 79 31 31 38 2e 70 45 78 70 72 2d 3e 78  r.yy118.pExpr->x
3e1fa 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d  .pList = yymsp[-
3e1fb 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 3b 0d  2].minor.yy322;.
3e1fc 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
3e1fd 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
3e1fe 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
3e1ff 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a 20 20 7d  118.pExpr);..  }
3e200 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74  else{..    sqlit
3e201 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
3e202 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
3e203 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
3e204 32 32 29 3b 0d 0a 20 20 7d 0d 0a 20 20 79 79 67  22);..  }..  yyg
3e205 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 7a  otominor.yy118.z
3e206 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34  Start = yymsp[-4
3e207 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0d 0a  ].minor.yy0.z;..
3e208 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3e209 31 31 38 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  118.zEnd = &yyms
3e20a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
3e20b 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
3e20c 79 79 30 2e 6e 5d 3b 0d 0a 7d 0d 0a 20 20 20 20  yy0.n];..}..    
3e20d 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3e20e 20 20 63 61 73 65 20 32 33 33 3a 20 2f 2a 20 63    case 233: /* c
3e20f 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
3e210 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
3e211 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
3e212 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f  pr */..{..  yygo
3e213 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20  tominor.yy322 = 
3e214 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3e215 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
3e216 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
3e217 32 32 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  22, yymsp[-2].mi
3e218 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 29  nor.yy118.pExpr)
3e219 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ;..  yygotominor
3e21a 2e 79 79 33 32 32 20 3d 20 73 71 6c 69 74 65 33  .yy322 = sqlite3
3e21b 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
3e21c 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
3e21d 72 2e 79 79 33 32 32 2c 20 79 79 6d 73 70 5b 30  r.yy322, yymsp[0
3e21e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  ].minor.yy118.pE
3e21f 78 70 72 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20  xpr);..}..      
3e220 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3e221 63 61 73 65 20 32 33 34 3a 20 2f 2a 20 63 61 73  case 234: /* cas
3e222 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57  e_exprlist ::= W
3e223 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
3e224 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f  pr */..{..  yygo
3e225 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d 20  tominor.yy322 = 
3e226 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3e227 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20  ppend(pParse,0, 
3e228 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3e229 79 79 31 31 38 2e 70 45 78 70 72 29 3b 0d 0a 20  yy118.pExpr);.. 
3e22a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3e22b 32 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  22 = sqlite3Expr
3e22c 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
3e22d 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
3e22e 33 32 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  322, yymsp[0].mi
3e22f 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 29  nor.yy118.pExpr)
3e230 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3e231 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e232 20 32 34 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   243: /* cmd ::=
3e233 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65   createkw unique
3e234 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74  flag INDEX ifnot
3e235 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f  exists nm dbnm O
3e236 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20  N nm LP idxlist 
3e237 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69  RP */..{..  sqli
3e238 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
3e239 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 36  Parse, &yymsp[-6
3e23a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
3e23b 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
3e23c 30 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  0, ..           
3e23d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3e23e 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70  3SrcListAppend(p
3e23f 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d  Parse->db,0,&yym
3e240 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
3e241 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ,0), yymsp[-1].m
3e242 69 6e 6f 72 2e 79 79 33 32 32 2c 20 79 79 6d 73  inor.yy322, yyms
3e243 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79 34 2c  p[-9].minor.yy4,
3e244 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3e245 20 20 20 20 20 20 20 20 26 79 79 6d 73 70 5b 2d          &yymsp[-
3e246 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  10].minor.yy0, &
3e247 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3e248 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53  y0, SQLITE_SO_AS
3e249 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  C, yymsp[-7].min
3e24a 6f 72 2e 79 79 34 29 3b 0d 0a 7d 0d 0a 20 20 20  or.yy4);..}..   
3e24b 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3e24c 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a 20     case 244: /* 
3e24d 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55  uniqueflag ::= U
3e24e 4e 49 51 55 45 20 2a 2f 0d 0a 20 20 20 20 20 20  NIQUE */..      
3e24f 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 72 61 69  case 298: /* rai
3e250 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54  setype ::= ABORT
3e251 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3e252 79 72 75 6c 65 6e 6f 3d 3d 32 39 38 29 3b 0d 0a  yruleno==298);..
3e253 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
3e254 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0d 0a 20   = OE_Abort;}.. 
3e255 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3e256 20 20 20 20 20 63 61 73 65 20 32 34 35 3a 20 2f       case 245: /
3e257 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d  * uniqueflag ::=
3e258 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f   */..{yygotomino
3e259 72 2e 79 79 34 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  r.yy4 = OE_None;
3e25a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3e25b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  ;..      case 24
3e25c 38 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a  8: /* idxlist ::
3e25d 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20  = idxlist COMMA 
3e25e 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f  nm collate sorto
3e25f 72 64 65 72 20 2a 2f 0d 0a 7b 0d 0a 20 20 45 78  rder */..{..  Ex
3e260 70 72 20 2a 70 20 3d 20 30 3b 0d 0a 20 20 69 66  pr *p = 0;..  if
3e261 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ( yymsp[-1].mino
3e262 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0d 0a 20 20  r.yy0.n>0 ){..  
3e263 20 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70    p = sqlite3Exp
3e264 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b  r(pParse->db, TK
3e265 5f 43 4f 4c 55 4d 4e 2c 20 30 29 3b 0d 0a 20 20  _COLUMN, 0);..  
3e266 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
3e267 43 6f 6c 6c 42 79 54 6f 6b 65 6e 28 70 50 61 72  CollByToken(pPar
3e268 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31  se, p, &yymsp[-1
3e269 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 20  ].minor.yy0);.. 
3e26a 20 7d 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f   }..  yygotomino
3e26b 72 2e 79 79 33 32 32 20 3d 20 73 71 6c 69 74 65  r.yy322 = sqlite
3e26c 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
3e26d 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d  pParse,yymsp[-4]
3e26e 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 20 70 29  .minor.yy322, p)
3e26f 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;..  sqlite3Expr
3e270 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72  ListSetName(pPar
3e271 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  se,yygotominor.y
3e272 79 33 32 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  y322,&yymsp[-2].
3e273 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 0d 0a 20  minor.yy0,1);.. 
3e274 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3e275 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
3e276 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
3e277 79 79 33 32 32 2c 20 22 69 6e 64 65 78 22 29 3b  yy322, "index");
3e278 0d 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ..  if( yygotomi
3e279 6e 6f 72 2e 79 79 33 32 32 20 29 20 79 79 67 6f  nor.yy322 ) yygo
3e27a 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 2d 3e 61  tominor.yy322->a
3e27b 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  [yygotominor.yy3
3e27c 32 32 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72  22->nExpr-1].sor
3e27d 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d  tOrder = (u8)yym
3e27e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 3b  sp[0].minor.yy4;
3e27f 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ..}..        bre
3e280 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3e281 32 34 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20  249: /* idxlist 
3e282 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73  ::= nm collate s
3e283 6f 72 74 6f 72 64 65 72 20 2a 2f 0d 0a 7b 0d 0a  ortorder */..{..
3e284 20 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0d 0a    Expr *p = 0;..
3e285 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e    if( yymsp[-1].
3e286 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b  minor.yy0.n>0 ){
3e287 0d 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ..    p = sqlite
3e288 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3e289 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20  K_COLUMN, 0, 0, 
3e28a 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  0);..    sqlite3
3e28b 45 78 70 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b  ExprSetCollByTok
3e28c 65 6e 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79  en(pParse, p, &y
3e28d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e28e 79 30 29 3b 0d 0a 20 20 7d 0d 0a 20 20 79 79 67  y0);..  }..  yyg
3e28f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20 3d  otominor.yy322 =
3e290 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3e291 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
3e292 20 70 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45   p);..  sqlite3E
3e293 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
3e294 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3e295 6f 72 2e 79 79 33 32 32 2c 20 26 79 79 6d 73 70  or.yy322, &yymsp
3e296 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
3e297 31 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 78  1);..  sqlite3Ex
3e298 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
3e299 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  h(pParse, yygoto
3e29a 6d 69 6e 6f 72 2e 79 79 33 32 32 2c 20 22 69 6e  minor.yy322, "in
3e29b 64 65 78 22 29 3b 0d 0a 20 20 69 66 28 20 79 79  dex");..  if( yy
3e29c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 32 20  gotominor.yy322 
3e29d 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
3e29e 33 32 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  322->a[yygotomin
3e29f 6f 72 2e 79 79 33 32 32 2d 3e 6e 45 78 70 72 2d  or.yy322->nExpr-
3e2a0 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
3e2a1 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
3e2a2 72 2e 79 79 34 3b 0d 0a 7d 0d 0a 20 20 20 20 20  r.yy4;..}..     
3e2a3 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3e2a4 20 63 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f   case 250: /* co
3e2a5 6c 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0d 0a 7b 79  llate ::= */..{y
3e2a6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a  ygotominor.yy0.z
3e2a7 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f   = 0; yygotomino
3e2a8 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0d 0a 20  r.yy0.n = 0;}.. 
3e2a9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3e2aa 20 20 20 20 20 63 61 73 65 20 32 35 32 3a 20 2f       case 252: /
3e2ab 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49  * cmd ::= DROP I
3e2ac 4e 44 45 58 20 69 66 65 78 69 73 74 73 20 66 75  NDEX ifexists fu
3e2ad 6c 6c 6e 61 6d 65 20 2a 2f 0d 0a 7b 73 71 6c 69  llname */..{sqli
3e2ae 74 65 33 44 72 6f 70 49 6e 64 65 78 28 70 50 61  te3DropIndex(pPa
3e2af 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
3e2b0 6e 6f 72 2e 79 79 32 35 39 2c 20 79 79 6d 73 70  nor.yy259, yymsp
3e2b1 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 29 3b  [-1].minor.yy4);
3e2b2 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3e2b3 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  ;..      case 25
3e2b4 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41  3: /* cmd ::= VA
3e2b5 43 55 55 4d 20 2a 2f 0d 0a 20 20 20 20 20 20 63  CUUM */..      c
3e2b6 61 73 65 20 32 35 34 3a 20 2f 2a 20 63 6d 64 20  ase 254: /* cmd 
3e2b7 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 20 2a 2f  ::= VACUUM nm */
3e2b8 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3e2b9 6c 65 6e 6f 3d 3d 32 35 34 29 3b 0d 0a 7b 73 71  leno==254);..{sq
3e2ba 6c 69 74 65 33 56 61 63 75 75 6d 28 70 50 61 72  lite3Vacuum(pPar
3e2bb 73 65 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62  se);}..        b
3e2bc 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e2bd 65 20 32 35 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 255: /* cmd ::
3e2be 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
3e2bf 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 50 72 61   */..{sqlite3Pra
3e2c0 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
3e2c1 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
3e2c2 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3e2c3 79 79 30 2c 30 2c 30 29 3b 7d 0d 0a 20 20 20 20  yy0,0,0);}..    
3e2c4 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3e2c5 20 20 63 61 73 65 20 32 35 36 3a 20 2f 2a 20 63    case 256: /* c
3e2c6 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
3e2c7 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 20 2a   dbnm EQ nmnum *
3e2c8 2f 0d 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  /..{sqlite3Pragm
3e2c9 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
3e2ca 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
3e2cb 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3e2cc 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
3e2cd 6f 72 2e 79 79 30 2c 30 29 3b 7d 0d 0a 20 20 20  or.yy0,0);}..   
3e2ce 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3e2cf 20 20 20 63 61 73 65 20 32 35 37 3a 20 2f 2a 20     case 257: /* 
3e2d0 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
3e2d1 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20  m dbnm LP nmnum 
3e2d2 52 50 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 50  RP */..{sqlite3P
3e2d3 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
3e2d4 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3e2d5 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
3e2d6 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
3e2d7 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d  ].minor.yy0,0);}
3e2d8 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e2d9 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 38  ..      case 258
3e2da 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
3e2db 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6d  GMA nm dbnm EQ m
3e2dc 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0d 0a 7b 73 71  inus_num */..{sq
3e2dd 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
3e2de 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se,&yymsp[-3].mi
3e2df 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
3e2e0 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  2].minor.yy0,&yy
3e2e1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3e2e2 2c 31 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62  ,1);}..        b
3e2e3 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e2e4 65 20 32 35 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 259: /* cmd ::
3e2e5 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
3e2e6 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50   LP minus_num RP
3e2e7 20 2a 2f 0d 0a 7b 73 71 6c 69 74 65 33 50 72 61   */..{sqlite3Pra
3e2e8 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
3e2e9 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-4].minor.yy0,
3e2ea 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
3e2eb 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0,&yymsp[-1].
3e2ec 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0d 0a  minor.yy0,1);}..
3e2ed 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e2ee 20 20 20 20 20 20 63 61 73 65 20 32 37 30 3a 20        case 270: 
3e2ef 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  /* cmd ::= creat
3e2f0 65 6b 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c  ekw trigger_decl
3e2f1 20 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63   BEGIN trigger_c
3e2f2 6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a 2f 0d 0a  md_list END */..
3e2f3 7b 0d 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b 0d  {..  Token all;.
3e2f4 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70  .  all.z = yymsp
3e2f5 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
3e2f6 3b 0d 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28 69 6e  ;..  all.n = (in
3e2f7 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t)(yymsp[0].mino
3e2f8 72 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73 70 5b  r.yy0.z - yymsp[
3e2f9 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29  -3].minor.yy0.z)
3e2fa 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   + yymsp[0].mino
3e2fb 72 2e 79 79 30 2e 6e 3b 0d 0a 20 20 73 71 6c 69  r.yy0.n;..  sqli
3e2fc 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
3e2fd 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3e2fe 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 30 33 2c 20  1].minor.yy203, 
3e2ff 26 61 6c 6c 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20  &all);..}..     
3e300 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3e301 20 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72   case 271: /* tr
3e302 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74  igger_decl ::= t
3e303 65 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f  emp TRIGGER ifno
3e304 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
3e305 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69  trigger_time tri
3e306 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75  gger_event ON fu
3e307 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63  llname foreach_c
3e308 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73  lause when_claus
3e309 65 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74  e */..{..  sqlit
3e30a 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28 70  e3BeginTrigger(p
3e30b 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37  Parse, &yymsp[-7
3e30c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
3e30d 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
3e30e 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  0, yymsp[-5].min
3e30f 6f 72 2e 79 79 34 2c 20 79 79 6d 73 70 5b 2d 34  or.yy4, yymsp[-4
3e310 5d 2e 6d 69 6e 6f 72 2e 79 79 39 30 2e 61 2c 20  ].minor.yy90.a, 
3e311 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3e312 79 79 39 30 2e 62 2c 20 79 79 6d 73 70 5b 2d 32  yy90.b, yymsp[-2
3e313 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 39 2c 20 79  ].minor.yy259, y
3e314 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3e315 33 31 34 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e  314, yymsp[-10].
3e316 6d 69 6e 6f 72 2e 79 79 34 2c 20 79 79 6d 73 70  minor.yy4, yymsp
3e317 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 34 29 3b  [-8].minor.yy4);
3e318 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ..  yygotominor.
3e319 79 79 30 20 3d 20 28 79 79 6d 73 70 5b 2d 36 5d  yy0 = (yymsp[-6]
3e31a 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30 3f  .minor.yy0.n==0?
3e31b 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
3e31c 79 79 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  yy0:yymsp[-6].mi
3e31d 6e 6f 72 2e 79 79 30 29 3b 0d 0a 7d 0d 0a 20 20  nor.yy0);..}..  
3e31e 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3e31f 20 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f 2a      case 272: /*
3e320 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a   trigger_time ::
3e321 3d 20 42 45 46 4f 52 45 20 2a 2f 0d 0a 20 20 20  = BEFORE */..   
3e322 20 20 20 63 61 73 65 20 32 37 35 3a 20 2f 2a 20     case 275: /* 
3e323 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
3e324 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3e325 79 72 75 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0d 0a  yruleno==275);..
3e326 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3e327 34 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 20 7d  4 = TK_BEFORE; }
3e328 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e329 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 33  ..      case 273
3e32a 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
3e32b 65 20 3a 3a 3d 20 41 46 54 45 52 20 2a 2f 0d 0a  e ::= AFTER */..
3e32c 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3e32d 34 20 3d 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d  4 = TK_AFTER;  }
3e32e 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e32f 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 34  ..      case 274
3e330 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
3e331 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46  e ::= INSTEAD OF
3e332 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e   */..{ yygotomin
3e333 6f 72 2e 79 79 34 20 3d 20 54 4b 5f 49 4e 53 54  or.yy4 = TK_INST
3e334 45 41 44 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62  EAD;}..        b
3e335 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e336 65 20 32 37 36 3a 20 2f 2a 20 74 72 69 67 67 65  e 276: /* trigge
3e337 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45  r_event ::= DELE
3e338 54 45 7c 49 4e 53 45 52 54 20 2a 2f 0d 0a 20 20  TE|INSERT */..  
3e339 20 20 20 20 63 61 73 65 20 32 37 37 3a 20 2f 2a      case 277: /*
3e33a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a   trigger_event :
3e33b 3a 3d 20 55 50 44 41 54 45 20 2a 2f 20 79 79 74  := UPDATE */ yyt
3e33c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e33d 3d 3d 32 37 37 29 3b 0d 0a 7b 79 79 67 6f 74 6f  ==277);..{yygoto
3e33e 6d 69 6e 6f 72 2e 79 79 39 30 2e 61 20 3d 20 79  minor.yy90.a = y
3e33f 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79  ymsp[0].major; y
3e340 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 30 2e  ygotominor.yy90.
3e341 62 20 3d 20 30 3b 7d 0d 0a 20 20 20 20 20 20 20  b = 0;}..       
3e342 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3e343 61 73 65 20 32 37 38 3a 20 2f 2a 20 74 72 69 67  ase 278: /* trig
3e344 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50  ger_event ::= UP
3e345 44 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69  DATE OF inscolli
3e346 73 74 20 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69  st */..{yygotomi
3e347 6e 6f 72 2e 79 79 39 30 2e 61 20 3d 20 54 4b 5f  nor.yy90.a = TK_
3e348 55 50 44 41 54 45 3b 20 79 79 67 6f 74 6f 6d 69  UPDATE; yygotomi
3e349 6e 6f 72 2e 79 79 39 30 2e 62 20 3d 20 79 79 6d  nor.yy90.b = yym
3e34a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38  sp[0].minor.yy38
3e34b 34 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  4;}..        bre
3e34c 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3e34d 32 38 31 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61  281: /* when_cla
3e34e 75 73 65 20 3a 3a 3d 20 2a 2f 0d 0a 20 20 20 20  use ::= */..    
3e34f 20 20 63 61 73 65 20 33 30 33 3a 20 2f 2a 20 6b    case 303: /* k
3e350 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  ey_opt ::= */ yy
3e351 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3e352 6f 3d 3d 33 30 33 29 3b 0d 0a 7b 20 79 79 67 6f  o==303);..{ yygo
3e353 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 34 20 3d 20  tominor.yy314 = 
3e354 30 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  0; }..        br
3e355 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e356 20 32 38 32 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c   282: /* when_cl
3e357 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78  ause ::= WHEN ex
3e358 70 72 20 2a 2f 0d 0a 20 20 20 20 20 20 63 61 73  pr */..      cas
3e359 65 20 33 30 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70  e 304: /* key_op
3e35a 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 20 2a  t ::= KEY expr *
3e35b 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3e35c 75 6c 65 6e 6f 3d 3d 33 30 34 29 3b 0d 0a 7b 20  uleno==304);..{ 
3e35d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31  yygotominor.yy31
3e35e 34 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  4 = yymsp[0].min
3e35f 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 3b 20  or.yy118.pExpr; 
3e360 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3e361 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  ;..      case 28
3e362 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  3: /* trigger_cm
3e363 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67  d_list ::= trigg
3e364 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67  er_cmd_list trig
3e365 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0d  ger_cmd SEMI */.
3e366 0a 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 79 79  .{..  assert( yy
3e367 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3e368 32 30 33 21 3d 30 20 29 3b 0d 0a 20 20 79 79 6d  203!=0 );..  yym
3e369 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-2].minor.yy2
3e36a 30 33 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74  03->pLast->pNext
3e36b 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
3e36c 6f 72 2e 79 79 32 30 33 3b 0d 0a 20 20 79 79 6d  or.yy203;..  yym
3e36d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-2].minor.yy2
3e36e 30 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73  03->pLast = yyms
3e36f 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 30  p[-1].minor.yy20
3e370 33 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  3;..  yygotomino
3e371 72 2e 79 79 32 30 33 20 3d 20 79 79 6d 73 70 5b  r.yy203 = yymsp[
3e372 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 30 33 3b  -2].minor.yy203;
3e373 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ..}..        bre
3e374 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3e375 32 38 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  284: /* trigger_
3e376 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
3e377 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f  gger_cmd SEMI */
3e378 0d 0a 7b 20 0d 0a 20 20 61 73 73 65 72 74 28 20  ..{ ..  assert( 
3e379 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3e37a 79 79 32 30 33 21 3d 30 20 29 3b 0d 0a 20 20 79  yy203!=0 );..  y
3e37b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3e37c 79 32 30 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79  y203->pLast = yy
3e37d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3e37e 32 30 33 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69  203;..  yygotomi
3e37f 6e 6f 72 2e 79 79 32 30 33 20 3d 20 79 79 6d 73  nor.yy203 = yyms
3e380 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 30  p[-1].minor.yy20
3e381 33 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62  3;..}..        b
3e382 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e383 65 20 32 38 36 3a 20 2f 2a 20 74 72 6e 6d 20 3a  e 286: /* trnm :
3e384 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0d  := nm DOT nm */.
3e385 0a 7b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .{..  yygotomino
3e386 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d  r.yy0 = yymsp[0]
3e387 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0d 0a 20 20 73  .minor.yy0;..  s
3e388 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
3e389 50 61 72 73 65 2c 20 0d 0a 20 20 20 20 20 20 20  Parse, ..       
3e38a 20 22 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c   "qualified tabl
3e38b 65 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74 20  e names are not 
3e38c 61 6c 6c 6f 77 65 64 20 6f 6e 20 49 4e 53 45 52  allowed on INSER
3e38d 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20 44  T, UPDATE, and D
3e38e 45 4c 45 54 45 20 22 0d 0a 20 20 20 20 20 20 20  ELETE "..       
3e38f 20 22 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74   "statements wit
3e390 68 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0d  hin triggers");.
3e391 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  .}..        brea
3e392 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32  k;..      case 2
3e393 38 38 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a  88: /* tridxby :
3e394 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d  := INDEXED BY nm
3e395 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74 65   */..{..  sqlite
3e396 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
3e397 2c 0d 0a 20 20 20 20 20 20 20 20 22 74 68 65 20  ,..        "the 
3e398 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3e399 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  e is not allowed
3e39a 20 6f 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   on UPDATE or DE
3e39b 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  LETE statements 
3e39c 22 0d 0a 20 20 20 20 20 20 20 20 22 77 69 74 68  "..        "with
3e39d 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0d 0a  in triggers");..
3e39e 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  }..        break
3e39f 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  ;..      case 28
3e3a0 39 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a 3a  9: /* tridxby ::
3e3a1 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f  = NOT INDEXED */
3e3a2 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74 65 33 45 72  ..{..  sqlite3Er
3e3a3 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0d 0a  rorMsg(pParse,..
3e3a4 20 20 20 20 20 20 20 20 22 74 68 65 20 4e 4f 54          "the NOT
3e3a5 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20   INDEXED clause 
3e3a6 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  is not allowed o
3e3a7 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  n UPDATE or DELE
3e3a8 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 22 0d  TE statements ".
3e3a9 0a 20 20 20 20 20 20 20 20 22 77 69 74 68 69 6e  .        "within
3e3aa 20 74 72 69 67 67 65 72 73 22 29 3b 0d 0a 7d 0d   triggers");..}.
3e3ab 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3e3ac 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30 3a  .      case 290:
3e3ad 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
3e3ae 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
3e3af 66 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 53  f trnm tridxby S
3e3b0 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
3e3b1 5f 6f 70 74 20 2a 2f 0d 0a 7b 20 79 79 67 6f 74  _opt */..{ yygot
3e3b2 6f 6d 69 6e 6f 72 2e 79 79 32 30 33 20 3d 20 73  ominor.yy203 = s
3e3b3 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64  qlite3TriggerUpd
3e3b4 61 74 65 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ateStep(pParse->
3e3b5 64 62 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  db, &yymsp[-4].m
3e3b6 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
3e3b7 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c  -1].minor.yy322,
3e3b8 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3e3b9 79 79 33 31 34 2c 20 79 79 6d 73 70 5b 2d 35 5d  yy314, yymsp[-5]
3e3ba 2e 6d 69 6e 6f 72 2e 79 79 32 31 30 29 3b 20 7d  .minor.yy210); }
3e3bb 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e3bc 0d 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 31  ..      case 291
3e3bd 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
3e3be 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
3e3bf 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c  INTO trnm inscol
3e3c0 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
3e3c1 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20 2a  LP itemlist RP *
3e3c2 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /..{yygotominor.
3e3c3 79 79 32 30 33 20 3d 20 73 71 6c 69 74 65 33 54  yy203 = sqlite3T
3e3c4 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
3e3c5 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
3e3c6 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
3e3c7 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  0, yymsp[-4].min
3e3c8 6f 72 2e 79 79 33 38 34 2c 20 79 79 6d 73 70 5b  or.yy384, yymsp[
3e3c9 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 32 2c  -1].minor.yy322,
3e3ca 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69   0, yymsp[-7].mi
3e3cb 6e 6f 72 2e 79 79 32 31 30 29 3b 7d 0d 0a 20 20  nor.yy210);}..  
3e3cc 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
3e3cd 20 20 20 20 63 61 73 65 20 32 39 32 3a 20 2f 2a      case 292: /*
3e3ce 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
3e3cf 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
3e3d0 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74   trnm inscollist
3e3d1 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f 0d 0a  _opt select */..
3e3d2 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32  {yygotominor.yy2
3e3d3 30 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  03 = sqlite3Trig
3e3d4 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 70 50  gerInsertStep(pP
3e3d5 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70  arse->db, &yymsp
3e3d6 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
3e3d7 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3e3d8 79 79 33 38 34 2c 20 30 2c 20 79 79 6d 73 70 5b  yy384, 0, yymsp[
3e3d9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 38 37 2c 20  0].minor.yy387, 
3e3da 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3e3db 79 79 32 31 30 29 3b 7d 0d 0a 20 20 20 20 20 20  yy210);}..      
3e3dc 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3e3dd 63 61 73 65 20 32 39 33 3a 20 2f 2a 20 74 72 69  case 293: /* tri
3e3de 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c  gger_cmd ::= DEL
3e3df 45 54 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72  ETE FROM trnm tr
3e3e0 69 64 78 62 79 20 77 68 65 72 65 5f 6f 70 74 20  idxby where_opt 
3e3e1 2a 2f 0d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  */..{yygotominor
3e3e2 2e 79 79 32 30 33 20 3d 20 73 71 6c 69 74 65 33  .yy203 = sqlite3
3e3e3 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
3e3e4 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79  p(pParse->db, &y
3e3e5 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3e3e6 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0, yymsp[0].min
3e3e7 6f 72 2e 79 79 33 31 34 29 3b 7d 0d 0a 20 20 20  or.yy314);}..   
3e3e8 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3e3e9 20 20 20 63 61 73 65 20 32 39 34 3a 20 2f 2a 20     case 294: /* 
3e3ea 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
3e3eb 73 65 6c 65 63 74 20 2a 2f 0d 0a 7b 79 79 67 6f  select */..{yygo
3e3ec 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 33 20 3d 20  tominor.yy203 = 
3e3ed 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
3e3ee 6c 65 63 74 53 74 65 70 28 70 50 61 72 73 65 2d  lectStep(pParse-
3e3ef 3e 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  >db, yymsp[0].mi
3e3f0 6e 6f 72 2e 79 79 33 38 37 29 3b 20 7d 0d 0a 20  nor.yy387); }.. 
3e3f1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3e3f2 20 20 20 20 20 63 61 73 65 20 32 39 35 3a 20 2f       case 295: /
3e3f3 2a 20 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45  * expr ::= RAISE
3e3f4 20 4c 50 20 49 47 4e 4f 52 45 20 52 50 20 2a 2f   LP IGNORE RP */
3e3f5 0d 0a 7b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  ..{..  yygotomin
3e3f6 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72 20 3d  or.yy118.pExpr =
3e3f7 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
3e3f8 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20  arse, TK_RAISE, 
3e3f9 30 2c 20 30 2c 20 30 29 3b 20 0d 0a 20 20 69 66  0, 0, 0); ..  if
3e3fa 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
3e3fb 31 31 38 2e 70 45 78 70 72 20 29 7b 0d 0a 20 20  118.pExpr ){..  
3e3fc 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3e3fd 31 31 38 2e 70 45 78 70 72 2d 3e 61 66 66 69 6e  118.pExpr->affin
3e3fe 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  ity = OE_Ignore;
3e3ff 0d 0a 20 20 7d 0d 0a 20 20 79 79 67 6f 74 6f 6d  ..  }..  yygotom
3e400 69 6e 6f 72 2e 79 79 31 31 38 2e 7a 53 74 61 72  inor.yy118.zStar
3e401 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  t = yymsp[-3].mi
3e402 6e 6f 72 2e 79 79 30 2e 7a 3b 0d 0a 20 20 79 79  nor.yy0.z;..  yy
3e403 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e  gotominor.yy118.
3e404 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
3e405 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
3e406 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3e407 6e 5d 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20  n];..}..        
3e408 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
3e409 73 65 20 32 39 36 3a 20 2f 2a 20 65 78 70 72 20  se 296: /* expr 
3e40a 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69  ::= RAISE LP rai
3e40b 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20  setype COMMA nm 
3e40c 52 50 20 2a 2f 0d 0a 7b 0d 0a 20 20 79 79 67 6f  RP */..{..  yygo
3e40d 74 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  tominor.yy118.pE
3e40e 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3e40f 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41  pr(pParse, TK_RA
3e410 49 53 45 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  ISE, 0, 0, &yyms
3e411 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
3e412 3b 20 0d 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ; ..  if( yygoto
3e413 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70  minor.yy118.pExp
3e414 72 20 29 20 7b 0d 0a 20 20 20 20 79 79 67 6f 74  r ) {..    yygot
3e415 6f 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78  ominor.yy118.pEx
3e416 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 28  pr->affinity = (
3e417 63 68 61 72 29 79 79 6d 73 70 5b 2d 33 5d 2e 6d  char)yymsp[-3].m
3e418 69 6e 6f 72 2e 79 79 34 3b 0d 0a 20 20 7d 0d 0a  inor.yy4;..  }..
3e419 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3e41a 31 31 38 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  118.zStart = yym
3e41b 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
3e41c 2e 7a 3b 0d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .z;..  yygotomin
3e41d 6f 72 2e 79 79 31 31 38 2e 7a 45 6e 64 20 3d 20  or.yy118.zEnd = 
3e41e 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3e41f 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
3e420 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0d 0a 7d 0d  inor.yy0.n];..}.
3e421 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3e422 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37 3a  .      case 297:
3e423 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a   /* raisetype ::
3e424 3d 20 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0d 0a 7b  = ROLLBACK */..{
3e425 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 20  yygotominor.yy4 
3e426 3d 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3b 7d 0d  = OE_Rollback;}.
3e427 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3e428 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 39 3a  .      case 299:
3e429 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a   /* raisetype ::
3e42a 3d 20 46 41 49 4c 20 2a 2f 0d 0a 7b 79 79 67 6f  = FAIL */..{yygo
3e42b 74 6f 6d 69 6e 6f 72 2e 79 79 34 20 3d 20 4f 45  tominor.yy4 = OE
3e42c 5f 46 61 69 6c 3b 7d 0d 0a 20 20 20 20 20 20 20  _Fail;}..       
3e42d 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63   break;..      c
3e42e 61 73 65 20 33 30 30 3a 20 2f 2a 20 63 6d 64 20  ase 300: /* cmd 
3e42f 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45 52  ::= DROP TRIGGER
3e430 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
3e431 6d 65 20 2a 2f 0d 0a 7b 0d 0a 20 20 73 71 6c 69  me */..{..  sqli
3e432 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 70  te3DropTrigger(p
3e433 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d  Parse,yymsp[0].m
3e434 69 6e 6f 72 2e 79 79 32 35 39 2c 79 79 6d 73 70  inor.yy259,yymsp
3e435 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 29 3b  [-1].minor.yy4);
3e436 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ..}..        bre
3e437 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
3e438 33 30 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  301: /* cmd ::= 
3e439 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f  ATTACH database_
3e43a 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65  kw_opt expr AS e
3e43b 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0d 0a  xpr key_opt */..
3e43c 7b 0d 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61  {..  sqlite3Atta
3e43d 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ch(pParse, yymsp
3e43e 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38  [-3].minor.yy118
3e43f 2e 70 45 78 70 72 2c 20 79 79 6d 73 70 5b 2d 31  .pExpr, yymsp[-1
3e440 5d 2e 6d 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45  ].minor.yy118.pE
3e441 78 70 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  xpr, yymsp[0].mi
3e442 6e 6f 72 2e 79 79 33 31 34 29 3b 0d 0a 7d 0d 0a  nor.yy314);..}..
3e443 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e444 20 20 20 20 20 20 63 61 73 65 20 33 30 32 3a 20        case 302: 
3e445 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43  /* cmd ::= DETAC
3e446 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70  H database_kw_op
3e447 74 20 65 78 70 72 20 2a 2f 0d 0a 7b 0d 0a 20 20  t expr */..{..  
3e448 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 70 50  sqlite3Detach(pP
3e449 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
3e44a 69 6e 6f 72 2e 79 79 31 31 38 2e 70 45 78 70 72  inor.yy118.pExpr
3e44b 29 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62  );..}..        b
3e44c 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
3e44d 65 20 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 307: /* cmd ::
3e44e 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0d 0a 7b 73  = REINDEX */..{s
3e44f 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50  qlite3Reindex(pP
3e450 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0d 0a 20  arse, 0, 0);}.. 
3e451 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3e452 20 20 20 20 20 63 61 73 65 20 33 30 38 3a 20 2f       case 308: /
3e453 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45  * cmd ::= REINDE
3e454 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0d 0a 7b 73  X nm dbnm */..{s
3e455 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50  qlite3Reindex(pP
3e456 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  arse, &yymsp[-1]
3e457 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
3e458 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3e459 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;}..        brea
3e45a 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65 20 33  k;..      case 3
3e45b 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  09: /* cmd ::= A
3e45c 4e 41 4c 59 5a 45 20 2a 2f 0d 0a 7b 73 71 6c 69  NALYZE */..{sqli
3e45d 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73  te3Analyze(pPars
3e45e 65 2c 20 30 2c 20 30 29 3b 7d 0d 0a 20 20 20 20  e, 0, 0);}..    
3e45f 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3e460 20 20 63 61 73 65 20 33 31 30 3a 20 2f 2a 20 63    case 310: /* c
3e461 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e  md ::= ANALYZE n
3e462 6d 20 64 62 6e 6d 20 2a 2f 0d 0a 7b 73 71 6c 69  m dbnm */..{sqli
3e463 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73  te3Analyze(pPars
3e464 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  e, &yymsp[-1].mi
3e465 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
3e466 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d  0].minor.yy0);}.
3e467 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3e468 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 31 3a  .      case 311:
3e469 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45   /* cmd ::= ALTE
3e46a 52 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65  R TABLE fullname
3e46b 20 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 20 2a 2f   RENAME TO nm */
3e46c 0d 0a 7b 0d 0a 20 20 73 71 6c 69 74 65 33 41 6c  ..{..  sqlite3Al
3e46d 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 70  terRenameTable(p
3e46e 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e  Parse,yymsp[-3].
3e46f 6d 69 6e 6f 72 2e 79 79 32 35 39 2c 26 79 79 6d  minor.yy259,&yym
3e470 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3e471 3b 0d 0a 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  ;..}..        br
3e472 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e473 20 33 31 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   312: /* cmd ::=
3e474 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64   ALTER TABLE add
3e475 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
3e476 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70   ADD kwcolumn_op
3e477 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 7b 0d 0a  t column */..{..
3e478 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69    sqlite3AlterFi
3e479 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50  nishAddColumn(pP
3e47a 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  arse, &yymsp[0].
3e47b 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 7d 0d 0a  minor.yy0);..}..
3e47c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e47d 20 20 20 20 20 20 63 61 73 65 20 33 31 33 3a 20        case 313: 
3e47e 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75  /* add_column_fu
3e47f 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e  llname ::= fulln
3e480 61 6d 65 20 2a 2f 0d 0a 7b 0d 0a 20 20 70 50 61  ame */..{..  pPa
3e481 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69  rse->db->lookasi
3e482 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b  de.bEnabled = 0;
3e483 0d 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  ..  sqlite3Alter
3e484 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 70  BeginAddColumn(p
3e485 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
3e486 6d 69 6e 6f 72 2e 79 79 32 35 39 29 3b 0d 0a 7d  minor.yy259);..}
3e487 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e488 0d 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 36  ..      case 316
3e489 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65  : /* cmd ::= cre
3e48a 61 74 65 5f 76 74 61 62 20 2a 2f 0d 0a 7b 73 71  ate_vtab */..{sq
3e48b 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
3e48c 61 72 73 65 28 70 50 61 72 73 65 2c 30 29 3b 7d  arse(pParse,0);}
3e48d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e48e 0d 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 37  ..      case 317
3e48f 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65  : /* cmd ::= cre
3e490 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62  ate_vtab LP vtab
3e491 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0d 0a 7b  arglist RP */..{
3e492 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
3e493 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 26 79  hParse(pParse,&y
3e494 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3e495 30 29 3b 7d 0d 0a 20 20 20 20 20 20 20 20 62 72  0);}..        br
3e496 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
3e497 20 33 31 38 3a 20 2f 2a 20 63 72 65 61 74 65 5f   318: /* create_
3e498 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b  vtab ::= createk
3e499 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  w VIRTUAL TABLE 
3e49a 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d  nm dbnm USING nm
3e49b 20 2a 2f 0d 0a 7b 0d 0a 20 20 20 20 73 71 6c 69   */..{..    sqli
3e49c 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73  te3VtabBeginPars
3e49d 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  e(pParse, &yymsp
3e49e 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-3].minor.yy0, 
3e49f 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
3e4a0 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  .yy0, &yymsp[0].
3e4a1 6d 69 6e 6f 72 2e 79 79 30 29 3b 0d 0a 7d 0d 0a  minor.yy0);..}..
3e4a2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e4a3 20 20 20 20 20 20 63 61 73 65 20 33 32 31 3a 20        case 321: 
3e4a4 2f 2a 20 76 74 61 62 61 72 67 20 3a 3a 3d 20 2a  /* vtabarg ::= *
3e4a5 2f 0d 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41  /..{sqlite3VtabA
3e4a6 72 67 49 6e 69 74 28 70 50 61 72 73 65 29 3b 7d  rgInit(pParse);}
3e4a7 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3e4a8 0d 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 33  ..      case 323
3e4a9 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65  : /* vtabargtoke
3e4aa 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f 0d 0a 20 20  n ::= ANY */..  
3e4ab 20 20 20 20 63 61 73 65 20 33 32 34 3a 20 2f 2a      case 324: /*
3e4ac 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a   vtabargtoken ::
3e4ad 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 20  = lp anylist RP 
3e4ae 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3e4af 72 75 6c 65 6e 6f 3d 3d 33 32 34 29 3b 0d 0a 20  ruleno==324);.. 
3e4b0 20 20 20 20 20 63 61 73 65 20 33 32 35 3a 20 2f       case 325: /
3e4b1 2a 20 6c 70 20 3a 3a 3d 20 4c 50 20 2a 2f 20 79  * lp ::= LP */ y
3e4b2 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3e4b3 6e 6f 3d 3d 33 32 35 29 3b 0d 0a 7b 73 71 6c 69  no==325);..{sqli
3e4b4 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
3e4b5 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
3e4b6 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0d 0a  ].minor.yy0);}..
3e4b7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3e4b8 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0d 0a        default:..
3e4b9 20 20 20 20 20 20 2f 2a 20 28 30 29 20 69 6e 70        /* (0) inp
3e4ba 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 2a  ut ::= cmdlist *
3e4bb 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3e4bc 75 6c 65 6e 6f 3d 3d 30 29 3b 0d 0a 20 20 20 20  uleno==0);..    
3e4bd 20 20 2f 2a 20 28 31 29 20 63 6d 64 6c 69 73 74    /* (1) cmdlist
3e4be 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d   ::= cmdlist ecm
3e4bf 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  d */ yytestcase(
3e4c0 79 79 72 75 6c 65 6e 6f 3d 3d 31 29 3b 0d 0a 20  yyruleno==1);.. 
3e4c1 20 20 20 20 20 2f 2a 20 28 32 29 20 63 6d 64 6c       /* (2) cmdl
3e4c2 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 20  ist ::= ecmd */ 
3e4c3 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3e4c4 65 6e 6f 3d 3d 32 29 3b 0d 0a 20 20 20 20 20 20  eno==2);..      
3e4c5 2f 2a 20 28 33 29 20 65 63 6d 64 20 3a 3a 3d 20  /* (3) ecmd ::= 
3e4c6 53 45 4d 49 20 2a 2f 20 79 79 74 65 73 74 63 61  SEMI */ yytestca
3e4c7 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 29 3b  se(yyruleno==3);
3e4c8 0d 0a 20 20 20 20 20 20 2f 2a 20 28 34 29 20 65  ..      /* (4) e
3e4c9 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20  cmd ::= explain 
3e4ca 63 6d 64 78 20 53 45 4d 49 20 2a 2f 20 79 79 74  cmdx SEMI */ yyt
3e4cb 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e4cc 3d 3d 34 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20  ==4);..      /* 
3e4cd 28 31 30 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a  (10) trans_opt :
3e4ce 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
3e4cf 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 29 3b 0d  (yyruleno==10);.
3e4d0 0a 20 20 20 20 20 20 2f 2a 20 28 31 31 29 20 74  .      /* (11) t
3e4d1 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41  rans_opt ::= TRA
3e4d2 4e 53 41 43 54 49 4f 4e 20 2a 2f 20 79 79 74 65  NSACTION */ yyte
3e4d3 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3e4d4 3d 31 31 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20  =11);..      /* 
3e4d5 28 31 32 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a  (12) trans_opt :
3e4d6 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e  := TRANSACTION n
3e4d7 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  m */ yytestcase(
3e4d8 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 29 3b 0d 0a  yyruleno==12);..
3e4d9 20 20 20 20 20 20 2f 2a 20 28 32 30 29 20 73 61        /* (20) sa
3e4da 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20  vepoint_opt ::= 
3e4db 53 41 56 45 50 4f 49 4e 54 20 2a 2f 20 79 79 74  SAVEPOINT */ yyt
3e4dc 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e4dd 3d 3d 32 30 29 3b 0d 0a 20 20 20 20 20 20 2f 2a  ==20);..      /*
3e4de 20 28 32 31 29 20 73 61 76 65 70 6f 69 6e 74 5f   (21) savepoint_
3e4df 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
3e4e0 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3e4e1 32 31 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28  21);..      /* (
3e4e2 32 35 29 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61  25) cmd ::= crea
3e4e3 74 65 5f 74 61 62 6c 65 20 63 72 65 61 74 65 5f  te_table create_
3e4e4 74 61 62 6c 65 5f 61 72 67 73 20 2a 2f 20 79 79  table_args */ yy
3e4e5 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3e4e6 6f 3d 3d 32 35 29 3b 0d 0a 20 20 20 20 20 20 2f  o==25);..      /
3e4e7 2a 20 28 33 34 29 20 63 6f 6c 75 6d 6e 6c 69 73  * (34) columnlis
3e4e8 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74  t ::= columnlist
3e4e9 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f   COMMA column */
3e4ea 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3e4eb 6c 65 6e 6f 3d 3d 33 34 29 3b 0d 0a 20 20 20 20  leno==34);..    
3e4ec 20 20 2f 2a 20 28 33 35 29 20 63 6f 6c 75 6d 6e    /* (35) column
3e4ed 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 20  list ::= column 
3e4ee 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3e4ef 72 75 6c 65 6e 6f 3d 3d 33 35 29 3b 0d 0a 20 20  ruleno==35);..  
3e4f0 20 20 20 20 2f 2a 20 28 34 34 29 20 74 79 70 65      /* (44) type
3e4f1 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3e4f2 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 34 29  se(yyruleno==44)
3e4f3 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 35 31 29  ;..      /* (51)
3e4f4 20 73 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73   signed ::= plus
3e4f5 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  _num */ yytestca
3e4f6 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 31 29  se(yyruleno==51)
3e4f7 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 35 32 29  ;..      /* (52)
3e4f8 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75   signed ::= minu
3e4f9 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73 74 63  s_num */ yytestc
3e4fa 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 32  ase(yyruleno==52
3e4fb 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 35 33  );..      /* (53
3e4fc 29 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63  ) carglist ::= c
3e4fd 61 72 67 6c 69 73 74 20 63 61 72 67 20 2a 2f 20  arglist carg */ 
3e4fe 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3e4ff 65 6e 6f 3d 3d 35 33 29 3b 0d 0a 20 20 20 20 20  eno==53);..     
3e500 20 2f 2a 20 28 35 34 29 20 63 61 72 67 6c 69 73   /* (54) carglis
3e501 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
3e502 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 34  ase(yyruleno==54
3e503 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 35 35  );..      /* (55
3e504 29 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54  ) carg ::= CONST
3e505 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a  RAINT nm ccons *
3e506 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3e507 75 6c 65 6e 6f 3d 3d 35 35 29 3b 0d 0a 20 20 20  uleno==55);..   
3e508 20 20 20 2f 2a 20 28 35 36 29 20 63 61 72 67 20     /* (56) carg 
3e509 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74  ::= ccons */ yyt
3e50a 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e50b 3d 3d 35 36 29 3b 0d 0a 20 20 20 20 20 20 2f 2a  ==56);..      /*
3e50c 20 28 36 32 29 20 63 63 6f 6e 73 20 3a 3a 3d 20   (62) ccons ::= 
3e50d 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 20 79  NULL onconf */ y
3e50e 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3e50f 6e 6f 3d 3d 36 32 29 3b 0d 0a 20 20 20 20 20 20  no==62);..      
3e510 2f 2a 20 28 39 30 29 20 63 6f 6e 73 6c 69 73 74  /* (90) conslist
3e511 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f   ::= conslist CO
3e512 4d 4d 41 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74  MMA tcons */ yyt
3e513 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e514 3d 3d 39 30 29 3b 0d 0a 20 20 20 20 20 20 2f 2a  ==90);..      /*
3e515 20 28 39 31 29 20 63 6f 6e 73 6c 69 73 74 20 3a   (91) conslist :
3e516 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e  := conslist tcon
3e517 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  s */ yytestcase(
3e518 79 79 72 75 6c 65 6e 6f 3d 3d 39 31 29 3b 0d 0a  yyruleno==91);..
3e519 20 20 20 20 20 20 2f 2a 20 28 39 32 29 20 63 6f        /* (92) co
3e51a 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73  nslist ::= tcons
3e51b 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3e51c 79 72 75 6c 65 6e 6f 3d 3d 39 32 29 3b 0d 0a 20  yruleno==92);.. 
3e51d 20 20 20 20 20 2f 2a 20 28 39 33 29 20 74 63 6f       /* (93) tco
3e51e 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
3e51f 54 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  T nm */ yytestca
3e520 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 33 29  se(yyruleno==93)
3e521 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 32 36 38  ;..      /* (268
3e522 29 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50  ) plus_opt ::= P
3e523 4c 55 53 20 2a 2f 20 79 79 74 65 73 74 63 61 73  LUS */ yytestcas
3e524 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 38 29  e(yyruleno==268)
3e525 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 32 36 39  ;..      /* (269
3e526 29 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 2a  ) plus_opt ::= *
3e527 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3e528 75 6c 65 6e 6f 3d 3d 32 36 39 29 3b 0d 0a 20 20  uleno==269);..  
3e529 20 20 20 20 2f 2a 20 28 32 37 39 29 20 66 6f 72      /* (279) for
3e52a 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20  each_clause ::= 
3e52b 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3e52c 72 75 6c 65 6e 6f 3d 3d 32 37 39 29 3b 0d 0a 20  ruleno==279);.. 
3e52d 20 20 20 20 20 2f 2a 20 28 32 38 30 29 20 66 6f       /* (280) fo
3e52e 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d  reach_clause ::=
3e52f 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 2a 2f   FOR EACH ROW */
3e530 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3e531 6c 65 6e 6f 3d 3d 32 38 30 29 3b 0d 0a 20 20 20  leno==280);..   
3e532 20 20 20 2f 2a 20 28 32 38 37 29 20 74 72 69 64     /* (287) trid
3e533 78 62 79 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  xby ::= */ yytes
3e534 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3e535 32 38 37 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20  287);..      /* 
3e536 28 33 30 35 29 20 64 61 74 61 62 61 73 65 5f 6b  (305) database_k
3e537 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41  w_opt ::= DATABA
3e538 53 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  SE */ yytestcase
3e539 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 35 29 3b  (yyruleno==305);
3e53a 0d 0a 20 20 20 20 20 20 2f 2a 20 28 33 30 36 29  ..      /* (306)
3e53b 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74   database_kw_opt
3e53c 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3e53d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 36  se(yyruleno==306
3e53e 29 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 33 31  );..      /* (31
3e53f 34 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  4) kwcolumn_opt 
3e540 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
3e541 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 34 29  e(yyruleno==314)
3e542 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 28 33 31 35  ;..      /* (315
3e543 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a  ) kwcolumn_opt :
3e544 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 20 79  := COLUMNKW */ y
3e545 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3e546 6e 6f 3d 3d 33 31 35 29 3b 0d 0a 20 20 20 20 20  no==315);..     
3e547 20 2f 2a 20 28 33 31 39 29 20 76 74 61 62 61 72   /* (319) vtabar
3e548 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72  glist ::= vtabar
3e549 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  g */ yytestcase(
3e54a 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 39 29 3b 0d  yyruleno==319);.
3e54b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 30 29 20  .      /* (320) 
3e54c 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
3e54d 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d  vtabarglist COMM
3e54e 41 20 76 74 61 62 61 72 67 20 2a 2f 20 79 79 74  A vtabarg */ yyt
3e54f 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3e550 3d 3d 33 32 30 29 3b 0d 0a 20 20 20 20 20 20 2f  ==320);..      /
3e551 2a 20 28 33 32 32 29 20 76 74 61 62 61 72 67 20  * (322) vtabarg 
3e552 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62  ::= vtabarg vtab
3e553 61 72 67 74 6f 6b 65 6e 20 2a 2f 20 79 79 74 65  argtoken */ yyte
3e554 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3e555 3d 33 32 32 29 3b 0d 0a 20 20 20 20 20 20 2f 2a  =322);..      /*
3e556 20 28 33 32 36 29 20 61 6e 79 6c 69 73 74 20 3a   (326) anylist :
3e557 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
3e558 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 36 29 3b  (yyruleno==326);
3e559 0d 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 37 29  ..      /* (327)
3e55a 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79   anylist ::= any
3e55b 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74 20  list LP anylist 
3e55c 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  RP */ yytestcase
3e55d 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 37 29 3b  (yyruleno==327);
3e55e 0d 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 38 29  ..      /* (328)
3e55f 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79   anylist ::= any
3e560 6c 69 73 74 20 41 4e 59 20 2a 2f 20 79 79 74 65  list ANY */ yyte
3e561 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3e562 3d 33 32 38 29 3b 0d 0a 20 20 20 20 20 20 20 20  =328);..        
3e563 62 72 65 61 6b 3b 0d 0a 20 20 7d 3b 0d 0a 20 20  break;..  };..  
3e564 79 79 67 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49  yygoto = yyRuleI
3e565 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68  nfo[yyruleno].lh
3e566 73 3b 0d 0a 20 20 79 79 73 69 7a 65 20 3d 20 79  s;..  yysize = y
3e567 79 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65  yRuleInfo[yyrule
3e568 6e 6f 5d 2e 6e 72 68 73 3b 0d 0a 20 20 79 79 70  no].nrhs;..  yyp
3e569 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 2d 3d  Parser->yyidx -=
3e56a 20 79 79 73 69 7a 65 3b 0d 0a 20 20 79 79 61 63   yysize;..  yyac
3e56b 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64 75  t = yy_find_redu
3e56c 63 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73 70 5b  ce_action(yymsp[
3e56d 2d 79 79 73 69 7a 65 5d 2e 73 74 61 74 65 6e 6f  -yysize].stateno
3e56e 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 67  ,(YYCODETYPE)yyg
3e56f 6f 74 6f 29 3b 0d 0a 20 20 69 66 28 20 79 79 61  oto);..  if( yya
3e570 63 74 20 3c 20 59 59 4e 53 54 41 54 45 20 29 7b  ct < YYNSTATE ){
3e571 0d 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 0d  ..#ifdef NDEBUG.
3e572 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72  .    /* If we ar
3e573 65 20 6e 6f 74 20 64 65 62 75 67 67 69 6e 67 20  e not debugging 
3e574 61 6e 64 20 74 68 65 20 72 65 64 75 63 65 20 61  and the reduce a
3e575 63 74 69 6f 6e 20 70 6f 70 70 65 64 20 61 74 20  ction popped at 
3e576 6c 65 61 73 74 0d 0a 20 20 20 20 2a 2a 20 6f 6e  least..    ** on
3e577 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74 68  e element off th
3e578 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 77 65  e stack, then we
3e579 20 63 61 6e 20 70 75 73 68 20 74 68 65 20 6e 65   can push the ne
3e57a 77 20 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0d 0a  w element back..
3e57b 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20      ** onto the 
3e57c 73 74 61 63 6b 20 68 65 72 65 2c 20 61 6e 64 20  stack here, and 
3e57d 73 6b 69 70 20 74 68 65 20 73 74 61 63 6b 20 6f  skip the stack o
3e57e 76 65 72 66 6c 6f 77 20 74 65 73 74 20 69 6e 20  verflow test in 
3e57f 79 79 5f 73 68 69 66 74 28 29 2e 0d 0a 20 20 20  yy_shift()...   
3e580 20 2a 2a 20 54 68 61 74 20 67 69 76 65 73 20 61   ** That gives a
3e581 20 73 69 67 6e 69 66 69 63 61 6e 74 20 73 70 65   significant spe
3e582 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 20  ed improvement. 
3e583 2a 2f 0d 0a 20 20 20 20 69 66 28 20 79 79 73 69  */..    if( yysi
3e584 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20 79 79 70  ze ){..      yyp
3e585 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b  Parser->yyidx++;
3e586 0d 0a 20 20 20 20 20 20 79 79 6d 73 70 20 2d 3d  ..      yymsp -=
3e587 20 79 79 73 69 7a 65 2d 31 3b 0d 0a 20 20 20 20   yysize-1;..    
3e588 20 20 79 79 6d 73 70 2d 3e 73 74 61 74 65 6e 6f    yymsp->stateno
3e589 20 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45   = (YYACTIONTYPE
3e58a 29 79 79 61 63 74 3b 0d 0a 20 20 20 20 20 20 79  )yyact;..      y
3e58b 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59  ymsp->major = (Y
3e58c 59 43 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f  YCODETYPE)yygoto
3e58d 3b 0d 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e  ;..      yymsp->
3e58e 6d 69 6e 6f 72 20 3d 20 79 79 67 6f 74 6f 6d 69  minor = yygotomi
3e58f 6e 6f 72 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 0d  nor;..    }else.
3e590 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 7b 0d 0a  .#endif..    {..
3e591 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79        yy_shift(y
3e592 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79  ypParser,yyact,y
3e593 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d 69 6e  ygoto,&yygotomin
3e594 6f 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  or);..    }..  }
3e595 65 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73 65 72  else{..    asser
3e596 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53  t( yyact == YYNS
3e597 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b  TATE + YYNRULE +
3e598 20 31 20 29 3b 0d 0a 20 20 20 20 79 79 5f 61 63   1 );..    yy_ac
3e599 63 65 70 74 28 79 79 70 50 61 72 73 65 72 29 3b  cept(yypParser);
3e59a 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
3e59b 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
3e59c 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77   code executes w
3e59d 68 65 6e 20 74 68 65 20 70 61 72 73 65 20 66 61  hen the parse fa
3e59e 69 6c 73 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  ils..*/..#ifndef
3e59f 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45   YYNOERRORRECOVE
3e5a0 52 59 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  RY..static void 
3e5a1 79 79 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28  yy_parse_failed(
3e5a2 0d 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  ..  yyParser *yy
3e5a3 70 50 61 72 73 65 72 20 20 20 20 20 20 20 20 20  pParser         
3e5a4 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
3e5a5 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65  */..){..  sqlite
3e5a6 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
3e5a7 3b 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ;..#ifndef NDEBU
3e5a8 47 0d 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  G..  if( yyTrace
3e5a9 46 49 4c 45 20 29 7b 0d 0a 20 20 20 20 66 70 72  FILE ){..    fpr
3e5aa 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
3e5ab 2c 22 25 73 46 61 69 6c 21 5c 6e 22 2c 79 79 54  ,"%sFail!\n",yyT
3e5ac 72 61 63 65 50 72 6f 6d 70 74 29 3b 0d 0a 20 20  racePrompt);..  
3e5ad 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 77 68 69  }..#endif..  whi
3e5ae 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  le( yypParser->y
3e5af 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
3e5b0 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79  _parser_stack(yy
3e5b1 70 50 61 72 73 65 72 29 3b 0d 0a 20 20 2f 2a 20  pParser);..  /* 
3e5b2 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73  Here code is ins
3e5b3 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c  erted which will
3e5b4 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65   be executed whe
3e5b5 6e 65 76 65 72 20 74 68 65 0d 0a 20 20 2a 2a 20  never the..  ** 
3e5b6 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f 0d  parser fails */.
3e5b7 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
3e5b8 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
3e5b9 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
3e5ba 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
3e5bb 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
3e5bc 61 62 6c 65 20 2a 2f 0d 0a 7d 0d 0a 23 65 6e 64  able */..}..#end
3e5bd 69 66 20 2f 2a 20 59 59 4e 4f 45 52 52 4f 52 52  if /* YYNOERRORR
3e5be 45 43 4f 56 45 52 59 20 2a 2f 0d 0a 0d 0a 2f 2a  ECOVERY */..../*
3e5bf 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
3e5c0 6e 67 20 63 6f 64 65 20 65 78 65 63 75 74 65 73  ng code executes
3e5c1 20 77 68 65 6e 20 61 20 73 79 6e 74 61 78 20 65   when a syntax e
3e5c2 72 72 6f 72 20 66 69 72 73 74 20 6f 63 63 75 72  rror first occur
3e5c3 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  s...*/..static v
3e5c4 6f 69 64 20 79 79 5f 73 79 6e 74 61 78 5f 65 72  oid yy_syntax_er
3e5c5 72 6f 72 28 0d 0a 20 20 79 79 50 61 72 73 65 72  ror(..  yyParser
3e5c6 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20   *yypParser,    
3e5c7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
3e5c8 72 73 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 79  rser */..  int y
3e5c9 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20  ymajor,         
3e5ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3e5cb 20 6d 61 6a 6f 72 20 74 79 70 65 20 6f 66 20 74   major type of t
3e5cc 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a  he error token *
3e5cd 2f 0d 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  /..  YYMINORTYPE
3e5ce 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20 20 20   yyminor        
3e5cf 20 20 20 20 2f 2a 20 54 68 65 20 6d 69 6e 6f 72      /* The minor
3e5d0 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72   type of the err
3e5d1 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 29 7b 0d  or token */..){.
3e5d2 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
3e5d3 41 52 47 5f 46 45 54 43 48 3b 0d 0a 23 64 65 66  ARG_FETCH;..#def
3e5d4 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e  ine TOKEN (yymin
3e5d5 6f 72 2e 79 79 30 29 0d 0a 0d 0a 20 20 55 4e 55  or.yy0)....  UNU
3e5d6 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 79  SED_PARAMETER(yy
3e5d7 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65  major);  /* Sile
3e5d8 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  nce some compile
3e5d9 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0d 0a 20  r warnings */.. 
3e5da 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a   assert( TOKEN.z
3e5db 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74  [0] );  /* The t
3e5dc 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20  okenizer always 
3e5dd 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e  gives us a token
3e5de 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 45 72   */..  sqlite3Er
3e5df 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
3e5e0 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e  near \"%T\": syn
3e5e1 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b  tax error", &TOK
3e5e2 45 4e 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 50  EN);..  sqlite3P
3e5e3 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20  arserARG_STORE; 
3e5e4 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e  /* Suppress warn
3e5e5 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64  ing about unused
3e5e6 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74   %extra_argument
3e5e7 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 7d 0d   variable */..}.
3e5e8 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f  .../*..** The fo
3e5e9 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65 63 75  llowing is execu
3e5ea 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 72  ted when the par
3e5eb 73 65 72 20 61 63 63 65 70 74 73 0d 0a 2a 2f 0d  ser accepts..*/.
3e5ec 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
3e5ed 61 63 63 65 70 74 28 0d 0a 20 20 79 79 50 61 72  accept(..  yyPar
3e5ee 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20  ser *yypParser  
3e5ef 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3e5f0 70 61 72 73 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20  parser */..){.. 
3e5f1 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
3e5f2 47 5f 46 45 54 43 48 3b 0d 0a 23 69 66 6e 64 65  G_FETCH;..#ifnde
3e5f3 66 20 4e 44 45 42 55 47 0d 0a 20 20 69 66 28 20  f NDEBUG..  if( 
3e5f4 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0d 0a  yyTraceFILE ){..
3e5f5 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
3e5f6 61 63 65 46 49 4c 45 2c 22 25 73 41 63 63 65 70  aceFILE,"%sAccep
3e5f7 74 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  t!\n",yyTracePro
3e5f8 6d 70 74 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64  mpt);..  }..#end
3e5f9 69 66 0d 0a 20 20 77 68 69 6c 65 28 20 79 79 70  if..  while( yyp
3e5fa 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
3e5fb 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
3e5fc 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
3e5fd 29 3b 0d 0a 20 20 2f 2a 20 48 65 72 65 20 63 6f  );..  /* Here co
3e5fe 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
3e5ff 68 69 63 68 20 77 69 6c 6c 20 62 65 20 65 78 65  hich will be exe
3e600 63 75 74 65 64 20 77 68 65 6e 65 76 65 72 20 74  cuted whenever t
3e601 68 65 0d 0a 20 20 2a 2a 20 70 61 72 73 65 72 20  he..  ** parser 
3e602 61 63 63 65 70 74 73 20 2a 2f 0d 0a 20 20 73 71  accepts */..  sq
3e603 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
3e604 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73  TORE; /* Suppres
3e605 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20  s warning about 
3e606 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72  unused %extra_ar
3e607 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  gument variable 
3e608 2a 2f 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 54 68 65 20  */..}..../* The 
3e609 6d 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f 67  main parser prog
3e60a 72 61 6d 2e 0d 0a 2a 2a 20 54 68 65 20 66 69 72  ram...** The fir
3e60b 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
3e60c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
3e60d 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64  ructure obtained
3e60e 20 66 72 6f 6d 0d 0a 2a 2a 20 22 73 71 6c 69 74   from..** "sqlit
3e60f 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20 77  e3ParserAlloc" w
3e610 68 69 63 68 20 64 65 73 63 72 69 62 65 73 20 74  hich describes t
3e611 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
3e612 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e 0d   of the parser..
3e613 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
3e614 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d  rgument is the m
3e615 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65  ajor token numbe
3e616 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69 73  r.  The third is
3e617 0d 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74  ..** the minor t
3e618 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74  oken.  The fourt
3e619 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d  h optional argum
3e61a 65 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20  ent is whatever 
3e61b 74 68 65 0d 0a 2a 2a 20 75 73 65 72 20 77 61 6e  the..** user wan
3e61c 74 73 20 28 61 6e 64 20 73 70 65 63 69 66 69 65  ts (and specifie
3e61d 64 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  d in the grammar
3e61e 29 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62  ) and is availab
3e61f 6c 65 20 66 6f 72 0d 0a 2a 2a 20 75 73 65 20 62  le for..** use b
3e620 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75  y the action rou
3e621 74 69 6e 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  tines...**..** I
3e622 6e 70 75 74 73 3a 0d 0a 2a 2a 20 3c 75 6c 3e 0d  nputs:..** <ul>.
3e623 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 6f 69 6e 74  .** <li> A point
3e624 65 72 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  er to the parser
3e625 20 28 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75   (an opaque stru
3e626 63 74 75 72 65 2e 29 0d 0a 2a 2a 20 3c 6c 69 3e  cture.)..** <li>
3e627 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e   The major token
3e628 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 20 3c 6c 69   number...** <li
3e629 3e 20 54 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65  > The minor toke
3e62a 6e 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2a 20 3c 6c  n number...** <l
3e62b 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20 61 72 67  i> An option arg
3e62c 75 6d 65 6e 74 20 6f 66 20 61 20 67 72 61 6d 6d  ument of a gramm
3e62d 61 72 2d 73 70 65 63 69 66 69 65 64 20 74 79 70  ar-specified typ
3e62e 65 2e 0d 0a 2a 2a 20 3c 2f 75 6c 3e 0d 0a 2a 2a  e...** </ul>..**
3e62f 0d 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0d 0a 2a  ..** Outputs:..*
3e630 2a 20 4e 6f 6e 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  * None...*/..SQL
3e631 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3e632 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 0d   sqlite3Parser(.
3e633 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20  .  void *yyp,   
3e634 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e635 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
3e636 0d 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c  ..  int yymajor,
3e637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e638 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f   /* The major to
3e639 6b 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20  ken code number 
3e63a 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 50 61 72  */..  sqlite3Par
3e63b 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d  serTOKENTYPE yym
3e63c 69 6e 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68  inor       /* Th
3e63d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
3e63e 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69  token */..  sqli
3e63f 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45  te3ParserARG_PDE
3e640 43 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CL              
3e641 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78   /* Optional %ex
3e642 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 70 61 72  tra_argument par
3e643 61 6d 65 74 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20  ameter */..){.. 
3e644 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d   YYMINORTYPE yym
3e645 69 6e 6f 72 75 6e 69 6f 6e 3b 0d 0a 20 20 69 6e  inorunion;..  in
3e646 74 20 79 79 61 63 74 3b 20 20 20 20 20 20 20 20  t yyact;        
3e647 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
3e648 72 20 61 63 74 69 6f 6e 2e 20 2a 2f 0d 0a 23 69  r action. */..#i
3e649 66 20 21 64 65 66 69 6e 65 64 28 59 59 45 52 52  f !defined(YYERR
3e64a 4f 52 53 59 4d 42 4f 4c 29 20 26 26 20 21 64 65  ORSYMBOL) && !de
3e64b 66 69 6e 65 64 28 59 59 4e 4f 45 52 52 4f 52 52  fined(YYNOERRORR
3e64c 45 43 4f 56 45 52 59 29 0d 0a 20 20 69 6e 74 20  ECOVERY)..  int 
3e64d 79 79 65 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20  yyendofinput;   
3e64e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20    /* True if we 
3e64f 61 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  are at the end o
3e650 66 20 69 6e 70 75 74 20 2a 2f 0d 0a 23 65 6e 64  f input */..#end
3e651 69 66 0d 0a 23 69 66 64 65 66 20 59 59 45 52 52  if..#ifdef YYERR
3e652 4f 52 53 59 4d 42 4f 4c 0d 0a 20 20 69 6e 74 20  ORSYMBOL..  int 
3e653 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20  yyerrorhit = 0; 
3e654 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d    /* True if yym
3e655 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64  ajor has invoked
3e656 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0d 0a 23 65   an error */..#e
3e657 6e 64 69 66 0d 0a 20 20 79 79 50 61 72 73 65 72  ndif..  yyParser
3e658 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a   *yypParser;  /*
3e659 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0d 0a   The parser */..
3e65a 0d 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69  ..  /* (re)initi
3e65b 61 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65 72  alize the parser
3e65c 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a  , if necessary *
3e65d 2f 0d 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d  /..  yypParser =
3e65e 20 28 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b   (yyParser*)yyp;
3e65f 0d 0a 20 20 69 66 28 20 79 79 70 50 61 72 73 65  ..  if( yypParse
3e660 72 2d 3e 79 79 69 64 78 3c 30 20 29 7b 0d 0a 23  r->yyidx<0 ){..#
3e661 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c  if YYSTACKDEPTH<
3e662 3d 30 0d 0a 20 20 20 20 69 66 28 20 79 79 70 50  =0..    if( yypP
3e663 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3c  arser->yystksz <
3e664 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 6d  =0 ){..      /*m
3e665 65 6d 73 65 74 28 26 79 79 6d 69 6e 6f 72 75 6e  emset(&yyminorun
3e666 69 6f 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79  ion, 0, sizeof(y
3e667 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 29 3b 2a 2f  yminorunion));*/
3e668 0d 0a 20 20 20 20 20 20 79 79 6d 69 6e 6f 72 75  ..      yyminoru
3e669 6e 69 6f 6e 20 3d 20 79 79 7a 65 72 6f 6d 69 6e  nion = yyzeromin
3e66a 6f 72 3b 0d 0a 20 20 20 20 20 20 79 79 53 74 61  or;..      yySta
3e66b 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61  ckOverflow(yypPa
3e66c 72 73 65 72 2c 20 26 79 79 6d 69 6e 6f 72 75 6e  rser, &yyminorun
3e66d 69 6f 6e 29 3b 0d 0a 20 20 20 20 20 20 72 65 74  ion);..      ret
3e66e 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  urn;..    }..#en
3e66f 64 69 66 0d 0a 20 20 20 20 79 79 70 50 61 72 73  dif..    yypPars
3e670 65 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0d 0a  er->yyidx = 0;..
3e671 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
3e672 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0d 0a 20  yerrcnt = -1;.. 
3e673 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
3e674 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f  stack[0].stateno
3e675 20 3d 20 30 3b 0d 0a 20 20 20 20 79 79 70 50 61   = 0;..    yypPa
3e676 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d  rser->yystack[0]
3e677 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0d 0a 20 20 7d  .major = 0;..  }
3e678 0d 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  ..  yyminorunion
3e679 2e 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0d  .yy0 = yyminor;.
3e67a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 59 59  .#if !defined(YY
3e67b 45 52 52 4f 52 53 59 4d 42 4f 4c 29 20 26 26 20  ERRORSYMBOL) && 
3e67c 21 64 65 66 69 6e 65 64 28 59 59 4e 4f 45 52 52  !defined(YYNOERR
3e67d 4f 52 52 45 43 4f 56 45 52 59 29 0d 0a 20 20 79  ORRECOVERY)..  y
3e67e 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28 79  yendofinput = (y
3e67f 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0d 0a 23 65 6e  ymajor==0);..#en
3e680 64 69 66 0d 0a 20 20 73 71 6c 69 74 65 33 50 61  dif..  sqlite3Pa
3e681 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 0d 0a  rserARG_STORE;..
3e682 0d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
3e683 0d 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  ..  if( yyTraceF
3e684 49 4c 45 20 29 7b 0d 0a 20 20 20 20 66 70 72 69  ILE ){..    fpri
3e685 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
3e686 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79  "%sInput %s\n",y
3e687 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54  yTracePrompt,yyT
3e688 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72  okenName[yymajor
3e689 5d 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66  ]);..  }..#endif
3e68a 0d 0a 0d 0a 20 20 64 6f 7b 0d 0a 20 20 20 20 79  ....  do{..    y
3e68b 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73  yact = yy_find_s
3e68c 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50  hift_action(yypP
3e68d 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50  arser,(YYCODETYP
3e68e 45 29 79 79 6d 61 6a 6f 72 29 3b 0d 0a 20 20 20  E)yymajor);..   
3e68f 20 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54   if( yyact<YYNST
3e690 41 54 45 20 29 7b 0d 0a 20 20 20 20 20 20 79 79  ATE ){..      yy
3e691 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72  _shift(yypParser
3e692 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72 2c 26  ,yyact,yymajor,&
3e693 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0d 0a  yyminorunion);..
3e694 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
3e695 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0d 0a 20 20  >yyerrcnt--;..  
3e696 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
3e697 4e 4f 43 4f 44 45 3b 0d 0a 20 20 20 20 7d 65 6c  NOCODE;..    }el
3e698 73 65 20 69 66 28 20 79 79 61 63 74 20 3c 20 59  se if( yyact < Y
3e699 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c  YNSTATE + YYNRUL
3e69a 45 20 29 7b 0d 0a 20 20 20 20 20 20 79 79 5f 72  E ){..      yy_r
3e69b 65 64 75 63 65 28 79 79 70 50 61 72 73 65 72 2c  educe(yypParser,
3e69c 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29 3b  yyact-YYNSTATE);
3e69d 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
3e69e 20 20 20 20 61 73 73 65 72 74 28 20 79 79 61 63      assert( yyac
3e69f 74 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43  t == YY_ERROR_AC
3e6a0 54 49 4f 4e 20 29 3b 0d 0a 23 69 66 64 65 66 20  TION );..#ifdef 
3e6a1 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0d 0a 20  YYERRORSYMBOL.. 
3e6a2 20 20 20 20 20 69 6e 74 20 79 79 6d 78 3b 0d 0a       int yymx;..
3e6a3 23 65 6e 64 69 66 0d 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
3e6a4 4e 44 45 42 55 47 0d 0a 20 20 20 20 20 20 69 66  NDEBUG..      if
3e6a5 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
3e6a6 0d 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74  ..        fprint
3e6a7 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
3e6a8 73 53 79 6e 74 61 78 20 45 72 72 6f 72 21 5c 6e  sSyntax Error!\n
3e6a9 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
3e6aa 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64  ;..      }..#end
3e6ab 69 66 0d 0a 23 69 66 64 65 66 20 59 59 45 52 52  if..#ifdef YYERR
3e6ac 4f 52 53 59 4d 42 4f 4c 0d 0a 20 20 20 20 20 20  ORSYMBOL..      
3e6ad 2f 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f  /* A syntax erro
3e6ae 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0d  r has occurred..
3e6af 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65  .      ** The re
3e6b0 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72  sponse to an err
3e6b1 6f 72 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20  or depends upon 
3e6b2 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
3e6b3 68 65 0d 0a 20 20 20 20 20 20 2a 2a 20 67 72 61  he..      ** gra
3e6b4 6d 6d 61 72 20 64 65 66 69 6e 65 73 20 61 6e 20  mmar defines an 
3e6b5 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52  error token "ERR
3e6b6 4f 52 22 2e 20 20 0d 0a 20 20 20 20 20 20 2a 2a  OR".  ..      **
3e6b7 0d 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  ..      ** This 
3e6b8 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69 66  is what we do if
3e6b9 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65   the grammar doe
3e6ba 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0d  s define ERROR:.
3e6bb 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20  .      **..     
3e6bc 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20   **  * Call the 
3e6bd 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75  %syntax_error fu
3e6be 6e 63 74 69 6f 6e 2e 0d 0a 20 20 20 20 20 20 2a  nction...      *
3e6bf 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42  *..      **  * B
3e6c0 65 67 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65  egin popping the
3e6c1 20 73 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20   stack until we 
3e6c2 65 6e 74 65 72 20 61 20 73 74 61 74 65 20 77 68  enter a state wh
3e6c3 65 72 65 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20  ere..      **   
3e6c4 20 69 74 20 69 73 20 6c 65 67 61 6c 20 74 6f 20   it is legal to 
3e6c5 73 68 69 66 74 20 74 68 65 20 65 72 72 6f 72 20  shift the error 
3e6c6 73 79 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69  symbol, then shi
3e6c7 66 74 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ft..      **    
3e6c8 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c  the error symbol
3e6c9 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20  ...      **..   
3e6ca 20 20 20 2a 2a 20 20 2a 20 53 65 74 20 74 68 65     **  * Set the
3e6cb 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 74 6f 20   error count to 
3e6cc 74 68 72 65 65 2e 0d 0a 20 20 20 20 20 20 2a 2a  three...      **
3e6cd 0d 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65  ..      **  * Be
3e6ce 67 69 6e 20 61 63 63 65 70 74 69 6e 67 20 61 6e  gin accepting an
3e6cf 64 20 73 68 69 66 74 69 6e 67 20 6e 65 77 20 74  d shifting new t
3e6d0 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77 20 65  okens.  No new e
3e6d1 72 72 6f 72 0d 0a 20 20 20 20 20 20 2a 2a 20 20  rror..      **  
3e6d2 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77 69 6c    processing wil
3e6d3 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20 74 68  l occur until th
3e6d4 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76 65 20  ree tokens have 
3e6d5 62 65 65 6e 0d 0a 20 20 20 20 20 20 2a 2a 20 20  been..      **  
3e6d6 20 20 73 68 69 66 74 65 64 20 73 75 63 63 65 73    shifted succes
3e6d7 73 66 75 6c 6c 79 2e 0d 0a 20 20 20 20 20 20 2a  sfully...      *
3e6d8 2a 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  *..      */..   
3e6d9 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
3e6da 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29 7b 0d  ->yyerrcnt<0 ){.
3e6db 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74  .        yy_synt
3e6dc 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
3e6dd 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
3e6de 6f 72 75 6e 69 6f 6e 29 3b 0d 0a 20 20 20 20 20  orunion);..     
3e6df 20 7d 0d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d   }..      yymx =
3e6e0 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74   yypParser->yyst
3e6e1 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
3e6e2 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0d 0a 20 20  yidx].major;..  
3e6e3 20 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59      if( yymx==YY
3e6e4 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79  ERRORSYMBOL || y
3e6e5 79 65 72 72 6f 72 68 69 74 20 29 7b 0d 0a 23 69  yerrorhit ){..#i
3e6e6 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 20 20  fndef NDEBUG..  
3e6e7 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
3e6e8 65 46 49 4c 45 20 29 7b 0d 0a 20 20 20 20 20 20  eFILE ){..      
3e6e9 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
3e6ea 61 63 65 46 49 4c 45 2c 22 25 73 44 69 73 63 61  aceFILE,"%sDisca
3e6eb 72 64 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25  rd input token %
3e6ec 73 5c 6e 22 2c 0d 0a 20 20 20 20 20 20 20 20 20  s\n",..         
3e6ed 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
3e6ee 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  t,yyTokenName[yy
3e6ef 6d 61 6a 6f 72 5d 29 3b 0d 0a 20 20 20 20 20 20  major]);..      
3e6f0 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20    }..#endif..   
3e6f1 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74       yy_destruct
3e6f2 6f 72 28 79 79 70 50 61 72 73 65 72 2c 20 28 59  or(yypParser, (Y
3e6f3 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f  YCODETYPE)yymajo
3e6f4 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  r,&yyminorunion)
3e6f5 3b 0d 0a 20 20 20 20 20 20 20 20 79 79 6d 61 6a  ;..        yymaj
3e6f6 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0d 0a  or = YYNOCODE;..
3e6f7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3e6f8 20 20 20 20 20 20 20 77 68 69 6c 65 28 0d 0a 20         while(.. 
3e6f9 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73           yypPars
3e6fa 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26  er->yyidx >= 0 &
3e6fb 26 0d 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d  &..          yym
3e6fc 78 20 21 3d 20 59 59 45 52 52 4f 52 53 59 4d 42  x != YYERRORSYMB
3e6fd 4f 4c 20 26 26 0d 0a 20 20 20 20 20 20 20 20 20  OL &&..         
3e6fe 20 28 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e   (yyact = yy_fin
3e6ff 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28  d_reduce_action(
3e700 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3e701 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72            yypPar
3e702 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70  ser->yystack[yyp
3e703 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73  Parser->yyidx].s
3e704 74 61 74 65 6e 6f 2c 0d 0a 20 20 20 20 20 20 20  tateno,..       
3e705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e706 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29   YYERRORSYMBOL))
3e707 20 3e 3d 20 59 59 4e 53 54 41 54 45 0d 0a 20 20   >= YYNSTATE..  
3e708 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20        ){..      
3e709 20 20 20 20 79 79 5f 70 6f 70 5f 70 61 72 73 65      yy_pop_parse
3e70a 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65  r_stack(yypParse
3e70b 72 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  r);..        }..
3e70c 20 20 20 20 20 20 20 20 69 66 28 20 79 79 70 50          if( yypP
3e70d 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3c 20 30  arser->yyidx < 0
3e70e 20 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d 30 20 29   || yymajor==0 )
3e70f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  {..          yy_
3e710 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61  destructor(yypPa
3e711 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45  rser,(YYCODETYPE
3e712 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  )yymajor,&yymino
3e713 72 75 6e 69 6f 6e 29 3b 0d 0a 20 20 20 20 20 20  runion);..      
3e714 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69      yy_parse_fai
3e715 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b 0d  led(yypParser);.
3e716 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 61 6a  .          yymaj
3e717 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0d 0a  or = YYNOCODE;..
3e718 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
3e719 28 20 79 79 6d 78 21 3d 59 59 45 52 52 4f 52 53  ( yymx!=YYERRORS
3e71a 59 4d 42 4f 4c 20 29 7b 0d 0a 20 20 20 20 20 20  YMBOL ){..      
3e71b 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20      YYMINORTYPE 
3e71c 75 32 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 75  u2;..          u
3e71d 32 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20 30  2.YYERRSYMDT = 0
3e71e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  ;..          yy_
3e71f 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c  shift(yypParser,
3e720 79 79 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d  yyact,YYERRORSYM
3e721 42 4f 4c 2c 26 75 32 29 3b 0d 0a 20 20 20 20 20  BOL,&u2);..     
3e722 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
3e723 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
3e724 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0d 0a 20  yyerrcnt = 3;.. 
3e725 20 20 20 20 20 79 79 65 72 72 6f 72 68 69 74 20       yyerrorhit 
3e726 3d 20 31 3b 0d 0a 23 65 6c 69 66 20 64 65 66 69  = 1;..#elif defi
3e727 6e 65 64 28 59 59 4e 4f 45 52 52 4f 52 52 45 43  ned(YYNOERRORREC
3e728 4f 56 45 52 59 29 0d 0a 20 20 20 20 20 20 2f 2a  OVERY)..      /*
3e729 20 49 66 20 74 68 65 20 59 59 4e 4f 45 52 52 4f   If the YYNOERRO
3e72a 52 52 45 43 4f 56 45 52 59 20 6d 61 63 72 6f 20  RRECOVERY macro 
3e72b 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  is defined, then
3e72c 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
3e72d 74 6f 0d 0a 20 20 20 20 20 20 2a 2a 20 64 6f 20  to..      ** do 
3e72e 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  any kind of erro
3e72f 72 20 72 65 63 6f 76 65 72 79 2e 20 20 49 6e 73  r recovery.  Ins
3e730 74 65 61 64 2c 20 73 69 6d 70 6c 79 20 69 6e 76  tead, simply inv
3e731 6f 6b 65 20 74 68 65 20 73 79 6e 74 61 78 0d 0a  oke the syntax..
3e732 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 72        ** error r
3e733 6f 75 74 69 6e 65 20 61 6e 64 20 63 6f 6e 74 69  outine and conti
3e734 6e 75 65 20 67 6f 69 6e 67 20 61 73 20 69 66 20  nue going as if 
3e735 6e 6f 74 68 69 6e 67 20 68 61 64 20 68 61 70 70  nothing had happ
3e736 65 6e 65 64 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d  ened...      **.
3e737 0a 20 20 20 20 20 20 2a 2a 20 41 70 70 6c 69 63  .      ** Applic
3e738 61 74 69 6f 6e 73 20 63 61 6e 20 73 65 74 20 74  ations can set t
3e739 68 69 73 20 6d 61 63 72 6f 20 28 66 6f 72 20 65  his macro (for e
3e73a 78 61 6d 70 6c 65 20 69 6e 73 69 64 65 20 25 69  xample inside %i
3e73b 6e 63 6c 75 64 65 29 20 69 66 0d 0a 20 20 20 20  nclude) if..    
3e73c 20 20 2a 2a 20 74 68 65 79 20 69 6e 74 65 6e 64    ** they intend
3e73d 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20   to abandon the 
3e73e 70 61 72 73 65 20 75 70 6f 6e 20 74 68 65 20 66  parse upon the f
3e73f 69 72 73 74 20 73 79 6e 74 61 78 20 65 72 72 6f  irst syntax erro
3e740 72 20 73 65 65 6e 2e 0d 0a 20 20 20 20 20 20 2a  r seen...      *
3e741 2f 0d 0a 20 20 20 20 20 20 79 79 5f 73 79 6e 74  /..      yy_synt
3e742 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
3e743 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
3e744 6f 72 75 6e 69 6f 6e 29 3b 0d 0a 20 20 20 20 20  orunion);..     
3e745 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
3e746 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45  ypParser,(YYCODE
3e747 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79  TYPE)yymajor,&yy
3e748 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0d 0a 20 20  minorunion);..  
3e749 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
3e74a 4e 4f 43 4f 44 45 3b 0d 0a 20 20 20 20 20 20 0d  NOCODE;..      .
3e74b 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52  .#else  /* YYERR
3e74c 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20  ORSYMBOL is not 
3e74d 64 65 66 69 6e 65 64 20 2a 2f 0d 0a 20 20 20 20  defined */..    
3e74e 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77 68 61    /* This is wha
3e74f 74 20 77 65 20 64 6f 20 69 66 20 74 68 65 20 67  t we do if the g
3e750 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20  rammar does not 
3e751 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0d 0a 20  define ERROR:.. 
3e752 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a       **..      *
3e753 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65  *  * Report an e
3e754 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e  rror message, an
3e755 64 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65  d throw away the
3e756 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0d 0a 20   input token... 
3e757 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a       **..      *
3e758 2a 20 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75  *  * If the inpu
3e759 74 20 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68  t token is $, th
3e75a 65 6e 20 66 61 69 6c 20 74 68 65 20 70 61 72 73  en fail the pars
3e75b 65 2e 0d 0a 20 20 20 20 20 20 2a 2a 0d 0a 20 20  e...      **..  
3e75c 20 20 20 20 2a 2a 20 41 73 20 62 65 66 6f 72 65      ** As before
3e75d 2c 20 73 75 62 73 65 71 75 65 6e 74 20 65 72 72  , subsequent err
3e75e 6f 72 20 6d 65 73 73 61 67 65 73 20 61 72 65 20  or messages are 
3e75f 73 75 70 70 72 65 73 73 65 64 20 75 6e 74 69 6c  suppressed until
3e760 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 72 65 65  ..      ** three
3e761 20 69 6e 70 75 74 20 74 6f 6b 65 6e 73 20 68 61   input tokens ha
3e762 76 65 20 62 65 65 6e 20 73 75 63 63 65 73 73 66  ve been successf
3e763 75 6c 6c 79 20 73 68 69 66 74 65 64 2e 0d 0a 20  ully shifted... 
3e764 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69       */..      i
3e765 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
3e766 65 72 72 63 6e 74 3c 3d 30 20 29 7b 0d 0a 20 20  errcnt<=0 ){..  
3e767 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f        yy_syntax_
3e768 65 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c  error(yypParser,
3e769 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75  yymajor,yyminoru
3e76a 6e 69 6f 6e 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  nion);..      }.
3e76b 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
3e76c 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0d  ->yyerrcnt = 3;.
3e76d 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75  .      yy_destru
3e76e 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28  ctor(yypParser,(
3e76f 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
3e770 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
3e771 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 79 79  );..      if( yy
3e772 65 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0d 0a 20  endofinput ){.. 
3e773 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f         yy_parse_
3e774 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72  failed(yypParser
3e775 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
3e776 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
3e777 4f 43 4f 44 45 3b 0d 0a 23 65 6e 64 69 66 0d 0a  OCODE;..#endif..
3e778 20 20 20 20 7d 0d 0a 20 20 7d 77 68 69 6c 65 28      }..  }while(
3e779 20 79 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f   yymajor!=YYNOCO
3e77a 44 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d  DE && yypParser-
3e77b 3e 79 79 69 64 78 3e 3d 30 20 29 3b 0d 0a 20 20  >yyidx>=0 );..  
3e77c 72 65 74 75 72 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  return;..}..../*
3e77d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
3e77e 64 20 6f 66 20 70 61 72 73 65 2e 63 20 2a 2a 2a  d of parse.c ***
3e77f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e781 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
3e782 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
3e783 65 67 69 6e 20 66 69 6c 65 20 74 6f 6b 65 6e 69  egin file tokeni
3e784 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
3e785 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e786 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
3e787 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  /*..** 2001 Sept
3e788 65 6d 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a  ember 15..**..**
3e789 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
3e78a 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
3e78b 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
3e78c 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3e78d 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
3e78e 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3e78f 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
3e790 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
3e791 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
3e792 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
3e793 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
3e794 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
3e795 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
3e796 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
3e797 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
3e798 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
3e799 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
3e79a 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
3e79b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e79c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e79d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e79e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e79f 0d 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65  ..** An tokenize
3e7a0 72 20 66 6f 72 20 53 51 4c 0d 0a 2a 2a 0d 0a 2a  r for SQL..**..*
3e7a1 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
3e7a2 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74  ains C code that
3e7a3 20 73 70 6c 69 74 73 20 61 6e 20 53 51 4c 20 69   splits an SQL i
3e7a4 6e 70 75 74 20 73 74 72 69 6e 67 20 75 70 20 69  nput string up i
3e7a5 6e 74 6f 0d 0a 2a 2a 20 69 6e 64 69 76 69 64 75  nto..** individu
3e7a6 61 6c 20 74 6f 6b 65 6e 73 20 61 6e 64 20 73 65  al tokens and se
3e7a7 6e 64 73 20 74 68 6f 73 65 20 74 6f 6b 65 6e 73  nds those tokens
3e7a8 20 6f 6e 65 2d 62 79 2d 6f 6e 65 20 6f 76 65 72   one-by-one over
3e7a9 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 61 72 73   to the..** pars
3e7aa 65 72 20 66 6f 72 20 61 6e 61 6c 79 73 69 73 2e  er for analysis.
3e7ab 0d 0a 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  ..*/../* #includ
3e7ac 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0d  e <stdlib.h> */.
3e7ad 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 63 68  .../*..** The ch
3e7ae 61 72 4d 61 70 28 29 20 6d 61 63 72 6f 20 6d 61  arMap() macro ma
3e7af 70 73 20 61 6c 70 68 61 62 65 74 69 63 20 63 68  ps alphabetic ch
3e7b0 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
3e7b1 65 69 72 0d 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61  eir..** lower-ca
3e7b2 73 65 20 41 53 43 49 49 20 65 71 75 69 76 61 6c  se ASCII equival
3e7b3 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49 20 6d  ent.  On ASCII m
3e7b4 61 63 68 69 6e 65 73 2c 20 74 68 69 73 20 69 73  achines, this is
3e7b5 20 6a 75 73 74 0d 0a 2a 2a 20 61 6e 20 75 70 70   just..** an upp
3e7b6 65 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73 65  er-to-lower case
3e7b7 20 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44 49 43   map.  On EBCDIC
3e7b8 20 6d 61 63 68 69 6e 65 73 20 77 65 20 61 6c 73   machines we als
3e7b9 6f 20 6e 65 65 64 0d 0a 2a 2a 20 74 6f 20 61 64  o need..** to ad
3e7ba 6a 75 73 74 20 74 68 65 20 65 6e 63 6f 64 69 6e  just the encodin
3e7bb 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65  g.  Only alphabe
3e7bc 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 61  tic characters a
3e7bd 6e 64 20 75 6e 64 65 72 73 63 6f 72 65 73 0d 0a  nd underscores..
3e7be 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72  ** need to be tr
3e7bf 61 6e 73 6c 61 74 65 64 2e 0d 0a 2a 2f 0d 0a 23  anslated...*/..#
3e7c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
3e7c1 49 49 0d 0a 23 20 64 65 66 69 6e 65 20 63 68 61  II..# define cha
3e7c2 72 4d 61 70 28 58 29 20 6f 61 73 55 70 70 65 72  rMap(X) oasUpper
3e7c3 32 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64  2Lower[(unsigned
3e7c4 20 63 68 61 72 29 58 5d 0d 0a 23 65 6e 64 69 66   char)X]..#endif
3e7c5 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
3e7c6 45 42 43 44 49 43 0d 0a 23 20 64 65 66 69 6e 65  EBCDIC..# define
3e7c7 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63 64   charMap(X) ebcd
3e7c8 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69 67  icToAscii[(unsig
3e7c9 6e 65 64 20 63 68 61 72 29 58 5d 0d 0a 63 6f 6e  ned char)X]..con
3e7ca 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
3e7cb 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d   ebcdicToAscii[]
3e7cc 20 3d 20 7b 0d 0a 2f 2a 20 30 20 20 20 31 20 20   = {../* 0   1  
3e7cd 20 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20   2   3   4   5  
3e7ce 20 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20   6   7   8   9  
3e7cf 20 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20   A   B   C   D  
3e7d0 20 45 20 20 20 46 20 2a 2f 0d 0a 20 20 20 30 2c   E   F */..   0,
3e7d1 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7d2 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7d3 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7d4 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
3e7d5 20 30 78 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 30   0x */..   0,  0
3e7d6 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7d7 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7d8 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7d9 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78  ,  0,  0,  /* 1x
3e7da 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 30 2c 20 20   */..   0,  0,  
3e7db 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e7dc 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e7dd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e7de 30 2c 20 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f  0,  0,  /* 2x */
3e7df 0d 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  ..   0,  0,  0, 
3e7e0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7e1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7e2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7e3 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0d 0a 20   0,  /* 3x */.. 
3e7e4 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7e5 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7e6 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7e7 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7e8 20 20 2f 2a 20 34 78 20 2a 2f 0d 0a 20 20 20 30    /* 4x */..   0
3e7e9 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7ea 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7eb 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e7ec 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
3e7ed 2a 20 35 78 20 2a 2f 0d 0a 20 20 20 30 2c 20 20  * 5x */..   0,  
3e7ee 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e7ef 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e7f0 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 39  0,  0,  0,  0, 9
3e7f1 35 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 36  5,  0,  0,  /* 6
3e7f2 78 20 2a 2f 0d 0a 20 20 20 30 2c 20 20 30 2c 20  x */..   0,  0, 
3e7f3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7f4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7f5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e7f6 20 30 2c 20 20 30 2c 20 20 2f 2a 20 37 78 20 2a   0,  0,  /* 7x *
3e7f7 2f 0d 0a 20 20 20 30 2c 20 39 37 2c 20 39 38 2c  /..   0, 97, 98,
3e7f8 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
3e7f9 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30 2c  103,104,105,  0,
3e7fa 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e7fb 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0d 0a    0,  /* 8x */..
3e7fc 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38     0,106,107,108
3e7fd 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
3e7fe 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30  ,113,114,  0,  0
3e7ff 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e800 2c 20 20 2f 2a 20 39 78 20 2a 2f 0d 0a 20 20 20  ,  /* 9x */..   
3e801 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31  0,  0,115,116,11
3e802 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32  7,118,119,120,12
3e803 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20  1,122,  0,  0,  
3e804 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e805 2f 2a 20 41 78 20 2a 2f 0d 0a 20 20 20 30 2c 20  /* Ax */..   0, 
3e806 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e807 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e808 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e809 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
3e80a 42 78 20 2a 2f 0d 0a 20 20 20 30 2c 20 39 37 2c  Bx */..   0, 97,
3e80b 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
3e80c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c  102,103,104,105,
3e80d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
3e80e 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20    0,  0,  /* Cx 
3e80f 2a 2f 0d 0a 20 20 20 30 2c 31 30 36 2c 31 30 37  */..   0,106,107
3e810 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31  ,108,109,110,111
3e811 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30  ,112,113,114,  0
3e812 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3e813 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f 0d  ,  0,  /* Dx */.
3e814 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
3e815 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
3e816 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
3e817 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3e818 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0d 0a 20 20  0,  /* Ex */..  
3e819 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e81a 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e81b 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e81c 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
3e81d 20 2f 2a 20 46 78 20 2a 2f 0d 0a 7d 3b 0d 0a 23   /* Fx */..};..#
3e81e 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  endif..../*..** 
3e81f 54 68 65 20 73 71 6c 69 74 65 33 4b 65 79 77 6f  The sqlite3Keywo
3e820 72 64 43 6f 64 65 20 66 75 6e 63 74 69 6f 6e 20  rdCode function 
3e821 6c 6f 6f 6b 73 20 75 70 20 61 6e 20 69 64 65 6e  looks up an iden
3e822 74 69 66 69 65 72 20 74 6f 20 64 65 74 65 72 6d  tifier to determ
3e823 69 6e 65 20 69 66 0d 0a 2a 2a 20 69 74 20 69 73  ine if..** it is
3e824 20 61 20 6b 65 79 77 6f 72 64 2e 20 20 49 66 20   a keyword.  If 
3e825 69 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2c  it is a keyword,
3e826 20 74 68 65 20 74 6f 6b 65 6e 20 63 6f 64 65 20   the token code 
3e827 6f 66 20 74 68 61 74 20 6b 65 79 77 6f 72 64 20  of that keyword 
3e828 69 73 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64  is ..** returned
3e829 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  .  If the input 
3e82a 69 73 20 6e 6f 74 20 61 20 6b 65 79 77 6f 72 64  is not a keyword
3e82b 2c 20 54 4b 5f 49 44 20 69 73 20 72 65 74 75 72  , TK_ID is retur
3e82c 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ned...**..** The
3e82d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3e82e 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
3e82f 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79  was generated by
3e830 20 61 20 70 72 6f 67 72 61 6d 2c 0d 0a 2a 2a 20   a program,..** 
3e831 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 2c  mkkeywordhash.h,
3e832 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20   located in the 
3e833 74 6f 6f 6c 20 73 75 62 64 69 72 65 63 74 6f 72  tool subdirector
3e834 79 20 6f 66 20 74 68 65 20 64 69 73 74 72 69 62  y of the distrib
3e835 75 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 65 20 6f  ution...** The o
3e836 75 74 70 75 74 20 6f 66 20 74 68 65 20 6d 6b 6b  utput of the mkk
3e837 65 79 77 6f 72 64 68 61 73 68 2e 63 20 70 72 6f  eywordhash.c pro
3e838 67 72 61 6d 20 69 73 20 77 72 69 74 74 65 6e 20  gram is written 
3e839 69 6e 74 6f 20 61 20 66 69 6c 65 0d 0a 2a 2a 20  into a file..** 
3e83a 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64 68 61 73  named keywordhas
3e83b 68 2e 68 20 61 6e 64 20 74 68 65 6e 20 69 6e 63  h.h and then inc
3e83c 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 69 73 20  luded into this 
3e83d 73 6f 75 72 63 65 20 66 69 6c 65 20 62 79 0d 0a  source file by..
3e83e 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20  ** the #include 
3e83f 62 65 6c 6f 77 2e 0d 0a 2a 2f 0d 0a 2f 2a 2a 2a  below...*/../***
3e840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
3e841 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ude keywordhash.
3e842 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
3e843 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
3e844 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
3e845 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
3e846 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68  in file keywordh
3e847 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
3e848 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3e849 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
3e84a 2a 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  **** This file c
3e84b 6f 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69  ontains automati
3e84c 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
3e84d 63 6f 64 65 20 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d  code ******..**.
3e84e 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
3e84f 74 68 69 73 20 66 69 6c 65 20 68 61 73 20 62 65  this file has be
3e850 65 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  en automatically
3e851 20 67 65 6e 65 72 61 74 65 64 20 62 79 0d 0a 2a   generated by..*
3e852 2a 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 2f 74  *..**   sqlite/t
3e853 6f 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73  ool/mkkeywordhas
3e854 68 2e 63 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  h.c..**..** The 
3e855 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
3e856 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66  e implements a f
3e857 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 74  unction that det
3e858 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 0d  ermines whether.
3e859 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69 76  .** or not a giv
3e85a 65 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 73  en identifier is
3e85b 20 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20 6b   really an SQL k
3e85c 65 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61 6d  eyword.  The sam
3e85d 65 20 74 68 69 6e 67 0d 0a 2a 2a 20 6d 69 67 68  e thing..** migh
3e85e 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64  t be implemented
3e85f 20 6d 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75   more directly u
3e860 73 69 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74  sing a hand-writ
3e861 74 65 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0d  ten hash table..
3e862 0a 2a 2a 20 42 75 74 20 62 79 20 75 73 69 6e 67  .** But by using
3e863 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61   this automatica
3e864 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
3e865 64 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20  de, the size of 
3e866 74 68 65 20 63 6f 64 65 0d 0a 2a 2a 20 69 73 20  the code..** is 
3e867 73 75 62 73 74 61 6e 74 69 61 6c 6c 79 20 72 65  substantially re
3e868 64 75 63 65 64 2e 20 20 54 68 69 73 20 69 73 20  duced.  This is 
3e869 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 65 6d  important for em
3e86a 62 65 64 64 65 64 20 61 70 70 6c 69 63 61 74 69  bedded applicati
3e86b 6f 6e 73 0d 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66  ons..** on platf
3e86c 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d 69 74 65  orms with limite
3e86d 64 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 2f  d memory...*/../
3e86e 2a 20 48 61 73 68 20 73 63 6f 72 65 3a 20 31 37  * Hash score: 17
3e86f 35 20 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  5 */..static int
3e870 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e   keywordCode(con
3e871 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
3e872 6e 29 7b 0d 0a 20 20 2f 2a 20 7a 54 65 78 74 5b  n){..  /* zText[
3e873 5d 20 65 6e 63 6f 64 65 73 20 38 31 31 20 62 79  ] encodes 811 by
3e874 74 65 73 20 6f 66 20 6b 65 79 77 6f 72 64 73 20  tes of keywords 
3e875 69 6e 20 35 34 31 20 62 79 74 65 73 20 2a 2f 0d  in 541 bytes */.
3e876 0a 20 20 2f 2a 20 20 20 52 45 49 4e 44 45 58 45  .  /*   REINDEXE
3e877 44 45 53 43 41 50 45 41 43 48 45 43 4b 45 59 42  DESCAPEACHECKEYB
3e878 45 46 4f 52 45 49 47 4e 4f 52 45 47 45 58 50 4c  EFOREIGNOREGEXPL
3e879 41 49 4e 53 54 45 41 44 44 41 54 41 42 41 53 45  AINSTEADDATABASE
3e87a 4c 45 43 54 20 20 20 20 20 20 20 2a 2f 0d 0a 20  LECT       */.. 
3e87b 20 2f 2a 20 20 20 41 42 4c 45 46 54 48 45 4e 44   /*   ABLEFTHEND
3e87c 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43 45 50  EFERRABLELSEXCEP
3e87d 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41  TRANSACTIONATURA
3e87e 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49 56 45  LTERAISEXCLUSIVE
3e87f 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 2f           */..  /
3e880 2a 20 20 20 58 49 53 54 53 41 56 45 50 4f 49 4e  *   XISTSAVEPOIN
3e881 54 45 52 53 45 43 54 52 49 47 47 45 52 45 46 45  TERSECTRIGGEREFE
3e882 52 45 4e 43 45 53 43 4f 4e 53 54 52 41 49 4e 54  RENCESCONSTRAINT
3e883 4f 46 46 53 45 54 45 4d 50 4f 52 41 52 59 20 20  OFFSETEMPORARY  
3e884 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 2f 2a 20         */..  /* 
3e885 20 20 55 4e 49 51 55 45 52 59 41 54 54 41 43 48    UNIQUERYATTACH
3e886 41 56 49 4e 47 52 4f 55 50 44 41 54 45 42 45 47  AVINGROUPDATEBEG
3e887 49 4e 4e 45 52 45 4c 45 41 53 45 42 45 54 57 45  INNERELEASEBETWE
3e888 45 4e 4f 54 4e 55 4c 4c 49 4b 45 20 20 20 20 20  ENOTNULLIKE     
3e889 20 20 20 20 20 2a 2f 0d 0a 20 20 2f 2a 20 20 20       */..  /*   
3e88a 43 41 53 43 41 44 45 4c 45 54 45 43 41 53 45 43  CASCADELETECASEC
3e88b 4f 4c 4c 41 54 45 43 52 45 41 54 45 43 55 52 52  OLLATECREATECURR
3e88c 45 4e 54 5f 44 41 54 45 44 45 54 41 43 48 49 4d  ENT_DATEDETACHIM
3e88d 4d 45 44 49 41 54 45 4a 4f 49 4e 20 20 20 20 20  MEDIATEJOIN     
3e88e 20 20 20 2a 2f 0d 0a 20 20 2f 2a 20 20 20 53 45     */..  /*   SE
3e88f 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c 59 5a 45  RTMATCHPLANALYZE
3e890 50 52 41 47 4d 41 42 4f 52 54 56 41 4c 55 45 53  PRAGMABORTVALUES
3e891 56 49 52 54 55 41 4c 49 4d 49 54 57 48 45 4e 57  VIRTUALIMITWHENW
3e892 48 45 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20  HERENAME        
3e893 20 2a 2f 0d 0a 20 20 2f 2a 20 20 20 41 46 54 45   */..  /*   AFTE
3e894 52 45 50 4c 41 43 45 41 4e 44 45 46 41 55 4c 54  REPLACEANDEFAULT
3e895 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 43 41 53  AUTOINCREMENTCAS
3e896 54 43 4f 4c 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e  TCOLUMNCOMMITCON
3e897 46 4c 49 43 54 43 52 4f 53 53 20 20 20 20 20 2a  FLICTCROSS     *
3e898 2f 0d 0a 20 20 2f 2a 20 20 20 43 55 52 52 45 4e  /..  /*   CURREN
3e899 54 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41 52  T_TIMESTAMPRIMAR
3e89a 59 44 45 46 45 52 52 45 44 49 53 54 49 4e 43 54  YDEFERREDISTINCT
3e89b 44 52 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c 4c  DROPFAILFROMFULL
3e89c 47 4c 4f 42 59 49 46 20 20 20 20 20 20 2a 2f 0d  GLOBYIF      */.
3e89d 0a 20 20 2f 2a 20 20 20 49 53 4e 55 4c 4c 4f 52  .  /*   ISNULLOR
3e89e 44 45 52 45 53 54 52 49 43 54 4f 55 54 45 52 49  DERESTRICTOUTERI
3e89f 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e  GHTROLLBACKROWUN
3e8a0 49 4f 4e 55 53 49 4e 47 56 41 43 55 55 4d 56 49  IONUSINGVACUUMVI
3e8a1 45 57 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  EW         */.. 
3e8a2 20 2f 2a 20 20 20 49 4e 49 54 49 41 4c 4c 59 20   /*   INITIALLY 
3e8a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8a6 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 73           */..  s
3e8a7 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
3e8a8 20 7a 54 65 78 74 5b 35 34 30 5d 20 3d 20 7b 0d   zText[540] = {.
3e8a9 0a 20 20 20 20 27 52 27 2c 27 45 27 2c 27 49 27  .    'R','E','I'
3e8aa 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 58 27  ,'N','D','E','X'
3e8ab 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 53 27  ,'E','D','E','S'
3e8ac 2c 27 43 27 2c 27 41 27 2c 27 50 27 2c 27 45 27  ,'C','A','P','E'
3e8ad 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 0d 0a 20  ,'A','C','H',.. 
3e8ae 20 20 20 27 45 27 2c 27 43 27 2c 27 4b 27 2c 27     'E','C','K','
3e8af 45 27 2c 27 59 27 2c 27 42 27 2c 27 45 27 2c 27  E','Y','B','E','
3e8b0 46 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27  F','O','R','E','
3e8b1 49 27 2c 27 47 27 2c 27 4e 27 2c 27 4f 27 2c 27  I','G','N','O','
3e8b2 52 27 2c 27 45 27 2c 27 47 27 2c 0d 0a 20 20 20  R','E','G',..   
3e8b3 20 27 45 27 2c 27 58 27 2c 27 50 27 2c 27 4c 27   'E','X','P','L'
3e8b4 2c 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27  ,'A','I','N','S'
3e8b5 2c 27 54 27 2c 27 45 27 2c 27 41 27 2c 27 44 27  ,'T','E','A','D'
3e8b6 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 41 27  ,'D','A','T','A'
3e8b7 2c 27 42 27 2c 27 41 27 2c 0d 0a 20 20 20 20 27  ,'B','A',..    '
3e8b8 53 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  S','E','L','E','
3e8b9 43 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27  C','T','A','B','
3e8ba 4c 27 2c 27 45 27 2c 27 46 27 2c 27 54 27 2c 27  L','E','F','T','
3e8bb 48 27 2c 27 45 27 2c 27 4e 27 2c 27 44 27 2c 27  H','E','N','D','
3e8bc 45 27 2c 27 46 27 2c 0d 0a 20 20 20 20 27 45 27  E','F',..    'E'
3e8bd 2c 27 52 27 2c 27 52 27 2c 27 41 27 2c 27 42 27  ,'R','R','A','B'
3e8be 2c 27 4c 27 2c 27 45 27 2c 27 4c 27 2c 27 53 27  ,'L','E','L','S'
3e8bf 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27 45 27  ,'E','X','C','E'
3e8c0 2c 27 50 27 2c 27 54 27 2c 27 52 27 2c 27 41 27  ,'P','T','R','A'
3e8c1 2c 27 4e 27 2c 0d 0a 20 20 20 20 27 53 27 2c 27  ,'N',..    'S','
3e8c2 41 27 2c 27 43 27 2c 27 54 27 2c 27 49 27 2c 27  A','C','T','I','
3e8c3 4f 27 2c 27 4e 27 2c 27 41 27 2c 27 54 27 2c 27  O','N','A','T','
3e8c4 55 27 2c 27 52 27 2c 27 41 27 2c 27 4c 27 2c 27  U','R','A','L','
3e8c5 54 27 2c 27 45 27 2c 27 52 27 2c 27 41 27 2c 27  T','E','R','A','
3e8c6 49 27 2c 0d 0a 20 20 20 20 27 53 27 2c 27 45 27  I',..    'S','E'
3e8c7 2c 27 58 27 2c 27 43 27 2c 27 4c 27 2c 27 55 27  ,'X','C','L','U'
3e8c8 2c 27 53 27 2c 27 49 27 2c 27 56 27 2c 27 45 27  ,'S','I','V','E'
3e8c9 2c 27 58 27 2c 27 49 27 2c 27 53 27 2c 27 54 27  ,'X','I','S','T'
3e8ca 2c 27 53 27 2c 27 41 27 2c 27 56 27 2c 27 45 27  ,'S','A','V','E'
3e8cb 2c 0d 0a 20 20 20 20 27 50 27 2c 27 4f 27 2c 27  ,..    'P','O','
3e8cc 49 27 2c 27 4e 27 2c 27 54 27 2c 27 45 27 2c 27  I','N','T','E','
3e8cd 52 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27  R','S','E','C','
3e8ce 54 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27  T','R','I','G','
3e8cf 47 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 0d  G','E','R','E',.
3e8d0 0a 20 20 20 20 27 46 27 2c 27 45 27 2c 27 52 27  .    'F','E','R'
3e8d1 2c 27 45 27 2c 27 4e 27 2c 27 43 27 2c 27 45 27  ,'E','N','C','E'
3e8d2 2c 27 53 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27  ,'S','C','O','N'
3e8d3 2c 27 53 27 2c 27 54 27 2c 27 52 27 2c 27 41 27  ,'S','T','R','A'
3e8d4 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c 0d 0a 20  ,'I','N','T',.. 
3e8d5 20 20 20 27 4f 27 2c 27 46 27 2c 27 46 27 2c 27     'O','F','F','
3e8d6 53 27 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c 27  S','E','T','E','
3e8d7 4d 27 2c 27 50 27 2c 27 4f 27 2c 27 52 27 2c 27  M','P','O','R','
3e8d8 41 27 2c 27 52 27 2c 27 59 27 2c 27 55 27 2c 27  A','R','Y','U','
3e8d9 4e 27 2c 27 49 27 2c 27 51 27 2c 0d 0a 20 20 20  N','I','Q',..   
3e8da 20 27 55 27 2c 27 45 27 2c 27 52 27 2c 27 59 27   'U','E','R','Y'
3e8db 2c 27 41 27 2c 27 54 27 2c 27 54 27 2c 27 41 27  ,'A','T','T','A'
3e8dc 2c 27 43 27 2c 27 48 27 2c 27 41 27 2c 27 56 27  ,'C','H','A','V'
3e8dd 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27 52 27  ,'I','N','G','R'
3e8de 2c 27 4f 27 2c 27 55 27 2c 0d 0a 20 20 20 20 27  ,'O','U',..    '
3e8df 50 27 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27  P','D','A','T','
3e8e0 45 27 2c 27 42 27 2c 27 45 27 2c 27 47 27 2c 27  E','B','E','G','
3e8e1 49 27 2c 27 4e 27 2c 27 4e 27 2c 27 45 27 2c 27  I','N','N','E','
3e8e2 52 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  R','E','L','E','
3e8e3 41 27 2c 27 53 27 2c 0d 0a 20 20 20 20 27 45 27  A','S',..    'E'
3e8e4 2c 27 42 27 2c 27 45 27 2c 27 54 27 2c 27 57 27  ,'B','E','T','W'
3e8e5 2c 27 45 27 2c 27 45 27 2c 27 4e 27 2c 27 4f 27  ,'E','E','N','O'
3e8e6 2c 27 54 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27  ,'T','N','U','L'
3e8e7 2c 27 4c 27 2c 27 49 27 2c 27 4b 27 2c 27 45 27  ,'L','I','K','E'
3e8e8 2c 27 43 27 2c 0d 0a 20 20 20 20 27 41 27 2c 27  ,'C',..    'A','
3e8e9 53 27 2c 27 43 27 2c 27 41 27 2c 27 44 27 2c 27  S','C','A','D','
3e8ea 45 27 2c 27 4c 27 2c 27 45 27 2c 27 54 27 2c 27  E','L','E','T','
3e8eb 45 27 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27  E','C','A','S','
3e8ec 45 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27  E','C','O','L','
3e8ed 4c 27 2c 0d 0a 20 20 20 20 27 41 27 2c 27 54 27  L',..    'A','T'
3e8ee 2c 27 45 27 2c 27 43 27 2c 27 52 27 2c 27 45 27  ,'E','C','R','E'
3e8ef 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27  ,'A','T','E','C'
3e8f0 2c 27 55 27 2c 27 52 27 2c 27 52 27 2c 27 45 27  ,'U','R','R','E'
3e8f1 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 44 27  ,'N','T','_','D'
3e8f2 2c 0d 0a 20 20 20 20 27 41 27 2c 27 54 27 2c 27  ,..    'A','T','
3e8f3 45 27 2c 27 44 27 2c 27 45 27 2c 27 54 27 2c 27  E','D','E','T','
3e8f4 41 27 2c 27 43 27 2c 27 48 27 2c 27 49 27 2c 27  A','C','H','I','
3e8f5 4d 27 2c 27 4d 27 2c 27 45 27 2c 27 44 27 2c 27  M','M','E','D','
3e8f6 49 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 0d  I','A','T','E',.
3e8f7 0a 20 20 20 20 27 4a 27 2c 27 4f 27 2c 27 49 27  .    'J','O','I'
3e8f8 2c 27 4e 27 2c 27 53 27 2c 27 45 27 2c 27 52 27  ,'N','S','E','R'
3e8f9 2c 27 54 27 2c 27 4d 27 2c 27 41 27 2c 27 54 27  ,'T','M','A','T'
3e8fa 2c 27 43 27 2c 27 48 27 2c 27 50 27 2c 27 4c 27  ,'C','H','P','L'
3e8fb 2c 27 41 27 2c 27 4e 27 2c 27 41 27 2c 0d 0a 20  ,'A','N','A',.. 
3e8fc 20 20 20 27 4c 27 2c 27 59 27 2c 27 5a 27 2c 27     'L','Y','Z','
3e8fd 45 27 2c 27 50 27 2c 27 52 27 2c 27 41 27 2c 27  E','P','R','A','
3e8fe 47 27 2c 27 4d 27 2c 27 41 27 2c 27 42 27 2c 27  G','M','A','B','
3e8ff 4f 27 2c 27 52 27 2c 27 54 27 2c 27 56 27 2c 27  O','R','T','V','
3e900 41 27 2c 27 4c 27 2c 27 55 27 2c 0d 0a 20 20 20  A','L','U',..   
3e901 20 27 45 27 2c 27 53 27 2c 27 56 27 2c 27 49 27   'E','S','V','I'
3e902 2c 27 52 27 2c 27 54 27 2c 27 55 27 2c 27 41 27  ,'R','T','U','A'
3e903 2c 27 4c 27 2c 27 49 27 2c 27 4d 27 2c 27 49 27  ,'L','I','M','I'
3e904 2c 27 54 27 2c 27 57 27 2c 27 48 27 2c 27 45 27  ,'T','W','H','E'
3e905 2c 27 4e 27 2c 27 57 27 2c 0d 0a 20 20 20 20 27  ,'N','W',..    '
3e906 48 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27  H','E','R','E','
3e907 4e 27 2c 27 41 27 2c 27 4d 27 2c 27 45 27 2c 27  N','A','M','E','
3e908 41 27 2c 27 46 27 2c 27 54 27 2c 27 45 27 2c 27  A','F','T','E','
3e909 52 27 2c 27 45 27 2c 27 50 27 2c 27 4c 27 2c 27  R','E','P','L','
3e90a 41 27 2c 27 43 27 2c 0d 0a 20 20 20 20 27 45 27  A','C',..    'E'
3e90b 2c 27 41 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27  ,'A','N','D','E'
3e90c 2c 27 46 27 2c 27 41 27 2c 27 55 27 2c 27 4c 27  ,'F','A','U','L'
3e90d 2c 27 54 27 2c 27 41 27 2c 27 55 27 2c 27 54 27  ,'T','A','U','T'
3e90e 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27  ,'O','I','N','C'
3e90f 2c 27 52 27 2c 0d 0a 20 20 20 20 27 45 27 2c 27  ,'R',..    'E','
3e910 4d 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27  M','E','N','T','
3e911 43 27 2c 27 41 27 2c 27 53 27 2c 27 54 27 2c 27  C','A','S','T','
3e912 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 55 27 2c 27  C','O','L','U','
3e913 4d 27 2c 27 4e 27 2c 27 43 27 2c 27 4f 27 2c 27  M','N','C','O','
3e914 4d 27 2c 0d 0a 20 20 20 20 27 4d 27 2c 27 49 27  M',..    'M','I'
3e915 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27  ,'T','C','O','N'
3e916 2c 27 46 27 2c 27 4c 27 2c 27 49 27 2c 27 43 27  ,'F','L','I','C'
3e917 2c 27 54 27 2c 27 43 27 2c 27 52 27 2c 27 4f 27  ,'T','C','R','O'
3e918 2c 27 53 27 2c 27 53 27 2c 27 43 27 2c 27 55 27  ,'S','S','C','U'
3e919 2c 0d 0a 20 20 20 20 27 52 27 2c 27 52 27 2c 27  ,..    'R','R','
3e91a 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27  E','N','T','_','
3e91b 54 27 2c 27 49 27 2c 27 4d 27 2c 27 45 27 2c 27  T','I','M','E','
3e91c 53 27 2c 27 54 27 2c 27 41 27 2c 27 4d 27 2c 27  S','T','A','M','
3e91d 50 27 2c 27 52 27 2c 27 49 27 2c 27 4d 27 2c 0d  P','R','I','M',.
3e91e 0a 20 20 20 20 27 41 27 2c 27 52 27 2c 27 59 27  .    'A','R','Y'
3e91f 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 45 27  ,'D','E','F','E'
3e920 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 44 27  ,'R','R','E','D'
3e921 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 49 27  ,'I','S','T','I'
3e922 2c 27 4e 27 2c 27 43 27 2c 27 54 27 2c 0d 0a 20  ,'N','C','T',.. 
3e923 20 20 20 27 44 27 2c 27 52 27 2c 27 4f 27 2c 27     'D','R','O','
3e924 50 27 2c 27 46 27 2c 27 41 27 2c 27 49 27 2c 27  P','F','A','I','
3e925 4c 27 2c 27 46 27 2c 27 52 27 2c 27 4f 27 2c 27  L','F','R','O','
3e926 4d 27 2c 27 46 27 2c 27 55 27 2c 27 4c 27 2c 27  M','F','U','L','
3e927 4c 27 2c 27 47 27 2c 27 4c 27 2c 0d 0a 20 20 20  L','G','L',..   
3e928 20 27 4f 27 2c 27 42 27 2c 27 59 27 2c 27 49 27   'O','B','Y','I'
3e929 2c 27 46 27 2c 27 49 27 2c 27 53 27 2c 27 4e 27  ,'F','I','S','N'
3e92a 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27  ,'U','L','L','O'
3e92b 2c 27 52 27 2c 27 44 27 2c 27 45 27 2c 27 52 27  ,'R','D','E','R'
3e92c 2c 27 45 27 2c 27 53 27 2c 0d 0a 20 20 20 20 27  ,'E','S',..    '
3e92d 54 27 2c 27 52 27 2c 27 49 27 2c 27 43 27 2c 27  T','R','I','C','
3e92e 54 27 2c 27 4f 27 2c 27 55 27 2c 27 54 27 2c 27  T','O','U','T','
3e92f 45 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27  E','R','I','G','
3e930 48 27 2c 27 54 27 2c 27 52 27 2c 27 4f 27 2c 27  H','T','R','O','
3e931 4c 27 2c 27 4c 27 2c 0d 0a 20 20 20 20 27 42 27  L','L',..    'B'
3e932 2c 27 41 27 2c 27 43 27 2c 27 4b 27 2c 27 52 27  ,'A','C','K','R'
3e933 2c 27 4f 27 2c 27 57 27 2c 27 55 27 2c 27 4e 27  ,'O','W','U','N'
3e934 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 55 27  ,'I','O','N','U'
3e935 2c 27 53 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27  ,'S','I','N','G'
3e936 2c 27 56 27 2c 0d 0a 20 20 20 20 27 41 27 2c 27  ,'V',..    'A','
3e937 43 27 2c 27 55 27 2c 27 55 27 2c 27 4d 27 2c 27  C','U','U','M','
3e938 56 27 2c 27 49 27 2c 27 45 27 2c 27 57 27 2c 27  V','I','E','W','
3e939 49 27 2c 27 4e 27 2c 27 49 27 2c 27 54 27 2c 27  I','N','I','T','
3e93a 49 27 2c 27 41 27 2c 27 4c 27 2c 27 4c 27 2c 27  I','A','L','L','
3e93b 59 27 2c 0d 0a 20 20 7d 3b 0d 0a 20 20 73 74 61  Y',..  };..  sta
3e93c 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
3e93d 65 64 20 63 68 61 72 20 61 48 61 73 68 5b 31 32  ed char aHash[12
3e93e 37 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 37 32  7] = {..      72
3e93f 2c 20 31 30 31 2c 20 31 31 34 2c 20 20 37 30 2c  , 101, 114,  70,
3e940 20 20 20 30 2c 20 20 34 35 2c 20 20 20 30 2c 20     0,  45,   0, 
3e941 20 20 30 2c 20 20 37 38 2c 20 20 20 30 2c 20 20    0,  78,   0,  
3e942 37 33 2c 20 20 20 30 2c 20 20 20 30 2c 0d 0a 20  73,   0,   0,.. 
3e943 20 20 20 20 20 34 32 2c 20 20 31 32 2c 20 20 37       42,  12,  7
3e944 34 2c 20 20 31 35 2c 20 20 20 30 2c 20 31 31 33  4,  15,   0, 113
3e945 2c 20 20 38 31 2c 20 20 35 30 2c 20 31 30 38 2c  ,  81,  50, 108,
3e946 20 20 20 30 2c 20 20 31 39 2c 20 20 20 30 2c 20     0,  19,   0, 
3e947 20 20 30 2c 0d 0a 20 20 20 20 20 31 31 38 2c 20    0,..     118, 
3e948 20 20 30 2c 20 31 31 36 2c 20 31 31 31 2c 20 20    0, 116, 111,  
3e949 20 30 2c 20 20 32 32 2c 20 20 38 39 2c 20 20 20   0,  22,  89,   
3e94a 30 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30  0,   9,   0,   0
3e94b 2c 20 20 36 36 2c 20 20 36 37 2c 0d 0a 20 20 20  ,  66,  67,..   
3e94c 20 20 20 20 30 2c 20 20 36 35 2c 20 20 20 36 2c      0,  65,   6,
3e94d 20 20 20 30 2c 20 20 34 38 2c 20 20 38 36 2c 20     0,  48,  86, 
3e94e 20 39 38 2c 20 20 20 30 2c 20 31 31 35 2c 20 20   98,   0, 115,  
3e94f 39 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34  97,   0,   0,  4
3e950 34 2c 0d 0a 20 20 20 20 20 20 20 30 2c 20 20 39  4,..       0,  9
3e951 39 2c 20 20 32 34 2c 20 20 20 30 2c 20 20 31 37  9,  24,   0,  17
3e952 2c 20 20 20 30 2c 20 31 31 39 2c 20 20 34 39 2c  ,   0, 119,  49,
3e953 20 20 32 33 2c 20 20 20 30 2c 20 20 20 35 2c 20    23,   0,   5, 
3e954 31 30 36 2c 20 20 32 35 2c 0d 0a 20 20 20 20 20  106,  25,..     
3e955 20 39 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 31   92,   0,   0, 1
3e956 32 31 2c 20 31 30 32 2c 20 20 35 36 2c 20 31 32  21, 102,  56, 12
3e957 30 2c 20 20 35 33 2c 20 20 32 38 2c 20 20 35 31  0,  53,  28,  51
3e958 2c 20 20 20 30 2c 20 20 38 37 2c 20 20 20 30 2c  ,   0,  87,   0,
3e959 0d 0a 20 20 20 20 20 20 39 36 2c 20 20 32 36 2c  ..      96,  26,
3e95a 20 20 20 30 2c 20 20 39 35 2c 20 20 20 30 2c 20     0,  95,   0, 
3e95b 20 20 30 2c 20 20 20 30 2c 20 20 39 31 2c 20 20    0,   0,  91,  
3e95c 38 38 2c 20 20 39 33 2c 20 20 38 34 2c 20 31 30  88,  93,  84, 10
3e95d 35 2c 20 20 31 34 2c 0d 0a 20 20 20 20 20 20 33  5,  14,..      3
3e95e 39 2c 20 31 30 34 2c 20 20 20 30 2c 20 20 37 37  9, 104,   0,  77
3e95f 2c 20 20 20 30 2c 20 20 31 38 2c 20 20 38 35 2c  ,   0,  18,  85,
3e960 20 31 30 37 2c 20 20 33 32 2c 20 20 20 30 2c 20   107,  32,   0, 
3e961 31 31 37 2c 20 20 37 36 2c 20 31 30 39 2c 0d 0a  117,  76, 109,..
3e962 20 20 20 20 20 20 35 38 2c 20 20 34 36 2c 20 20        58,  46,  
3e963 38 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 39  80,   0,   0,  9
3e964 30 2c 20 20 34 30 2c 20 20 20 30 2c 20 31 31 32  0,  40,   0, 112
3e965 2c 20 20 20 30 2c 20 20 33 36 2c 20 20 20 30 2c  ,   0,  36,   0,
3e966 20 20 20 30 2c 0d 0a 20 20 20 20 20 20 32 39 2c     0,..      29,
3e967 20 20 20 30 2c 20 20 38 32 2c 20 20 35 39 2c 20     0,  82,  59, 
3e968 20 36 30 2c 20 20 20 30 2c 20 20 32 30 2c 20 20   60,   0,  20,  
3e969 35 37 2c 20 20 20 30 2c 20 20 35 32 2c 0d 0a 20  57,   0,  52,.. 
3e96a 20 7d 3b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f   };..  static co
3e96b 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3e96c 72 20 61 4e 65 78 74 5b 31 32 31 5d 20 3d 20 7b  r aNext[121] = {
3e96d 0d 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c  ..       0,   0,
3e96e 20 20 20 30 2c 20 20 20 30 2c 20 20 20 34 2c 20     0,   0,   4, 
3e96f 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3e970 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
3e971 30 2c 20 20 20 30 2c 0d 0a 20 20 20 20 20 20 20  0,   0,..       
3e972 30 2c 20 20 20 32 2c 20 20 20 30 2c 20 20 20 30  0,   2,   0,   0
3e973 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3e974 20 20 20 30 2c 20 20 31 33 2c 20 20 20 30 2c 20     0,  13,   0, 
3e975 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0d 0a    0,   0,   0,..
3e976 20 20 20 20 20 20 20 30 2c 20 20 20 37 2c 20 20         0,   7,  
3e977 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
3e978 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3e979 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3e97a 20 20 20 30 2c 0d 0a 20 20 20 20 20 20 20 30 2c     0,..       0,
3e97b 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3e97c 20 33 33 2c 20 20 20 30 2c 20 20 32 31 2c 20 20   33,   0,  21,  
3e97d 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34   0,   0,   0,  4
3e97e 33 2c 20 20 20 33 2c 20 20 34 37 2c 0d 0a 20 20  3,   3,  47,..  
3e97f 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30       0,   0,   0
3e980 2c 20 20 20 30 2c 20 20 33 30 2c 20 20 20 30 2c  ,   0,  30,   0,
3e981 20 20 35 34 2c 20 20 20 30 2c 20 20 33 38 2c 20    54,   0,  38, 
3e982 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3e983 20 31 2c 0d 0a 20 20 20 20 20 20 36 32 2c 20 20   1,..      62,  
3e984 20 30 2c 20 20 20 30 2c 20 20 36 33 2c 20 20 20   0,   0,  63,   
3e985 30 2c 20 20 34 31 2c 20 20 20 30 2c 20 20 20 30  0,  41,   0,   0
3e986 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3e987 20 20 20 30 2c 20 20 20 30 2c 0d 0a 20 20 20 20     0,   0,..    
3e988 20 20 36 31 2c 20 20 20 30 2c 20 20 20 30 2c 20    61,   0,   0, 
3e989 20 20 30 2c 20 20 20 30 2c 20 20 33 31 2c 20 20    0,   0,  31,  
3e98a 35 35 2c 20 20 31 36 2c 20 20 33 34 2c 20 20 31  55,  16,  34,  1
3e98b 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3e98c 2c 0d 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30  ,..       0,   0
3e98d 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c  ,   0,   0,  11,
3e98e 20 20 36 38 2c 20 20 37 35 2c 20 20 20 30 2c 20    68,  75,   0, 
3e98f 20 20 38 2c 20 20 20 30 2c 20 31 30 30 2c 20 20    8,   0, 100,  
3e990 39 34 2c 20 20 20 30 2c 0d 0a 20 20 20 20 20 31  94,   0,..     1
3e991 30 33 2c 20 20 20 30 2c 20 20 38 33 2c 20 20 20  03,   0,  83,   
3e992 30 2c 20 20 37 31 2c 20 20 20 30 2c 20 20 20 30  0,  71,   0,   0
3e993 2c 20 31 31 30 2c 20 20 32 37 2c 20 20 33 37 2c  , 110,  27,  37,
3e994 20 20 36 39 2c 20 20 37 39 2c 20 20 20 30 2c 0d    69,  79,   0,.
3e995 0a 20 20 20 20 20 20 33 35 2c 20 20 36 34 2c 20  .      35,  64, 
3e996 20 20 30 2c 20 20 20 30 2c 0d 0a 20 20 7d 3b 0d    0,   0,..  };.
3e997 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3e998 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c  unsigned char aL
3e999 65 6e 5b 31 32 31 5d 20 3d 20 7b 0d 0a 20 20 20  en[121] = {..   
3e99a 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c      7,   7,   5,
3e99b 20 20 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20     4,   6,   4, 
3e99c 20 20 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20    5,   3,   6,  
3e99d 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   7,   3,   6,   
3e99e 36 2c 0d 0a 20 20 20 20 20 20 20 37 2c 20 20 20  6,..       7,   
3e99f 37 2c 20 20 20 33 2c 20 20 20 38 2c 20 20 20 32  7,   3,   8,   2
3e9a0 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c  ,   6,   5,   4,
3e9a1 20 20 20 34 2c 20 20 20 33 2c 20 20 31 30 2c 20     4,   3,  10, 
3e9a2 20 20 34 2c 20 20 20 36 2c 0d 0a 20 20 20 20 20    4,   6,..     
3e9a3 20 31 31 2c 20 20 20 36 2c 20 20 20 32 2c 20 20   11,   6,   2,  
3e9a4 20 37 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20   7,   5,   5,   
3e9a5 39 2c 20 20 20 36 2c 20 20 20 39 2c 20 20 20 39  9,   6,   9,   9
3e9a6 2c 20 20 20 37 2c 20 20 31 30 2c 20 20 31 30 2c  ,   7,  10,  10,
3e9a7 0d 0a 20 20 20 20 20 20 20 34 2c 20 20 20 36 2c  ..       4,   6,
3e9a8 20 20 20 32 2c 20 20 20 33 2c 20 20 20 39 2c 20     2,   3,   9, 
3e9a9 20 20 34 2c 20 20 20 32 2c 20 20 20 36 2c 20 20    4,   2,   6,  
3e9aa 20 35 2c 20 20 20 36 2c 20 20 20 36 2c 20 20 20   5,   6,   6,   
3e9ab 35 2c 20 20 20 36 2c 0d 0a 20 20 20 20 20 20 20  5,   6,..       
3e9ac 35 2c 20 20 20 35 2c 20 20 20 37 2c 20 20 20 37  5,   5,   7,   7
3e9ad 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 32 2c  ,   7,   3,   2,
3e9ae 20 20 20 34 2c 20 20 20 34 2c 20 20 20 37 2c 20     4,   4,   7, 
3e9af 20 20 33 2c 20 20 20 36 2c 20 20 20 34 2c 0d 0a    3,   6,   4,..
3e9b0 20 20 20 20 20 20 20 37 2c 20 20 20 36 2c 20 20         7,   6,  
3e9b1 31 32 2c 20 20 20 36 2c 20 20 20 39 2c 20 20 20  12,   6,   9,   
3e9b2 34 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34  4,   6,   5,   4
3e9b3 2c 20 20 20 37 2c 20 20 20 36 2c 20 20 20 35 2c  ,   7,   6,   5,
3e9b4 20 20 20 36 2c 0d 0a 20 20 20 20 20 20 20 37 2c     6,..       7,
3e9b5 20 20 20 35 2c 20 20 20 34 2c 20 20 20 35 2c 20     5,   4,   5, 
3e9b6 20 20 36 2c 20 20 20 35 2c 20 20 20 37 2c 20 20    6,   5,   7,  
3e9b7 20 33 2c 20 20 20 37 2c 20 20 31 33 2c 20 20 20   3,   7,  13,   
3e9b8 32 2c 20 20 20 32 2c 20 20 20 34 2c 0d 0a 20 20  2,   2,   4,..  
3e9b9 20 20 20 20 20 36 2c 20 20 20 36 2c 20 20 20 38       6,   6,   8
3e9ba 2c 20 20 20 35 2c 20 20 31 37 2c 20 20 31 32 2c  ,   5,  17,  12,
3e9bb 20 20 20 37 2c 20 20 20 38 2c 20 20 20 38 2c 20     7,   8,   8, 
3e9bc 20 20 32 2c 20 20 20 34 2c 20 20 20 34 2c 20 20    2,   4,   4,  
3e9bd 20 34 2c 0d 0a 20 20 20 20 20 20 20 34 2c 20 20   4,..       4,  
3e9be 20 34 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20   4,   2,   2,   
3e9bf 36 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20 35  6,   5,   8,   5
3e9c0 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20 33 2c  ,   5,   8,   3,
3e9c1 20 20 20 35 2c 20 20 20 35 2c 0d 0a 20 20 20 20     5,   5,..    
3e9c2 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39 2c 20     6,   4,   9, 
3e9c3 20 20 33 2c 0d 0a 20 20 7d 3b 0d 0a 20 20 73 74    3,..  };..  st
3e9c4 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
3e9c5 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 20 61 4f  ned short int aO
3e9c6 66 66 73 65 74 5b 31 32 31 5d 20 3d 20 7b 0d 0a  ffset[121] = {..
3e9c7 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20         0,   2,  
3e9c8 20 32 2c 20 20 20 38 2c 20 20 20 39 2c 20 20 31   2,   8,   9,  1
3e9c9 34 2c 20 20 31 36 2c 20 20 32 30 2c 20 20 32 33  4,  16,  20,  23
3e9ca 2c 20 20 32 35 2c 20 20 32 35 2c 20 20 32 39 2c  ,  25,  25,  29,
3e9cb 20 20 33 33 2c 0d 0a 20 20 20 20 20 20 33 36 2c    33,..      36,
3e9cc 20 20 34 31 2c 20 20 34 36 2c 20 20 34 38 2c 20    41,  46,  48, 
3e9cd 20 35 33 2c 20 20 35 34 2c 20 20 35 39 2c 20 20   53,  54,  59,  
3e9ce 36 32 2c 20 20 36 35 2c 20 20 36 37 2c 20 20 36  62,  65,  67,  6
3e9cf 39 2c 20 20 37 38 2c 20 20 38 31 2c 0d 0a 20 20  9,  78,  81,..  
3e9d0 20 20 20 20 38 36 2c 20 20 39 31 2c 20 20 39 35      86,  91,  95
3e9d1 2c 20 20 39 36 2c 20 31 30 31 2c 20 31 30 35 2c  ,  96, 101, 105,
3e9d2 20 31 30 39 2c 20 31 31 37 2c 20 31 32 32 2c 20   109, 117, 122, 
3e9d3 31 32 38 2c 20 31 33 36 2c 20 31 34 32 2c 20 31  128, 136, 142, 1
3e9d4 35 32 2c 0d 0a 20 20 20 20 20 31 35 39 2c 20 31  52,..     159, 1
3e9d5 36 32 2c 20 31 36 32 2c 20 31 36 35 2c 20 31 36  62, 162, 165, 16
3e9d6 37 2c 20 31 36 37 2c 20 31 37 31 2c 20 31 37 36  7, 167, 171, 176
3e9d7 2c 20 31 37 39 2c 20 31 38 34 2c 20 31 38 39 2c  , 179, 184, 189,
3e9d8 20 31 39 34 2c 20 31 39 37 2c 0d 0a 20 20 20 20   194, 197,..    
3e9d9 20 32 30 33 2c 20 32 30 36 2c 20 32 31 30 2c 20   203, 206, 210, 
3e9da 32 31 37 2c 20 32 32 33 2c 20 32 32 33 2c 20 32  217, 223, 223, 2
3e9db 32 33 2c 20 32 32 36 2c 20 32 32 39 2c 20 32 33  23, 226, 229, 23
3e9dc 33 2c 20 32 33 34 2c 20 32 33 38 2c 20 32 34 34  3, 234, 238, 244
3e9dd 2c 0d 0a 20 20 20 20 20 32 34 38 2c 20 32 35 35  ,..     248, 255
3e9de 2c 20 32 36 31 2c 20 32 37 33 2c 20 32 37 39 2c  , 261, 273, 279,
3e9df 20 32 38 38 2c 20 32 39 30 2c 20 32 39 36 2c 20   288, 290, 296, 
3e9e0 33 30 31 2c 20 33 30 33 2c 20 33 31 30 2c 20 33  301, 303, 310, 3
3e9e1 31 35 2c 20 33 32 30 2c 0d 0a 20 20 20 20 20 33  15, 320,..     3
3e9e2 32 36 2c 20 33 33 32 2c 20 33 33 37 2c 20 33 34  26, 332, 337, 34
3e9e3 31 2c 20 33 34 34 2c 20 33 35 30 2c 20 33 35 34  1, 344, 350, 354
3e9e4 2c 20 33 36 31 2c 20 33 36 33 2c 20 33 37 30 2c  , 361, 363, 370,
3e9e5 20 33 37 32 2c 20 33 37 34 2c 20 33 38 33 2c 0d   372, 374, 383,.
3e9e6 0a 20 20 20 20 20 33 38 37 2c 20 33 39 33 2c 20  .     387, 393, 
3e9e7 33 39 39 2c 20 34 30 37 2c 20 34 31 32 2c 20 34  399, 407, 412, 4
3e9e8 31 32 2c 20 34 32 38 2c 20 34 33 35 2c 20 34 34  12, 428, 435, 44
3e9e9 32 2c 20 34 34 33 2c 20 34 35 30 2c 20 34 35 34  2, 443, 450, 454
3e9ea 2c 20 34 35 38 2c 0d 0a 20 20 20 20 20 34 36 32  , 458,..     462
3e9eb 2c 20 34 36 36 2c 20 34 36 39 2c 20 34 37 31 2c  , 466, 469, 471,
3e9ec 20 34 37 33 2c 20 34 37 39 2c 20 34 38 33 2c 20   473, 479, 483, 
3e9ed 34 39 31 2c 20 34 39 35 2c 20 35 30 30 2c 20 35  491, 495, 500, 5
3e9ee 30 38 2c 20 35 31 31 2c 20 35 31 36 2c 0d 0a 20  08, 511, 516,.. 
3e9ef 20 20 20 20 35 32 31 2c 20 35 32 37 2c 20 35 33      521, 527, 53
3e9f0 31 2c 20 35 33 36 2c 0d 0a 20 20 7d 3b 0d 0a 20  1, 536,..  };.. 
3e9f1 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
3e9f2 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 64  signed char aCod
3e9f3 65 5b 31 32 31 5d 20 3d 20 7b 0d 0a 20 20 20 20  e[121] = {..    
3e9f4 54 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20 54  TK_REINDEX,    T
3e9f5 4b 5f 49 4e 44 45 58 45 44 2c 20 20 20 20 54 4b  K_INDEXED,    TK
3e9f6 5f 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f  _INDEX,      TK_
3e9f7 44 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f 45  DESC,       TK_E
3e9f8 53 43 41 50 45 2c 20 20 20 20 20 0d 0a 20 20 20  SCAPE,     ..   
3e9f9 20 54 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20   TK_EACH,       
3e9fa 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54  TK_CHECK,      T
3e9fb 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b  K_KEY,        TK
3e9fc 5f 42 45 46 4f 52 45 2c 20 20 20 20 20 54 4b 5f  _BEFORE,     TK_
3e9fd 46 4f 52 45 49 47 4e 2c 20 20 20 20 0d 0a 20 20  FOREIGN,    ..  
3e9fe 20 20 54 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20    TK_FOR,       
3e9ff 20 54 4b 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20   TK_IGNORE,     
3ea00 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
3ea01 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b  K_EXPLAIN,    TK
3ea02 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 0d 0a 20  _INSTEAD,    .. 
3ea03 20 20 20 54 4b 5f 41 44 44 2c 20 20 20 20 20 20     TK_ADD,      
3ea04 20 20 54 4b 5f 44 41 54 41 42 41 53 45 2c 20 20    TK_DATABASE,  
3ea05 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20   TK_AS,         
3ea06 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54  TK_SELECT,     T
3ea07 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20 0d 0a  K_TABLE,      ..
3ea08 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
3ea09 20 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20 20 20     TK_THEN,     
3ea0a 20 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20 20 20    TK_END,       
3ea0b 20 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 2c 20   TK_DEFERRABLE, 
3ea0c 54 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20 0d  TK_ELSE,       .
3ea0d 0a 20 20 20 20 54 4b 5f 45 58 43 45 50 54 2c 20  .    TK_EXCEPT, 
3ea0e 20 20 20 20 54 4b 5f 54 52 41 4e 53 41 43 54 49      TK_TRANSACTI
3ea0f 4f 4e 2c 54 4b 5f 41 43 54 49 4f 4e 2c 20 20 20  ON,TK_ACTION,   
3ea10 20 20 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20 20    TK_ON,        
3ea11 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
3ea12 0d 0a 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20  ..    TK_ALTER, 
3ea13 20 20 20 20 20 54 4b 5f 52 41 49 53 45 2c 20 20       TK_RAISE,  
3ea14 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49 56 45      TK_EXCLUSIVE
3ea15 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20  ,  TK_EXISTS,   
3ea16 20 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 2c 20    TK_SAVEPOINT, 
3ea17 20 0d 0a 20 20 20 20 54 4b 5f 49 4e 54 45 52 53   ..    TK_INTERS
3ea18 45 43 54 2c 20 20 54 4b 5f 54 52 49 47 47 45 52  ECT,  TK_TRIGGER
3ea19 2c 20 20 20 20 54 4b 5f 52 45 46 45 52 45 4e 43  ,    TK_REFERENC
3ea1a 45 53 2c 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e  ES, TK_CONSTRAIN
3ea1b 54 2c 20 54 4b 5f 49 4e 54 4f 2c 20 20 20 20 20  T, TK_INTO,     
3ea1c 20 20 0d 0a 20 20 20 20 54 4b 5f 4f 46 46 53 45    ..    TK_OFFSE
3ea1d 54 2c 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20 20  T,     TK_OF,   
3ea1e 20 20 20 20 20 20 54 4b 5f 53 45 54 2c 20 20 20        TK_SET,   
3ea1f 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20       TK_TEMP,   
3ea20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20      TK_TEMP,    
3ea21 20 20 20 0d 0a 20 20 20 20 54 4b 5f 4f 52 2c 20     ..    TK_OR, 
3ea22 20 20 20 20 20 20 20 20 54 4b 5f 55 4e 49 51 55          TK_UNIQU
3ea23 45 2c 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c  E,     TK_QUERY,
3ea24 20 20 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c        TK_ATTACH,
3ea25 20 20 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20       TK_HAVING, 
3ea26 20 20 20 20 0d 0a 20 20 20 20 54 4b 5f 47 52 4f      ..    TK_GRO
3ea27 55 50 2c 20 20 20 20 20 20 54 4b 5f 55 50 44 41  UP,      TK_UPDA
3ea28 54 45 2c 20 20 20 20 20 54 4b 5f 42 45 47 49 4e  TE,     TK_BEGIN
3ea29 2c 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b  ,      TK_JOIN_K
3ea2a 57 2c 20 20 20 20 54 4b 5f 52 45 4c 45 41 53 45  W,    TK_RELEASE
3ea2b 2c 20 20 20 20 0d 0a 20 20 20 20 54 4b 5f 42 45  ,    ..    TK_BE
3ea2c 54 57 45 45 4e 2c 20 20 20 20 54 4b 5f 4e 4f 54  TWEEN,    TK_NOT
3ea2d 4e 55 4c 4c 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c  NULL,    TK_NOT,
3ea2e 20 20 20 20 20 20 20 20 54 4b 5f 4e 4f 2c 20 20          TK_NO,  
3ea2f 20 20 20 20 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20         TK_NULL, 
3ea30 20 20 20 20 20 20 0d 0a 20 20 20 20 54 4b 5f 4c        ..    TK_L
3ea31 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41  IKE_KW,    TK_CA
3ea32 53 43 41 44 45 2c 20 20 20 20 54 4b 5f 41 53 43  SCADE,    TK_ASC
3ea33 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45 4c 45  ,        TK_DELE
3ea34 54 45 2c 20 20 20 20 20 54 4b 5f 43 41 53 45 2c  TE,     TK_CASE,
3ea35 20 20 20 20 20 20 20 0d 0a 20 20 20 20 54 4b 5f         ..    TK_
3ea36 43 4f 4c 4c 41 54 45 2c 20 20 20 20 54 4b 5f 43  COLLATE,    TK_C
3ea37 52 45 41 54 45 2c 20 20 20 20 20 54 4b 5f 43 54  REATE,     TK_CT
3ea38 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 44 45 54  IME_KW,   TK_DET
3ea39 41 43 48 2c 20 20 20 20 20 54 4b 5f 49 4d 4d 45  ACH,     TK_IMME
3ea3a 44 49 41 54 45 2c 20 20 0d 0a 20 20 20 20 54 4b  DIATE,  ..    TK
3ea3b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _JOIN,       TK_
3ea3c 49 4e 53 45 52 54 2c 20 20 20 20 20 54 4b 5f 4d  INSERT,     TK_M
3ea3d 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f 50 4c  ATCH,      TK_PL
3ea3e 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f 41 4e 41  AN,       TK_ANA
3ea3f 4c 59 5a 45 2c 20 20 20 20 0d 0a 20 20 20 20 54  LYZE,    ..    T
3ea40 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20 20 54 4b  K_PRAGMA,     TK
3ea41 5f 41 42 4f 52 54 2c 20 20 20 20 20 20 54 4b 5f  _ABORT,      TK_
3ea42 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f 56  VALUES,     TK_V
3ea43 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f 4c 49  IRTUAL,    TK_LI
3ea44 4d 49 54 2c 20 20 20 20 20 20 0d 0a 20 20 20 20  MIT,      ..    
3ea45 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20 20 54  TK_WHEN,       T
3ea46 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20 54 4b  K_WHERE,      TK
3ea47 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f  _RENAME,     TK_
3ea48 41 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f 52  AFTER,      TK_R
3ea49 45 50 4c 41 43 45 2c 20 20 20 20 0d 0a 20 20 20  EPLACE,    ..   
3ea4a 20 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20 20   TK_AND,        
3ea4b 54 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20 54  TK_DEFAULT,    T
3ea4c 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b  K_AUTOINCR,   TK
3ea4d 5f 54 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _TO,         TK_
3ea4e 49 4e 2c 20 20 20 20 20 20 20 20 20 0d 0a 20 20  IN,         ..  
3ea4f 20 20 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20    TK_CAST,      
3ea50 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20   TK_COLUMNKW,   
3ea51 54 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54  TK_COMMIT,     T
3ea52 4b 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b  K_CONFLICT,   TK
3ea53 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0d 0a 20  _JOIN_KW,    .. 
3ea54 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20     TK_CTIME_KW, 
3ea55 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20    TK_CTIME_KW,  
3ea56 20 54 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20 20   TK_PRIMARY,    
3ea57 54 4b 5f 44 45 46 45 52 52 45 44 2c 20 20 20 54  TK_DEFERRED,   T
3ea58 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20 0d 0a  K_DISTINCT,   ..
3ea59 20 20 20 20 54 4b 5f 49 53 2c 20 20 20 20 20 20      TK_IS,      
3ea5a 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20 20     TK_DROP,     
3ea5b 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20    TK_FAIL,      
3ea5c 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20   TK_FROM,       
3ea5d 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0d  TK_JOIN_KW,    .
3ea5e 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c  .    TK_LIKE_KW,
3ea5f 20 20 20 20 54 4b 5f 42 59 2c 20 20 20 20 20 20      TK_BY,      
3ea60 20 20 20 54 4b 5f 49 46 2c 20 20 20 20 20 20 20     TK_IF,       
3ea61 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20 20 20    TK_ISNULL,    
3ea62 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20 20 20 20   TK_ORDER,      
3ea63 0d 0a 20 20 20 20 54 4b 5f 52 45 53 54 52 49 43  ..    TK_RESTRIC
3ea64 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c  T,   TK_JOIN_KW,
3ea65 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
3ea66 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20     TK_ROLLBACK, 
3ea67 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20 20 20 20    TK_ROW,       
3ea68 20 0d 0a 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c   ..    TK_UNION,
3ea69 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20        TK_USING, 
3ea6a 20 20 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20       TK_VACUUM, 
3ea6b 20 20 20 20 54 4b 5f 56 49 45 57 2c 20 20 20 20      TK_VIEW,    
3ea6c 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c     TK_INITIALLY,
3ea6d 20 20 0d 0a 20 20 20 20 54 4b 5f 41 4c 4c 2c 20    ..    TK_ALL, 
3ea6e 20 20 20 20 20 20 20 0d 0a 20 20 7d 3b 0d 0a 20         ..  };.. 
3ea6f 20 69 6e 74 20 68 2c 20 69 3b 0d 0a 20 20 69 66   int h, i;..  if
3ea70 28 20 6e 3c 32 20 29 20 72 65 74 75 72 6e 20 54  ( n<2 ) return T
3ea71 4b 5f 49 44 3b 0d 0a 20 20 68 20 3d 20 28 28 63  K_ID;..  h = ((c
3ea72 68 61 72 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20  harMap(z[0])*4) 
3ea73 5e 0d 0a 20 20 20 20 20 20 28 63 68 61 72 4d 61  ^..      (charMa
3ea74 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20 5e 0d 0a  p(z[n-1])*3) ^..
3ea75 20 20 20 20 20 20 6e 29 20 25 20 31 32 37 3b 0d        n) % 127;.
3ea76 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74 29 61  .  for(i=((int)a
3ea77 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d 30  Hash[h])-1; i>=0
3ea78 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78 74 5b  ; i=((int)aNext[
3ea79 69 5d 29 2d 31 29 7b 0d 0a 20 20 20 20 69 66 28  i])-1){..    if(
3ea7a 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73   aLen[i]==n && s
3ea7b 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 26  qlite3StrNICmp(&
3ea7c 7a 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69 5d  zText[aOffset[i]
3ea7d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0d 0a 20 20  ],z,n)==0 ){..  
3ea7e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3ea7f 3d 30 20 29 3b 20 2f 2a 20 52 45 49 4e 44 45 58  =0 ); /* REINDEX
3ea80 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63   */..      testc
3ea81 61 73 65 28 20 69 3d 3d 31 20 29 3b 20 2f 2a 20  ase( i==1 ); /* 
3ea82 49 4e 44 45 58 45 44 20 2a 2f 0d 0a 20 20 20 20  INDEXED */..    
3ea83 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
3ea84 20 29 3b 20 2f 2a 20 49 4e 44 45 58 20 2a 2f 0d   ); /* INDEX */.
3ea85 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3ea86 20 69 3d 3d 33 20 29 3b 20 2f 2a 20 44 45 53 43   i==3 ); /* DESC
3ea87 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63   */..      testc
3ea88 61 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a 20  ase( i==4 ); /* 
3ea89 45 53 43 41 50 45 20 2a 2f 0d 0a 20 20 20 20 20  ESCAPE */..     
3ea8a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 20   testcase( i==5 
3ea8b 29 3b 20 2f 2a 20 45 41 43 48 20 2a 2f 0d 0a 20  ); /* EACH */.. 
3ea8c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3ea8d 3d 3d 36 20 29 3b 20 2f 2a 20 43 48 45 43 4b 20  ==6 ); /* CHECK 
3ea8e 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3ea8f 73 65 28 20 69 3d 3d 37 20 29 3b 20 2f 2a 20 4b  se( i==7 ); /* K
3ea90 45 59 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  EY */..      tes
3ea91 74 63 61 73 65 28 20 69 3d 3d 38 20 29 3b 20 2f  tcase( i==8 ); /
3ea92 2a 20 42 45 46 4f 52 45 20 2a 2f 0d 0a 20 20 20  * BEFORE */..   
3ea93 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3ea94 39 20 29 3b 20 2f 2a 20 46 4f 52 45 49 47 4e 20  9 ); /* FOREIGN 
3ea95 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3ea96 73 65 28 20 69 3d 3d 31 30 20 29 3b 20 2f 2a 20  se( i==10 ); /* 
3ea97 46 4f 52 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  FOR */..      te
3ea98 73 74 63 61 73 65 28 20 69 3d 3d 31 31 20 29 3b  stcase( i==11 );
3ea99 20 2f 2a 20 49 47 4e 4f 52 45 20 2a 2f 0d 0a 20   /* IGNORE */.. 
3ea9a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3ea9b 3d 3d 31 32 20 29 3b 20 2f 2a 20 52 45 47 45 58  ==12 ); /* REGEX
3ea9c 50 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  P */..      test
3ea9d 63 61 73 65 28 20 69 3d 3d 31 33 20 29 3b 20 2f  case( i==13 ); /
3ea9e 2a 20 45 58 50 4c 41 49 4e 20 2a 2f 0d 0a 20 20  * EXPLAIN */..  
3ea9f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eaa0 3d 31 34 20 29 3b 20 2f 2a 20 49 4e 53 54 45 41  =14 ); /* INSTEA
3eaa1 44 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  D */..      test
3eaa2 63 61 73 65 28 20 69 3d 3d 31 35 20 29 3b 20 2f  case( i==15 ); /
3eaa3 2a 20 41 44 44 20 2a 2f 0d 0a 20 20 20 20 20 20  * ADD */..      
3eaa4 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 36 20  testcase( i==16 
3eaa5 29 3b 20 2f 2a 20 44 41 54 41 42 41 53 45 20 2a  ); /* DATABASE *
3eaa6 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3eaa7 65 28 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20 41  e( i==17 ); /* A
3eaa8 53 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  S */..      test
3eaa9 63 61 73 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f  case( i==18 ); /
3eaaa 2a 20 53 45 4c 45 43 54 20 2a 2f 0d 0a 20 20 20  * SELECT */..   
3eaab 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eaac 31 39 20 29 3b 20 2f 2a 20 54 41 42 4c 45 20 2a  19 ); /* TABLE *
3eaad 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3eaae 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a 20 4c  e( i==20 ); /* L
3eaaf 45 46 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  EFT */..      te
3eab0 73 74 63 61 73 65 28 20 69 3d 3d 32 31 20 29 3b  stcase( i==21 );
3eab1 20 2f 2a 20 54 48 45 4e 20 2a 2f 0d 0a 20 20 20   /* THEN */..   
3eab2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eab3 32 32 20 29 3b 20 2f 2a 20 45 4e 44 20 2a 2f 0d  22 ); /* END */.
3eab4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eab5 20 69 3d 3d 32 33 20 29 3b 20 2f 2a 20 44 45 46   i==23 ); /* DEF
3eab6 45 52 52 41 42 4c 45 20 2a 2f 0d 0a 20 20 20 20  ERRABLE */..    
3eab7 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
3eab8 34 20 29 3b 20 2f 2a 20 45 4c 53 45 20 2a 2f 0d  4 ); /* ELSE */.
3eab9 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eaba 20 69 3d 3d 32 35 20 29 3b 20 2f 2a 20 45 58 43   i==25 ); /* EXC
3eabb 45 50 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  EPT */..      te
3eabc 73 74 63 61 73 65 28 20 69 3d 3d 32 36 20 29 3b  stcase( i==26 );
3eabd 20 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20   /* TRANSACTION 
3eabe 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3eabf 73 65 28 20 69 3d 3d 32 37 20 29 3b 20 2f 2a 20  se( i==27 ); /* 
3eac0 41 43 54 49 4f 4e 20 2a 2f 0d 0a 20 20 20 20 20  ACTION */..     
3eac1 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 38   testcase( i==28
3eac2 20 29 3b 20 2f 2a 20 4f 4e 20 2a 2f 0d 0a 20 20   ); /* ON */..  
3eac3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eac4 3d 32 39 20 29 3b 20 2f 2a 20 4e 41 54 55 52 41  =29 ); /* NATURA
3eac5 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  L */..      test
3eac6 63 61 73 65 28 20 69 3d 3d 33 30 20 29 3b 20 2f  case( i==30 ); /
3eac7 2a 20 41 4c 54 45 52 20 2a 2f 0d 0a 20 20 20 20  * ALTER */..    
3eac8 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
3eac9 31 20 29 3b 20 2f 2a 20 52 41 49 53 45 20 2a 2f  1 ); /* RAISE */
3eaca 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eacb 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 45 58  ( i==32 ); /* EX
3eacc 43 4c 55 53 49 56 45 20 2a 2f 0d 0a 20 20 20 20  CLUSIVE */..    
3eacd 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
3eace 33 20 29 3b 20 2f 2a 20 45 58 49 53 54 53 20 2a  3 ); /* EXISTS *
3eacf 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3ead0 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 53  e( i==34 ); /* S
3ead1 41 56 45 50 4f 49 4e 54 20 2a 2f 0d 0a 20 20 20  AVEPOINT */..   
3ead2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3ead3 33 35 20 29 3b 20 2f 2a 20 49 4e 54 45 52 53 45  35 ); /* INTERSE
3ead4 43 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  CT */..      tes
3ead5 74 63 61 73 65 28 20 69 3d 3d 33 36 20 29 3b 20  tcase( i==36 ); 
3ead6 2f 2a 20 54 52 49 47 47 45 52 20 2a 2f 0d 0a 20  /* TRIGGER */.. 
3ead7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3ead8 3d 3d 33 37 20 29 3b 20 2f 2a 20 52 45 46 45 52  ==37 ); /* REFER
3ead9 45 4e 43 45 53 20 2a 2f 0d 0a 20 20 20 20 20 20  ENCES */..      
3eada 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 38 20  testcase( i==38 
3eadb 29 3b 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54  ); /* CONSTRAINT
3eadc 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63   */..      testc
3eadd 61 73 65 28 20 69 3d 3d 33 39 20 29 3b 20 2f 2a  ase( i==39 ); /*
3eade 20 49 4e 54 4f 20 2a 2f 0d 0a 20 20 20 20 20 20   INTO */..      
3eadf 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 30 20  testcase( i==40 
3eae0 29 3b 20 2f 2a 20 4f 46 46 53 45 54 20 2a 2f 0d  ); /* OFFSET */.
3eae1 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eae2 20 69 3d 3d 34 31 20 29 3b 20 2f 2a 20 4f 46 20   i==41 ); /* OF 
3eae3 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3eae4 73 65 28 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20  se( i==42 ); /* 
3eae5 53 45 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  SET */..      te
3eae6 73 74 63 61 73 65 28 20 69 3d 3d 34 33 20 29 3b  stcase( i==43 );
3eae7 20 2f 2a 20 54 45 4d 50 4f 52 41 52 59 20 2a 2f   /* TEMPORARY */
3eae8 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eae9 28 20 69 3d 3d 34 34 20 29 3b 20 2f 2a 20 54 45  ( i==44 ); /* TE
3eaea 4d 50 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  MP */..      tes
3eaeb 74 63 61 73 65 28 20 69 3d 3d 34 35 20 29 3b 20  tcase( i==45 ); 
3eaec 2f 2a 20 4f 52 20 2a 2f 0d 0a 20 20 20 20 20 20  /* OR */..      
3eaed 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 36 20  testcase( i==46 
3eaee 29 3b 20 2f 2a 20 55 4e 49 51 55 45 20 2a 2f 0d  ); /* UNIQUE */.
3eaef 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eaf0 20 69 3d 3d 34 37 20 29 3b 20 2f 2a 20 51 55 45   i==47 ); /* QUE
3eaf1 52 59 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  RY */..      tes
3eaf2 74 63 61 73 65 28 20 69 3d 3d 34 38 20 29 3b 20  tcase( i==48 ); 
3eaf3 2f 2a 20 41 54 54 41 43 48 20 2a 2f 0d 0a 20 20  /* ATTACH */..  
3eaf4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eaf5 3d 34 39 20 29 3b 20 2f 2a 20 48 41 56 49 4e 47  =49 ); /* HAVING
3eaf6 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63   */..      testc
3eaf7 61 73 65 28 20 69 3d 3d 35 30 20 29 3b 20 2f 2a  ase( i==50 ); /*
3eaf8 20 47 52 4f 55 50 20 2a 2f 0d 0a 20 20 20 20 20   GROUP */..     
3eaf9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 31   testcase( i==51
3eafa 20 29 3b 20 2f 2a 20 55 50 44 41 54 45 20 2a 2f   ); /* UPDATE */
3eafb 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eafc 28 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 42 45  ( i==52 ); /* BE
3eafd 47 49 4e 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  GIN */..      te
3eafe 73 74 63 61 73 65 28 20 69 3d 3d 35 33 20 29 3b  stcase( i==53 );
3eaff 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f 0d 0a 20 20   /* INNER */..  
3eb00 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb01 3d 35 34 20 29 3b 20 2f 2a 20 52 45 4c 45 41 53  =54 ); /* RELEAS
3eb02 45 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  E */..      test
3eb03 63 61 73 65 28 20 69 3d 3d 35 35 20 29 3b 20 2f  case( i==55 ); /
3eb04 2a 20 42 45 54 57 45 45 4e 20 2a 2f 0d 0a 20 20  * BETWEEN */..  
3eb05 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb06 3d 35 36 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55 4c  =56 ); /* NOTNUL
3eb07 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  L */..      test
3eb08 63 61 73 65 28 20 69 3d 3d 35 37 20 29 3b 20 2f  case( i==57 ); /
3eb09 2a 20 4e 4f 54 20 2a 2f 0d 0a 20 20 20 20 20 20  * NOT */..      
3eb0a 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 38 20  testcase( i==58 
3eb0b 29 3b 20 2f 2a 20 4e 4f 20 2a 2f 0d 0a 20 20 20  ); /* NO */..   
3eb0c 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eb0d 35 39 20 29 3b 20 2f 2a 20 4e 55 4c 4c 20 2a 2f  59 ); /* NULL */
3eb0e 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb0f 28 20 69 3d 3d 36 30 20 29 3b 20 2f 2a 20 4c 49  ( i==60 ); /* LI
3eb10 4b 45 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  KE */..      tes
3eb11 74 63 61 73 65 28 20 69 3d 3d 36 31 20 29 3b 20  tcase( i==61 ); 
3eb12 2f 2a 20 43 41 53 43 41 44 45 20 2a 2f 0d 0a 20  /* CASCADE */.. 
3eb13 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb14 3d 3d 36 32 20 29 3b 20 2f 2a 20 41 53 43 20 2a  ==62 ); /* ASC *
3eb15 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3eb16 65 28 20 69 3d 3d 36 33 20 29 3b 20 2f 2a 20 44  e( i==63 ); /* D
3eb17 45 4c 45 54 45 20 2a 2f 0d 0a 20 20 20 20 20 20  ELETE */..      
3eb18 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20  testcase( i==64 
3eb19 29 3b 20 2f 2a 20 43 41 53 45 20 2a 2f 0d 0a 20  ); /* CASE */.. 
3eb1a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb1b 3d 3d 36 35 20 29 3b 20 2f 2a 20 43 4f 4c 4c 41  ==65 ); /* COLLA
3eb1c 54 45 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  TE */..      tes
3eb1d 74 63 61 73 65 28 20 69 3d 3d 36 36 20 29 3b 20  tcase( i==66 ); 
3eb1e 2f 2a 20 43 52 45 41 54 45 20 2a 2f 0d 0a 20 20  /* CREATE */..  
3eb1f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb20 3d 36 37 20 29 3b 20 2f 2a 20 43 55 52 52 45 4e  =67 ); /* CURREN
3eb21 54 5f 44 41 54 45 20 2a 2f 0d 0a 20 20 20 20 20  T_DATE */..     
3eb22 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 38   testcase( i==68
3eb23 20 29 3b 20 2f 2a 20 44 45 54 41 43 48 20 2a 2f   ); /* DETACH */
3eb24 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb25 28 20 69 3d 3d 36 39 20 29 3b 20 2f 2a 20 49 4d  ( i==69 ); /* IM
3eb26 4d 45 44 49 41 54 45 20 2a 2f 0d 0a 20 20 20 20  MEDIATE */..    
3eb27 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
3eb28 30 20 29 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0d  0 ); /* JOIN */.
3eb29 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eb2a 20 69 3d 3d 37 31 20 29 3b 20 2f 2a 20 49 4e 53   i==71 ); /* INS
3eb2b 45 52 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  ERT */..      te
3eb2c 73 74 63 61 73 65 28 20 69 3d 3d 37 32 20 29 3b  stcase( i==72 );
3eb2d 20 2f 2a 20 4d 41 54 43 48 20 2a 2f 0d 0a 20 20   /* MATCH */..  
3eb2e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb2f 3d 37 33 20 29 3b 20 2f 2a 20 50 4c 41 4e 20 2a  =73 ); /* PLAN *
3eb30 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3eb31 65 28 20 69 3d 3d 37 34 20 29 3b 20 2f 2a 20 41  e( i==74 ); /* A
3eb32 4e 41 4c 59 5a 45 20 2a 2f 0d 0a 20 20 20 20 20  NALYZE */..     
3eb33 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 35   testcase( i==75
3eb34 20 29 3b 20 2f 2a 20 50 52 41 47 4d 41 20 2a 2f   ); /* PRAGMA */
3eb35 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb36 28 20 69 3d 3d 37 36 20 29 3b 20 2f 2a 20 41 42  ( i==76 ); /* AB
3eb37 4f 52 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  ORT */..      te
3eb38 73 74 63 61 73 65 28 20 69 3d 3d 37 37 20 29 3b  stcase( i==77 );
3eb39 20 2f 2a 20 56 41 4c 55 45 53 20 2a 2f 0d 0a 20   /* VALUES */.. 
3eb3a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb3b 3d 3d 37 38 20 29 3b 20 2f 2a 20 56 49 52 54 55  ==78 ); /* VIRTU
3eb3c 41 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  AL */..      tes
3eb3d 74 63 61 73 65 28 20 69 3d 3d 37 39 20 29 3b 20  tcase( i==79 ); 
3eb3e 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0d 0a 20 20 20  /* LIMIT */..   
3eb3f 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eb40 38 30 20 29 3b 20 2f 2a 20 57 48 45 4e 20 2a 2f  80 ); /* WHEN */
3eb41 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb42 28 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 57 48  ( i==81 ); /* WH
3eb43 45 52 45 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  ERE */..      te
3eb44 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b  stcase( i==82 );
3eb45 20 2f 2a 20 52 45 4e 41 4d 45 20 2a 2f 0d 0a 20   /* RENAME */.. 
3eb46 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb47 3d 3d 38 33 20 29 3b 20 2f 2a 20 41 46 54 45 52  ==83 ); /* AFTER
3eb48 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63   */..      testc
3eb49 61 73 65 28 20 69 3d 3d 38 34 20 29 3b 20 2f 2a  ase( i==84 ); /*
3eb4a 20 52 45 50 4c 41 43 45 20 2a 2f 0d 0a 20 20 20   REPLACE */..   
3eb4b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eb4c 38 35 20 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f 0d  85 ); /* AND */.
3eb4d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eb4e 20 69 3d 3d 38 36 20 29 3b 20 2f 2a 20 44 45 46   i==86 ); /* DEF
3eb4f 41 55 4c 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74  AULT */..      t
3eb50 65 73 74 63 61 73 65 28 20 69 3d 3d 38 37 20 29  estcase( i==87 )
3eb51 3b 20 2f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45  ; /* AUTOINCREME
3eb52 4e 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73  NT */..      tes
3eb53 74 63 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20  tcase( i==88 ); 
3eb54 2f 2a 20 54 4f 20 2a 2f 0d 0a 20 20 20 20 20 20  /* TO */..      
3eb55 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 39 20  testcase( i==89 
3eb56 29 3b 20 2f 2a 20 49 4e 20 2a 2f 0d 0a 20 20 20  ); /* IN */..   
3eb57 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3eb58 39 30 20 29 3b 20 2f 2a 20 43 41 53 54 20 2a 2f  90 ); /* CAST */
3eb59 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb5a 28 20 69 3d 3d 39 31 20 29 3b 20 2f 2a 20 43 4f  ( i==91 ); /* CO
3eb5b 4c 55 4d 4e 20 2a 2f 0d 0a 20 20 20 20 20 20 74  LUMN */..      t
3eb5c 65 73 74 63 61 73 65 28 20 69 3d 3d 39 32 20 29  estcase( i==92 )
3eb5d 3b 20 2f 2a 20 43 4f 4d 4d 49 54 20 2a 2f 0d 0a  ; /* COMMIT */..
3eb5e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3eb5f 69 3d 3d 39 33 20 29 3b 20 2f 2a 20 43 4f 4e 46  i==93 ); /* CONF
3eb60 4c 49 43 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74  LICT */..      t
3eb61 65 73 74 63 61 73 65 28 20 69 3d 3d 39 34 20 29  estcase( i==94 )
3eb62 3b 20 2f 2a 20 43 52 4f 53 53 20 2a 2f 0d 0a 20  ; /* CROSS */.. 
3eb63 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb64 3d 3d 39 35 20 29 3b 20 2f 2a 20 43 55 52 52 45  ==95 ); /* CURRE
3eb65 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 2a 2f 0d  NT_TIMESTAMP */.
3eb66 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eb67 20 69 3d 3d 39 36 20 29 3b 20 2f 2a 20 43 55 52   i==96 ); /* CUR
3eb68 52 45 4e 54 5f 54 49 4d 45 20 2a 2f 0d 0a 20 20  RENT_TIME */..  
3eb69 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb6a 3d 39 37 20 29 3b 20 2f 2a 20 50 52 49 4d 41 52  =97 ); /* PRIMAR
3eb6b 59 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  Y */..      test
3eb6c 63 61 73 65 28 20 69 3d 3d 39 38 20 29 3b 20 2f  case( i==98 ); /
3eb6d 2a 20 44 45 46 45 52 52 45 44 20 2a 2f 0d 0a 20  * DEFERRED */.. 
3eb6e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb6f 3d 3d 39 39 20 29 3b 20 2f 2a 20 44 49 53 54 49  ==99 ); /* DISTI
3eb70 4e 43 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  NCT */..      te
3eb71 73 74 63 61 73 65 28 20 69 3d 3d 31 30 30 20 29  stcase( i==100 )
3eb72 3b 20 2f 2a 20 49 53 20 2a 2f 0d 0a 20 20 20 20  ; /* IS */..    
3eb73 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
3eb74 30 31 20 29 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f  01 ); /* DROP */
3eb75 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb76 28 20 69 3d 3d 31 30 32 20 29 3b 20 2f 2a 20 46  ( i==102 ); /* F
3eb77 41 49 4c 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  AIL */..      te
3eb78 73 74 63 61 73 65 28 20 69 3d 3d 31 30 33 20 29  stcase( i==103 )
3eb79 3b 20 2f 2a 20 46 52 4f 4d 20 2a 2f 0d 0a 20 20  ; /* FROM */..  
3eb7a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb7b 3d 31 30 34 20 29 3b 20 2f 2a 20 46 55 4c 4c 20  =104 ); /* FULL 
3eb7c 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3eb7d 73 65 28 20 69 3d 3d 31 30 35 20 29 3b 20 2f 2a  se( i==105 ); /*
3eb7e 20 47 4c 4f 42 20 2a 2f 0d 0a 20 20 20 20 20 20   GLOB */..      
3eb7f 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 36  testcase( i==106
3eb80 20 29 3b 20 2f 2a 20 42 59 20 2a 2f 0d 0a 20 20   ); /* BY */..  
3eb81 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb82 3d 31 30 37 20 29 3b 20 2f 2a 20 49 46 20 2a 2f  =107 ); /* IF */
3eb83 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb84 28 20 69 3d 3d 31 30 38 20 29 3b 20 2f 2a 20 49  ( i==108 ); /* I
3eb85 53 4e 55 4c 4c 20 2a 2f 0d 0a 20 20 20 20 20 20  SNULL */..      
3eb86 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 39  testcase( i==109
3eb87 20 29 3b 20 2f 2a 20 4f 52 44 45 52 20 2a 2f 0d   ); /* ORDER */.
3eb88 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3eb89 20 69 3d 3d 31 31 30 20 29 3b 20 2f 2a 20 52 45   i==110 ); /* RE
3eb8a 53 54 52 49 43 54 20 2a 2f 0d 0a 20 20 20 20 20  STRICT */..     
3eb8b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
3eb8c 31 20 29 3b 20 2f 2a 20 4f 55 54 45 52 20 2a 2f  1 ); /* OUTER */
3eb8d 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3eb8e 28 20 69 3d 3d 31 31 32 20 29 3b 20 2f 2a 20 52  ( i==112 ); /* R
3eb8f 49 47 48 54 20 2a 2f 0d 0a 20 20 20 20 20 20 74  IGHT */..      t
3eb90 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 33 20  estcase( i==113 
3eb91 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b 20 2a  ); /* ROLLBACK *
3eb92 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  /..      testcas
3eb93 65 28 20 69 3d 3d 31 31 34 20 29 3b 20 2f 2a 20  e( i==114 ); /* 
3eb94 52 4f 57 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65  ROW */..      te
3eb95 73 74 63 61 73 65 28 20 69 3d 3d 31 31 35 20 29  stcase( i==115 )
3eb96 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0d 0a 20  ; /* UNION */.. 
3eb97 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eb98 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 55 53 49 4e  ==116 ); /* USIN
3eb99 47 20 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74  G */..      test
3eb9a 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29 3b 20  case( i==117 ); 
3eb9b 2f 2a 20 56 41 43 55 55 4d 20 2a 2f 0d 0a 20 20  /* VACUUM */..  
3eb9c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3eb9d 3d 31 31 38 20 29 3b 20 2f 2a 20 56 49 45 57 20  =118 ); /* VIEW 
3eb9e 2a 2f 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
3eb9f 73 65 28 20 69 3d 3d 31 31 39 20 29 3b 20 2f 2a  se( i==119 ); /*
3eba0 20 49 4e 49 54 49 41 4c 4c 59 20 2a 2f 0d 0a 20   INITIALLY */.. 
3eba1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3eba2 3d 3d 31 32 30 20 29 3b 20 2f 2a 20 41 4c 4c 20  ==120 ); /* ALL 
3eba3 2a 2f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  */..      return
3eba4 20 61 43 6f 64 65 5b 69 5d 3b 0d 0a 20 20 20 20   aCode[i];..    
3eba5 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  }..  }..  return
3eba6 20 54 4b 5f 49 44 3b 0d 0a 7d 0d 0a 53 51 4c 49   TK_ID;..}..SQLI
3eba7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3eba8 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64  qlite3KeywordCod
3eba9 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  e(const unsigned
3ebaa 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
3ebab 7b 0d 0a 20 20 72 65 74 75 72 6e 20 6b 65 79 77  {..  return keyw
3ebac 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29 7a  ordCode((char*)z
3ebad 2c 20 6e 29 3b 0d 0a 7d 0d 0a 23 64 65 66 69 6e  , n);..}..#defin
3ebae 65 20 53 51 4c 49 54 45 5f 4e 5f 4b 45 59 57 4f  e SQLITE_N_KEYWO
3ebaf 52 44 20 31 32 31 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  RD 121..../*****
3ebb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
3ebb1 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a   keywordhash.h *
3ebb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ebb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ebb4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
3ebb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
3ebb6 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
3ebb7 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e  eft off in token
3ebb8 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
3ebb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 0d 0a  *********/......
3ebba 2f 2a 0d 0a 2a 2a 20 49 66 20 58 20 69 73 20 61  /*..** If X is a
3ebbb 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 20   character that 
3ebbc 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
3ebbd 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68 65  n identifier the
3ebbe 6e 0d 0a 2a 2a 20 49 64 43 68 61 72 28 58 29 20  n..** IdChar(X) 
3ebbf 77 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f  will be true.  O
3ebc0 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66  therwise it is f
3ebc1 61 6c 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6f  alse...**..** Fo
3ebc2 72 20 41 53 43 49 49 2c 20 61 6e 79 20 63 68 61  r ASCII, any cha
3ebc3 72 61 63 74 65 72 20 77 69 74 68 20 74 68 65 20  racter with the 
3ebc4 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 73  high-order bit s
3ebc5 65 74 20 69 73 0d 0a 2a 2a 20 61 6c 6c 6f 77 65  et is..** allowe
3ebc6 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69  d in an identifi
3ebc7 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74 20 63  er.  For 7-bit c
3ebc8 68 61 72 61 63 74 65 72 73 2c 20 0d 0a 2a 2a 20  haracters, ..** 
3ebc9 73 71 6c 69 74 65 33 49 73 49 64 43 68 61 72 5b  sqlite3IsIdChar[
3ebca 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0d 0a 2a  X] must be 1...*
3ebcb 2a 0d 0a 2a 2a 20 46 6f 72 20 45 42 43 44 49 43  *..** For EBCDIC
3ebcc 2c 20 74 68 65 20 72 75 6c 65 73 20 61 72 65 20  , the rules are 
3ebcd 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 62 75 74  more complex but
3ebce 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 0d 0a   have the same..
3ebcf 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74 2e 0d 0a  ** end result...
3ebd0 2a 2a 0d 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31  **..** Ticket #1
3ebd1 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74  066.  the SQL st
3ebd2 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20  andard does not 
3ebd3 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65  allow '$' in the
3ebd4 0d 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69  ..** middle of i
3ebd5 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20  dentfiers.  But 
3ebd6 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65  many SQL impleme
3ebd7 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0d 0a 2a  ntations do. ..*
3ebd8 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  * SQLite will al
3ebd9 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74  low '$' in ident
3ebda 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61  ifiers for compa
3ebdb 74 69 62 69 6c 69 74 79 2e 0d 0a 2a 2a 20 42 75  tibility...** Bu
3ebdc 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73  t the feature is
3ebdd 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0d 0a   undocumented...
3ebde 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
3ebdf 45 5f 41 53 43 49 49 0d 0a 23 64 65 66 69 6e 65  E_ASCII..#define
3ebe0 20 49 64 43 68 61 72 28 43 29 20 20 28 28 73 71   IdChar(C)  ((sq
3ebe1 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75  lite3CtypeMap[(u
3ebe2 6e 73 69 67 6e 65 64 20 63 68 61 72 29 43 5d 26  nsigned char)C]&
3ebe3 30 78 34 36 29 21 3d 30 29 0d 0a 23 65 6e 64 69  0x46)!=0)..#endi
3ebe4 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
3ebe5 5f 45 42 43 44 49 43 0d 0a 53 51 4c 49 54 45 5f  _EBCDIC..SQLITE_
3ebe6 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
3ebe7 61 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  ar sqlite3IsEbcd
3ebe8 69 63 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0d 0a  icIdChar[] = {..
3ebe9 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78  /* x0 x1 x2 x3 x
3ebea 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39  4 x5 x6 x7 x8 x9
3ebeb 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20   xA xB xC xD xE 
3ebec 78 46 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 30 2c  xF */..    0, 0,
3ebed 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3ebee 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
3ebef 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34  , 0, 0, 0,  /* 4
3ebf0 78 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 31 2c 20  x */..    0, 1, 
3ebf1 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3ebf2 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c  , 1, 1, 0, 1, 0,
3ebf3 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 35 78   0, 0, 0,  /* 5x
3ebf4 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 30 2c 20 31   */..    0, 0, 1
3ebf5 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3ebf6 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
3ebf7 31 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 36 78 20  1, 0, 0,  /* 6x 
3ebf8 2a 2f 0d 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c  */..    0, 1, 1,
3ebf9 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3ebfa 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
3ebfb 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
3ebfc 2f 0d 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  /..    0, 1, 1, 
3ebfd 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3ebfe 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 0, 0, 1, 1,
3ebff 20 31 2c 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f   1, 0,  /* 8x */
3ec00 0d 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  ..    0, 1, 1, 1
3ec01 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3ec02 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   1, 0, 0, 1, 0, 
3ec03 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0d  1, 0,  /* 9x */.
3ec04 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 31 2c  .    1, 0, 1, 1,
3ec05 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3ec06 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
3ec07 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0d 0a  , 0,  /* Ax */..
3ec08 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
3ec09 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
3ec0a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
3ec0b 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0d 0a 20   0,  /* Bx */.. 
3ec0c 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
3ec0d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3ec0e 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
3ec0f 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0d 0a 20 20  1,  /* Cx */..  
3ec10 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
3ec11 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3ec12 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
3ec13 2c 20 20 2f 2a 20 44 78 20 2a 2f 0d 0a 20 20 20  ,  /* Dx */..   
3ec14 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 0, 1, 1, 1, 
3ec15 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
3ec16 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3ec17 20 20 2f 2a 20 45 78 20 2a 2f 0d 0a 20 20 20 20    /* Ex */..    
3ec18 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3ec19 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
3ec1a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
3ec1b 20 2f 2a 20 46 78 20 2a 2f 0d 0a 7d 3b 0d 0a 23   /* Fx */..};..#
3ec1c 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
3ec1d 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20    (((c=C)>=0x42 
3ec1e 26 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  && sqlite3IsEbcd
3ec1f 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d  icIdChar[c-0x40]
3ec20 29 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a  ))..#endif......
3ec21 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
3ec22 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
3ec23 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e  token that begin
3ec24 73 20 61 74 20 7a 5b 30 5d 2e 20 0d 0a 2a 2a 20  s at z[0]. ..** 
3ec25 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20  Store the token 
3ec26 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79  type in *tokenTy
3ec27 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  pe before return
3ec28 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ing...*/..SQLITE
3ec29 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3ec2a 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e  ite3GetToken(con
3ec2b 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
3ec2c 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54   *z, int *tokenT
3ec2d 79 70 65 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20  ype){..  int i, 
3ec2e 63 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 2a 7a  c;..  switch( *z
3ec2f 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 27 20   ){..    case ' 
3ec30 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61  ': case '\t': ca
3ec31 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c  se '\n': case '\
3ec32 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b  f': case '\r': {
3ec33 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
3ec34 28 20 7a 5b 30 5d 3d 3d 27 20 27 20 29 3b 0d 0a  ( z[0]==' ' );..
3ec35 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3ec36 7a 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b 0d 0a 20  z[0]=='\t' );.. 
3ec37 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
3ec38 5b 30 5d 3d 3d 27 5c 6e 27 20 29 3b 0d 0a 20 20  [0]=='\n' );..  
3ec39 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b      testcase( z[
3ec3a 30 5d 3d 3d 27 5c 66 27 20 29 3b 0d 0a 20 20 20  0]=='\f' );..   
3ec3b 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
3ec3c 5d 3d 3d 27 5c 72 27 20 29 3b 0d 0a 20 20 20 20  ]=='\r' );..    
3ec3d 20 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74    for(i=1; sqlit
3ec3e 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e3Isspace(z[i]);
3ec3f 20 69 2b 2b 29 7b 7d 0d 0a 20 20 20 20 20 20 2a   i++){}..      *
3ec40 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
3ec41 50 41 43 45 3b 0d 0a 20 20 20 20 20 20 72 65 74  PACE;..      ret
3ec42 75 72 6e 20 69 3b 0d 0a 20 20 20 20 7d 0d 0a 20  urn i;..    }.. 
3ec43 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 0d 0a     case '-': {..
3ec44 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d 3d        if( z[1]==
3ec45 27 2d 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  '-' ){..        
3ec46 2f 2a 20 49 4d 50 3a 20 52 2d 35 30 34 31 37 2d  /* IMP: R-50417-
3ec47 32 37 39 37 36 20 2d 2d 20 73 79 6e 74 61 78 20  27976 -- syntax 
3ec48 64 69 61 67 72 61 6d 20 66 6f 72 20 63 6f 6d 6d  diagram for comm
3ec49 65 6e 74 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20  ents */..       
3ec4a 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
3ec4b 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27  ])!=0 && c!='\n'
3ec4c 3b 20 69 2b 2b 29 7b 7d 0d 0a 20 20 20 20 20 20  ; i++){}..      
3ec4d 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
3ec4e 4b 5f 53 50 41 43 45 3b 20 20 20 2f 2a 20 49 4d  K_SPACE;   /* IM
3ec4f 50 3a 20 52 2d 32 32 39 33 34 2d 32 35 31 33 34  P: R-22934-25134
3ec50 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72 65 74   */..        ret
3ec51 75 72 6e 20 69 3b 0d 0a 20 20 20 20 20 20 7d 0d  urn i;..      }.
3ec52 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
3ec53 65 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0d 0a 20  e = TK_MINUS;.. 
3ec54 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a       return 1;..
3ec55 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
3ec56 27 28 27 3a 20 7b 0d 0a 20 20 20 20 20 20 2a 74  '(': {..      *t
3ec57 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 50  okenType = TK_LP
3ec58 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
3ec59 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  1;..    }..    c
3ec5a 61 73 65 20 27 29 27 3a 20 7b 0d 0a 20 20 20 20  ase ')': {..    
3ec5b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
3ec5c 4b 5f 52 50 3b 0d 0a 20 20 20 20 20 20 72 65 74  K_RP;..      ret
3ec5d 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  urn 1;..    }.. 
3ec5e 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0d 0a     case ';': {..
3ec5f 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3ec60 20 3d 20 54 4b 5f 53 45 4d 49 3b 0d 0a 20 20 20   = TK_SEMI;..   
3ec61 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20     return 1;..  
3ec62 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 27 2b    }..    case '+
3ec63 27 3a 20 7b 0d 0a 20 20 20 20 20 20 2a 74 6f 6b  ': {..      *tok
3ec64 65 6e 54 79 70 65 20 3d 20 54 4b 5f 50 4c 55 53  enType = TK_PLUS
3ec65 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
3ec66 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63  1;..    }..    c
3ec67 61 73 65 20 27 2a 27 3a 20 7b 0d 0a 20 20 20 20  ase '*': {..    
3ec68 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
3ec69 4b 5f 53 54 41 52 3b 0d 0a 20 20 20 20 20 20 72  K_STAR;..      r
3ec6a 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d  eturn 1;..    }.
3ec6b 0a 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b  .    case '/': {
3ec6c 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  ..      if( z[1]
3ec6d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30  !='*' || z[2]==0
3ec6e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f   ){..        *to
3ec6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41  kenType = TK_SLA
3ec70 53 48 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74  SH;..        ret
3ec71 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d  urn 1;..      }.
3ec72 0a 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52  .      /* IMP: R
3ec73 2d 35 30 34 31 37 2d 32 37 39 37 36 20 2d 2d 20  -50417-27976 -- 
3ec74 73 79 6e 74 61 78 20 64 69 61 67 72 61 6d 20 66  syntax diagram f
3ec75 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0d 0a  or comments */..
3ec76 20 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63        for(i=3, c
3ec77 3d 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c  =z[2]; (c!='*' |
3ec78 7c 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20  | z[i]!='/') && 
3ec79 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
3ec7a 29 7b 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 63  ){}..      if( c
3ec7b 20 29 20 69 2b 2b 3b 0d 0a 20 20 20 20 20 20 2a   ) i++;..      *
3ec7c 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
3ec7d 50 41 43 45 3b 20 20 20 2f 2a 20 49 4d 50 3a 20  PACE;   /* IMP: 
3ec7e 52 2d 32 32 39 33 34 2d 32 35 31 33 34 20 2a 2f  R-22934-25134 */
3ec7f 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ..      return i
3ec80 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
3ec81 73 65 20 27 25 27 3a 20 7b 0d 0a 20 20 20 20 20  se '%': {..     
3ec82 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3ec83 5f 52 45 4d 3b 0d 0a 20 20 20 20 20 20 72 65 74  _REM;..      ret
3ec84 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20  urn 1;..    }.. 
3ec85 20 20 20 63 61 73 65 20 27 3d 27 3a 20 7b 0d 0a     case '=': {..
3ec86 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3ec87 20 3d 20 54 4b 5f 45 51 3b 0d 0a 20 20 20 20 20   = TK_EQ;..     
3ec88 20 72 65 74 75 72 6e 20 31 20 2b 20 28 7a 5b 31   return 1 + (z[1
3ec89 5d 3d 3d 27 3d 27 29 3b 0d 0a 20 20 20 20 7d 0d  ]=='=');..    }.
3ec8a 0a 20 20 20 20 63 61 73 65 20 27 3c 27 3a 20 7b  .    case '<': {
3ec8b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a  ..      if( (c=z
3ec8c 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0d 0a 20 20  [1])=='=' ){..  
3ec8d 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3ec8e 20 3d 20 54 4b 5f 4c 45 3b 0d 0a 20 20 20 20 20   = TK_LE;..     
3ec8f 20 20 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20     return 2;..  
3ec90 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
3ec91 3d 27 3e 27 20 29 7b 0d 0a 20 20 20 20 20 20 20  ='>' ){..       
3ec92 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3ec93 5f 4e 45 3b 0d 0a 20 20 20 20 20 20 20 20 72 65  _NE;..        re
3ec94 74 75 72 6e 20 32 3b 0d 0a 20 20 20 20 20 20 7d  turn 2;..      }
3ec95 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20  else if( c=='<' 
3ec96 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  ){..        *tok
3ec97 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49  enType = TK_LSHI
3ec98 46 54 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74  FT;..        ret
3ec99 75 72 6e 20 32 3b 0d 0a 20 20 20 20 20 20 7d 65  urn 2;..      }e
3ec9a 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74  lse{..        *t
3ec9b 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 54  okenType = TK_LT
3ec9c 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ;..        retur
3ec9d 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  n 1;..      }.. 
3ec9e 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 27     }..    case '
3ec9f 3e 27 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28  >': {..      if(
3eca0 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29   (c=z[1])=='=' )
3eca1 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  {..        *toke
3eca2 6e 54 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0d 0a  nType = TK_GE;..
3eca3 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
3eca4 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  ;..      }else i
3eca5 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0d 0a 20 20  f( c=='>' ){..  
3eca6 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3eca7 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0d 0a 20   = TK_RSHIFT;.. 
3eca8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
3eca9 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
3ecaa 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
3ecab 70 65 20 3d 20 54 4b 5f 47 54 3b 0d 0a 20 20 20  pe = TK_GT;..   
3ecac 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a       return 1;..
3ecad 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
3ecae 20 20 20 20 63 61 73 65 20 27 21 27 3a 20 7b 0d      case '!': {.
3ecaf 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21  .      if( z[1]!
3ecb0 3d 27 3d 27 20 29 7b 0d 0a 20 20 20 20 20 20 20  ='=' ){..       
3ecb1 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3ecb2 5f 49 4c 4c 45 47 41 4c 3b 0d 0a 20 20 20 20 20  _ILLEGAL;..     
3ecb3 20 20 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20     return 2;..  
3ecb4 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
3ecb5 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3ecb6 20 54 4b 5f 4e 45 3b 0d 0a 20 20 20 20 20 20 20   TK_NE;..       
3ecb7 20 72 65 74 75 72 6e 20 32 3b 0d 0a 20 20 20 20   return 2;..    
3ecb8 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
3ecb9 63 61 73 65 20 27 7c 27 3a 20 7b 0d 0a 20 20 20  case '|': {..   
3ecba 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27     if( z[1]!='|'
3ecbb 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f   ){..        *to
3ecbc 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54  kenType = TK_BIT
3ecbd 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74  OR;..        ret
3ecbe 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20 7d 65  urn 1;..      }e
3ecbf 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74  lse{..        *t
3ecc0 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f  okenType = TK_CO
3ecc1 4e 43 41 54 3b 0d 0a 20 20 20 20 20 20 20 20 72  NCAT;..        r
3ecc2 65 74 75 72 6e 20 32 3b 0d 0a 20 20 20 20 20 20  eturn 2;..      
3ecc3 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  }..    }..    ca
3ecc4 73 65 20 27 2c 27 3a 20 7b 0d 0a 20 20 20 20 20  se ',': {..     
3ecc5 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3ecc6 5f 43 4f 4d 4d 41 3b 0d 0a 20 20 20 20 20 20 72  _COMMA;..      r
3ecc7 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d  eturn 1;..    }.
3ecc8 0a 20 20 20 20 63 61 73 65 20 27 26 27 3a 20 7b  .    case '&': {
3ecc9 0d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  ..      *tokenTy
3ecca 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0d  pe = TK_BITAND;.
3eccb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
3eccc 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73  ..    }..    cas
3eccd 65 20 27 7e 27 3a 20 7b 0d 0a 20 20 20 20 20 20  e '~': {..      
3ecce 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
3eccf 42 49 54 4e 4f 54 3b 0d 0a 20 20 20 20 20 20 72  BITNOT;..      r
3ecd0 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d  eturn 1;..    }.
3ecd1 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 0d 0a  .    case '`':..
3ecd2 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0d 0a      case '\'':..
3ecd3 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0d      case '"': {.
3ecd4 0a 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d  .      int delim
3ecd5 20 3d 20 7a 5b 30 5d 3b 0d 0a 20 20 20 20 20 20   = z[0];..      
3ecd6 74 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d  testcase( delim=
3ecd7 3d 27 60 27 20 29 3b 0d 0a 20 20 20 20 20 20 74  ='`' );..      t
3ecd8 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d 3d  estcase( delim==
3ecd9 27 5c 27 27 20 29 3b 0d 0a 20 20 20 20 20 20 74  '\'' );..      t
3ecda 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d 3d  estcase( delim==
3ecdb 27 22 27 20 29 3b 0d 0a 20 20 20 20 20 20 66 6f  '"' );..      fo
3ecdc 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
3ecdd 3d 30 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  =0; i++){..     
3ecde 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20     if( c==delim 
3ecdf 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66  ){..          if
3ece0 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20  ( z[i+1]==delim 
3ece1 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
3ece2 69 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  i++;..          
3ece3 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
3ece4 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3ece5 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
3ece6 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
3ece7 20 20 20 69 66 28 20 63 3d 3d 27 5c 27 27 20 29     if( c=='\'' )
3ece8 7b 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  {..        *toke
3ece9 6e 54 79 70 65 20 3d 20 54 4b 5f 53 54 52 49 4e  nType = TK_STRIN
3ecea 47 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  G;..        retu
3eceb 72 6e 20 69 2b 31 3b 0d 0a 20 20 20 20 20 20 7d  rn i+1;..      }
3ecec 65 6c 73 65 20 69 66 28 20 63 21 3d 30 20 29 7b  else if( c!=0 ){
3eced 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ..        *token
3ecee 54 79 70 65 20 3d 20 54 4b 5f 49 44 3b 0d 0a 20  Type = TK_ID;.. 
3ecef 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
3ecf0 31 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1;..      }else{
3ecf1 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ..        *token
3ecf2 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
3ecf3 4c 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  L;..        retu
3ecf4 72 6e 20 69 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rn i;..      }..
3ecf5 20 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20      }..    case 
3ecf6 27 2e 27 3a 20 7b 0d 0a 23 69 66 6e 64 65 66 20  '.': {..#ifndef 
3ecf7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
3ecf8 54 49 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 20 20  TING_POINT..    
3ecf9 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
3ecfa 64 69 67 69 74 28 7a 5b 31 5d 29 20 29 0d 0a 23  digit(z[1]) )..#
3ecfb 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 7b 0d 0a  endif..      {..
3ecfc 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
3ecfd 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0d 0a 20 20  pe = TK_DOT;..  
3ecfe 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0d        return 1;.
3ecff 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3ed00 2f 2a 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  /* If the next c
3ed01 68 61 72 61 63 74 65 72 20 69 73 20 61 20 64 69  haracter is a di
3ed02 67 69 74 2c 20 74 68 69 73 20 69 73 20 61 20 66  git, this is a f
3ed03 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0d 0a 20  loating point.. 
3ed04 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 74       ** number t
3ed05 68 61 74 20 62 65 67 69 6e 73 20 77 69 74 68 20  hat begins with 
3ed06 22 2e 22 2e 20 20 46 61 6c 6c 20 74 68 72 75 20  ".".  Fall thru 
3ed07 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
3ed08 73 65 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20  se */..    }..  
3ed09 20 20 63 61 73 65 20 27 30 27 3a 20 63 61 73 65    case '0': case
3ed0a 20 27 31 27 3a 20 63 61 73 65 20 27 32 27 3a 20   '1': case '2': 
3ed0b 63 61 73 65 20 27 33 27 3a 20 63 61 73 65 20 27  case '3': case '
3ed0c 34 27 3a 0d 0a 20 20 20 20 63 61 73 65 20 27 35  4':..    case '5
3ed0d 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63 61 73  ': case '6': cas
3ed0e 65 20 27 37 27 3a 20 63 61 73 65 20 27 38 27 3a  e '7': case '8':
3ed0f 20 63 61 73 65 20 27 39 27 3a 20 7b 0d 0a 20 20   case '9': {..  
3ed10 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b      testcase( z[
3ed11 30 5d 3d 3d 27 30 27 20 29 3b 20 20 74 65 73 74  0]=='0' );  test
3ed12 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27 20  case( z[0]=='1' 
3ed13 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b  );  testcase( z[
3ed14 30 5d 3d 3d 27 32 27 20 29 3b 0d 0a 20 20 20 20  0]=='2' );..    
3ed15 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3ed16 3d 3d 27 33 27 20 29 3b 20 20 74 65 73 74 63 61  =='3' );  testca
3ed17 73 65 28 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b  se( z[0]=='4' );
3ed18 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3ed19 3d 3d 27 35 27 20 29 3b 0d 0a 20 20 20 20 20 20  =='5' );..      
3ed1a 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
3ed1b 27 36 27 20 29 3b 20 20 74 65 73 74 63 61 73 65  '6' );  testcase
3ed1c 28 20 7a 5b 30 5d 3d 3d 27 37 27 20 29 3b 20 20  ( z[0]=='7' );  
3ed1d 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
3ed1e 27 38 27 20 29 3b 0d 0a 20 20 20 20 20 20 74 65  '8' );..      te
3ed1f 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 39  stcase( z[0]=='9
3ed20 27 20 29 3b 0d 0a 20 20 20 20 20 20 2a 74 6f 6b  ' );..      *tok
3ed21 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45  enType = TK_INTE
3ed22 47 45 52 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28  GER;..      for(
3ed23 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 64 69  i=0; sqlite3Isdi
3ed24 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  git(z[i]); i++){
3ed25 7d 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
3ed26 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
3ed27 50 4f 49 4e 54 0d 0a 20 20 20 20 20 20 69 66 28  POINT..      if(
3ed28 20 7a 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0d 0a 20   z[i]=='.' ){.. 
3ed29 20 20 20 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 20         i++;..   
3ed2a 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69       while( sqli
3ed2b 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29  te3Isdigit(z[i])
3ed2c 20 29 7b 20 69 2b 2b 3b 20 7d 0d 0a 20 20 20 20   ){ i++; }..    
3ed2d 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3ed2e 20 54 4b 5f 46 4c 4f 41 54 3b 0d 0a 20 20 20 20   TK_FLOAT;..    
3ed2f 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 28    }..      if( (
3ed30 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20 7a 5b 69  z[i]=='e' || z[i
3ed31 5d 3d 3d 27 45 27 29 20 26 26 0d 0a 20 20 20 20  ]=='E') &&..    
3ed32 20 20 20 20 20 20 20 28 20 73 71 6c 69 74 65 33         ( sqlite3
3ed33 49 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d 29 20  Isdigit(z[i+1]) 
3ed34 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  ..            ||
3ed35 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20 7c   ((z[i+1]=='+' |
3ed36 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20 26  | z[i+1]=='-') &
3ed37 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  & sqlite3Isdigit
3ed38 28 7a 5b 69 2b 32 5d 29 29 0d 0a 20 20 20 20 20  (z[i+2]))..     
3ed39 20 20 20 20 20 20 29 0d 0a 20 20 20 20 20 20 29        )..      )
3ed3a 7b 0d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  {..        i += 
3ed3b 32 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c  2;..        whil
3ed3c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  e( sqlite3Isdigi
3ed3d 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20  t(z[i]) ){ i++; 
3ed3e 7d 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  }..        *toke
3ed3f 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54  nType = TK_FLOAT
3ed40 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64  ;..      }..#end
3ed41 69 66 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28  if..      while(
3ed42 20 49 64 43 68 61 72 28 7a 5b 69 5d 29 20 29 7b   IdChar(z[i]) ){
3ed43 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ..        *token
3ed44 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
3ed45 4c 3b 0d 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b  L;..        i++;
3ed46 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
3ed47 20 72 65 74 75 72 6e 20 69 3b 0d 0a 20 20 20 20   return i;..    
3ed48 7d 0d 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  }..    case '[':
3ed49 20 7b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   {..      for(i=
3ed4a 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d  1, c=z[0]; c!=']
3ed4b 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  ' && (c=z[i])!=0
3ed4c 3b 20 69 2b 2b 29 7b 7d 0d 0a 20 20 20 20 20 20  ; i++){}..      
3ed4d 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 63 3d 3d  *tokenType = c==
3ed4e 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a 20 54 4b  ']' ? TK_ID : TK
3ed4f 5f 49 4c 4c 45 47 41 4c 3b 0d 0a 20 20 20 20 20  _ILLEGAL;..     
3ed50 20 72 65 74 75 72 6e 20 69 3b 0d 0a 20 20 20 20   return i;..    
3ed51 7d 0d 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a  }..    case '?':
3ed52 20 7b 0d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e   {..      *token
3ed53 54 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42  Type = TK_VARIAB
3ed54 4c 45 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 69  LE;..      for(i
3ed55 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67  =1; sqlite3Isdig
3ed56 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
3ed57 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ..      return i
3ed58 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
3ed59 73 65 20 27 23 27 3a 20 7b 0d 0a 20 20 20 20 20  se '#': {..     
3ed5a 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65   for(i=1; sqlite
3ed5b 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20  3Isdigit(z[i]); 
3ed5c 69 2b 2b 29 7b 7d 0d 0a 20 20 20 20 20 20 69 66  i++){}..      if
3ed5d 28 20 69 3e 31 20 29 7b 0d 0a 20 20 20 20 20 20  ( i>1 ){..      
3ed5e 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20    /* Parameters 
3ed5f 6f 66 20 74 68 65 20 66 6f 72 6d 20 23 4e 4e 4e  of the form #NNN
3ed60 20 28 77 68 65 72 65 20 4e 4e 4e 20 69 73 20 61   (where NNN is a
3ed61 20 6e 75 6d 62 65 72 29 20 61 72 65 20 75 73 65   number) are use
3ed62 64 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  d..        ** in
3ed63 74 65 72 6e 61 6c 6c 79 20 62 79 20 73 71 6c 69  ternally by sqli
3ed64 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 2e 20  te3NestedParse. 
3ed65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f   */..        *to
3ed66 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45 47  kenType = TK_REG
3ed67 49 53 54 45 52 3b 0d 0a 20 20 20 20 20 20 20 20  ISTER;..        
3ed68 72 65 74 75 72 6e 20 69 3b 0d 0a 20 20 20 20 20  return i;..     
3ed69 20 7d 0d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c   }..      /* Fal
3ed6a 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74  l through into t
3ed6b 68 65 20 6e 65 78 74 20 63 61 73 65 20 69 66 20  he next case if 
3ed6c 74 68 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66  the '#' is not f
3ed6d 6f 6c 6c 6f 77 65 64 20 62 79 0d 0a 20 20 20 20  ollowed by..    
3ed6e 20 20 2a 2a 20 61 20 64 69 67 69 74 2e 20 54 72    ** a digit. Tr
3ed6f 79 20 74 6f 20 6d 61 74 63 68 20 23 41 41 41 41  y to match #AAAA
3ed70 20 77 68 65 72 65 20 41 41 41 41 20 69 73 20 61   where AAAA is a
3ed71 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e   parameter name.
3ed72 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 23 69 66 6e   */..    }..#ifn
3ed73 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3ed74 54 43 4c 5f 56 41 52 49 41 42 4c 45 0d 0a 20 20  TCL_VARIABLE..  
3ed75 20 20 63 61 73 65 20 27 24 27 3a 0d 0a 23 65 6e    case '$':..#en
3ed76 64 69 66 0d 0a 20 20 20 20 63 61 73 65 20 27 40  dif..    case '@
3ed77 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61  ':  /* For compa
3ed78 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d 53  tibility with MS
3ed79 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0d 0a   SQL Server */..
3ed7a 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0d      case ':': {.
3ed7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30  .      int n = 0
3ed7c 3b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
3ed7d 65 28 20 7a 5b 30 5d 3d 3d 27 24 27 20 29 3b 20  e( z[0]=='$' ); 
3ed7e 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
3ed7f 3d 27 40 27 20 29 3b 20 20 74 65 73 74 63 61 73  ='@' );  testcas
3ed80 65 28 20 7a 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0d  e( z[0]==':' );.
3ed81 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
3ed82 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b  e = TK_VARIABLE;
3ed83 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ..      for(i=1;
3ed84 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b   (c=z[i])!=0; i+
3ed85 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  +){..        if(
3ed86 20 49 64 43 68 61 72 28 63 29 20 29 7b 0d 0a 20   IdChar(c) ){.. 
3ed87 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0d 0a 23           n++;..#
3ed88 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3ed89 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0d  IT_TCL_VARIABLE.
3ed8a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
3ed8b 66 28 20 63 3d 3d 27 28 27 20 26 26 20 6e 3e 30  f( c=='(' && n>0
3ed8c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 64   ){..          d
3ed8d 6f 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  o{..            
3ed8e 69 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  i++;..          
3ed8f 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29  }while( (c=z[i])
3ed90 21 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33 49  !=0 && !sqlite3I
3ed91 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21 3d  sspace(c) && c!=
3ed92 27 29 27 20 29 3b 0d 0a 20 20 20 20 20 20 20 20  ')' );..        
3ed93 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0d    if( c==')' ){.
3ed94 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
3ed95 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  ;..          }el
3ed96 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  se{..           
3ed97 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3ed98 5f 49 4c 4c 45 47 41 4c 3b 0d 0a 20 20 20 20 20  _ILLEGAL;..     
3ed99 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
3ed9a 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3ed9b 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
3ed9c 3a 27 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a  :' && z[i+1]==':
3ed9d 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ' ){..          
3ed9e 69 2b 2b 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  i++;..#endif..  
3ed9f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3eda0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3eda1 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3eda2 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 3d   }..      if( n=
3eda3 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65 20  =0 ) *tokenType 
3eda4 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0d 0a 20  = TK_ILLEGAL;.. 
3eda5 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a       return i;..
3eda6 20 20 20 20 7d 0d 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
3eda7 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
3eda8 4c 49 54 45 52 41 4c 0d 0a 20 20 20 20 63 61 73  LITERAL..    cas
3eda9 65 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a  e 'x': case 'X':
3edaa 20 7b 0d 0a 20 20 20 20 20 20 74 65 73 74 63 61   {..      testca
3edab 73 65 28 20 7a 5b 30 5d 3d 3d 27 78 27 20 29 3b  se( z[0]=='x' );
3edac 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
3edad 3d 27 58 27 20 29 3b 0d 0a 20 20 20 20 20 20 69  ='X' );..      i
3edae 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b  f( z[1]=='\'' ){
3edaf 0d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ..        *token
3edb0 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0d  Type = TK_BLOB;.
3edb1 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
3edb2 3b 20 73 71 6c 69 74 65 33 49 73 78 64 69 67 69  ; sqlite3Isxdigi
3edb3 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0d  t(z[i]); i++){}.
3edb4 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69  .        if( z[i
3edb5 5d 21 3d 27 5c 27 27 20 7c 7c 20 69 25 32 20 29  ]!='\'' || i%2 )
3edb6 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 74 6f  {..          *to
3edb7 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
3edb8 45 47 41 4c 3b 0d 0a 20 20 20 20 20 20 20 20 20  EGAL;..         
3edb9 20 77 68 69 6c 65 28 20 7a 5b 69 5d 20 26 26 20   while( z[i] && 
3edba 7a 5b 69 5d 21 3d 27 5c 27 27 20 29 7b 20 69 2b  z[i]!='\'' ){ i+
3edbb 2b 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d  +; }..        }.
3edbc 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69  .        if( z[i
3edbd 5d 20 29 20 69 2b 2b 3b 0d 0a 20 20 20 20 20 20  ] ) i++;..      
3edbe 20 20 72 65 74 75 72 6e 20 69 3b 0d 0a 20 20 20    return i;..   
3edbf 20 20 20 7d 0d 0a 20 20 20 20 20 20 2f 2a 20 4f     }..      /* O
3edc0 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68  therwise fall th
3edc1 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
3edc2 74 20 63 61 73 65 20 2a 2f 0d 0a 20 20 20 20 7d  t case */..    }
3edc3 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 64 65  ..#endif..    de
3edc4 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20 20  fault: {..      
3edc5 69 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29 20  if( !IdChar(*z) 
3edc6 29 7b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ){..        brea
3edc7 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
3edc8 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64 43 68     for(i=1; IdCh
3edc9 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  ar(z[i]); i++){}
3edca 0d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  ..      *tokenTy
3edcb 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65  pe = keywordCode
3edcc 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0d 0a  ((char*)z, i);..
3edcd 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0d        return i;.
3edce 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 2a  .    }..  }..  *
3edcf 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
3edd0 4c 4c 45 47 41 4c 3b 0d 0a 20 20 72 65 74 75 72  LLEGAL;..  retur
3edd1 6e 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n 1;..}..../*..*
3edd2 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72  * Run the parser
3edd3 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 53 51   on the given SQ
3edd4 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 70  L string.  The p
3edd5 61 72 73 65 72 20 73 74 72 75 63 74 75 72 65 20  arser structure 
3edd6 69 73 0d 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e  is..** passed in
3edd7 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73 74  .  An SQLITE_ st
3edd8 61 74 75 73 20 63 6f 64 65 20 69 73 20 72 65 74  atus code is ret
3edd9 75 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65 72  urned.  If an er
3edda 72 6f 72 20 6f 63 63 75 72 73 0d 0a 2a 2a 20 74  ror occurs..** t
3eddb 68 65 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d  hen an and attem
3eddc 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72  pt is made to wr
3eddd 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
3edde 73 61 67 65 20 69 6e 74 6f 20 0d 0a 2a 2a 20 6d  sage into ..** m
3eddf 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
3ede0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
3ede1 6f 63 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65  oc() and to make
3ede2 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74   *pzErrMsg point
3ede3 20 74 6f 20 74 68 61 74 0d 0a 2a 2a 20 65 72 72   to that..** err
3ede4 6f 72 20 6d 65 73 73 61 67 65 2e 0d 0a 2a 2f 0d  or message...*/.
3ede5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3ede6 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61  int sqlite3RunPa
3ede7 72 73 65 72 28 50 61 72 73 65 20 2a 70 50 61 72  rser(Parse *pPar
3ede8 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
3ede9 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a 45  zSql, char **pzE
3edea 72 72 4d 73 67 29 7b 0d 0a 20 20 69 6e 74 20 6e  rrMsg){..  int n
3edeb 45 72 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Err = 0;        
3edec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3eded 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65  mber of errors e
3edee 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0d 0a 20  ncountered */.. 
3edef 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
3edf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3edf1 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
3edf2 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 45 6e   */..  void *pEn
3edf3 67 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20  gine;           
3edf4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 45         /* The LE
3edf5 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c 41  MON-generated LA
3edf6 4c 52 28 31 29 20 70 61 72 73 65 72 20 2a 2f 0d  LR(1) parser */.
3edf7 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65  .  int tokenType
3edf8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3edf9 20 20 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68     /* type of th
3edfa 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0d  e next token */.
3edfb 0a 20 20 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e  .  int lastToken
3edfc 50 61 72 73 65 64 20 3d 20 2d 31 3b 20 20 20 20  Parsed = -1;    
3edfd 20 20 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68     /* type of th
3edfe 65 20 70 72 65 76 69 6f 75 73 20 74 6f 6b 65 6e  e previous token
3edff 20 2a 2f 0d 0a 20 20 75 38 20 65 6e 61 62 6c 65   */..  u8 enable
3ee00 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  Lookaside;      
3ee01 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20         /* Saved 
3ee02 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6c 6f 6f  value of db->loo
3ee03 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
3ee04 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */..  sqlite3 *d
3ee05 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
3ee06 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
3ee07 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ee08 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 53 71 6c   */..  int mxSql
3ee09 4c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  Len;            
3ee0a 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 6c 65         /* Max le
3ee0b 6e 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73  ngth of an SQL s
3ee0c 74 72 69 6e 67 20 2a 2f 0d 0a 0d 0a 0d 0a 20 20  tring */......  
3ee0d 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e 61  mxSqlLen = db->a
3ee0e 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
3ee0f 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0d  IT_SQL_LENGTH];.
3ee10 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76  .  if( db->activ
3ee11 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0d 0a  eVdbeCnt==0 ){..
3ee12 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74      db->u1.isInt
3ee13 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0d 0a 20  errupted = 0;.. 
3ee14 20 7d 0d 0a 20 20 70 50 61 72 73 65 2d 3e 72 63   }..  pParse->rc
3ee15 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
3ee16 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
3ee17 20 7a 53 71 6c 3b 0d 0a 20 20 69 20 3d 20 30 3b   zSql;..  i = 0;
3ee18 0d 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72  ..  assert( pzEr
3ee19 72 4d 73 67 21 3d 30 20 29 3b 0d 0a 20 20 70 45  rMsg!=0 );..  pE
3ee1a 6e 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50  ngine = sqlite3P
3ee1b 61 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64  arserAlloc((void
3ee1c 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c  *(*)(size_t))sql
3ee1d 69 74 65 33 4d 61 6c 6c 6f 63 29 3b 0d 0a 20 20  ite3Malloc);..  
3ee1e 69 66 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29  if( pEngine==0 )
3ee1f 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  {..    db->mallo
3ee20 63 46 61 69 6c 65 64 20 3d 20 31 3b 0d 0a 20 20  cFailed = 1;..  
3ee21 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3ee22 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 61  NOMEM;..  }..  a
3ee23 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
3ee24 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0d 0a  NewTable==0 );..
3ee25 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
3ee26 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d 3d 30  ->pNewTrigger==0
3ee27 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
3ee28 50 61 72 73 65 2d 3e 6e 56 61 72 3d 3d 30 20 29  Parse->nVar==0 )
3ee29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
3ee2a 72 73 65 2d 3e 6e 7a 56 61 72 3d 3d 30 20 29 3b  rse->nzVar==0 );
3ee2b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
3ee2c 73 65 2d 3e 61 7a 56 61 72 3d 3d 30 20 29 3b 0d  se->azVar==0 );.
3ee2d 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69  .  enableLookasi
3ee2e 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  de = db->lookasi
3ee2f 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0d 0a 20 20  de.bEnabled;..  
3ee30 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  if( db->lookasid
3ee31 65 2e 70 53 74 61 72 74 20 29 20 64 62 2d 3e 6c  e.pStart ) db->l
3ee32 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
3ee33 64 20 3d 20 31 3b 0d 0a 20 20 77 68 69 6c 65 28  d = 1;..  while(
3ee34 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
3ee35 65 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30  ed && zSql[i]!=0
3ee36 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
3ee37 20 69 3e 3d 30 20 29 3b 0d 0a 20 20 20 20 70 50   i>=0 );..    pP
3ee38 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
3ee39 2e 7a 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0d 0a  .z = &zSql[i];..
3ee3a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61 73      pParse->sLas
3ee3b 74 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74  tToken.n = sqlit
3ee3c 65 33 47 65 74 54 6f 6b 65 6e 28 28 75 6e 73 69  e3GetToken((unsi
3ee3d 67 6e 65 64 20 63 68 61 72 2a 29 26 7a 53 71 6c  gned char*)&zSql
3ee3e 5b 69 5d 2c 26 74 6f 6b 65 6e 54 79 70 65 29 3b  [i],&tokenType);
3ee3f 0d 0a 20 20 20 20 69 20 2b 3d 20 70 50 61 72 73  ..    i += pPars
3ee40 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b  e->sLastToken.n;
3ee41 0d 0a 20 20 20 20 69 66 28 20 69 3e 6d 78 53 71  ..    if( i>mxSq
3ee42 6c 4c 65 6e 20 29 7b 0d 0a 20 20 20 20 20 20 70  lLen ){..      p
3ee43 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
3ee44 54 45 5f 54 4f 4f 42 49 47 3b 0d 0a 20 20 20 20  TE_TOOBIG;..    
3ee45 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
3ee46 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 6f 6b  .    switch( tok
3ee47 65 6e 54 79 70 65 20 29 7b 0d 0a 20 20 20 20 20  enType ){..     
3ee48 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45 3a 20   case TK_SPACE: 
3ee49 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  {..        if( d
3ee4a 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
3ee4b 74 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ted ){..        
3ee4c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
3ee4d 67 28 70 50 61 72 73 65 2c 20 22 69 6e 74 65 72  g(pParse, "inter
3ee4e 72 75 70 74 22 29 3b 0d 0a 20 20 20 20 20 20 20  rupt");..       
3ee4f 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
3ee50 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
3ee51 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ;..          got
3ee52 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0d 0a  o abort_parse;..
3ee53 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3ee54 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
3ee55 20 7d 0d 0a 20 20 20 20 20 20 63 61 73 65 20 54   }..      case T
3ee56 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b 0d 0a 20 20  K_ILLEGAL: {..  
3ee57 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
3ee58 72 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73  ree(db, *pzErrMs
3ee59 67 29 3b 0d 0a 20 20 20 20 20 20 20 20 2a 70 7a  g);..        *pz
3ee5a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
3ee5b 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 72  MPrintf(db, "unr
3ee5c 65 63 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a  ecognized token:
3ee5d 20 5c 22 25 54 5c 22 22 2c 0d 0a 20 20 20 20 20   \"%T\"",..     
3ee5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ee5f 20 20 20 26 70 50 61 72 73 65 2d 3e 73 4c 61 73     &pParse->sLas
3ee60 74 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20  tToken);..      
3ee61 20 20 6e 45 72 72 2b 2b 3b 0d 0a 20 20 20 20 20    nErr++;..     
3ee62 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61     goto abort_pa
3ee63 72 73 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  rse;..      }.. 
3ee64 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4d       case TK_SEM
3ee65 49 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 70 50  I: {..        pP
3ee66 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a  arse->zTail = &z
3ee67 53 71 6c 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 20  Sql[i];..       
3ee68 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e   /* Fall thru in
3ee69 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  to the default c
3ee6a 61 73 65 20 2a 2f 0d 0a 20 20 20 20 20 20 7d 0d  ase */..      }.
3ee6b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
3ee6c 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {..        sqlit
3ee6d 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65  e3Parser(pEngine
3ee6e 2c 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61  , tokenType, pPa
3ee6f 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c  rse->sLastToken,
3ee70 20 70 50 61 72 73 65 29 3b 0d 0a 20 20 20 20 20   pParse);..     
3ee71 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73     lastTokenPars
3ee72 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0d  ed = tokenType;.
3ee73 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
3ee74 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
3ee75 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
3ee76 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73   goto abort_pars
3ee77 65 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  e;..        }.. 
3ee78 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3ee79 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
3ee7a 20 7d 0d 0a 61 62 6f 72 74 5f 70 61 72 73 65 3a   }..abort_parse:
3ee7b 0d 0a 20 20 69 66 28 20 7a 53 71 6c 5b 69 5d 3d  ..  if( zSql[i]=
3ee7c 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30 20 26 26  =0 && nErr==0 &&
3ee7d 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c   pParse->rc==SQL
3ee7e 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69  ITE_OK ){..    i
3ee7f 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73  f( lastTokenPars
3ee80 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b 0d 0a  ed!=TK_SEMI ){..
3ee81 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
3ee82 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b 5f  ser(pEngine, TK_
3ee83 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73 4c  SEMI, pParse->sL
3ee84 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65  astToken, pParse
3ee85 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 72 73 65  );..      pParse
3ee86 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b  ->zTail = &zSql[
3ee87 69 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  i];..    }..    
3ee88 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45  sqlite3Parser(pE
3ee89 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65  ngine, 0, pParse
3ee8a 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50  ->sLastToken, pP
3ee8b 61 72 73 65 29 3b 0d 0a 20 20 7d 0d 0a 23 69 66  arse);..  }..#if
3ee8c 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
3ee8d 41 43 4b 44 45 50 54 48 0d 0a 20 20 73 71 6c 69  ACKDEPTH..  sqli
3ee8e 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
3ee8f 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45  ITE_STATUS_PARSE
3ee90 52 5f 53 54 41 43 4b 2c 0d 0a 20 20 20 20 20 20  R_STACK,..      
3ee91 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61  sqlite3ParserSta
3ee92 63 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65 29 0d  ckPeak(pEngine).
3ee93 0a 20 20 29 3b 0d 0a 23 65 6e 64 69 66 20 2f 2a  .  );..#endif /*
3ee94 20 59 59 44 45 42 55 47 20 2a 2f 0d 0a 20 20 73   YYDEBUG */..  s
3ee95 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65  qlite3ParserFree
3ee96 28 70 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74 65  (pEngine, sqlite
3ee97 33 5f 66 72 65 65 29 3b 0d 0a 20 20 64 62 2d 3e  3_free);..  db->
3ee98 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
3ee99 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61  ed = enableLooka
3ee9a 73 69 64 65 3b 0d 0a 20 20 69 66 28 20 64 62 2d  side;..  if( db-
3ee9b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
3ee9c 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  ..    pParse->rc
3ee9d 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3ee9e 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 50 61  ..  }..  if( pPa
3ee9f 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
3eea0 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63  OK && pParse->rc
3eea1 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26  !=SQLITE_DONE &&
3eea2 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
3eea3 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  ==0 ){..    sqli
3eea4 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 50  te3SetString(&pP
3eea5 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  arse->zErrMsg, d
3eea6 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
3eea7 45 72 72 53 74 72 28 70 50 61 72 73 65 2d 3e 72  ErrStr(pParse->r
3eea8 63 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73  c));..  }..  ass
3eea9 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30  ert( pzErrMsg!=0
3eeaa 20 29 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 73   );..  if( pPars
3eeab 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0d 0a 20  e->zErrMsg ){.. 
3eeac 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 70     *pzErrMsg = p
3eead 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0d  Parse->zErrMsg;.
3eeae 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67  .    sqlite3_log
3eeaf 28 70 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73  (pParse->rc, "%s
3eeb0 22 2c 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0d 0a  ", *pzErrMsg);..
3eeb1 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72      pParse->zErr
3eeb2 4d 73 67 20 3d 20 30 3b 0d 0a 20 20 20 20 6e 45  Msg = 0;..    nE
3eeb3 72 72 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  rr++;..  }..  if
3eeb4 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  ( pParse->pVdbe 
3eeb5 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e  && pParse->nErr>
3eeb6 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73  0 && pParse->nes
3eeb7 74 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73  ted==0 ){..    s
3eeb8 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
3eeb9 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b  (pParse->pVdbe);
3eeba 0d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56  ..    pParse->pV
3eebb 64 62 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 23  dbe = 0;..  }..#
3eebc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3eebd 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0d  IT_SHARED_CACHE.
3eebe 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
3eebf 65 73 74 65 64 3d 3d 30 20 29 7b 0d 0a 20 20 20  ested==0 ){..   
3eec0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
3eec1 62 2c 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  b, pParse->aTabl
3eec2 65 4c 6f 63 6b 29 3b 0d 0a 20 20 20 20 70 50 61  eLock);..    pPa
3eec3 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20  rse->aTableLock 
3eec4 3d 20 30 3b 0d 0a 20 20 20 20 70 50 61 72 73 65  = 0;..    pParse
3eec5 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30  ->nTableLock = 0
3eec6 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
3eec7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3eec8 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
3eec9 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
3eeca 28 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c  (pParse->apVtabL
3eecb 6f 63 6b 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ock);..#endif...
3eecc 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41  .  if( !IN_DECLA
3eecd 52 45 5f 56 54 41 42 20 29 7b 0d 0a 20 20 20 20  RE_VTAB ){..    
3eece 2f 2a 20 49 66 20 74 68 65 20 70 50 61 72 73 65  /* If the pParse
3eecf 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c  ->declareVtab fl
3eed0 61 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f  ag is set, do no
3eed1 74 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62  t delete any tab
3eed2 6c 65 20 0d 0a 20 20 20 20 2a 2a 20 73 74 72 75  le ..    ** stru
3eed3 63 74 75 72 65 20 62 75 69 6c 74 20 75 70 20 69  cture built up i
3eed4 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  n pParse->pNewTa
3eed5 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67  ble. The calling
3eed6 20 63 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e   code (see vtab.
3eed7 63 29 0d 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20  c)..    ** will 
3eed8 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62 69 6c  take responsibil
3eed9 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e 67 20  ity for freeing 
3eeda 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
3eedb 75 72 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ure...    */..  
3eedc 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
3eedd 61 62 6c 65 28 64 62 2c 20 70 50 61 72 73 65 2d  able(db, pParse-
3eede 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0d 0a 20 20  >pNewTable);..  
3eedf 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 44 65  }....  sqlite3De
3eee0 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
3eee1 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
3eee2 67 65 72 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 70  ger);..  for(i=p
3eee3 50 61 72 73 65 2d 3e 6e 7a 56 61 72 2d 31 3b 20  Parse->nzVar-1; 
3eee4 69 3e 3d 30 3b 20 69 2d 2d 29 20 73 71 6c 69 74  i>=0; i--) sqlit
3eee5 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61  e3DbFree(db, pPa
3eee6 72 73 65 2d 3e 61 7a 56 61 72 5b 69 5d 29 3b 0d  rse->azVar[i]);.
3eee7 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
3eee8 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56  (db, pParse->azV
3eee9 61 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 44  ar);..  sqlite3D
3eeea 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
3eeeb 2d 3e 61 41 6c 69 61 73 29 3b 0d 0a 20 20 77 68  ->aAlias);..  wh
3eeec 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 41 69  ile( pParse->pAi
3eeed 6e 63 20 29 7b 0d 0a 20 20 20 20 41 75 74 6f 69  nc ){..    Autoi
3eeee 6e 63 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61 72  ncInfo *p = pPar
3eeef 73 65 2d 3e 70 41 69 6e 63 3b 0d 0a 20 20 20 20  se->pAinc;..    
3eef0 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20  pParse->pAinc = 
3eef1 70 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20 73  p->pNext;..    s
3eef2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3eef3 20 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20 77 68 69   p);..  }..  whi
3eef4 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d  le( pParse->pZom
3eef5 62 69 65 54 61 62 20 29 7b 0d 0a 20 20 20 20 54  bieTab ){..    T
3eef6 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65  able *p = pParse
3eef7 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0d 0a 20  ->pZombieTab;.. 
3eef8 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62     pParse->pZomb
3eef9 69 65 54 61 62 20 3d 20 70 2d 3e 70 4e 65 78 74  ieTab = p->pNext
3eefa 5a 6f 6d 62 69 65 3b 0d 0a 20 20 20 20 73 71 6c  Zombie;..    sql
3eefb 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
3eefc 64 62 2c 20 70 29 3b 0d 0a 20 20 7d 0d 0a 20 20  db, p);..  }..  
3eefd 69 66 28 20 6e 45 72 72 3e 30 20 26 26 20 70 50  if( nErr>0 && pP
3eefe 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
3eeff 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 50 61 72  _OK ){..    pPar
3ef00 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
3ef01 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 20 20 72  ERROR;..  }..  r
3ef02 65 74 75 72 6e 20 6e 45 72 72 3b 0d 0a 7d 0d 0a  eturn nErr;..}..
3ef03 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3ef04 2a 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69 7a  * End of tokeniz
3ef05 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
3ef06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef08 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
3ef09 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 6f  ** Begin file co
3ef0a 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  mplete.c *******
3ef0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef0d 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20  */../*..** 2001 
3ef0e 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a 2a 2a  September 15..**
3ef0f 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
3ef10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
3ef11 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
3ef12 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
3ef13 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61  ce of..** a lega
3ef14 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
3ef15 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a  s a blessing:..*
3ef16 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  *..**    May you
3ef17 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3ef18 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61   evil...**    Ma
3ef19 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3ef1a 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3ef1b 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3ef1c 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d  others...**    M
3ef1d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
3ef1e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
3ef1f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
3ef20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a  give...**..*****
3ef21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ef25 2a 2a 2a 2a 0d 0a 2a 2a 20 41 6e 20 74 6f 6b 65  ****..** An toke
3ef26 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c 0d 0a 2a  nizer for SQL..*
3ef27 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  *..** This file 
3ef28 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
3ef29 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
3ef2a 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  the sqlite3_comp
3ef2b 6c 65 74 65 28 29 20 41 50 49 2e 0d 0a 2a 2a 20  lete() API...** 
3ef2c 54 68 69 73 20 63 6f 64 65 20 75 73 65 64 20 74  This code used t
3ef2d 6f 20 62 65 20 70 61 72 74 20 6f 66 20 74 68 65  o be part of the
3ef2e 20 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f 75   tokenizer.c sou
3ef2f 72 63 65 20 66 69 6c 65 2e 20 20 42 75 74 20 62  rce file.  But b
3ef30 79 0d 0a 2a 2a 20 73 65 70 61 72 61 74 69 6e 67  y..** separating
3ef31 20 69 74 20 6f 75 74 2c 20 74 68 65 20 63 6f 64   it out, the cod
3ef32 65 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  e will be automa
3ef33 74 69 63 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20  tically omitted 
3ef34 66 72 6f 6d 0d 0a 2a 2a 20 73 74 61 74 69 63 20  from..** static 
3ef35 6c 69 6e 6b 73 20 74 68 61 74 20 64 6f 20 6e 6f  links that do no
3ef36 74 20 75 73 65 20 69 74 2e 0d 0a 2a 2f 0d 0a 23  t use it...*/..#
3ef37 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3ef38 49 54 5f 43 4f 4d 50 4c 45 54 45 0d 0a 0d 0a 2f  IT_COMPLETE..../
3ef39 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 65  *..** This is de
3ef3a 66 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69 7a  fined in tokeniz
3ef3b 65 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68 61  e.c.  We just ha
3ef3c 76 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68 65  ve to import the
3ef3d 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0d 0a 2a 2f   definition...*/
3ef3e 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3ef3f 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0d 0a 23  _AMALGAMATION..#
3ef40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
3ef41 49 49 0d 0a 23 64 65 66 69 6e 65 20 49 64 43 68  II..#define IdCh
3ef42 61 72 28 43 29 20 20 28 28 73 71 6c 69 74 65 33  ar(C)  ((sqlite3
3ef43 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
3ef44 65 64 20 63 68 61 72 29 43 5d 26 30 78 34 36 29  ed char)C]&0x46)
3ef45 21 3d 30 29 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  !=0)..#endif..#i
3ef46 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
3ef47 49 43 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  IC..SQLITE_PRIVA
3ef48 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
3ef49 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
3ef4a 68 61 72 5b 5d 3b 0d 0a 23 64 65 66 69 6e 65 20  har[];..#define 
3ef4b 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d  IdChar(C)  (((c=
3ef4c 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69  C)>=0x42 && sqli
3ef4d 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61  te3IsEbcdicIdCha
3ef4e 72 5b 63 2d 30 78 34 30 5d 29 29 0d 0a 23 65 6e  r[c-0x40]))..#en
3ef4f 64 69 66 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  dif..#endif /* S
3ef50 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
3ef51 4f 4e 20 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  ON */....../*..*
3ef52 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 75 73  * Token types us
3ef53 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
3ef54 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75  3_complete() rou
3ef55 74 69 6e 65 2e 20 20 53 65 65 20 74 68 65 20 68  tine.  See the h
3ef56 65 61 64 65 72 0d 0a 2a 2a 20 63 6f 6d 6d 65 6e  eader..** commen
3ef57 74 73 20 6f 6e 20 74 68 61 74 20 70 72 6f 63 65  ts on that proce
3ef58 64 75 72 65 20 66 6f 72 20 61 64 64 69 74 69 6f  dure for additio
3ef59 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
3ef5a 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 74 6b  ..*/..#define tk
3ef5b 53 45 4d 49 20 20 20 20 30 0d 0a 23 64 65 66 69  SEMI    0..#defi
3ef5c 6e 65 20 74 6b 57 53 20 20 20 20 20 20 31 0d 0a  ne tkWS      1..
3ef5d 23 64 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20  #define tkOTHER 
3ef5e 20 20 32 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c    2..#ifndef SQL
3ef5f 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
3ef60 0d 0a 23 64 65 66 69 6e 65 20 74 6b 45 58 50 4c  ..#define tkEXPL
3ef61 41 49 4e 20 33 0d 0a 23 64 65 66 69 6e 65 20 74  AIN 3..#define t
3ef62 6b 43 52 45 41 54 45 20 20 34 0d 0a 23 64 65 66  kCREATE  4..#def
3ef63 69 6e 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0d  ine tkTEMP    5.
3ef64 0a 23 64 65 66 69 6e 65 20 74 6b 54 52 49 47 47  .#define tkTRIGG
3ef65 45 52 20 36 0d 0a 23 64 65 66 69 6e 65 20 74 6b  ER 6..#define tk
3ef66 45 4e 44 20 20 20 20 20 37 0d 0a 23 65 6e 64 69  END     7..#endi
3ef67 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  f..../*..** Retu
3ef68 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67  rn TRUE if the g
3ef69 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 20  iven SQL string 
3ef6a 65 6e 64 73 20 69 6e 20 61 20 73 65 6d 69 63 6f  ends in a semico
3ef6b 6c 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 70 65  lon...**..** Spe
3ef6c 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73  cial handling is
3ef6d 20 72 65 71 75 69 72 65 20 66 6f 72 20 43 52 45   require for CRE
3ef6e 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
3ef6f 65 6d 65 6e 74 73 2e 0d 0a 2a 2a 20 57 68 65 6e  ements...** When
3ef70 65 76 65 72 20 74 68 65 20 43 52 45 41 54 45 20  ever the CREATE 
3ef71 54 52 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73  TRIGGER keywords
3ef72 20 61 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73   are seen, the s
3ef73 74 61 74 65 6d 65 6e 74 0d 0a 2a 2a 20 6d 75 73  tatement..** mus
3ef74 74 20 65 6e 64 20 77 69 74 68 20 22 3b 45 4e 44  t end with ";END
3ef75 3b 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ;"...**..** This
3ef76 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3ef77 75 73 65 73 20 61 20 73 74 61 74 65 20 6d 61 63  uses a state mac
3ef78 68 69 6e 65 20 77 69 74 68 20 38 20 73 74 61 74  hine with 8 stat
3ef79 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28 30  es:..**..**   (0
3ef7a 29 20 49 4e 56 41 4c 49 44 20 20 20 57 65 20 68  ) INVALID   We h
3ef7b 61 76 65 20 6e 6f 74 20 79 65 74 20 73 65 65 6e  ave not yet seen
3ef7c 20 61 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63   a non-whitespac
3ef7d 65 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 2a 2a  e character...**
3ef7e 0d 0a 2a 2a 20 20 20 28 31 29 20 53 54 41 52 54  ..**   (1) START
3ef7f 20 20 20 20 20 41 74 20 74 68 65 20 62 65 67 69       At the begi
3ef80 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20  nning or end of 
3ef81 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
3ef82 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0d  .  This routine.
3ef83 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3ef84 20 20 20 20 72 65 74 75 72 6e 73 20 31 20 69 66      returns 1 if
3ef85 20 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65 20   it ends in the 
3ef86 53 54 41 52 54 20 73 74 61 74 65 20 61 6e 64 20  START state and 
3ef87 30 20 69 66 20 69 74 20 65 6e 64 73 0d 0a 2a 2a  0 if it ends..**
3ef88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ef89 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74   in any other st
3ef8a 61 74 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28  ate...**..**   (
3ef8b 32 29 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65 20  2) NORMAL    We 
3ef8c 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  are in the middl
3ef8d 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 77  e of statement w
3ef8e 68 69 63 68 20 65 6e 64 73 20 77 69 74 68 20 61  hich ends with a
3ef8f 20 73 69 6e 67 6c 65 0d 0a 2a 2a 20 20 20 20 20   single..**     
3ef90 20 20 20 20 20 20 20 20 20 20 20 20 73 65 6d 69              semi
3ef91 63 6f 6c 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  colon...**..**  
3ef92 20 28 33 29 20 45 58 50 4c 41 49 4e 20 20 20 54   (3) EXPLAIN   T
3ef93 68 65 20 6b 65 79 77 6f 72 64 20 45 58 50 4c 41  he keyword EXPLA
3ef94 49 4e 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e  IN has been seen
3ef95 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
3ef96 67 20 6f 66 20 0d 0a 2a 2a 20 20 20 20 20 20 20  g of ..**       
3ef97 20 20 20 20 20 20 20 20 20 20 61 20 73 74 61 74            a stat
3ef98 65 6d 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ement...**..**  
3ef99 20 28 34 29 20 43 52 45 41 54 45 20 20 20 20 54   (4) CREATE    T
3ef9a 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45 41 54  he keyword CREAT
3ef9b 45 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20  E has been seen 
3ef9c 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
3ef9d 20 6f 66 20 61 0d 0a 2a 2a 20 20 20 20 20 20 20   of a..**       
3ef9e 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
3ef9f 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70 72  ent, possibly pr
3efa0 65 63 65 65 64 65 64 20 62 79 20 45 58 50 4c 41  eceeded by EXPLA
3efa1 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77  IN and/or follow
3efa2 65 64 20 62 79 0d 0a 2a 2a 20 20 20 20 20 20 20  ed by..**       
3efa3 20 20 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f            TEMP o
3efa4 72 20 54 45 4d 50 4f 52 41 52 59 0d 0a 2a 2a 0d  r TEMPORARY..**.
3efa5 0a 2a 2a 20 20 20 28 35 29 20 54 52 49 47 47 45  .**   (5) TRIGGE
3efa6 52 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68  R   We are in th
3efa7 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72  e middle of a tr
3efa8 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
3efa9 20 74 68 61 74 20 6d 75 73 74 20 62 65 0d 0a 2a   that must be..*
3efaa 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3efab 20 20 65 6e 64 65 64 20 62 79 20 61 20 73 65 6d    ended by a sem
3efac 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79 77  icolon, the keyw
3efad 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e 6f  ord END, and ano
3efae 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0d  ther semicolon..
3efaf 0a 2a 2a 0d 0a 2a 2a 20 20 20 28 36 29 20 53 45  .**..**   (6) SE
3efb0 4d 49 20 20 20 20 20 20 57 65 27 76 65 20 73 65  MI      We've se
3efb1 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 65 6d  en the first sem
3efb2 69 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b  icolon in the ";
3efb3 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72  END;" that occur
3efb4 73 20 61 74 0d 0a 2a 2a 20 20 20 20 20 20 20 20  s at..**        
3efb5 20 20 20 20 20 20 20 20 20 74 68 65 20 65 6e 64           the end
3efb6 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65   of a trigger de
3efb7 66 69 6e 69 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a  finition...**..*
3efb8 2a 20 20 20 28 37 29 20 45 4e 44 20 20 20 20 20  *   (7) END     
3efb9 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65    We've seen the
3efba 20 22 3b 45 4e 44 22 20 6f 66 20 74 68 65 20 22   ";END" of the "
3efbb 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75  ;END;" that occu
3efbc 72 73 20 61 74 20 74 68 65 20 65 6e 64 0d 0a 2a  rs at the end..*
3efbd 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3efbe 20 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64    of a trigger d
3efbf 69 66 69 6e 69 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  ifinition...**..
3efc0 2a 2a 20 54 72 61 6e 73 69 74 69 6f 6e 73 20 62  ** Transitions b
3efc1 65 74 77 65 65 6e 20 73 74 61 74 65 73 20 61 62  etween states ab
3efc2 6f 76 65 20 61 72 65 20 64 65 74 65 72 6d 69 6e  ove are determin
3efc3 65 64 20 62 79 20 74 6f 6b 65 6e 73 20 65 78 74  ed by tokens ext
3efc4 72 61 63 74 65 64 0d 0a 2a 2a 20 66 72 6f 6d 20  racted..** from 
3efc5 74 68 65 20 69 6e 70 75 74 2e 20 20 54 68 65 20  the input.  The 
3efc6 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b 65 6e 73  following tokens
3efc7 20 61 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74   are significant
3efc8 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 28 30 29 20  :..**..**   (0) 
3efc9 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20 73 65  tkSEMI      A se
3efca 6d 69 63 6f 6c 6f 6e 2e 0d 0a 2a 2a 20 20 20 28  micolon...**   (
3efcb 31 29 20 74 6b 57 53 20 20 20 20 20 20 20 20 57  1) tkWS        W
3efcc 68 69 74 65 73 70 61 63 65 2e 0d 0a 2a 2a 20 20  hitespace...**  
3efcd 20 28 32 29 20 74 6b 4f 54 48 45 52 20 20 20 20   (2) tkOTHER    
3efce 20 41 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 74   Any other SQL t
3efcf 6f 6b 65 6e 2e 0d 0a 2a 2a 20 20 20 28 33 29 20  oken...**   (3) 
3efd0 74 6b 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20  tkEXPLAIN   The 
3efd1 22 65 78 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72  "explain" keywor
3efd2 64 2e 0d 0a 2a 2a 20 20 20 28 34 29 20 74 6b 43  d...**   (4) tkC
3efd3 52 45 41 54 45 20 20 20 20 54 68 65 20 22 63 72  REATE    The "cr
3efd4 65 61 74 65 22 20 6b 65 79 77 6f 72 64 2e 0d 0a  eate" keyword...
3efd5 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d 50 20  **   (5) tkTEMP 
3efd6 20 20 20 20 20 54 68 65 20 22 74 65 6d 70 22 20       The "temp" 
3efd7 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22 20 6b  or "temporary" k
3efd8 65 79 77 6f 72 64 2e 0d 0a 2a 2a 20 20 20 28 36  eyword...**   (6
3efd9 29 20 74 6b 54 52 49 47 47 45 52 20 20 20 54 68  ) tkTRIGGER   Th
3efda 65 20 22 74 72 69 67 67 65 72 22 20 6b 65 79 77  e "trigger" keyw
3efdb 6f 72 64 2e 0d 0a 2a 2a 20 20 20 28 37 29 20 74  ord...**   (7) t
3efdc 6b 45 4e 44 20 20 20 20 20 20 20 54 68 65 20 22  kEND       The "
3efdd 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e 0d 0a 2a  end" keyword...*
3efde 2a 0d 0a 2a 2a 20 57 68 69 74 65 73 70 61 63 65  *..** Whitespace
3efdf 20 6e 65 76 65 72 20 63 61 75 73 65 73 20 61 20   never causes a 
3efe0 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e  state transition
3efe1 20 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20 69   and is always i
3efe2 67 6e 6f 72 65 64 2e 0d 0a 2a 2a 20 54 68 69 73  gnored...** This
3efe3 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 53 51   means that a SQ
3efe4 4c 20 73 74 72 69 6e 67 20 6f 66 20 61 6c 6c 20  L string of all 
3efe5 77 68 69 74 65 73 70 61 63 65 20 69 73 20 69 6e  whitespace is in
3efe6 76 61 6c 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  valid...**..** I
3efe7 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
3efe8 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  h SQLITE_OMIT_TR
3efe9 49 47 47 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68  IGGER, all of th
3efea 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65  e computation ne
3efeb 65 64 65 64 0d 0a 2a 2a 20 74 6f 20 72 65 63 6f  eded..** to reco
3efec 67 6e 69 7a 65 20 74 68 65 20 65 6e 64 20 6f 66  gnize the end of
3efed 20 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62   a trigger can b
3efee 65 20 6f 6d 69 74 74 65 64 2e 20 20 41 6c 6c 20  e omitted.  All 
3efef 77 65 20 68 61 76 65 20 74 6f 20 64 6f 0d 0a 2a  we have to do..*
3eff0 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f 72 20 61 20  * is look for a 
3eff1 73 65 6d 69 63 6f 6c 6f 6e 20 74 68 61 74 20 69  semicolon that i
3eff2 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 6e  s not part of an
3eff3 20 73 74 72 69 6e 67 20 6f 72 20 63 6f 6d 6d 65   string or comme
3eff4 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  nt...*/..SQLITE_
3eff5 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3eff6 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20 63  complete(const c
3eff7 68 61 72 20 2a 7a 53 71 6c 29 7b 0d 0a 20 20 75  har *zSql){..  u
3eff8 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20 20 2f  8 state = 0;   /
3eff9 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65 2c  * Current state,
3effa 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73 20 64   using numbers d
3effb 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64 65 72  efined in header
3effc 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0d 0a 20 20 75   comment */..  u
3effd 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f  8 token;       /
3effe 2a 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  * Value of the n
3efff 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 0d 0a  ext token */....
3f000 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3f001 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20 2f  MIT_TRIGGER..  /
3f002 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74 61 74  * A complex stat
3f003 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20 75 73  ement machine us
3f004 65 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65  ed to detect the
3f005 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41 54 45   end of a CREATE
3f006 20 54 52 49 47 47 45 52 0d 0a 20 20 2a 2a 20 73   TRIGGER..  ** s
3f007 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73 20  tatement.  This 
3f008 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63 61  is the normal ca
3f009 73 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 74 61  se...  */..  sta
3f00a 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61  tic const u8 tra
3f00b 6e 73 5b 38 5d 5b 38 5d 20 3d 20 7b 0d 0a 20 20  ns[8][8] = {..  
3f00c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f00d 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20     /* Token:    
3f00e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f00f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f010 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a              */..
3f011 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20       /* State:  
3f012 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57       **  SEMI  W
3f013 53 20 20 4f 54 48 45 52 20 20 45 58 50 4c 41 49  S  OTHER  EXPLAI
3f014 4e 20 20 43 52 45 41 54 45 20 20 54 45 4d 50 20  N  CREATE  TEMP 
3f015 20 54 52 49 47 47 45 52 20 20 45 4e 44 20 2a 2f   TRIGGER  END */
3f016 0d 0a 20 20 20 20 20 2f 2a 20 30 20 49 4e 56 41  ..     /* 0 INVA
3f017 4c 49 44 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20  LID: */ {    1, 
3f018 20 30 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20   0,     2,      
3f019 20 33 2c 20 20 20 20 20 20 34 2c 20 20 20 20 32   3,      4,    2
3f01a 2c 20 20 20 20 20 20 20 32 2c 20 20 20 32 2c 20  ,       2,   2, 
3f01b 7d 2c 0d 0a 20 20 20 20 20 2f 2a 20 31 20 20 20  },..     /* 1   
3f01c 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 31  START: */ {    1
3f01d 2c 20 20 31 2c 20 20 20 20 20 32 2c 20 20 20 20  ,  1,     2,    
3f01e 20 20 20 33 2c 20 20 20 20 20 20 34 2c 20 20 20     3,      4,   
3f01f 20 32 2c 20 20 20 20 20 20 20 32 2c 20 20 20 32   2,       2,   2
3f020 2c 20 7d 2c 0d 0a 20 20 20 20 20 2f 2a 20 32 20  , },..     /* 2 
3f021 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20   NORMAL: */ {   
3f022 20 31 2c 20 20 32 2c 20 20 20 20 20 32 2c 20 20   1,  2,     2,  
3f023 20 20 20 20 20 32 2c 20 20 20 20 20 20 32 2c 20       2,      2, 
3f024 20 20 20 32 2c 20 20 20 20 20 20 20 32 2c 20 20     2,       2,  
3f025 20 32 2c 20 7d 2c 0d 0a 20 20 20 20 20 2f 2a 20   2, },..     /* 
3f026 33 20 45 58 50 4c 41 49 4e 3a 20 2a 2f 20 7b 20  3 EXPLAIN: */ { 
3f027 20 20 20 31 2c 20 20 33 2c 20 20 20 20 20 33 2c     1,  3,     3,
3f028 20 20 20 20 20 20 20 32 2c 20 20 20 20 20 20 34         2,      4
3f029 2c 20 20 20 20 32 2c 20 20 20 20 20 20 20 32 2c  ,    2,       2,
3f02a 20 20 20 32 2c 20 7d 2c 0d 0a 20 20 20 20 20 2f     2, },..     /
3f02b 2a 20 34 20 20 43 52 45 41 54 45 3a 20 2a 2f 20  * 4  CREATE: */ 
3f02c 7b 20 20 20 20 31 2c 20 20 34 2c 20 20 20 20 20  {    1,  4,     
3f02d 32 2c 20 20 20 20 20 20 20 32 2c 20 20 20 20 20  2,       2,     
3f02e 20 32 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20   2,    4,       
3f02f 35 2c 20 20 20 32 2c 20 7d 2c 0d 0a 20 20 20 20  5,   2, },..    
3f030 20 2f 2a 20 35 20 54 52 49 47 47 45 52 3a 20 2a   /* 5 TRIGGER: *
3f031 2f 20 7b 20 20 20 20 36 2c 20 20 35 2c 20 20 20  / {    6,  5,   
3f032 20 20 35 2c 20 20 20 20 20 20 20 35 2c 20 20 20    5,       5,   
3f033 20 20 20 35 2c 20 20 20 20 35 2c 20 20 20 20 20     5,    5,     
3f034 20 20 35 2c 20 20 20 35 2c 20 7d 2c 0d 0a 20 20    5,   5, },..  
3f035 20 20 20 2f 2a 20 36 20 20 20 20 53 45 4d 49 3a     /* 6    SEMI:
3f036 20 2a 2f 20 7b 20 20 20 20 36 2c 20 20 36 2c 20   */ {    6,  6, 
3f037 20 20 20 20 35 2c 20 20 20 20 20 20 20 35 2c 20      5,       5, 
3f038 20 20 20 20 20 35 2c 20 20 20 20 35 2c 20 20 20       5,    5,   
3f039 20 20 20 20 35 2c 20 20 20 37 2c 20 7d 2c 0d 0a      5,   7, },..
3f03a 20 20 20 20 20 2f 2a 20 37 20 20 20 20 20 45 4e       /* 7     EN
3f03b 44 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20 37  D: */ {    1,  7
3f03c 2c 20 20 20 20 20 35 2c 20 20 20 20 20 20 20 35  ,     5,       5
3f03d 2c 20 20 20 20 20 20 35 2c 20 20 20 20 35 2c 20  ,      5,    5, 
3f03e 20 20 20 20 20 20 35 2c 20 20 20 35 2c 20 7d 2c        5,   5, },
3f03f 0d 0a 20 20 7d 3b 0d 0a 23 65 6c 73 65 0d 0a 20  ..  };..#else.. 
3f040 20 2f 2a 20 49 66 20 74 72 69 67 67 65 72 73 20   /* If triggers 
3f041 61 72 65 20 6e 6f 74 20 73 75 70 70 6f 72 74 65  are not supporte
3f042 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70 69 6c  d by this compil
3f043 65 20 74 68 65 6e 20 74 68 65 20 73 74 61 74 65  e then the state
3f044 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0d 0a 20 20  ment machine..  
3f045 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 63  ** used to detec
3f046 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 73  t the end of a s
3f047 74 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63 68  tatement is much
3f048 20 73 69 6d 70 6c 69 65 72 0d 0a 20 20 2a 2f 0d   simplier..  */.
3f049 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3f04a 75 38 20 74 72 61 6e 73 5b 33 5d 5b 33 5d 20 3d  u8 trans[3][3] =
3f04b 20 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   {..            
3f04c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
3f04d 6e 3a 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d  n:           */.
3f04e 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20  .     /* State: 
3f04f 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20        **  SEMI  
3f050 57 53 20 20 4f 54 48 45 52 20 2a 2f 0d 0a 20 20  WS  OTHER */..  
3f051 20 20 20 2f 2a 20 30 20 49 4e 56 41 4c 49 44 3a     /* 0 INVALID:
3f052 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20 30 2c 20   */ {    1,  0, 
3f053 20 20 20 20 32 2c 20 7d 2c 0d 0a 20 20 20 20 20      2, },..     
3f054 2f 2a 20 31 20 20 20 53 54 41 52 54 3a 20 2a 2f  /* 1   START: */
3f055 20 7b 20 20 20 20 31 2c 20 20 31 2c 20 20 20 20   {    1,  1,    
3f056 20 32 2c 20 7d 2c 0d 0a 20 20 20 20 20 2f 2a 20   2, },..     /* 
3f057 32 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20  2  NORMAL: */ { 
3f058 20 20 20 31 2c 20 20 32 2c 20 20 20 20 20 32 2c     1,  2,     2,
3f059 20 7d 2c 0d 0a 20 20 7d 3b 0d 0a 23 65 6e 64 69   },..  };..#endi
3f05a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
3f05b 5f 54 52 49 47 47 45 52 20 2a 2f 0d 0a 0d 0a 20  _TRIGGER */.... 
3f05c 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 29 7b   while( *zSql ){
3f05d 0d 0a 20 20 20 20 73 77 69 74 63 68 28 20 2a 7a  ..    switch( *z
3f05e 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20 20 63 61  Sql ){..      ca
3f05f 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41 20  se ';': {  /* A 
3f060 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0d 0a 20 20  semicolon */..  
3f061 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3f062 53 45 4d 49 3b 0d 0a 20 20 20 20 20 20 20 20 62  SEMI;..        b
3f063 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
3f064 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 0d        case ' ':.
3f065 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 72 27  .      case '\r'
3f066 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  :..      case '\
3f067 74 27 3a 0d 0a 20 20 20 20 20 20 63 61 73 65 20  t':..      case 
3f068 27 5c 6e 27 3a 0d 0a 20 20 20 20 20 20 63 61 73  '\n':..      cas
3f069 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68  e '\f': {  /* Wh
3f06a 69 74 65 20 73 70 61 63 65 20 69 73 20 69 67 6e  ite space is ign
3f06b 6f 72 65 64 20 2a 2f 0d 0a 20 20 20 20 20 20 20  ored */..       
3f06c 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0d 0a   token = tkWS;..
3f06d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3f06e 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 63        }..      c
3f06f 61 73 65 20 27 2f 27 3a 20 7b 20 20 20 2f 2a 20  ase '/': {   /* 
3f070 43 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73  C-style comments
3f071 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28   */..        if(
3f072 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a 27 20 29 7b   zSql[1]!='*' ){
3f073 0d 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65  ..          toke
3f074 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0d 0a 20 20  n = tkOTHER;..  
3f075 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
3f076 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3f077 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0d 0a 20     zSql += 2;.. 
3f078 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53         while( zS
3f079 71 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30  ql[0] && (zSql[0
3f07a 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31  ]!='*' || zSql[1
3f07b 5d 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b  ]!='/') ){ zSql+
3f07c 2b 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66  +; }..        if
3f07d 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72  ( zSql[0]==0 ) r
3f07e 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20  eturn 0;..      
3f07f 20 20 7a 53 71 6c 2b 2b 3b 0d 0a 20 20 20 20 20    zSql++;..     
3f080 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b     token = tkWS;
3f081 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
3f082 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
3f083 20 63 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f   case '-': {   /
3f084 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d  * SQL-style comm
3f085 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74  ents from "--" t
3f086 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f  o end of line */
3f087 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53  ..        if( zS
3f088 71 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0d 0a 20  ql[1]!='-' ){.. 
3f089 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
3f08a 20 74 6b 4f 54 48 45 52 3b 0d 0a 20 20 20 20 20   tkOTHER;..     
3f08b 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
3f08c 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
3f08d 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20  while( *zSql && 
3f08e 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20 29 7b 20 7a  *zSql!='\n' ){ z
3f08f 53 71 6c 2b 2b 3b 20 7d 0d 0a 20 20 20 20 20 20  Sql++; }..      
3f090 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29    if( *zSql==0 )
3f091 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d 31   return state==1
3f092 3b 0d 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  ;..        token
3f093 20 3d 20 74 6b 57 53 3b 0d 0a 20 20 20 20 20 20   = tkWS;..      
3f094 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
3f095 7d 0d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b  }..      case '[
3f096 27 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73  ': {   /* Micros
3f097 6f 66 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69  oft-style identi
3f098 66 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a  fiers in [...] *
3f099 2f 0d 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b  /..        zSql+
3f09a 2b 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c  +;..        whil
3f09b 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71  e( *zSql && *zSq
3f09c 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b  l!=']' ){ zSql++
3f09d 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28  ; }..        if(
3f09e 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75   *zSql==0 ) retu
3f09f 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 74  rn 0;..        t
3f0a0 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0d  oken = tkOTHER;.
3f0a1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
3f0a2 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
3f0a3 63 61 73 65 20 27 60 27 3a 20 20 20 20 20 2f 2a  case '`':     /*
3f0a4 20 47 72 61 76 65 2d 61 63 63 65 6e 74 20 71 75   Grave-accent qu
3f0a5 6f 74 65 64 20 73 79 6d 62 6f 6c 73 20 75 73 65  oted symbols use
3f0a6 64 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0d 0a 20  d by MySQL */.. 
3f0a7 20 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20       case '"':  
3f0a8 20 20 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61 6e     /* single- an
3f0a9 64 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  d double-quoted 
3f0aa 73 74 72 69 6e 67 73 20 2a 2f 0d 0a 20 20 20 20  strings */..    
3f0ab 20 20 63 61 73 65 20 27 5c 27 27 3a 20 7b 0d 0a    case '\'': {..
3f0ac 20 20 20 20 20 20 20 20 69 6e 74 20 63 20 3d 20          int c = 
3f0ad 2a 7a 53 71 6c 3b 0d 0a 20 20 20 20 20 20 20 20  *zSql;..        
3f0ae 7a 53 71 6c 2b 2b 3b 0d 0a 20 20 20 20 20 20 20  zSql++;..       
3f0af 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26   while( *zSql &&
3f0b0 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20 7a 53 71   *zSql!=c ){ zSq
3f0b1 6c 2b 2b 3b 20 7d 0d 0a 20 20 20 20 20 20 20 20  l++; }..        
3f0b2 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72  if( *zSql==0 ) r
3f0b3 65 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 20 20  eturn 0;..      
3f0b4 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3f0b5 52 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  R;..        brea
3f0b6 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
3f0b7 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a 23     default: {..#
3f0b8 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
3f0b9 44 49 43 0d 0a 20 20 20 20 20 20 20 20 75 6e 73  DIC..        uns
3f0ba 69 67 6e 65 64 20 63 68 61 72 20 63 3b 0d 0a 23  igned char c;..#
3f0bb 65 6e 64 69 66 0d 0a 20 20 20 20 20 20 20 20 69  endif..        i
3f0bc 66 28 20 49 64 43 68 61 72 28 28 75 38 29 2a 7a  f( IdChar((u8)*z
3f0bd 53 71 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  Sql) ){..       
3f0be 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61     /* Keywords a
3f0bf 6e 64 20 75 6e 71 75 6f 74 65 64 20 69 64 65 6e  nd unquoted iden
3f0c0 74 69 66 69 65 72 73 20 2a 2f 0d 0a 20 20 20 20  tifiers */..    
3f0c1 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0d 0a        int nId;..
3f0c2 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 49            for(nI
3f0c3 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71 6c  d=1; IdChar(zSql
3f0c4 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d  [nId]); nId++){}
3f0c5 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
3f0c6 4f 4d 49 54 5f 54 52 49 47 47 45 52 0d 0a 20 20  OMIT_TRIGGER..  
3f0c7 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3f0c8 74 6b 4f 54 48 45 52 3b 0d 0a 23 65 6c 73 65 0d  tkOTHER;..#else.
3f0c9 0a 20 20 20 20 20 20 20 20 20 20 73 77 69 74 63  .          switc
3f0ca 68 28 20 2a 7a 53 71 6c 20 29 7b 0d 0a 20 20 20  h( *zSql ){..   
3f0cb 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27 63           case 'c
3f0cc 27 3a 20 63 61 73 65 20 27 43 27 3a 20 7b 0d 0a  ': case 'C': {..
3f0cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
3f0ce 28 20 6e 49 64 3d 3d 36 20 26 26 20 73 71 6c 69  ( nId==6 && sqli
3f0cf 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
3f0d0 2c 20 22 63 72 65 61 74 65 22 2c 20 36 29 3d 3d  , "create", 6)==
3f0d1 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  0 ){..          
3f0d2 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3f0d3 43 52 45 41 54 45 3b 0d 0a 20 20 20 20 20 20 20  CREATE;..       
3f0d4 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
3f0d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
3f0d6 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0d  oken = tkOTHER;.
3f0d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
3f0d8 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3f0d9 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
3f0da 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
3f0db 20 20 20 63 61 73 65 20 27 74 27 3a 20 63 61 73     case 't': cas
3f0dc 65 20 27 54 27 3a 20 7b 0d 0a 20 20 20 20 20 20  e 'T': {..      
3f0dd 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
3f0de 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =7 && sqlite3Str
3f0df 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69  NICmp(zSql, "tri
3f0e0 67 67 65 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0d  gger", 7)==0 ){.
3f0e1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3f0e2 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49 47 47   token = tkTRIGG
3f0e3 45 52 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ER;..           
3f0e4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64     }else if( nId
3f0e5 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==4 && sqlite3St
3f0e6 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65  rNICmp(zSql, "te
3f0e7 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0d 0a 20  mp", 4)==0 ){.. 
3f0e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
3f0e9 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0d 0a  oken = tkTEMP;..
3f0ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
3f0eb 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26  lse if( nId==9 &
3f0ec 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
3f0ed 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61  p(zSql, "tempora
3f0ee 72 79 22 2c 20 39 29 3d 3d 30 20 29 7b 0d 0a 20  ry", 9)==0 ){.. 
3f0ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
3f0f0 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0d 0a  oken = tkTEMP;..
3f0f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
3f0f2 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  lse{..          
3f0f3 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3f0f4 4f 54 48 45 52 3b 0d 0a 20 20 20 20 20 20 20 20  OTHER;..        
3f0f5 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
3f0f6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
3f0f7 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20             }..  
3f0f8 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27            case '
3f0f9 65 27 3a 20 20 63 61 73 65 20 27 45 27 3a 20 7b  e':  case 'E': {
3f0fa 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3f0fb 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20 73 71  if( nId==3 && sq
3f0fc 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
3f0fd 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d 30  ql, "end", 3)==0
3f0fe 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
3f0ff 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45       token = tkE
3f100 4e 44 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  ND;..           
3f101 20 20 20 7d 65 6c 73 65 0d 0a 23 69 66 6e 64 65     }else..#ifnde
3f102 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
3f103 50 4c 41 49 4e 0d 0a 20 20 20 20 20 20 20 20 20  PLAIN..         
3f104 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20       if( nId==7 
3f105 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
3f106 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69  mp(zSql, "explai
3f107 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0d 0a 20 20  n", 7)==0 ){..  
3f108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
3f109 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49 4e 3b  ken = tkEXPLAIN;
3f10a 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3f10b 7d 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 20  }else..#endif.. 
3f10c 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 0d 0a               {..
3f10d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f10e 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
3f10f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
3f110 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  }..             
3f111 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
3f112 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
3f113 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a      default: {..
3f114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
3f115 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0d 0a  ken = tkOTHER;..
3f116 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
3f117 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  eak;..          
3f118 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 7d    }..          }
3f119 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
3f11a 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
3f11b 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 53  */..          zS
3f11c 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0d 0a 20 20  ql += nId-1;..  
3f11d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
3f11e 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61          /* Opera
3f11f 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69 61 6c  tors and special
3f120 20 73 79 6d 62 6f 6c 73 20 2a 2f 0d 0a 20 20 20   symbols */..   
3f121 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
3f122 6b 4f 54 48 45 52 3b 0d 0a 20 20 20 20 20 20 20  kOTHER;..       
3f123 20 7d 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61   }..        brea
3f124 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
3f125 20 7d 0d 0a 20 20 20 20 73 74 61 74 65 20 3d 20   }..    state = 
3f126 74 72 61 6e 73 5b 73 74 61 74 65 5d 5b 74 6f 6b  trans[state][tok
3f127 65 6e 5d 3b 0d 0a 20 20 20 20 7a 53 71 6c 2b 2b  en];..    zSql++
3f128 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
3f129 20 73 74 61 74 65 3d 3d 31 3b 0d 0a 7d 0d 0a 0d   state==1;..}...
3f12a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3f12b 4f 4d 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a  OMIT_UTF16../*..
3f12c 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3f12d 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
3f12e 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  he sqlite3_compl
3f12f 65 74 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65  ete() routine de
3f130 73 63 72 69 62 65 64 0d 0a 2a 2a 20 61 62 6f 76  scribed..** abov
3f131 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 74  e, except that t
3f132 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
3f133 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 55  required to be U
3f134 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e  TF-16 encoded, n
3f135 6f 74 0d 0a 2a 2a 20 55 54 46 2d 38 2e 0d 0a 2a  ot..** UTF-8...*
3f136 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
3f137 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  t sqlite3_comple
3f138 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20  te16(const void 
3f139 2a 7a 53 71 6c 29 7b 0d 0a 20 20 73 71 6c 69 74  *zSql){..  sqlit
3f13a 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0d  e3_value *pVal;.
3f13b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
3f13c 53 71 6c 38 3b 0d 0a 20 20 69 6e 74 20 72 63 20  Sql8;..  int rc 
3f13d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
3f13e 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
3f13f 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0d  E_OMIT_AUTOINIT.
3f140 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3f141 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0d 0a 20  initialize();.. 
3f142 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
3f143 20 72 63 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20   rc;..#endif..  
3f144 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
3f145 6c 75 65 4e 65 77 28 30 29 3b 0d 0a 20 20 73 71  lueNew(0);..  sq
3f146 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
3f147 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c  (pVal, -1, zSql,
3f148 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
3f149 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  IVE, SQLITE_STAT
3f14a 49 43 29 3b 0d 0a 20 20 7a 53 71 6c 38 20 3d 20  IC);..  zSql8 = 
3f14b 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
3f14c 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
3f14d 46 38 29 3b 0d 0a 20 20 69 66 28 20 7a 53 71 6c  F8);..  if( zSql
3f14e 38 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73  8 ){..    rc = s
3f14f 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
3f150 7a 53 71 6c 38 29 3b 0d 0a 20 20 7d 65 6c 73 65  zSql8);..  }else
3f151 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  {..    rc = SQLI
3f152 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a  TE_NOMEM;..  }..
3f153 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
3f154 65 65 28 70 56 61 6c 29 3b 0d 0a 20 20 72 65 74  ee(pVal);..  ret
3f155 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
3f156 69 74 28 30 2c 20 72 63 29 3b 0d 0a 7d 0d 0a 23  it(0, rc);..}..#
3f157 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
3f158 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 23  OMIT_UTF16 */..#
3f159 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
3f15a 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f  OMIT_COMPLETE */
3f15b 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
3f15c 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c  *** End of compl
3f15d 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
3f15e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f15f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f160 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
3f161 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3f162 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
3f163 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f164 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f165 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30  ***/../*..** 200
3f166 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0d 0a  1 September 15..
3f167 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
3f168 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
3f169 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
3f16a 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
3f16b 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
3f16c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3f16d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
3f16e 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
3f16f 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
3f170 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
3f171 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
3f172 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
3f173 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
3f174 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
3f175 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
3f176 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
3f177 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
3f178 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
3f179 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f17a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f17b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f17c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f17d 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 4d 61 69 6e 20  ******..** Main 
3f17e 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c  file for the SQL
3f17f 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
3f180 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
3f181 69 73 20 66 69 6c 65 0d 0a 2a 2a 20 69 6d 70 6c  is file..** impl
3f182 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61  ement the progra
3f183 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74  mmer interface t
3f184 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  o the library.  
3f185 52 6f 75 74 69 6e 65 73 20 69 6e 0d 0a 2a 2a 20  Routines in..** 
3f186 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20  other files are 
3f187 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
3f188 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73   by SQLite and s
3f189 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0d 0a 2a 2a  hould not be..**
3f18a 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
3f18b 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
3f18c 79 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66  y...*/....#ifdef
3f18d 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
3f18e 54 53 33 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  TS3../**********
3f18f 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
3f190 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  3.h in the middl
3f191 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  e of main.c ****
3f192 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f193 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
3f194 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3f195 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.h ********
3f196 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f197 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f198 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30  ****/../*..** 20
3f199 30 36 20 4f 63 74 20 31 30 0d 0a 2a 2a 0d 0a 2a  06 Oct 10..**..*
3f19a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
3f19b 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
3f19c 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3f19d 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3f19e 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e  of..** a legal n
3f19f 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3f1a0 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a   blessing:..**..
3f1a1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
3f1a2 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
3f1a3 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  il...**    May y
3f1a4 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
3f1a5 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
3f1a6 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
3f1a7 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  ers...**    May 
3f1a8 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
3f1a9 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
3f1aa 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
3f1ab 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a  e...**..********
3f1ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1b0 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ******..**..** T
3f1b1 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
3f1b2 69 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72  is used by progr
3f1b3 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f  ams that want to
3f1b4 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
3f1b5 65 0d 0a 2a 2a 20 46 54 53 33 20 6c 69 62 72 61  e..** FTS3 libra
3f1b6 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73  ry.  All it does
3f1b7 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65 20   is declare the 
3f1b8 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28  sqlite3Fts3Init(
3f1b9 29 20 69 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2f  ) interface...*/
3f1ba 0d 0a 0d 0a 23 69 66 20 30 0d 0a 65 78 74 65 72  ....#if 0..exter
3f1bb 6e 20 22 43 22 20 7b 0d 0a 23 65 6e 64 69 66 20  n "C" {..#endif 
3f1bc 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20   /* __cplusplus 
3f1bd 2a 2f 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  */....SQLITE_PRI
3f1be 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3f1bf 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts3Init(sqlite3
3f1c0 20 2a 64 62 29 3b 0d 0a 0d 0a 23 69 66 20 30 0d   *db);....#if 0.
3f1c1 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43  .}  /* extern "C
3f1c2 22 20 2a 2f 0d 0a 23 65 6e 64 69 66 20 20 2f 2a  " */..#endif  /*
3f1c3 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0d   __cplusplus */.
3f1c4 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
3f1c5 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68  ** End of fts3.h
3f1c6 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3f1c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1c9 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
3f1ca 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
3f1cb 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
3f1cc 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   in main.c *****
3f1cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1ce 2a 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  **/..#endif..#if
3f1cf 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
3f1d0 45 5f 52 54 52 45 45 0d 0a 2f 2a 2a 2a 2a 2a 2a  E_RTREE../******
3f1d1 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
3f1d2 20 72 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20   rtree.h in the 
3f1d3 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
3f1d4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3f1d5 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
3f1d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
3f1d7 66 69 6c 65 20 72 74 72 65 65 2e 68 20 2a 2a 2a  file rtree.h ***
3f1d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1da 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
3f1db 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0d 0a 2a  * 2008 May 26..*
3f1dc 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
3f1dd 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
3f1de 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
3f1df 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
3f1e0 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
3f1e1 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
3f1e2 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
3f1e3 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
3f1e4 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3f1e5 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
3f1e6 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
3f1e7 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
3f1e8 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
3f1e9 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
3f1ea 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
3f1eb 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
3f1ec 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
3f1ed 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
3f1ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f1f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a  **********..**..
3f1f3 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
3f1f4 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20 70  ile is used by p
3f1f5 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e  rograms that wan
3f1f6 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73  t to link agains
3f1f7 74 20 74 68 65 0d 0a 2a 2a 20 52 54 52 45 45 20  t the..** RTREE 
3f1f8 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74  library.  All it
3f1f9 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65   does is declare
3f1fa 20 74 68 65 20 73 71 6c 69 74 65 33 52 74 72 65   the sqlite3Rtre
3f1fb 65 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63  eInit() interfac
3f1fc 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 20 30 0d  e...*/....#if 0.
3f1fd 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0d 0a 23  .extern "C" {..#
3f1fe 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
3f1ff 73 70 6c 75 73 20 2a 2f 0d 0a 0d 0a 53 51 4c 49  splus */....SQLI
3f200 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3f201 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28  qlite3RtreeInit(
3f202 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0d 0a 0d  sqlite3 *db);...
3f203 0a 23 69 66 20 30 0d 0a 7d 20 20 2f 2a 20 65 78  .#if 0..}  /* ex
3f204 74 65 72 6e 20 22 43 22 20 2a 2f 0d 0a 23 65 6e  tern "C" */..#en
3f205 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70  dif  /* __cplusp
3f206 6c 75 73 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  lus */..../*****
3f207 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
3f208 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   rtree.h *******
3f209 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f20a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f20b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
3f20c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
3f20d 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
3f20e 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e  eft off in main.
3f20f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 23 65 6e 64  *********/..#end
3f211 69 66 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
3f212 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0d 0a 2f 2a  E_ENABLE_ICU../*
3f213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
3f214 63 6c 75 64 65 20 73 71 6c 69 74 65 69 63 75 2e  clude sqliteicu.
3f215 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
3f216 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  of main.c ******
3f217 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
3f218 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
3f219 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65  egin file sqlite
3f21a 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.h **********
3f21b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f21c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
3f21d 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20  /*..** 2008 May 
3f21e 32 36 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  26..**..** The a
3f21f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3f220 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3f221 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3f222 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
3f223 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
3f224 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
3f225 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
3f226 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
3f227 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
3f228 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3f229 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3f22a 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3f22b 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
3f22c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
3f22d 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
3f22e 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
3f22f 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
3f230 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3f231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f233 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d  ***************.
3f235 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 68 65 61  .**..** This hea
3f236 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64  der file is used
3f237 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61   by programs tha
3f238 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61  t want to link a
3f239 67 61 69 6e 73 74 20 74 68 65 0d 0a 2a 2a 20 49  gainst the..** I
3f23a 43 55 20 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41  CU extension.  A
3f23b 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64 65  ll it does is de
3f23c 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74 65  clare the sqlite
3f23d 33 49 63 75 49 6e 69 74 28 29 20 69 6e 74 65 72  3IcuInit() inter
3f23e 66 61 63 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66  face...*/....#if
3f23f 20 30 0d 0a 65 78 74 65 72 6e 20 22 43 22 20 7b   0..extern "C" {
3f240 0d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  ..#endif  /* __c
3f241 70 6c 75 73 70 6c 75 73 20 2a 2f 0d 0a 0d 0a 53  plusplus */....S
3f242 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
3f243 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74  t sqlite3IcuInit
3f244 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0d 0a  (sqlite3 *db);..
3f245 0d 0a 23 69 66 20 30 0d 0a 7d 20 20 2f 2a 20 65  ..#if 0..}  /* e
3f246 78 74 65 72 6e 20 22 43 22 20 2a 2f 0d 0a 23 65  xtern "C" */..#e
3f247 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
3f248 70 6c 75 73 20 2a 2f 0d 0a 0d 0a 0d 0a 2f 2a 2a  plus */....../**
3f249 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
3f24a 20 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68 20   of sqliteicu.h 
3f24b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f24c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f24d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
3f24e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
3f24f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
3f250 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
3f251 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
3f252 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 23  ************/..#
3f253 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 6e 64 65 66  endif....#ifndef
3f254 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
3f255 54 49 4f 4e 0d 0a 2f 2a 20 49 4d 50 4c 45 4d 45  TION../* IMPLEME
3f256 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 34 36  NTATION-OF: R-46
3f257 36 35 36 2d 34 35 31 35 36 20 54 68 65 20 73 71  656-45156 The sq
3f258 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20  lite3_version[] 
3f259 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 0d  string constant.
3f25a 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  .** contains the
3f25b 20 74 65 78 74 20 6f 66 20 53 51 4c 49 54 45 5f   text of SQLITE_
3f25c 56 45 52 53 49 4f 4e 20 6d 61 63 72 6f 2e 20 0d  VERSION macro. .
3f25d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3f25e 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
3f25f 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
3f260 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0d 0a  QLITE_VERSION;..
3f261 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 49 4d 50  #endif..../* IMP
3f262 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20  LEMENTATION-OF: 
3f263 52 2d 35 33 35 33 36 2d 34 32 35 37 35 20 54 68  R-53536-42575 Th
3f264 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  e sqlite3_libver
3f265 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  sion() function 
3f266 72 65 74 75 72 6e 73 0d 0a 2a 2a 20 61 20 70 6f  returns..** a po
3f267 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 6f 20  inter to the to 
3f268 74 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73  the sqlite3_vers
3f269 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e  ion[] string con
3f26a 73 74 61 6e 74 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c  stant. ..*/..SQL
3f26b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
3f26c 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76  ar *sqlite3_libv
3f26d 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65  ersion(void){ re
3f26e 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 65 72  turn sqlite3_ver
3f26f 73 69 6f 6e 3b 20 7d 0d 0a 0d 0a 2f 2a 20 49 4d  sion; }..../* IM
3f270 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a  PLEMENTATION-OF:
3f271 20 52 2d 36 33 31 32 34 2d 33 39 33 30 30 20 54   R-63124-39300 T
3f272 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63  he sqlite3_sourc
3f273 65 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  eid() function r
3f274 65 74 75 72 6e 73 20 61 0d 0a 2a 2a 20 70 6f 69  eturns a..** poi
3f275 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67  nter to a string
3f276 20 63 6f 6e 73 74 61 6e 74 20 77 68 6f 73 65 20   constant whose 
3f277 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d  value is the sam
3f278 65 20 61 73 20 74 68 65 0d 0a 2a 2a 20 53 51 4c  e as the..** SQL
3f279 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 43 20  ITE_SOURCE_ID C 
3f27a 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63  preprocessor mac
3f27b 72 6f 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ro. ..*/..SQLITE
3f27c 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
3f27d 2a 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  *sqlite3_sourcei
3f27e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
3f27f 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44  SQLITE_SOURCE_ID
3f280 3b 20 7d 0d 0a 0d 0a 2f 2a 20 49 4d 50 4c 45 4d  ; }..../* IMPLEM
3f281 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 33  ENTATION-OF: R-3
3f282 35 32 31 30 2d 36 33 35 30 38 20 54 68 65 20 73  5210-63508 The s
3f283 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
3f284 6e 5f 6e 75 6d 62 65 72 28 29 20 66 75 6e 63 74  n_number() funct
3f285 69 6f 6e 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ion..** returns 
3f286 61 6e 20 69 6e 74 65 67 65 72 20 65 71 75 61 6c  an integer equal
3f287 20 74 6f 20 53 51 4c 49 54 45 5f 56 45 52 53 49   to SQLITE_VERSI
3f288 4f 4e 5f 4e 55 4d 42 45 52 2e 0d 0a 2a 2f 0d 0a  ON_NUMBER...*/..
3f289 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3f28a 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
3f28b 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 7b 20  n_number(void){ 
3f28c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 56 45  return SQLITE_VE
3f28d 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d 0d  RSION_NUMBER; }.
3f28e 0a 0d 0a 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41  .../* IMPLEMENTA
3f28f 54 49 4f 4e 2d 4f 46 3a 20 52 2d 32 30 37 39 30  TION-OF: R-20790
3f290 2d 31 34 30 32 35 20 54 68 65 20 73 71 6c 69 74  -14025 The sqlit
3f291 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 20  e3_threadsafe() 
3f292 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
3f293 0d 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 61 6e 64  ..** zero if and
3f294 20 6f 6e 6c 79 20 69 66 20 53 51 4c 69 74 65 20   only if SQLite 
3f295 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
3f296 68 20 6d 75 74 65 78 69 6e 67 20 63 6f 64 65 20  h mutexing code 
3f297 6f 6d 69 74 74 65 64 20 64 75 65 20 74 6f 0d 0a  omitted due to..
3f298 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 54 48  ** the SQLITE_TH
3f299 52 45 41 44 53 41 46 45 20 63 6f 6d 70 69 6c 65  READSAFE compile
3f29a 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 62 65 69  -time option bei
3f29b 6e 67 20 73 65 74 20 74 6f 20 30 2e 0d 0a 2a 2f  ng set to 0...*/
3f29c 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
3f29d 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73   sqlite3_threads
3f29e 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  afe(void){ retur
3f29f 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  n SQLITE_THREADS
3f2a0 41 46 45 3b 20 7d 0d 0a 0d 0a 23 69 66 20 21 64  AFE; }....#if !d
3f2a1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
3f2a2 49 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  IT_TRACE) && def
3f2a3 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
3f2a4 4c 45 5f 49 4f 54 52 41 43 45 29 0d 0a 2f 2a 0d  LE_IOTRACE)../*.
3f2a5 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f  .** If the follo
3f2a6 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f  wing function po
3f2a7 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
3f2a8 4c 20 61 6e 64 20 69 66 0d 0a 2a 2a 20 53 51 4c  L and if..** SQL
3f2a9 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41  ITE_ENABLE_IOTRA
3f2aa 43 45 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  CE is enabled, t
3f2ab 68 65 6e 20 6d 65 73 73 61 67 65 73 20 64 65 73  hen messages des
3f2ac 63 72 69 62 69 6e 67 0d 0a 2a 2a 20 49 2f 4f 20  cribing..** I/O 
3f2ad 61 63 74 69 76 65 20 61 72 65 20 77 72 69 74 74  active are writt
3f2ae 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  en using this fu
3f2af 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 6d  nction.  These m
3f2b0 65 73 73 61 67 65 73 0d 0a 2a 2a 20 61 72 65 20  essages..** are 
3f2b1 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 64 65 62  intended for deb
3f2b2 75 67 67 69 6e 67 20 61 63 74 69 76 69 74 79 20  ugging activity 
3f2b3 6f 6e 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  only...*/..SQLIT
3f2b4 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
3f2b5 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29  *sqlite3IoTrace)
3f2b6 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  (const char*, ..
3f2b7 2e 29 20 3d 20 30 3b 0d 0a 23 65 6e 64 69 66 0d  .) = 0;..#endif.
3f2b8 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  .../*..** If the
3f2b9 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61   following globa
3f2ba 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74  l variable point
3f2bb 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68  s to a string wh
3f2bc 69 63 68 20 69 73 20 74 68 65 0d 0a 2a 2a 20 6e  ich is the..** n
3f2bd 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f  ame of a directo
3f2be 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69  ry, then that di
3f2bf 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20  rectory will be 
3f2c0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0d 0a 2a  used to store..*
3f2c1 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
3f2c2 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 65 65 20 61  s...**..** See a
3f2c3 6c 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41 20  lso the "PRAGMA 
3f2c4 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
3f2c5 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e  tory" SQL comman
3f2c6 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  d...*/..SQLITE_A
3f2c7 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
3f2c8 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
3f2c9 3d 20 30 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  = 0;..../*..** I
3f2ca 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c 69 74 65  nitialize SQLite
3f2cb 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  .  ..**..** This
3f2cc 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
3f2cd 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69   called to initi
3f2ce 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79  alize the memory
3f2cf 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0d 0a 2a 2a   allocation,..**
3f2d0 20 56 46 53 2c 20 61 6e 64 20 6d 75 74 65 78 20   VFS, and mutex 
3f2d1 73 75 62 73 79 73 74 65 6d 73 20 70 72 69 6f 72  subsystems prior
3f2d2 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79 20 73 65   to doing any se
3f2d3 72 69 6f 75 73 20 77 6f 72 6b 20 77 69 74 68 0d  rious work with.
3f2d4 0a 2a 2a 20 53 51 4c 69 74 65 2e 20 20 42 75 74  .** SQLite.  But
3f2d5 20 61 73 20 6c 6f 6e 67 20 61 73 20 79 6f 75 20   as long as you 
3f2d6 64 6f 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 20 77  do not compile w
3f2d7 69 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ith SQLITE_OMIT_
3f2d8 41 55 54 4f 49 4e 49 54 0d 0a 2a 2a 20 74 68 69  AUTOINIT..** thi
3f2d9 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62  s routine will b
3f2da 65 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74  e called automat
3f2db 69 63 61 6c 6c 79 20 62 79 20 6b 65 79 20 72 6f  ically by key ro
3f2dc 75 74 69 6e 65 73 20 73 75 63 68 20 61 73 0d 0a  utines such as..
3f2dd 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
3f2de 29 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  ).  ..**..** Thi
3f2df 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
3f2e0 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f 6e 20 69  o-op except on i
3f2e1 74 73 20 76 65 72 79 20 66 69 72 73 74 20 63 61  ts very first ca
3f2e2 6c 6c 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65  ll for the proce
3f2e3 73 73 2c 0d 0a 2a 2a 20 6f 72 20 66 6f 72 20 74  ss,..** or for t
3f2e4 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 61 66  he first call af
3f2e5 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ter a call to sq
3f2e6 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 2e 0d  lite3_shutdown..
3f2e7 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69 72 73  .**..** The firs
3f2e8 74 20 74 68 72 65 61 64 20 74 6f 20 63 61 6c 6c  t thread to call
3f2e9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   this routine ru
3f2ea 6e 73 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  ns the initializ
3f2eb 61 74 69 6f 6e 20 74 6f 0d 0a 2a 2a 20 63 6f 6d  ation to..** com
3f2ec 70 6c 65 74 69 6f 6e 2e 20 20 49 66 20 73 75 62  pletion.  If sub
3f2ed 73 65 71 75 65 6e 74 20 74 68 72 65 61 64 73 20  sequent threads 
3f2ee 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
3f2ef 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  e before the fir
3f2f0 73 74 0d 0a 2a 2a 20 74 68 72 65 61 64 20 68 61  st..** thread ha
3f2f1 73 20 66 69 6e 69 73 68 65 64 20 74 68 65 20 69  s finished the i
3f2f2 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72  nitialization pr
3f2f3 6f 63 65 73 73 2c 20 74 68 65 6e 20 74 68 65 20  ocess, then the 
3f2f4 73 75 62 73 65 71 75 65 6e 74 0d 0a 2a 2a 20 74  subsequent..** t
3f2f5 68 72 65 61 64 73 20 6d 75 73 74 20 62 6c 6f 63  hreads must bloc
3f2f6 6b 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73  k until the firs
3f2f7 74 20 74 68 72 65 61 64 20 66 69 6e 69 73 68 65  t thread finishe
3f2f8 73 20 77 69 74 68 20 74 68 65 20 69 6e 69 74 69  s with the initi
3f2f9 61 6c 69 7a 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a  alization...**..
3f2fa 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 68 72  ** The first thr
3f2fb 65 61 64 20 6d 69 67 68 74 20 63 61 6c 6c 20 74  ead might call t
3f2fc 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 63 75  his routine recu
3f2fd 72 73 69 76 65 6c 79 2e 20 20 52 65 63 75 72 73  rsively.  Recurs
3f2fe 69 76 65 0d 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f  ive..** calls to
3f2ff 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   this routine sh
3f300 6f 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c 20  ould not block, 
3f301 6f 66 20 63 6f 75 72 73 65 2e 20 20 4f 74 68 65  of course.  Othe
3f302 72 77 69 73 65 20 74 68 65 0d 0a 2a 2a 20 69 6e  rwise the..** in
3f303 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f  itialization pro
3f304 63 65 73 73 20 77 6f 75 6c 64 20 6e 65 76 65 72  cess would never
3f305 20 63 6f 6d 70 6c 65 74 65 2e 0d 0a 2a 2a 0d 0a   complete...**..
3f306 2a 2a 20 4c 65 74 20 58 20 62 65 20 74 68 65 20  ** Let X be the 
3f307 66 69 72 73 74 20 74 68 72 65 61 64 20 74 6f 20  first thread to 
3f308 65 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  enter this routi
3f309 6e 65 2e 20 20 4c 65 74 20 59 20 62 65 20 73 6f  ne.  Let Y be so
3f30a 6d 65 20 6f 74 68 65 72 0d 0a 2a 2a 20 74 68 72  me other..** thr
3f30b 65 61 64 2e 20 20 54 68 65 6e 20 77 68 69 6c 65  ead.  Then while
3f30c 20 74 68 65 20 69 6e 69 74 69 61 6c 20 69 6e 76   the initial inv
3f30d 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ocation of this 
3f30e 72 6f 75 74 69 6e 65 20 62 79 20 58 20 69 73 0d  routine by X is.
3f30f 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65 74 65 2c 20  .** incomplete, 
3f310 69 74 20 69 73 20 72 65 71 75 69 72 65 64 20 74  it is required t
3f311 68 61 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  hat:..**..**    
3f312 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74 68 69 73  *  Calls to this
3f313 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 59 20   routine from Y 
3f314 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69 6c  must block until
3f315 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f 73 74 0d   the outer-most.
3f316 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c 20 62  .**       call b
3f317 79 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e 0d 0a  y X completes...
3f318 2a 2a 0d 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63  **..**    *  Rec
3f319 75 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20  ursive calls to 
3f31a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
3f31b 6d 20 74 68 72 65 61 64 20 58 20 72 65 74 75 72  m thread X retur
3f31c 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0d 0a 2a  n immediately..*
3f31d 2a 20 20 20 20 20 20 20 77 69 74 68 6f 75 74 20  *       without 
3f31e 62 6c 6f 63 6b 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53  blocking...*/..S
3f31f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3f320 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
3f321 28 76 6f 69 64 29 7b 0d 0a 20 20 4d 55 54 45 58  (void){..  MUTEX
3f322 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f  _LOGIC( sqlite3_
3f323 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20  mutex *pMaster; 
3f324 29 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d  )       /* The m
3f325 61 69 6e 20 73 74 61 74 69 63 20 6d 75 74 65 78  ain static mutex
3f326 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20   */..  int rc;  
3f327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f329 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
3f32a 64 65 20 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20  de */....#ifdef 
3f32b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0d  SQLITE_OMIT_WSD.
3f32c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3f32d 77 73 64 5f 69 6e 69 74 28 34 30 39 36 2c 20 32  wsd_init(4096, 2
3f32e 34 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53  4);..  if( rc!=S
3f32f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
3f330 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d   return rc;..  }
3f331 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a  ..#endif....  /*
3f332 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c   If SQLite is al
3f333 72 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 6c 79  ready completely
3f334 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
3f335 65 6e 20 74 68 69 73 20 63 61 6c 6c 0d 0a 20 20  en this call..  
3f336 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ** to sqlite3_in
3f337 69 74 69 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c  itialize() shoul
3f338 64 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 42  d be a no-op.  B
3f339 75 74 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  ut the initializ
3f33a 61 74 69 6f 6e 0d 0a 20 20 2a 2a 20 6d 75 73 74  ation..  ** must
3f33b 20 62 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53   be complete.  S
3f33c 6f 20 69 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f  o isInit must no
3f33d 74 20 62 65 20 73 65 74 20 75 6e 74 69 6c 20 74  t be set until t
3f33e 68 65 20 76 65 72 79 20 65 6e 64 0d 0a 20 20 2a  he very end..  *
3f33f 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * of this routin
3f340 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  e...  */..  if( 
3f341 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f342 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74  fig.isInit ) ret
3f343 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
3f344 0d 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
3f345 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
3f346 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
3f347 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c 65 20  zed.  If unable 
3f348 74 6f 20 0d 0a 20 20 2a 2a 20 69 6e 69 74 69 61  to ..  ** initia
3f349 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
3f34a 75 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72 6e  ubsystem, return
3f34b 20 65 61 72 6c 79 20 77 69 74 68 20 74 68 65 20   early with the 
3f34c 65 72 72 6f 72 2e 0d 0a 20 20 2a 2a 20 49 66 20  error...  ** If 
3f34d 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f  the system is so
3f34e 20 73 69 63 6b 20 74 68 61 74 20 77 65 20 61 72   sick that we ar
3f34f 65 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  e unable to allo
3f350 63 61 74 65 20 61 20 6d 75 74 65 78 2c 0d 0a 20  cate a mutex,.. 
3f351 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74   ** there is not
3f352 20 6d 75 63 68 20 53 51 4c 69 74 65 20 69 73 20   much SQLite is 
3f353 67 6f 69 6e 67 20 74 6f 20 62 65 20 61 62 6c 65  going to be able
3f354 20 74 6f 20 64 6f 2e 0d 0a 20 20 2a 2a 0d 0a 20   to do...  **.. 
3f355 20 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 73 75   ** The mutex su
3f356 62 73 79 73 74 65 6d 20 6d 75 73 74 20 74 61 6b  bsystem must tak
3f357 65 20 63 61 72 65 20 6f 66 20 73 65 72 69 61 6c  e care of serial
3f358 69 7a 69 6e 67 20 69 74 73 20 6f 77 6e 0d 0a 20  izing its own.. 
3f359 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69   ** initializati
3f35a 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 72 63 20  on...  */..  rc 
3f35b 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e  = sqlite3MutexIn
3f35c 69 74 28 29 3b 0d 0a 20 20 69 66 28 20 72 63 20  it();..  if( rc 
3f35d 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a  ) return rc;....
3f35e 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
3f35f 74 68 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73  the malloc() sys
3f360 74 65 6d 20 61 6e 64 20 74 68 65 20 72 65 63 75  tem and the recu
3f361 72 73 69 76 65 20 70 49 6e 69 74 4d 75 74 65 78  rsive pInitMutex
3f362 20 6d 75 74 65 78 2e 0d 0a 20 20 2a 2a 20 54 68   mutex...  ** Th
3f363 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  is operation is 
3f364 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
3f365 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
3f366 75 74 65 78 2e 20 20 4e 6f 74 65 20 74 68 61 74  utex.  Note that
3f367 0d 0a 20 20 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f  ..  ** MutexAllo
3f368 63 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 6f  c() is called fo
3f369 72 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  r a static mutex
3f36a 20 70 72 69 6f 72 20 74 6f 20 69 6e 69 74 69 61   prior to initia
3f36b 6c 69 7a 69 6e 67 20 74 68 65 0d 0a 20 20 2a 2a  lizing the..  **
3f36c 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65   malloc subsyste
3f36d 6d 20 2d 20 74 68 69 73 20 69 6d 70 6c 69 65 73  m - this implies
3f36e 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61   that the alloca
3f36f 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74 69 63  tion of a static
3f370 0d 0a 20 20 2a 2a 20 6d 75 74 65 78 20 6d 75 73  ..  ** mutex mus
3f371 74 20 6e 6f 74 20 72 65 71 75 69 72 65 20 73 75  t not require su
3f372 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20 6d  pport from the m
3f373 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 2e  alloc subsystem.
3f374 0d 0a 20 20 2a 2f 0d 0a 20 20 4d 55 54 45 58 5f  ..  */..  MUTEX_
3f375 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20 3d  LOGIC( pMaster =
3f376 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
3f377 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
3f378 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 20  STATIC_MASTER); 
3f379 29 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  )..  sqlite3_mut
3f37a 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72  ex_enter(pMaster
3f37b 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f  );..  sqlite3Glo
3f37c 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65  balConfig.isMute
3f37d 78 49 6e 69 74 20 3d 20 31 3b 0d 0a 20 20 69 66  xInit = 1;..  if
3f37e 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
3f37f 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49  Config.isMallocI
3f380 6e 69 74 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  nit ){..    rc =
3f381 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e   sqlite3MallocIn
3f382 69 74 28 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  it();..  }..  if
3f383 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3f384 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ){..    sqlite3G
3f385 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61  lobalConfig.isMa
3f386 6c 6c 6f 63 49 6e 69 74 20 3d 20 31 3b 0d 0a 20  llocInit = 1;.. 
3f387 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47     if( !sqlite3G
3f388 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
3f389 74 4d 75 74 65 78 20 29 7b 0d 0a 20 20 20 20 20  tMutex ){..     
3f38a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3f38b 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20  nfig.pInitMutex 
3f38c 3d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 73 71  =..           sq
3f38d 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
3f38e 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
3f38f 55 52 53 49 56 45 29 3b 0d 0a 20 20 20 20 20 20  URSIVE);..      
3f390 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
3f391 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
3f392 65 78 20 26 26 20 21 73 71 6c 69 74 65 33 47 6c  ex && !sqlite3Gl
3f393 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
3f394 4d 75 74 65 78 20 29 7b 0d 0a 20 20 20 20 20 20  Mutex ){..      
3f395 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3f396 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  MEM;..      }.. 
3f397 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28     }..  }..  if(
3f398 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3f399 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  {..    sqlite3Gl
3f39a 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49  obalConfig.nRefI
3f39b 6e 69 74 4d 75 74 65 78 2b 2b 3b 0d 0a 20 20 7d  nitMutex++;..  }
3f39c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
3f39d 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
3f39e 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 72 63 20  ;....  /* If rc 
3f39f 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 4b  is not SQLITE_OK
3f3a0 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   at this point, 
3f3a1 74 68 65 6e 20 65 69 74 68 65 72 20 74 68 65 20  then either the 
3f3a2 6d 61 6c 6c 6f 63 0d 0a 20 20 2a 2a 20 73 75 62  malloc..  ** sub
3f3a3 73 79 73 74 65 6d 20 63 6f 75 6c 64 20 6e 6f 74  system could not
3f3a4 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
3f3a5 6f 72 20 74 68 65 20 73 79 73 74 65 6d 20 66 61  or the system fa
3f3a6 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  iled to allocate
3f3a7 0d 0a 20 20 2a 2a 20 74 68 65 20 70 49 6e 69 74  ..  ** the pInit
3f3a8 4d 75 74 65 78 20 6d 75 74 65 78 2e 20 52 65 74  Mutex mutex. Ret
3f3a9 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  urn an error in 
3f3aa 65 69 74 68 65 72 20 63 61 73 65 2e 20 20 2a 2f  either case.  */
3f3ab 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
3f3ac 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 65  TE_OK ){..    re
3f3ad 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d  turn rc;..  }...
3f3ae 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 72 65 73  .  /* Do the res
3f3af 74 20 6f 66 20 74 68 65 20 69 6e 69 74 69 61 6c  t of the initial
3f3b0 69 7a 61 74 69 6f 6e 20 75 6e 64 65 72 20 74 68  ization under th
3f3b1 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  e recursive mute
3f3b2 78 20 73 6f 0d 0a 20 20 2a 2a 20 74 68 61 74 20  x so..  ** that 
3f3b3 77 65 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20  we will be able 
3f3b4 74 6f 20 68 61 6e 64 6c 65 20 72 65 63 75 72 73  to handle recurs
3f3b5 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0d 0a  ive calls into..
3f3b6 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69    ** sqlite3_ini
3f3b7 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68 65 20  tialize().  The 
3f3b8 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  recursive calls 
3f3b9 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20 74 68  normally come th
3f3ba 72 6f 75 67 68 0d 0a 20 20 2a 2a 20 73 71 6c 69  rough..  ** sqli
3f3bb 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77 68  te3_os_init() wh
3f3bc 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73 71  en it invokes sq
3f3bd 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
3f3be 65 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72 0d  er(), but other.
3f3bf 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20  .  ** recursive 
3f3c0 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f  calls might also
3f3c1 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 20   be possible... 
3f3c2 20 2a 2a 0d 0a 20 20 2a 2a 20 49 4d 50 4c 45 4d   **..  ** IMPLEM
3f3c3 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 30  ENTATION-OF: R-0
3f3c4 30 31 34 30 2d 33 37 34 34 35 20 53 51 4c 69 74  0140-37445 SQLit
3f3c5 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
3f3c6 73 65 72 69 61 6c 69 7a 65 73 20 63 61 6c 6c 73  serializes calls
3f3c7 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 49  ..  ** to the xI
3f3c8 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74  nit method, so t
3f3c9 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20  he xInit method 
3f3ca 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65  need not be thre
3f3cb 61 64 73 61 66 65 2e 0d 0a 20 20 2a 2a 0d 0a 20  adsafe...  **.. 
3f3cc 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** The followin
3f3cd 67 20 6d 75 74 65 78 20 69 73 20 77 68 61 74 20  g mutex is what 
3f3ce 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73  serializes acces
3f3cf 73 20 74 6f 20 74 68 65 20 61 70 70 64 65 66 20  s to the appdef 
3f3d0 70 63 61 63 68 65 20 78 49 6e 69 74 0d 0a 20 20  pcache xInit..  
3f3d1 2a 2a 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65  ** methods.  The
3f3d2 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
3f3d3 6d 65 74 68 6f 64 73 2e 78 49 6e 69 74 28 29 20  methods.xInit() 
3f3d4 61 6c 6c 20 69 73 20 65 6d 62 65 64 64 65 64 20  all is embedded 
3f3d5 69 6e 20 74 68 65 0d 0a 20 20 2a 2a 20 63 61 6c  in the..  ** cal
3f3d6 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63  l to sqlite3Pcac
3f3d7 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0d  heInitialize()..
3f3d8 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  .  */..  sqlite3
3f3d9 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
3f3da 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3f3db 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0d 0a 20  .pInitMutex);.. 
3f3dc 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
3f3dd 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 3d  alConfig.isInit=
3f3de 3d 30 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  =0 && sqlite3Glo
3f3df 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67  balConfig.inProg
3f3e0 72 65 73 73 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  ress==0 ){..    
3f3e1 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
3f3e2 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e  sh = &GLOBAL(Fun
3f3e3 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65  cDefHash, sqlite
3f3e4 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  3GlobalFunctions
3f3e5 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 47  );..    sqlite3G
3f3e6 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72  lobalConfig.inPr
3f3e7 6f 67 72 65 73 73 20 3d 20 31 3b 0d 0a 20 20 20  ogress = 1;..   
3f3e8 20 6d 65 6d 73 65 74 28 70 48 61 73 68 2c 20 30   memset(pHash, 0
3f3e9 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  , sizeof(sqlite3
3f3ea 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29  GlobalFunctions)
3f3eb 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 52  );..    sqlite3R
3f3ec 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e  egisterGlobalFun
3f3ed 63 74 69 6f 6e 73 28 29 3b 0d 0a 20 20 20 20 69  ctions();..    i
3f3ee 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3f3ef 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49  Config.isPCacheI
3f3f0 6e 69 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  nit==0 ){..     
3f3f1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61   rc = sqlite3Pca
3f3f2 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b  cheInitialize();
3f3f3 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
3f3f4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3f3f5 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
3f3f6 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50  GlobalConfig.isP
3f3f7 43 61 63 68 65 49 6e 69 74 20 3d 20 31 3b 0d 0a  CacheInit = 1;..
3f3f8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
3f3f9 65 33 4f 73 49 6e 69 74 28 29 3b 0d 0a 20 20 20  e3OsInit();..   
3f3fa 20 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   }..    if( rc==
3f3fb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
3f3fc 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68      sqlite3PCach
3f3fd 65 42 75 66 66 65 72 53 65 74 75 70 28 20 73 71  eBufferSetup( sq
3f3fe 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f3ff 67 2e 70 50 61 67 65 2c 20 0d 0a 20 20 20 20 20  g.pPage, ..     
3f400 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3f401 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c  alConfig.szPage,
3f402 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3f403 6e 66 69 67 2e 6e 50 61 67 65 29 3b 0d 0a 20 20  nfig.nPage);..  
3f404 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3f405 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d  lConfig.isInit =
3f406 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20   1;..    }..    
3f407 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f408 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d  fig.inProgress =
3f409 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69   0;..  }..  sqli
3f40a 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
3f40b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f40c 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b  fig.pInitMutex);
3f40d 0d 0a 0d 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b  ....  /* Go back
3f40e 20 75 6e 64 65 72 20 74 68 65 20 73 74 61 74 69   under the stati
3f40f 63 20 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61  c mutex and clea
3f410 6e 20 75 70 20 74 68 65 20 72 65 63 75 72 73 69  n up the recursi
3f411 76 65 0d 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74  ve..  ** mutex t
3f412 6f 20 70 72 65 76 65 6e 74 20 61 20 72 65 73 6f  o prevent a reso
3f413 75 72 63 65 20 6c 65 61 6b 2e 0d 0a 20 20 2a 2f  urce leak...  */
3f414 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
3f415 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29  x_enter(pMaster)
3f416 3b 0d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  ;..  sqlite3Glob
3f417 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69  alConfig.nRefIni
3f418 74 4d 75 74 65 78 2d 2d 3b 0d 0a 20 20 69 66 28  tMutex--;..  if(
3f419 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3f41a 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74  nfig.nRefInitMut
3f41b 65 78 3c 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73  ex<=0 ){..    as
3f41c 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
3f41d 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e  balConfig.nRefIn
3f41e 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b 0d 0a 20  itMutex==0 );.. 
3f41f 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
3f420 5f 66 72 65 65 28 73 71 6c 69 74 65 33 47 6c 6f  _free(sqlite3Glo
3f421 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d  balConfig.pInitM
3f422 75 74 65 78 29 3b 0d 0a 20 20 20 20 73 71 6c 69  utex);..    sqli
3f423 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3f424 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b 0d  pInitMutex = 0;.
3f425 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
3f426 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73  mutex_leave(pMas
3f427 74 65 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68  ter);....  /* Th
3f428 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 6a  e following is j
3f429 75 73 74 20 61 20 73 61 6e 69 74 79 20 63 68 65  ust a sanity che
3f42a 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
3f42b 53 51 4c 69 74 65 20 68 61 73 0d 0a 20 20 2a 2a  SQLite has..  **
3f42c 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65 64 20 63   been compiled c
3f42d 6f 72 72 65 63 74 6c 79 2e 20 20 49 74 20 69 73  orrectly.  It is
3f42e 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 72 75   important to ru
3f42f 6e 20 74 68 69 73 20 63 6f 64 65 2c 20 62 75 74  n this code, but
3f430 0d 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20  ..  ** we don't 
3f431 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74 20 74  want to run it t
3f432 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73 6f 61  oo often and soa
3f433 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65 73 20  k up CPU cycles 
3f434 66 6f 72 20 6e 6f 0d 0a 20 20 2a 2a 20 72 65 61  for no..  ** rea
3f435 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e 20  son.  So we run 
3f436 69 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 69  it once during i
3f437 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0d 0a  nitialization...
3f438 20 20 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 4e 44    */..#ifndef ND
3f439 45 42 55 47 0d 0a 23 69 66 6e 64 65 66 20 53 51  EBUG..#ifndef SQ
3f43a 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
3f43b 4e 47 5f 50 4f 49 4e 54 0d 0a 20 20 2f 2a 20 54  NG_POINT..  /* T
3f43c 68 69 73 20 73 65 63 74 69 6f 6e 20 6f 66 20 63  his section of c
3f43d 6f 64 65 27 73 20 6f 6e 6c 79 20 22 6f 75 74 70  ode's only "outp
3f43e 75 74 22 20 69 73 20 76 69 61 20 61 73 73 65 72  ut" is via asser
3f43f 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  t() statements. 
3f440 2a 2f 0d 0a 20 20 69 66 20 28 20 72 63 3d 3d 53  */..  if ( rc==S
3f441 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
3f442 20 75 36 34 20 78 20 3d 20 28 28 28 75 36 34 29   u64 x = (((u64)
3f443 31 29 3c 3c 36 33 29 2d 31 3b 0d 0a 20 20 20 20  1)<<63)-1;..    
3f444 64 6f 75 62 6c 65 20 79 3b 0d 0a 20 20 20 20 61  double y;..    a
3f445 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29 3d  ssert(sizeof(x)=
3f446 3d 38 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74  =8);..    assert
3f447 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65  (sizeof(x)==size
3f448 6f 66 28 79 29 29 3b 0d 0a 20 20 20 20 6d 65 6d  of(y));..    mem
3f449 63 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0d  cpy(&y, &x, 8);.
3f44a 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
3f44b 69 74 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0d  ite3IsNaN(y) );.
3f44c 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 23 65  .  }..#endif..#e
3f44d 6e 64 69 66 0d 0a 0d 0a 20 20 2f 2a 20 44 6f 20  ndif....  /* Do 
3f44e 65 78 74 72 61 20 69 6e 69 74 69 61 6c 69 7a 61  extra initializa
3f44f 74 69 6f 6e 20 73 74 65 70 73 20 72 65 71 75 65  tion steps reque
3f450 73 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  sted by the SQLI
3f451 54 45 5f 45 58 54 52 41 5f 49 4e 49 54 0d 0a 20  TE_EXTRA_INIT.. 
3f452 20 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   ** compile-time
3f453 20 6f 70 74 69 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a   option...  */..
3f454 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 58  #ifdef SQLITE_EX
3f455 54 52 41 5f 49 4e 49 54 0d 0a 20 20 69 66 28 20  TRA_INIT..  if( 
3f456 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3f457 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3f458 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 7b 0d 0a  nfig.isInit ){..
3f459 20 20 20 20 69 6e 74 20 53 51 4c 49 54 45 5f 45      int SQLITE_E
3f45a 58 54 52 41 5f 49 4e 49 54 28 63 6f 6e 73 74 20  XTRA_INIT(const 
3f45b 63 68 61 72 2a 29 3b 0d 0a 20 20 20 20 72 63 20  char*);..    rc 
3f45c 3d 20 53 51 4c 49 54 45 5f 45 58 54 52 41 5f 49  = SQLITE_EXTRA_I
3f45d 4e 49 54 28 30 29 3b 0d 0a 20 20 7d 0d 0a 23 65  NIT(0);..  }..#e
3f45e 6e 64 69 66 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  ndif....  return
3f45f 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
3f460 2a 20 55 6e 64 6f 20 74 68 65 20 65 66 66 65 63  * Undo the effec
3f461 74 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  ts of sqlite3_in
3f462 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73  itialize().  Mus
3f463 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  t not be called 
3f464 77 68 69 6c 65 0d 0a 2a 2a 20 74 68 65 72 65 20  while..** there 
3f465 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  are outstanding 
3f466 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3f467 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61  ions or memory a
3f468 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0d 0a 2a  llocations or..*
3f469 2a 20 77 68 69 6c 65 20 61 6e 79 20 70 61 72 74  * while any part
3f46a 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 6f 74   of SQLite is ot
3f46b 68 65 72 77 69 73 65 20 69 6e 20 75 73 65 20 69  herwise in use i
3f46c 6e 20 61 6e 79 20 74 68 72 65 61 64 2e 20 20 54  n any thread.  T
3f46d 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  his..** routine 
3f46e 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
3f46f 65 2e 20 20 42 75 74 20 69 74 20 69 73 20 73 61  e.  But it is sa
3f470 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69  fe to invoke thi
3f471 73 20 72 6f 75 74 69 6e 65 0d 0a 2a 2a 20 6f 6e  s routine..** on
3f472 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
3f473 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77  already shut dow
3f474 6e 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73  n.  If SQLite is
3f475 20 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f   already shut do
3f476 77 6e 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73  wn..** when this
3f477 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f   routine is invo
3f478 6b 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ked, then this r
3f479 6f 75 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d  outine is a harm
3f47a 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d  less no-op...*/.
3f47b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3f47c 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
3f47d 28 76 6f 69 64 29 7b 0d 0a 20 20 69 66 28 20 73  (void){..  if( s
3f47e 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3f47f 69 67 2e 69 73 49 6e 69 74 20 29 7b 0d 0a 23 69  ig.isInit ){..#i
3f480 66 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54 52  fdef SQLITE_EXTR
3f481 41 5f 53 48 55 54 44 4f 57 4e 0d 0a 20 20 20 20  A_SHUTDOWN..    
3f482 76 6f 69 64 20 53 51 4c 49 54 45 5f 45 58 54 52  void SQLITE_EXTR
3f483 41 5f 53 48 55 54 44 4f 57 4e 28 76 6f 69 64 29  A_SHUTDOWN(void)
3f484 3b 0d 0a 20 20 20 20 53 51 4c 49 54 45 5f 45 58  ;..    SQLITE_EX
3f485 54 52 41 5f 53 48 55 54 44 4f 57 4e 28 29 3b 0d  TRA_SHUTDOWN();.
3f486 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 73 71 6c  .#endif..    sql
3f487 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0d 0a  ite3_os_end();..
3f488 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
3f489 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  t_auto_extension
3f48a 28 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ();..    sqlite3
3f48b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
3f48c 6e 69 74 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  nit = 0;..  }.. 
3f48d 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
3f48e 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
3f48f 65 49 6e 69 74 20 29 7b 0d 0a 20 20 20 20 73 71  eInit ){..    sq
3f490 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64  lite3PcacheShutd
3f491 6f 77 6e 28 29 3b 0d 0a 20 20 20 20 73 71 6c 69  own();..    sqli
3f492 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3f493 69 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20 30  isPCacheInit = 0
3f494 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 73 71  ;..  }..  if( sq
3f495 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f496 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29  g.isMallocInit )
3f497 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61  {..    sqlite3Ma
3f498 6c 6c 6f 63 45 6e 64 28 29 3b 0d 0a 20 20 20 20  llocEnd();..    
3f499 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f49a 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  fig.isMallocInit
3f49b 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66   = 0;..  }..  if
3f49c 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
3f49d 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e 69  onfig.isMutexIni
3f49e 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  t ){..    sqlite
3f49f 33 4d 75 74 65 78 45 6e 64 28 29 3b 0d 0a 20 20  3MutexEnd();..  
3f4a0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3f4a1 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e 69  onfig.isMutexIni
3f4a2 74 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  t = 0;..  }.... 
3f4a3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3f4a4 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
3f4a5 54 68 69 73 20 41 50 49 20 61 6c 6c 6f 77 73 20  This API allows 
3f4a6 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20  applications to 
3f4a7 6d 6f 64 69 66 79 20 74 68 65 20 67 6c 6f 62 61  modify the globa
3f4a8 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
3f4a9 6f 66 0d 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  of..** the SQLit
3f4aa 65 20 6c 69 62 72 61 72 79 20 61 74 20 72 75 6e  e library at run
3f4ab 2d 74 69 6d 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  -time...**..** T
3f4ac 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
3f4ad 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
3f4ae 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  d when there are
3f4af 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 0d   no outstanding.
3f4b0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
3f4b1 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f  nections or memo
3f4b2 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20  ry allocations. 
3f4b3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
3f4b4 20 6e 6f 74 0d 0a 2a 2a 20 74 68 72 65 61 64 73   not..** threads
3f4b5 61 66 65 2e 20 20 46 61 69 6c 75 72 65 20 74 6f  afe.  Failure to
3f4b6 20 68 65 65 64 20 74 68 65 73 65 20 77 61 72 6e   heed these warn
3f4b7 69 6e 67 73 20 63 61 6e 20 6c 65 61 64 20 74 6f  ings can lead to
3f4b8 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 0d 0a   unpredictable..
3f4b9 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0d 0a 2a 2f  ** behavior...*/
3f4ba 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
3f4bb 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
3f4bc 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0d 0a 20  int op, ...){.. 
3f4bd 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20 20   va_list ap;..  
3f4be 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3f4bf 4f 4b 3b 0d 0a 0d 0a 20 20 2f 2a 20 73 71 6c 69  OK;....  /* sqli
3f4c0 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 73 68 61  te3_config() sha
3f4c1 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ll return SQLITE
3f4c2 5f 4d 49 53 55 53 45 20 69 66 20 69 74 20 69 73  _MISUSE if it is
3f4c3 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0d 0a   invoked while..
3f4c4 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20    ** the SQLite 
3f4c5 6c 69 62 72 61 72 79 20 69 73 20 69 6e 20 75 73  library is in us
3f4c6 65 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  e. */..  if( sql
3f4c7 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3f4c8 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e  .isInit ) return
3f4c9 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42   SQLITE_MISUSE_B
3f4ca 4b 50 54 3b 0d 0a 0d 0a 20 20 76 61 5f 73 74 61  KPT;....  va_sta
3f4cb 72 74 28 61 70 2c 20 6f 70 29 3b 0d 0a 20 20 73  rt(ap, op);..  s
3f4cc 77 69 74 63 68 28 20 6f 70 20 29 7b 0d 0a 0d 0a  witch( op ){....
3f4cd 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
3f4ce 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
3f4cf 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69  ns are only avai
3f4d0 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72 65 61  lable in a threa
3f4d1 64 73 61 66 65 0d 0a 20 20 20 20 2a 2a 20 63 6f  dsafe..    ** co
3f4d2 6d 70 69 6c 65 2e 20 0d 0a 20 20 20 20 2a 2f 0d  mpile. ..    */.
3f4d3 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
3f4d4 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 20  ITE_THREADSAFE) 
3f4d5 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
3f4d6 53 41 46 45 3e 30 0d 0a 20 20 20 20 63 61 73 65  SAFE>0..    case
3f4d7 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53   SQLITE_CONFIG_S
3f4d8 49 4e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0d 0a  INGLETHREAD: {..
3f4d9 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
3f4da 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f   all mutexing */
3f4db 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ..      sqlite3G
3f4dc 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
3f4dd 65 4d 75 74 65 78 20 3d 20 30 3b 0d 0a 20 20 20  eMutex = 0;..   
3f4de 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3f4df 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
3f4e0 78 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 62 72  x = 0;..      br
3f4e1 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
3f4e2 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
3f4e3 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3a  FIG_MULTITHREAD:
3f4e4 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 44 69 73   {..      /* Dis
3f4e5 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66  able mutexing of
3f4e6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3f4e7 74 69 6f 6e 73 20 2a 2f 0d 0a 20 20 20 20 20 20  tions */..      
3f4e8 2f 2a 20 45 6e 61 62 6c 65 20 6d 75 74 65 78 69  /* Enable mutexi
3f4e9 6e 67 20 6f 66 20 63 6f 72 65 20 64 61 74 61 20  ng of core data 
3f4ea 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0d 0a 20  structures */.. 
3f4eb 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3f4ec 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
3f4ed 74 65 78 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20  tex = 1;..      
3f4ee 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f4ef 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
3f4f0 20 30 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b   0;..      break
3f4f1 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
3f4f2 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
3f4f3 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0d 0a  _SERIALIZED: {..
3f4f4 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
3f4f5 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0d  all mutexing */.
3f4f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3f4f7 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
3f4f8 4d 75 74 65 78 20 3d 20 31 3b 0d 0a 20 20 20 20  Mutex = 1;..    
3f4f9 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3f4fa 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
3f4fb 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 62 72 65   = 1;..      bre
3f4fc 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
3f4fd 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
3f4fe 49 47 5f 4d 55 54 45 58 3a 20 7b 0d 0a 20 20 20  IG_MUTEX: {..   
3f4ff 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e     /* Specify an
3f500 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 75 74   alternative mut
3f501 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
3f502 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69  n */..      sqli
3f503 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3f504 6d 75 74 65 78 20 3d 20 2a 76 61 5f 61 72 67 28  mutex = *va_arg(
3f505 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ap, sqlite3_mute
3f506 78 5f 6d 65 74 68 6f 64 73 2a 29 3b 0d 0a 20 20  x_methods*);..  
3f507 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3f508 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  }..    case SQLI
3f509 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54  TE_CONFIG_GETMUT
3f50a 45 58 3a 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  EX: {..      /* 
3f50b 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72  Retrieve the cur
3f50c 72 65 6e 74 20 6d 75 74 65 78 20 69 6d 70 6c 65  rent mutex imple
3f50d 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20  mentation */..  
3f50e 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20      *va_arg(ap, 
3f50f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3f510 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65  thods*) = sqlite
3f511 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
3f512 74 65 78 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  tex;..      brea
3f513 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69  k;..    }..#endi
3f514 66 0d 0a 0d 0a 0d 0a 20 20 20 20 63 61 73 65 20  f......    case 
3f515 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41  SQLITE_CONFIG_MA
3f516 4c 4c 4f 43 3a 20 7b 0d 0a 20 20 20 20 20 20 2f  LLOC: {..      /
3f517 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
3f518 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20  ernative malloc 
3f519 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
3f51a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  /..      sqlite3
3f51b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d  GlobalConfig.m =
3f51c 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
3f51d 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
3f51e 2a 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  *);..      break
3f51f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 63 61  ;..    }..    ca
3f520 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
3f521 5f 47 45 54 4d 41 4c 4c 4f 43 3a 20 7b 0d 0a 20  _GETMALLOC: {.. 
3f522 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65       /* Retrieve
3f523 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 6c   the current mal
3f524 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61  loc() implementa
3f525 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69  tion */..      i
3f526 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3f527 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
3f528 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 4d 65 6d  ==0 ) sqlite3Mem
3f529 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0d 0a 20  SetDefault();.. 
3f52a 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c       *va_arg(ap,
3f52b 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
3f52c 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33  hods*) = sqlite3
3f52d 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 3b 0d  GlobalConfig.m;.
3f52e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
3f52f 20 20 20 7d 0d 0a 20 20 20 20 63 61 73 65 20 53     }..    case S
3f530 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d  QLITE_CONFIG_MEM
3f531 53 54 41 54 55 53 3a 20 7b 0d 0a 20 20 20 20 20  STATUS: {..     
3f532 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69   /* Enable or di
3f533 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c 6f 63  sable the malloc
3f534 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 69   status collecti
3f535 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c  on */..      sql
3f536 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3f537 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f 61  .bMemstat = va_a
3f538 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d 0a 20 20  rg(ap, int);..  
3f539 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3f53a 7d 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  }..    case SQLI
3f53b 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
3f53c 48 3a 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 44  H: {..      /* D
3f53d 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
3f53e 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65  r for scratch me
3f53f 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0d 0a 20  mory space */.. 
3f540 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3f541 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
3f542 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  h = va_arg(ap, v
3f543 6f 69 64 2a 29 3b 0d 0a 20 20 20 20 20 20 73 71  oid*);..      sq
3f544 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f545 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 76 61  g.szScratch = va
3f546 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d 0a  _arg(ap, int);..
3f547 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
3f548 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74  balConfig.nScrat
3f549 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ch = va_arg(ap, 
3f54a 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  int);..      bre
3f54b 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ak;..    }..    
3f54c 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
3f54d 49 47 5f 50 41 47 45 43 41 43 48 45 3a 20 7b 0d  IG_PAGECACHE: {.
3f54e 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e  .      /* Design
3f54f 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72  ate a buffer for
3f550 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f   page cache memo
3f551 72 79 20 73 70 61 63 65 20 2a 2f 0d 0a 20 20 20  ry space */..   
3f552 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3f553 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 76  Config.pPage = v
3f554 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29  a_arg(ap, void*)
3f555 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
3f556 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
3f557 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
3f558 20 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 73 71   int);..      sq
3f559 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f55a 67 2e 6e 50 61 67 65 20 3d 20 76 61 5f 61 72 67  g.nPage = va_arg
3f55b 28 61 70 2c 20 69 6e 74 29 3b 0d 0a 20 20 20 20  (ap, int);..    
3f55c 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
3f55d 0a 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ...    case SQLI
3f55e 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
3f55f 3a 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 6e 6f  : {..      /* no
3f560 2d 6f 70 20 2a 2f 0d 0a 20 20 20 20 20 20 62 72  -op */..      br
3f561 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  eak;..    }..   
3f562 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
3f563 46 49 47 5f 47 45 54 50 43 41 43 48 45 3a 20 7b  FIG_GETPCACHE: {
3f564 0d 0a 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61  ..      /* now a
3f565 6e 20 65 72 72 6f 72 20 2a 2f 0d 0a 20 20 20 20  n error */..    
3f566 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
3f567 52 4f 52 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  ROR;..      brea
3f568 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  k;..    }....   
3f569 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
3f56a 46 49 47 5f 50 43 41 43 48 45 32 3a 20 7b 0d 0a  FIG_PCACHE2: {..
3f56b 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79        /* Specify
3f56c 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20   an alternative 
3f56d 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
3f56e 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20  mentation */..  
3f56f 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3f570 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 20  lConfig.pcache2 
3f571 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71  = *va_arg(ap, sq
3f572 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
3f573 68 6f 64 73 32 2a 29 3b 0d 0a 20 20 20 20 20 20  hods2*);..      
3f574 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
3f575 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
3f576 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 32  ONFIG_GETPCACHE2
3f577 3a 20 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 73  : {..      if( s
3f578 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3f579 69 67 2e 70 63 61 63 68 65 32 2e 78 49 6e 69 74  ig.pcache2.xInit
3f57a 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
3f57b 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74  sqlite3PCacheSet
3f57c 44 65 66 61 75 6c 74 28 29 3b 0d 0a 20 20 20 20  Default();..    
3f57d 20 20 7d 0d 0a 20 20 20 20 20 20 2a 76 61 5f 61    }..      *va_a
3f57e 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70  rg(ap, sqlite3_p
3f57f 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 2a 29  cache_methods2*)
3f580 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
3f581 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 3b 0d  Config.pcache2;.
3f582 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
3f583 20 20 20 7d 0d 0a 0d 0a 23 69 66 20 64 65 66 69     }....#if defi
3f584 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3f585 45 5f 4d 45 4d 53 59 53 33 29 20 7c 7c 20 64 65  E_MEMSYS3) || de
3f586 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
3f587 42 4c 45 5f 4d 45 4d 53 59 53 35 29 0d 0a 20 20  BLE_MEMSYS5)..  
3f588 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
3f589 4e 46 49 47 5f 48 45 41 50 3a 20 7b 0d 0a 20 20  NFIG_HEAP: {..  
3f58a 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65      /* Designate
3f58b 20 61 20 62 75 66 66 65 72 20 66 6f 72 20 68 65   a buffer for he
3f58c 61 70 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  ap memory space 
3f58d 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  */..      sqlite
3f58e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48  3GlobalConfig.pH
3f58f 65 61 70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  eap = va_arg(ap,
3f590 20 76 6f 69 64 2a 29 3b 0d 0a 20 20 20 20 20 20   void*);..      
3f591 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f592 66 69 67 2e 6e 48 65 61 70 20 3d 20 76 61 5f 61  fig.nHeap = va_a
3f593 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d 0a 20 20  rg(ap, int);..  
3f594 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3f595 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 20 3d 20  lConfig.mnReq = 
3f596 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
3f597 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71  ....      if( sq
3f598 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f599 67 2e 6d 6e 52 65 71 3c 31 20 29 7b 0d 0a 20 20  g.mnReq<1 ){..  
3f59a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
3f59b 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 20  balConfig.mnReq 
3f59c 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73  = 1;..      }els
3f59d 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f  e if( sqlite3Glo
3f59e 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 3e  balConfig.mnReq>
3f59f 28 31 3c 3c 31 32 29 20 29 7b 0d 0a 20 20 20 20  (1<<12) ){..    
3f5a0 20 20 20 20 2f 2a 20 63 61 70 20 6d 69 6e 20 72      /* cap min r
3f5a1 65 71 75 65 73 74 20 73 69 7a 65 20 61 74 20 32  equest size at 2
3f5a2 5e 31 32 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ^12 */..        
3f5a3 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3f5a4 66 69 67 2e 6d 6e 52 65 71 20 3d 20 28 31 3c 3c  fig.mnReq = (1<<
3f5a5 31 32 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d  12);..      }...
3f5a6 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
3f5a7 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
3f5a8 48 65 61 70 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Heap==0 ){..    
3f5a9 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 65      /* If the he
3f5aa 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20 4e 55  ap pointer is NU
3f5ab 4c 4c 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65  LL, then restore
3f5ac 20 74 68 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c   the malloc impl
3f5ad 65 6d 65 6e 74 61 74 69 6f 6e 0d 0a 20 20 20 20  ementation..    
3f5ae 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4e      ** back to N
3f5af 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 74 6f 6f  ULL pointers too
3f5b0 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75  .  This will cau
3f5b1 73 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 74 6f  se the malloc to
3f5b2 20 67 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20   go..        ** 
3f5b3 62 61 63 6b 20 74 6f 20 69 74 73 20 64 65 66 61  back to its defa
3f5b4 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ult implementati
3f5b5 6f 6e 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f  on when sqlite3_
3f5b6 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 0d  initialize() is.
3f5b7 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 75 6e 2e  .        ** run.
3f5b8 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20  ..        */..  
3f5b9 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73 71        memset(&sq
3f5ba 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3f5bb 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  g.m, 0, sizeof(s
3f5bc 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3f5bd 69 67 2e 6d 29 29 3b 0d 0a 20 20 20 20 20 20 7d  ig.m));..      }
3f5be 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 2f  else{..        /
3f5bf 2a 20 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74  * The heap point
3f5c0 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  er is not NULL, 
3f5c1 74 68 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65  then install one
3f5c2 20 6f 66 20 74 68 65 0d 0a 20 20 20 20 20 20 20   of the..       
3f5c3 20 2a 2a 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e   ** mem5.c/mem3.
3f5c4 63 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 6e 65  c methods. If ne
3f5c5 69 74 68 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d  ither ENABLE_MEM
3f5c6 53 59 53 33 20 6e 6f 72 0d 0a 20 20 20 20 20 20  SYS3 nor..      
3f5c7 20 20 2a 2a 20 45 4e 41 42 4c 45 5f 4d 45 4d 53    ** ENABLE_MEMS
3f5c8 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2c 20  YS5 is defined, 
3f5c9 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
3f5ca 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 23 69  ..        */..#i
3f5cb 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3f5cc 4c 45 5f 4d 45 4d 53 59 53 33 0d 0a 20 20 20 20  LE_MEMSYS3..    
3f5cd 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3f5ce 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c  lConfig.m = *sql
3f5cf 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
3f5d0 33 28 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  3();..#endif..#i
3f5d1 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3f5d2 4c 45 5f 4d 45 4d 53 59 53 35 0d 0a 20 20 20 20  LE_MEMSYS5..    
3f5d3 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3f5d4 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c  lConfig.m = *sql
3f5d5 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
3f5d6 35 28 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20  5();..#endif..  
3f5d7 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65      }..      bre
3f5d8 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64  ak;..    }..#end
3f5d9 69 66 0d 0a 0d 0a 20 20 20 20 63 61 73 65 20 53  if....    case S
3f5da 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
3f5db 4b 41 53 49 44 45 3a 20 7b 0d 0a 20 20 20 20 20  KASIDE: {..     
3f5dc 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3f5dd 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65  nfig.szLookaside
3f5de 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
3f5df 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  t);..      sqlit
3f5e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
3f5e1 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61  Lookaside = va_a
3f5e2 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d 0a 20 20  rg(ap, int);..  
3f5e3 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
3f5e4 7d 0d 0a 20 20 20 20 0d 0a 20 20 20 20 2f 2a 20  }..    ..    /* 
3f5e5 52 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72  Record a pointer
3f5e6 20 74 6f 20 74 68 65 20 6c 6f 67 67 65 72 20 66   to the logger f
3f5e7 75 6e 63 63 74 69 6f 6e 20 61 6e 64 20 69 74 73  uncction and its
3f5e8 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
3f5e9 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 64 65 66  ..    ** The def
3f5ea 61 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 20 4c  ault is NULL.  L
3f5eb 6f 67 67 69 6e 67 20 69 73 20 64 69 73 61 62 6c  ogging is disabl
3f5ec 65 64 20 69 66 20 74 68 65 20 66 75 6e 63 74 69  ed if the functi
3f5ed 6f 6e 20 70 6f 69 6e 74 65 72 20 69 73 0d 0a 20  on pointer is.. 
3f5ee 20 20 20 2a 2a 20 4e 55 4c 4c 2e 0d 0a 20 20 20     ** NULL...   
3f5ef 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 51   */..    case SQ
3f5f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47 3a  LITE_CONFIG_LOG:
3f5f1 20 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4d 53 56   {..      /* MSV
3f5f2 43 20 69 73 20 70 69 63 6b 79 20 61 62 6f 75 74  C is picky about
3f5f3 20 70 75 6c 6c 69 6e 67 20 66 75 6e 63 20 70 74   pulling func pt
3f5f4 72 73 20 66 72 6f 6d 20 76 61 20 6c 69 73 74 73  rs from va lists
3f5f5 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 68 74 74 70  ...      ** http
3f5f6 3a 2f 2f 73 75 70 70 6f 72 74 2e 6d 69 63 72 6f  ://support.micro
3f5f7 73 6f 66 74 2e 63 6f 6d 2f 6b 62 2f 34 37 39 36  soft.com/kb/4796
3f5f8 31 0d 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  1..      ** sqli
3f5f9 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3f5fa 78 4c 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70  xLog = va_arg(ap
3f5fb 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  , void(*)(void*,
3f5fc 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
3f5fd 29 3b 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20  );..      */..  
3f5fe 20 20 20 20 74 79 70 65 64 65 66 20 76 6f 69 64      typedef void
3f5ff 28 2a 4c 4f 47 46 55 4e 43 5f 74 29 28 76 6f 69  (*LOGFUNC_t)(voi
3f600 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
3f601 72 2a 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  r*);..      sqli
3f602 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3f603 78 4c 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70  xLog = va_arg(ap
3f604 2c 20 4c 4f 47 46 55 4e 43 5f 74 29 3b 0d 0a 20  , LOGFUNC_t);.. 
3f605 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3f606 61 6c 43 6f 6e 66 69 67 2e 70 4c 6f 67 41 72 67  alConfig.pLogArg
3f607 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
3f608 69 64 2a 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  id*);..      bre
3f609 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  ak;..    }....  
3f60a 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
3f60b 4e 46 49 47 5f 55 52 49 3a 20 7b 0d 0a 20 20 20  NFIG_URI: {..   
3f60c 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3f60d 43 6f 6e 66 69 67 2e 62 4f 70 65 6e 55 72 69 20  Config.bOpenUri 
3f60e 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
3f60f 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
3f610 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 64  ..    }....    d
3f611 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20  efault: {..     
3f612 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
3f613 4f 52 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  OR;..      break
3f614 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
3f615 20 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20 20   va_end(ap);..  
3f616 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
3f617 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 75 70 20 74  ./*..** Set up t
3f618 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  he lookaside buf
3f619 66 65 72 73 20 66 6f 72 20 61 20 64 61 74 61 62  fers for a datab
3f61a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0d  ase connection..
3f61b 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
3f61c 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
3f61d 20 20 0d 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61 73    ..** If lookas
3f61e 69 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ide is already a
3f61f 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 53 51  ctive, return SQ
3f620 4c 49 54 45 5f 42 55 53 59 2e 0d 0a 2a 2a 0d 0a  LITE_BUSY...**..
3f621 2a 2a 20 54 68 65 20 73 7a 20 70 61 72 61 6d 65  ** The sz parame
3f622 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ter is the numbe
3f623 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 65 61  r of bytes in ea
3f624 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f  ch lookaside slo
3f625 74 2e 0d 0a 2a 2a 20 54 68 65 20 63 6e 74 20 70  t...** The cnt p
3f626 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
3f627 6e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 2e  number of slots.
3f628 20 20 49 66 20 70 53 74 61 72 74 20 69 73 20 4e    If pStart is N
3f629 55 4c 4c 20 74 68 65 0d 0a 2a 2a 20 73 70 61 63  ULL the..** spac
3f62a 65 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b 61 73  e for the lookas
3f62b 69 64 65 20 6d 65 6d 6f 72 79 20 69 73 20 6f 62  ide memory is ob
3f62c 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
3f62d 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0d 0a 2a  te3_malloc()...*
3f62e 2a 20 49 66 20 70 53 74 61 72 74 20 69 73 20 6e  * If pStart is n
3f62f 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20  ot NULL then it 
3f630 69 73 20 73 7a 2a 63 6e 74 20 62 79 74 65 73 20  is sz*cnt bytes 
3f631 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 75 73 65  of memory to use
3f632 20 66 6f 72 0d 0a 2a 2a 20 74 68 65 20 6c 6f 6f   for..** the loo
3f633 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0d 0a  kaside memory...
3f634 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  */..static int s
3f635 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71  etupLookaside(sq
3f636 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
3f637 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69  *pBuf, int sz, i
3f638 6e 74 20 63 6e 74 29 7b 0d 0a 20 20 76 6f 69 64  nt cnt){..  void
3f639 20 2a 70 53 74 61 72 74 3b 0d 0a 20 20 69 66 28   *pStart;..  if(
3f63a 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
3f63b 4f 75 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  Out ){..    retu
3f63c 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0d  rn SQLITE_BUSY;.
3f63d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 46 72 65 65 20  .  }..  /* Free 
3f63e 61 6e 79 20 65 78 69 73 74 69 6e 67 20 6c 6f 6f  any existing loo
3f63f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 66 6f  kaside buffer fo
3f640 72 20 74 68 69 73 20 68 61 6e 64 6c 65 20 62 65  r this handle be
3f641 66 6f 72 65 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63  fore..  ** alloc
3f642 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 20  ating a new one 
3f643 73 6f 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65  so we don't have
3f644 20 74 6f 20 68 61 76 65 20 73 70 61 63 65 20 66   to have space f
3f645 6f 72 20 0d 0a 20 20 2a 2a 20 62 6f 74 68 20 61  or ..  ** both a
3f646 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e  t the same time.
3f647 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 64 62  ..  */..  if( db
3f648 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c  ->lookaside.bMal
3f649 6c 6f 63 65 64 20 29 7b 0d 0a 20 20 20 20 73 71  loced ){..    sq
3f64a 6c 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c  lite3_free(db->l
3f64b 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29  ookaside.pStart)
3f64c 3b 0d 0a 20 20 7d 0d 0a 20 20 2f 2a 20 54 68 65  ;..  }..  /* The
3f64d 20 73 69 7a 65 20 6f 66 20 61 20 6c 6f 6f 6b 61   size of a looka
3f64e 73 69 64 65 20 73 6c 6f 74 20 61 66 74 65 72 20  side slot after 
3f64f 52 4f 55 4e 44 44 4f 57 4e 38 20 6e 65 65 64 73  ROUNDDOWN8 needs
3f650 20 74 6f 20 62 65 20 6c 61 72 67 65 72 0d 0a 20   to be larger.. 
3f651 20 2a 2a 20 74 68 61 6e 20 61 20 70 6f 69 6e 74   ** than a point
3f652 65 72 20 74 6f 20 62 65 20 75 73 65 66 75 6c 2e  er to be useful.
3f653 0d 0a 20 20 2a 2f 0d 0a 20 20 73 7a 20 3d 20 52  ..  */..  sz = R
3f654 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 20 20  OUNDDOWN8(sz);  
3f655 2f 2a 20 49 4d 50 3a 20 52 2d 33 33 30 33 38 2d  /* IMP: R-33038-
3f656 30 39 33 38 32 20 2a 2f 0d 0a 20 20 69 66 28 20  09382 */..  if( 
3f657 73 7a 3c 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28  sz<=(int)sizeof(
3f658 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20  LookasideSlot*) 
3f659 29 20 73 7a 20 3d 20 30 3b 0d 0a 20 20 69 66 28  ) sz = 0;..  if(
3f65a 20 63 6e 74 3c 30 20 29 20 63 6e 74 20 3d 20 30   cnt<0 ) cnt = 0
3f65b 3b 0d 0a 20 20 69 66 28 20 73 7a 3d 3d 30 20 7c  ;..  if( sz==0 |
3f65c 7c 20 63 6e 74 3d 3d 30 20 29 7b 0d 0a 20 20 20  | cnt==0 ){..   
3f65d 20 73 7a 20 3d 20 30 3b 0d 0a 20 20 20 20 70 53   sz = 0;..    pS
3f65e 74 61 72 74 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  tart = 0;..  }el
3f65f 73 65 20 69 66 28 20 70 42 75 66 3d 3d 30 20 29  se if( pBuf==0 )
3f660 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  {..    sqlite3Be
3f661 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
3f662 29 3b 0d 0a 20 20 20 20 70 53 74 61 72 74 20 3d  );..    pStart =
3f663 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
3f664 73 7a 2a 63 6e 74 20 29 3b 20 20 2f 2a 20 49 4d  sz*cnt );  /* IM
3f665 50 3a 20 52 2d 36 31 39 34 39 2d 33 35 37 32 37  P: R-61949-35727
3f666 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   */..    sqlite3
3f667 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
3f668 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 53 74 61  );..    if( pSta
3f669 72 74 20 29 20 63 6e 74 20 3d 20 73 71 6c 69 74  rt ) cnt = sqlit
3f66a 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 53 74  e3MallocSize(pSt
3f66b 61 72 74 29 2f 73 7a 3b 0d 0a 20 20 7d 65 6c 73  art)/sz;..  }els
3f66c 65 7b 0d 0a 20 20 20 20 70 53 74 61 72 74 20 3d  e{..    pStart =
3f66d 20 70 42 75 66 3b 0d 0a 20 20 7d 0d 0a 20 20 64   pBuf;..  }..  d
3f66e 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74  b->lookaside.pSt
3f66f 61 72 74 20 3d 20 70 53 74 61 72 74 3b 0d 0a 20  art = pStart;.. 
3f670 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
3f671 46 72 65 65 20 3d 20 30 3b 0d 0a 20 20 64 62 2d  Free = 0;..  db-
3f672 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 3d 20  >lookaside.sz = 
3f673 28 75 31 36 29 73 7a 3b 0d 0a 20 20 69 66 28 20  (u16)sz;..  if( 
3f674 70 53 74 61 72 74 20 29 7b 0d 0a 20 20 20 20 69  pStart ){..    i
3f675 6e 74 20 69 3b 0d 0a 20 20 20 20 4c 6f 6f 6b 61  nt i;..    Looka
3f676 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0d 0a 20 20  sideSlot *p;..  
3f677 20 20 61 73 73 65 72 74 28 20 73 7a 20 3e 20 28    assert( sz > (
3f678 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61  int)sizeof(Looka
3f679 73 69 64 65 53 6c 6f 74 2a 29 20 29 3b 0d 0a 20  sideSlot*) );.. 
3f67a 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64     p = (Lookasid
3f67b 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b 0d 0a  eSlot*)pStart;..
3f67c 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31 3b      for(i=cnt-1;
3f67d 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0d 0a 20 20   i>=0; i--){..  
3f67e 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64      p->pNext = d
3f67f 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
3f680 65 65 3b 0d 0a 20 20 20 20 20 20 64 62 2d 3e 6c  ee;..      db->l
3f681 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d  ookaside.pFree =
3f682 20 70 3b 0d 0a 20 20 20 20 20 20 70 20 3d 20 28   p;..      p = (
3f683 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 26  LookasideSlot*)&
3f684 28 28 75 38 2a 29 70 29 5b 73 7a 5d 3b 0d 0a 20  ((u8*)p)[sz];.. 
3f685 20 20 20 7d 0d 0a 20 20 20 20 64 62 2d 3e 6c 6f     }..    db->lo
3f686 6f 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 70  okaside.pEnd = p
3f687 3b 0d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  ;..    db->looka
3f688 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
3f689 31 3b 0d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  1;..    db->look
3f68a 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20  aside.bMalloced 
3f68b 3d 20 70 42 75 66 3d 3d 30 20 3f 31 3a 30 3b 0d  = pBuf==0 ?1:0;.
3f68c 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 64  .  }else{..    d
3f68d 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
3f68e 64 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62 2d 3e  d = 0;..    db->
3f68f 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
3f690 65 64 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62 2d  ed = 0;..    db-
3f691 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
3f692 6f 63 65 64 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  oced = 0;..  }..
3f693 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3f694 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
3f695 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65   Return the mute
3f696 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
3f697 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  h a database con
3f698 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51  nection...*/..SQ
3f699 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
3f69a 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f  _mutex *sqlite3_
3f69b 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33  db_mutex(sqlite3
3f69c 20 2a 64 62 29 7b 0d 0a 20 20 72 65 74 75 72 6e   *db){..  return
3f69d 20 64 62 2d 3e 6d 75 74 65 78 3b 0d 0a 7d 0d 0a   db->mutex;..}..
3f69e 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 75 70  ../*..** Free up
3f69f 20 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20   as much memory 
3f6a0 61 73 20 77 65 20 63 61 6e 20 66 72 6f 6d 20 74  as we can from t
3f6a1 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73  he given databas
3f6a2 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  e..** connection
3f6a3 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
3f6a4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62  I int sqlite3_db
3f6a5 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
3f6a6 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20  sqlite3 *db){.. 
3f6a7 20 69 6e 74 20 69 3b 0d 0a 20 20 73 71 6c 69 74   int i;..  sqlit
3f6a8 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
3f6a9 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 73 71  b->mutex);..  sq
3f6aa 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
3f6ab 6c 6c 28 64 62 29 3b 0d 0a 20 20 66 6f 72 28 69  ll(db);..  for(i
3f6ac 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
3f6ad 2b 2b 29 7b 0d 0a 20 20 20 20 42 74 72 65 65 20  ++){..    Btree 
3f6ae 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
3f6af 5d 2e 70 42 74 3b 0d 0a 20 20 20 20 69 66 28 20  ].pBt;..    if( 
3f6b0 70 42 74 20 29 7b 0d 0a 20 20 20 20 20 20 50 61  pBt ){..      Pa
3f6b1 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71  ger *pPager = sq
3f6b2 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
3f6b3 70 42 74 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  pBt);..      sql
3f6b4 69 74 65 33 50 61 67 65 72 53 68 72 69 6e 6b 28  ite3PagerShrink(
3f6b5 70 50 61 67 65 72 29 3b 0d 0a 20 20 20 20 7d 0d  pPager);..    }.
3f6b6 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 42  .  }..  sqlite3B
3f6b7 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
3f6b8 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
3f6b9 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3f6ba 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  ex);..  return S
3f6bb 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
3f6bc 2f 2a 0d 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61  /*..** Configura
3f6bd 74 69 6f 6e 20 73 65 74 74 69 6e 67 73 20 66 6f  tion settings fo
3f6be 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20  r an individual 
3f6bf 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3f6c0 69 6f 6e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ion..*/..SQLITE_
3f6c1 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3f6c2 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65  db_config(sqlite
3f6c3 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70 2c 20 2e  3 *db, int op, .
3f6c4 2e 2e 29 7b 0d 0a 20 20 76 61 5f 6c 69 73 74 20  ..){..  va_list 
3f6c5 61 70 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  ap;..  int rc;..
3f6c6 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
3f6c7 70 29 3b 0d 0a 20 20 73 77 69 74 63 68 28 20 6f  p);..  switch( o
3f6c8 70 20 29 7b 0d 0a 20 20 20 20 63 61 73 65 20 53  p ){..    case S
3f6c9 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c  QLITE_DBCONFIG_L
3f6ca 4f 4f 4b 41 53 49 44 45 3a 20 7b 0d 0a 20 20 20  OOKASIDE: {..   
3f6cb 20 20 20 76 6f 69 64 20 2a 70 42 75 66 20 3d 20     void *pBuf = 
3f6cc 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
3f6cd 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 36 38  ); /* IMP: R-268
3f6ce 33 35 2d 31 30 39 36 34 20 2a 2f 0d 0a 20 20 20  35-10964 */..   
3f6cf 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61     int sz = va_a
3f6d0 72 67 28 61 70 2c 20 69 6e 74 29 3b 20 20 20 20  rg(ap, int);    
3f6d1 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 34 37 38     /* IMP: R-478
3f6d2 37 31 2d 32 35 39 39 34 20 2a 2f 0d 0a 20 20 20  71-25994 */..   
3f6d3 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 76 61 5f     int cnt = va_
3f6d4 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 20 20 20  arg(ap, int);   
3f6d5 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 34     /* IMP: R-044
3f6d6 36 30 2d 35 33 33 38 36 20 2a 2f 0d 0a 20 20 20  60-53386 */..   
3f6d7 20 20 20 72 63 20 3d 20 73 65 74 75 70 4c 6f 6f     rc = setupLoo
3f6d8 6b 61 73 69 64 65 28 64 62 2c 20 70 42 75 66 2c  kaside(db, pBuf,
3f6d9 20 73 7a 2c 20 63 6e 74 29 3b 0d 0a 20 20 20 20   sz, cnt);..    
3f6da 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
3f6db 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d  .    default: {.
3f6dc 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f  .      static co
3f6dd 6e 73 74 20 73 74 72 75 63 74 20 7b 0d 0a 20 20  nst struct {..  
3f6de 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20        int op;   
3f6df 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65     /* The opcode
3f6e0 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 75 33 32   */..        u32
3f6e1 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20 4d 61 73   mask;    /* Mas
3f6e2 6b 20 6f 66 20 74 68 65 20 62 69 74 20 69 6e 20  k of the bit in 
3f6e3 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 74 6f  sqlite3.flags to
3f6e4 20 73 65 74 2f 63 6c 65 61 72 20 2a 2f 0d 0a 20   set/clear */.. 
3f6e5 20 20 20 20 20 7d 20 61 46 6c 61 67 4f 70 5b 5d       } aFlagOp[]
3f6e6 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20 20 7b 20   = {..        { 
3f6e7 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f  SQLITE_DBCONFIG_
3f6e8 45 4e 41 42 4c 45 5f 46 4b 45 59 2c 20 20 20 20  ENABLE_FKEY,    
3f6e9 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
3f6ea 79 73 20 20 20 20 7d 2c 0d 0a 20 20 20 20 20 20  ys    },..      
3f6eb 20 20 7b 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e    { SQLITE_DBCON
3f6ec 46 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49 47 47  FIG_ENABLE_TRIGG
3f6ed 45 52 2c 20 53 51 4c 49 54 45 5f 45 6e 61 62 6c  ER, SQLITE_Enabl
3f6ee 65 54 72 69 67 67 65 72 20 20 7d 2c 0d 0a 20 20  eTrigger  },..  
3f6ef 20 20 20 20 7d 3b 0d 0a 20 20 20 20 20 20 75 6e      };..      un
3f6f0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0d 0a 20  signed int i;.. 
3f6f1 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3f6f2 5f 45 52 52 4f 52 3b 20 2f 2a 20 49 4d 50 3a 20  _ERROR; /* IMP: 
3f6f3 52 2d 34 32 37 39 30 2d 32 33 33 37 32 20 2a 2f  R-42790-23372 */
3f6f4 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ..      for(i=0;
3f6f5 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 46 6c   i<ArraySize(aFl
3f6f6 61 67 4f 70 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20  agOp); i++){..  
3f6f7 20 20 20 20 20 20 69 66 28 20 61 46 6c 61 67 4f        if( aFlagO
3f6f8 70 5b 69 5d 2e 6f 70 3d 3d 6f 70 20 29 7b 0d 0a  p[i].op==op ){..
3f6f9 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6e            int on
3f6fa 6f 66 66 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  off = va_arg(ap,
3f6fb 20 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 20 20   int);..        
3f6fc 20 20 69 6e 74 20 2a 70 52 65 73 20 3d 20 76 61    int *pRes = va
3f6fd 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0d  _arg(ap, int*);.
3f6fe 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6f  .          int o
3f6ff 6c 64 46 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c  ldFlags = db->fl
3f700 61 67 73 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ags;..          
3f701 69 66 28 20 6f 6e 6f 66 66 3e 30 20 29 7b 0d 0a  if( onoff>0 ){..
3f702 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
3f703 66 6c 61 67 73 20 7c 3d 20 61 46 6c 61 67 4f 70  flags |= aFlagOp
3f704 5b 69 5d 2e 6d 61 73 6b 3b 0d 0a 20 20 20 20 20  [i].mask;..     
3f705 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f       }else if( o
3f706 6e 6f 66 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  noff==0 ){..    
3f707 20 20 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67          db->flag
3f708 73 20 26 3d 20 7e 61 46 6c 61 67 4f 70 5b 69 5d  s &= ~aFlagOp[i]
3f709 2e 6d 61 73 6b 3b 0d 0a 20 20 20 20 20 20 20 20  .mask;..        
3f70a 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 69    }..          i
3f70b 66 28 20 6f 6c 64 46 6c 61 67 73 21 3d 64 62 2d  f( oldFlags!=db-
3f70c 3e 66 6c 61 67 73 20 29 7b 0d 0a 20 20 20 20 20  >flags ){..     
3f70d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
3f70e 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
3f70f 65 6d 65 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20  ements(db);..   
3f710 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
3f711 20 20 20 20 69 66 28 20 70 52 65 73 20 29 7b 0d      if( pRes ){.
3f712 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 52  .            *pR
3f713 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20  es = (db->flags 
3f714 26 20 61 46 6c 61 67 4f 70 5b 69 5d 2e 6d 61 73  & aFlagOp[i].mas
3f715 6b 29 21 3d 30 3b 0d 0a 20 20 20 20 20 20 20 20  k)!=0;..        
3f716 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 72    }..          r
3f717 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
3f718 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3f719 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
3f71a 20 20 20 7d 0d 0a 20 20 20 20 20 20 62 72 65 61     }..      brea
3f71b 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  k;..    }..  }..
3f71c 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 20    va_end(ap);.. 
3f71d 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
3f71e 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72  ..../*..** Retur
3f71f 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 62 75  n true if the bu
3f720 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 63  ffer z[0..n-1] c
3f721 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70 61 63  ontains all spac
3f722 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
3f723 69 6e 74 20 61 6c 6c 53 70 61 63 65 73 28 63 6f  int allSpaces(co
3f724 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
3f725 20 6e 29 7b 0d 0a 20 20 77 68 69 6c 65 28 20 6e   n){..  while( n
3f726 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20  >0 && z[n-1]==' 
3f727 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0d 0a 20 20 72  ' ){ n--; }..  r
3f728 65 74 75 72 6e 20 6e 3d 3d 30 3b 0d 0a 7d 0d 0a  eturn n==0;..}..
3f729 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73  ../*..** This is
3f72a 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
3f72b 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
3f72c 6e 61 6d 65 64 20 22 42 49 4e 41 52 59 22 20 77  named "BINARY" w
3f72d 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 0d 0a  hich is always..
3f72e 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0d 0a 2a  ** available...*
3f72f 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64  *..** If the pad
3f730 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73  Flag argument is
3f731 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73   not NULL then s
3f732 70 61 63 65 20 70 61 64 64 69 6e 67 20 61 74 20  pace padding at 
3f733 74 68 65 20 65 6e 64 0d 0a 2a 2a 20 6f 66 20 73  the end..** of s
3f734 74 72 69 6e 67 73 20 69 73 20 69 67 6e 6f 72 65  trings is ignore
3f735 64 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  d.  This impleme
3f736 6e 74 73 20 74 68 65 20 52 54 52 49 4d 20 63 6f  nts the RTRIM co
3f737 6c 6c 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74  llation...*/..st
3f738 61 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6c 6c  atic int binColl
3f739 46 75 6e 63 28 0d 0a 20 20 76 6f 69 64 20 2a 70  Func(..  void *p
3f73a 61 64 46 6c 61 67 2c 0d 0a 20 20 69 6e 74 20 6e  adFlag,..  int n
3f73b 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  Key1, const void
3f73c 20 2a 70 4b 65 79 31 2c 0d 0a 20 20 69 6e 74 20   *pKey1,..  int 
3f73d 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey2, const voi
3f73e 64 20 2a 70 4b 65 79 32 0d 0a 29 7b 0d 0a 20 20  d *pKey2..){..  
3f73f 69 6e 74 20 72 63 2c 20 6e 3b 0d 0a 20 20 6e 20  int rc, n;..  n 
3f740 3d 20 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20  = nKey1<nKey2 ? 
3f741 6e 4b 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0d 0a  nKey1 : nKey2;..
3f742 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b    rc = memcmp(pK
3f743 65 79 31 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0d  ey1, pKey2, n);.
3f744 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0d  .  if( rc==0 ){.
3f745 0a 20 20 20 20 69 66 28 20 70 61 64 46 6c 61 67  .    if( padFlag
3f746 0d 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61  ..     && allSpa
3f747 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79  ces(((char*)pKey
3f748 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0d 0a  1)+n, nKey1-n)..
3f749 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65       && allSpace
3f74a 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79 32 29  s(((char*)pKey2)
3f74b 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0d 0a 20 20  +n, nKey2-n)..  
3f74c 20 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4c    ){..      /* L
3f74d 65 61 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65  eave rc unchange
3f74e 64 20 61 74 20 30 20 2a 2f 0d 0a 20 20 20 20 7d  d at 0 */..    }
3f74f 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20  else{..      rc 
3f750 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b 65 79 32 3b  = nKey1 - nKey2;
3f751 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
3f752 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
3f753 0a 2f 2a 0d 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  ./*..** Another 
3f754 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69  built-in collati
3f755 6e 67 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43  ng sequence: NOC
3f756 41 53 45 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ASE. ..**..** Th
3f757 69 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  is collating seq
3f758 75 65 6e 63 65 20 69 73 20 69 6e 74 65 6e 64 65  uence is intende
3f759 64 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72  d to be used for
3f75a 20 22 63 61 73 65 20 69 6e 64 65 70 65 6e 64 61   "case independa
3f75b 6e 74 0d 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f  nt..** compariso
3f75c 6e 22 2e 20 53 51 4c 69 74 65 27 73 20 6b 6e 6f  n". SQLite's kno
3f75d 77 6c 65 64 67 65 20 6f 66 20 75 70 70 65 72 20  wledge of upper 
3f75e 61 6e 64 20 6c 6f 77 65 72 20 63 61 73 65 20 65  and lower case e
3f75f 71 75 69 76 61 6c 65 6e 74 73 0d 0a 2a 2a 20 65  quivalents..** e
3f760 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74 6f 20 74  xtends only to t
3f761 68 65 20 32 36 20 63 68 61 72 61 63 74 65 72 73  he 26 characters
3f762 20 75 73 65 64 20 69 6e 20 74 68 65 20 45 6e 67   used in the Eng
3f763 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 2e 0d 0a  lish language...
3f764 2a 2a 0d 0a 2a 2a 20 41 74 20 74 68 65 20 6d 6f  **..** At the mo
3f765 6d 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f 6e  ment there is on
3f766 6c 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c 65  ly a UTF-8 imple
3f767 6d 65 6e 74 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  mentation...*/..
3f768 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 63 61 73  static int nocas
3f769 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0d  eCollatingFunc(.
3f76a 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  .  void *NotUsed
3f76b 2c 0d 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20  ,..  int nKey1, 
3f76c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
3f76d 31 2c 0d 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c  1,..  int nKey2,
3f76e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3f76f 79 32 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 20  y2..){..  int r 
3f770 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  = sqlite3StrNICm
3f771 70 28 0d 0a 20 20 20 20 20 20 28 63 6f 6e 73 74  p(..      (const
3f772 20 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28   char *)pKey1, (
3f773 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
3f774 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32  y2, (nKey1<nKey2
3f775 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0d  )?nKey1:nKey2);.
3f776 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
3f777 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0d 0a 20  TER(NotUsed);.. 
3f778 20 69 66 28 20 30 3d 3d 72 20 29 7b 0d 0a 20 20   if( 0==r ){..  
3f779 20 20 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79    r = nKey1-nKey
3f77a 32 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72  2;..  }..  retur
3f77b 6e 20 72 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  n r;..}..../*..*
3f77c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 52 4f 57  * Return the ROW
3f77d 49 44 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  ID of the most r
3f77e 65 63 65 6e 74 20 69 6e 73 65 72 74 0d 0a 2a 2f  ecent insert..*/
3f77f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ..SQLITE_API sql
3f780 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  ite_int64 sqlite
3f781 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
3f782 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  wid(sqlite3 *db)
3f783 7b 0d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  {..  return db->
3f784 6c 61 73 74 52 6f 77 69 64 3b 0d 0a 7d 0d 0a 0d  lastRowid;..}...
3f785 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./*..** Return t
3f786 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
3f787 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74  nges in the most
3f788 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
3f789 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0d  sqlite3_exec()..
3f78a 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3f78b 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  int sqlite3_chan
3f78c 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ges(sqlite3 *db)
3f78d 7b 0d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  {..  return db->
3f78e 6e 43 68 61 6e 67 65 3b 0d 0a 7d 0d 0a 0d 0a 2f  nChange;..}..../
3f78f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
3f790 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
3f791 65 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74  es since the dat
3f792 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77 61 73  abase handle was
3f793 20 6f 70 65 6e 65 64 2e 0d 0a 2a 2f 0d 0a 53 51   opened...*/..SQ
3f794 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3f795 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
3f796 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
3f797 0d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e  ..  return db->n
3f798 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0d 0a 7d 0d  TotalChange;..}.
3f799 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20  .../*..** Close 
3f79a 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  all open savepoi
3f79b 6e 74 73 2e 20 54 68 69 73 20 66 75 6e 63 74 69  nts. This functi
3f79c 6f 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61  on only manipula
3f79d 74 65 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68  tes fields of th
3f79e 65 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68  e..** database h
3f79f 61 6e 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69 74  andle object, it
3f7a0 20 64 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65 20   does not close 
3f7a1 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74  any savepoints t
3f7a2 68 61 74 20 6d 61 79 20 62 65 20 6f 70 65 6e 0d  hat may be open.
3f7a3 0a 2a 2a 20 61 74 20 74 68 65 20 62 2d 74 72 65  .** at the b-tre
3f7a4 65 2f 70 61 67 65 72 20 6c 65 76 65 6c 2e 0d 0a  e/pager level...
3f7a5 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
3f7a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
3f7a7 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 73  loseSavepoints(s
3f7a8 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20  qlite3 *db){..  
3f7a9 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65  while( db->pSave
3f7aa 70 6f 69 6e 74 20 29 7b 0d 0a 20 20 20 20 53 61  point ){..    Sa
3f7ab 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 20 3d 20  vepoint *pTmp = 
3f7ac 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0d  db->pSavepoint;.
3f7ad 0a 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f  .    db->pSavepo
3f7ae 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e 65 78  int = pTmp->pNex
3f7af 74 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 44  t;..    sqlite3D
3f7b0 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29 3b  bFree(db, pTmp);
3f7b1 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d 3e 6e 53 61  ..  }..  db->nSa
3f7b2 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0d 0a 20 20  vepoint = 0;..  
3f7b3 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d  db->nStatement =
3f7b4 20 30 3b 0d 0a 20 20 64 62 2d 3e 69 73 54 72 61   0;..  db->isTra
3f7b5 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
3f7b6 74 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  t = 0;..}..../*.
3f7b7 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 64  .** Invoke the d
3f7b8 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
3f7b9 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
3f7ba 74 68 20 46 75 6e 63 44 65 66 20 70 2c 20 69 66  th FuncDef p, if
3f7bb 20 61 6e 79 2e 20 45 78 63 65 70 74 2c 0d 0a 2a   any. Except,..*
3f7bc 2a 20 69 66 20 74 68 69 73 20 69 73 20 6e 6f 74  * if this is not
3f7bd 20 74 68 65 20 6c 61 73 74 20 63 6f 70 79 20 6f   the last copy o
3f7be 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20  f the function, 
3f7bf 64 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 69 74  do not invoke it
3f7c0 2e 20 4d 75 6c 74 69 70 6c 65 0d 0a 2a 2a 20 63  . Multiple..** c
3f7c1 6f 70 69 65 73 20 6f 66 20 61 20 73 69 6e 67 6c  opies of a singl
3f7c2 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 63  e function are c
3f7c3 72 65 61 74 65 64 20 77 68 65 6e 20 63 72 65 61  reated when crea
3f7c4 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 69 73  te_function() is
3f7c5 20 63 61 6c 6c 65 64 0d 0a 2a 2a 20 77 69 74 68   called..** with
3f7c6 20 53 51 4c 49 54 45 5f 41 4e 59 20 61 73 20 74   SQLITE_ANY as t
3f7c7 68 65 20 65 6e 63 6f 64 69 6e 67 2e 0d 0a 2a 2f  he encoding...*/
3f7c8 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 75  ..static void fu
3f7c9 6e 63 74 69 6f 6e 44 65 73 74 72 6f 79 28 73 71  nctionDestroy(sq
3f7ca 6c 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44  lite3 *db, FuncD
3f7cb 65 66 20 2a 70 29 7b 0d 0a 20 20 46 75 6e 63 44  ef *p){..  FuncD
3f7cc 65 73 74 72 75 63 74 6f 72 20 2a 70 44 65 73 74  estructor *pDest
3f7cd 72 75 63 74 6f 72 20 3d 20 70 2d 3e 70 44 65 73  ructor = p->pDes
3f7ce 74 72 75 63 74 6f 72 3b 0d 0a 20 20 69 66 28 20  tructor;..  if( 
3f7cf 70 44 65 73 74 72 75 63 74 6f 72 20 29 7b 0d 0a  pDestructor ){..
3f7d0 20 20 20 20 70 44 65 73 74 72 75 63 74 6f 72 2d      pDestructor-
3f7d1 3e 6e 52 65 66 2d 2d 3b 0d 0a 20 20 20 20 69 66  >nRef--;..    if
3f7d2 28 20 70 44 65 73 74 72 75 63 74 6f 72 2d 3e 6e  ( pDestructor->n
3f7d3 52 65 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  Ref==0 ){..     
3f7d4 20 70 44 65 73 74 72 75 63 74 6f 72 2d 3e 78 44   pDestructor->xD
3f7d5 65 73 74 72 6f 79 28 70 44 65 73 74 72 75 63 74  estroy(pDestruct
3f7d6 6f 72 2d 3e 70 55 73 65 72 44 61 74 61 29 3b 0d  or->pUserData);.
3f7d7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
3f7d8 46 72 65 65 28 64 62 2c 20 70 44 65 73 74 72 75  Free(db, pDestru
3f7d9 63 74 6f 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  ctor);..    }.. 
3f7da 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
3f7db 43 6c 6f 73 65 20 61 6e 20 65 78 69 73 74 69 6e  Close an existin
3f7dc 67 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  g SQLite databas
3f7dd 65 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  e..*/..SQLITE_AP
3f7de 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c  I int sqlite3_cl
3f7df 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ose(sqlite3 *db)
3f7e0 7b 0d 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69  {..  HashElem *i
3f7e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3f7e2 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
3f7e3 6c 65 20 69 74 65 72 61 74 6f 72 20 2a 2f 0d 0a  le iterator */..
3f7e4 20 20 69 6e 74 20 6a 3b 0d 0a 0d 0a 20 20 69 66    int j;....  if
3f7e5 28 20 21 64 62 20 29 7b 0d 0a 20 20 20 20 72 65  ( !db ){..    re
3f7e6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
3f7e7 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21 73 71 6c  .  }..  if( !sql
3f7e8 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
3f7e9 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0d 0a  ickOrOk(db) ){..
3f7ea 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3f7eb 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0d 0a  E_MISUSE_BKPT;..
3f7ec 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d    }..  sqlite3_m
3f7ed 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3f7ee 75 74 65 78 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 46  utex);....  /* F
3f7ef 6f 72 63 65 20 78 44 65 73 74 72 6f 79 20 63 61  orce xDestroy ca
3f7f0 6c 6c 73 20 6f 6e 20 61 6c 6c 20 76 69 72 74 75  lls on all virtu
3f7f1 61 6c 20 74 61 62 6c 65 73 20 2a 2f 0d 0a 20 20  al tables */..  
3f7f2 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
3f7f3 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 2d  rnalSchema(db, -
3f7f4 31 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 61  1);....  /* If a
3f7f5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
3f7f6 6f 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74 49  open, the ResetI
3f7f7 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29 20  nternalSchema() 
3f7f8 63 61 6c 6c 20 61 62 6f 76 65 0d 0a 20 20 2a 2a  call above..  **
3f7f9 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63   will not have c
3f7fa 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f  alled the xDisco
3f7fb 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f  nnect() method o
3f7fc 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0d 0a 20  n any virtual.. 
3f7fd 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68   ** tables in th
3f7fe 65 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20  e db->aVTrans[] 
3f7ff 61 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f  array. The follo
3f800 77 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61 62  wing sqlite3Vtab
3f801 52 6f 6c 6c 62 61 63 6b 28 29 0d 0a 20 20 2a 2a  Rollback()..  **
3f802 20 63 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f   call will do so
3f803 2e 20 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20  . We need to do 
3f804 74 68 69 73 20 62 65 66 6f 72 65 20 74 68 65 20  this before the 
3f805 63 68 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65  check for active
3f806 0d 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65  ..  ** SQL state
3f807 6d 65 6e 74 73 20 62 65 6c 6f 77 2c 20 61 73 20  ments below, as 
3f808 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c  the v-table impl
3f809 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62  ementation may b
3f80a 65 20 73 74 6f 72 69 6e 67 0d 0a 20 20 2a 2a 20  e storing..  ** 
3f80b 73 6f 6d 65 20 70 72 65 70 61 72 65 64 20 73 74  some prepared st
3f80c 61 74 65 6d 65 6e 74 73 20 69 6e 74 65 72 6e 61  atements interna
3f80d 6c 6c 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71  lly...  */..  sq
3f80e 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
3f80f 6b 28 64 62 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  k(db);....  /* I
3f810 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
3f811 6f 75 74 73 74 61 6e 64 69 6e 67 20 56 4d 73 2c  outstanding VMs,
3f812 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
3f813 55 53 59 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 64  USY. */..  if( d
3f814 62 2d 3e 70 56 64 62 65 20 29 7b 0d 0a 20 20 20  b->pVdbe ){..   
3f815 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
3f816 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0d  , SQLITE_BUSY, .
3f817 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  .        "unable
3f818 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f   to close due to
3f819 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61   unfinalised sta
3f81a 74 65 6d 65 6e 74 73 22 29 3b 0d 0a 20 20 20 20  tements");..    
3f81b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3f81c 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ave(db->mutex);.
3f81d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
3f81e 54 45 5f 42 55 53 59 3b 0d 0a 20 20 7d 0d 0a 20  TE_BUSY;..  }.. 
3f81f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
3f820 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
3f821 72 4f 6b 28 64 62 29 20 29 3b 0d 0a 0d 0a 20 20  rOk(db) );....  
3f822 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e  for(j=0; j<db->n
3f823 44 62 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 42  Db; j++){..    B
3f824 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
3f825 61 44 62 5b 6a 5d 2e 70 42 74 3b 0d 0a 20 20 20  aDb[j].pBt;..   
3f826 20 69 66 28 20 70 42 74 20 26 26 20 73 71 6c 69   if( pBt && sqli
3f827 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b  te3BtreeIsInBack
3f828 75 70 28 70 42 74 29 20 29 7b 0d 0a 20 20 20 20  up(pBt) ){..    
3f829 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
3f82a 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
3f82b 0d 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 61  ..          "una
3f82c 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65  ble to close due
3f82d 20 74 6f 20 75 6e 66 69 6e 69 73 68 65 64 20 62   to unfinished b
3f82e 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 22  ackup operation"
3f82f 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
3f830 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3f831 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 20 20 20  ->mutex);..     
3f832 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
3f833 55 53 59 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  USY;..    }..  }
3f834 0d 0a 0d 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e  ....  /* Free an
3f835 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 53 61  y outstanding Sa
3f836 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
3f837 65 73 2e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  es. */..  sqlite
3f838 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
3f839 28 64 62 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 6a  (db);....  for(j
3f83a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a  =0; j<db->nDb; j
3f83b 2b 2b 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74  ++){..    struct
3f83c 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e   Db *pDb = &db->
3f83d 61 44 62 5b 6a 5d 3b 0d 0a 20 20 20 20 69 66 28  aDb[j];..    if(
3f83e 20 70 44 62 2d 3e 70 42 74 20 29 7b 0d 0a 20 20   pDb->pBt ){..  
3f83f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
3f840 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b  Close(pDb->pBt);
3f841 0d 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74  ..      pDb->pBt
3f842 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66 28   = 0;..      if(
3f843 20 6a 21 3d 31 20 29 7b 0d 0a 20 20 20 20 20 20   j!=1 ){..      
3f844 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d    pDb->pSchema =
3f845 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   0;..      }..  
3f846 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69    }..  }..  sqli
3f847 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
3f848 53 63 68 65 6d 61 28 64 62 2c 20 2d 31 29 3b 0d  Schema(db, -1);.
3f849 0a 0d 0a 20 20 2f 2a 20 54 65 6c 6c 20 74 68 65  ...  /* Tell the
3f84a 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e   code in notify.
3f84b 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65  c that the conne
3f84c 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  ction no longer 
3f84d 68 6f 6c 64 73 20 61 6e 79 0d 0a 20 20 2a 2a 20  holds any..  ** 
3f84e 6c 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20 6e  locks and does n
3f84f 6f 74 20 72 65 71 75 69 72 65 20 61 6e 79 20 66  ot require any f
3f850 75 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e 6f  urther unlock-no
3f851 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0d  tify callbacks..
3f852 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  .  */..  sqlite3
3f853 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
3f854 28 64 62 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  (db);....  asser
3f855 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
3f856 0d 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ..  assert( db->
3f857 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74  aDb==db->aDbStat
3f858 69 63 20 29 3b 0d 0a 20 20 66 6f 72 28 6a 3d 30  ic );..  for(j=0
3f859 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62  ; j<ArraySize(db
3f85a 2d 3e 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29  ->aFunc.a); j++)
3f85b 7b 0d 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  {..    FuncDef *
3f85c 70 4e 65 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a  pNext, *pHash, *
3f85d 70 3b 0d 0a 20 20 20 20 66 6f 72 28 70 3d 64 62  p;..    for(p=db
3f85e 2d 3e 61 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b  ->aFunc.a[j]; p;
3f85f 20 70 3d 70 48 61 73 68 29 7b 0d 0a 20 20 20 20   p=pHash){..    
3f860 20 20 70 48 61 73 68 20 3d 20 70 2d 3e 70 48 61    pHash = p->pHa
3f861 73 68 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65  sh;..      while
3f862 28 20 70 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ( p ){..        
3f863 66 75 6e 63 74 69 6f 6e 44 65 73 74 72 6f 79 28  functionDestroy(
3f864 64 62 2c 20 70 29 3b 0d 0a 20 20 20 20 20 20 20  db, p);..       
3f865 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78   pNext = p->pNex
3f866 74 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t;..        sqli
3f867 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
3f868 3b 0d 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  ;..        p = p
3f869 4e 65 78 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  Next;..      }..
3f86a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 66 6f      }..  }..  fo
3f86b 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
3f86c 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
3f86d 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65 48  q); i; i=sqliteH
3f86e 61 73 68 4e 65 78 74 28 69 29 29 7b 0d 0a 20 20  ashNext(i)){..  
3f86f 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
3f870 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71   = (CollSeq *)sq
3f871 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b  liteHashData(i);
3f872 0d 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ..    /* Invoke 
3f873 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
3f874 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
3f875 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3f876 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0d  e user data. */.
3f877 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
3f878 33 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  3; j++){..      
3f879 69 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65  if( pColl[j].xDe
3f87a 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 43  l ){..        pC
3f87b 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c  oll[j].xDel(pCol
3f87c 6c 5b 6a 5d 2e 70 55 73 65 72 29 3b 0d 0a 20 20  l[j].pUser);..  
3f87d 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
3f87e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
3f87f 64 62 2c 20 70 43 6f 6c 6c 29 3b 0d 0a 20 20 7d  db, pColl);..  }
3f880 0d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  ..  sqlite3HashC
3f881 6c 65 61 72 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  lear(&db->aCollS
3f882 65 71 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51  eq);..#ifndef SQ
3f883 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
3f884 4c 54 41 42 4c 45 0d 0a 20 20 66 6f 72 28 69 3d  LTABLE..  for(i=
3f885 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
3f886 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20 69  &db->aModule); i
3f887 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; i=sqliteHashNe
3f888 78 74 28 69 29 29 7b 0d 0a 20 20 20 20 4d 6f 64  xt(i)){..    Mod
3f889 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64  ule *pMod = (Mod
3f88a 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ule *)sqliteHash
3f88b 44 61 74 61 28 69 29 3b 0d 0a 20 20 20 20 69 66  Data(i);..    if
3f88c 28 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79  ( pMod->xDestroy
3f88d 20 29 7b 0d 0a 20 20 20 20 20 20 70 4d 6f 64 2d   ){..      pMod-
3f88e 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e  >xDestroy(pMod->
3f88f 70 41 75 78 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20  pAux);..    }.. 
3f890 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
3f891 28 64 62 2c 20 70 4d 6f 64 29 3b 0d 0a 20 20 7d  (db, pMod);..  }
3f892 0d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  ..  sqlite3HashC
3f893 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  lear(&db->aModul
3f894 65 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  e);..#endif.... 
3f895 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
3f896 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
3f897 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 73 20   /* Deallocates 
3f898 61 6e 79 20 63 61 63 68 65 64 20 65 72 72 6f 72  any cached error
3f899 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0d 0a 20 20   strings. */..  
3f89a 69 66 28 20 64 62 2d 3e 70 45 72 72 20 29 7b 0d  if( db->pErr ){.
3f89b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
3f89c 65 46 72 65 65 28 64 62 2d 3e 70 45 72 72 29 3b  eFree(db->pErr);
3f89d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33  ..  }..  sqlite3
3f89e 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28  CloseExtensions(
3f89f 64 62 29 3b 0d 0a 0d 0a 20 20 64 62 2d 3e 6d 61  db);....  db->ma
3f8a0 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
3f8a1 49 43 5f 45 52 52 4f 52 3b 0d 0a 0d 0a 20 20 2f  IC_ERROR;....  /
3f8a2 2a 20 54 68 65 20 74 65 6d 70 2d 64 61 74 61 62  * The temp-datab
3f8a3 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c  ase schema is al
3f8a4 6c 6f 63 61 74 65 64 20 64 69 66 66 65 72 65 6e  located differen
3f8a5 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68  tly from the oth
3f8a6 65 72 20 73 63 68 65 6d 61 0d 0a 20 20 2a 2a 20  er schema..  ** 
3f8a7 6f 62 6a 65 63 74 73 20 28 75 73 69 6e 67 20 73  objects (using s
3f8a8 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64 69  qliteMalloc() di
3f8a9 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20  rectly, instead 
3f8aa 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  of sqlite3BtreeS
3f8ab 63 68 65 6d 61 28 29 29 2e 0d 0a 20 20 2a 2a 20  chema())...  ** 
3f8ac 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  So it needs to b
3f8ad 65 20 66 72 65 65 64 20 68 65 72 65 2e 20 54 6f  e freed here. To
3f8ae 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c  do: Why not roll
3f8af 20 74 68 65 20 74 65 6d 70 20 73 63 68 65 6d 61   the temp schema
3f8b0 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20 74 68 65 20   into..  ** the 
3f8b1 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  same sqliteMallo
3f8b2 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20 74  c() as the one t
3f8b3 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74 68  hat allocates th
3f8b4 65 20 64 61 74 61 62 61 73 65 20 0d 0a 20 20 2a  e database ..  *
3f8b5 2a 20 73 74 72 75 63 74 75 72 65 3f 0d 0a 20 20  * structure?..  
3f8b6 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 44 62 46  */..  sqlite3DbF
3f8b7 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  ree(db, db->aDb[
3f8b8 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0d 0a 20 20  1].pSchema);..  
3f8b9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3f8ba 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ave(db->mutex);.
3f8bb 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53  .  db->magic = S
3f8bc 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53  QLITE_MAGIC_CLOS
3f8bd 45 44 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ED;..  sqlite3_m
3f8be 75 74 65 78 5f 66 72 65 65 28 64 62 2d 3e 6d 75  utex_free(db->mu
3f8bf 74 65 78 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  tex);..  assert(
3f8c0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
3f8c1 4f 75 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 46 61  Out==0 );  /* Fa
3f8c2 69 6c 73 20 6f 6e 20 61 20 6c 6f 6f 6b 61 73 69  ils on a lookasi
3f8c3 64 65 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 2a  de memory leak *
3f8c4 2f 0d 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f  /..  if( db->loo
3f8c5 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64  kaside.bMalloced
3f8c6 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
3f8c7 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73  _free(db->lookas
3f8c8 69 64 65 2e 70 53 74 61 72 74 29 3b 0d 0a 20 20  ide.pStart);..  
3f8c9 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
3f8ca 65 28 64 62 29 3b 0d 0a 20 20 72 65 74 75 72 6e  e(db);..  return
3f8cb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
3f8cc 0d 0a 2f 2a 0d 0a 2a 2a 20 52 6f 6c 6c 62 61 63  ../*..** Rollbac
3f8cd 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66  k all database f
3f8ce 69 6c 65 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  iles...*/..SQLIT
3f8cf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3f8d0 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
3f8d1 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d  l(sqlite3 *db){.
3f8d2 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 69 6e 74  .  int i;..  int
3f8d3 20 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0d 0a 20   inTrans = 0;.. 
3f8d4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
3f8d5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
3f8d6 6d 75 74 65 78 29 20 29 3b 0d 0a 20 20 73 71 6c  mutex) );..  sql
3f8d7 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
3f8d8 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 66 6f 72 28  alloc();..  for(
3f8d9 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
3f8da 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20 64  i++){..    if( d
3f8db 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 20 29 7b  b->aDb[i].pBt ){
3f8dc 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ..      if( sqli
3f8dd 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
3f8de 73 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  s(db->aDb[i].pBt
3f8df 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  ) ){..        in
3f8e0 54 72 61 6e 73 20 3d 20 31 3b 0d 0a 20 20 20 20  Trans = 1;..    
3f8e1 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69 74    }..      sqlit
3f8e2 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
3f8e3 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
3f8e4 0d 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  ..      db->aDb[
3f8e5 69 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0d  i].inTrans = 0;.
3f8e6 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73  .    }..  }..  s
3f8e7 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
3f8e8 63 6b 28 64 62 29 3b 0d 0a 20 20 73 71 6c 69 74  ck(db);..  sqlit
3f8e9 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
3f8ea 63 28 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 64 62  c();....  if( db
3f8eb 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49  ->flags&SQLITE_I
3f8ec 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0d  nternChanges ){.
3f8ed 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69  .    sqlite3Expi
3f8ee 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d  rePreparedStatem
3f8ef 65 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20 20 73  ents(db);..    s
3f8f0 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
3f8f1 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 2d 31  nalSchema(db, -1
3f8f2 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  );..  }....  /* 
3f8f3 41 6e 79 20 64 65 66 65 72 72 65 64 20 63 6f 6e  Any deferred con
3f8f4 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
3f8f5 6e 73 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e  ns have now been
3f8f6 20 72 65 73 6f 6c 76 65 64 2e 20 2a 2f 0d 0a 20   resolved. */.. 
3f8f7 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f   db->nDeferredCo
3f8f8 6e 73 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20  ns = 0;....  /* 
3f8f9 49 66 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20  If one has been 
3f8fa 63 6f 6e 66 69 67 75 72 65 64 2c 20 69 6e 76 6f  configured, invo
3f8fb 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2d  ke the rollback-
3f8fc 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  hook callback */
3f8fd 0d 0a 20 20 69 66 28 20 64 62 2d 3e 78 52 6f 6c  ..  if( db->xRol
3f8fe 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20 26 26  lbackCallback &&
3f8ff 20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21 64 62   (inTrans || !db
3f900 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20 29 7b  ->autoCommit) ){
3f901 0d 0a 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c 62  ..    db->xRollb
3f902 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  ackCallback(db->
3f903 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0d 0a  pRollbackArg);..
3f904 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
3f905 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69 63   Return a static
3f906 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73   string that des
3f907 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64 20  cribes the kind 
3f908 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66 69  of error specifi
3f909 65 64 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 61 72  ed in the..** ar
3f90a 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  gument...*/..SQL
3f90b 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
3f90c 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
3f90d 72 72 53 74 72 28 69 6e 74 20 72 63 29 7b 0d 0a  rrStr(int rc){..
3f90e 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
3f90f 68 61 72 2a 20 63 6f 6e 73 74 20 61 4d 73 67 5b  har* const aMsg[
3f910 5d 20 3d 20 7b 0d 0a 20 20 20 20 2f 2a 20 53 51  ] = {..    /* SQ
3f911 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20  LITE_OK         
3f912 20 2a 2f 20 22 6e 6f 74 20 61 6e 20 65 72 72 6f   */ "not an erro
3f913 72 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  r",..    /* SQLI
3f914 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 2a  TE_ERROR       *
3f915 2f 20 22 53 51 4c 20 6c 6f 67 69 63 20 65 72 72  / "SQL logic err
3f916 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61  or or missing da
3f917 74 61 62 61 73 65 22 2c 0d 0a 20 20 20 20 2f 2a  tabase",..    /*
3f918 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c   SQLITE_INTERNAL
3f919 20 20 20 20 2a 2f 20 30 2c 0d 0a 20 20 20 20 2f      */ 0,..    /
3f91a 2a 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20  * SQLITE_PERM   
3f91b 20 20 20 20 20 2a 2f 20 22 61 63 63 65 73 73 20       */ "access 
3f91c 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65  permission denie
3f91d 64 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  d",..    /* SQLI
3f91e 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 2a  TE_ABORT       *
3f91f 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75  / "callback requ
3f920 65 73 74 65 64 20 71 75 65 72 79 20 61 62 6f 72  ested query abor
3f921 74 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  t",..    /* SQLI
3f922 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 2a  TE_BUSY        *
3f923 2f 20 22 64 61 74 61 62 61 73 65 20 69 73 20 6c  / "database is l
3f924 6f 63 6b 65 64 22 2c 0d 0a 20 20 20 20 2f 2a 20  ocked",..    /* 
3f925 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20  SQLITE_LOCKED   
3f926 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20     */ "database 
3f927 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 22  table is locked"
3f928 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ,..    /* SQLITE
3f929 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20  _NOMEM       */ 
3f92a 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c  "out of memory",
3f92b 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ..    /* SQLITE_
3f92c 52 45 41 44 4f 4e 4c 59 20 20 20 20 2a 2f 20 22  READONLY    */ "
3f92d 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
3f92e 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61   a readonly data
3f92f 62 61 73 65 22 2c 0d 0a 20 20 20 20 2f 2a 20 53  base",..    /* S
3f930 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20  QLITE_INTERRUPT 
3f931 20 20 2a 2f 20 22 69 6e 74 65 72 72 75 70 74 65    */ "interrupte
3f932 64 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  d",..    /* SQLI
3f933 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 2a  TE_IOERR       *
3f934 2f 20 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f  / "disk I/O erro
3f935 72 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  r",..    /* SQLI
3f936 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 2a  TE_CORRUPT     *
3f937 2f 20 22 64 61 74 61 62 61 73 65 20 64 69 73 6b  / "database disk
3f938 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72   image is malfor
3f939 6d 65 64 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51  med",..    /* SQ
3f93a 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20  LITE_NOTFOUND   
3f93b 20 2a 2f 20 22 75 6e 6b 6e 6f 77 6e 20 6f 70 65   */ "unknown ope
3f93c 72 61 74 69 6f 6e 22 2c 0d 0a 20 20 20 20 2f 2a  ration",..    /*
3f93d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20   SQLITE_FULL    
3f93e 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65      */ "database
3f93f 20 6f 72 20 64 69 73 6b 20 69 73 20 66 75 6c 6c   or disk is full
3f940 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  ",..    /* SQLIT
3f941 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 2a 2f  E_CANTOPEN    */
3f942 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e   "unable to open
3f943 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 22 2c   database file",
3f944 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ..    /* SQLITE_
3f945 50 52 4f 54 4f 43 4f 4c 20 20 20 20 2a 2f 20 22  PROTOCOL    */ "
3f946 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c  locking protocol
3f947 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  ",..    /* SQLIT
3f948 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 2a 2f  E_EMPTY       */
3f949 20 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73   "table contains
3f94a 20 6e 6f 20 64 61 74 61 22 2c 0d 0a 20 20 20 20   no data",..    
3f94b 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  /* SQLITE_SCHEMA
3f94c 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61        */ "databa
3f94d 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  se schema has ch
3f94e 61 6e 67 65 64 22 2c 0d 0a 20 20 20 20 2f 2a 20  anged",..    /* 
3f94f 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20  SQLITE_TOOBIG   
3f950 20 20 20 2a 2f 20 22 73 74 72 69 6e 67 20 6f 72     */ "string or
3f951 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 0d   blob too big",.
3f952 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
3f953 4f 4e 53 54 52 41 49 4e 54 20 20 2a 2f 20 22 63  ONSTRAINT  */ "c
3f954 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
3f955 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  ",..    /* SQLIT
3f956 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 2a 2f  E_MISMATCH    */
3f957 20 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61   "datatype misma
3f958 74 63 68 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51  tch",..    /* SQ
3f959 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20  LITE_MISUSE     
3f95a 20 2a 2f 20 22 6c 69 62 72 61 72 79 20 72 6f 75   */ "library rou
3f95b 74 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20  tine called out 
3f95c 6f 66 20 73 65 71 75 65 6e 63 65 22 2c 0d 0a 20  of sequence",.. 
3f95d 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4c     /* SQLITE_NOL
3f95e 46 53 20 20 20 20 20 20 20 2a 2f 20 22 6c 61 72  FS       */ "lar
3f95f 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20  ge file support 
3f960 69 73 20 64 69 73 61 62 6c 65 64 22 2c 0d 0a 20  is disabled",.. 
3f961 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 55 54     /* SQLITE_AUT
3f962 48 20 20 20 20 20 20 20 20 2a 2f 20 22 61 75 74  H        */ "aut
3f963 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65  horization denie
3f964 64 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c 49  d",..    /* SQLI
3f965 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 2a  TE_FORMAT      *
3f966 2f 20 22 61 75 78 69 6c 69 61 72 79 20 64 61 74  / "auxiliary dat
3f967 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72  abase format err
3f968 6f 72 22 2c 0d 0a 20 20 20 20 2f 2a 20 53 51 4c  or",..    /* SQL
3f969 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20  ITE_RANGE       
3f96a 2a 2f 20 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75  */ "bind or colu
3f96b 6d 6e 20 69 6e 64 65 78 20 6f 75 74 20 6f 66 20  mn index out of 
3f96c 72 61 6e 67 65 22 2c 0d 0a 20 20 20 20 2f 2a 20  range",..    /* 
3f96d 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
3f96e 20 20 20 2a 2f 20 22 66 69 6c 65 20 69 73 20 65     */ "file is e
3f96f 6e 63 72 79 70 74 65 64 20 6f 72 20 69 73 20 6e  ncrypted or is n
3f970 6f 74 20 61 20 64 61 74 61 62 61 73 65 22 2c 0d  ot a database",.
3f971 0a 20 20 7d 3b 0d 0a 20 20 72 63 20 26 3d 20 30  .  };..  rc &= 0
3f972 78 66 66 3b 0d 0a 20 20 69 66 28 20 41 4c 57 41  xff;..  if( ALWA
3f973 59 53 28 72 63 3e 3d 30 29 20 26 26 20 72 63 3c  YS(rc>=0) && rc<
3f974 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61 4d 73  (int)(sizeof(aMs
3f975 67 29 2f 73 69 7a 65 6f 66 28 61 4d 73 67 5b 30  g)/sizeof(aMsg[0
3f976 5d 29 29 20 26 26 20 61 4d 73 67 5b 72 63 5d 21  ])) && aMsg[rc]!
3f977 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  =0 ){..    retur
3f978 6e 20 61 4d 73 67 5b 72 63 5d 3b 0d 0a 20 20 7d  n aMsg[rc];..  }
3f979 65 6c 73 65 7b 0d 0a 20 20 20 20 72 65 74 75 72  else{..    retur
3f97a 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72  n "unknown error
3f97b 22 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  ";..  }..}..../*
3f97c 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
3f97d 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 62  e implements a b
3f97e 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  usy callback tha
3f97f 74 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72 69  t sleeps and tri
3f980 65 73 0d 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74  es..** again unt
3f981 69 6c 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c  il a timeout val
3f982 75 65 20 69 73 20 72 65 61 63 68 65 64 2e 20 20  ue is reached.  
3f983 54 68 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75  The timeout valu
3f984 65 20 69 73 0d 0a 2a 2a 20 61 6e 20 69 6e 74 65  e is..** an inte
3f985 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  ger number of mi
3f986 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73 73 65  lliseconds passe
3f987 64 20 69 6e 20 61 73 20 74 68 65 20 66 69 72 73  d in as the firs
3f988 74 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0d  t..** argument..
3f989 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
3f98a 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73  sqliteDefaultBus
3f98b 79 43 61 6c 6c 62 61 63 6b 28 0d 0a 20 76 6f 69  yCallback(.. voi
3f98c 64 20 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20  d *ptr,         
3f98d 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
3f98e 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
3f98f 0a 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20  . int count     
3f990 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3f991 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 61  mber of times ta
3f992 62 6c 65 20 68 61 73 20 62 65 65 6e 20 62 75 73  ble has been bus
3f993 79 20 2a 2f 0d 0a 29 7b 0d 0a 23 69 66 20 53 51  y */..){..#if SQ
3f994 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 7c 7c 20 28  LITE_OS_WIN || (
3f995 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c  defined(HAVE_USL
3f996 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c  EEP) && HAVE_USL
3f997 45 45 50 29 0d 0a 20 20 73 74 61 74 69 63 20 63  EEP)..  static c
3f998 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73 5b 5d  onst u8 delays[]
3f999 20 3d 0d 0a 20 20 20 20 20 7b 20 31 2c 20 32 2c   =..     { 1, 2,
3f99a 20 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c 20   5, 10, 15, 20, 
3f99b 32 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35 30  25, 25,  25,  50
3f99c 2c 20 20 35 30 2c 20 31 30 30 20 7d 3b 0d 0a 20  ,  50, 100 };.. 
3f99d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
3f99e 20 74 6f 74 61 6c 73 5b 5d 20 3d 0d 0a 20 20 20   totals[] =..   
3f99f 20 20 7b 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c    { 0, 1, 3,  8,
3f9a0 20 31 38 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c   18, 33, 53, 78,
3f9a1 20 31 30 33 2c 20 31 32 38 2c 20 31 37 38 2c 20   103, 128, 178, 
3f9a2 32 32 38 20 7d 3b 0d 0a 23 20 64 65 66 69 6e 65  228 };..# define
3f9a3 20 4e 44 45 4c 41 59 20 41 72 72 61 79 53 69 7a   NDELAY ArraySiz
3f9a4 65 28 64 65 6c 61 79 73 29 0d 0a 20 20 73 71 6c  e(delays)..  sql
3f9a5 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69  ite3 *db = (sqli
3f9a6 74 65 33 20 2a 29 70 74 72 3b 0d 0a 20 20 69 6e  te3 *)ptr;..  in
3f9a7 74 20 74 69 6d 65 6f 75 74 20 3d 20 64 62 2d 3e  t timeout = db->
3f9a8 62 75 73 79 54 69 6d 65 6f 75 74 3b 0d 0a 20 20  busyTimeout;..  
3f9a9 69 6e 74 20 64 65 6c 61 79 2c 20 70 72 69 6f 72  int delay, prior
3f9aa 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 63  ;....  assert( c
3f9ab 6f 75 6e 74 3e 3d 30 20 29 3b 0d 0a 20 20 69 66  ount>=0 );..  if
3f9ac 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41 59  ( count < NDELAY
3f9ad 20 29 7b 0d 0a 20 20 20 20 64 65 6c 61 79 20 3d   ){..    delay =
3f9ae 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0d   delays[count];.
3f9af 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74  .    prior = tot
3f9b0 61 6c 73 5b 63 6f 75 6e 74 5d 3b 0d 0a 20 20 7d  als[count];..  }
3f9b1 65 6c 73 65 7b 0d 0a 20 20 20 20 64 65 6c 61 79  else{..    delay
3f9b2 20 3d 20 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59   = delays[NDELAY
3f9b3 2d 31 5d 3b 0d 0a 20 20 20 20 70 72 69 6f 72 20  -1];..    prior 
3f9b4 3d 20 74 6f 74 61 6c 73 5b 4e 44 45 4c 41 59 2d  = totals[NDELAY-
3f9b5 31 5d 20 2b 20 64 65 6c 61 79 2a 28 63 6f 75 6e  1] + delay*(coun
3f9b6 74 2d 28 4e 44 45 4c 41 59 2d 31 29 29 3b 0d 0a  t-(NDELAY-1));..
3f9b7 20 20 7d 0d 0a 20 20 69 66 28 20 70 72 69 6f 72    }..  if( prior
3f9b8 20 2b 20 64 65 6c 61 79 20 3e 20 74 69 6d 65 6f   + delay > timeo
3f9b9 75 74 20 29 7b 0d 0a 20 20 20 20 64 65 6c 61 79  ut ){..    delay
3f9ba 20 3d 20 74 69 6d 65 6f 75 74 20 2d 20 70 72 69   = timeout - pri
3f9bb 6f 72 3b 0d 0a 20 20 20 20 69 66 28 20 64 65 6c  or;..    if( del
3f9bc 61 79 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ay<=0 ) return 0
3f9bd 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
3f9be 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66  3OsSleep(db->pVf
3f9bf 73 2c 20 64 65 6c 61 79 2a 31 30 30 30 29 3b 0d  s, delay*1000);.
3f9c0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 23 65  .  return 1;..#e
3f9c1 6c 73 65 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  lse..  sqlite3 *
3f9c2 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29  db = (sqlite3 *)
3f9c3 70 74 72 3b 0d 0a 20 20 69 6e 74 20 74 69 6d 65  ptr;..  int time
3f9c4 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65 33 20  out = ((sqlite3 
3f9c5 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69 6d 65  *)ptr)->busyTime
3f9c6 6f 75 74 3b 0d 0a 20 20 69 66 28 20 28 63 6f 75  out;..  if( (cou
3f9c7 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69 6d  nt+1)*1000 > tim
3f9c8 65 6f 75 74 20 29 7b 0d 0a 20 20 20 20 72 65 74  eout ){..    ret
3f9c9 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73  urn 0;..  }..  s
3f9ca 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62  qlite3OsSleep(db
3f9cb 2d 3e 70 56 66 73 2c 20 31 30 30 30 30 30 30 29  ->pVfs, 1000000)
3f9cc 3b 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a  ;..  return 1;..
3f9cd 23 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  #endif..}..../*.
3f9ce 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67  .** Invoke the g
3f9cf 69 76 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65  iven busy handle
3f9d0 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  r...**..** This 
3f9d1 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
3f9d2 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74  d when an operat
3f9d3 69 6f 6e 20 66 61 69 6c 65 64 20 77 69 74 68 20  ion failed with 
3f9d4 61 20 6c 6f 63 6b 2e 0d 0a 2a 2a 20 49 66 20 74  a lock...** If t
3f9d5 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
3f9d6 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
3f9d7 65 20 6c 6f 63 6b 20 69 73 20 72 65 74 72 69 65  e lock is retrie
3f9d8 64 2e 20 20 49 66 20 69 74 0d 0a 2a 2a 20 72 65  d.  If it..** re
3f9d9 74 75 72 6e 73 20 30 2c 20 74 68 65 20 6f 70 65  turns 0, the ope
3f9da 72 61 74 69 6f 6e 20 61 62 6f 72 74 73 20 77 69  ration aborts wi
3f9db 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 42 55 53  th an SQLITE_BUS
3f9dc 59 20 65 72 72 6f 72 2e 0d 0a 2a 2f 0d 0a 53 51  Y error...*/..SQ
3f9dd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3f9de 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
3f9df 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61  syHandler(BusyHa
3f9e0 6e 64 6c 65 72 20 2a 70 29 7b 0d 0a 20 20 69 6e  ndler *p){..  in
3f9e1 74 20 72 63 3b 0d 0a 20 20 69 66 28 20 4e 45 56  t rc;..  if( NEV
3f9e2 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78  ER(p==0) || p->x
3f9e3 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42  Func==0 || p->nB
3f9e4 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30  usy<0 ) return 0
3f9e5 3b 0d 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75  ;..  rc = p->xFu
3f9e6 6e 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e  nc(p->pArg, p->n
3f9e7 42 75 73 79 29 3b 0d 0a 20 20 69 66 28 20 72 63  Busy);..  if( rc
3f9e8 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6e  ==0 ){..    p->n
3f9e9 42 75 73 79 20 3d 20 2d 31 3b 0d 0a 20 20 7d 65  Busy = -1;..  }e
3f9ea 6c 73 65 7b 0d 0a 20 20 20 20 70 2d 3e 6e 42 75  lse{..    p->nBu
3f9eb 73 79 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  sy++;..  }..  re
3f9ec 74 75 72 6e 20 72 63 3b 20 0d 0a 7d 0d 0a 0d 0a  turn rc; ..}....
3f9ed 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /*..** This rout
3f9ee 69 6e 65 20 73 65 74 73 20 74 68 65 20 62 75 73  ine sets the bus
3f9ef 79 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  y callback for a
3f9f0 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73  n Sqlite databas
3f9f1 65 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 67 69 76  e to the..** giv
3f9f2 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  en callback func
3f9f3 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69  tion with the gi
3f9f4 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a  ven argument...*
3f9f5 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
3f9f6 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68  t sqlite3_busy_h
3f9f7 61 6e 64 6c 65 72 28 0d 0a 20 20 73 71 6c 69 74  andler(..  sqlit
3f9f8 65 33 20 2a 64 62 2c 0d 0a 20 20 69 6e 74 20 28  e3 *db,..  int (
3f9f9 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e  *xBusy)(void*,in
3f9fa 74 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72  t),..  void *pAr
3f9fb 67 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  g..){..  sqlite3
3f9fc 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
3f9fd 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 64 62 2d 3e  >mutex);..  db->
3f9fe 62 75 73 79 48 61 6e 64 6c 65 72 2e 78 46 75 6e  busyHandler.xFun
3f9ff 63 20 3d 20 78 42 75 73 79 3b 0d 0a 20 20 64 62  c = xBusy;..  db
3fa00 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 70 41  ->busyHandler.pA
3fa01 72 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 64 62  rg = pArg;..  db
3fa02 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42  ->busyHandler.nB
3fa03 75 73 79 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69  usy = 0;..  sqli
3fa04 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
3fa05 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72  db->mutex);..  r
3fa06 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3fa07 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  ..}....#ifndef S
3fa08 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
3fa09 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0d 0a 2f 2a  ESS_CALLBACK../*
3fa0a 0d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
3fa0b 65 20 73 65 74 73 20 74 68 65 20 70 72 6f 67 72  e sets the progr
3fa0c 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72  ess callback for
3fa0d 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62   an Sqlite datab
3fa0e 61 73 65 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 67  ase to the..** g
3fa0f 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75  iven callback fu
3fa10 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
3fa11 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20  given argument. 
3fa12 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  The progress cal
3fa13 6c 62 61 63 6b 20 77 69 6c 6c 0d 0a 2a 2a 20 62  lback will..** b
3fa14 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79 20  e invoked every 
3fa15 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0d 0a 2a  nOps opcodes...*
3fa16 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  /..SQLITE_API vo
3fa17 69 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  id sqlite3_progr
3fa18 65 73 73 5f 68 61 6e 64 6c 65 72 28 0d 0a 20 20  ess_handler(..  
3fa19 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0d 0a 20  sqlite3 *db, .. 
3fa1a 20 69 6e 74 20 6e 4f 70 73 2c 0d 0a 20 20 69 6e   int nOps,..  in
3fa1b 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76  t (*xProgress)(v
3fa1c 6f 69 64 2a 29 2c 20 0d 0a 20 20 76 6f 69 64 20  oid*), ..  void 
3fa1d 2a 70 41 72 67 0d 0a 29 7b 0d 0a 20 20 73 71 6c  *pArg..){..  sql
3fa1e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3fa1f 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fa20 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b 0d 0a 20  if( nOps>0 ){.. 
3fa21 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73     db->xProgress
3fa22 20 3d 20 78 50 72 6f 67 72 65 73 73 3b 0d 0a 20   = xProgress;.. 
3fa23 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
3fa24 4f 70 73 20 3d 20 6e 4f 70 73 3b 0d 0a 20 20 20  Ops = nOps;..   
3fa25 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72   db->pProgressAr
3fa26 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 7d 65 6c  g = pArg;..  }el
3fa27 73 65 7b 0d 0a 20 20 20 20 64 62 2d 3e 78 50 72  se{..    db->xPr
3fa28 6f 67 72 65 73 73 20 3d 20 30 3b 0d 0a 20 20 20  ogress = 0;..   
3fa29 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70   db->nProgressOp
3fa2a 73 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62 2d 3e  s = 0;..    db->
3fa2b 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20 30  pProgressArg = 0
3fa2c 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65  ;..  }..  sqlite
3fa2d 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3fa2e 2d 3e 6d 75 74 65 78 29 3b 0d 0a 7d 0d 0a 23 65  ->mutex);..}..#e
3fa2f 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ndif....../*..**
3fa30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e   This routine in
3fa31 73 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c 74  stalls a default
3fa32 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68   busy handler th
3fa33 61 74 20 77 61 69 74 73 20 66 6f 72 20 74 68 65  at waits for the
3fa34 0d 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e  ..** specified n
3fa35 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
3fa36 63 6f 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74  conds before ret
3fa37 75 72 6e 69 6e 67 20 30 2e 0d 0a 2a 2f 0d 0a 53  urning 0...*/..S
3fa38 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3fa39 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
3fa3a 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ut(sqlite3 *db, 
3fa3b 69 6e 74 20 6d 73 29 7b 0d 0a 20 20 69 66 28 20  int ms){..  if( 
3fa3c 6d 73 3e 30 20 29 7b 0d 0a 20 20 20 20 64 62 2d  ms>0 ){..    db-
3fa3d 3e 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d  >busyTimeout = m
3fa3e 73 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  s;..    sqlite3_
3fa3f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c  busy_handler(db,
3fa40 20 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75   sqliteDefaultBu
3fa41 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69  syCallback, (voi
3fa42 64 2a 29 64 62 29 3b 0d 0a 20 20 7d 65 6c 73 65  d*)db);..  }else
3fa43 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  {..    sqlite3_b
3fa44 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20  usy_handler(db, 
3fa45 30 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  0, 0);..  }..  r
3fa46 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3fa47 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61  ..}..../*..** Ca
3fa48 75 73 65 20 61 6e 79 20 70 65 6e 64 69 6e 67 20  use any pending 
3fa49 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 73 74 6f  operation to sto
3fa4a 70 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 73  p at its earlies
3fa4b 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 0d 0a  t opportunity...
3fa4c 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  */..SQLITE_API v
3fa4d 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  oid sqlite3_inte
3fa4e 72 72 75 70 74 28 73 71 6c 69 74 65 33 20 2a 64  rrupt(sqlite3 *d
3fa4f 62 29 7b 0d 0a 20 20 64 62 2d 3e 75 31 2e 69 73  b){..  db->u1.is
3fa50 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b  Interrupted = 1;
3fa51 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
3fa52 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
3fa53 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d   exactly the sam
3fa54 65 20 61 73 20 73 71 6c 69 74 65 33 5f 63 72 65  e as sqlite3_cre
3fa55 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20  ate_function(), 
3fa56 65 78 63 65 70 74 0d 0a 2a 2a 20 74 68 61 74 20  except..** that 
3fa57 69 74 20 69 73 20 64 65 73 69 67 6e 65 64 20 74  it is designed t
3fa58 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 69  o be called by i
3fa59 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e 20 54 68  nternal code. Th
3fa5a 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 0d  e difference is.
3fa5b 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20 6d 61  .** that if a ma
3fa5c 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 6e 20  lloc() fails in 
3fa5d 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
3fa5e 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20 65 72  unction(), an er
3fa5f 72 6f 72 20 63 6f 64 65 0d 0a 2a 2a 20 69 73 20  ror code..** is 
3fa60 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
3fa61 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
3fa62 61 67 20 63 6c 65 61 72 65 64 2e 20 0d 0a 2a 2f  ag cleared. ..*/
3fa63 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
3fa64 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61   int sqlite3Crea
3fa65 74 65 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69 74  teFunc(..  sqlit
3fa66 65 33 20 2a 64 62 2c 0d 0a 20 20 63 6f 6e 73 74  e3 *db,..  const
3fa67 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e   char *zFunction
3fa68 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20 6e 41 72  Name,..  int nAr
3fa69 67 2c 0d 0a 20 20 69 6e 74 20 65 6e 63 2c 0d 0a  g,..  int enc,..
3fa6a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74    void *pUserDat
3fa6b 61 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  a,..  void (*xFu
3fa6c 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
3fa6d 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
3fa6e 5f 76 61 6c 75 65 20 2a 2a 29 2c 0d 0a 20 20 76  _value **),..  v
3fa6f 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
3fa70 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
3fa71 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
3fa72 2a 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78  **),..  void (*x
3fa73 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
3fa74 6f 6e 74 65 78 74 2a 29 2c 0d 0a 20 20 46 75 6e  ontext*),..  Fun
3fa75 63 44 65 73 74 72 75 63 74 6f 72 20 2a 70 44 65  cDestructor *pDe
3fa76 73 74 72 75 63 74 6f 72 0d 0a 29 7b 0d 0a 20 20  structor..){..  
3fa77 46 75 6e 63 44 65 66 20 2a 70 3b 0d 0a 20 20 69  FuncDef *p;..  i
3fa78 6e 74 20 6e 4e 61 6d 65 3b 0d 0a 0d 0a 20 20 61  nt nName;....  a
3fa79 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
3fa7a 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
3fa7b 74 65 78 29 20 29 3b 0d 0a 20 20 69 66 28 20 7a  tex) );..  if( z
3fa7c 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20  FunctionName==0 
3fa7d 7c 7c 0d 0a 20 20 20 20 20 20 28 78 46 75 6e 63  ||..      (xFunc
3fa7e 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78   && (xFinal || x
3fa7f 53 74 65 70 29 29 20 7c 7c 20 0d 0a 20 20 20 20  Step)) || ..    
3fa80 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 78 46    (!xFunc && (xF
3fa81 69 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29 29  inal && !xStep))
3fa82 20 7c 7c 0d 0a 20 20 20 20 20 20 28 21 78 46 75   ||..      (!xFu
3fa83 6e 63 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26  nc && (!xFinal &
3fa84 26 20 78 53 74 65 70 29 29 20 7c 7c 0d 0a 20 20  & xStep)) ||..  
3fa85 20 20 20 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20      (nArg<-1 || 
3fa86 6e 41 72 67 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  nArg>SQLITE_MAX_
3fa87 46 55 4e 43 54 49 4f 4e 5f 41 52 47 29 20 7c 7c  FUNCTION_ARG) ||
3fa88 0d 0a 20 20 20 20 20 20 28 32 35 35 3c 28 6e 4e  ..      (255<(nN
3fa89 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
3fa8a 6c 65 6e 33 30 28 20 7a 46 75 6e 63 74 69 6f 6e  len30( zFunction
3fa8b 4e 61 6d 65 29 29 29 20 29 7b 0d 0a 20 20 20 20  Name))) ){..    
3fa8c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3fa8d 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d  SUSE_BKPT;..  }.
3fa8e 0a 20 20 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  ..#ifndef SQL
3fa8f 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0d 0a  ITE_OMIT_UTF16..
3fa90 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55    /* If SQLITE_U
3fa91 54 46 31 36 20 69 73 20 73 70 65 63 69 66 69 65  TF16 is specifie
3fa92 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  d as the encodin
3fa93 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72  g type, transfor
3fa94 6d 20 74 68 69 73 0d 0a 20 20 2a 2a 20 74 6f 20  m this..  ** to 
3fa95 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54  one of SQLITE_UT
3fa96 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f  F16LE or SQLITE_
3fa97 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74 68  UTF16BE using th
3fa98 65 0d 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  e..  ** SQLITE_U
3fa99 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
3fa9a 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
3fa9b 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
3fa9c 6e 61 6c 6c 79 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  nally...  **..  
3fa9d 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 41 4e 59  ** If SQLITE_ANY
3fa9e 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 61   is specified, a
3fa9f 64 64 20 74 68 72 65 65 20 76 65 72 73 69 6f 6e  dd three version
3faa0 73 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  s of the functio
3faa1 6e 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 68  n..  ** to the h
3faa2 61 73 68 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f  ash table...  */
3faa3 0d 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  ..  if( enc==SQL
3faa4 49 54 45 5f 55 54 46 31 36 20 29 7b 0d 0a 20 20  ITE_UTF16 ){..  
3faa5 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55    enc = SQLITE_U
3faa6 54 46 31 36 4e 41 54 49 56 45 3b 0d 0a 20 20 7d  TF16NATIVE;..  }
3faa7 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51  else if( enc==SQ
3faa8 4c 49 54 45 5f 41 4e 59 20 29 7b 0d 0a 20 20 20  LITE_ANY ){..   
3faa9 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20 20 72 63   int rc;..    rc
3faaa 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
3faab 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69  Func(db, zFuncti
3faac 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51  onName, nArg, SQ
3faad 4c 49 54 45 5f 55 54 46 38 2c 0d 0a 20 20 20 20  LITE_UTF8,..    
3faae 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20       pUserData, 
3faaf 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  xFunc, xStep, xF
3fab0 69 6e 61 6c 2c 20 70 44 65 73 74 72 75 63 74 6f  inal, pDestructo
3fab1 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  r);..    if( rc=
3fab2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
3fab3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
3fab4 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
3fab5 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
3fab6 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Arg, SQLITE_UTF1
3fab7 36 4c 45 2c 0d 0a 20 20 20 20 20 20 20 20 20 20  6LE,..          
3fab8 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
3fab9 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 2c  , xStep, xFinal,
3faba 20 70 44 65 73 74 72 75 63 74 6f 72 29 3b 0d 0a   pDestructor);..
3fabb 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
3fabc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
3fabd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
3fabe 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 65 6e  ;..    }..    en
3fabf 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c = SQLITE_UTF16
3fac0 42 45 3b 0d 0a 20 20 7d 0d 0a 23 65 6c 73 65 0d  BE;..  }..#else.
3fac1 0a 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  .  enc = SQLITE_
3fac2 55 54 46 38 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20  UTF8;..#endif.. 
3fac3 20 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66   ..  /* Check if
3fac4 20 61 6e 20 65 78 69 73 74 69 6e 67 20 66 75 6e   an existing fun
3fac5 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6f  ction is being o
3fac6 76 65 72 72 69 64 64 65 6e 20 6f 72 20 64 65 6c  verridden or del
3fac7 65 74 65 64 2e 20 49 66 20 73 6f 2c 0d 0a 20 20  eted. If so,..  
3fac8 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  ** and there are
3fac9 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74 68 65   active VMs, the
3faca 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  n return SQLITE_
3facb 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e 63 74  BUSY. If a funct
3facc 69 6f 6e 0d 0a 20 20 2a 2a 20 69 73 20 62 65 69  ion..  ** is bei
3facd 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64 65  ng overridden/de
3face 6c 65 74 65 64 20 62 75 74 20 74 68 65 72 65 20  leted but there 
3facf 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56 4d  are no active VM
3fad0 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0d 0a 20 20  s, allow the..  
3fad1 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ** operation to 
3fad2 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76  continue but inv
3fad3 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63  alidate all prec
3fad4 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
3fad5 74 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 70 20 3d  ts...  */..  p =
3fad6 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
3fad7 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
3fad8 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
3fad9 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 30 29  Arg, (u8)enc, 0)
3fada 3b 0d 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  ;..  if( p && p-
3fadb 3e 69 50 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26  >iPrefEnc==enc &
3fadc 26 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20  & p->nArg==nArg 
3fadd 29 7b 0d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  ){..    if( db->
3fade 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b  activeVdbeCnt ){
3fadf 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ..      sqlite3E
3fae0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
3fae1 42 55 53 59 2c 20 0d 0a 20 20 20 20 20 20 20 20  BUSY, ..        
3fae2 22 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  "unable to delet
3fae3 65 2f 6d 6f 64 69 66 79 20 75 73 65 72 2d 66 75  e/modify user-fu
3fae4 6e 63 74 69 6f 6e 20 64 75 65 20 74 6f 20 61 63  nction due to ac
3fae5 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22  tive statements"
3fae6 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
3fae7 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
3fae8 6c 65 64 20 29 3b 0d 0a 20 20 20 20 20 20 72 65  led );..      re
3fae9 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
3faea 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
3faeb 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69       sqlite3Expi
3faec 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d  rePreparedStatem
3faed 65 6e 74 73 28 64 62 29 3b 0d 0a 20 20 20 20 7d  ents(db);..    }
3faee 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 20 3d 20 73  ..  }....  p = s
3faef 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
3faf0 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
3faf1 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
3faf2 67 2c 20 28 75 38 29 65 6e 63 2c 20 31 29 3b 0d  g, (u8)enc, 1);.
3faf3 0a 20 20 61 73 73 65 72 74 28 70 20 7c 7c 20 64  .  assert(p || d
3faf4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
3faf5 3b 0d 0a 20 20 69 66 28 20 21 70 20 29 7b 0d 0a  ;..  if( !p ){..
3faf6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3faf7 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d  E_NOMEM;..  }...
3faf8 0a 20 20 2f 2a 20 49 66 20 61 6e 20 6f 6c 64 65  .  /* If an olde
3faf9 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  r version of the
3fafa 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
3fafb 20 63 6f 6e 66 69 67 75 72 65 64 20 64 65 73 74   configured dest
3fafc 72 75 63 74 6f 72 20 69 73 0d 0a 20 20 2a 2a 20  ructor is..  ** 
3fafd 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 69  being replaced i
3fafe 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75  nvoke the destru
3faff 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 68 65  ctor function he
3fb00 72 65 2e 20 2a 2f 0d 0a 20 20 66 75 6e 63 74 69  re. */..  functi
3fb01 6f 6e 44 65 73 74 72 6f 79 28 64 62 2c 20 70 29  onDestroy(db, p)
3fb02 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 44 65 73 74  ;....  if( pDest
3fb03 72 75 63 74 6f 72 20 29 7b 0d 0a 20 20 20 20 70  ructor ){..    p
3fb04 44 65 73 74 72 75 63 74 6f 72 2d 3e 6e 52 65 66  Destructor->nRef
3fb05 2b 2b 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e 70  ++;..  }..  p->p
3fb06 44 65 73 74 72 75 63 74 6f 72 20 3d 20 70 44 65  Destructor = pDe
3fb07 73 74 72 75 63 74 6f 72 3b 0d 0a 20 20 70 2d 3e  structor;..  p->
3fb08 66 6c 61 67 73 20 3d 20 30 3b 0d 0a 20 20 70 2d  flags = 0;..  p-
3fb09 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0d  >xFunc = xFunc;.
3fb0a 0a 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78 53  .  p->xStep = xS
3fb0b 74 65 70 3b 0d 0a 20 20 70 2d 3e 78 46 69 6e 61  tep;..  p->xFina
3fb0c 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0d 0a  lize = xFinal;..
3fb0d 20 20 70 2d 3e 70 55 73 65 72 44 61 74 61 20 3d    p->pUserData =
3fb0e 20 70 55 73 65 72 44 61 74 61 3b 0d 0a 20 20 70   pUserData;..  p
3fb0f 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29 6e 41  ->nArg = (u16)nA
3fb10 72 67 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  rg;..  return SQ
3fb11 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
3fb12 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 6e 65 77  *..** Create new
3fb13 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e   user functions.
3fb14 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
3fb15 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
3fb16 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0d 0a 20  ate_function(.. 
3fb17 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20   sqlite3 *db,.. 
3fb18 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
3fb19 6e 63 2c 0d 0a 20 20 69 6e 74 20 6e 41 72 67 2c  nc,..  int nArg,
3fb1a 0d 0a 20 20 69 6e 74 20 65 6e 63 2c 0d 0a 20 20  ..  int enc,..  
3fb1b 76 6f 69 64 20 2a 70 2c 0d 0a 20 20 76 6f 69 64  void *p,..  void
3fb1c 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
3fb1d 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
3fb1e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
3fb1f 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ,..  void (*xSte
3fb20 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
3fb21 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
3fb22 76 61 6c 75 65 20 2a 2a 29 2c 0d 0a 20 20 76 6f  value **),..  vo
3fb23 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
3fb24 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0d 0a  ite3_context*)..
3fb25 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  ){..  return sql
3fb26 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
3fb27 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a 46 75 6e  tion_v2(db, zFun
3fb28 63 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c  c, nArg, enc, p,
3fb29 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 0d 0a   xFunc, xStep,..
3fb2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fb2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fb2c 20 20 20 20 78 46 69 6e 61 6c 2c 20 30 29 3b 0d      xFinal, 0);.
3fb2d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 41 50 49  .}....SQLITE_API
3fb2e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
3fb2f 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28  ate_function_v2(
3fb30 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
3fb31 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
3fb32 7a 46 75 6e 63 2c 0d 0a 20 20 69 6e 74 20 6e 41  zFunc,..  int nA
3fb33 72 67 2c 0d 0a 20 20 69 6e 74 20 65 6e 63 2c 0d  rg,..  int enc,.
3fb34 0a 20 20 76 6f 69 64 20 2a 70 2c 0d 0a 20 20 76  .  void *p,..  v
3fb35 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
3fb36 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
3fb37 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
3fb38 2a 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78  **),..  void (*x
3fb39 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
3fb3a 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
3fb3b 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0d 0a 20  e3_value **),.. 
3fb3c 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
3fb3d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
3fb3e 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  ),..  void (*xDe
3fb3f 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 0d 0a  stroy)(void *)..
3fb40 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
3fb41 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
3fb42 46 75 6e 63 44 65 73 74 72 75 63 74 6f 72 20 2a  FuncDestructor *
3fb43 70 41 72 67 20 3d 20 30 3b 0d 0a 20 20 73 71 6c  pArg = 0;..  sql
3fb44 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3fb45 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fb46 69 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b 0d  if( xDestroy ){.
3fb47 0a 20 20 20 20 70 41 72 67 20 3d 20 28 46 75 6e  .    pArg = (Fun
3fb48 63 44 65 73 74 72 75 63 74 6f 72 20 2a 29 73 71  cDestructor *)sq
3fb49 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
3fb4a 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 46 75 6e  o(db, sizeof(Fun
3fb4b 63 44 65 73 74 72 75 63 74 6f 72 29 29 3b 0d 0a  cDestructor));..
3fb4c 20 20 20 20 69 66 28 20 21 70 41 72 67 20 29 7b      if( !pArg ){
3fb4d 0d 0a 20 20 20 20 20 20 78 44 65 73 74 72 6f 79  ..      xDestroy
3fb4e 28 70 29 3b 0d 0a 20 20 20 20 20 20 67 6f 74 6f  (p);..      goto
3fb4f 20 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   out;..    }..  
3fb50 20 20 70 41 72 67 2d 3e 78 44 65 73 74 72 6f 79    pArg->xDestroy
3fb51 20 3d 20 78 44 65 73 74 72 6f 79 3b 0d 0a 20 20   = xDestroy;..  
3fb52 20 20 70 41 72 67 2d 3e 70 55 73 65 72 44 61 74    pArg->pUserDat
3fb53 61 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a 20 20 72  a = p;..  }..  r
3fb54 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  c = sqlite3Creat
3fb55 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 2c  eFunc(db, zFunc,
3fb56 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78   nArg, enc, p, x
3fb57 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  Func, xStep, xFi
3fb58 6e 61 6c 2c 20 70 41 72 67 29 3b 0d 0a 20 20 69  nal, pArg);..  i
3fb59 66 28 20 70 41 72 67 20 26 26 20 70 41 72 67 2d  f( pArg && pArg-
3fb5a 3e 6e 52 65 66 3d 3d 30 20 29 7b 0d 0a 20 20 20  >nRef==0 ){..   
3fb5b 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
3fb5c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 78  ITE_OK );..    x
3fb5d 44 65 73 74 72 6f 79 28 70 29 3b 0d 0a 20 20 20  Destroy(p);..   
3fb5e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
3fb5f 62 2c 20 70 41 72 67 29 3b 0d 0a 20 20 7d 0d 0a  b, pArg);..  }..
3fb60 0d 0a 20 6f 75 74 3a 0d 0a 20 20 72 63 20 3d 20  .. out:..  rc = 
3fb61 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
3fb62 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74  b, rc);..  sqlit
3fb63 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3fb64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  b->mutex);..  re
3fb65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23  turn rc;..}....#
3fb66 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3fb67 49 54 5f 55 54 46 31 36 0d 0a 53 51 4c 49 54 45  IT_UTF16..SQLITE
3fb68 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3fb69 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3fb6a 31 36 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  16(..  sqlite3 *
3fb6b 64 62 2c 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69  db,..  const voi
3fb6c 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  d *zFunctionName
3fb6d 2c 0d 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0d 0a  ,..  int nArg,..
3fb6e 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0d    int eTextRep,.
3fb6f 0a 20 20 76 6f 69 64 20 2a 70 2c 0d 0a 20 20 76  .  void *p,..  v
3fb70 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
3fb71 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
3fb72 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
3fb73 2a 29 2c 0d 0a 20 20 76 6f 69 64 20 28 2a 78 53  *),..  void (*xS
3fb74 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
3fb75 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
3fb76 33 5f 76 61 6c 75 65 2a 2a 29 2c 0d 0a 20 20 76  3_value**),..  v
3fb77 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
3fb78 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0d  lite3_context*).
3fb79 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  .){..  int rc;..
3fb7a 20 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0d    char *zFunc8;.
3fb7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3fb7c 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
3fb7d 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 21 64  );..  assert( !d
3fb7e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3fb7f 29 3b 0d 0a 20 20 7a 46 75 6e 63 38 20 3d 20 73  );..  zFunc8 = s
3fb80 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
3fb81 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
3fb82 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
3fb83 31 36 4e 41 54 49 56 45 29 3b 0d 0a 20 20 72 63  16NATIVE);..  rc
3fb84 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
3fb85 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 38 2c  Func(db, zFunc8,
3fb86 20 6e 41 72 67 2c 20 65 54 65 78 74 52 65 70 2c   nArg, eTextRep,
3fb87 20 70 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70   p, xFunc, xStep
3fb88 2c 20 78 46 69 6e 61 6c 2c 30 29 3b 0d 0a 20 20  , xFinal,0);..  
3fb89 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
3fb8a 2c 20 7a 46 75 6e 63 38 29 3b 0d 0a 20 20 72 63  , zFunc8);..  rc
3fb8b 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
3fb8c 74 28 64 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71  t(db, rc);..  sq
3fb8d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3fb8e 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  e(db->mutex);.. 
3fb8f 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
3fb90 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  #endif....../*..
3fb91 2a 2a 20 44 65 63 6c 61 72 65 20 74 68 61 74 20  ** Declare that 
3fb92 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 62  a function has b
3fb93 65 65 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 62  een overloaded b
3fb94 79 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  y a virtual tabl
3fb95 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  e...**..** If th
3fb96 65 20 66 75 6e 63 74 69 6f 6e 20 61 6c 72 65 61  e function alrea
3fb97 64 79 20 65 78 69 73 74 73 20 61 73 20 61 20 72  dy exists as a r
3fb98 65 67 75 6c 61 72 20 67 6c 6f 62 61 6c 20 66 75  egular global fu
3fb99 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 0d 0a 2a 2a  nction, then..**
3fb9a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
3fb9b 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 74 68   a no-op.  If th
3fb9c 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  e function does 
3fb9d 6e 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20  not exist, then 
3fb9e 63 72 65 61 74 65 0d 0a 2a 2a 20 61 20 6e 65 77  create..** a new
3fb9f 20 6f 6e 65 20 74 68 61 74 20 61 6c 77 61 79 73   one that always
3fba0 20 74 68 72 6f 77 73 20 61 20 72 75 6e 2d 74 69   throws a run-ti
3fba1 6d 65 20 65 72 72 6f 72 2e 20 20 0d 0a 2a 2a 0d  me error.  ..**.
3fba2 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75 61 6c  .** When virtual
3fba3 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64 20 74   tables intend t
3fba4 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f 76 65  o provide an ove
3fba5 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69 6f 6e  rloaded function
3fba6 2c 20 74 68 65 79 0d 0a 2a 2a 20 73 68 6f 75 6c  , they..** shoul
3fba7 64 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  d call this rout
3fba8 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ine to make sure
3fba9 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63   the global func
3fbaa 74 69 6f 6e 20 65 78 69 73 74 73 2e 0d 0a 2a 2a  tion exists...**
3fbab 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69   A global functi
3fbac 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e  on must exist in
3fbad 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20   order for name 
3fbae 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f  resolution to wo
3fbaf 72 6b 0d 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e  rk..** properly.
3fbb0 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
3fbb1 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
3fbb2 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0d  rload_function(.
3fbb3 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0d  .  sqlite3 *db,.
3fbb4 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3fbb5 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20 6e 41 72  Name,..  int nAr
3fbb6 67 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 6e 4e 61  g..){..  int nNa
3fbb7 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
3fbb8 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0d 0a 20 20  en30(zName);..  
3fbb9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3fbba 4f 4b 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  OK;..  sqlite3_m
3fbbb 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3fbbc 75 74 65 78 29 3b 0d 0a 20 20 69 66 28 20 73 71  utex);..  if( sq
3fbbd 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
3fbbe 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  n(db, zName, nNa
3fbbf 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
3fbc0 5f 55 54 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0d  _UTF8, 0)==0 ){.
3fbc1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3fbc2 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
3fbc3 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c  zName, nArg, SQL
3fbc4 49 54 45 5f 55 54 46 38 2c 0d 0a 20 20 20 20 20  ITE_UTF8,..     
3fbc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fbc6 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
3fbc7 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c  InvalidFunction,
3fbc8 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 7d 0d   0, 0, 0);..  }.
3fbc9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
3fbca 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0d  piExit(db, rc);.
3fbcb 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3fbcc 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3fbcd 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
3fbce 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53  ..}....#ifndef S
3fbcf 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
3fbd0 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67 69 73 74 65  ../*..** Registe
3fbd1 72 20 61 20 74 72 61 63 65 20 66 75 6e 63 74 69  r a trace functi
3fbd2 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72  on.  The pArg fr
3fbd3 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  om the previousl
3fbd4 79 20 72 65 67 69 73 74 65 72 65 64 20 74 72 61  y registered tra
3fbd5 63 65 0d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ce..** is return
3fbd6 65 64 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20  ed.  ..**..** A 
3fbd7 4e 55 4c 4c 20 74 72 61 63 65 20 66 75 6e 63 74  NULL trace funct
3fbd8 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  ion means that n
3fbd9 6f 20 74 72 61 63 69 6e 67 20 69 73 20 65 78 65  o tracing is exe
3fbda 63 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55  cutes.  A non-NU
3fbdb 4c 4c 0d 0a 2a 2a 20 74 72 61 63 65 20 69 73 20  LL..** trace is 
3fbdc 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
3fbdd 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20  unction that is 
3fbde 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 73  invoked at the s
3fbdf 74 61 72 74 20 6f 66 20 65 61 63 68 0d 0a 2a 2a  tart of each..**
3fbe0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0d   SQL statement..
3fbe1 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
3fbe2 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74 72  void *sqlite3_tr
3fbe3 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ace(sqlite3 *db,
3fbe4 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29 28   void (*xTrace)(
3fbe5 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
3fbe6 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  *), void *pArg){
3fbe7 0d 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0d  ..  void *pOld;.
3fbe8 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3fbe9 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
3fbea 29 3b 0d 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  );..  pOld = db-
3fbeb 3e 70 54 72 61 63 65 41 72 67 3b 0d 0a 20 20 64  >pTraceArg;..  d
3fbec 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72 61  b->xTrace = xTra
3fbed 63 65 3b 0d 0a 20 20 64 62 2d 3e 70 54 72 61 63  ce;..  db->pTrac
3fbee 65 41 72 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20  eArg = pArg;..  
3fbef 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3fbf0 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ave(db->mutex);.
3fbf1 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0d  .  return pOld;.
3fbf2 0a 7d 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67 69 73  .}../*..** Regis
3fbf3 74 65 72 20 61 20 70 72 6f 66 69 6c 65 20 66 75  ter a profile fu
3fbf4 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 70 41 72  nction.  The pAr
3fbf5 67 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69  g from the previ
3fbf6 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
3fbf7 20 0d 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75   ..** profile fu
3fbf8 6e 63 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e  nction is return
3fbf9 65 64 2e 20 20 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20  ed.  ..**..** A 
3fbfa 4e 55 4c 4c 20 70 72 6f 66 69 6c 65 20 66 75 6e  NULL profile fun
3fbfb 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74  ction means that
3fbfc 20 6e 6f 20 70 72 6f 66 69 6c 69 6e 67 20 69 73   no profiling is
3fbfd 20 65 78 65 63 75 74 65 73 2e 20 20 41 20 6e 6f   executes.  A no
3fbfe 6e 2d 4e 55 4c 4c 0d 0a 2a 2a 20 70 72 6f 66 69  n-NULL..** profi
3fbff 6c 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  le is a pointer 
3fc00 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  to a function th
3fc01 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74  at is invoked at
3fc02 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
3fc03 6f 66 0d 0a 2a 2a 20 65 61 63 68 20 53 51 4c 20  of..** each SQL 
3fc04 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69  statement that i
3fc05 73 20 72 75 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  s run...*/..SQLI
3fc06 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
3fc07 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 0d 0a 20  ite3_profile(.. 
3fc08 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20   sqlite3 *db,.. 
3fc09 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65   void (*xProfile
3fc0a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68  )(void*,const ch
3fc0b 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36  ar*,sqlite_uint6
3fc0c 34 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72  4),..  void *pAr
3fc0d 67 0d 0a 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 70  g..){..  void *p
3fc0e 4f 6c 64 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  Old;..  sqlite3_
3fc0f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
3fc10 6d 75 74 65 78 29 3b 0d 0a 20 20 70 4f 6c 64 20  mutex);..  pOld 
3fc11 3d 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72  = db->pProfileAr
3fc12 67 3b 0d 0a 20 20 64 62 2d 3e 78 50 72 6f 66 69  g;..  db->xProfi
3fc13 6c 65 20 3d 20 78 50 72 6f 66 69 6c 65 3b 0d 0a  le = xProfile;..
3fc14 20 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72    db->pProfileAr
3fc15 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 73 71 6c  g = pArg;..  sql
3fc16 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3fc17 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fc18 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0d 0a 7d 0d  return pOld;..}.
3fc19 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
3fc1a 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a 2f 0d  E_OMIT_TRACE */.
3fc1b 0a 0d 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .../*** EXPERIME
3fc1c 4e 54 41 4c 20 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  NTAL ***..**..**
3fc1d 20 52 65 67 69 73 74 65 72 20 61 20 66 75 6e 63   Register a func
3fc1e 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b  tion to be invok
3fc1f 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  ed when a transa
3fc20 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0d  ction comments..
3fc21 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76 6f 6b  .** If the invok
3fc22 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  ed function retu
3fc23 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
3fc24 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 62 65  en the commit be
3fc25 63 6f 6d 65 73 20 61 0d 0a 2a 2a 20 72 6f 6c 6c  comes a..** roll
3fc26 62 61 63 6b 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  back...*/..SQLIT
3fc27 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
3fc28 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
3fc29 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
3fc2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3fc2b 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
3fc2c 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
3fc2d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 43  e */..  int (*xC
3fc2e 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c  allback)(void*),
3fc2f 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f    /* Function to
3fc30 20 69 6e 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20   invoke on each 
3fc31 63 6f 6d 6d 69 74 20 2a 2f 0d 0a 20 20 76 6f 69  commit */..  voi
3fc32 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20  d *pArg         
3fc33 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
3fc34 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
3fc35 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 76 6f 69  on */..){..  voi
3fc36 64 20 2a 70 4f 6c 64 3b 0d 0a 20 20 73 71 6c 69  d *pOld;..  sqli
3fc37 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3fc38 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 70  db->mutex);..  p
3fc39 4f 6c 64 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d 69  Old = db->pCommi
3fc3a 74 41 72 67 3b 0d 0a 20 20 64 62 2d 3e 78 43 6f  tArg;..  db->xCo
3fc3b 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 3d 20 78  mmitCallback = x
3fc3c 43 61 6c 6c 62 61 63 6b 3b 0d 0a 20 20 64 62 2d  Callback;..  db-
3fc3d 3e 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20 70 41  >pCommitArg = pA
3fc3e 72 67 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rg;..  sqlite3_m
3fc3f 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3fc40 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
3fc41 20 70 4f 6c 64 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d   pOld;..}..../*.
3fc42 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
3fc43 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e  allback to be in
3fc44 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20  voked each time 
3fc45 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  a row is updated
3fc46 2c 0d 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f  ,..** inserted o
3fc47 72 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20  r deleted using 
3fc48 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  this database co
3fc49 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53  nnection...*/..S
3fc4a 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
3fc4b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
3fc4c 6f 6f 6b 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  ook(..  sqlite3 
3fc4d 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
3fc4e 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20    /* Attach the 
3fc4f 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74  hook to this dat
3fc50 61 62 61 73 65 20 2a 2f 0d 0a 20 20 76 6f 69 64  abase */..  void
3fc51 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
3fc52 69 64 2a 2c 69 6e 74 2c 63 68 61 72 20 63 6f 6e  id*,int,char con
3fc53 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20  st *,char const 
3fc54 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c  *,sqlite_int64),
3fc55 0d 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20  ..  void *pArg  
3fc56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3fc57 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   Argument to the
3fc58 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 29 7b   function */..){
3fc59 0d 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0d  ..  void *pRet;.
3fc5a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3fc5b 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
3fc5c 29 3b 0d 0a 20 20 70 52 65 74 20 3d 20 64 62 2d  );..  pRet = db-
3fc5d 3e 70 55 70 64 61 74 65 41 72 67 3b 0d 0a 20 20  >pUpdateArg;..  
3fc5e 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
3fc5f 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
3fc60 0d 0a 20 20 64 62 2d 3e 70 55 70 64 61 74 65 41  ..  db->pUpdateA
3fc61 72 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 73 71  rg = pArg;..  sq
3fc62 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3fc63 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  e(db->mutex);.. 
3fc64 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0d 0a 7d   return pRet;..}
3fc65 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67 69 73  ..../*..** Regis
3fc66 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  ter a callback t
3fc67 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63  o be invoked eac
3fc68 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63  h time a transac
3fc69 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0d 0a  tion is rolled..
3fc6a 2a 2a 20 62 61 63 6b 20 62 79 20 74 68 69 73 20  ** back by this 
3fc6b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3fc6c 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ion...*/..SQLITE
3fc6d 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
3fc6e 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
3fc6f 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  (..  sqlite3 *db
3fc70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
3fc71 2a 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f  * Attach the hoo
3fc72 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  k to this databa
3fc73 73 65 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 28 2a  se */..  void (*
3fc74 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  xCallback)(void*
3fc75 29 2c 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66  ), /* Callback f
3fc76 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f  unction */..  vo
3fc77 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20  id *pArg        
3fc78 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
3fc79 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
3fc7a 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 76 6f  ion */..){..  vo
3fc7b 69 64 20 2a 70 52 65 74 3b 0d 0a 20 20 73 71 6c  id *pRet;..  sql
3fc7c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3fc7d 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fc7e 70 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c  pRet = db->pRoll
3fc7f 62 61 63 6b 41 72 67 3b 0d 0a 20 20 64 62 2d 3e  backArg;..  db->
3fc80 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
3fc81 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0d 0a  k = xCallback;..
3fc82 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41    db->pRollbackA
3fc83 72 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 73 71  rg = pArg;..  sq
3fc84 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3fc85 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  e(db->mutex);.. 
3fc86 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0d 0a 7d   return pRet;..}
3fc87 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
3fc88 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d 0a 2f 2a 0d  TE_OMIT_WAL../*.
3fc89 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
3fc8a 77 61 6c 5f 68 6f 6f 6b 28 29 20 63 61 6c 6c 62  wal_hook() callb
3fc8b 61 63 6b 20 72 65 67 69 73 74 65 72 65 64 20 62  ack registered b
3fc8c 79 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 61 75  y sqlite3_wal_au
3fc8d 74 6f 63 68 65 63 6b 70 6f 69 6e 74 28 29 2e 0d  tocheckpoint()..
3fc8e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74  .** Invoke sqlit
3fc8f 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
3fc90 74 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t if the number 
3fc91 6f 66 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65  of frames in the
3fc92 20 6c 6f 67 20 66 69 6c 65 0d 0a 2a 2a 20 69 73   log file..** is
3fc93 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 73 71   greater than sq
3fc94 6c 69 74 65 33 2e 70 57 61 6c 41 72 67 20 63 61  lite3.pWalArg ca
3fc95 73 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  st to an integer
3fc96 20 28 74 68 65 20 76 61 6c 75 65 20 63 6f 6e 66   (the value conf
3fc97 69 67 75 72 65 64 20 62 79 0d 0a 2a 2a 20 77 61  igured by..** wa
3fc98 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74  l_autocheckpoint
3fc99 28 29 29 2e 0d 0a 2a 2f 20 0d 0a 53 51 4c 49 54  ())...*/ ..SQLIT
3fc9a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
3fc9b 6c 69 74 65 33 57 61 6c 44 65 66 61 75 6c 74 48  lite3WalDefaultH
3fc9c 6f 6f 6b 28 0d 0a 20 20 76 6f 69 64 20 2a 70 43  ook(..  void *pC
3fc9d 6c 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 2f  lientData,     /
3fc9e 2a 20 41 72 67 75 6d 65 6e 74 20 2a 2f 0d 0a 20  * Argument */.. 
3fc9f 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
3fca0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65          /* Conne
3fca1 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73  ction */..  cons
3fca2 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20  t char *zDb,    
3fca3 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 2a     /* Database *
3fca4 2f 0d 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 20  /..  int nFrame 
3fca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
3fca6 69 7a 65 20 6f 66 20 57 41 4c 20 2a 2f 0d 0a 29  ize of WAL */..)
3fca7 7b 0d 0a 20 20 69 66 28 20 6e 46 72 61 6d 65 3e  {..  if( nFrame>
3fca8 3d 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49  =SQLITE_PTR_TO_I
3fca9 4e 54 28 70 43 6c 69 65 6e 74 44 61 74 61 29 20  NT(pClientData) 
3fcaa 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ){..    sqlite3B
3fcab 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
3fcac 28 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  ();..    sqlite3
3fcad 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28  _wal_checkpoint(
3fcae 64 62 2c 20 7a 44 62 29 3b 0d 0a 20 20 20 20 73  db, zDb);..    s
3fcaf 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
3fcb0 61 6c 6c 6f 63 28 29 3b 0d 0a 20 20 7d 0d 0a 20  alloc();..  }.. 
3fcb1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3fcb2 4b 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a  K;..}..#endif /*
3fcb3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c   SQLITE_OMIT_WAL
3fcb4 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6f   */..../*..** Co
3fcb5 6e 66 69 67 75 72 65 20 61 6e 20 73 71 6c 69 74  nfigure an sqlit
3fcb6 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 20 63 61  e3_wal_hook() ca
3fcb7 6c 6c 62 61 63 6b 20 74 6f 20 61 75 74 6f 6d 61  llback to automa
3fcb8 74 69 63 61 6c 6c 79 20 63 68 65 63 6b 70 6f 69  tically checkpoi
3fcb9 6e 74 0d 0a 2a 2a 20 61 20 64 61 74 61 62 61 73  nt..** a databas
3fcba 65 20 61 66 74 65 72 20 63 6f 6d 6d 69 74 74 69  e after committi
3fcbb 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ng a transaction
3fcbc 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 46   if there are nF
3fcbd 72 61 6d 65 20 6f 72 0d 0a 2a 2a 20 6d 6f 72 65  rame or..** more
3fcbe 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 6c   frames in the l
3fcbf 6f 67 20 66 69 6c 65 2e 20 50 61 73 73 69 6e 67  og file. Passing
3fcc0 20 7a 65 72 6f 20 6f 72 20 61 20 6e 65 67 61 74   zero or a negat
3fcc1 69 76 65 20 76 61 6c 75 65 20 61 73 20 74 68 65  ive value as the
3fcc2 0d 0a 2a 2a 20 6e 46 72 61 6d 65 20 70 61 72 61  ..** nFrame para
3fcc3 6d 65 74 65 72 20 64 69 73 61 62 6c 65 73 20 61  meter disables a
3fcc4 75 74 6f 6d 61 74 69 63 20 63 68 65 63 6b 70 6f  utomatic checkpo
3fcc5 69 6e 74 73 20 65 6e 74 69 72 65 6c 79 2e 0d 0a  ints entirely...
3fcc6 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  **..** The callb
3fcc7 61 63 6b 20 72 65 67 69 73 74 65 72 65 64 20 62  ack registered b
3fcc8 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  y this function 
3fcc9 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69  replaces any exi
3fcca 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 0d 0a  sting callback..
3fccb 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 75 73  ** registered us
3fccc 69 6e 67 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f  ing sqlite3_wal_
3fccd 68 6f 6f 6b 28 29 2e 20 4c 69 6b 65 77 69 73 65  hook(). Likewise
3fcce 2c 20 72 65 67 69 73 74 65 72 69 6e 67 20 61 20  , registering a 
3fccf 63 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 75 73 69  callback..** usi
3fcd0 6e 67 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68  ng sqlite3_wal_h
3fcd1 6f 6f 6b 28 29 20 64 69 73 61 62 6c 65 73 20 74  ook() disables t
3fcd2 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 68 65  he automatic che
3fcd3 63 6b 70 6f 69 6e 74 20 6d 65 63 68 61 6e 69 73  ckpoint mechanis
3fcd4 6d 0d 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64  m..** configured
3fcd5 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
3fcd6 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  n...*/..SQLITE_A
3fcd7 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77  PI int sqlite3_w
3fcd8 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
3fcd9 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  t(sqlite3 *db, i
3fcda 6e 74 20 6e 46 72 61 6d 65 29 7b 0d 0a 23 69 66  nt nFrame){..#if
3fcdb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3fcdc 57 41 4c 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  WAL..  UNUSED_PA
3fcdd 52 41 4d 45 54 45 52 28 64 62 29 3b 0d 0a 20 20  RAMETER(db);..  
3fcde 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
3fcdf 28 6e 46 72 61 6d 65 29 3b 0d 0a 23 65 6c 73 65  (nFrame);..#else
3fce0 0d 0a 20 20 69 66 28 20 6e 46 72 61 6d 65 3e 30  ..  if( nFrame>0
3fce1 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
3fce2 5f 77 61 6c 5f 68 6f 6f 6b 28 64 62 2c 20 73 71  _wal_hook(db, sq
3fce3 6c 69 74 65 33 57 61 6c 44 65 66 61 75 6c 74 48  lite3WalDefaultH
3fce4 6f 6f 6b 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ook, SQLITE_INT_
3fce5 54 4f 5f 50 54 52 28 6e 46 72 61 6d 65 29 29 3b  TO_PTR(nFrame));
3fce6 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
3fce7 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b  sqlite3_wal_hook
3fce8 28 64 62 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 7d  (db, 0, 0);..  }
3fce9 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 72 65 74 75  ..#endif..  retu
3fcea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
3fceb 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67 69 73  ..../*..** Regis
3fcec 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  ter a callback t
3fced 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63  o be invoked eac
3fcee 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63  h time a transac
3fcef 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 0d  tion is written.
3fcf0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 77 72 69  .** into the wri
3fcf1 74 65 2d 61 68 65 61 64 2d 6c 6f 67 20 62 79 20  te-ahead-log by 
3fcf2 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  this database co
3fcf3 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53  nnection...*/..S
3fcf4 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
3fcf5 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b  sqlite3_wal_hook
3fcf6 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  (..  sqlite3 *db
3fcf7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3fcf8 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
3fcf9 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20  he hook to this 
3fcfa 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  db handle */..  
3fcfb 69 6e 74 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  int(*xCallback)(
3fcfc 76 6f 69 64 20 2a 2c 20 73 71 6c 69 74 65 33 2a  void *, sqlite3*
3fcfd 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
3fcfe 6e 74 29 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41  nt),..  void *pA
3fcff 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
3fd00 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
3fd01 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
3fd02 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 20   to xCallback() 
3fd03 2a 2f 0d 0a 29 7b 0d 0a 23 69 66 6e 64 65 66 20  */..){..#ifndef 
3fd04 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d  SQLITE_OMIT_WAL.
3fd05 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0d 0a  .  void *pRet;..
3fd06 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3fd07 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
3fd08 3b 0d 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e  ;..  pRet = db->
3fd09 70 57 61 6c 41 72 67 3b 0d 0a 20 20 64 62 2d 3e  pWalArg;..  db->
3fd0a 78 57 61 6c 43 61 6c 6c 62 61 63 6b 20 3d 20 78  xWalCallback = x
3fd0b 43 61 6c 6c 62 61 63 6b 3b 0d 0a 20 20 64 62 2d  Callback;..  db-
3fd0c 3e 70 57 61 6c 41 72 67 20 3d 20 70 41 72 67 3b  >pWalArg = pArg;
3fd0d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
3fd0e 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
3fd0f 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 70 52  x);..  return pR
3fd10 65 74 3b 0d 0a 23 65 6c 73 65 0d 0a 20 20 72 65  et;..#else..  re
3fd11 74 75 72 6e 20 30 3b 0d 0a 23 65 6e 64 69 66 0d  turn 0;..#endif.
3fd12 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 65  .}..../*..** Che
3fd13 63 6b 70 6f 69 6e 74 20 64 61 74 61 62 61 73 65  ckpoint database
3fd14 20 7a 44 62 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54   zDb...*/..SQLIT
3fd15 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3fd16 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74  3_wal_checkpoint
3fd17 5f 76 32 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  _v2(..  sqlite3 
3fd18 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
3fd19 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
3fd1a 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ase handle */.. 
3fd1b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3fd1c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3fd1d 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 74 74 61   /* Name of atta
3fd1e 63 68 65 64 20 64 61 74 61 62 61 73 65 20 28 6f  ched database (o
3fd1f 72 20 4e 55 4c 4c 29 20 2a 2f 0d 0a 20 20 69 6e  r NULL) */..  in
3fd20 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20  t eMode,        
3fd21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3fd22 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49   SQLITE_CHECKPOI
3fd23 4e 54 5f 2a 20 76 61 6c 75 65 20 2a 2f 0d 0a 20  NT_* value */.. 
3fd24 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 20 20 20   int *pnLog,    
3fd25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fd26 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65 20 6f 66   /* OUT: Size of
3fd27 20 57 41 4c 20 6c 6f 67 20 69 6e 20 66 72 61 6d   WAL log in fram
3fd28 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e  es */..  int *pn
3fd29 43 6b 70 74 20 20 20 20 20 20 20 20 20 20 20 20  Ckpt            
3fd2a 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3fd2b 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
3fd2c 20 66 72 61 6d 65 73 20 63 68 65 63 6b 70 6f 69   frames checkpoi
3fd2d 6e 74 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 23 69 66  nted */..){..#if
3fd2e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3fd2f 57 41 4c 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  WAL..  return SQ
3fd30 4c 49 54 45 5f 4f 4b 3b 0d 0a 23 65 6c 73 65 0d  LITE_OK;..#else.
3fd31 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
3fd32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fd33 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
3fd34 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 62 20  e */..  int iDb 
3fd35 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  = SQLITE_MAX_ATT
3fd36 41 43 48 45 44 3b 20 20 2f 2a 20 73 71 6c 69 74  ACHED;  /* sqlit
3fd37 65 33 2e 61 44 62 5b 5d 20 69 6e 64 65 78 20 6f  e3.aDb[] index o
3fd38 66 20 64 62 20 74 6f 20 63 68 65 63 6b 70 6f 69  f db to checkpoi
3fd39 6e 74 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 49 6e  nt */....  /* In
3fd3a 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74  itialize the out
3fd3b 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f  put variables to
3fd3c 20 2d 31 20 69 6e 20 63 61 73 65 20 61 6e 20 65   -1 in case an e
3fd3d 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0d  rror occurs. */.
3fd3e 0a 20 20 69 66 28 20 70 6e 4c 6f 67 20 29 20 2a  .  if( pnLog ) *
3fd3f 70 6e 4c 6f 67 20 3d 20 2d 31 3b 0d 0a 20 20 69  pnLog = -1;..  i
3fd40 66 28 20 70 6e 43 6b 70 74 20 29 20 2a 70 6e 43  f( pnCkpt ) *pnC
3fd41 6b 70 74 20 3d 20 2d 31 3b 0d 0a 0d 0a 20 20 61  kpt = -1;....  a
3fd42 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 43 48  ssert( SQLITE_CH
3fd43 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 3e 53 51  ECKPOINT_FULL>SQ
3fd44 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f  LITE_CHECKPOINT_
3fd45 50 41 53 53 49 56 45 20 29 3b 0d 0a 20 20 61 73  PASSIVE );..  as
3fd46 73 65 72 74 28 20 53 51 4c 49 54 45 5f 43 48 45  sert( SQLITE_CHE
3fd47 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 3c 53 51 4c  CKPOINT_FULL<SQL
3fd48 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52  ITE_CHECKPOINT_R
3fd49 45 53 54 41 52 54 20 29 3b 0d 0a 20 20 61 73 73  ESTART );..  ass
3fd4a 65 72 74 28 20 53 51 4c 49 54 45 5f 43 48 45 43  ert( SQLITE_CHEC
3fd4b 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 2b 32  KPOINT_PASSIVE+2
3fd4c 3d 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f  ==SQLITE_CHECKPO
3fd4d 49 4e 54 5f 52 45 53 54 41 52 54 20 29 3b 0d 0a  INT_RESTART );..
3fd4e 20 20 69 66 28 20 65 4d 6f 64 65 3c 53 51 4c 49    if( eMode<SQLI
3fd4f 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41  TE_CHECKPOINT_PA
3fd50 53 53 49 56 45 20 7c 7c 20 65 4d 6f 64 65 3e 53  SSIVE || eMode>S
3fd51 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54  QLITE_CHECKPOINT
3fd52 5f 52 45 53 54 41 52 54 20 29 7b 0d 0a 20 20 20  _RESTART ){..   
3fd53 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
3fd54 49 53 55 53 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  ISUSE;..  }.... 
3fd55 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3fd56 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
3fd57 0d 0a 20 20 69 66 28 20 7a 44 62 20 26 26 20 7a  ..  if( zDb && z
3fd58 44 62 5b 30 5d 20 29 7b 0d 0a 20 20 20 20 69 44  Db[0] ){..    iD
3fd59 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  b = sqlite3FindD
3fd5a 62 4e 61 6d 65 28 64 62 2c 20 7a 44 62 29 3b 0d  bName(db, zDb);.
3fd5b 0a 20 20 7d 0d 0a 20 20 69 66 28 20 69 44 62 3c  .  }..  if( iDb<
3fd5c 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53  0 ){..    rc = S
3fd5d 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
3fd5e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
3fd5f 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
3fd60 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61   "unknown databa
3fd61 73 65 3a 20 25 73 22 2c 20 7a 44 62 29 3b 0d 0a  se: %s", zDb);..
3fd62 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63    }else{..    rc
3fd63 20 3d 20 73 71 6c 69 74 65 33 43 68 65 63 6b 70   = sqlite3Checkp
3fd64 6f 69 6e 74 28 64 62 2c 20 69 44 62 2c 20 65 4d  oint(db, iDb, eM
3fd65 6f 64 65 2c 20 70 6e 4c 6f 67 2c 20 70 6e 43 6b  ode, pnLog, pnCk
3fd66 70 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65  pt);..    sqlite
3fd67 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30  3Error(db, rc, 0
3fd68 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20  );..  }..  rc = 
3fd69 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
3fd6a 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74  b, rc);..  sqlit
3fd6b 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3fd6c 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  b->mutex);..  re
3fd6d 74 75 72 6e 20 72 63 3b 0d 0a 23 65 6e 64 69 66  turn rc;..#endif
3fd6e 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
3fd6f 43 68 65 63 6b 70 6f 69 6e 74 20 64 61 74 61 62  Checkpoint datab
3fd70 61 73 65 20 7a 44 62 2e 20 49 66 20 7a 44 62 20  ase zDb. If zDb 
3fd71 69 73 20 4e 55 4c 4c 2c 20 6f 72 20 69 66 20 74  is NULL, or if t
3fd72 68 65 20 62 75 66 66 65 72 20 7a 44 62 20 70 6f  he buffer zDb po
3fd73 69 6e 74 73 0d 0a 2a 2a 20 74 6f 20 63 6f 6e 74  ints..** to cont
3fd74 61 69 6e 73 20 61 20 7a 65 72 6f 2d 6c 65 6e 67  ains a zero-leng
3fd75 74 68 20 73 74 72 69 6e 67 2c 20 61 6c 6c 20 61  th string, all a
3fd76 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
3fd77 73 20 61 72 65 20 0d 0a 2a 2a 20 63 68 65 63 6b  s are ..** check
3fd78 70 6f 69 6e 74 65 64 2e 0d 0a 2a 2f 0d 0a 53 51  pointed...*/..SQ
3fd79 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3fd7a 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f  ite3_wal_checkpo
3fd7b 69 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  int(sqlite3 *db,
3fd7c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3fd7d 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  ){..  return sql
3fd7e 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f  ite3_wal_checkpo
3fd7f 69 6e 74 5f 76 32 28 64 62 2c 20 7a 44 62 2c 20  int_v2(db, zDb, 
3fd80 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
3fd81 54 5f 50 41 53 53 49 56 45 2c 20 30 2c 20 30 29  T_PASSIVE, 0, 0)
3fd82 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 6e 64 65 66 20  ;..}....#ifndef 
3fd83 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0d  SQLITE_OMIT_WAL.
3fd84 0a 2f 2a 0d 0a 2a 2a 20 52 75 6e 20 61 20 63 68  ./*..** Run a ch
3fd85 65 63 6b 70 6f 69 6e 74 20 6f 6e 20 64 61 74 61  eckpoint on data
3fd86 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 69  base iDb. This i
3fd87 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 64 61 74  s a no-op if dat
3fd88 61 62 61 73 65 20 69 44 62 20 69 73 0d 0a 2a 2a  abase iDb is..**
3fd89 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f   not currently o
3fd8a 70 65 6e 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2e  pen in WAL mode.
3fd8b 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 20 74 72  ..**..** If a tr
3fd8c 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
3fd8d 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
3fd8e 65 20 62 65 69 6e 67 20 63 68 65 63 6b 70 6f 69  e being checkpoi
3fd8f 6e 74 65 64 2c 20 74 68 69 73 20 0d 0a 2a 2a 20  nted, this ..** 
3fd90 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
3fd91 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 61   SQLITE_LOCKED a
3fd92 6e 64 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20  nd a checkpoint 
3fd93 69 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64  is not attempted
3fd94 2e 20 49 66 20 0d 0a 2a 2a 20 61 6e 20 65 72 72  . If ..** an err
3fd95 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
3fd96 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 68 65 63  running the chec
3fd97 6b 70 6f 69 6e 74 2c 20 61 6e 20 53 51 4c 69 74  kpoint, an SQLit
3fd98 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
3fd99 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 28 69  ..** returned (i
3fd9a 2e 65 2e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  .e. SQLITE_IOERR
3fd9b 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51  ). Otherwise, SQ
3fd9c 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a  LITE_OK...**..**
3fd9d 20 54 68 65 20 6d 75 74 65 78 20 6f 6e 20 64 61   The mutex on da
3fd9e 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
3fd9f 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20   should be held 
3fda0 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54  by the caller. T
3fda1 68 65 20 6d 75 74 65 78 0d 0a 2a 2a 20 61 73 73  he mutex..** ass
3fda2 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3fda3 20 73 70 65 63 69 66 69 63 20 62 2d 74 72 65 65   specific b-tree
3fda4 20 62 65 69 6e 67 20 63 68 65 63 6b 70 6f 69 6e   being checkpoin
3fda5 74 65 64 20 69 73 20 74 61 6b 65 6e 20 62 79 0d  ted is taken by.
3fda6 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
3fda7 6e 20 77 68 69 6c 65 20 74 68 65 20 63 68 65 63  n while the chec
3fda8 6b 70 6f 69 6e 74 20 69 73 20 72 75 6e 6e 69 6e  kpoint is runnin
3fda9 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69 44  g...**..** If iD
3fdaa 62 20 69 73 20 70 61 73 73 65 64 20 53 51 4c 49  b is passed SQLI
3fdab 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2c  TE_MAX_ATTACHED,
3fdac 20 74 68 65 6e 20 61 6c 6c 20 61 74 74 61 63 68   then all attach
3fdad 65 64 20 64 61 74 61 62 61 73 65 73 20 61 72 65  ed databases are
3fdae 0d 0a 2a 2a 20 63 68 65 63 6b 70 6f 69 6e 74 65  ..** checkpointe
3fdaf 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69  d. If an error i
3fdb0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74  s encountered it
3fdb1 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d   is returned imm
3fdb2 65 64 69 61 74 65 6c 79 20 2d 0d 0a 2a 2a 20 6e  ediately -..** n
3fdb3 6f 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  o attempt is mad
3fdb4 65 20 74 6f 20 63 68 65 63 6b 70 6f 69 6e 74 20  e to checkpoint 
3fdb5 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20 64 61  any remaining da
3fdb6 74 61 62 61 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  tabases...**..**
3fdb7 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65   Parameter eMode
3fdb8 20 69 73 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54   is one of SQLIT
3fdb9 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53  E_CHECKPOINT_PAS
3fdba 53 49 56 45 2c 20 46 55 4c 4c 20 6f 72 20 52 45  SIVE, FULL or RE
3fdbb 53 54 41 52 54 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  START...*/..SQLI
3fdbc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3fdbd 71 6c 69 74 65 33 43 68 65 63 6b 70 6f 69 6e 74  qlite3Checkpoint
3fdbe 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
3fdbf 74 20 69 44 62 2c 20 69 6e 74 20 65 4d 6f 64 65  t iDb, int eMode
3fdc0 2c 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 69 6e  , int *pnLog, in
3fdc1 74 20 2a 70 6e 43 6b 70 74 29 7b 0d 0a 20 20 69  t *pnCkpt){..  i
3fdc2 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3fdc3 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
3fdc4 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
3fdc5 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  ..  int i;      
3fdc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fdc7 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69      /* Used to i
3fdc8 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
3fdc9 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0d 0a  ttached dbs */..
3fdca 20 20 69 6e 74 20 62 42 75 73 79 20 3d 20 30 3b    int bBusy = 0;
3fdcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fdcc 20 20 2f 2a 20 54 72 75 65 20 69 66 20 53 51 4c    /* True if SQL
3fdcd 49 54 45 5f 42 55 53 59 20 68 61 73 20 62 65 65  ITE_BUSY has bee
3fdce 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f  n encountered */
3fdcf 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 73 71  ....  assert( sq
3fdd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
3fdd1 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0d 0a  (db->mutex) );..
3fdd2 20 20 61 73 73 65 72 74 28 20 21 70 6e 4c 6f 67    assert( !pnLog
3fdd3 20 7c 7c 20 2a 70 6e 4c 6f 67 3d 3d 2d 31 20 29   || *pnLog==-1 )
3fdd4 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 21 70 6e  ;..  assert( !pn
3fdd5 43 6b 70 74 20 7c 7c 20 2a 70 6e 43 6b 70 74 3d  Ckpt || *pnCkpt=
3fdd6 3d 2d 31 20 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28  =-1 );....  for(
3fdd7 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 20 26  i=0; i<db->nDb &
3fdd8 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
3fdd9 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20   i++){..    if( 
3fdda 69 3d 3d 69 44 62 20 7c 7c 20 69 44 62 3d 3d 53  i==iDb || iDb==S
3fddb 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
3fddc 45 44 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  ED ){..      rc 
3fddd 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 68  = sqlite3BtreeCh
3fdde 65 63 6b 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62  eckpoint(db->aDb
3fddf 5b 69 5d 2e 70 42 74 2c 20 65 4d 6f 64 65 2c 20  [i].pBt, eMode, 
3fde0 70 6e 4c 6f 67 2c 20 70 6e 43 6b 70 74 29 3b 0d  pnLog, pnCkpt);.
3fde1 0a 20 20 20 20 20 20 70 6e 4c 6f 67 20 3d 20 30  .      pnLog = 0
3fde2 3b 0d 0a 20 20 20 20 20 20 70 6e 43 6b 70 74 20  ;..      pnCkpt 
3fde3 3d 20 30 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  = 0;..      if( 
3fde4 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
3fde5 29 7b 0d 0a 20 20 20 20 20 20 20 20 62 42 75 73  ){..        bBus
3fde6 79 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  y = 1;..        
3fde7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
3fde8 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
3fde9 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
3fdea 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc==SQLITE_OK 
3fdeb 26 26 20 62 42 75 73 79 29 20 3f 20 53 51 4c 49  && bBusy) ? SQLI
3fdec 54 45 5f 42 55 53 59 20 3a 20 72 63 3b 0d 0a 7d  TE_BUSY : rc;..}
3fded 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
3fdee 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f 0d 0a  TE_OMIT_WAL */..
3fdef 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
3fdf0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
3fdf1 72 75 65 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f  rue if main-memo
3fdf2 72 79 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  ry should be use
3fdf3 64 20 69 6e 73 74 65 61 64 20 6f 66 0d 0a 2a 2a  d instead of..**
3fdf4 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
3fdf5 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74 20  e for transient 
3fdf6 70 61 67 65 72 20 66 69 6c 65 73 20 61 6e 64 20  pager files and 
3fdf7 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
3fdf8 6c 73 2e 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ls...** The valu
3fdf9 65 20 72 65 74 75 72 6e 65 64 20 64 65 70 65 6e  e returned depen
3fdfa 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ds on the value 
3fdfb 6f 66 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  of db->temp_stor
3fdfc 65 20 28 72 75 6e 74 69 6d 65 0d 0a 2a 2a 20 70  e (runtime..** p
3fdfd 61 72 61 6d 65 74 65 72 29 20 61 6e 64 20 74 68  arameter) and th
3fdfe 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 76  e compile time v
3fdff 61 6c 75 65 20 6f 66 20 53 51 4c 49 54 45 5f 54  alue of SQLITE_T
3fe00 45 4d 50 5f 53 54 4f 52 45 2e 20 54 68 65 0d 0a  EMP_STORE. The..
3fe01 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  ** following tab
3fe02 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  le describes the
3fe03 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 62 65   relationship be
3fe04 74 77 65 65 6e 20 74 68 65 73 65 20 74 77 6f 20  tween these two 
3fe05 76 61 6c 75 65 73 0d 0a 2a 2a 20 61 6e 64 20 74  values..** and t
3fe06 68 69 73 20 66 75 6e 63 74 69 6f 6e 73 20 72 65  his functions re
3fe07 74 75 72 6e 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d  turn value...**.
3fe08 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 54 45 4d  .**   SQLITE_TEM
3fe09 50 5f 53 54 4f 52 45 20 20 20 20 20 64 62 2d 3e  P_STORE     db->
3fe0a 74 65 6d 70 5f 73 74 6f 72 65 20 20 20 20 20 4c  temp_store     L
3fe0b 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d 70 6f  ocation of tempo
3fe0c 72 61 72 79 20 64 61 74 61 62 61 73 65 0d 0a 2a  rary database..*
3fe0d 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
3fe0e 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d  -----     ------
3fe0f 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d  --------     ---
3fe10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3fe11 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20  -----------..** 
3fe12 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
3fe13 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
3fe14 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
3fe15 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0d       (return 0).
3fe16 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20  .**   1         
3fe17 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20              1   
3fe18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
3fe19 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
3fe1a 20 30 29 0d 0a 2a 2a 20 20 20 31 20 20 20 20 20   0)..**   1     
3fe1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fe1c 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
3fe1d 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65     memory    (re
3fe1e 74 75 72 6e 20 31 29 0d 0a 2a 2a 20 20 20 31 20  turn 1)..**   1 
3fe1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fe20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
3fe21 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20 20         file     
3fe22 20 28 72 65 74 75 72 6e 20 30 29 0d 0a 2a 2a 20   (return 0)..** 
3fe23 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
3fe24 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
3fe25 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
3fe26 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0d       (return 0).
3fe27 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20  .**   2         
3fe28 20 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20              2   
3fe29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
3fe2a 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e  emory    (return
3fe2b 20 31 29 0d 0a 2a 2a 20 20 20 32 20 20 20 20 20   1)..**   2     
3fe2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fe2d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
3fe2e 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65     memory    (re
3fe2f 74 75 72 6e 20 31 29 0d 0a 2a 2a 20 20 20 33 20  turn 1)..**   3 
3fe30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fe31 20 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20      any         
3fe32 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20         memory   
3fe33 20 28 72 65 74 75 72 6e 20 31 29 0d 0a 2a 2f 0d   (return 1)..*/.
3fe34 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3fe35 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49  int sqlite3TempI
3fe36 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71  nMemory(const sq
3fe37 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 23 69 66  lite3 *db){..#if
3fe38 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
3fe39 52 45 3d 3d 31 0d 0a 20 20 72 65 74 75 72 6e 20  RE==1..  return 
3fe3a 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  ( db->temp_store
3fe3b 3d 3d 32 20 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a  ==2 );..#endif..
3fe3c 23 69 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  #if SQLITE_TEMP_
3fe3d 53 54 4f 52 45 3d 3d 32 0d 0a 20 20 72 65 74 75  STORE==2..  retu
3fe3e 72 6e 20 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74  rn ( db->temp_st
3fe3f 6f 72 65 21 3d 31 20 29 3b 0d 0a 23 65 6e 64 69  ore!=1 );..#endi
3fe40 66 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  f..#if SQLITE_TE
3fe41 4d 50 5f 53 54 4f 52 45 3d 3d 33 0d 0a 20 20 72  MP_STORE==3..  r
3fe42 65 74 75 72 6e 20 31 3b 0d 0a 23 65 6e 64 69 66  eturn 1;..#endif
3fe43 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45 4d  ..#if SQLITE_TEM
3fe44 50 5f 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51 4c  P_STORE<1 || SQL
3fe45 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e 33  ITE_TEMP_STORE>3
3fe46 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 23  ..  return 0;..#
3fe47 65 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  endif..}..../*..
3fe48 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 38 20  ** Return UTF-8 
3fe49 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20  encoded English 
3fe4a 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61  language explana
3fe4b 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
3fe4c 20 72 65 63 65 6e 74 0d 0a 2a 2a 20 65 72 72 6f   recent..** erro
3fe4d 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  r...*/..SQLITE_A
3fe4e 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
3fe4f 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71  qlite3_errmsg(sq
3fe50 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 63  lite3 *db){..  c
3fe51 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0d 0a 20  onst char *z;.. 
3fe52 20 69 66 28 20 21 64 62 20 29 7b 0d 0a 20 20 20   if( !db ){..   
3fe53 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
3fe54 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d  rrStr(SQLITE_NOM
3fe55 45 4d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28  EM);..  }..  if(
3fe56 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
3fe57 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
3fe58 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
3fe59 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51  sqlite3ErrStr(SQ
3fe5a 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54  LITE_MISUSE_BKPT
3fe5b 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  );..  }..  sqlit
3fe5c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
3fe5d 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 69 66  b->mutex);..  if
3fe5e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
3fe5f 65 64 20 29 7b 0d 0a 20 20 20 20 7a 20 3d 20 73  ed ){..    z = s
3fe60 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c  qlite3ErrStr(SQL
3fe61 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0d 0a 20 20 7d  ITE_NOMEM);..  }
3fe62 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 20 3d 20 28  else{..    z = (
3fe63 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
3fe64 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72  lue_text(db->pEr
3fe65 72 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28  r);..    assert(
3fe66 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
3fe67 65 64 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a  ed );..    if( z
3fe68 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 7a 20  ==0 ){..      z 
3fe69 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  = sqlite3ErrStr(
3fe6a 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b 0d 0a 20  db->errCode);.. 
3fe6b 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c     }..  }..  sql
3fe6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3fe6d 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fe6e 72 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 0d 0a  return z;..}....
3fe6f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3fe70 4d 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a  MIT_UTF16../*..*
3fe71 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36 20  * Return UTF-16 
3fe72 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20  encoded English 
3fe73 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61  language explana
3fe74 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
3fe75 20 72 65 63 65 6e 74 0d 0a 2a 2a 20 65 72 72 6f   recent..** erro
3fe76 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41  r...*/..SQLITE_A
3fe77 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
3fe78 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
3fe79 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20  sqlite3 *db){.. 
3fe7a 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 31   static const u1
3fe7b 36 20 6f 75 74 4f 66 4d 65 6d 5b 5d 20 3d 20 7b  6 outOfMem[] = {
3fe7c 0d 0a 20 20 20 20 27 6f 27 2c 20 27 75 27 2c 20  ..    'o', 'u', 
3fe7d 27 74 27 2c 20 27 20 27 2c 20 27 6f 27 2c 20 27  't', ' ', 'o', '
3fe7e 66 27 2c 20 27 20 27 2c 20 27 6d 27 2c 20 27 65  f', ' ', 'm', 'e
3fe7f 27 2c 20 27 6d 27 2c 20 27 6f 27 2c 20 27 72 27  ', 'm', 'o', 'r'
3fe80 2c 20 27 79 27 2c 20 30 0d 0a 20 20 7d 3b 0d 0a  , 'y', 0..  };..
3fe81 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
3fe82 31 36 20 6d 69 73 75 73 65 5b 5d 20 3d 20 7b 0d  16 misuse[] = {.
3fe83 0a 20 20 20 20 27 6c 27 2c 20 27 69 27 2c 20 27  .    'l', 'i', '
3fe84 62 27 2c 20 27 72 27 2c 20 27 61 27 2c 20 27 72  b', 'r', 'a', 'r
3fe85 27 2c 20 27 79 27 2c 20 27 20 27 2c 20 0d 0a 20  ', 'y', ' ', .. 
3fe86 20 20 20 27 72 27 2c 20 27 6f 27 2c 20 27 75 27     'r', 'o', 'u'
3fe87 2c 20 27 74 27 2c 20 27 69 27 2c 20 27 6e 27 2c  , 't', 'i', 'n',
3fe88 20 27 65 27 2c 20 27 20 27 2c 20 0d 0a 20 20 20   'e', ' ', ..   
3fe89 20 27 63 27 2c 20 27 61 27 2c 20 27 6c 27 2c 20   'c', 'a', 'l', 
3fe8a 27 6c 27 2c 20 27 65 27 2c 20 27 64 27 2c 20 27  'l', 'e', 'd', '
3fe8b 20 27 2c 20 0d 0a 20 20 20 20 27 6f 27 2c 20 27   ', ..    'o', '
3fe8c 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 0d 0a  u', 't', ' ', ..
3fe8d 20 20 20 20 27 6f 27 2c 20 27 66 27 2c 20 27 20      'o', 'f', ' 
3fe8e 27 2c 20 0d 0a 20 20 20 20 27 73 27 2c 20 27 65  ', ..    's', 'e
3fe8f 27 2c 20 27 71 27 2c 20 27 75 27 2c 20 27 65 27  ', 'q', 'u', 'e'
3fe90 2c 20 27 6e 27 2c 20 27 63 27 2c 20 27 65 27 2c  , 'n', 'c', 'e',
3fe91 20 30 0d 0a 20 20 7d 3b 0d 0a 0d 0a 20 20 63 6f   0..  };....  co
3fe92 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0d 0a 20 20  nst void *z;..  
3fe93 69 66 28 20 21 64 62 20 29 7b 0d 0a 20 20 20 20  if( !db ){..    
3fe94 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6f  return (void *)o
3fe95 75 74 4f 66 4d 65 6d 3b 0d 0a 20 20 7d 0d 0a 20  utOfMem;..  }.. 
3fe96 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
3fe97 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
3fe98 28 64 62 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  (db) ){..    ret
3fe99 75 72 6e 20 28 76 6f 69 64 20 2a 29 6d 69 73 75  urn (void *)misu
3fe9a 73 65 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  se;..  }..  sqli
3fe9b 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3fe9c 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 69  db->mutex);..  i
3fe9d 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
3fe9e 6c 65 64 20 29 7b 0d 0a 20 20 20 20 7a 20 3d 20  led ){..    z = 
3fe9f 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d  (void *)outOfMem
3fea0 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
3fea1 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   z = sqlite3_val
3fea2 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45  ue_text16(db->pE
3fea3 72 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a 3d  rr);..    if( z=
3fea4 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  =0 ){..      sql
3fea5 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
3fea6 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71  db->pErr, -1, sq
3fea7 6c 69 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e  lite3ErrStr(db->
3fea8 65 72 72 43 6f 64 65 29 2c 0d 0a 20 20 20 20 20  errCode),..     
3fea9 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
3feaa 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
3feab 29 3b 0d 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  );..      z = sq
3feac 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
3fead 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0d 0a 20  16(db->pErr);.. 
3feae 20 20 20 7d 0d 0a 20 20 20 20 2f 2a 20 41 20 6d     }..    /* A m
3feaf 61 6c 6c 6f 63 28 29 20 6d 61 79 20 68 61 76 65  alloc() may have
3feb0 20 66 61 69 6c 65 64 20 77 69 74 68 69 6e 20 74   failed within t
3feb1 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
3feb2 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
3feb3 29 0d 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e  )..    ** above.
3feb4 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
3feb5 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64  case, then the d
3feb6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3feb7 66 6c 61 67 20 6e 65 65 64 73 20 74 6f 0d 0a 20  flag needs to.. 
3feb8 20 20 20 2a 2a 20 62 65 20 63 6c 65 61 72 65 64     ** be cleared
3feb9 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
3feba 67 2e 20 44 6f 20 74 68 69 73 20 64 69 72 65 63  g. Do this direc
3febb 74 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f 66 20  tly, instead of 
3febc 76 69 61 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69  via..    ** sqli
3febd 74 65 33 41 70 69 45 78 69 74 28 29 2c 20 74 6f  te3ApiExit(), to
3febe 20 61 76 6f 69 64 20 73 65 74 74 69 6e 67 20 74   avoid setting t
3febf 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
3fec0 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  le error message
3fec1 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 64  ...    */..    d
3fec2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3fec3 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  = 0;..  }..  sql
3fec4 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3fec5 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
3fec6 72 65 74 75 72 6e 20 7a 3b 0d 0a 7d 0d 0a 23 65  return z;..}..#e
3fec7 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
3fec8 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 0d 0a  MIT_UTF16 */....
3fec9 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /*..** Return th
3feca 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
3fecb 72 6f 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ror code generat
3fecc 65 64 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20  ed by an SQLite 
3fecd 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c  routine. If NULL
3fece 20 69 73 0d 0a 2a 2a 20 70 61 73 73 65 64 20 74   is..** passed t
3fecf 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  o this function,
3fed0 20 77 65 20 61 73 73 75 6d 65 20 61 20 6d 61 6c   we assume a mal
3fed1 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72  loc() failed dur
3fed2 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ing sqlite3_open
3fed3 28 29 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ()...*/..SQLITE_
3fed4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3fed5 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20  errcode(sqlite3 
3fed6 2a 64 62 29 7b 0d 0a 20 20 69 66 28 20 64 62 20  *db){..  if( db 
3fed7 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  && !sqlite3Safet
3fed8 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
3fed9 62 29 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  b) ){..    retur
3feda 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f  n SQLITE_MISUSE_
3fedb 42 4b 50 54 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  BKPT;..  }..  if
3fedc 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c  ( !db || db->mal
3fedd 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0d 0a 20 20  locFailed ){..  
3fede 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3fedf 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 72  NOMEM;..  }..  r
3fee0 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64  eturn db->errCod
3fee1 65 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b  e & db->errMask;
3fee2 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..}..SQLITE_API 
3fee3 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
3fee4 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c  nded_errcode(sql
3fee5 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 69 66  ite3 *db){..  if
3fee6 28 20 64 62 20 26 26 20 21 73 71 6c 69 74 65 33  ( db && !sqlite3
3fee7 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
3fee8 72 4f 6b 28 64 62 29 20 29 7b 0d 0a 20 20 20 20  rOk(db) ){..    
3fee9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3feea 53 55 53 45 5f 42 4b 50 54 3b 0d 0a 20 20 7d 0d  SUSE_BKPT;..  }.
3feeb 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62  .  if( !db || db
3feec 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
3feed 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  {..    return SQ
3feee 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d  LITE_NOMEM;..  }
3feef 0d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65  ..  return db->e
3fef0 72 72 43 6f 64 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  rrCode;..}..../*
3fef1 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  ..** Create a ne
3fef2 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  w collating func
3fef3 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62 61 73  tion for databas
3fef4 65 20 22 64 62 22 2e 20 20 54 68 65 20 6e 61 6d  e "db".  The nam
3fef5 65 20 69 73 20 7a 4e 61 6d 65 0d 0a 2a 2a 20 61  e is zName..** a
3fef6 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  nd the encoding 
3fef7 69 73 20 65 6e 63 2e 0d 0a 2a 2f 0d 0a 73 74 61  is enc...*/..sta
3fef8 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f  tic int createCo
3fef9 6c 6c 61 74 69 6f 6e 28 0d 0a 20 20 73 71 6c 69  llation(..  sqli
3fefa 74 65 33 2a 20 64 62 2c 0d 0a 20 20 63 6f 6e 73  te3* db,..  cons
3fefb 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0d  t char *zName, .
3fefc 0a 20 20 75 38 20 65 6e 63 2c 0d 0a 20 20 76 6f  .  u8 enc,..  vo
3fefd 69 64 2a 20 70 43 74 78 2c 0d 0a 20 20 69 6e 74  id* pCtx,..  int
3fefe 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
3feff 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
3ff00 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
3ff01 2a 29 2c 0d 0a 20 20 76 6f 69 64 28 2a 78 44 65  *),..  void(*xDe
3ff02 6c 29 28 76 6f 69 64 2a 29 0d 0a 29 7b 0d 0a 20  l)(void*)..){.. 
3ff03 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
3ff04 0d 0a 20 20 69 6e 74 20 65 6e 63 32 3b 0d 0a 20  ..  int enc2;.. 
3ff05 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c   int nName = sql
3ff06 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
3ff07 6d 65 29 3b 0d 0a 20 20 0d 0a 20 20 61 73 73 65  me);..  ..  asse
3ff08 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
3ff09 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
3ff0a 29 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  ) );....  /* If 
3ff0b 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20  SQLITE_UTF16 is 
3ff0c 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65  specified as the
3ff0d 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20   encoding type, 
3ff0e 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0d 0a  transform this..
3ff0f 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53    ** to one of S
3ff10 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
3ff11 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
3ff12 75 73 69 6e 67 20 74 68 65 0d 0a 20 20 2a 2a 20  using the..  ** 
3ff13 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
3ff14 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45  VE macro. SQLITE
3ff15 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73  _UTF16 is not us
3ff16 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0d 0a  ed internally...
3ff17 20 20 2a 2f 0d 0a 20 20 65 6e 63 32 20 3d 20 65    */..  enc2 = e
3ff18 6e 63 3b 0d 0a 20 20 74 65 73 74 63 61 73 65 28  nc;..  testcase(
3ff19 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54   enc2==SQLITE_UT
3ff1a 46 31 36 20 29 3b 0d 0a 20 20 74 65 73 74 63 61  F16 );..  testca
3ff1b 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45  se( enc2==SQLITE
3ff1c 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29  _UTF16_ALIGNED )
3ff1d 3b 0d 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53  ;..  if( enc2==S
3ff1e 51 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c 20 65  QLITE_UTF16 || e
3ff1f 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  nc2==SQLITE_UTF1
3ff20 36 5f 41 4c 49 47 4e 45 44 20 29 7b 0d 0a 20 20  6_ALIGNED ){..  
3ff21 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f    enc2 = SQLITE_
3ff22 55 54 46 31 36 4e 41 54 49 56 45 3b 0d 0a 20 20  UTF16NATIVE;..  
3ff23 7d 0d 0a 20 20 69 66 28 20 65 6e 63 32 3c 53 51  }..  if( enc2<SQ
3ff24 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63  LITE_UTF8 || enc
3ff25 32 3e 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  2>SQLITE_UTF16BE
3ff26 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
3ff27 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
3ff28 50 54 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  PT;..  }....  /*
3ff29 20 43 68 65 63 6b 20 69 66 20 74 68 69 73 20 63   Check if this c
3ff2a 61 6c 6c 20 69 73 20 72 65 6d 6f 76 69 6e 67 20  all is removing 
3ff2b 6f 72 20 72 65 70 6c 61 63 69 6e 67 20 61 6e 20  or replacing an 
3ff2c 65 78 69 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69  existing collati
3ff2d 6f 6e 20 0d 0a 20 20 2a 2a 20 73 65 71 75 65 6e  on ..  ** sequen
3ff2e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e 64 20 74  ce. If so, and t
3ff2f 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
3ff30 56 4d 73 2c 20 72 65 74 75 72 6e 20 62 75 73 79  VMs, return busy
3ff31 2e 20 49 66 20 74 68 65 72 65 0d 0a 20 20 2a 2a  . If there..  **
3ff32 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56   are no active V
3ff33 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65 20 61  Ms, invalidate a
3ff34 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20  ny pre-compiled 
3ff35 73 74 61 74 65 6d 65 6e 74 73 2e 0d 0a 20 20 2a  statements...  *
3ff36 2f 0d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  /..  pColl = sql
3ff37 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
3ff38 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e  db, (u8)enc2, zN
3ff39 61 6d 65 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20  ame, 0);..  if( 
3ff3a 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e  pColl && pColl->
3ff3b 78 43 6d 70 20 29 7b 0d 0a 20 20 20 20 69 66 28  xCmp ){..    if(
3ff3c 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
3ff3d 6e 74 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c  nt ){..      sql
3ff3e 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
3ff3f 4c 49 54 45 5f 42 55 53 59 2c 20 0d 0a 20 20 20  LITE_BUSY, ..   
3ff40 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
3ff41 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f  delete/modify co
3ff42 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3ff43 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73   due to active s
3ff44 74 61 74 65 6d 65 6e 74 73 22 29 3b 0d 0a 20 20  tatements");..  
3ff45 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3ff46 45 5f 42 55 53 59 3b 0d 0a 20 20 20 20 7d 0d 0a  E_BUSY;..    }..
3ff47 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72      sqlite3Expir
3ff48 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
3ff49 6e 74 73 28 64 62 29 3b 0d 0a 0d 0a 20 20 20 20  nts(db);....    
3ff4a 2f 2a 20 49 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  /* If collation 
3ff4b 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 77  sequence pColl w
3ff4c 61 73 20 63 72 65 61 74 65 64 20 64 69 72 65 63  as created direc
3ff4d 74 6c 79 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  tly by a call to
3ff4e 0d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  ..    ** sqlite3
3ff4f 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
3ff50 6e 2c 20 61 6e 64 20 6e 6f 74 20 67 65 6e 65 72  n, and not gener
3ff51 61 74 65 64 20 62 79 20 73 79 6e 74 68 43 6f 6c  ated by synthCol
3ff52 6c 53 65 71 28 29 2c 0d 0a 20 20 20 20 2a 2a 20  lSeq(),..    ** 
3ff53 74 68 65 6e 20 61 6e 79 20 63 6f 70 69 65 73 20  then any copies 
3ff54 6d 61 64 65 20 62 79 20 73 79 6e 74 68 43 6f 6c  made by synthCol
3ff55 6c 53 65 71 28 29 20 6e 65 65 64 20 74 6f 20 62  lSeq() need to b
3ff56 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0d 0a  e invalidated...
3ff57 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f 6c      ** Also, col
3ff58 6c 61 74 69 6f 6e 20 64 65 73 74 72 75 63 74 6f  lation destructo
3ff59 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78 44 65 6c  r - CollSeq.xDel
3ff5a 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6d 61  () - function ma
3ff5b 79 20 6e 65 65 64 0d 0a 20 20 20 20 2a 2a 20 74  y need..    ** t
3ff5c 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0d 0a 20 20  o be called...  
3ff5d 20 20 2a 2f 20 0d 0a 20 20 20 20 69 66 28 20 28    */ ..    if( (
3ff5e 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51  pColl->enc & ~SQ
3ff5f 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
3ff60 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0d 0a 20 20  ED)==enc2 ){..  
3ff61 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f      CollSeq *aCo
3ff62 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ll = sqlite3Hash
3ff63 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  Find(&db->aCollS
3ff64 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  eq, zName, nName
3ff65 29 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  );..      int j;
3ff66 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ..      for(j=0;
3ff67 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20   j<3; j++){..   
3ff68 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20       CollSeq *p 
3ff69 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0d 0a 20 20  = &aColl[j];..  
3ff6a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 6e 63        if( p->enc
3ff6b 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0d  ==pColl->enc ){.
3ff6c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
3ff6d 2d 3e 78 44 65 6c 20 29 7b 0d 0a 20 20 20 20 20  ->xDel ){..     
3ff6e 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 70         p->xDel(p
3ff6f 2d 3e 70 55 73 65 72 29 3b 0d 0a 20 20 20 20 20  ->pUser);..     
3ff70 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
3ff71 20 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0d 0a    p->xCmp = 0;..
3ff72 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
3ff73 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
3ff74 0d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ..  pColl = sqli
3ff75 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
3ff76 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61  b, (u8)enc2, zNa
3ff77 6d 65 2c 20 31 29 3b 0d 0a 20 20 69 66 28 20 70  me, 1);..  if( p
3ff78 43 6f 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  Coll==0 ) return
3ff79 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
3ff7a 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20    pColl->xCmp = 
3ff7b 78 43 6f 6d 70 61 72 65 3b 0d 0a 20 20 70 43 6f  xCompare;..  pCo
3ff7c 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78  ll->pUser = pCtx
3ff7d 3b 0d 0a 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c  ;..  pColl->xDel
3ff7e 20 3d 20 78 44 65 6c 3b 0d 0a 20 20 70 43 6f 6c   = xDel;..  pCol
3ff7f 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28 65 6e  l->enc = (u8)(en
3ff80 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c 49  c2 | (enc & SQLI
3ff81 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
3ff82 29 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 45 72  ));..  sqlite3Er
3ff83 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
3ff84 4b 2c 20 30 29 3b 0d 0a 20 20 72 65 74 75 72 6e  K, 0);..  return
3ff85 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
3ff86 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
3ff87 61 72 72 61 79 20 64 65 66 69 6e 65 73 20 68 61  array defines ha
3ff88 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 73 20  rd upper bounds 
3ff89 6f 6e 20 6c 69 6d 69 74 20 76 61 6c 75 65 73 2e  on limit values.
3ff8a 20 20 54 68 65 0d 0a 2a 2a 20 69 6e 69 74 69 61    The..** initia
3ff8b 6c 69 7a 65 72 20 6d 75 73 74 20 62 65 20 6b 65  lizer must be ke
3ff8c 70 74 20 69 6e 20 73 79 6e 63 20 77 69 74 68 20  pt in sync with 
3ff8d 74 68 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  the SQLITE_LIMIT
3ff8e 5f 2a 0d 0a 2a 2a 20 23 64 65 66 69 6e 65 73 20  _*..** #defines 
3ff8f 69 6e 20 73 71 6c 69 74 65 33 2e 68 2e 0d 0a 2a  in sqlite3.h...*
3ff90 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  /..static const 
3ff91 69 6e 74 20 61 48 61 72 64 4c 69 6d 69 74 5b 5d  int aHardLimit[]
3ff92 20 3d 20 7b 0d 0a 20 20 53 51 4c 49 54 45 5f 4d   = {..  SQLITE_M
3ff93 41 58 5f 4c 45 4e 47 54 48 2c 0d 0a 20 20 53 51  AX_LENGTH,..  SQ
3ff94 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
3ff95 47 54 48 2c 0d 0a 20 20 53 51 4c 49 54 45 5f 4d  GTH,..  SQLITE_M
3ff96 41 58 5f 43 4f 4c 55 4d 4e 2c 0d 0a 20 20 53 51  AX_COLUMN,..  SQ
3ff97 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
3ff98 50 54 48 2c 0d 0a 20 20 53 51 4c 49 54 45 5f 4d  PTH,..  SQLITE_M
3ff99 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
3ff9a 43 54 2c 0d 0a 20 20 53 51 4c 49 54 45 5f 4d 41  CT,..  SQLITE_MA
3ff9b 58 5f 56 44 42 45 5f 4f 50 2c 0d 0a 20 20 53 51  X_VDBE_OP,..  SQ
3ff9c 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f  LITE_MAX_FUNCTIO
3ff9d 4e 5f 41 52 47 2c 0d 0a 20 20 53 51 4c 49 54 45  N_ARG,..  SQLITE
3ff9e 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2c 0d 0a  _MAX_ATTACHED,..
3ff9f 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b    SQLITE_MAX_LIK
3ffa0 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
3ffa1 2c 0d 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ,..  SQLITE_MAX_
3ffa2 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 2c  VARIABLE_NUMBER,
3ffa3 0d 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54  ..  SQLITE_MAX_T
3ffa4 52 49 47 47 45 52 5f 44 45 50 54 48 2c 0d 0a 7d  RIGGER_DEPTH,..}
3ffa5 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6b 65  ;..../*..** Make
3ffa6 20 73 75 72 65 20 74 68 65 20 68 61 72 64 20 6c   sure the hard l
3ffa7 69 6d 69 74 73 20 61 72 65 20 73 65 74 20 74 6f  imits are set to
3ffa8 20 72 65 61 73 6f 6e 61 62 6c 65 20 76 61 6c 75   reasonable valu
3ffa9 65 73 0d 0a 2a 2f 0d 0a 23 69 66 20 53 51 4c 49  es..*/..#if SQLI
3ffaa 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3c 31 30  TE_MAX_LENGTH<10
3ffab 30 0d 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  0..# error SQLIT
3ffac 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 6d 75 73  E_MAX_LENGTH mus
3ffad 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30  t be at least 10
3ffae 30 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 53  0..#endif..#if S
3ffaf 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
3ffb0 4e 47 54 48 3c 31 30 30 0d 0a 23 20 65 72 72 6f  NGTH<100..# erro
3ffb1 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  r SQLITE_MAX_SQL
3ffb2 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20  _LENGTH must be 
3ffb3 61 74 20 6c 65 61 73 74 20 31 30 30 0d 0a 23 65  at least 100..#e
3ffb4 6e 64 69 66 0d 0a 23 69 66 20 53 51 4c 49 54 45  ndif..#if SQLITE
3ffb5 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e  _MAX_SQL_LENGTH>
3ffb6 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
3ffb7 48 0d 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  H..# error SQLIT
3ffb8 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
3ffb9 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 67 72 65   must not be gre
3ffba 61 74 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ater than SQLITE
3ffbb 5f 4d 41 58 5f 4c 45 4e 47 54 48 0d 0a 23 65 6e  _MAX_LENGTH..#en
3ffbc 64 69 66 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f  dif..#if SQLITE_
3ffbd 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
3ffbe 45 43 54 3c 32 0d 0a 23 20 65 72 72 6f 72 20 53  ECT<2..# error S
3ffbf 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55  QLITE_MAX_COMPOU
3ffc0 4e 44 5f 53 45 4c 45 43 54 20 6d 75 73 74 20 62  ND_SELECT must b
3ffc1 65 20 61 74 20 6c 65 61 73 74 20 32 0d 0a 23 65  e at least 2..#e
3ffc2 6e 64 69 66 0d 0a 23 69 66 20 53 51 4c 49 54 45  ndif..#if SQLITE
3ffc3 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 3c 34 30 0d  _MAX_VDBE_OP<40.
3ffc4 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
3ffc5 4d 41 58 5f 56 44 42 45 5f 4f 50 20 6d 75 73 74  MAX_VDBE_OP must
3ffc6 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 30 0d   be at least 40.
3ffc7 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20 53 51 4c  .#endif..#if SQL
3ffc8 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
3ffc9 5f 41 52 47 3c 30 20 7c 7c 20 53 51 4c 49 54 45  _ARG<0 || SQLITE
3ffca 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
3ffcb 47 3e 31 30 30 30 0d 0a 23 20 65 72 72 6f 72 20  G>1000..# error 
3ffcc 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
3ffcd 49 4f 4e 5f 41 52 47 20 6d 75 73 74 20 62 65 20  ION_ARG must be 
3ffce 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 30  between 0 and 10
3ffcf 30 30 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66 20  00..#endif..#if 
3ffd0 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
3ffd1 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f  HED<0 || SQLITE_
3ffd2 4d 41 58 5f 41 54 54 41 43 48 45 44 3e 36 32 0d  MAX_ATTACHED>62.
3ffd3 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
3ffd4 4d 41 58 5f 41 54 54 41 43 48 45 44 20 6d 75 73  MAX_ATTACHED mus
3ffd5 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61  t be between 0 a
3ffd6 6e 64 20 36 32 0d 0a 23 65 6e 64 69 66 0d 0a 23  nd 62..#endif..#
3ffd7 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  if SQLITE_MAX_LI
3ffd8 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
3ffd9 48 3c 31 0d 0a 23 20 65 72 72 6f 72 20 53 51 4c  H<1..# error SQL
3ffda 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
3ffdb 54 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74  TERN_LENGTH must
3ffdc 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 0d 0a   be at least 1..
3ffdd 23 65 6e 64 69 66 0d 0a 23 69 66 20 53 51 4c 49  #endif..#if SQLI
3ffde 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32  TE_MAX_COLUMN>32
3ffdf 37 36 37 0d 0a 23 20 65 72 72 6f 72 20 53 51 4c  767..# error SQL
3ffe0 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d  ITE_MAX_COLUMN m
3ffe1 75 73 74 20 6e 6f 74 20 65 78 63 65 65 64 20 33  ust not exceed 3
3ffe2 32 37 36 37 0d 0a 23 65 6e 64 69 66 0d 0a 23 69  2767..#endif..#i
3ffe3 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49  f SQLITE_MAX_TRI
3ffe4 47 47 45 52 5f 44 45 50 54 48 3c 31 0d 0a 23 20  GGER_DEPTH<1..# 
3ffe5 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
3ffe6 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 6d  _TRIGGER_DEPTH m
3ffe7 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20  ust be at least 
3ffe8 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d 0a 2f  1..#endif....../
3ffe9 2a 0d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  *..** Change the
3ffea 20 76 61 6c 75 65 20 6f 66 20 61 20 6c 69 6d 69   value of a limi
3ffeb 74 2e 20 20 52 65 70 6f 72 74 20 74 68 65 20 6f  t.  Report the o
3ffec 6c 64 20 76 61 6c 75 65 2e 0d 0a 2a 2a 20 49 66  ld value...** If
3ffed 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c 69 6d 69   an invalid limi
3ffee 74 20 69 6e 64 65 78 20 69 73 20 73 75 70 70 6c  t index is suppl
3ffef 69 65 64 2c 20 72 65 70 6f 72 74 20 2d 31 2e 0d  ied, report -1..
3fff0 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e  .** Make no chan
3fff1 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20 72 65  ges but still re
3fff2 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c  port the old val
3fff3 75 65 20 69 66 20 74 68 65 0d 0a 2a 2a 20 6e 65  ue if the..** ne
3fff4 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61 74  w limit is negat
3fff5 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 6e  ive...**..** A n
3fff6 65 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64  ew lower limit d
3fff7 6f 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65  oes not shrink e
3fff8 78 69 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63  xisting construc
3fff9 74 73 2e 0d 0a 2a 2a 20 49 74 20 6d 65 72 65 6c  ts...** It merel
3fffa 79 20 70 72 65 76 65 6e 74 73 20 6e 65 77 20 63  y prevents new c
3fffb 6f 6e 73 74 72 75 63 74 73 20 74 68 61 74 20 65  onstructs that e
3fffc 78 63 65 65 64 20 74 68 65 20 6c 69 6d 69 74 0d  xceed the limit.
3fffd 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67  .** from forming
3fffe 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
3ffff 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69  I int sqlite3_li
40000 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  mit(sqlite3 *db,
40001 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c 20 69 6e   int limitId, in
40002 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0d 0a 20 20  t newLimit){..  
40003 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0d 0a 0d  int oldLimit;...
40004 0a 0d 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45  ...  /* EVIDENCE
40005 2d 4f 46 3a 20 52 2d 33 30 31 38 39 2d 35 34 30  -OF: R-30189-540
40006 39 37 20 46 6f 72 20 65 61 63 68 20 6c 69 6d 69  97 For each limi
40007 74 20 63 61 74 65 67 6f 72 79 20 53 51 4c 49 54  t category SQLIT
40008 45 5f 4c 49 4d 49 54 5f 4e 41 4d 45 0d 0a 20 20  E_LIMIT_NAME..  
40009 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 61  ** there is a ha
4000a 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 73  rd upper bound s
4000b 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  et at compile-ti
4000c 6d 65 20 62 79 20 61 20 43 20 70 72 65 70 72 6f  me by a C prepro
4000d 63 65 73 73 6f 72 0d 0a 20 20 2a 2a 20 6d 61 63  cessor..  ** mac
4000e 72 6f 20 63 61 6c 6c 65 64 20 53 51 4c 49 54 45  ro called SQLITE
4000f 5f 4d 41 58 5f 4e 41 4d 45 2e 20 28 54 68 65 20  _MAX_NAME. (The 
40010 22 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 65  "_LIMIT_" in the
40011 20 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 64   name is changed
40012 20 74 6f 0d 0a 20 20 2a 2a 20 22 5f 4d 41 58 5f   to..  ** "_MAX_
40013 22 2e 29 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73  ".)..  */..  ass
40014 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69 74 5b  ert( aHardLimit[
40015 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
40016 47 54 48 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58  GTH]==SQLITE_MAX
40017 5f 4c 45 4e 47 54 48 20 29 3b 0d 0a 20 20 61 73  _LENGTH );..  as
40018 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69 74  sert( aHardLimit
40019 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  [SQLITE_LIMIT_SQ
4001a 4c 5f 4c 45 4e 47 54 48 5d 3d 3d 53 51 4c 49 54  L_LENGTH]==SQLIT
4001b 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
4001c 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61   );..  assert( a
4001d 48 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45  HardLimit[SQLITE
4001e 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3d 3d  _LIMIT_COLUMN]==
4001f 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
40020 4e 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  N );..  assert( 
40021 61 48 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54  aHardLimit[SQLIT
40022 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50  E_LIMIT_EXPR_DEP
40023 54 48 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  TH]==SQLITE_MAX_
40024 45 58 50 52 5f 44 45 50 54 48 20 29 3b 0d 0a 20  EXPR_DEPTH );.. 
40025 20 61 73 73 65 72 74 28 20 61 48 61 72 64 4c 69   assert( aHardLi
40026 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
40027 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
40028 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  ]==SQLITE_MAX_CO
40029 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 29 3b 0d  MPOUND_SELECT);.
4002a 0a 20 20 61 73 73 65 72 74 28 20 61 48 61 72 64  .  assert( aHard
4002b 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
4002c 49 54 5f 56 44 42 45 5f 4f 50 5d 3d 3d 53 51 4c  IT_VDBE_OP]==SQL
4002d 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20  ITE_MAX_VDBE_OP 
4002e 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 48  );..  assert( aH
4002f 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ardLimit[SQLITE_
40030 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
40031 52 47 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  RG]==SQLITE_MAX_
40032 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 29 3b 0d  FUNCTION_ARG );.
40033 0a 20 20 61 73 73 65 72 74 28 20 61 48 61 72 64  .  assert( aHard
40034 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
40035 49 54 5f 41 54 54 41 43 48 45 44 5d 3d 3d 53 51  IT_ATTACHED]==SQ
40036 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
40037 44 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  D );..  assert( 
40038 61 48 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54  aHardLimit[SQLIT
40039 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54  E_LIMIT_LIKE_PAT
4003a 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 3d 3d 0d 0a  TERN_LENGTH]==..
4003b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4003c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4003d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
4003e 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
4003f 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 29 3b  ATTERN_LENGTH );
40040 0d 0a 20 20 61 73 73 65 72 74 28 20 61 48 61 72  ..  assert( aHar
40041 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  dLimit[SQLITE_LI
40042 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
40043 42 45 52 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58  BER]==SQLITE_MAX
40044 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
40045 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 61 48  );..  assert( aH
40046 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ardLimit[SQLITE_
40047 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
40048 50 54 48 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58  PTH]==SQLITE_MAX
40049 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 29  _TRIGGER_DEPTH )
4004a 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c  ;..  assert( SQL
4004b 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45  ITE_LIMIT_TRIGGE
4004c 52 5f 44 45 50 54 48 3d 3d 28 53 51 4c 49 54 45  R_DEPTH==(SQLITE
4004d 5f 4e 5f 4c 49 4d 49 54 2d 31 29 20 29 3b 0d 0a  _N_LIMIT-1) );..
4004e 0d 0a 0d 0a 20 20 69 66 28 20 6c 69 6d 69 74 49  ....  if( limitI
4004f 64 3c 30 20 7c 7c 20 6c 69 6d 69 74 49 64 3e 3d  d<0 || limitId>=
40050 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 29  SQLITE_N_LIMIT )
40051 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31  {..    return -1
40052 3b 0d 0a 20 20 7d 0d 0a 20 20 6f 6c 64 4c 69 6d  ;..  }..  oldLim
40053 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  it = db->aLimit[
40054 6c 69 6d 69 74 49 64 5d 3b 0d 0a 20 20 69 66 28  limitId];..  if(
40055 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 20   newLimit>=0 ){ 
40056 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40057 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 32 34 37    /* IMP: R-5247
40058 36 2d 32 38 37 33 32 20 2a 2f 0d 0a 20 20 20 20  6-28732 */..    
40059 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e 61 48 61  if( newLimit>aHa
4005a 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d  rdLimit[limitId]
4005b 20 29 7b 0d 0a 20 20 20 20 20 20 6e 65 77 4c 69   ){..      newLi
4005c 6d 69 74 20 3d 20 61 48 61 72 64 4c 69 6d 69 74  mit = aHardLimit
4005d 5b 6c 69 6d 69 74 49 64 5d 3b 20 20 2f 2a 20 49  [limitId];  /* I
4005e 4d 50 3a 20 52 2d 35 31 34 36 33 2d 32 35 36 33  MP: R-51463-2563
4005f 34 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  4 */..    }..   
40060 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69   db->aLimit[limi
40061 74 49 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b  tId] = newLimit;
40062 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
40063 6f 6c 64 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  oldLimit;       
40064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40065 20 49 4d 50 3a 20 52 2d 35 33 33 34 31 2d 33 35   IMP: R-53341-35
40066 34 31 39 20 2a 2f 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  419 */..}..../*.
40067 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
40068 6e 20 69 73 20 75 73 65 64 20 74 6f 20 70 61 72  n is used to par
40069 73 65 20 62 6f 74 68 20 55 52 49 73 20 61 6e 64  se both URIs and
4006a 20 6e 6f 6e 2d 55 52 49 20 66 69 6c 65 6e 61 6d   non-URI filenam
4006b 65 73 20 70 61 73 73 65 64 20 62 79 20 74 68 65  es passed by the
4006c 0d 0a 2a 2a 20 75 73 65 72 20 74 6f 20 41 50 49  ..** user to API
4006d 20 66 75 6e 63 74 69 6f 6e 73 20 73 71 6c 69 74   functions sqlit
4006e 65 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c  e3_open() or sql
4006f 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2c 20  ite3_open_v2(), 
40070 61 6e 64 20 66 6f 72 20 64 61 74 61 62 61 73 65  and for database
40071 0d 0a 2a 2a 20 55 52 49 73 20 73 70 65 63 69 66  ..** URIs specif
40072 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 41  ied as part of A
40073 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 73  TTACH statements
40074 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69  ...**..** The fi
40075 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
40076 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
40077 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
40078 20 56 46 53 20 74 6f 20 75 73 65 20 28 6f 72 0d   VFS to use (or.
40079 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 6f 20 73 69  .** a NULL to si
4007a 67 6e 69 66 79 20 74 68 65 20 64 65 66 61 75 6c  gnify the defaul
4007b 74 20 56 46 53 29 20 69 66 20 74 68 65 20 55 52  t VFS) if the UR
4007c 49 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  I does not conta
4007d 69 6e 20 61 20 22 76 66 73 3d 78 78 78 22 0d 0a  in a "vfs=xxx"..
4007e 2a 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74  ** query paramet
4007f 65 72 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  er. The second a
40080 72 67 75 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 73  rgument contains
40081 20 74 68 65 20 55 52 49 20 28 6f 72 20 6e 6f 6e   the URI (or non
40082 2d 55 52 49 20 66 69 6c 65 6e 61 6d 65 29 0d 0a  -URI filename)..
40083 2a 2a 20 69 74 73 65 6c 66 2e 20 57 68 65 6e 20  ** itself. When 
40084 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
40085 20 63 61 6c 6c 65 64 20 74 68 65 20 2a 70 46 6c   called the *pFl
40086 61 67 73 20 76 61 72 69 61 62 6c 65 20 73 68 6f  ags variable sho
40087 75 6c 64 20 63 6f 6e 74 61 69 6e 0d 0a 2a 2a 20  uld contain..** 
40088 74 68 65 20 64 65 66 61 75 6c 74 20 66 6c 61 67  the default flag
40089 73 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61  s to open the da
4008a 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77 69  tabase handle wi
4008b 74 68 2e 20 54 68 65 20 76 61 6c 75 65 20 73 74  th. The value st
4008c 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 2a 70 46 6c  ored in..** *pFl
4008d 61 67 73 20 6d 61 79 20 62 65 20 75 70 64 61 74  ags may be updat
4008e 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ed before return
4008f 69 6e 67 20 69 66 20 74 68 65 20 55 52 49 20 66  ing if the URI f
40090 69 6c 65 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 73  ilename contains
40091 20 0d 0a 2a 2a 20 22 63 61 63 68 65 3d 78 78 78   ..** "cache=xxx
40092 22 20 6f 72 20 22 6d 6f 64 65 3d 78 78 78 22 20  " or "mode=xxx" 
40093 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 73  query parameters
40094 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63  ...**..** If suc
40095 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f  cessful, SQLITE_
40096 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
40097 49 6e 20 74 68 69 73 20 63 61 73 65 20 2a 70 70  In this case *pp
40098 56 66 73 20 69 73 20 73 65 74 20 74 6f 20 70 6f  Vfs is set to po
40099 69 6e 74 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 56  int to..** the V
4009a 46 53 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  FS that should b
4009b 65 20 75 73 65 64 20 74 6f 20 6f 70 65 6e 20 74  e used to open t
4009c 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
4009d 2e 20 2a 70 7a 46 69 6c 65 20 69 73 20 73 65 74  . *pzFile is set
4009e 20 74 6f 0d 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f   to..** point to
4009f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69   a buffer contai
400a0 6e 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66  ning the name of
400a1 20 74 68 65 20 66 69 6c 65 20 74 6f 20 6f 70 65   the file to ope
400a2 6e 2e 20 49 74 20 69 73 20 74 68 65 20 0d 0a 2a  n. It is the ..*
400a3 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  * responsibility
400a4 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74   of the caller t
400a5 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 63 61 6c  o eventually cal
400a6 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  l sqlite3_free()
400a7 20 74 6f 20 72 65 6c 65 61 73 65 0d 0a 2a 2a 20   to release..** 
400a8 74 68 69 73 20 62 75 66 66 65 72 2e 0d 0a 2a 2a  this buffer...**
400a9 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  ..** If an error
400aa 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 61 6e   occurs, then an
400ab 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
400ac 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
400ad 6e 64 20 2a 70 7a 45 72 72 4d 73 67 0d 0a 2a 2a  nd *pzErrMsg..**
400ae 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 70   may be set to p
400af 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
400b0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 45   containing an E
400b1 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
400b2 65 72 72 6f 72 20 0d 0a 2a 2a 20 6d 65 73 73 61  error ..** messa
400b3 67 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65  ge. It is the re
400b4 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
400b5 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  the caller to ev
400b6 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73 65  entually release
400b7 0d 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72  ..** this buffer
400b8 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
400b9 74 65 33 5f 66 72 65 65 28 29 2e 0d 0a 2a 2f 0d  te3_free()...*/.
400ba 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
400bb 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65  int sqlite3Parse
400bc 55 72 69 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68  Uri(..  const ch
400bd 61 72 20 2a 7a 44 65 66 61 75 6c 74 56 66 73 2c  ar *zDefaultVfs,
400be 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 74          /* VFS t
400bf 6f 20 75 73 65 20 69 66 20 6e 6f 20 22 76 66 73  o use if no "vfs
400c0 3d 78 78 78 22 20 71 75 65 72 79 20 6f 70 74 69  =xxx" query opti
400c1 6f 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  on */..  const c
400c2 68 61 72 20 2a 7a 55 72 69 2c 20 20 20 20 20 20  har *zUri,      
400c3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d           /* Nul-
400c4 74 65 72 6d 69 6e 61 74 65 64 20 55 52 49 20 74  terminated URI t
400c5 6f 20 70 61 72 73 65 20 2a 2f 0d 0a 20 20 75 6e  o parse */..  un
400c6 73 69 67 6e 65 64 20 69 6e 74 20 2a 70 46 6c 61  signed int *pFla
400c7 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs,           /*
400c8 20 49 4e 2f 4f 55 54 3a 20 53 51 4c 49 54 45 5f   IN/OUT: SQLITE_
400c9 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 2a  OPEN_XXX flags *
400ca 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  /..  sqlite3_vfs
400cb 20 2a 2a 70 70 56 66 73 2c 20 20 20 20 20 20 20   **ppVfs,       
400cc 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 46 53       /* OUT: VFS
400cd 20 74 6f 20 75 73 65 20 2a 2f 20 0d 0a 20 20 63   to use */ ..  c
400ce 68 61 72 20 2a 2a 70 7a 46 69 6c 65 2c 20 20 20  har **pzFile,   
400cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
400d0 2a 20 4f 55 54 3a 20 46 69 6c 65 6e 61 6d 65 20  * OUT: Filename 
400d1 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 55 52 49  component of URI
400d2 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a   */..  char **pz
400d3 45 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20  ErrMsg          
400d4 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
400d5 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 69 66  rror message (if
400d6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20   rc!=SQLITE_OK) 
400d7 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
400d8 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
400d9 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 66 6c   unsigned int fl
400da 61 67 73 20 3d 20 2a 70 46 6c 61 67 73 3b 0d 0a  ags = *pFlags;..
400db 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56    const char *zV
400dc 66 73 20 3d 20 7a 44 65 66 61 75 6c 74 56 66 73  fs = zDefaultVfs
400dd 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ;..  char *zFile
400de 3b 0d 0a 20 20 63 68 61 72 20 63 3b 0d 0a 20 20  ;..  char c;..  
400df 69 6e 74 20 6e 55 72 69 20 3d 20 73 71 6c 69 74  int nUri = sqlit
400e0 65 33 53 74 72 6c 65 6e 33 30 28 7a 55 72 69 29  e3Strlen30(zUri)
400e1 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 2a  ;....  assert( *
400e2 70 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0d 0a  pzErrMsg==0 );..
400e3 0d 0a 20 20 69 66 28 20 28 28 66 6c 61 67 73 20  ..  if( ((flags 
400e4 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52  & SQLITE_OPEN_UR
400e5 49 29 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f  I) || sqlite3Glo
400e6 62 61 6c 43 6f 6e 66 69 67 2e 62 4f 70 65 6e 55  balConfig.bOpenU
400e7 72 69 29 20 0d 0a 20 20 20 26 26 20 6e 55 72 69  ri) ..   && nUri
400e8 3e 3d 35 20 26 26 20 6d 65 6d 63 6d 70 28 7a 55  >=5 && memcmp(zU
400e9 72 69 2c 20 22 66 69 6c 65 3a 22 2c 20 35 29 3d  ri, "file:", 5)=
400ea 3d 30 20 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 63  =0 ..  ){..    c
400eb 68 61 72 20 2a 7a 4f 70 74 3b 0d 0a 20 20 20 20  har *zOpt;..    
400ec 69 6e 74 20 65 53 74 61 74 65 3b 20 20 20 20 20  int eState;     
400ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
400ee 20 50 61 72 73 65 72 20 73 74 61 74 65 20 77 68   Parser state wh
400ef 65 6e 20 70 61 72 73 69 6e 67 20 55 52 49 20 2a  en parsing URI *
400f0 2f 0d 0a 20 20 20 20 69 6e 74 20 69 49 6e 3b 20  /..    int iIn; 
400f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
400f2 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 63 68       /* Input ch
400f3 61 72 61 63 74 65 72 20 69 6e 64 65 78 20 2a 2f  aracter index */
400f4 0d 0a 20 20 20 20 69 6e 74 20 69 4f 75 74 20 3d  ..    int iOut =
400f5 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
400f6 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 63 68      /* Output ch
400f7 61 72 61 63 74 65 72 20 69 6e 64 65 78 20 2a 2f  aracter index */
400f8 0d 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20  ..    int nByte 
400f9 3d 20 6e 55 72 69 2b 32 3b 20 20 20 20 20 20 20  = nUri+2;       
400fa 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20      /* Bytes of 
400fb 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74  space to allocat
400fc 65 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4d  e */....    /* M
400fd 61 6b 65 20 73 75 72 65 20 74 68 65 20 53 51 4c  ake sure the SQL
400fe 49 54 45 5f 4f 50 45 4e 5f 55 52 49 20 66 6c 61  ITE_OPEN_URI fla
400ff 67 20 69 73 20 73 65 74 20 74 6f 20 69 6e 64 69  g is set to indi
40100 63 61 74 65 20 74 6f 20 74 68 65 20 56 46 53 20  cate to the VFS 
40101 78 4f 70 65 6e 20 0d 0a 20 20 20 20 2a 2a 20 6d  xOpen ..    ** m
40102 65 74 68 6f 64 20 74 68 61 74 20 74 68 65 72 65  ethod that there
40103 20 6d 61 79 20 62 65 20 65 78 74 72 61 20 70 61   may be extra pa
40104 72 61 6d 65 74 65 72 73 20 66 6f 6c 6c 6f 77 69  rameters followi
40105 6e 67 20 74 68 65 20 66 69 6c 65 2d 6e 61 6d 65  ng the file-name
40106 2e 20 20 2a 2f 0d 0a 20 20 20 20 66 6c 61 67 73  .  */..    flags
40107 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   |= SQLITE_OPEN_
40108 55 52 49 3b 0d 0a 0d 0a 20 20 20 20 66 6f 72 28  URI;....    for(
40109 69 49 6e 3d 30 3b 20 69 49 6e 3c 6e 55 72 69 3b  iIn=0; iIn<nUri;
4010a 20 69 49 6e 2b 2b 29 20 6e 42 79 74 65 20 2b 3d   iIn++) nByte +=
4010b 20 28 7a 55 72 69 5b 69 49 6e 5d 3d 3d 27 26 27   (zUri[iIn]=='&'
4010c 29 3b 0d 0a 20 20 20 20 7a 46 69 6c 65 20 3d 20  );..    zFile = 
4010d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
4010e 42 79 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20  Byte);..    if( 
4010f 21 7a 46 69 6c 65 20 29 20 72 65 74 75 72 6e 20  !zFile ) return 
40110 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 0d  SQLITE_NOMEM;...
40111 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72 64 20  .    /* Discard 
40112 74 68 65 20 73 63 68 65 6d 65 20 61 6e 64 20 61  the scheme and a
40113 75 74 68 6f 72 69 74 79 20 73 65 67 6d 65 6e 74  uthority segment
40114 73 20 6f 66 20 74 68 65 20 55 52 49 2e 20 2a 2f  s of the URI. */
40115 0d 0a 20 20 20 20 69 66 28 20 7a 55 72 69 5b 35  ..    if( zUri[5
40116 5d 3d 3d 27 2f 27 20 26 26 20 7a 55 72 69 5b 36  ]=='/' && zUri[6
40117 5d 3d 3d 27 2f 27 20 29 7b 0d 0a 20 20 20 20 20  ]=='/' ){..     
40118 20 69 49 6e 20 3d 20 37 3b 0d 0a 20 20 20 20 20   iIn = 7;..     
40119 20 77 68 69 6c 65 28 20 7a 55 72 69 5b 69 49 6e   while( zUri[iIn
4011a 5d 20 26 26 20 7a 55 72 69 5b 69 49 6e 5d 21 3d  ] && zUri[iIn]!=
4011b 27 2f 27 20 29 20 69 49 6e 2b 2b 3b 0d 0a 0d 0a  '/' ) iIn++;....
4011c 20 20 20 20 20 20 69 66 28 20 69 49 6e 21 3d 37        if( iIn!=7
4011d 20 26 26 20 28 69 49 6e 21 3d 31 36 20 7c 7c 20   && (iIn!=16 || 
4011e 6d 65 6d 63 6d 70 28 22 6c 6f 63 61 6c 68 6f 73  memcmp("localhos
4011f 74 22 2c 20 26 7a 55 72 69 5b 37 5d 2c 20 39 29  t", &zUri[7], 9)
40120 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 70  ) ){..        *p
40121 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
40122 33 5f 6d 70 72 69 6e 74 66 28 22 69 6e 76 61 6c  3_mprintf("inval
40123 69 64 20 75 72 69 20 61 75 74 68 6f 72 69 74 79  id uri authority
40124 3a 20 25 2e 2a 73 22 2c 20 0d 0a 20 20 20 20 20  : %.*s", ..     
40125 20 20 20 20 20 20 20 69 49 6e 2d 37 2c 20 26 7a         iIn-7, &z
40126 55 72 69 5b 37 5d 29 3b 0d 0a 20 20 20 20 20 20  Uri[7]);..      
40127 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
40128 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f  ROR;..        go
40129 74 6f 20 70 61 72 73 65 5f 75 72 69 5f 6f 75 74  to parse_uri_out
4012a 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
4012b 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 69 49  }else{..      iI
4012c 6e 20 3d 20 35 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  n = 5;..    }...
4012d 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  .    /* Copy the
4012e 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 61 6e   filename and an
4012f 79 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 65  y query paramete
40130 72 73 20 69 6e 74 6f 20 74 68 65 20 7a 46 69 6c  rs into the zFil
40131 65 20 62 75 66 66 65 72 2e 20 0d 0a 20 20 20 20  e buffer. ..    
40132 2a 2a 20 44 65 63 6f 64 65 20 25 48 48 20 65 73  ** Decode %HH es
40133 63 61 70 65 20 63 6f 64 65 73 20 61 6c 6f 6e 67  cape codes along
40134 20 74 68 65 20 77 61 79 2e 20 0d 0a 20 20 20 20   the way. ..    
40135 2a 2a 0d 0a 20 20 20 20 2a 2a 20 57 69 74 68 69  **..    ** Withi
40136 6e 20 74 68 69 73 20 6c 6f 6f 70 2c 20 76 61 72  n this loop, var
40137 69 61 62 6c 65 20 65 53 74 61 74 65 20 6d 61 79  iable eState may
40138 20 62 65 20 73 65 74 20 74 6f 20 30 2c 20 31 20   be set to 0, 1 
40139 6f 72 20 32 2c 20 64 65 70 65 6e 64 69 6e 67 0d  or 2, depending.
4013a 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 70  .    ** on the p
4013b 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
4013c 41 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 20 20 20  As follows:..   
4013d 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 30 3a   **..    **   0:
4013e 20 50 61 72 73 69 6e 67 20 66 69 6c 65 2d 6e 61   Parsing file-na
4013f 6d 65 2e 0d 0a 20 20 20 20 2a 2a 20 20 20 31 3a  me...    **   1:
40140 20 50 61 72 73 69 6e 67 20 6e 61 6d 65 20 73 65   Parsing name se
40141 63 74 69 6f 6e 20 6f 66 20 61 20 6e 61 6d 65 3d  ction of a name=
40142 76 61 6c 75 65 20 71 75 65 72 79 20 70 61 72 61  value query para
40143 6d 65 74 65 72 2e 0d 0a 20 20 20 20 2a 2a 20 20  meter...    **  
40144 20 32 3a 20 50 61 72 73 69 6e 67 20 76 61 6c 75   2: Parsing valu
40145 65 20 73 65 63 74 69 6f 6e 20 6f 66 20 61 20 6e  e section of a n
40146 61 6d 65 3d 76 61 6c 75 65 20 71 75 65 72 79 20  ame=value query 
40147 70 61 72 61 6d 65 74 65 72 2e 0d 0a 20 20 20 20  parameter...    
40148 2a 2f 0d 0a 20 20 20 20 65 53 74 61 74 65 20 3d  */..    eState =
40149 20 30 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20   0;..    while( 
4014a 28 63 20 3d 20 7a 55 72 69 5b 69 49 6e 5d 29 21  (c = zUri[iIn])!
4014b 3d 30 20 26 26 20 63 21 3d 27 23 27 20 29 7b 0d  =0 && c!='#' ){.
4014c 0a 20 20 20 20 20 20 69 49 6e 2b 2b 3b 0d 0a 20  .      iIn++;.. 
4014d 20 20 20 20 20 69 66 28 20 63 3d 3d 27 25 27 20       if( c=='%' 
4014e 0d 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69  ..       && sqli
4014f 74 65 33 49 73 78 64 69 67 69 74 28 7a 55 72 69  te3Isxdigit(zUri
40150 5b 69 49 6e 5d 29 20 0d 0a 20 20 20 20 20 20 20  [iIn]) ..       
40151 26 26 20 73 71 6c 69 74 65 33 49 73 78 64 69 67  && sqlite3Isxdig
40152 69 74 28 7a 55 72 69 5b 69 49 6e 2b 31 5d 29 20  it(zUri[iIn+1]) 
40153 0d 0a 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20  ..      ){..    
40154 20 20 20 20 69 6e 74 20 6f 63 74 65 74 20 3d 20      int octet = 
40155 28 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e 74  (sqlite3HexToInt
40156 28 7a 55 72 69 5b 69 49 6e 2b 2b 5d 29 20 3c 3c  (zUri[iIn++]) <<
40157 20 34 29 3b 0d 0a 20 20 20 20 20 20 20 20 6f 63   4);..        oc
40158 74 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 48 65  tet += sqlite3He
40159 78 54 6f 49 6e 74 28 7a 55 72 69 5b 69 49 6e 2b  xToInt(zUri[iIn+
4015a 2b 5d 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20  +]);....        
4015b 61 73 73 65 72 74 28 20 6f 63 74 65 74 3e 3d 30  assert( octet>=0
4015c 20 26 26 20 6f 63 74 65 74 3c 32 35 36 20 29 3b   && octet<256 );
4015d 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6f 63  ..        if( oc
4015e 74 65 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  tet==0 ){..     
4015f 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
40160 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65  nch is taken whe
40161 6e 20 22 25 30 30 22 20 61 70 70 65 61 72 73 20  n "%00" appears 
40162 77 69 74 68 69 6e 20 74 68 65 20 55 52 49 2e 20  within the URI. 
40163 49 6e 20 74 68 69 73 0d 0a 20 20 20 20 20 20 20  In this..       
40164 20 20 20 2a 2a 20 63 61 73 65 20 77 65 20 69 67     ** case we ig
40165 6e 6f 72 65 20 61 6c 6c 20 74 65 78 74 20 69 6e  nore all text in
40166 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   the remainder o
40167 66 20 74 68 65 20 70 61 74 68 2c 20 6e 61 6d 65  f the path, name
40168 20 6f 72 0d 0a 20 20 20 20 20 20 20 20 20 20 2a   or..          *
40169 2a 20 76 61 6c 75 65 20 63 75 72 72 65 6e 74 6c  * value currentl
4016a 79 20 62 65 69 6e 67 20 70 61 72 73 65 64 2e 20  y being parsed. 
4016b 53 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 63 75  So ignore the cu
4016c 72 72 65 6e 74 20 63 68 61 72 61 63 74 65 72 0d  rrent character.
4016d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  .          ** an
4016e 64 20 73 6b 69 70 20 74 6f 20 74 68 65 20 6e 65  d skip to the ne
4016f 78 74 20 22 3f 22 2c 20 22 3d 22 20 6f 72 20 22  xt "?", "=" or "
40170 26 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  &", as appropria
40171 74 65 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  te. */..        
40172 20 20 77 68 69 6c 65 28 20 28 63 20 3d 20 7a 55    while( (c = zU
40173 72 69 5b 69 49 6e 5d 29 21 3d 30 20 26 26 20 63  ri[iIn])!=0 && c
40174 21 3d 27 23 27 20 0d 0a 20 20 20 20 20 20 20 20  !='#' ..        
40175 20 20 20 20 20 20 26 26 20 28 65 53 74 61 74 65        && (eState
40176 21 3d 30 20 7c 7c 20 63 21 3d 27 3f 27 29 0d 0a  !=0 || c!='?')..
40177 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
40178 20 28 65 53 74 61 74 65 21 3d 31 20 7c 7c 20 28   (eState!=1 || (
40179 63 21 3d 27 3d 27 20 26 26 20 63 21 3d 27 26 27  c!='=' && c!='&'
4017a 29 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ))..            
4017b 20 20 26 26 20 28 65 53 74 61 74 65 21 3d 32 20    && (eState!=2 
4017c 7c 7c 20 63 21 3d 27 26 27 29 0d 0a 20 20 20 20  || c!='&')..    
4017d 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20        ){..      
4017e 20 20 20 20 20 20 69 49 6e 2b 2b 3b 0d 0a 20 20        iIn++;..  
4017f 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
40180 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a       continue;..
40181 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
40182 20 20 20 63 20 3d 20 6f 63 74 65 74 3b 0d 0a 20     c = octet;.. 
40183 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65       }else if( e
40184 53 74 61 74 65 3d 3d 31 20 26 26 20 28 63 3d 3d  State==1 && (c==
40185 27 26 27 20 7c 7c 20 63 3d 3d 27 3d 27 29 20 29  '&' || c=='=') )
40186 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  {..        if( z
40187 46 69 6c 65 5b 69 4f 75 74 2d 31 5d 3d 3d 30 20  File[iOut-1]==0 
40188 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ){..          /*
40189 20 41 6e 20 65 6d 70 74 79 20 6f 70 74 69 6f 6e   An empty option
4018a 20 6e 61 6d 65 2e 20 49 67 6e 6f 72 65 20 74 68   name. Ignore th
4018b 69 73 20 6f 70 74 69 6f 6e 20 61 6c 74 6f 67 65  is option altoge
4018c 74 68 65 72 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  ther. */..      
4018d 20 20 20 20 77 68 69 6c 65 28 20 7a 55 72 69 5b      while( zUri[
4018e 69 49 6e 5d 20 26 26 20 7a 55 72 69 5b 69 49 6e  iIn] && zUri[iIn
4018f 5d 21 3d 27 23 27 20 26 26 20 7a 55 72 69 5b 69  ]!='#' && zUri[i
40190 49 6e 2d 31 5d 21 3d 27 26 27 20 29 20 69 49 6e  In-1]!='&' ) iIn
40191 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 63  ++;..          c
40192 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20  ontinue;..      
40193 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
40194 20 63 3d 3d 27 26 27 20 29 7b 0d 0a 20 20 20 20   c=='&' ){..    
40195 20 20 20 20 20 20 7a 46 69 6c 65 5b 69 4f 75 74        zFile[iOut
40196 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20  ++] = '\0';..   
40197 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
40198 20 20 20 20 20 20 20 65 53 74 61 74 65 20 3d 20         eState = 
40199 32 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  2;..        }.. 
4019a 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0d 0a 20         c = 0;.. 
4019b 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28       }else if( (
4019c 65 53 74 61 74 65 3d 3d 30 20 26 26 20 63 3d 3d  eState==0 && c==
4019d 27 3f 27 29 20 7c 7c 20 28 65 53 74 61 74 65 3d  '?') || (eState=
4019e 3d 32 20 26 26 20 63 3d 3d 27 26 27 29 20 29 7b  =2 && c=='&') ){
4019f 0d 0a 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b  ..        c = 0;
401a0 0d 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 65  ..        eState
401a1 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a   = 1;..      }..
401a2 20 20 20 20 20 20 7a 46 69 6c 65 5b 69 4f 75 74        zFile[iOut
401a3 2b 2b 5d 20 3d 20 63 3b 0d 0a 20 20 20 20 7d 0d  ++] = c;..    }.
401a4 0a 20 20 20 20 69 66 28 20 65 53 74 61 74 65 3d  .    if( eState=
401a5 3d 31 20 29 20 7a 46 69 6c 65 5b 69 4f 75 74 2b  =1 ) zFile[iOut+
401a6 2b 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20  +] = '\0';..    
401a7 7a 46 69 6c 65 5b 69 4f 75 74 2b 2b 5d 20 3d 20  zFile[iOut++] = 
401a8 27 5c 30 27 3b 0d 0a 20 20 20 20 7a 46 69 6c 65  '\0';..    zFile
401a9 5b 69 4f 75 74 2b 2b 5d 20 3d 20 27 5c 30 27 3b  [iOut++] = '\0';
401aa 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  ....    /* Check
401ab 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20 61   if there were a
401ac 6e 79 20 6f 70 74 69 6f 6e 73 20 73 70 65 63 69  ny options speci
401ad 66 69 65 64 20 74 68 61 74 20 73 68 6f 75 6c 64  fied that should
401ae 20 62 65 20 69 6e 74 65 72 70 72 65 74 65 64 20   be interpreted 
401af 0d 0a 20 20 20 20 2a 2a 20 68 65 72 65 2e 20 4f  ..    ** here. O
401b0 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20  ptions that are 
401b1 69 6e 74 65 72 70 72 65 74 65 64 20 68 65 72 65  interpreted here
401b2 20 69 6e 63 6c 75 64 65 20 22 76 66 73 22 20 61   include "vfs" a
401b3 6e 64 20 74 68 6f 73 65 20 74 68 61 74 0d 0a 20  nd those that.. 
401b4 20 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64     ** correspond
401b5 20 74 6f 20 66 6c 61 67 73 20 74 68 61 74 20 6d   to flags that m
401b6 61 79 20 62 65 20 70 61 73 73 65 64 20 74 6f 20  ay be passed to 
401b7 74 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  the sqlite3_open
401b8 5f 76 32 28 29 0d 0a 20 20 20 20 2a 2a 20 6d 65  _v2()..    ** me
401b9 74 68 6f 64 2e 20 2a 2f 0d 0a 20 20 20 20 7a 4f  thod. */..    zO
401ba 70 74 20 3d 20 26 7a 46 69 6c 65 5b 73 71 6c 69  pt = &zFile[sqli
401bb 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c  te3Strlen30(zFil
401bc 65 29 2b 31 5d 3b 0d 0a 20 20 20 20 77 68 69 6c  e)+1];..    whil
401bd 65 28 20 7a 4f 70 74 5b 30 5d 20 29 7b 0d 0a 20  e( zOpt[0] ){.. 
401be 20 20 20 20 20 69 6e 74 20 6e 4f 70 74 20 3d 20       int nOpt = 
401bf 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
401c0 7a 4f 70 74 29 3b 0d 0a 20 20 20 20 20 20 63 68  zOpt);..      ch
401c1 61 72 20 2a 7a 56 61 6c 20 3d 20 26 7a 4f 70 74  ar *zVal = &zOpt
401c2 5b 6e 4f 70 74 2b 31 5d 3b 0d 0a 20 20 20 20 20  [nOpt+1];..     
401c3 20 69 6e 74 20 6e 56 61 6c 20 3d 20 73 71 6c 69   int nVal = sqli
401c4 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c  te3Strlen30(zVal
401c5 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20  );....      if( 
401c6 6e 4f 70 74 3d 3d 33 20 26 26 20 6d 65 6d 63 6d  nOpt==3 && memcm
401c7 70 28 22 76 66 73 22 2c 20 7a 4f 70 74 2c 20 33  p("vfs", zOpt, 3
401c8 29 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  )==0 ){..       
401c9 20 7a 56 66 73 20 3d 20 7a 56 61 6c 3b 0d 0a 20   zVfs = zVal;.. 
401ca 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
401cb 20 20 20 20 20 73 74 72 75 63 74 20 4f 70 65 6e       struct Open
401cc 4d 6f 64 65 20 7b 0d 0a 20 20 20 20 20 20 20 20  Mode {..        
401cd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
401ce 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ..          int 
401cf 6d 6f 64 65 3b 0d 0a 20 20 20 20 20 20 20 20 7d  mode;..        }
401d0 20 2a 61 4d 6f 64 65 20 3d 20 30 3b 0d 0a 20 20   *aMode = 0;..  
401d1 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 6f 64        char *zMod
401d2 65 54 79 70 65 20 3d 20 30 3b 0d 0a 20 20 20 20  eType = 0;..    
401d3 20 20 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30      int mask = 0
401d4 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  ;..        int l
401d5 69 6d 69 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20  imit = 0;....   
401d6 20 20 20 20 20 69 66 28 20 6e 4f 70 74 3d 3d 35       if( nOpt==5
401d7 20 26 26 20 6d 65 6d 63 6d 70 28 22 63 61 63 68   && memcmp("cach
401d8 65 22 2c 20 7a 4f 70 74 2c 20 35 29 3d 3d 30 20  e", zOpt, 5)==0 
401d9 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 74  ){..          st
401da 61 74 69 63 20 73 74 72 75 63 74 20 4f 70 65 6e  atic struct Open
401db 4d 6f 64 65 20 61 43 61 63 68 65 4d 6f 64 65 5b  Mode aCacheMode[
401dc 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20 20 20  ] = {..         
401dd 20 20 20 7b 20 22 73 68 61 72 65 64 22 2c 20 20     { "shared",  
401de 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
401df 45 44 43 41 43 48 45 20 7d 2c 0d 0a 20 20 20 20  EDCACHE },..    
401e0 20 20 20 20 20 20 20 20 7b 20 22 70 72 69 76 61          { "priva
401e1 74 65 22 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  te", SQLITE_OPEN
401e2 5f 50 52 49 56 41 54 45 43 41 43 48 45 20 7d 2c  _PRIVATECACHE },
401e3 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ..            { 
401e4 30 2c 20 30 20 7d 0d 0a 20 20 20 20 20 20 20 20  0, 0 }..        
401e5 20 20 7d 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20    };....        
401e6 20 20 6d 61 73 6b 20 3d 20 53 51 4c 49 54 45 5f    mask = SQLITE_
401e7 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45  OPEN_SHAREDCACHE
401e8 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49  |SQLITE_OPEN_PRI
401e9 56 41 54 45 43 41 43 48 45 3b 0d 0a 20 20 20 20  VATECACHE;..    
401ea 20 20 20 20 20 20 61 4d 6f 64 65 20 3d 20 61 43        aMode = aC
401eb 61 63 68 65 4d 6f 64 65 3b 0d 0a 20 20 20 20 20  acheMode;..     
401ec 20 20 20 20 20 6c 69 6d 69 74 20 3d 20 6d 61 73       limit = mas
401ed 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 4d  k;..          zM
401ee 6f 64 65 54 79 70 65 20 3d 20 22 63 61 63 68 65  odeType = "cache
401ef 22 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  ";..        }.. 
401f0 20 20 20 20 20 20 20 69 66 28 20 6e 4f 70 74 3d         if( nOpt=
401f1 3d 34 20 26 26 20 6d 65 6d 63 6d 70 28 22 6d 6f  =4 && memcmp("mo
401f2 64 65 22 2c 20 7a 4f 70 74 2c 20 34 29 3d 3d 30  de", zOpt, 4)==0
401f3 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73   ){..          s
401f4 74 61 74 69 63 20 73 74 72 75 63 74 20 4f 70 65  tatic struct Ope
401f5 6e 4d 6f 64 65 20 61 4f 70 65 6e 4d 6f 64 65 5b  nMode aOpenMode[
401f6 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20 20 20 20  ] = {..         
401f7 20 20 20 7b 20 22 72 6f 22 2c 20 20 53 51 4c 49     { "ro",  SQLI
401f8 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
401f9 20 7d 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20   },..           
401fa 20 7b 20 22 72 77 22 2c 20 20 53 51 4c 49 54 45   { "rw",  SQLITE
401fb 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
401fc 7d 2c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  }, ..           
401fd 20 7b 20 22 72 77 63 22 2c 20 53 51 4c 49 54 45   { "rwc", SQLITE
401fe 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
401ff 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  | SQLITE_OPEN_CR
40200 45 41 54 45 20 7d 2c 0d 0a 20 20 20 20 20 20 20  EATE },..       
40201 20 20 20 20 20 7b 20 30 2c 20 30 20 7d 0d 0a 20       { 0, 0 }.. 
40202 20 20 20 20 20 20 20 20 20 7d 3b 0d 0a 0d 0a 20           };.... 
40203 20 20 20 20 20 20 20 20 20 6d 61 73 6b 20 3d 20           mask = 
40204 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
40205 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  ONLY|SQLITE_OPEN
40206 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
40207 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 3b 0d 0a  E_OPEN_CREATE;..
40208 20 20 20 20 20 20 20 20 20 20 61 4d 6f 64 65 20            aMode 
40209 3d 20 61 4f 70 65 6e 4d 6f 64 65 3b 0d 0a 20 20  = aOpenMode;..  
4020a 20 20 20 20 20 20 20 20 6c 69 6d 69 74 20 3d 20          limit = 
4020b 6d 61 73 6b 20 26 20 66 6c 61 67 73 3b 0d 0a 20  mask & flags;.. 
4020c 20 20 20 20 20 20 20 20 20 7a 4d 6f 64 65 54 79           zModeTy
4020d 70 65 20 3d 20 22 61 63 63 65 73 73 22 3b 0d 0a  pe = "access";..
4020e 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20          }....   
4020f 20 20 20 20 20 69 66 28 20 61 4d 6f 64 65 20 29       if( aMode )
40210 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  {..          int
40211 20 69 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69   i;..          i
40212 6e 74 20 6d 6f 64 65 20 3d 20 30 3b 0d 0a 20 20  nt mode = 0;..  
40213 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
40214 20 61 4d 6f 64 65 5b 69 5d 2e 7a 3b 20 69 2b 2b   aMode[i].z; i++
40215 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
40216 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
40217 61 4d 6f 64 65 5b 69 5d 2e 7a 3b 0d 0a 20 20 20  aMode[i].z;..   
40218 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 56 61           if( nVa
40219 6c 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  l==sqlite3Strlen
4021a 33 30 28 7a 29 20 26 26 20 30 3d 3d 6d 65 6d 63  30(z) && 0==memc
4021b 6d 70 28 7a 56 61 6c 2c 20 7a 2c 20 6e 56 61 6c  mp(zVal, z, nVal
4021c 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) ){..          
4021d 20 20 20 20 6d 6f 64 65 20 3d 20 61 4d 6f 64 65      mode = aMode
4021e 5b 69 5d 2e 6d 6f 64 65 3b 0d 0a 20 20 20 20 20  [i].mode;..     
4021f 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d           break;.
40220 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  .            }..
40221 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
40222 20 20 20 20 20 20 20 69 66 28 20 6d 6f 64 65 3d         if( mode=
40223 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  =0 ){..         
40224 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
40225 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
40226 6e 6f 20 73 75 63 68 20 25 73 20 6d 6f 64 65 3a  no such %s mode:
40227 20 25 73 22 2c 20 7a 4d 6f 64 65 54 79 70 65 2c   %s", zModeType,
40228 20 7a 56 61 6c 29 3b 0d 0a 20 20 20 20 20 20 20   zVal);..       
40229 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
4022a 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20  _ERROR;..       
4022b 20 20 20 20 20 67 6f 74 6f 20 70 61 72 73 65 5f       goto parse_
4022c 75 72 69 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20  uri_out;..      
4022d 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20      }..         
4022e 20 69 66 28 20 6d 6f 64 65 3e 6c 69 6d 69 74 20   if( mode>limit 
4022f 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
40230 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  *pzErrMsg = sqli
40231 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 20  te3_mprintf("%s 
40232 6d 6f 64 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  mode not allowed
40233 3a 20 25 73 22 2c 0d 0a 20 20 20 20 20 20 20 20  : %s",..        
40234 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40235 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40236 7a 4d 6f 64 65 54 79 70 65 2c 20 7a 56 61 6c 29  zModeType, zVal)
40237 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ;..            r
40238 63 20 3d 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b  c = SQLITE_PERM;
40239 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  ..            go
4023a 74 6f 20 70 61 72 73 65 5f 75 72 69 5f 6f 75 74  to parse_uri_out
4023b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
4023c 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20            flags 
4023d 3d 20 28 66 6c 61 67 73 20 26 20 7e 6d 61 73 6b  = (flags & ~mask
4023e 29 20 7c 20 6d 6f 64 65 3b 0d 0a 20 20 20 20 20  ) | mode;..     
4023f 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d     }..      }...
40240 0a 20 20 20 20 20 20 7a 4f 70 74 20 3d 20 26 7a  .      zOpt = &z
40241 56 61 6c 5b 6e 56 61 6c 2b 31 5d 3b 0d 0a 20 20  Val[nVal+1];..  
40242 20 20 7d 0d 0a 0d 0a 20 20 7d 65 6c 73 65 7b 0d    }....  }else{.
40243 0a 20 20 20 20 7a 46 69 6c 65 20 3d 20 73 71 6c  .    zFile = sql
40244 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 55 72 69  ite3_malloc(nUri
40245 2b 32 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a  +2);..    if( !z
40246 46 69 6c 65 20 29 20 72 65 74 75 72 6e 20 53 51  File ) return SQ
40247 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
40248 20 6d 65 6d 63 70 79 28 7a 46 69 6c 65 2c 20 7a   memcpy(zFile, z
40249 55 72 69 2c 20 6e 55 72 69 29 3b 0d 0a 20 20 20  Uri, nUri);..   
4024a 20 7a 46 69 6c 65 5b 6e 55 72 69 5d 20 3d 20 27   zFile[nUri] = '
4024b 5c 30 27 3b 0d 0a 20 20 20 20 7a 46 69 6c 65 5b  \0';..    zFile[
4024c 6e 55 72 69 2b 31 5d 20 3d 20 27 5c 30 27 3b 0d  nUri+1] = '\0';.
4024d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 70 56 66 73  .  }....  *ppVfs
4024e 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66   = sqlite3_vfs_f
4024f 69 6e 64 28 7a 56 66 73 29 3b 0d 0a 20 20 69 66  ind(zVfs);..  if
40250 28 20 2a 70 70 56 66 73 3d 3d 30 20 29 7b 0d 0a  ( *ppVfs==0 ){..
40251 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
40252 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
40253 22 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73  "no such vfs: %s
40254 22 2c 20 7a 56 66 73 29 3b 0d 0a 20 20 20 20 72  ", zVfs);..    r
40255 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
40256 3b 0d 0a 20 20 7d 0d 0a 20 70 61 72 73 65 5f 75  ;..  }.. parse_u
40257 72 69 5f 6f 75 74 3a 0d 0a 20 20 69 66 28 20 72  ri_out:..  if( r
40258 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
40259 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
4025a 65 28 7a 46 69 6c 65 29 3b 0d 0a 20 20 20 20 7a  e(zFile);..    z
4025b 46 69 6c 65 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  File = 0;..  }..
4025c 20 20 2a 70 46 6c 61 67 73 20 3d 20 66 6c 61 67    *pFlags = flag
4025d 73 3b 0d 0a 20 20 2a 70 7a 46 69 6c 65 20 3d 20  s;..  *pzFile = 
4025e 7a 46 69 6c 65 3b 0d 0a 20 20 72 65 74 75 72 6e  zFile;..  return
4025f 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d   rc;..}....../*.
40260 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
40261 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f   does the work o
40262 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61 74 61  f opening a data
40263 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66 20 6f  base on behalf o
40264 66 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  f..** sqlite3_op
40265 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  en() and sqlite3
40266 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20 64  _open16(). The d
40267 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
40268 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0d 0a   "zFilename"  ..
40269 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f  ** is UTF-8 enco
4026a 64 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ded...*/..static
4026b 20 69 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73   int openDatabas
4026c 65 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  e(..  const char
4026d 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20   *zFilename, /* 
4026e 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  Database filenam
4026f 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  e UTF-8 encoded 
40270 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  */..  sqlite3 **
40271 70 70 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20  ppDb,        /* 
40272 4f 55 54 3a 20 52 65 74 75 72 6e 65 64 20 64 61  OUT: Returned da
40273 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
40274 0d 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ..  unsigned int
40275 20 66 6c 61 67 73 2c 20 20 20 20 2f 2a 20 4f 70   flags,    /* Op
40276 65 72 61 74 69 6f 6e 61 6c 20 66 6c 61 67 73 20  erational flags 
40277 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
40278 20 2a 7a 56 66 73 20 20 20 20 20 20 20 2f 2a 20   *zVfs       /* 
40279 4e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20  Name of the VFS 
4027a 74 6f 20 75 73 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  to use */..){.. 
4027b 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
4027c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4027d 20 2f 2a 20 53 74 6f 72 65 20 61 6c 6c 6f 63 61   /* Store alloca
4027e 74 65 64 20 68 61 6e 64 6c 65 20 68 65 72 65 20  ted handle here 
4027f 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  */..  int rc;   
40280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40281 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
40282 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  code */..  int i
40283 73 54 68 72 65 61 64 73 61 66 65 3b 20 20 20 20  sThreadsafe;    
40284 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
40285 75 65 20 66 6f 72 20 74 68 72 65 61 64 73 61 66  ue for threadsaf
40286 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f  e connections */
40287 0d 0a 20 20 63 68 61 72 20 2a 7a 4f 70 65 6e 20  ..  char *zOpen 
40288 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
40289 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20      /* Filename 
4028a 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73  argument to pass
4028b 20 74 6f 20 42 74 72 65 65 4f 70 65 6e 28 29 20   to BtreeOpen() 
4028c 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  */..  char *zErr
4028d 4d 73 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Msg = 0;        
4028e 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
4028f 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c 69  essage from sqli
40290 74 65 33 50 61 72 73 65 55 72 69 28 29 20 2a 2f  te3ParseUri() */
40291 0d 0a 0d 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b  ....  *ppDb = 0;
40292 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
40293 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a  _OMIT_AUTOINIT..
40294 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
40295 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0d 0a 20 20  nitialize();..  
40296 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
40297 72 63 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20  rc;..#endif.... 
40298 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 73   /* Only allow s
40299 65 6e 73 69 62 6c 65 20 63 6f 6d 62 69 6e 61 74  ensible combinat
4029a 69 6f 6e 73 20 6f 66 20 62 69 74 73 20 69 6e 20  ions of bits in 
4029b 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  the flags argume
4029c 6e 74 2e 20 20 0d 0a 20 20 2a 2a 20 54 68 72 6f  nt.  ..  ** Thro
4029d 77 20 61 6e 20 65 72 72 6f 72 20 69 66 20 61 6e  w an error if an
4029e 79 20 6e 6f 6e 2d 73 65 6e 73 65 20 63 6f 6d 62  y non-sense comb
4029f 69 6e 61 74 69 6f 6e 20 69 73 20 75 73 65 64 2e  ination is used.
402a0 20 20 49 66 20 77 65 0d 0a 20 20 2a 2a 20 64 6f    If we..  ** do
402a1 20 6e 6f 74 20 62 6c 6f 63 6b 20 69 6c 6c 65 67   not block illeg
402a2 61 6c 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20  al combinations 
402a3 68 65 72 65 2c 20 69 74 20 63 6f 75 6c 64 20 74  here, it could t
402a4 72 69 67 67 65 72 0d 0a 20 20 2a 2a 20 61 73 73  rigger..  ** ass
402a5 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
402a6 20 69 6e 20 64 65 65 70 65 72 20 6c 61 79 65 72   in deeper layer
402a7 73 2e 20 20 53 65 6e 73 69 62 6c 65 20 63 6f 6d  s.  Sensible com
402a8 62 69 6e 61 74 69 6f 6e 73 0d 0a 20 20 2a 2a 20  binations..  ** 
402a9 61 72 65 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  are:..  **..  **
402aa 20 20 31 3a 20 20 53 51 4c 49 54 45 5f 4f 50 45    1:  SQLITE_OPE
402ab 4e 5f 52 45 41 44 4f 4e 4c 59 0d 0a 20 20 2a 2a  N_READONLY..  **
402ac 20 20 32 3a 20 20 53 51 4c 49 54 45 5f 4f 50 45    2:  SQLITE_OPE
402ad 4e 5f 52 45 41 44 57 52 49 54 45 0d 0a 20 20 2a  N_READWRITE..  *
402ae 2a 20 20 36 3a 20 20 53 51 4c 49 54 45 5f 4f 50  *  6:  SQLITE_OP
402af 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
402b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
402b1 45 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73 73 65 72  E..  */..  asser
402b2 74 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  t( SQLITE_OPEN_R
402b3 45 41 44 4f 4e 4c 59 20 20 3d 3d 20 30 78 30 31  EADONLY  == 0x01
402b4 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 53   );..  assert( S
402b5 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
402b6 52 49 54 45 20 3d 3d 20 30 78 30 32 20 29 3b 0d  RITE == 0x02 );.
402b7 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54  .  assert( SQLIT
402b8 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20  E_OPEN_CREATE   
402b9 20 3d 3d 20 30 78 30 34 20 29 3b 0d 0a 20 20 74   == 0x04 );..  t
402ba 65 73 74 63 61 73 65 28 20 28 31 3c 3c 28 66 6c  estcase( (1<<(fl
402bb 61 67 73 26 37 29 29 3d 3d 30 78 30 32 20 29 3b  ags&7))==0x02 );
402bc 20 2f 2a 20 52 45 41 44 4f 4e 4c 59 20 2a 2f 0d   /* READONLY */.
402bd 0a 20 20 74 65 73 74 63 61 73 65 28 20 28 31 3c  .  testcase( (1<
402be 3c 28 66 6c 61 67 73 26 37 29 29 3d 3d 30 78 30  <(flags&7))==0x0
402bf 34 20 29 3b 20 2f 2a 20 52 45 41 44 57 52 49 54  4 ); /* READWRIT
402c0 45 20 2a 2f 0d 0a 20 20 74 65 73 74 63 61 73 65  E */..  testcase
402c1 28 20 28 31 3c 3c 28 66 6c 61 67 73 26 37 29 29  ( (1<<(flags&7))
402c2 3d 3d 30 78 34 30 20 29 3b 20 2f 2a 20 52 45 41  ==0x40 ); /* REA
402c3 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 20  DWRITE | CREATE 
402c4 2a 2f 0d 0a 20 20 69 66 28 20 28 28 31 3c 3c 28  */..  if( ((1<<(
402c5 66 6c 61 67 73 26 37 29 29 20 26 20 30 78 34 36  flags&7)) & 0x46
402c6 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  )==0 ) return SQ
402c7 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54  LITE_MISUSE_BKPT
402c8 3b 0d 0a 0d 0a 20 20 69 66 28 20 73 71 6c 69 74  ;....  if( sqlit
402c9 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
402ca 43 6f 72 65 4d 75 74 65 78 3d 3d 30 20 29 7b 0d  CoreMutex==0 ){.
402cb 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66  .    isThreadsaf
402cc 65 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 20  e = 0;..  }else 
402cd 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
402ce 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20  TE_OPEN_NOMUTEX 
402cf 29 7b 0d 0a 20 20 20 20 69 73 54 68 72 65 61 64  ){..    isThread
402d0 73 61 66 65 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  safe = 0;..  }el
402d1 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  se if( flags & S
402d2 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
402d3 55 54 45 58 20 29 7b 0d 0a 20 20 20 20 69 73 54  UTEX ){..    isT
402d4 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b 0d 0a  hreadsafe = 1;..
402d5 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 73    }else{..    is
402d6 54 68 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c  Threadsafe = sql
402d7 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
402d8 2e 62 46 75 6c 6c 4d 75 74 65 78 3b 0d 0a 20 20  .bFullMutex;..  
402d9 7d 0d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  }..  if( flags &
402da 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49   SQLITE_OPEN_PRI
402db 56 41 54 45 43 41 43 48 45 20 29 7b 0d 0a 20 20  VATECACHE ){..  
402dc 20 20 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49    flags &= ~SQLI
402dd 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
402de 43 48 45 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  CHE;..  }else if
402df 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
402e0 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68  onfig.sharedCach
402e1 65 45 6e 61 62 6c 65 64 20 29 7b 0d 0a 20 20 20  eEnabled ){..   
402e2 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45   flags |= SQLITE
402e3 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
402e4 45 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  E;..  }....  /* 
402e5 52 65 6d 6f 76 65 20 68 61 72 6d 66 75 6c 20 62  Remove harmful b
402e6 69 74 73 20 66 72 6f 6d 20 74 68 65 20 66 6c 61  its from the fla
402e7 67 73 20 70 61 72 61 6d 65 74 65 72 0d 0a 20 20  gs parameter..  
402e8 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 53 51 4c  **..  ** The SQL
402e9 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
402ea 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 50 45 4e   and SQLITE_OPEN
402eb 5f 46 55 4c 4c 4d 55 54 45 58 20 66 6c 61 67 73  _FULLMUTEX flags
402ec 20 77 65 72 65 0d 0a 20 20 2a 2a 20 64 65 61 6c   were..  ** deal
402ed 74 20 77 69 74 68 20 69 6e 20 74 68 65 20 70 72  t with in the pr
402ee 65 76 69 6f 75 73 20 63 6f 64 65 20 62 6c 6f 63  evious code bloc
402ef 6b 2e 20 20 42 65 73 69 64 65 73 20 74 68 65 73  k.  Besides thes
402f0 65 2c 20 74 68 65 20 6f 6e 6c 79 0d 0a 20 20 2a  e, the only..  *
402f1 2a 20 76 61 6c 69 64 20 69 6e 70 75 74 20 66 6c  * valid input fl
402f2 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  ags for sqlite3_
402f3 6f 70 65 6e 5f 76 32 28 29 20 61 72 65 20 53 51  open_v2() are SQ
402f4 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
402f5 4c 59 2c 0d 0a 20 20 2a 2a 20 53 51 4c 49 54 45  LY,..  ** SQLITE
402f6 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 2c  _OPEN_READWRITE,
402f7 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
402f8 41 54 45 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  ATE, SQLITE_OPEN
402f9 5f 53 48 41 52 45 44 43 41 43 48 45 2c 0d 0a 20  _SHAREDCACHE,.. 
402fa 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   ** SQLITE_OPEN_
402fb 50 52 49 56 41 54 45 43 41 43 48 45 2c 20 61 6e  PRIVATECACHE, an
402fc 64 20 73 6f 6d 65 20 72 65 73 65 72 76 65 64 20  d some reserved 
402fd 62 69 74 73 2e 20 20 53 69 6c 65 6e 74 6c 79 20  bits.  Silently 
402fe 6d 61 73 6b 0d 0a 20 20 2a 2a 20 6f 66 66 20 61  mask..  ** off a
402ff 6c 6c 20 6f 74 68 65 72 20 66 6c 61 67 73 2e 0d  ll other flags..
40300 0a 20 20 2a 2f 0d 0a 20 20 66 6c 61 67 73 20 26  .  */..  flags &
40301 3d 20 20 7e 28 20 53 51 4c 49 54 45 5f 4f 50 45  =  ~( SQLITE_OPE
40302 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
40303 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  |..             
40304 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
40305 43 4c 55 53 49 56 45 20 7c 0d 0a 20 20 20 20 20  CLUSIVE |..     
40306 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
40307 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 0d  _OPEN_MAIN_DB |.
40308 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
40309 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
4030a 5f 44 42 20 7c 20 0d 0a 20 20 20 20 20 20 20 20  _DB | ..        
4030b 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
4030c 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20  EN_TRANSIENT_DB 
4030d 7c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  | ..            
4030e 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d     SQLITE_OPEN_M
4030f 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0d 0a  AIN_JOURNAL | ..
40310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
40311 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
40312 4a 4f 55 52 4e 41 4c 20 7c 20 0d 0a 20 20 20 20  JOURNAL | ..    
40313 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
40314 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
40315 4c 20 7c 20 0d 0a 20 20 20 20 20 20 20 20 20 20  L | ..          
40316 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
40317 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
40318 7c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  |..             
40319 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f    SQLITE_OPEN_NO
4031a 4d 55 54 45 58 20 7c 0d 0a 20 20 20 20 20 20 20  MUTEX |..       
4031b 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
4031c 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 7c 0d  PEN_FULLMUTEX |.
4031d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
4031e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 0d  SQLITE_OPEN_WAL.
4031f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b  .             );
40320 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ....  /* Allocat
40321 65 20 74 68 65 20 73 71 6c 69 74 65 20 64 61 74  e the sqlite dat
40322 61 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a  a structure */..
40323 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 4d 61    db = sqlite3Ma
40324 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
40325 28 73 71 6c 69 74 65 33 29 20 29 3b 0d 0a 20 20  (sqlite3) );..  
40326 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f  if( db==0 ) goto
40327 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0d 0a 20 20   opendb_out;..  
40328 69 66 28 20 69 73 54 68 72 65 61 64 73 61 66 65  if( isThreadsafe
40329 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d 75 74   ){..    db->mut
4032a 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
4032b 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
4032c 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0d  TEX_RECURSIVE);.
4032d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 75 74  .    if( db->mut
4032e 65 78 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ex==0 ){..      
4032f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29  sqlite3_free(db)
40330 3b 0d 0a 20 20 20 20 20 20 64 62 20 3d 20 30 3b  ;..      db = 0;
40331 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65  ..      goto ope
40332 6e 64 62 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d  ndb_out;..    }.
40333 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
40334 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
40335 6d 75 74 65 78 29 3b 0d 0a 20 20 64 62 2d 3e 65  mutex);..  db->e
40336 72 72 4d 61 73 6b 20 3d 20 30 78 66 66 3b 0d 0a  rrMask = 0xff;..
40337 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0d 0a    db->nDb = 2;..
40338 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
40339 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b  LITE_MAGIC_BUSY;
4033a 0d 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62  ..  db->aDb = db
4033b 2d 3e 61 44 62 53 74 61 74 69 63 3b 0d 0a 0d 0a  ->aDbStatic;....
4033c 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
4033d 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73 69  (db->aLimit)==si
4033e 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69 74 29  zeof(aHardLimit)
4033f 20 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 64 62   );..  memcpy(db
40340 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c  ->aLimit, aHardL
40341 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d  imit, sizeof(db-
40342 3e 61 4c 69 6d 69 74 29 29 3b 0d 0a 20 20 64 62  >aLimit));..  db
40343 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
40344 3b 0d 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74  ;..  db->nextAut
40345 6f 76 61 63 20 3d 20 2d 31 3b 0d 0a 20 20 64 62  ovac = -1;..  db
40346 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d  ->nextPagesize =
40347 20 30 3b 0d 0a 20 20 64 62 2d 3e 66 6c 61 67 73   0;..  db->flags
40348 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74   |= SQLITE_Short
40349 43 6f 6c 4e 61 6d 65 73 20 7c 20 53 51 4c 49 54  ColNames | SQLIT
4034a 45 5f 41 75 74 6f 49 6e 64 65 78 20 7c 20 53 51  E_AutoIndex | SQ
4034b 4c 49 54 45 5f 45 6e 61 62 6c 65 54 72 69 67 67  LITE_EnableTrigg
4034c 65 72 0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 44  er..#if SQLITE_D
4034d 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d  EFAULT_FILE_FORM
4034e 41 54 3c 34 0d 0a 20 20 20 20 20 20 20 20 20 20  AT<4..          
4034f 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
40350 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0d 0a 23  LegacyFileFmt..#
40351 65 6e 64 69 66 0d 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
40352 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44  LITE_ENABLE_LOAD
40353 5f 45 58 54 45 4e 53 49 4f 4e 0d 0a 20 20 20 20  _EXTENSION..    
40354 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 53               | S
40355 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73  QLITE_LoadExtens
40356 69 6f 6e 0d 0a 23 65 6e 64 69 66 0d 0a 23 69 66  ion..#endif..#if
40357 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
40358 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45  RECURSIVE_TRIGGE
40359 52 53 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  RS..            
4035a 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 52 65       | SQLITE_Re
4035b 63 54 72 69 67 67 65 72 73 0d 0a 23 65 6e 64 69  cTriggers..#endi
4035c 66 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  f..#if defined(S
4035d 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 4f  QLITE_DEFAULT_FO
4035e 52 45 49 47 4e 5f 4b 45 59 53 29 20 26 26 20 53  REIGN_KEYS) && S
4035f 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 4f  QLITE_DEFAULT_FO
40360 52 45 49 47 4e 5f 4b 45 59 53 0d 0a 20 20 20 20  REIGN_KEYS..    
40361 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 53               | S
40362 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
40363 73 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20 20  s..#endif..     
40364 20 3b 0d 0a 20 20 73 71 6c 69 74 65 33 48 61 73   ;..  sqlite3Has
40365 68 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  hInit(&db->aColl
40366 53 65 71 29 3b 0d 0a 23 69 66 6e 64 65 66 20 53  Seq);..#ifndef S
40367 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
40368 41 4c 54 41 42 4c 45 0d 0a 20 20 73 71 6c 69 74  ALTABLE..  sqlit
40369 65 33 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e  e3HashInit(&db->
4036a 61 4d 6f 64 75 6c 65 29 3b 0d 0a 23 65 6e 64 69  aModule);..#endi
4036b 66 0d 0a 0d 0a 20 20 2f 2a 20 41 64 64 20 74 68  f....  /* Add th
4036c 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
4036d 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e  ion sequence BIN
4036e 41 52 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b  ARY. BINARY work
4036f 73 20 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38  s for both UTF-8
40370 0d 0a 20 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31  ..  ** and UTF-1
40371 36 2c 20 73 6f 20 61 64 64 20 61 20 76 65 72 73  6, so add a vers
40372 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 6f 20  ion for each to 
40373 61 76 6f 69 64 20 61 6e 79 20 75 6e 6e 65 63 65  avoid any unnece
40374 73 73 61 72 79 0d 0a 20 20 2a 2a 20 63 6f 6e 76  ssary..  ** conv
40375 65 72 73 69 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c  ersions. The onl
40376 79 20 65 72 72 6f 72 20 74 68 61 74 20 63 61 6e  y error that can
40377 20 6f 63 63 75 72 20 68 65 72 65 20 69 73 20 61   occur here is a
40378 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
40379 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 63 72 65 61  e...  */..  crea
4037a 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20  teCollation(db, 
4037b 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45  "BINARY", SQLITE
4037c 5f 55 54 46 38 2c 20 30 2c 20 62 69 6e 43 6f 6c  _UTF8, 0, binCol
4037d 6c 46 75 6e 63 2c 20 30 29 3b 0d 0a 20 20 63 72  lFunc, 0);..  cr
4037e 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
4037f 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49  , "BINARY", SQLI
40380 54 45 5f 55 54 46 31 36 42 45 2c 20 30 2c 20 62  TE_UTF16BE, 0, b
40381 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0d  inCollFunc, 0);.
40382 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69  .  createCollati
40383 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c  on(db, "BINARY",
40384 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
40385 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c   0, binCollFunc,
40386 20 30 29 3b 0d 0a 20 20 63 72 65 61 74 65 43 6f   0);..  createCo
40387 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52 54 52  llation(db, "RTR
40388 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  IM", SQLITE_UTF8
40389 2c 20 28 76 6f 69 64 2a 29 31 2c 20 62 69 6e 43  , (void*)1, binC
4038a 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0d 0a 20 20  ollFunc, 0);..  
4038b 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
4038c 69 6c 65 64 20 29 7b 0d 0a 20 20 20 20 67 6f 74  iled ){..    got
4038d 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0d 0a 20  o opendb_out;.. 
4038e 20 7d 0d 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43   }..  db->pDfltC
4038f 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
40390 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
40391 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52  ITE_UTF8, "BINAR
40392 59 22 2c 20 30 29 3b 0d 0a 20 20 61 73 73 65 72  Y", 0);..  asser
40393 74 28 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  t( db->pDfltColl
40394 21 3d 30 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41  !=0 );....  /* A
40395 6c 73 6f 20 61 64 64 20 61 20 55 54 46 2d 38 20  lso add a UTF-8 
40396 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65  case-insensitive
40397 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
40398 6e 63 65 2e 20 2a 2f 0d 0a 20 20 63 72 65 61 74  nce. */..  creat
40399 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
4039a 4e 4f 43 41 53 45 22 2c 20 53 51 4c 49 54 45 5f  NOCASE", SQLITE_
4039b 55 54 46 38 2c 20 30 2c 20 6e 6f 63 61 73 65 43  UTF8, 0, nocaseC
4039c 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 2c 20 30 29  ollatingFunc, 0)
4039d 3b 0d 0a 0d 0a 20 20 2f 2a 20 50 61 72 73 65 20  ;....  /* Parse 
4039e 74 68 65 20 66 69 6c 65 6e 61 6d 65 2f 55 52 49  the filename/URI
4039f 20 61 72 67 75 6d 65 6e 74 2e 20 2a 2f 0d 0a 20   argument. */.. 
403a0 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d   db->openFlags =
403a1 20 66 6c 61 67 73 3b 0d 0a 20 20 72 63 20 3d 20   flags;..  rc = 
403a2 73 71 6c 69 74 65 33 50 61 72 73 65 55 72 69 28  sqlite3ParseUri(
403a3 7a 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  zVfs, zFilename,
403a4 20 26 66 6c 61 67 73 2c 20 26 64 62 2d 3e 70 56   &flags, &db->pV
403a5 66 73 2c 20 26 7a 4f 70 65 6e 2c 20 26 7a 45 72  fs, &zOpen, &zEr
403a6 72 4d 73 67 29 3b 0d 0a 20 20 69 66 28 20 72 63  rMsg);..  if( rc
403a7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
403a8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
403a9 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d  TE_NOMEM ) db->m
403aa 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
403ab 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
403ac 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 72 4d  or(db, rc, zErrM
403ad 73 67 20 3f 20 22 25 73 22 20 3a 20 30 2c 20 7a  sg ? "%s" : 0, z
403ae 45 72 72 4d 73 67 29 3b 0d 0a 20 20 20 20 73 71  ErrMsg);..    sq
403af 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d  lite3_free(zErrM
403b0 73 67 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 6f  sg);..    goto o
403b1 70 65 6e 64 62 5f 6f 75 74 3b 0d 0a 20 20 7d 0d  pendb_out;..  }.
403b2 0a 0d 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65  ...  /* Open the
403b3 20 62 61 63 6b 65 6e 64 20 64 61 74 61 62 61 73   backend databas
403b4 65 20 64 72 69 76 65 72 20 2a 2f 0d 0a 20 20 72  e driver */..  r
403b5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
403b6 4f 70 65 6e 28 64 62 2d 3e 70 56 66 73 2c 20 7a  Open(db->pVfs, z
403b7 4f 70 65 6e 2c 20 64 62 2c 20 26 64 62 2d 3e 61  Open, db, &db->a
403b8 44 62 5b 30 5d 2e 70 42 74 2c 20 30 2c 0d 0a 20  Db[0].pBt, 0,.. 
403b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
403ba 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 20 53         flags | S
403bb 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
403bc 44 42 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d  DB);..  if( rc!=
403bd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
403be 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
403bf 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0d  _IOERR_NOMEM ){.
403c0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
403c1 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d  TE_NOMEM;..    }
403c2 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ..    sqlite3Err
403c3 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0d 0a  or(db, rc, 0);..
403c4 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
403c5 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d  out;..  }..  db-
403c6 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 20  >aDb[0].pSchema 
403c7 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47  = sqlite3SchemaG
403c8 65 74 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 30  et(db, db->aDb[0
403c9 5d 2e 70 42 74 29 3b 0d 0a 20 20 64 62 2d 3e 61  ].pBt);..  db->a
403ca 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 3d 20  Db[1].pSchema = 
403cb 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
403cc 28 64 62 2c 20 30 29 3b 0d 0a 0d 0a 0d 0a 20 20  (db, 0);......  
403cd 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73  /* The default s
403ce 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20  afety_level for 
403cf 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
403d0 65 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72  e is 'full'; for
403d1 20 74 68 65 20 74 65 6d 70 0d 0a 20 20 2a 2a 20   the temp..  ** 
403d2 64 61 74 61 62 61 73 65 20 69 74 20 69 73 20 27  database it is '
403d3 4e 4f 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63  NONE'. This matc
403d4 68 65 73 20 74 68 65 20 70 61 67 65 72 20 6c 61  hes the pager la
403d5 79 65 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0d  yer defaults.  .
403d6 0a 20 20 2a 2f 0d 0a 20 20 64 62 2d 3e 61 44 62  .  */..  db->aDb
403d7 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61 69  [0].zName = "mai
403d8 6e 22 3b 0d 0a 20 20 64 62 2d 3e 61 44 62 5b 30  n";..  db->aDb[0
403d9 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ].safety_level =
403da 20 33 3b 0d 0a 20 20 64 62 2d 3e 61 44 62 5b 31   3;..  db->aDb[1
403db 5d 2e 7a 4e 61 6d 65 20 3d 20 22 74 65 6d 70 22  ].zName = "temp"
403dc 3b 0d 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  ;..  db->aDb[1].
403dd 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 31  safety_level = 1
403de 3b 0d 0a 0d 0a 20 20 64 62 2d 3e 6d 61 67 69 63  ;....  db->magic
403df 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
403e0 4f 50 45 4e 3b 0d 0a 20 20 69 66 28 20 64 62 2d  OPEN;..  if( db-
403e1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
403e2 0d 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  ..    goto opend
403e3 62 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  b_out;..  }.... 
403e4 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c   /* Register all
403e5 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
403e6 6f 6e 73 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20  ons, but do not 
403e7 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20  attempt to read 
403e8 74 68 65 0d 0a 20 20 2a 2a 20 64 61 74 61 62 61  the..  ** databa
403e9 73 65 20 73 63 68 65 6d 61 20 79 65 74 2e 20 54  se schema yet. T
403ea 68 69 73 20 69 73 20 64 65 6c 61 79 65 64 20 75  his is delayed u
403eb 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 74  ntil the first t
403ec 69 6d 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ime the database
403ed 0d 0a 20 20 2a 2a 20 69 73 20 61 63 63 65 73 73  ..  ** is access
403ee 65 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c  ed...  */..  sql
403ef 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
403f0 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0d 0a 20 20  LITE_OK, 0);..  
403f1 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42  sqlite3RegisterB
403f2 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28  uiltinFunctions(
403f3 64 62 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 61  db);....  /* Loa
403f4 64 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  d automatic exte
403f5 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65 6e 73 69  nsions - extensi
403f6 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 62 65  ons that have be
403f7 65 6e 20 72 65 67 69 73 74 65 72 65 64 0d 0a 20  en registered.. 
403f8 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71   ** using the sq
403f9 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69 63 5f  lite3_automatic_
403fa 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50 49 2e  extension() API.
403fb 0d 0a 20 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 73  ..  */..  rc = s
403fc 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
403fd 62 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  b);..  if( rc==S
403fe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
403ff 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64   sqlite3AutoLoad
40400 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0d  Extensions(db);.
40401 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
40402 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0d 0a  3_errcode(db);..
40403 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
40404 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
40405 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
40406 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
40407 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
40408 41 42 4c 45 5f 46 54 53 31 0d 0a 20 20 69 66 28  ABLE_FTS1..  if(
40409 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
4040a 65 64 20 29 7b 0d 0a 20 20 20 20 65 78 74 65 72  ed ){..    exter
4040b 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  n int sqlite3Fts
4040c 31 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b  1Init(sqlite3*);
4040d 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
4040e 65 33 46 74 73 31 49 6e 69 74 28 64 62 29 3b 0d  e3Fts1Init(db);.
4040f 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  .  }..#endif....
40410 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
40411 41 42 4c 45 5f 46 54 53 32 0d 0a 20 20 69 66 28  ABLE_FTS2..  if(
40412 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
40413 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  ed && rc==SQLITE
40414 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 65 78 74 65  _OK ){..    exte
40415 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  rn int sqlite3Ft
40416 73 32 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29  s2Init(sqlite3*)
40417 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
40418 74 65 33 46 74 73 32 49 6e 69 74 28 64 62 29 3b  te3Fts2Init(db);
40419 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d  ..  }..#endif...
4041a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
4041b 4e 41 42 4c 45 5f 46 54 53 33 0d 0a 20 20 69 66  NABLE_FTS3..  if
4041c 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
4041d 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
4041e 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20  E_OK ){..    rc 
4041f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
40420 74 28 64 62 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e  t(db);..  }..#en
40421 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51  dif....#ifdef SQ
40422 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0d  LITE_ENABLE_ICU.
40423 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
40424 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
40425 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
40426 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 63    rc = sqlite3Ic
40427 75 49 6e 69 74 28 64 62 29 3b 0d 0a 20 20 7d 0d  uInit(db);..  }.
40428 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 69 66 64 65  .#endif....#ifde
40429 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
4042a 52 54 52 45 45 0d 0a 20 20 69 66 28 20 21 64 62  RTREE..  if( !db
4042b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
4042c 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  & rc==SQLITE_OK)
4042d 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  {..    rc = sqli
4042e 74 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29  te3RtreeInit(db)
4042f 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  ;..  }..#endif..
40430 0d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
40431 28 64 62 2c 20 72 63 2c 20 30 29 3b 0d 0a 0d 0a  (db, rc, 0);....
40432 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44 45    /* -DSQLITE_DE
40433 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
40434 44 45 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c 55  DE=1 makes EXCLU
40435 53 49 56 45 20 74 68 65 20 64 65 66 61 75 6c 74  SIVE the default
40436 20 6c 6f 63 6b 69 6e 67 0d 0a 20 20 2a 2a 20 6d   locking..  ** m
40437 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44  ode.  -DSQLITE_D
40438 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
40439 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41  ODE=0 make NORMA
4043a 4c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c 6f  L the default lo
4043b 63 6b 69 6e 67 0d 0a 20 20 2a 2a 20 6d 6f 64 65  cking..  ** mode
4043c 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e 67  .  Doing nothing
4043d 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b   at all also mak
4043e 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65  es NORMAL the de
4043f 66 61 75 6c 74 2e 0d 0a 20 20 2a 2f 0d 0a 23 69  fault...  */..#i
40440 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  fdef SQLITE_DEFA
40441 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
40442 0d 0a 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  ..  db->dfltLock
40443 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45  Mode = SQLITE_DE
40444 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
40445 44 45 3b 0d 0a 20 20 73 71 6c 69 74 65 33 50 61  DE;..  sqlite3Pa
40446 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73  gerLockingMode(s
40447 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
40448 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  (db->aDb[0].pBt)
40449 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,..             
4044a 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
4044b 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b  ITE_DEFAULT_LOCK
4044c 49 4e 47 5f 4d 4f 44 45 29 3b 0d 0a 23 65 6e 64  ING_MODE);..#end
4044d 69 66 0d 0a 0d 0a 20 20 2f 2a 20 45 6e 61 62 6c  if....  /* Enabl
4044e 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d  e the lookaside-
4044f 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
40450 20 2a 2f 0d 0a 20 20 73 65 74 75 70 4c 6f 6f 6b   */..  setupLook
40451 61 73 69 64 65 28 64 62 2c 20 30 2c 20 73 71 6c  aside(db, 0, sql
40452 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
40453 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0d 0a 20  .szLookaside,.. 
40454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40455 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
40456 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b  obalConfig.nLook
40457 61 73 69 64 65 29 3b 0d 0a 0d 0a 20 20 73 71 6c  aside);....  sql
40458 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65  ite3_wal_autoche
40459 63 6b 70 6f 69 6e 74 28 64 62 2c 20 53 51 4c 49  ckpoint(db, SQLI
4045a 54 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41  TE_DEFAULT_WAL_A
4045b 55 54 4f 43 48 45 43 4b 50 4f 49 4e 54 29 3b 0d  UTOCHECKPOINT);.
4045c 0a 0d 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0d 0a  ...opendb_out:..
4045d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
4045e 4f 70 65 6e 29 3b 0d 0a 20 20 69 66 28 20 64 62  Open);..  if( db
4045f 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   ){..    assert(
40460 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30 20 7c 7c   db->mutex!=0 ||
40461 20 69 73 54 68 72 65 61 64 73 61 66 65 3d 3d 30   isThreadsafe==0
40462 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   || sqlite3Globa
40463 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
40464 65 78 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 73 71  ex==0 );..    sq
40465 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
40466 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  e(db->mutex);.. 
40467 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
40468 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0d  e3_errcode(db);.
40469 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
4046a 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
4046b 4f 4d 45 4d 20 29 3b 0d 0a 20 20 69 66 28 20 72  OMEM );..  if( r
4046c 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
4046d 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
4046e 63 6c 6f 73 65 28 64 62 29 3b 0d 0a 20 20 20 20  close(db);..    
4046f 64 62 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65  db = 0;..  }else
40470 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
40471 4f 4b 20 29 7b 0d 0a 20 20 20 20 64 62 2d 3e 6d  OK ){..    db->m
40472 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
40473 47 49 43 5f 53 49 43 4b 3b 0d 0a 20 20 7d 0d 0a  GIC_SICK;..  }..
40474 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0d 0a 20    *ppDb = db;.. 
40475 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
40476 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0d 0a  piExit(0, rc);..
40477 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e  }..../*..** Open
40478 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
40479 68 61 6e 64 6c 65 2e 0d 0a 2a 2f 0d 0a 53 51 4c  handle...*/..SQL
4047a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
4047b 74 65 33 5f 6f 70 65 6e 28 0d 0a 20 20 63 6f 6e  te3_open(..  con
4047c 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
4047d 6d 65 2c 20 0d 0a 20 20 73 71 6c 69 74 65 33 20  me, ..  sqlite3 
4047e 2a 2a 70 70 44 62 20 0d 0a 29 7b 0d 0a 20 20 72  **ppDb ..){..  r
4047f 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61  eturn openDataba
40480 73 65 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 70  se(zFilename, pp
40481 44 62 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  Db,..           
40482 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
40483 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
40484 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
40485 52 45 41 54 45 2c 20 30 29 3b 0d 0a 7d 0d 0a 53  REATE, 0);..}..S
40486 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
40487 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0d 0a  lite3_open_v2(..
40488 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
40489 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74  lename,   /* Dat
4048a 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28  abase filename (
4048b 55 54 46 2d 38 29 20 2a 2f 0d 0a 20 20 73 71 6c  UTF-8) */..  sql
4048c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20  ite3 **ppDb,    
4048d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c       /* OUT: SQL
4048e 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f  ite db handle */
4048f 0d 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  ..  int flags,  
40490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
40491 6c 61 67 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  lags */..  const
40492 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20   char *zVfs     
40493 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46     /* Name of VF
40494 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20  S module to use 
40495 2a 2f 0d 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e  */..){..  return
40496 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 66 69   openDatabase(fi
40497 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 20 28 75  lename, ppDb, (u
40498 6e 73 69 67 6e 65 64 20 69 6e 74 29 66 6c 61 67  nsigned int)flag
40499 73 2c 20 7a 56 66 73 29 3b 0d 0a 7d 0d 0a 0d 0a  s, zVfs);..}....
4049a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
4049b 4d 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a  MIT_UTF16../*..*
4049c 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74  * Open a new dat
4049d 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0d 0a 2a  abase handle...*
4049e 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
4049f 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  t sqlite3_open16
404a0 28 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  (..  const void 
404a1 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0d 0a 20 20  *zFilename, ..  
404a2 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 0d 0a  sqlite3 **ppDb..
404a3 29 7b 0d 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  ){..  char const
404a4 20 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b 20 20 20   *zFilename8;   
404a5 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 6e 63  /* zFilename enc
404a6 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 69 6e  oded in UTF-8 in
404a7 73 74 65 61 64 20 6f 66 20 55 54 46 2d 31 36 20  stead of UTF-16 
404a8 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  */..  sqlite3_va
404a9 6c 75 65 20 2a 70 56 61 6c 3b 0d 0a 20 20 69 6e  lue *pVal;..  in
404aa 74 20 72 63 3b 0d 0a 0d 0a 20 20 61 73 73 65 72  t rc;....  asser
404ab 74 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0d  t( zFilename );.
404ac 0a 20 20 61 73 73 65 72 74 28 20 70 70 44 62 20  .  assert( ppDb 
404ad 29 3b 0d 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b  );..  *ppDb = 0;
404ae 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
404af 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0d 0a  _OMIT_AUTOINIT..
404b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
404b1 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0d 0a 20 20  nitialize();..  
404b2 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
404b3 72 63 3b 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 70  rc;..#endif..  p
404b4 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
404b5 75 65 4e 65 77 28 30 29 3b 0d 0a 20 20 73 71 6c  ueNew(0);..  sql
404b6 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
404b7 70 56 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e  pVal, -1, zFilen
404b8 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  ame, SQLITE_UTF1
404b9 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
404ba 53 54 41 54 49 43 29 3b 0d 0a 20 20 7a 46 69 6c  STATIC);..  zFil
404bb 65 6e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33  ename8 = sqlite3
404bc 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
404bd 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0d 0a 20  SQLITE_UTF8);.. 
404be 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 38 20   if( zFilename8 
404bf 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 6f 70 65  ){..    rc = ope
404c0 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e  nDatabase(zFilen
404c1 61 6d 65 38 2c 20 70 70 44 62 2c 0d 0a 20 20 20  ame8, ppDb,..   
404c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
404c3 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
404c4 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54  EADWRITE | SQLIT
404c5 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20 30  E_OPEN_CREATE, 0
404c6 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  );..    assert( 
404c7 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51 4c  *ppDb || rc==SQL
404c8 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0d 0a 20 20  ITE_NOMEM );..  
404c9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
404ca 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50 72 6f  _OK && !DbHasPro
404cb 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30 2c 20  perty(*ppDb, 0, 
404cc 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
404cd 20 29 7b 0d 0a 20 20 20 20 20 20 45 4e 43 28 2a   ){..      ENC(*
404ce 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f 55  ppDb) = SQLITE_U
404cf 54 46 31 36 4e 41 54 49 56 45 3b 0d 0a 20 20 20  TF16NATIVE;..   
404d0 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20   }..  }else{..  
404d1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
404d2 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  MEM;..  }..  sql
404d3 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
404d4 61 6c 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  al);....  return
404d5 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
404d6 30 2c 20 72 63 29 3b 0d 0a 7d 0d 0a 23 65 6e 64  0, rc);..}..#end
404d7 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
404d8 54 5f 55 54 46 31 36 20 2a 2f 0d 0a 0d 0a 2f 2a  T_UTF16 */..../*
404d9 0d 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  ..** Register a 
404da 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  new collation se
404db 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20  quence with the 
404dc 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
404dd 64 62 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  db...*/..SQLITE_
404de 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
404df 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
404e0 28 0d 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  (..  sqlite3* db
404e1 2c 20 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  , ..  const char
404e2 20 2a 7a 4e 61 6d 65 2c 20 0d 0a 20 20 69 6e 74   *zName, ..  int
404e3 20 65 6e 63 2c 20 0d 0a 20 20 76 6f 69 64 2a 20   enc, ..  void* 
404e4 70 43 74 78 2c 0d 0a 20 20 69 6e 74 28 2a 78 43  pCtx,..  int(*xC
404e5 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
404e6 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
404e7 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0d 0a  t,const void*)..
404e8 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  ){..  int rc;.. 
404e9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
404ea 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
404eb 0d 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ..  assert( !db-
404ec 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
404ed 0d 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43  ..  rc = createC
404ee 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  ollation(db, zNa
404ef 6d 65 2c 20 28 75 38 29 65 6e 63 2c 20 70 43 74  me, (u8)enc, pCt
404f0 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b  x, xCompare, 0);
404f1 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
404f2 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
404f3 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
404f4 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
404f5 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  x);..  return rc
404f6 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
404f7 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f  egister a new co
404f8 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
404f9 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
404fa 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0d 0a 2a  se handle db...*
404fb 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  /..SQLITE_API in
404fc 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
404fd 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0d 0a  _collation_v2(..
404fe 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0d    sqlite3* db, .
404ff 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
40500 4e 61 6d 65 2c 20 0d 0a 20 20 69 6e 74 20 65 6e  Name, ..  int en
40501 63 2c 20 0d 0a 20 20 76 6f 69 64 2a 20 70 43 74  c, ..  void* pCt
40502 78 2c 0d 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  x,..  int(*xComp
40503 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
40504 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
40505 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0d 0a 20 20  onst void*),..  
40506 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64  void(*xDel)(void
40507 2a 29 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  *)..){..  int rc
40508 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
40509 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
4050a 65 78 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ex);..  assert( 
4050b 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
4050c 64 20 29 3b 0d 0a 20 20 72 63 20 3d 20 63 72 65  d );..  rc = cre
4050d 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
4050e 20 7a 4e 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c   zName, (u8)enc,
4050f 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c   pCtx, xCompare,
40510 20 78 44 65 6c 29 3b 0d 0a 20 20 72 63 20 3d 20   xDel);..  rc = 
40511 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
40512 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71 6c 69 74  b, rc);..  sqlit
40513 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
40514 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  b->mutex);..  re
40515 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23  turn rc;..}....#
40516 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
40517 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a 2a  IT_UTF16../*..**
40518 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
40519 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
4051a 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
4051b 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0d  base handle db..
4051c 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  .*/..SQLITE_API 
4051d 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
4051e 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0d  te_collation16(.
4051f 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
40520 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ..  const void *
40521 7a 4e 61 6d 65 2c 0d 0a 20 20 69 6e 74 20 65 6e  zName,..  int en
40522 63 2c 20 0d 0a 20 20 76 6f 69 64 2a 20 70 43 74  c, ..  void* pCt
40523 78 2c 0d 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  x,..  int(*xComp
40524 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
40525 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
40526 6f 6e 73 74 20 76 6f 69 64 2a 29 0d 0a 29 7b 0d  onst void*)..){.
40527 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
40528 54 45 5f 4f 4b 3b 0d 0a 20 20 63 68 61 72 20 2a  TE_OK;..  char *
40529 7a 4e 61 6d 65 38 3b 0d 0a 20 20 73 71 6c 69 74  zName8;..  sqlit
4052a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
4052b 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 61 73  b->mutex);..  as
4052c 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
4052d 63 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 7a 4e  cFailed );..  zN
4052e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74  ame8 = sqlite3Ut
4052f 66 31 36 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65  f16to8(db, zName
40530 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
40531 31 36 4e 41 54 49 56 45 29 3b 0d 0a 20 20 69 66  16NATIVE);..  if
40532 28 20 7a 4e 61 6d 65 38 20 29 7b 0d 0a 20 20 20  ( zName8 ){..   
40533 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c   rc = createColl
40534 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38  ation(db, zName8
40535 2c 20 28 75 38 29 65 6e 63 2c 20 70 43 74 78 2c  , (u8)enc, pCtx,
40536 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0d 0a   xCompare, 0);..
40537 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
40538 65 28 64 62 2c 20 7a 4e 61 6d 65 38 29 3b 0d 0a  e(db, zName8);..
40539 20 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69    }..  rc = sqli
4053a 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
4053b 63 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  c);..  sqlite3_m
4053c 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
4053d 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
4053e 20 72 63 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20   rc;..}..#endif 
4053f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
40540 54 46 31 36 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  TF16 */..../*..*
40541 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c  * Register a col
40542 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
40543 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
40544 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
40545 73 65 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 64 62  se handle..** db
40546 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72  . Replace any pr
40547 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c  eviously install
40548 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
40549 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0d 0a  uence factory...
4054a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
4054b 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
4054c 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0d 0a 20 20  tion_needed(..  
4054d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0d 0a 20  sqlite3 *db, .. 
4054e 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64   void *pCollNeed
4054f 65 64 41 72 67 2c 20 0d 0a 20 20 76 6f 69 64 28  edArg, ..  void(
40550 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f  *xCollNeeded)(vo
40551 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
40552 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
40553 63 68 61 72 2a 29 0d 0a 29 7b 0d 0a 20 20 73 71  char*)..){..  sq
40554 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
40555 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  r(db->mutex);.. 
40556 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
40557 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 3b 0d   = xCollNeeded;.
40558 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
40559 65 64 31 36 20 3d 20 30 3b 0d 0a 20 20 64 62 2d  ed16 = 0;..  db-
4055a 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20  >pCollNeededArg 
4055b 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  = pCollNeededArg
4055c 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
4055d 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
4055e 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  ex);..  return S
4055f 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
40560 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
40561 4d 49 54 5f 55 54 46 31 36 0d 0a 2f 2a 0d 0a 2a  MIT_UTF16../*..*
40562 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c  * Register a col
40563 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
40564 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
40565 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
40566 73 65 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 64 62  se handle..** db
40567 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72  . Replace any pr
40568 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c  eviously install
40569 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
4056a 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0d 0a  uence factory...
4056b 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
4056c 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
4056d 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0d 0a  tion_needed16(..
4056e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0d    sqlite3 *db, .
4056f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65  .  void *pCollNe
40570 65 64 65 64 41 72 67 2c 20 0d 0a 20 20 76 6f 69  ededArg, ..  voi
40571 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  d(*xCollNeeded16
40572 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
40573 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
40574 6e 73 74 20 76 6f 69 64 2a 29 0d 0a 29 7b 0d 0a  nst void*)..){..
40575 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
40576 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
40577 3b 0d 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  ;..  db->xCollNe
40578 65 64 65 64 20 3d 20 30 3b 0d 0a 20 20 64 62 2d  eded = 0;..  db-
40579 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d  >xCollNeeded16 =
4057a 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b 0d   xCollNeeded16;.
4057b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  .  db->pCollNeed
4057c 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65  edArg = pCollNee
4057d 64 65 64 41 72 67 3b 0d 0a 20 20 73 71 6c 69 74  dedArg;..  sqlit
4057e 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
4057f 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 72 65  b->mutex);..  re
40580 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
40581 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
40582 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
40583 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51  */....#ifndef SQ
40584 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
40585 41 54 45 44 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  ATED../*..** Thi
40586 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f  s function is no
40587 77 20 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d  w an anachronism
40588 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 20  . It used to be 
40589 75 73 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20  used to recover 
4058a 66 72 6f 6d 20 61 0d 0a 2a 2a 20 6d 61 6c 6c 6f  from a..** mallo
4058b 63 28 29 20 66 61 69 6c 75 72 65 2c 20 62 75 74  c() failure, but
4058c 20 53 51 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73   SQLite now does
4058d 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61   this automatica
4058e 6c 6c 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  lly...*/..SQLITE
4058f 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
40590 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28  _global_recover(
40591 76 6f 69 64 29 7b 0d 0a 20 20 72 65 74 75 72 6e  void){..  return
40592 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
40593 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
40594 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65   Test to see whe
40595 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
40596 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
40597 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f  ion is in autoco
40598 6d 6d 69 74 0d 0a 2a 2a 20 6d 6f 64 65 2e 20 20  mmit..** mode.  
40599 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69  Return TRUE if i
4059a 74 20 69 73 20 61 6e 64 20 46 41 4c 53 45 20 69  t is and FALSE i
4059b 66 20 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d 6d  f not.  Autocomm
4059c 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 0d 0a 2a  it mode is on..*
4059d 2a 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 41  * by default.  A
4059e 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20 64 69 73  utocommit is dis
4059f 61 62 6c 65 64 20 62 79 20 61 20 42 45 47 49 4e  abled by a BEGIN
405a0 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72   statement and r
405a1 65 65 6e 61 62 6c 65 64 0d 0a 2a 2a 20 62 79 20  eenabled..** by 
405a2 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d 49 54 20  the next COMMIT 
405a3 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0d 0a 2a 2a  or ROLLBACK...**
405a4 0d 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20 49  ..******* THIS I
405a5 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54 41  S AN EXPERIMENTA
405a6 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55 42  L API AND IS SUB
405a7 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20 2a  JECT TO CHANGE *
405a8 2a 2a 2a 2a 2a 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  *****..*/..SQLIT
405a9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
405aa 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
405ab 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a  (sqlite3 *db){..
405ac 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74    return db->aut
405ad 6f 43 6f 6d 6d 69 74 3b 0d 0a 7d 0d 0a 0d 0a 2f  oCommit;..}..../
405ae 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  *..** The follow
405af 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
405b0 20 73 75 62 74 69 74 75 74 65 73 20 66 6f 72 20   subtitutes for 
405b1 63 6f 6e 73 74 61 6e 74 73 20 53 51 4c 49 54 45  constants SQLITE
405b2 5f 43 4f 52 52 55 50 54 2c 0d 0a 2a 2a 20 53 51  _CORRUPT,..** SQ
405b3 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 53 51 4c  LITE_MISUSE, SQL
405b4 49 54 45 5f 43 41 4e 54 4f 50 45 4e 2c 20 53 51  ITE_CANTOPEN, SQ
405b5 4c 49 54 45 5f 49 4f 45 52 52 20 61 6e 64 20 70  LITE_IOERR and p
405b6 6f 73 73 69 62 6c 79 20 6f 74 68 65 72 20 65 72  ossibly other er
405b7 72 6f 72 0d 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74  ror..** constant
405b8 73 2e 20 20 54 68 65 79 20 73 65 72 76 65 72 20  s.  They server 
405b9 74 77 6f 20 70 75 72 70 6f 73 65 73 3a 0d 0a 2a  two purposes:..*
405ba 2a 0d 0a 2a 2a 20 20 20 31 2e 20 20 53 65 72 76  *..**   1.  Serv
405bb 65 20 61 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e  e as a convenien
405bc 74 20 70 6c 61 63 65 20 74 6f 20 73 65 74 20 61  t place to set a
405bd 20 62 72 65 61 6b 70 6f 69 6e 74 20 69 6e 20 61   breakpoint in a
405be 20 64 65 62 75 67 67 65 72 0d 0a 2a 2a 20 20 20   debugger..**   
405bf 20 20 20 20 74 6f 20 64 65 74 65 63 74 20 77 68      to detect wh
405c0 65 6e 20 76 65 72 73 69 6f 6e 20 65 72 72 6f 72  en version error
405c1 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 63 63 75   conditions occu
405c2 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 2e  rs...**..**   2.
405c3 20 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33    Invoke sqlite3
405c4 5f 6c 6f 67 28 29 20 74 6f 20 70 72 6f 76 69 64  _log() to provid
405c5 65 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64  e the source cod
405c6 65 20 6c 6f 63 61 74 69 6f 6e 20 77 68 65 72 65  e location where
405c7 0d 0a 2a 2a 20 20 20 20 20 20 20 61 20 6c 6f 77  ..**       a low
405c8 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 69 73 20  -level error is 
405c9 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0d  first detected..
405ca 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
405cb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
405cc 6f 72 72 75 70 74 45 72 72 6f 72 28 69 6e 74 20  orruptError(int 
405cd 6c 69 6e 65 6e 6f 29 7b 0d 0a 20 20 74 65 73 74  lineno){..  test
405ce 63 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f  case( sqlite3Glo
405cf 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d  balConfig.xLog!=
405d0 30 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  0 );..  sqlite3_
405d1 6c 6f 67 28 53 51 4c 49 54 45 5f 43 4f 52 52 55  log(SQLITE_CORRU
405d2 50 54 2c 0d 0a 20 20 20 20 20 20 20 20 20 20 20  PT,..           
405d3 20 20 20 22 64 61 74 61 62 61 73 65 20 63 6f 72     "database cor
405d4 72 75 70 74 69 6f 6e 20 61 74 20 6c 69 6e 65 20  ruption at line 
405d5 25 64 20 6f 66 20 5b 25 2e 31 30 73 5d 22 2c 0d  %d of [%.10s]",.
405d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c  .              l
405d7 69 6e 65 6e 6f 2c 20 32 30 2b 73 71 6c 69 74 65  ineno, 20+sqlite
405d8 33 5f 73 6f 75 72 63 65 69 64 28 29 29 3b 0d 0a  3_sourceid());..
405d9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
405da 43 4f 52 52 55 50 54 3b 0d 0a 7d 0d 0a 53 51 4c  CORRUPT;..}..SQL
405db 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
405dc 73 71 6c 69 74 65 33 4d 69 73 75 73 65 45 72 72  sqlite3MisuseErr
405dd 6f 72 28 69 6e 74 20 6c 69 6e 65 6e 6f 29 7b 0d  or(int lineno){.
405de 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c  .  testcase( sql
405df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
405e0 2e 78 4c 6f 67 21 3d 30 20 29 3b 0d 0a 20 20 73  .xLog!=0 );..  s
405e1 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54  qlite3_log(SQLIT
405e2 45 5f 4d 49 53 55 53 45 2c 20 0d 0a 20 20 20 20  E_MISUSE, ..    
405e3 20 20 20 20 20 20 20 20 20 20 22 6d 69 73 75 73            "misus
405e4 65 20 61 74 20 6c 69 6e 65 20 25 64 20 6f 66 20  e at line %d of 
405e5 5b 25 2e 31 30 73 5d 22 2c 0d 0a 20 20 20 20 20  [%.10s]",..     
405e6 20 20 20 20 20 20 20 20 20 6c 69 6e 65 6e 6f 2c           lineno,
405e7 20 32 30 2b 73 71 6c 69 74 65 33 5f 73 6f 75 72   20+sqlite3_sour
405e8 63 65 69 64 28 29 29 3b 0d 0a 20 20 72 65 74 75  ceid());..  retu
405e9 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
405ea 3b 0d 0a 7d 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ;..}..SQLITE_PRI
405eb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
405ec 43 61 6e 74 6f 70 65 6e 45 72 72 6f 72 28 69 6e  CantopenError(in
405ed 74 20 6c 69 6e 65 6e 6f 29 7b 0d 0a 20 20 74 65  t lineno){..  te
405ee 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33 47  stcase( sqlite3G
405ef 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67  lobalConfig.xLog
405f0 21 3d 30 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  !=0 );..  sqlite
405f1 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 43 41 4e  3_log(SQLITE_CAN
405f2 54 4f 50 45 4e 2c 20 0d 0a 20 20 20 20 20 20 20  TOPEN, ..       
405f3 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f         "cannot o
405f4 70 65 6e 20 66 69 6c 65 20 61 74 20 6c 69 6e 65  pen file at line
405f5 20 25 64 20 6f 66 20 5b 25 2e 31 30 73 5d 22 2c   %d of [%.10s]",
405f6 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
405f7 6c 69 6e 65 6e 6f 2c 20 32 30 2b 73 71 6c 69 74  lineno, 20+sqlit
405f8 65 33 5f 73 6f 75 72 63 65 69 64 28 29 29 3b 0d  e3_sourceid());.
405f9 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
405fa 5f 43 41 4e 54 4f 50 45 4e 3b 0d 0a 7d 0d 0a 0d  _CANTOPEN;..}...
405fb 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
405fc 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
405fd 44 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69  D../*..** This i
405fe 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  s a convenience 
405ff 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6d 61 6b  routine that mak
40600 65 73 20 73 75 72 65 20 74 68 61 74 20 61 6c 6c  es sure that all
40601 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63   thread-specific
40602 0d 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68  ..** data for th
40603 69 73 20 74 68 72 65 61 64 20 68 61 73 20 62 65  is thread has be
40604 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0d  en deallocated..
40605 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 6e  .**..** SQLite n
40606 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 74 68  o longer uses th
40607 72 65 61 64 2d 73 70 65 63 69 66 69 63 20 64 61  read-specific da
40608 74 61 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69  ta so this routi
40609 6e 65 20 69 73 20 6e 6f 77 20 61 0d 0a 2a 2a 20  ne is now a..** 
4060a 6e 6f 2d 6f 70 2e 20 20 49 74 20 69 73 20 72 65  no-op.  It is re
4060b 74 61 69 6e 65 64 20 66 6f 72 20 68 69 73 74 6f  tained for histo
4060c 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
4060d 69 74 79 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ity...*/..SQLITE
4060e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
4060f 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70  3_thread_cleanup
40610 28 76 6f 69 64 29 7b 0d 0a 7d 0d 0a 23 65 6e 64  (void){..}..#end
40611 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  if..../*..** Ret
40612 75 72 6e 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61  urn meta informa
40613 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 70 65  tion about a spe
40614 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20  cific column of 
40615 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
40616 2e 0d 0a 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e  ...** See commen
40617 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 68 20 28  t in sqlite3.h (
40618 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20 66 6f 72  sqlite.h.in) for
40619 20 64 65 74 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 23   details...*/..#
4061a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
4061b 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
4061c 41 54 41 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ATA..SQLITE_API 
4061d 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
4061e 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
4061f 61 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  a(..  sqlite3 *d
40620 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
40621 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
40622 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e  handle */..  con
40623 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65  st char *zDbName
40624 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  ,        /* Data
40625 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c  base name or NUL
40626 4c 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  L */..  const ch
40627 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20  ar *zTableName, 
40628 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
40629 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  e */..  const ch
4062a 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c  ar *zColumnName,
4062b 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
4062c 6d 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 63 6f  me */..  char co
4062d 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65  nst **pzDataType
4062e 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20  ,    /* OUTPUT: 
4062f 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79  Declared data ty
40630 70 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 63 6f  pe */..  char co
40631 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c  nst **pzCollSeq,
40632 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
40633 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
40634 63 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e  ce name */..  in
40635 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20  t *pNotNull,    
40636 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
40637 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54  PUT: True if NOT
40638 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74   NULL constraint
40639 20 65 78 69 73 74 73 20 2a 2f 0d 0a 20 20 69 6e   exists */..  in
4063a 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20  t *pPrimaryKey, 
4063b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
4063c 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
4063d 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a  umn part of PK *
4063e 2f 0d 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69  /..  int *pAutoi
4063f 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
40640 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
40641 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75   if column is au
40642 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0d  to-increment */.
40643 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  .){..  int rc;..
40644 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20    char *zErrMsg 
40645 3d 20 30 3b 0d 0a 20 20 54 61 62 6c 65 20 2a 70  = 0;..  Table *p
40646 54 61 62 20 3d 20 30 3b 0d 0a 20 20 43 6f 6c 75  Tab = 0;..  Colu
40647 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0d 0a 20  mn *pCol = 0;.. 
40648 20 69 6e 74 20 69 43 6f 6c 3b 0d 0a 0d 0a 20 20   int iCol;....  
40649 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74  char const *zDat
4064a 61 54 79 70 65 20 3d 20 30 3b 0d 0a 20 20 63 68  aType = 0;..  ch
4064b 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53  ar const *zCollS
4064c 65 71 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 6e  eq = 0;..  int n
4064d 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0d 0a 20 20 69  otnull = 0;..  i
4064e 6e 74 20 70 72 69 6d 61 72 79 6b 65 79 20 3d 20  nt primarykey = 
4064f 30 3b 0d 0a 20 20 69 6e 74 20 61 75 74 6f 69 6e  0;..  int autoin
40650 63 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 45  c = 0;....  /* E
40651 6e 73 75 72 65 20 74 68 65 20 64 61 74 61 62 61  nsure the databa
40652 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65  se schema has be
40653 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0d 0a 20 20  en loaded */..  
40654 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
40655 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ter(db->mutex);.
40656 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
40657 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0d 0a 20 20  nterAll(db);..  
40658 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
40659 28 64 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0d  (db, &zErrMsg);.
4065a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
4065b 21 3d 72 63 20 29 7b 0d 0a 20 20 20 20 67 6f 74  !=rc ){..    got
4065c 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0d 0a 20 20  o error_out;..  
4065d 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f 63 61 74 65  }....  /* Locate
4065e 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 71 75   the table in qu
4065f 65 73 74 69 6f 6e 20 2a 2f 0d 0a 20 20 70 54 61  estion */..  pTa
40660 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
40661 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 6c 65 4e  able(db, zTableN
40662 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3b 0d 0a  ame, zDbName);..
40663 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20 70    if( !pTab || p
40664 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0d  Tab->pSelect ){.
40665 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0d 0a  .    pTab = 0;..
40666 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
40667 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  ut;..  }....  /*
40668 20 46 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e   Find the column
40669 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 20   for which info 
4066a 69 73 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0d  is requested */.
4066b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
4066c 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d  Rowid(zColumnNam
4066d 65 29 20 29 7b 0d 0a 20 20 20 20 69 43 6f 6c 20  e) ){..    iCol 
4066e 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0d 0a  = pTab->iPKey;..
4066f 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
40670 29 7b 0d 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d  ){..      pCol =
40671 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
40672 6c 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65  l];..    }..  }e
40673 6c 73 65 7b 0d 0a 20 20 20 20 66 6f 72 28 69 43  lse{..    for(iC
40674 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
40675 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0d  >nCol; iCol++){.
40676 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70  .      pCol = &p
40677 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b  Tab->aCol[iCol];
40678 0d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73  ..      if( 0==s
40679 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
4067a 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75  ol->zName, zColu
4067b 6d 6e 4e 61 6d 65 29 20 29 7b 0d 0a 20 20 20 20  mnName) ){..    
4067c 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
4067d 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
4067e 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e  if( iCol==pTab->
4067f 6e 43 6f 6c 20 29 7b 0d 0a 20 20 20 20 20 20 70  nCol ){..      p
40680 54 61 62 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  Tab = 0;..      
40681 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0d  goto error_out;.
40682 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
40683 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
40684 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74  g block stores t
40685 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  he meta informat
40686 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ion that will be
40687 20 72 65 74 75 72 6e 65 64 0d 0a 20 20 2a 2a 20   returned..  ** 
40688 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e  to the caller in
40689 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
4068a 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c   zDataType, zCol
4068b 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70  lSeq, notnull, p
4068c 72 69 6d 61 72 79 6b 65 79 0d 0a 20 20 2a 2a 20  rimarykey..  ** 
4068d 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20  and autoinc. At 
4068e 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65  this point there
4068f 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69   are two possibi
40690 6c 69 74 69 65 73 3a 0d 0a 20 20 2a 2a 20 0d 0a  lities:..  ** ..
40691 20 20 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20    **     1. The 
40692 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e  specified column
40693 20 6e 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22   name was rowid"
40694 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77  , "oid" or "_row
40695 69 64 5f 22 20 0d 0a 20 20 2a 2a 20 20 20 20 20  id_" ..  **     
40696 20 20 20 61 6e 64 20 74 68 65 72 65 20 69 73 20     and there is 
40697 6e 6f 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  no explicitly de
40698 63 6c 61 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d  clared IPK colum
40699 6e 2e 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  n. ..  **..  ** 
4069a 20 20 20 20 32 2e 20 54 68 65 20 74 61 62 6c 65      2. The table
4069b 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 61   is not a view a
4069c 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  nd the column na
4069d 6d 65 20 69 64 65 6e 74 69 66 69 65 64 20 61 6e  me identified an
4069e 20 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 65   ..  **        e
4069f 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
406a0 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f 70 79 20  ed column. Copy 
406a1 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
406a2 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0d 0a 20 20   from *pCol...  
406a3 2a 2f 20 0d 0a 20 20 69 66 28 20 70 43 6f 6c 20  */ ..  if( pCol 
406a4 29 7b 0d 0a 20 20 20 20 7a 44 61 74 61 54 79 70  ){..    zDataTyp
406a5 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b  e = pCol->zType;
406a6 0d 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d  ..    zCollSeq =
406a7 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0d 0a 20   pCol->zColl;.. 
406a8 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f     notnull = pCo
406a9 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30 3b 0d 0a  l->notNull!=0;..
406aa 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 20      primarykey  
406ab 3d 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  = pCol->isPrimKe
406ac 79 21 3d 30 3b 0d 0a 20 20 20 20 61 75 74 6f 69  y!=0;..    autoi
406ad 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  nc = pTab->iPKey
406ae 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54 61 62 2d  ==iCol && (pTab-
406af 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
406b0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 21 3d 30  utoincrement)!=0
406b1 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
406b2 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49 4e   zDataType = "IN
406b3 54 45 47 45 52 22 3b 0d 0a 20 20 20 20 70 72 69  TEGER";..    pri
406b4 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0d 0a 20 20  marykey = 1;..  
406b5 7d 0d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53  }..  if( !zCollS
406b6 65 71 20 29 7b 0d 0a 20 20 20 20 7a 43 6f 6c 6c  eq ){..    zColl
406b7 53 65 71 20 3d 20 22 42 49 4e 41 52 59 22 3b 0d  Seq = "BINARY";.
406b8 0a 20 20 7d 0d 0a 0d 0a 65 72 72 6f 72 5f 6f 75  .  }....error_ou
406b9 74 3a 0d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  t:..  sqlite3Btr
406ba 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0d  eeLeaveAll(db);.
406bb 0a 0d 0a 20 20 2f 2a 20 57 68 65 74 68 65 72 20  ...  /* Whether 
406bc 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  the function cal
406bd 6c 20 73 75 63 63 65 65 64 65 64 20 6f 72 20 66  l succeeded or f
406be 61 69 6c 65 64 2c 20 73 65 74 20 74 68 65 20 6f  ailed, set the o
406bf 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
406c0 0d 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74 65 76  ..  ** to whatev
406c1 65 72 20 74 68 65 69 72 20 6c 6f 63 61 6c 20 63  er their local c
406c2 6f 75 6e 74 65 72 70 61 72 74 73 20 63 6f 6e 74  ounterparts cont
406c3 61 69 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ain. If an error
406c4 20 64 69 64 20 6f 63 63 75 72 2c 0d 0a 20 20 2a   did occur,..  *
406c5 2a 20 74 68 69 73 20 68 61 73 20 74 68 65 20 65  * this has the e
406c6 66 66 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e 67  ffect of zeroing
406c7 20 61 6c 6c 20 6f 75 74 70 75 74 20 70 61 72 61   all output para
406c8 6d 65 74 65 72 73 2e 0d 0a 20 20 2a 2f 0d 0a 20  meters...  */.. 
406c9 20 69 66 28 20 70 7a 44 61 74 61 54 79 70 65 20   if( pzDataType 
406ca 29 20 2a 70 7a 44 61 74 61 54 79 70 65 20 3d 20  ) *pzDataType = 
406cb 7a 44 61 74 61 54 79 70 65 3b 0d 0a 20 20 69 66  zDataType;..  if
406cc 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70  ( pzCollSeq ) *p
406cd 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c  zCollSeq = zColl
406ce 53 65 71 3b 0d 0a 20 20 69 66 28 20 70 4e 6f 74  Seq;..  if( pNot
406cf 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c  Null ) *pNotNull
406d0 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0d 0a 20 20 69   = notnull;..  i
406d1 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29  f( pPrimaryKey )
406d2 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20   *pPrimaryKey = 
406d3 70 72 69 6d 61 72 79 6b 65 79 3b 0d 0a 20 20 69  primarykey;..  i
406d4 66 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a 70  f( pAutoinc ) *p
406d5 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e  Autoinc = autoin
406d6 63 3b 0d 0a 0d 0a 20 20 69 66 28 20 53 51 4c 49  c;....  if( SQLI
406d7 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54  TE_OK==rc && !pT
406d8 61 62 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  ab ){..    sqlit
406d9 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
406da 72 4d 73 67 29 3b 0d 0a 20 20 20 20 7a 45 72 72  rMsg);..    zErr
406db 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Msg = sqlite3MPr
406dc 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63  intf(db, "no suc
406dd 68 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20  h table column: 
406de 25 73 2e 25 73 22 2c 20 7a 54 61 62 6c 65 4e 61  %s.%s", zTableNa
406df 6d 65 2c 0d 0a 20 20 20 20 20 20 20 20 7a 43 6f  me,..        zCo
406e0 6c 75 6d 6e 4e 61 6d 65 29 3b 0d 0a 20 20 20 20  lumnName);..    
406e1 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
406e2 52 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74  R;..  }..  sqlit
406e3 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
406e4 28 7a 45 72 72 4d 73 67 3f 22 25 73 22 3a 30 29  (zErrMsg?"%s":0)
406e5 2c 20 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 73  , zErrMsg);..  s
406e6 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
406e7 20 7a 45 72 72 4d 73 67 29 3b 0d 0a 20 20 72 63   zErrMsg);..  rc
406e8 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
406e9 74 28 64 62 2c 20 72 63 29 3b 0d 0a 20 20 73 71  t(db, rc);..  sq
406ea 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
406eb 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20  e(db->mutex);.. 
406ec 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
406ed 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
406ee 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74   Sleep for a lit
406ef 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75  tle while.  Retu
406f0 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
406f1 20 74 69 6d 65 20 73 6c 65 70 74 2e 0d 0a 2a 2f   time slept...*/
406f2 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ..SQLITE_API int
406f3 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69   sqlite3_sleep(i
406f4 6e 74 20 6d 73 29 7b 0d 0a 20 20 73 71 6c 69 74  nt ms){..  sqlit
406f5 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 0d 0a 20  e3_vfs *pVfs;.. 
406f6 20 69 6e 74 20 72 63 3b 0d 0a 20 20 70 56 66 73   int rc;..  pVfs
406f7 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66   = sqlite3_vfs_f
406f8 69 6e 64 28 30 29 3b 0d 0a 20 20 69 66 28 20 70  ind(0);..  if( p
406f9 56 66 73 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Vfs==0 ) return 
406fa 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73 20  0;....  /* This 
406fb 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 69  function works i
406fc 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20  n milliseconds, 
406fd 62 75 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69  but the underlyi
406fe 6e 67 20 4f 73 53 6c 65 65 70 28 29 20 0d 0a 20  ng OsSleep() .. 
406ff 20 2a 2a 20 41 50 49 20 75 73 65 73 20 6d 69 63   ** API uses mic
40700 72 6f 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65  roseconds. Hence
40701 20 74 68 65 20 31 30 30 30 27 73 2e 0d 0a 20 20   the 1000's...  
40702 2a 2f 0d 0a 20 20 72 63 20 3d 20 28 73 71 6c 69  */..  rc = (sqli
40703 74 65 33 4f 73 53 6c 65 65 70 28 70 56 66 73 2c  te3OsSleep(pVfs,
40704 20 31 30 30 30 2a 6d 73 29 2f 31 30 30 30 29 3b   1000*ms)/1000);
40705 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
40706 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 6e 61 62  }..../*..** Enab
40707 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68  le or disable th
40708 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
40709 74 20 63 6f 64 65 73 2e 0d 0a 2a 2f 0d 0a 53 51  t codes...*/..SQ
4070a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
4070b 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
4070c 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74  sult_codes(sqlit
4070d 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66  e3 *db, int onof
4070e 66 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  f){..  sqlite3_m
4070f 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
40710 75 74 65 78 29 3b 0d 0a 20 20 64 62 2d 3e 65 72  utex);..  db->er
40711 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66 20 3f 20  rMask = onoff ? 
40712 30 78 66 66 66 66 66 66 66 66 20 3a 20 30 78 66  0xffffffff : 0xf
40713 66 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  f;..  sqlite3_mu
40714 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
40715 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  tex);..  return 
40716 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
40717 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ./*..** Invoke t
40718 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
40719 6d 65 74 68 6f 64 20 6f 6e 20 61 20 70 61 72 74  method on a part
4071a 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 2e  icular database.
4071b 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
4071c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c   int sqlite3_fil
4071d 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  e_control(sqlite
4071e 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
4071f 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20  r *zDbName, int 
40720 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
40721 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
40722 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 69 6e  ITE_ERROR;..  in
40723 74 20 69 44 62 3b 0d 0a 20 20 73 71 6c 69 74 65  t iDb;..  sqlite
40724 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
40725 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 69 66 28  ->mutex);..  if(
40726 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0d 0a   zDbName==0 ){..
40727 20 20 20 20 69 44 62 20 3d 20 30 3b 0d 0a 20 20      iDb = 0;..  
40728 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 66 6f 72 28  }else{..    for(
40729 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d 3e 6e  iDb=0; iDb<db->n
4072a 44 62 3b 20 69 44 62 2b 2b 29 7b 0d 0a 20 20 20  Db; iDb++){..   
4072b 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 64 62     if( strcmp(db
4072c 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
4072d 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d 30 20 29 20  , zDbName)==0 ) 
4072e 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
4072f 20 7d 0d 0a 20 20 69 66 28 20 69 44 62 3c 64 62   }..  if( iDb<db
40730 2d 3e 6e 44 62 20 29 7b 0d 0a 20 20 20 20 42 74  ->nDb ){..    Bt
40731 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 64 62  ree *pBtree = db
40732 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0d  ->aDb[iDb].pBt;.
40733 0a 20 20 20 20 69 66 28 20 70 42 74 72 65 65 20  .    if( pBtree 
40734 29 7b 0d 0a 20 20 20 20 20 20 50 61 67 65 72 20  ){..      Pager 
40735 2a 70 50 61 67 65 72 3b 0d 0a 20 20 20 20 20 20  *pPager;..      
40736 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
40737 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
40738 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
40739 65 29 3b 0d 0a 20 20 20 20 20 20 70 50 61 67 65  e);..      pPage
4073a 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
4073b 50 61 67 65 72 28 70 42 74 72 65 65 29 3b 0d 0a  Pager(pBtree);..
4073c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
4073d 61 67 65 72 21 3d 30 20 29 3b 0d 0a 20 20 20 20  ager!=0 );..    
4073e 20 20 66 64 20 3d 20 73 71 6c 69 74 65 33 50 61    fd = sqlite3Pa
4073f 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
40740 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
40741 66 64 21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  fd!=0 );..      
40742 69 66 28 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 46  if( op==SQLITE_F
40743 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  CNTL_FILE_POINTE
40744 52 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 28  R ){..        *(
40745 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2a 29 70  sqlite3_file**)p
40746 41 72 67 20 3d 20 66 64 3b 0d 0a 20 20 20 20 20  Arg = fd;..     
40747 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
40748 4b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  K;..      }else 
40749 69 66 28 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73  if( fd->pMethods
4074a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
4074b 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  = sqlite3OsFileC
4074c 6f 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c 20 70  ontrol(fd, op, p
4074d 41 72 67 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  Arg);..      }el
4074e 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  se{..        rc 
4074f 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e  = SQLITE_NOTFOUN
40750 44 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  D;..      }..   
40751 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
40752 65 61 76 65 28 70 42 74 72 65 65 29 3b 0d 0a 20  eave(pBtree);.. 
40753 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c     }..  }..  sql
40754 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
40755 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20  (db->mutex);..  
40756 72 65 74 75 72 6e 20 72 63 3b 20 20 20 0d 0a 7d  return rc;   ..}
40757 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74 65 72  ..../*..** Inter
40758 66 61 63 65 20 74 6f 20 74 68 65 20 74 65 73 74  face to the test
40759 69 6e 67 20 6c 6f 67 69 63 2e 0d 0a 2a 2f 0d 0a  ing logic...*/..
4075a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
4075b 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
4075c 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  rol(int op, ...)
4075d 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  {..  int rc = 0;
4075e 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
4075f 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
40760 53 54 0d 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  ST..  va_list ap
40761 3b 0d 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  ;..  va_start(ap
40762 2c 20 6f 70 29 3b 0d 0a 20 20 73 77 69 74 63 68  , op);..  switch
40763 28 20 6f 70 20 29 7b 0d 0a 0d 0a 20 20 20 20 2f  ( op ){....    /
40764 2a 0d 0a 20 20 20 20 2a 2a 20 53 61 76 65 20 74  *..    ** Save t
40765 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
40766 20 6f 66 20 74 68 65 20 50 52 4e 47 2e 0d 0a 20   of the PRNG... 
40767 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20     */..    case 
40768 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
40769 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0d 0a 20 20  PRNG_SAVE: {..  
4076a 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 53      sqlite3PrngS
4076b 61 76 65 53 74 61 74 65 28 29 3b 0d 0a 20 20 20  aveState();..   
4076c 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
4076d 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20 20 20  ....    /*..    
4076e 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 73  ** Restore the s
4076f 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47  tate of the PRNG
40770 20 74 6f 20 74 68 65 20 6c 61 73 74 20 73 74 61   to the last sta
40771 74 65 20 73 61 76 65 64 20 75 73 69 6e 67 0d 0a  te saved using..
40772 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53 41 56 45      ** PRNG_SAVE
40773 2e 20 20 49 66 20 50 52 4e 47 5f 53 41 56 45 20  .  If PRNG_SAVE 
40774 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65  has never before
40775 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68   been called, th
40776 65 6e 0d 0a 20 20 20 20 2a 2a 20 74 68 69 73 20  en..    ** this 
40777 76 65 72 62 20 61 63 74 73 20 6c 69 6b 65 20 50  verb acts like P
40778 52 4e 47 5f 52 45 53 45 54 2e 0d 0a 20 20 20 20  RNG_RESET...    
40779 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c  */..    case SQL
4077a 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
4077b 47 5f 52 45 53 54 4f 52 45 3a 20 7b 0d 0a 20 20  G_RESTORE: {..  
4077c 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52      sqlite3PrngR
4077d 65 73 74 6f 72 65 53 74 61 74 65 28 29 3b 0d 0a  estoreState();..
4077e 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
4077f 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20    }....    /*.. 
40780 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20     ** Reset the 
40781 50 52 4e 47 20 62 61 63 6b 20 74 6f 20 69 74 73  PRNG back to its
40782 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73   uninitialized s
40783 74 61 74 65 2e 20 20 54 68 65 20 6e 65 78 74 20  tate.  The next 
40784 63 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 74 6f 20  call..    ** to 
40785 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
40786 73 73 28 29 20 77 69 6c 6c 20 72 65 73 65 65 64  ss() will reseed
40787 20 74 68 65 20 50 52 4e 47 20 75 73 69 6e 67 20   the PRNG using 
40788 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0d 0a 20  a single call.. 
40789 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 52 61     ** to the xRa
4078a 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20  ndomness method 
4078b 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56  of the default V
4078c 46 53 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  FS...    */..   
4078d 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
4078e 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54  TCTRL_PRNG_RESET
4078f 3a 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  : {..      sqlit
40790 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65  e3PrngResetState
40791 28 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b  ();..      break
40792 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
40793 2f 2a 0d 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69  /*..    **  sqli
40794 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
40795 28 42 49 54 56 45 43 5f 54 45 53 54 2c 20 73 69  (BITVEC_TEST, si
40796 7a 65 2c 20 70 72 6f 67 72 61 6d 29 0d 0a 20 20  ze, program)..  
40797 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 52 75 6e    **..    ** Run
40798 20 61 20 74 65 73 74 20 61 67 61 69 6e 73 74 20   a test against 
40799 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20  a Bitvec object 
4079a 6f 66 20 73 69 7a 65 2e 20 20 54 68 65 20 70 72  of size.  The pr
4079b 6f 67 72 61 6d 20 61 72 67 75 6d 65 6e 74 0d 0a  ogram argument..
4079c 20 20 20 20 2a 2a 20 69 73 20 61 6e 20 61 72 72      ** is an arr
4079d 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74  ay of integers t
4079e 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
4079f 74 65 73 74 2e 20 20 52 65 74 75 72 6e 20 2d 31  test.  Return -1
407a0 20 6f 6e 20 61 0d 0a 20 20 20 20 2a 2a 20 6d 65   on a..    ** me
407a1 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
407a2 65 72 72 6f 72 2c 20 30 20 6f 6e 20 73 75 63 63  error, 0 on succ
407a3 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  ess, or non-zero
407a4 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 2e 0d 0a   for an error...
407a5 20 20 20 20 2a 2a 20 53 65 65 20 74 68 65 20 73      ** See the s
407a6 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
407a7 74 69 6e 54 65 73 74 28 29 20 66 6f 72 20 61 64  tinTest() for ad
407a8 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
407a9 74 69 6f 6e 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  tion...    */.. 
407aa 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
407ab 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54  ESTCTRL_BITVEC_T
407ac 45 53 54 3a 20 7b 0d 0a 20 20 20 20 20 20 69 6e  EST: {..      in
407ad 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70  t sz = va_arg(ap
407ae 2c 20 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20 69  , int);..      i
407af 6e 74 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61  nt *aProg = va_a
407b0 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0d 0a 20  rg(ap, int*);.. 
407b1 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
407b2 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
407b3 73 74 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0d 0a  st(sz, aProg);..
407b4 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
407b5 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20    }....    /*.. 
407b6 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74     **  sqlite3_t
407b7 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e 49  est_control(BENI
407b8 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c  GN_MALLOC_HOOKS,
407b9 20 78 42 65 67 69 6e 2c 20 78 45 6e 64 29 0d 0a   xBegin, xEnd)..
407ba 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 52      **..    ** R
407bb 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f  egister hooks to
407bc 20 63 61 6c 6c 20 74 6f 20 69 6e 64 69 63 61 74   call to indicat
407bd 65 20 77 68 69 63 68 20 6d 61 6c 6c 6f 63 28 29  e which malloc()
407be 20 66 61 69 6c 75 72 65 73 20 0d 0a 20 20 20 20   failures ..    
407bf 2a 2a 20 61 72 65 20 62 65 6e 69 67 6e 2e 0d 0a  ** are benign...
407c0 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65      */..    case
407c1 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
407c2 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48  _BENIGN_MALLOC_H
407c3 4f 4f 4b 53 3a 20 7b 0d 0a 20 20 20 20 20 20 74  OOKS: {..      t
407c4 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 76 6f  ypedef void (*vo
407c5 69 64 5f 66 75 6e 63 74 69 6f 6e 29 28 76 6f 69  id_function)(voi
407c6 64 29 3b 0d 0a 20 20 20 20 20 20 76 6f 69 64 5f  d);..      void_
407c7 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e  function xBenign
407c8 42 65 67 69 6e 3b 0d 0a 20 20 20 20 20 20 76 6f  Begin;..      vo
407c9 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e  id_function xBen
407ca 69 67 6e 45 6e 64 3b 0d 0a 20 20 20 20 20 20 78  ignEnd;..      x
407cb 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 76 61  BenignBegin = va
407cc 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75  _arg(ap, void_fu
407cd 6e 63 74 69 6f 6e 29 3b 0d 0a 20 20 20 20 20 20  nction);..      
407ce 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61 5f  xBenignEnd = va_
407cf 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e  arg(ap, void_fun
407d0 63 74 69 6f 6e 29 3b 0d 0a 20 20 20 20 20 20 73  ction);..      s
407d1 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
407d2 6f 63 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42  ocHooks(xBenignB
407d3 65 67 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64  egin, xBenignEnd
407d4 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
407d5 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
407d6 2a 0d 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  *..    **  sqlit
407d7 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
407d8 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
407d9 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 75 6e  PENDING_BYTE, un
407da 73 69 67 6e 65 64 20 69 6e 74 20 58 29 0d 0a 20  signed int X).. 
407db 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 53 65     **..    ** Se
407dc 74 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79  t the PENDING by
407dd 74 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  te to the value 
407de 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c  in the argument,
407df 20 69 66 20 58 3e 30 2e 0d 0a 20 20 20 20 2a 2a   if X>0...    **
407e0 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73   Make no changes
407e1 20 69 66 20 58 3d 3d 30 2e 20 20 52 65 74 75 72   if X==0.  Retur
407e2 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
407e3 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 0d  he pending byte.
407e4 0a 20 20 20 20 2a 2a 20 61 73 20 69 74 20 65 78  .    ** as it ex
407e5 69 73 74 69 6e 67 20 62 65 66 6f 72 65 20 74 68  isting before th
407e6 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63  is routine was c
407e7 61 6c 6c 65 64 2e 0d 0a 20 20 20 20 2a 2a 0d 0a  alled...    **..
407e8 20 20 20 20 2a 2a 20 49 4d 50 4f 52 54 41 4e 54      ** IMPORTANT
407e9 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20  :  Changing the 
407ea 50 45 4e 44 49 4e 47 20 62 79 74 65 20 66 72 6f  PENDING byte fro
407eb 6d 20 30 78 34 30 30 30 30 30 30 30 20 72 65 73  m 0x40000000 res
407ec 75 6c 74 73 20 69 6e 0d 0a 20 20 20 20 2a 2a 20  ults in..    ** 
407ed 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  an incompatible 
407ee 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
407ef 72 6d 61 74 2e 20 20 43 68 61 6e 67 69 6e 67 20  rmat.  Changing 
407f0 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65  the PENDING byte
407f1 0d 0a 20 20 20 20 2a 2a 20 77 68 69 6c 65 20 61  ..    ** while a
407f2 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ny database conn
407f3 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 72  ection is open r
407f4 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69  esults in undefi
407f5 6e 65 64 20 61 6e 64 0d 0a 20 20 20 20 2a 2a 20  ned and..    ** 
407f6 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61  dileterious beha
407f7 76 69 6f 72 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  vior...    */.. 
407f8 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
407f9 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f  ESTCTRL_PENDING_
407fa 42 59 54 45 3a 20 7b 0d 0a 20 20 20 20 20 20 72  BYTE: {..      r
407fb 63 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  c = PENDING_BYTE
407fc 3b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
407fd 45 5f 4f 4d 49 54 5f 57 53 44 0d 0a 20 20 20 20  E_OMIT_WSD..    
407fe 20 20 7b 0d 0a 20 20 20 20 20 20 20 20 75 6e 73    {..        uns
407ff 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 56 61 6c  igned int newVal
40800 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 75 6e   = va_arg(ap, un
40801 73 69 67 6e 65 64 20 69 6e 74 29 3b 0d 0a 20 20  signed int);..  
40802 20 20 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c        if( newVal
40803 20 29 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e   ) sqlite3Pendin
40804 67 42 79 74 65 20 3d 20 6e 65 77 56 61 6c 3b 0d  gByte = newVal;.
40805 0a 20 20 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  .      }..#endif
40806 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
40807 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 0d      }....    /*.
40808 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
40809 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51  _test_control(SQ
4080a 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53  LITE_TESTCTRL_AS
4080b 53 45 52 54 2c 20 69 6e 74 20 58 29 0d 0a 20 20  SERT, int X)..  
4080c 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 69    **..    ** Thi
4080d 73 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  s action provide
4080e 73 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73  s a run-time tes
4080f 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
40810 20 6f 72 20 6e 6f 74 0d 0a 20 20 20 20 2a 2a 20   or not..    ** 
40811 61 73 73 65 72 74 28 29 20 77 61 73 20 65 6e 61  assert() was ena
40812 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  bled at compile-
40813 74 69 6d 65 2e 20 20 49 66 20 58 20 69 73 20 74  time.  If X is t
40814 72 75 65 20 61 6e 64 20 61 73 73 65 72 74 28 29  rue and assert()
40815 0d 0a 20 20 20 20 2a 2a 20 69 73 20 65 6e 61 62  ..    ** is enab
40816 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72 65  led, then the re
40817 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 72  turn value is tr
40818 75 65 2e 20 20 49 66 20 58 20 69 73 20 74 72 75  ue.  If X is tru
40819 65 20 61 6e 64 0d 0a 20 20 20 20 2a 2a 20 61 73  e and..    ** as
4081a 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
4081b 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74  ed, then the ret
4081c 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a 65 72  urn value is zer
4081d 6f 2e 20 20 49 66 20 58 20 69 73 0d 0a 20 20 20  o.  If X is..   
4081e 20 2a 2a 20 66 61 6c 73 65 20 61 6e 64 20 61 73   ** false and as
4081f 73 65 72 74 28 29 20 69 73 20 65 6e 61 62 6c 65  sert() is enable
40820 64 2c 20 74 68 65 6e 20 74 68 65 20 61 73 73 65  d, then the asse
40821 72 74 69 6f 6e 20 66 69 72 65 73 20 61 6e 64 20  rtion fires and 
40822 74 68 65 0d 0a 20 20 20 20 2a 2a 20 70 72 6f 63  the..    ** proc
40823 65 73 73 20 61 62 6f 72 74 73 2e 20 20 49 66 20  ess aborts.  If 
40824 58 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 61  X is false and a
40825 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62  ssert() is disab
40826 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0d 0a 20  led, then the.. 
40827 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c     ** return val
40828 75 65 20 69 73 20 7a 65 72 6f 2e 0d 0a 20 20 20  ue is zero...   
40829 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53 51   */..    case SQ
4082a 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53  LITE_TESTCTRL_AS
4082b 53 45 52 54 3a 20 7b 0d 0a 20 20 20 20 20 20 76  SERT: {..      v
4082c 6f 6c 61 74 69 6c 65 20 69 6e 74 20 78 20 3d 20  olatile int x = 
4082d 30 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  0;..      assert
4082e 28 20 28 78 20 3d 20 76 61 5f 61 72 67 28 61 70  ( (x = va_arg(ap
4082f 2c 69 6e 74 29 29 21 3d 30 20 29 3b 0d 0a 20 20  ,int))!=0 );..  
40830 20 20 20 20 72 63 20 3d 20 78 3b 0d 0a 20 20 20      rc = x;..   
40831 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
40832 0d 0a 0d 0a 0d 0a 20 20 20 20 2f 2a 0d 0a 20 20  ......    /*..  
40833 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65    **  sqlite3_te
40834 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54  st_control(SQLIT
40835 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59  E_TESTCTRL_ALWAY
40836 53 2c 20 69 6e 74 20 58 29 0d 0a 20 20 20 20 2a  S, int X)..    *
40837 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 61  *..    ** This a
40838 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61  ction provides a
40839 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 74   run-time test t
4083a 6f 20 73 65 65 20 68 6f 77 20 74 68 65 20 41 4c  o see how the AL
4083b 57 41 59 53 20 61 6e 64 0d 0a 20 20 20 20 2a 2a  WAYS and..    **
4083c 20 4e 45 56 45 52 20 6d 61 63 72 6f 73 20 77 65   NEVER macros we
4083d 72 65 20 64 65 66 69 6e 65 64 20 61 74 20 63 6f  re defined at co
4083e 6d 70 69 6c 65 2d 74 69 6d 65 2e 0d 0a 20 20 20  mpile-time...   
4083f 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20   **..    ** The 
40840 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
40841 41 4c 57 41 59 53 28 58 29 2e 20 20 0d 0a 20 20  ALWAYS(X).  ..  
40842 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65    **..    ** The
40843 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 65 73   recommended tes
40844 74 20 69 73 20 58 3d 3d 32 2e 20 20 49 66 20 74  t is X==2.  If t
40845 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
40846 69 73 20 32 2c 20 74 68 61 74 20 6d 65 61 6e 73  is 2, that means
40847 0d 0a 20 20 20 20 2a 2a 20 41 4c 57 41 59 53 28  ..    ** ALWAYS(
40848 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 61 72  ) and NEVER() ar
40849 65 20 62 6f 74 68 20 6e 6f 2d 6f 70 20 70 61 73  e both no-op pas
4084a 73 2d 74 68 72 6f 75 67 68 20 6d 61 63 72 6f 73  s-through macros
4084b 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 0d 0a  , which is the..
4084c 20 20 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 73      ** default s
4084d 65 74 74 69 6e 67 2e 20 20 49 66 20 74 68 65 20  etting.  If the 
4084e 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
4084f 31 2c 20 74 68 65 6e 20 41 4c 57 41 59 53 28 29  1, then ALWAYS()
40850 20 69 73 20 65 69 74 68 65 72 0d 0a 20 20 20 20   is either..    
40851 2a 2a 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f  ** hard-coded to
40852 20 74 72 75 65 20 6f 72 20 65 6c 73 65 20 69 74   true or else it
40853 20 61 73 73 65 72 74 73 20 69 66 20 69 74 73 20   asserts if its 
40854 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73  argument is fals
40855 65 2e 0d 0a 20 20 20 20 2a 2a 20 54 68 65 20 66  e...    ** The f
40856 69 72 73 74 20 62 65 68 61 76 69 6f 72 20 28 68  irst behavior (h
40857 61 72 64 2d 63 6f 64 65 64 20 74 6f 20 74 72 75  ard-coded to tru
40858 65 29 20 69 73 20 74 68 65 20 63 61 73 65 20 69  e) is the case i
40859 66 0d 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  f..    ** SQLITE
4085a 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54  _TESTCTRL_ASSERT
4085b 20 73 68 6f 77 73 20 74 68 61 74 20 61 73 73 65   shows that asse
4085c 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64  rt() is disabled
4085d 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0d   and the second.
4085e 0a 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72  .    ** behavior
4085f 20 28 61 73 73 65 72 74 20 69 66 20 74 68 65 20   (assert if the 
40860 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41  argument to ALWA
40861 59 53 28 29 20 69 73 20 66 61 6c 73 65 29 20 69  YS() is false) i
40862 73 20 74 68 65 20 63 61 73 65 20 69 66 0d 0a 20  s the case if.. 
40863 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53     ** SQLITE_TES
40864 54 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68 6f  TCTRL_ASSERT sho
40865 77 73 20 74 68 61 74 20 61 73 73 65 72 74 28 29  ws that assert()
40866 20 69 73 20 65 6e 61 62 6c 65 64 2e 0d 0a 20 20   is enabled...  
40867 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 54 68 65    **..    ** The
40868 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 70   run-time test p
40869 72 6f 63 65 64 75 72 65 20 6d 69 67 68 74 20 6c  rocedure might l
4086a 6f 6f 6b 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69  ook something li
4086b 6b 65 20 74 68 69 73 3a 0d 0a 20 20 20 20 2a 2a  ke this:..    **
4086c 0d 0a 20 20 20 20 2a 2a 20 20 20 20 69 66 28 20  ..    **    if( 
4086d 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
4086e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
4086f 43 54 52 4c 5f 41 4c 57 41 59 53 2c 20 32 29 3d  CTRL_ALWAYS, 2)=
40870 3d 32 20 29 7b 0d 0a 20 20 20 20 2a 2a 20 20 20  =2 ){..    **   
40871 20 20 20 2f 2f 20 41 4c 57 41 59 53 28 29 20 61     // ALWAYS() a
40872 6e 64 20 4e 45 56 45 52 28 29 20 61 72 65 20 6e  nd NEVER() are n
40873 6f 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f 75 67  o-op pass-throug
40874 68 20 6d 61 63 72 6f 73 0d 0a 20 20 20 20 2a 2a  h macros..    **
40875 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71      }else if( sq
40876 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
40877 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ol(SQLITE_TESTCT
40878 52 4c 5f 41 53 53 45 52 54 2c 20 31 29 20 29 7b  RL_ASSERT, 1) ){
40879 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f  ..    **      //
4087a 20 41 4c 57 41 59 53 28 78 29 20 61 73 73 65 72   ALWAYS(x) asser
4087b 74 73 20 74 68 61 74 20 78 20 69 73 20 74 72 75  ts that x is tru
4087c 65 2e 20 4e 45 56 45 52 28 78 29 20 61 73 73 65  e. NEVER(x) asse
4087d 72 74 73 20 78 20 69 73 20 66 61 6c 73 65 2e 0d  rts x is false..
4087e 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65 6c 73 65  .    **    }else
4087f 7b 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f  {..    **      /
40880 2f 20 41 4c 57 41 59 53 28 78 29 20 69 73 20 61  / ALWAYS(x) is a
40881 20 63 6f 6e 73 74 61 6e 74 20 31 2e 20 20 4e 45   constant 1.  NE
40882 56 45 52 28 78 29 20 69 73 20 61 20 63 6f 6e 73  VER(x) is a cons
40883 74 61 6e 74 20 30 2e 0d 0a 20 20 20 20 2a 2a 20  tant 0...    ** 
40884 20 20 20 7d 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20     }..    */..  
40885 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
40886 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 3a 20 7b  STCTRL_ALWAYS: {
40887 0d 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20  ..      int x = 
40888 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0d  va_arg(ap,int);.
40889 0a 20 20 20 20 20 20 72 63 20 3d 20 41 4c 57 41  .      rc = ALWA
4088a 59 53 28 78 29 3b 0d 0a 20 20 20 20 20 20 62 72  YS(x);..      br
4088b 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  eak;..    }.... 
4088c 20 20 20 2f 2a 20 20 20 73 71 6c 69 74 65 33 5f     /*   sqlite3_
4088d 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c  test_control(SQL
4088e 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53  ITE_TESTCTRL_RES
4088f 45 52 56 45 2c 20 73 71 6c 69 74 65 33 20 2a 64  ERVE, sqlite3 *d
40890 62 2c 20 69 6e 74 20 4e 29 0d 0a 20 20 20 20 2a  b, int N)..    *
40891 2a 0d 0a 20 20 20 20 2a 2a 20 53 65 74 20 74 68  *..    ** Set th
40892 65 20 6e 52 65 73 65 72 76 65 20 73 69 7a 65 20  e nReserve size 
40893 74 6f 20 4e 20 66 6f 72 20 74 68 65 20 6d 61 69  to N for the mai
40894 6e 20 64 61 74 61 62 61 73 65 20 6f 6e 20 74 68  n database on th
40895 65 20 64 61 74 61 62 61 73 65 0d 0a 20 20 20 20  e database..    
40896 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  ** connection db
40897 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  ...    */..    c
40898 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
40899 54 52 4c 5f 52 45 53 45 52 56 45 3a 20 7b 0d 0a  TRL_RESERVE: {..
4089a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
4089b 62 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 73  b = va_arg(ap, s
4089c 71 6c 69 74 65 33 2a 29 3b 0d 0a 20 20 20 20 20  qlite3*);..     
4089d 20 69 6e 74 20 78 20 3d 20 76 61 5f 61 72 67 28   int x = va_arg(
4089e 61 70 2c 69 6e 74 29 3b 0d 0a 20 20 20 20 20 20  ap,int);..      
4089f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
408a0 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ter(db->mutex);.
408a1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
408a2 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 64  reeSetPageSize(d
408a3 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 2c 20 30  b->aDb[0].pBt, 0
408a4 2c 20 78 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  , x, 0);..      
408a5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
408a6 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0d  ave(db->mutex);.
408a7 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
408a8 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 20     }....    /*  
408a9 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
408aa 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
408ab 43 54 52 4c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CTRL_OPTIMIZATIO
408ac 4e 53 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  NS, sqlite3 *db,
408ad 20 69 6e 74 20 4e 29 0d 0a 20 20 20 20 2a 2a 0d   int N)..    **.
408ae 0a 20 20 20 20 2a 2a 20 45 6e 61 62 6c 65 20 6f  .    ** Enable o
408af 72 20 64 69 73 61 62 6c 65 20 76 61 72 69 6f 75  r disable variou
408b0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  s optimizations 
408b1 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  for testing purp
408b2 6f 73 65 73 2e 20 20 54 68 65 20 0d 0a 20 20 20  oses.  The ..   
408b3 20 2a 2a 20 61 72 67 75 6d 65 6e 74 20 4e 20 69   ** argument N i
408b4 73 20 61 20 62 69 74 6d 61 73 6b 20 6f 66 20 6f  s a bitmask of o
408b5 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20  ptimizations to 
408b6 62 65 20 64 69 73 61 62 6c 65 64 2e 20 20 46 6f  be disabled.  Fo
408b7 72 20 6e 6f 72 6d 61 6c 0d 0a 20 20 20 20 2a 2a  r normal..    **
408b8 20 6f 70 65 72 61 74 69 6f 6e 20 4e 20 73 68 6f   operation N sho
408b9 75 6c 64 20 62 65 20 30 2e 20 20 54 68 65 20 69  uld be 0.  The i
408ba 64 65 61 20 69 73 20 74 68 61 74 20 61 20 74 65  dea is that a te
408bb 73 74 20 70 72 6f 67 72 61 6d 20 28 6c 69 6b 65  st program (like
408bc 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 53 51 4c   the..    ** SQL
408bd 20 4c 6f 67 69 63 20 54 65 73 74 20 6f 72 20 53   Logic Test or S
408be 4c 54 20 74 65 73 74 20 6d 6f 64 75 6c 65 29 20  LT test module) 
408bf 63 61 6e 20 72 75 6e 20 74 68 65 20 73 61 6d 65  can run the same
408c0 20 53 51 4c 20 6d 75 6c 74 69 70 6c 65 20 74 69   SQL multiple ti
408c1 6d 65 73 0d 0a 20 20 20 20 2a 2a 20 77 69 74 68  mes..    ** with
408c2 20 76 61 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a   various optimiz
408c3 61 74 69 6f 6e 73 20 64 69 73 61 62 6c 65 64 20  ations disabled 
408c4 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74  to verify that t
408c5 68 65 20 73 61 6d 65 20 61 6e 73 77 65 72 0d 0a  he same answer..
408c6 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e      ** is obtain
408c7 65 64 20 69 6e 20 65 76 65 72 79 20 63 61 73 65  ed in every case
408c8 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 63  ...    */..    c
408c9 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
408ca 54 52 4c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  TRL_OPTIMIZATION
408cb 53 3a 20 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  S: {..      sqli
408cc 74 65 33 20 2a 64 62 20 3d 20 76 61 5f 61 72 67  te3 *db = va_arg
408cd 28 61 70 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0d  (ap, sqlite3*);.
408ce 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 76  .      int x = v
408cf 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0d 0a  a_arg(ap,int);..
408d0 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20        db->flags 
408d1 3d 20 28 78 20 26 20 53 51 4c 49 54 45 5f 4f 70  = (x & SQLITE_Op
408d2 74 4d 61 73 6b 29 20 7c 20 28 64 62 2d 3e 66 6c  tMask) | (db->fl
408d3 61 67 73 20 26 20 7e 53 51 4c 49 54 45 5f 4f 70  ags & ~SQLITE_Op
408d4 74 4d 61 73 6b 29 3b 0d 0a 20 20 20 20 20 20 62  tMask);..      b
408d5 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  reak;..    }....
408d6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f  #ifdef SQLITE_N_
408d7 4b 45 59 57 4f 52 44 0d 0a 20 20 20 20 2f 2a 20  KEYWORD..    /* 
408d8 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
408d9 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
408da 43 54 52 4c 5f 49 53 4b 45 59 57 4f 52 44 2c 20  CTRL_ISKEYWORD, 
408db 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 6f 72  const char *zWor
408dc 64 29 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  d)..    **..    
408dd 2a 2a 20 49 66 20 7a 57 6f 72 64 20 69 73 20 61  ** If zWord is a
408de 20 6b 65 79 77 6f 72 64 20 72 65 63 6f 67 6e 69   keyword recogni
408df 7a 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  zed by the parse
408e0 72 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74  r, then return t
408e1 68 65 0d 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65  he..    ** numbe
408e2 72 20 6f 66 20 6b 65 79 77 6f 72 64 73 2e 20 20  r of keywords.  
408e3 4f 72 20 69 66 20 7a 57 6f 72 64 20 69 73 20 6e  Or if zWord is n
408e4 6f 74 20 61 20 6b 65 79 77 6f 72 64 2c 20 72 65  ot a keyword, re
408e5 74 75 72 6e 20 30 2e 0d 0a 20 20 20 20 2a 2a 20  turn 0...    ** 
408e6 0d 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 74 65  ..    ** This te
408e7 73 74 20 66 65 61 74 75 72 65 20 69 73 20 6f 6e  st feature is on
408e8 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ly available in 
408e9 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
408ea 20 73 69 6e 63 65 0d 0a 20 20 20 20 2a 2a 20 74   since..    ** t
408eb 68 65 20 53 51 4c 49 54 45 5f 4e 5f 4b 45 59 57  he SQLITE_N_KEYW
408ec 4f 52 44 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74  ORD macro is not
408ed 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73   defined in this
408ee 20 66 69 6c 65 20 69 66 20 53 51 4c 69 74 65 0d   file if SQLite.
408ef 0a 20 20 20 20 2a 2a 20 69 73 20 62 75 69 6c 74  .    ** is built
408f0 20 75 73 69 6e 67 20 73 65 70 61 72 61 74 65 20   using separate 
408f1 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 0d 0a 20  source files... 
408f2 20 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20     */..    case 
408f3 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
408f4 49 53 4b 45 59 57 4f 52 44 3a 20 7b 0d 0a 20 20  ISKEYWORD: {..  
408f5 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
408f6 7a 57 6f 72 64 20 3d 20 76 61 5f 61 72 67 28 61  zWord = va_arg(a
408f7 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  p, const char*);
408f8 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  ..      int n = 
408f9 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
408fa 7a 57 6f 72 64 29 3b 0d 0a 20 20 20 20 20 20 72  zWord);..      r
408fb 63 20 3d 20 28 73 71 6c 69 74 65 33 4b 65 79 77  c = (sqlite3Keyw
408fc 6f 72 64 43 6f 64 65 28 28 75 38 2a 29 7a 57 6f  ordCode((u8*)zWo
408fd 72 64 2c 20 6e 29 21 3d 54 4b 5f 49 44 29 20 3f  rd, n)!=TK_ID) ?
408fe 20 53 51 4c 49 54 45 5f 4e 5f 4b 45 59 57 4f 52   SQLITE_N_KEYWOR
408ff 44 20 3a 20 30 3b 0d 0a 20 20 20 20 20 20 62 72  D : 0;..      br
40900 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e  eak;..    }..#en
40901 64 69 66 20 0d 0a 0d 0a 20 20 20 20 2f 2a 20 73  dif ....    /* s
40902 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
40903 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
40904 54 52 4c 5f 53 43 52 41 54 43 48 4d 41 4c 4c 4f  TRL_SCRATCHMALLO
40905 43 2c 20 73 7a 2c 20 26 70 4e 65 77 2c 20 70 46  C, sz, &pNew, pF
40906 72 65 65 29 3b 0d 0a 20 20 20 20 2a 2a 0d 0a 20  ree);..    **.. 
40907 20 20 20 2a 2a 20 50 61 73 73 20 70 46 72 65 65     ** Pass pFree
40908 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 53 63 72   into sqlite3Scr
40909 61 74 63 68 46 72 65 65 28 29 2e 20 0d 0a 20 20  atchFree(). ..  
4090a 20 20 2a 2a 20 49 66 20 73 7a 3e 30 20 74 68 65    ** If sz>0 the
4090b 6e 20 61 6c 6c 6f 63 61 74 65 20 61 20 73 63 72  n allocate a scr
4090c 61 74 63 68 20 62 75 66 66 65 72 20 69 6e 74 6f  atch buffer into
4090d 20 70 4e 65 77 2e 20 20 0d 0a 20 20 20 20 2a 2f   pNew.  ..    */
4090e 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
4090f 45 5f 54 45 53 54 43 54 52 4c 5f 53 43 52 41 54  E_TESTCTRL_SCRAT
40910 43 48 4d 41 4c 4c 4f 43 3a 20 7b 0d 0a 20 20 20  CHMALLOC: {..   
40911 20 20 20 76 6f 69 64 20 2a 70 46 72 65 65 2c 20     void *pFree, 
40912 2a 2a 70 70 4e 65 77 3b 0d 0a 20 20 20 20 20 20  **ppNew;..      
40913 69 6e 74 20 73 7a 3b 0d 0a 20 20 20 20 20 20 73  int sz;..      s
40914 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  z = va_arg(ap, i
40915 6e 74 29 3b 0d 0a 20 20 20 20 20 20 70 70 4e 65  nt);..      ppNe
40916 77 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  w = va_arg(ap, v
40917 6f 69 64 2a 2a 29 3b 0d 0a 20 20 20 20 20 20 70  oid**);..      p
40918 46 72 65 65 20 3d 20 76 61 5f 61 72 67 28 61 70  Free = va_arg(ap
40919 2c 20 76 6f 69 64 2a 29 3b 0d 0a 20 20 20 20 20  , void*);..     
4091a 20 69 66 28 20 73 7a 20 29 20 2a 70 70 4e 65 77   if( sz ) *ppNew
4091b 20 3d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63   = sqlite3Scratc
4091c 68 4d 61 6c 6c 6f 63 28 73 7a 29 3b 0d 0a 20 20  hMalloc(sz);..  
4091d 20 20 20 20 73 71 6c 69 74 65 33 53 63 72 61 74      sqlite3Scrat
4091e 63 68 46 72 65 65 28 70 46 72 65 65 29 3b 0d 0a  chFree(pFree);..
4091f 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
40920 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 20 20    }....    /*   
40921 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
40922 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
40923 43 54 52 4c 5f 4c 4f 43 41 4c 54 49 4d 45 5f 46  CTRL_LOCALTIME_F
40924 41 55 4c 54 2c 20 69 6e 74 20 6f 6e 6f 66 66 29  AULT, int onoff)
40925 3b 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  ;..    **..    *
40926 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6f  * If parameter o
40927 6e 6f 66 66 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  noff is non-zero
40928 2c 20 63 6f 6e 66 69 67 75 72 65 20 74 68 65 20  , configure the 
40929 77 72 61 70 70 65 72 73 20 73 6f 20 74 68 61 74  wrappers so that
4092a 20 61 6c 6c 0d 0a 20 20 20 20 2a 2a 20 73 75 62   all..    ** sub
4092b 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
4092c 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 61 6e 64   localtime() and
4092d 20 76 61 72 69 61 6e 74 73 20 66 61 69 6c 2e 20   variants fail. 
4092e 49 66 20 6f 6e 6f 66 66 20 69 73 20 7a 65 72 6f  If onoff is zero
4092f 2c 0d 0a 20 20 20 20 2a 2a 20 75 6e 64 6f 20 74  ,..    ** undo t
40930 68 69 73 20 73 65 74 74 69 6e 67 2e 0d 0a 20 20  his setting...  
40931 20 20 2a 2f 0d 0a 20 20 20 20 63 61 73 65 20 53    */..    case S
40932 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 4c  QLITE_TESTCTRL_L
40933 4f 43 41 4c 54 49 4d 45 5f 46 41 55 4c 54 3a 20  OCALTIME_FAULT: 
40934 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
40935 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4c 6f  GlobalConfig.bLo
40936 63 61 6c 74 69 6d 65 46 61 75 6c 74 20 3d 20 76  caltimeFault = v
40937 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0d  a_arg(ap, int);.
40938 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  .      break;.. 
40939 20 20 20 7d 0d 0a 0d 0a 23 69 66 20 64 65 66 69     }....#if defi
4093a 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
4093b 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 29 0d  E_TREE_EXPLAIN).
4093c 0a 20 20 20 20 2f 2a 20 20 20 73 71 6c 69 74 65  .    /*   sqlite
4093d 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53  3_test_control(S
4093e 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 45  QLITE_TESTCTRL_E
4093f 58 50 4c 41 49 4e 5f 53 54 4d 54 2c 0d 0a 20 20  XPLAIN_STMT,..  
40940 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
40941 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
40942 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74 20  te3_stmt*,const 
40943 63 68 61 72 2a 2a 29 3b 0d 0a 20 20 20 20 2a 2a  char**);..    **
40944 0d 0a 20 20 20 20 2a 2a 20 49 66 20 63 6f 6d 70  ..    ** If comp
40945 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45  iled with SQLITE
40946 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f 45 58 50  _ENABLE_TREE_EXP
40947 4c 41 49 4e 2c 20 65 61 63 68 20 73 71 6c 69 74  LAIN, each sqlit
40948 65 33 5f 73 74 6d 74 20 68 6f 6c 64 73 0d 0a 20  e3_stmt holds.. 
40949 20 20 20 2a 2a 20 61 20 73 74 72 69 6e 67 20 74     ** a string t
4094a 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
4094b 65 20 6f 70 74 69 6d 69 7a 65 64 20 70 61 72 73  e optimized pars
4094c 65 20 74 72 65 65 2e 20 20 54 68 69 73 20 74 65  e tree.  This te
4094d 73 74 2d 63 6f 6e 74 72 6f 6c 0d 0a 20 20 20 20  st-control..    
4094e 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ** returns a poi
4094f 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72  nter to that str
40950 69 6e 67 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  ing...    */..  
40951 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
40952 53 54 43 54 52 4c 5f 45 58 50 4c 41 49 4e 5f 53  STCTRL_EXPLAIN_S
40953 54 4d 54 3a 20 7b 0d 0a 20 20 20 20 20 20 73 71  TMT: {..      sq
40954 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
40955 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 73  t = va_arg(ap, s
40956 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0d 0a  qlite3_stmt*);..
40957 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
40958 20 2a 2a 70 7a 52 65 74 20 3d 20 76 61 5f 61 72   **pzRet = va_ar
40959 67 28 61 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  g(ap, const char
4095a 2a 2a 29 3b 0d 0a 20 20 20 20 20 20 2a 70 7a 52  **);..      *pzR
4095b 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  et = sqlite3Vdbe
4095c 45 78 70 6c 61 6e 61 74 69 6f 6e 28 28 56 64 62  Explanation((Vdb
4095d 65 2a 29 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20  e*)pStmt);..    
4095e 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d    break;..    }.
4095f 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20 7d 0d 0a  .#endif....  }..
40960 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0d 0a 23    va_end(ap);..#
40961 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
40962 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
40963 54 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 72  T */..  return r
40964 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
40965 54 68 69 73 20 69 73 20 61 20 75 74 69 6c 69 74  This is a utilit
40966 79 20 72 6f 75 74 69 6e 65 2c 20 75 73 65 66 75  y routine, usefu
40967 6c 20 74 6f 20 56 46 53 20 69 6d 70 6c 65 6d 65  l to VFS impleme
40968 6e 74 61 74 69 6f 6e 73 2c 20 74 68 61 74 20 63  ntations, that c
40969 68 65 63 6b 73 0d 0a 2a 2a 20 74 6f 20 73 65 65  hecks..** to see
4096a 20 69 66 20 61 20 64 61 74 61 62 61 73 65 20 66   if a database f
4096b 69 6c 65 20 77 61 73 20 61 20 55 52 49 20 74 68  ile was a URI th
4096c 61 74 20 63 6f 6e 74 61 69 6e 65 64 20 61 20 73  at contained a s
4096d 70 65 63 69 66 69 63 20 71 75 65 72 79 20 0d 0a  pecific query ..
4096e 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 61 6e  ** parameter, an
4096f 64 20 69 66 20 73 6f 20 6f 62 74 61 69 6e 73 20  d if so obtains 
40970 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
40971 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72   query parameter
40972 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 7a 46  ...**..** The zF
40973 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74  ilename argument
40974 20 69 73 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   is the filename
40975 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 20   pointer passed 
40976 69 6e 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29  into the xOpen()
40977 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 61  ..** method of a
40978 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
40979 69 6f 6e 2e 20 20 54 68 65 20 7a 50 61 72 61 6d  ion.  The zParam
4097a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
4097b 20 6e 61 6d 65 20 6f 66 20 74 68 65 0d 0a 2a 2a   name of the..**
4097c 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72   query parameter
4097d 20 77 65 20 73 65 65 6b 2e 20 20 54 68 69 73 20   we seek.  This 
4097e 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
4097f 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
40980 20 7a 50 61 72 61 6d 0d 0a 2a 2a 20 70 61 72 61   zParam..** para
40981 6d 65 74 65 72 20 69 66 20 69 74 20 65 78 69 73  meter if it exis
40982 74 73 2e 20 20 49 66 20 74 68 65 20 70 61 72 61  ts.  If the para
40983 6d 65 74 65 72 20 64 6f 65 73 20 6e 6f 74 20 65  meter does not e
40984 78 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74 69  xist, this routi
40985 6e 65 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  ne..** returns a
40986 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d 0a   NULL pointer...
40987 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  */..SQLITE_API c
40988 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
40989 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65 72  e3_uri_parameter
4098a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
4098b 6c 65 6e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68  lename, const ch
4098c 61 72 20 2a 7a 50 61 72 61 6d 29 7b 0d 0a 20 20  ar *zParam){..  
4098d 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
4098e 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20   ) return 0;..  
4098f 7a 46 69 6c 65 6e 61 6d 65 20 2b 3d 20 73 71 6c  zFilename += sql
40990 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69  ite3Strlen30(zFi
40991 6c 65 6e 61 6d 65 29 20 2b 20 31 3b 0d 0a 20 20  lename) + 1;..  
40992 77 68 69 6c 65 28 20 7a 46 69 6c 65 6e 61 6d 65  while( zFilename
40993 5b 30 5d 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  [0] ){..    int 
40994 78 20 3d 20 73 74 72 63 6d 70 28 7a 46 69 6c 65  x = strcmp(zFile
40995 6e 61 6d 65 2c 20 7a 50 61 72 61 6d 29 3b 0d 0a  name, zParam);..
40996 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 2b 3d      zFilename +=
40997 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
40998 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 31 3b  (zFilename) + 1;
40999 0d 0a 20 20 20 20 69 66 28 20 78 3d 3d 30 20 29  ..    if( x==0 )
4099a 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d   return zFilenam
4099b 65 3b 0d 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d  e;..    zFilenam
4099c 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  e += sqlite3Strl
4099d 65 6e 33 30 28 7a 46 69 6c 65 6e 61 6d 65 29 20  en30(zFilename) 
4099e 2b 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74  + 1;..  }..  ret
4099f 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  urn 0;..}..../*.
409a0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6f 6f  .** Return a boo
409a1 6c 65 61 6e 20 76 61 6c 75 65 20 66 6f 72 20 61  lean value for a
409a2 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72   query parameter
409a3 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
409a4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 72  I int sqlite3_ur
409a5 69 5f 62 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20  i_boolean(const 
409a6 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
409a7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
409a8 72 61 6d 2c 20 69 6e 74 20 62 44 66 6c 74 29 7b  ram, int bDflt){
409a9 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
409aa 7a 20 3d 20 73 71 6c 69 74 65 33 5f 75 72 69 5f  z = sqlite3_uri_
409ab 70 61 72 61 6d 65 74 65 72 28 7a 46 69 6c 65 6e  parameter(zFilen
409ac 61 6d 65 2c 20 7a 50 61 72 61 6d 29 3b 0d 0a 20  ame, zParam);.. 
409ad 20 72 65 74 75 72 6e 20 7a 20 3f 20 73 71 6c 69   return z ? sqli
409ae 74 65 33 47 65 74 42 6f 6f 6c 65 61 6e 28 7a 29  te3GetBoolean(z)
409af 20 3a 20 28 62 44 66 6c 74 21 3d 30 29 3b 0d 0a   : (bDflt!=0);..
409b0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
409b1 72 6e 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  rn a 64-bit inte
409b2 67 65 72 20 76 61 6c 75 65 20 66 6f 72 20 61 20  ger value for a 
409b3 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 2e  query parameter.
409b4 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
409b5 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
409b6 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e 74 36 34  qlite3_uri_int64
409b7 28 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  (..  const char 
409b8 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f  *zFilename,    /
409b9 2a 20 46 69 6c 65 6e 61 6d 65 20 61 73 20 70 61  * Filename as pa
409ba 73 73 65 64 20 74 6f 20 78 4f 70 65 6e 20 2a 2f  ssed to xOpen */
409bb 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
409bc 7a 50 61 72 61 6d 2c 20 20 20 20 20 20 20 2f 2a  zParam,       /*
409bd 20 55 52 49 20 70 61 72 61 6d 65 74 65 72 20 73   URI parameter s
409be 6f 75 67 68 74 20 2a 2f 0d 0a 20 20 73 71 6c 69  ought */..  sqli
409bf 74 65 33 5f 69 6e 74 36 34 20 62 44 66 6c 74 20  te3_int64 bDflt 
409c0 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20        /* return 
409c1 69 66 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  if parameter is 
409c2 6d 69 73 73 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a  missing */..){..
409c3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
409c4 3d 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 70 61  = sqlite3_uri_pa
409c5 72 61 6d 65 74 65 72 28 7a 46 69 6c 65 6e 61 6d  rameter(zFilenam
409c6 65 2c 20 7a 50 61 72 61 6d 29 3b 0d 0a 20 20 73  e, zParam);..  s
409c7 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 76 3b 0d  qlite3_int64 v;.
409c8 0a 20 20 69 66 28 20 7a 20 26 26 20 73 71 6c 69  .  if( z && sqli
409c9 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 2c  te3Atoi64(z, &v,
409ca 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
409cb 28 7a 29 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (z), SQLITE_UTF8
409cc 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  )==SQLITE_OK ){.
409cd 0a 20 20 20 20 62 44 66 6c 74 20 3d 20 76 3b 0d  .    bDflt = v;.
409ce 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 62  .  }..  return b
409cf 44 66 6c 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  Dflt;..}..../*..
409d0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69  ** Return the fi
409d1 6c 65 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  lename of the da
409d2 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65  tabase associate
409d3 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
409d4 65 0d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  e..** connection
409d5 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50  ...*/..SQLITE_AP
409d6 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
409d7 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65 6e 61 6d  lite3_db_filenam
409d8 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  e(sqlite3 *db, c
409d9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61  onst char *zDbNa
409da 6d 65 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  me){..  int i;..
409db 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
409dc 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  >nDb; i++){..   
409dd 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
409de 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 53 74  pBt && sqlite3St
409df 72 49 43 6d 70 28 7a 44 62 4e 61 6d 65 2c 20 64  rICmp(zDbName, d
409e0 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29  b->aDb[i].zName)
409e1 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  ==0 ){..      re
409e2 74 75 72 6e 20 73 71 6c 69 74 65 33 42 74 72 65  turn sqlite3Btre
409e3 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d  eGetFilename(db-
409e4 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0d 0a 20  >aDb[i].pBt);.. 
409e5 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
409e6 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 2a  urn 0;..}..../**
409e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
409e8 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   of main.c *****
409e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
409ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
409eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
409ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
409ed 67 69 6e 20 66 69 6c 65 20 6e 6f 74 69 66 79 2e  gin file notify.
409ee 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
409ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
409f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
409f1 2a 0d 0a 2a 2a 20 32 30 30 39 20 4d 61 72 63 68  *..** 2009 March
409f2 20 33 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61   3..**..** The a
409f3 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
409f4 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
409f5 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
409f6 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20  In place of..** 
409f7 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
409f8 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
409f9 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d  ng:..**..**    M
409fa 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
409fb 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a  nd not evil...**
409fc 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
409fd 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
409fe 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
409ff 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a 2a  rgive others...*
40a00 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
40a01 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
40a02 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
40a03 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d  n you give...**.
40a04 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
40a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a  **********..**..
40a09 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
40a0a 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d  tains the implem
40a0b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
40a0c 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
40a0d 6f 74 69 66 79 28 29 0d 0a 2a 2a 20 41 50 49 20  otify()..** API 
40a0e 6d 65 74 68 6f 64 20 61 6e 64 20 69 74 73 20 61  method and its a
40a0f 73 73 6f 63 69 61 74 65 64 20 66 75 6e 63 74 69  ssociated functi
40a10 6f 6e 61 6c 69 74 79 2e 0d 0a 2a 2f 0d 0a 0d 0a  onality...*/....
40a11 2f 2a 20 4f 6d 69 74 20 74 68 69 73 20 65 6e 74  /* Omit this ent
40a12 69 72 65 20 66 69 6c 65 20 69 66 20 53 51 4c 49  ire file if SQLI
40a13 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b  TE_ENABLE_UNLOCK
40a14 5f 4e 4f 54 49 46 59 20 69 73 20 6e 6f 74 20 64  _NOTIFY is not d
40a15 65 66 69 6e 65 64 2e 20 2a 2f 0d 0a 23 69 66 64  efined. */..#ifd
40a16 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
40a17 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0d 0a  _UNLOCK_NOTIFY..
40a18 0d 0a 2f 2a 0d 0a 2a 2a 20 50 75 62 6c 69 63 20  ../*..** Public 
40a19 69 6e 74 65 72 66 61 63 65 73 3a 0d 0a 2a 2a 0d  interfaces:..**.
40a1a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
40a1b 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29  nectionBlocked()
40a1c 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f  ..**   sqlite3Co
40a1d 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
40a1e 28 29 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  ()..**   sqlite3
40a1f 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
40a20 28 29 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  ()..**   sqlite3
40a21 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
40a22 0d 0a 2a 2f 0d 0a 0d 0a 23 64 65 66 69 6e 65 20  ..*/....#define 
40a23 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64 28  assertMutexHeld(
40a24 29 20 5c 0d 0a 20 20 61 73 73 65 72 74 28 20 73  ) \..  assert( s
40a25 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
40a26 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  d(sqlite3MutexAl
40a27 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
40a28 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
40a29 20 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 61   )..../*..** Hea
40a2a 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d of a linked li
40a2b 73 74 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65  st of all sqlite
40a2c 33 20 6f 62 6a 65 63 74 73 20 63 72 65 61 74 65  3 objects create
40a2d 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 73  d by this proces
40a2e 73 0d 0a 2a 2a 20 66 6f 72 20 77 68 69 63 68 20  s..** for which 
40a2f 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 2e 70  either sqlite3.p
40a30 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
40a31 6f 6e 20 6f 72 20 73 71 6c 69 74 65 33 2e 70 55  on or sqlite3.pU
40a32 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 0d  nlockConnection.
40a33 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e  .** is not NULL.
40a34 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6d   This variable m
40a35 61 79 20 6f 6e 6c 79 20 61 63 63 65 73 73 65 64  ay only accessed
40a36 20 77 68 69 6c 65 20 74 68 65 20 53 54 41 54 49   while the STATI
40a37 43 5f 4d 41 53 54 45 52 0d 0a 2a 2a 20 6d 75 74  C_MASTER..** mut
40a38 65 78 20 69 73 20 68 65 6c 64 2e 0d 0a 2a 2f 0d  ex is held...*/.
40a39 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 20  .static sqlite3 
40a3a 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69  *SQLITE_WSD sqli
40a3b 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d  te3BlockedList =
40a3c 20 30 3b 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4e   0;....#ifndef N
40a3d 44 45 42 55 47 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  DEBUG../*..** Th
40a3e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
40a3f 20 63 6f 6d 70 6c 65 78 20 61 73 73 65 72 74 28   complex assert(
40a40 29 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20  ) that verifies 
40a41 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0d 0a  the following ..
40a42 2a 2a 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66  ** properties of
40a43 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
40a44 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 3a 0d 0a  nections list:..
40a45 2a 2a 0d 0a 2a 2a 20 20 20 31 29 20 45 61 63 68  **..**   1) Each
40a46 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6c 69   entry in the li
40a47 73 74 20 68 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c  st has a non-NUL
40a48 4c 20 76 61 6c 75 65 20 66 6f 72 20 65 69 74 68  L value for eith
40a49 65 72 20 0d 0a 2a 2a 20 20 20 20 20 20 70 55 6e  er ..**      pUn
40a4a 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f  lockConnection o
40a4b 72 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  r pBlockingConne
40a4c 63 74 69 6f 6e 2c 20 6f 72 20 62 6f 74 68 2e 0d  ction, or both..
40a4d 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 29 20 41 6c 6c  .**..**   2) All
40a4e 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
40a4f 6c 69 73 74 20 74 68 61 74 20 73 68 61 72 65 20  list that share 
40a50 61 20 63 6f 6d 6d 6f 6e 20 76 61 6c 75 65 20 66  a common value f
40a51 6f 72 20 0d 0a 2a 2a 20 20 20 20 20 20 78 55 6e  or ..**      xUn
40a52 6c 6f 63 6b 4e 6f 74 69 66 79 20 61 72 65 20 67  lockNotify are g
40a53 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 2e  rouped together.
40a54 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 29 20 49 66  ..**..**   3) If
40a55 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 64 62   the argument db
40a56 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
40a57 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65  en none of the e
40a58 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0d 0a 2a  ntries in the..*
40a59 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65 64 20 63  *      blocked c
40a5a 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 20  onnections list 
40a5b 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  have pUnlockConn
40a5c 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b  ection or pBlock
40a5d 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0d 0a 2a  ingConnection..*
40a5e 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
40a5f 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 77  . This is used w
40a60 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e 6e  hen closing conn
40a61 65 63 74 69 6f 6e 20 64 62 2e 0d 0a 2a 2f 0d 0a  ection db...*/..
40a62 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
40a63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28  kListProperties(
40a64 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20  sqlite3 *db){.. 
40a65 20 73 71 6c 69 74 65 33 20 2a 70 3b 0d 0a 20 20   sqlite3 *p;..  
40a66 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f  for(p=sqlite3Blo
40a67 63 6b 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70  ckedList; p; p=p
40a68 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b  ->pNextBlocked){
40a69 0d 0a 20 20 20 20 69 6e 74 20 73 65 65 6e 20 3d  ..    int seen =
40a6a 20 30 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   0;..    sqlite3
40a6b 20 2a 70 32 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20   *p2;....    /* 
40a6c 56 65 72 69 66 79 20 70 72 6f 70 65 72 74 79 20  Verify property 
40a6d 28 31 29 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65  (1) */..    asse
40a6e 72 74 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f  rt( p->pUnlockCo
40a6f 6e 6e 65 63 74 69 6f 6e 20 7c 7c 20 70 2d 3e 70  nnection || p->p
40a70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
40a71 6f 6e 20 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20  on );....    /* 
40a72 56 65 72 69 66 79 20 70 72 6f 70 65 72 74 79 20  Verify property 
40a73 28 32 29 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28  (2) */..    for(
40a74 70 32 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p2=sqlite3Blocke
40a75 64 4c 69 73 74 3b 20 70 32 21 3d 70 3b 20 70 32  dList; p2!=p; p2
40a76 3d 70 32 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65  =p2->pNextBlocke
40a77 64 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70  d){..      if( p
40a78 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  2->xUnlockNotify
40a79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69  ==p->xUnlockNoti
40a7a 66 79 20 29 20 73 65 65 6e 20 3d 20 31 3b 0d 0a  fy ) seen = 1;..
40a7b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 32        assert( p2
40a7c 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d  ->xUnlockNotify=
40a7d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  =p->xUnlockNotif
40a7e 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b 0d 0a 20  y || !seen );.. 
40a7f 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 3d       assert( db=
40a80 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f 63 6b  =0 || p->pUnlock
40a81 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20 29  Connection!=db )
40a82 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
40a83 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42 6c   db==0 || p->pBl
40a84 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
40a85 21 3d 64 62 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a  !=db );..    }..
40a86 20 20 7d 0d 0a 7d 0d 0a 23 65 6c 73 65 0d 0a 23    }..}..#else..#
40a87 20 64 65 66 69 6e 65 20 63 68 65 63 6b 4c 69 73   define checkLis
40a88 74 50 72 6f 70 65 72 74 69 65 73 28 78 29 0d 0a  tProperties(x)..
40a89 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  #endif..../*..**
40a8a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69   Remove connecti
40a8b 6f 6e 20 64 62 20 66 72 6f 6d 20 74 68 65 20 62  on db from the b
40a8c 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
40a8d 6e 73 20 6c 69 73 74 2e 20 49 66 20 63 6f 6e 6e  ns list. If conn
40a8e 65 63 74 69 6f 6e 0d 0a 2a 2a 20 64 62 20 69 73  ection..** db is
40a8f 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
40a90 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73   part of the lis
40a91 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  t, this function
40a92 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2f   is a no-op...*/
40a93 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  ..static void re
40a94 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c  moveFromBlockedL
40a95 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ist(sqlite3 *db)
40a96 7b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  {..  sqlite3 **p
40a97 70 3b 0d 0a 20 20 61 73 73 65 72 74 4d 75 74 65  p;..  assertMute
40a98 78 48 65 6c 64 28 29 3b 0d 0a 20 20 66 6f 72 28  xHeld();..  for(
40a99 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b  pp=&sqlite3Block
40a9a 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70 70 20  edList; *pp; pp 
40a9b 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42  = &(*pp)->pNextB
40a9c 6c 6f 63 6b 65 64 29 7b 0d 0a 20 20 20 20 69 66  locked){..    if
40a9d 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0d 0a 20 20  ( *pp==db ){..  
40a9e 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d      *pp = (*pp)-
40a9f 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0d 0a  >pNextBlocked;..
40aa0 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
40aa1 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f    }..  }..}..../
40aa2 2a 0d 0a 2a 2a 20 41 64 64 20 63 6f 6e 6e 65 63  *..** Add connec
40aa3 74 69 6f 6e 20 64 62 20 74 6f 20 74 68 65 20 62  tion db to the b
40aa4 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
40aa5 6e 73 20 6c 69 73 74 2e 20 49 74 20 69 73 20 61  ns list. It is a
40aa6 73 73 75 6d 65 64 0d 0a 2a 2a 20 74 68 61 74 20  ssumed..** that 
40aa7 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  it is not alread
40aa8 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20  y a part of the 
40aa9 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  list...*/..stati
40aaa 63 20 76 6f 69 64 20 61 64 64 54 6f 42 6c 6f 63  c void addToBloc
40aab 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20  kedList(sqlite3 
40aac 2a 64 62 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  *db){..  sqlite3
40aad 20 2a 2a 70 70 3b 0d 0a 20 20 61 73 73 65 72 74   **pp;..  assert
40aae 4d 75 74 65 78 48 65 6c 64 28 29 3b 0d 0a 20 20  MutexHeld();..  
40aaf 66 6f 72 28 0d 0a 20 20 20 20 70 70 3d 26 73 71  for(..    pp=&sq
40ab0 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74  lite3BlockedList
40ab1 3b 20 0d 0a 20 20 20 20 2a 70 70 20 26 26 20 28  ; ..    *pp && (
40ab2 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  *pp)->xUnlockNot
40ab3 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63 6b  ify!=db->xUnlock
40ab4 4e 6f 74 69 66 79 3b 20 0d 0a 20 20 20 20 70 70  Notify; ..    pp
40ab5 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c  =&(*pp)->pNextBl
40ab6 6f 63 6b 65 64 0d 0a 20 20 29 3b 0d 0a 20 20 64  ocked..  );..  d
40ab7 62 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20  b->pNextBlocked 
40ab8 3d 20 2a 70 70 3b 0d 0a 20 20 2a 70 70 20 3d 20  = *pp;..  *pp = 
40ab9 64 62 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  db;..}..../*..**
40aba 20 4f 62 74 61 69 6e 20 74 68 65 20 53 54 41 54   Obtain the STAT
40abb 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e  IC_MASTER mutex.
40abc 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
40abd 64 20 65 6e 74 65 72 4d 75 74 65 78 28 76 6f 69  d enterMutex(voi
40abe 64 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  d){..  sqlite3_m
40abf 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
40ac0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
40ac1 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
40ac2 5f 4d 41 53 54 45 52 29 29 3b 0d 0a 20 20 63 68  _MASTER));..  ch
40ac3 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
40ac4 73 28 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  s(0);..}..../*..
40ac5 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 53  ** Release the S
40ac6 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
40ac7 65 78 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ex...*/..static 
40ac8 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65 78 28  void leaveMutex(
40ac9 76 6f 69 64 29 7b 0d 0a 20 20 61 73 73 65 72 74  void){..  assert
40aca 4d 75 74 65 78 48 65 6c 64 28 29 3b 0d 0a 20 20  MutexHeld();..  
40acb 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
40acc 69 65 73 28 30 29 3b 0d 0a 20 20 73 71 6c 69 74  ies(0);..  sqlit
40acd 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
40ace 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
40acf 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
40ad0 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0d 0a  ATIC_MASTER));..
40ad1 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67 69  }..../*..** Regi
40ad2 73 74 65 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e  ster an unlock-n
40ad3 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2e 0d  otify callback..
40ad4 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20  .**..** This is 
40ad5 63 61 6c 6c 65 64 20 61 66 74 65 72 20 63 6f 6e  called after con
40ad6 6e 65 63 74 69 6f 6e 20 22 64 62 22 20 68 61 73  nection "db" has
40ad7 20 61 74 74 65 6d 70 74 65 64 20 73 6f 6d 65 20   attempted some 
40ad8 6f 70 65 72 61 74 69 6f 6e 0d 0a 2a 2a 20 62 75  operation..** bu
40ad9 74 20 68 61 73 20 72 65 63 65 69 76 65 64 20 61  t has received a
40ada 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
40adb 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 61 6e  error because an
40adc 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
40add 0d 0a 2a 2a 20 28 63 61 6c 6c 20 69 74 20 70 4f  ..** (call it pO
40ade 74 68 65 72 29 20 69 6e 20 74 68 65 20 73 61 6d  ther) in the sam
40adf 65 20 70 72 6f 63 65 73 73 20 77 61 73 20 62 75  e process was bu
40ae0 73 79 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  sy using the sam
40ae1 65 20 73 68 61 72 65 64 0d 0a 2a 2a 20 63 61 63  e shared..** cac
40ae2 68 65 2e 20 20 70 4f 74 68 65 72 20 69 73 20 66  he.  pOther is f
40ae3 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20  ound by looking 
40ae4 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  at db->pBlocking
40ae5 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0d 0a 2a 2a 0d  Connection...**.
40ae6 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
40ae7 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  no blocking conn
40ae8 65 63 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c 6c  ection, the call
40ae9 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
40aea 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0d 0a 2a 2a  immediately,..**
40aeb 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
40aec 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0d 0a 2a  tine returns...*
40aed 2a 0d 0a 2a 2a 20 49 66 20 70 4f 74 68 65 72 20  *..** If pOther 
40aee 69 73 20 61 6c 72 65 61 64 79 20 62 6c 6f 63 6b  is already block
40aef 65 64 20 6f 6e 20 64 62 2c 20 74 68 65 6e 20 72  ed on db, then r
40af0 65 70 6f 72 74 20 53 51 4c 49 54 45 5f 4c 4f 43  eport SQLITE_LOC
40af1 4b 45 44 2c 20 74 6f 20 69 6e 64 69 63 61 74 65  KED, to indicate
40af2 0d 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2e  ..** a deadlock.
40af3 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69  ..**..** Otherwi
40af4 73 65 2c 20 6d 61 6b 65 20 61 72 72 61 6e 67 65  se, make arrange
40af5 6d 65 6e 74 73 20 74 6f 20 69 6e 76 6f 6b 65 20  ments to invoke 
40af6 78 4e 6f 74 69 66 79 20 77 68 65 6e 20 70 4f 74  xNotify when pOt
40af7 68 65 72 20 64 72 6f 70 73 0d 0a 2a 2a 20 69 74  her drops..** it
40af8 73 20 6c 6f 63 6b 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  s locks...**..**
40af9 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 74 68   Each call to th
40afa 69 73 20 72 6f 75 74 69 6e 65 20 6f 76 65 72 72  is routine overr
40afb 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 63  ides any prior c
40afc 61 6c 6c 62 61 63 6b 73 20 72 65 67 69 73 74 65  allbacks registe
40afd 72 65 64 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 73  red..** on the s
40afe 61 6d 65 20 22 64 62 22 2e 20 20 49 66 20 78 4e  ame "db".  If xN
40aff 6f 74 69 66 79 3d 3d 30 20 74 68 65 6e 20 61 6e  otify==0 then an
40b00 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61 63 6b  y prior callback
40b01 73 20 61 72 65 20 69 6d 6d 65 64 69 61 74 65 6c  s are immediatel
40b02 79 0d 0a 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 2e  y..** cancelled.
40b03 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49  ..*/..SQLITE_API
40b04 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c   int sqlite3_unl
40b05 6f 63 6b 5f 6e 6f 74 69 66 79 28 0d 0a 20 20 73  ock_notify(..  s
40b06 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20 20 76  qlite3 *db,..  v
40b07 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76  oid (*xNotify)(v
40b08 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0d 0a 20  oid **, int),.. 
40b09 20 76 6f 69 64 20 2a 70 41 72 67 0d 0a 29 7b 0d   void *pArg..){.
40b0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
40b0b 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 73 71 6c 69  TE_OK;....  sqli
40b0c 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
40b0d 64 62 2d 3e 6d 75 74 65 78 29 3b 0d 0a 20 20 65  db->mutex);..  e
40b0e 6e 74 65 72 4d 75 74 65 78 28 29 3b 0d 0a 0d 0a  nterMutex();....
40b0f 20 20 69 66 28 20 78 4e 6f 74 69 66 79 3d 3d 30    if( xNotify==0
40b10 20 29 7b 0d 0a 20 20 20 20 72 65 6d 6f 76 65 46   ){..    removeF
40b11 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64  romBlockedList(d
40b12 62 29 3b 0d 0a 20 20 20 20 64 62 2d 3e 70 42 6c  b);..    db->pBl
40b13 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
40b14 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62 2d 3e 70   = 0;..    db->p
40b15 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
40b16 20 3d 20 30 3b 0d 0a 20 20 20 20 64 62 2d 3e 78   = 0;..    db->x
40b17 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30  UnlockNotify = 0
40b18 3b 0d 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f  ;..    db->pUnlo
40b19 63 6b 41 72 67 20 3d 20 30 3b 0d 0a 20 20 7d 65  ckArg = 0;..  }e
40b1a 6c 73 65 20 69 66 28 20 30 3d 3d 64 62 2d 3e 70  lse if( 0==db->p
40b1b 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
40b1c 6f 6e 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  on ){..    /* Th
40b1d 65 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e 73  e blocking trans
40b1e 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20  action has been 
40b1f 63 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74 68  concluded. Or th
40b20 65 72 65 20 6e 65 76 65 72 20 77 61 73 20 61 20  ere never was a 
40b21 0d 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e  ..    ** blockin
40b22 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  g transaction. I
40b23 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 69  n either case, i
40b24 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79  nvoke the notify
40b25 20 63 61 6c 6c 62 61 63 6b 0d 0a 20 20 20 20 2a   callback..    *
40b26 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0d  * immediately. .
40b27 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 78 4e 6f  .    */..    xNo
40b28 74 69 66 79 28 26 70 41 72 67 2c 20 31 29 3b 0d  tify(&pArg, 1);.
40b29 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73  .  }else{..    s
40b2a 71 6c 69 74 65 33 20 2a 70 3b 0d 0a 0d 0a 20 20  qlite3 *p;....  
40b2b 20 20 66 6f 72 28 70 3d 64 62 2d 3e 70 42 6c 6f    for(p=db->pBlo
40b2c 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b  ckingConnection;
40b2d 20 70 20 26 26 20 70 21 3d 64 62 3b 20 70 3d 70   p && p!=db; p=p
40b2e 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
40b2f 69 6f 6e 29 7b 7d 0d 0a 20 20 20 20 69 66 28 20  ion){}..    if( 
40b30 70 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  p ){..      rc =
40b31 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20   SQLITE_LOCKED; 
40b32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40b33 44 65 61 64 6c 6f 63 6b 20 64 65 74 65 63 74 65  Deadlock detecte
40b34 64 2e 20 2a 2f 0d 0a 20 20 20 20 7d 65 6c 73 65  d. */..    }else
40b35 7b 0d 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e  {..      db->pUn
40b36 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  lockConnection =
40b37 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f   db->pBlockingCo
40b38 6e 6e 65 63 74 69 6f 6e 3b 0d 0a 20 20 20 20 20  nnection;..     
40b39 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69   db->xUnlockNoti
40b3a 66 79 20 3d 20 78 4e 6f 74 69 66 79 3b 0d 0a 20  fy = xNotify;.. 
40b3b 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b       db->pUnlock
40b3c 41 72 67 20 3d 20 70 41 72 67 3b 0d 0a 20 20 20  Arg = pArg;..   
40b3d 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f     removeFromBlo
40b3e 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0d 0a 20  ckedList(db);.. 
40b3f 20 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b 65       addToBlocke
40b40 64 4c 69 73 74 28 64 62 29 3b 0d 0a 20 20 20 20  dList(db);..    
40b41 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 6c 65 61 76  }..  }....  leav
40b42 65 4d 75 74 65 78 28 29 3b 0d 0a 20 20 61 73 73  eMutex();..  ass
40b43 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
40b44 46 61 69 6c 65 64 20 29 3b 0d 0a 20 20 73 71 6c  Failed );..  sql
40b45 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
40b46 2c 20 28 72 63 3f 22 64 61 74 61 62 61 73 65 20  , (rc?"database 
40b47 69 73 20 64 65 61 64 6c 6f 63 6b 65 64 22 3a 30  is deadlocked":0
40b48 29 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ));..  sqlite3_m
40b49 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
40b4a 75 74 65 78 29 3b 0d 0a 20 20 72 65 74 75 72 6e  utex);..  return
40b4b 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
40b4c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
40b4d 69 73 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20  is called while 
40b4e 73 74 65 70 70 69 6e 67 20 6f 72 20 70 72 65 70  stepping or prep
40b4f 61 72 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e  aring a statemen
40b50 74 20 0d 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  t ..** associate
40b51 64 20 77 69 74 68 20 63 6f 6e 6e 65 63 74 69 6f  d with connectio
40b52 6e 20 64 62 2e 20 54 68 65 20 6f 70 65 72 61 74  n db. The operat
40b53 69 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ion will return 
40b54 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 0d 0a 2a  SQLITE_LOCKED..*
40b55 2a 20 74 6f 20 74 68 65 20 75 73 65 72 20 62 65  * to the user be
40b56 63 61 75 73 65 20 69 74 20 72 65 71 75 69 72 65  cause it require
40b57 73 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 77 69  s a lock that wi
40b58 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61  ll not be availa
40b59 62 6c 65 0d 0a 2a 2a 20 75 6e 74 69 6c 20 63 6f  ble..** until co
40b5a 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63 6b 65  nnection pBlocke
40b5b 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20  r concludes its 
40b5c 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
40b5d 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ion...*/..SQLITE
40b5e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
40b5f 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
40b60 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
40b61 64 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  db, sqlite3 *pBl
40b62 6f 63 6b 65 72 29 7b 0d 0a 20 20 65 6e 74 65 72  ocker){..  enter
40b63 4d 75 74 65 78 28 29 3b 0d 0a 20 20 69 66 28 20  Mutex();..  if( 
40b64 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  db->pBlockingCon
40b65 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20 64 62  nection==0 && db
40b66 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
40b67 69 6f 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61  ion==0 ){..    a
40b68 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28  ddToBlockedList(
40b69 64 62 29 3b 0d 0a 20 20 7d 0d 0a 20 20 64 62 2d  db);..  }..  db-
40b6a 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
40b6b 74 69 6f 6e 20 3d 20 70 42 6c 6f 63 6b 65 72 3b  tion = pBlocker;
40b6c 0d 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  ..  leaveMutex()
40b6d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
40b6e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
40b6f 63 61 6c 6c 65 64 20 77 68 65 6e 0d 0a 2a 2a 20  called when..** 
40b70 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
40b71 6f 70 65 6e 65 64 20 62 79 20 64 61 74 61 62 61  opened by databa
40b72 73 65 20 64 62 20 68 61 73 20 6a 75 73 74 20 66  se db has just f
40b73 69 6e 69 73 68 65 64 2e 20 4c 6f 63 6b 73 20 68  inished. Locks h
40b74 65 6c 64 20 0d 0a 2a 2a 20 62 79 20 64 61 74 61  eld ..** by data
40b75 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
40b76 64 62 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c  db have been rel
40b77 65 61 73 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  eased...**..** T
40b78 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f  his function loo
40b79 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 20  ps through each 
40b7a 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62 6c 6f  entry in the blo
40b7b 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
40b7c 0d 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64 6f  ..** list and do
40b7d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
40b7e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31 29 20 49  :..**..**   1) I
40b7f 66 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 42  f the sqlite3.pB
40b80 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
40b81 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61 20 6c 69  n member of a li
40b82 73 74 20 65 6e 74 72 79 20 69 73 0d 0a 2a 2a 20  st entry is..** 
40b83 20 20 20 20 20 73 65 74 20 74 6f 20 64 62 2c 20       set to db, 
40b84 74 68 65 6e 20 73 65 74 20 70 42 6c 6f 63 6b 69  then set pBlocki
40b85 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0d  ngConnection=0..
40b86 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 29 20 49 66 20  .**..**   2) If 
40b87 74 68 65 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c  the sqlite3.pUnl
40b88 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65  ockConnection me
40b89 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65  mber of a list e
40b8a 6e 74 72 79 20 69 73 0d 0a 2a 2a 20 20 20 20 20  ntry is..**     
40b8b 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e   set to db, then
40b8c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6e 66   invoke the conf
40b8d 69 67 75 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f  igured unlock-no
40b8e 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 6e  tify callback an
40b8f 64 0d 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 70  d..**      set p
40b90 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
40b91 3d 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 29  =0...**..**   3)
40b92 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 65 70   If the two step
40b93 73 20 61 62 6f 76 65 20 6d 65 61 6e 20 74 68 61  s above mean tha
40b94 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  t pBlockingConne
40b95 63 74 69 6f 6e 3d 3d 30 20 61 6e 64 0d 0a 2a 2a  ction==0 and..**
40b96 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e        pUnlockCon
40b97 6e 65 63 74 69 6f 6e 3d 3d 30 2c 20 72 65 6d 6f  nection==0, remo
40b98 76 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f  ve the entry fro
40b99 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  m the blocked co
40b9a 6e 6e 65 63 74 69 6f 6e 73 0d 0a 2a 2a 20 20 20  nnections..**   
40b9b 20 20 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 53 51     list...*/..SQ
40b9c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
40b9d 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
40b9e 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 6c 69  ionUnlocked(sqli
40b9f 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 76 6f 69  te3 *db){..  voi
40ba0 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  d (*xUnlockNotif
40ba1 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29  y)(void **, int)
40ba2 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63 6b 2d   = 0; /* Unlock-
40ba3 6e 6f 74 69 66 79 20 63 62 20 74 6f 20 69 6e 76  notify cb to inv
40ba4 6f 6b 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41  oke */..  int nA
40ba5 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  rg = 0;         
40ba6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40ba7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
40ba8 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72 67 5b  entries in aArg[
40ba9 5d 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 20  ] */..  sqlite3 
40baa 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20  **pp;           
40bab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40bac 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
40bad 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 76 6f 69 64  iable */..  void
40bae 20 2a 2a 61 41 72 67 3b 20 20 20 20 20 20 20 20   **aArg;        
40baf 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
40bb0 6e 74 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63  nts to the unloc
40bb1 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 20  k callback */.. 
40bb2 20 76 6f 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30   void **aDyn = 0
40bb3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ;           /* D
40bb4 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
40bb5 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 61  ated space for a
40bb6 41 72 67 5b 5d 20 2a 2f 0d 0a 20 20 76 6f 69 64  Arg[] */..  void
40bb7 20 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b 20 20   *aStatic[16];  
40bb8 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 65         /* Starte
40bb9 72 20 73 70 61 63 65 20 66 6f 72 20 61 41 72 67  r space for aArg
40bba 5b 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72  [].  No malloc r
40bbb 65 71 75 69 72 65 64 20 2a 2f 0d 0a 0d 0a 20 20  equired */....  
40bbc 61 41 72 67 20 3d 20 61 53 74 61 74 69 63 3b 0d  aArg = aStatic;.
40bbd 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
40bbe 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 74 65           /* Ente
40bbf 72 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  r STATIC_MASTER 
40bc0 6d 75 74 65 78 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a  mutex */....  /*
40bc1 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20   This loop runs 
40bc2 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65 6e  once for each en
40bc3 74 72 79 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b  try in the block
40bc4 65 64 2d 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed-connections l
40bc5 69 73 74 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 70  ist. */..  for(p
40bc6 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p=&sqlite3Blocke
40bc7 64 4c 69 73 74 3b 20 2a 70 70 3b 20 2f 2a 20 6e  dList; *pp; /* n
40bc8 6f 2d 6f 70 20 2a 2f 20 29 7b 0d 0a 20 20 20 20  o-op */ ){..    
40bc9 73 71 6c 69 74 65 33 20 2a 70 20 3d 20 2a 70 70  sqlite3 *p = *pp
40bca 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 74 65 70  ;....    /* Step
40bcb 20 31 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20   1. */..    if( 
40bcc 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  p->pBlockingConn
40bcd 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0d 0a 20  ection==db ){.. 
40bce 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e       p->pBlockin
40bcf 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b  gConnection = 0;
40bd0 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f  ..    }....    /
40bd1 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0d 0a 20 20  * Step 2. */..  
40bd2 20 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b    if( p->pUnlock
40bd3 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29  Connection==db )
40bd4 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
40bd5 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66   p->xUnlockNotif
40bd6 79 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  y );..      if( 
40bd7 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
40bd8 21 3d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  !=xUnlockNotify 
40bd9 26 26 20 6e 41 72 67 21 3d 30 20 29 7b 0d 0a 20  && nArg!=0 ){.. 
40bda 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f         xUnlockNo
40bdb 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29  tify(aArg, nArg)
40bdc 3b 0d 0a 20 20 20 20 20 20 20 20 6e 41 72 67 20  ;..        nArg 
40bdd 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d  = 0;..      }...
40bde 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
40bdf 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
40be0 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74  );..      assert
40be1 28 20 61 41 72 67 3d 3d 61 44 79 6e 20 7c 7c 20  ( aArg==aDyn || 
40be2 28 61 44 79 6e 3d 3d 30 20 26 26 20 61 41 72 67  (aDyn==0 && aArg
40be3 3d 3d 61 53 74 61 74 69 63 29 20 29 3b 0d 0a 20  ==aStatic) );.. 
40be4 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72       assert( nAr
40be5 67 3c 3d 28 69 6e 74 29 41 72 72 61 79 53 69 7a  g<=(int)ArraySiz
40be6 65 28 61 53 74 61 74 69 63 29 20 7c 7c 20 61 41  e(aStatic) || aA
40be7 72 67 3d 3d 61 44 79 6e 20 29 3b 0d 0a 20 20 20  rg==aDyn );..   
40be8 20 20 20 69 66 28 20 28 21 61 44 79 6e 20 26 26     if( (!aDyn &&
40be9 20 6e 41 72 67 3d 3d 28 69 6e 74 29 41 72 72 61   nArg==(int)Arra
40bea 79 53 69 7a 65 28 61 53 74 61 74 69 63 29 29 0d  ySize(aStatic)).
40beb 0a 20 20 20 20 20 20 20 7c 7c 20 28 61 44 79 6e  .       || (aDyn
40bec 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74 29 28   && nArg==(int)(
40bed 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
40bee 65 28 61 44 79 6e 29 2f 73 69 7a 65 6f 66 28 76  e(aDyn)/sizeof(v
40bef 6f 69 64 2a 29 29 29 0d 0a 20 20 20 20 20 20 29  oid*)))..      )
40bf0 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  {..        /* Th
40bf1 65 20 61 41 72 67 5b 5d 20 61 72 72 61 79 20 6e  e aArg[] array n
40bf2 65 65 64 73 20 74 6f 20 67 72 6f 77 2e 20 2a 2f  eeds to grow. */
40bf3 0d 0a 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a  ..        void *
40bf4 2a 70 4e 65 77 20 3d 20 28 76 6f 69 64 20 2a 2a  *pNew = (void **
40bf5 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e  )sqlite3Malloc(n
40bf6 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20  Arg*sizeof(void 
40bf7 2a 29 2a 32 29 3b 0d 0a 20 20 20 20 20 20 20 20  *)*2);..        
40bf8 69 66 28 20 70 4e 65 77 20 29 7b 0d 0a 20 20 20  if( pNew ){..   
40bf9 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e         memcpy(pN
40bfa 65 77 2c 20 61 41 72 67 2c 20 6e 41 72 67 2a 73  ew, aArg, nArg*s
40bfb 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 29 3b 0d  izeof(void *));.
40bfc 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
40bfd 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0d 0a  e3_free(aDyn);..
40bfe 20 20 20 20 20 20 20 20 20 20 61 44 79 6e 20 3d            aDyn =
40bff 20 61 41 72 67 20 3d 20 70 4e 65 77 3b 0d 0a 20   aArg = pNew;.. 
40c00 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
40c01 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
40c02 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 65   occurs when the
40c03 20 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65 78   array of contex
40c04 74 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20  t pointers that 
40c05 6e 65 65 64 20 74 6f 0d 0a 20 20 20 20 20 20 20  need to..       
40c06 20 20 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20     ** be passed 
40c07 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  to the unlock-no
40c08 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
40c09 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
40c0a 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  ..          ** a
40c0b 53 74 61 74 69 63 5b 5d 20 61 72 72 61 79 20 61  Static[] array a
40c0c 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20  llocated on the 
40c0d 73 74 61 63 6b 20 61 6e 64 20 74 68 65 20 61 74  stack and the at
40c0e 74 65 6d 70 74 20 74 6f 20 0d 0a 20 20 20 20 20  tempt to ..     
40c0f 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65       ** allocate
40c10 20 61 20 6c 61 72 67 65 72 20 61 72 72 61 79 20   a larger array 
40c11 66 72 6f 6d 20 74 68 65 20 68 65 61 70 20 68 61  from the heap ha
40c12 73 20 66 61 69 6c 65 64 2e 0d 0a 20 20 20 20 20  s failed...     
40c13 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20       **..       
40c14 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20     ** This is a 
40c15 64 69 66 66 69 63 75 6c 74 20 73 69 74 75 61 74  difficult situat
40c16 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 2e 20 52  ion to handle. R
40c17 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
40c18 72 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  r..          ** 
40c19 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c  code to the call
40c1a 65 72 20 69 73 20 69 6e 73 75 66 66 69 63 69 65  er is insufficie
40c1b 6e 74 2c 20 61 73 20 65 76 65 6e 20 69 66 20 61  nt, as even if a
40c1c 6e 20 65 72 72 6f 72 20 63 6f 64 65 0d 0a 20 20  n error code..  
40c1d 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65          ** is re
40c1e 74 75 72 6e 65 64 20 74 68 65 20 74 72 61 6e 73  turned the trans
40c1f 61 63 74 69 6f 6e 20 6f 6e 20 63 6f 6e 6e 65 63  action on connec
40c20 74 69 6f 6e 20 64 62 20 77 69 6c 6c 20 73 74 69  tion db will sti
40c21 6c 6c 20 62 65 0d 0a 20 20 20 20 20 20 20 20 20  ll be..         
40c22 20 2a 2a 20 63 6c 6f 73 65 64 20 61 6e 64 20 74   ** closed and t
40c23 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  he unlock-notify
40c24 20 63 61 6c 6c 62 61 63 6b 73 20 6f 6e 20 62 6c   callbacks on bl
40c25 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
40c26 73 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  s..          ** 
40c27 77 69 6c 6c 20 67 6f 20 75 6e 69 73 73 75 65 64  will go unissued
40c28 2e 20 54 68 69 73 20 6d 69 67 68 74 20 63 61 75  . This might cau
40c29 73 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  se the applicati
40c2a 6f 6e 20 74 6f 20 77 61 69 74 0d 0a 20 20 20 20  on to wait..    
40c2b 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 66 69 6e        ** indefin
40c2c 69 74 65 6c 79 20 66 6f 72 20 61 6e 20 75 6e 6c  itely for an unl
40c2d 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
40c2e 61 63 6b 20 74 68 61 74 20 77 69 6c 6c 20 6e 65  ack that will ne
40c2f 76 65 72 20 0d 0a 20 20 20 20 20 20 20 20 20 20  ver ..          
40c30 2a 2a 20 61 72 72 69 76 65 2e 0d 0a 20 20 20 20  ** arrive...    
40c31 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
40c32 20 20 20 20 2a 2a 20 49 6e 73 74 65 61 64 2c 20      ** Instead, 
40c33 69 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c 6f 63  invoke the unloc
40c34 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
40c35 6b 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65  k with the conte
40c36 78 74 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  xt..          **
40c37 20 61 72 72 61 79 20 61 6c 72 65 61 64 79 20 61   array already a
40c38 63 63 75 6d 75 6c 61 74 65 64 2e 20 57 65 20 63  ccumulated. We c
40c39 61 6e 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  an then clear th
40c3a 65 20 61 72 72 61 79 20 61 6e 64 0d 0a 20 20 20  e array and..   
40c3b 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69 6e 20         ** begin 
40c3c 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79  accumulating any
40c3d 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65 78 74   further context
40c3e 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 6f 75   pointers withou
40c3f 74 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  t ..          **
40c40 20 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20 64   requiring any d
40c41 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f  ynamic allocatio
40c42 6e 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d 6f  n. This is sub-o
40c43 70 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0d 0a  ptimal because..
40c44 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20            ** it 
40c45 6d 65 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65  means that inste
40c46 61 64 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61  ad of one callba
40c47 63 6b 20 77 69 74 68 20 61 20 6c 61 72 67 65 20  ck with a large 
40c48 61 72 72 61 79 20 6f 66 0d 0a 20 20 20 20 20 20  array of..      
40c49 20 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70      ** context p
40c4a 6f 69 6e 74 65 72 73 20 74 68 65 20 61 70 70 6c  ointers the appl
40c4b 69 63 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63  ication will rec
40c4c 65 69 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65  eive two or more
40c4d 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  ..          ** c
40c4e 61 6c 6c 62 61 63 6b 73 20 77 69 74 68 20 73 6d  allbacks with sm
40c4f 61 6c 6c 65 72 20 61 72 72 61 79 73 20 6f 66 20  aller arrays of 
40c50 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73  context pointers
40c51 2c 20 77 68 69 63 68 20 77 69 6c 6c 0d 0a 20 20  , which will..  
40c52 20 20 20 20 20 20 20 20 2a 2a 20 72 65 64 75 63          ** reduc
40c53 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  e the applicatio
40c54 6e 73 20 61 62 69 6c 69 74 79 20 74 6f 20 70 72  ns ability to pr
40c55 69 6f 72 69 74 69 7a 65 20 6d 75 6c 74 69 70 6c  ioritize multipl
40c56 65 20 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  e ..          **
40c57 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 42 75   connections. Bu
40c58 74 20 69 74 20 69 73 20 74 68 65 20 62 65 73 74  t it is the best
40c59 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 6f 6e   that can be don
40c5a 65 20 75 6e 64 65 72 20 74 68 65 0d 0a 20 20 20  e under the..   
40c5b 20 20 20 20 20 20 20 2a 2a 20 63 69 72 63 75 6d         ** circum
40c5c 73 74 61 6e 63 65 73 2e 0d 0a 20 20 20 20 20 20  stances...      
40c5d 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20      */..        
40c5e 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28    xUnlockNotify(
40c5f 61 41 72 67 2c 20 6e 41 72 67 29 3b 0d 0a 20 20  aArg, nArg);..  
40c60 20 20 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30          nArg = 0
40c61 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
40c62 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c      }..      sql
40c63 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
40c64 6c 6f 63 28 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  loc();....      
40c65 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20 70  aArg[nArg++] = p
40c66 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b 0d 0a 20  ->pUnlockArg;.. 
40c67 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69       xUnlockNoti
40c68 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  fy = p->xUnlockN
40c69 6f 74 69 66 79 3b 0d 0a 20 20 20 20 20 20 70 2d  otify;..      p-
40c6a 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  >pUnlockConnecti
40c6b 6f 6e 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70  on = 0;..      p
40c6c 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
40c6d 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 70  = 0;..      p->p
40c6e 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0d 0a  UnlockArg = 0;..
40c6f 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20      }....    /* 
40c70 53 74 65 70 20 33 2e 20 2a 2f 0d 0a 20 20 20 20  Step 3. */..    
40c71 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67  if( p->pBlocking
40c72 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26  Connection==0 &&
40c73 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65   p->pUnlockConne
40c74 63 74 69 6f 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20  ction==0 ){..   
40c75 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e     /* Remove con
40c76 6e 65 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74  nection p from t
40c77 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
40c78 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0d  ctions list. */.
40c79 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e  .      *pp = p->
40c7a 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0d 0a 20  pNextBlocked;.. 
40c7b 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 42 6c 6f       p->pNextBlo
40c7c 63 6b 65 64 20 3d 20 30 3b 0d 0a 20 20 20 20 7d  cked = 0;..    }
40c7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 70 20  else{..      pp 
40c7e 3d 20 26 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b  = &p->pNextBlock
40c7f 65 64 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ed;..    }..  }.
40c80 0a 0d 0a 20 20 69 66 28 20 6e 41 72 67 21 3d 30  ...  if( nArg!=0
40c81 20 29 7b 0d 0a 20 20 20 20 78 55 6e 6c 6f 63 6b   ){..    xUnlock
40c82 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72  Notify(aArg, nAr
40c83 67 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69  g);..  }..  sqli
40c84 74 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0d  te3_free(aDyn);.
40c85 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b  .  leaveMutex();
40c86 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 76           /* Leav
40c87 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  e STATIC_MASTER 
40c88 6d 75 74 65 78 20 2a 2f 0d 0a 7d 0d 0a 0d 0a 2f  mutex */..}..../
40c89 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61  *..** This is ca
40c8a 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  lled when the da
40c8b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
40c8c 6e 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  n passed as an a
40c8d 72 67 75 6d 65 6e 74 20 69 73 20 0d 0a 2a 2a 20  rgument is ..** 
40c8e 62 65 69 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68  being closed. Th
40c8f 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
40c90 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
40c91 20 62 6c 6f 63 6b 65 64 20 6c 69 73 74 2e 0d 0a   blocked list...
40c92 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
40c93 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
40c94 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
40c95 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20  sqlite3 *db){.. 
40c96 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
40c97 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 29 3b 0d  onUnlocked(db);.
40c98 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
40c99 0d 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c  ..  removeFromBl
40c9a 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0d 0a  ockedList(db);..
40c9b 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65    checkListPrope
40c9c 72 74 69 65 73 28 64 62 29 3b 0d 0a 20 20 6c 65  rties(db);..  le
40c9d 61 76 65 4d 75 74 65 78 28 29 3b 0d 0a 7d 0d 0a  aveMutex();..}..
40c9e 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  #endif..../*****
40c9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
40ca0 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a   notify.c ******
40ca1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ca2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ca3 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
40ca4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
40ca5 20 66 69 6c 65 20 66 74 73 33 2e 63 20 2a 2a 2a   file fts3.c ***
40ca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ca7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ca8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
40ca9 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0d 0a  ** 2006 Oct 10..
40caa 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  **..** The autho
40cab 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
40cac 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
40cad 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
40cae 6c 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65  lace of..** a le
40caf 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
40cb0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d   is a blessing:.
40cb1 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  .**..**    May y
40cb2 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
40cb3 6f 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20  ot evil...**    
40cb4 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
40cb5 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
40cb6 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
40cb7 65 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20  e others...**   
40cb8 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
40cb9 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
40cba 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
40cbb 75 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a  u give...**..***
40cbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40cbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40cbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40cbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d  ***********..**.
40cc1 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53  .** This is an S
40cc2 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70  QLite module imp
40cc3 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74  lementing full-t
40cc4 65 78 74 20 73 65 61 72 63 68 2e 0d 0a 2a 2f 0d  ext search...*/.
40cc5 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f  .../*..** The co
40cc6 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
40cc7 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
40cc8 20 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20   if:..**..**    
40cc9 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
40cca 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
40ccb 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
40ccc 6e 0d 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20  n..**       (in 
40ccd 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
40cce 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65  E_CORE is not de
40ccf 66 69 6e 65 64 29 2c 20 6f 72 0d 0a 2a 2a 0d 0a  fined), or..**..
40cd0 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
40cd1 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
40cd2 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  g built into the
40cd3 20 63 6f 72 65 20 6f 66 0d 0a 2a 2a 20 20 20 20   core of..**    
40cd4 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68     SQLite (in wh
40cd5 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
40cd6 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64  ENABLE_FTS3 is d
40cd7 65 66 69 6e 65 64 29 2e 0d 0a 2a 2f 0d 0a 0d 0a  efined)...*/....
40cd8 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74 65 78 74  /* The full-text
40cd9 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72 65 64   index is stored
40cda 20 69 6e 20 61 20 73 65 72 69 65 73 20 6f 66 20   in a series of 
40cdb 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65 29 0d 0a  b+tree (-like)..
40cdc 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 63 61  ** structures ca
40cdd 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20 77 68  lled segments wh
40cde 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20 74 6f  ich map terms to
40cdf 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0d   doclists.  The.
40ce0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 61  .** structures a
40ce1 72 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73 20  re like b+trees 
40ce2 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20 61  in layout, but a
40ce3 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66  re constructed f
40ce4 72 6f 6d 20 74 68 65 0d 0a 2a 2a 20 62 6f 74 74  rom the..** bott
40ce5 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c  om up in optimal
40ce6 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65   fashion and are
40ce7 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20   not updatable. 
40ce8 20 53 69 6e 63 65 20 74 72 65 65 73 0d 0a 2a 2a   Since trees..**
40ce9 20 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d 20   are built from 
40cea 74 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74  the bottom up, t
40ceb 68 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64 65  hings will be de
40cec 73 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68 65  scribed from the
40ced 0d 0a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0d  ..** bottom up..
40cee 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 20 56 61  .**..**..**** Va
40cef 72 69 6e 74 73 20 2a 2a 2a 2a 0d 0a 2a 2a 20 54  rints ****..** T
40cf0 68 65 20 62 61 73 69 63 20 75 6e 69 74 20 6f 66  he basic unit of
40cf1 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 76   encoding is a v
40cf2 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
40cf3 6e 74 65 67 65 72 20 63 61 6c 6c 65 64 20 61 0d  nteger called a.
40cf4 0a 2a 2a 20 76 61 72 69 6e 74 2e 20 20 57 65 20  .** varint.  We 
40cf5 65 6e 63 6f 64 65 20 76 61 72 69 61 62 6c 65 2d  encode variable-
40cf6 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20  length integers 
40cf7 69 6e 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  in little-endian
40cf8 20 6f 72 64 65 72 0d 0a 2a 2a 20 75 73 69 6e 67   order..** using
40cf9 20 73 65 76 65 6e 20 62 69 74 73 20 2a 20 70 65   seven bits * pe
40cfa 72 20 62 79 74 65 20 61 73 20 66 6f 6c 6c 6f 77  r byte as follow
40cfb 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 4b 45 59 3a 0d  s:..**..** KEY:.
40cfc 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20  .**         A = 
40cfd 30 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  0xxxxxxx    7 bi
40cfe 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
40cff 6e 65 20 66 6c 61 67 20 62 69 74 0d 0a 2a 2a 20  ne flag bit..** 
40d00 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78 78          B = 1xxx
40d01 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f  xxxx    7 bits o
40d02 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66  f data and one f
40d03 6c 61 67 20 62 69 74 0d 0a 2a 2a 0d 0a 2a 2a 20  lag bit..**..** 
40d04 20 37 20 62 69 74 73 20 2d 20 41 0d 0a 2a 2a 20   7 bits - A..** 
40d05 31 34 20 62 69 74 73 20 2d 20 42 41 0d 0a 2a 2a  14 bits - BA..**
40d06 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0d 0a   21 bits - BBA..
40d07 2a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0d 0a 2a  ** and so on...*
40d08 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 69  *..** This is si
40d09 6d 69 6c 61 72 20 69 6e 20 63 6f 6e 63 65 70 74  milar in concept
40d0a 20 74 6f 20 68 6f 77 20 73 71 6c 69 74 65 20 65   to how sqlite e
40d0b 6e 63 6f 64 65 73 20 22 76 61 72 69 6e 74 73 22  ncodes "varints"
40d0c 20 62 75 74 0d 0a 2a 2a 20 74 68 65 20 65 6e 63   but..** the enc
40d0d 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20 74 68 65  oding is not the
40d0e 20 73 61 6d 65 2e 20 20 53 51 4c 69 74 65 20 76   same.  SQLite v
40d0f 61 72 69 6e 74 73 20 61 72 65 20 62 69 67 2d 65  arints are big-e
40d10 6e 64 69 61 6e 0d 0a 2a 2a 20 61 72 65 20 61 72  ndian..** are ar
40d11 65 20 6c 69 6d 69 74 65 64 20 74 6f 20 39 20 62  e limited to 9 b
40d12 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 20 77  ytes in length w
40d13 68 65 72 65 61 73 20 46 54 53 33 20 76 61 72 69  hereas FTS3 vari
40d14 6e 74 73 20 61 72 65 0d 0a 2a 2a 20 6c 69 74 74  nts are..** litt
40d15 6c 65 2d 65 6e 64 69 61 6e 20 61 6e 64 20 63 61  le-endian and ca
40d16 6e 20 62 65 20 75 70 20 74 6f 20 31 30 20 62 79  n be up to 10 by
40d17 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 20 28 69  tes in length (i
40d18 6e 20 74 68 65 6f 72 79 29 2e 0d 0a 2a 2a 0d 0a  n theory)...**..
40d19 2a 2a 20 45 78 61 6d 70 6c 65 20 65 6e 63 6f 64  ** Example encod
40d1a 69 6e 67 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ings:..**..**   
40d1b 20 20 31 3a 20 20 20 20 30 78 30 31 0d 0a 2a 2a    1:    0x01..**
40d1c 20 20 20 31 32 37 3a 20 20 20 20 30 78 37 66 0d     127:    0x7f.
40d1d 0a 2a 2a 20 20 20 31 32 38 3a 20 20 20 20 30 78  .**   128:    0x
40d1e 38 31 20 30 78 30 30 0d 0a 2a 2a 0d 0a 2a 2a 0d  81 0x00..**..**.
40d1f 0a 2a 2a 2a 2a 20 44 6f 63 75 6d 65 6e 74 20 6c  .**** Document l
40d20 69 73 74 73 20 2a 2a 2a 2a 0d 0a 2a 2a 20 41 20  ists ****..** A 
40d21 64 6f 63 6c 69 73 74 20 28 64 6f 63 75 6d 65 6e  doclist (documen
40d22 74 20 6c 69 73 74 29 20 68 6f 6c 64 73 20 61 20  t list) holds a 
40d23 64 6f 63 69 64 2d 73 6f 72 74 65 64 20 6c 69 73  docid-sorted lis
40d24 74 20 6f 66 20 68 69 74 73 20 66 6f 72 20 61 0d  t of hits for a.
40d25 0a 2a 2a 20 67 69 76 65 6e 20 74 65 72 6d 2e 20  .** given term. 
40d26 20 44 6f 63 6c 69 73 74 73 20 68 6f 6c 64 20 64   Doclists hold d
40d27 6f 63 69 64 73 20 61 6e 64 20 61 73 73 6f 63 69  ocids and associ
40d28 61 74 65 64 20 74 6f 6b 65 6e 20 70 6f 73 69 74  ated token posit
40d29 69 6f 6e 73 2e 0d 0a 2a 2a 20 41 20 64 6f 63 69  ions...** A doci
40d2a 64 20 69 73 20 74 68 65 20 75 6e 69 71 75 65 20  d is the unique 
40d2b 69 6e 74 65 67 65 72 20 69 64 65 6e 74 69 66 69  integer identifi
40d2c 65 72 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  er for a single 
40d2d 64 6f 63 75 6d 65 6e 74 2e 0d 0a 2a 2a 20 41 20  document...** A 
40d2e 70 6f 73 69 74 69 6f 6e 20 69 73 20 74 68 65 20  position is the 
40d2f 69 6e 64 65 78 20 6f 66 20 61 20 77 6f 72 64 20  index of a word 
40d30 77 69 74 68 69 6e 20 74 68 65 20 64 6f 63 75 6d  within the docum
40d31 65 6e 74 2e 20 20 54 68 65 20 66 69 72 73 74 20  ent.  The first 
40d32 0d 0a 2a 2a 20 77 6f 72 64 20 6f 66 20 74 68 65  ..** word of the
40d33 20 64 6f 63 75 6d 65 6e 74 20 68 61 73 20 61 20   document has a 
40d34 70 6f 73 69 74 69 6f 6e 20 6f 66 20 30 2e 0d 0a  position of 0...
40d35 2a 2a 0d 0a 2a 2a 20 46 54 53 33 20 75 73 65 64  **..** FTS3 used
40d36 20 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 73   to optionally s
40d37 74 6f 72 65 20 63 68 61 72 61 63 74 65 72 20 6f  tore character o
40d38 66 66 73 65 74 73 20 75 73 69 6e 67 20 61 20 63  ffsets using a c
40d39 6f 6d 70 69 6c 65 2d 74 69 6d 65 0d 0a 2a 2a 20  ompile-time..** 
40d3a 6f 70 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 61  option.  But tha
40d3b 74 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20  t functionality 
40d3c 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 73 75 70  is no longer sup
40d3d 70 6f 72 74 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ported...**..** 
40d3e 41 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f  A doclist is sto
40d3f 72 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0d 0a  red like this:..
40d40 2a 2a 0d 0a 2a 2a 20 61 72 72 61 79 20 7b 0d 0a  **..** array {..
40d41 2a 2a 20 20 20 76 61 72 69 6e 74 20 64 6f 63 69  **   varint doci
40d42 64 3b 0d 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b  d;..**   array {
40d43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40d44 28 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66  (position list f
40d45 6f 72 20 63 6f 6c 75 6d 6e 20 30 29 0d 0a 2a 2a  or column 0)..**
40d46 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f 73 69       varint posi
40d47 74 69 6f 6e 3b 20 20 20 20 20 28 32 20 6d 6f 72  tion;     (2 mor
40d48 65 20 74 68 61 6e 20 74 68 65 20 64 65 6c 74 61  e than the delta
40d49 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 70   from previous p
40d4a 6f 73 69 74 69 6f 6e 29 0d 0a 2a 2a 20 20 20 7d  osition)..**   }
40d4b 0d 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0d 0a  ..**   array {..
40d4c 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 50 4f  **     varint PO
40d4d 53 5f 43 4f 4c 55 4d 4e 3b 20 20 20 28 6d 61 72  S_COLUMN;   (mar
40d4e 6b 73 20 73 74 61 72 74 20 6f 66 20 70 6f 73 69  ks start of posi
40d4f 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 6e 65  tion list for ne
40d50 77 20 63 6f 6c 75 6d 6e 29 0d 0a 2a 2a 20 20 20  w column)..**   
40d51 20 20 76 61 72 69 6e 74 20 63 6f 6c 75 6d 6e 3b    varint column;
40d52 20 20 20 20 20 20 20 28 69 6e 64 65 78 20 6f 66         (index of
40d53 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0d 0a 2a 2a   new column)..**
40d54 20 20 20 20 20 61 72 72 61 79 20 7b 0d 0a 2a 2a       array {..**
40d55 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f         varint po
40d56 73 69 74 69 6f 6e 3b 20 20 20 28 32 20 6d 6f 72  sition;   (2 mor
40d57 65 20 74 68 61 6e 20 74 68 65 20 64 65 6c 74 61  e than the delta
40d58 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 70   from previous p
40d59 6f 73 69 74 69 6f 6e 29 0d 0a 2a 2a 20 20 20 20  osition)..**    
40d5a 20 7d 0d 0a 2a 2a 20 20 20 7d 0d 0a 2a 2a 20 20   }..**   }..**  
40d5b 20 76 61 72 69 6e 74 20 50 4f 53 5f 45 4e 44 3b   varint POS_END;
40d5c 20 20 20 20 20 20 20 20 28 6d 61 72 6b 73 20 65          (marks e
40d5d 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20  nd of positions 
40d5e 66 6f 72 20 74 68 69 73 20 64 6f 63 75 6d 65 6e  for this documen
40d5f 74 2e 0d 0a 2a 2a 20 7d 0d 0a 2a 2a 0d 0a 2a 2a  t...** }..**..**
40d60 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
40d61 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
40d62 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
40d63 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
40d64 20 69 6e 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20   in..** memory. 
40d65 20 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69 73   A "position" is
40d66 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20 74   an index of a t
40d67 6f 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65  oken in the toke
40d68 6e 20 73 74 72 65 61 6d 0d 0a 2a 2a 20 67 65 6e  n stream..** gen
40d69 65 72 61 74 65 64 20 62 79 20 74 68 65 20 74 6f  erated by the to
40d6a 6b 65 6e 69 7a 65 72 2e 20 4e 6f 74 65 20 74 68  kenizer. Note th
40d6b 61 74 20 50 4f 53 5f 45 4e 44 20 61 6e 64 20 50  at POS_END and P
40d6c 4f 53 5f 43 4f 4c 55 4d 4e 20 6f 63 63 75 72 20  OS_COLUMN occur 
40d6d 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 73 61 6d 65  ..** in the same
40d6e 20 6c 6f 67 69 63 61 6c 20 70 6c 61 63 65 20 61   logical place a
40d6f 73 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 65  s the position e
40d70 6c 65 6d 65 6e 74 2c 20 61 6e 64 20 61 63 74 20  lement, and act 
40d71 61 73 20 73 65 6e 74 69 6e 61 6c 73 0d 0a 2a 2a  as sentinals..**
40d72 20 65 6e 64 69 6e 67 20 61 20 70 6f 73 69 74 69   ending a positi
40d73 6f 6e 20 6c 69 73 74 20 61 72 72 61 79 2e 20 20  on list array.  
40d74 50 4f 53 5f 45 4e 44 20 69 73 20 30 2e 20 20 50  POS_END is 0.  P
40d75 4f 53 5f 43 4f 4c 55 4d 4e 20 69 73 20 31 2e 0d  OS_COLUMN is 1..
40d76 0a 2a 2a 20 54 68 65 20 70 6f 73 69 74 69 6f 6e  .** The position
40d77 73 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6e 6f  s numbers are no
40d78 74 20 73 74 6f 72 65 64 20 6c 69 74 65 72 61 6c  t stored literal
40d79 6c 79 20 62 75 74 20 72 61 74 68 65 72 20 61 73  ly but rather as
40d7a 20 74 77 6f 20 6d 6f 72 65 0d 0a 2a 2a 20 74 68   two more..** th
40d7b 61 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  an the differenc
40d7c 65 20 66 72 6f 6d 20 74 68 65 20 70 72 69 6f 72  e from the prior
40d7d 20 70 6f 73 69 74 69 6f 6e 2c 20 6f 72 20 74 68   position, or th
40d7e 65 20 6a 75 73 74 20 74 68 65 20 70 6f 73 69 74  e just the posit
40d7f 69 6f 6e 20 70 6c 75 73 0d 0a 2a 2a 20 32 20 66  ion plus..** 2 f
40d80 6f 72 20 74 68 65 20 66 69 72 73 74 20 70 6f 73  or the first pos
40d81 69 74 69 6f 6e 2e 20 20 45 78 61 6d 70 6c 65 3a  ition.  Example:
40d82 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 6c 61 62 65 6c  ..**..**   label
40d83 3a 20 20 20 20 20 20 20 41 20 42 20 43 20 44 20  :       A B C D 
40d84 45 20 20 46 20 20 47 20 48 20 20 20 49 20 20 4a  E  F  G H   I  J
40d85 20 4b 0d 0a 2a 2a 20 20 20 76 61 6c 75 65 3a 20   K..**   value: 
40d86 20 20 20 20 31 32 33 20 35 20 39 20 31 20 31 20      123 5 9 1 1 
40d87 31 34 20 33 35 20 30 20 32 33 34 20 37 32 20 30  14 35 0 234 72 0
40d88 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 31 32 33  ..**..** The 123
40d89 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 66 69   value is the fi
40d8a 72 73 74 20 64 6f 63 69 64 2e 20 20 46 6f 72 20  rst docid.  For 
40d8b 63 6f 6c 75 6d 6e 20 7a 65 72 6f 20 69 6e 20 74  column zero in t
40d8c 68 69 73 20 64 6f 63 75 6d 65 6e 74 0d 0a 2a 2a  his document..**
40d8d 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6d   there are two m
40d8e 61 74 63 68 65 73 20 61 74 20 70 6f 73 69 74 69  atches at positi
40d8f 6f 6e 73 20 33 20 61 6e 64 20 31 30 20 28 35 2d  ons 3 and 10 (5-
40d90 32 20 61 6e 64 20 39 2d 32 2b 33 29 2e 20 20 54  2 and 9-2+3).  T
40d91 68 65 20 31 0d 0a 2a 2a 20 61 74 20 44 20 73 69  he 1..** at D si
40d92 67 6e 61 6c 73 20 74 68 65 20 73 74 61 72 74 20  gnals the start 
40d93 6f 66 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 3b  of a new column;
40d94 20 74 68 65 20 31 20 61 74 20 45 20 69 6e 64 69   the 1 at E indi
40d95 63 61 74 65 73 20 74 68 61 74 20 74 68 65 0d 0a  cates that the..
40d96 2a 2a 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  ** new column is
40d97 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 31   column number 1
40d98 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 77 6f  .  There are two
40d99 20 70 6f 73 69 74 69 6f 6e 73 20 61 74 20 31 32   positions at 12
40d9a 20 61 6e 64 20 34 35 0d 0a 2a 2a 20 28 31 34 2d   and 45..** (14-
40d9b 32 20 61 6e 64 20 33 35 2d 32 2b 31 32 29 2e 20  2 and 35-2+12). 
40d9c 20 54 68 65 20 30 20 61 74 20 48 20 69 6e 64 69   The 0 at H indi
40d9d 63 61 74 65 20 74 68 65 20 65 6e 64 2d 6f 66 2d  cate the end-of-
40d9e 64 6f 63 75 6d 65 6e 74 2e 20 20 54 68 65 0d 0a  document.  The..
40d9f 2a 2a 20 32 33 34 20 61 74 20 49 20 69 73 20 74  ** 234 at I is t
40da0 68 65 20 6e 65 78 74 20 64 6f 63 69 64 2e 20 20  he next docid.  
40da1 49 74 20 68 61 73 20 6f 6e 65 20 70 6f 73 69 74  It has one posit
40da2 69 6f 6e 20 37 32 20 28 37 32 2d 32 29 20 61 6e  ion 72 (72-2) an
40da3 64 20 74 68 65 6e 0d 0a 2a 2a 20 74 65 72 6d 69  d then..** termi
40da4 6e 61 74 65 73 20 77 69 74 68 20 74 68 65 20 30  nates with the 0
40da5 20 61 74 20 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41   at K...**..** A
40da6 20 22 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 22   "position-list"
40da7 20 69 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20   is the list of 
40da8 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 6d 75  positions for mu
40da9 6c 74 69 70 6c 65 20 63 6f 6c 75 6d 6e 73 20 66  ltiple columns f
40daa 6f 72 0d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  or..** a single 
40dab 64 6f 63 69 64 2e 20 20 41 20 22 63 6f 6c 75 6d  docid.  A "colum
40dac 6e 2d 6c 69 73 74 22 20 69 73 20 74 68 65 20 73  n-list" is the s
40dad 65 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20  et of positions 
40dae 66 6f 72 20 61 20 73 69 6e 67 6c 65 0d 0a 2a 2a  for a single..**
40daf 20 63 6f 6c 75 6d 6e 2e 20 20 48 65 6e 63 65 2c   column.  Hence,
40db0 20 61 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74   a position-list
40db1 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65   consists of one
40db2 20 6f 72 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 2d   or more column-
40db3 6c 69 73 74 73 2c 0d 0a 2a 2a 20 61 20 64 6f 63  lists,..** a doc
40db4 75 6d 65 6e 74 20 72 65 63 6f 72 64 20 63 6f 6e  ument record con
40db5 73 69 73 74 73 20 6f 66 20 61 20 64 6f 63 69 64  sists of a docid
40db6 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 70   followed by a p
40db7 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 61 6e 64  osition-list and
40db8 0d 0a 2a 2a 20 61 20 64 6f 63 6c 69 73 74 20 63  ..** a doclist c
40db9 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f  onsists of one o
40dba 72 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74 20  r more document 
40dbb 72 65 63 6f 72 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a  records...**..**
40dbc 20 41 20 62 61 72 65 20 64 6f 63 6c 69 73 74 20   A bare doclist 
40dbd 6f 6d 69 74 73 20 74 68 65 20 70 6f 73 69 74 69  omits the positi
40dbe 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20  on information, 
40dbf 62 65 63 6f 6d 69 6e 67 20 61 6e 20 0d 0a 2a 2a  becoming an ..**
40dc0 20 61 72 72 61 79 20 6f 66 20 76 61 72 69 6e 74   array of varint
40dc1 2d 65 6e 63 6f 64 65 64 20 64 6f 63 69 64 73 2e  -encoded docids.
40dc2 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 20 53 65 67 6d 65  ..**..**** Segme
40dc3 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a  nt leaf nodes **
40dc4 2a 2a 0d 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  **..** Segment l
40dc5 65 61 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  eaf nodes store 
40dc6 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73  terms and doclis
40dc7 74 73 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74  ts, ordered by t
40dc8 65 72 6d 2e 20 20 4c 65 61 66 0d 0a 2a 2a 20 6e  erm.  Leaf..** n
40dc9 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  odes are written
40dca 20 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65   using LeafWrite
40dcb 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e  r, and read usin
40dcc 67 20 4c 65 61 66 52 65 61 64 65 72 20 28 74 6f  g LeafReader (to
40dcd 0d 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72  ..** iterate thr
40dce 6f 75 67 68 20 61 20 73 69 6e 67 6c 65 20 6c 65  ough a single le
40dcf 61 66 20 6e 6f 64 65 27 73 20 64 61 74 61 29 20  af node's data) 
40dd0 61 6e 64 20 4c 65 61 76 65 73 52 65 61 64 65 72  and LeavesReader
40dd1 20 28 74 6f 0d 0a 2a 2a 20 69 74 65 72 61 74 65   (to..** iterate
40dd2 20 74 68 72 6f 75 67 68 20 61 20 73 65 67 6d 65   through a segme
40dd3 6e 74 27 73 20 65 6e 74 69 72 65 20 6c 65 61 66  nt's entire leaf
40dd4 20 6c 61 79 65 72 29 2e 20 20 4c 65 61 66 20 6e   layer).  Leaf n
40dd5 6f 64 65 73 20 68 61 76 65 0d 0a 2a 2a 20 74 68  odes have..** th
40dd6 65 20 66 6f 72 6d 61 74 3a 0d 0a 2a 2a 0d 0a 2a  e format:..**..*
40dd7 2a 20 76 61 72 69 6e 74 20 69 48 65 69 67 68 74  * varint iHeight
40dd8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 68  ;             (h
40dd9 65 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66 20  eight from leaf 
40dda 6c 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 30 29  level, always 0)
40ddb 0d 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 54 65 72  ..** varint nTer
40ddc 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
40ddd 20 28 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73   (length of firs
40dde 74 20 74 65 72 6d 29 0d 0a 2a 2a 20 63 68 61 72  t term)..** char
40ddf 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20   pTerm[nTerm];  
40de0 20 20 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74          (content
40de1 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0d   of first term).
40de2 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 44 6f 63 6c  .** varint nDocl
40de3 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
40de4 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d 27  (length of term'
40de5 73 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63  s associated doc
40de6 6c 69 73 74 29 0d 0a 2a 2a 20 63 68 61 72 20 70  list)..** char p
40de7 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
40de8 5d 3b 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f  ];    (content o
40de9 66 20 64 6f 63 6c 69 73 74 29 0d 0a 2a 2a 20 61  f doclist)..** a
40dea 72 72 61 79 20 7b 0d 0a 2a 2a 20 20 20 20 20 20  rray {..**      
40deb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40dec 20 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20         (further 
40ded 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d  terms are delta-
40dee 65 6e 63 6f 64 65 64 29 0d 0a 2a 2a 20 20 20 76  encoded)..**   v
40def 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20  arint nPrefix;  
40df0 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
40df1 20 6f 66 20 70 72 65 66 69 78 20 73 68 61 72 65   of prefix share
40df2 64 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20  d with previous 
40df3 74 65 72 6d 29 0d 0a 2a 2a 20 20 20 76 61 72 69  term)..**   vari
40df4 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
40df5 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
40df6 20 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78   unshared suffix
40df7 29 0d 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65  )..**   char pTe
40df8 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78  rmSuffix[nSuffix
40df9 5d 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66 66  ];(unshared suff
40dfa 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29  ix of next term)
40dfb 0d 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44  ..**   varint nD
40dfc 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
40dfd 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d   (length of term
40dfe 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f  's associated do
40dff 63 6c 69 73 74 29 0d 0a 2a 2a 20 20 20 63 68 61  clist)..**   cha
40e00 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c  r pDoclist[nDocl
40e01 69 73 74 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20  ist];  (content 
40e02 6f 66 20 64 6f 63 6c 69 73 74 29 0d 0a 2a 2a 20  of doclist)..** 
40e03 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 48 65 72 65 2c 20  }..**..** Here, 
40e04 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e  array { X } mean
40e05 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f  s zero or more o
40e06 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c  ccurrences of X,
40e07 20 61 64 6a 61 63 65 6e 74 20 69 6e 0d 0a 2a 2a   adjacent in..**
40e08 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a   memory...**..**
40e09 20 4c 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20   Leaf nodes are 
40e0a 62 72 6f 6b 65 6e 20 69 6e 74 6f 20 62 6c 6f 63  broken into bloc
40e0b 6b 73 20 77 68 69 63 68 20 61 72 65 20 73 74 6f  ks which are sto
40e0c 72 65 64 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79  red contiguously
40e0d 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 25 5f 73 65   in..** the %_se
40e0e 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20  gments table in 
40e0f 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54  sorted order.  T
40e10 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77  his means that w
40e11 68 65 6e 20 74 68 65 20 65 6e 64 0d 0a 2a 2a 20  hen the end..** 
40e12 6f 66 20 61 20 6e 6f 64 65 20 69 73 20 72 65 61  of a node is rea
40e13 63 68 65 64 2c 20 74 68 65 20 6e 65 78 74 20 74  ched, the next t
40e14 65 72 6d 20 69 73 20 69 6e 20 74 68 65 20 6e 6f  erm is in the no
40e15 64 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74  de with the next
40e16 0d 0a 2a 2a 20 67 72 65 61 74 65 72 20 6e 6f 64  ..** greater nod
40e17 65 20 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 65  e id...**..** Ne
40e18 77 20 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65  w data is spille
40e19 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61 66 20  d to a new leaf 
40e1a 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20 63 75  node when the cu
40e1b 72 72 65 6e 74 20 6e 6f 64 65 0d 0a 2a 2a 20 65  rrent node..** e
40e1c 78 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58 20  xceeds LEAF_MAX 
40e1d 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 32  bytes (default 2
40e1e 30 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61 20  048).  New data 
40e1f 77 68 69 63 68 20 69 74 73 65 6c 66 20 69 73 0d  which itself is.
40e20 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
40e21 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28  STANDALONE_MIN (
40e22 64 65 66 61 75 6c 74 20 31 30 32 34 29 20 69 73  default 1024) is
40e23 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61   placed in a sta
40e24 6e 64 61 6c 6f 6e 65 0d 0a 2a 2a 20 6e 6f 64 65  ndalone..** node
40e25 20 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 69   (a leaf node wi
40e26 74 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d  th a single term
40e27 20 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20 20   and doclist).  
40e28 54 68 65 20 67 6f 61 6c 20 6f 66 0d 0a 2a 2a 20  The goal of..** 
40e29 74 68 65 73 65 20 73 65 74 74 69 6e 67 73 20 69  these settings i
40e2a 73 20 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68  s to pack togeth
40e2b 65 72 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61  er groups of sma
40e2c 6c 6c 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c  ll doclists whil
40e2d 65 0d 0a 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20  e..** making it 
40e2e 65 66 66 69 63 69 65 6e 74 20 74 6f 20 64 69 72  efficient to dir
40e2f 65 63 74 6c 79 20 61 63 63 65 73 73 20 6c 61 72  ectly access lar
40e30 67 65 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  ge doclists.  Th
40e31 65 0d 0a 2a 2a 20 61 73 73 75 6d 70 74 69 6f 6e  e..** assumption
40e32 20 69 73 20 74 68 61 74 20 6c 61 72 67 65 20 64   is that large d
40e33 6f 63 6c 69 73 74 73 20 72 65 70 72 65 73 65 6e  oclists represen
40e34 74 20 74 65 72 6d 73 20 77 68 69 63 68 20 61 72  t terms which ar
40e35 65 20 6d 6f 72 65 0d 0a 2a 2a 20 6c 69 6b 65 6c  e more..** likel
40e36 79 20 74 6f 20 62 65 20 71 75 65 72 79 20 74 61  y to be query ta
40e37 72 67 65 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  rgets...**..** T
40e38 4f 44 4f 28 73 68 65 73 73 29 20 49 74 20 6d 61  ODO(shess) It ma
40e39 79 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  y be useful for 
40e3a 62 6c 6f 63 6b 69 6e 67 20 64 65 63 69 73 69 6f  blocking decisio
40e3b 6e 73 20 74 6f 20 62 65 20 6d 6f 72 65 0d 0a 2a  ns to be more..*
40e3c 2a 20 64 79 6e 61 6d 69 63 2e 20 20 46 6f 72 20  * dynamic.  For 
40e3d 69 6e 73 74 61 6e 63 65 2c 20 69 74 20 6d 61 79  instance, it may
40e3e 20 6d 61 6b 65 20 6d 6f 72 65 20 73 65 6e 73 65   make more sense
40e3f 20 74 6f 20 68 61 76 65 20 61 20 32 2e 35 6b 20   to have a 2.5k 
40e40 6c 65 61 66 0d 0a 2a 2a 20 6e 6f 64 65 20 72 61  leaf..** node ra
40e41 74 68 65 72 20 74 68 61 6e 20 73 70 6c 69 74 74  ther than splitt
40e42 69 6e 67 20 69 6e 74 6f 20 32 6b 20 61 6e 64 20  ing into 2k and 
40e43 2e 35 6b 20 6e 6f 64 65 73 2e 20 20 4d 79 20 69  .5k nodes.  My i
40e44 6e 74 75 69 74 69 6f 6e 20 69 73 0d 0a 2a 2a 20  ntuition is..** 
40e45 74 68 61 74 20 74 68 69 73 20 6d 69 67 68 74 20  that this might 
40e46 65 78 74 65 6e 64 20 74 68 72 6f 75 67 68 20 32  extend through 2
40e47 78 20 6f 72 20 34 78 20 74 68 65 20 70 61 67 65  x or 4x the page
40e48 73 69 7a 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a  size...**..**..*
40e49 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  *** Segment inte
40e4a 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0d  rior nodes ****.
40e4b 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  .** Segment inte
40e4c 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f 72 65  rior nodes store
40e4d 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20 73 75   blockids for su
40e4e 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e 64 20  btree nodes and 
40e4f 74 65 72 6d 73 0d 0a 2a 2a 20 74 6f 20 64 65 73  terms..** to des
40e50 63 72 69 62 65 20 77 68 61 74 20 64 61 74 61 20  cribe what data 
40e51 69 73 20 73 74 6f 72 65 64 20 62 79 20 74 68 65  is stored by the
40e52 20 65 61 63 68 20 73 75 62 74 72 65 65 2e 20 20   each subtree.  
40e53 49 6e 74 65 72 69 6f 72 0d 0a 2a 2a 20 6e 6f 64  Interior..** nod
40e54 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  es are written u
40e55 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69  sing InteriorWri
40e56 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73  ter, and read us
40e57 69 6e 67 0d 0a 2a 2a 20 49 6e 74 65 72 69 6f 72  ing..** Interior
40e58 52 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69 6f  Reader.  Interio
40e59 72 57 72 69 74 65 72 73 20 61 72 65 20 63 72 65  rWriters are cre
40e5a 61 74 65 64 20 61 73 20 6e 65 65 64 65 64 20 77  ated as needed w
40e5b 68 65 6e 0d 0a 2a 2a 20 53 65 67 6d 65 6e 74 57  hen..** SegmentW
40e5c 72 69 74 65 72 20 63 72 65 61 74 65 73 20 6e 65  riter creates ne
40e5d 77 20 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72  w leaf nodes, or
40e5e 20 77 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f   when an interio
40e5f 72 20 6e 6f 64 65 0d 0a 2a 2a 20 69 74 73 65 6c  r node..** itsel
40e60 66 20 67 72 6f 77 73 20 74 6f 6f 20 62 69 67 20  f grows too big 
40e61 61 6e 64 20 6d 75 73 74 20 62 65 20 73 70 6c 69  and must be spli
40e62 74 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 20 6f  t.  The format o
40e63 66 20 69 6e 74 65 72 69 6f 72 0d 0a 2a 2a 20 6e  f interior..** n
40e64 6f 64 65 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 76 61  odes:..**..** va
40e65 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20  rint iHeight;   
40e66 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
40e67 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
40e68 20 61 6c 77 61 79 73 20 3e 30 29 0d 0a 2a 2a 20   always >0)..** 
40e69 76 61 72 69 6e 74 20 69 42 6c 6f 63 6b 69 64 3b  varint iBlockid;
40e6a 20 20 20 20 20 20 20 20 20 20 28 62 6c 6f 63 6b            (block
40e6b 20 69 64 20 6f 66 20 6e 6f 64 65 27 73 20 6c 65   id of node's le
40e6c 66 74 6d 6f 73 74 20 73 75 62 74 72 65 65 29 0d  ftmost subtree).
40e6d 0a 2a 2a 20 6f 70 74 69 6f 6e 61 6c 20 7b 0d 0a  .** optional {..
40e6e 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 54 65 72  **   varint nTer
40e6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65  m;           (le
40e70 6e 67 74 68 20 6f 66 20 66 69 72 73 74 20 74 65  ngth of first te
40e71 72 6d 29 0d 0a 2a 2a 20 20 20 63 68 61 72 20 70  rm)..**   char p
40e72 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20  Term[nTerm];    
40e73 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69    (content of fi
40e74 72 73 74 20 74 65 72 6d 29 0d 0a 2a 2a 20 20 20  rst term)..**   
40e75 61 72 72 61 79 20 7b 0d 0a 2a 2a 20 20 20 20 20  array {..**     
40e76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40e77 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72 74             (furt
40e78 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64 65  her terms are de
40e79 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0d 0a 2a 2a  lta-encoded)..**
40e7a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 50 72 65       varint nPre
40e7b 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
40e7c 28 6c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65  (length of share
40e7d 64 20 70 72 65 66 69 78 20 77 69 74 68 20 70 72  d prefix with pr
40e7e 65 76 69 6f 75 73 20 74 65 72 6d 29 0d 0a 2a 2a  evious term)..**
40e7f 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66       varint nSuf
40e80 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
40e81 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73 68 61  (length of unsha
40e82 72 65 64 20 73 75 66 66 69 78 29 0d 0a 2a 2a 20  red suffix)..** 
40e83 20 20 20 20 63 68 61 72 20 70 54 65 72 6d 53 75      char pTermSu
40e84 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20 28  ffix[nSuffix]; (
40e85 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20  unshared suffix 
40e86 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0d 0a 2a  of next term)..*
40e87 2a 20 20 20 7d 0d 0a 2a 2a 20 7d 0d 0a 2a 2a 0d  *   }..** }..**.
40e88 0a 2a 2a 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e  .** Here, option
40e89 61 6c 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61  al { X } means a
40e8a 6e 20 6f 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65  n optional eleme
40e8b 6e 74 2c 20 77 68 69 6c 65 20 61 72 72 61 79 20  nt, while array 
40e8c 7b 20 58 20 7d 0d 0a 2a 2a 20 6d 65 61 6e 73 20  { X }..** means 
40e8d 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63  zero or more occ
40e8e 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61  urrences of X, a
40e8f 64 6a 61 63 65 6e 74 20 69 6e 20 6d 65 6d 6f 72  djacent in memor
40e90 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 69 6e  y...**..** An in
40e91 74 65 72 69 6f 72 20 6e 6f 64 65 20 65 6e 63 6f  terior node enco
40e92 64 65 73 20 6e 20 74 65 72 6d 73 20 73 65 70 61  des n terms sepa
40e93 72 61 74 69 6e 67 20 6e 2b 31 20 73 75 62 74 72  rating n+1 subtr
40e94 65 65 73 2e 20 20 54 68 65 0d 0a 2a 2a 20 73 75  ees.  The..** su
40e95 62 74 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65  btree blocks are
40e96 20 63 6f 6e 74 69 67 75 6f 75 73 2c 20 73 6f 20   contiguous, so 
40e97 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 73  only the first s
40e98 75 62 74 72 65 65 27 73 20 62 6c 6f 63 6b 69 64  ubtree's blockid
40e99 0d 0a 2a 2a 20 69 73 20 65 6e 63 6f 64 65 64 2e  ..** is encoded.
40e9a 20 20 54 68 65 20 73 75 62 74 72 65 65 20 61 74    The subtree at
40e9b 20 69 42 6c 6f 63 6b 69 64 20 77 69 6c 6c 20 63   iBlockid will c
40e9c 6f 6e 74 61 69 6e 20 61 6c 6c 20 74 65 72 6d 73  ontain all terms
40e9d 20 6c 65 73 73 0d 0a 2a 2a 20 74 68 61 6e 20 74   less..** than t
40e9e 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 65 6e  he first term en
40e9f 63 6f 64 65 64 20 28 6f 72 20 61 6c 6c 20 74 65  coded (or all te
40ea0 72 6d 73 20 69 66 20 6e 6f 20 74 65 72 6d 20 69  rms if no term i
40ea1 73 20 65 6e 63 6f 64 65 64 29 2e 0d 0a 2a 2a 20  s encoded)...** 
40ea2 4f 74 68 65 72 77 69 73 65 2c 20 66 6f 72 20 74  Otherwise, for t
40ea3 65 72 6d 73 20 67 72 65 61 74 65 72 20 74 68 61  erms greater tha
40ea4 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 70 54  n or equal to pT
40ea5 65 72 6d 5b 69 5d 20 62 75 74 20 6c 65 73 73 0d  erm[i] but less.
40ea6 0a 2a 2a 20 74 68 61 6e 20 70 54 65 72 6d 5b 69  .** than pTerm[i
40ea7 2b 31 5d 2c 20 74 68 65 20 73 75 62 74 72 65 65  +1], the subtree
40ea8 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d 20 77   for that term w
40ea9 69 6c 6c 20 62 65 20 72 6f 6f 74 65 64 20 61 74  ill be rooted at
40eaa 0d 0a 2a 2a 20 69 42 6c 6f 63 6b 69 64 2b 69 2e  ..** iBlockid+i.
40eab 20 20 49 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73    Interior nodes
40eac 20 6f 6e 6c 79 20 73 74 6f 72 65 20 65 6e 6f 75   only store enou
40ead 67 68 20 74 65 72 6d 20 64 61 74 61 20 74 6f 0d  gh term data to.
40eae 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69 73 68 20  .** distinguish 
40eaf 61 64 6a 61 63 65 6e 74 20 63 68 69 6c 64 72 65  adjacent childre
40eb0 6e 20 28 69 66 20 74 68 65 20 72 69 67 68 74 6d  n (if the rightm
40eb1 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
40eb2 6c 65 66 74 0d 0a 2a 2a 20 63 68 69 6c 64 20 69  left..** child i
40eb3 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c 20 61  s "something", a
40eb4 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20  nd the leftmost 
40eb5 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69 67 68  term of the righ
40eb6 74 20 63 68 69 6c 64 20 69 73 0d 0a 2a 2a 20 22  t child is..** "
40eb7 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22 77  wicked", only "w
40eb8 22 20 69 73 20 73 74 6f 72 65 64 29 2e 0d 0a 2a  " is stored)...*
40eb9 2a 0d 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69  *..** New data i
40eba 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e  s spilled to a n
40ebb 65 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ew interior node
40ebc 20 61 74 20 74 68 65 20 73 61 6d 65 20 68 65 69   at the same hei
40ebd 67 68 74 20 77 68 65 6e 0d 0a 2a 2a 20 74 68 65  ght when..** the
40ebe 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78   current node ex
40ebf 63 65 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d  ceeds INTERIOR_M
40ec0 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
40ec1 74 20 32 30 34 38 29 2e 0d 0a 2a 2a 20 49 4e 54  t 2048)...** INT
40ec2 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20  ERIOR_MIN_TERMS 
40ec3 28 64 65 66 61 75 6c 74 20 37 29 20 6b 65 65 70  (default 7) keep
40ec4 73 20 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72  s large terms fr
40ec5 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0d  om monopolizing.
40ec6 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
40ec7 65 73 20 61 6e 64 20 6d 61 6b 69 6e 67 20 74 68  es and making th
40ec8 65 20 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e  e tree too skinn
40ec9 79 2e 20 20 54 68 65 20 69 6e 74 65 72 69 6f 72  y.  The interior
40eca 20 6e 6f 64 65 73 0d 0a 2a 2a 20 61 74 20 61 20   nodes..** at a 
40ecb 67 69 76 65 6e 20 68 65 69 67 68 74 20 61 72 65  given height are
40ecc 20 6e 61 74 75 72 61 6c 6c 79 20 74 72 61 63 6b   naturally track
40ecd 65 64 20 62 79 20 69 6e 74 65 72 69 6f 72 20 6e  ed by interior n
40ece 6f 64 65 73 20 61 74 0d 0a 2a 2a 20 68 65 69 67  odes at..** heig
40ecf 68 74 2b 31 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  ht+1, and so on.
40ed0 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 20 53  ..**..**..**** S
40ed1 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79  egment directory
40ed2 20 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 73 65   ****..** The se
40ed3 67 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20  gment directory 
40ed4 69 6e 20 74 61 62 6c 65 20 25 5f 73 65 67 64 69  in table %_segdi
40ed5 72 20 73 74 6f 72 65 73 20 6d 65 74 61 2d 69 6e  r stores meta-in
40ed6 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 0d 0a 2a  formation for..*
40ed7 2a 20 6d 65 72 67 69 6e 67 20 61 6e 64 20 64 65  * merging and de
40ed8 6c 65 74 69 6e 67 20 73 65 67 6d 65 6e 74 73 2c  leting segments,
40ed9 20 61 6e 64 20 61 6c 73 6f 20 74 68 65 20 72 6f   and also the ro
40eda 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65 0d 0a  ot node of the..
40edb 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20 74 72 65  ** segment's tre
40edc 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72  e...**..** The r
40edd 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68 65 20  oot node is the 
40ede 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68 65 20  top node of the 
40edf 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65 20 61  segment's tree a
40ee0 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0d 0a 2a  fter encoding..*
40ee1 2a 20 74 68 65 20 65 6e 74 69 72 65 20 73 65 67  * the entire seg
40ee2 6d 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65 64  ment, restricted
40ee3 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74   to ROOT_MAX byt
40ee4 65 73 20 28 64 65 66 61 75 6c 74 20 31 30 32 34  es (default 1024
40ee5 29 2e 0d 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  )...** This coul
40ee6 64 20 62 65 20 65 69 74 68 65 72 20 61 20 6c 65  d be either a le
40ee7 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e  af node or an in
40ee8 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66  terior node.  If
40ee9 20 74 68 65 20 74 6f 70 0d 0a 2a 2a 20 6e 6f 64   the top..** nod
40eea 65 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65 20  e requires more 
40eeb 74 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79  than ROOT_MAX by
40eec 74 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73 68  tes, it is flush
40eed 65 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73  ed to %_segments
40eee 0d 0a 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72  ..** and a new r
40eef 6f 6f 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  oot interior nod
40ef0 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 28  e is generated (
40ef1 77 68 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77  which should alw
40ef2 61 79 73 20 66 69 74 0d 0a 2a 2a 20 77 69 74 68  ays fit..** with
40ef3 69 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61  in ROOT_MAX beca
40ef4 75 73 65 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64  use it only need
40ef5 73 20 73 70 61 63 65 20 66 6f 72 20 32 20 76 61  s space for 2 va
40ef6 72 69 6e 74 73 2c 20 74 68 65 0d 0a 2a 2a 20 68  rints, the..** h
40ef7 65 69 67 68 74 20 61 6e 64 20 74 68 65 20 62 6c  eight and the bl
40ef8 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 70 72 65  ockid of the pre
40ef9 76 69 6f 75 73 20 72 6f 6f 74 29 2e 0d 0a 2a 2a  vious root)...**
40efa 0d 0a 2a 2a 20 54 68 65 20 6d 65 74 61 2d 69 6e  ..** The meta-in
40efb 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65  formation in the
40efc 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
40efd 72 79 20 69 73 3a 0d 0a 2a 2a 20 20 20 6c 65 76  ry is:..**   lev
40efe 65 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  el              
40eff 20 2d 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c   - segment level
40f00 20 28 73 65 65 20 62 65 6c 6f 77 29 0d 0a 2a 2a   (see below)..**
40f01 20 20 20 69 64 78 20 20 20 20 20 20 20 20 20 20     idx          
40f02 20 20 20 20 20 20 20 2d 20 69 6e 64 65 78 20 77         - index w
40f03 69 74 68 69 6e 20 6c 65 76 65 6c 0d 0a 2a 2a 20  ithin level..** 
40f04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40f05 20 20 20 20 20 20 2d 20 28 6c 65 76 65 6c 2c 69        - (level,i
40f06 64 78 20 75 6e 69 71 75 65 6c 79 20 69 64 65 6e  dx uniquely iden
40f07 74 69 66 79 20 61 20 73 65 67 6d 65 6e 74 29 0d  tify a segment).
40f08 0a 2a 2a 20 20 20 73 74 61 72 74 5f 62 6c 6f 63  .**   start_bloc
40f09 6b 20 20 20 20 20 20 20 20 20 2d 20 66 69 72 73  k         - firs
40f0a 74 20 6c 65 61 66 20 6e 6f 64 65 0d 0a 2a 2a 20  t leaf node..** 
40f0b 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f    leaves_end_blo
40f0c 63 6b 20 20 20 20 2d 20 6c 61 73 74 20 6c 65 61  ck    - last lea
40f0d 66 20 6e 6f 64 65 0d 0a 2a 2a 20 20 20 65 6e 64  f node..**   end
40f0e 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20 20  _block          
40f0f 20 2d 20 6c 61 73 74 20 62 6c 6f 63 6b 20 28 69   - last block (i
40f10 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 69 6f  ncluding interio
40f11 72 20 6e 6f 64 65 73 29 0d 0a 2a 2a 20 20 20 72  r nodes)..**   r
40f12 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
40f13 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73 20 6f 66     - contents of
40f14 20 72 6f 6f 74 20 6e 6f 64 65 0d 0a 2a 2a 0d 0a   root node..**..
40f15 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e  ** If the root n
40f16 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  ode is a leaf no
40f17 64 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f 62  de, then start_b
40f18 6c 6f 63 6b 2c 0d 0a 2a 2a 20 6c 65 61 76 65 73  lock,..** leaves
40f19 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20  _end_block, and 
40f1a 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c  end_block are al
40f1b 6c 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  l 0...**..**..**
40f1c 2a 2a 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 69  ** Segment mergi
40f1d 6e 67 20 2a 2a 2a 2a 0d 0a 2a 2a 20 54 6f 20 61  ng ****..** To a
40f1e 6d 6f 72 74 69 7a 65 20 75 70 64 61 74 65 20 63  mortize update c
40f1f 6f 73 74 73 2c 20 73 65 67 6d 65 6e 74 73 20 61  osts, segments a
40f20 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20  re grouped into 
40f21 6c 65 76 65 6c 73 20 61 6e 64 0d 0a 2a 2a 20 6d  levels and..** m
40f22 65 72 67 65 64 20 69 6e 20 62 61 74 63 68 65 73  erged in batches
40f23 2e 20 20 45 61 63 68 20 69 6e 63 72 65 61 73 65  .  Each increase
40f24 20 69 6e 20 6c 65 76 65 6c 20 72 65 70 72 65 73   in level repres
40f25 65 6e 74 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c  ents exponential
40f26 6c 79 0d 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63 75  ly..** more docu
40f27 6d 65 6e 74 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4e  ments...**..** N
40f28 65 77 20 64 6f 63 75 6d 65 6e 74 73 20 28 61 63  ew documents (ac
40f29 74 75 61 6c 6c 79 2c 20 64 6f 63 75 6d 65 6e 74  tually, document
40f2a 20 75 70 64 61 74 65 73 29 20 61 72 65 20 74 6f   updates) are to
40f2b 6b 65 6e 69 7a 65 64 20 61 6e 64 0d 0a 2a 2a 20  kenized and..** 
40f2c 77 72 69 74 74 65 6e 20 69 6e 64 69 76 69 64 75  written individu
40f2d 61 6c 6c 79 20 28 75 73 69 6e 67 20 4c 65 61 66  ally (using Leaf
40f2e 57 72 69 74 65 72 29 20 74 6f 20 61 20 6c 65 76  Writer) to a lev
40f2f 65 6c 20 30 20 73 65 67 6d 65 6e 74 2c 20 77 69  el 0 segment, wi
40f30 74 68 0d 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74  th..** increment
40f31 69 6e 67 20 69 64 78 2e 20 20 57 68 65 6e 20 69  ing idx.  When i
40f32 64 78 20 72 65 61 63 68 65 73 20 4d 45 52 47 45  dx reaches MERGE
40f33 5f 43 4f 55 4e 54 20 28 64 65 66 61 75 6c 74 20  _COUNT (default 
40f34 31 36 29 2c 20 61 6c 6c 0d 0a 2a 2a 20 6c 65 76  16), all..** lev
40f35 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 20 61 72  el 0 segments ar
40f36 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20 61 20  e merged into a 
40f37 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 31 20 73  single level 1 s
40f38 65 67 6d 65 6e 74 2e 20 20 4c 65 76 65 6c 20 31  egment.  Level 1
40f39 0d 0a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65  ..** is populate
40f3a 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20 30 2c 20  d like level 0, 
40f3b 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 4d  and eventually M
40f3c 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65 76 65 6c  ERGE_COUNT level
40f3d 20 31 0d 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20   1..** segments 
40f3e 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20 61 20  are merged to a 
40f3f 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32 20 73  single level 2 s
40f40 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73 65 6e  egment (represen
40f41 74 69 6e 67 0d 0a 2a 2a 20 4d 45 52 47 45 5f 43  ting..** MERGE_C
40f42 4f 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29 2c  OUNT^2 updates),
40f43 20 61 6e 64 20 73 6f 20 6f 6e 2e 0d 0a 2a 2a 0d   and so on...**.
40f44 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20 6d 65  .** A segment me
40f45 72 67 65 20 74 72 61 76 65 72 73 65 73 20 61 6c  rge traverses al
40f46 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 61 20  l segments at a 
40f47 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e 0d 0a  given level in..
40f48 2a 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65 72  ** parallel, per
40f49 66 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69 67  forming a straig
40f4a 68 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65 64  htforward sorted
40f4b 20 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20 73   merge.  Since s
40f4c 65 67 6d 65 6e 74 0d 0a 2a 2a 20 6c 65 61 66 20  egment..** leaf 
40f4d 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65  nodes are writte
40f4e 6e 20 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65  n in to the %_se
40f4f 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20  gments table in 
40f50 6f 72 64 65 72 2c 20 74 68 69 73 0d 0a 2a 2a 20  order, this..** 
40f51 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20  merge traverses 
40f52 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 73  the underlying s
40f53 71 6c 69 74 65 20 64 69 73 6b 20 73 74 72 75 63  qlite disk struc
40f54 74 75 72 65 73 20 65 66 66 69 63 69 65 6e 74 6c  tures efficientl
40f55 79 2e 0d 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  y...** After the
40f56 20 6d 65 72 67 65 2c 20 61 6c 6c 20 73 65 67 6d   merge, all segm
40f57 65 6e 74 20 62 6c 6f 63 6b 73 20 66 72 6f 6d 20  ent blocks from 
40f58 74 68 65 20 6d 65 72 67 65 64 20 6c 65 76 65 6c  the merged level
40f59 20 61 72 65 0d 0a 2a 2a 20 64 65 6c 65 74 65 64   are..** deleted
40f5a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4d 45 52 47 45 5f  ...**..** MERGE_
40f5b 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68  COUNT controls h
40f5c 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67  ow often we merg
40f5d 65 20 73 65 67 6d 65 6e 74 73 2e 20 20 31 36 20  e segments.  16 
40f5e 73 65 65 6d 73 20 74 6f 20 62 65 0d 0a 2a 2a 20  seems to be..** 
40f5f 73 6f 6d 65 77 68 61 74 20 6f 66 20 61 20 73 77  somewhat of a sw
40f60 65 65 74 20 73 70 6f 74 20 66 6f 72 20 69 6e 73  eet spot for ins
40f61 65 72 74 69 6f 6e 20 70 65 72 66 6f 72 6d 61 6e  ertion performan
40f62 63 65 2e 20 20 33 32 20 61 6e 64 20 36 34 20 73  ce.  32 and 64 s
40f63 68 6f 77 0d 0a 2a 2a 20 76 65 72 79 20 73 69 6d  how..** very sim
40f64 69 6c 61 72 20 70 65 72 66 6f 72 6d 61 6e 63 65  ilar performance
40f65 20 6e 75 6d 62 65 72 73 20 74 6f 20 31 36 20 6f   numbers to 16 o
40f66 6e 20 69 6e 73 65 72 74 69 6f 6e 2c 20 74 68 6f  n insertion, tho
40f67 75 67 68 20 74 68 65 79 27 72 65 0d 0a 2a 2a 20  ugh they're..** 
40f68 61 20 74 69 6e 79 20 62 69 74 20 73 6c 6f 77 65  a tiny bit slowe
40f69 72 20 28 70 65 72 68 61 70 73 20 64 75 65 20 74  r (perhaps due t
40f6a 6f 20 6d 6f 72 65 20 6f 76 65 72 68 65 61 64 20  o more overhead 
40f6b 69 6e 20 6d 65 72 67 65 2d 74 69 6d 65 0d 0a 2a  in merge-time..*
40f6c 2a 20 73 6f 72 74 69 6e 67 29 2e 20 20 38 20 69  * sorting).  8 i
40f6d 73 20 61 62 6f 75 74 20 32 30 25 20 73 6c 6f 77  s about 20% slow
40f6e 65 72 20 74 68 61 6e 20 31 36 2c 20 34 20 61 62  er than 16, 4 ab
40f6f 6f 75 74 20 35 30 25 20 73 6c 6f 77 65 72 20 74  out 50% slower t
40f70 68 61 6e 0d 0a 2a 2a 20 31 36 2c 20 32 20 61 62  han..** 16, 2 ab
40f71 6f 75 74 20 36 36 25 20 73 6c 6f 77 65 72 20 74  out 66% slower t
40f72 68 61 6e 20 31 36 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  han 16...**..** 
40f73 41 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20 68  At query time, h
40f74 69 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  igh MERGE_COUNT 
40f75 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e 75  increases the nu
40f76 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73  mber of segments
40f77 0d 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20  ..** which need 
40f78 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e  to be scanned an
40f79 64 20 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69  d merged.  For i
40f7a 6e 73 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30  nstance, with 10
40f7b 30 6b 20 64 6f 63 73 0d 0a 2a 2a 20 69 6e 73 65  0k docs..** inse
40f7c 72 74 65 64 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  rted:..**..**   
40f7d 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73   MERGE_COUNT   s
40f7e 65 67 6d 65 6e 74 73 0d 0a 2a 2a 20 20 20 20 20  egments..**     
40f7f 20 20 31 36 20 20 20 20 20 20 20 20 20 20 20 32    16           2
40f80 35 0d 0a 2a 2a 20 20 20 20 20 20 20 20 38 20 20  5..**        8  
40f81 20 20 20 20 20 20 20 20 20 31 32 0d 0a 2a 2a 20           12..** 
40f82 20 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20         4        
40f83 20 20 20 31 30 0d 0a 2a 2a 20 20 20 20 20 20 20     10..**       
40f84 20 32 20 20 20 20 20 20 20 20 20 20 20 20 36 0d   2            6.
40f85 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 61 70 70  .**..** This app
40f86 65 61 72 73 20 74 6f 20 68 61 76 65 20 6f 6e 6c  ears to have onl
40f87 79 20 61 20 6d 6f 64 65 72 61 74 65 20 69 6d 70  y a moderate imp
40f88 61 63 74 20 6f 6e 20 71 75 65 72 69 65 73 20 66  act on queries f
40f89 6f 72 20 76 65 72 79 0d 0a 2a 2a 20 66 72 65 71  or very..** freq
40f8a 75 65 6e 74 20 74 65 72 6d 73 20 28 77 68 69 63  uent terms (whic
40f8b 68 20 61 72 65 20 73 6f 6d 65 77 68 61 74 20 64  h are somewhat d
40f8c 6f 6d 69 6e 61 74 65 64 20 62 79 20 73 65 67 6d  ominated by segm
40f8d 65 6e 74 20 6d 65 72 67 65 0d 0a 2a 2a 20 63 6f  ent merge..** co
40f8e 73 74 73 29 2c 20 61 6e 64 20 69 6e 66 72 65 71  sts), and infreq
40f8f 75 65 6e 74 20 61 6e 64 20 6e 6f 6e 2d 65 78 69  uent and non-exi
40f90 73 74 65 6e 74 20 74 65 72 6d 73 20 73 74 69 6c  stent terms stil
40f91 6c 20 73 65 65 6d 20 74 6f 20 62 65 20 66 61 73  l seem to be fas
40f92 74 0d 0a 2a 2a 20 65 76 65 6e 20 77 69 74 68 20  t..** even with 
40f93 6d 61 6e 79 20 73 65 67 6d 65 6e 74 73 2e 0d 0a  many segments...
40f94 2a 2a 0d 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  **..** TODO(shes
40f95 73 29 20 54 68 61 74 20 73 61 69 64 2c 20 69 74  s) That said, it
40f96 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
40f97 6f 20 68 61 76 65 20 61 20 62 65 74 74 65 72 20  o have a better 
40f98 71 75 65 72 79 2d 73 69 64 65 0d 0a 2a 2a 20 61  query-side..** a
40f99 72 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52 47  rgument for MERG
40f9a 45 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20 20  E_COUNT of 16.  
40f9b 41 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73 73  Also, it is poss
40f9c 69 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61 74  ible/likely that
40f9d 0d 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  ..** optimizatio
40f9e 6e 73 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b  ns to things lik
40f9f 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e  e doclist mergin
40fa0 67 20 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65  g will swing the
40fa1 20 73 77 65 65 74 0d 0a 2a 2a 20 73 70 6f 74 20   sweet..** spot 
40fa2 61 72 6f 75 6e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 0d  around...**..**.
40fa3 0a 2a 2a 0d 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69  .**..**** Handli
40fa4 6e 67 20 6f 66 20 64 65 6c 65 74 69 6f 6e 73 20  ng of deletions 
40fa5 61 6e 64 20 75 70 64 61 74 65 73 20 2a 2a 2a 2a  and updates ****
40fa6 0d 0a 2a 2a 20 53 69 6e 63 65 20 77 65 27 72 65  ..** Since we're
40fa7 20 75 73 69 6e 67 20 61 20 73 65 67 6d 65 6e 74   using a segment
40fa8 65 64 20 73 74 72 75 63 74 75 72 65 2c 20 77 69  ed structure, wi
40fa9 74 68 20 6e 6f 20 64 6f 63 69 64 2d 6f 72 69 65  th no docid-orie
40faa 6e 74 65 64 0d 0a 2a 2a 20 69 6e 64 65 78 20 69  nted..** index i
40fab 6e 74 6f 20 74 68 65 20 74 65 72 6d 20 69 6e 64  nto the term ind
40fac 65 78 2c 20 77 65 20 63 6c 65 61 72 6c 79 20 63  ex, we clearly c
40fad 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 75 70 64  annot simply upd
40fae 61 74 65 20 74 68 65 20 74 65 72 6d 0d 0a 2a 2a  ate the term..**
40faf 20 69 6e 64 65 78 20 77 68 65 6e 20 61 20 64 6f   index when a do
40fb0 63 75 6d 65 6e 74 20 69 73 20 64 65 6c 65 74 65  cument is delete
40fb1 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 46  d or updated.  F
40fb2 6f 72 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 65  or deletions, we
40fb3 0d 0a 2a 2a 20 77 72 69 74 65 20 61 6e 20 65 6d  ..** write an em
40fb4 70 74 79 20 64 6f 63 6c 69 73 74 20 28 76 61 72  pty doclist (var
40fb5 69 6e 74 28 64 6f 63 69 64 29 20 76 61 72 69 6e  int(docid) varin
40fb6 74 28 50 4f 53 5f 45 4e 44 29 29 2c 20 66 6f 72  t(POS_END)), for
40fb7 20 75 70 64 61 74 65 73 0d 0a 2a 2a 20 77 65 20   updates..** we 
40fb8 73 69 6d 70 6c 79 20 77 72 69 74 65 20 74 68 65  simply write the
40fb9 20 6e 65 77 20 64 6f 63 6c 69 73 74 2e 20 20 53   new doclist.  S
40fba 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 20 6f 76  egment merges ov
40fbb 65 72 77 72 69 74 65 20 6f 6c 64 65 72 0d 0a 2a  erwrite older..*
40fbc 2a 20 64 61 74 61 20 66 6f 72 20 61 20 70 61 72  * data for a par
40fbd 74 69 63 75 6c 61 72 20 64 6f 63 69 64 20 77 69  ticular docid wi
40fbe 74 68 20 6e 65 77 65 72 20 64 61 74 61 2c 20 73  th newer data, s
40fbf 6f 20 64 65 6c 65 74 65 73 20 6f 72 20 75 70 64  o deletes or upd
40fc0 61 74 65 73 0d 0a 2a 2a 20 77 69 6c 6c 20 65 76  ates..** will ev
40fc1 65 6e 74 75 61 6c 6c 79 20 6f 76 65 72 74 61 6b  entually overtak
40fc2 65 20 74 68 65 20 65 61 72 6c 69 65 72 20 64 61  e the earlier da
40fc3 74 61 20 61 6e 64 20 6b 6e 6f 63 6b 20 69 74 20  ta and knock it 
40fc4 6f 75 74 2e 20 20 54 68 65 0d 0a 2a 2a 20 71 75  out.  The..** qu
40fc5 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b 65 77 69  ery logic likewi
40fc6 73 65 20 6d 65 72 67 65 73 20 64 6f 63 6c 69 73  se merges doclis
40fc7 74 73 20 73 6f 20 74 68 61 74 20 6e 65 77 65 72  ts so that newer
40fc8 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20 6f 75 74   data knocks out
40fc9 0d 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74 61 2e  ..** older data.
40fca 0d 0a 2a 2a 0d 0a 2a 2a 20 54 4f 44 4f 28 73 68  ..**..** TODO(sh
40fcb 65 73 73 29 20 50 72 6f 76 69 64 65 20 61 20 56  ess) Provide a V
40fcc 41 43 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61  ACUUM type opera
40fcd 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75  tion to clear ou
40fce 74 20 61 6c 6c 0d 0a 2a 2a 20 64 65 6c 65 74 69  t all..** deleti
40fcf 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63 61 74  ons and duplicat
40fd0 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f 75 6c  ions.  This woul
40fd1 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65 20 61  d basically be a
40fd2 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0d 0a 2a   forced merge..*
40fd3 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
40fd4 73 65 67 6d 65 6e 74 2e 0d 0a 2a 2f 0d 0a 0d 0a  segment...*/....
40fd5 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
40fd6 49 6e 63 6c 75 64 65 20 66 74 73 33 49 6e 74 2e  Include fts3Int.
40fd7 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
40fd8 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  of fts3.c ******
40fd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
40fda 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
40fdb 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
40fdc 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
40fdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40fde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
40fdf 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 39 20 4e 6f  ../*..** 2009 No
40fe0 76 20 31 32 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  v 12..**..** The
40fe1 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
40fe2 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
40fe3 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
40fe4 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
40fe5 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
40fe6 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
40fe7 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
40fe8 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
40fe9 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
40fea 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
40feb 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
40fec 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
40fed 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
40fee 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
40fef 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
40ff0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
40ff1 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
40ff2 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
40ff3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ff4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
40ff7 2a 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 23 69 66 6e 64  *..**..*/..#ifnd
40ff8 65 66 20 5f 46 54 53 49 4e 54 5f 48 0d 0a 23 64  ef _FTSINT_H..#d
40ff9 65 66 69 6e 65 20 5f 46 54 53 49 4e 54 5f 48 0d  efine _FTSINT_H.
40ffa 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ...#if !defined(
40ffb 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69  NDEBUG) && !defi
40ffc 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
40ffd 29 20 0d 0a 23 20 64 65 66 69 6e 65 20 4e 44 45  ) ..# define NDE
40ffe 42 55 47 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d  BUG 1..#endif...
40fff 0a 2f 2a 0d 0a 2a 2a 20 46 54 53 34 20 69 73 20  ./*..** FTS4 is 
41000 72 65 61 6c 6c 79 20 61 6e 20 65 78 74 65 6e 73  really an extens
41001 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e 20 20 49  ion for FTS3.  I
41002 74 20 69 73 20 65 6e 61 62 6c 65 64 20 75 73 69  t is enabled usi
41003 6e 67 20 74 68 65 0d 0a 2a 2a 20 53 51 4c 49 54  ng the..** SQLIT
41004 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 6d 61  E_ENABLE_FTS3 ma
41005 63 72 6f 2e 20 20 42 75 74 20 74 6f 20 61 76 6f  cro.  But to avo
41006 69 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 65 20  id confusion we 
41007 61 6c 73 6f 20 61 6c 6c 0d 0a 2a 2a 20 74 68 65  also all..** the
41008 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
41009 54 53 34 20 6d 61 63 72 6f 20 74 6f 20 73 65 72  TS4 macro to ser
4100a 76 65 20 61 73 20 61 6e 20 61 6c 69 73 73 65 20  ve as an alisse 
4100b 66 6f 72 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  for SQLITE_ENABL
4100c 45 5f 46 54 53 33 2e 0d 0a 2a 2f 0d 0a 23 69 66  E_FTS3...*/..#if
4100d 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
4100e 45 4e 41 42 4c 45 5f 46 54 53 34 29 20 26 26 20  ENABLE_FTS4) && 
4100f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
41010 45 4e 41 42 4c 45 5f 46 54 53 33 29 0d 0a 23 20  ENABLE_FTS3)..# 
41011 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
41012 41 42 4c 45 5f 46 54 53 33 0d 0a 23 65 6e 64 69  ABLE_FTS3..#endi
41013 66 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65  f....#if !define
41014 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
41015 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
41016 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0d 0a 0d  _ENABLE_FTS3)...
41017 0a 2f 2a 20 49 66 20 6e 6f 74 20 62 75 69 6c 64  ./* If not build
41018 69 6e 67 20 61 73 20 70 61 72 74 20 6f 66 20 74  ing as part of t
41019 68 65 20 63 6f 72 65 2c 20 69 6e 63 6c 75 64 65  he core, include
4101a 20 73 71 6c 69 74 65 33 65 78 74 2e 68 2e 20 2a   sqlite3ext.h. *
4101b 2f 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
4101c 45 5f 43 4f 52 45 0d 0a 53 51 4c 49 54 45 5f 41  E_CORE..SQLITE_A
4101d 50 49 20 65 78 74 65 72 6e 20 63 6f 6e 73 74 20  PI extern const 
4101e 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
4101f 69 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f 61 70  ines *sqlite3_ap
41020 69 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  i;..#endif..../*
41021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
41022 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e  clude fts3_token
41023 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  izer.h in the mi
41024 64 64 6c 65 20 6f 66 20 66 74 73 33 49 6e 74 2e  ddle of fts3Int.
41025 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  h **********/../
41026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
41027 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74  egin file fts3_t
41028 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a  okenizer.h *****
41029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4102a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
4102b 2f 2a 0d 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79  /*..** 2006 July
4102c 20 31 30 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   10..**..** The 
4102d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
4102e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
4102f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 0d  is source code..
41030 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
41031 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
41032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
41033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
41034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
41035 2a 2a 20 44 65 66 69 6e 65 73 20 74 68 65 20 69  ** Defines the i
41036 6e 74 65 72 66 61 63 65 20 74 6f 20 74 6f 6b 65  nterface to toke
41037 6e 69 7a 65 72 73 20 75 73 65 64 20 62 79 20 66  nizers used by f
41038 75 6c 6c 74 65 78 74 2d 73 65 61 72 63 68 2e 20  ulltext-search. 
41039 20 54 68 65 72 65 0d 0a 2a 2a 20 61 72 65 20 74   There..** are t
4103a 68 72 65 65 20 62 61 73 69 63 20 63 6f 6d 70 6f  hree basic compo
4103b 6e 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 73  nents:..**..** s
4103c 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
4103d 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73 69 6e  _module is a sin
4103e 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e 67 20  gleton defining 
4103f 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0d 0a 2a  the tokenizer..*
41040 2a 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63  * interface func
41041 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73 20  tions.  This is 
41042 65 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65 20  essentially the 
41043 63 6c 61 73 73 20 73 74 72 75 63 74 75 72 65 20  class structure 
41044 66 6f 72 0d 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65  for..** tokenize
41045 72 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69  rs...**..** sqli
41046 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73  te3_tokenizer is
41047 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20   used to define 
41048 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b  a particular tok
41049 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0d  enizer, perhaps.
4104a 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  .** including cu
4104b 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f  stomization info
4104c 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  rmation defined 
4104d 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65  at creation time
4104e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 73 71 6c 69 74 65  ...**..** sqlite
4104f 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
41050 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  or is generated 
41051 62 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74  by a tokenizer t
41052 6f 20 67 65 6e 65 72 61 74 65 0d 0a 2a 2a 20 74  o generate..** t
41053 6f 6b 65 6e 73 20 66 72 6f 6d 20 61 20 70 61 72  okens from a par
41054 74 69 63 75 6c 61 72 20 69 6e 70 75 74 2e 0d 0a  ticular input...
41055 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 5f 46 54 53  */..#ifndef _FTS
41056 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0d 0a  3_TOKENIZER_H_..
41057 23 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 54 4f  #define _FTS3_TO
41058 4b 45 4e 49 5a 45 52 5f 48 5f 0d 0a 0d 0a 2f 2a  KENIZER_H_..../*
41059 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f 6e 6c   TODO(shess) Onl
4105a 79 20 75 73 65 64 20 66 6f 72 20 53 51 4c 49 54  y used for SQLIT
4105b 45 5f 4f 4b 20 61 6e 64 20 53 51 4c 49 54 45 5f  E_OK and SQLITE_
4105c 44 4f 4e 45 20 61 74 20 74 68 69 73 20 74 69 6d  DONE at this tim
4105d 65 2e 0d 0a 2a 2a 20 49 66 20 74 6f 6b 65 6e 69  e...** If tokeni
4105e 7a 65 72 73 20 61 72 65 20 74 6f 20 62 65 20 61  zers are to be a
4105f 6c 6c 6f 77 65 64 20 74 6f 20 63 61 6c 6c 20 73  llowed to call s
41060 71 6c 69 74 65 33 5f 2a 28 29 20 66 75 6e 63 74  qlite3_*() funct
41061 69 6f 6e 73 2c 20 74 68 65 6e 0d 0a 2a 2a 20 77  ions, then..** w
41062 65 20 77 69 6c 6c 20 6e 65 65 64 20 61 20 77 61  e will need a wa
41063 79 20 74 6f 20 72 65 67 69 73 74 65 72 20 74 68  y to register th
41064 65 20 41 50 49 20 63 6f 6e 73 69 73 74 65 6e 74  e API consistent
41065 6c 79 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  ly...*/..../*..*
41066 2a 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65  * Structures use
41067 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
41068 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57 68  er interface. Wh
41069 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  en a new tokeniz
4106a 65 72 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  er..** implement
4106b 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65  ation is registe
4106c 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  red, the caller 
4106d 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74  provides a point
4106e 65 72 20 74 6f 0d 0a 2a 2a 20 61 6e 20 73 71 6c  er to..** an sql
4106f 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
41070 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  odule containing
41071 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
41072 20 63 61 6c 6c 62 61 63 6b 0d 0a 2a 2a 20 66 75   callback..** fu
41073 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b  nctions that mak
41074 65 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e  e up an implemen
41075 74 61 74 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  tation...**..** 
41076 57 68 65 6e 20 61 6e 20 66 74 73 33 20 74 61 62  When an fts3 tab
41077 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 20 69  le is created, i
41078 74 20 70 61 73 73 65 73 20 61 6e 79 20 61 72 67  t passes any arg
41079 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f  uments passed to
4107a 0d 0a 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a  ..** the tokeniz
4107b 65 72 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  er clause of the
4107c 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
4107d 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
4107e 74 6f 20 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74  to the..** sqlit
4107f 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
41080 75 6c 65 2e 78 43 72 65 61 74 65 28 29 20 66 75  ule.xCreate() fu
41081 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  nction of the re
41082 71 75 65 73 74 65 64 20 74 6f 6b 65 6e 69 7a 65  quested tokenize
41083 72 0d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  r..** implementa
41084 74 69 6f 6e 2e 20 54 68 65 20 78 43 72 65 61 74  tion. The xCreat
41085 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20  e() function in 
41086 74 75 72 6e 20 72 65 74 75 72 6e 73 20 61 6e 20  turn returns an 
41087 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ..** sqlite3_tok
41088 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72 65  enizer structure
41089 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
4108a 65 20 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e  e specific token
4108b 69 7a 65 72 20 74 6f 0d 0a 2a 2a 20 62 65 20 75  izer to..** be u
4108c 73 65 64 20 66 6f 72 20 74 68 65 20 66 74 73 33  sed for the fts3
4108d 20 74 61 62 6c 65 20 28 63 75 73 74 6f 6d 69 7a   table (customiz
4108e 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69  ed by the tokeni
4108f 7a 65 72 20 63 6c 61 75 73 65 20 61 72 67 75 6d  zer clause argum
41090 65 6e 74 73 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ents)...**..** T
41091 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69 6e  o tokenize an in
41092 70 75 74 20 62 75 66 66 65 72 2c 20 74 68 65 20  put buffer, the 
41093 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
41094 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 28 29  r_module.xOpen()
41095 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20 63  ..** method is c
41096 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75 72 6e  alled. It return
41097 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b  s an sqlite3_tok
41098 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 6f 62  enizer_cursor ob
41099 6a 65 63 74 0d 0a 2a 2a 20 74 68 61 74 20 6d 61  ject..** that ma
4109a 79 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f 6b  y be used to tok
4109b 65 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69 63  enize a specific
4109c 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 62 61   input buffer ba
4109d 73 65 64 20 6f 6e 0d 0a 2a 2a 20 74 68 65 20 74  sed on..** the t
4109e 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65  okenization rule
4109f 73 20 73 75 70 70 6c 69 65 64 20 62 79 20 61 20  s supplied by a 
410a0 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65 33  specific sqlite3
410a1 5f 74 6f 6b 65 6e 69 7a 65 72 0d 0a 2a 2a 20 6f  _tokenizer..** o
410a2 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  bject...*/..type
410a3 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
410a4 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
410a5 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule sqlite3_toke
410a6 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0d 0a 74  nizer_module;..t
410a7 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
410a8 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
410a9 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
410aa 72 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75  r;..typedef stru
410ab 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ct sqlite3_token
410ac 69 7a 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69  izer_cursor sqli
410ad 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
410ae 72 73 6f 72 3b 0d 0a 0d 0a 73 74 72 75 63 74 20  rsor;....struct 
410af 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
410b0 72 5f 6d 6f 64 75 6c 65 20 7b 0d 0a 0d 0a 20 20  r_module {....  
410b1 2f 2a 0d 0a 20 20 2a 2a 20 53 74 72 75 63 74 75  /*..  ** Structu
410b2 72 65 20 76 65 72 73 69 6f 6e 2e 20 53 68 6f 75  re version. Shou
410b3 6c 64 20 61 6c 77 61 79 73 20 62 65 20 73 65 74  ld always be set
410b4 20 74 6f 20 30 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   to 0...  */..  
410b5 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0d 0a 0d  int iVersion;...
410b6 0a 20 20 2f 2a 0d 0a 20 20 2a 2a 20 43 72 65 61  .  /*..  ** Crea
410b7 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  te a new tokeniz
410b8 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 69  er. The values i
410b9 6e 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72  n the argv[] arr
410ba 61 79 20 61 72 65 20 74 68 65 0d 0a 20 20 2a 2a  ay are the..  **
410bb 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
410bc 64 20 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69  d to the "tokeni
410bd 7a 65 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74  zer" clause of t
410be 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41  he CREATE VIRTUA
410bf 4c 0d 0a 20 20 2a 2a 20 54 41 42 4c 45 20 73 74  L..  ** TABLE st
410c0 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 72 65  atement that cre
410c1 61 74 65 64 20 74 68 65 20 66 74 73 33 20 74 61  ated the fts3 ta
410c2 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ble. For example
410c3 2c 20 69 66 0d 0a 20 20 2a 2a 20 74 68 65 20 66  , if..  ** the f
410c4 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73 20  ollowing SQL is 
410c5 65 78 65 63 75 74 65 64 3a 0d 0a 20 20 2a 2a 0d  executed:..  **.
410c6 0a 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e  .  **   CREATE .
410c7 2e 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e  . USING fts3( ..
410c8 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74  . , tokenizer <t
410c9 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61  okenizer-name> a
410ca 72 67 31 20 61 72 67 32 29 0d 0a 20 20 2a 2a 0d  rg1 arg2)..  **.
410cb 0a 20 20 2a 2a 20 74 68 65 6e 20 61 72 67 63 20  .  ** then argc 
410cc 69 73 20 73 65 74 20 74 6f 20 32 2c 20 61 6e 64  is set to 2, and
410cd 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61   the argv[] arra
410ce 79 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74  y contains point
410cf 65 72 73 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65  ers..  ** to the
410d0 20 73 74 72 69 6e 67 73 20 22 61 72 67 31 22 20   strings "arg1" 
410d1 61 6e 64 20 22 61 72 67 32 22 2e 0d 0a 20 20 2a  and "arg2"...  *
410d2 2a 0d 0a 20 20 2a 2a 20 54 68 69 73 20 6d 65 74  *..  ** This met
410d3 68 6f 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72  hod should retur
410d4 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  n either SQLITE_
410d5 4f 4b 20 28 30 29 2c 20 6f 72 20 61 6e 20 53 51  OK (0), or an SQ
410d6 4c 69 74 65 20 65 72 72 6f 72 20 0d 0a 20 20 2a  Lite error ..  *
410d7 2a 20 63 6f 64 65 2e 20 49 66 20 53 51 4c 49 54  * code. If SQLIT
410d8 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
410d9 2c 20 74 68 65 6e 20 2a 70 70 54 6f 6b 65 6e 69  , then *ppTokeni
410da 7a 65 72 20 73 68 6f 75 6c 64 20 62 65 20 73 65  zer should be se
410db 74 0d 0a 20 20 2a 2a 20 74 6f 20 70 6f 69 6e 74  t..  ** to point
410dc 20 61 74 20 74 68 65 20 6e 65 77 6c 79 20 63 72   at the newly cr
410dd 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20  eated tokenizer 
410de 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 67  structure. The g
410df 65 6e 65 72 69 63 0d 0a 20 20 2a 2a 20 73 71 6c  eneric..  ** sql
410e0 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 70  ite3_tokenizer.p
410e1 4d 6f 64 75 6c 65 20 76 61 72 69 61 62 6c 65 20  Module variable 
410e2 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 69 6e  should not be in
410e3 69 74 69 61 6c 69 73 65 64 20 62 79 0d 0a 20 20  itialised by..  
410e4 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b  ** this callback
410e5 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  . The caller wil
410e6 6c 20 64 6f 20 73 6f 2e 0d 0a 20 20 2a 2f 0d 0a  l do so...  */..
410e7 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29    int (*xCreate)
410e8 28 0d 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c  (..    int argc,
410e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
410ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
410eb 7a 65 20 6f 66 20 61 72 67 76 20 61 72 72 61 79  ze of argv array
410ec 20 2a 2f 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63   */..    const c
410ed 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
410ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
410ef 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75 6d 65  Tokenizer argume
410f0 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0d 0a 20  nt strings */.. 
410f1 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
410f2 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
410f3 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43  er     /* OUT: C
410f4 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72  reated tokenizer
410f5 20 2a 2f 0d 0a 20 20 29 3b 0d 0a 0d 0a 20 20 2f   */..  );....  /
410f6 2a 0d 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20  *..  ** Destroy 
410f7 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65  an existing toke
410f8 6e 69 7a 65 72 2e 20 54 68 65 20 66 74 73 33 20  nizer. The fts3 
410f9 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69  module calls thi
410fa 73 20 6d 65 74 68 6f 64 0d 0a 20 20 2a 2a 20 65  s method..  ** e
410fb 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20  xactly once for 
410fc 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c 20  each successful 
410fd 63 61 6c 6c 20 74 6f 20 78 43 72 65 61 74 65 28  call to xCreate(
410fe 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20  )...  */..  int 
410ff 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69  (*xDestroy)(sqli
41100 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
41101 54 6f 6b 65 6e 69 7a 65 72 29 3b 0d 0a 0d 0a 20  Tokenizer);.... 
41102 20 2f 2a 0d 0a 20 20 2a 2a 20 43 72 65 61 74 65   /*..  ** Create
41103 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72   a tokenizer cur
41104 73 6f 72 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20  sor to tokenize 
41105 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72 2e  an input buffer.
41106 20 54 68 65 20 63 61 6c 6c 65 72 0d 0a 20 20 2a   The caller..  *
41107 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  * is responsible
41108 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68   for ensuring th
41109 61 74 20 74 68 65 20 69 6e 70 75 74 20 62 75 66  at the input buf
4110a 66 65 72 20 72 65 6d 61 69 6e 73 20 76 61 6c 69  fer remains vali
4110b 64 0d 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68  d..  ** until th
4110c 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  e cursor is clos
4110d 65 64 20 28 75 73 69 6e 67 20 74 68 65 20 78 43  ed (using the xC
4110e 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29 2e 20  lose() method). 
4110f 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a  ..  */..  int (*
41110 78 4f 70 65 6e 29 28 0d 0a 20 20 20 20 73 71 6c  xOpen)(..    sql
41111 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
41112 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
41113 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f    /* Tokenizer o
41114 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 20 20 63 6f  bject */..    co
41115 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74  nst char *pInput
41116 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20  , int nBytes,   
41117 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66 66     /* Input buff
41118 65 72 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74  er */..    sqlit
41119 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
4111a 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
4111b 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
4111c 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  tokenizer cursor
4111d 20 2a 2f 0d 0a 20 20 29 3b 0d 0a 0d 0a 20 20 2f   */..  );....  /
4111e 2a 0d 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20  *..  ** Destroy 
4111f 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65  an existing toke
41120 6e 69 7a 65 72 20 63 75 72 73 6f 72 2e 20 54 68  nizer cursor. Th
41121 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63 61  e fts3 module ca
41122 6c 6c 73 20 74 68 69 73 20 0d 0a 20 20 2a 2a 20  lls this ..  ** 
41123 6d 65 74 68 6f 64 20 65 78 61 63 74 6c 79 20 6f  method exactly o
41124 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63  nce for each suc
41125 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
41126 78 4f 70 65 6e 28 29 2e 0d 0a 20 20 2a 2f 0d 0a  xOpen()...  */..
41127 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28    int (*xClose)(
41128 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
41129 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
4112a 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 0d 0a 20 20 2a  r);....  /*..  *
4112b 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20 6e  * Retrieve the n
4112c 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74  ext token from t
4112d 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72  he tokenizer cur
4112e 73 6f 72 20 70 43 75 72 73 6f 72 2e 20 54 68 69  sor pCursor. Thi
4112f 73 0d 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 73  s..  ** method s
41130 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72 65 74  hould either ret
41131 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
41132 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 73  d set the values
41133 20 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 22 4f   of the..  ** "O
41134 55 54 22 20 76 61 72 69 61 62 6c 65 73 20 69 64  UT" variables id
41135 65 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c 20  entified below, 
41136 6f 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74  or SQLITE_DONE t
41137 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0d  o indicate that.
41138 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  .  ** the end of
41139 20 74 68 65 20 62 75 66 66 65 72 20 68 61 73 20   the buffer has 
4113a 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72  been reached, or
4113b 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
4113c 20 63 6f 64 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20   code...  **..  
4113d 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f 75  ** *ppToken shou
4113e 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69  ld be set to poi
4113f 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20 63  nt at a buffer c
41140 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0d 0a  ontaining the ..
41141 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20    ** normalized 
41142 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  version of the t
41143 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72  oken (i.e. after
41144 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e   any case-foldin
41145 67 20 61 6e 64 2f 6f 72 0d 0a 20 20 2a 2a 20 73  g and/or..  ** s
41146 74 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e  temming has been
41147 20 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e   performed). *pn
41148 42 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20  Bytes should be 
41149 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74  set to the lengt
4114a 68 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20  h..  ** of this 
4114b 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e  buffer in bytes.
4114c 20 54 68 65 20 69 6e 70 75 74 20 74 65 78 74 20   The input text 
4114d 74 68 61 74 20 67 65 6e 65 72 61 74 65 64 20 74  that generated t
4114e 68 65 20 74 6f 6b 65 6e 20 69 73 0d 0a 20 20 2a  he token is..  *
4114f 2a 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  * identified by 
41150 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 73  the byte offsets
41151 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 69   returned in *pi
41152 53 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64 0d  StartOffset and.
41153 0a 20 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66 73  .  ** *piEndOffs
41154 65 74 2e 20 2a 70 69 53 74 61 72 74 4f 66 66 73  et. *piStartOffs
41155 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74  et should be set
41156 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
41157 20 74 68 65 20 66 69 72 73 74 0d 0a 20 20 2a 2a   the first..  **
41158 20 62 79 74 65 20 6f 66 20 74 68 65 20 74 6f 6b   byte of the tok
41159 65 6e 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20  en in the input 
4115a 62 75 66 66 65 72 2e 20 2a 70 69 45 6e 64 4f 66  buffer. *piEndOf
4115b 66 73 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73  fset should be s
4115c 65 74 0d 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  et..  ** to the 
4115d 69 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 72  index of the fir
4115e 73 74 20 62 79 74 65 20 6a 75 73 74 20 70 61 73  st byte just pas
4115f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
41160 20 74 6f 6b 65 6e 20 69 6e 0d 0a 20 20 2a 2a 20   token in..  ** 
41161 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72  the input buffer
41162 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68  ...  **..  ** Th
41163 65 20 62 75 66 66 65 72 20 2a 70 70 54 6f 6b 65  e buffer *ppToke
41164 6e 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  n is set to poin
41165 74 20 61 74 20 69 73 20 6d 61 6e 61 67 65 64 20  t at is managed 
41166 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  by the tokenizer
41167 0d 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ..  ** implement
41168 61 74 69 6f 6e 2e 20 49 74 20 69 73 20 6f 6e 6c  ation. It is onl
41169 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  y required to be
4116a 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
4116b 20 6e 65 78 74 20 63 61 6c 6c 0d 0a 20 20 2a 2a   next call..  **
4116c 20 74 6f 20 78 4e 65 78 74 28 29 20 6f 72 20 78   to xNext() or x
4116d 43 6c 6f 73 65 28 29 2e 20 0d 0a 20 20 2a 2f 0d  Close(). ..  */.
4116e 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
4116f 29 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d  ) current implem
41170 65 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65  entation require
41171 73 20 70 49 6e 70 75 74 20 74 6f 20 62 65 0d 0a  s pInput to be..
41172 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61    ** nul-termina
41173 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f 75 6c  ted.  This shoul
41174 64 20 65 69 74 68 65 72 20 62 65 20 66 69 78 65  d either be fixe
41175 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e 42 79  d, or pInput/nBy
41176 74 65 73 0d 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  tes..  ** should
41177 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
41178 20 7a 49 6e 70 75 74 2e 0d 0a 20 20 2a 2f 0d 0a   zInput...  */..
41179 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0d    int (*xNext)(.
4117a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  .    sqlite3_tok
4117b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
4117c 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b  Cursor,   /* Tok
4117d 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
4117e 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ..    const char
4117f 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20   **ppToken, int 
41180 2a 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55  *pnBytes,  /* OU
41181 54 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65  T: Normalized te
41182 78 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0d  xt for token */.
41183 0a 20 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72  .    int *piStar
41184 74 4f 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54  tOffset,  /* OUT
41185 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  : Byte offset of
41186 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20   token in input 
41187 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 20 20 69  buffer */..    i
41188 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
41189 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65      /* OUT: Byte
4118a 20 6f 66 66 73 65 74 20 6f 66 20 65 6e 64 20 6f   offset of end o
4118b 66 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74  f token in input
4118c 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 20 20   buffer */..    
4118d 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20  int *piPosition 
4118e 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
4118f 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65  ber of tokens re
41190 74 75 72 6e 65 64 20 62 65 66 6f 72 65 20 74 68  turned before th
41191 69 73 20 6f 6e 65 20 2a 2f 0d 0a 20 20 29 3b 0d  is one */..  );.
41192 0a 7d 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 73 71  .};....struct sq
41193 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
41194 7b 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  {..  const sqlit
41195 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
41196 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f  ule *pModule;  /
41197 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72  * The module for
41198 20 74 68 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20   this tokenizer 
41199 2a 2f 0d 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a  */..  /* Tokeniz
4119a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  er implementatio
4119b 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c  ns will typicall
4119c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
4119d 20 66 69 65 6c 64 73 20 2a 2f 0d 0a 7d 3b 0d 0a   fields */..};..
4119e 0d 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ..struct sqlite3
4119f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
411a0 72 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74  r {..  sqlite3_t
411a1 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
411a2 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
411a3 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
411a4 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0d 0a 20 20  s cursor. */..  
411a5 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70  /* Tokenizer imp
411a6 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
411a7 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
411a8 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
411a9 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 69 6e 74 20  s */..};....int 
411aa 66 74 73 33 5f 67 6c 6f 62 61 6c 5f 74 65 72 6d  fts3_global_term
411ab 5f 63 6e 74 28 69 6e 74 20 69 54 65 72 6d 2c 20  _cnt(int iTerm, 
411ac 69 6e 74 20 69 43 6f 6c 29 3b 0d 0a 69 6e 74 20  int iCol);..int 
411ad 66 74 73 33 5f 74 65 72 6d 5f 63 6e 74 28 69 6e  fts3_term_cnt(in
411ae 74 20 69 54 65 72 6d 2c 20 69 6e 74 20 69 43 6f  t iTerm, int iCo
411af 6c 29 3b 0d 0a 0d 0a 0d 0a 23 65 6e 64 69 66 20  l);......#endif 
411b0 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a  /* _FTS3_TOKENIZ
411b1 45 52 5f 48 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a  ER_H_ */..../***
411b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
411b3 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  of fts3_tokenize
411b4 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.h ************
411b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
411b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
411b8 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
411b9 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
411ba 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3Int.h *********
411bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
411bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
411bd 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73 68 2e  clude fts3_hash.
411be 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
411bf 6f 66 20 66 74 73 33 49 6e 74 2e 68 20 2a 2a 2a  of fts3Int.h ***
411c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
411c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
411c2 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68  egin file fts3_h
411c3 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
411c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
411c6 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  /*..** 2001 Sept
411c7 65 6d 62 65 72 20 32 32 0d 0a 2a 2a 0d 0a 2a 2a  ember 22..**..**
411c8 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
411c9 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
411ca 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
411cb 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
411cc 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
411cd 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
411ce 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
411cf 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
411d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
411d1 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
411d2 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
411d3 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
411d4 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
411d5 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
411d6 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
411d7 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
411d8 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
411d9 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
411da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
411de 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  ..** This is the
411df 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72   header file for
411e0 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73   the generic has
411e1 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  h-table implemen
411e2 61 74 69 6f 6e 0d 0a 2a 2a 20 75 73 65 64 20 69  ation..** used i
411e3 6e 20 53 51 4c 69 74 65 2e 20 20 57 65 27 76 65  n SQLite.  We've
411e4 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69   modified it sli
411e5 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61  ghtly to serve a
411e6 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0d 0a  s a standalone..
411e7 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 69 6d  ** hash table im
411e8 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
411e9 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
411ea 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0d  ndexing module..
411eb 0a 2a 2a 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66  .**..*/..#ifndef
411ec 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0d 0a   _FTS3_HASH_H_..
411ed 23 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 48 41  #define _FTS3_HA
411ee 53 48 5f 48 5f 0d 0a 0d 0a 2f 2a 20 46 6f 72 77  SH_H_..../* Forw
411ef 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
411f0 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e 20   of structures. 
411f1 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75  */..typedef stru
411f2 63 74 20 46 74 73 33 48 61 73 68 20 46 74 73 33  ct Fts3Hash Fts3
411f3 48 61 73 68 3b 0d 0a 74 79 70 65 64 65 66 20 73  Hash;..typedef s
411f4 74 72 75 63 74 20 46 74 73 33 48 61 73 68 45 6c  truct Fts3HashEl
411f5 65 6d 20 46 74 73 33 48 61 73 68 45 6c 65 6d 3b  em Fts3HashElem;
411f6 0d 0a 0d 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74  ..../* A complet
411f7 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
411f8 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
411f9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
411fa 75 63 74 75 72 65 2e 0d 0a 2a 2a 20 54 68 65 20  ucture...** The 
411fb 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69  internals of thi
411fc 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
411fd 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
411fe 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0d  paque -- client.
411ff 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  .** code should 
41200 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61  not attempt to a
41201 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20  ccess or modify 
41202 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  the fields of th
41203 69 73 20 73 74 72 75 63 74 75 72 65 0d 0a 2a 2a  is structure..**
41204 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e   directly.  Chan
41205 67 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ge this structur
41206 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20  e only by using 
41207 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  the routines bel
41208 6f 77 2e 0d 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ow...** However,
41209 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70 72   many of the "pr
4120a 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 66  ocedures" and "f
4120b 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f  unctions" for mo
4120c 64 69 66 79 69 6e 67 20 61 6e 64 0d 0a 2a 2a 20  difying and..** 
4120d 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
4120e 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
4120f 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
41210 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
41211 61 6b 65 0d 0a 2a 2a 20 74 68 69 73 20 73 74 72  ake..** this str
41212 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0d 0a  ucture opaque...
41213 2a 2f 0d 0a 73 74 72 75 63 74 20 46 74 73 33 48  */..struct Fts3H
41214 61 73 68 20 7b 0d 0a 20 20 63 68 61 72 20 6b 65  ash {..  char ke
41215 79 43 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20  yClass;         
41216 20 2f 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50   /* HASH_INT, _P
41217 4f 49 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c  OINTER, _STRING,
41218 20 5f 42 49 4e 41 52 59 20 2a 2f 0d 0a 20 20 63   _BINARY */..  c
41219 68 61 72 20 63 6f 70 79 4b 65 79 3b 20 20 20 20  har copyKey;    
4121a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
4121b 66 20 63 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61  f copy of key ma
4121c 64 65 20 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0d  de on insert */.
4121d 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20  .  int count;   
4121e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
4121f 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
41220 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  in this table */
41221 0d 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  ..  Fts3HashElem
41222 20 2a 66 69 72 73 74 3b 20 20 20 20 2f 2a 20 54   *first;    /* T
41223 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
41224 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f   of the array */
41225 0d 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b 20  ..  int htsize; 
41226 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
41227 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73  umber of buckets
41228 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
41229 6c 65 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  le */..  struct 
4122a 5f 66 74 73 33 68 74 20 7b 20 20 20 20 20 20 20  _fts3ht {       
4122b 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
4122c 6c 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 63  le */..    int c
4122d 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
4122e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
4122f 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68   entries with th
41230 69 73 20 68 61 73 68 20 2a 2f 0d 0a 20 20 20 20  is hash */..    
41231 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 63 68  Fts3HashElem *ch
41232 61 69 6e 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ain;     /* Poin
41233 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74  ter to first ent
41234 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73  ry with this has
41235 68 20 2a 2f 0d 0a 20 20 7d 20 2a 68 74 3b 0d 0a  h */..  } *ht;..
41236 7d 3b 0d 0a 0d 0a 2f 2a 20 45 61 63 68 20 65 6c  };..../* Each el
41237 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73  ement in the has
41238 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e  h table is an in
41239 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
4123a 6c 6c 6f 77 69 6e 67 20 0d 0a 2a 2a 20 73 74 72  llowing ..** str
4123b 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65  ucture.  All ele
4123c 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64  ments are stored
4123d 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75   on a single dou
4123e 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
4123f 0d 0a 2a 2a 0d 0a 2a 2a 20 41 67 61 69 6e 2c 20  ..**..** Again, 
41240 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
41241 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
41242 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20   opaque, but it 
41243 63 61 6e 27 74 20 72 65 61 6c 6c 79 0d 0a 2a 2a  can't really..**
41244 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75   be opaque becau
41245 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79  se it is used by
41246 20 6d 61 63 72 6f 73 2e 0d 0a 2a 2f 0d 0a 73 74   macros...*/..st
41247 72 75 63 74 20 46 74 73 33 48 61 73 68 45 6c 65  ruct Fts3HashEle
41248 6d 20 7b 0d 0a 20 20 46 74 73 33 48 61 73 68 45  m {..  Fts3HashE
41249 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76  lem *next, *prev
4124a 3b 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 72  ; /* Next and pr
4124b 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20  evious elements 
4124c 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d  in the table */.
4124d 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20  .  void *data;  
4124e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4124f 20 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64   Data associated
41250 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
41251 6e 74 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70  nt */..  void *p
41252 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20  Key; int nKey;  
41253 20 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63      /* Key assoc
41254 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
41255 65 6c 65 6d 65 6e 74 20 2a 2f 0d 0a 7d 3b 0d 0a  element */..};..
41256 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 72 65 20 61  ../*..** There a
41257 72 65 20 32 20 64 69 66 66 65 72 65 6e 74 20 6d  re 2 different m
41258 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74 69 6f  odes of operatio
41259 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74 61 62  n for a hash tab
4125a 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 46 54  le:..**..**   FT
4125b 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20  S3_HASH_STRING  
4125c 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74        pKey point
4125d 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68  s to a string th
4125e 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65 73  at is nKey bytes
4125f 20 6c 6f 6e 67 0d 0a 2a 2a 20 20 20 20 20 20 20   long..**       
41260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41261 20 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74      (including t
41262 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  he null-terminat
41263 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61  or, if any).  Ca
41264 73 65 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  se..**          
41265 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41266 20 69 73 20 72 65 73 70 65 63 74 65 64 20 69 6e   is respected in
41267 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0d 0a 2a   comparisons...*
41268 2a 0d 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53  *..**   FTS3_HAS
41269 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20 20 20  H_BINARY        
4126a 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62  pKey points to b
4126b 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20  inary data nKey 
4126c 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0d 0a 2a 2a  bytes long. ..**
4126d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4126e 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d             memcm
4126f 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63  p() is used to c
41270 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0d 0a 2a 2a  ompare keys...**
41271 0d 0a 2a 2a 20 41 20 63 6f 70 79 20 6f 66 20 74  ..** A copy of t
41272 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69  he key is made i
41273 66 20 74 68 65 20 63 6f 70 79 4b 65 79 20 70 61  f the copyKey pa
41274 72 61 6d 65 74 65 72 20 74 6f 20 66 74 73 33 48  rameter to fts3H
41275 61 73 68 49 6e 69 74 20 69 73 20 31 2e 20 20 0d  ashInit is 1.  .
41276 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46 54 53  .*/..#define FTS
41277 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20 20  3_HASH_STRING   
41278 20 31 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 33   1..#define FTS3
41279 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20 20  _HASH_BINARY    
4127a 32 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 63 63 65  2..../*..** Acce
4127b 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f  ss routines.  To
4127c 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20   delete, insert 
4127d 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0d  a NULL pointer..
4127e 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
4127f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
41280 46 74 73 33 48 61 73 68 49 6e 69 74 28 46 74 73  Fts3HashInit(Fts
41281 33 48 61 73 68 20 2a 70 4e 65 77 2c 20 63 68 61  3Hash *pNew, cha
41282 72 20 6b 65 79 43 6c 61 73 73 2c 20 63 68 61 72  r keyClass, char
41283 20 63 6f 70 79 4b 65 79 29 3b 0d 0a 53 51 4c 49   copyKey);..SQLI
41284 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
41285 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68  *sqlite3Fts3Hash
41286 49 6e 73 65 72 74 28 46 74 73 33 48 61 73 68 2a  Insert(Fts3Hash*
41287 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
41288 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f  ey, int nKey, vo
41289 69 64 20 2a 70 44 61 74 61 29 3b 0d 0a 53 51 4c  id *pData);..SQL
4128a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4128b 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
4128c 68 46 69 6e 64 28 63 6f 6e 73 74 20 46 74 73 33  hFind(const Fts3
4128d 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  Hash*, const voi
4128e 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
4128f 79 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  y);..SQLITE_PRIV
41290 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
41291 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 46 74  Fts3HashClear(Ft
41292 73 33 48 61 73 68 2a 29 3b 0d 0a 53 51 4c 49 54  s3Hash*);..SQLIT
41293 45 5f 50 52 49 56 41 54 45 20 46 74 73 33 48 61  E_PRIVATE Fts3Ha
41294 73 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 46  shElem *sqlite3F
41295 74 73 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28  ts3HashFindElem(
41296 63 6f 6e 73 74 20 46 74 73 33 48 61 73 68 20 2a  const Fts3Hash *
41297 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20  , const void *, 
41298 69 6e 74 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  int);..../*..** 
41299 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20 74 68  Shorthand for th
4129a 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76  e functions abov
4129b 65 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 66  e..*/..#define f
4129c 74 73 33 48 61 73 68 49 6e 69 74 20 20 20 20 20  ts3HashInit     
4129d 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
4129e 6e 69 74 0d 0a 23 64 65 66 69 6e 65 20 66 74 73  nit..#define fts
4129f 33 48 61 73 68 49 6e 73 65 72 74 20 20 20 73 71  3HashInsert   sq
412a0 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
412a1 65 72 74 0d 0a 23 64 65 66 69 6e 65 20 66 74 73  ert..#define fts
412a2 33 48 61 73 68 46 69 6e 64 20 20 20 20 20 73 71  3HashFind     sq
412a3 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
412a4 64 0d 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  d..#define fts3H
412a5 61 73 68 43 6c 65 61 72 20 20 20 20 73 71 6c 69  ashClear    sqli
412a6 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
412a7 0d 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  ..#define fts3Ha
412a8 73 68 46 69 6e 64 45 6c 65 6d 20 73 71 6c 69 74  shFindElem sqlit
412a9 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 45 6c  e3Fts3HashFindEl
412aa 65 6d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 63  em..../*..** Mac
412ab 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
412ac 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
412ad 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
412ae 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
412af 0d 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0d  ..** like this:.
412b0 0a 2a 2a 0d 0a 2a 2a 20 20 20 46 74 73 33 48 61  .**..**   Fts3Ha
412b1 73 68 20 68 3b 0d 0a 2a 2a 20 20 20 46 74 73 33  sh h;..**   Fts3
412b2 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0d 0a 2a 2a  HashElem *p;..**
412b3 20 20 20 2e 2e 2e 0d 0a 2a 2a 20 20 20 66 6f 72     .....**   for
412b4 28 70 3d 66 74 73 33 48 61 73 68 46 69 72 73 74  (p=fts3HashFirst
412b5 28 26 68 29 3b 20 70 3b 20 70 3d 66 74 73 33 48  (&h); p; p=fts3H
412b6 61 73 68 4e 65 78 74 28 70 29 29 7b 0d 0a 2a 2a  ashNext(p)){..**
412b7 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75       SomeStructu
412b8 72 65 20 2a 70 44 61 74 61 20 3d 20 66 74 73 33  re *pData = fts3
412b9 48 61 73 68 44 61 74 61 28 70 29 3b 0d 0a 2a 2a  HashData(p);..**
412ba 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74       // do somet
412bb 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 0d  hing with pData.
412bc 0a 2a 2a 20 20 20 7d 0d 0a 2a 2f 0d 0a 23 64 65  .**   }..*/..#de
412bd 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 72  fine fts3HashFir
412be 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
412bf 73 74 29 0d 0a 23 64 65 66 69 6e 65 20 66 74 73  st)..#define fts
412c0 33 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28  3HashNext(E)   (
412c1 28 45 29 2d 3e 6e 65 78 74 29 0d 0a 23 64 65 66  (E)->next)..#def
412c2 69 6e 65 20 66 74 73 33 48 61 73 68 44 61 74 61  ine fts3HashData
412c3 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61  (E)   ((E)->data
412c4 29 0d 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  )..#define fts3H
412c5 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45  ashKey(E)    ((E
412c6 29 2d 3e 70 4b 65 79 29 0d 0a 23 64 65 66 69 6e  )->pKey)..#defin
412c7 65 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a  e fts3HashKeysiz
412c8 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29  e(E) ((E)->nKey)
412c9 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4e 75 6d 62 65  ..../*..** Numbe
412ca 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
412cb 61 20 68 61 73 68 20 74 61 62 6c 65 0d 0a 2a 2f  a hash table..*/
412cc 0d 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  ..#define fts3Ha
412cd 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29  shCount(H)  ((H)
412ce 2d 3e 63 6f 75 6e 74 29 0d 0a 0d 0a 23 65 6e 64  ->count)....#end
412cf 69 66 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53 48  if /* _FTS3_HASH
412d0 5f 48 5f 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  _H_ */..../*****
412d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
412d2 20 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a   fts3_hash.h ***
412d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412d5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
412d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
412d7 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
412d8 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 49  eft off in fts3I
412d9 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
412da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 0d 0a 2f 2a  *********/..../*
412db 0d 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 73 74 61  ..** This consta
412dc 6e 74 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20  nt controls how 
412dd 6f 66 74 65 6e 20 73 65 67 6d 65 6e 74 73 20 61  often segments a
412de 72 65 20 6d 65 72 67 65 64 2e 20 4f 6e 63 65 20  re merged. Once 
412df 74 68 65 72 65 20 61 72 65 0d 0a 2a 2a 20 46 54  there are..** FT
412e0 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73  S3_MERGE_COUNT s
412e1 65 67 6d 65 6e 74 73 20 6f 66 20 6c 65 76 65 6c  egments of level
412e2 20 4e 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72   N, they are mer
412e3 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  ged into a singl
412e4 65 0d 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6f 66  e..** segment of
412e5 20 6c 65 76 65 6c 20 4e 2b 31 2e 0d 0a 2a 2f 0d   level N+1...*/.
412e6 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 45  .#define FTS3_ME
412e7 52 47 45 5f 43 4f 55 4e 54 20 31 36 0d 0a 0d 0a  RGE_COUNT 16....
412e8 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  /*..** This is t
412e9 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e  he maximum amoun
412ea 74 20 6f 66 20 64 61 74 61 20 28 69 6e 20 62 79  t of data (in by
412eb 74 65 73 29 20 74 6f 20 73 74 6f 72 65 20 69 6e  tes) to store in
412ec 20 74 68 65 20 0d 0a 2a 2a 20 46 74 73 33 54 61   the ..** Fts3Ta
412ed 62 6c 65 2e 70 65 6e 64 69 6e 67 54 65 72 6d 73  ble.pendingTerms
412ee 20 68 61 73 68 20 74 61 62 6c 65 2e 20 4e 6f 72   hash table. Nor
412ef 6d 61 6c 6c 79 2c 20 74 68 65 20 68 61 73 68 20  mally, the hash 
412f0 74 61 62 6c 65 20 69 73 0d 0a 2a 2a 20 70 6f 70  table is..** pop
412f1 75 6c 61 74 65 64 20 61 73 20 64 6f 63 75 6d 65  ulated as docume
412f2 6e 74 73 20 61 72 65 20 69 6e 73 65 72 74 65 64  nts are inserted
412f3 2f 75 70 64 61 74 65 64 2f 64 65 6c 65 74 65 64  /updated/deleted
412f4 20 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f   in a transactio
412f5 6e 0d 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 74  n..** and used t
412f6 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 73  o create a new s
412f7 65 67 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20  egment when the 
412f8 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
412f9 6f 6d 6d 69 74 74 65 64 2e 0d 0a 2a 2a 20 48 6f  ommitted...** Ho
412fa 77 65 76 65 72 20 69 66 20 74 68 69 73 20 6c 69  wever if this li
412fb 6d 69 74 20 69 73 20 72 65 61 63 68 65 64 20 6d  mit is reached m
412fc 69 64 77 61 79 20 74 68 72 6f 75 67 68 20 61 20  idway through a 
412fd 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6e  transaction, a n
412fe 65 77 20 0d 0a 2a 2a 20 73 65 67 6d 65 6e 74 20  ew ..** segment 
412ff 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 74  is created and t
41300 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 63 6c  he hash table cl
41301 65 61 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  eared immediatel
41302 79 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  y...*/..#define 
41303 46 54 53 33 5f 4d 41 58 5f 50 45 4e 44 49 4e 47  FTS3_MAX_PENDING
41304 5f 44 41 54 41 20 28 31 2a 31 30 32 34 2a 31 30  _DATA (1*1024*10
41305 32 34 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61  24)..../*..** Ma
41306 63 72 6f 20 74 6f 20 72 65 74 75 72 6e 20 74 68  cro to return th
41307 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
41308 65 6e 74 73 20 69 6e 20 61 6e 20 61 72 72 61 79  ents in an array
41309 2e 20 53 51 4c 69 74 65 20 68 61 73 20 61 0d 0a  . SQLite has a..
4130a 2a 2a 20 73 69 6d 69 6c 61 72 20 6d 61 63 72 6f  ** similar macro
4130b 20 63 61 6c 6c 65 64 20 41 72 72 61 79 53 69 7a   called ArraySiz
4130c 65 28 29 2e 20 55 73 65 20 61 20 64 69 66 66 65  e(). Use a diffe
4130d 72 65 6e 74 20 6e 61 6d 65 20 74 6f 20 61 76 6f  rent name to avo
4130e 69 64 0d 0a 2a 2a 20 61 20 63 6f 6c 6c 69 73 69  id..** a collisi
4130f 6f 6e 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67  on when building
41310 20 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e   an amalgamation
41311 20 77 69 74 68 20 62 75 69 6c 74 2d 69 6e 20 46   with built-in F
41312 54 53 33 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  TS3...*/..#defin
41313 65 20 53 69 7a 65 6f 66 41 72 72 61 79 28 58 29  e SizeofArray(X)
41314 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58   ((int)(sizeof(X
41315 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29  )/sizeof(X[0])))
41316 0d 0a 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 4d 49  ......#ifndef MI
41317 4e 0d 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28  N..# define MIN(
41318 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78  x,y) ((x)<(y)?(x
41319 29 3a 28 79 29 29 0d 0a 23 65 6e 64 69 66 0d 0a  ):(y))..#endif..
4131a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 78 69 6d 75 6d  ../*..** Maximum
4131b 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 76 61 72   length of a var
4131c 69 6e 74 20 65 6e 63 6f 64 65 64 20 69 6e 74 65  int encoded inte
4131d 67 65 72 2e 20 54 68 65 20 76 61 72 69 6e 74 20  ger. The varint 
4131e 66 6f 72 6d 61 74 20 69 73 20 64 69 66 66 65 72  format is differ
4131f 65 6e 74 0d 0a 2a 2a 20 66 72 6f 6d 20 74 68 61  ent..** from tha
41320 74 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  t used by SQLite
41321 2c 20 73 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d  , so the maximum
41322 20 6c 65 6e 67 74 68 20 69 73 20 31 30 2c 20 6e   length is 10, n
41323 6f 74 20 39 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  ot 9...*/..#defi
41324 6e 65 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d  ne FTS3_VARINT_M
41325 41 58 20 31 30 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  AX 10..../*..** 
41326 46 54 53 34 20 76 69 72 74 75 61 6c 20 74 61 62  FTS4 virtual tab
41327 6c 65 73 20 6d 61 79 20 6d 61 69 6e 74 61 69 6e  les may maintain
41328 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64 65 78 65   multiple indexe
41329 73 20 2d 20 6f 6e 65 20 69 6e 64 65 78 20 6f 66  s - one index of
4132a 20 61 6c 6c 20 74 65 72 6d 73 0d 0a 2a 2a 20 69   all terms..** i
4132b 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 73  n the document s
4132c 65 74 20 61 6e 64 20 7a 65 72 6f 20 6f 72 20 6d  et and zero or m
4132d 6f 72 65 20 70 72 65 66 69 78 20 69 6e 64 65 78  ore prefix index
4132e 65 73 2e 20 41 6c 6c 20 69 6e 64 65 78 65 73 20  es. All indexes 
4132f 61 72 65 20 73 74 6f 72 65 64 0d 0a 2a 2a 20 61  are stored..** a
41330 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 62 2b  s one or more b+
41331 2d 74 72 65 65 73 20 69 6e 20 74 68 65 20 25 5f  -trees in the %_
41332 73 65 67 6d 65 6e 74 73 20 61 6e 64 20 25 5f 73  segments and %_s
41333 65 67 64 69 72 20 74 61 62 6c 65 73 2e 20 0d 0a  egdir tables. ..
41334 2a 2a 0d 0a 2a 2a 20 49 74 20 69 73 20 70 6f 73  **..** It is pos
41335 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69  sible to determi
41336 6e 65 20 77 68 69 63 68 20 69 6e 64 65 78 20 61  ne which index a
41337 20 62 2b 2d 74 72 65 65 20 62 65 6c 6f 6e 67 73   b+-tree belongs
41338 20 74 6f 20 62 61 73 65 64 20 6f 6e 20 74 68 65   to based on the
41339 0d 0a 2a 2a 20 76 61 6c 75 65 20 73 74 6f 72 65  ..** value store
4133a 64 20 69 6e 20 74 68 65 20 22 25 5f 73 65 67 64  d in the "%_segd
4133b 69 72 2e 6c 65 76 65 6c 22 20 63 6f 6c 75 6d 6e  ir.level" column
4133c 2e 20 47 69 76 65 6e 20 74 68 69 73 20 76 61 6c  . Given this val
4133d 75 65 20 4c 2c 20 74 68 65 20 69 6e 64 65 78 0d  ue L, the index.
4133e 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 62 2b 2d  .** that the b+-
4133f 74 72 65 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20  tree belongs to 
41340 69 73 20 28 4c 3c 3c 31 30 29 2e 20 49 6e 20 6f  is (L<<10). In o
41341 74 68 65 72 20 77 6f 72 64 73 2c 20 61 6c 6c 20  ther words, all 
41342 62 2b 2d 74 72 65 65 73 20 77 69 74 68 0d 0a 2a  b+-trees with..*
41343 2a 20 6c 65 76 65 6c 20 76 61 6c 75 65 73 20 62  * level values b
41344 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 30 32  etween 0 and 102
41345 33 20 28 69 6e 63 6c 75 73 69 76 65 29 20 62 65  3 (inclusive) be
41346 6c 6f 6e 67 20 74 6f 20 69 6e 64 65 78 20 30 2c  long to index 0,
41347 20 61 6c 6c 20 6c 65 76 65 6c 73 0d 0a 2a 2a 20   all levels..** 
41348 62 65 74 77 65 65 6e 20 31 30 32 34 20 61 6e 64  between 1024 and
41349 20 32 30 34 37 20 74 6f 20 69 6e 64 65 78 20 31   2047 to index 1
4134a 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0d 0a 2a 2a  , and so on...**
4134b 0d 0a 2a 2a 20 49 74 20 69 73 20 63 6f 6e 73 69  ..** It is consi
4134c 64 65 72 65 64 20 69 6d 70 6f 73 73 69 62 6c 65  dered impossible
4134d 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 6f   for an index to
4134e 20 75 73 65 20 6d 6f 72 65 20 74 68 61 6e 20 31   use more than 1
4134f 30 32 34 20 6c 65 76 65 6c 73 2e 20 49 6e 20 0d  024 levels. In .
41350 0a 2a 2a 20 74 68 65 6f 72 79 20 74 68 6f 75 67  .** theory thoug
41351 68 20 74 68 69 73 20 6d 61 79 20 68 61 70 70 65  h this may happe
41352 6e 2c 20 62 75 74 20 6f 6e 6c 79 20 61 66 74 65  n, but only afte
41353 72 20 61 74 20 6c 65 61 73 74 20 0d 0a 2a 2a 20  r at least ..** 
41354 28 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e  (FTS3_MERGE_COUN
41355 54 5e 31 30 32 34 29 20 73 65 70 61 72 61 74 65  T^1024) separate
41356 20 66 6c 75 73 68 65 73 20 6f 66 20 74 68 65 20   flushes of the 
41357 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61  pending-terms ta
41358 62 6c 65 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  bles...*/..#defi
41359 6e 65 20 46 54 53 33 5f 53 45 47 44 49 52 5f 4d  ne FTS3_SEGDIR_M
4135a 41 58 4c 45 56 45 4c 20 20 20 20 20 20 31 30 32  AXLEVEL      102
4135b 34 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  4..#define FTS3_
4135c 53 45 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 5f  SEGDIR_MAXLEVEL_
4135d 53 54 52 20 22 31 30 32 34 22 0d 0a 0d 0a 2f 2a  STR "1024"..../*
4135e 0d 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61 73  ..** The testcas
4135f 65 28 29 20 6d 61 63 72 6f 20 69 73 20 6f 6e 6c  e() macro is onl
41360 79 20 75 73 65 64 20 62 79 20 74 68 65 20 61 6d  y used by the am
41361 61 6c 67 61 6d 61 74 69 6f 6e 2e 20 20 49 66 20  algamation.  If 
41362 75 6e 64 65 66 69 6e 65 64 2c 0d 0a 2a 2a 20 6d  undefined,..** m
41363 61 6b 65 20 69 74 20 61 20 6e 6f 2d 6f 70 2e 0d  ake it a no-op..
41364 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 74 65 73  .*/..#ifndef tes
41365 74 63 61 73 65 0d 0a 23 20 64 65 66 69 6e 65 20  tcase..# define 
41366 74 65 73 74 63 61 73 65 28 58 29 0d 0a 23 65 6e  testcase(X)..#en
41367 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 65  dif..../*..** Te
41368 72 6d 69 6e 61 74 6f 72 20 76 61 6c 75 65 73 20  rminator values 
41369 66 6f 72 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73  for position-lis
4136a 74 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 2d 6c 69  ts and column-li
4136b 73 74 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e  sts...*/..#defin
4136c 65 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 20 28 31  e POS_COLUMN  (1
4136d 29 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 2d  )     /* Column-
4136e 6c 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 20  list terminator 
4136f 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 50 4f 53 5f  */..#define POS_
41370 45 4e 44 20 20 20 20 20 28 30 29 20 20 20 20 20  END     (0)     
41371 2f 2a 20 50 6f 73 69 74 69 6f 6e 2d 6c 69 73 74  /* Position-list
41372 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 20 0d   terminator */ .
41373 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 73  .../*..** This s
41374 65 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20  ection provides 
41375 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 6f 20 61  definitions to a
41376 6c 6c 6f 77 20 74 68 65 0d 0a 2a 2a 20 46 54 53  llow the..** FTS
41377 33 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f 20 62  3 extension to b
41378 65 20 63 6f 6d 70 69 6c 65 64 20 6f 75 74 73 69  e compiled outsi
41379 64 65 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 61  de of the ..** a
4137a 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0d 0a 2a 2f  malgamation...*/
4137b 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
4137c 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0d 0a 2f  _AMALGAMATION../
4137d 2a 0d 0a 2a 2a 20 4d 61 63 72 6f 73 20 69 6e 64  *..** Macros ind
4137e 69 63 61 74 69 6e 67 20 74 68 61 74 20 63 6f 6e  icating that con
4137f 64 69 74 69 6f 6e 61 6c 20 65 78 70 72 65 73 73  ditional express
41380 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20  ions are always 
41381 74 72 75 65 20 6f 72 0d 0a 2a 2a 20 66 61 6c 73  true or..** fals
41382 65 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53  e...*/..#ifdef S
41383 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
41384 45 53 54 0d 0a 23 20 64 65 66 69 6e 65 20 41 4c  EST..# define AL
41385 57 41 59 53 28 78 29 20 28 31 29 0d 0a 23 20 64  WAYS(x) (1)..# d
41386 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
41387 28 30 29 0d 0a 23 65 6c 73 65 0d 0a 23 20 64 65  (0)..#else..# de
41388 66 69 6e 65 20 41 4c 57 41 59 53 28 78 29 20 28  fine ALWAYS(x) (
41389 78 29 0d 0a 23 20 64 65 66 69 6e 65 20 4e 45 56  x)..# define NEV
4138a 45 52 28 58 29 20 20 28 78 29 0d 0a 23 65 6e 64  ER(X)  (x)..#end
4138b 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 74  if..../*..** Int
4138c 65 72 6e 61 6c 20 74 79 70 65 73 20 75 73 65 64  ernal types used
4138d 20 62 79 20 53 51 4c 69 74 65 2e 0d 0a 2a 2f 0d   by SQLite...*/.
4138e 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
4138f 64 20 63 68 61 72 20 75 38 3b 20 20 20 20 20 20  d char u8;      
41390 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 28 6f 72     /* 1-byte (or
41391 20 6c 61 72 67 65 72 29 20 75 6e 73 69 67 6e 65   larger) unsigne
41392 64 20 69 6e 74 65 67 65 72 20 2a 2f 0d 0a 74 79  d integer */..ty
41393 70 65 64 65 66 20 73 68 6f 72 74 20 69 6e 74 20  pedef short int 
41394 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  i16;            
41395 2f 2a 20 32 2d 62 79 74 65 20 28 6f 72 20 6c 61  /* 2-byte (or la
41396 72 67 65 72 29 20 73 69 67 6e 65 64 20 69 6e 74  rger) signed int
41397 65 67 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65 66  eger */..typedef
41398 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 75 33   unsigned int u3
41399 32 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 34 2d  2;         /* 4-
4139a 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e  byte unsigned in
4139b 74 65 67 65 72 20 2a 2f 0d 0a 74 79 70 65 64 65  teger */..typede
4139c 66 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34  f sqlite3_uint64
4139d 20 75 36 34 3b 20 20 20 20 20 20 20 2f 2a 20 38   u64;       /* 8
4139e 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
4139f 6e 74 65 67 65 72 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  nteger */..../*.
413a0 0a 2a 2a 20 4d 61 63 72 6f 20 75 73 65 64 20 74  .** Macro used t
413a1 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69  o suppress compi
413a2 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 66 6f 72  ler warnings for
413a3 20 75 6e 75 73 65 64 20 70 61 72 61 6d 65 74 65   unused paramete
413a4 72 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65  rs...*/..#define
413a5 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
413a6 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0d 0a  R(x) (void)(x)..
413a7 0d 0a 2f 2a 0d 0a 2a 2a 20 41 63 74 69 76 61 74  ../*..** Activat
413a8 65 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20  e assert() only 
413a9 69 66 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69  if SQLITE_TEST i
413aa 73 20 65 6e 61 62 6c 65 64 2e 0d 0a 2a 2f 0d 0a  s enabled...*/..
413ab 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
413ac 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
413ad 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 0d  (SQLITE_DEBUG) .
413ae 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55 47  .# define NDEBUG
413af 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a   1..#endif..../*
413b0 0d 0a 2a 2a 20 54 68 65 20 54 45 53 54 4f 4e 4c  ..** The TESTONL
413b1 59 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  Y macro is used 
413b2 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61 72 69 61  to enclose varia
413b3 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ble declarations
413b4 20 6f 72 0d 0a 2a 2a 20 6f 74 68 65 72 20 62 69   or..** other bi
413b5 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  ts of code that 
413b6 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75  are needed to su
413b7 70 70 6f 72 74 20 74 68 65 20 61 72 67 75 6d 65  pport the argume
413b8 6e 74 73 0d 0a 2a 2a 20 77 69 74 68 69 6e 20 74  nts..** within t
413b9 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 61 73  estcase() and as
413ba 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0d 0a  sert() macros...
413bb 2a 2f 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
413bc 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
413bd 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
413be 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0d 0a  COVERAGE_TEST)..
413bf 23 20 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c  # define TESTONL
413c0 59 28 58 29 20 20 58 0d 0a 23 65 6c 73 65 0d 0a  Y(X)  X..#else..
413c1 23 20 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c  # define TESTONL
413c2 59 28 58 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  Y(X)..#endif....
413c3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
413c4 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f  _AMALGAMATION */
413c5 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ....#ifdef SQLIT
413c6 45 5f 44 45 42 55 47 0d 0a 53 51 4c 49 54 45 5f  E_DEBUG..SQLITE_
413c7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
413c8 74 65 33 46 74 73 33 43 6f 72 72 75 70 74 28 76  te3Fts3Corrupt(v
413c9 6f 69 64 29 3b 0d 0a 23 20 64 65 66 69 6e 65 20  oid);..# define 
413ca 46 54 53 5f 43 4f 52 52 55 50 54 5f 56 54 41 42  FTS_CORRUPT_VTAB
413cb 20 73 71 6c 69 74 65 33 46 74 73 33 43 6f 72 72   sqlite3Fts3Corr
413cc 75 70 74 28 29 0d 0a 23 65 6c 73 65 0d 0a 23 20  upt()..#else..# 
413cd 64 65 66 69 6e 65 20 46 54 53 5f 43 4f 52 52 55  define FTS_CORRU
413ce 50 54 5f 56 54 41 42 20 53 51 4c 49 54 45 5f 43  PT_VTAB SQLITE_C
413cf 4f 52 52 55 50 54 5f 56 54 41 42 0d 0a 23 65 6e  ORRUPT_VTAB..#en
413d0 64 69 66 0d 0a 0d 0a 74 79 70 65 64 65 66 20 73  dif....typedef s
413d1 74 72 75 63 74 20 46 74 73 33 54 61 62 6c 65 20  truct Fts3Table 
413d2 46 74 73 33 54 61 62 6c 65 3b 0d 0a 74 79 70 65  Fts3Table;..type
413d3 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33 43  def struct Fts3C
413d4 75 72 73 6f 72 20 46 74 73 33 43 75 72 73 6f 72  ursor Fts3Cursor
413d5 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
413d6 74 20 46 74 73 33 45 78 70 72 20 46 74 73 33 45  t Fts3Expr Fts3E
413d7 78 70 72 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  xpr;..typedef st
413d8 72 75 63 74 20 46 74 73 33 50 68 72 61 73 65 20  ruct Fts3Phrase 
413d9 46 74 73 33 50 68 72 61 73 65 3b 0d 0a 74 79 70  Fts3Phrase;..typ
413da 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33  edef struct Fts3
413db 50 68 72 61 73 65 54 6f 6b 65 6e 20 46 74 73 33  PhraseToken Fts3
413dc 50 68 72 61 73 65 54 6f 6b 65 6e 3b 0d 0a 0d 0a  PhraseToken;....
413dd 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
413de 74 73 33 44 6f 63 6c 69 73 74 20 46 74 73 33 44  ts3Doclist Fts3D
413df 6f 63 6c 69 73 74 3b 0d 0a 74 79 70 65 64 65 66  oclist;..typedef
413e0 20 73 74 72 75 63 74 20 46 74 73 33 53 65 67 46   struct Fts3SegF
413e1 69 6c 74 65 72 20 46 74 73 33 53 65 67 46 69 6c  ilter Fts3SegFil
413e2 74 65 72 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  ter;..typedef st
413e3 72 75 63 74 20 46 74 73 33 44 65 66 65 72 72 65  ruct Fts3Deferre
413e4 64 54 6f 6b 65 6e 20 46 74 73 33 44 65 66 65 72  dToken Fts3Defer
413e5 72 65 64 54 6f 6b 65 6e 3b 0d 0a 74 79 70 65 64  redToken;..typed
413e6 65 66 20 73 74 72 75 63 74 20 46 74 73 33 53 65  ef struct Fts3Se
413e7 67 52 65 61 64 65 72 20 46 74 73 33 53 65 67 52  gReader Fts3SegR
413e8 65 61 64 65 72 3b 0d 0a 74 79 70 65 64 65 66 20  eader;..typedef 
413e9 73 74 72 75 63 74 20 46 74 73 33 4d 75 6c 74 69  struct Fts3Multi
413ea 53 65 67 52 65 61 64 65 72 20 46 74 73 33 4d 75  SegReader Fts3Mu
413eb 6c 74 69 53 65 67 52 65 61 64 65 72 3b 0d 0a 0d  ltiSegReader;...
413ec 0a 2f 2a 0d 0a 2a 2a 20 41 20 63 6f 6e 6e 65 63  ./*..** A connec
413ed 74 69 6f 6e 20 74 6f 20 61 20 66 75 6c 6c 74 65  tion to a fullte
413ee 78 74 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69  xt index is an i
413ef 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
413f0 6f 6c 6c 6f 77 69 6e 67 0d 0a 2a 2a 20 73 74 72  ollowing..** str
413f1 75 63 74 75 72 65 2e 20 54 68 65 20 78 43 72 65  ucture. The xCre
413f2 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  ate and xConnect
413f3 20 6d 65 74 68 6f 64 73 20 63 72 65 61 74 65 20   methods create 
413f4 61 6e 20 69 6e 73 74 61 6e 63 65 0d 0a 2a 2a 20  an instance..** 
413f5 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
413f6 65 20 61 6e 64 20 78 44 65 73 74 72 6f 79 20 61  e and xDestroy a
413f7 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 66  nd xDisconnect f
413f8 72 65 65 20 74 68 61 74 20 69 6e 73 74 61 6e 63  ree that instanc
413f9 65 2e 0d 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  e...** All other
413fa 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69 76 65   methods receive
413fb 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
413fc 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20 6f  e structure as o
413fd 6e 65 20 6f 66 20 74 68 65 69 72 0d 0a 2a 2a 20  ne of their..** 
413fe 61 72 67 75 6d 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a  arguments...*/..
413ff 73 74 72 75 63 74 20 46 74 73 33 54 61 62 6c 65  struct Fts3Table
41400 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   {..  sqlite3_vt
41401 61 62 20 62 61 73 65 3b 20 20 20 20 20 20 20 20  ab base;        
41402 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c        /* Base cl
41403 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c 69  ass used by SQLi
41404 74 65 20 63 6f 72 65 20 2a 2f 0d 0a 20 20 73 71  te core */..  sq
41405 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
41406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41407 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
41408 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 63  nnection */..  c
41409 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
4140a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4140b 2a 20 6c 6f 67 69 63 61 6c 20 64 61 74 61 62 61  * logical databa
4140c 73 65 20 6e 61 6d 65 20 2a 2f 0d 0a 20 20 63 6f  se name */..  co
4140d 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
4140e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4140f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e   virtual table n
41410 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43  ame */..  int nC
41411 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
41412 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d            /* num
41413 62 65 72 20 6f 66 20 6e 61 6d 65 64 20 63 6f 6c  ber of named col
41414 75 6d 6e 73 20 69 6e 20 76 69 72 74 75 61 6c 20  umns in virtual 
41415 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 63 68 61 72  table */..  char
41416 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20   **azColumn;    
41417 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
41418 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61  olumn names.  ma
41419 6c 6c 6f 63 65 64 20 2a 2f 0d 0a 20 20 73 71 6c  lloced */..  sql
4141a 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
4141b 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 2f 2a 20  pTokenizer;  /* 
4141c 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69 6e  tokenizer for in
4141d 73 65 72 74 73 20 61 6e 64 20 71 75 65 72 69 65  serts and querie
4141e 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 43  s */..  char *zC
4141f 6f 6e 74 65 6e 74 54 62 6c 3b 20 20 20 20 20 20  ontentTbl;      
41420 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65          /* conte
41421 6e 74 3d 78 78 78 20 6f 70 74 69 6f 6e 2c 20 6f  nt=xxx option, o
41422 72 20 4e 55 4c 4c 20 2a 2f 0d 0a 0d 0a 20 20 2f  r NULL */....  /
41423 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64 20 73 74  * Precompiled st
41424 61 74 65 6d 65 6e 74 73 20 75 73 65 64 20 62 79  atements used by
41425 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
41426 69 6f 6e 2e 20 45 61 63 68 20 6f 66 20 74 68 65  ion. Each of the
41427 73 65 20 0d 0a 20 20 2a 2a 20 73 74 61 74 65 6d  se ..  ** statem
41428 65 6e 74 73 20 69 73 20 72 75 6e 20 61 6e 64 20  ents is run and 
41429 72 65 73 65 74 20 77 69 74 68 69 6e 20 61 20 73  reset within a s
4142a 69 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 74 61  ingle virtual ta
4142b 62 6c 65 20 41 50 49 20 63 61 6c 6c 2e 20 0d 0a  ble API call. ..
4142c 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f    */..  sqlite3_
4142d 73 74 6d 74 20 2a 61 53 74 6d 74 5b 32 37 5d 3b  stmt *aStmt[27];
4142e 0d 0a 0d 0a 20 20 63 68 61 72 20 2a 7a 52 65 61  ....  char *zRea
4142f 64 45 78 70 72 6c 69 73 74 3b 0d 0a 20 20 63 68  dExprlist;..  ch
41430 61 72 20 2a 7a 57 72 69 74 65 45 78 70 72 6c 69  ar *zWriteExprli
41431 73 74 3b 0d 0a 0d 0a 20 20 69 6e 74 20 6e 4e 6f  st;....  int nNo
41432 64 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  deSize;         
41433 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 66 74           /* Soft
41434 20 6c 69 6d 69 74 20 66 6f 72 20 6e 6f 64 65 20   limit for node 
41435 73 69 7a 65 20 2a 2f 0d 0a 20 20 75 38 20 62 48  size */..  u8 bH
41436 61 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 20  asStat;         
41437 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
41438 75 65 20 69 66 20 25 5f 73 74 61 74 20 74 61 62  ue if %_stat tab
41439 6c 65 20 65 78 69 73 74 73 20 2a 2f 0d 0a 20 20  le exists */..  
4143a 75 38 20 62 48 61 73 44 6f 63 73 69 7a 65 3b 20  u8 bHasDocsize; 
4143b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4143c 2f 2a 20 54 72 75 65 20 69 66 20 25 5f 64 6f 63  /* True if %_doc
4143d 73 69 7a 65 20 74 61 62 6c 65 20 65 78 69 73 74  size table exist
4143e 73 20 2a 2f 0d 0a 20 20 75 38 20 62 44 65 73 63  s */..  u8 bDesc
4143f 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
41440 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
41441 69 66 20 64 6f 63 6c 69 73 74 73 20 61 72 65 20  if doclists are 
41442 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
41443 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50 67 73 7a   */..  int nPgsz
41444 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
41445 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73         /* Page s
41446 69 7a 65 20 66 6f 72 20 68 6f 73 74 20 64 61 74  ize for host dat
41447 61 62 61 73 65 20 2a 2f 0d 0a 20 20 63 68 61 72  abase */..  char
41448 20 2a 7a 53 65 67 6d 65 6e 74 73 54 62 6c 3b 20   *zSegmentsTbl; 
41449 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
4144a 61 6d 65 20 6f 66 20 25 5f 73 65 67 6d 65 6e 74  ame of %_segment
4144b 73 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 73 71  s table */..  sq
4144c 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 53 65 67  lite3_blob *pSeg
4144d 6d 65 6e 74 73 3b 20 20 20 20 20 20 20 20 2f 2a  ments;        /*
4144e 20 42 6c 6f 62 20 68 61 6e 64 6c 65 20 6f 70 65   Blob handle ope
4144f 6e 20 6f 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20  n on %_segments 
41450 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a  table */....  /*
41451 20 54 4f 44 4f 3a 20 46 69 78 20 74 68 65 20 66   TODO: Fix the f
41452 69 72 73 74 20 70 61 72 61 67 72 61 70 68 20 6f  irst paragraph o
41453 66 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0d  f this comment..
41454 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68 65 20  .  **..  ** The 
41455 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 73 68 20 74  following hash t
41456 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
41457 62 75 66 66 65 72 20 70 65 6e 64 69 6e 67 20 69  buffer pending i
41458 6e 64 65 78 20 75 70 64 61 74 65 73 20 64 75 72  ndex updates dur
41459 69 6e 67 0d 0a 20 20 2a 2a 20 74 72 61 6e 73 61  ing..  ** transa
4145a 63 74 69 6f 6e 73 2e 20 56 61 72 69 61 62 6c 65  ctions. Variable
4145b 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73   nPendingData es
4145c 74 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f  timates the memo
4145d 72 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0d  ry size of the .
4145e 0a 20 20 2a 2a 20 70 65 6e 64 69 6e 67 20 64 61  .  ** pending da
4145f 74 61 2c 20 69 6e 63 6c 75 64 69 6e 67 20 68 61  ta, including ha
41460 73 68 20 74 61 62 6c 65 20 6f 76 65 72 68 65 61  sh table overhea
41461 64 2c 20 62 75 74 20 6e 6f 74 20 6d 61 6c 6c 6f  d, but not mallo
41462 63 20 6f 76 65 72 68 65 61 64 2e 20 0d 0a 20 20  c overhead. ..  
41463 2a 2a 20 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67  ** When nPending
41464 44 61 74 61 20 65 78 63 65 65 64 73 20 6e 4d 61  Data exceeds nMa
41465 78 50 65 6e 64 69 6e 67 44 61 74 61 2c 20 74 68  xPendingData, th
41466 65 20 62 75 66 66 65 72 20 69 73 20 66 6c 75 73  e buffer is flus
41467 68 65 64 20 0d 0a 20 20 2a 2a 20 61 75 74 6f 6d  hed ..  ** autom
41468 61 74 69 63 61 6c 6c 79 2e 20 56 61 72 69 61 62  atically. Variab
41469 6c 65 20 69 50 72 65 76 44 6f 63 69 64 20 69 73  le iPrevDocid is
4146a 20 74 68 65 20 64 6f 63 69 64 20 6f 66 20 74 68   the docid of th
4146b 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0d  e most recently.
4146c 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 64 20 72  .  ** inserted r
4146d 65 63 6f 72 64 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  ecord...  **..  
4146e 2a 2a 20 41 20 73 69 6e 67 6c 65 20 46 54 53 34  ** A single FTS4
4146f 20 74 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20   table may have 
41470 6d 75 6c 74 69 70 6c 65 20 66 75 6c 6c 2d 74 65  multiple full-te
41471 78 74 20 69 6e 64 65 78 65 73 2e 20 46 6f 72 20  xt indexes. For 
41472 65 61 63 68 20 69 6e 64 65 78 0d 0a 20 20 2a 2a  each index..  **
41473 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74   there is an ent
41474 72 79 20 69 6e 20 74 68 65 20 61 49 6e 64 65 78  ry in the aIndex
41475 5b 5d 20 61 72 72 61 79 2e 20 49 6e 64 65 78 20  [] array. Index 
41476 30 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 66  0 is an index of
41477 20 61 6c 6c 20 74 68 65 0d 0a 20 20 2a 2a 20 74   all the..  ** t
41478 65 72 6d 73 20 74 68 61 74 20 61 70 70 65 61 72  erms that appear
41479 20 69 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   in the document
4147a 20 73 65 74 2e 20 45 61 63 68 20 73 75 62 73 65   set. Each subse
4147b 71 75 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61  quent index in a
4147c 49 6e 64 65 78 5b 5d 0d 0a 20 20 2a 2a 20 69 73  Index[]..  ** is
4147d 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 70 72 65   an index of pre
4147e 66 69 78 65 73 20 6f 66 20 61 20 73 70 65 63 69  fixes of a speci
4147f 66 69 63 20 6c 65 6e 67 74 68 2e 0d 0a 20 20 2a  fic length...  *
41480 2f 0d 0a 20 20 69 6e 74 20 6e 49 6e 64 65 78 3b  /..  int nIndex;
41481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41482 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
41483 61 49 6e 64 65 78 5b 5d 20 2a 2f 0d 0a 20 20 73  aIndex[] */..  s
41484 74 72 75 63 74 20 46 74 73 33 49 6e 64 65 78 20  truct Fts3Index 
41485 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 50 72 65 66  {..    int nPref
41486 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ix;             
41487 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 6c       /* Prefix l
41488 65 6e 67 74 68 20 28 30 20 66 6f 72 20 6d 61 69  ength (0 for mai
41489 6e 20 74 65 72 6d 73 20 69 6e 64 65 78 29 20 2a  n terms index) *
4148a 2f 0d 0a 20 20 20 20 46 74 73 33 48 61 73 68 20  /..    Fts3Hash 
4148b 68 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20  hPending;       
4148c 20 20 20 20 20 2f 2a 20 50 65 6e 64 69 6e 67 20       /* Pending 
4148d 74 65 72 6d 73 20 74 61 62 6c 65 20 66 6f 72 20  terms table for 
4148e 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0d 0a 20  this index */.. 
4148f 20 7d 20 2a 61 49 6e 64 65 78 3b 0d 0a 20 20 69   } *aIndex;..  i
41490 6e 74 20 6e 4d 61 78 50 65 6e 64 69 6e 67 44 61  nt nMaxPendingDa
41491 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ta;            /
41492 2a 20 4d 61 78 20 70 65 6e 64 69 6e 67 20 64 61  * Max pending da
41493 74 61 20 62 65 66 6f 72 65 20 66 6c 75 73 68 20  ta before flush 
41494 74 6f 20 64 69 73 6b 20 2a 2f 0d 0a 20 20 69 6e  to disk */..  in
41495 74 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 20  t nPendingData; 
41496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41497 20 43 75 72 72 65 6e 74 20 62 79 74 65 73 20 6f   Current bytes o
41498 66 20 70 65 6e 64 69 6e 67 20 64 61 74 61 20 2a  f pending data *
41499 2f 0d 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  /..  sqlite_int6
4149a 34 20 69 50 72 65 76 44 6f 63 69 64 3b 20 20 20  4 iPrevDocid;   
4149b 20 20 20 20 20 2f 2a 20 44 6f 63 69 64 20 6f 66       /* Docid of
4149c 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69   most recently i
4149d 6e 73 65 72 74 65 64 20 64 6f 63 75 6d 65 6e 74  nserted document
4149e 20 2a 2f 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e   */....#if defin
4149f 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
414a0 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
414a1 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54  TE_COVERAGE_TEST
414a2 29 0d 0a 20 20 2f 2a 20 53 74 61 74 65 20 76 61  )..  /* State va
414a3 72 69 61 62 6c 65 73 20 75 73 65 64 20 66 6f 72  riables used for
414a4 20 76 61 6c 69 64 61 74 69 6e 67 20 74 68 61 74   validating that
414a5 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
414a6 20 63 6f 6e 74 72 6f 6c 0d 0a 20 20 2a 2a 20 6d   control..  ** m
414a7 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 76 69  ethods of the vi
414a8 72 74 75 61 6c 20 74 61 62 6c 65 20 61 72 65 20  rtual table are 
414a9 63 61 6c 6c 65 64 20 61 74 20 61 70 70 72 6f 70  called at approp
414aa 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20 54 68  riate times.  Th
414ab 65 73 65 0d 0a 20 20 2a 2a 20 76 61 6c 75 65 73  ese..  ** values
414ac 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75   do not contribu
414ad 74 69 6f 6e 20 74 6f 20 74 68 65 20 46 54 53 20  tion to the FTS 
414ae 63 6f 6d 70 75 74 61 74 69 6f 6e 3b 20 74 68 65  computation; the
414af 79 20 61 72 65 20 75 73 65 64 20 66 6f 72 0d 0a  y are used for..
414b0 20 20 2a 2a 20 76 65 72 69 66 79 69 6e 67 20 74    ** verifying t
414b1 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 2e 0d  he SQLite core..
414b2 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 6e 54  .  */..  int inT
414b3 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
414b4 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 78 42  /* True after xB
414b5 65 67 69 6e 20 62 75 74 20 62 65 66 6f 72 65 20  egin but before 
414b6 78 43 6f 6d 6d 69 74 2f 78 52 6f 6c 6c 62 61 63  xCommit/xRollbac
414b7 6b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6d 78 53 61  k */..  int mxSa
414b8 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 2f  vepoint;       /
414b9 2a 20 4c 61 72 67 65 73 74 20 76 61 6c 69 64 20  * Largest valid 
414ba 78 53 61 76 65 70 6f 69 6e 74 20 69 6e 74 65 67  xSavepoint integ
414bb 65 72 20 2a 2f 0d 0a 23 65 6e 64 69 66 0d 0a 7d  er */..#endif..}
414bc 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 68 65 6e  ;..../*..** When
414bd 20 74 68 65 20 63 6f 72 65 20 77 61 6e 74 73 20   the core wants 
414be 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  to read from the
414bf 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20   virtual table, 
414c0 69 74 20 63 72 65 61 74 65 73 20 61 0d 0a 2a 2a  it creates a..**
414c1 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
414c2 75 72 73 6f 72 20 28 61 6e 20 69 6e 73 74 61 6e  ursor (an instan
414c3 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
414c4 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20 75  ing structure) u
414c5 73 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 78 4f 70  sing..** the xOp
414c6 65 6e 20 6d 65 74 68 6f 64 2e 20 43 75 72 73 6f  en method. Curso
414c7 72 73 20 61 72 65 20 64 65 73 74 72 6f 79 65 64  rs are destroyed
414c8 20 75 73 69 6e 67 20 74 68 65 20 78 43 6c 6f 73   using the xClos
414c9 65 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73  e method...*/..s
414ca 74 72 75 63 74 20 46 74 73 33 43 75 72 73 6f 72  truct Fts3Cursor
414cb 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   {..  sqlite3_vt
414cc 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 20  ab_cursor base; 
414cd 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c        /* Base cl
414ce 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c 69  ass used by SQLi
414cf 74 65 20 63 6f 72 65 20 2a 2f 0d 0a 20 20 69 31  te core */..  i1
414d0 36 20 65 53 65 61 72 63 68 3b 20 20 20 20 20 20  6 eSearch;      
414d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
414d2 20 53 65 61 72 63 68 20 73 74 72 61 74 65 67 79   Search strategy
414d3 20 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0d   (see below) */.
414d4 0a 20 20 75 38 20 69 73 45 6f 66 3b 20 20 20 20  .  u8 isEof;    
414d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
414d6 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74     /* True if at
414d7 20 45 6e 64 20 4f 66 20 52 65 73 75 6c 74 73 20   End Of Results 
414d8 2a 2f 0d 0a 20 20 75 38 20 69 73 52 65 71 75 69  */..  u8 isRequi
414d9 72 65 53 65 65 6b 3b 20 20 20 20 20 20 20 20 20  reSeek;         
414da 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
414db 20 6d 75 73 74 20 73 65 65 6b 20 70 53 74 6d 74   must seek pStmt
414dc 20 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 72 6f   to %_content ro
414dd 77 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  w */..  sqlite3_
414de 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
414df 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61          /* Prepa
414e0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  red statement in
414e1 20 75 73 65 20 62 79 20 74 68 65 20 63 75 72 73   use by the curs
414e2 6f 72 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70  or */..  Fts3Exp
414e3 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
414e4 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
414e5 65 64 20 4d 41 54 43 48 20 71 75 65 72 79 20 73  ed MATCH query s
414e6 74 72 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74 20  tring */..  int 
414e7 6e 50 68 72 61 73 65 3b 20 20 20 20 20 20 20 20  nPhrase;        
414e8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
414e9 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 61 62  umber of matchab
414ea 6c 65 20 70 68 72 61 73 65 73 20 69 6e 20 71 75  le phrases in qu
414eb 65 72 79 20 2a 2f 0d 0a 20 20 46 74 73 33 44 65  ery */..  Fts3De
414ec 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 44 65  ferredToken *pDe
414ed 66 65 72 72 65 64 3b 20 20 20 2f 2a 20 44 65 66  ferred;   /* Def
414ee 65 72 72 65 64 20 73 65 61 72 63 68 20 74 6f 6b  erred search tok
414ef 65 6e 73 2c 20 69 66 20 61 6e 79 20 2a 2f 0d 0a  ens, if any */..
414f0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
414f1 69 50 72 65 76 49 64 3b 20 20 20 20 20 20 20 20  iPrevId;        
414f2 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 64    /* Previous id
414f3 20 72 65 61 64 20 66 72 6f 6d 20 61 44 6f 63 6c   read from aDocl
414f4 69 73 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  ist */..  char *
414f5 70 4e 65 78 74 49 64 3b 20 20 20 20 20 20 20 20  pNextId;        
414f6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
414f7 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f  nter into the bo
414f8 64 79 20 6f 66 20 61 44 6f 63 6c 69 73 74 20 2a  dy of aDoclist *
414f9 2f 0d 0a 20 20 63 68 61 72 20 2a 61 44 6f 63 6c  /..  char *aDocl
414fa 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
414fb 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
414fc 64 6f 63 69 64 73 20 66 6f 72 20 66 75 6c 6c 2d  docids for full-
414fd 74 65 78 74 20 71 75 65 72 69 65 73 20 2a 2f 0d  text queries */.
414fe 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b  .  int nDoclist;
414ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41500 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
41501 66 66 65 72 20 61 74 20 61 44 6f 63 6c 69 73 74  ffer at aDoclist
41502 20 2a 2f 0d 0a 20 20 75 38 20 62 44 65 73 63 3b   */..  u8 bDesc;
41503 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41504 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
41505 6f 20 73 6f 72 74 20 69 6e 20 64 65 73 63 65 6e  o sort in descen
41506 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0d 0a 20  ding order */.. 
41507 20 69 6e 74 20 65 45 76 61 6c 6d 6f 64 65 3b 20   int eEvalmode; 
41508 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41509 20 2f 2a 20 41 6e 20 46 54 53 33 5f 45 56 41 4c   /* An FTS3_EVAL
4150a 5f 58 58 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0d  _XX constant */.
4150b 0a 20 20 69 6e 74 20 6e 52 6f 77 41 76 67 3b 20  .  int nRowAvg; 
4150c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4150d 20 20 20 2f 2a 20 41 76 65 72 61 67 65 20 73 69     /* Average si
4150e 7a 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 72  ze of database r
4150f 6f 77 73 2c 20 69 6e 20 70 61 67 65 73 20 2a 2f  ows, in pages */
41510 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
41511 34 20 6e 44 6f 63 3b 20 20 20 20 20 20 20 20 20  4 nDoc;         
41512 20 20 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 73      /* Documents
41513 20 69 6e 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a   in table */....
41514 20 20 69 6e 74 20 69 73 4d 61 74 63 68 69 6e 66    int isMatchinf
41515 6f 4e 65 65 64 65 64 3b 20 20 20 20 20 20 20 20  oNeeded;        
41516 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e 20 61    /* True when a
41517 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 6e 65 65 64  Matchinfo[] need
41518 73 20 66 69 6c 6c 69 6e 67 20 69 6e 20 2a 2f 0d  s filling in */.
41519 0a 20 20 75 33 32 20 2a 61 4d 61 74 63 68 69 6e  .  u32 *aMatchin
4151a 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fo;             
4151b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
4151c 6e 20 61 62 6f 75 74 20 6d 6f 73 74 20 72 65 63  n about most rec
4151d 65 6e 74 20 6d 61 74 63 68 20 2a 2f 0d 0a 20 20  ent match */..  
4151e 69 6e 74 20 6e 4d 61 74 63 68 69 6e 66 6f 3b 20  int nMatchinfo; 
4151f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41520 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
41521 6d 65 6e 74 73 20 69 6e 20 61 4d 61 74 63 68 69  ments in aMatchi
41522 6e 66 6f 5b 5d 20 2a 2f 0d 0a 20 20 63 68 61 72  nfo[] */..  char
41523 20 2a 7a 4d 61 74 63 68 69 6e 66 6f 3b 20 20 20   *zMatchinfo;   
41524 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
41525 61 74 63 68 69 6e 66 6f 20 73 70 65 63 69 66 69  atchinfo specifi
41526 63 61 74 69 6f 6e 20 2a 2f 0d 0a 7d 3b 0d 0a 0d  cation */..};...
41527 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 45 56  .#define FTS3_EV
41528 41 4c 5f 46 49 4c 54 45 52 20 20 20 20 30 0d 0a  AL_FILTER    0..
41529 23 64 65 66 69 6e 65 20 46 54 53 33 5f 45 56 41  #define FTS3_EVA
4152a 4c 5f 4e 45 58 54 20 20 20 20 20 20 31 0d 0a 23  L_NEXT      1..#
4152b 64 65 66 69 6e 65 20 46 54 53 33 5f 45 56 41 4c  define FTS3_EVAL
4152c 5f 4d 41 54 43 48 49 4e 46 4f 20 32 0d 0a 0d 0a  _MATCHINFO 2....
4152d 2f 2a 0d 0a 2a 2a 20 54 68 65 20 46 74 73 33 43  /*..** The Fts3C
4152e 75 72 73 6f 72 2e 65 53 65 61 72 63 68 20 6d 65  ursor.eSearch me
4152f 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 73  mber is always s
41530 65 74 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65  et to one of the
41531 20 66 6f 6c 6c 6f 77 69 6e 67 2e 0d 0a 2a 2a 20   following...** 
41532 41 63 74 75 61 6c 79 2c 20 46 74 73 33 43 75 72  Actualy, Fts3Cur
41533 73 6f 72 2e 65 53 65 61 72 63 68 20 63 61 6e 20  sor.eSearch can 
41534 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
41535 6f 72 20 65 71 75 61 6c 20 74 6f 0d 0a 2a 2a 20  or equal to..** 
41536 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
41537 41 52 43 48 2e 20 20 49 66 20 73 6f 2c 20 74 68  ARCH.  If so, th
41538 65 6e 20 46 74 73 33 43 75 72 73 6f 72 2e 65 53  en Fts3Cursor.eS
41539 65 61 72 63 68 20 2d 20 32 20 69 73 20 74 68 65  earch - 2 is the
4153a 20 69 6e 64 65 78 0d 0a 2a 2a 20 6f 66 20 74 68   index..** of th
4153b 65 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 73  e column to be s
4153c 65 61 72 63 68 65 64 2e 20 20 46 6f 72 20 65 78  earched.  For ex
4153d 61 6d 70 6c 65 2c 20 69 6e 0d 0a 2a 2a 0d 0a 2a  ample, in..**..*
4153e 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 52  *     CREATE VIR
4153f 54 55 41 4c 20 54 41 42 4c 45 20 65 78 31 20 55  TUAL TABLE ex1 U
41540 53 49 4e 47 20 66 74 73 33 28 61 2c 62 2c 63 2c  SING fts3(a,b,c,
41541 64 29 3b 0d 0a 2a 2a 20 20 20 20 20 53 45 4c 45  d);..**     SELE
41542 43 54 20 64 6f 63 69 64 20 46 52 4f 4d 20 65 78  CT docid FROM ex
41543 31 20 57 48 45 52 45 20 62 20 4d 41 54 43 48 20  1 WHERE b MATCH 
41544 27 6f 6e 65 20 74 77 6f 20 74 68 72 65 65 27 3b  'one two three';
41545 0d 0a 2a 2a 20 0d 0a 2a 2a 20 42 65 63 61 75 73  ..** ..** Becaus
41546 65 20 74 68 65 20 4c 48 53 20 6f 66 20 74 68 65  e the LHS of the
41547 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
41548 69 73 20 32 6e 64 20 63 6f 6c 75 6d 6e 20 22 62  is 2nd column "b
41549 22 2c 0d 0a 2a 2a 20 46 74 73 33 43 75 72 73 6f  ",..** Fts3Curso
4154a 72 2e 65 53 65 61 72 63 68 20 77 69 6c 6c 20 62  r.eSearch will b
4154b 65 20 73 65 74 20 74 6f 20 46 54 53 33 5f 46 55  e set to FTS3_FU
4154c 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 2b 31 2e  LLTEXT_SEARCH+1.
4154d 20 20 28 2b 30 20 66 6f 72 20 61 2c 0d 0a 2a 2a    (+0 for a,..**
4154e 20 2b 31 20 66 6f 72 20 62 2c 20 2b 32 20 66 6f   +1 for b, +2 fo
4154f 72 20 63 2c 20 2b 33 20 66 6f 72 20 64 2e 29 20  r c, +3 for d.) 
41550 20 49 66 20 74 68 65 20 4c 48 53 20 6f 66 20 4d   If the LHS of M
41551 41 54 43 48 20 77 65 72 65 20 22 65 78 31 22 20  ATCH were "ex1" 
41552 0d 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  ..** indicating 
41553 74 68 61 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  that all columns
41554 20 73 68 6f 75 6c 64 20 62 65 20 73 65 61 72 63   should be searc
41555 68 65 64 2c 0d 0a 2a 2a 20 74 68 65 6e 20 65 53  hed,..** then eS
41556 65 61 72 63 68 20 77 6f 75 6c 64 20 62 65 20 73  earch would be s
41557 65 74 20 74 6f 20 46 54 53 33 5f 46 55 4c 4c 54  et to FTS3_FULLT
41558 45 58 54 5f 53 45 41 52 43 48 2b 34 2e 0d 0a 2a  EXT_SEARCH+4...*
41559 2f 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  /..#define FTS3_
4155a 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 20  FULLSCAN_SEARCH 
4155b 30 20 20 20 20 2f 2a 20 4c 69 6e 65 61 72 20 73  0    /* Linear s
4155c 63 61 6e 20 6f 66 20 25 5f 63 6f 6e 74 65 6e 74  can of %_content
4155d 20 74 61 62 6c 65 20 2a 2f 0d 0a 23 64 65 66 69   table */..#defi
4155e 6e 65 20 46 54 53 33 5f 44 4f 43 49 44 5f 53 45  ne FTS3_DOCID_SE
4155f 41 52 43 48 20 20 20 20 31 20 20 20 20 2f 2a 20  ARCH    1    /* 
41560 4c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 20  Lookup by rowid 
41561 6f 6e 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  on %_content tab
41562 6c 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46  le */..#define F
41563 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41  TS3_FULLTEXT_SEA
41564 52 43 48 20 32 20 20 20 20 2f 2a 20 46 75 6c 6c  RCH 2    /* Full
41565 2d 74 65 78 74 20 69 6e 64 65 78 20 73 65 61 72  -text index sear
41566 63 68 20 2a 2f 0d 0a 0d 0a 0d 0a 73 74 72 75 63  ch */......struc
41567 74 20 46 74 73 33 44 6f 63 6c 69 73 74 20 7b 0d  t Fts3Doclist {.
41568 0a 20 20 63 68 61 72 20 2a 61 41 6c 6c 3b 20 20  .  char *aAll;  
41569 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4156a 20 20 2f 2a 20 41 72 72 61 79 20 63 6f 6e 74 61    /* Array conta
4156b 69 6e 69 6e 67 20 64 6f 63 6c 69 73 74 20 28 6f  ining doclist (o
4156c 72 20 4e 55 4c 4c 29 20 2a 2f 0d 0a 20 20 69 6e  r NULL) */..  in
4156d 74 20 6e 41 6c 6c 3b 20 20 20 20 20 20 20 20 20  t nAll;         
4156e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4156f 53 69 7a 65 20 6f 66 20 61 5b 5d 20 69 6e 20 62  Size of a[] in b
41570 79 74 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20  ytes */..  char 
41571 2a 70 4e 65 78 74 44 6f 63 69 64 3b 20 20 20 20  *pNextDocid;    
41572 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
41573 6e 74 65 72 20 74 6f 20 6e 65 78 74 20 64 6f 63  nter to next doc
41574 69 64 20 2a 2f 0d 0a 0d 0a 20 20 73 71 6c 69 74  id */....  sqlit
41575 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b  e3_int64 iDocid;
41576 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
41577 72 65 6e 74 20 64 6f 63 69 64 20 28 69 66 20 70  rent docid (if p
41578 4c 69 73 74 21 3d 30 29 20 2a 2f 0d 0a 20 20 69  List!=0) */..  i
41579 6e 74 20 62 46 72 65 65 4c 69 73 74 3b 20 20 20  nt bFreeList;   
4157a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4157b 20 54 72 75 65 20 69 66 20 70 4c 69 73 74 20 73   True if pList s
4157c 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33  hould be sqlite3
4157d 5f 66 72 65 65 28 29 64 20 2a 2f 0d 0a 20 20 63  _free()d */..  c
4157e 68 61 72 20 2a 70 4c 69 73 74 3b 20 20 20 20 20  har *pList;     
4157f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41580 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 6f 73 69   Pointer to posi
41581 74 69 6f 6e 20 6c 69 73 74 20 66 6f 6c 6c 6f 77  tion list follow
41582 69 6e 67 20 69 44 6f 63 69 64 20 2a 2f 0d 0a 20  ing iDocid */.. 
41583 20 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20 20 20   int nList;     
41584 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41585 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 70 6f 73  /* Length of pos
41586 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0d 0a 7d  ition list */..}
41587 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 22 70  ;..../*..** A "p
41588 68 72 61 73 65 22 20 69 73 20 61 20 73 65 71 75  hrase" is a sequ
41589 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d  ence of one or m
4158a 6f 72 65 20 74 6f 6b 65 6e 73 20 74 68 61 74 20  ore tokens that 
4158b 6d 75 73 74 20 6d 61 74 63 68 20 69 6e 0d 0a 2a  must match in..*
4158c 2a 20 73 65 71 75 65 6e 63 65 2e 20 20 41 20 73  * sequence.  A s
4158d 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 69 73 20 74  ingle token is t
4158e 68 65 20 62 61 73 65 20 63 61 73 65 20 61 6e 64  he base case and
4158f 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e   the most common
41590 20 63 61 73 65 2e 0d 0a 2a 2a 20 46 6f 72 20 61   case...** For a
41591 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b   sequence of tok
41592 65 6e 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  ens contained in
41593 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 20 28   double-quotes (
41594 69 2e 65 2e 20 22 6f 6e 65 20 74 77 6f 20 74 68  i.e. "one two th
41595 72 65 65 22 29 0d 0a 2a 2a 20 6e 54 6f 6b 65 6e  ree")..** nToken
41596 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d   will be the num
41597 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e  ber of tokens in
41598 20 74 68 65 20 73 74 72 69 6e 67 2e 0d 0a 2a 2f   the string...*/
41599 0d 0a 73 74 72 75 63 74 20 46 74 73 33 50 68 72  ..struct Fts3Phr
4159a 61 73 65 54 6f 6b 65 6e 20 7b 0d 0a 20 20 63 68  aseToken {..  ch
4159b 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
4159c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4159d 20 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b   Text of the tok
4159e 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 3b 20  en */..  int n; 
4159f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
415a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
415a1 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62  er of bytes in b
415a2 75 66 66 65 72 20 7a 20 2a 2f 0d 0a 20 20 69 6e  uffer z */..  in
415a3 74 20 69 73 50 72 65 66 69 78 3b 20 20 20 20 20  t isPrefix;     
415a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
415a5 20 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 65   True if token e
415a6 6e 64 73 20 77 69 74 68 20 61 20 22 2a 22 20 63  nds with a "*" c
415a7 68 61 72 61 63 74 65 72 20 2a 2f 0d 0a 20 20 69  haracter */..  i
415a8 6e 74 20 62 46 69 72 73 74 3b 20 20 20 20 20 20  nt bFirst;      
415a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
415aa 2a 20 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20  * True if token 
415ab 6d 75 73 74 20 61 70 70 65 61 72 20 61 74 20 70  must appear at p
415ac 6f 73 69 74 69 6f 6e 20 30 20 2a 2f 0d 0a 0d 0a  osition 0 */....
415ad 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 61    /* Variables a
415ae 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74 20  bove this point 
415af 61 72 65 20 70 6f 70 75 6c 61 74 65 64 20 77 68  are populated wh
415b0 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
415b1 6e 20 69 73 0d 0a 20 20 2a 2a 20 70 61 72 73 65  n is..  ** parse
415b2 64 20 28 62 79 20 63 6f 64 65 20 69 6e 20 66 74  d (by code in ft
415b3 73 33 5f 65 78 70 72 2e 63 29 2e 20 42 65 6c 6f  s3_expr.c). Belo
415b4 77 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  w this point the
415b5 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 0d 0a   variables are..
415b6 20 20 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 65    ** used when e
415b7 76 61 6c 75 61 74 69 6e 67 20 74 68 65 20 65 78  valuating the ex
415b8 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0d 0a 20 20  pression. */..  
415b9 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65  Fts3DeferredToke
415ba 6e 20 2a 70 44 65 66 65 72 72 65 64 3b 20 20 20  n *pDeferred;   
415bb 2f 2a 20 44 65 66 65 72 72 65 64 20 74 6f 6b 65  /* Deferred toke
415bc 6e 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 69  n object for thi
415bd 73 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 46 74  s token */..  Ft
415be 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
415bf 20 2a 70 53 65 67 63 73 72 3b 20 20 20 20 2f 2a   *pSegcsr;    /*
415c0 20 53 65 67 6d 65 6e 74 2d 72 65 61 64 65 72 20   Segment-reader 
415c1 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e 20 2a  for this token *
415c2 2f 0d 0a 7d 3b 0d 0a 0d 0a 73 74 72 75 63 74 20  /..};....struct 
415c3 46 74 73 33 50 68 72 61 73 65 20 7b 0d 0a 20 20  Fts3Phrase {..  
415c4 2f 2a 20 43 61 63 68 65 20 6f 66 20 64 6f 63 6c  /* Cache of docl
415c5 69 73 74 20 66 6f 72 20 74 68 69 73 20 70 68 72  ist for this phr
415c6 61 73 65 2e 20 2a 2f 0d 0a 20 20 46 74 73 33 44  ase. */..  Fts3D
415c7 6f 63 6c 69 73 74 20 64 6f 63 6c 69 73 74 3b 0d  oclist doclist;.
415c8 0a 20 20 69 6e 74 20 62 49 6e 63 72 3b 20 20 20  .  int bIncr;   
415c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
415ca 20 54 72 75 65 20 69 66 20 64 6f 63 6c 69 73 74   True if doclist
415cb 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 63 72 65   is loaded incre
415cc 6d 65 6e 74 61 6c 6c 79 20 2a 2f 0d 0a 20 20 69  mentally */..  i
415cd 6e 74 20 69 44 6f 63 6c 69 73 74 54 6f 6b 65 6e  nt iDoclistToken
415ce 3b 0d 0a 0d 0a 20 20 2f 2a 20 56 61 72 69 61 62  ;....  /* Variab
415cf 6c 65 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70  les below this p
415d0 6f 69 6e 74 20 61 72 65 20 70 6f 70 75 6c 61 74  oint are populat
415d1 65 64 20 62 79 20 66 74 73 33 5f 65 78 70 72 2e  ed by fts3_expr.
415d2 63 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 0d  c when parsing .
415d3 0a 20 20 2a 2a 20 61 20 4d 41 54 43 48 20 65 78  .  ** a MATCH ex
415d4 70 72 65 73 73 69 6f 6e 2e 20 45 76 65 72 79 74  pression. Everyt
415d5 68 69 6e 67 20 61 62 6f 76 65 20 69 73 20 70 61  hing above is pa
415d6 72 74 20 6f 66 20 74 68 65 20 65 76 61 6c 75 61  rt of the evalua
415d7 74 69 6f 6e 20 70 68 61 73 65 2e 20 0d 0a 20 20  tion phase. ..  
415d8 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e  */..  int nToken
415d9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
415da 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f   /* Number of to
415db 6b 65 6e 73 20 69 6e 20 74 68 65 20 70 68 72 61  kens in the phra
415dc 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f  se */..  int iCo
415dd 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lumn;           
415de 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
415df 63 6f 6c 75 6d 6e 20 74 68 69 73 20 70 68 72 61  column this phra
415e0 73 65 20 6d 75 73 74 20 6d 61 74 63 68 20 2a 2f  se must match */
415e1 0d 0a 20 20 46 74 73 33 50 68 72 61 73 65 54 6f  ..  Fts3PhraseTo
415e2 6b 65 6e 20 61 54 6f 6b 65 6e 5b 31 5d 3b 20 2f  ken aToken[1]; /
415e3 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
415e4 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68  each token in th
415e5 65 20 70 68 72 61 73 65 20 2a 2f 0d 0a 7d 3b 0d  e phrase */..};.
415e6 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 74 72 65 65  .../*..** A tree
415e7 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63 74   of these object
415e8 73 20 66 6f 72 6d 73 20 74 68 65 20 52 48 53 20  s forms the RHS 
415e9 6f 66 20 61 20 4d 41 54 43 48 20 6f 70 65 72 61  of a MATCH opera
415ea 74 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  tor...**..** If 
415eb 46 74 73 33 45 78 70 72 2e 65 54 79 70 65 20 69  Fts3Expr.eType i
415ec 73 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  s FTSQUERY_PHRAS
415ed 45 20 61 6e 64 20 69 73 4c 6f 61 64 65 64 20 69  E and isLoaded i
415ee 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 44 6f  s true, then aDo
415ef 63 6c 69 73 74 20 0d 0a 2a 2a 20 70 6f 69 6e 74  clist ..** point
415f0 73 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 65 64 20  s to a malloced 
415f1 62 75 66 66 65 72 2c 20 73 69 7a 65 20 6e 44 6f  buffer, size nDo
415f2 63 6c 69 73 74 20 62 79 74 65 73 2c 20 63 6f 6e  clist bytes, con
415f3 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75  taining the resu
415f4 6c 74 73 20 0d 0a 2a 2a 20 6f 66 20 74 68 69 73  lts ..** of this
415f5 20 70 68 72 61 73 65 20 71 75 65 72 79 20 69 6e   phrase query in
415f6 20 46 54 53 33 20 64 6f 63 6c 69 73 74 20 66 6f   FTS3 doclist fo
415f7 72 6d 61 74 2e 20 41 73 20 75 73 75 61 6c 2c 20  rmat. As usual, 
415f8 74 68 65 20 69 6e 69 74 69 61 6c 20 0d 0a 2a 2a  the initial ..**
415f9 20 22 4c 65 6e 67 74 68 22 20 66 69 65 6c 64 20   "Length" field 
415fa 66 6f 75 6e 64 20 69 6e 20 64 6f 63 6c 69 73 74  found in doclist
415fb 73 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b  s stored on disk
415fc 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d   is omitted from
415fd 20 74 68 69 73 20 0d 0a 2a 2a 20 62 75 66 66 65   this ..** buffe
415fe 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 56 61 72 69 61  r...**..** Varia
415ff 62 6c 65 20 61 4d 49 20 69 73 20 75 73 65 64 20  ble aMI is used 
41600 6f 6e 6c 79 20 66 6f 72 20 46 54 53 51 55 45 52  only for FTSQUER
41601 59 5f 4e 45 41 52 20 6e 6f 64 65 73 20 74 6f 20  Y_NEAR nodes to 
41602 73 74 6f 72 65 20 74 68 65 20 67 6c 6f 62 61 6c  store the global
41603 0d 0a 2a 2a 20 6d 61 74 63 68 69 6e 66 6f 20 64  ..** matchinfo d
41604 61 74 61 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  ata. If it is no
41605 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
41606 73 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  s to an array of
41607 20 73 69 7a 65 20 6e 43 6f 6c 2a 33 2c 0d 0a 2a   size nCol*3,..*
41608 2a 20 77 68 65 72 65 20 6e 43 6f 6c 20 69 73 20  * where nCol is 
41609 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
4160a 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 71 75 65  lumns in the que
4160b 72 69 65 64 20 46 54 53 20 74 61 62 6c 65 2e 20  ried FTS table. 
4160c 54 68 65 20 61 72 72 61 79 0d 0a 2a 2a 20 69 73  The array..** is
4160d 20 70 6f 70 75 6c 61 74 65 64 20 61 73 20 66 6f   populated as fo
4160e 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  llows:..**..**  
4160f 20 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 30 5d   aMI[iCol*3 + 0]
41610 20 3d 20 55 6e 64 65 66 69 6e 65 64 0d 0a 2a 2a   = Undefined..**
41611 20 20 20 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20     aMI[iCol*3 + 
41612 31 5d 20 3d 20 4e 75 6d 62 65 72 20 6f 66 20 6f  1] = Number of o
41613 63 63 75 72 72 65 6e 63 65 73 0d 0a 2a 2a 20 20  ccurrences..**  
41614 20 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d   aMI[iCol*3 + 2]
41615 20 3d 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77   = Number of row
41616 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 74 20  s containing at 
41617 6c 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e  least one instan
41618 63 65 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61  ce..**..** The a
41619 4d 49 20 61 72 72 61 79 20 69 73 20 61 6c 6c 6f  MI array is allo
4161a 63 61 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69  cated using sqli
4161b 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 74  te3_malloc(). It
4161c 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64   should be freed
4161d 20 0d 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 65   ..** when the e
4161e 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 69  xpression node i
4161f 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 46  s...*/..struct F
41620 74 73 33 45 78 70 72 20 7b 0d 0a 20 20 69 6e 74  ts3Expr {..  int
41621 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20   eType;         
41622 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
41623 66 20 74 68 65 20 46 54 53 51 55 45 52 59 5f 58  f the FTSQUERY_X
41624 58 58 20 76 61 6c 75 65 73 20 64 65 66 69 6e 65  XX values define
41625 64 20 62 65 6c 6f 77 20 2a 2f 0d 0a 20 20 69 6e  d below */..  in
41626 74 20 6e 4e 65 61 72 3b 20 20 20 20 20 20 20 20  t nNear;        
41627 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69           /* Vali
41628 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54 53 51  d if eType==FTSQ
41629 55 45 52 59 5f 4e 45 41 52 20 2a 2f 0d 0a 20 20  UERY_NEAR */..  
4162a 46 74 73 33 45 78 70 72 20 2a 70 50 61 72 65 6e  Fts3Expr *pParen
4162b 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50  t;         /* pP
4162c 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74 68  arent->pLeft==th
4162d 69 73 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e 70  is or pParent->p
4162e 52 69 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0d 0a  Right==this */..
4162f 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65 66    Fts3Expr *pLef
41630 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
41631 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0d  Left operand */.
41632 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 69  .  Fts3Expr *pRi
41633 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ght;          /*
41634 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   Right operand *
41635 2f 0d 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  /..  Fts3Phrase 
41636 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20 20 20  *pPhrase;       
41637 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79 70  /* Valid if eTyp
41638 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
41639 53 45 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 54 68  SE */....  /* Th
4163a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
4163b 75 73 65 64 20 62 79 20 74 68 65 20 66 74 73 33  used by the fts3
4163c 5f 65 76 61 6c 2e 63 20 6d 6f 64 75 6c 65 2e 20  _eval.c module. 
4163d 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  */..  sqlite3_in
4163e 74 36 34 20 69 44 6f 63 69 64 3b 20 20 20 20 20  t64 iDocid;     
4163f 20 2f 2a 20 43 75 72 72 65 6e 74 20 64 6f 63 69   /* Current doci
41640 64 20 2a 2f 0d 0a 20 20 75 38 20 62 45 6f 66 3b  d */..  u8 bEof;
41641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41642 20 20 20 2f 2a 20 54 72 75 65 20 74 68 69 73 20     /* True this 
41643 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 74  expression is at
41644 20 45 4f 46 20 61 6c 72 65 61 64 79 20 2a 2f 0d   EOF already */.
41645 0a 20 20 75 38 20 62 53 74 61 72 74 3b 20 20 20  .  u8 bStart;   
41646 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41647 20 54 72 75 65 20 69 66 20 69 44 6f 63 69 64 20   True if iDocid 
41648 69 73 20 76 61 6c 69 64 20 2a 2f 0d 0a 20 20 75  is valid */..  u
41649 38 20 62 44 65 66 65 72 72 65 64 3b 20 20 20 20  8 bDeferred;    
4164a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
4164b 65 20 69 66 20 74 68 69 73 20 65 78 70 72 65 73  e if this expres
4164c 73 69 6f 6e 20 69 73 20 65 6e 74 69 72 65 6c 79  sion is entirely
4164d 20 64 65 66 65 72 72 65 64 20 2a 2f 0d 0a 0d 0a   deferred */....
4164e 20 20 75 33 32 20 2a 61 4d 49 3b 0d 0a 7d 3b 0d    u32 *aMI;..};.
4164f 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 6e 64 69 64  .../*..** Candid
41650 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46  ate values for F
41651 74 73 33 51 75 65 72 79 2e 65 54 79 70 65 2e 20  ts3Query.eType. 
41652 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72  Note that the or
41653 64 65 72 20 6f 66 20 74 68 65 20 66 69 72 73 74  der of the first
41654 0d 0a 2a 2a 20 66 6f 75 72 20 76 61 6c 75 65 73  ..** four values
41655 20 69 73 20 69 6e 20 6f 72 64 65 72 20 6f 66 20   is in order of 
41656 70 72 65 63 65 64 65 6e 63 65 20 77 68 65 6e 20  precedence when 
41657 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
41658 6f 6e 73 2e 20 46 6f 72 20 0d 0a 2a 2a 20 65 78  ons. For ..** ex
41659 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f  ample, the follo
4165a 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  wing:..**..**   
4165b 22 61 20 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f  "a OR b AND c NO
4165c 54 20 64 20 4e 45 41 52 20 65 22 0d 0a 2a 2a 0d  T d NEAR e"..**.
4165d 0a 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65 6e  .** is equivalen
4165e 74 20 74 6f 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  t to:..**..**   
4165f 22 61 20 4f 52 20 28 62 20 41 4e 44 20 28 63 20  "a OR (b AND (c 
41660 4e 4f 54 20 28 64 20 4e 45 41 52 20 65 29 29 29  NOT (d NEAR e)))
41661 22 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46  "..*/..#define F
41662 54 53 51 55 45 52 59 5f 4e 45 41 52 20 20 20 31  TSQUERY_NEAR   1
41663 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  ..#define FTSQUE
41664 52 59 5f 4e 4f 54 20 20 20 20 32 0d 0a 23 64 65  RY_NOT    2..#de
41665 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 41 4e  fine FTSQUERY_AN
41666 44 20 20 20 20 33 0d 0a 23 64 65 66 69 6e 65 20  D    3..#define 
41667 46 54 53 51 55 45 52 59 5f 4f 52 20 20 20 20 20  FTSQUERY_OR     
41668 34 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55  4..#define FTSQU
41669 45 52 59 5f 50 48 52 41 53 45 20 35 0d 0a 0d 0a  ERY_PHRASE 5....
4166a 0d 0a 2f 2a 20 66 74 73 33 5f 77 72 69 74 65 2e  ../* fts3_write.
4166b 63 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  c */..SQLITE_PRI
4166c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4166d 46 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f 64  Fts3UpdateMethod
4166e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 2c 69  (sqlite3_vtab*,i
4166f 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
41670 2a 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  **,sqlite3_int64
41671 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
41672 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
41673 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 46  ts3PendingTermsF
41674 6c 75 73 68 28 46 74 73 33 54 61 62 6c 65 20 2a  lush(Fts3Table *
41675 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
41676 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
41677 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 43  ts3PendingTermsC
41678 6c 65 61 72 28 46 74 73 33 54 61 62 6c 65 20 2a  lear(Fts3Table *
41679 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
4167a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
4167b 73 33 4f 70 74 69 6d 69 7a 65 28 46 74 73 33 54  s3Optimize(Fts3T
4167c 61 62 6c 65 20 2a 29 3b 0d 0a 53 51 4c 49 54 45  able *);..SQLITE
4167d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4167e 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
4167f 72 4e 65 77 28 69 6e 74 2c 20 73 71 6c 69 74 65  rNew(int, sqlite
41680 33 5f 69 6e 74 36 34 2c 0d 0a 20 20 73 71 6c 69  3_int64,..  sqli
41681 74 65 33 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74  te3_int64, sqlit
41682 65 33 5f 69 6e 74 36 34 2c 20 63 6f 6e 73 74 20  e3_int64, const 
41683 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73  char *, int, Fts
41684 33 53 65 67 52 65 61 64 65 72 2a 2a 29 3b 0d 0a  3SegReader**);..
41685 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
41686 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  nt sqlite3Fts3Se
41687 67 52 65 61 64 65 72 50 65 6e 64 69 6e 67 28 0d  gReaderPending(.
41688 0a 20 20 46 74 73 33 54 61 62 6c 65 2a 2c 69 6e  .  Fts3Table*,in
41689 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  t,const char*,in
4168a 74 2c 69 6e 74 2c 46 74 73 33 53 65 67 52 65 61  t,int,Fts3SegRea
4168b 64 65 72 2a 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  der**);..SQLITE_
4168c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4168d 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
4168e 72 46 72 65 65 28 46 74 73 33 53 65 67 52 65 61  rFree(Fts3SegRea
4168f 64 65 72 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  der *);..SQLITE_
41690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
41691 74 65 33 46 74 73 33 41 6c 6c 53 65 67 64 69 72  te3Fts3AllSegdir
41692 73 28 46 74 73 33 54 61 62 6c 65 2a 2c 20 69 6e  s(Fts3Table*, in
41693 74 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f  t, int, sqlite3_
41694 73 74 6d 74 20 2a 2a 29 3b 0d 0a 53 51 4c 49 54  stmt **);..SQLIT
41695 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
41696 6c 69 74 65 33 46 74 73 33 52 65 61 64 4c 6f 63  lite3Fts3ReadLoc
41697 6b 28 46 74 73 33 54 61 62 6c 65 20 2a 29 3b 0d  k(Fts3Table *);.
41698 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
41699 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 52  int sqlite3Fts3R
4169a 65 61 64 42 6c 6f 63 6b 28 46 74 73 33 54 61 62  eadBlock(Fts3Tab
4169b 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  le*, sqlite3_int
4169c 36 34 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74  64, char **, int
4169d 2a 2c 20 69 6e 74 2a 29 3b 0d 0a 0d 0a 53 51 4c  *, int*);....SQL
4169e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4169f 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c 65 63  sqlite3Fts3Selec
416a0 74 44 6f 63 74 6f 74 61 6c 28 46 74 73 33 54 61  tDoctotal(Fts3Ta
416a1 62 6c 65 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73  ble *, sqlite3_s
416a2 74 6d 74 20 2a 2a 29 3b 0d 0a 53 51 4c 49 54 45  tmt **);..SQLITE
416a3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
416a4 69 74 65 33 46 74 73 33 53 65 6c 65 63 74 44 6f  ite3Fts3SelectDo
416a5 63 73 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20  csize(Fts3Table 
416a6 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
416a7 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
416a8 2a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  *);....SQLITE_PR
416a9 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
416aa 65 33 46 74 73 33 46 72 65 65 44 65 66 65 72 72  e3Fts3FreeDeferr
416ab 65 64 54 6f 6b 65 6e 73 28 46 74 73 33 43 75 72  edTokens(Fts3Cur
416ac 73 6f 72 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f  sor *);..SQLITE_
416ad 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
416ae 74 65 33 46 74 73 33 44 65 66 65 72 54 6f 6b 65  te3Fts3DeferToke
416af 6e 28 46 74 73 33 43 75 72 73 6f 72 20 2a 2c 20  n(Fts3Cursor *, 
416b0 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20  Fts3PhraseToken 
416b1 2a 2c 20 69 6e 74 29 3b 0d 0a 53 51 4c 49 54 45  *, int);..SQLITE
416b2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
416b3 69 74 65 33 46 74 73 33 43 61 63 68 65 44 65 66  ite3Fts3CacheDef
416b4 65 72 72 65 64 44 6f 63 6c 69 73 74 73 28 46 74  erredDoclists(Ft
416b5 73 33 43 75 72 73 6f 72 20 2a 29 3b 0d 0a 53 51  s3Cursor *);..SQ
416b6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
416b7 64 20 73 71 6c 69 74 65 33 46 74 73 33 46 72 65  d sqlite3Fts3Fre
416b8 65 44 65 66 65 72 72 65 64 44 6f 63 6c 69 73 74  eDeferredDoclist
416b9 73 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 3b  s(Fts3Cursor *);
416ba 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
416bb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
416bc 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 46  3SegmentsClose(F
416bd 74 73 33 54 61 62 6c 65 20 2a 29 3b 0d 0a 0d 0a  ts3Table *);....
416be 2f 2a 20 53 70 65 63 69 61 6c 20 76 61 6c 75 65  /* Special value
416bf 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 62 79  s interpreted by
416c0 20 73 71 6c 69 74 65 33 53 65 67 52 65 61 64 65   sqlite3SegReade
416c1 72 43 75 72 73 6f 72 28 29 20 2a 2f 0d 0a 23 64  rCursor() */..#d
416c2 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47 43 55  efine FTS3_SEGCU
416c3 52 53 4f 52 5f 50 45 4e 44 49 4e 47 20 20 20 20  RSOR_PENDING    
416c4 20 20 20 20 2d 31 0d 0a 23 64 65 66 69 6e 65 20      -1..#define 
416c5 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f 41  FTS3_SEGCURSOR_A
416c6 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2d 32  LL            -2
416c7 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ....SQLITE_PRIVA
416c8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
416c9 73 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74  s3SegReaderStart
416ca 28 46 74 73 33 54 61 62 6c 65 2a 2c 20 46 74 73  (Fts3Table*, Fts
416cb 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 2a  3MultiSegReader*
416cc 2c 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 2a  , Fts3SegFilter*
416cd 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
416ce 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
416cf 73 33 53 65 67 52 65 61 64 65 72 53 74 65 70 28  s3SegReaderStep(
416d0 46 74 73 33 54 61 62 6c 65 20 2a 2c 20 46 74 73  Fts3Table *, Fts
416d1 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20  3MultiSegReader 
416d2 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
416d3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
416d4 46 74 73 33 53 65 67 52 65 61 64 65 72 46 69 6e  Fts3SegReaderFin
416d5 69 73 68 28 46 74 73 33 4d 75 6c 74 69 53 65 67  ish(Fts3MultiSeg
416d6 52 65 61 64 65 72 20 2a 29 3b 0d 0a 0d 0a 53 51  Reader *);....SQ
416d7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
416d8 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
416d9 65 61 64 65 72 43 75 72 73 6f 72 28 0d 0a 20 20  eaderCursor(..  
416da 20 20 46 74 73 33 54 61 62 6c 65 20 2a 2c 20 69    Fts3Table *, i
416db 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  nt, int, const c
416dc 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  har *, int, int,
416dd 20 69 6e 74 2c 20 46 74 73 33 4d 75 6c 74 69 53   int, Fts3MultiS
416de 65 67 52 65 61 64 65 72 20 2a 29 3b 0d 0a 0d 0a  egReader *);....
416df 2f 2a 20 46 6c 61 67 73 20 61 6c 6c 6f 77 65 64  /* Flags allowed
416e0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
416e1 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  4th argument to 
416e2 53 65 67 6d 65 6e 74 52 65 61 64 65 72 49 74 65  SegmentReaderIte
416e3 72 61 74 65 28 29 20 2a 2f 0d 0a 23 64 65 66 69  rate() */..#defi
416e4 6e 65 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f  ne FTS3_SEGMENT_
416e5 52 45 51 55 49 52 45 5f 50 4f 53 20 20 20 30 78  REQUIRE_POS   0x
416e6 30 30 30 30 30 30 30 31 0d 0a 23 64 65 66 69 6e  00000001..#defin
416e7 65 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 49  e FTS3_SEGMENT_I
416e8 47 4e 4f 52 45 5f 45 4d 50 54 59 20 20 30 78 30  GNORE_EMPTY  0x0
416e9 30 30 30 30 30 30 32 0d 0a 23 64 65 66 69 6e 65  0000002..#define
416ea 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43 4f   FTS3_SEGMENT_CO
416eb 4c 55 4d 4e 5f 46 49 4c 54 45 52 20 30 78 30 30  LUMN_FILTER 0x00
416ec 30 30 30 30 30 34 0d 0a 23 64 65 66 69 6e 65 20  000004..#define 
416ed 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 50 52 45  FTS3_SEGMENT_PRE
416ee 46 49 58 20 20 20 20 20 20 20 20 30 78 30 30 30  FIX        0x000
416ef 30 30 30 30 38 0d 0a 23 64 65 66 69 6e 65 20 46  00008..#define F
416f0 54 53 33 5f 53 45 47 4d 45 4e 54 5f 53 43 41 4e  TS3_SEGMENT_SCAN
416f1 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
416f2 30 30 31 30 0d 0a 23 64 65 66 69 6e 65 20 46 54  0010..#define FT
416f3 53 33 5f 53 45 47 4d 45 4e 54 5f 46 49 52 53 54  S3_SEGMENT_FIRST
416f4 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
416f5 30 32 30 0d 0a 0d 0a 2f 2a 20 54 79 70 65 20 70  020..../* Type p
416f6 61 73 73 65 64 20 61 73 20 34 74 68 20 61 72 67  assed as 4th arg
416f7 75 6d 65 6e 74 20 74 6f 20 53 65 67 6d 65 6e 74  ument to Segment
416f8 52 65 61 64 65 72 49 74 65 72 61 74 65 28 29 20  ReaderIterate() 
416f9 2a 2f 0d 0a 73 74 72 75 63 74 20 46 74 73 33 53  */..struct Fts3S
416fa 65 67 46 69 6c 74 65 72 20 7b 0d 0a 20 20 63 6f  egFilter {..  co
416fb 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  nst char *zTerm;
416fc 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0d 0a  ..  int nTerm;..
416fd 20 20 69 6e 74 20 69 43 6f 6c 3b 0d 0a 20 20 69    int iCol;..  i
416fe 6e 74 20 66 6c 61 67 73 3b 0d 0a 7d 3b 0d 0a 0d  nt flags;..};...
416ff 0a 73 74 72 75 63 74 20 46 74 73 33 4d 75 6c 74  .struct Fts3Mult
41700 69 53 65 67 52 65 61 64 65 72 20 7b 0d 0a 20 20  iSegReader {..  
41701 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c  /* Used internal
41702 6c 79 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ly by sqlite3Fts
41703 33 53 65 67 52 65 61 64 65 72 58 58 58 28 29 20  3SegReaderXXX() 
41704 63 61 6c 6c 73 20 2a 2f 0d 0a 20 20 46 74 73 33  calls */..  Fts3
41705 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70 53 65  SegReader **apSe
41706 67 6d 65 6e 74 3b 20 20 20 20 20 20 2f 2a 20 41  gment;      /* A
41707 72 72 61 79 20 6f 66 20 46 74 73 33 53 65 67 52  rray of Fts3SegR
41708 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20 2a 2f  eader objects */
41709 0d 0a 20 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74  ..  int nSegment
4170a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4170b 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
4170c 70 53 65 67 6d 65 6e 74 20 61 72 72 61 79 20 2a  pSegment array *
4170d 2f 0d 0a 20 20 69 6e 74 20 6e 41 64 76 61 6e 63  /..  int nAdvanc
4170e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
4170f 20 20 20 20 20 2f 2a 20 48 6f 77 20 6d 61 6e 79       /* How many
41710 20 73 65 67 2d 72 65 61 64 65 72 73 20 74 6f 20   seg-readers to 
41711 61 64 76 61 6e 63 65 20 2a 2f 0d 0a 20 20 46 74  advance */..  Ft
41712 73 33 53 65 67 46 69 6c 74 65 72 20 2a 70 46 69  s3SegFilter *pFi
41713 6c 74 65 72 3b 20 20 20 20 20 20 20 20 20 2f 2a  lter;         /*
41714 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 6c 74   Pointer to filt
41715 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20  er object */..  
41716 63 68 61 72 20 2a 61 42 75 66 66 65 72 3b 20 20  char *aBuffer;  
41717 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41718 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 6d 65 72  /* Buffer to mer
41719 67 65 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 2a  ge doclists in *
4171a 2f 0d 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72  /..  int nBuffer
4171b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4171c 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
4171d 64 20 73 69 7a 65 20 6f 66 20 61 42 75 66 66 65  d size of aBuffe
4171e 72 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d  r[] in bytes */.
4171f 0a 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 46 69 6c  ...  int iColFil
41720 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ter;            
41721 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20       /* If >=0, 
41722 66 69 6c 74 65 72 20 66 6f 72 20 74 68 69 73 20  filter for this 
41723 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 69 6e 74  column */..  int
41724 20 62 52 65 73 74 61 72 74 3b 0d 0a 0d 0a 20 20   bRestart;....  
41725 2f 2a 20 55 73 65 64 20 62 79 20 66 74 73 33 2e  /* Used by fts3.
41726 63 20 6f 6e 6c 79 2e 20 2a 2f 0d 0a 20 20 69 6e  c only. */..  in
41727 74 20 6e 43 6f 73 74 3b 20 20 20 20 20 20 20 20  t nCost;        
41728 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41729 20 43 6f 73 74 20 6f 66 20 72 75 6e 6e 69 6e 67   Cost of running
4172a 20 69 74 65 72 61 74 6f 72 20 2a 2f 0d 0a 20 20   iterator */..  
4172b 69 6e 74 20 62 4c 6f 6f 6b 75 70 3b 20 20 20 20  int bLookup;    
4172c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4172d 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6c 6f 6f  /* True if a loo
4172e 6b 75 70 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  kup of a single 
4172f 65 6e 74 72 79 2e 20 2a 2f 0d 0a 0d 0a 20 20 2f  entry. */....  /
41730 2a 20 4f 75 74 70 75 74 20 76 61 6c 75 65 73 2e  * Output values.
41731 20 56 61 6c 69 64 20 6f 6e 6c 79 20 61 66 74 65   Valid only afte
41732 72 20 46 74 73 33 53 65 67 52 65 61 64 65 72 53  r Fts3SegReaderS
41733 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 53 51  tep() returns SQ
41734 4c 49 54 45 5f 52 4f 57 2e 20 2a 2f 0d 0a 20 20  LITE_ROW. */..  
41735 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20  char *zTerm;    
41736 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41737 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 65  /* Pointer to te
41738 72 6d 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20  rm buffer */..  
41739 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20  int nTerm;      
4173a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4173b 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 65 72 6d  /* Size of zTerm
4173c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20   in bytes */..  
4173d 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 3b 20  char *aDoclist; 
4173e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4173f 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 6f  /* Pointer to do
41740 63 6c 69 73 74 20 62 75 66 66 65 72 20 2a 2f 0d  clist buffer */.
41741 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b  .  int nDoclist;
41742 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41743 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 44     /* Size of aD
41744 6f 63 6c 69 73 74 5b 5d 20 69 6e 20 62 79 74 65  oclist[] in byte
41745 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 66  s */..};..../* f
41746 74 73 33 2e 63 20 2a 2f 0d 0a 53 51 4c 49 54 45  ts3.c */..SQLITE
41747 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
41748 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e  ite3Fts3PutVarin
41749 74 28 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65  t(char *, sqlite
4174a 33 5f 69 6e 74 36 34 29 3b 0d 0a 53 51 4c 49 54  3_int64);..SQLIT
4174b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4174c 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
4174d 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  nt(const char *,
4174e 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 29   sqlite_int64 *)
4174f 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
41750 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
41751 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e  3GetVarint32(con
41752 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a  st char *, int *
41753 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
41754 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
41755 73 33 56 61 72 69 6e 74 4c 65 6e 28 73 71 6c 69  s3VarintLen(sqli
41756 74 65 33 5f 75 69 6e 74 36 34 29 3b 0d 0a 53 51  te3_uint64);..SQ
41757 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
41758 64 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71  d sqlite3Fts3Deq
41759 75 6f 74 65 28 63 68 61 72 20 2a 29 3b 0d 0a 53  uote(char *);..S
4175a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4175b 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f  id sqlite3Fts3Do
4175c 63 6c 69 73 74 50 72 65 76 28 69 6e 74 2c 63 68  clistPrev(int,ch
4175d 61 72 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 73  ar*,int,char**,s
4175e 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 2c 69 6e  qlite3_int64*,in
4175f 74 2a 2c 75 38 2a 29 3b 0d 0a 53 51 4c 49 54 45  t*,u8*);..SQLITE
41760 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
41761 69 74 65 33 46 74 73 33 45 76 61 6c 50 68 72 61  ite3Fts3EvalPhra
41762 73 65 53 74 61 74 73 28 46 74 73 33 43 75 72 73  seStats(Fts3Curs
41763 6f 72 20 2a 2c 20 46 74 73 33 45 78 70 72 20 2a  or *, Fts3Expr *
41764 2c 20 75 33 32 20 2a 29 3b 0d 0a 53 51 4c 49 54  , u32 *);..SQLIT
41765 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
41766 6c 69 74 65 33 46 74 73 33 46 69 72 73 74 46 69  lite3Fts3FirstFi
41767 6c 74 65 72 28 73 71 6c 69 74 65 33 5f 69 6e 74  lter(sqlite3_int
41768 36 34 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  64, char *, int,
41769 20 63 68 61 72 20 2a 29 3b 0d 0a 0d 0a 2f 2a 20   char *);..../* 
4176a 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63  fts3_tokenizer.c
4176b 20 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
4176c 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
4176d 73 71 6c 69 74 65 33 46 74 73 33 4e 65 78 74 54  sqlite3Fts3NextT
4176e 6f 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  oken(const char 
4176f 2a 2c 20 69 6e 74 20 2a 29 3b 0d 0a 53 51 4c 49  *, int *);..SQLI
41770 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
41771 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
41772 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  shTable(sqlite3 
41773 2a 2c 20 46 74 73 33 48 61 73 68 20 2a 2c 20 63  *, Fts3Hash *, c
41774 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0d 0a 53  onst char *);..S
41775 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
41776 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  t sqlite3Fts3Ini
41777 74 54 6f 6b 65 6e 69 7a 65 72 28 46 74 73 33 48  tTokenizer(Fts3H
41778 61 73 68 20 2a 70 48 61 73 68 2c 20 63 6f 6e 73  ash *pHash, cons
41779 74 20 63 68 61 72 20 2a 2c 20 0d 0a 20 20 20 20  t char *, ..    
4177a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
4177b 72 20 2a 2a 2c 20 63 68 61 72 20 2a 2a 0d 0a 29  r **, char **..)
4177c 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
4177d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
4177e 33 49 73 49 64 43 68 61 72 28 63 68 61 72 29 3b  3IsIdChar(char);
4177f 0d 0a 0d 0a 2f 2a 20 66 74 73 33 5f 73 6e 69 70  ..../* fts3_snip
41780 70 65 74 2e 63 20 2a 2f 0d 0a 53 51 4c 49 54 45  pet.c */..SQLITE
41781 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
41782 6c 69 74 65 33 46 74 73 33 4f 66 66 73 65 74 73  lite3Fts3Offsets
41783 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
41784 2a 2c 20 46 74 73 33 43 75 72 73 6f 72 2a 29 3b  *, Fts3Cursor*);
41785 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
41786 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
41787 33 53 6e 69 70 70 65 74 28 73 71 6c 69 74 65 33  3Snippet(sqlite3
41788 5f 63 6f 6e 74 65 78 74 20 2a 2c 20 46 74 73 33  _context *, Fts3
41789 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20  Cursor *, const 
4178a 63 68 61 72 20 2a 2c 0d 0a 20 20 63 6f 6e 73 74  char *,..  const
4178b 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63   char *, const c
4178c 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 0d  har *, int, int.
4178d 0a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .);..SQLITE_PRIV
4178e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4178f 46 74 73 33 4d 61 74 63 68 69 6e 66 6f 28 73 71  Fts3Matchinfo(sq
41790 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 2c  lite3_context *,
41791 20 46 74 73 33 43 75 72 73 6f 72 20 2a 2c 20 63   Fts3Cursor *, c
41792 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0d 0a 0d  onst char *);...
41793 0a 2f 2a 20 66 74 73 33 5f 65 78 70 72 2e 63 20  ./* fts3_expr.c 
41794 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
41795 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
41796 73 33 45 78 70 72 50 61 72 73 65 28 73 71 6c 69  s3ExprParse(sqli
41797 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2c  te3_tokenizer *,
41798 20 0d 0a 20 20 63 68 61 72 20 2a 2a 2c 20 69 6e   ..  char **, in
41799 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e  t, int, int, con
4179a 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
4179b 46 74 73 33 45 78 70 72 20 2a 2a 0d 0a 29 3b 0d  Fts3Expr **..);.
4179c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4179d 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
4179e 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70  ExprFree(Fts3Exp
4179f 72 20 2a 29 3b 0d 0a 23 69 66 64 65 66 20 53 51  r *);..#ifdef SQ
417a0 4c 49 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54  LITE_TEST..SQLIT
417a1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
417a2 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
417a3 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 73  tTestInterface(s
417a4 71 6c 69 74 65 33 20 2a 64 62 29 3b 0d 0a 53 51  qlite3 *db);..SQ
417a5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
417a6 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
417a7 54 65 72 6d 28 73 71 6c 69 74 65 33 20 2a 64 62  Term(sqlite3 *db
417a8 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a  );..#endif..../*
417a9 20 66 74 73 33 5f 61 75 78 2e 63 20 2a 2f 0d 0a   fts3_aux.c */..
417aa 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
417ab 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  nt sqlite3Fts3In
417ac 69 74 41 75 78 28 73 71 6c 69 74 65 33 20 2a 64  itAux(sqlite3 *d
417ad 62 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52  b);....SQLITE_PR
417ae 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
417af 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65  e3Fts3EvalPhrase
417b0 43 6c 65 61 6e 75 70 28 46 74 73 33 50 68 72 61  Cleanup(Fts3Phra
417b1 73 65 20 2a 29 3b 0d 0a 0d 0a 53 51 4c 49 54 45  se *);....SQLITE
417b2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
417b3 69 74 65 33 46 74 73 33 4d 73 72 49 6e 63 72 53  ite3Fts3MsrIncrS
417b4 74 61 72 74 28 0d 0a 20 20 20 20 46 74 73 33 54  tart(..    Fts3T
417b5 61 62 6c 65 2a 2c 20 46 74 73 33 4d 75 6c 74 69  able*, Fts3Multi
417b6 53 65 67 52 65 61 64 65 72 2a 2c 20 69 6e 74 2c  SegReader*, int,
417b7 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e   const char*, in
417b8 74 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
417b9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
417ba 74 73 33 4d 73 72 49 6e 63 72 4e 65 78 74 28 0d  ts3MsrIncrNext(.
417bb 0a 20 20 20 20 46 74 73 33 54 61 62 6c 65 20 2a  .    Fts3Table *
417bc 2c 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65  , Fts3MultiSegRe
417bd 61 64 65 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  ader *, sqlite3_
417be 69 6e 74 36 34 20 2a 2c 20 63 68 61 72 20 2a 2a  int64 *, char **
417bf 2c 20 69 6e 74 20 2a 29 3b 0d 0a 53 51 4c 49 54  , int *);..SQLIT
417c0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
417c1 73 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50  sqlite3Fts3EvalP
417c2 68 72 61 73 65 50 6f 73 6c 69 73 74 28 46 74 73  hrasePoslist(Fts
417c3 33 43 75 72 73 6f 72 20 2a 2c 20 46 74 73 33 45  3Cursor *, Fts3E
417c4 78 70 72 20 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  xpr *, int iCol)
417c5 3b 20 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ; ..SQLITE_PRIVA
417c6 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
417c7 73 33 4d 73 72 4f 76 66 6c 28 46 74 73 33 43 75  s3MsrOvfl(Fts3Cu
417c8 72 73 6f 72 20 2a 2c 20 46 74 73 33 4d 75 6c 74  rsor *, Fts3Mult
417c9 69 53 65 67 52 65 61 64 65 72 20 2a 2c 20 69 6e  iSegReader *, in
417ca 74 20 2a 29 3b 0d 0a 53 51 4c 49 54 45 5f 50 52  t *);..SQLITE_PR
417cb 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
417cc 33 46 74 73 33 4d 73 72 49 6e 63 72 52 65 73 74  3Fts3MsrIncrRest
417cd 61 72 74 28 46 74 73 33 4d 75 6c 74 69 53 65 67  art(Fts3MultiSeg
417ce 52 65 61 64 65 72 20 2a 70 43 73 72 29 3b 0d 0a  Reader *pCsr);..
417cf 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
417d0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
417d1 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 4c 69 73  DeferredTokenLis
417d2 74 28 46 74 73 33 44 65 66 65 72 72 65 64 54 6f  t(Fts3DeferredTo
417d3 6b 65 6e 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20  ken *, char **, 
417d4 69 6e 74 20 2a 29 3b 0d 0a 0d 0a 23 65 6e 64 69  int *);....#endi
417d5 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 43 4f 52  f /* !SQLITE_COR
417d6 45 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42  E || SQLITE_ENAB
417d7 4c 45 5f 46 54 53 33 20 2a 2f 0d 0a 23 65 6e 64  LE_FTS3 */..#end
417d8 69 66 20 2f 2a 20 5f 46 54 53 49 4e 54 5f 48 20  if /* _FTSINT_H 
417d9 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
417da 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
417db 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3Int.h *********
417dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
417dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
417de 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
417df 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
417e0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
417e1 6f 66 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a  off in fts3.c **
417e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
417e3 2a 2a 2a 2a 2a 2f 0d 0a 23 69 66 20 21 64 65 66  *****/..#if !def
417e4 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
417e5 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
417e6 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
417e7 0d 0a 0d 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ....#if defined(
417e8 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
417e9 53 33 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  S3) && !defined(
417ea 53 51 4c 49 54 45 5f 43 4f 52 45 29 0d 0a 23 20  SQLITE_CORE)..# 
417eb 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
417ec 52 45 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  RE 1..#endif....
417ed 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61 73 73  /* #include <ass
417ee 65 72 74 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69  ert.h> */../* #i
417ef 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68  nclude <stdlib.h
417f0 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  > */../* #includ
417f1 65 20 3c 73 74 64 64 65 66 2e 68 3e 20 2a 2f 0d  e <stddef.h> */.
417f2 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
417f3 64 69 6f 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69  dio.h> */../* #i
417f4 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68  nclude <string.h
417f5 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  > */../* #includ
417f6 65 20 3c 73 74 64 61 72 67 2e 68 3e 20 2a 2f 0d  e <stdarg.h> */.
417f7 0a 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
417f8 45 5f 43 4f 52 45 20 0d 0a 20 20 53 51 4c 49 54  E_CORE ..  SQLIT
417f9 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
417fa 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 73 74 61  1..#endif....sta
417fb 74 69 63 20 69 6e 74 20 66 74 73 33 45 76 61 6c  tic int fts3Eval
417fc 4e 65 78 74 28 46 74 73 33 43 75 72 73 6f 72 20  Next(Fts3Cursor 
417fd 2a 70 43 73 72 29 3b 0d 0a 73 74 61 74 69 63 20  *pCsr);..static 
417fe 69 6e 74 20 66 74 73 33 45 76 61 6c 53 74 61 72  int fts3EvalStar
417ff 74 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43  t(Fts3Cursor *pC
41800 73 72 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e 74  sr);..static int
41801 20 66 74 73 33 54 65 72 6d 53 65 67 52 65 61 64   fts3TermSegRead
41802 65 72 43 75 72 73 6f 72 28 0d 0a 20 20 20 20 46  erCursor(..    F
41803 74 73 33 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e  ts3Cursor *, con
41804 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
41805 69 6e 74 2c 20 46 74 73 33 4d 75 6c 74 69 53 65  int, Fts3MultiSe
41806 67 52 65 61 64 65 72 20 2a 2a 29 3b 0d 0a 0d 0a  gReader **);....
41807 2f 2a 20 0d 0a 2a 2a 20 57 72 69 74 65 20 61 20  /* ..** Write a 
41808 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
41809 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74  length integer t
4180a 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  o memory startin
4180b 67 20 61 74 20 70 5b 30 5d 2e 0d 0a 2a 2a 20 54  g at p[0]...** T
4180c 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74  he length of dat
4180d 61 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62  a written will b
4180e 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
4180f 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20  FTS3_VARINT_MAX 
41810 62 79 74 65 73 2e 0d 0a 2a 2a 20 54 68 65 20 6e  bytes...** The n
41811 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77  umber of bytes w
41812 72 69 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e  ritten is return
41813 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ed...*/..SQLITE_
41814 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
41815 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74  te3Fts3PutVarint
41816 28 63 68 61 72 20 2a 70 2c 20 73 71 6c 69 74 65  (char *p, sqlite
41817 5f 69 6e 74 36 34 20 76 29 7b 0d 0a 20 20 75 6e  _int64 v){..  un
41818 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20 3d  signed char *q =
41819 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
4181a 2a 29 20 70 3b 0d 0a 20 20 73 71 6c 69 74 65 5f  *) p;..  sqlite_
4181b 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b 0d 0a  uint64 vu = v;..
4181c 20 20 64 6f 7b 0d 0a 20 20 20 20 2a 71 2b 2b 20    do{..    *q++ 
4181d 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
4181e 29 20 28 28 76 75 20 26 20 30 78 37 66 29 20 7c  ) ((vu & 0x7f) |
4181f 20 30 78 38 30 29 3b 0d 0a 20 20 20 20 76 75 20   0x80);..    vu 
41820 3e 3e 3d 20 37 3b 0d 0a 20 20 7d 77 68 69 6c 65  >>= 7;..  }while
41821 28 20 76 75 21 3d 30 20 29 3b 0d 0a 20 20 71 5b  ( vu!=0 );..  q[
41822 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a  -1] &= 0x7f;  /*
41823 20 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20 62   turn off high b
41824 69 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74 65  it in final byte
41825 20 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 71   */..  assert( q
41826 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   - (unsigned cha
41827 72 20 2a 29 70 20 3c 3d 20 46 54 53 33 5f 56 41  r *)p <= FTS3_VA
41828 52 49 4e 54 5f 4d 41 58 20 29 3b 0d 0a 20 20 72  RINT_MAX );..  r
41829 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d  eturn (int) (q -
4182a 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
4182b 2a 29 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  *)p);..}..../* .
4182c 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69  .** Read a 64-bi
4182d 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
4182e 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
4182f 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
41830 74 20 70 5b 30 5d 2e 0d 0a 2a 2a 20 52 65 74 75  t p[0]...** Retu
41831 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
41832 20 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72 20   bytes read, or 
41833 30 20 6f 6e 20 65 72 72 6f 72 2e 0d 0a 2a 2a 20  0 on error...** 
41834 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
41835 72 65 64 20 69 6e 20 2a 76 2e 0d 0a 2a 2f 0d 0a  red in *v...*/..
41836 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
41837 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  nt sqlite3Fts3Ge
41838 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 63 68  tVarint(const ch
41839 61 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e  ar *p, sqlite_in
4183a 74 36 34 20 2a 76 29 7b 0d 0a 20 20 63 6f 6e 73  t64 *v){..  cons
4183b 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
4183c 2a 71 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69  *q = (const unsi
4183d 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0d  gned char *) p;.
4183e 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  .  sqlite_uint64
4183f 20 78 20 3d 20 30 2c 20 79 20 3d 20 31 3b 0d 0a   x = 0, y = 1;..
41840 20 20 77 68 69 6c 65 28 20 28 2a 71 26 30 78 38    while( (*q&0x8
41841 30 29 3d 3d 30 78 38 30 20 26 26 20 71 2d 28 75  0)==0x80 && q-(u
41842 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
41843 3c 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58  <FTS3_VARINT_MAX
41844 20 29 7b 0d 0a 20 20 20 20 78 20 2b 3d 20 79 20   ){..    x += y 
41845 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37 66 29 3b  * (*q++ & 0x7f);
41846 0d 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b 0d 0a  ..    y <<= 7;..
41847 20 20 7d 0d 0a 20 20 78 20 2b 3d 20 79 20 2a 20    }..  x += y * 
41848 28 2a 71 2b 2b 29 3b 0d 0a 20 20 2a 76 20 3d 20  (*q++);..  *v = 
41849 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 78  (sqlite_int64) x
4184a 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  ;..  return (int
4184b 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64  ) (q - (unsigned
4184c 20 63 68 61 72 20 2a 29 70 29 3b 0d 0a 7d 0d 0a   char *)p);..}..
4184d 0d 0a 2f 2a 0d 0a 2a 2a 20 53 69 6d 69 6c 61 72  ../*..** Similar
4184e 20 74 6f 20 73 71 6c 69 74 65 33 46 74 73 33 47   to sqlite3Fts3G
4184f 65 74 56 61 72 69 6e 74 28 29 2c 20 65 78 63 65  etVarint(), exce
41850 70 74 20 74 68 61 74 20 74 68 65 20 6f 75 74 70  pt that the outp
41851 75 74 20 69 73 20 74 72 75 6e 63 61 74 65 64 20  ut is truncated 
41852 74 6f 20 61 0d 0a 2a 2a 20 33 32 2d 62 69 74 20  to a..** 32-bit 
41853 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65 20 69  integer before i
41854 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a  t is returned...
41855 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
41856 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
41857 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f  s3GetVarint32(co
41858 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69 6e 74  nst char *p, int
41859 20 2a 70 69 29 7b 0d 0a 20 73 71 6c 69 74 65 5f   *pi){.. sqlite_
4185a 69 6e 74 36 34 20 69 3b 0d 0a 20 69 6e 74 20 72  int64 i;.. int r
4185b 65 74 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  et = sqlite3Fts3
4185c 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29  GetVarint(p, &i)
4185d 3b 0d 0a 20 2a 70 69 20 3d 20 28 69 6e 74 29 20  ;.. *pi = (int) 
4185e 69 3b 0d 0a 20 72 65 74 75 72 6e 20 72 65 74 3b  i;.. return ret;
4185f 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
41860 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
41861 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65  of bytes require
41862 64 20 74 6f 20 65 6e 63 6f 64 65 20 76 20 61 73  d to encode v as
41863 20 61 20 76 61 72 69 6e 74 0d 0a 2a 2f 0d 0a 53   a varint..*/..S
41864 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
41865 74 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  t sqlite3Fts3Var
41866 69 6e 74 4c 65 6e 28 73 71 6c 69 74 65 33 5f 75  intLen(sqlite3_u
41867 69 6e 74 36 34 20 76 29 7b 0d 0a 20 20 69 6e 74  int64 v){..  int
41868 20 69 20 3d 20 30 3b 0d 0a 20 20 64 6f 7b 0d 0a   i = 0;..  do{..
41869 20 20 20 20 69 2b 2b 3b 0d 0a 20 20 20 20 76 20      i++;..    v 
4186a 3e 3e 3d 20 37 3b 0d 0a 20 20 7d 77 68 69 6c 65  >>= 7;..  }while
4186b 28 20 76 21 3d 30 20 29 3b 0d 0a 20 20 72 65 74  ( v!=0 );..  ret
4186c 75 72 6e 20 69 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  urn i;..}..../*.
4186d 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53  .** Convert an S
4186e 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20  QL-style quoted 
4186f 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f  string into a no
41870 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72  rmal string by r
41871 65 6d 6f 76 69 6e 67 0d 0a 2a 2a 20 74 68 65 20  emoving..** the 
41872 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73  quote characters
41873 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  .  The conversio
41874 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
41875 63 65 2e 20 20 49 66 20 74 68 65 0d 0a 2a 2a 20  ce.  If the..** 
41876 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62  input does not b
41877 65 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74  egin with a quot
41878 65 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65  e character, the
41879 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0d 0a  n this routine..
4187a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a  ** is a no-op...
4187b 2a 2a 0d 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 3a  **..** Examples:
4187c 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 22 61 62  ..**..**     "ab
4187d 63 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20 61  c"   becomes   a
4187e 62 63 0d 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27  bc..**     'xyz'
4187f 20 20 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a     becomes   xyz
41880 0d 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20 20  ..**     [pqr]  
41881 20 62 65 63 6f 6d 65 73 20 20 20 70 71 72 0d 0a   becomes   pqr..
41882 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20 20 20 62  **     `mno`   b
41883 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0d 0a 2a 2a  ecomes   mno..**
41884 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
41885 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
41886 33 46 74 73 33 44 65 71 75 6f 74 65 28 63 68 61  3Fts3Dequote(cha
41887 72 20 2a 7a 29 7b 0d 0a 20 20 63 68 61 72 20 71  r *z){..  char q
41888 75 6f 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  uote;           
41889 20 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 6f            /* Quo
4188a 74 65 20 63 68 61 72 61 63 74 65 72 20 28 69 66  te character (if
4188b 20 61 6e 79 20 29 20 2a 2f 0d 0a 0d 0a 20 20 71   any ) */....  q
4188c 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0d 0a 20 20  uote = z[0];..  
4188d 69 66 28 20 71 75 6f 74 65 3d 3d 27 5b 27 20 7c  if( quote=='[' |
4188e 7c 20 71 75 6f 74 65 3d 3d 27 5c 27 27 20 7c 7c  | quote=='\'' ||
4188f 20 71 75 6f 74 65 3d 3d 27 22 27 20 7c 7c 20 71   quote=='"' || q
41890 75 6f 74 65 3d 3d 27 60 27 20 29 7b 0d 0a 20 20  uote=='`' ){..  
41891 20 20 69 6e 74 20 69 49 6e 20 3d 20 31 3b 20 20    int iIn = 1;  
41892 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41893 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74  /* Index of next
41894 20 62 79 74 65 20 74 6f 20 72 65 61 64 20 66 72   byte to read fr
41895 6f 6d 20 69 6e 70 75 74 20 2a 2f 0d 0a 20 20 20  om input */..   
41896 20 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 20 20   int iOut = 0;  
41897 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41898 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20  * Index of next 
41899 62 79 74 65 20 74 6f 20 77 72 69 74 65 20 74 6f  byte to write to
4189a 20 6f 75 74 70 75 74 20 2a 2f 0d 0a 0d 0a 20 20   output */....  
4189b 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 72 73    /* If the firs
4189c 74 20 62 79 74 65 20 77 61 73 20 61 20 27 5b 27  t byte was a '['
4189d 2c 20 74 68 65 6e 20 74 68 65 20 63 6c 6f 73 65  , then the close
4189e 2d 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72  -quote character
4189f 20 69 73 20 61 20 27 5d 27 20 2a 2f 0d 0a 20 20   is a ']' */..  
418a0 20 20 69 66 28 20 71 75 6f 74 65 3d 3d 27 5b 27    if( quote=='['
418a1 20 29 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20   ) quote = ']'; 
418a2 20 0d 0a 0d 0a 20 20 20 20 77 68 69 6c 65 28 20   ....    while( 
418a3 41 4c 57 41 59 53 28 7a 5b 69 49 6e 5d 29 20 29  ALWAYS(z[iIn]) )
418a4 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69  {..      if( z[i
418a5 49 6e 5d 3d 3d 71 75 6f 74 65 20 29 7b 0d 0a 20  In]==quote ){.. 
418a6 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69 49 6e         if( z[iIn
418a7 2b 31 5d 21 3d 71 75 6f 74 65 20 29 20 62 72 65  +1]!=quote ) bre
418a8 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 7a 5b 69  ak;..        z[i
418a9 4f 75 74 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0d  Out++] = quote;.
418aa 0a 20 20 20 20 20 20 20 20 69 49 6e 20 2b 3d 20  .        iIn += 
418ab 32 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  2;..      }else{
418ac 0d 0a 20 20 20 20 20 20 20 20 7a 5b 69 4f 75 74  ..        z[iOut
418ad 2b 2b 5d 20 3d 20 7a 5b 69 49 6e 2b 2b 5d 3b 0d  ++] = z[iIn++];.
418ae 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
418af 0a 20 20 20 20 7a 5b 69 4f 75 74 5d 20 3d 20 27  .    z[iOut] = '
418b0 5c 30 27 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  \0';..  }..}....
418b1 2f 2a 0d 0a 2a 2a 20 52 65 61 64 20 61 20 73 69  /*..** Read a si
418b2 6e 67 6c 65 20 76 61 72 69 6e 74 20 66 72 6f 6d  ngle varint from
418b3 20 74 68 65 20 64 6f 63 6c 69 73 74 20 61 74 20   the doclist at 
418b4 2a 70 70 20 61 6e 64 20 61 64 76 61 6e 63 65 20  *pp and advance 
418b5 2a 70 70 20 74 6f 20 70 6f 69 6e 74 0d 0a 2a 2a  *pp to point..**
418b6 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79   to the first by
418b7 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
418b8 6f 66 20 74 68 65 20 76 61 72 69 6e 74 2e 20 20  of the varint.  
418b9 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Add the value of
418ba 20 74 68 65 20 76 61 72 69 6e 74 0d 0a 2a 2a 20   the varint..** 
418bb 74 6f 20 2a 70 56 61 6c 2e 0d 0a 2a 2f 0d 0a 73  to *pVal...*/..s
418bc 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 47  tatic void fts3G
418bd 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 63 68  etDeltaVarint(ch
418be 61 72 20 2a 2a 70 70 2c 20 73 71 6c 69 74 65 33  ar **pp, sqlite3
418bf 5f 69 6e 74 36 34 20 2a 70 56 61 6c 29 7b 0d 0a  _int64 *pVal){..
418c0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
418c1 69 56 61 6c 3b 0d 0a 20 20 2a 70 70 20 2b 3d 20  iVal;..  *pp += 
418c2 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
418c3 72 69 6e 74 28 2a 70 70 2c 20 26 69 56 61 6c 29  rint(*pp, &iVal)
418c4 3b 0d 0a 20 20 2a 70 56 61 6c 20 2b 3d 20 69 56  ;..  *pVal += iV
418c5 61 6c 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  al;..}..../*..**
418c6 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   When this funct
418c7 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a  ion is called, *
418c8 70 70 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  pp points to the
418c9 20 66 69 72 73 74 20 62 79 74 65 20 66 6f 6c 6c   first byte foll
418ca 6f 77 69 6e 67 20 61 0d 0a 2a 2a 20 76 61 72 69  owing a..** vari
418cb 6e 74 20 74 68 61 74 20 69 73 20 70 61 72 74 20  nt that is part 
418cc 6f 66 20 61 20 64 6f 63 6c 69 73 74 20 28 6f 72  of a doclist (or
418cd 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2c 20   position-list, 
418ce 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6c 69 73  or any other lis
418cf 74 0d 0a 2a 2a 20 6f 66 20 76 61 72 69 6e 74 73  t..** of varints
418d0 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ). This function
418d1 20 6d 6f 76 65 73 20 2a 70 70 20 74 6f 20 70 6f   moves *pp to po
418d2 69 6e 74 20 74 6f 20 74 68 65 20 73 74 61 72 74  int to the start
418d3 20 6f 66 20 74 68 61 74 20 76 61 72 69 6e 74 2c   of that varint,
418d4 0d 0a 2a 2a 20 61 6e 64 20 73 65 74 73 20 2a 70  ..** and sets *p
418d5 56 61 6c 20 62 79 20 74 68 65 20 76 61 72 69 6e  Val by the varin
418d6 74 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a  t value...**..**
418d7 20 41 72 67 75 6d 65 6e 74 20 70 53 74 61 72 74   Argument pStart
418d8 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
418d9 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
418da 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20 74 68   doclist that th
418db 65 0d 0a 2a 2a 20 76 61 72 69 6e 74 20 69 73 20  e..** varint is 
418dc 70 61 72 74 20 6f 66 2e 0d 0a 2a 2f 0d 0a 73 74  part of...*/..st
418dd 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 47 65  atic void fts3Ge
418de 74 52 65 76 65 72 73 65 56 61 72 69 6e 74 28 0d  tReverseVarint(.
418df 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 0d 0a  .  char **pp, ..
418e0 20 20 63 68 61 72 20 2a 70 53 74 61 72 74 2c 20    char *pStart, 
418e1 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ..  sqlite3_int6
418e2 34 20 2a 70 56 61 6c 0d 0a 29 7b 0d 0a 20 20 73  4 *pVal..){..  s
418e3 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 56 61  qlite3_int64 iVa
418e4 6c 3b 0d 0a 20 20 63 68 61 72 20 2a 70 3b 0d 0a  l;..  char *p;..
418e5 0d 0a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 70  ..  /* Pointer p
418e6 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74   now points at t
418e7 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
418e8 73 74 20 74 68 65 20 76 61 72 69 6e 74 20 77 65  st the varint we
418e9 20 61 72 65 20 0d 0a 20 20 2a 2a 20 69 6e 74 65   are ..  ** inte
418ea 72 65 73 74 65 64 20 69 6e 2e 20 53 6f 2c 20 75  rested in. So, u
418eb 6e 6c 65 73 73 20 74 68 65 20 64 6f 63 6c 69 73  nless the doclis
418ec 74 20 69 73 20 63 6f 72 72 75 70 74 2c 20 74 68  t is corrupt, th
418ed 65 20 30 78 38 30 20 62 69 74 20 69 73 0d 0a 20  e 0x80 bit is.. 
418ee 20 2a 2a 20 63 6c 65 61 72 20 6f 6e 20 63 68 61   ** clear on cha
418ef 72 61 63 74 65 72 20 70 5b 2d 31 5d 2e 20 2a 2f  racter p[-1]. */
418f0 0d 0a 20 20 66 6f 72 28 70 20 3d 20 28 2a 70 70  ..  for(p = (*pp
418f1 29 2d 32 3b 20 70 3e 3d 70 53 74 61 72 74 20 26  )-2; p>=pStart &
418f2 26 20 2a 70 26 30 78 38 30 3b 20 70 2d 2d 29 3b  & *p&0x80; p--);
418f3 0d 0a 20 20 70 2b 2b 3b 0d 0a 20 20 2a 70 70 20  ..  p++;..  *pp 
418f4 3d 20 70 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  = p;....  sqlite
418f5 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
418f6 2c 20 26 69 56 61 6c 29 3b 0d 0a 20 20 2a 70 56  , &iVal);..  *pV
418f7 61 6c 20 3d 20 69 56 61 6c 3b 0d 0a 7d 0d 0a 0d  al = iVal;..}...
418f8 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 78 44 69 73  ./*..** The xDis
418f9 63 6f 6e 6e 65 63 74 28 29 20 76 69 72 74 75 61  connect() virtua
418fa 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0d  l table method..
418fb 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
418fc 66 74 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65  fts3DisconnectMe
418fd 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61  thod(sqlite3_vta
418fe 62 20 2a 70 56 74 61 62 29 7b 0d 0a 20 20 46 74  b *pVtab){..  Ft
418ff 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74  s3Table *p = (Ft
41900 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62 3b  s3Table *)pVtab;
41901 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20  ..  int i;....  
41902 61 73 73 65 72 74 28 20 70 2d 3e 6e 50 65 6e 64  assert( p->nPend
41903 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0d 0a 20  ingData==0 );.. 
41904 20 61 73 73 65 72 74 28 20 70 2d 3e 70 53 65 67   assert( p->pSeg
41905 6d 65 6e 74 73 3d 3d 30 20 29 3b 0d 0a 0d 0a 20  ments==0 );.... 
41906 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 70 72 65   /* Free any pre
41907 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
41908 20 68 65 6c 64 20 2a 2f 0d 0a 20 20 66 6f 72 28   held */..  for(
41909 69 3d 30 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72  i=0; i<SizeofArr
4190a 61 79 28 70 2d 3e 61 53 74 6d 74 29 3b 20 69 2b  ay(p->aStmt); i+
4190b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  +){..    sqlite3
4190c 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 61 53 74  _finalize(p->aSt
4190d 6d 74 5b 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20  mt[i]);..  }..  
4190e 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
4190f 7a 53 65 67 6d 65 6e 74 73 54 62 6c 29 3b 0d 0a  zSegmentsTbl);..
41910 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
41911 2d 3e 7a 52 65 61 64 45 78 70 72 6c 69 73 74 29  ->zReadExprlist)
41912 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
41913 65 28 70 2d 3e 7a 57 72 69 74 65 45 78 70 72 6c  e(p->zWriteExprl
41914 69 73 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ist);..  sqlite3
41915 5f 66 72 65 65 28 70 2d 3e 7a 43 6f 6e 74 65 6e  _free(p->zConten
41916 74 54 62 6c 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  tTbl);....  /* I
41917 6e 76 6f 6b 65 20 74 68 65 20 74 6f 6b 65 6e 69  nvoke the tokeni
41918 7a 65 72 20 64 65 73 74 72 75 63 74 6f 72 20 74  zer destructor t
41919 6f 20 66 72 65 65 20 74 68 65 20 74 6f 6b 65 6e  o free the token
4191a 69 7a 65 72 2e 20 2a 2f 0d 0a 20 20 70 2d 3e 70  izer. */..  p->p
4191b 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
4191c 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 70 2d 3e  le->xDestroy(p->
4191d 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0d 0a 0d 0a  pTokenizer);....
4191e 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
4191f 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
41920 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
41921 0d 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 6f  ..** Construct o
41922 6e 65 20 6f 72 20 6d 6f 72 65 20 53 51 4c 20 73  ne or more SQL s
41923 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74  tatements from t
41924 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
41925 20 67 69 76 65 6e 0d 0a 2a 2a 20 61 6e 64 20 74   given..** and t
41926 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 6f  hen evaluate tho
41927 73 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 54  se statements. T
41928 68 65 20 73 75 63 63 65 73 73 20 63 6f 64 65 20  he success code 
41929 69 73 20 77 72 69 74 74 65 6e 0d 0a 2a 2a 20 69  is written..** i
4192a 6e 74 6f 20 2a 70 52 63 2e 0d 0a 2a 2a 0d 0a 2a  nto *pRc...**..*
4192b 2a 20 49 66 20 2a 70 52 63 20 69 73 20 69 6e 69  * If *pRc is ini
4192c 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20  tially non-zero 
4192d 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
4192e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a  e is a no-op...*
4192f 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  /..static void f
41930 74 73 33 44 62 45 78 65 63 28 0d 0a 20 20 69 6e  ts3DbExec(..  in
41931 74 20 2a 70 52 63 2c 20 20 20 20 20 20 20 20 20  t *pRc,         
41932 20 20 20 20 20 2f 2a 20 53 75 63 63 65 73 73 20       /* Success 
41933 63 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  code */..  sqlit
41934 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
41935 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e    /* Database in
41936 20 77 68 69 63 68 20 74 6f 20 72 75 6e 20 53 51   which to run SQ
41937 4c 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  L */..  const ch
41938 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 20 20 2f  ar *zFormat,   /
41939 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  * Format string 
4193a 66 6f 72 20 53 51 4c 20 2a 2f 0d 0a 20 20 2e 2e  for SQL */..  ..
4193b 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
4193c 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
4193d 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 74 20  s to the format 
4193e 73 74 72 69 6e 67 20 2a 2f 0d 0a 29 7b 0d 0a 20  string */..){.. 
4193f 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a 20 20   va_list ap;..  
41940 63 68 61 72 20 2a 7a 53 71 6c 3b 0d 0a 20 20 69  char *zSql;..  i
41941 66 28 20 2a 70 52 63 20 29 20 72 65 74 75 72 6e  f( *pRc ) return
41942 3b 0d 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  ;..  va_start(ap
41943 2c 20 7a 46 6f 72 6d 61 74 29 3b 0d 0a 20 20 7a  , zFormat);..  z
41944 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d  Sql = sqlite3_vm
41945 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20  printf(zFormat, 
41946 61 70 29 3b 0d 0a 20 20 76 61 5f 65 6e 64 28 61  ap);..  va_end(a
41947 70 29 3b 0d 0a 20 20 69 66 28 20 7a 53 71 6c 3d  p);..  if( zSql=
41948 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70 52 63 20  =0 ){..    *pRc 
41949 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
4194a 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2a  .  }else{..    *
4194b 70 52 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78  pRc = sqlite3_ex
4194c 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  ec(db, zSql, 0, 
4194d 30 2c 20 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69  0, 0);..    sqli
4194e 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d  te3_free(zSql);.
4194f 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
41950 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29  * The xDestroy()
41951 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
41952 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ethod...*/..stat
41953 69 63 20 69 6e 74 20 66 74 73 33 44 65 73 74 72  ic int fts3Destr
41954 6f 79 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  oyMethod(sqlite3
41955 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0d 0a  _vtab *pVtab){..
41956 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d    Fts3Table *p =
41957 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56   (Fts3Table *)pV
41958 74 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  tab;..  int rc =
41959 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
4195a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
4195b 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 63 6f  rn code */..  co
4195c 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
4195d 70 2d 3e 7a 44 62 3b 20 20 20 20 20 20 20 20 2f  p->zDb;        /
4195e 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
4195f 73 65 20 28 65 2e 67 2e 20 22 6d 61 69 6e 22 2c  se (e.g. "main",
41960 20 22 74 65 6d 70 22 29 20 2a 2f 0d 0a 20 20 73   "temp") */..  s
41961 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
41962 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
41963 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
41964 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 44 72  le */....  /* Dr
41965 6f 70 20 74 68 65 20 73 68 61 64 6f 77 20 74 61  op the shadow ta
41966 62 6c 65 73 20 2a 2f 0d 0a 20 20 69 66 28 20 70  bles */..  if( p
41967 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d 3d 30  ->zContentTbl==0
41968 20 29 7b 0d 0a 20 20 20 20 66 74 73 33 44 62 45   ){..    fts3DbE
41969 78 65 63 28 26 72 63 2c 20 64 62 2c 20 22 44 52  xec(&rc, db, "DR
4196a 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49 53  OP TABLE IF EXIS
4196b 54 53 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e  TS %Q.'%q_conten
4196c 74 27 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e 61  t'", zDb, p->zNa
4196d 6d 65 29 3b 0d 0a 20 20 7d 0d 0a 20 20 66 74 73  me);..  }..  fts
4196e 33 44 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c  3DbExec(&rc, db,
4196f 20 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20   "DROP TABLE IF 
41970 45 58 49 53 54 53 20 25 51 2e 27 25 71 5f 73 65  EXISTS %Q.'%q_se
41971 67 6d 65 6e 74 73 27 22 2c 20 7a 44 62 2c 70 2d  gments'", zDb,p-
41972 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 66 74 73 33  >zName);..  fts3
41973 44 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20  DbExec(&rc, db, 
41974 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45  "DROP TABLE IF E
41975 58 49 53 54 53 20 25 51 2e 27 25 71 5f 73 65 67  XISTS %Q.'%q_seg
41976 64 69 72 27 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a  dir'", zDb, p->z
41977 4e 61 6d 65 29 3b 0d 0a 20 20 66 74 73 33 44 62  Name);..  fts3Db
41978 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20 22 44  Exec(&rc, db, "D
41979 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49  ROP TABLE IF EXI
4197a 53 54 53 20 25 51 2e 27 25 71 5f 64 6f 63 73 69  STS %Q.'%q_docsi
4197b 7a 65 27 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e  ze'", zDb, p->zN
4197c 61 6d 65 29 3b 0d 0a 20 20 66 74 73 33 44 62 45  ame);..  fts3DbE
4197d 78 65 63 28 26 72 63 2c 20 64 62 2c 20 22 44 52  xec(&rc, db, "DR
4197e 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49 53  OP TABLE IF EXIS
4197f 54 53 20 25 51 2e 27 25 71 5f 73 74 61 74 27 22  TS %Q.'%q_stat'"
41980 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 29  , zDb, p->zName)
41981 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 65 76 65  ;....  /* If eve
41982 72 79 74 68 69 6e 67 20 68 61 73 20 77 6f 72 6b  rything has work
41983 65 64 2c 20 69 6e 76 6f 6b 65 20 66 74 73 33 44  ed, invoke fts3D
41984 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28  isconnectMethod(
41985 29 20 74 6f 20 66 72 65 65 20 74 68 65 0d 0a 20  ) to free the.. 
41986 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   ** memory assoc
41987 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 46  iated with the F
41988 74 73 33 54 61 62 6c 65 20 73 74 72 75 63 74 75  ts3Table structu
41989 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  re and return SQ
4198a 4c 49 54 45 5f 4f 4b 2e 0d 0a 20 20 2a 2a 20 4f  LITE_OK...  ** O
4198b 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
4198c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
4198d 20 63 6f 64 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   code...  */..  
4198e 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49  return (rc==SQLI
4198f 54 45 5f 4f 4b 20 3f 20 66 74 73 33 44 69 73 63  TE_OK ? fts3Disc
41990 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28 70 56 74  onnectMethod(pVt
41991 61 62 29 20 3a 20 72 63 29 3b 0d 0a 7d 0d 0a 0d  ab) : rc);..}...
41992 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 76 6f 6b 65  .../*..** Invoke
41993 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
41994 5f 76 74 61 62 28 29 20 74 6f 20 64 65 63 6c 61  _vtab() to decla
41995 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f  re the schema fo
41996 72 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65  r the FTS3 table
41997 0d 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74  ..** passed as t
41998 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
41999 74 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  t. This is done 
4199a 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 78  as part of the x
4199b 43 6f 6e 6e 65 63 74 28 29 0d 0a 2a 2a 20 61 6e  Connect()..** an
4199c 64 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68  d xCreate() meth
4199d 6f 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ods...**..** If 
4199e 2a 70 52 63 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  *pRc is non-zero
4199f 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
419a0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69  ion is called, i
419a1 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 0d 0a  t is a no-op. ..
419a2 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
419a3 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
419a4 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  , an SQLite erro
419a5 72 20 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64  r code is stored
419a6 20 69 6e 20 2a 70 52 63 0d 0a 2a 2a 20 62 65 66   in *pRc..** bef
419a7 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a  ore returning...
419a8 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
419a9 66 74 73 33 44 65 63 6c 61 72 65 56 74 61 62 28  fts3DeclareVtab(
419aa 69 6e 74 20 2a 70 52 63 2c 20 46 74 73 33 54 61  int *pRc, Fts3Ta
419ab 62 6c 65 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  ble *p){..  if( 
419ac 2a 70 52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  *pRc==SQLITE_OK 
419ad 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 3b 20 20  ){..    int i;  
419ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
419af 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
419b0 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20  r variable */.. 
419b1 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20     int rc;      
419b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
419b3 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
419b4 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  */..    char *zS
419b5 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ql;             
419b6 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
419b7 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  tement passed to
419b8 20 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20   declare_vtab() 
419b9 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 43  */..    char *zC
419ba 6f 6c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ols;            
419bb 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
419bc 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 63 6f   user defined co
419bd 6c 75 6d 6e 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20  lumns */....    
419be 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e  sqlite3_vtab_con
419bf 66 69 67 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  fig(p->db, SQLIT
419c0 45 5f 56 54 41 42 5f 43 4f 4e 53 54 52 41 49 4e  E_VTAB_CONSTRAIN
419c1 54 5f 53 55 50 50 4f 52 54 2c 20 31 29 3b 0d 0a  T_SUPPORT, 1);..
419c2 0d 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
419c3 61 20 6c 69 73 74 20 6f 66 20 75 73 65 72 20 63  a list of user c
419c4 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 76  olumns for the v
419c5 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0d  irtual table */.
419c6 0a 20 20 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c  .    zCols = sql
419c7 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 51  ite3_mprintf("%Q
419c8 2c 20 22 2c 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e  , ", p->azColumn
419c9 5b 30 5d 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69  [0]);..    for(i
419ca 3d 31 3b 20 7a 43 6f 6c 73 20 26 26 20 69 3c 70  =1; zCols && i<p
419cb 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
419cc 0d 0a 20 20 20 20 20 20 7a 43 6f 6c 73 20 3d 20  ..      zCols = 
419cd 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
419ce 22 25 7a 25 51 2c 20 22 2c 20 7a 43 6f 6c 73 2c  "%z%Q, ", zCols,
419cf 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29   p->azColumn[i])
419d0 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
419d1 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 77 68  /* Create the wh
419d2 6f 6c 65 20 22 43 52 45 41 54 45 20 54 41 42 4c  ole "CREATE TABL
419d3 45 22 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  E" statement to 
419d4 70 61 73 73 20 74 6f 20 53 51 4c 69 74 65 20 2a  pass to SQLite *
419d5 2f 0d 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71  /..    zSql = sq
419d6 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a  lite3_mprintf(..
419d7 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20          "CREATE 
419d8 54 41 42 4c 45 20 78 28 25 73 20 25 51 20 48 49  TABLE x(%s %Q HI
419d9 44 44 45 4e 2c 20 64 6f 63 69 64 20 48 49 44 44  DDEN, docid HIDD
419da 45 4e 29 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d 3e  EN)", zCols, p->
419db 7a 4e 61 6d 65 0d 0a 20 20 20 20 29 3b 0d 0a 20  zName..    );.. 
419dc 20 20 20 69 66 28 20 21 7a 43 6f 6c 73 20 7c 7c     if( !zCols ||
419dd 20 21 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20   !zSql ){..     
419de 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
419df 45 4d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  EM;..    }else{.
419e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
419e1 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
419e2 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 29 3b 0d 0a  (p->db, zSql);..
419e3 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71 6c      }....    sql
419e4 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
419e5 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
419e6 65 65 28 7a 43 6f 6c 73 29 3b 0d 0a 20 20 20 20  ee(zCols);..    
419e7 2a 70 52 63 20 3d 20 72 63 3b 0d 0a 20 20 7d 0d  *pRc = rc;..  }.
419e8 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 72 65  .}..../*..** Cre
419e9 61 74 65 20 74 68 65 20 62 61 63 6b 69 6e 67 20  ate the backing 
419ea 73 74 6f 72 65 20 74 61 62 6c 65 73 20 28 25 5f  store tables (%_
419eb 63 6f 6e 74 65 6e 74 2c 20 25 5f 73 65 67 6d 65  content, %_segme
419ec 6e 74 73 20 61 6e 64 20 25 5f 73 65 67 64 69 72  nts and %_segdir
419ed 29 0d 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 62  )..** required b
419ee 79 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65  y the FTS3 table
419ef 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 6f   passed as the o
419f0 6e 6c 79 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  nly argument. Th
419f1 69 73 20 69 73 20 64 6f 6e 65 0d 0a 2a 2a 20 61  is is done..** a
419f2 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 76 74  s part of the vt
419f3 61 62 20 78 43 72 65 61 74 65 28 29 20 6d 65 74  ab xCreate() met
419f4 68 6f 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  hod...**..** If 
419f5 74 68 65 20 70 2d 3e 62 48 61 73 44 6f 63 73 69  the p->bHasDocsi
419f6 7a 65 20 62 6f 6f 6c 65 61 6e 20 69 73 20 74 72  ze boolean is tr
419f7 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74  ue (indicating t
419f8 68 61 74 20 74 68 69 73 20 69 73 20 61 6e 0d 0a  hat this is an..
419f9 2a 2a 20 46 54 53 34 20 74 61 62 6c 65 2c 20 6e  ** FTS4 table, n
419fa 6f 74 20 61 6e 20 46 54 53 33 20 74 61 62 6c 65  ot an FTS3 table
419fb 29 20 74 68 65 6e 20 61 6c 73 6f 20 63 72 65 61  ) then also crea
419fc 74 65 20 74 68 65 20 25 5f 64 6f 63 73 69 7a 65  te the %_docsize
419fd 20 61 6e 64 0d 0a 2a 2a 20 25 5f 73 74 61 74 20   and..** %_stat 
419fe 74 61 62 6c 65 73 20 72 65 71 75 69 72 65 64 20  tables required 
419ff 62 79 20 46 54 53 34 2e 0d 0a 2a 2f 0d 0a 73 74  by FTS4...*/..st
41a00 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 72 65  atic int fts3Cre
41a01 61 74 65 54 61 62 6c 65 73 28 46 74 73 33 54 61  ateTables(Fts3Ta
41a02 62 6c 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20  ble *p){..  int 
41a03 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
41a04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
41a05 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20  eturn code */.. 
41a06 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
41a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41a08 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
41a09 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 73 71 6c 69  iable */..  sqli
41a0a 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
41a0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
41a0c 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
41a0d 65 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20 69  ection */....  i
41a0e 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62  f( p->zContentTb
41a0f 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63 68 61  l==0 ){..    cha
41a10 72 20 2a 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 3b  r *zContentCols;
41a11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
41a12 6c 75 6d 6e 73 20 6f 66 20 25 5f 63 6f 6e 74 65  lumns of %_conte
41a13 6e 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20  nt table */.... 
41a14 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6c     /* Create a l
41a15 69 73 74 20 6f 66 20 75 73 65 72 20 63 6f 6c 75  ist of user colu
41a16 6d 6e 73 20 66 6f 72 20 74 68 65 20 63 6f 6e 74  mns for the cont
41a17 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  ent table */..  
41a18 20 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 20 3d    zContentCols =
41a19 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
41a1a 28 22 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20  ("docid INTEGER 
41a1b 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b 0d 0a  PRIMARY KEY");..
41a1c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 43 6f      for(i=0; zCo
41a1d 6e 74 65 6e 74 43 6f 6c 73 20 26 26 20 69 3c 70  ntentCols && i<p
41a1e 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
41a1f 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20  ..      char *z 
41a20 3d 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d  = p->azColumn[i]
41a21 3b 0d 0a 20 20 20 20 20 20 7a 43 6f 6e 74 65 6e  ;..      zConten
41a22 74 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f  tCols = sqlite3_
41a23 6d 70 72 69 6e 74 66 28 22 25 7a 2c 20 27 63 25  mprintf("%z, 'c%
41a24 64 25 71 27 22 2c 20 7a 43 6f 6e 74 65 6e 74 43  d%q'", zContentC
41a25 6f 6c 73 2c 20 69 2c 20 7a 29 3b 0d 0a 20 20 20  ols, i, z);..   
41a26 20 7d 0d 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e   }..    if( zCon
41a27 74 65 6e 74 43 6f 6c 73 3d 3d 30 20 29 20 72 63  tentCols==0 ) rc
41a28 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
41a29 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20 43 72 65  ..  ..    /* Cre
41a2a 61 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ate the content 
41a2b 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20 66 74  table */..    ft
41a2c 73 33 44 62 45 78 65 63 28 26 72 63 2c 20 64 62  s3DbExec(&rc, db
41a2d 2c 20 0d 0a 20 20 20 20 20 20 20 22 43 52 45 41  , ..       "CREA
41a2e 54 45 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f  TE TABLE %Q.'%q_
41a2f 63 6f 6e 74 65 6e 74 27 28 25 73 29 22 2c 0d 0a  content'(%s)",..
41a30 20 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70         p->zDb, p
41a31 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6e 74 65 6e  ->zName, zConten
41a32 74 43 6f 6c 73 0d 0a 20 20 20 20 29 3b 0d 0a 20  tCols..    );.. 
41a33 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
41a34 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 29 3b 0d 0a  zContentCols);..
41a35 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 72 65 61    }....  /* Crea
41a36 74 65 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20  te other tables 
41a37 2a 2f 0d 0a 20 20 66 74 73 33 44 62 45 78 65 63  */..  fts3DbExec
41a38 28 26 72 63 2c 20 64 62 2c 20 0d 0a 20 20 20 20  (&rc, db, ..    
41a39 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20    "CREATE TABLE 
41a3a 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27  %Q.'%q_segments'
41a3b 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52  (blockid INTEGER
41a3c 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c   PRIMARY KEY, bl
41a3d 6f 63 6b 20 42 4c 4f 42 29 3b 22 2c 0d 0a 20 20  ock BLOB);",..  
41a3e 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a      p->zDb, p->z
41a3f 4e 61 6d 65 0d 0a 20 20 29 3b 0d 0a 20 20 66 74  Name..  );..  ft
41a40 73 33 44 62 45 78 65 63 28 26 72 63 2c 20 64 62  s3DbExec(&rc, db
41a41 2c 20 0d 0a 20 20 20 20 20 20 22 43 52 45 41 54  , ..      "CREAT
41a42 45 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73  E TABLE %Q.'%q_s
41a43 65 67 64 69 72 27 28 22 0d 0a 20 20 20 20 20 20  egdir'("..      
41a44 20 20 22 6c 65 76 65 6c 20 49 4e 54 45 47 45 52    "level INTEGER
41a45 2c 22 0d 0a 20 20 20 20 20 20 20 20 22 69 64 78  ,"..        "idx
41a46 20 49 4e 54 45 47 45 52 2c 22 0d 0a 20 20 20 20   INTEGER,"..    
41a47 20 20 20 20 22 73 74 61 72 74 5f 62 6c 6f 63 6b      "start_block
41a48 20 49 4e 54 45 47 45 52 2c 22 0d 0a 20 20 20 20   INTEGER,"..    
41a49 20 20 20 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f      "leaves_end_
41a4a 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0d  block INTEGER,".
41a4b 0a 20 20 20 20 20 20 20 20 22 65 6e 64 5f 62 6c  .        "end_bl
41a4c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0d 0a 20  ock INTEGER,".. 
41a4d 20 20 20 20 20 20 20 22 72 6f 6f 74 20 42 4c 4f         "root BLO
41a4e 42 2c 22 0d 0a 20 20 20 20 20 20 20 20 22 50 52  B,"..        "PR
41a4f 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 65 6c 2c  IMARY KEY(level,
41a50 20 69 64 78 29 22 0d 0a 20 20 20 20 20 20 22 29   idx)"..      ")
41a51 3b 22 2c 0d 0a 20 20 20 20 20 20 70 2d 3e 7a 44  ;",..      p->zD
41a52 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0d 0a 20 20 29  b, p->zName..  )
41a53 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 62 48 61 73  ;..  if( p->bHas
41a54 44 6f 63 73 69 7a 65 20 29 7b 0d 0a 20 20 20 20  Docsize ){..    
41a55 66 74 73 33 44 62 45 78 65 63 28 26 72 63 2c 20  fts3DbExec(&rc, 
41a56 64 62 2c 20 0d 0a 20 20 20 20 20 20 20 20 22 43  db, ..        "C
41a57 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 27  REATE TABLE %Q.'
41a58 25 71 5f 64 6f 63 73 69 7a 65 27 28 64 6f 63 69  %q_docsize'(doci
41a59 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
41a5a 59 20 4b 45 59 2c 20 73 69 7a 65 20 42 4c 4f 42  Y KEY, size BLOB
41a5b 29 3b 22 2c 0d 0a 20 20 20 20 20 20 20 20 70 2d  );",..        p-
41a5c 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0d 0a  >zDb, p->zName..
41a5d 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69      );..  }..  i
41a5e 66 28 20 70 2d 3e 62 48 61 73 53 74 61 74 20 29  f( p->bHasStat )
41a5f 7b 0d 0a 20 20 20 20 66 74 73 33 44 62 45 78 65  {..    fts3DbExe
41a60 63 28 26 72 63 2c 20 64 62 2c 20 0d 0a 20 20 20  c(&rc, db, ..   
41a61 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
41a62 4c 45 20 25 51 2e 27 25 71 5f 73 74 61 74 27 28  LE %Q.'%q_stat'(
41a63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
41a64 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 4c  RY KEY, value BL
41a65 4f 42 29 3b 22 2c 0d 0a 20 20 20 20 20 20 20 20  OB);",..        
41a66 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  p->zDb, p->zName
41a67 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 20  ..    );..  }.. 
41a68 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
41a69 0d 0a 2f 2a 0d 0a 2a 2a 20 53 74 6f 72 65 20 74  ../*..** Store t
41a6a 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
41a6b 61 73 65 20 70 61 67 65 2d 73 69 7a 65 20 69 6e  ase page-size in
41a6c 20 62 79 74 65 73 20 69 6e 20 70 2d 3e 6e 50 67   bytes in p->nPg
41a6d 73 7a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 2a  sz...**..** If *
41a6e 70 52 63 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20  pRc is non-zero 
41a6f 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
41a70 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74  on is called, it
41a71 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 0d 0a 2a   is a no-op. ..*
41a72 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
41a73 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
41a74 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
41a75 20 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20   code is stored 
41a76 69 6e 20 2a 70 52 63 0d 0a 2a 2a 20 62 65 66 6f  in *pRc..** befo
41a77 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a  re returning...*
41a78 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  /..static void f
41a79 74 73 33 44 61 74 61 62 61 73 65 50 61 67 65 53  ts3DatabasePageS
41a7a 69 7a 65 28 69 6e 74 20 2a 70 52 63 2c 20 46 74  ize(int *pRc, Ft
41a7b 73 33 54 61 62 6c 65 20 2a 70 29 7b 0d 0a 20 20  s3Table *p){..  
41a7c 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45  if( *pRc==SQLITE
41a7d 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  _OK ){..    int 
41a7e 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
41a7f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
41a80 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 20  urn code */..   
41a81 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20   char *zSql;    
41a82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41a83 2a 20 53 51 4c 20 74 65 78 74 20 22 50 52 41 47  * SQL text "PRAG
41a84 4d 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22  MA %Q.page_size"
41a85 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   */..    sqlite3
41a86 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20  _stmt *pStmt;   
41a87 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 69 6c         /* Compil
41a88 65 64 20 22 50 52 41 47 4d 41 20 25 51 2e 70 61  ed "PRAGMA %Q.pa
41a89 67 65 5f 73 69 7a 65 22 20 73 74 61 74 65 6d 65  ge_size" stateme
41a8a 6e 74 20 2a 2f 0d 0a 20 20 0d 0a 20 20 20 20 7a  nt */..  ..    z
41a8b 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
41a8c 72 69 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51  rintf("PRAGMA %Q
41a8d 2e 70 61 67 65 5f 73 69 7a 65 22 2c 20 70 2d 3e  .page_size", p->
41a8e 7a 44 62 29 3b 0d 0a 20 20 20 20 69 66 28 20 21  zDb);..    if( !
41a8f 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20 20 72  zSql ){..      r
41a90 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
41a91 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
41a92 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
41a93 33 5f 70 72 65 70 61 72 65 28 70 2d 3e 64 62 2c  3_prepare(p->db,
41a94 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
41a95 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 69 66  t, 0);..      if
41a96 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
41a97 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){..        sqli
41a98 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
41a99 0d 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 50 67  ..        p->nPg
41a9a 73 7a 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  sz = sqlite3_col
41a9b 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30  umn_int(pStmt, 0
41a9c 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  );..        rc =
41a9d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
41a9e 65 28 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 20  e(pStmt);..     
41a9f 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
41aa0 51 4c 49 54 45 5f 41 55 54 48 20 29 7b 0d 0a 20  QLITE_AUTH ){.. 
41aa1 20 20 20 20 20 20 20 70 2d 3e 6e 50 67 73 7a 20         p->nPgsz 
41aa2 3d 20 31 30 32 34 3b 0d 0a 20 20 20 20 20 20 20  = 1024;..       
41aa3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
41aa4 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
41aa5 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
41aa6 3e 6e 50 67 73 7a 3e 30 20 7c 7c 20 72 63 21 3d  >nPgsz>0 || rc!=
41aa7 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20  SQLITE_OK );..  
41aa8 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
41aa9 53 71 6c 29 3b 0d 0a 20 20 20 20 2a 70 52 63 20  Sql);..    *pRc 
41aaa 3d 20 72 63 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  = rc;..  }..}...
41aab 0a 2f 2a 0d 0a 2a 2a 20 22 53 70 65 63 69 61 6c  ./*..** "Special
41aac 22 20 46 54 53 34 20 61 72 67 75 6d 65 6e 74 73  " FTS4 arguments
41aad 20 61 72 65 20 63 6f 6c 75 6d 6e 20 73 70 65 63   are column spec
41aae 69 66 69 63 61 74 69 6f 6e 73 20 6f 66 20 74 68  ifications of th
41aaf 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
41ab0 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 3c 6b 65 79  :..**..**   <key
41ab1 3e 20 3d 20 3c 76 61 6c 75 65 3e 0d 0a 2a 2a 0d  > = <value>..**.
41ab2 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 6e 6f  .** There may no
41ab3 74 20 62 65 20 77 68 69 74 65 73 70 61 63 65 20  t be whitespace 
41ab4 73 75 72 72 6f 75 6e 64 69 6e 67 20 74 68 65 20  surrounding the 
41ab5 22 3d 22 20 63 68 61 72 61 63 74 65 72 2e 20 54  "=" character. T
41ab6 68 65 20 3c 76 61 6c 75 65 3e 20 0d 0a 2a 2a 20  he <value> ..** 
41ab7 74 65 72 6d 20 6d 61 79 20 62 65 20 71 75 6f 74  term may be quot
41ab8 65 64 2c 20 62 75 74 20 74 68 65 20 3c 6b 65 79  ed, but the <key
41ab9 3e 20 6d 61 79 20 6e 6f 74 2e 0d 0a 2a 2f 0d 0a  > may not...*/..
41aba 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49  static int fts3I
41abb 73 53 70 65 63 69 61 6c 43 6f 6c 75 6d 6e 28 0d  sSpecialColumn(.
41abc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
41abd 2c 20 0d 0a 20 20 69 6e 74 20 2a 70 6e 4b 65 79  , ..  int *pnKey
41abe 2c 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 56 61  ,..  char **pzVa
41abf 6c 75 65 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20  lue..){..  char 
41ac0 2a 7a 56 61 6c 75 65 3b 0d 0a 20 20 63 6f 6e 73  *zValue;..  cons
41ac1 74 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a  t char *zCsr = z
41ac2 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65 28 20 2a 7a  ;....  while( *z
41ac3 43 73 72 21 3d 27 3d 27 20 29 7b 0d 0a 20 20 20  Csr!='=' ){..   
41ac4 20 69 66 28 20 2a 7a 43 73 72 3d 3d 27 5c 30 27   if( *zCsr=='\0'
41ac5 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20   ) return 0;..  
41ac6 20 20 7a 43 73 72 2b 2b 3b 0d 0a 20 20 7d 0d 0a    zCsr++;..  }..
41ac7 0d 0a 20 20 2a 70 6e 4b 65 79 20 3d 20 28 69 6e  ..  *pnKey = (in
41ac8 74 29 28 7a 43 73 72 2d 7a 29 3b 0d 0a 20 20 7a  t)(zCsr-z);..  z
41ac9 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  Value = sqlite3_
41aca 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 26 7a  mprintf("%s", &z
41acb 43 73 72 5b 31 5d 29 3b 0d 0a 20 20 69 66 28 20  Csr[1]);..  if( 
41acc 7a 56 61 6c 75 65 20 29 7b 0d 0a 20 20 20 20 73  zValue ){..    s
41acd 71 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74  qlite3Fts3Dequot
41ace 65 28 7a 56 61 6c 75 65 29 3b 0d 0a 20 20 7d 0d  e(zValue);..  }.
41acf 0a 20 20 2a 70 7a 56 61 6c 75 65 20 3d 20 7a 56  .  *pzValue = zV
41ad0 61 6c 75 65 3b 0d 0a 20 20 72 65 74 75 72 6e 20  alue;..  return 
41ad1 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  1;..}..../*..** 
41ad2 41 70 70 65 6e 64 20 74 68 65 20 6f 75 74 70 75  Append the outpu
41ad3 74 20 6f 66 20 61 20 70 72 69 6e 74 66 28 29 20  t of a printf() 
41ad4 73 74 79 6c 65 20 66 6f 72 6d 61 74 74 69 6e 67  style formatting
41ad5 20 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20   to an existing 
41ad6 73 74 72 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61  string...*/..sta
41ad7 74 69 63 20 76 6f 69 64 20 66 74 73 33 41 70 70  tic void fts3App
41ad8 65 6e 64 66 28 0d 0a 20 20 69 6e 74 20 2a 70 52  endf(..  int *pR
41ad9 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
41ada 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
41adb 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a  UT: Error code *
41adc 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 2c 20  /..  char **pz, 
41add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41ade 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
41adf 50 6f 69 6e 74 65 72 20 74 6f 20 73 74 72 69 6e  Pointer to strin
41ae0 67 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 63  g buffer */..  c
41ae1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
41ae2 61 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  at,            /
41ae3 2a 20 50 72 69 6e 74 66 20 66 6f 72 6d 61 74 20  * Printf format 
41ae4 73 74 72 69 6e 67 20 74 6f 20 61 70 70 65 6e 64  string to append
41ae5 20 2a 2f 0d 0a 20 20 2e 2e 2e 20 20 20 20 20 20   */..  ...      
41ae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41ae7 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
41ae8 6e 74 73 20 66 6f 72 20 70 72 69 6e 74 66 20 66  nts for printf f
41ae9 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0d  ormat string */.
41aea 0a 29 7b 0d 0a 20 20 69 66 28 20 2a 70 52 63 3d  .){..  if( *pRc=
41aeb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
41aec 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0d 0a     va_list ap;..
41aed 20 20 20 20 63 68 61 72 20 2a 7a 3b 0d 0a 20 20      char *z;..  
41aee 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
41aef 46 6f 72 6d 61 74 29 3b 0d 0a 20 20 20 20 7a 20  Format);..    z 
41af0 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e  = sqlite3_vmprin
41af1 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  tf(zFormat, ap);
41af2 0d 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29  ..    va_end(ap)
41af3 3b 0d 0a 20 20 20 20 69 66 28 20 7a 20 26 26 20  ;..    if( z && 
41af4 2a 70 7a 20 29 7b 0d 0a 20 20 20 20 20 20 63 68  *pz ){..      ch
41af5 61 72 20 2a 7a 32 20 3d 20 73 71 6c 69 74 65 33  ar *z2 = sqlite3
41af6 5f 6d 70 72 69 6e 74 66 28 22 25 73 25 73 22 2c  _mprintf("%s%s",
41af7 20 2a 70 7a 2c 20 7a 29 3b 0d 0a 20 20 20 20 20   *pz, z);..     
41af8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 29   sqlite3_free(z)
41af9 3b 0d 0a 20 20 20 20 20 20 7a 20 3d 20 7a 32 3b  ;..      z = z2;
41afa 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
41afb 20 7a 3d 3d 30 20 29 20 2a 70 52 63 20 3d 20 53   z==0 ) *pRc = S
41afc 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
41afd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 2a    sqlite3_free(*
41afe 70 7a 29 3b 0d 0a 20 20 20 20 2a 70 7a 20 3d 20  pz);..    *pz = 
41aff 7a 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  z;..  }..}..../*
41b00 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 63 6f  ..** Return a co
41b01 70 79 20 6f 66 20 69 6e 70 75 74 20 73 74 72 69  py of input stri
41b02 6e 67 20 7a 49 6e 70 75 74 20 65 6e 63 6c 6f 73  ng zInput enclos
41b03 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f  ed in double-quo
41b04 74 65 73 20 28 22 29 20 61 6e 64 0d 0a 2a 2a 20  tes (") and..** 
41b05 77 69 74 68 20 61 6c 6c 20 64 6f 75 62 6c 65 20  with all double 
41b06 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73  quote characters
41b07 20 65 73 63 61 70 65 64 2e 20 46 6f 72 20 65 78   escaped. For ex
41b08 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ample:..**..**  
41b09 20 20 20 66 74 73 33 51 75 6f 74 65 49 64 28 22     fts3QuoteId("
41b0a 75 6e 20 5c 22 7a 69 70 5c 22 22 29 20 20 20 2d  un \"zip\"")   -
41b0b 3e 20 20 20 20 22 75 6e 20 5c 22 5c 22 7a 69 70  >    "un \"\"zip
41b0c 5c 22 5c 22 22 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  \"\""..**..** Th
41b0d 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
41b0e 65 64 20 70 6f 69 6e 74 73 20 74 6f 20 6d 65 6d  ed points to mem
41b0f 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
41b10 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
41b11 28 29 2e 20 49 74 0d 0a 2a 2a 20 69 73 20 74 68  (). It..** is th
41b12 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e  e callers respon
41b13 73 69 62 69 6c 69 74 79 20 74 6f 20 63 61 6c 6c  sibility to call
41b14 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20   sqlite3_free() 
41b15 74 6f 20 72 65 6c 65 61 73 65 20 74 68 69 73 0d  to release this.
41b16 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d  .** memory...*/.
41b17 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 74  .static char *ft
41b18 73 33 51 75 6f 74 65 49 64 28 63 68 61 72 20 63  s3QuoteId(char c
41b19 6f 6e 73 74 20 2a 7a 49 6e 70 75 74 29 7b 0d 0a  onst *zInput){..
41b1a 20 20 69 6e 74 20 6e 52 65 74 3b 0d 0a 20 20 63    int nRet;..  c
41b1b 68 61 72 20 2a 7a 52 65 74 3b 0d 0a 20 20 6e 52  har *zRet;..  nR
41b1c 65 74 20 3d 20 32 20 2b 20 73 74 72 6c 65 6e 28  et = 2 + strlen(
41b1d 7a 49 6e 70 75 74 29 2a 32 20 2b 20 31 3b 0d 0a  zInput)*2 + 1;..
41b1e 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33    zRet = sqlite3
41b1f 5f 6d 61 6c 6c 6f 63 28 6e 52 65 74 29 3b 0d 0a  _malloc(nRet);..
41b20 20 20 69 66 28 20 7a 52 65 74 20 29 7b 0d 0a 20    if( zRet ){.. 
41b21 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 63     int i;..    c
41b22 68 61 72 20 2a 7a 20 3d 20 7a 52 65 74 3b 0d 0a  har *z = zRet;..
41b23 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 27 22 27      *(z++) = '"'
41b24 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
41b25 7a 49 6e 70 75 74 5b 69 5d 3b 20 69 2b 2b 29 7b  zInput[i]; i++){
41b26 0d 0a 20 20 20 20 20 20 69 66 28 20 7a 49 6e 70  ..      if( zInp
41b27 75 74 5b 69 5d 3d 3d 27 22 27 20 29 20 2a 28 7a  ut[i]=='"' ) *(z
41b28 2b 2b 29 20 3d 20 27 22 27 3b 0d 0a 20 20 20 20  ++) = '"';..    
41b29 20 20 2a 28 7a 2b 2b 29 20 3d 20 7a 49 6e 70 75    *(z++) = zInpu
41b2a 74 5b 69 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  t[i];..    }..  
41b2b 20 20 2a 28 7a 2b 2b 29 20 3d 20 27 22 27 3b 0d    *(z++) = '"';.
41b2c 0a 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 27 5c  .    *(z++) = '\
41b2d 30 27 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  0';..  }..  retu
41b2e 72 6e 20 7a 52 65 74 3b 0d 0a 7d 0d 0a 0d 0a 2f  rn zRet;..}..../
41b2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c  *..** Return a l
41b30 69 73 74 20 6f 66 20 63 6f 6d 6d 61 20 73 65 70  ist of comma sep
41b31 61 72 61 74 65 64 20 53 51 4c 20 65 78 70 72 65  arated SQL expre
41b32 73 73 69 6f 6e 73 20 61 6e 64 20 61 20 46 52 4f  ssions and a FRO
41b33 4d 20 63 6c 61 75 73 65 20 74 68 61 74 20 0d 0a  M clause that ..
41b34 2a 2a 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64  ** could be used
41b35 20 69 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61   in a SELECT sta
41b36 74 65 6d 65 6e 74 20 73 75 63 68 20 61 73 20 74  tement such as t
41b37 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 2a  he following:..*
41b38 2a 0d 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  *..**     SELECT
41b39 20 3c 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73   <list of expres
41b3a 73 69 6f 6e 73 3e 20 46 52 4f 4d 20 25 5f 63 6f  sions> FROM %_co
41b3b 6e 74 65 6e 74 20 41 53 20 78 20 2e 2e 2e 0d 0a  ntent AS x .....
41b3c 2a 2a 0d 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e  **..** to return
41b3d 20 74 68 65 20 64 6f 63 69 64 2c 20 66 6f 6c 6c   the docid, foll
41b3e 6f 77 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  owed by each col
41b3f 75 6d 6e 20 6f 66 20 74 65 78 74 20 64 61 74 61  umn of text data
41b40 20 69 6e 20 6f 72 64 65 72 0d 0a 2a 2a 20 66 72   in order..** fr
41b41 6f 6d 20 6c 65 66 74 20 74 6f 20 77 72 69 74 65  om left to write
41b42 2e 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 7a  . If parameter z
41b43 46 75 6e 63 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Func is not NULL
41b44 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f  , then instead o
41b45 66 0d 0a 2a 2a 20 62 65 69 6e 67 20 72 65 74 75  f..** being retu
41b46 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20 65 61  rned directly ea
41b47 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 65 78  ch column of tex
41b48 74 20 64 61 74 61 20 69 73 20 70 61 73 73 65 64  t data is passed
41b49 20 74 6f 20 61 6e 20 53 51 4c 0d 0a 2a 2a 20 66   to an SQL..** f
41b4a 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 7a 46  unction named zF
41b4b 75 6e 63 20 66 69 72 73 74 2e 20 46 6f 72 20 65  unc first. For e
41b4c 78 61 6d 70 6c 65 2c 20 69 66 20 7a 46 75 6e 63  xample, if zFunc
41b4d 20 69 73 20 22 75 6e 7a 69 70 22 20 61 6e 64 20   is "unzip" and 
41b4e 74 68 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 68 61  the..** table ha
41b4f 73 20 74 68 65 20 74 68 72 65 65 20 75 73 65 72  s the three user
41b50 2d 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 73  -defined columns
41b51 20 22 61 22 2c 20 22 62 22 2c 20 61 6e 64 20 22   "a", "b", and "
41b52 63 22 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  c", the followin
41b53 67 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20  g..** string is 
41b54 72 65 74 75 72 6e 65 64 3a 0d 0a 2a 2a 0d 0a 2a  returned:..**..*
41b55 2a 20 20 20 20 20 22 64 6f 63 69 64 2c 20 75 6e  *     "docid, un
41b56 7a 69 70 28 78 2e 27 61 27 29 2c 20 75 6e 7a 69  zip(x.'a'), unzi
41b57 70 28 78 2e 27 62 27 29 2c 20 75 6e 7a 69 70 28  p(x.'b'), unzip(
41b58 78 2e 27 63 27 29 20 46 52 4f 4d 20 25 5f 63 6f  x.'c') FROM %_co
41b59 6e 74 65 6e 74 20 41 53 20 78 22 0d 0a 2a 2a 0d  ntent AS x"..**.
41b5a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20  .** The pointer 
41b5b 72 65 74 75 72 6e 65 64 20 70 6f 69 6e 74 73 20  returned points 
41b5c 74 6f 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  to a buffer allo
41b5d 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
41b5e 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 74 0d 0a 2a  _malloc(). It..*
41b5f 2a 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  * is the respons
41b60 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63  ibility of the c
41b61 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61  aller to eventua
41b62 6c 6c 79 20 66 72 65 65 20 69 74 2e 0d 0a 2a 2a  lly free it...**
41b63 0d 0a 2a 2a 20 49 66 20 2a 70 52 63 20 69 73 20  ..** If *pRc is 
41b64 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68  not SQLITE_OK wh
41b65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
41b66 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69   is called, it i
41b67 73 20 61 20 6e 6f 2d 6f 70 20 28 61 6e 64 0d 0a  s a no-op (and..
41b68 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
41b69 72 20 69 73 20 72 65 74 75 72 6e 65 64 29 2e 20  r is returned). 
41b6a 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e  Otherwise, if an
41b6b 20 4f 4f 4d 20 65 72 72 6f 72 20 69 73 20 65 6e   OOM error is en
41b6c 63 6f 75 6e 74 65 72 65 64 0d 0a 2a 2a 20 62 79  countered..** by
41b6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
41b6e 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
41b6f 20 61 6e 64 20 2a 70 52 63 20 69 73 20 73 65 74   and *pRc is set
41b70 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   to SQLITE_NOMEM
41b71 2e 20 49 66 0d 0a 2a 2a 20 6e 6f 20 65 72 72 6f  . If..** no erro
41b72 72 20 6f 63 63 75 72 73 2c 20 2a 70 52 63 20 69  r occurs, *pRc i
41b73 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65  s left unmodifie
41b74 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63  d...*/..static c
41b75 68 61 72 20 2a 66 74 73 33 52 65 61 64 45 78 70  har *fts3ReadExp
41b76 72 4c 69 73 74 28 46 74 73 33 54 61 62 6c 65 20  rList(Fts3Table 
41b77 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
41b78 7a 46 75 6e 63 2c 20 69 6e 74 20 2a 70 52 63 29  zFunc, int *pRc)
41b79 7b 0d 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 20  {..  char *zRet 
41b7a 3d 20 30 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46  = 0;..  char *zF
41b7b 72 65 65 20 3d 20 30 3b 0d 0a 20 20 63 68 61 72  ree = 0;..  char
41b7c 20 2a 7a 46 75 6e 63 74 69 6f 6e 3b 0d 0a 20 20   *zFunction;..  
41b7d 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20 69 66 28 20  int i;....  if( 
41b7e 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d 3d  p->zContentTbl==
41b7f 30 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 21 7a  0 ){..    if( !z
41b80 46 75 6e 63 20 29 7b 0d 0a 20 20 20 20 20 20 7a  Func ){..      z
41b81 46 75 6e 63 74 69 6f 6e 20 3d 20 22 22 3b 0d 0a  Function = "";..
41b82 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
41b83 20 20 7a 46 72 65 65 20 3d 20 7a 46 75 6e 63 74    zFree = zFunct
41b84 69 6f 6e 20 3d 20 66 74 73 33 51 75 6f 74 65 49  ion = fts3QuoteI
41b85 64 28 7a 46 75 6e 63 29 3b 0d 0a 20 20 20 20 7d  d(zFunc);..    }
41b86 0d 0a 20 20 20 20 66 74 73 33 41 70 70 65 6e 64  ..    fts3Append
41b87 66 28 70 52 63 2c 20 26 7a 52 65 74 2c 20 22 64  f(pRc, &zRet, "d
41b88 6f 63 69 64 22 29 3b 0d 0a 20 20 20 20 66 6f 72  ocid");..    for
41b89 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 75  (i=0; i<p->nColu
41b8a 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  mn; i++){..     
41b8b 20 66 74 73 33 41 70 70 65 6e 64 66 28 70 52 63   fts3Appendf(pRc
41b8c 2c 20 26 7a 52 65 74 2c 20 22 2c 25 73 28 78 2e  , &zRet, ",%s(x.
41b8d 27 63 25 64 25 71 27 29 22 2c 20 7a 46 75 6e 63  'c%d%q')", zFunc
41b8e 74 69 6f 6e 2c 20 69 2c 20 70 2d 3e 61 7a 43 6f  tion, i, p->azCo
41b8f 6c 75 6d 6e 5b 69 5d 29 3b 0d 0a 20 20 20 20 7d  lumn[i]);..    }
41b90 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
41b91 65 65 28 7a 46 72 65 65 29 3b 0d 0a 20 20 7d 65  ee(zFree);..  }e
41b92 6c 73 65 7b 0d 0a 20 20 20 20 66 74 73 33 41 70  lse{..    fts3Ap
41b93 70 65 6e 64 66 28 70 52 63 2c 20 26 7a 52 65 74  pendf(pRc, &zRet
41b94 2c 20 22 72 6f 77 69 64 22 29 3b 0d 0a 20 20 20  , "rowid");..   
41b95 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
41b96 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0d 0a 20  Column; i++){.. 
41b97 20 20 20 20 20 66 74 73 33 41 70 70 65 6e 64 66       fts3Appendf
41b98 28 70 52 63 2c 20 26 7a 52 65 74 2c 20 22 2c 20  (pRc, &zRet, ", 
41b99 78 2e 27 25 71 27 22 2c 20 70 2d 3e 61 7a 43 6f  x.'%q'", p->azCo
41b9a 6c 75 6d 6e 5b 69 5d 29 3b 0d 0a 20 20 20 20 7d  lumn[i]);..    }
41b9b 0d 0a 20 20 7d 0d 0a 20 20 66 74 73 33 41 70 70  ..  }..  fts3App
41b9c 65 6e 64 66 28 70 52 63 2c 20 26 7a 52 65 74 2c  endf(pRc, &zRet,
41b9d 20 22 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 25   "FROM '%q'.'%q%
41b9e 73 27 20 41 53 20 78 22 2c 20 0d 0a 20 20 20 20  s' AS x", ..    
41b9f 20 20 70 2d 3e 7a 44 62 2c 0d 0a 20 20 20 20 20    p->zDb,..     
41ba0 20 28 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c   (p->zContentTbl
41ba1 20 3f 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62   ? p->zContentTb
41ba2 6c 20 3a 20 70 2d 3e 7a 4e 61 6d 65 29 2c 0d 0a  l : p->zName),..
41ba3 20 20 20 20 20 20 28 70 2d 3e 7a 43 6f 6e 74 65        (p->zConte
41ba4 6e 74 54 62 6c 20 3f 20 22 22 20 3a 20 22 5f 63  ntTbl ? "" : "_c
41ba5 6f 6e 74 65 6e 74 22 29 0d 0a 20 20 29 3b 0d 0a  ontent")..  );..
41ba6 20 20 72 65 74 75 72 6e 20 7a 52 65 74 3b 0d 0a    return zRet;..
41ba7 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
41ba8 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 4e 20 63  rn a list of N c
41ba9 6f 6d 6d 61 20 73 65 70 61 72 61 74 65 64 20 71  omma separated q
41baa 75 65 73 74 69 6f 6e 20 6d 61 72 6b 73 2c 20 77  uestion marks, w
41bab 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
41bac 6d 62 65 72 0d 0a 2a 2a 20 6f 66 20 63 6f 6c 75  mber..** of colu
41bad 6d 6e 73 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e  mns in the %_con
41bae 74 65 6e 74 20 74 61 62 6c 65 20 28 6f 6e 65 20  tent table (one 
41baf 66 6f 72 20 74 68 65 20 64 6f 63 69 64 20 70 6c  for the docid pl
41bb0 75 73 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 0d  us one for each.
41bb1 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64  .** user-defined
41bb2 20 74 65 78 74 20 63 6f 6c 75 6d 6e 29 2e 0d 0a   text column)...
41bb3 2a 2a 0d 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  **..** If argume
41bb4 6e 74 20 7a 46 75 6e 63 20 69 73 20 6e 6f 74 20  nt zFunc is not 
41bb5 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 62  NULL, then all b
41bb6 75 74 20 74 68 65 20 66 69 72 73 74 20 71 75 65  ut the first que
41bb7 73 74 69 6f 6e 20 6d 61 72 6b 0d 0a 2a 2a 20 69  stion mark..** i
41bb8 73 20 70 72 65 63 65 64 65 64 20 62 79 20 7a 46  s preceded by zF
41bb9 75 6e 63 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20  unc and an open 
41bba 62 72 61 63 6b 65 74 2c 20 61 6e 64 20 66 6f 6c  bracket, and fol
41bbb 6c 6f 77 65 64 20 62 79 20 61 20 63 6c 6f 73 65  lowed by a close
41bbc 64 0d 0a 2a 2a 20 62 72 61 63 6b 65 74 2e 20 46  d..** bracket. F
41bbd 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a  or example, if z
41bbe 46 75 6e 63 20 69 73 20 22 7a 69 70 22 20 61 6e  Func is "zip" an
41bbf 64 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65  d the FTS3 table
41bc0 20 68 61 73 20 74 68 72 65 65 20 0d 0a 2a 2a 20   has three ..** 
41bc1 75 73 65 72 2d 64 65 66 69 6e 65 64 20 74 65 78  user-defined tex
41bc2 74 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 20 66  t columns, the f
41bc3 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67 20  ollowing string 
41bc4 69 73 20 72 65 74 75 72 6e 65 64 3a 0d 0a 2a 2a  is returned:..**
41bc5 0d 0a 2a 2a 20 20 20 20 20 22 3f 2c 20 7a 69 70  ..**     "?, zip
41bc6 28 3f 29 2c 20 7a 69 70 28 3f 29 2c 20 7a 69 70  (?), zip(?), zip
41bc7 28 3f 29 22 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  (?)"..**..** The
41bc8 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
41bc9 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75  d points to a bu
41bca 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62  ffer allocated b
41bcb 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  y sqlite3_malloc
41bcc 28 29 2e 20 49 74 0d 0a 2a 2a 20 69 73 20 74 68  (). It..** is th
41bcd 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
41bce 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74   of the caller t
41bcf 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  o eventually fre
41bd0 65 20 69 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  e it...**..** If
41bd1 20 2a 70 52 63 20 69 73 20 6e 6f 74 20 53 51 4c   *pRc is not SQL
41bd2 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73  ITE_OK when this
41bd3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
41bd4 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d  led, it is a no-
41bd5 6f 70 20 28 61 6e 64 0d 0a 2a 2a 20 61 20 4e 55  op (and..** a NU
41bd6 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65  LL pointer is re
41bd7 74 75 72 6e 65 64 29 2e 20 4f 74 68 65 72 77 69  turned). Otherwi
41bd8 73 65 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72  se, if an OOM er
41bd9 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
41bda 65 64 0d 0a 2a 2a 20 62 79 20 74 68 69 73 20 66  ed..** by this f
41bdb 75 6e 63 74 69 6f 6e 2c 20 4e 55 4c 4c 20 69 73  unction, NULL is
41bdc 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
41bdd 52 63 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  Rc is set to SQL
41bde 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 0d 0a 2a  ITE_NOMEM. If..*
41bdf 2a 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  * no error occur
41be0 73 2c 20 2a 70 52 63 20 69 73 20 6c 65 66 74 20  s, *pRc is left 
41be1 75 6e 6d 6f 64 69 66 69 65 64 2e 0d 0a 2a 2f 0d  unmodified...*/.
41be2 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 74  .static char *ft
41be3 73 33 57 72 69 74 65 45 78 70 72 4c 69 73 74 28  s3WriteExprList(
41be4 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 63 6f  Fts3Table *p, co
41be5 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 2c  nst char *zFunc,
41be6 20 69 6e 74 20 2a 70 52 63 29 7b 0d 0a 20 20 63   int *pRc){..  c
41be7 68 61 72 20 2a 7a 52 65 74 20 3d 20 30 3b 0d 0a  har *zRet = 0;..
41be8 20 20 63 68 61 72 20 2a 7a 46 72 65 65 20 3d 20    char *zFree = 
41be9 30 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 46 75 6e  0;..  char *zFun
41bea 63 74 69 6f 6e 3b 0d 0a 20 20 69 6e 74 20 69 3b  ction;..  int i;
41beb 0d 0a 0d 0a 20 20 69 66 28 20 21 7a 46 75 6e 63  ....  if( !zFunc
41bec 20 29 7b 0d 0a 20 20 20 20 7a 46 75 6e 63 74 69   ){..    zFuncti
41bed 6f 6e 20 3d 20 22 22 3b 0d 0a 20 20 7d 65 6c 73  on = "";..  }els
41bee 65 7b 0d 0a 20 20 20 20 7a 46 72 65 65 20 3d 20  e{..    zFree = 
41bef 7a 46 75 6e 63 74 69 6f 6e 20 3d 20 66 74 73 33  zFunction = fts3
41bf0 51 75 6f 74 65 49 64 28 7a 46 75 6e 63 29 3b 0d  QuoteId(zFunc);.
41bf1 0a 20 20 7d 0d 0a 20 20 66 74 73 33 41 70 70 65  .  }..  fts3Appe
41bf2 6e 64 66 28 70 52 63 2c 20 26 7a 52 65 74 2c 20  ndf(pRc, &zRet, 
41bf3 22 3f 22 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30  "?");..  for(i=0
41bf4 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  ; i<p->nColumn; 
41bf5 69 2b 2b 29 7b 0d 0a 20 20 20 20 66 74 73 33 41  i++){..    fts3A
41bf6 70 70 65 6e 64 66 28 70 52 63 2c 20 26 7a 52 65  ppendf(pRc, &zRe
41bf7 74 2c 20 22 2c 25 73 28 3f 29 22 2c 20 7a 46 75  t, ",%s(?)", zFu
41bf8 6e 63 74 69 6f 6e 29 3b 0d 0a 20 20 7d 0d 0a 20  nction);..  }.. 
41bf9 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46   sqlite3_free(zF
41bfa 72 65 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ree);..  return 
41bfb 7a 52 65 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  zRet;..}..../*..
41bfc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
41bfd 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20   interprets the 
41bfe 73 74 72 69 6e 67 20 61 74 20 28 2a 70 70 29 20  string at (*pp) 
41bff 61 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  as a non-negativ
41c00 65 20 69 6e 74 65 67 65 72 0d 0a 2a 2a 20 76 61  e integer..** va
41c01 6c 75 65 2e 20 49 74 20 72 65 61 64 73 20 74 68  lue. It reads th
41c02 65 20 69 6e 74 65 67 65 72 20 61 6e 64 20 73 65  e integer and se
41c03 74 73 20 2a 70 6e 4f 75 74 20 74 6f 20 74 68 65  ts *pnOut to the
41c04 20 76 61 6c 75 65 20 72 65 61 64 2c 20 74 68 65   value read, the
41c05 6e 20 0d 0a 2a 2a 20 73 65 74 73 20 2a 70 70 20  n ..** sets *pp 
41c06 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
41c07 62 79 74 65 20 69 6d 6d 65 64 69 61 74 65 6c 79  byte immediately
41c08 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c   following the l
41c09 61 73 74 20 62 79 74 65 20 6f 66 0d 0a 2a 2a 20  ast byte of..** 
41c0a 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
41c0b 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 6e 6c 79 20  e...**..** Only 
41c0c 64 65 63 69 6d 61 6c 20 64 69 67 69 74 73 20 28  decimal digits (
41c0d 27 30 27 2e 2e 27 39 27 29 20 6d 61 79 20 62 65  '0'..'9') may be
41c0e 20 70 61 72 74 20 6f 66 20 61 6e 20 69 6e 74 65   part of an inte
41c0f 67 65 72 20 76 61 6c 75 65 2e 20 0d 0a 2a 2a 0d  ger value. ..**.
41c10 0a 2a 2a 20 49 66 20 2a 70 70 20 64 6f 65 73 20  .** If *pp does 
41c11 6e 6f 74 20 62 65 69 6e 67 20 77 69 74 68 20 61  not being with a
41c12 20 64 65 63 69 6d 61 6c 20 64 69 67 69 74 20 53   decimal digit S
41c13 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72  QLITE_ERROR is r
41c14 65 74 75 72 6e 65 64 20 61 6e 64 0d 0a 2a 2a 20  eturned and..** 
41c15 74 68 65 20 6f 75 74 70 75 74 20 76 61 6c 75 65  the output value
41c16 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65   undefined. Othe
41c17 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 20  rwise SQLITE_OK 
41c18 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
41c19 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
41c1a 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  on is used when 
41c1b 70 61 72 73 69 6e 67 20 74 68 65 20 22 70 72 65  parsing the "pre
41c1c 66 69 78 3d 22 20 46 54 53 34 20 70 61 72 61 6d  fix=" FTS4 param
41c1d 65 74 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  eter...*/..stati
41c1e 63 20 69 6e 74 20 66 74 73 33 47 6f 62 62 6c 65  c int fts3Gobble
41c1f 49 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Int(const char *
41c20 2a 70 70 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29  *pp, int *pnOut)
41c21 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  {..  const char 
41c22 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *p;             
41c23 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
41c24 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20 20 69   pointer */..  i
41c25 6e 74 20 6e 49 6e 74 20 3d 20 30 3b 20 20 20 20  nt nInt = 0;    
41c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41c27 2a 20 4f 75 74 70 75 74 20 76 61 6c 75 65 20 2a  * Output value *
41c28 2f 0d 0a 0d 0a 20 20 66 6f 72 28 70 3d 2a 70 70  /....  for(p=*pp
41c29 3b 20 70 5b 30 5d 3e 3d 27 30 27 20 26 26 20 70  ; p[0]>='0' && p
41c2a 5b 30 5d 3c 3d 27 39 27 3b 20 70 2b 2b 29 7b 0d  [0]<='9'; p++){.
41c2b 0a 20 20 20 20 6e 49 6e 74 20 3d 20 6e 49 6e 74  .    nInt = nInt
41c2c 20 2a 20 31 30 20 2b 20 28 70 5b 30 5d 20 2d 20   * 10 + (p[0] - 
41c2d 27 30 27 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  '0');..  }..  if
41c2e 28 20 70 3d 3d 2a 70 70 20 29 20 72 65 74 75 72  ( p==*pp ) retur
41c2f 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
41c30 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 6e 49 6e 74  .  *pnOut = nInt
41c31 3b 0d 0a 20 20 2a 70 70 20 3d 20 70 3b 0d 0a 20  ;..  *pp = p;.. 
41c32 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
41c33 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
41c34 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
41c35 20 63 61 6c 6c 65 64 20 74 6f 20 61 6c 6c 6f 63   called to alloc
41c36 61 74 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ate an array of 
41c37 46 74 73 33 49 6e 64 65 78 20 73 74 72 75 63 74  Fts3Index struct
41c38 75 72 65 73 0d 0a 2a 2a 20 72 65 70 72 65 73 65  ures..** represe
41c39 6e 74 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65  nting the indexe
41c3a 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 62 79 20  s maintained by 
41c3b 74 68 65 20 63 75 72 72 65 6e 74 20 46 54 53 20  the current FTS 
41c3c 74 61 62 6c 65 2e 20 46 54 53 20 74 61 62 6c 65  table. FTS table
41c3d 73 0d 0a 2a 2a 20 61 6c 77 61 79 73 20 6d 61 69  s..** always mai
41c3e 6e 74 61 69 6e 20 74 68 65 20 6d 61 69 6e 20 22  ntain the main "
41c3f 74 65 72 6d 73 22 20 69 6e 64 65 78 2c 20 62 75  terms" index, bu
41c40 74 20 6d 61 79 20 61 6c 73 6f 20 6d 61 69 6e 74  t may also maint
41c41 61 69 6e 20 6f 6e 65 20 6f 72 0d 0a 2a 2a 20 6d  ain one or..** m
41c42 6f 72 65 20 22 70 72 65 66 69 78 22 20 69 6e 64  ore "prefix" ind
41c43 65 78 65 73 2c 20 64 65 70 65 6e 64 69 6e 67 20  exes, depending 
41c44 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  on the value of 
41c45 74 68 65 20 22 70 72 65 66 69 78 3d 22 20 70 61  the "prefix=" pa
41c46 72 61 6d 65 74 65 72 0d 0a 2a 2a 20 28 69 66 20  rameter..** (if 
41c47 61 6e 79 29 20 73 70 65 63 69 66 69 65 64 20 61  any) specified a
41c48 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 43 52  s part of the CR
41c49 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
41c4a 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0d 0a 2a  LE statement...*
41c4b 2a 0d 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a  *..** Argument z
41c4c 50 61 72 61 6d 20 69 73 20 70 61 73 73 65 64 20  Param is passed 
41c4d 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
41c4e 20 22 70 72 65 66 69 78 3d 22 20 6f 70 74 69 6f   "prefix=" optio
41c4f 6e 20 69 66 20 6f 6e 65 20 77 61 73 0d 0a 2a 2a  n if one was..**
41c50 20 73 70 65 63 69 66 69 65 64 2c 20 6f 72 20 4e   specified, or N
41c51 55 4c 4c 20 6f 74 68 65 72 77 69 73 65 2e 0d 0a  ULL otherwise...
41c52 2a 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 20 65 72 72  **..** If no err
41c53 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  or occurs, SQLIT
41c54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
41c55 20 61 6e 64 20 2a 61 70 49 6e 64 65 78 20 73 65   and *apIndex se
41c56 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0d 0a 2a  t to point to..*
41c57 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  * the allocated 
41c58 61 72 72 61 79 2e 20 2a 70 6e 49 6e 64 65 78 20  array. *pnIndex 
41c59 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
41c5a 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
41c5b 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 61 72 72 61   in the..** arra
41c5c 79 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 64  y. If an error d
41c5d 6f 65 73 20 6f 63 63 75 72 2c 20 61 6e 20 53 51  oes occur, an SQ
41c5e 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
41c5f 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a  is returned...**
41c60 0d 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20  ..** Regardless 
41c61 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
41c62 74 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  t an error is re
41c63 74 75 72 6e 65 64 2c 20 69 74 20 69 73 20 74 68  turned, it is th
41c64 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
41c65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c  ..** of the call
41c66 65 72 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  er to call sqlit
41c67 65 33 5f 66 72 65 65 28 29 20 6f 6e 20 74 68 65  e3_free() on the
41c68 20 6f 75 74 70 75 74 20 61 72 72 61 79 20 74 6f   output array to
41c69 20 66 72 65 65 20 69 74 2e 0d 0a 2a 2f 0d 0a 73   free it...*/..s
41c6a 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 72  tatic int fts3Pr
41c6b 65 66 69 78 50 61 72 61 6d 65 74 65 72 28 0d 0a  efixParameter(..
41c6c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
41c6d 61 72 61 6d 2c 20 20 20 20 20 20 20 20 20 20 20  aram,           
41c6e 20 20 2f 2a 20 41 42 43 20 69 6e 20 70 72 65 66    /* ABC in pref
41c6f 69 78 3d 41 42 43 20 70 61 72 61 6d 65 74 65 72  ix=ABC parameter
41c70 20 74 6f 20 70 61 72 73 65 20 2a 2f 0d 0a 20 20   to parse */..  
41c71 69 6e 74 20 2a 70 6e 49 6e 64 65 78 2c 20 20 20  int *pnIndex,   
41c72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41c73 2f 2a 20 4f 55 54 3a 20 73 69 7a 65 20 6f 66 20  /* OUT: size of 
41c74 2a 61 70 49 6e 64 65 78 5b 5d 20 61 72 72 61 79  *apIndex[] array
41c75 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20 46 74   */..  struct Ft
41c76 73 33 49 6e 64 65 78 20 2a 2a 61 70 49 6e 64 65  s3Index **apInde
41c77 78 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  x      /* OUT: A
41c78 72 72 61 79 20 6f 66 20 69 6e 64 65 78 65 73 20  rray of indexes 
41c79 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 2a  for this table *
41c7a 2f 0d 0a 29 7b 0d 0a 20 20 73 74 72 75 63 74 20  /..){..  struct 
41c7b 46 74 73 33 49 6e 64 65 78 20 2a 61 49 6e 64 65  Fts3Index *aInde
41c7c 78 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f  x;       /* Allo
41c7d 63 61 74 65 64 20 61 72 72 61 79 20 2a 2f 0d 0a  cated array */..
41c7e 20 20 69 6e 74 20 6e 49 6e 64 65 78 20 3d 20 31    int nIndex = 1
41c7f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
41c80 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
41c81 6e 74 72 69 65 73 20 69 6e 20 61 72 72 61 79 20  ntries in array 
41c82 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 7a 50 61 72  */....  if( zPar
41c83 61 6d 20 26 26 20 7a 50 61 72 61 6d 5b 30 5d 20  am && zParam[0] 
41c84 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ){..    const ch
41c85 61 72 20 2a 70 3b 0d 0a 20 20 20 20 6e 49 6e 64  ar *p;..    nInd
41c86 65 78 2b 2b 3b 0d 0a 20 20 20 20 66 6f 72 28 70  ex++;..    for(p
41c87 3d 7a 50 61 72 61 6d 3b 20 2a 70 3b 20 70 2b 2b  =zParam; *p; p++
41c88 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 2a 70  ){..      if( *p
41c89 3d 3d 27 2c 27 20 29 20 6e 49 6e 64 65 78 2b 2b  ==',' ) nIndex++
41c8a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
41c8b 0a 20 20 61 49 6e 64 65 78 20 3d 20 73 71 6c 69  .  aIndex = sqli
41c8c 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
41c8d 66 28 73 74 72 75 63 74 20 46 74 73 33 49 6e 64  f(struct Fts3Ind
41c8e 65 78 29 20 2a 20 6e 49 6e 64 65 78 29 3b 0d 0a  ex) * nIndex);..
41c8f 20 20 2a 61 70 49 6e 64 65 78 20 3d 20 61 49 6e    *apIndex = aIn
41c90 64 65 78 3b 0d 0a 20 20 2a 70 6e 49 6e 64 65 78  dex;..  *pnIndex
41c91 20 3d 20 6e 49 6e 64 65 78 3b 0d 0a 20 20 69 66   = nIndex;..  if
41c92 28 20 21 61 49 6e 64 65 78 20 29 7b 0d 0a 20 20  ( !aIndex ){..  
41c93 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
41c94 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  NOMEM;..  }.... 
41c95 20 6d 65 6d 73 65 74 28 61 49 6e 64 65 78 2c 20   memset(aIndex, 
41c96 30 2c 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74  0, sizeof(struct
41c97 20 46 74 73 33 49 6e 64 65 78 29 20 2a 20 6e 49   Fts3Index) * nI
41c98 6e 64 65 78 29 3b 0d 0a 20 20 69 66 28 20 7a 50  ndex);..  if( zP
41c99 61 72 61 6d 20 29 7b 0d 0a 20 20 20 20 63 6f 6e  aram ){..    con
41c9a 73 74 20 63 68 61 72 20 2a 70 20 3d 20 7a 50 61  st char *p = zPa
41c9b 72 61 6d 3b 0d 0a 20 20 20 20 69 6e 74 20 69 3b  ram;..    int i;
41c9c 0d 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ..    for(i=1; i
41c9d 3c 6e 49 6e 64 65 78 3b 20 69 2b 2b 29 7b 0d 0a  <nIndex; i++){..
41c9e 20 20 20 20 20 20 69 6e 74 20 6e 50 72 65 66 69        int nPrefi
41c9f 78 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 66 74  x;..      if( ft
41ca0 73 33 47 6f 62 62 6c 65 49 6e 74 28 26 70 2c 20  s3GobbleInt(&p, 
41ca1 26 6e 50 72 65 66 69 78 29 20 29 20 72 65 74 75  &nPrefix) ) retu
41ca2 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
41ca3 0d 0a 20 20 20 20 20 20 61 49 6e 64 65 78 5b 69  ..      aIndex[i
41ca4 5d 2e 6e 50 72 65 66 69 78 20 3d 20 6e 50 72 65  ].nPrefix = nPre
41ca5 66 69 78 3b 0d 0a 20 20 20 20 20 20 70 2b 2b 3b  fix;..      p++;
41ca6 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
41ca7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
41ca8 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
41ca9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
41caa 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e  s called when in
41cab 69 74 69 61 6c 69 7a 69 6e 67 20 61 6e 20 46 54  itializing an FT
41cac 53 34 20 74 61 62 6c 65 20 74 68 61 74 20 75 73  S4 table that us
41cad 65 73 20 74 68 65 0d 0a 2a 2a 20 63 6f 6e 74 65  es the..** conte
41cae 6e 74 3d 78 78 78 20 6f 70 74 69 6f 6e 2e 20 49  nt=xxx option. I
41caf 74 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65  t determines the
41cb0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 64 20 6e   number of and n
41cb1 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75  ames of the colu
41cb2 6d 6e 73 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 6e  mns..** of the n
41cb3 65 77 20 46 54 53 34 20 74 61 62 6c 65 2e 0d 0a  ew FTS4 table...
41cb4 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  **..** The third
41cb5 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
41cb6 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
41cb7 6e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 70  n is the value p
41cb8 61 73 73 65 64 20 74 6f 20 74 68 65 0d 0a 2a 2a  assed to the..**
41cb9 20 63 6f 6e 66 69 67 3d 78 78 78 20 6f 70 74 69   config=xxx opti
41cba 6f 6e 20 28 69 2e 65 2e 20 22 78 78 78 22 29 2e  on (i.e. "xxx").
41cbb 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 71   This function q
41cbc 75 65 72 69 65 73 20 74 68 65 20 64 61 74 61 62  ueries the datab
41cbd 61 73 65 20 66 6f 72 0d 0a 2a 2a 20 61 20 74 61  ase for..** a ta
41cbe 62 6c 65 20 6f 66 20 74 68 61 74 20 6e 61 6d 65  ble of that name
41cbf 2e 20 49 66 20 66 6f 75 6e 64 2c 20 74 68 65 20  . If found, the 
41cc0 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
41cc1 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64 0d 0a   are populated..
41cc2 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a  ** as follows:..
41cc3 2a 2a 0d 0a 2a 2a 20 20 20 2a 70 6e 43 6f 6c 3a  **..**   *pnCol:
41cc4 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 6e 75     Set to the nu
41cc5 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
41cc6 74 61 62 6c 65 20 78 78 78 20 68 61 73 2c 0d 0a  table xxx has,..
41cc7 2a 2a 0d 0a 2a 2a 20 20 20 2a 70 6e 53 74 72 3a  **..**   *pnStr:
41cc8 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 74 6f     Set to the to
41cc9 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70  tal amount of sp
41cca 61 63 65 20 72 65 71 75 69 72 65 64 20 74 6f 20  ace required to 
41ccb 73 74 6f 72 65 20 61 20 63 6f 70 79 0d 0a 2a 2a  store a copy..**
41ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
41ccd 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d  each columns nam
41cce 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  e, including the
41ccf 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e   nul-terminator.
41cd0 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 70 61 7a 43  ..**..**   *pazC
41cd1 6f 6c 3a 20 20 53 65 74 20 74 6f 20 70 6f 69 6e  ol:  Set to poin
41cd2 74 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  t to an array of
41cd3 20 2a 70 6e 43 6f 6c 20 73 74 72 69 6e 67 73 2e   *pnCol strings.
41cd4 20 45 61 63 68 20 73 74 72 69 6e 67 20 69 73 0d   Each string is.
41cd5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
41cd6 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
41cd7 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 6f  corresponding co
41cd8 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 78 78  lumn in table xx
41cd9 78 2e 20 54 68 65 20 61 72 72 61 79 0d 0a 2a 2a  x. The array..**
41cda 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64               and
41cdb 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 61 72   its contents ar
41cdc 65 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  e allocated usin
41cdd 67 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63  g a single alloc
41cde 61 74 69 6f 6e 2e 20 49 74 0d 0a 2a 2a 20 20 20  ation. It..**   
41cdf 20 20 20 20 20 20 20 20 20 20 69 73 20 74 68 65            is the
41ce0 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
41ce1 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
41ce2 20 66 72 65 65 20 74 68 69 73 20 61 6c 6c 6f 63   free this alloc
41ce3 61 74 69 6f 6e 0d 0a 2a 2a 20 20 20 20 20 20 20  ation..**       
41ce4 20 20 20 20 20 20 62 79 20 65 76 65 6e 74 75 61        by eventua
41ce5 6c 6c 79 20 70 61 73 73 69 6e 67 20 74 68 65 20  lly passing the 
41ce6 2a 70 61 7a 43 6f 6c 20 76 61 6c 75 65 20 74 6f  *pazCol value to
41ce7 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e   sqlite3_free().
41ce8 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
41ce9 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20  table cannot be 
41cea 66 6f 75 6e 64 2c 20 61 6e 20 65 72 72 6f 72 20  found, an error 
41ceb 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
41cec 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0d   and the output.
41ced 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 72  .** variables ar
41cee 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f 72 2c  e undefined. Or,
41cef 20 69 66 20 61 6e 20 4f 4f 4d 20 69 73 20 65 6e   if an OOM is en
41cf0 63 6f 75 6e 74 65 72 65 64 2c 20 53 51 4c 49 54  countered, SQLIT
41cf1 45 5f 4e 4f 4d 45 4d 20 69 73 0d 0a 2a 2a 20 72  E_NOMEM is..** r
41cf2 65 74 75 72 6e 65 64 20 28 61 6e 64 20 74 68 65  eturned (and the
41cf3 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
41cf4 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 29  s are undefined)
41cf5 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
41cf6 74 20 66 74 73 33 43 6f 6e 74 65 6e 74 43 6f 6c  t fts3ContentCol
41cf7 75 6d 6e 73 28 0d 0a 20 20 73 71 6c 69 74 65 33  umns(..  sqlite3
41cf8 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
41cf9 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
41cfa 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a  base handle */..
41cfb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
41cfc 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
41cfd 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 62 20    /* Name of db 
41cfe 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74  (i.e. "main", "t
41cff 65 6d 70 22 20 65 74 63 2e 29 20 2a 2f 0d 0a 20  emp" etc.) */.. 
41d00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
41d01 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
41d02 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6e 74   /* Name of cont
41d03 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  ent table */..  
41d04 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2a 70 61  const char ***pa
41d05 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zCol,           
41d06 2f 2a 20 4f 55 54 3a 20 4d 61 6c 6c 6f 63 27 64  /* OUT: Malloc'd
41d07 20 61 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e   array of column
41d08 20 6e 61 6d 65 73 20 2a 2f 0d 0a 20 20 69 6e 74   names */..  int
41d09 20 2a 70 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20   *pnCol,        
41d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41d0b 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 61 72 72  OUT: Size of arr
41d0c 61 79 20 2a 70 61 7a 43 6f 6c 20 2a 2f 0d 0a 20  ay *pazCol */.. 
41d0d 20 69 6e 74 20 2a 70 6e 53 74 72 20 20 20 20 20   int *pnStr     
41d0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41d0f 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 73 20 6f   /* OUT: Bytes o
41d10 66 20 73 74 72 69 6e 67 20 63 6f 6e 74 65 6e 74  f string content
41d11 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
41d12 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
41d13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
41d14 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  turn code */..  
41d15 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20  char *zSql;     
41d16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41d17 2f 2a 20 22 53 45 4c 45 43 54 20 2a 22 20 73 74  /* "SELECT *" st
41d18 61 74 65 6d 65 6e 74 20 6f 6e 20 7a 54 62 6c 20  atement on zTbl 
41d19 2a 2f 20 20 0d 0a 20 20 73 71 6c 69 74 65 33 5f  */  ..  sqlite3_
41d1a 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
41d1b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 69          /* Compi
41d1c 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 7a  led version of z
41d1d 53 71 6c 20 2a 2f 0d 0a 0d 0a 20 20 7a 53 71 6c  Sql */....  zSql
41d1e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
41d1f 74 66 28 22 53 45 4c 45 43 54 20 2a 20 46 52 4f  tf("SELECT * FRO
41d20 4d 20 25 51 2e 25 51 22 2c 20 7a 44 62 2c 20 7a  M %Q.%Q", zDb, z
41d21 54 62 6c 29 3b 0d 0a 20 20 69 66 28 20 21 7a 53  Tbl);..  if( !zS
41d22 71 6c 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  ql ){..    rc = 
41d23 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
41d24 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20   }else{..    rc 
41d25 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
41d26 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  e(db, zSql, -1, 
41d27 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 7d  &pStmt, 0);..  }
41d28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
41d29 28 7a 53 71 6c 29 3b 0d 0a 0d 0a 20 20 69 66 28  (zSql);....  if(
41d2a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
41d2b 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  {..    const cha
41d2c 72 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20 20  r **azCol;      
41d2d 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 61       /* Output a
41d2e 72 72 61 79 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  rray */..    int
41d2f 20 6e 53 74 72 20 3d 20 30 3b 20 20 20 20 20 20   nStr = 0;      
41d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
41d31 7a 65 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e  ze of all column
41d32 20 6e 61 6d 65 73 20 28 69 6e 63 6c 2e 20 30 78   names (incl. 0x
41d33 30 30 29 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  00) */..    int 
41d34 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
41d35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
41d36 62 65 72 20 6f 66 20 74 61 62 6c 65 20 63 6f 6c  ber of table col
41d37 75 6d 6e 73 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  umns */..    int
41d38 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
41d39 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
41d3a 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
41d3b 72 6f 75 67 68 20 63 6f 6c 75 6d 6e 73 20 2a 2f  rough columns */
41d3c 0d 0a 0d 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20  ....    /* Loop 
41d3d 74 68 72 6f 75 67 68 20 74 68 65 20 72 65 74 75  through the retu
41d3e 72 6e 65 64 20 63 6f 6c 75 6d 6e 73 2e 20 53 65  rned columns. Se
41d3f 74 20 6e 53 74 72 20 74 6f 20 74 68 65 20 6e 75  t nStr to the nu
41d40 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
41d41 0d 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 72  ..    ** space r
41d42 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
41d43 20 61 20 63 6f 70 79 20 6f 66 20 65 61 63 68 20   a copy of each 
41d44 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 69 6e 63  column name, inc
41d45 6c 75 64 69 6e 67 20 74 68 65 0d 0a 20 20 20 20  luding the..    
41d46 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  ** nul-terminato
41d47 72 20 62 79 74 65 2e 20 20 2a 2f 0d 0a 20 20 20  r byte.  */..   
41d48 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f   nCol = sqlite3_
41d49 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74  column_count(pSt
41d4a 6d 74 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  mt);..    for(i=
41d4b 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
41d4c 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ..      const ch
41d4d 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69 74  ar *zCol = sqlit
41d4e 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70  e3_column_name(p
41d4f 53 74 6d 74 2c 20 69 29 3b 0d 0a 20 20 20 20 20  Stmt, i);..     
41d50 20 6e 53 74 72 20 2b 3d 20 73 74 72 6c 65 6e 28   nStr += strlen(
41d51 7a 43 6f 6c 29 20 2b 20 31 3b 0d 0a 20 20 20 20  zCol) + 1;..    
41d52 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f  }....    /* Allo
41d53 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74  cate and populat
41d54 65 20 74 68 65 20 61 72 72 61 79 20 74 6f 20 72  e the array to r
41d55 65 74 75 72 6e 2e 20 2a 2f 0d 0a 20 20 20 20 61  eturn. */..    a
41d56 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68  zCol = (const ch
41d57 61 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61  ar **)sqlite3_ma
41d58 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72  lloc(sizeof(char
41d59 20 2a 29 20 2a 20 6e 43 6f 6c 20 2b 20 6e 53 74   *) * nCol + nSt
41d5a 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 61 7a 43  r);..    if( azC
41d5b 6f 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  ol==0 ){..      
41d5c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
41d5d 4d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  M;..    }else{..
41d5e 20 20 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20        char *p = 
41d5f 28 63 68 61 72 20 2a 29 26 61 7a 43 6f 6c 5b 6e  (char *)&azCol[n
41d60 43 6f 6c 5d 3b 0d 0a 20 20 20 20 20 20 66 6f 72  Col];..      for
41d61 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
41d62 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e  +){..        con
41d63 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20  st char *zCol = 
41d64 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
41d65 61 6d 65 28 70 53 74 6d 74 2c 20 69 29 3b 0d 0a  ame(pStmt, i);..
41d66 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
41d67 73 74 72 6c 65 6e 28 7a 43 6f 6c 29 2b 31 3b 0d  strlen(zCol)+1;.
41d68 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
41d69 70 2c 20 7a 43 6f 6c 2c 20 6e 29 3b 0d 0a 20 20  p, zCol, n);..  
41d6a 20 20 20 20 20 20 61 7a 43 6f 6c 5b 69 5d 20 3d        azCol[i] =
41d6b 20 70 3b 0d 0a 20 20 20 20 20 20 20 20 70 20 2b   p;..        p +
41d6c 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  = n;..      }.. 
41d6d 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
41d6e 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
41d6f 29 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 53 65 74  );....    /* Set
41d70 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69   the output vari
41d71 61 62 6c 65 73 2e 20 2a 2f 0d 0a 20 20 20 20 2a  ables. */..    *
41d72 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0d 0a 20  pnCol = nCol;.. 
41d73 20 20 20 2a 70 6e 53 74 72 20 3d 20 6e 53 74 72     *pnStr = nStr
41d74 3b 0d 0a 20 20 20 20 2a 70 61 7a 43 6f 6c 20 3d  ;..    *pazCol =
41d75 20 61 7a 43 6f 6c 3b 0d 0a 20 20 7d 0d 0a 0d 0a   azCol;..  }....
41d76 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
41d77 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
41d78 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69  unction is the i
41d79 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
41d7a 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e 6e 65   both the xConne
41d7b 63 74 20 61 6e 64 20 78 43 72 65 61 74 65 0d 0a  ct and xCreate..
41d7c 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68  ** methods of th
41d7d 65 20 46 54 53 33 20 76 69 72 74 75 61 6c 20 74  e FTS3 virtual t
41d7e 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  able...**..** Th
41d7f 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63  e argv[] array c
41d80 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c 6c  ontains the foll
41d81 6f 77 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  owing:..**..**  
41d82 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f   argv[0]   -> mo
41d83 64 75 6c 65 20 6e 61 6d 65 20 20 28 22 66 74 73  dule name  ("fts
41d84 33 22 20 6f 72 20 22 66 74 73 34 22 29 0d 0a 2a  3" or "fts4")..*
41d85 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d 3e  *   argv[1]   ->
41d86 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0d 0a   database name..
41d87 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d  **   argv[2]   -
41d88 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0d 0a 2a 2a  > table name..**
41d89 20 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20     argv[...] -> 
41d8a 22 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 20 61 6e  "column name" an
41d8b 64 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 20 61  d other module a
41d8c 72 67 75 6d 65 6e 74 20 66 69 65 6c 64 73 2e 0d  rgument fields..
41d8d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
41d8e 66 74 73 33 49 6e 69 74 56 74 61 62 28 0d 0a 20  fts3InitVtab(.. 
41d8f 20 69 6e 74 20 69 73 43 72 65 61 74 65 2c 20 20   int isCreate,  
41d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41d91 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43 72   /* True for xCr
41d92 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72 20  eate, false for 
41d93 78 43 6f 6e 6e 65 63 74 20 2a 2f 0d 0a 20 20 73  xConnect */..  s
41d94 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
41d95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41d96 2a 20 54 68 65 20 53 51 4c 69 74 65 20 64 61 74  * The SQLite dat
41d97 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
41d98 20 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 41 75   */..  void *pAu
41d99 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
41d9a 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74         /* Hash t
41d9b 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
41d9c 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f 0d 0a 20  tokenizers */.. 
41d9d 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20   int argc,      
41d9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41d9f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
41da0 65 6d 65 6e 74 73 20 69 6e 20 61 72 67 76 20 61  ements in argv a
41da1 72 72 61 79 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  rray */..  const
41da2 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61   char * const *a
41da3 72 67 76 2c 20 20 20 20 20 20 20 2f 2a 20 78 43  rgv,       /* xC
41da4 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 61  reate/xConnect a
41da5 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f  rgument array */
41da6 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ..  sqlite3_vtab
41da7 20 2a 2a 70 70 56 54 61 62 2c 20 20 20 20 20 20   **ppVTab,      
41da8 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
41da9 20 72 65 73 75 6c 74 69 6e 67 20 76 74 61 62 20   resulting vtab 
41daa 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a  structure here *
41dab 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  /..  char **pzEr
41dac 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
41dad 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e       /* Write an
41dae 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  y error message 
41daf 68 65 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46  here */..){..  F
41db0 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d  ts3Hash *pHash =
41db1 20 28 46 74 73 33 48 61 73 68 20 2a 29 70 41 75   (Fts3Hash *)pAu
41db2 78 3b 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20  x;..  Fts3Table 
41db3 2a 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  *p = 0;         
41db4 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
41db5 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 76 74   to allocated vt
41db6 61 62 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20  ab */..  int rc 
41db7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
41db8 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
41db9 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e  rn code */..  in
41dba 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
41dbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41dbc 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
41dbd 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79  le */..  int nBy
41dbe 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
41dbf 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
41dc0 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 75   of allocation u
41dc1 73 65 64 20 66 6f 72 20 2a 70 20 2a 2f 0d 0a 20  sed for *p */.. 
41dc2 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
41dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41dc4 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78   /* Column index
41dc5 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 74 72 69   */..  int nStri
41dc6 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ng = 0;         
41dc7 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
41dc8 72 65 71 75 69 72 65 64 20 74 6f 20 68 6f 6c 64  required to hold
41dc9 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   all column name
41dca 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c  s */..  int nCol
41dcb 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
41dcc 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
41dcd 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
41dce 74 68 65 20 46 54 53 20 74 61 62 6c 65 20 2a 2f  the FTS table */
41dcf 0d 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 3b 20  ..  char *zCsr; 
41dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41dd1 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
41dd2 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 20   holding column 
41dd3 6e 61 6d 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  names */..  int 
41dd4 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  nDb;            
41dd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
41dd6 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ytes required to
41dd7 20 68 6f 6c 64 20 64 61 74 61 62 61 73 65 20 6e   hold database n
41dd8 61 6d 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e  ame */..  int nN
41dd9 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
41dda 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
41ddb 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 68  es required to h
41ddc 6f 6c 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  old table name *
41ddd 2f 0d 0a 20 20 69 6e 74 20 69 73 46 74 73 34 20  /..  int isFts4 
41dde 3d 20 28 61 72 67 76 5b 30 5d 5b 33 5d 3d 3d 27  = (argv[0][3]=='
41ddf 34 27 29 3b 20 2f 2a 20 54 72 75 65 20 66 6f 72  4'); /* True for
41de0 20 46 54 53 34 2c 20 66 61 6c 73 65 20 66 6f 72   FTS4, false for
41de1 20 46 54 53 33 20 2a 2f 0d 0a 20 20 63 6f 6e 73   FTS3 */..  cons
41de2 74 20 63 68 61 72 20 2a 2a 61 43 6f 6c 3b 20 20  t char **aCol;  
41de3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
41de4 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
41de5 61 6d 65 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ames */..  sqlit
41de6 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
41de7 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 20 20 20  okenizer = 0;   
41de8 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65       /* Tokenize
41de9 72 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65  r for this table
41dea 20 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 6e 49 6e   */....  int nIn
41deb 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  dex;            
41dec 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
41ded 20 6f 66 20 61 49 6e 64 65 78 5b 5d 20 61 72 72   of aIndex[] arr
41dee 61 79 20 2a 2f 0d 0a 20 20 73 74 72 75 63 74 20  ay */..  struct 
41def 46 74 73 33 49 6e 64 65 78 20 2a 61 49 6e 64 65  Fts3Index *aInde
41df0 78 20 3d 20 30 3b 20 20 20 2f 2a 20 41 72 72 61  x = 0;   /* Arra
41df1 79 20 6f 66 20 69 6e 64 65 78 65 73 20 66 6f 72  y of indexes for
41df2 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0d 0a   this table */..
41df3 0d 0a 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c  ..  /* The resul
41df4 74 73 20 6f 66 20 70 61 72 73 69 6e 67 20 73 75  ts of parsing su
41df5 70 70 6f 72 74 65 64 20 46 54 53 34 20 6b 65 79  pported FTS4 key
41df6 3d 76 61 6c 75 65 20 6f 70 74 69 6f 6e 73 3a 20  =value options: 
41df7 2a 2f 0d 0a 20 20 69 6e 74 20 62 4e 6f 44 6f 63  */..  int bNoDoc
41df8 73 69 7a 65 20 3d 20 30 3b 20 20 20 20 20 20 20  size = 0;       
41df9 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
41dfa 20 6f 6d 69 74 20 25 5f 64 6f 63 73 69 7a 65 20   omit %_docsize 
41dfb 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  table */..  int 
41dfc 62 44 65 73 63 49 64 78 20 3d 20 30 3b 20 20 20  bDescIdx = 0;   
41dfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
41dfe 72 75 65 20 74 6f 20 73 74 6f 72 65 20 64 65 73  rue to store des
41dff 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 65 73 20  cending indexes 
41e00 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 50 72 65  */..  char *zPre
41e01 66 69 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  fix = 0;        
41e02 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20        /* Prefix 
41e03 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65 20  parameter value 
41e04 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0d 0a 20 20  (or NULL) */..  
41e05 63 68 61 72 20 2a 7a 43 6f 6d 70 72 65 73 73 20  char *zCompress 
41e06 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
41e07 2f 2a 20 63 6f 6d 70 72 65 73 73 3d 3f 20 70 61  /* compress=? pa
41e08 72 61 6d 65 74 65 72 20 28 6f 72 20 4e 55 4c 4c  rameter (or NULL
41e09 29 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 55  ) */..  char *zU
41e0a 6e 63 6f 6d 70 72 65 73 73 20 3d 20 30 3b 20 20  ncompress = 0;  
41e0b 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 63 6f 6d          /* uncom
41e0c 70 72 65 73 73 3d 3f 20 70 61 72 61 6d 65 74 65  press=? paramete
41e0d 72 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0d 0a  r (or NULL) */..
41e0e 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 6e 74    char *zContent
41e0f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
41e10 20 20 2f 2a 20 63 6f 6e 74 65 6e 74 3d 3f 20 70    /* content=? p
41e11 61 72 61 6d 65 74 65 72 20 28 6f 72 20 4e 55 4c  arameter (or NUL
41e12 4c 29 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72  L) */....  asser
41e13 74 28 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 30  t( strlen(argv[0
41e14 5d 29 3d 3d 34 20 29 3b 0d 0a 20 20 61 73 73 65  ])==4 );..  asse
41e15 72 74 28 20 28 73 71 6c 69 74 65 33 5f 73 74 72  rt( (sqlite3_str
41e16 6e 69 63 6d 70 28 61 72 67 76 5b 30 5d 2c 20 22  nicmp(argv[0], "
41e17 66 74 73 34 22 2c 20 34 29 3d 3d 30 20 26 26 20  fts4", 4)==0 && 
41e18 69 73 46 74 73 34 29 0d 0a 20 20 20 20 20 20 20  isFts4)..       
41e19 7c 7c 20 28 73 71 6c 69 74 65 33 5f 73 74 72 6e  || (sqlite3_strn
41e1a 69 63 6d 70 28 61 72 67 76 5b 30 5d 2c 20 22 66  icmp(argv[0], "f
41e1b 74 73 33 22 2c 20 34 29 3d 3d 30 20 26 26 20 21  ts3", 4)==0 && !
41e1c 69 73 46 74 73 34 29 0d 0a 20 20 29 3b 0d 0a 0d  isFts4)..  );...
41e1d 0a 20 20 6e 44 62 20 3d 20 28 69 6e 74 29 73 74  .  nDb = (int)st
41e1e 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 20 2b 20  rlen(argv[1]) + 
41e1f 31 3b 0d 0a 20 20 6e 4e 61 6d 65 20 3d 20 28 69  1;..  nName = (i
41e20 6e 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 32  nt)strlen(argv[2
41e21 5d 29 20 2b 20 31 3b 0d 0a 0d 0a 20 20 61 43 6f  ]) + 1;....  aCo
41e22 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  l = (const char 
41e23 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
41e24 63 28 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63  c(sizeof(const c
41e25 68 61 72 20 2a 29 20 2a 20 28 61 72 67 63 2d 32  har *) * (argc-2
41e26 29 20 29 3b 0d 0a 20 20 69 66 28 20 21 61 43 6f  ) );..  if( !aCo
41e27 6c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  l ) return SQLIT
41e28 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 6d 65 6d 73  E_NOMEM;..  mems
41e29 65 74 28 28 76 6f 69 64 20 2a 29 61 43 6f 6c 2c  et((void *)aCol,
41e2a 20 30 2c 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74   0, sizeof(const
41e2b 20 63 68 61 72 20 2a 29 20 2a 20 28 61 72 67 63   char *) * (argc
41e2c 2d 32 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f  -2));....  /* Lo
41e2d 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 6f  op through all o
41e2e 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20  f the arguments 
41e2f 70 61 73 73 65 64 20 62 79 20 74 68 65 20 75 73  passed by the us
41e30 65 72 20 74 6f 20 74 68 65 20 46 54 53 33 2f 34  er to the FTS3/4
41e31 0d 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 28 69  ..  ** module (i
41e32 2e 65 2e 20 61 6c 6c 20 74 68 65 20 63 6f 6c 75  .e. all the colu
41e33 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 73 70 65  mn names and spe
41e34 63 69 61 6c 20 61 72 67 75 6d 65 6e 74 73 29 2e  cial arguments).
41e35 20 54 68 69 73 20 6c 6f 6f 70 0d 0a 20 20 2a 2a   This loop..  **
41e36 20 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77   does the follow
41e37 69 6e 67 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ing:..  **..  **
41e38 20 20 20 2b 20 46 69 67 75 72 65 73 20 6f 75 74     + Figures out
41e39 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
41e3a 6f 6c 75 6d 6e 73 20 74 68 65 20 46 54 53 58 20  olumns the FTSX 
41e3b 74 61 62 6c 65 20 77 69 6c 6c 20 68 61 76 65 2c  table will have,
41e3c 20 61 6e 64 0d 0a 20 20 2a 2a 20 20 20 20 20 74   and..  **     t
41e3d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
41e3e 65 73 20 6f 66 20 73 70 61 63 65 20 74 68 61 74  es of space that
41e3f 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74   must be allocat
41e40 65 64 20 74 6f 20 73 74 6f 72 65 20 63 6f 70 69  ed to store copi
41e41 65 73 0d 0a 20 20 2a 2a 20 20 20 20 20 6f 66 20  es..  **     of 
41e42 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
41e43 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20  ...  **..  **   
41e44 2b 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20  + If there is a 
41e45 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66  tokenizer specif
41e46 69 63 61 74 69 6f 6e 20 69 6e 63 6c 75 64 65 64  ication included
41e47 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74   in the argument
41e48 73 2c 0d 0a 20 20 2a 2a 20 20 20 20 20 69 6e 69  s,..  **     ini
41e49 74 69 61 6c 69 7a 65 73 20 74 68 65 20 74 6f 6b  tializes the tok
41e4a 65 6e 69 7a 65 72 20 70 54 6f 6b 65 6e 69 7a 65  enizer pTokenize
41e4b 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28  r...  */..  for(
41e4c 69 3d 33 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=3; rc==SQLITE_
41e4d 4f 4b 20 26 26 20 69 3c 61 72 67 63 3b 20 69 2b  OK && i<argc; i+
41e4e 2b 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 63 6f  +){..    char co
41e4f 6e 73 74 20 2a 7a 20 3d 20 61 72 67 76 5b 69 5d  nst *z = argv[i]
41e50 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b  ;..    int nKey;
41e51 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 61 6c  ..    char *zVal
41e52 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63  ;....    /* Chec
41e53 6b 20 69 66 20 74 68 69 73 20 69 73 20 61 20 74  k if this is a t
41e54 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66 69  okenizer specifi
41e55 63 61 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 69  cation */..    i
41e56 66 28 20 21 70 54 6f 6b 65 6e 69 7a 65 72 20 0d  f( !pTokenizer .
41e57 0a 20 20 20 20 20 26 26 20 73 74 72 6c 65 6e 28  .     && strlen(
41e58 7a 29 3e 38 0d 0a 20 20 20 20 20 26 26 20 30 3d  z)>8..     && 0=
41e59 3d 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d  =sqlite3_strnicm
41e5a 70 28 7a 2c 20 22 74 6f 6b 65 6e 69 7a 65 22 2c  p(z, "tokenize",
41e5b 20 38 29 20 0d 0a 20 20 20 20 20 26 26 20 30 3d   8) ..     && 0=
41e5c 3d 73 71 6c 69 74 65 33 46 74 73 33 49 73 49 64  =sqlite3Fts3IsId
41e5d 43 68 61 72 28 7a 5b 38 5d 29 0d 0a 20 20 20 20  Char(z[8])..    
41e5e 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ){..      rc = s
41e5f 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 54 6f  qlite3Fts3InitTo
41e60 6b 65 6e 69 7a 65 72 28 70 48 61 73 68 2c 20 26  kenizer(pHash, &
41e61 7a 5b 39 5d 2c 20 26 70 54 6f 6b 65 6e 69 7a 65  z[9], &pTokenize
41e62 72 2c 20 70 7a 45 72 72 29 3b 0d 0a 20 20 20 20  r, pzErr);..    
41e63 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63  }....    /* Chec
41e64 6b 20 69 66 20 69 74 20 69 73 20 61 6e 20 46 54  k if it is an FT
41e65 53 34 20 73 70 65 63 69 61 6c 20 61 72 67 75 6d  S4 special argum
41e66 65 6e 74 2e 20 2a 2f 0d 0a 20 20 20 20 65 6c 73  ent. */..    els
41e67 65 20 69 66 28 20 69 73 46 74 73 34 20 26 26 20  e if( isFts4 && 
41e68 66 74 73 33 49 73 53 70 65 63 69 61 6c 43 6f 6c  fts3IsSpecialCol
41e69 75 6d 6e 28 7a 2c 20 26 6e 4b 65 79 2c 20 26 7a  umn(z, &nKey, &z
41e6a 56 61 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 73  Val) ){..      s
41e6b 74 72 75 63 74 20 46 74 73 34 4f 70 74 69 6f 6e  truct Fts4Option
41e6c 20 7b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   {..        cons
41e6d 74 20 63 68 61 72 20 2a 7a 4f 70 74 3b 0d 0a 20  t char *zOpt;.. 
41e6e 20 20 20 20 20 20 20 69 6e 74 20 6e 4f 70 74 3b         int nOpt;
41e6f 0d 0a 20 20 20 20 20 20 7d 20 61 46 74 73 34 4f  ..      } aFts4O
41e70 70 74 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 20 20  pt[] = {..      
41e71 20 20 7b 20 22 6d 61 74 63 68 69 6e 66 6f 22 2c    { "matchinfo",
41e72 20 20 20 39 20 7d 2c 20 20 20 20 20 2f 2a 20 30     9 },     /* 0
41e73 20 2d 3e 20 4d 41 54 43 48 49 4e 46 4f 20 2a 2f   -> MATCHINFO */
41e74 0d 0a 20 20 20 20 20 20 20 20 7b 20 22 70 72 65  ..        { "pre
41e75 66 69 78 22 2c 20 20 20 20 20 20 36 20 7d 2c 20  fix",      6 }, 
41e76 20 20 20 20 2f 2a 20 31 20 2d 3e 20 50 52 45 46      /* 1 -> PREF
41e77 49 58 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7b  IX */..        {
41e78 20 22 63 6f 6d 70 72 65 73 73 22 2c 20 20 20 20   "compress",    
41e79 38 20 7d 2c 20 20 20 20 20 2f 2a 20 32 20 2d 3e  8 },     /* 2 ->
41e7a 20 43 4f 4d 50 52 45 53 53 20 2a 2f 0d 0a 20 20   COMPRESS */..  
41e7b 20 20 20 20 20 20 7b 20 22 75 6e 63 6f 6d 70 72        { "uncompr
41e7c 65 73 73 22 2c 20 31 30 20 7d 2c 20 20 20 20 20  ess", 10 },     
41e7d 2f 2a 20 33 20 2d 3e 20 55 4e 43 4f 4d 50 52 45  /* 3 -> UNCOMPRE
41e7e 53 53 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 7b  SS */..        {
41e7f 20 22 6f 72 64 65 72 22 2c 20 20 20 20 20 20 20   "order",       
41e80 35 20 7d 2c 20 20 20 20 20 2f 2a 20 34 20 2d 3e  5 },     /* 4 ->
41e81 20 4f 52 44 45 52 20 2a 2f 0d 0a 20 20 20 20 20   ORDER */..     
41e82 20 20 20 7b 20 22 63 6f 6e 74 65 6e 74 22 2c 20     { "content", 
41e83 20 20 20 20 37 20 7d 20 20 20 20 20 20 2f 2a 20      7 }      /* 
41e84 35 20 2d 3e 20 43 4f 4e 54 45 4e 54 20 2a 2f 0d  5 -> CONTENT */.
41e85 0a 20 20 20 20 20 20 7d 3b 0d 0a 0d 0a 20 20 20  .      };....   
41e86 20 20 20 69 6e 74 20 69 4f 70 74 3b 0d 0a 20 20     int iOpt;..  
41e87 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20 29 7b      if( !zVal ){
41e88 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ..        rc = S
41e89 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
41e8a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
41e8b 20 20 20 20 66 6f 72 28 69 4f 70 74 3d 30 3b 20      for(iOpt=0; 
41e8c 69 4f 70 74 3c 53 69 7a 65 6f 66 41 72 72 61 79  iOpt<SizeofArray
41e8d 28 61 46 74 73 34 4f 70 74 29 3b 20 69 4f 70 74  (aFts4Opt); iOpt
41e8e 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ++){..          
41e8f 73 74 72 75 63 74 20 46 74 73 34 4f 70 74 69 6f  struct Fts4Optio
41e90 6e 20 2a 70 4f 70 20 3d 20 26 61 46 74 73 34 4f  n *pOp = &aFts4O
41e91 70 74 5b 69 4f 70 74 5d 3b 0d 0a 20 20 20 20 20  pt[iOpt];..     
41e92 20 20 20 20 20 69 66 28 20 6e 4b 65 79 3d 3d 70       if( nKey==p
41e93 4f 70 2d 3e 6e 4f 70 74 20 26 26 20 21 73 71 6c  Op->nOpt && !sql
41e94 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 7a 2c  ite3_strnicmp(z,
41e95 20 70 4f 70 2d 3e 7a 4f 70 74 2c 20 70 4f 70 2d   pOp->zOpt, pOp-
41e96 3e 6e 4f 70 74 29 20 29 7b 0d 0a 20 20 20 20 20  >nOpt) ){..     
41e97 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20         break;.. 
41e98 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
41e99 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
41e9a 66 28 20 69 4f 70 74 3d 3d 53 69 7a 65 6f 66 41  f( iOpt==SizeofA
41e9b 72 72 61 79 28 61 46 74 73 34 4f 70 74 29 20 29  rray(aFts4Opt) )
41e9c 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a  {..          *pz
41e9d 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
41e9e 72 69 6e 74 66 28 22 75 6e 72 65 63 6f 67 6e 69  rintf("unrecogni
41e9f 7a 65 64 20 70 61 72 61 6d 65 74 65 72 3a 20 25  zed parameter: %
41ea0 73 22 2c 20 7a 29 3b 0d 0a 20 20 20 20 20 20 20  s", z);..       
41ea1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
41ea2 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20 20 7d  RROR;..        }
41ea3 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
41ea4 20 73 77 69 74 63 68 28 20 69 4f 70 74 20 29 7b   switch( iOpt ){
41ea5 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  ..            ca
41ea6 73 65 20 30 3a 20 20 20 20 20 20 20 20 20 20 20  se 0:           
41ea7 20 20 20 20 2f 2a 20 4d 41 54 43 48 49 4e 46 4f      /* MATCHINFO
41ea8 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20   */..           
41ea9 20 20 20 69 66 28 20 73 74 72 6c 65 6e 28 7a 56     if( strlen(zV
41eaa 61 6c 29 21 3d 34 20 7c 7c 20 73 71 6c 69 74 65  al)!=4 || sqlite
41eab 33 5f 73 74 72 6e 69 63 6d 70 28 7a 56 61 6c 2c  3_strnicmp(zVal,
41eac 20 22 66 74 73 33 22 2c 20 34 29 20 29 7b 0d 0a   "fts3", 4) ){..
41ead 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41eae 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
41eaf 5f 6d 70 72 69 6e 74 66 28 22 75 6e 72 65 63 6f  _mprintf("unreco
41eb0 67 6e 69 7a 65 64 20 6d 61 74 63 68 69 6e 66 6f  gnized matchinfo
41eb1 3a 20 25 73 22 2c 20 7a 56 61 6c 29 3b 0d 0a 20  : %s", zVal);.. 
41eb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
41eb3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
41eb4 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
41eb5 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20   }..            
41eb6 20 20 62 4e 6f 44 6f 63 73 69 7a 65 20 3d 20 31    bNoDocsize = 1
41eb7 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
41eb8 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20   break;....     
41eb9 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 20 20         case 1:  
41eba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41ebb 50 52 45 46 49 58 20 2a 2f 0d 0a 20 20 20 20 20  PREFIX */..     
41ebc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
41ebd 5f 66 72 65 65 28 7a 50 72 65 66 69 78 29 3b 0d  _free(zPrefix);.
41ebe 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a  .              z
41ebf 50 72 65 66 69 78 20 3d 20 7a 56 61 6c 3b 0d 0a  Prefix = zVal;..
41ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 56                zV
41ec1 61 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  al = 0;..       
41ec2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d         break;...
41ec3 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73  .            cas
41ec4 65 20 32 3a 20 20 20 20 20 20 20 20 20 20 20 20  e 2:            
41ec5 20 20 20 2f 2a 20 43 4f 4d 50 52 45 53 53 20 2a     /* COMPRESS *
41ec6 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  /..             
41ec7 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
41ec8 6f 6d 70 72 65 73 73 29 3b 0d 0a 20 20 20 20 20  ompress);..     
41ec9 20 20 20 20 20 20 20 20 20 7a 43 6f 6d 70 72 65           zCompre
41eca 73 73 20 3d 20 7a 56 61 6c 3b 0d 0a 20 20 20 20  ss = zVal;..    
41ecb 20 20 20 20 20 20 20 20 20 20 7a 56 61 6c 20 3d            zVal =
41ecc 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   0;..           
41ecd 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20     break;....   
41ece 20 20 20 20 20 20 20 20 20 63 61 73 65 20 33 3a           case 3:
41ecf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41ed0 2a 20 55 4e 43 4f 4d 50 52 45 53 53 20 2a 2f 0d  * UNCOMPRESS */.
41ed1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
41ed2 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 55 6e 63  qlite3_free(zUnc
41ed3 6f 6d 70 72 65 73 73 29 3b 0d 0a 20 20 20 20 20  ompress);..     
41ed4 20 20 20 20 20 20 20 20 20 7a 55 6e 63 6f 6d 70           zUncomp
41ed5 72 65 73 73 20 3d 20 7a 56 61 6c 3b 0d 0a 20 20  ress = zVal;..  
41ed6 20 20 20 20 20 20 20 20 20 20 20 20 7a 56 61 6c              zVal
41ed7 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 0;..         
41ed8 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20       break;.... 
41ed9 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20             case 
41eda 34 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4:              
41edb 20 2f 2a 20 4f 52 44 45 52 20 2a 2f 0d 0a 20 20   /* ORDER */..  
41edc 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
41edd 28 73 74 72 6c 65 6e 28 7a 56 61 6c 29 21 3d 33  (strlen(zVal)!=3
41ede 20 7c 7c 20 73 71 6c 69 74 65 33 5f 73 74 72 6e   || sqlite3_strn
41edf 69 63 6d 70 28 7a 56 61 6c 2c 20 22 61 73 63 22  icmp(zVal, "asc"
41ee0 2c 20 33 29 29 20 0d 0a 20 20 20 20 20 20 20 20  , 3)) ..        
41ee1 20 20 20 20 20 20 20 26 26 20 28 73 74 72 6c 65         && (strle
41ee2 6e 28 7a 56 61 6c 29 21 3d 34 20 7c 7c 20 73 71  n(zVal)!=4 || sq
41ee3 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 7a  lite3_strnicmp(z
41ee4 56 61 6c 2c 20 22 64 65 73 63 22 2c 20 34 29 29  Val, "desc", 4))
41ee5 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ..             
41ee6 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
41ee7 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71       *pzErr = sq
41ee8 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75  lite3_mprintf("u
41ee9 6e 72 65 63 6f 67 6e 69 7a 65 64 20 6f 72 64 65  nrecognized orde
41eea 72 3a 20 25 73 22 2c 20 7a 56 61 6c 29 3b 0d 0a  r: %s", zVal);..
41eeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41eec 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
41eed 52 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  R;..            
41eee 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
41eef 20 20 20 62 44 65 73 63 49 64 78 20 3d 20 28 7a     bDescIdx = (z
41ef0 56 61 6c 5b 30 5d 3d 3d 27 64 27 20 7c 7c 20 7a  Val[0]=='d' || z
41ef1 56 61 6c 5b 30 5d 3d 3d 27 44 27 29 3b 0d 0a 20  Val[0]=='D');.. 
41ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
41ef3 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 20  ak;....         
41ef4 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
41ef5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 4f 4e 54           /* CONT
41ef6 45 4e 54 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ENT */..        
41ef7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 4f        assert( iO
41ef8 70 74 3d 3d 35 20 29 3b 0d 0a 20 20 20 20 20 20  pt==5 );..      
41ef9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
41efa 66 72 65 65 28 7a 55 6e 63 6f 6d 70 72 65 73 73  free(zUncompress
41efb 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
41efc 20 20 7a 43 6f 6e 74 65 6e 74 20 3d 20 7a 56 61    zContent = zVa
41efd 6c 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  l;..            
41efe 20 20 7a 56 61 6c 20 3d 20 30 3b 0d 0a 20 20 20    zVal = 0;..   
41eff 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
41f00 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
41f01 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
41f02 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
41f03 7a 56 61 6c 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  zVal);..      }.
41f04 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
41f05 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
41f06 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 63 6f  argument is a co
41f07 6c 75 6d 6e 20 6e 61 6d 65 2e 20 2a 2f 0d 0a 20  lumn name. */.. 
41f08 20 20 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 20     else {..     
41f09 20 6e 53 74 72 69 6e 67 20 2b 3d 20 28 69 6e 74   nString += (int
41f0a 29 28 73 74 72 6c 65 6e 28 7a 29 20 2b 20 31 29  )(strlen(z) + 1)
41f0b 3b 0d 0a 20 20 20 20 20 20 61 43 6f 6c 5b 6e 43  ;..      aCol[nC
41f0c 6f 6c 2b 2b 5d 20 3d 20 7a 3b 0d 0a 20 20 20 20  ol++] = z;..    
41f0d 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49  }..  }....  /* I
41f0e 66 20 61 20 63 6f 6e 74 65 6e 74 3d 78 78 78 20  f a content=xxx 
41f0f 6f 70 74 69 6f 6e 20 77 61 73 20 73 70 65 63 69  option was speci
41f10 66 69 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  fied, the follow
41f11 69 6e 67 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ing:..  **..  **
41f12 20 20 20 31 2e 20 49 67 6e 6f 72 65 20 61 6e 79     1. Ignore any
41f13 20 63 6f 6d 70 72 65 73 73 3d 20 61 6e 64 20 75   compress= and u
41f14 6e 63 6f 6d 70 72 65 73 73 3d 20 6f 70 74 69 6f  ncompress= optio
41f15 6e 73 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  ns...  **..  ** 
41f16 20 20 32 2e 20 49 66 20 6e 6f 20 63 6f 6c 75 6d    2. If no colum
41f17 6e 20 6e 61 6d 65 73 20 77 65 72 65 20 73 70 65  n names were spe
41f18 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
41f19 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
41f1a 54 55 41 4c 0d 0a 20 20 2a 2a 20 20 20 20 20 20  TUAL..  **      
41f1b 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c  TABLE statement,
41f1c 20 75 73 65 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73   use all columns
41f1d 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e 74 65 6e   from the conten
41f1e 74 20 74 61 62 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a  t table...  */..
41f1f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
41f20 5f 4f 4b 20 26 26 20 7a 43 6f 6e 74 65 6e 74 20  _OK && zContent 
41f21 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
41f22 66 72 65 65 28 7a 43 6f 6d 70 72 65 73 73 29 3b  free(zCompress);
41f23 20 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   ..    sqlite3_f
41f24 72 65 65 28 7a 55 6e 63 6f 6d 70 72 65 73 73 29  ree(zUncompress)
41f25 3b 20 0d 0a 20 20 20 20 7a 43 6f 6d 70 72 65 73  ; ..    zCompres
41f26 73 20 3d 20 30 3b 0d 0a 20 20 20 20 7a 55 6e 63  s = 0;..    zUnc
41f27 6f 6d 70 72 65 73 73 20 3d 20 30 3b 0d 0a 20 20  ompress = 0;..  
41f28 20 20 69 66 28 20 6e 43 6f 6c 3d 3d 30 20 29 7b    if( nCol==0 ){
41f29 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
41f2a 66 72 65 65 28 28 76 6f 69 64 2a 29 61 43 6f 6c  free((void*)aCol
41f2b 29 3b 20 0d 0a 20 20 20 20 20 20 61 43 6f 6c 20  ); ..      aCol 
41f2c 3d 20 30 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  = 0;..      rc =
41f2d 20 66 74 73 33 43 6f 6e 74 65 6e 74 43 6f 6c 75   fts3ContentColu
41f2e 6d 6e 73 28 64 62 2c 20 61 72 67 76 5b 31 5d 2c  mns(db, argv[1],
41f2f 20 7a 43 6f 6e 74 65 6e 74 2c 20 26 61 43 6f 6c   zContent, &aCol
41f30 2c 20 26 6e 43 6f 6c 2c 20 26 6e 53 74 72 69 6e  , &nCol, &nStrin
41f31 67 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  g);..    }..    
41f32 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
41f33 54 45 5f 4f 4b 20 7c 7c 20 6e 43 6f 6c 3e 30 20  TE_OK || nCol>0 
41f34 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72  );..  }..  if( r
41f35 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
41f36 6f 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75  oto fts3_init_ou
41f37 74 3b 0d 0a 0d 0a 20 20 69 66 28 20 6e 43 6f 6c  t;....  if( nCol
41f38 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ==0 ){..    asse
41f39 72 74 28 20 6e 53 74 72 69 6e 67 3d 3d 30 20 29  rt( nString==0 )
41f3a 3b 0d 0a 20 20 20 20 61 43 6f 6c 5b 30 5d 20 3d  ;..    aCol[0] =
41f3b 20 22 63 6f 6e 74 65 6e 74 22 3b 0d 0a 20 20 20   "content";..   
41f3c 20 6e 53 74 72 69 6e 67 20 3d 20 38 3b 0d 0a 20   nString = 8;.. 
41f3d 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0d 0a 20 20     nCol = 1;..  
41f3e 7d 0d 0a 0d 0a 20 20 69 66 28 20 70 54 6f 6b 65  }....  if( pToke
41f3f 6e 69 7a 65 72 3d 3d 30 20 29 7b 0d 0a 20 20 20  nizer==0 ){..   
41f40 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
41f41 33 49 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28 70  3InitTokenizer(p
41f42 48 61 73 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20  Hash, "simple", 
41f43 26 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 70 7a 45  &pTokenizer, pzE
41f44 72 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  rr);..    if( rc
41f45 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
41f46 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74  to fts3_init_out
41f47 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
41f48 28 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 3b 0d  ( pTokenizer );.
41f49 0a 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 50 72  ...  rc = fts3Pr
41f4a 65 66 69 78 50 61 72 61 6d 65 74 65 72 28 7a 50  efixParameter(zP
41f4b 72 65 66 69 78 2c 20 26 6e 49 6e 64 65 78 2c 20  refix, &nIndex, 
41f4c 26 61 49 6e 64 65 78 29 3b 0d 0a 20 20 69 66 28  &aIndex);..  if(
41f4d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f   rc==SQLITE_ERRO
41f4e 52 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  R ){..    assert
41f4f 28 20 7a 50 72 65 66 69 78 20 29 3b 0d 0a 20 20  ( zPrefix );..  
41f50 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
41f51 65 33 5f 6d 70 72 69 6e 74 66 28 22 65 72 72 6f  e3_mprintf("erro
41f52 72 20 70 61 72 73 69 6e 67 20 70 72 65 66 69 78  r parsing prefix
41f53 20 70 61 72 61 6d 65 74 65 72 3a 20 25 73 22 2c   parameter: %s",
41f54 20 7a 50 72 65 66 69 78 29 3b 0d 0a 20 20 7d 0d   zPrefix);..  }.
41f55 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
41f56 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 74 73 33  E_OK ) goto fts3
41f57 5f 69 6e 69 74 5f 6f 75 74 3b 0d 0a 0d 0a 20 20  _init_out;....  
41f58 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  /* Allocate and 
41f59 70 6f 70 75 6c 61 74 65 20 74 68 65 20 46 74 73  populate the Fts
41f5a 33 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65  3Table structure
41f5b 2e 20 2a 2f 0d 0a 20 20 6e 42 79 74 65 20 3d 20  . */..  nByte = 
41f5c 73 69 7a 65 6f 66 28 46 74 73 33 54 61 62 6c 65  sizeof(Fts3Table
41f5d 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) +             
41f5e 20 20 20 20 20 2f 2a 20 46 74 73 33 54 61 62 6c       /* Fts3Tabl
41f5f 65 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20  e */..          
41f60 6e 43 6f 6c 20 2a 20 73 69 7a 65 6f 66 28 63 68  nCol * sizeof(ch
41f61 61 72 20 2a 29 20 2b 20 20 20 20 20 20 20 20 20  ar *) +         
41f62 20 20 20 20 20 2f 2a 20 61 7a 43 6f 6c 75 6d 6e       /* azColumn
41f63 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 6e   */..          n
41f64 49 6e 64 65 78 20 2a 20 73 69 7a 65 6f 66 28 73  Index * sizeof(s
41f65 74 72 75 63 74 20 46 74 73 33 49 6e 64 65 78 29  truct Fts3Index)
41f66 20 2b 20 20 2f 2a 20 61 49 6e 64 65 78 20 2a 2f   +  /* aIndex */
41f67 0d 0a 20 20 20 20 20 20 20 20 20 20 6e 4e 61 6d  ..          nNam
41f68 65 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  e +             
41f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41f6a 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0d 0a 20 20   /* zName */..  
41f6b 20 20 20 20 20 20 20 20 6e 44 62 20 2b 20 20 20          nDb +   
41f6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41f6d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41f6e 7a 44 62 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  zDb */..        
41f6f 20 20 6e 53 74 72 69 6e 67 3b 20 20 20 20 20 20    nString;      
41f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41f71 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
41f72 66 6f 72 20 61 7a 43 6f 6c 75 6d 6e 20 73 74 72  for azColumn str
41f73 69 6e 67 73 20 2a 2f 0d 0a 20 20 70 20 3d 20 28  ings */..  p = (
41f74 46 74 73 33 54 61 62 6c 65 2a 29 73 71 6c 69 74  Fts3Table*)sqlit
41f75 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  e3_malloc(nByte)
41f76 3b 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  ;..  if( p==0 ){
41f77 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
41f78 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 67 6f  E_NOMEM;..    go
41f79 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74  to fts3_init_out
41f7a 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 65 74  ;..  }..  memset
41f7b 28 70 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0d 0a  (p, 0, nByte);..
41f7c 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0d 0a 20    p->db = db;.. 
41f7d 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43   p->nColumn = nC
41f7e 6f 6c 3b 0d 0a 20 20 70 2d 3e 6e 50 65 6e 64 69  ol;..  p->nPendi
41f7f 6e 67 44 61 74 61 20 3d 20 30 3b 0d 0a 20 20 70  ngData = 0;..  p
41f80 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 28 63 68  ->azColumn = (ch
41f81 61 72 20 2a 2a 29 26 70 5b 31 5d 3b 0d 0a 20 20  ar **)&p[1];..  
41f82 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  p->pTokenizer = 
41f83 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 20 20 70  pTokenizer;..  p
41f84 2d 3e 6e 4d 61 78 50 65 6e 64 69 6e 67 44 61 74  ->nMaxPendingDat
41f85 61 20 3d 20 46 54 53 33 5f 4d 41 58 5f 50 45 4e  a = FTS3_MAX_PEN
41f86 44 49 4e 47 5f 44 41 54 41 3b 0d 0a 20 20 70 2d  DING_DATA;..  p-
41f87 3e 62 48 61 73 44 6f 63 73 69 7a 65 20 3d 20 28  >bHasDocsize = (
41f88 69 73 46 74 73 34 20 26 26 20 62 4e 6f 44 6f 63  isFts4 && bNoDoc
41f89 73 69 7a 65 3d 3d 30 29 3b 0d 0a 20 20 70 2d 3e  size==0);..  p->
41f8a 62 48 61 73 53 74 61 74 20 3d 20 69 73 46 74 73  bHasStat = isFts
41f8b 34 3b 0d 0a 20 20 70 2d 3e 62 44 65 73 63 49 64  4;..  p->bDescId
41f8c 78 20 3d 20 62 44 65 73 63 49 64 78 3b 0d 0a 20  x = bDescIdx;.. 
41f8d 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 20   p->zContentTbl 
41f8e 3d 20 7a 43 6f 6e 74 65 6e 74 3b 0d 0a 20 20 7a  = zContent;..  z
41f8f 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0d 0a 20 20  Content = 0;..  
41f90 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 69 6e 54  TESTONLY( p->inT
41f91 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 2d 31 20  ransaction = -1 
41f92 29 3b 0d 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20  );..  TESTONLY( 
41f93 70 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3d  p->mxSavepoint =
41f94 20 2d 31 20 29 3b 0d 0a 0d 0a 20 20 70 2d 3e 61   -1 );....  p->a
41f95 49 6e 64 65 78 20 3d 20 28 73 74 72 75 63 74 20  Index = (struct 
41f96 46 74 73 33 49 6e 64 65 78 20 2a 29 26 70 2d 3e  Fts3Index *)&p->
41f97 61 7a 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 3b 0d  azColumn[nCol];.
41f98 0a 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 49 6e  .  memcpy(p->aIn
41f99 64 65 78 2c 20 61 49 6e 64 65 78 2c 20 73 69 7a  dex, aIndex, siz
41f9a 65 6f 66 28 73 74 72 75 63 74 20 46 74 73 33 49  eof(struct Fts3I
41f9b 6e 64 65 78 29 20 2a 20 6e 49 6e 64 65 78 29 3b  ndex) * nIndex);
41f9c 0d 0a 20 20 70 2d 3e 6e 49 6e 64 65 78 20 3d 20  ..  p->nIndex = 
41f9d 6e 49 6e 64 65 78 3b 0d 0a 20 20 66 6f 72 28 69  nIndex;..  for(i
41f9e 3d 30 3b 20 69 3c 6e 49 6e 64 65 78 3b 20 69 2b  =0; i<nIndex; i+
41f9f 2b 29 7b 0d 0a 20 20 20 20 66 74 73 33 48 61 73  +){..    fts3Has
41fa0 68 49 6e 69 74 28 26 70 2d 3e 61 49 6e 64 65 78  hInit(&p->aIndex
41fa1 5b 69 5d 2e 68 50 65 6e 64 69 6e 67 2c 20 46 54  [i].hPending, FT
41fa2 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
41fa3 31 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  1);..  }....  /*
41fa4 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 7a 4e 61   Fill in the zNa
41fa5 6d 65 20 61 6e 64 20 7a 44 62 20 66 69 65 6c 64  me and zDb field
41fa6 73 20 6f 66 20 74 68 65 20 76 74 61 62 20 73 74  s of the vtab st
41fa7 72 75 63 74 75 72 65 2e 20 2a 2f 0d 0a 20 20 7a  ructure. */..  z
41fa8 43 73 72 20 3d 20 28 63 68 61 72 20 2a 29 26 70  Csr = (char *)&p
41fa9 2d 3e 61 49 6e 64 65 78 5b 6e 49 6e 64 65 78 5d  ->aIndex[nIndex]
41faa 3b 0d 0a 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20  ;..  p->zName = 
41fab 7a 43 73 72 3b 0d 0a 20 20 6d 65 6d 63 70 79 28  zCsr;..  memcpy(
41fac 7a 43 73 72 2c 20 61 72 67 76 5b 32 5d 2c 20 6e  zCsr, argv[2], n
41fad 4e 61 6d 65 29 3b 0d 0a 20 20 7a 43 73 72 20 2b  Name);..  zCsr +
41fae 3d 20 6e 4e 61 6d 65 3b 0d 0a 20 20 70 2d 3e 7a  = nName;..  p->z
41faf 44 62 20 3d 20 7a 43 73 72 3b 0d 0a 20 20 6d 65  Db = zCsr;..  me
41fb0 6d 63 70 79 28 7a 43 73 72 2c 20 61 72 67 76 5b  mcpy(zCsr, argv[
41fb1 31 5d 2c 20 6e 44 62 29 3b 0d 0a 20 20 7a 43 73  1], nDb);..  zCs
41fb2 72 20 2b 3d 20 6e 44 62 3b 0d 0a 0d 0a 20 20 2f  r += nDb;....  /
41fb3 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 61 7a  * Fill in the az
41fb4 43 6f 6c 75 6d 6e 20 61 72 72 61 79 20 2a 2f 0d  Column array */.
41fb5 0a 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69  .  for(iCol=0; i
41fb6 43 6f 6c 3c 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  Col<nCol; iCol++
41fb7 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  ){..    char *z;
41fb8 20 0d 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 30   ..    int n = 0
41fb9 3b 0d 0a 20 20 20 20 7a 20 3d 20 28 63 68 61 72  ;..    z = (char
41fba 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 4e 65   *)sqlite3Fts3Ne
41fbb 78 74 54 6f 6b 65 6e 28 61 43 6f 6c 5b 69 43 6f  xtToken(aCol[iCo
41fbc 6c 5d 2c 20 26 6e 29 3b 0d 0a 20 20 20 20 6d 65  l], &n);..    me
41fbd 6d 63 70 79 28 7a 43 73 72 2c 20 7a 2c 20 6e 29  mcpy(zCsr, z, n)
41fbe 3b 0d 0a 20 20 20 20 7a 43 73 72 5b 6e 5d 20 3d  ;..    zCsr[n] =
41fbf 20 27 5c 30 27 3b 0d 0a 20 20 20 20 73 71 6c 69   '\0';..    sqli
41fc0 74 65 33 46 74 73 33 44 65 71 75 6f 74 65 28 7a  te3Fts3Dequote(z
41fc1 43 73 72 29 3b 0d 0a 20 20 20 20 70 2d 3e 61 7a  Csr);..    p->az
41fc2 43 6f 6c 75 6d 6e 5b 69 43 6f 6c 5d 20 3d 20 7a  Column[iCol] = z
41fc3 43 73 72 3b 0d 0a 20 20 20 20 7a 43 73 72 20 2b  Csr;..    zCsr +
41fc4 3d 20 6e 2b 31 3b 0d 0a 20 20 20 20 61 73 73 65  = n+1;..    asse
41fc5 72 74 28 20 7a 43 73 72 20 3c 3d 20 26 28 28 63  rt( zCsr <= &((c
41fc6 68 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d 20  har *)p)[nByte] 
41fc7 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  );..  }....  if(
41fc8 20 28 7a 43 6f 6d 70 72 65 73 73 3d 3d 30 29 21   (zCompress==0)!
41fc9 3d 28 7a 55 6e 63 6f 6d 70 72 65 73 73 3d 3d 30  =(zUncompress==0
41fca 29 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 63  ) ){..    char c
41fcb 6f 6e 73 74 20 2a 7a 4d 69 73 73 20 3d 20 28 7a  onst *zMiss = (z
41fcc 43 6f 6d 70 72 65 73 73 3d 3d 30 20 3f 20 22 63  Compress==0 ? "c
41fcd 6f 6d 70 72 65 73 73 22 20 3a 20 22 75 6e 63 6f  ompress" : "unco
41fce 6d 70 72 65 73 73 22 29 3b 0d 0a 20 20 20 20 72  mpress");..    r
41fcf 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
41fd0 3b 0d 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ;..    *pzErr = 
41fd1 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
41fd2 22 6d 69 73 73 69 6e 67 20 25 73 20 70 61 72 61  "missing %s para
41fd3 6d 65 74 65 72 20 69 6e 20 66 74 73 34 20 63 6f  meter in fts4 co
41fd4 6e 73 74 72 75 63 74 6f 72 22 2c 20 7a 4d 69 73  nstructor", zMis
41fd5 73 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 2d 3e 7a  s);..  }..  p->z
41fd6 52 65 61 64 45 78 70 72 6c 69 73 74 20 3d 20 66  ReadExprlist = f
41fd7 74 73 33 52 65 61 64 45 78 70 72 4c 69 73 74 28  ts3ReadExprList(
41fd8 70 2c 20 7a 55 6e 63 6f 6d 70 72 65 73 73 2c 20  p, zUncompress, 
41fd9 26 72 63 29 3b 0d 0a 20 20 70 2d 3e 7a 57 72 69  &rc);..  p->zWri
41fda 74 65 45 78 70 72 6c 69 73 74 20 3d 20 66 74 73  teExprlist = fts
41fdb 33 57 72 69 74 65 45 78 70 72 4c 69 73 74 28 70  3WriteExprList(p
41fdc 2c 20 7a 43 6f 6d 70 72 65 73 73 2c 20 26 72 63  , zCompress, &rc
41fdd 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
41fde 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66  LITE_OK ) goto f
41fdf 74 73 33 5f 69 6e 69 74 5f 6f 75 74 3b 0d 0a 0d  ts3_init_out;...
41fe0 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
41fe1 20 61 6e 20 78 43 72 65 61 74 65 20 63 61 6c 6c   an xCreate call
41fe2 2c 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64  , create the und
41fe3 65 72 6c 79 69 6e 67 20 74 61 62 6c 65 73 20 69  erlying tables i
41fe4 6e 20 74 68 65 20 0d 0a 20 20 2a 2a 20 64 61 74  n the ..  ** dat
41fe5 61 62 61 73 65 2e 20 54 4f 44 4f 3a 20 46 6f 72  abase. TODO: For
41fe6 20 78 43 6f 6e 6e 65 63 74 28 29 2c 20 69 74 20   xConnect(), it 
41fe7 63 6f 75 6c 64 20 76 65 72 69 66 79 20 74 68 61  could verify tha
41fe8 74 20 73 61 69 64 20 74 61 62 6c 65 73 20 65 78  t said tables ex
41fe9 69 73 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66  ist...  */..  if
41fea 28 20 69 73 43 72 65 61 74 65 20 29 7b 0d 0a 20  ( isCreate ){.. 
41feb 20 20 20 72 63 20 3d 20 66 74 73 33 43 72 65 61     rc = fts3Crea
41fec 74 65 54 61 62 6c 65 73 28 70 29 3b 0d 0a 20 20  teTables(p);..  
41fed 7d 0d 0a 0d 0a 20 20 2f 2a 20 46 69 67 75 72 65  }....  /* Figure
41fee 20 6f 75 74 20 74 68 65 20 70 61 67 65 2d 73 69   out the page-si
41fef 7a 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ze for the datab
41ff0 61 73 65 2e 20 54 68 69 73 20 69 73 20 72 65 71  ase. This is req
41ff1 75 69 72 65 64 20 69 6e 20 6f 72 64 65 72 20 74  uired in order t
41ff2 6f 0d 0a 20 20 2a 2a 20 65 73 74 69 6d 61 74 65  o..  ** estimate
41ff3 20 74 68 65 20 63 6f 73 74 20 6f 66 20 6c 6f 61   the cost of loa
41ff4 64 69 6e 67 20 6c 61 72 67 65 20 64 6f 63 6c 69  ding large docli
41ff5 73 74 73 20 66 72 6f 6d 20 74 68 65 20 64 61 74  sts from the dat
41ff6 61 62 61 73 65 2e 20 20 2a 2f 0d 0a 20 20 66 74  abase.  */..  ft
41ff7 73 33 44 61 74 61 62 61 73 65 50 61 67 65 53 69  s3DatabasePageSi
41ff8 7a 65 28 26 72 63 2c 20 70 29 3b 0d 0a 20 20 70  ze(&rc, p);..  p
41ff9 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20 3d 20 70 2d  ->nNodeSize = p-
41ffa 3e 6e 50 67 73 7a 2d 33 35 3b 0d 0a 0d 0a 20 20  >nPgsz-35;....  
41ffb 2f 2a 20 44 65 63 6c 61 72 65 20 74 68 65 20 74  /* Declare the t
41ffc 61 62 6c 65 20 73 63 68 65 6d 61 20 74 6f 20 53  able schema to S
41ffd 51 4c 69 74 65 2e 20 2a 2f 0d 0a 20 20 66 74 73  QLite. */..  fts
41ffe 33 44 65 63 6c 61 72 65 56 74 61 62 28 26 72 63  3DeclareVtab(&rc
41fff 2c 20 70 29 3b 0d 0a 0d 0a 66 74 73 33 5f 69 6e  , p);....fts3_in
42000 69 74 5f 6f 75 74 3a 0d 0a 20 20 73 71 6c 69 74  it_out:..  sqlit
42001 65 33 5f 66 72 65 65 28 7a 50 72 65 66 69 78 29  e3_free(zPrefix)
42002 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
42003 65 28 61 49 6e 64 65 78 29 3b 0d 0a 20 20 73 71  e(aIndex);..  sq
42004 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 70  lite3_free(zComp
42005 72 65 73 73 29 3b 0d 0a 20 20 73 71 6c 69 74 65  ress);..  sqlite
42006 33 5f 66 72 65 65 28 7a 55 6e 63 6f 6d 70 72 65  3_free(zUncompre
42007 73 73 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ss);..  sqlite3_
42008 66 72 65 65 28 7a 43 6f 6e 74 65 6e 74 29 3b 0d  free(zContent);.
42009 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
4200a 28 76 6f 69 64 20 2a 29 61 43 6f 6c 29 3b 0d 0a  (void *)aCol);..
4200b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
4200c 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  _OK ){..    if( 
4200d 70 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33  p ){..      fts3
4200e 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64  DisconnectMethod
4200f 28 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  ((sqlite3_vtab *
42010 29 70 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20  )p);..    }else 
42011 69 66 28 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29  if( pTokenizer )
42012 7b 0d 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 69  {..      pTokeni
42013 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  zer->pModule->xD
42014 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
42015 72 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65  r);..    }..  }e
42016 6c 73 65 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  lse{..    assert
42017 28 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d  ( p->pSegments==
42018 30 20 29 3b 0d 0a 20 20 20 20 2a 70 70 56 54 61  0 );..    *ppVTa
42019 62 20 3d 20 26 70 2d 3e 62 61 73 65 3b 0d 0a 20  b = &p->base;.. 
4201a 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
4201b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
4201c 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 61 6e 64  e xConnect() and
4201d 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
4201e 64 73 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  ds for the virtu
4201f 61 6c 20 74 61 62 6c 65 2e 20 41 6c 6c 20 74 68  al table. All th
42020 65 0d 0a 2a 2a 20 77 6f 72 6b 20 69 73 20 64 6f  e..** work is do
42021 6e 65 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66  ne in function f
42022 74 73 33 49 6e 69 74 56 74 61 62 28 29 2e 0d 0a  ts3InitVtab()...
42023 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  */..static int f
42024 74 73 33 43 6f 6e 6e 65 63 74 4d 65 74 68 6f 64  ts3ConnectMethod
42025 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  (..  sqlite3 *db
42026 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
42027 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
42028 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a   connection */..
42029 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20    void *pAux,   
4202a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4202b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
4202c 74 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68 20 74  tokenizer hash t
4202d 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 61  able */..  int a
4202e 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgc,            
4202f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
42030 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
42031 20 69 6e 20 61 72 67 76 20 61 72 72 61 79 20 2a   in argv array *
42032 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
42033 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
42034 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 2f       /* xCreate/
42035 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d 65 6e  xConnect argumen
42036 74 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 73 71  t array */..  sq
42037 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
42038 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  tab,          /*
42039 20 4f 55 54 3a 20 4e 65 77 20 73 71 6c 69 74 65   OUT: New sqlite
4203a 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 2a 2f  3_vtab object */
4203b 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ..  char **pzErr
4203c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4203d 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73 71 6c 69      /* OUT: sqli
4203e 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20 65 72 72  te3_malloc'd err
4203f 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0d 0a 29  or message */..)
42040 7b 0d 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33  {..  return fts3
42041 49 6e 69 74 56 74 61 62 28 30 2c 20 64 62 2c 20  InitVtab(0, db, 
42042 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76  pAux, argc, argv
42043 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 29  , ppVtab, pzErr)
42044 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74  ;..}..static int
42045 20 66 74 73 33 43 72 65 61 74 65 4d 65 74 68 6f   fts3CreateMetho
42046 64 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  d(..  sqlite3 *d
42047 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
42048 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
42049 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0d  e connection */.
4204a 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20  .  void *pAux,  
4204b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4204c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
4204d 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68 20   tokenizer hash 
4204e 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  table */..  int 
4204f 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
42050 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
42051 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
42052 73 20 69 6e 20 61 72 67 76 20 61 72 72 61 79 20  s in argv array 
42053 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
42054 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20   * const *argv, 
42055 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65        /* xCreate
42056 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d 65  /xConnect argume
42057 6e 74 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 73  nt array */..  s
42058 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
42059 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  Vtab,          /
4205a 2a 20 4f 55 54 3a 20 4e 65 77 20 73 71 6c 69 74  * OUT: New sqlit
4205b 65 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 2a  e3_vtab object *
4205c 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  /..  char **pzEr
4205d 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
4205e 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73 71 6c       /* OUT: sql
4205f 69 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20 65 72  ite3_malloc'd er
42060 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0d 0a  ror message */..
42061 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 66 74 73  ){..  return fts
42062 33 49 6e 69 74 56 74 61 62 28 31 2c 20 64 62 2c  3InitVtab(1, db,
42063 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67   pAux, argc, arg
42064 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72  v, ppVtab, pzErr
42065 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a  );..}..../* ..**
42066 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
42067 6f 66 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  of the xBestInde
42068 78 20 6d 65 74 68 6f 64 20 66 6f 72 20 46 54 53  x method for FTS
42069 33 20 74 61 62 6c 65 73 2e 20 54 68 65 72 65 0d  3 tables. There.
4206a 0a 2a 2a 20 61 72 65 20 74 68 72 65 65 20 70 6f  .** are three po
4206b 73 73 69 62 6c 65 20 73 74 72 61 74 65 67 69 65  ssible strategie
4206c 73 2c 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 70  s, in order of p
4206d 72 65 66 65 72 65 6e 63 65 3a 0d 0a 2a 2a 0d 0a  reference:..**..
4206e 2a 2a 20 20 20 31 2e 20 44 69 72 65 63 74 20 6c  **   1. Direct l
4206f 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 20 6f  ookup by rowid o
42070 72 20 64 6f 63 69 64 2e 20 0d 0a 2a 2a 20 20 20  r docid. ..**   
42071 32 2e 20 46 75 6c 6c 2d 74 65 78 74 20 73 65 61  2. Full-text sea
42072 72 63 68 20 75 73 69 6e 67 20 61 20 4d 41 54 43  rch using a MATC
42073 48 20 6f 70 65 72 61 74 6f 72 20 6f 6e 20 61 20  H operator on a 
42074 6e 6f 6e 2d 64 6f 63 69 64 20 63 6f 6c 75 6d 6e  non-docid column
42075 2e 0d 0a 2a 2a 20 20 20 33 2e 20 4c 69 6e 65 61  ...**   3. Linea
42076 72 20 73 63 61 6e 20 6f 66 20 25 5f 63 6f 6e 74  r scan of %_cont
42077 65 6e 74 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a  ent table...*/..
42078 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42  static int fts3B
42079 65 73 74 49 6e 64 65 78 4d 65 74 68 6f 64 28 73  estIndexMethod(s
4207a 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
4207b 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ab, sqlite3_inde
4207c 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0d  x_info *pInfo){.
4207d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20  .  Fts3Table *p 
4207e 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70  = (Fts3Table *)p
4207f 56 54 61 62 3b 0d 0a 20 20 69 6e 74 20 69 3b 20  VTab;..  int i; 
42080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42081 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
42082 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
42083 0d 0a 20 20 69 6e 74 20 69 43 6f 6e 73 20 3d 20  ..  int iCons = 
42084 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  -1;             
42085 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
42086 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 75 73  constraint to us
42087 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 42 79 20  e */....  /* By 
42088 64 65 66 61 75 6c 74 20 75 73 65 20 61 20 66 75  default use a fu
42089 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 20 54  ll table scan. T
4208a 68 69 73 20 69 73 20 61 6e 20 65 78 70 65 6e 73  his is an expens
4208b 69 76 65 20 6f 70 74 69 6f 6e 2c 0d 0a 20 20 2a  ive option,..  *
4208c 2a 20 73 6f 20 73 65 61 72 63 68 20 74 68 72 6f  * so search thro
4208d 75 67 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69  ugh the constrai
4208e 6e 74 73 20 74 6f 20 73 65 65 20 69 66 20 61 20  nts to see if a 
4208f 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 0d  more efficient .
42090 0a 20 20 2a 2a 20 73 74 72 61 74 65 67 79 20 69  .  ** strategy i
42091 73 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 20 20 2a  s possible...  *
42092 2f 0d 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e  /..  pInfo->idxN
42093 75 6d 20 3d 20 46 54 53 33 5f 46 55 4c 4c 53 43  um = FTS3_FULLSC
42094 41 4e 5f 53 45 41 52 43 48 3b 0d 0a 20 20 70 49  AN_SEARCH;..  pI
42095 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
42096 73 74 20 3d 20 35 30 30 30 30 30 3b 0d 0a 20 20  st = 500000;..  
42097 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f  for(i=0; i<pInfo
42098 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
42099 2b 2b 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74  ++){..    struct
4209a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
4209b 6f 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73  onstraint *pCons
4209c 20 3d 20 26 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73   = &pInfo->aCons
4209d 74 72 61 69 6e 74 5b 69 5d 3b 0d 0a 20 20 20 20  traint[i];..    
4209e 69 66 28 20 70 43 6f 6e 73 2d 3e 75 73 61 62 6c  if( pCons->usabl
4209f 65 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  e==0 ) continue;
420a0 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 20 64 69 72  ....    /* A dir
420a1 65 63 74 20 6c 6f 6f 6b 75 70 20 6f 6e 20 74 68  ect lookup on th
420a2 65 20 72 6f 77 69 64 20 6f 72 20 64 6f 63 69 64  e rowid or docid
420a3 20 63 6f 6c 75 6d 6e 2e 20 41 73 73 69 67 6e 20   column. Assign 
420a4 61 20 63 6f 73 74 20 6f 66 20 31 2e 30 2e 20 2a  a cost of 1.0. *
420a5 2f 0d 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73  /..    if( pCons
420a6 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  ->op==SQLITE_IND
420a7 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51  EX_CONSTRAINT_EQ
420a8 20 0d 0a 20 20 20 20 20 26 26 20 28 70 43 6f 6e   ..     && (pCon
420a9 73 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20  s->iColumn<0 || 
420aa 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  pCons->iColumn==
420ab 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 0d 0a  p->nColumn+1 )..
420ac 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 70 49      ){..      pI
420ad 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 46 54  nfo->idxNum = FT
420ae 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 3b  S3_DOCID_SEARCH;
420af 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65  ..      pInfo->e
420b0 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 31  stimatedCost = 1
420b1 2e 30 3b 0d 0a 20 20 20 20 20 20 69 43 6f 6e 73  .0;..      iCons
420b2 20 3d 20 69 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a   = i;..    }....
420b3 20 20 20 20 2f 2a 20 41 20 4d 41 54 43 48 20 63      /* A MATCH c
420b4 6f 6e 73 74 72 61 69 6e 74 2e 20 55 73 65 20 61  onstraint. Use a
420b5 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
420b6 68 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20  h...    **..    
420b7 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6d  ** If there is m
420b8 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 4d 41 54  ore than one MAT
420b9 43 48 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 76  CH constraint av
420ba 61 69 6c 61 62 6c 65 2c 20 75 73 65 20 74 68 65  ailable, use the
420bb 20 66 69 72 73 74 0d 0a 20 20 20 20 2a 2a 20 6f   first..    ** o
420bc 6e 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  ne encountered. 
420bd 49 66 20 74 68 65 72 65 20 69 73 20 62 6f 74 68  If there is both
420be 20 61 20 4d 41 54 43 48 20 63 6f 6e 73 74 72 61   a MATCH constra
420bf 69 6e 74 20 61 6e 64 20 61 20 64 69 72 65 63 74  int and a direct
420c0 0d 0a 20 20 20 20 2a 2a 20 72 6f 77 69 64 2f 64  ..    ** rowid/d
420c1 6f 63 69 64 20 6c 6f 6f 6b 75 70 2c 20 70 72 65  ocid lookup, pre
420c2 66 65 72 20 74 68 65 20 4d 41 54 43 48 20 73 74  fer the MATCH st
420c3 72 61 74 65 67 79 2e 20 54 68 69 73 20 69 73 20  rategy. This is 
420c4 64 6f 6e 65 20 65 76 65 6e 20 0d 0a 20 20 20 20  done even ..    
420c5 2a 2a 20 74 68 6f 75 67 68 20 74 68 65 20 72 6f  ** though the ro
420c6 77 69 64 2f 64 6f 63 69 64 20 6c 6f 6f 6b 75 70  wid/docid lookup
420c7 20 69 73 20 66 61 73 74 65 72 20 74 68 61 6e 20   is faster than 
420c8 61 20 4d 41 54 43 48 20 71 75 65 72 79 2c 20 73  a MATCH query, s
420c9 65 6c 65 63 74 69 6e 67 0d 0a 20 20 20 20 2a 2a  electing..    **
420ca 20 69 74 20 77 6f 75 6c 64 20 6c 65 61 64 20 74   it would lead t
420cb 6f 20 61 6e 20 22 75 6e 61 62 6c 65 20 74 6f 20  o an "unable to 
420cc 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 4d 41 54  use function MAT
420cd 43 48 20 69 6e 20 74 68 65 20 72 65 71 75 65 73  CH in the reques
420ce 74 65 64 20 0d 0a 20 20 20 20 2a 2a 20 63 6f 6e  ted ..    ** con
420cf 74 65 78 74 22 20 65 72 72 6f 72 2e 0d 0a 20 20  text" error...  
420d0 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 43    */..    if( pC
420d1 6f 6e 73 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f  ons->op==SQLITE_
420d2 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
420d3 5f 4d 41 54 43 48 20 0d 0a 20 20 20 20 20 26 26  _MATCH ..     &&
420d4 20 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3e   pCons->iColumn>
420d5 3d 30 20 26 26 20 70 43 6f 6e 73 2d 3e 69 43 6f  =0 && pCons->iCo
420d6 6c 75 6d 6e 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e  lumn<=p->nColumn
420d7 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20  ..    ){..      
420d8 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  pInfo->idxNum = 
420d9 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
420da 41 52 43 48 20 2b 20 70 43 6f 6e 73 2d 3e 69 43  ARCH + pCons->iC
420db 6f 6c 75 6d 6e 3b 0d 0a 20 20 20 20 20 20 70 49  olumn;..      pI
420dc 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
420dd 73 74 20 3d 20 32 2e 30 3b 0d 0a 20 20 20 20 20  st = 2.0;..     
420de 20 69 43 6f 6e 73 20 3d 20 69 3b 0d 0a 20 20 20   iCons = i;..   
420df 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
420e0 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 69  ..  }....  if( i
420e1 43 6f 6e 73 3e 3d 30 20 29 7b 0d 0a 20 20 20 20  Cons>=0 ){..    
420e2 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
420e3 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d 2e 61  ntUsage[iCons].a
420e4 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0d 0a 20  rgvIndex = 1;.. 
420e5 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74     pInfo->aConst
420e6 72 61 69 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73  raintUsage[iCons
420e7 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0d 0a 20 20 7d  ].omit = 1;..  }
420e8 20 0d 0a 0d 0a 20 20 2f 2a 20 52 65 67 61 72 64   ....  /* Regard
420e9 6c 65 73 73 20 6f 66 20 74 68 65 20 73 74 72 61  less of the stra
420ea 74 65 67 79 20 73 65 6c 65 63 74 65 64 2c 20 46  tegy selected, F
420eb 54 53 20 63 61 6e 20 64 65 6c 69 76 65 72 20 72  TS can deliver r
420ec 6f 77 73 20 69 6e 20 72 6f 77 69 64 20 28 6f 72  ows in rowid (or
420ed 0d 0a 20 20 2a 2a 20 64 6f 63 69 64 29 20 6f 72  ..  ** docid) or
420ee 64 65 72 2e 20 42 6f 74 68 20 61 73 63 65 6e 64  der. Both ascend
420ef 69 6e 67 20 61 6e 64 20 64 65 73 63 65 6e 64 69  ing and descendi
420f0 6e 67 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e  ng are possible.
420f1 20 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 70   ..  */..  if( p
420f2 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 3d 3d  Info->nOrderBy==
420f3 31 20 29 7b 0d 0a 20 20 20 20 73 74 72 75 63 74  1 ){..    struct
420f4 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
420f5 72 64 65 72 62 79 20 2a 70 4f 72 64 65 72 20 3d  rderby *pOrder =
420f6 20 26 70 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42   &pInfo->aOrderB
420f7 79 5b 30 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70  y[0];..    if( p
420f8 4f 72 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30  Order->iColumn<0
420f9 20 7c 7c 20 70 4f 72 64 65 72 2d 3e 69 43 6f 6c   || pOrder->iCol
420fa 75 6d 6e 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b  umn==p->nColumn+
420fb 31 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28 20  1 ){..      if( 
420fc 70 4f 72 64 65 72 2d 3e 64 65 73 63 20 29 7b 0d  pOrder->desc ){.
420fd 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  .        pInfo->
420fe 69 64 78 53 74 72 20 3d 20 22 44 45 53 43 22 3b  idxStr = "DESC";
420ff 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  ..      }else{..
42100 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69          pInfo->i
42101 64 78 53 74 72 20 3d 20 22 41 53 43 22 3b 0d 0a  dxStr = "ASC";..
42102 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70        }..      p
42103 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e  Info->orderByCon
42104 73 75 6d 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20  sumed = 1;..    
42105 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65  }..  }....  asse
42106 72 74 28 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73  rt( p->pSegments
42107 3d 3d 30 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e  ==0 );..  return
42108 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
42109 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65  ../*..** Impleme
4210a 6e 74 61 74 69 6f 6e 20 6f 66 20 78 4f 70 65 6e  ntation of xOpen
4210b 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74   method...*/..st
4210c 61 74 69 63 20 69 6e 74 20 66 74 73 33 4f 70 65  atic int fts3Ope
4210d 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  nMethod(sqlite3_
4210e 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c  vtab *pVTab, sql
4210f 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
42110 20 2a 2a 70 70 43 73 72 29 7b 0d 0a 20 20 73 71   **ppCsr){..  sq
42111 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
42112 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20  r *pCsr;        
42113 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61         /* Alloca
42114 74 65 64 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 0d  ted cursor */...
42115 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
42116 54 45 52 28 70 56 54 61 62 29 3b 0d 0a 0d 0a 20  TER(pVTab);.... 
42117 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62   /* Allocate a b
42118 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75  uffer large enou
42119 67 68 20 66 6f 72 20 61 6e 20 46 74 73 33 43 75  gh for an Fts3Cu
4211a 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20  rsor structure. 
4211b 49 66 20 74 68 65 0d 0a 20 20 2a 2a 20 61 6c 6c  If the..  ** all
4211c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 65 64 73  ocation succeeds
4211d 2c 20 7a 65 72 6f 20 69 74 20 61 6e 64 20 72 65  , zero it and re
4211e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  turn SQLITE_OK. 
4211f 4f 74 68 65 72 77 69 73 65 2c 20 0d 0a 20 20 2a  Otherwise, ..  *
42120 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  * if the allocat
42121 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72  ion fails, retur
42122 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0d  n SQLITE_NOMEM..
42123 0a 20 20 2a 2f 0d 0a 20 20 2a 70 70 43 73 72 20  .  */..  *ppCsr 
42124 3d 20 70 43 73 72 20 3d 20 28 73 71 6c 69 74 65  = pCsr = (sqlite
42125 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 29  3_vtab_cursor *)
42126 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
42127 69 7a 65 6f 66 28 46 74 73 33 43 75 72 73 6f 72  izeof(Fts3Cursor
42128 29 29 3b 0d 0a 20 20 69 66 28 20 21 70 43 73 72  ));..  if( !pCsr
42129 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
4212a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
4212b 20 7d 0d 0a 20 20 6d 65 6d 73 65 74 28 70 43 73   }..  memset(pCs
4212c 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73  r, 0, sizeof(Fts
4212d 33 43 75 72 73 6f 72 29 29 3b 0d 0a 20 20 72 65  3Cursor));..  re
4212e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
4212f 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f  .}..../*..** Clo
42130 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  se the cursor.  
42131 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  For additional i
42132 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 65 65 20 74  nformation see t
42133 68 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  he documentation
42134 0d 0a 2a 2a 20 6f 6e 20 74 68 65 20 78 43 6c 6f  ..** on the xClo
42135 73 65 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  se method of the
42136 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
42137 6e 74 65 72 66 61 63 65 2e 0d 0a 2a 2f 0d 0a 73  nterface...*/..s
42138 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 6c  tatic int fts3Cl
42139 6f 73 65 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  oseMethod(sqlite
4213a 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
4213b 43 75 72 73 6f 72 29 7b 0d 0a 20 20 46 74 73 33  Cursor){..  Fts3
4213c 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
4213d 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43 75  Fts3Cursor *)pCu
4213e 72 73 6f 72 3b 0d 0a 20 20 61 73 73 65 72 74 28  rsor;..  assert(
4213f 20 28 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70   ((Fts3Table *)p
42140 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 29  Csr->base.pVtab)
42141 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20 29  ->pSegments==0 )
42142 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  ;..  sqlite3_fin
42143 61 6c 69 7a 65 28 70 43 73 72 2d 3e 70 53 74 6d  alize(pCsr->pStm
42144 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 46 74  t);..  sqlite3Ft
42145 73 33 45 78 70 72 46 72 65 65 28 70 43 73 72 2d  s3ExprFree(pCsr-
42146 3e 70 45 78 70 72 29 3b 0d 0a 20 20 73 71 6c 69  >pExpr);..  sqli
42147 74 65 33 46 74 73 33 46 72 65 65 44 65 66 65 72  te3Fts3FreeDefer
42148 72 65 64 54 6f 6b 65 6e 73 28 70 43 73 72 29 3b  redTokens(pCsr);
42149 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
4214a 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 29  (pCsr->aDoclist)
4214b 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
4214c 65 28 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e  e(pCsr->aMatchin
4214d 66 6f 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  fo);..  assert( 
4214e 28 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43  ((Fts3Table *)pC
4214f 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 29 2d  sr->base.pVtab)-
42150 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20 29 3b  >pSegments==0 );
42151 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
42152 28 70 43 73 72 29 3b 0d 0a 20 20 72 65 74 75 72  (pCsr);..  retur
42153 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
42154 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 70 43 73  .../*..** If pCs
42155 72 2d 3e 70 53 74 6d 74 20 68 61 73 20 6e 6f 74  r->pStmt has not
42156 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 28   been prepared (
42157 69 2e 65 2e 20 69 66 20 70 43 73 72 2d 3e 70 53  i.e. if pCsr->pS
42158 74 6d 74 3d 3d 30 29 2c 20 74 68 65 6e 0d 0a 2a  tmt==0), then..*
42159 2a 20 63 6f 6d 70 6f 73 65 20 61 6e 64 20 70 72  * compose and pr
4215a 65 70 61 72 65 20 61 6e 20 53 51 4c 20 73 74 61  epare an SQL sta
4215b 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f  tement of the fo
4215c 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 22  rm:..**..**    "
4215d 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e 73 3e  SELECT <columns>
4215e 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 20   FROM %_content 
4215f 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 3f 22  WHERE rowid = ?"
42160 0d 0a 2a 2a 0d 0a 2a 2a 20 28 6f 72 20 74 68 65  ..**..** (or the
42161 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20   equivalent for 
42162 61 20 63 6f 6e 74 65 6e 74 3d 78 78 78 20 74 61  a content=xxx ta
42163 62 6c 65 29 20 61 6e 64 20 73 65 74 20 70 43 73  ble) and set pCs
42164 72 2d 3e 70 53 74 6d 74 20 74 6f 0d 0a 2a 2a 20  r->pStmt to..** 
42165 69 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  it. If an error 
42166 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61  occurs, return a
42167 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
42168 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68  ode...**..** Oth
42169 65 72 77 69 73 65 2c 20 73 65 74 20 2a 70 70 53  erwise, set *ppS
4216a 74 6d 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  tmt to point to 
4216b 70 43 73 72 2d 3e 70 53 74 6d 74 20 61 6e 64 20  pCsr->pStmt and 
4216c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
4216d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
4216e 74 20 66 74 73 33 43 75 72 73 6f 72 53 65 65 6b  t fts3CursorSeek
4216f 53 74 6d 74 28 46 74 73 33 43 75 72 73 6f 72 20  Stmt(Fts3Cursor 
42170 2a 70 43 73 72 2c 20 73 71 6c 69 74 65 33 5f 73  *pCsr, sqlite3_s
42171 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0d 0a  tmt **ppStmt){..
42172 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
42173 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 43 73  E_OK;..  if( pCs
42174 72 2d 3e 70 53 74 6d 74 3d 3d 30 20 29 7b 0d 0a  r->pStmt==0 ){..
42175 20 20 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70      Fts3Table *p
42176 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
42177 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
42178 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71  ;..    char *zSq
42179 6c 3b 0d 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73  l;..    zSql = s
4217a 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
4217b 53 45 4c 45 43 54 20 25 73 20 57 48 45 52 45 20  SELECT %s WHERE 
4217c 72 6f 77 69 64 20 3d 20 3f 22 2c 20 70 2d 3e 7a  rowid = ?", p->z
4217d 52 65 61 64 45 78 70 72 6c 69 73 74 29 3b 0d 0a  ReadExprlist);..
4217e 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 20      if( !zSql ) 
4217f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
42180 4d 45 4d 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73  MEM;..    rc = s
42181 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
42182 32 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d  2(p->db, zSql, -
42183 31 2c 20 26 70 43 73 72 2d 3e 70 53 74 6d 74 2c  1, &pCsr->pStmt,
42184 20 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65   0);..    sqlite
42185 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d 0a 20  3_free(zSql);.. 
42186 20 7d 0d 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20   }..  *ppStmt = 
42187 70 43 73 72 2d 3e 70 53 74 6d 74 3b 0d 0a 20 20  pCsr->pStmt;..  
42188 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
42189 0a 2f 2a 0d 0a 2a 2a 20 50 6f 73 69 74 69 6f 6e  ./*..** Position
4218a 20 74 68 65 20 70 43 73 72 2d 3e 70 53 74 6d 74   the pCsr->pStmt
4218b 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68   statement so th
4218c 61 74 20 69 74 20 69 73 20 6f 6e 20 74 68 65 20  at it is on the 
4218d 72 6f 77 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 25  row..** of the %
4218e 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 74  _content table t
4218f 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
42190 20 6c 61 73 74 20 6d 61 74 63 68 2e 20 20 52 65   last match.  Re
42191 74 75 72 6e 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f  turn..** SQLITE_
42192 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  OK on success.  
42193 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
42194 20 66 74 73 33 43 75 72 73 6f 72 53 65 65 6b 28   fts3CursorSeek(
42195 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
42196 2a 70 43 6f 6e 74 65 78 74 2c 20 46 74 73 33 43  *pContext, Fts3C
42197 75 72 73 6f 72 20 2a 70 43 73 72 29 7b 0d 0a 20  ursor *pCsr){.. 
42198 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
42199 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 43 73 72  _OK;..  if( pCsr
4219a 2d 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b 20  ->isRequireSeek 
4219b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
4219c 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
4219d 0d 0a 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ....    rc = fts
4219e 33 43 75 72 73 6f 72 53 65 65 6b 53 74 6d 74 28  3CursorSeekStmt(
4219f 70 43 73 72 2c 20 26 70 53 74 6d 74 29 3b 0d 0a  pCsr, &pStmt);..
421a0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
421a1 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
421a2 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
421a3 36 34 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20  64(pCsr->pStmt, 
421a4 31 2c 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64  1, pCsr->iPrevId
421a5 29 3b 0d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e  );..      pCsr->
421a6 69 73 52 65 71 75 69 72 65 53 65 65 6b 20 3d 20  isRequireSeek = 
421a7 30 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 53 51  0;..      if( SQ
421a8 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
421a9 33 5f 73 74 65 70 28 70 43 73 72 2d 3e 70 53 74  3_step(pCsr->pSt
421aa 6d 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  mt) ){..        
421ab 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
421ac 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
421ad 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
421ae 6c 69 74 65 33 5f 72 65 73 65 74 28 70 43 73 72  lite3_reset(pCsr
421af 2d 3e 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 20  ->pStmt);..     
421b0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
421b1 45 5f 4f 4b 20 26 26 20 28 28 46 74 73 33 54 61  E_OK && ((Fts3Ta
421b2 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65  ble *)pCsr->base
421b3 2e 70 56 74 61 62 29 2d 3e 7a 43 6f 6e 74 65 6e  .pVtab)->zConten
421b4 74 54 62 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  tTbl==0 ){..    
421b5 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 72        /* If no r
421b6 6f 77 20 77 61 73 20 66 6f 75 6e 64 20 61 6e 64  ow was found and
421b7 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20 6f 63   no error has oc
421b8 63 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  cured, then the 
421b9 25 5f 63 6f 6e 74 65 6e 74 0d 0a 20 20 20 20 20  %_content..     
421ba 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 73       ** table is
421bb 20 6d 69 73 73 69 6e 67 20 61 20 72 6f 77 20 74   missing a row t
421bc 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 20 69  hat is present i
421bd 6e 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  n the full-text 
421be 69 6e 64 65 78 2e 0d 0a 20 20 20 20 20 20 20 20  index...        
421bf 20 20 2a 2a 20 54 68 65 20 64 61 74 61 20 73 74    ** The data st
421c0 72 75 63 74 75 72 65 73 20 61 72 65 20 63 6f 72  ructures are cor
421c1 72 75 70 74 2e 20 20 2a 2f 0d 0a 20 20 20 20 20  rupt.  */..     
421c2 20 20 20 20 20 72 63 20 3d 20 46 54 53 5f 43 4f       rc = FTS_CO
421c3 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 20  RRUPT_VTAB;..   
421c4 20 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73 45         pCsr->isE
421c5 6f 66 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20  of = 1;..       
421c6 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
421c7 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28   }..  }....  if(
421c8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
421c9 26 20 70 43 6f 6e 74 65 78 74 20 29 7b 0d 0a 20  & pContext ){.. 
421ca 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
421cb 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 70 43 6f  t_error_code(pCo
421cc 6e 74 65 78 74 2c 20 72 63 29 3b 0d 0a 20 20 7d  ntext, rc);..  }
421cd 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
421ce 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
421cf 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
421d0 64 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 73  d to process a s
421d1 69 6e 67 6c 65 20 69 6e 74 65 72 69 6f 72 20 6e  ingle interior n
421d2 6f 64 65 20 77 68 65 6e 20 73 65 61 72 63 68 69  ode when searchi
421d3 6e 67 0d 0a 2a 2a 20 61 20 62 2d 74 72 65 65 20  ng..** a b-tree 
421d4 66 6f 72 20 61 20 74 65 72 6d 20 6f 72 20 74 65  for a term or te
421d5 72 6d 20 70 72 65 66 69 78 2e 20 54 68 65 20 6e  rm prefix. The n
421d6 6f 64 65 20 64 61 74 61 20 69 73 20 70 61 73 73  ode data is pass
421d7 65 64 20 74 6f 20 74 68 69 73 20 0d 0a 2a 2a 20  ed to this ..** 
421d8 66 75 6e 63 74 69 6f 6e 20 76 69 61 20 74 68 65  function via the
421d9 20 7a 4e 6f 64 65 2f 6e 4e 6f 64 65 20 70 61 72   zNode/nNode par
421da 61 6d 65 74 65 72 73 2e 20 54 68 65 20 74 65 72  ameters. The ter
421db 6d 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20  m to search for 
421dc 69 73 0d 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e  is..** passed in
421dd 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 0d 0a 2a   zTerm/nTerm...*
421de 2a 0d 0a 2a 2a 20 49 66 20 70 69 46 69 72 73 74  *..** If piFirst
421df 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
421e0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
421e1 20 73 65 74 73 20 2a 70 69 46 69 72 73 74 20 74   sets *piFirst t
421e2 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64 0d 0a 2a  o the blockid..*
421e3 2a 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6e  * of the child n
421e4 6f 64 65 20 74 68 61 74 20 68 65 61 64 73 20 74  ode that heads t
421e5 68 65 20 73 75 62 2d 74 72 65 65 20 74 68 61 74  he sub-tree that
421e6 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 74 68 65   may contain the
421e7 20 74 65 72 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   term...**..** I
421e8 66 20 70 69 4c 61 73 74 20 69 73 20 6e 6f 74 20  f piLast is not 
421e9 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70 69 4c 61  NULL, then *piLa
421ea 73 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  st is set to the
421eb 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c   right-most chil
421ec 64 20 6e 6f 64 65 0d 0a 2a 2a 20 74 68 61 74 20  d node..** that 
421ed 68 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65  heads a sub-tree
421ee 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69   that may contai
421ef 6e 20 61 20 74 65 72 6d 20 66 6f 72 20 77 68 69  n a term for whi
421f0 63 68 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 20 69  ch zTerm/nTerm i
421f1 73 0d 0a 2a 2a 20 61 20 70 72 65 66 69 78 2e 0d  s..** a prefix..
421f2 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f  .**..** If an OO
421f3 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
421f4 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
421f5 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
421f6 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0d  ise, SQLITE_OK..
421f7 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
421f8 66 74 73 33 53 63 61 6e 49 6e 74 65 72 69 6f 72  fts3ScanInterior
421f9 4e 6f 64 65 28 0d 0a 20 20 63 6f 6e 73 74 20 63  Node(..  const c
421fa 68 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20  har *zTerm,     
421fb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d           /* Term
421fc 20 74 6f 20 73 65 6c 65 63 74 20 6c 65 61 76 65   to select leave
421fd 73 20 66 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  s for */..  int 
421fe 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  nTerm,          
421ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
42200 69 7a 65 20 6f 66 20 74 65 72 6d 20 7a 54 65 72  ize of term zTer
42201 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20  m in bytes */.. 
42202 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 6f   const char *zNo
42203 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  de,             
42204 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61   /* Buffer conta
42205 69 6e 69 6e 67 20 73 65 67 6d 65 6e 74 20 69 6e  ining segment in
42206 74 65 72 69 6f 72 20 6e 6f 64 65 20 2a 2f 0d 0a  terior node */..
42207 20 20 69 6e 74 20 6e 4e 6f 64 65 2c 20 20 20 20    int nNode,    
42208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42209 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
4220a 66 65 72 20 61 74 20 7a 4e 6f 64 65 20 2a 2f 0d  fer at zNode */.
4220b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
4220c 20 2a 70 69 46 69 72 73 74 2c 20 20 20 20 20 20   *piFirst,      
4220d 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c 65 63     /* OUT: Selec
4220e 74 65 64 20 63 68 69 6c 64 20 6e 6f 64 65 20 2a  ted child node *
4220f 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  /..  sqlite3_int
42210 36 34 20 2a 70 69 4c 61 73 74 20 20 20 20 20 20  64 *piLast      
42211 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c       /* OUT: Sel
42212 65 63 74 65 64 20 63 68 69 6c 64 20 6e 6f 64 65  ected child node
42213 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
42214 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
42215 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
42216 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20  turn code */..  
42217 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 73 72  const char *zCsr
42218 20 3d 20 7a 4e 6f 64 65 3b 20 20 20 20 20 20 20   = zNode;       
42219 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 69 74 65  /* Cursor to ite
4221a 72 61 74 65 20 74 68 72 6f 75 67 68 20 6e 6f 64  rate through nod
4221b 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68  e */..  const ch
4221c 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 73 72  ar *zEnd = &zCsr
4221d 5b 6e 4e 6f 64 65 5d 3b 2f 2a 20 45 6e 64 20 6f  [nNode];/* End o
4221e 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  f interior node 
4221f 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 63 68 61  buffer */..  cha
42220 72 20 2a 7a 42 75 66 66 65 72 20 3d 20 30 3b 20  r *zBuffer = 0; 
42221 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42222 42 75 66 66 65 72 20 74 6f 20 6c 6f 61 64 20 74  Buffer to load t
42223 65 72 6d 73 20 69 6e 74 6f 20 2a 2f 0d 0a 20 20  erms into */..  
42224 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 30 3b 20  int nAlloc = 0; 
42225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42226 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63  /* Size of alloc
42227 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0d 0a  ated buffer */..
42228 20 20 69 6e 74 20 69 73 46 69 72 73 74 54 65 72    int isFirstTer
42229 6d 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  m = 1;          
4222a 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e 20 70    /* True when p
4222b 72 6f 63 65 73 73 69 6e 67 20 66 69 72 73 74 20  rocessing first 
4222c 74 65 72 6d 20 6f 6e 20 70 61 67 65 20 2a 2f 0d  term on page */.
4222d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
4222e 20 69 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20   iChild;        
4222f 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20 6f     /* Block id o
42230 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 74 6f 20  f child node to 
42231 64 65 73 63 65 6e 64 20 74 6f 20 2a 2f 0d 0a 0d  descend to */...
42232 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20  .  /* Skip over 
42233 74 68 65 20 27 68 65 69 67 68 74 27 20 76 61 72  the 'height' var
42234 69 6e 74 20 74 68 61 74 20 6f 63 63 75 72 73 20  int that occurs 
42235 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
42236 65 76 65 72 79 20 0d 0a 20 20 2a 2a 20 69 6e 74  every ..  ** int
42237 65 72 69 6f 72 20 6e 6f 64 65 2e 20 54 68 65 6e  erior node. Then
42238 20 6c 6f 61 64 20 74 68 65 20 62 6c 6f 63 6b 69   load the blocki
42239 64 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 63 68  d of the left-ch
4223a 69 6c 64 20 6f 66 20 74 68 65 20 62 2d 74 72 65  ild of the b-tre
4223b 65 0d 0a 20 20 2a 2a 20 6e 6f 64 65 20 69 6e 74  e..  ** node int
4223c 6f 20 76 61 72 69 61 62 6c 65 20 69 43 68 69 6c  o variable iChil
4223d 64 2e 20 20 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  d.  ..  **..  **
4223e 20 45 76 65 6e 20 69 66 20 74 68 65 20 64 61 74   Even if the dat
4223f 61 20 73 74 72 75 63 74 75 72 65 20 6f 6e 20 64  a structure on d
42240 69 73 6b 20 69 73 20 63 6f 72 72 75 70 74 65 64  isk is corrupted
42241 2c 20 74 68 69 73 20 28 72 65 61 64 69 6e 67 20  , this (reading 
42242 74 77 6f 0d 0a 20 20 2a 2a 20 76 61 72 69 6e 74  two..  ** varint
42243 73 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  s from the buffe
42244 72 29 20 64 6f 65 73 20 6e 6f 74 20 72 69 73 6b  r) does not risk
42245 20 61 6e 20 6f 76 65 72 72 65 61 64 2e 20 49 66   an overread. If
42246 20 7a 4e 6f 64 65 20 69 73 20 61 0d 0a 20 20 2a   zNode is a..  *
42247 2a 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 74 68 65  * root node, the
42248 6e 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 6d  n the buffer com
42249 65 73 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  es from a SELECT
4224a 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 51 4c 69   statement. SQLi
4224b 74 65 20 64 6f 65 73 0d 0a 20 20 2a 2a 20 6e 6f  te does..  ** no
4224c 74 20 6d 61 6b 65 20 74 68 69 73 20 67 75 61 72  t make this guar
4224d 61 6e 74 65 65 20 65 78 70 6c 69 63 69 74 6c 79  antee explicitly
4224e 2c 20 62 75 74 20 69 6e 20 70 72 61 63 74 69 63  , but in practic
4224f 65 20 74 68 65 72 65 20 61 72 65 20 61 6c 77 61  e there are alwa
42250 79 73 0d 0a 20 20 2a 2a 20 65 69 74 68 65 72 20  ys..  ** either 
42251 6d 6f 72 65 20 74 68 61 6e 20 32 30 20 62 79 74  more than 20 byt
42252 65 73 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  es of allocated 
42253 73 70 61 63 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  space following 
42254 74 68 65 20 6e 4e 6f 64 65 20 62 79 74 65 73 20  the nNode bytes 
42255 6f 66 0d 0a 20 20 2a 2a 20 63 6f 6e 74 65 6e 74  of..  ** content
42256 73 2c 20 6f 72 20 74 77 6f 20 7a 65 72 6f 20 62  s, or two zero b
42257 79 74 65 73 2e 20 4f 72 2c 20 69 66 20 74 68 65  ytes. Or, if the
42258 20 6e 6f 64 65 20 69 73 20 72 65 61 64 20 66 72   node is read fr
42259 6f 6d 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  om the %_segment
4225a 73 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74  s..  ** table, t
4225b 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 61 6c  hen there are al
4225c 77 61 79 73 20 32 30 20 62 79 74 65 73 20 6f 66  ways 20 bytes of
4225d 20 7a 65 72 6f 65 64 20 70 61 64 64 69 6e 67 20   zeroed padding 
4225e 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 0d 0a 20  following the.. 
4225f 20 2a 2a 20 6e 4e 6f 64 65 20 62 79 74 65 73 20   ** nNode bytes 
42260 6f 66 20 63 6f 6e 74 65 6e 74 20 28 73 65 65 20  of content (see 
42261 73 71 6c 69 74 65 33 46 74 73 33 52 65 61 64 42  sqlite3Fts3ReadB
42262 6c 6f 63 6b 28 29 20 66 6f 72 20 64 65 74 61 69  lock() for detai
42263 6c 73 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 7a 43  ls)...  */..  zC
42264 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  sr += sqlite3Fts
42265 33 47 65 74 56 61 72 69 6e 74 28 7a 43 73 72 2c  3GetVarint(zCsr,
42266 20 26 69 43 68 69 6c 64 29 3b 0d 0a 20 20 7a 43   &iChild);..  zC
42267 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  sr += sqlite3Fts
42268 33 47 65 74 56 61 72 69 6e 74 28 7a 43 73 72 2c  3GetVarint(zCsr,
42269 20 26 69 43 68 69 6c 64 29 3b 0d 0a 20 20 69 66   &iChild);..  if
4226a 28 20 7a 43 73 72 3e 7a 45 6e 64 20 29 7b 0d 0a  ( zCsr>zEnd ){..
4226b 20 20 20 20 72 65 74 75 72 6e 20 46 54 53 5f 43      return FTS_C
4226c 4f 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20  ORRUPT_VTAB;..  
4226d 7d 0d 0a 20 20 0d 0a 20 20 77 68 69 6c 65 28 20  }..  ..  while( 
4226e 7a 43 73 72 3c 7a 45 6e 64 20 26 26 20 28 70 69  zCsr<zEnd && (pi
4226f 46 69 72 73 74 20 7c 7c 20 70 69 4c 61 73 74 29  First || piLast)
42270 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 63 6d 70   ){..    int cmp
42271 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
42272 20 20 20 20 20 20 20 2f 2a 20 6d 65 6d 63 6d 70         /* memcmp
42273 28 29 20 72 65 73 75 6c 74 20 2a 2f 0d 0a 20 20  () result */..  
42274 20 20 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20    int nSuffix;  
42275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42276 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20  /* Size of term 
42277 73 75 66 66 69 78 20 2a 2f 0d 0a 20 20 20 20 69  suffix */..    i
42278 6e 74 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 20  nt nPrefix = 0; 
42279 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4227a 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 70 72 65  Size of term pre
4227b 66 69 78 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20  fix */..    int 
4227c 6e 42 75 66 66 65 72 3b 20 20 20 20 20 20 20 20  nBuffer;        
4227d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
4227e 61 6c 20 74 65 72 6d 20 73 69 7a 65 20 2a 2f 0d  al term size */.
4227f 0a 20 20 0d 0a 20 20 20 20 2f 2a 20 4c 6f 61 64  .  ..    /* Load
42280 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f   the next term o
42281 6e 20 74 68 65 20 6e 6f 64 65 20 69 6e 74 6f 20  n the node into 
42282 7a 42 75 66 66 65 72 2e 20 55 73 65 20 72 65 61  zBuffer. Use rea
42283 6c 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64  lloc() to expand
42284 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 69 7a  ..    ** the siz
42285 65 20 6f 66 20 7a 42 75 66 66 65 72 20 69 66 20  e of zBuffer if 
42286 72 65 71 75 69 72 65 64 2e 20 20 2a 2f 0d 0a 20  required.  */.. 
42287 20 20 20 69 66 28 20 21 69 73 46 69 72 73 74 54     if( !isFirstT
42288 65 72 6d 20 29 7b 0d 0a 20 20 20 20 20 20 7a 43  erm ){..      zC
42289 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  sr += sqlite3Fts
4228a 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a 43 73  3GetVarint32(zCs
4228b 72 2c 20 26 6e 50 72 65 66 69 78 29 3b 0d 0a 20  r, &nPrefix);.. 
4228c 20 20 20 7d 0d 0a 20 20 20 20 69 73 46 69 72 73     }..    isFirs
4228d 74 54 65 72 6d 20 3d 20 30 3b 0d 0a 20 20 20 20  tTerm = 0;..    
4228e 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46  zCsr += sqlite3F
4228f 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a  ts3GetVarint32(z
42290 43 73 72 2c 20 26 6e 53 75 66 66 69 78 29 3b 0d  Csr, &nSuffix);.
42291 0a 20 20 20 20 0d 0a 20 20 20 20 69 66 28 20 6e  .    ..    if( n
42292 50 72 65 66 69 78 3c 30 20 7c 7c 20 6e 53 75 66  Prefix<0 || nSuf
42293 66 69 78 3c 30 20 7c 7c 20 26 7a 43 73 72 5b 6e  fix<0 || &zCsr[n
42294 53 75 66 66 69 78 5d 3e 7a 45 6e 64 20 29 7b 0d  Suffix]>zEnd ){.
42295 0a 20 20 20 20 20 20 72 63 20 3d 20 46 54 53 5f  .      rc = FTS_
42296 43 4f 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20  CORRUPT_VTAB;.. 
42297 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68       goto finish
42298 5f 73 63 61 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _scan;..    }.. 
42299 20 20 20 69 66 28 20 6e 50 72 65 66 69 78 2b 6e     if( nPrefix+n
4229a 53 75 66 66 69 78 3e 6e 41 6c 6c 6f 63 20 29 7b  Suffix>nAlloc ){
4229b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  ..      char *zN
4229c 65 77 3b 0d 0a 20 20 20 20 20 20 6e 41 6c 6c 6f  ew;..      nAllo
4229d 63 20 3d 20 28 6e 50 72 65 66 69 78 2b 6e 53 75  c = (nPrefix+nSu
4229e 66 66 69 78 29 20 2a 20 32 3b 0d 0a 20 20 20 20  ffix) * 2;..    
4229f 20 20 7a 4e 65 77 20 3d 20 28 63 68 61 72 20 2a    zNew = (char *
422a0 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  )sqlite3_realloc
422a1 28 7a 42 75 66 66 65 72 2c 20 6e 41 6c 6c 6f 63  (zBuffer, nAlloc
422a2 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 7a  );..      if( !z
422a3 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  New ){..        
422a4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
422a5 4d 3b 0d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  M;..        goto
422a6 20 66 69 6e 69 73 68 5f 73 63 61 6e 3b 0d 0a 20   finish_scan;.. 
422a7 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7a 42       }..      zB
422a8 75 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0d 0a 20  uffer = zNew;.. 
422a9 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65 72 74     }..    assert
422aa 28 20 7a 42 75 66 66 65 72 20 29 3b 0d 0a 20 20  ( zBuffer );..  
422ab 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 66 65    memcpy(&zBuffe
422ac 72 5b 6e 50 72 65 66 69 78 5d 2c 20 7a 43 73 72  r[nPrefix], zCsr
422ad 2c 20 6e 53 75 66 66 69 78 29 3b 0d 0a 20 20 20  , nSuffix);..   
422ae 20 6e 42 75 66 66 65 72 20 3d 20 6e 50 72 65 66   nBuffer = nPref
422af 69 78 20 2b 20 6e 53 75 66 66 69 78 3b 0d 0a 20  ix + nSuffix;.. 
422b0 20 20 20 7a 43 73 72 20 2b 3d 20 6e 53 75 66 66     zCsr += nSuff
422b1 69 78 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f  ix;....    /* Co
422b2 6d 70 61 72 65 20 74 68 65 20 74 65 72 6d 20 77  mpare the term w
422b3 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
422b4 66 6f 72 20 77 69 74 68 20 74 68 65 20 74 65 72  for with the ter
422b5 6d 20 6a 75 73 74 20 6c 6f 61 64 65 64 20 66 72  m just loaded fr
422b6 6f 6d 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 69  om..    ** the i
422b7 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 49 66  nterior node. If
422b8 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74   the specified t
422b9 65 72 6d 20 69 73 20 67 72 65 61 74 65 72 20 74  erm is greater t
422ba 68 61 6e 20 6f 72 20 65 71 75 61 6c 0d 0a 20 20  han or equal..  
422bb 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 65 72 6d    ** to the term
422bc 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 69   from the interi
422bd 6f 72 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 6c  or node, then al
422be 6c 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 73  l terms on the s
422bf 75 62 2d 74 72 65 65 20 0d 0a 20 20 20 20 2a 2a  ub-tree ..    **
422c0 20 68 65 61 64 65 64 20 62 79 20 6e 6f 64 65 20   headed by node 
422c1 69 43 68 69 6c 64 20 61 72 65 20 73 6d 61 6c 6c  iChild are small
422c2 65 72 20 74 68 61 6e 20 7a 54 65 72 6d 2e 20 4e  er than zTerm. N
422c3 6f 20 6e 65 65 64 20 74 6f 20 73 65 61 72 63 68  o need to search
422c4 20 0d 0a 20 20 20 20 2a 2a 20 69 43 68 69 6c 64   ..    ** iChild
422c5 2e 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20 20 2a  ...    **..    *
422c6 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 69 6f  * If the interio
422c7 72 20 6e 6f 64 65 20 74 65 72 6d 20 69 73 20 6c  r node term is l
422c8 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 73  arger than the s
422c9 70 65 63 69 66 69 65 64 20 74 65 72 6d 2c 20 74  pecified term, t
422ca 68 65 6e 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20  hen..    ** the 
422cb 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 69  tree headed by i
422cc 43 68 69 6c 64 20 6d 61 79 20 63 6f 6e 74 61 69  Child may contai
422cd 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  n the specified 
422ce 74 65 72 6d 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  term...    */.. 
422cf 20 20 20 63 6d 70 20 3d 20 6d 65 6d 63 6d 70 28     cmp = memcmp(
422d0 7a 54 65 72 6d 2c 20 7a 42 75 66 66 65 72 2c 20  zTerm, zBuffer, 
422d1 28 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 20 3f  (nBuffer>nTerm ?
422d2 20 6e 54 65 72 6d 20 3a 20 6e 42 75 66 66 65 72   nTerm : nBuffer
422d3 29 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 69 46  ));..    if( piF
422d4 69 72 73 74 20 26 26 20 28 63 6d 70 3c 30 20 7c  irst && (cmp<0 |
422d5 7c 20 28 63 6d 70 3d 3d 30 20 26 26 20 6e 42 75  | (cmp==0 && nBu
422d6 66 66 65 72 3e 6e 54 65 72 6d 29 29 20 29 7b 0d  ffer>nTerm)) ){.
422d7 0a 20 20 20 20 20 20 2a 70 69 46 69 72 73 74 20  .      *piFirst 
422d8 3d 20 69 43 68 69 6c 64 3b 0d 0a 20 20 20 20 20  = iChild;..     
422d9 20 70 69 46 69 72 73 74 20 3d 20 30 3b 0d 0a 20   piFirst = 0;.. 
422da 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20     }....    if( 
422db 70 69 4c 61 73 74 20 26 26 20 63 6d 70 3c 30 20  piLast && cmp<0 
422dc 29 7b 0d 0a 20 20 20 20 20 20 2a 70 69 4c 61 73  ){..      *piLas
422dd 74 20 3d 20 69 43 68 69 6c 64 3b 0d 0a 20 20 20  t = iChild;..   
422de 20 20 20 70 69 4c 61 73 74 20 3d 20 30 3b 0d 0a     piLast = 0;..
422df 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 43 68      }....    iCh
422e0 69 6c 64 2b 2b 3b 0d 0a 20 20 7d 3b 0d 0a 0d 0a  ild++;..  };....
422e1 20 20 69 66 28 20 70 69 46 69 72 73 74 20 29 20    if( piFirst ) 
422e2 2a 70 69 46 69 72 73 74 20 3d 20 69 43 68 69 6c  *piFirst = iChil
422e3 64 3b 0d 0a 20 20 69 66 28 20 70 69 4c 61 73 74  d;..  if( piLast
422e4 20 29 20 2a 70 69 4c 61 73 74 20 3d 20 69 43 68   ) *piLast = iCh
422e5 69 6c 64 3b 0d 0a 0d 0a 20 66 69 6e 69 73 68 5f  ild;.... finish_
422e6 73 63 61 6e 3a 0d 0a 20 20 73 71 6c 69 74 65 33  scan:..  sqlite3
422e7 5f 66 72 65 65 28 7a 42 75 66 66 65 72 29 3b 0d  _free(zBuffer);.
422e8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
422e9 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  ....../*..** The
422ea 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
422eb 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 7a  to by argument z
422ec 4e 6f 64 65 20 28 73 69 7a 65 20 6e 4e 6f 64 65  Node (size nNode
422ed 20 62 79 74 65 73 29 20 63 6f 6e 74 61 69 6e 73   bytes) contains
422ee 20 61 6e 0d 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   an..** interior
422ef 20 6e 6f 64 65 20 6f 66 20 61 20 62 2d 74 72 65   node of a b-tre
422f0 65 20 73 65 67 6d 65 6e 74 2e 20 54 68 65 20 7a  e segment. The z
422f1 54 65 72 6d 20 62 75 66 66 65 72 20 28 73 69 7a  Term buffer (siz
422f2 65 20 6e 54 65 72 6d 20 62 79 74 65 73 29 0d 0a  e nTerm bytes)..
422f3 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 74 65  ** contains a te
422f4 72 6d 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  rm. This functio
422f5 6e 20 73 65 61 72 63 68 65 73 20 74 68 65 20 73  n searches the s
422f6 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62  ub-tree headed b
422f7 79 20 74 68 65 20 7a 4e 6f 64 65 0d 0a 2a 2a 20  y the zNode..** 
422f8 6e 6f 64 65 20 66 6f 72 20 74 68 65 20 72 61 6e  node for the ran
422f9 67 65 20 6f 66 20 6c 65 61 66 20 6e 6f 64 65 73  ge of leaf nodes
422fa 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69   that may contai
422fb 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  n the specified 
422fc 74 65 72 6d 0d 0a 2a 2a 20 6f 72 20 74 65 72 6d  term..** or term
422fd 73 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  s for which the 
422fe 73 70 65 63 69 66 69 65 64 20 74 65 72 6d 20 69  specified term i
422ff 73 20 61 20 70 72 65 66 69 78 2e 0d 0a 2a 2a 0d  s a prefix...**.
42300 0a 2a 2a 20 49 66 20 70 69 4c 65 61 66 20 69 73  .** If piLeaf is
42301 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
42302 2a 70 69 4c 65 61 66 20 69 73 20 73 65 74 20 74  *piLeaf is set t
42303 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f 66  o the blockid of
42304 20 74 68 65 20 0d 0a 2a 2a 20 6c 65 66 74 2d 6d   the ..** left-m
42305 6f 73 74 20 6c 65 61 66 20 6e 6f 64 65 20 69 6e  ost leaf node in
42306 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20 6d   the tree that m
42307 61 79 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 73  ay contain the s
42308 70 65 63 69 66 69 65 64 20 74 65 72 6d 2e 0d 0a  pecified term...
42309 2a 2a 20 49 66 20 70 69 4c 65 61 66 32 20 69 73  ** If piLeaf2 is
4230a 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
4230b 2a 70 69 4c 65 61 66 32 20 69 73 20 73 65 74 20  *piLeaf2 is set 
4230c 74 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f  to the blockid o
4230d 66 20 74 68 65 0d 0a 2a 2a 20 72 69 67 68 74 2d  f the..** right-
4230e 6d 6f 73 74 20 6c 65 61 66 20 6e 6f 64 65 20 74  most leaf node t
4230f 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  hat may contain 
42310 61 20 74 65 72 6d 20 66 6f 72 20 77 68 69 63 68  a term for which
42311 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0d 0a   the specified..
42312 2a 2a 20 74 65 72 6d 20 69 73 20 61 20 70 72 65  ** term is a pre
42313 66 69 78 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 74 20  fix...**..** It 
42314 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74  is possible that
42315 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 72 65   the range of re
42316 74 75 72 6e 65 64 20 6c 65 61 66 20 6e 6f 64 65  turned leaf node
42317 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  s does not conta
42318 69 6e 20 0d 0a 2a 2a 20 74 68 65 20 73 70 65 63  in ..** the spec
42319 69 66 69 65 64 20 74 65 72 6d 20 6f 72 20 61 6e  ified term or an
4231a 79 20 74 65 72 6d 73 20 66 6f 72 20 77 68 69 63  y terms for whic
4231b 68 20 69 74 20 69 73 20 61 20 70 72 65 66 69 78  h it is a prefix
4231c 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68  . However, if th
4231d 65 20 0d 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 64  e ..** segment d
4231e 6f 65 73 20 63 6f 6e 74 61 69 6e 20 61 6e 79 20  oes contain any 
4231f 73 75 63 68 20 74 65 72 6d 73 2c 20 74 68 65 79  such terms, they
42320 20 61 72 65 20 73 74 6f 72 65 64 20 77 69 74 68   are stored with
42321 69 6e 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  in the identifie
42322 64 0d 0a 2a 2a 20 72 61 6e 67 65 2e 20 42 65 63  d..** range. Bec
42323 61 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69  ause this functi
42324 6f 6e 20 6f 6e 6c 79 20 69 6e 73 70 65 63 74 73  on only inspects
42325 20 69 6e 74 65 72 69 6f 72 20 73 65 67 6d 65 6e   interior segmen
42326 74 20 6e 6f 64 65 73 20 28 61 6e 64 0d 0a 2a 2a  t nodes (and..**
42327 20 6e 65 76 65 72 20 6c 6f 61 64 73 20 6c 65 61   never loads lea
42328 66 20 6e 6f 64 65 73 20 69 6e 74 6f 20 6d 65 6d  f nodes into mem
42329 6f 72 79 29 2c 20 69 74 20 69 73 20 6e 6f 74 20  ory), it is not 
4232a 70 6f 73 73 69 62 6c 65 20 74 6f 20 62 65 20 73  possible to be s
4232b 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ure...**..** If 
4232c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
4232d 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
4232e 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
4232f 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
42330 0d 0a 2a 2f 20 0d 0a 73 74 61 74 69 63 20 69 6e  ..*/ ..static in
42331 74 20 66 74 73 33 53 65 6c 65 63 74 4c 65 61 66  t fts3SelectLeaf
42332 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  (..  Fts3Table *
42333 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
42334 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
42335 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  table handle */.
42336 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
42337 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20  Term,           
42338 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 73 65     /* Term to se
42339 6c 65 63 74 20 6c 65 61 76 65 73 20 66 6f 72 20  lect leaves for 
4233a 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c  */..  int nTerm,
4233b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4233c 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
4233d 20 74 65 72 6d 20 7a 54 65 72 6d 20 69 6e 20 62   term zTerm in b
4233e 79 74 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ytes */..  const
4233f 20 63 68 61 72 20 2a 7a 4e 6f 64 65 2c 20 20 20   char *zNode,   
42340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75             /* Bu
42341 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
42342 73 65 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72  segment interior
42343 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20   node */..  int 
42344 6e 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20  nNode,          
42345 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
42346 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74  ize of buffer at
42347 20 7a 4e 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c   zNode */..  sql
42348 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 65  ite3_int64 *piLe
42349 61 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  af,          /* 
4234a 53 65 6c 65 63 74 65 64 20 6c 65 61 66 20 6e 6f  Selected leaf no
4234b 64 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  de */..  sqlite3
4234c 5f 69 6e 74 36 34 20 2a 70 69 4c 65 61 66 32 20  _int64 *piLeaf2 
4234d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65           /* Sele
4234e 63 74 65 64 20 6c 65 61 66 20 6e 6f 64 65 20 2a  cted leaf node *
4234f 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  /..){..  int rc;
42350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42351 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
42352 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e  rn code */..  in
42353 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
42354 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42355 20 48 65 69 67 68 74 20 6f 66 20 74 68 69 73 20   Height of this 
42356 6e 6f 64 65 20 69 6e 20 74 72 65 65 20 2a 2f 0d  node in tree */.
42357 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 69 4c  ...  assert( piL
42358 65 61 66 20 7c 7c 20 70 69 4c 65 61 66 32 20 29  eaf || piLeaf2 )
42359 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 46 74  ;....  sqlite3Ft
4235a 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a 4e  s3GetVarint32(zN
4235b 6f 64 65 2c 20 26 69 48 65 69 67 68 74 29 3b 0d  ode, &iHeight);.
4235c 0a 20 20 72 63 20 3d 20 66 74 73 33 53 63 61 6e  .  rc = fts3Scan
4235d 49 6e 74 65 72 69 6f 72 4e 6f 64 65 28 7a 54 65  InteriorNode(zTe
4235e 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 4e 6f 64 65  rm, nTerm, zNode
4235f 2c 20 6e 4e 6f 64 65 2c 20 70 69 4c 65 61 66 2c  , nNode, piLeaf,
42360 20 70 69 4c 65 61 66 32 29 3b 0d 0a 20 20 61 73   piLeaf2);..  as
42361 73 65 72 74 28 20 21 70 69 4c 65 61 66 32 20 7c  sert( !piLeaf2 |
42362 7c 20 21 70 69 4c 65 61 66 20 7c 7c 20 72 63 21  | !piLeaf || rc!
42363 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 2a  =SQLITE_OK || (*
42364 70 69 4c 65 61 66 3c 3d 2a 70 69 4c 65 61 66 32  piLeaf<=*piLeaf2
42365 29 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 72 63  ) );....  if( rc
42366 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
42367 48 65 69 67 68 74 3e 31 20 29 7b 0d 0a 20 20 20  Height>1 ){..   
42368 20 63 68 61 72 20 2a 7a 42 6c 6f 62 20 3d 20 30   char *zBlob = 0
42369 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
4236a 2a 20 42 6c 6f 62 20 72 65 61 64 20 66 72 6f 6d  * Blob read from
4236b 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c   %_segments tabl
4236c 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 42  e */..    int nB
4236d 6c 6f 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  lob;            
4236e 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
4236f 6f 66 20 7a 42 6c 6f 62 20 69 6e 20 62 79 74 65  of zBlob in byte
42370 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 69 66 28 20  s */....    if( 
42371 70 69 4c 65 61 66 20 26 26 20 70 69 4c 65 61 66  piLeaf && piLeaf
42372 32 20 26 26 20 28 2a 70 69 4c 65 61 66 21 3d 2a  2 && (*piLeaf!=*
42373 70 69 4c 65 61 66 32 29 20 29 7b 0d 0a 20 20 20  piLeaf2) ){..   
42374 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
42375 74 73 33 52 65 61 64 42 6c 6f 63 6b 28 70 2c 20  ts3ReadBlock(p, 
42376 2a 70 69 4c 65 61 66 2c 20 26 7a 42 6c 6f 62 2c  *piLeaf, &zBlob,
42377 20 26 6e 42 6c 6f 62 2c 20 30 29 3b 0d 0a 20 20   &nBlob, 0);..  
42378 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
42379 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
4237a 20 20 72 63 20 3d 20 66 74 73 33 53 65 6c 65 63    rc = fts3Selec
4237b 74 4c 65 61 66 28 70 2c 20 7a 54 65 72 6d 2c 20  tLeaf(p, zTerm, 
4237c 6e 54 65 72 6d 2c 20 7a 42 6c 6f 62 2c 20 6e 42  nTerm, zBlob, nB
4237d 6c 6f 62 2c 20 70 69 4c 65 61 66 2c 20 30 29 3b  lob, piLeaf, 0);
4237e 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
4237f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42   sqlite3_free(zB
42380 6c 6f 62 29 3b 0d 0a 20 20 20 20 20 20 70 69 4c  lob);..      piL
42381 65 61 66 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  eaf = 0;..      
42382 7a 42 6c 6f 62 20 3d 20 30 3b 0d 0a 20 20 20 20  zBlob = 0;..    
42383 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  }....    if( rc=
42384 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
42385 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
42386 33 46 74 73 33 52 65 61 64 42 6c 6f 63 6b 28 70  3Fts3ReadBlock(p
42387 2c 20 70 69 4c 65 61 66 3f 2a 70 69 4c 65 61 66  , piLeaf?*piLeaf
42388 3a 2a 70 69 4c 65 61 66 32 2c 20 26 7a 42 6c 6f  :*piLeaf2, &zBlo
42389 62 2c 20 26 6e 42 6c 6f 62 2c 20 30 29 3b 0d 0a  b, &nBlob, 0);..
4238a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
4238b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
4238c 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
4238d 53 65 6c 65 63 74 4c 65 61 66 28 70 2c 20 7a 54  SelectLeaf(p, zT
4238e 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 42 6c 6f  erm, nTerm, zBlo
4238f 62 2c 20 6e 42 6c 6f 62 2c 20 70 69 4c 65 61 66  b, nBlob, piLeaf
42390 2c 20 70 69 4c 65 61 66 32 29 3b 0d 0a 20 20 20  , piLeaf2);..   
42391 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
42392 66 72 65 65 28 7a 42 6c 6f 62 29 3b 0d 0a 20 20  free(zBlob);..  
42393 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  }....  return rc
42394 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
42395 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
42396 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 64  used to create d
42397 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 73 65 72  elta-encoded ser
42398 69 61 6c 69 7a 65 64 20 6c 69 73 74 73 20 6f 66  ialized lists of
42399 20 46 54 53 33 20 0d 0a 2a 2a 20 76 61 72 69 6e   FTS3 ..** varin
4239a 74 73 2e 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  ts. Each call to
4239b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
4239c 70 70 65 6e 64 73 20 61 20 73 69 6e 67 6c 65 20  ppends a single 
4239d 76 61 72 69 6e 74 20 74 6f 20 61 20 6c 69 73 74  varint to a list
4239e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
4239f 69 64 20 66 74 73 33 50 75 74 44 65 6c 74 61 56  id fts3PutDeltaV
423a0 61 72 69 6e 74 28 0d 0a 20 20 63 68 61 72 20 2a  arint(..  char *
423a1 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pp,            
423a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
423a3 4f 55 54 3a 20 4f 75 74 70 75 74 20 70 6f 69 6e  OUT: Output poin
423a4 74 65 72 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ter */..  sqlite
423a5 33 5f 69 6e 74 36 34 20 2a 70 69 50 72 65 76 2c  3_int64 *piPrev,
423a6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
423a7 4f 55 54 3a 20 50 72 65 76 69 6f 75 73 20 76 61  OUT: Previous va
423a8 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f 20 6c  lue written to l
423a9 69 73 74 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ist */..  sqlite
423aa 33 5f 69 6e 74 36 34 20 69 56 61 6c 20 20 20 20  3_int64 iVal    
423ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
423ac 74 65 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f  te this value to
423ad 20 74 68 65 20 6c 69 73 74 20 2a 2f 0d 0a 29 7b   the list */..){
423ae 0d 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 6c  ..  assert( iVal
423af 2d 2a 70 69 50 72 65 76 20 3e 20 30 20 7c 7c 20  -*piPrev > 0 || 
423b0 28 2a 70 69 50 72 65 76 3d 3d 30 20 26 26 20 69  (*piPrev==0 && i
423b1 56 61 6c 3d 3d 30 29 20 29 3b 0d 0a 20 20 2a 70  Val==0) );..  *p
423b2 70 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  p += sqlite3Fts3
423b3 50 75 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 69  PutVarint(*pp, i
423b4 56 61 6c 2d 2a 70 69 50 72 65 76 29 3b 0d 0a 20  Val-*piPrev);.. 
423b5 20 2a 70 69 50 72 65 76 20 3d 20 69 56 61 6c 3b   *piPrev = iVal;
423b6 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 68  ..}..../*..** Wh
423b7 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
423b8 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70 50   is called, *ppP
423b9 6f 73 6c 69 73 74 20 69 73 20 61 73 73 75 6d 65  oslist is assume
423ba 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  d to point to th
423bb 65 20 0d 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20  e ..** start of 
423bc 61 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2e  a position-list.
423bd 20 41 66 74 65 72 20 69 74 20 72 65 74 75 72 6e   After it return
423be 73 2c 20 2a 70 70 50 6f 73 6c 69 73 74 20 70 6f  s, *ppPoslist po
423bf 69 6e 74 73 20 74 6f 20 74 68 65 0d 0a 2a 2a 20  ints to the..** 
423c0 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72  first byte after
423c1 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69   the position-li
423c2 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 70 6f  st...**..** A po
423c3 73 69 74 69 6f 6e 20 6c 69 73 74 20 69 73 20 6c  sition list is l
423c4 69 73 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73  ist of positions
423c5 20 28 64 65 6c 74 61 20 65 6e 63 6f 64 65 64 29   (delta encoded)
423c6 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 20 66 6f 72   and columns for
423c7 20 0d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64   ..** a single d
423c8 6f 63 75 6d 65 6e 74 20 72 65 63 6f 72 64 20 6f  ocument record o
423c9 66 20 61 20 64 6f 63 6c 69 73 74 2e 20 20 53 6f  f a doclist.  So
423ca 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  , in other words
423cb 2c 20 74 68 69 73 0d 0a 2a 2a 20 72 6f 75 74 69  , this..** routi
423cc 6e 65 20 61 64 76 61 6e 63 65 73 20 2a 70 70 50  ne advances *ppP
423cd 6f 73 6c 69 73 74 20 73 6f 20 74 68 61 74 20 69  oslist so that i
423ce 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
423cf 6e 65 78 74 20 64 6f 63 69 64 20 69 6e 0d 0a 2a  next docid in..*
423d0 2a 20 74 68 65 20 64 6f 63 6c 69 73 74 2c 20 6f  * the doclist, o
423d1 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  r to the first b
423d2 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  yte past the end
423d3 20 6f 66 20 74 68 65 20 64 6f 63 6c 69 73 74 2e   of the doclist.
423d4 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 70 20 69  ..**..** If pp i
423d5 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
423d6 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
423d7 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69   the position li
423d8 73 74 20 61 72 65 20 63 6f 70 69 65 64 0d 0a 2a  st are copied..*
423d9 2a 20 74 6f 20 2a 70 70 2e 20 2a 70 70 20 69 73  * to *pp. *pp is
423da 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
423db 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
423dc 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 62 79  past the last by
423dd 74 65 20 63 6f 70 69 65 64 0d 0a 2a 2a 20 62 65  te copied..** be
423de 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
423df 6f 6e 20 72 65 74 75 72 6e 73 2e 0d 0a 2a 2f 0d  on returns...*/.
423e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
423e1 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 63 68 61  3PoslistCopy(cha
423e2 72 20 2a 2a 70 70 2c 20 63 68 61 72 20 2a 2a 70  r **pp, char **p
423e3 70 50 6f 73 6c 69 73 74 29 7b 0d 0a 20 20 63 68  pPoslist){..  ch
423e4 61 72 20 2a 70 45 6e 64 20 3d 20 2a 70 70 50 6f  ar *pEnd = *ppPo
423e5 73 6c 69 73 74 3b 0d 0a 20 20 63 68 61 72 20 63  slist;..  char c
423e6 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68   = 0;....  /* Th
423e7 65 20 65 6e 64 20 6f 66 20 61 20 70 6f 73 69 74  e end of a posit
423e8 69 6f 6e 20 6c 69 73 74 20 69 73 20 6d 61 72 6b  ion list is mark
423e9 65 64 20 62 79 20 61 20 7a 65 72 6f 20 65 6e 63  ed by a zero enc
423ea 6f 64 65 64 20 61 73 20 61 6e 20 46 54 53 33 20  oded as an FTS3 
423eb 0d 0a 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 41  ..  ** varint. A
423ec 20 73 69 6e 67 6c 65 20 50 4f 53 5f 45 4e 44 20   single POS_END 
423ed 28 30 29 20 62 79 74 65 2e 20 45 78 63 65 70 74  (0) byte. Except
423ee 2c 20 69 66 20 74 68 65 20 30 20 62 79 74 65 20  , if the 0 byte 
423ef 69 73 20 70 72 65 63 65 64 65 64 20 62 79 0d 0a  is preceded by..
423f0 20 20 2a 2a 20 61 20 62 79 74 65 20 77 69 74 68    ** a byte with
423f1 20 74 68 65 20 30 78 38 30 20 62 69 74 20 73 65   the 0x80 bit se
423f2 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f  t, then it is no
423f3 74 20 61 20 76 61 72 69 6e 74 20 30 2c 20 62 75  t a varint 0, bu
423f4 74 20 74 68 65 20 74 61 69 6c 0d 0a 20 20 2a 2a  t the tail..  **
423f5 20 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72 2c 20   of some other, 
423f6 6d 75 6c 74 69 2d 62 79 74 65 2c 20 76 61 6c 75  multi-byte, valu
423f7 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54  e...  **..  ** T
423f8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 68 69  he following whi
423f9 6c 65 2d 6c 6f 6f 70 20 6d 6f 76 65 73 20 70 45  le-loop moves pE
423fa 6e 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  nd to point to t
423fb 68 65 20 66 69 72 73 74 20 62 79 74 65 20 74 68  he first byte th
423fc 61 74 20 69 73 20 6e 6f 74 20 0d 0a 20 20 2a 2a  at is not ..  **
423fd 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 72 65   immediately pre
423fe 63 65 64 65 64 20 62 79 20 61 20 62 79 74 65 20  ceded by a byte 
423ff 77 69 74 68 20 74 68 65 20 30 78 38 30 20 62 69  with the 0x80 bi
42400 74 20 73 65 74 2e 20 54 68 65 6e 20 69 6e 63 72  t set. Then incr
42401 65 6d 65 6e 74 73 0d 0a 20 20 2a 2a 20 70 45 6e  ements..  ** pEn
42402 64 20 6f 6e 63 65 20 6d 6f 72 65 20 73 6f 20 74  d once more so t
42403 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
42404 20 74 68 65 20 62 79 74 65 20 69 6d 6d 65 64 69   the byte immedi
42405 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
42406 74 68 65 0d 0a 20 20 2a 2a 20 6c 61 73 74 20 62  the..  ** last b
42407 79 74 65 20 69 6e 20 74 68 65 20 70 6f 73 69 74  yte in the posit
42408 69 6f 6e 2d 6c 69 73 74 2e 0d 0a 20 20 2a 2f 0d  ion-list...  */.
42409 0a 20 20 77 68 69 6c 65 28 20 2a 70 45 6e 64 20  .  while( *pEnd 
4240a 7c 20 63 20 29 7b 0d 0a 20 20 20 20 63 20 3d 20  | c ){..    c = 
4240b 2a 70 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0d  *pEnd++ & 0x80;.
4240c 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63  .    testcase( c
4240d 21 3d 30 20 26 26 20 28 2a 70 45 6e 64 29 3d 3d  !=0 && (*pEnd)==
4240e 30 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 45 6e  0 );..  }..  pEn
4240f 64 2b 2b 3b 20 20 2f 2a 20 41 64 76 61 6e 63 65  d++;  /* Advance
42410 20 70 61 73 74 20 74 68 65 20 50 4f 53 5f 45 4e   past the POS_EN
42411 44 20 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74  D terminator byt
42412 65 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 70 70  e */....  if( pp
42413 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 20 3d   ){..    int n =
42414 20 28 69 6e 74 29 28 70 45 6e 64 20 2d 20 2a 70   (int)(pEnd - *p
42415 70 50 6f 73 6c 69 73 74 29 3b 0d 0a 20 20 20 20  pPoslist);..    
42416 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b 0d 0a  char *p = *pp;..
42417 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 2a 70      memcpy(p, *p
42418 70 50 6f 73 6c 69 73 74 2c 20 6e 29 3b 0d 0a 20  pPoslist, n);.. 
42419 20 20 20 70 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20     p += n;..    
4241a 2a 70 70 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a 20  *pp = p;..  }.. 
4241b 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20 70 45   *ppPoslist = pE
4241c 6e 64 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  nd;..}..../*..**
4241d 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   When this funct
4241e 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a  ion is called, *
4241f 70 70 50 6f 73 6c 69 73 74 20 69 73 20 61 73 73  ppPoslist is ass
42420 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f  umed to point to
42421 20 74 68 65 20 0d 0a 2a 2a 20 73 74 61 72 74 20   the ..** start 
42422 6f 66 20 61 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74  of a column-list
42423 2e 20 41 66 74 65 72 20 69 74 20 72 65 74 75 72  . After it retur
42424 6e 73 2c 20 2a 70 70 50 6f 73 6c 69 73 74 20 70  ns, *ppPoslist p
42425 6f 69 6e 74 73 20 74 6f 20 74 68 65 0d 0a 2a 2a  oints to the..**
42426 20 74 6f 20 74 68 65 20 74 65 72 6d 69 6e 61 74   to the terminat
42427 6f 72 20 28 50 4f 53 5f 43 4f 4c 55 4d 4e 20 6f  or (POS_COLUMN o
42428 72 20 50 4f 53 5f 45 4e 44 29 20 62 79 74 65 20  r POS_END) byte 
42429 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69  of the column-li
4242a 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 63 6f  st...**..** A co
4242b 6c 75 6d 6e 2d 6c 69 73 74 20 69 73 20 6c 69 73  lumn-list is lis
4242c 74 20 6f 66 20 64 65 6c 74 61 2d 65 6e 63 6f 64  t of delta-encod
4242d 65 64 20 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72  ed positions for
4242e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
4242f 0d 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 69  ..** within a si
42430 6e 67 6c 65 20 64 6f 63 75 6d 65 6e 74 20 77 69  ngle document wi
42431 74 68 69 6e 20 61 20 64 6f 63 6c 69 73 74 2e 0d  thin a doclist..
42432 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 6c 75  .**..** The colu
42433 6d 6e 2d 6c 69 73 74 20 69 73 20 74 65 72 6d 69  mn-list is termi
42434 6e 61 74 65 64 20 65 69 74 68 65 72 20 62 79 20  nated either by 
42435 61 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 76 61 72  a POS_COLUMN var
42436 69 6e 74 20 28 31 29 20 6f 72 0d 0a 2a 2a 20 61  int (1) or..** a
42437 20 50 4f 53 5f 45 4e 44 20 76 61 72 69 6e 74 20   POS_END varint 
42438 28 30 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69  (0).  This routi
42439 6e 65 20 6c 65 61 76 65 73 20 2a 70 70 50 6f 73  ne leaves *ppPos
4243a 6c 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f  list pointing to
4243b 0d 0a 2a 2a 20 74 68 65 20 50 4f 53 5f 43 4f 4c  ..** the POS_COL
4243c 55 4d 4e 20 6f 72 20 50 4f 53 5f 45 4e 44 20 74  UMN or POS_END t
4243d 68 61 74 20 74 65 72 6d 69 6e 61 74 65 73 20 74  hat terminates t
4243e 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 2e 0d  he column-list..
4243f 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 70 70 20 69 73  .**..** If pp is
42440 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
42441 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
42442 74 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20  the column-list 
42443 61 72 65 20 63 6f 70 69 65 64 0d 0a 2a 2a 20 74  are copied..** t
42444 6f 20 2a 70 70 2e 20 2a 70 70 20 69 73 20 73 65  o *pp. *pp is se
42445 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
42446 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73  e first byte pas
42447 74 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20  t the last byte 
42448 63 6f 70 69 65 64 0d 0a 2a 2a 20 62 65 66 6f 72  copied..** befor
42449 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
4244a 72 65 74 75 72 6e 73 2e 20 20 54 68 65 20 50 4f  returns.  The PO
4244b 53 5f 43 4f 4c 55 4d 4e 20 6f 72 20 50 4f 53 5f  S_COLUMN or POS_
4244c 45 4e 44 20 74 65 72 6d 69 6e 61 74 6f 72 0d 0a  END terminator..
4244d 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64  ** is not copied
4244e 20 69 6e 74 6f 20 2a 70 70 2e 0d 0a 2a 2f 0d 0a   into *pp...*/..
4244f 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
42450 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 63  ColumnlistCopy(c
42451 68 61 72 20 2a 2a 70 70 2c 20 63 68 61 72 20 2a  har **pp, char *
42452 2a 70 70 50 6f 73 6c 69 73 74 29 7b 0d 0a 20 20  *ppPoslist){..  
42453 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 2a 70 70  char *pEnd = *pp
42454 50 6f 73 6c 69 73 74 3b 0d 0a 20 20 63 68 61 72  Poslist;..  char
42455 20 63 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20   c = 0;....  /* 
42456 41 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69 73  A column-list is
42457 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 65   terminated by e
42458 69 74 68 65 72 20 61 20 30 78 30 31 20 6f 72 20  ither a 0x01 or 
42459 30 78 30 30 20 62 79 74 65 20 74 68 61 74 20 69  0x00 byte that i
4245a 73 0d 0a 20 20 2a 2a 20 6e 6f 74 20 70 61 72 74  s..  ** not part
4245b 20 6f 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65   of a multi-byte
4245c 20 76 61 72 69 6e 74 2e 0d 0a 20 20 2a 2f 0d 0a   varint...  */..
4245d 20 20 77 68 69 6c 65 28 20 30 78 46 45 20 26 20    while( 0xFE & 
4245e 28 2a 70 45 6e 64 20 7c 20 63 29 20 29 7b 0d 0a  (*pEnd | c) ){..
4245f 20 20 20 20 63 20 3d 20 2a 70 45 6e 64 2b 2b 20      c = *pEnd++ 
42460 26 20 30 78 38 30 3b 0d 0a 20 20 20 20 74 65 73  & 0x80;..    tes
42461 74 63 61 73 65 28 20 63 21 3d 30 20 26 26 20 28  tcase( c!=0 && (
42462 28 2a 70 45 6e 64 29 26 30 78 66 65 29 3d 3d 30  (*pEnd)&0xfe)==0
42463 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20   );..  }..  if( 
42464 70 70 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e  pp ){..    int n
42465 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 20 2d 20   = (int)(pEnd - 
42466 2a 70 70 50 6f 73 6c 69 73 74 29 3b 0d 0a 20 20  *ppPoslist);..  
42467 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b    char *p = *pp;
42468 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20  ..    memcpy(p, 
42469 2a 70 70 50 6f 73 6c 69 73 74 2c 20 6e 29 3b 0d  *ppPoslist, n);.
4246a 0a 20 20 20 20 70 20 2b 3d 20 6e 3b 0d 0a 20 20  .    p += n;..  
4246b 20 20 2a 70 70 20 3d 20 70 3b 0d 0a 20 20 7d 0d    *pp = p;..  }.
4246c 0a 20 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20  .  *ppPoslist = 
4246d 70 45 6e 64 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  pEnd;..}..../*..
4246e 2a 2a 20 56 61 6c 75 65 20 75 73 65 64 20 74 6f  ** Value used to
4246f 20 73 69 67 6e 69 66 79 20 74 68 65 20 65 6e 64   signify the end
42470 20 6f 66 20 61 6e 20 70 6f 73 69 74 69 6f 6e 2d   of an position-
42471 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 73 61  list. This is sa
42472 66 65 20 62 65 63 61 75 73 65 0d 0a 2a 2a 20 69  fe because..** i
42473 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
42474 65 20 74 6f 20 68 61 76 65 20 61 20 64 6f 63 75  e to have a docu
42475 6d 65 6e 74 20 77 69 74 68 20 32 5e 33 31 20 74  ment with 2^31 t
42476 65 72 6d 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  erms...*/..#defi
42477 6e 65 20 50 4f 53 49 54 49 4f 4e 5f 4c 49 53 54  ne POSITION_LIST
42478 5f 45 4e 44 20 30 78 37 66 66 66 66 66 66 66 0d  _END 0x7fffffff.
42479 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
4247a 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
4247b 74 6f 20 68 65 6c 70 20 70 61 72 73 65 20 70 6f  to help parse po
4247c 73 69 74 69 6f 6e 2d 6c 69 73 74 73 2e 20 57 68  sition-lists. Wh
4247d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
4247e 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20   is..** called, 
4247f 2a 70 70 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f  *pp may point to
42480 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
42481 65 20 6e 65 78 74 20 76 61 72 69 6e 74 20 69 6e  e next varint in
42482 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69   the position-li
42483 73 74 0d 0a 2a 2a 20 62 65 69 6e 67 20 70 61 72  st..** being par
42484 73 65 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 70  sed, or it may p
42485 6f 69 6e 74 20 74 6f 20 31 20 62 79 74 65 20 70  oint to 1 byte p
42486 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
42487 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74  he position-list
42488 0d 0a 2a 2a 20 28 69 6e 20 77 68 69 63 68 20 63  ..** (in which c
42489 61 73 65 20 2a 2a 70 70 20 77 69 6c 6c 20 62 65  ase **pp will be
4248a 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 20 62 79   a terminator by
4248b 74 65 73 20 50 4f 53 5f 45 4e 44 20 28 30 29 20  tes POS_END (0) 
4248c 6f 72 0d 0a 2a 2a 20 28 31 29 29 2e 0d 0a 2a 2a  or..** (1))...**
4248d 0d 0a 2a 2a 20 49 66 20 2a 70 70 20 70 6f 69 6e  ..** If *pp poin
4248e 74 73 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  ts past the end 
4248f 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  of the current p
42490 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2c 20 73 65  osition-list, se
42491 74 20 2a 70 69 20 74 6f 20 0d 0a 2a 2a 20 50 4f  t *pi to ..** PO
42492 53 49 54 49 4f 4e 5f 4c 49 53 54 5f 45 4e 44 20  SITION_LIST_END 
42493 61 6e 64 20 72 65 74 75 72 6e 2e 20 4f 74 68 65  and return. Othe
42494 72 77 69 73 65 2c 20 72 65 61 64 20 74 68 65 20  rwise, read the 
42495 6e 65 78 74 20 76 61 72 69 6e 74 20 66 72 6f 6d  next varint from
42496 20 2a 70 70 2c 0d 0a 2a 2a 20 69 6e 63 72 65 6d   *pp,..** increm
42497 65 6e 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  ent the current 
42498 76 61 6c 75 65 20 6f 66 20 2a 70 69 20 62 79 20  value of *pi by 
42499 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 2c 20  the value read, 
4249a 61 6e 64 20 73 65 74 20 2a 70 70 20 74 6f 0d 0a  and set *pp to..
4249b 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  ** point to the 
4249c 6e 65 78 74 20 76 61 6c 75 65 20 62 65 66 6f 72  next value befor
4249d 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a  e returning...**
4249e 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 63 61 6c 6c  ..** Before call
4249f 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
424a0 20 2a 70 69 20 6d 75 73 74 20 62 65 20 69 6e 69   *pi must be ini
424a1 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20  tialized to the 
424a2 76 61 6c 75 65 20 6f 66 0d 0a 2a 2a 20 74 68 65  value of..** the
424a3 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69   previous positi
424a4 6f 6e 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 77  on, or zero if w
424a5 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
424a6 65 20 66 69 72 73 74 20 70 6f 73 69 74 69 6f 6e  e first position
424a7 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 70 6f 73 69  ..** in the posi
424a8 74 69 6f 6e 2d 6c 69 73 74 2e 20 20 42 65 63 61  tion-list.  Beca
424a9 75 73 65 20 70 6f 73 69 74 69 6f 6e 73 20 61 72  use positions ar
424aa 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2c  e delta-encoded,
424ab 20 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20 6f   the value..** o
424ac 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70  f the previous p
424ad 6f 73 69 74 69 6f 6e 20 69 73 20 6e 65 65 64 65  osition is neede
424ae 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f  d in order to co
424af 6d 70 75 74 65 20 74 68 65 20 76 61 6c 75 65 20  mpute the value 
424b0 6f 66 0d 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20  of..** the next 
424b1 70 6f 73 69 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73  position...*/..s
424b2 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52  tatic void fts3R
424b3 65 61 64 4e 65 78 74 50 6f 73 28 0d 0a 20 20 63  eadNextPos(..  c
424b4 68 61 72 20 2a 2a 70 70 2c 20 20 20 20 20 20 20  har **pp,       
424b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
424b6 49 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  IN/OUT: Pointer 
424b7 69 6e 74 6f 20 70 6f 73 69 74 69 6f 6e 2d 6c 69  into position-li
424b8 73 74 20 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20  st buffer */..  
424b9 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
424ba 69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i             /*
424bb 20 49 4e 2f 4f 55 54 3a 20 56 61 6c 75 65 20 72   IN/OUT: Value r
424bc 65 61 64 20 66 72 6f 6d 20 70 6f 73 69 74 69 6f  ead from positio
424bd 6e 2d 6c 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  n-list */..){.. 
424be 20 69 66 28 20 28 2a 2a 70 70 29 26 30 78 46 45   if( (**pp)&0xFE
424bf 20 29 7b 0d 0a 20 20 20 20 66 74 73 33 47 65 74   ){..    fts3Get
424c0 44 65 6c 74 61 56 61 72 69 6e 74 28 70 70 2c 20  DeltaVarint(pp, 
424c1 70 69 29 3b 0d 0a 20 20 20 20 2a 70 69 20 2d 3d  pi);..    *pi -=
424c2 20 32 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   2;..  }else{.. 
424c3 20 20 20 2a 70 69 20 3d 20 50 4f 53 49 54 49 4f     *pi = POSITIO
424c4 4e 5f 4c 49 53 54 5f 45 4e 44 3b 0d 0a 20 20 7d  N_LIST_END;..  }
424c5 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66  ..}..../*..** If
424c6 20 70 61 72 61 6d 65 74 65 72 20 69 43 6f 6c 20   parameter iCol 
424c7 69 73 20 6e 6f 74 20 30 2c 20 77 72 69 74 65 20  is not 0, write 
424c8 61 6e 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 28 31  an POS_COLUMN (1
424c9 29 20 62 79 74 65 20 66 6f 6c 6c 6f 77 65 64 20  ) byte followed 
424ca 62 79 0d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  by..** the value
424cb 20 6f 66 20 69 43 6f 6c 20 65 6e 63 6f 64 65 64   of iCol encoded
424cc 20 61 73 20 61 20 76 61 72 69 6e 74 20 74 6f 20   as a varint to 
424cd 2a 70 70 2e 20 20 20 54 68 69 73 20 77 69 6c 6c  *pp.   This will
424ce 20 73 74 61 72 74 20 61 20 6e 65 77 0d 0a 2a 2a   start a new..**
424cf 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0d 0a 2a   column list...*
424d0 2a 0d 0a 2a 2a 20 53 65 74 20 2a 70 70 20 74 6f  *..** Set *pp to
424d1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62 79   point to the by
424d2 74 65 20 6a 75 73 74 20 61 66 74 65 72 20 74 68  te just after th
424d3 65 20 6c 61 73 74 20 62 79 74 65 20 77 72 69 74  e last byte writ
424d4 74 65 6e 20 62 65 66 6f 72 65 20 0d 0a 2a 2a 20  ten before ..** 
424d5 72 65 74 75 72 6e 69 6e 67 20 28 64 6f 20 6e 6f  returning (do no
424d6 74 20 6d 6f 64 69 66 79 20 69 74 20 69 66 20 69  t modify it if i
424d7 43 6f 6c 3d 3d 30 29 2e 20 52 65 74 75 72 6e 20  Col==0). Return 
424d8 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
424d9 20 6f 66 20 62 79 74 65 73 0d 0a 2a 2a 20 77 72   of bytes..** wr
424da 69 74 74 65 6e 20 28 30 20 69 66 20 69 43 6f 6c  itten (0 if iCol
424db 3d 3d 30 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ==0)...*/..stati
424dc 63 20 69 6e 74 20 66 74 73 33 50 75 74 43 6f 6c  c int fts3PutCol
424dd 4e 75 6d 62 65 72 28 63 68 61 72 20 2a 2a 70 70  Number(char **pp
424de 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0d 0a 20 20  , int iCol){..  
424df 69 6e 74 20 6e 20 3d 20 30 3b 20 20 20 20 20 20  int n = 0;      
424e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
424e1 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
424e2 65 73 20 77 72 69 74 74 65 6e 20 2a 2f 0d 0a 20  es written */.. 
424e3 20 69 66 28 20 69 43 6f 6c 20 29 7b 0d 0a 20 20   if( iCol ){..  
424e4 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b    char *p = *pp;
424e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
424e6 2f 2a 20 4f 75 74 70 75 74 20 70 6f 69 6e 74 65  /* Output pointe
424e7 72 20 2a 2f 0d 0a 20 20 20 20 6e 20 3d 20 31 20  r */..    n = 1 
424e8 2b 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  + sqlite3Fts3Put
424e9 56 61 72 69 6e 74 28 26 70 5b 31 5d 2c 20 69 43  Varint(&p[1], iC
424ea 6f 6c 29 3b 0d 0a 20 20 20 20 2a 70 20 3d 20 30  ol);..    *p = 0
424eb 78 30 31 3b 0d 0a 20 20 20 20 2a 70 70 20 3d 20  x01;..    *pp = 
424ec 26 70 5b 6e 5d 3b 0d 0a 20 20 7d 0d 0a 20 20 72  &p[n];..  }..  r
424ed 65 74 75 72 6e 20 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn n;..}..../
424ee 2a 0d 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  *..** Compute th
424ef 65 20 75 6e 69 6f 6e 20 6f 66 20 74 77 6f 20 70  e union of two p
424f0 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 2e 20 20  osition lists.  
424f1 54 68 65 20 6f 75 74 70 75 74 20 77 72 69 74 74  The output writt
424f2 65 6e 0d 0a 2a 2a 20 69 6e 74 6f 20 2a 70 70 20  en..** into *pp 
424f3 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 70 6f 73  contains all pos
424f4 69 74 69 6f 6e 73 20 6f 66 20 62 6f 74 68 20 2a  itions of both *
424f5 70 70 31 20 61 6e 64 20 2a 70 70 32 20 69 6e 20  pp1 and *pp2 in 
424f6 73 6f 72 74 65 64 0d 0a 2a 2a 20 6f 72 64 65 72  sorted..** order
424f7 20 61 6e 64 20 77 69 74 68 20 61 6e 79 20 64 75   and with any du
424f8 70 6c 69 63 61 74 65 73 20 72 65 6d 6f 76 65 64  plicates removed
424f9 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73 20  .  All pointers 
424fa 61 72 65 0d 0a 2a 2a 20 75 70 64 61 74 65 64 20  are..** updated 
424fb 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20  appropriately.  
424fc 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
424fd 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69  esponsible for i
424fe 6e 73 75 72 69 6e 67 0d 0a 2a 2a 20 74 68 61 74  nsuring..** that
424ff 20 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68   there is enough
42500 20 73 70 61 63 65 20 69 6e 20 2a 70 70 20 74 6f   space in *pp to
42501 20 68 6f 6c 64 20 74 68 65 20 63 6f 6d 70 6c 65   hold the comple
42502 74 65 20 6f 75 74 70 75 74 2e 0d 0a 2a 2f 0d 0a  te output...*/..
42503 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
42504 50 6f 73 6c 69 73 74 4d 65 72 67 65 28 0d 0a 20  PoslistMerge(.. 
42505 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20 20 20   char **pp,     
42506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42507 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
42508 72 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70  r */..  char **p
42509 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  p1,             
4250a 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20          /* Left 
4250b 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0d 0a 20  input list */.. 
4250c 20 63 68 61 72 20 2a 2a 70 70 32 20 20 20 20 20   char **pp2     
4250d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4250e 20 2f 2a 20 52 69 67 68 74 20 69 6e 70 75 74 20   /* Right input 
4250f 6c 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63  list */..){..  c
42510 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b 0d 0a 20  har *p = *pp;.. 
42511 20 63 68 61 72 20 2a 70 31 20 3d 20 2a 70 70 31   char *p1 = *pp1
42512 3b 0d 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20  ;..  char *p2 = 
42513 2a 70 70 32 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65  *pp2;....  while
42514 28 20 2a 70 31 20 7c 7c 20 2a 70 32 20 29 7b 0d  ( *p1 || *p2 ){.
42515 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 31 3b 20  .    int iCol1; 
42516 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
42517 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 69 6e  urrent column in
42518 64 65 78 20 69 6e 20 70 70 31 20 2a 2f 0d 0a 20  dex in pp1 */.. 
42519 20 20 20 69 6e 74 20 69 43 6f 6c 32 3b 20 20 20     int iCol2;   
4251a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72        /* The cur
4251b 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 69 6e 64 65  rent column inde
4251c 78 20 69 6e 20 70 70 32 20 2a 2f 0d 0a 0d 0a 20  x in pp2 */.... 
4251d 20 20 20 69 66 28 20 2a 70 31 3d 3d 50 4f 53 5f     if( *p1==POS_
4251e 43 4f 4c 55 4d 4e 20 29 20 73 71 6c 69 74 65 33  COLUMN ) sqlite3
4251f 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
42520 26 70 31 5b 31 5d 2c 20 26 69 43 6f 6c 31 29 3b  &p1[1], &iCol1);
42521 0d 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 2a  ..    else if( *
42522 70 31 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 69 43  p1==POS_END ) iC
42523 6f 6c 31 20 3d 20 50 4f 53 49 54 49 4f 4e 5f 4c  ol1 = POSITION_L
42524 49 53 54 5f 45 4e 44 3b 0d 0a 20 20 20 20 65 6c  IST_END;..    el
42525 73 65 20 69 43 6f 6c 31 20 3d 20 30 3b 0d 0a 0d  se iCol1 = 0;...
42526 0a 20 20 20 20 69 66 28 20 2a 70 32 3d 3d 50 4f  .    if( *p2==PO
42527 53 5f 43 4f 4c 55 4d 4e 20 29 20 73 71 6c 69 74  S_COLUMN ) sqlit
42528 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33  e3Fts3GetVarint3
42529 32 28 26 70 32 5b 31 5d 2c 20 26 69 43 6f 6c 32  2(&p2[1], &iCol2
4252a 29 3b 0d 0a 20 20 20 20 65 6c 73 65 20 69 66 28  );..    else if(
4252b 20 2a 70 32 3d 3d 50 4f 53 5f 45 4e 44 20 29 20   *p2==POS_END ) 
4252c 69 43 6f 6c 32 20 3d 20 50 4f 53 49 54 49 4f 4e  iCol2 = POSITION
4252d 5f 4c 49 53 54 5f 45 4e 44 3b 0d 0a 20 20 20 20  _LIST_END;..    
4252e 65 6c 73 65 20 69 43 6f 6c 32 20 3d 20 30 3b 0d  else iCol2 = 0;.
4252f 0a 0d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 31  ...    if( iCol1
42530 3d 3d 69 43 6f 6c 32 20 29 7b 0d 0a 20 20 20 20  ==iCol2 ){..    
42531 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
42532 69 31 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  i1 = 0;       /*
42533 20 4c 61 73 74 20 70 6f 73 69 74 69 6f 6e 20 66   Last position f
42534 72 6f 6d 20 70 70 31 20 2a 2f 0d 0a 20 20 20 20  rom pp1 */..    
42535 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
42536 69 32 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  i2 = 0;       /*
42537 20 4c 61 73 74 20 70 6f 73 69 74 69 6f 6e 20 66   Last position f
42538 72 6f 6d 20 70 70 32 20 2a 2f 0d 0a 20 20 20 20  rom pp2 */..    
42539 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
4253a 69 50 72 65 76 20 3d 20 30 3b 0d 0a 20 20 20 20  iPrev = 0;..    
4253b 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75    int n = fts3Pu
4253c 74 43 6f 6c 4e 75 6d 62 65 72 28 26 70 2c 20 69  tColNumber(&p, i
4253d 43 6f 6c 31 29 3b 0d 0a 20 20 20 20 20 20 70 31  Col1);..      p1
4253e 20 2b 3d 20 6e 3b 0d 0a 20 20 20 20 20 20 70 32   += n;..      p2
4253f 20 2b 3d 20 6e 3b 0d 0a 0d 0a 20 20 20 20 20 20   += n;....      
42540 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
42541 2c 20 62 6f 74 68 20 70 31 20 61 6e 64 20 70 32  , both p1 and p2
42542 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 74   point to the st
42543 61 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 2d 6c 69  art of column-li
42544 73 74 73 0d 0a 20 20 20 20 20 20 2a 2a 20 66 6f  sts..      ** fo
42545 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d  r the same colum
42546 6e 20 28 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69  n (the column wi
42547 74 68 20 69 6e 64 65 78 20 69 43 6f 6c 31 20 61  th index iCol1 a
42548 6e 64 20 69 43 6f 6c 32 29 2e 0d 0a 20 20 20 20  nd iCol2)...    
42549 20 20 2a 2a 20 41 20 63 6f 6c 75 6d 6e 2d 6c 69    ** A column-li
4254a 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
4254b 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 64 65 6c  non-negative del
4254c 74 61 2d 65 6e 63 6f 64 65 64 20 76 61 72 69 6e  ta-encoded varin
4254d 74 73 2c 20 65 61 63 68 20 0d 0a 20 20 20 20 20  ts, each ..     
4254e 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   ** incremented 
4254f 62 79 20 32 20 62 65 66 6f 72 65 20 62 65 69 6e  by 2 before bein
42550 67 20 73 74 6f 72 65 64 2e 20 45 61 63 68 20 6c  g stored. Each l
42551 69 73 74 20 69 73 20 74 65 72 6d 69 6e 61 74 65  ist is terminate
42552 64 20 62 79 20 61 0d 0a 20 20 20 20 20 20 2a 2a  d by a..      **
42553 20 50 4f 53 5f 45 4e 44 20 28 30 29 20 6f 72 20   POS_END (0) or 
42554 50 4f 53 5f 43 4f 4c 55 4d 4e 20 28 31 29 2e 20  POS_COLUMN (1). 
42555 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
42556 6f 63 6b 20 6d 65 72 67 65 73 20 74 68 65 20 74  ock merges the t
42557 77 6f 20 6c 69 73 74 73 0d 0a 20 20 20 20 20 20  wo lists..      
42558 2a 2a 20 61 6e 64 20 77 72 69 74 65 73 20 74 68  ** and writes th
42559 65 20 72 65 73 75 6c 74 73 20 74 6f 20 62 75 66  e results to buf
4255a 66 65 72 20 70 2e 20 70 20 69 73 20 6c 65 66 74  fer p. p is left
4255b 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65   pointing to the
4255c 20 62 79 74 65 0d 0a 20 20 20 20 20 20 2a 2a 20   byte..      ** 
4255d 61 66 74 65 72 20 74 68 65 20 6c 69 73 74 20 77  after the list w
4255e 72 69 74 74 65 6e 2e 20 4e 6f 20 74 65 72 6d 69  ritten. No termi
4255f 6e 61 74 6f 72 20 28 50 4f 53 5f 45 4e 44 20 6f  nator (POS_END o
42560 72 20 50 4f 53 5f 43 4f 4c 55 4d 4e 29 20 69 73  r POS_COLUMN) is
42561 0d 0a 20 20 20 20 20 20 2a 2a 20 77 72 69 74 74  ..      ** writt
42562 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74  en to the output
42563 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
42564 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
42565 61 72 69 6e 74 28 26 70 31 2c 20 26 69 31 29 3b  arint(&p1, &i1);
42566 0d 0a 20 20 20 20 20 20 66 74 73 33 47 65 74 44  ..      fts3GetD
42567 65 6c 74 61 56 61 72 69 6e 74 28 26 70 32 2c 20  eltaVarint(&p2, 
42568 26 69 32 29 3b 0d 0a 20 20 20 20 20 20 64 6f 20  &i2);..      do 
42569 7b 0d 0a 20 20 20 20 20 20 20 20 66 74 73 33 50  {..        fts3P
4256a 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  utDeltaVarint(&p
4256b 2c 20 26 69 50 72 65 76 2c 20 28 69 31 3c 69 32  , &iPrev, (i1<i2
4256c 29 20 3f 20 69 31 20 3a 20 69 32 29 3b 20 0d 0a  ) ? i1 : i2); ..
4256d 20 20 20 20 20 20 20 20 69 50 72 65 76 20 2d 3d          iPrev -=
4256e 20 32 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28   2;..        if(
4256f 20 69 31 3d 3d 69 32 20 29 7b 0d 0a 20 20 20 20   i1==i2 ){..    
42570 20 20 20 20 20 20 66 74 73 33 52 65 61 64 4e 65        fts3ReadNe
42571 78 74 50 6f 73 28 26 70 31 2c 20 26 69 31 29 3b  xtPos(&p1, &i1);
42572 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  ..          fts3
42573 52 65 61 64 4e 65 78 74 50 6f 73 28 26 70 32 2c  ReadNextPos(&p2,
42574 20 26 69 32 29 3b 0d 0a 20 20 20 20 20 20 20 20   &i2);..        
42575 7d 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32 20  }else if( i1<i2 
42576 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74  ){..          ft
42577 73 33 52 65 61 64 4e 65 78 74 50 6f 73 28 26 70  s3ReadNextPos(&p
42578 31 2c 20 26 69 31 29 3b 0d 0a 20 20 20 20 20 20  1, &i1);..      
42579 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
4257a 20 20 20 20 66 74 73 33 52 65 61 64 4e 65 78 74      fts3ReadNext
4257b 50 6f 73 28 26 70 32 2c 20 26 69 32 29 3b 0d 0a  Pos(&p2, &i2);..
4257c 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
4257d 20 7d 77 68 69 6c 65 28 20 69 31 21 3d 50 4f 53   }while( i1!=POS
4257e 49 54 49 4f 4e 5f 4c 49 53 54 5f 45 4e 44 20 7c  ITION_LIST_END |
4257f 7c 20 69 32 21 3d 50 4f 53 49 54 49 4f 4e 5f 4c  | i2!=POSITION_L
42580 49 53 54 5f 45 4e 44 20 29 3b 0d 0a 20 20 20 20  IST_END );..    
42581 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6c 31 3c  }else if( iCol1<
42582 69 43 6f 6c 32 20 29 7b 0d 0a 20 20 20 20 20 20  iCol2 ){..      
42583 70 31 20 2b 3d 20 66 74 73 33 50 75 74 43 6f 6c  p1 += fts3PutCol
42584 4e 75 6d 62 65 72 28 26 70 2c 20 69 43 6f 6c 31  Number(&p, iCol1
42585 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 43 6f  );..      fts3Co
42586 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 26 70 2c  lumnlistCopy(&p,
42587 20 26 70 31 29 3b 0d 0a 20 20 20 20 7d 65 6c 73   &p1);..    }els
42588 65 7b 0d 0a 20 20 20 20 20 20 70 32 20 2b 3d 20  e{..      p2 += 
42589 66 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62 65 72  fts3PutColNumber
4258a 28 26 70 2c 20 69 43 6f 6c 32 29 3b 0d 0a 20 20  (&p, iCol2);..  
4258b 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69      fts3Columnli
4258c 73 74 43 6f 70 79 28 26 70 2c 20 26 70 32 29 3b  stCopy(&p, &p2);
4258d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
4258e 20 20 2a 70 2b 2b 20 3d 20 50 4f 53 5f 45 4e 44    *p++ = POS_END
4258f 3b 0d 0a 20 20 2a 70 70 20 3d 20 70 3b 0d 0a 20  ;..  *pp = p;.. 
42590 20 2a 70 70 31 20 3d 20 70 31 20 2b 20 31 3b 0d   *pp1 = p1 + 1;.
42591 0a 20 20 2a 70 70 32 20 3d 20 70 32 20 2b 20 31  .  *pp2 = p2 + 1
42592 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
42593 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
42594 75 73 65 64 20 74 6f 20 6d 65 72 67 65 20 74 77  used to merge tw
42595 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  o position lists
42596 20 69 6e 74 6f 20 6f 6e 65 2e 20 57 68 65 6e 20   into one. When 
42597 69 74 20 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65 64  it is..** called
42598 2c 20 2a 70 70 31 20 61 6e 64 20 2a 70 70 32 20  , *pp1 and *pp2 
42599 6d 75 73 74 20 62 6f 74 68 20 70 6f 69 6e 74 20  must both point 
4259a 74 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  to position list
4259b 73 2e 20 41 20 70 6f 73 69 74 69 6f 6e 2d 6c 69  s. A position-li
4259c 73 74 20 69 73 0d 0a 2a 2a 20 74 68 65 20 70 61  st is..** the pa
4259d 72 74 20 6f 66 20 61 20 64 6f 63 6c 69 73 74 20  rt of a doclist 
4259e 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63  that follows eac
4259f 68 20 64 6f 63 75 6d 65 6e 74 20 69 64 2e 20 46  h document id. F
425a0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61  or example, if a
425a1 20 72 6f 77 0d 0a 2a 2a 20 63 6f 6e 74 61 69 6e   row..** contain
425a2 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 27  s:..**..**     '
425a3 61 20 62 20 63 27 7c 27 78 20 79 20 7a 27 7c 27  a b c'|'x y z'|'
425a4 61 20 62 20 62 20 61 27 0d 0a 2a 2a 0d 0a 2a 2a  a b b a'..**..**
425a5 20 54 68 65 6e 20 74 68 65 20 70 6f 73 69 74 69   Then the positi
425a6 6f 6e 20 6c 69 73 74 20 66 6f 72 20 74 68 69 73  on list for this
425a7 20 72 6f 77 20 66 6f 72 20 74 6f 6b 65 6e 20 27   row for token '
425a8 62 27 20 77 6f 75 6c 64 20 63 6f 6e 73 69 73 74  b' would consist
425a9 20 6f 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20   of:..**..**    
425aa 20 30 78 30 32 20 30 78 30 31 20 30 78 30 32 20   0x02 0x01 0x02 
425ab 30 78 30 33 20 30 78 30 33 20 30 78 30 30 0d 0a  0x03 0x03 0x00..
425ac 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  **..** When this
425ad 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
425ae 73 2c 20 62 6f 74 68 20 2a 70 70 31 20 61 6e 64  s, both *pp1 and
425af 20 2a 70 70 32 20 61 72 65 20 6c 65 66 74 20 70   *pp2 are left p
425b0 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0d 0a  ointing to the..
425b1 2a 2a 20 62 79 74 65 20 66 6f 6c 6c 6f 77 69 6e  ** byte followin
425b2 67 20 74 68 65 20 30 78 30 30 20 74 65 72 6d 69  g the 0x00 termi
425b3 6e 61 74 6f 72 20 6f 66 20 74 68 65 69 72 20 72  nator of their r
425b4 65 73 70 65 63 74 69 76 65 20 70 6f 73 69 74 69  espective positi
425b5 6f 6e 20 6c 69 73 74 73 2e 0d 0a 2a 2a 0d 0a 2a  on lists...**..*
425b6 2a 20 49 66 20 69 73 53 61 76 65 4c 65 66 74 20  * If isSaveLeft 
425b7 69 73 20 30 2c 20 61 6e 20 65 6e 74 72 79 20 69  is 0, an entry i
425b8 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6f  s added to the o
425b9 75 74 70 75 74 20 70 6f 73 69 74 69 6f 6e 20 6c  utput position l
425ba 69 73 74 20 66 6f 72 20 0d 0a 2a 2a 20 65 61 63  ist for ..** eac
425bb 68 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 2a 70  h position in *p
425bc 70 32 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  p2 for which the
425bd 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72  re exists one or
425be 20 6d 6f 72 65 20 70 6f 73 69 74 69 6f 6e 73 20   more positions 
425bf 69 6e 0d 0a 2a 2a 20 2a 70 70 31 20 73 6f 20 74  in..** *pp1 so t
425c0 68 61 74 20 28 70 6f 73 28 2a 70 70 32 29 3e 70  hat (pos(*pp2)>p
425c1 6f 73 28 2a 70 70 31 29 20 26 26 20 70 6f 73 28  os(*pp1) && pos(
425c2 2a 70 70 32 29 2d 70 6f 73 28 2a 70 70 31 29 3c  *pp2)-pos(*pp1)<
425c3 3d 6e 54 6f 6b 65 6e 29 2e 20 69 2e 65 2e 0d 0a  =nToken). i.e...
425c4 2a 2a 20 77 68 65 6e 20 74 68 65 20 2a 70 70 31  ** when the *pp1
425c5 20 74 6f 6b 65 6e 20 61 70 70 65 61 72 73 20 62   token appears b
425c6 65 66 6f 72 65 20 74 68 65 20 2a 70 70 32 20 74  efore the *pp2 t
425c7 6f 6b 65 6e 2c 20 62 75 74 20 6e 6f 74 20 6d 6f  oken, but not mo
425c8 72 65 20 74 68 61 6e 20 6e 54 6f 6b 65 6e 0d 0a  re than nToken..
425c9 2a 2a 20 73 6c 6f 74 73 20 62 65 66 6f 72 65 20  ** slots before 
425ca 69 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 65 2e 67 2e  it...**..** e.g.
425cb 20 6e 54 6f 6b 65 6e 3d 3d 31 20 73 65 61 72 63   nToken==1 searc
425cc 68 65 73 20 66 6f 72 20 61 64 6a 61 63 65 6e 74  hes for adjacent
425cd 20 70 6f 73 69 74 69 6f 6e 73 2e 0d 0a 2a 2f 0d   positions...*/.
425ce 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
425cf 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72  PoslistPhraseMer
425d0 67 65 28 0d 0a 20 20 63 68 61 72 20 2a 2a 70 70  ge(..  char **pp
425d1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
425d2 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
425d3 3a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 6f  : Preallocated o
425d4 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0d  utput buffer */.
425d5 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 20  .  int nToken,  
425d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
425d7 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69     /* Maximum di
425d8 66 66 65 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65  fference in toke
425d9 6e 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0d 0a  n positions */..
425da 20 20 69 6e 74 20 69 73 53 61 76 65 4c 65 66 74    int isSaveLeft
425db 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
425dc 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 6c 65    /* Save the le
425dd 66 74 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0d 0a  ft position */..
425de 20 20 69 6e 74 20 69 73 45 78 61 63 74 2c 20 20    int isExact,  
425df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
425e0 20 20 2f 2a 20 49 66 20 2a 70 70 31 20 69 73 20    /* If *pp1 is 
425e1 65 78 61 63 74 6c 79 20 6e 54 6f 6b 65 6e 73 20  exactly nTokens 
425e2 62 65 66 6f 72 65 20 2a 70 70 32 20 2a 2f 0d 0a  before *pp2 */..
425e3 20 20 63 68 61 72 20 2a 2a 70 70 31 2c 20 20 20    char **pp1,   
425e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
425e5 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4c 65 66    /* IN/OUT: Lef
425e6 74 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0d  t input list */.
425e7 0a 20 20 63 68 61 72 20 2a 2a 70 70 32 20 20 20  .  char **pp2   
425e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
425e9 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 52 69     /* IN/OUT: Ri
425ea 67 68 74 20 69 6e 70 75 74 20 6c 69 73 74 20 2a  ght input list *
425eb 2f 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20 2a 70  /..){..  char *p
425ec 20 3d 20 2a 70 70 3b 0d 0a 20 20 63 68 61 72 20   = *pp;..  char 
425ed 2a 70 31 20 3d 20 2a 70 70 31 3b 0d 0a 20 20 63  *p1 = *pp1;..  c
425ee 68 61 72 20 2a 70 32 20 3d 20 2a 70 70 32 3b 0d  har *p2 = *pp2;.
425ef 0a 20 20 69 6e 74 20 69 43 6f 6c 31 20 3d 20 30  .  int iCol1 = 0
425f0 3b 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 32 20 3d  ;..  int iCol2 =
425f1 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 4e 65 76 65   0;....  /* Neve
425f2 72 20 73 65 74 20 62 6f 74 68 20 69 73 53 61 76  r set both isSav
425f3 65 4c 65 66 74 20 61 6e 64 20 69 73 45 78 61 63  eLeft and isExac
425f4 74 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 69  t for the same i
425f5 6e 76 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0d 0a 20  nvocation. */.. 
425f6 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 4c   assert( isSaveL
425f7 65 66 74 3d 3d 30 20 7c 7c 20 69 73 45 78 61 63  eft==0 || isExac
425f8 74 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 61 73 73  t==0 );....  ass
425f9 65 72 74 28 20 70 21 3d 30 20 26 26 20 2a 70 31  ert( p!=0 && *p1
425fa 21 3d 30 20 26 26 20 2a 70 32 21 3d 30 20 29 3b  !=0 && *p2!=0 );
425fb 0d 0a 20 20 69 66 28 20 2a 70 31 3d 3d 50 4f 53  ..  if( *p1==POS
425fc 5f 43 4f 4c 55 4d 4e 20 29 7b 20 0d 0a 20 20 20  _COLUMN ){ ..   
425fd 20 70 31 2b 2b 3b 0d 0a 20 20 20 20 70 31 20 2b   p1++;..    p1 +
425fe 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
425ff 56 61 72 69 6e 74 33 32 28 70 31 2c 20 26 69 43  Varint32(p1, &iC
42600 6f 6c 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66  ol1);..  }..  if
42601 28 20 2a 70 32 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  ( *p2==POS_COLUM
42602 4e 20 29 7b 20 0d 0a 20 20 20 20 70 32 2b 2b 3b  N ){ ..    p2++;
42603 0d 0a 20 20 20 20 70 32 20 2b 3d 20 73 71 6c 69  ..    p2 += sqli
42604 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
42605 33 32 28 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0d  32(p2, &iCol2);.
42606 0a 20 20 7d 0d 0a 0d 0a 20 20 77 68 69 6c 65 28  .  }....  while(
42607 20 31 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 69   1 ){..    if( i
42608 43 6f 6c 31 3d 3d 69 43 6f 6c 32 20 29 7b 0d 0a  Col1==iCol2 ){..
42609 20 20 20 20 20 20 63 68 61 72 20 2a 70 53 61 76        char *pSav
4260a 65 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 73 71  e = p;..      sq
4260b 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65  lite3_int64 iPre
4260c 76 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71  v = 0;..      sq
4260d 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73  lite3_int64 iPos
4260e 31 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 73 71  1 = 0;..      sq
4260f 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73  lite3_int64 iPos
42610 32 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 20 20  2 = 0;....      
42611 69 66 28 20 69 43 6f 6c 31 20 29 7b 0d 0a 20 20  if( iCol1 ){..  
42612 20 20 20 20 20 20 2a 70 2b 2b 20 3d 20 50 4f 53        *p++ = POS
42613 5f 43 4f 4c 55 4d 4e 3b 0d 0a 20 20 20 20 20 20  _COLUMN;..      
42614 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33 46 74    p += sqlite3Ft
42615 73 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 69  s3PutVarint(p, i
42616 43 6f 6c 31 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  Col1);..      }.
42617 0a 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ...      assert(
42618 20 2a 70 31 21 3d 50 4f 53 5f 45 4e 44 20 26 26   *p1!=POS_END &&
42619 20 2a 70 31 21 3d 50 4f 53 5f 43 4f 4c 55 4d 4e   *p1!=POS_COLUMN
4261a 20 29 3b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
4261b 74 28 20 2a 70 32 21 3d 50 4f 53 5f 45 4e 44 20  t( *p2!=POS_END 
4261c 26 26 20 2a 70 32 21 3d 50 4f 53 5f 43 4f 4c 55  && *p2!=POS_COLU
4261d 4d 4e 20 29 3b 0d 0a 20 20 20 20 20 20 66 74 73  MN );..      fts
4261e 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3GetDeltaVarint(
4261f 26 70 31 2c 20 26 69 50 6f 73 31 29 3b 20 69 50  &p1, &iPos1); iP
42620 6f 73 31 20 2d 3d 20 32 3b 0d 0a 20 20 20 20 20  os1 -= 2;..     
42621 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
42622 69 6e 74 28 26 70 32 2c 20 26 69 50 6f 73 32 29  int(&p2, &iPos2)
42623 3b 20 69 50 6f 73 32 20 2d 3d 20 32 3b 0d 0a 0d  ; iPos2 -= 2;...
42624 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
42625 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
42626 69 50 6f 73 32 3d 3d 69 50 6f 73 31 2b 6e 54 6f  iPos2==iPos1+nTo
42627 6b 65 6e 20 0d 0a 20 20 20 20 20 20 20 20 20 7c  ken ..         |
42628 7c 20 28 69 73 45 78 61 63 74 3d 3d 30 20 26 26  | (isExact==0 &&
42629 20 69 50 6f 73 32 3e 69 50 6f 73 31 20 26 26 20   iPos2>iPos1 && 
4262a 69 50 6f 73 32 3c 3d 69 50 6f 73 31 2b 6e 54 6f  iPos2<=iPos1+nTo
4262b 6b 65 6e 29 20 0d 0a 20 20 20 20 20 20 20 20 29  ken) ..        )
4262c 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
4262d 69 74 65 33 5f 69 6e 74 36 34 20 69 53 61 76 65  ite3_int64 iSave
4262e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 53 61  ;..          iSa
4262f 76 65 20 3d 20 69 73 53 61 76 65 4c 65 66 74 20  ve = isSaveLeft 
42630 3f 20 69 50 6f 73 31 20 3a 20 69 50 6f 73 32 3b  ? iPos1 : iPos2;
42631 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  ..          fts3
42632 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  PutDeltaVarint(&
42633 70 2c 20 26 69 50 72 65 76 2c 20 69 53 61 76 65  p, &iPrev, iSave
42634 2b 32 29 3b 20 69 50 72 65 76 20 2d 3d 20 32 3b  +2); iPrev -= 2;
42635 0d 0a 20 20 20 20 20 20 20 20 20 20 70 53 61 76  ..          pSav
42636 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  e = 0;..        
42637 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0d 0a    assert( p );..
42638 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
42639 20 20 20 69 66 28 20 28 21 69 73 53 61 76 65 4c     if( (!isSaveL
4263a 65 66 74 20 26 26 20 69 50 6f 73 32 3c 3d 28 69  eft && iPos2<=(i
4263b 50 6f 73 31 2b 6e 54 6f 6b 65 6e 29 29 20 7c 7c  Pos1+nToken)) ||
4263c 20 69 50 6f 73 32 3c 3d 69 50 6f 73 31 20 29 7b   iPos2<=iPos1 ){
4263d 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
4263e 28 2a 70 32 26 30 78 46 45 29 3d 3d 30 20 29 20  (*p2&0xFE)==0 ) 
4263f 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20  break;..        
42640 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
42641 72 69 6e 74 28 26 70 32 2c 20 26 69 50 6f 73 32  rint(&p2, &iPos2
42642 29 3b 20 69 50 6f 73 32 20 2d 3d 20 32 3b 0d 0a  ); iPos2 -= 2;..
42643 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
42644 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 2a            if( (*
42645 70 31 26 30 78 46 45 29 3d 3d 30 20 29 20 62 72  p1&0xFE)==0 ) br
42646 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  eak;..          
42647 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
42648 6e 74 28 26 70 31 2c 20 26 69 50 6f 73 31 29 3b  nt(&p1, &iPos1);
42649 20 69 50 6f 73 31 20 2d 3d 20 32 3b 0d 0a 20 20   iPos1 -= 2;..  
4264a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
4264b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 70 53  ....      if( pS
4264c 61 76 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ave ){..        
4264d 61 73 73 65 72 74 28 20 70 70 20 26 26 20 70 20  assert( pp && p 
4264e 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 20 3d 20  );..        p = 
4264f 70 53 61 76 65 3b 0d 0a 20 20 20 20 20 20 7d 0d  pSave;..      }.
42650 0a 0d 0a 20 20 20 20 20 20 66 74 73 33 43 6f 6c  ...      fts3Col
42651 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26  umnlistCopy(0, &
42652 70 31 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33  p1);..      fts3
42653 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30  ColumnlistCopy(0
42654 2c 20 26 70 32 29 3b 0d 0a 20 20 20 20 20 20 61  , &p2);..      a
42655 73 73 65 72 74 28 20 28 2a 70 31 26 30 78 46 45  ssert( (*p1&0xFE
42656 29 3d 3d 30 20 26 26 20 28 2a 70 32 26 30 78 46  )==0 && (*p2&0xF
42657 45 29 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20  E)==0 );..      
42658 69 66 28 20 30 3d 3d 2a 70 31 20 7c 7c 20 30 3d  if( 0==*p1 || 0=
42659 3d 2a 70 32 20 29 20 62 72 65 61 6b 3b 0d 0a 0d  =*p2 ) break;...
4265a 0a 20 20 20 20 20 20 70 31 2b 2b 3b 0d 0a 20 20  .      p1++;..  
4265b 20 20 20 20 70 31 20 2b 3d 20 73 71 6c 69 74 65      p1 += sqlite
4265c 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
4265d 28 70 31 2c 20 26 69 43 6f 6c 31 29 3b 0d 0a 20  (p1, &iCol1);.. 
4265e 20 20 20 20 20 70 32 2b 2b 3b 0d 0a 20 20 20 20       p2++;..    
4265f 20 20 70 32 20 2b 3d 20 73 71 6c 69 74 65 33 46    p2 += sqlite3F
42660 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
42661 32 2c 20 26 69 43 6f 6c 32 29 3b 0d 0a 20 20 20  2, &iCol2);..   
42662 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41 64 76   }....    /* Adv
42663 61 6e 63 65 20 70 6f 69 6e 74 65 72 20 70 31 20  ance pointer p1 
42664 6f 72 20 70 32 20 28 77 68 69 63 68 65 76 65 72  or p2 (whichever
42665 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
42666 74 68 65 20 73 6d 61 6c 6c 65 72 20 6f 66 0d 0a  the smaller of..
42667 20 20 20 20 2a 2a 20 69 43 6f 6c 31 20 61 6e 64      ** iCol1 and
42668 20 69 43 6f 6c 32 29 20 73 6f 20 74 68 61 74 20   iCol2) so that 
42669 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 65 69 74  it points to eit
4266a 68 65 72 20 74 68 65 20 30 78 30 30 20 74 68 61  her the 0x00 tha
4266b 74 20 6d 61 72 6b 73 20 74 68 65 0d 0a 20 20 20  t marks the..   
4266c 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70   ** end of the p
4266d 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2c 20 6f 72  osition list, or
4266e 20 74 68 65 20 30 78 30 31 20 74 68 61 74 20 70   the 0x01 that p
4266f 72 65 63 65 64 65 73 20 74 68 65 20 6e 65 78 74  recedes the next
42670 20 0d 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e   ..    ** column
42671 2d 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 70  -number in the p
42672 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e 20 0d 0a  osition list. ..
42673 20 20 20 20 2a 2f 0d 0a 20 20 20 20 65 6c 73 65      */..    else
42674 20 69 66 28 20 69 43 6f 6c 31 3c 69 43 6f 6c 32   if( iCol1<iCol2
42675 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33 43   ){..      fts3C
42676 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c  olumnlistCopy(0,
42677 20 26 70 31 29 3b 0d 0a 20 20 20 20 20 20 69 66   &p1);..      if
42678 28 20 30 3d 3d 2a 70 31 20 29 20 62 72 65 61 6b  ( 0==*p1 ) break
42679 3b 0d 0a 20 20 20 20 20 20 70 31 2b 2b 3b 0d 0a  ;..      p1++;..
4267a 20 20 20 20 20 20 70 31 20 2b 3d 20 73 71 6c 69        p1 += sqli
4267b 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
4267c 33 32 28 70 31 2c 20 26 69 43 6f 6c 31 29 3b 0d  32(p1, &iCol1);.
4267d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
4267e 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73     fts3Columnlis
4267f 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b 0d 0a  tCopy(0, &p2);..
42680 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a 70 32        if( 0==*p2
42681 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20   ) break;..     
42682 20 70 32 2b 2b 3b 0d 0a 20 20 20 20 20 20 70 32   p2++;..      p2
42683 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
42684 65 74 56 61 72 69 6e 74 33 32 28 70 32 2c 20 26  etVarint32(p2, &
42685 69 43 6f 6c 32 29 3b 0d 0a 20 20 20 20 7d 0d 0a  iCol2);..    }..
42686 20 20 7d 0d 0a 0d 0a 20 20 66 74 73 33 50 6f 73    }....  fts3Pos
42687 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29  listCopy(0, &p2)
42688 3b 0d 0a 20 20 66 74 73 33 50 6f 73 6c 69 73 74  ;..  fts3Poslist
42689 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0d 0a 20  Copy(0, &p1);.. 
4268a 20 2a 70 70 31 20 3d 20 70 31 3b 0d 0a 20 20 2a   *pp1 = p1;..  *
4268b 70 70 32 20 3d 20 70 32 3b 0d 0a 20 20 69 66 28  pp2 = p2;..  if(
4268c 20 2a 70 70 3d 3d 70 20 29 7b 0d 0a 20 20 20 20   *pp==p ){..    
4268d 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a  return 0;..  }..
4268e 20 20 2a 70 2b 2b 20 3d 20 30 78 30 30 3b 0d 0a    *p++ = 0x00;..
4268f 20 20 2a 70 70 20 3d 20 70 3b 0d 0a 20 20 72 65    *pp = p;..  re
42690 74 75 72 6e 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn 1;..}..../*
42691 0d 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 70  ..** Merge two p
42692 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 73 20 61 73  osition-lists as
42693 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
42694 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 2e 20   NEAR operator. 
42695 54 68 65 20 61 72 67 75 6d 65 6e 74 0d 0a 2a 2a  The argument..**
42696 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20   position lists 
42697 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68  correspond to th
42698 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74  e left and right
42699 20 70 68 72 61 73 65 73 20 6f 66 20 61 6e 20 65   phrases of an e
4269a 78 70 72 65 73 73 69 6f 6e 20 0d 0a 2a 2a 20 6c  xpression ..** l
4269b 69 6b 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ike:..**..**    
4269c 20 22 70 68 72 61 73 65 20 31 22 20 4e 45 41 52   "phrase 1" NEAR
4269d 20 22 70 68 72 61 73 65 20 6e 75 6d 62 65 72 20   "phrase number 
4269e 32 22 0d 0a 2a 2a 0d 0a 2a 2a 20 50 6f 73 69 74  2"..**..** Posit
4269f 69 6f 6e 20 6c 69 73 74 20 2a 70 70 31 20 63 6f  ion list *pp1 co
426a0 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
426a1 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
426a2 6f 66 20 74 68 65 20 4e 45 41 52 20 0d 0a 2a 2a  of the NEAR ..**
426a3 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
426a4 2a 70 70 32 20 74 6f 20 74 68 65 20 72 69 67 68  *pp2 to the righ
426a5 74 2e 20 41 73 20 75 73 75 61 6c 2c 20 74 68 65  t. As usual, the
426a6 20 69 6e 64 65 78 65 73 20 69 6e 20 74 68 65 20   indexes in the 
426a7 70 6f 73 69 74 69 6f 6e 20 0d 0a 2a 2a 20 6c 69  position ..** li
426a8 73 74 73 20 61 72 65 20 74 68 65 20 6f 66 66 73  sts are the offs
426a9 65 74 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20  ets of the last 
426aa 74 6f 6b 65 6e 20 69 6e 20 65 61 63 68 20 70 68  token in each ph
426ab 72 61 73 65 20 28 74 6f 6b 65 6e 73 20 22 31 22  rase (tokens "1"
426ac 20 61 6e 64 20 22 32 22 20 0d 0a 2a 2a 20 69 6e   and "2" ..** in
426ad 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
426ae 76 65 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ve)...**..** The
426af 20 6f 75 74 70 75 74 20 70 6f 73 69 74 69 6f 6e   output position
426b0 20 6c 69 73 74 20 2d 20 77 72 69 74 74 65 6e 20   list - written 
426b1 74 6f 20 2a 70 70 20 2d 20 69 73 20 61 20 63 6f  to *pp - is a co
426b2 70 79 20 6f 66 20 2a 70 70 32 20 77 69 74 68 20  py of *pp2 with 
426b3 74 68 6f 73 65 0d 0a 2a 2a 20 65 6e 74 72 69 65  those..** entrie
426b4 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 73  s that are not s
426b5 75 66 66 69 63 69 65 6e 74 6c 79 20 4e 45 41 52  ufficiently NEAR
426b6 20 65 6e 74 72 69 65 73 20 69 6e 20 2a 70 70 31   entries in *pp1
426b7 20 72 65 6d 6f 76 65 64 2e 0d 0a 2a 2f 0d 0a 73   removed...*/..s
426b8 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 6f  tatic int fts3Po
426b9 73 6c 69 73 74 4e 65 61 72 4d 65 72 67 65 28 0d  slistNearMerge(.
426ba 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20  .  char **pp,   
426bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
426bc 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
426bd 66 65 72 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a  fer */..  char *
426be 61 54 6d 70 2c 20 20 20 20 20 20 20 20 20 20 20  aTmp,           
426bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
426c0 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 73 70  porary buffer sp
426c1 61 63 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52  ace */..  int nR
426c2 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
426c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
426c4 69 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65 20  imum difference 
426c5 69 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f  in token positio
426c6 6e 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4c 65  ns */..  int nLe
426c7 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ft,             
426c8 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
426c9 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65 20 69  mum difference i
426ca 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e  n token position
426cb 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70  s */..  char **p
426cc 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  p1,             
426cd 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
426ce 54 3a 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69  T: Left input li
426cf 73 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a  st */..  char **
426d0 70 70 32 20 20 20 20 20 20 20 20 20 20 20 20 20  pp2             
426d1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
426d2 55 54 3a 20 52 69 67 68 74 20 69 6e 70 75 74 20  UT: Right input 
426d3 6c 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63  list */..){..  c
426d4 68 61 72 20 2a 70 31 20 3d 20 2a 70 70 31 3b 0d  har *p1 = *pp1;.
426d5 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20 2a 70  .  char *p2 = *p
426d6 70 32 3b 0d 0a 0d 0a 20 20 63 68 61 72 20 2a 70  p2;....  char *p
426d7 54 6d 70 31 20 3d 20 61 54 6d 70 3b 0d 0a 20 20  Tmp1 = aTmp;..  
426d8 63 68 61 72 20 2a 70 54 6d 70 32 3b 0d 0a 20 20  char *pTmp2;..  
426d9 63 68 61 72 20 2a 61 54 6d 70 32 3b 0d 0a 20 20  char *aTmp2;..  
426da 69 6e 74 20 72 65 73 20 3d 20 31 3b 0d 0a 0d 0a  int res = 1;....
426db 20 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68 72    fts3PoslistPhr
426dc 61 73 65 4d 65 72 67 65 28 26 70 54 6d 70 31 2c  aseMerge(&pTmp1,
426dd 20 6e 52 69 67 68 74 2c 20 30 2c 20 30 2c 20 70   nRight, 0, 0, p
426de 70 31 2c 20 70 70 32 29 3b 0d 0a 20 20 61 54 6d  p1, pp2);..  aTm
426df 70 32 20 3d 20 70 54 6d 70 32 20 3d 20 70 54 6d  p2 = pTmp2 = pTm
426e0 70 31 3b 0d 0a 20 20 2a 70 70 31 20 3d 20 70 31  p1;..  *pp1 = p1
426e1 3b 0d 0a 20 20 2a 70 70 32 20 3d 20 70 32 3b 0d  ;..  *pp2 = p2;.
426e2 0a 20 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68  .  fts3PoslistPh
426e3 72 61 73 65 4d 65 72 67 65 28 26 70 54 6d 70 32  raseMerge(&pTmp2
426e4 2c 20 6e 4c 65 66 74 2c 20 31 2c 20 30 2c 20 70  , nLeft, 1, 0, p
426e5 70 32 2c 20 70 70 31 29 3b 0d 0a 20 20 69 66 28  p2, pp1);..  if(
426e6 20 70 54 6d 70 31 21 3d 61 54 6d 70 20 26 26 20   pTmp1!=aTmp && 
426e7 70 54 6d 70 32 21 3d 61 54 6d 70 32 20 29 7b 0d  pTmp2!=aTmp2 ){.
426e8 0a 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74  .    fts3Poslist
426e9 4d 65 72 67 65 28 70 70 2c 20 26 61 54 6d 70 2c  Merge(pp, &aTmp,
426ea 20 26 61 54 6d 70 32 29 3b 0d 0a 20 20 7d 65 6c   &aTmp2);..  }el
426eb 73 65 20 69 66 28 20 70 54 6d 70 31 21 3d 61 54  se if( pTmp1!=aT
426ec 6d 70 20 29 7b 0d 0a 20 20 20 20 66 74 73 33 50  mp ){..    fts3P
426ed 6f 73 6c 69 73 74 43 6f 70 79 28 70 70 2c 20 26  oslistCopy(pp, &
426ee 61 54 6d 70 29 3b 0d 0a 20 20 7d 65 6c 73 65 20  aTmp);..  }else 
426ef 69 66 28 20 70 54 6d 70 32 21 3d 61 54 6d 70 32  if( pTmp2!=aTmp2
426f0 20 29 7b 0d 0a 20 20 20 20 66 74 73 33 50 6f 73   ){..    fts3Pos
426f1 6c 69 73 74 43 6f 70 79 28 70 70 2c 20 26 61 54  listCopy(pp, &aT
426f2 6d 70 32 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  mp2);..  }else{.
426f3 0a 20 20 20 20 72 65 73 20 3d 20 30 3b 0d 0a 20  .    res = 0;.. 
426f4 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72   }....  return r
426f5 65 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  es;..}..../* ..*
426f6 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
426f7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
426f8 73 20 75 73 65 64 20 74 6f 20 6d 65 72 67 65 20  s used to merge 
426f9 74 6f 67 65 74 68 65 72 20 74 68 65 20 28 70 6f  together the (po
426fa 74 65 6e 74 69 61 6c 6c 79 0d 0a 2a 2a 20 6c 61  tentially..** la
426fb 72 67 65 20 6e 75 6d 62 65 72 20 6f 66 29 20 64  rge number of) d
426fc 6f 63 6c 69 73 74 73 20 66 6f 72 20 65 61 63 68  oclists for each
426fd 20 74 65 72 6d 20 74 68 61 74 20 6d 61 74 63 68   term that match
426fe 65 73 20 61 20 70 72 65 66 69 78 20 71 75 65 72  es a prefix quer
426ff 79 2e 0d 0a 2a 2a 20 53 65 65 20 66 75 6e 63 74  y...** See funct
42700 69 6f 6e 20 66 74 73 33 54 65 72 6d 53 65 6c 65  ion fts3TermSele
42701 63 74 4d 65 72 67 65 28 29 20 66 6f 72 20 64 65  ctMerge() for de
42702 74 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 74 79 70 65  tails...*/..type
42703 64 65 66 20 73 74 72 75 63 74 20 54 65 72 6d 53  def struct TermS
42704 65 6c 65 63 74 20 54 65 72 6d 53 65 6c 65 63 74  elect TermSelect
42705 3b 0d 0a 73 74 72 75 63 74 20 54 65 72 6d 53 65  ;..struct TermSe
42706 6c 65 63 74 20 7b 0d 0a 20 20 63 68 61 72 20 2a  lect {..  char *
42707 61 61 4f 75 74 70 75 74 5b 31 36 5d 3b 20 20 20  aaOutput[16];   
42708 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c            /* Mal
42709 6c 6f 63 27 64 20 6f 75 74 70 75 74 20 62 75 66  loc'd output buf
4270a 66 65 72 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 61  fers */..  int a
4270b 6e 4f 75 74 70 75 74 5b 31 36 5d 3b 20 20 20 20  nOutput[16];    
4270c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
4270d 7a 65 20 65 61 63 68 20 6f 75 74 70 75 74 20 62  ze each output b
4270e 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a  uffer in bytes *
4270f 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  /..};..../*..** 
42710 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
42711 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 61 20   used to read a 
42712 73 69 6e 67 6c 65 20 76 61 72 69 6e 74 20 66 72  single varint fr
42713 6f 6d 20 61 20 62 75 66 66 65 72 2e 20 50 61 72  om a buffer. Par
42714 61 6d 65 74 65 72 0d 0a 2a 2a 20 70 45 6e 64 20  ameter..** pEnd 
42715 70 6f 69 6e 74 73 20 31 20 62 79 74 65 20 70 61  points 1 byte pa
42716 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  st the end of th
42717 65 20 62 75 66 66 65 72 2e 20 57 68 65 6e 20 74  e buffer. When t
42718 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0d  his function is.
42719 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 69 66 20 2a  .** called, if *
4271a 70 70 20 70 6f 69 6e 74 73 20 74 6f 20 70 45 6e  pp points to pEn
4271b 64 20 6f 72 20 67 72 65 61 74 65 72 2c 20 74 68  d or greater, th
4271c 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  en the end of th
4271d 65 20 62 75 66 66 65 72 0d 0a 2a 2a 20 68 61 73  e buffer..** has
4271e 20 62 65 65 6e 20 72 65 61 63 68 65 64 2e 20 49   been reached. I
4271f 6e 20 74 68 69 73 20 63 61 73 65 20 2a 70 70 20  n this case *pp 
42720 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20  is set to 0 and 
42721 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  the function ret
42722 75 72 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  urns...**..** If
42723 20 2a 70 70 20 64 6f 65 73 20 6e 6f 74 20 70 6f   *pp does not po
42724 69 6e 74 20 74 6f 20 6f 72 20 70 61 73 74 20 70  int to or past p
42725 45 6e 64 2c 20 74 68 65 6e 20 61 20 73 69 6e 67  End, then a sing
42726 6c 65 20 76 61 72 69 6e 74 20 69 73 20 72 65 61  le varint is rea
42727 64 0d 0a 2a 2a 20 66 72 6f 6d 20 2a 70 70 2e 20  d..** from *pp. 
42728 2a 70 70 20 69 73 20 74 68 65 6e 20 73 65 74 20  *pp is then set 
42729 74 6f 20 70 6f 69 6e 74 20 31 20 62 79 74 65 20  to point 1 byte 
4272a 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
4272b 74 68 65 20 72 65 61 64 20 76 61 72 69 6e 74 2e  the read varint.
4272c 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 62 44 65 73  ..**..** If bDes
4272d 63 49 64 78 20 69 73 20 66 61 6c 73 65 2c 20 74  cIdx is false, t
4272e 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 69 73  he value read is
4272f 20 61 64 64 65 64 20 74 6f 20 2a 70 56 61 6c 20   added to *pVal 
42730 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
42731 2e 0d 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 74  ...** If it is t
42732 72 75 65 2c 20 74 68 65 20 76 61 6c 75 65 20 72  rue, the value r
42733 65 61 64 20 69 73 20 73 75 62 74 72 61 63 74 65  ead is subtracte
42734 64 20 66 72 6f 6d 20 2a 70 56 61 6c 20 62 65 66  d from *pVal bef
42735 6f 72 65 20 74 68 69 73 20 0d 0a 2a 2a 20 66 75  ore this ..** fu
42736 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0d  nction returns..
42737 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
42738 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
42739 69 6e 74 33 28 0d 0a 20 20 63 68 61 72 20 2a 2a  int3(..  char **
4273a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
4273b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
4273c 55 54 3a 20 50 6f 69 6e 74 20 74 6f 20 72 65 61  UT: Point to rea
4273d 64 20 76 61 72 69 6e 74 20 66 72 6f 6d 20 2a 2f  d varint from */
4273e 0d 0a 20 20 63 68 61 72 20 2a 70 45 6e 64 2c 20  ..  char *pEnd, 
4273f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42740 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 62 75      /* End of bu
42741 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 62  ffer */..  int b
42742 44 65 73 63 49 64 78 2c 20 20 20 20 20 20 20 20  DescIdx,        
42743 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
42744 75 65 20 69 66 20 64 6f 63 69 64 73 20 61 72 65  ue if docids are
42745 20 64 65 73 63 65 6e 64 69 6e 67 20 2a 2f 0d 0a   descending */..
42746 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
42747 2a 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20  *pVal           
42748 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 49 6e 74    /* IN/OUT: Int
42749 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0d 0a 29  eger value */..)
4274a 7b 0d 0a 20 20 69 66 28 20 2a 70 70 3e 3d 70 45  {..  if( *pp>=pE
4274b 6e 64 20 29 7b 0d 0a 20 20 20 20 2a 70 70 20 3d  nd ){..    *pp =
4274c 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20   0;..  }else{.. 
4274d 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
4274e 20 69 56 61 6c 3b 0d 0a 20 20 20 20 2a 70 70 20   iVal;..    *pp 
4274f 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
42750 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 26 69 56  tVarint(*pp, &iV
42751 61 6c 29 3b 0d 0a 20 20 20 20 69 66 28 20 62 44  al);..    if( bD
42752 65 73 63 49 64 78 20 29 7b 0d 0a 20 20 20 20 20  escIdx ){..     
42753 20 2a 70 56 61 6c 20 2d 3d 20 69 56 61 6c 3b 0d   *pVal -= iVal;.
42754 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
42755 20 20 20 2a 70 56 61 6c 20 2b 3d 20 69 56 61 6c     *pVal += iVal
42756 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d  ;..    }..  }..}
42757 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
42758 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
42759 20 74 6f 20 77 72 69 74 65 20 61 20 73 69 6e 67   to write a sing
4275a 6c 65 20 76 61 72 69 6e 74 20 74 6f 20 61 20 62  le varint to a b
4275b 75 66 66 65 72 2e 20 54 68 65 20 76 61 72 69 6e  uffer. The varin
4275c 74 0d 0a 2a 2a 20 69 73 20 77 72 69 74 74 65 6e  t..** is written
4275d 20 74 6f 20 2a 70 70 2e 20 42 65 66 6f 72 65 20   to *pp. Before 
4275e 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 70 20 69  returning, *pp i
4275f 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 31  s set to point 1
42760 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0d 0a   byte past the..
42761 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 76 61  ** end of the va
42762 6c 75 65 20 77 72 69 74 74 65 6e 2e 0d 0a 2a 2a  lue written...**
42763 0d 0a 2a 2a 20 49 66 20 2a 70 62 46 69 72 73 74  ..** If *pbFirst
42764 20 69 73 20 7a 65 72 6f 20 77 68 65 6e 20 74 68   is zero when th
42765 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
42766 61 6c 6c 65 64 2c 20 74 68 65 20 76 61 6c 75 65  alled, the value
42767 20 77 72 69 74 74 65 6e 20 74 6f 0d 0a 2a 2a 20   written to..** 
42768 74 68 65 20 62 75 66 66 65 72 20 69 73 20 74 68  the buffer is th
42769 61 74 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20  at of parameter 
4276a 69 56 61 6c 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49  iVal. ..**..** I
4276b 66 20 2a 70 62 46 69 72 73 74 20 69 73 20 6e 6f  f *pbFirst is no
4276c 6e 2d 7a 65 72 6f 20 77 68 65 6e 20 74 68 69 73  n-zero when this
4276d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
4276e 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 76 61  led, then the va
4276f 6c 75 65 20 0d 0a 2a 2a 20 77 72 69 74 74 65 6e  lue ..** written
42770 20 69 73 20 65 69 74 68 65 72 20 28 69 56 61 6c   is either (iVal
42771 2d 2a 70 69 50 72 65 76 29 20 28 69 66 20 62 44  -*piPrev) (if bD
42772 65 73 63 49 64 78 20 69 73 20 7a 65 72 6f 29 20  escIdx is zero) 
42773 6f 72 20 28 2a 70 69 50 72 65 76 2d 69 56 61 6c  or (*piPrev-iVal
42774 29 0d 0a 2a 2a 20 28 69 66 20 62 44 65 73 63 49  )..** (if bDescI
42775 64 78 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 29 2e  dx is non-zero).
42776 0d 0a 2a 2a 0d 0a 2a 2a 20 42 65 66 6f 72 65 20  ..**..** Before 
42777 72 65 74 75 72 6e 69 6e 67 2c 20 74 68 69 73 20  returning, this 
42778 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
42779 73 65 74 73 20 2a 70 62 46 69 72 73 74 20 74 6f  sets *pbFirst to
4277a 20 31 20 61 6e 64 20 2a 70 69 50 72 65 76 0d 0a   1 and *piPrev..
4277b 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ** to the value 
4277c 6f 66 20 70 61 72 61 6d 65 74 65 72 20 69 56 61  of parameter iVa
4277d 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  l...*/..static v
4277e 6f 69 64 20 66 74 73 33 50 75 74 44 65 6c 74 61  oid fts3PutDelta
4277f 56 61 72 69 6e 74 33 28 0d 0a 20 20 63 68 61 72  Varint3(..  char
42780 20 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20   **pp,          
42781 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
42782 4e 2f 4f 55 54 3a 20 4f 75 74 70 75 74 20 70 6f  N/OUT: Output po
42783 69 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  inter */..  int 
42784 62 44 65 73 63 49 64 78 2c 20 20 20 20 20 20 20  bDescIdx,       
42785 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
42786 72 75 65 20 66 6f 72 20 64 65 73 63 65 6e 64 69  rue for descendi
42787 6e 67 20 64 6f 63 69 64 73 20 2a 2f 0d 0a 20 20  ng docids */..  
42788 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
42789 69 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20  iPrev,          
4278a 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 72 65 76 69  /* IN/OUT: Previ
4278b 6f 75 73 20 76 61 6c 75 65 20 77 72 69 74 74 65  ous value writte
4278c 6e 20 74 6f 20 6c 69 73 74 20 2a 2f 0d 0a 20 20  n to list */..  
4278d 69 6e 74 20 2a 70 62 46 69 72 73 74 2c 20 20 20  int *pbFirst,   
4278e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4278f 2f 2a 20 49 4e 2f 4f 55 54 3a 20 54 72 75 65 20  /* IN/OUT: True 
42790 61 66 74 65 72 20 66 69 72 73 74 20 69 6e 74 20  after first int 
42791 77 72 69 74 74 65 6e 20 2a 2f 0d 0a 20 20 73 71  written */..  sq
42792 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 56 61 6c  lite3_int64 iVal
42793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42794 20 57 72 69 74 65 20 74 68 69 73 20 76 61 6c 75   Write this valu
42795 65 20 74 6f 20 74 68 65 20 6c 69 73 74 20 2a 2f  e to the list */
42796 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ..){..  sqlite3_
42797 69 6e 74 36 34 20 69 57 72 69 74 65 3b 0d 0a 20  int64 iWrite;.. 
42798 20 69 66 28 20 62 44 65 73 63 49 64 78 3d 3d 30   if( bDescIdx==0
42799 20 7c 7c 20 2a 70 62 46 69 72 73 74 3d 3d 30 20   || *pbFirst==0 
4279a 29 7b 0d 0a 20 20 20 20 69 57 72 69 74 65 20 3d  ){..    iWrite =
4279b 20 69 56 61 6c 20 2d 20 2a 70 69 50 72 65 76 3b   iVal - *piPrev;
4279c 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
4279d 69 57 72 69 74 65 20 3d 20 2a 70 69 50 72 65 76  iWrite = *piPrev
4279e 20 2d 20 69 56 61 6c 3b 0d 0a 20 20 7d 0d 0a 20   - iVal;..  }.. 
4279f 20 61 73 73 65 72 74 28 20 2a 70 62 46 69 72 73   assert( *pbFirs
427a0 74 20 7c 7c 20 2a 70 69 50 72 65 76 3d 3d 30 20  t || *piPrev==0 
427a1 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 2a 70  );..  assert( *p
427a2 62 46 69 72 73 74 3d 3d 30 20 7c 7c 20 69 57 72  bFirst==0 || iWr
427a3 69 74 65 3e 30 20 29 3b 0d 0a 20 20 2a 70 70 20  ite>0 );..  *pp 
427a4 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  += sqlite3Fts3Pu
427a5 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 69 57 72  tVarint(*pp, iWr
427a6 69 74 65 29 3b 0d 0a 20 20 2a 70 69 50 72 65 76  ite);..  *piPrev
427a7 20 3d 20 69 56 61 6c 3b 0d 0a 20 20 2a 70 62 46   = iVal;..  *pbF
427a8 69 72 73 74 20 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a  irst = 1;..}....
427a9 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 6d 61  ../*..** This ma
427aa 63 72 6f 20 69 73 20 75 73 65 64 20 62 79 20 76  cro is used by v
427ab 61 72 69 6f 75 73 20 66 75 6e 63 74 69 6f 6e 73  arious functions
427ac 20 74 68 61 74 20 6d 65 72 67 65 20 64 6f 63 6c   that merge docl
427ad 69 73 74 73 2e 20 54 68 65 20 74 77 6f 0d 0a 2a  ists. The two..*
427ae 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  * arguments are 
427af 36 34 2d 62 69 74 20 64 6f 63 69 64 20 76 61 6c  64-bit docid val
427b0 75 65 73 2e 20 49 66 20 74 68 65 20 76 61 6c 75  ues. If the valu
427b1 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 76  e of the stack v
427b2 61 72 69 61 62 6c 65 0d 0a 2a 2a 20 62 44 65 73  ariable..** bDes
427b3 63 44 6f 63 6c 69 73 74 20 69 73 20 30 20 77 68  cDoclist is 0 wh
427b4 65 6e 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73  en this macro is
427b5 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 6e 20 69   invoked, then i
427b6 74 20 72 65 74 75 72 6e 73 20 28 69 31 2d 69 32  t returns (i1-i2
427b7 29 2e 20 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ). ..** Otherwis
427b8 65 2c 20 28 69 32 2d 69 31 29 2e 0d 0a 2a 2a 0d  e, (i2-i1)...**.
427b9 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 6d  .** Using this m
427ba 61 6b 65 73 20 69 74 20 65 61 73 69 65 72 20 74  akes it easier t
427bb 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74 68 61  o write code tha
427bc 74 20 63 61 6e 20 6d 65 72 67 65 20 64 6f 63 6c  t can merge docl
427bd 69 73 74 73 20 74 68 61 74 20 61 72 65 0d 0a 2a  ists that are..*
427be 2a 20 73 6f 72 74 65 64 20 69 6e 20 65 69 74 68  * sorted in eith
427bf 65 72 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 20  er ascending or 
427c0 64 65 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72  descending order
427c1 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 44  ...*/..#define D
427c2 4f 43 49 44 5f 43 4d 50 28 69 31 2c 20 69 32 29  OCID_CMP(i1, i2)
427c3 20 28 28 62 44 65 73 63 44 6f 63 6c 69 73 74 3f   ((bDescDoclist?
427c4 2d 31 3a 31 29 20 2a 20 28 69 31 2d 69 32 29 29  -1:1) * (i1-i2))
427c5 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
427c6 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 61 6e  function does an
427c7 20 22 4f 52 22 20 6d 65 72 67 65 20 6f 66 20 74   "OR" merge of t
427c8 77 6f 20 64 6f 63 6c 69 73 74 73 20 28 6f 75 74  wo doclists (out
427c9 70 75 74 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  put contains all
427ca 0d 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 73 20 63  ..** positions c
427cb 6f 6e 74 61 69 6e 65 64 20 69 6e 20 65 69 74 68  ontained in eith
427cc 65 72 20 61 72 67 75 6d 65 6e 74 20 64 6f 63 6c  er argument docl
427cd 69 73 74 29 2e 20 49 66 20 74 68 65 20 64 6f 63  ist). If the doc
427ce 69 64 73 20 69 6e 20 74 68 65 20 0d 0a 2a 2a 20  ids in the ..** 
427cf 69 6e 70 75 74 20 64 6f 63 6c 69 73 74 73 20 61  input doclists a
427d0 72 65 20 73 6f 72 74 65 64 20 69 6e 20 61 73 63  re sorted in asc
427d1 65 6e 64 69 6e 67 20 6f 72 64 65 72 2c 20 70 61  ending order, pa
427d2 72 61 6d 65 74 65 72 20 62 44 65 73 63 44 6f 63  rameter bDescDoc
427d3 6c 69 73 74 0d 0a 2a 2a 20 73 68 6f 75 6c 64 20  list..** should 
427d4 62 65 20 66 61 6c 73 65 2e 20 49 66 20 74 68 65  be false. If the
427d5 79 20 61 72 65 20 73 6f 72 74 65 64 20 69 6e 20  y are sorted in 
427d6 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2c  ascending order,
427d7 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0d 0a 2a   it should be..*
427d8 2a 20 70 61 73 73 65 64 20 61 20 6e 6f 6e 2d 7a  * passed a non-z
427d9 65 72 6f 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a  ero value...**..
427da 2a 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ** If no error o
427db 63 63 75 72 73 2c 20 2a 70 61 4f 75 74 20 69 73  ccurs, *paOut is
427dc 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74   set to point at
427dd 20 61 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   an sqlite3_mall
427de 6f 63 27 64 20 62 75 66 66 65 72 0d 0a 2a 2a 20  oc'd buffer..** 
427df 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f  containing the o
427e0 75 74 70 75 74 20 64 6f 63 6c 69 73 74 20 61 6e  utput doclist an
427e1 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  d SQLITE_OK is r
427e2 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
427e3 20 63 61 73 65 0d 0a 2a 2a 20 2a 70 6e 4f 75 74   case..** *pnOut
427e4 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
427e5 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
427e6 6e 20 74 68 65 20 6f 75 74 70 75 74 20 64 6f 63  n the output doc
427e7 6c 69 73 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  list...**..** If
427e8 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
427e9 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  , an SQLite erro
427ea 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
427eb 65 64 2e 20 54 68 65 20 6f 75 74 70 75 74 20 76  ed. The output v
427ec 61 6c 75 65 73 0d 0a 2a 2a 20 61 72 65 20 75 6e  alues..** are un
427ed 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20  defined in this 
427ee 63 61 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  case...*/..stati
427ef 63 20 69 6e 74 20 66 74 73 33 44 6f 63 6c 69 73  c int fts3Doclis
427f0 74 4f 72 4d 65 72 67 65 28 0d 0a 20 20 69 6e 74  tOrMerge(..  int
427f1 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20 20   bDescDoclist,  
427f2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
427f3 54 72 75 65 20 69 66 20 61 72 67 75 6d 65 6e 74  True if argument
427f4 73 20 61 72 65 20 64 65 73 63 20 2a 2f 0d 0a 20  s are desc */.. 
427f5 20 63 68 61 72 20 2a 61 31 2c 20 69 6e 74 20 6e   char *a1, int n
427f6 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
427f7 20 2f 2a 20 46 69 72 73 74 20 64 6f 63 6c 69 73   /* First doclis
427f8 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 61 32  t */..  char *a2
427f9 2c 20 69 6e 74 20 6e 32 2c 20 20 20 20 20 20 20  , int n2,       
427fa 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
427fb 64 20 64 6f 63 6c 69 73 74 20 2a 2f 0d 0a 20 20  d doclist */..  
427fc 63 68 61 72 20 2a 2a 70 61 4f 75 74 2c 20 69 6e  char **paOut, in
427fd 74 20 2a 70 6e 4f 75 74 20 20 20 20 20 20 20 20  t *pnOut        
427fe 2f 2a 20 4f 55 54 3a 20 4d 61 6c 6c 6f 63 27 64  /* OUT: Malloc'd
427ff 20 64 6f 63 6c 69 73 74 20 2a 2f 0d 0a 29 7b 0d   doclist */..){.
42800 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
42801 20 69 31 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69   i1 = 0;..  sqli
42802 74 65 33 5f 69 6e 74 36 34 20 69 32 20 3d 20 30  te3_int64 i2 = 0
42803 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ;..  sqlite3_int
42804 36 34 20 69 50 72 65 76 20 3d 20 30 3b 0d 0a 20  64 iPrev = 0;.. 
42805 20 63 68 61 72 20 2a 70 45 6e 64 31 20 3d 20 26   char *pEnd1 = &
42806 61 31 5b 6e 31 5d 3b 0d 0a 20 20 63 68 61 72 20  a1[n1];..  char 
42807 2a 70 45 6e 64 32 20 3d 20 26 61 32 5b 6e 32 5d  *pEnd2 = &a2[n2]
42808 3b 0d 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20  ;..  char *p1 = 
42809 61 31 3b 0d 0a 20 20 63 68 61 72 20 2a 70 32 20  a1;..  char *p2 
4280a 3d 20 61 32 3b 0d 0a 20 20 63 68 61 72 20 2a 70  = a2;..  char *p
4280b 3b 0d 0a 20 20 63 68 61 72 20 2a 61 4f 75 74 3b  ;..  char *aOut;
4280c 0d 0a 20 20 69 6e 74 20 62 46 69 72 73 74 4f 75  ..  int bFirstOu
4280d 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2a 70 61 4f  t = 0;....  *paO
4280e 75 74 20 3d 20 30 3b 0d 0a 20 20 2a 70 6e 4f 75  ut = 0;..  *pnOu
4280f 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 41  t = 0;....  /* A
42810 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
42811 72 20 74 68 65 20 6f 75 74 70 75 74 2e 20 42 6f  r the output. Bo
42812 74 68 20 74 68 65 20 69 6e 70 75 74 20 61 6e 64  th the input and
42813 20 6f 75 74 70 75 74 20 64 6f 63 6c 69 73 74 73   output doclists
42814 0d 0a 20 20 2a 2a 20 61 72 65 20 64 65 6c 74 61  ..  ** are delta
42815 20 65 6e 63 6f 64 65 64 2e 20 49 66 20 74 68 65   encoded. If the
42816 79 20 61 72 65 20 69 6e 20 61 73 63 65 6e 64 69  y are in ascendi
42817 6e 67 20 6f 72 64 65 72 20 28 62 44 65 73 63 44  ng order (bDescD
42818 6f 63 6c 69 73 74 3d 3d 30 29 2c 0d 0a 20 20 2a  oclist==0),..  *
42819 2a 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  * then the first
4281a 20 64 6f 63 69 64 20 69 6e 20 65 61 63 68 20 6c   docid in each l
4281b 69 73 74 20 69 73 20 73 69 6d 70 6c 79 20 65 6e  ist is simply en
4281c 63 6f 64 65 64 20 61 73 20 61 20 76 61 72 69 6e  coded as a varin
4281d 74 2e 20 46 6f 72 0d 0a 20 20 2a 2a 20 65 61 63  t. For..  ** eac
4281e 68 20 73 75 62 73 65 71 75 65 6e 74 20 64 6f 63  h subsequent doc
4281f 69 64 2c 20 74 68 65 20 76 61 72 69 6e 74 20 73  id, the varint s
42820 74 6f 72 65 64 20 69 73 20 74 68 65 20 64 69 66  tored is the dif
42821 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
42822 74 68 65 0d 0a 20 20 2a 2a 20 63 75 72 72 65 6e  the..  ** curren
42823 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 64  t and previous d
42824 6f 63 69 64 20 28 61 20 70 6f 73 69 74 69 76 65  ocid (a positive
42825 20 6e 75 6d 62 65 72 20 2d 20 73 69 6e 63 65 20   number - since 
42826 74 68 65 20 6c 69 73 74 20 69 73 20 69 6e 0d 0a  the list is in..
42827 20 20 2a 2a 20 61 73 63 65 6e 64 69 6e 67 20 6f    ** ascending o
42828 72 64 65 72 29 2e 0d 0a 20 20 2a 2a 0d 0a 20 20  rder)...  **..  
42829 2a 2a 20 54 68 65 20 66 69 72 73 74 20 64 6f 63  ** The first doc
4282a 69 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  id written to th
4282b 65 20 6f 75 74 70 75 74 20 69 73 20 74 68 65 72  e output is ther
4282c 65 66 6f 72 65 20 65 6e 63 6f 64 65 64 20 75 73  efore encoded us
4282d 69 6e 67 20 74 68 65 20 0d 0a 20 20 2a 2a 20 73  ing the ..  ** s
4282e 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  ame number of by
4282f 74 65 73 20 61 73 20 69 74 20 69 73 20 69 6e 20  tes as it is in 
42830 77 68 69 63 68 65 76 65 72 20 6f 66 20 74 68 65  whichever of the
42831 20 69 6e 70 75 74 20 6c 69 73 74 73 20 69 74 20   input lists it 
42832 69 73 0d 0a 20 20 2a 2a 20 72 65 61 64 20 66 72  is..  ** read fr
42833 6f 6d 2e 20 41 6e 64 20 65 61 63 68 20 73 75 62  om. And each sub
42834 73 65 71 75 65 6e 74 20 64 6f 63 69 64 20 72 65  sequent docid re
42835 61 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ad from the same
42836 20 69 6e 70 75 74 20 6c 69 73 74 20 0d 0a 20 20   input list ..  
42837 2a 2a 20 63 6f 6e 73 75 6d 65 73 20 65 69 74 68  ** consumes eith
42838 65 72 20 74 68 65 20 73 61 6d 65 20 6f 72 20 6c  er the same or l
42839 65 73 73 20 62 79 74 65 73 20 61 73 20 69 74 20  ess bytes as it 
4283a 64 69 64 20 69 6e 20 74 68 65 20 69 6e 70 75 74  did in the input
4283b 20 28 73 69 6e 63 65 0d 0a 20 20 2a 2a 20 74 68   (since..  ** th
4283c 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
4283d 77 65 65 6e 20 69 74 20 61 6e 64 20 74 68 65 20  ween it and the 
4283e 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69  previous value i
4283f 6e 20 74 68 65 20 6f 75 74 70 75 74 20 6d 75 73  n the output mus
42840 74 0d 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f 73  t..  ** be a pos
42841 69 74 69 76 65 20 76 61 6c 75 65 20 6c 65 73 73  itive value less
42842 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
42843 6f 20 74 68 65 20 64 65 6c 74 61 20 76 61 6c 75  o the delta valu
42844 65 20 72 65 61 64 20 66 72 6f 6d 20 0d 0a 20 20  e read from ..  
42845 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73  ** the input lis
42846 74 29 2e 20 54 68 65 20 73 61 6d 65 20 61 72 67  t). The same arg
42847 75 6d 65 6e 74 20 61 70 70 6c 69 65 73 20 74 6f  ument applies to
42848 20 61 6c 6c 20 62 75 74 20 74 68 65 20 66 69 72   all but the fir
42849 73 74 20 64 6f 63 69 64 0d 0a 20 20 2a 2a 20 72  st docid..  ** r
4284a 65 61 64 20 66 72 6f 6d 20 74 68 65 20 27 6f 74  ead from the 'ot
4284b 68 65 72 27 20 6c 69 73 74 2e 20 41 6e 64 20 74  her' list. And t
4284c 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  o the contents o
4284d 66 20 61 6c 6c 20 70 6f 73 69 74 69 6f 6e 20 6c  f all position l
4284e 69 73 74 73 0d 0a 20 20 2a 2a 20 74 68 61 74 20  ists..  ** that 
4284f 77 69 6c 6c 20 62 65 20 63 6f 70 69 65 64 20 61  will be copied a
42850 6e 64 20 6d 65 72 67 65 64 20 66 72 6f 6d 20 74  nd merged from t
42851 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20  he input to the 
42852 6f 75 74 70 75 74 2e 0d 0a 20 20 2a 2a 0d 0a 20  output...  **.. 
42853 20 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20   ** However, if 
42854 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20  the first docid 
42855 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 6f 75  copied to the ou
42856 74 70 75 74 20 69 73 20 61 20 6e 65 67 61 74 69  tput is a negati
42857 76 65 20 6e 75 6d 62 65 72 2c 0d 0a 20 20 2a 2a  ve number,..  **
42858 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f 64 69   then the encodi
42859 6e 67 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ng of the first 
4285a 64 6f 63 69 64 20 66 72 6f 6d 20 74 68 65 20 27  docid from the '
4285b 6f 74 68 65 72 27 20 69 6e 70 75 74 20 6c 69 73  other' input lis
4285c 74 20 6d 61 79 0d 0a 20 20 2a 2a 20 62 65 20 6c  t may..  ** be l
4285d 61 72 67 65 72 20 69 6e 20 74 68 65 20 6f 75 74  arger in the out
4285e 70 75 74 20 74 68 61 6e 20 69 74 20 77 61 73 20  put than it was 
4285f 69 6e 20 74 68 65 20 69 6e 70 75 74 20 28 73 69  in the input (si
42860 6e 63 65 20 74 68 65 20 64 65 6c 74 61 20 76 61  nce the delta va
42861 6c 75 65 0d 0a 20 20 2a 2a 20 6d 61 79 20 62 65  lue..  ** may be
42862 20 61 20 6c 61 72 67 65 72 20 70 6f 73 69 74 69   a larger positi
42863 76 65 20 69 6e 74 65 67 65 72 20 74 68 61 6e 20  ve integer than 
42864 74 68 65 20 61 63 74 75 61 6c 20 64 6f 63 69 64  the actual docid
42865 29 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54  )...  **..  ** T
42866 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65  he space require
42867 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6f  d to store the o
42868 75 74 70 75 74 20 69 73 20 74 68 65 72 65 66 6f  utput is therefo
42869 72 65 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68  re the sum of th
4286a 65 0d 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6f 66  e..  ** sizes of
4286b 20 74 68 65 20 74 77 6f 20 69 6e 70 75 74 73 2c   the two inputs,
4286c 20 70 6c 75 73 20 65 6e 6f 75 67 68 20 73 70 61   plus enough spa
4286d 63 65 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f  ce for exactly o
4286e 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 0d  ne of the input.
4286f 0a 20 20 2a 2a 20 64 6f 63 69 64 73 20 74 6f 20  .  ** docids to 
42870 67 72 6f 77 2e 20 0d 0a 20 20 2a 2a 0d 0a 20 20  grow. ..  **..  
42871 2a 2a 20 41 20 73 79 6d 65 74 72 69 63 20 61 72  ** A symetric ar
42872 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 6d 61  gument may be ma
42873 64 65 20 69 66 20 74 68 65 20 64 6f 63 6c 69 73  de if the doclis
42874 74 73 20 61 72 65 20 69 6e 20 64 65 73 63 65 6e  ts are in descen
42875 64 69 6e 67 20 0d 0a 20 20 2a 2a 20 6f 72 64 65  ding ..  ** orde
42876 72 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 4f 75 74  r...  */..  aOut
42877 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
42878 63 28 6e 31 2b 6e 32 2b 46 54 53 33 5f 56 41 52  c(n1+n2+FTS3_VAR
42879 49 4e 54 5f 4d 41 58 2d 31 29 3b 0d 0a 20 20 69  INT_MAX-1);..  i
4287a 66 28 20 21 61 4f 75 74 20 29 20 72 65 74 75 72  f( !aOut ) retur
4287b 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
4287c 0a 0d 0a 20 20 70 20 3d 20 61 4f 75 74 3b 0d 0a  ...  p = aOut;..
4287d 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
4287e 72 69 6e 74 33 28 26 70 31 2c 20 70 45 6e 64 31  rint3(&p1, pEnd1
4287f 2c 20 30 2c 20 26 69 31 29 3b 0d 0a 20 20 66 74  , 0, &i1);..  ft
42880 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
42881 33 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 30 2c  3(&p2, pEnd2, 0,
42882 20 26 69 32 29 3b 0d 0a 20 20 77 68 69 6c 65 28   &i2);..  while(
42883 20 70 31 20 7c 7c 20 70 32 20 29 7b 0d 0a 20 20   p1 || p2 ){..  
42884 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
42885 69 44 69 66 66 20 3d 20 44 4f 43 49 44 5f 43 4d  iDiff = DOCID_CM
42886 50 28 69 31 2c 20 69 32 29 3b 0d 0a 0d 0a 20 20  P(i1, i2);....  
42887 20 20 69 66 28 20 70 32 20 26 26 20 70 31 20 26    if( p2 && p1 &
42888 26 20 69 44 69 66 66 3d 3d 30 20 29 7b 0d 0a 20  & iDiff==0 ){.. 
42889 20 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74       fts3PutDelt
4288a 61 56 61 72 69 6e 74 33 28 26 70 2c 20 62 44 65  aVarint3(&p, bDe
4288b 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 50 72 65  scDoclist, &iPre
4288c 76 2c 20 26 62 46 69 72 73 74 4f 75 74 2c 20 69  v, &bFirstOut, i
4288d 31 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 50  1);..      fts3P
4288e 6f 73 6c 69 73 74 4d 65 72 67 65 28 26 70 2c 20  oslistMerge(&p, 
4288f 26 70 31 2c 20 26 70 32 29 3b 0d 0a 20 20 20 20  &p1, &p2);..    
42890 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
42891 72 69 6e 74 33 28 26 70 31 2c 20 70 45 6e 64 31  rint3(&p1, pEnd1
42892 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20  , bDescDoclist, 
42893 26 69 31 29 3b 0d 0a 20 20 20 20 20 20 66 74 73  &i1);..      fts
42894 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 33  3GetDeltaVarint3
42895 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 62 44 65  (&p2, pEnd2, bDe
42896 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 32 29 3b  scDoclist, &i2);
42897 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
42898 21 70 32 20 7c 7c 20 28 70 31 20 26 26 20 69 44  !p2 || (p1 && iD
42899 69 66 66 3c 30 29 20 29 7b 0d 0a 20 20 20 20 20  iff<0) ){..     
4289a 20 66 74 73 33 50 75 74 44 65 6c 74 61 56 61 72   fts3PutDeltaVar
4289b 69 6e 74 33 28 26 70 2c 20 62 44 65 73 63 44 6f  int3(&p, bDescDo
4289c 63 6c 69 73 74 2c 20 26 69 50 72 65 76 2c 20 26  clist, &iPrev, &
4289d 62 46 69 72 73 74 4f 75 74 2c 20 69 31 29 3b 0d  bFirstOut, i1);.
4289e 0a 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69  .      fts3Posli
4289f 73 74 43 6f 70 79 28 26 70 2c 20 26 70 31 29 3b  stCopy(&p, &p1);
428a0 0d 0a 20 20 20 20 20 20 66 74 73 33 47 65 74 44  ..      fts3GetD
428a1 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70 31 2c  eltaVarint3(&p1,
428a2 20 70 45 6e 64 31 2c 20 62 44 65 73 63 44 6f 63   pEnd1, bDescDoc
428a3 6c 69 73 74 2c 20 26 69 31 29 3b 0d 0a 20 20 20  list, &i1);..   
428a4 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 66   }else{..      f
428a5 74 73 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e  ts3PutDeltaVarin
428a6 74 33 28 26 70 2c 20 62 44 65 73 63 44 6f 63 6c  t3(&p, bDescDocl
428a7 69 73 74 2c 20 26 69 50 72 65 76 2c 20 26 62 46  ist, &iPrev, &bF
428a8 69 72 73 74 4f 75 74 2c 20 69 32 29 3b 0d 0a 20  irstOut, i2);.. 
428a9 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74       fts3Poslist
428aa 43 6f 70 79 28 26 70 2c 20 26 70 32 29 3b 0d 0a  Copy(&p, &p2);..
428ab 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c        fts3GetDel
428ac 74 61 56 61 72 69 6e 74 33 28 26 70 32 2c 20 70  taVarint3(&p2, p
428ad 45 6e 64 32 2c 20 62 44 65 73 63 44 6f 63 6c 69  End2, bDescDocli
428ae 73 74 2c 20 26 69 32 29 3b 0d 0a 20 20 20 20 7d  st, &i2);..    }
428af 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 61 4f 75  ..  }....  *paOu
428b0 74 20 3d 20 61 4f 75 74 3b 0d 0a 20 20 2a 70 6e  t = aOut;..  *pn
428b1 4f 75 74 20 3d 20 28 70 2d 61 4f 75 74 29 3b 0d  Out = (p-aOut);.
428b2 0a 20 20 61 73 73 65 72 74 28 20 2a 70 6e 4f 75  .  assert( *pnOu
428b3 74 3c 3d 6e 31 2b 6e 32 2b 46 54 53 33 5f 56 41  t<=n1+n2+FTS3_VA
428b4 52 49 4e 54 5f 4d 41 58 2d 31 20 29 3b 0d 0a 20  RINT_MAX-1 );.. 
428b5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
428b6 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
428b7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f  This function do
428b8 65 73 20 61 20 22 70 68 72 61 73 65 22 20 6d 65  es a "phrase" me
428b9 72 67 65 20 6f 66 20 74 77 6f 20 64 6f 63 6c 69  rge of two docli
428ba 73 74 73 2e 20 49 6e 20 61 20 70 68 72 61 73 65  sts. In a phrase
428bb 20 6d 65 72 67 65 2c 0d 0a 2a 2a 20 74 68 65 20   merge,..** the 
428bc 6f 75 74 70 75 74 20 63 6f 6e 74 61 69 6e 73 20  output contains 
428bd 61 20 63 6f 70 79 20 6f 66 20 65 61 63 68 20 70  a copy of each p
428be 6f 73 69 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  osition from the
428bf 20 72 69 67 68 74 2d 68 61 6e 64 20 69 6e 70 75   right-hand inpu
428c0 74 0d 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f  t..** doclist fo
428c1 72 20 77 68 69 63 68 20 74 68 65 72 65 20 69 73  r which there is
428c2 20 61 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74   a position in t
428c3 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 69 6e 70  he left-hand inp
428c4 75 74 20 64 6f 63 6c 69 73 74 0d 0a 2a 2a 20 65  ut doclist..** e
428c5 78 61 63 74 6c 79 20 6e 44 69 73 74 20 74 6f 6b  xactly nDist tok
428c6 65 6e 73 20 62 65 66 6f 72 65 20 69 74 2e 0d 0a  ens before it...
428c7 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f  **..** If the do
428c8 63 69 64 73 20 69 6e 20 74 68 65 20 69 6e 70 75  cids in the inpu
428c9 74 20 64 6f 63 6c 69 73 74 73 20 61 72 65 20 73  t doclists are s
428ca 6f 72 74 65 64 20 69 6e 20 61 73 63 65 6e 64 69  orted in ascendi
428cb 6e 67 20 6f 72 64 65 72 2c 0d 0a 2a 2a 20 70 61  ng order,..** pa
428cc 72 61 6d 65 74 65 72 20 62 44 65 73 63 44 6f 63  rameter bDescDoc
428cd 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 65 20 66  list should be f
428ce 61 6c 73 65 2e 20 49 66 20 74 68 65 79 20 61 72  alse. If they ar
428cf 65 20 73 6f 72 74 65 64 20 69 6e 20 61 73 63 65  e sorted in asce
428d0 6e 64 69 6e 67 20 0d 0a 2a 2a 20 6f 72 64 65 72  nding ..** order
428d1 2c 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 70  , it should be p
428d2 61 73 73 65 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f  assed a non-zero
428d3 20 76 61 6c 75 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   value...**..** 
428d4 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 69  The right-hand i
428d5 6e 70 75 74 20 64 6f 63 6c 69 73 74 20 69 73 20  nput doclist is 
428d6 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74  overwritten by t
428d7 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a  his function...*
428d8 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  /..static void f
428d9 74 73 33 44 6f 63 6c 69 73 74 50 68 72 61 73 65  ts3DoclistPhrase
428da 4d 65 72 67 65 28 0d 0a 20 20 69 6e 74 20 62 44  Merge(..  int bD
428db 65 73 63 44 6f 63 6c 69 73 74 2c 20 20 20 20 20  escDoclist,     
428dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
428dd 65 20 69 66 20 61 72 67 75 6d 65 6e 74 73 20 61  e if arguments a
428de 72 65 20 64 65 73 63 20 2a 2f 0d 0a 20 20 69 6e  re desc */..  in
428df 74 20 6e 44 69 73 74 2c 20 20 20 20 20 20 20 20  t nDist,        
428e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
428e1 20 44 69 73 74 61 6e 63 65 20 66 72 6f 6d 20 6c   Distance from l
428e2 65 66 74 20 74 6f 20 72 69 67 68 74 20 28 31 3d  eft to right (1=
428e3 61 64 6a 61 63 65 6e 74 29 20 2a 2f 0d 0a 20 20  adjacent) */..  
428e4 63 68 61 72 20 2a 61 4c 65 66 74 2c 20 69 6e 74  char *aLeft, int
428e5 20 6e 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20   nLeft,         
428e6 2f 2a 20 4c 65 66 74 20 64 6f 63 6c 69 73 74 20  /* Left doclist 
428e7 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 61 52 69 67  */..  char *aRig
428e8 68 74 2c 20 69 6e 74 20 2a 70 6e 52 69 67 68 74  ht, int *pnRight
428e9 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
428ea 20 52 69 67 68 74 2f 6f 75 74 70 75 74 20 64 6f   Right/output do
428eb 63 6c 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  clist */..){..  
428ec 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 31  sqlite3_int64 i1
428ed 20 3d 20 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33   = 0;..  sqlite3
428ee 5f 69 6e 74 36 34 20 69 32 20 3d 20 30 3b 0d 0a  _int64 i2 = 0;..
428ef 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
428f0 69 50 72 65 76 20 3d 20 30 3b 0d 0a 20 20 63 68  iPrev = 0;..  ch
428f1 61 72 20 2a 70 45 6e 64 31 20 3d 20 26 61 4c 65  ar *pEnd1 = &aLe
428f2 66 74 5b 6e 4c 65 66 74 5d 3b 0d 0a 20 20 63 68  ft[nLeft];..  ch
428f3 61 72 20 2a 70 45 6e 64 32 20 3d 20 26 61 52 69  ar *pEnd2 = &aRi
428f4 67 68 74 5b 2a 70 6e 52 69 67 68 74 5d 3b 0d 0a  ght[*pnRight];..
428f5 20 20 63 68 61 72 20 2a 70 31 20 3d 20 61 4c 65    char *p1 = aLe
428f6 66 74 3b 0d 0a 20 20 63 68 61 72 20 2a 70 32 20  ft;..  char *p2 
428f7 3d 20 61 52 69 67 68 74 3b 0d 0a 20 20 63 68 61  = aRight;..  cha
428f8 72 20 2a 70 3b 0d 0a 20 20 69 6e 74 20 62 46 69  r *p;..  int bFi
428f9 72 73 74 4f 75 74 20 3d 20 30 3b 0d 0a 20 20 63  rstOut = 0;..  c
428fa 68 61 72 20 2a 61 4f 75 74 20 3d 20 61 52 69 67  har *aOut = aRig
428fb 68 74 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  ht;....  assert(
428fc 20 6e 44 69 73 74 3e 30 20 29 3b 0d 0a 0d 0a 20   nDist>0 );.... 
428fd 20 70 20 3d 20 61 4f 75 74 3b 0d 0a 20 20 66 74   p = aOut;..  ft
428fe 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
428ff 33 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 30 2c  3(&p1, pEnd1, 0,
42900 20 26 69 31 29 3b 0d 0a 20 20 66 74 73 33 47 65   &i1);..  fts3Ge
42901 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70  tDeltaVarint3(&p
42902 32 2c 20 70 45 6e 64 32 2c 20 30 2c 20 26 69 32  2, pEnd2, 0, &i2
42903 29 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65 28 20 70  );....  while( p
42904 31 20 26 26 20 70 32 20 29 7b 0d 0a 20 20 20 20  1 && p2 ){..    
42905 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
42906 69 66 66 20 3d 20 44 4f 43 49 44 5f 43 4d 50 28  iff = DOCID_CMP(
42907 69 31 2c 20 69 32 29 3b 0d 0a 20 20 20 20 69 66  i1, i2);..    if
42908 28 20 69 44 69 66 66 3d 3d 30 20 29 7b 0d 0a 20  ( iDiff==0 ){.. 
42909 20 20 20 20 20 63 68 61 72 20 2a 70 53 61 76 65       char *pSave
4290a 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 73 71 6c   = p;..      sql
4290b 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65 76  ite3_int64 iPrev
4290c 53 61 76 65 20 3d 20 69 50 72 65 76 3b 0d 0a 20  Save = iPrev;.. 
4290d 20 20 20 20 20 69 6e 74 20 62 46 69 72 73 74 4f       int bFirstO
4290e 75 74 53 61 76 65 20 3d 20 62 46 69 72 73 74 4f  utSave = bFirstO
4290f 75 74 3b 0d 0a 0d 0a 20 20 20 20 20 20 66 74 73  ut;....      fts
42910 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 33  3PutDeltaVarint3
42911 28 26 70 2c 20 62 44 65 73 63 44 6f 63 6c 69 73  (&p, bDescDoclis
42912 74 2c 20 26 69 50 72 65 76 2c 20 26 62 46 69 72  t, &iPrev, &bFir
42913 73 74 4f 75 74 2c 20 69 31 29 3b 0d 0a 20 20 20  stOut, i1);..   
42914 20 20 20 69 66 28 20 30 3d 3d 66 74 73 33 50 6f     if( 0==fts3Po
42915 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  slistPhraseMerge
42916 28 26 70 2c 20 6e 44 69 73 74 2c 20 30 2c 20 31  (&p, nDist, 0, 1
42917 2c 20 26 70 31 2c 20 26 70 32 29 20 29 7b 0d 0a  , &p1, &p2) ){..
42918 20 20 20 20 20 20 20 20 70 20 3d 20 70 53 61 76          p = pSav
42919 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 50 72 65  e;..        iPre
4291a 76 20 3d 20 69 50 72 65 76 53 61 76 65 3b 0d 0a  v = iPrevSave;..
4291b 20 20 20 20 20 20 20 20 62 46 69 72 73 74 4f 75          bFirstOu
4291c 74 20 3d 20 62 46 69 72 73 74 4f 75 74 53 61 76  t = bFirstOutSav
4291d 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  e;..      }..   
4291e 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
4291f 61 72 69 6e 74 33 28 26 70 31 2c 20 70 45 6e 64  arint3(&p1, pEnd
42920 31 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c  1, bDescDoclist,
42921 20 26 69 31 29 3b 0d 0a 20 20 20 20 20 20 66 74   &i1);..      ft
42922 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
42923 33 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 62 44  3(&p2, pEnd2, bD
42924 65 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 32 29  escDoclist, &i2)
42925 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ;..    }else if(
42926 20 69 44 69 66 66 3c 30 20 29 7b 0d 0a 20 20 20   iDiff<0 ){..   
42927 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
42928 70 79 28 30 2c 20 26 70 31 29 3b 0d 0a 20 20 20  py(0, &p1);..   
42929 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
4292a 61 72 69 6e 74 33 28 26 70 31 2c 20 70 45 6e 64  arint3(&p1, pEnd
4292b 31 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c  1, bDescDoclist,
4292c 20 26 69 31 29 3b 0d 0a 20 20 20 20 7d 65 6c 73   &i1);..    }els
4292d 65 7b 0d 0a 20 20 20 20 20 20 66 74 73 33 50 6f  e{..      fts3Po
4292e 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32  slistCopy(0, &p2
4292f 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 47 65  );..      fts3Ge
42930 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70  tDeltaVarint3(&p
42931 32 2c 20 70 45 6e 64 32 2c 20 62 44 65 73 63 44  2, pEnd2, bDescD
42932 6f 63 6c 69 73 74 2c 20 26 69 32 29 3b 0d 0a 20  oclist, &i2);.. 
42933 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a     }..  }....  *
42934 70 6e 52 69 67 68 74 20 3d 20 70 20 2d 20 61 4f  pnRight = p - aO
42935 75 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ut;..}..../*..**
42936 20 41 72 67 75 6d 65 6e 74 20 70 4c 69 73 74 20   Argument pList 
42937 70 6f 69 6e 74 73 20 74 6f 20 61 20 70 6f 73 69  points to a posi
42938 74 69 6f 6e 20 6c 69 73 74 20 6e 4c 69 73 74 20  tion list nList 
42939 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 54  bytes in size. T
4293a 68 69 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his..** function
4293b 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69   checks to see i
4293c 66 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c  f the position l
4293d 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79  ist contains any
4293e 20 65 6e 74 72 69 65 73 20 66 6f 72 0d 0a 2a 2a   entries for..**
4293f 20 61 20 74 6f 6b 65 6e 20 69 6e 20 70 6f 73 69   a token in posi
42940 74 69 6f 6e 20 30 20 28 6f 66 20 61 6e 79 20 63  tion 0 (of any c
42941 6f 6c 75 6d 6e 29 2e 20 49 66 20 73 6f 2c 20 69  olumn). If so, i
42942 74 20 77 72 69 74 65 73 20 61 72 67 75 6d 65 6e  t writes argumen
42943 74 20 69 44 65 6c 74 61 0d 0a 2a 2a 20 74 6f 20  t iDelta..** to 
42944 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
42945 72 20 70 4f 75 74 2c 20 66 6f 6c 6c 6f 77 65 64  r pOut, followed
42946 20 62 79 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c   by a position l
42947 69 73 74 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f  ist consisting o
42948 6e 6c 79 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 65  nly..** of the e
42949 6e 74 72 69 65 73 20 66 72 6f 6d 20 70 4c 69 73  ntries from pLis
4294a 74 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 30 2c  t at position 0,
4294b 20 61 6e 64 20 74 65 72 6d 69 6e 61 74 65 64 20   and terminated 
4294c 62 79 20 61 6e 20 30 78 30 30 20 62 79 74 65 2e  by an 0x00 byte.
4294d 0d 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  ..** The value r
4294e 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e  eturned is the n
4294f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77  umber of bytes w
42950 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 20 28  ritten to pOut (
42951 69 66 20 61 6e 79 29 2e 0d 0a 2a 2f 0d 0a 53 51  if any)...*/..SQ
42952 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
42953 20 73 71 6c 69 74 65 33 46 74 73 33 46 69 72 73   sqlite3Fts3Firs
42954 74 46 69 6c 74 65 72 28 0d 0a 20 20 73 71 6c 69  tFilter(..  sqli
42955 74 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74 61  te3_int64 iDelta
42956 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  ,           /* V
42957 61 72 69 6e 74 20 74 68 61 74 20 6d 61 79 20 62  arint that may b
42958 65 20 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75  e written to pOu
42959 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70 4c  t */..  char *pL
4295a 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ist,            
4295b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 69 74          /* Posit
4295c 69 6f 6e 20 6c 69 73 74 20 28 6e 6f 20 30 78 30  ion list (no 0x0
4295d 30 20 74 65 72 6d 29 20 2a 2f 0d 0a 20 20 69 6e  0 term) */..  in
4295e 74 20 6e 4c 69 73 74 2c 20 20 20 20 20 20 20 20  t nList,        
4295f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42960 20 53 69 7a 65 20 6f 66 20 70 4c 69 73 74 20 69   Size of pList i
42961 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20 63 68  n bytes */..  ch
42962 61 72 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20  ar *pOut        
42963 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42964 20 57 72 69 74 65 20 6f 75 74 70 75 74 20 68 65   Write output he
42965 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  re */..){..  int
42966 20 6e 4f 75 74 20 3d 20 30 3b 0d 0a 20 20 69 6e   nOut = 0;..  in
42967 74 20 62 57 72 69 74 74 65 6e 20 3d 20 30 3b 20  t bWritten = 0; 
42968 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42969 20 54 72 75 65 20 6f 6e 63 65 20 69 44 65 6c 74   True once iDelt
4296a 61 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74  a has been writt
4296b 65 6e 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70  en */..  char *p
4296c 20 3d 20 70 4c 69 73 74 3b 0d 0a 20 20 63 68 61   = pList;..  cha
4296d 72 20 2a 70 45 6e 64 20 3d 20 26 70 4c 69 73 74  r *pEnd = &pList
4296e 5b 6e 4c 69 73 74 5d 3b 0d 0a 0d 0a 20 20 69 66  [nList];....  if
4296f 28 20 2a 70 21 3d 30 78 30 31 20 29 7b 0d 0a 20  ( *p!=0x01 ){.. 
42970 20 20 20 69 66 28 20 2a 70 3d 3d 30 78 30 32 20     if( *p==0x02 
42971 29 7b 0d 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b  ){..      nOut +
42972 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
42973 56 61 72 69 6e 74 28 26 70 4f 75 74 5b 6e 4f 75  Varint(&pOut[nOu
42974 74 5d 2c 20 69 44 65 6c 74 61 29 3b 0d 0a 20 20  t], iDelta);..  
42975 20 20 20 20 70 4f 75 74 5b 6e 4f 75 74 2b 2b 5d      pOut[nOut++]
42976 20 3d 20 30 78 30 32 3b 0d 0a 20 20 20 20 20 20   = 0x02;..      
42977 62 57 72 69 74 74 65 6e 20 3d 20 31 3b 0d 0a 20  bWritten = 1;.. 
42978 20 20 20 7d 0d 0a 20 20 20 20 66 74 73 33 43 6f     }..    fts3Co
42979 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20  lumnlistCopy(0, 
4297a 26 70 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 77  &p);..  }....  w
4297b 68 69 6c 65 28 20 70 3c 70 45 6e 64 20 26 26 20  hile( p<pEnd && 
4297c 2a 70 3d 3d 30 78 30 31 20 29 7b 0d 0a 20 20 20  *p==0x01 ){..   
4297d 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
4297e 43 6f 6c 3b 0d 0a 20 20 20 20 70 2b 2b 3b 0d 0a  Col;..    p++;..
4297f 20 20 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33      p += sqlite3
42980 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 2c  Fts3GetVarint(p,
42981 20 26 69 43 6f 6c 29 3b 0d 0a 20 20 20 20 69 66   &iCol);..    if
42982 28 20 2a 70 3d 3d 30 78 30 32 20 29 7b 0d 0a 20  ( *p==0x02 ){.. 
42983 20 20 20 20 20 69 66 28 20 62 57 72 69 74 74 65       if( bWritte
42984 6e 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  n==0 ){..       
42985 20 6e 4f 75 74 20 2b 3d 20 73 71 6c 69 74 65 33   nOut += sqlite3
42986 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70  Fts3PutVarint(&p
42987 4f 75 74 5b 6e 4f 75 74 5d 2c 20 69 44 65 6c 74  Out[nOut], iDelt
42988 61 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 57 72  a);..        bWr
42989 69 74 74 65 6e 20 3d 20 31 3b 0d 0a 20 20 20 20  itten = 1;..    
4298a 20 20 7d 0d 0a 20 20 20 20 20 20 70 4f 75 74 5b    }..      pOut[
4298b 6e 4f 75 74 2b 2b 5d 20 3d 20 30 78 30 31 3b 0d  nOut++] = 0x01;.
4298c 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 73  .      nOut += s
4298d 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
4298e 69 6e 74 28 26 70 4f 75 74 5b 6e 4f 75 74 5d 2c  int(&pOut[nOut],
4298f 20 69 43 6f 6c 29 3b 0d 0a 20 20 20 20 20 20 70   iCol);..      p
42990 4f 75 74 5b 6e 4f 75 74 2b 2b 5d 20 3d 20 30 78  Out[nOut++] = 0x
42991 30 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  02;..    }..    
42992 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f  fts3ColumnlistCo
42993 70 79 28 30 2c 20 26 70 29 3b 0d 0a 20 20 7d 0d  py(0, &p);..  }.
42994 0a 20 20 69 66 28 20 62 57 72 69 74 74 65 6e 20  .  if( bWritten 
42995 29 7b 0d 0a 20 20 20 20 70 4f 75 74 5b 6e 4f 75  ){..    pOut[nOu
42996 74 2b 2b 5d 20 3d 20 30 78 30 30 3b 0d 0a 20 20  t++] = 0x00;..  
42997 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 6e 4f  }....  return nO
42998 75 74 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  ut;..}....../*..
42999 2a 2a 20 4d 65 72 67 65 20 61 6c 6c 20 64 6f 63  ** Merge all doc
4299a 6c 69 73 74 73 20 69 6e 20 74 68 65 20 54 65 72  lists in the Ter
4299b 6d 53 65 6c 65 63 74 2e 61 61 4f 75 74 70 75 74  mSelect.aaOutput
4299c 5b 5d 20 61 72 72 61 79 20 69 6e 74 6f 20 61 20  [] array into a 
4299d 73 69 6e 67 6c 65 0d 0a 2a 2a 20 64 6f 63 6c 69  single..** docli
4299e 73 74 20 73 74 6f 72 65 64 20 69 6e 20 54 65 72  st stored in Ter
4299f 6d 53 65 6c 65 63 74 2e 61 61 4f 75 74 70 75 74  mSelect.aaOutput
429a0 5b 30 5d 2e 20 49 66 20 73 75 63 63 65 73 73 66  [0]. If successf
429a1 75 6c 2c 20 64 65 6c 65 74 65 20 61 6c 6c 0d 0a  ul, delete all..
429a2 2a 2a 20 6f 74 68 65 72 20 64 6f 63 6c 69 73 74  ** other doclist
429a3 73 20 28 65 78 63 65 70 74 20 74 68 65 20 61 61  s (except the aa
429a4 4f 75 74 70 75 74 5b 30 5d 20 6f 6e 65 29 20 61  Output[0] one) a
429a5 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
429a6 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  _OK...**..** If 
429a7 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63  an OOM error occ
429a8 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  urs, return SQLI
429a9 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69  TE_NOMEM. In thi
429aa 73 20 63 61 73 65 20 69 74 20 69 73 0d 0a 2a 2a  s case it is..**
429ab 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c   the responsibil
429ac 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65  ity of the calle
429ad 72 20 74 6f 20 66 72 65 65 20 61 6e 79 20 64 6f  r to free any do
429ae 63 6c 69 73 74 73 20 6c 65 66 74 20 69 6e 20 74  clists left in t
429af 68 65 0d 0a 2a 2a 20 54 65 72 6d 53 65 6c 65 63  he..** TermSelec
429b0 74 2e 61 61 4f 75 74 70 75 74 5b 5d 20 61 72 72  t.aaOutput[] arr
429b1 61 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ay...*/..static 
429b2 69 6e 74 20 66 74 73 33 54 65 72 6d 53 65 6c 65  int fts3TermSele
429b3 63 74 46 69 6e 69 73 68 4d 65 72 67 65 28 46 74  ctFinishMerge(Ft
429b4 73 33 54 61 62 6c 65 20 2a 70 2c 20 54 65 72 6d  s3Table *p, Term
429b5 53 65 6c 65 63 74 20 2a 70 54 53 29 7b 0d 0a 20  Select *pTS){.. 
429b6 20 63 68 61 72 20 2a 61 4f 75 74 20 3d 20 30 3b   char *aOut = 0;
429b7 0d 0a 20 20 69 6e 74 20 6e 4f 75 74 20 3d 20 30  ..  int nOut = 0
429b8 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20  ;..  int i;.... 
429b9 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
429ba 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 69 6e   the doclists in
429bb 20 74 68 65 20 61 61 4f 75 74 70 75 74 5b 5d 20   the aaOutput[] 
429bc 61 72 72 61 79 2e 20 4d 65 72 67 65 20 74 68 65  array. Merge the
429bd 6d 20 61 6c 6c 0d 0a 20 20 2a 2a 20 69 6e 74 6f  m all..  ** into
429be 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c 69 73   a single doclis
429bf 74 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28  t...  */..  for(
429c0 69 3d 30 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72  i=0; i<SizeofArr
429c1 61 79 28 70 54 53 2d 3e 61 61 4f 75 74 70 75 74  ay(pTS->aaOutput
429c2 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66  ); i++){..    if
429c3 28 20 70 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b  ( pTS->aaOutput[
429c4 69 5d 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  i] ){..      if(
429c5 20 21 61 4f 75 74 20 29 7b 0d 0a 20 20 20 20 20   !aOut ){..     
429c6 20 20 20 61 4f 75 74 20 3d 20 70 54 53 2d 3e 61     aOut = pTS->a
429c7 61 4f 75 74 70 75 74 5b 69 5d 3b 0d 0a 20 20 20  aOutput[i];..   
429c8 20 20 20 20 20 6e 4f 75 74 20 3d 20 70 54 53 2d       nOut = pTS-
429c9 3e 61 6e 4f 75 74 70 75 74 5b 69 5d 3b 0d 0a 20  >anOutput[i];.. 
429ca 20 20 20 20 20 20 20 70 54 53 2d 3e 61 61 4f 75         pTS->aaOu
429cb 74 70 75 74 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20  tput[i] = 0;..  
429cc 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
429cd 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 0d 0a 20      int nNew;.. 
429ce 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 4e 65         char *aNe
429cf 77 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69 6e  w;....        in
429d0 74 20 72 63 20 3d 20 66 74 73 33 44 6f 63 6c 69  t rc = fts3Docli
429d1 73 74 4f 72 4d 65 72 67 65 28 70 2d 3e 62 44 65  stOrMerge(p->bDe
429d2 73 63 49 64 78 2c 20 0d 0a 20 20 20 20 20 20 20  scIdx, ..       
429d3 20 20 20 20 20 70 54 53 2d 3e 61 61 4f 75 74 70       pTS->aaOutp
429d4 75 74 5b 69 5d 2c 20 70 54 53 2d 3e 61 6e 4f 75  ut[i], pTS->anOu
429d5 74 70 75 74 5b 69 5d 2c 20 61 4f 75 74 2c 20 6e  tput[i], aOut, n
429d6 4f 75 74 2c 20 26 61 4e 65 77 2c 20 26 6e 4e 65  Out, &aNew, &nNe
429d7 77 0d 0a 20 20 20 20 20 20 20 20 29 3b 0d 0a 20  w..        );.. 
429d8 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
429d9 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
429da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
429db 72 65 65 28 61 4f 75 74 29 3b 0d 0a 20 20 20 20  ree(aOut);..    
429dc 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
429dd 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  ..        }.... 
429de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
429df 72 65 65 28 70 54 53 2d 3e 61 61 4f 75 74 70 75  ree(pTS->aaOutpu
429e0 74 5b 69 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20  t[i]);..        
429e1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 4f 75  sqlite3_free(aOu
429e2 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 53  t);..        pTS
429e3 2d 3e 61 61 4f 75 74 70 75 74 5b 69 5d 20 3d 20  ->aaOutput[i] = 
429e4 30 3b 0d 0a 20 20 20 20 20 20 20 20 61 4f 75 74  0;..        aOut
429e5 20 3d 20 61 4e 65 77 3b 0d 0a 20 20 20 20 20 20   = aNew;..      
429e6 20 20 6e 4f 75 74 20 3d 20 6e 4e 65 77 3b 0d 0a    nOut = nNew;..
429e7 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
429e8 20 20 7d 0d 0a 0d 0a 20 20 70 54 53 2d 3e 61 61    }....  pTS->aa
429e9 4f 75 74 70 75 74 5b 30 5d 20 3d 20 61 4f 75 74  Output[0] = aOut
429ea 3b 0d 0a 20 20 70 54 53 2d 3e 61 6e 4f 75 74 70  ;..  pTS->anOutp
429eb 75 74 5b 30 5d 20 3d 20 6e 4f 75 74 3b 0d 0a 20  ut[0] = nOut;.. 
429ec 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
429ed 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
429ee 4d 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69 73  Merge the doclis
429ef 74 20 61 44 6f 63 6c 69 73 74 2f 6e 44 6f 63 6c  t aDoclist/nDocl
429f0 69 73 74 20 69 6e 74 6f 20 74 68 65 20 54 65 72  ist into the Ter
429f1 6d 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 70  mSelect object p
429f2 61 73 73 65 64 0d 0a 2a 2a 20 61 73 20 74 68 65  assed..** as the
429f3 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
429f4 20 54 68 65 20 6d 65 72 67 65 20 69 73 20 61 6e   The merge is an
429f5 20 22 4f 52 22 20 6d 65 72 67 65 20 28 73 65 65   "OR" merge (see
429f6 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 66 74   function..** ft
429f7 73 33 44 6f 63 6c 69 73 74 4f 72 4d 65 72 67 65  s3DoclistOrMerge
429f8 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 29 2e  () for details).
429f9 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ..**..** This fu
429fa 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
429fb 20 77 69 74 68 20 74 68 65 20 64 6f 63 6c 69 73   with the doclis
429fc 74 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  t for each term 
429fd 74 68 61 74 20 6d 61 74 63 68 65 73 0d 0a 2a 2a  that matches..**
429fe 20 61 20 71 75 65 72 69 65 64 20 70 72 65 66 69   a queried prefi
429ff 78 2e 20 49 74 20 6d 65 72 67 65 73 20 61 6c 6c  x. It merges all
42a00 20 74 68 65 73 65 20 64 6f 63 6c 69 73 74 73 20   these doclists 
42a01 69 6e 74 6f 20 6f 6e 65 2c 20 74 68 65 20 64 6f  into one, the do
42a02 63 6c 69 73 74 0d 0a 2a 2a 20 66 6f 72 20 74 68  clist..** for th
42a03 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 66  e specified pref
42a04 69 78 2e 20 53 69 6e 63 65 20 74 68 65 72 65 20  ix. Since there 
42a05 63 61 6e 20 62 65 20 61 20 76 65 72 79 20 6c 61  can be a very la
42a06 72 67 65 20 6e 75 6d 62 65 72 20 6f 66 0d 0a 2a  rge number of..*
42a07 2a 20 64 6f 63 6c 69 73 74 73 20 74 6f 20 6d 65  * doclists to me
42a08 72 67 65 2c 20 74 68 65 20 6d 65 72 67 69 6e 67  rge, the merging
42a09 20 69 73 20 64 6f 6e 65 20 70 61 69 72 2d 77 69   is done pair-wi
42a0a 73 65 20 75 73 69 6e 67 20 74 68 65 20 54 65 72  se using the Ter
42a0b 6d 53 65 6c 65 63 74 0d 0a 2a 2a 20 6f 62 6a 65  mSelect..** obje
42a0c 63 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  ct...**..** This
42a0d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
42a0e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74  s SQLITE_OK if t
42a0f 68 65 20 6d 65 72 67 65 20 69 73 20 73 75 63 63  he merge is succ
42a10 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 0d 0a 2a  essful, or an..*
42a11 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  * SQLite error c
42a12 6f 64 65 20 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  ode (SQLITE_NOME
42a13 4d 29 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  M) if an error o
42a14 63 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ccurs...*/..stat
42a15 69 63 20 69 6e 74 20 66 74 73 33 54 65 72 6d 53  ic int fts3TermS
42a16 65 6c 65 63 74 4d 65 72 67 65 28 0d 0a 20 20 46  electMerge(..  F
42a17 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20  ts3Table *p,    
42a18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42a19 2a 20 46 54 53 20 74 61 62 6c 65 20 68 61 6e 64  * FTS table hand
42a1a 6c 65 20 2a 2f 0d 0a 20 20 54 65 72 6d 53 65 6c  le */..  TermSel
42a1b 65 63 74 20 2a 70 54 53 2c 20 20 20 20 20 20 20  ect *pTS,       
42a1c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d           /* Term
42a1d 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 74 6f  Select object to
42a1e 20 6d 65 72 67 65 20 69 6e 74 6f 20 2a 2f 0d 0a   merge into */..
42a1f 20 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74    char *aDoclist
42a20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
42a21 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
42a22 64 6f 63 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e  doclist */..  in
42a23 74 20 6e 44 6f 63 6c 69 73 74 20 20 20 20 20 20  t nDoclist      
42a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42a25 20 53 69 7a 65 20 6f 66 20 61 44 6f 63 6c 69 73   Size of aDoclis
42a26 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 29  t in bytes */..)
42a27 7b 0d 0a 20 20 69 66 28 20 70 54 53 2d 3e 61 61  {..  if( pTS->aa
42a28 4f 75 74 70 75 74 5b 30 5d 3d 3d 30 20 29 7b 0d  Output[0]==0 ){.
42a29 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
42a2a 69 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72  is the first ter
42a2b 6d 20 73 65 6c 65 63 74 65 64 2c 20 63 6f 70 79  m selected, copy
42a2c 20 74 68 65 20 64 6f 63 6c 69 73 74 20 74 6f 20   the doclist to 
42a2d 74 68 65 20 6f 75 74 70 75 74 0d 0a 20 20 20 20  the output..    
42a2e 2a 2a 20 62 75 66 66 65 72 20 75 73 69 6e 67 20  ** buffer using 
42a2f 6d 65 6d 63 70 79 28 29 2e 20 2a 2f 0d 0a 20 20  memcpy(). */..  
42a30 20 20 70 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b    pTS->aaOutput[
42a31 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  0] = sqlite3_mal
42a32 6c 6f 63 28 6e 44 6f 63 6c 69 73 74 29 3b 0d 0a  loc(nDoclist);..
42a33 20 20 20 20 70 54 53 2d 3e 61 6e 4f 75 74 70 75      pTS->anOutpu
42a34 74 5b 30 5d 20 3d 20 6e 44 6f 63 6c 69 73 74 3b  t[0] = nDoclist;
42a35 0d 0a 20 20 20 20 69 66 28 20 70 54 53 2d 3e 61  ..    if( pTS->a
42a36 61 4f 75 74 70 75 74 5b 30 5d 20 29 7b 0d 0a 20  aOutput[0] ){.. 
42a37 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 53 2d       memcpy(pTS-
42a38 3e 61 61 4f 75 74 70 75 74 5b 30 5d 2c 20 61 44  >aaOutput[0], aD
42a39 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69 73 74  oclist, nDoclist
42a3a 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
42a3b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
42a3c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
42a3d 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  }..  }else{..   
42a3e 20 63 68 61 72 20 2a 61 4d 65 72 67 65 20 3d 20   char *aMerge = 
42a3f 61 44 6f 63 6c 69 73 74 3b 0d 0a 20 20 20 20 69  aDoclist;..    i
42a40 6e 74 20 6e 4d 65 72 67 65 20 3d 20 6e 44 6f 63  nt nMerge = nDoc
42a41 6c 69 73 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69  list;..    int i
42a42 4f 75 74 3b 0d 0a 0d 0a 20 20 20 20 66 6f 72 28  Out;....    for(
42a43 69 4f 75 74 3d 30 3b 20 69 4f 75 74 3c 53 69 7a  iOut=0; iOut<Siz
42a44 65 6f 66 41 72 72 61 79 28 70 54 53 2d 3e 61 61  eofArray(pTS->aa
42a45 4f 75 74 70 75 74 29 3b 20 69 4f 75 74 2b 2b 29  Output); iOut++)
42a46 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54 53  {..      if( pTS
42a47 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74 5d  ->aaOutput[iOut]
42a48 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ==0 ){..        
42a49 61 73 73 65 72 74 28 20 69 4f 75 74 3e 30 20 29  assert( iOut>0 )
42a4a 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 53 2d 3e  ;..        pTS->
42a4b 61 61 4f 75 74 70 75 74 5b 69 4f 75 74 5d 20 3d  aaOutput[iOut] =
42a4c 20 61 4d 65 72 67 65 3b 0d 0a 20 20 20 20 20 20   aMerge;..      
42a4d 20 20 70 54 53 2d 3e 61 6e 4f 75 74 70 75 74 5b    pTS->anOutput[
42a4e 69 4f 75 74 5d 20 3d 20 6e 4d 65 72 67 65 3b 0d  iOut] = nMerge;.
42a4f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
42a50 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
42a51 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 4e 65         char *aNe
42a52 77 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  w;..        int 
42a53 6e 4e 65 77 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  nNew;....       
42a54 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33 44 6f   int rc = fts3Do
42a55 63 6c 69 73 74 4f 72 4d 65 72 67 65 28 70 2d 3e  clistOrMerge(p->
42a56 62 44 65 73 63 49 64 78 2c 20 61 4d 65 72 67 65  bDescIdx, aMerge
42a57 2c 20 6e 4d 65 72 67 65 2c 20 0d 0a 20 20 20 20  , nMerge, ..    
42a58 20 20 20 20 20 20 20 20 70 54 53 2d 3e 61 61 4f          pTS->aaO
42a59 75 74 70 75 74 5b 69 4f 75 74 5d 2c 20 70 54 53  utput[iOut], pTS
42a5a 2d 3e 61 6e 4f 75 74 70 75 74 5b 69 4f 75 74 5d  ->anOutput[iOut]
42a5b 2c 20 26 61 4e 65 77 2c 20 26 6e 4e 65 77 0d 0a  , &aNew, &nNew..
42a5c 20 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20          );..    
42a5d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
42a5e 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
42a5f 20 20 20 20 69 66 28 20 61 4d 65 72 67 65 21 3d      if( aMerge!=
42a60 61 44 6f 63 6c 69 73 74 20 29 20 73 71 6c 69 74  aDoclist ) sqlit
42a61 65 33 5f 66 72 65 65 28 61 4d 65 72 67 65 29 3b  e3_free(aMerge);
42a62 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ..          retu
42a63 72 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20  rn rc;..        
42a64 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69 66 28  }....        if(
42a65 20 61 4d 65 72 67 65 21 3d 61 44 6f 63 6c 69 73   aMerge!=aDoclis
42a66 74 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  t ) sqlite3_free
42a67 28 61 4d 65 72 67 65 29 3b 0d 0a 20 20 20 20 20  (aMerge);..     
42a68 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
42a69 70 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f  pTS->aaOutput[iO
42a6a 75 74 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 70  ut]);..        p
42a6b 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75  TS->aaOutput[iOu
42a6c 74 5d 20 3d 20 30 3b 0d 0a 20 20 0d 0a 20 20 20  t] = 0;..  ..   
42a6d 20 20 20 20 20 61 4d 65 72 67 65 20 3d 20 61 4e       aMerge = aN
42a6e 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 6e 4d 65  ew;..        nMe
42a6f 72 67 65 20 3d 20 6e 4e 65 77 3b 0d 0a 20 20 20  rge = nNew;..   
42a70 20 20 20 20 20 69 66 28 20 28 69 4f 75 74 2b 31       if( (iOut+1
42a71 29 3d 3d 53 69 7a 65 6f 66 41 72 72 61 79 28 70  )==SizeofArray(p
42a72 54 53 2d 3e 61 61 4f 75 74 70 75 74 29 20 29 7b  TS->aaOutput) ){
42a73 0d 0a 20 20 20 20 20 20 20 20 20 20 70 54 53 2d  ..          pTS-
42a74 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74 5d 20  >aaOutput[iOut] 
42a75 3d 20 61 4d 65 72 67 65 3b 0d 0a 20 20 20 20 20  = aMerge;..     
42a76 20 20 20 20 20 70 54 53 2d 3e 61 6e 4f 75 74 70       pTS->anOutp
42a77 75 74 5b 69 4f 75 74 5d 20 3d 20 6e 4d 65 72 67  ut[iOut] = nMerg
42a78 65 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  e;..        }.. 
42a79 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
42a7a 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
42a7b 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
42a7c 0d 0a 2a 2a 20 41 70 70 65 6e 64 20 53 65 67 52  ..** Append SegR
42a7d 65 61 64 65 72 20 6f 62 6a 65 63 74 20 70 4e 65  eader object pNe
42a7e 77 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  w to the end of 
42a7f 74 68 65 20 70 43 73 72 2d 3e 61 70 53 65 67 6d  the pCsr->apSegm
42a80 65 6e 74 5b 5d 20 61 72 72 61 79 2e 0d 0a 2a 2f  ent[] array...*/
42a81 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
42a82 33 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72  3SegReaderCursor
42a83 41 70 70 65 6e 64 28 0d 0a 20 20 46 74 73 33 4d  Append(..  Fts3M
42a84 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 2a 70  ultiSegReader *p
42a85 43 73 72 2c 20 0d 0a 20 20 46 74 73 33 53 65 67  Csr, ..  Fts3Seg
42a86 52 65 61 64 65 72 20 2a 70 4e 65 77 0d 0a 29 7b  Reader *pNew..){
42a87 0d 0a 20 20 69 66 28 20 28 70 43 73 72 2d 3e 6e  ..  if( (pCsr->n
42a88 53 65 67 6d 65 6e 74 25 31 36 29 3d 3d 30 20 29  Segment%16)==0 )
42a89 7b 0d 0a 20 20 20 20 46 74 73 33 53 65 67 52 65  {..    Fts3SegRe
42a8a 61 64 65 72 20 2a 2a 61 70 4e 65 77 3b 0d 0a 20  ader **apNew;.. 
42a8b 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
42a8c 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 20 2b  pCsr->nSegment +
42a8d 20 31 36 29 2a 73 69 7a 65 6f 66 28 46 74 73 33   16)*sizeof(Fts3
42a8e 53 65 67 52 65 61 64 65 72 2a 29 3b 0d 0a 20 20  SegReader*);..  
42a8f 20 20 61 70 4e 65 77 20 3d 20 28 46 74 73 33 53    apNew = (Fts3S
42a90 65 67 52 65 61 64 65 72 20 2a 2a 29 73 71 6c 69  egReader **)sqli
42a91 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72  te3_realloc(pCsr
42a92 2d 3e 61 70 53 65 67 6d 65 6e 74 2c 20 6e 42 79  ->apSegment, nBy
42a93 74 65 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 61  te);..    if( !a
42a94 70 4e 65 77 20 29 7b 0d 0a 20 20 20 20 20 20 73  pNew ){..      s
42a95 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
42a96 64 65 72 46 72 65 65 28 70 4e 65 77 29 3b 0d 0a  derFree(pNew);..
42a97 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
42a98 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
42a99 7d 0d 0a 20 20 20 20 70 43 73 72 2d 3e 61 70 53  }..    pCsr->apS
42a9a 65 67 6d 65 6e 74 20 3d 20 61 70 4e 65 77 3b 0d  egment = apNew;.
42a9b 0a 20 20 7d 0d 0a 20 20 70 43 73 72 2d 3e 61 70  .  }..  pCsr->ap
42a9c 53 65 67 6d 65 6e 74 5b 70 43 73 72 2d 3e 6e 53  Segment[pCsr->nS
42a9d 65 67 6d 65 6e 74 2b 2b 5d 20 3d 20 70 4e 65 77  egment++] = pNew
42a9e 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
42a9f 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  TE_OK;..}..../*.
42aa0 0a 2a 2a 20 41 64 64 20 73 65 67 2d 72 65 61 64  .** Add seg-read
42aa1 65 72 20 6f 62 6a 65 63 74 73 20 74 6f 20 74 68  er objects to th
42aa2 65 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65  e Fts3MultiSegRe
42aa3 61 64 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73  ader object pass
42aa4 65 64 20 61 73 20 74 68 65 0d 0a 2a 2a 20 38 74  ed as the..** 8t
42aa5 68 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2a 0d  h argument...**.
42aa6 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
42aa7 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  n returns SQLITE
42aa8 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
42aa9 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  l, or an SQLite 
42aaa 65 72 72 6f 72 20 63 6f 64 65 0d 0a 2a 2a 20 6f  error code..** o
42aab 74 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a 73  therwise...*/..s
42aac 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
42aad 67 52 65 61 64 65 72 43 75 72 73 6f 72 28 0d 0a  gReaderCursor(..
42aae 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
42aaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ab0 20 20 2f 2a 20 46 54 53 33 20 74 61 62 6c 65 20    /* FTS3 table 
42ab1 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74  handle */..  int
42ab2 20 69 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20   iIndex,        
42ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42ab4 49 6e 64 65 78 20 74 6f 20 73 65 61 72 63 68 20  Index to search 
42ab5 28 66 72 6f 6d 20 30 20 74 6f 20 70 2d 3e 6e 49  (from 0 to p->nI
42ab6 6e 64 65 78 2d 31 29 20 2a 2f 0d 0a 20 20 69 6e  ndex-1) */..  in
42ab7 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 20 20 20  t iLevel,       
42ab8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42ab9 20 4c 65 76 65 6c 20 6f 66 20 73 65 67 6d 65 6e   Level of segmen
42aba 74 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0d 0a 20  ts to scan */.. 
42abb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65   const char *zTe
42abc 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
42abd 20 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72   /* Term to quer
42abe 79 20 66 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  y for */..  int 
42abf 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  nTerm,          
42ac0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
42ac1 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20 69 6e 20  ize of zTerm in 
42ac2 62 79 74 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  bytes */..  int 
42ac3 69 73 50 72 65 66 69 78 2c 20 20 20 20 20 20 20  isPrefix,       
42ac4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
42ac5 72 75 65 20 66 6f 72 20 61 20 70 72 65 66 69 78  rue for a prefix
42ac6 20 73 65 61 72 63 68 20 2a 2f 0d 0a 20 20 69 6e   search */..  in
42ac7 74 20 69 73 53 63 61 6e 2c 20 20 20 20 20 20 20  t isScan,       
42ac8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42ac9 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20 66 72   True to scan fr
42aca 6f 6d 20 7a 54 65 72 6d 20 74 6f 20 45 4f 46 20  om zTerm to EOF 
42acb 2a 2f 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69 53  */..  Fts3MultiS
42acc 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 20 20  egReader *pCsr  
42acd 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
42ace 6f 62 6a 65 63 74 20 74 6f 20 70 6f 70 75 6c 61  object to popula
42acf 74 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  te */..){..  int
42ad0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
42ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42ad2 45 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 20  Error code */.. 
42ad3 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
42ad4 53 74 6d 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Stmt = 0;       
42ad5 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 74 6f   /* Statement to
42ad6 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
42ad7 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20   segments */..  
42ad8 69 6e 74 20 72 63 32 3b 20 20 20 20 20 20 20 20  int rc2;        
42ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ada 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 73 71 6c  /* Result of sql
42adb 69 74 65 33 5f 72 65 73 65 74 28 29 20 2a 2f 0d  ite3_reset() */.
42adc 0a 0d 0a 20 20 2f 2a 20 49 66 20 69 4c 65 76 65  ...  /* If iLeve
42add 6c 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 30  l is less than 0
42ade 20 61 6e 64 20 74 68 69 73 20 69 73 20 6e 6f 74   and this is not
42adf 20 61 20 73 63 61 6e 2c 20 69 6e 63 6c 75 64 65   a scan, include
42ae0 20 61 20 73 65 67 2d 72 65 61 64 65 72 20 0d 0a   a seg-reader ..
42ae1 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 65 6e    ** for the pen
42ae2 64 69 6e 67 2d 74 65 72 6d 73 2e 20 49 66 20 74  ding-terms. If t
42ae3 68 69 73 20 69 73 20 61 20 73 63 61 6e 2c 20 74  his is a scan, t
42ae4 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 6d 75  hen this call mu
42ae5 73 74 20 62 65 20 62 65 69 6e 67 0d 0a 20 20 2a  st be being..  *
42ae6 2a 20 6d 61 64 65 20 62 79 20 61 6e 20 66 74 73  * made by an fts
42ae7 34 61 75 78 20 6d 6f 64 75 6c 65 2c 20 6e 6f 74  4aux module, not
42ae8 20 61 6e 20 46 54 53 20 74 61 62 6c 65 2e 20 49   an FTS table. I
42ae9 6e 20 74 68 69 73 20 63 61 73 65 20 63 61 6c 6c  n this case call
42aea 69 6e 67 0d 0a 20 20 2a 2a 20 46 74 73 33 53 65  ing..  ** Fts3Se
42aeb 67 52 65 61 64 65 72 50 65 6e 64 69 6e 67 20 6d  gReaderPending m
42aec 69 67 68 74 20 73 65 67 66 61 75 6c 74 2c 20 61  ight segfault, a
42aed 73 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63  s the data struc
42aee 74 75 72 65 73 20 75 73 65 64 20 62 79 20 0d 0a  tures used by ..
42aef 20 20 2a 2a 20 66 74 73 34 61 75 78 20 61 72 65    ** fts4aux are
42af0 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20   not completely 
42af1 70 6f 70 75 6c 61 74 65 64 2e 20 53 6f 20 69 74  populated. So it
42af2 27 73 20 65 61 73 69 65 73 74 20 74 6f 20 66 69  's easiest to fi
42af3 6c 74 65 72 20 74 68 65 73 65 0d 0a 20 20 2a 2a  lter these..  **
42af4 20 63 61 6c 6c 73 20 6f 75 74 20 68 65 72 65 2e   calls out here.
42af5 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69 4c 65 76    */..  if( iLev
42af6 65 6c 3c 30 20 26 26 20 70 2d 3e 61 49 6e 64 65  el<0 && p->aInde
42af7 78 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 53 65  x ){..    Fts3Se
42af8 67 52 65 61 64 65 72 20 2a 70 53 65 67 20 3d 20  gReader *pSeg = 
42af9 30 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  0;..    rc = sql
42afa 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
42afb 72 50 65 6e 64 69 6e 67 28 70 2c 20 69 49 6e 64  rPending(p, iInd
42afc 65 78 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  ex, zTerm, nTerm
42afd 2c 20 69 73 50 72 65 66 69 78 2c 20 26 70 53 65  , isPrefix, &pSe
42afe 67 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  g);..    if( rc=
42aff 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 53  =SQLITE_OK && pS
42b00 65 67 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  eg ){..      rc 
42b01 3d 20 66 74 73 33 53 65 67 52 65 61 64 65 72 43  = fts3SegReaderC
42b02 75 72 73 6f 72 41 70 70 65 6e 64 28 70 43 73 72  ursorAppend(pCsr
42b03 2c 20 70 53 65 67 29 3b 0d 0a 20 20 20 20 7d 0d  , pSeg);..    }.
42b04 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 69 4c  .  }....  if( iL
42b05 65 76 65 6c 21 3d 46 54 53 33 5f 53 45 47 43 55  evel!=FTS3_SEGCU
42b06 52 53 4f 52 5f 50 45 4e 44 49 4e 47 20 29 7b 0d  RSOR_PENDING ){.
42b07 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
42b08 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
42b09 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
42b0a 33 41 6c 6c 53 65 67 64 69 72 73 28 70 2c 20 69  3AllSegdirs(p, i
42b0b 49 6e 64 65 78 2c 20 69 4c 65 76 65 6c 2c 20 26  Index, iLevel, &
42b0c 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a  pStmt);..    }..
42b0d 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d  ..    while( rc=
42b0e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 53 51  =SQLITE_OK && SQ
42b0f 4c 49 54 45 5f 52 4f 57 3d 3d 28 72 63 20 3d 20  LITE_ROW==(rc = 
42b10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
42b11 6d 74 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 46  mt)) ){..      F
42b12 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 53  ts3SegReader *pS
42b13 65 67 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 20  eg = 0;....     
42b14 20 2f 2a 20 52 65 61 64 20 74 68 65 20 76 61 6c   /* Read the val
42b15 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ues returned by 
42b16 74 68 65 20 53 45 4c 45 43 54 20 69 6e 74 6f 20  the SELECT into 
42b17 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 2e  local variables.
42b18 20 2a 2f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   */..      sqlit
42b19 65 33 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  e3_int64 iStartB
42b1a 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 63  lock = sqlite3_c
42b1b 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d  olumn_int64(pStm
42b1c 74 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 73 71  t, 1);..      sq
42b1d 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 65 61  lite3_int64 iLea
42b1e 76 65 73 45 6e 64 42 6c 6f 63 6b 20 3d 20 73 71  vesEndBlock = sq
42b1f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
42b20 36 34 28 70 53 74 6d 74 2c 20 32 29 3b 0d 0a 20  64(pStmt, 2);.. 
42b21 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
42b22 36 34 20 69 45 6e 64 42 6c 6f 63 6b 20 3d 20 73  64 iEndBlock = s
42b23 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
42b24 74 36 34 28 70 53 74 6d 74 2c 20 33 29 3b 0d 0a  t64(pStmt, 3);..
42b25 20 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 20        int nRoot 
42b26 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
42b27 5f 62 79 74 65 73 28 70 53 74 6d 74 2c 20 34 29  _bytes(pStmt, 4)
42b28 3b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f  ;..      char co
42b29 6e 73 74 20 2a 7a 52 6f 6f 74 20 3d 20 73 71 6c  nst *zRoot = sql
42b2a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
42b2b 28 70 53 74 6d 74 2c 20 34 29 3b 0d 0a 0d 0a 20  (pStmt, 4);.... 
42b2c 20 20 20 20 20 2f 2a 20 49 66 20 7a 54 65 72 6d       /* If zTerm
42b2d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e   is not NULL, an
42b2e 64 20 74 68 69 73 20 73 65 67 6d 65 6e 74 20 69  d this segment i
42b2f 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 65 6e 74  s not stored ent
42b30 69 72 65 6c 79 20 6f 6e 20 69 74 73 0d 0a 20 20  irely on its..  
42b31 20 20 20 20 2a 2a 20 72 6f 6f 74 20 6e 6f 64 65      ** root node
42b32 2c 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 6c  , the range of l
42b33 65 61 76 65 73 20 73 63 61 6e 6e 65 64 20 63 61  eaves scanned ca
42b34 6e 20 62 65 20 72 65 64 75 63 65 64 2e 20 44 6f  n be reduced. Do
42b35 20 74 68 69 73 2e 20 2a 2f 0d 0a 20 20 20 20 20   this. */..     
42b36 20 69 66 28 20 69 53 74 61 72 74 42 6c 6f 63 6b   if( iStartBlock
42b37 20 26 26 20 7a 54 65 72 6d 20 29 7b 0d 0a 20 20   && zTerm ){..  
42b38 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
42b39 74 36 34 20 2a 70 69 20 3d 20 28 69 73 50 72 65  t64 *pi = (isPre
42b3a 66 69 78 20 3f 20 26 69 4c 65 61 76 65 73 45 6e  fix ? &iLeavesEn
42b3b 64 42 6c 6f 63 6b 20 3a 20 30 29 3b 0d 0a 20 20  dBlock : 0);..  
42b3c 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53        rc = fts3S
42b3d 65 6c 65 63 74 4c 65 61 66 28 70 2c 20 7a 54 65  electLeaf(p, zTe
42b3e 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 52 6f 6f 74  rm, nTerm, zRoot
42b3f 2c 20 6e 52 6f 6f 74 2c 20 26 69 53 74 61 72 74  , nRoot, &iStart
42b40 42 6c 6f 63 6b 2c 20 70 69 29 3b 0d 0a 20 20 20  Block, pi);..   
42b41 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
42b42 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69  ITE_OK ) goto fi
42b43 6e 69 73 68 65 64 3b 0d 0a 20 20 20 20 20 20 20  nished;..       
42b44 20 69 66 28 20 69 73 50 72 65 66 69 78 3d 3d 30   if( isPrefix==0
42b45 20 26 26 20 69 73 53 63 61 6e 3d 3d 30 20 29 20   && isScan==0 ) 
42b46 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 20  iLeavesEndBlock 
42b47 3d 20 69 53 74 61 72 74 42 6c 6f 63 6b 3b 0d 0a  = iStartBlock;..
42b48 20 20 20 20 20 20 7d 0d 0a 20 0d 0a 20 20 20 20        }.. ..    
42b49 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
42b4a 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28 70  s3SegReaderNew(p
42b4b 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 2b 31 2c  Csr->nSegment+1,
42b4c 20 0d 0a 20 20 20 20 20 20 20 20 20 20 69 53 74   ..          iSt
42b4d 61 72 74 42 6c 6f 63 6b 2c 20 69 4c 65 61 76 65  artBlock, iLeave
42b4e 73 45 6e 64 42 6c 6f 63 6b 2c 20 69 45 6e 64 42  sEndBlock, iEndB
42b4f 6c 6f 63 6b 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f  lock, zRoot, nRo
42b50 6f 74 2c 20 26 70 53 65 67 0d 0a 20 20 20 20 20  ot, &pSeg..     
42b51 20 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72   );..      if( r
42b52 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
42b53 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0d 0a 20  oto finished;.. 
42b54 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65       rc = fts3Se
42b55 67 52 65 61 64 65 72 43 75 72 73 6f 72 41 70 70  gReaderCursorApp
42b56 65 6e 64 28 70 43 73 72 2c 20 70 53 65 67 29 3b  end(pCsr, pSeg);
42b57 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
42b58 20 66 69 6e 69 73 68 65 64 3a 0d 0a 20 20 72 63   finished:..  rc
42b59 32 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  2 = sqlite3_rese
42b5a 74 28 70 53 74 6d 74 29 3b 0d 0a 20 20 69 66 28  t(pStmt);..  if(
42b5b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
42b5c 20 29 20 72 63 20 3d 20 72 63 32 3b 0d 0a 0d 0a   ) rc = rc2;....
42b5d 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
42b5e 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20 75 70  .../*..** Set up
42b5f 20 61 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74   a cursor object
42b60 20 66 6f 72 20 69 74 65 72 61 74 69 6e 67 20 74   for iterating t
42b61 68 72 6f 75 67 68 20 61 20 66 75 6c 6c 2d 74 65  hrough a full-te
42b62 78 74 20 69 6e 64 65 78 20 6f 72 20 61 20 0d 0a  xt index or a ..
42b63 2a 2a 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20  ** single level 
42b64 74 68 65 72 65 69 6e 2e 0d 0a 2a 2f 0d 0a 53 51  therein...*/..SQ
42b65 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
42b66 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
42b67 65 61 64 65 72 43 75 72 73 6f 72 28 0d 0a 20 20  eaderCursor(..  
42b68 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
42b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42b6a 2f 2a 20 46 54 53 33 20 74 61 62 6c 65 20 68 61  /* FTS3 table ha
42b6b 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ndle */..  int i
42b6c 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  Index,          
42b6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
42b6e 64 65 78 20 74 6f 20 73 65 61 72 63 68 20 28 66  dex to search (f
42b6f 72 6f 6d 20 30 20 74 6f 20 70 2d 3e 6e 49 6e 64  rom 0 to p->nInd
42b70 65 78 2d 31 29 20 2a 2f 0d 0a 20 20 69 6e 74 20  ex-1) */..  int 
42b71 69 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20  iLevel,         
42b72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
42b73 65 76 65 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73  evel of segments
42b74 20 74 6f 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 63   to scan */..  c
42b75 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
42b76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
42b77 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79 20  * Term to query 
42b78 66 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54  for */..  int nT
42b79 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
42b7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
42b7b 65 20 6f 66 20 7a 54 65 72 6d 20 69 6e 20 62 79  e of zTerm in by
42b7c 74 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73  tes */..  int is
42b7d 50 72 65 66 69 78 2c 20 20 20 20 20 20 20 20 20  Prefix,         
42b7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
42b7f 65 20 66 6f 72 20 61 20 70 72 65 66 69 78 20 73  e for a prefix s
42b80 65 61 72 63 68 20 2a 2f 0d 0a 20 20 69 6e 74 20  earch */..  int 
42b81 69 73 53 63 61 6e 2c 20 20 20 20 20 20 20 20 20  isScan,         
42b82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
42b83 72 75 65 20 74 6f 20 73 63 61 6e 20 66 72 6f 6d  rue to scan from
42b84 20 7a 54 65 72 6d 20 74 6f 20 45 4f 46 20 2a 2f   zTerm to EOF */
42b85 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  ..  Fts3MultiSeg
42b86 52 65 61 64 65 72 20 2a 70 43 73 72 20 20 20 20  Reader *pCsr    
42b87 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a     /* Cursor obj
42b88 65 63 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20  ect to populate 
42b89 2a 2f 0d 0a 29 7b 0d 0a 20 20 61 73 73 65 72 74  */..){..  assert
42b8a 28 20 69 49 6e 64 65 78 3e 3d 30 20 26 26 20 69  ( iIndex>=0 && i
42b8b 49 6e 64 65 78 3c 70 2d 3e 6e 49 6e 64 65 78 20  Index<p->nIndex 
42b8c 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 4c  );..  assert( iL
42b8d 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55  evel==FTS3_SEGCU
42b8e 52 53 4f 52 5f 41 4c 4c 0d 0a 20 20 20 20 20 20  RSOR_ALL..      
42b8f 7c 7c 20 20 69 4c 65 76 65 6c 3d 3d 46 54 53 33  ||  iLevel==FTS3
42b90 5f 53 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49  _SEGCURSOR_PENDI
42b91 4e 47 20 0d 0a 20 20 20 20 20 20 7c 7c 20 20 69  NG ..      ||  i
42b92 4c 65 76 65 6c 3e 3d 30 0d 0a 20 20 29 3b 0d 0a  Level>=0..  );..
42b93 20 20 61 73 73 65 72 74 28 20 69 4c 65 76 65 6c    assert( iLevel
42b94 3c 46 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58  <FTS3_SEGDIR_MAX
42b95 4c 45 56 45 4c 20 29 3b 0d 0a 20 20 61 73 73 65  LEVEL );..  asse
42b96 72 74 28 20 46 54 53 33 5f 53 45 47 43 55 52 53  rt( FTS3_SEGCURS
42b97 4f 52 5f 41 4c 4c 3c 30 20 26 26 20 46 54 53 33  OR_ALL<0 && FTS3
42b98 5f 53 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49  _SEGCURSOR_PENDI
42b99 4e 47 3c 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  NG<0 );..  asser
42b9a 74 28 20 69 73 50 72 65 66 69 78 3d 3d 30 20 7c  t( isPrefix==0 |
42b9b 7c 20 69 73 53 63 61 6e 3d 3d 30 20 29 3b 0d 0a  | isScan==0 );..
42b9c 0d 0a 20 20 2f 2a 20 22 69 73 53 63 61 6e 22 20  ..  /* "isScan" 
42b9d 69 73 20 6f 6e 6c 79 20 73 65 74 20 74 6f 20 74  is only set to t
42b9e 72 75 65 20 62 79 20 74 68 65 20 66 74 34 61 75  rue by the ft4au
42b9f 78 20 6d 6f 64 75 6c 65 2c 20 61 6e 20 6f 72 64  x module, an ord
42ba0 69 6e 61 72 79 0d 0a 20 20 2a 2a 20 66 75 6c 6c  inary..  ** full
42ba1 2d 74 65 78 74 20 74 61 62 6c 65 73 2e 20 2a 2f  -text tables. */
42ba2 0d 0a 20 20 61 73 73 65 72 74 28 20 69 73 53 63  ..  assert( isSc
42ba3 61 6e 3d 3d 30 20 7c 7c 20 70 2d 3e 61 49 6e 64  an==0 || p->aInd
42ba4 65 78 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 6d 65  ex==0 );....  me
42ba5 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69  mset(pCsr, 0, si
42ba6 7a 65 6f 66 28 46 74 73 33 4d 75 6c 74 69 53 65  zeof(Fts3MultiSe
42ba7 67 52 65 61 64 65 72 29 29 3b 0d 0a 0d 0a 20 20  gReader));....  
42ba8 72 65 74 75 72 6e 20 66 74 73 33 53 65 67 52 65  return fts3SegRe
42ba9 61 64 65 72 43 75 72 73 6f 72 28 0d 0a 20 20 20  aderCursor(..   
42baa 20 20 20 70 2c 20 69 49 6e 64 65 78 2c 20 69 4c     p, iIndex, iL
42bab 65 76 65 6c 2c 20 7a 54 65 72 6d 2c 20 6e 54 65  evel, zTerm, nTe
42bac 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 69 73  rm, isPrefix, is
42bad 53 63 61 6e 2c 20 70 43 73 72 0d 0a 20 20 29 3b  Scan, pCsr..  );
42bae 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e  ..}..../*..** In
42baf 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 69 74 73   addition to its
42bb0 20 63 75 72 72 65 6e 74 20 63 6f 6e 66 69 67 75   current configu
42bb1 72 61 74 69 6f 6e 2c 20 68 61 76 65 20 74 68 65  ration, have the
42bb2 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
42bb3 64 65 72 0d 0a 2a 2a 20 70 61 73 73 65 64 20 61  der..** passed a
42bb4 73 20 74 68 65 20 34 74 68 20 61 72 67 75 6d 65  s the 4th argume
42bb5 6e 74 20 61 6c 73 6f 20 73 63 61 6e 20 74 68 65  nt also scan the
42bb6 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 65 72   doclist for ter
42bb7 6d 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 0d 0a  m zTerm/nTerm...
42bb8 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  **..** SQLITE_OK
42bb9 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
42bba 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  no error occurs,
42bbb 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51   otherwise an SQ
42bbc 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
42bbd 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
42bbe 20 66 74 73 33 53 65 67 52 65 61 64 65 72 43 75   fts3SegReaderCu
42bbf 72 73 6f 72 41 64 64 5a 65 72 6f 28 0d 0a 20 20  rsorAddZero(..  
42bc0 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
42bc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42bc2 2f 2a 20 46 54 53 20 76 69 72 74 75 61 6c 20 74  /* FTS virtual t
42bc3 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a  able handle */..
42bc4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
42bc5 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
42bc6 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 73 63 61    /* Term to sca
42bc7 6e 20 64 6f 63 6c 69 73 74 20 6f 66 20 2a 2f 0d  n doclist of */.
42bc8 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20  .  int nTerm,   
42bc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42bca 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
42bcb 62 79 74 65 73 20 69 6e 20 7a 54 65 72 6d 20 2a  bytes in zTerm *
42bcc 2f 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65  /..  Fts3MultiSe
42bcd 67 52 65 61 64 65 72 20 2a 70 43 73 72 20 20 20  gReader *pCsr   
42bce 20 20 20 20 20 2f 2a 20 46 74 73 33 4d 75 6c 74       /* Fts3Mult
42bcf 69 53 65 67 52 65 61 64 65 72 20 74 6f 20 6d 6f  iSegReader to mo
42bd0 64 69 66 79 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 72  dify */..){..  r
42bd1 65 74 75 72 6e 20 66 74 73 33 53 65 67 52 65 61  eturn fts3SegRea
42bd2 64 65 72 43 75 72 73 6f 72 28 70 2c 20 30 2c 20  derCursor(p, 0, 
42bd3 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f 41  FTS3_SEGCURSOR_A
42bd4 4c 4c 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  LL, zTerm, nTerm
42bd5 2c 20 30 2c 20 30 2c 70 43 73 72 29 3b 0d 0a 7d  , 0, 0,pCsr);..}
42bd6 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f 70 65 6e 20  ..../*..** Open 
42bd7 61 6e 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52  an Fts3MultiSegR
42bd8 65 61 64 65 72 20 74 6f 20 73 63 61 6e 20 74 68  eader to scan th
42bd9 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 65  e doclist for te
42bda 72 6d 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 20  rm zTerm/nTerm. 
42bdb 4f 72 2c 0d 0a 2a 2a 20 69 66 20 69 73 50 72 65  Or,..** if isPre
42bdc 66 69 78 20 69 73 20 74 72 75 65 2c 20 74 6f 20  fix is true, to 
42bdd 73 63 61 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  scan the doclist
42bde 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 66   for all terms f
42bdf 6f 72 20 77 68 69 63 68 20 0d 0a 2a 2a 20 7a 54  or which ..** zT
42be0 65 72 6d 2f 6e 54 65 72 6d 20 69 73 20 61 20 70  erm/nTerm is a p
42be1 72 65 66 69 78 2e 20 49 66 20 73 75 63 63 65 73  refix. If succes
42be2 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
42be3 49 54 45 5f 4f 4b 20 61 6e 64 20 77 72 69 74 65  ITE_OK and write
42be4 0d 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  ..** a pointer t
42be5 6f 20 74 68 65 20 6e 65 77 20 46 74 73 33 4d 75  o the new Fts3Mu
42be6 6c 74 69 53 65 67 52 65 61 64 65 72 20 74 6f 20  ltiSegReader to 
42be7 2a 70 70 53 65 67 63 73 72 2e 20 4f 74 68 65 72  *ppSegcsr. Other
42be8 77 69 73 65 2c 20 72 65 74 75 72 6e 0d 0a 2a 2a  wise, return..**
42be9 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
42bea 20 63 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49   code...**..** I
42beb 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
42bec 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63  ibility of the c
42bed 61 6c 6c 65 72 20 74 6f 20 66 72 65 65 20 74 68  aller to free th
42bee 69 73 20 6f 62 6a 65 63 74 20 62 79 20 65 76 65  is object by eve
42bef 6e 74 75 61 6c 6c 79 0d 0a 2a 2a 20 70 61 73 73  ntually..** pass
42bf0 69 6e 67 20 69 74 20 74 6f 20 66 74 73 33 53 65  ing it to fts3Se
42bf1 67 52 65 61 64 65 72 43 75 72 73 6f 72 46 72 65  gReaderCursorFre
42bf2 65 28 29 20 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c  e() ..**..** SQL
42bf3 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
42bf4 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ed if no error o
42bf5 63 63 75 72 73 2c 20 6f 74 68 65 72 77 69 73 65  ccurs, otherwise
42bf6 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
42bf7 20 63 6f 64 65 2e 0d 0a 2a 2a 20 4f 75 74 70 75   code...** Outpu
42bf8 74 20 70 61 72 61 6d 65 74 65 72 20 2a 70 70 53  t parameter *ppS
42bf9 65 67 63 73 72 20 69 73 20 73 65 74 20 74 6f 20  egcsr is set to 
42bfa 30 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  0 if an error oc
42bfb 63 75 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  curs...*/..stati
42bfc 63 20 69 6e 74 20 66 74 73 33 54 65 72 6d 53 65  c int fts3TermSe
42bfd 67 52 65 61 64 65 72 43 75 72 73 6f 72 28 0d 0a  gReaderCursor(..
42bfe 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
42bff 73 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  sr,             
42c00 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
42c01 6c 65 20 63 75 72 73 6f 72 20 68 61 6e 64 6c 65  le cursor handle
42c02 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
42c03 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20  r *zTerm,       
42c04 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74         /* Term t
42c05 6f 20 71 75 65 72 79 20 66 6f 72 20 2a 2f 0d 0a  o query for */..
42c06 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20    int nTerm,    
42c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c08 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 65    /* Size of zTe
42c09 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  rm in bytes */..
42c0a 20 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 20    int isPrefix, 
42c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c0c 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
42c0d 70 72 65 66 69 78 20 73 65 61 72 63 68 20 2a 2f  prefix search */
42c0e 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  ..  Fts3MultiSeg
42c0f 52 65 61 64 65 72 20 2a 2a 70 70 53 65 67 63 73  Reader **ppSegcs
42c10 72 20 20 20 2f 2a 20 4f 55 54 3a 20 41 6c 6c 6f  r   /* OUT: Allo
42c11 63 61 74 65 64 20 73 65 67 2d 72 65 61 64 65 72  cated seg-reader
42c12 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 29 7b 0d 0a   cursor */..){..
42c13 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65    Fts3MultiSegRe
42c14 61 64 65 72 20 2a 70 53 65 67 63 73 72 3b 20 20  ader *pSegcsr;  
42c15 20 20 2f 2a 20 4f 62 6a 65 63 74 20 74 6f 20 61    /* Object to a
42c16 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75  llocate and retu
42c17 72 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 20  rn */..  int rc 
42c18 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 20  = SQLITE_NOMEM; 
42c19 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
42c1a 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20  rn code */....  
42c1b 70 53 65 67 63 73 72 20 3d 20 73 71 6c 69 74 65  pSegcsr = sqlite
42c1c 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
42c1d 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64  Fts3MultiSegRead
42c1e 65 72 29 29 3b 0d 0a 20 20 69 66 28 20 70 53 65  er));..  if( pSe
42c1f 67 63 73 72 20 29 7b 0d 0a 20 20 20 20 69 6e 74  gcsr ){..    int
42c20 20 69 3b 0d 0a 20 20 20 20 69 6e 74 20 62 46 6f   i;..    int bFo
42c21 75 6e 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  und = 0;        
42c22 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f         /* True o
42c23 6e 63 65 20 61 6e 20 69 6e 64 65 78 20 68 61 73  nce an index has
42c24 20 62 65 65 6e 20 66 6f 75 6e 64 20 2a 2f 0d 0a   been found */..
42c25 20 20 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70      Fts3Table *p
42c26 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
42c27 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
42c28 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69 73 50  ;....    if( isP
42c29 72 65 66 69 78 20 29 7b 0d 0a 20 20 20 20 20 20  refix ){..      
42c2a 66 6f 72 28 69 3d 31 3b 20 62 46 6f 75 6e 64 3d  for(i=1; bFound=
42c2b 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 49 6e 64 65  =0 && i<p->nInde
42c2c 78 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  x; i++){..      
42c2d 20 20 69 66 28 20 70 2d 3e 61 49 6e 64 65 78 5b    if( p->aIndex[
42c2e 69 5d 2e 6e 50 72 65 66 69 78 3d 3d 6e 54 65 72  i].nPrefix==nTer
42c2f 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  m ){..          
42c30 62 46 6f 75 6e 64 20 3d 20 31 3b 0d 0a 20 20 20  bFound = 1;..   
42c31 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
42c32 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
42c33 43 75 72 73 6f 72 28 0d 0a 20 20 20 20 20 20 20  Cursor(..       
42c34 20 20 20 20 20 20 20 70 2c 20 69 2c 20 46 54 53         p, i, FTS
42c35 33 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c 2c  3_SEGCURSOR_ALL,
42c36 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30   zTerm, nTerm, 0
42c37 2c 20 30 2c 20 70 53 65 67 63 73 72 29 3b 0d 0a  , 0, pSegcsr);..
42c38 20 20 20 20 20 20 20 20 20 20 70 53 65 67 63 73            pSegcs
42c39 72 2d 3e 62 4c 6f 6f 6b 75 70 20 3d 20 31 3b 0d  r->bLookup = 1;.
42c3a 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
42c3b 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 66 6f 72    }....      for
42c3c 28 69 3d 31 3b 20 62 46 6f 75 6e 64 3d 3d 30 20  (i=1; bFound==0 
42c3d 26 26 20 69 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20  && i<p->nIndex; 
42c3e 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  i++){..        i
42c3f 66 28 20 70 2d 3e 61 49 6e 64 65 78 5b 69 5d 2e  f( p->aIndex[i].
42c40 6e 50 72 65 66 69 78 3d 3d 6e 54 65 72 6d 2b 31  nPrefix==nTerm+1
42c41 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 62   ){..          b
42c42 46 6f 75 6e 64 20 3d 20 31 3b 0d 0a 20 20 20 20  Found = 1;..    
42c43 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
42c44 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 43  e3Fts3SegReaderC
42c45 75 72 73 6f 72 28 0d 0a 20 20 20 20 20 20 20 20  ursor(..        
42c46 20 20 20 20 20 20 70 2c 20 69 2c 20 46 54 53 33        p, i, FTS3
42c47 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c 2c 20  _SEGCURSOR_ALL, 
42c48 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 31 2c  zTerm, nTerm, 1,
42c49 20 30 2c 20 70 53 65 67 63 73 72 0d 0a 20 20 20   0, pSegcsr..   
42c4a 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20         );..     
42c4b 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
42c4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
42c4d 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
42c4e 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72 41  SegReaderCursorA
42c4f 64 64 5a 65 72 6f 28 70 2c 20 7a 54 65 72 6d 2c  ddZero(p, zTerm,
42c50 20 6e 54 65 72 6d 2c 20 70 53 65 67 63 73 72 29   nTerm, pSegcsr)
42c51 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
42c52 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
42c53 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20   }..    }....   
42c54 20 69 66 28 20 62 46 6f 75 6e 64 3d 3d 30 20 29   if( bFound==0 )
42c55 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  {..      rc = sq
42c56 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
42c57 65 72 43 75 72 73 6f 72 28 0d 0a 20 20 20 20 20  erCursor(..     
42c58 20 20 20 20 20 70 2c 20 30 2c 20 46 54 53 33 5f       p, 0, FTS3_
42c59 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c 2c 20 7a  SEGCURSOR_ALL, z
42c5a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
42c5b 72 65 66 69 78 2c 20 30 2c 20 70 53 65 67 63 73  refix, 0, pSegcs
42c5c 72 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20 20  r..      );..   
42c5d 20 20 20 70 53 65 67 63 73 72 2d 3e 62 4c 6f 6f     pSegcsr->bLoo
42c5e 6b 75 70 20 3d 20 21 69 73 50 72 65 66 69 78 3b  kup = !isPrefix;
42c5f 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
42c60 20 20 2a 70 70 53 65 67 63 73 72 20 3d 20 70 53    *ppSegcsr = pS
42c61 65 67 63 73 72 3b 0d 0a 20 20 72 65 74 75 72 6e  egcsr;..  return
42c62 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
42c63 2a 20 46 72 65 65 20 61 6e 20 46 74 73 33 4d 75  * Free an Fts3Mu
42c64 6c 74 69 53 65 67 52 65 61 64 65 72 20 61 6c 6c  ltiSegReader all
42c65 6f 63 61 74 65 64 20 62 79 20 66 74 73 33 54 65  ocated by fts3Te
42c66 72 6d 53 65 67 52 65 61 64 65 72 43 75 72 73 6f  rmSegReaderCurso
42c67 72 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  r()...*/..static
42c68 20 76 6f 69 64 20 66 74 73 33 53 65 67 52 65 61   void fts3SegRea
42c69 64 65 72 43 75 72 73 6f 72 46 72 65 65 28 46 74  derCursorFree(Ft
42c6a 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
42c6b 20 2a 70 53 65 67 63 73 72 29 7b 0d 0a 20 20 73   *pSegcsr){..  s
42c6c 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
42c6d 64 65 72 46 69 6e 69 73 68 28 70 53 65 67 63 73  derFinish(pSegcs
42c6e 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  r);..  sqlite3_f
42c6f 72 65 65 28 70 53 65 67 63 73 72 29 3b 0d 0a 7d  ree(pSegcsr);..}
42c70 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
42c71 66 75 6e 63 74 69 6f 6e 20 72 65 74 72 65 69 76  function retreiv
42c72 65 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66  es the doclist f
42c73 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
42c74 20 74 65 72 6d 20 28 6f 72 20 74 65 72 6d 0d 0a   term (or term..
42c75 2a 2a 20 70 72 65 66 69 78 29 20 66 72 6f 6d 20  ** prefix) from 
42c76 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a 2a  the database...*
42c77 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
42c78 73 33 54 65 72 6d 53 65 6c 65 63 74 28 0d 0a 20  s3TermSelect(.. 
42c79 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20   Fts3Table *p,  
42c7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c7b 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
42c7c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46  e handle */..  F
42c7d 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a  ts3PhraseToken *
42c7e 70 54 6f 6b 2c 20 20 20 20 20 20 20 20 20 20 2f  pTok,          /
42c7f 2a 20 54 6f 6b 65 6e 20 74 6f 20 71 75 65 72 79  * Token to query
42c80 20 66 6f 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 69   for */..  int i
42c81 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
42c82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
42c83 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79 20 28 6f  lumn to query (o
42c84 72 20 2d 76 65 20 66 6f 72 20 61 6c 6c 20 63 6f  r -ve for all co
42c85 6c 75 6d 6e 73 29 20 2a 2f 0d 0a 20 20 69 6e 74  lumns) */..  int
42c86 20 2a 70 6e 4f 75 74 2c 20 20 20 20 20 20 20 20   *pnOut,        
42c87 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42c88 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 62 75 66  OUT: Size of buf
42c89 66 65 72 20 61 74 20 2a 70 70 4f 75 74 20 2a 2f  fer at *ppOut */
42c8a 0d 0a 20 20 63 68 61 72 20 2a 2a 70 70 4f 75 74  ..  char **ppOut
42c8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c8c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 6c 6c      /* OUT: Mall
42c8d 6f 63 65 64 20 72 65 73 75 6c 74 20 62 75 66 66  oced result buff
42c8e 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  er */..){..  int
42c8f 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
42c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42c91 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
42c92 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65    Fts3MultiSegRe
42c93 61 64 65 72 20 2a 70 53 65 67 63 73 72 3b 20 20  ader *pSegcsr;  
42c94 20 20 2f 2a 20 53 65 67 2d 72 65 61 64 65 72 20    /* Seg-reader 
42c95 63 75 72 73 6f 72 20 66 6f 72 20 74 68 69 73 20  cursor for this 
42c96 74 65 72 6d 20 2a 2f 0d 0a 20 20 54 65 72 6d 53  term */..  TermS
42c97 65 6c 65 63 74 20 74 73 63 3b 20 20 20 20 20 20  elect tsc;      
42c98 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 62             /* Ob
42c99 6a 65 63 74 20 66 6f 72 20 70 61 69 72 2d 77 69  ject for pair-wi
42c9a 73 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69  se doclist mergi
42c9b 6e 67 20 2a 2f 0d 0a 20 20 46 74 73 33 53 65 67  ng */..  Fts3Seg
42c9c 46 69 6c 74 65 72 20 66 69 6c 74 65 72 3b 20 20  Filter filter;  
42c9d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d           /* Segm
42c9e 65 6e 74 20 74 65 72 6d 20 66 69 6c 74 65 72 20  ent term filter 
42c9f 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f  configuration */
42ca0 0d 0a 0d 0a 20 20 70 53 65 67 63 73 72 20 3d 20  ....  pSegcsr = 
42ca1 70 54 6f 6b 2d 3e 70 53 65 67 63 73 72 3b 0d 0a  pTok->pSegcsr;..
42ca2 20 20 6d 65 6d 73 65 74 28 26 74 73 63 2c 20 30    memset(&tsc, 0
42ca3 2c 20 73 69 7a 65 6f 66 28 54 65 72 6d 53 65 6c  , sizeof(TermSel
42ca4 65 63 74 29 29 3b 0d 0a 0d 0a 20 20 66 69 6c 74  ect));....  filt
42ca5 65 72 2e 66 6c 61 67 73 20 3d 20 46 54 53 33 5f  er.flags = FTS3_
42ca6 53 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45  SEGMENT_IGNORE_E
42ca7 4d 50 54 59 20 7c 20 46 54 53 33 5f 53 45 47 4d  MPTY | FTS3_SEGM
42ca8 45 4e 54 5f 52 45 51 55 49 52 45 5f 50 4f 53 0d  ENT_REQUIRE_POS.
42ca9 0a 20 20 20 20 20 20 20 20 7c 20 28 70 54 6f 6b  .        | (pTok
42caa 2d 3e 69 73 50 72 65 66 69 78 20 3f 20 46 54 53  ->isPrefix ? FTS
42cab 33 5f 53 45 47 4d 45 4e 54 5f 50 52 45 46 49 58  3_SEGMENT_PREFIX
42cac 20 3a 20 30 29 0d 0a 20 20 20 20 20 20 20 20 7c   : 0)..        |
42cad 20 28 70 54 6f 6b 2d 3e 62 46 69 72 73 74 20 3f   (pTok->bFirst ?
42cae 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 46 49   FTS3_SEGMENT_FI
42caf 52 53 54 20 3a 20 30 29 0d 0a 20 20 20 20 20 20  RST : 0)..      
42cb0 20 20 7c 20 28 69 43 6f 6c 75 6d 6e 3c 70 2d 3e    | (iColumn<p->
42cb1 6e 43 6f 6c 75 6d 6e 20 3f 20 46 54 53 33 5f 53  nColumn ? FTS3_S
42cb2 45 47 4d 45 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49  EGMENT_COLUMN_FI
42cb3 4c 54 45 52 20 3a 20 30 29 3b 0d 0a 20 20 66 69  LTER : 0);..  fi
42cb4 6c 74 65 72 2e 69 43 6f 6c 20 3d 20 69 43 6f 6c  lter.iCol = iCol
42cb5 75 6d 6e 3b 0d 0a 20 20 66 69 6c 74 65 72 2e 7a  umn;..  filter.z
42cb6 54 65 72 6d 20 3d 20 70 54 6f 6b 2d 3e 7a 3b 0d  Term = pTok->z;.
42cb7 0a 20 20 66 69 6c 74 65 72 2e 6e 54 65 72 6d 20  .  filter.nTerm 
42cb8 3d 20 70 54 6f 6b 2d 3e 6e 3b 0d 0a 0d 0a 20 20  = pTok->n;....  
42cb9 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
42cba 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28 70  SegReaderStart(p
42cbb 2c 20 70 53 65 67 63 73 72 2c 20 26 66 69 6c 74  , pSegcsr, &filt
42cbc 65 72 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 53  er);..  while( S
42cbd 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0d 0a 20 20  QLITE_OK==rc..  
42cbe 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 52 4f      && SQLITE_RO
42cbf 57 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  W==(rc = sqlite3
42cc0 46 74 73 33 53 65 67 52 65 61 64 65 72 53 74 65  Fts3SegReaderSte
42cc1 70 28 70 2c 20 70 53 65 67 63 73 72 29 29 20 0d  p(p, pSegcsr)) .
42cc2 0a 20 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  .  ){..    rc = 
42cc3 66 74 73 33 54 65 72 6d 53 65 6c 65 63 74 4d 65  fts3TermSelectMe
42cc4 72 67 65 28 70 2c 20 26 74 73 63 2c 20 70 53 65  rge(p, &tsc, pSe
42cc5 67 63 73 72 2d 3e 61 44 6f 63 6c 69 73 74 2c 20  gcsr->aDoclist, 
42cc6 70 53 65 67 63 73 72 2d 3e 6e 44 6f 63 6c 69 73  pSegcsr->nDoclis
42cc7 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  t);..  }....  if
42cc8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
42cc9 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ){..    rc = fts
42cca 33 54 65 72 6d 53 65 6c 65 63 74 46 69 6e 69 73  3TermSelectFinis
42ccb 68 4d 65 72 67 65 28 70 2c 20 26 74 73 63 29 3b  hMerge(p, &tsc);
42ccc 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d  ..  }..  if( rc=
42ccd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
42cce 20 20 20 2a 70 70 4f 75 74 20 3d 20 74 73 63 2e     *ppOut = tsc.
42ccf 61 61 4f 75 74 70 75 74 5b 30 5d 3b 0d 0a 20 20  aaOutput[0];..  
42cd0 20 20 2a 70 6e 4f 75 74 20 3d 20 74 73 63 2e 61    *pnOut = tsc.a
42cd1 6e 4f 75 74 70 75 74 5b 30 5d 3b 0d 0a 20 20 7d  nOutput[0];..  }
42cd2 65 6c 73 65 7b 0d 0a 20 20 20 20 69 6e 74 20 69  else{..    int i
42cd3 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
42cd4 69 3c 53 69 7a 65 6f 66 41 72 72 61 79 28 74 73  i<SizeofArray(ts
42cd5 63 2e 61 61 4f 75 74 70 75 74 29 3b 20 69 2b 2b  c.aaOutput); i++
42cd6 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
42cd7 33 5f 66 72 65 65 28 74 73 63 2e 61 61 4f 75 74  3_free(tsc.aaOut
42cd8 70 75 74 5b 69 5d 29 3b 0d 0a 20 20 20 20 7d 0d  put[i]);..    }.
42cd9 0a 20 20 7d 0d 0a 0d 0a 20 20 66 74 73 33 53 65  .  }....  fts3Se
42cda 67 52 65 61 64 65 72 43 75 72 73 6f 72 46 72 65  gReaderCursorFre
42cdb 65 28 70 53 65 67 63 73 72 29 3b 0d 0a 20 20 70  e(pSegcsr);..  p
42cdc 54 6f 6b 2d 3e 70 53 65 67 63 73 72 20 3d 20 30  Tok->pSegcsr = 0
42cdd 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
42cde 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
42cdf 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6e 74  s function count
42ce0 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
42ce1 65 72 20 6f 66 20 64 6f 63 69 64 73 20 69 6e 20  er of docids in 
42ce2 74 68 65 20 64 6f 63 6c 69 73 74 20 73 74 6f 72  the doclist stor
42ce3 65 64 0d 0a 2a 2a 20 69 6e 20 62 75 66 66 65 72  ed..** in buffer
42ce4 20 61 4c 69 73 74 5b 5d 2c 20 73 69 7a 65 20 6e   aList[], size n
42ce5 4c 69 73 74 20 62 79 74 65 73 2e 0d 0a 2a 2a 0d  List bytes...**.
42ce6 0a 2a 2a 20 49 66 20 74 68 65 20 69 73 50 6f 73  .** If the isPos
42ce7 6c 69 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  list argument is
42ce8 20 74 72 75 65 2c 20 74 68 65 6e 20 69 74 20 69   true, then it i
42ce9 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  s assumed that t
42cea 68 65 20 64 6f 63 6c 69 73 74 0d 0a 2a 2a 20 63  he doclist..** c
42ceb 6f 6e 74 61 69 6e 73 20 61 20 70 6f 73 69 74 69  ontains a positi
42cec 6f 6e 2d 6c 69 73 74 20 66 6f 6c 6c 6f 77 69 6e  on-list followin
42ced 67 20 65 61 63 68 20 64 6f 63 69 64 2e 20 4f 74  g each docid. Ot
42cee 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 20 61  herwise, it is a
42cef 73 73 75 6d 65 64 0d 0a 2a 2a 20 74 68 61 74 20  ssumed..** that 
42cf0 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73 20 73  the doclist is s
42cf1 69 6d 70 6c 79 20 61 20 6c 69 73 74 20 6f 66 20  imply a list of 
42cf2 64 6f 63 69 64 73 20 73 74 6f 72 65 64 20 61 73  docids stored as
42cf3 20 64 65 6c 74 61 20 65 6e 63 6f 64 65 64 20 0d   delta encoded .
42cf4 0a 2a 2a 20 76 61 72 69 6e 74 73 2e 0d 0a 2a 2f  .** varints...*/
42cf5 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
42cf6 33 44 6f 63 6c 69 73 74 43 6f 75 6e 74 44 6f 63  3DoclistCountDoc
42cf7 69 64 73 28 63 68 61 72 20 2a 61 4c 69 73 74 2c  ids(char *aList,
42cf8 20 69 6e 74 20 6e 4c 69 73 74 29 7b 0d 0a 20 20   int nList){..  
42cf9 69 6e 74 20 6e 44 6f 63 20 3d 20 30 3b 20 20 20  int nDoc = 0;   
42cfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cfb 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20  /* Return value 
42cfc 2a 2f 0d 0a 20 20 69 66 28 20 61 4c 69 73 74 20  */..  if( aList 
42cfd 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 61 45  ){..    char *aE
42cfe 6e 64 20 3d 20 26 61 4c 69 73 74 5b 6e 4c 69 73  nd = &aList[nLis
42cff 74 5d 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  t];   /* Pointer
42d00 20 74 6f 20 6f 6e 65 20 62 79 74 65 20 61 66 74   to one byte aft
42d01 65 72 20 45 4f 46 20 2a 2f 0d 0a 20 20 20 20 63  er EOF */..    c
42d02 68 61 72 20 2a 70 20 3d 20 61 4c 69 73 74 3b 20  har *p = aList; 
42d03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42d04 43 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 20 20 77  Cursor */..    w
42d05 68 69 6c 65 28 20 70 3c 61 45 6e 64 20 29 7b 0d  hile( p<aEnd ){.
42d06 0a 20 20 20 20 20 20 6e 44 6f 63 2b 2b 3b 0d 0a  .      nDoc++;..
42d07 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 2a 70        while( (*p
42d08 2b 2b 29 26 30 78 38 30 20 29 3b 20 20 20 20 20  ++)&0x80 );     
42d09 2f 2a 20 53 6b 69 70 20 64 6f 63 69 64 20 76 61  /* Skip docid va
42d0a 72 69 6e 74 20 2a 2f 0d 0a 20 20 20 20 20 20 66  rint */..      f
42d0b 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30  ts3PoslistCopy(0
42d0c 2c 20 26 70 29 3b 20 20 20 2f 2a 20 53 6b 69 70  , &p);   /* Skip
42d0d 20 6f 76 65 72 20 70 6f 73 69 74 69 6f 6e 20 6c   over position l
42d0e 69 73 74 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20  ist */..    }.. 
42d0f 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 6e   }....  return n
42d10 44 6f 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  Doc;..}..../*..*
42d11 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 75  * Advance the cu
42d12 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74  rsor to the next
42d13 20 72 6f 77 20 69 6e 20 74 68 65 20 25 5f 63 6f   row in the %_co
42d14 6e 74 65 6e 74 20 74 61 62 6c 65 20 74 68 61 74  ntent table that
42d15 0d 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65  ..** matches the
42d16 20 73 65 61 72 63 68 20 63 72 69 74 65 72 69 61   search criteria
42d17 2e 20 20 46 6f 72 20 61 20 4d 41 54 43 48 20 73  .  For a MATCH s
42d18 65 61 72 63 68 2c 20 74 68 69 73 20 77 69 6c 6c  earch, this will
42d19 20 62 65 0d 0a 2a 2a 20 74 68 65 20 6e 65 78 74   be..** the next
42d1a 20 72 6f 77 20 74 68 61 74 20 6d 61 74 63 68 65   row that matche
42d1b 73 2e 20 46 6f 72 20 61 20 66 75 6c 6c 2d 74 61  s. For a full-ta
42d1c 62 6c 65 20 73 63 61 6e 2c 20 74 68 69 73 20 77  ble scan, this w
42d1d 69 6c 6c 20 62 65 0d 0a 2a 2a 20 73 69 6d 70 6c  ill be..** simpl
42d1e 79 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69  y the next row i
42d1f 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20  n the %_content 
42d20 74 61 62 6c 65 2e 20 20 46 6f 72 20 61 20 64 6f  table.  For a do
42d21 63 69 64 20 6c 6f 6f 6b 75 70 2c 0d 0a 2a 2a 20  cid lookup,..** 
42d22 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 69 6d  this routine sim
42d23 70 6c 79 20 73 65 74 73 20 74 68 65 20 45 4f 46  ply sets the EOF
42d24 20 66 6c 61 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52   flag...**..** R
42d25 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
42d26 69 66 20 6e 6f 74 68 69 6e 67 20 67 6f 65 73 20  if nothing goes 
42d27 77 72 6f 6e 67 2e 20 20 53 51 4c 49 54 45 5f 4f  wrong.  SQLITE_O
42d28 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0d 0a 2a  K is returned..*
42d29 2a 20 65 76 65 6e 20 69 66 20 77 65 20 72 65 61  * even if we rea
42d2a 63 68 20 65 6e 64 2d 6f 66 2d 66 69 6c 65 2e 20  ch end-of-file. 
42d2b 20 54 68 65 20 66 74 73 33 45 6f 66 4d 65 74 68   The fts3EofMeth
42d2c 6f 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  od() will be cal
42d2d 6c 65 64 0d 0a 2a 2a 20 73 75 62 73 65 71 75 65  led..** subseque
42d2e 6e 74 6c 79 20 74 6f 20 64 65 74 65 72 6d 69 6e  ntly to determin
42d2f 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
42d30 20 61 6e 20 45 4f 46 20 77 61 73 20 68 69 74 2e   an EOF was hit.
42d31 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
42d32 20 66 74 73 33 4e 65 78 74 4d 65 74 68 6f 64 28   fts3NextMethod(
42d33 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
42d34 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0d 0a  sor *pCursor){..
42d35 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 46 74 73    int rc;..  Fts
42d36 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  3Cursor *pCsr = 
42d37 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43  (Fts3Cursor *)pC
42d38 75 72 73 6f 72 3b 0d 0a 20 20 69 66 28 20 70 43  ursor;..  if( pC
42d39 73 72 2d 3e 65 53 65 61 72 63 68 3d 3d 46 54 53  sr->eSearch==FTS
42d3a 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 20 7c  3_DOCID_SEARCH |
42d3b 7c 20 70 43 73 72 2d 3e 65 53 65 61 72 63 68 3d  | pCsr->eSearch=
42d3c 3d 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53  =FTS3_FULLSCAN_S
42d3d 45 41 52 43 48 20 29 7b 0d 0a 20 20 20 20 69 66  EARCH ){..    if
42d3e 28 20 53 51 4c 49 54 45 5f 52 4f 57 21 3d 73 71  ( SQLITE_ROW!=sq
42d3f 6c 69 74 65 33 5f 73 74 65 70 28 70 43 73 72 2d  lite3_step(pCsr-
42d40 3e 70 53 74 6d 74 29 20 29 7b 0d 0a 20 20 20 20  >pStmt) ){..    
42d41 20 20 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20    pCsr->isEof = 
42d42 31 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  1;..      rc = s
42d43 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 43 73  qlite3_reset(pCs
42d44 72 2d 3e 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20  r->pStmt);..    
42d45 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 70 43  }else{..      pC
42d46 73 72 2d 3e 69 50 72 65 76 49 64 20 3d 20 73 71  sr->iPrevId = sq
42d47 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
42d48 36 34 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20  64(pCsr->pStmt, 
42d49 30 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  0);..      rc = 
42d4a 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20  SQLITE_OK;..    
42d4b 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  }..  }else{..   
42d4c 20 72 63 20 3d 20 66 74 73 33 45 76 61 6c 4e 65   rc = fts3EvalNe
42d4d 78 74 28 28 46 74 73 33 43 75 72 73 6f 72 20 2a  xt((Fts3Cursor *
42d4e 29 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20 7d 0d  )pCursor);..  }.
42d4f 0a 20 20 61 73 73 65 72 74 28 20 28 28 46 74 73  .  assert( ((Fts
42d50 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62  3Table *)pCsr->b
42d51 61 73 65 2e 70 56 74 61 62 29 2d 3e 70 53 65 67  ase.pVtab)->pSeg
42d52 6d 65 6e 74 73 3d 3d 30 20 29 3b 0d 0a 20 20 72  ments==0 );..  r
42d53 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
42d54 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  /*..** This is t
42d55 68 65 20 78 46 69 6c 74 65 72 20 69 6e 74 65 72  he xFilter inter
42d56 66 61 63 65 20 66 6f 72 20 74 68 65 20 76 69 72  face for the vir
42d57 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 53 65 65  tual table.  See
42d58 0d 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c  ..** the virtual
42d59 20 74 61 62 6c 65 20 78 46 69 6c 74 65 72 20 6d   table xFilter m
42d5a 65 74 68 6f 64 20 64 6f 63 75 6d 65 6e 74 61 74  ethod documentat
42d5b 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ion for addition
42d5c 61 6c 0d 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  al..** informati
42d5d 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69  on...**..** If i
42d5e 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 46 55 4c 4c  dxNum==FTS3_FULL
42d5f 53 43 41 4e 5f 53 45 41 52 43 48 20 74 68 65 6e  SCAN_SEARCH then
42d60 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   do a full table
42d61 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0d 0a 2a   scan against..*
42d62 2a 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20  * the %_content 
42d63 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  table...**..** I
42d64 66 20 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 44  f idxNum==FTS3_D
42d65 4f 43 49 44 5f 53 45 41 52 43 48 20 74 68 65 6e  OCID_SEARCH then
42d66 20 64 6f 20 61 20 64 6f 63 69 64 20 6c 6f 6f 6b   do a docid look
42d67 75 70 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  up for a single 
42d68 65 6e 74 72 79 0d 0a 2a 2a 20 69 6e 20 74 68 65  entry..** in the
42d69 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
42d6a 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 69 64 78  ...**..** If idx
42d6b 4e 75 6d 3e 3d 46 54 53 33 5f 46 55 4c 4c 54 45  Num>=FTS3_FULLTE
42d6c 58 54 5f 53 45 41 52 43 48 20 74 68 65 6e 20 75  XT_SEARCH then u
42d6d 73 65 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74  se the full text
42d6e 20 69 6e 64 65 78 2e 20 20 54 68 65 0d 0a 2a 2a   index.  The..**
42d6f 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c   column on the l
42d70 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
42d71 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61   the MATCH opera
42d72 74 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0d 0a 2a  tor is column..*
42d73 2a 20 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d 2d  * number idxNum-
42d74 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
42d75 41 52 43 48 2c 20 30 20 69 6e 64 65 78 65 64 2e  ARCH, 0 indexed.
42d76 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65    argv[0] is the
42d77 20 72 69 67 68 74 2d 68 61 6e 64 0d 0a 2a 2a 20   right-hand..** 
42d78 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
42d79 48 20 6f 70 65 72 61 74 6f 72 2e 0d 0a 2a 2f 0d  H operator...*/.
42d7a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
42d7b 46 69 6c 74 65 72 4d 65 74 68 6f 64 28 0d 0a 20  FilterMethod(.. 
42d7c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
42d7d 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
42d7e 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 75   /* The cursor u
42d7f 73 65 64 20 66 6f 72 20 74 68 69 73 20 71 75 65  sed for this que
42d80 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 64 78  ry */..  int idx
42d81 4e 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  Num,            
42d82 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 61           /* Stra
42d83 74 65 67 79 20 69 6e 64 65 78 20 2a 2f 0d 0a 20  tegy index */.. 
42d84 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78   const char *idx
42d85 53 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Str,            
42d86 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0d 0a 20   /* Unused */.. 
42d87 20 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20   int nVal,      
42d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42d89 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
42d8a 65 6d 65 6e 74 73 20 69 6e 20 61 70 56 61 6c 20  ements in apVal 
42d8b 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  */..  sqlite3_va
42d8c 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20  lue **apVal     
42d8d 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
42d8e 74 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  ts for the index
42d8f 69 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0d 0a 29  ing scheme */..)
42d90 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  {..  int rc;..  
42d91 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20  char *zSql;     
42d92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42d93 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  /* SQL statement
42d94 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
42d95 25 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0d 0a 20 20  %_content */..  
42d96 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28  Fts3Table *p = (
42d97 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72  Fts3Table *)pCur
42d98 73 6f 72 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 46  sor->pVtab;..  F
42d99 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20  ts3Cursor *pCsr 
42d9a 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29  = (Fts3Cursor *)
42d9b 70 43 75 72 73 6f 72 3b 0d 0a 0d 0a 20 20 55 4e  pCursor;....  UN
42d9c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
42d9d 64 78 53 74 72 29 3b 0d 0a 20 20 55 4e 55 53 45  dxStr);..  UNUSE
42d9e 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 56 61 6c  D_PARAMETER(nVal
42d9f 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  );....  assert( 
42da0 69 64 78 4e 75 6d 3e 3d 30 20 26 26 20 69 64 78  idxNum>=0 && idx
42da1 4e 75 6d 3c 3d 28 46 54 53 33 5f 46 55 4c 4c 54  Num<=(FTS3_FULLT
42da2 45 58 54 5f 53 45 41 52 43 48 2b 70 2d 3e 6e 43  EXT_SEARCH+p->nC
42da3 6f 6c 75 6d 6e 29 20 29 3b 0d 0a 20 20 61 73 73  olumn) );..  ass
42da4 65 72 74 28 20 6e 56 61 6c 3d 3d 30 20 7c 7c 20  ert( nVal==0 || 
42da5 6e 56 61 6c 3d 3d 31 20 29 3b 0d 0a 20 20 61 73  nVal==1 );..  as
42da6 73 65 72 74 28 20 28 6e 56 61 6c 3d 3d 30 29 3d  sert( (nVal==0)=
42da7 3d 28 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 46  =(idxNum==FTS3_F
42da8 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 29 20  ULLSCAN_SEARCH) 
42da9 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  );..  assert( p-
42daa 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20 29 3b  >pSegments==0 );
42dab 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 20 63 61 73 65  ....  /* In case
42dac 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
42dad 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65  been used before
42dae 2c 20 63 6c 65 61 72 20 69 74 20 6e 6f 77 2e 20  , clear it now. 
42daf 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  */..  sqlite3_fi
42db0 6e 61 6c 69 7a 65 28 70 43 73 72 2d 3e 70 53 74  nalize(pCsr->pSt
42db1 6d 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  mt);..  sqlite3_
42db2 66 72 65 65 28 70 43 73 72 2d 3e 61 44 6f 63 6c  free(pCsr->aDocl
42db3 69 73 74 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ist);..  sqlite3
42db4 46 74 73 33 45 78 70 72 46 72 65 65 28 70 43 73  Fts3ExprFree(pCs
42db5 72 2d 3e 70 45 78 70 72 29 3b 0d 0a 20 20 6d 65  r->pExpr);..  me
42db6 6d 73 65 74 28 26 70 43 75 72 73 6f 72 5b 31 5d  mset(&pCursor[1]
42db7 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  , 0, sizeof(Fts3
42db8 43 75 72 73 6f 72 29 2d 73 69 7a 65 6f 66 28 73  Cursor)-sizeof(s
42db9 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
42dba 6f 72 29 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 69  or));....  if( i
42dbb 64 78 53 74 72 20 29 7b 0d 0a 20 20 20 20 70 43  dxStr ){..    pC
42dbc 73 72 2d 3e 62 44 65 73 63 20 3d 20 28 69 64 78  sr->bDesc = (idx
42dbd 53 74 72 5b 30 5d 3d 3d 27 44 27 29 3b 0d 0a 20  Str[0]=='D');.. 
42dbe 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 43 73   }else{..    pCs
42dbf 72 2d 3e 62 44 65 73 63 20 3d 20 70 2d 3e 62 44  r->bDesc = p->bD
42dc0 65 73 63 49 64 78 3b 0d 0a 20 20 7d 0d 0a 20 20  escIdx;..  }..  
42dc1 70 43 73 72 2d 3e 65 53 65 61 72 63 68 20 3d 20  pCsr->eSearch = 
42dc2 28 69 31 36 29 69 64 78 4e 75 6d 3b 0d 0a 0d 0a  (i16)idxNum;....
42dc3 20 20 69 66 28 20 69 64 78 4e 75 6d 21 3d 46 54    if( idxNum!=FT
42dc4 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 20  S3_DOCID_SEARCH 
42dc5 26 26 20 69 64 78 4e 75 6d 21 3d 46 54 53 33 5f  && idxNum!=FTS3_
42dc6 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 20  FULLSCAN_SEARCH 
42dc7 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c  ){..    int iCol
42dc8 20 3d 20 69 64 78 4e 75 6d 2d 46 54 53 33 5f 46   = idxNum-FTS3_F
42dc9 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 3b 0d  ULLTEXT_SEARCH;.
42dca 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
42dcb 2a 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e 73 74  *zQuery = (const
42dcc 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
42dcd 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c  value_text(apVal
42dce 5b 30 5d 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28  [0]);....    if(
42dcf 20 7a 51 75 65 72 79 3d 3d 30 20 26 26 20 73 71   zQuery==0 && sq
42dd0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
42dd1 28 61 70 56 61 6c 5b 30 5d 29 21 3d 53 51 4c 49  (apVal[0])!=SQLI
42dd2 54 45 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20  TE_NULL ){..    
42dd3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
42dd4 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  NOMEM;..    }...
42dd5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
42dd6 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28 70  3Fts3ExprParse(p
42dd7 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 70 2d  ->pTokenizer, p-
42dd8 3e 61 7a 43 6f 6c 75 6d 6e 2c 20 70 2d 3e 62 48  >azColumn, p->bH
42dd9 61 73 53 74 61 74 2c 20 0d 0a 20 20 20 20 20 20  asStat, ..      
42dda 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 43    p->nColumn, iC
42ddb 6f 6c 2c 20 7a 51 75 65 72 79 2c 20 2d 31 2c 20  ol, zQuery, -1, 
42ddc 26 70 43 73 72 2d 3e 70 45 78 70 72 0d 0a 20 20  &pCsr->pExpr..  
42ddd 20 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63    );..    if( rc
42dde 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
42ddf 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
42de0 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0d 0a 20  LITE_ERROR ){.. 
42de1 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
42de2 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  nst char *zErr =
42de3 20 22 6d 61 6c 66 6f 72 6d 65 64 20 4d 41 54 43   "malformed MATC
42de4 48 20 65 78 70 72 65 73 73 69 6f 6e 3a 20 5b 25  H expression: [%
42de5 73 5d 22 3b 0d 0a 20 20 20 20 20 20 20 20 70 2d  s]";..        p-
42de6 3e 62 61 73 65 2e 7a 45 72 72 4d 73 67 20 3d 20  >base.zErrMsg = 
42de7 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
42de8 7a 45 72 72 2c 20 7a 51 75 65 72 79 29 3b 0d 0a  zErr, zQuery);..
42de9 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72        }..      r
42dea 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d  eturn rc;..    }
42deb 0d 0a 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ....    rc = sql
42dec 69 74 65 33 46 74 73 33 52 65 61 64 4c 6f 63 6b  ite3Fts3ReadLock
42ded 28 70 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  (p);..    if( rc
42dee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
42def 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20 20 20 20  turn rc;....    
42df0 72 63 20 3d 20 66 74 73 33 45 76 61 6c 53 74 61  rc = fts3EvalSta
42df1 72 74 28 70 43 73 72 29 3b 0d 0a 0d 0a 20 20 20  rt(pCsr);....   
42df2 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 6d   sqlite3Fts3Segm
42df3 65 6e 74 73 43 6c 6f 73 65 28 70 29 3b 0d 0a 20  entsClose(p);.. 
42df4 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
42df5 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
42df6 3b 0d 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 65  ;..    pCsr->pNe
42df7 78 74 49 64 20 3d 20 70 43 73 72 2d 3e 61 44 6f  xtId = pCsr->aDo
42df8 63 6c 69 73 74 3b 0d 0a 20 20 20 20 70 43 73 72  clist;..    pCsr
42df9 2d 3e 69 50 72 65 76 49 64 20 3d 20 30 3b 0d 0a  ->iPrevId = 0;..
42dfa 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43 6f 6d 70    }....  /* Comp
42dfb 69 6c 65 20 61 20 53 45 4c 45 43 54 20 73 74 61  ile a SELECT sta
42dfc 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 69 73 20  tement for this 
42dfd 63 75 72 73 6f 72 2e 20 46 6f 72 20 61 20 66 75  cursor. For a fu
42dfe 6c 6c 2d 74 61 62 6c 65 2d 73 63 61 6e 2c 20 74  ll-table-scan, t
42dff 68 65 0d 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  he..  ** stateme
42e00 6e 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  nt loops through
42e01 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20 74 68 65   all rows of the
42e02 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
42e03 2e 20 46 6f 72 20 61 0d 0a 20 20 2a 2a 20 66 75  . For a..  ** fu
42e04 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 6f 72  ll-text query or
42e05 20 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 2c 20 74   docid lookup, t
42e06 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  he statement ret
42e07 72 69 65 76 65 73 20 61 20 73 69 6e 67 6c 65 0d  rieves a single.
42e08 0a 20 20 2a 2a 20 72 6f 77 20 62 79 20 64 6f 63  .  ** row by doc
42e09 69 64 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  id...  */..  if(
42e0a 20 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 46 55   idxNum==FTS3_FU
42e0b 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 20 29 7b  LLSCAN_SEARCH ){
42e0c 0d 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c  ..    zSql = sql
42e0d 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a 20  ite3_mprintf(.. 
42e0e 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 25         "SELECT %
42e0f 73 20 4f 52 44 45 52 20 42 59 20 72 6f 77 69 64  s ORDER BY rowid
42e10 20 25 73 22 2c 0d 0a 20 20 20 20 20 20 20 20 70   %s",..        p
42e11 2d 3e 7a 52 65 61 64 45 78 70 72 6c 69 73 74 2c  ->zReadExprlist,
42e12 20 28 70 43 73 72 2d 3e 62 44 65 73 63 20 3f 20   (pCsr->bDesc ? 
42e13 22 44 45 53 43 22 20 3a 20 22 41 53 43 22 29 0d  "DESC" : "ASC").
42e14 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20 69 66 28  .    );..    if(
42e15 20 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20 20   zSql ){..      
42e16 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
42e17 70 61 72 65 5f 76 32 28 70 2d 3e 64 62 2c 20 7a  pare_v2(p->db, z
42e18 53 71 6c 2c 20 2d 31 2c 20 26 70 43 73 72 2d 3e  Sql, -1, &pCsr->
42e19 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20 20  pStmt, 0);..    
42e1a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
42e1b 53 71 6c 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65  Sql);..    }else
42e1c 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  {..      rc = SQ
42e1d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
42e1e 20 7d 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   }..  }else if( 
42e1f 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 44 4f 43  idxNum==FTS3_DOC
42e20 49 44 5f 53 45 41 52 43 48 20 29 7b 0d 0a 20 20  ID_SEARCH ){..  
42e21 20 20 72 63 20 3d 20 66 74 73 33 43 75 72 73 6f    rc = fts3Curso
42e22 72 53 65 65 6b 53 74 6d 74 28 70 43 73 72 2c 20  rSeekStmt(pCsr, 
42e23 26 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0d 0a  &pCsr->pStmt);..
42e24 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
42e25 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
42e26 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
42e27 64 5f 76 61 6c 75 65 28 70 43 73 72 2d 3e 70 53  d_value(pCsr->pS
42e28 74 6d 74 2c 20 31 2c 20 61 70 56 61 6c 5b 30 5d  tmt, 1, apVal[0]
42e29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  );..    }..  }..
42e2a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
42e2b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
42e2c 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 66 74 73  ....  return fts
42e2d 33 4e 65 78 74 4d 65 74 68 6f 64 28 70 43 75 72  3NextMethod(pCur
42e2e 73 6f 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  sor);..}..../* .
42e2f 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
42e30 78 45 6f 66 20 6d 65 74 68 6f 64 20 6f 66 20 74  xEof method of t
42e31 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
42e32 2e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  . SQLite calls t
42e33 68 69 73 20 0d 0a 2a 2a 20 72 6f 75 74 69 6e 65  his ..** routine
42e34 20 74 6f 20 66 69 6e 64 20 6f 75 74 20 69 66 20   to find out if 
42e35 69 74 20 68 61 73 20 72 65 61 63 68 65 64 20 74  it has reached t
42e36 68 65 20 65 6e 64 20 6f 66 20 61 20 72 65 73 75  he end of a resu
42e37 6c 74 20 73 65 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  lt set...*/..sta
42e38 74 69 63 20 69 6e 74 20 66 74 73 33 45 6f 66 4d  tic int fts3EofM
42e39 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74  ethod(sqlite3_vt
42e3a 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
42e3b 6f 72 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 28  or){..  return (
42e3c 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43  (Fts3Cursor *)pC
42e3d 75 72 73 6f 72 29 2d 3e 69 73 45 6f 66 3b 0d 0a  ursor)->isEof;..
42e3e 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 69  }..../* ..** Thi
42e3f 73 20 69 73 20 74 68 65 20 78 52 6f 77 69 64 20  s is the xRowid 
42e40 6d 65 74 68 6f 64 2e 20 54 68 65 20 53 51 4c 69  method. The SQLi
42e41 74 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74 68  te core calls th
42e42 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0d 0a 2a  is routine to..*
42e43 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  * retrieve the r
42e44 6f 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72  owid for the cur
42e45 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
42e46 72 65 73 75 6c 74 20 73 65 74 2e 20 66 74 73 33  result set. fts3
42e47 0d 0a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63  ..** exposes %_c
42e48 6f 6e 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20  ontent.docid as 
42e49 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
42e4a 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
42e4b 20 54 68 65 0d 0a 2a 2a 20 72 6f 77 69 64 20 73   The..** rowid s
42e4c 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
42e4d 20 74 6f 20 2a 70 52 6f 77 69 64 2e 0d 0a 2a 2f   to *pRowid...*/
42e4e 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
42e4f 33 52 6f 77 69 64 4d 65 74 68 6f 64 28 73 71 6c  3RowidMethod(sql
42e50 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
42e51 20 2a 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74   *pCursor, sqlit
42e52 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 29  e_int64 *pRowid)
42e53 7b 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  {..  Fts3Cursor 
42e54 2a 70 43 73 72 20 3d 20 28 46 74 73 33 43 75 72  *pCsr = (Fts3Cur
42e55 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0d  sor *) pCursor;.
42e56 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 43 73  .  *pRowid = pCs
42e57 72 2d 3e 69 50 72 65 76 49 64 3b 0d 0a 20 20 72  r->iPrevId;..  r
42e58 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
42e59 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54  ..}..../* ..** T
42e5a 68 69 73 20 69 73 20 74 68 65 20 78 43 6f 6c 75  his is the xColu
42e5b 6d 6e 20 6d 65 74 68 6f 64 2c 20 63 61 6c 6c 65  mn method, calle
42e5c 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 72  d by SQLite to r
42e5d 65 71 75 65 73 74 20 61 20 76 61 6c 75 65 20 66  equest a value f
42e5e 72 6f 6d 0d 0a 2a 2a 20 74 68 65 20 72 6f 77 20  rom..** the row 
42e5f 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
42e60 64 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74  d cursor current
42e61 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0d 0a 2a  ly points to...*
42e62 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
42e63 73 33 43 6f 6c 75 6d 6e 4d 65 74 68 6f 64 28 0d  s3ColumnMethod(.
42e64 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
42e65 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
42e66 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20     /* Cursor to 
42e67 72 65 74 72 69 65 76 65 20 76 61 6c 75 65 20 66  retrieve value f
42e68 72 6f 6d 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  rom */..  sqlite
42e69 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
42e6a 65 78 74 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ext,      /* Con
42e6b 74 65 78 74 20 66 6f 72 20 73 71 6c 69 74 65 33  text for sqlite3
42e6c 5f 72 65 73 75 6c 74 5f 78 78 78 28 29 20 63 61  _result_xxx() ca
42e6d 6c 6c 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43  lls */..  int iC
42e6e 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ol              
42e6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
42e70 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 74 6f 20  ex of column to 
42e71 72 65 61 64 20 76 61 6c 75 65 20 66 72 6f 6d 20  read value from 
42e72 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
42e73 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
42e74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
42e75 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a 20 20 46  urn Code */..  F
42e76 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20  ts3Cursor *pCsr 
42e77 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29  = (Fts3Cursor *)
42e78 20 70 43 75 72 73 6f 72 3b 0d 0a 20 20 46 74 73   pCursor;..  Fts
42e79 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73  3Table *p = (Fts
42e7a 33 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72  3Table *)pCursor
42e7b 2d 3e 70 56 74 61 62 3b 0d 0a 0d 0a 20 20 2f 2a  ->pVtab;....  /*
42e7c 20 54 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   The column valu
42e7d 65 20 73 75 70 70 6c 69 65 64 20 62 79 20 53 51  e supplied by SQ
42e7e 4c 69 74 65 20 6d 75 73 74 20 62 65 20 69 6e 20  Lite must be in 
42e7f 72 61 6e 67 65 2e 20 2a 2f 0d 0a 20 20 61 73 73  range. */..  ass
42e80 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20  ert( iCol>=0 && 
42e81 69 43 6f 6c 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e  iCol<=p->nColumn
42e82 2b 31 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 69  +1 );....  if( i
42e83 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b  Col==p->nColumn+
42e84 31 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 69  1 ){..    /* Thi
42e85 73 20 63 61 6c 6c 20 69 73 20 61 20 72 65 71 75  s call is a requ
42e86 65 73 74 20 66 6f 72 20 74 68 65 20 22 64 6f 63  est for the "doc
42e87 69 64 22 20 63 6f 6c 75 6d 6e 2e 20 53 69 6e 63  id" column. Sinc
42e88 65 20 22 64 6f 63 69 64 22 20 69 73 20 61 6e 20  e "docid" is an 
42e89 0d 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 20 66  ..    ** alias f
42e8a 6f 72 20 22 72 6f 77 69 64 22 2c 20 75 73 65 20  or "rowid", use 
42e8b 74 68 65 20 78 52 6f 77 69 64 28 29 20 6d 65 74  the xRowid() met
42e8c 68 6f 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  hod to obtain th
42e8d 65 20 76 61 6c 75 65 2e 0d 0a 20 20 20 20 2a 2f  e value...    */
42e8e 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
42e8f 73 75 6c 74 5f 69 6e 74 36 34 28 70 43 6f 6e 74  sult_int64(pCont
42e90 65 78 74 2c 20 70 43 73 72 2d 3e 69 50 72 65 76  ext, pCsr->iPrev
42e91 49 64 29 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66  Id);..  }else if
42e92 28 20 69 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75  ( iCol==p->nColu
42e93 6d 6e 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  mn ){..    /* Th
42e94 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20 77  e extra column w
42e95 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68 65  hose name is the
42e96 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61 62   same as the tab
42e97 6c 65 2e 0d 0a 20 20 20 20 2a 2a 20 52 65 74 75  le...    ** Retu
42e98 72 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20  rn a blob which 
42e99 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
42e9a 74 68 65 20 63 75 72 73 6f 72 2e 0d 0a 20 20 20  the cursor...   
42e9b 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   */..    sqlite3
42e9c 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f  _result_blob(pCo
42e9d 6e 74 65 78 74 2c 20 26 70 43 73 72 2c 20 73 69  ntext, &pCsr, si
42e9e 7a 65 6f 66 28 70 43 73 72 29 2c 20 53 51 4c 49  zeof(pCsr), SQLI
42e9f 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a  TE_TRANSIENT);..
42ea0 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63    }else{..    rc
42ea1 20 3d 20 66 74 73 33 43 75 72 73 6f 72 53 65 65   = fts3CursorSee
42ea2 6b 28 30 2c 20 70 43 73 72 29 3b 0d 0a 20 20 20  k(0, pCsr);..   
42ea3 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
42ea4 4f 4b 20 26 26 20 73 71 6c 69 74 65 33 5f 64 61  OK && sqlite3_da
42ea5 74 61 5f 63 6f 75 6e 74 28 70 43 73 72 2d 3e 70  ta_count(pCsr->p
42ea6 53 74 6d 74 29 3e 28 69 43 6f 6c 2b 31 29 20 29  Stmt)>(iCol+1) )
42ea7 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
42ea8 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
42ea9 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
42eaa 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 70 43 73  column_value(pCs
42eab 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31  r->pStmt, iCol+1
42eac 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ));..    }..  }.
42ead 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 28 28 46  ...  assert( ((F
42eae 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d  ts3Table *)pCsr-
42eaf 3e 62 61 73 65 2e 70 56 74 61 62 29 2d 3e 70 53  >base.pVtab)->pS
42eb0 65 67 6d 65 6e 74 73 3d 3d 30 20 29 3b 0d 0a 20  egments==0 );.. 
42eb1 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
42eb2 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 66  ../* ..** This f
42eb3 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69  unction is the i
42eb4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
42eb5 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c   the xUpdate cal
42eb6 6c 62 61 63 6b 20 75 73 65 64 20 62 79 20 0d 0a  lback used by ..
42eb7 2a 2a 20 46 54 53 33 20 76 69 72 74 75 61 6c 20  ** FTS3 virtual 
42eb8 74 61 62 6c 65 73 2e 20 49 74 20 69 73 20 69 6e  tables. It is in
42eb9 76 6f 6b 65 64 20 62 79 20 53 51 4c 69 74 65 20  voked by SQLite 
42eba 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f 77 20  each time a row 
42ebb 69 73 20 74 6f 20 62 65 0d 0a 2a 2a 20 69 6e 73  is to be..** ins
42ebc 65 72 74 65 64 2c 20 75 70 64 61 74 65 64 20 6f  erted, updated o
42ebd 72 20 64 65 6c 65 74 65 64 2e 0d 0a 2a 2f 0d 0a  r deleted...*/..
42ebe 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 55  static int fts3U
42ebf 70 64 61 74 65 4d 65 74 68 6f 64 28 0d 0a 20 20  pdateMethod(..  
42ec0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
42ec1 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tab,            
42ec2 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
42ec3 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e   handle */..  in
42ec4 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
42ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42ec6 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e   Size of argumen
42ec7 74 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 73 71  t array */..  sq
42ec8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
42ec9 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Val,          /*
42eca 20 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d 65   Array of argume
42ecb 6e 74 73 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  nts */..  sqlite
42ecc 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 20 20  _int64 *pRowid  
42ecd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
42ece 3a 20 54 68 65 20 61 66 66 65 63 74 65 64 20 28  : The affected (
42ecf 6f 72 20 65 66 66 65 63 74 65 64 29 20 72 6f 77  or effected) row
42ed0 69 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 72 65 74  id */..){..  ret
42ed1 75 72 6e 20 73 71 6c 69 74 65 33 46 74 73 33 55  urn sqlite3Fts3U
42ed2 70 64 61 74 65 4d 65 74 68 6f 64 28 70 56 74 61  pdateMethod(pVta
42ed3 62 2c 20 6e 41 72 67 2c 20 61 70 56 61 6c 2c 20  b, nArg, apVal, 
42ed4 70 52 6f 77 69 64 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  pRowid);..}..../
42ed5 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  *..** Implementa
42ed6 74 69 6f 6e 20 6f 66 20 78 53 79 6e 63 28 29 20  tion of xSync() 
42ed7 6d 65 74 68 6f 64 2e 20 46 6c 75 73 68 20 74 68  method. Flush th
42ed8 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
42ed9 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 0d  e pending-terms.
42eda 0a 2a 2a 20 68 61 73 68 2d 74 61 62 6c 65 20 74  .** hash-table t
42edb 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d  o the database..
42edc 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
42edd 66 74 73 33 53 79 6e 63 4d 65 74 68 6f 64 28 73  fts3SyncMethod(s
42ede 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
42edf 61 62 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  ab){..  int rc =
42ee0 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
42ee1 69 6e 67 54 65 72 6d 73 46 6c 75 73 68 28 28 46  ingTermsFlush((F
42ee2 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62  ts3Table *)pVtab
42ee3 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 46 74 73  );..  sqlite3Fts
42ee4 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 28  3SegmentsClose((
42ee5 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61  Fts3Table *)pVta
42ee6 62 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  b);..  return rc
42ee7 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
42ee8 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
42ee9 20 78 42 65 67 69 6e 28 29 20 6d 65 74 68 6f 64   xBegin() method
42eea 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  . This is a no-o
42eeb 70 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  p...*/..static i
42eec 6e 74 20 66 74 73 33 42 65 67 69 6e 4d 65 74 68  nt fts3BeginMeth
42eed 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  od(sqlite3_vtab 
42eee 2a 70 56 74 61 62 29 7b 0d 0a 20 20 54 45 53 54  *pVtab){..  TEST
42eef 4f 4e 4c 59 28 20 46 74 73 33 54 61 62 6c 65 20  ONLY( Fts3Table 
42ef0 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65 2a  *p = (Fts3Table*
42ef1 29 70 56 74 61 62 20 29 3b 0d 0a 20 20 55 4e 55  )pVtab );..  UNU
42ef2 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
42ef3 74 61 62 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  tab);..  assert(
42ef4 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30   p->pSegments==0
42ef5 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
42ef6 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d  ->nPendingData==
42ef7 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
42ef8 70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  p->inTransaction
42ef9 21 3d 31 20 29 3b 0d 0a 20 20 54 45 53 54 4f 4e  !=1 );..  TESTON
42efa 4c 59 28 20 70 2d 3e 69 6e 54 72 61 6e 73 61 63  LY( p->inTransac
42efb 74 69 6f 6e 20 3d 20 31 20 29 3b 0d 0a 20 20 54  tion = 1 );..  T
42efc 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 6d 78 53 61  ESTONLY( p->mxSa
42efd 76 65 70 6f 69 6e 74 20 3d 20 2d 31 3b 20 29 3b  vepoint = -1; );
42efe 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
42eff 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
42f00 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
42f01 6e 20 6f 66 20 78 43 6f 6d 6d 69 74 28 29 20 6d  n of xCommit() m
42f02 65 74 68 6f 64 2e 20 54 68 69 73 20 69 73 20 61  ethod. This is a
42f03 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 63 6f 6e 74   no-op. The cont
42f04 65 6e 74 73 20 6f 66 0d 0a 2a 2a 20 74 68 65 20  ents of..** the 
42f05 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61  pending-terms ha
42f06 73 68 2d 74 61 62 6c 65 20 68 61 76 65 20 61 6c  sh-table have al
42f07 72 65 61 64 79 20 62 65 65 6e 20 66 6c 75 73 68  ready been flush
42f08 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ed into the data
42f09 62 61 73 65 0d 0a 2a 2a 20 62 79 20 66 74 73 33  base..** by fts3
42f0a 53 79 6e 63 4d 65 74 68 6f 64 28 29 2e 0d 0a 2a  SyncMethod()...*
42f0b 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
42f0c 73 33 43 6f 6d 6d 69 74 4d 65 74 68 6f 64 28 73  s3CommitMethod(s
42f0d 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
42f0e 61 62 29 7b 0d 0a 20 20 54 45 53 54 4f 4e 4c 59  ab){..  TESTONLY
42f0f 28 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d  ( Fts3Table *p =
42f10 20 28 46 74 73 33 54 61 62 6c 65 2a 29 70 56 74   (Fts3Table*)pVt
42f11 61 62 20 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f  ab );..  UNUSED_
42f12 50 41 52 41 4d 45 54 45 52 28 70 56 74 61 62 29  PARAMETER(pVtab)
42f13 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
42f14 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20  nPendingData==0 
42f15 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  );..  assert( p-
42f16 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
42f17 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
42f18 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20  p->pSegments==0 
42f19 29 3b 0d 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20  );..  TESTONLY( 
42f1a 70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  p->inTransaction
42f1b 20 3d 20 30 20 29 3b 0d 0a 20 20 54 45 53 54 4f   = 0 );..  TESTO
42f1c 4e 4c 59 28 20 70 2d 3e 6d 78 53 61 76 65 70 6f  NLY( p->mxSavepo
42f1d 69 6e 74 20 3d 20 2d 31 3b 20 29 3b 0d 0a 20 20  int = -1; );..  
42f1e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
42f1f 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
42f20 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
42f21 20 78 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 44 69   xRollback(). Di
42f22 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
42f23 74 73 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e  ts of the pendin
42f24 67 2d 74 65 72 6d 73 0d 0a 2a 2a 20 68 61 73 68  g-terms..** hash
42f25 2d 74 61 62 6c 65 2e 20 41 6e 79 20 63 68 61 6e  -table. Any chan
42f26 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20  ges made to the 
42f27 64 61 74 61 62 61 73 65 20 61 72 65 20 72 65 76  database are rev
42f28 65 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  erted by SQLite.
42f29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
42f2a 20 66 74 73 33 52 6f 6c 6c 62 61 63 6b 4d 65 74   fts3RollbackMet
42f2b 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
42f2c 20 2a 70 56 74 61 62 29 7b 0d 0a 20 20 46 74 73   *pVtab){..  Fts
42f2d 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73  3Table *p = (Fts
42f2e 33 54 61 62 6c 65 2a 29 70 56 74 61 62 3b 0d 0a  3Table*)pVtab;..
42f2f 20 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e    sqlite3Fts3Pen
42f30 64 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 70  dingTermsClear(p
42f31 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d  );..  assert( p-
42f32 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
42f33 30 20 29 3b 0d 0a 20 20 54 45 53 54 4f 4e 4c 59  0 );..  TESTONLY
42f34 28 20 70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  ( p->inTransacti
42f35 6f 6e 20 3d 20 30 20 29 3b 0d 0a 20 20 54 45 53  on = 0 );..  TES
42f36 54 4f 4e 4c 59 28 20 70 2d 3e 6d 78 53 61 76 65  TONLY( p->mxSave
42f37 70 6f 69 6e 74 20 3d 20 2d 31 3b 20 29 3b 0d 0a  point = -1; );..
42f38 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
42f39 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
42f3a 20 57 68 65 6e 20 63 61 6c 6c 65 64 2c 20 2a 70   When called, *p
42f3b 70 50 6f 73 6c 69 73 74 20 6d 75 73 74 20 70 6f  pPoslist must po
42f3c 69 6e 74 20 74 6f 20 74 68 65 20 62 79 74 65 20  int to the byte 
42f3d 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
42f3e 6f 77 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 65 6e  owing the..** en
42f3f 64 20 6f 66 20 61 20 70 6f 73 69 74 69 6f 6e 2d  d of a position-
42f40 6c 69 73 74 2e 20 69 2e 65 2e 20 28 20 28 2a 70  list. i.e. ( (*p
42f41 70 50 6f 73 6c 69 73 74 29 5b 2d 31 5d 3d 3d 50  pPoslist)[-1]==P
42f42 4f 53 5f 45 4e 44 20 29 2e 20 54 68 69 73 20 66  OS_END ). This f
42f43 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 6f 76 65  unction..** move
42f44 73 20 2a 70 70 50 6f 73 6c 69 73 74 20 73 6f 20  s *ppPoslist so 
42f45 74 68 61 74 20 69 74 20 69 6e 73 74 65 61 64 20  that it instead 
42f46 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69  points to the fi
42f47 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 0d  rst byte of the.
42f48 0a 2a 2a 20 73 61 6d 65 20 70 6f 73 69 74 69 6f  .** same positio
42f49 6e 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 73 74 61  n list...*/..sta
42f4a 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65 76  tic void fts3Rev
42f4b 65 72 73 65 50 6f 73 6c 69 73 74 28 63 68 61 72  ersePoslist(char
42f4c 20 2a 70 53 74 61 72 74 2c 20 63 68 61 72 20 2a   *pStart, char *
42f4d 2a 70 70 50 6f 73 6c 69 73 74 29 7b 0d 0a 20 20  *ppPoslist){..  
42f4e 63 68 61 72 20 2a 70 20 3d 20 26 28 2a 70 70 50  char *p = &(*ppP
42f4f 6f 73 6c 69 73 74 29 5b 2d 32 5d 3b 0d 0a 20 20  oslist)[-2];..  
42f50 63 68 61 72 20 63 20 3d 20 30 3b 0d 0a 0d 0a 20  char c = 0;.... 
42f51 20 77 68 69 6c 65 28 20 70 3e 70 53 74 61 72 74   while( p>pStart
42f52 20 26 26 20 28 63 3d 2a 70 2d 2d 29 3d 3d 30 20   && (c=*p--)==0 
42f53 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 3e 70  );..  while( p>p
42f54 53 74 61 72 74 20 26 26 20 28 2a 70 20 26 20 30  Start && (*p & 0
42f55 78 38 30 29 20 7c 20 63 20 29 7b 20 0d 0a 20 20  x80) | c ){ ..  
42f56 20 20 63 20 3d 20 2a 70 2d 2d 3b 20 0d 0a 20 20    c = *p--; ..  
42f57 7d 0d 0a 20 20 69 66 28 20 70 3e 70 53 74 61 72  }..  if( p>pStar
42f58 74 20 29 7b 20 70 20 3d 20 26 70 5b 32 5d 3b 20  t ){ p = &p[2]; 
42f59 7d 0d 0a 20 20 77 68 69 6c 65 28 20 2a 70 2b 2b  }..  while( *p++
42f5a 26 30 78 38 30 20 29 3b 0d 0a 20 20 2a 70 70 50  &0x80 );..  *ppP
42f5b 6f 73 6c 69 73 74 20 3d 20 70 3b 0d 0a 7d 0d 0a  oslist = p;..}..
42f5c 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72 20  ../*..** Helper 
42f5d 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79  function used by
42f5e 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
42f5f 69 6f 6e 20 6f 66 20 74 68 65 20 6f 76 65 72 6c  ion of the overl
42f60 6f 61 64 65 64 20 73 6e 69 70 70 65 74 28 29 2c  oaded snippet(),
42f61 0d 0a 2a 2a 20 6f 66 66 73 65 74 73 28 29 20 61  ..** offsets() a
42f62 6e 64 20 6f 70 74 69 6d 69 7a 65 28 29 20 53 51  nd optimize() SQ
42f63 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0d 0a 2a 2a  L functions...**
42f64 0d 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
42f65 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
42f66 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
42f67 73 20 61 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65  s a blob of size
42f68 0d 0a 2a 2a 20 73 69 7a 65 6f 66 28 46 74 73 33  ..** sizeof(Fts3
42f69 43 75 72 73 6f 72 2a 29 2c 20 74 68 65 6e 20 74  Cursor*), then t
42f6a 68 65 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 73  he blob contents
42f6b 20 61 72 65 20 63 6f 70 69 65 64 20 74 6f 20 74   are copied to t
42f6c 68 65 20 0d 0a 2a 2a 20 6f 75 74 70 75 74 20 76  he ..** output v
42f6d 61 72 69 61 62 6c 65 20 2a 70 70 43 73 72 20 61  ariable *ppCsr a
42f6e 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  nd SQLITE_OK is 
42f6f 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
42f70 69 73 65 2c 20 61 6e 20 65 72 72 6f 72 0d 0a 2a  ise, an error..*
42f71 2a 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69  * message is wri
42f72 74 74 65 6e 20 74 6f 20 63 6f 6e 74 65 78 74 20  tten to context 
42f73 70 43 6f 6e 74 65 78 74 20 61 6e 64 20 53 51 4c  pContext and SQL
42f74 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e  ITE_ERROR return
42f75 65 64 2e 20 54 68 65 0d 0a 2a 2a 20 73 74 72 69  ed. The..** stri
42f76 6e 67 20 70 61 73 73 65 64 20 76 69 61 20 7a 46  ng passed via zF
42f77 75 6e 63 20 69 73 20 75 73 65 64 20 61 73 20 70  unc is used as p
42f78 61 72 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72  art of the error
42f79 20 6d 65 73 73 61 67 65 2e 0d 0a 2a 2f 0d 0a 73   message...*/..s
42f7a 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 46 75  tatic int fts3Fu
42f7b 6e 63 74 69 6f 6e 41 72 67 28 0d 0a 20 20 73 71  nctionArg(..  sq
42f7c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
42f7d 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20 2f 2a  Context,      /*
42f7e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 61   SQL function ca
42f7f 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20  ll context */.. 
42f80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
42f81 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
42f82 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e 61 6d   /* Function nam
42f83 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  e */..  sqlite3_
42f84 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 20 20 20  value *pVal,    
42f85 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 76 5b          /* argv[
42f86 30 5d 20 70 61 73 73 65 64 20 74 6f 20 66 75 6e  0] passed to fun
42f87 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 46 74 73 33  ction */..  Fts3
42f88 43 75 72 73 6f 72 20 2a 2a 70 70 43 73 72 20 20  Cursor **ppCsr  
42f89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
42f8a 55 54 3a 20 53 74 6f 72 65 20 63 75 72 73 6f 72  UT: Store cursor
42f8b 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0d   handle here */.
42f8c 0a 29 7b 0d 0a 20 20 46 74 73 33 43 75 72 73 6f  .){..  Fts3Curso
42f8d 72 20 2a 70 52 65 74 3b 0d 0a 20 20 69 66 28 20  r *pRet;..  if( 
42f8e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
42f8f 70 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45  pe(pVal)!=SQLITE
42f90 5f 42 4c 4f 42 20 0d 0a 20 20 20 7c 7c 20 73 71  _BLOB ..   || sq
42f91 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
42f92 73 28 70 56 61 6c 29 21 3d 73 69 7a 65 6f 66 28  s(pVal)!=sizeof(
42f93 46 74 73 33 43 75 72 73 6f 72 20 2a 29 0d 0a 20  Fts3Cursor *).. 
42f94 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a   ){..    char *z
42f95 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
42f96 72 69 6e 74 66 28 22 69 6c 6c 65 67 61 6c 20 66  rintf("illegal f
42f97 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
42f98 20 25 73 22 2c 20 7a 46 75 6e 63 29 3b 0d 0a 20   %s", zFunc);.. 
42f99 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
42f9a 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  t_error(pContext
42f9b 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 20 20  , zErr, -1);..  
42f9c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
42f9d 45 72 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  Err);..    retur
42f9e 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
42f9f 0a 20 20 7d 0d 0a 20 20 6d 65 6d 63 70 79 28 26  .  }..  memcpy(&
42fa0 70 52 65 74 2c 20 73 71 6c 69 74 65 33 5f 76 61  pRet, sqlite3_va
42fa1 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c 29 2c 20  lue_blob(pVal), 
42fa2 73 69 7a 65 6f 66 28 46 74 73 33 43 75 72 73 6f  sizeof(Fts3Curso
42fa3 72 20 2a 29 29 3b 0d 0a 20 20 2a 70 70 43 73 72  r *));..  *ppCsr
42fa4 20 3d 20 70 52 65 74 3b 0d 0a 20 20 72 65 74 75   = pRet;..  retu
42fa5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
42fa6 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65  ..../*..** Imple
42fa7 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
42fa8 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63 74   snippet() funct
42fa9 69 6f 6e 20 66 6f 72 20 46 54 53 33 0d 0a 2a 2f  ion for FTS3..*/
42faa 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  ..static void ft
42fab 73 33 53 6e 69 70 70 65 74 46 75 6e 63 28 0d 0a  s3SnippetFunc(..
42fac 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
42fad 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20  t *pContext,    
42fae 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63    /* SQLite func
42faf 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78  tion call contex
42fb0 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 56 61 6c  t */..  int nVal
42fb1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
42fb2 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
42fb3 6f 66 20 61 70 56 61 6c 5b 5d 20 61 72 72 61 79  of apVal[] array
42fb4 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76   */..  sqlite3_v
42fb5 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20  alue **apVal    
42fb6 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
42fb7 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d  of arguments */.
42fb8 0a 29 7b 0d 0a 20 20 46 74 73 33 43 75 72 73 6f  .){..  Fts3Curso
42fb9 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20  r *pCsr;        
42fba 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
42fbb 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74   handle passed t
42fbc 68 72 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20  hrough apVal[0] 
42fbd 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
42fbe 20 2a 7a 53 74 61 72 74 20 3d 20 22 3c 62 3e 22   *zStart = "<b>"
42fbf 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ;..  const char 
42fc0 2a 7a 45 6e 64 20 3d 20 22 3c 2f 62 3e 22 3b 0d  *zEnd = "</b>";.
42fc1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
42fc2 45 6c 6c 69 70 73 69 73 20 3d 20 22 3c 62 3e 2e  Ellipsis = "<b>.
42fc3 2e 2e 3c 2f 62 3e 22 3b 0d 0a 20 20 69 6e 74 20  ..</b>";..  int 
42fc4 69 43 6f 6c 20 3d 20 2d 31 3b 0d 0a 20 20 69 6e  iCol = -1;..  in
42fc5 74 20 6e 54 6f 6b 65 6e 20 3d 20 31 35 3b 20 20  t nToken = 15;  
42fc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42fc7 20 44 65 66 61 75 6c 74 20 6e 75 6d 62 65 72 20   Default number 
42fc8 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 73 6e 69  of tokens in sni
42fc9 70 70 65 74 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  ppet */....  /* 
42fca 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 74  There must be at
42fcb 20 6c 65 61 73 74 20 6f 6e 65 20 61 72 67 75 6d   least one argum
42fcc 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68  ent passed to th
42fcd 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74 68  is function (oth
42fce 65 72 77 69 73 65 0d 0a 20 20 2a 2a 20 74 68 65  erwise..  ** the
42fcf 20 6e 6f 6e 2d 6f 76 65 72 6c 6f 61 64 65 64 20   non-overloaded 
42fd0 76 65 72 73 69 6f 6e 20 77 6f 75 6c 64 20 68 61  version would ha
42fd1 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 69  ve been called i
42fd2 6e 73 74 65 61 64 20 6f 66 20 74 68 69 73 20 6f  nstead of this o
42fd3 6e 65 29 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 73  ne)...  */..  as
42fd4 73 65 72 74 28 20 6e 56 61 6c 3e 3d 31 20 29 3b  sert( nVal>=1 );
42fd5 0d 0a 0d 0a 20 20 69 66 28 20 6e 56 61 6c 3e 36  ....  if( nVal>6
42fd6 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
42fd7 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43  _result_error(pC
42fd8 6f 6e 74 65 78 74 2c 20 0d 0a 20 20 20 20 20 20  ontext, ..      
42fd9 20 20 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20    "wrong number 
42fda 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
42fdb 66 75 6e 63 74 69 6f 6e 20 73 6e 69 70 70 65 74  function snippet
42fdc 28 29 22 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 72  ()", -1);..    r
42fdd 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 69  eturn;..  }..  i
42fde 66 28 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41  f( fts3FunctionA
42fdf 72 67 28 70 43 6f 6e 74 65 78 74 2c 20 22 73 6e  rg(pContext, "sn
42fe0 69 70 70 65 74 22 2c 20 61 70 56 61 6c 5b 30 5d  ippet", apVal[0]
42fe1 2c 20 26 70 43 73 72 29 20 29 20 72 65 74 75 72  , &pCsr) ) retur
42fe2 6e 3b 0d 0a 0d 0a 20 20 73 77 69 74 63 68 28 20  n;....  switch( 
42fe3 6e 56 61 6c 20 29 7b 0d 0a 20 20 20 20 63 61 73  nVal ){..    cas
42fe4 65 20 36 3a 20 6e 54 6f 6b 65 6e 20 3d 20 73 71  e 6: nToken = sq
42fe5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
42fe6 61 70 56 61 6c 5b 35 5d 29 3b 0d 0a 20 20 20 20  apVal[5]);..    
42fe7 63 61 73 65 20 35 3a 20 69 43 6f 6c 20 3d 20 73  case 5: iCol = s
42fe8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
42fe9 28 61 70 56 61 6c 5b 34 5d 29 3b 0d 0a 20 20 20  (apVal[4]);..   
42fea 20 63 61 73 65 20 34 3a 20 7a 45 6c 6c 69 70 73   case 4: zEllips
42feb 69 73 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  is = (const char
42fec 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
42fed 74 65 78 74 28 61 70 56 61 6c 5b 33 5d 29 3b 0d  text(apVal[3]);.
42fee 0a 20 20 20 20 63 61 73 65 20 33 3a 20 7a 45 6e  .    case 3: zEn
42fef 64 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  d = (const char*
42ff0 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
42ff1 65 78 74 28 61 70 56 61 6c 5b 32 5d 29 3b 0d 0a  ext(apVal[2]);..
42ff2 20 20 20 20 63 61 73 65 20 32 3a 20 7a 53 74 61      case 2: zSta
42ff3 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  rt = (const char
42ff4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
42ff5 74 65 78 74 28 61 70 56 61 6c 5b 31 5d 29 3b 0d  text(apVal[1]);.
42ff6 0a 20 20 7d 0d 0a 20 20 69 66 28 20 21 7a 45 6c  .  }..  if( !zEl
42ff7 6c 69 70 73 69 73 20 7c 7c 20 21 7a 45 6e 64 20  lipsis || !zEnd 
42ff8 7c 7c 20 21 7a 53 74 61 72 74 20 29 7b 0d 0a 20  || !zStart ){.. 
42ff9 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
42ffa 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70 43  t_error_nomem(pC
42ffb 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 7d 65 6c 73  ontext);..  }els
42ffc 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d  e if( SQLITE_OK=
42ffd 3d 66 74 73 33 43 75 72 73 6f 72 53 65 65 6b 28  =fts3CursorSeek(
42ffe 70 43 6f 6e 74 65 78 74 2c 20 70 43 73 72 29 20  pContext, pCsr) 
42fff 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46  ){..    sqlite3F
43000 74 73 33 53 6e 69 70 70 65 74 28 70 43 6f 6e 74  ts3Snippet(pCont
43001 65 78 74 2c 20 70 43 73 72 2c 20 7a 53 74 61 72  ext, pCsr, zStar
43002 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73  t, zEnd, zEllips
43003 69 73 2c 20 69 43 6f 6c 2c 20 6e 54 6f 6b 65 6e  is, iCol, nToken
43004 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  );..  }..}..../*
43005 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
43006 69 6f 6e 20 6f 66 20 74 68 65 20 6f 66 66 73 65  ion of the offse
43007 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ts() function fo
43008 72 20 46 54 53 33 0d 0a 2a 2f 0d 0a 73 74 61 74  r FTS3..*/..stat
43009 69 63 20 76 6f 69 64 20 66 74 73 33 4f 66 66 73  ic void fts3Offs
4300a 65 74 73 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69  etsFunc(..  sqli
4300b 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
4300c 6e 74 65 78 74 2c 20 20 20 20 20 20 2f 2a 20 53  ntext,      /* S
4300d 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 20 63  QLite function c
4300e 61 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a  all context */..
4300f 20 20 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20    int nVal,     
43010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43011 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67    /* Size of arg
43012 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0d 0a  ument array */..
43013 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
43014 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20 20 20  **apVal         
43015 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 72    /* Array of ar
43016 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a 29 7b 0d 0a  guments */..){..
43017 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
43018 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sr;             
43019 20 20 2f 2a 20 43 75 72 73 6f 72 20 68 61 6e 64    /* Cursor hand
4301a 6c 65 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  le passed throug
4301b 68 20 61 70 56 61 6c 5b 30 5d 20 2a 2f 0d 0a 0d  h apVal[0] */...
4301c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
4301d 54 45 52 28 6e 56 61 6c 29 3b 0d 0a 0d 0a 20 20  TER(nVal);....  
4301e 61 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 31 20  assert( nVal==1 
4301f 29 3b 0d 0a 20 20 69 66 28 20 66 74 73 33 46 75  );..  if( fts3Fu
43020 6e 63 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65  nctionArg(pConte
43021 78 74 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 61  xt, "offsets", a
43022 70 56 61 6c 5b 30 5d 2c 20 26 70 43 73 72 29 20  pVal[0], &pCsr) 
43023 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 61 73 73  ) return;..  ass
43024 65 72 74 28 20 70 43 73 72 20 29 3b 0d 0a 20 20  ert( pCsr );..  
43025 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 66  if( SQLITE_OK==f
43026 74 73 33 43 75 72 73 6f 72 53 65 65 6b 28 70 43  ts3CursorSeek(pC
43027 6f 6e 74 65 78 74 2c 20 70 43 73 72 29 20 29 7b  ontext, pCsr) ){
43028 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ..    sqlite3Fts
43029 33 4f 66 66 73 65 74 73 28 70 43 6f 6e 74 65 78  3Offsets(pContex
4302a 74 2c 20 70 43 73 72 29 3b 0d 0a 20 20 7d 0d 0a  t, pCsr);..  }..
4302b 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 49 6d 70  }..../* ..** Imp
4302c 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
4302d 68 65 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d  he special optim
4302e 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ize() function f
4302f 6f 72 20 46 54 53 33 2e 20 54 68 69 73 20 0d 0a  or FTS3. This ..
43030 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 65 72 67  ** function merg
43031 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  es all segments 
43032 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
43033 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
43034 65 6e 74 2e 0d 0a 2a 2a 20 45 78 61 6d 70 6c 65  ent...** Example
43035 20 75 73 61 67 65 20 69 73 3a 0d 0a 2a 2a 0d 0a   usage is:..**..
43036 2a 2a 20 20 20 53 45 4c 45 43 54 20 6f 70 74 69  **   SELECT opti
43037 6d 69 7a 65 28 74 29 20 46 52 4f 4d 20 74 20 4c  mize(t) FROM t L
43038 49 4d 49 54 20 31 3b 0d 0a 2a 2a 0d 0a 2a 2a 20  IMIT 1;..**..** 
43039 77 68 65 72 65 20 27 74 27 20 69 73 20 74 68 65  where 't' is the
4303a 20 6e 61 6d 65 20 6f 66 20 61 6e 20 46 54 53 33   name of an FTS3
4303b 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61   table...*/..sta
4303c 74 69 63 20 76 6f 69 64 20 66 74 73 33 4f 70 74  tic void fts3Opt
4303d 69 6d 69 7a 65 46 75 6e 63 28 0d 0a 20 20 73 71  imizeFunc(..  sq
4303e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
4303f 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20 2f 2a  Context,      /*
43040 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e   SQLite function
43041 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f   call context */
43042 0d 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 20 20  ..  int nVal,   
43043 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43044 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
43045 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f  rgument array */
43046 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ..  sqlite3_valu
43047 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20  e **apVal       
43048 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
43049 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a 29 7b  arguments */..){
4304a 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ..  int rc;     
4304b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4304c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
4304d 64 65 20 2a 2f 0d 0a 20 20 46 74 73 33 54 61 62  de */..  Fts3Tab
4304e 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  le *p;          
4304f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
43050 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
43051 20 2a 2f 0d 0a 20 20 46 74 73 33 43 75 72 73 6f   */..  Fts3Curso
43052 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 20  r *pCursor;     
43053 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
43054 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74   handle passed t
43055 68 72 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20  hrough apVal[0] 
43056 2a 2f 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50  */....  UNUSED_P
43057 41 52 41 4d 45 54 45 52 28 6e 56 61 6c 29 3b 0d  ARAMETER(nVal);.
43058 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 56 61  ...  assert( nVa
43059 6c 3d 3d 31 20 29 3b 0d 0a 20 20 69 66 28 20 66  l==1 );..  if( f
4305a 74 73 33 46 75 6e 63 74 69 6f 6e 41 72 67 28 70  ts3FunctionArg(p
4305b 43 6f 6e 74 65 78 74 2c 20 22 6f 70 74 69 6d 69  Context, "optimi
4305c 7a 65 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20 26  ze", apVal[0], &
4305d 70 43 75 72 73 6f 72 29 20 29 20 72 65 74 75 72  pCursor) ) retur
4305e 6e 3b 0d 0a 20 20 70 20 3d 20 28 46 74 73 33 54  n;..  p = (Fts3T
4305f 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d 3e  able *)pCursor->
43060 62 61 73 65 2e 70 56 74 61 62 3b 0d 0a 20 20 61  base.pVtab;..  a
43061 73 73 65 72 74 28 20 70 20 29 3b 0d 0a 0d 0a 20  ssert( p );.... 
43062 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
43063 33 4f 70 74 69 6d 69 7a 65 28 70 29 3b 0d 0a 0d  3Optimize(p);...
43064 0a 20 20 73 77 69 74 63 68 28 20 72 63 20 29 7b  .  switch( rc ){
43065 0d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
43066 45 5f 4f 4b 3a 0d 0a 20 20 20 20 20 20 73 71 6c  E_OK:..      sql
43067 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
43068 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65  (pContext, "Inde
43069 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d 31  x optimized", -1
4306a 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
4306b 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
4306c 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
4306d 5f 44 4f 4e 45 3a 0d 0a 20 20 20 20 20 20 73 71  _DONE:..      sq
4306e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
4306f 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64  t(pContext, "Ind
43070 65 78 20 61 6c 72 65 61 64 79 20 6f 70 74 69 6d  ex already optim
43071 61 6c 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  al", -1, SQLITE_
43072 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 20 20  STATIC);..      
43073 62 72 65 61 6b 3b 0d 0a 20 20 20 20 64 65 66 61  break;..    defa
43074 75 6c 74 3a 0d 0a 20 20 20 20 20 20 73 71 6c 69  ult:..      sqli
43075 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
43076 5f 63 6f 64 65 28 70 43 6f 6e 74 65 78 74 2c 20  _code(pContext, 
43077 72 63 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61  rc);..      brea
43078 6b 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  k;..  }..}..../*
43079 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
4307a 69 6f 6e 20 6f 66 20 74 68 65 20 6d 61 74 63 68  ion of the match
4307b 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f 6e 20  info() function 
4307c 66 6f 72 20 46 54 53 33 0d 0a 2a 2f 0d 0a 73 74  for FTS3..*/..st
4307d 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 4d 61  atic void fts3Ma
4307e 74 63 68 69 6e 66 6f 46 75 6e 63 28 0d 0a 20 20  tchinfoFunc(..  
4307f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
43080 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20  *pContext,      
43081 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69  /* SQLite functi
43082 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74 20  on call context 
43083 2a 2f 0d 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20  */..  int nVal, 
43084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43085 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
43086 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20   argument array 
43087 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  */..  sqlite3_va
43088 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20  lue **apVal     
43089 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f        /* Array o
4308a 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a  f arguments */..
4308b 29 7b 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72  ){..  Fts3Cursor
4308c 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20   *pCsr;         
4308d 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
4308e 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 68  handle passed th
4308f 72 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20 2a  rough apVal[0] *
43090 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 6e 56 61  /..  assert( nVa
43091 6c 3d 3d 31 20 7c 7c 20 6e 56 61 6c 3d 3d 32 20  l==1 || nVal==2 
43092 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45  );..  if( SQLITE
43093 5f 4f 4b 3d 3d 66 74 73 33 46 75 6e 63 74 69 6f  _OK==fts3Functio
43094 6e 41 72 67 28 70 43 6f 6e 74 65 78 74 2c 20 22  nArg(pContext, "
43095 6d 61 74 63 68 69 6e 66 6f 22 2c 20 61 70 56 61  matchinfo", apVa
43096 6c 5b 30 5d 2c 20 26 70 43 73 72 29 20 29 7b 0d  l[0], &pCsr) ){.
43097 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
43098 2a 7a 41 72 67 20 3d 20 30 3b 0d 0a 20 20 20 20  *zArg = 0;..    
43099 69 66 28 20 6e 56 61 6c 3e 31 20 29 7b 0d 0a 20  if( nVal>1 ){.. 
4309a 20 20 20 20 20 7a 41 72 67 20 3d 20 28 63 6f 6e       zArg = (con
4309b 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
4309c 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56  3_value_text(apV
4309d 61 6c 5b 31 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a  al[1]);..    }..
4309e 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 4d      sqlite3Fts3M
4309f 61 74 63 68 69 6e 66 6f 28 70 43 6f 6e 74 65 78  atchinfo(pContex
430a0 74 2c 20 70 43 73 72 2c 20 7a 41 72 67 29 3b 0d  t, pCsr, zArg);.
430a1 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
430a2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
430a3 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46  mplements the xF
430a4 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
430a5 6f 64 20 66 6f 72 20 74 68 65 20 46 54 53 33 0d  od for the FTS3.
430a6 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
430a7 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
430a8 6e 74 20 66 74 73 33 46 69 6e 64 46 75 6e 63 74  nt fts3FindFunct
430a9 69 6f 6e 4d 65 74 68 6f 64 28 0d 0a 20 20 73 71  ionMethod(..  sq
430aa 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
430ab 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
430ac 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   Virtual table h
430ad 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20  andle */..  int 
430ae 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 20 20  nArg,           
430af 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
430b0 75 6d 62 65 72 20 6f 66 20 53 51 4c 20 66 75 6e  umber of SQL fun
430b1 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
430b2 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
430b3 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
430b4 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
430b5 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   SQL function */
430b6 0d 0a 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75  ..  void (**pxFu
430b7 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
430b8 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
430b9 5f 76 61 6c 75 65 2a 2a 29 2c 20 2f 2a 20 4f 55  _value**), /* OU
430ba 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0d 0a 20 20  T: Result */..  
430bb 76 6f 69 64 20 2a 2a 70 70 41 72 67 20 20 20 20  void **ppArg    
430bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430bd 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0d 0a 29 7b  /* Unused */..){
430be 0d 0a 20 20 73 74 72 75 63 74 20 4f 76 65 72 6c  ..  struct Overl
430bf 6f 61 64 65 64 20 7b 0d 0a 20 20 20 20 63 6f 6e  oaded {..    con
430c0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0d  st char *zName;.
430c1 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  .    void (*xFun
430c2 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
430c3 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
430c4 76 61 6c 75 65 2a 2a 29 3b 0d 0a 20 20 7d 20 61  value**);..  } a
430c5 4f 76 65 72 6c 6f 61 64 5b 5d 20 3d 20 7b 0d 0a  Overload[] = {..
430c6 20 20 20 20 7b 20 22 73 6e 69 70 70 65 74 22 2c      { "snippet",
430c7 20 66 74 73 33 53 6e 69 70 70 65 74 46 75 6e 63   fts3SnippetFunc
430c8 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 6f 66 66 73   },..    { "offs
430c9 65 74 73 22 2c 20 66 74 73 33 4f 66 66 73 65 74  ets", fts3Offset
430ca 73 46 75 6e 63 20 7d 2c 0d 0a 20 20 20 20 7b 20  sFunc },..    { 
430cb 22 6f 70 74 69 6d 69 7a 65 22 2c 20 66 74 73 33  "optimize", fts3
430cc 4f 70 74 69 6d 69 7a 65 46 75 6e 63 20 7d 2c 0d  OptimizeFunc },.
430cd 0a 20 20 20 20 7b 20 22 6d 61 74 63 68 69 6e 66  .    { "matchinf
430ce 6f 22 2c 20 66 74 73 33 4d 61 74 63 68 69 6e 66  o", fts3Matchinf
430cf 6f 46 75 6e 63 20 7d 2c 0d 0a 20 20 7d 3b 0d 0a  oFunc },..  };..
430d0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
430d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430d2 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
430d3 72 69 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 55  riable */....  U
430d4 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
430d5 70 56 74 61 62 29 3b 0d 0a 20 20 55 4e 55 53 45  pVtab);..  UNUSE
430d6 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 41 72 67  D_PARAMETER(nArg
430d7 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
430d8 41 4d 45 54 45 52 28 70 70 41 72 67 29 3b 0d 0a  AMETER(ppArg);..
430d9 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53  ..  for(i=0; i<S
430da 69 7a 65 6f 66 41 72 72 61 79 28 61 4f 76 65 72  izeofArray(aOver
430db 6c 6f 61 64 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20  load); i++){..  
430dc 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61    if( strcmp(zNa
430dd 6d 65 2c 20 61 4f 76 65 72 6c 6f 61 64 5b 69 5d  me, aOverload[i]
430de 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0d 0a 20  .zName)==0 ){.. 
430df 20 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 61       *pxFunc = a
430e0 4f 76 65 72 6c 6f 61 64 5b 69 5d 2e 78 46 75 6e  Overload[i].xFun
430e1 63 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  c;..      return
430e2 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   1;..    }..  }.
430e3 0a 0d 0a 20 20 2f 2a 20 4e 6f 20 66 75 6e 63 74  ...  /* No funct
430e4 69 6f 6e 20 6f 66 20 74 68 65 20 73 70 65 63 69  ion of the speci
430e5 66 69 65 64 20 6e 61 6d 65 20 77 61 73 20 66 6f  fied name was fo
430e6 75 6e 64 2e 20 52 65 74 75 72 6e 20 30 2e 20 2a  und. Return 0. *
430e7 2f 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a  /..  return 0;..
430e8 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c  }..../*..** Impl
430e9 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 46 54  ementation of FT
430ea 53 33 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  S3 xRename metho
430eb 64 2e 20 52 65 6e 61 6d 65 20 61 6e 20 66 74 73  d. Rename an fts
430ec 33 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74  3 table...*/..st
430ed 61 74 69 63 20 69 6e 74 20 66 74 73 33 52 65 6e  atic int fts3Ren
430ee 61 6d 65 4d 65 74 68 6f 64 28 0d 0a 20 20 73 71  ameMethod(..  sq
430ef 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
430f0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
430f1 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   Virtual table h
430f2 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 63 6f 6e 73  andle */..  cons
430f3 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 20  t char *zName   
430f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
430f5 65 77 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c 65  ew name of table
430f6 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 54   */..){..  Fts3T
430f7 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54  able *p = (Fts3T
430f8 61 62 6c 65 20 2a 29 70 56 74 61 62 3b 0d 0a 20  able *)pVtab;.. 
430f9 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
430fa 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20  ->db;           
430fb 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
430fc 6e 65 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e  nection */..  in
430fd 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
430fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
430ff 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d   Return Code */.
43100 0a 0d 0a 20 20 2f 2a 20 41 73 20 69 74 20 68 61  ...  /* As it ha
43101 70 70 65 6e 73 2c 20 74 68 65 20 70 65 6e 64 69  ppens, the pendi
43102 6e 67 20 74 65 72 6d 73 20 74 61 62 6c 65 20 69  ng terms table i
43103 73 20 61 6c 77 61 79 73 20 65 6d 70 74 79 20 68  s always empty h
43104 65 72 65 2e 20 54 68 69 73 20 69 73 0d 0a 20 20  ere. This is..  
43105 2a 2a 20 62 65 63 61 75 73 65 20 61 6e 20 22 41  ** because an "A
43106 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41 4d  LTER TABLE RENAM
43107 45 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65  E TABLE" stateme
43108 6e 74 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e  nt inside a tran
43109 73 61 63 74 69 6f 6e 20 0d 0a 20 20 2a 2a 20 61  saction ..  ** a
4310a 6c 77 61 79 73 20 6f 70 65 6e 73 20 61 20 73 61  lways opens a sa
4310b 76 65 70 6f 69 6e 74 20 74 72 61 6e 73 61 63 74  vepoint transact
4310c 69 6f 6e 2e 20 41 6e 64 20 74 68 65 20 78 53 61  ion. And the xSa
4310d 76 65 70 6f 69 6e 74 28 29 20 6d 65 74 68 6f 64  vepoint() method
4310e 20 0d 0a 20 20 2a 2a 20 66 6c 75 73 68 65 73 20   ..  ** flushes 
4310f 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65 72 6d  the pending term
43110 73 20 74 61 62 6c 65 2e 20 42 75 74 20 6c 65 61  s table. But lea
43111 76 65 20 74 68 65 20 28 6e 6f 2d 6f 70 29 20 63  ve the (no-op) c
43112 61 6c 6c 20 74 6f 0d 0a 20 20 2a 2a 20 50 65 6e  all to..  ** Pen
43113 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68 28 29  dingTermsFlush()
43114 20 69 6e 20 69 6e 20 63 61 73 65 20 74 68 61 74   in in case that
43115 20 63 68 61 6e 67 65 73 2e 0d 0a 20 20 2a 2f 0d   changes...  */.
43116 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 50  .  assert( p->nP
43117 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b  endingData==0 );
43118 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
43119 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73  Fts3PendingTerms
4311a 46 6c 75 73 68 28 70 29 3b 0d 0a 0d 0a 20 20 69  Flush(p);....  i
4311b 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62  f( p->zContentTb
4311c 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 66 74 73  l==0 ){..    fts
4311d 33 44 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c  3DbExec(&rc, db,
4311e 0d 0a 20 20 20 20 20 20 22 41 4c 54 45 52 20 54  ..      "ALTER T
4311f 41 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74  ABLE %Q.'%q_cont
43120 65 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  ent'  RENAME TO 
43121 27 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 2c 0d  '%q_content';",.
43122 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70  .      p->zDb, p
43123 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0d 0a  ->zName, zName..
43124 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20      );..  }.... 
43125 20 69 66 28 20 70 2d 3e 62 48 61 73 44 6f 63 73   if( p->bHasDocs
43126 69 7a 65 20 29 7b 0d 0a 20 20 20 20 66 74 73 33  ize ){..    fts3
43127 44 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c 0d  DbExec(&rc, db,.
43128 0a 20 20 20 20 20 20 22 41 4c 54 45 52 20 54 41  .      "ALTER TA
43129 42 4c 45 20 25 51 2e 27 25 71 5f 64 6f 63 73 69  BLE %Q.'%q_docsi
4312a 7a 65 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27  ze'  RENAME TO '
4312b 25 71 5f 64 6f 63 73 69 7a 65 27 3b 22 2c 0d 0a  %q_docsize';",..
4312c 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70 2d        p->zDb, p-
4312d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0d 0a 20  >zName, zName.. 
4312e 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66     );..  }..  if
4312f 28 20 70 2d 3e 62 48 61 73 53 74 61 74 20 29 7b  ( p->bHasStat ){
43130 0d 0a 20 20 20 20 66 74 73 33 44 62 45 78 65 63  ..    fts3DbExec
43131 28 26 72 63 2c 20 64 62 2c 0d 0a 20 20 20 20 20  (&rc, db,..     
43132 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51   "ALTER TABLE %Q
43133 2e 27 25 71 5f 73 74 61 74 27 20 20 52 45 4e 41  .'%q_stat'  RENA
43134 4d 45 20 54 4f 20 27 25 71 5f 73 74 61 74 27 3b  ME TO '%q_stat';
43135 22 2c 0d 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62  ",..      p->zDb
43136 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  , p->zName, zNam
43137 65 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a  e..    );..  }..
43138 20 20 66 74 73 33 44 62 45 78 65 63 28 26 72 63    fts3DbExec(&rc
43139 2c 20 64 62 2c 0d 0a 20 20 20 20 22 41 4c 54 45  , db,..    "ALTE
4313a 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73  R TABLE %Q.'%q_s
4313b 65 67 6d 65 6e 74 73 27 20 52 45 4e 41 4d 45 20  egments' RENAME 
4313c 54 4f 20 27 25 71 5f 73 65 67 6d 65 6e 74 73 27  TO '%q_segments'
4313d 3b 22 2c 0d 0a 20 20 20 20 70 2d 3e 7a 44 62 2c  ;",..    p->zDb,
4313e 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
4313f 0d 0a 20 20 29 3b 0d 0a 20 20 66 74 73 33 44 62  ..  );..  fts3Db
43140 45 78 65 63 28 26 72 63 2c 20 64 62 2c 0d 0a 20  Exec(&rc, db,.. 
43141 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
43142 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 20  %Q.'%q_segdir'  
43143 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73   RENAME TO '%q_s
43144 65 67 64 69 72 27 3b 22 2c 0d 0a 20 20 20 20 70  egdir';",..    p
43145 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
43146 20 7a 4e 61 6d 65 0d 0a 20 20 29 3b 0d 0a 20 20   zName..  );..  
43147 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
43148 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 78 53 61 76  ./*..** The xSav
43149 65 70 6f 69 6e 74 28 29 20 6d 65 74 68 6f 64 2e  epoint() method.
4314a 0d 0a 2a 2a 0d 0a 2a 2a 20 46 6c 75 73 68 20 74  ..**..** Flush t
4314b 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
4314c 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73  he pending-terms
4314d 20 74 61 62 6c 65 20 74 6f 20 64 69 73 6b 2e 0d   table to disk..
4314e 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
4314f 66 74 73 33 53 61 76 65 70 6f 69 6e 74 4d 65 74  fts3SavepointMet
43150 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
43151 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 69 53 61   *pVtab, int iSa
43152 76 65 70 6f 69 6e 74 29 7b 0d 0a 20 20 55 4e 55  vepoint){..  UNU
43153 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 53  SED_PARAMETER(iS
43154 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 61 73  avepoint);..  as
43155 73 65 72 74 28 20 28 28 46 74 73 33 54 61 62 6c  sert( ((Fts3Tabl
43156 65 20 2a 29 70 56 74 61 62 29 2d 3e 69 6e 54 72  e *)pVtab)->inTr
43157 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0d 0a 20 20  ansaction );..  
43158 61 73 73 65 72 74 28 20 28 28 46 74 73 33 54 61  assert( ((Fts3Ta
43159 62 6c 65 20 2a 29 70 56 74 61 62 29 2d 3e 6d 78  ble *)pVtab)->mx
4315a 53 61 76 65 70 6f 69 6e 74 20 3c 20 69 53 61 76  Savepoint < iSav
4315b 65 70 6f 69 6e 74 20 29 3b 0d 0a 20 20 54 45 53  epoint );..  TES
4315c 54 4f 4e 4c 59 28 20 28 28 46 74 73 33 54 61 62  TONLY( ((Fts3Tab
4315d 6c 65 20 2a 29 70 56 74 61 62 29 2d 3e 6d 78 53  le *)pVtab)->mxS
4315e 61 76 65 70 6f 69 6e 74 20 3d 20 69 53 61 76 65  avepoint = iSave
4315f 70 6f 69 6e 74 20 29 3b 0d 0a 20 20 72 65 74 75  point );..  retu
43160 72 6e 20 66 74 73 33 53 79 6e 63 4d 65 74 68 6f  rn fts3SyncMetho
43161 64 28 70 56 74 61 62 29 3b 0d 0a 7d 0d 0a 0d 0a  d(pVtab);..}....
43162 2f 2a 0d 0a 2a 2a 20 54 68 65 20 78 52 65 6c 65  /*..** The xRele
43163 61 73 65 28 29 20 6d 65 74 68 6f 64 2e 0d 0a 2a  ase() method...*
43164 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  *..** This is a 
43165 6e 6f 2d 6f 70 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  no-op...*/..stat
43166 69 63 20 69 6e 74 20 66 74 73 33 52 65 6c 65 61  ic int fts3Relea
43167 73 65 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  seMethod(sqlite3
43168 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e  _vtab *pVtab, in
43169 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0d 0a  t iSavepoint){..
4316a 20 20 54 45 53 54 4f 4e 4c 59 28 20 46 74 73 33    TESTONLY( Fts3
4316b 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33  Table *p = (Fts3
4316c 54 61 62 6c 65 2a 29 70 56 74 61 62 20 29 3b 0d  Table*)pVtab );.
4316d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
4316e 54 45 52 28 69 53 61 76 65 70 6f 69 6e 74 29 3b  TER(iSavepoint);
4316f 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
43170 45 54 45 52 28 70 56 74 61 62 29 3b 0d 0a 20 20  ETER(pVtab);..  
43171 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
43172 6e 73 61 63 74 69 6f 6e 20 29 3b 0d 0a 20 20 61  nsaction );..  a
43173 73 73 65 72 74 28 20 70 2d 3e 6d 78 53 61 76 65  ssert( p->mxSave
43174 70 6f 69 6e 74 20 3e 3d 20 69 53 61 76 65 70 6f  point >= iSavepo
43175 69 6e 74 20 29 3b 0d 0a 20 20 54 45 53 54 4f 4e  int );..  TESTON
43176 4c 59 28 20 70 2d 3e 6d 78 53 61 76 65 70 6f 69  LY( p->mxSavepoi
43177 6e 74 20 3d 20 69 53 61 76 65 70 6f 69 6e 74 2d  nt = iSavepoint-
43178 31 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  1 );..  return S
43179 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
4317a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 78 52 6f 6c 6c  /*..** The xRoll
4317b 62 61 63 6b 54 6f 28 29 20 6d 65 74 68 6f 64 2e  backTo() method.
4317c 0d 0a 2a 2a 0d 0a 2a 2a 20 44 69 73 63 61 72 64  ..**..** Discard
4317d 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
4317e 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65 72   the pending ter
4317f 6d 73 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73  ms table...*/..s
43180 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 52 6f  tatic int fts3Ro
43181 6c 6c 62 61 63 6b 54 6f 4d 65 74 68 6f 64 28 73  llbackToMethod(s
43182 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
43183 61 62 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69  ab, int iSavepoi
43184 6e 74 29 7b 0d 0a 20 20 46 74 73 33 54 61 62 6c  nt){..  Fts3Tabl
43185 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c  e *p = (Fts3Tabl
43186 65 2a 29 70 56 74 61 62 3b 0d 0a 20 20 55 4e 55  e*)pVtab;..  UNU
43187 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 53  SED_PARAMETER(iS
43188 61 76 65 70 6f 69 6e 74 29 3b 0d 0a 20 20 61 73  avepoint);..  as
43189 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73  sert( p->inTrans
4318a 61 63 74 69 6f 6e 20 29 3b 0d 0a 20 20 61 73 73  action );..  ass
4318b 65 72 74 28 20 70 2d 3e 6d 78 53 61 76 65 70 6f  ert( p->mxSavepo
4318c 69 6e 74 20 3e 3d 20 69 53 61 76 65 70 6f 69 6e  int >= iSavepoin
4318d 74 20 29 3b 0d 0a 20 20 54 45 53 54 4f 4e 4c 59  t );..  TESTONLY
4318e 28 20 70 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74  ( p->mxSavepoint
4318f 20 3d 20 69 53 61 76 65 70 6f 69 6e 74 20 29 3b   = iSavepoint );
43190 0d 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 50  ..  sqlite3Fts3P
43191 65 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65 61 72  endingTermsClear
43192 28 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  (p);..  return S
43193 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
43194 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
43195 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33  ite3_module fts3
43196 4d 6f 64 75 6c 65 20 3d 20 7b 0d 0a 20 20 2f 2a  Module = {..  /*
43197 20 69 56 65 72 73 69 6f 6e 20 20 20 20 20 20 2a   iVersion      *
43198 2f 20 32 2c 0d 0a 20 20 2f 2a 20 78 43 72 65 61  / 2,..  /* xCrea
43199 74 65 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33  te       */ fts3
4319a 43 72 65 61 74 65 4d 65 74 68 6f 64 2c 0d 0a 20  CreateMethod,.. 
4319b 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20 20 20   /* xConnect    
4319c 20 20 2a 2f 20 66 74 73 33 43 6f 6e 6e 65 63 74    */ fts3Connect
4319d 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20 78 42  Method,..  /* xB
4319e 65 73 74 49 6e 64 65 78 20 20 20 20 2a 2f 20 66  estIndex    */ f
4319f 74 73 33 42 65 73 74 49 6e 64 65 78 4d 65 74 68  ts3BestIndexMeth
431a0 6f 64 2c 0d 0a 20 20 2f 2a 20 78 44 69 73 63 6f  od,..  /* xDisco
431a1 6e 6e 65 63 74 20 20 20 2a 2f 20 66 74 73 33 44  nnect   */ fts3D
431a2 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 2c  isconnectMethod,
431a3 0d 0a 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20  ..  /* xDestroy 
431a4 20 20 20 20 20 2a 2f 20 66 74 73 33 44 65 73 74       */ fts3Dest
431a5 72 6f 79 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a  royMethod,..  /*
431a6 20 78 4f 70 65 6e 20 20 20 20 20 20 20 20 20 2a   xOpen         *
431a7 2f 20 66 74 73 33 4f 70 65 6e 4d 65 74 68 6f 64  / fts3OpenMethod
431a8 2c 0d 0a 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20  ,..  /* xClose  
431a9 20 20 20 20 20 20 2a 2f 20 66 74 73 33 43 6c 6f        */ fts3Clo
431aa 73 65 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20  seMethod,..  /* 
431ab 78 46 69 6c 74 65 72 20 20 20 20 20 20 20 2a 2f  xFilter       */
431ac 20 66 74 73 33 46 69 6c 74 65 72 4d 65 74 68 6f   fts3FilterMetho
431ad 64 2c 0d 0a 20 20 2f 2a 20 78 4e 65 78 74 20 20  d,..  /* xNext  
431ae 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 4e 65         */ fts3Ne
431af 78 74 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20  xtMethod,..  /* 
431b0 78 45 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f  xEof          */
431b1 20 66 74 73 33 45 6f 66 4d 65 74 68 6f 64 2c 0d   fts3EofMethod,.
431b2 0a 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 20 20  .  /* xColumn   
431b3 20 20 20 20 2a 2f 20 66 74 73 33 43 6f 6c 75 6d      */ fts3Colum
431b4 6e 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20 78  nMethod,..  /* x
431b5 52 6f 77 69 64 20 20 20 20 20 20 20 20 2a 2f 20  Rowid        */ 
431b6 66 74 73 33 52 6f 77 69 64 4d 65 74 68 6f 64 2c  fts3RowidMethod,
431b7 0d 0a 20 20 2f 2a 20 78 55 70 64 61 74 65 20 20  ..  /* xUpdate  
431b8 20 20 20 20 20 2a 2f 20 66 74 73 33 55 70 64 61       */ fts3Upda
431b9 74 65 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20  teMethod,..  /* 
431ba 78 42 65 67 69 6e 20 20 20 20 20 20 20 20 2a 2f  xBegin        */
431bb 20 66 74 73 33 42 65 67 69 6e 4d 65 74 68 6f 64   fts3BeginMethod
431bc 2c 0d 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20  ,..  /* xSync   
431bd 20 20 20 20 20 20 2a 2f 20 66 74 73 33 53 79 6e        */ fts3Syn
431be 63 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a 20 78  cMethod,..  /* x
431bf 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 2a 2f 20  Commit       */ 
431c0 66 74 73 33 43 6f 6d 6d 69 74 4d 65 74 68 6f 64  fts3CommitMethod
431c1 2c 0d 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63  ,..  /* xRollbac
431c2 6b 20 20 20 20 20 2a 2f 20 66 74 73 33 52 6f 6c  k     */ fts3Rol
431c3 6c 62 61 63 6b 4d 65 74 68 6f 64 2c 0d 0a 20 20  lbackMethod,..  
431c4 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  /* xFindFunction
431c5 20 2a 2f 20 66 74 73 33 46 69 6e 64 46 75 6e 63   */ fts3FindFunc
431c6 74 69 6f 6e 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f  tionMethod,..  /
431c7 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20 20 20  * xRename */    
431c8 20 20 20 66 74 73 33 52 65 6e 61 6d 65 4d 65 74     fts3RenameMet
431c9 68 6f 64 2c 0d 0a 20 20 2f 2a 20 78 53 61 76 65  hod,..  /* xSave
431ca 70 6f 69 6e 74 20 20 20 20 2a 2f 20 66 74 73 33  point    */ fts3
431cb 53 61 76 65 70 6f 69 6e 74 4d 65 74 68 6f 64 2c  SavepointMethod,
431cc 0d 0a 20 20 2f 2a 20 78 52 65 6c 65 61 73 65 20  ..  /* xRelease 
431cd 20 20 20 20 20 2a 2f 20 66 74 73 33 52 65 6c 65       */ fts3Rele
431ce 61 73 65 4d 65 74 68 6f 64 2c 0d 0a 20 20 2f 2a  aseMethod,..  /*
431cf 20 78 52 6f 6c 6c 62 61 63 6b 54 6f 20 20 20 2a   xRollbackTo   *
431d0 2f 20 66 74 73 33 52 6f 6c 6c 62 61 63 6b 54 6f  / fts3RollbackTo
431d1 4d 65 74 68 6f 64 2c 0d 0a 7d 3b 0d 0a 0d 0a 2f  Method,..};..../
431d2 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
431d3 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65  ion is registere
431d4 64 20 61 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  d as the module 
431d5 64 65 73 74 72 75 63 74 6f 72 20 28 63 61 6c 6c  destructor (call
431d6 65 64 20 77 68 65 6e 20 61 6e 0d 0a 2a 2a 20 46  ed when an..** F
431d7 54 53 33 20 65 6e 61 62 6c 65 64 20 64 61 74 61  TS3 enabled data
431d8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
431d9 69 73 20 63 6c 6f 73 65 64 29 2e 20 49 74 20 66  is closed). It f
431da 72 65 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 0d  rees the memory.
431db 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  .** allocated fo
431dc 72 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  r the tokenizer 
431dd 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  hash table...*/.
431de 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 61 73  .static void has
431df 68 44 65 73 74 72 6f 79 28 76 6f 69 64 20 2a 70  hDestroy(void *p
431e0 29 7b 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a  ){..  Fts3Hash *
431e1 70 48 61 73 68 20 3d 20 28 46 74 73 33 48 61 73  pHash = (Fts3Has
431e2 68 20 2a 29 70 3b 0d 0a 20 20 73 71 6c 69 74 65  h *)p;..  sqlite
431e3 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 70  3Fts3HashClear(p
431e4 48 61 73 68 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Hash);..  sqlite
431e5 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b 0d 0a  3_free(pHash);..
431e6 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20  }..../*..** The 
431e7 66 74 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f  fts3 built-in to
431e8 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70  kenizers - "simp
431e9 6c 65 22 2c 20 22 70 6f 72 74 65 72 22 20 61 6e  le", "porter" an
431ea 64 20 22 69 63 75 22 2d 20 61 72 65 20 0d 0a 2a  d "icu"- are ..*
431eb 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e  * implemented in
431ec 20 66 69 6c 65 73 20 66 74 73 33 5f 74 6f 6b 65   files fts3_toke
431ed 6e 69 7a 65 72 31 2e 63 2c 20 66 74 73 33 5f 70  nizer1.c, fts3_p
431ee 6f 72 74 65 72 2e 63 20 61 6e 64 20 66 74 73 33  orter.c and fts3
431ef 5f 69 63 75 2e 63 0d 0a 2a 2a 20 72 65 73 70 65  _icu.c..** respe
431f0 63 74 69 76 65 6c 79 2e 20 54 68 65 20 66 6f 6c  ctively. The fol
431f1 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 6f 72  lowing three for
431f2 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
431f3 73 20 61 72 65 20 66 6f 72 20 66 75 6e 63 74 69  s are for functi
431f4 6f 6e 73 0d 0a 2a 2a 20 64 65 63 6c 61 72 65 64  ons..** declared
431f5 20 69 6e 20 74 68 65 73 65 20 66 69 6c 65 73 20   in these files 
431f6 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65  used to retrieve
431f7 20 74 68 65 20 72 65 73 70 65 63 74 69 76 65 20   the respective 
431f8 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
431f9 0d 0a 2a 2a 0d 0a 2a 2a 20 43 61 6c 6c 69 6e 67  ..**..** Calling
431fa 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
431fb 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
431fc 65 28 29 20 73 65 74 73 20 74 68 65 20 76 61 6c  e() sets the val
431fd 75 65 20 70 6f 69 6e 74 65 64 0d 0a 2a 2a 20 74  ue pointed..** t
431fe 6f 20 62 79 20 74 68 65 20 61 72 67 75 6d 65 6e  o by the argumen
431ff 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
43200 65 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e  e "simple" token
43201 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74  izer implementat
43202 69 6f 6e 2e 0d 0a 2a 2a 20 41 6e 64 20 73 6f 20  ion...** And so 
43203 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  on...*/..SQLITE_
43204 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
43205 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
43206 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71  kenizerModule(sq
43207 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
43208 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
43209 4d 6f 64 75 6c 65 29 3b 0d 0a 53 51 4c 49 54 45  Module);..SQLITE
4320a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4320b 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72 54  lite3Fts3PorterT
4320c 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73  okenizerModule(s
4320d 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
4320e 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
4320f 70 4d 6f 64 75 6c 65 29 3b 0d 0a 23 69 66 64 65  pModule);..#ifde
43210 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
43211 49 43 55 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ICU..SQLITE_PRIV
43212 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
43213 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72  Fts3IcuTokenizer
43214 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
43215 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
43216 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
43217 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d  ;..#endif..../*.
43218 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 74  .** Initialise t
43219 68 65 20 66 74 73 33 20 65 78 74 65 6e 73 69 6f  he fts3 extensio
4321a 6e 2e 20 49 66 20 74 68 69 73 20 65 78 74 65 6e  n. If this exten
4321b 73 69 6f 6e 20 69 73 20 62 75 69 6c 74 20 61 73  sion is built as
4321c 20 70 61 72 74 0d 0a 2a 2a 20 6f 66 20 74 68 65   part..** of the
4321d 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72 79 2c   sqlite library,
4321e 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
4321f 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 69  ion is called di
43220 72 65 63 74 6c 79 20 62 79 0d 0a 2a 2a 20 53 51  rectly by..** SQ
43221 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69 73  Lite. If fts3 is
43222 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e 61   built as a dyna
43223 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c 65  mically loadable
43224 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73   extension, this
43225 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  ..** function is
43226 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73   called by the s
43227 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
43228 5f 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f  _init() entry po
43229 69 6e 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  int...*/..SQLITE
4322a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4322b 69 74 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c  ite3Fts3Init(sql
4322c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 69 6e  ite3 *db){..  in
4322d 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
4322e 3b 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70  ;..  Fts3Hash *p
4322f 48 61 73 68 20 3d 20 30 3b 0d 0a 20 20 63 6f 6e  Hash = 0;..  con
43230 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
43231 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 53 69  izer_module *pSi
43232 6d 70 6c 65 20 3d 20 30 3b 0d 0a 20 20 63 6f 6e  mple = 0;..  con
43233 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
43234 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 50 6f  izer_module *pPo
43235 72 74 65 72 20 3d 20 30 3b 0d 0a 0d 0a 23 69 66  rter = 0;....#if
43236 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
43237 45 5f 49 43 55 0d 0a 20 20 63 6f 6e 73 74 20 73  E_ICU..  const s
43238 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
43239 5f 6d 6f 64 75 6c 65 20 2a 70 49 63 75 20 3d 20  _module *pIcu = 
4323a 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 46 74 73  0;..  sqlite3Fts
4323b 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  3IcuTokenizerMod
4323c 75 6c 65 28 26 70 49 63 75 29 3b 0d 0a 23 65 6e  ule(&pIcu);..#en
4323d 64 69 66 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51  dif....#ifdef SQ
4323e 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 72 63 20  LITE_TEST..  rc 
4323f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
43240 74 54 65 72 6d 28 64 62 29 3b 0d 0a 20 20 69 66  tTerm(db);..  if
43241 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
43242 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 23 65  ) return rc;..#e
43243 6e 64 69 66 0d 0a 0d 0a 20 20 72 63 20 3d 20 73  ndif....  rc = s
43244 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 41 75  qlite3Fts3InitAu
43245 78 28 64 62 29 3b 0d 0a 20 20 69 66 28 20 72 63  x(db);..  if( rc
43246 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
43247 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20 20 73 71  turn rc;....  sq
43248 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54  lite3Fts3SimpleT
43249 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26  okenizerModule(&
4324a 70 53 69 6d 70 6c 65 29 3b 0d 0a 20 20 73 71 6c  pSimple);..  sql
4324b 69 74 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f  ite3Fts3PorterTo
4324c 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70  kenizerModule(&p
4324d 50 6f 72 74 65 72 29 3b 0d 0a 0d 0a 20 20 2f 2a  Porter);....  /*
4324e 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e   Allocate and in
4324f 69 74 69 61 6c 69 73 65 20 74 68 65 20 68 61 73  itialise the has
43250 68 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20  h-table used to 
43251 73 74 6f 72 65 20 74 6f 6b 65 6e 69 7a 65 72 73  store tokenizers
43252 2e 20 2a 2f 0d 0a 20 20 70 48 61 73 68 20 3d 20  . */..  pHash = 
43253 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
43254 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68 29 29  izeof(Fts3Hash))
43255 3b 0d 0a 20 20 69 66 28 20 21 70 48 61 73 68 20  ;..  if( !pHash 
43256 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
43257 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 65  ITE_NOMEM;..  }e
43258 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  lse{..    sqlite
43259 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48  3Fts3HashInit(pH
4325a 61 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53  ash, FTS3_HASH_S
4325b 54 52 49 4e 47 2c 20 31 29 3b 0d 0a 20 20 7d 0d  TRING, 1);..  }.
4325c 0a 0d 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65  ...  /* Load the
4325d 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69   built-in tokeni
4325e 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 68 61  zers into the ha
4325f 73 68 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69  sh table */..  i
43260 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
43261 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 73 71 6c   ){..    if( sql
43262 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
43263 72 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c  rt(pHash, "simpl
43264 65 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  e", 7, (void *)p
43265 53 69 6d 70 6c 65 29 0d 0a 20 20 20 20 20 7c 7c  Simple)..     ||
43266 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
43267 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 70  Insert(pHash, "p
43268 6f 72 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64  orter", 7, (void
43269 20 2a 29 70 50 6f 72 74 65 72 29 20 0d 0a 23 69   *)pPorter) ..#i
4326a 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
4326b 4c 45 5f 49 43 55 0d 0a 20 20 20 20 20 7c 7c 20  LE_ICU..     || 
4326c 28 70 49 63 75 20 26 26 20 73 71 6c 69 74 65 33  (pIcu && sqlite3
4326d 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
4326e 48 61 73 68 2c 20 22 69 63 75 22 2c 20 34 2c 20  Hash, "icu", 4, 
4326f 28 76 6f 69 64 20 2a 29 70 49 63 75 29 29 0d 0a  (void *)pIcu))..
43270 23 65 6e 64 69 66 0d 0a 20 20 20 20 29 7b 0d 0a  #endif..    ){..
43271 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
43272 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d  E_NOMEM;..    }.
43273 0a 20 20 7d 0d 0a 0d 0a 23 69 66 64 65 66 20 53  .  }....#ifdef S
43274 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 69 66  QLITE_TEST..  if
43275 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
43276 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ){..    rc = sql
43277 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74  ite3Fts3ExprInit
43278 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 64 62  TestInterface(db
43279 29 3b 0d 0a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d  );..  }..#endif.
4327a 0a 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74  ...  /* Create t
4327b 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
4327c 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   wrapper around 
4327d 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 61  the hash-table a
4327e 6e 64 20 6f 76 65 72 6c 6f 61 64 20 0d 0a 20 20  nd overload ..  
4327f 2a 2a 20 74 68 65 20 74 77 6f 20 73 63 61 6c 61  ** the two scala
43280 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20  r functions. If 
43281 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
43282 75 6c 2c 20 72 65 67 69 73 74 65 72 20 74 68 65  ul, register the
43283 0d 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 77 69  ..  ** module wi
43284 74 68 20 73 71 6c 69 74 65 2e 0d 0a 20 20 2a 2f  th sqlite...  */
43285 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
43286 4b 3d 3d 72 63 20 0d 0a 20 20 20 26 26 20 53 51  K==rc ..   && SQ
43287 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
43288 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
43289 73 68 54 61 62 6c 65 28 64 62 2c 20 70 48 61 73  shTable(db, pHas
4328a 68 2c 20 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  h, "fts3_tokeniz
4328b 65 72 22 29 29 0d 0a 20 20 20 26 26 20 53 51 4c  er"))..   && SQL
4328c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
4328d 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
4328e 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 73 6e 69  unction(db, "sni
4328f 70 70 65 74 22 2c 20 2d 31 29 29 0d 0a 20 20 20  ppet", -1))..   
43290 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
43291 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
43292 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
43293 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 31 29 29  , "offsets", 1))
43294 0d 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  ..   && SQLITE_O
43295 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
43296 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
43297 6f 6e 28 64 62 2c 20 22 6d 61 74 63 68 69 6e 66  on(db, "matchinf
43298 6f 22 2c 20 31 29 29 0d 0a 20 20 20 26 26 20 53  o", 1))..   && S
43299 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
4329a 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
4329b 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6d  _function(db, "m
4329c 61 74 63 68 69 6e 66 6f 22 2c 20 32 29 29 0d 0a  atchinfo", 2))..
4329d 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
4329e 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f  =(rc = sqlite3_o
4329f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
432a0 28 64 62 2c 20 22 6f 70 74 69 6d 69 7a 65 22 2c  (db, "optimize",
432a1 20 31 29 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20   1))..  ){..    
432a2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
432a3 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0d 0a  ate_module_v2(..
432a4 20 20 20 20 20 20 20 20 64 62 2c 20 22 66 74 73          db, "fts
432a5 33 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65 2c  3", &fts3Module,
432a6 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c 20   (void *)pHash, 
432a7 68 61 73 68 44 65 73 74 72 6f 79 0d 0a 20 20 20  hashDestroy..   
432a8 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d   );..    if( rc=
432a9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
432aa 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
432ab 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
432ac 76 32 28 0d 0a 20 20 20 20 20 20 20 20 20 20 64  v2(..          d
432ad 62 2c 20 22 66 74 73 34 22 2c 20 26 66 74 73 33  b, "fts4", &fts3
432ae 4d 6f 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29  Module, (void *)
432af 70 48 61 73 68 2c 20 30 0d 0a 20 20 20 20 20 20  pHash, 0..      
432b0 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72  );..    }..    r
432b1 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a  eturn rc;..  }..
432b2 0d 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20  ..  /* An error 
432b3 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 44 65  has occurred. De
432b4 6c 65 74 65 20 74 68 65 20 68 61 73 68 20 74 61  lete the hash ta
432b5 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ble and return t
432b6 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a  he error code. *
432b7 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  /..  assert( rc!
432b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20  =SQLITE_OK );.. 
432b9 20 69 66 28 20 70 48 61 73 68 20 29 7b 0d 0a 20   if( pHash ){.. 
432ba 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
432bb 73 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0d  shClear(pHash);.
432bc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
432bd 65 28 70 48 61 73 68 29 3b 0d 0a 20 20 7d 0d 0a  e(pHash);..  }..
432be 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
432bf 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61  .../*..** Alloca
432c0 74 65 20 61 6e 20 46 74 73 33 4d 75 6c 74 69 53  te an Fts3MultiS
432c1 65 67 52 65 61 64 65 72 20 66 6f 72 20 65 61 63  egReader for eac
432c2 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65  h token in the e
432c3 78 70 72 65 73 73 69 6f 6e 20 68 65 61 64 65 64  xpression headed
432c4 0d 0a 2a 2a 20 62 79 20 70 45 78 70 72 2e 20 0d  ..** by pExpr. .
432c5 0a 2a 2a 0d 0a 2a 2a 20 41 6e 20 46 74 73 33 53  .**..** An Fts3S
432c6 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 20  egReader object 
432c7 69 73 20 61 20 63 75 72 73 6f 72 20 74 68 61 74  is a cursor that
432c8 20 63 61 6e 20 73 65 65 6b 20 6f 72 20 73 63 61   can seek or sca
432c9 6e 20 61 20 72 61 6e 67 65 20 6f 66 0d 0a 2a 2a  n a range of..**
432ca 20 65 6e 74 72 69 65 73 20 77 69 74 68 69 6e 20   entries within 
432cb 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
432cc 20 62 2d 74 72 65 65 2e 20 41 6e 20 46 74 73 33   b-tree. An Fts3
432cd 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 75  MultiSegReader u
432ce 73 65 73 20 6d 75 6c 74 69 70 6c 65 0d 0a 2a 2a  ses multiple..**
432cf 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 6f   Fts3SegReader o
432d0 62 6a 65 63 74 73 20 69 6e 74 65 72 6e 61 6c 6c  bjects internall
432d1 79 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20  y to provide an 
432d2 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73 65 65  interface to see
432d3 6b 20 6f 72 20 73 63 61 6e 0d 0a 2a 2a 20 77 69  k or scan..** wi
432d4 74 68 69 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f  thin the union o
432d5 66 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 6f  f all segments o
432d6 66 20 61 20 62 2d 74 72 65 65 2e 20 48 65 6e 63  f a b-tree. Henc
432d7 65 20 74 68 65 20 6e 61 6d 65 2e 0d 0a 2a 2a 0d  e the name...**.
432d8 0a 2a 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63  .** If the alloc
432d9 61 74 65 64 20 46 74 73 33 4d 75 6c 74 69 53 65  ated Fts3MultiSe
432da 67 52 65 61 64 65 72 20 6a 75 73 74 20 73 65 65  gReader just see
432db 6b 73 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 65  ks to a single e
432dc 6e 74 72 79 20 69 6e 20 61 0d 0a 2a 2a 20 73 65  ntry in a..** se
432dd 67 6d 65 6e 74 20 62 2d 74 72 65 65 20 28 69 66  gment b-tree (if
432de 20 74 68 65 20 74 65 72 6d 20 69 73 20 6e 6f 74   the term is not
432df 20 61 20 70 72 65 66 69 78 20 6f 72 20 69 74 20   a prefix or it 
432e0 69 73 20 61 20 70 72 65 66 69 78 20 66 6f 72 20  is a prefix for 
432e1 77 68 69 63 68 0d 0a 2a 2a 20 74 68 65 72 65 20  which..** there 
432e2 65 78 69 73 74 73 20 70 72 65 66 69 78 20 62 2d  exists prefix b-
432e3 74 72 65 65 20 6f 66 20 74 68 65 20 72 69 67 68  tree of the righ
432e4 74 20 6c 65 6e 67 74 68 29 20 74 68 65 6e 20 69  t length) then i
432e5 74 20 6d 61 79 20 62 65 20 74 72 61 76 65 72 73  t may be travers
432e6 65 64 0d 0a 2a 2a 20 61 6e 64 20 6d 65 72 67 65  ed..** and merge
432e7 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 2e  d incrementally.
432e8 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 68   Otherwise, it h
432e9 61 73 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20  as to be merged 
432ea 69 6e 74 6f 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  into an in-memor
432eb 79 20 0d 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 61  y ..** doclist a
432ec 6e 64 20 74 68 65 6e 20 74 72 61 76 65 72 73 65  nd then traverse
432ed 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  d...*/..static v
432ee 6f 69 64 20 66 74 73 33 45 76 61 6c 41 6c 6c 6f  oid fts3EvalAllo
432ef 63 61 74 65 52 65 61 64 65 72 73 28 0d 0a 20 20  cateReaders(..  
432f0 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
432f1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
432f2 2f 2a 20 46 54 53 20 63 75 72 73 6f 72 20 68 61  /* FTS cursor ha
432f3 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46 74 73 33 45  ndle */..  Fts3E
432f4 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
432f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
432f6 6c 6f 63 61 74 65 20 72 65 61 64 65 72 73 20 66  locate readers f
432f7 6f 72 20 74 68 69 73 20 65 78 70 72 65 73 73 69  or this expressi
432f8 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e  on */..  int *pn
432f9 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20  Token,          
432fa 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
432fb 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
432fc 20 74 6f 6b 65 6e 73 20 69 6e 20 70 68 72 61 73   tokens in phras
432fd 65 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e  e. */..  int *pn
432fe 4f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Or,             
432ff 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
43300 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
43301 20 4f 52 20 6e 6f 64 65 73 20 69 6e 20 65 78 70   OR nodes in exp
43302 72 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 52  r. */..  int *pR
43303 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
43304 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
43305 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a  UT: Error code *
43306 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 70 45 78  /..){..  if( pEx
43307 70 72 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d  pr && SQLITE_OK=
43308 3d 2a 70 52 63 20 29 7b 0d 0a 20 20 20 20 69 66  =*pRc ){..    if
43309 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d  ( pExpr->eType==
4330a 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
4330b 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  ){..      int i;
4330c 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b  ..      int nTok
4330d 65 6e 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72  en = pExpr->pPhr
4330e 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0d 0a 20 20  ase->nToken;..  
4330f 20 20 20 20 2a 70 6e 54 6f 6b 65 6e 20 2b 3d 20      *pnToken += 
43310 6e 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 66  nToken;..      f
43311 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e  or(i=0; i<nToken
43312 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; i++){..       
43313 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e   Fts3PhraseToken
43314 20 2a 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70   *pToken = &pExp
43315 72 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  r->pPhrase->aTok
43316 65 6e 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 20 20  en[i];..        
43317 69 6e 74 20 72 63 20 3d 20 66 74 73 33 54 65 72  int rc = fts3Ter
43318 6d 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72  mSegReaderCursor
43319 28 70 43 73 72 2c 20 0d 0a 20 20 20 20 20 20 20  (pCsr, ..       
4331a 20 20 20 20 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20       pToken->z, 
4331b 70 54 6f 6b 65 6e 2d 3e 6e 2c 20 70 54 6f 6b 65  pToken->n, pToke
4331c 6e 2d 3e 69 73 50 72 65 66 69 78 2c 20 26 70 54  n->isPrefix, &pT
4331d 6f 6b 65 6e 2d 3e 70 53 65 67 63 73 72 0d 0a 20  oken->pSegcsr.. 
4331e 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20         );..     
4331f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
43320 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
43321 20 20 20 2a 70 52 63 20 3d 20 72 63 3b 0d 0a 20     *pRc = rc;.. 
43322 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b           return;
43323 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
43324 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
43325 72 74 28 20 70 45 78 70 72 2d 3e 70 50 68 72 61  rt( pExpr->pPhra
43326 73 65 2d 3e 69 44 6f 63 6c 69 73 74 54 6f 6b 65  se->iDoclistToke
43327 6e 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70  n==0 );..      p
43328 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2d 3e 69  Expr->pPhrase->i
43329 44 6f 63 6c 69 73 74 54 6f 6b 65 6e 20 3d 20 2d  DoclistToken = -
4332a 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  1;..    }else{..
4332b 20 20 20 20 20 20 2a 70 6e 4f 72 20 2b 3d 20 28        *pnOr += (
4332c 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54  pExpr->eType==FT
4332d 53 51 55 45 52 59 5f 4f 52 29 3b 0d 0a 20 20 20  SQUERY_OR);..   
4332e 20 20 20 66 74 73 33 45 76 61 6c 41 6c 6c 6f 63     fts3EvalAlloc
4332f 61 74 65 52 65 61 64 65 72 73 28 70 43 73 72 2c  ateReaders(pCsr,
43330 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
43331 6e 54 6f 6b 65 6e 2c 20 70 6e 4f 72 2c 20 70 52  nToken, pnOr, pR
43332 63 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 45  c);..      fts3E
43333 76 61 6c 41 6c 6c 6f 63 61 74 65 52 65 61 64 65  valAllocateReade
43334 72 73 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e  rs(pCsr, pExpr->
43335 70 52 69 67 68 74 2c 20 70 6e 54 6f 6b 65 6e 2c  pRight, pnToken,
43336 20 70 6e 4f 72 2c 20 70 52 63 29 3b 0d 0a 20 20   pnOr, pRc);..  
43337 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f    }..  }..}..../
43338 2a 0d 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 73 20  *..** Arguments 
43339 70 4c 69 73 74 2f 6e 4c 69 73 74 20 63 6f 6e 74  pList/nList cont
4333a 61 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ain the doclist 
4333b 66 6f 72 20 74 6f 6b 65 6e 20 69 54 6f 6b 65 6e  for token iToken
4333c 20 6f 66 20 70 68 72 61 73 65 20 70 2e 0d 0a 2a   of phrase p...*
4333d 2a 20 49 74 20 69 73 20 6d 65 72 67 65 64 20 69  * It is merged i
4333e 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 6f 63  nto the main doc
4333f 6c 69 73 74 20 73 74 6f 72 65 64 20 69 6e 20 70  list stored in p
43340 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 2f 6e  ->doclist.aAll/n
43341 41 6c 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69  All...**..** Thi
43342 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
43343 65 73 20 74 68 61 74 20 70 4c 69 73 74 20 70 6f  es that pList po
43344 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
43345 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
43346 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ..** sqlite3_mal
43347 6c 6f 63 28 29 2e 20 54 68 69 73 20 66 75 6e 63  loc(). This func
43348 74 69 6f 6e 20 74 61 6b 65 73 20 72 65 73 70 6f  tion takes respo
43349 6e 73 69 62 69 6c 69 74 79 20 66 6f 72 20 65 76  nsibility for ev
4334a 65 6e 74 75 61 6c 6c 79 0d 0a 2a 2a 20 66 72 65  entually..** fre
4334b 65 69 6e 67 20 74 68 65 20 62 75 66 66 65 72 2e  eing the buffer.
4334c 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
4334d 64 20 66 74 73 33 45 76 61 6c 50 68 72 61 73 65  d fts3EvalPhrase
4334e 4d 65 72 67 65 54 6f 6b 65 6e 28 0d 0a 20 20 46  MergeToken(..  F
4334f 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  ts3Table *pTab, 
43350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43351 2a 20 46 54 53 20 54 61 62 6c 65 20 70 6f 69 6e  * FTS Table poin
43352 74 65 72 20 2a 2f 0d 0a 20 20 46 74 73 33 50 68  ter */..  Fts3Ph
43353 72 61 73 65 20 2a 70 2c 20 20 20 20 20 20 20 20  rase *p,        
43354 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72            /* Phr
43355 61 73 65 20 74 6f 20 6d 65 72 67 65 20 70 4c 69  ase to merge pLi
43356 73 74 2f 6e 4c 69 73 74 20 69 6e 74 6f 20 2a 2f  st/nList into */
43357 0d 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 2c 20  ..  int iToken, 
43358 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43359 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 70 4c 69      /* Token pLi
4335a 73 74 2f 6e 4c 69 73 74 20 63 6f 72 72 65 73 70  st/nList corresp
4335b 6f 6e 64 73 20 74 6f 20 2a 2f 0d 0a 20 20 63 68  onds to */..  ch
4335c 61 72 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20  ar *pList,      
4335d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4335e 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c   Pointer to docl
4335f 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4c  ist */..  int nL
43360 69 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ist             
43361 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
43362 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
43363 70 4c 69 73 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  pList */..){..  
43364 61 73 73 65 72 74 28 20 69 54 6f 6b 65 6e 21 3d  assert( iToken!=
43365 70 2d 3e 69 44 6f 63 6c 69 73 74 54 6f 6b 65 6e  p->iDoclistToken
43366 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 4c 69   );....  if( pLi
43367 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 73 71  st==0 ){..    sq
43368 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 64 6f  lite3_free(p->do
43369 63 6c 69 73 74 2e 61 41 6c 6c 29 3b 0d 0a 20 20  clist.aAll);..  
4336a 20 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c    p->doclist.aAl
4336b 6c 20 3d 20 30 3b 0d 0a 20 20 20 20 70 2d 3e 64  l = 0;..    p->d
4336c 6f 63 6c 69 73 74 2e 6e 41 6c 6c 20 3d 20 30 3b  oclist.nAll = 0;
4336d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 65 6c 73 65 20  ..  }....  else 
4336e 69 66 28 20 70 2d 3e 69 44 6f 63 6c 69 73 74 54  if( p->iDoclistT
4336f 6f 6b 65 6e 3c 30 20 29 7b 0d 0a 20 20 20 20 70  oken<0 ){..    p
43370 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 20 3d  ->doclist.aAll =
43371 20 70 4c 69 73 74 3b 0d 0a 20 20 20 20 70 2d 3e   pList;..    p->
43372 64 6f 63 6c 69 73 74 2e 6e 41 6c 6c 20 3d 20 6e  doclist.nAll = n
43373 4c 69 73 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  List;..  }....  
43374 65 6c 73 65 20 69 66 28 20 70 2d 3e 64 6f 63 6c  else if( p->docl
43375 69 73 74 2e 61 41 6c 6c 3d 3d 30 20 29 7b 0d 0a  ist.aAll==0 ){..
43376 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
43377 28 70 4c 69 73 74 29 3b 0d 0a 20 20 7d 0d 0a 0d  (pList);..  }...
43378 0a 20 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 63  .  else {..    c
43379 68 61 72 20 2a 70 4c 65 66 74 3b 0d 0a 20 20 20  har *pLeft;..   
4337a 20 63 68 61 72 20 2a 70 52 69 67 68 74 3b 0d 0a   char *pRight;..
4337b 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 3b 0d 0a      int nLeft;..
4337c 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74 3b 0d      int nRight;.
4337d 0a 20 20 20 20 69 6e 74 20 6e 44 69 66 66 3b 0d  .    int nDiff;.
4337e 0a 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 44  ...    if( p->iD
4337f 6f 63 6c 69 73 74 54 6f 6b 65 6e 3c 69 54 6f 6b  oclistToken<iTok
43380 65 6e 20 29 7b 0d 0a 20 20 20 20 20 20 70 4c 65  en ){..      pLe
43381 66 74 20 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e  ft = p->doclist.
43382 61 41 6c 6c 3b 0d 0a 20 20 20 20 20 20 6e 4c 65  aAll;..      nLe
43383 66 74 20 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e  ft = p->doclist.
43384 6e 41 6c 6c 3b 0d 0a 20 20 20 20 20 20 70 52 69  nAll;..      pRi
43385 67 68 74 20 3d 20 70 4c 69 73 74 3b 0d 0a 20 20  ght = pList;..  
43386 20 20 20 20 6e 52 69 67 68 74 20 3d 20 6e 4c 69      nRight = nLi
43387 73 74 3b 0d 0a 20 20 20 20 20 20 6e 44 69 66 66  st;..      nDiff
43388 20 3d 20 69 54 6f 6b 65 6e 20 2d 20 70 2d 3e 69   = iToken - p->i
43389 44 6f 63 6c 69 73 74 54 6f 6b 65 6e 3b 0d 0a 20  DoclistToken;.. 
4338a 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
4338b 20 70 52 69 67 68 74 20 3d 20 70 2d 3e 64 6f 63   pRight = p->doc
4338c 6c 69 73 74 2e 61 41 6c 6c 3b 0d 0a 20 20 20 20  list.aAll;..    
4338d 20 20 6e 52 69 67 68 74 20 3d 20 70 2d 3e 64 6f    nRight = p->do
4338e 63 6c 69 73 74 2e 6e 41 6c 6c 3b 0d 0a 20 20 20  clist.nAll;..   
4338f 20 20 20 70 4c 65 66 74 20 3d 20 70 4c 69 73 74     pLeft = pList
43390 3b 0d 0a 20 20 20 20 20 20 6e 4c 65 66 74 20 3d  ;..      nLeft =
43391 20 6e 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 6e   nList;..      n
43392 44 69 66 66 20 3d 20 70 2d 3e 69 44 6f 63 6c 69  Diff = p->iDocli
43393 73 74 54 6f 6b 65 6e 20 2d 20 69 54 6f 6b 65 6e  stToken - iToken
43394 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
43395 66 74 73 33 44 6f 63 6c 69 73 74 50 68 72 61 73  fts3DoclistPhras
43396 65 4d 65 72 67 65 28 70 54 61 62 2d 3e 62 44 65  eMerge(pTab->bDe
43397 73 63 49 64 78 2c 20 6e 44 69 66 66 2c 20 70 4c  scIdx, nDiff, pL
43398 65 66 74 2c 20 6e 4c 65 66 74 2c 20 70 52 69 67  eft, nLeft, pRig
43399 68 74 2c 26 6e 52 69 67 68 74 29 3b 0d 0a 20 20  ht,&nRight);..  
4339a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
4339b 4c 65 66 74 29 3b 0d 0a 20 20 20 20 70 2d 3e 64  Left);..    p->d
4339c 6f 63 6c 69 73 74 2e 61 41 6c 6c 20 3d 20 70 52  oclist.aAll = pR
4339d 69 67 68 74 3b 0d 0a 20 20 20 20 70 2d 3e 64 6f  ight;..    p->do
4339e 63 6c 69 73 74 2e 6e 41 6c 6c 20 3d 20 6e 52 69  clist.nAll = nRi
4339f 67 68 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69  ght;..  }....  i
433a0 66 28 20 69 54 6f 6b 65 6e 3e 70 2d 3e 69 44 6f  f( iToken>p->iDo
433a1 63 6c 69 73 74 54 6f 6b 65 6e 20 29 20 70 2d 3e  clistToken ) p->
433a2 69 44 6f 63 6c 69 73 74 54 6f 6b 65 6e 20 3d 20  iDoclistToken = 
433a3 69 54 6f 6b 65 6e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  iToken;..}..../*
433a4 0d 0a 2a 2a 20 4c 6f 61 64 20 74 68 65 20 64 6f  ..** Load the do
433a5 63 6c 69 73 74 20 66 6f 72 20 70 68 72 61 73 65  clist for phrase
433a6 20 70 20 69 6e 74 6f 20 70 2d 3e 64 6f 63 6c 69   p into p->docli
433a7 73 74 2e 61 41 6c 6c 2f 6e 41 6c 6c 2e 20 54 68  st.aAll/nAll. Th
433a8 65 20 6c 6f 61 64 65 64 20 64 6f 63 6c 69 73 74  e loaded doclist
433a9 0d 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 74 61  ..** does not ta
433aa 6b 65 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65  ke deferred toke
433ab 6e 73 20 69 6e 74 6f 20 61 63 63 6f 75 6e 74 2e  ns into account.
433ac 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f  ..**..** SQLITE_
433ad 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
433ae 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
433af 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20  s, otherwise an 
433b0 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
433b1 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
433b2 6e 74 20 66 74 73 33 45 76 61 6c 50 68 72 61 73  nt fts3EvalPhras
433b3 65 4c 6f 61 64 28 0d 0a 20 20 46 74 73 33 43 75  eLoad(..  Fts3Cu
433b4 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
433b5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
433b6 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a   Cursor handle *
433b7 2f 0d 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  /..  Fts3Phrase 
433b8 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *p              
433b9 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 6f       /* Phrase o
433ba 62 6a 65 63 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  bject */..){..  
433bb 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 20  Fts3Table *pTab 
433bc 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70  = (Fts3Table *)p
433bd 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  Csr->base.pVtab;
433be 0d 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0d  ..  int iToken;.
433bf 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
433c0 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 66 6f 72 28  TE_OK;....  for(
433c1 69 54 6f 6b 65 6e 3d 30 3b 20 72 63 3d 3d 53 51  iToken=0; rc==SQ
433c2 4c 49 54 45 5f 4f 4b 20 26 26 20 69 54 6f 6b 65  LITE_OK && iToke
433c3 6e 3c 70 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 54 6f  n<p->nToken; iTo
433c4 6b 65 6e 2b 2b 29 7b 0d 0a 20 20 20 20 46 74 73  ken++){..    Fts
433c5 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54  3PhraseToken *pT
433c6 6f 6b 65 6e 20 3d 20 26 70 2d 3e 61 54 6f 6b 65  oken = &p->aToke
433c7 6e 5b 69 54 6f 6b 65 6e 5d 3b 0d 0a 20 20 20 20  n[iToken];..    
433c8 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e  assert( pToken->
433c9 70 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20  pDeferred==0 || 
433ca 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63 73 72 3d  pToken->pSegcsr=
433cb 3d 30 20 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28  =0 );....    if(
433cc 20 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63 73 72   pToken->pSegcsr
433cd 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   ){..      int n
433ce 54 68 69 73 20 3d 20 30 3b 0d 0a 20 20 20 20 20  This = 0;..     
433cf 20 63 68 61 72 20 2a 70 54 68 69 73 20 3d 20 30   char *pThis = 0
433d0 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74  ;..      rc = ft
433d1 73 33 54 65 72 6d 53 65 6c 65 63 74 28 70 54 61  s3TermSelect(pTa
433d2 62 2c 20 70 54 6f 6b 65 6e 2c 20 70 2d 3e 69 43  b, pToken, p->iC
433d3 6f 6c 75 6d 6e 2c 20 26 6e 54 68 69 73 2c 20 26  olumn, &nThis, &
433d4 70 54 68 69 73 29 3b 0d 0a 20 20 20 20 20 20 69  pThis);..      i
433d5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
433d6 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 66 74 73   ){..        fts
433d7 33 45 76 61 6c 50 68 72 61 73 65 4d 65 72 67 65  3EvalPhraseMerge
433d8 54 6f 6b 65 6e 28 70 54 61 62 2c 20 70 2c 20 69  Token(pTab, p, i
433d9 54 6f 6b 65 6e 2c 20 70 54 68 69 73 2c 20 6e 54  Token, pThis, nT
433da 68 69 73 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  his);..      }..
433db 20 20 20 20 7d 0d 0a 20 20 20 20 61 73 73 65 72      }..    asser
433dc 74 28 20 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63  t( pToken->pSegc
433dd 73 72 3d 3d 30 20 29 3b 0d 0a 20 20 7d 0d 0a 0d  sr==0 );..  }...
433de 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
433df 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
433e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
433e1 65 64 20 6f 6e 20 65 61 63 68 20 70 68 72 61 73  ed on each phras
433e2 65 20 61 66 74 65 72 20 74 68 65 20 70 6f 73 69  e after the posi
433e3 74 69 6f 6e 20 6c 69 73 74 73 20 66 6f 72 0d 0a  tion lists for..
433e4 2a 2a 20 61 6e 79 20 64 65 66 65 72 72 65 64 20  ** any deferred 
433e5 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65 65 6e  tokens have been
433e6 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d   loaded into mem
433e7 6f 72 79 2e 20 49 74 20 75 70 64 61 74 65 73 20  ory. It updates 
433e8 74 68 65 20 70 68 72 61 73 65 73 0d 0a 2a 2a 20  the phrases..** 
433e9 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e  current position
433ea 20 6c 69 73 74 20 74 6f 20 69 6e 63 6c 75 64 65   list to include
433eb 20 6f 6e 6c 79 20 74 68 6f 73 65 20 70 6f 73 69   only those posi
433ec 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 72  tions that are r
433ed 65 61 6c 6c 79 0d 0a 2a 2a 20 69 6e 73 74 61 6e  eally..** instan
433ee 63 65 73 20 6f 66 20 74 68 65 20 70 68 72 61 73  ces of the phras
433ef 65 20 28 61 66 74 65 72 20 63 6f 6e 73 69 64 65  e (after conside
433f0 72 69 6e 67 20 64 65 66 65 72 72 65 64 20 74 6f  ring deferred to
433f1 6b 65 6e 73 29 2e 20 49 66 20 74 68 69 73 0d 0a  kens). If this..
433f2 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ** means that th
433f3 65 20 70 68 72 61 73 65 20 64 6f 65 73 20 6e 6f  e phrase does no
433f4 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
433f5 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 64 6f 63  current row, doc
433f6 6c 69 73 74 2e 70 4c 69 73 74 0d 0a 2a 2a 20 61  list.pList..** a
433f7 6e 64 20 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74  nd doclist.nList
433f8 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f 65 64   are both zeroed
433f9 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54 45  ...**..** SQLITE
433fa 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
433fb 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  if no error occu
433fc 72 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e  rs, otherwise an
433fd 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
433fe 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  de...*/..static 
433ff 69 6e 74 20 66 74 73 33 45 76 61 6c 44 65 66 65  int fts3EvalDefe
43400 72 72 65 64 50 68 72 61 73 65 28 46 74 73 33 43  rredPhrase(Fts3C
43401 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 46 74 73  ursor *pCsr, Fts
43402 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73 65  3Phrase *pPhrase
43403 29 7b 0d 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e  ){..  int iToken
43404 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
43405 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
43406 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
43407 20 70 68 72 61 73 65 20 74 6f 6b 65 6e 73 20 2a   phrase tokens *
43408 2f 0d 0a 20 20 63 68 61 72 20 2a 61 50 6f 73 6c  /..  char *aPosl
43409 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ist = 0;        
4340a 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
4340b 20 6c 69 73 74 20 66 6f 72 20 64 65 66 65 72 72   list for deferr
4340c 65 64 20 74 6f 6b 65 6e 73 20 2a 2f 0d 0a 20 20  ed tokens */..  
4340d 69 6e 74 20 6e 50 6f 73 6c 69 73 74 20 3d 20 30  int nPoslist = 0
4340e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4340f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
43410 65 73 20 69 6e 20 61 50 6f 73 6c 69 73 74 20 2a  es in aPoslist *
43411 2f 0d 0a 20 20 69 6e 74 20 69 50 72 65 76 20 3d  /..  int iPrev =
43412 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   -1;            
43413 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 6e 75       /* Token nu
43414 6d 62 65 72 20 6f 66 20 70 72 65 76 69 6f 75 73  mber of previous
43415 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 20   deferred token 
43416 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
43417 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
43418 2e 62 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 3b  .bFreeList==0 );
43419 0d 0a 0d 0a 20 20 66 6f 72 28 69 54 6f 6b 65 6e  ....  for(iToken
4341a 3d 30 3b 20 69 54 6f 6b 65 6e 3c 70 50 68 72 61  =0; iToken<pPhra
4341b 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 54 6f 6b  se->nToken; iTok
4341c 65 6e 2b 2b 29 7b 0d 0a 20 20 20 20 46 74 73 33  en++){..    Fts3
4341d 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f  PhraseToken *pTo
4341e 6b 65 6e 20 3d 20 26 70 50 68 72 61 73 65 2d 3e  ken = &pPhrase->
4341f 61 54 6f 6b 65 6e 5b 69 54 6f 6b 65 6e 5d 3b 0d  aToken[iToken];.
43420 0a 20 20 20 20 46 74 73 33 44 65 66 65 72 72 65  .    Fts3Deferre
43421 64 54 6f 6b 65 6e 20 2a 70 44 65 66 65 72 72 65  dToken *pDeferre
43422 64 20 3d 20 70 54 6f 6b 65 6e 2d 3e 70 44 65 66  d = pToken->pDef
43423 65 72 72 65 64 3b 0d 0a 0d 0a 20 20 20 20 69 66  erred;....    if
43424 28 20 70 44 65 66 65 72 72 65 64 20 29 7b 0d 0a  ( pDeferred ){..
43425 20 20 20 20 20 20 63 68 61 72 20 2a 70 4c 69 73        char *pLis
43426 74 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 4c  t;..      int nL
43427 69 73 74 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20  ist;..      int 
43428 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
43429 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 4c 69 73  DeferredTokenLis
4342a 74 28 70 44 65 66 65 72 72 65 64 2c 20 26 70 4c  t(pDeferred, &pL
4342b 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0d 0a 20  ist, &nList);.. 
4342c 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
4342d 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
4342e 72 63 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28  rc;....      if(
4342f 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0d 0a 20 20   pList==0 ){..  
43430 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
43431 65 65 28 61 50 6f 73 6c 69 73 74 29 3b 0d 0a 20  ee(aPoslist);.. 
43432 20 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e         pPhrase->
43433 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 20 3d 20  doclist.pList = 
43434 30 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 68 72  0;..        pPhr
43435 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69  ase->doclist.nLi
43436 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  st = 0;..       
43437 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
43438 4b 3b 0d 0a 0d 0a 20 20 20 20 20 20 7d 65 6c 73  K;....      }els
43439 65 20 69 66 28 20 61 50 6f 73 6c 69 73 74 3d 3d  e if( aPoslist==
4343a 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 50  0 ){..        aP
4343b 6f 73 6c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0d  oslist = pList;.
4343c 0a 20 20 20 20 20 20 20 20 6e 50 6f 73 6c 69 73  .        nPoslis
4343d 74 20 3d 20 6e 4c 69 73 74 3b 0d 0a 0d 0a 20 20  t = nList;....  
4343e 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
4343f 20 20 20 20 63 68 61 72 20 2a 61 4f 75 74 20 3d      char *aOut =
43440 20 70 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20   pList;..       
43441 20 63 68 61 72 20 2a 70 31 20 3d 20 61 50 6f 73   char *p1 = aPos
43442 6c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 63  list;..        c
43443 68 61 72 20 2a 70 32 20 3d 20 61 4f 75 74 3b 0d  har *p2 = aOut;.
43444 0a 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ...        asser
43445 74 28 20 69 50 72 65 76 3e 3d 30 20 29 3b 0d 0a  t( iPrev>=0 );..
43446 20 20 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c          fts3Posl
43447 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 26  istPhraseMerge(&
43448 61 4f 75 74 2c 20 69 54 6f 6b 65 6e 2d 69 50 72  aOut, iToken-iPr
43449 65 76 2c 20 30 2c 20 31 2c 20 26 70 31 2c 20 26  ev, 0, 1, &p1, &
4344a 70 32 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71  p2);..        sq
4344b 6c 69 74 65 33 5f 66 72 65 65 28 61 50 6f 73 6c  lite3_free(aPosl
4344c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 61  ist);..        a
4344d 50 6f 73 6c 69 73 74 20 3d 20 70 4c 69 73 74 3b  Poslist = pList;
4344e 0d 0a 20 20 20 20 20 20 20 20 6e 50 6f 73 6c 69  ..        nPosli
4344f 73 74 20 3d 20 61 4f 75 74 20 2d 20 61 50 6f 73  st = aOut - aPos
43450 6c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 69  list;..        i
43451 66 28 20 6e 50 6f 73 6c 69 73 74 3d 3d 30 20 29  f( nPoslist==0 )
43452 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  {..          sql
43453 69 74 65 33 5f 66 72 65 65 28 61 50 6f 73 6c 69  ite3_free(aPosli
43454 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  st);..          
43455 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
43456 2e 70 4c 69 73 74 20 3d 20 30 3b 0d 0a 20 20 20  .pList = 0;..   
43457 20 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e         pPhrase->
43458 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 3d 20  doclist.nList = 
43459 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65  0;..          re
4345a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
4345b 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
4345c 20 20 7d 0d 0a 20 20 20 20 20 20 69 50 72 65 76    }..      iPrev
4345d 20 3d 20 69 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20   = iToken;..    
4345e 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20  }..  }....  if( 
4345f 69 50 72 65 76 3e 3d 30 20 29 7b 0d 0a 20 20 20  iPrev>=0 ){..   
43460 20 69 6e 74 20 6e 4d 61 78 55 6e 64 65 66 65 72   int nMaxUndefer
43461 72 65 64 20 3d 20 70 50 68 72 61 73 65 2d 3e 69  red = pPhrase->i
43462 44 6f 63 6c 69 73 74 54 6f 6b 65 6e 3b 0d 0a 20  DoclistToken;.. 
43463 20 20 20 69 66 28 20 6e 4d 61 78 55 6e 64 65 66     if( nMaxUndef
43464 65 72 72 65 64 3c 30 20 29 7b 0d 0a 20 20 20 20  erred<0 ){..    
43465 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69    pPhrase->docli
43466 73 74 2e 70 4c 69 73 74 20 3d 20 61 50 6f 73 6c  st.pList = aPosl
43467 69 73 74 3b 0d 0a 20 20 20 20 20 20 70 50 68 72  ist;..      pPhr
43468 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69  ase->doclist.nLi
43469 73 74 20 3d 20 6e 50 6f 73 6c 69 73 74 3b 0d 0a  st = nPoslist;..
4346a 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64        pPhrase->d
4346b 6f 63 6c 69 73 74 2e 69 44 6f 63 69 64 20 3d 20  oclist.iDocid = 
4346c 70 43 73 72 2d 3e 69 50 72 65 76 49 64 3b 0d 0a  pCsr->iPrevId;..
4346d 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64        pPhrase->d
4346e 6f 63 6c 69 73 74 2e 62 46 72 65 65 4c 69 73 74  oclist.bFreeList
4346f 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65   = 1;..    }else
43470 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 69  {..      int nDi
43471 73 74 61 6e 63 65 3b 0d 0a 20 20 20 20 20 20 63  stance;..      c
43472 68 61 72 20 2a 70 31 3b 0d 0a 20 20 20 20 20 20  har *p1;..      
43473 63 68 61 72 20 2a 70 32 3b 0d 0a 20 20 20 20 20  char *p2;..     
43474 20 63 68 61 72 20 2a 61 4f 75 74 3b 0d 0a 0d 0a   char *aOut;....
43475 20 20 20 20 20 20 69 66 28 20 6e 4d 61 78 55 6e        if( nMaxUn
43476 64 65 66 65 72 72 65 64 3e 69 50 72 65 76 20 29  deferred>iPrev )
43477 7b 0d 0a 20 20 20 20 20 20 20 20 70 31 20 3d 20  {..        p1 = 
43478 61 50 6f 73 6c 69 73 74 3b 0d 0a 20 20 20 20 20  aPoslist;..     
43479 20 20 20 70 32 20 3d 20 70 50 68 72 61 73 65 2d     p2 = pPhrase-
4347a 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 3b 0d  >doclist.pList;.
4347b 0a 20 20 20 20 20 20 20 20 6e 44 69 73 74 61 6e  .        nDistan
4347c 63 65 20 3d 20 6e 4d 61 78 55 6e 64 65 66 65 72  ce = nMaxUndefer
4347d 72 65 64 20 2d 20 69 50 72 65 76 3b 0d 0a 20 20  red - iPrev;..  
4347e 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
4347f 20 20 20 20 70 31 20 3d 20 70 50 68 72 61 73 65      p1 = pPhrase
43480 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 3b  ->doclist.pList;
43481 0d 0a 20 20 20 20 20 20 20 20 70 32 20 3d 20 61  ..        p2 = a
43482 50 6f 73 6c 69 73 74 3b 0d 0a 20 20 20 20 20 20  Poslist;..      
43483 20 20 6e 44 69 73 74 61 6e 63 65 20 3d 20 69 50    nDistance = iP
43484 72 65 76 20 2d 20 6e 4d 61 78 55 6e 64 65 66 65  rev - nMaxUndefe
43485 72 72 65 64 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  rred;..      }..
43486 0d 0a 20 20 20 20 20 20 61 4f 75 74 20 3d 20 28  ..      aOut = (
43487 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  char *)sqlite3_m
43488 61 6c 6c 6f 63 28 6e 50 6f 73 6c 69 73 74 2b 38  alloc(nPoslist+8
43489 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 61  );..      if( !a
4348a 4f 75 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Out ){..        
4348b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 50 6f  sqlite3_free(aPo
4348c 73 6c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20 20  slist);..       
4348d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
4348e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  OMEM;..      }..
4348f 20 20 20 20 20 20 0d 0a 20 20 20 20 20 20 70 50        ..      pP
43490 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70  hrase->doclist.p
43491 4c 69 73 74 20 3d 20 61 4f 75 74 3b 0d 0a 20 20  List = aOut;..  
43492 20 20 20 20 69 66 28 20 66 74 73 33 50 6f 73 6c      if( fts3Posl
43493 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 26  istPhraseMerge(&
43494 61 4f 75 74 2c 20 6e 44 69 73 74 61 6e 63 65 2c  aOut, nDistance,
43495 20 30 2c 20 31 2c 20 26 70 31 2c 20 26 70 32 29   0, 1, &p1, &p2)
43496 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70 50 68   ){..        pPh
43497 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 62 46  rase->doclist.bF
43498 72 65 65 4c 69 73 74 20 3d 20 31 3b 0d 0a 20 20  reeList = 1;..  
43499 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64        pPhrase->d
4349a 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 3d 20 28  oclist.nList = (
4349b 61 4f 75 74 20 2d 20 70 50 68 72 61 73 65 2d 3e  aOut - pPhrase->
4349c 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 29 3b 0d  doclist.pList);.
4349d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
4349e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
4349f 72 65 65 28 61 4f 75 74 29 3b 0d 0a 20 20 20 20  ree(aOut);..    
434a0 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63      pPhrase->doc
434a1 6c 69 73 74 2e 70 4c 69 73 74 20 3d 20 30 3b 0d  list.pList = 0;.
434a2 0a 20 20 20 20 20 20 20 20 70 50 68 72 61 73 65  .        pPhrase
434a3 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20  ->doclist.nList 
434a4 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  = 0;..      }.. 
434a5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
434a6 65 28 61 50 6f 73 6c 69 73 74 29 3b 0d 0a 20 20  e(aPoslist);..  
434a7 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65    }..  }....  re
434a8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
434a9 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
434aa 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
434ab 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 20 46 74  lled for each Ft
434ac 73 33 50 68 72 61 73 65 20 69 6e 20 61 20 66 75  s3Phrase in a fu
434ad 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 0d 0a  ll-text query ..
434ae 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  ** expression to
434af 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
434b0 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 72 65  mechanism for re
434b1 74 75 72 6e 69 6e 67 20 72 6f 77 73 2e 20 4f 6e  turning rows. On
434b2 63 65 20 74 68 69 73 0d 0a 2a 2a 20 66 75 6e 63  ce this..** func
434b3 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 61  tion has been ca
434b4 6c 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  lled successfull
434b5 79 20 6f 6e 20 61 6e 20 46 74 73 33 50 68 72 61  y on an Fts3Phra
434b6 73 65 2c 20 69 74 20 6d 61 79 20 62 65 0d 0a 2a  se, it may be..*
434b7 2a 20 75 73 65 64 20 77 69 74 68 20 66 74 73 33  * used with fts3
434b8 45 76 61 6c 50 68 72 61 73 65 4e 65 78 74 28 29  EvalPhraseNext()
434b9 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
434ba 75 67 68 20 74 68 65 20 6d 61 74 63 68 69 6e 67  ugh the matching
434bb 20 64 6f 63 69 64 73 2e 0d 0a 2a 2a 0d 0a 2a 2a   docids...**..**
434bc 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 62 4f   If parameter bO
434bd 70 74 4f 6b 20 69 73 20 74 72 75 65 2c 20 74 68  ptOk is true, th
434be 65 6e 20 74 68 65 20 70 68 72 61 73 65 20 6d 61  en the phrase ma
434bf 79 20 28 6f 72 20 6d 61 79 20 6e 6f 74 29 20 75  y (or may not) u
434c0 73 65 20 74 68 65 0d 0a 2a 2a 20 69 6e 63 72 65  se the..** incre
434c1 6d 65 6e 74 61 6c 20 6c 6f 61 64 69 6e 67 20 73  mental loading s
434c2 74 72 61 74 65 67 79 2e 20 4f 74 68 65 72 77 69  trategy. Otherwi
434c3 73 65 2c 20 74 68 65 20 65 6e 74 69 72 65 20 64  se, the entire d
434c4 6f 63 6c 69 73 74 20 69 73 20 6c 6f 61 64 65 64  oclist is loaded
434c5 20 69 6e 74 6f 0d 0a 2a 2a 20 6d 65 6d 6f 72 79   into..** memory
434c6 20 77 69 74 68 69 6e 20 74 68 69 73 20 63 61 6c   within this cal
434c7 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 49 54  l...**..** SQLIT
434c8 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
434c9 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63   if no error occ
434ca 75 72 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61  urs, otherwise a
434cb 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
434cc 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ode...*/..static
434cd 20 69 6e 74 20 66 74 73 33 45 76 61 6c 50 68 72   int fts3EvalPhr
434ce 61 73 65 53 74 61 72 74 28 46 74 73 33 43 75 72  aseStart(Fts3Cur
434cf 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20 62  sor *pCsr, int b
434d0 4f 70 74 4f 6b 2c 20 46 74 73 33 50 68 72 61 73  OptOk, Fts3Phras
434d1 65 20 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 72 63  e *p){..  int rc
434d2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
434d3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
434d4 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 46 74  or code */..  Ft
434d5 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70  s3PhraseToken *p
434d6 46 69 72 73 74 20 3d 20 26 70 2d 3e 61 54 6f 6b  First = &p->aTok
434d7 65 6e 5b 30 5d 3b 0d 0a 20 20 46 74 73 33 54 61  en[0];..  Fts3Ta
434d8 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 46 74 73  ble *pTab = (Fts
434d9 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62  3Table *)pCsr->b
434da 61 73 65 2e 70 56 74 61 62 3b 0d 0a 0d 0a 20 20  ase.pVtab;....  
434db 69 66 28 20 70 43 73 72 2d 3e 62 44 65 73 63 3d  if( pCsr->bDesc=
434dc 3d 70 54 61 62 2d 3e 62 44 65 73 63 49 64 78 20  =pTab->bDescIdx 
434dd 0d 0a 20 20 20 26 26 20 62 4f 70 74 4f 6b 3d 3d  ..   && bOptOk==
434de 31 20 0d 0a 20 20 20 26 26 20 70 2d 3e 6e 54 6f  1 ..   && p->nTo
434df 6b 65 6e 3d 3d 31 20 0d 0a 20 20 20 26 26 20 70  ken==1 ..   && p
434e0 46 69 72 73 74 2d 3e 70 53 65 67 63 73 72 20 0d  First->pSegcsr .
434e1 0a 20 20 20 26 26 20 70 46 69 72 73 74 2d 3e 70  .   && pFirst->p
434e2 53 65 67 63 73 72 2d 3e 62 4c 6f 6f 6b 75 70 20  Segcsr->bLookup 
434e3 0d 0a 20 20 20 26 26 20 70 46 69 72 73 74 2d 3e  ..   && pFirst->
434e4 62 46 69 72 73 74 3d 3d 30 0d 0a 20 20 29 7b 0d  bFirst==0..  ){.
434e5 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20  .    /* Use the 
434e6 69 6e 63 72 65 6d 65 6e 74 61 6c 20 61 70 70 72  incremental appr
434e7 6f 61 63 68 2e 20 2a 2f 0d 0a 20 20 20 20 69 6e  oach. */..    in
434e8 74 20 69 43 6f 6c 20 3d 20 28 70 2d 3e 69 43 6f  t iCol = (p->iCo
434e9 6c 75 6d 6e 20 3e 3d 20 70 54 61 62 2d 3e 6e 43  lumn >= pTab->nC
434ea 6f 6c 75 6d 6e 20 3f 20 2d 31 20 3a 20 70 2d 3e  olumn ? -1 : p->
434eb 69 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20 20 20 72  iColumn);..    r
434ec 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 4d  c = sqlite3Fts3M
434ed 73 72 49 6e 63 72 53 74 61 72 74 28 0d 0a 20 20  srIncrStart(..  
434ee 20 20 20 20 20 20 70 54 61 62 2c 20 70 46 69 72        pTab, pFir
434ef 73 74 2d 3e 70 53 65 67 63 73 72 2c 20 69 43 6f  st->pSegcsr, iCo
434f0 6c 2c 20 70 46 69 72 73 74 2d 3e 7a 2c 20 70 46  l, pFirst->z, pF
434f1 69 72 73 74 2d 3e 6e 29 3b 0d 0a 20 20 20 20 70  irst->n);..    p
434f2 2d 3e 62 49 6e 63 72 20 3d 20 31 3b 0d 0a 0d 0a  ->bIncr = 1;....
434f3 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a    }else{..    /*
434f4 20 4c 6f 61 64 20 74 68 65 20 66 75 6c 6c 20 64   Load the full d
434f5 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65 20 70  oclist for the p
434f6 68 72 61 73 65 20 69 6e 74 6f 20 6d 65 6d 6f 72  hrase into memor
434f7 79 2e 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20  y. */..    rc = 
434f8 66 74 73 33 45 76 61 6c 50 68 72 61 73 65 4c 6f  fts3EvalPhraseLo
434f9 61 64 28 70 43 73 72 2c 20 70 29 3b 0d 0a 20 20  ad(pCsr, p);..  
434fa 20 20 70 2d 3e 62 49 6e 63 72 20 3d 20 30 3b 0d    p->bIncr = 0;.
434fb 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74  .  }....  assert
434fc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
434fd 7c 7c 20 70 2d 3e 6e 54 6f 6b 65 6e 3c 31 20 7c  || p->nToken<1 |
434fe 7c 20 70 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 70  | p->aToken[0].p
434ff 53 65 67 63 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e  Segcsr==0 || p->
43500 62 49 6e 63 72 20 29 3b 0d 0a 20 20 72 65 74 75  bIncr );..  retu
43501 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
43502 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
43503 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 74 65  n is used to ite
43504 72 61 74 65 20 62 61 63 6b 77 61 72 64 73 20 28  rate backwards (
43505 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 74 6f 20  from the end to 
43506 73 74 61 72 74 29 20 0d 0a 2a 2a 20 74 68 72 6f  start) ..** thro
43507 75 67 68 20 64 6f 63 6c 69 73 74 73 2e 20 49 74  ugh doclists. It
43508 20 69 73 20 75 73 65 64 20 62 79 20 74 68 69 73   is used by this
43509 20 6d 6f 64 75 6c 65 20 74 6f 20 69 74 65 72 61   module to itera
4350a 74 65 20 74 68 72 6f 75 67 68 20 70 68 72 61 73  te through phras
4350b 65 0d 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20 69  e..** doclists i
4350c 6e 20 72 65 76 65 72 73 65 20 61 6e 64 20 62 79  n reverse and by
4350d 20 74 68 65 20 66 74 73 33 5f 77 72 69 74 65 2e   the fts3_write.
4350e 63 20 6d 6f 64 75 6c 65 20 74 6f 20 69 74 65 72  c module to iter
4350f 61 74 65 20 74 68 72 6f 75 67 68 0d 0a 2a 2a 20  ate through..** 
43510 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 6c 69  pending-terms li
43511 73 74 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67  sts when writing
43512 20 74 6f 20 64 61 74 61 62 61 73 65 73 20 77 69   to databases wi
43513 74 68 20 22 6f 72 64 65 72 3d 64 65 73 63 22 2e  th "order=desc".
43514 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 64 6f 63  ..**..** The doc
43515 6c 69 73 74 20 6d 61 79 20 62 65 20 73 6f 72 74  list may be sort
43516 65 64 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  ed in ascending 
43517 28 70 61 72 61 6d 65 74 65 72 20 62 44 65 73 63  (parameter bDesc
43518 49 64 78 3d 3d 30 29 20 6f 72 20 0d 0a 2a 2a 20  Idx==0) or ..** 
43519 64 65 73 63 65 6e 64 69 6e 67 20 28 70 61 72 61  descending (para
4351a 6d 65 74 65 72 20 62 44 65 73 63 49 64 78 3d 3d  meter bDescIdx==
4351b 31 29 20 6f 72 64 65 72 20 6f 66 20 64 6f 63 69  1) order of doci
4351c 64 2e 20 52 65 67 61 72 64 6c 65 73 73 2c 20 74  d. Regardless, t
4351d 68 69 73 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his..** function
4351e 20 69 74 65 72 61 74 65 73 20 66 72 6f 6d 20 74   iterates from t
4351f 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 6f  he end of the do
43520 63 6c 69 73 74 20 74 6f 20 74 68 65 20 62 65 67  clist to the beg
43521 69 6e 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c  inning...*/..SQL
43522 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
43523 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f 63 6c   sqlite3Fts3Docl
43524 69 73 74 50 72 65 76 28 0d 0a 20 20 69 6e 74 20  istPrev(..  int 
43525 62 44 65 73 63 49 64 78 2c 20 20 20 20 20 20 20  bDescIdx,       
43526 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
43527 72 75 65 20 69 66 20 74 68 65 20 64 6f 63 6c 69  rue if the docli
43528 73 74 20 69 73 20 64 65 73 63 20 2a 2f 0d 0a 20  st is desc */.. 
43529 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 2c   char *aDoclist,
4352a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4352b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 65   /* Pointer to e
4352c 6e 74 69 72 65 20 64 6f 63 6c 69 73 74 20 2a 2f  ntire doclist */
4352d 0d 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74  ..  int nDoclist
4352e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4352f 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
43530 20 61 44 6f 63 6c 69 73 74 20 69 6e 20 62 79 74   aDoclist in byt
43531 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a  es */..  char **
43532 70 70 49 74 65 72 2c 20 20 20 20 20 20 20 20 20  ppIter,         
43533 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
43534 55 54 3a 20 49 74 65 72 61 74 6f 72 20 70 6f 69  UT: Iterator poi
43535 6e 74 65 72 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  nter */..  sqlit
43536 65 33 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63 69  e3_int64 *piDoci
43537 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e  d,         /* IN
43538 2f 4f 55 54 3a 20 44 6f 63 69 64 20 70 6f 69 6e  /OUT: Docid poin
43539 74 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70  ter */..  int *p
4353a 6e 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20  nList,          
4353b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
4353c 4f 55 54 3a 20 4c 69 73 74 20 6c 65 6e 67 74 68  OUT: List length
4353d 20 70 6f 69 6e 74 65 72 20 2a 2f 0d 0a 20 20 75   pointer */..  u
4353e 38 20 2a 70 62 45 6f 66 20 20 20 20 20 20 20 20  8 *pbEof        
4353f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43540 2a 20 4f 55 54 3a 20 45 6e 64 2d 6f 66 2d 66 69  * OUT: End-of-fi
43541 6c 65 20 66 6c 61 67 20 2a 2f 0d 0a 29 7b 0d 0a  le flag */..){..
43542 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 49    char *p = *ppI
43543 74 65 72 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  ter;....  assert
43544 28 20 6e 44 6f 63 6c 69 73 74 3e 30 20 29 3b 0d  ( nDoclist>0 );.
43545 0a 20 20 61 73 73 65 72 74 28 20 2a 70 62 45 6f  .  assert( *pbEo
43546 66 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72  f==0 );..  asser
43547 74 28 20 70 20 7c 7c 20 2a 70 69 44 6f 63 69 64  t( p || *piDocid
43548 3d 3d 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  ==0 );..  assert
43549 28 20 21 70 20 7c 7c 20 28 70 3e 61 44 6f 63 6c  ( !p || (p>aDocl
4354a 69 73 74 20 26 26 20 70 3c 26 61 44 6f 63 6c 69  ist && p<&aDocli
4354b 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 29 20 29 3b  st[nDoclist]) );
4354c 0d 0a 0d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  ....  if( p==0 )
4354d 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  {..    sqlite3_i
4354e 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20 30 3b  nt64 iDocid = 0;
4354f 0d 0a 20 20 20 20 63 68 61 72 20 2a 70 4e 65 78  ..    char *pNex
43550 74 20 3d 20 30 3b 0d 0a 20 20 20 20 63 68 61 72  t = 0;..    char
43551 20 2a 70 44 6f 63 69 64 20 3d 20 61 44 6f 63 6c   *pDocid = aDocl
43552 69 73 74 3b 0d 0a 20 20 20 20 63 68 61 72 20 2a  ist;..    char *
43553 70 45 6e 64 20 3d 20 26 61 44 6f 63 6c 69 73 74  pEnd = &aDoclist
43554 5b 6e 44 6f 63 6c 69 73 74 5d 3b 0d 0a 20 20 20  [nDoclist];..   
43555 20 69 6e 74 20 69 4d 75 6c 20 3d 20 31 3b 0d 0a   int iMul = 1;..
43556 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 70 44 6f  ..    while( pDo
43557 63 69 64 3c 70 45 6e 64 20 29 7b 0d 0a 20 20 20  cid<pEnd ){..   
43558 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
43559 20 69 44 65 6c 74 61 3b 0d 0a 20 20 20 20 20 20   iDelta;..      
4355a 70 44 6f 63 69 64 20 2b 3d 20 73 71 6c 69 74 65  pDocid += sqlite
4355b 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
4355c 44 6f 63 69 64 2c 20 26 69 44 65 6c 74 61 29 3b  Docid, &iDelta);
4355d 0d 0a 20 20 20 20 20 20 69 44 6f 63 69 64 20 2b  ..      iDocid +
4355e 3d 20 28 69 4d 75 6c 20 2a 20 69 44 65 6c 74 61  = (iMul * iDelta
4355f 29 3b 0d 0a 20 20 20 20 20 20 70 4e 65 78 74 20  );..      pNext 
43560 3d 20 70 44 6f 63 69 64 3b 0d 0a 20 20 20 20 20  = pDocid;..     
43561 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79   fts3PoslistCopy
43562 28 30 2c 20 26 70 44 6f 63 69 64 29 3b 0d 0a 20  (0, &pDocid);.. 
43563 20 20 20 20 20 77 68 69 6c 65 28 20 70 44 6f 63       while( pDoc
43564 69 64 3c 70 45 6e 64 20 26 26 20 2a 70 44 6f 63  id<pEnd && *pDoc
43565 69 64 3d 3d 30 20 29 20 70 44 6f 63 69 64 2b 2b  id==0 ) pDocid++
43566 3b 0d 0a 20 20 20 20 20 20 69 4d 75 6c 20 3d 20  ;..      iMul = 
43567 28 62 44 65 73 63 49 64 78 20 3f 20 2d 31 20 3a  (bDescIdx ? -1 :
43568 20 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20   1);..    }.... 
43569 20 20 20 2a 70 6e 4c 69 73 74 20 3d 20 70 45 6e     *pnList = pEn
4356a 64 20 2d 20 70 4e 65 78 74 3b 0d 0a 20 20 20 20  d - pNext;..    
4356b 2a 70 70 49 74 65 72 20 3d 20 70 4e 65 78 74 3b  *ppIter = pNext;
4356c 0d 0a 20 20 20 20 2a 70 69 44 6f 63 69 64 20 3d  ..    *piDocid =
4356d 20 69 44 6f 63 69 64 3b 0d 0a 20 20 7d 65 6c 73   iDocid;..  }els
4356e 65 7b 0d 0a 20 20 20 20 69 6e 74 20 69 4d 75 6c  e{..    int iMul
4356f 20 3d 20 28 62 44 65 73 63 49 64 78 20 3f 20 2d   = (bDescIdx ? -
43570 31 20 3a 20 31 29 3b 0d 0a 20 20 20 20 73 71 6c  1 : 1);..    sql
43571 69 74 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74  ite3_int64 iDelt
43572 61 3b 0d 0a 20 20 20 20 66 74 73 33 47 65 74 52  a;..    fts3GetR
43573 65 76 65 72 73 65 56 61 72 69 6e 74 28 26 70 2c  everseVarint(&p,
43574 20 61 44 6f 63 6c 69 73 74 2c 20 26 69 44 65 6c   aDoclist, &iDel
43575 74 61 29 3b 0d 0a 20 20 20 20 2a 70 69 44 6f 63  ta);..    *piDoc
43576 69 64 20 2d 3d 20 28 69 4d 75 6c 20 2a 20 69 44  id -= (iMul * iD
43577 65 6c 74 61 29 3b 0d 0a 0d 0a 20 20 20 20 69 66  elta);....    if
43578 28 20 70 3d 3d 61 44 6f 63 6c 69 73 74 20 29 7b  ( p==aDoclist ){
43579 0d 0a 20 20 20 20 20 20 2a 70 62 45 6f 66 20 3d  ..      *pbEof =
4357a 20 31 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   1;..    }else{.
4357b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 53 61  .      char *pSa
4357c 76 65 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 66  ve = p;..      f
4357d 74 73 33 52 65 76 65 72 73 65 50 6f 73 6c 69 73  ts3ReversePoslis
4357e 74 28 61 44 6f 63 6c 69 73 74 2c 20 26 70 29 3b  t(aDoclist, &p);
4357f 0d 0a 20 20 20 20 20 20 2a 70 6e 4c 69 73 74 20  ..      *pnList 
43580 3d 20 28 70 53 61 76 65 20 2d 20 70 29 3b 0d 0a  = (pSave - p);..
43581 20 20 20 20 7d 0d 0a 20 20 20 20 2a 70 70 49 74      }..    *ppIt
43582 65 72 20 3d 20 70 3b 0d 0a 20 20 7d 0d 0a 7d 0d  er = p;..  }..}.
43583 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 74 74 65 6d 70  .../*..** Attemp
43584 74 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 68  t to move the ph
43585 72 61 73 65 20 69 74 65 72 61 74 6f 72 20 74 6f  rase iterator to
43586 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
43587 78 74 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 69  xt matching doci
43588 64 2e 20 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72  d. ..** If an er
43589 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
4358a 72 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  rn an SQLite err
4358b 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69  or code. Otherwi
4358c 73 65 2c 20 72 65 74 75 72 6e 20 0d 0a 2a 2a 20  se, return ..** 
4358d 53 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a  SQLITE_OK...**..
4358e 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  ** If there is n
4358f 6f 20 22 6e 65 78 74 22 20 65 6e 74 72 79 20 61  o "next" entry a
43590 6e 64 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  nd no error occu
43591 72 73 2c 20 74 68 65 6e 20 2a 70 62 45 6f 66 20  rs, then *pbEof 
43592 69 73 20 73 65 74 20 74 6f 0d 0a 2a 2a 20 31 20  is set to..** 1 
43593 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
43594 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
43595 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  no error occurs 
43596 61 6e 64 20 74 68 65 20 69 74 65 72 61 74 6f 72  and the iterator
43597 20 69 73 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66   is..** successf
43598 75 6c 6c 79 20 61 64 76 61 6e 63 65 64 2c 20 2a  ully advanced, *
43599 70 62 45 6f 66 20 69 73 20 73 65 74 20 74 6f 20  pbEof is set to 
4359a 30 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  0...*/..static i
4359b 6e 74 20 66 74 73 33 45 76 61 6c 50 68 72 61 73  nt fts3EvalPhras
4359c 65 4e 65 78 74 28 0d 0a 20 20 46 74 73 33 43 75  eNext(..  Fts3Cu
4359d 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
4359e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
4359f 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a   Cursor handle *
435a0 2f 0d 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  /..  Fts3Phrase 
435a1 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
435a2 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 6f       /* Phrase o
435a3 62 6a 65 63 74 20 74 6f 20 61 64 76 61 6e 63 65  bject to advance
435a4 20 74 6f 20 6e 65 78 74 20 64 6f 63 69 64 20 2a   to next docid *
435a5 2f 0d 0a 20 20 75 38 20 2a 70 62 45 6f 66 20 20  /..  u8 *pbEof  
435a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435a7 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74       /* OUT: Set
435a8 20 74 6f 20 31 20 69 66 20 45 4f 46 20 2a 2f 0d   to 1 if EOF */.
435a9 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  .){..  int rc = 
435aa 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 46 74  SQLITE_OK;..  Ft
435ab 73 33 44 6f 63 6c 69 73 74 20 2a 70 44 4c 20 3d  s3Doclist *pDL =
435ac 20 26 70 2d 3e 64 6f 63 6c 69 73 74 3b 0d 0a 20   &p->doclist;.. 
435ad 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62   Fts3Table *pTab
435ae 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
435af 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
435b0 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 2d 3e 62 49  ;....  if( p->bI
435b1 6e 63 72 20 29 7b 0d 0a 20 20 20 20 61 73 73 65  ncr ){..    asse
435b2 72 74 28 20 70 2d 3e 6e 54 6f 6b 65 6e 3d 3d 31  rt( p->nToken==1
435b3 20 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28   );..    assert(
435b4 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64   pDL->pNextDocid
435b5 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d  ==0 );..    rc =
435b6 20 73 71 6c 69 74 65 33 46 74 73 33 4d 73 72 49   sqlite3Fts3MsrI
435b7 6e 63 72 4e 65 78 74 28 70 54 61 62 2c 20 70 2d  ncrNext(pTab, p-
435b8 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 70 53 65 67 63  >aToken[0].pSegc
435b9 73 72 2c 20 0d 0a 20 20 20 20 20 20 20 20 26 70  sr, ..        &p
435ba 44 4c 2d 3e 69 44 6f 63 69 64 2c 20 26 70 44 4c  DL->iDocid, &pDL
435bb 2d 3e 70 4c 69 73 74 2c 20 26 70 44 4c 2d 3e 6e  ->pList, &pDL->n
435bc 4c 69 73 74 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  List..    );..  
435bd 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
435be 5f 4f 4b 20 26 26 20 21 70 44 4c 2d 3e 70 4c 69  _OK && !pDL->pLi
435bf 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 62  st ){..      *pb
435c0 45 6f 66 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  Eof = 1;..    }.
435c1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 73  .  }else if( pCs
435c2 72 2d 3e 62 44 65 73 63 21 3d 70 54 61 62 2d 3e  r->bDesc!=pTab->
435c3 62 44 65 73 63 49 64 78 20 26 26 20 70 44 4c 2d  bDescIdx && pDL-
435c4 3e 6e 41 6c 6c 20 29 7b 0d 0a 20 20 20 20 73 71  >nAll ){..    sq
435c5 6c 69 74 65 33 46 74 73 33 44 6f 63 6c 69 73 74  lite3Fts3Doclist
435c6 50 72 65 76 28 70 54 61 62 2d 3e 62 44 65 73 63  Prev(pTab->bDesc
435c7 49 64 78 2c 20 70 44 4c 2d 3e 61 41 6c 6c 2c 20  Idx, pDL->aAll, 
435c8 70 44 4c 2d 3e 6e 41 6c 6c 2c 20 0d 0a 20 20 20  pDL->nAll, ..   
435c9 20 20 20 20 20 26 70 44 4c 2d 3e 70 4e 65 78 74       &pDL->pNext
435ca 44 6f 63 69 64 2c 20 26 70 44 4c 2d 3e 69 44 6f  Docid, &pDL->iDo
435cb 63 69 64 2c 20 26 70 44 4c 2d 3e 6e 4c 69 73 74  cid, &pDL->nList
435cc 2c 20 70 62 45 6f 66 0d 0a 20 20 20 20 29 3b 0d  , pbEof..    );.
435cd 0a 20 20 20 20 70 44 4c 2d 3e 70 4c 69 73 74 20  .    pDL->pList 
435ce 3d 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69  = pDL->pNextDoci
435cf 64 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  d;..  }else{..  
435d0 20 20 63 68 61 72 20 2a 70 49 74 65 72 3b 20 20    char *pIter;  
435d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
435d3 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72  d to iterate thr
435d4 6f 75 67 68 20 61 41 6c 6c 20 2a 2f 0d 0a 20 20  ough aAll */..  
435d5 20 20 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 26    char *pEnd = &
435d6 70 44 4c 2d 3e 61 41 6c 6c 5b 70 44 4c 2d 3e 6e  pDL->aAll[pDL->n
435d7 41 6c 6c 5d 3b 20 20 20 20 20 2f 2a 20 31 20 62  All];     /* 1 b
435d8 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20  yte past end of 
435d9 61 41 6c 6c 20 2a 2f 0d 0a 20 20 20 20 69 66 28  aAll */..    if(
435da 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64   pDL->pNextDocid
435db 20 29 7b 0d 0a 20 20 20 20 20 20 70 49 74 65 72   ){..      pIter
435dc 20 3d 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63   = pDL->pNextDoc
435dd 69 64 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  id;..    }else{.
435de 0a 20 20 20 20 20 20 70 49 74 65 72 20 3d 20 70  .      pIter = p
435df 44 4c 2d 3e 61 41 6c 6c 3b 0d 0a 20 20 20 20 7d  DL->aAll;..    }
435e0 0d 0a 0d 0a 20 20 20 20 69 66 28 20 70 49 74 65  ....    if( pIte
435e1 72 3e 3d 70 45 6e 64 20 29 7b 0d 0a 20 20 20 20  r>=pEnd ){..    
435e2 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 6c 72    /* We have alr
435e3 65 61 64 79 20 72 65 61 63 68 65 64 20 74 68 65  eady reached the
435e4 20 65 6e 64 20 6f 66 20 74 68 69 73 20 64 6f 63   end of this doc
435e5 6c 69 73 74 2e 20 45 4f 46 2e 20 2a 2f 0d 0a 20  list. EOF. */.. 
435e6 20 20 20 20 20 2a 70 62 45 6f 66 20 3d 20 31 3b       *pbEof = 1;
435e7 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
435e8 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
435e9 34 20 69 44 65 6c 74 61 3b 0d 0a 20 20 20 20 20  4 iDelta;..     
435ea 20 70 49 74 65 72 20 2b 3d 20 73 71 6c 69 74 65   pIter += sqlite
435eb 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
435ec 49 74 65 72 2c 20 26 69 44 65 6c 74 61 29 3b 0d  Iter, &iDelta);.
435ed 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 2d  .      if( pTab-
435ee 3e 62 44 65 73 63 49 64 78 3d 3d 30 20 7c 7c 20  >bDescIdx==0 || 
435ef 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64 3d  pDL->pNextDocid=
435f0 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  =0 ){..        p
435f1 44 4c 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44  DL->iDocid += iD
435f2 65 6c 74 61 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  elta;..      }el
435f3 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 70 44 4c  se{..        pDL
435f4 2d 3e 69 44 6f 63 69 64 20 2d 3d 20 69 44 65 6c  ->iDocid -= iDel
435f5 74 61 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  ta;..      }..  
435f6 20 20 20 20 70 44 4c 2d 3e 70 4c 69 73 74 20 3d      pDL->pList =
435f7 20 70 49 74 65 72 3b 0d 0a 20 20 20 20 20 20 66   pIter;..      f
435f8 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30  ts3PoslistCopy(0
435f9 2c 20 26 70 49 74 65 72 29 3b 0d 0a 20 20 20 20  , &pIter);..    
435fa 20 20 70 44 4c 2d 3e 6e 4c 69 73 74 20 3d 20 28    pDL->nList = (
435fb 70 49 74 65 72 20 2d 20 70 44 4c 2d 3e 70 4c 69  pIter - pDL->pLi
435fc 73 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  st);....      /*
435fd 20 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74   pIter now point
435fe 73 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  s just past the 
435ff 30 78 30 30 20 74 68 61 74 20 74 65 72 6d 69 6e  0x00 that termin
43600 61 74 65 73 20 74 68 65 20 70 6f 73 69 74 69 6f  ates the positio
43601 6e 2d 0d 0a 20 20 20 20 20 20 2a 2a 20 6c 69 73  n-..      ** lis
43602 74 20 66 6f 72 20 64 6f 63 75 6d 65 6e 74 20 70  t for document p
43603 44 4c 2d 3e 69 44 6f 63 69 64 2e 20 48 6f 77 65  DL->iDocid. Howe
43604 76 65 72 2c 20 69 66 20 74 68 69 73 20 70 6f 73  ver, if this pos
43605 69 74 69 6f 6e 2d 6c 69 73 74 20 77 61 73 0d 0a  ition-list was..
43606 20 20 20 20 20 20 2a 2a 20 65 64 69 74 65 64 20        ** edited 
43607 69 6e 20 70 6c 61 63 65 20 62 79 20 66 74 73 33  in place by fts3
43608 45 76 61 6c 4e 65 61 72 54 72 69 6d 28 29 2c 20  EvalNearTrim(), 
43609 74 68 65 6e 20 70 49 74 65 72 20 6d 61 79 20 6e  then pIter may n
4360a 6f 74 20 61 63 74 75 61 6c 6c 79 0d 0a 20 20 20  ot actually..   
4360b 20 20 20 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74     ** point to t
4360c 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
4360d 6e 65 78 74 20 64 6f 63 69 64 20 76 61 6c 75 65  next docid value
4360e 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
4360f 6c 69 6e 65 20 64 65 61 6c 73 0d 0a 20 20 20 20  line deals..    
43610 20 20 2a 2a 20 77 69 74 68 20 74 68 69 73 20 63    ** with this c
43611 61 73 65 20 62 79 20 61 64 76 61 6e 63 69 6e 67  ase by advancing
43612 20 70 49 74 65 72 20 70 61 73 74 20 74 68 65 20   pIter past the 
43613 7a 65 72 6f 2d 70 61 64 64 69 6e 67 20 61 64 64  zero-padding add
43614 65 64 20 62 79 0d 0a 20 20 20 20 20 20 2a 2a 20  ed by..      ** 
43615 66 74 73 33 45 76 61 6c 4e 65 61 72 54 72 69 6d  fts3EvalNearTrim
43616 28 29 2e 20 20 2a 2f 0d 0a 20 20 20 20 20 20 77  ().  */..      w
43617 68 69 6c 65 28 20 70 49 74 65 72 3c 70 45 6e 64  hile( pIter<pEnd
43618 20 26 26 20 2a 70 49 74 65 72 3d 3d 30 20 29 20   && *pIter==0 ) 
43619 70 49 74 65 72 2b 2b 3b 0d 0a 0d 0a 20 20 20 20  pIter++;....    
4361a 20 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69    pDL->pNextDoci
4361b 64 20 3d 20 70 49 74 65 72 3b 0d 0a 20 20 20 20  d = pIter;..    
4361c 20 20 61 73 73 65 72 74 28 20 70 49 74 65 72 3e    assert( pIter>
4361d 3d 26 70 44 4c 2d 3e 61 41 6c 6c 5b 70 44 4c 2d  =&pDL->aAll[pDL-
4361e 3e 6e 41 6c 6c 5d 20 7c 7c 20 2a 70 49 74 65 72  >nAll] || *pIter
4361f 20 29 3b 0d 0a 20 20 20 20 20 20 2a 70 62 45 6f   );..      *pbEo
43620 66 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20  f = 0;..    }.. 
43621 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72   }....  return r
43622 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 0d  c;..}..../*..**.
43623 0a 2a 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e  .** If *pRc is n
43624 6f 74 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68 65  ot SQLITE_OK whe
43625 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
43626 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73  is called, it is
43627 20 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 20 4f 74   a no-op...** Ot
43628 68 65 72 77 69 73 65 2c 20 66 74 73 33 45 76 61  herwise, fts3Eva
43629 6c 50 68 72 61 73 65 53 74 61 72 74 28 29 20 69  lPhraseStart() i
4362a 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6c 6c 20  s called on all 
4362b 70 68 72 61 73 65 73 20 77 69 74 68 69 6e 20 74  phrases within t
4362c 68 65 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  he..** expressio
4362d 6e 2e 20 41 6c 73 6f 20 74 68 65 20 46 74 73 33  n. Also the Fts3
4362e 45 78 70 72 2e 62 44 65 66 65 72 72 65 64 20 76  Expr.bDeferred v
4362f 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
43630 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 79 0d 0a  o true for any..
43631 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66  ** expressions f
43632 6f 72 20 77 68 69 63 68 20 61 6c 6c 20 64 65 73  or which all des
43633 63 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20 61  cendent tokens a
43634 72 65 20 64 65 66 65 72 72 65 64 2e 0d 0a 2a 2a  re deferred...**
43635 0d 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  ..** If paramete
43636 72 20 62 4f 70 74 4f 6b 20 69 73 20 7a 65 72 6f  r bOptOk is zero
43637 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61  , then it is gua
43638 72 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65  ranteed that the
43639 0d 0a 2a 2a 20 46 74 73 33 50 68 72 61 73 65 2e  ..** Fts3Phrase.
4363a 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 2f 6e 41 6c  doclist.aAll/nAl
4363b 6c 20 76 61 72 69 61 62 6c 65 73 20 63 6f 6e 74  l variables cont
4363c 61 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 64  ain the entire d
4363d 6f 63 6c 69 73 74 20 66 6f 72 0d 0a 2a 2a 20 65  oclist for..** e
4363e 61 63 68 20 70 68 72 61 73 65 20 69 6e 20 74 68  ach phrase in th
4363f 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28 73 75  e expression (su
43640 62 6a 65 63 74 20 74 6f 20 64 65 66 65 72 72 65  bject to deferre
43641 64 20 74 6f 6b 65 6e 20 70 72 6f 63 65 73 73 69  d token processi
43642 6e 67 29 2e 0d 0a 2a 2a 20 4f 72 2c 20 69 66 20  ng)...** Or, if 
43643 62 4f 70 74 4f 6b 20 69 73 20 6e 6f 6e 2d 7a 65  bOptOk is non-ze
43644 72 6f 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20  ro, then one or 
43645 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 77 69 74 68  more tokens with
43646 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
43647 6e 0d 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 61  n..** may be loa
43648 64 65 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c  ded incrementall
43649 79 2c 20 6d 65 61 6e 69 6e 67 20 64 6f 63 6c 69  y, meaning docli
4364a 73 74 2e 61 41 6c 6c 2f 6e 41 6c 6c 20 69 73 20  st.aAll/nAll is 
4364b 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 0d 0a  not available...
4364c 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  **..** If an err
4364d 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
4364e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
4364f 2a 70 52 63 20 69 73 20 73 65 74 20 74 6f 20 61  *pRc is set to a
43650 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 0d 0a  n SQLite error..
43651 2a 2a 20 63 6f 64 65 20 62 65 66 6f 72 65 20 72  ** code before r
43652 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73  eturning...*/..s
43653 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 45  tatic void fts3E
43654 76 61 6c 53 74 61 72 74 52 65 61 64 65 72 73 28  valStartReaders(
43655 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a  ..  Fts3Cursor *
43656 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20 20  pCsr,           
43657 20 20 20 20 2f 2a 20 46 54 53 20 43 75 72 73 6f      /* FTS Curso
43658 72 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46  r handle */..  F
43659 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
4365a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4365b 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  * Expression to 
4365c 69 6e 69 74 69 61 6c 69 7a 65 20 70 68 72 61 73  initialize phras
4365d 65 73 20 69 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  es in */..  int 
4365e 62 4f 70 74 4f 6b 2c 20 20 20 20 20 20 20 20 20  bOptOk,         
4365f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
43660 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 69 6e  rue to enable in
43661 63 72 65 6d 65 6e 74 61 6c 20 6c 6f 61 64 69 6e  cremental loadin
43662 67 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 52 63  g */..  int *pRc
43663 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43664 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
43665 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a 2f  T: Error code */
43666 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 70 45 78 70  ..){..  if( pExp
43667 72 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  r && SQLITE_OK==
43668 2a 70 52 63 20 29 7b 0d 0a 20 20 20 20 69 66 28  *pRc ){..    if(
43669 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46   pExpr->eType==F
4366a 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
4366b 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0d  {..      int i;.
4366c 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  .      int nToke
4366d 6e 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61  n = pExpr->pPhra
4366e 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0d 0a 20 20 20  se->nToken;..   
4366f 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54     for(i=0; i<nT
43670 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  oken; i++){..   
43671 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
43672 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
43673 69 5d 2e 70 44 65 66 65 72 72 65 64 3d 3d 30 20  i].pDeferred==0 
43674 29 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20  ) break;..      
43675 7d 0d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  }..      pExpr->
43676 62 44 65 66 65 72 72 65 64 20 3d 20 28 69 3d 3d  bDeferred = (i==
43677 6e 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20  nToken);..      
43678 2a 70 52 63 20 3d 20 66 74 73 33 45 76 61 6c 50  *pRc = fts3EvalP
43679 68 72 61 73 65 53 74 61 72 74 28 70 43 73 72 2c  hraseStart(pCsr,
4367a 20 62 4f 70 74 4f 6b 2c 20 70 45 78 70 72 2d 3e   bOptOk, pExpr->
4367b 70 50 68 72 61 73 65 29 3b 0d 0a 20 20 20 20 7d  pPhrase);..    }
4367c 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 66 74 73  else{..      fts
4367d 33 45 76 61 6c 53 74 61 72 74 52 65 61 64 65 72  3EvalStartReader
4367e 73 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70  s(pCsr, pExpr->p
4367f 4c 65 66 74 2c 20 62 4f 70 74 4f 6b 2c 20 70 52  Left, bOptOk, pR
43680 63 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 45  c);..      fts3E
43681 76 61 6c 53 74 61 72 74 52 65 61 64 65 72 73 28  valStartReaders(
43682 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 52 69  pCsr, pExpr->pRi
43683 67 68 74 2c 20 62 4f 70 74 4f 6b 2c 20 70 52 63  ght, bOptOk, pRc
43684 29 3b 0d 0a 20 20 20 20 20 20 70 45 78 70 72 2d  );..      pExpr-
43685 3e 62 44 65 66 65 72 72 65 64 20 3d 20 28 70 45  >bDeferred = (pE
43686 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 62 44 65 66  xpr->pLeft->bDef
43687 65 72 72 65 64 20 26 26 20 70 45 78 70 72 2d 3e  erred && pExpr->
43688 70 52 69 67 68 74 2d 3e 62 44 65 66 65 72 72 65  pRight->bDeferre
43689 64 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  d);..    }..  }.
4368a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20  .}..../*..** An 
4368b 61 72 72 61 79 20 6f 66 20 74 68 65 20 66 6f 6c  array of the fol
4368c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
4368d 73 20 69 73 20 61 73 73 65 6d 62 6c 65 64 20 61  s is assembled a
4368e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 72  s part of the pr
4368f 6f 63 65 73 73 0d 0a 2a 2a 20 6f 66 20 73 65 6c  ocess..** of sel
43690 65 63 74 69 6e 67 20 74 6f 6b 65 6e 73 20 74 6f  ecting tokens to
43691 20 64 65 66 65 72 20 62 65 66 6f 72 65 20 74 68   defer before th
43692 65 20 71 75 65 72 79 20 73 74 61 72 74 73 20 65  e query starts e
43693 78 65 63 75 74 69 6e 67 20 28 61 73 20 70 61 72  xecuting (as par
43694 74 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 78 46 69  t..** of the xFi
43695 6c 74 65 72 28 29 20 6d 65 74 68 6f 64 29 2e 20  lter() method). 
43696 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65 6c 65  There is one ele
43697 6d 65 6e 74 20 69 6e 20 74 68 65 20 61 72 72 61  ment in the arra
43698 79 20 66 6f 72 20 65 61 63 68 0d 0a 2a 2a 20 74  y for each..** t
43699 6f 6b 65 6e 20 69 6e 20 74 68 65 20 46 54 53 20  oken in the FTS 
4369a 65 78 70 72 65 73 73 69 6f 6e 2e 0d 0a 2a 2a 0d  expression...**.
4369b 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 64  .** Tokens are d
4369c 69 76 69 64 65 64 20 69 6e 74 6f 20 41 4e 44 2f  ivided into AND/
4369d 4e 45 41 52 20 63 6c 75 73 74 65 72 73 2e 20 41  NEAR clusters. A
4369e 6c 6c 20 74 6f 6b 65 6e 73 20 69 6e 20 61 20 63  ll tokens in a c
4369f 6c 75 73 74 65 72 20 62 65 6c 6f 6e 67 0d 0a 2a  luster belong..*
436a0 2a 20 74 6f 20 70 68 72 61 73 65 73 20 74 68 61  * to phrases tha
436a1 74 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 64 20  t are connected 
436a2 6f 6e 6c 79 20 62 79 20 41 4e 44 20 61 6e 64 20  only by AND and 
436a3 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 73 20 28  NEAR operators (
436a4 6e 6f 74 20 4f 52 20 6f 72 0d 0a 2a 2a 20 4e 4f  not OR or..** NO
436a5 54 29 2e 20 57 68 65 6e 20 64 65 74 65 72 6d 69  T). When determi
436a6 6e 69 6e 67 20 74 6f 6b 65 6e 73 20 74 6f 20 64  ning tokens to d
436a7 65 66 65 72 2c 20 65 61 63 68 20 41 4e 44 2f 4e  efer, each AND/N
436a8 45 41 52 20 63 6c 75 73 74 65 72 20 69 73 20 63  EAR cluster is c
436a9 6f 6e 73 69 64 65 72 65 64 0d 0a 2a 2a 20 73 65  onsidered..** se
436aa 70 61 72 61 74 65 6c 79 2e 20 54 68 65 20 72 6f  parately. The ro
436ab 6f 74 20 6f 66 20 61 20 74 6f 6b 65 6e 73 20 41  ot of a tokens A
436ac 4e 44 2f 4e 45 41 52 20 63 6c 75 73 74 65 72 20  ND/NEAR cluster 
436ad 69 73 20 73 74 6f 72 65 64 20 69 6e 20 0d 0a 2a  is stored in ..*
436ae 2a 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f  * Fts3TokenAndCo
436af 73 74 2e 70 52 6f 6f 74 2e 0d 0a 2a 2f 0d 0a 74  st.pRoot...*/..t
436b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
436b1 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 46  s3TokenAndCost F
436b2 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 3b  ts3TokenAndCost;
436b3 0d 0a 73 74 72 75 63 74 20 46 74 73 33 54 6f 6b  ..struct Fts3Tok
436b4 65 6e 41 6e 64 43 6f 73 74 20 7b 0d 0a 20 20 46  enAndCost {..  F
436b5 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
436b6 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  se;            /
436b7 2a 20 54 68 65 20 70 68 72 61 73 65 20 74 68 65  * The phrase the
436b8 20 74 6f 6b 65 6e 20 62 65 6c 6f 6e 67 73 20 74   token belongs t
436b9 6f 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 6f 6b  o */..  int iTok
436ba 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
436bb 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 69 74          /* Posit
436bc 69 6f 6e 20 6f 66 20 74 6f 6b 65 6e 20 69 6e 20  ion of token in 
436bd 70 68 72 61 73 65 20 2a 2f 0d 0a 20 20 46 74 73  phrase */..  Fts
436be 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54  3PhraseToken *pT
436bf 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20  oken;        /* 
436c0 54 68 65 20 74 6f 6b 65 6e 20 69 74 73 65 6c 66  The token itself
436c1 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20   */..  Fts3Expr 
436c2 2a 70 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20  *pRoot;         
436c3 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f         /* Root o
436c4 66 20 4e 45 41 52 2f 41 4e 44 20 63 6c 75 73 74  f NEAR/AND clust
436c5 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4f 76  er */..  int nOv
436c6 66 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fl;             
436c7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
436c8 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70  er of overflow p
436c9 61 67 65 73 20 74 6f 20 6c 6f 61 64 20 64 6f 63  ages to load doc
436ca 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  list */..  int i
436cb 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
436cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
436cd 65 20 63 6f 6c 75 6d 6e 20 74 68 65 20 74 6f 6b  e column the tok
436ce 65 6e 20 6d 75 73 74 20 6d 61 74 63 68 20 2a 2f  en must match */
436cf 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ..};..../*..** T
436d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
436d1 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 65  used to populate
436d2 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 46 74   an allocated Ft
436d3 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 61  s3TokenAndCost a
436d4 72 72 61 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  rray...**..** If
436d5 20 2a 70 52 63 20 69 73 20 6e 6f 74 20 53 51 4c   *pRc is not SQL
436d6 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73  ITE_OK when this
436d7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
436d8 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d  led, it is a no-
436d9 6f 70 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  op...** Otherwis
436da 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  e, if an error o
436db 63 63 75 72 73 20 64 75 72 69 6e 67 20 65 78 65  ccurs during exe
436dc 63 75 74 69 6f 6e 2c 20 2a 70 52 63 20 69 73 20  cution, *pRc is 
436dd 73 65 74 20 74 6f 20 61 6e 0d 0a 2a 2a 20 53 51  set to an..** SQ
436de 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
436df 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
436e0 64 20 66 74 73 33 45 76 61 6c 54 6f 6b 65 6e 43  d fts3EvalTokenC
436e1 6f 73 74 73 28 0d 0a 20 20 46 74 73 33 43 75 72  osts(..  Fts3Cur
436e2 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20 20  sor *pCsr,      
436e3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 20           /* FTS 
436e4 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a 2f  Cursor handle */
436e5 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  ..  Fts3Expr *pR
436e6 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  oot,            
436e7 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 63      /* Root of c
436e8 75 72 72 65 6e 74 20 41 4e 44 2f 4e 45 41 52 20  urrent AND/NEAR 
436e9 63 6c 75 73 74 65 72 20 2a 2f 0d 0a 20 20 46 74  cluster */..  Ft
436ea 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20  s3Expr *pExpr,  
436eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
436ec 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63   Expression to c
436ed 6f 6e 73 69 64 65 72 20 2a 2f 0d 0a 20 20 46 74  onsider */..  Ft
436ee 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 2a  s3TokenAndCost *
436ef 2a 70 70 54 43 2c 20 20 20 20 20 20 20 20 2f 2a  *ppTC,        /*
436f0 20 57 72 69 74 65 20 6e 65 77 20 65 6e 74 72 69   Write new entri
436f1 65 73 20 74 6f 20 2a 28 2a 70 70 54 43 29 2b 2b  es to *(*ppTC)++
436f2 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20   */..  Fts3Expr 
436f3 2a 2a 2a 70 70 4f 72 2c 20 20 20 20 20 20 20 20  ***ppOr,        
436f4 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
436f5 6e 65 77 20 4f 52 20 72 6f 6f 74 20 74 6f 20 2a  new OR root to *
436f6 28 2a 70 70 4f 72 29 2b 2b 20 2a 2f 0d 0a 20 20  (*ppOr)++ */..  
436f7 69 6e 74 20 2a 70 52 63 20 20 20 20 20 20 20 20  int *pRc        
436f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
436f9 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 72 72 6f 72  /* IN/OUT: Error
436fa 20 63 6f 64 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   code */..){..  
436fb 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45  if( *pRc==SQLITE
436fc 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  _OK ){..    if( 
436fd 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54  pExpr->eType==FT
436fe 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 7b  SQUERY_PHRASE ){
436ff 0d 0a 20 20 20 20 20 20 46 74 73 33 50 68 72 61  ..      Fts3Phra
43700 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45  se *pPhrase = pE
43701 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0d 0a 20  xpr->pPhrase;.. 
43702 20 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20       int i;..   
43703 20 20 20 66 6f 72 28 69 3d 30 3b 20 2a 70 52 63     for(i=0; *pRc
43704 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
43705 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  <pPhrase->nToken
43706 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20  ; i++){..       
43707 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73   Fts3TokenAndCos
43708 74 20 2a 70 54 43 20 3d 20 28 2a 70 70 54 43 29  t *pTC = (*ppTC)
43709 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 43  ++;..        pTC
4370a 2d 3e 70 50 68 72 61 73 65 20 3d 20 70 50 68 72  ->pPhrase = pPhr
4370b 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20 70 54  ase;..        pT
4370c 43 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69 3b 0d 0a  C->iToken = i;..
4370d 20 20 20 20 20 20 20 20 70 54 43 2d 3e 70 52 6f          pTC->pRo
4370e 6f 74 20 3d 20 70 52 6f 6f 74 3b 0d 0a 20 20 20  ot = pRoot;..   
4370f 20 20 20 20 20 70 54 43 2d 3e 70 54 6f 6b 65 6e       pTC->pToken
43710 20 3d 20 26 70 50 68 72 61 73 65 2d 3e 61 54 6f   = &pPhrase->aTo
43711 6b 65 6e 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 20  ken[i];..       
43712 20 70 54 43 2d 3e 69 43 6f 6c 20 3d 20 70 50 68   pTC->iCol = pPh
43713 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0d 0a  rase->iColumn;..
43714 20 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 73          *pRc = s
43715 71 6c 69 74 65 33 46 74 73 33 4d 73 72 4f 76 66  qlite3Fts3MsrOvf
43716 6c 28 70 43 73 72 2c 20 70 54 43 2d 3e 70 54 6f  l(pCsr, pTC->pTo
43717 6b 65 6e 2d 3e 70 53 65 67 63 73 72 2c 20 26 70  ken->pSegcsr, &p
43718 54 43 2d 3e 6e 4f 76 66 6c 29 3b 0d 0a 20 20 20  TC->nOvfl);..   
43719 20 20 20 7d 0d 0a 20 20 20 20 7d 65 6c 73 65 20     }..    }else 
4371a 69 66 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  if( pExpr->eType
4371b 21 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 29  !=FTSQUERY_NOT )
4371c 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
4371d 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46   pExpr->eType==F
4371e 54 53 51 55 45 52 59 5f 4f 52 0d 0a 20 20 20 20  TSQUERY_OR..    
4371f 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d         || pExpr-
43720 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
43721 5f 41 4e 44 0d 0a 20 20 20 20 20 20 20 20 20 20  _AND..          
43722 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54 79 70 65   || pExpr->eType
43723 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 0d  ==FTSQUERY_NEAR.
43724 0a 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20  .      );..     
43725 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
43726 70 4c 65 66 74 20 26 26 20 70 45 78 70 72 2d 3e  pLeft && pExpr->
43727 70 52 69 67 68 74 20 29 3b 0d 0a 20 20 20 20 20  pRight );..     
43728 20 69 66 28 20 70 45 78 70 72 2d 3e 65 54 79 70   if( pExpr->eTyp
43729 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52 20 29  e==FTSQUERY_OR )
4372a 7b 0d 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74  {..        pRoot
4372b 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
4372c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 70 70 4f 72  ..        **ppOr
4372d 20 3d 20 70 52 6f 6f 74 3b 0d 0a 20 20 20 20 20   = pRoot;..     
4372e 20 20 20 28 2a 70 70 4f 72 29 2b 2b 3b 0d 0a 20     (*ppOr)++;.. 
4372f 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 66 74       }..      ft
43730 73 33 45 76 61 6c 54 6f 6b 65 6e 43 6f 73 74 73  s3EvalTokenCosts
43731 28 70 43 73 72 2c 20 70 52 6f 6f 74 2c 20 70 45  (pCsr, pRoot, pE
43732 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 70 54 43  xpr->pLeft, ppTC
43733 2c 20 70 70 4f 72 2c 20 70 52 63 29 3b 0d 0a 20  , ppOr, pRc);.. 
43734 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
43735 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
43736 4f 52 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70  OR ){..        p
43737 52 6f 6f 74 20 3d 20 70 45 78 70 72 2d 3e 70 52  Root = pExpr->pR
43738 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 20 20 2a  ight;..        *
43739 2a 70 70 4f 72 20 3d 20 70 52 6f 6f 74 3b 0d 0a  *ppOr = pRoot;..
4373a 20 20 20 20 20 20 20 20 28 2a 70 70 4f 72 29 2b          (*ppOr)+
4373b 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  +;..      }..   
4373c 20 20 20 66 74 73 33 45 76 61 6c 54 6f 6b 65 6e     fts3EvalToken
4373d 43 6f 73 74 73 28 70 43 73 72 2c 20 70 52 6f 6f  Costs(pCsr, pRoo
4373e 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
4373f 2c 20 70 70 54 43 2c 20 70 70 4f 72 2c 20 70 52  , ppTC, ppOr, pR
43740 63 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  c);..    }..  }.
43741 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 74  .}..../*..** Det
43742 65 72 6d 69 6e 65 20 74 68 65 20 61 76 65 72 61  ermine the avera
43743 67 65 20 64 6f 63 75 6d 65 6e 74 20 28 72 6f 77  ge document (row
43744 29 20 73 69 7a 65 20 69 6e 20 70 61 67 65 73 2e  ) size in pages.
43745 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0d   If successful,.
43746 0a 2a 2a 20 77 72 69 74 65 20 74 68 69 73 20 76  .** write this v
43747 61 6c 75 65 20 74 6f 20 2a 70 6e 50 61 67 65 20  alue to *pnPage 
43748 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
43749 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
4374a 20 72 65 74 75 72 6e 0d 0a 2a 2a 20 61 6e 20 53   return..** an S
4374b 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
4374c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 61 76  ...**..** The av
4374d 65 72 61 67 65 20 64 6f 63 75 6d 65 6e 74 20 73  erage document s
4374e 69 7a 65 20 69 6e 20 70 61 67 65 73 20 69 73 20  ize in pages is 
4374f 63 61 6c 63 75 6c 61 74 65 64 20 62 79 20 66 69  calculated by fi
43750 72 73 74 20 63 61 6c 63 75 6c 61 74 69 6e 67 20  rst calculating 
43751 0d 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 69 6e 67  ..** determining
43752 20 74 68 65 20 61 76 65 72 61 67 65 20 73 69 7a   the average siz
43753 65 20 69 6e 20 62 79 74 65 73 2c 20 42 2e 20 49  e in bytes, B. I
43754 66 20 42 20 69 73 20 6c 65 73 73 20 74 68 61 6e  f B is less than
43755 20 74 68 65 20 61 6d 6f 75 6e 74 0d 0a 2a 2a 20   the amount..** 
43756 6f 66 20 64 61 74 61 20 74 68 61 74 20 77 69 6c  of data that wil
43757 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c  l fit on a singl
43758 65 20 6c 65 61 66 20 70 61 67 65 20 6f 66 20 61  e leaf page of a
43759 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 69  n intkey table i
4375a 6e 0d 0a 2a 2a 20 74 68 69 73 20 64 61 74 61 62  n..** this datab
4375b 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 61 76  ase, then the av
4375c 65 72 61 67 65 20 64 6f 63 73 69 7a 65 20 69 73  erage docsize is
4375d 20 31 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69   1. Otherwise, i
4375e 74 20 69 73 20 31 20 70 6c 75 73 0d 0a 2a 2a 20  t is 1 plus..** 
4375f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 76  the number of ov
43760 65 72 66 6c 6f 77 20 70 61 67 65 73 20 63 6f 6e  erflow pages con
43761 73 75 6d 65 64 20 62 79 20 61 20 72 65 63 6f 72  sumed by a recor
43762 64 20 42 20 62 79 74 65 73 20 69 6e 20 73 69 7a  d B bytes in siz
43763 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
43764 6e 74 20 66 74 73 33 45 76 61 6c 41 76 65 72 61  nt fts3EvalAvera
43765 67 65 44 6f 63 73 69 7a 65 28 46 74 73 33 43 75  geDocsize(Fts3Cu
43766 72 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20  rsor *pCsr, int 
43767 2a 70 6e 50 61 67 65 29 7b 0d 0a 20 20 69 66 28  *pnPage){..  if(
43768 20 70 43 73 72 2d 3e 6e 52 6f 77 41 76 67 3d 3d   pCsr->nRowAvg==
43769 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65  0 ){..    /* The
4376a 20 61 76 65 72 61 67 65 20 64 6f 63 75 6d 65 6e   average documen
4376b 74 20 73 69 7a 65 2c 20 77 68 69 63 68 20 69 73  t size, which is
4376c 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 61 6c   required to cal
4376d 63 75 6c 61 74 65 20 74 68 65 20 63 6f 73 74 0d  culate the cost.
4376e 0a 20 20 20 20 2a 2a 20 6f 66 20 65 61 63 68 20  .    ** of each 
4376f 64 6f 63 6c 69 73 74 2c 20 68 61 73 20 6e 6f 74  doclist, has not
43770 20 79 65 74 20 62 65 65 6e 20 64 65 74 65 72 6d   yet been determ
43771 69 6e 65 64 2e 20 52 65 61 64 20 74 68 65 20 72  ined. Read the r
43772 65 71 75 69 72 65 64 20 0d 0a 20 20 20 20 2a 2a  equired ..    **
43773 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 25   data from the %
43774 5f 73 74 61 74 20 74 61 62 6c 65 20 74 6f 20 63  _stat table to c
43775 61 6c 63 75 6c 61 74 65 20 69 74 2e 0d 0a 20 20  alculate it...  
43776 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 45 6e 74    **..    ** Ent
43777 72 79 20 30 20 6f 66 20 74 68 65 20 25 5f 73 74  ry 0 of the %_st
43778 61 74 20 74 61 62 6c 65 20 69 73 20 61 20 62 6c  at table is a bl
43779 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28 6e  ob containing (n
4377a 43 6f 6c 2b 31 29 20 46 54 53 33 20 0d 0a 20 20  Col+1) FTS3 ..  
4377b 20 20 2a 2a 20 76 61 72 69 6e 74 73 2c 20 77 68    ** varints, wh
4377c 65 72 65 20 6e 43 6f 6c 20 69 73 20 74 68 65 20  ere nCol is the 
4377d 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
4377e 73 20 69 6e 20 74 68 65 20 46 54 53 33 20 74 61  s in the FTS3 ta
4377f 62 6c 65 2e 0d 0a 20 20 20 20 2a 2a 20 54 68 65  ble...    ** The
43780 20 66 69 72 73 74 20 76 61 72 69 6e 74 20 69 73   first varint is
43781 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
43782 6f 63 75 6d 65 6e 74 73 20 63 75 72 72 65 6e 74  ocuments current
43783 6c 79 20 73 74 6f 72 65 64 20 69 6e 0d 0a 20 20  ly stored in..  
43784 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20    ** the table. 
43785 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 43  The following nC
43786 6f 6c 20 76 61 72 69 6e 74 73 20 63 6f 6e 74 61  ol varints conta
43787 69 6e 20 74 68 65 20 74 6f 74 61 6c 20 61 6d 6f  in the total amo
43788 75 6e 74 20 6f 66 0d 0a 20 20 20 20 2a 2a 20 64  unt of..    ** d
43789 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 61 6c  ata stored in al
4378a 6c 20 72 6f 77 73 20 6f 66 20 65 61 63 68 20 63  l rows of each c
4378b 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
4378c 6c 65 2c 20 66 72 6f 6d 20 6c 65 66 74 0d 0a 20  le, from left.. 
4378d 20 20 20 2a 2a 20 74 6f 20 72 69 67 68 74 2e 0d     ** to right..
4378e 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  .    */..    int
4378f 20 72 63 3b 0d 0a 20 20 20 20 46 74 73 33 54 61   rc;..    Fts3Ta
43790 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61  ble *p = (Fts3Ta
43791 62 6c 65 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e  ble*)pCsr->base.
43792 70 56 74 61 62 3b 0d 0a 20 20 20 20 73 71 6c 69  pVtab;..    sqli
43793 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
43794 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  ..    sqlite3_in
43795 74 36 34 20 6e 44 6f 63 20 3d 20 30 3b 0d 0a 20  t64 nDoc = 0;.. 
43796 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
43797 20 6e 42 79 74 65 20 3d 20 30 3b 0d 0a 20 20 20   nByte = 0;..   
43798 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 45 6e   const char *pEn
43799 64 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  d;..    const ch
4379a 61 72 20 2a 61 3b 0d 0a 0d 0a 20 20 20 20 72 63  ar *a;....    rc
4379b 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65   = sqlite3Fts3Se
4379c 6c 65 63 74 44 6f 63 74 6f 74 61 6c 28 70 2c 20  lectDoctotal(p, 
4379d 26 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 69 66  &pStmt);..    if
4379e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
4379f 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20  ) return rc;..  
437a0 20 20 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f    a = sqlite3_co
437a1 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  lumn_blob(pStmt,
437a2 20 30 29 3b 0d 0a 20 20 20 20 61 73 73 65 72 74   0);..    assert
437a3 28 20 61 20 29 3b 0d 0a 0d 0a 20 20 20 20 70 45  ( a );....    pE
437a4 6e 64 20 3d 20 26 61 5b 73 71 6c 69 74 65 33 5f  nd = &a[sqlite3_
437a5 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74  column_bytes(pSt
437a6 6d 74 2c 20 30 29 5d 3b 0d 0a 20 20 20 20 61 20  mt, 0)];..    a 
437a7 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
437a8 74 56 61 72 69 6e 74 28 61 2c 20 26 6e 44 6f 63  tVarint(a, &nDoc
437a9 29 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 61  );..    while( a
437aa 3c 70 45 6e 64 20 29 7b 0d 0a 20 20 20 20 20 20  <pEnd ){..      
437ab 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  a += sqlite3Fts3
437ac 47 65 74 56 61 72 69 6e 74 28 61 2c 20 26 6e 42  GetVarint(a, &nB
437ad 79 74 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  yte);..    }..  
437ae 20 20 69 66 28 20 6e 44 6f 63 3d 3d 30 20 7c 7c    if( nDoc==0 ||
437af 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0d 0a 20 20   nByte==0 ){..  
437b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
437b1 74 28 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 20  t(pStmt);..     
437b2 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f 52 52   return FTS_CORR
437b3 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 20 20 7d  UPT_VTAB;..    }
437b4 0d 0a 0d 0a 20 20 20 20 70 43 73 72 2d 3e 6e 44  ....    pCsr->nD
437b5 6f 63 20 3d 20 6e 44 6f 63 3b 0d 0a 20 20 20 20  oc = nDoc;..    
437b6 70 43 73 72 2d 3e 6e 52 6f 77 41 76 67 20 3d 20  pCsr->nRowAvg = 
437b7 28 69 6e 74 29 28 28 28 6e 42 79 74 65 20 2f 20  (int)(((nByte / 
437b8 6e 44 6f 63 29 20 2b 20 70 2d 3e 6e 50 67 73 7a  nDoc) + p->nPgsz
437b9 29 20 2f 20 70 2d 3e 6e 50 67 73 7a 29 3b 0d 0a  ) / p->nPgsz);..
437ba 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
437bb 2d 3e 6e 52 6f 77 41 76 67 3e 30 20 29 3b 20 0d  ->nRowAvg>0 ); .
437bc 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
437bd 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0d  3_reset(pStmt);.
437be 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
437bf 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
437c0 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70  rc;..  }....  *p
437c1 6e 50 61 67 65 20 3d 20 70 43 73 72 2d 3e 6e 52  nPage = pCsr->nR
437c2 6f 77 41 76 67 3b 0d 0a 20 20 72 65 74 75 72 6e  owAvg;..  return
437c3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
437c4 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
437c5 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
437c6 20 74 6f 20 73 65 6c 65 63 74 20 74 68 65 20 74   to select the t
437c7 6f 6b 65 6e 73 20 28 69 66 20 61 6e 79 29 20 74  okens (if any) t
437c8 68 61 74 20 77 69 6c 6c 20 62 65 20 0d 0a 2a 2a  hat will be ..**
437c9 20 64 65 66 65 72 72 65 64 2e 20 54 68 65 20 61   deferred. The a
437ca 72 72 61 79 20 61 54 43 5b 5d 20 68 61 73 20 61  rray aTC[] has a
437cb 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6f 70 75  lready been popu
437cc 6c 61 74 65 64 20 77 68 65 6e 20 74 68 69 73 20  lated when this 
437cd 69 73 0d 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0d 0a  is..** called...
437ce 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  **..** This func
437cf 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 6f  tion is called o
437d0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 41 4e 44  nce for each AND
437d1 2f 4e 45 41 52 20 63 6c 75 73 74 65 72 20 69 6e  /NEAR cluster in
437d2 20 74 68 65 20 0d 0a 2a 2a 20 65 78 70 72 65 73   the ..** expres
437d3 73 69 6f 6e 2e 20 45 61 63 68 20 69 6e 76 6f 63  sion. Each invoc
437d4 61 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73  ation determines
437d5 20 77 68 69 63 68 20 74 6f 6b 65 6e 73 20 74 6f   which tokens to
437d6 20 64 65 66 65 72 20 77 69 74 68 69 6e 0d 0a 2a   defer within..*
437d7 2a 20 74 68 65 20 63 6c 75 73 74 65 72 20 77 69  * the cluster wi
437d8 74 68 20 72 6f 6f 74 20 6e 6f 64 65 20 70 52 6f  th root node pRo
437d9 6f 74 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  ot. See comments
437da 20 61 62 6f 76 65 20 74 68 65 20 64 65 66 69 6e   above the defin
437db 69 74 69 6f 6e 0d 0a 2a 2a 20 6f 66 20 73 74 72  ition..** of str
437dc 75 63 74 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64  uct Fts3TokenAnd
437dd 43 6f 73 74 20 66 6f 72 20 6d 6f 72 65 20 64 65  Cost for more de
437de 74 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  tails...**..** I
437df 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
437e0 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  s, SQLITE_OK is 
437e1 72 65 74 75 72 6e 65 64 20 61 6e 64 20 73 71 6c  returned and sql
437e2 69 74 65 33 46 74 73 33 44 65 66 65 72 54 6f 6b  ite3Fts3DeferTok
437e3 65 6e 28 29 0d 0a 2a 2a 20 63 61 6c 6c 65 64 20  en()..** called 
437e4 6f 6e 20 65 61 63 68 20 74 6f 6b 65 6e 20 74 6f  on each token to
437e5 20 64 65 66 65 72 2e 20 4f 74 68 65 72 77 69 73   defer. Otherwis
437e6 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  e, an SQLite err
437e7 6f 72 20 63 6f 64 65 20 69 73 0d 0a 2a 2a 20 72  or code is..** r
437e8 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74  eturned...*/..st
437e9 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 76 61  atic int fts3Eva
437ea 6c 53 65 6c 65 63 74 44 65 66 65 72 72 65 64 28  lSelectDeferred(
437eb 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a  ..  Fts3Cursor *
437ec 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20 20  pCsr,           
437ed 20 20 20 20 2f 2a 20 46 54 53 20 43 75 72 73 6f      /* FTS Curso
437ee 72 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46  r handle */..  F
437ef 74 73 33 45 78 70 72 20 2a 70 52 6f 6f 74 2c 20  ts3Expr *pRoot, 
437f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
437f1 2a 20 43 6f 6e 73 69 64 65 72 20 74 6f 6b 65 6e  * Consider token
437f2 73 20 77 69 74 68 20 74 68 69 73 20 72 6f 6f 74  s with this root
437f3 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 46 74 73 33   node */..  Fts3
437f4 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 2a 61 54  TokenAndCost *aT
437f5 43 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  C,          /* A
437f6 72 72 61 79 20 6f 66 20 65 78 70 72 65 73 73 69  rray of expressi
437f7 6f 6e 20 74 6f 6b 65 6e 73 20 61 6e 64 20 63 6f  on tokens and co
437f8 73 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54  sts */..  int nT
437f9 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C               
437fa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
437fb 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
437fc 6e 20 61 54 43 5b 5d 20 2a 2f 0d 0a 29 7b 0d 0a  n aTC[] */..){..
437fd 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61    Fts3Table *pTa
437fe 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a  b = (Fts3Table *
437ff 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61  )pCsr->base.pVta
43800 62 3b 0d 0a 20 20 69 6e 74 20 6e 44 6f 63 53 69  b;..  int nDocSi
43801 7a 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ze = 0;         
43802 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
43803 6f 66 20 70 61 67 65 73 20 70 65 72 20 64 6f 63  of pages per doc
43804 20 6c 6f 61 64 65 64 20 2a 2f 0d 0a 20 20 69 6e   loaded */..  in
43805 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
43806 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
43807 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   Return code */.
43808 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20  .  int ii;      
43809 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4380a 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
4380b 61 72 69 61 62 6c 65 20 66 6f 72 20 76 61 72 69  ariable for vari
4380c 6f 75 73 20 70 75 72 70 6f 73 65 73 20 2a 2f 0d  ous purposes */.
4380d 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 30  .  int nOvfl = 0
4380e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4380f 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 76 65 72     /* Total over
43810 66 6c 6f 77 20 70 61 67 65 73 20 75 73 65 64 20  flow pages used 
43811 62 79 20 64 6f 63 6c 69 73 74 73 20 2a 2f 0d 0a  by doclists */..
43812 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30    int nToken = 0
43813 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
43814 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
43815 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 63  r of tokens in c
43816 6c 75 73 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 69  luster */....  i
43817 6e 74 20 6e 4d 69 6e 45 73 74 20 3d 20 30 3b 20  nt nMinEst = 0; 
43818 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43819 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 63 6f  * The minimum co
4381a 75 6e 74 20 66 6f 72 20 61 6e 79 20 70 68 72 61  unt for any phra
4381b 73 65 20 73 6f 20 66 61 72 2e 20 2a 2f 0d 0a 20  se so far. */.. 
4381c 20 69 6e 74 20 6e 4c 6f 61 64 34 20 3d 20 31 3b   int nLoad4 = 1;
4381d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4381e 20 2f 2a 20 28 50 68 72 61 73 65 73 20 74 68 61   /* (Phrases tha
4381f 74 20 77 69 6c 6c 20 62 65 20 6c 6f 61 64 65 64  t will be loaded
43820 29 5e 34 2e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20  )^4. */....  /* 
43821 54 6f 6b 65 6e 73 20 61 72 65 20 6e 65 76 65 72  Tokens are never
43822 20 64 65 66 65 72 72 65 64 20 66 6f 72 20 46 54   deferred for FT
43823 53 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 64  S tables created
43824 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6e 74 65   using the conte
43825 6e 74 3d 78 78 78 0d 0a 20 20 2a 2a 20 6f 70 74  nt=xxx..  ** opt
43826 69 6f 6e 2e 20 54 68 65 20 72 65 61 73 6f 6e 20  ion. The reason 
43827 62 65 69 6e 67 20 74 68 61 74 20 69 74 20 69 73  being that it is
43828 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20   not guaranteed 
43829 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
4382a 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 20 61 63 74  ..  ** table act
4382b 75 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74  ually contains t
4382c 68 65 20 73 61 6d 65 20 64 61 74 61 20 61 73 20  he same data as 
4382d 74 68 65 20 69 6e 64 65 78 2e 20 54 6f 20 70 72  the index. To pr
4382e 65 76 65 6e 74 20 74 68 69 73 20 66 72 6f 6d 0d  event this from.
4382f 0a 20 20 2a 2a 20 63 61 75 73 69 6e 67 20 61 6e  .  ** causing an
43830 79 20 70 72 6f 62 6c 65 6d 73 2c 20 74 68 65 20  y problems, the 
43831 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 20 6f  deferred token o
43832 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63  ptimization is c
43833 6f 6d 70 6c 65 74 65 6c 79 0d 0a 20 20 2a 2a 20  ompletely..  ** 
43834 64 69 73 61 62 6c 65 64 20 66 6f 72 20 63 6f 6e  disabled for con
43835 74 65 6e 74 3d 78 78 78 20 74 61 62 6c 65 73 2e  tent=xxx tables.
43836 20 2a 2f 0d 0a 20 20 69 66 28 20 70 54 61 62 2d   */..  if( pTab-
43837 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 20 29 7b 0d  >zContentTbl ){.
43838 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
43839 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  TE_OK;..  }.... 
4383a 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 74 6f   /* Count the to
4383b 6b 65 6e 73 20 69 6e 20 74 68 69 73 20 41 4e 44  kens in this AND
4383c 2f 4e 45 41 52 20 63 6c 75 73 74 65 72 2e 20 49  /NEAR cluster. I
4383d 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 64 6f  f none of the do
4383e 63 6c 69 73 74 73 0d 0a 20 20 2a 2a 20 61 73 73  clists..  ** ass
4383f 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
43840 20 74 6f 6b 65 6e 73 20 73 70 69 6c 6c 20 6f 6e   tokens spill on
43841 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
43842 73 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69  s, or if there i
43843 73 0d 0a 20 20 2a 2a 20 6f 6e 6c 79 20 31 20 74  s..  ** only 1 t
43844 6f 6b 65 6e 2c 20 65 78 69 74 20 65 61 72 6c 79  oken, exit early
43845 2e 20 4e 6f 20 74 6f 6b 65 6e 73 20 74 6f 20 64  . No tokens to d
43846 65 66 65 72 20 69 6e 20 74 68 69 73 20 63 61 73  efer in this cas
43847 65 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 69 3d  e. */..  for(ii=
43848 30 3b 20 69 69 3c 6e 54 43 3b 20 69 69 2b 2b 29  0; ii<nTC; ii++)
43849 7b 0d 0a 20 20 20 20 69 66 28 20 61 54 43 5b 69  {..    if( aTC[i
4384a 69 5d 2e 70 52 6f 6f 74 3d 3d 70 52 6f 6f 74 20  i].pRoot==pRoot 
4384b 29 7b 0d 0a 20 20 20 20 20 20 6e 4f 76 66 6c 20  ){..      nOvfl 
4384c 2b 3d 20 61 54 43 5b 69 69 5d 2e 6e 4f 76 66 6c  += aTC[ii].nOvfl
4384d 3b 0d 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 2b  ;..      nToken+
4384e 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  +;..    }..  }..
4384f 20 20 69 66 28 20 6e 4f 76 66 6c 3d 3d 30 20 7c    if( nOvfl==0 |
43850 7c 20 6e 54 6f 6b 65 6e 3c 32 20 29 20 72 65 74  | nToken<2 ) ret
43851 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
43852 0d 0a 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68  ..  /* Obtain th
43853 65 20 61 76 65 72 61 67 65 20 64 6f 63 73 69 7a  e average docsiz
43854 65 20 28 69 6e 20 70 61 67 65 73 29 2e 20 2a 2f  e (in pages). */
43855 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 45 76 61  ..  rc = fts3Eva
43856 6c 41 76 65 72 61 67 65 44 6f 63 73 69 7a 65 28  lAverageDocsize(
43857 70 43 73 72 2c 20 26 6e 44 6f 63 53 69 7a 65 29  pCsr, &nDocSize)
43858 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  ;..  assert( rc!
43859 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 44  =SQLITE_OK || nD
4385a 6f 63 53 69 7a 65 3e 30 20 29 3b 0d 0a 0d 0a 0d  ocSize>0 );.....
4385b 0a 20 20 2f 2a 20 49 74 65 72 61 74 65 20 74 68  .  /* Iterate th
4385c 72 6f 75 67 68 20 61 6c 6c 20 74 6f 6b 65 6e 73  rough all tokens
4385d 20 69 6e 20 74 68 69 73 20 41 4e 44 2f 4e 45 41   in this AND/NEA
4385e 52 20 63 6c 75 73 74 65 72 2c 20 69 6e 20 61 73  R cluster, in as
4385f 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 0d 0a  cending order ..
43860 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 75 6d 62    ** of the numb
43861 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70  er of overflow p
43862 61 67 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62  ages that will b
43863 65 20 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20  e loaded by the 
43864 70 61 67 65 72 20 6c 61 79 65 72 20 0d 0a 20 20  pager layer ..  
43865 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  ** to retrieve t
43866 68 65 20 65 6e 74 69 72 65 20 64 6f 63 6c 69 73  he entire doclis
43867 74 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20  t for the token 
43868 66 72 6f 6d 20 74 68 65 20 66 75 6c 6c 2d 74 65  from the full-te
43869 78 74 20 69 6e 64 65 78 2e 0d 0a 20 20 2a 2a 20  xt index...  ** 
4386a 4c 6f 61 64 20 74 68 65 20 64 6f 63 6c 69 73 74  Load the doclist
4386b 73 20 66 6f 72 20 74 6f 6b 65 6e 73 20 74 68 61  s for tokens tha
4386c 74 20 61 72 65 20 65 69 74 68 65 72 3a 0d 0a 20  t are either:.. 
4386d 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 61 2e 20 54   **..  **   a. T
4386e 68 65 20 63 68 65 61 70 65 73 74 20 74 6f 6b 65  he cheapest toke
4386f 6e 20 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20  n in the entire 
43870 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68 65 20  query (i.e. the 
43871 6f 6e 65 20 76 69 73 69 74 65 64 20 62 79 20 74  one visited by t
43872 68 65 0d 0a 20 20 2a 2a 20 20 20 20 20 20 66 69  he..  **      fi
43873 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  rst iteration of
43874 20 74 68 69 73 20 6c 6f 6f 70 29 2c 20 6f 72 0d   this loop), or.
43875 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 62 2e  .  **..  **   b.
43876 20 50 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69   Part of a multi
43877 2d 74 6f 6b 65 6e 20 70 68 72 61 73 65 2e 0d 0a  -token phrase...
43878 20 20 2a 2a 0d 0a 20 20 2a 2a 20 41 66 74 65 72    **..  ** After
43879 20 65 61 63 68 20 74 6f 6b 65 6e 20 64 6f 63 6c   each token docl
4387a 69 73 74 20 69 73 20 6c 6f 61 64 65 64 2c 20 6d  ist is loaded, m
4387b 65 72 67 65 20 69 74 20 77 69 74 68 20 74 68 65  erge it with the
4387c 20 6f 74 68 65 72 73 20 66 72 6f 6d 20 74 68 65   others from the
4387d 0d 0a 20 20 2a 2a 20 73 61 6d 65 20 70 68 72 61  ..  ** same phra
4387e 73 65 20 61 6e 64 20 63 6f 75 6e 74 20 74 68 65  se and count the
4387f 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63 75 6d   number of docum
43880 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 6d 65  ents that the me
43881 72 67 65 64 20 64 6f 63 6c 69 73 74 0d 0a 20 20  rged doclist..  
43882 2a 2a 20 63 6f 6e 74 61 69 6e 73 2e 20 53 65 74  ** contains. Set
43883 20 76 61 72 69 61 62 6c 65 20 22 6e 4d 69 6e 45   variable "nMinE
43884 73 74 22 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c  st" to the small
43885 65 73 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f  est number of do
43886 63 75 6d 65 6e 74 73 20 69 6e 20 0d 0a 20 20 2a  cuments in ..  *
43887 2a 20 61 6e 79 20 70 68 72 61 73 65 20 64 6f 63  * any phrase doc
43888 6c 69 73 74 20 66 6f 72 20 77 68 69 63 68 20 31  list for which 1
43889 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 20 64   or more token d
4388a 6f 63 6c 69 73 74 73 20 68 61 76 65 20 62 65 65  oclists have bee
4388b 6e 20 6c 6f 61 64 65 64 2e 0d 0a 20 20 2a 2a 20  n loaded...  ** 
4388c 4c 65 74 20 6e 4f 74 68 65 72 20 62 65 20 74 68  Let nOther be th
4388d 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 74 68 65  e number of othe
4388e 72 20 70 68 72 61 73 65 73 20 66 6f 72 20 77 68  r phrases for wh
4388f 69 63 68 20 69 74 20 69 73 20 63 65 72 74 61 69  ich it is certai
43890 6e 20 74 68 61 74 0d 0a 20 20 2a 2a 20 6f 6e 65  n that..  ** one
43891 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20   or more tokens 
43892 77 69 6c 6c 20 6e 6f 74 20 62 65 20 64 65 66 65  will not be defe
43893 72 72 65 64 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a  rred...  **..  *
43894 2a 20 54 68 65 6e 2c 20 66 6f 72 20 65 61 63 68  * Then, for each
43895 20 74 6f 6b 65 6e 2c 20 64 65 66 65 72 20 69 74   token, defer it
43896 20 69 66 20 6c 6f 61 64 69 6e 67 20 74 68 65 20   if loading the 
43897 64 6f 63 6c 69 73 74 20 77 6f 75 6c 64 20 72 65  doclist would re
43898 73 75 6c 74 20 69 6e 0d 0a 20 20 2a 2a 20 6c 6f  sult in..  ** lo
43899 61 64 69 6e 67 20 4e 20 6f 72 20 6d 6f 72 65 20  ading N or more 
4389a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 69  overflow pages i
4389b 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 77 68 65 72  nto memory, wher
4389c 65 20 4e 20 69 73 20 63 6f 6d 70 75 74 65 64 20  e N is computed 
4389d 61 73 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  as:..  **..  ** 
4389e 20 20 20 28 6e 4d 69 6e 45 73 74 20 2b 20 34 5e     (nMinEst + 4^
4389f 6e 4f 74 68 65 72 20 2d 20 31 29 20 2f 20 28 34  nOther - 1) / (4
438a0 5e 6e 4f 74 68 65 72 29 0d 0a 20 20 2a 2f 0d 0a  ^nOther)..  */..
438a1 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
438a2 54 6f 6b 65 6e 20 26 26 20 72 63 3d 3d 53 51 4c  Token && rc==SQL
438a3 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0d 0a  ITE_OK; ii++){..
438a4 20 20 20 20 69 6e 74 20 69 54 43 3b 20 20 20 20      int iTC;    
438a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
438a6 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
438a7 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 54 43  rate through aTC
438a8 5b 5d 20 61 72 72 61 79 2e 20 2a 2f 0d 0a 20 20  [] array. */..  
438a9 20 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f    Fts3TokenAndCo
438aa 73 74 20 2a 70 54 43 20 3d 20 30 3b 20 20 20 20  st *pTC = 0;    
438ab 2f 2a 20 53 65 74 20 74 6f 20 63 68 65 61 70 65  /* Set to cheape
438ac 73 74 20 72 65 6d 61 69 6e 69 6e 67 20 74 6f 6b  st remaining tok
438ad 65 6e 2e 20 2a 2f 0d 0a 0d 0a 20 20 20 20 2f 2a  en. */....    /*
438ae 20 53 65 74 20 70 54 43 20 74 6f 20 70 6f 69 6e   Set pTC to poin
438af 74 20 74 6f 20 74 68 65 20 63 68 65 61 70 65 73  t to the cheapes
438b0 74 20 72 65 6d 61 69 6e 69 6e 67 20 74 6f 6b 65  t remaining toke
438b1 6e 2e 20 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69  n. */..    for(i
438b2 54 43 3d 30 3b 20 69 54 43 3c 6e 54 43 3b 20 69  TC=0; iTC<nTC; i
438b3 54 43 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 69 66  TC++){..      if
438b4 28 20 61 54 43 5b 69 54 43 5d 2e 70 54 6f 6b 65  ( aTC[iTC].pToke
438b5 6e 20 26 26 20 61 54 43 5b 69 54 43 5d 2e 70 52  n && aTC[iTC].pR
438b6 6f 6f 74 3d 3d 70 52 6f 6f 74 20 0d 0a 20 20 20  oot==pRoot ..   
438b7 20 20 20 20 26 26 20 28 21 70 54 43 20 7c 7c 20      && (!pTC || 
438b8 61 54 43 5b 69 54 43 5d 2e 6e 4f 76 66 6c 3c 70  aTC[iTC].nOvfl<p
438b9 54 43 2d 3e 6e 4f 76 66 6c 29 20 0d 0a 20 20 20  TC->nOvfl) ..   
438ba 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 70     ){..        p
438bb 54 43 20 3d 20 26 61 54 43 5b 69 54 43 5d 3b 0d  TC = &aTC[iTC];.
438bc 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
438bd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 43  .    assert( pTC
438be 20 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69   );....    if( i
438bf 69 20 26 26 20 70 54 43 2d 3e 6e 4f 76 66 6c 3e  i && pTC->nOvfl>
438c0 3d 28 28 6e 4d 69 6e 45 73 74 2b 28 6e 4c 6f 61  =((nMinEst+(nLoa
438c1 64 34 2f 34 29 2d 31 29 2f 28 6e 4c 6f 61 64 34  d4/4)-1)/(nLoad4
438c2 2f 34 29 29 2a 6e 44 6f 63 53 69 7a 65 20 29 7b  /4))*nDocSize ){
438c3 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  ..      /* The n
438c4 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f  umber of overflo
438c5 77 20 70 61 67 65 73 20 74 6f 20 6c 6f 61 64 20  w pages to load 
438c6 66 6f 72 20 74 68 69 73 20 28 61 6e 64 20 74 68  for this (and th
438c7 65 72 65 66 6f 72 65 20 61 6c 6c 0d 0a 20 20 20  erefore all..   
438c8 20 20 20 2a 2a 20 73 75 62 73 65 71 75 65 6e 74     ** subsequent
438c9 29 20 74 6f 6b 65 6e 73 20 69 73 20 67 72 65 61  ) tokens is grea
438ca 74 65 72 20 74 68 61 6e 20 74 68 65 20 65 73 74  ter than the est
438cb 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  imated number of
438cc 20 70 61 67 65 73 20 0d 0a 20 20 20 20 20 20 2a   pages ..      *
438cd 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6c  * that will be l
438ce 6f 61 64 65 64 20 69 66 20 61 6c 6c 20 73 75 62  oaded if all sub
438cf 73 65 71 75 65 6e 74 20 74 6f 6b 65 6e 73 20 61  sequent tokens a
438d0 72 65 20 64 65 66 65 72 72 65 64 2e 0d 0a 20 20  re deferred...  
438d1 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20 46 74      */..      Ft
438d2 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70  s3PhraseToken *p
438d3 54 6f 6b 65 6e 20 3d 20 70 54 43 2d 3e 70 54 6f  Token = pTC->pTo
438d4 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  ken;..      rc =
438d5 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 66 65   sqlite3Fts3Defe
438d6 72 54 6f 6b 65 6e 28 70 43 73 72 2c 20 70 54 6f  rToken(pCsr, pTo
438d7 6b 65 6e 2c 20 70 54 43 2d 3e 69 43 6f 6c 29 3b  ken, pTC->iCol);
438d8 0d 0a 20 20 20 20 20 20 66 74 73 33 53 65 67 52  ..      fts3SegR
438d9 65 61 64 65 72 43 75 72 73 6f 72 46 72 65 65 28  eaderCursorFree(
438da 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63 73 72 29  pToken->pSegcsr)
438db 3b 0d 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 2d  ;..      pToken-
438dc 3e 70 53 65 67 63 73 72 20 3d 20 30 3b 0d 0a 20  >pSegcsr = 0;.. 
438dd 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
438de 20 2f 2a 20 53 65 74 20 6e 4c 6f 61 64 34 20 74   /* Set nLoad4 t
438df 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 28  o the value of (
438e0 34 5e 6e 4f 74 68 65 72 29 20 66 6f 72 20 74 68  4^nOther) for th
438e1 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
438e2 20 6f 66 20 74 68 65 0d 0a 20 20 20 20 20 20 2a   of the..      *
438e3 2a 20 66 6f 72 2d 6c 6f 6f 70 2e 20 45 78 63 65  * for-loop. Exce
438e4 70 74 2c 20 6c 69 6d 69 74 20 74 68 65 20 76 61  pt, limit the va
438e5 6c 75 65 20 74 6f 20 32 5e 32 34 20 74 6f 20 70  lue to 2^24 to p
438e6 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 0d  revent it from .
438e7 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c  .      ** overfl
438e8 6f 77 69 6e 67 20 74 68 65 20 33 32 2d 62 69 74  owing the 32-bit
438e9 20 69 6e 74 65 67 65 72 20 69 74 20 69 73 20 73   integer it is s
438ea 74 6f 72 65 64 20 69 6e 2e 20 2a 2f 0d 0a 20 20  tored in. */..  
438eb 20 20 20 20 69 66 28 20 69 69 3c 31 32 20 29 20      if( ii<12 ) 
438ec 6e 4c 6f 61 64 34 20 3d 20 6e 4c 6f 61 64 34 2a  nLoad4 = nLoad4*
438ed 34 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20  4;....      if( 
438ee 69 69 3d 3d 30 20 7c 7c 20 70 54 43 2d 3e 70 50  ii==0 || pTC->pP
438ef 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e 31 20  hrase->nToken>1 
438f0 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 45  ){..        /* E
438f1 69 74 68 65 72 20 74 68 69 73 20 69 73 20 74 68  ither this is th
438f2 65 20 63 68 65 61 70 65 73 74 20 74 6f 6b 65 6e  e cheapest token
438f3 20 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 71   in the entire q
438f4 75 65 72 79 2c 20 6f 72 20 69 74 20 69 73 0d 0a  uery, or it is..
438f5 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 74 20          ** part 
438f6 6f 66 20 61 20 6d 75 6c 74 69 2d 74 6f 6b 65 6e  of a multi-token
438f7 20 70 68 72 61 73 65 2e 20 45 69 74 68 65 72 20   phrase. Either 
438f8 77 61 79 2c 20 74 68 65 20 65 6e 74 69 72 65 20  way, the entire 
438f9 64 6f 63 6c 69 73 74 20 77 69 6c 6c 0d 0a 20 20  doclist will..  
438fa 20 20 20 20 20 20 2a 2a 20 28 65 76 65 6e 74 75        ** (eventu
438fb 61 6c 6c 79 29 20 62 65 20 6c 6f 61 64 65 64 20  ally) be loaded 
438fc 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 74 20  into memory. It 
438fd 6d 61 79 20 61 73 20 77 65 6c 6c 20 62 65 20 6e  may as well be n
438fe 6f 77 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  ow. */..        
438ff 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20  Fts3PhraseToken 
43900 2a 70 54 6f 6b 65 6e 20 3d 20 70 54 43 2d 3e 70  *pToken = pTC->p
43901 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20  Token;..        
43902 69 6e 74 20 6e 4c 69 73 74 20 3d 20 30 3b 0d 0a  int nList = 0;..
43903 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4c          char *pL
43904 69 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  ist = 0;..      
43905 20 20 72 63 20 3d 20 66 74 73 33 54 65 72 6d 53    rc = fts3TermS
43906 65 6c 65 63 74 28 70 54 61 62 2c 20 70 54 6f 6b  elect(pTab, pTok
43907 65 6e 2c 20 70 54 43 2d 3e 69 43 6f 6c 2c 20 26  en, pTC->iCol, &
43908 6e 4c 69 73 74 2c 20 26 70 4c 69 73 74 29 3b 0d  nList, &pList);.
43909 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
4390a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
4390b 7c 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0d 0a 20  | pList==0 );.. 
4390c 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
4390d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
4390e 20 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 75 6e         int nCoun
4390f 74 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74  t;..          ft
43910 73 33 45 76 61 6c 50 68 72 61 73 65 4d 65 72 67  s3EvalPhraseMerg
43911 65 54 6f 6b 65 6e 28 70 54 61 62 2c 20 70 54 43  eToken(pTab, pTC
43912 2d 3e 70 50 68 72 61 73 65 2c 20 70 54 43 2d 3e  ->pPhrase, pTC->
43913 69 54 6f 6b 65 6e 2c 70 4c 69 73 74 2c 6e 4c 69  iToken,pList,nLi
43914 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  st);..          
43915 6e 43 6f 75 6e 74 20 3d 20 66 74 73 33 44 6f 63  nCount = fts3Doc
43916 6c 69 73 74 43 6f 75 6e 74 44 6f 63 69 64 73 28  listCountDocids(
43917 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ..              
43918 70 54 43 2d 3e 70 50 68 72 61 73 65 2d 3e 64 6f  pTC->pPhrase->do
43919 63 6c 69 73 74 2e 61 41 6c 6c 2c 20 70 54 43 2d  clist.aAll, pTC-
4391a 3e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73  >pPhrase->doclis
4391b 74 2e 6e 41 6c 6c 0d 0a 20 20 20 20 20 20 20 20  t.nAll..        
4391c 20 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20    );..          
4391d 69 66 28 20 69 69 3d 3d 30 20 7c 7c 20 6e 43 6f  if( ii==0 || nCo
4391e 75 6e 74 3c 6e 4d 69 6e 45 73 74 20 29 20 6e 4d  unt<nMinEst ) nM
4391f 69 6e 45 73 74 20 3d 20 6e 43 6f 75 6e 74 3b 0d  inEst = nCount;.
43920 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
43921 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
43922 70 54 43 2d 3e 70 54 6f 6b 65 6e 20 3d 20 30 3b  pTC->pToken = 0;
43923 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
43924 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
43925 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
43926 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
43927 77 69 74 68 69 6e 20 74 68 65 20 78 46 69 6c 74  within the xFilt
43928 65 72 20 6d 65 74 68 6f 64 2e 20 49 74 20 69 6e  er method. It in
43929 69 74 69 61 6c 69 7a 65 73 0d 0a 2a 2a 20 74 68  itializes..** th
4392a 65 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72  e full-text quer
4392b 79 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72  y currently stor
4392c 65 64 20 69 6e 20 70 43 73 72 2d 3e 70 45 78 70  ed in pCsr->pExp
4392d 72 2e 20 54 6f 20 69 74 65 72 61 74 65 20 74 68  r. To iterate th
4392e 72 6f 75 67 68 0d 0a 2a 2a 20 74 68 65 20 72 65  rough..** the re
4392f 73 75 6c 74 73 20 6f 66 20 61 20 71 75 65 72 79  sults of a query
43930 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 64 6f 65  , the caller doe
43931 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 66 74  s:..**..**    ft
43932 73 33 45 76 61 6c 53 74 61 72 74 28 70 43 73 72  s3EvalStart(pCsr
43933 29 3b 0d 0a 2a 2a 20 20 20 20 77 68 69 6c 65 28  );..**    while(
43934 20 31 20 29 7b 0d 0a 2a 2a 20 20 20 20 20 20 66   1 ){..**      f
43935 74 73 33 45 76 61 6c 4e 65 78 74 28 70 43 73 72  ts3EvalNext(pCsr
43936 29 3b 0d 0a 2a 2a 20 20 20 20 20 20 69 66 28 20  );..**      if( 
43937 70 43 73 72 2d 3e 62 45 6f 66 20 29 20 62 72 65  pCsr->bEof ) bre
43938 61 6b 3b 0d 0a 2a 2a 20 20 20 20 20 20 2e 2e 2e  ak;..**      ...
43939 20 72 65 74 75 72 6e 20 72 6f 77 20 70 43 73 72   return row pCsr
4393a 2d 3e 69 50 72 65 76 49 64 20 74 6f 20 74 68 65  ->iPrevId to the
4393b 20 63 61 6c 6c 65 72 20 2e 2e 2e 0d 0a 2a 2a 20   caller .....** 
4393c 20 20 20 7d 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63     }..*/..static
4393d 20 69 6e 74 20 66 74 73 33 45 76 61 6c 53 74 61   int fts3EvalSta
4393e 72 74 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70  rt(Fts3Cursor *p
4393f 43 73 72 29 7b 0d 0a 20 20 46 74 73 33 54 61 62  Csr){..  Fts3Tab
43940 6c 65 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33  le *pTab = (Fts3
43941 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61  Table *)pCsr->ba
43942 73 65 2e 70 56 74 61 62 3b 0d 0a 20 20 69 6e 74  se.pVtab;..  int
43943 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
43944 0d 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d  ..  int nToken =
43945 20 30 3b 0d 0a 20 20 69 6e 74 20 6e 4f 72 20 3d   0;..  int nOr =
43946 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f   0;....  /* Allo
43947 63 61 74 65 20 61 20 4d 75 6c 74 69 53 65 67 52  cate a MultiSegR
43948 65 61 64 65 72 20 66 6f 72 20 65 61 63 68 20 74  eader for each t
43949 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72  oken in the expr
4394a 65 73 73 69 6f 6e 2e 20 2a 2f 0d 0a 20 20 66 74  ession. */..  ft
4394b 73 33 45 76 61 6c 41 6c 6c 6f 63 61 74 65 52 65  s3EvalAllocateRe
4394c 61 64 65 72 73 28 70 43 73 72 2c 20 70 43 73 72  aders(pCsr, pCsr
4394d 2d 3e 70 45 78 70 72 2c 20 26 6e 54 6f 6b 65 6e  ->pExpr, &nToken
4394e 2c 20 26 6e 4f 72 2c 20 26 72 63 29 3b 0d 0a 0d  , &nOr, &rc);...
4394f 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20  .  /* Determine 
43950 77 68 69 63 68 2c 20 69 66 20 61 6e 79 2c 20 74  which, if any, t
43951 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 65 78 70  okens in the exp
43952 72 65 73 73 69 6f 6e 20 73 68 6f 75 6c 64 20 62  ression should b
43953 65 20 64 65 66 65 72 72 65 64 2e 20 2a 2f 0d 0a  e deferred. */..
43954 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
43955 5f 4f 4b 20 26 26 20 6e 54 6f 6b 65 6e 3e 31 20  _OK && nToken>1 
43956 26 26 20 70 54 61 62 2d 3e 62 48 61 73 53 74 61  && pTab->bHasSta
43957 74 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 54 6f  t ){..    Fts3To
43958 6b 65 6e 41 6e 64 43 6f 73 74 20 2a 61 54 43 3b  kenAndCost *aTC;
43959 0d 0a 20 20 20 20 46 74 73 33 45 78 70 72 20 2a  ..    Fts3Expr *
4395a 2a 61 70 4f 72 3b 0d 0a 20 20 20 20 61 54 43 20  *apOr;..    aTC 
4395b 3d 20 28 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43  = (Fts3TokenAndC
4395c 6f 73 74 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  ost *)sqlite3_ma
4395d 6c 6c 6f 63 28 0d 0a 20 20 20 20 20 20 20 20 73  lloc(..        s
4395e 69 7a 65 6f 66 28 46 74 73 33 54 6f 6b 65 6e 41  izeof(Fts3TokenA
4395f 6e 64 43 6f 73 74 29 20 2a 20 6e 54 6f 6b 65 6e  ndCost) * nToken
43960 0d 0a 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66  ..      + sizeof
43961 28 46 74 73 33 45 78 70 72 20 2a 29 20 2a 20 6e  (Fts3Expr *) * n
43962 4f 72 20 2a 20 32 0d 0a 20 20 20 20 29 3b 0d 0a  Or * 2..    );..
43963 20 20 20 20 61 70 4f 72 20 3d 20 28 46 74 73 33      apOr = (Fts3
43964 45 78 70 72 20 2a 2a 29 26 61 54 43 5b 6e 54 6f  Expr **)&aTC[nTo
43965 6b 65 6e 5d 3b 0d 0a 0d 0a 20 20 20 20 69 66 28  ken];....    if(
43966 20 21 61 54 43 20 29 7b 0d 0a 20 20 20 20 20 20   !aTC ){..      
43967 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
43968 4d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  M;..    }else{..
43969 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20        int ii;.. 
4396a 20 20 20 20 20 46 74 73 33 54 6f 6b 65 6e 41 6e       Fts3TokenAn
4396b 64 43 6f 73 74 20 2a 70 54 43 20 3d 20 61 54 43  dCost *pTC = aTC
4396c 3b 0d 0a 20 20 20 20 20 20 46 74 73 33 45 78 70  ;..      Fts3Exp
4396d 72 20 2a 2a 70 70 4f 72 20 3d 20 61 70 4f 72 3b  r **ppOr = apOr;
4396e 0d 0a 0d 0a 20 20 20 20 20 20 66 74 73 33 45 76  ....      fts3Ev
4396f 61 6c 54 6f 6b 65 6e 43 6f 73 74 73 28 70 43 73  alTokenCosts(pCs
43970 72 2c 20 30 2c 20 70 43 73 72 2d 3e 70 45 78 70  r, 0, pCsr->pExp
43971 72 2c 20 26 70 54 43 2c 20 26 70 70 4f 72 2c 20  r, &pTC, &ppOr, 
43972 26 72 63 29 3b 0d 0a 20 20 20 20 20 20 6e 54 6f  &rc);..      nTo
43973 6b 65 6e 20 3d 20 70 54 43 2d 61 54 43 3b 0d 0a  ken = pTC-aTC;..
43974 20 20 20 20 20 20 6e 4f 72 20 3d 20 70 70 4f 72        nOr = ppOr
43975 2d 61 70 4f 72 3b 0d 0a 0d 0a 20 20 20 20 20 20  -apOr;....      
43976 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
43977 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63  K ){..        rc
43978 20 3d 20 66 74 73 33 45 76 61 6c 53 65 6c 65 63   = fts3EvalSelec
43979 74 44 65 66 65 72 72 65 64 28 70 43 73 72 2c 20  tDeferred(pCsr, 
4397a 30 2c 20 61 54 43 2c 20 6e 54 6f 6b 65 6e 29 3b  0, aTC, nToken);
4397b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69  ..        for(ii
4397c 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
4397d 4b 20 26 26 20 69 69 3c 6e 4f 72 3b 20 69 69 2b  K && ii<nOr; ii+
4397e 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72  +){..          r
4397f 63 20 3d 20 66 74 73 33 45 76 61 6c 53 65 6c 65  c = fts3EvalSele
43980 63 74 44 65 66 65 72 72 65 64 28 70 43 73 72 2c  ctDeferred(pCsr,
43981 20 61 70 4f 72 5b 69 69 5d 2c 20 61 54 43 2c 20   apOr[ii], aTC, 
43982 6e 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20 20 20  nToken);..      
43983 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a    }..      }....
43984 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
43985 65 65 28 61 54 43 29 3b 0d 0a 20 20 20 20 7d 0d  ee(aTC);..    }.
43986 0a 20 20 7d 0d 0a 0d 0a 20 20 66 74 73 33 45 76  .  }....  fts3Ev
43987 61 6c 53 74 61 72 74 52 65 61 64 65 72 73 28 70  alStartReaders(p
43988 43 73 72 2c 20 70 43 73 72 2d 3e 70 45 78 70 72  Csr, pCsr->pExpr
43989 2c 20 31 2c 20 26 72 63 29 3b 0d 0a 20 20 72 65  , 1, &rc);..  re
4398a 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
4398b 2a 0d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  *..** Invalidate
4398c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73   the current pos
4398d 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 70  ition list for p
4398e 68 72 61 73 65 20 70 50 68 72 61 73 65 2e 0d 0a  hrase pPhrase...
4398f 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
43990 66 74 73 33 45 76 61 6c 49 6e 76 61 6c 69 64 61  fts3EvalInvalida
43991 74 65 50 6f 73 6c 69 73 74 28 46 74 73 33 50 68  tePoslist(Fts3Ph
43992 72 61 73 65 20 2a 70 50 68 72 61 73 65 29 7b 0d  rase *pPhrase){.
43993 0a 20 20 69 66 28 20 70 50 68 72 61 73 65 2d 3e  .  if( pPhrase->
43994 64 6f 63 6c 69 73 74 2e 62 46 72 65 65 4c 69 73  doclist.bFreeLis
43995 74 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  t ){..    sqlite
43996 33 5f 66 72 65 65 28 70 50 68 72 61 73 65 2d 3e  3_free(pPhrase->
43997 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 29 3b 0d  doclist.pList);.
43998 0a 20 20 7d 0d 0a 20 20 70 50 68 72 61 73 65 2d  .  }..  pPhrase-
43999 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 20 3d  >doclist.pList =
4399a 20 30 3b 0d 0a 20 20 70 50 68 72 61 73 65 2d 3e   0;..  pPhrase->
4399b 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 3d 20  doclist.nList = 
4399c 30 3b 0d 0a 20 20 70 50 68 72 61 73 65 2d 3e 64  0;..  pPhrase->d
4399d 6f 63 6c 69 73 74 2e 62 46 72 65 65 4c 69 73 74  oclist.bFreeList
4399e 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a   = 0;..}..../*..
4399f 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
439a0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 65 64   is called to ed
439a1 69 74 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20  it the position 
439a2 6c 69 73 74 20 61 73 73 6f 63 69 61 74 65 64 20  list associated 
439a3 77 69 74 68 0d 0a 2a 2a 20 74 68 65 20 70 68 72  with..** the phr
439a4 61 73 65 20 6f 62 6a 65 63 74 20 70 61 73 73 65  ase object passe
439a5 64 20 61 73 20 74 68 65 20 66 69 66 74 68 20 61  d as the fifth a
439a6 72 67 75 6d 65 6e 74 20 61 63 63 6f 72 64 69 6e  rgument accordin
439a7 67 20 74 6f 20 61 20 4e 45 41 52 0d 0a 2a 2a 20  g to a NEAR..** 
439a8 63 6f 6e 64 69 74 69 6f 6e 2e 20 46 6f 72 20 65  condition. For e
439a9 78 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  xample:..**..** 
439aa 20 20 20 20 61 62 63 20 4e 45 41 52 2f 35 20 22      abc NEAR/5 "
439ab 64 65 66 20 67 68 69 22 0d 0a 2a 2a 0d 0a 2a 2a  def ghi"..**..**
439ac 20 50 61 72 61 6d 65 74 65 72 20 6e 4e 65 61 72   Parameter nNear
439ad 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20 4e   is passed the N
439ae 45 41 52 20 64 69 73 74 61 6e 63 65 20 6f 66 20  EAR distance of 
439af 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28  the expression (
439b0 35 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 65 78 61  5 in..** the exa
439b1 6d 70 6c 65 20 61 62 6f 76 65 29 2e 20 57 68 65  mple above). Whe
439b2 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
439b3 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 61 50 6f  is called, *paPo
439b4 73 6c 69 73 74 20 70 6f 69 6e 74 73 20 74 6f 0d  slist points to.
439b5 0a 2a 2a 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  .** the position
439b6 20 6c 69 73 74 2c 20 61 6e 64 20 2a 70 6e 54 6f   list, and *pnTo
439b7 6b 65 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ken is the numbe
439b8 72 20 6f 66 20 70 68 72 61 73 65 20 74 6f 6b 65  r of phrase toke
439b9 6e 73 20 69 6e 2c 20 74 68 65 0d 0a 2a 2a 20 70  ns in, the..** p
439ba 68 72 61 73 65 20 6f 6e 20 74 68 65 20 6f 74 68  hrase on the oth
439bb 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 4e  er side of the N
439bc 45 41 52 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  EAR operator to 
439bd 70 50 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61  pPhrase. For exa
439be 6d 70 6c 65 2c 0d 0a 2a 2a 20 69 66 20 70 50 68  mple,..** if pPh
439bf 72 61 73 65 20 72 65 66 65 72 73 20 74 6f 20 74  rase refers to t
439c0 68 65 20 22 64 65 66 20 67 68 69 22 20 70 68 72  he "def ghi" phr
439c1 61 73 65 2c 20 74 68 65 6e 20 2a 70 61 50 6f 73  ase, then *paPos
439c2 6c 69 73 74 20 70 6f 69 6e 74 73 20 74 6f 0d 0a  list points to..
439c3 2a 2a 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20  ** the position 
439c4 6c 69 73 74 20 61 73 73 6f 63 69 61 74 65 64 20  list associated 
439c5 77 69 74 68 20 70 68 72 61 73 65 20 22 61 62 63  with phrase "abc
439c6 22 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 6c 6c 20 70  "...**..** All p
439c7 6f 73 69 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  ositions in the 
439c8 70 50 68 72 61 73 65 20 70 6f 73 69 74 69 6f 6e  pPhrase position
439c9 20 6c 69 73 74 20 74 68 61 74 20 61 72 65 20 6e   list that are n
439ca 6f 74 20 73 75 66 66 69 63 69 65 6e 74 6c 79 0d  ot sufficiently.
439cb 0a 2a 2a 20 63 6c 6f 73 65 20 74 6f 20 61 20 70  .** close to a p
439cc 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 2a  osition in the *
439cd 70 61 50 6f 73 6c 69 73 74 20 70 6f 73 69 74 69  paPoslist positi
439ce 6f 6e 20 6c 69 73 74 20 61 72 65 20 72 65 6d 6f  on list are remo
439cf 76 65 64 2e 20 49 66 20 74 68 69 73 0d 0a 2a 2a  ved. If this..**
439d0 20 6c 65 61 76 65 73 20 30 20 70 6f 73 69 74 69   leaves 0 positi
439d1 6f 6e 73 2c 20 7a 65 72 6f 20 69 73 20 72 65 74  ons, zero is ret
439d2 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
439d3 2c 20 6e 6f 6e 2d 7a 65 72 6f 2e 0d 0a 2a 2a 0d  , non-zero...**.
439d4 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72  .** Before retur
439d5 6e 69 6e 67 2c 20 2a 70 61 50 6f 73 6c 69 73 74  ning, *paPoslist
439d6 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
439d7 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69 6f 6e   to the position
439d8 20 6c 73 69 74 20 0d 0a 2a 2a 20 61 73 73 6f 63   lsit ..** assoc
439d9 69 61 74 65 64 20 77 69 74 68 20 70 50 68 72 61  iated with pPhra
439da 73 65 2e 20 41 6e 64 20 2a 70 6e 54 6f 6b 65 6e  se. And *pnToken
439db 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
439dc 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
439dd 69 6e 0d 0a 2a 2a 20 70 50 68 72 61 73 65 2e 0d  in..** pPhrase..
439de 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
439df 66 74 73 33 45 76 61 6c 4e 65 61 72 54 72 69 6d  fts3EvalNearTrim
439e0 28 0d 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 20  (..  int nNear, 
439e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439e2 20 20 20 20 20 2f 2a 20 4e 45 41 52 20 64 69 73       /* NEAR dis
439e3 74 61 6e 63 65 2e 20 41 73 20 69 6e 20 22 4e 45  tance. As in "NE
439e4 41 52 2f 6e 4e 65 61 72 22 2e 20 2a 2f 0d 0a 20  AR/nNear". */.. 
439e5 20 63 68 61 72 20 2a 61 54 6d 70 2c 20 20 20 20   char *aTmp,    
439e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439e7 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70   /* Temporary sp
439e8 61 63 65 20 74 6f 20 75 73 65 20 2a 2f 0d 0a 20  ace to use */.. 
439e9 20 63 68 61 72 20 2a 2a 70 61 50 6f 73 6c 69 73   char **paPoslis
439ea 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
439eb 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 6f 73 69   /* IN/OUT: Posi
439ec 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0d 0a 20 20  tion list */..  
439ed 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 2c 20 20 20  int *pnToken,   
439ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439ef 2f 2a 20 49 4e 2f 4f 55 54 3a 20 54 6f 6b 65 6e  /* IN/OUT: Token
439f0 73 20 69 6e 20 70 68 72 61 73 65 20 6f 66 20 2a  s in phrase of *
439f1 70 61 50 6f 73 6c 69 73 74 20 2a 2f 0d 0a 20 20  paPoslist */..  
439f2 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72  Fts3Phrase *pPhr
439f3 61 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ase             
439f4 2f 2a 20 54 68 65 20 70 68 72 61 73 65 20 6f 62  /* The phrase ob
439f5 6a 65 63 74 20 74 6f 20 74 72 69 6d 20 74 68 65  ject to trim the
439f6 20 64 6f 63 6c 69 73 74 20 6f 66 20 2a 2f 0d 0a   doclist of */..
439f7 29 7b 0d 0a 20 20 69 6e 74 20 6e 50 61 72 61 6d  ){..  int nParam
439f8 31 20 3d 20 6e 4e 65 61 72 20 2b 20 70 50 68 72  1 = nNear + pPhr
439f9 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0d 0a 20 20  ase->nToken;..  
439fa 69 6e 74 20 6e 50 61 72 61 6d 32 20 3d 20 6e 4e  int nParam2 = nN
439fb 65 61 72 20 2b 20 2a 70 6e 54 6f 6b 65 6e 3b 0d  ear + *pnToken;.
439fc 0a 20 20 69 6e 74 20 6e 4e 65 77 3b 0d 0a 20 20  .  int nNew;..  
439fd 63 68 61 72 20 2a 70 32 3b 20 0d 0a 20 20 63 68  char *p2; ..  ch
439fe 61 72 20 2a 70 4f 75 74 3b 20 0d 0a 20 20 69 6e  ar *pOut; ..  in
439ff 74 20 72 65 73 3b 0d 0a 0d 0a 20 20 61 73 73 65  t res;....  asse
43a00 72 74 28 20 70 50 68 72 61 73 65 2d 3e 64 6f 63  rt( pPhrase->doc
43a01 6c 69 73 74 2e 70 4c 69 73 74 20 29 3b 0d 0a 0d  list.pList );...
43a02 0a 20 20 70 32 20 3d 20 70 4f 75 74 20 3d 20 70  .  p2 = pOut = p
43a03 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e  Phrase->doclist.
43a04 70 4c 69 73 74 3b 0d 0a 20 20 72 65 73 20 3d 20  pList;..  res = 
43a05 66 74 73 33 50 6f 73 6c 69 73 74 4e 65 61 72 4d  fts3PoslistNearM
43a06 65 72 67 65 28 0d 0a 20 20 20 20 26 70 4f 75 74  erge(..    &pOut
43a07 2c 20 61 54 6d 70 2c 20 6e 50 61 72 61 6d 31 2c  , aTmp, nParam1,
43a08 20 6e 50 61 72 61 6d 32 2c 20 70 61 50 6f 73 6c   nParam2, paPosl
43a09 69 73 74 2c 20 26 70 32 0d 0a 20 20 29 3b 0d 0a  ist, &p2..  );..
43a0a 20 20 69 66 28 20 72 65 73 20 29 7b 0d 0a 20 20    if( res ){..  
43a0b 20 20 6e 4e 65 77 20 3d 20 28 70 4f 75 74 20 2d    nNew = (pOut -
43a0c 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73   pPhrase->doclis
43a0d 74 2e 70 4c 69 73 74 29 20 2d 20 31 3b 0d 0a 20  t.pList) - 1;.. 
43a0e 20 20 20 61 73 73 65 72 74 28 20 70 50 68 72 61     assert( pPhra
43a0f 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73  se->doclist.pLis
43a10 74 5b 6e 4e 65 77 5d 3d 3d 27 5c 30 27 20 29 3b  t[nNew]=='\0' );
43a11 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4e  ..    assert( nN
43a12 65 77 3c 3d 70 50 68 72 61 73 65 2d 3e 64 6f 63  ew<=pPhrase->doc
43a13 6c 69 73 74 2e 6e 4c 69 73 74 20 26 26 20 6e 4e  list.nList && nN
43a14 65 77 3e 30 20 29 3b 0d 0a 20 20 20 20 6d 65 6d  ew>0 );..    mem
43a15 73 65 74 28 26 70 50 68 72 61 73 65 2d 3e 64 6f  set(&pPhrase->do
43a16 63 6c 69 73 74 2e 70 4c 69 73 74 5b 6e 4e 65 77  clist.pList[nNew
43a17 5d 2c 20 30 2c 20 70 50 68 72 61 73 65 2d 3e 64  ], 0, pPhrase->d
43a18 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 2d 20 6e  oclist.nList - n
43a19 4e 65 77 29 3b 0d 0a 20 20 20 20 70 50 68 72 61  New);..    pPhra
43a1a 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69 73  se->doclist.nLis
43a1b 74 20 3d 20 6e 4e 65 77 3b 0d 0a 20 20 20 20 2a  t = nNew;..    *
43a1c 70 61 50 6f 73 6c 69 73 74 20 3d 20 70 50 68 72  paPoslist = pPhr
43a1d 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69  ase->doclist.pLi
43a1e 73 74 3b 0d 0a 20 20 20 20 2a 70 6e 54 6f 6b 65  st;..    *pnToke
43a1f 6e 20 3d 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f  n = pPhrase->nTo
43a20 6b 65 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72  ken;..  }....  r
43a21 65 74 75 72 6e 20 72 65 73 3b 0d 0a 7d 0d 0a 0d  eturn res;..}...
43a22 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
43a23 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
43a24 20 69 66 20 2a 70 52 63 20 69 73 20 6f 74 68 65   if *pRc is othe
43a25 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b  r than SQLITE_OK
43a26 20 77 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c   when it is call
43a27 65 64 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed...** Otherwis
43a28 65 2c 20 69 74 20 61 64 76 61 6e 63 65 73 20 74  e, it advances t
43a29 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61  he expression pa
43a2a 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
43a2b 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0d 0a  nd argument to..
43a2c 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  ** point to the 
43a2d 6e 65 78 74 20 6d 61 74 63 68 69 6e 67 20 72 6f  next matching ro
43a2e 77 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  w in the databas
43a2f 65 2e 20 45 78 70 72 65 73 73 69 6f 6e 73 20 69  e. Expressions i
43a30 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 0d 0a  terate through..
43a31 2a 2a 20 6d 61 74 63 68 69 6e 67 20 72 6f 77 73  ** matching rows
43a32 20 69 6e 20 64 6f 63 69 64 20 6f 72 64 65 72 2e   in docid order.
43a33 20 41 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   Ascending order
43a34 20 69 66 20 46 74 73 33 43 75 72 73 6f 72 2e 62   if Fts3Cursor.b
43a35 44 65 73 63 20 69 73 20 7a 65 72 6f 2c 0d 0a 2a  Desc is zero,..*
43a36 2a 20 6f 72 20 64 65 73 63 65 6e 64 69 6e 67 20  * or descending 
43a37 69 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72  if it is non-zer
43a38 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e  o...**..** If an
43a39 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a   error occurs, *
43a3a 70 52 63 20 69 73 20 73 65 74 20 74 6f 20 61 6e  pRc is set to an
43a3b 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
43a3c 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  de. Otherwise, i
43a3d 66 0d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  f..** successful
43a3e 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
43a3f 76 61 72 69 61 62 6c 65 73 20 69 6e 20 70 45 78  variables in pEx
43a40 70 72 20 61 72 65 20 73 65 74 3a 0d 0a 2a 2a 0d  pr are set:..**.
43a41 0a 2a 2a 20 20 20 46 74 73 33 45 78 70 72 2e 62  .**   Fts3Expr.b
43a42 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Eof             
43a43 20 20 20 28 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20     (non-zero if 
43a44 45 4f 46 20 2d 20 74 68 65 72 65 20 69 73 20 6e  EOF - there is n
43a45 6f 20 6e 65 78 74 20 72 6f 77 29 0d 0a 2a 2a 20  o next row)..** 
43a46 20 20 46 74 73 33 45 78 70 72 2e 69 44 6f 63 69    Fts3Expr.iDoci
43a47 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  d              (
43a48 76 61 6c 69 64 20 69 66 20 62 45 6f 66 3d 3d 30  valid if bEof==0
43a49 2e 20 54 68 65 20 64 6f 63 69 64 20 6f 66 20 74  . The docid of t
43a4a 68 65 20 6e 65 78 74 20 72 6f 77 29 0d 0a 2a 2a  he next row)..**
43a4b 0d 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  ..** If the expr
43a4c 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 79 70  ession is of typ
43a4d 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
43a4e 45 2c 20 61 6e 64 20 74 68 65 20 65 78 70 72 65  E, and the expre
43a4f 73 73 69 6f 6e 20 69 73 20 6e 6f 74 0d 0a 2a 2a  ssion is not..**
43a50 20 61 74 20 45 4f 46 2c 20 74 68 65 6e 20 74 68   at EOF, then th
43a51 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
43a52 61 62 6c 65 73 20 61 72 65 20 70 6f 70 75 6c 61  ables are popula
43a53 74 65 64 20 77 69 74 68 20 74 68 65 20 70 6f 73  ted with the pos
43a54 69 74 69 6f 6e 20 6c 69 73 74 0d 0a 2a 2a 20 66  ition list..** f
43a55 6f 72 20 74 68 65 20 70 68 72 61 73 65 20 66 6f  or the phrase fo
43a56 72 20 74 68 65 20 76 69 73 69 74 65 64 20 72 6f  r the visited ro
43a57 77 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 46 54 73  w:..**..**   FTs
43a58 33 45 78 70 72 2e 70 50 68 72 61 73 65 2d 3e 64  3Expr.pPhrase->d
43a59 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 20 20 20  oclist.nList    
43a5a 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 70      (length of p
43a5b 4c 69 73 74 20 69 6e 20 62 79 74 65 73 29 0d 0a  List in bytes)..
43a5c 2a 2a 20 20 20 46 54 73 33 45 78 70 72 2e 70 50  **   FTs3Expr.pP
43a5d 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70  hrase->doclist.p
43a5e 4c 69 73 74 20 20 20 20 20 20 20 20 28 70 6f 69  List        (poi
43a5f 6e 74 65 72 20 74 6f 20 70 6f 73 69 74 69 6f 6e  nter to position
43a60 20 6c 69 73 74 29 0d 0a 2a 2a 0d 0a 2a 2a 20 49   list)..**..** I
43a61 74 20 73 61 79 73 20 61 62 6f 76 65 20 74 68 61  t says above tha
43a62 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  t this function 
43a63 61 64 76 61 6e 63 65 73 20 74 68 65 20 65 78 70  advances the exp
43a64 72 65 73 73 69 6f 6e 20 74 6f 20 74 68 65 20 6e  ression to the n
43a65 65 78 74 0d 0a 2a 2a 20 6d 61 74 63 68 69 6e 67  ext..** matching
43a66 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73   row. This is us
43a67 75 61 6c 6c 79 20 74 72 75 65 2c 20 62 75 74 20  ually true, but 
43a68 74 68 65 72 65 20 61 72 65 20 74 68 65 20 66 6f  there are the fo
43a69 6c 6c 6f 77 69 6e 67 20 65 78 63 65 70 74 69 6f  llowing exceptio
43a6a 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31 2e  ns:..**..**   1.
43a6b 20 44 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73   Deferred tokens
43a6c 20 61 72 65 20 6e 6f 74 20 74 61 6b 65 6e 20 69   are not taken i
43a6d 6e 74 6f 20 61 63 63 6f 75 6e 74 2e 20 49 66 20  nto account. If 
43a6e 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69 73 74  a phrase consist
43a6f 73 0d 0a 2a 2a 20 20 20 20 20 20 65 6e 74 69 72  s..**      entir
43a70 65 6c 79 20 6f 66 20 64 65 66 65 72 72 65 64 20  ely of deferred 
43a71 74 6f 6b 65 6e 73 2c 20 69 74 20 69 73 20 61 73  tokens, it is as
43a72 73 75 6d 65 64 20 74 6f 20 6d 61 74 63 68 20 65  sumed to match e
43a73 76 65 72 79 20 72 6f 77 20 69 6e 0d 0a 2a 2a 20  very row in..** 
43a74 20 20 20 20 20 74 68 65 20 64 62 2e 20 49 6e 20       the db. In 
43a75 74 68 69 73 20 63 61 73 65 20 74 68 65 20 70 6f  this case the po
43a76 73 69 74 69 6f 6e 2d 6c 69 73 74 20 69 73 20 6e  sition-list is n
43a77 6f 74 20 70 6f 70 75 6c 61 74 65 64 20 61 74 20  ot populated at 
43a78 61 6c 6c 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  all. ..**..**   
43a79 20 20 20 4f 72 2c 20 69 66 20 61 20 70 68 72 61     Or, if a phra
43a7a 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  se contains one 
43a7b 6f 72 20 6d 6f 72 65 20 64 65 66 65 72 72 65 64  or more deferred
43a7c 20 74 6f 6b 65 6e 73 20 61 6e 64 20 6f 6e 65 20   tokens and one 
43a7d 6f 72 0d 0a 2a 2a 20 20 20 20 20 20 6d 6f 72 65  or..**      more
43a7e 20 6e 6f 6e 2d 64 65 66 65 72 72 65 64 20 74 6f   non-deferred to
43a7f 6b 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 65  kens, then the e
43a80 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 64 76  xpression is adv
43a81 61 6e 63 65 64 20 74 6f 20 74 68 65 20 0d 0a 2a  anced to the ..*
43a82 2a 20 20 20 20 20 20 6e 65 78 74 20 70 6f 73 73  *      next poss
43a83 69 62 6c 65 20 6d 61 74 63 68 2c 20 63 6f 6e 73  ible match, cons
43a84 69 64 65 72 69 6e 67 20 6f 6e 6c 79 20 6e 6f 6e  idering only non
43a85 2d 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73  -deferred tokens
43a86 2e 20 49 6e 20 6f 74 68 65 72 0d 0a 2a 2a 20 20  . In other..**  
43a87 20 20 20 20 77 6f 72 64 73 2c 20 69 66 20 74 68      words, if th
43a88 65 20 70 68 72 61 73 65 20 69 73 20 22 41 20 42  e phrase is "A B
43a89 20 43 22 2c 20 61 6e 64 20 22 42 22 20 69 73 20   C", and "B" is 
43a8a 64 65 66 65 72 72 65 64 2c 20 74 68 65 20 65 78  deferred, the ex
43a8b 70 72 65 73 73 69 6f 6e 0d 0a 2a 2a 20 20 20 20  pression..**    
43a8c 20 20 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f    is advanced to
43a8d 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 74 68   the next row th
43a8e 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69  at contains an i
43a8f 6e 73 74 61 6e 63 65 20 6f 66 20 22 41 20 2a 20  nstance of "A * 
43a90 43 22 2c 20 0d 0a 2a 2a 20 20 20 20 20 20 77 68  C", ..**      wh
43a91 65 72 65 20 22 2a 22 20 6d 61 79 20 6d 61 74 63  ere "*" may matc
43a92 68 20 61 6e 79 20 73 69 6e 67 6c 65 20 74 6f 6b  h any single tok
43a93 65 6e 2e 20 54 68 65 20 70 6f 73 69 74 69 6f 6e  en. The position
43a94 20 6c 69 73 74 20 69 6e 20 74 68 69 73 20 63 61   list in this ca
43a95 73 65 0d 0a 2a 2a 20 20 20 20 20 20 69 73 20 70  se..**      is p
43a96 6f 70 75 6c 61 74 65 64 20 61 73 20 66 6f 72 20  opulated as for 
43a97 22 41 20 2a 20 43 22 20 62 65 66 6f 72 65 20 72  "A * C" before r
43a98 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a  eturning...**..*
43a99 2a 20 20 20 32 2e 20 4e 45 41 52 20 69 73 20 74  *   2. NEAR is t
43a9a 72 65 61 74 65 64 20 61 73 20 41 4e 44 2e 20 49  reated as AND. I
43a9b 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
43a9c 20 69 73 20 22 78 20 4e 45 41 52 20 79 22 2c 20   is "x NEAR y", 
43a9d 69 74 20 69 73 20 0d 0a 2a 2a 20 20 20 20 20 20  it is ..**      
43a9e 61 64 76 61 6e 63 65 64 20 74 6f 20 70 6f 69 6e  advanced to poin
43a9f 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f  t to the next ro
43aa0 77 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 22  w that matches "
43aa1 78 20 41 4e 44 20 79 22 2e 0d 0a 2a 2a 20 0d 0a  x AND y"...** ..
43aa2 2a 2a 20 53 65 65 20 66 74 73 33 45 76 61 6c 54  ** See fts3EvalT
43aa3 65 73 74 44 65 66 65 72 72 65 64 41 6e 64 4e 65  estDeferredAndNe
43aa4 61 72 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73  ar() for details
43aa5 20 6f 6e 20 74 65 73 74 69 6e 67 20 69 66 20 61   on testing if a
43aa6 20 72 6f 77 20 69 73 0d 0a 2a 2a 20 72 65 61 6c   row is..** real
43aa7 6c 79 20 61 20 6d 61 74 63 68 2c 20 74 61 6b 69  ly a match, taki
43aa8 6e 67 20 69 6e 74 6f 20 61 63 63 6f 75 6e 74 20  ng into account 
43aa9 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 20  deferred tokens 
43aaa 61 6e 64 20 4e 45 41 52 20 6f 70 65 72 61 74 6f  and NEAR operato
43aab 72 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  rs...*/..static 
43aac 76 6f 69 64 20 66 74 73 33 45 76 61 6c 4e 65 78  void fts3EvalNex
43aad 74 52 6f 77 28 0d 0a 20 20 46 74 73 33 43 75 72  tRow(..  Fts3Cur
43aae 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20 20  sor *pCsr,      
43aaf 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 20           /* FTS 
43ab0 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a 2f  Cursor handle */
43ab1 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45  ..  Fts3Expr *pE
43ab2 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
43ab3 20 20 20 20 2f 2a 20 45 78 70 72 2e 20 74 6f 20      /* Expr. to 
43ab4 61 64 76 61 6e 63 65 20 74 6f 20 6e 65 78 74 20  advance to next 
43ab5 6d 61 74 63 68 69 6e 67 20 72 6f 77 20 2a 2f 0d  matching row */.
43ab6 0a 20 20 69 6e 74 20 2a 70 52 63 20 20 20 20 20  .  int *pRc     
43ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ab8 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 72     /* IN/OUT: Er
43ab9 72 6f 72 20 63 6f 64 65 20 2a 2f 0d 0a 29 7b 0d  ror code */..){.
43aba 0a 20 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c  .  if( *pRc==SQL
43abb 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69  ITE_OK ){..    i
43abc 6e 74 20 62 44 65 73 63 44 6f 63 6c 69 73 74 20  nt bDescDoclist 
43abd 3d 20 70 43 73 72 2d 3e 62 44 65 73 63 3b 20 20  = pCsr->bDesc;  
43abe 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62         /* Used b
43abf 79 20 44 4f 43 49 44 5f 43 4d 50 28 29 20 6d 61  y DOCID_CMP() ma
43ac0 63 72 6f 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65  cro */..    asse
43ac1 72 74 28 20 70 45 78 70 72 2d 3e 62 45 6f 66 3d  rt( pExpr->bEof=
43ac2 3d 30 20 29 3b 0d 0a 20 20 20 20 70 45 78 70 72  =0 );..    pExpr
43ac3 2d 3e 62 53 74 61 72 74 20 3d 20 31 3b 0d 0a 0d  ->bStart = 1;...
43ac4 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78  .    switch( pEx
43ac5 70 72 2d 3e 65 54 79 70 65 20 29 7b 0d 0a 20 20  pr->eType ){..  
43ac6 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
43ac7 59 5f 4e 45 41 52 3a 0d 0a 20 20 20 20 20 20 63  Y_NEAR:..      c
43ac8 61 73 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44  ase FTSQUERY_AND
43ac9 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 46 74 73  : {..        Fts
43aca 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70  3Expr *pLeft = p
43acb 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20  Expr->pLeft;..  
43acc 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
43acd 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
43ace 70 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 20  pRight;..       
43acf 20 61 73 73 65 72 74 28 20 21 70 4c 65 66 74 2d   assert( !pLeft-
43ad0 3e 62 44 65 66 65 72 72 65 64 20 7c 7c 20 21 70  >bDeferred || !p
43ad1 52 69 67 68 74 2d 3e 62 44 65 66 65 72 72 65 64  Right->bDeferred
43ad2 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69   );....        i
43ad3 66 28 20 70 4c 65 66 74 2d 3e 62 44 65 66 65 72  f( pLeft->bDefer
43ad4 72 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  red ){..        
43ad5 20 20 2f 2a 20 4c 48 53 20 69 73 20 65 6e 74 69    /* LHS is enti
43ad6 72 65 6c 79 20 64 65 66 65 72 72 65 64 2e 20 53  rely deferred. S
43ad7 6f 20 77 65 20 61 73 73 75 6d 65 20 69 74 20 6d  o we assume it m
43ad8 61 74 63 68 65 73 20 65 76 65 72 79 20 72 6f 77  atches every row
43ad9 2e 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ...          ** 
43ada 41 64 76 61 6e 63 65 20 74 68 65 20 52 48 53 20  Advance the RHS 
43adb 69 74 65 72 61 74 6f 72 20 74 6f 20 66 69 6e 64  iterator to find
43adc 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 76 69   the next row vi
43add 73 69 74 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 20  sited. */..     
43ade 20 20 20 20 20 66 74 73 33 45 76 61 6c 4e 65 78       fts3EvalNex
43adf 74 52 6f 77 28 70 43 73 72 2c 20 70 52 69 67 68  tRow(pCsr, pRigh
43ae0 74 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20 20 20  t, pRc);..      
43ae1 20 20 20 20 70 45 78 70 72 2d 3e 69 44 6f 63 69      pExpr->iDoci
43ae2 64 20 3d 20 70 52 69 67 68 74 2d 3e 69 44 6f 63  d = pRight->iDoc
43ae3 69 64 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  id;..          p
43ae4 45 78 70 72 2d 3e 62 45 6f 66 20 3d 20 70 52 69  Expr->bEof = pRi
43ae5 67 68 74 2d 3e 62 45 6f 66 3b 0d 0a 20 20 20 20  ght->bEof;..    
43ae6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52      }else if( pR
43ae7 69 67 68 74 2d 3e 62 44 65 66 65 72 72 65 64 20  ight->bDeferred 
43ae8 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ){..          /*
43ae9 20 52 48 53 20 69 73 20 65 6e 74 69 72 65 6c 79   RHS is entirely
43aea 20 64 65 66 65 72 72 65 64 2e 20 53 6f 20 77 65   deferred. So we
43aeb 20 61 73 73 75 6d 65 20 69 74 20 6d 61 74 63 68   assume it match
43aec 65 73 20 65 76 65 72 79 20 72 6f 77 2e 0d 0a 20  es every row... 
43aed 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64 76 61           ** Adva
43aee 6e 63 65 20 74 68 65 20 4c 48 53 20 69 74 65 72  nce the LHS iter
43aef 61 74 6f 72 20 74 6f 20 66 69 6e 64 20 74 68 65  ator to find the
43af0 20 6e 65 78 74 20 72 6f 77 20 76 69 73 69 74 65   next row visite
43af1 64 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20  d. */..         
43af2 20 66 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77   fts3EvalNextRow
43af3 28 70 43 73 72 2c 20 70 4c 65 66 74 2c 20 70 52  (pCsr, pLeft, pR
43af4 63 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  c);..          p
43af5 45 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d 20 70  Expr->iDocid = p
43af6 4c 65 66 74 2d 3e 69 44 6f 63 69 64 3b 0d 0a 20  Left->iDocid;.. 
43af7 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
43af8 62 45 6f 66 20 3d 20 70 4c 65 66 74 2d 3e 62 45  bEof = pLeft->bE
43af9 6f 66 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  of;..        }el
43afa 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2f  se{..          /
43afb 2a 20 4e 65 69 74 68 65 72 20 74 68 65 20 52 48  * Neither the RH
43afc 53 20 6f 72 20 4c 48 53 20 61 72 65 20 64 65 66  S or LHS are def
43afd 65 72 72 65 64 2e 20 2a 2f 0d 0a 20 20 20 20 20  erred. */..     
43afe 20 20 20 20 20 66 74 73 33 45 76 61 6c 4e 65 78       fts3EvalNex
43aff 74 52 6f 77 28 70 43 73 72 2c 20 70 4c 65 66 74  tRow(pCsr, pLeft
43b00 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20 20 20 20  , pRc);..       
43b01 20 20 20 66 74 73 33 45 76 61 6c 4e 65 78 74 52     fts3EvalNextR
43b02 6f 77 28 70 43 73 72 2c 20 70 52 69 67 68 74 2c  ow(pCsr, pRight,
43b03 20 70 52 63 29 3b 0d 0a 20 20 20 20 20 20 20 20   pRc);..        
43b04 20 20 77 68 69 6c 65 28 20 21 70 4c 65 66 74 2d    while( !pLeft-
43b05 3e 62 45 6f 66 20 26 26 20 21 70 52 69 67 68 74  >bEof && !pRight
43b06 2d 3e 62 45 6f 66 20 26 26 20 2a 70 52 63 3d 3d  ->bEof && *pRc==
43b07 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
43b08 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
43b09 33 5f 69 6e 74 36 34 20 69 44 69 66 66 20 3d 20  3_int64 iDiff = 
43b0a 44 4f 43 49 44 5f 43 4d 50 28 70 4c 65 66 74 2d  DOCID_CMP(pLeft-
43b0b 3e 69 44 6f 63 69 64 2c 20 70 52 69 67 68 74 2d  >iDocid, pRight-
43b0c 3e 69 44 6f 63 69 64 29 3b 0d 0a 20 20 20 20 20  >iDocid);..     
43b0d 20 20 20 20 20 20 20 69 66 28 20 69 44 69 66 66         if( iDiff
43b0e 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  ==0 ) break;..  
43b0f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 44            if( iD
43b10 69 66 66 3c 30 20 29 7b 0d 0a 20 20 20 20 20 20  iff<0 ){..      
43b11 20 20 20 20 20 20 20 20 66 74 73 33 45 76 61 6c          fts3Eval
43b12 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20 70 4c  NextRow(pCsr, pL
43b13 65 66 74 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20  eft, pRc);..    
43b14 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
43b15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 74                ft
43b16 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43  s3EvalNextRow(pC
43b17 73 72 2c 20 70 52 69 67 68 74 2c 20 70 52 63 29  sr, pRight, pRc)
43b18 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ;..            }
43b19 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
43b1a 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
43b1b 69 44 6f 63 69 64 20 3d 20 70 4c 65 66 74 2d 3e  iDocid = pLeft->
43b1c 69 44 6f 63 69 64 3b 0d 0a 20 20 20 20 20 20 20  iDocid;..       
43b1d 20 20 20 70 45 78 70 72 2d 3e 62 45 6f 66 20 3d     pExpr->bEof =
43b1e 20 28 70 4c 65 66 74 2d 3e 62 45 6f 66 20 7c 7c   (pLeft->bEof ||
43b1f 20 70 52 69 67 68 74 2d 3e 62 45 6f 66 29 3b 0d   pRight->bEof);.
43b20 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
43b21 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
43b22 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 20 20 63    }..  ..      c
43b23 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a  ase FTSQUERY_OR:
43b24 20 7b 0d 0a 20 20 20 20 20 20 20 20 46 74 73 33   {..        Fts3
43b25 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
43b26 78 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20 20  xpr->pLeft;..   
43b27 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70       Fts3Expr *p
43b28 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
43b29 52 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 20 20  Right;..        
43b2a 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 43  sqlite3_int64 iC
43b2b 6d 70 20 3d 20 44 4f 43 49 44 5f 43 4d 50 28 70  mp = DOCID_CMP(p
43b2c 4c 65 66 74 2d 3e 69 44 6f 63 69 64 2c 20 70 52  Left->iDocid, pR
43b2d 69 67 68 74 2d 3e 69 44 6f 63 69 64 29 3b 0d 0a  ight->iDocid);..
43b2e 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
43b2f 28 20 70 4c 65 66 74 2d 3e 62 53 74 61 72 74 20  ( pLeft->bStart 
43b30 7c 7c 20 70 4c 65 66 74 2d 3e 69 44 6f 63 69 64  || pLeft->iDocid
43b31 3d 3d 70 52 69 67 68 74 2d 3e 69 44 6f 63 69 64  ==pRight->iDocid
43b32 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73   );..        ass
43b33 65 72 74 28 20 70 52 69 67 68 74 2d 3e 62 53 74  ert( pRight->bSt
43b34 61 72 74 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 44  art || pLeft->iD
43b35 6f 63 69 64 3d 3d 70 52 69 67 68 74 2d 3e 69 44  ocid==pRight->iD
43b36 6f 63 69 64 20 29 3b 0d 0a 0d 0a 20 20 20 20 20  ocid );....     
43b37 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 62     if( pRight->b
43b38 45 6f 66 20 7c 7c 20 28 70 4c 65 66 74 2d 3e 62  Eof || (pLeft->b
43b39 45 6f 66 3d 3d 30 20 26 26 20 69 43 6d 70 3c 30  Eof==0 && iCmp<0
43b3a 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) ){..          
43b3b 66 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28  fts3EvalNextRow(
43b3c 70 43 73 72 2c 20 70 4c 65 66 74 2c 20 70 52 63  pCsr, pLeft, pRc
43b3d 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  );..        }els
43b3e 65 20 69 66 28 20 70 4c 65 66 74 2d 3e 62 45 6f  e if( pLeft->bEo
43b3f 66 20 7c 7c 20 28 70 52 69 67 68 74 2d 3e 62 45  f || (pRight->bE
43b40 6f 66 3d 3d 30 20 26 26 20 69 43 6d 70 3e 30 29  of==0 && iCmp>0)
43b41 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66   ){..          f
43b42 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70  ts3EvalNextRow(p
43b43 43 73 72 2c 20 70 52 69 67 68 74 2c 20 70 52 63  Csr, pRight, pRc
43b44 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  );..        }els
43b45 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74  e{..          ft
43b46 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43  s3EvalNextRow(pC
43b47 73 72 2c 20 70 4c 65 66 74 2c 20 70 52 63 29 3b  sr, pLeft, pRc);
43b48 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  ..          fts3
43b49 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72  EvalNextRow(pCsr
43b4a 2c 20 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0d  , pRight, pRc);.
43b4b 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  .        }....  
43b4c 20 20 20 20 20 20 70 45 78 70 72 2d 3e 62 45 6f        pExpr->bEo
43b4d 66 20 3d 20 28 70 4c 65 66 74 2d 3e 62 45 6f 66  f = (pLeft->bEof
43b4e 20 26 26 20 70 52 69 67 68 74 2d 3e 62 45 6f 66   && pRight->bEof
43b4f 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 43 6d 70  );..        iCmp
43b50 20 3d 20 44 4f 43 49 44 5f 43 4d 50 28 70 4c 65   = DOCID_CMP(pLe
43b51 66 74 2d 3e 69 44 6f 63 69 64 2c 20 70 52 69 67  ft->iDocid, pRig
43b52 68 74 2d 3e 69 44 6f 63 69 64 29 3b 0d 0a 20 20  ht->iDocid);..  
43b53 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
43b54 2d 3e 62 45 6f 66 20 7c 7c 20 28 70 4c 65 66 74  ->bEof || (pLeft
43b55 2d 3e 62 45 6f 66 3d 3d 30 20 26 26 20 20 69 43  ->bEof==0 &&  iC
43b56 6d 70 3c 30 29 20 29 7b 0d 0a 20 20 20 20 20 20  mp<0) ){..      
43b57 20 20 20 20 70 45 78 70 72 2d 3e 69 44 6f 63 69      pExpr->iDoci
43b58 64 20 3d 20 70 4c 65 66 74 2d 3e 69 44 6f 63 69  d = pLeft->iDoci
43b59 64 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  d;..        }els
43b5a 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 45  e{..          pE
43b5b 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d 20 70 52  xpr->iDocid = pR
43b5c 69 67 68 74 2d 3e 69 44 6f 63 69 64 3b 0d 0a 20  ight->iDocid;.. 
43b5d 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20         }....    
43b5e 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
43b5f 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 63 61 73    }....      cas
43b60 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 20  e FTSQUERY_NOT: 
43b61 7b 0d 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  {..        Fts3E
43b62 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78  xpr *pLeft = pEx
43b63 70 72 2d 3e 70 4c 65 66 74 3b 0d 0a 20 20 20 20  pr->pLeft;..    
43b64 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 52      Fts3Expr *pR
43b65 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52  ight = pExpr->pR
43b66 69 67 68 74 3b 0d 0a 0d 0a 20 20 20 20 20 20 20  ight;....       
43b67 20 69 66 28 20 70 52 69 67 68 74 2d 3e 62 53 74   if( pRight->bSt
43b68 61 72 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  art==0 ){..     
43b69 20 20 20 20 20 66 74 73 33 45 76 61 6c 4e 65 78       fts3EvalNex
43b6a 74 52 6f 77 28 70 43 73 72 2c 20 70 52 69 67 68  tRow(pCsr, pRigh
43b6b 74 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20 20 20  t, pRc);..      
43b6c 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 52 63      assert( *pRc
43b6d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  !=SQLITE_OK || p
43b6e 52 69 67 68 74 2d 3e 62 53 74 61 72 74 20 29 3b  Right->bStart );
43b6f 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  ..        }.... 
43b70 20 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 4e         fts3EvalN
43b71 65 78 74 52 6f 77 28 70 43 73 72 2c 20 70 4c 65  extRow(pCsr, pLe
43b72 66 74 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20 20  ft, pRc);..     
43b73 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 62 45     if( pLeft->bE
43b74 6f 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  of==0 ){..      
43b75 20 20 20 20 77 68 69 6c 65 28 20 21 2a 70 52 63      while( !*pRc
43b76 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ..             
43b77 20 26 26 20 21 70 52 69 67 68 74 2d 3e 62 45 6f   && !pRight->bEo
43b78 66 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  f ..            
43b79 20 20 26 26 20 44 4f 43 49 44 5f 43 4d 50 28 70    && DOCID_CMP(p
43b7a 4c 65 66 74 2d 3e 69 44 6f 63 69 64 2c 20 70 52  Left->iDocid, pR
43b7b 69 67 68 74 2d 3e 69 44 6f 63 69 64 29 3e 30 20  ight->iDocid)>0 
43b7c 0d 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0d 0a  ..          ){..
43b7d 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33              fts3
43b7e 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72  EvalNextRow(pCsr
43b7f 2c 20 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0d  , pRight, pRc);.
43b80 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
43b81 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
43b82 20 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d   pExpr->iDocid =
43b83 20 70 4c 65 66 74 2d 3e 69 44 6f 63 69 64 3b 0d   pLeft->iDocid;.
43b84 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
43b85 62 45 6f 66 20 3d 20 70 4c 65 66 74 2d 3e 62 45  bEof = pLeft->bE
43b86 6f 66 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65  of;..        bre
43b87 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a  ak;..      }....
43b88 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
43b89 0d 0a 20 20 20 20 20 20 20 20 46 74 73 33 50 68  ..        Fts3Ph
43b8a 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20  rase *pPhrase = 
43b8b 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0d  pExpr->pPhrase;.
43b8c 0a 20 20 20 20 20 20 20 20 66 74 73 33 45 76 61  .        fts3Eva
43b8d 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f 73 6c 69  lInvalidatePosli
43b8e 73 74 28 70 50 68 72 61 73 65 29 3b 0d 0a 20 20  st(pPhrase);..  
43b8f 20 20 20 20 20 20 2a 70 52 63 20 3d 20 66 74 73        *pRc = fts
43b90 33 45 76 61 6c 50 68 72 61 73 65 4e 65 78 74 28  3EvalPhraseNext(
43b91 70 43 73 72 2c 20 70 50 68 72 61 73 65 2c 20 26  pCsr, pPhrase, &
43b92 70 45 78 70 72 2d 3e 62 45 6f 66 29 3b 0d 0a 20  pExpr->bEof);.. 
43b93 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 44         pExpr->iD
43b94 6f 63 69 64 20 3d 20 70 50 68 72 61 73 65 2d 3e  ocid = pPhrase->
43b95 64 6f 63 6c 69 73 74 2e 69 44 6f 63 69 64 3b 0d  doclist.iDocid;.
43b96 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
43b97 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
43b98 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  .  }..}..../*..*
43b99 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e 6f 74  * If *pRc is not
43b9a 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 6f 72 20 69   SQLITE_OK, or i
43b9b 66 20 70 45 78 70 72 20 69 73 20 6e 6f 74 20 74  f pExpr is not t
43b9c 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
43b9d 61 20 4e 45 41 52 0d 0a 2a 2a 20 63 6c 75 73 74  a NEAR..** clust
43b9e 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  er, then this fu
43b9f 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 31  nction returns 1
43ba0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 2a   immediately...*
43ba1 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  *..** Otherwise,
43ba2 20 69 74 20 63 68 65 63 6b 73 20 69 66 20 74 68   it checks if th
43ba3 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 72 65  e current row re
43ba4 61 6c 6c 79 20 64 6f 65 73 20 6d 61 74 63 68 20  ally does match 
43ba5 74 68 65 20 4e 45 41 52 20 0d 0a 2a 2a 20 65 78  the NEAR ..** ex
43ba6 70 72 65 73 73 69 6f 6e 2c 20 75 73 69 6e 67 20  pression, using 
43ba7 74 68 65 20 64 61 74 61 20 63 75 72 72 65 6e 74  the data current
43ba8 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ly stored in the
43ba9 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20   position lists 
43baa 0d 0a 2a 2a 20 28 46 74 73 33 45 78 70 72 2d 3e  ..** (Fts3Expr->
43bab 70 50 68 72 61 73 65 2e 64 6f 63 6c 69 73 74 2e  pPhrase.doclist.
43bac 70 4c 69 73 74 2f 6e 4c 69 73 74 29 20 66 6f 72  pList/nList) for
43bad 20 65 61 63 68 20 70 68 72 61 73 65 20 69 6e 20   each phrase in 
43bae 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  the expression. 
43baf 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
43bb0 63 75 72 72 65 6e 74 20 72 6f 77 20 69 73 20 61  current row is a
43bb1 20 6d 61 74 63 68 2c 20 74 68 65 20 70 6f 73 69   match, the posi
43bb2 74 69 6f 6e 20 6c 69 73 74 20 61 73 73 6f 63 69  tion list associ
43bb3 61 74 65 64 20 77 69 74 68 20 65 61 63 68 0d 0a  ated with each..
43bb4 2a 2a 20 70 68 72 61 73 65 20 69 6e 20 74 68 65  ** phrase in the
43bb5 20 4e 45 41 52 20 65 78 70 72 65 73 73 69 6f 6e   NEAR expression
43bb6 20 69 73 20 65 64 69 74 65 64 20 69 6e 20 70 6c   is edited in pl
43bb7 61 63 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6f  ace to contain o
43bb8 6e 6c 79 20 74 68 6f 73 65 0d 0a 2a 2a 20 70 68  nly those..** ph
43bb9 72 61 73 65 20 69 6e 73 74 61 6e 63 65 73 20 73  rase instances s
43bba 75 66 66 69 63 69 65 6e 74 6c 79 20 63 6c 6f 73  ufficiently clos
43bbb 65 20 74 6f 20 74 68 65 69 72 20 70 65 65 72 73  e to their peers
43bbc 20 74 6f 20 73 61 74 69 73 66 79 20 61 6c 6c 20   to satisfy all 
43bbd 4e 45 41 52 0d 0a 2a 2a 20 63 6f 6e 73 74 72 61  NEAR..** constra
43bbe 69 6e 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61  ints. In this ca
43bbf 73 65 20 69 74 20 72 65 74 75 72 6e 73 20 31 2e  se it returns 1.
43bc0 20 49 66 20 74 68 65 20 4e 45 41 52 20 65 78 70   If the NEAR exp
43bc1 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ression does not
43bc2 20 0d 0a 2a 2a 20 6d 61 74 63 68 20 74 68 65 20   ..** match the 
43bc3 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 30 20 69  current row, 0 i
43bc4 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20  s returned. The 
43bc5 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 6d  position lists m
43bc6 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 0d 0a 2a  ay or may not..*
43bc7 2a 20 62 65 20 65 64 69 74 65 64 20 69 66 20 30  * be edited if 0
43bc8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a   is returned...*
43bc9 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
43bca 73 33 45 76 61 6c 4e 65 61 72 54 65 73 74 28 46  s3EvalNearTest(F
43bcb 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
43bcc 69 6e 74 20 2a 70 52 63 29 7b 0d 0a 20 20 69 6e  int *pRc){..  in
43bcd 74 20 72 65 73 20 3d 20 31 3b 0d 0a 0d 0a 20 20  t res = 1;....  
43bce 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
43bcf 20 62 6c 6f 63 6b 20 72 75 6e 73 20 69 66 20 70   block runs if p
43bd0 45 78 70 72 20 69 73 20 74 68 65 20 72 6f 6f 74  Expr is the root
43bd1 20 6f 66 20 61 20 4e 45 41 52 20 71 75 65 72 79   of a NEAR query
43bd2 2e 0d 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d  ...  ** For exam
43bd3 70 6c 65 2c 20 74 68 65 20 71 75 65 72 79 3a 0d  ple, the query:.
43bd4 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 20 20  .  **..  **     
43bd5 20 20 20 20 22 77 22 20 4e 45 41 52 20 22 78 22      "w" NEAR "x"
43bd6 20 4e 45 41 52 20 22 79 22 20 4e 45 41 52 20 22   NEAR "y" NEAR "
43bd7 7a 22 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 77  z"..  **..  ** w
43bd8 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 6e  hich is represen
43bd9 74 65 64 20 69 6e 20 74 72 65 65 20 66 6f 72 6d  ted in tree form
43bda 20 61 73 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a   as:..  **..  **
43bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
43bdd 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  ..  **          
43bde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43bdf 2b 2d 2d 4e 45 41 52 2d 2d 2b 20 20 20 20 20 20  +--NEAR--+      
43be0 3c 2d 2d 20 72 6f 6f 74 20 6f 66 20 4e 45 41 52  <-- root of NEAR
43be1 20 71 75 65 72 79 0d 0a 20 20 2a 2a 20 20 20 20   query..  **    
43be2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43be3 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 7c        |        |
43be4 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  ..  **          
43be5 20 20 20 20 20 20 20 20 20 20 20 2b 2d 2d 4e 45             +--NE
43be6 41 52 2d 2d 2b 20 20 20 22 7a 22 0d 0a 20 20 2a  AR--+   "z"..  *
43be7 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
43be8 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 7c        |        |
43be9 0d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  ..  **          
43bea 20 20 20 20 20 20 2b 2d 2d 4e 45 41 52 2d 2d 2b        +--NEAR--+
43beb 20 20 20 22 79 22 0d 0a 20 20 2a 2a 20 20 20 20     "y"..  **    
43bec 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20              |   
43bed 20 20 20 20 20 7c 0d 0a 20 20 2a 2a 20 20 20 20       |..  **    
43bee 20 20 20 20 20 20 20 20 20 20 20 22 77 22 20 20             "w"  
43bef 20 20 20 20 22 78 22 0d 0a 20 20 2a 2a 0d 0a 20      "x"..  **.. 
43bf0 20 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   ** The right-ha
43bf1 6e 64 20 63 68 69 6c 64 20 6f 66 20 61 20 4e 45  nd child of a NE
43bf2 41 52 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 79  AR node is alway
43bf3 73 20 61 20 70 68 72 61 73 65 2e 20 54 68 65 20  s a phrase. The 
43bf4 0d 0a 20 20 2a 2a 20 6c 65 66 74 2d 68 61 6e 64  ..  ** left-hand
43bf5 20 63 68 69 6c 64 20 6d 61 79 20 62 65 20 65 69   child may be ei
43bf6 74 68 65 72 20 61 20 70 68 72 61 73 65 20 6f 72  ther a phrase or
43bf7 20 61 20 4e 45 41 52 20 6e 6f 64 65 2e 20 54 68   a NEAR node. Th
43bf8 65 72 65 20 61 72 65 0d 0a 20 20 2a 2a 20 6e 6f  ere are..  ** no
43bf9 20 65 78 63 65 70 74 69 6f 6e 73 20 74 6f 20 74   exceptions to t
43bfa 68 69 73 20 2d 20 69 74 27 73 20 74 68 65 20 77  his - it's the w
43bfb 61 79 20 74 68 65 20 70 61 72 73 65 72 20 69 6e  ay the parser in
43bfc 20 66 74 73 33 5f 65 78 70 72 2e 63 20 77 6f 72   fts3_expr.c wor
43bfd 6b 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28  ks...  */..  if(
43bfe 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b   *pRc==SQLITE_OK
43bff 20 0d 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e   ..   && pExpr->
43c00 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
43c01 4e 45 41 52 20 0d 0a 20 20 20 26 26 20 70 45 78  NEAR ..   && pEx
43c02 70 72 2d 3e 62 45 6f 66 3d 3d 30 0d 0a 20 20 20  pr->bEof==0..   
43c03 26 26 20 28 70 45 78 70 72 2d 3e 70 50 61 72 65  && (pExpr->pPare
43c04 6e 74 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d 3e  nt==0 || pExpr->
43c05 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 21 3d  pParent->eType!=
43c06 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0d 0a  FTSQUERY_NEAR)..
43c07 20 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 45 78    ){..    Fts3Ex
43c08 70 72 20 2a 70 3b 20 0d 0a 20 20 20 20 69 6e 74  pr *p; ..    int
43c09 20 6e 54 6d 70 20 3d 20 30 3b 20 20 20 20 20 20   nTmp = 0;      
43c0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
43c0b 74 65 73 20 6f 66 20 74 65 6d 70 20 73 70 61 63  tes of temp spac
43c0c 65 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a  e */..    char *
43c0d 61 54 6d 70 3b 20 20 20 20 20 20 20 20 20 20 20  aTmp;           
43c0e 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
43c0f 73 70 61 63 65 20 66 6f 72 20 50 6f 73 6c 69 73  space for Poslis
43c10 74 4e 65 61 72 4d 65 72 67 65 28 29 20 2a 2f 0d  tNearMerge() */.
43c11 0a 0d 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  ...    /* Alloca
43c12 74 65 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72  te temporary wor
43c13 6b 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0d 0a  king space. */..
43c14 20 20 20 20 66 6f 72 28 70 3d 70 45 78 70 72 3b      for(p=pExpr;
43c15 20 70 2d 3e 70 4c 65 66 74 3b 20 70 3d 70 2d 3e   p->pLeft; p=p->
43c16 70 4c 65 66 74 29 7b 0d 0a 20 20 20 20 20 20 6e  pLeft){..      n
43c17 54 6d 70 20 2b 3d 20 70 2d 3e 70 52 69 67 68 74  Tmp += p->pRight
43c18 2d 3e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69  ->pPhrase->docli
43c19 73 74 2e 6e 4c 69 73 74 3b 0d 0a 20 20 20 20 7d  st.nList;..    }
43c1a 0d 0a 20 20 20 20 6e 54 6d 70 20 2b 3d 20 70 2d  ..    nTmp += p-
43c1b 3e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73  >pPhrase->doclis
43c1c 74 2e 6e 4c 69 73 74 3b 0d 0a 20 20 20 20 61 54  t.nList;..    aT
43c1d 6d 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  mp = sqlite3_mal
43c1e 6c 6f 63 28 6e 54 6d 70 2a 32 29 3b 0d 0a 20 20  loc(nTmp*2);..  
43c1f 20 20 69 66 28 20 21 61 54 6d 70 20 29 7b 0d 0a    if( !aTmp ){..
43c20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 53 51 4c        *pRc = SQL
43c21 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
43c22 20 20 72 65 73 20 3d 20 30 3b 0d 0a 20 20 20 20    res = 0;..    
43c23 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 63 68  }else{..      ch
43c24 61 72 20 2a 61 50 6f 73 6c 69 73 74 20 3d 20 70  ar *aPoslist = p
43c25 2d 3e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69  ->pPhrase->docli
43c26 73 74 2e 70 4c 69 73 74 3b 0d 0a 20 20 20 20 20  st.pList;..     
43c27 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 2d   int nToken = p-
43c28 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
43c29 3b 0d 0a 0d 0a 20 20 20 20 20 20 66 6f 72 28 70  ;....      for(p
43c2a 3d 70 2d 3e 70 50 61 72 65 6e 74 3b 72 65 73 20  =p->pParent;res 
43c2b 26 26 20 70 20 26 26 20 70 2d 3e 65 54 79 70 65  && p && p->eType
43c2c 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3b  ==FTSQUERY_NEAR;
43c2d 20 70 3d 70 2d 3e 70 50 61 72 65 6e 74 29 7b 0d   p=p->pParent){.
43c2e 0a 20 20 20 20 20 20 20 20 46 74 73 33 50 68 72  .        Fts3Phr
43c2f 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20 70  ase *pPhrase = p
43c30 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73  ->pRight->pPhras
43c31 65 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  e;..        int 
43c32 6e 4e 65 61 72 20 3d 20 70 2d 3e 6e 4e 65 61 72  nNear = p->nNear
43c33 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d  ;..        res =
43c34 20 66 74 73 33 45 76 61 6c 4e 65 61 72 54 72 69   fts3EvalNearTri
43c35 6d 28 6e 4e 65 61 72 2c 20 61 54 6d 70 2c 20 26  m(nNear, aTmp, &
43c36 61 50 6f 73 6c 69 73 74 2c 20 26 6e 54 6f 6b 65  aPoslist, &nToke
43c37 6e 2c 20 70 50 68 72 61 73 65 29 3b 0d 0a 20 20  n, pPhrase);..  
43c38 20 20 20 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 20      }..  ..     
43c39 20 61 50 6f 73 6c 69 73 74 20 3d 20 70 45 78 70   aPoslist = pExp
43c3a 72 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61  r->pRight->pPhra
43c3b 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73  se->doclist.pLis
43c3c 74 3b 0d 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e  t;..      nToken
43c3d 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74   = pExpr->pRight
43c3e 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
43c3f 6e 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 70 3d  n;..      for(p=
43c40 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 20 70 20  pExpr->pLeft; p 
43c41 26 26 20 72 65 73 3b 20 70 3d 70 2d 3e 70 4c 65  && res; p=p->pLe
43c42 66 74 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  ft){..        in
43c43 74 20 6e 4e 65 61 72 3b 0d 0a 20 20 20 20 20 20  t nNear;..      
43c44 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50    Fts3Phrase *pP
43c45 68 72 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20  hrase;..        
43c46 61 73 73 65 72 74 28 20 70 2d 3e 70 50 61 72 65  assert( p->pPare
43c47 6e 74 20 26 26 20 70 2d 3e 70 50 61 72 65 6e 74  nt && p->pParent
43c48 2d 3e 70 4c 65 66 74 3d 3d 70 20 29 3b 0d 0a 20  ->pLeft==p );.. 
43c49 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 70         nNear = p
43c4a 2d 3e 70 50 61 72 65 6e 74 2d 3e 6e 4e 65 61 72  ->pParent->nNear
43c4b 3b 0d 0a 20 20 20 20 20 20 20 20 70 50 68 72 61  ;..        pPhra
43c4c 73 65 20 3d 20 28 0d 0a 20 20 20 20 20 20 20 20  se = (..        
43c4d 20 20 20 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54      p->eType==FT
43c4e 53 51 55 45 52 59 5f 4e 45 41 52 20 3f 20 70 2d  SQUERY_NEAR ? p-
43c4f 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65  >pRight->pPhrase
43c50 20 3a 20 70 2d 3e 70 50 68 72 61 73 65 0d 0a 20   : p->pPhrase.. 
43c51 20 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20         );..     
43c52 20 20 20 72 65 73 20 3d 20 66 74 73 33 45 76 61     res = fts3Eva
43c53 6c 4e 65 61 72 54 72 69 6d 28 6e 4e 65 61 72 2c  lNearTrim(nNear,
43c54 20 61 54 6d 70 2c 20 26 61 50 6f 73 6c 69 73 74   aTmp, &aPoslist
43c55 2c 20 26 6e 54 6f 6b 65 6e 2c 20 70 50 68 72 61  , &nToken, pPhra
43c56 73 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  se);..      }.. 
43c57 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71 6c 69     }....    sqli
43c58 74 65 33 5f 66 72 65 65 28 61 54 6d 70 29 3b 0d  te3_free(aTmp);.
43c59 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
43c5a 20 72 65 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a   res;..}..../*..
43c5b 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
43c5c 20 69 73 20 61 20 68 65 6c 70 65 72 20 66 75 6e   is a helper fun
43c5d 63 74 69 6f 6e 20 66 6f 72 20 66 74 73 33 45 76  ction for fts3Ev
43c5e 61 6c 54 65 73 74 44 65 66 65 72 72 65 64 41 6e  alTestDeferredAn
43c5f 64 4e 65 61 72 28 29 2e 0d 0a 2a 2a 20 41 73 73  dNear()...** Ass
43c60 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72 20 6f  uming no error o
43c61 63 63 75 72 73 20 6f 72 20 68 61 73 20 6f 63 63  ccurs or has occ
43c62 75 72 72 65 64 2c 20 49 74 20 72 65 74 75 72 6e  urred, It return
43c63 73 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  s non-zero if th
43c64 65 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  e..** expression
43c65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
43c66 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 6d  econd argument m
43c67 61 74 63 68 65 73 20 74 68 65 20 72 6f 77 20 74  atches the row t
43c68 68 61 74 20 70 43 73 72 20 0d 0a 2a 2a 20 63 75  hat pCsr ..** cu
43c69 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74  rrently points t
43c6a 6f 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 69 74  o, or zero if it
43c6b 20 64 6f 65 73 20 6e 6f 74 2e 0d 0a 2a 2a 0d 0a   does not...**..
43c6c 2a 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e 6f  ** If *pRc is no
43c6d 74 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68 65 6e  t SQLITE_OK when
43c6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
43c6f 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20  s called, it is 
43c70 61 20 6e 6f 2d 6f 70 2e 0d 0a 2a 2a 20 49 66 20  a no-op...** If 
43c71 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
43c72 64 75 72 69 6e 67 20 65 78 65 63 75 74 69 6f 6e  during execution
43c73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
43c74 6e 2c 20 2a 70 52 63 20 69 73 20 73 65 74 20 74  n, *pRc is set t
43c75 6f 20 0d 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f  o ..** the appro
43c76 70 72 69 61 74 65 20 53 51 4c 69 74 65 20 65 72  priate SQLite er
43c77 72 6f 72 20 63 6f 64 65 2e 20 49 6e 20 74 68 69  ror code. In thi
43c78 73 20 63 61 73 65 20 74 68 65 20 72 65 74 75 72  s case the retur
43c79 6e 65 64 20 76 61 6c 75 65 20 69 73 20 0d 0a 2a  ned value is ..*
43c7a 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f  * undefined...*/
43c7b 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
43c7c 33 45 76 61 6c 54 65 73 74 45 78 70 72 28 0d 0a  3EvalTestExpr(..
43c7d 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
43c7e 73 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  sr,             
43c7f 20 20 2f 2a 20 46 54 53 20 63 75 72 73 6f 72 20    /* FTS cursor 
43c80 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46 74 73  handle */..  Fts
43c81 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
43c82 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43c83 45 78 70 72 20 74 6f 20 74 65 73 74 2e 20 4d 61  Expr to test. Ma
43c84 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  y or may not be 
43c85 72 6f 6f 74 2e 20 2a 2f 0d 0a 20 20 69 6e 74 20  root. */..  int 
43c86 2a 70 52 63 20 20 20 20 20 20 20 20 20 20 20 20  *pRc            
43c87 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
43c88 4e 2f 4f 55 54 3a 20 45 72 72 6f 72 20 63 6f 64  N/OUT: Error cod
43c89 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  e */..){..  int 
43c8a 62 48 69 74 20 3d 20 31 3b 20 20 20 20 20 20 20  bHit = 1;       
43c8b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
43c8c 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0d 0a  eturn value */..
43c8d 20 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49    if( *pRc==SQLI
43c8e 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 77  TE_OK ){..    sw
43c8f 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79  itch( pExpr->eTy
43c90 70 65 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 73  pe ){..      cas
43c91 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3a  e FTSQUERY_NEAR:
43c92 0d 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53  ..      case FTS
43c93 51 55 45 52 59 5f 41 4e 44 3a 0d 0a 20 20 20 20  QUERY_AND:..    
43c94 20 20 20 20 62 48 69 74 20 3d 20 28 0d 0a 20 20      bHit = (..  
43c95 20 20 20 20 20 20 20 20 20 20 66 74 73 33 45 76            fts3Ev
43c96 61 6c 54 65 73 74 45 78 70 72 28 70 43 73 72 2c  alTestExpr(pCsr,
43c97 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
43c98 52 63 29 0d 0a 20 20 20 20 20 20 20 20 20 26 26  Rc)..         &&
43c99 20 66 74 73 33 45 76 61 6c 54 65 73 74 45 78 70   fts3EvalTestExp
43c9a 72 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70  r(pCsr, pExpr->p
43c9b 52 69 67 68 74 2c 20 70 52 63 29 0d 0a 20 20 20  Right, pRc)..   
43c9c 20 20 20 20 20 20 26 26 20 66 74 73 33 45 76 61        && fts3Eva
43c9d 6c 4e 65 61 72 54 65 73 74 28 70 45 78 70 72 2c  lNearTest(pExpr,
43c9e 20 70 52 63 29 0d 0a 20 20 20 20 20 20 20 20 29   pRc)..        )
43c9f 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20  ;....        /* 
43ca0 49 66 20 74 68 65 20 4e 45 41 52 20 65 78 70 72  If the NEAR expr
43ca1 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  ession does not 
43ca2 6d 61 74 63 68 20 61 6e 79 20 72 6f 77 73 2c 20  match any rows, 
43ca3 7a 65 72 6f 20 74 68 65 20 64 6f 63 6c 69 73 74  zero the doclist
43ca4 20 66 6f 72 20 0d 0a 20 20 20 20 20 20 20 20 2a   for ..        *
43ca5 2a 20 61 6c 6c 20 70 68 72 61 73 65 73 20 69 6e  * all phrases in
43ca6 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 4e 45  volved in the NE
43ca7 41 52 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  AR. This is beca
43ca8 75 73 65 20 74 68 65 20 73 6e 69 70 70 65 74 28  use the snippet(
43ca9 29 2c 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ),..        ** o
43caa 66 66 73 65 74 73 28 29 20 61 6e 64 20 6d 61 74  ffsets() and mat
43cab 63 68 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f  chinfo() functio
43cac 6e 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f  ns are not suppo
43cad 73 65 64 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65  sed to recognize
43cae 20 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e   ..        ** an
43caf 79 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 70  y instances of p
43cb0 68 72 61 73 65 73 20 74 68 61 74 20 61 72 65 20  hrases that are 
43cb1 70 61 72 74 20 6f 66 20 75 6e 6d 61 74 63 68 65  part of unmatche
43cb2 64 20 4e 45 41 52 20 71 75 65 72 69 65 73 2e 20  d NEAR queries. 
43cb3 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72  ..        ** For
43cb4 20 65 78 61 6d 70 6c 65 20 69 66 20 74 68 69 73   example if this
43cb5 20 65 78 70 72 65 73 73 69 6f 6e 3a 0d 0a 20 20   expression:..  
43cb6 20 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20        **..      
43cb7 20 20 2a 2a 20 20 20 20 2e 2e 2e 20 4d 41 54 43    **    ... MATC
43cb8 48 20 27 61 20 4f 52 20 28 62 20 4e 45 41 52 20  H 'a OR (b NEAR 
43cb9 63 29 27 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d  c)'..        **.
43cba 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6d  .        ** is m
43cbb 61 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 61  atched against a
43cbc 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 3a   row containing:
43cbd 0d 0a 20 20 20 20 20 20 20 20 2a 2a 0d 0a 20 20  ..        **..  
43cbe 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
43cbf 27 61 20 62 20 64 20 65 27 0d 0a 20 20 20 20 20  'a b d e'..     
43cc0 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20 2a     **..        *
43cc1 2a 20 74 68 65 6e 20 61 6e 79 20 73 6e 69 70 70  * then any snipp
43cc2 65 74 28 29 20 73 68 6f 75 6c 64 20 6f 6e 79 20  et() should ony 
43cc3 68 69 67 68 6c 69 67 68 74 20 74 68 65 20 22 61  highlight the "a
43cc4 22 20 74 65 72 6d 2c 20 6e 6f 74 20 74 68 65 20  " term, not the 
43cc5 22 62 22 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  "b"..        ** 
43cc6 28 61 73 20 22 62 22 20 69 73 20 70 61 72 74 20  (as "b" is part 
43cc7 6f 66 20 61 20 6e 6f 6e 2d 6d 61 74 63 68 69 6e  of a non-matchin
43cc8 67 20 4e 45 41 52 20 63 6c 61 75 73 65 29 2e 0d  g NEAR clause)..
43cc9 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  .        */..   
43cca 20 20 20 20 20 69 66 28 20 62 48 69 74 3d 3d 30       if( bHit==0
43ccb 20 0d 0a 20 20 20 20 20 20 20 20 20 26 26 20 70   ..         && p
43ccc 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53  Expr->eType==FTS
43ccd 51 55 45 52 59 5f 4e 45 41 52 20 0d 0a 20 20 20  QUERY_NEAR ..   
43cce 20 20 20 20 20 20 26 26 20 28 70 45 78 70 72 2d        && (pExpr-
43ccf 3e 70 50 61 72 65 6e 74 3d 3d 30 20 7c 7c 20 70  >pParent==0 || p
43cd0 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65  Expr->pParent->e
43cd1 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 4e  Type!=FTSQUERY_N
43cd2 45 41 52 29 0d 0a 20 20 20 20 20 20 20 20 29 7b  EAR)..        ){
43cd3 0d 0a 20 20 20 20 20 20 20 20 20 20 46 74 73 33  ..          Fts3
43cd4 45 78 70 72 20 2a 70 3b 0d 0a 20 20 20 20 20 20  Expr *p;..      
43cd5 20 20 20 20 66 6f 72 28 70 3d 70 45 78 70 72 3b      for(p=pExpr;
43cd6 20 70 2d 3e 70 50 68 72 61 73 65 3d 3d 30 3b 20   p->pPhrase==0; 
43cd7 70 3d 70 2d 3e 70 4c 65 66 74 29 7b 0d 0a 20 20  p=p->pLeft){..  
43cd8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d            if( p-
43cd9 3e 70 52 69 67 68 74 2d 3e 69 44 6f 63 69 64 3d  >pRight->iDocid=
43cda 3d 70 43 73 72 2d 3e 69 50 72 65 76 49 64 20 29  =pCsr->iPrevId )
43cdb 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  {..             
43cdc 20 66 74 73 33 45 76 61 6c 49 6e 76 61 6c 69 64   fts3EvalInvalid
43cdd 61 74 65 50 6f 73 6c 69 73 74 28 70 2d 3e 70 52  atePoslist(p->pR
43cde 69 67 68 74 2d 3e 70 50 68 72 61 73 65 29 3b 0d  ight->pPhrase);.
43cdf 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  .            }..
43ce0 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
43ce1 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 44         if( p->iD
43ce2 6f 63 69 64 3d 3d 70 43 73 72 2d 3e 69 50 72 65  ocid==pCsr->iPre
43ce3 76 49 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  vId ){..        
43ce4 20 20 20 20 66 74 73 33 45 76 61 6c 49 6e 76 61      fts3EvalInva
43ce5 6c 69 64 61 74 65 50 6f 73 6c 69 73 74 28 70 2d  lidatePoslist(p-
43ce6 3e 70 50 68 72 61 73 65 29 3b 0d 0a 20 20 20 20  >pPhrase);..    
43ce7 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
43ce8 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 62 72   }....        br
43ce9 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20 63 61  eak;....      ca
43cea 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a 20  se FTSQUERY_OR: 
43ceb 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 62  {..        int b
43cec 48 69 74 31 20 3d 20 66 74 73 33 45 76 61 6c 54  Hit1 = fts3EvalT
43ced 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
43cee 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 52 63 29  xpr->pLeft, pRc)
43cef 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 62  ;..        int b
43cf0 48 69 74 32 20 3d 20 66 74 73 33 45 76 61 6c 54  Hit2 = fts3EvalT
43cf1 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
43cf2 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 52 63  xpr->pRight, pRc
43cf3 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 48 69 74  );..        bHit
43cf4 20 3d 20 62 48 69 74 31 20 7c 7c 20 62 48 69 74   = bHit1 || bHit
43cf5 32 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  2;..        brea
43cf6 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20  k;..      }.... 
43cf7 20 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45       case FTSQUE
43cf8 52 59 5f 4e 4f 54 3a 0d 0a 20 20 20 20 20 20 20  RY_NOT:..       
43cf9 20 62 48 69 74 20 3d 20 28 0d 0a 20 20 20 20 20   bHit = (..     
43cfa 20 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54         fts3EvalT
43cfb 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
43cfc 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 52 63 29  xpr->pLeft, pRc)
43cfd 0d 0a 20 20 20 20 20 20 20 20 20 26 26 20 21 66  ..         && !f
43cfe 74 73 33 45 76 61 6c 54 65 73 74 45 78 70 72 28  ts3EvalTestExpr(
43cff 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 52 69  pCsr, pExpr->pRi
43d00 67 68 74 2c 20 70 52 63 29 0d 0a 20 20 20 20 20  ght, pRc)..     
43d01 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 62     );..        b
43d02 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20 64  reak;....      d
43d03 65 66 61 75 6c 74 3a 20 7b 0d 0a 20 20 20 20 20  efault: {..     
43d04 20 20 20 69 66 28 20 70 43 73 72 2d 3e 70 44 65     if( pCsr->pDe
43d05 66 65 72 72 65 64 20 0d 0a 20 20 20 20 20 20 20  ferred ..       
43d06 20 20 26 26 20 28 70 45 78 70 72 2d 3e 69 44 6f    && (pExpr->iDo
43d07 63 69 64 3d 3d 70 43 73 72 2d 3e 69 50 72 65 76  cid==pCsr->iPrev
43d08 49 64 20 7c 7c 20 70 45 78 70 72 2d 3e 62 44 65  Id || pExpr->bDe
43d09 66 65 72 72 65 64 29 0d 0a 20 20 20 20 20 20 20  ferred)..       
43d0a 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 46   ){..          F
43d0b 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
43d0c 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72  se = pExpr->pPhr
43d0d 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ase;..          
43d0e 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 62  assert( pExpr->b
43d0f 44 65 66 65 72 72 65 64 20 7c 7c 20 70 50 68 72  Deferred || pPhr
43d10 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 62 46 72  ase->doclist.bFr
43d11 65 65 4c 69 73 74 3d 3d 30 20 29 3b 0d 0a 20 20  eeList==0 );..  
43d12 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70          if( pExp
43d13 72 2d 3e 62 44 65 66 65 72 72 65 64 20 29 7b 0d  r->bDeferred ){.
43d14 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73  .            fts
43d15 33 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50  3EvalInvalidateP
43d16 6f 73 6c 69 73 74 28 70 50 68 72 61 73 65 29 3b  oslist(pPhrase);
43d17 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20  ..          }.. 
43d18 20 20 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20           *pRc = 
43d19 66 74 73 33 45 76 61 6c 44 65 66 65 72 72 65 64  fts3EvalDeferred
43d1a 50 68 72 61 73 65 28 70 43 73 72 2c 20 70 50 68  Phrase(pCsr, pPh
43d1b 72 61 73 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  rase);..        
43d1c 20 20 62 48 69 74 20 3d 20 28 70 50 68 72 61 73    bHit = (pPhras
43d1d 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74  e->doclist.pList
43d1e 21 3d 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  !=0);..         
43d1f 20 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d   pExpr->iDocid =
43d20 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64 3b 0d   pCsr->iPrevId;.
43d21 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  .        }else{.
43d22 0a 20 20 20 20 20 20 20 20 20 20 62 48 69 74 20  .          bHit 
43d23 3d 20 28 70 45 78 70 72 2d 3e 62 45 6f 66 3d 3d  = (pExpr->bEof==
43d24 30 20 26 26 20 70 45 78 70 72 2d 3e 69 44 6f 63  0 && pExpr->iDoc
43d25 69 64 3d 3d 70 43 73 72 2d 3e 69 50 72 65 76 49  id==pCsr->iPrevI
43d26 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  d);..        }..
43d27 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a          break;..
43d28 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
43d29 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 62 48    }..  return bH
43d2a 69 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  it;..}..../*..**
43d2b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
43d2c 73 20 63 61 6c 6c 65 64 20 61 73 20 74 68 65 20  s called as the 
43d2d 73 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 65  second part of e
43d2e 61 63 68 20 78 4e 65 78 74 20 6f 70 65 72 61 74  ach xNext operat
43d2f 69 6f 6e 20 77 68 65 6e 0d 0a 2a 2a 20 69 74 65  ion when..** ite
43d30 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20 74  rating through t
43d31 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 20  he results of a 
43d32 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 2e  full-text query.
43d33 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
43d34 68 65 0d 0a 2a 2a 20 63 75 72 73 6f 72 20 70 6f  he..** cursor po
43d35 69 6e 74 73 20 74 6f 20 61 20 72 6f 77 20 74 68  ints to a row th
43d36 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 71  at matches the q
43d37 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2c  uery expression,
43d38 20 77 69 74 68 20 74 68 65 0d 0a 2a 2a 20 66 6f   with the..** fo
43d39 6c 6c 6f 77 69 6e 67 20 63 61 76 65 61 74 73 3a  llowing caveats:
43d3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 55 70 20  ..**..**   * Up 
43d3b 75 6e 74 69 6c 20 74 68 69 73 20 70 6f 69 6e 74  until this point
43d3c 2c 20 22 4e 45 41 52 22 20 6f 70 65 72 61 74 6f  , "NEAR" operato
43d3d 72 73 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  rs in the expres
43d3e 73 69 6f 6e 20 68 61 76 65 20 62 65 65 6e 0d 0a  sion have been..
43d3f 2a 2a 20 20 20 20 20 74 72 65 61 74 65 64 20 61  **     treated a
43d40 73 20 22 41 4e 44 22 2e 0d 0a 2a 2a 0d 0a 2a 2a  s "AND"...**..**
43d41 20 20 20 2a 20 44 65 66 65 72 72 65 64 20 74 6f     * Deferred to
43d42 6b 65 6e 73 20 68 61 76 65 20 6e 6f 74 20 79 65  kens have not ye
43d43 74 20 62 65 65 6e 20 63 6f 6e 73 69 64 65 72 65  t been considere
43d44 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 2a 70  d...**..** If *p
43d45 52 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45  Rc is not SQLITE
43d46 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75  _OK when this fu
43d47 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
43d48 2c 20 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79  , it immediately
43d49 0d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 30 2e 20  ..** returns 0. 
43d4a 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 74 65  Otherwise, it te
43d4b 73 74 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  sts whether or n
43d4c 6f 74 20 61 66 74 65 72 20 63 6f 6e 73 69 64 65  ot after conside
43d4d 72 69 6e 67 20 4e 45 41 52 0d 0a 2a 2a 20 6f 70  ring NEAR..** op
43d4e 65 72 61 74 6f 72 73 20 61 6e 64 20 64 65 66 65  erators and defe
43d4f 72 72 65 64 20 74 6f 6b 65 6e 73 20 74 68 65 20  rred tokens the 
43d50 63 75 72 72 65 6e 74 20 72 6f 77 20 69 73 20 73  current row is s
43d51 74 69 6c 6c 20 61 20 6d 61 74 63 68 20 66 6f 72  till a match for
43d52 20 74 68 65 0d 0a 2a 2a 20 65 78 70 72 65 73 73   the..** express
43d53 69 6f 6e 2e 20 49 74 20 72 65 74 75 72 6e 73 20  ion. It returns 
43d54 31 20 69 66 20 62 6f 74 68 20 6f 66 20 74 68 65  1 if both of the
43d55 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
43d56 72 75 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31  rue:..**..**   1
43d57 2e 20 2a 70 52 63 20 69 73 20 53 51 4c 49 54 45  . *pRc is SQLITE
43d58 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75  _OK when this fu
43d59 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20  nction returns, 
43d5a 61 6e 64 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 2e  and..**..**   2.
43d5b 20 41 66 74 65 72 20 73 63 61 6e 6e 69 6e 67 20   After scanning 
43d5c 74 68 65 20 63 75 72 72 65 6e 74 20 46 54 53 20  the current FTS 
43d5d 74 61 62 6c 65 20 72 6f 77 20 66 6f 72 20 74 68  table row for th
43d5e 65 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e  e deferred token
43d5f 73 2c 0d 0a 2a 2a 20 20 20 20 20 20 69 74 20 69  s,..**      it i
43d60 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61  s determined tha
43d61 74 20 74 68 65 20 72 6f 77 20 64 6f 65 73 20 2a  t the row does *
43d62 6e 6f 74 2a 20 6d 61 74 63 68 20 74 68 65 20 71  not* match the q
43d63 75 65 72 79 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 72  uery...**..** Or
43d64 2c 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63  , if no error oc
43d65 63 75 72 73 20 61 6e 64 20 69 74 20 73 65 65 6d  curs and it seem
43d66 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  s the current ro
43d67 77 20 64 6f 65 73 20 6d 61 74 63 68 20 74 68 65  w does match the
43d68 20 46 54 53 0d 0a 2a 2a 20 71 75 65 72 79 2c 20   FTS..** query, 
43d69 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f 0d 0a 73  return 0...*/..s
43d6a 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 76  tatic int fts3Ev
43d6b 61 6c 54 65 73 74 44 65 66 65 72 72 65 64 41 6e  alTestDeferredAn
43d6c 64 4e 65 61 72 28 46 74 73 33 43 75 72 73 6f 72  dNear(Fts3Cursor
43d6d 20 2a 70 43 73 72 2c 20 69 6e 74 20 2a 70 52 63   *pCsr, int *pRc
43d6e 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 2a  ){..  int rc = *
43d6f 70 52 63 3b 0d 0a 20 20 69 6e 74 20 62 4d 69 73  pRc;..  int bMis
43d70 73 20 3d 20 30 3b 0d 0a 20 20 69 66 28 20 72 63  s = 0;..  if( rc
43d71 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
43d72 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
43d73 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  e are one or mor
43d74 65 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e  e deferred token
43d75 73 2c 20 6c 6f 61 64 20 74 68 65 20 63 75 72 72  s, load the curr
43d76 65 6e 74 20 72 6f 77 20 69 6e 74 6f 0d 0a 20 20  ent row into..  
43d77 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20    ** memory and 
43d78 73 63 61 6e 20 69 74 20 74 6f 20 64 65 74 65 72  scan it to deter
43d79 6d 69 6e 65 20 74 68 65 20 70 6f 73 69 74 69 6f  mine the positio
43d7a 6e 20 6c 69 73 74 20 66 6f 72 20 65 61 63 68 20  n list for each 
43d7b 64 65 66 65 72 72 65 64 0d 0a 20 20 20 20 2a 2a  deferred..    **
43d7c 20 74 6f 6b 65 6e 2e 20 54 68 65 6e 2c 20 73 65   token. Then, se
43d7d 65 20 69 66 20 74 68 69 73 20 72 6f 77 20 69 73  e if this row is
43d7e 20 72 65 61 6c 6c 79 20 61 20 6d 61 74 63 68 2c   really a match,
43d7f 20 63 6f 6e 73 69 64 65 72 69 6e 67 20 64 65 66   considering def
43d80 65 72 72 65 64 0d 0a 20 20 20 20 2a 2a 20 74 6f  erred..    ** to
43d81 6b 65 6e 73 20 61 6e 64 20 4e 45 41 52 20 6f 70  kens and NEAR op
43d82 65 72 61 74 6f 72 73 20 28 6e 65 69 74 68 65 72  erators (neither
43d83 20 6f 66 20 77 68 69 63 68 20 77 65 72 65 20 74   of which were t
43d84 61 6b 65 6e 20 69 6e 74 6f 20 61 63 63 6f 75 6e  aken into accoun
43d85 74 0d 0a 20 20 20 20 2a 2a 20 65 61 72 6c 69 65  t..    ** earlie
43d86 72 2c 20 62 79 20 66 74 73 33 45 76 61 6c 4e 65  r, by fts3EvalNe
43d87 78 74 52 6f 77 28 29 29 2e 20 0d 0a 20 20 20 20  xtRow()). ..    
43d88 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 43 73 72  */..    if( pCsr
43d89 2d 3e 70 44 65 66 65 72 72 65 64 20 29 7b 0d 0a  ->pDeferred ){..
43d8a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 43        rc = fts3C
43d8b 75 72 73 6f 72 53 65 65 6b 28 30 2c 20 70 43 73  ursorSeek(0, pCs
43d8c 72 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72  r);..      if( r
43d8d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
43d8e 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
43d8f 6c 69 74 65 33 46 74 73 33 43 61 63 68 65 44 65  lite3Fts3CacheDe
43d90 66 65 72 72 65 64 44 6f 63 6c 69 73 74 73 28 70  ferredDoclists(p
43d91 43 73 72 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  Csr);..      }..
43d92 20 20 20 20 7d 0d 0a 20 20 20 20 62 4d 69 73 73      }..    bMiss
43d93 20 3d 20 28 30 3d 3d 66 74 73 33 45 76 61 6c 54   = (0==fts3EvalT
43d94 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 43  estExpr(pCsr, pC
43d95 73 72 2d 3e 70 45 78 70 72 2c 20 26 72 63 29 29  sr->pExpr, &rc))
43d96 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 72 65 65  ;....    /* Free
43d97 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69   the position-li
43d98 73 74 73 20 61 63 63 75 6d 75 6c 61 74 65 64 20  sts accumulated 
43d99 66 6f 72 20 65 61 63 68 20 64 65 66 65 72 72 65  for each deferre
43d9a 64 20 74 6f 6b 65 6e 20 61 62 6f 76 65 2e 20 2a  d token above. *
43d9b 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  /..    sqlite3Ft
43d9c 73 33 46 72 65 65 44 65 66 65 72 72 65 64 44 6f  s3FreeDeferredDo
43d9d 63 6c 69 73 74 73 28 70 43 73 72 29 3b 0d 0a 20  clists(pCsr);.. 
43d9e 20 20 20 2a 70 52 63 20 3d 20 72 63 3b 0d 0a 20     *pRc = rc;.. 
43d9f 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 28 72 63   }..  return (rc
43da0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 62  ==SQLITE_OK && b
43da1 4d 69 73 73 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  Miss);..}..../*.
43da2 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74  .** Advance to t
43da3 68 65 20 6e 65 78 74 20 64 6f 63 75 6d 65 6e 74  he next document
43da4 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
43da5 65 20 46 54 53 20 65 78 70 72 65 73 73 69 6f 6e  e FTS expression
43da6 20 69 6e 0d 0a 2a 2a 20 46 74 73 33 43 75 72 73   in..** Fts3Curs
43da7 6f 72 2e 70 45 78 70 72 2e 0d 0a 2a 2f 0d 0a 73  or.pExpr...*/..s
43da8 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 76  tatic int fts3Ev
43da9 61 6c 4e 65 78 74 28 46 74 73 33 43 75 72 73 6f  alNext(Fts3Curso
43daa 72 20 2a 70 43 73 72 29 7b 0d 0a 20 20 69 6e 74  r *pCsr){..  int
43dab 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
43dac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43dad 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a  Return Code */..
43dae 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
43daf 72 20 3d 20 70 43 73 72 2d 3e 70 45 78 70 72 3b  r = pCsr->pExpr;
43db0 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72  ..  assert( pCsr
43db1 2d 3e 69 73 45 6f 66 3d 3d 30 20 29 3b 0d 0a 20  ->isEof==0 );.. 
43db2 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b   if( pExpr==0 ){
43db3 0d 0a 20 20 20 20 70 43 73 72 2d 3e 69 73 45 6f  ..    pCsr->isEo
43db4 66 20 3d 20 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b  f = 1;..  }else{
43db5 0d 0a 20 20 20 20 64 6f 20 7b 0d 0a 20 20 20 20  ..    do {..    
43db6 20 20 69 66 28 20 70 43 73 72 2d 3e 69 73 52 65    if( pCsr->isRe
43db7 71 75 69 72 65 53 65 65 6b 3d 3d 30 20 29 7b 0d  quireSeek==0 ){.
43db8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
43db9 5f 72 65 73 65 74 28 70 43 73 72 2d 3e 70 53 74  _reset(pCsr->pSt
43dba 6d 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  mt);..      }.. 
43dbb 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
43dbc 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28  ite3_data_count(
43dbd 70 43 73 72 2d 3e 70 53 74 6d 74 29 3d 3d 30 20  pCsr->pStmt)==0 
43dbe 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 45 76  );..      fts3Ev
43dbf 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20  alNextRow(pCsr, 
43dc0 70 45 78 70 72 2c 20 26 72 63 29 3b 0d 0a 20 20  pExpr, &rc);..  
43dc1 20 20 20 20 70 43 73 72 2d 3e 69 73 45 6f 66 20      pCsr->isEof 
43dc2 3d 20 70 45 78 70 72 2d 3e 62 45 6f 66 3b 0d 0a  = pExpr->bEof;..
43dc3 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73 52 65        pCsr->isRe
43dc4 71 75 69 72 65 53 65 65 6b 20 3d 20 31 3b 0d 0a  quireSeek = 1;..
43dc5 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73 4d 61        pCsr->isMa
43dc6 74 63 68 69 6e 66 6f 4e 65 65 64 65 64 20 3d 20  tchinfoNeeded = 
43dc7 31 3b 0d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e  1;..      pCsr->
43dc8 69 50 72 65 76 49 64 20 3d 20 70 45 78 70 72 2d  iPrevId = pExpr-
43dc9 3e 69 44 6f 63 69 64 3b 0d 0a 20 20 20 20 7d 77  >iDocid;..    }w
43dca 68 69 6c 65 28 20 70 43 73 72 2d 3e 69 73 45 6f  hile( pCsr->isEo
43dcb 66 3d 3d 30 20 26 26 20 66 74 73 33 45 76 61 6c  f==0 && fts3Eval
43dcc 54 65 73 74 44 65 66 65 72 72 65 64 41 6e 64 4e  TestDeferredAndN
43dcd 65 61 72 28 70 43 73 72 2c 20 26 72 63 29 20 29  ear(pCsr, &rc) )
43dce 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
43dcf 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
43dd0 2a 20 52 65 73 74 61 72 74 20 69 6e 74 65 72 61  * Restart intera
43dd1 74 69 6f 6e 20 66 6f 72 20 65 78 70 72 65 73 73  tion for express
43dd2 69 6f 6e 20 70 45 78 70 72 20 73 6f 20 74 68 61  ion pExpr so tha
43dd3 74 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  t the next call 
43dd4 74 6f 0d 0a 2a 2a 20 66 74 73 33 45 76 61 6c 4e  to..** fts3EvalN
43dd5 65 78 74 28 29 20 76 69 73 69 74 73 20 74 68 65  ext() visits the
43dd6 20 66 69 72 73 74 20 72 6f 77 2e 20 44 6f 20 6e   first row. Do n
43dd7 6f 74 20 61 6c 6c 6f 77 20 69 6e 63 72 65 6d 65  ot allow increme
43dd8 6e 74 61 6c 20 0d 0a 2a 2a 20 6c 6f 61 64 69 6e  ntal ..** loadin
43dd9 67 20 6f 72 20 6d 65 72 67 69 6e 67 20 6f 66 20  g or merging of 
43dda 70 68 72 61 73 65 20 64 6f 63 6c 69 73 74 73 20  phrase doclists 
43ddb 66 6f 72 20 74 68 69 73 20 69 74 65 72 61 74 69  for this iterati
43ddc 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 2a  on...**..** If *
43ddd 70 52 63 20 69 73 20 6f 74 68 65 72 20 74 68 61  pRc is other tha
43dde 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68 65 6e  n SQLITE_OK when
43ddf 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
43de0 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 0d  s called, it is.
43de1 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 49 66 20  .** a no-op. If 
43de2 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
43de3 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
43de4 74 69 6f 6e 2c 20 2a 70 52 63 20 69 73 20 73 65  tion, *pRc is se
43de5 74 20 74 6f 20 61 6e 0d 0a 2a 2a 20 53 51 4c 69  t to an..** SQLi
43de6 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 62 65  te error code be
43de7 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d  fore returning..
43de8 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
43de9 20 66 74 73 33 45 76 61 6c 52 65 73 74 61 72 74   fts3EvalRestart
43dea 28 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  (..  Fts3Cursor 
43deb 2a 70 43 73 72 2c 0d 0a 20 20 46 74 73 33 45 78  *pCsr,..  Fts3Ex
43dec 70 72 20 2a 70 45 78 70 72 2c 0d 0a 20 20 69 6e  pr *pExpr,..  in
43ded 74 20 2a 70 52 63 0d 0a 29 7b 0d 0a 20 20 69 66  t *pRc..){..  if
43dee 28 20 70 45 78 70 72 20 26 26 20 2a 70 52 63 3d  ( pExpr && *pRc=
43def 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
43df0 20 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70     Fts3Phrase *p
43df1 50 68 72 61 73 65 20 3d 20 70 45 78 70 72 2d 3e  Phrase = pExpr->
43df2 70 50 68 72 61 73 65 3b 0d 0a 0d 0a 20 20 20 20  pPhrase;....    
43df3 69 66 28 20 70 50 68 72 61 73 65 20 29 7b 0d 0a  if( pPhrase ){..
43df4 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 49 6e        fts3EvalIn
43df5 76 61 6c 69 64 61 74 65 50 6f 73 6c 69 73 74 28  validatePoslist(
43df6 70 50 68 72 61 73 65 29 3b 0d 0a 20 20 20 20 20  pPhrase);..     
43df7 20 69 66 28 20 70 50 68 72 61 73 65 2d 3e 62 49   if( pPhrase->bI
43df8 6e 63 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ncr ){..        
43df9 61 73 73 65 72 74 28 20 70 50 68 72 61 73 65 2d  assert( pPhrase-
43dfa 3e 6e 54 6f 6b 65 6e 3d 3d 31 20 29 3b 0d 0a 20  >nToken==1 );.. 
43dfb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
43dfc 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30  Phrase->aToken[0
43dfd 5d 2e 70 53 65 67 63 73 72 20 29 3b 0d 0a 20 20  ].pSegcsr );..  
43dfe 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
43dff 33 4d 73 72 49 6e 63 72 52 65 73 74 61 72 74 28  3MsrIncrRestart(
43e00 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
43e01 30 5d 2e 70 53 65 67 63 73 72 29 3b 0d 0a 20 20  0].pSegcsr);..  
43e02 20 20 20 20 20 20 2a 70 52 63 20 3d 20 66 74 73        *pRc = fts
43e03 33 45 76 61 6c 50 68 72 61 73 65 53 74 61 72 74  3EvalPhraseStart
43e04 28 70 43 73 72 2c 20 30 2c 20 70 50 68 72 61 73  (pCsr, 0, pPhras
43e05 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a  e);..      }....
43e06 20 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64        pPhrase->d
43e07 6f 63 6c 69 73 74 2e 70 4e 65 78 74 44 6f 63 69  oclist.pNextDoci
43e08 64 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 70 50  d = 0;..      pP
43e09 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 69  hrase->doclist.i
43e0a 44 6f 63 69 64 20 3d 20 30 3b 0d 0a 20 20 20 20  Docid = 0;..    
43e0b 7d 0d 0a 0d 0a 20 20 20 20 70 45 78 70 72 2d 3e  }....    pExpr->
43e0c 69 44 6f 63 69 64 20 3d 20 30 3b 0d 0a 20 20 20  iDocid = 0;..   
43e0d 20 70 45 78 70 72 2d 3e 62 45 6f 66 20 3d 20 30   pExpr->bEof = 0
43e0e 3b 0d 0a 20 20 20 20 70 45 78 70 72 2d 3e 62 53  ;..    pExpr->bS
43e0f 74 61 72 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20  tart = 0;....   
43e10 20 66 74 73 33 45 76 61 6c 52 65 73 74 61 72 74   fts3EvalRestart
43e11 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 4c  (pCsr, pExpr->pL
43e12 65 66 74 2c 20 70 52 63 29 3b 0d 0a 20 20 20 20  eft, pRc);..    
43e13 66 74 73 33 45 76 61 6c 52 65 73 74 61 72 74 28  fts3EvalRestart(
43e14 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 52 69  pCsr, pExpr->pRi
43e15 67 68 74 2c 20 70 52 63 29 3b 0d 0a 20 20 7d 0d  ght, pRc);..  }.
43e16 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 66 74  .}..../*..** Aft
43e17 65 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68  er allocating th
43e18 65 20 46 74 73 33 45 78 70 72 2e 61 4d 49 5b 5d  e Fts3Expr.aMI[]
43e19 20 61 72 72 61 79 20 66 6f 72 20 65 61 63 68 20   array for each 
43e1a 70 68 72 61 73 65 20 69 6e 20 74 68 65 20 0d 0a  phrase in the ..
43e1b 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 72 6f  ** expression ro
43e1c 6f 74 65 64 20 61 74 20 70 45 78 70 72 2c 20 74  oted at pExpr, t
43e1d 68 65 20 63 75 72 73 6f 72 20 69 74 65 72 61 74  he cursor iterat
43e1e 65 73 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 72  es through all r
43e1f 6f 77 73 20 6d 61 74 63 68 65 64 0d 0a 2a 2a 20  ows matched..** 
43e20 62 79 20 70 45 78 70 72 2c 20 63 61 6c 6c 69 6e  by pExpr, callin
43e21 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  g this function 
43e22 66 6f 72 20 65 61 63 68 20 72 6f 77 2e 20 54 68  for each row. Th
43e23 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 63 72  is function incr
43e24 65 6d 65 6e 74 73 0d 0a 2a 2a 20 74 68 65 20 76  ements..** the v
43e25 61 6c 75 65 73 20 69 6e 20 46 74 73 33 45 78 70  alues in Fts3Exp
43e26 72 2e 61 4d 49 5b 5d 20 61 63 63 6f 72 64 69 6e  r.aMI[] accordin
43e27 67 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69 6f  g to the positio
43e28 6e 2d 6c 69 73 74 20 63 75 72 72 65 6e 74 6c 79  n-list currently
43e29 0d 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 46 74  ..** found in Ft
43e2a 73 33 45 78 70 72 2e 70 50 68 72 61 73 65 2d 3e  s3Expr.pPhrase->
43e2b 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 20 66 6f  doclist.pList fo
43e2c 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 70 68  r each of the ph
43e2d 72 61 73 65 20 0d 0a 2a 2a 20 65 78 70 72 65 73  rase ..** expres
43e2e 73 69 6f 6e 20 6e 6f 64 65 73 2e 0d 0a 2a 2f 0d  sion nodes...*/.
43e2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
43e30 33 45 76 61 6c 55 70 64 61 74 65 43 6f 75 6e 74  3EvalUpdateCount
43e31 73 28 46 74 73 33 45 78 70 72 20 2a 70 45 78 70  s(Fts3Expr *pExp
43e32 72 29 7b 0d 0a 20 20 69 66 28 20 70 45 78 70 72  r){..  if( pExpr
43e33 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 50 68 72   ){..    Fts3Phr
43e34 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20 70  ase *pPhrase = p
43e35 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0d 0a  Expr->pPhrase;..
43e36 20 20 20 20 69 66 28 20 70 50 68 72 61 73 65 20      if( pPhrase 
43e37 26 26 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c  && pPhrase->docl
43e38 69 73 74 2e 70 4c 69 73 74 20 29 7b 0d 0a 20 20  ist.pList ){..  
43e39 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 30      int iCol = 0
43e3a 3b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70  ;..      char *p
43e3b 20 3d 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c   = pPhrase->docl
43e3c 69 73 74 2e 70 4c 69 73 74 3b 0d 0a 0d 0a 20 20  ist.pList;....  
43e3d 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 20 29      assert( *p )
43e3e 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ;..      while( 
43e3f 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 75 38  1 ){..        u8
43e40 20 63 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20   c = 0;..       
43e41 20 69 6e 74 20 69 43 6e 74 20 3d 20 30 3b 0d 0a   int iCnt = 0;..
43e42 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 30          while( 0
43e43 78 46 45 20 26 20 28 2a 70 20 7c 20 63 29 20 29  xFE & (*p | c) )
43e44 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  {..          if(
43e45 20 28 63 26 30 78 38 30 29 3d 3d 30 20 29 20 69   (c&0x80)==0 ) i
43e46 43 6e 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20  Cnt++;..        
43e47 20 20 63 20 3d 20 2a 70 2b 2b 20 26 20 30 78 38    c = *p++ & 0x8
43e48 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d  0;..        }...
43e49 0a 20 20 20 20 20 20 20 20 2f 2a 20 61 4d 49 5b  .        /* aMI[
43e4a 69 43 6f 6c 2a 33 20 2b 20 31 5d 20 3d 20 4e 75  iCol*3 + 1] = Nu
43e4b 6d 62 65 72 20 6f 66 20 6f 63 63 75 72 72 65 6e  mber of occurren
43e4c 63 65 73 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20  ces..        ** 
43e4d 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20  aMI[iCol*3 + 2] 
43e4e 3d 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  = Number of rows
43e4f 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 74 20 6c   containing at l
43e50 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63  east one instanc
43e51 65 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  e..        */.. 
43e52 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 4d         pExpr->aM
43e53 49 5b 69 43 6f 6c 2a 33 20 2b 20 31 5d 20 2b 3d  I[iCol*3 + 1] +=
43e54 20 69 43 6e 74 3b 0d 0a 20 20 20 20 20 20 20 20   iCnt;..        
43e55 70 45 78 70 72 2d 3e 61 4d 49 5b 69 43 6f 6c 2a  pExpr->aMI[iCol*
43e56 33 20 2b 20 32 5d 20 2b 3d 20 28 69 43 6e 74 3e  3 + 2] += (iCnt>
43e57 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  0);..        if(
43e58 20 2a 70 3d 3d 30 78 30 30 20 29 20 62 72 65 61   *p==0x00 ) brea
43e59 6b 3b 0d 0a 20 20 20 20 20 20 20 20 70 2b 2b 3b  k;..        p++;
43e5a 0d 0a 20 20 20 20 20 20 20 20 70 20 2b 3d 20 73  ..        p += s
43e5b 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72  qlite3Fts3GetVar
43e5c 69 6e 74 33 32 28 70 2c 20 26 69 43 6f 6c 29 3b  int32(p, &iCol);
43e5d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
43e5e 0d 0a 0d 0a 20 20 20 20 66 74 73 33 45 76 61 6c  ....    fts3Eval
43e5f 55 70 64 61 74 65 43 6f 75 6e 74 73 28 70 45 78  UpdateCounts(pEx
43e60 70 72 2d 3e 70 4c 65 66 74 29 3b 0d 0a 20 20 20  pr->pLeft);..   
43e61 20 66 74 73 33 45 76 61 6c 55 70 64 61 74 65 43   fts3EvalUpdateC
43e62 6f 75 6e 74 73 28 70 45 78 70 72 2d 3e 70 52 69  ounts(pExpr->pRi
43e63 67 68 74 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ght);..  }..}...
43e64 0a 2f 2a 0d 0a 2a 2a 20 45 78 70 72 65 73 73 69  ./*..** Expressi
43e65 6f 6e 20 70 45 78 70 72 20 6d 75 73 74 20 62 65  on pExpr must be
43e66 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45 52   of type FTSQUER
43e67 59 5f 50 48 52 41 53 45 2e 0d 0a 2a 2a 0d 0a 2a  Y_PHRASE...**..*
43e68 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61  * If it is not a
43e69 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64  lready allocated
43e6a 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 2c 20   and populated, 
43e6b 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  this function al
43e6c 6c 6f 63 61 74 65 73 20 61 6e 64 0d 0a 2a 2a 20  locates and..** 
43e6d 70 6f 70 75 6c 61 74 65 73 20 74 68 65 20 46 74  populates the Ft
43e6e 73 33 45 78 70 72 2e 61 4d 49 5b 5d 20 61 72 72  s3Expr.aMI[] arr
43e6f 61 79 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ay for expressio
43e70 6e 20 70 45 78 70 72 2e 20 49 66 20 70 45 78 70  n pExpr. If pExp
43e71 72 20 69 73 20 70 61 72 74 0d 0a 2a 2a 20 6f 66  r is part..** of
43e72 20 61 20 4e 45 41 52 20 65 78 70 72 65 73 73 69   a NEAR expressi
43e73 6f 6e 2c 20 74 68 65 6e 20 69 74 20 61 6c 73 6f  on, then it also
43e74 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 64 20 70   allocates and p
43e75 6f 70 75 6c 61 74 65 73 20 74 68 65 20 73 61 6d  opulates the sam
43e76 65 20 61 72 72 61 79 0d 0a 2a 2a 20 66 6f 72 20  e array..** for 
43e77 61 6c 6c 20 6f 74 68 65 72 20 70 68 72 61 73 65  all other phrase
43e78 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20  s that are part 
43e79 6f 66 20 74 68 65 20 4e 45 41 52 20 65 78 70 72  of the NEAR expr
43e7a 65 73 73 69 6f 6e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  ession...**..** 
43e7b 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
43e7c 75 72 6e 65 64 20 69 66 20 74 68 65 20 61 4d 49  urned if the aMI
43e7d 5b 5d 20 61 72 72 61 79 20 69 73 20 73 75 63 63  [] array is succ
43e7e 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74  essfully allocat
43e7f 65 64 20 61 6e 64 0d 0a 2a 2a 20 70 6f 70 75 6c  ed and..** popul
43e80 61 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  ated. Otherwise,
43e81 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
43e82 75 72 73 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  urs, an SQLite e
43e83 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
43e84 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  urned...*/..stat
43e85 69 63 20 69 6e 74 20 66 74 73 33 45 76 61 6c 47  ic int fts3EvalG
43e86 61 74 68 65 72 53 74 61 74 73 28 0d 0a 20 20 46  atherStats(..  F
43e87 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  ts3Cursor *pCsr,
43e88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43e89 2a 20 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20  * Cursor object 
43e8a 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a  */..  Fts3Expr *
43e8b 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20  pExpr           
43e8c 20 20 20 20 20 20 2f 2a 20 46 54 53 51 55 45 52        /* FTSQUER
43e8d 59 5f 50 48 52 41 53 45 20 65 78 70 72 65 73 73  Y_PHRASE express
43e8e 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ion */..){..  in
43e8f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
43e90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
43e91 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d   Return code */.
43e92 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  ...  assert( pEx
43e93 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  pr->eType==FTSQU
43e94 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0d 0a 20  ERY_PHRASE );.. 
43e95 20 69 66 28 20 70 45 78 70 72 2d 3e 61 4d 49 3d   if( pExpr->aMI=
43e96 3d 30 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 54  =0 ){..    Fts3T
43e97 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 46 74  able *pTab = (Ft
43e98 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e  s3Table *)pCsr->
43e99 62 61 73 65 2e 70 56 74 61 62 3b 0d 0a 20 20 20  base.pVtab;..   
43e9a 20 46 74 73 33 45 78 70 72 20 2a 70 52 6f 6f 74   Fts3Expr *pRoot
43e9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
43e9c 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 4e 45 41 52   /* Root of NEAR
43e9d 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a   expression */..
43e9e 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 3b      Fts3Expr *p;
43e9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ea0 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
43ea1 75 73 65 64 20 66 6f 72 20 73 65 76 65 72 61 6c  used for several
43ea2 20 70 75 72 70 6f 73 65 73 20 2a 2f 0d 0a 0d 0a   purposes */....
43ea3 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
43ea4 34 20 69 50 72 65 76 49 64 20 3d 20 70 43 73 72  4 iPrevId = pCsr
43ea5 2d 3e 69 50 72 65 76 49 64 3b 0d 0a 20 20 20 20  ->iPrevId;..    
43ea6 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
43ea7 6f 63 69 64 3b 0d 0a 20 20 20 20 75 38 20 62 45  ocid;..    u8 bE
43ea8 6f 66 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 46 69  of;....    /* Fi
43ea9 6e 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  nd the root of t
43eaa 68 65 20 4e 45 41 52 20 65 78 70 72 65 73 73 69  he NEAR expressi
43eab 6f 6e 20 2a 2f 0d 0a 20 20 20 20 70 52 6f 6f 74  on */..    pRoot
43eac 20 3d 20 70 45 78 70 72 3b 0d 0a 20 20 20 20 77   = pExpr;..    w
43ead 68 69 6c 65 28 20 70 52 6f 6f 74 2d 3e 70 50 61  hile( pRoot->pPa
43eae 72 65 6e 74 20 26 26 20 70 52 6f 6f 74 2d 3e 70  rent && pRoot->p
43eaf 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46  Parent->eType==F
43eb0 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0d  TSQUERY_NEAR ){.
43eb1 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 70  .      pRoot = p
43eb2 52 6f 6f 74 2d 3e 70 50 61 72 65 6e 74 3b 0d 0a  Root->pParent;..
43eb3 20 20 20 20 7d 0d 0a 20 20 20 20 69 44 6f 63 69      }..    iDoci
43eb4 64 20 3d 20 70 52 6f 6f 74 2d 3e 69 44 6f 63 69  d = pRoot->iDoci
43eb5 64 3b 0d 0a 20 20 20 20 62 45 6f 66 20 3d 20 70  d;..    bEof = p
43eb6 52 6f 6f 74 2d 3e 62 45 6f 66 3b 0d 0a 20 20 20  Root->bEof;..   
43eb7 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e   assert( pRoot->
43eb8 62 53 74 61 72 74 20 29 3b 0d 0a 0d 0a 20 20 20  bStart );....   
43eb9 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
43eba 63 65 20 66 6f 72 20 74 68 65 20 61 4d 53 49 5b  ce for the aMSI[
43ebb 5d 20 61 72 72 61 79 20 6f 66 20 65 61 63 68 20  ] array of each 
43ebc 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
43ebd 6e 6f 64 65 20 2a 2f 0d 0a 20 20 20 20 66 6f 72  node */..    for
43ebe 28 70 3d 70 52 6f 6f 74 3b 20 70 3b 20 70 3d 70  (p=pRoot; p; p=p
43ebf 2d 3e 70 4c 65 66 74 29 7b 0d 0a 20 20 20 20 20  ->pLeft){..     
43ec0 20 46 74 73 33 45 78 70 72 20 2a 70 45 20 3d 20   Fts3Expr *pE = 
43ec1 28 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  (p->eType==FTSQU
43ec2 45 52 59 5f 50 48 52 41 53 45 3f 70 3a 70 2d 3e  ERY_PHRASE?p:p->
43ec3 70 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 20 20  pRight);..      
43ec4 61 73 73 65 72 74 28 20 70 45 2d 3e 61 4d 49 3d  assert( pE->aMI=
43ec5 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 70 45 2d  =0 );..      pE-
43ec6 3e 61 4d 49 20 3d 20 28 75 33 32 20 2a 29 73 71  >aMI = (u32 *)sq
43ec7 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 54 61  lite3_malloc(pTa
43ec8 62 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 33 20 2a  b->nColumn * 3 *
43ec9 20 73 69 7a 65 6f 66 28 75 33 32 29 29 3b 0d 0a   sizeof(u32));..
43eca 20 20 20 20 20 20 69 66 28 20 21 70 45 2d 3e 61        if( !pE->a
43ecb 4d 49 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  MI ) return SQLI
43ecc 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20  TE_NOMEM;..     
43ecd 20 6d 65 6d 73 65 74 28 70 45 2d 3e 61 4d 49 2c   memset(pE->aMI,
43ece 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d   0, pTab->nColum
43ecf 6e 20 2a 20 33 20 2a 20 73 69 7a 65 6f 66 28 75  n * 3 * sizeof(u
43ed0 33 32 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  32));..    }....
43ed1 20 20 20 20 66 74 73 33 45 76 61 6c 52 65 73 74      fts3EvalRest
43ed2 61 72 74 28 70 43 73 72 2c 20 70 52 6f 6f 74 2c  art(pCsr, pRoot,
43ed3 20 26 72 63 29 3b 0d 0a 0d 0a 20 20 20 20 77 68   &rc);....    wh
43ed4 69 6c 65 28 20 70 43 73 72 2d 3e 69 73 45 6f 66  ile( pCsr->isEof
43ed5 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
43ed6 45 5f 4f 4b 20 29 7b 0d 0a 0d 0a 20 20 20 20 20  E_OK ){....     
43ed7 20 64 6f 20 7b 0d 0a 20 20 20 20 20 20 20 20 2f   do {..        /
43ed8 2a 20 45 6e 73 75 72 65 20 74 68 65 20 25 5f 63  * Ensure the %_c
43ed9 6f 6e 74 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  ontent statement
43eda 20 69 73 20 72 65 73 65 74 2e 20 2a 2f 0d 0a 20   is reset. */.. 
43edb 20 20 20 20 20 20 20 69 66 28 20 70 43 73 72 2d         if( pCsr-
43edc 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b 3d 3d  >isRequireSeek==
43edd 30 20 29 20 73 71 6c 69 74 65 33 5f 72 65 73 65  0 ) sqlite3_rese
43ede 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0d  t(pCsr->pStmt);.
43edf 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
43ee0 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
43ee1 75 6e 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 29  unt(pCsr->pStmt)
43ee2 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  ==0 );....      
43ee3 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20    /* Advance to 
43ee4 74 68 65 20 6e 65 78 74 20 64 6f 63 75 6d 65 6e  the next documen
43ee5 74 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 66 74  t */..        ft
43ee6 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43  s3EvalNextRow(pC
43ee7 73 72 2c 20 70 52 6f 6f 74 2c 20 26 72 63 29 3b  sr, pRoot, &rc);
43ee8 0d 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e  ..        pCsr->
43ee9 69 73 45 6f 66 20 3d 20 70 52 6f 6f 74 2d 3e 62  isEof = pRoot->b
43eea 45 6f 66 3b 0d 0a 20 20 20 20 20 20 20 20 70 43  Eof;..        pC
43eeb 73 72 2d 3e 69 73 52 65 71 75 69 72 65 53 65 65  sr->isRequireSee
43eec 6b 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20  k = 1;..        
43eed 70 43 73 72 2d 3e 69 73 4d 61 74 63 68 69 6e 66  pCsr->isMatchinf
43eee 6f 4e 65 65 64 65 64 20 3d 20 31 3b 0d 0a 20 20  oNeeded = 1;..  
43eef 20 20 20 20 20 20 70 43 73 72 2d 3e 69 50 72 65        pCsr->iPre
43ef0 76 49 64 20 3d 20 70 52 6f 6f 74 2d 3e 69 44 6f  vId = pRoot->iDo
43ef1 63 69 64 3b 0d 0a 20 20 20 20 20 20 7d 77 68 69  cid;..      }whi
43ef2 6c 65 28 20 70 43 73 72 2d 3e 69 73 45 6f 66 3d  le( pCsr->isEof=
43ef3 3d 30 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20  =0 ..           
43ef4 26 26 20 70 52 6f 6f 74 2d 3e 65 54 79 70 65 3d  && pRoot->eType=
43ef5 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 0d  =FTSQUERY_NEAR .
43ef6 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 66  .           && f
43ef7 74 73 33 45 76 61 6c 54 65 73 74 44 65 66 65 72  ts3EvalTestDefer
43ef8 72 65 64 41 6e 64 4e 65 61 72 28 70 43 73 72 2c  redAndNear(pCsr,
43ef9 20 26 72 63 29 20 0d 0a 20 20 20 20 20 20 29 3b   &rc) ..      );
43efa 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63  ....      if( rc
43efb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
43efc 43 73 72 2d 3e 69 73 45 6f 66 3d 3d 30 20 29 7b  Csr->isEof==0 ){
43efd 0d 0a 20 20 20 20 20 20 20 20 66 74 73 33 45 76  ..        fts3Ev
43efe 61 6c 55 70 64 61 74 65 43 6f 75 6e 74 73 28 70  alUpdateCounts(p
43eff 52 6f 6f 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  Root);..      }.
43f00 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 70 43  .    }....    pC
43f01 73 72 2d 3e 69 73 45 6f 66 20 3d 20 30 3b 0d 0a  sr->isEof = 0;..
43f02 20 20 20 20 70 43 73 72 2d 3e 69 50 72 65 76 49      pCsr->iPrevI
43f03 64 20 3d 20 69 50 72 65 76 49 64 3b 0d 0a 0d 0a  d = iPrevId;....
43f04 20 20 20 20 69 66 28 20 62 45 6f 66 20 29 7b 0d      if( bEof ){.
43f05 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 62 45  .      pRoot->bE
43f06 6f 66 20 3d 20 62 45 6f 66 3b 0d 0a 20 20 20 20  of = bEof;..    
43f07 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a  }else{..      /*
43f08 20 43 61 75 74 69 6f 6e 3a 20 70 52 6f 6f 74 20   Caution: pRoot 
43f09 6d 61 79 20 69 74 65 72 61 74 65 20 74 68 72 6f  may iterate thro
43f0a 75 67 68 20 64 6f 63 69 64 73 20 69 6e 20 61 73  ugh docids in as
43f0b 63 65 6e 64 69 6e 67 20 6f 72 20 64 65 73 63 65  cending or desce
43f0c 6e 64 69 6e 67 0d 0a 20 20 20 20 20 20 2a 2a 20  nding..      ** 
43f0d 6f 72 64 65 72 2e 20 46 6f 72 20 74 68 69 73 20  order. For this 
43f0e 72 65 61 73 6f 6e 2c 20 65 76 65 6e 20 74 68 6f  reason, even tho
43f0f 75 67 68 20 69 74 20 73 65 65 6d 73 20 6d 6f 72  ugh it seems mor
43f10 65 20 64 65 66 65 6e 73 69 76 65 2c 20 74 68 65  e defensive, the
43f11 20 0d 0a 20 20 20 20 20 20 2a 2a 20 64 6f 20 6c   ..      ** do l
43f12 6f 6f 70 20 63 61 6e 20 6e 6f 74 20 62 65 20 77  oop can not be w
43f13 72 69 74 74 65 6e 3a 0d 0a 20 20 20 20 20 20 2a  ritten:..      *
43f14 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 20 20 64 6f  *..      **   do
43f15 20 7b 2e 2e 2e 7d 20 77 68 69 6c 65 28 20 70 52   {...} while( pR
43f16 6f 6f 74 2d 3e 69 44 6f 63 69 64 3c 69 44 6f 63  oot->iDocid<iDoc
43f17 69 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  id && rc==SQLITE
43f18 5f 4f 4b 20 29 3b 0d 0a 20 20 20 20 20 20 2a 2f  _OK );..      */
43f19 0d 0a 20 20 20 20 20 20 66 74 73 33 45 76 61 6c  ..      fts3Eval
43f1a 52 65 73 74 61 72 74 28 70 43 73 72 2c 20 70 52  Restart(pCsr, pR
43f1b 6f 6f 74 2c 20 26 72 63 29 3b 0d 0a 20 20 20 20  oot, &rc);..    
43f1c 20 20 64 6f 20 7b 0d 0a 20 20 20 20 20 20 20 20    do {..        
43f1d 66 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28  fts3EvalNextRow(
43f1e 70 43 73 72 2c 20 70 52 6f 6f 74 2c 20 26 72 63  pCsr, pRoot, &rc
43f1f 29 3b 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65  );..        asse
43f20 72 74 28 20 70 52 6f 6f 74 2d 3e 62 45 6f 66 3d  rt( pRoot->bEof=
43f21 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 7d 77 68  =0 );..      }wh
43f22 69 6c 65 28 20 70 52 6f 6f 74 2d 3e 69 44 6f 63  ile( pRoot->iDoc
43f23 69 64 21 3d 69 44 6f 63 69 64 20 26 26 20 72 63  id!=iDocid && rc
43f24 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a  ==SQLITE_OK );..
43f25 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54 65        fts3EvalTe
43f26 73 74 44 65 66 65 72 72 65 64 41 6e 64 4e 65 61  stDeferredAndNea
43f27 72 28 70 43 73 72 2c 20 26 72 63 29 3b 0d 0a 20  r(pCsr, &rc);.. 
43f28 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
43f29 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
43f2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
43f2b 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 68  on is used by th
43f2c 65 20 6d 61 74 63 68 69 6e 66 6f 28 29 20 6d 6f  e matchinfo() mo
43f2d 64 75 6c 65 20 74 6f 20 71 75 65 72 79 20 61 20  dule to query a 
43f2e 70 68 72 61 73 65 20 0d 0a 2a 2a 20 65 78 70 72  phrase ..** expr
43f2f 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
43f30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
43f31 66 6f 72 6d 61 74 69 6f 6e 3a 0d 0a 2a 2a 0d 0a  formation:..**..
43f32 2a 2a 20 20 20 31 2e 20 54 68 65 20 74 6f 74 61  **   1. The tota
43f33 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 63 63 75  l number of occu
43f34 72 72 65 6e 63 65 73 20 6f 66 20 74 68 65 20 70  rrences of the p
43f35 68 72 61 73 65 20 69 6e 20 65 61 63 68 20 63 6f  hrase in each co
43f36 6c 75 6d 6e 20 6f 66 20 0d 0a 2a 2a 20 20 20 20  lumn of ..**    
43f37 20 20 74 68 65 20 46 54 53 20 74 61 62 6c 65 20    the FTS table 
43f38 28 63 6f 6e 73 69 64 65 72 69 6e 67 20 61 6c 6c  (considering all
43f39 20 72 6f 77 73 29 2c 20 61 6e 64 0d 0a 2a 2a 0d   rows), and..**.
43f3a 0a 2a 2a 20 20 20 32 2e 20 46 6f 72 20 65 61 63  .**   2. For eac
43f3b 68 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6e 75  h column, the nu
43f3c 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
43f3d 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 77 68  the table for wh
43f3e 69 63 68 20 74 68 65 0d 0a 2a 2a 20 20 20 20 20  ich the..**     
43f3f 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73   column contains
43f40 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 69 6e   at least one in
43f41 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 70 68  stance of the ph
43f42 72 61 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  rase...**..** If
43f43 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   no error occurs
43f44 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
43f45 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20  eturned and the 
43f46 76 61 6c 75 65 73 20 66 6f 72 20 65 61 63 68 20  values for each 
43f47 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 77 72 69 74 74  column..** writt
43f48 65 6e 20 69 6e 74 6f 20 74 68 65 20 61 72 72 61  en into the arra
43f49 79 20 61 69 4f 75 74 20 61 73 20 66 6f 6c 6c 6f  y aiOut as follo
43f4a 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 61 69  ws:..**..**   ai
43f4b 4f 75 74 5b 69 43 6f 6c 2a 33 20 2b 20 31 5d 20  Out[iCol*3 + 1] 
43f4c 3d 20 4e 75 6d 62 65 72 20 6f 66 20 6f 63 63 75  = Number of occu
43f4d 72 72 65 6e 63 65 73 0d 0a 2a 2a 20 20 20 61 69  rrences..**   ai
43f4e 4f 75 74 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20  Out[iCol*3 + 2] 
43f4f 3d 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  = Number of rows
43f50 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 74 20 6c   containing at l
43f51 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63  east one instanc
43f52 65 0d 0a 2a 2a 0d 0a 2a 2a 20 43 61 76 65 61 74  e..**..** Caveat
43f53 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 49  s:..**..**   * I
43f54 66 20 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69  f a phrase consi
43f55 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20  sts entirely of 
43f56 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 2c  deferred tokens,
43f57 20 74 68 65 6e 20 61 6c 6c 20 6f 75 74 70 75 74   then all output
43f58 20 0d 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 73   ..**     values
43f59 20 61 72 65 20 73 65 74 20 74 6f 20 74 68 65 20   are set to the 
43f5a 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63 75 6d 65  number of docume
43f5b 6e 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  nts in the table
43f5c 2e 20 49 6e 20 6f 74 68 65 72 0d 0a 2a 2a 20 20  . In other..**  
43f5d 20 20 20 77 6f 72 64 73 20 77 65 20 61 73 73 75     words we assu
43f5e 6d 65 20 74 68 61 74 20 76 65 72 79 20 63 6f 6d  me that very com
43f5f 6d 6f 6e 20 74 6f 6b 65 6e 73 20 6f 63 63 75 72  mon tokens occur
43f60 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 69 6e   exactly once in
43f61 20 65 61 63 68 20 0d 0a 2a 2a 20 20 20 20 20 63   each ..**     c
43f62 6f 6c 75 6d 6e 20 6f 66 20 65 61 63 68 20 72 6f  olumn of each ro
43f63 77 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0d  w of the table..
43f64 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 49 66 20 61  .**..**   * If a
43f65 20 70 68 72 61 73 65 20 63 6f 6e 74 61 69 6e 73   phrase contains
43f66 20 73 6f 6d 65 20 64 65 66 65 72 72 65 64 20 74   some deferred t
43f67 6f 6b 65 6e 73 20 28 61 6e 64 20 73 6f 6d 65 20  okens (and some 
43f68 6e 6f 6e 2d 64 65 66 65 72 72 65 64 20 0d 0a 2a  non-deferred ..*
43f69 2a 20 20 20 20 20 74 6f 6b 65 6e 73 29 2c 20 63  *     tokens), c
43f6a 6f 75 6e 74 20 74 68 65 20 70 6f 74 65 6e 74 69  ount the potenti
43f6b 61 6c 20 6f 63 63 75 72 72 65 6e 63 65 20 69 64  al occurrence id
43f6c 65 6e 74 69 66 69 65 64 20 62 79 20 63 6f 6e 73  entified by cons
43f6d 69 64 65 72 69 6e 67 0d 0a 2a 2a 20 20 20 20 20  idering..**     
43f6e 74 68 65 20 6e 6f 6e 2d 64 65 66 65 72 72 65 64  the non-deferred
43f6f 20 74 6f 6b 65 6e 73 20 69 6e 73 74 65 61 64 20   tokens instead 
43f70 6f 66 20 61 63 74 75 61 6c 20 70 68 72 61 73 65  of actual phrase
43f71 20 6f 63 63 75 72 72 65 6e 63 65 73 2e 0d 0a 2a   occurrences...*
43f72 2a 0d 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  *..**   * If the
43f73 20 70 68 72 61 73 65 20 69 73 20 70 61 72 74 20   phrase is part 
43f74 6f 66 20 61 20 4e 45 41 52 20 65 78 70 72 65 73  of a NEAR expres
43f75 73 69 6f 6e 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  sion, then only 
43f76 70 68 72 61 73 65 20 69 6e 73 74 61 6e 63 65 73  phrase instances
43f77 0d 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 6d 65  ..**     that me
43f78 65 74 20 74 68 65 20 4e 45 41 52 20 63 6f 6e 73  et the NEAR cons
43f79 74 72 61 69 6e 74 20 61 72 65 20 69 6e 63 6c 75  traint are inclu
43f7a 64 65 64 20 69 6e 20 74 68 65 20 63 6f 75 6e 74  ded in the count
43f7b 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50  s...*/..SQLITE_P
43f7c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
43f7d 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65  e3Fts3EvalPhrase
43f7e 53 74 61 74 73 28 0d 0a 20 20 46 74 73 33 43 75  Stats(..  Fts3Cu
43f7f 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
43f80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
43f81 20 63 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a   cursor handle *
43f82 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  /..  Fts3Expr *p
43f83 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
43f84 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 65       /* Phrase e
43f85 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20  xpression */..  
43f86 75 33 32 20 2a 61 69 4f 75 74 20 20 20 20 20 20  u32 *aiOut      
43f87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f88 2f 2a 20 41 72 72 61 79 20 74 6f 20 77 72 69 74  /* Array to writ
43f89 65 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 28  e results into (
43f8a 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0d 0a 29  see above) */..)
43f8b 7b 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  {..  Fts3Table *
43f8c 70 54 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c  pTab = (Fts3Tabl
43f8d 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70  e *)pCsr->base.p
43f8e 56 74 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63 20  Vtab;..  int rc 
43f8f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  = SQLITE_OK;..  
43f90 69 6e 74 20 69 43 6f 6c 3b 0d 0a 0d 0a 20 20 69  int iCol;....  i
43f91 66 28 20 70 45 78 70 72 2d 3e 62 44 65 66 65 72  f( pExpr->bDefer
43f92 72 65 64 20 26 26 20 70 45 78 70 72 2d 3e 70 50  red && pExpr->pP
43f93 61 72 65 6e 74 2d 3e 65 54 79 70 65 21 3d 46 54  arent->eType!=FT
43f94 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0d 0a  SQUERY_NEAR ){..
43f95 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
43f96 2d 3e 6e 44 6f 63 3e 30 20 29 3b 0d 0a 20 20 20  ->nDoc>0 );..   
43f97 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
43f98 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b  l<pTab->nColumn;
43f99 20 69 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20   iCol++){..     
43f9a 20 61 69 4f 75 74 5b 69 43 6f 6c 2a 33 20 2b 20   aiOut[iCol*3 + 
43f9b 31 5d 20 3d 20 28 75 33 32 29 70 43 73 72 2d 3e  1] = (u32)pCsr->
43f9c 6e 44 6f 63 3b 0d 0a 20 20 20 20 20 20 61 69 4f  nDoc;..      aiO
43f9d 75 74 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20 3d  ut[iCol*3 + 2] =
43f9e 20 28 75 33 32 29 70 43 73 72 2d 3e 6e 44 6f 63   (u32)pCsr->nDoc
43f9f 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
43fa0 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  e{..    rc = fts
43fa1 33 45 76 61 6c 47 61 74 68 65 72 53 74 61 74 73  3EvalGatherStats
43fa2 28 70 43 73 72 2c 20 70 45 78 70 72 29 3b 0d 0a  (pCsr, pExpr);..
43fa3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
43fa4 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
43fa5 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61  assert( pExpr->a
43fa6 4d 49 20 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72  MI );..      for
43fa7 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54  (iCol=0; iCol<pT
43fa8 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 43 6f  ab->nColumn; iCo
43fa9 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 61  l++){..        a
43faa 69 4f 75 74 5b 69 43 6f 6c 2a 33 20 2b 20 31 5d  iOut[iCol*3 + 1]
43fab 20 3d 20 70 45 78 70 72 2d 3e 61 4d 49 5b 69 43   = pExpr->aMI[iC
43fac 6f 6c 2a 33 20 2b 20 31 5d 3b 0d 0a 20 20 20 20  ol*3 + 1];..    
43fad 20 20 20 20 61 69 4f 75 74 5b 69 43 6f 6c 2a 33      aiOut[iCol*3
43fae 20 2b 20 32 5d 20 3d 20 70 45 78 70 72 2d 3e 61   + 2] = pExpr->a
43faf 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 3b 0d  MI[iCol*3 + 2];.
43fb0 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
43fb1 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  .  }....  return
43fb2 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
43fb3 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
43fb4 20 70 45 78 70 72 20 70 61 73 73 65 64 20 61 73   pExpr passed as
43fb5 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
43fb6 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
43fb7 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75 73 74 20 62  ction..** must b
43fb8 65 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45  e of type FTSQUE
43fb9 52 59 5f 50 48 52 41 53 45 2e 20 0d 0a 2a 2a 0d  RY_PHRASE. ..**.
43fba 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
43fbb 20 76 61 6c 75 65 20 69 73 20 65 69 74 68 65 72   value is either
43fbc 20 4e 55 4c 4c 20 6f 72 20 61 20 70 6f 69 6e 74   NULL or a point
43fbd 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63  er to a buffer c
43fbe 6f 6e 74 61 69 6e 69 6e 67 0d 0a 2a 2a 20 61 20  ontaining..** a 
43fbf 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 69 6e  position-list in
43fc0 64 69 63 61 74 69 6e 67 20 74 68 65 20 6f 63 63  dicating the occ
43fc1 75 72 72 65 6e 63 65 73 20 6f 66 20 74 68 65 20  urrences of the 
43fc2 70 68 72 61 73 65 20 69 6e 20 63 6f 6c 75 6d 6e  phrase in column
43fc3 20 69 43 6f 6c 0d 0a 2a 2a 20 6f 66 20 74 68 65   iCol..** of the
43fc4 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 0d 0a   current row. ..
43fc5 2a 2a 0d 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63  **..** More spec
43fc6 69 66 69 63 61 6c 6c 79 2c 20 74 68 65 20 72 65  ifically, the re
43fc7 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 63 6f  turned buffer co
43fc8 6e 74 61 69 6e 73 20 31 20 76 61 72 69 6e 74 20  ntains 1 varint 
43fc9 66 6f 72 20 65 61 63 68 20 0d 0a 2a 2a 20 6f 63  for each ..** oc
43fca 63 75 72 65 6e 63 65 20 6f 66 20 74 68 65 20 70  curence of the p
43fcb 68 72 61 73 65 20 69 6e 20 74 68 65 20 63 6f 6c  hrase in the col
43fcc 75 6d 6e 2c 20 73 74 6f 72 65 64 20 75 73 69 6e  umn, stored usin
43fcd 67 20 74 68 65 20 6e 6f 72 6d 61 6c 20 28 64 65  g the normal (de
43fce 6c 74 61 2b 32 29 20 0d 0a 2a 2a 20 63 6f 6d 70  lta+2) ..** comp
43fcf 72 65 73 73 69 6f 6e 20 61 6e 64 20 69 73 20 74  ression and is t
43fd0 65 72 6d 69 6e 61 74 65 64 20 62 79 20 65 69 74  erminated by eit
43fd1 68 65 72 20 61 6e 20 30 78 30 31 20 6f 72 20 30  her an 0x01 or 0
43fd2 78 30 30 20 62 79 74 65 2e 20 46 6f 72 20 65 78  x00 byte. For ex
43fd3 61 6d 70 6c 65 2c 0d 0a 2a 2a 20 69 66 20 74 68  ample,..** if th
43fd4 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 75  e requested colu
43fd5 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 22 61 20 62  mn contains "a b
43fd6 20 58 20 63 20 64 20 58 20 58 22 20 61 6e 64 20   X c d X X" and 
43fd7 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73  the position-lis
43fd8 74 0d 0a 2a 2a 20 66 6f 72 20 27 58 27 20 69 73  t..** for 'X' is
43fd9 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 20   requested, the 
43fda 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 20  buffer returned 
43fdb 6d 61 79 20 63 6f 6e 74 61 69 6e 3a 0d 0a 2a 2a  may contain:..**
43fdc 0d 0a 2a 2a 20 20 20 20 20 30 78 30 34 20 30 78  ..**     0x04 0x
43fdd 30 35 20 30 78 30 33 20 30 78 30 31 20 20 20 6f  05 0x03 0x01   o
43fde 72 20 20 20 30 78 30 34 20 30 78 30 35 20 30 78  r   0x04 0x05 0x
43fdf 30 33 20 30 78 30 30 0d 0a 2a 2a 0d 0a 2a 2a 20  03 0x00..**..** 
43fe0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f  This function wo
43fe1 72 6b 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f  rks regardless o
43fe2 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
43fe3 20 74 68 65 20 70 68 72 61 73 65 20 69 73 20 64   the phrase is d
43fe4 65 66 65 72 72 65 64 2c 0d 0a 2a 2a 20 69 6e 63  eferred,..** inc
43fe5 72 65 6d 65 6e 74 61 6c 2c 20 6f 72 20 6e 65 69  remental, or nei
43fe6 74 68 65 72 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ther...*/..SQLIT
43fe7 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
43fe8 73 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50  sqlite3Fts3EvalP
43fe9 68 72 61 73 65 50 6f 73 6c 69 73 74 28 0d 0a 20  hrasePoslist(.. 
43fea 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
43feb 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
43fec 20 2f 2a 20 46 54 53 33 20 63 75 72 73 6f 72 20   /* FTS3 cursor 
43fed 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 46 74 73  object */..  Fts
43fee 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
43fef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43ff0 50 68 72 61 73 65 20 74 6f 20 72 65 74 75 72 6e  Phrase to return
43ff1 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 2a 2f 0d   doclist for */.
43ff2 0a 20 20 69 6e 74 20 69 43 6f 6c 20 20 20 20 20  .  int iCol     
43ff3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ff4 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20     /* Column to 
43ff5 72 65 74 75 72 6e 20 70 6f 73 69 74 69 6f 6e 20  return position 
43ff6 6c 69 73 74 20 66 6f 72 20 2a 2f 0d 0a 29 7b 0d  list for */..){.
43ff7 0a 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70  .  Fts3Phrase *p
43ff8 50 68 72 61 73 65 20 3d 20 70 45 78 70 72 2d 3e  Phrase = pExpr->
43ff9 70 50 68 72 61 73 65 3b 0d 0a 20 20 46 74 73 33  pPhrase;..  Fts3
43ffa 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 46  Table *pTab = (F
43ffb 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d  ts3Table *)pCsr-
43ffc 3e 62 61 73 65 2e 70 56 74 61 62 3b 0d 0a 20 20  >base.pVtab;..  
43ffd 63 68 61 72 20 2a 70 49 74 65 72 20 3d 20 70 50  char *pIter = pP
43ffe 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70  hrase->doclist.p
43fff 4c 69 73 74 3b 0d 0a 20 20 69 6e 74 20 69 54 68  List;..  int iTh
44000 69 73 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28  is;....  assert(
44001 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c   iCol>=0 && iCol
44002 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  <pTab->nColumn )
44003 3b 0d 0a 20 20 69 66 28 20 21 70 49 74 65 72 20  ;..  if( !pIter 
44004 0d 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 62  ..   || pExpr->b
44005 45 6f 66 20 0d 0a 20 20 20 7c 7c 20 70 45 78 70  Eof ..   || pExp
44006 72 2d 3e 69 44 6f 63 69 64 21 3d 70 43 73 72 2d  r->iDocid!=pCsr-
44007 3e 69 50 72 65 76 49 64 0d 0a 20 20 20 7c 7c 20  >iPrevId..   || 
44008 28 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  (pPhrase->iColum
44009 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 20  n<pTab->nColumn 
4400a 26 26 20 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  && pPhrase->iCol
4400b 75 6d 6e 21 3d 69 43 6f 6c 29 20 0d 0a 20 20 29  umn!=iCol) ..  )
4400c 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {..    return 0;
4400d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72  ..  }....  asser
4400e 74 28 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c  t( pPhrase->docl
4400f 69 73 74 2e 6e 4c 69 73 74 3e 30 20 29 3b 0d 0a  ist.nList>0 );..
44010 20 20 69 66 28 20 2a 70 49 74 65 72 3d 3d 30 78    if( *pIter==0x
44011 30 31 20 29 7b 0d 0a 20 20 20 20 70 49 74 65 72  01 ){..    pIter
44012 2b 2b 3b 0d 0a 20 20 20 20 70 49 74 65 72 20 2b  ++;..    pIter +
44013 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
44014 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20  Varint32(pIter, 
44015 26 69 54 68 69 73 29 3b 0d 0a 20 20 7d 65 6c 73  &iThis);..  }els
44016 65 7b 0d 0a 20 20 20 20 69 54 68 69 73 20 3d 20  e{..    iThis = 
44017 30 3b 0d 0a 20 20 7d 0d 0a 20 20 77 68 69 6c 65  0;..  }..  while
44018 28 20 69 54 68 69 73 3c 69 43 6f 6c 20 29 7b 0d  ( iThis<iCol ){.
44019 0a 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c  .    fts3Columnl
4401a 69 73 74 43 6f 70 79 28 30 2c 20 26 70 49 74 65  istCopy(0, &pIte
4401b 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 2a 70 49  r);..    if( *pI
4401c 74 65 72 3d 3d 30 78 30 30 20 29 20 72 65 74 75  ter==0x00 ) retu
4401d 72 6e 20 30 3b 0d 0a 20 20 20 20 70 49 74 65 72  rn 0;..    pIter
4401e 2b 2b 3b 0d 0a 20 20 20 20 70 49 74 65 72 20 2b  ++;..    pIter +
4401f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
44020 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20  Varint32(pIter, 
44021 26 69 54 68 69 73 29 3b 0d 0a 20 20 7d 0d 0a 0d  &iThis);..  }...
44022 0a 20 20 72 65 74 75 72 6e 20 28 28 69 43 6f 6c  .  return ((iCol
44023 3d 3d 69 54 68 69 73 29 3f 70 49 74 65 72 3a 30  ==iThis)?pIter:0
44024 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
44025 46 72 65 65 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65  Free all compone
44026 6e 74 73 20 6f 66 20 74 68 65 20 46 74 73 33 50  nts of the Fts3P
44027 68 72 61 73 65 20 73 74 72 75 63 74 75 72 65 20  hrase structure 
44028 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63 61  that were alloca
44029 74 65 64 20 62 79 0d 0a 2a 2a 20 74 68 65 20 65  ted by..** the e
4402a 76 61 6c 20 6d 6f 64 75 6c 65 2e 20 53 70 65 63  val module. Spec
4402b 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 6d  ifically, this m
4402c 65 61 6e 73 20 74 6f 20 66 72 65 65 3a 0d 0a 2a  eans to free:..*
4402d 2a 0d 0a 2a 2a 20 20 20 2a 20 74 68 65 20 63 6f  *..**   * the co
4402e 6e 74 65 6e 74 73 20 6f 66 20 70 50 68 72 61 73  ntents of pPhras
4402f 65 2d 3e 64 6f 63 6c 69 73 74 2c 20 61 6e 64 0d  e->doclist, and.
44030 0a 2a 2a 20 20 20 2a 20 61 6e 79 20 46 74 73 33  .**   * any Fts3
44031 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 6f  MultiSegReader o
44032 62 6a 65 63 74 73 20 68 65 6c 64 20 62 79 20 70  bjects held by p
44033 68 72 61 73 65 20 74 6f 6b 65 6e 73 2e 0d 0a 2a  hrase tokens...*
44034 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
44035 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
44036 73 33 45 76 61 6c 50 68 72 61 73 65 43 6c 65 61  s3EvalPhraseClea
44037 6e 75 70 28 46 74 73 33 50 68 72 61 73 65 20 2a  nup(Fts3Phrase *
44038 70 50 68 72 61 73 65 29 7b 0d 0a 20 20 69 66 28  pPhrase){..  if(
44039 20 70 50 68 72 61 73 65 20 29 7b 0d 0a 20 20 20   pPhrase ){..   
4403a 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 73 71 6c   int i;..    sql
4403b 69 74 65 33 5f 66 72 65 65 28 70 50 68 72 61 73  ite3_free(pPhras
4403c 65 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 29  e->doclist.aAll)
4403d 3b 0d 0a 20 20 20 20 66 74 73 33 45 76 61 6c 49  ;..    fts3EvalI
4403e 6e 76 61 6c 69 64 61 74 65 50 6f 73 6c 69 73 74  nvalidatePoslist
4403f 28 70 50 68 72 61 73 65 29 3b 0d 0a 20 20 20 20  (pPhrase);..    
44040 6d 65 6d 73 65 74 28 26 70 50 68 72 61 73 65 2d  memset(&pPhrase-
44041 3e 64 6f 63 6c 69 73 74 2c 20 30 2c 20 73 69 7a  >doclist, 0, siz
44042 65 6f 66 28 46 74 73 33 44 6f 63 6c 69 73 74 29  eof(Fts3Doclist)
44043 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  );..    for(i=0;
44044 20 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b   i<pPhrase->nTok
44045 65 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20  en; i++){..     
44046 20 66 74 73 33 53 65 67 52 65 61 64 65 72 43 75   fts3SegReaderCu
44047 72 73 6f 72 46 72 65 65 28 70 50 68 72 61 73 65  rsorFree(pPhrase
44048 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 70 53 65 67  ->aToken[i].pSeg
44049 63 73 72 29 3b 0d 0a 20 20 20 20 20 20 70 50 68  csr);..      pPh
4404a 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e  rase->aToken[i].
4404b 70 53 65 67 63 73 72 20 3d 20 30 3b 0d 0a 20 20  pSegcsr = 0;..  
4404c 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f    }..  }..}..../
4404d 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  *..** Return SQL
4404e 49 54 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42  ITE_CORRUPT_VTAB
4404f 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20 53 51  ...*/..#ifdef SQ
44050 4c 49 54 45 5f 44 45 42 55 47 0d 0a 53 51 4c 49  LITE_DEBUG..SQLI
44051 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
44052 71 6c 69 74 65 33 46 74 73 33 43 6f 72 72 75 70  qlite3Fts3Corrup
44053 74 28 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 53  t(){..  return S
44054 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 56 54  QLITE_CORRUPT_VT
44055 41 42 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a  AB;..}..#endif..
44056 0d 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f  ..#if !SQLITE_CO
44057 52 45 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6e 69 74 69  RE../*..** Initi
44058 61 6c 69 7a 65 20 41 50 49 20 70 6f 69 6e 74 65  alize API pointe
44059 72 20 74 61 62 6c 65 2c 20 69 66 20 72 65 71 75  r table, if requ
4405a 69 72 65 64 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  ired...*/..SQLIT
4405b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
4405c 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
4405d 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  (..  sqlite3 *db
4405e 2c 20 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  , ..  char **pzE
4405f 72 72 4d 73 67 2c 0d 0a 20 20 63 6f 6e 73 74 20  rrMsg,..  const 
44060 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
44061 69 6e 65 73 20 2a 70 41 70 69 0d 0a 29 7b 0d 0a  ines *pApi..){..
44062 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
44063 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0d 0a  ON_INIT2(pApi)..
44064 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
44065 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0d 0a 7d  Fts3Init(db);..}
44066 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 65 6e 64  ..#endif....#end
44067 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  if..../*********
44068 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
44069 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
4406a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4406b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4406c 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
4406d 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
4406e 65 20 66 74 73 33 5f 61 75 78 2e 63 20 2a 2a 2a  e fts3_aux.c ***
4406f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44071 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
44072 30 31 31 20 4a 61 6e 20 32 37 0d 0a 2a 2a 0d 0a  011 Jan 27..**..
44073 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
44074 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
44075 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
44076 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
44077 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
44078 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
44079 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
4407a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
4407b 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
4407c 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
4407d 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
4407e 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
4407f 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
44080 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
44081 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
44082 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
44083 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
44084 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
44085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44088 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44089 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2f 0d  *******..**..*/.
4408a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
4408b 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
4408c 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
4408d 42 4c 45 5f 46 54 53 33 29 0d 0a 0d 0a 2f 2a 20  BLE_FTS3)..../* 
4408e 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
4408f 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c  .h> */../* #incl
44090 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a  ude <assert.h> *
44091 2f 0d 0a 0d 0a 74 79 70 65 64 65 66 20 73 74 72  /....typedef str
44092 75 63 74 20 46 74 73 33 61 75 78 54 61 62 6c 65  uct Fts3auxTable
44093 20 46 74 73 33 61 75 78 54 61 62 6c 65 3b 0d 0a   Fts3auxTable;..
44094 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
44095 74 73 33 61 75 78 43 75 72 73 6f 72 20 46 74 73  ts3auxCursor Fts
44096 33 61 75 78 43 75 72 73 6f 72 3b 0d 0a 0d 0a 73  3auxCursor;....s
44097 74 72 75 63 74 20 46 74 73 33 61 75 78 54 61 62  truct Fts3auxTab
44098 6c 65 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  le {..  sqlite3_
44099 76 74 61 62 20 62 61 73 65 3b 20 20 20 20 20 20  vtab base;      
4409a 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20          /* Base 
4409b 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53 51  class used by SQ
4409c 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0d 0a 20 20  Lite core */..  
4409d 46 74 73 33 54 61 62 6c 65 20 2a 70 46 74 73 33  Fts3Table *pFts3
4409e 54 61 62 3b 0d 0a 7d 3b 0d 0a 0d 0a 73 74 72 75  Tab;..};....stru
4409f 63 74 20 46 74 73 33 61 75 78 43 75 72 73 6f 72  ct Fts3auxCursor
440a0 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   {..  sqlite3_vt
440a1 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 20  ab_cursor base; 
440a2 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c        /* Base cl
440a3 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c 69  ass used by SQLi
440a4 74 65 20 63 6f 72 65 20 2a 2f 0d 0a 20 20 46 74  te core */..  Ft
440a5 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
440a6 20 63 73 72 3b 20 20 20 20 20 20 20 20 2f 2a 20   csr;        /* 
440a7 4d 75 73 74 20 62 65 20 72 69 67 68 74 20 61 66  Must be right af
440a8 74 65 72 20 22 62 61 73 65 22 20 2a 2f 0d 0a 20  ter "base" */.. 
440a9 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 66   Fts3SegFilter f
440aa 69 6c 74 65 72 3b 0d 0a 20 20 63 68 61 72 20 2a  ilter;..  char *
440ab 7a 53 74 6f 70 3b 0d 0a 20 20 69 6e 74 20 6e 53  zStop;..  int nS
440ac 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  top;            
440ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
440ae 65 2d 6c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  e-length of stri
440af 6e 67 20 7a 53 74 6f 70 20 2a 2f 0d 0a 20 20 69  ng zStop */..  i
440b0 6e 74 20 69 73 45 6f 66 3b 20 20 20 20 20 20 20  nt isEof;       
440b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
440b2 2a 20 54 72 75 65 20 69 66 20 63 75 72 73 6f 72  * True if cursor
440b3 20 69 73 20 61 74 20 45 4f 46 20 2a 2f 0d 0a 20   is at EOF */.. 
440b4 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
440b5 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20  Rowid;          
440b6 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 6f 77 69   /* Current rowi
440b7 64 20 2a 2f 0d 0a 0d 0a 20 20 69 6e 74 20 69 43  d */....  int iC
440b8 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
440b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
440ba 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 27 63  rent value of 'c
440bb 6f 6c 27 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20  ol' column */.. 
440bc 20 69 6e 74 20 6e 53 74 61 74 3b 20 20 20 20 20   int nStat;     
440bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440be 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 53 74 61   /* Size of aSta
440bf 74 5b 5d 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20  t[] array */..  
440c0 73 74 72 75 63 74 20 46 74 73 33 61 75 78 43 6f  struct Fts3auxCo
440c1 6c 73 74 61 74 73 20 7b 0d 0a 20 20 20 20 73 71  lstats {..    sq
440c2 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 44 6f 63  lite3_int64 nDoc
440c3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 27  ;           /* '
440c4 64 6f 63 75 6d 65 6e 74 73 27 20 76 61 6c 75 65  documents' value
440c5 73 20 66 6f 72 20 63 75 72 72 65 6e 74 20 63 73  s for current cs
440c6 72 20 72 6f 77 20 2a 2f 0d 0a 20 20 20 20 73 71  r row */..    sq
440c7 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 4f 63 63  lite3_int64 nOcc
440c8 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 27  ;           /* '
440c9 6f 63 63 75 72 72 65 6e 63 65 73 27 20 76 61 6c  occurrences' val
440ca 75 65 73 20 66 6f 72 20 63 75 72 72 65 6e 74 20  ues for current 
440cb 63 73 72 20 72 6f 77 20 2a 2f 0d 0a 20 20 7d 20  csr row */..  } 
440cc 2a 61 53 74 61 74 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f  *aStat;..};..../
440cd 2a 0d 0a 2a 2a 20 53 63 68 65 6d 61 20 6f 66 20  *..** Schema of 
440ce 74 68 65 20 74 65 72 6d 73 20 74 61 62 6c 65 2e  the terms table.
440cf 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46 54  ..*/..#define FT
440d0 53 33 5f 54 45 52 4d 53 5f 53 43 48 45 4d 41 20  S3_TERMS_SCHEMA 
440d1 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28  "CREATE TABLE x(
440d2 74 65 72 6d 2c 20 63 6f 6c 2c 20 64 6f 63 75 6d  term, col, docum
440d3 65 6e 74 73 2c 20 6f 63 63 75 72 72 65 6e 63 65  ents, occurrence
440d4 73 29 22 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  s)"..../*..** Th
440d5 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
440d6 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 66 6f   all the work fo
440d7 72 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e 6e  r both the xConn
440d8 65 63 74 20 61 6e 64 20 78 43 72 65 61 74 65 20  ect and xCreate 
440d9 6d 65 74 68 6f 64 73 2e 0d 0a 2a 2a 20 54 68 65  methods...** The
440da 73 65 20 74 61 62 6c 65 73 20 68 61 76 65 20 6e  se tables have n
440db 6f 20 70 65 72 73 69 73 74 65 6e 74 20 72 65 70  o persistent rep
440dc 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
440dd 68 65 69 72 20 6f 77 6e 2c 20 73 6f 20 78 43 6f  heir own, so xCo
440de 6e 6e 65 63 74 0d 0a 2a 2a 20 61 6e 64 20 78 43  nnect..** and xC
440df 72 65 61 74 65 20 61 72 65 20 69 64 65 6e 74 69  reate are identi
440e0 63 61 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0d  cal operations..
440e1 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
440e2 66 74 73 33 61 75 78 43 6f 6e 6e 65 63 74 4d 65  fts3auxConnectMe
440e3 74 68 6f 64 28 0d 0a 20 20 73 71 6c 69 74 65 33  thod(..  sqlite3
440e4 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
440e5 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
440e6 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
440e7 2a 2f 0d 0a 20 20 76 6f 69 64 20 2a 70 55 6e 75  */..  void *pUnu
440e8 73 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  sed,            
440e9 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20        /* Unused 
440ea 2a 2f 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  */..  int argc, 
440eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440ec 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
440ed 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61  of elements in a
440ee 72 67 76 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20  rgv array */..  
440ef 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
440f0 73 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20  st *argv,       
440f1 2f 2a 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  /* xCreate/xConn
440f2 65 63 74 20 61 72 67 75 6d 65 6e 74 20 61 72 72  ect argument arr
440f3 61 79 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  ay */..  sqlite3
440f4 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 20  _vtab **ppVtab, 
440f5 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
440f6 20 4e 65 77 20 73 71 6c 69 74 65 33 5f 76 74 61   New sqlite3_vta
440f7 62 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 63  b object */..  c
440f8 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20  har **pzErr     
440f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
440fa 2a 20 4f 55 54 3a 20 73 71 6c 69 74 65 33 5f 6d  * OUT: sqlite3_m
440fb 61 6c 6c 6f 63 27 64 20 65 72 72 6f 72 20 6d 65  alloc'd error me
440fc 73 73 61 67 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ssage */..){..  
440fd 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 62 3b  char const *zDb;
440fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440ff 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
44100 61 73 65 20 28 65 2e 67 2e 20 22 6d 61 69 6e 22  ase (e.g. "main"
44101 29 20 2a 2f 0d 0a 20 20 63 68 61 72 20 63 6f 6e  ) */..  char con
44102 73 74 20 2a 7a 46 74 73 33 3b 20 20 20 20 20 20  st *zFts3;      
44103 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
44104 6f 66 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f  of fts3 table */
44105 0d 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  ..  int nDb;    
44106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44107 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
44108 20 73 74 72 6c 65 6e 28 7a 44 62 29 20 2a 2f 0d   strlen(zDb) */.
44109 0a 20 20 69 6e 74 20 6e 46 74 73 33 3b 20 20 20  .  int nFts3;   
4410a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4410b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
4410c 73 74 72 6c 65 6e 28 7a 46 74 73 33 29 20 2a 2f  strlen(zFts3) */
4410d 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  ..  int nByte;  
4410e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4410f 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20      /* Bytes of 
44110 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74  space to allocat
44111 65 20 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74  e here */..  int
44112 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
44113 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44114 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
44115 79 20 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  y declare_vtab()
44116 20 2a 2f 0d 0a 20 20 46 74 73 33 61 75 78 54 61   */..  Fts3auxTa
44117 62 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20  ble *p;         
44118 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
44119 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 74  l table object t
4411a 6f 20 72 65 74 75 72 6e 20 2a 2f 0d 0a 0d 0a 20  o return */.... 
4411b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
4411c 52 28 70 55 6e 75 73 65 64 29 3b 0d 0a 0d 0a 20  R(pUnused);.... 
4411d 20 2f 2a 20 54 68 65 20 75 73 65 72 20 73 68 6f   /* The user sho
4411e 75 6c 64 20 73 70 65 63 69 66 79 20 61 20 73 69  uld specify a si
4411f 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 2d 20  ngle argument - 
44120 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 66  the name of an f
44121 74 73 33 20 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20  ts3 table. */.. 
44122 20 69 66 28 20 61 72 67 63 21 3d 34 20 29 7b 0d   if( argc!=4 ){.
44123 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
44124 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a  lite3_mprintf(..
44125 20 20 20 20 20 20 20 20 22 77 72 6f 6e 67 20 6e          "wrong n
44126 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
44127 74 73 20 74 6f 20 66 74 73 34 61 75 78 20 63 6f  ts to fts4aux co
44128 6e 73 74 72 75 63 74 6f 72 22 0d 0a 20 20 20 20  nstructor"..    
44129 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53  );..    return S
4412a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20  QLITE_ERROR;..  
4412b 7d 0d 0a 0d 0a 20 20 7a 44 62 20 3d 20 61 72 67  }....  zDb = arg
4412c 76 5b 31 5d 3b 20 0d 0a 20 20 6e 44 62 20 3d 20  v[1]; ..  nDb = 
4412d 73 74 72 6c 65 6e 28 7a 44 62 29 3b 0d 0a 20 20  strlen(zDb);..  
4412e 7a 46 74 73 33 20 3d 20 61 72 67 76 5b 33 5d 3b  zFts3 = argv[3];
4412f 0d 0a 20 20 6e 46 74 73 33 20 3d 20 73 74 72 6c  ..  nFts3 = strl
44130 65 6e 28 7a 46 74 73 33 29 3b 0d 0a 0d 0a 20 20  en(zFts3);....  
44131 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63  rc = sqlite3_dec
44132 6c 61 72 65 5f 76 74 61 62 28 64 62 2c 20 46 54  lare_vtab(db, FT
44133 53 33 5f 54 45 52 4d 53 5f 53 43 48 45 4d 41 29  S3_TERMS_SCHEMA)
44134 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
44135 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
44136 72 63 3b 0d 0a 0d 0a 20 20 6e 42 79 74 65 20 3d  rc;....  nByte =
44137 20 73 69 7a 65 6f 66 28 46 74 73 33 61 75 78 54   sizeof(Fts3auxT
44138 61 62 6c 65 29 20 2b 20 73 69 7a 65 6f 66 28 46  able) + sizeof(F
44139 74 73 33 54 61 62 6c 65 29 20 2b 20 6e 44 62 20  ts3Table) + nDb 
4413a 2b 20 6e 46 74 73 33 20 2b 20 32 3b 0d 0a 20 20  + nFts3 + 2;..  
4413b 70 20 3d 20 28 46 74 73 33 61 75 78 54 61 62 6c  p = (Fts3auxTabl
4413c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  e *)sqlite3_mall
4413d 6f 63 28 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66  oc(nByte);..  if
4413e 28 20 21 70 20 29 20 72 65 74 75 72 6e 20 53 51  ( !p ) return SQ
4413f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 6d  LITE_NOMEM;..  m
44140 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74  emset(p, 0, nByt
44141 65 29 3b 0d 0a 0d 0a 20 20 70 2d 3e 70 46 74 73  e);....  p->pFts
44142 33 54 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c  3Tab = (Fts3Tabl
44143 65 20 2a 29 26 70 5b 31 5d 3b 0d 0a 20 20 70 2d  e *)&p[1];..  p-
44144 3e 70 46 74 73 33 54 61 62 2d 3e 7a 44 62 20 3d  >pFts3Tab->zDb =
44145 20 28 63 68 61 72 20 2a 29 26 70 2d 3e 70 46 74   (char *)&p->pFt
44146 73 33 54 61 62 5b 31 5d 3b 0d 0a 20 20 70 2d 3e  s3Tab[1];..  p->
44147 70 46 74 73 33 54 61 62 2d 3e 7a 4e 61 6d 65 20  pFts3Tab->zName 
44148 3d 20 26 70 2d 3e 70 46 74 73 33 54 61 62 2d 3e  = &p->pFts3Tab->
44149 7a 44 62 5b 6e 44 62 2b 31 5d 3b 0d 0a 20 20 70  zDb[nDb+1];..  p
4414a 2d 3e 70 46 74 73 33 54 61 62 2d 3e 64 62 20 3d  ->pFts3Tab->db =
4414b 20 64 62 3b 0d 0a 20 20 70 2d 3e 70 46 74 73 33   db;..  p->pFts3
4414c 54 61 62 2d 3e 6e 49 6e 64 65 78 20 3d 20 31 3b  Tab->nIndex = 1;
4414d 0d 0a 0d 0a 20 20 6d 65 6d 63 70 79 28 28 63 68  ....  memcpy((ch
4414e 61 72 20 2a 29 70 2d 3e 70 46 74 73 33 54 61 62  ar *)p->pFts3Tab
4414f 2d 3e 7a 44 62 2c 20 7a 44 62 2c 20 6e 44 62 29  ->zDb, zDb, nDb)
44150 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 28 63 68 61  ;..  memcpy((cha
44151 72 20 2a 29 70 2d 3e 70 46 74 73 33 54 61 62 2d  r *)p->pFts3Tab-
44152 3e 7a 4e 61 6d 65 2c 20 7a 46 74 73 33 2c 20 6e  >zName, zFts3, n
44153 46 74 73 33 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Fts3);..  sqlite
44154 33 46 74 73 33 44 65 71 75 6f 74 65 28 28 63 68  3Fts3Dequote((ch
44155 61 72 20 2a 29 70 2d 3e 70 46 74 73 33 54 61 62  ar *)p->pFts3Tab
44156 2d 3e 7a 4e 61 6d 65 29 3b 0d 0a 0d 0a 20 20 2a  ->zName);....  *
44157 70 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65  ppVtab = (sqlite
44158 33 5f 76 74 61 62 20 2a 29 70 3b 0d 0a 20 20 72  3_vtab *)p;..  r
44159 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
4415a 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  ..}..../*..** Th
4415b 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
4415c 20 74 68 65 20 77 6f 72 6b 20 66 6f 72 20 62 6f   the work for bo
4415d 74 68 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65  th the xDisconne
4415e 63 74 20 61 6e 64 20 78 44 65 73 74 72 6f 79 20  ct and xDestroy 
4415f 6d 65 74 68 6f 64 73 2e 0d 0a 2a 2a 20 54 68 65  methods...** The
44160 73 65 20 74 61 62 6c 65 73 20 68 61 76 65 20 6e  se tables have n
44161 6f 20 70 65 72 73 69 73 74 65 6e 74 20 72 65 70  o persistent rep
44162 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
44163 68 65 69 72 20 6f 77 6e 2c 20 73 6f 20 78 44 69  heir own, so xDi
44164 73 63 6f 6e 6e 65 63 74 0d 0a 2a 2a 20 61 6e 64  sconnect..** and
44165 20 78 44 65 73 74 72 6f 79 20 61 72 65 20 69 64   xDestroy are id
44166 65 6e 74 69 63 61 6c 20 6f 70 65 72 61 74 69 6f  entical operatio
44167 6e 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ns...*/..static 
44168 69 6e 74 20 66 74 73 33 61 75 78 44 69 73 63 6f  int fts3auxDisco
44169 6e 6e 65 63 74 4d 65 74 68 6f 64 28 73 71 6c 69  nnectMethod(sqli
4416a 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
4416b 7b 0d 0a 20 20 46 74 73 33 61 75 78 54 61 62 6c  {..  Fts3auxTabl
4416c 65 20 2a 70 20 3d 20 28 46 74 73 33 61 75 78 54  e *p = (Fts3auxT
4416d 61 62 6c 65 20 2a 29 70 56 74 61 62 3b 0d 0a 20  able *)pVtab;.. 
4416e 20 46 74 73 33 54 61 62 6c 65 20 2a 70 46 74 73   Fts3Table *pFts
4416f 33 20 3d 20 70 2d 3e 70 46 74 73 33 54 61 62 3b  3 = p->pFts3Tab;
44170 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20  ..  int i;....  
44171 2f 2a 20 46 72 65 65 20 61 6e 79 20 70 72 65 70  /* Free any prep
44172 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
44173 68 65 6c 64 20 2a 2f 0d 0a 20 20 66 6f 72 28 69  held */..  for(i
44174 3d 30 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72 61  =0; i<SizeofArra
44175 79 28 70 46 74 73 33 2d 3e 61 53 74 6d 74 29 3b  y(pFts3->aStmt);
44176 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69   i++){..    sqli
44177 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 46 74  te3_finalize(pFt
44178 73 33 2d 3e 61 53 74 6d 74 5b 69 5d 29 3b 0d 0a  s3->aStmt[i]);..
44179 20 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66    }..  sqlite3_f
4417a 72 65 65 28 70 46 74 73 33 2d 3e 7a 53 65 67 6d  ree(pFts3->zSegm
4417b 65 6e 74 73 54 62 6c 29 3b 0d 0a 20 20 73 71 6c  entsTbl);..  sql
4417c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
4417d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
4417e 4b 3b 0d 0a 7d 0d 0a 0d 0a 23 64 65 66 69 6e 65  K;..}....#define
4417f 20 46 54 53 34 41 55 58 5f 45 51 5f 43 4f 4e 53   FTS4AUX_EQ_CONS
44180 54 52 41 49 4e 54 20 31 0d 0a 23 64 65 66 69 6e  TRAINT 1..#defin
44181 65 20 46 54 53 34 41 55 58 5f 47 45 5f 43 4f 4e  e FTS4AUX_GE_CON
44182 53 54 52 41 49 4e 54 20 32 0d 0a 23 64 65 66 69  STRAINT 2..#defi
44183 6e 65 20 46 54 53 34 41 55 58 5f 4c 45 5f 43 4f  ne FTS4AUX_LE_CO
44184 4e 53 54 52 41 49 4e 54 20 34 0d 0a 0d 0a 2f 2a  NSTRAINT 4..../*
44185 0d 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 20  ..** xBestIndex 
44186 2d 20 41 6e 61 6c 79 7a 65 20 61 20 57 48 45 52  - Analyze a WHER
44187 45 20 61 6e 64 20 4f 52 44 45 52 20 42 59 20 63  E and ORDER BY c
44188 6c 61 75 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  lause...*/..stat
44189 69 63 20 69 6e 74 20 66 74 73 33 61 75 78 42 65  ic int fts3auxBe
4418a 73 74 49 6e 64 65 78 4d 65 74 68 6f 64 28 0d 0a  stIndexMethod(..
4418b 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
4418c 70 56 54 61 62 2c 20 0d 0a 20 20 73 71 6c 69 74  pVTab, ..  sqlit
4418d 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
4418e 49 6e 66 6f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  Info..){..  int 
4418f 69 3b 0d 0a 20 20 69 6e 74 20 69 45 71 20 3d 20  i;..  int iEq = 
44190 2d 31 3b 0d 0a 20 20 69 6e 74 20 69 47 65 20 3d  -1;..  int iGe =
44191 20 2d 31 3b 0d 0a 20 20 69 6e 74 20 69 4c 65 20   -1;..  int iLe 
44192 3d 20 2d 31 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45  = -1;....  UNUSE
44193 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 54 61  D_PARAMETER(pVTa
44194 62 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 69 73  b);....  /* This
44195 20 76 74 61 62 20 64 65 6c 69 76 65 72 73 20 61   vtab delivers a
44196 6c 77 61 79 73 20 72 65 73 75 6c 74 73 20 69 6e  lways results in
44197 20 22 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20   "ORDER BY term 
44198 41 53 43 22 20 6f 72 64 65 72 2e 20 2a 2f 0d 0a  ASC" order. */..
44199 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 4f 72    if( pInfo->nOr
4419a 64 65 72 42 79 3d 3d 31 20 0d 0a 20 20 20 26 26  derBy==1 ..   &&
4419b 20 70 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79   pInfo->aOrderBy
4419c 5b 30 5d 2e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 0d  [0].iColumn==0 .
4419d 0a 20 20 20 26 26 20 70 49 6e 66 6f 2d 3e 61 4f  .   && pInfo->aO
4419e 72 64 65 72 42 79 5b 30 5d 2e 64 65 73 63 3d 3d  rderBy[0].desc==
4419f 30 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 70 49 6e  0..  ){..    pIn
441a0 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75  fo->orderByConsu
441a1 6d 65 64 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 0d  med = 1;..  }...
441a2 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72  .  /* Search for
441a3 20 65 71 75 61 6c 69 74 79 20 61 6e 64 20 72 61   equality and ra
441a4 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  nge constraints 
441a5 6f 6e 20 74 68 65 20 22 74 65 72 6d 22 20 63 6f  on the "term" co
441a6 6c 75 6d 6e 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28  lumn. */..  for(
441a7 69 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43  i=0; i<pInfo->nC
441a8 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b  onstraint; i++){
441a9 0d 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d  ..    if( pInfo-
441aa 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
441ab 75 73 61 62 6c 65 20 26 26 20 70 49 6e 66 6f 2d  usable && pInfo-
441ac 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
441ad 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0d 0a 20  iColumn==0 ){.. 
441ae 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 49       int op = pI
441af 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
441b0 5b 69 5d 2e 6f 70 3b 0d 0a 20 20 20 20 20 20 69  [i].op;..      i
441b1 66 28 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e  f( op==SQLITE_IN
441b2 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45  DEX_CONSTRAINT_E
441b3 51 20 29 20 69 45 71 20 3d 20 69 3b 0d 0a 20 20  Q ) iEq = i;..  
441b4 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 51 4c 49      if( op==SQLI
441b5 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
441b6 49 4e 54 5f 4c 54 20 29 20 69 4c 65 20 3d 20 69  INT_LT ) iLe = i
441b7 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 3d  ;..      if( op=
441b8 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
441b9 4e 53 54 52 41 49 4e 54 5f 4c 45 20 29 20 69 4c  NSTRAINT_LE ) iL
441ba 65 20 3d 20 69 3b 0d 0a 20 20 20 20 20 20 69 66  e = i;..      if
441bb 28 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  ( op==SQLITE_IND
441bc 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54  EX_CONSTRAINT_GT
441bd 20 29 20 69 47 65 20 3d 20 69 3b 0d 0a 20 20 20   ) iGe = i;..   
441be 20 20 20 69 66 28 20 6f 70 3d 3d 53 51 4c 49 54     if( op==SQLIT
441bf 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
441c0 4e 54 5f 47 45 20 29 20 69 47 65 20 3d 20 69 3b  NT_GE ) iGe = i;
441c1 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
441c2 20 20 69 66 28 20 69 45 71 3e 3d 30 20 29 7b 0d    if( iEq>=0 ){.
441c3 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e  .    pInfo->idxN
441c4 75 6d 20 3d 20 46 54 53 34 41 55 58 5f 45 51 5f  um = FTS4AUX_EQ_
441c5 43 4f 4e 53 54 52 41 49 4e 54 3b 0d 0a 20 20 20  CONSTRAINT;..   
441c6 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61   pInfo->aConstra
441c7 69 6e 74 55 73 61 67 65 5b 69 45 71 5d 2e 61 72  intUsage[iEq].ar
441c8 67 76 49 6e 64 65 78 20 3d 20 31 3b 0d 0a 20 20  gvIndex = 1;..  
441c9 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74    pInfo->estimat
441ca 65 64 43 6f 73 74 20 3d 20 35 3b 0d 0a 20 20 7d  edCost = 5;..  }
441cb 65 6c 73 65 7b 0d 0a 20 20 20 20 70 49 6e 66 6f  else{..    pInfo
441cc 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0d 0a 20  ->idxNum = 0;.. 
441cd 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61     pInfo->estima
441ce 74 65 64 43 6f 73 74 20 3d 20 32 30 30 30 30 3b  tedCost = 20000;
441cf 0d 0a 20 20 20 20 69 66 28 20 69 47 65 3e 3d 30  ..    if( iGe>=0
441d0 20 29 7b 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f   ){..      pInfo
441d1 2d 3e 69 64 78 4e 75 6d 20 2b 3d 20 46 54 53 34  ->idxNum += FTS4
441d2 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41 49 4e  AUX_GE_CONSTRAIN
441d3 54 3b 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  T;..      pInfo-
441d4 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
441d5 65 5b 69 47 65 5d 2e 61 72 67 76 49 6e 64 65 78  e[iGe].argvIndex
441d6 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 70 49 6e   = 1;..      pIn
441d7 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
441d8 74 20 2f 3d 20 32 3b 0d 0a 20 20 20 20 7d 0d 0a  t /= 2;..    }..
441d9 20 20 20 20 69 66 28 20 69 4c 65 3e 3d 30 20 29      if( iLe>=0 )
441da 7b 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  {..      pInfo->
441db 69 64 78 4e 75 6d 20 2b 3d 20 46 54 53 34 41 55  idxNum += FTS4AU
441dc 58 5f 4c 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b  X_LE_CONSTRAINT;
441dd 0d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  ..      pInfo->a
441de 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
441df 69 4c 65 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  iLe].argvIndex =
441e0 20 31 20 2b 20 28 69 47 65 3e 3d 30 29 3b 0d 0a   1 + (iGe>=0);..
441e1 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74        pInfo->est
441e2 69 6d 61 74 65 64 43 6f 73 74 20 2f 3d 20 32 3b  imatedCost /= 2;
441e3 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
441e4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
441e5 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
441e6 20 78 4f 70 65 6e 20 2d 20 4f 70 65 6e 20 61 20   xOpen - Open a 
441e7 63 75 72 73 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61  cursor...*/..sta
441e8 74 69 63 20 69 6e 74 20 66 74 73 33 61 75 78 4f  tic int fts3auxO
441e9 70 65 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  penMethod(sqlite
441ea 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73  3_vtab *pVTab, s
441eb 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
441ec 6f 72 20 2a 2a 70 70 43 73 72 29 7b 0d 0a 20 20  or **ppCsr){..  
441ed 46 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70  Fts3auxCursor *p
441ee 43 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Csr;            
441ef 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75  /* Pointer to cu
441f0 72 73 6f 72 20 6f 62 6a 65 63 74 20 74 6f 20 72  rsor object to r
441f1 65 74 75 72 6e 20 2a 2f 0d 0a 0d 0a 20 20 55 4e  eturn */....  UN
441f2 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
441f3 56 54 61 62 29 3b 0d 0a 0d 0a 20 20 70 43 73 72  VTab);....  pCsr
441f4 20 3d 20 28 46 74 73 33 61 75 78 43 75 72 73 6f   = (Fts3auxCurso
441f5 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
441f6 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 61 75  oc(sizeof(Fts3au
441f7 78 43 75 72 73 6f 72 29 29 3b 0d 0a 20 20 69 66  xCursor));..  if
441f8 28 20 21 70 43 73 72 20 29 20 72 65 74 75 72 6e  ( !pCsr ) return
441f9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
441fa 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30    memset(pCsr, 0
441fb 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 61 75 78  , sizeof(Fts3aux
441fc 43 75 72 73 6f 72 29 29 3b 0d 0a 0d 0a 20 20 2a  Cursor));....  *
441fd 70 70 43 73 72 20 3d 20 28 73 71 6c 69 74 65 33  ppCsr = (sqlite3
441fe 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 29 70  _vtab_cursor *)p
441ff 43 73 72 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53  Csr;..  return S
44200 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
44201 2f 2a 0d 0a 2a 2a 20 78 43 6c 6f 73 65 20 2d 20  /*..** xClose - 
44202 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 0d  Close a cursor..
44203 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
44204 66 74 73 33 61 75 78 43 6c 6f 73 65 4d 65 74 68  fts3auxCloseMeth
44205 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  od(sqlite3_vtab_
44206 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
44207 7b 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  {..  Fts3Table *
44208 70 46 74 73 33 20 3d 20 28 28 46 74 73 33 61 75  pFts3 = ((Fts3au
44209 78 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72  xTable *)pCursor
4420a 2d 3e 70 56 74 61 62 29 2d 3e 70 46 74 73 33 54  ->pVtab)->pFts3T
4420b 61 62 3b 0d 0a 20 20 46 74 73 33 61 75 78 43 75  ab;..  Fts3auxCu
4420c 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74  rsor *pCsr = (Ft
4420d 73 33 61 75 78 43 75 72 73 6f 72 20 2a 29 70 43  s3auxCursor *)pC
4420e 75 72 73 6f 72 3b 0d 0a 0d 0a 20 20 73 71 6c 69  ursor;....  sqli
4420f 74 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43  te3Fts3SegmentsC
44210 6c 6f 73 65 28 70 46 74 73 33 29 3b 0d 0a 20 20  lose(pFts3);..  
44211 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
44212 61 64 65 72 46 69 6e 69 73 68 28 26 70 43 73 72  aderFinish(&pCsr
44213 2d 3e 63 73 72 29 3b 0d 0a 20 20 73 71 6c 69 74  ->csr);..  sqlit
44214 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29  e3_free((void *)
44215 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e 7a 54 65  pCsr->filter.zTe
44216 72 6d 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  rm);..  sqlite3_
44217 66 72 65 65 28 70 43 73 72 2d 3e 7a 53 74 6f 70  free(pCsr->zStop
44218 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
44219 65 65 28 70 43 73 72 2d 3e 61 53 74 61 74 29 3b  ee(pCsr->aStat);
4421a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
4421b 28 70 43 73 72 29 3b 0d 0a 20 20 72 65 74 75 72  (pCsr);..  retur
4421c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
4421d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  ...static int ft
4421e 73 33 61 75 78 47 72 6f 77 53 74 61 74 41 72 72  s3auxGrowStatArr
4421f 61 79 28 46 74 73 33 61 75 78 43 75 72 73 6f 72  ay(Fts3auxCursor
44220 20 2a 70 43 73 72 2c 20 69 6e 74 20 6e 53 69 7a   *pCsr, int nSiz
44221 65 29 7b 0d 0a 20 20 69 66 28 20 6e 53 69 7a 65  e){..  if( nSize
44222 3e 70 43 73 72 2d 3e 6e 53 74 61 74 20 29 7b 0d  >pCsr->nStat ){.
44223 0a 20 20 20 20 73 74 72 75 63 74 20 46 74 73 33  .    struct Fts3
44224 61 75 78 43 6f 6c 73 74 61 74 73 20 2a 61 4e 65  auxColstats *aNe
44225 77 3b 0d 0a 20 20 20 20 61 4e 65 77 20 3d 20 28  w;..    aNew = (
44226 73 74 72 75 63 74 20 46 74 73 33 61 75 78 43 6f  struct Fts3auxCo
44227 6c 73 74 61 74 73 20 2a 29 73 71 6c 69 74 65 33  lstats *)sqlite3
44228 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 61  _realloc(pCsr->a
44229 53 74 61 74 2c 20 0d 0a 20 20 20 20 20 20 20 20  Stat, ..        
4422a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 46 74  sizeof(struct Ft
4422b 73 33 61 75 78 43 6f 6c 73 74 61 74 73 29 20 2a  s3auxColstats) *
4422c 20 6e 53 69 7a 65 0d 0a 20 20 20 20 29 3b 0d 0a   nSize..    );..
4422d 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
4422e 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
4422f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 6d 65 6d 73  NOMEM;..    mems
44230 65 74 28 26 61 4e 65 77 5b 70 43 73 72 2d 3e 6e  et(&aNew[pCsr->n
44231 53 74 61 74 5d 2c 20 30 2c 20 0d 0a 20 20 20 20  Stat], 0, ..    
44232 20 20 20 20 73 69 7a 65 6f 66 28 73 74 72 75 63      sizeof(struc
44233 74 20 46 74 73 33 61 75 78 43 6f 6c 73 74 61 74  t Fts3auxColstat
44234 73 29 20 2a 20 28 6e 53 69 7a 65 20 2d 20 70 43  s) * (nSize - pC
44235 73 72 2d 3e 6e 53 74 61 74 29 0d 0a 20 20 20 20  sr->nStat)..    
44236 29 3b 0d 0a 20 20 20 20 70 43 73 72 2d 3e 61 53  );..    pCsr->aS
44237 74 61 74 20 3d 20 61 4e 65 77 3b 0d 0a 20 20 20  tat = aNew;..   
44238 20 70 43 73 72 2d 3e 6e 53 74 61 74 20 3d 20 6e   pCsr->nStat = n
44239 53 69 7a 65 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  Size;..  }..  re
4423a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
4423b 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 78 4e 65  .}..../*..** xNe
4423c 78 74 20 2d 20 41 64 76 61 6e 63 65 20 74 68 65  xt - Advance the
4423d 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e   cursor to the n
4423e 65 78 74 20 72 6f 77 2c 20 69 66 20 61 6e 79 2e  ext row, if any.
4423f 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
44240 20 66 74 73 33 61 75 78 4e 65 78 74 4d 65 74 68   fts3auxNextMeth
44241 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  od(sqlite3_vtab_
44242 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
44243 7b 0d 0a 20 20 46 74 73 33 61 75 78 43 75 72 73  {..  Fts3auxCurs
44244 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33  or *pCsr = (Fts3
44245 61 75 78 43 75 72 73 6f 72 20 2a 29 70 43 75 72  auxCursor *)pCur
44246 73 6f 72 3b 0d 0a 20 20 46 74 73 33 54 61 62 6c  sor;..  Fts3Tabl
44247 65 20 2a 70 46 74 73 33 20 3d 20 28 28 46 74 73  e *pFts3 = ((Fts
44248 33 61 75 78 54 61 62 6c 65 20 2a 29 70 43 75 72  3auxTable *)pCur
44249 73 6f 72 2d 3e 70 56 74 61 62 29 2d 3e 70 46 74  sor->pVtab)->pFt
4424a 73 33 54 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63  s3Tab;..  int rc
4424b 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 6e 63 72 65 6d  ;....  /* Increm
4424c 65 6e 74 20 6f 75 72 20 70 72 65 74 65 6e 64 20  ent our pretend 
4424d 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 2a 2f 0d  rowid value. */.
4424e 0a 20 20 70 43 73 72 2d 3e 69 52 6f 77 69 64 2b  .  pCsr->iRowid+
4424f 2b 3b 0d 0a 0d 0a 20 20 66 6f 72 28 70 43 73 72  +;....  for(pCsr
44250 2d 3e 69 43 6f 6c 2b 2b 3b 20 70 43 73 72 2d 3e  ->iCol++; pCsr->
44251 69 43 6f 6c 3c 70 43 73 72 2d 3e 6e 53 74 61 74  iCol<pCsr->nStat
44252 3b 20 70 43 73 72 2d 3e 69 43 6f 6c 2b 2b 29 7b  ; pCsr->iCol++){
44253 0d 0a 20 20 20 20 69 66 28 20 70 43 73 72 2d 3e  ..    if( pCsr->
44254 61 53 74 61 74 5b 70 43 73 72 2d 3e 69 43 6f 6c  aStat[pCsr->iCol
44255 5d 2e 6e 44 6f 63 3e 30 20 29 20 72 65 74 75 72  ].nDoc>0 ) retur
44256 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
44257 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  }....  rc = sqli
44258 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
44259 53 74 65 70 28 70 46 74 73 33 2c 20 26 70 43 73  Step(pFts3, &pCs
4425a 72 2d 3e 63 73 72 29 3b 0d 0a 20 20 69 66 28 20  r->csr);..  if( 
4425b 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
4425c 7b 0d 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30  {..    int i = 0
4425d 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63 6c  ;..    int nDocl
4425e 69 73 74 20 3d 20 70 43 73 72 2d 3e 63 73 72 2e  ist = pCsr->csr.
4425f 6e 44 6f 63 6c 69 73 74 3b 0d 0a 20 20 20 20 63  nDoclist;..    c
44260 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 20 3d 20  har *aDoclist = 
44261 70 43 73 72 2d 3e 63 73 72 2e 61 44 6f 63 6c 69  pCsr->csr.aDocli
44262 73 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69 43 6f  st;..    int iCo
44263 6c 3b 0d 0a 0d 0a 20 20 20 20 69 6e 74 20 65 53  l;....    int eS
44264 74 61 74 65 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20  tate = 0;....   
44265 20 69 66 28 20 70 43 73 72 2d 3e 7a 53 74 6f 70   if( pCsr->zStop
44266 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e   ){..      int n
44267 20 3d 20 28 70 43 73 72 2d 3e 6e 53 74 6f 70 3c   = (pCsr->nStop<
44268 70 43 73 72 2d 3e 63 73 72 2e 6e 54 65 72 6d 29  pCsr->csr.nTerm)
44269 20 3f 20 70 43 73 72 2d 3e 6e 53 74 6f 70 20 3a   ? pCsr->nStop :
4426a 20 70 43 73 72 2d 3e 63 73 72 2e 6e 54 65 72 6d   pCsr->csr.nTerm
4426b 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6d 63 20  ;..      int mc 
4426c 3d 20 6d 65 6d 63 6d 70 28 70 43 73 72 2d 3e 7a  = memcmp(pCsr->z
4426d 53 74 6f 70 2c 20 70 43 73 72 2d 3e 63 73 72 2e  Stop, pCsr->csr.
4426e 7a 54 65 72 6d 2c 20 6e 29 3b 0d 0a 20 20 20 20  zTerm, n);..    
4426f 20 20 69 66 28 20 6d 63 3c 30 20 7c 7c 20 28 6d    if( mc<0 || (m
44270 63 3d 3d 30 20 26 26 20 70 43 73 72 2d 3e 63 73  c==0 && pCsr->cs
44271 72 2e 6e 54 65 72 6d 3e 70 43 73 72 2d 3e 6e 53  r.nTerm>pCsr->nS
44272 74 6f 70 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  top) ){..       
44273 20 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31   pCsr->isEof = 1
44274 3b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ;..        retur
44275 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20  n SQLITE_OK;..  
44276 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a      }..    }....
44277 20 20 20 20 69 66 28 20 66 74 73 33 61 75 78 47      if( fts3auxG
44278 72 6f 77 53 74 61 74 41 72 72 61 79 28 70 43 73  rowStatArray(pCs
44279 72 2c 20 32 29 20 29 20 72 65 74 75 72 6e 20 53  r, 2) ) return S
4427a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
4427b 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2d 3e 61    memset(pCsr->a
4427c 53 74 61 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Stat, 0, sizeof(
4427d 73 74 72 75 63 74 20 46 74 73 33 61 75 78 43 6f  struct Fts3auxCo
4427e 6c 73 74 61 74 73 29 20 2a 20 70 43 73 72 2d 3e  lstats) * pCsr->
4427f 6e 53 74 61 74 29 3b 0d 0a 20 20 20 20 69 43 6f  nStat);..    iCo
44280 6c 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 77 68  l = 0;....    wh
44281 69 6c 65 28 20 69 3c 6e 44 6f 63 6c 69 73 74 20  ile( i<nDoclist 
44282 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
44283 33 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0d 0a  3_int64 v = 0;..
44284 0d 0a 20 20 20 20 20 20 69 20 2b 3d 20 73 71 6c  ..      i += sql
44285 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
44286 74 28 26 61 44 6f 63 6c 69 73 74 5b 69 5d 2c 20  t(&aDoclist[i], 
44287 26 76 29 3b 0d 0a 20 20 20 20 20 20 73 77 69 74  &v);..      swit
44288 63 68 28 20 65 53 74 61 74 65 20 29 7b 0d 0a 20  ch( eState ){.. 
44289 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20         /* State 
4428a 30 2e 20 49 6e 20 74 68 69 73 20 73 74 61 74 65  0. In this state
4428b 20 74 68 65 20 69 6e 74 65 67 65 72 20 6a 75 73   the integer jus
4428c 74 20 72 65 61 64 20 77 61 73 20 61 20 64 6f 63  t read was a doc
4428d 69 64 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20  id. */..        
4428e 63 61 73 65 20 30 3a 0d 0a 20 20 20 20 20 20 20  case 0:..       
4428f 20 20 20 70 43 73 72 2d 3e 61 53 74 61 74 5b 30     pCsr->aStat[0
44290 5d 2e 6e 44 6f 63 2b 2b 3b 0d 0a 20 20 20 20 20  ].nDoc++;..     
44291 20 20 20 20 20 65 53 74 61 74 65 20 3d 20 31 3b       eState = 1;
44292 0d 0a 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c  ..          iCol
44293 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20   = 0;..         
44294 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20   break;....     
44295 20 20 20 2f 2a 20 53 74 61 74 65 20 31 2e 20 49     /* State 1. I
44296 6e 20 74 68 69 73 20 73 74 61 74 65 20 77 65 20  n this state we 
44297 61 72 65 20 65 78 70 65 63 74 69 6e 67 20 65 69  are expecting ei
44298 74 68 65 72 20 61 20 31 2c 20 69 6e 64 69 63 61  ther a 1, indica
44299 74 69 6e 67 0d 0a 20 20 20 20 20 20 20 20 2a 2a  ting..        **
4429a 20 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77   that the follow
4429b 69 6e 67 20 69 6e 74 65 67 65 72 20 77 69 6c 6c  ing integer will
4429c 20 62 65 20 61 20 63 6f 6c 75 6d 6e 20 6e 75 6d   be a column num
4429d 62 65 72 2c 20 6f 72 20 74 68 65 0d 0a 20 20 20  ber, or the..   
4429e 20 20 20 20 20 2a 2a 20 73 74 61 72 74 20 6f 66       ** start of
4429f 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74   a position list
442a0 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 2e 20 20   for column 0.  
442a1 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 0d 0a 20  ..        ** .. 
442a2 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6f 6e         ** The on
442a3 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  ly difference be
442a4 74 77 65 65 6e 20 73 74 61 74 65 20 31 20 61 6e  tween state 1 an
442a5 64 20 73 74 61 74 65 20 32 20 69 73 20 74 68 61  d state 2 is tha
442a6 74 20 69 66 20 74 68 65 0d 0a 20 20 20 20 20 20  t if the..      
442a7 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 65 6e 63    ** integer enc
442a8 6f 75 6e 74 65 72 65 64 20 69 6e 20 73 74 61 74  ountered in stat
442a9 65 20 31 20 69 73 20 6e 6f 74 20 30 20 6f 72 20  e 1 is not 0 or 
442aa 31 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64 20  1, then we need 
442ab 74 6f 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  to..        ** i
442ac 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 6c  ncrement the col
442ad 75 6d 6e 20 30 20 22 6e 44 6f 63 22 20 63 6f 75  umn 0 "nDoc" cou
442ae 6e 74 20 66 6f 72 20 74 68 69 73 20 74 65 72 6d  nt for this term
442af 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20  ...        */.. 
442b0 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 0d 0a         case 1:..
442b1 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
442b2 28 20 69 43 6f 6c 3d 3d 30 20 29 3b 0d 0a 20 20  ( iCol==0 );..  
442b3 20 20 20 20 20 20 20 20 69 66 28 20 76 3e 31 20          if( v>1 
442b4 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ){..            
442b5 70 43 73 72 2d 3e 61 53 74 61 74 5b 31 5d 2e 6e  pCsr->aStat[1].n
442b6 44 6f 63 2b 2b 3b 0d 0a 20 20 20 20 20 20 20 20  Doc++;..        
442b7 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 65    }..          e
442b8 53 74 61 74 65 20 3d 20 32 3b 0d 0a 20 20 20 20  State = 2;..    
442b9 20 20 20 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68        /* fall th
442ba 72 6f 75 67 68 20 2a 2f 0d 0a 0d 0a 20 20 20 20  rough */....    
442bb 20 20 20 20 63 61 73 65 20 32 3a 0d 0a 20 20 20      case 2:..   
442bc 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30 20         if( v==0 
442bd 29 7b 20 20 20 20 20 20 20 2f 2a 20 30 78 30 30  ){       /* 0x00
442be 2e 20 4e 65 78 74 20 69 6e 74 65 67 65 72 20 77  . Next integer w
442bf 69 6c 6c 20 62 65 20 61 20 64 6f 63 69 64 2e 20  ill be a docid. 
442c0 2a 2f 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
442c1 65 53 74 61 74 65 20 3d 20 30 3b 0d 0a 20 20 20  eState = 0;..   
442c2 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
442c3 20 76 3d 3d 31 20 29 7b 20 2f 2a 20 30 78 30 31   v==1 ){ /* 0x01
442c4 2e 20 4e 65 78 74 20 69 6e 74 65 67 65 72 20 77  . Next integer w
442c5 69 6c 6c 20 62 65 20 61 20 63 6f 6c 75 6d 6e 20  ill be a column 
442c6 6e 75 6d 62 65 72 2e 20 2a 2f 0d 0a 20 20 20 20  number. */..    
442c7 20 20 20 20 20 20 20 20 65 53 74 61 74 65 20 3d          eState =
442c8 20 33 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d   3;..          }
442c9 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
442ca 20 2f 2a 20 32 20 6f 72 20 67 72 65 61 74 65 72   /* 2 or greater
442cb 2e 20 41 20 70 6f 73 69 74 69 6f 6e 2e 20 2a 2f  . A position. */
442cc 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43  ..            pC
442cd 73 72 2d 3e 61 53 74 61 74 5b 69 43 6f 6c 2b 31  sr->aStat[iCol+1
442ce 5d 2e 6e 4f 63 63 2b 2b 3b 0d 0a 20 20 20 20 20  ].nOcc++;..     
442cf 20 20 20 20 20 20 20 70 43 73 72 2d 3e 61 53 74         pCsr->aSt
442d0 61 74 5b 30 5d 2e 6e 4f 63 63 2b 2b 3b 0d 0a 20  at[0].nOcc++;.. 
442d1 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
442d2 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a        break;....
442d3 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65          /* State
442d4 20 33 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20   3. The integer 
442d5 6a 75 73 74 20 72 65 61 64 20 69 73 20 61 20 63  just read is a c
442d6 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 2a 2f  olumn number. */
442d7 0d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c  ..        defaul
442d8 74 3a 20 61 73 73 65 72 74 28 20 65 53 74 61 74  t: assert( eStat
442d9 65 3d 3d 33 20 29 3b 0d 0a 20 20 20 20 20 20 20  e==3 );..       
442da 20 20 20 69 43 6f 6c 20 3d 20 28 69 6e 74 29 76     iCol = (int)v
442db 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
442dc 20 66 74 73 33 61 75 78 47 72 6f 77 53 74 61 74   fts3auxGrowStat
442dd 41 72 72 61 79 28 70 43 73 72 2c 20 69 43 6f 6c  Array(pCsr, iCol
442de 2b 32 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  +2) ) return SQL
442df 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
442e0 20 20 20 20 20 20 70 43 73 72 2d 3e 61 53 74 61        pCsr->aSta
442e1 74 5b 69 43 6f 6c 2b 31 5d 2e 6e 44 6f 63 2b 2b  t[iCol+1].nDoc++
442e2 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 65 53 74  ;..          eSt
442e3 61 74 65 20 3d 20 32 3b 0d 0a 20 20 20 20 20 20  ate = 2;..      
442e4 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
442e5 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20    }..    }....  
442e6 20 20 70 43 73 72 2d 3e 69 43 6f 6c 20 3d 20 30    pCsr->iCol = 0
442e7 3b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ;..    rc = SQLI
442e8 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65 7b  TE_OK;..  }else{
442e9 0d 0a 20 20 20 20 70 43 73 72 2d 3e 69 73 45 6f  ..    pCsr->isEo
442ea 66 20 3d 20 31 3b 0d 0a 20 20 7d 0d 0a 20 20 72  f = 1;..  }..  r
442eb 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
442ec 2f 2a 0d 0a 2a 2a 20 78 46 69 6c 74 65 72 20 2d  /*..** xFilter -
442ed 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 63 75   Initialize a cu
442ee 72 73 6f 72 20 74 6f 20 70 6f 69 6e 74 20 61 74  rsor to point at
442ef 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74   the start of it
442f0 73 20 64 61 74 61 2e 0d 0a 2a 2f 0d 0a 73 74 61  s data...*/..sta
442f1 74 69 63 20 69 6e 74 20 66 74 73 33 61 75 78 46  tic int fts3auxF
442f2 69 6c 74 65 72 4d 65 74 68 6f 64 28 0d 0a 20 20  ilterMethod(..  
442f3 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
442f4 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 20  sor *pCursor,   
442f5 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 75 73  /* The cursor us
442f6 65 64 20 66 6f 72 20 74 68 69 73 20 71 75 65 72  ed for this quer
442f7 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 64 78 4e  y */..  int idxN
442f8 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
442f9 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 61 74          /* Strat
442fa 65 67 79 20 69 6e 64 65 78 20 2a 2f 0d 0a 20 20  egy index */..  
442fb 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53  const char *idxS
442fc 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
442fd 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0d 0a 20 20  /* Unused */..  
442fe 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20  int nVal,       
442ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44300 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
44301 6d 65 6e 74 73 20 69 6e 20 61 70 56 61 6c 20 2a  ments in apVal *
44302 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  /..  sqlite3_val
44303 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20  ue **apVal      
44304 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
44305 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69  s for the indexi
44306 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0d 0a 29 7b  ng scheme */..){
44307 0d 0a 20 20 46 74 73 33 61 75 78 43 75 72 73 6f  ..  Fts3auxCurso
44308 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33 61  r *pCsr = (Fts3a
44309 75 78 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73  uxCursor *)pCurs
4430a 6f 72 3b 0d 0a 20 20 46 74 73 33 54 61 62 6c 65  or;..  Fts3Table
4430b 20 2a 70 46 74 73 33 20 3d 20 28 28 46 74 73 33   *pFts3 = ((Fts3
4430c 61 75 78 54 61 62 6c 65 20 2a 29 70 43 75 72 73  auxTable *)pCurs
4430d 6f 72 2d 3e 70 56 74 61 62 29 2d 3e 70 46 74 73  or->pVtab)->pFts
4430e 33 54 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63 3b  3Tab;..  int rc;
4430f 0d 0a 20 20 69 6e 74 20 69 73 53 63 61 6e 3b 0d  ..  int isScan;.
44310 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ...  UNUSED_PARA
44311 4d 45 54 45 52 28 6e 56 61 6c 29 3b 0d 0a 20 20  METER(nVal);..  
44312 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
44313 28 69 64 78 53 74 72 29 3b 0d 0a 0d 0a 20 20 61  (idxStr);....  a
44314 73 73 65 72 74 28 20 69 64 78 53 74 72 3d 3d 30  ssert( idxStr==0
44315 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69   );..  assert( i
44316 64 78 4e 75 6d 3d 3d 46 54 53 34 41 55 58 5f 45  dxNum==FTS4AUX_E
44317 51 5f 43 4f 4e 53 54 52 41 49 4e 54 20 7c 7c 20  Q_CONSTRAINT || 
44318 69 64 78 4e 75 6d 3d 3d 30 0d 0a 20 20 20 20 20  idxNum==0..     
44319 20 20 7c 7c 20 69 64 78 4e 75 6d 3d 3d 46 54 53    || idxNum==FTS
4431a 34 41 55 58 5f 4c 45 5f 43 4f 4e 53 54 52 41 49  4AUX_LE_CONSTRAI
4431b 4e 54 20 7c 7c 20 69 64 78 4e 75 6d 3d 3d 46 54  NT || idxNum==FT
4431c 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41  S4AUX_GE_CONSTRA
4431d 49 4e 54 0d 0a 20 20 20 20 20 20 20 7c 7c 20 69  INT..       || i
4431e 64 78 4e 75 6d 3d 3d 28 46 54 53 34 41 55 58 5f  dxNum==(FTS4AUX_
4431f 4c 45 5f 43 4f 4e 53 54 52 41 49 4e 54 7c 46 54  LE_CONSTRAINT|FT
44320 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41  S4AUX_GE_CONSTRA
44321 49 4e 54 29 0d 0a 20 20 29 3b 0d 0a 20 20 69 73  INT)..  );..  is
44322 53 63 61 6e 20 3d 20 28 69 64 78 4e 75 6d 21 3d  Scan = (idxNum!=
44323 46 54 53 34 41 55 58 5f 45 51 5f 43 4f 4e 53 54  FTS4AUX_EQ_CONST
44324 52 41 49 4e 54 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  RAINT);....  /* 
44325 49 6e 20 63 61 73 65 20 74 68 69 73 20 63 75 72  In case this cur
44326 73 6f 72 20 69 73 20 62 65 69 6e 67 20 72 65 75  sor is being reu
44327 73 65 64 2c 20 63 6c 6f 73 65 20 61 6e 64 20 7a  sed, close and z
44328 65 72 6f 20 69 74 2e 20 2a 2f 0d 0a 20 20 74 65  ero it. */..  te
44329 73 74 63 61 73 65 28 70 43 73 72 2d 3e 66 69 6c  stcase(pCsr->fil
4432a 74 65 72 2e 7a 54 65 72 6d 29 3b 0d 0a 20 20 73  ter.zTerm);..  s
4432b 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
4432c 64 65 72 46 69 6e 69 73 68 28 26 70 43 73 72 2d  derFinish(&pCsr-
4432d 3e 63 73 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65  >csr);..  sqlite
4432e 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29 70  3_free((void *)p
4432f 43 73 72 2d 3e 66 69 6c 74 65 72 2e 7a 54 65 72  Csr->filter.zTer
44330 6d 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  m);..  sqlite3_f
44331 72 65 65 28 70 43 73 72 2d 3e 61 53 74 61 74 29  ree(pCsr->aStat)
44332 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26 70 43 73  ;..  memset(&pCs
44333 72 2d 3e 63 73 72 2c 20 30 2c 20 28 28 75 38 2a  r->csr, 0, ((u8*
44334 29 26 70 43 73 72 5b 31 5d 29 20 2d 20 28 75 38  )&pCsr[1]) - (u8
44335 2a 29 26 70 43 73 72 2d 3e 63 73 72 29 3b 0d 0a  *)&pCsr->csr);..
44336 0d 0a 20 20 70 43 73 72 2d 3e 66 69 6c 74 65 72  ..  pCsr->filter
44337 2e 66 6c 61 67 73 20 3d 20 46 54 53 33 5f 53 45  .flags = FTS3_SE
44338 47 4d 45 4e 54 5f 52 45 51 55 49 52 45 5f 50 4f  GMENT_REQUIRE_PO
44339 53 7c 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 49  S|FTS3_SEGMENT_I
4433a 47 4e 4f 52 45 5f 45 4d 50 54 59 3b 0d 0a 20 20  GNORE_EMPTY;..  
4433b 69 66 28 20 69 73 53 63 61 6e 20 29 20 70 43 73  if( isScan ) pCs
4433c 72 2d 3e 66 69 6c 74 65 72 2e 66 6c 61 67 73 20  r->filter.flags 
4433d 7c 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f  |= FTS3_SEGMENT_
4433e 53 43 41 4e 3b 0d 0a 0d 0a 20 20 69 66 28 20 69  SCAN;....  if( i
4433f 64 78 4e 75 6d 26 28 46 54 53 34 41 55 58 5f 45  dxNum&(FTS4AUX_E
44340 51 5f 43 4f 4e 53 54 52 41 49 4e 54 7c 46 54 53  Q_CONSTRAINT|FTS
44341 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41 49  4AUX_GE_CONSTRAI
44342 4e 54 29 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73  NT) ){..    cons
44343 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
44344 2a 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f  *zStr = sqlite3_
44345 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c  value_text(apVal
44346 5b 30 5d 29 3b 0d 0a 20 20 20 20 69 66 28 20 7a  [0]);..    if( z
44347 53 74 72 20 29 7b 0d 0a 20 20 20 20 20 20 70 43  Str ){..      pC
44348 73 72 2d 3e 66 69 6c 74 65 72 2e 7a 54 65 72 6d  sr->filter.zTerm
44349 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
4434a 74 66 28 22 25 73 22 2c 20 7a 53 74 72 29 3b 0d  tf("%s", zStr);.
4434b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 66 69 6c  .      pCsr->fil
4434c 74 65 72 2e 6e 54 65 72 6d 20 3d 20 73 71 6c 69  ter.nTerm = sqli
4434d 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
4434e 61 70 56 61 6c 5b 30 5d 29 3b 0d 0a 20 20 20 20  apVal[0]);..    
4434f 20 20 69 66 28 20 70 43 73 72 2d 3e 66 69 6c 74    if( pCsr->filt
44350 65 72 2e 7a 54 65 72 6d 3d 3d 30 20 29 20 72 65  er.zTerm==0 ) re
44351 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
44352 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  M;..    }..  }..
44353 20 20 69 66 28 20 69 64 78 4e 75 6d 26 46 54 53    if( idxNum&FTS
44354 34 41 55 58 5f 4c 45 5f 43 4f 4e 53 54 52 41 49  4AUX_LE_CONSTRAI
44355 4e 54 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69  NT ){..    int i
44356 49 64 78 20 3d 20 28 69 64 78 4e 75 6d 26 46 54  Idx = (idxNum&FT
44357 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41  S4AUX_GE_CONSTRA
44358 49 4e 54 29 20 3f 20 31 20 3a 20 30 3b 0d 0a 20  INT) ? 1 : 0;.. 
44359 20 20 20 70 43 73 72 2d 3e 7a 53 74 6f 70 20 3d     pCsr->zStop =
4435a 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
4435b 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76  ("%s", sqlite3_v
4435c 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b  alue_text(apVal[
4435d 69 49 64 78 5d 29 29 3b 0d 0a 20 20 20 20 70 43  iIdx]));..    pC
4435e 73 72 2d 3e 6e 53 74 6f 70 20 3d 20 73 71 6c 69  sr->nStop = sqli
4435f 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
44360 61 70 56 61 6c 5b 69 49 64 78 5d 29 3b 0d 0a 20  apVal[iIdx]);.. 
44361 20 20 20 69 66 28 20 70 43 73 72 2d 3e 7a 53 74     if( pCsr->zSt
44362 6f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  op==0 ) return S
44363 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
44364 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69  }....  rc = sqli
44365 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
44366 43 75 72 73 6f 72 28 70 46 74 73 33 2c 20 30 2c  Cursor(pFts3, 0,
44367 20 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f   FTS3_SEGCURSOR_
44368 41 4c 4c 2c 0d 0a 20 20 20 20 20 20 70 43 73 72  ALL,..      pCsr
44369 2d 3e 66 69 6c 74 65 72 2e 7a 54 65 72 6d 2c 20  ->filter.zTerm, 
4436a 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e 6e 54 65  pCsr->filter.nTe
4436b 72 6d 2c 20 30 2c 20 69 73 53 63 61 6e 2c 20 26  rm, 0, isScan, &
4436c 70 43 73 72 2d 3e 63 73 72 0d 0a 20 20 29 3b 0d  pCsr->csr..  );.
4436d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
4436e 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20  E_OK ){..    rc 
4436f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67  = sqlite3Fts3Seg
44370 52 65 61 64 65 72 53 74 61 72 74 28 70 46 74 73  ReaderStart(pFts
44371 33 2c 20 26 70 43 73 72 2d 3e 63 73 72 2c 20 26  3, &pCsr->csr, &
44372 70 43 73 72 2d 3e 66 69 6c 74 65 72 29 3b 0d 0a  pCsr->filter);..
44373 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d    }....  if( rc=
44374 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
44375 3d 20 66 74 73 33 61 75 78 4e 65 78 74 4d 65 74  = fts3auxNextMet
44376 68 6f 64 28 70 43 75 72 73 6f 72 29 3b 0d 0a 20  hod(pCursor);.. 
44377 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
44378 0d 0a 2f 2a 0d 0a 2a 2a 20 78 45 6f 66 20 2d 20  ../*..** xEof - 
44379 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
4437a 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 74 20  he cursor is at 
4437b 45 4f 46 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74  EOF, or false ot
4437c 68 65 72 77 69 73 65 2e 0d 0a 2a 2f 0d 0a 73 74  herwise...*/..st
4437d 61 74 69 63 20 69 6e 74 20 66 74 73 33 61 75 78  atic int fts3aux
4437e 45 6f 66 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  EofMethod(sqlite
4437f 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
44380 43 75 72 73 6f 72 29 7b 0d 0a 20 20 46 74 73 33  Cursor){..  Fts3
44381 61 75 78 43 75 72 73 6f 72 20 2a 70 43 73 72 20  auxCursor *pCsr 
44382 3d 20 28 46 74 73 33 61 75 78 43 75 72 73 6f 72  = (Fts3auxCursor
44383 20 2a 29 70 43 75 72 73 6f 72 3b 0d 0a 20 20 72   *)pCursor;..  r
44384 65 74 75 72 6e 20 70 43 73 72 2d 3e 69 73 45 6f  eturn pCsr->isEo
44385 66 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  f;..}..../*..** 
44386 78 43 6f 6c 75 6d 6e 20 2d 20 52 65 74 75 72 6e  xColumn - Return
44387 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2e   a column value.
44388 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
44389 20 66 74 73 33 61 75 78 43 6f 6c 75 6d 6e 4d 65   fts3auxColumnMe
4438a 74 68 6f 64 28 0d 0a 20 20 73 71 6c 69 74 65 33  thod(..  sqlite3
4438b 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43  _vtab_cursor *pC
4438c 75 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75 72 73  ursor,   /* Curs
4438d 6f 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 76  or to retrieve v
4438e 61 6c 75 65 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20  alue from */..  
4438f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
44390 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20  *pContext,      
44391 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 73  /* Context for s
44392 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 78 78  qlite3_result_xx
44393 78 28 29 20 63 61 6c 6c 73 20 2a 2f 0d 0a 20 20  x() calls */..  
44394 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20 20 20  int iCol        
44395 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44396 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
44397 6d 6e 20 74 6f 20 72 65 61 64 20 76 61 6c 75 65  mn to read value
44398 20 66 72 6f 6d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   from */..){..  
44399 46 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70  Fts3auxCursor *p
4439a 20 3d 20 28 46 74 73 33 61 75 78 43 75 72 73 6f   = (Fts3auxCurso
4439b 72 20 2a 29 70 43 75 72 73 6f 72 3b 0d 0a 0d 0a  r *)pCursor;....
4439c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 45    assert( p->isE
4439d 6f 66 3d 3d 30 20 29 3b 0d 0a 20 20 69 66 28 20  of==0 );..  if( 
4439e 69 43 6f 6c 3d 3d 30 20 29 7b 20 20 20 20 20 20  iCol==0 ){      
4439f 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 22 74 65 72    /* Column "ter
443a0 6d 22 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74  m" */..    sqlit
443a1 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
443a2 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 63 73 72 2e  Context, p->csr.
443a3 7a 54 65 72 6d 2c 20 70 2d 3e 63 73 72 2e 6e 54  zTerm, p->csr.nT
443a4 65 72 6d 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  erm, SQLITE_TRAN
443a5 53 49 45 4e 54 29 3b 0d 0a 20 20 7d 65 6c 73 65  SIENT);..  }else
443a6 20 69 66 28 20 69 43 6f 6c 3d 3d 31 20 29 7b 20   if( iCol==1 ){ 
443a7 20 2f 2a 20 43 6f 6c 75 6d 6e 20 22 63 6f 6c 22   /* Column "col"
443a8 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e   */..    if( p->
443a9 69 43 6f 6c 20 29 7b 0d 0a 20 20 20 20 20 20 73  iCol ){..      s
443aa 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
443ab 74 28 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 69  t(pContext, p->i
443ac 43 6f 6c 2d 31 29 3b 0d 0a 20 20 20 20 7d 65 6c  Col-1);..    }el
443ad 73 65 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  se{..      sqlit
443ae 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
443af 43 6f 6e 74 65 78 74 2c 20 22 2a 22 2c 20 2d 31  Context, "*", -1
443b0 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
443b1 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
443b2 65 20 69 66 28 20 69 43 6f 6c 3d 3d 32 20 29 7b  e if( iCol==2 ){
443b3 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 22 64 6f 63    /* Column "doc
443b4 75 6d 65 6e 74 73 22 20 2a 2f 0d 0a 20 20 20 20  uments" */..    
443b5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
443b6 6e 74 36 34 28 70 43 6f 6e 74 65 78 74 2c 20 70  nt64(pContext, p
443b7 2d 3e 61 53 74 61 74 5b 70 2d 3e 69 43 6f 6c 5d  ->aStat[p->iCol]
443b8 2e 6e 44 6f 63 29 3b 0d 0a 20 20 7d 65 6c 73 65  .nDoc);..  }else
443b9 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
443ba 20 2f 2a 20 43 6f 6c 75 6d 6e 20 22 6f 63 63 75   /* Column "occu
443bb 72 72 65 6e 63 65 73 22 20 2a 2f 0d 0a 20 20 20  rrences" */..   
443bc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
443bd 69 6e 74 36 34 28 70 43 6f 6e 74 65 78 74 2c 20  int64(pContext, 
443be 70 2d 3e 61 53 74 61 74 5b 70 2d 3e 69 43 6f 6c  p->aStat[p->iCol
443bf 5d 2e 6e 4f 63 63 29 3b 0d 0a 20 20 7d 0d 0a 0d  ].nOcc);..  }...
443c0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
443c1 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
443c2 2a 20 78 52 6f 77 69 64 20 2d 20 52 65 74 75 72  * xRowid - Retur
443c3 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  n the current ro
443c4 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72 73  wid for the curs
443c5 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  or...*/..static 
443c6 69 6e 74 20 66 74 73 33 61 75 78 52 6f 77 69 64  int fts3auxRowid
443c7 4d 65 74 68 6f 64 28 0d 0a 20 20 73 71 6c 69 74  Method(..  sqlit
443c8 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
443c9 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75  pCursor,   /* Cu
443ca 72 73 6f 72 20 74 6f 20 72 65 74 72 69 65 76 65  rsor to retrieve
443cb 20 76 61 6c 75 65 20 66 72 6f 6d 20 2a 2f 0d 0a   value from */..
443cc 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a    sqlite_int64 *
443cd 70 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  pRowid          
443ce 20 20 2f 2a 20 4f 55 54 3a 20 52 6f 77 69 64 20    /* OUT: Rowid 
443cf 76 61 6c 75 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  value */..){..  
443d0 46 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70  Fts3auxCursor *p
443d1 43 73 72 20 3d 20 28 46 74 73 33 61 75 78 43 75  Csr = (Fts3auxCu
443d2 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0d  rsor *)pCursor;.
443d3 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 43 73  .  *pRowid = pCs
443d4 72 2d 3e 69 52 6f 77 69 64 3b 0d 0a 20 20 72 65  r->iRowid;..  re
443d5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
443d6 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 67  .}..../*..** Reg
443d7 69 73 74 65 72 20 74 68 65 20 66 74 73 33 61 75  ister the fts3au
443d8 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 64 61  x module with da
443d9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
443da 6e 20 64 62 2e 20 52 65 74 75 72 6e 20 53 51 4c  n db. Return SQL
443db 49 54 45 5f 4f 4b 0d 0a 2a 2a 20 69 66 20 73 75  ITE_OK..** if su
443dc 63 63 65 73 73 66 75 6c 20 6f 72 20 61 6e 20 65  ccessful or an e
443dd 72 72 6f 72 20 63 6f 64 65 20 69 66 20 73 71 6c  rror code if sql
443de 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
443df 6c 65 28 29 20 66 61 69 6c 73 2e 0d 0a 2a 2f 0d  le() fails...*/.
443e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
443e1 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
443e2 6e 69 74 41 75 78 28 73 71 6c 69 74 65 33 20 2a  nitAux(sqlite3 *
443e3 64 62 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63  db){..  static c
443e4 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
443e5 75 6c 65 20 66 74 73 33 61 75 78 5f 6d 6f 64 75  ule fts3aux_modu
443e6 6c 65 20 3d 20 7b 0d 0a 20 20 20 20 20 30 2c 20  le = {..     0, 
443e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
443e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
443e9 72 73 69 6f 6e 20 20 20 20 20 20 2a 2f 0d 0a 20  rsion      */.. 
443ea 20 20 20 20 66 74 73 33 61 75 78 43 6f 6e 6e 65      fts3auxConne
443eb 63 74 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 20  ctMethod,       
443ec 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20 20   /* xCreate     
443ed 20 20 2a 2f 0d 0a 20 20 20 20 20 66 74 73 33 61    */..     fts3a
443ee 75 78 43 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 2c  uxConnectMethod,
443ef 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6e 6e          /* xConn
443f0 65 63 74 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ect      */..   
443f1 20 20 66 74 73 33 61 75 78 42 65 73 74 49 6e 64    fts3auxBestInd
443f2 65 78 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 2f  exMethod,      /
443f3 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20  * xBestIndex    
443f4 2a 2f 0d 0a 20 20 20 20 20 66 74 73 33 61 75 78  */..     fts3aux
443f5 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64  DisconnectMethod
443f6 2c 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f 6e  ,     /* xDiscon
443f7 6e 65 63 74 20 20 20 2a 2f 0d 0a 20 20 20 20 20  nect   */..     
443f8 66 74 73 33 61 75 78 44 69 73 63 6f 6e 6e 65 63  fts3auxDisconnec
443f9 74 4d 65 74 68 6f 64 2c 20 20 20 20 20 2f 2a 20  tMethod,     /* 
443fa 78 44 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f  xDestroy      */
443fb 0d 0a 20 20 20 20 20 66 74 73 33 61 75 78 4f 70  ..     fts3auxOp
443fc 65 6e 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 20  enMethod,       
443fd 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20      /* xOpen    
443fe 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 66 74       */..     ft
443ff 73 33 61 75 78 43 6c 6f 73 65 4d 65 74 68 6f 64  s3auxCloseMethod
44400 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43  ,          /* xC
44401 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f 0d 0a  lose        */..
44402 20 20 20 20 20 66 74 73 33 61 75 78 46 69 6c 74       fts3auxFilt
44403 65 72 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 20  erMethod,       
44404 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20    /* xFilter    
44405 20 20 20 2a 2f 0d 0a 20 20 20 20 20 66 74 73 33     */..     fts3
44406 61 75 78 4e 65 78 74 4d 65 74 68 6f 64 2c 20 20  auxNextMethod,  
44407 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65 78           /* xNex
44408 74 20 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20  t         */..  
44409 20 20 20 66 74 73 33 61 75 78 45 6f 66 4d 65 74     fts3auxEofMet
4440a 68 6f 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  hod,            
4440b 2f 2a 20 78 45 6f 66 20 20 20 20 20 20 20 20 20  /* xEof         
4440c 20 2a 2f 0d 0a 20 20 20 20 20 66 74 73 33 61 75   */..     fts3au
4440d 78 43 6f 6c 75 6d 6e 4d 65 74 68 6f 64 2c 20 20  xColumnMethod,  
4440e 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6c 75 6d         /* xColum
4440f 6e 20 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20  n       */..    
44410 20 66 74 73 33 61 75 78 52 6f 77 69 64 4d 65 74   fts3auxRowidMet
44411 68 6f 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  hod,          /*
44412 20 78 52 6f 77 69 64 20 20 20 20 20 20 20 20 2a   xRowid        *
44413 2f 0d 0a 20 20 20 20 20 30 2c 20 20 20 20 20 20  /..     0,      
44414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44415 20 20 20 20 20 2f 2a 20 78 55 70 64 61 74 65 20       /* xUpdate 
44416 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 30        */..     0
44417 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
44418 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
44419 42 65 67 69 6e 20 20 20 20 20 20 20 20 2a 2f 0d  Begin        */.
4441a 0a 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  .     0,        
4441b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4441c 20 20 20 2f 2a 20 78 53 79 6e 63 20 20 20 20 20     /* xSync     
4441d 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 30 2c 20      */..     0, 
4441e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4441f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f            /* xCo
44420 6d 6d 69 74 20 20 20 20 20 20 20 2a 2f 0d 0a 20  mmit       */.. 
44421 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
44422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44423 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20   /* xRollback   
44424 20 20 2a 2f 0d 0a 20 20 20 20 20 30 2c 20 20 20    */..     0,   
44425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44426 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64          /* xFind
44427 46 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20  Function */..   
44428 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
44429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4442a 2a 20 78 52 65 6e 61 6d 65 20 20 20 20 20 20 20  * xRename       
4442b 2a 2f 0d 0a 20 20 20 20 20 30 2c 20 20 20 20 20  */..     0,     
4442c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4442d 20 20 20 20 20 20 2f 2a 20 78 53 61 76 65 70 6f        /* xSavepo
4442e 69 6e 74 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  int    */..     
4442f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
44430 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44431 78 52 65 6c 65 61 73 65 20 20 20 20 20 20 2a 2f  xRelease      */
44432 0d 0a 20 20 20 20 20 30 20 20 20 20 20 20 20 20  ..     0        
44433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44434 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b      /* xRollback
44435 54 6f 20 20 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 20  To   */..  };.. 
44436 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
44437 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44438 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
44439 2a 2f 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c  */....  rc = sql
4443a 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
4443b 6c 65 28 64 62 2c 20 22 66 74 73 34 61 75 78 22  le(db, "fts4aux"
4443c 2c 20 26 66 74 73 33 61 75 78 5f 6d 6f 64 75 6c  , &fts3aux_modul
4443d 65 2c 20 30 29 3b 0d 0a 20 20 72 65 74 75 72 6e  e, 0);..  return
4443e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64 69   rc;..}....#endi
4443f 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
44440 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
44441 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
44442 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0d 0a 0d 0a  BLE_FTS3) */....
44443 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
44444 45 6e 64 20 6f 66 20 66 74 73 33 5f 61 75 78 2e  End of fts3_aux.
44445 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
44446 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44447 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
44448 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
44449 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
4444a 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.c ********
4444b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4444c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4444d 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 38 20 4e 6f  ../*..** 2008 No
4444e 76 20 32 38 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  v 28..**..** The
4444f 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
44450 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
44451 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
44452 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a    In place of..*
44453 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
44454 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
44455 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  sing:..**..**   
44456 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
44457 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a   and not evil...
44458 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
44459 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
4445a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
4445b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d  forgive others..
4445c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
4445d 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4445e 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4445f 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a  han you give...*
44460 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
44461 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44462 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44463 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44464 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44465 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6d  *..**..** This m
44466 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63  odule contains c
44467 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ode that impleme
44468 6e 74 73 20 61 20 70 61 72 73 65 72 20 66 6f 72  nts a parser for
44469 20 66 74 73 33 20 71 75 65 72 79 20 73 74 72 69   fts3 query stri
4446a 6e 67 73 0d 0a 2a 2a 20 28 74 68 65 20 72 69 67  ngs..** (the rig
4446b 68 74 2d 68 61 6e 64 20 61 72 67 75 6d 65 6e 74  ht-hand argument
4446c 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f 70   to the MATCH op
4446d 65 72 61 74 6f 72 29 2e 20 42 65 63 61 75 73 65  erator). Because
4446e 20 74 68 65 20 73 75 70 70 6f 72 74 65 64 20 0d   the supported .
4446f 0a 2a 2a 20 73 79 6e 74 61 78 20 69 73 20 72 65  .** syntax is re
44470 6c 61 74 69 76 65 6c 79 20 73 69 6d 70 6c 65 2c  latively simple,
44471 20 74 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65 6e   the whole token
44472 69 7a 65 72 2f 70 61 72 73 65 72 20 73 79 73 74  izer/parser syst
44473 65 6d 20 69 73 0d 0a 2a 2a 20 68 61 6e 64 2d 63  em is..** hand-c
44474 6f 64 65 64 2e 20 0d 0a 2a 2f 0d 0a 23 69 66 20  oded. ..*/..#if 
44475 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
44476 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
44477 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
44478 54 53 33 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 42  TS3)..../*..** B
44479 79 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20  y default, this 
4447a 6d 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68  module parses th
4447b 65 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20  e legacy syntax 
4447c 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 0d 0a  that has been ..
4447d 2a 2a 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79  ** traditionally
4447e 20 75 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f   used by fts3. O
4447f 72 2c 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  r, if SQLITE_ENA
44480 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48  BLE_FTS3_PARENTH
44481 45 53 49 53 0d 0a 2a 2a 20 69 73 20 64 65 66 69  ESIS..** is defi
44482 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 75 73 65  ned, then it use
44483 73 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78  s the new syntax
44484 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  . The difference
44485 73 20 62 65 74 77 65 65 6e 0d 0a 2a 2a 20 74 68  s between..** th
44486 65 20 6e 65 77 20 61 6e 64 20 74 68 65 20 6f 6c  e new and the ol
44487 64 20 73 79 6e 74 61 78 65 73 20 61 72 65 3a 0d  d syntaxes are:.
44488 0a 2a 2a 0d 0a 2a 2a 20 20 61 29 20 54 68 65 20  .**..**  a) The 
44489 6e 65 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f  new syntax suppo
4448a 72 74 73 20 70 61 72 65 6e 74 68 65 73 69 73 2e  rts parenthesis.
4448b 20 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f   The old does no
4448c 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 62 29 20 54  t...**..**  b) T
4448d 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 73 75  he new syntax su
4448e 70 70 6f 72 74 73 20 74 68 65 20 41 4e 44 20 61  pports the AND a
4448f 6e 64 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 73  nd NOT operators
44490 2e 20 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e  . The old does n
44491 6f 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 63 29 20  ot...**..**  c) 
44492 54 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 20 73  The old syntax s
44493 75 70 70 6f 72 74 73 20 74 68 65 20 22 2d 22 20  upports the "-" 
44494 74 6f 6b 65 6e 20 71 75 61 6c 69 66 69 65 72 2e  token qualifier.
44495 20 54 68 69 73 20 69 73 20 6e 6f 74 20 0d 0a 2a   This is not ..*
44496 2a 20 20 20 20 20 73 75 70 70 6f 72 74 65 64 20  *     supported 
44497 62 79 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61  by the new synta
44498 78 20 28 69 74 20 69 73 20 72 65 70 6c 61 63 65  x (it is replace
44499 64 20 62 79 20 74 68 65 20 4e 4f 54 20 6f 70 65  d by the NOT ope
4449a 72 61 74 6f 72 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  rator)...**..** 
4449b 20 64 29 20 57 68 65 6e 20 75 73 69 6e 67 20 74   d) When using t
4449c 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 2c 20 74  he old syntax, t
4449d 68 65 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68  he OR operator h
4449e 61 73 20 61 20 67 72 65 61 74 65 72 20 70 72 65  as a greater pre
4449f 63 65 64 65 6e 63 65 0d 0a 2a 2a 20 20 20 20 20  cedence..**     
444a0 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63 69 74  than an implicit
444a1 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69 6e 67   AND. When using
444a2 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68 20 69   the new, both i
444a3 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65 78 70  mplicity and exp
444a4 6c 69 63 69 74 0d 0a 2a 2a 20 20 20 20 20 41 4e  licit..**     AN
444a5 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76 65  D operators have
444a6 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
444a7 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0d 0a 2a  ence than OR...*
444a8 2a 0d 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65  *..** If compile
444a9 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45  d with SQLITE_TE
444aa 53 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  ST defined, then
444ab 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70   this module exp
444ac 6f 72 74 73 20 74 68 65 0d 0a 2a 2a 20 73 79 6d  orts the..** sym
444ad 62 6f 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33  bol "int sqlite3
444ae 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
444af 65 6e 74 68 65 73 65 73 22 2e 20 53 65 74 74 69  entheses". Setti
444b0 6e 67 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  ng this variable
444b1 0d 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75  ..** to zero cau
444b2 73 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20 74  ses the module t
444b3 6f 20 75 73 65 20 74 68 65 20 6f 6c 64 20 73 79  o use the old sy
444b4 6e 74 61 78 2e 20 49 66 20 69 74 20 69 73 20 73  ntax. If it is s
444b5 65 74 20 74 6f 20 0d 0a 2a 2a 20 6e 6f 6e 2d 7a  et to ..** non-z
444b6 65 72 6f 20 74 68 65 20 6e 65 77 20 73 79 6e 74  ero the new synt
444b7 61 78 20 69 73 20 61 63 74 69 76 61 74 65 64 2e  ax is activated.
444b8 20 54 68 69 73 20 69 73 20 73 6f 20 62 6f 74 68   This is so both
444b9 20 73 79 6e 74 61 78 65 73 20 63 61 6e 0d 0a 2a   syntaxes can..*
444ba 2a 20 62 65 20 74 65 73 74 65 64 20 75 73 69 6e  * be tested usin
444bb 67 20 61 20 73 69 6e 67 6c 65 20 62 75 69 6c 64  g a single build
444bc 20 6f 66 20 74 65 73 74 66 69 78 74 75 72 65 2e   of testfixture.
444bd 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c  ..**..** The fol
444be 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65 73  lowing describes
444bf 20 74 68 65 20 73 79 6e 74 61 78 20 73 75 70 70   the syntax supp
444c0 6f 72 74 65 64 20 62 79 20 74 68 65 20 66 74 73  orted by the fts
444c1 33 20 4d 41 54 43 48 0d 0a 2a 2a 20 6f 70 65 72  3 MATCH..** oper
444c2 61 74 6f 72 20 69 6e 20 61 20 73 69 6d 69 6c 61  ator in a simila
444c3 72 20 66 6f 72 6d 61 74 20 74 6f 20 74 68 61 74  r format to that
444c4 20 75 73 65 64 20 62 79 20 74 68 65 20 6c 65 6d   used by the lem
444c5 6f 6e 20 70 61 72 73 65 72 0d 0a 2a 2a 20 67 65  on parser..** ge
444c6 6e 65 72 61 74 6f 72 2e 20 54 68 69 73 20 6d 6f  nerator. This mo
444c7 64 75 6c 65 20 64 6f 65 73 20 6e 6f 74 20 75 73  dule does not us
444c8 65 20 61 63 74 75 61 6c 6c 79 20 6c 65 6d 6f 6e  e actually lemon
444c9 2c 20 69 74 20 75 73 65 73 20 61 0d 0a 2a 2a 20  , it uses a..** 
444ca 63 75 73 74 6f 6d 20 70 61 72 73 65 72 2e 0d 0a  custom parser...
444cb 2a 2a 0d 0a 2a 2a 20 20 20 71 75 65 72 79 20 3a  **..**   query :
444cc 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52 20 61  := andexpr (OR a
444cd 6e 64 65 78 70 72 29 2a 2e 0d 0a 2a 2a 0d 0a 2a  ndexpr)*...**..*
444ce 2a 20 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20  *   andexpr ::= 
444cf 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f  notexpr (AND? no
444d0 74 65 78 70 72 29 2a 2e 0d 0a 2a 2a 0d 0a 2a 2a  texpr)*...**..**
444d1 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 6e     notexpr ::= n
444d2 65 61 72 65 78 70 72 20 28 4e 4f 54 20 6e 65 61  earexpr (NOT nea
444d3 72 65 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0d  rexpr|-TOKEN)*..
444d4 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a  .**   notexpr ::
444d5 3d 20 4c 50 20 71 75 65 72 79 20 52 50 2e 0d 0a  = LP query RP...
444d6 2a 2a 0d 0a 2a 2a 20 20 20 6e 65 61 72 65 78 70  **..**   nearexp
444d7 72 20 3a 3a 3d 20 70 68 72 61 73 65 20 28 4e 45  r ::= phrase (NE
444d8 41 52 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20  AR distance_opt 
444d9 6e 65 61 72 65 78 70 72 29 2a 2e 0d 0a 2a 2a 0d  nearexpr)*...**.
444da 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f 6f  .**   distance_o
444db 70 74 20 3a 3a 3d 20 2e 0d 0a 2a 2a 20 20 20 64  pt ::= ...**   d
444dc 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20  istance_opt ::= 
444dd 2f 20 49 4e 54 45 47 45 52 2e 0d 0a 2a 2a 0d 0a  / INTEGER...**..
444de 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20  **   phrase ::= 
444df 54 4f 4b 45 4e 2e 0d 0a 2a 2a 20 20 20 70 68 72  TOKEN...**   phr
444e0 61 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 3a 54  ase ::= COLUMN:T
444e1 4f 4b 45 4e 2e 0d 0a 2a 2a 20 20 20 70 68 72 61  OKEN...**   phra
444e2 73 65 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20 54 4f  se ::= "TOKEN TO
444e3 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e 0d 0a  KEN TOKEN..."...
444e4 2a 2f 0d 0a 0d 0a 23 69 66 64 65 66 20 53 51 4c  */....#ifdef SQL
444e5 49 54 45 5f 54 45 53 54 0d 0a 53 51 4c 49 54 45  ITE_TEST..SQLITE
444e6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
444e7 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
444e8 65 6e 74 68 65 73 65 73 20 3d 20 30 3b 0d 0a 23  entheses = 0;..#
444e9 65 6c 73 65 0d 0a 23 20 69 66 64 65 66 20 53 51  else..# ifdef SQ
444ea 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
444eb 5f 50 41 52 45 4e 54 48 45 53 49 53 20 0d 0a 23  _PARENTHESIS ..#
444ec 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
444ed 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
444ee 65 6e 74 68 65 73 65 73 20 31 0d 0a 23 20 65 6c  entheses 1..# el
444ef 73 65 0d 0a 23 20 20 64 65 66 69 6e 65 20 73 71  se..#  define sq
444f0 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
444f1 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 30 0d  e_parentheses 0.
444f2 0a 23 20 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66  .# endif..#endif
444f3 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 66 61 75  ..../*..** Defau
444f4 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e 45 41 52  lt span for NEAR
444f5 20 6f 70 65 72 61 74 6f 72 73 2e 0d 0a 2a 2f 0d   operators...*/.
444f6 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
444f7 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41  FTS3_DEFAULT_NEA
444f8 52 5f 50 41 52 41 4d 20 31 30 0d 0a 0d 0a 2f 2a  R_PARAM 10..../*
444f9 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e   #include <strin
444fa 67 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63  g.h> */../* #inc
444fb 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20  lude <assert.h> 
444fc 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 69 73 4e  */..../*..** isN
444fd 6f 74 3a 0d 0a 2a 2a 20 20 20 54 68 69 73 20 76  ot:..**   This v
444fe 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20  ariable is used 
444ff 62 79 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 4e  by function getN
44500 65 78 74 4e 6f 64 65 28 29 2e 20 57 68 65 6e 20  extNode(). When 
44501 67 65 74 4e 65 78 74 4e 6f 64 65 28 29 20 69 73  getNextNode() is
44502 0d 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2c 20 69  ..**   called, i
44503 74 20 73 65 74 73 20 50 61 72 73 65 43 6f 6e 74  t sets ParseCont
44504 65 78 74 2e 69 73 4e 6f 74 20 74 6f 20 74 72 75  ext.isNot to tru
44505 65 20 69 66 20 74 68 65 20 27 6e 65 78 74 20 6e  e if the 'next n
44506 6f 64 65 27 20 69 73 20 61 20 0d 0a 2a 2a 20 20  ode' is a ..**  
44507 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
44508 20 77 69 74 68 20 61 20 75 6e 61 72 79 20 22 2d   with a unary "-
44509 22 20 61 74 74 61 63 68 65 64 20 74 6f 20 69 74  " attached to it
4450a 2e 20 69 2e 65 2e 20 22 6d 79 73 71 6c 22 20 69  . i.e. "mysql" i
4450b 6e 20 74 68 65 0d 0a 2a 2a 20 20 20 46 54 53 33  n the..**   FTS3
4450c 20 71 75 65 72 79 20 22 73 71 6c 69 74 65 20 2d   query "sqlite -
4450d 6d 79 73 71 6c 22 2e 20 4f 74 68 65 72 77 69 73  mysql". Otherwis
4450e 65 2c 20 50 61 72 73 65 43 6f 6e 74 65 78 74 2e  e, ParseContext.
4450f 69 73 4e 6f 74 20 69 73 20 73 65 74 20 74 6f 0d  isNot is set to.
44510 0a 2a 2a 20 20 20 7a 65 72 6f 2e 0d 0a 2a 2f 0d  .**   zero...*/.
44511 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
44512 50 61 72 73 65 43 6f 6e 74 65 78 74 20 50 61 72  ParseContext Par
44513 73 65 43 6f 6e 74 65 78 74 3b 0d 0a 73 74 72 75  seContext;..stru
44514 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  ct ParseContext 
44515 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  {..  sqlite3_tok
44516 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
44517 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  er;      /* Toke
44518 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0d  nizer module */.
44519 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
4451a 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  azCol;          
4451b 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
4451c 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  of column names 
4451d 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a  for fts3 table *
4451e 2f 0d 0a 20 20 69 6e 74 20 62 46 74 73 34 3b 20  /..  int bFts4; 
4451f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44520 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
44521 20 74 6f 20 61 6c 6c 6f 77 20 46 54 53 34 2d 6f   to allow FTS4-o
44522 6e 6c 79 20 73 79 6e 74 61 78 20 2a 2f 0d 0a 20  nly syntax */.. 
44523 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
44524 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44525 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
44526 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 7a 43  f entries in azC
44527 6f 6c 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ol[] */..  int i
44528 44 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20 20 20  DefaultCol;     
44529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4452a 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e  * Default column
4452b 20 74 6f 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20   to query */..  
4452c 69 6e 74 20 69 73 4e 6f 74 3b 20 20 20 20 20 20  int isNot;      
4452d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4452e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 67      /* True if g
4452f 65 74 4e 65 78 74 4e 6f 64 65 28 29 20 73 65 65  etNextNode() see
44530 73 20 61 20 75 6e 61 72 79 20 2d 20 2a 2f 0d 0a  s a unary - */..
44531 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
44532 74 20 2a 70 43 74 78 3b 20 20 20 20 20 20 20 20  t *pCtx;        
44533 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
44534 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
44535 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e 65 73  e */..  int nNes
44536 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
44537 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
44538 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
44539 62 72 61 63 6b 65 74 73 20 2a 2f 0d 0a 7d 3b 0d  brackets */..};.
4453a 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  .../*..** This f
4453b 75 6e 63 74 69 6f 6e 20 69 73 20 65 71 75 69 76  unction is equiv
4453c 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 73 74 61  alent to the sta
4453d 6e 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20  ndard isspace() 
4453e 66 75 6e 63 74 69 6f 6e 2e 20 0d 0a 2a 2a 0d 0a  function. ..**..
4453f 2a 2a 20 54 68 65 20 73 74 61 6e 64 61 72 64 20  ** The standard 
44540 69 73 73 70 61 63 65 28 29 20 63 61 6e 20 62 65  isspace() can be
44541 20 61 77 6b 77 61 72 64 20 74 6f 20 75 73 65 20   awkward to use 
44542 73 61 66 65 6c 79 2c 20 62 65 63 61 75 73 65 20  safely, because 
44543 61 6c 74 68 6f 75 67 68 20 69 74 0d 0a 2a 2a 20  although it..** 
44544 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61 63  is defined to ac
44545 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e 74  cept an argument
44546 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 74   of type int, it
44547 73 20 62 65 68 61 76 69 6f 75 72 20 77 68 65 6e  s behaviour when
44548 20 70 61 73 73 65 64 0d 0a 2a 2a 20 61 6e 20 69   passed..** an i
44549 6e 74 65 67 65 72 20 74 68 61 74 20 66 61 6c 6c  nteger that fall
4454a 73 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  s outside of the
4454b 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e   range of the un
4454c 73 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65  signed char type
4454d 0d 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  ..** is undefine
4454e 64 20 28 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73  d (and sometimes
4454f 2c 20 22 75 6e 64 65 66 69 6e 65 64 22 20 6d 65  , "undefined" me
44550 61 6e 73 20 73 65 67 66 61 75 6c 74 29 2e 20 54  ans segfault). T
44551 68 69 73 20 77 72 61 70 70 65 72 0d 0a 2a 2a 20  his wrapper..** 
44552 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61 63  is defined to ac
44553 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e 74  cept an argument
44554 20 6f 66 20 74 79 70 65 20 63 68 61 72 2c 20 61   of type char, a
44555 6e 64 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  nd always return
44556 73 20 30 20 66 6f 72 0d 0a 2a 2a 20 61 6e 79 20  s 0 for..** any 
44557 76 61 6c 75 65 73 20 74 68 61 74 20 66 61 6c 6c  values that fall
44558 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
44559 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e 73  range of the uns
4455a 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65 20  igned char type 
4455b 28 69 2e 65 2e 0d 0a 2a 2a 20 6e 65 67 61 74 69  (i.e...** negati
4455c 76 65 20 76 61 6c 75 65 73 29 2e 0d 0a 2a 2f 0d  ve values)...*/.
4455d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
4455e 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29 7b  isspace(char c){
4455f 0d 0a 20 20 72 65 74 75 72 6e 20 63 3d 3d 27 20  ..  return c==' 
44560 27 20 7c 7c 20 63 3d 3d 27 5c 74 27 20 7c 7c 20  ' || c=='\t' || 
44561 63 3d 3d 27 5c 6e 27 20 7c 7c 20 63 3d 3d 27 5c  c=='\n' || c=='\
44562 72 27 20 7c 7c 20 63 3d 3d 27 5c 76 27 20 7c 7c  r' || c=='\v' ||
44563 20 63 3d 3d 27 5c 66 27 3b 0d 0a 7d 0d 0a 0d 0a   c=='\f';..}....
44564 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  /*..** Allocate 
44565 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 6d  nByte bytes of m
44566 65 6d 6f 72 79 20 75 73 69 6e 67 20 73 71 6c 69  emory using sqli
44567 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66  te3_malloc(). If
44568 20 73 75 63 63 65 73 73 66 75 6c 2c 0d 0a 2a 2a   successful,..**
44569 20 7a 65 72 6f 20 74 68 65 20 6d 65 6d 6f 72 79   zero the memory
4456a 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
4456b 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  g a pointer to i
4456c 74 2e 20 49 66 20 75 6e 73 75 63 63 65 73 73 66  t. If unsuccessf
4456d 75 6c 2c 20 0d 0a 2a 2a 20 72 65 74 75 72 6e 20  ul, ..** return 
4456e 4e 55 4c 4c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  NULL...*/..stati
4456f 63 20 76 6f 69 64 20 2a 66 74 73 33 4d 61 6c 6c  c void *fts3Mall
44570 6f 63 5a 65 72 6f 28 69 6e 74 20 6e 42 79 74 65  ocZero(int nByte
44571 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 70 52 65 74  ){..  void *pRet
44572 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
44573 63 28 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66 28  c(nByte);..  if(
44574 20 70 52 65 74 20 29 20 6d 65 6d 73 65 74 28 70   pRet ) memset(p
44575 52 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0d  Ret, 0, nByte);.
44576 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0d  .  return pRet;.
44577 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45  .}....../*..** E
44578 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
44579 74 6f 6b 65 6e 20 66 72 6f 6d 20 62 75 66 66 65  token from buffe
4457a 72 20 7a 20 28 6c 65 6e 67 74 68 20 6e 29 20 75  r z (length n) u
4457b 73 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a  sing the tokeniz
4457c 65 72 0d 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72  er..** and other
4457d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 63 6f   information (co
4457e 6c 75 6d 6e 20 6e 61 6d 65 73 20 65 74 63 2e 29  lumn names etc.)
4457f 20 69 6e 20 70 50 61 72 73 65 2e 20 43 72 65 61   in pParse. Crea
44580 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 0d 0a  te an Fts3Expr..
44581 2a 2a 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  ** structure of 
44582 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50 48  type FTSQUERY_PH
44583 52 41 53 45 20 63 6f 6e 74 61 69 6e 69 6e 67 20  RASE containing 
44584 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69 73 74  a phrase consist
44585 69 6e 67 20 6f 66 20 74 68 69 73 0d 0a 2a 2a 20  ing of this..** 
44586 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 61 6e 64  single token and
44587 20 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20   set *ppExpr to 
44588 70 6f 69 6e 74 20 74 6f 20 69 74 2e 20 49 66 20  point to it. If 
44589 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62  the end of the b
4458a 75 66 66 65 72 20 69 73 0d 0a 2a 2a 20 72 65 61  uffer is..** rea
4458b 63 68 65 64 20 62 65 66 6f 72 65 20 61 20 74 6f  ched before a to
4458c 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c 20 73 65  ken is found, se
4458d 74 20 2a 70 70 45 78 70 72 20 74 6f 20 7a 65 72  t *ppExpr to zer
4458e 6f 2e 20 49 74 20 69 73 20 74 68 65 0d 0a 2a 2a  o. It is the..**
4458f 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
44590 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
44591 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65 61 6c   eventually deal
44592 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c 6f 63  locate the alloc
44593 61 74 65 64 20 0d 0a 2a 2a 20 46 74 73 33 45 78  ated ..** Fts3Ex
44594 70 72 20 73 74 72 75 63 74 75 72 65 20 28 69 66  pr structure (if
44595 20 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e 67   any) by passing
44596 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 66   it to sqlite3_f
44597 72 65 65 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52  ree()...**..** R
44598 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
44599 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f  if successful, o
4459a 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  r SQLITE_NOMEM i
4459b 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
4459c 61 74 69 6f 6e 0d 0a 2a 2a 20 66 61 69 6c 73 2e  ation..** fails.
4459d 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
4459e 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28 0d 0a   getNextToken(..
4459f 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a    ParseContext *
445a0 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
445a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73            /* fts
445a2 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f  3 query parse co
445a3 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  ntext */..  int 
445a4 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  iCol,           
445a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445a6 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72      /* Value for
445a7 20 46 74 73 33 50 68 72 61 73 65 2e 69 43 6f 6c   Fts3Phrase.iCol
445a8 75 6d 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  umn */..  const 
445a9 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  char *z, int n, 
445aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445ab 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e    /* Input strin
445ac 67 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72  g */..  Fts3Expr
445ad 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20   **ppExpr,      
445ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445af 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69  /* OUT: expressi
445b0 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e  on */..  int *pn
445b1 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20  Consumed        
445b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445b3 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
445b4 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
445b5 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69  d */..){..  sqli
445b6 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
445b7 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50 61 72  Tokenizer = pPar
445b8 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d  se->pTokenizer;.
445b9 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
445ba 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
445bb 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f  t *pModule = pTo
445bc 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
445bd 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  ;..  int rc;..  
445be 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
445bf 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
445c0 72 3b 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a  r;..  Fts3Expr *
445c1 70 52 65 74 20 3d 20 30 3b 0d 0a 20 20 69 6e 74  pRet = 0;..  int
445c2 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0d   nConsumed = 0;.
445c3 0a 0d 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ...  rc = pModul
445c4 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
445c5 7a 65 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72  zer, z, n, &pCur
445c6 73 6f 72 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d  sor);..  if( rc=
445c7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
445c8 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
445c9 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 69 6e 74 20  Token;..    int 
445ca 6e 54 6f 6b 65 6e 2c 20 69 53 74 61 72 74 2c 20  nToken, iStart, 
445cb 69 45 6e 64 2c 20 69 50 6f 73 69 74 69 6f 6e 3b  iEnd, iPosition;
445cc 0d 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b  ..    int nByte;
445cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
445cf 2a 20 74 6f 74 61 6c 20 73 70 61 63 65 20 74 6f  * total space to
445d0 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0d 0a 0d 0a   allocate */....
445d1 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f      pCursor->pTo
445d2 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
445d3 69 7a 65 72 3b 0d 0a 20 20 20 20 72 63 20 3d 20  izer;..    rc = 
445d4 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70  pModule->xNext(p
445d5 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
445d6 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72   &nToken, &iStar
445d7 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 69  t, &iEnd, &iPosi
445d8 74 69 6f 6e 29 3b 0d 0a 0d 0a 20 20 20 20 69 66  tion);....    if
445d9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
445da 29 7b 0d 0a 20 20 20 20 20 20 6e 42 79 74 65 20  ){..      nByte 
445db 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  = sizeof(Fts3Exp
445dc 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33  r) + sizeof(Fts3
445dd 50 68 72 61 73 65 29 20 2b 20 6e 54 6f 6b 65 6e  Phrase) + nToken
445de 3b 0d 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20  ;..      pRet = 
445df 28 46 74 73 33 45 78 70 72 20 2a 29 66 74 73 33  (Fts3Expr *)fts3
445e0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 74 65  MallocZero(nByte
445e1 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 21 70  );..      if( !p
445e2 52 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  Ret ){..        
445e3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
445e4 4d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  M;..      }else{
445e5 0d 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ..        pRet->
445e6 65 54 79 70 65 20 3d 20 46 54 53 51 55 45 52 59  eType = FTSQUERY
445e7 5f 50 48 52 41 53 45 3b 0d 0a 20 20 20 20 20 20  _PHRASE;..      
445e8 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 20    pRet->pPhrase 
445e9 3d 20 28 46 74 73 33 50 68 72 61 73 65 20 2a 29  = (Fts3Phrase *)
445ea 26 70 52 65 74 5b 31 5d 3b 0d 0a 20 20 20 20 20  &pRet[1];..     
445eb 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65     pRet->pPhrase
445ec 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 31 3b 0d 0a 20  ->nToken = 1;.. 
445ed 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
445ee 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  rase->iColumn = 
445ef 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20 20 20 70  iCol;..        p
445f0 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54  Ret->pPhrase->aT
445f1 6f 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e 54 6f 6b  oken[0].n = nTok
445f2 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20 70 52 65  en;..        pRe
445f3 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  t->pPhrase->aTok
445f4 65 6e 5b 30 5d 2e 7a 20 3d 20 28 63 68 61 72 20  en[0].z = (char 
445f5 2a 29 26 70 52 65 74 2d 3e 70 50 68 72 61 73 65  *)&pRet->pPhrase
445f6 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 6d 65  [1];..        me
445f7 6d 63 70 79 28 70 52 65 74 2d 3e 70 50 68 72 61  mcpy(pRet->pPhra
445f8 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 2c  se->aToken[0].z,
445f9 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29   zToken, nToken)
445fa 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69 66 28  ;....        if(
445fb 20 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69 45 6e   iEnd<n && z[iEn
445fc 64 5d 3d 3d 27 2a 27 20 29 7b 0d 0a 20 20 20 20  d]=='*' ){..    
445fd 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72        pRet->pPhr
445fe 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 69  ase->aToken[0].i
445ff 73 50 72 65 66 69 78 20 3d 20 31 3b 0d 0a 20 20  sPrefix = 1;..  
44600 20 20 20 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0d          iEnd++;.
44601 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  .        }....  
44602 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20 29        while( 1 )
44603 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  {..          if(
44604 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65   !sqlite3_fts3_e
44605 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
44606 73 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 26  s ..           &
44607 26 20 69 53 74 61 72 74 3e 30 20 26 26 20 7a 5b  & iStart>0 && z[
44608 69 53 74 61 72 74 2d 31 5d 3d 3d 27 2d 27 20 0d  iStart-1]=='-' .
44609 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0d 0a 20  .          ){.. 
4460a 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
4460b 65 2d 3e 69 73 4e 6f 74 20 3d 20 31 3b 0d 0a 20  e->isNot = 1;.. 
4460c 20 20 20 20 20 20 20 20 20 20 20 69 53 74 61 72             iStar
4460d 74 2d 2d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  t--;..          
4460e 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65  }else if( pParse
4460f 2d 3e 62 46 74 73 34 20 26 26 20 69 53 74 61 72  ->bFts4 && iStar
44610 74 3e 30 20 26 26 20 7a 5b 69 53 74 61 72 74 2d  t>0 && z[iStart-
44611 31 5d 3d 3d 27 5e 27 20 29 7b 0d 0a 20 20 20 20  1]=='^' ){..    
44612 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
44613 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d  hrase->aToken[0]
44614 2e 62 46 69 72 73 74 20 3d 20 31 3b 0d 0a 20 20  .bFirst = 1;..  
44615 20 20 20 20 20 20 20 20 20 20 69 53 74 61 72 74            iStart
44616 2d 2d 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  --;..          }
44617 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
44618 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20     break;..     
44619 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
4461a 7d 0d 0a 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  }....      }..  
4461b 20 20 20 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20      nConsumed = 
4461c 69 45 6e 64 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  iEnd;..    }....
4461d 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c      pModule->xCl
4461e 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0d 0a 20  ose(pCursor);.. 
4461f 20 7d 0d 0a 20 20 0d 0a 20 20 2a 70 6e 43 6f 6e   }..  ..  *pnCon
44620 73 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d 65  sumed = nConsume
44621 64 3b 0d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  d;..  *ppExpr = 
44622 70 52 65 74 3b 0d 0a 20 20 72 65 74 75 72 6e 20  pRet;..  return 
44623 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a  rc;..}....../*..
44624 2a 2a 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d  ** Enlarge a mem
44625 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  ory allocation. 
44626 20 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65   If an out-of-me
44627 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
44628 6f 63 63 75 72 73 2c 0d 0a 2a 2a 20 74 68 65 6e  occurs,..** then
44629 20 66 72 65 65 20 74 68 65 20 6f 6c 64 20 61 6c   free the old al
4462a 6c 6f 63 61 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 73  location...*/..s
4462b 74 61 74 69 63 20 76 6f 69 64 20 2a 66 74 73 33  tatic void *fts3
4462c 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f  ReallocOrFree(vo
4462d 69 64 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e  id *pOrig, int n
4462e 4e 65 77 29 7b 0d 0a 20 20 76 6f 69 64 20 2a 70  New){..  void *p
4462f 52 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  Ret = sqlite3_re
44630 61 6c 6c 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65  alloc(pOrig, nNe
44631 77 29 3b 0d 0a 20 20 69 66 28 20 21 70 52 65 74  w);..  if( !pRet
44632 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
44633 5f 66 72 65 65 28 70 4f 72 69 67 29 3b 0d 0a 20  _free(pOrig);.. 
44634 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 52 65   }..  return pRe
44635 74 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  t;..}..../*..** 
44636 42 75 66 66 65 72 20 7a 49 6e 70 75 74 2c 20 6c  Buffer zInput, l
44637 65 6e 67 74 68 20 6e 49 6e 70 75 74 2c 20 63 6f  ength nInput, co
44638 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65  ntains the conte
44639 6e 74 73 20 6f 66 20 61 20 71 75 6f 74 65 64 20  nts of a quoted 
4463a 73 74 72 69 6e 67 0d 0a 2a 2a 20 74 68 61 74 20  string..** that 
4463b 61 70 70 65 61 72 65 64 20 61 73 20 70 61 72 74  appeared as part
4463c 20 6f 66 20 61 6e 20 66 74 73 33 20 71 75 65 72   of an fts3 quer
4463d 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 65  y expression. Ne
4463e 69 74 68 65 72 20 71 75 6f 74 65 20 63 68 61 72  ither quote char
4463f 61 63 74 65 72 0d 0a 2a 2a 20 69 73 20 69 6e 63  acter..** is inc
44640 6c 75 64 65 64 20 69 6e 20 74 68 65 20 62 75 66  luded in the buf
44641 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
44642 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 74  on attempts to t
44643 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65 6e 74 69  okenize the enti
44644 72 65 0d 0a 2a 2a 20 69 6e 70 75 74 20 62 75 66  re..** input buf
44645 66 65 72 20 61 6e 64 20 63 72 65 61 74 65 20 61  fer and create a
44646 6e 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63  n Fts3Expr struc
44647 74 75 72 65 20 6f 66 20 74 79 70 65 20 46 54 53  ture of type FTS
44648 51 55 45 52 59 5f 50 48 52 41 53 45 20 0d 0a 2a  QUERY_PHRASE ..*
44649 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  * containing the
4464a 20 72 65 73 75 6c 74 73 2e 0d 0a 2a 2a 0d 0a 2a   results...**..*
4464b 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
4464c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
4464d 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78  turned and *ppEx
4464e 70 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  pr set to point 
4464f 61 74 20 74 68 65 0d 0a 2a 2a 20 61 6c 6c 6f 63  at the..** alloc
44650 61 74 65 64 20 46 74 73 33 45 78 70 72 20 73 74  ated Fts3Expr st
44651 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69  ructure. Otherwi
44652 73 65 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  se, either SQLIT
44653 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20  E_NOMEM (out of 
44654 6d 65 6d 6f 72 79 0d 0a 2a 2a 20 65 72 72 6f 72  memory..** error
44655 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  ) or SQLITE_ERRO
44656 52 20 28 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20  R (tokenization 
44657 65 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e  error) is return
44658 65 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73  ed and *ppExpr s
44659 65 74 0d 0a 2a 2a 20 74 6f 20 30 2e 0d 0a 2a 2f  et..** to 0...*/
4465a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  ..static int get
4465b 4e 65 78 74 53 74 72 69 6e 67 28 0d 0a 20 20 50  NextString(..  P
4465c 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61  arseContext *pPa
4465d 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
4465e 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71         /* fts3 q
4465f 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65  uery parse conte
44660 78 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  xt */..  const c
44661 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74  har *zInput, int
44662 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20   nInput,        
44663 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
44664 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20   */..  Fts3Expr 
44665 2a 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20  **ppExpr        
44666 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44667 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f  * OUT: expressio
44668 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69  n */..){..  sqli
44669 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
4466a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50 61 72  Tokenizer = pPar
4466b 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d  se->pTokenizer;.
4466c 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
4466d 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
4466e 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f  t *pModule = pTo
4466f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
44670 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  ;..  int rc;..  
44671 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b  Fts3Expr *p = 0;
44672 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ..  sqlite3_toke
44673 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
44674 75 72 73 6f 72 20 3d 20 30 3b 0d 0a 20 20 63 68  ursor = 0;..  ch
44675 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b 0d 0a  ar *zTemp = 0;..
44676 20 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30 3b    int nTemp = 0;
44677 0d 0a 0d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  ....  const int 
44678 6e 53 70 61 63 65 20 3d 20 73 69 7a 65 6f 66 28  nSpace = sizeof(
44679 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
4467a 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0d  of(Fts3Phrase);.
4467b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20  .  int nToken = 
4467c 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 66  0;....  /* The f
4467d 69 6e 61 6c 20 46 74 73 33 45 78 70 72 20 64 61  inal Fts3Expr da
4467e 74 61 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e  ta structure, in
4467f 63 6c 75 64 69 6e 67 20 74 68 65 20 46 74 73 33  cluding the Fts3
44680 50 68 72 61 73 65 2c 0d 0a 20 20 2a 2a 20 46 74  Phrase,..  ** Ft
44681 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 73 74  s3PhraseToken st
44682 72 75 63 74 75 72 65 73 20 74 6f 6b 65 6e 20 62  ructures token b
44683 75 66 66 65 72 73 20 61 72 65 20 61 6c 6c 20 73  uffers are all s
44684 74 6f 72 65 64 20 61 73 20 61 20 73 69 6e 67 6c  tored as a singl
44685 65 20 0d 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  e ..  ** allocat
44686 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ion so that the 
44687 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 62  expression can b
44688 65 20 66 72 65 65 64 20 77 69 74 68 20 61 20 73  e freed with a s
44689 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 0d 0a 20  ingle call to.. 
4468a 20 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65   ** sqlite3_free
4468b 28 29 2e 20 53 65 74 74 69 6e 67 20 74 68 69 73  (). Setting this
4468c 20 75 70 20 72 65 71 75 69 72 65 73 20 61 20 74   up requires a t
4468d 77 6f 20 70 61 73 73 20 61 70 70 72 6f 61 63 68  wo pass approach
4468e 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 68  ...  **..  ** Th
4468f 65 20 66 69 72 73 74 20 70 61 73 73 2c 20 69 6e  e first pass, in
44690 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77   the block below
44691 2c 20 75 73 65 73 20 61 20 74 6f 6b 65 6e 69 7a  , uses a tokeniz
44692 65 72 20 63 75 72 73 6f 72 20 74 6f 20 69 74 65  er cursor to ite
44693 72 61 74 65 0d 0a 20 20 2a 2a 20 74 68 72 6f 75  rate..  ** throu
44694 67 68 20 74 68 65 20 74 6f 6b 65 6e 73 20 69 6e  gh the tokens in
44695 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
44696 20 54 68 69 73 20 70 61 73 73 20 75 73 65 73 20   This pass uses 
44697 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
44698 65 28 29 0d 0a 20 20 2a 2a 20 74 6f 20 61 73 73  e()..  ** to ass
44699 65 6d 62 6c 65 20 64 61 74 61 20 69 6e 20 74 77  emble data in tw
4469a 6f 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65 72  o dynamic buffer
4469b 73 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20  s:..  **..  **  
4469c 20 42 75 66 66 65 72 20 70 3a 20 50 6f 69 6e 74   Buffer p: Point
4469d 73 20 74 6f 20 74 68 65 20 46 74 73 33 45 78 70  s to the Fts3Exp
4469e 72 20 73 74 72 75 63 74 75 72 65 2c 20 66 6f 6c  r structure, fol
4469f 6c 6f 77 65 64 20 62 79 20 74 68 65 20 46 74 73  lowed by the Fts
446a0 33 50 68 72 61 73 65 0d 0a 20 20 2a 2a 20 20 20  3Phrase..  **   
446a1 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74            struct
446a2 75 72 65 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ure, followed by
446a3 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 46 74   the array of Ft
446a4 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20 0d 0a  s3PhraseToken ..
446a5 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
446a6 20 73 74 72 75 63 74 75 72 65 73 2e 20 54 68 69   structures. Thi
446a7 73 20 70 61 73 73 20 6f 6e 6c 79 20 70 6f 70 75  s pass only popu
446a8 6c 61 74 65 73 20 74 68 65 20 46 74 73 33 50 68  lates the Fts3Ph
446a9 72 61 73 65 54 6f 6b 65 6e 20 61 72 72 61 79 2e  raseToken array.
446aa 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20 42  ..  **..  **   B
446ab 75 66 66 65 72 20 7a 54 65 6d 70 3a 20 43 6f 6e  uffer zTemp: Con
446ac 74 61 69 6e 73 20 63 6f 70 69 65 73 20 6f 66 20  tains copies of 
446ad 61 6c 6c 20 74 6f 6b 65 6e 73 2e 0d 0a 20 20 2a  all tokens...  *
446ae 2a 0d 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  *..  ** The seco
446af 6e 64 20 70 61 73 73 2c 20 69 6e 20 74 68 65 20  nd pass, in the 
446b0 62 6c 6f 63 6b 20 74 68 61 74 20 62 65 67 69 6e  block that begin
446b1 73 20 22 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  s "if( rc==SQLIT
446b2 45 5f 44 4f 4e 45 20 29 22 20 62 65 6c 6f 77 2c  E_DONE )" below,
446b3 0d 0a 20 20 2a 2a 20 61 70 70 65 6e 64 73 20 62  ..  ** appends b
446b4 75 66 66 65 72 20 7a 54 65 6d 70 20 74 6f 20 62  uffer zTemp to b
446b5 75 66 66 65 72 20 70 2c 20 61 6e 64 20 66 69 6c  uffer p, and fil
446b6 6c 73 20 69 6e 20 74 68 65 20 46 74 73 33 45 78  ls in the Fts3Ex
446b7 70 72 20 61 6e 64 20 46 74 73 33 50 68 72 61 73  pr and Fts3Phras
446b8 65 0d 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72  e..  ** structur
446b9 65 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 72 63 20  es...  */..  rc 
446ba 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e  = pModule->xOpen
446bb 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e  (pTokenizer, zIn
446bc 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43  put, nInput, &pC
446bd 75 72 73 6f 72 29 3b 0d 0a 20 20 69 66 28 20 72  ursor);..  if( r
446be 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
446bf 0a 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20  .    int ii;..  
446c0 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
446c1 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
446c2 65 72 3b 0d 0a 20 20 20 20 66 6f 72 28 69 69 3d  er;..    for(ii=
446c3 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
446c4 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  ; ii++){..      
446c5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 79 74  const char *zByt
446c6 65 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 42  e;..      int nB
446c7 79 74 65 2c 20 69 42 65 67 69 6e 2c 20 69 45 6e  yte, iBegin, iEn
446c8 64 2c 20 69 50 6f 73 3b 0d 0a 20 20 20 20 20 20  d, iPos;..      
446c9 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e  rc = pModule->xN
446ca 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26 7a 42  ext(pCursor, &zB
446cb 79 74 65 2c 20 26 6e 42 79 74 65 2c 20 26 69 42  yte, &nByte, &iB
446cc 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50  egin, &iEnd, &iP
446cd 6f 73 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20  os);..      if( 
446ce 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
446cf 0d 0a 20 20 20 20 20 20 20 20 46 74 73 33 50 68  ..        Fts3Ph
446d0 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  raseToken *pToke
446d1 6e 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 70 20  n;....        p 
446d2 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
446d3 72 65 65 28 70 2c 20 6e 53 70 61 63 65 20 2b 20  ree(p, nSpace + 
446d4 69 69 2a 73 69 7a 65 6f 66 28 46 74 73 33 50 68  ii*sizeof(Fts3Ph
446d5 72 61 73 65 54 6f 6b 65 6e 29 29 3b 0d 0a 20 20  raseToken));..  
446d6 20 20 20 20 20 20 69 66 28 20 21 70 20 29 20 67        if( !p ) g
446d7 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 0d 0a 20  oto no_mem;.... 
446d8 20 20 20 20 20 20 20 7a 54 65 6d 70 20 3d 20 66         zTemp = f
446d9 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  ts3ReallocOrFree
446da 28 7a 54 65 6d 70 2c 20 6e 54 65 6d 70 20 2b 20  (zTemp, nTemp + 
446db 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 20 20 20  nByte);..       
446dc 20 69 66 28 20 21 7a 54 65 6d 70 20 29 20 67 6f   if( !zTemp ) go
446dd 74 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 0d 0a 20 20  to no_mem;....  
446de 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 54        assert( nT
446df 6f 6b 65 6e 3d 3d 69 69 20 29 3b 0d 0a 20 20 20  oken==ii );..   
446e0 20 20 20 20 20 70 54 6f 6b 65 6e 20 3d 20 26 28       pToken = &(
446e1 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 28 26  (Fts3Phrase *)(&
446e2 70 5b 31 5d 29 29 2d 3e 61 54 6f 6b 65 6e 5b 69  p[1]))->aToken[i
446e3 69 5d 3b 0d 0a 20 20 20 20 20 20 20 20 6d 65 6d  i];..        mem
446e4 73 65 74 28 70 54 6f 6b 65 6e 2c 20 30 2c 20 73  set(pToken, 0, s
446e5 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73 65  izeof(Fts3Phrase
446e6 54 6f 6b 65 6e 29 29 3b 0d 0a 0d 0a 20 20 20 20  Token));....    
446e7 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
446e8 70 5b 6e 54 65 6d 70 5d 2c 20 7a 42 79 74 65 2c  p[nTemp], zByte,
446e9 20 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 20 20   nByte);..      
446ea 20 20 6e 54 65 6d 70 20 2b 3d 20 6e 42 79 74 65    nTemp += nByte
446eb 3b 0d 0a 0d 0a 20 20 20 20 20 20 20 20 70 54 6f  ;....        pTo
446ec 6b 65 6e 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0d  ken->n = nByte;.
446ed 0a 20 20 20 20 20 20 20 20 70 54 6f 6b 65 6e 2d  .        pToken-
446ee 3e 69 73 50 72 65 66 69 78 20 3d 20 28 69 45 6e  >isPrefix = (iEn
446ef 64 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70  d<nInput && zInp
446f0 75 74 5b 69 45 6e 64 5d 3d 3d 27 2a 27 29 3b 0d  ut[iEnd]=='*');.
446f1 0a 20 20 20 20 20 20 20 20 70 54 6f 6b 65 6e 2d  .        pToken-
446f2 3e 62 46 69 72 73 74 20 3d 20 28 69 42 65 67 69  >bFirst = (iBegi
446f3 6e 3e 30 20 26 26 20 7a 49 6e 70 75 74 5b 69 42  n>0 && zInput[iB
446f4 65 67 69 6e 2d 31 5d 3d 3d 27 5e 27 29 3b 0d 0a  egin-1]=='^');..
446f5 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d          nToken =
446f6 20 69 69 2b 31 3b 0d 0a 20 20 20 20 20 20 7d 0d   ii+1;..      }.
446f7 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 70 4d  .    }....    pM
446f8 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
446f9 75 72 73 6f 72 29 3b 0d 0a 20 20 20 20 70 43 75  ursor);..    pCu
446fa 72 73 6f 72 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  rsor = 0;..  }..
446fb 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
446fc 54 45 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20 20 20  TE_DONE ){..    
446fd 69 6e 74 20 6a 6a 3b 0d 0a 20 20 20 20 63 68 61  int jj;..    cha
446fe 72 20 2a 7a 42 75 66 20 3d 20 30 3b 0d 0a 0d 0a  r *zBuf = 0;....
446ff 20 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c      p = fts3Real
44700 6c 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 53 70  locOrFree(p, nSp
44701 61 63 65 20 2b 20 6e 54 6f 6b 65 6e 2a 73 69 7a  ace + nToken*siz
44702 65 6f 66 28 46 74 73 33 50 68 72 61 73 65 54 6f  eof(Fts3PhraseTo
44703 6b 65 6e 29 20 2b 20 6e 54 65 6d 70 29 3b 0d 0a  ken) + nTemp);..
44704 20 20 20 20 69 66 28 20 21 70 20 29 20 67 6f 74      if( !p ) got
44705 6f 20 6e 6f 5f 6d 65 6d 3b 0d 0a 20 20 20 20 6d  o no_mem;..    m
44706 65 6d 73 65 74 28 70 2c 20 30 2c 20 28 63 68 61  emset(p, 0, (cha
44707 72 20 2a 29 26 28 28 28 46 74 73 33 50 68 72 61  r *)&(((Fts3Phra
44708 73 65 20 2a 29 26 70 5b 31 5d 29 2d 3e 61 54 6f  se *)&p[1])->aTo
44709 6b 65 6e 5b 30 5d 29 2d 28 63 68 61 72 20 2a 29  ken[0])-(char *)
4470a 70 29 3b 0d 0a 20 20 20 20 70 2d 3e 65 54 79 70  p);..    p->eTyp
4470b 65 20 3d 20 46 54 53 51 55 45 52 59 5f 50 48 52  e = FTSQUERY_PHR
4470c 41 53 45 3b 0d 0a 20 20 20 20 70 2d 3e 70 50 68  ASE;..    p->pPh
4470d 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61  rase = (Fts3Phra
4470e 73 65 20 2a 29 26 70 5b 31 5d 3b 0d 0a 20 20 20  se *)&p[1];..   
4470f 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f   p->pPhrase->iCo
44710 6c 75 6d 6e 20 3d 20 70 50 61 72 73 65 2d 3e 69  lumn = pParse->i
44711 44 65 66 61 75 6c 74 43 6f 6c 3b 0d 0a 20 20 20  DefaultCol;..   
44712 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f   p->pPhrase->nTo
44713 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0d 0a 0d  ken = nToken;...
44714 0a 20 20 20 20 7a 42 75 66 20 3d 20 28 63 68 61  .    zBuf = (cha
44715 72 20 2a 29 26 70 2d 3e 70 50 68 72 61 73 65 2d  r *)&p->pPhrase-
44716 3e 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b  >aToken[nToken];
44717 0d 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 20  ..    if( zTemp 
44718 29 7b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  ){..      memcpy
44719 28 7a 42 75 66 2c 20 7a 54 65 6d 70 2c 20 6e 54  (zBuf, zTemp, nT
4471a 65 6d 70 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c  emp);..      sql
4471b 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29  ite3_free(zTemp)
4471c 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
4471d 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 54 65       assert( nTe
4471e 6d 70 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 7d 0d  mp==0 );..    }.
4471f 0a 0d 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b  ...    for(jj=0;
44720 20 6a 6a 3c 70 2d 3e 70 50 68 72 61 73 65 2d 3e   jj<p->pPhrase->
44721 6e 54 6f 6b 65 6e 3b 20 6a 6a 2b 2b 29 7b 0d 0a  nToken; jj++){..
44722 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65        p->pPhrase
44723 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d  ->aToken[jj].z =
44724 20 7a 42 75 66 3b 0d 0a 20 20 20 20 20 20 7a 42   zBuf;..      zB
44725 75 66 20 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65  uf += p->pPhrase
44726 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0d  ->aToken[jj].n;.
44727 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
44728 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d   SQLITE_OK;..  }
44729 0d 0a 0d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  ....  *ppExpr = 
4472a 70 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p;..  return rc;
4472b 0d 0a 6e 6f 5f 6d 65 6d 3a 0d 0a 0d 0a 20 20 69  ..no_mem:....  i
4472c 66 28 20 70 43 75 72 73 6f 72 20 29 7b 0d 0a 20  f( pCursor ){.. 
4472d 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
4472e 73 65 28 70 43 75 72 73 6f 72 29 3b 0d 0a 20 20  se(pCursor);..  
4472f 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
44730 65 28 7a 54 65 6d 70 29 3b 0d 0a 20 20 73 71 6c  e(zTemp);..  sql
44731 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20  ite3_free(p);.. 
44732 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0d 0a 20   *ppExpr = 0;.. 
44733 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
44734 4f 4d 45 4d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  OMEM;..}..../*..
44735 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 67 65 74 4e  ** Function getN
44736 65 78 74 4e 6f 64 65 28 29 2c 20 77 68 69 63 68  extNode(), which
44737 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 66 74   is called by ft
44738 73 33 45 78 70 72 50 61 72 73 65 28 29 2c 20 6d  s3ExprParse(), m
44739 61 79 20 69 74 73 65 6c 66 0d 0a 2a 2a 20 63 61  ay itself..** ca
4473a 6c 6c 20 66 74 73 33 45 78 70 72 50 61 72 73 65  ll fts3ExprParse
4473b 28 29 2e 20 53 6f 20 74 68 69 73 20 66 6f 72 77  (). So this forw
4473c 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
4473d 69 73 20 72 65 71 75 69 72 65 64 2e 0d 0a 2a 2f  is required...*/
4473e 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
4473f 33 45 78 70 72 50 61 72 73 65 28 50 61 72 73 65  3ExprParse(Parse
44740 43 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73 74  Context *, const
44741 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74   char *, int, Ft
44742 73 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20 2a  s3Expr **, int *
44743 29 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  );..../*..** The
44744 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
44745 20 2a 70 70 45 78 70 72 20 69 73 20 70 6f 70 75   *ppExpr is popu
44746 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 61 6c  lated with an al
44747 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78 70 72  located Fts3Expr
44748 20 0d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c   ..** structure,
44749 20 6f 72 20 73 65 74 20 74 6f 20 30 20 69 66 20   or set to 0 if 
4474a 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
4474b 6e 70 75 74 20 62 75 66 66 65 72 20 69 73 20 72  nput buffer is r
4474c 65 61 63 68 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  eached...**..** 
4474d 52 65 74 75 72 6e 73 20 61 6e 20 53 51 4c 69 74  Returns an SQLit
4474e 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 53 51  e error code. SQ
4474f 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
44750 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c  thing works, SQL
44751 49 54 45 5f 4e 4f 4d 45 4d 0d 0a 2a 2a 20 69 66  ITE_NOMEM..** if
44752 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
44753 65 20 6f 63 63 75 72 73 2c 20 6f 72 20 53 51 4c  e occurs, or SQL
44754 49 54 45 5f 45 52 52 4f 52 20 69 66 20 61 20 70  ITE_ERROR if a p
44755 61 72 73 65 20 65 72 72 6f 72 20 69 73 20 65 6e  arse error is en
44756 63 6f 75 6e 74 65 72 65 64 2e 0d 0a 2a 2a 20 49  countered...** I
44757 66 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  f SQLITE_ERROR i
44758 73 20 72 65 74 75 72 6e 65 64 2c 20 70 43 6f 6e  s returned, pCon
44759 74 65 78 74 20 69 73 20 70 6f 70 75 6c 61 74 65  text is populate
4475a 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
4475b 6d 65 73 73 61 67 65 2e 0d 0a 2a 2f 0d 0a 73 74  message...*/..st
4475c 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
4475d 4e 6f 64 65 28 0d 0a 20 20 50 61 72 73 65 43 6f  Node(..  ParseCo
4475e 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20  ntext *pParse,  
4475f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44760 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70   /* fts3 query p
44761 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d  arse context */.
44762 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
44763 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
44764 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
44765 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0d 0a 20  put string */.. 
44766 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78   Fts3Expr **ppEx
44767 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pr,             
44768 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
44769 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0d 0a   expression */..
4476a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65    int *pnConsume
4476b 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
4476c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
4476d 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
4476e 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0d 0a 29  s consumed */..)
4476f 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  {..  static cons
44770 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79  t struct Fts3Key
44771 77 6f 72 64 20 7b 0d 0a 20 20 20 20 63 68 61 72  word {..    char
44772 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
44773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44774 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78    /* Keyword tex
44775 74 20 2a 2f 0d 0a 20 20 20 20 75 6e 73 69 67 6e  t */..    unsign
44776 65 64 20 63 68 61 72 20 6e 3b 20 20 20 20 20 20  ed char n;      
44777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44778 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
44779 20 6b 65 79 77 6f 72 64 20 2a 2f 0d 0a 20 20 20   keyword */..   
4477a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70   unsigned char p
4477b 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20 20  arenOnly;       
4477c 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 76         /* Only v
4477d 61 6c 69 64 20 69 6e 20 70 61 72 65 6e 20 6d 6f  alid in paren mo
4477e 64 65 20 2a 2f 0d 0a 20 20 20 20 75 6e 73 69 67  de */..    unsig
4477f 6e 65 64 20 63 68 61 72 20 65 54 79 70 65 3b 20  ned char eType; 
44780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44781 20 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f 64 65   /* Keyword code
44782 20 2a 2f 0d 0a 20 20 7d 20 61 4b 65 79 77 6f 72   */..  } aKeywor
44783 64 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 7b 20 22  d[] = {..    { "
44784 4f 52 22 20 2c 20 20 32 2c 20 30 2c 20 46 54 53  OR" ,  2, 0, FTS
44785 51 55 45 52 59 5f 4f 52 20 20 20 7d 2c 0d 0a 20  QUERY_OR   },.. 
44786 20 20 20 7b 20 22 41 4e 44 22 2c 20 20 33 2c 20     { "AND",  3, 
44787 31 2c 20 46 54 53 51 55 45 52 59 5f 41 4e 44 20  1, FTSQUERY_AND 
44788 20 7d 2c 0d 0a 20 20 20 20 7b 20 22 4e 4f 54 22   },..    { "NOT"
44789 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55 45 52  ,  3, 1, FTSQUER
4478a 59 5f 4e 4f 54 20 20 7d 2c 0d 0a 20 20 20 20 7b  Y_NOT  },..    {
4478b 20 22 4e 45 41 52 22 2c 20 34 2c 20 30 2c 20 46   "NEAR", 4, 0, F
4478c 54 53 51 55 45 52 59 5f 4e 45 41 52 20 7d 0d 0a  TSQUERY_NEAR }..
4478d 20 20 7d 3b 0d 0a 20 20 69 6e 74 20 69 69 3b 0d    };..  int ii;.
4478e 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0d 0a 20 20  .  int iCol;..  
4478f 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b 0d 0a 20 20  int iColLen;..  
44790 69 6e 74 20 72 63 3b 0d 0a 20 20 46 74 73 33 45  int rc;..  Fts3E
44791 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0d 0a  xpr *pRet = 0;..
44792 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
44793 7a 49 6e 70 75 74 20 3d 20 7a 3b 0d 0a 20 20 69  zInput = z;..  i
44794 6e 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b 0d 0a  nt nInput = n;..
44795 0d 0a 20 20 70 50 61 72 73 65 2d 3e 69 73 4e 6f  ..  pParse->isNo
44796 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 53  t = 0;....  /* S
44797 6b 69 70 20 6f 76 65 72 20 61 6e 79 20 77 68 69  kip over any whi
44798 74 65 73 70 61 63 65 20 62 65 66 6f 72 65 20 63  tespace before c
44799 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 6b 65  hecking for a ke
4479a 79 77 6f 72 64 2c 20 61 6e 20 6f 70 65 6e 20 6f  yword, an open o
4479b 72 0d 0a 20 20 2a 2a 20 63 6c 6f 73 65 20 62 72  r..  ** close br
4479c 61 63 6b 65 74 2c 20 6f 72 20 61 20 71 75 6f 74  acket, or a quot
4479d 65 64 20 73 74 72 69 6e 67 2e 20 0d 0a 20 20 2a  ed string. ..  *
4479e 2f 0d 0a 20 20 77 68 69 6c 65 28 20 6e 49 6e 70  /..  while( nInp
4479f 75 74 3e 30 20 26 26 20 66 74 73 33 69 73 73 70  ut>0 && fts3issp
447a0 61 63 65 28 2a 7a 49 6e 70 75 74 29 20 29 7b 0d  ace(*zInput) ){.
447a1 0a 20 20 20 20 6e 49 6e 70 75 74 2d 2d 3b 0d 0a  .    nInput--;..
447a2 20 20 20 20 7a 49 6e 70 75 74 2b 2b 3b 0d 0a 20      zInput++;.. 
447a3 20 7d 0d 0a 20 20 69 66 28 20 6e 49 6e 70 75 74   }..  if( nInput
447a4 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  ==0 ){..    retu
447a5 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d  rn SQLITE_DONE;.
447a6 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 65  .  }....  /* See
447a7 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
447a8 6e 67 20 77 69 74 68 20 61 20 6b 65 79 77 6f 72  ng with a keywor
447a9 64 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 69 3d  d. */..  for(ii=
447aa 30 3b 20 69 69 3c 28 69 6e 74 29 28 73 69 7a 65  0; ii<(int)(size
447ab 6f 66 28 61 4b 65 79 77 6f 72 64 29 2f 73 69 7a  of(aKeyword)/siz
447ac 65 6f 66 28 73 74 72 75 63 74 20 46 74 73 33 4b  eof(struct Fts3K
447ad 65 79 77 6f 72 64 29 29 3b 20 69 69 2b 2b 29 7b  eyword)); ii++){
447ae 0d 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75  ..    const stru
447af 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 20 2a  ct Fts3Keyword *
447b0 70 4b 65 79 20 3d 20 26 61 4b 65 79 77 6f 72 64  pKey = &aKeyword
447b1 5b 69 69 5d 3b 0d 0a 0d 0a 20 20 20 20 69 66 28  [ii];....    if(
447b2 20 28 70 4b 65 79 2d 3e 70 61 72 65 6e 4f 6e 6c   (pKey->parenOnl
447b3 79 20 26 20 7e 73 71 6c 69 74 65 33 5f 66 74 73  y & ~sqlite3_fts
447b4 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
447b5 65 73 65 73 29 21 3d 30 20 29 7b 0d 0a 20 20 20  eses)!=0 ){..   
447b6 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20     continue;..  
447b7 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 6e    }....    if( n
447b8 49 6e 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e 20 26  Input>=pKey->n &
447b9 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49 6e 70  & 0==memcmp(zInp
447ba 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70 4b 65  ut, pKey->z, pKe
447bb 79 2d 3e 6e 29 20 29 7b 0d 0a 20 20 20 20 20 20  y->n) ){..      
447bc 69 6e 74 20 6e 4e 65 61 72 20 3d 20 53 51 4c 49  int nNear = SQLI
447bd 54 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f  TE_FTS3_DEFAULT_
447be 4e 45 41 52 5f 50 41 52 41 4d 3b 0d 0a 20 20 20  NEAR_PARAM;..   
447bf 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 4b     int nKey = pK
447c0 65 79 2d 3e 6e 3b 0d 0a 20 20 20 20 20 20 63 68  ey->n;..      ch
447c1 61 72 20 63 4e 65 78 74 3b 0d 0a 0d 0a 20 20 20  ar cNext;....   
447c2 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
447c3 20 61 20 22 4e 45 41 52 22 20 6b 65 79 77 6f 72   a "NEAR" keywor
447c4 64 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20  d, check for an 
447c5 65 78 70 6c 69 63 69 74 20 6e 65 61 72 6e 65 73  explicit nearnes
447c6 73 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28  s. */..      if(
447c7 20 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46 54   pKey->eType==FT
447c8 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0d 0a  SQUERY_NEAR ){..
447c9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
447ca 6e 4b 65 79 3d 3d 34 20 29 3b 0d 0a 20 20 20 20  nKey==4 );..    
447cb 20 20 20 20 69 66 28 20 7a 49 6e 70 75 74 5b 34      if( zInput[4
447cc 5d 3d 3d 27 2f 27 20 26 26 20 7a 49 6e 70 75 74  ]=='/' && zInput
447cd 5b 35 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70  [5]>='0' && zInp
447ce 75 74 5b 35 5d 3c 3d 27 39 27 20 29 7b 0d 0a 20  ut[5]<='9' ){.. 
447cf 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d           nNear =
447d0 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 66   0;..          f
447d1 6f 72 28 6e 4b 65 79 3d 35 3b 20 7a 49 6e 70 75  or(nKey=5; zInpu
447d2 74 5b 6e 4b 65 79 5d 3e 3d 27 30 27 20 26 26 20  t[nKey]>='0' && 
447d3 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3c 3d 27 39  zInput[nKey]<='9
447d4 27 3b 20 6e 4b 65 79 2b 2b 29 7b 0d 0a 20 20 20  '; nKey++){..   
447d5 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d           nNear =
447d6 20 6e 4e 65 61 72 20 2a 20 31 30 20 2b 20 28 7a   nNear * 10 + (z
447d7 49 6e 70 75 74 5b 6e 4b 65 79 5d 20 2d 20 27 30  Input[nKey] - '0
447d8 27 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d  ');..          }
447d9 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
447da 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a     }....      /*
447db 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
447dc 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c 79 20  his is probably 
447dd 61 20 6b 65 79 77 6f 72 64 2e 20 42 75 74 20 66  a keyword. But f
447de 6f 72 20 74 68 61 74 20 74 6f 20 62 65 20 74 72  or that to be tr
447df 75 65 2c 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68  ue,..      ** th
447e0 65 20 6e 65 78 74 20 62 79 74 65 20 6d 75 73 74  e next byte must
447e1 20 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20   contain either 
447e2 77 68 69 74 65 73 70 61 63 65 2c 20 61 6e 20 6f  whitespace, an o
447e3 70 65 6e 20 6f 72 20 63 6c 6f 73 65 0d 0a 20 20  pen or close..  
447e4 20 20 20 20 2a 2a 20 70 61 72 65 6e 74 68 65 73      ** parenthes
447e5 69 73 2c 20 61 20 71 75 6f 74 65 20 63 68 61 72  is, a quote char
447e6 61 63 74 65 72 2c 20 6f 72 20 45 4f 46 2e 20 0d  acter, or EOF. .
447e7 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
447e8 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70 75 74 5b   cNext = zInput[
447e9 6e 4b 65 79 5d 3b 0d 0a 20 20 20 20 20 20 69 66  nKey];..      if
447ea 28 20 66 74 73 33 69 73 73 70 61 63 65 28 63 4e  ( fts3isspace(cN
447eb 65 78 74 29 20 0d 0a 20 20 20 20 20 20 20 7c 7c  ext) ..       ||
447ec 20 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20 63   cNext=='"' || c
447ed 4e 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e 65  Next=='(' || cNe
447ee 78 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78 74  xt==')' || cNext
447ef 3d 3d 30 0d 0a 20 20 20 20 20 20 29 7b 0d 0a 20  ==0..      ){.. 
447f0 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46         pRet = (F
447f1 74 73 33 45 78 70 72 20 2a 29 66 74 73 33 4d 61  ts3Expr *)fts3Ma
447f2 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28  llocZero(sizeof(
447f3 46 74 73 33 45 78 70 72 29 29 3b 0d 0a 20 20 20  Fts3Expr));..   
447f4 20 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29       if( !pRet )
447f5 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  {..          ret
447f6 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
447f7 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
447f8 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79 70        pRet->eTyp
447f9 65 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65 3b  e = pKey->eType;
447fa 0d 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ..        pRet->
447fb 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0d 0a  nNear = nNear;..
447fc 20 20 20 20 20 20 20 20 2a 70 70 45 78 70 72 20          *ppExpr 
447fd 3d 20 70 52 65 74 3b 0d 0a 20 20 20 20 20 20 20  = pRet;..       
447fe 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28   *pnConsumed = (
447ff 69 6e 74 29 28 28 7a 49 6e 70 75 74 20 2d 20 7a  int)((zInput - z
44800 29 20 2b 20 6e 4b 65 79 29 3b 0d 0a 20 20 20 20  ) + nKey);..    
44801 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
44802 45 5f 4f 4b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  E_OK;..      }..
44803 0d 0a 20 20 20 20 20 20 2f 2a 20 54 75 72 6e 73  ..      /* Turns
44804 20 6f 75 74 20 74 68 61 74 20 77 61 73 6e 27 74   out that wasn't
44805 20 61 20 6b 65 79 77 6f 72 64 20 61 66 74 65 72   a keyword after
44806 20 61 6c 6c 2e 20 54 68 69 73 20 68 61 70 70 65   all. This happe
44807 6e 73 20 69 66 20 74 68 65 0d 0a 20 20 20 20 20  ns if the..     
44808 20 2a 2a 20 75 73 65 72 20 68 61 73 20 73 75 70   ** user has sup
44809 70 6c 69 65 64 20 61 20 74 6f 6b 65 6e 20 73 75  plied a token su
4480a 63 68 20 61 73 20 22 4f 52 61 63 6c 65 22 2e 20  ch as "ORacle". 
4480b 43 6f 6e 74 69 6e 75 65 2e 0d 0a 20 20 20 20 20  Continue...     
4480c 20 2a 2f 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   */..    }..  }.
4480d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f  ...  /* Check fo
4480e 72 20 61 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65  r an open bracke
4480f 74 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c  t. */..  if( sql
44810 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
44811 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0d  _parentheses ){.
44812 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74  .    if( *zInput
44813 3d 3d 27 28 27 20 29 7b 0d 0a 20 20 20 20 20 20  =='(' ){..      
44814 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0d 0a  int nConsumed;..
44815 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4e        pParse->nN
44816 65 73 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 72 63  est++;..      rc
44817 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73 65   = fts3ExprParse
44818 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70 75 74  (pParse, &zInput
44819 5b 31 5d 2c 20 6e 49 6e 70 75 74 2d 31 2c 20 70  [1], nInput-1, p
4481a 70 45 78 70 72 2c 20 26 6e 43 6f 6e 73 75 6d 65  pExpr, &nConsume
4481b 64 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 72  d);..      if( r
4481c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
4481d 21 2a 70 70 45 78 70 72 20 29 7b 0d 0a 20 20 20  !*ppExpr ){..   
4481e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
4481f 5f 44 4f 4e 45 3b 0d 0a 20 20 20 20 20 20 7d 0d  _DONE;..      }.
44820 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  .      *pnConsum
44821 65 64 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70  ed = (int)((zInp
44822 75 74 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e 43  ut - z) + 1 + nC
44823 6f 6e 73 75 6d 65 64 29 3b 0d 0a 20 20 20 20 20  onsumed);..     
44824 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
44825 20 7d 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20 43   }..  ..    /* C
44826 68 65 63 6b 20 66 6f 72 20 61 20 63 6c 6f 73 65  heck for a close
44827 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0d 0a 20 20   bracket. */..  
44828 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27    if( *zInput=='
44829 29 27 20 29 7b 0d 0a 20 20 20 20 20 20 70 50 61  )' ){..      pPa
4482a 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b 0d 0a 20  rse->nNest--;.. 
4482b 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64       *pnConsumed
4482c 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75 74   = (int)((zInput
4482d 20 2d 20 7a 29 20 2b 20 31 29 3b 0d 0a 20 20 20   - z) + 1);..   
4482e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
4482f 5f 44 4f 4e 45 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _DONE;..    }.. 
44830 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 65 20 69   }....  /* See i
44831 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
44832 20 77 69 74 68 20 61 20 71 75 6f 74 65 64 20 70   with a quoted p
44833 68 72 61 73 65 2e 20 49 66 20 74 68 69 73 20 69  hrase. If this i
44834 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
44835 0d 0a 20 20 2a 2a 20 73 65 61 72 63 68 20 66 6f  ..  ** search fo
44836 72 20 74 68 65 20 63 6c 6f 73 69 6e 67 20 71 75  r the closing qu
44837 6f 74 65 20 61 6e 64 20 70 61 73 73 20 74 68 65  ote and pass the
44838 20 77 68 6f 6c 65 20 73 74 72 69 6e 67 20 74 6f   whole string to
44839 20 67 65 74 4e 65 78 74 53 74 72 69 6e 67 28 29   getNextString()
4483a 0d 0a 20 20 2a 2a 20 66 6f 72 20 70 72 6f 63 65  ..  ** for proce
4483b 73 73 69 6e 67 2e 20 54 68 69 73 20 69 73 20 65  ssing. This is e
4483c 61 73 79 20 74 6f 20 64 6f 2c 20 61 73 20 66 74  asy to do, as ft
4483d 73 33 20 68 61 73 20 6e 6f 20 73 79 6e 74 61 78  s3 has no syntax
4483e 20 66 6f 72 20 65 73 63 61 70 69 6e 67 0d 0a 20   for escaping.. 
4483f 20 2a 2a 20 61 20 71 75 6f 74 65 20 63 68 61 72   ** a quote char
44840 61 63 74 65 72 20 65 6d 62 65 64 64 65 64 20 69  acter embedded i
44841 6e 20 61 20 73 74 72 69 6e 67 2e 0d 0a 20 20 2a  n a string...  *
44842 2f 0d 0a 20 20 69 66 28 20 2a 7a 49 6e 70 75 74  /..  if( *zInput
44843 3d 3d 27 22 27 20 29 7b 0d 0a 20 20 20 20 66 6f  =='"' ){..    fo
44844 72 28 69 69 3d 31 3b 20 69 69 3c 6e 49 6e 70 75  r(ii=1; ii<nInpu
44845 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 69 5d 21  t && zInput[ii]!
44846 3d 27 22 27 3b 20 69 69 2b 2b 29 3b 0d 0a 20 20  ='"'; ii++);..  
44847 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20    *pnConsumed = 
44848 28 69 6e 74 29 28 28 7a 49 6e 70 75 74 20 2d 20  (int)((zInput - 
44849 7a 29 20 2b 20 69 69 20 2b 20 31 29 3b 0d 0a 20  z) + ii + 1);.. 
4484a 20 20 20 69 66 28 20 69 69 3d 3d 6e 49 6e 70 75     if( ii==nInpu
4484b 74 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  t ){..      retu
4484c 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
4484d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 65 74  ..    }..    ret
4484e 75 72 6e 20 67 65 74 4e 65 78 74 53 74 72 69 6e  urn getNextStrin
4484f 67 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70 75  g(pParse, &zInpu
44850 74 5b 31 5d 2c 20 69 69 2d 31 2c 20 70 70 45 78  t[1], ii-1, ppEx
44851 70 72 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20  pr);..  }...... 
44852 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66   /* If control f
44853 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69  lows to this poi
44854 6e 74 2c 20 74 68 69 73 20 6d 75 73 74 20 62 65  nt, this must be
44855 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e   a regular token
44856 2c 20 6f 72 20 0d 0a 20 20 2a 2a 20 74 68 65 20  , or ..  ** the 
44857 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75 74  end of the input
44858 2e 20 52 65 61 64 20 61 20 72 65 67 75 6c 61 72  . Read a regular
44859 20 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74 68 65   token using the
4485a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
4485b 65 72 0d 0a 20 20 2a 2a 20 69 6e 74 65 72 66 61  er..  ** interfa
4485c 63 65 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67  ce. Before doing
4485d 20 73 6f 2c 20 66 69 67 75 72 65 20 6f 75 74 20   so, figure out 
4485e 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  if there is an e
4485f 78 70 6c 69 63 69 74 0d 0a 20 20 2a 2a 20 63 6f  xplicit..  ** co
44860 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72 20 66  lumn specifier f
44861 6f 72 20 74 68 65 20 74 6f 6b 65 6e 2e 20 0d 0a  or the token. ..
44862 20 20 2a 2a 0d 0a 20 20 2a 2a 20 54 4f 44 4f 3a    **..  ** TODO:
44863 20 53 74 72 61 6e 67 65 6c 79 2c 20 69 74 20 69   Strangely, it i
44864 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
44865 6f 20 61 73 73 6f 63 69 61 74 65 20 61 20 63 6f  o associate a co
44866 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72 0d 0a  lumn specifier..
44867 20 20 2a 2a 20 77 69 74 68 20 61 20 71 75 6f 74    ** with a quot
44868 65 64 20 70 68 72 61 73 65 2c 20 6f 6e 6c 79 20  ed phrase, only 
44869 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 6f  with a single to
4486a 6b 65 6e 2e 20 4e 6f 74 20 73 75 72 65 20 69 66  ken. Not sure if
4486b 20 74 68 69 73 20 77 61 73 0d 0a 20 20 2a 2a 20   this was..  ** 
4486c 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
4486d 6e 20 61 72 74 69 66 61 63 74 20 6f 72 20 61 6e  n artifact or an
4486e 20 69 6e 74 65 6e 74 69 6f 6e 61 6c 20 64 65 63   intentional dec
4486f 69 73 69 6f 6e 20 77 68 65 6e 20 66 74 73 33 20  ision when fts3 
44870 77 61 73 0d 0a 20 20 2a 2a 20 66 69 72 73 74 20  was..  ** first 
44871 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 57 68 69  implemented. Whi
44872 63 68 65 76 65 72 20 69 74 20 77 61 73 2c 20 74  chever it was, t
44873 68 69 73 20 6d 6f 64 75 6c 65 20 64 75 70 6c 69  his module dupli
44874 63 61 74 65 73 20 74 68 65 20 0d 0a 20 20 2a 2a  cates the ..  **
44875 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0d 0a 20 20   limitation...  
44876 2a 2f 0d 0a 20 20 69 43 6f 6c 20 3d 20 70 50 61  */..  iCol = pPa
44877 72 73 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c  rse->iDefaultCol
44878 3b 0d 0a 20 20 69 43 6f 6c 4c 65 6e 20 3d 20 30  ;..  iColLen = 0
44879 3b 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
4487a 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 3b 20  i<pParse->nCol; 
4487b 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 63 6f 6e 73  ii++){..    cons
4487c 74 20 63 68 61 72 20 2a 7a 53 74 72 20 3d 20 70  t char *zStr = p
4487d 50 61 72 73 65 2d 3e 61 7a 43 6f 6c 5b 69 69 5d  Parse->azCol[ii]
4487e 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 53 74 72 20  ;..    int nStr 
4487f 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 53  = (int)strlen(zS
44880 74 72 29 3b 0d 0a 20 20 20 20 69 66 28 20 6e 49  tr);..    if( nI
44881 6e 70 75 74 3e 6e 53 74 72 20 26 26 20 7a 49 6e  nput>nStr && zIn
44882 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20 0d  put[nStr]==':' .
44883 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33  .     && sqlite3
44884 5f 73 74 72 6e 69 63 6d 70 28 7a 53 74 72 2c 20  _strnicmp(zStr, 
44885 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d 3d 30  zInput, nStr)==0
44886 20 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20   ..    ){..     
44887 20 69 43 6f 6c 20 3d 20 69 69 3b 0d 0a 20 20 20   iCol = ii;..   
44888 20 20 20 69 43 6f 6c 4c 65 6e 20 3d 20 28 69 6e     iColLen = (in
44889 74 29 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20  t)((zInput - z) 
4488a 2b 20 6e 53 74 72 20 2b 20 31 29 3b 0d 0a 20 20  + nStr + 1);..  
4488b 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
4488c 7d 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 67  }..  }..  rc = g
4488d 65 74 4e 65 78 74 54 6f 6b 65 6e 28 70 50 61 72  etNextToken(pPar
4488e 73 65 2c 20 69 43 6f 6c 2c 20 26 7a 5b 69 43 6f  se, iCol, &z[iCo
4488f 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f 6c 4c 65 6e  lLen], n-iColLen
44890 2c 20 70 70 45 78 70 72 2c 20 70 6e 43 6f 6e 73  , ppExpr, pnCons
44891 75 6d 65 64 29 3b 0d 0a 20 20 2a 70 6e 43 6f 6e  umed);..  *pnCon
44892 73 75 6d 65 64 20 2b 3d 20 69 43 6f 6c 4c 65 6e  sumed += iColLen
44893 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
44894 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65  .}..../*..** The
44895 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
44896 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
44897 72 65 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  re for a binary 
44898 6f 70 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79  operator (any ty
44899 70 65 0d 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e  pe..** except an
4489a 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
4489b 29 2e 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74  ). Return an int
4489c 65 67 65 72 20 76 61 6c 75 65 20 72 65 70 72 65  eger value repre
4489d 73 65 6e 74 69 6e 67 20 74 68 65 0d 0a 2a 2a 20  senting the..** 
4489e 70 72 65 63 65 64 65 6e 63 65 20 6f 66 20 74 68  precedence of th
4489f 65 20 6f 70 65 72 61 74 6f 72 2e 20 4c 6f 77 65  e operator. Lowe
448a0 72 20 76 61 6c 75 65 73 20 68 61 76 65 20 61 20  r values have a 
448a1 68 69 67 68 65 72 20 70 72 65 63 65 64 65 6e 63  higher precedenc
448a2 65 20 28 69 2e 65 2e 0d 0a 2a 2a 20 67 72 6f 75  e (i.e...** grou
448a3 70 20 6d 6f 72 65 20 74 69 67 68 74 6c 79 29 2e  p more tightly).
448a4 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e   For example, in
448a5 20 74 68 65 20 43 20 6c 61 6e 67 75 61 67 65 2c   the C language,
448a6 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72   the == operator
448a7 0d 0a 2a 2a 20 67 72 6f 75 70 73 20 6d 6f 72 65  ..** groups more
448a8 20 74 69 67 68 74 6c 79 20 74 68 61 6e 20 7c 7c   tightly than ||
448a9 2c 20 61 6e 64 20 77 6f 75 6c 64 20 74 68 65 72  , and would ther
448aa 65 66 6f 72 65 20 68 61 76 65 20 61 20 68 69 67  efore have a hig
448ab 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 2e 0d  her precedence..
448ac 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 75 73 69  .**..** When usi
448ad 6e 67 20 74 68 65 20 6e 65 77 20 66 74 73 33 20  ng the new fts3 
448ae 71 75 65 72 79 20 73 79 6e 74 61 78 20 28 77 68  query syntax (wh
448af 65 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  en SQLITE_ENABLE
448b0 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49  _FTS3_PARENTHESI
448b1 53 0d 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  S..** is defined
448b2 29 2c 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  ), the order of 
448b3 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 69 6e  the operators in
448b4 20 70 72 65 63 65 64 65 6e 63 65 20 66 72 6f 6d   precedence from
448b5 20 68 69 67 68 65 73 74 20 74 6f 0d 0a 2a 2a 20   highest to..** 
448b6 6c 6f 77 65 73 74 20 69 73 3a 0d 0a 2a 2a 0d 0a  lowest is:..**..
448b7 2a 2a 20 20 20 4e 45 41 52 0d 0a 2a 2a 20 20 20  **   NEAR..**   
448b8 4e 4f 54 0d 0a 2a 2a 20 20 20 41 4e 44 20 28 69  NOT..**   AND (i
448b9 6e 63 6c 75 64 69 6e 67 20 69 6d 70 6c 69 63 69  ncluding implici
448ba 74 20 41 4e 44 73 29 0d 0a 2a 2a 20 20 20 4f 52  t ANDs)..**   OR
448bb 0d 0a 2a 2a 0d 0a 2a 2a 20 4e 6f 74 65 20 74 68  ..**..** Note th
448bc 61 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68  at when using th
448bd 65 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e 74  e old query synt
448be 61 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61  ax, the OR opera
448bf 74 6f 72 20 68 61 73 20 61 20 68 69 67 68 65 72  tor has a higher
448c0 0d 0a 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20  ..** precedence 
448c1 74 68 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65  than the AND ope
448c2 72 61 74 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  rator...*/..stat
448c3 69 63 20 69 6e 74 20 6f 70 50 72 65 63 65 64 65  ic int opPrecede
448c4 6e 63 65 28 46 74 73 33 45 78 70 72 20 2a 70 29  nce(Fts3Expr *p)
448c5 7b 0d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  {..  assert( p->
448c6 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f  eType!=FTSQUERY_
448c7 50 48 52 41 53 45 20 29 3b 0d 0a 20 20 69 66 28  PHRASE );..  if(
448c8 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
448c9 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
448ca 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
448cb 70 2d 3e 65 54 79 70 65 3b 0d 0a 20 20 7d 65 6c  p->eType;..  }el
448cc 73 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d  se if( p->eType=
448cd 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29  =FTSQUERY_NEAR )
448ce 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  {..    return 1;
448cf 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ..  }else if( p-
448d0 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
448d1 5f 4f 52 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  _OR ){..    retu
448d2 72 6e 20 32 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73  rn 2;..  }..  as
448d3 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65 3d 3d  sert( p->eType==
448d4 46 54 53 51 55 45 52 59 5f 41 4e 44 20 29 3b 0d  FTSQUERY_AND );.
448d5 0a 20 20 72 65 74 75 72 6e 20 33 3b 0d 0a 7d 0d  .  return 3;..}.
448d6 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 72 67 75 6d 65  .../*..** Argume
448d7 6e 74 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69  nt ppHead contai
448d8 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
448d9 74 68 65 20 63 75 72 72 65 6e 74 20 68 65 61 64  the current head
448da 20 6f 66 20 61 20 71 75 65 72 79 20 0d 0a 2a 2a   of a query ..**
448db 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
448dc 20 62 65 69 6e 67 20 70 61 72 73 65 64 2e 20 70   being parsed. p
448dd 50 72 65 76 20 69 73 20 74 68 65 20 65 78 70 72  Prev is the expr
448de 65 73 73 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74  ession node most
448df 20 72 65 63 65 6e 74 6c 79 0d 0a 2a 2a 20 69 6e   recently..** in
448e0 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
448e1 74 72 65 65 2e 20 54 68 69 73 20 66 75 6e 63 74  tree. This funct
448e2 69 6f 6e 20 61 64 64 73 20 70 4e 65 77 2c 20 77  ion adds pNew, w
448e3 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 20 61  hich is always a
448e4 20 62 69 6e 61 72 79 0d 0a 2a 2a 20 6f 70 65 72   binary..** oper
448e5 61 74 6f 72 20 6e 6f 64 65 2c 20 69 6e 74 6f 20  ator node, into 
448e6 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
448e7 72 65 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ree based on the
448e8 20 72 65 6c 61 74 69 76 65 20 70 72 65 63 65 64   relative preced
448e9 65 6e 63 65 0d 0a 2a 2a 20 6f 66 20 70 4e 65 77  ence..** of pNew
448ea 20 61 6e 64 20 74 68 65 20 65 78 69 73 74 69 6e   and the existin
448eb 67 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 20 74  g nodes of the t
448ec 72 65 65 2e 20 54 68 69 73 20 6d 61 79 20 72 65  ree. This may re
448ed 73 75 6c 74 20 69 6e 20 74 68 65 20 68 65 61 64  sult in the head
448ee 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 74 72 65 65  ..** of the tree
448ef 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e 20 77 68   changing, in wh
448f0 69 63 68 20 63 61 73 65 20 2a 70 70 48 65 61 64  ich case *ppHead
448f1 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
448f2 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e 0d 0a 2a  ew root node...*
448f3 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  /..static void i
448f4 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61  nsertBinaryOpera
448f5 74 6f 72 28 0d 0a 20 20 46 74 73 33 45 78 70 72  tor(..  Fts3Expr
448f6 20 2a 2a 70 70 48 65 61 64 2c 20 20 20 20 20 20   **ppHead,      
448f7 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
448f8 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
448f9 61 20 74 72 65 65 20 2a 2f 0d 0a 20 20 46 74 73  a tree */..  Fts
448fa 33 45 78 70 72 20 2a 70 50 72 65 76 2c 20 20 20  3Expr *pPrev,   
448fb 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f        /* Node mo
448fc 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65  st recently inse
448fd 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
448fe 65 65 20 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70  ee */..  Fts3Exp
448ff 72 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  r *pNew         
44900 20 20 2f 2a 20 4e 65 77 20 62 69 6e 61 72 79 20    /* New binary 
44901 6e 6f 64 65 20 74 6f 20 69 6e 73 65 72 74 20 69  node to insert i
44902 6e 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  nto expression t
44903 72 65 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46 74  ree */..){..  Ft
44904 73 33 45 78 70 72 20 2a 70 53 70 6c 69 74 20 3d  s3Expr *pSplit =
44905 20 70 50 72 65 76 3b 0d 0a 20 20 77 68 69 6c 65   pPrev;..  while
44906 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  ( pSplit->pParen
44907 74 20 26 26 20 6f 70 50 72 65 63 65 64 65 6e 63  t && opPrecedenc
44908 65 28 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  e(pSplit->pParen
44909 74 29 3c 3d 6f 70 50 72 65 63 65 64 65 6e 63 65  t)<=opPrecedence
4490a 28 70 4e 65 77 29 20 29 7b 0d 0a 20 20 20 20 70  (pNew) ){..    p
4490b 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74 2d 3e  Split = pSplit->
4490c 70 50 61 72 65 6e 74 3b 0d 0a 20 20 7d 0d 0a 0d  pParent;..  }...
4490d 0a 20 20 69 66 28 20 70 53 70 6c 69 74 2d 3e 70  .  if( pSplit->p
4490e 50 61 72 65 6e 74 20 29 7b 0d 0a 20 20 20 20 61  Parent ){..    a
4490f 73 73 65 72 74 28 20 70 53 70 6c 69 74 2d 3e 70  ssert( pSplit->p
44910 50 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d  Parent->pRight==
44911 70 53 70 6c 69 74 20 29 3b 0d 0a 20 20 20 20 70  pSplit );..    p
44912 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e  Split->pParent->
44913 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b 0d 0a  pRight = pNew;..
44914 20 20 20 20 70 4e 65 77 2d 3e 70 50 61 72 65 6e      pNew->pParen
44915 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  t = pSplit->pPar
44916 65 6e 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  ent;..  }else{..
44917 20 20 20 20 2a 70 70 48 65 61 64 20 3d 20 70 4e      *ppHead = pN
44918 65 77 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4e 65 77  ew;..  }..  pNew
44919 2d 3e 70 4c 65 66 74 20 3d 20 70 53 70 6c 69 74  ->pLeft = pSplit
4491a 3b 0d 0a 20 20 70 53 70 6c 69 74 2d 3e 70 50 61  ;..  pSplit->pPa
4491b 72 65 6e 74 20 3d 20 70 4e 65 77 3b 0d 0a 7d 0d  rent = pNew;..}.
4491c 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 73 65 20  .../*..** Parse 
4491d 74 68 65 20 66 74 73 33 20 71 75 65 72 79 20 65  the fts3 query e
4491e 78 70 72 65 73 73 69 6f 6e 20 66 6f 75 6e 64 20  xpression found 
4491f 69 6e 20 62 75 66 66 65 72 20 7a 2c 20 6c 65 6e  in buffer z, len
44920 67 74 68 20 6e 2e 20 54 68 69 73 20 66 75 6e 63  gth n. This func
44921 74 69 6f 6e 0d 0a 2a 2a 20 72 65 74 75 72 6e 73  tion..** returns
44922 20 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65   either when the
44923 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66   end of the buff
44924 65 72 20 69 73 20 72 65 61 63 68 65 64 20 6f 72  er is reached or
44925 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20 0d 0a   an unmatched ..
44926 2a 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63 6b  ** closing brack
44927 65 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65 6e  et - ')' - is en
44928 63 6f 75 6e 74 65 72 65 64 2e 0d 0a 2a 2a 0d 0a  countered...**..
44929 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
4492a 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
4492b 65 74 75 72 6e 65 64 2c 20 2a 70 70 45 78 70 72  eturned, *ppExpr
4492c 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
4492d 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 70 61 72 73   to the..** pars
4492e 65 64 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 65  ed form of the e
4492f 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 2a 70  xpression and *p
44930 6e 43 6f 6e 73 75 6d 65 64 20 69 73 20 73 65 74  nConsumed is set
44931 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
44932 66 0d 0a 2a 2a 20 62 79 74 65 73 20 72 65 61 64  f..** bytes read
44933 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a 2e 20   from buffer z. 
44934 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70 45 78  Otherwise, *ppEx
44935 70 72 20 69 73 20 73 65 74 20 74 6f 20 30 20 61  pr is set to 0 a
44936 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0d  nd SQLITE_NOMEM.
44937 0a 2a 2a 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f  .** (out of memo
44938 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51 4c  ry error) or SQL
44939 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73 65  ITE_ERROR (parse
4493a 20 65 72 72 6f 72 29 20 69 73 20 72 65 74 75 72   error) is retur
4493b 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ned...*/..static
4493c 20 69 6e 74 20 66 74 73 33 45 78 70 72 50 61 72   int fts3ExprPar
4493d 73 65 28 0d 0a 20 20 50 61 72 73 65 43 6f 6e 74  se(..  ParseCont
4493e 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20  ext *pParse,    
4493f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44940 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61 72  * fts3 query par
44941 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20  se context */.. 
44942 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
44943 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
44944 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
44945 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72 79 20   of MATCH query 
44946 2a 2f 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a  */..  Fts3Expr *
44947 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20  *ppExpr,        
44948 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44949 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75 65   OUT: Parsed que
4494a 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0d  ry structure */.
4494b 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
4494c 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
4494d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
4494e 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
4494f 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0d 0a  es consumed */..
44950 29 7b 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a  ){..  Fts3Expr *
44951 70 52 65 74 20 3d 20 30 3b 0d 0a 20 20 46 74 73  pRet = 0;..  Fts
44952 33 45 78 70 72 20 2a 70 50 72 65 76 20 3d 20 30  3Expr *pPrev = 0
44953 3b 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  ;..  Fts3Expr *p
44954 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b 20 20  NotBranch = 0;  
44955 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44956 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c 65 67  Only used in leg
44957 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65 20 2a  acy parse mode *
44958 2f 0d 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20 6e  /..  int nIn = n
44959 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ;..  const char 
4495a 2a 7a 49 6e 20 3d 20 7a 3b 0d 0a 20 20 69 6e 74  *zIn = z;..  int
4495b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
4495c 0d 0a 20 20 69 6e 74 20 69 73 52 65 71 75 69 72  ..  int isRequir
4495d 65 50 68 72 61 73 65 20 3d 20 31 3b 0d 0a 0d 0a  ePhrase = 1;....
4495e 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
4495f 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 46  ITE_OK ){..    F
44960 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0d  ts3Expr *p = 0;.
44961 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  .    int nByte =
44962 20 30 3b 0d 0a 20 20 20 20 72 63 20 3d 20 67 65   0;..    rc = ge
44963 74 4e 65 78 74 4e 6f 64 65 28 70 50 61 72 73 65  tNextNode(pParse
44964 2c 20 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c 20  , zIn, nIn, &p, 
44965 26 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 69 66  &nByte);..    if
44966 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
44967 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 73  ){..      int is
44968 50 68 72 61 73 65 3b 0d 0a 0d 0a 20 20 20 20 20  Phrase;....     
44969 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74   if( !sqlite3_ft
4496a 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74  s3_enable_parent
4496b 68 65 73 65 73 20 0d 0a 20 20 20 20 20 20 20 26  heses ..       &
4496c 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  & p->eType==FTSQ
4496d 55 45 52 59 5f 50 48 52 41 53 45 20 26 26 20 70  UERY_PHRASE && p
4496e 50 61 72 73 65 2d 3e 69 73 4e 6f 74 20 0d 0a 20  Parse->isNot .. 
4496f 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20       ){..       
44970 20 2f 2a 20 43 72 65 61 74 65 20 61 6e 20 69 6d   /* Create an im
44971 70 6c 69 63 69 74 20 4e 4f 54 20 6f 70 65 72 61  plicit NOT opera
44972 74 6f 72 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20  tor. */..       
44973 20 46 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 20   Fts3Expr *pNot 
44974 3d 20 66 74 73 33 4d 61 6c 6c 6f 63 5a 65 72 6f  = fts3MallocZero
44975 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
44976 29 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  ));..        if(
44977 20 21 70 4e 6f 74 20 29 7b 0d 0a 20 20 20 20 20   !pNot ){..     
44978 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
44979 45 78 70 72 46 72 65 65 28 70 29 3b 0d 0a 20 20  ExprFree(p);..  
4497a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
4497b 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
4497c 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
4497d 61 72 73 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 20  arse_out;..     
4497e 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 70 4e     }..        pN
4497f 6f 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51  ot->eType = FTSQ
44980 55 45 52 59 5f 4e 4f 54 3b 0d 0a 20 20 20 20 20  UERY_NOT;..     
44981 20 20 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20     pNot->pRight 
44982 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20 20 69 66  = p;..        if
44983 28 20 70 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0d  ( pNotBranch ){.
44984 0a 20 20 20 20 20 20 20 20 20 20 70 4e 6f 74 2d  .          pNot-
44985 3e 70 4c 65 66 74 20 3d 20 70 4e 6f 74 42 72 61  >pLeft = pNotBra
44986 6e 63 68 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  nch;..        }.
44987 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 42 72 61  .        pNotBra
44988 6e 63 68 20 3d 20 70 4e 6f 74 3b 0d 0a 20 20 20  nch = pNot;..   
44989 20 20 20 20 20 70 20 3d 20 70 50 72 65 76 3b 0d       p = pPrev;.
4498a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
4498b 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 65         int eType
4498c 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0d 0a 20 20   = p->eType;..  
4498d 20 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d        isPhrase =
4498e 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52   (eType==FTSQUER
4498f 59 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70  Y_PHRASE || p->p
44990 4c 65 66 74 29 3b 0d 0a 0d 0a 20 20 20 20 20 20  Left);....      
44991 20 20 2f 2a 20 54 68 65 20 69 73 52 65 71 75 69    /* The isRequi
44992 72 65 50 68 72 61 73 65 20 76 61 72 69 61 62 6c  rePhrase variabl
44993 65 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  e is set to true
44994 20 69 66 20 61 20 70 68 72 61 73 65 20 6f 72 0d   if a phrase or.
44995 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65  .        ** an e
44996 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69  xpression contai
44997 6e 65 64 20 69 6e 20 70 61 72 65 6e 74 68 65 73  ned in parenthes
44998 69 73 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  is is required. 
44999 49 66 20 61 0d 0a 20 20 20 20 20 20 20 20 2a 2a  If a..        **
4499a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f 72   binary operator
4499b 20 28 41 4e 44 2c 20 4f 52 2c 20 4e 4f 54 20 6f   (AND, OR, NOT o
4499c 72 20 4e 45 41 52 29 20 69 73 20 65 6e 63 6f 75  r NEAR) is encou
4499d 6e 74 65 64 20 77 68 65 6e 0d 0a 20 20 20 20 20  nted when..     
4499e 20 20 20 2a 2a 20 69 73 52 65 71 75 69 72 65 50     ** isRequireP
4499f 68 72 61 73 65 20 69 73 20 73 65 74 2c 20 74 68  hrase is set, th
449a0 69 73 20 69 73 20 61 20 73 79 6e 74 61 78 20 65  is is a syntax e
449a1 72 72 6f 72 2e 0d 0a 20 20 20 20 20 20 20 20 2a  rror...        *
449a2 2f 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  /..        if( !
449a3 69 73 50 68 72 61 73 65 20 26 26 20 69 73 52 65  isPhrase && isRe
449a4 71 75 69 72 65 50 68 72 61 73 65 20 29 7b 0d 0a  quirePhrase ){..
449a5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
449a6 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 29  3Fts3ExprFree(p)
449a7 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ;..          rc 
449a8 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  = SQLITE_ERROR;.
449a9 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
449aa 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0d 0a  exprparse_out;..
449ab 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 0d 0a 20          }..  .. 
449ac 20 20 20 20 20 20 20 69 66 28 20 69 73 50 68 72         if( isPhr
449ad 61 73 65 20 26 26 20 21 69 73 52 65 71 75 69 72  ase && !isRequir
449ae 65 50 68 72 61 73 65 20 29 7b 0d 0a 20 20 20 20  ePhrase ){..    
449af 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20        /* Insert 
449b0 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 20  an implicit AND 
449b1 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0d 0a 20 20  operator. */..  
449b2 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
449b3 20 2a 70 41 6e 64 3b 0d 0a 20 20 20 20 20 20 20   *pAnd;..       
449b4 20 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20     assert( pRet 
449b5 26 26 20 70 50 72 65 76 20 29 3b 0d 0a 20 20 20  && pPrev );..   
449b6 20 20 20 20 20 20 20 70 41 6e 64 20 3d 20 66 74         pAnd = ft
449b7 73 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  s3MallocZero(siz
449b8 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0d  eof(Fts3Expr));.
449b9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
449ba 70 41 6e 64 20 29 7b 0d 0a 20 20 20 20 20 20 20  pAnd ){..       
449bb 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
449bc 45 78 70 72 46 72 65 65 28 70 29 3b 0d 0a 20 20  ExprFree(p);..  
449bd 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
449be 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
449bf 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
449c0 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0d 0a 20  xprparse_out;.. 
449c1 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
449c2 20 20 20 20 20 20 70 41 6e 64 2d 3e 65 54 79 70        pAnd->eTyp
449c3 65 20 3d 20 46 54 53 51 55 45 52 59 5f 41 4e 44  e = FTSQUERY_AND
449c4 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 69 6e 73  ;..          ins
449c5 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
449c6 72 28 26 70 52 65 74 2c 20 70 50 72 65 76 2c 20  r(&pRet, pPrev, 
449c7 70 41 6e 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  pAnd);..        
449c8 20 20 70 50 72 65 76 20 3d 20 70 41 6e 64 3b 0d    pPrev = pAnd;.
449c9 0a 20 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20  .        }....  
449ca 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65        /* This te
449cb 73 74 20 63 61 74 63 68 65 73 20 61 74 74 65 6d  st catches attem
449cc 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69 74 68  pts to make eith
449cd 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  er operand of a 
449ce 4e 45 41 52 0d 0a 20 20 20 20 20 20 20 20 2a 2a  NEAR..        **
449cf 20 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74 68   operator someth
449d0 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ing other than a
449d1 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61   phrase. For exa
449d2 6d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66 0d  mple, either of.
449d3 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
449d4 66 6f 6c 6c 6f 77 69 6e 67 3a 0d 0a 20 20 20 20  following:..    
449d5 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20      **..        
449d6 2a 2a 20 20 20 20 28 62 72 61 63 6b 65 74 65 64  **    (bracketed
449d7 20 65 78 70 72 65 73 73 69 6f 6e 29 20 4e 45 41   expression) NEA
449d8 52 20 70 68 72 61 73 65 0d 0a 20 20 20 20 20 20  R phrase..      
449d9 20 20 2a 2a 20 20 20 20 70 68 72 61 73 65 20 4e    **    phrase N
449da 45 41 52 20 28 62 72 61 63 6b 65 74 65 64 20 65  EAR (bracketed e
449db 78 70 72 65 73 73 69 6f 6e 29 0d 0a 20 20 20 20  xpression)..    
449dc 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 20 20      **..        
449dd 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
449de 6f 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73  or in either cas
449df 65 2e 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a  e...        */..
449e0 20 20 20 20 20 20 20 20 69 66 28 20 70 50 72 65          if( pPre
449e1 76 20 26 26 20 28 0d 0a 20 20 20 20 20 20 20 20  v && (..        
449e2 20 20 20 20 28 65 54 79 70 65 3d 3d 46 54 53 51      (eType==FTSQ
449e3 55 45 52 59 5f 4e 45 41 52 20 26 26 20 21 69 73  UERY_NEAR && !is
449e4 50 68 72 61 73 65 20 26 26 20 70 50 72 65 76 2d  Phrase && pPrev-
449e5 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59  >eType!=FTSQUERY
449e6 5f 50 48 52 41 53 45 29 0d 0a 20 20 20 20 20 20  _PHRASE)..      
449e7 20 20 20 7c 7c 20 28 65 54 79 70 65 21 3d 46 54     || (eType!=FT
449e8 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26  SQUERY_PHRASE &&
449e9 20 69 73 50 68 72 61 73 65 20 26 26 20 70 50 72   isPhrase && pPr
449ea 65 76 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ev->eType==FTSQU
449eb 45 52 59 5f 4e 45 41 52 29 0d 0a 20 20 20 20 20  ERY_NEAR)..     
449ec 20 20 20 29 29 7b 0d 0a 20 20 20 20 20 20 20 20     )){..        
449ed 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
449ee 72 46 72 65 65 28 70 29 3b 0d 0a 20 20 20 20 20  rFree(p);..     
449ef 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
449f0 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 20 20 20  _ERROR;..       
449f1 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73     goto exprpars
449f2 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 20 20 20 20  e_out;..        
449f3 7d 0d 0a 20 20 0d 0a 20 20 20 20 20 20 20 20 69  }..  ..        i
449f4 66 28 20 69 73 50 68 72 61 73 65 20 29 7b 0d 0a  f( isPhrase ){..
449f5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52            if( pR
449f6 65 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  et ){..         
449f7 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76     assert( pPrev
449f8 20 26 26 20 70 50 72 65 76 2d 3e 70 4c 65 66 74   && pPrev->pLeft
449f9 20 26 26 20 70 50 72 65 76 2d 3e 70 52 69 67 68   && pPrev->pRigh
449fa 74 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 20 20 20  t==0 );..       
449fb 20 20 20 20 20 70 50 72 65 76 2d 3e 70 52 69 67       pPrev->pRig
449fc 68 74 20 3d 20 70 3b 0d 0a 20 20 20 20 20 20 20  ht = p;..       
449fd 20 20 20 20 20 70 2d 3e 70 50 61 72 65 6e 74 20       p->pParent 
449fe 3d 20 70 50 72 65 76 3b 0d 0a 20 20 20 20 20 20  = pPrev;..      
449ff 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
44a00 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 70          pRet = p
44a01 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
44a02 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a          }else{..
44a03 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74            insert
44a04 42 69 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 26  BinaryOperator(&
44a05 70 52 65 74 2c 20 70 50 72 65 76 2c 20 70 29 3b  pRet, pPrev, p);
44a06 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
44a07 20 20 20 20 20 69 73 52 65 71 75 69 72 65 50 68       isRequirePh
44a08 72 61 73 65 20 3d 20 21 69 73 50 68 72 61 73 65  rase = !isPhrase
44a09 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
44a0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e    assert( nByte>
44a0b 30 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  0 );..    }..   
44a0c 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
44a0d 49 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74 65  ITE_OK || (nByte
44a0e 3e 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49 6e  >0 && nByte<=nIn
44a0f 29 20 29 3b 0d 0a 20 20 20 20 6e 49 6e 20 2d 3d  ) );..    nIn -=
44a10 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20 7a 49 6e   nByte;..    zIn
44a11 20 2b 3d 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20   += nByte;..    
44a12 70 50 72 65 76 20 3d 20 70 3b 0d 0a 20 20 7d 0d  pPrev = p;..  }.
44a13 0a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ...  if( rc==SQL
44a14 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 52 65 74  ITE_DONE && pRet
44a15 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
44a16 61 73 65 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ase ){..    rc =
44a17 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a   SQLITE_ERROR;..
44a18 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 72 63 3d    }....  if( rc=
44a19 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0d  =SQLITE_DONE ){.
44a1a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
44a1b 5f 4f 4b 3b 0d 0a 20 20 20 20 69 66 28 20 21 73  _OK;..    if( !s
44a1c 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62  qlite3_fts3_enab
44a1d 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 26  le_parentheses &
44a1e 26 20 70 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0d  & pNotBranch ){.
44a1f 0a 20 20 20 20 20 20 69 66 28 20 21 70 52 65 74  .      if( !pRet
44a20 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
44a21 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  = SQLITE_ERROR;.
44a22 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  .      }else{.. 
44a23 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20         Fts3Expr 
44a24 2a 70 49 74 65 72 20 3d 20 70 4e 6f 74 42 72 61  *pIter = pNotBra
44a25 6e 63 68 3b 0d 0a 20 20 20 20 20 20 20 20 77 68  nch;..        wh
44a26 69 6c 65 28 20 70 49 74 65 72 2d 3e 70 4c 65 66  ile( pIter->pLef
44a27 74 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  t ){..          
44a28 70 49 74 65 72 20 3d 20 70 49 74 65 72 2d 3e 70  pIter = pIter->p
44a29 4c 65 66 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d  Left;..        }
44a2a 0d 0a 20 20 20 20 20 20 20 20 70 49 74 65 72 2d  ..        pIter-
44a2b 3e 70 4c 65 66 74 20 3d 20 70 52 65 74 3b 0d 0a  >pLeft = pRet;..
44a2c 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 70          pRet = p
44a2d 4e 6f 74 42 72 61 6e 63 68 3b 0d 0a 20 20 20 20  NotBranch;..    
44a2e 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
44a2f 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d  .  *pnConsumed =
44a30 20 6e 20 2d 20 6e 49 6e 3b 0d 0a 0d 0a 65 78 70   n - nIn;....exp
44a31 72 70 61 72 73 65 5f 6f 75 74 3a 0d 0a 20 20 69  rparse_out:..  i
44a32 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
44a33 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
44a34 46 74 73 33 45 78 70 72 46 72 65 65 28 70 52 65  Fts3ExprFree(pRe
44a35 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  t);..    sqlite3
44a36 46 74 73 33 45 78 70 72 46 72 65 65 28 70 4e 6f  Fts3ExprFree(pNo
44a37 74 42 72 61 6e 63 68 29 3b 0d 0a 20 20 20 20 70  tBranch);..    p
44a38 52 65 74 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20  Ret = 0;..  }.. 
44a39 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b   *ppExpr = pRet;
44a3a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
44a3b 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 61 72 61  }..../*..** Para
44a3c 6d 65 74 65 72 73 20 7a 20 61 6e 64 20 6e 20 63  meters z and n c
44a3d 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
44a3e 20 74 6f 20 61 6e 64 20 6c 65 6e 67 74 68 20 6f   to and length o
44a3f 66 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  f a buffer conta
44a40 69 6e 69 6e 67 0d 0a 2a 2a 20 61 6e 20 66 74 73  ining..** an fts
44a41 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
44a42 6f 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  on, respectively
44a43 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
44a44 61 74 74 65 6d 70 74 73 20 74 6f 20 70 61 72 73  attempts to pars
44a45 65 20 74 68 65 0d 0a 2a 2a 20 71 75 65 72 79 20  e the..** query 
44a46 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 63  expression and c
44a47 72 65 61 74 65 20 61 20 74 72 65 65 20 6f 66 20  reate a tree of 
44a48 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
44a49 72 65 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67  res representing
44a4a 20 74 68 65 0d 0a 2a 2a 20 70 61 72 73 65 64 20   the..** parsed 
44a4b 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 66 20 73  expression. If s
44a4c 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70 45 78  uccessful, *ppEx
44a4d 70 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  pr is set to poi
44a4e 6e 74 20 74 6f 20 74 68 65 20 68 65 61 64 0d 0a  nt to the head..
44a4f 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 65 64  ** of the parsed
44a50 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
44a51 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69   and SQLITE_OK i
44a52 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61  s returned. If a
44a53 6e 20 65 72 72 6f 72 0d 0a 2a 2a 20 6f 63 63 75  n error..** occu
44a54 72 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  rs, either SQLIT
44a55 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 2d 6f 66 2d  E_NOMEM (out-of-
44a56 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72  memory error) or
44a57 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70   SQLITE_ERROR (p
44a58 61 72 73 65 0d 0a 2a 2a 20 65 72 72 6f 72 29 20  arse..** error) 
44a59 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
44a5a 2a 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74  *ppExpr is set t
44a5b 6f 20 30 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  o 0...**..** If 
44a5c 70 61 72 61 6d 65 74 65 72 20 6e 20 69 73 20 61  parameter n is a
44a5d 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72   negative number
44a5e 2c 20 74 68 65 6e 20 7a 20 69 73 20 61 73 73 75  , then z is assu
44a5f 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  med to point to 
44a60 61 0d 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e  a..** nul-termin
44a61 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e 64 20  ated string and 
44a62 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20 64 65  the length is de
44a63 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 73  termined using s
44a64 74 72 6c 65 6e 28 29 2e 0d 0a 2a 2a 0d 0a 2a 2a  trlen()...**..**
44a65 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
44a66 65 74 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72  eter, pTokenizer
44a67 2c 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20  , is passed the 
44a68 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d  fts3 tokenizer m
44a69 6f 64 75 6c 65 20 74 6f 0d 0a 2a 2a 20 75 73 65  odule to..** use
44a6a 20 74 6f 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75   to normalize qu
44a6b 65 72 79 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65  ery tokens while
44a6c 20 70 61 72 73 69 6e 67 20 74 68 65 20 65 78 70   parsing the exp
44a6d 72 65 73 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43  ression. The azC
44a6e 6f 6c 5b 5d 0d 0a 2a 2a 20 61 72 72 61 79 2c 20  ol[]..** array, 
44a6f 77 68 69 63 68 20 69 73 20 61 73 73 75 6d 65 64  which is assumed
44a70 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c   to contain nCol
44a71 20 65 6e 74 72 69 65 73 2c 20 73 68 6f 75 6c 64   entries, should
44a72 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d   contain the nam
44a73 65 73 0d 0a 2a 2a 20 6f 66 20 65 61 63 68 20 63  es..** of each c
44a74 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 72  olumn in the tar
44a75 67 65 74 20 66 74 73 33 20 74 61 62 6c 65 2c 20  get fts3 table, 
44a76 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6c 65  in order from le
44a77 66 74 20 74 6f 20 72 69 67 68 74 2e 20 0d 0a 2a  ft to right. ..*
44a78 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d  * Column names m
44a79 75 73 74 20 62 65 20 6e 75 6c 2d 74 65 72 6d 69  ust be nul-termi
44a7a 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2e 0d 0a  nated strings...
44a7b 2a 2a 0d 0a 2a 2a 20 54 68 65 20 69 44 65 66 61  **..** The iDefa
44a7c 75 6c 74 43 6f 6c 20 70 61 72 61 6d 65 74 65 72  ultCol parameter
44a7d 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
44a7e 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  d the index of t
44a7f 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0d  he table column.
44a80 0a 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72 73  .** that appears
44a81 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   on the left-han
44a82 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41  d-side of the MA
44a83 54 43 48 20 6f 70 65 72 61 74 6f 72 20 28 74 68  TCH operator (th
44a84 65 20 64 65 66 61 75 6c 74 0d 0a 2a 2a 20 63 6f  e default..** co
44a85 6c 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20 61 67  lumn to match ag
44a86 61 69 6e 73 74 20 66 6f 72 20 74 6f 6b 65 6e 73  ainst for tokens
44a87 20 66 6f 72 20 77 68 69 63 68 20 61 20 63 6f 6c   for which a col
44a88 75 6d 6e 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  umn name is not 
44a89 65 78 70 6c 69 63 69 74 6c 79 0d 0a 2a 2a 20 73  explicitly..** s
44a8a 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
44a8b 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 73 74   of the query st
44a8c 72 69 6e 67 29 2c 20 6f 72 20 2d 31 20 69 66 20  ring), or -1 if 
44a8d 74 6f 6b 65 6e 73 20 6d 61 79 20 62 79 20 64 65  tokens may by de
44a8e 66 61 75 6c 74 0d 0a 2a 2a 20 6d 61 74 63 68 20  fault..** match 
44a8f 61 6e 79 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  any table column
44a90 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
44a91 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
44a92 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28 0d  3Fts3ExprParse(.
44a93 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
44a94 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
44a95 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  ,      /* Tokeni
44a96 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0d 0a 20  zer module */.. 
44a97 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20   char **azCol,  
44a98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a99 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
44a9a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   column names fo
44a9b 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f 0d  r fts3 table */.
44a9c 0a 20 20 69 6e 74 20 62 46 74 73 34 2c 20 20 20  .  int bFts4,   
44a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a9e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
44a9f 6f 20 61 6c 6c 6f 77 20 46 54 53 34 2d 6f 6e 6c  o allow FTS4-onl
44aa0 79 20 73 79 6e 74 61 78 20 2a 2f 0d 0a 20 20 69  y syntax */..  i
44aa1 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20  nt nCol,        
44aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44aa3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
44aa4 65 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c  entries in azCol
44aa5 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 44 65  [] */..  int iDe
44aa6 66 61 75 6c 74 43 6f 6c 2c 20 20 20 20 20 20 20  faultCol,       
44aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44aa8 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74  Default column t
44aa9 6f 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20 63 6f  o query */..  co
44aaa 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
44aab 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
44aac 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41 54    /* Text of MAT
44aad 43 48 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20 46  CH query */..  F
44aae 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72  ts3Expr **ppExpr
44aaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ab0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65     /* OUT: Parse
44ab1 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72  d query structur
44ab2 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  e */..){..  int 
44ab3 6e 50 61 72 73 65 64 3b 0d 0a 20 20 69 6e 74 20  nParsed;..  int 
44ab4 72 63 3b 0d 0a 20 20 50 61 72 73 65 43 6f 6e 74  rc;..  ParseCont
44ab5 65 78 74 20 73 50 61 72 73 65 3b 0d 0a 20 20 73  ext sParse;..  s
44ab6 50 61 72 73 65 2e 70 54 6f 6b 65 6e 69 7a 65 72  Parse.pTokenizer
44ab7 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a   = pTokenizer;..
44ab8 20 20 73 50 61 72 73 65 2e 61 7a 43 6f 6c 20 3d    sParse.azCol =
44ab9 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29   (const char **)
44aba 61 7a 43 6f 6c 3b 0d 0a 20 20 73 50 61 72 73 65  azCol;..  sParse
44abb 2e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0d 0a 20  .nCol = nCol;.. 
44abc 20 73 50 61 72 73 65 2e 69 44 65 66 61 75 6c 74   sParse.iDefault
44abd 43 6f 6c 20 3d 20 69 44 65 66 61 75 6c 74 43 6f  Col = iDefaultCo
44abe 6c 3b 0d 0a 20 20 73 50 61 72 73 65 2e 6e 4e 65  l;..  sParse.nNe
44abf 73 74 20 3d 20 30 3b 0d 0a 20 20 73 50 61 72 73  st = 0;..  sPars
44ac0 65 2e 62 46 74 73 34 20 3d 20 62 46 74 73 34 3b  e.bFts4 = bFts4;
44ac1 0d 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0d  ..  if( z==0 ){.
44ac2 0a 20 20 20 20 2a 70 70 45 78 70 72 20 3d 20 30  .    *ppExpr = 0
44ac3 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ;..    return SQ
44ac4 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20  LITE_OK;..  }.. 
44ac5 20 69 66 28 20 6e 3c 30 20 29 7b 0d 0a 20 20 20   if( n<0 ){..   
44ac6 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e   n = (int)strlen
44ac7 28 7a 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63 20  (z);..  }..  rc 
44ac8 3d 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28  = fts3ExprParse(
44ac9 26 73 50 61 72 73 65 2c 20 7a 2c 20 6e 2c 20 70  &sParse, z, n, p
44aca 70 45 78 70 72 2c 20 26 6e 50 61 72 73 65 64 29  pExpr, &nParsed)
44acb 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;....  /* Check 
44acc 66 6f 72 20 6d 69 73 6d 61 74 63 68 65 64 20 70  for mismatched p
44acd 61 72 65 6e 74 68 65 73 69 73 20 2a 2f 0d 0a 20  arenthesis */.. 
44ace 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
44acf 4f 4b 20 26 26 20 73 50 61 72 73 65 2e 6e 4e 65  OK && sParse.nNe
44ad0 73 74 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  st ){..    rc = 
44ad1 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
44ad2 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78     sqlite3Fts3Ex
44ad3 70 72 46 72 65 65 28 2a 70 70 45 78 70 72 29 3b  prFree(*ppExpr);
44ad4 0d 0a 20 20 20 20 2a 70 70 45 78 70 72 20 3d 20  ..    *ppExpr = 
44ad5 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  0;..  }....  ret
44ad6 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
44ad7 0d 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73  ..** Free a pars
44ad8 65 64 20 66 74 73 33 20 71 75 65 72 79 20 65 78  ed fts3 query ex
44ad9 70 72 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74  pression allocat
44ada 65 64 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ed by sqlite3Fts
44adb 33 45 78 70 72 50 61 72 73 65 28 29 2e 0d 0a 2a  3ExprParse()...*
44adc 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
44add 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
44ade 73 33 45 78 70 72 46 72 65 65 28 46 74 73 33 45  s3ExprFree(Fts3E
44adf 78 70 72 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20  xpr *p){..  if( 
44ae0 70 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  p ){..    assert
44ae1 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  ( p->eType==FTSQ
44ae2 55 45 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 70  UERY_PHRASE || p
44ae3 2d 3e 70 50 68 72 61 73 65 3d 3d 30 20 29 3b 0d  ->pPhrase==0 );.
44ae4 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  .    sqlite3Fts3
44ae5 45 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65 66  ExprFree(p->pLef
44ae6 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  t);..    sqlite3
44ae7 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e  Fts3ExprFree(p->
44ae8 70 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 73 71  pRight);..    sq
44ae9 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50 68 72  lite3Fts3EvalPhr
44aea 61 73 65 43 6c 65 61 6e 75 70 28 70 2d 3e 70 50  aseCleanup(p->pP
44aeb 68 72 61 73 65 29 3b 0d 0a 20 20 20 20 73 71 6c  hrase);..    sql
44aec 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 4d 49  ite3_free(p->aMI
44aed 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
44aee 66 72 65 65 28 70 29 3b 0d 0a 20 20 7d 0d 0a 7d  free(p);..  }..}
44aef 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
44af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af4 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *..*************
44af5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44af9 0d 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67 20  ..** Everything 
44afa 61 66 74 65 72 20 74 68 69 73 20 70 6f 69 6e 74  after this point
44afb 20 69 73 20 6a 75 73 74 20 74 65 73 74 20 63 6f   is just test co
44afc 64 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 64 65  de...*/....#ifde
44afd 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 0d  f SQLITE_TEST...
44afe 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
44aff 64 69 6f 2e 68 3e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d  dio.h> */..../*.
44b00 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20  .** Function to 
44b01 71 75 65 72 79 20 74 68 65 20 68 61 73 68 2d 74  query the hash-t
44b02 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65  able of tokenize
44b03 72 73 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74  rs (see README.t
44b04 6f 6b 65 6e 69 7a 65 72 73 29 2e 0d 0a 2a 2f 0d  okenizers)...*/.
44b05 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65 72  .static int quer
44b06 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28 0d  yTestTokenizer(.
44b07 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
44b08 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
44b09 7a 4e 61 6d 65 2c 20 20 0d 0a 20 20 63 6f 6e 73  zName,  ..  cons
44b0a 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
44b0b 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0d  zer_module **pp.
44b0c 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  .){..  int rc;..
44b0d 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
44b0e 70 53 74 6d 74 3b 0d 0a 20 20 63 6f 6e 73 74 20  pStmt;..  const 
44b0f 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
44b10 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
44b11 69 7a 65 72 28 3f 29 22 3b 0d 0a 0d 0a 20 20 2a  izer(?)";....  *
44b12 70 70 20 3d 20 30 3b 0d 0a 20 20 72 63 20 3d 20  pp = 0;..  rc = 
44b13 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
44b14 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
44b15 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20   &pStmt, 0);..  
44b16 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
44b17 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  K ){..    return
44b18 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73   rc;..  }....  s
44b19 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
44b1a 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65  (pStmt, 1, zName
44b1b 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
44b1c 54 49 43 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c  TIC);..  if( SQL
44b1d 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
44b1e 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0d  _step(pStmt) ){.
44b1f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
44b20 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
44b21 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42  mt, 0)==SQLITE_B
44b22 4c 4f 42 20 29 7b 0d 0a 20 20 20 20 20 20 6d 65  LOB ){..      me
44b23 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 70 70 2c  mcpy((void *)pp,
44b24 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
44b25 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20  blob(pStmt, 0), 
44b26 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0d 0a 20  sizeof(*pp));.. 
44b27 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72     }..  }....  r
44b28 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
44b29 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0d 0a  nalize(pStmt);..
44b2a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75  }..../*..** Retu
44b2b 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
44b2c 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  a buffer contain
44b2d 69 6e 67 20 61 20 74 65 78 74 20 72 65 70 72 65  ing a text repre
44b2e 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
44b2f 0d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  ..** expression 
44b30 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
44b31 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
44b32 65 20 62 75 66 66 65 72 20 69 73 20 6f 62 74 61  e buffer is obta
44b33 69 6e 65 64 20 66 72 6f 6d 0d 0a 2a 2a 20 73 71  ined from..** sq
44b34 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20  lite3_malloc(). 
44b35 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  It is the respon
44b36 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
44b37 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 0d 0a  caller to use ..
44b38 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ** sqlite3_free(
44b39 29 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65  ) to release the
44b3a 20 6d 65 6d 6f 72 79 2e 20 49 66 20 61 6e 20 4f   memory. If an O
44b3b 4f 4d 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20  OM condition is 
44b3c 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0d 0a 2a 2a  encountered,..**
44b3d 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
44b3e 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  d...**..** If th
44b3f 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
44b40 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  t is not NULL, t
44b41 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73  hen its contents
44b42 20 61 72 65 20 70 72 65 70 65 6e 64 65 64 20 74   are prepended t
44b43 6f 20 0d 0a 2a 2a 20 74 68 65 20 72 65 74 75 72  o ..** the retur
44b44 6e 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ned expression t
44b45 65 78 74 20 61 6e 64 20 74 68 65 6e 20 66 72 65  ext and then fre
44b46 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
44b47 5f 66 72 65 65 28 29 2e 0d 0a 2a 2f 0d 0a 73 74  _free()...*/..st
44b48 61 74 69 63 20 63 68 61 72 20 2a 65 78 70 72 54  atic char *exprT
44b49 6f 53 74 72 69 6e 67 28 46 74 73 33 45 78 70 72  oString(Fts3Expr
44b4a 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 2a 7a   *pExpr, char *z
44b4b 42 75 66 29 7b 0d 0a 20 20 73 77 69 74 63 68 28  Buf){..  switch(
44b4c 20 70 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b   pExpr->eType ){
44b4d 0d 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  ..    case FTSQU
44b4e 45 52 59 5f 50 48 52 41 53 45 3a 20 7b 0d 0a 20  ERY_PHRASE: {.. 
44b4f 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65 20       Fts3Phrase 
44b50 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78 70 72  *pPhrase = pExpr
44b51 2d 3e 70 50 68 72 61 73 65 3b 0d 0a 20 20 20 20  ->pPhrase;..    
44b52 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 20 20    int i;..      
44b53 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zBuf = sqlite3_m
44b54 70 72 69 6e 74 66 28 0d 0a 20 20 20 20 20 20 20  printf(..       
44b55 20 20 20 22 25 7a 50 48 52 41 53 45 20 25 64 20     "%zPHRASE %d 
44b56 30 22 2c 20 7a 42 75 66 2c 20 70 50 68 72 61 73  0", zBuf, pPhras
44b57 65 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0d 0a 20 20  e->iColumn);..  
44b58 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 42 75      for(i=0; zBu
44b59 66 20 26 26 20 69 3c 70 50 68 72 61 73 65 2d 3e  f && i<pPhrase->
44b5a 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0d 0a 20  nToken; i++){.. 
44b5b 20 20 20 20 20 20 20 7a 42 75 66 20 3d 20 73 71         zBuf = sq
44b5c 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
44b5d 7a 20 25 2e 2a 73 25 73 22 2c 20 7a 42 75 66 2c  z %.*s%s", zBuf,
44b5e 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70   ..            p
44b5f 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69  Phrase->aToken[i
44b60 5d 2e 6e 2c 20 70 50 68 72 61 73 65 2d 3e 61 54  ].n, pPhrase->aT
44b61 6f 6b 65 6e 5b 69 5d 2e 7a 2c 0d 0a 20 20 20 20  oken[i].z,..    
44b62 20 20 20 20 20 20 20 20 28 70 50 68 72 61 73 65          (pPhrase
44b63 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72  ->aToken[i].isPr
44b64 65 66 69 78 3f 22 2b 22 3a 22 22 29 0d 0a 20 20  efix?"+":"")..  
44b65 20 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20        );..      
44b66 7d 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  }..      return 
44b67 7a 42 75 66 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  zBuf;..    }....
44b68 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
44b69 59 5f 4e 45 41 52 3a 0d 0a 20 20 20 20 20 20 7a  Y_NEAR:..      z
44b6a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Buf = sqlite3_mp
44b6b 72 69 6e 74 66 28 22 25 7a 4e 45 41 52 2f 25 64  rintf("%zNEAR/%d
44b6c 20 22 2c 20 7a 42 75 66 2c 20 70 45 78 70 72 2d   ", zBuf, pExpr-
44b6d 3e 6e 4e 65 61 72 29 3b 0d 0a 20 20 20 20 20 20  >nNear);..      
44b6e 62 72 65 61 6b 3b 0d 0a 20 20 20 20 63 61 73 65  break;..    case
44b6f 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0d 0a   FTSQUERY_NOT:..
44b70 20 20 20 20 20 20 7a 42 75 66 20 3d 20 73 71 6c        zBuf = sql
44b71 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a  ite3_mprintf("%z
44b72 4e 4f 54 20 22 2c 20 7a 42 75 66 29 3b 0d 0a 20  NOT ", zBuf);.. 
44b73 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
44b74 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
44b75 4e 44 3a 0d 0a 20 20 20 20 20 20 7a 42 75 66 20  ND:..      zBuf 
44b76 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
44b77 66 28 22 25 7a 41 4e 44 20 22 2c 20 7a 42 75 66  f("%zAND ", zBuf
44b78 29 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  );..      break;
44b79 0d 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  ..    case FTSQU
44b7a 45 52 59 5f 4f 52 3a 0d 0a 20 20 20 20 20 20 7a  ERY_OR:..      z
44b7b 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Buf = sqlite3_mp
44b7c 72 69 6e 74 66 28 22 25 7a 4f 52 20 22 2c 20 7a  rintf("%zOR ", z
44b7d 42 75 66 29 3b 0d 0a 20 20 20 20 20 20 62 72 65  Buf);..      bre
44b7e 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  ak;..  }....  if
44b7f 28 20 7a 42 75 66 20 29 20 7a 42 75 66 20 3d 20  ( zBuf ) zBuf = 
44b80 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
44b81 22 25 7a 7b 22 2c 20 7a 42 75 66 29 3b 0d 0a 20  "%z{", zBuf);.. 
44b82 20 69 66 28 20 7a 42 75 66 20 29 20 7a 42 75 66   if( zBuf ) zBuf
44b83 20 3d 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28   = exprToString(
44b84 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42  pExpr->pLeft, zB
44b85 75 66 29 3b 0d 0a 20 20 69 66 28 20 7a 42 75 66  uf);..  if( zBuf
44b86 20 29 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65   ) zBuf = sqlite
44b87 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a 7d 20 7b  3_mprintf("%z} {
44b88 22 2c 20 7a 42 75 66 29 3b 0d 0a 0d 0a 20 20 69  ", zBuf);....  i
44b89 66 28 20 7a 42 75 66 20 29 20 7a 42 75 66 20 3d  f( zBuf ) zBuf =
44b8a 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45   exprToString(pE
44b8b 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75  xpr->pRight, zBu
44b8c 66 29 3b 0d 0a 20 20 69 66 28 20 7a 42 75 66 20  f);..  if( zBuf 
44b8d 29 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33  ) zBuf = sqlite3
44b8e 5f 6d 70 72 69 6e 74 66 28 22 25 7a 7d 22 2c 20  _mprintf("%z}", 
44b8f 7a 42 75 66 29 3b 0d 0a 0d 0a 20 20 72 65 74 75  zBuf);....  retu
44b90 72 6e 20 7a 42 75 66 3b 0d 0a 7d 0d 0a 0d 0a 2f  rn zBuf;..}..../
44b91 2a 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  *..** This is th
44b92 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
44b93 20 6f 66 20 61 20 73 63 61 6c 61 72 20 53 51 4c   of a scalar SQL
44b94 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74   function used t
44b95 6f 20 74 65 73 74 20 74 68 65 20 0d 0a 2a 2a 20  o test the ..** 
44b96 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65  expression parse
44b97 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20  r. It should be 
44b98 63 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77  called as follow
44b99 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 66 74 73  s:..**..**   fts
44b9a 33 5f 65 78 70 72 74 65 73 74 28 3c 74 6f 6b 65  3_exprtest(<toke
44b9b 6e 69 7a 65 72 3e 2c 20 3c 65 78 70 72 3e 2c 20  nizer>, <expr>, 
44b9c 3c 63 6f 6c 75 6d 6e 20 31 3e 2c 20 2e 2e 2e 29  <column 1>, ...)
44b9d 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69  ;..**..** The fi
44b9e 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 3c 74  rst argument, <t
44b9f 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73 20 74 68  okenizer>, is th
44ba0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 74  e name of the ft
44ba1 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 75 73 65  s3 tokenizer use
44ba2 64 0d 0a 2a 2a 20 74 6f 20 70 61 72 73 65 20 74  d..** to parse t
44ba3 68 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73  he query express
44ba4 69 6f 6e 20 28 73 65 65 20 52 45 41 44 4d 45 2e  ion (see README.
44ba5 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54 68 65  tokenizers). The
44ba6 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
44ba7 0d 0a 2a 2a 20 69 73 20 74 68 65 20 71 75 65 72  ..** is the quer
44ba8 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  y expression to 
44ba9 70 61 72 73 65 2e 20 45 61 63 68 20 73 75 62 73  parse. Each subs
44baa 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 20  equent argument 
44bab 69 73 20 74 68 65 20 6e 61 6d 65 0d 0a 2a 2a 20  is the name..** 
44bac 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  of a column of t
44bad 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 74 68  he fts3 table th
44bae 61 74 20 74 68 65 20 71 75 65 72 79 20 65 78 70  at the query exp
44baf 72 65 73 73 69 6f 6e 20 6d 61 79 20 72 65 66 65  ression may refe
44bb0 72 20 74 6f 2e 0d 0a 2a 2a 20 46 6f 72 20 65 78  r to...** For ex
44bb1 61 6d 70 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ample:..**..**  
44bb2 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65 78 70   SELECT fts3_exp
44bb3 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c 20  rtest('simple', 
44bb4 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67 67  'Bill col2:Blogg
44bb5 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63 6f 6c  s', 'col1', 'col
44bb6 32 27 29 3b 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  2');..*/..static
44bb7 20 76 6f 69 64 20 66 74 73 33 45 78 70 72 54 65   void fts3ExprTe
44bb8 73 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63  st(..  sqlite3_c
44bb9 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
44bba 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20  ..  int argc,.. 
44bbb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
44bbc 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 73 71 6c  *argv..){..  sql
44bbd 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
44bbe 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f  odule const *pMo
44bbf 64 75 6c 65 20 3d 20 30 3b 0d 0a 20 20 73 71 6c  dule = 0;..  sql
44bc0 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
44bc1 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0d  pTokenizer = 0;.
44bc2 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 63 68  .  int rc;..  ch
44bc3 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20 30 3b 0d  ar **azCol = 0;.
44bc4 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
44bc5 45 78 70 72 3b 0d 0a 20 20 69 6e 74 20 6e 45 78  Expr;..  int nEx
44bc6 70 72 3b 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  pr;..  int nCol;
44bc7 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 46  ..  int ii;..  F
44bc8 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 3b 0d  ts3Expr *pExpr;.
44bc9 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  .  char *zBuf = 
44bca 30 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  0;..  sqlite3 *d
44bcb 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
44bcc 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
44bcd 6e 74 65 78 74 29 3b 0d 0a 0d 0a 20 20 69 66 28  ntext);....  if(
44bce 20 61 72 67 63 3c 33 20 29 7b 0d 0a 20 20 20 20   argc<3 ){..    
44bcf 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
44bd0 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0d 0a  rror(context, ..
44bd1 20 20 20 20 20 20 20 20 22 55 73 61 67 65 3a 20          "Usage: 
44bd2 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 74 6f  fts3_exprtest(to
44bd3 6b 65 6e 69 7a 65 72 2c 20 65 78 70 72 2c 20 63  kenizer, expr, c
44bd4 6f 6c 31 2c 20 2e 2e 2e 22 2c 20 2d 31 0d 0a 20  ol1, ...", -1.. 
44bd5 20 20 20 29 3b 0d 0a 20 20 20 20 72 65 74 75 72     );..    retur
44bd6 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20  n;..  }....  rc 
44bd7 3d 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65 6e  = queryTestToken
44bd8 69 7a 65 72 28 64 62 2c 0d 0a 20 20 20 20 20 20  izer(db,..      
44bd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bda 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20      (const char 
44bdb 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
44bdc 74 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26  text(argv[0]), &
44bdd 70 4d 6f 64 75 6c 65 29 3b 0d 0a 20 20 69 66 28  pModule);..  if(
44bde 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
44bdf 4d 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65  M ){..    sqlite
44be0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
44be1 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0d 0a  omem(context);..
44be2 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73      goto exprtes
44be3 74 5f 6f 75 74 3b 0d 0a 20 20 7d 65 6c 73 65 20  t_out;..  }else 
44be4 69 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b 0d  if( !pModule ){.
44be5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
44be6 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
44be7 74 2c 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65  t, "No such toke
44be8 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d  nizer module", -
44be9 31 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65 78  1);..    goto ex
44bea 70 72 74 65 73 74 5f 6f 75 74 3b 0d 0a 20 20 7d  prtest_out;..  }
44beb 0d 0a 0d 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75  ....  rc = pModu
44bec 6c 65 2d 3e 78 43 72 65 61 74 65 28 30 2c 20 30  le->xCreate(0, 0
44bed 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0d  , &pTokenizer);.
44bee 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
44bef 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72  QLITE_NOMEM || r
44bf0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d  c==SQLITE_OK );.
44bf1 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
44bf2 45 5f 4e 4f 4d 45 4d 20 29 7b 0d 0a 20 20 20 20  E_NOMEM ){..    
44bf3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
44bf4 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
44bf5 78 74 29 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 65  xt);..    goto e
44bf6 78 70 72 74 65 73 74 5f 6f 75 74 3b 0d 0a 20 20  xprtest_out;..  
44bf7 7d 0d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  }..  pTokenizer-
44bf8 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 75  >pModule = pModu
44bf9 6c 65 3b 0d 0a 0d 0a 20 20 7a 45 78 70 72 20 3d  le;....  zExpr =
44bfa 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
44bfb 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
44bfc 74 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20 20 6e  t(argv[1]);..  n
44bfd 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  Expr = sqlite3_v
44bfe 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
44bff 31 5d 29 3b 0d 0a 20 20 6e 43 6f 6c 20 3d 20 61  1]);..  nCol = a
44c00 72 67 63 2d 32 3b 0d 0a 20 20 61 7a 43 6f 6c 20  rgc-2;..  azCol 
44c01 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69 74  = (char **)sqlit
44c02 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73  e3_malloc(nCol*s
44c03 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b 0d  izeof(char *));.
44c04 0a 20 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b  .  if( !azCol ){
44c05 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
44c06 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
44c07 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 20 20  (context);..    
44c08 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75  goto exprtest_ou
44c09 74 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69  t;..  }..  for(i
44c0a 69 3d 30 3b 20 69 69 3c 6e 43 6f 6c 3b 20 69 69  i=0; ii<nCol; ii
44c0b 2b 2b 29 7b 0d 0a 20 20 20 20 61 7a 43 6f 6c 5b  ++){..    azCol[
44c0c 69 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ii] = (char *)sq
44c0d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
44c0e 28 61 72 67 76 5b 69 69 2b 32 5d 29 3b 0d 0a 20  (argv[ii+2]);.. 
44c0f 20 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c   }....  rc = sql
44c10 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
44c11 65 28 0d 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  e(..      pToken
44c12 69 7a 65 72 2c 20 61 7a 43 6f 6c 2c 20 30 2c 20  izer, azCol, 0, 
44c13 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a 45 78 70  nCol, nCol, zExp
44c14 72 2c 20 6e 45 78 70 72 2c 20 26 70 45 78 70 72  r, nExpr, &pExpr
44c15 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20 72 63  ..  );..  if( rc
44c16 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
44c17 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c!=SQLITE_NOMEM 
44c18 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
44c19 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
44c1a 74 65 78 74 2c 20 22 45 72 72 6f 72 20 70 61 72  text, "Error par
44c1b 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 22  sing expression"
44c1c 2c 20 2d 31 29 3b 0d 0a 20 20 7d 65 6c 73 65 20  , -1);..  }else 
44c1d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
44c1e 4f 4d 45 4d 20 7c 7c 20 21 28 7a 42 75 66 20 3d  OMEM || !(zBuf =
44c1f 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45   exprToString(pE
44c20 78 70 72 2c 20 30 29 29 20 29 7b 0d 0a 20 20 20  xpr, 0)) ){..   
44c21 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
44c22 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
44c23 65 78 74 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  ext);..  }else{.
44c24 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
44c25 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
44c26 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
44c27 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0d 0a  TE_TRANSIENT);..
44c28 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
44c29 28 7a 42 75 66 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a  (zBuf);..  }....
44c2a 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
44c2b 72 46 72 65 65 28 70 45 78 70 72 29 3b 0d 0a 0d  rFree(pExpr);...
44c2c 0a 65 78 70 72 74 65 73 74 5f 6f 75 74 3a 0d 0a  .exprtest_out:..
44c2d 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20 26 26    if( pModule &&
44c2e 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0d 0a   pTokenizer ){..
44c2f 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
44c30 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65  ->xDestroy(pToke
44c31 6e 69 7a 65 72 29 3b 0d 0a 20 20 7d 0d 0a 20 20  nizer);..  }..  
44c32 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43  sqlite3_free(azC
44c33 6f 6c 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  ol);..}..../*..*
44c34 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 71  * Register the q
44c35 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  uery expression 
44c36 70 61 72 73 65 72 20 74 65 73 74 20 66 75 6e 63  parser test func
44c37 74 69 6f 6e 20 66 74 73 33 5f 65 78 70 72 74 65  tion fts3_exprte
44c38 73 74 28 29 20 0d 0a 2a 2a 20 77 69 74 68 20 64  st() ..** with d
44c39 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
44c3a 6f 6e 20 64 62 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c  on db. ..*/..SQL
44c3b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
44c3c 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 49  sqlite3Fts3ExprI
44c3d 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61 63 65  nitTestInterface
44c3e 28 73 71 6c 69 74 65 33 2a 20 64 62 29 7b 0d 0a  (sqlite3* db){..
44c3f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
44c40 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
44c41 28 0d 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74  (..      db, "ft
44c42 73 33 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31  s3_exprtest", -1
44c43 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
44c44 2c 20 66 74 73 33 45 78 70 72 54 65 73 74 2c 20  , fts3ExprTest, 
44c45 30 2c 20 30 0d 0a 20 20 29 3b 0d 0a 7d 0d 0a 0d  0, 0..  );..}...
44c46 0a 23 65 6e 64 69 66 0d 0a 23 65 6e 64 69 66 20  .#endif..#endif 
44c47 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
44c48 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
44c49 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
44c4a 45 5f 46 54 53 33 29 20 2a 2f 0d 0a 0d 0a 2f 2a  E_FTS3) */..../*
44c4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
44c4c 64 20 6f 66 20 66 74 73 33 5f 65 78 70 72 2e 63  d of fts3_expr.c
44c4d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
44c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f  ************/../
44c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
44c51 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68  egin file fts3_h
44c52 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.c **********
44c53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a  *************/..
44c55 2f 2a 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  /*..** 2001 Sept
44c56 65 6d 62 65 72 20 32 32 0d 0a 2a 2a 0d 0a 2a 2a  ember 22..**..**
44c57 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
44c58 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
44c59 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
44c5a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
44c5b 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  f..** a legal no
44c5c 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
44c5d 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a  blessing:..**..*
44c5e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
44c5f 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
44c60 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  l...**    May yo
44c61 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
44c62 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
44c63 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
44c64 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79  rs...**    May y
44c65 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
44c66 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
44c67 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
44c68 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ...**..*********
44c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c6d 0d 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  ..** This is the
44c6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
44c6f 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d  of generic hash-
44c70 74 61 62 6c 65 73 20 75 73 65 64 20 69 6e 20 53  tables used in S
44c71 51 4c 69 74 65 2e 0d 0a 2a 2a 20 57 65 27 76 65  QLite...** We've
44c72 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69   modified it sli
44c73 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61  ghtly to serve a
44c74 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20 68  s a standalone h
44c75 61 73 68 20 74 61 62 6c 65 0d 0a 2a 2a 20 69 6d  ash table..** im
44c76 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
44c77 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
44c78 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0d  ndexing module..
44c79 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  .*/..../*..** Th
44c7a 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
44c7b 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
44c7c 69 6c 65 64 20 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a  iled if:..**..**
44c7d 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
44c7e 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
44c7f 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65  built as an exte
44c80 6e 73 69 6f 6e 0d 0a 2a 2a 20 20 20 20 20 20 20  nsion..**       
44c81 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
44c82 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
44c83 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0d 0a  t defined), or..
44c84 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  **..**     * The
44c85 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
44c86 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f  being built into
44c87 20 74 68 65 20 63 6f 72 65 20 6f 66 0d 0a 2a 2a   the core of..**
44c88 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
44c89 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
44c8a 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
44c8b 69 73 20 64 65 66 69 6e 65 64 29 2e 0d 0a 2a 2f  is defined)...*/
44c8c 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
44c8d 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
44c8e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
44c8f 41 42 4c 45 5f 46 54 53 33 29 0d 0a 0d 0a 2f 2a  ABLE_FTS3)..../*
44c90 20 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72   #include <asser
44c91 74 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63  t.h> */../* #inc
44c92 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20  lude <stdlib.h> 
44c93 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  */../* #include 
44c94 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d 0a 0d  <string.h> */...
44c95 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 6c 6c 6f 63  .../*..** Malloc
44c96 20 61 6e 64 20 46 72 65 65 20 66 75 6e 63 74 69   and Free functi
44c97 6f 6e 73 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ons..*/..static 
44c98 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d 61  void *fts3HashMa
44c99 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0d 0a 20 20  lloc(int n){..  
44c9a 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
44c9b 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0d 0a 20 20  3_malloc(n);..  
44c9c 69 66 28 20 70 20 29 7b 0d 0a 20 20 20 20 6d 65  if( p ){..    me
44c9d 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0d 0a  mset(p, 0, n);..
44c9e 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 3b    }..  return p;
44c9f 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  ..}..static void
44ca0 20 66 74 73 33 48 61 73 68 46 72 65 65 28 76 6f   fts3HashFree(vo
44ca1 69 64 20 2a 70 29 7b 0d 0a 20 20 73 71 6c 69 74  id *p){..  sqlit
44ca2 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 7d 0d 0a  e3_free(p);..}..
44ca3 0d 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d  ../* Turn bulk m
44ca4 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73  emory into a has
44ca5 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62  h table object b
44ca6 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
44ca7 68 65 0d 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66  he..** fields of
44ca8 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74   the Hash struct
44ca9 75 72 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 22 70 4e  ure...**..** "pN
44caa 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ew" is a pointer
44cab 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62   to the hash tab
44cac 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
44cad 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0d 0a 2a   initialized...*
44cae 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20 6f 6e  * keyClass is on
44caf 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e  e of the constan
44cb0 74 73 20 0d 0a 2a 2a 20 46 54 53 33 5f 48 41 53  ts ..** FTS3_HAS
44cb1 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53 33  H_BINARY or FTS3
44cb2 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20 54  _HASH_STRING.  T
44cb3 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79 43  he value of keyC
44cb4 6c 61 73 73 20 0d 0a 2a 2a 20 64 65 74 65 72 6d  lass ..** determ
44cb5 69 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f  ines what kind o
44cb6 66 20 6b 65 79 20 74 68 65 20 68 61 73 68 20 74  f key the hash t
44cb7 61 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20  able will use.  
44cb8 22 63 6f 70 79 4b 65 79 22 20 69 73 0d 0a 2a 2a  "copyKey" is..**
44cb9 20 74 72 75 65 20 69 66 20 74 68 65 20 68 61 73   true if the has
44cba 68 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d  h table should m
44cbb 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76  ake its own priv
44cbc 61 74 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73  ate copy of keys
44cbd 20 61 6e 64 0d 0a 2a 2a 20 66 61 6c 73 65 20 69   and..** false i
44cbe 66 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74  f it should just
44cbf 20 75 73 65 20 74 68 65 20 73 75 70 70 6c 69 65   use the supplie
44cc0 64 20 70 6f 69 6e 74 65 72 2e 0d 0a 2a 2f 0d 0a  d pointer...*/..
44cc1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
44cc2 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48  oid sqlite3Fts3H
44cc3 61 73 68 49 6e 69 74 28 46 74 73 33 48 61 73 68  ashInit(Fts3Hash
44cc4 20 2a 70 4e 65 77 2c 20 63 68 61 72 20 6b 65 79   *pNew, char key
44cc5 43 6c 61 73 73 2c 20 63 68 61 72 20 63 6f 70 79  Class, char copy
44cc6 4b 65 79 29 7b 0d 0a 20 20 61 73 73 65 72 74 28  Key){..  assert(
44cc7 20 70 4e 65 77 21 3d 30 20 29 3b 0d 0a 20 20 61   pNew!=0 );..  a
44cc8 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3e  ssert( keyClass>
44cc9 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e  =FTS3_HASH_STRIN
44cca 47 20 26 26 20 6b 65 79 43 6c 61 73 73 3c 3d 46  G && keyClass<=F
44ccb 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
44ccc 29 3b 0d 0a 20 20 70 4e 65 77 2d 3e 6b 65 79 43  );..  pNew->keyC
44ccd 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61 73 73 3b  lass = keyClass;
44cce 0d 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65  ..  pNew->copyKe
44ccf 79 20 3d 20 63 6f 70 79 4b 65 79 3b 0d 0a 20 20  y = copyKey;..  
44cd0 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b  pNew->first = 0;
44cd1 0d 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20  ..  pNew->count 
44cd2 3d 20 30 3b 0d 0a 20 20 70 4e 65 77 2d 3e 68 74  = 0;..  pNew->ht
44cd3 73 69 7a 65 20 3d 20 30 3b 0d 0a 20 20 70 4e 65  size = 0;..  pNe
44cd4 77 2d 3e 68 74 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d  w->ht = 0;..}...
44cd5 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65  ./* Remove all e
44cd6 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61  ntries from a ha
44cd7 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61  sh table.  Recla
44cd8 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0d 0a  im all memory...
44cd9 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75  ** Call this rou
44cda 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61  tine to delete a
44cdb 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74   hash table or t
44cdc 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74  o reset a hash t
44cdd 61 62 6c 65 0d 0a 2a 2a 20 74 6f 20 74 68 65 20  able..** to the 
44cde 65 6d 70 74 79 20 73 74 61 74 65 2e 0d 0a 2a 2f  empty state...*/
44cdf 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
44ce0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
44ce1 33 48 61 73 68 43 6c 65 61 72 28 46 74 73 33 48  3HashClear(Fts3H
44ce2 61 73 68 20 2a 70 48 29 7b 0d 0a 20 20 46 74 73  ash *pH){..  Fts
44ce3 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b  3HashElem *elem;
44ce4 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
44ce5 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c  looping over all
44ce6 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
44ce7 20 74 61 62 6c 65 20 2a 2f 0d 0a 0d 0a 20 20 61   table */....  a
44ce8 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0d  ssert( pH!=0 );.
44ce9 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69  .  elem = pH->fi
44cea 72 73 74 3b 0d 0a 20 20 70 48 2d 3e 66 69 72 73  rst;..  pH->firs
44ceb 74 20 3d 20 30 3b 0d 0a 20 20 66 74 73 33 48 61  t = 0;..  fts3Ha
44cec 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0d  shFree(pH->ht);.
44ced 0a 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0d 0a  .  pH->ht = 0;..
44cee 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30    pH->htsize = 0
44cef 3b 0d 0a 20 20 77 68 69 6c 65 28 20 65 6c 65 6d  ;..  while( elem
44cf0 20 29 7b 0d 0a 20 20 20 20 46 74 73 33 48 61 73   ){..    Fts3Has
44cf1 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d  hElem *next_elem
44cf2 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0d 0a   = elem->next;..
44cf3 20 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79      if( pH->copy
44cf4 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65  Key && elem->pKe
44cf5 79 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33  y ){..      fts3
44cf6 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70  HashFree(elem->p
44cf7 4b 65 79 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  Key);..    }..  
44cf8 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28 65    fts3HashFree(e
44cf9 6c 65 6d 29 3b 0d 0a 20 20 20 20 65 6c 65 6d 20  lem);..    elem 
44cfa 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0d 0a 20 20  = next_elem;..  
44cfb 7d 0d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d  }..  pH->count =
44cfc 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a   0;..}..../*..**
44cfd 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72   Hash and compar
44cfe 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77  ison functions w
44cff 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20  hen the mode is 
44d00 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
44d01 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
44d02 20 66 74 73 33 53 74 72 48 61 73 68 28 63 6f 6e   fts3StrHash(con
44d03 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
44d04 6e 74 20 6e 4b 65 79 29 7b 0d 0a 20 20 63 6f 6e  nt nKey){..  con
44d05 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f  st char *z = (co
44d06 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b  nst char *)pKey;
44d07 0d 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0d 0a  ..  int h = 0;..
44d08 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20    if( nKey<=0 ) 
44d09 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74 72  nKey = (int) str
44d0a 6c 65 6e 28 7a 29 3b 0d 0a 20 20 77 68 69 6c 65  len(z);..  while
44d0b 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0d 0a  ( nKey > 0  ){..
44d0c 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e      h = (h<<3) ^
44d0d 20 68 20 5e 20 2a 7a 2b 2b 3b 0d 0a 20 20 20 20   h ^ *z++;..    
44d0e 6e 4b 65 79 2d 2d 3b 0d 0a 20 20 7d 0d 0a 20 20  nKey--;..  }..  
44d0f 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66  return h & 0x7ff
44d10 66 66 66 66 66 3b 0d 0a 7d 0d 0a 73 74 61 74 69  fffff;..}..stati
44d11 63 20 69 6e 74 20 66 74 73 33 53 74 72 43 6f 6d  c int fts3StrCom
44d12 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20  pare(const void 
44d13 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20  *pKey1, int n1, 
44d14 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
44d15 32 2c 20 69 6e 74 20 6e 32 29 7b 0d 0a 20 20 69  2, int n2){..  i
44d16 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
44d17 72 6e 20 31 3b 0d 0a 20 20 72 65 74 75 72 6e 20  rn 1;..  return 
44d18 73 74 72 6e 63 6d 70 28 28 63 6f 6e 73 74 20 63  strncmp((const c
44d19 68 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73  har*)pKey1,(cons
44d1a 74 20 63 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31  t char*)pKey2,n1
44d1b 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  );..}..../*..** 
44d1c 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69  Hash and compari
44d1d 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  son functions wh
44d1e 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46  en the mode is F
44d1f 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 0d  TS3_HASH_BINARY.
44d20 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
44d21 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f 6e 73  fts3BinHash(cons
44d22 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
44d23 74 20 6e 4b 65 79 29 7b 0d 0a 20 20 69 6e 74 20  t nKey){..  int 
44d24 68 20 3d 20 30 3b 0d 0a 20 20 63 6f 6e 73 74 20  h = 0;..  const 
44d25 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
44d26 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0d 0a 20   char *)pKey;.. 
44d27 20 77 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e   while( nKey-- >
44d28 20 30 20 29 7b 0d 0a 20 20 20 20 68 20 3d 20 28   0 ){..    h = (
44d29 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b  h<<3) ^ h ^ *(z+
44d2a 2b 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  +);..  }..  retu
44d2b 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
44d2c 66 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e  f;..}..static in
44d2d 74 20 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65  t fts3BinCompare
44d2e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
44d2f 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73  y1, int n1, cons
44d30 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69  t void *pKey2, i
44d31 6e 74 20 6e 32 29 7b 0d 0a 20 20 69 66 28 20 6e  nt n2){..  if( n
44d32 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31  1!=n2 ) return 1
44d33 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d 63  ;..  return memc
44d34 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e  mp(pKey1,pKey2,n
44d35 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  1);..}..../*..**
44d36 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
44d37 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  r to the appropr
44d38 69 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69  iate hash functi
44d39 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79  on given the key
44d3a 20 63 6c 61 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   class...**..** 
44d3b 54 68 65 20 43 20 73 79 6e 74 61 78 20 69 6e 20  The C syntax in 
44d3c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  this function de
44d3d 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62 65 20  finition may be 
44d3e 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73 6f 6d  unfamilar to som
44d3f 65 20 0d 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65  e ..** programme
44d40 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69 64  rs, so we provid
44d41 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
44d42 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c 61  additional expla
44d43 6e 61 74 69 6f 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  nation:..**..** 
44d44 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
44d45 66 75 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73  function is "fts
44d46 48 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20  HashFunction".  
44d47 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  The function tak
44d48 65 73 20 61 0d 0a 2a 2a 20 73 69 6e 67 6c 65 20  es a..** single 
44d49 70 61 72 61 6d 65 74 65 72 20 22 6b 65 79 43 6c  parameter "keyCl
44d4a 61 73 73 22 2e 20 20 54 68 65 20 72 65 74 75 72  ass".  The retur
44d4b 6e 20 76 61 6c 75 65 20 6f 66 20 66 74 73 48 61  n value of ftsHa
44d4c 73 68 46 75 6e 63 74 69 6f 6e 28 29 0d 0a 2a 2a  shFunction()..**
44d4d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
44d4e 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f   another functio
44d4f 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79  n.  Specifically
44d50 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
44d51 75 65 0d 0a 2a 2a 20 6f 66 20 66 74 73 48 61 73  ue..** of ftsHas
44d52 68 46 75 6e 63 74 69 6f 6e 28 29 20 69 73 20 61  hFunction() is a
44d53 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
44d54 6e 63 74 69 6f 6e 20 74 68 61 74 20 74 61 6b 65  nction that take
44d55 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73  s two parameters
44d56 0d 0a 2a 2a 20 77 69 74 68 20 74 79 70 65 73 20  ..** with types 
44d57 22 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20 61 6e  "const void*" an
44d58 64 20 22 69 6e 74 22 20 61 6e 64 20 72 65 74 75  d "int" and retu
44d59 72 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0d 0a 2a  rns an "int"...*
44d5a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a  /..static int (*
44d5b 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
44d5c 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63  int keyClass))(c
44d5d 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b  onst void*,int){
44d5e 0d 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73  ..  if( keyClass
44d5f 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  ==FTS3_HASH_STRI
44d60 4e 47 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  NG ){..    retur
44d61 6e 20 26 66 74 73 33 53 74 72 48 61 73 68 3b 0d  n &fts3StrHash;.
44d62 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 61  .  }else{..    a
44d63 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d  ssert( keyClass=
44d64 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52  =FTS3_HASH_BINAR
44d65 59 20 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  Y );..    return
44d66 20 26 66 74 73 33 42 69 6e 48 61 73 68 3b 0d 0a   &fts3BinHash;..
44d67 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
44d68 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
44d69 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  r to the appropr
44d6a 69 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69  iate hash functi
44d6b 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79  on given the key
44d6c 20 63 6c 61 73 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20   class...**..** 
44d6d 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e 74 65  For help in inte
44d6e 72 70 72 65 74 65 64 20 74 68 65 20 6f 62 73 63  rpreted the obsc
44d6f 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20 74 68  ure C code in th
44d70 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
44d71 69 74 69 6f 6e 2c 0d 0a 2a 2a 20 73 65 65 20 74  ition,..** see t
44d72 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
44d73 74 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f 75  t on the previou
44d74 73 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a 2f 0d  s function...*/.
44d75 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74  .static int (*ft
44d76 73 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e  sCompareFunction
44d77 28 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28  (int keyClass))(
44d78 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
44d79 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
44d7a 7b 0d 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73  {..  if( keyClas
44d7b 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52  s==FTS3_HASH_STR
44d7c 49 4e 47 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  ING ){..    retu
44d7d 72 6e 20 26 66 74 73 33 53 74 72 43 6f 6d 70 61  rn &fts3StrCompa
44d7e 72 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20  re;..  }else{.. 
44d7f 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c     assert( keyCl
44d80 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42  ass==FTS3_HASH_B
44d81 49 4e 41 52 59 20 29 3b 0d 0a 20 20 20 20 72 65  INARY );..    re
44d82 74 75 72 6e 20 26 66 74 73 33 42 69 6e 43 6f 6d  turn &fts3BinCom
44d83 70 61 72 65 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  pare;..  }..}...
44d84 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d  ./* Link an elem
44d85 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
44d86 68 20 74 61 62 6c 65 0d 0a 2a 2f 0d 0a 73 74 61  h table..*/..sta
44d87 74 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73  tic void fts3Has
44d88 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0d  hInsertElement(.
44d89 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c  .  Fts3Hash *pH,
44d8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
44d8b 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68  he complete hash
44d8c 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 73 74 72   table */..  str
44d8d 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e  uct _fts3ht *pEn
44d8e 74 72 79 2c 20 20 2f 2a 20 54 68 65 20 65 6e 74  try,  /* The ent
44d8f 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e  ry into which pN
44d90 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a  ew is inserted *
44d91 2f 0d 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65  /..  Fts3HashEle
44d92 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 2f 2a  m *pNew       /*
44d93 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   The element to 
44d94 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0d 0a  be inserted */..
44d95 29 7b 0d 0a 20 20 46 74 73 33 48 61 73 68 45 6c  ){..  Fts3HashEl
44d96 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 2f  em *pHead;     /
44d97 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  * First element 
44d98 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72  already in pEntr
44d99 79 20 2a 2f 0d 0a 20 20 70 48 65 61 64 20 3d 20  y */..  pHead = 
44d9a 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0d 0a  pEntry->chain;..
44d9b 20 20 69 66 28 20 70 48 65 61 64 20 29 7b 0d 0a    if( pHead ){..
44d9c 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d      pNew->next =
44d9d 20 70 48 65 61 64 3b 0d 0a 20 20 20 20 70 4e 65   pHead;..    pNe
44d9e 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d  w->prev = pHead-
44d9f 3e 70 72 65 76 3b 0d 0a 20 20 20 20 69 66 28 20  >prev;..    if( 
44da0 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70  pHead->prev ){ p
44da1 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  Head->prev->next
44da2 20 3d 20 70 4e 65 77 3b 20 7d 0d 0a 20 20 20 20   = pNew; }..    
44da3 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20  else            
44da4 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70   { pH->first = p
44da5 4e 65 77 3b 20 7d 0d 0a 20 20 20 20 70 48 65 61  New; }..    pHea
44da6 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0d  d->prev = pNew;.
44da7 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70  .  }else{..    p
44da8 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e  New->next = pH->
44da9 66 69 72 73 74 3b 0d 0a 20 20 20 20 69 66 28 20  first;..    if( 
44daa 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d  pH->first ){ pH-
44dab 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70  >first->prev = p
44dac 4e 65 77 3b 20 7d 0d 0a 20 20 20 20 70 4e 65 77  New; }..    pNew
44dad 2d 3e 70 72 65 76 20 3d 20 30 3b 0d 0a 20 20 20  ->prev = 0;..   
44dae 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65   pH->first = pNe
44daf 77 3b 0d 0a 20 20 7d 0d 0a 20 20 70 45 6e 74 72  w;..  }..  pEntr
44db0 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0d 0a 20 20 70  y->count++;..  p
44db1 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70  Entry->chain = p
44db2 4e 65 77 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 20  New;..}....../* 
44db3 52 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20  Resize the hash 
44db4 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74  table so that it
44db5 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73   cantains "new_s
44db6 69 7a 65 22 20 62 75 63 6b 65 74 73 2e 0d 0a 2a  ize" buckets...*
44db7 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d 75 73  * "new_size" mus
44db8 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
44db9 32 2e 20 20 54 68 65 20 68 61 73 68 20 74 61 62  2.  The hash tab
44dba 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20 0d 0a  le might fail ..
44dbb 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20  ** to resize if 
44dbc 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 66  sqliteMalloc() f
44dbd 61 69 6c 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65  ails...**..** Re
44dbe 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
44dbf 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
44dc0 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
44dc1 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
44dc2 6e 74 20 66 74 73 33 52 65 68 61 73 68 28 46 74  nt fts3Rehash(Ft
44dc3 73 33 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20  s3Hash *pH, int 
44dc4 6e 65 77 5f 73 69 7a 65 29 7b 0d 0a 20 20 73 74  new_size){..  st
44dc5 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 6e 65  ruct _fts3ht *ne
44dc6 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f  w_ht;          /
44dc7 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74  * The new hash t
44dc8 61 62 6c 65 20 2a 2f 0d 0a 20 20 46 74 73 33 48  able */..  Fts3H
44dc9 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a  ashElem *elem, *
44dca 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46  next_elem;  /* F
44dcb 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
44dcc 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74  existing element
44dcd 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 48  s */..  int (*xH
44dce 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ash)(const void*
44dcf 2c 69 6e 74 29 3b 20 20 20 2f 2a 20 54 68 65 20  ,int);   /* The 
44dd0 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  hash function */
44dd1 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 28 6e  ....  assert( (n
44dd2 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77 5f 73  ew_size & (new_s
44dd3 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0d 0a 20  ize-1))==0 );.. 
44dd4 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63   new_ht = (struc
44dd5 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73 33  t _fts3ht *)fts3
44dd6 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f  HashMalloc( new_
44dd7 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
44dd8 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0d 0a  ct _fts3ht) );..
44dd9 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20    if( new_ht==0 
44dda 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 66  ) return 1;..  f
44ddb 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e  ts3HashFree(pH->
44ddc 68 74 29 3b 0d 0a 20 20 70 48 2d 3e 68 74 20 3d  ht);..  pH->ht =
44ddd 20 6e 65 77 5f 68 74 3b 0d 0a 20 20 70 48 2d 3e   new_ht;..  pH->
44dde 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a  htsize = new_siz
44ddf 65 3b 0d 0a 20 20 78 48 61 73 68 20 3d 20 66 74  e;..  xHash = ft
44de0 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48  sHashFunction(pH
44de1 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0d 0a 20 20  ->keyClass);..  
44de2 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72  for(elem=pH->fir
44de3 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b  st, pH->first=0;
44de4 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65   elem; elem = ne
44de5 78 74 5f 65 6c 65 6d 29 7b 0d 0a 20 20 20 20 69  xt_elem){..    i
44de6 6e 74 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28  nt h = (*xHash)(
44de7 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d  elem->pKey, elem
44de8 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73  ->nKey) & (new_s
44de9 69 7a 65 2d 31 29 3b 0d 0a 20 20 20 20 6e 65 78  ize-1);..    nex
44dea 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
44deb 65 78 74 3b 0d 0a 20 20 20 20 66 74 73 33 48 61  ext;..    fts3Ha
44dec 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28  shInsertElement(
44ded 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20  pH, &new_ht[h], 
44dee 65 6c 65 6d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72  elem);..  }..  r
44def 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn 0;..}..../
44df0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
44df1 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73  (for internal us
44df2 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20  e only) locates 
44df3 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e  an element in an
44df4 0d 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20  ..** hash table 
44df5 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
44df6 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65   given key.  The
44df7 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b   hash for this k
44df8 65 79 20 68 61 73 0d 0a 2a 2a 20 61 6c 72 65 61  ey has..** alrea
44df9 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  dy been computed
44dfa 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61   and is passed a
44dfb 73 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  s the 4th parame
44dfc 74 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ter...*/..static
44dfd 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66   Fts3HashElem *f
44dfe 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
44dff 48 61 73 68 28 0d 0a 20 20 63 6f 6e 73 74 20 46  Hash(..  const F
44e00 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20  ts3Hash *pH, /* 
44e01 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61  The pH to be sea
44e02 72 63 68 65 64 20 2a 2f 0d 0a 20 20 63 6f 6e 73  rched */..  cons
44e03 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
44e04 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72  /* The key we ar
44e05 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20  e searching for 
44e06 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0d  */..  int nKey,.
44e07 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20  .  int h        
44e08 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61         /* The ha
44e09 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e  sh for this key.
44e0a 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 48   */..){..  Fts3H
44e0b 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
44e0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
44e0d 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74  d to loop thru t
44e0e 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20  he element list 
44e0f 2a 2f 0d 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b  */..  int count;
44e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e11 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
44e12 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20  f elements left 
44e13 74 6f 20 74 65 73 74 20 2a 2f 0d 0a 20 20 69 6e  to test */..  in
44e14 74 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f  t (*xCompare)(co
44e15 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
44e16 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
44e17 20 2f 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66   /* comparison f
44e18 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 0d 0a 20 20  unction */....  
44e19 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0d 0a 20  if( pH->ht ){.. 
44e1a 20 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68     struct _fts3h
44e1b 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d  t *pEntry = &pH-
44e1c 3e 68 74 5b 68 5d 3b 0d 0a 20 20 20 20 65 6c 65  >ht[h];..    ele
44e1d 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  m = pEntry->chai
44e1e 6e 3b 0d 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20  n;..    count = 
44e1f 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0d 0a  pEntry->count;..
44e20 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d 20 66      xCompare = f
44e21 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f  tsCompareFunctio
44e22 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b  n(pH->keyClass);
44e23 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6f 75  ..    while( cou
44e24 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0d  nt-- && elem ){.
44e25 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78 43 6f  .      if( (*xCo
44e26 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b 65  mpare)(elem->pKe
44e27 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65  y,elem->nKey,pKe
44e28 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0d 0a  y,nKey)==0 ){ ..
44e29 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 65          return e
44e2a 6c 65 6d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  lem;..      }.. 
44e2b 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d       elem = elem
44e2c 2d 3e 6e 65 78 74 3b 0d 0a 20 20 20 20 7d 0d 0a  ->next;..    }..
44e2d 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
44e2e 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 52 65 6d 6f 76 65  ..}..../* Remove
44e2f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20   a single entry 
44e30 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
44e31 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e  ble given a poin
44e32 74 65 72 20 74 6f 20 74 68 61 74 0d 0a 2a 2a 20  ter to that..** 
44e33 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61  element and a ha
44e34 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e  sh on the elemen
44e35 74 27 73 20 6b 65 79 2e 0d 0a 2a 2f 0d 0a 73 74  t's key...*/..st
44e36 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
44e37 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73  moveElementByHas
44e38 68 28 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a  h(..  Fts3Hash *
44e39 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  pH,         /* T
44e3a 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67  he pH containing
44e3b 20 22 65 6c 65 6d 22 20 2a 2f 0d 0a 20 20 46 74   "elem" */..  Ft
44e3c 73 33 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d  s3HashElem* elem
44e3d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65  ,   /* The eleme
44e3e 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64  nt to be removed
44e3f 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0d   from the pH */.
44e40 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20  .  int h        
44e41 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
44e42 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65   value for the e
44e43 6c 65 6d 65 6e 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  lement */..){.. 
44e44 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
44e45 2a 70 45 6e 74 72 79 3b 0d 0a 20 20 69 66 28 20  *pEntry;..  if( 
44e46 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0d 0a 20  elem->prev ){.. 
44e47 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e     elem->prev->n
44e48 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ext = elem->next
44e49 3b 20 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ; ..  }else{..  
44e4a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c    pH->first = el
44e4b 65 6d 2d 3e 6e 65 78 74 3b 0d 0a 20 20 7d 0d 0a  em->next;..  }..
44e4c 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74    if( elem->next
44e4d 20 29 7b 0d 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e   ){..    elem->n
44e4e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d  ext->prev = elem
44e4f 2d 3e 70 72 65 76 3b 0d 0a 20 20 7d 0d 0a 20 20  ->prev;..  }..  
44e50 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
44e51 5b 68 5d 3b 0d 0a 20 20 69 66 28 20 70 45 6e 74  [h];..  if( pEnt
44e52 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20  ry->chain==elem 
44e53 29 7b 0d 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e  ){..    pEntry->
44e54 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65  chain = elem->ne
44e55 78 74 3b 0d 0a 20 20 7d 0d 0a 20 20 70 45 6e 74  xt;..  }..  pEnt
44e56 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0d 0a 20 20  ry->count--;..  
44e57 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  if( pEntry->coun
44e58 74 3c 3d 30 20 29 7b 0d 0a 20 20 20 20 70 45 6e  t<=0 ){..    pEn
44e59 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b 0d  try->chain = 0;.
44e5a 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70 48 2d 3e  .  }..  if( pH->
44e5b 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d  copyKey && elem-
44e5c 3e 70 4b 65 79 20 29 7b 0d 0a 20 20 20 20 66 74  >pKey ){..    ft
44e5d 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d  s3HashFree(elem-
44e5e 3e 70 4b 65 79 29 3b 0d 0a 20 20 7d 0d 0a 20 20  >pKey);..  }..  
44e5f 66 74 73 33 48 61 73 68 46 72 65 65 28 20 65 6c  fts3HashFree( el
44e60 65 6d 20 29 3b 0d 0a 20 20 70 48 2d 3e 63 6f 75  em );..  pH->cou
44e61 6e 74 2d 2d 3b 0d 0a 20 20 69 66 28 20 70 48 2d  nt--;..  if( pH-
44e62 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0d 0a 20 20  >count<=0 ){..  
44e63 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69    assert( pH->fi
44e64 72 73 74 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 61  rst==0 );..    a
44e65 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74  ssert( pH->count
44e66 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 66 74 73 33  ==0 );..    fts3
44e67 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0d 0a  HashClear(pH);..
44e68 20 20 7d 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45    }..}....SQLITE
44e69 5f 50 52 49 56 41 54 45 20 46 74 73 33 48 61 73  _PRIVATE Fts3Has
44e6a 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 46 74  hElem *sqlite3Ft
44e6b 73 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 0d  s3HashFindElem(.
44e6c 0a 20 20 63 6f 6e 73 74 20 46 74 73 33 48 61 73  .  const Fts3Has
44e6d 68 20 2a 70 48 2c 20 0d 0a 20 20 63 6f 6e 73 74  h *pH, ..  const
44e6e 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 0d 0a 20   void *pKey, .. 
44e6f 20 69 6e 74 20 6e 4b 65 79 0d 0a 29 7b 0d 0a 20   int nKey..){.. 
44e70 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
44e71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e72 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65   /* A hash on ke
44e73 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 48  y */..  int (*xH
44e74 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ash)(const void*
44e75 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68  ,int);  /* The h
44e76 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d  ash function */.
44e77 0a 0d 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c  ...  if( pH==0 |
44e78 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65  | pH->ht==0 ) re
44e79 74 75 72 6e 20 30 3b 0d 0a 20 20 78 48 61 73 68  turn 0;..  xHash
44e7a 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69   = ftsHashFuncti
44e7b 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29  on(pH->keyClass)
44e7c 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 78 48 61  ;..  assert( xHa
44e7d 73 68 21 3d 30 20 29 3b 0d 0a 20 20 68 20 3d 20  sh!=0 );..  h = 
44e7e 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 6e 4b  (*xHash)(pKey,nK
44e7f 65 79 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20  ey);..  assert( 
44e80 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
44e81 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
44e82 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 66 74   );..  return ft
44e83 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48  s3FindElementByH
44e84 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79  ash(pH,pKey,nKey
44e85 2c 20 68 20 26 20 28 70 48 2d 3e 68 74 73 69 7a  , h & (pH->htsiz
44e86 65 2d 31 29 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20  e-1));..}..../* 
44e87 0d 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ..** Attempt to 
44e88 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
44e89 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
44e8a 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
44e8b 79 0d 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68  y..** that match
44e8c 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52  es pKey,nKey.  R
44e8d 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 66  eturn the data f
44e8e 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  or this element 
44e8f 69 66 20 69 74 20 69 73 0d 0a 2a 2a 20 66 6f 75  if it is..** fou
44e90 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  nd, or NULL if t
44e91 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
44e92 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
44e93 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
44e94 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28  te3Fts3HashFind(
44e95 63 6f 6e 73 74 20 46 74 73 33 48 61 73 68 20 2a  const Fts3Hash *
44e96 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  pH, const void *
44e97 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b  pKey, int nKey){
44e98 0d 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  ..  Fts3HashElem
44e99 20 2a 70 45 6c 65 6d 3b 20 20 20 20 20 20 20 20   *pElem;        
44e9a 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65      /* The eleme
44e9b 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  nt that matches 
44e9c 6b 65 79 20 28 69 66 20 61 6e 79 29 20 2a 2f 0d  key (if any) */.
44e9d 0a 0d 0a 20 20 70 45 6c 65 6d 20 3d 20 73 71 6c  ...  pElem = sql
44e9e 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
44e9f 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79 2c 20 6e  Elem(pH, pKey, n
44ea0 4b 65 79 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  Key);..  return 
44ea1 70 45 6c 65 6d 20 3f 20 70 45 6c 65 6d 2d 3e 64  pElem ? pElem->d
44ea2 61 74 61 20 3a 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  ata : 0;..}..../
44ea3 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d  * Insert an elem
44ea4 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
44ea5 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65  h table pH.  The
44ea6 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65   key is pKey,nKe
44ea7 79 0d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  y..** and the da
44ea8 74 61 20 69 73 20 22 64 61 74 61 22 2e 0d 0a 2a  ta is "data"...*
44ea9 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  *..** If no elem
44eaa 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
44eab 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
44eac 74 68 65 6e 20 61 20 6e 65 77 0d 0a 2a 2a 20 65  then a new..** e
44ead 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65  lement is create
44eae 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68  d.  A copy of th
44eaf 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66  e key is made if
44eb0 20 74 68 65 20 63 6f 70 79 4b 65 79 0d 0a 2a 2a   the copyKey..**
44eb1 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e   flag is set.  N
44eb2 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
44eb3 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e 6f 74  ..**..** If anot
44eb4 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65  her element alre
44eb5 61 64 79 20 65 78 69 73 74 73 20 77 69 74 68 20  ady exists with 
44eb6 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68  the same key, th
44eb7 65 6e 20 74 68 65 0d 0a 2a 2a 20 6e 65 77 20 64  en the..** new d
44eb8 61 74 61 20 72 65 70 6c 61 63 65 73 20 74 68 65  ata replaces the
44eb9 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68   old data and th
44eba 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65  e old data is re
44ebb 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20 54 68 65 20  turned...** The 
44ebc 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65  key is not copie
44ebd 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e  d in this instan
44ebe 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63  ce.  If a malloc
44ebf 20 66 61 69 6c 73 2c 20 74 68 65 6e 0d 0a 2a 2a   fails, then..**
44ec0 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73   the new data is
44ec1 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
44ec2 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
44ec3 75 6e 63 68 61 6e 67 65 64 2e 0d 0a 2a 2a 0d 0a  unchanged...**..
44ec4 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22  ** If the "data"
44ec5 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
44ec6 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  is function is N
44ec7 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0d 0a 2a  ULL, then the..*
44ec8 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73  * element corres
44ec9 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22  ponding to "key"
44eca 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
44ecb 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
44ecc 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
44ecd 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
44ece 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
44ecf 28 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70  (..  Fts3Hash *p
44ed0 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  H,        /* The
44ed1 20 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69   hash table to i
44ed2 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0d 0a 20  nsert into */.. 
44ed3 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
44ed4 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
44ed5 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4b 65 79 2c   */..  int nKey,
44ed6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
44ed7 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
44ed8 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0d 0a 20 20  n the key */..  
44ed9 76 6f 69 64 20 2a 64 61 74 61 20 20 20 20 20 20  void *data      
44eda 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
44edb 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 68   */..){..  int h
44edc 72 61 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  raw;            
44edd 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68       /* Raw hash
44ede 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
44edf 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 68 3b 20 20  y */..  int h;  
44ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ee1 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66    /* the hash of
44ee2 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20   the key modulo 
44ee3 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20  hash table size 
44ee4 2a 2f 0d 0a 20 20 46 74 73 33 48 61 73 68 45 6c  */..  Fts3HashEl
44ee5 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
44ee6 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
44ee7 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
44ee8 20 6c 69 73 74 20 2a 2f 0d 0a 20 20 46 74 73 33   list */..  Fts3
44ee9 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c  HashElem *new_el
44eea 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65  em;   /* New ele
44eeb 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68  ment added to th
44eec 65 20 70 48 20 2a 2f 0d 0a 20 20 69 6e 74 20 28  e pH */..  int (
44eed 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f  *xHash)(const vo
44eee 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68  id*,int);  /* Th
44eef 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
44ef0 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  */....  assert( 
44ef1 70 48 21 3d 30 20 29 3b 0d 0a 20 20 78 48 61 73  pH!=0 );..  xHas
44ef2 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74  h = ftsHashFunct
44ef3 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73  ion(pH->keyClass
44ef4 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 78 48  );..  assert( xH
44ef5 61 73 68 21 3d 30 20 29 3b 0d 0a 20 20 68 72 61  ash!=0 );..  hra
44ef6 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65  w = (*xHash)(pKe
44ef7 79 2c 20 6e 4b 65 79 29 3b 0d 0a 20 20 61 73 73  y, nKey);..  ass
44ef8 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
44ef9 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
44efa 29 29 3d 3d 30 20 29 3b 0d 0a 20 20 68 20 3d 20  ))==0 );..  h = 
44efb 68 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69  hraw & (pH->htsi
44efc 7a 65 2d 31 29 3b 0d 0a 20 20 65 6c 65 6d 20 3d  ze-1);..  elem =
44efd 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74   fts3FindElement
44efe 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e  ByHash(pH,pKey,n
44eff 4b 65 79 2c 68 29 3b 0d 0a 20 20 69 66 28 20 65  Key,h);..  if( e
44f00 6c 65 6d 20 29 7b 0d 0a 20 20 20 20 76 6f 69 64  lem ){..    void
44f01 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65   *old_data = ele
44f02 6d 2d 3e 64 61 74 61 3b 0d 0a 20 20 20 20 69 66  m->data;..    if
44f03 28 20 64 61 74 61 3d 3d 30 20 29 7b 0d 0a 20 20  ( data==0 ){..  
44f04 20 20 20 20 66 74 73 33 52 65 6d 6f 76 65 45 6c      fts3RemoveEl
44f05 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c 65  ementByHash(pH,e
44f06 6c 65 6d 2c 68 29 3b 0d 0a 20 20 20 20 7d 65 6c  lem,h);..    }el
44f07 73 65 7b 0d 0a 20 20 20 20 20 20 65 6c 65 6d 2d  se{..      elem-
44f08 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0d 0a 20  >data = data;.. 
44f09 20 20 20 7d 0d 0a 20 20 20 20 72 65 74 75 72 6e     }..    return
44f0a 20 6f 6c 64 5f 64 61 74 61 3b 0d 0a 20 20 7d 0d   old_data;..  }.
44f0b 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29  .  if( data==0 )
44f0c 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20 69 66   return 0;..  if
44f0d 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30  ( (pH->htsize==0
44f0e 20 26 26 20 66 74 73 33 52 65 68 61 73 68 28 70   && fts3Rehash(p
44f0f 48 2c 38 29 29 0d 0a 20 20 20 7c 7c 20 28 70 48  H,8))..   || (pH
44f10 2d 3e 63 6f 75 6e 74 3e 3d 70 48 2d 3e 68 74 73  ->count>=pH->hts
44f11 69 7a 65 20 26 26 20 66 74 73 33 52 65 68 61 73  ize && fts3Rehas
44f12 68 28 70 48 2c 20 70 48 2d 3e 68 74 73 69 7a 65  h(pH, pH->htsize
44f13 2a 32 29 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20  *2))..  ){..    
44f14 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0d 0a  pH->count = 0;..
44f15 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b      return data;
44f16 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74 28  ..  }..  assert(
44f17 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b   pH->htsize>0 );
44f18 0d 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28  ..  new_elem = (
44f19 46 74 73 33 48 61 73 68 45 6c 65 6d 2a 29 66 74  Fts3HashElem*)ft
44f1a 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 73 69  s3HashMalloc( si
44f1b 7a 65 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65  zeof(Fts3HashEle
44f1c 6d 29 20 29 3b 0d 0a 20 20 69 66 28 20 6e 65 77  m) );..  if( new
44f1d 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72  _elem==0 ) retur
44f1e 6e 20 64 61 74 61 3b 0d 0a 20 20 69 66 28 20 70  n data;..  if( p
44f1f 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b  H->copyKey && pK
44f20 65 79 21 3d 30 20 29 7b 0d 0a 20 20 20 20 6e 65  ey!=0 ){..    ne
44f21 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 66  w_elem->pKey = f
44f22 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e  ts3HashMalloc( n
44f23 4b 65 79 20 29 3b 0d 0a 20 20 20 20 69 66 28 20  Key );..    if( 
44f24 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d  new_elem->pKey==
44f25 30 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33  0 ){..      fts3
44f26 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65  HashFree(new_ele
44f27 6d 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  m);..      retur
44f28 6e 20 64 61 74 61 3b 0d 0a 20 20 20 20 7d 0d 0a  n data;..    }..
44f29 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64      memcpy((void
44f2a 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79  *)new_elem->pKey
44f2b 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0d 0a  , pKey, nKey);..
44f2c 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 6e 65    }else{..    ne
44f2d 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28  w_elem->pKey = (
44f2e 76 6f 69 64 2a 29 70 4b 65 79 3b 0d 0a 20 20 7d  void*)pKey;..  }
44f2f 0d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b  ..  new_elem->nK
44f30 65 79 20 3d 20 6e 4b 65 79 3b 0d 0a 20 20 70 48  ey = nKey;..  pH
44f31 2d 3e 63 6f 75 6e 74 2b 2b 3b 0d 0a 20 20 61 73  ->count++;..  as
44f32 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65  sert( pH->htsize
44f33 3e 30 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  >0 );..  assert(
44f34 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28   (pH->htsize & (
44f35 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d  pH->htsize-1))==
44f36 30 20 29 3b 0d 0a 20 20 68 20 3d 20 68 72 61 77  0 );..  h = hraw
44f37 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
44f38 29 3b 0d 0a 20 20 66 74 73 33 48 61 73 68 49 6e  );..  fts3HashIn
44f39 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
44f3a 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f  &pH->ht[h], new_
44f3b 65 6c 65 6d 29 3b 0d 0a 20 20 6e 65 77 5f 65 6c  elem);..  new_el
44f3c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
44f3d 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
44f3e 0d 0a 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ....#endif /* !d
44f3f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
44f40 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
44f41 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
44f42 33 29 20 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a  3) */..../******
44f43 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
44f44 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a  fts3_hash.c ****
44f45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f47 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a  *******/../*****
44f48 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
44f49 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65 72  file fts3_porter
44f4a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
44f4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f4c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a  ********/../*..*
44f4d 2a 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72  * 2006 September
44f4e 20 33 30 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20   30..**..** The 
44f4f 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
44f50 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
44f51 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
44f52 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a 2a 2a   In place of..**
44f53 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
44f54 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
44f55 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  ing:..**..**    
44f56 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
44f57 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d 0a 2a  and not evil...*
44f58 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
44f59 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
44f5a 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
44f5b 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0d 0a  orgive others...
44f5c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
44f5d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
44f5e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
44f5f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a 2a 2a  an you give...**
44f60 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
44f61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44f64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 20  ***********..** 
44f65 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
44f66 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 2d  f the full-text-
44f67 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72  search tokenizer
44f68 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
44f69 0d 0a 2a 2a 20 61 20 50 6f 72 74 65 72 20 73 74  ..** a Porter st
44f6a 65 6d 6d 65 72 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a  emmer...*/..../*
44f6b 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  ..** The code in
44f6c 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
44f6d 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0d  ly compiled if:.
44f6e 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 54 68  .**..**     * Th
44f6f 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
44f70 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
44f71 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0d 0a 2a 2a  an extension..**
44f72 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68         (in which
44f73 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
44f74 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  E is not defined
44f75 29 2c 20 6f 72 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ), or..**..**   
44f76 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
44f77 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
44f78 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
44f79 20 6f 66 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51   of..**       SQ
44f7a 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63  Lite (in which c
44f7b 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ase SQLITE_ENABL
44f7c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65  E_FTS3 is define
44f7d 64 29 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65  d)...*/..#if !de
44f7e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
44f7f 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
44f80 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
44f81 29 0d 0a 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  )..../* #include
44f82 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a   <assert.h> */..
44f83 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  /* #include <std
44f84 6c 69 62 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69  lib.h> */../* #i
44f85 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
44f86 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65   */../* #include
44f87 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d 0a   <string.h> */..
44f88 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 61 73 73  ..../*..** Class
44f89 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71   derived from sq
44f8a 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0d  lite3_tokenizer.
44f8b 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72  .*/..typedef str
44f8c 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  uct porter_token
44f8d 69 7a 65 72 20 7b 0d 0a 20 20 73 71 6c 69 74 65  izer {..  sqlite
44f8e 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65  3_tokenizer base
44f8f 3b 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63  ;      /* Base c
44f90 6c 61 73 73 20 2a 2f 0d 0a 7d 20 70 6f 72 74 65  lass */..} porte
44f91 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 0d 0a  r_tokenizer;....
44f92 2f 2a 0d 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72  /*..** Class der
44f93 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ived from sqlite
44f94 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
44f95 6f 72 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20  or..*/..typedef 
44f96 73 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f  struct porter_to
44f97 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b  kenizer_cursor {
44f98 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ..  sqlite3_toke
44f99 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73  nizer_cursor bas
44f9a 65 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  e;..  const char
44f9b 20 2a 7a 49 6e 70 75 74 3b 20 20 20 20 20 20 20   *zInput;       
44f9c 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20 61     /* input we a
44f9d 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f  re tokenizing */
44f9e 0d 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 20  ..  int nInput; 
44f9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44fa0 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20   /* size of the 
44fa1 69 6e 70 75 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  input */..  int 
44fa2 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
44fa3 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
44fa4 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
44fa5 7a 49 6e 70 75 74 20 2a 2f 0d 0a 20 20 69 6e 74  zInput */..  int
44fa6 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   iToken;        
44fa7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64            /* ind
44fa8 65 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e  ex of next token
44fa9 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
44faa 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b  */..  char *zTok
44fab 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
44fac 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f     /* storage fo
44fad 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20  r current token 
44fae 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63  */..  int nAlloc
44faf 61 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  ated;           
44fb0 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f     /* space allo
44fb1 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20  cated to zToken 
44fb2 62 75 66 66 65 72 20 2a 2f 0d 0a 7d 20 70 6f 72  buffer */..} por
44fb3 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ter_tokenizer_cu
44fb4 72 73 6f 72 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  rsor;....../*..*
44fb5 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74  * Create a new t
44fb6 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63  okenizer instanc
44fb7 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
44fb8 6e 74 20 70 6f 72 74 65 72 43 72 65 61 74 65 28  nt porterCreate(
44fb9 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  ..  int argc, co
44fba 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
44fbb 20 2a 61 72 67 76 2c 0d 0a 20 20 73 71 6c 69 74   *argv,..  sqlit
44fbc 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70  e3_tokenizer **p
44fbd 70 54 6f 6b 65 6e 69 7a 65 72 0d 0a 29 7b 0d 0a  pTokenizer..){..
44fbe 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a    porter_tokeniz
44fbf 65 72 20 2a 74 3b 0d 0a 0d 0a 20 20 55 4e 55 53  er *t;....  UNUS
44fc0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
44fc1 63 29 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  c);..  UNUSED_PA
44fc2 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0d 0a  RAMETER(argv);..
44fc3 0d 0a 20 20 74 20 3d 20 28 70 6f 72 74 65 72 5f  ..  t = (porter_
44fc4 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c  tokenizer *) sql
44fc5 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
44fc6 6f 66 28 2a 74 29 29 3b 0d 0a 20 20 69 66 28 20  of(*t));..  if( 
44fc7 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  t==NULL ) return
44fc8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
44fc9 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73    memset(t, 0, s
44fca 69 7a 65 6f 66 28 2a 74 29 29 3b 0d 0a 20 20 2a  izeof(*t));..  *
44fcb 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74  ppTokenizer = &t
44fcc 2d 3e 62 61 73 65 3b 0d 0a 20 20 72 65 74 75 72  ->base;..  retur
44fcd 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
44fce 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 73 74 72 6f  .../*..** Destro
44fcf 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0d 0a 2a  y a tokenizer..*
44fd0 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  /..static int po
44fd1 72 74 65 72 44 65 73 74 72 6f 79 28 73 71 6c 69  rterDestroy(sqli
44fd2 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
44fd3 54 6f 6b 65 6e 69 7a 65 72 29 7b 0d 0a 20 20 73  Tokenizer){..  s
44fd4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b  qlite3_free(pTok
44fd5 65 6e 69 7a 65 72 29 3b 0d 0a 20 20 72 65 74 75  enizer);..  retu
44fd6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
44fd7 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72 65 70 61  ..../*..** Prepa
44fd8 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
44fd9 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
44fda 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
44fdb 20 69 6e 70 75 74 0d 0a 2a 2a 20 73 74 72 69 6e   input..** strin
44fdc 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
44fdd 64 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e  d is zInput[0..n
44fde 49 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75 72  Input-1].  A cur
44fdf 73 6f 72 0d 0a 2a 2a 20 75 73 65 64 20 74 6f 20  sor..** used to 
44fe0 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f  incrementally to
44fe1 6b 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69  kenize this stri
44fe2 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ng is returned i
44fe3 6e 20 0d 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72  n ..** *ppCursor
44fe4 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
44fe5 74 20 70 6f 72 74 65 72 4f 70 65 6e 28 0d 0a 20  t porterOpen(.. 
44fe6 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
44fe7 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
44fe8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
44fe9 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0d 0a 20 20 63  okenizer */..  c
44fea 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75  onst char *zInpu
44feb 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20  t, int nInput,  
44fec 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
44fed 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
44fee 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  */..  sqlite3_to
44fef 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
44ff0 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20  *ppCursor    /* 
44ff1 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f  OUT: Tokenizatio
44ff2 6e 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 29 7b 0d  n cursor */..){.
44ff3 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
44ff4 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0d 0a  zer_cursor *c;..
44ff5 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
44ff6 45 54 45 52 28 70 54 6f 6b 65 6e 69 7a 65 72 29  ETER(pTokenizer)
44ff7 3b 0d 0a 0d 0a 20 20 63 20 3d 20 28 70 6f 72 74  ;....  c = (port
44ff8 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
44ff9 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d  sor *) sqlite3_m
44ffa 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29  alloc(sizeof(*c)
44ffb 29 3b 0d 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c  );..  if( c==NUL
44ffc 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
44ffd 45 5f 4e 4f 4d 45 4d 3b 0d 0a 0d 0a 20 20 63 2d  E_NOMEM;....  c-
44ffe 3e 7a 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74  >zInput = zInput
44fff 3b 0d 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d  ;..  if( zInput=
45000 3d 30 20 29 7b 0d 0a 20 20 20 20 63 2d 3e 6e 49  =0 ){..    c->nI
45001 6e 70 75 74 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  nput = 0;..  }el
45002 73 65 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20  se if( nInput<0 
45003 29 7b 0d 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75  ){..    c->nInpu
45004 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  t = (int)strlen(
45005 7a 49 6e 70 75 74 29 3b 0d 0a 20 20 7d 65 6c 73  zInput);..  }els
45006 65 7b 0d 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75  e{..    c->nInpu
45007 74 20 3d 20 6e 49 6e 70 75 74 3b 0d 0a 20 20 7d  t = nInput;..  }
45008 0d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d  ..  c->iOffset =
45009 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
4500a 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b      /* start tok
4500b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62  enizing at the b
4500c 65 67 69 6e 6e 69 6e 67 20 2a 2f 0d 0a 20 20 63  eginning */..  c
4500d 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0d 0a 20  ->iToken = 0;.. 
4500e 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c   c->zToken = NUL
4500f 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L;              
45010 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c   /* no space all
45011 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0d  ocated, yet. */.
45012 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64  .  c->nAllocated
45013 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2a 70 70 43 75   = 0;....  *ppCu
45014 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b  rsor = &c->base;
45015 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
45016 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
45017 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e  ** Close a token
45018 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70  ization cursor p
45019 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64  reviously opened
4501a 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0d 0a 2a   by a call to..*
4501b 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 20 61  * porterOpen() a
4501c 62 6f 76 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  bove...*/..stati
4501d 63 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f 73  c int porterClos
4501e 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
4501f 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
45020 73 6f 72 29 7b 0d 0a 20 20 70 6f 72 74 65 72 5f  sor){..  porter_
45021 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
45022 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f   *c = (porter_to
45023 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
45024 29 20 70 43 75 72 73 6f 72 3b 0d 0a 20 20 73 71  ) pCursor;..  sq
45025 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54  lite3_free(c->zT
45026 6f 6b 65 6e 29 3b 0d 0a 20 20 73 71 6c 69 74 65  oken);..  sqlite
45027 33 5f 66 72 65 65 28 63 29 3b 0d 0a 20 20 72 65  3_free(c);..  re
45028 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
45029 0a 7d 0d 0a 2f 2a 0d 0a 2a 2a 20 56 6f 77 65 6c  .}../*..** Vowel
4502a 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74 0d 0a 2a   or consonant..*
4502b 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  /..static const 
4502c 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d 20 7b  char cType[] = {
4502d 0d 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  ..   0, 1, 1, 1,
4502e 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
4502f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
45030 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
45031 20 30 2c 0d 0a 20 20 20 31 2c 20 31 2c 20 31 2c   0,..   1, 1, 1,
45032 20 32 2c 20 31 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d   2, 1..};..../*.
45033 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  .** isConsonant(
45034 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28 29 20  ) and isVowel() 
45035 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65  determine if the
45036 69 72 20 66 69 72 73 74 20 63 68 61 72 61 63 74  ir first charact
45037 65 72 20 69 6e 0d 0a 2a 2a 20 74 68 65 20 73 74  er in..** the st
45038 72 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74 20  ring they point 
45039 74 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e  to is a consonan
4503a 74 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61 63  t or a vowel, ac
4503b 63 6f 72 64 69 6e 67 0d 0a 2a 2a 20 74 6f 20 50  cording..** to P
4503c 6f 72 74 65 72 20 72 75 6c 73 2e 20 20 0d 0a 2a  orter ruls.  ..*
4503d 2a 0d 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61 74  *..** A consonat
4503e 65 20 69 73 20 61 6e 79 20 6c 65 74 74 65 72 20  e is any letter 
4503f 6f 74 68 65 72 20 74 68 61 6e 20 27 61 27 2c 20  other than 'a', 
45040 27 65 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20 6f  'e', 'i', 'o', o
45041 72 20 27 75 27 2e 0d 0a 2a 2a 20 27 59 27 20 69  r 'u'...** 'Y' i
45042 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e  s a consonant un
45043 6c 65 73 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20  less it follows 
45044 61 6e 6f 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e  another consonan
45045 74 2c 0d 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20  t,..** in which 
45046 63 61 73 65 20 69 74 20 69 73 20 61 20 76 6f 77  case it is a vow
45047 65 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 20 74  el...**..** In t
45048 68 65 73 65 20 72 6f 75 74 69 6e 65 2c 20 74 68  hese routine, th
45049 65 20 6c 65 74 74 65 72 73 20 61 72 65 20 69 6e  e letters are in
4504a 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20   reverse order. 
4504b 20 53 6f 20 74 68 65 20 27 79 27 20 72 75 6c 65   So the 'y' rule
4504c 0d 0a 2a 2a 20 69 73 20 74 68 61 74 20 27 79 27  ..** is that 'y'
4504d 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20   is a consonant 
4504e 75 6e 6c 65 73 73 20 69 74 20 69 73 20 66 6f 6c  unless it is fol
4504f 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74 68 65 72  lowed by another
45050 0d 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e 0d  ..** consonent..
45051 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
45052 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  isVowel(const ch
45053 61 72 2a 29 3b 0d 0a 73 74 61 74 69 63 20 69 6e  ar*);..static in
45054 74 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f  t isConsonant(co
45055 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 20  nst char *z){.. 
45056 20 69 6e 74 20 6a 3b 0d 0a 20 20 63 68 61 72 20   int j;..  char 
45057 78 20 3d 20 2a 7a 3b 0d 0a 20 20 69 66 28 20 78  x = *z;..  if( x
45058 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ==0 ) return 0;.
45059 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61  .  assert( x>='a
4505a 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0d 0a  ' && x<='z' );..
4505b 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61    j = cType[x-'a
4505c 27 5d 3b 0d 0a 20 20 69 66 28 20 6a 3c 32 20 29  '];..  if( j<2 )
4505d 20 72 65 74 75 72 6e 20 6a 3b 0d 0a 20 20 72 65   return j;..  re
4505e 74 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20  turn z[1]==0 || 
4505f 69 73 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b 0d  isVowel(z + 1);.
45060 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .}..static int i
45061 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61  sVowel(const cha
45062 72 20 2a 7a 29 7b 0d 0a 20 20 69 6e 74 20 6a 3b  r *z){..  int j;
45063 0d 0a 20 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b  ..  char x = *z;
45064 0d 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72  ..  if( x==0 ) r
45065 65 74 75 72 6e 20 30 3b 0d 0a 20 20 61 73 73 65  eturn 0;..  asse
45066 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c  rt( x>='a' && x<
45067 3d 27 7a 27 20 29 3b 0d 0a 20 20 6a 20 3d 20 63  ='z' );..  j = c
45068 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0d 0a 20 20  Type[x-'a'];..  
45069 69 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e  if( j<2 ) return
4506a 20 31 2d 6a 3b 0d 0a 20 20 72 65 74 75 72 6e 20   1-j;..  return 
4506b 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20  isConsonant(z + 
4506c 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  1);..}..../*..**
4506d 20 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e 63   Let any sequenc
4506e 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  e of one or more
4506f 20 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72 65   vowels be repre
45070 73 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64 20  sented by V and 
45071 6c 65 74 0d 0a 2a 2a 20 43 20 62 65 20 73 65 71  let..** C be seq
45072 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
45073 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e  more consonants.
45074 20 20 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72    Then every wor
45075 64 20 63 61 6e 20 62 65 0d 0a 2a 2a 20 72 65 70  d can be..** rep
45076 72 65 73 65 6e 74 65 64 20 61 73 3a 0d 0a 2a 2a  resented as:..**
45077 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  ..**           [
45078 43 5d 20 28 56 43 29 7b 6d 7d 20 5b 56 5d 0d 0a  C] (VC){m} [V]..
45079 2a 2a 0d 0a 2a 2a 20 49 6e 20 70 72 6f 73 65 3a  **..** In prose:
4507a 20 20 41 20 77 6f 72 64 20 69 73 20 61 6e 20 6f    A word is an o
4507b 70 74 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e  ptional consonan
4507c 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 7a 65  t followed by ze
4507d 72 6f 20 6f 72 0d 0a 2a 2a 20 76 6f 77 65 6c 2d  ro or..** vowel-
4507e 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 20  consonant pairs 
4507f 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 6f  followed by an o
45080 70 74 69 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20 20  ptional vowel.  
45081 22 6d 22 20 69 73 20 74 68 65 0d 0a 2a 2a 20 6e  "m" is the..** n
45082 75 6d 62 65 72 20 6f 66 20 76 6f 77 65 6c 20 63  umber of vowel c
45083 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 2e 20  onsonant pairs. 
45084 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
45085 6d 70 75 74 65 73 20 74 68 65 20 76 61 6c 75 65  mputes the value
45086 0d 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20 74 68  ..** of m for th
45087 65 20 66 69 72 73 74 20 69 20 62 79 74 65 73 20  e first i bytes 
45088 6f 66 20 61 20 77 6f 72 64 2e 0d 0a 2a 2a 0d 0a  of a word...**..
45089 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
4508a 66 20 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f  f the m-value fo
4508b 72 20 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65  r z is 1 or more
4508c 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
4508d 73 2c 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72  s,..** return tr
4508e 75 65 20 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73  ue if z contains
4508f 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f   at least one vo
45090 77 65 6c 20 74 68 61 74 20 69 73 20 66 6f 6c 6c  wel that is foll
45091 6f 77 65 64 0d 0a 2a 2a 20 62 79 20 61 20 63 6f  owed..** by a co
45092 6e 73 6f 6e 61 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a  nsonant...**..**
45093 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   In this routine
45094 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76 65 72   z[] is in rever
45095 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 77 65  se order.  So we
45096 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b   are really look
45097 69 6e 67 0d 0a 2a 2a 20 66 6f 72 20 61 6e 20 69  ing..** for an i
45098 6e 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61 20  nstance of of a 
45099 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77  consonant follow
4509a 65 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0d 0a  ed by a vowel...
4509b 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  */..static int m
4509c 5f 67 74 5f 30 28 63 6f 6e 73 74 20 63 68 61 72  _gt_0(const char
4509d 20 2a 7a 29 7b 0d 0a 20 20 77 68 69 6c 65 28 20   *z){..  while( 
4509e 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b  isVowel(z) ){ z+
4509f 2b 3b 20 7d 0d 0a 20 20 69 66 28 20 2a 7a 3d 3d  +; }..  if( *z==
450a0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20  0 ) return 0;.. 
450a1 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e   while( isConson
450a2 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  ant(z) ){ z++; }
450a3 0d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30  ..  return *z!=0
450a4 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 4c 69 6b 65 20  ;..}..../* Like 
450a5 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70  mgt0 above excep
450a6 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  t we are looking
450a7 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20   for a value of 
450a8 6d 20 77 68 69 63 68 20 69 73 0d 0a 2a 2a 20 65  m which is..** e
450a9 78 61 63 74 6c 79 20 31 0d 0a 2a 2f 0d 0a 73 74  xactly 1..*/..st
450aa 61 74 69 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28  atic int m_eq_1(
450ab 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0d  const char *z){.
450ac 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
450ad 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a  l(z) ){ z++; }..
450ae 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
450af 74 75 72 6e 20 30 3b 0d 0a 20 20 77 68 69 6c 65  turn 0;..  while
450b0 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
450b1 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a 20 20 69 66   ){ z++; }..  if
450b2 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
450b3 20 30 3b 0d 0a 20 20 77 68 69 6c 65 28 20 69 73   0;..  while( is
450b4 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
450b5 20 7d 0d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20   }..  if( *z==0 
450b6 29 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 77  ) return 1;..  w
450b7 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e  hile( isConsonan
450b8 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a  t(z) ){ z++; }..
450b9 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0d    return *z==0;.
450ba 0a 7d 0d 0a 0d 0a 2f 2a 20 4c 69 6b 65 20 6d 67  .}..../* Like mg
450bb 74 30 20 61 62 6f 76 65 20 65 78 63 65 70 74 20  t0 above except 
450bc 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66  we are looking f
450bd 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20 6d 3e  or a value of m>
450be 31 20 69 6e 73 74 65 61 64 0d 0a 2a 2a 20 6f 72  1 instead..** or
450bf 20 6d 3e 30 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63   m>0..*/..static
450c0 20 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e 73   int m_gt_1(cons
450c1 74 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 20 20 77  t char *z){..  w
450c2 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
450c3 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a 20 20 69 66   ){ z++; }..  if
450c4 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
450c5 20 30 3b 0d 0a 20 20 77 68 69 6c 65 28 20 69 73   0;..  while( is
450c6 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
450c7 7a 2b 2b 3b 20 7d 0d 0a 20 20 69 66 28 20 2a 7a  z++; }..  if( *z
450c8 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  ==0 ) return 0;.
450c9 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
450ca 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a  l(z) ){ z++; }..
450cb 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
450cc 74 75 72 6e 20 30 3b 0d 0a 20 20 77 68 69 6c 65  turn 0;..  while
450cd 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
450ce 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a 20 20 72 65   ){ z++; }..  re
450cf 74 75 72 6e 20 2a 7a 21 3d 30 3b 0d 0a 7d 0d 0a  turn *z!=0;..}..
450d0 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ../*..** Return 
450d1 54 52 55 45 20 69 66 20 74 68 65 72 65 20 69 73  TRUE if there is
450d2 20 61 20 76 6f 77 65 6c 20 61 6e 79 77 68 65 72   a vowel anywher
450d3 65 20 77 69 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d  e within z[0..n-
450d4 31 5d 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  1]..*/..static i
450d5 6e 74 20 68 61 73 56 6f 77 65 6c 28 63 6f 6e 73  nt hasVowel(cons
450d6 74 20 63 68 61 72 20 2a 7a 29 7b 0d 0a 20 20 77  t char *z){..  w
450d7 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e  hile( isConsonan
450d8 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0d 0a  t(z) ){ z++; }..
450d9 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0d    return *z!=0;.
450da 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74  .}..../*..** Ret
450db 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
450dc 77 6f 72 64 20 65 6e 64 73 20 69 6e 20 61 20 64  word ends in a d
450dd 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61 6e 74 2e  ouble consonant.
450de 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 65 78  ..**..** The tex
450df 74 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65  t is reversed he
450e0 72 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72 65  re. So we are re
450e1 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 0d  ally looking at.
450e2 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77  .** the first tw
450e3 6f 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20  o characters of 
450e4 7a 5b 5d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  z[]...*/..static
450e5 20 69 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73 6f   int doubleConso
450e6 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  nant(const char 
450e7 2a 7a 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 69  *z){..  return i
450e8 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26  sConsonant(z) &&
450e9 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 3b 0d 0a 7d 0d   z[0]==z[1];..}.
450ea 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
450eb 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
450ec 64 20 65 6e 64 73 20 77 69 74 68 20 74 68 72 65  d ends with thre
450ed 65 20 6c 65 74 74 65 72 73 20 77 68 69 63 68 0d  e letters which.
450ee 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e  .** are consonan
450ef 74 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e  t-vowel-consonen
450f0 74 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20  t and where the 
450f1 66 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0d  final consonant.
450f2 0a 2a 2a 20 69 73 20 6e 6f 74 20 27 77 27 2c 20  .** is not 'w', 
450f3 27 78 27 2c 20 6f 72 20 27 79 27 2e 0d 0a 2a 2a  'x', or 'y'...**
450f4 0d 0a 2a 2a 20 54 68 65 20 77 6f 72 64 20 69 73  ..** The word is
450f5 20 72 65 76 65 72 73 65 64 20 68 65 72 65 2e 20   reversed here. 
450f6 20 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c   So we are reall
450f7 79 20 63 68 65 63 6b 69 6e 67 20 74 68 65 0d 0a  y checking the..
450f8 2a 2a 20 66 69 72 73 74 20 74 68 72 65 65 20 6c  ** first three l
450f9 65 74 74 65 72 73 20 61 6e 64 20 74 68 65 20 66  etters and the f
450fa 69 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f 74 20  irst one cannot 
450fb 62 65 20 69 6e 20 5b 77 78 79 5d 2e 0d 0a 2a 2f  be in [wxy]...*/
450fc 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 61  ..static int sta
450fd 72 5f 6f 68 28 63 6f 6e 73 74 20 63 68 61 72 20  r_oh(const char 
450fe 2a 7a 29 7b 0d 0a 20 20 72 65 74 75 72 6e 0d 0a  *z){..  return..
450ff 20 20 20 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28      isConsonant(
45100 7a 29 20 26 26 0d 0a 20 20 20 20 7a 5b 30 5d 21  z) &&..    z[0]!
45101 3d 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27 78  ='w' && z[0]!='x
45102 27 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20 26  ' && z[0]!='y' &
45103 26 0d 0a 20 20 20 20 69 73 56 6f 77 65 6c 28 7a  &..    isVowel(z
45104 2b 31 29 20 26 26 0d 0a 20 20 20 20 69 73 43 6f  +1) &&..    isCo
45105 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0d 0a 7d  nsonant(z+2);..}
45106 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 66 20 74 68  ..../*..** If th
45107 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68  e word ends with
45108 20 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e 64   zFrom and xCond
45109 28 29 20 69 73 20 74 72 75 65 20 66 6f 72 20 74  () is true for t
4510a 68 65 20 73 74 65 6d 0d 0a 2a 2a 20 6f 66 20 74  he stem..** of t
4510b 68 65 20 77 6f 72 64 20 74 68 61 74 20 70 72 65  he word that pre
4510c 63 65 65 64 73 20 74 68 65 20 7a 46 72 6f 6d 20  ceeds the zFrom 
4510d 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 63 68 61  ending, then cha
4510e 6e 67 65 20 74 68 65 20 0d 0a 2a 2a 20 65 6e 64  nge the ..** end
4510f 69 6e 67 20 74 6f 20 7a 54 6f 2e 0d 0a 2a 2a 0d  ing to zTo...**.
45110 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 77 6f  .** The input wo
45111 72 64 20 2a 70 7a 20 61 6e 64 20 7a 46 72 6f 6d  rd *pz and zFrom
45112 20 61 72 65 20 62 6f 74 68 20 69 6e 20 72 65 76   are both in rev
45113 65 72 73 65 20 6f 72 64 65 72 2e 20 20 7a 54 6f  erse order.  zTo
45114 0d 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f 72 6d 61  ..** is in norma
45115 6c 20 6f 72 64 65 72 2e 20 0d 0a 2a 2a 0d 0a 2a  l order. ..**..*
45116 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
45117 20 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e 20   zFrom matches. 
45118 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66   Return FALSE if
45119 20 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74 0d   zFrom does not.
4511a 0a 2a 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20  .** match.  Not 
4511b 74 68 61 74 20 54 52 55 45 20 69 73 20 72 65 74  that TRUE is ret
4511c 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 78 43  urned even if xC
4511d 6f 6e 64 28 29 20 66 61 69 6c 73 20 61 6e 64 0d  ond() fails and.
4511e 0a 2a 2a 20 6e 6f 20 73 75 62 73 74 69 74 75 74  .** no substitut
4511f 69 6f 6e 20 6f 63 63 75 72 73 2e 0d 0a 2a 2f 0d  ion occurs...*/.
45120 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 65 6d  .static int stem
45121 28 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 2c 20  (..  char **pz, 
45122 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
45123 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20 73 74  he word being st
45124 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65 64 29  emmed (Reversed)
45125 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
45126 72 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f 2a  r *zFrom,     /*
45127 20 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20 6d   If the ending m
45128 61 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20 28  atches this... (
45129 52 65 76 65 72 73 65 64 29 20 2a 2f 0d 0a 20 20  Reversed) */..  
4512a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c  const char *zTo,
4512b 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68         /* ... ch
4512c 61 6e 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20  ange the ending 
4512d 74 6f 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76  to this (not rev
4512e 65 72 73 65 64 29 20 2a 2f 0d 0a 20 20 69 6e 74  ersed) */..  int
4512f 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20   (*xCond)(const 
45130 63 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64  char*)   /* Cond
45131 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20  ition that must 
45132 62 65 20 74 72 75 65 20 2a 2f 0d 0a 29 7b 0d 0a  be true */..){..
45133 20 20 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b    char *z = *pz;
45134 0d 0a 20 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f  ..  while( *zFro
45135 6d 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20  m && *zFrom==*z 
45136 29 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b  ){ z++; zFrom++;
45137 20 7d 0d 0a 20 20 69 66 28 20 2a 7a 46 72 6f 6d   }..  if( *zFrom
45138 21 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0d  !=0 ) return 0;.
45139 0a 20 20 69 66 28 20 78 43 6f 6e 64 20 26 26 20  .  if( xCond && 
4513a 21 78 43 6f 6e 64 28 7a 29 20 29 20 72 65 74 75  !xCond(z) ) retu
4513b 72 6e 20 31 3b 0d 0a 20 20 77 68 69 6c 65 28 20  rn 1;..  while( 
4513c 2a 7a 54 6f 20 29 7b 0d 0a 20 20 20 20 2a 28 2d  *zTo ){..    *(-
4513d 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29 3b 0d  -z) = *(zTo++);.
4513e 0a 20 20 7d 0d 0a 20 20 2a 70 7a 20 3d 20 7a 3b  .  }..  *pz = z;
4513f 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 7d  ..  return 1;..}
45140 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
45141 69 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b 20  is the fallback 
45142 73 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68 65  stemmer used whe
45143 6e 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65  n the porter ste
45144 6d 6d 65 72 20 69 73 0d 0a 2a 2a 20 69 6e 61 70  mmer is..** inap
45145 70 72 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20  propriate.  The 
45146 69 6e 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f  input word is co
45147 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75  pied into the ou
45148 74 70 75 74 20 77 69 74 68 0d 0a 2a 2a 20 55 53  tput with..** US
45149 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64  -ASCII case fold
4514a 69 6e 67 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ing.  If the inp
4514b 75 74 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c  ut word is too l
4514c 6f 6e 67 20 28 6d 6f 72 65 0d 0a 2a 2a 20 74 68  ong (more..** th
4514d 61 6e 20 32 30 20 62 79 74 65 73 20 69 66 20 69  an 20 bytes if i
4514e 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 69  t contains no di
4514f 67 69 74 73 20 6f 72 20 6d 6f 72 65 20 74 68 61  gits or more tha
45150 6e 20 36 20 62 79 74 65 73 20 69 66 0d 0a 2a 2a  n 6 bytes if..**
45151 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 69 67   it contains dig
45152 69 74 73 29 20 74 68 65 6e 20 77 6f 72 64 20 69  its) then word i
45153 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 32  s truncated to 2
45154 30 20 6f 72 20 36 20 62 79 74 65 73 0d 0a 2a 2a  0 or 6 bytes..**
45155 20 62 79 20 74 61 6b 69 6e 67 20 31 30 20 6f 72   by taking 10 or
45156 20 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   3 bytes from th
45157 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  e beginning and 
45158 65 6e 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  end...*/..static
45159 20 76 6f 69 64 20 63 6f 70 79 5f 73 74 65 6d 6d   void copy_stemm
4515a 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  er(const char *z
4515b 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61  In, int nIn, cha
4515c 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e  r *zOut, int *pn
4515d 4f 75 74 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20  Out){..  int i, 
4515e 6d 78 2c 20 6a 3b 0d 0a 20 20 69 6e 74 20 68 61  mx, j;..  int ha
4515f 73 44 69 67 69 74 20 3d 20 30 3b 0d 0a 20 20 66  sDigit = 0;..  f
45160 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20 69  or(i=0; i<nIn; i
45161 2b 2b 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 63  ++){..    char c
45162 20 3d 20 7a 49 6e 5b 69 5d 3b 0d 0a 20 20 20 20   = zIn[i];..    
45163 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c  if( c>='A' && c<
45164 3d 27 5a 27 20 29 7b 0d 0a 20 20 20 20 20 20 7a  ='Z' ){..      z
45165 4f 75 74 5b 69 5d 20 3d 20 63 20 2d 20 27 41 27  Out[i] = c - 'A'
45166 20 2b 20 27 61 27 3b 0d 0a 20 20 20 20 7d 65 6c   + 'a';..    }el
45167 73 65 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 63  se{..      if( c
45168 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20  >='0' && c<='9' 
45169 29 20 68 61 73 44 69 67 69 74 20 3d 20 31 3b 0d  ) hasDigit = 1;.
4516a 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d  .      zOut[i] =
4516b 20 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   c;..    }..  }.
4516c 0a 20 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74  .  mx = hasDigit
4516d 20 3f 20 33 20 3a 20 31 30 3b 0d 0a 20 20 69 66   ? 3 : 10;..  if
4516e 28 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b 0d 0a 20  ( nIn>mx*2 ){.. 
4516f 20 20 20 66 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e     for(j=mx, i=n
45170 49 6e 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b  In-mx; i<nIn; i+
45171 2b 2c 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  +, j++){..      
45172 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b 69  zOut[j] = zOut[i
45173 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  ];..    }..    i
45174 20 3d 20 6a 3b 0d 0a 20 20 7d 0d 0a 20 20 7a 4f   = j;..  }..  zO
45175 75 74 5b 69 5d 20 3d 20 30 3b 0d 0a 20 20 2a 70  ut[i] = 0;..  *p
45176 6e 4f 75 74 20 3d 20 69 3b 0d 0a 7d 0d 0a 0d 0a  nOut = i;..}....
45177 0d 0a 2f 2a 0d 0a 2a 2a 20 53 74 65 6d 20 74 68  ../*..** Stem th
45178 65 20 69 6e 70 75 74 20 77 6f 72 64 20 7a 49 6e  e input word zIn
45179 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f  [0..nIn-1].  Sto
4517a 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 69 6e  re the output in
4517b 20 7a 4f 75 74 2e 0d 0a 2a 2a 20 7a 4f 75 74 20   zOut...** zOut 
4517c 69 73 20 61 74 20 6c 65 61 73 74 20 62 69 67 20  is at least big 
4517d 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e  enough to hold n
4517e 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69 74 65  In bytes.  Write
4517f 20 74 68 65 20 61 63 74 75 61 6c 0d 0a 2a 2a 20   the actual..** 
45180 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70  size of the outp
45181 75 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73 69  ut word (exclusi
45182 76 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20 74  ve of the '\0' t
45183 65 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f 20  erminator) into 
45184 2a 70 6e 4f 75 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  *pnOut...**..** 
45185 41 6e 79 20 75 70 70 65 72 2d 63 61 73 65 20 63  Any upper-case c
45186 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
45187 20 55 53 2d 41 53 43 49 49 20 63 68 61 72 61 63   US-ASCII charac
45188 74 65 72 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0d  ter set ([A-Z]).
45189 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
4518a 64 20 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e  d to lower case.
4518b 20 20 55 70 70 65 72 2d 63 61 73 65 20 55 54 46    Upper-case UTF
4518c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0d   characters are.
4518d 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0d 0a  .** unchanged...
4518e 2a 2a 0d 0a 2a 2a 20 57 6f 72 64 73 20 74 68 61  **..** Words tha
4518f 74 20 61 72 65 20 6c 6f 6e 67 65 72 20 74 68 61  t are longer tha
45190 6e 20 61 62 6f 75 74 20 32 30 20 62 79 74 65 73  n about 20 bytes
45191 20 61 72 65 20 73 74 65 6d 6d 65 64 20 62 79 20   are stemmed by 
45192 72 65 74 61 69 6e 69 6e 67 0d 0a 2a 2a 20 61 20  retaining..** a 
45193 66 65 77 20 62 79 74 65 73 20 66 72 6f 6d 20 74  few bytes from t
45194 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  he beginning and
45195 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
45196 77 6f 72 64 2e 20 20 49 66 20 74 68 65 0d 0a 2a  word.  If the..*
45197 2a 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  * word contains 
45198 64 69 67 69 74 73 2c 20 33 20 62 79 74 65 73 20  digits, 3 bytes 
45199 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74  are taken from t
4519a 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  he beginning and
4519b 0d 0a 2a 2a 20 33 20 62 79 74 65 73 20 66 72 6f  ..** 3 bytes fro
4519c 6d 20 74 68 65 20 65 6e 64 2e 20 20 46 6f 72 20  m the end.  For 
4519d 6c 6f 6e 67 20 77 6f 72 64 73 20 77 69 74 68 6f  long words witho
4519e 75 74 20 64 69 67 69 74 73 2c 20 31 30 20 62 79  ut digits, 10 by
4519f 74 65 73 0d 0a 2a 2a 20 61 72 65 20 74 61 6b 65  tes..** are take
451a0 6e 20 66 72 6f 6d 20 65 61 63 68 20 65 6e 64 2e  n from each end.
451a1 20 20 55 53 2d 41 53 43 49 49 20 63 61 73 65 20    US-ASCII case 
451a2 66 6f 6c 64 69 6e 67 20 73 74 69 6c 6c 20 61 70  folding still ap
451a3 70 6c 69 65 73 2e 0d 0a 2a 2a 20 0d 0a 2a 2a 20  plies...** ..** 
451a4 49 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72  If the input wor
451a5 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 20 64  d contains not d
451a6 69 67 69 74 73 20 62 75 74 20 64 6f 65 73 20 63  igits but does c
451a7 68 61 72 61 63 74 65 72 73 20 6e 6f 74 20 0d 0a  haracters not ..
451a8 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 74  ** in [a-zA-Z] t
451a9 68 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e 67 20  hen no stemming 
451aa 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 6e 64  is attempted and
451ab 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   this routine ju
451ac 73 74 20 0d 0a 2a 2a 20 63 6f 70 69 65 73 20 74  st ..** copies t
451ad 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68  he input into th
451ae 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65  e input into the
451af 20 6f 75 74 70 75 74 20 77 69 74 68 20 55 53 2d   output with US-
451b0 41 53 43 49 49 0d 0a 2a 2a 20 63 61 73 65 20 66  ASCII..** case f
451b1 6f 6c 64 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  olding...**..** 
451b2 53 74 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69  Stemming never i
451b3 6e 63 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e  ncreases the len
451b4 67 74 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e  gth of the word.
451b5 20 20 53 6f 20 74 68 65 72 65 20 69 73 0d 0a 2a    So there is..*
451b6 2a 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f  * no chance of o
451b7 76 65 72 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a  verflowing the z
451b8 4f 75 74 20 62 75 66 66 65 72 2e 0d 0a 2a 2f 0d  Out buffer...*/.
451b9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f 72  .static void por
451ba 74 65 72 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73  ter_stemmer(cons
451bb 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74  t char *zIn, int
451bc 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74   nIn, char *zOut
451bd 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0d 0a  , int *pnOut){..
451be 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a 20 20 63    int i, j;..  c
451bf 68 61 72 20 7a 52 65 76 65 72 73 65 5b 32 38 5d  har zReverse[28]
451c0 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a  ;..  char *z, *z
451c1 32 3b 0d 0a 20 20 69 66 28 20 6e 49 6e 3c 33 20  2;..  if( nIn<3 
451c2 7c 7c 20 6e 49 6e 3e 3d 28 69 6e 74 29 73 69 7a  || nIn>=(int)siz
451c3 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 37 20  eof(zReverse)-7 
451c4 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20 77  ){..    /* The w
451c5 6f 72 64 20 69 73 20 74 6f 6f 20 62 69 67 20 6f  ord is too big o
451c6 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20  r too small for 
451c7 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d 6d  the porter stemm
451c8 65 72 2e 0d 0a 20 20 20 20 2a 2a 20 46 61 6c 6c  er...    ** Fall
451c9 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f 70 79  back to the copy
451ca 20 73 74 65 6d 6d 65 72 20 2a 2f 0d 0a 20 20 20   stemmer */..   
451cb 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49   copy_stemmer(zI
451cc 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e  n, nIn, zOut, pn
451cd 4f 75 74 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  Out);..    retur
451ce 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69  n;..  }..  for(i
451cf 3d 30 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52 65  =0, j=sizeof(zRe
451d0 76 65 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b  verse)-6; i<nIn;
451d1 20 69 2b 2b 2c 20 6a 2d 2d 29 7b 0d 0a 20 20 20   i++, j--){..   
451d2 20 63 68 61 72 20 63 20 3d 20 7a 49 6e 5b 69 5d   char c = zIn[i]
451d3 3b 0d 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41  ;..    if( c>='A
451d4 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0d 0a  ' && c<='Z' ){..
451d5 20 20 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a        zReverse[j
451d6 5d 20 3d 20 63 20 2b 20 27 61 27 20 2d 20 27 41  ] = c + 'a' - 'A
451d7 27 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ';..    }else if
451d8 28 20 63 3e 3d 27 61 27 20 26 26 20 63 3c 3d 27  ( c>='a' && c<='
451d9 7a 27 20 29 7b 0d 0a 20 20 20 20 20 20 7a 52 65  z' ){..      zRe
451da 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b 0d 0a 20  verse[j] = c;.. 
451db 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
451dc 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66 20 61   /* The use of a
451dd 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69   character not i
451de 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73  n [a-zA-Z] means
451df 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62 61 63   that we fallbac
451e0 6b 0d 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  k..      ** to t
451e1 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20  he copy stemmer 
451e2 2a 2f 0d 0a 20 20 20 20 20 20 63 6f 70 79 5f 73  */..      copy_s
451e3 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c  temmer(zIn, nIn,
451e4 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0d 0a   zOut, pnOut);..
451e5 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20        return;.. 
451e6 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d     }..  }..  mem
451e7 73 65 74 28 26 7a 52 65 76 65 72 73 65 5b 73 69  set(&zReverse[si
451e8 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 35  zeof(zReverse)-5
451e9 5d 2c 20 30 2c 20 35 29 3b 0d 0a 20 20 7a 20 3d  ], 0, 5);..  z =
451ea 20 26 7a 52 65 76 65 72 73 65 5b 6a 2b 31 5d 3b   &zReverse[j+1];
451eb 0d 0a 0d 0a 0d 0a 20 20 2f 2a 20 53 74 65 70 20  ......  /* Step 
451ec 31 61 20 2a 2f 0d 0a 20 20 69 66 28 20 7a 5b 30  1a */..  if( z[0
451ed 5d 3d 3d 27 73 27 20 29 7b 0d 0a 20 20 20 20 69  ]=='s' ){..    i
451ee 66 28 0d 0a 20 20 20 20 20 21 73 74 65 6d 28 26  f(..     !stem(&
451ef 7a 2c 20 22 73 65 73 73 22 2c 20 22 73 73 22 2c  z, "sess", "ss",
451f0 20 30 29 20 26 26 0d 0a 20 20 20 20 20 21 73 74   0) &&..     !st
451f1 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c 20 22 69  em(&z, "sei", "i
451f2 22 2c 20 30 29 20 20 26 26 0d 0a 20 20 20 20 20  ", 0)  &&..     
451f3 21 73 74 65 6d 28 26 7a 2c 20 22 73 73 22 2c 20  !stem(&z, "ss", 
451f4 22 73 73 22 2c 20 30 29 0d 0a 20 20 20 20 29 7b  "ss", 0)..    ){
451f5 0d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0d 0a 20 20  ..      z++;..  
451f6 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a    }..  }....  /*
451f7 20 53 74 65 70 20 31 62 20 2a 2f 20 20 0d 0a 20   Step 1b */  .. 
451f8 20 7a 32 20 3d 20 7a 3b 0d 0a 20 20 69 66 28 20   z2 = z;..  if( 
451f9 73 74 65 6d 28 26 7a 2c 20 22 64 65 65 22 2c 20  stem(&z, "dee", 
451fa 22 65 65 22 2c 20 6d 5f 67 74 5f 30 29 20 29 7b  "ee", m_gt_0) ){
451fb 0d 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68  ..    /* Do noth
451fc 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b 20 77  ing.  The work w
451fd 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20 74 65  as all in the te
451fe 73 74 20 2a 2f 0d 0a 20 20 7d 65 6c 73 65 20 69  st */..  }else i
451ff 66 28 20 0d 0a 20 20 20 20 20 28 73 74 65 6d 28  f( ..     (stem(
45200 26 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c 20 68  &z, "gni", "", h
45201 61 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74 65 6d  asVowel) || stem
45202 28 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c 20 68  (&z, "de", "", h
45203 61 73 56 6f 77 65 6c 29 29 0d 0a 20 20 20 20 20  asVowel))..     
45204 20 26 26 20 7a 21 3d 7a 32 0d 0a 20 20 29 7b 0d   && z!=z2..  ){.
45205 0a 20 20 20 20 20 69 66 28 20 73 74 65 6d 28 26  .     if( stem(&
45206 7a 2c 20 22 74 61 22 2c 20 22 61 74 65 22 2c 20  z, "ta", "ate", 
45207 30 29 20 7c 7c 0d 0a 20 20 20 20 20 20 20 20 20  0) ||..         
45208 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22 2c 20 22  stem(&z, "lb", "
45209 62 6c 65 22 2c 20 30 29 20 7c 7c 0d 0a 20 20 20  ble", 0) ||..   
4520a 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22        stem(&z, "
4520b 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30 29 20 29  zi", "ize", 0) )
4520c 7b 0d 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  {..       /* Do 
4520d 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
4520e 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
4520f 65 20 74 65 73 74 20 2a 2f 0d 0a 20 20 20 20 20  e test */..     
45210 7d 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c 65  }else if( double
45211 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20  Consonant(z) && 
45212 28 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d  (*z!='l' && *z!=
45213 27 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20  's' && *z!='z') 
45214 29 7b 0d 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0d  ){..       z++;.
45215 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  .     }else if( 
45216 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73 74 61  m_eq_1(z) && sta
45217 72 5f 6f 68 28 7a 29 20 29 7b 0d 0a 20 20 20 20  r_oh(z) ){..    
45218 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27 3b     *(--z) = 'e';
45219 0d 0a 20 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ..     }..  }...
4521a 0a 20 20 2f 2a 20 53 74 65 70 20 31 63 20 2a 2f  .  /* Step 1c */
4521b 0d 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 79  ..  if( z[0]=='y
4521c 27 20 26 26 20 68 61 73 56 6f 77 65 6c 28 7a 2b  ' && hasVowel(z+
4521d 31 29 20 29 7b 0d 0a 20 20 20 20 7a 5b 30 5d 20  1) ){..    z[0] 
4521e 3d 20 27 69 27 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  = 'i';..  }.... 
4521f 20 2f 2a 20 53 74 65 70 20 32 20 2a 2f 0d 0a 20   /* Step 2 */.. 
45220 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b   switch( z[1] ){
45221 0d 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0d 0a  ..   case 'a':..
45222 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
45223 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20  anoita", "ate", 
45224 6d 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20  m_gt_0) ||..    
45225 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69   stem(&z, "lanoi
45226 74 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74  t", "tion", m_gt
45227 5f 30 29 3b 0d 0a 20 20 20 20 20 62 72 65 61 6b  _0);..     break
45228 3b 0d 0a 20 20 20 63 61 73 65 20 27 63 27 3a 0d  ;..   case 'c':.
45229 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
4522a 69 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20 6d  icne", "ence", m
4522b 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20 20  _gt_0) ||..     
4522c 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c  stem(&z, "icna",
4522d 20 22 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ance", m_gt_0)
4522e 3b 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ;..     break;..
4522f 20 20 20 63 61 73 65 20 27 65 27 3a 0d 0a 20 20     case 'e':..  
45230 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72 65 7a     stem(&z, "rez
45231 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f  i", "ize", m_gt_
45232 30 29 3b 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b  0);..     break;
45233 0d 0a 20 20 20 63 61 73 65 20 27 67 27 3a 0d 0a  ..   case 'g':..
45234 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
45235 67 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f 67  gol", "log", m_g
45236 74 5f 30 29 3b 0d 0a 20 20 20 20 20 62 72 65 61  t_0);..     brea
45237 6b 3b 0d 0a 20 20 20 63 61 73 65 20 27 6c 27 3a  k;..   case 'l':
45238 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  ..     stem(&z, 
45239 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f  "ilb", "ble", m_
4523a 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20 20 73  gt_0) ||..     s
4523b 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c 20  tem(&z, "illa", 
4523c 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  "al", m_gt_0) ||
4523d 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  ..     stem(&z, 
4523e 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c 20  "iltne", "ent", 
4523f 6d 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20  m_gt_0) ||..    
45240 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c   stem(&z, "ile",
45241 20 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c   "e", m_gt_0) ||
45242 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  ..     stem(&z, 
45243 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20  "ilsuo", "ous", 
45244 6d 5f 67 74 5f 30 29 3b 0d 0a 20 20 20 20 20 62  m_gt_0);..     b
45245 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73 65 20 27  reak;..   case '
45246 6f 27 3a 0d 0a 20 20 20 20 20 73 74 65 6d 28 26  o':..     stem(&
45247 7a 2c 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69  z, "noitazi", "i
45248 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0d  ze", m_gt_0) ||.
45249 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
4524a 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d  noita", "ate", m
4524b 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20 20  _gt_0) ||..     
4524c 73 74 65 6d 28 26 7a 2c 20 22 72 6f 74 61 22 2c  stem(&z, "rota",
4524d 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ate", m_gt_0);
4524e 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20  ..     break;.. 
4524f 20 20 63 61 73 65 20 27 73 27 3a 0d 0a 20 20 20    case 's':..   
45250 20 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73 69 6c    stem(&z, "msil
45251 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
45252 29 20 7c 7c 0d 0a 20 20 20 20 20 73 74 65 6d 28  ) ||..     stem(
45253 26 7a 2c 20 22 73 73 65 6e 65 76 69 22 2c 20 22  &z, "ssenevi", "
45254 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  ive", m_gt_0) ||
45255 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  ..     stem(&z, 
45256 22 73 73 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22  "ssenluf", "ful"
45257 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20  , m_gt_0) ||..  
45258 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65     stem(&z, "sse
45259 6e 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f  nsuo", "ous", m_
4525a 67 74 5f 30 29 3b 0d 0a 20 20 20 20 20 62 72 65  gt_0);..     bre
4525b 61 6b 3b 0d 0a 20 20 20 63 61 73 65 20 27 74 27  ak;..   case 't'
4525c 3a 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  :..     stem(&z,
4525d 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "itila", "al", 
4525e 6d 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20 20  m_gt_0) ||..    
4525f 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76 69   stem(&z, "itivi
45260 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30  ", "ive", m_gt_0
45261 29 20 7c 7c 0d 0a 20 20 20 20 20 73 74 65 6d 28  ) ||..     stem(
45262 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62  &z, "itilib", "b
45263 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0d 0a 20  le", m_gt_0);.. 
45264 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d      break;..  }.
45265 0a 0d 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 2a  ...  /* Step 3 *
45266 2f 0d 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30  /..  switch( z[0
45267 5d 20 29 7b 0d 0a 20 20 20 63 61 73 65 20 27 65  ] ){..   case 'e
45268 27 3a 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  ':..     stem(&z
45269 2c 20 22 65 74 61 63 69 22 2c 20 22 69 63 22 2c  , "etaci", "ic",
4526a 20 6d 5f 67 74 5f 30 29 20 7c 7c 0d 0a 20 20 20   m_gt_0) ||..   
4526b 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 76 69 74    stem(&z, "evit
4526c 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 20  a", "", m_gt_0) 
4526d 20 20 7c 7c 0d 0a 20 20 20 20 20 73 74 65 6d 28    ||..     stem(
4526e 26 7a 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61 6c  &z, "ezila", "al
4526f 22 2c 20 6d 5f 67 74 5f 30 29 3b 0d 0a 20 20 20  ", m_gt_0);..   
45270 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73    break;..   cas
45271 65 20 27 69 27 3a 0d 0a 20 20 20 20 20 73 74 65  e 'i':..     ste
45272 6d 28 26 7a 2c 20 22 69 74 69 63 69 22 2c 20 22  m(&z, "itici", "
45273 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b 0d 0a 20  ic", m_gt_0);.. 
45274 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 63      break;..   c
45275 61 73 65 20 27 6c 27 3a 0d 0a 20 20 20 20 20 73  ase 'l':..     s
45276 74 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20  tem(&z, "laci", 
45277 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  "ic", m_gt_0) ||
45278 0d 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  ..     stem(&z, 
45279 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "luf", "", m_gt_
4527a 30 29 3b 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b  0);..     break;
4527b 0d 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0d 0a  ..   case 's':..
4527c 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
4527d 73 65 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  sen", "", m_gt_0
4527e 29 3b 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d  );..     break;.
4527f 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 74 65  .  }....  /* Ste
45280 70 20 34 20 2a 2f 0d 0a 20 20 73 77 69 74 63 68  p 4 */..  switch
45281 28 20 7a 5b 31 5d 20 29 7b 0d 0a 20 20 20 63 61  ( z[1] ){..   ca
45282 73 65 20 27 61 27 3a 0d 0a 20 20 20 20 20 69 66  se 'a':..     if
45283 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d  ( z[0]=='l' && m
45284 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0d 0a 20  _gt_1(z+2) ){.. 
45285 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0d 0a 20        z += 2;.. 
45286 20 20 20 20 7d 0d 0a 20 20 20 20 20 62 72 65 61      }..     brea
45287 6b 3b 0d 0a 20 20 20 63 61 73 65 20 27 63 27 3a  k;..   case 'c':
45288 0d 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  ..     if( z[0]=
45289 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e  ='e' && z[2]=='n
4528a 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20  ' && (z[3]=='a' 
4528b 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26  || z[3]=='e')  &
4528c 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29  & m_gt_1(z+4)  )
4528d 7b 0d 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34  {..       z += 4
4528e 3b 0d 0a 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ;..     }..     
4528f 62 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73 65 20  break;..   case 
45290 27 65 27 3a 0d 0a 20 20 20 20 20 69 66 28 20 7a  'e':..     if( z
45291 5b 30 5d 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74  [0]=='r' && m_gt
45292 5f 31 28 7a 2b 32 29 20 29 7b 0d 0a 20 20 20 20  _1(z+2) ){..    
45293 20 20 20 7a 20 2b 3d 20 32 3b 0d 0a 20 20 20 20     z += 2;..    
45294 20 7d 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d   }..     break;.
45295 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0d 0a 20  .   case 'i':.. 
45296 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 63      if( z[0]=='c
45297 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29  ' && m_gt_1(z+2)
45298 20 29 7b 0d 0a 20 20 20 20 20 20 20 7a 20 2b 3d   ){..       z +=
45299 20 32 3b 0d 0a 20 20 20 20 20 7d 0d 0a 20 20 20   2;..     }..   
4529a 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73    break;..   cas
4529b 65 20 27 6c 27 3a 0d 0a 20 20 20 20 20 69 66 28  e 'l':..     if(
4529c 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b   z[0]=='e' && z[
4529d 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33 5d  2]=='b' && (z[3]
4529e 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27  =='a' || z[3]=='
4529f 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  i') && m_gt_1(z+
452a0 34 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 7a 20  4) ){..       z 
452a1 2b 3d 20 34 3b 0d 0a 20 20 20 20 20 7d 0d 0a 20  += 4;..     }.. 
452a2 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 63      break;..   c
452a3 61 73 65 20 27 6e 27 3a 0d 0a 20 20 20 20 20 69  ase 'n':..     i
452a4 66 28 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0d  f( z[0]=='t' ){.
452a5 0a 20 20 20 20 20 20 20 69 66 28 20 7a 5b 32 5d  .       if( z[2]
452a6 3d 3d 27 61 27 20 29 7b 0d 0a 20 20 20 20 20 20  =='a' ){..      
452a7 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b     if( m_gt_1(z+
452a8 33 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  3) ){..         
452a9 20 20 7a 20 2b 3d 20 33 3b 0d 0a 20 20 20 20 20    z += 3;..     
452aa 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 7d 65      }..       }e
452ab 6c 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 65  lse if( z[2]=='e
452ac 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 73  ' ){..         s
452ad 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22 2c  tem(&z, "tneme",
452ae 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0d   "", m_gt_1) ||.
452af 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26  .         stem(&
452b0 7a 2c 20 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d  z, "tnem", "", m
452b1 5f 67 74 5f 31 29 20 7c 7c 0d 0a 20 20 20 20 20  _gt_1) ||..     
452b2 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e      stem(&z, "tn
452b3 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b  e", "", m_gt_1);
452b4 0d 0a 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ..       }..    
452b5 20 7d 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d   }..     break;.
452b6 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0d 0a 20  .   case 'o':.. 
452b7 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75      if( z[0]=='u
452b8 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 69 66 28  ' ){..       if(
452b9 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0d   m_gt_1(z+2) ){.
452ba 0a 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 32  .         z += 2
452bb 3b 0d 0a 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ;..       }..   
452bc 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d    }else if( z[3]
452bd 3d 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27  =='s' || z[3]=='
452be 74 27 20 29 7b 0d 0a 20 20 20 20 20 20 20 73 74  t' ){..       st
452bf 65 6d 28 26 7a 2c 20 22 6e 6f 69 22 2c 20 22 22  em(&z, "noi", ""
452c0 2c 20 6d 5f 67 74 5f 31 29 3b 0d 0a 20 20 20 20  , m_gt_1);..    
452c1 20 7d 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d   }..     break;.
452c2 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0d 0a 20  .   case 's':.. 
452c3 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6d      if( z[0]=='m
452c4 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26  ' && z[2]=='i' &
452c5 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b  & m_gt_1(z+3) ){
452c6 0d 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  ..       z += 3;
452c7 0d 0a 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 62  ..     }..     b
452c8 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73 65 20 27  reak;..   case '
452c9 74 27 3a 0d 0a 20 20 20 20 20 73 74 65 6d 28 26  t':..     stem(&
452ca 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20 6d 5f  z, "eta", "", m_
452cb 67 74 5f 31 29 20 7c 7c 0d 0a 20 20 20 20 20 73  gt_1) ||..     s
452cc 74 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20 22  tem(&z, "iti", "
452cd 22 2c 20 6d 5f 67 74 5f 31 29 3b 0d 0a 20 20 20  ", m_gt_1);..   
452ce 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 63 61 73    break;..   cas
452cf 65 20 27 75 27 3a 0d 0a 20 20 20 20 20 69 66 28  e 'u':..     if(
452d0 20 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a 5b   z[0]=='s' && z[
452d1 32 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74 5f  2]=='o' && m_gt_
452d2 31 28 7a 2b 33 29 20 29 7b 0d 0a 20 20 20 20 20  1(z+3) ){..     
452d3 20 20 7a 20 2b 3d 20 33 3b 0d 0a 20 20 20 20 20    z += 3;..     
452d4 7d 0d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  }..     break;..
452d5 20 20 20 63 61 73 65 20 27 76 27 3a 0d 0a 20 20     case 'v':..  
452d6 20 63 61 73 65 20 27 7a 27 3a 0d 0a 20 20 20 20   case 'z':..    
452d7 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26   if( z[0]=='e' &
452d8 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d  & z[2]=='i' && m
452d9 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0d 0a 20  _gt_1(z+3) ){.. 
452da 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0d 0a 20        z += 3;.. 
452db 20 20 20 20 7d 0d 0a 20 20 20 20 20 62 72 65 61      }..     brea
452dc 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  k;..  }....  /* 
452dd 53 74 65 70 20 35 61 20 2a 2f 0d 0a 20 20 69 66  Step 5a */..  if
452de 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0d 0a  ( z[0]=='e' ){..
452df 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a      if( m_gt_1(z
452e0 2b 31 29 20 29 7b 0d 0a 20 20 20 20 20 20 7a 2b  +1) ){..      z+
452e1 2b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  +;..    }else if
452e2 28 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26 26  ( m_eq_1(z+1) &&
452e3 20 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20 29   !star_oh(z+1) )
452e4 7b 0d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0d 0a 20  {..      z++;.. 
452e5 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f     }..  }....  /
452e6 2a 20 53 74 65 70 20 35 62 20 2a 2f 0d 0a 20 20  * Step 5b */..  
452e7 69 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20 26 26  if( m_gt_1(z) &&
452e8 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 7a 5b   z[0]=='l' && z[
452e9 31 5d 3d 3d 27 6c 27 20 29 7b 0d 0a 20 20 20 20  1]=='l' ){..    
452ea 7a 2b 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  z++;..  }....  /
452eb 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20 74 68 65  * z[] is now the
452ec 20 73 74 65 6d 6d 65 64 20 77 6f 72 64 20 69 6e   stemmed word in
452ed 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20   reverse order. 
452ee 20 46 6c 69 70 20 69 74 20 62 61 63 6b 0d 0a 20   Flip it back.. 
452ef 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74 6f 20   ** around into 
452f0 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 61 6e  forward order an
452f1 64 20 72 65 74 75 72 6e 2e 0d 0a 20 20 2a 2f 0d  d return...  */.
452f2 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20  .  *pnOut = i = 
452f3 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 29 3b 0d  (int)strlen(z);.
452f4 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0d  .  zOut[i] = 0;.
452f5 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0d  .  while( *z ){.
452f6 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d  .    zOut[--i] =
452f7 20 2a 28 7a 2b 2b 29 3b 0d 0a 20 20 7d 0d 0a 7d   *(z++);..  }..}
452f8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 72 61  ..../*..** Chara
452f9 63 74 65 72 73 20 74 68 61 74 20 63 61 6e 20 62  cters that can b
452fa 65 20 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65  e part of a toke
452fb 6e 2e 20 20 57 65 20 61 73 73 75 6d 65 20 61 6e  n.  We assume an
452fc 79 20 63 68 61 72 61 63 74 65 72 0d 0a 2a 2a 20  y character..** 
452fd 77 68 6f 73 65 20 76 61 6c 75 65 20 69 73 20 67  whose value is g
452fe 72 65 61 74 65 72 20 74 68 61 6e 20 30 78 38 30  reater than 0x80
452ff 20 28 61 6e 79 20 55 54 46 20 63 68 61 72 61 63   (any UTF charac
45300 74 65 72 29 20 63 61 6e 20 62 65 0d 0a 2a 2a 20  ter) can be..** 
45301 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e  part of a token.
45302 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
45303 2c 20 64 65 6c 69 6d 69 74 65 72 73 20 61 6c 6c  , delimiters all
45304 20 6d 75 73 74 20 68 61 76 65 0d 0a 2a 2a 20 76   must have..** v
45305 61 6c 75 65 73 20 6f 66 20 30 78 37 66 20 6f 72  alues of 0x7f or
45306 20 6c 6f 77 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61   lower...*/..sta
45307 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 70  tic const char p
45308 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20 3d 20  orterIdChar[] = 
45309 7b 0d 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78  {../* x0 x1 x2 x
4530a 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38  3 x4 x5 x6 x7 x8
4530b 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20   x9 xA xB xC xD 
4530c 78 45 20 78 46 20 2a 2f 0d 0a 20 20 20 20 31 2c  xE xF */..    1,
4530d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
4530e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
4530f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
45310 2a 20 33 78 20 2a 2f 0d 0a 20 20 20 20 30 2c 20  * 3x */..    0, 
45311 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
45312 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
45313 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
45314 20 34 78 20 2a 2f 0d 0a 20 20 20 20 31 2c 20 31   4x */..    1, 1
45315 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
45316 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
45317 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20  0, 0, 0, 1,  /* 
45318 35 78 20 2a 2f 0d 0a 20 20 20 20 30 2c 20 31 2c  5x */..    0, 1,
45319 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
4531a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
4531b 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36  , 1, 1, 1,  /* 6
4531c 78 20 2a 2f 0d 0a 20 20 20 20 31 2c 20 31 2c 20  x */..    1, 1, 
4531d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
4531e 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
4531f 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
45320 20 2a 2f 0d 0a 7d 3b 0d 0a 23 64 65 66 69 6e 65   */..};..#define
45321 20 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28 63   isDelim(C) (((c
45322 68 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26 26  h=C)&0x80)==0 &&
45323 20 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70 6f   (ch<0x30 || !po
45324 72 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30 78  rterIdChar[ch-0x
45325 33 30 5d 29 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  30]))..../*..** 
45326 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74  Extract the next
45327 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f   token from a to
45328 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
45329 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  r.  The cursor m
4532a 75 73 74 0d 0a 2a 2a 20 68 61 76 65 20 62 65 65  ust..** have bee
4532b 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72  n opened by a pr
4532c 69 6f 72 20 63 61 6c 6c 20 74 6f 20 70 6f 72 74  ior call to port
4532d 65 72 4f 70 65 6e 28 29 2e 0d 0a 2a 2f 0d 0a 73  erOpen()...*/..s
4532e 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
4532f 4e 65 78 74 28 0d 0a 20 20 73 71 6c 69 74 65 33  Next(..  sqlite3
45330 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
45331 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
45332 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
45333 62 79 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a 2f  by porterOpen */
45334 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
45335 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  *pzToken,       
45336 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
45337 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68 65 20  *pzToken is the 
45338 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0d 0a 20  token text */.. 
45339 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20   int *pnBytes,  
4533a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4533b 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
4533c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
4533d 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  token */..  int 
4533e 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
4533f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45340 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
45341 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
45342 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 69 45 6e   */..  int *piEn
45343 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  dOffset,        
45344 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
45345 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74  T: Ending offset
45346 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20   of token */..  
45347 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20  int *piPosition 
45348 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45349 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69      /* OUT: Posi
4534a 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20  tion integer of 
4534b 74 6f 6b 65 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  token */..){..  
4534c 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
4534d 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70 6f  _cursor *c = (po
4534e 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
4534f 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
45350 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ;..  const char 
45351 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74 3b 0d  *z = c->zInput;.
45352 0a 0d 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  ...  while( c->i
45353 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
45354 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 53 74   ){..    int iSt
45355 61 72 74 4f 66 66 73 65 74 2c 20 63 68 3b 0d 0a  artOffset, ch;..
45356 0d 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70 61  ..    /* Scan pa
45357 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61  st delimiter cha
45358 72 61 63 74 65 72 73 20 2a 2f 0d 0a 20 20 20 20  racters */..    
45359 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
4535a 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20 69  t<c->nInput && i
4535b 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66  sDelim(z[c->iOff
4535c 73 65 74 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20  set]) ){..      
4535d 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0d 0a 20  c->iOffset++;.. 
4535e 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43     }....    /* C
4535f 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74  ount non-delimit
45360 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a  er characters. *
45361 2f 0d 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66  /..    iStartOff
45362 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
45363 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d  ;..    while( c-
45364 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70  >iOffset<c->nInp
45365 75 74 20 26 26 20 21 69 73 44 65 6c 69 6d 28 7a  ut && !isDelim(z
45366 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b  [c->iOffset]) ){
45367 0d 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73  ..      c->iOffs
45368 65 74 2b 2b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  et++;..    }....
45369 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73      if( c->iOffs
4536a 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  et>iStartOffset 
4536b 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 20  ){..      int n 
4536c 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74  = c->iOffset-iSt
4536d 61 72 74 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20  artOffset;..    
4536e 20 20 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f    if( n>c->nAllo
4536f 63 61 74 65 64 20 29 7b 0d 0a 20 20 20 20 20 20  cated ){..      
45370 20 20 63 68 61 72 20 2a 70 4e 65 77 3b 0d 0a 20    char *pNew;.. 
45371 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f 63         c->nAlloc
45372 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0d 0a 20 20  ated = n+20;..  
45373 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
45374 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e  ite3_realloc(c->
45375 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f  zToken, c->nAllo
45376 63 61 74 65 64 29 3b 0d 0a 20 20 20 20 20 20 20  cated);..       
45377 20 69 66 28 20 21 70 4e 65 77 20 29 20 72 65 74   if( !pNew ) ret
45378 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
45379 3b 0d 0a 20 20 20 20 20 20 20 20 63 2d 3e 7a 54  ;..        c->zT
4537a 6f 6b 65 6e 20 3d 20 70 4e 65 77 3b 0d 0a 20 20  oken = pNew;..  
4537b 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 6f 72      }..      por
4537c 74 65 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69  ter_stemmer(&z[i
4537d 53 74 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c  StartOffset], n,
4537e 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79   c->zToken, pnBy
4537f 74 65 73 29 3b 0d 0a 20 20 20 20 20 20 2a 70 7a  tes);..      *pz
45380 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65  Token = c->zToke
45381 6e 3b 0d 0a 20 20 20 20 20 20 2a 70 69 53 74 61  n;..      *piSta
45382 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72  rtOffset = iStar
45383 74 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 20 20  tOffset;..      
45384 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63  *piEndOffset = c
45385 2d 3e 69 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20  ->iOffset;..    
45386 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
45387 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0d 0a 20 20  c->iToken++;..  
45388 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
45389 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  E_OK;..    }..  
4538a 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
4538b 54 45 5f 44 4f 4e 45 3b 0d 0a 7d 0d 0a 0d 0a 2f  TE_DONE;..}..../
4538c 2a 0d 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  *..** The set of
4538d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69   routines that i
4538e 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 6f 72  mplement the por
4538f 74 65 72 2d 73 74 65 6d 6d 65 72 20 74 6f 6b 65  ter-stemmer toke
45390 6e 69 7a 65 72 0d 0a 2a 2f 0d 0a 73 74 61 74 69  nizer..*/..stati
45391 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
45392 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
45393 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72   porterTokenizer
45394 4d 6f 64 75 6c 65 20 3d 20 7b 0d 0a 20 20 30 2c  Module = {..  0,
45395 0d 0a 20 20 70 6f 72 74 65 72 43 72 65 61 74 65  ..  porterCreate
45396 2c 0d 0a 20 20 70 6f 72 74 65 72 44 65 73 74 72  ,..  porterDestr
45397 6f 79 2c 0d 0a 20 20 70 6f 72 74 65 72 4f 70 65  oy,..  porterOpe
45398 6e 2c 0d 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73  n,..  porterClos
45399 65 2c 0d 0a 20 20 70 6f 72 74 65 72 4e 65 78 74  e,..  porterNext
4539a 2c 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ,..};..../*..** 
4539b 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
4539c 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2e  orter tokenizer.
4539d 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
4539e 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0d 0a 2a  er to the new..*
4539f 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a  * tokenizer in *
453a0 70 70 4d 6f 64 75 6c 65 0d 0a 2a 2f 0d 0a 53 51  ppModule..*/..SQ
453a1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
453a2 64 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72  d sqlite3Fts3Por
453a3 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
453a4 6c 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74  le(..  sqlite3_t
453a5 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
453a6 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0d  const**ppModule.
453a7 0a 29 7b 0d 0a 20 20 2a 70 70 4d 6f 64 75 6c 65  .){..  *ppModule
453a8 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b 65 6e 69   = &porterTokeni
453a9 7a 65 72 4d 6f 64 75 6c 65 3b 0d 0a 7d 0d 0a 0d  zerModule;..}...
453aa 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
453ab 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
453ac 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
453ad 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
453ae 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
453af 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
453b0 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
453b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453b3 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
453b4 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
453b5 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  e fts3_tokenizer
453b6 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
453b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453b8 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
453b9 30 30 37 20 4a 75 6e 65 20 32 32 0d 0a 2a 2a 0d  007 June 22..**.
453ba 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
453bb 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
453bc 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
453bd 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
453be 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c  e of..** a legal
453bf 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
453c0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a   a blessing:..**
453c1 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
453c2 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
453c3 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  evil...**    May
453c4 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
453c5 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
453c6 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
453c7 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61  thers...**    Ma
453c8 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
453c9 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
453ca 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
453cb 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a  ive...**..******
453cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
453d0 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a  ********..**..**
453d1 20 54 68 69 73 20 69 73 20 70 61 72 74 20 6f 66   This is part of
453d2 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c   an SQLite modul
453d3 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66  e implementing f
453d4 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e  ull-text search.
453d5 0d 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69 63  ..** This partic
453d6 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65 6d  ular file implem
453d7 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69 63  ents the generic
453d8 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72   tokenizer inter
453d9 66 61 63 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d  face...*/..../*.
453da 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
453db 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
453dc 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0d 0a  y compiled if:..
453dd 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  **..**     * The
453de 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
453df 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
453e0 6e 20 65 78 74 65 6e 73 69 6f 6e 0d 0a 2a 2a 20  n extension..** 
453e1 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
453e2 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
453e3 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
453e4 2c 20 6f 72 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20  , or..**..**    
453e5 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
453e6 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
453e7 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20  t into the core 
453e8 6f 66 0d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  of..**       SQL
453e9 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
453ea 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
453eb 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
453ec 29 2e 0d 0a 2a 2f 0d 0a 23 69 66 20 21 64 65 66  )...*/..#if !def
453ed 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
453ee 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
453ef 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
453f0 0d 0a 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  ..../* #include 
453f1 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a 2f  <assert.h> */../
453f2 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69  * #include <stri
453f3 6e 67 2e 68 3e 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a  ng.h> */..../*..
453f4 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
453f5 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
453f6 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
453f7 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
453f8 6e 64 65 72 6c 79 69 6e 67 20 0d 0a 2a 2a 20 68  nderlying ..** h
453f9 61 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20  ash table. This 
453fa 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
453fb 63 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77  called as follow
453fc 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c  s:..**..**   SEL
453fd 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61  ECT <function-na
453fe 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b  me>(<key-name>);
453ff 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  ..**   SELECT <f
45400 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
45401 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74  ey-name>, <point
45402 65 72 3e 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 77 68  er>);..**..** wh
45403 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61  ere <function-na
45404 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20  me> is the name 
45405 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
45406 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0d 0a 2a  cond argument..*
45407 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
45408 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
45409 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
4540a 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
4540b 65 72 27 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  er')...**..** If
4540c 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
4540d 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
4540e 66 69 65 64 2c 20 69 74 20 6d 75 73 74 20 62 65  fied, it must be
4540f 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 0d 0a 2a   a blob value..*
45410 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70  * containing a p
45411 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 73 74 6f  ointer to be sto
45412 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
45413 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
45414 6e 67 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74  ng..** to the st
45415 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 2e  ring <key-name>.
45416 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e 20 69 73   If <pointer> is
45417 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 2c 20   not specified, 
45418 74 68 65 6e 0d 0a 2a 2a 20 74 68 65 20 73 74 72  then..** the str
45419 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 6d  ing <key-name> m
4541a 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ust already exis
4541b 74 20 69 6e 20 74 68 65 20 68 61 73 20 74 61 62  t in the has tab
4541c 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0d 0a  le. Otherwise,..
4541d 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  ** an error is r
4541e 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a  eturned...**..**
4541f 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
45420 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72  the <pointer> ar
45421 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69 66  gument is specif
45422 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20 72  ied, the value r
45423 65 74 75 72 6e 65 64 0d 0a 2a 2a 20 69 73 20 61  eturned..** is a
45424 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
45425 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
45426 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
45427 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
45428 6e 67 0d 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67  ng..** to string
45429 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74   <key-name> (aft
4542a 65 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  er the hash-tabl
4542b 65 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66  e is updated, if
4542c 20 61 70 70 6c 69 63 61 62 6c 65 29 2e 0d 0a 2a   applicable)...*
4542d 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  /..static void s
4542e 63 61 6c 61 72 46 75 6e 63 28 0d 0a 20 20 73 71  calarFunc(..  sq
4542f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
45430 6f 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61  ontext,..  int a
45431 72 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f  rgc,..  sqlite3_
45432 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b  value **argv..){
45433 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48  ..  Fts3Hash *pH
45434 61 73 68 3b 0d 0a 20 20 76 6f 69 64 20 2a 70 50  ash;..  void *pP
45435 74 72 20 3d 20 30 3b 0d 0a 20 20 63 6f 6e 73 74  tr = 0;..  const
45436 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
45437 7a 4e 61 6d 65 3b 0d 0a 20 20 69 6e 74 20 6e 4e  zName;..  int nN
45438 61 6d 65 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74  ame;....  assert
45439 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67  ( argc==1 || arg
4543a 63 3d 3d 32 20 29 3b 0d 0a 0d 0a 20 20 70 48 61  c==2 );....  pHa
4543b 73 68 20 3d 20 28 46 74 73 33 48 61 73 68 20 2a  sh = (Fts3Hash *
4543c 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  )sqlite3_user_da
4543d 74 61 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 0d 0a  ta(context);....
4543e 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
4543f 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
45440 76 5b 30 5d 29 3b 0d 0a 20 20 6e 4e 61 6d 65 20  v[0]);..  nName 
45441 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
45442 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31  bytes(argv[0])+1
45443 3b 0d 0a 0d 0a 20 20 69 66 28 20 61 72 67 63 3d  ;....  if( argc=
45444 3d 32 20 29 7b 0d 0a 20 20 20 20 76 6f 69 64 20  =2 ){..    void 
45445 2a 70 4f 6c 64 3b 0d 0a 20 20 20 20 69 6e 74 20  *pOld;..    int 
45446 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
45447 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29  e_bytes(argv[1])
45448 3b 0d 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69  ;..    if( n!=si
45449 7a 65 6f 66 28 70 50 74 72 29 20 29 7b 0d 0a 20  zeof(pPtr) ){.. 
4544a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
4544b 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
4544c 74 2c 20 22 61 72 67 75 6d 65 6e 74 20 74 79 70  t, "argument typ
4544d 65 20 6d 69 73 6d 61 74 63 68 22 2c 20 2d 31 29  e mismatch", -1)
4544e 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ;..      return;
4544f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 50 74  ..    }..    pPt
45450 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a 29 73 71  r = *(void **)sq
45451 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
45452 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20 20 20 20  (argv[1]);..    
45453 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 46 74  pOld = sqlite3Ft
45454 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  s3HashInsert(pHa
45455 73 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e 61 6d  sh, (void *)zNam
45456 65 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72 29 3b  e, nName, pPtr);
45457 0d 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 3d 3d  ..    if( pOld==
45458 70 50 74 72 20 29 7b 0d 0a 20 20 20 20 20 20 73  pPtr ){..      s
45459 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
4545a 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75  ror(context, "ou
4545b 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31  t of memory", -1
4545c 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e  );..      return
4545d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
4545e 65 7b 0d 0a 20 20 20 20 70 50 74 72 20 3d 20 73  e{..    pPtr = s
4545f 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
45460 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  nd(pHash, zName,
45461 20 6e 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 69 66   nName);..    if
45462 28 20 21 70 50 74 72 20 29 7b 0d 0a 20 20 20 20  ( !pPtr ){..    
45463 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73    char *zErr = s
45464 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
45465 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
45466 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d  r: %s", zName);.
45467 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
45468 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
45469 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d  ext, zErr, -1);.
4546a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
4546b 72 65 65 28 7a 45 72 72 29 3b 0d 0a 20 20 20 20  ree(zErr);..    
4546c 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 7d    return;..    }
4546d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74  ..  }....  sqlit
4546e 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63  e3_result_blob(c
4546f 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64 20 2a 29  ontext, (void *)
45470 26 70 50 74 72 2c 20 73 69 7a 65 6f 66 28 70 50  &pPtr, sizeof(pP
45471 74 72 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  tr), SQLITE_TRAN
45472 53 49 45 4e 54 29 3b 0d 0a 7d 0d 0a 0d 0a 53 51  SIENT);..}....SQ
45473 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
45474 20 73 71 6c 69 74 65 33 46 74 73 33 49 73 49 64   sqlite3Fts3IsId
45475 43 68 61 72 28 63 68 61 72 20 63 29 7b 0d 0a 20  Char(char c){.. 
45476 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
45477 61 72 20 69 73 46 74 73 49 64 43 68 61 72 5b 5d  ar isFtsIdChar[]
45478 20 3d 20 7b 0d 0a 20 20 20 20 20 20 30 2c 20 30   = {..      0, 0
45479 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
4547a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
4547b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
4547c 30 78 20 2a 2f 0d 0a 20 20 20 20 20 20 30 2c 20  0x */..      0, 
4547d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
4547e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
4547f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
45480 20 31 78 20 2a 2f 0d 0a 20 20 20 20 20 20 30 2c   1x */..      0,
45481 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 0, 0, 1, 0, 
45482 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
45483 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
45484 2a 20 32 78 20 2a 2f 0d 0a 20 20 20 20 20 20 31  * 2x */..      1
45485 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
45486 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
45487 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20  0, 0, 0, 0, 0,  
45488 2f 2a 20 33 78 20 2a 2f 0d 0a 20 20 20 20 20 20  /* 3x */..      
45489 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
4548a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
4548b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
4548c 20 2f 2a 20 34 78 20 2a 2f 0d 0a 20 20 20 20 20   /* 4x */..     
4548d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
4548e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
4548f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  , 0, 0, 0, 0, 1,
45490 20 20 2f 2a 20 35 78 20 2a 2f 0d 0a 20 20 20 20    /* 5x */..    
45491 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
45492 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
45493 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
45494 2c 20 20 2f 2a 20 36 78 20 2a 2f 0d 0a 20 20 20  ,  /* 6x */..   
45495 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
45496 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
45497 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
45498 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0d 0a 20 20  0,  /* 7x */..  
45499 7d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 63 26  };..  return (c&
4549a 30 78 38 30 20 7c 7c 20 69 73 46 74 73 49 64 43  0x80 || isFtsIdC
4549b 68 61 72 5b 28 69 6e 74 29 28 63 29 5d 29 3b 0d  har[(int)(c)]);.
4549c 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  .}....SQLITE_PRI
4549d 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
4549e 2a 73 71 6c 69 74 65 33 46 74 73 33 4e 65 78 74  *sqlite3Fts3Next
4549f 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  Token(const char
454a0 20 2a 7a 53 74 72 2c 20 69 6e 74 20 2a 70 6e 29   *zStr, int *pn)
454a1 7b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  {..  const char 
454a2 2a 7a 31 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  *z1;..  const ch
454a3 61 72 20 2a 7a 32 20 3d 20 30 3b 0d 0a 0d 0a 20  ar *z2 = 0;.... 
454a4 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 74 61   /* Find the sta
454a5 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74  rt of the next t
454a6 6f 6b 65 6e 2e 20 2a 2f 0d 0a 20 20 7a 31 20 3d  oken. */..  z1 =
454a7 20 7a 53 74 72 3b 0d 0a 20 20 77 68 69 6c 65 28   zStr;..  while(
454a8 20 7a 32 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63   z2==0 ){..    c
454a9 68 61 72 20 63 20 3d 20 2a 7a 31 3b 0d 0a 20 20  har c = *z1;..  
454aa 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0d 0a    switch( c ){..
454ab 20 20 20 20 20 20 63 61 73 65 20 27 5c 30 27 3a        case '\0':
454ac 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
454ad 20 20 2f 2a 20 4e 6f 20 6d 6f 72 65 20 74 6f 6b    /* No more tok
454ae 65 6e 73 20 68 65 72 65 20 2a 2f 0d 0a 20 20 20  ens here */..   
454af 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0d 0a 20     case '\'':.. 
454b0 20 20 20 20 20 63 61 73 65 20 27 22 27 3a 0d 0a       case '"':..
454b1 20 20 20 20 20 20 63 61 73 65 20 27 60 27 3a 20        case '`': 
454b2 7b 0d 0a 20 20 20 20 20 20 20 20 7a 32 20 3d 20  {..        z2 = 
454b3 7a 31 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 69  z1;..        whi
454b4 6c 65 28 20 2a 2b 2b 7a 32 20 26 26 20 28 2a 7a  le( *++z2 && (*z
454b5 32 21 3d 63 20 7c 7c 20 2a 2b 2b 7a 32 3d 3d 63  2!=c || *++z2==c
454b6 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 72  ) );..        br
454b7 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
454b8 20 20 20 20 20 63 61 73 65 20 27 5b 27 3a 0d 0a       case '[':..
454b9 20 20 20 20 20 20 20 20 7a 32 20 3d 20 26 7a 31          z2 = &z1
454ba 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 77 68  [1];..        wh
454bb 69 6c 65 28 20 2a 7a 32 20 26 26 20 7a 32 5b 30  ile( *z2 && z2[0
454bc 5d 21 3d 27 5d 27 20 29 20 7a 32 2b 2b 3b 0d 0a  ]!=']' ) z2++;..
454bd 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 32 20          if( *z2 
454be 29 20 7a 32 2b 2b 3b 0d 0a 20 20 20 20 20 20 20  ) z2++;..       
454bf 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20   break;....     
454c0 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20 20   default:..     
454c1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 74     if( sqlite3Ft
454c2 73 33 49 73 49 64 43 68 61 72 28 2a 7a 31 29 20  s3IsIdChar(*z1) 
454c3 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 7a 32  ){..          z2
454c4 20 3d 20 26 7a 31 5b 31 5d 3b 0d 0a 20 20 20 20   = &z1[1];..    
454c5 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
454c6 69 74 65 33 46 74 73 33 49 73 49 64 43 68 61 72  ite3Fts3IsIdChar
454c7 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0d 0a 20  (*z2) ) z2++;.. 
454c8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20         }else{.. 
454c9 20 20 20 20 20 20 20 20 20 7a 31 2b 2b 3b 0d 0a           z1++;..
454ca 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d          }..    }
454cb 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 6e 20 3d  ..  }....  *pn =
454cc 20 28 69 6e 74 29 28 7a 32 2d 7a 31 29 3b 0d 0a   (int)(z2-z1);..
454cd 20 20 72 65 74 75 72 6e 20 7a 31 3b 0d 0a 7d 0d    return z1;..}.
454ce 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
454cf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
454d0 33 49 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28 0d  3InitTokenizer(.
454d1 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61  .  Fts3Hash *pHa
454d2 73 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  sh,             
454d3 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
454d4 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  hash table */.. 
454d5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
454d6 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
454d7 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6e 61   /* Tokenizer na
454d8 6d 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  me */..  sqlite3
454d9 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
454da 6f 6b 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  ok,      /* OUT:
454db 20 54 6f 6b 65 6e 69 7a 65 72 20 28 69 66 20 61   Tokenizer (if a
454dc 70 70 6c 69 63 61 62 6c 65 29 20 2a 2f 0d 0a 20  pplicable) */.. 
454dd 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20   char **pzErr   
454de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
454df 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20   /* OUT: Set to 
454e0 6d 61 6c 6c 6f 63 65 64 20 65 72 72 6f 72 20 6d  malloced error m
454e1 65 73 73 61 67 65 20 2a 2f 0d 0a 29 7b 0d 0a 20  essage */..){.. 
454e2 20 69 6e 74 20 72 63 3b 0d 0a 20 20 63 68 61 72   int rc;..  char
454e3 20 2a 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 41   *z = (char *)zA
454e4 72 67 3b 0d 0a 20 20 69 6e 74 20 6e 20 3d 20 30  rg;..  int n = 0
454e5 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 43 6f 70 79  ;..  char *zCopy
454e6 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 45 6e 64 3b  ;..  char *zEnd;
454e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
454e8 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
454e9 74 6f 20 6e 75 6c 2d 74 65 72 6d 20 6f 66 20 7a  to nul-term of z
454ea 43 6f 70 79 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  Copy */..  sqlit
454eb 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
454ec 75 6c 65 20 2a 6d 3b 0d 0a 0d 0a 20 20 7a 43 6f  ule *m;....  zCo
454ed 70 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  py = sqlite3_mpr
454ee 69 6e 74 66 28 22 25 73 22 2c 20 7a 41 72 67 29  intf("%s", zArg)
454ef 3b 0d 0a 20 20 69 66 28 20 21 7a 43 6f 70 79 20  ;..  if( !zCopy 
454f0 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
454f1 4e 4f 4d 45 4d 3b 0d 0a 20 20 7a 45 6e 64 20 3d  NOMEM;..  zEnd =
454f2 20 26 7a 43 6f 70 79 5b 73 74 72 6c 65 6e 28 7a   &zCopy[strlen(z
454f3 43 6f 70 79 29 5d 3b 0d 0a 0d 0a 20 20 7a 20 3d  Copy)];....  z =
454f4 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
454f5 46 74 73 33 4e 65 78 74 54 6f 6b 65 6e 28 7a 43  Fts3NextToken(zC
454f6 6f 70 79 2c 20 26 6e 29 3b 0d 0a 20 20 7a 5b 6e  opy, &n);..  z[n
454f7 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20 20 73 71 6c  ] = '\0';..  sql
454f8 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65 28  ite3Fts3Dequote(
454f9 7a 29 3b 0d 0a 0d 0a 20 20 6d 20 3d 20 28 73 71  z);....  m = (sq
454fa 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
454fb 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  module *)sqlite3
454fc 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61  Fts3HashFind(pHa
454fd 73 68 2c 7a 2c 28 69 6e 74 29 73 74 72 6c 65 6e  sh,z,(int)strlen
454fe 28 7a 29 2b 31 29 3b 0d 0a 20 20 69 66 28 20 21  (z)+1);..  if( !
454ff 6d 20 29 7b 0d 0a 20 20 20 20 2a 70 7a 45 72 72  m ){..    *pzErr
45500 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
45501 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65  tf("unknown toke
45502 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 29 3b 0d  nizer: %s", z);.
45503 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
45504 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 65 6c 73 65  _ERROR;..  }else
45505 7b 0d 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73  {..    char cons
45506 74 20 2a 2a 61 41 72 67 20 3d 20 30 3b 0d 0a 20  t **aArg = 0;.. 
45507 20 20 20 69 6e 74 20 69 41 72 67 20 3d 20 30 3b     int iArg = 0;
45508 0d 0a 20 20 20 20 7a 20 3d 20 26 7a 5b 6e 2b 31  ..    z = &z[n+1
45509 5d 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 7a  ];..    while( z
4550a 3c 7a 45 6e 64 20 26 26 20 28 4e 55 4c 4c 21 3d  <zEnd && (NULL!=
4550b 28 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  (z = (char *)sql
4550c 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65  ite3Fts3NextToke
4550d 6e 28 7a 2c 20 26 6e 29 29 29 20 29 7b 0d 0a 20  n(z, &n))) ){.. 
4550e 20 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20       int nNew = 
4550f 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28  sizeof(char *)*(
45510 69 41 72 67 2b 31 29 3b 0d 0a 20 20 20 20 20 20  iArg+1);..      
45511 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 61 4e 65  char const **aNe
45512 77 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  w = (const char 
45513 2a 2a 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  **)sqlite3_reall
45514 6f 63 28 28 76 6f 69 64 20 2a 29 61 41 72 67 2c  oc((void *)aArg,
45515 20 6e 4e 65 77 29 3b 0d 0a 20 20 20 20 20 20 69   nNew);..      i
45516 66 28 20 21 61 4e 65 77 20 29 7b 0d 0a 20 20 20  f( !aNew ){..   
45517 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
45518 65 28 7a 43 6f 70 79 29 3b 0d 0a 20 20 20 20 20  e(zCopy);..     
45519 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
4551a 28 76 6f 69 64 20 2a 29 61 41 72 67 29 3b 0d 0a  (void *)aArg);..
4551b 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
4551c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
4551d 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 41 72      }..      aAr
4551e 67 20 3d 20 61 4e 65 77 3b 0d 0a 20 20 20 20 20  g = aNew;..     
4551f 20 61 41 72 67 5b 69 41 72 67 2b 2b 5d 20 3d 20   aArg[iArg++] = 
45520 7a 3b 0d 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d  z;..      z[n] =
45521 20 27 5c 30 27 3b 0d 0a 20 20 20 20 20 20 73 71   '\0';..      sq
45522 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65  lite3Fts3Dequote
45523 28 7a 29 3b 0d 0a 20 20 20 20 20 20 7a 20 3d 20  (z);..      z = 
45524 26 7a 5b 6e 2b 31 5d 3b 0d 0a 20 20 20 20 7d 0d  &z[n+1];..    }.
45525 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78 43 72  .    rc = m->xCr
45526 65 61 74 65 28 69 41 72 67 2c 20 61 41 72 67 2c  eate(iArg, aArg,
45527 20 70 70 54 6f 6b 29 3b 0d 0a 20 20 20 20 61 73   ppTok);..    as
45528 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
45529 5f 4f 4b 20 7c 7c 20 2a 70 70 54 6f 6b 20 29 3b  _OK || *ppTok );
4552a 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
4552b 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
4552c 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
4552d 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
4552e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 22 29 3b  own tokenizer");
4552f 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
45530 20 20 20 20 28 2a 70 70 54 6f 6b 29 2d 3e 70 4d      (*ppTok)->pM
45531 6f 64 75 6c 65 20 3d 20 6d 3b 20 0d 0a 20 20 20  odule = m; ..   
45532 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
45533 66 72 65 65 28 28 76 6f 69 64 20 2a 29 61 41 72  free((void *)aAr
45534 67 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71  g);..  }....  sq
45535 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 70 79  lite3_free(zCopy
45536 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
45537 0d 0a 7d 0d 0a 0d 0a 0d 0a 23 69 66 64 65 66 20  ..}......#ifdef 
45538 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 0d 0a 2f  SQLITE_TEST..../
45539 2a 20 23 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e  * #include <tcl.
4553a 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75  h> */../* #inclu
4553b 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f  de <string.h> */
4553c 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65  ..../*..** Imple
4553d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73  mentation of a s
4553e 70 65 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61  pecial SQL scala
4553f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74  r function for t
45540 65 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  esting tokenizer
45541 73 20 0d 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20  s ..** designed 
45542 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f  to be used in co
45543 6e 63 65 72 74 20 77 69 74 68 20 74 68 65 20 54  ncert with the T
45544 63 6c 20 74 65 73 74 69 6e 67 20 66 72 61 6d 65  cl testing frame
45545 77 6f 72 6b 2e 20 54 68 69 73 0d 0a 2a 2a 20 66  work. This..** f
45546 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
45547 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20  called with two 
45548 61 72 67 75 6d 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a  arguments:..**..
45549 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
4554a 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
4554b 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74 2d 73  -name>, <input-s
4554c 74 72 69 6e 67 3e 29 3b 0d 0a 2a 2a 20 20 20 53  tring>);..**   S
4554d 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
4554e 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
4554f 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0d 0a 2a  , <pointer>);..*
45550 2a 0d 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e  *..** where <fun
45551 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ction-name> is t
45552 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61  he name passed a
45553 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
45554 75 6d 65 6e 74 0d 0a 2a 2a 20 74 6f 20 74 68 65  ument..** to the
45555 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
45556 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63  HashTable() func
45557 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33  tion (e.g. 'fts3
45558 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 0d 0a 2a 2a  _tokenizer')..**
45559 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77 69   concatenated wi
4555a 74 68 20 74 68 65 20 73 74 72 69 6e 67 20 27 5f  th the string '_
4555b 74 65 73 74 27 20 28 65 2e 67 2e 20 27 66 74 73  test' (e.g. 'fts
4555c 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74  3_tokenizer_test
4555d 27 29 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  ')...**..** The 
4555e 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
4555f 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6d 61  a string that ma
45560 79 20 62 65 20 69 6e 74 65 72 70 72 65 74 65 64  y be interpreted
45561 20 61 73 20 61 20 54 63 6c 0d 0a 2a 2a 20 6c 69   as a Tcl..** li
45562 73 74 2e 20 46 6f 72 20 65 61 63 68 20 74 6f 6b  st. For each tok
45563 65 6e 20 69 6e 20 74 68 65 20 3c 69 6e 70 75 74  en in the <input
45564 2d 73 74 72 69 6e 67 3e 2c 20 74 68 72 65 65 20  -string>, three 
45565 65 6c 65 6d 65 6e 74 73 20 61 72 65 0d 0a 2a 2a  elements are..**
45566 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 72 65   added to the re
45567 74 75 72 6e 65 64 20 6c 69 73 74 2e 20 54 68 65  turned list. The
45568 20 66 69 72 73 74 20 69 73 20 74 68 65 20 74 6f   first is the to
45569 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68  ken position, th
4556a 65 20 0d 0a 2a 2a 20 73 65 63 6f 6e 64 20 69 73  e ..** second is
4556b 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
4556c 28 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d 65 64  (folded, stemmed
4556d 2c 20 65 74 63 2e 29 20 61 6e 64 20 74 68 65 20  , etc.) and the 
4556e 74 68 69 72 64 20 69 73 20 74 68 65 0d 0a 2a 2a  third is the..**
4556f 20 73 75 62 73 74 72 69 6e 67 20 6f 66 20 3c 69   substring of <i
45570 6e 70 75 74 2d 73 74 72 69 6e 67 3e 20 61 73 73  nput-string> ass
45571 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
45572 20 74 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61 6d   token. For exam
45573 70 6c 65 2c 20 0d 0a 2a 2a 20 75 73 69 6e 67 20  ple, ..** using 
45574 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 22 73 69  the built-in "si
45575 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a  mple" tokenizer:
45576 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c 45 43  ..**..**   SELEC
45577 54 20 66 74 73 5f 74 6f 6b 65 6e 69 7a 65 72 5f  T fts_tokenizer_
45578 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27  test('simple', '
45579 49 20 64 6f 6e 27 74 20 73 65 65 20 68 6f 77 27  I don't see how'
4557a 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 77 69 6c 6c 20  );..**..** will 
4557b 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e  return the strin
4557c 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 22 7b 30  g:..**..**   "{0
4557d 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27   i I 1 dont don'
4557e 74 20 32 20 73 65 65 20 73 65 65 20 33 20 68 6f  t 2 see see 3 ho
4557f 77 20 68 6f 77 7d 22 0d 0a 2a 2a 20 20 20 0d 0a  w how}"..**   ..
45580 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
45581 74 65 73 74 46 75 6e 63 28 0d 0a 20 20 73 71 6c  testFunc(..  sql
45582 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
45583 6e 74 65 78 74 2c 0d 0a 20 20 69 6e 74 20 61 72  ntext,..  int ar
45584 67 63 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  gc,..  sqlite3_v
45585 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b 0d  alue **argv..){.
45586 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61  .  Fts3Hash *pHa
45587 73 68 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74  sh;..  sqlite3_t
45588 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
45589 2a 70 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74  *p;..  sqlite3_t
4558a 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
4558b 69 7a 65 72 20 3d 20 30 3b 0d 0a 20 20 73 71 6c  izer = 0;..  sql
4558c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
4558d 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 30 3b  ursor *pCsr = 0;
4558e 0d 0a 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ....  const char
4558f 20 2a 7a 45 72 72 20 3d 20 30 3b 0d 0a 0d 0a 20   *zErr = 0;.... 
45590 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
45591 6d 65 3b 0d 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  me;..  int nName
45592 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ;..  const char 
45593 2a 7a 49 6e 70 75 74 3b 0d 0a 20 20 69 6e 74 20  *zInput;..  int 
45594 6e 49 6e 70 75 74 3b 0d 0a 0d 0a 20 20 63 6f 6e  nInput;....  con
45595 73 74 20 63 68 61 72 20 2a 7a 41 72 67 20 3d 20  st char *zArg = 
45596 30 3b 0d 0a 0d 0a 20 20 63 6f 6e 73 74 20 63 68  0;....  const ch
45597 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0d 0a 20 20 69  ar *zToken;..  i
45598 6e 74 20 6e 54 6f 6b 65 6e 3b 0d 0a 20 20 69 6e  nt nToken;..  in
45599 74 20 69 53 74 61 72 74 3b 0d 0a 20 20 69 6e 74  t iStart;..  int
4559a 20 69 45 6e 64 3b 0d 0a 20 20 69 6e 74 20 69 50   iEnd;..  int iP
4559b 6f 73 3b 0d 0a 0d 0a 20 20 54 63 6c 5f 4f 62 6a  os;....  Tcl_Obj
4559c 20 2a 70 52 65 74 3b 0d 0a 0d 0a 20 20 61 73 73   *pRet;....  ass
4559d 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c 7c 20  ert( argc==2 || 
4559e 61 72 67 63 3d 3d 33 20 29 3b 0d 0a 0d 0a 20 20  argc==3 );....  
4559f 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  nName = sqlite3_
455a0 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
455a1 5b 30 5d 29 3b 0d 0a 20 20 7a 4e 61 6d 65 20 3d  [0]);..  zName =
455a2 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
455a3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
455a4 74 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 6e  t(argv[0]);..  n
455a5 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f  Input = sqlite3_
455a6 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
455a7 5b 61 72 67 63 2d 31 5d 29 3b 0d 0a 20 20 7a 49  [argc-1]);..  zI
455a8 6e 70 75 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  nput = (const ch
455a9 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
455aa 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61 72 67  ue_text(argv[arg
455ab 63 2d 31 5d 29 3b 0d 0a 0d 0a 20 20 69 66 28 20  c-1]);....  if( 
455ac 61 72 67 63 3d 3d 33 20 29 7b 0d 0a 20 20 20 20  argc==3 ){..    
455ad 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63 68  zArg = (const ch
455ae 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
455af 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
455b0 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 48 61 73  ;..  }....  pHas
455b1 68 20 3d 20 28 46 74 73 33 48 61 73 68 20 2a 29  h = (Fts3Hash *)
455b2 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
455b3 61 28 63 6f 6e 74 65 78 74 29 3b 0d 0a 20 20 70  a(context);..  p
455b4 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
455b5 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73  nizer_module *)s
455b6 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
455b7 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  nd(pHash, zName,
455b8 20 6e 4e 61 6d 65 2b 31 29 3b 0d 0a 0d 0a 20 20   nName+1);....  
455b9 69 66 28 20 21 70 20 29 7b 0d 0a 20 20 20 20 63  if( !p ){..    c
455ba 68 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69  har *zErr = sqli
455bb 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b  te3_mprintf("unk
455bc 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20  nown tokenizer: 
455bd 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20  %s", zName);..  
455be 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
455bf 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
455c0 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 20 20 20 20  zErr, -1);..    
455c1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
455c2 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b  r);..    return;
455c3 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 52 65 74 20  ..  }....  pRet 
455c4 3d 20 54 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0d  = Tcl_NewObj();.
455c5 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f  .  Tcl_IncrRefCo
455c6 75 6e 74 28 70 52 65 74 29 3b 0d 0a 0d 0a 20 20  unt(pRet);....  
455c7 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70  if( SQLITE_OK!=p
455c8 2d 3e 78 43 72 65 61 74 65 28 7a 41 72 67 20 3f  ->xCreate(zArg ?
455c9 20 31 20 3a 20 30 2c 20 26 7a 41 72 67 2c 20 26   1 : 0, &zArg, &
455ca 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0d 0a  pTokenizer) ){..
455cb 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f      zErr = "erro
455cc 72 20 69 6e 20 78 43 72 65 61 74 65 28 29 22 3b  r in xCreate()";
455cd 0d 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73  ..    goto finis
455ce 68 3b 0d 0a 20 20 7d 0d 0a 20 20 70 54 6f 6b 65  h;..  }..  pToke
455cf 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
455d0 20 70 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49 54   p;..  if( SQLIT
455d1 45 5f 4f 4b 21 3d 70 2d 3e 78 4f 70 65 6e 28 70  E_OK!=p->xOpen(p
455d2 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75  Tokenizer, zInpu
455d3 74 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 73 72  t, nInput, &pCsr
455d4 29 20 29 7b 0d 0a 20 20 20 20 7a 45 72 72 20 3d  ) ){..    zErr =
455d5 20 22 65 72 72 6f 72 20 69 6e 20 78 4f 70 65 6e   "error in xOpen
455d6 28 29 22 3b 0d 0a 20 20 20 20 67 6f 74 6f 20 66  ()";..    goto f
455d7 69 6e 69 73 68 3b 0d 0a 20 20 7d 0d 0a 20 20 70  inish;..  }..  p
455d8 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  Csr->pTokenizer 
455d9 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 0d  = pTokenizer;...
455da 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45  .  while( SQLITE
455db 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43  _OK==p->xNext(pC
455dc 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54  sr, &zToken, &nT
455dd 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26  oken, &iStart, &
455de 69 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0d  iEnd, &iPos) ){.
455df 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a  .    Tcl_ListObj
455e0 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c  AppendElement(0,
455e1 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e   pRet, Tcl_NewIn
455e2 74 4f 62 6a 28 69 50 6f 73 29 29 3b 0d 0a 20 20  tObj(iPos));..  
455e3 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70    Tcl_ListObjApp
455e4 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52  endElement(0, pR
455e5 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e  et, Tcl_NewStrin
455e6 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  gObj(zToken, nTo
455e7 6b 65 6e 29 29 3b 0d 0a 20 20 20 20 7a 54 6f 6b  ken));..    zTok
455e8 65 6e 20 3d 20 26 7a 49 6e 70 75 74 5b 69 53 74  en = &zInput[iSt
455e9 61 72 74 5d 3b 0d 0a 20 20 20 20 6e 54 6f 6b 65  art];..    nToke
455ea 6e 20 3d 20 69 45 6e 64 2d 69 53 74 61 72 74 3b  n = iEnd-iStart;
455eb 0d 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62  ..    Tcl_ListOb
455ec 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30  jAppendElement(0
455ed 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53  , pRet, Tcl_NewS
455ee 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c  tringObj(zToken,
455ef 20 6e 54 6f 6b 65 6e 29 29 3b 0d 0a 20 20 7d 0d   nToken));..  }.
455f0 0a 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ...  if( SQLITE_
455f1 4f 4b 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70 43  OK!=p->xClose(pC
455f2 73 72 29 20 29 7b 0d 0a 20 20 20 20 7a 45 72 72  sr) ){..    zErr
455f3 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 6c   = "error in xCl
455f4 6f 73 65 28 29 22 3b 0d 0a 20 20 20 20 67 6f 74  ose()";..    got
455f5 6f 20 66 69 6e 69 73 68 3b 0d 0a 20 20 7d 0d 0a  o finish;..  }..
455f6 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
455f7 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f  =p->xDestroy(pTo
455f8 6b 65 6e 69 7a 65 72 29 20 29 7b 0d 0a 20 20 20  kenizer) ){..   
455f9 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
455fa 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b 0d 0a  n xDestroy()";..
455fb 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b      goto finish;
455fc 0d 0a 20 20 7d 0d 0a 0d 0a 66 69 6e 69 73 68 3a  ..  }....finish:
455fd 0d 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0d  ..  if( zErr ){.
455fe 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
455ff 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
45600 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 20  t, zErr, -1);.. 
45601 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 73 71 6c   }else{..    sql
45602 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
45603 28 63 6f 6e 74 65 78 74 2c 20 54 63 6c 5f 47 65  (context, Tcl_Ge
45604 74 53 74 72 69 6e 67 28 70 52 65 74 29 2c 20 2d  tString(pRet), -
45605 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
45606 45 4e 54 29 3b 0d 0a 20 20 7d 0d 0a 20 20 54 63  ENT);..  }..  Tc
45607 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e 74 28 70  l_DecrRefCount(p
45608 52 65 74 29 3b 0d 0a 7d 0d 0a 0d 0a 73 74 61 74  Ret);..}....stat
45609 69 63 0d 0a 69 6e 74 20 72 65 67 69 73 74 65 72  ic..int register
4560a 54 6f 6b 65 6e 69 7a 65 72 28 0d 0a 20 20 73 71  Tokenizer(..  sq
4560b 6c 69 74 65 33 20 2a 64 62 2c 20 0d 0a 20 20 63  lite3 *db, ..  c
4560c 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0d 0a 20 20  har *zName, ..  
4560d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
4560e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
4560f 70 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  p..){..  int rc;
45610 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
45611 20 2a 70 53 74 6d 74 3b 0d 0a 20 20 63 6f 6e 73   *pStmt;..  cons
45612 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20  t char zSql[] = 
45613 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b  "SELECT fts3_tok
45614 65 6e 69 7a 65 72 28 3f 2c 20 3f 29 22 3b 0d 0a  enizer(?, ?)";..
45615 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
45616 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
45617 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
45618 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 21  , 0);..  if( rc!
45619 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
4561a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
4561b 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f   }....  sqlite3_
4561c 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c  bind_text(pStmt,
4561d 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53   1, zName, -1, S
4561e 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
4561f 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
45620 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 26 70  lob(pStmt, 2, &p
45621 2c 20 73 69 7a 65 6f 66 28 70 29 2c 20 53 51 4c  , sizeof(p), SQL
45622 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20  ITE_STATIC);..  
45623 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
45624 6d 74 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e  mt);....  return
45625 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
45626 65 28 70 53 74 6d 74 29 3b 0d 0a 7d 0d 0a 0d 0a  e(pStmt);..}....
45627 73 74 61 74 69 63 0d 0a 69 6e 74 20 71 75 65 72  static..int quer
45628 79 54 6f 6b 65 6e 69 7a 65 72 28 0d 0a 20 20 73  yTokenizer(..  s
45629 71 6c 69 74 65 33 20 2a 64 62 2c 20 0d 0a 20 20  qlite3 *db, ..  
4562a 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0d 0a  char *zName,  ..
4562b 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
4562c 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
4562d 20 2a 2a 70 70 0d 0a 29 7b 0d 0a 20 20 69 6e 74   **pp..){..  int
4562e 20 72 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f   rc;..  sqlite3_
4562f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0d 0a 20 20  stmt *pStmt;..  
45630 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b  const char zSql[
45631 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33  ] = "SELECT fts3
45632 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0d  _tokenizer(?)";.
45633 0a 0d 0a 20 20 2a 70 70 20 3d 20 30 3b 0d 0a 20  ...  *pp = 0;.. 
45634 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
45635 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71  epare_v2(db, zSq
45636 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
45637 29 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
45638 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
45639 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d  return rc;..  }.
4563a 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ...  sqlite3_bin
4563b 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c  d_text(pStmt, 1,
4563c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49   zName, -1, SQLI
4563d 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 69  TE_STATIC);..  i
4563e 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  f( SQLITE_ROW==s
4563f 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
45640 74 29 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 73  t) ){..    if( s
45641 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
45642 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51  pe(pStmt, 0)==SQ
45643 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0d 0a 20 20  LITE_BLOB ){..  
45644 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64      memcpy((void
45645 20 2a 29 70 70 2c 20 73 71 6c 69 74 65 33 5f 63   *)pp, sqlite3_c
45646 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
45647 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70  , 0), sizeof(*pp
45648 29 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ));..    }..  }.
45649 0a 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ...  return sqli
4564a 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
4564b 6d 74 29 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54  mt);..}....SQLIT
4564c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4564d 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
4564e 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
4564f 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
45650 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
45651 70 70 4d 6f 64 75 6c 65 29 3b 0d 0a 0d 0a 2f 2a  ppModule);..../*
45652 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
45653 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61  ion of the scala
45654 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f  r function fts3_
45655 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e  tokenizer_intern
45656 61 6c 5f 74 65 73 74 28 29 2e 0d 0a 2a 2a 20 54  al_test()...** T
45657 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
45658 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
45659 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74   only, it is not
4565a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
4565b 0d 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73  ..** build unles
4565c 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73  s SQLITE_TEST is
4565d 20 64 65 66 69 6e 65 64 2e 0d 0a 2a 2a 0d 0a 2a   defined...**..*
4565e 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
4565f 20 74 68 69 73 20 69 73 20 74 6f 20 74 65 73 74   this is to test
45660 20 74 68 61 74 20 74 68 65 20 66 74 73 33 5f 74   that the fts3_t
45661 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74  okenizer() funct
45662 69 6f 6e 0d 0a 2a 2a 20 63 61 6e 20 62 65 20 75  ion..** can be u
45663 73 65 64 20 61 73 20 64 65 73 69 67 6e 65 64 20  sed as designed 
45664 62 79 20 74 68 65 20 43 2d 63 6f 64 65 20 69 6e  by the C-code in
45665 20 74 68 65 20 71 75 65 72 79 54 6f 6b 65 6e 69   the queryTokeni
45666 7a 65 72 20 61 6e 64 0d 0a 2a 2a 20 72 65 67 69  zer and..** regi
45667 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 29 20  sterTokenizer() 
45668 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 2e  functions above.
45669 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74   These two funct
4566a 69 6f 6e 73 20 61 72 65 20 72 65 70 65 61 74 65  ions are repeate
4566b 64 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 52 45 41  d..** in the REA
4566c 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 20 66 69  DME.tokenizer fi
4566d 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65  le as an example
4566e 2c 20 73 6f 20 69 74 20 69 73 20 69 6d 70 6f 72  , so it is impor
4566f 74 61 6e 74 20 74 6f 0d 0a 2a 2a 20 74 65 73 74  tant to..** test
45670 20 74 68 65 6d 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54   them...**..** T
45671 6f 20 72 75 6e 20 74 68 65 20 74 65 73 74 73 2c  o run the tests,
45672 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 66 74   evaluate the ft
45673 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74  s3_tokenizer_int
45674 65 72 6e 61 6c 5f 74 65 73 74 28 29 20 73 63 61  ernal_test() sca
45675 6c 61 72 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  lar..** function
45676 20 77 69 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e   with no argumen
45677 74 73 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20  ts. An assert() 
45678 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 61 20 70  will fail if a p
45679 72 6f 62 6c 65 6d 20 69 73 0d 0a 2a 2a 20 64 65  roblem is..** de
4567a 74 65 63 74 65 64 2e 20 69 2e 65 2e 3a 0d 0a 2a  tected. i.e.:..*
4567b 2a 0d 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  *..**     SELECT
4567c 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f   fts3_tokenizer_
4567d 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 3b  internal_test();
4567e 0d 0a 2a 2a 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ..**..*/..static
4567f 20 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75 6e   void intTestFun
45680 63 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  c(..  sqlite3_co
45681 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0d  ntext *context,.
45682 0a 20 20 69 6e 74 20 61 72 67 63 2c 0d 0a 20 20  .  int argc,..  
45683 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
45684 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  argv..){..  int 
45685 72 63 3b 0d 0a 20 20 63 6f 6e 73 74 20 73 71 6c  rc;..  const sql
45686 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
45687 6f 64 75 6c 65 20 2a 70 31 3b 0d 0a 20 20 63 6f  odule *p1;..  co
45688 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
45689 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 32  nizer_module *p2
4568a 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ;..  sqlite3 *db
4568b 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73 71   = (sqlite3 *)sq
4568c 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
4568d 63 6f 6e 74 65 78 74 29 3b 0d 0a 0d 0a 20 20 55  context);....  U
4568e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
4568f 61 72 67 63 29 3b 0d 0a 20 20 55 4e 55 53 45 44  argc);..  UNUSED
45690 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29  _PARAMETER(argv)
45691 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 65 73 74 20 74  ;....  /* Test t
45692 68 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f  he query functio
45693 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 46  n */..  sqlite3F
45694 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
45695 65 72 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0d 0a  erModule(&p1);..
45696 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65    rc = queryToke
45697 6e 69 7a 65 72 28 64 62 2c 20 22 73 69 6d 70 6c  nizer(db, "simpl
45698 65 22 2c 20 26 70 32 29 3b 0d 0a 20 20 61 73 73  e", &p2);..  ass
45699 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
4569a 4f 4b 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  OK );..  assert(
4569b 20 70 31 3d 3d 70 32 20 29 3b 0d 0a 20 20 72 63   p1==p2 );..  rc
4569c 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65   = queryTokenize
4569d 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b  r(db, "nosuchtok
4569e 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0d 0a  enizer", &p2);..
4569f 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
456a0 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0d 0a 20  LITE_ERROR );.. 
456a1 20 61 73 73 65 72 74 28 20 70 32 3d 3d 30 20 29   assert( p2==0 )
456a2 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  ;..  assert( 0==
456a3 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 65  strcmp(sqlite3_e
456a4 72 72 6d 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e  rrmsg(db), "unkn
456a5 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e  own tokenizer: n
456a6 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29  osuchtokenizer")
456a7 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 54 65 73 74   );....  /* Test
456a8 20 74 68 65 20 73 74 6f 72 61 67 65 20 66 75 6e   the storage fun
456a9 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20 72 63 20 3d  ction */..  rc =
456aa 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
456ab 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f  er(db, "nosuchto
456ac 6b 65 6e 69 7a 65 72 22 2c 20 70 31 29 3b 0d 0a  kenizer", p1);..
456ad 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
456ae 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 72 63  LITE_OK );..  rc
456af 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65   = queryTokenize
456b0 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b  r(db, "nosuchtok
456b1 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0d 0a  enizer", &p2);..
456b2 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
456b3 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 20 20 61 73  LITE_OK );..  as
456b4 73 65 72 74 28 20 70 32 3d 3d 70 31 20 29 3b 0d  sert( p2==p1 );.
456b5 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ...  sqlite3_res
456b6 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
456b7 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49  , "ok", -1, SQLI
456b8 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 7d 0d 0a  TE_STATIC);..}..
456b9 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a  ..#endif..../*..
456ba 2a 2a 20 53 65 74 20 75 70 20 53 51 4c 20 6f 62  ** Set up SQL ob
456bb 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73  jects in databas
456bc 65 20 64 62 20 75 73 65 64 20 74 6f 20 61 63 63  e db used to acc
456bd 65 73 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ess the contents
456be 20 6f 66 0d 0a 2a 2a 20 74 68 65 20 68 61 73 68   of..** the hash
456bf 20 74 61 62 6c 65 20 70 6f 69 6e 74 65 64 20 74   table pointed t
456c0 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 70 48  o by argument pH
456c1 61 73 68 2e 20 54 68 65 20 68 61 73 68 20 74 61  ash. The hash ta
456c2 62 6c 65 20 6d 75 73 74 0d 0a 2a 2a 20 62 65 65  ble must..** bee
456c3 6e 20 69 6e 69 74 69 61 6c 69 73 65 64 20 74 6f  n initialised to
456c4 20 75 73 65 20 73 74 72 69 6e 67 20 6b 65 79 73   use string keys
456c5 2c 20 61 6e 64 20 74 6f 20 74 61 6b 65 20 61 20  , and to take a 
456c6 70 72 69 76 61 74 65 20 63 6f 70 79 20 0d 0a 2a  private copy ..*
456c7 2a 20 6f 66 20 74 68 65 20 6b 65 79 20 77 68 65  * of the key whe
456c8 6e 20 61 20 76 61 6c 75 65 20 69 73 20 69 6e 73  n a value is ins
456c9 65 72 74 65 64 2e 20 69 2e 65 2e 20 62 79 20 61  erted. i.e. by a
456ca 20 63 61 6c 6c 20 73 69 6d 69 6c 61 72 20 74 6f   call similar to
456cb 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 73 71 6c  :..**..**    sql
456cc 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
456cd 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53  (pHash, FTS3_HAS
456ce 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0d 0a 2a  H_STRING, 1);..*
456cf 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
456d0 69 6f 6e 20 61 64 64 73 20 61 20 73 63 61 6c 61  ion adds a scala
456d1 72 20 66 75 6e 63 74 69 6f 6e 20 28 73 65 65 20  r function (see 
456d2 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61  header comment a
456d3 62 6f 76 65 0d 0a 2a 2a 20 73 63 61 6c 61 72 46  bove..** scalarF
456d4 75 6e 63 28 29 20 69 6e 20 74 68 69 73 20 66 69  unc() in this fi
456d5 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 29 20  le for details) 
456d6 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45 5f 54  and, if ENABLE_T
456d7 41 42 4c 45 20 69 73 0d 0a 2a 2a 20 64 65 66 69  ABLE is..** defi
456d8 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 61 74 69  ned at compilati
456d9 6f 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d 70 6f  on time, a tempo
456da 72 61 72 79 20 76 69 72 74 75 61 6c 20 74 61 62  rary virtual tab
456db 6c 65 20 28 73 65 65 20 68 65 61 64 65 72 20 0d  le (see header .
456dc 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76  .** comment abov
456dd 65 20 73 74 72 75 63 74 20 48 61 73 68 54 61 62  e struct HashTab
456de 6c 65 56 74 61 62 29 20 74 6f 20 74 68 65 20 64  leVtab) to the d
456df 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
456e0 42 6f 74 68 20 0d 0a 2a 2a 20 70 72 6f 76 69 64  Both ..** provid
456e1 65 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  e read/write acc
456e2 65 73 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65  ess to the conte
456e3 6e 74 73 20 6f 66 20 2a 70 48 61 73 68 2e 0d 0a  nts of *pHash...
456e4 2a 2a 0d 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  **..** The third
456e5 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
456e6 73 20 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d  s function, zNam
456e7 65 2c 20 69 73 20 75 73 65 64 20 61 73 20 74 68  e, is used as th
456e8 65 20 6e 61 6d 65 0d 0a 2a 2a 20 6f 66 20 62 6f  e name..** of bo
456e9 74 68 20 74 68 65 20 73 63 61 6c 61 72 20 61 6e  th the scalar an
456ea 64 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20 74  d, if created, t
456eb 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
456ec 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
456ed 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
456ee 33 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62  3Fts3InitHashTab
456ef 6c 65 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  le(..  sqlite3 *
456f0 64 62 2c 20 0d 0a 20 20 46 74 73 33 48 61 73 68  db, ..  Fts3Hash
456f1 20 2a 70 48 61 73 68 2c 20 0d 0a 20 20 63 6f 6e   *pHash, ..  con
456f2 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0d 0a  st char *zName..
456f3 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
456f4 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 76 6f 69  QLITE_OK;..  voi
456f5 64 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29 70  d *p = (void *)p
456f6 48 61 73 68 3b 0d 0a 20 20 63 6f 6e 73 74 20 69  Hash;..  const i
456f7 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49 54 45 5f  nt any = SQLITE_
456f8 41 4e 59 3b 0d 0a 0d 0a 23 69 66 64 65 66 20 53  ANY;....#ifdef S
456f9 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 63 68  QLITE_TEST..  ch
456fa 61 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0d 0a  ar *zTest = 0;..
456fb 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d    char *zTest2 =
456fc 20 30 3b 0d 0a 20 20 76 6f 69 64 20 2a 70 64 62   0;..  void *pdb
456fd 20 3d 20 28 76 6f 69 64 20 2a 29 64 62 3b 0d 0a   = (void *)db;..
456fe 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69 74 65    zTest = sqlite
456ff 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 74 65  3_mprintf("%s_te
45700 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0d 0a 20 20  st", zName);..  
45701 7a 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65 33  zTest2 = sqlite3
45702 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e 74  _mprintf("%s_int
45703 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e 61  ernal_test", zNa
45704 6d 65 29 3b 0d 0a 20 20 69 66 28 20 21 7a 54 65  me);..  if( !zTe
45705 73 74 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b  st || !zTest2 ){
45706 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
45707 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 23  E_NOMEM;..  }..#
45708 65 6e 64 69 66 0d 0a 0d 0a 20 20 69 66 28 20 53  endif....  if( S
45709 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0d  QLITE_OK==rc ){.
4570a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
4570b 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
4570c 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20  n(db, zName, 1, 
4570d 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75  any, p, scalarFu
4570e 6e 63 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 7d 0d  nc, 0, 0);..  }.
4570f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
45710 3d 3d 72 63 20 29 7b 0d 0a 20 20 20 20 72 63 20  ==rc ){..    rc 
45711 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
45712 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e  _function(db, zN
45713 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20  ame, 2, any, p, 
45714 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30  scalarFunc, 0, 0
45715 29 3b 0d 0a 20 20 7d 0d 0a 23 69 66 64 65 66 20  );..  }..#ifdef 
45716 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 69  SQLITE_TEST..  i
45717 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63  f( SQLITE_OK==rc
45718 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71   ){..    rc = sq
45719 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
4571a 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c  ction(db, zTest,
4571b 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74   2, any, p, test
4571c 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0d 0a 20 20  Func, 0, 0);..  
4571d 7d 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  }..  if( SQLITE_
4571e 4f 4b 3d 3d 72 63 20 29 7b 0d 0a 20 20 20 20 72  OK==rc ){..    r
4571f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
45720 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
45721 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79 2c 20 70  zTest, 3, any, p
45722 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30  , testFunc, 0, 0
45723 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 53  );..  }..  if( S
45724 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0d  QLITE_OK==rc ){.
45725 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
45726 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
45727 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c 20 30 2c  n(db, zTest2, 0,
45728 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74 54 65   any, pdb, intTe
45729 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0d 0a  stFunc, 0, 0);..
4572a 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23    }..#endif....#
4572b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
4572c 54 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  T..  sqlite3_fre
4572d 65 28 7a 54 65 73 74 29 3b 0d 0a 20 20 73 71 6c  e(zTest);..  sql
4572e 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 32  ite3_free(zTest2
4572f 29 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 20 20  );..#endif....  
45730 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
45731 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
45732 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
45733 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
45734 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
45735 2a 2f 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  */..../*********
45736 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
45737 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a  3_tokenizer.c **
45738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45739 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4573a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
4573b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
4573c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  e fts3_tokenizer
4573d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  1.c ************
4573e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4573f 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
45740 30 30 36 20 4f 63 74 20 31 30 0d 0a 2a 2a 0d 0a  006 Oct 10..**..
45741 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
45742 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
45743 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
45744 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
45745 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
45746 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
45747 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
45748 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
45749 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
4574a 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
4574b 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
4574c 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
4574d 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
4574e 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
4574f 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
45750 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
45751 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
45752 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
45753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45755 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45756 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45757 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20  *******..**..** 
45758 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
45759 66 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20 66  f the "simple" f
4575a 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68 20  ull-text-search 
4575b 74 6f 6b 65 6e 69 7a 65 72 2e 0d 0a 2a 2f 0d 0a  tokenizer...*/..
4575c 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 63 6f 64  ../*..** The cod
4575d 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
4575e 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20  s only compiled 
4575f 69 66 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20  if:..**..**     
45760 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
45761 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
45762 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e   as an extension
45763 0d 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  ..**       (in w
45764 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
45765 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
45766 69 6e 65 64 29 2c 20 6f 72 0d 0a 2a 2a 0d 0a 2a  ined), or..**..*
45767 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
45768 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
45769 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
4576a 63 6f 72 65 20 6f 66 0d 0a 2a 2a 20 20 20 20 20  core of..**     
4576b 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69    SQLite (in whi
4576c 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45  ch case SQLITE_E
4576d 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65  NABLE_FTS3 is de
4576e 66 69 6e 65 64 29 2e 0d 0a 2a 2f 0d 0a 23 69 66  fined)...*/..#if
4576f 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
45770 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
45771 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
45772 46 54 53 33 29 0d 0a 0d 0a 2f 2a 20 23 69 6e 63  FTS3)..../* #inc
45773 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20  lude <assert.h> 
45774 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  */../* #include 
45775 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0d 0a 2f  <stdlib.h> */../
45776 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69  * #include <stdi
45777 6f 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63  o.h> */../* #inc
45778 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20  lude <string.h> 
45779 2a 2f 0d 0a 0d 0a 0d 0a 74 79 70 65 64 65 66 20  */......typedef 
4577a 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f  struct simple_to
4577b 6b 65 6e 69 7a 65 72 20 7b 0d 0a 20 20 73 71 6c  kenizer {..  sql
4577c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62  ite3_tokenizer b
4577d 61 73 65 3b 0d 0a 20 20 63 68 61 72 20 64 65 6c  ase;..  char del
4577e 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20 20  im[128];        
4577f 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53 43       /* flag ASC
45780 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f  II delimiters */
45781 0d 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  ..} simple_token
45782 69 7a 65 72 3b 0d 0a 0d 0a 74 79 70 65 64 65 66  izer;....typedef
45783 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74   struct simple_t
45784 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
45785 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  {..  sqlite3_tok
45786 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61  enizer_cursor ba
45787 73 65 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  se;..  const cha
45788 72 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20 20  r *pInput;      
45789 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20      /* input we 
4578a 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a  are tokenizing *
4578b 2f 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b  /..  int nBytes;
4578c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4578d 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
4578e 20 69 6e 70 75 74 20 2a 2f 0d 0a 20 20 69 6e 74   input */..  int
4578f 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
45790 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72            /* cur
45791 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e  rent position in
45792 20 70 49 6e 70 75 74 20 2a 2f 0d 0a 20 20 69 6e   pInput */..  in
45793 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  t iToken;       
45794 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
45795 64 65 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65  dex of next toke
45796 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
45797 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70 54 6f   */..  char *pTo
45798 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
45799 20 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20 66      /* storage f
4579a 6f 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e  or current token
4579b 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 6f 6b 65   */..  int nToke
4579c 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20 20  nAllocated;     
4579d 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c      /* space all
4579e 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e  ocated to zToken
4579f 20 62 75 66 66 65 72 20 2a 2f 0d 0a 7d 20 73 69   buffer */..} si
457a0 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
457a1 75 72 73 6f 72 3b 0d 0a 0d 0a 0d 0a 73 74 61 74  ursor;......stat
457a2 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c  ic int simpleDel
457a3 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  im(simple_tokeni
457a4 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64  zer *t, unsigned
457a5 20 63 68 61 72 20 63 29 7b 0d 0a 20 20 72 65 74   char c){..  ret
457a6 75 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74 2d  urn c<0x80 && t-
457a7 3e 64 65 6c 69 6d 5b 63 5d 3b 0d 0a 7d 0d 0a 73  >delim[c];..}..s
457a8 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 5f 69  tatic int fts3_i
457a9 73 61 6c 6e 75 6d 28 69 6e 74 20 78 29 7b 0d 0a  salnum(int x){..
457aa 20 20 72 65 74 75 72 6e 20 28 78 3e 3d 27 30 27    return (x>='0'
457ab 20 26 26 20 78 3c 3d 27 39 27 29 20 7c 7c 20 28   && x<='9') || (
457ac 78 3e 3d 27 41 27 20 26 26 20 78 3c 3d 27 5a 27  x>='A' && x<='Z'
457ad 29 20 7c 7c 20 28 78 3e 3d 27 61 27 20 26 26 20  ) || (x>='a' && 
457ae 78 3c 3d 27 7a 27 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  x<='z');..}..../
457af 2a 0d 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  *..** Create a n
457b0 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73  ew tokenizer ins
457b1 74 61 6e 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  tance...*/..stat
457b2 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 72 65  ic int simpleCre
457b3 61 74 65 28 0d 0a 20 20 69 6e 74 20 61 72 67 63  ate(..  int argc
457b4 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  , const char * c
457b5 6f 6e 73 74 20 2a 61 72 67 76 2c 0d 0a 20 20 73  onst *argv,..  s
457b6 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
457b7 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0d 0a   **ppTokenizer..
457b8 29 7b 0d 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b  ){..  simple_tok
457b9 65 6e 69 7a 65 72 20 2a 74 3b 0d 0a 0d 0a 20 20  enizer *t;....  
457ba 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  t = (simple_toke
457bb 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33  nizer *) sqlite3
457bc 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
457bd 74 29 29 3b 0d 0a 20 20 69 66 28 20 74 3d 3d 4e  t));..  if( t==N
457be 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ULL ) return SQL
457bf 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 6d 65  ITE_NOMEM;..  me
457c0 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f  mset(t, 0, sizeo
457c1 66 28 2a 74 29 29 3b 0d 0a 0d 0a 20 20 2f 2a 20  f(*t));....  /* 
457c2 54 4f 44 4f 28 73 68 65 73 73 29 20 44 65 6c 69  TODO(shess) Deli
457c3 6d 69 74 65 72 73 20 6e 65 65 64 20 74 6f 20 72  miters need to r
457c4 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20 66  emain the same f
457c5 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c 0d  rom run to run,.
457c6 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65  .  ** else we ne
457c7 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e 20 20  ed to reindex.  
457c8 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75  One solution wou
457c9 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74 61 62  ld be a meta-tab
457ca 6c 65 20 74 6f 0d 0a 20 20 2a 2a 20 74 72 61 63  le to..  ** trac
457cb 6b 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74 69  k such informati
457cc 6f 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  on in the databa
457cd 73 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f 6e  se, then we'd on
457ce 6c 79 20 77 61 6e 74 20 74 68 69 73 0d 0a 20 20  ly want this..  
457cf 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ** information o
457d0 6e 20 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72  n the initial cr
457d1 65 61 74 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69  eate...  */..  i
457d2 66 28 20 61 72 67 63 3e 31 20 29 7b 0d 0a 20 20  f( argc>1 ){..  
457d3 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 28 69 6e    int i, n = (in
457d4 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d  t)strlen(argv[1]
457d5 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  );..    for(i=0;
457d6 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20   i<n; i++){..   
457d7 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
457d8 20 63 68 20 3d 20 61 72 67 76 5b 31 5d 5b 69 5d   ch = argv[1][i]
457d9 3b 0d 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 65  ;..      /* We e
457da 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e 27 74 20  xplicitly don't 
457db 73 75 70 70 6f 72 74 20 55 54 46 2d 38 20 64 65  support UTF-8 de
457dc 6c 69 6d 69 74 65 72 73 20 66 6f 72 20 6e 6f 77  limiters for now
457dd 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20  . */..      if( 
457de 63 68 3e 3d 30 78 38 30 20 29 7b 0d 0a 20 20 20  ch>=0x80 ){..   
457df 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
457e0 65 28 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  e(t);..        r
457e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
457e2 4f 52 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  OR;..      }..  
457e3 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b 63 68 5d      t->delim[ch]
457e4 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 1;..    }..  
457e5 7d 20 65 6c 73 65 20 7b 0d 0a 20 20 20 20 2f 2a  } else {..    /*
457e6 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68 61 6e   Mark non-alphan
457e7 75 6d 65 72 69 63 20 41 53 43 49 49 20 63 68 61  umeric ASCII cha
457e8 72 61 63 74 65 72 73 20 61 73 20 64 65 6c 69 6d  racters as delim
457e9 69 74 65 72 73 20 2a 2f 0d 0a 20 20 20 20 69 6e  iters */..    in
457ea 74 20 69 3b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  t i;..    for(i=
457eb 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
457ec 0d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d  ..      t->delim
457ed 5b 69 5d 20 3d 20 21 66 74 73 33 5f 69 73 61 6c  [i] = !fts3_isal
457ee 6e 75 6d 28 69 29 20 3f 20 2d 31 20 3a 20 30 3b  num(i) ? -1 : 0;
457ef 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
457f0 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
457f1 20 26 74 2d 3e 62 61 73 65 3b 0d 0a 20 20 72 65   &t->base;..  re
457f2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
457f3 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 73  .}..../*..** Des
457f4 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
457f5 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
457f6 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73   simpleDestroy(s
457f7 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
457f8 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0d 0a   *pTokenizer){..
457f9 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
457fa 54 6f 6b 65 6e 69 7a 65 72 29 3b 0d 0a 20 20 72  Tokenizer);..  r
457fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
457fc 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50 72  ..}..../*..** Pr
457fd 65 70 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74  epare to begin t
457fe 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74  okenizing a part
457ff 69 63 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20  icular string.  
45800 54 68 65 20 69 6e 70 75 74 0d 0a 2a 2a 20 73 74  The input..** st
45801 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
45802 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74 5b 30  ized is pInput[0
45803 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20 41 20  ..nBytes-1].  A 
45804 63 75 72 73 6f 72 0d 0a 2a 2a 20 75 73 65 64 20  cursor..** used 
45805 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  to incrementally
45806 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20 73   tokenize this s
45807 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
45808 64 20 69 6e 20 0d 0a 2a 2a 20 2a 70 70 43 75 72  d in ..** *ppCur
45809 73 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  sor...*/..static
4580a 20 69 6e 74 20 73 69 6d 70 6c 65 4f 70 65 6e 28   int simpleOpen(
4580b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ..  sqlite3_toke
4580c 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
4580d 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
4580e 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0d 0a  e tokenizer */..
4580f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49    const char *pI
45810 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65 73  nput, int nBytes
45811 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69  ,        /* Stri
45812 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a  ng to be tokeniz
45813 65 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  ed */..  sqlite3
45814 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
45815 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
45816 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
45817 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0d 0a  tion cursor */..
45818 29 7b 0d 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b  ){..  simple_tok
45819 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63  enizer_cursor *c
4581a 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  ;....  UNUSED_PA
4581b 52 41 4d 45 54 45 52 28 70 54 6f 6b 65 6e 69 7a  RAMETER(pTokeniz
4581c 65 72 29 3b 0d 0a 0d 0a 20 20 63 20 3d 20 28 73  er);....  c = (s
4581d 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
4581e 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65  cursor *) sqlite
4581f 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
45820 2a 63 29 29 3b 0d 0a 20 20 69 66 28 20 63 3d 3d  *c));..  if( c==
45821 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
45822 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 0d 0a 20  LITE_NOMEM;.... 
45823 20 63 2d 3e 70 49 6e 70 75 74 20 3d 20 70 49 6e   c->pInput = pIn
45824 70 75 74 3b 0d 0a 20 20 69 66 28 20 70 49 6e 70  put;..  if( pInp
45825 75 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 63 2d  ut==0 ){..    c-
45826 3e 6e 42 79 74 65 73 20 3d 20 30 3b 0d 0a 20 20  >nBytes = 0;..  
45827 7d 65 6c 73 65 20 69 66 28 20 6e 42 79 74 65 73  }else if( nBytes
45828 3c 30 20 29 7b 0d 0a 20 20 20 20 63 2d 3e 6e 42  <0 ){..    c->nB
45829 79 74 65 73 20 3d 20 28 69 6e 74 29 73 74 72 6c  ytes = (int)strl
4582a 65 6e 28 70 49 6e 70 75 74 29 3b 0d 0a 20 20 7d  en(pInput);..  }
4582b 65 6c 73 65 7b 0d 0a 20 20 20 20 63 2d 3e 6e 42  else{..    c->nB
4582c 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b 0d 0a  ytes = nBytes;..
4582d 20 20 7d 0d 0a 20 20 63 2d 3e 69 4f 66 66 73 65    }..  c->iOffse
4582e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
4582f 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20         /* start 
45830 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68  tokenizing at th
45831 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0d 0a  e beginning */..
45832 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b    c->iToken = 0;
45833 0d 0a 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20  ..  c->pToken = 
45834 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20 20  NULL;           
45835 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20      /* no space 
45836 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e 20  allocated, yet. 
45837 2a 2f 0d 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41  */..  c->nTokenA
45838 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0d 0a 0d  llocated = 0;...
45839 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26  .  *ppCursor = &
4583a 63 2d 3e 62 61 73 65 3b 0d 0a 20 20 72 65 74 75  c->base;..  retu
4583b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
4583c 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65  ..../*..** Close
4583d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
4583e 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
4583f 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
45840 6c 6c 20 74 6f 0d 0a 2a 2a 20 73 69 6d 70 6c 65  ll to..** simple
45841 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e 0d 0a 2a  Open() above...*
45842 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  /..static int si
45843 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69 74 65  mpleClose(sqlite
45844 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
45845 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0d 0a 20  or *pCursor){.. 
45846 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65   simple_tokenize
45847 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73  r_cursor *c = (s
45848 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
45849 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
4584a 72 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r;..  sqlite3_fr
4584b 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0d 0a  ee(c->pToken);..
4584c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
4584d 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
4584e 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
4584f 0d 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  ..** Extract the
45850 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d   next token from
45851 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
45852 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72  cursor.  The cur
45853 73 6f 72 20 6d 75 73 74 0d 0a 2a 2a 20 68 61 76  sor must..** hav
45854 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  e been opened by
45855 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
45856 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0d 0a   simpleOpen()...
45857 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  */..static int s
45858 69 6d 70 6c 65 4e 65 78 74 28 0d 0a 20 20 73 71  impleNext(..  sq
45859 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
4585a 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
4585b 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75    /* Cursor retu
4585c 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f 70  rned by simpleOp
4585d 65 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63  en */..  const c
4585e 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
4585f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45860 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
45861 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
45862 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74  */..  int *pnByt
45863 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
45864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
45865 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
45866 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 20  s in token */.. 
45867 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
45868 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
45869 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
4586a 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  rting offset of 
4586b 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  token */..  int 
4586c 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
4586d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4586e 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f  /* OUT: Ending o
4586f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
45870 2f 0d 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69  /..  int *piPosi
45871 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
45872 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
45873 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65   Position intege
45874 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 29  r of token */..)
45875 7b 0d 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  {..  simple_toke
45876 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20  nizer_cursor *c 
45877 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  = (simple_tokeni
45878 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  zer_cursor *) pC
45879 75 72 73 6f 72 3b 0d 0a 20 20 73 69 6d 70 6c 65  ursor;..  simple
4587a 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20  _tokenizer *t = 
4587b 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
4587c 72 20 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54  r *) pCursor->pT
4587d 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 20 20 75 6e 73  okenizer;..  uns
4587e 69 67 6e 65 64 20 63 68 61 72 20 2a 70 20 3d 20  igned char *p = 
4587f 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
45880 29 63 2d 3e 70 49 6e 70 75 74 3b 0d 0a 0d 0a 20  )c->pInput;.... 
45881 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
45882 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29 7b 0d  et<c->nBytes ){.
45883 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f  .    int iStartO
45884 66 66 73 65 74 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  ffset;....    /*
45885 20 53 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d   Scan past delim
45886 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73 20  iter characters 
45887 2a 2f 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 63  */..    while( c
45888 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79  ->iOffset<c->nBy
45889 74 65 73 20 26 26 20 73 69 6d 70 6c 65 44 65 6c  tes && simpleDel
4588a 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73  im(t, p[c->iOffs
4588b 65 74 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 63  et]) ){..      c
4588c 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0d 0a 20 20  ->iOffset++;..  
4588d 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f    }....    /* Co
4588e 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65  unt non-delimite
4588f 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f  r characters. */
45890 0d 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66 73  ..    iStartOffs
45891 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b  et = c->iOffset;
45892 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  ..    while( c->
45893 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65  iOffset<c->nByte
45894 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65 6c 69  s && !simpleDeli
45895 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65  m(t, p[c->iOffse
45896 74 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 63 2d  t]) ){..      c-
45897 3e 69 4f 66 66 73 65 74 2b 2b 3b 0d 0a 20 20 20  >iOffset++;..   
45898 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 63 2d   }....    if( c-
45899 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72 74 4f  >iOffset>iStartO
4589a 66 66 73 65 74 20 29 7b 0d 0a 20 20 20 20 20 20  ffset ){..      
4589b 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f  int i, n = c->iO
4589c 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73  ffset-iStartOffs
4589d 65 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e  et;..      if( n
4589e 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61  >c->nTokenAlloca
4589f 74 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ted ){..        
458a0 63 68 61 72 20 2a 70 4e 65 77 3b 0d 0a 20 20 20  char *pNew;..   
458a1 20 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c       c->nTokenAl
458a2 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0d  located = n+20;.
458a3 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
458a4 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
458a5 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54  c->pToken, c->nT
458a6 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0d  okenAllocated);.
458a7 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4e  .        if( !pN
458a8 65 77 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  ew ) return SQLI
458a9 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20  TE_NOMEM;..     
458aa 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 70     c->pToken = p
458ab 4e 65 77 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  New;..      }.. 
458ac 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
458ad 6e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  n; i++){..      
458ae 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
458af 20 54 68 69 73 20 6e 65 65 64 73 20 65 78 70 61   This needs expa
458b0 6e 73 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 20  nsion to handle 
458b1 55 54 46 2d 38 0d 0a 20 20 20 20 20 20 20 20 2a  UTF-8..        *
458b2 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69  * case-insensiti
458b3 76 69 74 79 2e 0d 0a 20 20 20 20 20 20 20 20 2a  vity...        *
458b4 2f 0d 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67  /..        unsig
458b5 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 70 5b  ned char ch = p[
458b6 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 5d 3b  iStartOffset+i];
458b7 0d 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f  ..        c->pTo
458b8 6b 65 6e 5b 69 5d 20 3d 20 28 63 68 61 72 29 28  ken[i] = (char)(
458b9 28 63 68 3e 3d 27 41 27 20 26 26 20 63 68 3c 3d  (ch>='A' && ch<=
458ba 27 5a 27 29 20 3f 20 63 68 2d 27 41 27 2b 27 61  'Z') ? ch-'A'+'a
458bb 27 20 3a 20 63 68 29 3b 0d 0a 20 20 20 20 20 20  ' : ch);..      
458bc 7d 0d 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b 65  }..      *ppToke
458bd 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0d 0a  n = c->pToken;..
458be 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73 20 3d        *pnBytes =
458bf 20 6e 3b 0d 0a 20 20 20 20 20 20 2a 70 69 53 74   n;..      *piSt
458c0 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61  artOffset = iSta
458c1 72 74 4f 66 66 73 65 74 3b 0d 0a 20 20 20 20 20  rtOffset;..     
458c2 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
458c3 63 2d 3e 69 4f 66 66 73 65 74 3b 0d 0a 20 20 20  c->iOffset;..   
458c4 20 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d     *piPosition =
458c5 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0d 0a 0d   c->iToken++;...
458c6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
458c7 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d  LITE_OK;..    }.
458c8 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
458c9 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a 7d 0d 0a  QLITE_DONE;..}..
458ca 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 65 74  ../*..** The set
458cb 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61   of routines tha
458cc 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
458cd 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  simple tokenizer
458ce 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63 6f 6e  ..*/..static con
458cf 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
458d0 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70  izer_module simp
458d1 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
458d2 65 20 3d 20 7b 0d 0a 20 20 30 2c 0d 0a 20 20 73  e = {..  0,..  s
458d3 69 6d 70 6c 65 43 72 65 61 74 65 2c 0d 0a 20 20  impleCreate,..  
458d4 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0d 0a  simpleDestroy,..
458d5 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0d 0a 20    simpleOpen,.. 
458d6 20 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0d 0a 20   simpleClose,.. 
458d7 20 73 69 6d 70 6c 65 4e 65 78 74 2c 0d 0a 7d 3b   simpleNext,..};
458d8 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6c 6c 6f 63  ..../*..** Alloc
458d9 61 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c 65  ate a new simple
458da 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74   tokenizer.  Ret
458db 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
458dc 20 74 68 65 20 6e 65 77 0d 0a 2a 2a 20 74 6f 6b   the new..** tok
458dd 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64  enizer in *ppMod
458de 75 6c 65 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  ule..*/..SQLITE_
458df 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
458e0 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
458e1 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0d 0a  kenizerModule(..
458e2 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
458e3 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
458e4 2a 2a 70 70 4d 6f 64 75 6c 65 0d 0a 29 7b 0d 0a  **ppModule..){..
458e5 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 73    *ppModule = &s
458e6 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
458e7 64 75 6c 65 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64  dule;..}....#end
458e8 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
458e9 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
458ea 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
458eb 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0d 0a 0d  ABLE_FTS3) */...
458ec 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
458ed 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b   End of fts3_tok
458ee 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a  enizer1.c ******
458ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
458f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
458f1 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
458f2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
458f3 33 5f 77 72 69 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  3_write.c ******
458f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
458f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
458f6 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32 30 30 39 20 4f  /../*..** 2009 O
458f7 63 74 20 32 33 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ct 23..**..** Th
458f8 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
458f9 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
458fa 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
458fb 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d 0a  .  In place of..
458fc 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
458fd 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
458fe 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  ssing:..**..**  
458ff 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
45900 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0d  d and not evil..
45901 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
45902 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
45903 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
45904 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
45905 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
45906 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
45907 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
45908 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d 0a  than you give...
45909 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **..************
4590a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4590b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4590c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4590d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4590e 2a 2a 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  **..**..** This 
4590f 66 69 6c 65 20 69 73 20 70 61 72 74 20 6f 66 20  file is part of 
45910 74 68 65 20 53 51 4c 69 74 65 20 46 54 53 33 20  the SQLite FTS3 
45911 65 78 74 65 6e 73 69 6f 6e 20 6d 6f 64 75 6c 65  extension module
45912 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 0d  . Specifically,.
45913 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 20 63 6f  .** this file co
45914 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69  ntains code to i
45915 6e 73 65 72 74 2c 20 75 70 64 61 74 65 20 61 6e  nsert, update an
45916 64 20 64 65 6c 65 74 65 20 72 6f 77 73 20 66 72  d delete rows fr
45917 6f 6d 20 46 54 53 33 0d 0a 2a 2a 20 74 61 62 6c  om FTS3..** tabl
45918 65 73 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74  es. It also cont
45919 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 6d 65 72  ains code to mer
4591a 67 65 20 46 54 53 33 20 62 2d 74 72 65 65 20 73  ge FTS3 b-tree s
4591b 65 67 6d 65 6e 74 73 2e 20 53 6f 6d 65 0d 0a 2a  egments. Some..*
4591c 2a 20 6f 66 20 74 68 65 20 73 75 62 2d 72 6f 75  * of the sub-rou
4591d 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 6d 65  tines used to me
4591e 72 67 65 20 73 65 67 6d 65 6e 74 73 20 61 72 65  rge segments are
4591f 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68   also used by th
45920 65 20 71 75 65 72 79 20 0d 0a 2a 2a 20 63 6f 64  e query ..** cod
45921 65 20 69 6e 20 66 74 73 33 2e 63 2e 0d 0a 2a 2f  e in fts3.c...*/
45922 0d 0a 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
45923 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
45924 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
45925 45 4e 41 42 4c 45 5f 46 54 53 33 29 0d 0a 0d 0a  ENABLE_FTS3)....
45926 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72  /* #include <str
45927 69 6e 67 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69  ing.h> */../* #i
45928 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68  nclude <assert.h
45929 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  > */../* #includ
4592a 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0d  e <stdlib.h> */.
4592b 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 57 68 65 6e 20 66  .../*..** When f
4592c 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 6e  ull-text index n
4592d 6f 64 65 73 20 61 72 65 20 6c 6f 61 64 65 64 20  odes are loaded 
4592e 66 72 6f 6d 20 64 69 73 6b 2c 20 74 68 65 20 62  from disk, the b
4592f 75 66 66 65 72 20 74 68 61 74 20 74 68 65 79 0d  uffer that they.
45930 0a 2a 2a 20 61 72 65 20 6c 6f 61 64 65 64 20 69  .** are loaded i
45931 6e 74 6f 20 68 61 73 20 74 68 65 20 66 6f 6c 6c  nto has the foll
45932 6f 77 69 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  owing number of 
45933 62 79 74 65 73 20 6f 66 20 70 61 64 64 69 6e 67  bytes of padding
45934 20 61 74 20 74 68 65 20 65 6e 64 20 0d 0a 2a 2a   at the end ..**
45935 20 6f 66 20 69 74 2e 20 69 2e 65 2e 20 69 66 20   of it. i.e. if 
45936 61 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  a full-text inde
45937 78 20 6e 6f 64 65 20 69 73 20 39 30 30 20 62 79  x node is 900 by
45938 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65  tes in size, the
45939 6e 20 61 20 62 75 66 66 65 72 0d 0a 2a 2a 20 6f  n a buffer..** o
4593a 66 20 39 32 30 20 62 79 74 65 73 20 69 73 20 61  f 920 bytes is a
4593b 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 69 74 2e  llocated for it.
4593c 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 6d 65  ..**..** This me
4593d 61 6e 73 20 74 68 61 74 20 69 66 20 77 65 20 68  ans that if we h
4593e 61 76 65 20 61 20 70 6f 69 6e 74 65 72 20 69 6e  ave a pointer in
4593f 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74  to a buffer cont
45940 61 69 6e 69 6e 67 20 6e 6f 64 65 20 64 61 74 61  aining node data
45941 2c 0d 0a 2a 2a 20 69 74 20 69 73 20 61 6c 77 61  ,..** it is alwa
45942 79 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  ys safe to read 
45943 75 70 20 74 6f 20 74 77 6f 20 76 61 72 69 6e 74  up to two varint
45944 73 20 66 72 6f 6d 20 69 74 20 77 69 74 68 6f 75  s from it withou
45945 74 20 72 69 73 6b 69 6e 67 20 61 6e 0d 0a 2a 2a  t risking an..**
45946 20 6f 76 65 72 72 65 61 64 2c 20 65 76 65 6e 20   overread, even 
45947 69 66 20 74 68 65 20 6e 6f 64 65 20 64 61 74 61  if the node data
45948 20 69 73 20 63 6f 72 72 75 70 74 65 64 2e 0d 0a   is corrupted...
45949 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 33  */..#define FTS3
4594a 5f 4e 4f 44 45 5f 50 41 44 44 49 4e 47 20 28 46  _NODE_PADDING (F
4594b 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 2a 32  TS3_VARINT_MAX*2
4594c 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 55 6e 64 65  )..../*..** Unde
4594d 72 20 63 65 72 74 61 69 6e 20 63 69 72 63 75 6d  r certain circum
4594e 73 74 61 6e 63 65 73 2c 20 62 2d 74 72 65 65 20  stances, b-tree 
4594f 6e 6f 64 65 73 20 28 64 6f 63 6c 69 73 74 73 29  nodes (doclists)
45950 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 69   can be loaded i
45951 6e 74 6f 0d 0a 2a 2a 20 6d 65 6d 6f 72 79 20 69  nto..** memory i
45952 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 69 6e 73  ncrementally ins
45953 74 65 61 64 20 6f 66 20 61 6c 6c 20 61 74 20 6f  tead of all at o
45954 6e 63 65 2e 20 54 68 69 73 20 63 61 6e 20 62 65  nce. This can be
45955 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e   a big performan
45956 63 65 0d 0a 2a 2a 20 77 69 6e 20 28 72 65 64 75  ce..** win (redu
45957 63 65 64 20 49 4f 20 61 6e 64 20 43 50 55 29 20  ced IO and CPU) 
45958 69 66 20 53 51 4c 69 74 65 20 73 74 6f 70 73 20  if SQLite stops 
45959 63 61 6c 6c 69 6e 67 20 74 68 65 20 76 69 72 74  calling the virt
4595a 75 61 6c 20 74 61 62 6c 65 20 78 4e 65 78 74 28  ual table xNext(
4595b 29 0d 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 66  )..** method bef
4595c 6f 72 65 20 72 65 74 72 69 65 76 69 6e 67 20 61  ore retrieving a
4595d 6c 6c 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  ll query results
4595e 20 28 61 73 20 6d 61 79 20 68 61 70 70 65 6e 2c   (as may happen,
4595f 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0d 0a 2a   for example,..*
45960 2a 20 69 66 20 61 20 71 75 65 72 79 20 68 61 73  * if a query has
45961 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 29   a LIMIT clause)
45962 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 6e 63 72 65 6d  ...**..** Increm
45963 65 6e 74 61 6c 20 6c 6f 61 64 69 6e 67 20 69 73  ental loading is
45964 20 75 73 65 64 20 66 6f 72 20 62 2d 74 72 65 65   used for b-tree
45965 20 6e 6f 64 65 73 20 46 54 53 33 5f 4e 4f 44 45   nodes FTS3_NODE
45966 5f 43 48 55 4e 4b 5f 54 48 52 45 53 48 4f 4c 44  _CHUNK_THRESHOLD
45967 20 0d 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20   ..** bytes and 
45968 6c 61 72 67 65 72 2e 20 4e 6f 64 65 73 20 61 72  larger. Nodes ar
45969 65 20 6c 6f 61 64 65 64 20 69 6e 20 63 68 75 6e  e loaded in chun
4596a 6b 73 20 6f 66 20 46 54 53 33 5f 4e 4f 44 45 5f  ks of FTS3_NODE_
4596b 43 48 55 4e 4b 53 49 5a 45 20 62 79 74 65 73 2e  CHUNKSIZE bytes.
4596c 0d 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 73  ..** The code is
4596d 20 77 72 69 74 74 65 6e 20 73 6f 20 74 68 61 74   written so that
4596e 20 74 68 65 20 68 61 72 64 20 6c 6f 77 65 72 2d   the hard lower-
4596f 6c 69 6d 69 74 20 66 6f 72 20 65 61 63 68 20 6f  limit for each o
45970 66 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 0d  f these values .
45971 0a 2a 2a 20 69 73 20 31 2e 20 43 6c 65 61 72 6c  .** is 1. Clearl
45972 79 20 73 75 63 68 20 73 6d 61 6c 6c 20 76 61 6c  y such small val
45973 75 65 73 20 77 6f 75 6c 64 20 62 65 20 69 6e 65  ues would be ine
45974 66 66 69 63 69 65 6e 74 2c 20 62 75 74 20 63 61  fficient, but ca
45975 6e 20 62 65 20 75 73 65 66 75 6c 20 0d 0a 2a 2a  n be useful ..**
45976 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   for testing pur
45977 70 6f 73 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49  poses...**..** I
45978 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  f this module is
45979 20 62 75 69 6c 74 20 77 69 74 68 20 53 51 4c 49   built with SQLI
4597a 54 45 5f 54 45 53 54 20 64 65 66 69 6e 65 64 2c  TE_TEST defined,
4597b 20 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   these constants
4597c 20 6d 61 79 0d 0a 2a 2a 20 62 65 20 6f 76 65 72   may..** be over
4597d 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d  ridden at runtim
4597e 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75  e for testing pu
4597f 72 70 6f 73 65 73 2e 20 46 69 6c 65 20 66 74 73  rposes. File fts
45980 33 5f 74 65 73 74 2e 63 20 63 6f 6e 74 61 69 6e  3_test.c contain
45981 73 0d 0a 2a 2a 20 61 20 54 63 6c 20 69 6e 74 65  s..** a Tcl inte
45982 72 66 61 63 65 20 74 6f 20 72 65 61 64 20 61 6e  rface to read an
45983 64 20 77 72 69 74 65 20 74 68 65 20 76 61 6c 75  d write the valu
45984 65 73 2e 0d 0a 2a 2f 0d 0a 23 69 66 64 65 66 20  es...*/..#ifdef 
45985 53 51 4c 49 54 45 5f 54 45 53 54 0d 0a 69 6e 74  SQLITE_TEST..int
45986 20 74 65 73 74 5f 66 74 73 33 5f 6e 6f 64 65 5f   test_fts3_node_
45987 63 68 75 6e 6b 73 69 7a 65 20 3d 20 28 34 2a 31  chunksize = (4*1
45988 30 32 34 29 3b 0d 0a 69 6e 74 20 74 65 73 74 5f  024);..int test_
45989 66 74 73 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b 5f  fts3_node_chunk_
4598a 74 68 72 65 73 68 6f 6c 64 20 3d 20 28 34 2a 31  threshold = (4*1
4598b 30 32 34 29 2a 34 3b 0d 0a 23 20 64 65 66 69 6e  024)*4;..# defin
4598c 65 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e  e FTS3_NODE_CHUN
4598d 4b 53 49 5a 45 20 20 20 20 20 20 20 74 65 73 74  KSIZE       test
4598e 5f 66 74 73 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b  _fts3_node_chunk
4598f 73 69 7a 65 0d 0a 23 20 64 65 66 69 6e 65 20 46  size..# define F
45990 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b 5f 54  TS3_NODE_CHUNK_T
45991 48 52 45 53 48 4f 4c 44 20 74 65 73 74 5f 66 74  HRESHOLD test_ft
45992 73 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b 5f 74 68  s3_node_chunk_th
45993 72 65 73 68 6f 6c 64 0d 0a 23 65 6c 73 65 0d 0a  reshold..#else..
45994 23 20 64 65 66 69 6e 65 20 46 54 53 33 5f 4e 4f  # define FTS3_NO
45995 44 45 5f 43 48 55 4e 4b 53 49 5a 45 20 28 34 2a  DE_CHUNKSIZE (4*
45996 31 30 32 34 29 20 0d 0a 23 20 64 65 66 69 6e 65  1024) ..# define
45997 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b   FTS3_NODE_CHUNK
45998 5f 54 48 52 45 53 48 4f 4c 44 20 28 46 54 53 33  _THRESHOLD (FTS3
45999 5f 4e 4f 44 45 5f 43 48 55 4e 4b 53 49 5a 45 2a  _NODE_CHUNKSIZE*
4599a 34 29 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 74 79  4)..#endif....ty
4599b 70 65 64 65 66 20 73 74 72 75 63 74 20 50 65 6e  pedef struct Pen
4599c 64 69 6e 67 4c 69 73 74 20 50 65 6e 64 69 6e 67  dingList Pending
4599d 4c 69 73 74 3b 0d 0a 74 79 70 65 64 65 66 20 73  List;..typedef s
4599e 74 72 75 63 74 20 53 65 67 6d 65 6e 74 4e 6f 64  truct SegmentNod
4599f 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65 3b 0d 0a  e SegmentNode;..
459a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
459a1 65 67 6d 65 6e 74 57 72 69 74 65 72 20 53 65 67  egmentWriter Seg
459a2 6d 65 6e 74 57 72 69 74 65 72 3b 0d 0a 0d 0a 2f  mentWriter;..../
459a3 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  *..** An instanc
459a4 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
459a5 6e 67 20 64 61 74 61 20 73 74 72 75 63 74 75 72  ng data structur
459a6 65 20 69 73 20 75 73 65 64 20 74 6f 20 62 75 69  e is used to bui
459a7 6c 64 20 64 6f 63 6c 69 73 74 73 0d 0a 2a 2a 20  ld doclists..** 
459a8 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 2e 20 53  incrementally. S
459a9 65 65 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33  ee function fts3
459aa 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
459ab 64 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  d() for details.
459ac 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 50 65 6e  ..*/..struct Pen
459ad 64 69 6e 67 4c 69 73 74 20 7b 0d 0a 20 20 69 6e  dingList {..  in
459ae 74 20 6e 44 61 74 61 3b 0d 0a 20 20 63 68 61 72  t nData;..  char
459af 20 2a 61 44 61 74 61 3b 0d 0a 20 20 69 6e 74 20   *aData;..  int 
459b0 6e 53 70 61 63 65 3b 0d 0a 20 20 73 71 6c 69 74  nSpace;..  sqlit
459b1 65 33 5f 69 6e 74 36 34 20 69 4c 61 73 74 44 6f  e3_int64 iLastDo
459b2 63 69 64 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  cid;..  sqlite3_
459b3 69 6e 74 36 34 20 69 4c 61 73 74 43 6f 6c 3b 0d  int64 iLastCol;.
459b4 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
459b5 20 69 4c 61 73 74 50 6f 73 3b 0d 0a 7d 3b 0d 0a   iLastPos;..};..
459b6 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 61 63 68 20  ..../*..** Each 
459b7 63 75 72 73 6f 72 20 68 61 73 20 61 20 28 70 6f  cursor has a (po
459b8 73 73 69 62 6c 79 20 65 6d 70 74 79 29 20 6c 69  ssibly empty) li
459b9 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65  nked list of the
459ba 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63   following objec
459bb 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20  ts...*/..struct 
459bc 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65  Fts3DeferredToke
459bd 6e 20 7b 0d 0a 20 20 46 74 73 33 50 68 72 61 73  n {..  Fts3Phras
459be 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20  eToken *pToken; 
459bf 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
459c0 72 20 74 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69  r to correspondi
459c1 6e 67 20 65 78 70 72 20 74 6f 6b 65 6e 20 2a 2f  ng expr token */
459c2 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  ..  int iCol;   
459c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
459c4 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f      /* Column to
459c5 6b 65 6e 20 6d 75 73 74 20 6f 63 63 75 72 20 69  ken must occur i
459c6 6e 20 2a 2f 0d 0a 20 20 46 74 73 33 44 65 66 65  n */..  Fts3Defe
459c7 72 72 65 64 54 6f 6b 65 6e 20 2a 70 4e 65 78 74  rredToken *pNext
459c8 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20  ;       /* Next 
459c9 69 6e 20 6c 69 73 74 20 6f 66 20 64 65 66 65 72  in list of defer
459ca 72 65 64 20 74 6f 6b 65 6e 73 20 2a 2f 0d 0a 20  red tokens */.. 
459cb 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 4c   PendingList *pL
459cc 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
459cd 20 2f 2a 20 44 6f 63 6c 69 73 74 20 69 73 20 61   /* Doclist is a
459ce 73 73 65 6d 62 6c 65 64 20 68 65 72 65 20 2a 2f  ssembled here */
459cf 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ..};..../*..** A
459d0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
459d1 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
459d2 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20  used to iterate 
459d3 74 68 72 6f 75 67 68 20 74 68 65 20 74 65 72 6d  through the term
459d4 73 20 6f 6e 0d 0a 2a 2a 20 61 20 63 6f 6e 74 69  s on..** a conti
459d5 67 75 6f 75 73 20 73 65 74 20 6f 66 20 73 65 67  guous set of seg
459d6 6d 65 6e 74 20 62 2d 74 72 65 65 20 6c 65 61 66  ment b-tree leaf
459d7 20 6e 6f 64 65 73 2e 20 41 6c 74 68 6f 75 67 68   nodes. Although
459d8 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f 66 0d   the details of.
459d9 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
459da 72 65 20 61 72 65 20 6f 6e 6c 79 20 6d 61 6e 69  re are only mani
459db 70 75 6c 61 74 65 64 20 62 79 20 63 6f 64 65 20  pulated by code 
459dc 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 6f 70  in this file, op
459dd 61 71 75 65 20 68 61 6e 64 6c 65 73 0d 0a 2a 2a  aque handles..**
459de 20 6f 66 20 74 79 70 65 20 46 74 73 33 53 65 67   of type Fts3Seg
459df 52 65 61 64 65 72 2a 20 61 72 65 20 61 6c 73 6f  Reader* are also
459e0 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e   used by code in
459e1 20 66 74 73 33 2e 63 20 74 6f 20 69 74 65 72 61   fts3.c to itera
459e2 74 65 20 74 68 72 6f 75 67 68 0d 0a 2a 2a 20 74  te through..** t
459e3 65 72 6d 73 20 77 68 65 6e 20 71 75 65 72 79 69  erms when queryi
459e4 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  ng the full-text
459e5 20 69 6e 64 65 78 2e 20 53 65 65 20 66 75 6e 63   index. See func
459e6 74 69 6f 6e 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  tions:..**..**  
459e7 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
459e8 65 61 64 65 72 4e 65 77 28 29 0d 0a 2a 2a 20 20  eaderNew()..**  
459e9 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
459ea 65 61 64 65 72 46 72 65 65 28 29 0d 0a 2a 2a 20  eaderFree()..** 
459eb 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67    sqlite3Fts3Seg
459ec 52 65 61 64 65 72 49 74 65 72 61 74 65 28 29 0d  ReaderIterate().
459ed 0a 2a 2a 0d 0a 2a 2a 20 4d 65 74 68 6f 64 73 20  .**..** Methods 
459ee 75 73 65 64 20 74 6f 20 6d 61 6e 69 70 75 6c 61  used to manipula
459ef 74 65 20 46 74 73 33 53 65 67 52 65 61 64 65 72  te Fts3SegReader
459f0 20 73 74 72 75 63 74 75 72 65 73 3a 0d 0a 2a 2a   structures:..**
459f1 0d 0a 2a 2a 20 20 20 66 74 73 33 53 65 67 52 65  ..**   fts3SegRe
459f2 61 64 65 72 4e 65 78 74 28 29 0d 0a 2a 2a 20 20  aderNext()..**  
459f3 20 66 74 73 33 53 65 67 52 65 61 64 65 72 46 69   fts3SegReaderFi
459f4 72 73 74 44 6f 63 69 64 28 29 0d 0a 2a 2a 20 20  rstDocid()..**  
459f5 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65   fts3SegReaderNe
459f6 78 74 44 6f 63 69 64 28 29 0d 0a 2a 2f 0d 0a 73  xtDocid()..*/..s
459f7 74 72 75 63 74 20 46 74 73 33 53 65 67 52 65 61  truct Fts3SegRea
459f8 64 65 72 20 7b 0d 0a 20 20 69 6e 74 20 69 49 64  der {..  int iId
459f9 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
459fa 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
459fb 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c 2c 20  x within level, 
459fc 6f 72 20 30 78 37 46 46 46 46 46 46 46 20 66 6f  or 0x7FFFFFFF fo
459fd 72 20 50 54 20 2a 2f 0d 0a 0d 0a 20 20 73 71 6c  r PT */....  sql
459fe 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74 61 72  ite3_int64 iStar
459ff 74 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  tBlock;      /* 
45a00 52 6f 77 69 64 20 6f 66 20 66 69 72 73 74 20 6c  Rowid of first l
45a01 65 61 66 20 62 6c 6f 63 6b 20 74 6f 20 74 72 61  eaf block to tra
45a02 76 65 72 73 65 20 2a 2f 0d 0a 20 20 73 71 6c 69  verse */..  sqli
45a03 74 65 33 5f 69 6e 74 36 34 20 69 4c 65 61 66 45  te3_int64 iLeafE
45a04 6e 64 42 6c 6f 63 6b 3b 20 20 20 20 2f 2a 20 52  ndBlock;    /* R
45a05 6f 77 69 64 20 6f 66 20 66 69 6e 61 6c 20 6c 65  owid of final le
45a06 61 66 20 62 6c 6f 63 6b 20 74 6f 20 74 72 61 76  af block to trav
45a07 65 72 73 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  erse */..  sqlit
45a08 65 33 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  e3_int64 iEndBlo
45a09 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ck;        /* Ro
45a0a 77 69 64 20 6f 66 20 66 69 6e 61 6c 20 62 6c 6f  wid of final blo
45a0b 63 6b 20 69 6e 20 73 65 67 6d 65 6e 74 20 28 6f  ck in segment (o
45a0c 72 20 30 29 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  r 0) */..  sqlit
45a0d 65 33 5f 69 6e 74 36 34 20 69 43 75 72 72 65 6e  e3_int64 iCurren
45a0e 74 42 6c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 75  tBlock;    /* Cu
45a0f 72 72 65 6e 74 20 6c 65 61 66 20 62 6c 6f 63 6b  rrent leaf block
45a10 20 28 6f 72 20 30 29 20 2a 2f 0d 0a 0d 0a 20 20   (or 0) */....  
45a11 63 68 61 72 20 2a 61 4e 6f 64 65 3b 20 20 20 20  char *aNode;    
45a12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a13 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 6f  /* Pointer to no
45a14 64 65 20 64 61 74 61 20 28 6f 72 20 4e 55 4c 4c  de data (or NULL
45a15 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e 6f 64  ) */..  int nNod
45a16 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
45a17 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
45a18 6f 66 20 62 75 66 66 65 72 20 61 74 20 61 4e 6f  of buffer at aNo
45a19 64 65 20 28 6f 72 20 30 29 20 2a 2f 0d 0a 20 20  de (or 0) */..  
45a1a 69 6e 74 20 6e 50 6f 70 75 6c 61 74 65 3b 20 20  int nPopulate;  
45a1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a1c 2f 2a 20 49 66 20 3e 30 2c 20 62 79 74 65 73 20  /* If >0, bytes 
45a1d 6f 66 20 62 75 66 66 65 72 20 61 4e 6f 64 65 5b  of buffer aNode[
45a1e 5d 20 6c 6f 61 64 65 64 20 2a 2f 0d 0a 20 20 73  ] loaded */..  s
45a1f 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c  qlite3_blob *pBl
45a20 6f 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ob;            /
45a21 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 62  * If not NULL, b
45a22 6c 6f 62 20 68 61 6e 64 6c 65 20 74 6f 20 72 65  lob handle to re
45a23 61 64 20 6e 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20  ad node */....  
45a24 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 70  Fts3HashElem **p
45a25 70 4e 65 78 74 45 6c 65 6d 3b 0d 0a 0d 0a 20 20  pNextElem;....  
45a26 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 73 65 74  /* Variables set
45a27 20 62 79 20 66 74 73 33 53 65 67 52 65 61 64 65   by fts3SegReade
45a28 72 4e 65 78 74 28 29 2e 20 54 68 65 73 65 20 6d  rNext(). These m
45a29 61 79 20 62 65 20 72 65 61 64 20 64 69 72 65 63  ay be read direc
45a2a 74 6c 79 0d 0a 20 20 2a 2a 20 62 79 20 74 68 65  tly..  ** by the
45a2b 20 63 61 6c 6c 65 72 2e 20 54 68 65 79 20 61 72   caller. They ar
45a2c 65 20 76 61 6c 69 64 20 66 72 6f 6d 20 74 68 65  e valid from the
45a2d 20 74 69 6d 65 20 53 65 67 6d 65 6e 74 52 65 61   time SegmentRea
45a2e 64 65 72 4e 65 77 28 29 20 72 65 74 75 72 6e 73  derNew() returns
45a2f 0d 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 53 65 67  ..  ** until Seg
45a30 6d 65 6e 74 52 65 61 64 65 72 4e 65 78 74 28 29  mentReaderNext()
45a31 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69   returns somethi
45a32 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  ng other than SQ
45a33 4c 49 54 45 5f 4f 4b 0d 0a 20 20 2a 2a 20 28 69  LITE_OK..  ** (i
45a34 2e 65 2e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 29  .e. SQLITE_DONE)
45a35 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  ...  */..  int n
45a36 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
45a37 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
45a38 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
45a39 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a 2f   current term */
45a3a 0d 0a 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  ..  char *zTerm;
45a3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a3c 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
45a3d 6f 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a  o current term *
45a3e 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 41 6c  /..  int nTermAl
45a3f 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  loc;            
45a40 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
45a41 64 20 73 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20  d size of zTerm 
45a42 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 63 68 61  buffer */..  cha
45a43 72 20 2a 61 44 6f 63 6c 69 73 74 3b 20 20 20 20  r *aDoclist;    
45a44 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45a45 50 6f 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c 69  Pointer to docli
45a46 73 74 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e  st of current en
45a47 74 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 44  try */..  int nD
45a48 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
45a49 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
45a4a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e 20  e of doclist in 
45a4b 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f  current entry */
45a4c 0d 0a 0d 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  ....  /* The fol
45a4d 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
45a4e 20 61 72 65 20 75 73 65 64 20 62 79 20 66 74 73   are used by fts
45a4f 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 44 6f  3SegReaderNextDo
45a50 63 69 64 28 29 20 74 6f 20 69 74 65 72 61 74 65  cid() to iterate
45a51 20 0d 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20   ..  ** through 
45a52 74 68 65 20 63 75 72 72 65 6e 74 20 64 6f 63 6c  the current docl
45a53 69 73 74 20 28 61 44 6f 63 6c 69 73 74 2f 6e 44  ist (aDoclist/nD
45a54 6f 63 6c 69 73 74 29 2e 0d 0a 20 20 2a 2f 0d 0a  oclist)...  */..
45a55 20 20 63 68 61 72 20 2a 70 4f 66 66 73 65 74 4c    char *pOffsetL
45a56 69 73 74 3b 0d 0a 20 20 69 6e 74 20 6e 4f 66 66  ist;..  int nOff
45a57 73 65 74 4c 69 73 74 3b 20 20 20 20 20 20 20 20  setList;        
45a58 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 64          /* For d
45a59 65 73 63 65 6e 64 69 6e 67 20 70 65 6e 64 69 6e  escending pendin
45a5a 67 20 73 65 67 2d 72 65 61 64 65 72 73 20 6f 6e  g seg-readers on
45a5b 6c 79 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  ly */..  sqlite3
45a5c 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b 0d 0a  _int64 iDocid;..
45a5d 7d 3b 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 66 74  };....#define ft
45a5e 73 33 53 65 67 52 65 61 64 65 72 49 73 50 65 6e  s3SegReaderIsPen
45a5f 64 69 6e 67 28 70 29 20 28 28 70 29 2d 3e 70 70  ding(p) ((p)->pp
45a60 4e 65 78 74 45 6c 65 6d 21 3d 30 29 0d 0a 23 64  NextElem!=0)..#d
45a61 65 66 69 6e 65 20 66 74 73 33 53 65 67 52 65 61  efine fts3SegRea
45a62 64 65 72 49 73 52 6f 6f 74 4f 6e 6c 79 28 70 29  derIsRootOnly(p)
45a63 20 28 28 70 29 2d 3e 61 4e 6f 64 65 3d 3d 28 63   ((p)->aNode==(c
45a64 68 61 72 20 2a 29 26 28 70 29 5b 31 5d 29 0d 0a  har *)&(p)[1])..
45a65 0d 0a 2f 2a 0d 0a 2a 2a 20 41 6e 20 69 6e 73 74  ../*..** An inst
45a66 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
45a67 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
45a68 6f 20 63 72 65 61 74 65 20 61 20 73 65 67 6d 65  o create a segme
45a69 6e 74 20 62 2d 74 72 65 65 20 69 6e 20 74 68 65  nt b-tree in the
45a6a 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54  ..** database. T
45a6b 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61  he internal deta
45a6c 69 6c 73 20 6f 66 20 74 68 69 73 20 74 79 70 65  ils of this type
45a6d 20 61 72 65 20 6f 6e 6c 79 20 61 63 63 65 73 73   are only access
45a6e 65 64 20 62 79 20 74 68 65 0d 0a 2a 2a 20 66 6f  ed by the..** fo
45a6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
45a70 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 66 74 73  s:..**..**   fts
45a71 33 53 65 67 57 72 69 74 65 72 41 64 64 28 29 0d  3SegWriterAdd().
45a72 0a 2a 2a 20 20 20 66 74 73 33 53 65 67 57 72 69  .**   fts3SegWri
45a73 74 65 72 46 6c 75 73 68 28 29 0d 0a 2a 2a 20 20  terFlush()..**  
45a74 20 66 74 73 33 53 65 67 57 72 69 74 65 72 46 72   fts3SegWriterFr
45a75 65 65 28 29 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74  ee()..*/..struct
45a76 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 7b   SegmentWriter {
45a77 0d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  ..  SegmentNode 
45a78 2a 70 54 72 65 65 3b 20 20 20 20 20 20 20 20 20  *pTree;         
45a79 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
45a7a 6f 20 69 6e 74 65 72 69 6f 72 20 74 72 65 65 20  o interior tree 
45a7b 73 74 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20  structure */..  
45a7c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 46  sqlite3_int64 iF
45a7d 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
45a7e 2f 2a 20 46 69 72 73 74 20 73 6c 6f 74 20 69 6e  /* First slot in
45a7f 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 72 69 74   %_segments writ
45a80 74 65 6e 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ten */..  sqlite
45a81 33 5f 69 6e 74 36 34 20 69 46 72 65 65 3b 20 20  3_int64 iFree;  
45a82 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
45a83 74 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 25  t free slot in %
45a84 5f 73 65 67 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20  _segments */..  
45a85 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20  char *zTerm;    
45a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a87 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 72  /* Pointer to pr
45a88 65 76 69 6f 75 73 20 74 65 72 6d 20 62 75 66 66  evious term buff
45a89 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 65  er */..  int nTe
45a8a 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
45a8b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
45a8c 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a  er of bytes in z
45a8d 54 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  Term */..  int n
45a8e 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  Malloc;         
45a8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
45a90 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63 27 64 20 62  ze of malloc'd b
45a91 75 66 66 65 72 20 61 74 20 7a 4d 61 6c 6c 6f 63  uffer at zMalloc
45a92 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4d 61   */..  char *zMa
45a93 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
45a94 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63         /* Malloc
45a95 27 64 20 73 70 61 63 65 20 28 70 6f 73 73 69 62  'd space (possib
45a96 6c 79 29 20 75 73 65 64 20 66 6f 72 20 7a 54 65  ly) used for zTe
45a97 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 69  rm */..  int nSi
45a98 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
45a99 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
45a9a 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61   of allocation a
45a9b 74 20 61 44 61 74 61 20 2a 2f 0d 0a 20 20 69 6e  t aData */..  in
45a9c 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  t nData;        
45a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45a9e 20 42 79 74 65 73 20 6f 66 20 64 61 74 61 20 69   Bytes of data i
45a9f 6e 20 61 44 61 74 61 20 2a 2f 0d 0a 20 20 63 68  n aData */..  ch
45aa0 61 72 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20  ar *aData;      
45aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45aa2 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 6c 6f 63   Pointer to bloc
45aa3 6b 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20  k from malloc() 
45aa4 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  */..};..../*..**
45aa5 20 54 79 70 65 20 53 65 67 6d 65 6e 74 4e 6f 64   Type SegmentNod
45aa6 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  e is used by the
45aa7 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65   following three
45aa8 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 63 72   functions to cr
45aa9 65 61 74 65 0d 0a 2a 2a 20 74 68 65 20 69 6e 74  eate..** the int
45aaa 65 72 69 6f 72 20 70 61 72 74 20 6f 66 20 74 68  erior part of th
45aab 65 20 73 65 67 6d 65 6e 74 20 62 2b 2d 74 72 65  e segment b+-tre
45aac 65 20 73 74 72 75 63 74 75 72 65 73 20 28 65 76  e structures (ev
45aad 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 0d  erything except.
45aae 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  .** the leaf nod
45aaf 65 73 29 2e 20 54 68 65 73 65 20 66 75 6e 63 74  es). These funct
45ab0 69 6f 6e 73 20 61 6e 64 20 74 79 70 65 20 61 72  ions and type ar
45ab1 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 64  e only ever used
45ab2 20 62 79 20 63 6f 64 65 0d 0a 2a 2a 20 77 69 74   by code..** wit
45ab3 68 69 6e 20 74 68 65 20 66 74 73 33 53 65 67 57  hin the fts3SegW
45ab4 72 69 74 65 72 58 58 58 28 29 20 66 61 6d 69 6c  riterXXX() famil
45ab5 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 20 64  y of functions d
45ab6 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0d  escribed above..
45ab7 0a 2a 2a 0d 0a 2a 2a 20 20 20 66 74 73 33 4e 6f  .**..**   fts3No
45ab8 64 65 41 64 64 54 65 72 6d 28 29 0d 0a 2a 2a 20  deAddTerm()..** 
45ab9 20 20 66 74 73 33 4e 6f 64 65 57 72 69 74 65 28    fts3NodeWrite(
45aba 29 0d 0a 2a 2a 20 20 20 66 74 73 33 4e 6f 64 65  )..**   fts3Node
45abb 46 72 65 65 28 29 0d 0a 2a 2a 0d 0a 2a 2a 20 57  Free()..**..** W
45abc 68 65 6e 20 61 20 62 2b 74 72 65 65 20 69 73 20  hen a b+tree is 
45abd 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64  written to the d
45abe 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20  atabase (either 
45abf 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  as a result of a
45ac0 20 6d 65 72 67 65 0d 0a 2a 2a 20 6f 72 20 74 68   merge..** or th
45ac1 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20  e pending-terms 
45ac2 74 61 62 6c 65 20 62 65 69 6e 67 20 66 6c 75 73  table being flus
45ac3 68 65 64 29 2c 20 6c 65 61 76 65 73 20 61 72 65  hed), leaves are
45ac4 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
45ac5 65 20 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  e ..** database 
45ac6 66 69 6c 65 20 61 73 20 73 6f 6f 6e 20 61 73 20  file as soon as 
45ac7 74 68 65 79 20 61 72 65 20 63 6f 6d 70 6c 65 74  they are complet
45ac8 65 6c 79 20 70 6f 70 75 6c 61 74 65 64 2e 20 54  ely populated. T
45ac9 68 65 20 69 6e 74 65 72 69 6f 72 20 6f 66 0d 0a  he interior of..
45aca 2a 2a 20 74 68 65 20 74 72 65 65 20 69 73 20 61  ** the tree is a
45acb 73 73 65 6d 62 6c 65 64 20 69 6e 20 6d 65 6d 6f  ssembled in memo
45acc 72 79 20 61 6e 64 20 77 72 69 74 74 65 6e 20 6f  ry and written o
45acd 75 74 20 6f 6e 6c 79 20 6f 6e 63 65 20 61 6c 6c  ut only once all
45ace 20 6c 65 61 76 65 73 20 68 61 76 65 0d 0a 2a 2a   leaves have..**
45acf 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64 20   been populated 
45ad0 61 6e 64 20 73 74 6f 72 65 64 2e 20 54 68 69 73  and stored. This
45ad1 20 69 73 20 4f 6b 2c 20 61 73 20 74 68 65 20 62   is Ok, as the b
45ad2 2b 2d 74 72 65 65 20 66 61 6e 6f 75 74 20 69 73  +-tree fanout is
45ad3 20 75 73 75 61 6c 6c 79 0d 0a 2a 2a 20 76 65 72   usually..** ver
45ad4 79 20 6c 61 72 67 65 2c 20 6d 65 61 6e 69 6e 67  y large, meaning
45ad5 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 69   that the interi
45ad6 6f 72 20 6f 66 20 74 68 65 20 74 72 65 65 20 63  or of the tree c
45ad7 6f 6e 73 75 6d 65 73 20 72 65 6c 61 74 69 76 65  onsumes relative
45ad8 6c 79 20 0d 0a 2a 2a 20 6c 69 74 74 6c 65 20 6d  ly ..** little m
45ad9 65 6d 6f 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  emory...*/..stru
45ada 63 74 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 7b  ct SegmentNode {
45adb 0d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  ..  SegmentNode 
45adc 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20  *pParent;       
45add 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f      /* Parent no
45ade 64 65 20 28 6f 72 20 4e 55 4c 4c 20 66 6f 72 20  de (or NULL for 
45adf 72 6f 6f 74 20 6e 6f 64 65 29 20 2a 2f 0d 0a 20  root node) */.. 
45ae0 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 52   SegmentNode *pR
45ae1 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
45ae2 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 72   /* Pointer to r
45ae3 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 2a 2f 0d  ight-sibling */.
45ae4 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a  .  SegmentNode *
45ae5 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20  pLeftmost;      
45ae6 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
45ae7 20 6c 65 66 74 2d 6d 6f 73 74 20 6e 6f 64 65 20   left-most node 
45ae8 6f 66 20 74 68 69 73 20 64 65 70 74 68 20 2a 2f  of this depth */
45ae9 0d 0a 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20  ..  int nEntry; 
45aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45aeb 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
45aec 20 74 65 72 6d 73 20 77 72 69 74 74 65 6e 20 74   terms written t
45aed 6f 20 6e 6f 64 65 20 73 6f 20 66 61 72 20 2a 2f  o node so far */
45aee 0d 0a 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  ..  char *zTerm;
45aef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45af0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
45af1 6f 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 20  o previous term 
45af2 62 75 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74  buffer */..  int
45af3 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
45af4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45af5 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
45af6 69 6e 20 7a 54 65 72 6d 20 2a 2f 0d 0a 20 20 69  in zTerm */..  i
45af7 6e 74 20 6e 4d 61 6c 6c 6f 63 3b 20 20 20 20 20  nt nMalloc;     
45af8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45af9 2a 20 53 69 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63  * Size of malloc
45afa 27 64 20 62 75 66 66 65 72 20 61 74 20 7a 4d 61  'd buffer at zMa
45afb 6c 6c 6f 63 20 2a 2f 0d 0a 20 20 63 68 61 72 20  lloc */..  char 
45afc 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20  *zMalloc;       
45afd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
45afe 6c 6c 6f 63 27 64 20 73 70 61 63 65 20 28 70 6f  lloc'd space (po
45aff 73 73 69 62 6c 79 29 20 75 73 65 64 20 66 6f 72  ssibly) used for
45b00 20 7a 54 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74   zTerm */..  int
45b01 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20   nData;         
45b02 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45b03 42 79 74 65 73 20 6f 66 20 76 61 6c 69 64 20 64  Bytes of valid d
45b04 61 74 61 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20  ata so far */.. 
45b05 20 63 68 61 72 20 2a 61 44 61 74 61 3b 20 20 20   char *aData;   
45b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45b07 20 2f 2a 20 4e 6f 64 65 20 64 61 74 61 20 2a 2f   /* Node data */
45b08 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 56  ..};..../*..** V
45b09 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20  alid values for 
45b0a 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
45b0b 65 6e 74 20 74 6f 20 66 74 73 33 53 71 6c 53 74  ent to fts3SqlSt
45b0c 6d 74 28 29 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  mt()...*/..#defi
45b0d 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 43 4f  ne SQL_DELETE_CO
45b0e 4e 54 45 4e 54 20 20 20 20 20 20 20 20 20 20 20  NTENT           
45b0f 20 20 30 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c    0..#define SQL
45b10 5f 49 53 5f 45 4d 50 54 59 20 20 20 20 20 20 20  _IS_EMPTY       
45b11 20 20 20 20 20 20 20 20 20 20 20 20 31 0d 0a 23              1..#
45b12 64 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54  define SQL_DELET
45b13 45 5f 41 4c 4c 5f 43 4f 4e 54 45 4e 54 20 20 20  E_ALL_CONTENT   
45b14 20 20 20 20 20 20 32 20 0d 0a 23 64 65 66 69 6e        2 ..#defin
45b15 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c  e SQL_DELETE_ALL
45b16 5f 53 45 47 4d 45 4e 54 53 20 20 20 20 20 20 20  _SEGMENTS       
45b17 20 33 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   3..#define SQL_
45b18 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 45 47 44 49  DELETE_ALL_SEGDI
45b19 52 20 20 20 20 20 20 20 20 20 20 34 0d 0a 23 64  R          4..#d
45b1a 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45  efine SQL_DELETE
45b1b 5f 41 4c 4c 5f 44 4f 43 53 49 5a 45 20 20 20 20  _ALL_DOCSIZE    
45b1c 20 20 20 20 20 35 0d 0a 23 64 65 66 69 6e 65 20       5..#define 
45b1d 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53  SQL_DELETE_ALL_S
45b1e 54 41 54 20 20 20 20 20 20 20 20 20 20 20 20 36  TAT            6
45b1f 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45  ..#define SQL_SE
45b20 4c 45 43 54 5f 43 4f 4e 54 45 4e 54 5f 42 59 5f  LECT_CONTENT_BY_
45b21 52 4f 57 49 44 20 20 20 20 37 0d 0a 23 64 65 66  ROWID    7..#def
45b22 69 6e 65 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47  ine SQL_NEXT_SEG
45b23 4d 45 4e 54 5f 49 4e 44 45 58 20 20 20 20 20 20  MENT_INDEX      
45b24 20 20 20 38 0d 0a 23 64 65 66 69 6e 65 20 53 51     8..#define SQ
45b25 4c 5f 49 4e 53 45 52 54 5f 53 45 47 4d 45 4e 54  L_INSERT_SEGMENT
45b26 53 20 20 20 20 20 20 20 20 20 20 20 20 39 0d 0a  S            9..
45b27 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e 45 58 54  #define SQL_NEXT
45b28 5f 53 45 47 4d 45 4e 54 53 5f 49 44 20 20 20 20  _SEGMENTS_ID    
45b29 20 20 20 20 20 20 31 30 0d 0a 23 64 65 66 69 6e        10..#defin
45b2a 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  e SQL_INSERT_SEG
45b2b 44 49 52 20 20 20 20 20 20 20 20 20 20 20 20 20  DIR             
45b2c 31 31 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f  11..#define SQL_
45b2d 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 20 20 20  SELECT_LEVEL    
45b2e 20 20 20 20 20 20 20 20 20 20 31 32 0d 0a 23 64            12..#d
45b2f 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
45b30 5f 4c 45 56 45 4c 5f 52 41 4e 47 45 20 20 20 20  _LEVEL_RANGE    
45b31 20 20 20 20 31 33 0d 0a 23 64 65 66 69 6e 65 20      13..#define 
45b32 53 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c  SQL_SELECT_LEVEL
45b33 5f 43 4f 55 4e 54 20 20 20 20 20 20 20 20 31 34  _COUNT        14
45b34 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45  ..#define SQL_SE
45b35 4c 45 43 54 5f 53 45 47 44 49 52 5f 4d 41 58 5f  LECT_SEGDIR_MAX_
45b36 4c 45 56 45 4c 20 20 20 31 35 0d 0a 23 64 65 66  LEVEL   15..#def
45b37 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53  ine SQL_DELETE_S
45b38 45 47 44 49 52 5f 4c 45 56 45 4c 20 20 20 20 20  EGDIR_LEVEL     
45b39 20 20 31 36 0d 0a 23 64 65 66 69 6e 65 20 53 51    16..#define SQ
45b3a 4c 5f 44 45 4c 45 54 45 5f 53 45 47 4d 45 4e 54  L_DELETE_SEGMENT
45b3b 53 5f 52 41 4e 47 45 20 20 20 20 20 31 37 0d 0a  S_RANGE     17..
45b3c 23 64 65 66 69 6e 65 20 53 51 4c 5f 43 4f 4e 54  #define SQL_CONT
45b3d 45 4e 54 5f 49 4e 53 45 52 54 20 20 20 20 20 20  ENT_INSERT      
45b3e 20 20 20 20 20 20 31 38 0d 0a 23 64 65 66 69 6e        18..#defin
45b3f 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 44 4f 43  e SQL_DELETE_DOC
45b40 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 20  SIZE            
45b41 31 39 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f  19..#define SQL_
45b42 52 45 50 4c 41 43 45 5f 44 4f 43 53 49 5a 45 20  REPLACE_DOCSIZE 
45b43 20 20 20 20 20 20 20 20 20 20 32 30 0d 0a 23 64            20..#d
45b44 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
45b45 5f 44 4f 43 53 49 5a 45 20 20 20 20 20 20 20 20  _DOCSIZE        
45b46 20 20 20 20 32 31 0d 0a 23 64 65 66 69 6e 65 20      21..#define 
45b47 53 51 4c 5f 53 45 4c 45 43 54 5f 44 4f 43 54 4f  SQL_SELECT_DOCTO
45b48 54 41 4c 20 20 20 20 20 20 20 20 20 20 20 32 32  TAL           22
45b49 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 52 45  ..#define SQL_RE
45b4a 50 4c 41 43 45 5f 44 4f 43 54 4f 54 41 4c 20 20  PLACE_DOCTOTAL  
45b4b 20 20 20 20 20 20 20 20 32 33 0d 0a 0d 0a 23 64          23....#d
45b4c 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
45b4d 5f 41 4c 4c 5f 50 52 45 46 49 58 5f 4c 45 56 45  _ALL_PREFIX_LEVE
45b4e 4c 20 20 20 32 34 0d 0a 23 64 65 66 69 6e 65 20  L   24..#define 
45b4f 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 54  SQL_DELETE_ALL_T
45b50 45 52 4d 53 5f 53 45 47 44 49 52 20 20 20 32 35  ERMS_SEGDIR   25
45b51 0d 0a 0d 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f  ....#define SQL_
45b52 44 45 4c 45 54 45 5f 53 45 47 44 49 52 5f 52 41  DELETE_SEGDIR_RA
45b53 4e 47 45 20 20 20 20 20 20 20 32 36 0d 0a 0d 0a  NGE       26....
45b54 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /*..** This func
45b55 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
45b56 6f 62 74 61 69 6e 20 61 6e 20 53 51 4c 69 74 65  obtain an SQLite
45b57 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
45b58 65 6e 74 20 68 61 6e 64 6c 65 0d 0a 2a 2a 20 66  ent handle..** f
45b59 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  or the statement
45b5a 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
45b5b 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
45b5c 6e 74 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  nt. If successfu
45b5d 6c 2c 0d 0a 2a 2a 20 2a 70 70 20 69 73 20 73 65  l,..** *pp is se
45b5e 74 20 74 6f 20 74 68 65 20 72 65 71 75 65 73 74  t to the request
45b5f 65 64 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  ed statement han
45b60 64 6c 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  dle and SQLITE_O
45b61 4b 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2a 20  K returned...** 
45b62 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51  Otherwise, an SQ
45b63 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
45b64 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
45b65 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 30 2e  *pp is set to 0.
45b66 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 72 67 75  ..**..** If argu
45b67 6d 65 6e 74 20 61 70 56 61 6c 20 69 73 20 6e 6f  ment apVal is no
45b68 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  t NULL, then it 
45b69 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e  must point to an
45b6a 20 61 72 72 61 79 20 77 69 74 68 0d 0a 2a 2a 20   array with..** 
45b6b 61 74 20 6c 65 61 73 74 20 61 73 20 6d 61 6e 79  at least as many
45b6c 20 65 6e 74 72 69 65 73 20 61 73 20 74 68 65 20   entries as the 
45b6d 72 65 71 75 65 73 74 65 64 20 73 74 61 74 65 6d  requested statem
45b6e 65 6e 74 20 68 61 73 20 62 6f 75 6e 64 20 0d 0a  ent has bound ..
45b6f 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e 20 54  ** parameters. T
45b70 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 62 6f  he values are bo
45b71 75 6e 64 20 74 6f 20 74 68 65 20 73 74 61 74 65  und to the state
45b72 6d 65 6e 74 73 20 70 61 72 61 6d 65 74 65 72 73  ments parameters
45b73 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 72 65 74 75   before..** retu
45b74 72 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  rning...*/..stat
45b75 69 63 20 69 6e 74 20 66 74 73 33 53 71 6c 53 74  ic int fts3SqlSt
45b76 6d 74 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65  mt(..  Fts3Table
45b77 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
45b78 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
45b79 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
45b7a 2f 0d 0a 20 20 69 6e 74 20 65 53 74 6d 74 2c 20  /..  int eStmt, 
45b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45b7c 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
45b7d 68 65 20 53 51 4c 5f 58 58 58 20 63 6f 6e 73 74  he SQL_XXX const
45b7e 61 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0d 0a 20  ants above */.. 
45b7f 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
45b80 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
45b81 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
45b82 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  nt handle */..  
45b83 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
45b84 61 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20  apVal           
45b85 2f 2a 20 56 61 6c 75 65 73 20 74 6f 20 62 69 6e  /* Values to bin
45b86 64 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 20 2a  d to statement *
45b87 2f 0d 0a 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 63  /..){..  const c
45b88 68 61 72 20 2a 61 7a 53 71 6c 5b 5d 20 3d 20 7b  har *azSql[] = {
45b89 0d 0a 2f 2a 20 30 20 20 2a 2f 20 20 22 44 45 4c  ../* 0  */  "DEL
45b8a 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ETE FROM %Q.'%q_
45b8b 63 6f 6e 74 65 6e 74 27 20 57 48 45 52 45 20 72  content' WHERE r
45b8c 6f 77 69 64 20 3d 20 3f 22 2c 0d 0a 2f 2a 20 31  owid = ?",../* 1
45b8d 20 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 4e 4f    */  "SELECT NO
45b8e 54 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20  T EXISTS(SELECT 
45b8f 64 6f 63 69 64 20 46 52 4f 4d 20 25 51 2e 27 25  docid FROM %Q.'%
45b90 71 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45 52 45  q_content' WHERE
45b91 20 72 6f 77 69 64 21 3d 3f 29 22 2c 0d 0a 2f 2a   rowid!=?)",../*
45b92 20 32 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20   2  */  "DELETE 
45b93 46 52 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e 74  FROM %Q.'%q_cont
45b94 65 6e 74 27 22 2c 0d 0a 2f 2a 20 33 20 20 2a 2f  ent'",../* 3  */
45b95 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
45b96 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 22  Q.'%q_segments'"
45b97 2c 0d 0a 2f 2a 20 34 20 20 2a 2f 20 20 22 44 45  ,../* 4  */  "DE
45b98 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71  LETE FROM %Q.'%q
45b99 5f 73 65 67 64 69 72 27 22 2c 0d 0a 2f 2a 20 35  _segdir'",../* 5
45b9a 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52    */  "DELETE FR
45b9b 4f 4d 20 25 51 2e 27 25 71 5f 64 6f 63 73 69 7a  OM %Q.'%q_docsiz
45b9c 65 27 22 2c 0d 0a 2f 2a 20 36 20 20 2a 2f 20 20  e'",../* 6  */  
45b9d 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
45b9e 27 25 71 5f 73 74 61 74 27 22 2c 0d 0a 2f 2a 20  '%q_stat'",../* 
45b9f 37 20 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 25  7  */  "SELECT %
45ba0 73 20 57 48 45 52 45 20 72 6f 77 69 64 3d 3f 22  s WHERE rowid=?"
45ba1 2c 0d 0a 2f 2a 20 38 20 20 2a 2f 20 20 22 53 45  ,../* 8  */  "SE
45ba2 4c 45 43 54 20 28 53 45 4c 45 43 54 20 6d 61 78  LECT (SELECT max
45ba3 28 69 64 78 29 20 46 52 4f 4d 20 25 51 2e 27 25  (idx) FROM %Q.'%
45ba4 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45 20  q_segdir' WHERE 
45ba5 6c 65 76 65 6c 20 3d 20 3f 29 20 2b 20 31 22 2c  level = ?) + 1",
45ba6 0d 0a 2f 2a 20 39 20 20 2a 2f 20 20 22 49 4e 53  ../* 9  */  "INS
45ba7 45 52 54 20 49 4e 54 4f 20 25 51 2e 27 25 71 5f  ERT INTO %Q.'%q_
45ba8 73 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69  segments'(blocki
45ba9 64 2c 20 62 6c 6f 63 6b 29 20 56 41 4c 55 45 53  d, block) VALUES
45baa 28 3f 2c 20 3f 29 22 2c 0d 0a 2f 2a 20 31 30 20  (?, ?)",../* 10 
45bab 2a 2f 20 20 22 53 45 4c 45 43 54 20 63 6f 61 6c  */  "SELECT coal
45bac 65 73 63 65 28 28 53 45 4c 45 43 54 20 6d 61 78  esce((SELECT max
45bad 28 62 6c 6f 63 6b 69 64 29 20 46 52 4f 4d 20 25  (blockid) FROM %
45bae 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 29  Q.'%q_segments')
45baf 20 2b 20 31 2c 20 31 29 22 2c 0d 0a 2f 2a 20 31   + 1, 1)",../* 1
45bb0 31 20 2a 2f 20 20 22 49 4e 53 45 52 54 20 49 4e  1 */  "INSERT IN
45bb1 54 4f 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72  TO %Q.'%q_segdir
45bb2 27 20 56 41 4c 55 45 53 28 3f 2c 3f 2c 3f 2c 3f  ' VALUES(?,?,?,?
45bb3 2c 3f 2c 3f 29 22 2c 0d 0a 0d 0a 20 20 20 20 20  ,?,?)",....     
45bb4 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 73       /* Return s
45bb5 65 67 6d 65 6e 74 73 20 69 6e 20 6f 72 64 65 72  egments in order
45bb6 20 66 72 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20   from oldest to 
45bb7 6e 65 77 65 73 74 2e 2a 2f 20 0d 0a 2f 2a 20 31  newest.*/ ../* 1
45bb8 32 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 69 64  2 */  "SELECT id
45bb9 78 2c 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20  x, start_block, 
45bba 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
45bbb 2c 20 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  , end_block, roo
45bbc 74 20 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20  t "..           
45bbd 20 22 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65   "FROM %Q.'%q_se
45bbe 67 64 69 72 27 20 57 48 45 52 45 20 6c 65 76 65  gdir' WHERE leve
45bbf 6c 20 3d 20 3f 20 4f 52 44 45 52 20 42 59 20 69  l = ? ORDER BY i
45bc0 64 78 20 41 53 43 22 2c 0d 0a 2f 2a 20 31 33 20  dx ASC",../* 13 
45bc1 2a 2f 20 20 22 53 45 4c 45 43 54 20 69 64 78 2c  */  "SELECT idx,
45bc2 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65   start_block, le
45bc3 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20  aves_end_block, 
45bc4 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20  end_block, root 
45bc5 22 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  "..            "
45bc6 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64  FROM %Q.'%q_segd
45bc7 69 72 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20  ir' WHERE level 
45bc8 42 45 54 57 45 45 4e 20 3f 20 41 4e 44 20 3f 22  BETWEEN ? AND ?"
45bc9 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 4f  ..            "O
45bca 52 44 45 52 20 42 59 20 6c 65 76 65 6c 20 44 45  RDER BY level DE
45bcb 53 43 2c 20 69 64 78 20 41 53 43 22 2c 0d 0a 0d  SC, idx ASC",...
45bcc 0a 2f 2a 20 31 34 20 2a 2f 20 20 22 53 45 4c 45  ./* 14 */  "SELE
45bcd 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
45bce 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20   %Q.'%q_segdir' 
45bcf 57 48 45 52 45 20 6c 65 76 65 6c 20 3d 20 3f 22  WHERE level = ?"
45bd0 2c 0d 0a 2f 2a 20 31 35 20 2a 2f 20 20 22 53 45  ,../* 15 */  "SE
45bd1 4c 45 43 54 20 6d 61 78 28 6c 65 76 65 6c 29 20  LECT max(level) 
45bd2 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64  FROM %Q.'%q_segd
45bd3 69 72 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20  ir' WHERE level 
45bd4 42 45 54 57 45 45 4e 20 3f 20 41 4e 44 20 3f 22  BETWEEN ? AND ?"
45bd5 2c 0d 0a 0d 0a 2f 2a 20 31 36 20 2a 2f 20 20 22  ,..../* 16 */  "
45bd6 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27  DELETE FROM %Q.'
45bd7 25 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45  %q_segdir' WHERE
45bd8 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0d 0a 2f 2a   level = ?",../*
45bd9 20 31 37 20 2a 2f 20 20 22 44 45 4c 45 54 45 20   17 */  "DELETE 
45bda 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d  FROM %Q.'%q_segm
45bdb 65 6e 74 73 27 20 57 48 45 52 45 20 62 6c 6f 63  ents' WHERE bloc
45bdc 6b 69 64 20 42 45 54 57 45 45 4e 20 3f 20 41 4e  kid BETWEEN ? AN
45bdd 44 20 3f 22 2c 0d 0a 2f 2a 20 31 38 20 2a 2f 20  D ?",../* 18 */ 
45bde 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
45bdf 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 56 41  .'%q_content' VA
45be0 4c 55 45 53 28 25 73 29 22 2c 0d 0a 2f 2a 20 31  LUES(%s)",../* 1
45be1 39 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52  9 */  "DELETE FR
45be2 4f 4d 20 25 51 2e 27 25 71 5f 64 6f 63 73 69 7a  OM %Q.'%q_docsiz
45be3 65 27 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d  e' WHERE docid =
45be4 20 3f 22 2c 0d 0a 2f 2a 20 32 30 20 2a 2f 20 20   ?",../* 20 */  
45be5 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 25 51  "REPLACE INTO %Q
45be6 2e 27 25 71 5f 64 6f 63 73 69 7a 65 27 20 56 41  .'%q_docsize' VA
45be7 4c 55 45 53 28 3f 2c 3f 29 22 2c 0d 0a 2f 2a 20  LUES(?,?)",../* 
45be8 32 31 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 73  21 */  "SELECT s
45be9 69 7a 65 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ize FROM %Q.'%q_
45bea 64 6f 63 73 69 7a 65 27 20 57 48 45 52 45 20 64  docsize' WHERE d
45beb 6f 63 69 64 3d 3f 22 2c 0d 0a 2f 2a 20 32 32 20  ocid=?",../* 22 
45bec 2a 2f 20 20 22 53 45 4c 45 43 54 20 76 61 6c 75  */  "SELECT valu
45bed 65 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 74  e FROM %Q.'%q_st
45bee 61 74 27 20 57 48 45 52 45 20 69 64 3d 30 22 2c  at' WHERE id=0",
45bef 0d 0a 2f 2a 20 32 33 20 2a 2f 20 20 22 52 45 50  ../* 23 */  "REP
45bf0 4c 41 43 45 20 49 4e 54 4f 20 25 51 2e 27 25 71  LACE INTO %Q.'%q
45bf1 5f 73 74 61 74 27 20 56 41 4c 55 45 53 28 30 2c  _stat' VALUES(0,
45bf2 3f 29 22 2c 0d 0a 2f 2a 20 32 34 20 2a 2f 20 20  ?)",../* 24 */  
45bf3 22 22 2c 0d 0a 2f 2a 20 32 35 20 2a 2f 20 20 22  "",../* 25 */  "
45bf4 22 2c 0d 0a 0d 0a 2f 2a 20 32 36 20 2a 2f 20 22  ",..../* 26 */ "
45bf5 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27  DELETE FROM %Q.'
45bf6 25 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45  %q_segdir' WHERE
45bf7 20 6c 65 76 65 6c 20 42 45 54 57 45 45 4e 20 3f   level BETWEEN ?
45bf8 20 41 4e 44 20 3f 22 2c 0d 0a 0d 0a 20 20 7d 3b   AND ?",....  };
45bf9 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
45bfa 49 54 45 5f 4f 4b 3b 0d 0a 20 20 73 71 6c 69 74  ITE_OK;..  sqlit
45bfb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0d  e3_stmt *pStmt;.
45bfc 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 53 69 7a  ...  assert( Siz
45bfd 65 6f 66 41 72 72 61 79 28 61 7a 53 71 6c 29 3d  eofArray(azSql)=
45bfe 3d 53 69 7a 65 6f 66 41 72 72 61 79 28 70 2d 3e  =SizeofArray(p->
45bff 61 53 74 6d 74 29 20 29 3b 0d 0a 20 20 61 73 73  aStmt) );..  ass
45c00 65 72 74 28 20 65 53 74 6d 74 3c 53 69 7a 65 6f  ert( eStmt<Sizeo
45c01 66 41 72 72 61 79 28 61 7a 53 71 6c 29 20 26 26  fArray(azSql) &&
45c02 20 65 53 74 6d 74 3e 3d 30 20 29 3b 0d 0a 20 20   eStmt>=0 );..  
45c03 0d 0a 20 20 70 53 74 6d 74 20 3d 20 70 2d 3e 61  ..  pStmt = p->a
45c04 53 74 6d 74 5b 65 53 74 6d 74 5d 3b 0d 0a 20 20  Stmt[eStmt];..  
45c05 69 66 28 20 21 70 53 74 6d 74 20 29 7b 0d 0a 20  if( !pStmt ){.. 
45c06 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0d 0a     char *zSql;..
45c07 20 20 20 20 69 66 28 20 65 53 74 6d 74 3d 3d 53      if( eStmt==S
45c08 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  QL_CONTENT_INSER
45c09 54 20 29 7b 0d 0a 20 20 20 20 20 20 7a 53 71 6c  T ){..      zSql
45c0a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
45c0b 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74 5d 2c  tf(azSql[eStmt],
45c0c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
45c0d 65 2c 20 70 2d 3e 7a 57 72 69 74 65 45 78 70 72  e, p->zWriteExpr
45c0e 6c 69 73 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73  list);..    }els
45c0f 65 20 69 66 28 20 65 53 74 6d 74 3d 3d 53 51 4c  e if( eStmt==SQL
45c10 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54 5f  _SELECT_CONTENT_
45c11 42 59 5f 52 4f 57 49 44 20 29 7b 0d 0a 20 20 20  BY_ROWID ){..   
45c12 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65     zSql = sqlite
45c13 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b  3_mprintf(azSql[
45c14 65 53 74 6d 74 5d 2c 20 70 2d 3e 7a 52 65 61 64  eStmt], p->zRead
45c15 45 78 70 72 6c 69 73 74 29 3b 0d 0a 20 20 20 20  Exprlist);..    
45c16 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 7a 53  }else{..      zS
45c17 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
45c18 69 6e 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74  intf(azSql[eStmt
45c19 5d 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e  ], p->zDb, p->zN
45c1a 61 6d 65 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  ame);..    }..  
45c1b 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0d 0a    if( !zSql ){..
45c1c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
45c1d 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 65  E_NOMEM;..    }e
45c1e 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  lse{..      rc =
45c1f 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
45c20 5f 76 32 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  _v2(p->db, zSql,
45c21 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 4e 55 4c   -1, &pStmt, NUL
45c22 4c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  L);..      sqlit
45c23 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d 0a  e3_free(zSql);..
45c24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
45c25 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
45c26 53 74 6d 74 3d 3d 30 20 29 3b 0d 0a 20 20 20 20  Stmt==0 );..    
45c27 20 20 70 2d 3e 61 53 74 6d 74 5b 65 53 74 6d 74    p->aStmt[eStmt
45c28 5d 20 3d 20 70 53 74 6d 74 3b 0d 0a 20 20 20 20  ] = pStmt;..    
45c29 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 61 70  }..  }..  if( ap
45c2a 56 61 6c 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  Val ){..    int 
45c2b 69 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 50 61 72  i;..    int nPar
45c2c 61 6d 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  am = sqlite3_bin
45c2d 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
45c2e 74 28 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20 66  t(pStmt);..    f
45c2f 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
45c30 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 50 61 72 61  TE_OK && i<nPara
45c31 6d 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  m; i++){..      
45c32 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
45c33 64 5f 76 61 6c 75 65 28 70 53 74 6d 74 2c 20 69  d_value(pStmt, i
45c34 2b 31 2c 20 61 70 56 61 6c 5b 69 5d 29 3b 0d 0a  +1, apVal[i]);..
45c35 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 2a 70      }..  }..  *p
45c36 70 20 3d 20 70 53 74 6d 74 3b 0d 0a 20 20 72 65  p = pStmt;..  re
45c37 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 73  turn rc;..}....s
45c38 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
45c39 6c 65 63 74 44 6f 63 73 69 7a 65 28 0d 0a 20 20  lectDocsize(..  
45c3a 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c  Fts3Table *pTab,
45c3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c3c 2f 2a 20 46 54 53 33 20 74 61 62 6c 65 20 68 61  /* FTS3 table ha
45c3d 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 65  ndle */..  int e
45c3e 53 74 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  Stmt,           
45c3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69             /* Ei
45c40 74 68 65 72 20 53 51 4c 5f 53 45 4c 45 43 54 5f  ther SQL_SELECT_
45c41 44 4f 43 53 49 5a 45 20 6f 72 20 44 4f 43 54 4f  DOCSIZE or DOCTO
45c42 54 41 4c 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  TAL */..  sqlite
45c43 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 20  3_int64 iDocid, 
45c44 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63            /* Doc
45c45 69 64 20 74 6f 20 62 69 6e 64 20 66 6f 72 20 53  id to bind for S
45c46 51 4c 5f 53 45 4c 45 43 54 5f 44 4f 43 53 49 5a  QL_SELECT_DOCSIZ
45c47 45 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  E */..  sqlite3_
45c48 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 20 20 20  stmt **ppStmt   
45c49 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
45c4a 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  Statement handle
45c4b 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74   */..){..  sqlit
45c4c 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d  e3_stmt *pStmt =
45c4d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74   0;        /* St
45c4e 61 74 65 6d 65 6e 74 20 72 65 71 75 65 73 74 65  atement requeste
45c4f 64 20 66 72 6f 6d 20 66 74 73 33 53 71 6c 53 74  d from fts3SqlSt
45c50 6d 74 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  mt() */..  int r
45c51 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
45c52 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
45c53 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a  turn code */....
45c54 20 20 61 73 73 65 72 74 28 20 65 53 74 6d 74 3d    assert( eStmt=
45c55 3d 53 51 4c 5f 53 45 4c 45 43 54 5f 44 4f 43 53  =SQL_SELECT_DOCS
45c56 49 5a 45 20 7c 7c 20 65 53 74 6d 74 3d 3d 53 51  IZE || eStmt==SQ
45c57 4c 5f 53 45 4c 45 43 54 5f 44 4f 43 54 4f 54 41  L_SELECT_DOCTOTA
45c58 4c 20 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 66  L );....  rc = f
45c59 74 73 33 53 71 6c 53 74 6d 74 28 70 54 61 62 2c  ts3SqlStmt(pTab,
45c5a 20 65 53 74 6d 74 2c 20 26 70 53 74 6d 74 2c 20   eStmt, &pStmt, 
45c5b 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  0);..  if( rc==S
45c5c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
45c5d 20 69 66 28 20 65 53 74 6d 74 3d 3d 53 51 4c 5f   if( eStmt==SQL_
45c5e 53 45 4c 45 43 54 5f 44 4f 43 53 49 5a 45 20 29  SELECT_DOCSIZE )
45c5f 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
45c60 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d  _bind_int64(pStm
45c61 74 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0d 0a  t, 1, iDocid);..
45c62 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
45c63 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
45c64 6d 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63  mt);..    if( rc
45c65 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 7c 7c 20  !=SQLITE_ROW || 
45c66 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
45c67 79 70 65 28 70 53 74 6d 74 2c 20 30 29 21 3d 53  ype(pStmt, 0)!=S
45c68 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0d 0a 20  QLITE_BLOB ){.. 
45c69 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
45c6a 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0d  3_reset(pStmt);.
45c6b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
45c6c 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20  QLITE_OK ) rc = 
45c6d 46 54 53 5f 43 4f 52 52 55 50 54 5f 56 54 41 42  FTS_CORRUPT_VTAB
45c6e 3b 0d 0a 20 20 20 20 20 20 70 53 74 6d 74 20 3d  ;..      pStmt =
45c6f 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d   0;..    }else{.
45c70 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
45c71 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  TE_OK;..    }.. 
45c72 20 7d 0d 0a 0d 0a 20 20 2a 70 70 53 74 6d 74 20   }....  *ppStmt 
45c73 3d 20 70 53 74 6d 74 3b 0d 0a 20 20 72 65 74 75  = pStmt;..  retu
45c74 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c  rn rc;..}....SQL
45c75 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
45c76 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c 65 63  sqlite3Fts3Selec
45c77 74 44 6f 63 74 6f 74 61 6c 28 0d 0a 20 20 46 74  tDoctotal(..  Ft
45c78 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20  s3Table *pTab,  
45c79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45c7a 20 46 74 73 33 20 74 61 62 6c 65 20 68 61 6e 64   Fts3 table hand
45c7b 6c 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  le */..  sqlite3
45c7c 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 20 20  _stmt **ppStmt  
45c7d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
45c7e 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
45c7f 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 72 65 74 75  e */..){..  retu
45c80 72 6e 20 66 74 73 33 53 65 6c 65 63 74 44 6f 63  rn fts3SelectDoc
45c81 73 69 7a 65 28 70 54 61 62 2c 20 53 51 4c 5f 53  size(pTab, SQL_S
45c82 45 4c 45 43 54 5f 44 4f 43 54 4f 54 41 4c 2c 20  ELECT_DOCTOTAL, 
45c83 30 2c 20 70 70 53 74 6d 74 29 3b 0d 0a 7d 0d 0a  0, ppStmt);..}..
45c84 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
45c85 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
45c86 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 28 0d 0a  SelectDocsize(..
45c87 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61    Fts3Table *pTa
45c88 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
45c89 20 20 2f 2a 20 46 74 73 33 20 74 61 62 6c 65 20    /* Fts3 table 
45c8a 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 73 71 6c  handle */..  sql
45c8b 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69  ite3_int64 iDoci
45c8c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
45c8d 44 6f 63 69 64 20 74 6f 20 72 65 61 64 20 73 69  Docid to read si
45c8e 7a 65 20 64 61 74 61 20 66 6f 72 20 2a 2f 0d 0a  ze data for */..
45c8f 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
45c90 2a 70 70 53 74 6d 74 20 20 20 20 20 20 20 20 20  *ppStmt         
45c91 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d    /* OUT: Statem
45c92 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 29  ent handle */..)
45c93 7b 0d 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33  {..  return fts3
45c94 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 28 70 54  SelectDocsize(pT
45c95 61 62 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 44  ab, SQL_SELECT_D
45c96 4f 43 53 49 5a 45 2c 20 69 44 6f 63 69 64 2c 20  OCSIZE, iDocid, 
45c97 70 70 53 74 6d 74 29 3b 0d 0a 7d 0d 0a 0d 0a 2f  ppStmt);..}..../
45c98 2a 0d 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 74 6f  *..** Similar to
45c99 20 66 74 73 33 53 71 6c 53 74 6d 74 28 29 2e 20   fts3SqlStmt(). 
45c9a 45 78 63 65 70 74 2c 20 61 66 74 65 72 20 62 69  Except, after bi
45c9b 6e 64 69 6e 67 20 74 68 65 20 70 61 72 61 6d 65  nding the parame
45c9c 74 65 72 73 20 69 6e 0d 0a 2a 2a 20 61 72 72 61  ters in..** arra
45c9d 79 20 61 70 56 61 6c 5b 5d 20 74 6f 20 74 68 65  y apVal[] to the
45c9e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
45c9f 64 65 6e 74 69 66 69 65 64 20 62 79 20 65 53 74  dentified by eSt
45ca0 6d 74 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e  mt, the statemen
45ca1 74 0d 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65  t..** is execute
45ca2 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72  d...**..** Retur
45ca3 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  ns SQLITE_OK if 
45ca4 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
45ca5 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78   successfully ex
45ca6 65 63 75 74 65 64 2c 20 6f 72 20 61 6e 0d 0a 2a  ecuted, or an..*
45ca7 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  * SQLite error c
45ca8 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0d 0a  ode otherwise...
45ca9 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
45caa 66 74 73 33 53 71 6c 45 78 65 63 28 0d 0a 20 20  fts3SqlExec(..  
45cab 69 6e 74 20 2a 70 52 43 2c 20 20 20 20 20 20 20  int *pRC,       
45cac 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
45cad 6c 74 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 46 74  lt code */..  Ft
45cae 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20  s3Table *p,     
45caf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 54         /* The FT
45cb0 53 33 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 69  S3 table */..  i
45cb1 6e 74 20 65 53 74 6d 74 2c 20 20 20 20 20 20 20  nt eStmt,       
45cb2 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
45cb3 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 74 6f   of statement to
45cb4 20 65 76 61 6c 75 61 74 65 20 2a 2f 0d 0a 20 20   evaluate */..  
45cb5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
45cb6 61 70 56 61 6c 20 20 20 20 2f 2a 20 50 61 72 61  apVal    /* Para
45cb7 6d 65 74 65 72 73 20 74 6f 20 62 69 6e 64 20 2a  meters to bind *
45cb8 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  /..){..  sqlite3
45cb9 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0d 0a 20  _stmt *pStmt;.. 
45cba 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 66 28 20   int rc;..  if( 
45cbb 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0d 0a  *pRC ) return;..
45cbc 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74    rc = fts3SqlSt
45cbd 6d 74 28 70 2c 20 65 53 74 6d 74 2c 20 26 70 53  mt(p, eStmt, &pS
45cbe 74 6d 74 2c 20 61 70 56 61 6c 29 3b 20 0d 0a 20  tmt, apVal); .. 
45cbf 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
45cc0 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  OK ){..    sqlit
45cc1 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0d  e3_step(pStmt);.
45cc2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
45cc3 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0d  3_reset(pStmt);.
45cc4 0a 20 20 7d 0d 0a 20 20 2a 70 52 43 20 3d 20 72  .  }..  *pRC = r
45cc5 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  c;..}....../*..*
45cc6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
45cc7 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65  ensures that the
45cc8 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61   caller has obta
45cc9 69 6e 65 64 20 61 20 73 68 61 72 65 64 2d 63 61  ined a shared-ca
45cca 63 68 65 0d 0a 2a 2a 20 74 61 62 6c 65 2d 6c 6f  che..** table-lo
45ccb 63 6b 20 6f 6e 20 74 68 65 20 25 5f 63 6f 6e 74  ck on the %_cont
45ccc 65 6e 74 20 74 61 62 6c 65 2e 20 54 68 69 73 20  ent table. This 
45ccd 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  is required befo
45cce 72 65 20 72 65 61 64 69 6e 67 0d 0a 2a 2a 20 64  re reading..** d
45ccf 61 74 61 20 66 72 6f 6d 20 74 68 65 20 66 74 73  ata from the fts
45cd0 33 20 74 61 62 6c 65 2e 20 49 66 20 74 68 69 73  3 table. If this
45cd1 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 71   lock is not acq
45cd2 75 69 72 65 64 20 66 69 72 73 74 2c 20 74 68 65  uired first, the
45cd3 6e 0d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  n..** the caller
45cd4 20 6d 61 79 20 65 6e 64 20 75 70 20 68 6f 6c 64   may end up hold
45cd5 69 6e 67 20 72 65 61 64 2d 6c 6f 63 6b 73 20 6f  ing read-locks o
45cd6 6e 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73  n the %_segments
45cd7 20 61 6e 64 20 25 5f 73 65 67 64 69 72 0d 0a 2a   and %_segdir..*
45cd8 2a 20 74 61 62 6c 65 73 2c 20 62 75 74 20 6e 6f  * tables, but no
45cd9 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
45cda 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
45cdb 65 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65  e. If this happe
45cdc 6e 73 20 0d 0a 2a 2a 20 61 20 73 65 63 6f 6e 64  ns ..** a second
45cdd 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
45cde 20 62 65 20 61 62 6c 65 20 74 6f 20 77 72 69 74   be able to writ
45cdf 65 20 74 6f 20 74 68 65 20 66 74 73 33 20 74 61  e to the fts3 ta
45ce0 62 6c 65 2c 20 62 75 74 0d 0a 2a 2a 20 61 74 74  ble, but..** att
45ce1 65 6d 70 74 69 6e 67 20 74 6f 20 63 6f 6d 6d 69  empting to commi
45ce2 74 20 74 68 6f 73 65 20 77 72 69 74 65 73 20 6d  t those writes m
45ce3 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49  ight return SQLI
45ce4 54 45 5f 4c 4f 43 4b 45 44 20 6f 72 0d 0a 2a 2a  TE_LOCKED or..**
45ce5 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
45ce6 48 41 52 45 44 43 41 43 48 45 20 28 62 65 63 61  HAREDCACHE (beca
45ce7 75 73 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 61  use the commit a
45ce8 74 74 65 6d 70 74 73 20 74 6f 20 6f 62 74 61 69  ttempts to obtai
45ce9 6e 20 0d 0a 2a 2a 20 77 72 69 74 65 2d 6c 6f 63  n ..** write-loc
45cea 6b 73 20 6f 6e 20 74 68 65 20 25 5f 73 65 67 6d  ks on the %_segm
45ceb 65 6e 74 73 20 61 6e 64 20 25 5f 73 65 67 64 69  ents and %_segdi
45cec 72 20 2a 2a 20 74 61 62 6c 65 73 29 2e 20 0d 0a  r ** tables). ..
45ced 2a 2a 0d 0a 2a 2a 20 57 65 20 74 72 79 20 74 6f  **..** We try to
45cee 20 61 76 6f 69 64 20 74 68 69 73 20 62 65 63 61   avoid this beca
45cef 75 73 65 20 69 66 20 46 54 53 33 20 72 65 74 75  use if FTS3 retu
45cf0 72 6e 73 20 61 6e 79 20 65 72 72 6f 72 20 77 68  rns any error wh
45cf1 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 0d 0a 2a  en committing..*
45cf2 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  * a transaction,
45cf3 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73   the whole trans
45cf4 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72  action will be r
45cf5 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 41 6e 64 20  olled back. And 
45cf6 74 68 69 73 20 69 73 0d 0a 2a 2a 20 6e 6f 74 20  this is..** not 
45cf7 77 68 61 74 20 75 73 65 72 73 20 65 78 70 65 63  what users expec
45cf8 74 20 77 68 65 6e 20 74 68 65 79 20 67 65 74 20  t when they get 
45cf9 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
45cfa 41 52 45 44 43 41 43 48 45 2e 20 49 74 20 63 61  AREDCACHE. It ca
45cfb 6e 0d 0a 2a 2a 20 73 74 69 6c 6c 20 68 61 70 70  n..** still happ
45cfc 65 6e 20 69 66 20 74 68 65 20 75 73 65 72 20 72  en if the user r
45cfd 65 61 64 73 20 64 61 74 61 20 64 69 72 65 63 74  eads data direct
45cfe 6c 79 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65  ly from the %_se
45cff 67 6d 65 6e 74 73 20 6f 72 0d 0a 2a 2a 20 25 5f  gments or..** %_
45d00 73 65 67 64 69 72 20 74 61 62 6c 65 73 20 69 6e  segdir tables in
45d01 73 74 65 61 64 20 6f 66 20 67 6f 69 6e 67 20 74  stead of going t
45d02 68 72 6f 75 67 68 20 46 54 53 33 20 74 68 6f 75  hrough FTS3 thou
45d03 67 68 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73  gh...**..** This
45d04 20 72 65 61 73 6f 6e 69 6e 67 20 64 6f 65 73 20   reasoning does 
45d05 6e 6f 74 20 61 70 70 6c 79 20 74 6f 20 61 20 63  not apply to a c
45d06 6f 6e 74 65 6e 74 3d 78 78 78 20 74 61 62 6c 65  ontent=xxx table
45d07 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
45d08 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
45d09 33 46 74 73 33 52 65 61 64 4c 6f 63 6b 28 46 74  3Fts3ReadLock(Ft
45d0a 73 33 54 61 62 6c 65 20 2a 70 29 7b 0d 0a 20 20  s3Table *p){..  
45d0b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
45d0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45d0d 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
45d0e 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  /..  sqlite3_stm
45d0f 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20  t *pStmt;       
45d10 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e       /* Statemen
45d11 74 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  t used to obtain
45d12 20 6c 6f 63 6b 20 2a 2f 0d 0a 0d 0a 20 20 69 66   lock */....  if
45d13 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c  ( p->zContentTbl
45d14 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ==0 ){..    rc =
45d15 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
45d16 53 51 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45  SQL_SELECT_CONTE
45d17 4e 54 5f 42 59 5f 52 4f 57 49 44 2c 20 26 70 53  NT_BY_ROWID, &pS
45d18 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66  tmt, 0);..    if
45d19 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
45d1a 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){..      sqlite
45d1b 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d  3_bind_null(pStm
45d1c 74 2c 20 31 29 3b 0d 0a 20 20 20 20 20 20 73 71  t, 1);..      sq
45d1d 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
45d1e 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73  );..      rc = s
45d1f 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
45d20 6d 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  mt);..    }..  }
45d21 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20  else{..    rc = 
45d22 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d  SQLITE_OK;..  }.
45d23 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
45d24 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74  .}..../*..** Set
45d25 20 2a 70 70 53 74 6d 74 20 74 6f 20 61 20 73 74   *ppStmt to a st
45d26 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 74  atement handle t
45d27 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20  hat may be used 
45d28 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  to iterate throu
45d29 67 68 0d 0a 2a 2a 20 61 6c 6c 20 72 6f 77 73 20  gh..** all rows 
45d2a 69 6e 20 74 68 65 20 25 5f 73 65 67 64 69 72 20  in the %_segdir 
45d2b 74 61 62 6c 65 2c 20 66 72 6f 6d 20 6f 6c 64 65  table, from olde
45d2c 73 74 20 74 6f 20 6e 65 77 65 73 74 2e 20 49 66  st to newest. If
45d2d 20 73 75 63 63 65 73 73 66 75 6c 2c 0d 0a 2a 2a   successful,..**
45d2e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
45d2f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  K. If an error o
45d30 63 63 75 72 73 20 77 68 69 6c 65 20 70 72 65 70  ccurs while prep
45d31 61 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d  aring the statem
45d32 65 6e 74 2c 20 0d 0a 2a 2a 20 72 65 74 75 72 6e  ent, ..** return
45d33 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
45d34 20 63 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54   code...**..** T
45d35 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 65  here is only eve
45d36 72 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f  r one instance o
45d37 66 20 74 68 69 73 20 53 51 4c 20 73 74 61 74 65  f this SQL state
45d38 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 64 20 66 6f  ment compiled fo
45d39 72 0d 0a 2a 2a 20 65 61 63 68 20 46 54 53 33 20  r..** each FTS3 
45d3a 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  table...**..** T
45d3b 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  he statement ret
45d3c 75 72 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  urns the followi
45d3d 6e 67 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20  ng columns from 
45d3e 74 68 65 20 25 5f 73 65 67 64 69 72 20 74 61 62  the %_segdir tab
45d3f 6c 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 30 3a  le:..**..**   0:
45d40 20 69 64 78 0d 0a 2a 2a 20 20 20 31 3a 20 73 74   idx..**   1: st
45d41 61 72 74 5f 62 6c 6f 63 6b 0d 0a 2a 2a 20 20 20  art_block..**   
45d42 32 3a 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  2: leaves_end_bl
45d43 6f 63 6b 0d 0a 2a 2a 20 20 20 33 3a 20 65 6e 64  ock..**   3: end
45d44 5f 62 6c 6f 63 6b 0d 0a 2a 2a 20 20 20 34 3a 20  _block..**   4: 
45d45 72 6f 6f 74 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  root..*/..SQLITE
45d46 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
45d47 69 74 65 33 46 74 73 33 41 6c 6c 53 65 67 64 69  ite3Fts3AllSegdi
45d48 72 73 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65  rs(..  Fts3Table
45d49 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
45d4a 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20 74         /* FTS3 t
45d4b 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  able */..  int i
45d4c 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  Index,          
45d4d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
45d4e 64 65 78 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65  dex for p->aInde
45d4f 78 5b 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c  x[] */..  int iL
45d50 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20  evel,           
45d51 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76            /* Lev
45d52 65 6c 20 74 6f 20 73 65 6c 65 63 74 20 2a 2f 0d  el to select */.
45d53 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
45d54 2a 2a 70 70 53 74 6d 74 20 20 20 20 20 20 20 20  **ppStmt        
45d55 20 20 20 2f 2a 20 4f 55 54 3a 20 43 6f 6d 70 69     /* OUT: Compi
45d56 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  led statement */
45d57 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  ..){..  int rc;.
45d58 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
45d59 2a 70 53 74 6d 74 20 3d 20 30 3b 0d 0a 0d 0a 20  *pStmt = 0;.... 
45d5a 20 61 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3d   assert( iLevel=
45d5b 3d 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f  =FTS3_SEGCURSOR_
45d5c 41 4c 4c 20 7c 7c 20 69 4c 65 76 65 6c 3e 3d 30  ALL || iLevel>=0
45d5d 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69   );..  assert( i
45d5e 4c 65 76 65 6c 3c 46 54 53 33 5f 53 45 47 44 49  Level<FTS3_SEGDI
45d5f 52 5f 4d 41 58 4c 45 56 45 4c 20 29 3b 0d 0a 20  R_MAXLEVEL );.. 
45d60 20 61 73 73 65 72 74 28 20 69 49 6e 64 65 78 3e   assert( iIndex>
45d61 3d 30 20 26 26 20 69 49 6e 64 65 78 3c 70 2d 3e  =0 && iIndex<p->
45d62 6e 49 6e 64 65 78 20 29 3b 0d 0a 0d 0a 20 20 69  nIndex );....  i
45d63 66 28 20 69 4c 65 76 65 6c 3c 30 20 29 7b 0d 0a  f( iLevel<0 ){..
45d64 20 20 20 20 2f 2a 20 22 53 45 4c 45 43 54 20 2a      /* "SELECT *
45d65 20 46 52 4f 4d 20 25 5f 73 65 67 64 69 72 20 57   FROM %_segdir W
45d66 48 45 52 45 20 6c 65 76 65 6c 20 42 45 54 57 45  HERE level BETWE
45d67 45 4e 20 3f 20 41 4e 44 20 3f 20 4f 52 44 45 52  EN ? AND ? ORDER
45d68 20 42 59 20 2e 2e 2e 22 20 2a 2f 0d 0a 20 20 20   BY ..." */..   
45d69 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d   rc = fts3SqlStm
45d6a 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f  t(p, SQL_SELECT_
45d6b 4c 45 56 45 4c 5f 52 41 4e 47 45 2c 20 26 70 53  LEVEL_RANGE, &pS
45d6c 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20 20 69 66  tmt, 0);..    if
45d6d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
45d6e 29 7b 20 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  ){ ..      sqlit
45d6f 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d  e3_bind_int(pStm
45d70 74 2c 20 31 2c 20 69 49 6e 64 65 78 2a 46 54 53  t, 1, iIndex*FTS
45d71 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45 56 45  3_SEGDIR_MAXLEVE
45d72 4c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  L);..      sqlit
45d73 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d  e3_bind_int(pStm
45d74 74 2c 20 32 2c 20 28 69 49 6e 64 65 78 2b 31 29  t, 2, (iIndex+1)
45d75 2a 46 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58  *FTS3_SEGDIR_MAX
45d76 4c 45 56 45 4c 2d 31 29 3b 0d 0a 20 20 20 20 7d  LEVEL-1);..    }
45d77 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
45d78 2f 2a 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f  /* "SELECT * FRO
45d79 4d 20 25 5f 73 65 67 64 69 72 20 57 48 45 52 45  M %_segdir WHERE
45d7a 20 6c 65 76 65 6c 20 3d 20 3f 20 4f 52 44 45 52   level = ? ORDER
45d7b 20 42 59 20 2e 2e 2e 22 20 2a 2f 0d 0a 20 20 20   BY ..." */..   
45d7c 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d   rc = fts3SqlStm
45d7d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f  t(p, SQL_SELECT_
45d7e 4c 45 56 45 4c 2c 20 26 70 53 74 6d 74 2c 20 30  LEVEL, &pStmt, 0
45d7f 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  );..    if( rc==
45d80 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0d 0a 20  SQLITE_OK ){ .. 
45d81 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
45d82 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20  d_int(pStmt, 1, 
45d83 69 4c 65 76 65 6c 2b 69 49 6e 64 65 78 2a 46 54  iLevel+iIndex*FT
45d84 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45 56  S3_SEGDIR_MAXLEV
45d85 45 4c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  EL);..    }..  }
45d86 0d 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 70 53  ..  *ppStmt = pS
45d87 74 6d 74 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  tmt;..  return r
45d88 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  c;..}....../*..*
45d89 2a 20 41 70 70 65 6e 64 20 61 20 73 69 6e 67 6c  * Append a singl
45d8a 65 20 76 61 72 69 6e 74 20 74 6f 20 61 20 50 65  e varint to a Pe
45d8b 6e 64 69 6e 67 4c 69 73 74 20 62 75 66 66 65 72  ndingList buffer
45d8c 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  . SQLITE_OK is r
45d8d 65 74 75 72 6e 65 64 0d 0a 2a 2a 20 69 66 20 73  eturned..** if s
45d8e 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e  uccessful, or an
45d8f 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
45d90 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0d 0a 2a  de otherwise...*
45d91 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
45d92 69 6f 6e 20 61 6c 73 6f 20 73 65 72 76 65 73 20  ion also serves 
45d93 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  to allocate the 
45d94 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75  PendingList stru
45d95 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0d 0a 2a  cture itself...*
45d96 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  * For example, t
45d97 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 50  o create a new P
45d98 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75 63  endingList struc
45d99 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
45d9a 74 77 6f 0d 0a 2a 2a 20 76 61 72 69 6e 74 73 3a  two..** varints:
45d9b 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 50 65 6e 64 69  ..**..**   Pendi
45d9c 6e 67 4c 69 73 74 20 2a 70 20 3d 20 30 3b 0d 0a  ngList *p = 0;..
45d9d 2a 2a 20 20 20 66 74 73 33 50 65 6e 64 69 6e 67  **   fts3Pending
45d9e 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74  ListAppendVarint
45d9f 28 26 70 2c 20 31 29 3b 0d 0a 2a 2a 20 20 20 66  (&p, 1);..**   f
45da0 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70  ts3PendingListAp
45da1 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 32  pendVarint(&p, 2
45da2 29 3b 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  );..*/..static i
45da3 6e 74 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69  nt fts3PendingLi
45da4 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74 28 0d  stAppendVarint(.
45da5 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a  .  PendingList *
45da6 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pp,            
45da7 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 6f     /* IN/OUT: Po
45da8 69 6e 74 65 72 20 74 6f 20 50 65 6e 64 69 6e 67  inter to Pending
45da9 4c 69 73 74 20 73 74 72 75 63 74 20 2a 2f 0d 0a  List struct */..
45daa 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
45dab 69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i               
45dac 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 61 70    /* Value to ap
45dad 70 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0d  pend to data */.
45dae 0a 29 7b 0d 0a 20 20 50 65 6e 64 69 6e 67 4c 69  .){..  PendingLi
45daf 73 74 20 2a 70 20 3d 20 2a 70 70 3b 0d 0a 0d 0a  st *p = *pp;....
45db0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72    /* Allocate or
45db1 20 67 72 6f 77 20 74 68 65 20 50 65 6e 64 69 6e   grow the Pendin
45db2 67 4c 69 73 74 20 61 73 20 72 65 71 75 69 72 65  gList as require
45db3 64 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 21 70 20  d. */..  if( !p 
45db4 29 7b 0d 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  ){..    p = sqli
45db5 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
45db6 66 28 2a 70 29 20 2b 20 31 30 30 29 3b 0d 0a 20  f(*p) + 100);.. 
45db7 20 20 20 69 66 28 20 21 70 20 29 7b 0d 0a 20 20     if( !p ){..  
45db8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
45db9 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d  E_NOMEM;..    }.
45dba 0a 20 20 20 20 70 2d 3e 6e 53 70 61 63 65 20 3d  .    p->nSpace =
45dbb 20 31 30 30 3b 0d 0a 20 20 20 20 70 2d 3e 61 44   100;..    p->aD
45dbc 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 26 70  ata = (char *)&p
45dbd 5b 31 5d 3b 0d 0a 20 20 20 20 70 2d 3e 6e 44 61  [1];..    p->nDa
45dbe 74 61 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 20 20  ta = 0;..  }..  
45dbf 65 6c 73 65 20 69 66 28 20 70 2d 3e 6e 44 61 74  else if( p->nDat
45dc0 61 2b 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41  a+FTS3_VARINT_MA
45dc1 58 2b 31 3e 70 2d 3e 6e 53 70 61 63 65 20 29 7b  X+1>p->nSpace ){
45dc2 0d 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d  ..    int nNew =
45dc3 20 70 2d 3e 6e 53 70 61 63 65 20 2a 20 32 3b 0d   p->nSpace * 2;.
45dc4 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
45dc5 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 73 69 7a 65  _realloc(p, size
45dc6 6f 66 28 2a 70 29 20 2b 20 6e 4e 65 77 29 3b 0d  of(*p) + nNew);.
45dc7 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0d 0a  .    if( !p ){..
45dc8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
45dc9 65 65 28 2a 70 70 29 3b 0d 0a 20 20 20 20 20 20  ee(*pp);..      
45dca 2a 70 70 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  *pp = 0;..      
45dcb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
45dcc 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  MEM;..    }..   
45dcd 20 70 2d 3e 6e 53 70 61 63 65 20 3d 20 6e 4e 65   p->nSpace = nNe
45dce 77 3b 0d 0a 20 20 20 20 70 2d 3e 61 44 61 74 61  w;..    p->aData
45dcf 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31 5d   = (char *)&p[1]
45dd0 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41  ;..  }....  /* A
45dd1 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 65  ppend the new se
45dd2 72 69 61 6c 69 7a 65 64 20 76 61 72 69 6e 74 20  rialized varint 
45dd3 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
45dd4 65 20 6c 69 73 74 2e 20 2a 2f 0d 0a 20 20 70 2d  e list. */..  p-
45dd5 3e 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65  >nData += sqlite
45dd6 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
45dd7 70 2d 3e 61 44 61 74 61 5b 70 2d 3e 6e 44 61 74  p->aData[p->nDat
45dd8 61 5d 2c 20 69 29 3b 0d 0a 20 20 70 2d 3e 61 44  a], i);..  p->aD
45dd9 61 74 61 5b 70 2d 3e 6e 44 61 74 61 5d 20 3d 20  ata[p->nData] = 
45dda 27 5c 30 27 3b 0d 0a 20 20 2a 70 70 20 3d 20 70  '\0';..  *pp = p
45ddb 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
45ddc 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  TE_OK;..}..../*.
45ddd 0a 2a 2a 20 41 64 64 20 61 20 64 6f 63 69 64 2f  .** Add a docid/
45dde 63 6f 6c 75 6d 6e 2f 70 6f 73 69 74 69 6f 6e 20  column/position 
45ddf 65 6e 74 72 79 20 74 6f 20 61 20 50 65 6e 64 69  entry to a Pendi
45de0 6e 67 4c 69 73 74 20 73 74 72 75 63 74 75 72 65  ngList structure
45de1 2e 20 4e 6f 6e 2d 7a 65 72 6f 0d 0a 2a 2a 20 69  . Non-zero..** i
45de2 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
45de3 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73  e structure is s
45de4 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 65 64  qlite3_realloced
45de5 20 61 73 20 70 61 72 74 20 6f 66 20 61 64 64 69   as part of addi
45de6 6e 67 0d 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79  ng..** the entry
45de7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72  . Otherwise, zer
45de8 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 61 6e  o...**..** If an
45de9 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
45dea 73 2c 20 2a 70 52 63 20 69 73 20 73 65 74 20 74  s, *pRc is set t
45deb 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 62  o SQLITE_NOMEM b
45dec 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
45ded 0d 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 61 6c 77  ..** Zero is alw
45dee 61 79 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20  ays returned in 
45def 74 68 69 73 20 63 61 73 65 2e 20 4f 74 68 65 72  this case. Other
45df0 77 69 73 65 2c 20 69 66 20 6e 6f 20 4f 4f 4d 20  wise, if no OOM 
45df1 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0d 0a 2a  error occurs,..*
45df2 2a 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 53  * it is set to S
45df3 51 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2f 0d 0a 73  QLITE_OK...*/..s
45df4 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 65  tatic int fts3Pe
45df5 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 28  ndingListAppend(
45df6 0d 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  ..  PendingList 
45df7 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20  **pp,           
45df8 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50      /* IN/OUT: P
45df9 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75 63  endingList struc
45dfa 74 75 72 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74  ture */..  sqlit
45dfb 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c  e3_int64 iDocid,
45dfc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
45dfd 63 69 64 20 66 6f 72 20 65 6e 74 72 79 20 74 6f  cid for entry to
45dfe 20 61 64 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74   add */..  sqlit
45dff 65 33 5f 69 6e 74 36 34 20 69 43 6f 6c 2c 20 20  e3_int64 iCol,  
45e00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
45e01 6c 75 6d 6e 20 66 6f 72 20 65 6e 74 72 79 20 74  lumn for entry t
45e02 6f 20 61 64 64 20 2a 2f 0d 0a 20 20 73 71 6c 69  o add */..  sqli
45e03 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73 2c 20  te3_int64 iPos, 
45e04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
45e05 6f 73 69 74 69 6f 6e 20 6f 66 20 74 65 72 6d 20  osition of term 
45e06 66 6f 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64  for entry to add
45e07 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 52 63 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 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
45e0a 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 29  eturn code */..)
45e0b 7b 0d 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74  {..  PendingList
45e0c 20 2a 70 20 3d 20 2a 70 70 3b 0d 0a 20 20 69 6e   *p = *pp;..  in
45e0d 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
45e0e 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 21  ;....  assert( !
45e0f 70 20 7c 7c 20 70 2d 3e 69 4c 61 73 74 44 6f 63  p || p->iLastDoc
45e10 69 64 3c 3d 69 44 6f 63 69 64 20 29 3b 0d 0a 0d  id<=iDocid );...
45e11 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 70 2d 3e  .  if( !p || p->
45e12 69 4c 61 73 74 44 6f 63 69 64 21 3d 69 44 6f 63  iLastDocid!=iDoc
45e13 69 64 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  id ){..    sqlit
45e14 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74 61 20  e3_int64 iDelta 
45e15 3d 20 69 44 6f 63 69 64 20 2d 20 28 70 20 3f 20  = iDocid - (p ? 
45e16 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 20 3a 20  p->iLastDocid : 
45e17 30 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 20 29  0);..    if( p )
45e18 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  {..      assert(
45e19 20 70 2d 3e 6e 44 61 74 61 3c 70 2d 3e 6e 53 70   p->nData<p->nSp
45e1a 61 63 65 20 29 3b 0d 0a 20 20 20 20 20 20 61 73  ace );..      as
45e1b 73 65 72 74 28 20 70 2d 3e 61 44 61 74 61 5b 70  sert( p->aData[p
45e1c 2d 3e 6e 44 61 74 61 5d 3d 3d 30 20 29 3b 0d 0a  ->nData]==0 );..
45e1d 20 20 20 20 20 20 70 2d 3e 6e 44 61 74 61 2b 2b        p->nData++
45e1e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
45e1f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
45e20 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69   = fts3PendingLi
45e21 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26  stAppendVarint(&
45e22 70 2c 20 69 44 65 6c 74 61 29 29 20 29 7b 0d 0a  p, iDelta)) ){..
45e23 20 20 20 20 20 20 67 6f 74 6f 20 70 65 6e 64 69        goto pendi
45e24 6e 67 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74  nglistappend_out
45e25 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 2d  ;..    }..    p-
45e26 3e 69 4c 61 73 74 43 6f 6c 20 3d 20 2d 31 3b 0d  >iLastCol = -1;.
45e27 0a 20 20 20 20 70 2d 3e 69 4c 61 73 74 50 6f 73  .    p->iLastPos
45e28 20 3d 20 30 3b 0d 0a 20 20 20 20 70 2d 3e 69 4c   = 0;..    p->iL
45e29 61 73 74 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  astDocid = iDoci
45e2a 64 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 69  d;..  }..  if( i
45e2b 43 6f 6c 3e 30 20 26 26 20 70 2d 3e 69 4c 61 73  Col>0 && p->iLas
45e2c 74 43 6f 6c 21 3d 69 43 6f 6c 20 29 7b 0d 0a 20  tCol!=iCol ){.. 
45e2d 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
45e2e 21 3d 28 72 63 20 3d 20 66 74 73 33 50 65 6e 64  !=(rc = fts3Pend
45e2f 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72  ingListAppendVar
45e30 69 6e 74 28 26 70 2c 20 31 29 29 0d 0a 20 20 20  int(&p, 1))..   
45e31 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
45e32 28 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e  (rc = fts3Pendin
45e33 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69 6e  gListAppendVarin
45e34 74 28 26 70 2c 20 69 43 6f 6c 29 29 0d 0a 20 20  t(&p, iCol))..  
45e35 20 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f    ){..      goto
45e36 20 70 65 6e 64 69 6e 67 6c 69 73 74 61 70 70 65   pendinglistappe
45e37 6e 64 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a  nd_out;..    }..
45e38 20 20 20 20 70 2d 3e 69 4c 61 73 74 43 6f 6c 20      p->iLastCol 
45e39 3d 20 69 43 6f 6c 3b 0d 0a 20 20 20 20 70 2d 3e  = iCol;..    p->
45e3a 69 4c 61 73 74 50 6f 73 20 3d 20 30 3b 0d 0a 20  iLastPos = 0;.. 
45e3b 20 7d 0d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d   }..  if( iCol>=
45e3c 30 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  0 ){..    assert
45e3d 28 20 69 50 6f 73 3e 70 2d 3e 69 4c 61 73 74 50  ( iPos>p->iLastP
45e3e 6f 73 20 7c 7c 20 28 69 50 6f 73 3d 3d 30 20 26  os || (iPos==0 &
45e3f 26 20 70 2d 3e 69 4c 61 73 74 50 6f 73 3d 3d 30  & p->iLastPos==0
45e40 29 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 66  ) );..    rc = f
45e41 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70  ts3PendingListAp
45e42 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 32  pendVarint(&p, 2
45e43 2b 69 50 6f 73 2d 70 2d 3e 69 4c 61 73 74 50 6f  +iPos-p->iLastPo
45e44 73 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  s);..    if( rc=
45e45 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
45e46 20 20 20 20 20 70 2d 3e 69 4c 61 73 74 50 6f 73       p->iLastPos
45e47 20 3d 20 69 50 6f 73 3b 0d 0a 20 20 20 20 7d 0d   = iPos;..    }.
45e48 0a 20 20 7d 0d 0a 0d 0a 20 70 65 6e 64 69 6e 67  .  }.... pending
45e49 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74 3a 0d  listappend_out:.
45e4a 0a 20 20 2a 70 52 63 20 3d 20 72 63 3b 0d 0a 20  .  *pRc = rc;.. 
45e4b 20 69 66 28 20 70 21 3d 2a 70 70 20 29 7b 0d 0a   if( p!=*pp ){..
45e4c 20 20 20 20 2a 70 70 20 3d 20 70 3b 0d 0a 20 20      *pp = p;..  
45e4d 20 20 72 65 74 75 72 6e 20 31 3b 0d 0a 20 20 7d    return 1;..  }
45e4e 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d  ..  return 0;..}
45e4f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20  ..../*..** Free 
45e50 61 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 6f 62  a PendingList ob
45e51 6a 65 63 74 20 61 6c 6c 6f 63 61 74 65 64 20 62  ject allocated b
45e52 79 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73  y fts3PendingLis
45e53 74 41 70 70 65 6e 64 28 29 2e 0d 0a 2a 2f 0d 0a  tAppend()...*/..
45e54 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
45e55 50 65 6e 64 69 6e 67 4c 69 73 74 44 65 6c 65 74  PendingListDelet
45e56 65 28 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70  e(PendingList *p
45e57 4c 69 73 74 29 7b 0d 0a 20 20 73 71 6c 69 74 65  List){..  sqlite
45e58 33 5f 66 72 65 65 28 70 4c 69 73 74 29 3b 0d 0a  3_free(pList);..
45e59 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20  }..../*..** Add 
45e5a 61 6e 20 65 6e 74 72 79 20 74 6f 20 6f 6e 65 20  an entry to one 
45e5b 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74  of the pending-t
45e5c 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 73  erms hash tables
45e5d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
45e5e 74 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72  t fts3PendingTer
45e5f 6d 73 41 64 64 4f 6e 65 28 0d 0a 20 20 46 74 73  msAddOne(..  Fts
45e60 33 54 61 62 6c 65 20 2a 70 2c 0d 0a 20 20 69 6e  3Table *p,..  in
45e61 74 20 69 43 6f 6c 2c 0d 0a 20 20 69 6e 74 20 69  t iCol,..  int i
45e62 50 6f 73 2c 0d 0a 20 20 46 74 73 33 48 61 73 68  Pos,..  Fts3Hash
45e63 20 2a 70 48 61 73 68 2c 20 20 20 20 20 20 20 20   *pHash,        
45e64 20 20 20 20 20 20 20 20 2f 2a 20 50 65 6e 64 69          /* Pendi
45e65 6e 67 20 74 65 72 6d 73 20 68 61 73 68 20 74 61  ng terms hash ta
45e66 62 6c 65 20 74 6f 20 61 64 64 20 65 6e 74 72 79  ble to add entry
45e67 20 74 6f 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20   to */..  const 
45e68 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 2c 0d 0a 20  char *zToken,.. 
45e69 20 69 6e 74 20 6e 54 6f 6b 65 6e 0d 0a 29 7b 0d   int nToken..){.
45e6a 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a  .  PendingList *
45e6b 70 4c 69 73 74 3b 0d 0a 20 20 69 6e 74 20 72 63  pList;..  int rc
45e6c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d   = SQLITE_OK;...
45e6d 0a 20 20 70 4c 69 73 74 20 3d 20 28 50 65 6e 64  .  pList = (Pend
45e6e 69 6e 67 4c 69 73 74 20 2a 29 66 74 73 33 48 61  ingList *)fts3Ha
45e6f 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 54  shFind(pHash, zT
45e70 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0d 0a  oken, nToken);..
45e71 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0d 0a    if( pList ){..
45e72 20 20 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44      p->nPendingD
45e73 61 74 61 20 2d 3d 20 28 70 4c 69 73 74 2d 3e 6e  ata -= (pList->n
45e74 44 61 74 61 20 2b 20 6e 54 6f 6b 65 6e 20 2b 20  Data + nToken + 
45e75 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68 45  sizeof(Fts3HashE
45e76 6c 65 6d 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69  lem));..  }..  i
45e77 66 28 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69  f( fts3PendingLi
45e78 73 74 41 70 70 65 6e 64 28 26 70 4c 69 73 74 2c  stAppend(&pList,
45e79 20 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20   p->iPrevDocid, 
45e7a 69 43 6f 6c 2c 20 69 50 6f 73 2c 20 26 72 63 29  iCol, iPos, &rc)
45e7b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 70 4c 69   ){..    if( pLi
45e7c 73 74 3d 3d 66 74 73 33 48 61 73 68 49 6e 73 65  st==fts3HashInse
45e7d 72 74 28 70 48 61 73 68 2c 20 7a 54 6f 6b 65 6e  rt(pHash, zToken
45e7e 2c 20 6e 54 6f 6b 65 6e 2c 20 70 4c 69 73 74 29  , nToken, pList)
45e7f 20 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 4d 61   ){..      /* Ma
45e80 6c 6c 6f 63 20 66 61 69 6c 65 64 20 77 68 69 6c  lloc failed whil
45e81 65 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20  e inserting the 
45e82 6e 65 77 20 65 6e 74 72 79 2e 20 54 68 69 73 20  new entry. This 
45e83 63 61 6e 20 6f 6e 6c 79 20 0d 0a 20 20 20 20 20  can only ..     
45e84 20 2a 2a 20 68 61 70 70 65 6e 20 69 66 20 74 68   ** happen if th
45e85 65 72 65 20 77 61 73 20 6e 6f 20 70 72 65 76 69  ere was no previ
45e86 6f 75 73 20 65 6e 74 72 79 20 66 6f 72 20 74 68  ous entry for th
45e87 69 73 20 74 6f 6b 65 6e 2e 0d 0a 20 20 20 20 20  is token...     
45e88 20 2a 2f 0d 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
45e89 74 28 20 30 3d 3d 66 74 73 33 48 61 73 68 46 69  t( 0==fts3HashFi
45e8a 6e 64 28 70 48 61 73 68 2c 20 7a 54 6f 6b 65 6e  nd(pHash, zToken
45e8b 2c 20 6e 54 6f 6b 65 6e 29 20 29 3b 0d 0a 20 20  , nToken) );..  
45e8c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
45e8d 28 70 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20  (pList);..      
45e8e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
45e8f 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  M;..    }..  }..
45e90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
45e91 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 70 2d 3e 6e  _OK ){..    p->n
45e92 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d 20 28  PendingData += (
45e93 70 4c 69 73 74 2d 3e 6e 44 61 74 61 20 2b 20 6e  pList->nData + n
45e94 54 6f 6b 65 6e 20 2b 20 73 69 7a 65 6f 66 28 46  Token + sizeof(F
45e95 74 73 33 48 61 73 68 45 6c 65 6d 29 29 3b 0d 0a  ts3HashElem));..
45e96 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
45e97 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
45e98 6f 6b 65 6e 69 7a 65 20 74 68 65 20 6e 75 6c 2d  okenize the nul-
45e99 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
45e9a 67 20 7a 54 65 78 74 20 61 6e 64 20 61 64 64 20  g zText and add 
45e9b 61 6c 6c 20 74 6f 6b 65 6e 73 20 74 6f 20 74 68  all tokens to th
45e9c 65 0d 0a 2a 2a 20 70 65 6e 64 69 6e 67 2d 74 65  e..** pending-te
45e9d 72 6d 73 20 68 61 73 68 2d 74 61 62 6c 65 2e 20  rms hash-table. 
45e9e 54 68 65 20 64 6f 63 69 64 20 75 73 65 64 20 69  The docid used i
45e9f 73 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  s that currently
45ea0 20 73 74 6f 72 65 64 20 69 6e 0d 0a 2a 2a 20 70   stored in..** p
45ea1 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20 61 6e  ->iPrevDocid, an
45ea2 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  d the column is 
45ea3 73 70 65 63 69 66 69 65 64 20 62 79 20 61 72 67  specified by arg
45ea4 75 6d 65 6e 74 20 69 43 6f 6c 2e 0d 0a 2a 2a 0d  ument iCol...**.
45ea5 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
45ea6 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
45ea7 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
45ea8 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
45ea9 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a  rror code...*/..
45eaa 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50  static int fts3P
45eab 65 6e 64 69 6e 67 54 65 72 6d 73 41 64 64 28 0d  endingTermsAdd(.
45eac 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
45ead 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45eae 20 20 20 2f 2a 20 54 61 62 6c 65 20 69 6e 74 6f     /* Table into
45eaf 20 77 68 69 63 68 20 74 65 78 74 20 77 69 6c 6c   which text will
45eb0 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0d   be inserted */.
45eb1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
45eb2 54 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Text,           
45eb3 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 64 6f     /* Text of do
45eb4 63 75 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73  cument to be ins
45eb5 65 72 74 65 64 20 2a 2f 0d 0a 20 20 69 6e 74 20  erted */..  int 
45eb6 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  iCol,           
45eb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
45eb8 6f 6c 75 6d 6e 20 69 6e 74 6f 20 77 68 69 63 68  olumn into which
45eb9 20 74 65 78 74 20 69 73 20 62 65 69 6e 67 20 69   text is being i
45eba 6e 73 65 72 74 65 64 20 2a 2f 0d 0a 20 20 75 33  nserted */..  u3
45ebb 32 20 2a 70 6e 57 6f 72 64 20 20 20 20 20 20 20  2 *pnWord       
45ebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45ebd 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
45ebe 74 6f 6b 65 6e 73 20 69 6e 73 65 72 74 65 64 20  tokens inserted 
45ebf 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
45ec0 3b 0d 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b  ;..  int iStart;
45ec1 0d 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0d 0a 20  ..  int iEnd;.. 
45ec2 20 69 6e 74 20 69 50 6f 73 3b 0d 0a 20 20 69 6e   int iPos;..  in
45ec3 74 20 6e 57 6f 72 64 20 3d 20 30 3b 0d 0a 0d 0a  t nWord = 0;....
45ec4 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
45ec5 6f 6b 65 6e 3b 0d 0a 20 20 69 6e 74 20 6e 54 6f  oken;..  int nTo
45ec6 6b 65 6e 3b 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  ken;....  sqlite
45ec7 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
45ec8 6b 65 6e 69 7a 65 72 20 3d 20 70 2d 3e 70 54 6f  kenizer = p->pTo
45ec9 6b 65 6e 69 7a 65 72 3b 0d 0a 20 20 73 71 6c 69  kenizer;..  sqli
45eca 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
45ecb 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64  dule const *pMod
45ecc 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  ule = pTokenizer
45ecd 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a 20 20 73 71  ->pModule;..  sq
45ece 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
45ecf 63 75 72 73 6f 72 20 2a 70 43 73 72 3b 0d 0a 20  cursor *pCsr;.. 
45ed0 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71   int (*xNext)(sq
45ed1 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
45ed2 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
45ed3 0d 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ..      const ch
45ed4 61 72 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  ar**,int*,int*,i
45ed5 6e 74 2a 2c 69 6e 74 2a 29 3b 0d 0a 0d 0a 20 20  nt*,int*);....  
45ed6 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 69 7a  assert( pTokeniz
45ed7 65 72 20 26 26 20 70 4d 6f 64 75 6c 65 20 29 3b  er && pModule );
45ed8 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
45ed9 75 73 65 72 20 68 61 73 20 69 6e 73 65 72 74 65  user has inserte
45eda 64 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20  d a NULL value, 
45edb 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  this function ma
45edc 79 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  y be called with
45edd 0d 0a 20 20 2a 2a 20 7a 54 65 78 74 3d 3d 30 2e  ..  ** zText==0.
45ede 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61   In this case, a
45edf 64 64 20 7a 65 72 6f 20 74 6f 6b 65 6e 20 65 6e  dd zero token en
45ee0 74 72 69 65 73 20 74 6f 20 74 68 65 20 68 61 73  tries to the has
45ee1 68 20 74 61 62 6c 65 20 61 6e 64 20 0d 0a 20 20  h table and ..  
45ee2 2a 2a 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e  ** return early.
45ee3 20 2a 2f 0d 0a 20 20 69 66 28 20 7a 54 65 78 74   */..  if( zText
45ee4 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a 70 6e 57  ==0 ){..    *pnW
45ee5 6f 72 64 20 3d 20 30 3b 0d 0a 20 20 20 20 72 65  ord = 0;..    re
45ee6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
45ee7 0a 20 20 7d 0d 0a 0d 0a 20 20 72 63 20 3d 20 70  .  }....  rc = p
45ee8 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54  Module->xOpen(pT
45ee9 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65 78 74 2c  okenizer, zText,
45eea 20 2d 31 2c 20 26 70 43 73 72 29 3b 0d 0a 20 20   -1, &pCsr);..  
45eeb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
45eec 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  K ){..    return
45eed 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20 70 43 73   rc;..  }..  pCs
45eee 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  r->pTokenizer = 
45eef 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 0d 0a 20  pTokenizer;.... 
45ef0 20 78 4e 65 78 74 20 3d 20 70 4d 6f 64 75 6c 65   xNext = pModule
45ef1 2d 3e 78 4e 65 78 74 3b 0d 0a 20 20 77 68 69 6c  ->xNext;..  whil
45ef2 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63  e( SQLITE_OK==rc
45ef3 0d 0a 20 20 20 20 20 20 26 26 20 53 51 4c 49 54  ..      && SQLIT
45ef4 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 78 4e 65 78  E_OK==(rc = xNex
45ef5 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c  t(pCsr, &zToken,
45ef6 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72   &nToken, &iStar
45ef7 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  t, &iEnd, &iPos)
45ef8 29 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 69 6e 74  )..  ){..    int
45ef9 20 69 3b 0d 0a 20 20 20 20 69 66 28 20 69 50 6f   i;..    if( iPo
45efa 73 3e 3d 6e 57 6f 72 64 20 29 20 6e 57 6f 72 64  s>=nWord ) nWord
45efb 20 3d 20 69 50 6f 73 2b 31 3b 0d 0a 0d 0a 20 20   = iPos+1;....  
45efc 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63    /* Positions c
45efd 61 6e 6e 6f 74 20 62 65 20 6e 65 67 61 74 69 76  annot be negativ
45efe 65 3b 20 77 65 20 75 73 65 20 2d 31 20 61 73 20  e; we use -1 as 
45eff 61 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 6e 74  a terminator int
45f00 65 72 6e 61 6c 6c 79 2e 0d 0a 20 20 20 20 2a 2a  ernally...    **
45f01 20 54 6f 6b 65 6e 73 20 6d 75 73 74 20 68 61 76   Tokens must hav
45f02 65 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 6c 65 6e  e a non-zero len
45f03 67 74 68 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20  gth...    */..  
45f04 20 20 69 66 28 20 69 50 6f 73 3c 30 20 7c 7c 20    if( iPos<0 || 
45f05 21 7a 54 6f 6b 65 6e 20 7c 7c 20 6e 54 6f 6b 65  !zToken || nToke
45f06 6e 3c 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72  n<=0 ){..      r
45f07 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
45f08 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
45f09 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
45f0a 20 41 64 64 20 74 68 65 20 74 65 72 6d 20 74 6f   Add the term to
45f0b 20 74 68 65 20 74 65 72 6d 73 20 69 6e 64 65 78   the terms index
45f0c 20 2a 2f 0d 0a 20 20 20 20 72 63 20 3d 20 66 74   */..    rc = ft
45f0d 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41 64  s3PendingTermsAd
45f0e 64 4f 6e 65 28 0d 0a 20 20 20 20 20 20 20 20 70  dOne(..        p
45f0f 2c 20 69 43 6f 6c 2c 20 69 50 6f 73 2c 20 26 70  , iCol, iPos, &p
45f10 2d 3e 61 49 6e 64 65 78 5b 30 5d 2e 68 50 65 6e  ->aIndex[0].hPen
45f11 64 69 6e 67 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54  ding, zToken, nT
45f12 6f 6b 65 6e 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  oken..    );..  
45f13 20 20 0d 0a 20 20 20 20 2f 2a 20 41 64 64 20 74    ..    /* Add t
45f14 68 65 20 74 65 72 6d 20 74 6f 20 65 61 63 68 20  he term to each 
45f15 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 69 6e  of the prefix in
45f16 64 65 78 65 73 20 74 68 61 74 20 69 74 20 69 73  dexes that it is
45f17 20 6e 6f 74 20 74 6f 6f 20 0d 0a 20 20 20 20 2a   not too ..    *
45f18 2a 20 73 68 6f 72 74 20 66 6f 72 2e 20 2a 2f 0d  * short for. */.
45f19 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 72 63  .    for(i=1; rc
45f1a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
45f1b 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b 2b 29  <p->nIndex; i++)
45f1c 7b 0d 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  {..      struct 
45f1d 46 74 73 33 49 6e 64 65 78 20 2a 70 49 6e 64 65  Fts3Index *pInde
45f1e 78 20 3d 20 26 70 2d 3e 61 49 6e 64 65 78 5b 69  x = &p->aIndex[i
45f1f 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 54  ];..      if( nT
45f20 6f 6b 65 6e 3c 70 49 6e 64 65 78 2d 3e 6e 50 72  oken<pIndex->nPr
45f21 65 66 69 78 20 29 20 63 6f 6e 74 69 6e 75 65 3b  efix ) continue;
45f22 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ..      rc = fts
45f23 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41 64 64  3PendingTermsAdd
45f24 4f 6e 65 28 0d 0a 20 20 20 20 20 20 20 20 20 20  One(..          
45f25 70 2c 20 69 43 6f 6c 2c 20 69 50 6f 73 2c 20 26  p, iCol, iPos, &
45f26 70 49 6e 64 65 78 2d 3e 68 50 65 6e 64 69 6e 67  pIndex->hPending
45f27 2c 20 7a 54 6f 6b 65 6e 2c 20 70 49 6e 64 65 78  , zToken, pIndex
45f28 2d 3e 6e 50 72 65 66 69 78 0d 0a 20 20 20 20 20  ->nPrefix..     
45f29 20 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   );..    }..  }.
45f2a 0a 0d 0a 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  ...  pModule->xC
45f2b 6c 6f 73 65 28 70 43 73 72 29 3b 0d 0a 20 20 2a  lose(pCsr);..  *
45f2c 70 6e 57 6f 72 64 20 3d 20 6e 57 6f 72 64 3b 0d  pnWord = nWord;.
45f2d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53  .  return (rc==S
45f2e 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c  QLITE_DONE ? SQL
45f2f 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0d 0a 7d  ITE_OK : rc);..}
45f30 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 43 61 6c 6c  ..../* ..** Call
45f31 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
45f32 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  n indicates that
45f33 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
45f34 73 20 74 6f 20 0d 0a 2a 2a 20 66 74 73 33 50 65  s to ..** fts3Pe
45f35 6e 64 69 6e 67 54 65 72 6d 73 41 64 64 28 29 20  ndingTermsAdd() 
45f36 61 72 65 20 74 6f 20 61 64 64 20 74 65 72 6d 2f  are to add term/
45f37 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 70 61  position-list pa
45f38 69 72 73 20 66 6f 72 20 74 68 65 0d 0a 2a 2a 20  irs for the..** 
45f39 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
45f3a 64 6f 63 75 6d 65 6e 74 20 77 69 74 68 20 64 6f  document with do
45f3b 63 69 64 20 69 44 6f 63 69 64 2e 0d 0a 2a 2f 0d  cid iDocid...*/.
45f3c 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
45f3d 50 65 6e 64 69 6e 67 54 65 72 6d 73 44 6f 63 69  PendingTermsDoci
45f3e 64 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  d(Fts3Table *p, 
45f3f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
45f40 63 69 64 29 7b 0d 0a 20 20 2f 2a 20 54 4f 44 4f  cid){..  /* TODO
45f41 28 73 68 65 73 73 29 20 45 78 70 6c 6f 72 65 20  (shess) Explore 
45f42 77 68 65 74 68 65 72 20 70 61 72 74 69 61 6c 6c  whether partiall
45f43 79 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20 62  y flushing the b
45f44 75 66 66 65 72 20 6f 6e 0d 0a 20 20 2a 2a 20 66  uffer on..  ** f
45f45 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c  orced-flush woul
45f46 64 20 70 72 6f 76 69 64 65 20 62 65 74 74 65 72  d provide better
45f47 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49   performance.  I
45f48 20 73 75 73 70 65 63 74 20 74 68 61 74 20 69 66   suspect that if
45f49 0d 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72 65  ..  ** we ordere
45f4a 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 62  d the doclists b
45f4b 79 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73 68  y size and flush
45f4c 65 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 75  ed the largest u
45f4d 6e 74 69 6c 20 74 68 65 0d 0a 20 20 2a 2a 20 62  ntil the..  ** b
45f4e 75 66 66 65 72 20 77 61 73 20 68 61 6c 66 20 65  uffer was half e
45f4f 6d 70 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64  mpty, that would
45f50 20 6c 65 74 20 74 68 65 20 6c 65 73 73 20 66 72   let the less fr
45f51 65 71 75 65 6e 74 20 74 65 72 6d 73 0d 0a 20 20  equent terms..  
45f52 2a 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67  ** generate long
45f53 65 72 20 64 6f 63 6c 69 73 74 73 2e 0d 0a 20 20  er doclists...  
45f54 2a 2f 0d 0a 20 20 69 66 28 20 69 44 6f 63 69 64  */..  if( iDocid
45f55 3c 3d 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  <=p->iPrevDocid 
45f56 7c 7c 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  || p->nPendingDa
45f57 74 61 3e 70 2d 3e 6e 4d 61 78 50 65 6e 64 69 6e  ta>p->nMaxPendin
45f58 67 44 61 74 61 20 29 7b 0d 0a 20 20 20 20 69 6e  gData ){..    in
45f59 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74  t rc = sqlite3Ft
45f5a 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c  s3PendingTermsFl
45f5b 75 73 68 28 70 29 3b 0d 0a 20 20 20 20 69 66 28  ush(p);..    if(
45f5c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
45f5d 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d   return rc;..  }
45f5e 0d 0a 20 20 70 2d 3e 69 50 72 65 76 44 6f 63 69  ..  p->iPrevDoci
45f5f 64 20 3d 20 69 44 6f 63 69 64 3b 0d 0a 20 20 72  d = iDocid;..  r
45f60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
45f61 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 69  ..}..../*..** Di
45f62 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
45f63 74 73 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e  ts of the pendin
45f64 67 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61 62  g-terms hash tab
45f65 6c 65 73 2e 20 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  les. ..*/..SQLIT
45f66 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
45f67 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e  qlite3Fts3Pendin
45f68 67 54 65 72 6d 73 43 6c 65 61 72 28 46 74 73 33  gTermsClear(Fts3
45f69 54 61 62 6c 65 20 2a 70 29 7b 0d 0a 20 20 69 6e  Table *p){..  in
45f6a 74 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b  t i;..  for(i=0;
45f6b 20 69 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b   i<p->nIndex; i+
45f6c 2b 29 7b 0d 0a 20 20 20 20 46 74 73 33 48 61 73  +){..    Fts3Has
45f6d 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0d 0a 20  hElem *pElem;.. 
45f6e 20 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61     Fts3Hash *pHa
45f6f 73 68 20 3d 20 26 70 2d 3e 61 49 6e 64 65 78 5b  sh = &p->aIndex[
45f70 69 5d 2e 68 50 65 6e 64 69 6e 67 3b 0d 0a 20 20  i].hPending;..  
45f71 20 20 66 6f 72 28 70 45 6c 65 6d 3d 66 74 73 33    for(pElem=fts3
45f72 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
45f73 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 66  ; pElem; pElem=f
45f74 74 73 33 48 61 73 68 4e 65 78 74 28 70 45 6c 65  ts3HashNext(pEle
45f75 6d 29 29 7b 0d 0a 20 20 20 20 20 20 50 65 6e 64  m)){..      Pend
45f76 69 6e 67 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  ingList *pList =
45f77 20 28 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 29   (PendingList *)
45f78 66 74 73 33 48 61 73 68 44 61 74 61 28 70 45 6c  fts3HashData(pEl
45f79 65 6d 29 3b 0d 0a 20 20 20 20 20 20 66 74 73 33  em);..      fts3
45f7a 50 65 6e 64 69 6e 67 4c 69 73 74 44 65 6c 65 74  PendingListDelet
45f7b 65 28 70 4c 69 73 74 29 3b 0d 0a 20 20 20 20 7d  e(pList);..    }
45f7c 0d 0a 20 20 20 20 66 74 73 33 48 61 73 68 43 6c  ..    fts3HashCl
45f7d 65 61 72 28 70 48 61 73 68 29 3b 0d 0a 20 20 7d  ear(pHash);..  }
45f7e 0d 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44  ..  p->nPendingD
45f7f 61 74 61 20 3d 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  ata = 0;..}..../
45f80 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
45f81 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
45f82 20 74 68 65 20 78 55 70 64 61 74 65 28 29 20 6d   the xUpdate() m
45f83 65 74 68 6f 64 20 61 73 20 70 61 72 74 20 6f 66  ethod as part of
45f84 20 61 6e 20 49 4e 53 45 52 54 0d 0a 2a 2a 20 6f   an INSERT..** o
45f85 70 65 72 61 74 69 6f 6e 2e 20 49 74 20 61 64 64  peration. It add
45f86 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61  s entries for ea
45f87 63 68 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6e  ch term in the n
45f88 65 77 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65  ew record to the
45f89 0d 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d  ..** pendingTerm
45f8a 73 20 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a 2a  s hash table...*
45f8b 2a 0d 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 61  *..** Argument a
45f8c 70 56 61 6c 20 69 73 20 74 68 65 20 73 61 6d 65  pVal is the same
45f8d 20 61 73 20 74 68 65 20 73 69 6d 69 6c 61 72 6c   as the similarl
45f8e 79 20 6e 61 6d 65 64 20 61 72 67 75 6d 65 6e 74  y named argument
45f8f 20 70 61 73 73 65 64 20 74 6f 0d 0a 2a 2a 20 66   passed to..** f
45f90 74 73 33 49 6e 73 65 72 74 44 61 74 61 28 29 2e  ts3InsertData().
45f91 20 50 61 72 61 6d 65 74 65 72 20 69 44 6f 63 69   Parameter iDoci
45f92 64 20 69 73 20 74 68 65 20 64 6f 63 69 64 20 6f  d is the docid o
45f93 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0d 0a  f the new row...
45f94 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  */..static int f
45f95 74 73 33 49 6e 73 65 72 74 54 65 72 6d 73 28 46  ts3InsertTerms(F
45f96 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c  ts3Table *p, sql
45f97 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
45f98 61 6c 2c 20 75 33 32 20 2a 61 53 7a 29 7b 0d 0a  al, u32 *aSz){..
45f99 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
45f9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45f9b 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
45f9c 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 66 6f 72  riable */..  for
45f9d 28 69 3d 32 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 75  (i=2; i<p->nColu
45f9e 6d 6e 2b 32 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  mn+2; i++){..   
45f9f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65   const char *zTe
45fa0 78 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  xt = (const char
45fa1 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
45fa2 5f 74 65 78 74 28 61 70 56 61 6c 5b 69 5d 29 3b  _text(apVal[i]);
45fa3 0d 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 66  ..    int rc = f
45fa4 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41  ts3PendingTermsA
45fa5 64 64 28 70 2c 20 7a 54 65 78 74 2c 20 69 2d 32  dd(p, zText, i-2
45fa6 2c 20 26 61 53 7a 5b 69 2d 32 5d 29 3b 0d 0a 20  , &aSz[i-2]);.. 
45fa7 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
45fa8 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 72  E_OK ){..      r
45fa9 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d  eturn rc;..    }
45faa 0d 0a 20 20 20 20 61 53 7a 5b 70 2d 3e 6e 43 6f  ..    aSz[p->nCo
45fab 6c 75 6d 6e 5d 20 2b 3d 20 73 71 6c 69 74 65 33  lumn] += sqlite3
45fac 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70 56  _value_bytes(apV
45fad 61 6c 5b 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20  al[i]);..  }..  
45fae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
45faf 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
45fb0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
45fb1 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 78 55  called by the xU
45fb2 70 64 61 74 65 28 29 20 6d 65 74 68 6f 64 20 66  pdate() method f
45fb3 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65  or an INSERT ope
45fb4 72 61 74 69 6f 6e 2e 0d 0a 2a 2a 20 54 68 65 20  ration...** The 
45fb5 61 70 56 61 6c 20 70 61 72 61 6d 65 74 65 72 20  apVal parameter 
45fb6 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79  is passed a copy
45fb7 20 6f 66 20 74 68 65 20 61 70 56 61 6c 20 61 72   of the apVal ar
45fb8 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 62 79  gument passed by
45fb9 0d 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20 74  ..** SQLite to t
45fba 68 65 20 78 55 70 64 61 74 65 28 29 20 6d 65 74  he xUpdate() met
45fbb 68 6f 64 2e 20 69 2e 65 3a 0d 0a 2a 2a 0d 0a 2a  hod. i.e:..**..*
45fbc 2a 20 20 20 61 70 56 61 6c 5b 30 5d 20 20 20 20  *   apVal[0]    
45fbd 20 20 20 20 20 20 20 20 20 20 20 20 4e 6f 74 20              Not 
45fbe 75 73 65 64 20 66 6f 72 20 49 4e 53 45 52 54 2e  used for INSERT.
45fbf 0d 0a 2a 2a 20 20 20 61 70 56 61 6c 5b 31 5d 20  ..**   apVal[1] 
45fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
45fc1 6f 77 69 64 0d 0a 2a 2a 20 20 20 61 70 56 61 6c  owid..**   apVal
45fc2 5b 32 5d 20 20 20 20 20 20 20 20 20 20 20 20 20  [2]             
45fc3 20 20 20 4c 65 66 74 2d 6d 6f 73 74 20 75 73 65     Left-most use
45fc4 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e  r-defined column
45fc5 0d 0a 2a 2a 20 20 20 2e 2e 2e 0d 0a 2a 2a 20 20  ..**   .....**  
45fc6 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d   apVal[p->nColum
45fc7 6e 2b 31 5d 20 20 20 20 20 52 69 67 68 74 2d 6d  n+1]     Right-m
45fc8 6f 73 74 20 75 73 65 72 2d 64 65 66 69 6e 65 64  ost user-defined
45fc9 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 20 20 61 70   column..**   ap
45fca 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32  Val[p->nColumn+2
45fcb 5d 20 20 20 20 20 48 69 64 64 65 6e 20 63 6f 6c  ]     Hidden col
45fcc 75 6d 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  umn with same na
45fcd 6d 65 20 61 73 20 74 61 62 6c 65 0d 0a 2a 2a 20  me as table..** 
45fce 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75    apVal[p->nColu
45fcf 6d 6e 2b 33 5d 20 20 20 20 20 48 69 64 64 65 6e  mn+3]     Hidden
45fd0 20 22 64 6f 63 69 64 22 20 63 6f 6c 75 6d 6e 20   "docid" column 
45fd1 28 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69 64  (alias for rowid
45fd2 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  )..*/..static in
45fd3 74 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61  t fts3InsertData
45fd4 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  (..  Fts3Table *
45fd5 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
45fd6 20 20 20 20 20 2f 2a 20 46 75 6c 6c 2d 74 65 78       /* Full-tex
45fd7 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 73 71  t table */..  sq
45fd8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
45fd9 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Val,          /*
45fda 20 41 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73   Array of values
45fdb 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0d 0a 20   to insert */.. 
45fdc 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
45fdd 70 69 44 6f 63 69 64 20 20 20 20 20 20 20 20 20  piDocid         
45fde 20 2f 2a 20 4f 55 54 3a 20 44 6f 63 69 64 20 66   /* OUT: Docid f
45fdf 6f 72 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65  or row just inse
45fe0 72 74 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  rted */..){..  i
45fe1 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
45fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45fe3 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
45fe4 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
45fe5 20 2a 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74   *pContentInsert
45fe6 3b 20 20 20 2f 2a 20 49 4e 53 45 52 54 20 49 4e  ;   /* INSERT IN
45fe7 54 4f 20 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c  TO %_content VAL
45fe8 55 45 53 28 2e 2e 2e 29 20 2a 2f 0d 0a 0d 0a 20  UES(...) */.... 
45fe9 20 69 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74   if( p->zContent
45fea 54 62 6c 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  Tbl ){..    sqli
45feb 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 6f 77 69  te3_value *pRowi
45fec 64 20 3d 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f  d = apVal[p->nCo
45fed 6c 75 6d 6e 2b 33 5d 3b 0d 0a 20 20 20 20 69 66  lumn+3];..    if
45fee 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
45fef 74 79 70 65 28 70 52 6f 77 69 64 29 3d 3d 53 51  type(pRowid)==SQ
45ff0 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20  LITE_NULL ){..  
45ff1 20 20 20 20 70 52 6f 77 69 64 20 3d 20 61 70 56      pRowid = apV
45ff2 61 6c 5b 31 5d 3b 0d 0a 20 20 20 20 7d 0d 0a 20  al[1];..    }.. 
45ff3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76     if( sqlite3_v
45ff4 61 6c 75 65 5f 74 79 70 65 28 70 52 6f 77 69 64  alue_type(pRowid
45ff5 29 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  )!=SQLITE_INTEGE
45ff6 52 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  R ){..      retu
45ff7 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  rn SQLITE_CONSTR
45ff8 41 49 4e 54 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  AINT;..    }..  
45ff9 20 20 2a 70 69 44 6f 63 69 64 20 3d 20 73 71 6c    *piDocid = sql
45ffa 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
45ffb 28 70 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 72  (pRowid);..    r
45ffc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
45ffd 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 4c 6f  ..  }....  /* Lo
45ffe 63 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65  cate the stateme
45fff 6e 74 20 68 61 6e 64 6c 65 20 75 73 65 64 20 74  nt handle used t
46000 6f 20 69 6e 73 65 72 74 20 64 61 74 61 20 69 6e  o insert data in
46001 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  to the %_content
46002 0d 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68  ..  ** table. Th
46003 65 20 53 51 4c 20 66 6f 72 20 74 68 69 73 20 73  e SQL for this s
46004 74 61 74 65 6d 65 6e 74 20 69 73 3a 0d 0a 20 20  tatement is:..  
46005 2a 2a 0d 0a 20 20 2a 2a 20 20 20 49 4e 53 45 52  **..  **   INSER
46006 54 20 49 4e 54 4f 20 25 5f 63 6f 6e 74 65 6e 74  T INTO %_content
46007 20 56 41 4c 55 45 53 28 3f 2c 20 3f 2c 20 3f 2c   VALUES(?, ?, ?,
46008 20 2e 2e 2e 29 0d 0a 20 20 2a 2a 0d 0a 20 20 2a   ...)..  **..  *
46009 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * The statement 
4600a 66 65 61 74 75 72 65 73 20 4e 20 27 3f 27 20 76  features N '?' v
4600b 61 72 69 61 62 6c 65 73 2c 20 77 68 65 72 65 20  ariables, where 
4600c 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
4600d 6f 66 20 75 73 65 72 0d 0a 20 20 2a 2a 20 64 65  of user..  ** de
4600e 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 73 20 69 6e  fined columns in
4600f 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65 2c   the FTS3 table,
46010 20 70 6c 75 73 20 6f 6e 65 20 66 6f 72 20 74 68   plus one for th
46011 65 20 64 6f 63 69 64 20 66 69 65 6c 64 2e 0d 0a  e docid field...
46012 20 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 66 74 73    */..  rc = fts
46013 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
46014 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 2c 20  CONTENT_INSERT, 
46015 26 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c  &pContentInsert,
46016 20 26 61 70 56 61 6c 5b 31 5d 29 3b 0d 0a 20 20   &apVal[1]);..  
46017 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
46018 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  K ){..    return
46019 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f   rc;..  }....  /
4601a 2a 20 54 68 65 72 65 20 69 73 20 61 20 71 75 69  * There is a qui
4601b 72 6b 20 68 65 72 65 2e 20 54 68 65 20 75 73 65  rk here. The use
4601c 72 73 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  rs INSERT statem
4601d 65 6e 74 20 6d 61 79 20 68 61 76 65 20 73 70 65  ent may have spe
4601e 63 69 66 69 65 64 0d 0a 20 20 2a 2a 20 61 20 76  cified..  ** a v
4601f 61 6c 75 65 20 66 6f 72 20 74 68 65 20 22 72 6f  alue for the "ro
46020 77 69 64 22 20 66 69 65 6c 64 2c 20 66 6f 72 20  wid" field, for 
46021 74 68 65 20 22 64 6f 63 69 64 22 20 66 69 65 6c  the "docid" fiel
46022 64 2c 20 6f 72 20 66 6f 72 20 62 6f 74 68 2e 0d  d, or for both..
46023 0a 20 20 2a 2a 20 57 68 69 63 68 20 69 73 20 61  .  ** Which is a
46024 20 70 72 6f 62 6c 65 6d 2c 20 73 69 6e 63 65 20   problem, since 
46025 22 72 6f 77 69 64 22 20 61 6e 64 20 22 64 6f 63  "rowid" and "doc
46026 69 64 22 20 61 72 65 20 61 6c 69 61 73 65 73 20  id" are aliases 
46027 66 6f 72 20 74 68 65 0d 0a 20 20 2a 2a 20 73 61  for the..  ** sa
46028 6d 65 20 76 61 6c 75 65 2e 20 46 6f 72 20 65 78  me value. For ex
46029 61 6d 70 6c 65 3a 0d 0a 20 20 2a 2a 0d 0a 20 20  ample:..  **..  
4602a 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  **   INSERT INTO
4602b 20 66 74 73 33 74 62 6c 28 72 6f 77 69 64 2c 20   fts3tbl(rowid, 
4602c 64 6f 63 69 64 29 20 56 41 4c 55 45 53 28 31 2c  docid) VALUES(1,
4602d 20 32 29 3b 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a   2);..  **..  **
4602e 20 49 6e 20 46 54 53 33 2c 20 74 68 69 73 20 69   In FTS3, this i
4602f 73 20 61 6e 20 65 72 72 6f 72 2e 20 49 74 20 69  s an error. It i
46030 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 73 70  s an error to sp
46031 65 63 69 66 79 20 6e 6f 6e 2d 4e 55 4c 4c 20 76  ecify non-NULL v
46032 61 6c 75 65 73 0d 0a 20 20 2a 2a 20 66 6f 72 20  alues..  ** for 
46033 62 6f 74 68 20 64 6f 63 69 64 20 61 6e 64 20 73  both docid and s
46034 6f 6d 65 20 6f 74 68 65 72 20 72 6f 77 69 64 20  ome other rowid 
46035 61 6c 69 61 73 2e 0d 0a 20 20 2a 2f 0d 0a 20 20  alias...  */..  
46036 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21  if( SQLITE_NULL!
46037 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
46038 79 70 65 28 61 70 56 61 6c 5b 33 2b 70 2d 3e 6e  ype(apVal[3+p->n
46039 43 6f 6c 75 6d 6e 5d 29 20 29 7b 0d 0a 20 20 20  Column]) ){..   
4603a 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   if( SQLITE_NULL
4603b 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
4603c 74 79 70 65 28 61 70 56 61 6c 5b 30 5d 29 0d 0a  type(apVal[0])..
4603d 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4e       && SQLITE_N
4603e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL!=sqlite3_val
4603f 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b 31 5d  ue_type(apVal[1]
46040 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20 20 20  )..    ){..     
46041 20 2f 2a 20 41 20 72 6f 77 69 64 2f 64 6f 63 69   /* A rowid/doci
46042 64 20 63 6f 6e 66 6c 69 63 74 2e 20 2a 2f 0d 0a  d conflict. */..
46043 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
46044 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20  ITE_ERROR;..    
46045 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  }..    rc = sqli
46046 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70  te3_bind_value(p
46047 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c 20 31  ContentInsert, 1
46048 2c 20 61 70 56 61 6c 5b 33 2b 70 2d 3e 6e 43 6f  , apVal[3+p->nCo
46049 6c 75 6d 6e 5d 29 3b 0d 0a 20 20 20 20 69 66 28  lumn]);..    if(
4604a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
4604b 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 7d   return rc;..  }
4604c 0d 0a 0d 0a 20 20 2f 2a 20 45 78 65 63 75 74 65  ....  /* Execute
4604d 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
4604e 6f 20 69 6e 73 65 72 74 20 74 68 65 20 72 65 63  o insert the rec
4604f 6f 72 64 2e 20 53 65 74 20 2a 70 69 44 6f 63 69  ord. Set *piDoci
46050 64 20 74 6f 20 74 68 65 20 0d 0a 20 20 2a 2a 20  d to the ..  ** 
46051 6e 65 77 20 64 6f 63 69 64 20 76 61 6c 75 65 2e  new docid value.
46052 20 0d 0a 20 20 2a 2f 0d 0a 20 20 73 71 6c 69 74   ..  */..  sqlit
46053 65 33 5f 73 74 65 70 28 70 43 6f 6e 74 65 6e 74  e3_step(pContent
46054 49 6e 73 65 72 74 29 3b 0d 0a 20 20 72 63 20 3d  Insert);..  rc =
46055 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
46056 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 29 3b 0d  ContentInsert);.
46057 0a 0d 0a 20 20 2a 70 69 44 6f 63 69 64 20 3d 20  ...  *piDocid = 
46058 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
46059 65 72 74 5f 72 6f 77 69 64 28 70 2d 3e 64 62 29  ert_rowid(p->db)
4605a 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
4605b 0a 7d 0d 0a 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  .}......../*..**
4605c 20 52 65 6d 6f 76 65 20 61 6c 6c 20 64 61 74 61   Remove all data
4605d 20 66 72 6f 6d 20 74 68 65 20 46 54 53 33 20 74   from the FTS3 t
4605e 61 62 6c 65 2e 20 43 6c 65 61 72 20 74 68 65 20  able. Clear the 
4605f 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61  hash table conta
46060 69 6e 69 6e 67 0d 0a 2a 2a 20 70 65 6e 64 69 6e  ining..** pendin
46061 67 20 74 65 72 6d 73 2e 0d 0a 2a 2f 0d 0a 73 74  g terms...*/..st
46062 61 74 69 63 20 69 6e 74 20 66 74 73 33 44 65 6c  atic int fts3Del
46063 65 74 65 41 6c 6c 28 46 74 73 33 54 61 62 6c 65  eteAll(Fts3Table
46064 20 2a 70 2c 20 69 6e 74 20 62 43 6f 6e 74 65 6e   *p, int bConten
46065 74 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  t){..  int rc = 
46066 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
46067 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
46068 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a   code */....  /*
46069 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e   Discard the con
4606a 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 65 6e  tents of the pen
4606b 64 69 6e 67 2d 74 65 72 6d 73 20 68 61 73 68 20  ding-terms hash 
4606c 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 73 71 6c  table. */..  sql
4606d 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54  ite3Fts3PendingT
4606e 65 72 6d 73 43 6c 65 61 72 28 70 29 3b 0d 0a 0d  ermsClear(p);...
4606f 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 76 65  .  /* Delete eve
46070 72 79 74 68 69 6e 67 20 66 72 6f 6d 20 74 68 65  rything from the
46071 20 73 68 61 64 6f 77 20 74 61 62 6c 65 73 2e 20   shadow tables. 
46072 45 78 63 65 70 74 2c 20 6c 65 61 76 65 20 25 5f  Except, leave %_
46073 63 6f 6e 74 65 6e 74 20 61 73 0d 0a 20 20 2a 2a  content as..  **
46074 20 69 73 20 69 66 20 62 43 6f 6e 74 65 6e 74 20   is if bContent 
46075 69 73 20 66 61 6c 73 65 2e 20 20 2a 2f 0d 0a 20  is false.  */.. 
46076 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 43 6f 6e   assert( p->zCon
46077 74 65 6e 74 54 62 6c 3d 3d 30 20 7c 7c 20 62 43  tentTbl==0 || bC
46078 6f 6e 74 65 6e 74 3d 3d 30 20 29 3b 0d 0a 20 20  ontent==0 );..  
46079 69 66 28 20 62 43 6f 6e 74 65 6e 74 20 29 20 66  if( bContent ) f
4607a 74 73 33 53 71 6c 45 78 65 63 28 26 72 63 2c 20  ts3SqlExec(&rc, 
4607b 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c  p, SQL_DELETE_AL
4607c 4c 5f 43 4f 4e 54 45 4e 54 2c 20 30 29 3b 0d 0a  L_CONTENT, 0);..
4607d 20 20 66 74 73 33 53 71 6c 45 78 65 63 28 26 72    fts3SqlExec(&r
4607e 63 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45  c, p, SQL_DELETE
4607f 5f 41 4c 4c 5f 53 45 47 4d 45 4e 54 53 2c 20 30  _ALL_SEGMENTS, 0
46080 29 3b 0d 0a 20 20 66 74 73 33 53 71 6c 45 78 65  );..  fts3SqlExe
46081 63 28 26 72 63 2c 20 70 2c 20 53 51 4c 5f 44 45  c(&rc, p, SQL_DE
46082 4c 45 54 45 5f 41 4c 4c 5f 53 45 47 44 49 52 2c  LETE_ALL_SEGDIR,
46083 20 30 29 3b 0d 0a 20 20 69 66 28 20 70 2d 3e 62   0);..  if( p->b
46084 48 61 73 44 6f 63 73 69 7a 65 20 29 7b 0d 0a 20  HasDocsize ){.. 
46085 20 20 20 66 74 73 33 53 71 6c 45 78 65 63 28 26     fts3SqlExec(&
46086 72 63 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54  rc, p, SQL_DELET
46087 45 5f 41 4c 4c 5f 44 4f 43 53 49 5a 45 2c 20 30  E_ALL_DOCSIZE, 0
46088 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 70  );..  }..  if( p
46089 2d 3e 62 48 61 73 53 74 61 74 20 29 7b 0d 0a 20  ->bHasStat ){.. 
4608a 20 20 20 66 74 73 33 53 71 6c 45 78 65 63 28 26     fts3SqlExec(&
4608b 72 63 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54  rc, p, SQL_DELET
4608c 45 5f 41 4c 4c 5f 53 54 41 54 2c 20 30 29 3b 0d  E_ALL_STAT, 0);.
4608d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
4608e 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
4608f 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  The first elemen
46090 74 20 69 6e 20 74 68 65 20 61 70 56 61 6c 5b 5d  t in the apVal[]
46091 20 61 72 72 61 79 20 69 73 20 61 73 73 75 6d 65   array is assume
46092 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  d to contain the
46093 20 64 6f 63 69 64 0d 0a 2a 2a 20 28 61 6e 20 69   docid..** (an i
46094 6e 74 65 67 65 72 29 20 6f 66 20 61 20 72 6f 77  nteger) of a row
46095 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c   about to be del
46096 65 74 65 64 2e 20 52 65 6d 6f 76 65 20 61 6c 6c  eted. Remove all
46097 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 0d   terms from the.
46098 0a 2a 2a 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e  .** full-text in
46099 64 65 78 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  dex...*/..static
4609a 20 76 6f 69 64 20 66 74 73 33 44 65 6c 65 74 65   void fts3Delete
4609b 54 65 72 6d 73 28 20 0d 0a 20 20 69 6e 74 20 2a  Terms( ..  int *
4609c 70 52 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  pRC,            
4609d 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
4609e 65 20 2a 2f 0d 0a 20 20 46 74 73 33 54 61 62 6c  e */..  Fts3Tabl
4609f 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
460a0 2f 2a 20 54 68 65 20 46 54 53 20 74 61 62 6c 65  /* The FTS table
460a1 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20   to delete from 
460a2 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  */..  sqlite3_va
460a3 6c 75 65 20 2a 70 52 6f 77 69 64 2c 20 20 2f 2a  lue *pRowid,  /*
460a4 20 54 68 65 20 64 6f 63 69 64 20 74 6f 20 62 65   The docid to be
460a5 20 64 65 6c 65 74 65 64 20 2a 2f 0d 0a 20 20 75   deleted */..  u
460a6 33 32 20 2a 61 53 7a 20 20 20 20 20 20 20 20 20  32 *aSz         
460a7 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20         /* Sizes 
460a8 6f 66 20 64 65 6c 65 74 65 64 20 64 6f 63 75 6d  of deleted docum
460a9 65 6e 74 20 77 72 69 74 74 65 6e 20 68 65 72 65  ent written here
460aa 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
460ab 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  c;..  sqlite3_st
460ac 6d 74 20 2a 70 53 65 6c 65 63 74 3b 0d 0a 0d 0a  mt *pSelect;....
460ad 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74    if( *pRC ) ret
460ae 75 72 6e 3b 0d 0a 20 20 72 63 20 3d 20 66 74 73  urn;..  rc = fts
460af 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
460b0 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54 5f 42  SELECT_CONTENT_B
460b1 59 5f 52 4f 57 49 44 2c 20 26 70 53 65 6c 65 63  Y_ROWID, &pSelec
460b2 74 2c 20 26 70 52 6f 77 69 64 29 3b 0d 0a 20 20  t, &pRowid);..  
460b3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
460b4 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 53 51  K ){..    if( SQ
460b5 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
460b6 33 5f 73 74 65 70 28 70 53 65 6c 65 63 74 29 20  3_step(pSelect) 
460b7 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  ){..      int i;
460b8 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ..      for(i=1;
460b9 20 69 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20   i<=p->nColumn; 
460ba 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 63  i++){..        c
460bb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 78 74  onst char *zText
460bc 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
460bd 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
460be 74 65 78 74 28 70 53 65 6c 65 63 74 2c 20 69 29  text(pSelect, i)
460bf 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ;..        rc = 
460c0 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73  fts3PendingTerms
460c1 41 64 64 28 70 2c 20 7a 54 65 78 74 2c 20 2d 31  Add(p, zText, -1
460c2 2c 20 26 61 53 7a 5b 69 2d 31 5d 29 3b 0d 0a 20  , &aSz[i-1]);.. 
460c3 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
460c4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
460c5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
460c6 65 73 65 74 28 70 53 65 6c 65 63 74 29 3b 0d 0a  eset(pSelect);..
460c7 20 20 20 20 20 20 20 20 20 20 2a 70 52 43 20 3d            *pRC =
460c8 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 20 20   rc;..          
460c9 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 20 20 20  return;..       
460ca 20 7d 0d 0a 20 20 20 20 20 20 20 20 61 53 7a 5b   }..        aSz[
460cb 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 2b 3d 20 73  p->nColumn] += s
460cc 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
460cd 74 65 73 28 70 53 65 6c 65 63 74 2c 20 69 29 3b  tes(pSelect, i);
460ce 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
460cf 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
460d0 65 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74  e3_reset(pSelect
460d1 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  );..  }else{..  
460d2 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
460d3 70 53 65 6c 65 63 74 29 3b 0d 0a 20 20 7d 0d 0a  pSelect);..  }..
460d4 20 20 2a 70 52 43 20 3d 20 72 63 3b 0d 0a 7d 0d    *pRC = rc;..}.
460d5 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 6f 72 77 61 72  .../*..** Forwar
460d6 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 6f  d declaration to
460d7 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 65   account for the
460d8 20 63 69 72 63 75 6c 61 72 20 64 65 70 65 6e 64   circular depend
460d9 65 6e 63 79 20 62 65 74 77 65 65 6e 0d 0a 2a 2a  ency between..**
460da 20 66 75 6e 63 74 69 6f 6e 73 20 66 74 73 33 53   functions fts3S
460db 65 67 6d 65 6e 74 4d 65 72 67 65 28 29 20 61 6e  egmentMerge() an
460dc 64 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65  d fts3AllocateSe
460dd 67 64 69 72 49 64 78 28 29 2e 0d 0a 2a 2f 0d 0a  gdirIdx()...*/..
460de 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
460df 65 67 6d 65 6e 74 4d 65 72 67 65 28 46 74 73 33  egmentMerge(Fts3
460e0 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  Table *, int, in
460e1 74 29 3b 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 54  t);..../* ..** T
460e2 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c  his function all
460e3 6f 63 61 74 65 73 20 61 20 6e 65 77 20 6c 65 76  ocates a new lev
460e4 65 6c 20 69 4c 65 76 65 6c 20 69 6e 64 65 78 20  el iLevel index 
460e5 69 6e 20 74 68 65 20 73 65 67 64 69 72 20 74 61  in the segdir ta
460e6 62 6c 65 2e 0d 0a 2a 2a 20 55 73 75 61 6c 6c 79  ble...** Usually
460e7 2c 20 69 6e 64 65 78 65 73 20 61 72 65 20 61 6c  , indexes are al
460e8 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 61  located within a
460e9 20 6c 65 76 65 6c 20 73 65 71 75 65 6e 74 69 61   level sequentia
460ea 6c 6c 79 20 73 74 61 72 74 69 6e 67 0d 0a 2a 2a  lly starting..**
460eb 20 77 69 74 68 20 30 2c 20 73 6f 20 74 68 65 20   with 0, so the 
460ec 61 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78 20  allocated index 
460ed 69 73 20 6f 6e 65 20 67 72 65 61 74 65 72 20 74  is one greater t
460ee 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  han the value re
460ef 74 75 72 6e 65 64 0d 0a 2a 2a 20 62 79 3a 0d 0a  turned..** by:..
460f0 2a 2a 0d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  **..**   SELECT 
460f1 6d 61 78 28 69 64 78 29 20 46 52 4f 4d 20 25 5f  max(idx) FROM %_
460f2 73 65 67 64 69 72 20 57 48 45 52 45 20 6c 65 76  segdir WHERE lev
460f3 65 6c 20 3d 20 3a 69 4c 65 76 65 6c 0d 0a 2a 2a  el = :iLevel..**
460f4 0d 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66  ..** However, if
460f5 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61   there are alrea
460f6 64 79 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f  dy FTS3_MERGE_CO
460f7 55 4e 54 20 69 6e 64 65 78 65 73 20 61 74 20 74  UNT indexes at t
460f8 68 65 20 72 65 71 75 65 73 74 65 64 0d 0a 2a 2a  he requested..**
460f9 20 6c 65 76 65 6c 2c 20 74 68 65 79 20 61 72 65   level, they are
460fa 20 6d 65 72 67 65 64 20 69 6e 74 6f 20 61 20 73   merged into a s
460fb 69 6e 67 6c 65 20 6c 65 76 65 6c 20 28 69 4c 65  ingle level (iLe
460fc 76 65 6c 2b 31 29 20 73 65 67 6d 65 6e 74 20 61  vel+1) segment a
460fd 6e 64 20 74 68 65 20 0d 0a 2a 2a 20 61 6c 6c 6f  nd the ..** allo
460fe 63 61 74 65 64 20 69 6e 64 65 78 20 69 73 20 30  cated index is 0
460ff 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63  ...**..** If suc
46100 63 65 73 73 66 75 6c 2c 20 2a 70 69 49 64 78 20  cessful, *piIdx 
46101 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61 6c  is set to the al
46102 6c 6f 63 61 74 65 64 20 69 6e 64 65 78 20 73 6c  located index sl
46103 6f 74 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  ot and SQLITE_OK
46104 0d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f  ..** returned. O
46105 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c  therwise, an SQL
46106 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
46107 73 20 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d  s returned...*/.
46108 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
46109 41 6c 6c 6f 63 61 74 65 53 65 67 64 69 72 49 64  AllocateSegdirId
4610a 78 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20  x(..  Fts3Table 
4610b 2a 70 2c 20 0d 0a 20 20 69 6e 74 20 69 49 6e 64  *p, ..  int iInd
4610c 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ex,             
4610d 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
4610e 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65 78 20 2a   for p->aIndex *
4610f 2f 0d 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c  /..  int iLevel,
46110 20 0d 0a 20 20 69 6e 74 20 2a 70 69 49 64 78 0d   ..  int *piIdx.
46111 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20  .){..  int rc;  
46112 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46113 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
46114 20 43 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c 69   Code */..  sqli
46115 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 49  te3_stmt *pNextI
46116 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 51  dx;         /* Q
46117 75 65 72 79 20 66 6f 72 20 6e 65 78 74 20 69 64  uery for next id
46118 78 20 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65  x at level iLeve
46119 6c 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4e 65 78  l */..  int iNex
4611a 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
4611b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
4611c 74 20 6f 66 20 71 75 65 72 79 20 70 4e 65 78 74  t of query pNext
4611d 49 64 78 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 53  Idx */....  /* S
4611e 65 74 20 76 61 72 69 61 62 6c 65 20 69 4e 65 78  et variable iNex
4611f 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 61 76  t to the next av
46120 61 69 6c 61 62 6c 65 20 73 65 67 64 69 72 20 69  ailable segdir i
46121 6e 64 65 78 20 61 74 20 6c 65 76 65 6c 20 69 4c  ndex at level iL
46122 65 76 65 6c 2e 20 2a 2f 0d 0a 20 20 72 63 20 3d  evel. */..  rc =
46123 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
46124 53 51 4c 5f 4e 45 58 54 5f 53 45 47 4d 45 4e 54  SQL_NEXT_SEGMENT
46125 5f 49 4e 44 45 58 2c 20 26 70 4e 65 78 74 49 64  _INDEX, &pNextId
46126 78 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63  x, 0);..  if( rc
46127 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
46128 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
46129 5f 69 6e 74 28 70 4e 65 78 74 49 64 78 2c 20 31  _int(pNextIdx, 1
4612a 2c 20 69 49 6e 64 65 78 2a 46 54 53 33 5f 53 45  , iIndex*FTS3_SE
4612b 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20 2b 20  GDIR_MAXLEVEL + 
4612c 69 4c 65 76 65 6c 29 3b 0d 0a 20 20 20 20 69 66  iLevel);..    if
4612d 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
4612e 6c 69 74 65 33 5f 73 74 65 70 28 70 4e 65 78 74  lite3_step(pNext
4612f 49 64 78 29 20 29 7b 0d 0a 20 20 20 20 20 20 69  Idx) ){..      i
46130 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 63  Next = sqlite3_c
46131 6f 6c 75 6d 6e 5f 69 6e 74 28 70 4e 65 78 74 49  olumn_int(pNextI
46132 64 78 2c 20 30 29 3b 0d 0a 20 20 20 20 7d 0d 0a  dx, 0);..    }..
46133 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
46134 5f 72 65 73 65 74 28 70 4e 65 78 74 49 64 78 29  _reset(pNextIdx)
46135 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20  ;..  }....  if( 
46136 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
46137 0d 0a 20 20 20 20 2f 2a 20 49 66 20 69 4e 65 78  ..    /* If iNex
46138 74 20 69 73 20 46 54 53 33 5f 4d 45 52 47 45 5f  t is FTS3_MERGE_
46139 43 4f 55 4e 54 2c 20 69 6e 64 69 63 61 74 69 6e  COUNT, indicatin
4613a 67 20 74 68 61 74 20 6c 65 76 65 6c 20 69 4c 65  g that level iLe
4613b 76 65 6c 20 69 73 20 61 6c 72 65 61 64 79 0d 0a  vel is already..
4613c 20 20 20 20 2a 2a 20 66 75 6c 6c 2c 20 6d 65 72      ** full, mer
4613d 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  ge all segments 
4613e 69 6e 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20  in level iLevel 
4613f 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 69 4c  into a single iL
46140 65 76 65 6c 2b 31 0d 0a 20 20 20 20 2a 2a 20 73  evel+1..    ** s
46141 65 67 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 6f 63  egment and alloc
46142 61 74 65 20 28 6e 65 77 6c 79 20 66 72 65 65 64  ate (newly freed
46143 29 20 69 6e 64 65 78 20 30 20 61 74 20 6c 65 76  ) index 0 at lev
46144 65 6c 20 69 4c 65 76 65 6c 2e 20 4f 74 68 65 72  el iLevel. Other
46145 77 69 73 65 2c 0d 0a 20 20 20 20 2a 2a 20 69 66  wise,..    ** if
46146 20 69 4e 65 78 74 20 69 73 20 6c 65 73 73 20 74   iNext is less t
46147 68 61 6e 20 46 54 53 33 5f 4d 45 52 47 45 5f 43  han FTS3_MERGE_C
46148 4f 55 4e 54 2c 20 61 6c 6c 6f 63 61 74 65 20 69  OUNT, allocate i
46149 6e 64 65 78 20 69 4e 65 78 74 2e 0d 0a 20 20 20  ndex iNext...   
4614a 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 69 4e 65   */..    if( iNe
4614b 78 74 3e 3d 46 54 53 33 5f 4d 45 52 47 45 5f 43  xt>=FTS3_MERGE_C
4614c 4f 55 4e 54 20 29 7b 0d 0a 20 20 20 20 20 20 72  OUNT ){..      r
4614d 63 20 3d 20 66 74 73 33 53 65 67 6d 65 6e 74 4d  c = fts3SegmentM
4614e 65 72 67 65 28 70 2c 20 69 49 6e 64 65 78 2c 20  erge(p, iIndex, 
4614f 69 4c 65 76 65 6c 29 3b 0d 0a 20 20 20 20 20 20  iLevel);..      
46150 2a 70 69 49 64 78 20 3d 20 30 3b 0d 0a 20 20 20  *piIdx = 0;..   
46151 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2a   }else{..      *
46152 70 69 49 64 78 20 3d 20 69 4e 65 78 74 3b 0d 0a  piIdx = iNext;..
46153 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
46154 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
46155 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 25 5f 73 65  ./*..** The %_se
46156 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 73 20  gments table is 
46157 64 65 63 6c 61 72 65 64 20 61 73 20 66 6f 6c 6c  declared as foll
46158 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 43  ows:..**..**   C
46159 52 45 41 54 45 20 54 41 42 4c 45 20 25 5f 73 65  REATE TABLE %_se
4615a 67 6d 65 6e 74 73 28 62 6c 6f 63 6b 69 64 20 49  gments(blockid I
4615b 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
4615c 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 0d  EY, block BLOB).
4615d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  .**..** This fun
4615e 63 74 69 6f 6e 20 72 65 61 64 73 20 64 61 74 61  ction reads data
4615f 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 72   from a single r
46160 6f 77 20 6f 66 20 74 68 65 20 25 5f 73 65 67 6d  ow of the %_segm
46161 65 6e 74 73 20 74 61 62 6c 65 2e 20 54 68 65 0d  ents table. The.
46162 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 72 6f 77  .** specific row
46163 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
46164 79 20 74 68 65 20 69 42 6c 6f 63 6b 69 64 20 70  y the iBlockid p
46165 61 72 61 6d 65 74 65 72 2e 20 49 66 20 70 61 42  arameter. If paB
46166 6c 6f 62 20 69 73 20 6e 6f 74 0d 0a 2a 2a 20 4e  lob is not..** N
46167 55 4c 4c 2c 20 74 68 65 6e 20 61 20 62 75 66 66  ULL, then a buff
46168 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  er is allocated 
46169 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61  using sqlite3_ma
4616a 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 70 75 6c  lloc() and popul
4616b 61 74 65 64 0d 0a 2a 2a 20 77 69 74 68 20 74 68  ated..** with th
4616c 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
4616d 65 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e  e blob stored in
4616e 20 74 68 65 20 22 62 6c 6f 63 6b 22 20 63 6f 6c   the "block" col
4616f 75 6d 6e 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20  umn of the ..** 
46170 69 64 65 6e 74 69 66 69 65 64 20 74 61 62 6c 65  identified table
46171 20 72 6f 77 20 69 73 2e 20 57 68 65 74 68 65 72   row is. Whether
46172 20 6f 72 20 6e 6f 74 20 70 61 42 6c 6f 62 20 69   or not paBlob i
46173 73 20 4e 55 4c 4c 2c 20 2a 70 6e 42 6c 6f 62 20  s NULL, *pnBlob 
46174 69 73 20 73 65 74 0d 0a 2a 2a 20 74 6f 20 74 68  is set..** to th
46175 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c  e size of the bl
46176 6f 62 20 69 6e 20 62 79 74 65 73 20 62 65 66 6f  ob in bytes befo
46177 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0d 0a 2a  re returning...*
46178 2a 0d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  *..** If an erro
46179 72 20 6f 63 63 75 72 73 2c 20 6f 72 20 74 68 65  r occurs, or the
4617a 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
4617b 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 70 65 63  contain the spec
4617c 69 66 69 65 64 20 72 6f 77 2c 0d 0a 2a 2a 20 61  ified row,..** a
4617d 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
4617e 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
4617f 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49   Otherwise, SQLI
46180 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
46181 64 2e 20 49 66 0d 0a 2a 2a 20 70 61 42 6c 6f 62  d. If..** paBlob
46182 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68   is non-NULL, th
46183 65 6e 20 69 74 20 69 73 20 74 68 65 20 72 65 73  en it is the res
46184 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
46185 68 65 20 63 61 6c 6c 65 72 20 74 6f 0d 0a 2a 2a  he caller to..**
46186 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
46187 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75   the returned bu
46188 66 66 65 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ffer...**..** Th
46189 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
4618a 6c 65 61 76 65 20 61 6e 20 6f 70 65 6e 20 73 71  leave an open sq
4618b 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64  lite3_blob* hand
4618c 6c 65 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 46 74  le in the..** Ft
4618d 73 33 54 61 62 6c 65 2e 70 53 65 67 6d 65 6e 74  s3Table.pSegment
4618e 73 20 76 61 72 69 61 62 6c 65 2e 20 54 68 69 73  s variable. This
4618f 20 68 61 6e 64 6c 65 20 69 73 20 72 65 75 73 65   handle is reuse
46190 64 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20  d by subsequent 
46191 63 61 6c 6c 73 0d 0a 2a 2a 20 74 6f 20 74 68 69  calls..** to thi
46192 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  s function. The 
46193 68 61 6e 64 6c 65 20 6d 61 79 20 62 65 20 63 6c  handle may be cl
46194 6f 73 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20  osed by calling 
46195 74 68 65 0d 0a 2a 2a 20 73 71 6c 69 74 65 33 46  the..** sqlite3F
46196 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65  ts3SegmentsClose
46197 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 52 65 75  () function. Reu
46198 73 69 6e 67 20 61 20 62 6c 6f 62 20 68 61 6e 64  sing a blob hand
46199 6c 65 20 69 73 20 61 20 68 61 6e 64 79 0d 0a 2a  le is a handy..*
4619a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d  * performance im
4619b 70 72 6f 76 65 6d 65 6e 74 2c 20 62 75 74 20 74  provement, but t
4619c 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 73  he blob handle s
4619d 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
4619e 63 6c 6f 73 65 64 0d 0a 2a 2a 20 62 65 66 6f 72  closed..** befor
4619f 65 20 63 6f 6e 74 72 6f 6c 20 69 73 20 72 65 74  e control is ret
461a0 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65  urned to the use
461a1 72 20 28 74 6f 20 70 72 65 76 65 6e 74 20 61 20  r (to prevent a 
461a2 6c 6f 63 6b 20 62 65 69 6e 67 20 68 65 6c 64 0d  lock being held.
461a3 0a 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62  .** on the datab
461a4 61 73 65 20 66 69 6c 65 20 66 6f 72 20 6c 6f 6e  ase file for lon
461a5 67 65 72 20 74 68 61 6e 20 6e 65 63 65 73 73 61  ger than necessa
461a6 72 79 29 2e 20 54 68 75 73 2c 20 61 6e 79 20 76  ry). Thus, any v
461a7 69 72 74 75 61 6c 20 74 61 62 6c 65 0d 0a 2a 2a  irtual table..**
461a8 20 6d 65 74 68 6f 64 20 28 78 46 69 6c 74 65 72   method (xFilter
461a9 20 65 74 63 2e 29 20 74 68 61 74 20 6d 61 79 20   etc.) that may 
461aa 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69  directly or indi
461ab 72 65 63 74 6c 79 20 63 61 6c 6c 20 74 68 69 73  rectly call this
461ac 20 66 75 6e 63 74 69 6f 6e 0d 0a 2a 2a 20 6d 75   function..** mu
461ad 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 46  st call sqlite3F
461ae 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65  ts3SegmentsClose
461af 28 29 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  () before return
461b0 69 6e 67 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45  ing...*/..SQLITE
461b1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
461b2 69 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63  ite3Fts3ReadBloc
461b3 6b 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20  k(..  Fts3Table 
461b4 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
461b5 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20 74 61        /* FTS3 ta
461b6 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ble handle */.. 
461b7 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
461b8 42 6c 6f 63 6b 69 64 2c 20 20 20 20 20 20 20 20  Blockid,        
461b9 20 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20 72   /* Access the r
461ba 6f 77 20 77 69 74 68 20 62 6c 6f 63 6b 69 64 3d  ow with blockid=
461bb 24 69 42 6c 6f 63 6b 69 64 20 2a 2f 0d 0a 20 20  $iBlockid */..  
461bc 63 68 61 72 20 2a 2a 70 61 42 6c 6f 62 2c 20 20  char **paBlob,  
461bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
461be 2f 2a 20 4f 55 54 3a 20 42 6c 6f 62 20 64 61 74  /* OUT: Blob dat
461bf 61 20 69 6e 20 6d 61 6c 6c 6f 63 27 64 20 62 75  a in malloc'd bu
461c0 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  ffer */..  int *
461c1 70 6e 42 6c 6f 62 2c 20 20 20 20 20 20 20 20 20  pnBlob,         
461c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
461c3 54 3a 20 53 69 7a 65 20 6f 66 20 62 6c 6f 62 20  T: Size of blob 
461c4 64 61 74 61 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  data */..  int *
461c5 70 6e 4c 6f 61 64 20 20 20 20 20 20 20 20 20 20  pnLoad          
461c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
461c7 54 3a 20 42 79 74 65 73 20 61 63 74 75 61 6c 6c  T: Bytes actuall
461c8 79 20 6c 6f 61 64 65 64 20 2a 2f 0d 0a 29 7b 0d  y loaded */..){.
461c9 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
461ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
461cb 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
461cc 65 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 70 6e 42  e */....  /* pnB
461cd 6c 6f 62 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d  lob must be non-
461ce 4e 55 4c 4c 2e 20 70 61 42 6c 6f 62 20 6d 61 79  NULL. paBlob may
461cf 20 62 65 20 4e 55 4c 4c 20 6f 72 20 6e 6f 6e 2d   be NULL or non-
461d0 4e 55 4c 4c 2e 20 2a 2f 0d 0a 20 20 61 73 73 65  NULL. */..  asse
461d1 72 74 28 20 70 6e 42 6c 6f 62 29 3b 0d 0a 0d 0a  rt( pnBlob);....
461d2 20 20 69 66 28 20 70 2d 3e 70 53 65 67 6d 65 6e    if( p->pSegmen
461d3 74 73 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  ts ){..    rc = 
461d4 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 6f  sqlite3_blob_reo
461d5 70 65 6e 28 70 2d 3e 70 53 65 67 6d 65 6e 74 73  pen(p->pSegments
461d6 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0d 0a 20 20  , iBlockid);..  
461d7 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69 66 28 20  }else{..    if( 
461d8 30 3d 3d 70 2d 3e 7a 53 65 67 6d 65 6e 74 73 54  0==p->zSegmentsT
461d9 62 6c 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e  bl ){..      p->
461da 7a 53 65 67 6d 65 6e 74 73 54 62 6c 20 3d 20 73  zSegmentsTbl = s
461db 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
461dc 25 73 5f 73 65 67 6d 65 6e 74 73 22 2c 20 70 2d  %s_segments", p-
461dd 3e 7a 4e 61 6d 65 29 3b 0d 0a 20 20 20 20 20 20  >zName);..      
461de 69 66 28 20 30 3d 3d 70 2d 3e 7a 53 65 67 6d 65  if( 0==p->zSegme
461df 6e 74 73 54 62 6c 20 29 20 72 65 74 75 72 6e 20  ntsTbl ) return 
461e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
461e1 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73     }..    rc = s
461e2 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
461e3 28 0d 0a 20 20 20 20 20 20 20 70 2d 3e 64 62 2c  (..       p->db,
461e4 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 53 65 67   p->zDb, p->zSeg
461e5 6d 65 6e 74 73 54 62 6c 2c 20 22 62 6c 6f 63 6b  mentsTbl, "block
461e6 22 2c 20 69 42 6c 6f 63 6b 69 64 2c 20 30 2c 20  ", iBlockid, 0, 
461e7 26 70 2d 3e 70 53 65 67 6d 65 6e 74 73 0d 0a 20  &p->pSegments.. 
461e8 20 20 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20     );..  }....  
461e9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
461ea 4b 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 42  K ){..    int nB
461eb 79 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 62 6c  yte = sqlite3_bl
461ec 6f 62 5f 62 79 74 65 73 28 70 2d 3e 70 53 65 67  ob_bytes(p->pSeg
461ed 6d 65 6e 74 73 29 3b 0d 0a 20 20 20 20 2a 70 6e  ments);..    *pn
461ee 42 6c 6f 62 20 3d 20 6e 42 79 74 65 3b 0d 0a 20  Blob = nByte;.. 
461ef 20 20 20 69 66 28 20 70 61 42 6c 6f 62 20 29 7b     if( paBlob ){
461f0 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 61 42  ..      char *aB
461f1 79 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  yte = sqlite3_ma
461f2 6c 6c 6f 63 28 6e 42 79 74 65 20 2b 20 46 54 53  lloc(nByte + FTS
461f3 33 5f 4e 4f 44 45 5f 50 41 44 44 49 4e 47 29 3b  3_NODE_PADDING);
461f4 0d 0a 20 20 20 20 20 20 69 66 28 20 21 61 42 79  ..      if( !aBy
461f5 74 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  te ){..        r
461f6 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
461f7 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
461f8 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 6e 4c  .        if( pnL
461f9 6f 61 64 20 26 26 20 6e 42 79 74 65 3e 28 46 54  oad && nByte>(FT
461fa 53 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b 5f 54 48  S3_NODE_CHUNK_TH
461fb 52 45 53 48 4f 4c 44 29 20 29 7b 0d 0a 20 20 20  RESHOLD) ){..   
461fc 20 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 46         nByte = F
461fd 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b 53 49  TS3_NODE_CHUNKSI
461fe 5a 45 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a  ZE;..          *
461ff 70 6e 4c 6f 61 64 20 3d 20 6e 42 79 74 65 3b 0d  pnLoad = nByte;.
46200 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  .        }..    
46201 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
46202 5f 62 6c 6f 62 5f 72 65 61 64 28 70 2d 3e 70 53  _blob_read(p->pS
46203 65 67 6d 65 6e 74 73 2c 20 61 42 79 74 65 2c 20  egments, aByte, 
46204 6e 42 79 74 65 2c 20 30 29 3b 0d 0a 20 20 20 20  nByte, 0);..    
46205 20 20 20 20 6d 65 6d 73 65 74 28 26 61 42 79 74      memset(&aByt
46206 65 5b 6e 42 79 74 65 5d 2c 20 30 2c 20 46 54 53  e[nByte], 0, FTS
46207 33 5f 4e 4f 44 45 5f 50 41 44 44 49 4e 47 29 3b  3_NODE_PADDING);
46208 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ..        if( rc
46209 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  !=SQLITE_OK ){..
4620a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
4620b 33 5f 66 72 65 65 28 61 42 79 74 65 29 3b 0d 0a  3_free(aByte);..
4620c 20 20 20 20 20 20 20 20 20 20 61 42 79 74 65 20            aByte 
4620d 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  = 0;..        }.
4620e 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
4620f 2a 70 61 42 6c 6f 62 20 3d 20 61 42 79 74 65 3b  *paBlob = aByte;
46210 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
46211 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
46212 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f 73 65 20  .../*..** Close 
46213 74 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20  the blob handle 
46214 61 74 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 2c  at p->pSegments,
46215 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 2e 20   if it is open. 
46216 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
46217 76 65 0d 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  ve..** the sqlit
46218 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63 6b 28  e3Fts3ReadBlock(
46219 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64  ) function for d
4621a 65 74 61 69 6c 73 2e 0d 0a 2a 2f 0d 0a 53 51 4c  etails...*/..SQL
4621b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4621c 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 6d   sqlite3Fts3Segm
4621d 65 6e 74 73 43 6c 6f 73 65 28 46 74 73 33 54 61  entsClose(Fts3Ta
4621e 62 6c 65 20 2a 70 29 7b 0d 0a 20 20 73 71 6c 69  ble *p){..  sqli
4621f 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70  te3_blob_close(p
46220 2d 3e 70 53 65 67 6d 65 6e 74 73 29 3b 0d 0a 20  ->pSegments);.. 
46221 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 20 3d 20   p->pSegments = 
46222 30 3b 0d 0a 7d 0d 0a 20 20 20 20 0d 0a 73 74 61  0;..}..    ..sta
46223 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67 52  tic int fts3SegR
46224 65 61 64 65 72 49 6e 63 72 52 65 61 64 28 46 74  eaderIncrRead(Ft
46225 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65  s3SegReader *pRe
46226 61 64 65 72 29 7b 0d 0a 20 20 69 6e 74 20 6e 52  ader){..  int nR
46227 65 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ead;            
46228 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
46229 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
4622a 72 65 61 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 72  read */..  int r
4622b 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
4622c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
4622d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 0d 0a  turn code */....
4622e 20 20 6e 52 65 61 64 20 3d 20 4d 49 4e 28 70 52    nRead = MIN(pR
4622f 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 20 2d 20 70  eader->nNode - p
46230 52 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74  Reader->nPopulat
46231 65 2c 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48 55  e, FTS3_NODE_CHU
46232 4e 4b 53 49 5a 45 29 3b 0d 0a 20 20 72 63 20 3d  NKSIZE);..  rc =
46233 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
46234 61 64 28 0d 0a 20 20 20 20 20 20 70 52 65 61 64  ad(..      pRead
46235 65 72 2d 3e 70 42 6c 6f 62 2c 20 0d 0a 20 20 20  er->pBlob, ..   
46236 20 20 20 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f     &pReader->aNo
46237 64 65 5b 70 52 65 61 64 65 72 2d 3e 6e 50 6f 70  de[pReader->nPop
46238 75 6c 61 74 65 5d 2c 0d 0a 20 20 20 20 20 20 6e  ulate],..      n
46239 52 65 61 64 2c 0d 0a 20 20 20 20 20 20 70 52 65  Read,..      pRe
4623a 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74 65 0d  ader->nPopulate.
4623b 0a 20 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 72  .  );....  if( r
4623c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
4623d 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 50  .    pReader->nP
4623e 6f 70 75 6c 61 74 65 20 2b 3d 20 6e 52 65 61 64  opulate += nRead
4623f 3b 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70  ;..    memset(&p
46240 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52  Reader->aNode[pR
46241 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74 65  eader->nPopulate
46242 5d 2c 20 30 2c 20 46 54 53 33 5f 4e 4f 44 45 5f  ], 0, FTS3_NODE_
46243 50 41 44 44 49 4e 47 29 3b 0d 0a 20 20 20 20 69  PADDING);..    i
46244 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 50 6f 70  f( pReader->nPop
46245 75 6c 61 74 65 3d 3d 70 52 65 61 64 65 72 2d 3e  ulate==pReader->
46246 6e 4e 6f 64 65 20 29 7b 0d 0a 20 20 20 20 20 20  nNode ){..      
46247 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
46248 73 65 28 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f  se(pReader->pBlo
46249 62 29 3b 0d 0a 20 20 20 20 20 20 70 52 65 61 64  b);..      pRead
4624a 65 72 2d 3e 70 42 6c 6f 62 20 3d 20 30 3b 0d 0a  er->pBlob = 0;..
4624b 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e        pReader->n
4624c 50 6f 70 75 6c 61 74 65 20 3d 20 30 3b 0d 0a 20  Populate = 0;.. 
4624d 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
4624e 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 73 74  urn rc;..}....st
4624f 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
46250 52 65 61 64 65 72 52 65 71 75 69 72 65 28 46 74  ReaderRequire(Ft
46251 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65  s3SegReader *pRe
46252 61 64 65 72 2c 20 63 68 61 72 20 2a 70 46 72 6f  ader, char *pFro
46253 6d 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0d 0a  m, int nByte){..
46254 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
46255 45 5f 4f 4b 3b 0d 0a 20 20 61 73 73 65 72 74 28  E_OK;..  assert(
46256 20 21 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f 62   !pReader->pBlob
46257 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 46   ..       || (pF
46258 72 6f 6d 3e 3d 70 52 65 61 64 65 72 2d 3e 61 4e  rom>=pReader->aN
46259 6f 64 65 20 26 26 20 70 46 72 6f 6d 3c 26 70 52  ode && pFrom<&pR
4625a 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52 65  eader->aNode[pRe
4625b 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d 29 0d 0a 20  ader->nNode]).. 
4625c 20 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 52   );..  while( pR
4625d 65 61 64 65 72 2d 3e 70 42 6c 6f 62 20 26 26 20  eader->pBlob && 
4625e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0d 0a  rc==SQLITE_OK ..
4625f 20 20 20 20 20 26 26 20 20 28 70 46 72 6f 6d 20       &&  (pFrom 
46260 2d 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  - pReader->aNode
46261 20 2b 20 6e 42 79 74 65 29 3e 70 52 65 61 64 65   + nByte)>pReade
46262 72 2d 3e 6e 50 6f 70 75 6c 61 74 65 0d 0a 20 20  r->nPopulate..  
46263 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ){..    rc = fts
46264 33 53 65 67 52 65 61 64 65 72 49 6e 63 72 52 65  3SegReaderIncrRe
46265 61 64 28 70 52 65 61 64 65 72 29 3b 0d 0a 20 20  ad(pReader);..  
46266 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  }..  return rc;.
46267 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 6f 76  .}..../*..** Mov
46268 65 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 70  e the iterator p
46269 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
4626a 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
4626b 68 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20  he next term in 
4626c 74 68 65 0d 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e  the..** segment.
4626d 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
4626e 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
4626f 75 72 6e 65 64 2e 20 49 66 20 74 68 65 72 65 20  urned. If there 
46270 69 73 20 6e 6f 20 6e 65 78 74 20 74 65 72 6d 2c  is no next term,
46271 0d 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45  ..** SQLITE_DONE
46272 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20  . Otherwise, an 
46273 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
46274 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
46275 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65 72  nt fts3SegReader
46276 4e 65 78 74 28 0d 0a 20 20 46 74 73 33 54 61 62  Next(..  Fts3Tab
46277 6c 65 20 2a 70 2c 20 0d 0a 20 20 46 74 73 33 53  le *p, ..  Fts3S
46278 65 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  egReader *pReade
46279 72 2c 0d 0a 20 20 69 6e 74 20 62 49 6e 63 72 0d  r,..  int bIncr.
4627a 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20  .){..  int rc;  
4627b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4627c 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
4627d 20 63 6f 64 65 20 6f 66 20 76 61 72 69 6f 75 73   code of various
4627e 20 73 75 62 2d 72 6f 75 74 69 6e 65 73 20 2a 2f   sub-routines */
4627f 0d 0a 20 20 63 68 61 72 20 2a 70 4e 65 78 74 3b  ..  char *pNext;
46280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46281 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 76 61      /* Cursor va
46282 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74  riable */..  int
46283 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20   nPrefix;       
46284 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46285 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
46286 69 6e 20 74 65 72 6d 20 70 72 65 66 69 78 20 2a  in term prefix *
46287 2f 0d 0a 20 20 69 6e 74 20 6e 53 75 66 66 69 78  /..  int nSuffix
46288 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
46289 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4628a 66 20 62 79 74 65 73 20 69 6e 20 74 65 72 6d 20  f bytes in term 
4628b 73 75 66 66 69 78 20 2a 2f 0d 0a 0d 0a 20 20 69  suffix */....  i
4628c 66 28 20 21 70 52 65 61 64 65 72 2d 3e 61 44 6f  f( !pReader->aDo
4628d 63 6c 69 73 74 20 29 7b 0d 0a 20 20 20 20 70 4e  clist ){..    pN
4628e 65 78 74 20 3d 20 70 52 65 61 64 65 72 2d 3e 61  ext = pReader->a
4628f 4e 6f 64 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  Node;..  }else{.
46290 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 26 70 52  .    pNext = &pR
46291 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 5b  eader->aDoclist[
46292 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73  pReader->nDoclis
46293 74 5d 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66  t];..  }....  if
46294 28 20 21 70 4e 65 78 74 20 7c 7c 20 70 4e 65 78  ( !pNext || pNex
46295 74 3e 3d 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f  t>=&pReader->aNo
46296 64 65 5b 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64  de[pReader->nNod
46297 65 5d 20 29 7b 0d 0a 0d 0a 20 20 20 20 69 66 28  e] ){....    if(
46298 20 66 74 73 33 53 65 67 52 65 61 64 65 72 49 73   fts3SegReaderIs
46299 50 65 6e 64 69 6e 67 28 70 52 65 61 64 65 72 29  Pending(pReader)
4629a 20 29 7b 0d 0a 20 20 20 20 20 20 46 74 73 33 48   ){..      Fts3H
4629b 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 20 3d  ashElem *pElem =
4629c 20 2a 28 70 52 65 61 64 65 72 2d 3e 70 70 4e 65   *(pReader->ppNe
4629d 78 74 45 6c 65 6d 29 3b 0d 0a 20 20 20 20 20 20  xtElem);..      
4629e 69 66 28 20 70 45 6c 65 6d 3d 3d 30 20 29 7b 0d  if( pElem==0 ){.
4629f 0a 20 20 20 20 20 20 20 20 70 52 65 61 64 65 72  .        pReader
462a0 2d 3e 61 4e 6f 64 65 20 3d 20 30 3b 0d 0a 20 20  ->aNode = 0;..  
462a1 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
462a2 20 20 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20      PendingList 
462a3 2a 70 4c 69 73 74 20 3d 20 28 50 65 6e 64 69 6e  *pList = (Pendin
462a4 67 4c 69 73 74 20 2a 29 66 74 73 33 48 61 73 68  gList *)fts3Hash
462a5 44 61 74 61 28 70 45 6c 65 6d 29 3b 0d 0a 20 20  Data(pElem);..  
462a6 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 7a        pReader->z
462a7 54 65 72 6d 20 3d 20 28 63 68 61 72 20 2a 29 66  Term = (char *)f
462a8 74 73 33 48 61 73 68 4b 65 79 28 70 45 6c 65 6d  ts3HashKey(pElem
462a9 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 52 65 61  );..        pRea
462aa 64 65 72 2d 3e 6e 54 65 72 6d 20 3d 20 66 74 73  der->nTerm = fts
462ab 33 48 61 73 68 4b 65 79 73 69 7a 65 28 70 45 6c  3HashKeysize(pEl
462ac 65 6d 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 52  em);..        pR
462ad 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 20 3d 20 70  eader->nNode = p
462ae 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74  Reader->nDoclist
462af 20 3d 20 70 4c 69 73 74 2d 3e 6e 44 61 74 61 20   = pList->nData 
462b0 2b 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 70 52  + 1;..        pR
462b1 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 70  eader->aNode = p
462b2 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74  Reader->aDoclist
462b3 20 3d 20 70 4c 69 73 74 2d 3e 61 44 61 74 61 3b   = pList->aData;
462b4 0d 0a 20 20 20 20 20 20 20 20 70 52 65 61 64 65  ..        pReade
462b5 72 2d 3e 70 70 4e 65 78 74 45 6c 65 6d 2b 2b 3b  r->ppNextElem++;
462b6 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
462b7 28 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  ( pReader->aNode
462b8 20 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   );..      }..  
462b9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
462ba 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a  E_OK;..    }....
462bb 20 20 20 20 69 66 28 20 21 66 74 73 33 53 65 67      if( !fts3Seg
462bc 52 65 61 64 65 72 49 73 52 6f 6f 74 4f 6e 6c 79  ReaderIsRootOnly
462bd 28 70 52 65 61 64 65 72 29 20 29 7b 0d 0a 20 20  (pReader) ){..  
462be 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
462bf 28 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 29  (pReader->aNode)
462c0 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
462c1 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70 52 65 61  _blob_close(pRea
462c2 64 65 72 2d 3e 70 42 6c 6f 62 29 3b 0d 0a 20 20  der->pBlob);..  
462c3 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 42 6c      pReader->pBl
462c4 6f 62 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  ob = 0;..    }..
462c5 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f      pReader->aNo
462c6 64 65 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f  de = 0;....    /
462c7 2a 20 49 66 20 69 43 75 72 72 65 6e 74 42 6c 6f  * If iCurrentBlo
462c8 63 6b 3e 3d 69 4c 65 61 66 45 6e 64 42 6c 6f 63  ck>=iLeafEndBloc
462c9 6b 2c 20 74 68 69 73 20 69 73 20 61 6e 20 45 4f  k, this is an EO
462ca 46 20 63 6f 6e 64 69 74 69 6f 6e 2e 20 41 6c 6c  F condition. All
462cb 20 6c 65 61 66 20 0d 0a 20 20 20 20 2a 2a 20 62   leaf ..    ** b
462cc 6c 6f 63 6b 73 20 68 61 76 65 20 61 6c 72 65 61  locks have alrea
462cd 64 79 20 62 65 65 6e 20 74 72 61 76 65 72 73 65  dy been traverse
462ce 64 2e 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65  d.  */..    asse
462cf 72 74 28 20 70 52 65 61 64 65 72 2d 3e 69 43 75  rt( pReader->iCu
462d0 72 72 65 6e 74 42 6c 6f 63 6b 3c 3d 70 52 65 61  rrentBlock<=pRea
462d1 64 65 72 2d 3e 69 4c 65 61 66 45 6e 64 42 6c 6f  der->iLeafEndBlo
462d2 63 6b 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 70  ck );..    if( p
462d3 52 65 61 64 65 72 2d 3e 69 43 75 72 72 65 6e 74  Reader->iCurrent
462d4 42 6c 6f 63 6b 3e 3d 70 52 65 61 64 65 72 2d 3e  Block>=pReader->
462d5 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b 20 29 7b  iLeafEndBlock ){
462d6 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
462d7 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
462d8 0d 0a 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ....    rc = sql
462d9 69 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63  ite3Fts3ReadBloc
462da 6b 28 0d 0a 20 20 20 20 20 20 20 20 70 2c 20 2b  k(..        p, +
462db 2b 70 52 65 61 64 65 72 2d 3e 69 43 75 72 72 65  +pReader->iCurre
462dc 6e 74 42 6c 6f 63 6b 2c 20 26 70 52 65 61 64 65  ntBlock, &pReade
462dd 72 2d 3e 61 4e 6f 64 65 2c 20 26 70 52 65 61 64  r->aNode, &pRead
462de 65 72 2d 3e 6e 4e 6f 64 65 2c 20 0d 0a 20 20 20  er->nNode, ..   
462df 20 20 20 20 20 28 62 49 6e 63 72 20 3f 20 26 70       (bIncr ? &p
462e0 52 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74  Reader->nPopulat
462e1 65 20 3a 20 30 29 0d 0a 20 20 20 20 29 3b 0d 0a  e : 0)..    );..
462e2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
462e3 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
462e4 63 3b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  c;..    assert( 
462e5 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f 62 3d 3d  pReader->pBlob==
462e6 30 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 62 49  0 );..    if( bI
462e7 6e 63 72 20 26 26 20 70 52 65 61 64 65 72 2d 3e  ncr && pReader->
462e8 6e 50 6f 70 75 6c 61 74 65 3c 70 52 65 61 64 65  nPopulate<pReade
462e9 72 2d 3e 6e 4e 6f 64 65 20 29 7b 0d 0a 20 20 20  r->nNode ){..   
462ea 20 20 20 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f     pReader->pBlo
462eb 62 20 3d 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73  b = p->pSegments
462ec 3b 0d 0a 20 20 20 20 20 20 70 2d 3e 70 53 65 67  ;..      p->pSeg
462ed 6d 65 6e 74 73 20 3d 20 30 3b 0d 0a 20 20 20 20  ments = 0;..    
462ee 7d 0d 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  }..    pNext = p
462ef 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 3b 0d 0a  Reader->aNode;..
462f0 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28    }....  assert(
462f1 20 21 66 74 73 33 53 65 67 52 65 61 64 65 72 49   !fts3SegReaderI
462f2 73 50 65 6e 64 69 6e 67 28 70 52 65 61 64 65 72  sPending(pReader
462f3 29 20 29 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 66  ) );....  rc = f
462f4 74 73 33 53 65 67 52 65 61 64 65 72 52 65 71 75  ts3SegReaderRequ
462f5 69 72 65 28 70 52 65 61 64 65 72 2c 20 70 4e 65  ire(pReader, pNe
462f6 78 74 2c 20 46 54 53 33 5f 56 41 52 49 4e 54 5f  xt, FTS3_VARINT_
462f7 4d 41 58 2a 32 29 3b 0d 0a 20 20 69 66 28 20 72  MAX*2);..  if( r
462f8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
462f9 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 0d 0a 20  eturn rc;..  .. 
462fa 20 2f 2a 20 42 65 63 61 75 73 65 20 6f 66 20 74   /* Because of t
462fb 68 65 20 46 54 53 33 5f 4e 4f 44 45 5f 50 41 44  he FTS3_NODE_PAD
462fc 44 49 4e 47 20 62 79 74 65 73 20 6f 66 20 70 61  DING bytes of pa
462fd 64 64 69 6e 67 2c 20 74 68 65 20 66 6f 6c 6c 6f  dding, the follo
462fe 77 69 6e 67 20 69 73 20 0d 0a 20 20 2a 2a 20 73  wing is ..  ** s
462ff 61 66 65 20 28 6e 6f 20 72 69 73 6b 20 6f 66 20  afe (no risk of 
46300 6f 76 65 72 72 65 61 64 29 20 65 76 65 6e 20 69  overread) even i
46301 66 20 74 68 65 20 6e 6f 64 65 20 64 61 74 61 20  f the node data 
46302 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 2a 2f  is corrupted. */
46303 0d 0a 20 20 70 4e 65 78 74 20 2b 3d 20 73 71 6c  ..  pNext += sql
46304 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
46305 74 33 32 28 70 4e 65 78 74 2c 20 26 6e 50 72 65  t32(pNext, &nPre
46306 66 69 78 29 3b 0d 0a 20 20 70 4e 65 78 74 20 2b  fix);..  pNext +
46307 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
46308 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74 2c 20  Varint32(pNext, 
46309 26 6e 53 75 66 66 69 78 29 3b 0d 0a 20 20 69 66  &nSuffix);..  if
4630a 28 20 6e 50 72 65 66 69 78 3c 30 20 7c 7c 20 6e  ( nPrefix<0 || n
4630b 53 75 66 66 69 78 3c 3d 30 20 0d 0a 20 20 20 7c  Suffix<=0 ..   |
4630c 7c 20 26 70 4e 65 78 74 5b 6e 53 75 66 66 69 78  | &pNext[nSuffix
4630d 5d 3e 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64  ]>&pReader->aNod
4630e 65 5b 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65  e[pReader->nNode
4630f 5d 20 0d 0a 20 20 29 7b 0d 0a 20 20 20 20 72 65  ] ..  ){..    re
46310 74 75 72 6e 20 46 54 53 5f 43 4f 52 52 55 50 54  turn FTS_CORRUPT
46311 5f 56 54 41 42 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  _VTAB;..  }.... 
46312 20 69 66 28 20 6e 50 72 65 66 69 78 2b 6e 53 75   if( nPrefix+nSu
46313 66 66 69 78 3e 70 52 65 61 64 65 72 2d 3e 6e 54  ffix>pReader->nT
46314 65 72 6d 41 6c 6c 6f 63 20 29 7b 0d 0a 20 20 20  ermAlloc ){..   
46315 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 6e 50 72   int nNew = (nPr
46316 65 66 69 78 2b 6e 53 75 66 66 69 78 29 2a 32 3b  efix+nSuffix)*2;
46317 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77  ..    char *zNew
46318 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
46319 6f 63 28 70 52 65 61 64 65 72 2d 3e 7a 54 65 72  oc(pReader->zTer
4631a 6d 2c 20 6e 4e 65 77 29 3b 0d 0a 20 20 20 20 69  m, nNew);..    i
4631b 66 28 20 21 7a 4e 65 77 20 29 7b 0d 0a 20 20 20  f( !zNew ){..   
4631c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
4631d 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d 0a  _NOMEM;..    }..
4631e 20 20 20 20 70 52 65 61 64 65 72 2d 3e 7a 54 65      pReader->zTe
4631f 72 6d 20 3d 20 7a 4e 65 77 3b 0d 0a 20 20 20 20  rm = zNew;..    
46320 70 52 65 61 64 65 72 2d 3e 6e 54 65 72 6d 41 6c  pReader->nTermAl
46321 6c 6f 63 20 3d 20 6e 4e 65 77 3b 0d 0a 20 20 7d  loc = nNew;..  }
46322 0d 0a 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 53  ....  rc = fts3S
46323 65 67 52 65 61 64 65 72 52 65 71 75 69 72 65 28  egReaderRequire(
46324 70 52 65 61 64 65 72 2c 20 70 4e 65 78 74 2c 20  pReader, pNext, 
46325 6e 53 75 66 66 69 78 2b 46 54 53 33 5f 56 41 52  nSuffix+FTS3_VAR
46326 49 4e 54 5f 4d 41 58 29 3b 0d 0a 20 20 69 66 28  INT_MAX);..  if(
46327 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
46328 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20   return rc;.... 
46329 20 6d 65 6d 63 70 79 28 26 70 52 65 61 64 65 72   memcpy(&pReader
4632a 2d 3e 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d  ->zTerm[nPrefix]
4632b 2c 20 70 4e 65 78 74 2c 20 6e 53 75 66 66 69 78  , pNext, nSuffix
4632c 29 3b 0d 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e  );..  pReader->n
4632d 54 65 72 6d 20 3d 20 6e 50 72 65 66 69 78 2b 6e  Term = nPrefix+n
4632e 53 75 66 66 69 78 3b 0d 0a 20 20 70 4e 65 78 74  Suffix;..  pNext
4632f 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0d 0a 20 20   += nSuffix;..  
46330 70 4e 65 78 74 20 2b 3d 20 73 71 6c 69 74 65 33  pNext += sqlite3
46331 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
46332 70 4e 65 78 74 2c 20 26 70 52 65 61 64 65 72 2d  pNext, &pReader-
46333 3e 6e 44 6f 63 6c 69 73 74 29 3b 0d 0a 20 20 70  >nDoclist);..  p
46334 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74  Reader->aDoclist
46335 20 3d 20 70 4e 65 78 74 3b 0d 0a 20 20 70 52 65   = pNext;..  pRe
46336 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73  ader->pOffsetLis
46337 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f 2a 20 43  t = 0;....  /* C
46338 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 64 6f  heck that the do
46339 63 6c 69 73 74 20 64 6f 65 73 20 6e 6f 74 20 61  clist does not a
4633a 70 70 65 61 72 20 74 6f 20 65 78 74 65 6e 64 20  ppear to extend 
4633b 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
4633c 74 68 65 0d 0a 20 20 2a 2a 20 62 2d 74 72 65 65  the..  ** b-tree
4633d 20 6e 6f 64 65 2e 20 41 6e 64 20 74 68 61 74 20   node. And that 
4633e 74 68 65 20 66 69 6e 61 6c 20 62 79 74 65 20 6f  the final byte o
4633f 66 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73  f the doclist is
46340 20 30 78 30 30 2e 20 49 66 20 65 69 74 68 65 72   0x00. If either
46341 20 0d 0a 20 20 2a 2a 20 6f 66 20 74 68 65 73 65   ..  ** of these
46342 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 75   statements is u
46343 6e 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20  ntrue, then the 
46344 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 69  data structure i
46345 73 20 63 6f 72 72 75 70 74 2e 0d 0a 20 20 2a 2f  s corrupt...  */
46346 0d 0a 20 20 69 66 28 20 26 70 52 65 61 64 65 72  ..  if( &pReader
46347 2d 3e 61 44 6f 63 6c 69 73 74 5b 70 52 65 61 64  ->aDoclist[pRead
46348 65 72 2d 3e 6e 44 6f 63 6c 69 73 74 5d 3e 26 70  er->nDoclist]>&p
46349 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52  Reader->aNode[pR
4634a 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d 20 0d 0a  eader->nNode] ..
4634b 20 20 20 7c 7c 20 28 70 52 65 61 64 65 72 2d 3e     || (pReader->
4634c 6e 50 6f 70 75 6c 61 74 65 3d 3d 30 20 26 26 20  nPopulate==0 && 
4634d 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73  pReader->aDoclis
4634e 74 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c  t[pReader->nDocl
4634f 69 73 74 2d 31 5d 29 0d 0a 20 20 29 7b 0d 0a 20  ist-1])..  ){.. 
46350 20 20 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f     return FTS_CO
46351 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 7d  RRUPT_VTAB;..  }
46352 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
46353 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
46354 2a 2a 20 53 65 74 20 74 68 65 20 53 65 67 52 65  ** Set the SegRe
46355 61 64 65 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f  ader to point to
46356 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64   the first docid
46357 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20   in the doclist 
46358 61 73 73 6f 63 69 61 74 65 64 0d 0a 2a 2a 20 77  associated..** w
46359 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ith the current 
4635a 74 65 72 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  term...*/..stati
4635b 63 20 69 6e 74 20 66 74 73 33 53 65 67 52 65 61  c int fts3SegRea
4635c 64 65 72 46 69 72 73 74 44 6f 63 69 64 28 46 74  derFirstDocid(Ft
4635d 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 46  s3Table *pTab, F
4635e 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
4635f 65 61 64 65 72 29 7b 0d 0a 20 20 69 6e 74 20 72  eader){..  int r
46360 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
46361 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
46362 72 2d 3e 61 44 6f 63 6c 69 73 74 20 29 3b 0d 0a  r->aDoclist );..
46363 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61 64    assert( !pRead
46364 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20  er->pOffsetList 
46365 29 3b 0d 0a 20 20 69 66 28 20 70 54 61 62 2d 3e  );..  if( pTab->
46366 62 44 65 73 63 49 64 78 20 26 26 20 66 74 73 33  bDescIdx && fts3
46367 53 65 67 52 65 61 64 65 72 49 73 50 65 6e 64 69  SegReaderIsPendi
46368 6e 67 28 70 52 65 61 64 65 72 29 20 29 7b 0d 0a  ng(pReader) ){..
46369 20 20 20 20 75 38 20 62 45 6f 66 20 3d 20 30 3b      u8 bEof = 0;
4636a 0d 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69  ..    pReader->i
4636b 44 6f 63 69 64 20 3d 20 30 3b 0d 0a 20 20 20 20  Docid = 0;..    
4636c 70 52 65 61 64 65 72 2d 3e 6e 4f 66 66 73 65 74  pReader->nOffset
4636d 4c 69 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20 73  List = 0;..    s
4636e 71 6c 69 74 65 33 46 74 73 33 44 6f 63 6c 69 73  qlite3Fts3Doclis
4636f 74 50 72 65 76 28 30 2c 0d 0a 20 20 20 20 20 20  tPrev(0,..      
46370 20 20 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c    pReader->aDocl
46371 69 73 74 2c 20 70 52 65 61 64 65 72 2d 3e 6e 44  ist, pReader->nD
46372 6f 63 6c 69 73 74 2c 20 26 70 52 65 61 64 65 72  oclist, &pReader
46373 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 2c 20 0d  ->pOffsetList, .
46374 0a 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65  .        &pReade
46375 72 2d 3e 69 44 6f 63 69 64 2c 20 26 70 52 65 61  r->iDocid, &pRea
46376 64 65 72 2d 3e 6e 4f 66 66 73 65 74 4c 69 73 74  der->nOffsetList
46377 2c 20 26 62 45 6f 66 0d 0a 20 20 20 20 29 3b 0d  , &bEof..    );.
46378 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72  .  }else{..    r
46379 63 20 3d 20 66 74 73 33 53 65 67 52 65 61 64 65  c = fts3SegReade
4637a 72 52 65 71 75 69 72 65 28 70 52 65 61 64 65 72  rRequire(pReader
4637b 2c 20 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c  , pReader->aDocl
4637c 69 73 74 2c 20 46 54 53 33 5f 56 41 52 49 4e 54  ist, FTS3_VARINT
4637d 5f 4d 41 58 29 3b 0d 0a 20 20 20 20 69 66 28 20  _MAX);..    if( 
4637e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
4637f 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  ..      int n = 
46380 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
46381 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 61 44  rint(pReader->aD
46382 6f 63 6c 69 73 74 2c 20 26 70 52 65 61 64 65 72  oclist, &pReader
46383 2d 3e 69 44 6f 63 69 64 29 3b 0d 0a 20 20 20 20  ->iDocid);..    
46384 20 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73    pReader->pOffs
46385 65 74 4c 69 73 74 20 3d 20 26 70 52 65 61 64 65  etList = &pReade
46386 72 2d 3e 61 44 6f 63 6c 69 73 74 5b 6e 5d 3b 0d  r->aDoclist[n];.
46387 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72  .    }..  }..  r
46388 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
46389 2f 2a 0d 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74  /*..** Advance t
4638a 68 65 20 53 65 67 52 65 61 64 65 72 20 74 6f 20  he SegReader to 
4638b 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78  point to the nex
4638c 74 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64  t docid in the d
4638d 6f 63 6c 69 73 74 0d 0a 2a 2a 20 61 73 73 6f 63  oclist..** assoc
4638e 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 63  iated with the c
4638f 75 72 72 65 6e 74 20 74 65 72 6d 2e 0d 0a 2a 2a  urrent term...**
46390 20 0d 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e   ..** If argumen
46391 74 73 20 70 70 4f 66 66 73 65 74 4c 69 73 74 20  ts ppOffsetList 
46392 61 6e 64 20 70 6e 4f 66 66 73 65 74 4c 69 73 74  and pnOffsetList
46393 20 61 72 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74   are not NULL, t
46394 68 65 6e 20 0d 0a 2a 2a 20 2a 70 70 4f 66 66 73  hen ..** *ppOffs
46395 65 74 4c 69 73 74 20 69 73 20 73 65 74 20 74 6f  etList is set to
46396 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69   point to the fi
46397 72 73 74 20 63 6f 6c 75 6d 6e 2d 6f 66 66 73 65  rst column-offse
46398 74 20 6c 69 73 74 0d 0a 2a 2a 20 69 6e 20 74 68  t list..** in th
46399 65 20 64 6f 63 6c 69 73 74 20 65 6e 74 72 79 20  e doclist entry 
4639a 28 69 2e 65 2e 20 69 6d 6d 65 64 69 61 74 65 6c  (i.e. immediatel
4639b 79 20 70 61 73 74 20 74 68 65 20 64 6f 63 69 64  y past the docid
4639c 20 76 61 72 69 6e 74 29 2e 0d 0a 2a 2a 20 2a 70   varint)...** *p
4639d 6e 4f 66 66 73 65 74 4c 69 73 74 20 69 73 20 73  nOffsetList is s
4639e 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68  et to the length
4639f 20 6f 66 20 74 68 65 20 73 65 74 20 6f 66 20 63   of the set of c
463a0 6f 6c 75 6d 6e 2d 6f 66 66 73 65 74 0d 0a 2a 2a  olumn-offset..**
463a1 20 6c 69 73 74 73 2c 20 6e 6f 74 20 69 6e 63 6c   lists, not incl
463a2 75 64 69 6e 67 20 74 68 65 20 6e 75 6c 2d 74 65  uding the nul-te
463a3 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 2e 20 46  rminator byte. F
463a4 6f 72 20 65 78 61 6d 70 6c 65 3a 0d 0a 2a 2f 0d  or example:..*/.
463a5 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
463a6 53 65 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63  SegReaderNextDoc
463a7 69 64 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65  id(..  Fts3Table
463a8 20 2a 70 54 61 62 2c 0d 0a 20 20 46 74 73 33 53   *pTab,..  Fts3S
463a9 65 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  egReader *pReade
463aa 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  r,         /* Re
463ab 61 64 65 72 20 74 6f 20 61 64 76 61 6e 63 65 20  ader to advance 
463ac 74 6f 20 6e 65 78 74 20 64 6f 63 69 64 20 2a 2f  to next docid */
463ad 0d 0a 20 20 63 68 61 72 20 2a 2a 70 70 4f 66 66  ..  char **ppOff
463ae 73 65 74 4c 69 73 74 2c 20 20 20 20 20 20 20 20  setList,        
463af 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e      /* OUT: Poin
463b0 74 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 70  ter to current p
463b1 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 2a 2f 0d  osition-list */.
463b2 0a 20 20 69 6e 74 20 2a 70 6e 4f 66 66 73 65 74  .  int *pnOffset
463b3 4c 69 73 74 20 20 20 20 20 20 20 20 20 20 20 20  List            
463b4 20 20 20 2f 2a 20 4f 55 54 3a 20 4c 65 6e 67 74     /* OUT: Lengt
463b5 68 20 6f 66 20 2a 70 70 4f 66 66 73 65 74 4c 69  h of *ppOffsetLi
463b6 73 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  st in bytes */..
463b7 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
463b8 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 63 68 61  QLITE_OK;..  cha
463b9 72 20 2a 70 20 3d 20 70 52 65 61 64 65 72 2d 3e  r *p = pReader->
463ba 70 4f 66 66 73 65 74 4c 69 73 74 3b 0d 0a 20 20  pOffsetList;..  
463bb 63 68 61 72 20 63 20 3d 20 30 3b 0d 0a 0d 0a 20  char c = 0;.... 
463bc 20 61 73 73 65 72 74 28 20 70 20 29 3b 0d 0a 0d   assert( p );...
463bd 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 62 44 65  .  if( pTab->bDe
463be 73 63 49 64 78 20 26 26 20 66 74 73 33 53 65 67  scIdx && fts3Seg
463bf 52 65 61 64 65 72 49 73 50 65 6e 64 69 6e 67 28  ReaderIsPending(
463c0 70 52 65 61 64 65 72 29 20 29 7b 0d 0a 20 20 20  pReader) ){..   
463c1 20 2f 2a 20 41 20 70 65 6e 64 69 6e 67 2d 74 65   /* A pending-te
463c2 72 6d 73 20 73 65 67 2d 72 65 61 64 65 72 20 66  rms seg-reader f
463c3 6f 72 20 61 6e 20 46 54 53 34 20 74 61 62 6c 65  or an FTS4 table
463c4 20 74 68 61 74 20 75 73 65 73 20 6f 72 64 65 72   that uses order
463c5 3d 64 65 73 63 2e 0d 0a 20 20 20 20 2a 2a 20 50  =desc...    ** P
463c6 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 64 6f 63  ending-terms doc
463c7 6c 69 73 74 73 20 61 72 65 20 61 6c 77 61 79 73  lists are always
463c8 20 62 75 69 6c 74 20 75 70 20 69 6e 20 61 73 63   built up in asc
463c9 65 6e 64 69 6e 67 20 6f 72 64 65 72 2c 20 73 6f  ending order, so
463ca 0d 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65  ..    ** we have
463cb 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
463cc 75 67 68 20 74 68 65 6d 20 62 61 63 6b 77 61 72  ugh them backwar
463cd 64 73 20 68 65 72 65 2e 20 2a 2f 0d 0a 20 20 20  ds here. */..   
463ce 20 75 38 20 62 45 6f 66 20 3d 20 30 3b 0d 0a 20   u8 bEof = 0;.. 
463cf 20 20 20 69 66 28 20 70 70 4f 66 66 73 65 74 4c     if( ppOffsetL
463d0 69 73 74 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70  ist ){..      *p
463d1 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 70 52  pOffsetList = pR
463d2 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
463d3 73 74 3b 0d 0a 20 20 20 20 20 20 2a 70 6e 4f 66  st;..      *pnOf
463d4 66 73 65 74 4c 69 73 74 20 3d 20 70 52 65 61 64  fsetList = pRead
463d5 65 72 2d 3e 6e 4f 66 66 73 65 74 4c 69 73 74 20  er->nOffsetList 
463d6 2d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20  - 1;..    }..   
463d7 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f 63 6c   sqlite3Fts3Docl
463d8 69 73 74 50 72 65 76 28 30 2c 0d 0a 20 20 20 20  istPrev(0,..    
463d9 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61 44 6f      pReader->aDo
463da 63 6c 69 73 74 2c 20 70 52 65 61 64 65 72 2d 3e  clist, pReader->
463db 6e 44 6f 63 6c 69 73 74 2c 20 26 70 2c 20 26 70  nDoclist, &p, &p
463dc 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 2c 0d  Reader->iDocid,.
463dd 0a 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65  .        &pReade
463de 72 2d 3e 6e 4f 66 66 73 65 74 4c 69 73 74 2c 20  r->nOffsetList, 
463df 26 62 45 6f 66 0d 0a 20 20 20 20 29 3b 0d 0a 20  &bEof..    );.. 
463e0 20 20 20 69 66 28 20 62 45 6f 66 20 29 7b 0d 0a     if( bEof ){..
463e1 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70        pReader->p
463e2 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 30 3b 0d  OffsetList = 0;.
463e3 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  .    }else{..   
463e4 20 20 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66     pReader->pOff
463e5 73 65 74 4c 69 73 74 20 3d 20 70 3b 0d 0a 20 20  setList = p;..  
463e6 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20    }..  }else{.. 
463e7 20 20 20 63 68 61 72 20 2a 70 45 6e 64 20 3d 20     char *pEnd = 
463e8 26 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69  &pReader->aDocli
463e9 73 74 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63  st[pReader->nDoc
463ea 6c 69 73 74 5d 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  list];....    /*
463eb 20 50 6f 69 6e 74 65 72 20 70 20 63 75 72 72 65   Pointer p curre
463ec 6e 74 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 74  ntly points at t
463ed 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  he first byte of
463ee 20 61 6e 20 6f 66 66 73 65 74 20 6c 69 73 74 2e   an offset list.
463ef 20 54 68 65 0d 0a 20 20 20 20 2a 2a 20 66 6f 6c   The..    ** fol
463f0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 61 64 76  lowing block adv
463f1 61 6e 63 65 73 20 69 74 20 74 6f 20 70 6f 69 6e  ances it to poin
463f2 74 20 6f 6e 65 20 62 79 74 65 20 70 61 73 74 20  t one byte past 
463f3 74 68 65 20 65 6e 64 20 6f 66 0d 0a 20 20 20 20  the end of..    
463f4 2a 2a 20 74 68 65 20 73 61 6d 65 20 6f 66 66 73  ** the same offs
463f5 65 74 20 6c 69 73 74 2e 20 2a 2f 0d 0a 20 20 20  et list. */..   
463f6 20 77 68 69 6c 65 28 20 31 20 29 7b 0d 0a 20 20   while( 1 ){..  
463f7 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  ..      /* The f
463f8 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 20 6f 66  ollowing line of
463f9 20 63 6f 64 65 20 28 61 6e 64 20 74 68 65 20 22   code (and the "
463fa 70 2b 2b 22 20 62 65 6c 6f 77 20 74 68 65 20 77  p++" below the w
463fb 68 69 6c 65 28 29 20 6c 6f 6f 70 29 20 69 73 0d  hile() loop) is.
463fc 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c  .      ** normal
463fd 6c 79 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72  ly all that is r
463fe 65 71 75 69 72 65 64 20 74 6f 20 6d 6f 76 65 20  equired to move 
463ff 70 6f 69 6e 74 65 72 20 70 20 74 6f 20 74 68 65  pointer p to the
46400 20 64 65 73 69 72 65 64 20 0d 0a 20 20 20 20 20   desired ..     
46401 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 2e 20 54 68   ** position. Th
46402 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69  e exception is i
46403 66 20 74 68 69 73 20 6e 6f 64 65 20 69 73 20 62  f this node is b
46404 65 69 6e 67 20 6c 6f 61 64 65 64 20 66 72 6f 6d  eing loaded from
46405 20 64 69 73 6b 0d 0a 20 20 20 20 20 20 2a 2a 20   disk..      ** 
46406 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 61 6e  incrementally an
46407 64 20 70 6f 69 6e 74 65 72 20 22 70 22 20 6e 6f  d pointer "p" no
46408 77 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  w points to the 
46409 66 69 72 73 74 20 62 79 74 65 20 70 61 73 73 65  first byte passe
4640a 64 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  d..      ** the 
4640b 70 6f 70 75 6c 61 74 65 64 20 70 61 72 74 20 6f  populated part o
4640c 66 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  f pReader->aNode
4640d 5b 5d 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20  []...      */.. 
4640e 20 20 20 20 20 77 68 69 6c 65 28 20 2a 70 20 7c       while( *p |
4640f 20 63 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26 20   c ) c = *p++ & 
46410 30 78 38 30 3b 0d 0a 20 20 20 20 20 20 61 73 73  0x80;..      ass
46411 65 72 74 28 20 2a 70 3d 3d 30 20 29 3b 0d 0a 20  ert( *p==0 );.. 
46412 20 0d 0a 20 20 20 20 20 20 69 66 28 20 70 52 65   ..      if( pRe
46413 61 64 65 72 2d 3e 70 42 6c 6f 62 3d 3d 30 20 7c  ader->pBlob==0 |
46414 7c 20 70 3c 26 70 52 65 61 64 65 72 2d 3e 61 4e  | p<&pReader->aN
46415 6f 64 65 5b 70 52 65 61 64 65 72 2d 3e 6e 50 6f  ode[pReader->nPo
46416 70 75 6c 61 74 65 5d 20 29 20 62 72 65 61 6b 3b  pulate] ) break;
46417 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ..      rc = fts
46418 33 53 65 67 52 65 61 64 65 72 49 6e 63 72 52 65  3SegReaderIncrRe
46419 61 64 28 70 52 65 61 64 65 72 29 3b 0d 0a 20 20  ad(pReader);..  
4641a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
4641b 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
4641c 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70  c;..    }..    p
4641d 2b 2b 3b 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a 20  ++;..  ..    /* 
4641e 49 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70  If required, pop
4641f 75 6c 61 74 65 20 74 68 65 20 6f 75 74 70 75 74  ulate the output
46420 20 76 61 72 69 61 62 6c 65 73 20 77 69 74 68 20   variables with 
46421 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 64  a pointer to and
46422 20 74 68 65 0d 0a 20 20 20 20 2a 2a 20 73 69 7a   the..    ** siz
46423 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  e of the previou
46424 73 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e 0d 0a  s offset-list...
46425 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20      */..    if( 
46426 70 70 4f 66 66 73 65 74 4c 69 73 74 20 29 7b 0d  ppOffsetList ){.
46427 0a 20 20 20 20 20 20 2a 70 70 4f 66 66 73 65 74  .      *ppOffset
46428 4c 69 73 74 20 3d 20 70 52 65 61 64 65 72 2d 3e  List = pReader->
46429 70 4f 66 66 73 65 74 4c 69 73 74 3b 0d 0a 20 20  pOffsetList;..  
4642a 20 20 20 20 2a 70 6e 4f 66 66 73 65 74 4c 69 73      *pnOffsetLis
4642b 74 20 3d 20 28 69 6e 74 29 28 70 20 2d 20 70 52  t = (int)(p - pR
4642c 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
4642d 73 74 20 2d 20 31 29 3b 0d 0a 20 20 20 20 7d 0d  st - 1);..    }.
4642e 0a 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 70 3c  ...    while( p<
4642f 70 45 6e 64 20 26 26 20 2a 70 3d 3d 30 20 29 20  pEnd && *p==0 ) 
46430 70 2b 2b 3b 0d 0a 20 20 0d 0a 20 20 20 20 2f 2a  p++;..  ..    /*
46431 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
46432 20 6d 6f 72 65 20 65 6e 74 72 69 65 73 20 69 6e   more entries in
46433 20 74 68 65 20 64 6f 63 6c 69 73 74 2c 20 73 65   the doclist, se
46434 74 20 70 4f 66 66 73 65 74 4c 69 73 74 20 74 6f  t pOffsetList to
46435 0d 0a 20 20 20 20 2a 2a 20 4e 55 4c 4c 2e 20 4f  ..    ** NULL. O
46436 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 46 74  therwise, set Ft
46437 73 33 53 65 67 52 65 61 64 65 72 2e 69 44 6f 63  s3SegReader.iDoc
46438 69 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64  id to the next d
46439 6f 63 69 64 20 61 6e 64 0d 0a 20 20 20 20 2a 2a  ocid and..    **
4643a 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 70   Fts3SegReader.p
4643b 4f 66 66 73 65 74 4c 69 73 74 20 74 6f 20 70 6f  OffsetList to po
4643c 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20  int to the next 
4643d 6f 66 66 73 65 74 20 6c 69 73 74 20 62 65 66 6f  offset list befo
4643e 72 65 0d 0a 20 20 20 20 2a 2a 20 72 65 74 75 72  re..    ** retur
4643f 6e 69 6e 67 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  ning...    */.. 
46440 20 20 20 69 66 28 20 70 3e 3d 70 45 6e 64 20 29     if( p>=pEnd )
46441 7b 0d 0a 20 20 20 20 20 20 70 52 65 61 64 65 72  {..      pReader
46442 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20  ->pOffsetList = 
46443 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  0;..    }else{..
46444 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53        rc = fts3S
46445 65 67 52 65 61 64 65 72 52 65 71 75 69 72 65 28  egReaderRequire(
46446 70 52 65 61 64 65 72 2c 20 70 2c 20 46 54 53 33  pReader, p, FTS3
46447 5f 56 41 52 49 4e 54 5f 4d 41 58 29 3b 0d 0a 20  _VARINT_MAX);.. 
46448 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
46449 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
4644a 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
4644b 20 69 44 65 6c 74 61 3b 0d 0a 20 20 20 20 20 20   iDelta;..      
4644c 20 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73    pReader->pOffs
4644d 65 74 4c 69 73 74 20 3d 20 70 20 2b 20 73 71 6c  etList = p + sql
4644e 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
4644f 74 28 70 2c 20 26 69 44 65 6c 74 61 29 3b 0d 0a  t(p, &iDelta);..
46450 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62          if( pTab
46451 2d 3e 62 44 65 73 63 49 64 78 20 29 7b 0d 0a 20  ->bDescIdx ){.. 
46452 20 20 20 20 20 20 20 20 20 70 52 65 61 64 65 72           pReader
46453 2d 3e 69 44 6f 63 69 64 20 2d 3d 20 69 44 65 6c  ->iDocid -= iDel
46454 74 61 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c  ta;..        }el
46455 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 70  se{..          p
46456 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 20 2b  Reader->iDocid +
46457 3d 20 69 44 65 6c 74 61 3b 0d 0a 20 20 20 20 20  = iDelta;..     
46458 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20     }..      }.. 
46459 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72     }..  }....  r
4645a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
4645b 0d 0a 7d 0d 0a 0d 0a 0d 0a 53 51 4c 49 54 45 5f  ..}......SQLITE_
4645c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4645d 74 65 33 46 74 73 33 4d 73 72 4f 76 66 6c 28 0d  te3Fts3MsrOvfl(.
4645e 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
4645f 43 73 72 2c 20 0d 0a 20 20 46 74 73 33 4d 75 6c  Csr, ..  Fts3Mul
46460 74 69 53 65 67 52 65 61 64 65 72 20 2a 70 4d 73  tiSegReader *pMs
46461 72 2c 0d 0a 20 20 69 6e 74 20 2a 70 6e 4f 76 66  r,..  int *pnOvf
46462 6c 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 54 61 62  l..){..  Fts3Tab
46463 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62  le *p = (Fts3Tab
46464 6c 65 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70  le*)pCsr->base.p
46465 56 74 61 62 3b 0d 0a 20 20 69 6e 74 20 6e 4f 76  Vtab;..  int nOv
46466 66 6c 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69  fl = 0;..  int i
46467 69 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  i;..  int rc = S
46468 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74  QLITE_OK;..  int
46469 20 70 67 73 7a 20 3d 20 70 2d 3e 6e 50 67 73 7a   pgsz = p->nPgsz
4646a 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ;....  assert( p
4646b 2d 3e 62 48 61 73 53 74 61 74 20 29 3b 0d 0a 20  ->bHasStat );.. 
4646c 20 61 73 73 65 72 74 28 20 70 67 73 7a 3e 30 20   assert( pgsz>0 
4646d 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69 3d 30  );....  for(ii=0
4646e 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
4646f 26 26 20 69 69 3c 70 4d 73 72 2d 3e 6e 53 65 67  && ii<pMsr->nSeg
46470 6d 65 6e 74 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20  ment; ii++){..  
46471 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
46472 2a 70 52 65 61 64 65 72 20 3d 20 70 4d 73 72 2d  *pReader = pMsr-
46473 3e 61 70 53 65 67 6d 65 6e 74 5b 69 69 5d 3b 0d  >apSegment[ii];.
46474 0a 20 20 20 20 69 66 28 20 21 66 74 73 33 53 65  .    if( !fts3Se
46475 67 52 65 61 64 65 72 49 73 50 65 6e 64 69 6e 67  gReaderIsPending
46476 28 70 52 65 61 64 65 72 29 20 0d 0a 20 20 20 20  (pReader) ..    
46477 20 26 26 20 21 66 74 73 33 53 65 67 52 65 61 64   && !fts3SegRead
46478 65 72 49 73 52 6f 6f 74 4f 6e 6c 79 28 70 52 65  erIsRootOnly(pRe
46479 61 64 65 72 29 20 0d 0a 20 20 20 20 29 7b 0d 0a  ader) ..    ){..
4647a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
4647b 74 36 34 20 6a 6a 3b 0d 0a 20 20 20 20 20 20 66  t64 jj;..      f
4647c 6f 72 28 6a 6a 3d 70 52 65 61 64 65 72 2d 3e 69  or(jj=pReader->i
4647d 53 74 61 72 74 42 6c 6f 63 6b 3b 20 6a 6a 3c 3d  StartBlock; jj<=
4647e 70 52 65 61 64 65 72 2d 3e 69 4c 65 61 66 45 6e  pReader->iLeafEn
4647f 64 42 6c 6f 63 6b 3b 20 6a 6a 2b 2b 29 7b 0d 0a  dBlock; jj++){..
46480 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f          int nBlo
46481 62 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  b;..        rc =
46482 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61 64   sqlite3Fts3Read
46483 42 6c 6f 63 6b 28 70 2c 20 6a 6a 2c 20 30 2c 20  Block(p, jj, 0, 
46484 26 6e 42 6c 6f 62 2c 20 30 29 3b 0d 0a 20 20 20  &nBlob, 0);..   
46485 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
46486 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0d  ITE_OK ) break;.
46487 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6e 42  .        if( (nB
46488 6c 6f 62 2b 33 35 29 3e 70 67 73 7a 20 29 7b 0d  lob+35)>pgsz ){.
46489 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 76 66 6c  .          nOvfl
4648a 20 2b 3d 20 28 6e 42 6c 6f 62 20 2b 20 33 34 29   += (nBlob + 34)
4648b 2f 70 67 73 7a 3b 0d 0a 20 20 20 20 20 20 20 20  /pgsz;..        
4648c 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
4648d 7d 0d 0a 20 20 7d 0d 0a 20 20 2a 70 6e 4f 76 66  }..  }..  *pnOvf
4648e 6c 20 3d 20 6e 4f 76 66 6c 3b 0d 0a 20 20 72 65  l = nOvfl;..  re
4648f 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
46490 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 61  *..** Free all a
46491 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73 6f 63  llocations assoc
46492 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 69  iated with the i
46493 74 65 72 61 74 6f 72 20 70 61 73 73 65 64 20 61  terator passed a
46494 73 20 74 68 65 20 0d 0a 2a 2a 20 73 65 63 6f 6e  s the ..** secon
46495 64 20 61 72 67 75 6d 65 6e 74 2e 0d 0a 2a 2f 0d  d argument...*/.
46496 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
46497 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
46498 53 65 67 52 65 61 64 65 72 46 72 65 65 28 46 74  SegReaderFree(Ft
46499 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65  s3SegReader *pRe
4649a 61 64 65 72 29 7b 0d 0a 20 20 69 66 28 20 70 52  ader){..  if( pR
4649b 65 61 64 65 72 20 26 26 20 21 66 74 73 33 53 65  eader && !fts3Se
4649c 67 52 65 61 64 65 72 49 73 50 65 6e 64 69 6e 67  gReaderIsPending
4649d 28 70 52 65 61 64 65 72 29 20 29 7b 0d 0a 20 20  (pReader) ){..  
4649e 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
4649f 52 65 61 64 65 72 2d 3e 7a 54 65 72 6d 29 3b 0d  Reader->zTerm);.
464a0 0a 20 20 20 20 69 66 28 20 21 66 74 73 33 53 65  .    if( !fts3Se
464a1 67 52 65 61 64 65 72 49 73 52 6f 6f 74 4f 6e 6c  gReaderIsRootOnl
464a2 79 28 70 52 65 61 64 65 72 29 20 29 7b 0d 0a 20  y(pReader) ){.. 
464a3 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
464a4 65 28 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  e(pReader->aNode
464a5 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
464a6 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70 52 65  3_blob_close(pRe
464a7 61 64 65 72 2d 3e 70 42 6c 6f 62 29 3b 0d 0a 20  ader->pBlob);.. 
464a8 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c     }..  }..  sql
464a9 69 74 65 33 5f 66 72 65 65 28 70 52 65 61 64 65  ite3_free(pReade
464aa 72 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  r);..}..../*..**
464ab 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
464ac 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74  SegReader object
464ad 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52  ...*/..SQLITE_PR
464ae 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
464af 33 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65  3Fts3SegReaderNe
464b0 77 28 0d 0a 20 20 69 6e 74 20 69 41 67 65 2c 20  w(..  int iAge, 
464b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
464b2 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74        /* Segment
464b3 20 22 61 67 65 22 2e 20 2a 2f 0d 0a 20 20 73 71   "age". */..  sq
464b4 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74 61  lite3_int64 iSta
464b5 72 74 4c 65 61 66 2c 20 20 20 20 20 20 20 2f 2a  rtLeaf,       /*
464b6 20 46 69 72 73 74 20 6c 65 61 66 20 74 6f 20 74   First leaf to t
464b7 72 61 76 65 72 73 65 20 2a 2f 0d 0a 20 20 73 71  raverse */..  sq
464b8 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45 6e 64  lite3_int64 iEnd
464b9 4c 65 61 66 2c 20 20 20 20 20 20 20 20 20 2f 2a  Leaf,         /*
464ba 20 46 69 6e 61 6c 20 6c 65 61 66 20 74 6f 20 74   Final leaf to t
464bb 72 61 76 65 72 73 65 20 2a 2f 0d 0a 20 20 73 71  raverse */..  sq
464bc 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45 6e 64  lite3_int64 iEnd
464bd 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 2f 2a  Block,        /*
464be 20 46 69 6e 61 6c 20 62 6c 6f 63 6b 20 6f 66 20   Final block of 
464bf 73 65 67 6d 65 6e 74 20 2a 2f 0d 0a 20 20 63 6f  segment */..  co
464c0 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f 6f 74 2c  nst char *zRoot,
464c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
464c2 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   Buffer containi
464c3 6e 67 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0d  ng root node */.
464c4 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20  .  int nRoot,   
464c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
464c6 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
464c7 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
464c8 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20  root node */..  
464c9 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
464ca 70 70 52 65 61 64 65 72 20 20 20 20 20 20 20 20  ppReader        
464cb 2f 2a 20 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65  /* OUT: Allocate
464cc 64 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  d Fts3SegReader 
464cd 2a 2f 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 53 65  */..){..  Fts3Se
464ce 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  gReader *pReader
464cf 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77  ;         /* New
464d0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 53 65 67  ly allocated Seg
464d1 52 65 61 64 65 72 20 6f 62 6a 65 63 74 20 2a 2f  Reader object */
464d2 0d 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d  ..  int nExtra =
464d3 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
464d4 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20      /* Bytes to 
464d5 61 6c 6c 6f 63 61 74 65 20 73 65 67 6d 65 6e 74  allocate segment
464d6 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0d 0a 0d   root node */...
464d7 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72  .  assert( iStar
464d8 74 4c 65 61 66 3c 3d 69 45 6e 64 4c 65 61 66 20  tLeaf<=iEndLeaf 
464d9 29 3b 0d 0a 20 20 69 66 28 20 69 53 74 61 72 74  );..  if( iStart
464da 4c 65 61 66 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Leaf==0 ){..    
464db 6e 45 78 74 72 61 20 3d 20 6e 52 6f 6f 74 20 2b  nExtra = nRoot +
464dc 20 46 54 53 33 5f 4e 4f 44 45 5f 50 41 44 44 49   FTS3_NODE_PADDI
464dd 4e 47 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 52  NG;..  }....  pR
464de 65 61 64 65 72 20 3d 20 28 46 74 73 33 53 65 67  eader = (Fts3Seg
464df 52 65 61 64 65 72 20 2a 29 73 71 6c 69 74 65 33  Reader *)sqlite3
464e0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
464e1 74 73 33 53 65 67 52 65 61 64 65 72 29 20 2b 20  ts3SegReader) + 
464e2 6e 45 78 74 72 61 29 3b 0d 0a 20 20 69 66 28 20  nExtra);..  if( 
464e3 21 70 52 65 61 64 65 72 20 29 7b 0d 0a 20 20 20  !pReader ){..   
464e4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
464e5 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65  OMEM;..  }..  me
464e6 6d 73 65 74 28 70 52 65 61 64 65 72 2c 20 30 2c  mset(pReader, 0,
464e7 20 73 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52   sizeof(Fts3SegR
464e8 65 61 64 65 72 29 29 3b 0d 0a 20 20 70 52 65 61  eader));..  pRea
464e9 64 65 72 2d 3e 69 49 64 78 20 3d 20 69 41 67 65  der->iIdx = iAge
464ea 3b 0d 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 53  ;..  pReader->iS
464eb 74 61 72 74 42 6c 6f 63 6b 20 3d 20 69 53 74 61  tartBlock = iSta
464ec 72 74 4c 65 61 66 3b 0d 0a 20 20 70 52 65 61 64  rtLeaf;..  pRead
464ed 65 72 2d 3e 69 4c 65 61 66 45 6e 64 42 6c 6f 63  er->iLeafEndBloc
464ee 6b 20 3d 20 69 45 6e 64 4c 65 61 66 3b 0d 0a 20  k = iEndLeaf;.. 
464ef 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 42 6c   pReader->iEndBl
464f0 6f 63 6b 20 3d 20 69 45 6e 64 42 6c 6f 63 6b 3b  ock = iEndBlock;
464f1 0d 0a 0d 0a 20 20 69 66 28 20 6e 45 78 74 72 61  ....  if( nExtra
464f2 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68 65 20   ){..    /* The 
464f3 65 6e 74 69 72 65 20 73 65 67 6d 65 6e 74 20 69  entire segment i
464f4 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
464f5 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0d 0a 20  root node. */.. 
464f6 20 20 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64     pReader->aNod
464f7 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65  e = (char *)&pRe
464f8 61 64 65 72 5b 31 5d 3b 0d 0a 20 20 20 20 70 52  ader[1];..    pR
464f9 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 20 3d 20 6e  eader->nNode = n
464fa 52 6f 6f 74 3b 0d 0a 20 20 20 20 6d 65 6d 63 70  Root;..    memcp
464fb 79 28 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  y(pReader->aNode
464fc 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f 6f 74 29 3b  , zRoot, nRoot);
464fd 0d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 52  ..    memset(&pR
464fe 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 6e 52 6f  eader->aNode[nRo
464ff 6f 74 5d 2c 20 30 2c 20 46 54 53 33 5f 4e 4f 44  ot], 0, FTS3_NOD
46500 45 5f 50 41 44 44 49 4e 47 29 3b 0d 0a 20 20 7d  E_PADDING);..  }
46501 65 6c 73 65 7b 0d 0a 20 20 20 20 70 52 65 61 64  else{..    pRead
46502 65 72 2d 3e 69 43 75 72 72 65 6e 74 42 6c 6f 63  er->iCurrentBloc
46503 6b 20 3d 20 69 53 74 61 72 74 4c 65 61 66 2d 31  k = iStartLeaf-1
46504 3b 0d 0a 20 20 7d 0d 0a 20 20 2a 70 70 52 65 61  ;..  }..  *ppRea
46505 64 65 72 20 3d 20 70 52 65 61 64 65 72 3b 0d 0a  der = pReader;..
46506 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
46507 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
46508 20 54 68 69 73 20 69 73 20 61 20 63 6f 6d 70 61   This is a compa
46509 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75  rison function u
4650a 73 65 64 20 61 73 20 61 20 71 73 6f 72 74 28 29  sed as a qsort()
4650b 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 73   callback when s
4650c 6f 72 74 69 6e 67 0d 0a 2a 2a 20 61 6e 20 61 72  orting..** an ar
4650d 72 61 79 20 6f 66 20 70 65 6e 64 69 6e 67 20 74  ray of pending t
4650e 65 72 6d 73 20 62 79 20 74 65 72 6d 2e 20 54 68  erms by term. Th
4650f 69 73 20 6f 63 63 75 72 73 20 61 73 20 70 61 72  is occurs as par
46510 74 20 6f 66 20 66 6c 75 73 68 69 6e 67 0d 0a 2a  t of flushing..*
46511 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  * the contents o
46512 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65  f the pending-te
46513 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 20 74  rms hash table t
46514 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d  o the database..
46515 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
46516 66 74 73 33 43 6f 6d 70 61 72 65 45 6c 65 6d 42  fts3CompareElemB
46517 79 54 65 72 6d 28 63 6f 6e 73 74 20 76 6f 69 64  yTerm(const void
46518 20 2a 6c 68 73 2c 20 63 6f 6e 73 74 20 76 6f 69   *lhs, const voi
46519 64 20 2a 72 68 73 29 7b 0d 0a 20 20 63 68 61 72  d *rhs){..  char
4651a 20 2a 7a 31 20 3d 20 66 74 73 33 48 61 73 68 4b   *z1 = fts3HashK
4651b 65 79 28 2a 28 46 74 73 33 48 61 73 68 45 6c 65  ey(*(Fts3HashEle
4651c 6d 20 2a 2a 29 6c 68 73 29 3b 0d 0a 20 20 63 68  m **)lhs);..  ch
4651d 61 72 20 2a 7a 32 20 3d 20 66 74 73 33 48 61 73  ar *z2 = fts3Has
4651e 68 4b 65 79 28 2a 28 46 74 73 33 48 61 73 68 45  hKey(*(Fts3HashE
4651f 6c 65 6d 20 2a 2a 29 72 68 73 29 3b 0d 0a 20 20  lem **)rhs);..  
46520 69 6e 74 20 6e 31 20 3d 20 66 74 73 33 48 61 73  int n1 = fts3Has
46521 68 4b 65 79 73 69 7a 65 28 2a 28 46 74 73 33 48  hKeysize(*(Fts3H
46522 61 73 68 45 6c 65 6d 20 2a 2a 29 6c 68 73 29 3b  ashElem **)lhs);
46523 0d 0a 20 20 69 6e 74 20 6e 32 20 3d 20 66 74 73  ..  int n2 = fts
46524 33 48 61 73 68 4b 65 79 73 69 7a 65 28 2a 28 46  3HashKeysize(*(F
46525 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 29 72  ts3HashElem **)r
46526 68 73 29 3b 0d 0a 0d 0a 20 20 69 6e 74 20 6e 20  hs);....  int n 
46527 3d 20 28 6e 31 3c 6e 32 20 3f 20 6e 31 20 3a 20  = (n1<n2 ? n1 : 
46528 6e 32 29 3b 0d 0a 20 20 69 6e 74 20 63 20 3d 20  n2);..  int c = 
46529 6d 65 6d 63 6d 70 28 7a 31 2c 20 7a 32 2c 20 6e  memcmp(z1, z2, n
4652a 29 3b 0d 0a 20 20 69 66 28 20 63 3d 3d 30 20 29  );..  if( c==0 )
4652b 7b 0d 0a 20 20 20 20 63 20 3d 20 6e 31 20 2d 20  {..    c = n1 - 
4652c 6e 32 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  n2;..  }..  retu
4652d 72 6e 20 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn c;..}..../*..
4652e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
4652f 20 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f   is used to allo
46530 63 61 74 65 20 61 6e 20 46 74 73 33 53 65 67 52  cate an Fts3SegR
46531 65 61 64 65 72 20 74 68 61 74 20 69 74 65 72 61  eader that itera
46532 74 65 73 20 74 68 72 6f 75 67 68 0d 0a 2a 2a 20  tes through..** 
46533 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 20  a subset of the 
46534 74 65 72 6d 73 20 73 74 6f 72 65 64 20 69 6e 20  terms stored in 
46535 74 68 65 20 46 74 73 33 54 61 62 6c 65 2e 70 65  the Fts3Table.pe
46536 6e 64 69 6e 67 54 65 72 6d 73 20 61 72 72 61 79  ndingTerms array
46537 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
46538 20 69 73 50 72 65 66 69 78 49 74 65 72 20 70 61   isPrefixIter pa
46539 72 61 6d 65 74 65 72 20 69 73 20 7a 65 72 6f 2c  rameter is zero,
4653a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
4653b 65 64 20 53 65 67 52 65 61 64 65 72 20 69 74 65  ed SegReader ite
4653c 72 61 74 65 73 0d 0a 2a 2a 20 74 68 72 6f 75 67  rates..** throug
4653d 68 20 65 61 63 68 20 74 65 72 6d 20 69 6e 20 74  h each term in t
4653e 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73  he pending-terms
4653f 20 74 61 62 6c 65 2e 20 4f 72 2c 20 69 66 20 69   table. Or, if i
46540 73 50 72 65 66 69 78 49 74 65 72 20 69 73 0d 0a  sPrefixIter is..
46541 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 69 74 20  ** non-zero, it 
46542 69 74 65 72 61 74 65 73 20 74 68 72 6f 75 67 68  iterates through
46543 20 65 61 63 68 20 74 65 72 6d 20 61 6e 64 20 69   each term and i
46544 74 73 20 70 72 65 66 69 78 65 73 2e 20 46 6f 72  ts prefixes. For
46545 20 65 78 61 6d 70 6c 65 2c 20 69 66 0d 0a 2a 2a   example, if..**
46546 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65 72   the pending ter
46547 6d 73 20 68 61 73 68 20 74 61 62 6c 65 20 63 6f  ms hash table co
46548 6e 74 61 69 6e 73 20 74 68 65 20 74 65 72 6d 73  ntains the terms
46549 20 22 73 71 6c 69 74 65 22 2c 20 22 6d 79 73 71   "sqlite", "mysq
4654a 6c 22 20 61 6e 64 0d 0a 2a 2a 20 22 66 69 72 65  l" and..** "fire
4654b 62 69 72 64 22 2c 20 74 68 65 6e 20 74 68 65 20  bird", then the 
4654c 69 74 65 72 61 74 6f 72 20 76 69 73 69 74 73 20  iterator visits 
4654d 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 27 74  the following 't
4654e 65 72 6d 73 27 20 28 69 6e 20 74 68 65 20 6f 72  erms' (in the or
4654f 64 65 72 0d 0a 2a 2a 20 73 68 6f 77 6e 29 3a 0d  der..** shown):.
46550 0a 2a 2a 0d 0a 2a 2a 20 20 20 66 20 66 69 20 66  .**..**   f fi f
46551 69 72 20 66 69 72 65 20 66 69 72 65 62 20 66 69  ir fire fireb fi
46552 72 65 62 69 20 66 69 72 65 62 69 72 20 66 69 72  rebi firebir fir
46553 65 62 69 72 64 0d 0a 2a 2a 20 20 20 6d 20 6d 79  ebird..**   m my
46554 20 6d 79 73 20 6d 79 73 71 20 6d 79 73 71 6c 0d   mys mysq mysql.
46555 0a 2a 2a 20 20 20 73 20 73 71 20 73 71 6c 20 73  .**   s sq sql s
46556 71 6c 69 20 73 71 6c 69 74 20 73 71 6c 69 74 65  qli sqlit sqlite
46557 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 72 65 61 73  ..**..** Whereas
46558 20 69 66 20 69 73 50 72 65 66 69 78 49 74 65 72   if isPrefixIter
46559 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 74 65   is zero, the te
4655a 72 6d 73 20 76 69 73 69 74 65 64 20 61 72 65 3a  rms visited are:
4655b 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 66 69 72 65 62  ..**..**   fireb
4655c 69 72 64 20 6d 79 73 71 6c 20 73 71 6c 69 74 65  ird mysql sqlite
4655d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
4655e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4655f 46 74 73 33 53 65 67 52 65 61 64 65 72 50 65 6e  Fts3SegReaderPen
46560 64 69 6e 67 28 0d 0a 20 20 46 74 73 33 54 61 62  ding(..  Fts3Tab
46561 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
46562 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
46563 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
46564 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 49 6e 64 65   */..  int iInde
46565 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
46566 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
46567 66 6f 72 20 70 2d 3e 61 49 6e 64 65 78 20 2a 2f  for p->aIndex */
46568 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
46569 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  zTerm,          
4656a 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 73      /* Term to s
4656b 65 61 72 63 68 20 66 6f 72 20 2a 2f 0d 0a 20 20  earch for */..  
4656c 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20 20 20  int nTerm,      
4656d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4656e 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65  /* Size of buffe
4656f 72 20 7a 54 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e  r zTerm */..  in
46570 74 20 62 50 72 65 66 69 78 2c 20 20 20 20 20 20  t bPrefix,      
46571 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46572 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65 66   True for a pref
46573 69 78 20 69 74 65 72 61 74 6f 72 20 2a 2f 0d 0a  ix iterator */..
46574 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
46575 2a 2a 70 70 52 65 61 64 65 72 20 20 20 20 20 20  **ppReader      
46576 20 20 2f 2a 20 4f 55 54 3a 20 53 65 67 52 65 61    /* OUT: SegRea
46577 64 65 72 20 66 6f 72 20 70 65 6e 64 69 6e 67 2d  der for pending-
46578 74 65 72 6d 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  terms */..){..  
46579 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70  Fts3SegReader *p
4657a 52 65 61 64 65 72 20 3d 20 30 3b 20 20 20 20 20  Reader = 0;     
4657b 2f 2a 20 46 74 73 33 53 65 67 52 65 61 64 65 72  /* Fts3SegReader
4657c 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 74 75 72   object to retur
4657d 6e 20 2a 2f 0d 0a 20 20 46 74 73 33 48 61 73 68  n */..  Fts3Hash
4657e 45 6c 65 6d 20 2a 70 45 3b 20 20 20 20 20 20 20  Elem *pE;       
4657f 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
46580 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d  tor variable */.
46581 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  .  Fts3HashElem 
46582 2a 2a 61 45 6c 65 6d 20 3d 20 30 3b 20 20 20 20  **aElem = 0;    
46583 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 74     /* Array of t
46584 65 72 6d 20 68 61 73 68 20 65 6e 74 72 69 65 73  erm hash entries
46585 20 74 6f 20 73 63 61 6e 20 2a 2f 0d 0a 20 20 69   to scan */..  i
46586 6e 74 20 6e 45 6c 65 6d 20 3d 20 30 3b 20 20 20  nt nElem = 0;   
46587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
46588 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20  * Size of array 
46589 61 74 20 61 45 6c 65 6d 20 2a 2f 0d 0a 20 20 69  at aElem */..  i
4658a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
4658b 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
4658c 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f  * Return Code */
4658d 0d 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48  ..  Fts3Hash *pH
4658e 61 73 68 3b 0d 0a 0d 0a 20 20 70 48 61 73 68 20  ash;....  pHash 
4658f 3d 20 26 70 2d 3e 61 49 6e 64 65 78 5b 69 49 6e  = &p->aIndex[iIn
46590 64 65 78 5d 2e 68 50 65 6e 64 69 6e 67 3b 0d 0a  dex].hPending;..
46591 20 20 69 66 28 20 62 50 72 65 66 69 78 20 29 7b    if( bPrefix ){
46592 0d 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63  ..    int nAlloc
46593 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
46594 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
46595 6c 6c 6f 63 61 74 65 64 20 61 72 72 61 79 20 61  llocated array a
46596 74 20 61 45 6c 65 6d 20 2a 2f 0d 0a 0d 0a 20 20  t aElem */....  
46597 20 20 66 6f 72 28 70 45 3d 66 74 73 33 48 61 73    for(pE=fts3Has
46598 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70  hFirst(pHash); p
46599 45 3b 20 70 45 3d 66 74 73 33 48 61 73 68 4e 65  E; pE=fts3HashNe
4659a 78 74 28 70 45 29 29 7b 0d 0a 20 20 20 20 20 20  xt(pE)){..      
4659b 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 28 63 68  char *zKey = (ch
4659c 61 72 20 2a 29 66 74 73 33 48 61 73 68 4b 65 79  ar *)fts3HashKey
4659d 28 70 45 29 3b 0d 0a 20 20 20 20 20 20 69 6e 74  (pE);..      int
4659e 20 6e 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68   nKey = fts3Hash
4659f 4b 65 79 73 69 7a 65 28 70 45 29 3b 0d 0a 20 20  Keysize(pE);..  
465a0 20 20 20 20 69 66 28 20 6e 54 65 72 6d 3d 3d 30      if( nTerm==0
465a1 20 7c 7c 20 28 6e 4b 65 79 3e 3d 6e 54 65 72 6d   || (nKey>=nTerm
465a2 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 4b   && 0==memcmp(zK
465a3 65 79 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  ey, zTerm, nTerm
465a4 29 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 69  )) ){..        i
465a5 66 28 20 6e 45 6c 65 6d 3d 3d 6e 41 6c 6c 6f 63  f( nElem==nAlloc
465a6 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 46   ){..          F
465a7 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 61 45  ts3HashElem **aE
465a8 6c 65 6d 32 3b 0d 0a 20 20 20 20 20 20 20 20 20  lem2;..         
465a9 20 6e 41 6c 6c 6f 63 20 2b 3d 20 31 36 3b 0d 0a   nAlloc += 16;..
465aa 20 20 20 20 20 20 20 20 20 20 61 45 6c 65 6d 32            aElem2
465ab 20 3d 20 28 46 74 73 33 48 61 73 68 45 6c 65 6d   = (Fts3HashElem
465ac 20 2a 2a 29 73 71 6c 69 74 65 33 5f 72 65 61 6c   **)sqlite3_real
465ad 6c 6f 63 28 0d 0a 20 20 20 20 20 20 20 20 20 20  loc(..          
465ae 20 20 20 20 61 45 6c 65 6d 2c 20 6e 41 6c 6c 6f      aElem, nAllo
465af 63 2a 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73  c*sizeof(Fts3Has
465b0 68 45 6c 65 6d 20 2a 29 0d 0a 20 20 20 20 20 20  hElem *)..      
465b1 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 20 20      );..        
465b2 20 20 69 66 28 20 21 61 45 6c 65 6d 32 20 29 7b    if( !aElem2 ){
465b3 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  ..            rc
465b4 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
465b5 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 45  ..            nE
465b6 6c 65 6d 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20  lem = 0;..      
465b7 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20        break;..  
465b8 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
465b9 20 20 20 20 20 61 45 6c 65 6d 20 3d 20 61 45 6c       aElem = aEl
465ba 65 6d 32 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d  em2;..        }.
465bb 0a 0d 0a 20 20 20 20 20 20 20 20 61 45 6c 65 6d  ...        aElem
465bc 5b 6e 45 6c 65 6d 2b 2b 5d 20 3d 20 70 45 3b 0d  [nElem++] = pE;.
465bd 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
465be 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 6d 6f 72  ...    /* If mor
465bf 65 20 74 68 61 6e 20 6f 6e 65 20 74 65 72 6d 20  e than one term 
465c0 6d 61 74 63 68 65 73 20 74 68 65 20 70 72 65 66  matches the pref
465c1 69 78 2c 20 73 6f 72 74 20 74 68 65 20 46 74 73  ix, sort the Fts
465c2 33 48 61 73 68 45 6c 65 6d 0d 0a 20 20 20 20 2a  3HashElem..    *
465c3 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 65 72  * objects in ter
465c4 6d 20 6f 72 64 65 72 20 75 73 69 6e 67 20 71 73  m order using qs
465c5 6f 72 74 28 29 2e 20 54 68 69 73 20 75 73 65 73  ort(). This uses
465c6 20 74 68 65 20 73 61 6d 65 20 63 6f 6d 70 61 72   the same compar
465c7 69 73 6f 6e 0d 0a 20 20 20 20 2a 2a 20 63 61 6c  ison..    ** cal
465c8 6c 62 61 63 6b 20 61 73 20 69 73 20 75 73 65 64  lback as is used
465c9 20 77 68 65 6e 20 66 6c 75 73 68 69 6e 67 20 74   when flushing t
465ca 65 72 6d 73 20 74 6f 20 64 69 73 6b 2e 0d 0a 20  erms to disk... 
465cb 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 6e     */..    if( n
465cc 45 6c 65 6d 3e 31 20 29 7b 0d 0a 20 20 20 20 20  Elem>1 ){..     
465cd 20 71 73 6f 72 74 28 61 45 6c 65 6d 2c 20 6e 45   qsort(aElem, nE
465ce 6c 65 6d 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  lem, sizeof(Fts3
465cf 48 61 73 68 45 6c 65 6d 20 2a 29 2c 20 66 74 73  HashElem *), fts
465d0 33 43 6f 6d 70 61 72 65 45 6c 65 6d 42 79 54 65  3CompareElemByTe
465d1 72 6d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  rm);..    }.... 
465d2 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20   }else{..    /* 
465d3 54 68 65 20 71 75 65 72 79 20 69 73 20 61 20 73  The query is a s
465d4 69 6d 70 6c 65 20 74 65 72 6d 20 6c 6f 6f 6b 75  imple term looku
465d5 70 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 61  p that matches a
465d6 74 20 6d 6f 73 74 20 6f 6e 65 20 74 65 72 6d 20  t most one term 
465d7 69 6e 0d 0a 20 20 20 20 2a 2a 20 74 68 65 20 69  in..    ** the i
465d8 6e 64 65 78 2e 20 41 6c 6c 20 74 68 61 74 20 69  ndex. All that i
465d9 73 20 72 65 71 75 69 72 65 64 20 69 73 20 61 20  s required is a 
465da 73 74 72 61 69 67 68 74 20 68 61 73 68 2d 6c 6f  straight hash-lo
465db 6f 6b 75 70 2e 20 0d 0a 20 20 20 20 2a 2a 0d 0a  okup. ..    **..
465dc 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
465dd 68 65 20 73 74 61 63 6b 20 61 64 64 72 65 73 73  he stack address
465de 20 6f 66 20 70 45 20 6d 61 79 20 62 65 20 61 63   of pE may be ac
465df 63 65 73 73 65 64 20 76 69 61 20 74 68 65 20 61  cessed via the a
465e0 45 6c 65 6d 20 70 6f 69 6e 74 65 72 0d 0a 20 20  Elem pointer..  
465e1 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 74 68 65 20    ** below, the 
465e2 22 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70  "Fts3HashElem *p
465e3 45 22 20 6d 75 73 74 20 62 65 20 64 65 63 6c 61  E" must be decla
465e4 72 65 64 20 73 6f 20 74 68 61 74 20 69 74 20 69  red so that it i
465e5 73 20 76 61 6c 69 64 0d 0a 20 20 20 20 2a 2a 20  s valid..    ** 
465e6 77 69 74 68 69 6e 20 74 68 69 73 20 65 6e 74 69  within this enti
465e7 72 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74  re function, not
465e8 20 6a 75 73 74 20 74 68 69 73 20 22 65 6c 73 65   just this "else
465e9 7b 2e 2e 2e 7d 22 20 62 6c 6f 63 6b 2e 0d 0a 20  {...}" block... 
465ea 20 20 20 2a 2f 0d 0a 20 20 20 20 70 45 20 3d 20     */..    pE = 
465eb 66 74 73 33 48 61 73 68 46 69 6e 64 45 6c 65 6d  fts3HashFindElem
465ec 28 70 48 61 73 68 2c 20 7a 54 65 72 6d 2c 20 6e  (pHash, zTerm, n
465ed 54 65 72 6d 29 3b 0d 0a 20 20 20 20 69 66 28 20  Term);..    if( 
465ee 70 45 20 29 7b 0d 0a 20 20 20 20 20 20 61 45 6c  pE ){..      aEl
465ef 65 6d 20 3d 20 26 70 45 3b 0d 0a 20 20 20 20 20  em = &pE;..     
465f0 20 6e 45 6c 65 6d 20 3d 20 31 3b 0d 0a 20 20 20   nElem = 1;..   
465f1 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28   }..  }....  if(
465f2 20 6e 45 6c 65 6d 3e 30 20 29 7b 0d 0a 20 20 20   nElem>0 ){..   
465f3 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a   int nByte = siz
465f4 65 6f 66 28 46 74 73 33 53 65 67 52 65 61 64 65  eof(Fts3SegReade
465f5 72 29 20 2b 20 28 6e 45 6c 65 6d 2b 31 29 2a 73  r) + (nElem+1)*s
465f6 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68 45 6c  izeof(Fts3HashEl
465f7 65 6d 20 2a 29 3b 0d 0a 20 20 20 20 70 52 65 61  em *);..    pRea
465f8 64 65 72 20 3d 20 28 46 74 73 33 53 65 67 52 65  der = (Fts3SegRe
465f9 61 64 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  ader *)sqlite3_m
465fa 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0d 0a 20  alloc(nByte);.. 
465fb 20 20 20 69 66 28 20 21 70 52 65 61 64 65 72 20     if( !pReader 
465fc 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ){..      rc = S
465fd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
465fe 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
465ff 6d 65 6d 73 65 74 28 70 52 65 61 64 65 72 2c 20  memset(pReader, 
46600 30 2c 20 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20  0, nByte);..    
46601 20 20 70 52 65 61 64 65 72 2d 3e 69 49 64 78 20    pReader->iIdx 
46602 3d 20 30 78 37 46 46 46 46 46 46 46 3b 0d 0a 20  = 0x7FFFFFFF;.. 
46603 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 70       pReader->pp
46604 4e 65 78 74 45 6c 65 6d 20 3d 20 28 46 74 73 33  NextElem = (Fts3
46605 48 61 73 68 45 6c 65 6d 20 2a 2a 29 26 70 52 65  HashElem **)&pRe
46606 61 64 65 72 5b 31 5d 3b 0d 0a 20 20 20 20 20 20  ader[1];..      
46607 6d 65 6d 63 70 79 28 70 52 65 61 64 65 72 2d 3e  memcpy(pReader->
46608 70 70 4e 65 78 74 45 6c 65 6d 2c 20 61 45 6c 65  ppNextElem, aEle
46609 6d 2c 20 6e 45 6c 65 6d 2a 73 69 7a 65 6f 66 28  m, nElem*sizeof(
4660a 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 29 29  Fts3HashElem *))
4660b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
4660c 0a 20 20 69 66 28 20 62 50 72 65 66 69 78 20 29  .  if( bPrefix )
4660d 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  {..    sqlite3_f
4660e 72 65 65 28 61 45 6c 65 6d 29 3b 0d 0a 20 20 7d  ree(aElem);..  }
4660f 0d 0a 20 20 2a 70 70 52 65 61 64 65 72 20 3d 20  ..  *ppReader = 
46610 70 52 65 61 64 65 72 3b 0d 0a 20 20 72 65 74 75  pReader;..  retu
46611 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
46612 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
46613 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 65 64 20  entries pointed 
46614 74 6f 20 62 79 20 74 77 6f 20 46 74 73 33 53 65  to by two Fts3Se
46615 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75 72  gReader structur
46616 65 73 2e 20 0d 0a 2a 2a 20 43 6f 6d 70 61 72 69  es. ..** Compari
46617 73 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  son is as follow
46618 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 31 29 20  s:..**..**   1) 
46619 45 4f 46 20 69 73 20 67 72 65 61 74 65 72 20 74  EOF is greater t
4661a 68 61 6e 20 6e 6f 74 20 45 4f 46 2e 0d 0a 2a 2a  han not EOF...**
4661b 0d 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 63 75  ..**   2) The cu
4661c 72 72 65 6e 74 20 74 65 72 6d 73 20 28 69 66 20  rrent terms (if 
4661d 61 6e 79 29 20 61 72 65 20 63 6f 6d 70 61 72 65  any) are compare
4661e 64 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29  d using memcmp()
4661f 2e 20 49 66 20 6f 6e 65 0d 0a 2a 2a 20 20 20 20  . If one..**    
46620 20 20 74 65 72 6d 20 69 73 20 61 20 70 72 65 66    term is a pref
46621 69 78 20 6f 66 20 61 6e 6f 74 68 65 72 2c 20 74  ix of another, t
46622 68 65 20 6c 6f 6e 67 65 72 20 74 65 72 6d 20 69  he longer term i
46623 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 68 65  s considered the
46624 0d 0a 2a 2a 20 20 20 20 20 20 6c 61 72 67 65 72  ..**      larger
46625 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 29 20 42  ...**..**   3) B
46626 79 20 73 65 67 6d 65 6e 74 20 61 67 65 2e 20 41  y segment age. A
46627 6e 20 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74 20  n older segment 
46628 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 61  is considered la
46629 72 67 65 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  rger...*/..stati
4662a 63 20 69 6e 74 20 66 74 73 33 53 65 67 52 65 61  c int fts3SegRea
4662b 64 65 72 43 6d 70 28 46 74 73 33 53 65 67 52 65  derCmp(Fts3SegRe
4662c 61 64 65 72 20 2a 70 4c 68 73 2c 20 46 74 73 33  ader *pLhs, Fts3
4662d 53 65 67 52 65 61 64 65 72 20 2a 70 52 68 73 29  SegReader *pRhs)
4662e 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20  {..  int rc;..  
4662f 69 66 28 20 70 4c 68 73 2d 3e 61 4e 6f 64 65 20  if( pLhs->aNode 
46630 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65 20 29  && pRhs->aNode )
46631 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d  {..    int rc2 =
46632 20 70 4c 68 73 2d 3e 6e 54 65 72 6d 20 2d 20 70   pLhs->nTerm - p
46633 52 68 73 2d 3e 6e 54 65 72 6d 3b 0d 0a 20 20 20  Rhs->nTerm;..   
46634 20 69 66 28 20 72 63 32 3c 30 20 29 7b 0d 0a 20   if( rc2<0 ){.. 
46635 20 20 20 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70       rc = memcmp
46636 28 70 4c 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 52  (pLhs->zTerm, pR
46637 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 4c 68 73 2d  hs->zTerm, pLhs-
46638 3e 6e 54 65 72 6d 29 3b 0d 0a 20 20 20 20 7d 65  >nTerm);..    }e
46639 6c 73 65 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  lse{..      rc =
4663a 20 6d 65 6d 63 6d 70 28 70 4c 68 73 2d 3e 7a 54   memcmp(pLhs->zT
4663b 65 72 6d 2c 20 70 52 68 73 2d 3e 7a 54 65 72 6d  erm, pRhs->zTerm
4663c 2c 20 70 52 68 73 2d 3e 6e 54 65 72 6d 29 3b 0d  , pRhs->nTerm);.
4663d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
4663e 72 63 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20  rc==0 ){..      
4663f 72 63 20 3d 20 72 63 32 3b 0d 0a 20 20 20 20 7d  rc = rc2;..    }
46640 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
46641 72 63 20 3d 20 28 70 4c 68 73 2d 3e 61 4e 6f 64  rc = (pLhs->aNod
46642 65 3d 3d 30 29 20 2d 20 28 70 52 68 73 2d 3e 61  e==0) - (pRhs->a
46643 4e 6f 64 65 3d 3d 30 29 3b 0d 0a 20 20 7d 0d 0a  Node==0);..  }..
46644 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0d 0a    if( rc==0 ){..
46645 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d 3e 69      rc = pRhs->i
46646 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49 64 78  Idx - pLhs->iIdx
46647 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73 65 72 74  ;..  }..  assert
46648 28 20 72 63 21 3d 30 20 29 3b 0d 0a 20 20 72 65  ( rc!=0 );..  re
46649 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
4664a 2a 0d 0a 2a 2a 20 41 20 64 69 66 66 65 72 65 6e  *..** A differen
4664b 74 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  t comparison fun
4664c 63 74 69 6f 6e 20 66 6f 72 20 53 65 67 52 65 61  ction for SegRea
4664d 64 65 72 20 73 74 72 75 63 74 75 72 65 73 2e 20  der structures. 
4664e 49 6e 20 74 68 69 73 0d 0a 2a 2a 20 76 65 72 73  In this..** vers
4664f 69 6f 6e 2c 20 69 74 20 69 73 20 61 73 73 75 6d  ion, it is assum
46650 65 64 20 74 68 61 74 20 65 61 63 68 20 53 65 67  ed that each Seg
46651 52 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f  Reader points to
46652 20 61 6e 20 65 6e 74 72 79 20 69 6e 0d 0a 2a 2a   an entry in..**
46653 20 61 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 69   a doclist for i
46654 64 65 6e 74 69 63 61 6c 20 74 65 72 6d 73 2e 20  dentical terms. 
46655 43 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61  Comparison is ma
46656 64 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d 0a  de as follows:..
46657 2a 2a 0d 0a 2a 2a 20 20 20 31 29 20 45 4f 46 20  **..**   1) EOF 
46658 28 65 6e 64 20 6f 66 20 64 6f 63 6c 69 73 74 20  (end of doclist 
46659 69 6e 20 74 68 69 73 20 63 61 73 65 29 20 69 73  in this case) is
4665a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6e 6f   greater than no
4665b 74 20 45 4f 46 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  t EOF...**..**  
4665c 20 32 29 20 42 79 20 63 75 72 72 65 6e 74 20 64   2) By current d
4665d 6f 63 69 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ocid...**..**   
4665e 33 29 20 42 79 20 73 65 67 6d 65 6e 74 20 61 67  3) By segment ag
4665f 65 2e 20 41 6e 20 6f 6c 64 65 72 20 73 65 67 6d  e. An older segm
46660 65 6e 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ent is considere
46661 64 20 6c 61 72 67 65 72 2e 0d 0a 2a 2f 0d 0a 73  d larger...*/..s
46662 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
46663 67 52 65 61 64 65 72 44 6f 63 6c 69 73 74 43 6d  gReaderDoclistCm
46664 70 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20  p(Fts3SegReader 
46665 2a 70 4c 68 73 2c 20 46 74 73 33 53 65 67 52 65  *pLhs, Fts3SegRe
46666 61 64 65 72 20 2a 70 52 68 73 29 7b 0d 0a 20 20  ader *pRhs){..  
46667 69 6e 74 20 72 63 20 3d 20 28 70 4c 68 73 2d 3e  int rc = (pLhs->
46668 70 4f 66 66 73 65 74 4c 69 73 74 3d 3d 30 29 2d  pOffsetList==0)-
46669 28 70 52 68 73 2d 3e 70 4f 66 66 73 65 74 4c 69  (pRhs->pOffsetLi
4666a 73 74 3d 3d 30 29 3b 0d 0a 20 20 69 66 28 20 72  st==0);..  if( r
4666b 63 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66 28  c==0 ){..    if(
4666c 20 70 4c 68 73 2d 3e 69 44 6f 63 69 64 3d 3d 70   pLhs->iDocid==p
4666d 52 68 73 2d 3e 69 44 6f 63 69 64 20 29 7b 0d 0a  Rhs->iDocid ){..
4666e 20 20 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d        rc = pRhs-
4666f 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49  >iIdx - pLhs->iI
46670 64 78 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  dx;..    }else{.
46671 0a 20 20 20 20 20 20 72 63 20 3d 20 28 70 4c 68  .      rc = (pLh
46672 73 2d 3e 69 44 6f 63 69 64 20 3e 20 70 52 68 73  s->iDocid > pRhs
46673 2d 3e 69 44 6f 63 69 64 29 20 3f 20 31 20 3a 20  ->iDocid) ? 1 : 
46674 2d 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  -1;..    }..  }.
46675 0a 20 20 61 73 73 65 72 74 28 20 70 4c 68 73 2d  .  assert( pLhs-
46676 3e 61 4e 6f 64 65 20 26 26 20 70 52 68 73 2d 3e  >aNode && pRhs->
46677 61 4e 6f 64 65 20 29 3b 0d 0a 20 20 72 65 74 75  aNode );..  retu
46678 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 73 74 61 74 69  rn rc;..}..stati
46679 63 20 69 6e 74 20 66 74 73 33 53 65 67 52 65 61  c int fts3SegRea
4667a 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 52 65 76  derDoclistCmpRev
4667b 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a  (Fts3SegReader *
4667c 70 4c 68 73 2c 20 46 74 73 33 53 65 67 52 65 61  pLhs, Fts3SegRea
4667d 64 65 72 20 2a 70 52 68 73 29 7b 0d 0a 20 20 69  der *pRhs){..  i
4667e 6e 74 20 72 63 20 3d 20 28 70 4c 68 73 2d 3e 70  nt rc = (pLhs->p
4667f 4f 66 66 73 65 74 4c 69 73 74 3d 3d 30 29 2d 28  OffsetList==0)-(
46680 70 52 68 73 2d 3e 70 4f 66 66 73 65 74 4c 69 73  pRhs->pOffsetLis
46681 74 3d 3d 30 29 3b 0d 0a 20 20 69 66 28 20 72 63  t==0);..  if( rc
46682 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 66 28 20  ==0 ){..    if( 
46683 70 4c 68 73 2d 3e 69 44 6f 63 69 64 3d 3d 70 52  pLhs->iDocid==pR
46684 68 73 2d 3e 69 44 6f 63 69 64 20 29 7b 0d 0a 20  hs->iDocid ){.. 
46685 20 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d 3e       rc = pRhs->
46686 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49 64  iIdx - pLhs->iId
46687 78 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  x;..    }else{..
46688 20 20 20 20 20 20 72 63 20 3d 20 28 70 4c 68 73        rc = (pLhs
46689 2d 3e 69 44 6f 63 69 64 20 3c 20 70 52 68 73 2d  ->iDocid < pRhs-
4668a 3e 69 44 6f 63 69 64 29 20 3f 20 31 20 3a 20 2d  >iDocid) ? 1 : -
4668b 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  1;..    }..  }..
4668c 20 20 61 73 73 65 72 74 28 20 70 4c 68 73 2d 3e    assert( pLhs->
4668d 61 4e 6f 64 65 20 26 26 20 70 52 68 73 2d 3e 61  aNode && pRhs->a
4668e 4e 6f 64 65 20 29 3b 0d 0a 20 20 72 65 74 75 72  Node );..  retur
4668f 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
46690 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 74  ** Compare the t
46691 65 72 6d 20 74 68 61 74 20 74 68 65 20 46 74 73  erm that the Fts
46692 33 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63  3SegReader objec
46693 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
46694 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0d 0a  first argument..
46695 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 74  ** points to wit
46696 68 20 74 68 65 20 74 65 72 6d 20 73 70 65 63 69  h the term speci
46697 66 69 65 64 20 62 79 20 61 72 67 75 6d 65 6e 74  fied by argument
46698 73 20 7a 54 65 72 6d 20 61 6e 64 20 6e 54 65 72  s zTerm and nTer
46699 6d 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74  m. ..**..** If t
4669a 68 65 20 70 53 65 67 20 69 74 65 72 61 74 6f 72  he pSeg iterator
4669b 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 45   is already at E
4669c 4f 46 2c 20 72 65 74 75 72 6e 20 30 2e 20 4f 74  OF, return 0. Ot
4669d 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0d  herwise, return.
4669e 0a 2a 2a 20 2d 76 65 20 69 66 20 74 68 65 20 70  .** -ve if the p
4669f 53 65 67 20 74 65 72 6d 20 69 73 20 6c 65 73 73  Seg term is less
466a0 20 74 68 61 6e 20 7a 54 65 72 6d 2f 6e 54 65 72   than zTerm/nTer
466a1 6d 2c 20 30 20 69 66 20 74 68 65 20 74 77 6f 20  m, 0 if the two 
466a2 74 65 72 6d 73 20 61 72 65 0d 0a 2a 2a 20 65 71  terms are..** eq
466a3 75 61 6c 2c 20 6f 72 20 2b 76 65 20 69 66 20 74  ual, or +ve if t
466a4 68 65 20 70 53 65 67 20 74 65 72 6d 20 69 73 20  he pSeg term is 
466a5 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 54 65  greater than zTe
466a6 72 6d 2f 6e 54 65 72 6d 2e 0d 0a 2a 2f 0d 0a 73  rm/nTerm...*/..s
466a7 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
466a8 67 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 0d  gReaderTermCmp(.
466a9 0a 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72  .  Fts3SegReader
466aa 20 2a 70 53 65 67 2c 20 20 20 20 20 20 20 20 20   *pSeg,         
466ab 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20 72 65     /* Segment re
466ac 61 64 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a  ader object */..
466ad 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
466ae 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
466af 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 63 6f 6d    /* Term to com
466b0 70 61 72 65 20 74 6f 20 2a 2f 0d 0a 20 20 69 6e  pare to */..  in
466b1 74 20 6e 54 65 72 6d 20 20 20 20 20 20 20 20 20  t nTerm         
466b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
466b3 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 7a 54   Size of term zT
466b4 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d  erm in bytes */.
466b5 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 65 73 20 3d  .){..  int res =
466b6 20 30 3b 0d 0a 20 20 69 66 28 20 70 53 65 67 2d   0;..  if( pSeg-
466b7 3e 61 4e 6f 64 65 20 29 7b 0d 0a 20 20 20 20 69  >aNode ){..    i
466b8 66 28 20 70 53 65 67 2d 3e 6e 54 65 72 6d 3e 6e  f( pSeg->nTerm>n
466b9 54 65 72 6d 20 29 7b 0d 0a 20 20 20 20 20 20 72  Term ){..      r
466ba 65 73 20 3d 20 6d 65 6d 63 6d 70 28 70 53 65 67  es = memcmp(pSeg
466bb 2d 3e 7a 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20  ->zTerm, zTerm, 
466bc 6e 54 65 72 6d 29 3b 0d 0a 20 20 20 20 7d 65 6c  nTerm);..    }el
466bd 73 65 7b 0d 0a 20 20 20 20 20 20 72 65 73 20 3d  se{..      res =
466be 20 6d 65 6d 63 6d 70 28 70 53 65 67 2d 3e 7a 54   memcmp(pSeg->zT
466bf 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 70 53 65 67  erm, zTerm, pSeg
466c0 2d 3e 6e 54 65 72 6d 29 3b 0d 0a 20 20 20 20 7d  ->nTerm);..    }
466c1 0d 0a 20 20 20 20 69 66 28 20 72 65 73 3d 3d 30  ..    if( res==0
466c2 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 73 20 3d   ){..      res =
466c3 20 70 53 65 67 2d 3e 6e 54 65 72 6d 2d 6e 54 65   pSeg->nTerm-nTe
466c4 72 6d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  rm;..    }..  }.
466c5 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0d 0a  .  return res;..
466c6 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 72 67 75  }..../*..** Argu
466c7 6d 65 6e 74 20 61 70 53 65 67 6d 65 6e 74 20 69  ment apSegment i
466c8 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 6e 53  s an array of nS
466c9 65 67 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 2e  egment elements.
466ca 20 49 74 20 69 73 20 6b 6e 6f 77 6e 20 74 68 61   It is known tha
466cb 74 0d 0a 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20  t..** the final 
466cc 28 6e 53 65 67 6d 65 6e 74 2d 6e 53 75 73 70 65  (nSegment-nSuspe
466cd 63 74 29 20 6d 65 6d 62 65 72 73 20 61 72 65 20  ct) members are 
466ce 61 6c 72 65 61 64 79 20 69 6e 20 73 6f 72 74 65  already in sorte
466cf 64 20 6f 72 64 65 72 0d 0a 2a 2a 20 28 61 63 63  d order..** (acc
466d0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f  ording to the co
466d1 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
466d2 6e 20 70 72 6f 76 69 64 65 64 29 2e 20 54 68 69  n provided). Thi
466d3 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 75 66 66  s function shuff
466d4 6c 65 73 0d 0a 2a 2a 20 74 68 65 20 61 72 72 61  les..** the arra
466d5 79 20 61 72 6f 75 6e 64 20 75 6e 74 69 6c 20 61  y around until a
466d6 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 69  ll entries are i
466d7 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0d  n sorted order..
466d8 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
466d9 20 66 74 73 33 53 65 67 52 65 61 64 65 72 53 6f   fts3SegReaderSo
466da 72 74 28 0d 0a 20 20 46 74 73 33 53 65 67 52 65  rt(..  Fts3SegRe
466db 61 64 65 72 20 2a 2a 61 70 53 65 67 6d 65 6e 74  ader **apSegment
466dc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
466dd 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 74        /* Array t
466de 6f 20 73 6f 72 74 20 65 6e 74 72 69 65 73 20 6f  o sort entries o
466df 66 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 65 67  f */..  int nSeg
466e0 6d 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  ment,           
466e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466e2 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
466e3 66 20 61 70 53 65 67 6d 65 6e 74 20 61 72 72 61  f apSegment arra
466e4 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 75 73  y */..  int nSus
466e5 70 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  pect,           
466e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466e7 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 6f 72 74         /* Unsort
466e8 65 64 20 65 6e 74 72 79 20 63 6f 75 6e 74 20 2a  ed entry count *
466e9 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29  /..  int (*xCmp)
466ea 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a  (Fts3SegReader *
466eb 2c 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  , Fts3SegReader 
466ec 2a 29 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f  *)  /* Compariso
466ed 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0d 0a 29  n function */..)
466ee 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  {..  int i;     
466ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466f0 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
466f1 20 76 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 0d 0a   variable */....
466f2 20 20 61 73 73 65 72 74 28 20 6e 53 75 73 70 65    assert( nSuspe
466f3 63 74 3c 3d 6e 53 65 67 6d 65 6e 74 20 29 3b 0d  ct<=nSegment );.
466f4 0a 0d 0a 20 20 69 66 28 20 6e 53 75 73 70 65 63  ...  if( nSuspec
466f5 74 3d 3d 6e 53 65 67 6d 65 6e 74 20 29 20 6e 53  t==nSegment ) nS
466f6 75 73 70 65 63 74 2d 2d 3b 0d 0a 20 20 66 6f 72  uspect--;..  for
466f7 28 69 3d 6e 53 75 73 70 65 63 74 2d 31 3b 20 69  (i=nSuspect-1; i
466f8 3e 3d 30 3b 20 69 2d 2d 29 7b 0d 0a 20 20 20 20  >=0; i--){..    
466f9 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 66 6f 72 28  int j;..    for(
466fa 6a 3d 69 3b 20 6a 3c 28 6e 53 65 67 6d 65 6e 74  j=i; j<(nSegment
466fb 2d 31 29 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20  -1); j++){..    
466fc 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
466fd 2a 70 54 6d 70 3b 0d 0a 20 20 20 20 20 20 69 66  *pTmp;..      if
466fe 28 20 78 43 6d 70 28 61 70 53 65 67 6d 65 6e 74  ( xCmp(apSegment
466ff 5b 6a 5d 2c 20 61 70 53 65 67 6d 65 6e 74 5b 6a  [j], apSegment[j
46700 2b 31 5d 29 3c 30 20 29 20 62 72 65 61 6b 3b 0d  +1])<0 ) break;.
46701 0a 20 20 20 20 20 20 70 54 6d 70 20 3d 20 61 70  .      pTmp = ap
46702 53 65 67 6d 65 6e 74 5b 6a 2b 31 5d 3b 0d 0a 20  Segment[j+1];.. 
46703 20 20 20 20 20 61 70 53 65 67 6d 65 6e 74 5b 6a       apSegment[j
46704 2b 31 5d 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b  +1] = apSegment[
46705 6a 5d 3b 0d 0a 20 20 20 20 20 20 61 70 53 65 67  j];..      apSeg
46706 6d 65 6e 74 5b 6a 5d 20 3d 20 70 54 6d 70 3b 0d  ment[j] = pTmp;.
46707 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 23  .    }..  }....#
46708 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0d 0a 20  ifndef NDEBUG.. 
46709 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
4670a 68 65 20 6c 69 73 74 20 72 65 61 6c 6c 79 20 69  he list really i
4670b 73 20 73 6f 72 74 65 64 20 6e 6f 77 2e 20 2a 2f  s sorted now. */
4670c 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 28  ..  for(i=0; i<(
4670d 6e 53 75 73 70 65 63 74 2d 31 29 3b 20 69 2b 2b  nSuspect-1); i++
4670e 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
4670f 78 43 6d 70 28 61 70 53 65 67 6d 65 6e 74 5b 69  xCmp(apSegment[i
46710 5d 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 2b 31  ], apSegment[i+1
46711 5d 29 3c 30 20 29 3b 0d 0a 20 20 7d 0d 0a 23 65  ])<0 );..  }..#e
46712 6e 64 69 66 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a  ndif..}..../* ..
46713 2a 2a 20 49 6e 73 65 72 74 20 61 20 72 65 63 6f  ** Insert a reco
46714 72 64 20 69 6e 74 6f 20 74 68 65 20 25 5f 73 65  rd into the %_se
46715 67 6d 65 6e 74 73 20 74 61 62 6c 65 2e 0d 0a 2a  gments table...*
46716 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
46717 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74 28 0d  s3WriteSegment(.
46718 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
46719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4671a 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
4671b 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20  ble handle */.. 
4671c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
4671d 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  Block,          
4671e 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20 66 6f 72   /* Block id for
4671f 20 6e 65 77 20 62 6c 6f 63 6b 20 2a 2f 0d 0a 20   new block */.. 
46720 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 20   char *z,       
46721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46722 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62   /* Pointer to b
46723 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
46724 20 62 6c 6f 63 6b 20 64 61 74 61 20 2a 2f 0d 0a   block data */..
46725 20 20 69 6e 74 20 6e 20 20 20 20 20 20 20 20 20    int n         
46726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46727 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
46728 66 65 72 20 7a 20 69 6e 20 62 79 74 65 73 20 2a  fer z in bytes *
46729 2f 0d 0a 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33  /..){..  sqlite3
4672a 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0d 0a 20  _stmt *pStmt;.. 
4672b 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33 53 71   int rc = fts3Sq
4672c 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 49 4e 53  lStmt(p, SQL_INS
4672d 45 52 54 5f 53 45 47 4d 45 4e 54 53 2c 20 26 70  ERT_SEGMENTS, &p
4672e 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 69 66 28  Stmt, 0);..  if(
4672f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
46730 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  {..    sqlite3_b
46731 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c  ind_int64(pStmt,
46732 20 31 2c 20 69 42 6c 6f 63 6b 29 3b 0d 0a 20 20   1, iBlock);..  
46733 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
46734 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 7a 2c  lob(pStmt, 2, z,
46735 20 6e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   n, SQLITE_STATI
46736 43 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  C);..    sqlite3
46737 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0d 0a 20  _step(pStmt);.. 
46738 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
46739 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0d 0a 20  reset(pStmt);.. 
4673a 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
4673b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 49  ..}..../* ..** I
4673c 6e 73 65 72 74 20 61 20 72 65 63 6f 72 64 20 69  nsert a record i
4673d 6e 74 6f 20 74 68 65 20 25 5f 73 65 67 64 69 72  nto the %_segdir
4673e 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61   table...*/..sta
4673f 74 69 63 20 69 6e 74 20 66 74 73 33 57 72 69 74  tic int fts3Writ
46740 65 53 65 67 64 69 72 28 0d 0a 20 20 46 74 73 33  eSegdir(..  Fts3
46741 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
46742 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
46743 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e  irtual table han
46744 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c  dle */..  int iL
46745 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20  evel,           
46746 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
46747 75 65 20 66 6f 72 20 22 6c 65 76 65 6c 22 20 66  ue for "level" f
46748 69 65 6c 64 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ield */..  int i
46749 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
4674a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
4674b 6c 75 65 20 66 6f 72 20 22 69 64 78 22 20 66 69  lue for "idx" fi
4674c 65 6c 64 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  eld */..  sqlite
4674d 33 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  3_int64 iStartBl
4674e 6f 63 6b 2c 20 20 20 20 20 20 2f 2a 20 56 61 6c  ock,      /* Val
4674f 75 65 20 66 6f 72 20 22 73 74 61 72 74 5f 62 6c  ue for "start_bl
46750 6f 63 6b 22 20 66 69 65 6c 64 20 2a 2f 0d 0a 20  ock" field */.. 
46751 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
46752 4c 65 61 66 45 6e 64 42 6c 6f 63 6b 2c 20 20 20  LeafEndBlock,   
46753 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 6c   /* Value for "l
46754 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 22  eaves_end_block"
46755 20 66 69 65 6c 64 20 2a 2f 0d 0a 20 20 73 71 6c   field */..  sql
46756 69 74 65 33 5f 69 6e 74 36 34 20 69 45 6e 64 42  ite3_int64 iEndB
46757 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 2f 2a 20  lock,        /* 
46758 56 61 6c 75 65 20 66 6f 72 20 22 65 6e 64 5f 62  Value for "end_b
46759 6c 6f 63 6b 22 20 66 69 65 6c 64 20 2a 2f 0d 0a  lock" field */..
4675a 20 20 63 68 61 72 20 2a 7a 52 6f 6f 74 2c 20 20    char *zRoot,  
4675b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4675c 20 20 2f 2a 20 42 6c 6f 62 20 76 61 6c 75 65 20    /* Blob value 
4675d 66 6f 72 20 22 72 6f 6f 74 22 20 66 69 65 6c 64  for "root" field
4675e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 6f 6f 74   */..  int nRoot
4675f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46760 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
46761 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75 66   of bytes in buf
46762 66 65 72 20 7a 52 6f 6f 74 20 2a 2f 0d 0a 29 7b  fer zRoot */..){
46763 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
46764 20 2a 70 53 74 6d 74 3b 0d 0a 20 20 69 6e 74 20   *pStmt;..  int 
46765 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
46766 28 70 2c 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53  (p, SQL_INSERT_S
46767 45 47 44 49 52 2c 20 26 70 53 74 6d 74 2c 20 30  EGDIR, &pStmt, 0
46768 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
46769 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
4676a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
4676b 28 70 53 74 6d 74 2c 20 31 2c 20 69 4c 65 76 65  (pStmt, 1, iLeve
4676c 6c 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  l);..    sqlite3
4676d 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c  _bind_int(pStmt,
4676e 20 32 2c 20 69 49 64 78 29 3b 0d 0a 20 20 20 20   2, iIdx);..    
4676f 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
46770 36 34 28 70 53 74 6d 74 2c 20 33 2c 20 69 53 74  64(pStmt, 3, iSt
46771 61 72 74 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20 20  artBlock);..    
46772 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
46773 36 34 28 70 53 74 6d 74 2c 20 34 2c 20 69 4c 65  64(pStmt, 4, iLe
46774 61 66 45 6e 64 42 6c 6f 63 6b 29 3b 0d 0a 20 20  afEndBlock);..  
46775 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
46776 6e 74 36 34 28 70 53 74 6d 74 2c 20 35 2c 20 69  nt64(pStmt, 5, i
46777 45 6e 64 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20 20  EndBlock);..    
46778 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
46779 62 28 70 53 74 6d 74 2c 20 36 2c 20 7a 52 6f 6f  b(pStmt, 6, zRoo
4677a 74 2c 20 6e 52 6f 6f 74 2c 20 53 51 4c 49 54 45  t, nRoot, SQLITE
4677b 5f 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 73  _STATIC);..    s
4677c 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
4677d 74 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71  t);..    rc = sq
4677e 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d  lite3_reset(pStm
4677f 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  t);..  }..  retu
46780 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
46781 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
46782 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6d 6d 6f  ize of the commo
46783 6e 20 70 72 65 66 69 78 20 28 69 66 20 61 6e 79  n prefix (if any
46784 29 20 73 68 61 72 65 64 20 62 79 20 7a 50 72 65  ) shared by zPre
46785 76 20 61 6e 64 0d 0a 2a 2a 20 7a 4e 65 78 74 2c  v and..** zNext,
46786 20 69 6e 20 62 79 74 65 73 2e 20 46 6f 72 20 65   in bytes. For e
46787 78 61 6d 70 6c 65 2c 20 0d 0a 2a 2a 0d 0a 2a 2a  xample, ..**..**
46788 20 20 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d     fts3PrefixCom
46789 70 72 65 73 73 28 22 61 62 63 22 2c 20 33 2c 20  press("abc", 3, 
4678a 22 61 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f  "abcdef", 6)   /
4678b 2f 20 72 65 74 75 72 6e 73 20 33 0d 0a 2a 2a 20  / returns 3..** 
4678c 20 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70    fts3PrefixComp
4678d 72 65 73 73 28 22 61 62 58 22 2c 20 33 2c 20 22  ress("abX", 3, "
4678e 61 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f 2f  abcdef", 6)   //
4678f 20 72 65 74 75 72 6e 73 20 32 0d 0a 2a 2a 20 20   returns 2..**  
46790 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72   fts3PrefixCompr
46791 65 73 73 28 22 61 62 58 22 2c 20 33 2c 20 22 58  ess("abX", 3, "X
46792 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f 2f 20  bcdef", 6)   // 
46793 72 65 74 75 72 6e 73 20 30 0d 0a 2a 2f 0d 0a 73  returns 0..*/..s
46794 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 72  tatic int fts3Pr
46795 65 66 69 78 43 6f 6d 70 72 65 73 73 28 0d 0a 20  efixCompress(.. 
46796 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
46797 65 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ev,             
46798 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61   /* Buffer conta
46799 69 6e 69 6e 67 20 70 72 65 76 69 6f 75 73 20 74  ining previous t
4679a 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50  erm */..  int nP
4679b 72 65 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rev,            
4679c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
4679d 65 20 6f 66 20 62 75 66 66 65 72 20 7a 50 72 65  e of buffer zPre
4679e 76 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20  v in bytes */.. 
4679f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65   const char *zNe
467a0 78 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  xt,             
467a1 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61   /* Buffer conta
467a2 69 6e 69 6e 67 20 6e 65 78 74 20 74 65 72 6d 20  ining next term 
467a3 2a 2f 0d 0a 20 20 69 6e 74 20 6e 4e 65 78 74 20  */..  int nNext 
467a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
467a5 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
467a6 20 62 75 66 66 65 72 20 7a 4e 65 78 74 20 69 6e   buffer zNext in
467a7 20 62 79 74 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20   bytes */..){.. 
467a8 20 69 6e 74 20 6e 3b 0d 0a 20 20 55 4e 55 53 45   int n;..  UNUSE
467a9 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 65 78  D_PARAMETER(nNex
467aa 74 29 3b 0d 0a 20 20 66 6f 72 28 6e 3d 30 3b 20  t);..  for(n=0; 
467ab 6e 3c 6e 50 72 65 76 20 26 26 20 7a 50 72 65 76  n<nPrev && zPrev
467ac 5b 6e 5d 3d 3d 7a 4e 65 78 74 5b 6e 5d 3b 20 6e  [n]==zNext[n]; n
467ad 2b 2b 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6e  ++);..  return n
467ae 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41  ;..}..../*..** A
467af 64 64 20 74 65 72 6d 20 7a 54 65 72 6d 20 74 6f  dd term zTerm to
467b0 20 74 68 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65   the SegmentNode
467b1 2e 20 49 74 20 69 73 20 67 75 61 72 61 6e 74 65  . It is guarante
467b2 65 64 20 74 68 61 74 20 7a 54 65 72 6d 20 69 73  ed that zTerm is
467b3 20 6c 61 72 67 65 72 0d 0a 2a 2a 20 28 61 63 63   larger..** (acc
467b4 6f 72 64 69 6e 67 20 74 6f 20 6d 65 6d 63 6d 70  ording to memcmp
467b5 29 20 74 68 61 6e 20 74 68 65 20 70 72 65 76 69  ) than the previ
467b6 6f 75 73 20 74 65 72 6d 2e 0d 0a 2a 2f 0d 0a 73  ous term...*/..s
467b7 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f  tatic int fts3No
467b8 64 65 41 64 64 54 65 72 6d 28 0d 0a 20 20 46 74  deAddTerm(..  Ft
467b9 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20  s3Table *p,     
467ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
467bb 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   Virtual table h
467bc 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 53 65 67 6d  andle */..  Segm
467bd 65 6e 74 4e 6f 64 65 20 2a 2a 70 70 54 72 65 65  entNode **ppTree
467be 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
467bf 4e 2f 4f 55 54 3a 20 53 65 67 6d 65 6e 74 4e 6f  N/OUT: SegmentNo
467c0 64 65 20 68 61 6e 64 6c 65 20 2a 2f 20 0d 0a 20  de handle */ .. 
467c1 20 69 6e 74 20 69 73 43 6f 70 79 54 65 72 6d 2c   int isCopyTerm,
467c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
467c3 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 54 65 72   /* True if zTer
467c4 6d 2f 6e 54 65 72 6d 20 69 73 20 74 72 61 6e 73  m/nTerm is trans
467c5 69 65 6e 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ient */..  const
467c6 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20   char *zTerm,   
467c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
467c8 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20  inter to buffer 
467c9 63 6f 6e 74 61 69 6e 69 6e 67 20 74 65 72 6d 20  containing term 
467ca 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 20  */..  int nTerm 
467cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
467cc 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
467cd 20 74 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a   term in bytes *
467ce 2f 0d 0a 29 7b 0d 0a 20 20 53 65 67 6d 65 6e 74  /..){..  Segment
467cf 4e 6f 64 65 20 2a 70 54 72 65 65 20 3d 20 2a 70  Node *pTree = *p
467d0 70 54 72 65 65 3b 0d 0a 20 20 69 6e 74 20 72 63  pTree;..  int rc
467d1 3b 0d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65  ;..  SegmentNode
467d2 20 2a 70 4e 65 77 3b 0d 0a 0d 0a 20 20 2f 2a 20   *pNew;....  /* 
467d3 46 69 72 73 74 20 74 72 79 20 74 6f 20 61 70 70  First try to app
467d4 65 6e 64 20 74 68 65 20 74 65 72 6d 20 74 6f 20  end the term to 
467d5 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65  the current node
467d6 2e 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69  . Return early i
467d7 66 20 0d 0a 20 20 2a 2a 20 74 68 69 73 20 69 73  f ..  ** this is
467d8 20 70 6f 73 73 69 62 6c 65 2e 0d 0a 20 20 2a 2f   possible...  */
467d9 0d 0a 20 20 69 66 28 20 70 54 72 65 65 20 29 7b  ..  if( pTree ){
467da 0d 0a 20 20 20 20 69 6e 74 20 6e 44 61 74 61 20  ..    int nData 
467db 3d 20 70 54 72 65 65 2d 3e 6e 44 61 74 61 3b 20  = pTree->nData; 
467dc 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73      /* Current s
467dd 69 7a 65 20 6f 66 20 6e 6f 64 65 20 69 6e 20 62  ize of node in b
467de 79 74 65 73 20 2a 2f 0d 0a 20 20 20 20 69 6e 74  ytes */..    int
467df 20 6e 52 65 71 20 3d 20 6e 44 61 74 61 3b 20 20   nReq = nData;  
467e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
467e1 71 75 69 72 65 64 20 73 70 61 63 65 20 61 66 74  quired space aft
467e2 65 72 20 61 64 64 69 6e 67 20 7a 54 65 72 6d 20  er adding zTerm 
467e3 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 50 72 65  */..    int nPre
467e4 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
467e5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
467e6 6f 66 20 62 79 74 65 73 20 6f 66 20 70 72 65 66  of bytes of pref
467e7 69 78 20 63 6f 6d 70 72 65 73 73 69 6f 6e 20 2a  ix compression *
467e8 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 53 75 66 66  /..    int nSuff
467e9 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ix;             
467ea 20 20 20 20 20 2f 2a 20 53 75 66 66 69 78 20 6c       /* Suffix l
467eb 65 6e 67 74 68 20 2a 2f 0d 0a 0d 0a 20 20 20 20  ength */....    
467ec 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33 50 72  nPrefix = fts3Pr
467ed 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70 54 72  efixCompress(pTr
467ee 65 65 2d 3e 7a 54 65 72 6d 2c 20 70 54 72 65 65  ee->zTerm, pTree
467ef 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20  ->nTerm, zTerm, 
467f0 6e 54 65 72 6d 29 3b 0d 0a 20 20 20 20 6e 53 75  nTerm);..    nSu
467f1 66 66 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50 72  ffix = nTerm-nPr
467f2 65 66 69 78 3b 0d 0a 0d 0a 20 20 20 20 6e 52 65  efix;....    nRe
467f3 71 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  q += sqlite3Fts3
467f4 56 61 72 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69  VarintLen(nPrefi
467f5 78 29 2b 73 71 6c 69 74 65 33 46 74 73 33 56 61  x)+sqlite3Fts3Va
467f6 72 69 6e 74 4c 65 6e 28 6e 53 75 66 66 69 78 29  rintLen(nSuffix)
467f7 2b 6e 53 75 66 66 69 78 3b 0d 0a 20 20 20 20 69  +nSuffix;..    i
467f8 66 28 20 6e 52 65 71 3c 3d 70 2d 3e 6e 4e 6f 64  f( nReq<=p->nNod
467f9 65 53 69 7a 65 20 7c 7c 20 21 70 54 72 65 65 2d  eSize || !pTree-
467fa 3e 7a 54 65 72 6d 20 29 7b 0d 0a 0d 0a 20 20 20  >zTerm ){....   
467fb 20 20 20 69 66 28 20 6e 52 65 71 3e 70 2d 3e 6e     if( nReq>p->n
467fc 4e 6f 64 65 53 69 7a 65 20 29 7b 0d 0a 20 20 20  NodeSize ){..   
467fd 20 20 20 20 20 2f 2a 20 41 6e 20 75 6e 75 73 75       /* An unusu
467fe 61 6c 20 63 61 73 65 3a 20 74 68 69 73 20 69 73  al case: this is
467ff 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20   the first term 
46800 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  to be added to t
46801 68 65 20 6e 6f 64 65 0d 0a 20 20 20 20 20 20 20  he node..       
46802 20 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74   ** and the stat
46803 69 63 20 6e 6f 64 65 20 62 75 66 66 65 72 20 28  ic node buffer (
46804 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20 62 79 74  p->nNodeSize byt
46805 65 73 29 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  es) is not large
46806 0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 6e 6f  ..        ** eno
46807 75 67 68 2e 20 55 73 65 20 61 20 73 65 70 61 72  ugh. Use a separ
46808 61 74 65 6c 79 20 6d 61 6c 6c 6f 63 65 64 20 62  ately malloced b
46809 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 54 68  uffer instead Th
4680a 69 73 20 77 61 73 74 65 73 0d 0a 20 20 20 20 20  is wastes..     
4680b 20 20 20 2a 2a 20 70 2d 3e 6e 4e 6f 64 65 53 69     ** p->nNodeSi
4680c 7a 65 20 62 79 74 65 73 2c 20 62 75 74 20 73 69  ze bytes, but si
4680d 6e 63 65 20 74 68 69 73 20 73 63 65 6e 61 72 69  nce this scenari
4680e 6f 20 6f 6e 6c 79 20 63 6f 6d 65 73 20 61 62 6f  o only comes abo
4680f 75 74 20 77 68 65 6e 0d 0a 20 20 20 20 20 20 20  ut when..       
46810 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
46811 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20 74 65 72   contain two ter
46812 6d 73 20 74 68 61 74 20 73 68 61 72 65 20 61 20  ms that share a 
46813 70 72 65 66 69 78 20 6f 66 20 61 6c 6d 6f 73 74  prefix of almost
46814 20 32 4b 42 2c 20 0d 0a 20 20 20 20 20 20 20 20   2KB, ..        
46815 2a 2a 20 74 68 69 73 20 69 73 20 6e 6f 74 20 65  ** this is not e
46816 78 70 65 63 74 65 64 20 74 6f 20 62 65 20 61 20  xpected to be a 
46817 73 65 72 69 6f 75 73 20 70 72 6f 62 6c 65 6d 2e  serious problem.
46818 20 0d 0a 20 20 20 20 20 20 20 20 2a 2f 0d 0a 20   ..        */.. 
46819 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
4681a 54 72 65 65 2d 3e 61 44 61 74 61 3d 3d 28 63 68  Tree->aData==(ch
4681b 61 72 20 2a 29 26 70 54 72 65 65 5b 31 5d 20 29  ar *)&pTree[1] )
4681c 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 72 65 65  ;..        pTree
4681d 2d 3e 61 44 61 74 61 20 3d 20 28 63 68 61 72 20  ->aData = (char 
4681e 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
4681f 28 6e 52 65 71 29 3b 0d 0a 20 20 20 20 20 20 20  (nReq);..       
46820 20 69 66 28 20 21 70 54 72 65 65 2d 3e 61 44 61   if( !pTree->aDa
46821 74 61 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  ta ){..         
46822 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
46823 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20 20 20 7d  OMEM;..        }
46824 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20  ..      }....   
46825 20 20 20 69 66 28 20 70 54 72 65 65 2d 3e 7a 54     if( pTree->zT
46826 65 72 6d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  erm ){..        
46827 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70  /* There is no p
46828 72 65 66 69 78 2d 6c 65 6e 67 74 68 20 66 69 65  refix-length fie
46829 6c 64 20 66 6f 72 20 66 69 72 73 74 20 74 65 72  ld for first ter
4682a 6d 20 69 6e 20 61 20 6e 6f 64 65 20 2a 2f 0d 0a  m in a node */..
4682b 20 20 20 20 20 20 20 20 6e 44 61 74 61 20 2b 3d          nData +=
4682c 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56   sqlite3Fts3PutV
4682d 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44  arint(&pTree->aD
4682e 61 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 50 72 65  ata[nData], nPre
4682f 66 69 78 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  fix);..      }..
46830 0d 0a 20 20 20 20 20 20 6e 44 61 74 61 20 2b 3d  ..      nData +=
46831 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56   sqlite3Fts3PutV
46832 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44  arint(&pTree->aD
46833 61 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 53 75 66  ata[nData], nSuf
46834 66 69 78 29 3b 0d 0a 20 20 20 20 20 20 6d 65 6d  fix);..      mem
46835 63 70 79 28 26 70 54 72 65 65 2d 3e 61 44 61 74  cpy(&pTree->aDat
46836 61 5b 6e 44 61 74 61 5d 2c 20 26 7a 54 65 72 6d  a[nData], &zTerm
46837 5b 6e 50 72 65 66 69 78 5d 2c 20 6e 53 75 66 66  [nPrefix], nSuff
46838 69 78 29 3b 0d 0a 20 20 20 20 20 20 70 54 72 65  ix);..      pTre
46839 65 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74 61  e->nData = nData
4683a 20 2b 20 6e 53 75 66 66 69 78 3b 0d 0a 20 20 20   + nSuffix;..   
4683b 20 20 20 70 54 72 65 65 2d 3e 6e 45 6e 74 72 79     pTree->nEntry
4683c 2b 2b 3b 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28  ++;....      if(
4683d 20 69 73 43 6f 70 79 54 65 72 6d 20 29 7b 0d 0a   isCopyTerm ){..
4683e 20 20 20 20 20 20 20 20 69 66 28 20 70 54 72 65          if( pTre
4683f 65 2d 3e 6e 4d 61 6c 6c 6f 63 3c 6e 54 65 72 6d  e->nMalloc<nTerm
46840 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 63   ){..          c
46841 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  har *zNew = sqli
46842 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 54 72 65  te3_realloc(pTre
46843 65 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 6e 54 65 72  e->zMalloc, nTer
46844 6d 2a 32 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  m*2);..         
46845 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0d 0a 20   if( !zNew ){.. 
46846 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
46847 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
46848 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
46849 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e          pTree->n
4684a 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72 6d 2a 32  Malloc = nTerm*2
4684b 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 54 72  ;..          pTr
4684c 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e  ee->zMalloc = zN
4684d 65 77 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  ew;..        }..
4684e 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a          pTree->z
4684f 54 65 72 6d 20 3d 20 70 54 72 65 65 2d 3e 7a 4d  Term = pTree->zM
46850 61 6c 6c 6f 63 3b 0d 0a 20 20 20 20 20 20 20 20  alloc;..        
46851 6d 65 6d 63 70 79 28 70 54 72 65 65 2d 3e 7a 54  memcpy(pTree->zT
46852 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  erm, zTerm, nTer
46853 6d 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 54 72  m);..        pTr
46854 65 65 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72  ee->nTerm = nTer
46855 6d 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  m;..      }else{
46856 0d 0a 20 20 20 20 20 20 20 20 70 54 72 65 65 2d  ..        pTree-
46857 3e 7a 54 65 72 6d 20 3d 20 28 63 68 61 72 20 2a  >zTerm = (char *
46858 29 7a 54 65 72 6d 3b 0d 0a 20 20 20 20 20 20 20  )zTerm;..       
46859 20 70 54 72 65 65 2d 3e 6e 54 65 72 6d 20 3d 20   pTree->nTerm = 
4685a 6e 54 65 72 6d 3b 0d 0a 20 20 20 20 20 20 7d 0d  nTerm;..      }.
4685b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
4685c 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d  LITE_OK;..    }.
4685d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
4685e 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f  control flows to
4685f 20 68 65 72 65 2c 20 69 74 20 77 61 73 20 6e 6f   here, it was no
46860 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 70  t possible to ap
46861 70 65 6e 64 20 7a 54 65 72 6d 20 74 6f 20 74 68  pend zTerm to th
46862 65 0d 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20  e..  ** current 
46863 6e 6f 64 65 2e 20 43 72 65 61 74 65 20 61 20 6e  node. Create a n
46864 65 77 20 6e 6f 64 65 20 28 61 20 72 69 67 68 74  ew node (a right
46865 2d 73 69 62 6c 69 6e 67 20 6f 66 20 74 68 65 20  -sibling of the 
46866 63 75 72 72 65 6e 74 20 6e 6f 64 65 29 2e 0d 0a  current node)...
46867 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
46868 74 68 65 20 66 69 72 73 74 20 6e 6f 64 65 20 69  the first node i
46869 6e 20 74 68 65 20 74 72 65 65 2c 20 74 68 65 20  n the tree, the 
4686a 74 65 72 6d 20 69 73 20 61 64 64 65 64 20 74 6f  term is added to
4686b 20 69 74 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a   it...  **..  **
4686c 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
4686d 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 64 64 65  term is not adde
4686e 64 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 6f 64  d to the new nod
4686f 65 2c 20 69 74 20 69 73 20 6c 65 66 74 20 65 6d  e, it is left em
46870 70 74 79 20 66 6f 72 0d 0a 20 20 2a 2a 20 6e 6f  pty for..  ** no
46871 77 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  w. Instead, the 
46872 74 65 72 6d 20 69 73 20 69 6e 73 65 72 74 65 64  term is inserted
46873 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
46874 20 6f 66 20 70 54 72 65 65 2e 20 49 66 20 70 54   of pTree. If pT
46875 72 65 65 20 0d 0a 20 20 2a 2a 20 68 61 73 20 6e  ree ..  ** has n
46876 6f 20 70 61 72 65 6e 74 2c 20 6f 6e 65 20 69 73  o parent, one is
46877 20 63 72 65 61 74 65 64 20 68 65 72 65 2e 0d 0a   created here...
46878 20 20 2a 2f 0d 0a 20 20 70 4e 65 77 20 3d 20 28    */..  pNew = (
46879 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 29 73 71  SegmentNode *)sq
4687a 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
4687b 65 6f 66 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29  eof(SegmentNode)
4687c 20 2b 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29   + p->nNodeSize)
4687d 3b 0d 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29  ;..  if( !pNew )
4687e 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  {..    return SQ
4687f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d  LITE_NOMEM;..  }
46880 0d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c  ..  memset(pNew,
46881 20 30 2c 20 73 69 7a 65 6f 66 28 53 65 67 6d 65   0, sizeof(Segme
46882 6e 74 4e 6f 64 65 29 29 3b 0d 0a 20 20 70 4e 65  ntNode));..  pNe
46883 77 2d 3e 6e 44 61 74 61 20 3d 20 31 20 2b 20 46  w->nData = 1 + F
46884 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 3b 0d  TS3_VARINT_MAX;.
46885 0a 20 20 70 4e 65 77 2d 3e 61 44 61 74 61 20 3d  .  pNew->aData =
46886 20 28 63 68 61 72 20 2a 29 26 70 4e 65 77 5b 31   (char *)&pNew[1
46887 5d 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 54 72 65  ];....  if( pTre
46888 65 20 29 7b 0d 0a 20 20 20 20 53 65 67 6d 65 6e  e ){..    Segmen
46889 74 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  tNode *pParent =
4688a 20 70 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 3b   pTree->pParent;
4688b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 4e  ..    rc = fts3N
4688c 6f 64 65 41 64 64 54 65 72 6d 28 70 2c 20 26 70  odeAddTerm(p, &p
4688d 50 61 72 65 6e 74 2c 20 69 73 43 6f 70 79 54 65  Parent, isCopyTe
4688e 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, zTerm, nTerm
4688f 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 54 72 65  );..    if( pTre
46890 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b  e->pParent==0 ){
46891 0d 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70  ..      pTree->p
46892 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74  Parent = pParent
46893 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 54  ;..    }..    pT
46894 72 65 65 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e  ree->pRight = pN
46895 65 77 3b 0d 0a 20 20 20 20 70 4e 65 77 2d 3e 70  ew;..    pNew->p
46896 4c 65 66 74 6d 6f 73 74 20 3d 20 70 54 72 65 65  Leftmost = pTree
46897 2d 3e 70 4c 65 66 74 6d 6f 73 74 3b 0d 0a 20 20  ->pLeftmost;..  
46898 20 20 70 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20    pNew->pParent 
46899 3d 20 70 50 61 72 65 6e 74 3b 0d 0a 20 20 20 20  = pParent;..    
4689a 70 4e 65 77 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pNew->zMalloc = 
4689b 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0d  pTree->zMalloc;.
4689c 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 4d 61 6c 6c  .    pNew->nMall
4689d 6f 63 20 3d 20 70 54 72 65 65 2d 3e 6e 4d 61 6c  oc = pTree->nMal
4689e 6c 6f 63 3b 0d 0a 20 20 20 20 70 54 72 65 65 2d  loc;..    pTree-
4689f 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0d 0a 20  >zMalloc = 0;.. 
468a0 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 4e 65   }else{..    pNe
468a1 77 2d 3e 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70  w->pLeftmost = p
468a2 4e 65 77 3b 0d 0a 20 20 20 20 72 63 20 3d 20 66  New;..    rc = f
468a3 74 73 33 4e 6f 64 65 41 64 64 54 65 72 6d 28 70  ts3NodeAddTerm(p
468a4 2c 20 26 70 4e 65 77 2c 20 69 73 43 6f 70 79 54  , &pNew, isCopyT
468a5 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  erm, zTerm, nTer
468a6 6d 29 3b 20 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a  m); ..  }....  *
468a7 70 70 54 72 65 65 20 3d 20 70 4e 65 77 3b 0d 0a  ppTree = pNew;..
468a8 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
468a9 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72  .../*..** Helper
468aa 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 66 74   function for ft
468ab 73 33 4e 6f 64 65 57 72 69 74 65 28 29 2e 0d 0a  s3NodeWrite()...
468ac 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  */..static int f
468ad 74 73 33 54 72 65 65 46 69 6e 69 73 68 4e 6f 64  ts3TreeFinishNod
468ae 65 28 0d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64  e(..  SegmentNod
468af 65 20 2a 70 54 72 65 65 2c 20 0d 0a 20 20 69 6e  e *pTree, ..  in
468b0 74 20 69 48 65 69 67 68 74 2c 20 0d 0a 20 20 73  t iHeight, ..  s
468b1 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 65  qlite3_int64 iLe
468b2 66 74 43 68 69 6c 64 0d 0a 29 7b 0d 0a 20 20 69  ftChild..){..  i
468b3 6e 74 20 6e 53 74 61 72 74 3b 0d 0a 20 20 61 73  nt nStart;..  as
468b4 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d 31  sert( iHeight>=1
468b5 20 26 26 20 69 48 65 69 67 68 74 3c 31 32 38 20   && iHeight<128 
468b6 29 3b 0d 0a 20 20 6e 53 74 61 72 74 20 3d 20 46  );..  nStart = F
468b7 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20 2d  TS3_VARINT_MAX -
468b8 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69   sqlite3Fts3Vari
468b9 6e 74 4c 65 6e 28 69 4c 65 66 74 43 68 69 6c 64  ntLen(iLeftChild
468ba 29 3b 0d 0a 20 20 70 54 72 65 65 2d 3e 61 44 61  );..  pTree->aDa
468bb 74 61 5b 6e 53 74 61 72 74 5d 20 3d 20 28 63 68  ta[nStart] = (ch
468bc 61 72 29 69 48 65 69 67 68 74 3b 0d 0a 20 20 73  ar)iHeight;..  s
468bd 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
468be 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44 61 74  int(&pTree->aDat
468bf 61 5b 6e 53 74 61 72 74 2b 31 5d 2c 20 69 4c 65  a[nStart+1], iLe
468c0 66 74 43 68 69 6c 64 29 3b 0d 0a 20 20 72 65 74  ftChild);..  ret
468c1 75 72 6e 20 6e 53 74 61 72 74 3b 0d 0a 7d 0d 0a  urn nStart;..}..
468c2 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 74  ../*..** Write t
468c3 68 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68  he buffer for th
468c4 65 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 20 70  e segment node p
468c5 54 72 65 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20  Tree and all of 
468c6 69 74 73 20 70 65 65 72 73 20 74 6f 20 74 68 65  its peers to the
468c7 0d 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54  ..** database. T
468c8 68 65 6e 20 63 61 6c 6c 20 74 68 69 73 20 66 75  hen call this fu
468c9 6e 63 74 69 6f 6e 20 72 65 63 75 72 73 69 76 65  nction recursive
468ca 6c 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ly to write the 
468cb 70 61 72 65 6e 74 20 6f 66 20 0d 0a 2a 2a 20 70  parent of ..** p
468cc 54 72 65 65 20 61 6e 64 20 69 74 73 20 70 65 65  Tree and its pee
468cd 72 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  rs to the databa
468ce 73 65 2e 20 0d 0a 2a 2a 0d 0a 2a 2a 20 45 78 63  se. ..**..** Exc
468cf 65 70 74 2c 20 69 66 20 70 54 72 65 65 20 69 73  ept, if pTree is
468d0 20 61 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 64 6f   a root node, do
468d1 20 6e 6f 74 20 77 72 69 74 65 20 69 74 20 74 6f   not write it to
468d2 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
468d3 6e 73 74 65 61 64 2c 0d 0a 2a 2a 20 73 65 74 20  nstead,..** set 
468d4 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
468d5 20 2a 70 61 52 6f 6f 74 20 61 6e 64 20 2a 70 6e   *paRoot and *pn
468d6 52 6f 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20  Root to contain 
468d7 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 0d 0a  the root node...
468d8 2a 2a 0d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  **..** If succes
468d9 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
468da 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
468db 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20  output variable 
468dc 2a 70 69 4c 61 73 74 20 69 73 0d 0a 2a 2a 20 73  *piLast is..** s
468dd 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73  et to the larges
468de 74 20 62 6c 6f 63 6b 69 64 20 77 72 69 74 74 65  t blockid writte
468df 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
468e0 65 20 28 6f 72 20 7a 65 72 6f 20 69 66 20 6e 6f  e (or zero if no
468e1 0d 0a 2a 2a 20 62 6c 6f 63 6b 73 20 77 65 72 65  ..** blocks were
468e2 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
468e3 64 62 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  db). Otherwise, 
468e4 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
468e5 63 6f 64 65 20 69 73 20 0d 0a 2a 2a 20 72 65 74  code is ..** ret
468e6 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  urned...*/..stat
468e7 69 63 20 69 6e 74 20 66 74 73 33 4e 6f 64 65 57  ic int fts3NodeW
468e8 72 69 74 65 28 0d 0a 20 20 46 74 73 33 54 61 62  rite(..  Fts3Tab
468e9 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
468ea 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
468eb 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
468ec 20 2a 2f 0d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f   */..  SegmentNo
468ed 64 65 20 2a 70 54 72 65 65 2c 20 20 20 20 20 20  de *pTree,      
468ee 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e         /* Segmen
468ef 74 4e 6f 64 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  tNode handle */.
468f0 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 2c 20  .  int iHeight, 
468f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
468f2 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
468f3 74 68 69 73 20 6e 6f 64 65 20 69 6e 20 74 72 65  this node in tre
468f4 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f  e */..  sqlite3_
468f5 69 6e 74 36 34 20 69 4c 65 61 66 2c 20 20 20 20  int64 iLeaf,    
468f6 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b          /* Block
468f7 20 69 64 20 6f 66 20 66 69 72 73 74 20 6c 65 61   id of first lea
468f8 66 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c  f node */..  sql
468f9 69 74 65 33 5f 69 6e 74 36 34 20 69 46 72 65 65  ite3_int64 iFree
468fa 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
468fb 42 6c 6f 63 6b 20 69 64 20 6f 66 20 6e 65 78 74  Block id of next
468fc 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 25 5f   free slot in %_
468fd 73 65 67 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20 73  segments */..  s
468fe 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69  qlite3_int64 *pi
468ff 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20 20 2f  Last,          /
46900 2a 20 4f 55 54 3a 20 42 6c 6f 63 6b 20 69 64 20  * OUT: Block id 
46901 6f 66 20 6c 61 73 74 20 65 6e 74 72 79 20 77 72  of last entry wr
46902 69 74 74 65 6e 20 2a 2f 0d 0a 20 20 63 68 61 72  itten */..  char
46903 20 2a 2a 70 61 52 6f 6f 74 2c 20 20 20 20 20 20   **paRoot,      
46904 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
46905 55 54 3a 20 44 61 74 61 20 66 6f 72 20 72 6f 6f  UT: Data for roo
46906 74 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74  t node */..  int
46907 20 2a 70 6e 52 6f 6f 74 20 20 20 20 20 20 20 20   *pnRoot        
46908 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46909 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 72 6f 6f  OUT: Size of roo
4690a 74 20 6e 6f 64 65 20 69 6e 20 62 79 74 65 73 20  t node in bytes 
4690b 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63  */..){..  int rc
4690c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d   = SQLITE_OK;...
4690d 0a 20 20 69 66 28 20 21 70 54 72 65 65 2d 3e 70  .  if( !pTree->p
4690e 50 61 72 65 6e 74 20 29 7b 0d 0a 20 20 20 20 2f  Parent ){..    /
4690f 2a 20 52 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74  * Root node of t
46910 68 65 20 74 72 65 65 2e 20 2a 2f 0d 0a 20 20 20  he tree. */..   
46911 20 69 6e 74 20 6e 53 74 61 72 74 20 3d 20 66 74   int nStart = ft
46912 73 33 54 72 65 65 46 69 6e 69 73 68 4e 6f 64 65  s3TreeFinishNode
46913 28 70 54 72 65 65 2c 20 69 48 65 69 67 68 74 2c  (pTree, iHeight,
46914 20 69 4c 65 61 66 29 3b 0d 0a 20 20 20 20 2a 70   iLeaf);..    *p
46915 69 4c 61 73 74 20 3d 20 69 46 72 65 65 2d 31 3b  iLast = iFree-1;
46916 0d 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 20 3d 20  ..    *pnRoot = 
46917 70 54 72 65 65 2d 3e 6e 44 61 74 61 20 2d 20 6e  pTree->nData - n
46918 53 74 61 72 74 3b 0d 0a 20 20 20 20 2a 70 61 52  Start;..    *paR
46919 6f 6f 74 20 3d 20 26 70 54 72 65 65 2d 3e 61 44  oot = &pTree->aD
4691a 61 74 61 5b 6e 53 74 61 72 74 5d 3b 0d 0a 20 20  ata[nStart];..  
4691b 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 53 65 67 6d  }else{..    Segm
4691c 65 6e 74 4e 6f 64 65 20 2a 70 49 74 65 72 3b 0d  entNode *pIter;.
4691d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
4691e 36 34 20 69 4e 65 78 74 46 72 65 65 20 3d 20 69  64 iNextFree = i
4691f 46 72 65 65 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Free;..    sqlit
46920 65 33 5f 69 6e 74 36 34 20 69 4e 65 78 74 4c 65  e3_int64 iNextLe
46921 61 66 20 3d 20 69 4c 65 61 66 3b 0d 0a 20 20 20  af = iLeaf;..   
46922 20 66 6f 72 28 70 49 74 65 72 3d 70 54 72 65 65   for(pIter=pTree
46923 2d 3e 70 4c 65 66 74 6d 6f 73 74 3b 20 70 49 74  ->pLeftmost; pIt
46924 65 72 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  er && rc==SQLITE
46925 5f 4f 4b 3b 20 70 49 74 65 72 3d 70 49 74 65 72  _OK; pIter=pIter
46926 2d 3e 70 52 69 67 68 74 29 7b 0d 0a 20 20 20 20  ->pRight){..    
46927 20 20 69 6e 74 20 6e 53 74 61 72 74 20 3d 20 66    int nStart = f
46928 74 73 33 54 72 65 65 46 69 6e 69 73 68 4e 6f 64  ts3TreeFinishNod
46929 65 28 70 49 74 65 72 2c 20 69 48 65 69 67 68 74  e(pIter, iHeight
4692a 2c 20 69 4e 65 78 74 4c 65 61 66 29 3b 0d 0a 20  , iNextLeaf);.. 
4692b 20 20 20 20 20 69 6e 74 20 6e 57 72 69 74 65 20       int nWrite 
4692c 3d 20 70 49 74 65 72 2d 3e 6e 44 61 74 61 20 2d  = pIter->nData -
4692d 20 6e 53 74 61 72 74 3b 0d 0a 20 20 0d 0a 20 20   nStart;..  ..  
4692e 20 20 20 20 72 63 20 3d 20 66 74 73 33 57 72 69      rc = fts3Wri
4692f 74 65 53 65 67 6d 65 6e 74 28 70 2c 20 69 4e 65  teSegment(p, iNe
46930 78 74 46 72 65 65 2c 20 26 70 49 74 65 72 2d 3e  xtFree, &pIter->
46931 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 2c 20 6e  aData[nStart], n
46932 57 72 69 74 65 29 3b 0d 0a 20 20 20 20 20 20 69  Write);..      i
46933 4e 65 78 74 46 72 65 65 2b 2b 3b 0d 0a 20 20 20  NextFree++;..   
46934 20 20 20 69 4e 65 78 74 4c 65 61 66 20 2b 3d 20     iNextLeaf += 
46935 28 70 49 74 65 72 2d 3e 6e 45 6e 74 72 79 2b 31  (pIter->nEntry+1
46936 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
46937 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
46938 20 29 7b 0d 0a 20 20 20 20 20 20 61 73 73 65 72   ){..      asser
46939 74 28 20 69 4e 65 78 74 4c 65 61 66 3d 3d 69 46  t( iNextLeaf==iF
4693a 72 65 65 20 29 3b 0d 0a 20 20 20 20 20 20 72 63  ree );..      rc
4693b 20 3d 20 66 74 73 33 4e 6f 64 65 57 72 69 74 65   = fts3NodeWrite
4693c 28 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2c 20  (..          p, 
4693d 70 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 2c 20  pTree->pParent, 
4693e 69 48 65 69 67 68 74 2b 31 2c 20 69 46 72 65 65  iHeight+1, iFree
4693f 2c 20 69 4e 65 78 74 46 72 65 65 2c 20 70 69 4c  , iNextFree, piL
46940 61 73 74 2c 20 70 61 52 6f 6f 74 2c 20 70 6e 52  ast, paRoot, pnR
46941 6f 6f 74 0d 0a 20 20 20 20 20 20 29 3b 0d 0a 20  oot..      );.. 
46942 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72     }..  }....  r
46943 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
46944 2f 2a 0d 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20  /*..** Free all 
46945 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
46946 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ns associated wi
46947 74 68 20 74 68 65 20 74 72 65 65 20 70 54 72 65  th the tree pTre
46948 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  e...*/..static v
46949 6f 69 64 20 66 74 73 33 4e 6f 64 65 46 72 65 65  oid fts3NodeFree
4694a 28 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 54  (SegmentNode *pT
4694b 72 65 65 29 7b 0d 0a 20 20 69 66 28 20 70 54 72  ree){..  if( pTr
4694c 65 65 20 29 7b 0d 0a 20 20 20 20 53 65 67 6d 65  ee ){..    Segme
4694d 6e 74 4e 6f 64 65 20 2a 70 20 3d 20 70 54 72 65  ntNode *p = pTre
4694e 65 2d 3e 70 4c 65 66 74 6d 6f 73 74 3b 0d 0a 20  e->pLeftmost;.. 
4694f 20 20 20 66 74 73 33 4e 6f 64 65 46 72 65 65 28     fts3NodeFree(
46950 70 2d 3e 70 50 61 72 65 6e 74 29 3b 0d 0a 20 20  p->pParent);..  
46951 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0d 0a 20    while( p ){.. 
46952 20 20 20 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65       SegmentNode
46953 20 2a 70 52 69 67 68 74 20 3d 20 70 2d 3e 70 52   *pRight = p->pR
46954 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 69 66 28  ight;..      if(
46955 20 70 2d 3e 61 44 61 74 61 21 3d 28 63 68 61 72   p->aData!=(char
46956 20 2a 29 26 70 5b 31 5d 20 29 7b 0d 0a 20 20 20   *)&p[1] ){..   
46957 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
46958 65 28 70 2d 3e 61 44 61 74 61 29 3b 0d 0a 20 20  e(p->aData);..  
46959 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 61 73 73      }..      ass
4695a 65 72 74 28 20 70 52 69 67 68 74 3d 3d 30 20 7c  ert( pRight==0 |
4695b 7c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20  | p->zMalloc==0 
4695c 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
4695d 33 5f 66 72 65 65 28 70 2d 3e 7a 4d 61 6c 6c 6f  3_free(p->zMallo
4695e 63 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74  c);..      sqlit
4695f 65 33 5f 66 72 65 65 28 70 29 3b 0d 0a 20 20 20  e3_free(p);..   
46960 20 20 20 70 20 3d 20 70 52 69 67 68 74 3b 0d 0a     p = pRight;..
46961 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d      }..  }..}...
46962 0a 2f 2a 0d 0a 2a 2a 20 41 64 64 20 61 20 74 65  ./*..** Add a te
46963 72 6d 20 74 6f 20 74 68 65 20 73 65 67 6d 65 6e  rm to the segmen
46964 74 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63  t being construc
46965 74 65 64 20 62 79 20 74 68 65 20 53 65 67 6d 65  ted by the Segme
46966 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74 0d  ntWriter object.
46967 0a 2a 2a 20 2a 70 70 57 72 69 74 65 72 2e 20 57  .** *ppWriter. W
46968 68 65 6e 20 61 64 64 69 6e 67 20 74 68 65 20 66  hen adding the f
46969 69 72 73 74 20 74 65 72 6d 20 74 6f 20 61 20 73  irst term to a s
4696a 65 67 6d 65 6e 74 2c 20 2a 70 70 57 72 69 74 65  egment, *ppWrite
4696b 72 20 73 68 6f 75 6c 64 0d 0a 2a 2a 20 62 65 20  r should..** be 
4696c 70 61 73 73 65 64 20 4e 55 4c 4c 2e 20 54 68 69  passed NULL. Thi
4696d 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  s function will 
4696e 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 53  allocate a new S
4696f 65 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a  egmentWriter obj
46970 65 63 74 0d 0a 2a 2a 20 61 6e 64 20 72 65 74 75  ect..** and retu
46971 72 6e 20 69 74 20 76 69 61 20 74 68 65 20 69 6e  rn it via the in
46972 70 75 74 2f 6f 75 74 70 75 74 20 76 61 72 69 61  put/output varia
46973 62 6c 65 20 2a 70 70 57 72 69 74 65 72 20 69 6e  ble *ppWriter in
46974 20 74 68 69 73 20 63 61 73 65 2e 0d 0a 2a 2a 0d   this case...**.
46975 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
46976 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
46977 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
46978 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
46979 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a  rror code...*/..
4697a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
4697b 65 67 57 72 69 74 65 72 41 64 64 28 0d 0a 20 20  egWriterAdd(..  
4697c 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
4697d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4697e 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
4697f 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 53 65   handle */..  Se
46980 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 2a 70 70  gmentWriter **pp
46981 57 72 69 74 65 72 2c 20 20 20 20 20 20 20 2f 2a  Writer,       /*
46982 20 49 4e 2f 4f 55 54 3a 20 53 65 67 6d 65 6e 74   IN/OUT: Segment
46983 57 72 69 74 65 72 20 68 61 6e 64 6c 65 20 2a 2f  Writer handle */
46984 20 0d 0a 20 20 69 6e 74 20 69 73 43 6f 70 79 54   ..  int isCopyT
46985 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
46986 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
46987 62 75 66 66 65 72 20 7a 54 65 72 6d 20 6d 75 73  buffer zTerm mus
46988 74 20 62 65 20 63 6f 70 69 65 64 20 2a 2f 0d 0a  t be copied */..
46989 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
4698a 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
4698b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
4698c 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
4698d 67 20 74 65 72 6d 20 2a 2f 0d 0a 20 20 69 6e 74  g term */..  int
4698e 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20   nTerm,         
4698f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46990 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 69 6e 20  Size of term in 
46991 62 79 74 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73  bytes */..  cons
46992 74 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74  t char *aDoclist
46993 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ,           /* P
46994 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72  ointer to buffer
46995 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 6f 63 6c   containing docl
46996 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 44  ist */..  int nD
46997 6f 63 6c 69 73 74 20 20 20 20 20 20 20 20 20 20  oclist          
46998 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
46999 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e 20  e of doclist in 
4699a 62 79 74 65 73 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  bytes */..){..  
4699b 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20  int nPrefix;    
4699c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4699d 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20  /* Size of term 
4699e 70 72 65 66 69 78 20 69 6e 20 62 79 74 65 73 20  prefix in bytes 
4699f 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 75 66 66 69  */..  int nSuffi
469a0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
469a1 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
469a2 20 74 65 72 6d 20 73 75 66 66 69 78 20 69 6e 20   term suffix in 
469a3 62 79 74 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  bytes */..  int 
469a4 6e 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  nReq;           
469a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
469a6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
469a7 65 71 75 69 72 65 64 20 6f 6e 20 6c 65 61 66 20  equired on leaf 
469a8 70 61 67 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  page */..  int n
469a9 44 61 74 61 3b 0d 0a 20 20 53 65 67 6d 65 6e 74  Data;..  Segment
469aa 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 20  Writer *pWriter 
469ab 3d 20 2a 70 70 57 72 69 74 65 72 3b 0d 0a 0d 0a  = *ppWriter;....
469ac 20 20 69 66 28 20 21 70 57 72 69 74 65 72 20 29    if( !pWriter )
469ad 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 3b 0d 0a  {..    int rc;..
469ae 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74      sqlite3_stmt
469af 20 2a 70 53 74 6d 74 3b 0d 0a 0d 0a 20 20 20 20   *pStmt;....    
469b0 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
469b1 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 73 74  SegmentWriter st
469b2 72 75 63 74 75 72 65 20 2a 2f 0d 0a 20 20 20 20  ructure */..    
469b3 70 57 72 69 74 65 72 20 3d 20 28 53 65 67 6d 65  pWriter = (Segme
469b4 6e 74 57 72 69 74 65 72 20 2a 29 73 71 6c 69 74  ntWriter *)sqlit
469b5 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
469b6 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72 29 29  (SegmentWriter))
469b7 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 57 72 69  ;..    if( !pWri
469b8 74 65 72 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ter ) return SQL
469b9 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
469ba 6d 65 6d 73 65 74 28 70 57 72 69 74 65 72 2c 20  memset(pWriter, 
469bb 30 2c 20 73 69 7a 65 6f 66 28 53 65 67 6d 65 6e  0, sizeof(Segmen
469bc 74 57 72 69 74 65 72 29 29 3b 0d 0a 20 20 20 20  tWriter));..    
469bd 2a 70 70 57 72 69 74 65 72 20 3d 20 70 57 72 69  *ppWriter = pWri
469be 74 65 72 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41  ter;....    /* A
469bf 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65 72  llocate a buffer
469c0 20 69 6e 20 77 68 69 63 68 20 74 6f 20 61 63 63   in which to acc
469c1 75 6d 75 6c 61 74 65 20 64 61 74 61 20 2a 2f 0d  umulate data */.
469c2 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 61 44  .    pWriter->aD
469c3 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ata = (char *)sq
469c4 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 2d 3e  lite3_malloc(p->
469c5 6e 4e 6f 64 65 53 69 7a 65 29 3b 0d 0a 20 20 20  nNodeSize);..   
469c6 20 69 66 28 20 21 70 57 72 69 74 65 72 2d 3e 61   if( !pWriter->a
469c7 44 61 74 61 20 29 20 72 65 74 75 72 6e 20 53 51  Data ) return SQ
469c8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
469c9 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65 20   pWriter->nSize 
469ca 3d 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 3b 0d  = p->nNodeSize;.
469cb 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74  ...    /* Find t
469cc 68 65 20 6e 65 78 74 20 66 72 65 65 20 62 6c 6f  he next free blo
469cd 63 6b 69 64 20 69 6e 20 74 68 65 20 25 5f 73 65  ckid in the %_se
469ce 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 2a 2f 0d  gments table */.
469cf 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71  .    rc = fts3Sq
469d0 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 4e 45 58  lStmt(p, SQL_NEX
469d1 54 5f 53 45 47 4d 45 4e 54 53 5f 49 44 2c 20 26  T_SEGMENTS_ID, &
469d2 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20 20  pStmt, 0);..    
469d3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
469d4 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  K ) return rc;..
469d5 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52      if( SQLITE_R
469d6 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
469d7 28 70 53 74 6d 74 29 20 29 7b 0d 0a 20 20 20 20  (pStmt) ){..    
469d8 20 20 70 57 72 69 74 65 72 2d 3e 69 46 72 65 65    pWriter->iFree
469d9 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
469da 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 30  n_int64(pStmt, 0
469db 29 3b 0d 0a 20 20 20 20 20 20 70 57 72 69 74 65  );..      pWrite
469dc 72 2d 3e 69 46 69 72 73 74 20 3d 20 70 57 72 69  r->iFirst = pWri
469dd 74 65 72 2d 3e 69 46 72 65 65 3b 0d 0a 20 20 20  ter->iFree;..   
469de 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
469df 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
469e0 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d  );..    if( rc!=
469e1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
469e2 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20 6e  rn rc;..  }..  n
469e3 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d 3e  Data = pWriter->
469e4 6e 44 61 74 61 3b 0d 0a 0d 0a 20 20 6e 50 72 65  nData;....  nPre
469e5 66 69 78 20 3d 20 66 74 73 33 50 72 65 66 69 78  fix = fts3Prefix
469e6 43 6f 6d 70 72 65 73 73 28 70 57 72 69 74 65 72  Compress(pWriter
469e7 2d 3e 7a 54 65 72 6d 2c 20 70 57 72 69 74 65 72  ->zTerm, pWriter
469e8 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20  ->nTerm, zTerm, 
469e9 6e 54 65 72 6d 29 3b 0d 0a 20 20 6e 53 75 66 66  nTerm);..  nSuff
469ea 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50 72 65 66  ix = nTerm-nPref
469eb 69 78 3b 0d 0a 0d 0a 20 20 2f 2a 20 46 69 67 75  ix;....  /* Figu
469ec 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
469ed 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72  bytes are requir
469ee 65 64 20 62 79 20 74 68 69 73 20 6e 65 77 20 65  ed by this new e
469ef 6e 74 72 79 20 2a 2f 0d 0a 20 20 6e 52 65 71 20  ntry */..  nReq 
469f0 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  = sqlite3Fts3Var
469f1 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69 78 29 20  intLen(nPrefix) 
469f2 2b 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63  +    /* varint c
469f3 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66 69 78  ontaining prefix
469f4 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 20 20 73 71   size */..    sq
469f5 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c  lite3Fts3VarintL
469f6 65 6e 28 6e 53 75 66 66 69 78 29 20 2b 20 20 20  en(nSuffix) +   
469f7 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20        /* varint 
469f8 63 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66 69  containing suffi
469f9 78 20 73 69 7a 65 20 2a 2f 0d 0a 20 20 20 20 6e  x size */..    n
469fa 53 75 66 66 69 78 20 2b 20 20 20 20 20 20 20 20  Suffix +        
469fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
469fc 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73         /* Term s
469fd 75 66 66 69 78 20 2a 2f 0d 0a 20 20 20 20 73 71  uffix */..    sq
469fe 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c  lite3Fts3VarintL
469ff 65 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b 20 20  en(nDoclist) +  
46a00 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
46a01 20 64 6f 63 6c 69 73 74 20 2a 2f 0d 0a 20 20 20   doclist */..   
46a02 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20   nDoclist;      
46a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46a04 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c           /* Docl
46a05 69 73 74 20 64 61 74 61 20 2a 2f 0d 0a 0d 0a 20  ist data */.... 
46a06 20 69 66 28 20 6e 44 61 74 61 3e 30 20 26 26 20   if( nData>0 && 
46a07 6e 44 61 74 61 2b 6e 52 65 71 3e 70 2d 3e 6e 4e  nData+nReq>p->nN
46a08 6f 64 65 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20  odeSize ){..    
46a09 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 20 20 2f  int rc;....    /
46a0a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 65  * The current le
46a0b 61 66 20 6e 6f 64 65 20 69 73 20 66 75 6c 6c 2e  af node is full.
46a0c 20 57 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f   Write it out to
46a0d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 2a   the database. *
46a0e 2f 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  /..    rc = fts3
46a0f 57 72 69 74 65 53 65 67 6d 65 6e 74 28 70 2c 20  WriteSegment(p, 
46a10 70 57 72 69 74 65 72 2d 3e 69 46 72 65 65 2b 2b  pWriter->iFree++
46a11 2c 20 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61  , pWriter->aData
46a12 2c 20 6e 44 61 74 61 29 3b 0d 0a 20 20 20 20 69  , nData);..    i
46a13 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
46a14 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 0d   ) return rc;...
46a15 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
46a16 63 75 72 72 65 6e 74 20 74 65 72 6d 20 74 6f 20  current term to 
46a17 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  the interior nod
46a18 65 20 74 72 65 65 2e 20 54 68 65 20 74 65 72 6d  e tree. The term
46a19 20 61 64 64 65 64 20 74 6f 0d 0a 20 20 20 20 2a   added to..    *
46a1a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 74  * the interior t
46a1b 72 65 65 20 6d 75 73 74 3a 0d 0a 20 20 20 20 2a  ree must:..    *
46a1c 2a 0d 0a 20 20 20 20 2a 2a 20 20 20 61 29 20 62  *..    **   a) b
46a1d 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  e greater than t
46a1e 68 65 20 6c 61 72 67 65 73 74 20 74 65 72 6d 20  he largest term 
46a1f 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  on the leaf node
46a20 20 6a 75 73 74 20 77 72 69 74 74 65 6e 0d 0a 20   just written.. 
46a21 20 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 74 68     **      to th
46a22 65 20 64 61 74 61 62 61 73 65 20 28 73 74 69 6c  e database (stil
46a23 6c 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 70  l available in p
46a24 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 29 2c 20  Writer->zTerm), 
46a25 61 6e 64 0d 0a 20 20 20 20 2a 2a 0d 0a 20 20 20  and..    **..   
46a26 20 2a 2a 20 20 20 62 29 20 62 65 20 6c 65 73 73   **   b) be less
46a27 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
46a28 6f 20 74 68 65 20 74 65 72 6d 20 61 62 6f 75 74  o the term about
46a29 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
46a2a 74 68 65 20 6e 65 77 0d 0a 20 20 20 20 2a 2a 20  the new..    ** 
46a2b 20 20 20 20 20 6c 65 61 66 20 6e 6f 64 65 20 28       leaf node (
46a2c 7a 54 65 72 6d 2f 6e 54 65 72 6d 29 2e 0d 0a 20  zTerm/nTerm)... 
46a2d 20 20 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 49 6e     **..    ** In
46a2e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74   other words, it
46a2f 20 6d 75 73 74 20 62 65 20 74 68 65 20 70 72 65   must be the pre
46a30 66 69 78 20 6f 66 20 7a 54 65 72 6d 20 31 20 62  fix of zTerm 1 b
46a31 79 74 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 0d  yte longer than.
46a32 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 6d  .    ** the comm
46a33 6f 6e 20 70 72 65 66 69 78 20 28 69 66 20 61 6e  on prefix (if an
46a34 79 29 20 6f 66 20 7a 54 65 72 6d 20 61 6e 64 20  y) of zTerm and 
46a35 70 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 2e 0d  pWriter->zTerm..
46a36 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73  .    */..    ass
46a37 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e 54 65  ert( nPrefix<nTe
46a38 72 6d 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20  rm );..    rc = 
46a39 66 74 73 33 4e 6f 64 65 41 64 64 54 65 72 6d 28  fts3NodeAddTerm(
46a3a 70 2c 20 26 70 57 72 69 74 65 72 2d 3e 70 54 72  p, &pWriter->pTr
46a3b 65 65 2c 20 69 73 43 6f 70 79 54 65 72 6d 2c 20  ee, isCopyTerm, 
46a3c 7a 54 65 72 6d 2c 20 6e 50 72 65 66 69 78 2b 31  zTerm, nPrefix+1
46a3d 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d  );..    if( rc!=
46a3e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
46a3f 72 6e 20 72 63 3b 0d 0a 0d 0a 20 20 20 20 6e 44  rn rc;....    nD
46a40 61 74 61 20 3d 20 30 3b 0d 0a 20 20 20 20 70 57  ata = 0;..    pW
46a41 72 69 74 65 72 2d 3e 6e 54 65 72 6d 20 3d 20 30  riter->nTerm = 0
46a42 3b 0d 0a 0d 0a 20 20 20 20 6e 50 72 65 66 69 78  ;....    nPrefix
46a43 20 3d 20 30 3b 0d 0a 20 20 20 20 6e 53 75 66 66   = 0;..    nSuff
46a44 69 78 20 3d 20 6e 54 65 72 6d 3b 0d 0a 20 20 20  ix = nTerm;..   
46a45 20 6e 52 65 71 20 3d 20 31 20 2b 20 20 20 20 20   nReq = 1 +     
46a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46a47 20 20 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69           /* vari
46a48 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 72  nt containing pr
46a49 65 66 69 78 20 73 69 7a 65 20 2a 2f 0d 0a 20 20  efix size */..  
46a4a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 56      sqlite3Fts3V
46a4b 61 72 69 6e 74 4c 65 6e 28 6e 54 65 72 6d 29 20  arintLen(nTerm) 
46a4c 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 76 61 72  +         /* var
46a4d 69 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73  int containing s
46a4e 75 66 66 69 78 20 73 69 7a 65 20 2a 2f 0d 0a 20  uffix size */.. 
46a4f 20 20 20 20 20 6e 54 65 72 6d 20 2b 20 20 20 20       nTerm +    
46a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46a51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
46a52 72 6d 20 73 75 66 66 69 78 20 2a 2f 0d 0a 20 20  rm suffix */..  
46a53 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 56      sqlite3Fts3V
46a54 61 72 69 6e 74 4c 65 6e 28 6e 44 6f 63 6c 69 73  arintLen(nDoclis
46a55 74 29 20 2b 20 20 20 20 20 20 2f 2a 20 53 69 7a  t) +      /* Siz
46a56 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 2a 2f 0d  e of doclist */.
46a57 0a 20 20 20 20 20 20 6e 44 6f 63 6c 69 73 74 3b  .      nDoclist;
46a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46a59 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46a5a 44 6f 63 6c 69 73 74 20 64 61 74 61 20 2a 2f 0d  Doclist data */.
46a5b 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
46a5c 74 68 65 20 62 75 66 66 65 72 20 63 75 72 72 65  the buffer curre
46a5d 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69  ntly allocated i
46a5e 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20  s too small for 
46a5f 74 68 69 73 20 65 6e 74 72 79 2c 20 72 65 61 6c  this entry, real
46a60 6c 6f 63 0d 0a 20 20 2a 2a 20 74 68 65 20 62 75  loc..  ** the bu
46a61 66 66 65 72 20 74 6f 20 6d 61 6b 65 20 69 74 20  ffer to make it 
46a62 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 0d 0a 20  large enough... 
46a63 20 2a 2f 0d 0a 20 20 69 66 28 20 6e 52 65 71 3e   */..  if( nReq>
46a64 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65 20 29  pWriter->nSize )
46a65 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 61 4e 65  {..    char *aNe
46a66 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
46a67 6c 6f 63 28 70 57 72 69 74 65 72 2d 3e 61 44 61  loc(pWriter->aDa
46a68 74 61 2c 20 6e 52 65 71 29 3b 0d 0a 20 20 20 20  ta, nReq);..    
46a69 69 66 28 20 21 61 4e 65 77 20 29 20 72 65 74 75  if( !aNew ) retu
46a6a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
46a6b 0d 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 61  ..    pWriter->a
46a6c 44 61 74 61 20 3d 20 61 4e 65 77 3b 0d 0a 20 20  Data = aNew;..  
46a6d 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65    pWriter->nSize
46a6e 20 3d 20 6e 52 65 71 3b 0d 0a 20 20 7d 0d 0a 20   = nReq;..  }.. 
46a6f 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 2b 6e   assert( nData+n
46a70 52 65 71 3c 3d 70 57 72 69 74 65 72 2d 3e 6e 53  Req<=pWriter->nS
46a71 69 7a 65 20 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41  ize );....  /* A
46a72 70 70 65 6e 64 20 74 68 65 20 70 72 65 66 69 78  ppend the prefix
46a73 2d 63 6f 6d 70 72 65 73 73 65 64 20 74 65 72 6d  -compressed term
46a74 20 61 6e 64 20 64 6f 63 6c 69 73 74 20 74 6f 20   and doclist to 
46a75 74 68 65 20 62 75 66 66 65 72 2e 20 2a 2f 0d 0a  the buffer. */..
46a76 20 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74    nData += sqlit
46a77 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28  e3Fts3PutVarint(
46a78 26 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b  &pWriter->aData[
46a79 6e 44 61 74 61 5d 2c 20 6e 50 72 65 66 69 78 29  nData], nPrefix)
46a7a 3b 0d 0a 20 20 6e 44 61 74 61 20 2b 3d 20 73 71  ;..  nData += sq
46a7b 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69  lite3Fts3PutVari
46a7c 6e 74 28 26 70 57 72 69 74 65 72 2d 3e 61 44 61  nt(&pWriter->aDa
46a7d 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 53 75 66 66  ta[nData], nSuff
46a7e 69 78 29 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 26  ix);..  memcpy(&
46a7f 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e  pWriter->aData[n
46a80 44 61 74 61 5d 2c 20 26 7a 54 65 72 6d 5b 6e 50  Data], &zTerm[nP
46a81 72 65 66 69 78 5d 2c 20 6e 53 75 66 66 69 78 29  refix], nSuffix)
46a82 3b 0d 0a 20 20 6e 44 61 74 61 20 2b 3d 20 6e 53  ;..  nData += nS
46a83 75 66 66 69 78 3b 0d 0a 20 20 6e 44 61 74 61 20  uffix;..  nData 
46a84 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  += sqlite3Fts3Pu
46a85 74 56 61 72 69 6e 74 28 26 70 57 72 69 74 65 72  tVarint(&pWriter
46a86 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20  ->aData[nData], 
46a87 6e 44 6f 63 6c 69 73 74 29 3b 0d 0a 20 20 6d 65  nDoclist);..  me
46a88 6d 63 70 79 28 26 70 57 72 69 74 65 72 2d 3e 61  mcpy(&pWriter->a
46a89 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20 61 44 6f  Data[nData], aDo
46a8a 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69 73 74 29  clist, nDoclist)
46a8b 3b 0d 0a 20 20 70 57 72 69 74 65 72 2d 3e 6e 44  ;..  pWriter->nD
46a8c 61 74 61 20 3d 20 6e 44 61 74 61 20 2b 20 6e 44  ata = nData + nD
46a8d 6f 63 6c 69 73 74 3b 0d 0a 0d 0a 20 20 2f 2a 20  oclist;....  /* 
46a8e 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  Save the current
46a8f 20 74 65 72 6d 20 73 6f 20 74 68 61 74 20 69 74   term so that it
46a90 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
46a91 70 72 65 66 69 78 2d 63 6f 6d 70 72 65 73 73 20  prefix-compress 
46a92 74 68 65 20 6e 65 78 74 2e 0d 0a 20 20 2a 2a 20  the next...  ** 
46a93 49 66 20 74 68 65 20 69 73 43 6f 70 79 54 65 72  If the isCopyTer
46a94 6d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  m parameter is t
46a95 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 62 75  rue, then the bu
46a96 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
46a97 62 79 0d 0a 20 20 2a 2a 20 7a 54 65 72 6d 20 69  by..  ** zTerm i
46a98 73 20 74 72 61 6e 73 69 65 6e 74 2c 20 73 6f 20  s transient, so 
46a99 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  take a copy of t
46a9a 68 65 20 74 65 72 6d 20 64 61 74 61 2e 20 4f 74  he term data. Ot
46a9b 68 65 72 77 69 73 65 2c 20 6a 75 73 74 0d 0a 20  herwise, just.. 
46a9c 20 2a 2a 20 73 74 6f 72 65 20 61 20 63 6f 70 79   ** store a copy
46a9d 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2e   of the pointer.
46a9e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 69 73  ..  */..  if( is
46a9f 43 6f 70 79 54 65 72 6d 20 29 7b 0d 0a 20 20 20  CopyTerm ){..   
46aa0 20 69 66 28 20 6e 54 65 72 6d 3e 70 57 72 69 74   if( nTerm>pWrit
46aa1 65 72 2d 3e 6e 4d 61 6c 6c 6f 63 20 29 7b 0d 0a  er->nMalloc ){..
46aa2 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77        char *zNew
46aa3 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
46aa4 6f 63 28 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c  oc(pWriter->zMal
46aa5 6c 6f 63 2c 20 6e 54 65 72 6d 2a 32 29 3b 0d 0a  loc, nTerm*2);..
46aa6 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20        if( !zNew 
46aa7 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){..        retu
46aa8 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
46aa9 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
46aaa 20 70 57 72 69 74 65 72 2d 3e 6e 4d 61 6c 6c 6f   pWriter->nMallo
46aab 63 20 3d 20 6e 54 65 72 6d 2a 32 3b 0d 0a 20 20  c = nTerm*2;..  
46aac 20 20 20 20 70 57 72 69 74 65 72 2d 3e 7a 4d 61      pWriter->zMa
46aad 6c 6c 6f 63 20 3d 20 7a 4e 65 77 3b 0d 0a 20 20  lloc = zNew;..  
46aae 20 20 20 20 70 57 72 69 74 65 72 2d 3e 7a 54 65      pWriter->zTe
46aaf 72 6d 20 3d 20 7a 4e 65 77 3b 0d 0a 20 20 20 20  rm = zNew;..    
46ab0 7d 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  }..    assert( p
46ab1 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 3d 3d 70  Writer->zTerm==p
46ab2 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 20  Writer->zMalloc 
46ab3 29 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  );..    memcpy(p
46ab4 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 7a  Writer->zTerm, z
46ab5 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0d 0a 20  Term, nTerm);.. 
46ab6 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 70 57 72   }else{..    pWr
46ab7 69 74 65 72 2d 3e 7a 54 65 72 6d 20 3d 20 28 63  iter->zTerm = (c
46ab8 68 61 72 20 2a 29 7a 54 65 72 6d 3b 0d 0a 20 20  har *)zTerm;..  
46ab9 7d 0d 0a 20 20 70 57 72 69 74 65 72 2d 3e 6e 54  }..  pWriter->nT
46aba 65 72 6d 20 3d 20 6e 54 65 72 6d 3b 0d 0a 0d 0a  erm = nTerm;....
46abb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
46abc 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  OK;..}..../*..**
46abd 20 46 6c 75 73 68 20 61 6c 6c 20 64 61 74 61 20   Flush all data 
46abe 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
46abf 74 68 65 20 53 65 67 6d 65 6e 74 57 72 69 74 65  the SegmentWrite
46ac0 72 20 6f 62 6a 65 63 74 20 70 57 72 69 74 65 72  r object pWriter
46ac1 20 74 6f 20 74 68 65 0d 0a 2a 2a 20 64 61 74 61   to the..** data
46ac2 62 61 73 65 2e 20 54 68 69 73 20 66 75 6e 63 74  base. This funct
46ac3 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ion must be call
46ac4 65 64 20 61 66 74 65 72 20 61 6c 6c 20 74 65 72  ed after all ter
46ac5 6d 73 20 68 61 76 65 20 62 65 65 6e 20 61 64 64  ms have been add
46ac6 65 64 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 65  ed..** to the se
46ac7 67 6d 65 6e 74 20 75 73 69 6e 67 20 66 74 73 33  gment using fts3
46ac8 53 65 67 57 72 69 74 65 72 41 64 64 28 29 2e 20  SegWriterAdd(). 
46ac9 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  If successful, S
46aca 51 4c 49 54 45 5f 4f 4b 20 69 73 0d 0a 2a 2a 20  QLITE_OK is..** 
46acb 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
46acc 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
46acd 72 72 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a  rror code...*/..
46ace 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
46acf 65 67 57 72 69 74 65 72 46 6c 75 73 68 28 0d 0a  egWriterFlush(..
46ad0 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
46ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46ad2 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
46ad3 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  le handle */..  
46ad4 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70  SegmentWriter *p
46ad5 57 72 69 74 65 72 2c 20 20 20 20 20 20 20 20 20  Writer,         
46ad6 2f 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  /* SegmentWriter
46ad7 20 74 6f 20 66 6c 75 73 68 20 74 6f 20 74 68 65   to flush to the
46ad8 20 64 62 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c   db */..  int iL
46ad9 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20  evel,           
46ada 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
46adb 75 65 20 66 6f 72 20 27 6c 65 76 65 6c 27 20 63  ue for 'level' c
46adc 6f 6c 75 6d 6e 20 6f 66 20 25 5f 73 65 67 64 69  olumn of %_segdi
46add 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 49 64 78  r */..  int iIdx
46ade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46adf 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
46ae0 20 66 6f 72 20 27 69 64 78 27 20 63 6f 6c 75 6d   for 'idx' colum
46ae1 6e 20 6f 66 20 25 5f 73 65 67 64 69 72 20 2a 2f  n of %_segdir */
46ae2 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20  ..){..  int rc; 
46ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46ae4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
46ae5 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69 66 28  n code */..  if(
46ae6 20 70 57 72 69 74 65 72 2d 3e 70 54 72 65 65 20   pWriter->pTree 
46ae7 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ){..    sqlite3_
46ae8 69 6e 74 36 34 20 69 4c 61 73 74 20 3d 20 30 3b  int64 iLast = 0;
46ae9 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74        /* Largest
46aea 20 62 6c 6f 63 6b 20 69 64 20 77 72 69 74 74 65   block id writte
46aeb 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 2a 2f  n to database */
46aec 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  ..    sqlite3_in
46aed 74 36 34 20 69 4c 61 73 74 4c 65 61 66 3b 20 20  t64 iLastLeaf;  
46aee 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 6c      /* Largest l
46aef 65 61 66 20 62 6c 6f 63 6b 20 69 64 20 77 72 69  eaf block id wri
46af0 74 74 65 6e 20 74 6f 20 64 62 20 2a 2f 0d 0a 20  tten to db */.. 
46af1 20 20 20 63 68 61 72 20 2a 7a 52 6f 6f 74 20 3d     char *zRoot =
46af2 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20   NULL;          
46af3 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62   /* Pointer to b
46af4 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
46af5 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0d 0a 20   root node */.. 
46af6 20 20 20 69 6e 74 20 6e 52 6f 6f 74 20 3d 20 30     int nRoot = 0
46af7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
46af8 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66   /* Size of buff
46af9 65 72 20 7a 52 6f 6f 74 20 2a 2f 0d 0a 0d 0a 20  er zRoot */.... 
46afa 20 20 20 69 4c 61 73 74 4c 65 61 66 20 3d 20 70     iLastLeaf = p
46afb 57 72 69 74 65 72 2d 3e 69 46 72 65 65 3b 0d 0a  Writer->iFree;..
46afc 20 20 20 20 72 63 20 3d 20 66 74 73 33 57 72 69      rc = fts3Wri
46afd 74 65 53 65 67 6d 65 6e 74 28 70 2c 20 70 57 72  teSegment(p, pWr
46afe 69 74 65 72 2d 3e 69 46 72 65 65 2b 2b 2c 20 70  iter->iFree++, p
46aff 57 72 69 74 65 72 2d 3e 61 44 61 74 61 2c 20 70  Writer->aData, p
46b00 57 72 69 74 65 72 2d 3e 6e 44 61 74 61 29 3b 0d  Writer->nData);.
46b01 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
46b02 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
46b03 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 57 72   rc = fts3NodeWr
46b04 69 74 65 28 70 2c 20 70 57 72 69 74 65 72 2d 3e  ite(p, pWriter->
46b05 70 54 72 65 65 2c 20 31 2c 0d 0a 20 20 20 20 20  pTree, 1,..     
46b06 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 46       pWriter->iF
46b07 69 72 73 74 2c 20 70 57 72 69 74 65 72 2d 3e 69  irst, pWriter->i
46b08 46 72 65 65 2c 20 26 69 4c 61 73 74 2c 20 26 7a  Free, &iLast, &z
46b09 52 6f 6f 74 2c 20 26 6e 52 6f 6f 74 29 3b 0d 0a  Root, &nRoot);..
46b0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
46b0b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
46b0c 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
46b0d 57 72 69 74 65 53 65 67 64 69 72 28 0d 0a 20 20  WriteSegdir(..  
46b0e 20 20 20 20 20 20 20 20 70 2c 20 69 4c 65 76 65          p, iLeve
46b0f 6c 2c 20 69 49 64 78 2c 20 70 57 72 69 74 65 72  l, iIdx, pWriter
46b10 2d 3e 69 46 69 72 73 74 2c 20 69 4c 61 73 74 4c  ->iFirst, iLastL
46b11 65 61 66 2c 20 69 4c 61 73 74 2c 20 7a 52 6f 6f  eaf, iLast, zRoo
46b12 74 2c 20 6e 52 6f 6f 74 29 3b 0d 0a 20 20 20 20  t, nRoot);..    
46b13 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  }..  }else{..   
46b14 20 2f 2a 20 54 68 65 20 65 6e 74 69 72 65 20 74   /* The entire t
46b15 72 65 65 20 66 69 74 73 20 6f 6e 20 74 68 65 20  ree fits on the 
46b16 72 6f 6f 74 20 6e 6f 64 65 2e 20 57 72 69 74 65  root node. Write
46b17 20 69 74 20 74 6f 20 74 68 65 20 73 65 67 64 69   it to the segdi
46b18 72 20 74 61 62 6c 65 2e 20 2a 2f 0d 0a 20 20 20  r table. */..   
46b19 20 72 63 20 3d 20 66 74 73 33 57 72 69 74 65 53   rc = fts3WriteS
46b1a 65 67 64 69 72 28 0d 0a 20 20 20 20 20 20 20 20  egdir(..        
46b1b 70 2c 20 69 4c 65 76 65 6c 2c 20 69 49 64 78 2c  p, iLevel, iIdx,
46b1c 20 30 2c 20 30 2c 20 30 2c 20 70 57 72 69 74 65   0, 0, 0, pWrite
46b1d 72 2d 3e 61 44 61 74 61 2c 20 70 57 72 69 74 65  r->aData, pWrite
46b1e 72 2d 3e 6e 44 61 74 61 29 3b 0d 0a 20 20 7d 0d  r->nData);..  }.
46b1f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
46b20 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 6c 65 61  ..../*..** Relea
46b21 73 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 68 65  se all memory he
46b22 6c 64 20 62 79 20 74 68 65 20 53 65 67 6d 65 6e  ld by the Segmen
46b23 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74 20 70  tWriter object p
46b24 61 73 73 65 64 20 61 73 20 74 68 65 20 0d 0a 2a  assed as the ..*
46b25 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * first argument
46b26 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
46b27 69 64 20 66 74 73 33 53 65 67 57 72 69 74 65 72  id fts3SegWriter
46b28 46 72 65 65 28 53 65 67 6d 65 6e 74 57 72 69 74  Free(SegmentWrit
46b29 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0d 0a 20  er *pWriter){.. 
46b2a 20 69 66 28 20 70 57 72 69 74 65 72 20 29 7b 0d   if( pWriter ){.
46b2b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
46b2c 65 28 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61  e(pWriter->aData
46b2d 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
46b2e 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e 7a 4d  free(pWriter->zM
46b2f 61 6c 6c 6f 63 29 3b 0d 0a 20 20 20 20 66 74 73  alloc);..    fts
46b30 33 4e 6f 64 65 46 72 65 65 28 70 57 72 69 74 65  3NodeFree(pWrite
46b31 72 2d 3e 70 54 72 65 65 29 3b 0d 0a 20 20 20 20  r->pTree);..    
46b32 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72  sqlite3_free(pWr
46b33 69 74 65 72 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  iter);..  }..}..
46b34 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 69 72  ../*..** The fir
46b35 73 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  st value in the 
46b36 61 70 56 61 6c 5b 5d 20 61 72 72 61 79 20 69 73  apVal[] array is
46b37 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74   assumed to cont
46b38 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 0d  ain an integer..
46b39 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
46b3a 6e 20 74 65 73 74 73 20 69 66 20 74 68 65 72 65  n tests if there
46b3b 20 65 78 69 73 74 20 61 6e 79 20 64 6f 63 75 6d   exist any docum
46b3c 65 6e 74 73 20 77 69 74 68 20 64 6f 63 69 64 20  ents with docid 
46b3d 76 61 6c 75 65 73 20 74 68 61 74 0d 0a 2a 2a 20  values that..** 
46b3e 61 72 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  are different fr
46b3f 6f 6d 20 74 68 61 74 20 69 6e 74 65 67 65 72 2e  om that integer.
46b40 20 69 2e 65 2e 20 69 66 20 64 65 6c 65 74 69 6e   i.e. if deletin
46b41 67 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 77  g the document w
46b42 69 74 68 20 64 6f 63 69 64 0d 0a 2a 2a 20 70 52  ith docid..** pR
46b43 6f 77 69 64 20 77 6f 75 6c 64 20 6d 65 61 6e 20  owid would mean 
46b44 74 68 65 20 46 54 53 33 20 74 61 62 6c 65 20 77  the FTS3 table w
46b45 65 72 65 20 65 6d 70 74 79 2e 0d 0a 2a 2a 0d 0a  ere empty...**..
46b46 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
46b47 2c 20 2a 70 69 73 45 6d 70 74 79 20 69 73 20 73  , *pisEmpty is s
46b48 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 74 68  et to true if th
46b49 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
46b4a 20 65 78 63 65 70 74 20 66 6f 72 0d 0a 2a 2a 20   except for..** 
46b4b 64 6f 63 75 6d 65 6e 74 20 70 52 6f 77 69 64 2c  document pRowid,
46b4c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77   or false otherw
46b4d 69 73 65 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f  ise, and SQLITE_
46b4e 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
46b4f 49 66 20 61 6e 0d 0a 2a 2a 20 65 72 72 6f 72 20  If an..** error 
46b50 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 69 74  occurs, an SQLit
46b51 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
46b52 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73  returned...*/..s
46b53 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 73  tatic int fts3Is
46b54 45 6d 70 74 79 28 46 74 73 33 54 61 62 6c 65 20  Empty(Fts3Table 
46b55 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  *p, sqlite3_valu
46b56 65 20 2a 70 52 6f 77 69 64 2c 20 69 6e 74 20 2a  e *pRowid, int *
46b57 70 69 73 45 6d 70 74 79 29 7b 0d 0a 20 20 73 71  pisEmpty){..  sq
46b58 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
46b59 74 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  t;..  int rc;.. 
46b5a 20 69 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74   if( p->zContent
46b5b 54 62 6c 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 49  Tbl ){..    /* I
46b5c 66 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6e 74  f using the cont
46b5d 65 6e 74 3d 78 78 78 20 6f 70 74 69 6f 6e 2c 20  ent=xxx option, 
46b5e 61 73 73 75 6d 65 20 74 68 65 20 74 61 62 6c 65  assume the table
46b5f 20 69 73 20 6e 65 76 65 72 20 65 6d 70 74 79 20   is never empty 
46b60 2a 2f 0d 0a 20 20 20 20 2a 70 69 73 45 6d 70 74  */..    *pisEmpt
46b61 79 20 3d 20 30 3b 0d 0a 20 20 20 20 72 63 20 3d  y = 0;..    rc =
46b62 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d   SQLITE_OK;..  }
46b63 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63 20 3d 20  else{..    rc = 
46b64 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53  fts3SqlStmt(p, S
46b65 51 4c 5f 49 53 5f 45 4d 50 54 59 2c 20 26 70 53  QL_IS_EMPTY, &pS
46b66 74 6d 74 2c 20 26 70 52 6f 77 69 64 29 3b 0d 0a  tmt, &pRowid);..
46b67 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
46b68 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
46b69 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
46b6a 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
46b6b 6d 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  mt) ){..        
46b6c 2a 70 69 73 45 6d 70 74 79 20 3d 20 73 71 6c 69  *pisEmpty = sqli
46b6d 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70  te3_column_int(p
46b6e 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20 20 20  Stmt, 0);..     
46b6f 20 7d 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73   }..      rc = s
46b70 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
46b71 6d 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  mt);..    }..  }
46b72 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
46b73 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74 20  }..../*..** Set 
46b74 2a 70 6e 4d 61 78 20 74 6f 20 74 68 65 20 6c 61  *pnMax to the la
46b75 72 67 65 73 74 20 73 65 67 6d 65 6e 74 20 6c 65  rgest segment le
46b76 76 65 6c 20 69 6e 20 74 68 65 20 64 61 74 61 62  vel in the datab
46b77 61 73 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65  ase for the inde
46b78 78 0d 0a 2a 2a 20 69 49 6e 64 65 78 2e 0d 0a 2a  x..** iIndex...*
46b79 2a 0d 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65  *..** Segment le
46b7a 76 65 6c 73 20 61 72 65 20 73 74 6f 72 65 64 20  vels are stored 
46b7b 69 6e 20 74 68 65 20 27 6c 65 76 65 6c 27 20 63  in the 'level' c
46b7c 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 25 5f 73  olumn of the %_s
46b7d 65 67 64 69 72 20 74 61 62 6c 65 2e 0d 0a 2a 2a  egdir table...**
46b7e 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
46b7f 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73  TE_OK if success
46b80 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74  ful, or an SQLit
46b81 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  e error code if 
46b82 6e 6f 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  not...*/..static
46b83 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e 74   int fts3Segment
46b84 4d 61 78 4c 65 76 65 6c 28 46 74 73 33 54 61 62  MaxLevel(Fts3Tab
46b85 6c 65 20 2a 70 2c 20 69 6e 74 20 69 49 6e 64 65  le *p, int iInde
46b86 78 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 29 7b 0d  x, int *pnMax){.
46b87 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
46b88 2a 70 53 74 6d 74 3b 0d 0a 20 20 69 6e 74 20 72  *pStmt;..  int r
46b89 63 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 49  c;..  assert( iI
46b8a 6e 64 65 78 3e 3d 30 20 26 26 20 69 49 6e 64 65  ndex>=0 && iInde
46b8b 78 3c 70 2d 3e 6e 49 6e 64 65 78 20 29 3b 0d 0a  x<p->nIndex );..
46b8c 0d 0a 20 20 2f 2a 20 53 65 74 20 70 53 74 6d 74  ..  /* Set pStmt
46b8d 20 74 6f 20 74 68 65 20 63 6f 6d 70 69 6c 65 64   to the compiled
46b8e 20 76 65 72 73 69 6f 6e 20 6f 66 3a 0d 0a 20 20   version of:..  
46b8f 2a 2a 0d 0a 20 20 2a 2a 20 20 20 53 45 4c 45 43  **..  **   SELEC
46b90 54 20 6d 61 78 28 6c 65 76 65 6c 29 20 46 52 4f  T max(level) FRO
46b91 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  M %Q.'%q_segdir'
46b92 20 57 48 45 52 45 20 6c 65 76 65 6c 20 42 45 54   WHERE level BET
46b93 57 45 45 4e 20 3f 20 41 4e 44 20 3f 0d 0a 20 20  WEEN ? AND ?..  
46b94 2a 2a 0d 0a 20 20 2a 2a 20 28 31 30 32 34 20 69  **..  ** (1024 i
46b95 73 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 76  s actually the v
46b96 61 6c 75 65 20 6f 66 20 6d 61 63 72 6f 20 46 54  alue of macro FT
46b97 53 33 5f 53 45 47 44 49 52 5f 50 52 45 46 49 58  S3_SEGDIR_PREFIX
46b98 4c 45 56 45 4c 5f 53 54 52 29 2e 0d 0a 20 20 2a  LEVEL_STR)...  *
46b99 2f 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71  /..  rc = fts3Sq
46b9a 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c  lStmt(p, SQL_SEL
46b9b 45 43 54 5f 53 45 47 44 49 52 5f 4d 41 58 5f 4c  ECT_SEGDIR_MAX_L
46b9c 45 56 45 4c 2c 20 26 70 53 74 6d 74 2c 20 30 29  EVEL, &pStmt, 0)
46b9d 3b 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
46b9e 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
46b9f 72 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62  rc;..  sqlite3_b
46ba0 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31  ind_int(pStmt, 1
46ba1 2c 20 69 49 6e 64 65 78 2a 46 54 53 33 5f 53 45  , iIndex*FTS3_SE
46ba2 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 29 3b 0d  GDIR_MAXLEVEL);.
46ba3 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
46ba4 69 6e 74 28 70 53 74 6d 74 2c 20 32 2c 20 28 69  int(pStmt, 2, (i
46ba5 49 6e 64 65 78 2b 31 29 2a 46 54 53 33 5f 53 45  Index+1)*FTS3_SE
46ba6 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20 2d 20  GDIR_MAXLEVEL - 
46ba7 31 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49 54  1);..  if( SQLIT
46ba8 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
46ba9 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0d 0a 20  tep(pStmt) ){.. 
46baa 20 20 20 2a 70 6e 4d 61 78 20 3d 20 73 71 6c 69     *pnMax = sqli
46bab 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70  te3_column_int(p
46bac 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 7d 0d 0a  Stmt, 0);..  }..
46bad 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
46bae 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0d 0a  _reset(pStmt);..
46baf 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
46bb0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
46bb1 64 20 61 66 74 65 72 20 6d 65 72 67 69 6e 67 20  d after merging 
46bb2 6d 75 6c 74 69 70 6c 65 20 73 65 67 6d 65 6e 74  multiple segment
46bb3 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  s into a single 
46bb4 6c 61 72 67 65 0d 0a 2a 2a 20 73 65 67 6d 65 6e  large..** segmen
46bb5 74 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  t to delete the 
46bb6 6f 6c 64 2c 20 6e 6f 77 20 72 65 64 75 6e 64 61  old, now redunda
46bb7 6e 74 2c 20 73 65 67 6d 65 6e 74 20 62 2d 74 72  nt, segment b-tr
46bb8 65 65 73 2e 20 53 70 65 63 69 66 69 63 61 6c 6c  ees. Specificall
46bb9 79 2c 0d 0a 2a 2a 20 69 74 3a 0d 0a 2a 2a 20 0d  y,..** it:..** .
46bba 0a 2a 2a 20 20 20 31 29 20 44 65 6c 65 74 65 73  .**   1) Deletes
46bbb 20 61 6c 6c 20 25 5f 73 65 67 6d 65 6e 74 73 20   all %_segments 
46bbc 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20  entries for the 
46bbd 73 65 67 6d 65 6e 74 73 20 61 73 73 6f 63 69 61  segments associa
46bbe 74 65 64 20 77 69 74 68 20 0d 0a 2a 2a 20 20 20  ted with ..**   
46bbf 20 20 20 65 61 63 68 20 6f 66 20 74 68 65 20 53     each of the S
46bc0 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73  egReader objects
46bc1 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 70 61   in the array pa
46bc2 73 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72  ssed as the thir
46bc3 64 20 0d 0a 2a 2a 20 20 20 20 20 20 61 72 67 75  d ..**      argu
46bc4 6d 65 6e 74 2c 20 61 6e 64 0d 0a 2a 2a 0d 0a 2a  ment, and..**..*
46bc5 2a 20 20 20 32 29 20 64 65 6c 65 74 65 73 20 61  *   2) deletes a
46bc6 6c 6c 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72  ll %_segdir entr
46bc7 69 65 73 20 77 69 74 68 20 6c 65 76 65 6c 20 69  ies with level i
46bc8 4c 65 76 65 6c 2c 20 6f 72 20 61 6c 6c 20 25 5f  Level, or all %_
46bc9 73 65 67 64 69 72 0d 0a 2a 2a 20 20 20 20 20 20  segdir..**      
46bca 65 6e 74 72 69 65 73 20 72 65 67 61 72 64 6c 65  entries regardle
46bcb 73 73 20 6f 66 20 6c 65 76 65 6c 20 69 66 20 28  ss of level if (
46bcc 69 4c 65 76 65 6c 3c 30 29 2e 0d 0a 2a 2a 0d 0a  iLevel<0)...**..
46bcd 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
46bce 72 65 74 75 72 6e 65 64 20 69 66 20 73 75 63 63  returned if succ
46bcf 65 73 73 66 75 6c 2c 20 6f 74 68 65 72 77 69 73  essful, otherwis
46bd0 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  e an SQLite erro
46bd1 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  r code...*/..sta
46bd2 74 69 63 20 69 6e 74 20 66 74 73 33 44 65 6c 65  tic int fts3Dele
46bd3 74 65 53 65 67 64 69 72 28 0d 0a 20 20 46 74 73  teSegdir(..  Fts
46bd4 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
46bd5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46bd6 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
46bd7 6e 64 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ndle */..  int i
46bd8 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  Index,          
46bd9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
46bda 64 65 78 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65  dex for p->aInde
46bdb 78 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4c 65 76  x */..  int iLev
46bdc 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  el,             
46bdd 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c          /* Level
46bde 20 6f 66 20 25 5f 73 65 67 64 69 72 20 65 6e 74   of %_segdir ent
46bdf 72 69 65 73 20 74 6f 20 64 65 6c 65 74 65 20 2a  ries to delete *
46be0 2f 0d 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  /..  Fts3SegRead
46be1 65 72 20 2a 2a 61 70 53 65 67 6d 65 6e 74 2c 20  er **apSegment, 
46be2 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
46be3 20 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63   SegReader objec
46be4 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 52 65  ts */..  int nRe
46be5 61 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20  ader            
46be6 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
46be7 20 6f 66 20 61 72 72 61 79 20 61 70 53 65 67 6d   of array apSegm
46be8 65 6e 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e  ent */..){..  in
46be9 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
46bea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46beb 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d   Return Code */.
46bec 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
46bed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46bee 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
46bef 61 72 69 61 62 6c 65 20 2a 2f 0d 0a 20 20 73 71  ariable */..  sq
46bf0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
46bf1 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ete;          /*
46bf2 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
46bf3 6f 20 64 65 6c 65 74 65 20 72 6f 77 73 20 2a 2f  o delete rows */
46bf4 0d 0a 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 53  ....  rc = fts3S
46bf5 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44 45  qlStmt(p, SQL_DE
46bf6 4c 45 54 45 5f 53 45 47 4d 45 4e 54 53 5f 52 41  LETE_SEGMENTS_RA
46bf7 4e 47 45 2c 20 26 70 44 65 6c 65 74 65 2c 20 30  NGE, &pDelete, 0
46bf8 29 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72  );..  for(i=0; r
46bf9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
46bfa 69 3c 6e 52 65 61 64 65 72 3b 20 69 2b 2b 29 7b  i<nReader; i++){
46bfb 0d 0a 20 20 20 20 46 74 73 33 53 65 67 52 65 61  ..    Fts3SegRea
46bfc 64 65 72 20 2a 70 53 65 67 6d 65 6e 74 20 3d 20  der *pSegment = 
46bfd 61 70 53 65 67 6d 65 6e 74 5b 69 5d 3b 0d 0a 20  apSegment[i];.. 
46bfe 20 20 20 69 66 28 20 70 53 65 67 6d 65 6e 74 2d     if( pSegment-
46bff 3e 69 53 74 61 72 74 42 6c 6f 63 6b 20 29 7b 0d  >iStartBlock ){.
46c00 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
46c01 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74  ind_int64(pDelet
46c02 65 2c 20 31 2c 20 70 53 65 67 6d 65 6e 74 2d 3e  e, 1, pSegment->
46c03 69 53 74 61 72 74 42 6c 6f 63 6b 29 3b 0d 0a 20  iStartBlock);.. 
46c04 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
46c05 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74 65 2c  d_int64(pDelete,
46c06 20 32 2c 20 70 53 65 67 6d 65 6e 74 2d 3e 69 45   2, pSegment->iE
46c07 6e 64 42 6c 6f 63 6b 29 3b 0d 0a 20 20 20 20 20  ndBlock);..     
46c08 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44   sqlite3_step(pD
46c09 65 6c 65 74 65 29 3b 0d 0a 20 20 20 20 20 20 72  elete);..      r
46c0a 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
46c0b 74 28 70 44 65 6c 65 74 65 29 3b 0d 0a 20 20 20  t(pDelete);..   
46c0c 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20 72   }..  }..  if( r
46c0d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
46c0e 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  .    return rc;.
46c0f 0a 20 20 7d 0d 0a 0d 0a 20 20 61 73 73 65 72 74  .  }....  assert
46c10 28 20 69 4c 65 76 65 6c 3e 3d 30 20 7c 7c 20 69  ( iLevel>=0 || i
46c11 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43  Level==FTS3_SEGC
46c12 55 52 53 4f 52 5f 41 4c 4c 20 29 3b 0d 0a 20 20  URSOR_ALL );..  
46c13 69 66 28 20 69 4c 65 76 65 6c 3d 3d 46 54 53 33  if( iLevel==FTS3
46c14 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c 20 29  _SEGCURSOR_ALL )
46c15 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  {..    rc = fts3
46c16 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44  SqlStmt(p, SQL_D
46c17 45 4c 45 54 45 5f 53 45 47 44 49 52 5f 52 41 4e  ELETE_SEGDIR_RAN
46c18 47 45 2c 20 26 70 44 65 6c 65 74 65 2c 20 30 29  GE, &pDelete, 0)
46c19 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
46c1a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
46c1b 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
46c1c 69 6e 74 28 70 44 65 6c 65 74 65 2c 20 31 2c 20  int(pDelete, 1, 
46c1d 69 49 6e 64 65 78 2a 46 54 53 33 5f 53 45 47 44  iIndex*FTS3_SEGD
46c1e 49 52 5f 4d 41 58 4c 45 56 45 4c 29 3b 0d 0a 20  IR_MAXLEVEL);.. 
46c1f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
46c20 64 5f 69 6e 74 28 70 44 65 6c 65 74 65 2c 20 32  d_int(pDelete, 2
46c21 2c 20 28 69 49 6e 64 65 78 2b 31 29 20 2a 20 46  , (iIndex+1) * F
46c22 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45  TS3_SEGDIR_MAXLE
46c23 56 45 4c 20 2d 20 31 29 3b 0d 0a 20 20 20 20 7d  VEL - 1);..    }
46c24 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
46c25 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
46c26 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53  (p, SQL_DELETE_S
46c27 45 47 44 49 52 5f 4c 45 56 45 4c 2c 20 26 70 44  EGDIR_LEVEL, &pD
46c28 65 6c 65 74 65 2c 20 30 29 3b 0d 0a 20 20 20 20  elete, 0);..    
46c29 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
46c2a 4b 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69  K ){..      sqli
46c2b 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 44 65  te3_bind_int(pDe
46c2c 6c 65 74 65 2c 20 31 2c 20 69 49 6e 64 65 78 2a  lete, 1, iIndex*
46c2d 46 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c  FTS3_SEGDIR_MAXL
46c2e 45 56 45 4c 20 2b 20 69 4c 65 76 65 6c 29 3b 0d  EVEL + iLevel);.
46c2f 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
46c30 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
46c31 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  OK ){..    sqlit
46c32 65 33 5f 73 74 65 70 28 70 44 65 6c 65 74 65 29  e3_step(pDelete)
46c33 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
46c34 74 65 33 5f 72 65 73 65 74 28 70 44 65 6c 65 74  te3_reset(pDelet
46c35 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65  e);..  }....  re
46c36 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
46c37 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  *..** When this 
46c38 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
46c39 65 64 2c 20 62 75 66 66 65 72 20 2a 70 70 4c 69  ed, buffer *ppLi
46c3a 73 74 20 28 73 69 7a 65 20 2a 70 6e 4c 69 73 74  st (size *pnList
46c3b 20 62 79 74 65 73 29 20 63 6f 6e 74 61 69 6e 73   bytes) contains
46c3c 20 0d 0a 2a 2a 20 61 20 70 6f 73 69 74 69 6f 6e   ..** a position
46c3d 20 6c 69 73 74 20 74 68 61 74 20 6d 61 79 20 28   list that may (
46c3e 6f 72 20 6d 61 79 20 6e 6f 74 29 20 66 65 61 74  or may not) feat
46c3f 75 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6c  ure multiple col
46c40 75 6d 6e 73 2e 20 54 68 69 73 0d 0a 2a 2a 20 66  umns. This..** f
46c41 75 6e 63 74 69 6f 6e 20 61 64 6a 75 73 74 73 20  unction adjusts 
46c42 74 68 65 20 70 6f 69 6e 74 65 72 20 2a 70 70 4c  the pointer *ppL
46c43 69 73 74 20 61 6e 64 20 74 68 65 20 6c 65 6e 67  ist and the leng
46c44 74 68 20 2a 70 6e 4c 69 73 74 20 73 6f 20 74 68  th *pnList so th
46c45 61 74 20 74 68 65 79 0d 0a 2a 2a 20 69 64 65 6e  at they..** iden
46c46 74 69 66 79 20 74 68 65 20 73 75 62 73 65 74 20  tify the subset 
46c47 6f 66 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20  of the position 
46c48 6c 69 73 74 20 74 68 61 74 20 63 6f 72 72 65 73  list that corres
46c49 70 6f 6e 64 73 20 74 6f 20 63 6f 6c 75 6d 6e 20  ponds to column 
46c4a 69 43 6f 6c 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66  iCol...**..** If
46c4b 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65 6e   there are no en
46c4c 74 72 69 65 73 20 69 6e 20 74 68 65 20 69 6e 70  tries in the inp
46c4d 75 74 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  ut position list
46c4e 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c   for column iCol
46c4f 2c 20 74 68 65 6e 0d 0a 2a 2a 20 2a 70 6e 4c 69  , then..** *pnLi
46c50 73 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  st is set to zer
46c51 6f 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  o before returni
46c52 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ng...*/..static 
46c53 76 6f 69 64 20 66 74 73 33 43 6f 6c 75 6d 6e 46  void fts3ColumnF
46c54 69 6c 74 65 72 28 0d 0a 20 20 69 6e 74 20 69 43  ilter(..  int iC
46c55 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
46c56 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
46c57 75 6d 6e 20 74 6f 20 66 69 6c 74 65 72 20 6f 6e  umn to filter on
46c58 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 2a 70 70   */..  char **pp
46c59 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  List,           
46c5a 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
46c5b 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 6f 73  : Pointer to pos
46c5c 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0d 0a 20  ition list */.. 
46c5d 20 69 6e 74 20 2a 70 6e 4c 69 73 74 20 20 20 20   int *pnList    
46c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46c5f 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 69 7a 65   /* IN/OUT: Size
46c60 20 6f 66 20 62 75 66 66 65 72 20 2a 70 70 4c 69   of buffer *ppLi
46c61 73 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a  st in bytes */..
46c62 29 7b 0d 0a 20 20 63 68 61 72 20 2a 70 4c 69 73  ){..  char *pLis
46c63 74 20 3d 20 2a 70 70 4c 69 73 74 3b 0d 0a 20 20  t = *ppList;..  
46c64 69 6e 74 20 6e 4c 69 73 74 20 3d 20 2a 70 6e 4c  int nList = *pnL
46c65 69 73 74 3b 0d 0a 20 20 63 68 61 72 20 2a 70 45  ist;..  char *pE
46c66 6e 64 20 3d 20 26 70 4c 69 73 74 5b 6e 4c 69 73  nd = &pList[nLis
46c67 74 5d 3b 0d 0a 20 20 69 6e 74 20 69 43 75 72 72  t];..  int iCurr
46c68 65 6e 74 20 3d 20 30 3b 0d 0a 20 20 63 68 61 72  ent = 0;..  char
46c69 20 2a 70 20 3d 20 70 4c 69 73 74 3b 0d 0a 0d 0a   *p = pList;....
46c6a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d    assert( iCol>=
46c6b 30 20 29 3b 0d 0a 20 20 77 68 69 6c 65 28 20 31  0 );..  while( 1
46c6c 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 63 20   ){..    char c 
46c6d 3d 20 30 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  = 0;..    while(
46c6e 20 70 3c 70 45 6e 64 20 26 26 20 28 63 20 7c 20   p<pEnd && (c | 
46c6f 2a 70 29 26 30 78 46 45 20 29 20 63 20 3d 20 2a  *p)&0xFE ) c = *
46c70 70 2b 2b 20 26 20 30 78 38 30 3b 0d 0a 20 20 0d  p++ & 0x80;..  .
46c71 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 69  .    if( iCol==i
46c72 43 75 72 72 65 6e 74 20 29 7b 0d 0a 20 20 20 20  Current ){..    
46c73 20 20 6e 4c 69 73 74 20 3d 20 28 69 6e 74 29 28    nList = (int)(
46c74 70 20 2d 20 70 4c 69 73 74 29 3b 0d 0a 20 20 20  p - pList);..   
46c75 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d     break;..    }
46c76 0d 0a 0d 0a 20 20 20 20 6e 4c 69 73 74 20 2d 3d  ....    nList -=
46c77 20 28 69 6e 74 29 28 70 20 2d 20 70 4c 69 73 74   (int)(p - pList
46c78 29 3b 0d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  );..    pList = 
46c79 70 3b 0d 0a 20 20 20 20 69 66 28 20 6e 4c 69 73  p;..    if( nLis
46c7a 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 62  t==0 ){..      b
46c7b 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  reak;..    }..  
46c7c 20 20 70 20 3d 20 26 70 4c 69 73 74 5b 31 5d 3b    p = &pList[1];
46c7d 0d 0a 20 20 20 20 70 20 2b 3d 20 73 71 6c 69 74  ..    p += sqlit
46c7e 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33  e3Fts3GetVarint3
46c7f 32 28 70 2c 20 26 69 43 75 72 72 65 6e 74 29 3b  2(p, &iCurrent);
46c80 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 70 4c 69  ..  }....  *ppLi
46c81 73 74 20 3d 20 70 4c 69 73 74 3b 0d 0a 20 20 2a  st = pList;..  *
46c82 70 6e 4c 69 73 74 20 3d 20 6e 4c 69 73 74 3b 0d  pnList = nList;.
46c83 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 61 63  .}..../*..** Cac
46c84 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 46  he data in the F
46c85 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65  ts3MultiSegReade
46c86 72 2e 61 42 75 66 66 65 72 5b 5d 20 62 75 66 66  r.aBuffer[] buff
46c87 65 72 20 28 6f 76 65 72 77 72 69 74 69 6e 67 20  er (overwriting 
46c88 61 6e 79 0d 0a 2a 2a 20 65 78 69 73 74 69 6e 67  any..** existing
46c89 20 64 61 74 61 29 2e 20 47 72 6f 77 20 74 68 65   data). Grow the
46c8a 20 62 75 66 66 65 72 20 69 66 20 72 65 71 75 69   buffer if requi
46c8b 72 65 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  red...**..** If 
46c8c 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
46c8d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74  rn SQLITE_OK. Ot
46c8e 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 4f  herwise, if an O
46c8f 4f 4d 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  OM error is enco
46c90 75 6e 74 65 72 65 64 0d 0a 2a 2a 20 74 72 79 69  untered..** tryi
46c91 6e 67 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65  ng to resize the
46c92 20 62 75 66 66 65 72 2c 20 72 65 74 75 72 6e 20   buffer, return 
46c93 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0d 0a 2a  SQLITE_NOMEM...*
46c94 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  /..static int ft
46c95 73 33 4d 73 72 42 75 66 66 65 72 44 61 74 61 28  s3MsrBufferData(
46c96 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  ..  Fts3MultiSeg
46c97 52 65 61 64 65 72 20 2a 70 4d 73 72 2c 20 20 20  Reader *pMsr,   
46c98 20 20 20 20 2f 2a 20 4d 75 6c 74 69 2d 73 65 67      /* Multi-seg
46c99 6d 65 6e 74 2d 72 65 61 64 65 72 20 68 61 6e 64  ment-reader hand
46c9a 6c 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 70  le */..  char *p
46c9b 4c 69 73 74 2c 0d 0a 20 20 69 6e 74 20 6e 4c 69  List,..  int nLi
46c9c 73 74 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 6e 4c  st..){..  if( nL
46c9d 69 73 74 3e 70 4d 73 72 2d 3e 6e 42 75 66 66 65  ist>pMsr->nBuffe
46c9e 72 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a  r ){..    char *
46c9f 70 4e 65 77 3b 0d 0a 20 20 20 20 70 4d 73 72 2d  pNew;..    pMsr-
46ca0 3e 6e 42 75 66 66 65 72 20 3d 20 6e 4c 69 73 74  >nBuffer = nList
46ca1 2a 32 3b 0d 0a 20 20 20 20 70 4e 65 77 20 3d 20  *2;..    pNew = 
46ca2 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
46ca3 72 65 61 6c 6c 6f 63 28 70 4d 73 72 2d 3e 61 42  realloc(pMsr->aB
46ca4 75 66 66 65 72 2c 20 70 4d 73 72 2d 3e 6e 42 75  uffer, pMsr->nBu
46ca5 66 66 65 72 29 3b 0d 0a 20 20 20 20 69 66 28 20  ffer);..    if( 
46ca6 21 70 4e 65 77 20 29 20 72 65 74 75 72 6e 20 53  !pNew ) return S
46ca7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20  QLITE_NOMEM;..  
46ca8 20 20 70 4d 73 72 2d 3e 61 42 75 66 66 65 72 20    pMsr->aBuffer 
46ca9 3d 20 70 4e 65 77 3b 0d 0a 20 20 7d 0d 0a 0d 0a  = pNew;..  }....
46caa 20 20 6d 65 6d 63 70 79 28 70 4d 73 72 2d 3e 61    memcpy(pMsr->a
46cab 42 75 66 66 65 72 2c 20 70 4c 69 73 74 2c 20 6e  Buffer, pList, n
46cac 4c 69 73 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e  List);..  return
46cad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
46cae 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
46caf 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
46cb0 4d 73 72 49 6e 63 72 4e 65 78 74 28 0d 0a 20 20  MsrIncrNext(..  
46cb1 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
46cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46cb3 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
46cb4 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20 46 74   handle */..  Ft
46cb5 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
46cb6 20 2a 70 4d 73 72 2c 20 20 20 20 20 20 20 2f 2a   *pMsr,       /*
46cb7 20 4d 75 6c 74 69 2d 73 65 67 6d 65 6e 74 2d 72   Multi-segment-r
46cb8 65 61 64 65 72 20 68 61 6e 64 6c 65 20 2a 2f 0d  eader handle */.
46cb9 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
46cba 20 2a 70 69 44 6f 63 69 64 2c 20 20 20 20 20 20   *piDocid,      
46cbb 20 20 20 2f 2a 20 4f 55 54 3a 20 44 6f 63 69 64     /* OUT: Docid
46cbc 20 76 61 6c 75 65 20 2a 2f 0d 0a 20 20 63 68 61   value */..  cha
46cbd 72 20 2a 2a 70 61 50 6f 73 6c 69 73 74 2c 20 20  r **paPoslist,  
46cbe 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46cbf 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
46cc0 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f  position list */
46cc1 0d 0a 20 20 69 6e 74 20 2a 70 6e 50 6f 73 6c 69  ..  int *pnPosli
46cc2 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
46cc3 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65      /* OUT: Size
46cc4 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
46cc5 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 29  t in bytes */..)
46cc6 7b 0d 0a 20 20 69 6e 74 20 6e 4d 65 72 67 65 20  {..  int nMerge 
46cc7 3d 20 70 4d 73 72 2d 3e 6e 41 64 76 61 6e 63 65  = pMsr->nAdvance
46cc8 3b 0d 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  ;..  Fts3SegRead
46cc9 65 72 20 2a 2a 61 70 53 65 67 6d 65 6e 74 20 3d  er **apSegment =
46cca 20 70 4d 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74   pMsr->apSegment
46ccb 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29  ;..  int (*xCmp)
46ccc 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a  (Fts3SegReader *
46ccd 2c 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  , Fts3SegReader 
46cce 2a 29 20 3d 20 28 0d 0a 20 20 20 20 70 2d 3e 62  *) = (..    p->b
46ccf 44 65 73 63 49 64 78 20 3f 20 66 74 73 33 53 65  DescIdx ? fts3Se
46cd0 67 52 65 61 64 65 72 44 6f 63 6c 69 73 74 43 6d  gReaderDoclistCm
46cd1 70 52 65 76 20 3a 20 66 74 73 33 53 65 67 52 65  pRev : fts3SegRe
46cd2 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 0d 0a  aderDoclistCmp..
46cd3 20 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 6e 4d    );....  if( nM
46cd4 65 72 67 65 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  erge==0 ){..    
46cd5 2a 70 61 50 6f 73 6c 69 73 74 20 3d 20 30 3b 0d  *paPoslist = 0;.
46cd6 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
46cd7 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  TE_OK;..  }.... 
46cd8 20 77 68 69 6c 65 28 20 31 20 29 7b 0d 0a 20 20   while( 1 ){..  
46cd9 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
46cda 2a 70 53 65 67 3b 0d 0a 20 20 20 20 70 53 65 67  *pSeg;..    pSeg
46cdb 20 3d 20 70 4d 73 72 2d 3e 61 70 53 65 67 6d 65   = pMsr->apSegme
46cdc 6e 74 5b 30 5d 3b 0d 0a 0d 0a 20 20 20 20 69 66  nt[0];....    if
46cdd 28 20 70 53 65 67 2d 3e 70 4f 66 66 73 65 74 4c  ( pSeg->pOffsetL
46cde 69 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20  ist==0 ){..     
46cdf 20 2a 70 61 50 6f 73 6c 69 73 74 20 3d 20 30 3b   *paPoslist = 0;
46ce0 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a  ..      break;..
46ce1 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
46ce2 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 20 20 20    int rc;..     
46ce3 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 0d 0a 20   char *pList;.. 
46ce4 20 20 20 20 20 69 6e 74 20 6e 4c 69 73 74 3b 0d       int nList;.
46ce5 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0d 0a 20  .      int j;.. 
46ce6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
46ce7 36 34 20 69 44 6f 63 69 64 20 3d 20 61 70 53 65  64 iDocid = apSe
46ce8 67 6d 65 6e 74 5b 30 5d 2d 3e 69 44 6f 63 69 64  gment[0]->iDocid
46ce9 3b 0d 0a 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  ;....      rc = 
46cea 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 78  fts3SegReaderNex
46ceb 74 44 6f 63 69 64 28 70 2c 20 61 70 53 65 67 6d  tDocid(p, apSegm
46cec 65 6e 74 5b 30 5d 2c 20 26 70 4c 69 73 74 2c 20  ent[0], &pList, 
46ced 26 6e 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20  &nList);..      
46cee 6a 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 77 68  j = 1;..      wh
46cef 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
46cf0 4f 4b 20 0d 0a 20 20 20 20 20 20 20 20 26 26 20  OK ..        && 
46cf1 6a 3c 6e 4d 65 72 67 65 0d 0a 20 20 20 20 20 20  j<nMerge..      
46cf2 20 20 26 26 20 61 70 53 65 67 6d 65 6e 74 5b 6a    && apSegment[j
46cf3 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 0d 0a  ]->pOffsetList..
46cf4 20 20 20 20 20 20 20 20 26 26 20 61 70 53 65 67          && apSeg
46cf5 6d 65 6e 74 5b 6a 5d 2d 3e 69 44 6f 63 69 64 3d  ment[j]->iDocid=
46cf6 3d 69 44 6f 63 69 64 0d 0a 20 20 20 20 20 20 29  =iDocid..      )
46cf7 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  {..        rc = 
46cf8 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 78  fts3SegReaderNex
46cf9 74 44 6f 63 69 64 28 70 2c 20 61 70 53 65 67 6d  tDocid(p, apSegm
46cfa 65 6e 74 5b 6a 5d 2c 20 30 2c 20 30 29 3b 0d 0a  ent[j], 0, 0);..
46cfb 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0d 0a 20 20          j++;..  
46cfc 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
46cfd 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
46cfe 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
46cff 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
46d00 53 6f 72 74 28 70 4d 73 72 2d 3e 61 70 53 65 67  Sort(pMsr->apSeg
46d01 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 6a 2c  ment, nMerge, j,
46d02 20 78 43 6d 70 29 3b 0d 0a 0d 0a 20 20 20 20 20   xCmp);....     
46d03 20 69 66 28 20 70 4d 73 72 2d 3e 69 43 6f 6c 46   if( pMsr->iColF
46d04 69 6c 74 65 72 3e 3d 30 20 29 7b 0d 0a 20 20 20  ilter>=0 ){..   
46d05 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 46       fts3ColumnF
46d06 69 6c 74 65 72 28 70 4d 73 72 2d 3e 69 43 6f 6c  ilter(pMsr->iCol
46d07 46 69 6c 74 65 72 2c 20 26 70 4c 69 73 74 2c 20  Filter, &pList, 
46d08 26 6e 4c 69 73 74 29 3b 0d 0a 20 20 20 20 20 20  &nList);..      
46d09 7d 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28 20 6e  }....      if( n
46d0a 4c 69 73 74 3e 30 20 29 7b 0d 0a 20 20 20 20 20  List>0 ){..     
46d0b 20 20 20 69 66 28 20 66 74 73 33 53 65 67 52 65     if( fts3SegRe
46d0c 61 64 65 72 49 73 50 65 6e 64 69 6e 67 28 61 70  aderIsPending(ap
46d0d 53 65 67 6d 65 6e 74 5b 30 5d 29 20 29 7b 0d 0a  Segment[0]) ){..
46d0e 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66            rc = f
46d0f 74 73 33 4d 73 72 42 75 66 66 65 72 44 61 74 61  ts3MsrBufferData
46d10 28 70 4d 73 72 2c 20 70 4c 69 73 74 2c 20 6e 4c  (pMsr, pList, nL
46d11 69 73 74 2b 31 29 3b 0d 0a 20 20 20 20 20 20 20  ist+1);..       
46d12 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
46d13 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
46d14 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 70 61  ;..          *pa
46d15 50 6f 73 6c 69 73 74 20 3d 20 70 4d 73 72 2d 3e  Poslist = pMsr->
46d16 61 42 75 66 66 65 72 3b 0d 0a 20 20 20 20 20 20  aBuffer;..      
46d17 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 73      assert( (pMs
46d18 72 2d 3e 61 42 75 66 66 65 72 5b 6e 4c 69 73 74  r->aBuffer[nList
46d19 5d 20 26 20 30 78 46 45 29 3d 3d 30 78 30 30 20  ] & 0xFE)==0x00 
46d1a 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  );..        }els
46d1b 65 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 70  e{..          *p
46d1c 61 50 6f 73 6c 69 73 74 20 3d 20 70 4c 69 73 74  aPoslist = pList
46d1d 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
46d1e 20 20 20 20 20 20 2a 70 69 44 6f 63 69 64 20 3d        *piDocid =
46d1f 20 69 44 6f 63 69 64 3b 0d 0a 20 20 20 20 20 20   iDocid;..      
46d20 20 20 2a 70 6e 50 6f 73 6c 69 73 74 20 3d 20 6e    *pnPoslist = n
46d21 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20 62  List;..        b
46d22 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  reak;..      }..
46d23 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
46d24 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
46d25 3b 0d 0a 7d 0d 0a 0d 0a 73 74 61 74 69 63 20 69  ;..}....static i
46d26 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65 72  nt fts3SegReader
46d27 53 74 61 72 74 28 0d 0a 20 20 46 74 73 33 54 61  Start(..  Fts3Ta
46d28 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
46d29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72            /* Vir
46d2a 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c  tual table handl
46d2b 65 20 2a 2f 0d 0a 20 20 46 74 73 33 4d 75 6c 74  e */..  Fts3Mult
46d2c 69 53 65 67 52 65 61 64 65 72 20 2a 70 43 73 72  iSegReader *pCsr
46d2d 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  ,       /* Curso
46d2e 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 63  r object */..  c
46d2f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
46d30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
46d31 2a 20 54 65 72 6d 20 73 65 61 72 63 68 65 64 20  * Term searched 
46d32 66 6f 72 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f  for (or NULL) */
46d33 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 20 20 20  ..  int nTerm   
46d34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46d35 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
46d36 20 7a 54 65 72 6d 20 69 6e 20 62 79 74 65 73 20   zTerm in bytes 
46d37 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b  */..){..  int i;
46d38 0d 0a 20 20 69 6e 74 20 6e 53 65 67 20 3d 20 70  ..  int nSeg = p
46d39 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 0d 0a  Csr->nSegment;..
46d3a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 74  ..  /* If the Ft
46d3b 73 33 53 65 67 46 69 6c 74 65 72 20 64 65 66 69  s3SegFilter defi
46d3c 6e 65 73 20 61 20 73 70 65 63 69 66 69 63 20 74  nes a specific t
46d3d 65 72 6d 20 28 6f 72 20 74 65 72 6d 20 70 72 65  erm (or term pre
46d3e 66 69 78 29 20 74 6f 20 73 65 61 72 63 68 20 0d  fix) to search .
46d3f 0a 20 20 2a 2a 20 66 6f 72 2c 20 74 68 65 6e 20  .  ** for, then 
46d40 61 64 76 61 6e 63 65 20 65 61 63 68 20 73 65 67  advance each seg
46d41 6d 65 6e 74 20 69 74 65 72 61 74 6f 72 20 75 6e  ment iterator un
46d42 74 69 6c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  til it points to
46d43 20 61 20 74 65 72 6d 20 6f 66 0d 0a 20 20 2a 2a   a term of..  **
46d44 20 65 71 75 61 6c 20 6f 72 20 67 72 65 61 74 65   equal or greate
46d45 72 20 76 61 6c 75 65 20 74 68 61 6e 20 74 68 65  r value than the
46d46 20 73 70 65 63 69 66 69 65 64 20 74 65 72 6d 2e   specified term.
46d47 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 6d   This prevents m
46d48 61 6e 79 0d 0a 20 20 2a 2a 20 75 6e 6e 65 63 65  any..  ** unnece
46d49 73 73 61 72 79 20 6d 65 72 67 65 2f 73 6f 72 74  ssary merge/sort
46d4a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20   operations for 
46d4b 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73  the case where s
46d4c 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 0d 0a 20  ingle segment.. 
46d4d 20 2a 2a 20 62 2d 74 72 65 65 20 6c 65 61 66 20   ** b-tree leaf 
46d4e 6e 6f 64 65 73 20 63 6f 6e 74 61 69 6e 20 6d 6f  nodes contain mo
46d4f 72 65 20 74 68 61 6e 20 6f 6e 65 20 74 65 72 6d  re than one term
46d50 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 66 6f 72 28 69  ...  */..  for(i
46d51 3d 30 3b 20 70 43 73 72 2d 3e 62 52 65 73 74 61  =0; pCsr->bResta
46d52 72 74 3d 3d 30 20 26 26 20 69 3c 70 43 73 72 2d  rt==0 && i<pCsr-
46d53 3e 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b 29 7b  >nSegment; i++){
46d54 0d 0a 20 20 20 20 46 74 73 33 53 65 67 52 65 61  ..    Fts3SegRea
46d55 64 65 72 20 2a 70 53 65 67 20 3d 20 70 43 73 72  der *pSeg = pCsr
46d56 2d 3e 61 70 53 65 67 6d 65 6e 74 5b 69 5d 3b 0d  ->apSegment[i];.
46d57 0a 20 20 20 20 64 6f 20 7b 0d 0a 20 20 20 20 20  .    do {..     
46d58 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33 53 65   int rc = fts3Se
46d59 67 52 65 61 64 65 72 4e 65 78 74 28 70 2c 20 70  gReaderNext(p, p
46d5a 53 65 67 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  Seg, 0);..      
46d5b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
46d5c 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  K ) return rc;..
46d5d 20 20 20 20 7d 77 68 69 6c 65 28 20 7a 54 65 72      }while( zTer
46d5e 6d 20 26 26 20 66 74 73 33 53 65 67 52 65 61 64  m && fts3SegRead
46d5f 65 72 54 65 72 6d 43 6d 70 28 70 53 65 67 2c 20  erTermCmp(pSeg, 
46d60 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3c 30 20  zTerm, nTerm)<0 
46d61 29 3b 0d 0a 20 20 7d 0d 0a 20 20 66 74 73 33 53  );..  }..  fts3S
46d62 65 67 52 65 61 64 65 72 53 6f 72 74 28 70 43 73  egReaderSort(pCs
46d63 72 2d 3e 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53  r->apSegment, nS
46d64 65 67 2c 20 6e 53 65 67 2c 20 66 74 73 33 53 65  eg, nSeg, fts3Se
46d65 67 52 65 61 64 65 72 43 6d 70 29 3b 0d 0a 0d 0a  gReaderCmp);....
46d66 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
46d67 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45  OK;..}....SQLITE
46d68 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
46d69 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
46d6a 72 53 74 61 72 74 28 0d 0a 20 20 46 74 73 33 54  rStart(..  Fts3T
46d6b 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
46d6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
46d6d 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
46d6e 6c 65 20 2a 2f 0d 0a 20 20 46 74 73 33 4d 75 6c  le */..  Fts3Mul
46d6f 74 69 53 65 67 52 65 61 64 65 72 20 2a 70 43 73  tiSegReader *pCs
46d70 72 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  r,       /* Curs
46d71 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20  or object */..  
46d72 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 2a 70  Fts3SegFilter *p
46d73 46 69 6c 74 65 72 20 20 20 20 20 20 20 20 20 20  Filter          
46d74 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 73 20  /* Restrictions 
46d75 6f 6e 20 72 61 6e 67 65 20 6f 66 20 69 74 65 72  on range of iter
46d76 61 74 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ation */..){..  
46d77 70 43 73 72 2d 3e 70 46 69 6c 74 65 72 20 3d 20  pCsr->pFilter = 
46d78 70 46 69 6c 74 65 72 3b 0d 0a 20 20 72 65 74 75  pFilter;..  retu
46d79 72 6e 20 66 74 73 33 53 65 67 52 65 61 64 65 72  rn fts3SegReader
46d7a 53 74 61 72 74 28 70 2c 20 70 43 73 72 2c 20 70  Start(p, pCsr, p
46d7b 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70  Filter->zTerm, p
46d7c 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29 3b 0d  Filter->nTerm);.
46d7d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49  .}....SQLITE_PRI
46d7e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
46d7f 46 74 73 33 4d 73 72 49 6e 63 72 53 74 61 72 74  Fts3MsrIncrStart
46d80 28 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  (..  Fts3Table *
46d81 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
46d82 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
46d83 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  table handle */.
46d84 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52  .  Fts3MultiSegR
46d85 65 61 64 65 72 20 2a 70 43 73 72 2c 20 20 20 20  eader *pCsr,    
46d86 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a     /* Cursor obj
46d87 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43  ect */..  int iC
46d88 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
46d89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
46d8a 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20 6f 6e 2e  umn to match on.
46d8b 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
46d8c 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20  r *zTerm,       
46d8d 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74         /* Term t
46d8e 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
46d8f 68 20 61 20 64 6f 63 6c 69 73 74 20 66 6f 72 20  h a doclist for 
46d90 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 65 72 6d 20  */..  int nTerm 
46d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46d92 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
46d93 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 54 65 72  of bytes in zTer
46d94 6d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  m */..){..  int 
46d95 69 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20  i;..  int rc;.. 
46d96 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 20 3d 20   int nSegment = 
46d97 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 0d  pCsr->nSegment;.
46d98 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 46  .  int (*xCmp)(F
46d99 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2c 20  ts3SegReader *, 
46d9a 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 29  Fts3SegReader *)
46d9b 20 3d 20 28 0d 0a 20 20 20 20 70 2d 3e 62 44 65   = (..    p->bDe
46d9c 73 63 49 64 78 20 3f 20 66 74 73 33 53 65 67 52  scIdx ? fts3SegR
46d9d 65 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 52  eaderDoclistCmpR
46d9e 65 76 20 3a 20 66 74 73 33 53 65 67 52 65 61 64  ev : fts3SegRead
46d9f 65 72 44 6f 63 6c 69 73 74 43 6d 70 0d 0a 20 20  erDoclistCmp..  
46da0 29 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  );....  assert( 
46da1 70 43 73 72 2d 3e 70 46 69 6c 74 65 72 3d 3d 30  pCsr->pFilter==0
46da2 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 7a   );..  assert( z
46da3 54 65 72 6d 20 26 26 20 6e 54 65 72 6d 3e 30 20  Term && nTerm>0 
46da4 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 64 76 61 6e  );....  /* Advan
46da5 63 65 20 65 61 63 68 20 73 65 67 6d 65 6e 74 20  ce each segment 
46da6 69 74 65 72 61 74 6f 72 20 75 6e 74 69 6c 20 69  iterator until i
46da7 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
46da8 74 65 72 6d 20 7a 54 65 72 6d 2f 6e 54 65 72 6d  term zTerm/nTerm
46da9 2e 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 66 74 73  . */..  rc = fts
46daa 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28  3SegReaderStart(
46dab 70 2c 20 70 43 73 72 2c 20 7a 54 65 72 6d 2c 20  p, pCsr, zTerm, 
46dac 6e 54 65 72 6d 29 3b 0d 0a 20 20 69 66 28 20 72  nTerm);..  if( r
46dad 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
46dae 65 74 75 72 6e 20 72 63 3b 0d 0a 0d 0a 20 20 2f  eturn rc;....  /
46daf 2a 20 44 65 74 65 72 6d 69 6e 65 20 68 6f 77 20  * Determine how 
46db0 6d 61 6e 79 20 6f 66 20 74 68 65 20 73 65 67 6d  many of the segm
46db1 65 6e 74 73 20 61 63 74 75 61 6c 6c 79 20 70 6f  ents actually po
46db2 69 6e 74 20 74 6f 20 7a 54 65 72 6d 2f 6e 54 65  int to zTerm/nTe
46db3 72 6d 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d  rm. */..  for(i=
46db4 30 3b 20 69 3c 6e 53 65 67 6d 65 6e 74 3b 20 69  0; i<nSegment; i
46db5 2b 2b 29 7b 0d 0a 20 20 20 20 46 74 73 33 53 65  ++){..    Fts3Se
46db6 67 52 65 61 64 65 72 20 2a 70 53 65 67 20 3d 20  gReader *pSeg = 
46db7 70 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 5b  pCsr->apSegment[
46db8 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 21 70 53  i];..    if( !pS
46db9 65 67 2d 3e 61 4e 6f 64 65 20 7c 7c 20 66 74 73  eg->aNode || fts
46dba 33 53 65 67 52 65 61 64 65 72 54 65 72 6d 43 6d  3SegReaderTermCm
46dbb 70 28 70 53 65 67 2c 20 7a 54 65 72 6d 2c 20 6e  p(pSeg, zTerm, n
46dbc 54 65 72 6d 29 20 29 7b 0d 0a 20 20 20 20 20 20  Term) ){..      
46dbd 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
46dbe 20 7d 0d 0a 20 20 70 43 73 72 2d 3e 6e 41 64 76   }..  pCsr->nAdv
46dbf 61 6e 63 65 20 3d 20 69 3b 0d 0a 0d 0a 20 20 2f  ance = i;....  /
46dc0 2a 20 41 64 76 61 6e 63 65 20 65 61 63 68 20 6f  * Advance each o
46dc1 66 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 74  f the segments t
46dc2 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
46dc3 69 72 73 74 20 64 6f 63 69 64 2e 20 2a 2f 0d 0a  irst docid. */..
46dc4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 73    for(i=0; i<pCs
46dc5 72 2d 3e 6e 41 64 76 61 6e 63 65 3b 20 69 2b 2b  r->nAdvance; i++
46dc6 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ){..    rc = fts
46dc7 33 53 65 67 52 65 61 64 65 72 46 69 72 73 74 44  3SegReaderFirstD
46dc8 6f 63 69 64 28 70 2c 20 70 43 73 72 2d 3e 61 70  ocid(p, pCsr->ap
46dc9 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0d 0a 20 20  Segment[i]);..  
46dca 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
46dcb 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
46dcc 0d 0a 20 20 7d 0d 0a 20 20 66 74 73 33 53 65 67  ..  }..  fts3Seg
46dcd 52 65 61 64 65 72 53 6f 72 74 28 70 43 73 72 2d  ReaderSort(pCsr-
46dce 3e 61 70 53 65 67 6d 65 6e 74 2c 20 69 2c 20 69  >apSegment, i, i
46dcf 2c 20 78 43 6d 70 29 3b 0d 0a 0d 0a 20 20 61 73  , xCmp);....  as
46dd0 73 65 72 74 28 20 69 43 6f 6c 3c 30 20 7c 7c 20  sert( iCol<0 || 
46dd1 69 43 6f 6c 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20  iCol<p->nColumn 
46dd2 29 3b 0d 0a 20 20 70 43 73 72 2d 3e 69 43 6f 6c  );..  pCsr->iCol
46dd3 46 69 6c 74 65 72 20 3d 20 69 43 6f 6c 3b 0d 0a  Filter = iCol;..
46dd4 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
46dd5 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
46dd6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
46dd7 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20   is called on a 
46dd8 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 74  MultiSegReader t
46dd9 68 61 74 20 68 61 73 20 62 65 65 6e 20 73 74 61  hat has been sta
46dda 72 74 65 64 20 75 73 69 6e 67 0d 0a 2a 2a 20 73  rted using..** s
46ddb 71 6c 69 74 65 33 46 74 73 33 4d 73 72 49 6e 63  qlite3Fts3MsrInc
46ddc 72 53 74 61 72 74 28 29 2e 20 4f 6e 65 20 6f 72  rStart(). One or
46ddd 20 6d 6f 72 65 20 63 61 6c 6c 73 20 74 6f 20 4d   more calls to M
46dde 73 72 49 6e 63 72 4e 65 78 74 28 29 20 6d 61 79  srIncrNext() may
46ddf 20 61 6c 73 6f 0d 0a 2a 2a 20 68 61 76 65 20 62   also..** have b
46de0 65 65 6e 20 6d 61 64 65 2e 20 43 61 6c 6c 69 6e  een made. Callin
46de1 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  g this function 
46de2 70 75 74 73 20 74 68 65 20 4d 75 6c 74 69 53 65  puts the MultiSe
46de3 67 52 65 61 64 65 72 20 69 6e 20 73 75 63 68 0d  gReader in such.
46de4 0a 2a 2a 20 61 20 73 74 61 74 65 20 74 68 61 74  .** a state that
46de5 20 69 66 20 74 68 65 20 6e 65 78 74 20 74 77 6f   if the next two
46de6 20 63 61 6c 6c 73 20 61 72 65 3a 0d 0a 2a 2a 0d   calls are:..**.
46de7 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 74 73  .**   sqlite3Fts
46de8 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28  3SegReaderStart(
46de9 29 0d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46  )..**   sqlite3F
46dea 74 73 33 53 65 67 52 65 61 64 65 72 53 74 65 70  ts3SegReaderStep
46deb 28 29 0d 0a 2a 2a 0d 0a 2a 2a 20 74 68 65 6e 20  ()..**..** then 
46dec 74 68 65 20 65 6e 74 69 72 65 20 64 6f 63 6c 69  the entire docli
46ded 73 74 20 66 6f 72 20 74 68 65 20 74 65 72 6d 20  st for the term 
46dee 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  is available in 
46def 0d 0a 2a 2a 20 4d 75 6c 74 69 53 65 67 52 65 61  ..** MultiSegRea
46df0 64 65 72 2e 61 44 6f 63 6c 69 73 74 2f 6e 44 6f  der.aDoclist/nDo
46df1 63 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  clist...*/..SQLI
46df2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
46df3 71 6c 69 74 65 33 46 74 73 33 4d 73 72 49 6e 63  qlite3Fts3MsrInc
46df4 72 52 65 73 74 61 72 74 28 46 74 73 33 4d 75 6c  rRestart(Fts3Mul
46df5 74 69 53 65 67 52 65 61 64 65 72 20 2a 70 43 73  tiSegReader *pCs
46df6 72 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20  r){..  int i;   
46df7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46df8 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
46df9 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
46dfa 68 20 73 65 67 6d 65 6e 74 2d 72 65 61 64 65 72  h segment-reader
46dfb 73 20 2a 2f 0d 0a 0d 0a 20 20 61 73 73 65 72 74  s */....  assert
46dfc 28 20 70 43 73 72 2d 3e 7a 54 65 72 6d 3d 3d 30  ( pCsr->zTerm==0
46dfd 20 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
46dfe 43 73 72 2d 3e 6e 54 65 72 6d 3d 3d 30 20 29 3b  Csr->nTerm==0 );
46dff 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72  ..  assert( pCsr
46e00 2d 3e 61 44 6f 63 6c 69 73 74 3d 3d 30 20 29 3b  ->aDoclist==0 );
46e01 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72  ..  assert( pCsr
46e02 2d 3e 6e 44 6f 63 6c 69 73 74 3d 3d 30 20 29 3b  ->nDoclist==0 );
46e03 0d 0a 0d 0a 20 20 70 43 73 72 2d 3e 6e 41 64 76  ....  pCsr->nAdv
46e04 61 6e 63 65 20 3d 20 30 3b 0d 0a 20 20 70 43 73  ance = 0;..  pCs
46e05 72 2d 3e 62 52 65 73 74 61 72 74 20 3d 20 31 3b  r->bRestart = 1;
46e06 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
46e07 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 20 69  Csr->nSegment; i
46e08 2b 2b 29 7b 0d 0a 20 20 20 20 70 43 73 72 2d 3e  ++){..    pCsr->
46e09 61 70 53 65 67 6d 65 6e 74 5b 69 5d 2d 3e 70 4f  apSegment[i]->pO
46e0a 66 66 73 65 74 4c 69 73 74 20 3d 20 30 3b 0d 0a  ffsetList = 0;..
46e0b 20 20 20 20 70 43 73 72 2d 3e 61 70 53 65 67 6d      pCsr->apSegm
46e0c 65 6e 74 5b 69 5d 2d 3e 6e 4f 66 66 73 65 74 4c  ent[i]->nOffsetL
46e0d 69 73 74 20 3d 20 30 3b 0d 0a 20 20 20 20 70 43  ist = 0;..    pC
46e0e 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 5b 69 5d  sr->apSegment[i]
46e0f 2d 3e 69 44 6f 63 69 64 20 3d 20 30 3b 0d 0a 20  ->iDocid = 0;.. 
46e10 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53   }....  return S
46e11 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a  QLITE_OK;..}....
46e12 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
46e13 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
46e14 53 65 67 52 65 61 64 65 72 53 74 65 70 28 0d 0a  SegReaderStep(..
46e15 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
46e16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46e17 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
46e18 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0d 0a 20 20  le handle */..  
46e19 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64  Fts3MultiSegRead
46e1a 65 72 20 2a 70 43 73 72 20 20 20 20 20 20 20 20  er *pCsr        
46e1b 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a 65 63 74  /* Cursor object
46e1c 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
46e1d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
46e1e 0d 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65  ..  int isIgnore
46e1f 45 6d 70 74 79 20 3d 20 20 28 70 43 73 72 2d 3e  Empty =  (pCsr->
46e20 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20 26  pFilter->flags &
46e21 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 49 47   FTS3_SEGMENT_IG
46e22 4e 4f 52 45 5f 45 4d 50 54 59 29 3b 0d 0a 20 20  NORE_EMPTY);..  
46e23 69 6e 74 20 69 73 52 65 71 75 69 72 65 50 6f 73  int isRequirePos
46e24 20 3d 20 20 20 28 70 43 73 72 2d 3e 70 46 69 6c   =   (pCsr->pFil
46e25 74 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54 53  ter->flags & FTS
46e26 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52  3_SEGMENT_REQUIR
46e27 45 5f 50 4f 53 29 3b 0d 0a 20 20 69 6e 74 20 69  E_POS);..  int i
46e28 73 43 6f 6c 46 69 6c 74 65 72 20 3d 20 20 20 20  sColFilter =    
46e29 28 70 43 73 72 2d 3e 70 46 69 6c 74 65 72 2d 3e  (pCsr->pFilter->
46e2a 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45 47  flags & FTS3_SEG
46e2b 4d 45 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54  MENT_COLUMN_FILT
46e2c 45 52 29 3b 0d 0a 20 20 69 6e 74 20 69 73 50 72  ER);..  int isPr
46e2d 65 66 69 78 20 3d 20 20 20 20 20 20 20 28 70 43  efix =       (pC
46e2e 73 72 2d 3e 70 46 69 6c 74 65 72 2d 3e 66 6c 61  sr->pFilter->fla
46e2f 67 73 20 26 20 46 54 53 33 5f 53 45 47 4d 45 4e  gs & FTS3_SEGMEN
46e30 54 5f 50 52 45 46 49 58 29 3b 0d 0a 20 20 69 6e  T_PREFIX);..  in
46e31 74 20 69 73 53 63 61 6e 20 3d 20 20 20 20 20 20  t isScan =      
46e32 20 20 20 28 70 43 73 72 2d 3e 70 46 69 6c 74 65     (pCsr->pFilte
46e33 72 2d 3e 66 6c 61 67 73 20 26 20 46 54 53 33 5f  r->flags & FTS3_
46e34 53 45 47 4d 45 4e 54 5f 53 43 41 4e 29 3b 0d 0a  SEGMENT_SCAN);..
46e35 20 20 69 6e 74 20 69 73 46 69 72 73 74 20 3d 20    int isFirst = 
46e36 20 20 20 20 20 20 20 28 70 43 73 72 2d 3e 70 46         (pCsr->pF
46e37 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20 26 20 46  ilter->flags & F
46e38 54 53 33 5f 53 45 47 4d 45 4e 54 5f 46 49 52 53  TS3_SEGMENT_FIRS
46e39 54 29 3b 0d 0a 0d 0a 20 20 46 74 73 33 53 65 67  T);....  Fts3Seg
46e3a 52 65 61 64 65 72 20 2a 2a 61 70 53 65 67 6d 65  Reader **apSegme
46e3b 6e 74 20 3d 20 70 43 73 72 2d 3e 61 70 53 65 67  nt = pCsr->apSeg
46e3c 6d 65 6e 74 3b 0d 0a 20 20 69 6e 74 20 6e 53 65  ment;..  int nSe
46e3d 67 6d 65 6e 74 20 3d 20 70 43 73 72 2d 3e 6e 53  gment = pCsr->nS
46e3e 65 67 6d 65 6e 74 3b 0d 0a 20 20 46 74 73 33 53  egment;..  Fts3S
46e3f 65 67 46 69 6c 74 65 72 20 2a 70 46 69 6c 74 65  egFilter *pFilte
46e40 72 20 3d 20 70 43 73 72 2d 3e 70 46 69 6c 74 65  r = pCsr->pFilte
46e41 72 3b 0d 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70  r;..  int (*xCmp
46e42 29 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20  )(Fts3SegReader 
46e43 2a 2c 20 46 74 73 33 53 65 67 52 65 61 64 65 72  *, Fts3SegReader
46e44 20 2a 29 20 3d 20 28 0d 0a 20 20 20 20 70 2d 3e   *) = (..    p->
46e45 62 44 65 73 63 49 64 78 20 3f 20 66 74 73 33 53  bDescIdx ? fts3S
46e46 65 67 52 65 61 64 65 72 44 6f 63 6c 69 73 74 43  egReaderDoclistC
46e47 6d 70 52 65 76 20 3a 20 66 74 73 33 53 65 67 52  mpRev : fts3SegR
46e48 65 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 0d  eaderDoclistCmp.
46e49 0a 20 20 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 70  .  );....  if( p
46e4a 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3d 3d 30  Csr->nSegment==0
46e4b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
46e4c 5f 4f 4b 3b 0d 0a 0d 0a 20 20 64 6f 20 7b 0d 0a  _OK;....  do {..
46e4d 20 20 20 20 69 6e 74 20 6e 4d 65 72 67 65 3b 0d      int nMerge;.
46e4e 0a 20 20 20 20 69 6e 74 20 69 3b 0d 0a 20 20 0d  .    int i;..  .
46e4f 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20  .    /* Advance 
46e50 74 68 65 20 66 69 72 73 74 20 70 43 73 72 2d 3e  the first pCsr->
46e51 6e 41 64 76 61 6e 63 65 20 65 6e 74 72 69 65 73  nAdvance entries
46e52 20 69 6e 20 74 68 65 20 61 70 53 65 67 6d 65 6e   in the apSegmen
46e53 74 5b 5d 20 61 72 72 61 79 0d 0a 20 20 20 20 2a  t[] array..    *
46e54 2a 20 66 6f 72 77 61 72 64 2e 20 54 68 65 6e 20  * forward. Then 
46e55 73 6f 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e  sort the list in
46e56 20 6f 72 64 65 72 20 6f 66 20 63 75 72 72 65 6e   order of curren
46e57 74 20 74 65 72 6d 20 61 67 61 69 6e 2e 20 20 0d  t term again.  .
46e58 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 66 6f 72  .    */..    for
46e59 28 69 3d 30 3b 20 69 3c 70 43 73 72 2d 3e 6e 41  (i=0; i<pCsr->nA
46e5a 64 76 61 6e 63 65 3b 20 69 2b 2b 29 7b 0d 0a 20  dvance; i++){.. 
46e5b 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65       rc = fts3Se
46e5c 67 52 65 61 64 65 72 4e 65 78 74 28 70 2c 20 61  gReaderNext(p, a
46e5d 70 53 65 67 6d 65 6e 74 5b 69 5d 2c 20 30 29 3b  pSegment[i], 0);
46e5e 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
46e5f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
46e60 72 6e 20 72 63 3b 0d 0a 20 20 20 20 7d 0d 0a 20  rn rc;..    }.. 
46e61 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
46e62 53 6f 72 74 28 61 70 53 65 67 6d 65 6e 74 2c 20  Sort(apSegment, 
46e63 6e 53 65 67 6d 65 6e 74 2c 20 70 43 73 72 2d 3e  nSegment, pCsr->
46e64 6e 41 64 76 61 6e 63 65 2c 20 66 74 73 33 53 65  nAdvance, fts3Se
46e65 67 52 65 61 64 65 72 43 6d 70 29 3b 0d 0a 20 20  gReaderCmp);..  
46e66 20 20 70 43 73 72 2d 3e 6e 41 64 76 61 6e 63 65    pCsr->nAdvance
46e67 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20   = 0;....    /* 
46e68 49 66 20 61 6c 6c 20 74 68 65 20 73 65 67 2d 72  If all the seg-r
46e69 65 61 64 65 72 73 20 61 72 65 20 61 74 20 45 4f  eaders are at EO
46e6a 46 2c 20 77 65 27 72 65 20 66 69 6e 69 73 68 65  F, we're finishe
46e6b 64 2e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d. return SQLITE
46e6c 5f 4f 4b 2e 20 2a 2f 0d 0a 20 20 20 20 61 73 73  _OK. */..    ass
46e6d 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
46e6e 4f 4b 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 61  OK );..    if( a
46e6f 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 61 4e 6f  pSegment[0]->aNo
46e70 64 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0d 0a  de==0 ) break;..
46e71 0d 0a 20 20 20 20 70 43 73 72 2d 3e 6e 54 65 72  ..    pCsr->nTer
46e72 6d 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  m = apSegment[0]
46e73 2d 3e 6e 54 65 72 6d 3b 0d 0a 20 20 20 20 70 43  ->nTerm;..    pC
46e74 73 72 2d 3e 7a 54 65 72 6d 20 3d 20 61 70 53 65  sr->zTerm = apSe
46e75 67 6d 65 6e 74 5b 30 5d 2d 3e 7a 54 65 72 6d 3b  gment[0]->zTerm;
46e76 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ....    /* If th
46e77 69 73 20 69 73 20 61 20 70 72 65 66 69 78 2d 73  is is a prefix-s
46e78 65 61 72 63 68 2c 20 61 6e 64 20 69 66 20 74 68  earch, and if th
46e79 65 20 74 65 72 6d 20 74 68 61 74 20 61 70 53 65  e term that apSe
46e7a 67 6d 65 6e 74 5b 30 5d 20 70 6f 69 6e 74 73 0d  gment[0] points.
46e7b 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 65 73 20  .    ** to does 
46e7c 6e 6f 74 20 73 68 61 72 65 20 61 20 73 75 66 66  not share a suff
46e7d 69 78 20 77 69 74 68 20 70 46 69 6c 74 65 72 2d  ix with pFilter-
46e7e 3e 7a 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68  >zTerm/nTerm, th
46e7f 65 6e 20 61 6c 6c 20 0d 0a 20 20 20 20 2a 2a 20  en all ..    ** 
46e80 72 65 71 75 69 72 65 64 20 63 61 6c 6c 62 61 63  required callbac
46e81 6b 73 20 68 61 76 65 20 62 65 65 6e 20 6d 61 64  ks have been mad
46e82 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
46e83 65 78 69 74 20 65 61 72 6c 79 2e 0d 0a 20 20 20  exit early...   
46e84 20 2a 2a 0d 0a 20 20 20 20 2a 2a 20 53 69 6d 69   **..    ** Simi
46e85 6c 61 72 6c 79 2c 20 69 66 20 74 68 69 73 20 69  larly, if this i
46e86 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61  s a search for a
46e87 6e 20 65 78 61 63 74 20 6d 61 74 63 68 2c 20 61  n exact match, a
46e88 6e 64 20 74 68 65 20 66 69 72 73 74 20 74 65 72  nd the first ter
46e89 6d 0d 0a 20 20 20 20 2a 2a 20 6f 66 20 73 65 67  m..    ** of seg
46e8a 6d 65 6e 74 20 61 70 53 65 67 6d 65 6e 74 5b 30  ment apSegment[0
46e8b 5d 20 69 73 20 6e 6f 74 20 61 20 6d 61 74 63 68  ] is not a match
46e8c 2c 20 65 78 69 74 20 65 61 72 6c 79 2e 0d 0a 20  , exit early... 
46e8d 20 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 70     */..    if( p
46e8e 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 20 26 26  Filter->zTerm &&
46e8f 20 21 69 73 53 63 61 6e 20 29 7b 0d 0a 20 20 20   !isScan ){..   
46e90 20 20 20 69 66 28 20 70 43 73 72 2d 3e 6e 54 65     if( pCsr->nTe
46e91 72 6d 3c 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72  rm<pFilter->nTer
46e92 6d 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20 28 21  m ..       || (!
46e93 69 73 50 72 65 66 69 78 20 26 26 20 70 43 73 72  isPrefix && pCsr
46e94 2d 3e 6e 54 65 72 6d 3e 70 46 69 6c 74 65 72 2d  ->nTerm>pFilter-
46e95 3e 6e 54 65 72 6d 29 0d 0a 20 20 20 20 20 20 20  >nTerm)..       
46e96 7c 7c 20 6d 65 6d 63 6d 70 28 70 43 73 72 2d 3e  || memcmp(pCsr->
46e97 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d 3e  zTerm, pFilter->
46e98 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d 3e  zTerm, pFilter->
46e99 6e 54 65 72 6d 29 20 0d 0a 20 20 20 20 20 20 29  nTerm) ..      )
46e9a 7b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  {..        break
46e9b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
46e9c 7d 0d 0a 0d 0a 20 20 20 20 6e 4d 65 72 67 65 20  }....    nMerge 
46e9d 3d 20 31 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28  = 1;..    while(
46e9e 20 6e 4d 65 72 67 65 3c 6e 53 65 67 6d 65 6e 74   nMerge<nSegment
46e9f 20 0d 0a 20 20 20 20 20 20 20 20 26 26 20 61 70   ..        && ap
46ea0 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d  Segment[nMerge]-
46ea1 3e 61 4e 6f 64 65 0d 0a 20 20 20 20 20 20 20 20  >aNode..        
46ea2 26 26 20 61 70 53 65 67 6d 65 6e 74 5b 6e 4d 65  && apSegment[nMe
46ea3 72 67 65 5d 2d 3e 6e 54 65 72 6d 3d 3d 70 43 73  rge]->nTerm==pCs
46ea4 72 2d 3e 6e 54 65 72 6d 20 0d 0a 20 20 20 20 20  r->nTerm ..     
46ea5 20 20 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28     && 0==memcmp(
46ea6 70 43 73 72 2d 3e 7a 54 65 72 6d 2c 20 61 70 53  pCsr->zTerm, apS
46ea7 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d 3e  egment[nMerge]->
46ea8 7a 54 65 72 6d 2c 20 70 43 73 72 2d 3e 6e 54 65  zTerm, pCsr->nTe
46ea9 72 6d 29 0d 0a 20 20 20 20 29 7b 0d 0a 20 20 20  rm)..    ){..   
46eaa 20 20 20 6e 4d 65 72 67 65 2b 2b 3b 0d 0a 20 20     nMerge++;..  
46eab 20 20 7d 0d 0a 0d 0a 20 20 20 20 61 73 73 65 72    }....    asser
46eac 74 28 20 69 73 49 67 6e 6f 72 65 45 6d 70 74 79  t( isIgnoreEmpty
46ead 20 7c 7c 20 28 69 73 52 65 71 75 69 72 65 50 6f   || (isRequirePo
46eae 73 20 26 26 20 21 69 73 43 6f 6c 46 69 6c 74 65  s && !isColFilte
46eaf 72 29 20 29 3b 0d 0a 20 20 20 20 69 66 28 20 6e  r) );..    if( n
46eb0 4d 65 72 67 65 3d 3d 31 20 0d 0a 20 20 20 20 20  Merge==1 ..     
46eb1 26 26 20 21 69 73 49 67 6e 6f 72 65 45 6d 70 74  && !isIgnoreEmpt
46eb2 79 20 0d 0a 20 20 20 20 20 26 26 20 21 69 73 46  y ..     && !isF
46eb3 69 72 73 74 20 0d 0a 20 20 20 20 20 26 26 20 28  irst ..     && (
46eb4 70 2d 3e 62 44 65 73 63 49 64 78 3d 3d 30 20 7c  p->bDescIdx==0 |
46eb5 7c 20 66 74 73 33 53 65 67 52 65 61 64 65 72 49  | fts3SegReaderI
46eb6 73 50 65 6e 64 69 6e 67 28 61 70 53 65 67 6d 65  sPending(apSegme
46eb7 6e 74 5b 30 5d 29 3d 3d 30 29 0d 0a 20 20 20 20  nt[0])==0)..    
46eb8 29 7b 0d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e  ){..      pCsr->
46eb9 6e 44 6f 63 6c 69 73 74 20 3d 20 61 70 53 65 67  nDoclist = apSeg
46eba 6d 65 6e 74 5b 30 5d 2d 3e 6e 44 6f 63 6c 69 73  ment[0]->nDoclis
46ebb 74 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 66 74  t;..      if( ft
46ebc 73 33 53 65 67 52 65 61 64 65 72 49 73 50 65 6e  s3SegReaderIsPen
46ebd 64 69 6e 67 28 61 70 53 65 67 6d 65 6e 74 5b 30  ding(apSegment[0
46ebe 5d 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  ]) ){..        r
46ebf 63 20 3d 20 66 74 73 33 4d 73 72 42 75 66 66 65  c = fts3MsrBuffe
46ec0 72 44 61 74 61 28 70 43 73 72 2c 20 61 70 53 65  rData(pCsr, apSe
46ec1 67 6d 65 6e 74 5b 30 5d 2d 3e 61 44 6f 63 6c 69  gment[0]->aDocli
46ec2 73 74 2c 20 70 43 73 72 2d 3e 6e 44 6f 63 6c 69  st, pCsr->nDocli
46ec3 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 70 43  st);..        pC
46ec4 73 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 70  sr->aDoclist = p
46ec5 43 73 72 2d 3e 61 42 75 66 66 65 72 3b 0d 0a 20  Csr->aBuffer;.. 
46ec6 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
46ec7 20 20 20 20 20 70 43 73 72 2d 3e 61 44 6f 63 6c       pCsr->aDocl
46ec8 69 73 74 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b  ist = apSegment[
46ec9 30 5d 2d 3e 61 44 6f 63 6c 69 73 74 3b 0d 0a 20  0]->aDoclist;.. 
46eca 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
46ecb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
46ecc 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f  ) rc = SQLITE_RO
46ecd 57 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  W;..    }else{..
46ece 20 20 20 20 20 20 69 6e 74 20 6e 44 6f 63 6c 69        int nDocli
46ecf 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  st = 0;         
46ed0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63    /* Size of doc
46ed1 6c 69 73 74 20 2a 2f 0d 0a 20 20 20 20 20 20 73  list */..      s
46ed2 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72  qlite3_int64 iPr
46ed3 65 76 20 3d 20 30 3b 20 20 20 20 2f 2a 20 50 72  ev = 0;    /* Pr
46ed4 65 76 69 6f 75 73 20 64 6f 63 69 64 20 73 74 6f  evious docid sto
46ed5 72 65 64 20 69 6e 20 64 6f 63 6c 69 73 74 20 2a  red in doclist *
46ed6 2f 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68  /....      /* Th
46ed7 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 6f  e current term o
46ed8 66 20 74 68 65 20 66 69 72 73 74 20 6e 4d 65 72  f the first nMer
46ed9 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ge entries in th
46eda 65 20 61 72 72 61 79 0d 0a 20 20 20 20 20 20 2a  e array..      *
46edb 2a 20 6f 66 20 46 74 73 33 53 65 67 52 65 61 64  * of Fts3SegRead
46edc 65 72 20 6f 62 6a 65 63 74 73 20 69 73 20 74 68  er objects is th
46edd 65 20 73 61 6d 65 2e 20 54 68 65 20 64 6f 63 6c  e same. The docl
46ede 69 73 74 73 20 6d 75 73 74 20 62 65 20 6d 65 72  ists must be mer
46edf 67 65 64 0d 0a 20 20 20 20 20 20 2a 2a 20 61 6e  ged..      ** an
46ee0 64 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  d a single term 
46ee1 72 65 74 75 72 6e 65 64 20 77 69 74 68 20 74 68  returned with th
46ee2 65 20 6d 65 72 67 65 64 20 64 6f 63 6c 69 73 74  e merged doclist
46ee3 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
46ee4 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d     for(i=0; i<nM
46ee5 65 72 67 65 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  erge; i++){..   
46ee6 20 20 20 20 20 66 74 73 33 53 65 67 52 65 61 64       fts3SegRead
46ee7 65 72 46 69 72 73 74 44 6f 63 69 64 28 70 2c 20  erFirstDocid(p, 
46ee8 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0d 0a  apSegment[i]);..
46ee9 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 66        }..      f
46eea 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74  ts3SegReaderSort
46eeb 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 4d 65 72  (apSegment, nMer
46eec 67 65 2c 20 6e 4d 65 72 67 65 2c 20 78 43 6d 70  ge, nMerge, xCmp
46eed 29 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65 28  );..      while(
46eee 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 70   apSegment[0]->p
46eef 4f 66 66 73 65 74 4c 69 73 74 20 29 7b 0d 0a 20  OffsetList ){.. 
46ef0 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 20 20 20         int j;   
46ef1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46ef2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65   /* Number of se
46ef3 67 6d 65 6e 74 73 20 74 68 61 74 20 73 68 61 72  gments that shar
46ef4 65 20 61 20 64 6f 63 69 64 20 2a 2f 0d 0a 20 20  e a docid */..  
46ef5 20 20 20 20 20 20 63 68 61 72 20 2a 70 4c 69 73        char *pLis
46ef6 74 3b 0d 0a 20 20 20 20 20 20 20 20 69 6e 74 20  t;..        int 
46ef7 6e 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20  nList;..        
46ef8 69 6e 74 20 6e 42 79 74 65 3b 0d 0a 20 20 20 20  int nByte;..    
46ef9 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
46efa 34 20 69 44 6f 63 69 64 20 3d 20 61 70 53 65 67  4 iDocid = apSeg
46efb 6d 65 6e 74 5b 30 5d 2d 3e 69 44 6f 63 69 64 3b  ment[0]->iDocid;
46efc 0d 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 65  ..        fts3Se
46efd 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63 69 64  gReaderNextDocid
46efe 28 70 2c 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  (p, apSegment[0]
46eff 2c 20 26 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74  , &pList, &nList
46f00 29 3b 0d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20  );..        j = 
46f01 31 3b 0d 0a 20 20 20 20 20 20 20 20 77 68 69 6c  1;..        whil
46f02 65 28 20 6a 3c 6e 4d 65 72 67 65 0d 0a 20 20 20  e( j<nMerge..   
46f03 20 20 20 20 20 20 20 20 20 26 26 20 61 70 53 65           && apSe
46f04 67 6d 65 6e 74 5b 6a 5d 2d 3e 70 4f 66 66 73 65  gment[j]->pOffse
46f05 74 4c 69 73 74 0d 0a 20 20 20 20 20 20 20 20 20  tList..         
46f06 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e 74 5b     && apSegment[
46f07 6a 5d 2d 3e 69 44 6f 63 69 64 3d 3d 69 44 6f 63  j]->iDocid==iDoc
46f08 69 64 0d 0a 20 20 20 20 20 20 20 20 29 7b 0d 0a  id..        ){..
46f09 20 20 20 20 20 20 20 20 20 20 66 74 73 33 53 65            fts3Se
46f0a 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63 69 64  gReaderNextDocid
46f0b 28 70 2c 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d  (p, apSegment[j]
46f0c 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  , 0, 0);..      
46f0d 20 20 20 20 6a 2b 2b 3b 0d 0a 20 20 20 20 20 20      j++;..      
46f0e 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 20 20 69    }....        i
46f0f 66 28 20 69 73 43 6f 6c 46 69 6c 74 65 72 20 29  f( isColFilter )
46f10 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  {..          fts
46f11 33 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 70 46  3ColumnFilter(pF
46f12 69 6c 74 65 72 2d 3e 69 43 6f 6c 2c 20 26 70 4c  ilter->iCol, &pL
46f13 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0d 0a 20  ist, &nList);.. 
46f14 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20         }....    
46f15 20 20 20 20 69 66 28 20 21 69 73 49 67 6e 6f 72      if( !isIgnor
46f16 65 45 6d 70 74 79 20 7c 7c 20 6e 4c 69 73 74 3e  eEmpty || nList>
46f17 30 20 29 7b 0d 0a 0d 0a 20 20 20 20 20 20 20 20  0 ){....        
46f18 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74    /* Calculate t
46f19 68 65 20 27 64 6f 63 69 64 27 20 64 65 6c 74 61  he 'docid' delta
46f1a 20 76 61 6c 75 65 20 74 6f 20 77 72 69 74 65 20   value to write 
46f1b 69 6e 74 6f 20 74 68 65 20 6d 65 72 67 65 64 20  into the merged 
46f1c 0d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64  ..          ** d
46f1d 6f 63 6c 69 73 74 2e 20 2a 2f 0d 0a 20 20 20 20  oclist. */..    
46f1e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
46f1f 74 36 34 20 69 44 65 6c 74 61 3b 0d 0a 20 20 20  t64 iDelta;..   
46f20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 62 44         if( p->bD
46f21 65 73 63 49 64 78 20 26 26 20 6e 44 6f 63 6c 69  escIdx && nDocli
46f22 73 74 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  st>0 ){..       
46f23 20 20 20 20 20 69 44 65 6c 74 61 20 3d 20 69 50       iDelta = iP
46f24 72 65 76 20 2d 20 69 44 6f 63 69 64 3b 0d 0a 20  rev - iDocid;.. 
46f25 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d           }else{.
46f26 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 44 65  .            iDe
46f27 6c 74 61 20 3d 20 69 44 6f 63 69 64 20 2d 20 69  lta = iDocid - i
46f28 50 72 65 76 3b 0d 0a 20 20 20 20 20 20 20 20 20  Prev;..         
46f29 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 61 73   }..          as
46f2a 73 65 72 74 28 20 69 44 65 6c 74 61 3e 30 20 7c  sert( iDelta>0 |
46f2b 7c 20 28 6e 44 6f 63 6c 69 73 74 3d 3d 30 20 26  | (nDoclist==0 &
46f2c 26 20 69 44 65 6c 74 61 3d 3d 69 44 6f 63 69 64  & iDelta==iDocid
46f2d 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ) );..          
46f2e 61 73 73 65 72 74 28 20 6e 44 6f 63 6c 69 73 74  assert( nDoclist
46f2f 3e 30 20 7c 7c 20 69 44 65 6c 74 61 3d 3d 69 44  >0 || iDelta==iD
46f30 6f 63 69 64 20 29 3b 0d 0a 0d 0a 20 20 20 20 20  ocid );....     
46f31 20 20 20 20 20 6e 42 79 74 65 20 3d 20 73 71 6c       nByte = sql
46f32 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
46f33 6e 28 69 44 65 6c 74 61 29 20 2b 20 28 69 73 52  n(iDelta) + (isR
46f34 65 71 75 69 72 65 50 6f 73 3f 6e 4c 69 73 74 2b  equirePos?nList+
46f35 31 3a 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  1:0);..         
46f36 20 69 66 28 20 6e 44 6f 63 6c 69 73 74 2b 6e 42   if( nDoclist+nB
46f37 79 74 65 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65  yte>pCsr->nBuffe
46f38 72 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  r ){..          
46f39 20 20 63 68 61 72 20 2a 61 4e 65 77 3b 0d 0a 20    char *aNew;.. 
46f3a 20 20 20 20 20 20 20 20 20 20 20 70 43 73 72 2d             pCsr-
46f3b 3e 6e 42 75 66 66 65 72 20 3d 20 28 6e 44 6f 63  >nBuffer = (nDoc
46f3c 6c 69 73 74 2b 6e 42 79 74 65 29 2a 32 3b 0d 0a  list+nByte)*2;..
46f3d 20 20 20 20 20 20 20 20 20 20 20 20 61 4e 65 77              aNew
46f3e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
46f3f 6f 63 28 70 43 73 72 2d 3e 61 42 75 66 66 65 72  oc(pCsr->aBuffer
46f40 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 29  , pCsr->nBuffer)
46f41 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ;..            i
46f42 66 28 20 21 61 4e 65 77 20 29 7b 0d 0a 20 20 20  f( !aNew ){..   
46f43 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
46f44 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
46f45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  .            }..
46f46 20 20 20 20 20 20 20 20 20 20 20 20 70 43 73 72              pCsr
46f47 2d 3e 61 42 75 66 66 65 72 20 3d 20 61 4e 65 77  ->aBuffer = aNew
46f48 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a  ;..          }..
46f49 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
46f4a 69 73 46 69 72 73 74 20 29 7b 0d 0a 20 20 20 20  isFirst ){..    
46f4b 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 20          char *a 
46f4c 3d 20 26 70 43 73 72 2d 3e 61 42 75 66 66 65 72  = &pCsr->aBuffer
46f4d 5b 6e 44 6f 63 6c 69 73 74 5d 3b 0d 0a 20 20 20  [nDoclist];..   
46f4e 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 57 72           int nWr
46f4f 69 74 65 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ite;..          
46f50 20 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e   ..            n
46f51 57 72 69 74 65 20 3d 20 73 71 6c 69 74 65 33 46  Write = sqlite3F
46f52 74 73 33 46 69 72 73 74 46 69 6c 74 65 72 28 69  ts3FirstFilter(i
46f53 44 65 6c 74 61 2c 20 70 4c 69 73 74 2c 20 6e 4c  Delta, pList, nL
46f54 69 73 74 2c 20 61 29 3b 0d 0a 20 20 20 20 20 20  ist, a);..      
46f55 20 20 20 20 20 20 69 66 28 20 6e 57 72 69 74 65        if( nWrite
46f56 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20   ){..           
46f57 20 20 20 69 50 72 65 76 20 3d 20 69 44 6f 63 69     iPrev = iDoci
46f58 64 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  d;..            
46f59 20 20 6e 44 6f 63 6c 69 73 74 20 2b 3d 20 6e 57    nDoclist += nW
46f5a 72 69 74 65 3b 0d 0a 20 20 20 20 20 20 20 20 20  rite;..         
46f5b 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 20 20     }..          
46f5c 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
46f5d 20 20 20 20 6e 44 6f 63 6c 69 73 74 20 2b 3d 20      nDoclist += 
46f5e 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61  sqlite3Fts3PutVa
46f5f 72 69 6e 74 28 26 70 43 73 72 2d 3e 61 42 75 66  rint(&pCsr->aBuf
46f60 66 65 72 5b 6e 44 6f 63 6c 69 73 74 5d 2c 20 69  fer[nDoclist], i
46f61 44 65 6c 74 61 29 3b 0d 0a 20 20 20 20 20 20 20  Delta);..       
46f62 20 20 20 20 20 69 50 72 65 76 20 3d 20 69 44 6f       iPrev = iDo
46f63 63 69 64 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  cid;..          
46f64 20 20 69 66 28 20 69 73 52 65 71 75 69 72 65 50    if( isRequireP
46f65 6f 73 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  os ){..         
46f66 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 73       memcpy(&pCs
46f67 72 2d 3e 61 42 75 66 66 65 72 5b 6e 44 6f 63 6c  r->aBuffer[nDocl
46f68 69 73 74 5d 2c 20 70 4c 69 73 74 2c 20 6e 4c 69  ist], pList, nLi
46f69 73 74 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  st);..          
46f6a 20 20 20 20 6e 44 6f 63 6c 69 73 74 20 2b 3d 20      nDoclist += 
46f6b 6e 4c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20 20  nList;..        
46f6c 20 20 20 20 20 20 70 43 73 72 2d 3e 61 42 75 66        pCsr->aBuf
46f6d 66 65 72 5b 6e 44 6f 63 6c 69 73 74 2b 2b 5d 20  fer[nDoclist++] 
46f6e 3d 20 27 5c 30 27 3b 0d 0a 20 20 20 20 20 20 20  = '\0';..       
46f6f 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20       }..        
46f70 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a    }..        }..
46f71 0d 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 65  ..        fts3Se
46f72 67 52 65 61 64 65 72 53 6f 72 74 28 61 70 53 65  gReaderSort(apSe
46f73 67 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 6a  gment, nMerge, j
46f74 2c 20 78 43 6d 70 29 3b 0d 0a 20 20 20 20 20 20  , xCmp);..      
46f75 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 44 6f  }..      if( nDo
46f76 63 6c 69 73 74 3e 30 20 29 7b 0d 0a 20 20 20 20  clist>0 ){..    
46f77 20 20 20 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69      pCsr->aDocli
46f78 73 74 20 3d 20 70 43 73 72 2d 3e 61 42 75 66 66  st = pCsr->aBuff
46f79 65 72 3b 0d 0a 20 20 20 20 20 20 20 20 70 43 73  er;..        pCs
46f7a 72 2d 3e 6e 44 6f 63 6c 69 73 74 20 3d 20 6e 44  r->nDoclist = nD
46f7b 6f 63 6c 69 73 74 3b 0d 0a 20 20 20 20 20 20 20  oclist;..       
46f7c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57   rc = SQLITE_ROW
46f7d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  ;..      }..    
46f7e 7d 0d 0a 20 20 20 20 70 43 73 72 2d 3e 6e 41 64  }..    pCsr->nAd
46f7f 76 61 6e 63 65 20 3d 20 6e 4d 65 72 67 65 3b 0d  vance = nMerge;.
46f80 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53  .  }while( rc==S
46f81 51 4c 49 54 45 5f 4f 4b 20 29 3b 0d 0a 0d 0a 20  QLITE_OK );.... 
46f82 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
46f83 0d 0a 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ....SQLITE_PRIVA
46f84 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
46f85 74 73 33 53 65 67 52 65 61 64 65 72 46 69 6e 69  ts3SegReaderFini
46f86 73 68 28 0d 0a 20 20 46 74 73 33 4d 75 6c 74 69  sh(..  Fts3Multi
46f87 53 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 20  SegReader *pCsr 
46f88 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
46f89 6f 62 6a 65 63 74 20 2a 2f 0d 0a 29 7b 0d 0a 20  object */..){.. 
46f8a 20 69 66 28 20 70 43 73 72 20 29 7b 0d 0a 20 20   if( pCsr ){..  
46f8b 20 20 69 6e 74 20 69 3b 0d 0a 20 20 20 20 66 6f    int i;..    fo
46f8c 72 28 69 3d 30 3b 20 69 3c 70 43 73 72 2d 3e 6e  r(i=0; i<pCsr->n
46f8d 53 65 67 6d 65 6e 74 3b 20 69 2b 2b 29 7b 0d 0a  Segment; i++){..
46f8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
46f8f 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28 70  3SegReaderFree(p
46f90 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 5b 69  Csr->apSegment[i
46f91 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20  ]);..    }..    
46f92 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73  sqlite3_free(pCs
46f93 72 2d 3e 61 70 53 65 67 6d 65 6e 74 29 3b 0d 0a  r->apSegment);..
46f94 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
46f95 28 70 43 73 72 2d 3e 61 42 75 66 66 65 72 29 3b  (pCsr->aBuffer);
46f96 0d 0a 0d 0a 20 20 20 20 70 43 73 72 2d 3e 6e 53  ....    pCsr->nS
46f97 65 67 6d 65 6e 74 20 3d 20 30 3b 0d 0a 20 20 20  egment = 0;..   
46f98 20 70 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74   pCsr->apSegment
46f99 20 3d 20 30 3b 0d 0a 20 20 20 20 70 43 73 72 2d   = 0;..    pCsr-
46f9a 3e 61 42 75 66 66 65 72 20 3d 20 30 3b 0d 0a 20  >aBuffer = 0;.. 
46f9b 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
46f9c 4d 65 72 67 65 20 61 6c 6c 20 6c 65 76 65 6c 20  Merge all level 
46f9d 69 4c 65 76 65 6c 20 73 65 67 6d 65 6e 74 73 20  iLevel segments 
46f9e 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
46f9f 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 0d 0a  into a single ..
46fa0 2a 2a 20 69 4c 65 76 65 6c 2b 31 20 73 65 67 6d  ** iLevel+1 segm
46fa1 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 4c 65 76  ent. Or, if iLev
46fa2 65 6c 3c 30 2c 20 6d 65 72 67 65 20 61 6c 6c 20  el<0, merge all 
46fa3 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f 20 61 0d  segments into a.
46fa4 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 65 67 6d 65  .** single segme
46fa5 6e 74 20 77 69 74 68 20 61 20 6c 65 76 65 6c 20  nt with a level 
46fa6 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d  equal to the num
46fa7 65 72 69 63 61 6c 6c 79 20 6c 61 72 67 65 73 74  erically largest
46fa8 20 6c 65 76 65 6c 20 0d 0a 2a 2a 20 63 75 72 72   level ..** curr
46fa9 65 6e 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e  ently present in
46faa 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a   the database...
46fab 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 69 73 20 66  **..** If this f
46fac 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
46fad 64 20 77 69 74 68 20 69 4c 65 76 65 6c 3c 30 2c  d with iLevel<0,
46fae 20 62 75 74 20 74 68 65 72 65 20 69 73 20 6f 6e   but there is on
46faf 6c 79 20 6f 6e 65 0d 0a 2a 2a 20 73 65 67 6d 65  ly one..** segme
46fb0 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
46fb1 73 65 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  se, SQLITE_DONE 
46fb2 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  is returned imme
46fb3 64 69 61 74 65 6c 79 2e 20 0d 0a 2a 2a 20 4f 74  diately. ..** Ot
46fb4 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 63 63  herwise, if succ
46fb5 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
46fb6 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
46fb7 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
46fb8 73 2c 20 0d 0a 2a 2a 20 61 6e 20 53 51 4c 69 74  s, ..** an SQLit
46fb9 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
46fba 72 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73  returned...*/..s
46fbb 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
46fbc 67 6d 65 6e 74 4d 65 72 67 65 28 46 74 73 33 54  gmentMerge(Fts3T
46fbd 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 69 49 6e  able *p, int iIn
46fbe 64 65 78 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29  dex, int iLevel)
46fbf 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  {..  int rc;    
46fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fc1 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
46fc2 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 49  ode */..  int iI
46fc3 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dx = 0;         
46fc4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
46fc5 65 78 20 6f 66 20 6e 65 77 20 73 65 67 6d 65 6e  ex of new segmen
46fc6 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 4e 65 77  t */..  int iNew
46fc7 4c 65 76 65 6c 20 3d 20 30 3b 20 20 20 20 20 20  Level = 0;      
46fc8 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c          /* Level
46fc9 2f 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65  /index to create
46fca 20 6e 65 77 20 73 65 67 6d 65 6e 74 20 61 74 20   new segment at 
46fcb 2a 2f 0d 0a 20 20 53 65 67 6d 65 6e 74 57 72 69  */..  SegmentWri
46fcc 74 65 72 20 2a 70 57 72 69 74 65 72 20 3d 20 30  ter *pWriter = 0
46fcd 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f  ;     /* Used to
46fce 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 2c 20   write the new, 
46fcf 6d 65 72 67 65 64 2c 20 73 65 67 6d 65 6e 74 20  merged, segment 
46fd0 2a 2f 0d 0a 20 20 46 74 73 33 53 65 67 46 69 6c  */..  Fts3SegFil
46fd1 74 65 72 20 66 69 6c 74 65 72 3b 20 20 20 20 20  ter filter;     
46fd2 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74        /* Segment
46fd3 20 74 65 72 6d 20 66 69 6c 74 65 72 20 63 6f 6e   term filter con
46fd4 64 69 74 69 6f 6e 20 2a 2f 0d 0a 20 20 46 74 73  dition */..  Fts
46fd5 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20  3MultiSegReader 
46fd6 63 73 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  csr;        /* C
46fd7 75 72 73 6f 72 20 74 6f 20 69 74 65 72 61 74 65  ursor to iterate
46fd8 20 74 68 72 6f 75 67 68 20 6c 65 76 65 6c 28 73   through level(s
46fd9 29 20 2a 2f 0d 0a 20 20 69 6e 74 20 62 49 67 6e  ) */..  int bIgn
46fda 6f 72 65 45 6d 70 74 79 20 3d 20 30 3b 20 20 20  oreEmpty = 0;   
46fdb 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
46fdc 74 6f 20 69 67 6e 6f 72 65 20 65 6d 70 74 79 20  to ignore empty 
46fdd 73 65 67 6d 65 6e 74 73 20 2a 2f 0d 0a 0d 0a 20  segments */.... 
46fde 20 61 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3d   assert( iLevel=
46fdf 3d 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f  =FTS3_SEGCURSOR_
46fe0 41 4c 4c 0d 0a 20 20 20 20 20 20 20 7c 7c 20 69  ALL..       || i
46fe1 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43  Level==FTS3_SEGC
46fe2 55 52 53 4f 52 5f 50 45 4e 44 49 4e 47 0d 0a 20  URSOR_PENDING.. 
46fe3 20 20 20 20 20 20 7c 7c 20 69 4c 65 76 65 6c 3e        || iLevel>
46fe4 3d 30 0d 0a 20 20 29 3b 0d 0a 20 20 61 73 73 65  =0..  );..  asse
46fe5 72 74 28 20 69 4c 65 76 65 6c 3c 46 54 53 33 5f  rt( iLevel<FTS3_
46fe6 53 45 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20  SEGDIR_MAXLEVEL 
46fe7 29 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 69 49  );..  assert( iI
46fe8 6e 64 65 78 3e 3d 30 20 26 26 20 69 49 6e 64 65  ndex>=0 && iInde
46fe9 78 3c 70 2d 3e 6e 49 6e 64 65 78 20 29 3b 0d 0a  x<p->nIndex );..
46fea 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
46feb 46 74 73 33 53 65 67 52 65 61 64 65 72 43 75 72  Fts3SegReaderCur
46fec 73 6f 72 28 70 2c 20 69 49 6e 64 65 78 2c 20 69  sor(p, iIndex, i
46fed 4c 65 76 65 6c 2c 20 30 2c 20 30 2c 20 31 2c 20  Level, 0, 0, 1, 
46fee 30 2c 20 26 63 73 72 29 3b 0d 0a 20 20 69 66 28  0, &csr);..  if(
46fef 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
46ff0 7c 20 63 73 72 2e 6e 53 65 67 6d 65 6e 74 3d 3d  | csr.nSegment==
46ff1 30 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68 65  0 ) goto finishe
46ff2 64 3b 0d 0a 0d 0a 20 20 69 66 28 20 69 4c 65 76  d;....  if( iLev
46ff3 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55 52 53  el==FTS3_SEGCURS
46ff4 4f 52 5f 41 4c 4c 20 29 7b 0d 0a 20 20 20 20 2f  OR_ALL ){..    /
46ff5 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 74  * This call is t
46ff6 6f 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67 6d  o merge all segm
46ff7 65 6e 74 73 20 69 6e 20 74 68 65 20 64 61 74 61  ents in the data
46ff8 62 61 73 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  base to a single
46ff9 0d 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74  ..    ** segment
46ffa 2e 20 54 68 65 20 6c 65 76 65 6c 20 6f 66 20 74  . The level of t
46ffb 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 20 69  he new segment i
46ffc 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74  s equal to the t
46ffd 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0d  he numerically .
46ffe 0a 20 20 20 20 2a 2a 20 67 72 65 61 74 65 73 74  .    ** greatest
46fff 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c 20 63   segment level c
47000 75 72 72 65 6e 74 6c 79 20 70 72 65 73 65 6e 74  urrently present
47001 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
47002 20 66 6f 72 20 74 68 69 73 0d 0a 20 20 20 20 2a   for this..    *
47003 2a 20 69 6e 64 65 78 2e 20 54 68 65 20 69 64 78  * index. The idx
47004 20 6f 66 20 74 68 65 20 6e 65 77 20 73 65 67 6d   of the new segm
47005 65 6e 74 20 69 73 20 61 6c 77 61 79 73 20 30 2e  ent is always 0.
47006 20 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 63 73    */..    if( cs
47007 72 2e 6e 53 65 67 6d 65 6e 74 3d 3d 31 20 29 7b  r.nSegment==1 ){
47008 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ..      rc = SQL
47009 49 54 45 5f 44 4f 4e 45 3b 0d 0a 20 20 20 20 20  ITE_DONE;..     
4700a 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0d   goto finished;.
4700b 0a 20 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
4700c 20 66 74 73 33 53 65 67 6d 65 6e 74 4d 61 78 4c   fts3SegmentMaxL
4700d 65 76 65 6c 28 70 2c 20 69 49 6e 64 65 78 2c 20  evel(p, iIndex, 
4700e 26 69 4e 65 77 4c 65 76 65 6c 29 3b 0d 0a 20 20  &iNewLevel);..  
4700f 20 20 62 49 67 6e 6f 72 65 45 6d 70 74 79 20 3d    bIgnoreEmpty =
47010 20 31 3b 0d 0a 0d 0a 20 20 7d 65 6c 73 65 20 69   1;....  }else i
47011 66 28 20 69 4c 65 76 65 6c 3d 3d 46 54 53 33 5f  f( iLevel==FTS3_
47012 53 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49 4e  SEGCURSOR_PENDIN
47013 47 20 29 7b 0d 0a 20 20 20 20 69 4e 65 77 4c 65  G ){..    iNewLe
47014 76 65 6c 20 3d 20 69 49 6e 64 65 78 20 2a 20 46  vel = iIndex * F
47015 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45  TS3_SEGDIR_MAXLE
47016 56 45 4c 3b 20 0d 0a 20 20 20 20 72 63 20 3d 20  VEL; ..    rc = 
47017 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64  fts3AllocateSegd
47018 69 72 49 64 78 28 70 2c 20 69 49 6e 64 65 78 2c  irIdx(p, iIndex,
47019 20 30 2c 20 26 69 49 64 78 29 3b 0d 0a 20 20 7d   0, &iIdx);..  }
4701a 65 6c 73 65 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  else{..    /* Th
4701b 69 73 20 63 61 6c 6c 20 69 73 20 74 6f 20 6d 65  is call is to me
4701c 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  rge all segments
4701d 20 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c   at level iLevel
4701e 2e 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 0d  . find the next.
4701f 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c  .    ** availabl
47020 65 20 73 65 67 6d 65 6e 74 20 69 6e 64 65 78 20  e segment index 
47021 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 2b  at level iLevel+
47022 31 2e 20 54 68 65 20 63 61 6c 6c 20 74 6f 0d 0a  1. The call to..
47023 20 20 20 20 2a 2a 20 66 74 73 33 41 6c 6c 6f 63      ** fts3Alloc
47024 61 74 65 53 65 67 64 69 72 49 64 78 28 29 20 77  ateSegdirIdx() w
47025 69 6c 6c 20 6d 65 72 67 65 20 74 68 65 20 73 65  ill merge the se
47026 67 6d 65 6e 74 73 20 61 74 20 6c 65 76 65 6c 20  gments at level 
47027 69 4c 65 76 65 6c 2b 31 20 74 6f 20 0d 0a 20 20  iLevel+1 to ..  
47028 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 4c    ** a single iL
47029 65 76 65 6c 2b 32 20 73 65 67 6d 65 6e 74 20 69  evel+2 segment i
4702a 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 2a 2f  f necessary.  */
4702b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 41  ..    rc = fts3A
4702c 6c 6c 6f 63 61 74 65 53 65 67 64 69 72 49 64 78  llocateSegdirIdx
4702d 28 70 2c 20 69 49 6e 64 65 78 2c 20 69 4c 65 76  (p, iIndex, iLev
4702e 65 6c 2b 31 2c 20 26 69 49 64 78 29 3b 0d 0a 20  el+1, &iIdx);.. 
4702f 20 20 20 69 4e 65 77 4c 65 76 65 6c 20 3d 20 69     iNewLevel = i
47030 49 6e 64 65 78 20 2a 20 46 54 53 33 5f 53 45 47  Index * FTS3_SEG
47031 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20 2b 20 69  DIR_MAXLEVEL + i
47032 4c 65 76 65 6c 2b 31 3b 0d 0a 20 20 7d 0d 0a 20  Level+1;..  }.. 
47033 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
47034 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68  OK ) goto finish
47035 65 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 63  ed;..  assert( c
47036 73 72 2e 6e 53 65 67 6d 65 6e 74 3e 30 20 29 3b  sr.nSegment>0 );
47037 0d 0a 20 20 61 73 73 65 72 74 28 20 69 4e 65 77  ..  assert( iNew
47038 4c 65 76 65 6c 3e 3d 28 69 49 6e 64 65 78 2a 46  Level>=(iIndex*F
47039 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45  TS3_SEGDIR_MAXLE
4703a 56 45 4c 29 20 29 3b 0d 0a 20 20 61 73 73 65 72  VEL) );..  asser
4703b 74 28 20 69 4e 65 77 4c 65 76 65 6c 3c 28 28 69  t( iNewLevel<((i
4703c 49 6e 64 65 78 2b 31 29 2a 46 54 53 33 5f 53 45  Index+1)*FTS3_SE
4703d 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 29 20 29  GDIR_MAXLEVEL) )
4703e 3b 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 26 66  ;....  memset(&f
4703f 69 6c 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  ilter, 0, sizeof
47040 28 46 74 73 33 53 65 67 46 69 6c 74 65 72 29 29  (Fts3SegFilter))
47041 3b 0d 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67  ;..  filter.flag
47042 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  s = FTS3_SEGMENT
47043 5f 52 45 51 55 49 52 45 5f 50 4f 53 3b 0d 0a 20  _REQUIRE_POS;.. 
47044 20 66 69 6c 74 65 72 2e 66 6c 61 67 73 20 7c 3d   filter.flags |=
47045 20 28 62 49 67 6e 6f 72 65 45 6d 70 74 79 20 3f   (bIgnoreEmpty ?
47046 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 49 47   FTS3_SEGMENT_IG
47047 4e 4f 52 45 5f 45 4d 50 54 59 20 3a 20 30 29 3b  NORE_EMPTY : 0);
47048 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ....  rc = sqlit
47049 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 53  e3Fts3SegReaderS
4704a 74 61 72 74 28 70 2c 20 26 63 73 72 2c 20 26 66  tart(p, &csr, &f
4704b 69 6c 74 65 72 29 3b 0d 0a 20 20 77 68 69 6c 65  ilter);..  while
4704c 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
4704d 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ){..    rc = sql
4704e 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
4704f 72 53 74 65 70 28 70 2c 20 26 63 73 72 29 3b 0d  rStep(p, &csr);.
47050 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
47051 49 54 45 5f 52 4f 57 20 29 20 62 72 65 61 6b 3b  ITE_ROW ) break;
47052 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53  ..    rc = fts3S
47053 65 67 57 72 69 74 65 72 41 64 64 28 70 2c 20 26  egWriterAdd(p, &
47054 70 57 72 69 74 65 72 2c 20 31 2c 20 0d 0a 20 20  pWriter, 1, ..  
47055 20 20 20 20 20 20 63 73 72 2e 7a 54 65 72 6d 2c        csr.zTerm,
47056 20 63 73 72 2e 6e 54 65 72 6d 2c 20 63 73 72 2e   csr.nTerm, csr.
47057 61 44 6f 63 6c 69 73 74 2c 20 63 73 72 2e 6e 44  aDoclist, csr.nD
47058 6f 63 6c 69 73 74 29 3b 0d 0a 20 20 7d 0d 0a 20  oclist);..  }.. 
47059 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
4705a 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68  OK ) goto finish
4705b 65 64 3b 0d 0a 20 20 61 73 73 65 72 74 28 20 70  ed;..  assert( p
4705c 57 72 69 74 65 72 20 29 3b 0d 0a 0d 0a 20 20 69  Writer );....  i
4705d 66 28 20 69 4c 65 76 65 6c 21 3d 46 54 53 33 5f  f( iLevel!=FTS3_
4705e 53 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49 4e  SEGCURSOR_PENDIN
4705f 47 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66  G ){..    rc = f
47060 74 73 33 44 65 6c 65 74 65 53 65 67 64 69 72 28  ts3DeleteSegdir(
47061 70 2c 20 69 49 6e 64 65 78 2c 20 69 4c 65 76 65  p, iIndex, iLeve
47062 6c 2c 20 63 73 72 2e 61 70 53 65 67 6d 65 6e 74  l, csr.apSegment
47063 2c 20 63 73 72 2e 6e 53 65 67 6d 65 6e 74 29 3b  , csr.nSegment);
47064 0d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
47065 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66  LITE_OK ) goto f
47066 69 6e 69 73 68 65 64 3b 0d 0a 20 20 7d 0d 0a 20  inished;..  }.. 
47067 20 72 63 20 3d 20 66 74 73 33 53 65 67 57 72 69   rc = fts3SegWri
47068 74 65 72 46 6c 75 73 68 28 70 2c 20 70 57 72 69  terFlush(p, pWri
47069 74 65 72 2c 20 69 4e 65 77 4c 65 76 65 6c 2c 20  ter, iNewLevel, 
4706a 69 49 64 78 29 3b 0d 0a 0d 0a 20 66 69 6e 69 73  iIdx);.... finis
4706b 68 65 64 3a 0d 0a 20 20 66 74 73 33 53 65 67 57  hed:..  fts3SegW
4706c 72 69 74 65 72 46 72 65 65 28 70 57 72 69 74 65  riterFree(pWrite
4706d 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 46 74  r);..  sqlite3Ft
4706e 73 33 53 65 67 52 65 61 64 65 72 46 69 6e 69 73  s3SegReaderFinis
4706f 68 28 26 63 73 72 29 3b 0d 0a 20 20 72 65 74 75  h(&csr);..  retu
47070 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f  rn rc;..}....../
47071 2a 20 0d 0a 2a 2a 20 46 6c 75 73 68 20 74 68 65  * ..** Flush the
47072 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 65 6e   contents of pen
47073 64 69 6e 67 54 65 72 6d 73 20 74 6f 20 6c 65 76  dingTerms to lev
47074 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 2e 0d 0a  el 0 segments...
47075 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
47076 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
47077 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c  s3PendingTermsFl
47078 75 73 68 28 46 74 73 33 54 61 62 6c 65 20 2a 70  ush(Fts3Table *p
47079 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){..  int rc = S
4707a 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74  QLITE_OK;..  int
4707b 20 69 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20   i;..  for(i=0; 
4707c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
4707d 20 69 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b   i<p->nIndex; i+
4707e 2b 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74  +){..    rc = ft
4707f 73 33 53 65 67 6d 65 6e 74 4d 65 72 67 65 28 70  s3SegmentMerge(p
47080 2c 20 69 2c 20 46 54 53 33 5f 53 45 47 43 55 52  , i, FTS3_SEGCUR
47081 53 4f 52 5f 50 45 4e 44 49 4e 47 29 3b 0d 0a 20  SOR_PENDING);.. 
47082 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
47083 45 5f 44 4f 4e 45 20 29 20 72 63 20 3d 20 53 51  E_DONE ) rc = SQ
47084 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20  LITE_OK;..  }.. 
47085 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
47086 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 70 29  ingTermsClear(p)
47087 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
47088 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 6e 63  .}..../*..** Enc
47089 6f 64 65 20 4e 20 69 6e 74 65 67 65 72 73 20 61  ode N integers a
4708a 73 20 76 61 72 69 6e 74 73 20 69 6e 74 6f 20 61  s varints into a
4708b 20 62 6c 6f 62 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   blob...*/..stat
4708c 69 63 20 76 6f 69 64 20 66 74 73 33 45 6e 63 6f  ic void fts3Enco
4708d 64 65 49 6e 74 41 72 72 61 79 28 0d 0a 20 20 69  deIntArray(..  i
4708e 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20  nt N,           
4708f 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20    /* The number 
47090 6f 66 20 69 6e 74 65 67 65 72 73 20 74 6f 20 65  of integers to e
47091 6e 63 6f 64 65 20 2a 2f 0d 0a 20 20 75 33 32 20  ncode */..  u32 
47092 2a 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *a,            /
47093 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61  * The integer va
47094 6c 75 65 73 20 2a 2f 0d 0a 20 20 63 68 61 72 20  lues */..  char 
47095 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 2f 2a  *zBuf,        /*
47096 20 57 72 69 74 65 20 74 68 65 20 42 4c 4f 42 20   Write the BLOB 
47097 68 65 72 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  here */..  int *
47098 70 4e 42 75 66 20 20 20 20 20 20 20 20 20 2f 2a  pNBuf         /*
47099 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66   Write number of
4709a 20 62 79 74 65 73 20 69 66 20 7a 42 75 66 5b 5d   bytes if zBuf[]
4709b 20 75 73 65 64 20 68 65 72 65 20 2a 2f 0d 0a 29   used here */..)
4709c 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0d 0a  {..  int i, j;..
4709d 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 4e    for(i=j=0; i<N
4709e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 6a 20 2b  ; i++){..    j +
4709f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
470a0 56 61 72 69 6e 74 28 26 7a 42 75 66 5b 6a 5d 2c  Varint(&zBuf[j],
470a1 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
470a2 61 5b 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 2a  a[i]);..  }..  *
470a3 70 4e 42 75 66 20 3d 20 6a 3b 0d 0a 7d 0d 0a 0d  pNBuf = j;..}...
470a4 0a 2f 2a 0d 0a 2a 2a 20 44 65 63 6f 64 65 20 61  ./*..** Decode a
470a5 20 62 6c 6f 62 20 6f 66 20 76 61 72 69 6e 74 73   blob of varints
470a6 20 69 6e 74 6f 20 4e 20 69 6e 74 65 67 65 72 73   into N integers
470a7 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
470a8 64 20 66 74 73 33 44 65 63 6f 64 65 49 6e 74 41  d fts3DecodeIntA
470a9 72 72 61 79 28 0d 0a 20 20 69 6e 74 20 4e 2c 20  rray(..  int N, 
470aa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
470ab 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 74  he number of int
470ac 65 67 65 72 73 20 74 6f 20 64 65 63 6f 64 65 20  egers to decode 
470ad 2a 2f 0d 0a 20 20 75 33 32 20 2a 61 2c 20 20 20  */..  u32 *a,   
470ae 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
470af 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  e the integer va
470b0 6c 75 65 73 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  lues */..  const
470b1 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 20 2f 2a   char *zBuf,  /*
470b2 20 54 68 65 20 42 4c 4f 42 20 63 6f 6e 74 61 69   The BLOB contai
470b3 6e 69 6e 67 20 74 68 65 20 76 61 72 69 6e 74 73  ning the varints
470b4 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 75 66 20   */..  int nBuf 
470b5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a            /* siz
470b6 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 2a 2f  e of the BLOB */
470b7 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 2c 20 6a  ..){..  int i, j
470b8 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
470b9 4d 45 54 45 52 28 6e 42 75 66 29 3b 0d 0a 20 20  METER(nBuf);..  
470ba 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 4e 3b 20  for(i=j=0; i<N; 
470bb 69 2b 2b 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74  i++){..    sqlit
470bc 65 33 5f 69 6e 74 36 34 20 78 3b 0d 0a 20 20 20  e3_int64 x;..   
470bd 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73   j += sqlite3Fts
470be 33 47 65 74 56 61 72 69 6e 74 28 26 7a 42 75 66  3GetVarint(&zBuf
470bf 5b 6a 5d 2c 20 26 78 29 3b 0d 0a 20 20 20 20 61  [j], &x);..    a
470c0 73 73 65 72 74 28 6a 3c 3d 6e 42 75 66 29 3b 0d  ssert(j<=nBuf);.
470c1 0a 20 20 20 20 61 5b 69 5d 20 3d 20 28 75 33 32  .    a[i] = (u32
470c2 29 28 78 20 26 20 30 78 66 66 66 66 66 66 66 66  )(x & 0xffffffff
470c3 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a  );..  }..}..../*
470c4 0d 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20  ..** Insert the 
470c5 73 69 7a 65 73 20 28 69 6e 20 74 6f 6b 65 6e 73  sizes (in tokens
470c6 29 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  ) for each colum
470c7 6e 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e  n of the documen
470c8 74 0d 0a 2a 2a 20 77 69 74 68 20 64 6f 63 69 64  t..** with docid
470c9 20 65 71 75 61 6c 20 74 6f 20 70 2d 3e 69 50 72   equal to p->iPr
470ca 65 76 44 6f 63 69 64 2e 20 20 54 68 65 20 73 69  evDocid.  The si
470cb 7a 65 73 20 61 72 65 20 65 6e 63 6f 64 65 64 20  zes are encoded 
470cc 61 73 0d 0a 2a 2a 20 61 20 62 6c 6f 62 20 6f 66  as..** a blob of
470cd 20 76 61 72 69 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73   varints...*/..s
470ce 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 49  tatic void fts3I
470cf 6e 73 65 72 74 44 6f 63 73 69 7a 65 28 0d 0a 20  nsertDocsize(.. 
470d0 20 69 6e 74 20 2a 70 52 43 2c 20 20 20 20 20 20   int *pRC,      
470d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
470d2 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
470d3 2a 2f 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20  */..  Fts3Table 
470d4 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
470d5 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 69        /* Table i
470d6 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 69 6e 73  nto which to ins
470d7 65 72 74 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 61  ert */..  u32 *a
470d8 53 7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Sz              
470d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
470da 65 73 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  es of each colum
470db 6e 2c 20 69 6e 20 74 6f 6b 65 6e 73 20 2a 2f 0d  n, in tokens */.
470dc 0a 29 7b 0d 0a 20 20 63 68 61 72 20 2a 70 42 6c  .){..  char *pBl
470dd 6f 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ob;             
470de 2f 2a 20 54 68 65 20 42 4c 4f 42 20 65 6e 63 6f  /* The BLOB enco
470df 64 69 6e 67 20 6f 66 20 74 68 65 20 64 6f 63 75  ding of the docu
470e0 6d 65 6e 74 20 73 69 7a 65 20 2a 2f 0d 0a 20 20  ment size */..  
470e1 69 6e 74 20 6e 42 6c 6f 62 3b 20 20 20 20 20 20  int nBlob;      
470e2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
470e3 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
470e4 68 65 20 42 4c 4f 42 20 2a 2f 0d 0a 20 20 73 71  he BLOB */..  sq
470e5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
470e6 74 3b 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  t;     /* Statem
470e7 65 6e 74 20 75 73 65 64 20 74 6f 20 69 6e 73 65  ent used to inse
470e8 72 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  rt the encoding 
470e9 2a 2f 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  */..  int rc;   
470ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
470eb 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 72  * Result code fr
470ec 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20  om subfunctions 
470ed 2a 2f 0d 0a 0d 0a 20 20 69 66 28 20 2a 70 52 43  */....  if( *pRC
470ee 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 70 42   ) return;..  pB
470ef 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  lob = sqlite3_ma
470f0 6c 6c 6f 63 28 20 31 30 2a 70 2d 3e 6e 43 6f 6c  lloc( 10*p->nCol
470f1 75 6d 6e 20 29 3b 0d 0a 20 20 69 66 28 20 70 42  umn );..  if( pB
470f2 6c 6f 62 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 2a  lob==0 ){..    *
470f3 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  pRC = SQLITE_NOM
470f4 45 4d 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b  EM;..    return;
470f5 0d 0a 20 20 7d 0d 0a 20 20 66 74 73 33 45 6e 63  ..  }..  fts3Enc
470f6 6f 64 65 49 6e 74 41 72 72 61 79 28 70 2d 3e 6e  odeIntArray(p->n
470f7 43 6f 6c 75 6d 6e 2c 20 61 53 7a 2c 20 70 42 6c  Column, aSz, pBl
470f8 6f 62 2c 20 26 6e 42 6c 6f 62 29 3b 0d 0a 20 20  ob, &nBlob);..  
470f9 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
470fa 28 70 2c 20 53 51 4c 5f 52 45 50 4c 41 43 45 5f  (p, SQL_REPLACE_
470fb 44 4f 43 53 49 5a 45 2c 20 26 70 53 74 6d 74 2c  DOCSIZE, &pStmt,
470fc 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 20 29   0);..  if( rc )
470fd 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  {..    sqlite3_f
470fe 72 65 65 28 70 42 6c 6f 62 29 3b 0d 0a 20 20 20  ree(pBlob);..   
470ff 20 2a 70 52 43 20 3d 20 72 63 3b 0d 0a 20 20 20   *pRC = rc;..   
47100 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
47101 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
47102 74 36 34 28 70 53 74 6d 74 2c 20 31 2c 20 70 2d  t64(pStmt, 1, p-
47103 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0d 0a 20  >iPrevDocid);.. 
47104 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
47105 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 70 42 6c  ob(pStmt, 2, pBl
47106 6f 62 2c 20 6e 42 6c 6f 62 2c 20 73 71 6c 69 74  ob, nBlob, sqlit
47107 65 33 5f 66 72 65 65 29 3b 0d 0a 20 20 73 71 6c  e3_free);..  sql
47108 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
47109 3b 0d 0a 20 20 2a 70 52 43 20 3d 20 73 71 6c 69  ;..  *pRC = sqli
4710a 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29  te3_reset(pStmt)
4710b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52  ;..}..../*..** R
4710c 65 63 6f 72 64 20 30 20 6f 66 20 74 68 65 20 25  ecord 0 of the %
4710d 5f 73 74 61 74 20 74 61 62 6c 65 20 63 6f 6e 74  _stat table cont
4710e 61 69 6e 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  ains a blob cons
4710f 69 73 74 69 6e 67 20 6f 66 20 4e 20 76 61 72 69  isting of N vari
47110 6e 74 73 2c 0d 0a 2a 2a 20 77 68 65 72 65 20 4e  nts,..** where N
47111 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
47112 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 63  f user defined c
47113 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 66 74  olumns in the ft
47114 73 33 20 74 61 62 6c 65 20 70 6c 75 73 0d 0a 2a  s3 table plus..*
47115 2a 20 74 77 6f 2e 20 49 66 20 6e 43 6f 6c 20 69  * two. If nCol i
47116 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
47117 75 73 65 72 20 64 65 66 69 6e 65 64 20 63 6f 6c  user defined col
47118 75 6d 6e 73 2c 20 74 68 65 6e 20 76 61 6c 75 65  umns, then value
47119 73 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 76 61  s of the ..** va
4711a 72 69 6e 74 73 20 61 72 65 20 73 65 74 20 61 73  rints are set as
4711b 20 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a   follows:..**..*
4711c 2a 20 20 20 56 61 72 69 6e 74 20 30 3a 20 20 20  *   Varint 0:   
4711d 20 20 20 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72      Total number
4711e 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20   of rows in the 
4711f 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  table...**..**  
47120 20 56 61 72 69 6e 74 20 31 2e 2e 6e 43 6f 6c 3a   Varint 1..nCol:
47121 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   For each column
47122 2c 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  , the total numb
47123 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 73 74 6f  er of tokens sto
47124 72 65 64 20 69 6e 0d 0a 2a 2a 20 20 20 20 20 20  red in..**      
47125 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
47126 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 61 6c 6c 20   column for all 
47127 72 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c  rows of the tabl
47128 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 56 61 72  e...**..**   Var
47129 69 6e 74 20 31 2b 6e 43 6f 6c 3a 20 20 54 68 65  int 1+nCol:  The
4712a 20 74 6f 74 61 6c 20 73 69 7a 65 2c 20 69 6e 20   total size, in 
4712b 62 79 74 65 73 2c 20 6f 66 20 61 6c 6c 20 74 65  bytes, of all te
4712c 78 74 20 76 61 6c 75 65 73 20 69 6e 20 61 6c 6c  xt values in all
4712d 0d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
4712e 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f         columns o
4712f 66 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20 74 68  f all rows of th
47130 65 20 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2f  e table...**..*/
47131 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  ..static void ft
47132 73 33 55 70 64 61 74 65 44 6f 63 54 6f 74 61 6c  s3UpdateDocTotal
47133 73 28 0d 0a 20 20 69 6e 74 20 2a 70 52 43 2c 20  s(..  int *pRC, 
47134 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47135 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73        /* The res
47136 75 6c 74 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 46  ult code */..  F
47137 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20  ts3Table *p,    
47138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47139 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 75 70  * Table being up
4713a 64 61 74 65 64 20 2a 2f 0d 0a 20 20 75 33 32 20  dated */..  u32 
4713b 2a 61 53 7a 49 6e 73 2c 20 20 20 20 20 20 20 20  *aSzIns,        
4713c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
4713d 69 7a 65 20 69 6e 63 72 65 61 73 65 73 20 2a 2f  ize increases */
4713e 0d 0a 20 20 75 33 32 20 2a 61 53 7a 44 65 6c 2c  ..  u32 *aSzDel,
4713f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47140 20 20 20 20 2f 2a 20 53 69 7a 65 20 64 65 63 72      /* Size decr
47141 65 61 73 65 73 20 2a 2f 0d 0a 20 20 69 6e 74 20  eases */..  int 
47142 6e 43 68 6e 67 20 20 20 20 20 20 20 20 20 20 20  nChng           
47143 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
47144 68 61 6e 67 65 20 69 6e 20 74 68 65 20 6e 75 6d  hange in the num
47145 62 65 72 20 6f 66 20 64 6f 63 75 6d 65 6e 74 73  ber of documents
47146 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20   */..){..  char 
47147 2a 70 42 6c 6f 62 3b 20 20 20 20 20 20 20 20 20  *pBlob;         
47148 20 20 20 20 2f 2a 20 53 74 6f 72 61 67 65 20 66      /* Storage f
47149 6f 72 20 42 4c 4f 42 20 77 72 69 74 74 65 6e 20  or BLOB written 
4714a 69 6e 74 6f 20 25 5f 73 74 61 74 20 2a 2f 0d 0a  into %_stat */..
4714b 20 20 69 6e 74 20 6e 42 6c 6f 62 3b 20 20 20 20    int nBlob;    
4714c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
4714d 7a 65 20 6f 66 20 42 4c 4f 42 20 77 72 69 74 74  ze of BLOB writt
4714e 65 6e 20 69 6e 74 6f 20 25 5f 73 74 61 74 20 2a  en into %_stat *
4714f 2f 0d 0a 20 20 75 33 32 20 2a 61 3b 20 20 20 20  /..  u32 *a;    
47150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47151 20 41 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65   Array of intege
47152 72 73 20 74 68 61 74 20 62 65 63 6f 6d 65 73 20  rs that becomes 
47153 74 68 65 20 42 4c 4f 42 20 2a 2f 0d 0a 20 20 73  the BLOB */..  s
47154 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
47155 6d 74 3b 20 20 20 20 20 2f 2a 20 53 74 61 74 65  mt;     /* State
47156 6d 65 6e 74 20 66 6f 72 20 72 65 61 64 69 6e 67  ment for reading
47157 20 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0d   and writing */.
47158 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
47159 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
4715a 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a  oop counter */..
4715b 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
4715c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
4715d 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73  sult code from s
4715e 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0d 0a  ubfunctions */..
4715f 0d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53  ..  const int nS
47160 74 61 74 20 3d 20 70 2d 3e 6e 43 6f 6c 75 6d 6e  tat = p->nColumn
47161 2b 32 3b 0d 0a 0d 0a 20 20 69 66 28 20 2a 70 52  +2;....  if( *pR
47162 43 20 29 20 72 65 74 75 72 6e 3b 0d 0a 20 20 61  C ) return;..  a
47163 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
47164 63 28 20 28 73 69 7a 65 6f 66 28 75 33 32 29 2b  c( (sizeof(u32)+
47165 31 30 29 2a 6e 53 74 61 74 20 29 3b 0d 0a 20 20  10)*nStat );..  
47166 69 66 28 20 61 3d 3d 30 20 29 7b 0d 0a 20 20 20  if( a==0 ){..   
47167 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 4e   *pRC = SQLITE_N
47168 4f 4d 45 4d 3b 0d 0a 20 20 20 20 72 65 74 75 72  OMEM;..    retur
47169 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 70 42 6c 6f 62  n;..  }..  pBlob
4716a 20 3d 20 28 63 68 61 72 2a 29 26 61 5b 6e 53 74   = (char*)&a[nSt
4716b 61 74 5d 3b 0d 0a 20 20 72 63 20 3d 20 66 74 73  at];..  rc = fts
4716c 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
4716d 53 45 4c 45 43 54 5f 44 4f 43 54 4f 54 41 4c 2c  SELECT_DOCTOTAL,
4716e 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20   &pStmt, 0);..  
4716f 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20 73  if( rc ){..    s
47170 71 6c 69 74 65 33 5f 66 72 65 65 28 61 29 3b 0d  qlite3_free(a);.
47171 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0d  .    *pRC = rc;.
47172 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20  .    return;..  
47173 7d 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  }..  if( sqlite3
47174 5f 73 74 65 70 28 70 53 74 6d 74 29 3d 3d 53 51  _step(pStmt)==SQ
47175 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20 20 20  LITE_ROW ){..   
47176 20 66 74 73 33 44 65 63 6f 64 65 49 6e 74 41 72   fts3DecodeIntAr
47177 72 61 79 28 6e 53 74 61 74 2c 20 61 2c 0d 0a 20  ray(nStat, a,.. 
47178 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
47179 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d  column_blob(pStm
4717a 74 2c 20 30 29 2c 0d 0a 20 20 20 20 20 20 20 20  t, 0),..        
4717b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
4717c 62 79 74 65 73 28 70 53 74 6d 74 2c 20 30 29 29  bytes(pStmt, 0))
4717d 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20  ;..  }else{..   
4717e 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 69   memset(a, 0, si
4717f 7a 65 6f 66 28 75 33 32 29 2a 28 6e 53 74 61 74  zeof(u32)*(nStat
47180 29 20 29 3b 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c  ) );..  }..  sql
47181 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
47182 29 3b 0d 0a 20 20 69 66 28 20 6e 43 68 6e 67 3c  );..  if( nChng<
47183 30 20 26 26 20 61 5b 30 5d 3c 28 75 33 32 29 28  0 && a[0]<(u32)(
47184 2d 6e 43 68 6e 67 29 20 29 7b 0d 0a 20 20 20 20  -nChng) ){..    
47185 61 5b 30 5d 20 3d 20 30 3b 0d 0a 20 20 7d 65 6c  a[0] = 0;..  }el
47186 73 65 7b 0d 0a 20 20 20 20 61 5b 30 5d 20 2b 3d  se{..    a[0] +=
47187 20 6e 43 68 6e 67 3b 0d 0a 20 20 7d 0d 0a 20 20   nChng;..  }..  
47188 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
47189 6f 6c 75 6d 6e 2b 31 3b 20 69 2b 2b 29 7b 0d 0a  olumn+1; i++){..
4718a 20 20 20 20 75 33 32 20 78 20 3d 20 61 5b 69 2b      u32 x = a[i+
4718b 31 5d 3b 0d 0a 20 20 20 20 69 66 28 20 78 2b 61  1];..    if( x+a
4718c 53 7a 49 6e 73 5b 69 5d 20 3c 20 61 53 7a 44 65  SzIns[i] < aSzDe
4718d 6c 5b 69 5d 20 29 7b 0d 0a 20 20 20 20 20 20 78  l[i] ){..      x
4718e 20 3d 20 30 3b 0d 0a 20 20 20 20 7d 65 6c 73 65   = 0;..    }else
4718f 7b 0d 0a 20 20 20 20 20 20 78 20 3d 20 78 20 2b  {..      x = x +
47190 20 61 53 7a 49 6e 73 5b 69 5d 20 2d 20 61 53 7a   aSzIns[i] - aSz
47191 44 65 6c 5b 69 5d 3b 0d 0a 20 20 20 20 7d 0d 0a  Del[i];..    }..
47192 20 20 20 20 61 5b 69 2b 31 5d 20 3d 20 78 3b 0d      a[i+1] = x;.
47193 0a 20 20 7d 0d 0a 20 20 66 74 73 33 45 6e 63 6f  .  }..  fts3Enco
47194 64 65 49 6e 74 41 72 72 61 79 28 6e 53 74 61 74  deIntArray(nStat
47195 2c 20 61 2c 20 70 42 6c 6f 62 2c 20 26 6e 42 6c  , a, pBlob, &nBl
47196 6f 62 29 3b 0d 0a 20 20 72 63 20 3d 20 66 74 73  ob);..  rc = fts
47197 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
47198 52 45 50 4c 41 43 45 5f 44 4f 43 54 4f 54 41 4c  REPLACE_DOCTOTAL
47199 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20  , &pStmt, 0);.. 
4719a 20 69 66 28 20 72 63 20 29 7b 0d 0a 20 20 20 20   if( rc ){..    
4719b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 29 3b  sqlite3_free(a);
4719c 0d 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b  ..    *pRC = rc;
4719d 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20  ..    return;.. 
4719e 20 7d 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   }..  sqlite3_bi
4719f 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 31  nd_blob(pStmt, 1
471a0 2c 20 70 42 6c 6f 62 2c 20 6e 42 6c 6f 62 2c 20  , pBlob, nBlob, 
471a1 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d  SQLITE_STATIC);.
471a2 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
471a3 70 53 74 6d 74 29 3b 0d 0a 20 20 2a 70 52 43 20  pStmt);..  *pRC 
471a4 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
471a5 70 53 74 6d 74 29 3b 0d 0a 20 20 73 71 6c 69 74  pStmt);..  sqlit
471a6 65 33 5f 66 72 65 65 28 61 29 3b 0d 0a 7d 0d 0a  e3_free(a);..}..
471a7 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
471a8 33 44 6f 4f 70 74 69 6d 69 7a 65 28 46 74 73 33  3DoOptimize(Fts3
471a9 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 62 52  Table *p, int bR
471aa 65 74 75 72 6e 44 6f 6e 65 29 7b 0d 0a 20 20 69  eturnDone){..  i
471ab 6e 74 20 69 3b 0d 0a 20 20 69 6e 74 20 62 53 65  nt i;..  int bSe
471ac 65 6e 44 6f 6e 65 20 3d 20 30 3b 0d 0a 20 20 69  enDone = 0;..  i
471ad 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
471ae 4b 3b 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72  K;..  for(i=0; r
471af 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
471b0 69 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b 2b  i<p->nIndex; i++
471b1 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ){..    rc = fts
471b2 33 53 65 67 6d 65 6e 74 4d 65 72 67 65 28 70 2c  3SegmentMerge(p,
471b3 20 69 2c 20 46 54 53 33 5f 53 45 47 43 55 52 53   i, FTS3_SEGCURS
471b4 4f 52 5f 41 4c 4c 29 3b 0d 0a 20 20 20 20 69 66  OR_ALL);..    if
471b5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
471b6 45 20 29 7b 0d 0a 20 20 20 20 20 20 62 53 65 65  E ){..      bSee
471b7 6e 44 6f 6e 65 20 3d 20 31 3b 0d 0a 20 20 20 20  nDone = 1;..    
471b8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
471b9 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
471ba 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 6d   sqlite3Fts3Segm
471bb 65 6e 74 73 43 6c 6f 73 65 28 70 29 3b 0d 0a 20  entsClose(p);.. 
471bc 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
471bd 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 70 29  ingTermsClear(p)
471be 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 28 72  ;....  return (r
471bf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
471c0 62 52 65 74 75 72 6e 44 6f 6e 65 20 26 26 20 62  bReturnDone && b
471c1 53 65 65 6e 44 6f 6e 65 29 20 3f 20 53 51 4c 49  SeenDone) ? SQLI
471c2 54 45 5f 44 4f 4e 45 20 3a 20 72 63 3b 0d 0a 7d  TE_DONE : rc;..}
471c3 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
471c4 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
471c5 65 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72  ed when the user
471c6 20 65 78 65 63 75 74 65 73 20 74 68 65 20 66 6f   executes the fo
471c7 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
471c8 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 49  t:..**..**     I
471c9 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 62 6c 3e  NSERT INTO <tbl>
471ca 28 3c 74 62 6c 3e 29 20 56 41 4c 55 45 53 28 27  (<tbl>) VALUES('
471cb 72 65 62 75 69 6c 64 27 29 3b 0d 0a 2a 2a 0d 0a  rebuild');..**..
471cc 2a 2a 20 54 68 65 20 65 6e 74 69 72 65 20 46 54  ** The entire FT
471cd 53 20 69 6e 64 65 78 20 69 73 20 64 69 73 63 61  S index is disca
471ce 72 64 65 64 20 61 6e 64 20 72 65 62 75 69 6c 74  rded and rebuilt
471cf 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 69  . If the table i
471d0 73 20 6f 6e 65 20 0d 0a 2a 2a 20 63 72 65 61 74  s one ..** creat
471d1 65 64 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6e  ed using the con
471d2 74 65 6e 74 3d 78 78 78 20 6f 70 74 69 6f 6e 2c  tent=xxx option,
471d3 20 74 68 65 6e 20 74 68 65 20 6e 65 77 20 69 6e   then the new in
471d4 64 65 78 20 69 73 20 62 61 73 65 64 20 6f 6e 0d  dex is based on.
471d5 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20  .** the current 
471d6 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
471d7 78 78 78 20 74 61 62 6c 65 2e 20 4f 74 68 65 72  xxx table. Other
471d8 77 69 73 65 2c 20 69 74 20 69 73 20 72 65 62 75  wise, it is rebu
471d9 69 6c 74 20 62 61 73 65 64 0d 0a 2a 2a 20 6f 6e  ilt based..** on
471da 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
471db 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
471dc 61 62 6c 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  able...*/..stati
471dd 63 20 69 6e 74 20 66 74 73 33 44 6f 52 65 62 75  c int fts3DoRebu
471de 69 6c 64 28 46 74 73 33 54 61 62 6c 65 20 2a 70  ild(Fts3Table *p
471df 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  ){..  int rc;   
471e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471e1 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
471e2 43 6f 64 65 20 2a 2f 0d 0a 0d 0a 20 20 72 63 20  Code */....  rc 
471e3 3d 20 66 74 73 33 44 65 6c 65 74 65 41 6c 6c 28  = fts3DeleteAll(
471e4 70 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63  p, 0);..  if( rc
471e5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
471e6 20 20 20 20 75 33 32 20 2a 61 53 7a 20 3d 20 30      u32 *aSz = 0
471e7 3b 0d 0a 20 20 20 20 75 33 32 20 2a 61 53 7a 49  ;..    u32 *aSzI
471e8 6e 73 20 3d 20 30 3b 0d 0a 20 20 20 20 75 33 32  ns = 0;..    u32
471e9 20 2a 61 53 7a 44 65 6c 20 3d 20 30 3b 0d 0a 20   *aSzDel = 0;.. 
471ea 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
471eb 2a 70 53 74 6d 74 20 3d 20 30 3b 0d 0a 20 20 20  *pStmt = 0;..   
471ec 20 69 6e 74 20 6e 45 6e 74 72 79 20 3d 20 30 3b   int nEntry = 0;
471ed 0d 0a 0d 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 6f  ....    /* Compo
471ee 73 65 20 61 6e 64 20 70 72 65 70 61 72 65 20 61  se and prepare a
471ef 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
471f0 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20  to loop through 
471f1 74 68 65 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c  the content tabl
471f2 65 20 2a 2f 0d 0a 20 20 20 20 63 68 61 72 20 2a  e */..    char *
471f3 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSql = sqlite3_m
471f4 70 72 69 6e 74 66 28 22 53 45 4c 45 43 54 20 25  printf("SELECT %
471f5 73 22 20 2c 20 70 2d 3e 7a 52 65 61 64 45 78 70  s" , p->zReadExp
471f6 72 6c 69 73 74 29 3b 0d 0a 20 20 20 20 69 66 28  rlist);..    if(
471f7 20 21 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 20   !zSql ){..     
471f8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
471f9 45 4d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  EM;..    }else{.
471fa 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
471fb 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 70  te3_prepare_v2(p
471fc 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  ->db, zSql, -1, 
471fd 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 20  &pStmt, 0);..   
471fe 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
471ff 7a 53 71 6c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 0d  zSql);..    }...
47200 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
47201 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
47202 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a   int nByte = siz
47203 65 6f 66 28 75 33 32 29 20 2a 20 28 70 2d 3e 6e  eof(u32) * (p->n
47204 43 6f 6c 75 6d 6e 2b 31 29 2a 33 3b 0d 0a 20 20  Column+1)*3;..  
47205 20 20 20 20 61 53 7a 20 3d 20 28 75 33 32 20 2a      aSz = (u32 *
47206 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
47207 6e 42 79 74 65 29 3b 0d 0a 20 20 20 20 20 20 69  nByte);..      i
47208 66 28 20 61 53 7a 3d 3d 30 20 29 7b 0d 0a 20 20  f( aSz==0 ){..  
47209 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
4720a 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20 20  E_NOMEM;..      
4720b 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20  }else{..        
4720c 6d 65 6d 73 65 74 28 61 53 7a 2c 20 30 2c 20 6e  memset(aSz, 0, n
4720d 42 79 74 65 29 3b 0d 0a 20 20 20 20 20 20 20 20  Byte);..        
4720e 61 53 7a 49 6e 73 20 3d 20 26 61 53 7a 5b 70 2d  aSzIns = &aSz[p-
4720f 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0d 0a 20 20  >nColumn+1];..  
47210 20 20 20 20 20 20 61 53 7a 44 65 6c 20 3d 20 26        aSzDel = &
47211 61 53 7a 49 6e 73 5b 70 2d 3e 6e 43 6f 6c 75 6d  aSzIns[p->nColum
47212 6e 2b 31 5d 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  n+1];..      }..
47213 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 77 68 69      }....    whi
47214 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
47215 4b 20 26 26 20 53 51 4c 49 54 45 5f 52 4f 57 3d  K && SQLITE_ROW=
47216 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53  =sqlite3_step(pS
47217 74 6d 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 69  tmt) ){..      i
47218 6e 74 20 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20  nt iCol;..      
47219 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67  rc = fts3Pending
4721a 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20 73 71  TermsDocid(p, sq
4721b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
4721c 36 34 28 70 53 74 6d 74 2c 20 30 29 29 3b 0d 0a  64(pStmt, 0));..
4721d 20 20 20 20 20 20 61 53 7a 5b 70 2d 3e 6e 43 6f        aSz[p->nCo
4721e 6c 75 6d 6e 5d 20 3d 20 30 3b 0d 0a 20 20 20 20  lumn] = 0;..    
4721f 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 72 63    for(iCol=0; rc
47220 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
47221 43 6f 6c 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  Col<p->nColumn; 
47222 69 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  iCol++){..      
47223 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
47224 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
47225 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
47226 74 65 78 74 28 70 53 74 6d 74 2c 20 69 43 6f 6c  text(pStmt, iCol
47227 2b 31 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63  +1);..        rc
47228 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65   = fts3PendingTe
47229 72 6d 73 41 64 64 28 70 2c 20 7a 2c 20 69 43 6f  rmsAdd(p, z, iCo
4722a 6c 2c 20 26 61 53 7a 5b 69 43 6f 6c 5d 29 3b 0d  l, &aSz[iCol]);.
4722b 0a 20 20 20 20 20 20 20 20 61 53 7a 5b 70 2d 3e  .        aSz[p->
4722c 6e 43 6f 6c 75 6d 6e 5d 20 2b 3d 20 73 71 6c 69  nColumn] += sqli
4722d 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
4722e 28 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b  (pStmt, iCol+1);
4722f 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
47230 20 69 66 28 20 70 2d 3e 62 48 61 73 44 6f 63 73   if( p->bHasDocs
47231 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ize ){..        
47232 66 74 73 33 49 6e 73 65 72 74 44 6f 63 73 69 7a  fts3InsertDocsiz
47233 65 28 26 72 63 2c 20 70 2c 20 61 53 7a 29 3b 0d  e(&rc, p, aSz);.
47234 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
47235 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
47236 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 73 71  K ){..        sq
47237 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
47238 53 74 6d 74 29 3b 0d 0a 20 20 20 20 20 20 20 20  Stmt);..        
47239 70 53 74 6d 74 20 3d 20 30 3b 0d 0a 20 20 20 20  pStmt = 0;..    
4723a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
4723b 20 20 6e 45 6e 74 72 79 2b 2b 3b 0d 0a 20 20 20    nEntry++;..   
4723c 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b       for(iCol=0;
4723d 20 69 43 6f 6c 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d   iCol<=p->nColum
4723e 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20  n; iCol++){..   
4723f 20 20 20 20 20 20 20 61 53 7a 49 6e 73 5b 69 43         aSzIns[iC
47240 6f 6c 5d 20 2b 3d 20 61 53 7a 5b 69 43 6f 6c 5d  ol] += aSz[iCol]
47241 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
47242 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
47243 20 20 69 66 28 20 70 2d 3e 62 48 61 73 53 74 61    if( p->bHasSta
47244 74 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33  t ){..      fts3
47245 55 70 64 61 74 65 44 6f 63 54 6f 74 61 6c 73 28  UpdateDocTotals(
47246 26 72 63 2c 20 70 2c 20 61 53 7a 49 6e 73 2c 20  &rc, p, aSzIns, 
47247 61 53 7a 44 65 6c 2c 20 6e 45 6e 74 72 79 29 3b  aSzDel, nEntry);
47248 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
47249 69 74 65 33 5f 66 72 65 65 28 61 53 7a 29 3b 0d  ite3_free(aSz);.
4724a 0a 0d 0a 20 20 20 20 69 66 28 20 70 53 74 6d 74  ...    if( pStmt
4724b 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72   ){..      int r
4724c 63 32 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  c2 = sqlite3_fin
4724d 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0d 0a 20  alize(pStmt);.. 
4724e 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
4724f 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
47250 20 20 20 72 63 20 3d 20 72 63 32 3b 0d 0a 20 20     rc = rc2;..  
47251 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20      }..    }..  
47252 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  }....  return rc
47253 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 48  ;..}..../*..** H
47254 61 6e 64 6c 65 20 61 20 27 73 70 65 63 69 61 6c  andle a 'special
47255 27 20 49 4e 53 45 52 54 20 6f 66 20 74 68 65 20  ' INSERT of the 
47256 66 6f 72 6d 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  form:..**..**   
47257 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 62 6c  "INSERT INTO tbl
47258 28 74 62 6c 29 20 56 41 4c 55 45 53 28 3c 65 78  (tbl) VALUES(<ex
47259 70 72 3e 29 22 0d 0a 2a 2a 0d 0a 2a 2a 20 41 72  pr>)"..**..** Ar
4725a 67 75 6d 65 6e 74 20 70 56 61 6c 20 63 6f 6e 74  gument pVal cont
4725b 61 69 6e 73 20 74 68 65 20 72 65 73 75 6c 74 20  ains the result 
4725c 6f 66 20 3c 65 78 70 72 3e 2e 20 43 75 72 72 65  of <expr>. Curre
4725d 6e 74 6c 79 20 74 68 65 20 6f 6e 6c 79 20 0d 0a  ntly the only ..
4725e 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 76 61  ** meaningful va
4725f 6c 75 65 20 74 6f 20 69 6e 73 65 72 74 20 69 73  lue to insert is
47260 20 74 68 65 20 74 65 78 74 20 27 6f 70 74 69 6d   the text 'optim
47261 69 7a 65 27 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ize'...*/..stati
47262 63 20 69 6e 74 20 66 74 73 33 53 70 65 63 69 61  c int fts3Specia
47263 6c 49 6e 73 65 72 74 28 46 74 73 33 54 61 62 6c  lInsert(Fts3Tabl
47264 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61  e *p, sqlite3_va
47265 6c 75 65 20 2a 70 56 61 6c 29 7b 0d 0a 20 20 69  lue *pVal){..  i
47266 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
47267 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47268 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f  * Return Code */
47269 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
4726a 7a 56 61 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68  zVal = (const ch
4726b 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
4726c 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0d 0a  ue_text(pVal);..
4726d 20 20 69 6e 74 20 6e 56 61 6c 20 3d 20 73 71 6c    int nVal = sql
4726e 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
4726f 28 70 56 61 6c 29 3b 0d 0a 0d 0a 20 20 69 66 28  (pVal);....  if(
47270 20 21 7a 56 61 6c 20 29 7b 0d 0a 20 20 20 20 72   !zVal ){..    r
47271 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
47272 45 4d 3b 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28  EM;..  }else if(
47273 20 6e 56 61 6c 3d 3d 38 20 26 26 20 30 3d 3d 73   nVal==8 && 0==s
47274 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28  qlite3_strnicmp(
47275 7a 56 61 6c 2c 20 22 6f 70 74 69 6d 69 7a 65 22  zVal, "optimize"
47276 2c 20 38 29 20 29 7b 0d 0a 20 20 20 20 72 63 20  , 8) ){..    rc 
47277 3d 20 66 74 73 33 44 6f 4f 70 74 69 6d 69 7a 65  = fts3DoOptimize
47278 28 70 2c 20 30 29 3b 0d 0a 20 20 7d 65 6c 73 65  (p, 0);..  }else
47279 20 69 66 28 20 6e 56 61 6c 3d 3d 37 20 26 26 20   if( nVal==7 && 
4727a 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  0==sqlite3_strni
4727b 63 6d 70 28 7a 56 61 6c 2c 20 22 72 65 62 75 69  cmp(zVal, "rebui
4727c 6c 64 22 2c 20 37 29 20 29 7b 0d 0a 20 20 20 20  ld", 7) ){..    
4727d 72 63 20 3d 20 66 74 73 33 44 6f 52 65 62 75 69  rc = fts3DoRebui
4727e 6c 64 28 70 29 3b 0d 0a 23 69 66 64 65 66 20 53  ld(p);..#ifdef S
4727f 51 4c 49 54 45 5f 54 45 53 54 0d 0a 20 20 7d 65  QLITE_TEST..  }e
47280 6c 73 65 20 69 66 28 20 6e 56 61 6c 3e 39 20 26  lse if( nVal>9 &
47281 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72  & 0==sqlite3_str
47282 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22 6e 6f 64  nicmp(zVal, "nod
47283 65 73 69 7a 65 3d 22 2c 20 39 29 20 29 7b 0d 0a  esize=", 9) ){..
47284 20 20 20 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65      p->nNodeSize
47285 20 3d 20 61 74 6f 69 28 26 7a 56 61 6c 5b 39 5d   = atoi(&zVal[9]
47286 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  );..    rc = SQL
47287 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 65 6c 73 65  ITE_OK;..  }else
47288 20 69 66 28 20 6e 56 61 6c 3e 31 31 20 26 26 20   if( nVal>11 && 
47289 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  0==sqlite3_strni
4728a 63 6d 70 28 7a 56 61 6c 2c 20 22 6d 61 78 70 65  cmp(zVal, "maxpe
4728b 6e 64 69 6e 67 3d 22 2c 20 39 29 20 29 7b 0d 0a  nding=", 9) ){..
4728c 20 20 20 20 70 2d 3e 6e 4d 61 78 50 65 6e 64 69      p->nMaxPendi
4728d 6e 67 44 61 74 61 20 3d 20 61 74 6f 69 28 26 7a  ngData = atoi(&z
4728e 56 61 6c 5b 31 31 5d 29 3b 0d 0a 20 20 20 20 72  Val[11]);..    r
4728f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
47290 23 65 6e 64 69 66 0d 0a 20 20 7d 65 6c 73 65 7b  #endif..  }else{
47291 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ..    rc = SQLIT
47292 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 0d  E_ERROR;..  }...
47293 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
47294 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74  ..../*..** Delet
47295 65 20 61 6c 6c 20 63 61 63 68 65 64 20 64 65 66  e all cached def
47296 65 72 72 65 64 20 64 6f 63 6c 69 73 74 73 2e 20  erred doclists. 
47297 44 65 66 65 72 72 65 64 20 64 6f 63 6c 69 73 74  Deferred doclist
47298 73 20 61 72 65 20 63 61 63 68 65 64 0d 0a 2a 2a  s are cached..**
47299 20 28 61 6c 6c 6f 63 61 74 65 64 29 20 62 79 20   (allocated) by 
4729a 74 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 43  the sqlite3Fts3C
4729b 61 63 68 65 44 65 66 65 72 72 65 64 44 6f 63 6c  acheDeferredDocl
4729c 69 73 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e  ists() function.
4729d 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
4729e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4729f 33 46 74 73 33 46 72 65 65 44 65 66 65 72 72 65  3Fts3FreeDeferre
472a0 64 44 6f 63 6c 69 73 74 73 28 46 74 73 33 43 75  dDoclists(Fts3Cu
472a1 72 73 6f 72 20 2a 70 43 73 72 29 7b 0d 0a 20 20  rsor *pCsr){..  
472a2 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65  Fts3DeferredToke
472a3 6e 20 2a 70 44 65 66 3b 0d 0a 20 20 66 6f 72 28  n *pDef;..  for(
472a4 70 44 65 66 3d 70 43 73 72 2d 3e 70 44 65 66 65  pDef=pCsr->pDefe
472a5 72 72 65 64 3b 20 70 44 65 66 3b 20 70 44 65 66  rred; pDef; pDef
472a6 3d 70 44 65 66 2d 3e 70 4e 65 78 74 29 7b 0d 0a  =pDef->pNext){..
472a7 20 20 20 20 66 74 73 33 50 65 6e 64 69 6e 67 4c      fts3PendingL
472a8 69 73 74 44 65 6c 65 74 65 28 70 44 65 66 2d 3e  istDelete(pDef->
472a9 70 4c 69 73 74 29 3b 0d 0a 20 20 20 20 70 44 65  pList);..    pDe
472aa 66 2d 3e 70 4c 69 73 74 20 3d 20 30 3b 0d 0a 20  f->pList = 0;.. 
472ab 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
472ac 46 72 65 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  Free all entries
472ad 20 69 6e 20 74 68 65 20 70 43 73 72 2d 3e 70 44   in the pCsr->pD
472ae 65 66 66 65 72 65 64 20 6c 69 73 74 2e 20 45 6e  effered list. En
472af 74 72 69 65 73 20 61 72 65 20 61 64 64 65 64 20  tries are added 
472b0 74 6f 20 0d 0a 2a 2a 20 74 68 69 73 20 6c 69 73  to ..** this lis
472b1 74 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 46  t using sqlite3F
472b2 74 73 33 44 65 66 65 72 54 6f 6b 65 6e 28 29 2e  ts3DeferToken().
472b3 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
472b4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
472b5 33 46 74 73 33 46 72 65 65 44 65 66 65 72 72 65  3Fts3FreeDeferre
472b6 64 54 6f 6b 65 6e 73 28 46 74 73 33 43 75 72 73  dTokens(Fts3Curs
472b7 6f 72 20 2a 70 43 73 72 29 7b 0d 0a 20 20 46 74  or *pCsr){..  Ft
472b8 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 20  s3DeferredToken 
472b9 2a 70 44 65 66 3b 0d 0a 20 20 46 74 73 33 44 65  *pDef;..  Fts3De
472ba 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 4e 65  ferredToken *pNe
472bb 78 74 3b 0d 0a 20 20 66 6f 72 28 70 44 65 66 3d  xt;..  for(pDef=
472bc 70 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64 3b  pCsr->pDeferred;
472bd 20 70 44 65 66 3b 20 70 44 65 66 3d 70 4e 65 78   pDef; pDef=pNex
472be 74 29 7b 0d 0a 20 20 20 20 70 4e 65 78 74 20 3d  t){..    pNext =
472bf 20 70 44 65 66 2d 3e 70 4e 65 78 74 3b 0d 0a 20   pDef->pNext;.. 
472c0 20 20 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69     fts3PendingLi
472c1 73 74 44 65 6c 65 74 65 28 70 44 65 66 2d 3e 70  stDelete(pDef->p
472c2 4c 69 73 74 29 3b 0d 0a 20 20 20 20 73 71 6c 69  List);..    sqli
472c3 74 65 33 5f 66 72 65 65 28 70 44 65 66 29 3b 0d  te3_free(pDef);.
472c4 0a 20 20 7d 0d 0a 20 20 70 43 73 72 2d 3e 70 44  .  }..  pCsr->pD
472c5 65 66 65 72 72 65 64 20 3d 20 30 3b 0d 0a 7d 0d  eferred = 0;..}.
472c6 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 47 65 6e 65 72 61  .../*..** Genera
472c7 74 65 20 64 65 66 65 72 72 65 64 2d 64 6f 63 6c  te deferred-docl
472c8 69 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 6f 6b  ists for all tok
472c9 65 6e 73 20 69 6e 20 74 68 65 20 70 43 73 72 2d  ens in the pCsr-
472ca 3e 70 44 65 66 65 72 72 65 64 20 6c 69 73 74 0d  >pDeferred list.
472cb 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 74 68 65  .** based on the
472cc 20 72 6f 77 20 74 68 61 74 20 70 43 73 72 20 63   row that pCsr c
472cd 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
472ce 74 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 41 20 64 65  to...**..** A de
472cf 66 65 72 72 65 64 2d 64 6f 63 6c 69 73 74 20 69  ferred-doclist i
472d0 73 20 6c 69 6b 65 20 61 6e 79 20 6f 74 68 65 72  s like any other
472d1 20 64 6f 63 6c 69 73 74 20 77 69 74 68 20 70 6f   doclist with po
472d2 73 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  sition informati
472d3 6f 6e 0d 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 2c  on..** included,
472d4 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
472d5 6f 6e 6c 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e  only contains en
472d6 74 72 69 65 73 20 66 6f 72 20 61 20 73 69 6e 67  tries for a sing
472d7 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0d 0a 2a  le row of the..*
472d8 2a 20 74 61 62 6c 65 2c 20 6e 6f 74 20 66 6f 72  * table, not for
472d9 20 61 6c 6c 20 72 6f 77 73 2e 0d 0a 2a 2f 0d 0a   all rows...*/..
472da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
472db 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 43 61  nt sqlite3Fts3Ca
472dc 63 68 65 44 65 66 65 72 72 65 64 44 6f 63 6c 69  cheDeferredDocli
472dd 73 74 73 28 46 74 73 33 43 75 72 73 6f 72 20 2a  sts(Fts3Cursor *
472de 70 43 73 72 29 7b 0d 0a 20 20 69 6e 74 20 72 63  pCsr){..  int rc
472df 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
472e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
472e1 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a 20 20 69  urn code */..  i
472e2 66 28 20 70 43 73 72 2d 3e 70 44 65 66 65 72 72  f( pCsr->pDeferr
472e3 65 64 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69  ed ){..    int i
472e4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
472e5 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
472e6 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
472e7 75 67 68 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  ugh table column
472e8 73 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65  s */..    sqlite
472e9 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b 20  3_int64 iDocid; 
472ea 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 69 64          /* Docid
472eb 20 6f 66 20 74 68 65 20 72 6f 77 20 70 43 73 72   of the row pCsr
472ec 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0d 0a 20   points to */.. 
472ed 20 20 20 46 74 73 33 44 65 66 65 72 72 65 64 54     Fts3DeferredT
472ee 6f 6b 65 6e 20 2a 70 44 65 66 3b 20 20 20 20 20  oken *pDef;     
472ef 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
472f0 61 74 65 20 74 68 72 6f 75 67 68 20 64 65 66 65  ate through defe
472f1 72 72 65 64 20 74 6f 6b 65 6e 73 20 2a 2f 0d 0a  rred tokens */..
472f2 20 20 0d 0a 20 20 20 20 46 74 73 33 54 61 62 6c    ..    Fts3Tabl
472f3 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c  e *p = (Fts3Tabl
472f4 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70  e *)pCsr->base.p
472f5 56 74 61 62 3b 0d 0a 20 20 20 20 73 71 6c 69 74  Vtab;..    sqlit
472f6 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
472f7 20 3d 20 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72   = p->pTokenizer
472f8 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74  ;..    sqlite3_t
472f9 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
472fa 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
472fb 20 70 54 2d 3e 70 4d 6f 64 75 6c 65 3b 0d 0a 20   pT->pModule;.. 
472fc 20 20 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20    ..    assert( 
472fd 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72 65 53  pCsr->isRequireS
472fe 65 65 6b 3d 3d 30 20 29 3b 0d 0a 20 20 20 20 69  eek==0 );..    i
472ff 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Docid = sqlite3_
47300 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 43 73  column_int64(pCs
47301 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20  r->pStmt, 0);.. 
47302 20 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   ..    for(i=0; 
47303 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20  i<p->nColumn && 
47304 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
47305 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 63 6f 6e 73  ++){..      cons
47306 74 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20  t char *zText = 
47307 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
47308 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
47309 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69  t(pCsr->pStmt, i
4730a 2b 31 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69  +1);..      sqli
4730b 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
4730c 72 73 6f 72 20 2a 70 54 43 20 3d 20 30 3b 0d 0a  rsor *pTC = 0;..
4730d 20 20 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 70    ..      rc = p
4730e 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54  Module->xOpen(pT
4730f 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 26 70 54  , zText, -1, &pT
47310 43 29 3b 0d 0a 20 20 20 20 20 20 77 68 69 6c 65  C);..      while
47311 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
47312 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 68 61 72  ){..        char
47313 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 65 6e 3b 20   const *zToken; 
47314 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
47315 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b 65 6e  containing token
47316 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69 6e 74   */..        int
47317 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   nToken;        
47318 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
47319 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b   of bytes in tok
4731a 65 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 69  en */..        i
4731b 6e 74 20 69 44 75 6d 31 2c 20 69 44 75 6d 32 3b  nt iDum1, iDum2;
4731c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d           /* Dumm
4731d 79 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0d 0a  y variables */..
4731e 20 20 20 20 20 20 20 20 69 6e 74 20 69 50 6f 73          int iPos
4731f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47320 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 6f 66    /* Position of
47321 20 74 6f 6b 65 6e 20 69 6e 20 7a 54 65 78 74 20   token in zText 
47322 2a 2f 0d 0a 20 20 0d 0a 20 20 20 20 20 20 20 20  */..  ..        
47323 70 54 43 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  pTC->pTokenizer 
47324 3d 20 70 54 3b 0d 0a 20 20 20 20 20 20 20 20 72  = pT;..        r
47325 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  c = pModule->xNe
47326 78 74 28 70 54 43 2c 20 26 7a 54 6f 6b 65 6e 2c  xt(pTC, &zToken,
47327 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 44 75 6d 31   &nToken, &iDum1
47328 2c 20 26 69 44 75 6d 32 2c 20 26 69 50 6f 73 29  , &iDum2, &iPos)
47329 3b 0d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  ;..        for(p
4732a 44 65 66 3d 70 43 73 72 2d 3e 70 44 65 66 65 72  Def=pCsr->pDefer
4732b 72 65 64 3b 20 70 44 65 66 20 26 26 20 72 63 3d  red; pDef && rc=
4732c 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 44 65 66  =SQLITE_OK; pDef
4732d 3d 70 44 65 66 2d 3e 70 4e 65 78 74 29 7b 0d 0a  =pDef->pNext){..
4732e 20 20 20 20 20 20 20 20 20 20 46 74 73 33 50 68            Fts3Ph
4732f 72 61 73 65 54 6f 6b 65 6e 20 2a 70 50 54 20 3d  raseToken *pPT =
47330 20 70 44 65 66 2d 3e 70 54 6f 6b 65 6e 3b 0d 0a   pDef->pToken;..
47331 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70            if( (p
47332 44 65 66 2d 3e 69 43 6f 6c 3e 3d 70 2d 3e 6e 43  Def->iCol>=p->nC
47333 6f 6c 75 6d 6e 20 7c 7c 20 70 44 65 66 2d 3e 69  olumn || pDef->i
47334 43 6f 6c 3d 3d 69 29 0d 0a 20 20 20 20 20 20 20  Col==i)..       
47335 20 20 20 20 26 26 20 28 70 50 54 2d 3e 62 46 69      && (pPT->bFi
47336 72 73 74 3d 3d 30 20 7c 7c 20 69 50 6f 73 3d 3d  rst==0 || iPos==
47337 30 29 0d 0a 20 20 20 20 20 20 20 20 20 20 20 26  0)..           &
47338 26 20 28 70 50 54 2d 3e 6e 3d 3d 6e 54 6f 6b 65  & (pPT->n==nToke
47339 6e 20 7c 7c 20 28 70 50 54 2d 3e 69 73 50 72 65  n || (pPT->isPre
4733a 66 69 78 20 26 26 20 70 50 54 2d 3e 6e 3c 6e 54  fix && pPT->n<nT
4733b 6f 6b 65 6e 29 29 0d 0a 20 20 20 20 20 20 20 20  oken))..        
4733c 20 20 20 26 26 20 28 30 3d 3d 6d 65 6d 63 6d 70     && (0==memcmp
4733d 28 7a 54 6f 6b 65 6e 2c 20 70 50 54 2d 3e 7a 2c  (zToken, pPT->z,
4733e 20 70 50 54 2d 3e 6e 29 29 0d 0a 20 20 20 20 20   pPT->n))..     
4733f 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 20       ){..       
47340 20 20 20 20 20 66 74 73 33 50 65 6e 64 69 6e 67       fts3Pending
47341 4c 69 73 74 41 70 70 65 6e 64 28 26 70 44 65 66  ListAppend(&pDef
47342 2d 3e 70 4c 69 73 74 2c 20 69 44 6f 63 69 64 2c  ->pList, iDocid,
47343 20 69 2c 20 69 50 6f 73 2c 20 26 72 63 29 3b 0d   i, iPos, &rc);.
47344 0a 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  .          }..  
47345 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
47346 0d 0a 20 20 20 20 20 20 69 66 28 20 70 54 43 20  ..      if( pTC 
47347 29 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  ) pModule->xClos
47348 65 28 70 54 43 29 3b 0d 0a 20 20 20 20 20 20 69  e(pTC);..      i
47349 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
4734a 4e 45 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45  NE ) rc = SQLITE
4734b 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 0d  _OK;..    }..  .
4734c 0a 20 20 20 20 66 6f 72 28 70 44 65 66 3d 70 43  .    for(pDef=pC
4734d 73 72 2d 3e 70 44 65 66 65 72 72 65 64 3b 20 70  sr->pDeferred; p
4734e 44 65 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  Def && rc==SQLIT
4734f 45 5f 4f 4b 3b 20 70 44 65 66 3d 70 44 65 66 2d  E_OK; pDef=pDef-
47350 3e 70 4e 65 78 74 29 7b 0d 0a 20 20 20 20 20 20  >pNext){..      
47351 69 66 28 20 70 44 65 66 2d 3e 70 4c 69 73 74 20  if( pDef->pList 
47352 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){..        rc =
47353 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74   fts3PendingList
47354 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70 44  AppendVarint(&pD
47355 65 66 2d 3e 70 4c 69 73 74 2c 20 30 29 3b 0d 0a  ef->pList, 0);..
47356 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
47357 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20    }....  return 
47358 72 63 3b 0d 0a 7d 0d 0a 0d 0a 53 51 4c 49 54 45  rc;..}....SQLITE
47359 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4735a 69 74 65 33 46 74 73 33 44 65 66 65 72 72 65 64  ite3Fts3Deferred
4735b 54 6f 6b 65 6e 4c 69 73 74 28 0d 0a 20 20 46 74  TokenList(..  Ft
4735c 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 20  s3DeferredToken 
4735d 2a 70 2c 20 0d 0a 20 20 63 68 61 72 20 2a 2a 70  *p, ..  char **p
4735e 70 44 61 74 61 2c 20 0d 0a 20 20 69 6e 74 20 2a  pData, ..  int *
4735f 70 6e 44 61 74 61 0d 0a 29 7b 0d 0a 20 20 63 68  pnData..){..  ch
47360 61 72 20 2a 70 52 65 74 3b 0d 0a 20 20 69 6e 74  ar *pRet;..  int
47361 20 6e 53 6b 69 70 3b 0d 0a 20 20 73 71 6c 69 74   nSkip;..  sqlit
47362 65 33 5f 69 6e 74 36 34 20 64 75 6d 6d 79 3b 0d  e3_int64 dummy;.
47363 0a 0d 0a 20 20 2a 70 70 44 61 74 61 20 3d 20 30  ...  *ppData = 0
47364 3b 0d 0a 20 20 2a 70 6e 44 61 74 61 20 3d 20 30  ;..  *pnData = 0
47365 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 2d 3e 70 4c  ;....  if( p->pL
47366 69 73 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72  ist==0 ){..    r
47367 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
47368 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 52 65 74 20  ..  }....  pRet 
47369 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
4736a 33 5f 6d 61 6c 6c 6f 63 28 70 2d 3e 70 4c 69 73  3_malloc(p->pLis
4736b 74 2d 3e 6e 44 61 74 61 29 3b 0d 0a 20 20 69 66  t->nData);..  if
4736c 28 20 21 70 52 65 74 20 29 20 72 65 74 75 72 6e  ( !pRet ) return
4736d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
4736e 0d 0a 20 20 6e 53 6b 69 70 20 3d 20 73 71 6c 69  ..  nSkip = sqli
4736f 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
47370 28 70 2d 3e 70 4c 69 73 74 2d 3e 61 44 61 74 61  (p->pList->aData
47371 2c 20 26 64 75 6d 6d 79 29 3b 0d 0a 20 20 2a 70  , &dummy);..  *p
47372 6e 44 61 74 61 20 3d 20 70 2d 3e 70 4c 69 73 74  nData = p->pList
47373 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 6b 69 70 3b  ->nData - nSkip;
47374 0d 0a 20 20 2a 70 70 44 61 74 61 20 3d 20 70 52  ..  *ppData = pR
47375 65 74 3b 0d 0a 20 20 0d 0a 20 20 6d 65 6d 63 70  et;..  ..  memcp
47376 79 28 70 52 65 74 2c 20 26 70 2d 3e 70 4c 69 73  y(pRet, &p->pLis
47377 74 2d 3e 61 44 61 74 61 5b 6e 53 6b 69 70 5d 2c  t->aData[nSkip],
47378 20 2a 70 6e 44 61 74 61 29 3b 0d 0a 20 20 72 65   *pnData);..  re
47379 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
4737a 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 64  .}..../*..** Add
4737b 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 6f   an entry for to
4737c 6b 65 6e 20 70 54 6f 6b 65 6e 20 74 6f 20 74 68  ken pToken to th
4737d 65 20 70 43 73 72 2d 3e 70 44 65 66 65 72 72 65  e pCsr->pDeferre
4737e 64 20 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a 53 51 4c  d list...*/..SQL
4737f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
47380 73 71 6c 69 74 65 33 46 74 73 33 44 65 66 65 72  sqlite3Fts3Defer
47381 54 6f 6b 65 6e 28 0d 0a 20 20 46 74 73 33 43 75  Token(..  Fts3Cu
47382 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
47383 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74 73            /* Fts
47384 33 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 2a  3 table cursor *
47385 2f 0d 0a 20 20 46 74 73 33 50 68 72 61 73 65 54  /..  Fts3PhraseT
47386 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20  oken *pToken,   
47387 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 6f       /* Token to
47388 20 64 65 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74   defer */..  int
47389 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
4738a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4738b 43 6f 6c 75 6d 6e 20 74 68 61 74 20 74 6f 6b 65  Column that toke
4738c 6e 20 6d 75 73 74 20 61 70 70 65 61 72 20 69 6e  n must appear in
4738d 20 28 6f 72 20 2d 31 29 20 2a 2f 0d 0a 29 7b 0d   (or -1) */..){.
4738e 0a 20 20 46 74 73 33 44 65 66 65 72 72 65 64 54  .  Fts3DeferredT
4738f 6f 6b 65 6e 20 2a 70 44 65 66 65 72 72 65 64 3b  oken *pDeferred;
47390 0d 0a 20 20 70 44 65 66 65 72 72 65 64 20 3d 20  ..  pDeferred = 
47391 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
47392 69 7a 65 6f 66 28 2a 70 44 65 66 65 72 72 65 64  izeof(*pDeferred
47393 29 29 3b 0d 0a 20 20 69 66 28 20 21 70 44 65 66  ));..  if( !pDef
47394 65 72 72 65 64 20 29 7b 0d 0a 20 20 20 20 72 65  erred ){..    re
47395 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
47396 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 65  M;..  }..  memse
47397 74 28 70 44 65 66 65 72 72 65 64 2c 20 30 2c 20  t(pDeferred, 0, 
47398 73 69 7a 65 6f 66 28 2a 70 44 65 66 65 72 72 65  sizeof(*pDeferre
47399 64 29 29 3b 0d 0a 20 20 70 44 65 66 65 72 72 65  d));..  pDeferre
4739a 64 2d 3e 70 54 6f 6b 65 6e 20 3d 20 70 54 6f 6b  d->pToken = pTok
4739b 65 6e 3b 0d 0a 20 20 70 44 65 66 65 72 72 65 64  en;..  pDeferred
4739c 2d 3e 70 4e 65 78 74 20 3d 20 70 43 73 72 2d 3e  ->pNext = pCsr->
4739d 70 44 65 66 65 72 72 65 64 3b 20 0d 0a 20 20 70  pDeferred; ..  p
4739e 44 65 66 65 72 72 65 64 2d 3e 69 43 6f 6c 20 3d  Deferred->iCol =
4739f 20 69 43 6f 6c 3b 0d 0a 20 20 70 43 73 72 2d 3e   iCol;..  pCsr->
473a0 70 44 65 66 65 72 72 65 64 20 3d 20 70 44 65 66  pDeferred = pDef
473a1 65 72 72 65 64 3b 0d 0a 0d 0a 20 20 61 73 73 65  erred;....  asse
473a2 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 70 44 65 66  rt( pToken->pDef
473a3 65 72 72 65 64 3d 3d 30 20 29 3b 0d 0a 20 20 70  erred==0 );..  p
473a4 54 6f 6b 65 6e 2d 3e 70 44 65 66 65 72 72 65 64  Token->pDeferred
473a5 20 3d 20 70 44 65 66 65 72 72 65 64 3b 0d 0a 0d   = pDeferred;...
473a6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
473a7 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  _OK;..}..../*..*
473a8 2a 20 53 51 4c 69 74 65 20 76 61 6c 75 65 20 70  * SQLite value p
473a9 52 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73 20 74  Rowid contains t
473aa 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f  he rowid of a ro
473ab 77 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d 61  w that may or ma
473ac 79 20 6e 6f 74 20 62 65 0d 0a 2a 2a 20 70 72 65  y not be..** pre
473ad 73 65 6e 74 20 69 6e 20 74 68 65 20 46 54 53 33  sent in the FTS3
473ae 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73   table. If it is
473af 2c 20 64 65 6c 65 74 65 20 69 74 20 61 6e 64 20  , delete it and 
473b0 61 64 6a 75 73 74 20 74 68 65 20 63 6f 6e 74 65  adjust the conte
473b1 6e 74 73 0d 0a 2a 2a 20 6f 66 20 73 75 62 73 69  nts..** of subsi
473b2 64 75 61 72 79 20 64 61 74 61 20 73 74 72 75 63  duary data struc
473b3 74 75 72 65 73 20 61 63 63 6f 72 64 69 6e 67 6c  tures accordingl
473b4 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  y...*/..static i
473b5 6e 74 20 66 74 73 33 44 65 6c 65 74 65 42 79 52  nt fts3DeleteByR
473b6 6f 77 69 64 28 0d 0a 20 20 46 74 73 33 54 61 62  owid(..  Fts3Tab
473b7 6c 65 20 2a 70 2c 20 0d 0a 20 20 73 71 6c 69 74  le *p, ..  sqlit
473b8 65 33 5f 76 61 6c 75 65 20 2a 70 52 6f 77 69 64  e3_value *pRowid
473b9 2c 20 0d 0a 20 20 69 6e 74 20 2a 70 6e 44 6f 63  , ..  int *pnDoc
473ba 2c 0d 0a 20 20 75 33 32 20 2a 61 53 7a 44 65 6c  ,..  u32 *aSzDel
473bb 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 73 45 6d  ..){..  int isEm
473bc 70 74 79 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20  pty = 0;..  int 
473bd 72 63 20 3d 20 66 74 73 33 49 73 45 6d 70 74 79  rc = fts3IsEmpty
473be 28 70 2c 20 70 52 6f 77 69 64 2c 20 26 69 73 45  (p, pRowid, &isE
473bf 6d 70 74 79 29 3b 0d 0a 20 20 69 66 28 20 72 63  mpty);..  if( rc
473c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
473c1 20 20 20 20 69 66 28 20 69 73 45 6d 70 74 79 20      if( isEmpty 
473c2 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 44 65 6c  ){..      /* Del
473c3 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77 20 6d  eting this row m
473c4 65 61 6e 73 20 74 68 65 20 77 68 6f 6c 65 20 74  eans the whole t
473c5 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 49  able is empty. I
473c6 6e 20 74 68 69 73 20 63 61 73 65 0d 0a 20 20 20  n this case..   
473c7 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65     ** delete the
473c8 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c   contents of all
473c9 20 74 68 72 65 65 20 74 61 62 6c 65 73 20 61 6e   three tables an
473ca 64 20 74 68 72 6f 77 20 61 77 61 79 20 61 6e 79  d throw away any
473cb 0d 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20  ..      ** data 
473cc 69 6e 20 74 68 65 20 70 65 6e 64 69 6e 67 54 65  in the pendingTe
473cd 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 2e 20  rms hash table. 
473ce 20 2a 2f 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   */..      rc = 
473cf 66 74 73 33 44 65 6c 65 74 65 41 6c 6c 28 70 2c  fts3DeleteAll(p,
473d0 20 31 29 3b 0d 0a 20 20 20 20 20 20 2a 70 6e 44   1);..      *pnD
473d1 6f 63 20 3d 20 2a 70 6e 44 6f 63 20 2d 20 31 3b  oc = *pnDoc - 1;
473d2 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
473d3 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
473d4 34 20 69 52 65 6d 6f 76 65 20 3d 20 73 71 6c 69  4 iRemove = sqli
473d5 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
473d6 70 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 20 20  pRowid);..      
473d7 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67  rc = fts3Pending
473d8 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20 69 52  TermsDocid(p, iR
473d9 65 6d 6f 76 65 29 3b 0d 0a 20 20 20 20 20 20 66  emove);..      f
473da 74 73 33 44 65 6c 65 74 65 54 65 72 6d 73 28 26  ts3DeleteTerms(&
473db 72 63 2c 20 70 2c 20 70 52 6f 77 69 64 2c 20 61  rc, p, pRowid, a
473dc 53 7a 44 65 6c 29 3b 0d 0a 20 20 20 20 20 20 69  SzDel);..      i
473dd 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62  f( p->zContentTb
473de 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20  l==0 ){..       
473df 20 66 74 73 33 53 71 6c 45 78 65 63 28 26 72 63   fts3SqlExec(&rc
473e0 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f  , p, SQL_DELETE_
473e1 43 4f 4e 54 45 4e 54 2c 20 26 70 52 6f 77 69 64  CONTENT, &pRowid
473e2 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
473e3 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
473e4 70 2d 3e 64 62 29 20 29 20 2a 70 6e 44 6f 63 20  p->db) ) *pnDoc 
473e5 3d 20 2a 70 6e 44 6f 63 20 2d 20 31 3b 0d 0a 20  = *pnDoc - 1;.. 
473e6 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
473e7 20 20 20 20 20 2a 70 6e 44 6f 63 20 3d 20 2a 70       *pnDoc = *p
473e8 6e 44 6f 63 20 2d 20 31 3b 0d 0a 20 20 20 20 20  nDoc - 1;..     
473e9 20 7d 0d 0a 20 20 20 20 20 20 69 66 28 20 70 2d   }..      if( p-
473ea 3e 62 48 61 73 44 6f 63 73 69 7a 65 20 29 7b 0d  >bHasDocsize ){.
473eb 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 71 6c  .        fts3Sql
473ec 45 78 65 63 28 26 72 63 2c 20 70 2c 20 53 51 4c  Exec(&rc, p, SQL
473ed 5f 44 45 4c 45 54 45 5f 44 4f 43 53 49 5a 45 2c  _DELETE_DOCSIZE,
473ee 20 26 70 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20   &pRowid);..    
473ef 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
473f0 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
473f1 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69  .}..../*..** Thi
473f2 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  s function does 
473f3 74 68 65 20 77 6f 72 6b 20 66 6f 72 20 74 68 65  the work for the
473f4 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20   xUpdate method 
473f5 6f 66 20 46 54 53 33 20 76 69 72 74 75 61 6c 0d  of FTS3 virtual.
473f6 0a 2a 2a 20 74 61 62 6c 65 73 2e 0d 0a 2a 2f 0d  .** tables...*/.
473f7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
473f8 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 55  int sqlite3Fts3U
473f9 70 64 61 74 65 4d 65 74 68 6f 64 28 0d 0a 20 20  pdateMethod(..  
473fa 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
473fb 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tab,            
473fc 2f 2a 20 46 54 53 33 20 76 74 61 62 20 6f 62 6a  /* FTS3 vtab obj
473fd 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 41  ect */..  int nA
473fe 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rg,             
473ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
47400 65 20 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72  e of argument ar
47401 72 61 79 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ray */..  sqlite
47402 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 2c  3_value **apVal,
47403 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
47404 61 79 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  ay of arguments 
47405 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  */..  sqlite_int
47406 36 34 20 2a 70 52 6f 77 69 64 20 20 20 20 20 20  64 *pRowid      
47407 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 68        /* OUT: Th
47408 65 20 61 66 66 65 63 74 65 64 20 28 6f 72 20 65  e affected (or e
47409 66 66 65 63 74 65 64 29 20 72 6f 77 69 64 20 2a  ffected) rowid *
4740a 2f 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 54 61 62  /..){..  Fts3Tab
4740b 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62  le *p = (Fts3Tab
4740c 6c 65 20 2a 29 70 56 74 61 62 3b 0d 0a 20 20 69  le *)pVtab;..  i
4740d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
4740e 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
4740f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f  * Return Code */
47410 0d 0a 20 20 69 6e 74 20 69 73 52 65 6d 6f 76 65  ..  int isRemove
47411 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
47412 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
47413 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  an UPDATE or DEL
47414 45 54 45 20 2a 2f 0d 0a 20 20 75 33 32 20 2a 61  ETE */..  u32 *a
47415 53 7a 49 6e 73 20 3d 20 30 3b 20 20 20 20 20 20  SzIns = 0;      
47416 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
47417 65 73 20 6f 66 20 69 6e 73 65 72 74 65 64 20 64  es of inserted d
47418 6f 63 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20 75  ocuments */..  u
47419 33 32 20 2a 61 53 7a 44 65 6c 3b 20 20 20 20 20  32 *aSzDel;     
4741a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4741b 2a 20 53 69 7a 65 73 20 6f 66 20 64 65 6c 65 74  * Sizes of delet
4741c 65 64 20 64 6f 63 75 6d 65 6e 74 73 20 2a 2f 0d  ed documents */.
4741d 0a 20 20 69 6e 74 20 6e 43 68 6e 67 20 3d 20 30  .  int nChng = 0
4741e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4741f 20 20 20 2f 2a 20 4e 65 74 20 63 68 61 6e 67 65     /* Net change
47420 20 69 6e 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f   in number of do
47421 63 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20 20 69 6e  cuments */..  in
47422 74 20 62 49 6e 73 65 72 74 44 6f 6e 65 20 3d 20  t bInsertDone = 
47423 30 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  0;....  assert( 
47424 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20  p->pSegments==0 
47425 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b  );....  /* Check
47426 20 66 6f 72 20 61 20 22 73 70 65 63 69 61 6c 22   for a "special"
47427 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
47428 6e 2e 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f  n. One of the fo
47429 72 6d 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20  rm:..  **..  ** 
4742a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 79    INSERT INTO xy
4742b 7a 28 78 79 7a 29 20 56 41 4c 55 45 53 28 27 63  z(xyz) VALUES('c
4742c 6f 6d 6d 61 6e 64 27 29 3b 0d 0a 20 20 2a 2f 0d  ommand');..  */.
4742d 0a 20 20 69 66 28 20 6e 41 72 67 3e 31 20 0d 0a  .  if( nArg>1 ..
4742e 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f 76 61     && sqlite3_va
4742f 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b 30  lue_type(apVal[0
47430 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
47431 0d 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f  ..   && sqlite3_
47432 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c  value_type(apVal
47433 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 5d 29 21  [p->nColumn+2])!
47434 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 0d 0a 20  =SQLITE_NULL .. 
47435 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74   ){..    rc = ft
47436 73 33 53 70 65 63 69 61 6c 49 6e 73 65 72 74 28  s3SpecialInsert(
47437 70 2c 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c  p, apVal[p->nCol
47438 75 6d 6e 2b 32 5d 29 3b 0d 0a 20 20 20 20 67 6f  umn+2]);..    go
47439 74 6f 20 75 70 64 61 74 65 5f 6f 75 74 3b 0d 0a  to update_out;..
4743a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f    }....  /* Allo
4743b 63 61 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f  cate space to ho
4743c 6c 64 20 74 68 65 20 63 68 61 6e 67 65 20 69 6e  ld the change in
4743d 20 64 6f 63 75 6d 65 6e 74 20 73 69 7a 65 73 20   document sizes 
4743e 2a 2f 0d 0a 20 20 61 53 7a 49 6e 73 20 3d 20 73  */..  aSzIns = s
4743f 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
47440 69 7a 65 6f 66 28 61 53 7a 49 6e 73 5b 30 5d 29  izeof(aSzIns[0])
47441 2a 28 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 2a  *(p->nColumn+1)*
47442 32 20 29 3b 0d 0a 20 20 69 66 28 20 61 53 7a 49  2 );..  if( aSzI
47443 6e 73 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 72 63  ns==0 ){..    rc
47444 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
47445 0d 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74  ..    goto updat
47446 65 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 20 20 61  e_out;..  }..  a
47447 53 7a 44 65 6c 20 3d 20 26 61 53 7a 49 6e 73 5b  SzDel = &aSzIns[
47448 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0d 0a  p->nColumn+1];..
47449 20 20 6d 65 6d 73 65 74 28 61 53 7a 49 6e 73 2c    memset(aSzIns,
4744a 20 30 2c 20 73 69 7a 65 6f 66 28 61 53 7a 49 6e   0, sizeof(aSzIn
4744b 73 5b 30 5d 29 2a 28 70 2d 3e 6e 43 6f 6c 75 6d  s[0])*(p->nColum
4744c 6e 2b 31 29 2a 32 29 3b 0d 0a 0d 0a 20 20 2f 2a  n+1)*2);....  /*
4744d 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
4744e 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2c  NSERT operation,
4744f 20 6f 72 20 61 6e 20 55 50 44 41 54 45 20 74 68   or an UPDATE th
47450 61 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  at modifies the 
47451 72 6f 77 69 64 0d 0a 20 20 2a 2a 20 76 61 6c 75  rowid..  ** valu
47452 65 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65  e, then this ope
47453 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  ration requires 
47454 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 6e 64 6c  constraint handl
47455 69 6e 67 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a  ing...  **..  **
47456 20 49 66 20 74 68 65 20 6f 6e 2d 63 6f 6e 66 6c   If the on-confl
47457 69 63 74 20 6d 6f 64 65 20 69 73 20 52 45 50 4c  ict mode is REPL
47458 41 43 45 2c 20 74 68 69 73 20 6d 65 61 6e 73 20  ACE, this means 
47459 74 68 61 74 20 74 68 65 20 65 78 69 73 74 69 6e  that the existin
4745a 67 20 72 6f 77 0d 0a 20 20 2a 2a 20 73 68 6f 75  g row..  ** shou
4745b 6c 64 20 62 65 20 64 65 6c 65 74 65 64 20 66 72  ld be deleted fr
4745c 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
4745d 62 65 66 6f 72 65 20 69 6e 73 65 72 74 69 6e 67  before inserting
4745e 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20 4f 72   the new row. Or
4745f 2c 0d 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 6f  ,..  ** if the o
47460 6e 2d 63 6f 6e 66 6c 69 63 74 20 6d 6f 64 65 20  n-conflict mode 
47461 69 73 20 6f 74 68 65 72 20 74 68 61 6e 20 52 45  is other than RE
47462 50 4c 41 43 45 2c 20 74 68 65 6e 20 74 68 69 73  PLACE, then this
47463 20 6d 65 74 68 6f 64 20 6d 75 73 74 0d 0a 20 20   method must..  
47464 2a 2a 20 64 65 74 65 63 74 20 74 68 65 20 63 6f  ** detect the co
47465 6e 66 6c 69 63 74 20 61 6e 64 20 72 65 74 75 72  nflict and retur
47466 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  n SQLITE_CONSTRA
47467 49 4e 54 20 62 65 66 6f 72 65 20 62 65 67 69 6e  INT before begin
47468 6e 69 6e 67 20 74 6f 0d 0a 20 20 2a 2a 20 6d 6f  ning to..  ** mo
47469 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73  dify the databas
4746a 65 20 66 69 6c 65 2e 0d 0a 20 20 2a 2f 0d 0a 20  e file...  */.. 
4746b 20 69 66 28 20 6e 41 72 67 3e 31 20 26 26 20 70   if( nArg>1 && p
4746c 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d 3d 30  ->zContentTbl==0
4746d 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 46 69 6e 64   ){..    /* Find
4746e 20 74 68 65 20 76 61 6c 75 65 20 6f 62 6a 65 63   the value objec
4746f 74 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  t that holds the
47470 20 6e 65 77 20 72 6f 77 69 64 20 76 61 6c 75 65   new rowid value
47471 2e 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69 74 65  . */..    sqlite
47472 33 5f 76 61 6c 75 65 20 2a 70 4e 65 77 52 6f 77  3_value *pNewRow
47473 69 64 20 3d 20 61 70 56 61 6c 5b 33 2b 70 2d 3e  id = apVal[3+p->
47474 6e 43 6f 6c 75 6d 6e 5d 3b 0d 0a 20 20 20 20 69  nColumn];..    i
47475 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
47476 5f 74 79 70 65 28 70 4e 65 77 52 6f 77 69 64 29  _type(pNewRowid)
47477 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b  ==SQLITE_NULL ){
47478 0d 0a 20 20 20 20 20 20 70 4e 65 77 52 6f 77 69  ..      pNewRowi
47479 64 20 3d 20 61 70 56 61 6c 5b 31 5d 3b 0d 0a 20  d = apVal[1];.. 
4747a 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20     }....    if( 
4747b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
4747c 70 65 28 70 4e 65 77 52 6f 77 69 64 29 21 3d 53  pe(pNewRowid)!=S
4747d 51 4c 49 54 45 5f 4e 55 4c 4c 20 26 26 20 28 20  QLITE_NULL && ( 
4747e 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
4747f 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56  3_value_type(apV
47480 61 6c 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  al[0])==SQLITE_N
47481 55 4c 4c 0d 0a 20 20 20 20 20 7c 7c 20 73 71 6c  ULL..     || sql
47482 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
47483 28 61 70 56 61 6c 5b 30 5d 29 21 3d 73 71 6c 69  (apVal[0])!=sqli
47484 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
47485 70 4e 65 77 52 6f 77 69 64 29 0d 0a 20 20 20 20  pNewRowid)..    
47486 29 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68  )){..      /* Th
47487 65 20 6e 65 77 20 72 6f 77 69 64 20 69 73 20 6e  e new rowid is n
47488 6f 74 20 4e 55 4c 4c 20 28 69 6e 20 74 68 69 73  ot NULL (in this
47489 20 63 61 73 65 20 74 68 65 20 72 6f 77 69 64 20   case the rowid 
4748a 77 69 6c 6c 20 62 65 0d 0a 20 20 20 20 20 20 2a  will be..      *
4748b 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
4748c 61 73 73 69 67 6e 65 64 20 61 6e 64 20 74 68 65  assigned and the
4748d 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20  re is no chance 
4748e 6f 66 20 61 20 63 6f 6e 66 6c 69 63 74 29 2c 20  of a conflict), 
4748f 61 6e 64 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74  and ..      ** t
47490 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
47491 65 69 74 68 65 72 20 61 6e 20 49 4e 53 45 52 54  either an INSERT
47492 20 6f 72 20 61 6e 20 55 50 44 41 54 45 20 74 68   or an UPDATE th
47493 61 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0d  at modifies the.
47494 0a 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20  .      ** rowid 
47495 63 6f 6c 75 6d 6e 2e 20 53 6f 20 69 66 20 74 68  column. So if th
47496 65 20 63 6f 6e 66 6c 69 63 74 20 6d 6f 64 65 20  e conflict mode 
47497 69 73 20 52 45 50 4c 41 43 45 2c 20 74 68 65 6e  is REPLACE, then
47498 20 64 65 6c 65 74 65 20 61 6e 79 0d 0a 20 20 20   delete any..   
47499 20 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 72     ** existing r
4749a 6f 77 20 77 69 74 68 20 72 6f 77 69 64 3d 70 4e  ow with rowid=pN
4749b 65 77 52 6f 77 69 64 2e 20 0d 0a 20 20 20 20 20  ewRowid. ..     
4749c 20 2a 2a 0d 0a 20 20 20 20 20 20 2a 2a 20 4f 72   **..      ** Or
4749d 2c 20 69 66 20 74 68 65 20 63 6f 6e 66 6c 69 63  , if the conflic
4749e 74 20 6d 6f 64 65 20 69 73 20 6e 6f 74 20 52 45  t mode is not RE
4749f 50 4c 41 43 45 2c 20 69 6e 73 65 72 74 20 74 68  PLACE, insert th
474a0 65 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74  e new record int
474a1 6f 20 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  o ..      ** the
474a2 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
474a3 2e 20 49 66 20 77 65 20 68 69 74 20 74 68 65 20  . If we hit the 
474a4 64 75 70 6c 69 63 61 74 65 20 72 6f 77 69 64 20  duplicate rowid 
474a5 63 6f 6e 73 74 72 61 69 6e 74 20 28 6f 72 20 61  constraint (or a
474a6 6e 79 0d 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  ny..      ** oth
474a7 65 72 20 65 72 72 6f 72 29 20 77 68 69 6c 65 20  er error) while 
474a8 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e  doing so, return
474a9 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 20   immediately... 
474aa 20 20 20 20 20 2a 2a 0d 0a 20 20 20 20 20 20 2a       **..      *
474ab 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61  * This branch ma
474ac 79 20 61 6c 73 6f 20 72 75 6e 20 69 66 20 70 4e  y also run if pN
474ad 65 77 52 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73  ewRowid contains
474ae 20 61 20 76 61 6c 75 65 20 74 68 61 74 20 63 61   a value that ca
474af 6e 6e 6f 74 0d 0a 20 20 20 20 20 20 2a 2a 20 62  nnot..      ** b
474b0 65 20 6c 6f 73 73 6c 65 73 73 6c 79 20 63 6f 6e  e losslessly con
474b1 76 65 72 74 65 64 20 74 6f 20 61 6e 20 69 6e 74  verted to an int
474b2 65 67 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61  eger. In this ca
474b3 73 65 2c 20 74 68 65 20 65 76 65 6e 74 75 61 6c  se, the eventual
474b4 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c   ..      ** call
474b5 20 74 6f 20 66 74 73 33 49 6e 73 65 72 74 44 61   to fts3InsertDa
474b6 74 61 28 29 20 28 65 69 74 68 65 72 20 6a 75 73  ta() (either jus
474b7 74 20 62 65 6c 6f 77 20 6f 72 20 66 75 72 74 68  t below or furth
474b8 65 72 20 6f 6e 20 69 6e 20 74 68 69 73 0d 0a 20  er on in this.. 
474b9 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
474ba 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  ) will return SQ
474bb 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 2e 20 49  LITE_MISMATCH. I
474bc 66 20 66 74 73 33 44 65 6c 65 74 65 42 79 52 6f  f fts3DeleteByRo
474bd 77 69 64 20 69 73 20 0d 0a 20 20 20 20 20 20 2a  wid is ..      *
474be 2a 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20 77 69  * invoked, it wi
474bf 6c 6c 20 64 65 6c 65 74 65 20 7a 65 72 6f 20 72  ll delete zero r
474c0 6f 77 73 20 28 73 69 6e 63 65 20 6e 6f 20 72 6f  ows (since no ro
474c1 77 20 77 69 6c 6c 20 68 61 76 65 0d 0a 20 20 20  w will have..   
474c2 20 20 20 2a 2a 20 64 6f 63 69 64 3d 24 70 4e 65     ** docid=$pNe
474c3 77 52 6f 77 69 64 20 69 66 20 24 70 4e 65 77 52  wRowid if $pNewR
474c4 6f 77 69 64 20 69 73 20 6e 6f 74 20 61 6e 20 69  owid is not an i
474c5 6e 74 65 67 65 72 20 76 61 6c 75 65 29 2e 0d 0a  nteger value)...
474c6 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20 20        */..      
474c7 69 66 28 20 73 71 6c 69 74 65 33 5f 76 74 61 62  if( sqlite3_vtab
474c8 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 70 2d 3e  _on_conflict(p->
474c9 64 62 29 3d 3d 53 51 4c 49 54 45 5f 52 45 50 4c  db)==SQLITE_REPL
474ca 41 43 45 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ACE ){..        
474cb 72 63 20 3d 20 66 74 73 33 44 65 6c 65 74 65 42  rc = fts3DeleteB
474cc 79 52 6f 77 69 64 28 70 2c 20 70 4e 65 77 52 6f  yRowid(p, pNewRo
474cd 77 69 64 2c 20 26 6e 43 68 6e 67 2c 20 61 53 7a  wid, &nChng, aSz
474ce 44 65 6c 29 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  Del);..      }el
474cf 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  se{..        rc 
474d0 3d 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61  = fts3InsertData
474d1 28 70 2c 20 61 70 56 61 6c 2c 20 70 52 6f 77 69  (p, apVal, pRowi
474d2 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 62 49 6e  d);..        bIn
474d3 73 65 72 74 44 6f 6e 65 20 3d 20 31 3b 0d 0a 20  sertDone = 1;.. 
474d4 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
474d5 20 7d 0d 0a 20 20 69 66 28 20 72 63 21 3d 53 51   }..  if( rc!=SQ
474d6 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
474d7 67 6f 74 6f 20 75 70 64 61 74 65 5f 6f 75 74 3b  goto update_out;
474d8 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66  ..  }....  /* If
474d9 20 74 68 69 73 20 69 73 20 61 20 44 45 4c 45 54   this is a DELET
474da 45 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72  E or UPDATE oper
474db 61 74 69 6f 6e 2c 20 72 65 6d 6f 76 65 20 74 68  ation, remove th
474dc 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20 2a 2f  e old record. */
474dd 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
474de 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c  value_type(apVal
474df 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])!=SQLITE_NUL
474e0 4c 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  L ){..    assert
474e1 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
474e2 74 79 70 65 28 61 70 56 61 6c 5b 30 5d 29 3d 3d  type(apVal[0])==
474e3 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29  SQLITE_INTEGER )
474e4 3b 0d 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  ;..    rc = fts3
474e5 44 65 6c 65 74 65 42 79 52 6f 77 69 64 28 70 2c  DeleteByRowid(p,
474e6 20 61 70 56 61 6c 5b 30 5d 2c 20 26 6e 43 68 6e   apVal[0], &nChn
474e7 67 2c 20 61 53 7a 44 65 6c 29 3b 0d 0a 20 20 20  g, aSzDel);..   
474e8 20 69 73 52 65 6d 6f 76 65 20 3d 20 31 3b 0d 0a   isRemove = 1;..
474e9 20 20 7d 0d 0a 20 20 0d 0a 20 20 2f 2a 20 49 66    }..  ..  /* If
474ea 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45   this is an INSE
474eb 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65  RT or UPDATE ope
474ec 72 61 74 69 6f 6e 2c 20 69 6e 73 65 72 74 20 74  ration, insert t
474ed 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 2a  he new record. *
474ee 2f 0d 0a 20 20 69 66 28 20 6e 41 72 67 3e 31 20  /..  if( nArg>1 
474ef 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
474f0 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 62 49 6e   ){..    if( bIn
474f1 73 65 72 74 44 6f 6e 65 3d 3d 30 20 29 7b 0d 0a  sertDone==0 ){..
474f2 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 49        rc = fts3I
474f3 6e 73 65 72 74 44 61 74 61 28 70 2c 20 61 70 56  nsertData(p, apV
474f4 61 6c 2c 20 70 52 6f 77 69 64 29 3b 0d 0a 20 20  al, pRowid);..  
474f5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
474f6 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26  TE_CONSTRAINT &&
474f7 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d   p->zContentTbl=
474f8 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72  =0 ){..        r
474f9 63 20 3d 20 46 54 53 5f 43 4f 52 52 55 50 54 5f  c = FTS_CORRUPT_
474fa 56 54 41 42 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  VTAB;..      }..
474fb 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72      }..    if( r
474fc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
474fd 28 21 69 73 52 65 6d 6f 76 65 20 7c 7c 20 2a 70  (!isRemove || *p
474fe 52 6f 77 69 64 21 3d 70 2d 3e 69 50 72 65 76 44  Rowid!=p->iPrevD
474ff 6f 63 69 64 20 29 20 29 7b 0d 0a 20 20 20 20 20  ocid ) ){..     
47500 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e   rc = fts3Pendin
47501 67 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20 2a  gTermsDocid(p, *
47502 70 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20 7d 0d  pRowid);..    }.
47503 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
47504 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
47505 20 61 73 73 65 72 74 28 20 70 2d 3e 69 50 72 65   assert( p->iPre
47506 76 44 6f 63 69 64 3d 3d 2a 70 52 6f 77 69 64 20  vDocid==*pRowid 
47507 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66  );..      rc = f
47508 74 73 33 49 6e 73 65 72 74 54 65 72 6d 73 28 70  ts3InsertTerms(p
47509 2c 20 61 70 56 61 6c 2c 20 61 53 7a 49 6e 73 29  , apVal, aSzIns)
4750a 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66  ;..    }..    if
4750b 28 20 70 2d 3e 62 48 61 73 44 6f 63 73 69 7a 65  ( p->bHasDocsize
4750c 20 29 7b 0d 0a 20 20 20 20 20 20 66 74 73 33 49   ){..      fts3I
4750d 6e 73 65 72 74 44 6f 63 73 69 7a 65 28 26 72 63  nsertDocsize(&rc
4750e 2c 20 70 2c 20 61 53 7a 49 6e 73 29 3b 0d 0a 20  , p, aSzIns);.. 
4750f 20 20 20 7d 0d 0a 20 20 20 20 6e 43 68 6e 67 2b     }..    nChng+
47510 2b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  +;..  }....  if(
47511 20 70 2d 3e 62 48 61 73 53 74 61 74 20 29 7b 0d   p->bHasStat ){.
47512 0a 20 20 20 20 66 74 73 33 55 70 64 61 74 65 44  .    fts3UpdateD
47513 6f 63 54 6f 74 61 6c 73 28 26 72 63 2c 20 70 2c  ocTotals(&rc, p,
47514 20 61 53 7a 49 6e 73 2c 20 61 53 7a 44 65 6c 2c   aSzIns, aSzDel,
47515 20 6e 43 68 6e 67 29 3b 0d 0a 20 20 7d 0d 0a 0d   nChng);..  }...
47516 0a 20 75 70 64 61 74 65 5f 6f 75 74 3a 0d 0a 20  . update_out:.. 
47517 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 53   sqlite3_free(aS
47518 7a 49 6e 73 29 3b 0d 0a 20 20 73 71 6c 69 74 65  zIns);..  sqlite
47519 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f  3Fts3SegmentsClo
4751a 73 65 28 70 29 3b 0d 0a 20 20 72 65 74 75 72 6e  se(p);..  return
4751b 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a   rc;..}..../* ..
4751c 2a 2a 20 46 6c 75 73 68 20 61 6e 79 20 64 61 74  ** Flush any dat
4751d 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69 6e 67  a in the pending
4751e 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c  -terms hash tabl
4751f 65 20 74 6f 20 64 69 73 6b 2e 20 49 66 20 73 75  e to disk. If su
47520 63 63 65 73 73 66 75 6c 2c 0d 0a 2a 2a 20 6d 65  ccessful,..** me
47521 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  rge all segments
47522 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
47523 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20   (including the 
47524 6e 65 77 20 73 65 67 6d 65 6e 74 2c 20 69 66 20  new segment, if 
47525 0d 0a 2a 2a 20 74 68 65 72 65 20 77 61 73 20 61  ..** there was a
47526 6e 79 20 64 61 74 61 20 74 6f 20 66 6c 75 73 68  ny data to flush
47527 29 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  ) into a single 
47528 73 65 67 6d 65 6e 74 2e 20 0d 0a 2a 2f 0d 0a 53  segment. ..*/..S
47529 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4752a 74 20 73 71 6c 69 74 65 33 46 74 73 33 4f 70 74  t sqlite3Fts3Opt
4752b 69 6d 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20  imize(Fts3Table 
4752c 2a 70 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d  *p){..  int rc;.
4752d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
4752e 65 78 65 63 28 70 2d 3e 64 62 2c 20 22 53 41 56  exec(p->db, "SAV
4752f 45 50 4f 49 4e 54 20 66 74 73 33 22 2c 20 30 2c  EPOINT fts3", 0,
47530 20 30 2c 20 30 29 3b 0d 0a 20 20 69 66 28 20 72   0, 0);..  if( r
47531 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
47532 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 44 6f  .    rc = fts3Do
47533 4f 70 74 69 6d 69 7a 65 28 70 2c 20 31 29 3b 0d  Optimize(p, 1);.
47534 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
47535 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51  ITE_OK || rc==SQ
47536 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20  LITE_DONE ){..  
47537 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73 71      int rc2 = sq
47538 6c 69 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62  lite3_exec(p->db
47539 2c 20 22 52 45 4c 45 41 53 45 20 66 74 73 33 22  , "RELEASE fts3"
4753a 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  , 0, 0, 0);..   
4753b 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49     if( rc2!=SQLI
4753c 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32  TE_OK ) rc = rc2
4753d 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
4753e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65       sqlite3_exe
4753f 63 28 70 2d 3e 64 62 2c 20 22 52 4f 4c 4c 42 41  c(p->db, "ROLLBA
47540 43 4b 20 54 4f 20 66 74 73 33 22 2c 20 30 2c 20  CK TO fts3", 0, 
47541 30 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 73 71  0, 0);..      sq
47542 6c 69 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62  lite3_exec(p->db
47543 2c 20 22 52 45 4c 45 41 53 45 20 66 74 73 33 22  , "RELEASE fts3"
47544 2c 20 30 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20  , 0, 0, 0);..   
47545 20 7d 0d 0a 20 20 7d 0d 0a 20 20 73 71 6c 69 74   }..  }..  sqlit
47546 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43 6c  e3Fts3SegmentsCl
47547 6f 73 65 28 70 29 3b 0d 0a 20 20 72 65 74 75 72  ose(p);..  retur
47548 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 65 6e 64  n rc;..}....#end
47549 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  if..../*********
4754a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
4754b 33 5f 77 72 69 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  3_write.c ******
4754c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4754d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4754e 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
4754f 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
47550 65 20 66 74 73 33 5f 73 6e 69 70 70 65 74 2e 63  e fts3_snippet.c
47551 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
47552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47553 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a 20 32  *****/../*..** 2
47554 30 30 39 20 4f 63 74 20 32 33 0d 0a 2a 2a 0d 0a  009 Oct 23..**..
47555 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
47556 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
47557 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
47558 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
47559 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67 61 6c 20   of..** a legal 
4755a 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
4755b 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d  a blessing:..**.
4755c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
4755d 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
4755e 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20  vil...**    May 
4755f 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
47560 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
47561 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
47562 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79  hers...**    May
47563 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
47564 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
47565 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
47566 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a  ve...**..*******
47567 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4756a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4756b 2a 2a 2a 2a 2a 2a 2a 0d 0a 2a 2f 0d 0a 0d 0a 23  *******..*/....#
4756c 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
4756d 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
4756e 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
4756f 45 5f 46 54 53 33 29 0d 0a 0d 0a 2f 2a 20 23 69  E_FTS3)..../* #i
47570 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68  nclude <string.h
47571 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64  > */../* #includ
47572 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d  e <assert.h> */.
47573 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 68 61 72 61 63  .../*..** Charac
47574 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 61 70  ters that may ap
47575 70 65 61 72 20 69 6e 20 74 68 65 20 73 65 63 6f  pear in the seco
47576 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6d  nd argument to m
47577 61 74 63 68 69 6e 66 6f 28 29 2e 0d 0a 2a 2f 0d  atchinfo()...*/.
47578 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41  .#define FTS3_MA
47579 54 43 48 49 4e 46 4f 5f 4e 50 48 52 41 53 45 20  TCHINFO_NPHRASE 
4757a 20 20 27 70 27 20 20 20 20 20 20 20 20 2f 2a 20    'p'        /* 
4757b 31 20 76 61 6c 75 65 20 2a 2f 0d 0a 23 64 65 66  1 value */..#def
4757c 69 6e 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e  ine FTS3_MATCHIN
4757d 46 4f 5f 4e 43 4f 4c 20 20 20 20 20 20 27 63 27  FO_NCOL      'c'
4757e 20 20 20 20 20 20 20 20 2f 2a 20 31 20 76 61 6c          /* 1 val
4757f 75 65 20 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 46  ue */..#define F
47580 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 44  TS3_MATCHINFO_ND
47581 4f 43 20 20 20 20 20 20 27 6e 27 20 20 20 20 20  OC      'n'     
47582 20 20 20 2f 2a 20 31 20 76 61 6c 75 65 20 2a 2f     /* 1 value */
47583 0d 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d  ..#define FTS3_M
47584 41 54 43 48 49 4e 46 4f 5f 41 56 47 4c 45 4e 47  ATCHINFO_AVGLENG
47585 54 48 20 27 61 27 20 20 20 20 20 20 20 20 2f 2a  TH 'a'        /*
47586 20 6e 43 6f 6c 20 76 61 6c 75 65 73 20 2a 2f 0d   nCol values */.
47587 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41  .#define FTS3_MA
47588 54 43 48 49 4e 46 4f 5f 4c 45 4e 47 54 48 20 20  TCHINFO_LENGTH  
47589 20 20 27 6c 27 20 20 20 20 20 20 20 20 2f 2a 20    'l'        /* 
4758a 6e 43 6f 6c 20 76 61 6c 75 65 73 20 2a 2f 0d 0a  nCol values */..
4758b 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41 54  #define FTS3_MAT
4758c 43 48 49 4e 46 4f 5f 4c 43 53 20 20 20 20 20 20  CHINFO_LCS      
4758d 20 27 73 27 20 20 20 20 20 20 20 20 2f 2a 20 6e   's'        /* n
4758e 43 6f 6c 20 76 61 6c 75 65 73 20 2a 2f 0d 0a 23  Col values */..#
4758f 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41 54 43  define FTS3_MATC
47590 48 49 4e 46 4f 5f 48 49 54 53 20 20 20 20 20 20  HINFO_HITS      
47591 27 78 27 20 20 20 20 20 20 20 20 2f 2a 20 33 2a  'x'        /* 3*
47592 6e 43 6f 6c 2a 6e 50 68 72 61 73 65 20 76 61 6c  nCol*nPhrase val
47593 75 65 73 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  ues */..../*..**
47594 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
47595 75 65 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  ue for the secon
47596 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6d 61  d argument to ma
47597 74 63 68 69 6e 66 6f 28 29 2e 20 0d 0a 2a 2f 0d  tchinfo(). ..*/.
47598 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41  .#define FTS3_MA
47599 54 43 48 49 4e 46 4f 5f 44 45 46 41 55 4c 54 20  TCHINFO_DEFAULT 
4759a 20 20 22 70 63 78 22 0d 0a 0d 0a 0d 0a 2f 2a 0d    "pcx"....../*.
4759b 0a 2a 2a 20 55 73 65 64 20 61 73 20 61 6e 20 66  .** Used as an f
4759c 74 73 33 45 78 70 72 49 74 65 72 61 74 65 28 29  ts3ExprIterate()
4759d 20 63 6f 6e 74 65 78 74 20 77 68 65 6e 20 6c 6f   context when lo
4759e 61 64 69 6e 67 20 70 68 72 61 73 65 20 64 6f 63  ading phrase doc
4759f 6c 69 73 74 73 20 74 6f 0d 0a 2a 2a 20 46 74 73  lists to..** Fts
475a0 33 45 78 70 72 2e 61 44 6f 63 6c 69 73 74 5b 5d  3Expr.aDoclist[]
475a1 2f 6e 44 6f 63 6c 69 73 74 2e 0d 0a 2a 2f 0d 0a  /nDoclist...*/..
475a2 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
475a3 6f 61 64 44 6f 63 6c 69 73 74 43 74 78 20 4c 6f  oadDoclistCtx Lo
475a4 61 64 44 6f 63 6c 69 73 74 43 74 78 3b 0d 0a 73  adDoclistCtx;..s
475a5 74 72 75 63 74 20 4c 6f 61 64 44 6f 63 6c 69 73  truct LoadDoclis
475a6 74 43 74 78 20 7b 0d 0a 20 20 46 74 73 33 43 75  tCtx {..  Fts3Cu
475a7 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20  rsor *pCsr;     
475a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
475a9 33 20 43 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 69  3 Cursor */..  i
475aa 6e 74 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20  nt nPhrase;     
475ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
475ac 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 68 72 61  * Number of phra
475ad 73 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20  ses seen so far 
475ae 2a 2f 0d 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e  */..  int nToken
475af 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
475b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
475b1 6f 66 20 74 6f 6b 65 6e 73 20 73 65 65 6e 20 73  of tokens seen s
475b2 6f 20 66 61 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a  o far */..};....
475b3 2f 2a 0d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  /*..** The follo
475b4 77 69 6e 67 20 74 79 70 65 73 20 61 72 65 20 75  wing types are u
475b5 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  sed as part of t
475b6 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
475b7 6e 20 6f 66 20 74 68 65 20 0d 0a 2a 2a 20 66 74  n of the ..** ft
475b8 73 33 42 65 73 74 53 6e 69 70 70 65 74 28 29 20  s3BestSnippet() 
475b9 72 6f 75 74 69 6e 65 2e 0d 0a 2a 2f 0d 0a 74 79  routine...*/..ty
475ba 70 65 64 65 66 20 73 74 72 75 63 74 20 53 6e 69  pedef struct Sni
475bb 70 70 65 74 49 74 65 72 20 53 6e 69 70 70 65 74  ppetIter Snippet
475bc 49 74 65 72 3b 0d 0a 74 79 70 65 64 65 66 20 73  Iter;..typedef s
475bd 74 72 75 63 74 20 53 6e 69 70 70 65 74 50 68 72  truct SnippetPhr
475be 61 73 65 20 53 6e 69 70 70 65 74 50 68 72 61 73  ase SnippetPhras
475bf 65 3b 0d 0a 74 79 70 65 64 65 66 20 73 74 72 75  e;..typedef stru
475c0 63 74 20 53 6e 69 70 70 65 74 46 72 61 67 6d 65  ct SnippetFragme
475c1 6e 74 20 53 6e 69 70 70 65 74 46 72 61 67 6d 65  nt SnippetFragme
475c2 6e 74 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 53 6e  nt;....struct Sn
475c3 69 70 70 65 74 49 74 65 72 20 7b 0d 0a 20 20 46  ippetIter {..  F
475c4 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 3b  ts3Cursor *pCsr;
475c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
475c6 2a 20 43 75 72 73 6f 72 20 73 6e 69 70 70 65 74  * Cursor snippet
475c7 20 69 73 20 62 65 69 6e 67 20 67 65 6e 65 72 61   is being genera
475c8 74 65 64 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69  ted from */..  i
475c9 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
475ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
475cb 2a 20 45 78 74 72 61 63 74 20 73 6e 69 70 70 65  * Extract snippe
475cc 74 20 66 72 6f 6d 20 74 68 69 73 20 63 6f 6c 75  t from this colu
475cd 6d 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 53 6e  mn */..  int nSn
475ce 69 70 70 65 74 3b 20 20 20 20 20 20 20 20 20 20  ippet;          
475cf 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75           /* Requ
475d0 65 73 74 65 64 20 73 6e 69 70 70 65 74 20 6c 65  ested snippet le
475d1 6e 67 74 68 20 28 69 6e 20 74 6f 6b 65 6e 73 29  ngth (in tokens)
475d2 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 50 68 72 61   */..  int nPhra
475d3 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
475d4 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
475d5 20 6f 66 20 70 68 72 61 73 65 73 20 69 6e 20 71   of phrases in q
475d6 75 65 72 79 20 2a 2f 0d 0a 20 20 53 6e 69 70 70  uery */..  Snipp
475d7 65 74 50 68 72 61 73 65 20 2a 61 50 68 72 61 73  etPhrase *aPhras
475d8 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72  e;         /* Ar
475d9 72 61 79 20 6f 66 20 73 69 7a 65 20 6e 50 68 72  ray of size nPhr
475da 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43  ase */..  int iC
475db 75 72 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20  urrent;         
475dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
475dd 73 74 20 74 6f 6b 65 6e 20 6f 66 20 63 75 72 72  st token of curr
475de 65 6e 74 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a  ent snippet */..
475df 7d 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 53 6e 69  };....struct Sni
475e0 70 70 65 74 50 68 72 61 73 65 20 7b 0d 0a 20 20  ppetPhrase {..  
475e1 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20  int nToken;     
475e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475e3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b  /* Number of tok
475e4 65 6e 73 20 69 6e 20 70 68 72 61 73 65 20 2a 2f  ens in phrase */
475e5 0d 0a 20 20 63 68 61 72 20 2a 70 4c 69 73 74 3b  ..  char *pList;
475e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475e7 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
475e8 6f 20 73 74 61 72 74 20 6f 66 20 70 68 72 61 73  o start of phras
475e9 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20  e position list 
475ea 2a 2f 0d 0a 20 20 69 6e 74 20 69 48 65 61 64 3b  */..  int iHead;
475eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475ec 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 76 61        /* Next va
475ed 6c 75 65 20 69 6e 20 70 6f 73 69 74 69 6f 6e 20  lue in position 
475ee 6c 69 73 74 20 2a 2f 0d 0a 20 20 63 68 61 72 20  list */..  char 
475ef 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 20 20  *pHead;         
475f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
475f1 73 69 74 69 6f 6e 20 6c 69 73 74 20 64 61 74 61  sition list data
475f2 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 48 65 61 64   following iHead
475f3 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 54 61 69 6c   */..  int iTail
475f4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
475f5 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 76         /* Next v
475f6 61 6c 75 65 20 69 6e 20 74 72 61 69 6c 69 6e 67  alue in trailing
475f7 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a   position list *
475f8 2f 0d 0a 20 20 63 68 61 72 20 2a 70 54 61 69 6c  /..  char *pTail
475f9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
475fa 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
475fb 20 6c 69 73 74 20 64 61 74 61 20 66 6f 6c 6c 6f   list data follo
475fc 77 69 6e 67 20 69 54 61 69 6c 20 2a 2f 0d 0a 7d  wing iTail */..}
475fd 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 53 6e 69 70  ;....struct Snip
475fe 70 65 74 46 72 61 67 6d 65 6e 74 20 7b 0d 0a 20  petFragment {.. 
475ff 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
47600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47601 20 2f 2a 20 43 6f 6c 75 6d 6e 20 73 6e 69 70 70   /* Column snipp
47602 65 74 20 69 73 20 65 78 74 72 61 63 74 65 64 20  et is extracted 
47603 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  from */..  int i
47604 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Pos;            
47605 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
47606 64 65 78 20 6f 66 20 66 69 72 73 74 20 74 6f 6b  dex of first tok
47607 65 6e 20 69 6e 20 73 6e 69 70 70 65 74 20 2a 2f  en in snippet */
47608 0d 0a 20 20 75 36 34 20 63 6f 76 65 72 65 64 3b  ..  u64 covered;
47609 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4760a 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 71      /* Mask of q
4760b 75 65 72 79 20 70 68 72 61 73 65 73 20 63 6f 76  uery phrases cov
4760c 65 72 65 64 20 2a 2f 0d 0a 20 20 75 36 34 20 68  ered */..  u64 h
4760d 6c 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  lmask;          
4760e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
4760f 73 6b 20 6f 66 20 73 6e 69 70 70 65 74 20 74 65  sk of snippet te
47610 72 6d 73 20 74 6f 20 68 69 67 68 6c 69 67 68 74  rms to highlight
47611 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a   */..};..../*..*
47612 2a 20 54 68 69 73 20 74 79 70 65 20 69 73 20 75  * This type is u
47613 73 65 64 20 61 73 20 61 6e 20 66 74 73 33 45 78  sed as an fts3Ex
47614 70 72 49 74 65 72 61 74 65 28 29 20 63 6f 6e 74  prIterate() cont
47615 65 78 74 20 6f 62 6a 65 63 74 20 77 68 69 6c 65  ext object while
47616 20 0d 0a 2a 2a 20 61 63 63 75 6d 75 6c 61 74 69   ..** accumulati
47617 6e 67 20 74 68 65 20 64 61 74 61 20 72 65 74 75  ng the data retu
47618 72 6e 65 64 20 62 79 20 74 68 65 20 6d 61 74 63  rned by the matc
47619 68 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f 6e  hinfo() function
4761a 2e 0d 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73  ...*/..typedef s
4761b 74 72 75 63 74 20 4d 61 74 63 68 49 6e 66 6f 20  truct MatchInfo 
4761c 4d 61 74 63 68 49 6e 66 6f 3b 0d 0a 73 74 72 75  MatchInfo;..stru
4761d 63 74 20 4d 61 74 63 68 49 6e 66 6f 20 7b 0d 0a  ct MatchInfo {..
4761e 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
4761f 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
47620 20 20 2f 2a 20 46 54 53 33 20 43 75 72 73 6f 72    /* FTS3 Cursor
47621 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b   */..  int nCol;
47622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47623 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
47624 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
47625 61 62 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e  able */..  int n
47626 50 68 72 61 73 65 3b 20 20 20 20 20 20 20 20 20  Phrase;         
47627 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
47628 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 61 62 6c  mber of matchabl
47629 65 20 70 68 72 61 73 65 73 20 69 6e 20 71 75 65  e phrases in que
4762a 72 79 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33  ry */..  sqlite3
4762b 5f 69 6e 74 36 34 20 6e 44 6f 63 3b 20 20 20 20  _int64 nDoc;    
4762c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
4762d 65 72 20 6f 66 20 64 6f 63 73 20 69 6e 20 64 61  er of docs in da
4762e 74 61 62 61 73 65 20 2a 2f 0d 0a 20 20 75 33 32  tabase */..  u32
4762f 20 2a 61 4d 61 74 63 68 69 6e 66 6f 3b 20 20 20   *aMatchinfo;   
47630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47631 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75  Pre-allocated bu
47632 66 66 65 72 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d  ffer */..};.....
47633 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73 6e  .../*..** The sn
47634 69 70 70 65 74 28 29 20 61 6e 64 20 6f 66 66 73  ippet() and offs
47635 65 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 20  ets() functions 
47636 62 6f 74 68 20 72 65 74 75 72 6e 20 74 65 78 74  both return text
47637 20 76 61 6c 75 65 73 2e 20 41 6e 20 69 6e 73 74   values. An inst
47638 61 6e 63 65 0d 0a 2a 2a 20 6f 66 20 74 68 65 20  ance..** of the 
47639 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
4763a 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 61  ure is used to a
4763b 63 63 75 6d 75 6c 61 74 65 20 74 68 6f 73 65 20  ccumulate those 
4763c 76 61 6c 75 65 73 20 77 68 69 6c 65 20 74 68 65  values while the
4763d 0d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61  ..** functions a
4763e 72 65 20 72 75 6e 6e 69 6e 67 2e 20 53 65 65 20  re running. See 
4763f 66 74 73 33 53 74 72 69 6e 67 41 70 70 65 6e 64  fts3StringAppend
47640 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0d  () for details..
47641 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72  .*/..typedef str
47642 75 63 74 20 53 74 72 42 75 66 66 65 72 20 53 74  uct StrBuffer St
47643 72 42 75 66 66 65 72 3b 0d 0a 73 74 72 75 63 74  rBuffer;..struct
47644 20 53 74 72 42 75 66 66 65 72 20 7b 0d 0a 20 20   StrBuffer {..  
47645 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
47646 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47647 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75  /* Pointer to bu
47648 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
47649 73 74 72 69 6e 67 20 2a 2f 0d 0a 20 20 69 6e 74  string */..  int
4764a 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
4764b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4764c 4c 65 6e 67 74 68 20 6f 66 20 7a 20 69 6e 20 62  Length of z in b
4764d 79 74 65 73 20 28 65 78 63 6c 2e 20 6e 75 6c 2d  ytes (excl. nul-
4764e 74 65 72 6d 29 20 2a 2f 0d 0a 20 20 69 6e 74 20  term) */..  int 
4764f 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
47650 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
47651 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
47652 20 62 75 66 66 65 72 20 7a 20 69 6e 20 62 79 74   buffer z in byt
47653 65 73 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f  es */..};....../
47654 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  *..** This funct
47655 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 68  ion is used to h
47656 65 6c 70 20 69 74 65 72 61 74 65 20 74 68 72 6f  elp iterate thro
47657 75 67 68 20 61 20 70 6f 73 69 74 69 6f 6e 2d 6c  ugh a position-l
47658 69 73 74 2e 20 41 20 70 6f 73 69 74 69 6f 6e 0d  ist. A position.
47659 0a 2a 2a 20 6c 69 73 74 20 69 73 20 61 20 6c 69  .** list is a li
4765a 73 74 20 6f 66 20 75 6e 69 71 75 65 20 69 6e 74  st of unique int
4765b 65 67 65 72 73 2c 20 73 6f 72 74 65 64 20 66 72  egers, sorted fr
4765c 6f 6d 20 73 6d 61 6c 6c 65 73 74 20 74 6f 20 6c  om smallest to l
4765d 61 72 67 65 73 74 2e 20 45 61 63 68 0d 0a 2a 2a  argest. Each..**
4765e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
4765f 6c 69 73 74 20 69 73 20 72 65 70 72 65 73 65 6e  list is represen
47660 74 65 64 20 62 79 20 61 6e 20 46 54 53 33 20 76  ted by an FTS3 v
47661 61 72 69 6e 74 20 74 68 61 74 20 74 61 6b 65 73  arint that takes
47662 20 74 68 65 20 76 61 6c 75 65 0d 0a 2a 2a 20 6f   the value..** o
47663 66 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65  f the difference
47664 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72   between the cur
47665 72 65 6e 74 20 65 6c 65 6d 65 6e 74 20 61 6e 64  rent element and
47666 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 6e   the previous on
47667 65 20 70 6c 75 73 0d 0a 2a 2a 20 74 77 6f 2e 20  e plus..** two. 
47668 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  For example, to 
47669 73 74 6f 72 65 20 74 68 65 20 70 6f 73 69 74 69  store the positi
4766a 6f 6e 2d 6c 69 73 74 3a 0d 0a 2a 2a 0d 0a 2a 2a  on-list:..**..**
4766b 20 20 20 20 20 34 20 39 20 31 31 33 0d 0a 2a 2a       4 9 113..**
4766c 0d 0a 2a 2a 20 74 68 65 20 74 68 72 65 65 20 76  ..** the three v
4766d 61 72 69 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  arints:..**..** 
4766e 20 20 20 20 36 20 37 20 31 30 36 0d 0a 2a 2a 0d      6 7 106..**.
4766f 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 64 2e  .** are encoded.
47670 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65 6e 20 74 68  ..**..** When th
47671 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
47672 61 6c 6c 65 64 2c 20 2a 70 70 20 70 6f 69 6e 74  alled, *pp point
47673 73 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  s to the start o
47674 66 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 0d  f an element of.
47675 0a 2a 2a 20 74 68 65 20 6c 69 73 74 2e 20 2a 70  .** the list. *p
47676 69 50 6f 73 20 63 6f 6e 74 61 69 6e 73 20 74 68  iPos contains th
47677 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70  e value of the p
47678 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e  revious entry in
47679 20 74 68 65 20 6c 69 73 74 2e 0d 0a 2a 2a 20 41   the list...** A
4767a 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 2c  fter it returns,
4767b 20 2a 70 69 50 6f 73 20 63 6f 6e 74 61 69 6e 73   *piPos contains
4767c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
4767d 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 6f  e next element o
4767e 66 20 74 68 65 0d 0a 2a 2a 20 6c 69 73 74 20 61  f the..** list a
4767f 6e 64 20 2a 70 70 20 69 73 20 61 64 76 61 6e 63  nd *pp is advanc
47680 65 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ed to the follow
47681 69 6e 67 20 76 61 72 69 6e 74 2e 0d 0a 2a 2f 0d  ing varint...*/.
47682 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
47683 33 47 65 74 44 65 6c 74 61 50 6f 73 69 74 69 6f  3GetDeltaPositio
47684 6e 28 63 68 61 72 20 2a 2a 70 70 2c 20 69 6e 74  n(char **pp, int
47685 20 2a 70 69 50 6f 73 29 7b 0d 0a 20 20 69 6e 74   *piPos){..  int
47686 20 69 56 61 6c 3b 0d 0a 20 20 2a 70 70 20 2b 3d   iVal;..  *pp +=
47687 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
47688 61 72 69 6e 74 33 32 28 2a 70 70 2c 20 26 69 56  arint32(*pp, &iV
47689 61 6c 29 3b 0d 0a 20 20 2a 70 69 50 6f 73 20 2b  al);..  *piPos +
4768a 3d 20 28 69 56 61 6c 2d 32 29 3b 0d 0a 7d 0d 0a  = (iVal-2);..}..
4768b 0d 0a 2f 2a 0d 0a 2a 2a 20 48 65 6c 70 65 72 20  ../*..** Helper 
4768c 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 66 74 73  function for fts
4768d 33 45 78 70 72 49 74 65 72 61 74 65 28 29 20 28  3ExprIterate() (
4768e 73 65 65 20 62 65 6c 6f 77 29 2e 0d 0a 2a 2f 0d  see below)...*/.
4768f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
47690 45 78 70 72 49 74 65 72 61 74 65 32 28 0d 0a 20  ExprIterate2(.. 
47691 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
47692 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
47693 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74   /* Expression t
47694 6f 20 69 74 65 72 61 74 65 20 70 68 72 61 73 65  o iterate phrase
47695 73 20 6f 66 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  s of */..  int *
47696 70 69 50 68 72 61 73 65 2c 20 20 20 20 20 20 20  piPhrase,       
47697 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
47698 69 6e 74 65 72 20 74 6f 20 70 68 72 61 73 65 20  inter to phrase 
47699 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 69 6e  counter */..  in
4769a 74 20 28 2a 78 29 28 46 74 73 33 45 78 70 72 2a  t (*x)(Fts3Expr*
4769b 2c 69 6e 74 2c 76 6f 69 64 2a 29 2c 20 20 2f 2a  ,int,void*),  /*
4769c 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   Callback functi
4769d 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 66 6f 72  on to invoke for
4769e 20 70 68 72 61 73 65 73 20 2a 2f 0d 0a 20 20 76   phrases */..  v
4769f 6f 69 64 20 2a 70 43 74 78 20 20 20 20 20 20 20  oid *pCtx       
476a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
476a1 2a 20 53 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  * Second argumen
476a2 74 20 74 6f 20 70 61 73 73 20 74 6f 20 63 61 6c  t to pass to cal
476a3 6c 62 61 63 6b 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  lback */..){..  
476a4 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
476a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476a6 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
476a7 2f 0d 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d  /..  int eType =
476a8 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3b 20 20   pExpr->eType;  
476a9 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
476aa 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
476ab 70 45 78 70 72 20 2a 2f 0d 0a 0d 0a 20 20 69 66  pExpr */....  if
476ac 28 20 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ( eType!=FTSQUER
476ad 59 5f 50 48 52 41 53 45 20 29 7b 0d 0a 20 20 20  Y_PHRASE ){..   
476ae 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
476af 70 4c 65 66 74 20 26 26 20 70 45 78 70 72 2d 3e  pLeft && pExpr->
476b0 70 52 69 67 68 74 20 29 3b 0d 0a 20 20 20 20 72  pRight );..    r
476b1 63 20 3d 20 66 74 73 33 45 78 70 72 49 74 65 72  c = fts3ExprIter
476b2 61 74 65 32 28 70 45 78 70 72 2d 3e 70 4c 65 66  ate2(pExpr->pLef
476b3 74 2c 20 70 69 50 68 72 61 73 65 2c 20 78 2c 20  t, piPhrase, x, 
476b4 70 43 74 78 29 3b 0d 0a 20 20 20 20 69 66 28 20  pCtx);..    if( 
476b5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
476b6 20 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59   eType!=FTSQUERY
476b7 5f 4e 4f 54 20 29 7b 0d 0a 20 20 20 20 20 20 72  _NOT ){..      r
476b8 63 20 3d 20 66 74 73 33 45 78 70 72 49 74 65 72  c = fts3ExprIter
476b9 61 74 65 32 28 70 45 78 70 72 2d 3e 70 52 69 67  ate2(pExpr->pRig
476ba 68 74 2c 20 70 69 50 68 72 61 73 65 2c 20 78 2c  ht, piPhrase, x,
476bb 20 70 43 74 78 29 3b 0d 0a 20 20 20 20 7d 0d 0a   pCtx);..    }..
476bc 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 63    }else{..    rc
476bd 20 3d 20 78 28 70 45 78 70 72 2c 20 2a 70 69 50   = x(pExpr, *piP
476be 68 72 61 73 65 2c 20 70 43 74 78 29 3b 0d 0a 20  hrase, pCtx);.. 
476bf 20 20 20 28 2a 70 69 50 68 72 61 73 65 29 2b 2b     (*piPhrase)++
476c0 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
476c1 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
476c2 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67  * Iterate throug
476c3 68 20 61 6c 6c 20 70 68 72 61 73 65 20 6e 6f 64  h all phrase nod
476c4 65 73 20 69 6e 20 61 6e 20 46 54 53 33 20 71 75  es in an FTS3 qu
476c5 65 72 79 2c 20 65 78 63 65 70 74 20 74 68 6f 73  ery, except thos
476c6 65 20 74 68 61 74 0d 0a 2a 2a 20 61 72 65 20 70  e that..** are p
476c7 61 72 74 20 6f 66 20 61 20 73 75 62 2d 74 72 65  art of a sub-tre
476c8 65 20 74 68 61 74 20 69 73 20 74 68 65 20 72 69  e that is the ri
476c9 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
476ca 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e   a NOT operator.
476cb 0d 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 70 68  ..** For each ph
476cc 72 61 73 65 20 6e 6f 64 65 20 66 6f 75 6e 64 2c  rase node found,
476cd 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 63 61   the supplied ca
476ce 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
476cf 69 73 20 69 6e 76 6f 6b 65 64 2e 0d 0a 2a 2a 0d  is invoked...**.
476d0 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62  .** If the callb
476d1 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  ack function ret
476d2 75 72 6e 73 20 61 6e 79 74 68 69 6e 67 20 6f 74  urns anything ot
476d3 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
476d4 4f 4b 2c 20 0d 0a 2a 2a 20 74 68 65 20 69 74 65  OK, ..** the ite
476d5 72 61 74 69 6f 6e 20 69 73 20 61 62 61 6e 64 6f  ration is abando
476d6 6e 65 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ned and the erro
476d7 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
476d8 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0d 0a 2a 2a  immediately...**
476d9 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49   Otherwise, SQLI
476da 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
476db 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 62 61  d after a callba
476dc 63 6b 20 68 61 73 20 62 65 65 6e 20 6d 61 64 65  ck has been made
476dd 20 66 6f 72 0d 0a 2a 2a 20 61 6c 6c 20 65 6c 69   for..** all eli
476de 67 69 62 6c 65 20 70 68 72 61 73 65 20 6e 6f 64  gible phrase nod
476df 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
476e0 69 6e 74 20 66 74 73 33 45 78 70 72 49 74 65 72  int fts3ExprIter
476e1 61 74 65 28 0d 0a 20 20 46 74 73 33 45 78 70 72  ate(..  Fts3Expr
476e2 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
476e3 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
476e4 73 73 69 6f 6e 20 74 6f 20 69 74 65 72 61 74 65  ssion to iterate
476e5 20 70 68 72 61 73 65 73 20 6f 66 20 2a 2f 0d 0a   phrases of */..
476e6 20 20 69 6e 74 20 28 2a 78 29 28 46 74 73 33 45    int (*x)(Fts3E
476e7 78 70 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 2c  xpr*,int,void*),
476e8 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75    /* Callback fu
476e9 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
476ea 20 66 6f 72 20 70 68 72 61 73 65 73 20 2a 2f 0d   for phrases */.
476eb 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20 20  .  void *pCtx   
476ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476ed 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 61 72 67     /* Second arg
476ee 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ument to pass to
476ef 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0d 0a 29 7b   callback */..){
476f0 0d 0a 20 20 69 6e 74 20 69 50 68 72 61 73 65 20  ..  int iPhrase 
476f1 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
476f2 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20      /* Variable 
476f3 75 73 65 64 20 61 73 20 74 68 65 20 70 68 72 61  used as the phra
476f4 73 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20  se counter */.. 
476f5 20 72 65 74 75 72 6e 20 66 74 73 33 45 78 70 72   return fts3Expr
476f6 49 74 65 72 61 74 65 32 28 70 45 78 70 72 2c 20  Iterate2(pExpr, 
476f7 26 69 50 68 72 61 73 65 2c 20 78 2c 20 70 43 74  &iPhrase, x, pCt
476f8 78 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  x);..}..../*..**
476f9 20 54 68 69 73 20 69 73 20 61 6e 20 66 74 73 33   This is an fts3
476fa 45 78 70 72 49 74 65 72 61 74 65 28 29 20 63 61  ExprIterate() ca
476fb 6c 6c 62 61 63 6b 20 75 73 65 64 20 77 68 69 6c  llback used whil
476fc 65 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 6f  e loading the do
476fd 63 6c 69 73 74 73 0d 0a 2a 2a 20 66 6f 72 20 65  clists..** for e
476fe 61 63 68 20 70 68 72 61 73 65 20 69 6e 74 6f 20  ach phrase into 
476ff 46 74 73 33 45 78 70 72 2e 61 44 6f 63 6c 69 73  Fts3Expr.aDoclis
47700 74 5b 5d 2f 6e 44 6f 63 6c 69 73 74 2e 20 53 65  t[]/nDoclist. Se
47701 65 20 61 6c 73 6f 0d 0a 2a 2a 20 66 74 73 33 45  e also..** fts3E
47702 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73 74 73 28  xprLoadDoclists(
47703 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  )...*/..static i
47704 6e 74 20 66 74 73 33 45 78 70 72 4c 6f 61 64 44  nt fts3ExprLoadD
47705 6f 63 6c 69 73 74 73 43 62 28 46 74 73 33 45 78  oclistsCb(Fts3Ex
47706 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 69  pr *pExpr, int i
47707 50 68 72 61 73 65 2c 20 76 6f 69 64 20 2a 63 74  Phrase, void *ct
47708 78 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  x){..  int rc = 
47709 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 46 74  SQLITE_OK;..  Ft
4770a 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73  s3Phrase *pPhras
4770b 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61  e = pExpr->pPhra
4770c 73 65 3b 0d 0a 20 20 4c 6f 61 64 44 6f 63 6c 69  se;..  LoadDocli
4770d 73 74 43 74 78 20 2a 70 20 3d 20 28 4c 6f 61 64  stCtx *p = (Load
4770e 44 6f 63 6c 69 73 74 43 74 78 20 2a 29 63 74 78  DoclistCtx *)ctx
4770f 3b 0d 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41  ;....  UNUSED_PA
47710 52 41 4d 45 54 45 52 28 69 50 68 72 61 73 65 29  RAMETER(iPhrase)
47711 3b 0d 0a 0d 0a 20 20 70 2d 3e 6e 50 68 72 61 73  ;....  p->nPhras
47712 65 2b 2b 3b 0d 0a 20 20 70 2d 3e 6e 54 6f 6b 65  e++;..  p->nToke
47713 6e 20 2b 3d 20 70 50 68 72 61 73 65 2d 3e 6e 54  n += pPhrase->nT
47714 6f 6b 65 6e 3b 0d 0a 0d 0a 20 20 72 65 74 75 72  oken;....  retur
47715 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
47716 2a 2a 20 4c 6f 61 64 20 74 68 65 20 64 6f 63 6c  ** Load the docl
47717 69 73 74 73 20 66 6f 72 20 65 61 63 68 20 70 68  ists for each ph
47718 72 61 73 65 20 69 6e 20 74 68 65 20 71 75 65 72  rase in the quer
47719 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
4771a 68 20 46 54 53 33 20 63 75 72 73 6f 72 0d 0a 2a  h FTS3 cursor..*
4771b 2a 20 70 43 73 72 2e 20 0d 0a 2a 2a 0d 0a 2a 2a  * pCsr. ..**..**
4771c 20 49 66 20 70 6e 50 68 72 61 73 65 20 69 73 20   If pnPhrase is 
4771d 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a  not NULL, then *
4771e 70 6e 50 68 72 61 73 65 20 69 73 20 73 65 74 20  pnPhrase is set 
4771f 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
47720 20 6d 61 74 63 68 61 62 6c 65 20 0d 0a 2a 2a 20   matchable ..** 
47721 70 68 72 61 73 65 73 20 69 6e 20 74 68 65 20 65  phrases in the e
47722 78 70 72 65 73 73 69 6f 6e 20 28 61 6c 6c 20 70  xpression (all p
47723 68 72 61 73 65 73 20 65 78 63 65 70 74 20 74 68  hrases except th
47724 6f 73 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20  ose directly or 
47725 0d 0a 2a 2a 20 69 6e 64 69 72 65 63 74 6c 79 20  ..** indirectly 
47726 64 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74  descended from t
47727 68 65 20 72 69 67 68 74 2d 68 61 6e 64 2d 73 69  he right-hand-si
47728 64 65 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72  de of a NOT oper
47729 61 74 6f 72 29 2e 20 49 66 20 0d 0a 2a 2a 20 70  ator). If ..** p
4772a 6e 54 6f 6b 65 6e 20 69 73 20 6e 6f 74 20 4e 55  nToken is not NU
4772b 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 73  LL, then it is s
4772c 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
4772d 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 61 6c   of tokens in al
4772e 6c 0d 0a 2a 2a 20 6d 61 74 63 68 61 62 6c 65 20  l..** matchable 
4772f 70 68 72 61 73 65 73 20 6f 66 20 74 68 65 20 65  phrases of the e
47730 78 70 72 65 73 73 69 6f 6e 2e 0d 0a 2a 2f 0d 0a  xpression...*/..
47731 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45  static int fts3E
47732 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73 74 73 28  xprLoadDoclists(
47733 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a  ..  Fts3Cursor *
47734 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20 20  pCsr,           
47735 20 20 20 20 2f 2a 20 46 74 73 33 20 63 75 72 73      /* Fts3 curs
47736 6f 72 20 66 6f 72 20 63 75 72 72 65 6e 74 20 71  or for current q
47737 75 65 72 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  uery */..  int *
47738 70 6e 50 68 72 61 73 65 2c 20 20 20 20 20 20 20  pnPhrase,       
47739 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
4773a 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 70 68 72  T: Number of phr
4773b 61 73 65 73 20 69 6e 20 71 75 65 72 79 20 2a 2f  ases in query */
4773c 0d 0a 20 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e  ..  int *pnToken
4773d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4773e 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
4773f 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
47740 71 75 65 72 79 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  query */..){..  
47741 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
47742 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47743 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
47744 2f 0d 0a 20 20 4c 6f 61 64 44 6f 63 6c 69 73 74  /..  LoadDoclist
47745 43 74 78 20 73 43 74 78 20 3d 20 7b 30 2c 30 2c  Ctx sCtx = {0,0,
47746 30 7d 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20  0};  /* Context 
47747 66 6f 72 20 66 74 73 33 45 78 70 72 49 74 65 72  for fts3ExprIter
47748 61 74 65 28 29 20 2a 2f 0d 0a 20 20 73 43 74 78  ate() */..  sCtx
47749 2e 70 43 73 72 20 3d 20 70 43 73 72 3b 0d 0a 20  .pCsr = pCsr;.. 
4774a 20 72 63 20 3d 20 66 74 73 33 45 78 70 72 49 74   rc = fts3ExprIt
4774b 65 72 61 74 65 28 70 43 73 72 2d 3e 70 45 78 70  erate(pCsr->pExp
4774c 72 2c 20 66 74 73 33 45 78 70 72 4c 6f 61 64 44  r, fts3ExprLoadD
4774d 6f 63 6c 69 73 74 73 43 62 2c 20 28 76 6f 69 64  oclistsCb, (void
4774e 20 2a 29 26 73 43 74 78 29 3b 0d 0a 20 20 69 66   *)&sCtx);..  if
4774f 28 20 70 6e 50 68 72 61 73 65 20 29 20 2a 70 6e  ( pnPhrase ) *pn
47750 50 68 72 61 73 65 20 3d 20 73 43 74 78 2e 6e 50  Phrase = sCtx.nP
47751 68 72 61 73 65 3b 0d 0a 20 20 69 66 28 20 70 6e  hrase;..  if( pn
47752 54 6f 6b 65 6e 20 29 20 2a 70 6e 54 6f 6b 65 6e  Token ) *pnToken
47753 20 3d 20 73 43 74 78 2e 6e 54 6f 6b 65 6e 3b 0d   = sCtx.nToken;.
47754 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
47755 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  ....static int f
47756 74 73 33 45 78 70 72 50 68 72 61 73 65 43 6f 75  ts3ExprPhraseCou
47757 6e 74 43 62 28 46 74 73 33 45 78 70 72 20 2a 70  ntCb(Fts3Expr *p
47758 45 78 70 72 2c 20 69 6e 74 20 69 50 68 72 61 73  Expr, int iPhras
47759 65 2c 20 76 6f 69 64 20 2a 63 74 78 29 7b 0d 0a  e, void *ctx){..
4775a 20 20 28 2a 28 69 6e 74 20 2a 29 63 74 78 29 2b    (*(int *)ctx)+
4775b 2b 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  +;..  UNUSED_PAR
4775c 41 4d 45 54 45 52 28 70 45 78 70 72 29 3b 0d 0a  AMETER(pExpr);..
4775d 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
4775e 45 52 28 69 50 68 72 61 73 65 29 3b 0d 0a 20 20  ER(iPhrase);..  
4775f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
47760 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 69 6e 74  ;..}..static int
47761 20 66 74 73 33 45 78 70 72 50 68 72 61 73 65 43   fts3ExprPhraseC
47762 6f 75 6e 74 28 46 74 73 33 45 78 70 72 20 2a 70  ount(Fts3Expr *p
47763 45 78 70 72 29 7b 0d 0a 20 20 69 6e 74 20 6e 50  Expr){..  int nP
47764 68 72 61 73 65 20 3d 20 30 3b 0d 0a 20 20 28 76  hrase = 0;..  (v
47765 6f 69 64 29 66 74 73 33 45 78 70 72 49 74 65 72  oid)fts3ExprIter
47766 61 74 65 28 70 45 78 70 72 2c 20 66 74 73 33 45  ate(pExpr, fts3E
47767 78 70 72 50 68 72 61 73 65 43 6f 75 6e 74 43 62  xprPhraseCountCb
47768 2c 20 28 76 6f 69 64 20 2a 29 26 6e 50 68 72 61  , (void *)&nPhra
47769 73 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 6e  se);..  return n
4776a 50 68 72 61 73 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  Phrase;..}..../*
4776b 0d 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65  ..** Advance the
4776c 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 69   position list i
4776d 74 65 72 61 74 6f 72 20 73 70 65 63 69 66 69 65  terator specifie
4776e 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 74  d by the first t
4776f 77 6f 20 0d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  wo ..** argument
47770 73 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  s so that it poi
47771 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74  nts to the first
47772 20 65 6c 65 6d 65 6e 74 20 77 69 74 68 20 61 20   element with a 
47773 76 61 6c 75 65 20 67 72 65 61 74 65 72 0d 0a 2a  value greater..*
47774 2a 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  * than or equal 
47775 74 6f 20 70 61 72 61 6d 65 74 65 72 20 69 4e 65  to parameter iNe
47776 78 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  xt...*/..static 
47777 76 6f 69 64 20 66 74 73 33 53 6e 69 70 70 65 74  void fts3Snippet
47778 41 64 76 61 6e 63 65 28 63 68 61 72 20 2a 2a 70  Advance(char **p
47779 70 49 74 65 72 2c 20 69 6e 74 20 2a 70 69 49 74  pIter, int *piIt
4777a 65 72 2c 20 69 6e 74 20 69 4e 65 78 74 29 7b 0d  er, int iNext){.
4777b 0a 20 20 63 68 61 72 20 2a 70 49 74 65 72 20 3d  .  char *pIter =
4777c 20 2a 70 70 49 74 65 72 3b 0d 0a 20 20 69 66 28   *ppIter;..  if(
4777d 20 70 49 74 65 72 20 29 7b 0d 0a 20 20 20 20 69   pIter ){..    i
4777e 6e 74 20 69 49 74 65 72 20 3d 20 2a 70 69 49 74  nt iIter = *piIt
4777f 65 72 3b 0d 0a 0d 0a 20 20 20 20 77 68 69 6c 65  er;....    while
47780 28 20 69 49 74 65 72 3c 69 4e 65 78 74 20 29 7b  ( iIter<iNext ){
47781 0d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28  ..      if( 0==(
47782 2a 70 49 74 65 72 20 26 20 30 78 46 45 29 20 29  *pIter & 0xFE) )
47783 7b 0d 0a 20 20 20 20 20 20 20 20 69 49 74 65 72  {..        iIter
47784 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 20 20 20 20   = -1;..        
47785 70 49 74 65 72 20 3d 20 30 3b 0d 0a 20 20 20 20  pIter = 0;..    
47786 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
47787 20 20 7d 0d 0a 20 20 20 20 20 20 66 74 73 33 47    }..      fts3G
47788 65 74 44 65 6c 74 61 50 6f 73 69 74 69 6f 6e 28  etDeltaPosition(
47789 26 70 49 74 65 72 2c 20 26 69 49 74 65 72 29 3b  &pIter, &iIter);
4778a 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2a  ..    }....    *
4778b 70 69 49 74 65 72 20 3d 20 69 49 74 65 72 3b 0d  piIter = iIter;.
4778c 0a 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70  .    *ppIter = p
4778d 49 74 65 72 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  Iter;..  }..}...
4778e 0a 2f 2a 0d 0a 2a 2a 20 41 64 76 61 6e 63 65 20  ./*..** Advance 
4778f 74 68 65 20 73 6e 69 70 70 65 74 20 69 74 65 72  the snippet iter
47790 61 74 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74  ator to the next
47791 20 63 61 6e 64 69 64 61 74 65 20 73 6e 69 70 70   candidate snipp
47792 65 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  et...*/..static 
47793 69 6e 74 20 66 74 73 33 53 6e 69 70 70 65 74 4e  int fts3SnippetN
47794 65 78 74 43 61 6e 64 69 64 61 74 65 28 53 6e 69  extCandidate(Sni
47795 70 70 65 74 49 74 65 72 20 2a 70 49 74 65 72 29  ppetIter *pIter)
47796 7b 0d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  {..  int i;     
47797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47798 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
47799 6e 74 65 72 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28  nter */....  if(
4779a 20 70 49 74 65 72 2d 3e 69 43 75 72 72 65 6e 74   pIter->iCurrent
4779b 3c 30 20 29 7b 0d 0a 20 20 20 20 2f 2a 20 54 68  <0 ){..    /* Th
4779c 65 20 53 6e 69 70 70 65 74 49 74 65 72 20 6f 62  e SnippetIter ob
4779d 6a 65 63 74 20 68 61 73 20 6a 75 73 74 20 62 65  ject has just be
4779e 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  en initialized. 
4779f 54 68 65 20 66 69 72 73 74 20 73 6e 69 70 70 65  The first snippe
477a0 74 0d 0a 20 20 20 20 2a 2a 20 63 61 6e 64 69 64  t..    ** candid
477a1 61 74 65 20 61 6c 77 61 79 73 20 73 74 61 72 74  ate always start
477a2 73 20 61 74 20 6f 66 66 73 65 74 20 30 20 28 65  s at offset 0 (e
477a3 76 65 6e 20 69 66 20 74 68 69 73 20 63 61 6e 64  ven if this cand
477a4 69 64 61 74 65 20 68 61 73 20 61 0d 0a 20 20 20  idate has a..   
477a5 20 2a 2a 20 73 63 6f 72 65 20 6f 66 20 30 2e 30   ** score of 0.0
477a6 29 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20  )...    */..    
477a7 70 49 74 65 72 2d 3e 69 43 75 72 72 65 6e 74 20  pIter->iCurrent 
477a8 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 41  = 0;....    /* A
477a9 64 76 61 6e 63 65 20 74 68 65 20 27 68 65 61 64  dvance the 'head
477aa 27 20 69 74 65 72 61 74 6f 72 20 6f 66 20 65 61  ' iterator of ea
477ab 63 68 20 70 68 72 61 73 65 20 74 6f 20 74 68 65  ch phrase to the
477ac 20 66 69 72 73 74 20 6f 66 66 73 65 74 20 74 68   first offset th
477ad 61 74 0d 0a 20 20 20 20 2a 2a 20 69 73 20 67 72  at..    ** is gr
477ae 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
477af 75 61 6c 20 74 6f 20 28 69 4e 65 78 74 2b 6e 53  ual to (iNext+nS
477b0 6e 69 70 70 65 74 29 2e 0d 0a 20 20 20 20 2a 2f  nippet)...    */
477b1 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
477b2 3c 70 49 74 65 72 2d 3e 6e 50 68 72 61 73 65 3b  <pIter->nPhrase;
477b3 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 53 6e   i++){..      Sn
477b4 69 70 70 65 74 50 68 72 61 73 65 20 2a 70 50 68  ippetPhrase *pPh
477b5 72 61 73 65 20 3d 20 26 70 49 74 65 72 2d 3e 61  rase = &pIter->a
477b6 50 68 72 61 73 65 5b 69 5d 3b 0d 0a 20 20 20 20  Phrase[i];..    
477b7 20 20 66 74 73 33 53 6e 69 70 70 65 74 41 64 76    fts3SnippetAdv
477b8 61 6e 63 65 28 26 70 50 68 72 61 73 65 2d 3e 70  ance(&pPhrase->p
477b9 48 65 61 64 2c 20 26 70 50 68 72 61 73 65 2d 3e  Head, &pPhrase->
477ba 69 48 65 61 64 2c 20 70 49 74 65 72 2d 3e 6e 53  iHead, pIter->nS
477bb 6e 69 70 70 65 74 29 3b 0d 0a 20 20 20 20 7d 0d  nippet);..    }.
477bc 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 69  .  }else{..    i
477bd 6e 74 20 69 53 74 61 72 74 3b 0d 0a 20 20 20 20  nt iStart;..    
477be 69 6e 74 20 69 45 6e 64 20 3d 20 30 78 37 46 46  int iEnd = 0x7FF
477bf 46 46 46 46 46 3b 0d 0a 0d 0a 20 20 20 20 66 6f  FFFFF;....    fo
477c0 72 28 69 3d 30 3b 20 69 3c 70 49 74 65 72 2d 3e  r(i=0; i<pIter->
477c1 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29 7b 0d 0a  nPhrase; i++){..
477c2 20 20 20 20 20 20 53 6e 69 70 70 65 74 50 68 72        SnippetPhr
477c3 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20 26  ase *pPhrase = &
477c4 70 49 74 65 72 2d 3e 61 50 68 72 61 73 65 5b 69  pIter->aPhrase[i
477c5 5d 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 50  ];..      if( pP
477c6 68 72 61 73 65 2d 3e 70 48 65 61 64 20 26 26 20  hrase->pHead && 
477c7 70 50 68 72 61 73 65 2d 3e 69 48 65 61 64 3c 69  pPhrase->iHead<i
477c8 45 6e 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  End ){..        
477c9 69 45 6e 64 20 3d 20 70 50 68 72 61 73 65 2d 3e  iEnd = pPhrase->
477ca 69 48 65 61 64 3b 0d 0a 20 20 20 20 20 20 7d 0d  iHead;..      }.
477cb 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28 20  .    }..    if( 
477cc 69 45 6e 64 3d 3d 30 78 37 46 46 46 46 46 46 46  iEnd==0x7FFFFFFF
477cd 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72   ){..      retur
477ce 6e 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20  n 1;..    }.... 
477cf 20 20 20 70 49 74 65 72 2d 3e 69 43 75 72 72 65     pIter->iCurre
477d0 6e 74 20 3d 20 69 53 74 61 72 74 20 3d 20 69 45  nt = iStart = iE
477d1 6e 64 20 2d 20 70 49 74 65 72 2d 3e 6e 53 6e 69  nd - pIter->nSni
477d2 70 70 65 74 20 2b 20 31 3b 0d 0a 20 20 20 20 66  ppet + 1;..    f
477d3 6f 72 28 69 3d 30 3b 20 69 3c 70 49 74 65 72 2d  or(i=0; i<pIter-
477d4 3e 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29 7b 0d  >nPhrase; i++){.
477d5 0a 20 20 20 20 20 20 53 6e 69 70 70 65 74 50 68  .      SnippetPh
477d6 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20  rase *pPhrase = 
477d7 26 70 49 74 65 72 2d 3e 61 50 68 72 61 73 65 5b  &pIter->aPhrase[
477d8 69 5d 3b 0d 0a 20 20 20 20 20 20 66 74 73 33 53  i];..      fts3S
477d9 6e 69 70 70 65 74 41 64 76 61 6e 63 65 28 26 70  nippetAdvance(&p
477da 50 68 72 61 73 65 2d 3e 70 48 65 61 64 2c 20 26  Phrase->pHead, &
477db 70 50 68 72 61 73 65 2d 3e 69 48 65 61 64 2c 20  pPhrase->iHead, 
477dc 69 45 6e 64 2b 31 29 3b 0d 0a 20 20 20 20 20 20  iEnd+1);..      
477dd 66 74 73 33 53 6e 69 70 70 65 74 41 64 76 61 6e  fts3SnippetAdvan
477de 63 65 28 26 70 50 68 72 61 73 65 2d 3e 70 54 61  ce(&pPhrase->pTa
477df 69 6c 2c 20 26 70 50 68 72 61 73 65 2d 3e 69 54  il, &pPhrase->iT
477e0 61 69 6c 2c 20 69 53 74 61 72 74 29 3b 0d 0a 20  ail, iStart);.. 
477e1 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72     }..  }....  r
477e2 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f  eturn 0;..}..../
477e3 2a 0d 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 69  *..** Retrieve i
477e4 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
477e5 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 61 6e   the current can
477e6 64 69 64 61 74 65 20 73 6e 69 70 70 65 74 20 6f  didate snippet o
477e7 66 20 73 6e 69 70 70 65 74 20 0d 0a 2a 2a 20 69  f snippet ..** i
477e8 74 65 72 61 74 6f 72 20 70 49 74 65 72 2e 0d 0a  terator pIter...
477e9 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
477ea 66 74 73 33 53 6e 69 70 70 65 74 44 65 74 61 69  fts3SnippetDetai
477eb 6c 73 28 0d 0a 20 20 53 6e 69 70 70 65 74 49 74  ls(..  SnippetIt
477ec 65 72 20 2a 70 49 74 65 72 2c 20 20 20 20 20 20  er *pIter,      
477ed 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65         /* Snippe
477ee 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0d 0a 20  t iterator */.. 
477ef 20 75 36 34 20 6d 43 6f 76 65 72 65 64 2c 20 20   u64 mCovered,  
477f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f1 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 70   /* Bitmask of p
477f2 68 72 61 73 65 73 20 61 6c 72 65 61 64 79 20 63  hrases already c
477f3 6f 76 65 72 65 64 20 2a 2f 0d 0a 20 20 69 6e 74  overed */..  int
477f4 20 2a 70 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20   *piToken,      
477f5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
477f6 4f 55 54 3a 20 46 69 72 73 74 20 74 6f 6b 65 6e  OUT: First token
477f7 20 6f 66 20 70 72 6f 70 6f 73 65 64 20 73 6e 69   of proposed sni
477f8 70 70 65 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  ppet */..  int *
477f9 70 69 53 63 6f 72 65 2c 20 20 20 20 20 20 20 20  piScore,        
477fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
477fb 54 3a 20 22 53 63 6f 72 65 22 20 66 6f 72 20 74  T: "Score" for t
477fc 68 69 73 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a  his snippet */..
477fd 20 20 75 36 34 20 2a 70 6d 43 6f 76 65 72 2c 20    u64 *pmCover, 
477fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477ff 20 20 2f 2a 20 4f 55 54 3a 20 42 69 74 6d 61 73    /* OUT: Bitmas
47800 6b 20 6f 66 20 70 68 72 61 73 65 73 20 63 6f 76  k of phrases cov
47801 65 72 65 64 20 2a 2f 0d 0a 20 20 75 36 34 20 2a  ered */..  u64 *
47802 70 6d 48 69 67 68 6c 69 67 68 74 20 20 20 20 20  pmHighlight     
47803 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
47804 54 3a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 65  T: Bitmask of te
47805 72 6d 73 20 74 6f 20 68 69 67 68 6c 69 67 68 74  rms to highlight
47806 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69   */..){..  int i
47807 53 74 61 72 74 20 3d 20 70 49 74 65 72 2d 3e 69  Start = pIter->i
47808 43 75 72 72 65 6e 74 3b 20 20 20 2f 2a 20 46 69  Current;   /* Fi
47809 72 73 74 20 74 6f 6b 65 6e 20 6f 66 20 73 6e 69  rst token of sni
4780a 70 70 65 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ppet */..  int i
4780b 53 63 6f 72 65 20 3d 20 30 3b 20 20 20 20 20 20  Score = 0;      
4780c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
4780d 6f 72 65 20 6f 66 20 74 68 69 73 20 73 6e 69 70  ore of this snip
4780e 70 65 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 3b  pet */..  int i;
4780f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47810 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
47811 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20  p counter */..  
47812 75 36 34 20 6d 43 6f 76 65 72 20 3d 20 30 3b 20  u64 mCover = 0; 
47813 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47814 2f 2a 20 4d 61 73 6b 20 6f 66 20 70 68 72 61 73  /* Mask of phras
47815 65 73 20 63 6f 76 65 72 65 64 20 62 79 20 74 68  es covered by th
47816 69 73 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 20  is snippet */.. 
47817 20 75 36 34 20 6d 48 69 67 68 6c 69 67 68 74 20   u64 mHighlight 
47818 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
47819 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 6f 6b 65   /* Mask of toke
4781a 6e 73 20 74 6f 20 68 69 67 68 6c 69 67 68 74 20  ns to highlight 
4781b 69 6e 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 0d  in snippet */...
4781c 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
4781d 74 65 72 2d 3e 6e 50 68 72 61 73 65 3b 20 69 2b  ter->nPhrase; i+
4781e 2b 29 7b 0d 0a 20 20 20 20 53 6e 69 70 70 65 74  +){..    Snippet
4781f 50 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 20  Phrase *pPhrase 
47820 3d 20 26 70 49 74 65 72 2d 3e 61 50 68 72 61 73  = &pIter->aPhras
47821 65 5b 69 5d 3b 0d 0a 20 20 20 20 69 66 28 20 70  e[i];..    if( p
47822 50 68 72 61 73 65 2d 3e 70 54 61 69 6c 20 29 7b  Phrase->pTail ){
47823 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 43  ..      char *pC
47824 73 72 20 3d 20 70 50 68 72 61 73 65 2d 3e 70 54  sr = pPhrase->pT
47825 61 69 6c 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20  ail;..      int 
47826 69 43 73 72 20 3d 20 70 50 68 72 61 73 65 2d 3e  iCsr = pPhrase->
47827 69 54 61 69 6c 3b 0d 0a 0d 0a 20 20 20 20 20 20  iTail;....      
47828 77 68 69 6c 65 28 20 69 43 73 72 3c 28 69 53 74  while( iCsr<(iSt
47829 61 72 74 2b 70 49 74 65 72 2d 3e 6e 53 6e 69 70  art+pIter->nSnip
4782a 70 65 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  pet) ){..       
4782b 20 69 6e 74 20 6a 3b 0d 0a 20 20 20 20 20 20 20   int j;..       
4782c 20 75 36 34 20 6d 50 68 72 61 73 65 20 3d 20 28   u64 mPhrase = (
4782d 75 36 34 29 31 20 3c 3c 20 69 3b 0d 0a 20 20 20  u64)1 << i;..   
4782e 20 20 20 20 20 75 36 34 20 6d 50 6f 73 20 3d 20       u64 mPos = 
4782f 28 75 36 34 29 31 20 3c 3c 20 28 69 43 73 72 20  (u64)1 << (iCsr 
47830 2d 20 69 53 74 61 72 74 29 3b 0d 0a 20 20 20 20  - iStart);..    
47831 20 20 20 20 61 73 73 65 72 74 28 20 69 43 73 72      assert( iCsr
47832 3e 3d 69 53 74 61 72 74 20 29 3b 0d 0a 20 20 20  >=iStart );..   
47833 20 20 20 20 20 69 66 28 20 28 6d 43 6f 76 65 72       if( (mCover
47834 7c 6d 43 6f 76 65 72 65 64 29 26 6d 50 68 72 61  |mCovered)&mPhra
47835 73 65 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  se ){..         
47836 20 69 53 63 6f 72 65 2b 2b 3b 0d 0a 20 20 20 20   iScore++;..    
47837 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
47838 20 20 20 20 20 20 69 53 63 6f 72 65 20 2b 3d 20        iScore += 
47839 31 30 30 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d  1000;..        }
4783a 0d 0a 20 20 20 20 20 20 20 20 6d 43 6f 76 65 72  ..        mCover
4783b 20 7c 3d 20 6d 50 68 72 61 73 65 3b 0d 0a 0d 0a   |= mPhrase;....
4783c 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
4783d 20 6a 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b   j<pPhrase->nTok
4783e 65 6e 3b 20 6a 2b 2b 29 7b 0d 0a 20 20 20 20 20  en; j++){..     
4783f 20 20 20 20 20 6d 48 69 67 68 6c 69 67 68 74 20       mHighlight 
47840 7c 3d 20 28 6d 50 6f 73 3e 3e 6a 29 3b 0d 0a 20  |= (mPos>>j);.. 
47841 20 20 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20         }....    
47842 20 20 20 20 69 66 28 20 30 3d 3d 28 2a 70 43 73      if( 0==(*pCs
47843 72 20 26 20 30 78 30 46 45 29 20 29 20 62 72 65  r & 0x0FE) ) bre
47844 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 66 74 73  ak;..        fts
47845 33 47 65 74 44 65 6c 74 61 50 6f 73 69 74 69 6f  3GetDeltaPositio
47846 6e 28 26 70 43 73 72 2c 20 26 69 43 73 72 29 3b  n(&pCsr, &iCsr);
47847 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
47848 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65  ..  }....  /* Se
47849 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72  t the output var
4784a 69 61 62 6c 65 73 20 62 65 66 6f 72 65 20 72 65  iables before re
4784b 74 75 72 6e 69 6e 67 2e 20 2a 2f 0d 0a 20 20 2a  turning. */..  *
4784c 70 69 54 6f 6b 65 6e 20 3d 20 69 53 74 61 72 74  piToken = iStart
4784d 3b 0d 0a 20 20 2a 70 69 53 63 6f 72 65 20 3d 20  ;..  *piScore = 
4784e 69 53 63 6f 72 65 3b 0d 0a 20 20 2a 70 6d 43 6f  iScore;..  *pmCo
4784f 76 65 72 20 3d 20 6d 43 6f 76 65 72 3b 0d 0a 20  ver = mCover;.. 
47850 20 2a 70 6d 48 69 67 68 6c 69 67 68 74 20 3d 20   *pmHighlight = 
47851 6d 48 69 67 68 6c 69 67 68 74 3b 0d 0a 7d 0d 0a  mHighlight;..}..
47852 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ../*..** This fu
47853 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 66 74 73  nction is an fts
47854 33 45 78 70 72 49 74 65 72 61 74 65 28 29 20 63  3ExprIterate() c
47855 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 62 79 20  allback used by 
47856 66 74 73 33 42 65 73 74 53 6e 69 70 70 65 74 28  fts3BestSnippet(
47857 29 2e 0d 0a 2a 2a 20 45 61 63 68 20 69 6e 76 6f  )...** Each invo
47858 63 61 74 69 6f 6e 20 70 6f 70 75 6c 61 74 65 73  cation populates
47859 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74   an element of t
4785a 68 65 20 53 6e 69 70 70 65 74 49 74 65 72 2e 61  he SnippetIter.a
4785b 50 68 72 61 73 65 5b 5d 20 61 72 72 61 79 2e 0d  Phrase[] array..
4785c 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
4785d 66 74 73 33 53 6e 69 70 70 65 74 46 69 6e 64 50  fts3SnippetFindP
4785e 6f 73 69 74 69 6f 6e 73 28 46 74 73 33 45 78 70  ositions(Fts3Exp
4785f 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 69 50  r *pExpr, int iP
47860 68 72 61 73 65 2c 20 76 6f 69 64 20 2a 63 74 78  hrase, void *ctx
47861 29 7b 0d 0a 20 20 53 6e 69 70 70 65 74 49 74 65  ){..  SnippetIte
47862 72 20 2a 70 20 3d 20 28 53 6e 69 70 70 65 74 49  r *p = (SnippetI
47863 74 65 72 20 2a 29 63 74 78 3b 0d 0a 20 20 53 6e  ter *)ctx;..  Sn
47864 69 70 70 65 74 50 68 72 61 73 65 20 2a 70 50 68  ippetPhrase *pPh
47865 72 61 73 65 20 3d 20 26 70 2d 3e 61 50 68 72 61  rase = &p->aPhra
47866 73 65 5b 69 50 68 72 61 73 65 5d 3b 0d 0a 20 20  se[iPhrase];..  
47867 63 68 61 72 20 2a 70 43 73 72 3b 0d 0a 0d 0a 20  char *pCsr;.... 
47868 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e   pPhrase->nToken
47869 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73   = pExpr->pPhras
4786a 65 2d 3e 6e 54 6f 6b 65 6e 3b 0d 0a 0d 0a 20 20  e->nToken;....  
4786b 70 43 73 72 20 3d 20 73 71 6c 69 74 65 33 46 74  pCsr = sqlite3Ft
4786c 73 33 45 76 61 6c 50 68 72 61 73 65 50 6f 73 6c  s3EvalPhrasePosl
4786d 69 73 74 28 70 2d 3e 70 43 73 72 2c 20 70 45 78  ist(p->pCsr, pEx
4786e 70 72 2c 20 70 2d 3e 69 43 6f 6c 29 3b 0d 0a 20  pr, p->iCol);.. 
4786f 20 69 66 28 20 70 43 73 72 20 29 7b 0d 0a 20 20   if( pCsr ){..  
47870 20 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 30    int iFirst = 0
47871 3b 0d 0a 20 20 20 20 70 50 68 72 61 73 65 2d 3e  ;..    pPhrase->
47872 70 4c 69 73 74 20 3d 20 70 43 73 72 3b 0d 0a 20  pList = pCsr;.. 
47873 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 50     fts3GetDeltaP
47874 6f 73 69 74 69 6f 6e 28 26 70 43 73 72 2c 20 26  osition(&pCsr, &
47875 69 46 69 72 73 74 29 3b 0d 0a 20 20 20 20 61 73  iFirst);..    as
47876 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20  sert( iFirst>=0 
47877 29 3b 0d 0a 20 20 20 20 70 50 68 72 61 73 65 2d  );..    pPhrase-
47878 3e 70 48 65 61 64 20 3d 20 70 43 73 72 3b 0d 0a  >pHead = pCsr;..
47879 20 20 20 20 70 50 68 72 61 73 65 2d 3e 70 54 61      pPhrase->pTa
4787a 69 6c 20 3d 20 70 43 73 72 3b 0d 0a 20 20 20 20  il = pCsr;..    
4787b 70 50 68 72 61 73 65 2d 3e 69 48 65 61 64 20 3d  pPhrase->iHead =
4787c 20 69 46 69 72 73 74 3b 0d 0a 20 20 20 20 70 50   iFirst;..    pP
4787d 68 72 61 73 65 2d 3e 69 54 61 69 6c 20 3d 20 69  hrase->iTail = i
4787e 46 69 72 73 74 3b 0d 0a 20 20 7d 65 6c 73 65 7b  First;..  }else{
4787f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ..    assert( pP
47880 68 72 61 73 65 2d 3e 70 4c 69 73 74 3d 3d 30 20  hrase->pList==0 
47881 26 26 20 70 50 68 72 61 73 65 2d 3e 70 48 65 61  && pPhrase->pHea
47882 64 3d 3d 30 20 26 26 20 70 50 68 72 61 73 65 2d  d==0 && pPhrase-
47883 3e 70 54 61 69 6c 3d 3d 30 20 29 3b 0d 0a 20 20  >pTail==0 );..  
47884 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  }....  return SQ
47885 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
47886 2a 0d 0a 2a 2a 20 53 65 6c 65 63 74 20 74 68 65  *..** Select the
47887 20 66 72 61 67 6d 65 6e 74 20 6f 66 20 74 65 78   fragment of tex
47888 74 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20  t consisting of 
47889 6e 46 72 61 67 6d 65 6e 74 20 63 6f 6e 74 69 67  nFragment contig
4788a 75 6f 75 73 20 74 6f 6b 65 6e 73 20 0d 0a 2a 2a  uous tokens ..**
4788b 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 20 69 43 6f   from column iCo
4788c 6c 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  l that represent
4788d 20 74 68 65 20 22 62 65 73 74 22 20 73 6e 69 70   the "best" snip
4788e 70 65 74 2e 20 54 68 65 20 62 65 73 74 20 73 6e  pet. The best sn
4788f 69 70 70 65 74 0d 0a 2a 2a 20 69 73 20 74 68 65  ippet..** is the
47890 20 73 6e 69 70 70 65 74 20 77 69 74 68 20 74 68   snippet with th
47891 65 20 68 69 67 68 65 73 74 20 73 63 6f 72 65 2c  e highest score,
47892 20 77 68 65 72 65 20 73 63 6f 72 65 73 20 61 72   where scores ar
47893 65 20 63 61 6c 63 75 6c 61 74 65 64 0d 0a 2a 2a  e calculated..**
47894 20 62 79 20 61 64 64 69 6e 67 3a 0d 0a 2a 2a 0d   by adding:..**.
47895 0a 2a 2a 20 20 20 28 61 29 20 2b 31 20 70 6f 69  .**   (a) +1 poi
47896 6e 74 20 66 6f 72 20 65 61 63 68 20 6f 63 63 75  nt for each occu
47897 72 65 6e 63 65 20 6f 66 20 61 20 6d 61 74 63 68  rence of a match
47898 61 62 6c 65 20 70 68 72 61 73 65 20 69 6e 20 74  able phrase in t
47899 68 65 20 73 6e 69 70 70 65 74 2e 0d 0a 2a 2a 0d  he snippet...**.
4789a 0a 2a 2a 20 20 20 28 62 29 20 2b 31 30 30 30 20  .**   (b) +1000 
4789b 70 6f 69 6e 74 73 20 66 6f 72 20 74 68 65 20 66  points for the f
4789c 69 72 73 74 20 6f 63 63 75 72 65 6e 63 65 20 6f  irst occurence o
4789d 66 20 65 61 63 68 20 6d 61 74 63 68 61 62 6c 65  f each matchable
4789e 20 70 68 72 61 73 65 20 69 6e 20 0d 0a 2a 2a 20   phrase in ..** 
4789f 20 20 20 20 20 20 74 68 65 20 73 6e 69 70 70 65        the snippe
478a0 74 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  t for which the 
478a1 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6d 43  corresponding mC
478a2 6f 76 65 72 65 64 20 62 69 74 20 69 73 20 6e 6f  overed bit is no
478a3 74 20 73 65 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54  t set...**..** T
478a4 68 65 20 73 65 6c 65 63 74 65 64 20 73 6e 69 70  he selected snip
478a5 70 65 74 20 70 61 72 61 6d 65 74 65 72 73 20 61  pet parameters a
478a6 72 65 20 73 74 6f 72 65 64 20 69 6e 20 73 74 72  re stored in str
478a7 75 63 74 75 72 65 20 2a 70 46 72 61 67 6d 65 6e  ucture *pFragmen
478a8 74 20 62 65 66 6f 72 65 0d 0a 2a 2a 20 72 65 74  t before..** ret
478a9 75 72 6e 69 6e 67 2e 20 54 68 65 20 73 63 6f 72  urning. The scor
478aa 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 65  e of the selecte
478ab 64 20 73 6e 69 70 70 65 74 20 69 73 20 73 74 6f  d snippet is sto
478ac 72 65 64 20 69 6e 20 2a 70 69 53 63 6f 72 65 0d  red in *piScore.
478ad 0a 2a 2a 20 62 65 66 6f 72 65 20 72 65 74 75 72  .** before retur
478ae 6e 69 6e 67 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ning...*/..stati
478af 63 20 69 6e 74 20 66 74 73 33 42 65 73 74 53 6e  c int fts3BestSn
478b0 69 70 70 65 74 28 0d 0a 20 20 69 6e 74 20 6e 53  ippet(..  int nS
478b1 6e 69 70 70 65 74 2c 20 20 20 20 20 20 20 20 20  nippet,         
478b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73            /* Des
478b3 69 72 65 64 20 73 6e 69 70 70 65 74 20 6c 65 6e  ired snippet len
478b4 67 74 68 20 2a 2f 0d 0a 20 20 46 74 73 33 43 75  gth */..  Fts3Cu
478b5 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
478b6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
478b7 73 6f 72 20 74 6f 20 63 72 65 61 74 65 20 73 6e  sor to create sn
478b8 69 70 70 65 74 20 66 6f 72 20 2a 2f 0d 0a 20 20  ippet for */..  
478b9 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20  int iCol,       
478ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478bb 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
478bc 6d 6e 20 74 6f 20 63 72 65 61 74 65 20 73 6e 69  mn to create sni
478bd 70 70 65 74 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20  ppet from */..  
478be 75 36 34 20 6d 43 6f 76 65 72 65 64 2c 20 20 20  u64 mCovered,   
478bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478c0 2f 2a 20 4d 61 73 6b 20 6f 66 20 70 68 72 61 73  /* Mask of phras
478c1 65 73 20 61 6c 72 65 61 64 79 20 63 6f 76 65 72  es already cover
478c2 65 64 20 2a 2f 0d 0a 20 20 75 36 34 20 2a 70 6d  ed */..  u64 *pm
478c3 53 65 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Seen,           
478c4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
478c5 55 54 3a 20 4d 61 73 6b 20 6f 66 20 70 68 72 61  UT: Mask of phra
478c6 73 65 73 20 73 65 65 6e 20 2a 2f 0d 0a 20 20 53  ses seen */..  S
478c7 6e 69 70 70 65 74 46 72 61 67 6d 65 6e 74 20 2a  nippetFragment *
478c8 70 46 72 61 67 6d 65 6e 74 2c 20 20 20 20 20 2f  pFragment,     /
478c9 2a 20 4f 55 54 3a 20 42 65 73 74 20 73 6e 69 70  * OUT: Best snip
478ca 70 65 74 20 66 6f 75 6e 64 20 2a 2f 0d 0a 20 20  pet found */..  
478cb 69 6e 74 20 2a 70 69 53 63 6f 72 65 20 20 20 20  int *piScore    
478cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478cd 2f 2a 20 4f 55 54 3a 20 53 63 6f 72 65 20 6f 66  /* OUT: Score of
478ce 20 73 6e 69 70 70 65 74 20 70 46 72 61 67 6d 65   snippet pFragme
478cf 6e 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74  nt */..){..  int
478d0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
478d1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
478d2 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a  Return Code */..
478d3 20 20 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20 20    int nList;    
478d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478d5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
478d6 68 72 61 73 65 73 20 69 6e 20 65 78 70 72 65 73  hrases in expres
478d7 73 69 6f 6e 20 2a 2f 0d 0a 20 20 53 6e 69 70 70  sion */..  Snipp
478d8 65 74 49 74 65 72 20 73 49 74 65 72 3b 20 20 20  etIter sIter;   
478d9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
478da 65 72 61 74 65 73 20 74 68 72 6f 75 67 68 20 73  erates through s
478db 6e 69 70 70 65 74 20 63 61 6e 64 69 64 61 74 65  nippet candidate
478dc 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 42 79 74  s */..  int nByt
478dd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
478de 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
478df 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
478e0 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ace to allocate 
478e1 2a 2f 0d 0a 20 20 69 6e 74 20 69 42 65 73 74 53  */..  int iBestS
478e2 63 6f 72 65 20 3d 20 2d 31 3b 20 20 20 20 20 20  core = -1;      
478e3 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 73 6e        /* Best sn
478e4 69 70 70 65 74 20 73 63 6f 72 65 20 66 6f 75 6e  ippet score foun
478e5 64 20 73 6f 20 66 61 72 20 2a 2f 0d 0a 20 20 69  d so far */..  i
478e6 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
478e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
478e8 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
478e9 2f 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73  /....  memset(&s
478ea 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Iter, 0, sizeof(
478eb 73 49 74 65 72 29 29 3b 0d 0a 0d 0a 20 20 2f 2a  sIter));....  /*
478ec 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   Iterate through
478ed 20 74 68 65 20 70 68 72 61 73 65 73 20 69 6e 20   the phrases in 
478ee 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
478ef 6f 20 63 6f 75 6e 74 20 74 68 65 6d 2e 20 54 68  o count them. Th
478f0 65 20 73 61 6d 65 0d 0a 20 20 2a 2a 20 63 61 6c  e same..  ** cal
478f1 6c 62 61 63 6b 20 6d 61 6b 65 73 20 73 75 72 65  lback makes sure
478f2 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 61 72   the doclists ar
478f3 65 20 6c 6f 61 64 65 64 20 66 6f 72 20 65 61 63  e loaded for eac
478f4 68 20 70 68 72 61 73 65 2e 0d 0a 20 20 2a 2f 0d  h phrase...  */.
478f5 0a 20 20 72 63 20 3d 20 66 74 73 33 45 78 70 72  .  rc = fts3Expr
478f6 4c 6f 61 64 44 6f 63 6c 69 73 74 73 28 70 43 73  LoadDoclists(pCs
478f7 72 2c 20 26 6e 4c 69 73 74 2c 20 30 29 3b 0d 0a  r, &nList, 0);..
478f8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
478f9 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  _OK ){..    retu
478fa 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  rn rc;..  }.... 
478fb 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 69 74 20   /* Now that it 
478fc 69 73 20 6b 6e 6f 77 6e 20 68 6f 77 20 6d 61 6e  is known how man
478fd 79 20 70 68 72 61 73 65 73 20 74 68 65 72 65 20  y phrases there 
478fe 61 72 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e  are, allocate an
478ff 64 20 7a 65 72 6f 0d 0a 20 20 2a 2a 20 74 68 65  d zero..  ** the
47900 20 72 65 71 75 69 72 65 64 20 73 70 61 63 65 20   required space 
47901 75 73 69 6e 67 20 6d 61 6c 6c 6f 63 28 29 2e 0d  using malloc()..
47902 0a 20 20 2a 2f 0d 0a 20 20 6e 42 79 74 65 20 3d  .  */..  nByte =
47903 20 73 69 7a 65 6f 66 28 53 6e 69 70 70 65 74 50   sizeof(SnippetP
47904 68 72 61 73 65 29 20 2a 20 6e 4c 69 73 74 3b 0d  hrase) * nList;.
47905 0a 20 20 73 49 74 65 72 2e 61 50 68 72 61 73 65  .  sIter.aPhrase
47906 20 3d 20 28 53 6e 69 70 70 65 74 50 68 72 61 73   = (SnippetPhras
47907 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  e *)sqlite3_mall
47908 6f 63 28 6e 42 79 74 65 29 3b 0d 0a 20 20 69 66  oc(nByte);..  if
47909 28 20 21 73 49 74 65 72 2e 61 50 68 72 61 73 65  ( !sIter.aPhrase
4790a 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
4790b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
4790c 20 7d 0d 0a 20 20 6d 65 6d 73 65 74 28 73 49 74   }..  memset(sIt
4790d 65 72 2e 61 50 68 72 61 73 65 2c 20 30 2c 20 6e  er.aPhrase, 0, n
4790e 42 79 74 65 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49  Byte);....  /* I
4790f 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
47910 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 53 6e  ntents of the Sn
47911 69 70 70 65 74 49 74 65 72 20 6f 62 6a 65 63 74  ippetIter object
47912 2e 20 54 68 65 6e 20 69 74 65 72 61 74 65 20 74  . Then iterate t
47913 68 72 6f 75 67 68 0d 0a 20 20 2a 2a 20 74 68 65  hrough..  ** the
47914 20 73 65 74 20 6f 66 20 70 68 72 61 73 65 73 20   set of phrases 
47915 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
47916 6e 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68  n to populate th
47917 65 20 61 50 68 72 61 73 65 5b 5d 20 61 72 72 61  e aPhrase[] arra
47918 79 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 73 49 74 65  y...  */..  sIte
47919 72 2e 70 43 73 72 20 3d 20 70 43 73 72 3b 0d 0a  r.pCsr = pCsr;..
4791a 20 20 73 49 74 65 72 2e 69 43 6f 6c 20 3d 20 69    sIter.iCol = i
4791b 43 6f 6c 3b 0d 0a 20 20 73 49 74 65 72 2e 6e 53  Col;..  sIter.nS
4791c 6e 69 70 70 65 74 20 3d 20 6e 53 6e 69 70 70 65  nippet = nSnippe
4791d 74 3b 0d 0a 20 20 73 49 74 65 72 2e 6e 50 68 72  t;..  sIter.nPhr
4791e 61 73 65 20 3d 20 6e 4c 69 73 74 3b 0d 0a 20 20  ase = nList;..  
4791f 73 49 74 65 72 2e 69 43 75 72 72 65 6e 74 20 3d  sIter.iCurrent =
47920 20 2d 31 3b 0d 0a 20 20 28 76 6f 69 64 29 66 74   -1;..  (void)ft
47921 73 33 45 78 70 72 49 74 65 72 61 74 65 28 70 43  s3ExprIterate(pC
47922 73 72 2d 3e 70 45 78 70 72 2c 20 66 74 73 33 53  sr->pExpr, fts3S
47923 6e 69 70 70 65 74 46 69 6e 64 50 6f 73 69 74 69  nippetFindPositi
47924 6f 6e 73 2c 20 28 76 6f 69 64 20 2a 29 26 73 49  ons, (void *)&sI
47925 74 65 72 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 53 65  ter);....  /* Se
47926 74 20 74 68 65 20 2a 70 6d 53 65 65 6e 20 6f 75  t the *pmSeen ou
47927 74 70 75 74 20 76 61 72 69 61 62 6c 65 2e 20 2a  tput variable. *
47928 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  /..  for(i=0; i<
47929 6e 4c 69 73 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20  nList; i++){..  
4792a 20 20 69 66 28 20 73 49 74 65 72 2e 61 50 68 72    if( sIter.aPhr
4792b 61 73 65 5b 69 5d 2e 70 48 65 61 64 20 29 7b 0d  ase[i].pHead ){.
4792c 0a 20 20 20 20 20 20 2a 70 6d 53 65 65 6e 20 7c  .      *pmSeen |
4792d 3d 20 28 75 36 34 29 31 20 3c 3c 20 69 3b 0d 0a  = (u64)1 << i;..
4792e 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
4792f 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
47930 61 6c 6c 20 63 61 6e 64 69 64 61 74 65 20 73 6e  all candidate sn
47931 69 70 70 65 74 73 2e 20 53 74 6f 72 65 20 74 68  ippets. Store th
47932 65 20 62 65 73 74 20 73 6e 69 70 70 65 74 20 69  e best snippet i
47933 6e 20 0d 0a 20 20 2a 2a 20 2a 70 46 72 61 67 6d  n ..  ** *pFragm
47934 65 6e 74 2e 20 53 74 6f 72 65 20 69 74 73 20 61  ent. Store its a
47935 73 73 6f 63 69 61 74 65 64 20 27 73 63 6f 72 65  ssociated 'score
47936 27 20 69 6e 20 69 42 65 73 74 53 63 6f 72 65 2e  ' in iBestScore.
47937 0d 0a 20 20 2a 2f 0d 0a 20 20 70 46 72 61 67 6d  ..  */..  pFragm
47938 65 6e 74 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c  ent->iCol = iCol
47939 3b 0d 0a 20 20 77 68 69 6c 65 28 20 21 66 74 73  ;..  while( !fts
4793a 33 53 6e 69 70 70 65 74 4e 65 78 74 43 61 6e 64  3SnippetNextCand
4793b 69 64 61 74 65 28 26 73 49 74 65 72 29 20 29 7b  idate(&sIter) ){
4793c 0d 0a 20 20 20 20 69 6e 74 20 69 50 6f 73 3b 0d  ..    int iPos;.
4793d 0a 20 20 20 20 69 6e 74 20 69 53 63 6f 72 65 3b  .    int iScore;
4793e 0d 0a 20 20 20 20 75 36 34 20 6d 43 6f 76 65 72  ..    u64 mCover
4793f 3b 0d 0a 20 20 20 20 75 36 34 20 6d 48 69 67 68  ;..    u64 mHigh
47940 6c 69 67 68 74 3b 0d 0a 20 20 20 20 66 74 73 33  light;..    fts3
47941 53 6e 69 70 70 65 74 44 65 74 61 69 6c 73 28 26  SnippetDetails(&
47942 73 49 74 65 72 2c 20 6d 43 6f 76 65 72 65 64 2c  sIter, mCovered,
47943 20 26 69 50 6f 73 2c 20 26 69 53 63 6f 72 65 2c   &iPos, &iScore,
47944 20 26 6d 43 6f 76 65 72 2c 20 26 6d 48 69 67 68   &mCover, &mHigh
47945 6c 69 67 68 74 29 3b 0d 0a 20 20 20 20 61 73 73  light);..    ass
47946 65 72 74 28 20 69 53 63 6f 72 65 3e 3d 30 20 29  ert( iScore>=0 )
47947 3b 0d 0a 20 20 20 20 69 66 28 20 69 53 63 6f 72  ;..    if( iScor
47948 65 3e 69 42 65 73 74 53 63 6f 72 65 20 29 7b 0d  e>iBestScore ){.
47949 0a 20 20 20 20 20 20 70 46 72 61 67 6d 65 6e 74  .      pFragment
4794a 2d 3e 69 50 6f 73 20 3d 20 69 50 6f 73 3b 0d 0a  ->iPos = iPos;..
4794b 20 20 20 20 20 20 70 46 72 61 67 6d 65 6e 74 2d        pFragment-
4794c 3e 68 6c 6d 61 73 6b 20 3d 20 6d 48 69 67 68 6c  >hlmask = mHighl
4794d 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 70 46 72  ight;..      pFr
4794e 61 67 6d 65 6e 74 2d 3e 63 6f 76 65 72 65 64 20  agment->covered 
4794f 3d 20 6d 43 6f 76 65 72 3b 0d 0a 20 20 20 20 20  = mCover;..     
47950 20 69 42 65 73 74 53 63 6f 72 65 20 3d 20 69 53   iBestScore = iS
47951 63 6f 72 65 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  core;..    }..  
47952 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  }....  sqlite3_f
47953 72 65 65 28 73 49 74 65 72 2e 61 50 68 72 61 73  ree(sIter.aPhras
47954 65 29 3b 0d 0a 20 20 2a 70 69 53 63 6f 72 65 20  e);..  *piScore 
47955 3d 20 69 42 65 73 74 53 63 6f 72 65 3b 0d 0a 20  = iBestScore;.. 
47956 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
47957 4b 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  K;..}....../*..*
47958 2a 20 41 70 70 65 6e 64 20 61 20 73 74 72 69 6e  * Append a strin
47959 67 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 2d  g to the string-
4795a 62 75 66 66 65 72 20 70 61 73 73 65 64 20 61 73  buffer passed as
4795b 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
4795c 65 6e 74 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20  ent...**..** If 
4795d 6e 41 70 70 65 6e 64 20 69 73 20 6e 65 67 61 74  nAppend is negat
4795e 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 65  ive, then the le
4795f 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69  ngth of the stri
47960 6e 67 20 7a 41 70 70 65 6e 64 20 69 73 0d 0a 2a  ng zAppend is..*
47961 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69  * determined usi
47962 6e 67 20 73 74 72 6c 65 6e 28 29 2e 0d 0a 2a 2f  ng strlen()...*/
47963 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
47964 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28 0d 0a  3StringAppend(..
47965 20 20 53 74 72 42 75 66 66 65 72 20 2a 70 53 74    StrBuffer *pSt
47966 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
47967 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 61    /* Buffer to a
47968 70 70 65 6e 64 20 74 6f 20 2a 2f 0d 0a 20 20 63  ppend to */..  c
47969 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 70 70 65  onst char *zAppe
4796a 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  nd,            /
4796b 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  * Pointer to dat
4796c 61 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 62  a to append to b
4796d 75 66 66 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20  uffer */..  int 
4796e 6e 41 70 70 65 6e 64 20 20 20 20 20 20 20 20 20  nAppend         
4796f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
47970 69 7a 65 20 6f 66 20 7a 41 70 70 65 6e 64 20 69  ize of zAppend i
47971 6e 20 62 79 74 65 73 20 28 6f 72 20 2d 31 29 20  n bytes (or -1) 
47972 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 6e 41  */..){..  if( nA
47973 70 70 65 6e 64 3c 30 20 29 7b 0d 0a 20 20 20 20  ppend<0 ){..    
47974 6e 41 70 70 65 6e 64 20 3d 20 28 69 6e 74 29 73  nAppend = (int)s
47975 74 72 6c 65 6e 28 7a 41 70 70 65 6e 64 29 3b 0d  trlen(zAppend);.
47976 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20  .  }....  /* If 
47977 74 68 65 72 65 20 69 73 20 69 6e 73 75 66 66 69  there is insuffi
47978 63 69 65 6e 74 20 73 70 61 63 65 20 61 6c 6c 6f  cient space allo
47979 63 61 74 65 64 20 61 74 20 53 74 72 42 75 66 66  cated at StrBuff
4797a 65 72 2e 7a 2c 20 75 73 65 20 72 65 61 6c 6c 6f  er.z, use reallo
4797b 63 28 29 0d 0a 20 20 2a 2a 20 74 6f 20 67 72 6f  c()..  ** to gro
4797c 77 20 74 68 65 20 62 75 66 66 65 72 20 75 6e 74  w the buffer unt
4797d 69 6c 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  il so that it is
4797e 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 61   big enough to a
4797f 63 63 6f 6d 61 64 61 74 65 20 74 68 65 0d 0a 20  ccomadate the.. 
47980 20 2a 2a 20 61 70 70 65 6e 64 65 64 20 64 61 74   ** appended dat
47981 61 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  a...  */..  if( 
47982 70 53 74 72 2d 3e 6e 2b 6e 41 70 70 65 6e 64 2b  pStr->n+nAppend+
47983 31 3e 3d 70 53 74 72 2d 3e 6e 41 6c 6c 6f 63 20  1>=pStr->nAlloc 
47984 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c  ){..    int nAll
47985 6f 63 20 3d 20 70 53 74 72 2d 3e 6e 41 6c 6c 6f  oc = pStr->nAllo
47986 63 2b 6e 41 70 70 65 6e 64 2b 31 30 30 3b 0d 0a  c+nAppend+100;..
47987 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d      char *zNew =
47988 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
47989 28 70 53 74 72 2d 3e 7a 2c 20 6e 41 6c 6c 6f 63  (pStr->z, nAlloc
4798a 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 7a 4e 65  );..    if( !zNe
4798b 77 20 29 7b 0d 0a 20 20 20 20 20 20 72 65 74 75  w ){..      retu
4798c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
4798d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 70 53 74  ..    }..    pSt
4798e 72 2d 3e 7a 20 3d 20 7a 4e 65 77 3b 0d 0a 20 20  r->z = zNew;..  
4798f 20 20 70 53 74 72 2d 3e 6e 41 6c 6c 6f 63 20 3d    pStr->nAlloc =
47990 20 6e 41 6c 6c 6f 63 3b 0d 0a 20 20 7d 0d 0a 0d   nAlloc;..  }...
47991 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65  .  /* Append the
47992 20 64 61 74 61 20 74 6f 20 74 68 65 20 73 74 72   data to the str
47993 69 6e 67 20 62 75 66 66 65 72 2e 20 2a 2f 0d 0a  ing buffer. */..
47994 20 20 6d 65 6d 63 70 79 28 26 70 53 74 72 2d 3e    memcpy(&pStr->
47995 7a 5b 70 53 74 72 2d 3e 6e 5d 2c 20 7a 41 70 70  z[pStr->n], zApp
47996 65 6e 64 2c 20 6e 41 70 70 65 6e 64 29 3b 0d 0a  end, nAppend);..
47997 20 20 70 53 74 72 2d 3e 6e 20 2b 3d 20 6e 41 70    pStr->n += nAp
47998 70 65 6e 64 3b 0d 0a 20 20 70 53 74 72 2d 3e 7a  pend;..  pStr->z
47999 5b 70 53 74 72 2d 3e 6e 5d 20 3d 20 27 5c 30 27  [pStr->n] = '\0'
4799a 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51  ;....  return SQ
4799b 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f  LITE_OK;..}..../
4799c 2a 0d 0a 2a 2a 20 54 68 65 20 66 74 73 33 42 65  *..** The fts3Be
4799d 73 74 53 6e 69 70 70 65 74 28 29 20 66 75 6e 63  stSnippet() func
4799e 74 69 6f 6e 20 6f 66 74 65 6e 20 73 65 6c 65 63  tion often selec
4799f 74 73 20 73 6e 69 70 70 65 74 73 20 74 68 61 74  ts snippets that
479a0 20 65 6e 64 20 77 69 74 68 20 61 0d 0a 2a 2a 20   end with a..** 
479a1 71 75 65 72 79 20 74 65 72 6d 2e 20 54 68 61 74  query term. That
479a2 20 69 73 2c 20 74 68 65 20 66 69 6e 61 6c 20 74   is, the final t
479a3 65 72 6d 20 6f 66 20 74 68 65 20 73 6e 69 70 70  erm of the snipp
479a4 65 74 20 69 73 20 61 6c 77 61 79 73 20 61 20 74  et is always a t
479a5 65 72 6d 0d 0a 2a 2a 20 74 68 61 74 20 72 65 71  erm..** that req
479a6 75 69 72 65 73 20 68 69 67 68 6c 69 67 68 74 69  uires highlighti
479a7 6e 67 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  ng. For example,
479a8 20 69 66 20 27 58 27 20 69 73 20 61 20 68 69 67   if 'X' is a hig
479a9 68 6c 69 67 68 74 65 64 20 74 65 72 6d 0d 0a 2a  hlighted term..*
479aa 2a 20 61 6e 64 20 27 2e 27 20 69 73 20 61 20 6e  * and '.' is a n
479ab 6f 6e 2d 68 69 67 68 6c 69 67 68 74 65 64 20 74  on-highlighted t
479ac 65 72 6d 2c 20 42 65 73 74 53 6e 69 70 70 65 74  erm, BestSnippet
479ad 28 29 20 6d 61 79 20 73 65 6c 65 63 74 3a 0d 0a  () may select:..
479ae 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2e 2e 2e 2e 2e  **..**     .....
479af 2e 2e 2e 58 2e 2e 2e 2e 2e 58 0d 0a 2a 2a 0d 0a  ...X.....X..**..
479b0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
479b1 20 22 73 68 69 66 74 73 22 20 74 68 65 20 62 65   "shifts" the be
479b2 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 73  ginning of the s
479b3 6e 69 70 70 65 74 20 66 6f 72 77 61 72 64 20 69  nippet forward i
479b4 6e 20 74 68 65 20 0d 0a 2a 2a 20 64 6f 63 75 6d  n the ..** docum
479b5 65 6e 74 20 73 6f 20 74 68 61 74 20 74 68 65 72  ent so that ther
479b6 65 20 61 72 65 20 61 70 70 72 6f 78 69 6d 61 74  e are approximat
479b7 65 6c 79 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ely the same num
479b8 62 65 72 20 6f 66 20 0d 0a 2a 2a 20 6e 6f 6e 2d  ber of ..** non-
479b9 68 69 67 68 6c 69 67 68 74 65 64 20 74 65 72 6d  highlighted term
479ba 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f  s to the right o
479bb 66 20 74 68 65 20 66 69 6e 61 6c 20 68 69 67 68  f the final high
479bc 6c 69 67 68 74 65 64 20 74 65 72 6d 20 61 73 20  lighted term as 
479bd 74 68 65 72 65 0d 0a 2a 2a 20 61 72 65 20 74 6f  there..** are to
479be 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
479bf 20 66 69 72 73 74 20 68 69 67 68 6c 69 67 68 74   first highlight
479c0 65 64 20 74 65 72 6d 2e 20 46 6f 72 20 65 78 61  ed term. For exa
479c1 6d 70 6c 65 2c 20 74 6f 20 74 68 69 73 3a 0d 0a  mple, to this:..
479c2 2a 2a 0d 0a 2a 2a 20 20 20 20 20 2e 2e 2e 2e 58  **..**     ....X
479c3 2e 2e 2e 2e 2e 58 2e 2e 2e 2e 0d 0a 2a 2a 0d 0a  .....X......**..
479c4 2a 2a 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  ** This is done 
479c5 61 73 20 70 61 72 74 20 6f 66 20 65 78 74 72 61  as part of extra
479c6 63 74 69 6e 67 20 74 68 65 20 73 6e 69 70 70 65  cting the snippe
479c7 74 20 74 65 78 74 2c 20 6e 6f 74 20 77 68 65 6e  t text, not when
479c8 20 73 65 6c 65 63 74 69 6e 67 0d 0a 2a 2a 20 74   selecting..** t
479c9 68 65 20 73 6e 69 70 70 65 74 2e 20 53 6e 69 70  he snippet. Snip
479ca 70 65 74 20 73 65 6c 65 63 74 69 6f 6e 20 69 73  pet selection is
479cb 20 64 6f 6e 65 20 62 61 73 65 64 20 6f 6e 20 64   done based on d
479cc 6f 63 6c 69 73 74 73 20 6f 6e 6c 79 2c 20 73 6f  oclists only, so
479cd 20 74 68 65 72 65 0d 0a 2a 2a 20 69 73 20 6e 6f   there..** is no
479ce 20 77 61 79 20 66 6f 72 20 66 74 73 33 42 65 73   way for fts3Bes
479cf 74 53 6e 69 70 70 65 74 28 29 20 74 6f 20 6b 6e  tSnippet() to kn
479d0 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ow whether or no
479d1 74 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 0d  t the document .
479d2 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e  .** actually con
479d3 74 61 69 6e 73 20 74 65 72 6d 73 20 74 68 61 74  tains terms that
479d4 20 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61   follow the fina
479d5 6c 20 68 69 67 68 6c 69 67 68 74 65 64 20 74 65  l highlighted te
479d6 72 6d 2e 20 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  rm. ..*/..static
479d7 20 69 6e 74 20 66 74 73 33 53 6e 69 70 70 65 74   int fts3Snippet
479d8 53 68 69 66 74 28 0d 0a 20 20 46 74 73 33 54 61  Shift(..  Fts3Ta
479d9 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
479da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
479db 33 20 74 61 62 6c 65 20 73 6e 69 70 70 65 74 20  3 table snippet 
479dc 63 6f 6d 65 73 20 66 72 6f 6d 20 2a 2f 0d 0a 20  comes from */.. 
479dd 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 2c 20 20   int nSnippet,  
479de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479df 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f   /* Number of to
479e0 6b 65 6e 73 20 64 65 73 69 72 65 64 20 66 6f 72  kens desired for
479e1 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 20 20 63   snippet */..  c
479e2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c  onst char *zDoc,
479e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
479e4 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65 78 74 20  * Document text 
479e5 74 6f 20 65 78 74 72 61 63 74 20 73 6e 69 70 70  to extract snipp
479e6 65 74 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 69 6e  et from */..  in
479e7 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
479e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
479e9 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
479ea 7a 44 6f 63 20 69 6e 20 62 79 74 65 73 20 2a 2f  zDoc in bytes */
479eb 0d 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 2c 20  ..  int *piPos, 
479ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479ed 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 46      /* IN/OUT: F
479ee 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66 20 73 6e  irst token of sn
479ef 69 70 70 65 74 20 2a 2f 0d 0a 20 20 75 36 34 20  ippet */..  u64 
479f0 2a 70 48 6c 6d 61 73 6b 20 20 20 20 20 20 20 20  *pHlmask        
479f1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
479f2 4e 2f 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 74  N/OUT: Mask of t
479f3 6f 6b 65 6e 73 20 74 6f 20 68 69 67 68 6c 69 67  okens to highlig
479f4 68 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 75 36 34  ht */..){..  u64
479f5 20 68 6c 6d 61 73 6b 20 3d 20 2a 70 48 6c 6d 61   hlmask = *pHlma
479f6 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sk;          /* 
479f7 4c 6f 63 61 6c 20 63 6f 70 79 20 6f 66 20 69 6e  Local copy of in
479f8 69 74 69 61 6c 20 68 69 67 68 6c 69 67 68 74 2d  itial highlight-
479f9 6d 61 73 6b 20 2a 2f 0d 0a 0d 0a 20 20 69 66 28  mask */....  if(
479fa 20 68 6c 6d 61 73 6b 20 29 7b 0d 0a 20 20 20 20   hlmask ){..    
479fb 69 6e 74 20 6e 4c 65 66 74 3b 20 20 20 20 20 20  int nLeft;      
479fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
479fd 20 54 6f 6b 65 6e 73 20 74 6f 20 74 68 65 20 6c   Tokens to the l
479fe 65 66 74 20 6f 66 20 66 69 72 73 74 20 68 69 67  eft of first hig
479ff 68 6c 69 67 68 74 20 2a 2f 0d 0a 20 20 20 20 69  hlight */..    i
47a00 6e 74 20 6e 52 69 67 68 74 3b 20 20 20 20 20 20  nt nRight;      
47a01 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47a02 54 6f 6b 65 6e 73 20 74 6f 20 74 68 65 20 72 69  Tokens to the ri
47a03 67 68 74 20 6f 66 20 6c 61 73 74 20 68 69 67 68  ght of last high
47a04 6c 69 67 68 74 20 2a 2f 0d 0a 20 20 20 20 69 6e  light */..    in
47a05 74 20 6e 44 65 73 69 72 65 64 3b 20 20 20 20 20  t nDesired;     
47a06 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
47a07 64 65 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  deal number of t
47a08 6f 6b 65 6e 73 20 74 6f 20 73 68 69 66 74 20 66  okens to shift f
47a09 6f 72 77 61 72 64 20 2a 2f 0d 0a 0d 0a 20 20 20  orward */....   
47a0a 20 66 6f 72 28 6e 4c 65 66 74 3d 30 3b 20 21 28   for(nLeft=0; !(
47a0b 68 6c 6d 61 73 6b 20 26 20 28 28 75 36 34 29 31  hlmask & ((u64)1
47a0c 20 3c 3c 20 6e 4c 65 66 74 29 29 3b 20 6e 4c 65   << nLeft)); nLe
47a0d 66 74 2b 2b 29 3b 0d 0a 20 20 20 20 66 6f 72 28  ft++);..    for(
47a0e 6e 52 69 67 68 74 3d 30 3b 20 21 28 68 6c 6d 61  nRight=0; !(hlma
47a0f 73 6b 20 26 20 28 28 75 36 34 29 31 20 3c 3c 20  sk & ((u64)1 << 
47a10 28 6e 53 6e 69 70 70 65 74 2d 31 2d 6e 52 69 67  (nSnippet-1-nRig
47a11 68 74 29 29 29 3b 20 6e 52 69 67 68 74 2b 2b 29  ht))); nRight++)
47a12 3b 0d 0a 20 20 20 20 6e 44 65 73 69 72 65 64 20  ;..    nDesired 
47a13 3d 20 28 6e 4c 65 66 74 2d 6e 52 69 67 68 74 29  = (nLeft-nRight)
47a14 2f 32 3b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 64  /2;....    /* Id
47a15 65 61 6c 6c 79 2c 20 74 68 65 20 73 74 61 72 74  eally, the start
47a16 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 20   of the snippet 
47a17 73 68 6f 75 6c 64 20 62 65 20 70 75 73 68 65 64  should be pushed
47a18 20 66 6f 72 77 61 72 64 20 69 6e 20 74 68 65 0d   forward in the.
47a19 0a 20 20 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  .    ** document
47a1a 20 6e 44 65 73 69 72 65 64 20 74 6f 6b 65 6e 73   nDesired tokens
47a1b 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 68 65  . This block che
47a1c 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65  cks if there are
47a1d 20 61 63 74 75 61 6c 6c 79 0d 0a 20 20 20 20 2a   actually..    *
47a1e 2a 20 6e 44 65 73 69 72 65 64 20 74 6f 6b 65 6e  * nDesired token
47a1f 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f  s to the right o
47a20 66 20 74 68 65 20 73 6e 69 70 70 65 74 2e 20 49  f the snippet. I
47a21 66 20 73 6f 2c 20 2a 70 69 50 6f 73 20 61 6e 64  f so, *piPos and
47a22 0d 0a 20 20 20 20 2a 2a 20 2a 70 48 6c 4d 61 73  ..    ** *pHlMas
47a23 6b 20 61 72 65 20 75 70 64 61 74 65 64 20 74 6f  k are updated to
47a24 20 73 68 69 66 74 20 74 68 65 20 73 6e 69 70 70   shift the snipp
47a25 65 74 20 6e 44 65 73 69 72 65 64 20 74 6f 6b 65  et nDesired toke
47a26 6e 73 20 74 6f 20 74 68 65 0d 0a 20 20 20 20 2a  ns to the..    *
47a27 2a 20 72 69 67 68 74 2e 20 4f 74 68 65 72 77 69  * right. Otherwi
47a28 73 65 2c 20 74 68 65 20 73 6e 69 70 70 65 74 20  se, the snippet 
47a29 69 73 20 73 68 69 66 74 65 64 20 62 79 20 74 68  is shifted by th
47a2a 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  e number of toke
47a2b 6e 73 0d 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c  ns..    ** avail
47a2c 61 62 6c 65 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20  able...    */.. 
47a2d 20 20 20 69 66 28 20 6e 44 65 73 69 72 65 64 3e     if( nDesired>
47a2e 30 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20  0 ){..      int 
47a2f 6e 53 68 69 66 74 3b 20 20 20 20 20 20 20 20 20  nShift;         
47a30 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
47a31 72 20 6f 66 20 74 6f 6b 65 6e 73 20 74 6f 20 73  r of tokens to s
47a32 68 69 66 74 20 73 6e 69 70 70 65 74 20 62 79 20  hift snippet by 
47a33 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 43  */..      int iC
47a34 75 72 72 65 6e 74 20 3d 20 30 3b 20 20 20 20 20  urrent = 0;     
47a35 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 63        /* Token c
47a36 6f 75 6e 74 65 72 20 2a 2f 0d 0a 20 20 20 20 20  ounter */..     
47a37 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
47a38 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47a39 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a  Return Code */..
47a3a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f        sqlite3_to
47a3b 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
47a3c 70 4d 6f 64 3b 0d 0a 20 20 20 20 20 20 73 71 6c  pMod;..      sql
47a3d 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
47a3e 75 72 73 6f 72 20 2a 70 43 3b 0d 0a 20 20 20 20  ursor *pC;..    
47a3f 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65    pMod = (sqlite
47a40 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
47a41 6c 65 20 2a 29 70 54 61 62 2d 3e 70 54 6f 6b 65  le *)pTab->pToke
47a42 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0d  nizer->pModule;.
47a43 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e  ...      /* Open
47a44 20 61 20 63 75 72 73 6f 72 20 6f 6e 20 7a 44 6f   a cursor on zDo
47a45 63 2f 6e 44 6f 63 2e 20 43 68 65 63 6b 20 69 66  c/nDoc. Check if
47a46 20 74 68 65 72 65 20 61 72 65 20 28 6e 53 6e 69   there are (nSni
47a47 70 70 65 74 2b 6e 44 65 73 69 72 65 64 29 0d 0a  ppet+nDesired)..
47a48 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65        ** or more
47a49 20 74 6f 6b 65 6e 73 20 69 6e 20 7a 44 6f 63 2f   tokens in zDoc/
47a4a 6e 44 6f 63 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  nDoc...      */.
47a4b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  .      rc = pMod
47a4c 2d 3e 78 4f 70 65 6e 28 70 54 61 62 2d 3e 70 54  ->xOpen(pTab->pT
47a4d 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44 6f 63 2c 20  okenizer, zDoc, 
47a4e 6e 44 6f 63 2c 20 26 70 43 29 3b 0d 0a 20 20 20  nDoc, &pC);..   
47a4f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
47a50 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20  E_OK ){..       
47a51 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20   return rc;..   
47a52 20 20 20 7d 0d 0a 20 20 20 20 20 20 70 43 2d 3e     }..      pC->
47a53 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 61  pTokenizer = pTa
47a54 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a  b->pTokenizer;..
47a55 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d        while( rc=
47a56 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43  =SQLITE_OK && iC
47a57 75 72 72 65 6e 74 3c 28 6e 53 6e 69 70 70 65 74  urrent<(nSnippet
47a58 2b 6e 44 65 73 69 72 65 64 29 20 29 7b 0d 0a 20  +nDesired) ){.. 
47a59 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
47a5a 72 20 2a 5a 44 55 4d 4d 59 3b 20 69 6e 74 20 44  r *ZDUMMY; int D
47a5b 55 4d 4d 59 31 2c 20 44 55 4d 4d 59 32 2c 20 44  UMMY1, DUMMY2, D
47a5c 55 4d 4d 59 33 3b 0d 0a 20 20 20 20 20 20 20 20  UMMY3;..        
47a5d 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65 78 74  rc = pMod->xNext
47a5e 28 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20 26 44  (pC, &ZDUMMY, &D
47a5f 55 4d 4d 59 31 2c 20 26 44 55 4d 4d 59 32 2c 20  UMMY1, &DUMMY2, 
47a60 26 44 55 4d 4d 59 33 2c 20 26 69 43 75 72 72 65  &DUMMY3, &iCurre
47a61 6e 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  nt);..      }.. 
47a62 20 20 20 20 20 70 4d 6f 64 2d 3e 78 43 6c 6f 73       pMod->xClos
47a63 65 28 70 43 29 3b 0d 0a 20 20 20 20 20 20 69 66  e(pC);..      if
47a64 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
47a65 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  && rc!=SQLITE_DO
47a66 4e 45 20 29 7b 20 72 65 74 75 72 6e 20 72 63 3b  NE ){ return rc;
47a67 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 6e 53 68 69   }....      nShi
47a68 66 74 20 3d 20 28 72 63 3d 3d 53 51 4c 49 54 45  ft = (rc==SQLITE
47a69 5f 44 4f 4e 45 29 2b 69 43 75 72 72 65 6e 74 2d  _DONE)+iCurrent-
47a6a 6e 53 6e 69 70 70 65 74 3b 0d 0a 20 20 20 20 20  nSnippet;..     
47a6b 20 61 73 73 65 72 74 28 20 6e 53 68 69 66 74 3c   assert( nShift<
47a6c 3d 6e 44 65 73 69 72 65 64 20 29 3b 0d 0a 20 20  =nDesired );..  
47a6d 20 20 20 20 69 66 28 20 6e 53 68 69 66 74 3e 30      if( nShift>0
47a6e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a 70 69   ){..        *pi
47a6f 50 6f 73 20 2b 3d 20 6e 53 68 69 66 74 3b 0d 0a  Pos += nShift;..
47a70 20 20 20 20 20 20 20 20 2a 70 48 6c 6d 61 73 6b          *pHlmask
47a71 20 3d 20 68 6c 6d 61 73 6b 20 3e 3e 20 6e 53 68   = hlmask >> nSh
47a72 69 66 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ift;..      }.. 
47a73 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65 74     }..  }..  ret
47a74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  urn SQLITE_OK;..
47a75 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 45 78 74 72  }..../*..** Extr
47a76 61 63 74 20 74 68 65 20 73 6e 69 70 70 65 74 20  act the snippet 
47a77 74 65 78 74 20 66 6f 72 20 66 72 61 67 6d 65 6e  text for fragmen
47a78 74 20 70 46 72 61 67 6d 65 6e 74 20 66 72 6f 6d  t pFragment from
47a79 20 63 75 72 73 6f 72 20 70 43 73 72 20 61 6e 64   cursor pCsr and
47a7a 0d 0a 2a 2a 20 61 70 70 65 6e 64 20 69 74 20 74  ..** append it t
47a7b 6f 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20  o string buffer 
47a7c 70 4f 75 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  pOut...*/..stati
47a7d 63 20 69 6e 74 20 66 74 73 33 53 6e 69 70 70 65  c int fts3Snippe
47a7e 74 54 65 78 74 28 0d 0a 20 20 46 74 73 33 43 75  tText(..  Fts3Cu
47a7f 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20  rsor *pCsr,     
47a80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
47a81 33 20 43 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 53  3 Cursor */..  S
47a82 6e 69 70 70 65 74 46 72 61 67 6d 65 6e 74 20 2a  nippetFragment *
47a83 70 46 72 61 67 6d 65 6e 74 2c 20 20 20 20 20 2f  pFragment,     /
47a84 2a 20 53 6e 69 70 70 65 74 20 74 6f 20 65 78 74  * Snippet to ext
47a85 72 61 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69  ract */..  int i
47a86 46 72 61 67 6d 65 6e 74 2c 20 20 20 20 20 20 20  Fragment,       
47a87 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72             /* Fr
47a88 61 67 6d 65 6e 74 20 6e 75 6d 62 65 72 20 2a 2f  agment number */
47a89 0d 0a 20 20 69 6e 74 20 69 73 4c 61 73 74 2c 20  ..  int isLast, 
47a8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a8b 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
47a8c 66 69 6e 61 6c 20 66 72 61 67 6d 65 6e 74 20 69  final fragment i
47a8d 6e 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 20 20  n snippet */..  
47a8e 69 6e 74 20 6e 53 6e 69 70 70 65 74 2c 20 20 20  int nSnippet,   
47a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b  /* Number of tok
47a91 65 6e 73 20 69 6e 20 65 78 74 72 61 63 74 65 64  ens in extracted
47a92 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 20 20 63   snippet */..  c
47a93 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 70 65 6e  onst char *zOpen
47a94 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
47a95 2a 20 53 74 72 69 6e 67 20 69 6e 73 65 72 74 65  * String inserte
47a96 64 20 62 65 66 6f 72 65 20 68 69 67 68 6c 69 67  d before highlig
47a97 68 74 65 64 20 74 65 72 6d 20 2a 2f 0d 0a 20 20  hted term */..  
47a98 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6c 6f  const char *zClo
47a99 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
47a9a 2f 2a 20 53 74 72 69 6e 67 20 69 6e 73 65 72 74  /* String insert
47a9b 65 64 20 61 66 74 65 72 20 68 69 67 68 6c 69 67  ed after highlig
47a9c 68 74 65 64 20 74 65 72 6d 20 2a 2f 0d 0a 20 20  hted term */..  
47a9d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c  const char *zEll
47a9e 69 70 73 69 73 2c 20 20 20 20 20 20 20 20 20 20  ipsis,          
47a9f 2f 2a 20 53 74 72 69 6e 67 20 69 6e 73 65 72 74  /* String insert
47aa0 65 64 20 62 65 74 77 65 65 6e 20 73 6e 69 70 70  ed between snipp
47aa1 65 74 73 20 2a 2f 0d 0a 20 20 53 74 72 42 75 66  ets */..  StrBuf
47aa2 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 20  fer *pOut       
47aa3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
47aa4 74 65 20 6f 75 74 70 75 74 20 68 65 72 65 20 2a  te output here *
47aa5 2f 0d 0a 29 7b 0d 0a 20 20 46 74 73 33 54 61 62  /..){..  Fts3Tab
47aa6 6c 65 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33  le *pTab = (Fts3
47aa7 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61  Table *)pCsr->ba
47aa8 73 65 2e 70 56 74 61 62 3b 0d 0a 20 20 69 6e 74  se.pVtab;..  int
47aa9 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
47aaa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47aab 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
47aac 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
47aad 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  oc;             
47aae 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65    /* Document te
47aaf 78 74 20 74 6f 20 65 78 74 72 61 63 74 20 73 6e  xt to extract sn
47ab0 69 70 70 65 74 20 66 72 6f 6d 20 2a 2f 0d 0a 20  ippet from */.. 
47ab1 20 69 6e 74 20 6e 44 6f 63 3b 20 20 20 20 20 20   int nDoc;      
47ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ab3 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 44 6f 63   /* Size of zDoc
47ab4 20 69 6e 20 62 79 74 65 73 20 2a 2f 0d 0a 20 20   in bytes */..  
47ab5 69 6e 74 20 69 43 75 72 72 65 6e 74 20 3d 20 30  int iCurrent = 0
47ab6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47ab7 2f 2a 20 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e  /* Current token
47ab8 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63 75 6d   number of docum
47ab9 65 6e 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 45  ent */..  int iE
47aba 6e 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nd = 0;         
47abb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
47abc 65 20 6f 66 66 73 65 74 20 6f 66 20 65 6e 64 20  e offset of end 
47abd 6f 66 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e  of current token
47abe 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 53 68 69   */..  int isShi
47abf 66 74 44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20  ftDone = 0;     
47ac0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
47ac1 66 74 65 72 20 73 6e 69 70 70 65 74 20 69 73 20  fter snippet is 
47ac2 73 68 69 66 74 65 64 20 2a 2f 0d 0a 20 20 69 6e  shifted */..  in
47ac3 74 20 69 50 6f 73 20 3d 20 70 46 72 61 67 6d 65  t iPos = pFragme
47ac4 6e 74 2d 3e 69 50 6f 73 3b 20 20 20 20 20 2f 2a  nt->iPos;     /*
47ac5 20 46 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66 20   First token of 
47ac6 73 6e 69 70 70 65 74 20 2a 2f 0d 0a 20 20 75 36  snippet */..  u6
47ac7 34 20 68 6c 6d 61 73 6b 20 3d 20 70 46 72 61 67  4 hlmask = pFrag
47ac8 6d 65 6e 74 2d 3e 68 6c 6d 61 73 6b 3b 20 2f 2a  ment->hlmask; /*
47ac9 20 48 69 67 68 6c 69 67 68 74 2d 6d 61 73 6b 20   Highlight-mask 
47aca 66 6f 72 20 73 6e 69 70 70 65 74 20 2a 2f 0d 0a  for snippet */..
47acb 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 46 72    int iCol = pFr
47acc 61 67 6d 65 6e 74 2d 3e 69 43 6f 6c 2b 31 3b 20  agment->iCol+1; 
47acd 20 20 2f 2a 20 51 75 65 72 79 20 63 6f 6c 75 6d    /* Query colum
47ace 6e 20 74 6f 20 65 78 74 72 61 63 74 20 74 65 78  n to extract tex
47acf 74 20 66 72 6f 6d 20 2a 2f 0d 0a 20 20 73 71 6c  t from */..  sql
47ad0 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
47ad1 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 20 2f 2a 20  odule *pMod; /* 
47ad2 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65  Tokenizer module
47ad3 20 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20   methods object 
47ad4 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  */..  sqlite3_to
47ad5 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
47ad6 70 43 3b 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a  pC;   /* Tokeniz
47ad7 65 72 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 6f  er cursor open o
47ad8 6e 20 7a 44 6f 63 2f 6e 44 6f 63 20 2a 2f 0d 0a  n zDoc/nDoc */..
47ad9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 5a 44    const char *ZD
47ada 55 4d 4d 59 3b 20 20 20 20 20 20 20 20 20 20 20  UMMY;           
47adb 20 20 2f 2a 20 44 75 6d 6d 79 20 61 72 67 75 6d    /* Dummy argum
47adc 65 6e 74 20 75 73 65 64 20 77 69 74 68 20 74 6f  ent used with to
47add 6b 65 6e 69 7a 65 72 20 2a 2f 0d 0a 20 20 69 6e  kenizer */..  in
47ade 74 20 44 55 4d 4d 59 31 3b 20 20 20 20 20 20 20  t DUMMY1;       
47adf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47ae0 20 44 75 6d 6d 79 20 61 72 67 75 6d 65 6e 74 20   Dummy argument 
47ae1 75 73 65 64 20 77 69 74 68 20 74 6f 6b 65 6e 69  used with tokeni
47ae2 7a 65 72 20 2a 2f 0d 0a 20 20 0d 0a 20 20 7a 44  zer */..  ..  zD
47ae3 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  oc = (const char
47ae4 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
47ae5 6e 5f 74 65 78 74 28 70 43 73 72 2d 3e 70 53 74  n_text(pCsr->pSt
47ae6 6d 74 2c 20 69 43 6f 6c 29 3b 0d 0a 20 20 69 66  mt, iCol);..  if
47ae7 28 20 7a 44 6f 63 3d 3d 30 20 29 7b 0d 0a 20 20  ( zDoc==0 ){..  
47ae8 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
47ae9 6c 75 6d 6e 5f 74 79 70 65 28 70 43 73 72 2d 3e  lumn_type(pCsr->
47aea 70 53 74 6d 74 2c 20 69 43 6f 6c 29 21 3d 53 51  pStmt, iCol)!=SQ
47aeb 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20  LITE_NULL ){..  
47aec 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
47aed 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 7d 0d  E_NOMEM;..    }.
47aee 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
47aef 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20 6e  TE_OK;..  }..  n
47af0 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Doc = sqlite3_co
47af1 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 73 72 2d  lumn_bytes(pCsr-
47af2 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 29 3b 0d 0a  >pStmt, iCol);..
47af3 0d 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20 74 6f  ..  /* Open a to
47af4 6b 65 6e 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ken cursor on th
47af5 65 20 64 6f 63 75 6d 65 6e 74 2e 20 2a 2f 0d 0a  e document. */..
47af6 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65    pMod = (sqlite
47af7 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
47af8 6c 65 20 2a 29 70 54 61 62 2d 3e 70 54 6f 6b 65  le *)pTab->pToke
47af9 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0d  nizer->pModule;.
47afa 0a 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4f  .  rc = pMod->xO
47afb 70 65 6e 28 70 54 61 62 2d 3e 70 54 6f 6b 65 6e  pen(pTab->pToken
47afc 69 7a 65 72 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63  izer, zDoc, nDoc
47afd 2c 20 26 70 43 29 3b 0d 0a 20 20 69 66 28 20 72  , &pC);..  if( r
47afe 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
47aff 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  .    return rc;.
47b00 0a 20 20 7d 0d 0a 20 20 70 43 2d 3e 70 54 6f 6b  .  }..  pC->pTok
47b01 65 6e 69 7a 65 72 20 3d 20 70 54 61 62 2d 3e 70  enizer = pTab->p
47b02 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 0d 0a 20 20  Tokenizer;....  
47b03 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
47b04 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 6e 74  E_OK ){..    int
47b05 20 69 42 65 67 69 6e 3b 20 20 20 20 20 20 20 20   iBegin;        
47b06 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
47b07 66 73 65 74 20 69 6e 20 7a 44 6f 63 20 6f 66 20  fset in zDoc of 
47b08 73 74 61 72 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  start of token *
47b09 2f 0d 0a 20 20 20 20 69 6e 74 20 69 46 69 6e 3b  /..    int iFin;
47b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b0b 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
47b0c 6e 20 7a 44 6f 63 20 6f 66 20 65 6e 64 20 6f 66  n zDoc of end of
47b0d 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 20 20 69   token */..    i
47b0e 6e 74 20 69 73 48 69 67 68 6c 69 67 68 74 3b 20  nt isHighlight; 
47b0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47b10 54 72 75 65 20 66 6f 72 20 68 69 67 68 6c 69 67  True for highlig
47b11 68 74 65 64 20 74 65 72 6d 73 20 2a 2f 0d 0a 0d  hted terms */...
47b12 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e  .    rc = pMod->
47b13 78 4e 65 78 74 28 70 43 2c 20 26 5a 44 55 4d 4d  xNext(pC, &ZDUMM
47b14 59 2c 20 26 44 55 4d 4d 59 31 2c 20 26 69 42 65  Y, &DUMMY1, &iBe
47b15 67 69 6e 2c 20 26 69 46 69 6e 2c 20 26 69 43 75  gin, &iFin, &iCu
47b16 72 72 65 6e 74 29 3b 0d 0a 20 20 20 20 69 66 28  rrent);..    if(
47b17 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
47b18 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  {..      if( rc=
47b19 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0d  =SQLITE_DONE ){.
47b1a 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63  .        /* Spec
47b1b 69 61 6c 20 63 61 73 65 20 2d 20 74 68 65 20 6c  ial case - the l
47b1c 61 73 74 20 74 6f 6b 65 6e 20 6f 66 20 74 68 65  ast token of the
47b1d 20 73 6e 69 70 70 65 74 20 69 73 20 61 6c 73 6f   snippet is also
47b1e 20 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0d   the last token.
47b1f 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
47b20 68 65 20 63 6f 6c 75 6d 6e 2e 20 41 70 70 65 6e  he column. Appen
47b21 64 20 61 6e 79 20 70 75 6e 63 74 75 61 74 69 6f  d any punctuatio
47b22 6e 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20  n that occurred 
47b23 62 65 74 77 65 65 6e 20 74 68 65 20 65 6e 64 0d  between the end.
47b24 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
47b25 68 65 20 70 72 65 76 69 6f 75 73 20 74 6f 6b 65  he previous toke
47b26 6e 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f 66  n and the end of
47b27 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 74 6f   the document to
47b28 20 74 68 65 20 6f 75 74 70 75 74 2e 20 0d 0a 20   the output. .. 
47b29 20 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 62         ** Then b
47b2a 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
47b2b 6c 6f 6f 70 2e 20 2a 2f 0d 0a 20 20 20 20 20 20  loop. */..      
47b2c 20 20 72 63 20 3d 20 66 74 73 33 53 74 72 69 6e    rc = fts3Strin
47b2d 67 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 26 7a  gAppend(pOut, &z
47b2e 44 6f 63 5b 69 45 6e 64 5d 2c 20 2d 31 29 3b 0d  Doc[iEnd], -1);.
47b2f 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
47b30 62 72 65 61 6b 3b 0d 0a 20 20 20 20 7d 0d 0a 20  break;..    }.. 
47b31 20 20 20 69 66 28 20 69 43 75 72 72 65 6e 74 3c     if( iCurrent<
47b32 69 50 6f 73 20 29 7b 20 63 6f 6e 74 69 6e 75 65  iPos ){ continue
47b33 3b 20 7d 0d 0a 0d 0a 20 20 20 20 69 66 28 20 21  ; }....    if( !
47b34 69 73 53 68 69 66 74 44 6f 6e 65 20 29 7b 0d 0a  isShiftDone ){..
47b35 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 6e 44        int n = nD
47b36 6f 63 20 2d 20 69 42 65 67 69 6e 3b 0d 0a 20 20  oc - iBegin;..  
47b37 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 6e 69      rc = fts3Sni
47b38 70 70 65 74 53 68 69 66 74 28 70 54 61 62 2c 20  ppetShift(pTab, 
47b39 6e 53 6e 69 70 70 65 74 2c 20 26 7a 44 6f 63 5b  nSnippet, &zDoc[
47b3a 69 42 65 67 69 6e 5d 2c 20 6e 2c 20 26 69 50 6f  iBegin], n, &iPo
47b3b 73 2c 20 26 68 6c 6d 61 73 6b 29 3b 0d 0a 20 20  s, &hlmask);..  
47b3c 20 20 20 20 69 73 53 68 69 66 74 44 6f 6e 65 20      isShiftDone 
47b3d 3d 20 31 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a  = 1;....      /*
47b3e 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20 73 68   Now that the sh
47b3f 69 66 74 20 68 61 73 20 62 65 65 6e 20 64 6f 6e  ift has been don
47b40 65 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65 20  e, check if the 
47b41 69 6e 69 74 69 61 6c 20 22 2e 2e 2e 22 20 61 72  initial "..." ar
47b42 65 0d 0a 20 20 20 20 20 20 2a 2a 20 72 65 71 75  e..      ** requ
47b43 69 72 65 64 2e 20 54 68 65 79 20 61 72 65 20 72  ired. They are r
47b44 65 71 75 69 72 65 64 20 69 66 20 28 61 29 20 74  equired if (a) t
47b45 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 66  his is not the f
47b46 69 72 73 74 20 66 72 61 67 6d 65 6e 74 2c 0d 0a  irst fragment,..
47b47 20 20 20 20 20 20 2a 2a 20 6f 72 20 28 62 29 20        ** or (b) 
47b48 74 68 69 73 20 66 72 61 67 6d 65 6e 74 20 64 6f  this fragment do
47b49 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 61 74 20  es not begin at 
47b4a 70 6f 73 69 74 69 6f 6e 20 30 20 6f 66 20 69 74  position 0 of it
47b4b 73 20 63 6f 6c 75 6d 6e 2e 20 0d 0a 20 20 20 20  s column. ..    
47b4c 20 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28 20    */..      if( 
47b4d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
47b4e 20 28 69 50 6f 73 3e 30 20 7c 7c 20 69 46 72 61   (iPos>0 || iFra
47b4f 67 6d 65 6e 74 3e 30 29 20 29 7b 0d 0a 20 20 20  gment>0) ){..   
47b50 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 74       rc = fts3St
47b51 72 69 6e 67 41 70 70 65 6e 64 28 70 4f 75 74 2c  ringAppend(pOut,
47b52 20 7a 45 6c 6c 69 70 73 69 73 2c 20 2d 31 29 3b   zEllipsis, -1);
47b53 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
47b54 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
47b55 4f 4b 20 7c 7c 20 69 43 75 72 72 65 6e 74 3c 69  OK || iCurrent<i
47b56 50 6f 73 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0d  Pos ) continue;.
47b57 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69 66  .    }....    if
47b58 28 20 69 43 75 72 72 65 6e 74 3e 3d 28 69 50 6f  ( iCurrent>=(iPo
47b59 73 2b 6e 53 6e 69 70 70 65 74 29 20 29 7b 0d 0a  s+nSnippet) ){..
47b5a 20 20 20 20 20 20 69 66 28 20 69 73 4c 61 73 74        if( isLast
47b5b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
47b5c 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70 70 65  = fts3StringAppe
47b5d 6e 64 28 70 4f 75 74 2c 20 7a 45 6c 6c 69 70 73  nd(pOut, zEllips
47b5e 69 73 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 20 20  is, -1);..      
47b5f 7d 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  }..      break;.
47b60 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
47b61 20 53 65 74 20 69 73 48 69 67 68 6c 69 67 68 74   Set isHighlight
47b62 20 74 6f 20 74 72 75 65 20 69 66 20 74 68 69 73   to true if this
47b63 20 74 65 72 6d 20 73 68 6f 75 6c 64 20 62 65 20   term should be 
47b64 68 69 67 68 6c 69 67 68 74 65 64 2e 20 2a 2f 0d  highlighted. */.
47b65 0a 20 20 20 20 69 73 48 69 67 68 6c 69 67 68 74  .    isHighlight
47b66 20 3d 20 28 68 6c 6d 61 73 6b 20 26 20 28 28 75   = (hlmask & ((u
47b67 36 34 29 31 20 3c 3c 20 28 69 43 75 72 72 65 6e  64)1 << (iCurren
47b68 74 2d 69 50 6f 73 29 29 29 21 3d 30 3b 0d 0a 0d  t-iPos)))!=0;...
47b69 0a 20 20 20 20 69 66 28 20 69 43 75 72 72 65 6e  .    if( iCurren
47b6a 74 3e 69 50 6f 73 20 29 20 72 63 20 3d 20 66 74  t>iPos ) rc = ft
47b6b 73 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28 70  s3StringAppend(p
47b6c 4f 75 74 2c 20 26 7a 44 6f 63 5b 69 45 6e 64 5d  Out, &zDoc[iEnd]
47b6d 2c 20 69 42 65 67 69 6e 2d 69 45 6e 64 29 3b 0d  , iBegin-iEnd);.
47b6e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
47b6f 49 54 45 5f 4f 4b 20 26 26 20 69 73 48 69 67 68  ITE_OK && isHigh
47b70 6c 69 67 68 74 20 29 20 72 63 20 3d 20 66 74 73  light ) rc = fts
47b71 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28 70 4f  3StringAppend(pO
47b72 75 74 2c 20 7a 4f 70 65 6e 2c 20 2d 31 29 3b 0d  ut, zOpen, -1);.
47b73 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
47b74 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 66 74  ITE_OK ) rc = ft
47b75 73 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28 70  s3StringAppend(p
47b76 4f 75 74 2c 20 26 7a 44 6f 63 5b 69 42 65 67 69  Out, &zDoc[iBegi
47b77 6e 5d 2c 20 69 46 69 6e 2d 69 42 65 67 69 6e 29  n], iFin-iBegin)
47b78 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
47b79 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73 48 69  QLITE_OK && isHi
47b7a 67 68 6c 69 67 68 74 20 29 20 72 63 20 3d 20 66  ghlight ) rc = f
47b7b 74 73 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28  ts3StringAppend(
47b7c 70 4f 75 74 2c 20 7a 43 6c 6f 73 65 2c 20 2d 31  pOut, zClose, -1
47b7d 29 3b 0d 0a 0d 0a 20 20 20 20 69 45 6e 64 20 3d  );....    iEnd =
47b7e 20 69 46 69 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20   iFin;..  }.... 
47b7f 20 70 4d 6f 64 2d 3e 78 43 6c 6f 73 65 28 70 43   pMod->xClose(pC
47b80 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
47b81 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ..}....../*..** 
47b82 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
47b83 20 75 73 65 64 20 74 6f 20 63 6f 75 6e 74 20 74   used to count t
47b84 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20  he entries in a 
47b85 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 28 61 20 0d  column-list (a .
47b86 0a 2a 2a 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65  .** delta-encode
47b87 64 20 6c 69 73 74 20 6f 66 20 74 65 72 6d 20 6f  d list of term o
47b88 66 66 73 65 74 73 20 77 69 74 68 69 6e 20 61 20  ffsets within a 
47b89 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66  single column of
47b8a 20 61 20 73 69 6e 67 6c 65 20 0d 0a 2a 2a 20 72   a single ..** r
47b8b 6f 77 29 2e 20 57 68 65 6e 20 74 68 69 73 20 66  ow). When this f
47b8c 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
47b8d 64 2c 20 2a 70 70 43 6f 6c 6c 69 73 74 20 73 68  d, *ppCollist sh
47b8e 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 74 68  ould point to th
47b8f 65 0d 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  e..** beginning 
47b90 6f 66 20 74 68 65 20 66 69 72 73 74 20 76 61 72  of the first var
47b91 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d  int in the colum
47b92 6e 2d 6c 69 73 74 20 28 74 68 65 20 76 61 72 69  n-list (the vari
47b93 6e 74 20 74 68 61 74 0d 0a 2a 2a 20 63 6f 6e 74  nt that..** cont
47b94 61 69 6e 73 20 74 68 65 20 70 6f 73 69 74 69 6f  ains the positio
47b95 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6d  n of the first m
47b96 61 74 63 68 69 6e 67 20 74 65 72 6d 20 69 6e 20  atching term in 
47b97 74 68 65 20 63 6f 6c 75 6d 6e 20 64 61 74 61 29  the column data)
47b98 2e 0d 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74  ...** Before ret
47b99 75 72 6e 69 6e 67 2c 20 2a 70 70 43 6f 6c 6c 69  urning, *ppColli
47b9a 73 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  st is set to poi
47b9b 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
47b9c 62 79 74 65 20 61 66 74 65 72 0d 0a 2a 2a 20 74  byte after..** t
47b9d 68 65 20 6c 61 73 74 20 76 61 72 69 6e 74 20 69  he last varint i
47b9e 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73  n the column-lis
47b9f 74 20 28 65 69 74 68 65 72 20 74 68 65 20 30 78  t (either the 0x
47ba0 30 30 20 73 69 67 6e 69 66 79 69 6e 67 20 74 68  00 signifying th
47ba1 65 20 65 6e 64 0d 0a 2a 2a 20 6f 66 20 74 68 65  e end..** of the
47ba2 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2c 20   position-list, 
47ba3 6f 72 20 74 68 65 20 30 78 30 31 20 74 68 61 74  or the 0x01 that
47ba4 20 70 72 65 63 65 64 65 73 20 74 68 65 20 63 6f   precedes the co
47ba5 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 0d 0a  lumn number of..
47ba6 2a 2a 20 74 68 65 20 6e 65 78 74 20 63 6f 6c 75  ** the next colu
47ba7 6d 6e 20 69 6e 20 74 68 65 20 70 6f 73 69 74 69  mn in the positi
47ba8 6f 6e 2d 6c 69 73 74 29 2e 0d 0a 2a 2a 0d 0a 2a  on-list)...**..*
47ba9 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
47baa 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
47bab 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69 73 20 72  column-list is r
47bac 65 74 75 72 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74  eturned...*/..st
47bad 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 6f 6c  atic int fts3Col
47bae 75 6d 6e 6c 69 73 74 43 6f 75 6e 74 28 63 68 61  umnlistCount(cha
47baf 72 20 2a 2a 70 70 43 6f 6c 6c 69 73 74 29 7b 0d  r **ppCollist){.
47bb0 0a 20 20 63 68 61 72 20 2a 70 45 6e 64 20 3d 20  .  char *pEnd = 
47bb1 2a 70 70 43 6f 6c 6c 69 73 74 3b 0d 0a 20 20 63  *ppCollist;..  c
47bb2 68 61 72 20 63 20 3d 20 30 3b 0d 0a 20 20 69 6e  har c = 0;..  in
47bb3 74 20 6e 45 6e 74 72 79 20 3d 20 30 3b 0d 0a 0d  t nEntry = 0;...
47bb4 0a 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e 2d 6c  .  /* A column-l
47bb5 69 73 74 20 69 73 20 74 65 72 6d 69 6e 61 74 65  ist is terminate
47bb6 64 20 62 79 20 65 69 74 68 65 72 20 61 20 30 78  d by either a 0x
47bb7 30 31 20 6f 72 20 30 78 30 30 2e 20 2a 2f 0d 0a  01 or 0x00. */..
47bb8 20 20 77 68 69 6c 65 28 20 30 78 46 45 20 26 20    while( 0xFE & 
47bb9 28 2a 70 45 6e 64 20 7c 20 63 29 20 29 7b 0d 0a  (*pEnd | c) ){..
47bba 20 20 20 20 63 20 3d 20 2a 70 45 6e 64 2b 2b 20      c = *pEnd++ 
47bbb 26 20 30 78 38 30 3b 0d 0a 20 20 20 20 69 66 28  & 0x80;..    if(
47bbc 20 21 63 20 29 20 6e 45 6e 74 72 79 2b 2b 3b 0d   !c ) nEntry++;.
47bbd 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 70 43 6f 6c  .  }....  *ppCol
47bbe 6c 69 73 74 20 3d 20 70 45 6e 64 3b 0d 0a 20 20  list = pEnd;..  
47bbf 72 65 74 75 72 6e 20 6e 45 6e 74 72 79 3b 0d 0a  return nEntry;..
47bc0 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 66 74 73 33  }..../*..** fts3
47bc1 45 78 70 72 49 74 65 72 61 74 65 28 29 20 63 61  ExprIterate() ca
47bc2 6c 6c 62 61 63 6b 20 75 73 65 64 20 74 6f 20 63  llback used to c
47bc3 6f 6c 6c 65 63 74 20 74 68 65 20 22 67 6c 6f 62  ollect the "glob
47bc4 61 6c 22 20 6d 61 74 63 68 69 6e 66 6f 20 73 74  al" matchinfo st
47bc5 61 74 73 0d 0a 2a 2a 20 66 6f 72 20 61 20 73 69  ats..** for a si
47bc6 6e 67 6c 65 20 71 75 65 72 79 2e 20 0d 0a 2a 2a  ngle query. ..**
47bc7 0d 0a 2a 2a 20 66 74 73 33 45 78 70 72 49 74 65  ..** fts3ExprIte
47bc8 72 61 74 65 28 29 20 63 61 6c 6c 62 61 63 6b 20  rate() callback 
47bc9 74 6f 20 6c 6f 61 64 20 74 68 65 20 27 67 6c 6f  to load the 'glo
47bca 62 61 6c 27 20 65 6c 65 6d 65 6e 74 73 20 6f 66  bal' elements of
47bcb 20 61 0d 0a 2a 2a 20 46 54 53 33 5f 4d 41 54 43   a..** FTS3_MATC
47bcc 48 49 4e 46 4f 5f 48 49 54 53 20 6d 61 74 63 68  HINFO_HITS match
47bcd 69 6e 66 6f 20 61 72 72 61 79 2e 20 54 68 65 20  info array. The 
47bce 67 6c 6f 62 61 6c 20 73 74 61 74 73 20 61 72 65  global stats are
47bcf 20 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 20   those elements 
47bd0 0d 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 61 74 63  ..** of the matc
47bd1 68 69 6e 66 6f 20 61 72 72 61 79 20 74 68 61 74  hinfo array that
47bd2 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 66 6f   are constant fo
47bd3 72 20 61 6c 6c 20 72 6f 77 73 20 72 65 74 75 72  r all rows retur
47bd4 6e 65 64 20 62 79 20 74 68 65 20 0d 0a 2a 2a 20  ned by the ..** 
47bd5 63 75 72 72 65 6e 74 20 71 75 65 72 79 2e 0d 0a  current query...
47bd6 2a 2a 0d 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  **..** Argument 
47bd7 70 43 74 78 20 69 73 20 61 63 74 75 61 6c 6c 79  pCtx is actually
47bd8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
47bd9 73 74 72 75 63 74 20 6f 66 20 74 79 70 65 20 4d  struct of type M
47bda 61 74 63 68 49 6e 66 6f 2e 20 54 68 69 73 0d 0a  atchInfo. This..
47bdb 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 6f 70 75  ** function popu
47bdc 6c 61 74 65 73 20 4d 61 74 63 68 69 6e 66 6f 2e  lates Matchinfo.
47bdd 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 61 73 20  aMatchinfo[] as 
47bde 66 6f 6c 6c 6f 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  follows:..**..**
47bdf 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
47be0 43 6f 6c 3c 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  Col<nCol; iCol++
47be1 29 7b 0d 0a 2a 2a 20 20 20 20 20 61 4d 61 74 63  ){..**     aMatc
47be2 68 69 6e 66 6f 5b 33 2a 69 50 68 72 61 73 65 2a  hinfo[3*iPhrase*
47be3 6e 43 6f 6c 20 2b 20 33 2a 69 43 6f 6c 20 2b 20  nCol + 3*iCol + 
47be4 31 5d 20 3d 20 58 3b 0d 0a 2a 2a 20 20 20 20 20  1] = X;..**     
47be5 61 4d 61 74 63 68 69 6e 66 6f 5b 33 2a 69 50 68  aMatchinfo[3*iPh
47be6 72 61 73 65 2a 6e 43 6f 6c 20 2b 20 33 2a 69 43  rase*nCol + 3*iC
47be7 6f 6c 20 2b 20 32 5d 20 3d 20 59 3b 0d 0a 2a 2a  ol + 2] = Y;..**
47be8 20 20 20 7d 0d 0a 2a 2a 0d 0a 2a 2a 20 77 68 65     }..**..** whe
47be9 72 65 20 58 20 69 73 20 74 68 65 20 6e 75 6d 62  re X is the numb
47bea 65 72 20 6f 66 20 6d 61 74 63 68 65 73 20 66 6f  er of matches fo
47beb 72 20 70 68 72 61 73 65 20 69 50 68 72 61 73 65  r phrase iPhrase
47bec 20 69 73 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20   is column iCol 
47bed 6f 66 20 61 6c 6c 0d 0a 2a 2a 20 72 6f 77 73 20  of all..** rows 
47bee 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 59 20  of the table. Y 
47bef 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
47bf0 20 72 6f 77 73 20 66 6f 72 20 77 68 69 63 68 20   rows for which 
47bf1 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 63 6f 6e 74  column iCol cont
47bf2 61 69 6e 73 0d 0a 2a 2a 20 61 74 20 6c 65 61 73  ains..** at leas
47bf3 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f  t one instance o
47bf4 66 20 70 68 72 61 73 65 20 69 50 68 72 61 73 65  f phrase iPhrase
47bf5 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65  ...**..** If the
47bf6 20 70 68 72 61 73 65 20 70 45 78 70 72 20 63 6f   phrase pExpr co
47bf7 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20  nsists entirely 
47bf8 6f 66 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65  of deferred toke
47bf9 6e 73 2c 20 74 68 65 6e 20 61 6c 6c 20 58 20 61  ns, then all X a
47bfa 6e 64 0d 0a 2a 2a 20 59 20 76 61 6c 75 65 73 20  nd..** Y values 
47bfb 61 72 65 20 73 65 74 20 74 6f 20 6e 44 6f 63 2c  are set to nDoc,
47bfc 20 77 68 65 72 65 20 6e 44 6f 63 20 69 73 20 74   where nDoc is t
47bfd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63  he number of doc
47bfe 75 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 0d 0a  uments in the ..
47bff 2a 2a 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20  ** file system. 
47c00 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63  This is done bec
47c01 61 75 73 65 20 74 68 65 20 66 75 6c 6c 2d 74 65  ause the full-te
47c02 78 74 20 69 6e 64 65 78 20 64 6f 63 6c 69 73 74  xt index doclist
47c03 20 69 73 20 72 65 71 75 69 72 65 64 0d 0a 2a 2a   is required..**
47c04 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 74 68   to calculate th
47c05 65 73 65 20 76 61 6c 75 65 73 20 70 72 6f 70 65  ese values prope
47c06 72 6c 79 2c 20 61 6e 64 20 74 68 65 20 66 75 6c  rly, and the ful
47c07 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 64 6f 63  l-text index doc
47c08 6c 69 73 74 20 69 73 0d 0a 2a 2a 20 6e 6f 74 20  list is..** not 
47c09 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 64 65  available for de
47c0a 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 2e 0d 0a  ferred tokens...
47c0b 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  */..static int f
47c0c 74 73 33 45 78 70 72 47 6c 6f 62 61 6c 48 69 74  ts3ExprGlobalHit
47c0d 73 43 62 28 0d 0a 20 20 46 74 73 33 45 78 70 72  sCb(..  Fts3Expr
47c0e 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
47c0f 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73          /* Phras
47c10 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  e expression nod
47c11 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 68 72  e */..  int iPhr
47c12 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ase,            
47c13 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73          /* Phras
47c14 65 20 6e 75 6d 62 65 72 20 28 6e 75 6d 62 65 72  e number (number
47c15 65 64 20 66 72 6f 6d 20 7a 65 72 6f 29 20 2a 2f  ed from zero) */
47c16 0d 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20  ..  void *pCtx  
47c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c18 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
47c19 6f 20 4d 61 74 63 68 49 6e 66 6f 20 73 74 72 75  o MatchInfo stru
47c1a 63 74 75 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  cture */..){..  
47c1b 4d 61 74 63 68 49 6e 66 6f 20 2a 70 20 3d 20 28  MatchInfo *p = (
47c1c 4d 61 74 63 68 49 6e 66 6f 20 2a 29 70 43 74 78  MatchInfo *)pCtx
47c1d 3b 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ;..  return sqli
47c1e 74 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73  te3Fts3EvalPhras
47c1f 65 53 74 61 74 73 28 0d 0a 20 20 20 20 20 20 70  eStats(..      p
47c20 2d 3e 70 43 75 72 73 6f 72 2c 20 70 45 78 70 72  ->pCursor, pExpr
47c21 2c 20 26 70 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  , &p->aMatchinfo
47c22 5b 33 2a 69 50 68 72 61 73 65 2a 70 2d 3e 6e 43  [3*iPhrase*p->nC
47c23 6f 6c 5d 0d 0a 20 20 29 3b 0d 0a 7d 0d 0a 0d 0a  ol]..  );..}....
47c24 2f 2a 0d 0a 2a 2a 20 66 74 73 33 45 78 70 72 49  /*..** fts3ExprI
47c25 74 65 72 61 74 65 28 29 20 63 61 6c 6c 62 61 63  terate() callbac
47c26 6b 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63  k used to collec
47c27 74 20 74 68 65 20 22 6c 6f 63 61 6c 22 20 70 61  t the "local" pa
47c28 72 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 46 54  rt of the..** FT
47c29 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 48 49 54  S3_MATCHINFO_HIT
47c2a 53 20 61 72 72 61 79 2e 20 54 68 65 20 6c 6f 63  S array. The loc
47c2b 61 6c 20 73 74 61 74 73 20 61 72 65 20 74 68 6f  al stats are tho
47c2c 73 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  se elements of t
47c2d 68 65 20 0d 0a 2a 2a 20 61 72 72 61 79 20 74 68  he ..** array th
47c2e 61 74 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  at are different
47c2f 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 72 65   for each row re
47c30 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 71 75  turned by the qu
47c31 65 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ery...*/..static
47c32 20 69 6e 74 20 66 74 73 33 45 78 70 72 4c 6f 63   int fts3ExprLoc
47c33 61 6c 48 69 74 73 43 62 28 0d 0a 20 20 46 74 73  alHitsCb(..  Fts
47c34 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
47c35 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47c36 50 68 72 61 73 65 20 65 78 70 72 65 73 73 69 6f  Phrase expressio
47c37 6e 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e 74  n node */..  int
47c38 20 69 50 68 72 61 73 65 2c 20 20 20 20 20 20 20   iPhrase,       
47c39 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47c3a 50 68 72 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  Phrase number */
47c3b 0d 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20  ..  void *pCtx  
47c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c3d 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
47c3e 6f 20 4d 61 74 63 68 49 6e 66 6f 20 73 74 72 75  o MatchInfo stru
47c3f 63 74 75 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  cture */..){..  
47c40 4d 61 74 63 68 49 6e 66 6f 20 2a 70 20 3d 20 28  MatchInfo *p = (
47c41 4d 61 74 63 68 49 6e 66 6f 20 2a 29 70 43 74 78  MatchInfo *)pCtx
47c42 3b 0d 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20  ;..  int iStart 
47c43 3d 20 69 50 68 72 61 73 65 20 2a 20 70 2d 3e 6e  = iPhrase * p->n
47c44 43 6f 6c 20 2a 20 33 3b 0d 0a 20 20 69 6e 74 20  Col * 3;..  int 
47c45 69 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d 30 3b  i;....  for(i=0;
47c46 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29   i<p->nCol; i++)
47c47 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 70 43 73  {..    char *pCs
47c48 72 3b 0d 0a 20 20 20 20 70 43 73 72 20 3d 20 73  r;..    pCsr = s
47c49 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50 68  qlite3Fts3EvalPh
47c4a 72 61 73 65 50 6f 73 6c 69 73 74 28 70 2d 3e 70  rasePoslist(p->p
47c4b 43 75 72 73 6f 72 2c 20 70 45 78 70 72 2c 20 69  Cursor, pExpr, i
47c4c 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 73 72  );..    if( pCsr
47c4d 20 29 7b 0d 0a 20 20 20 20 20 20 70 2d 3e 61 4d   ){..      p->aM
47c4e 61 74 63 68 69 6e 66 6f 5b 69 53 74 61 72 74 2b  atchinfo[iStart+
47c4f 69 2a 33 5d 20 3d 20 66 74 73 33 43 6f 6c 75 6d  i*3] = fts3Colum
47c50 6e 6c 69 73 74 43 6f 75 6e 74 28 26 70 43 73 72  nlistCount(&pCsr
47c51 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
47c52 20 20 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 69        p->aMatchi
47c53 6e 66 6f 5b 69 53 74 61 72 74 2b 69 2a 33 5d 20  nfo[iStart+i*3] 
47c54 3d 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  = 0;..    }..  }
47c55 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ....  return SQL
47c56 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 73 74  ITE_OK;..}....st
47c57 61 74 69 63 20 69 6e 74 20 66 74 73 33 4d 61 74  atic int fts3Mat
47c58 63 68 69 6e 66 6f 43 68 65 63 6b 28 0d 0a 20 20  chinfoCheck(..  
47c59 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c  Fts3Table *pTab,
47c5a 20 0d 0a 20 20 63 68 61 72 20 63 41 72 67 2c 0d   ..  char cArg,.
47c5b 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0d  .  char **pzErr.
47c5c 0a 29 7b 0d 0a 20 20 69 66 28 20 28 63 41 72 67  .){..  if( (cArg
47c5d 3d 3d 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f  ==FTS3_MATCHINFO
47c5e 5f 4e 50 48 52 41 53 45 29 0d 0a 20 20 20 7c 7c  _NPHRASE)..   ||
47c5f 20 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54   (cArg==FTS3_MAT
47c60 43 48 49 4e 46 4f 5f 4e 43 4f 4c 29 0d 0a 20 20  CHINFO_NCOL)..  
47c61 20 7c 7c 20 28 63 41 72 67 3d 3d 46 54 53 33 5f   || (cArg==FTS3_
47c62 4d 41 54 43 48 49 4e 46 4f 5f 4e 44 4f 43 20 26  MATCHINFO_NDOC &
47c63 26 20 70 54 61 62 2d 3e 62 48 61 73 53 74 61 74  & pTab->bHasStat
47c64 29 0d 0a 20 20 20 7c 7c 20 28 63 41 72 67 3d 3d  )..   || (cArg==
47c65 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 41  FTS3_MATCHINFO_A
47c66 56 47 4c 45 4e 47 54 48 20 26 26 20 70 54 61 62  VGLENGTH && pTab
47c67 2d 3e 62 48 61 73 53 74 61 74 29 0d 0a 20 20 20  ->bHasStat)..   
47c68 7c 7c 20 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d  || (cArg==FTS3_M
47c69 41 54 43 48 49 4e 46 4f 5f 4c 45 4e 47 54 48 20  ATCHINFO_LENGTH 
47c6a 26 26 20 70 54 61 62 2d 3e 62 48 61 73 44 6f 63  && pTab->bHasDoc
47c6b 73 69 7a 65 29 0d 0a 20 20 20 7c 7c 20 28 63 41  size)..   || (cA
47c6c 72 67 3d 3d 46 54 53 33 5f 4d 41 54 43 48 49 4e  rg==FTS3_MATCHIN
47c6d 46 4f 5f 4c 43 53 29 0d 0a 20 20 20 7c 7c 20 28  FO_LCS)..   || (
47c6e 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54 43 48  cArg==FTS3_MATCH
47c6f 49 4e 46 4f 5f 48 49 54 53 29 0d 0a 20 20 29 7b  INFO_HITS)..  ){
47c70 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
47c71 49 54 45 5f 4f 4b 3b 0d 0a 20 20 7d 0d 0a 20 20  ITE_OK;..  }..  
47c72 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
47c73 5f 6d 70 72 69 6e 74 66 28 22 75 6e 72 65 63 6f  _mprintf("unreco
47c74 67 6e 69 7a 65 64 20 6d 61 74 63 68 69 6e 66 6f  gnized matchinfo
47c75 20 72 65 71 75 65 73 74 3a 20 25 63 22 2c 20 63   request: %c", c
47c76 41 72 67 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  Arg);..  return 
47c77 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 7d  SQLITE_ERROR;..}
47c78 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  ....static int f
47c79 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 69 7a 65  ts3MatchinfoSize
47c7a 28 4d 61 74 63 68 49 6e 66 6f 20 2a 70 49 6e 66  (MatchInfo *pInf
47c7b 6f 2c 20 63 68 61 72 20 63 41 72 67 29 7b 0d 0a  o, char cArg){..
47c7c 20 20 69 6e 74 20 6e 56 61 6c 3b 20 20 20 20 20    int nVal;     
47c7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c7e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
47c7f 6e 74 65 67 65 72 73 20 6f 75 74 70 75 74 20 62  ntegers output b
47c80 79 20 63 41 72 67 20 2a 2f 0d 0a 0d 0a 20 20 73  y cArg */....  s
47c81 77 69 74 63 68 28 20 63 41 72 67 20 29 7b 0d 0a  witch( cArg ){..
47c82 20 20 20 20 63 61 73 65 20 46 54 53 33 5f 4d 41      case FTS3_MA
47c83 54 43 48 49 4e 46 4f 5f 4e 44 4f 43 3a 0d 0a 20  TCHINFO_NDOC:.. 
47c84 20 20 20 63 61 73 65 20 46 54 53 33 5f 4d 41 54     case FTS3_MAT
47c85 43 48 49 4e 46 4f 5f 4e 50 48 52 41 53 45 3a 20  CHINFO_NPHRASE: 
47c86 0d 0a 20 20 20 20 63 61 73 65 20 46 54 53 33 5f  ..    case FTS3_
47c87 4d 41 54 43 48 49 4e 46 4f 5f 4e 43 4f 4c 3a 20  MATCHINFO_NCOL: 
47c88 0d 0a 20 20 20 20 20 20 6e 56 61 6c 20 3d 20 31  ..      nVal = 1
47c89 3b 0d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  ;..      break;.
47c8a 0a 0d 0a 20 20 20 20 63 61 73 65 20 46 54 53 33  ...    case FTS3
47c8b 5f 4d 41 54 43 48 49 4e 46 4f 5f 41 56 47 4c 45  _MATCHINFO_AVGLE
47c8c 4e 47 54 48 3a 0d 0a 20 20 20 20 63 61 73 65 20  NGTH:..    case 
47c8d 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c  FTS3_MATCHINFO_L
47c8e 45 4e 47 54 48 3a 0d 0a 20 20 20 20 63 61 73 65  ENGTH:..    case
47c8f 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f   FTS3_MATCHINFO_
47c90 4c 43 53 3a 0d 0a 20 20 20 20 20 20 6e 56 61 6c  LCS:..      nVal
47c91 20 3d 20 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 0d   = pInfo->nCol;.
47c92 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d  .      break;...
47c93 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0d 0a 20  .    default:.. 
47c94 20 20 20 20 20 61 73 73 65 72 74 28 20 63 41 72       assert( cAr
47c95 67 3d 3d 46 54 53 33 5f 4d 41 54 43 48 49 4e 46  g==FTS3_MATCHINF
47c96 4f 5f 48 49 54 53 20 29 3b 0d 0a 20 20 20 20 20  O_HITS );..     
47c97 20 6e 56 61 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6e   nVal = pInfo->n
47c98 43 6f 6c 20 2a 20 70 49 6e 66 6f 2d 3e 6e 50 68  Col * pInfo->nPh
47c99 72 61 73 65 20 2a 20 33 3b 0d 0a 20 20 20 20 20  rase * 3;..     
47c9a 20 62 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a   break;..  }....
47c9b 20 20 72 65 74 75 72 6e 20 6e 56 61 6c 3b 0d 0a    return nVal;..
47c9c 7d 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  }....static int 
47c9d 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 65 6c  fts3MatchinfoSel
47c9e 65 63 74 44 6f 63 74 6f 74 61 6c 28 0d 0a 20 20  ectDoctotal(..  
47c9f 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c  Fts3Table *pTab,
47ca0 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
47ca1 20 2a 2a 70 70 53 74 6d 74 2c 0d 0a 20 20 73 71   **ppStmt,..  sq
47ca2 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 6e 44  lite3_int64 *pnD
47ca3 6f 63 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  oc,..  const cha
47ca4 72 20 2a 2a 70 61 4c 65 6e 0d 0a 29 7b 0d 0a 20  r **paLen..){.. 
47ca5 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
47ca6 53 74 6d 74 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  Stmt;..  const c
47ca7 68 61 72 20 2a 61 3b 0d 0a 20 20 73 71 6c 69 74  har *a;..  sqlit
47ca8 65 33 5f 69 6e 74 36 34 20 6e 44 6f 63 3b 0d 0a  e3_int64 nDoc;..
47ca9 0d 0a 20 20 69 66 28 20 21 2a 70 70 53 74 6d 74  ..  if( !*ppStmt
47caa 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 20   ){..    int rc 
47cab 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c  = sqlite3Fts3Sel
47cac 65 63 74 44 6f 63 74 6f 74 61 6c 28 70 54 61 62  ectDoctotal(pTab
47cad 2c 20 70 70 53 74 6d 74 29 3b 0d 0a 20 20 20 20  , ppStmt);..    
47cae 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
47caf 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  K ) return rc;..
47cb0 20 20 7d 0d 0a 20 20 70 53 74 6d 74 20 3d 20 2a    }..  pStmt = *
47cb1 70 70 53 74 6d 74 3b 0d 0a 20 20 61 73 73 65 72  ppStmt;..  asser
47cb2 74 28 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f  t( sqlite3_data_
47cb3 63 6f 75 6e 74 28 70 53 74 6d 74 29 3d 3d 31 20  count(pStmt)==1 
47cb4 29 3b 0d 0a 0d 0a 20 20 61 20 3d 20 73 71 6c 69  );....  a = sqli
47cb5 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
47cb6 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20 20 61 20  pStmt, 0);..  a 
47cb7 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
47cb8 74 56 61 72 69 6e 74 28 61 2c 20 26 6e 44 6f 63  tVarint(a, &nDoc
47cb9 29 3b 0d 0a 20 20 69 66 28 20 6e 44 6f 63 3d 3d  );..  if( nDoc==
47cba 30 20 29 20 72 65 74 75 72 6e 20 46 54 53 5f 43  0 ) return FTS_C
47cbb 4f 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20  ORRUPT_VTAB;..  
47cbc 2a 70 6e 44 6f 63 20 3d 20 28 75 33 32 29 6e 44  *pnDoc = (u32)nD
47cbd 6f 63 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 61 4c  oc;....  if( paL
47cbe 65 6e 20 29 20 2a 70 61 4c 65 6e 20 3d 20 61 3b  en ) *paLen = a;
47cbf 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
47cc0 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  E_OK;..}..../*..
47cc1 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
47cc2 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
47cc3 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
47cc4 64 20 74 6f 20 73 74 6f 72 65 20 73 74 61 74 65  d to store state
47cc5 20 77 68 69 6c 65 20 0d 0a 2a 2a 20 69 74 65 72   while ..** iter
47cc6 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20 61 20  ating through a 
47cc7 6d 75 6c 74 69 2d 63 6f 6c 75 6d 6e 20 70 6f 73  multi-column pos
47cc8 69 74 69 6f 6e 2d 6c 69 73 74 20 63 6f 72 72 65  ition-list corre
47cc9 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 0d  sponding to the.
47cca 0a 2a 2a 20 68 69 74 73 20 66 6f 72 20 61 20 73  .** hits for a s
47ccb 69 6e 67 6c 65 20 70 68 72 61 73 65 20 6f 6e 20  ingle phrase on 
47ccc 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e 20  a single row in 
47ccd 6f 72 64 65 72 20 74 6f 20 63 61 6c 63 75 6c 61  order to calcula
47cce 74 65 20 74 68 65 0d 0a 2a 2a 20 76 61 6c 75 65  te the..** value
47ccf 73 20 66 6f 72 20 61 20 6d 61 74 63 68 69 6e 66  s for a matchinf
47cd0 6f 28 29 20 46 54 53 33 5f 4d 41 54 43 48 49 4e  o() FTS3_MATCHIN
47cd1 46 4f 5f 4c 43 53 20 72 65 71 75 65 73 74 2e 0d  FO_LCS request..
47cd2 0a 2a 2f 0d 0a 74 79 70 65 64 65 66 20 73 74 72  .*/..typedef str
47cd3 75 63 74 20 4c 63 73 49 74 65 72 61 74 6f 72 20  uct LcsIterator 
47cd4 4c 63 73 49 74 65 72 61 74 6f 72 3b 0d 0a 73 74  LcsIterator;..st
47cd5 72 75 63 74 20 4c 63 73 49 74 65 72 61 74 6f 72  ruct LcsIterator
47cd6 20 7b 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a   {..  Fts3Expr *
47cd7 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
47cd8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
47cd9 20 74 6f 20 70 68 72 61 73 65 20 65 78 70 72 65   to phrase expre
47cda 73 73 69 6f 6e 20 2a 2f 0d 0a 20 20 69 6e 74 20  ssion */..  int 
47cdb 69 50 6f 73 4f 66 66 73 65 74 3b 20 20 20 20 20  iPosOffset;     
47cdc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
47cdd 6f 6b 65 6e 73 20 63 6f 75 6e 74 20 75 70 20 74  okens count up t
47cde 6f 20 65 6e 64 20 6f 66 20 74 68 69 73 20 70 68  o end of this ph
47cdf 72 61 73 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20  rase */..  char 
47ce0 2a 70 52 65 61 64 3b 20 20 20 20 20 20 20 20 20  *pRead;         
47ce1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
47ce2 72 73 6f 72 20 75 73 65 64 20 74 6f 20 69 74 65  rsor used to ite
47ce3 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 44 6f  rate through aDo
47ce4 63 6c 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  clist */..  int 
47ce5 69 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20  iPos;           
47ce6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
47ce7 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20  urrent position 
47ce8 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  */..};..../* ..*
47ce9 2a 20 49 66 20 4c 63 73 49 74 65 72 61 74 6f 72  * If LcsIterator
47cea 2e 69 43 6f 6c 20 69 73 20 73 65 74 20 74 6f 20  .iCol is set to 
47ceb 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  the following va
47cec 6c 75 65 2c 20 74 68 65 20 69 74 65 72 61 74 6f  lue, the iterato
47ced 72 20 68 61 73 0d 0a 2a 2a 20 66 69 6e 69 73 68  r has..** finish
47cee 65 64 20 69 74 65 72 61 74 69 6e 67 20 74 68 72  ed iterating thr
47cef 6f 75 67 68 20 61 6c 6c 20 6f 66 66 73 65 74 73  ough all offsets
47cf0 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73   for all columns
47cf1 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 4c  ...*/..#define L
47cf2 43 53 5f 49 54 45 52 41 54 4f 52 5f 46 49 4e 49  CS_ITERATOR_FINI
47cf3 53 48 45 44 20 30 78 37 46 46 46 46 46 46 46 3b  SHED 0x7FFFFFFF;
47cf4 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  ....static int f
47cf5 74 73 33 4d 61 74 63 68 69 6e 66 6f 4c 63 73 43  ts3MatchinfoLcsC
47cf6 62 28 0d 0a 20 20 46 74 73 33 45 78 70 72 20 2a  b(..  Fts3Expr *
47cf7 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
47cf8 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20        /* Phrase 
47cf9 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
47cfa 2a 2f 0d 0a 20 20 69 6e 74 20 69 50 68 72 61 73  */..  int iPhras
47cfb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
47cfc 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20        /* Phrase 
47cfd 6e 75 6d 62 65 72 20 28 6e 75 6d 62 65 72 65 64  number (numbered
47cfe 20 66 72 6f 6d 20 7a 65 72 6f 29 20 2a 2f 0d 0a   from zero) */..
47cff 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20 20 20    void *pCtx    
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d01 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
47d02 4d 61 74 63 68 49 6e 66 6f 20 73 74 72 75 63 74  MatchInfo struct
47d03 75 72 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 4c 63  ure */..){..  Lc
47d04 73 49 74 65 72 61 74 6f 72 20 2a 61 49 74 65 72  sIterator *aIter
47d05 20 3d 20 28 4c 63 73 49 74 65 72 61 74 6f 72 20   = (LcsIterator 
47d06 2a 29 70 43 74 78 3b 0d 0a 20 20 61 49 74 65 72  *)pCtx;..  aIter
47d07 5b 69 50 68 72 61 73 65 5d 2e 70 45 78 70 72 20  [iPhrase].pExpr 
47d08 3d 20 70 45 78 70 72 3b 0d 0a 20 20 72 65 74 75  = pExpr;..  retu
47d09 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
47d0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 64 76 61 6e  ..../*..** Advan
47d0b 63 65 20 74 68 65 20 69 74 65 72 61 74 6f 72 20  ce the iterator 
47d0c 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
47d0d 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6e 65 78  ument to the nex
47d0e 74 20 70 6f 73 69 74 69 6f 6e 2e 20 52 65 74 75  t position. Retu
47d0f 72 6e 0d 0a 2a 2a 20 31 20 69 66 20 74 68 65 20  rn..** 1 if the 
47d10 69 74 65 72 61 74 6f 72 20 69 73 20 61 74 20 45  iterator is at E
47d11 4f 46 20 6f 72 20 69 66 20 69 74 20 6e 6f 77 20  OF or if it now 
47d12 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 74  points to the st
47d13 61 72 74 20 6f 66 20 74 68 65 0d 0a 2a 2a 20 70  art of the..** p
47d14 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
47d15 20 74 68 65 20 6e 65 78 74 20 63 6f 6c 75 6d 6e   the next column
47d16 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
47d17 74 20 66 74 73 33 4c 63 73 49 74 65 72 61 74 6f  t fts3LcsIterato
47d18 72 41 64 76 61 6e 63 65 28 4c 63 73 49 74 65 72  rAdvance(LcsIter
47d19 61 74 6f 72 20 2a 70 49 74 65 72 29 7b 0d 0a 20  ator *pIter){.. 
47d1a 20 63 68 61 72 20 2a 70 52 65 61 64 20 3d 20 70   char *pRead = p
47d1b 49 74 65 72 2d 3e 70 52 65 61 64 3b 0d 0a 20 20  Iter->pRead;..  
47d1c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52  sqlite3_int64 iR
47d1d 65 61 64 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  ead;..  int rc =
47d1e 20 30 3b 0d 0a 0d 0a 20 20 70 52 65 61 64 20 2b   0;....  pRead +
47d1f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
47d20 56 61 72 69 6e 74 28 70 52 65 61 64 2c 20 26 69  Varint(pRead, &i
47d21 52 65 61 64 29 3b 0d 0a 20 20 69 66 28 20 69 52  Read);..  if( iR
47d22 65 61 64 3d 3d 30 20 7c 7c 20 69 52 65 61 64 3d  ead==0 || iRead=
47d23 3d 31 20 29 7b 0d 0a 20 20 20 20 70 52 65 61 64  =1 ){..    pRead
47d24 20 3d 20 30 3b 0d 0a 20 20 20 20 72 63 20 3d 20   = 0;..    rc = 
47d25 31 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  1;..  }else{..  
47d26 20 20 70 49 74 65 72 2d 3e 69 50 6f 73 20 2b 3d    pIter->iPos +=
47d27 20 28 69 6e 74 29 28 69 52 65 61 64 2d 32 29 3b   (int)(iRead-2);
47d28 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70 49 74 65 72  ..  }....  pIter
47d29 2d 3e 70 52 65 61 64 20 3d 20 70 52 65 61 64 3b  ->pRead = pRead;
47d2a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
47d2b 7d 0d 0a 20 20 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68  }..  ../*..** Th
47d2c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
47d2d 65 6d 65 6e 74 73 20 74 68 65 20 46 54 53 33 5f  ements the FTS3_
47d2e 4d 41 54 43 48 49 4e 46 4f 5f 4c 43 53 20 6d 61  MATCHINFO_LCS ma
47d2f 74 63 68 69 6e 66 6f 28 29 20 66 6c 61 67 2e 20  tchinfo() flag. 
47d30 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68 65 20  ..**..** If the 
47d31 63 61 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66  call is successf
47d32 75 6c 2c 20 74 68 65 20 6c 6f 6e 67 65 73 74 2d  ul, the longest-
47d33 63 6f 6d 6d 6f 6e 2d 73 75 62 73 74 72 69 6e 67  common-substring
47d34 20 6c 65 6e 67 74 68 73 20 66 6f 72 20 65 61 63   lengths for eac
47d35 68 0d 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 61 72 65  h..** column are
47d36 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
47d37 65 20 66 69 72 73 74 20 6e 43 6f 6c 20 65 6c 65  e first nCol ele
47d38 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 70 49 6e  ments of the pIn
47d39 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d  fo->aMatchinfo[]
47d3a 20 0d 0a 2a 2a 20 61 72 72 61 79 20 62 65 66 6f   ..** array befo
47d3b 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 53 51  re returning. SQ
47d3c 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
47d3d 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  ned in this case
47d3e 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68 65 72 77  ...**..** Otherw
47d3f 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72  ise, if an error
47d40 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 69   occurs, an SQLi
47d41 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
47d42 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
47d43 65 0d 0a 2a 2a 20 64 61 74 61 20 77 72 69 74 74  e..** data writt
47d44 65 6e 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  en to the first 
47d45 6e 43 6f 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66  nCol elements of
47d46 20 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e   pInfo->aMatchin
47d47 66 6f 5b 5d 20 69 73 20 0d 0a 2a 2a 20 75 6e 64  fo[] is ..** und
47d48 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61  efined...*/..sta
47d49 74 69 63 20 69 6e 74 20 66 74 73 33 4d 61 74 63  tic int fts3Matc
47d4a 68 69 6e 66 6f 4c 63 73 28 46 74 73 33 43 75 72  hinfoLcs(Fts3Cur
47d4b 73 6f 72 20 2a 70 43 73 72 2c 20 4d 61 74 63 68  sor *pCsr, Match
47d4c 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0d 0a 20  Info *pInfo){.. 
47d4d 20 4c 63 73 49 74 65 72 61 74 6f 72 20 2a 61 49   LcsIterator *aI
47d4e 74 65 72 3b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  ter;..  int i;..
47d4f 20 20 69 6e 74 20 69 43 6f 6c 3b 0d 0a 20 20 69    int iCol;..  i
47d50 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0d 0a  nt nToken = 0;..
47d51 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
47d52 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65  and populate the
47d53 20 61 72 72 61 79 20 6f 66 20 4c 63 73 49 74 65   array of LcsIte
47d54 72 61 74 6f 72 20 6f 62 6a 65 63 74 73 2e 20 54  rator objects. T
47d55 68 65 20 61 72 72 61 79 0d 0a 20 20 2a 2a 20 63  he array..  ** c
47d56 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 65 6c 65 6d  ontains one elem
47d57 65 6e 74 20 66 6f 72 20 65 61 63 68 20 6d 61 74  ent for each mat
47d58 63 68 61 62 6c 65 20 70 68 72 61 73 65 20 69 6e  chable phrase in
47d59 20 74 68 65 20 71 75 65 72 79 2e 0d 0a 20 20 2a   the query...  *
47d5a 2a 2f 0d 0a 20 20 61 49 74 65 72 20 3d 20 73 71  */..  aIter = sq
47d5b 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
47d5c 65 6f 66 28 4c 63 73 49 74 65 72 61 74 6f 72 29  eof(LcsIterator)
47d5d 20 2a 20 70 43 73 72 2d 3e 6e 50 68 72 61 73 65   * pCsr->nPhrase
47d5e 29 3b 0d 0a 20 20 69 66 28 20 21 61 49 74 65 72  );..  if( !aIter
47d5f 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
47d60 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 6d 65 6d 73 65  _NOMEM;..  memse
47d61 74 28 61 49 74 65 72 2c 20 30 2c 20 73 69 7a 65  t(aIter, 0, size
47d62 6f 66 28 4c 63 73 49 74 65 72 61 74 6f 72 29 20  of(LcsIterator) 
47d63 2a 20 70 43 73 72 2d 3e 6e 50 68 72 61 73 65 29  * pCsr->nPhrase)
47d64 3b 0d 0a 20 20 28 76 6f 69 64 29 66 74 73 33 45  ;..  (void)fts3E
47d65 78 70 72 49 74 65 72 61 74 65 28 70 43 73 72 2d  xprIterate(pCsr-
47d66 3e 70 45 78 70 72 2c 20 66 74 73 33 4d 61 74 63  >pExpr, fts3Matc
47d67 68 69 6e 66 6f 4c 63 73 43 62 2c 20 28 76 6f 69  hinfoLcsCb, (voi
47d68 64 2a 29 61 49 74 65 72 29 3b 0d 0a 0d 0a 20 20  d*)aIter);....  
47d69 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f  for(i=0; i<pInfo
47d6a 2d 3e 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29 7b  ->nPhrase; i++){
47d6b 0d 0a 20 20 20 20 4c 63 73 49 74 65 72 61 74 6f  ..    LcsIterato
47d6c 72 20 2a 70 49 74 65 72 20 3d 20 26 61 49 74 65  r *pIter = &aIte
47d6d 72 5b 69 5d 3b 0d 0a 20 20 20 20 6e 54 6f 6b 65  r[i];..    nToke
47d6e 6e 20 2d 3d 20 70 49 74 65 72 2d 3e 70 45 78 70  n -= pIter->pExp
47d6f 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
47d70 65 6e 3b 0d 0a 20 20 20 20 70 49 74 65 72 2d 3e  en;..    pIter->
47d71 69 50 6f 73 4f 66 66 73 65 74 20 3d 20 6e 54 6f  iPosOffset = nTo
47d72 6b 65 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 66  ken;..  }....  f
47d73 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
47d74 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  pInfo->nCol; iCo
47d75 6c 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20 6e  l++){..    int n
47d76 4c 63 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Lcs = 0;        
47d77 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 43 53 20           /* LCS 
47d78 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 63  value for this c
47d79 6f 6c 75 6d 6e 20 2a 2f 0d 0a 20 20 20 20 69 6e  olumn */..    in
47d7a 74 20 6e 4c 69 76 65 20 3d 20 30 3b 20 20 20 20  t nLive = 0;    
47d7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
47d7c 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 6f  umber of iterato
47d7d 72 73 20 69 6e 20 61 49 74 65 72 20 6e 6f 74 20  rs in aIter not 
47d7e 61 74 20 45 4f 46 20 2a 2f 0d 0a 0d 0a 20 20 20  at EOF */....   
47d7f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66   for(i=0; i<pInf
47d80 6f 2d 3e 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29  o->nPhrase; i++)
47d81 7b 0d 0a 20 20 20 20 20 20 4c 63 73 49 74 65 72  {..      LcsIter
47d82 61 74 6f 72 20 2a 70 49 74 20 3d 20 26 61 49 74  ator *pIt = &aIt
47d83 65 72 5b 69 5d 3b 0d 0a 20 20 20 20 20 20 70 49  er[i];..      pI
47d84 74 2d 3e 70 52 65 61 64 20 3d 20 73 71 6c 69 74  t->pRead = sqlit
47d85 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65  e3Fts3EvalPhrase
47d86 50 6f 73 6c 69 73 74 28 70 43 73 72 2c 20 70 49  Poslist(pCsr, pI
47d87 74 2d 3e 70 45 78 70 72 2c 20 69 43 6f 6c 29 3b  t->pExpr, iCol);
47d88 0d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 2d  ..      if( pIt-
47d89 3e 70 52 65 61 64 20 29 7b 0d 0a 20 20 20 20 20  >pRead ){..     
47d8a 20 20 20 70 49 74 2d 3e 69 50 6f 73 20 3d 20 70     pIt->iPos = p
47d8b 49 74 2d 3e 69 50 6f 73 4f 66 66 73 65 74 3b 0d  It->iPosOffset;.
47d8c 0a 20 20 20 20 20 20 20 20 66 74 73 33 4c 63 73  .        fts3Lcs
47d8d 49 74 65 72 61 74 6f 72 41 64 76 61 6e 63 65 28  IteratorAdvance(
47d8e 26 61 49 74 65 72 5b 69 5d 29 3b 0d 0a 20 20 20  &aIter[i]);..   
47d8f 20 20 20 20 20 6e 4c 69 76 65 2b 2b 3b 0d 0a 20       nLive++;.. 
47d90 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d       }..    }...
47d91 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 4c 69 76  .    while( nLiv
47d92 65 3e 30 20 29 7b 0d 0a 20 20 20 20 20 20 4c 63  e>0 ){..      Lc
47d93 73 49 74 65 72 61 74 6f 72 20 2a 70 41 64 76 20  sIterator *pAdv 
47d94 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  = 0;      /* The
47d95 20 69 74 65 72 61 74 6f 72 20 74 6f 20 61 64 76   iterator to adv
47d96 61 6e 63 65 20 62 79 20 6f 6e 65 20 70 6f 73 69  ance by one posi
47d97 74 69 6f 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 69  tion */..      i
47d98 6e 74 20 6e 54 68 69 73 4c 63 73 20 3d 20 30 3b  nt nThisLcs = 0;
47d99 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 43             /* LC
47d9a 53 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  S for the curren
47d9b 74 20 69 74 65 72 61 74 6f 72 20 70 6f 73 69 74  t iterator posit
47d9c 69 6f 6e 73 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20  ions */....     
47d9d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66   for(i=0; i<pInf
47d9e 6f 2d 3e 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29  o->nPhrase; i++)
47d9f 7b 0d 0a 20 20 20 20 20 20 20 20 4c 63 73 49 74  {..        LcsIt
47da0 65 72 61 74 6f 72 20 2a 70 49 74 65 72 20 3d 20  erator *pIter = 
47da1 26 61 49 74 65 72 5b 69 5d 3b 0d 0a 20 20 20 20  &aIter[i];..    
47da2 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70      if( pIter->p
47da3 52 65 61 64 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  Read==0 ){..    
47da4 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 74        /* This it
47da5 65 72 61 74 6f 72 20 69 73 20 61 6c 72 65 61 64  erator is alread
47da6 79 20 61 74 20 45 4f 46 20 66 6f 72 20 74 68 69  y at EOF for thi
47da7 73 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0d 0a 20 20  s column. */..  
47da8 20 20 20 20 20 20 20 20 6e 54 68 69 73 4c 63 73          nThisLcs
47da9 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d   = 0;..        }
47daa 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
47dab 20 69 66 28 20 70 41 64 76 3d 3d 30 20 7c 7c 20   if( pAdv==0 || 
47dac 70 49 74 65 72 2d 3e 69 50 6f 73 3c 70 41 64 76  pIter->iPos<pAdv
47dad 2d 3e 69 50 6f 73 20 29 7b 0d 0a 20 20 20 20 20  ->iPos ){..     
47dae 20 20 20 20 20 20 20 70 41 64 76 20 3d 20 70 49         pAdv = pI
47daf 74 65 72 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ter;..          
47db0 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  }..          if(
47db1 20 6e 54 68 69 73 4c 63 73 3d 3d 30 20 7c 7c 20   nThisLcs==0 || 
47db2 70 49 74 65 72 2d 3e 69 50 6f 73 3d 3d 70 49 74  pIter->iPos==pIt
47db3 65 72 5b 2d 31 5d 2e 69 50 6f 73 20 29 7b 0d 0a  er[-1].iPos ){..
47db4 20 20 20 20 20 20 20 20 20 20 20 20 6e 54 68 69              nThi
47db5 73 4c 63 73 2b 2b 3b 0d 0a 20 20 20 20 20 20 20  sLcs++;..       
47db6 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
47db7 20 20 20 20 20 20 20 6e 54 68 69 73 4c 63 73 20         nThisLcs 
47db8 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  = 1;..          
47db9 7d 0d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  }..          if(
47dba 20 6e 54 68 69 73 4c 63 73 3e 6e 4c 63 73 20 29   nThisLcs>nLcs )
47dbb 20 6e 4c 63 73 20 3d 20 6e 54 68 69 73 4c 63 73   nLcs = nThisLcs
47dbc 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
47dbd 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66 28      }..      if(
47dbe 20 66 74 73 33 4c 63 73 49 74 65 72 61 74 6f 72   fts3LcsIterator
47dbf 41 64 76 61 6e 63 65 28 70 41 64 76 29 20 29 20  Advance(pAdv) ) 
47dc0 6e 4c 69 76 65 2d 2d 3b 0d 0a 20 20 20 20 7d 0d  nLive--;..    }.
47dc1 0a 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 4d  ...    pInfo->aM
47dc2 61 74 63 68 69 6e 66 6f 5b 69 43 6f 6c 5d 20 3d  atchinfo[iCol] =
47dc3 20 6e 4c 63 73 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20   nLcs;..  }.... 
47dc4 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 49   sqlite3_free(aI
47dc5 74 65 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  ter);..  return 
47dc6 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
47dc7 0a 2f 2a 0d 0a 2a 2a 20 50 6f 70 75 6c 61 74 65  ./*..** Populate
47dc8 20 74 68 65 20 62 75 66 66 65 72 20 70 49 6e 66   the buffer pInf
47dc9 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20  o->aMatchinfo[] 
47dca 77 69 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66  with an array of
47dcb 20 69 6e 74 65 67 65 72 73 20 74 6f 0d 0a 2a 2a   integers to..**
47dcc 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20   be returned by 
47dcd 74 68 65 20 6d 61 74 63 68 69 6e 66 6f 28 29 20  the matchinfo() 
47dce 66 75 6e 63 74 69 6f 6e 2e 20 41 72 67 75 6d 65  function. Argume
47dcf 6e 74 20 7a 41 72 67 20 63 6f 6e 74 61 69 6e 73  nt zArg contains
47dd0 20 74 68 65 20 0d 0a 2a 2a 20 66 6f 72 6d 61 74   the ..** format
47dd1 20 73 74 72 69 6e 67 20 70 61 73 73 65 64 20 61   string passed a
47dd2 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
47dd3 75 6d 65 6e 74 20 74 6f 20 6d 61 74 63 68 69 6e  ument to matchin
47dd4 66 6f 20 28 6f 72 20 74 68 65 0d 0a 2a 2a 20 64  fo (or the..** d
47dd5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 22 70 63  efault value "pc
47dd6 78 22 20 69 66 20 6e 6f 20 73 65 63 6f 6e 64 20  x" if no second 
47dd7 61 72 67 75 6d 65 6e 74 20 77 61 73 20 73 70 65  argument was spe
47dd8 63 69 66 69 65 64 29 2e 20 54 68 65 20 66 6f 72  cified). The for
47dd9 6d 61 74 0d 0a 2a 2a 20 73 74 72 69 6e 67 20 68  mat..** string h
47dda 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
47ddb 76 61 6c 69 64 61 74 65 64 20 61 6e 64 20 74 68  validated and th
47ddc 65 20 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69  e pInfo->aMatchi
47ddd 6e 66 6f 5b 5d 20 61 72 72 61 79 0d 0a 2a 2a 20  nfo[] array..** 
47dde 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
47ddf 20 62 65 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   be large enough
47de0 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2e   for the output.
47de1 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 62 47 6c 6f  ..**..** If bGlo
47de2 62 61 6c 20 69 73 20 74 72 75 65 2c 20 74 68 65  bal is true, the
47de3 6e 20 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20 66  n populate all f
47de4 69 65 6c 64 73 20 6f 66 20 74 68 65 20 6d 61 74  ields of the mat
47de5 63 68 69 6e 66 6f 28 29 20 6f 75 74 70 75 74 2e  chinfo() output.
47de6 0d 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 66 61  ..** If it is fa
47de7 6c 73 65 2c 20 74 68 65 6e 20 61 73 73 75 6d 65  lse, then assume
47de8 20 74 68 61 74 20 74 68 6f 73 65 20 66 69 65 6c   that those fiel
47de9 64 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63  ds that do not c
47dea 68 61 6e 67 65 20 62 65 74 77 65 65 6e 0d 0a 2a  hange between..*
47deb 2a 20 72 6f 77 73 20 28 69 2e 65 2e 20 46 54 53  * rows (i.e. FTS
47dec 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 50 48 52  3_MATCHINFO_NPHR
47ded 41 53 45 2c 20 4e 43 4f 4c 2c 20 4e 44 4f 43 2c  ASE, NCOL, NDOC,
47dee 20 41 56 47 4c 45 4e 47 54 48 20 61 6e 64 20 70   AVGLENGTH and p
47def 61 72 74 20 6f 66 20 48 49 54 53 29 0d 0a 2a 2a  art of HITS)..**
47df0 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
47df1 65 6e 20 70 6f 70 75 6c 61 74 65 64 2e 0d 0a 2a  en populated...*
47df2 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  *..** Return SQL
47df3 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
47df4 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69  sful, or an SQLi
47df5 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  te error code if
47df6 20 61 6e 20 65 72 72 6f 72 20 0d 0a 2a 2a 20 6f   an error ..** o
47df7 63 63 75 72 73 2e 20 49 66 20 61 20 76 61 6c 75  ccurs. If a valu
47df8 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  e other than SQL
47df9 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
47dfa 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 68  ed, the state th
47dfb 65 0d 0a 2a 2a 20 70 49 6e 66 6f 2d 3e 61 4d 61  e..** pInfo->aMa
47dfc 74 63 68 69 6e 66 6f 5b 5d 20 62 75 66 66 65 72  tchinfo[] buffer
47dfd 20 69 73 20 6c 65 66 74 20 69 6e 20 69 73 20 75   is left in is u
47dfe 6e 64 65 66 69 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73  ndefined...*/..s
47dff 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4d 61  tatic int fts3Ma
47e00 74 63 68 69 6e 66 6f 56 61 6c 75 65 73 28 0d 0a  tchinfoValues(..
47e01 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
47e02 73 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  sr,             
47e03 20 20 2f 2a 20 46 54 53 33 20 63 75 72 73 6f 72    /* FTS3 cursor
47e04 20 6f 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e   object */..  in
47e05 74 20 62 47 6c 6f 62 61 6c 2c 20 20 20 20 20 20  t bGlobal,      
47e06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47e07 20 54 72 75 65 20 74 6f 20 67 72 61 62 20 74 68   True to grab th
47e08 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 73 20 2a  e global stats *
47e09 2f 0d 0a 20 20 4d 61 74 63 68 49 6e 66 6f 20 2a  /..  MatchInfo *
47e0a 70 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20  pInfo,          
47e0b 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e 66       /* Matchinf
47e0c 6f 20 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74  o context object
47e0d 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
47e0e 72 20 2a 7a 41 72 67 20 20 20 20 20 20 20 20 20  r *zArg         
47e0f 20 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69         /* Matchi
47e10 6e 66 6f 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  nfo format strin
47e11 67 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  g */..){..  int 
47e12 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
47e13 0a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 46 74 73  .  int i;..  Fts
47e14 33 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28  3Table *pTab = (
47e15 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
47e16 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0d 0a 20  ->base.pVtab;.. 
47e17 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
47e18 53 65 6c 65 63 74 20 3d 20 30 3b 0d 0a 0d 0a 20  Select = 0;.... 
47e19 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
47e1a 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 41 72 67 5b  LITE_OK && zArg[
47e1b 69 5d 3b 20 69 2b 2b 29 7b 0d 0a 0d 0a 20 20 20  i]; i++){....   
47e1c 20 73 77 69 74 63 68 28 20 7a 41 72 67 5b 69 5d   switch( zArg[i]
47e1d 20 29 7b 0d 0a 20 20 20 20 20 20 63 61 73 65 20   ){..      case 
47e1e 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e  FTS3_MATCHINFO_N
47e1f 50 48 52 41 53 45 3a 0d 0a 20 20 20 20 20 20 20  PHRASE:..       
47e20 20 69 66 28 20 62 47 6c 6f 62 61 6c 20 29 20 70   if( bGlobal ) p
47e21 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  Info->aMatchinfo
47e22 5b 30 5d 20 3d 20 70 49 6e 66 6f 2d 3e 6e 50 68  [0] = pInfo->nPh
47e23 72 61 73 65 3b 0d 0a 20 20 20 20 20 20 20 20 62  rase;..        b
47e24 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20 63  reak;....      c
47e25 61 73 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e  ase FTS3_MATCHIN
47e26 46 4f 5f 4e 43 4f 4c 3a 0d 0a 20 20 20 20 20 20  FO_NCOL:..      
47e27 20 20 69 66 28 20 62 47 6c 6f 62 61 6c 20 29 20    if( bGlobal ) 
47e28 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66  pInfo->aMatchinf
47e29 6f 5b 30 5d 20 3d 20 70 49 6e 66 6f 2d 3e 6e 43  o[0] = pInfo->nC
47e2a 6f 6c 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65  ol;..        bre
47e2b 61 6b 3b 0d 0a 20 20 20 20 20 20 20 20 0d 0a 20  ak;..        .. 
47e2c 20 20 20 20 20 63 61 73 65 20 46 54 53 33 5f 4d       case FTS3_M
47e2d 41 54 43 48 49 4e 46 4f 5f 4e 44 4f 43 3a 0d 0a  ATCHINFO_NDOC:..
47e2e 20 20 20 20 20 20 20 20 69 66 28 20 62 47 6c 6f          if( bGlo
47e2f 62 61 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  bal ){..        
47e30 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
47e31 6e 44 6f 63 20 3d 20 30 3b 0d 0a 20 20 20 20 20  nDoc = 0;..     
47e32 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 4d 61       rc = fts3Ma
47e33 74 63 68 69 6e 66 6f 53 65 6c 65 63 74 44 6f 63  tchinfoSelectDoc
47e34 74 6f 74 61 6c 28 70 54 61 62 2c 20 26 70 53 65  total(pTab, &pSe
47e35 6c 65 63 74 2c 20 26 6e 44 6f 63 2c 20 30 29 3b  lect, &nDoc, 0);
47e36 0d 0a 20 20 20 20 20 20 20 20 20 20 70 49 6e 66  ..          pInf
47e37 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 30 5d  o->aMatchinfo[0]
47e38 20 3d 20 28 75 33 32 29 6e 44 6f 63 3b 0d 0a 20   = (u32)nDoc;.. 
47e39 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
47e3a 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20    break;....    
47e3b 20 20 63 61 73 65 20 46 54 53 33 5f 4d 41 54 43    case FTS3_MATC
47e3c 48 49 4e 46 4f 5f 41 56 47 4c 45 4e 47 54 48 3a  HINFO_AVGLENGTH:
47e3d 20 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 62   ..        if( b
47e3e 47 6c 6f 62 61 6c 20 29 7b 0d 0a 20 20 20 20 20  Global ){..     
47e3f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
47e40 36 34 20 6e 44 6f 63 3b 20 20 20 20 20 2f 2a 20  64 nDoc;     /* 
47e41 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  Number of rows i
47e42 6e 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20 20  n table */..    
47e43 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
47e44 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   *a;          /*
47e45 20 41 67 67 72 65 67 61 74 65 20 63 6f 6c 75 6d   Aggregate colum
47e46 6e 20 6c 65 6e 67 74 68 20 61 72 72 61 79 20 2a  n length array *
47e47 2f 0d 0a 0d 0a 20 20 20 20 20 20 20 20 20 20 72  /....          r
47e48 63 20 3d 20 66 74 73 33 4d 61 74 63 68 69 6e 66  c = fts3Matchinf
47e49 6f 53 65 6c 65 63 74 44 6f 63 74 6f 74 61 6c 28  oSelectDoctotal(
47e4a 70 54 61 62 2c 20 26 70 53 65 6c 65 63 74 2c 20  pTab, &pSelect, 
47e4b 26 6e 44 6f 63 2c 20 26 61 29 3b 0d 0a 20 20 20  &nDoc, &a);..   
47e4c 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
47e4d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
47e4e 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f           int iCo
47e4f 6c 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20  l;..            
47e50 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
47e51 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 20 69 43  <pInfo->nCol; iC
47e52 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20  ol++){..        
47e53 20 20 20 20 20 20 75 33 32 20 69 56 61 6c 3b 0d        u32 iVal;.
47e54 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
47e55 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 54 6f  qlite3_int64 nTo
47e56 6b 65 6e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  ken;..          
47e57 20 20 20 20 61 20 2b 3d 20 73 71 6c 69 74 65 33      a += sqlite3
47e58 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 61 2c  Fts3GetVarint(a,
47e59 20 26 6e 54 6f 6b 65 6e 29 3b 0d 0a 20 20 20 20   &nToken);..    
47e5a 20 20 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d            iVal =
47e5b 20 28 75 33 32 29 28 28 28 75 33 32 29 28 6e 54   (u32)(((u32)(nT
47e5c 6f 6b 65 6e 26 30 78 66 66 66 66 66 66 66 66 29  oken&0xffffffff)
47e5d 2b 6e 44 6f 63 2f 32 29 2f 6e 44 6f 63 29 3b 0d  +nDoc/2)/nDoc);.
47e5e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
47e5f 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  Info->aMatchinfo
47e60 5b 69 43 6f 6c 5d 20 3d 20 69 56 61 6c 3b 0d 0a  [iCol] = iVal;..
47e61 20 20 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20              }.. 
47e62 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
47e63 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 62      }..        b
47e64 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20 63  reak;....      c
47e65 61 73 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e  ase FTS3_MATCHIN
47e66 46 4f 5f 4c 45 4e 47 54 48 3a 20 7b 0d 0a 20 20  FO_LENGTH: {..  
47e67 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
47e68 6d 74 20 2a 70 53 65 6c 65 63 74 44 6f 63 73 69  mt *pSelectDocsi
47e69 7a 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20  ze = 0;..       
47e6a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
47e6b 33 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 28 70  3SelectDocsize(p
47e6c 54 61 62 2c 20 70 43 73 72 2d 3e 69 50 72 65 76  Tab, pCsr->iPrev
47e6d 49 64 2c 20 26 70 53 65 6c 65 63 74 44 6f 63 73  Id, &pSelectDocs
47e6e 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20 20 20 69  ize);..        i
47e6f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
47e70 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 69   ){..          i
47e71 6e 74 20 69 43 6f 6c 3b 0d 0a 20 20 20 20 20 20  nt iCol;..      
47e72 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
47e73 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  a = sqlite3_colu
47e74 6d 6e 5f 62 6c 6f 62 28 70 53 65 6c 65 63 74 44  mn_blob(pSelectD
47e75 6f 63 73 69 7a 65 2c 20 30 29 3b 0d 0a 20 20 20  ocsize, 0);..   
47e76 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d         for(iCol=
47e77 30 3b 20 69 43 6f 6c 3c 70 49 6e 66 6f 2d 3e 6e  0; iCol<pInfo->n
47e78 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0d 0a 20  Col; iCol++){.. 
47e79 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
47e7a 65 33 5f 69 6e 74 36 34 20 6e 54 6f 6b 65 6e 3b  e3_int64 nToken;
47e7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20  ..            a 
47e7c 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
47e7d 74 56 61 72 69 6e 74 28 61 2c 20 26 6e 54 6f 6b  tVarint(a, &nTok
47e7e 65 6e 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  en);..          
47e7f 20 20 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69    pInfo->aMatchi
47e80 6e 66 6f 5b 69 43 6f 6c 5d 20 3d 20 28 75 33 32  nfo[iCol] = (u32
47e81 29 6e 54 6f 6b 65 6e 3b 0d 0a 20 20 20 20 20 20  )nToken;..      
47e82 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d      }..        }
47e83 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
47e84 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74 44  3_reset(pSelectD
47e85 6f 63 73 69 7a 65 29 3b 0d 0a 20 20 20 20 20 20  ocsize);..      
47e86 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20    break;..      
47e87 7d 0d 0a 0d 0a 20 20 20 20 20 20 63 61 73 65 20  }....      case 
47e88 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c  FTS3_MATCHINFO_L
47e89 43 53 3a 0d 0a 20 20 20 20 20 20 20 20 72 63 20  CS:..        rc 
47e8a 3d 20 66 74 73 33 45 78 70 72 4c 6f 61 64 44 6f  = fts3ExprLoadDo
47e8b 63 6c 69 73 74 73 28 70 43 73 72 2c 20 30 2c 20  clists(pCsr, 0, 
47e8c 30 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  0);..        if(
47e8d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
47e8e 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  {..          rc 
47e8f 3d 20 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 4c  = fts3MatchinfoL
47e90 63 73 28 70 43 73 72 2c 20 70 49 6e 66 6f 29 3b  cs(pCsr, pInfo);
47e91 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20  ..        }..   
47e92 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20       break;.... 
47e93 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d       default: {.
47e94 0a 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70  .        Fts3Exp
47e95 72 20 2a 70 45 78 70 72 3b 0d 0a 20 20 20 20 20  r *pExpr;..     
47e96 20 20 20 61 73 73 65 72 74 28 20 7a 41 72 67 5b     assert( zArg[
47e97 69 5d 3d 3d 46 54 53 33 5f 4d 41 54 43 48 49 4e  i]==FTS3_MATCHIN
47e98 46 4f 5f 48 49 54 53 20 29 3b 0d 0a 20 20 20 20  FO_HITS );..    
47e99 20 20 20 20 70 45 78 70 72 20 3d 20 70 43 73 72      pExpr = pCsr
47e9a 2d 3e 70 45 78 70 72 3b 0d 0a 20 20 20 20 20 20  ->pExpr;..      
47e9b 20 20 72 63 20 3d 20 66 74 73 33 45 78 70 72 4c    rc = fts3ExprL
47e9c 6f 61 64 44 6f 63 6c 69 73 74 73 28 70 43 73 72  oadDoclists(pCsr
47e9d 2c 20 30 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20  , 0, 0);..      
47e9e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
47e9f 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  _OK ) break;..  
47ea0 20 20 20 20 20 20 69 66 28 20 62 47 6c 6f 62 61        if( bGloba
47ea1 6c 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  l ){..          
47ea2 69 66 28 20 70 43 73 72 2d 3e 70 44 65 66 65 72  if( pCsr->pDefer
47ea3 72 65 64 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  red ){..        
47ea4 20 20 20 20 72 63 20 3d 20 66 74 73 33 4d 61 74      rc = fts3Mat
47ea5 63 68 69 6e 66 6f 53 65 6c 65 63 74 44 6f 63 74  chinfoSelectDoct
47ea6 6f 74 61 6c 28 70 54 61 62 2c 20 26 70 53 65 6c  otal(pTab, &pSel
47ea7 65 63 74 2c 20 26 70 49 6e 66 6f 2d 3e 6e 44 6f  ect, &pInfo->nDo
47ea8 63 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 20 20  c, 0);..        
47ea9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
47eaa 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0d 0a  TE_OK ) break;..
47eab 20 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20            }..   
47eac 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
47ead 45 78 70 72 49 74 65 72 61 74 65 28 70 45 78 70  ExprIterate(pExp
47eae 72 2c 20 66 74 73 33 45 78 70 72 47 6c 6f 62 61  r, fts3ExprGloba
47eaf 6c 48 69 74 73 43 62 2c 28 76 6f 69 64 2a 29 70  lHitsCb,(void*)p
47eb0 49 6e 66 6f 29 3b 0d 0a 20 20 20 20 20 20 20 20  Info);..        
47eb1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
47eb2 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0d 0a 20 20  _OK ) break;..  
47eb3 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20        }..       
47eb4 20 28 76 6f 69 64 29 66 74 73 33 45 78 70 72 49   (void)fts3ExprI
47eb5 74 65 72 61 74 65 28 70 45 78 70 72 2c 20 66 74  terate(pExpr, ft
47eb6 73 33 45 78 70 72 4c 6f 63 61 6c 48 69 74 73 43  s3ExprLocalHitsC
47eb7 62 2c 28 76 6f 69 64 2a 29 70 49 6e 66 6f 29 3b  b,(void*)pInfo);
47eb8 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
47eb9 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
47eba 0d 0a 0d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61  ....    pInfo->a
47ebb 4d 61 74 63 68 69 6e 66 6f 20 2b 3d 20 66 74 73  Matchinfo += fts
47ebc 33 4d 61 74 63 68 69 6e 66 6f 53 69 7a 65 28 70  3MatchinfoSize(p
47ebd 49 6e 66 6f 2c 20 7a 41 72 67 5b 69 5d 29 3b 0d  Info, zArg[i]);.
47ebe 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  .  }....  sqlite
47ebf 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74 29  3_reset(pSelect)
47ec0 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ;..  return rc;.
47ec1 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 50  .}....../*..** P
47ec2 6f 70 75 6c 61 74 65 20 70 43 73 72 2d 3e 61 4d  opulate pCsr->aM
47ec3 61 74 63 68 69 6e 66 6f 5b 5d 20 77 69 74 68 20  atchinfo[] with 
47ec4 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
47ec5 72 65 6e 74 20 72 6f 77 2e 20 54 68 65 20 0d 0a  rent row. The ..
47ec6 2a 2a 20 27 6d 61 74 63 68 69 6e 66 6f 27 20 64  ** 'matchinfo' d
47ec7 61 74 61 20 69 73 20 61 6e 20 61 72 72 61 79 20  ata is an array 
47ec8 6f 66 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e  of 32-bit unsign
47ec9 65 64 20 69 6e 74 65 67 65 72 73 20 28 43 20 74  ed integers (C t
47eca 79 70 65 20 75 33 32 29 2e 0d 0a 2a 2f 0d 0a 73  ype u32)...*/..s
47ecb 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65  tatic int fts3Ge
47ecc 74 4d 61 74 63 68 69 6e 66 6f 28 0d 0a 20 20 46  tMatchinfo(..  F
47ecd 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  ts3Cursor *pCsr,
47ece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47ecf 2a 20 46 54 53 33 20 43 75 72 73 6f 72 20 6f 62  * FTS3 Cursor ob
47ed0 6a 65 63 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74  ject */..  const
47ed1 20 63 68 61 72 20 2a 7a 41 72 67 20 20 20 20 20   char *zArg     
47ed2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
47ed3 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
47ed4 20 6d 61 74 63 68 69 6e 66 6f 28 29 20 66 75 6e   matchinfo() fun
47ed5 63 74 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ction */..){..  
47ed6 4d 61 74 63 68 49 6e 66 6f 20 73 49 6e 66 6f 3b  MatchInfo sInfo;
47ed7 0d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  ..  Fts3Table *p
47ed8 54 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65  Tab = (Fts3Table
47ed9 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56   *)pCsr->base.pV
47eda 74 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d  tab;..  int rc =
47edb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69   SQLITE_OK;..  i
47edc 6e 74 20 62 47 6c 6f 62 61 6c 20 3d 20 30 3b 20  nt bGlobal = 0; 
47edd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47ede 2a 20 43 6f 6c 6c 65 63 74 20 27 67 6c 6f 62 61  * Collect 'globa
47edf 6c 27 20 73 74 61 74 73 20 61 73 20 77 65 6c 6c  l' stats as well
47ee0 20 61 73 20 6c 6f 63 61 6c 20 2a 2f 0d 0a 0d 0a   as local */....
47ee1 20 20 6d 65 6d 73 65 74 28 26 73 49 6e 66 6f 2c    memset(&sInfo,
47ee2 20 30 2c 20 73 69 7a 65 6f 66 28 4d 61 74 63 68   0, sizeof(Match
47ee3 49 6e 66 6f 29 29 3b 0d 0a 20 20 73 49 6e 66 6f  Info));..  sInfo
47ee4 2e 70 43 75 72 73 6f 72 20 3d 20 70 43 73 72 3b  .pCursor = pCsr;
47ee5 0d 0a 20 20 73 49 6e 66 6f 2e 6e 43 6f 6c 20 3d  ..  sInfo.nCol =
47ee6 20 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0d   pTab->nColumn;.
47ee7 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ...  /* If there
47ee8 20 69 73 20 63 61 63 68 65 64 20 6d 61 74 63 68   is cached match
47ee9 69 6e 66 6f 28 29 20 64 61 74 61 2c 20 62 75 74  info() data, but
47eea 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
47eeb 6e 67 20 66 6f 72 20 74 68 65 20 0d 0a 20 20 2a  ng for the ..  *
47eec 2a 20 63 61 63 68 65 20 64 6f 65 73 20 6e 6f 74  * cache does not
47eed 20 6d 61 74 63 68 20 74 68 65 20 66 6f 72 6d 61   match the forma
47eee 74 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 69  t string for thi
47eef 73 20 72 65 71 75 65 73 74 2c 20 64 69 73 63 61  s request, disca
47ef0 72 64 20 0d 0a 20 20 2a 2a 20 74 68 65 20 63 61  rd ..  ** the ca
47ef1 63 68 65 64 20 64 61 74 61 2e 20 2a 2f 0d 0a 20  ched data. */.. 
47ef2 20 69 66 28 20 70 43 73 72 2d 3e 7a 4d 61 74 63   if( pCsr->zMatc
47ef3 68 69 6e 66 6f 20 26 26 20 73 74 72 63 6d 70 28  hinfo && strcmp(
47ef4 70 43 73 72 2d 3e 7a 4d 61 74 63 68 69 6e 66 6f  pCsr->zMatchinfo
47ef5 2c 20 7a 41 72 67 29 20 29 7b 0d 0a 20 20 20 20  , zArg) ){..    
47ef6 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 4d  assert( pCsr->aM
47ef7 61 74 63 68 69 6e 66 6f 20 29 3b 0d 0a 20 20 20  atchinfo );..   
47ef8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
47ef9 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 29 3b  sr->aMatchinfo);
47efa 0d 0a 20 20 20 20 70 43 73 72 2d 3e 7a 4d 61 74  ..    pCsr->zMat
47efb 63 68 69 6e 66 6f 20 3d 20 30 3b 0d 0a 20 20 20  chinfo = 0;..   
47efc 20 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66   pCsr->aMatchinf
47efd 6f 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  o = 0;..  }.... 
47efe 20 2f 2a 20 49 66 20 46 74 73 33 43 75 72 73 6f   /* If Fts3Curso
47eff 72 2e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 69  r.aMatchinfo[] i
47f00 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69  s NULL, then thi
47f01 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74  s is the first t
47f02 69 6d 65 20 74 68 65 0d 0a 20 20 2a 2a 20 6d 61  ime the..  ** ma
47f03 74 63 68 69 6e 66 6f 20 66 75 6e 63 74 69 6f 6e  tchinfo function
47f04 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
47f05 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 2e   for this query.
47f06 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 0d 0a   In this case ..
47f07 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68    ** allocate th
47f08 65 20 61 72 72 61 79 20 75 73 65 64 20 74 6f 20  e array used to 
47f09 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 6d  accumulate the m
47f0a 61 74 63 68 69 6e 66 6f 20 64 61 74 61 20 61 6e  atchinfo data an
47f0b 64 0d 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  d..  ** initiali
47f0c 7a 65 20 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74  ze those element
47f0d 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73 74  s that are const
47f0e 61 6e 74 20 66 6f 72 20 65 76 65 72 79 20 72 6f  ant for every ro
47f0f 77 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20  w...  */..  if( 
47f10 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  pCsr->aMatchinfo
47f11 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20  ==0 ){..    int 
47f12 6e 4d 61 74 63 68 69 6e 66 6f 20 3d 20 30 3b 20  nMatchinfo = 0; 
47f13 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
47f14 62 65 72 20 6f 66 20 75 33 32 20 65 6c 65 6d 65  ber of u32 eleme
47f15 6e 74 73 20 69 6e 20 6d 61 74 63 68 2d 69 6e 66  nts in match-inf
47f16 6f 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 41  o */..    int nA
47f17 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
47f18 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
47f19 20 69 6e 20 7a 41 72 67 20 2a 2f 0d 0a 20 20 20   in zArg */..   
47f1a 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
47f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47f1c 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74  * Used to iterat
47f1d 65 20 74 68 72 6f 75 67 68 20 7a 41 72 67 20 2a  e through zArg *
47f1e 2f 0d 0a 0d 0a 20 20 20 20 2f 2a 20 44 65 74 65  /....    /* Dete
47f1f 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72  rmine the number
47f20 20 6f 66 20 70 68 72 61 73 65 73 20 69 6e 20 74   of phrases in t
47f21 68 65 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20 20  he query */..   
47f22 20 70 43 73 72 2d 3e 6e 50 68 72 61 73 65 20 3d   pCsr->nPhrase =
47f23 20 66 74 73 33 45 78 70 72 50 68 72 61 73 65 43   fts3ExprPhraseC
47f24 6f 75 6e 74 28 70 43 73 72 2d 3e 70 45 78 70 72  ount(pCsr->pExpr
47f25 29 3b 0d 0a 20 20 20 20 73 49 6e 66 6f 2e 6e 50  );..    sInfo.nP
47f26 68 72 61 73 65 20 3d 20 70 43 73 72 2d 3e 6e 50  hrase = pCsr->nP
47f27 68 72 61 73 65 3b 0d 0a 0d 0a 20 20 20 20 2f 2a  hrase;....    /*
47f28 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e   Determine the n
47f29 75 6d 62 65 72 20 6f 66 20 69 6e 74 65 67 65 72  umber of integer
47f2a 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20  s in the buffer 
47f2b 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
47f2c 20 63 61 6c 6c 2e 20 2a 2f 0d 0a 20 20 20 20 66   call. */..    f
47f2d 6f 72 28 69 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b  or(i=0; zArg[i];
47f2e 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 6e 4d   i++){..      nM
47f2f 61 74 63 68 69 6e 66 6f 20 2b 3d 20 66 74 73 33  atchinfo += fts3
47f30 4d 61 74 63 68 69 6e 66 6f 53 69 7a 65 28 26 73  MatchinfoSize(&s
47f31 49 6e 66 6f 2c 20 7a 41 72 67 5b 69 5d 29 3b 0d  Info, zArg[i]);.
47f32 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
47f33 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
47f34 66 6f 72 20 46 74 73 33 43 75 72 73 6f 72 2e 61  for Fts3Cursor.a
47f35 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 61 6e 64 20  Matchinfo[] and 
47f36 46 74 73 33 43 75 72 73 6f 72 2e 7a 4d 61 74 63  Fts3Cursor.zMatc
47f37 68 69 6e 66 6f 2e 20 2a 2f 0d 0a 20 20 20 20 6e  hinfo. */..    n
47f38 41 72 67 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  Arg = (int)strle
47f39 6e 28 7a 41 72 67 29 3b 0d 0a 20 20 20 20 70 43  n(zArg);..    pC
47f3a 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 20 3d  sr->aMatchinfo =
47f3b 20 28 75 33 32 20 2a 29 73 71 6c 69 74 65 33 5f   (u32 *)sqlite3_
47f3c 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 33  malloc(sizeof(u3
47f3d 32 29 2a 6e 4d 61 74 63 68 69 6e 66 6f 20 2b 20  2)*nMatchinfo + 
47f3e 6e 41 72 67 20 2b 20 31 29 3b 0d 0a 20 20 20 20  nArg + 1);..    
47f3f 69 66 28 20 21 70 43 73 72 2d 3e 61 4d 61 74 63  if( !pCsr->aMatc
47f40 68 69 6e 66 6f 20 29 20 72 65 74 75 72 6e 20 53  hinfo ) return S
47f41 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 0d 0a  QLITE_NOMEM;....
47f42 20 20 20 20 70 43 73 72 2d 3e 7a 4d 61 74 63 68      pCsr->zMatch
47f43 69 6e 66 6f 20 3d 20 28 63 68 61 72 20 2a 29 26  info = (char *)&
47f44 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  pCsr->aMatchinfo
47f45 5b 6e 4d 61 74 63 68 69 6e 66 6f 5d 3b 0d 0a 20  [nMatchinfo];.. 
47f46 20 20 20 70 43 73 72 2d 3e 6e 4d 61 74 63 68 69     pCsr->nMatchi
47f47 6e 66 6f 20 3d 20 6e 4d 61 74 63 68 69 6e 66 6f  nfo = nMatchinfo
47f48 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 43  ;..    memcpy(pC
47f49 73 72 2d 3e 7a 4d 61 74 63 68 69 6e 66 6f 2c 20  sr->zMatchinfo, 
47f4a 7a 41 72 67 2c 20 6e 41 72 67 2b 31 29 3b 0d 0a  zArg, nArg+1);..
47f4b 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2d      memset(pCsr-
47f4c 3e 61 4d 61 74 63 68 69 6e 66 6f 2c 20 30 2c 20  >aMatchinfo, 0, 
47f4d 73 69 7a 65 6f 66 28 75 33 32 29 2a 6e 4d 61 74  sizeof(u32)*nMat
47f4e 63 68 69 6e 66 6f 29 3b 0d 0a 20 20 20 20 70 43  chinfo);..    pC
47f4f 73 72 2d 3e 69 73 4d 61 74 63 68 69 6e 66 6f 4e  sr->isMatchinfoN
47f50 65 65 64 65 64 20 3d 20 31 3b 0d 0a 20 20 20 20  eeded = 1;..    
47f51 62 47 6c 6f 62 61 6c 20 3d 20 31 3b 0d 0a 20 20  bGlobal = 1;..  
47f52 7d 0d 0a 0d 0a 20 20 73 49 6e 66 6f 2e 61 4d 61  }....  sInfo.aMa
47f53 74 63 68 69 6e 66 6f 20 3d 20 70 43 73 72 2d 3e  tchinfo = pCsr->
47f54 61 4d 61 74 63 68 69 6e 66 6f 3b 0d 0a 20 20 73  aMatchinfo;..  s
47f55 49 6e 66 6f 2e 6e 50 68 72 61 73 65 20 3d 20 70  Info.nPhrase = p
47f56 43 73 72 2d 3e 6e 50 68 72 61 73 65 3b 0d 0a 20  Csr->nPhrase;.. 
47f57 20 69 66 28 20 70 43 73 72 2d 3e 69 73 4d 61 74   if( pCsr->isMat
47f58 63 68 69 6e 66 6f 4e 65 65 64 65 64 20 29 7b 0d  chinfoNeeded ){.
47f59 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 4d 61  .    rc = fts3Ma
47f5a 74 63 68 69 6e 66 6f 56 61 6c 75 65 73 28 70 43  tchinfoValues(pC
47f5b 73 72 2c 20 62 47 6c 6f 62 61 6c 2c 20 26 73 49  sr, bGlobal, &sI
47f5c 6e 66 6f 2c 20 7a 41 72 67 29 3b 0d 0a 20 20 20  nfo, zArg);..   
47f5d 20 70 43 73 72 2d 3e 69 73 4d 61 74 63 68 69 6e   pCsr->isMatchin
47f5e 66 6f 4e 65 65 64 65 64 20 3d 20 30 3b 0d 0a 20  foNeeded = 0;.. 
47f5f 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72   }....  return r
47f60 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
47f61 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
47f62 66 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63  f snippet() func
47f63 74 69 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54  tion...*/..SQLIT
47f64 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
47f65 71 6c 69 74 65 33 46 74 73 33 53 6e 69 70 70 65  qlite3Fts3Snippe
47f66 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  t(..  sqlite3_co
47f67 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20 20  ntext *pCtx,    
47f68 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20        /* SQLite 
47f69 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f  function call co
47f6a 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 46 74 73 33  ntext */..  Fts3
47f6b 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20  Cursor *pCsr,   
47f6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
47f6d 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0d  ursor object */.
47f6e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
47f6f 53 74 61 72 74 2c 20 20 20 20 20 20 20 20 20 20  Start,          
47f70 20 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 73 74     /* Snippet st
47f71 61 72 74 20 74 65 78 74 20 2d 20 22 3c 62 3e 22  art text - "<b>"
47f72 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61   */..  const cha
47f73 72 20 2a 7a 45 6e 64 2c 20 20 20 20 20 20 20 20  r *zEnd,        
47f74 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65         /* Snippe
47f75 74 20 65 6e 64 20 74 65 78 74 20 2d 20 22 3c 2f  t end text - "</
47f76 62 3e 22 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  b>" */..  const 
47f77 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 2c  char *zEllipsis,
47f78 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6e 69            /* Sni
47f79 70 70 65 74 20 65 6c 6c 69 70 73 69 73 20 74 65  ppet ellipsis te
47f7a 78 74 20 2d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e  xt - "<b>...</b>
47f7b 22 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 43 6f 6c  " */..  int iCol
47f7c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
47f7d 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61          /* Extra
47f7e 63 74 20 73 6e 69 70 70 65 74 20 66 72 6f 6d 20  ct snippet from 
47f7f 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0d 0a  this column */..
47f80 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 20 20 20    int nToken    
47f81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f82 20 20 2f 2a 20 41 70 70 72 6f 78 69 6d 61 74 65    /* Approximate
47f83 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   number of token
47f84 73 20 69 6e 20 73 6e 69 70 70 65 74 20 2a 2f 0d  s in snippet */.
47f85 0a 29 7b 0d 0a 20 20 46 74 73 33 54 61 62 6c 65  .){..  Fts3Table
47f86 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33 54 61   *pTab = (Fts3Ta
47f87 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65  ble *)pCsr->base
47f88 2e 70 56 74 61 62 3b 0d 0a 20 20 69 6e 74 20 72  .pVtab;..  int r
47f89 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
47f8a 20 20 69 6e 74 20 69 3b 0d 0a 20 20 53 74 72 42    int i;..  StrB
47f8b 75 66 66 65 72 20 72 65 73 20 3d 20 7b 30 2c 20  uffer res = {0, 
47f8c 30 2c 20 30 7d 3b 0d 0a 0d 0a 20 20 2f 2a 20 54  0, 0};....  /* T
47f8d 68 65 20 72 65 74 75 72 6e 65 64 20 74 65 78 74  he returned text
47f8e 20 69 6e 63 6c 75 64 65 73 20 75 70 20 74 6f 20   includes up to 
47f8f 66 6f 75 72 20 66 72 61 67 6d 65 6e 74 73 20 6f  four fragments o
47f90 66 20 74 65 78 74 20 65 78 74 72 61 63 74 65 64  f text extracted
47f91 20 66 72 6f 6d 0d 0a 20 20 2a 2a 20 74 68 65 20   from..  ** the 
47f92 64 61 74 61 20 69 6e 20 74 68 65 20 63 75 72 72  data in the curr
47f93 65 6e 74 20 72 6f 77 2e 20 54 68 65 20 66 69 72  ent row. The fir
47f94 73 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  st iteration of 
47f95 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f  the for(...) loo
47f96 70 0d 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 61 74  p..  ** below at
47f97 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 61 74 65  tempts to locate
47f98 20 61 20 73 69 6e 67 6c 65 20 66 72 61 67 6d 65   a single fragme
47f99 6e 74 20 6f 66 20 74 65 78 74 20 6e 54 6f 6b 65  nt of text nToke
47f9a 6e 20 74 6f 6b 65 6e 73 20 69 6e 20 0d 0a 20 20  n tokens in ..  
47f9b 2a 2a 20 73 69 7a 65 20 74 68 61 74 20 63 6f 6e  ** size that con
47f9c 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f  tains at least o
47f9d 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 61  ne instance of a
47f9e 6c 6c 20 70 68 72 61 73 65 73 20 69 6e 20 74 68  ll phrases in th
47f9f 65 20 71 75 65 72 79 0d 0a 20 20 2a 2a 20 65 78  e query..  ** ex
47fa0 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 61 70  pression that ap
47fa1 70 65 61 72 20 69 6e 20 74 68 65 20 63 75 72 72  pear in the curr
47fa2 65 6e 74 20 72 6f 77 2e 20 49 66 20 73 75 63 68  ent row. If such
47fa3 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 20 74   a fragment of t
47fa4 65 78 74 0d 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74  ext..  ** cannot
47fa5 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65 20 73   be found, the s
47fa6 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20  econd iteration 
47fa7 6f 66 20 74 68 65 20 6c 6f 6f 70 20 61 74 74 65  of the loop atte
47fa8 6d 70 74 73 20 74 6f 20 6c 6f 63 61 74 65 0d 0a  mpts to locate..
47fa9 20 20 2a 2a 20 61 20 70 61 69 72 20 6f 66 20 66    ** a pair of f
47faa 72 61 67 6d 65 6e 74 73 2c 20 61 6e 64 20 73 6f  ragments, and so
47fab 20 6f 6e 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 6e   on...  */..  in
47fac 74 20 6e 53 6e 69 70 70 65 74 20 3d 20 30 3b 20  t nSnippet = 0; 
47fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47fae 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d   Number of fragm
47faf 65 6e 74 73 20 69 6e 20 74 68 69 73 20 73 6e 69  ents in this sni
47fb0 70 70 65 74 20 2a 2f 0d 0a 20 20 53 6e 69 70 70  ppet */..  Snipp
47fb1 65 74 46 72 61 67 6d 65 6e 74 20 61 53 6e 69 70  etFragment aSnip
47fb2 70 65 74 5b 34 5d 3b 20 20 20 20 2f 2a 20 4d 61  pet[4];    /* Ma
47fb3 78 69 6d 75 6d 20 6f 66 20 34 20 66 72 61 67 6d  ximum of 4 fragm
47fb4 65 6e 74 73 20 70 65 72 20 73 6e 69 70 70 65 74  ents per snippet
47fb5 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 46 54 6f 6b   */..  int nFTok
47fb6 65 6e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  en = -1;        
47fb7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
47fb8 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 65 61   of tokens in ea
47fb9 63 68 20 66 72 61 67 6d 65 6e 74 20 2a 2f 0d 0a  ch fragment */..
47fba 0d 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 70  ..  if( !pCsr->p
47fbb 45 78 70 72 20 29 7b 0d 0a 20 20 20 20 73 71 6c  Expr ){..    sql
47fbc 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
47fbd 28 70 43 74 78 2c 20 22 22 2c 20 30 2c 20 53 51  (pCtx, "", 0, SQ
47fbe 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a 20  LITE_STATIC);.. 
47fbf 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d     return;..  }.
47fc0 0a 0d 0a 20 20 66 6f 72 28 6e 53 6e 69 70 70 65  ...  for(nSnippe
47fc1 74 3d 31 3b 20 31 3b 20 6e 53 6e 69 70 70 65 74  t=1; 1; nSnippet
47fc2 2b 2b 29 7b 0d 0a 0d 0a 20 20 20 20 69 6e 74 20  ++){....    int 
47fc3 69 53 6e 69 70 3b 20 20 20 20 20 20 20 20 20 20  iSnip;          
47fc4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
47fc5 70 20 63 6f 75 6e 74 65 72 20 30 2e 2e 6e 53 6e  p counter 0..nSn
47fc6 69 70 70 65 74 2d 31 20 2a 2f 0d 0a 20 20 20 20  ippet-1 */..    
47fc7 75 36 34 20 6d 43 6f 76 65 72 65 64 20 3d 20 30  u64 mCovered = 0
47fc8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
47fc9 20 42 69 74 6d 61 73 6b 20 6f 66 20 70 68 72 61   Bitmask of phra
47fca 73 65 73 20 63 6f 76 65 72 65 64 20 62 79 20 73  ses covered by s
47fcb 6e 69 70 70 65 74 20 2a 2f 0d 0a 20 20 20 20 75  nippet */..    u
47fcc 36 34 20 6d 53 65 65 6e 20 3d 20 30 3b 20 20 20  64 mSeen = 0;   
47fcd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47fce 42 69 74 6d 61 73 6b 20 6f 66 20 70 68 72 61 73  Bitmask of phras
47fcf 65 73 20 73 65 65 6e 20 62 79 20 42 65 73 74 53  es seen by BestS
47fd0 6e 69 70 70 65 74 28 29 20 2a 2f 0d 0a 0d 0a 20  nippet() */.... 
47fd1 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3e 3d 30     if( nToken>=0
47fd2 20 29 7b 0d 0a 20 20 20 20 20 20 6e 46 54 6f 6b   ){..      nFTok
47fd3 65 6e 20 3d 20 28 6e 54 6f 6b 65 6e 2b 6e 53 6e  en = (nToken+nSn
47fd4 69 70 70 65 74 2d 31 29 20 2f 20 6e 53 6e 69 70  ippet-1) / nSnip
47fd5 70 65 74 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  pet;..    }else{
47fd6 0d 0a 20 20 20 20 20 20 6e 46 54 6f 6b 65 6e 20  ..      nFToken 
47fd7 3d 20 2d 31 20 2a 20 6e 54 6f 6b 65 6e 3b 0d 0a  = -1 * nToken;..
47fd8 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 66 6f 72      }....    for
47fd9 28 69 53 6e 69 70 3d 30 3b 20 69 53 6e 69 70 3c  (iSnip=0; iSnip<
47fda 6e 53 6e 69 70 70 65 74 3b 20 69 53 6e 69 70 2b  nSnippet; iSnip+
47fdb 2b 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69  +){..      int i
47fdc 42 65 73 74 53 63 6f 72 65 20 3d 20 2d 31 3b 20  BestScore = -1; 
47fdd 20 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 73         /* Best s
47fde 63 6f 72 65 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  core of columns 
47fdf 63 68 65 63 6b 65 64 20 73 6f 20 66 61 72 20 2a  checked so far *
47fe0 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65  /..      int iRe
47fe1 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ad;             
47fe2 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
47fe3 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
47fe4 63 6f 6c 75 6d 6e 73 20 2a 2f 0d 0a 20 20 20 20  columns */..    
47fe5 20 20 53 6e 69 70 70 65 74 46 72 61 67 6d 65 6e    SnippetFragmen
47fe6 74 20 2a 70 46 72 61 67 6d 65 6e 74 20 3d 20 26  t *pFragment = &
47fe7 61 53 6e 69 70 70 65 74 5b 69 53 6e 69 70 5d 3b  aSnippet[iSnip];
47fe8 0d 0a 0d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  ....      memset
47fe9 28 70 46 72 61 67 6d 65 6e 74 2c 20 30 2c 20 73  (pFragment, 0, s
47fea 69 7a 65 6f 66 28 2a 70 46 72 61 67 6d 65 6e 74  izeof(*pFragment
47feb 29 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 2f 2a 20  ));....      /* 
47fec 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c  Loop through all
47fed 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
47fee 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73  table being cons
47fef 69 64 65 72 65 64 20 66 6f 72 20 73 6e 69 70 70  idered for snipp
47ff0 65 74 73 2e 0d 0a 20 20 20 20 20 20 2a 2a 20 49  ets...      ** I
47ff1 66 20 74 68 65 20 69 43 6f 6c 20 61 72 67 75 6d  f the iCol argum
47ff2 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
47ff3 74 69 6f 6e 20 77 61 73 20 6e 65 67 61 74 69 76  tion was negativ
47ff4 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 61 6c  e, this means al
47ff5 6c 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  l..      ** colu
47ff6 6d 6e 73 20 6f 66 20 74 68 65 20 46 54 53 33 20  mns of the FTS3 
47ff7 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65  table. Otherwise
47ff8 2c 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 20 69 43  , only column iC
47ff9 6f 6c 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ol is considered
47ffa 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20  ...      */..   
47ffb 20 20 20 66 6f 72 28 69 52 65 61 64 3d 30 3b 20     for(iRead=0; 
47ffc 69 52 65 61 64 3c 70 54 61 62 2d 3e 6e 43 6f 6c  iRead<pTab->nCol
47ffd 75 6d 6e 3b 20 69 52 65 61 64 2b 2b 29 7b 0d 0a  umn; iRead++){..
47ffe 20 20 20 20 20 20 20 20 53 6e 69 70 70 65 74 46          SnippetF
47fff 72 61 67 6d 65 6e 74 20 73 46 20 3d 20 7b 30 2c  ragment sF = {0,
48000 20 30 2c 20 30 2c 20 30 7d 3b 0d 0a 20 20 20 20   0, 0, 0};..    
48001 20 20 20 20 69 6e 74 20 69 53 3b 0d 0a 20 20 20      int iS;..   
48002 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30       if( iCol>=0
48003 20 26 26 20 69 52 65 61 64 21 3d 69 43 6f 6c 20   && iRead!=iCol 
48004 29 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 0d 0a 20  ) continue;.... 
48005 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74         /* Find t
48006 68 65 20 62 65 73 74 20 73 6e 69 70 70 65 74 20  he best snippet 
48007 6f 66 20 6e 46 54 6f 6b 65 6e 20 74 6f 6b 65 6e  of nFToken token
48008 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 52 65 61  s in column iRea
48009 64 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 72  d. */..        r
4800a 63 20 3d 20 66 74 73 33 42 65 73 74 53 6e 69 70  c = fts3BestSnip
4800b 70 65 74 28 6e 46 54 6f 6b 65 6e 2c 20 70 43 73  pet(nFToken, pCs
4800c 72 2c 20 69 52 65 61 64 2c 20 6d 43 6f 76 65 72  r, iRead, mCover
4800d 65 64 2c 20 26 6d 53 65 65 6e 2c 20 26 73 46 2c  ed, &mSeen, &sF,
4800e 20 26 69 53 29 3b 0d 0a 20 20 20 20 20 20 20 20   &iS);..        
4800f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
48010 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  K ){..          
48011 67 6f 74 6f 20 73 6e 69 70 70 65 74 5f 6f 75 74  goto snippet_out
48012 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
48013 20 20 20 20 20 20 69 66 28 20 69 53 3e 69 42 65        if( iS>iBe
48014 73 74 53 63 6f 72 65 20 29 7b 0d 0a 20 20 20 20  stScore ){..    
48015 20 20 20 20 20 20 2a 70 46 72 61 67 6d 65 6e 74        *pFragment
48016 20 3d 20 73 46 3b 0d 0a 20 20 20 20 20 20 20 20   = sF;..        
48017 20 20 69 42 65 73 74 53 63 6f 72 65 20 3d 20 69    iBestScore = i
48018 53 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20  S;..        }.. 
48019 20 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20       }....      
4801a 6d 43 6f 76 65 72 65 64 20 7c 3d 20 70 46 72 61  mCovered |= pFra
4801b 67 6d 65 6e 74 2d 3e 63 6f 76 65 72 65 64 3b 0d  gment->covered;.
4801c 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 2f 2a  .    }....    /*
4801d 20 49 66 20 61 6c 6c 20 71 75 65 72 79 20 70 68   If all query ph
4801e 72 61 73 65 73 20 73 65 65 6e 20 62 79 20 66 74  rases seen by ft
4801f 73 33 42 65 73 74 53 6e 69 70 70 65 74 28 29 20  s3BestSnippet() 
48020 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 61  are present in a
48021 74 20 6c 65 61 73 74 0d 0a 20 20 20 20 2a 2a 20  t least..    ** 
48022 6f 6e 65 20 6f 66 20 74 68 65 20 6e 53 6e 69 70  one of the nSnip
48023 70 65 74 20 73 6e 69 70 70 65 74 20 66 72 61 67  pet snippet frag
48024 6d 65 6e 74 73 2c 20 62 72 65 61 6b 20 6f 75 74  ments, break out
48025 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0d 0a 20   of the loop... 
48026 20 20 20 2a 2f 0d 0a 20 20 20 20 61 73 73 65 72     */..    asser
48027 74 28 20 28 6d 43 6f 76 65 72 65 64 26 6d 53 65  t( (mCovered&mSe
48028 65 6e 29 3d 3d 6d 43 6f 76 65 72 65 64 20 29 3b  en)==mCovered );
48029 0d 0a 20 20 20 20 69 66 28 20 6d 53 65 65 6e 3d  ..    if( mSeen=
4802a 3d 6d 43 6f 76 65 72 65 64 20 7c 7c 20 6e 53 6e  =mCovered || nSn
4802b 69 70 70 65 74 3d 3d 53 69 7a 65 6f 66 41 72 72  ippet==SizeofArr
4802c 61 79 28 61 53 6e 69 70 70 65 74 29 20 29 20 62  ay(aSnippet) ) b
4802d 72 65 61 6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  reak;..  }....  
4802e 61 73 73 65 72 74 28 20 6e 46 54 6f 6b 65 6e 3e  assert( nFToken>
4802f 30 20 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d  0 );....  for(i=
48030 30 3b 20 69 3c 6e 53 6e 69 70 70 65 74 20 26 26  0; i<nSnippet &&
48031 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
48032 69 2b 2b 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20  i++){..    rc = 
48033 66 74 73 33 53 6e 69 70 70 65 74 54 65 78 74 28  fts3SnippetText(
48034 70 43 73 72 2c 20 26 61 53 6e 69 70 70 65 74 5b  pCsr, &aSnippet[
48035 69 5d 2c 20 0d 0a 20 20 20 20 20 20 20 20 69 2c  i], ..        i,
48036 20 28 69 3d 3d 6e 53 6e 69 70 70 65 74 2d 31 29   (i==nSnippet-1)
48037 2c 20 6e 46 54 6f 6b 65 6e 2c 20 7a 53 74 61 72  , nFToken, zStar
48038 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73  t, zEnd, zEllips
48039 69 73 2c 20 26 72 65 73 0d 0a 20 20 20 20 29 3b  is, &res..    );
4803a 0d 0a 20 20 7d 0d 0a 0d 0a 20 73 6e 69 70 70 65  ..  }.... snippe
4803b 74 5f 6f 75 74 3a 0d 0a 20 20 73 71 6c 69 74 65  t_out:..  sqlite
4803c 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f  3Fts3SegmentsClo
4803d 73 65 28 70 54 61 62 29 3b 0d 0a 20 20 69 66 28  se(pTab);..  if(
4803e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
4803f 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  {..    sqlite3_r
48040 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
48041 28 70 43 74 78 2c 20 72 63 29 3b 0d 0a 20 20 20  (pCtx, rc);..   
48042 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65   sqlite3_free(re
48043 73 2e 7a 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  s.z);..  }else{.
48044 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
48045 75 6c 74 5f 74 65 78 74 28 70 43 74 78 2c 20 72  ult_text(pCtx, r
48046 65 73 2e 7a 2c 20 2d 31 2c 20 73 71 6c 69 74 65  es.z, -1, sqlite
48047 33 5f 66 72 65 65 29 3b 0d 0a 20 20 7d 0d 0a 7d  3_free);..  }..}
48048 0d 0a 0d 0a 0d 0a 74 79 70 65 64 65 66 20 73 74  ......typedef st
48049 72 75 63 74 20 54 65 72 6d 4f 66 66 73 65 74 20  ruct TermOffset 
4804a 54 65 72 6d 4f 66 66 73 65 74 3b 0d 0a 74 79 70  TermOffset;..typ
4804b 65 64 65 66 20 73 74 72 75 63 74 20 54 65 72 6d  edef struct Term
4804c 4f 66 66 73 65 74 43 74 78 20 54 65 72 6d 4f 66  OffsetCtx TermOf
4804d 66 73 65 74 43 74 78 3b 0d 0a 0d 0a 73 74 72 75  fsetCtx;....stru
4804e 63 74 20 54 65 72 6d 4f 66 66 73 65 74 20 7b 0d  ct TermOffset {.
4804f 0a 20 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 20  .  char *pList; 
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48051 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 2d 6c     /* Position-l
48052 69 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50  ist */..  int iP
48053 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  os;             
48054 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73            /* Pos
48055 69 74 69 6f 6e 20 6a 75 73 74 20 72 65 61 64 20  ition just read 
48056 66 72 6f 6d 20 70 4c 69 73 74 20 2a 2f 0d 0a 20  from pList */.. 
48057 20 69 6e 74 20 69 4f 66 66 3b 20 20 20 20 20 20   int iOff;      
48058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48059 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 74 68   /* Offset of th
4805a 69 73 20 74 65 72 6d 20 66 72 6f 6d 20 72 65 61  is term from rea
4805b 64 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0d 0a  d positions */..
4805c 7d 3b 0d 0a 0d 0a 73 74 72 75 63 74 20 54 65 72  };....struct Ter
4805d 6d 4f 66 66 73 65 74 43 74 78 20 7b 0d 0a 20 20  mOffsetCtx {..  
4805e 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
4805f 3b 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  ;..  int iCol;  
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48061 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f       /* Column o
48062 66 20 74 61 62 6c 65 20 74 6f 20 70 6f 70 75 6c  f table to popul
48063 61 74 65 20 61 54 65 72 6d 20 66 6f 72 20 2a 2f  ate aTerm for */
48064 0d 0a 20 20 69 6e 74 20 69 54 65 72 6d 3b 0d 0a  ..  int iTerm;..
48065 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
48066 69 44 6f 63 69 64 3b 0d 0a 20 20 54 65 72 6d 4f  iDocid;..  TermO
48067 66 66 73 65 74 20 2a 61 54 65 72 6d 3b 0d 0a 7d  ffset *aTerm;..}
48068 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  ;..../*..** This
48069 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20   function is an 
4806a 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65 28  fts3ExprIterate(
4806b 29 20 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 20  ) callback used 
4806c 62 79 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66  by sqlite3Fts3Of
4806d 66 73 65 74 73 28 29 2e 0d 0a 2a 2f 0d 0a 73 74  fsets()...*/..st
4806e 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
4806f 72 54 65 72 6d 4f 66 66 73 65 74 49 6e 69 74 28  rTermOffsetInit(
48070 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
48071 20 69 6e 74 20 69 50 68 72 61 73 65 2c 20 76 6f   int iPhrase, vo
48072 69 64 20 2a 63 74 78 29 7b 0d 0a 20 20 54 65 72  id *ctx){..  Ter
48073 6d 4f 66 66 73 65 74 43 74 78 20 2a 70 20 3d 20  mOffsetCtx *p = 
48074 28 54 65 72 6d 4f 66 66 73 65 74 43 74 78 20 2a  (TermOffsetCtx *
48075 29 63 74 78 3b 0d 0a 20 20 69 6e 74 20 6e 54 65  )ctx;..  int nTe
48076 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
48077 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
48078 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
48079 70 68 72 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74  phrase */..  int
4807a 20 69 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   iTerm;         
4807b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4807c 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
4807d 75 67 68 20 6e 54 65 72 6d 20 70 68 72 61 73 65  ugh nTerm phrase
4807e 20 74 65 72 6d 73 20 2a 2f 0d 0a 20 20 63 68 61   terms */..  cha
4807f 72 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20  r *pList;       
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
48081 50 6f 69 6e 74 65 72 20 74 6f 20 70 6f 73 69 74  Pointer to posit
48082 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 70 68 72  ion list for phr
48083 61 73 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 50  ase */..  int iP
48084 6f 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  os = 0;         
48085 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
48086 73 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70  st position in p
48087 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 2a 2f 0d  osition-list */.
48088 0a 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ...  UNUSED_PARA
48089 4d 45 54 45 52 28 69 50 68 72 61 73 65 29 3b 0d  METER(iPhrase);.
4808a 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
4808b 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65  e3Fts3EvalPhrase
4808c 50 6f 73 6c 69 73 74 28 70 2d 3e 70 43 73 72 2c  Poslist(p->pCsr,
4808d 20 70 45 78 70 72 2c 20 70 2d 3e 69 43 6f 6c 29   pExpr, p->iCol)
4808e 3b 0d 0a 20 20 6e 54 65 72 6d 20 3d 20 70 45 78  ;..  nTerm = pEx
4808f 70 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  pr->pPhrase->nTo
48090 6b 65 6e 3b 0d 0a 20 20 69 66 28 20 70 4c 69 73  ken;..  if( pLis
48091 74 20 29 7b 0d 0a 20 20 20 20 66 74 73 33 47 65  t ){..    fts3Ge
48092 74 44 65 6c 74 61 50 6f 73 69 74 69 6f 6e 28 26  tDeltaPosition(&
48093 70 4c 69 73 74 2c 20 26 69 50 6f 73 29 3b 0d 0a  pList, &iPos);..
48094 20 20 20 20 61 73 73 65 72 74 28 20 69 50 6f 73      assert( iPos
48095 3e 3d 30 20 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  >=0 );..  }.... 
48096 20 66 6f 72 28 69 54 65 72 6d 3d 30 3b 20 69 54   for(iTerm=0; iT
48097 65 72 6d 3c 6e 54 65 72 6d 3b 20 69 54 65 72 6d  erm<nTerm; iTerm
48098 2b 2b 29 7b 0d 0a 20 20 20 20 54 65 72 6d 4f 66  ++){..    TermOf
48099 66 73 65 74 20 2a 70 54 20 3d 20 26 70 2d 3e 61  fset *pT = &p->a
4809a 54 65 72 6d 5b 70 2d 3e 69 54 65 72 6d 2b 2b 5d  Term[p->iTerm++]
4809b 3b 0d 0a 20 20 20 20 70 54 2d 3e 69 4f 66 66 20  ;..    pT->iOff 
4809c 3d 20 6e 54 65 72 6d 2d 69 54 65 72 6d 2d 31 3b  = nTerm-iTerm-1;
4809d 0d 0a 20 20 20 20 70 54 2d 3e 70 4c 69 73 74 20  ..    pT->pList 
4809e 3d 20 70 4c 69 73 74 3b 0d 0a 20 20 20 20 70 54  = pList;..    pT
4809f 2d 3e 69 50 6f 73 20 3d 20 69 50 6f 73 3b 0d 0a  ->iPos = iPos;..
480a0 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20    }....  return 
480a1 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
480a2 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
480a3 74 61 74 69 6f 6e 20 6f 66 20 6f 66 66 73 65 74  tation of offset
480a4 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0d 0a 2a  s() function...*
480a5 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
480a6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
480a7 73 33 4f 66 66 73 65 74 73 28 0d 0a 20 20 73 71  s3Offsets(..  sq
480a8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
480a9 43 74 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ctx,          /*
480aa 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e   SQLite function
480ab 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f   call context */
480ac 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a  ..  Fts3Cursor *
480ad 70 43 73 72 20 20 20 20 20 20 20 20 20 20 20 20  pCsr            
480ae 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62      /* Cursor ob
480af 6a 65 63 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 46  ject */..){..  F
480b0 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  ts3Table *pTab =
480b1 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43   (Fts3Table *)pC
480b2 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0d  sr->base.pVtab;.
480b3 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
480b4 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
480b5 74 20 2a 70 4d 6f 64 20 3d 20 70 54 61 62 2d 3e  t *pMod = pTab->
480b6 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64  pTokenizer->pMod
480b7 75 6c 65 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68  ule;..  const ch
480b8 61 72 20 2a 5a 44 55 4d 4d 59 3b 20 20 20 20 20  ar *ZDUMMY;     
480b9 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79          /* Dummy
480ba 20 61 72 67 75 6d 65 6e 74 20 75 73 65 64 20 77   argument used w
480bb 69 74 68 20 78 4e 65 78 74 28 29 20 2a 2f 0d 0a  ith xNext() */..
480bc 20 20 69 6e 74 20 4e 44 55 4d 4d 59 3b 20 20 20    int NDUMMY;   
480bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480be 20 20 2f 2a 20 44 75 6d 6d 79 20 61 72 67 75 6d    /* Dummy argum
480bf 65 6e 74 20 75 73 65 64 20 77 69 74 68 20 78 4e  ent used with xN
480c0 65 78 74 28 29 20 2a 2f 0d 0a 20 20 69 6e 74 20  ext() */..  int 
480c1 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
480c2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
480c3 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0d 0a 20  eturn Code */.. 
480c4 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20   int nToken;    
480c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480c6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f   /* Number of to
480c7 6b 65 6e 73 20 69 6e 20 71 75 65 72 79 20 2a 2f  kens in query */
480c8 0d 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  ..  int iCol;   
480c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480ca 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 63 75      /* Column cu
480cb 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 70 72  rrently being pr
480cc 6f 63 65 73 73 65 64 20 2a 2f 0d 0a 20 20 53 74  ocessed */..  St
480cd 72 42 75 66 66 65 72 20 72 65 73 20 3d 20 7b 30  rBuffer res = {0
480ce 2c 20 30 2c 20 30 7d 3b 20 20 20 20 20 20 2f 2a  , 0, 0};      /*
480cf 20 52 65 73 75 6c 74 20 73 74 72 69 6e 67 20 2a   Result string *
480d0 2f 0d 0a 20 20 54 65 72 6d 4f 66 66 73 65 74 43  /..  TermOffsetC
480d1 74 78 20 73 43 74 78 3b 20 20 20 20 20 20 20 20  tx sCtx;        
480d2 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
480d3 66 6f 72 20 66 74 73 33 45 78 70 72 54 65 72 6d  for fts3ExprTerm
480d4 4f 66 66 73 65 74 49 6e 69 74 28 29 20 2a 2f 0d  OffsetInit() */.
480d5 0a 0d 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e  ...  if( !pCsr->
480d6 70 45 78 70 72 20 29 7b 0d 0a 20 20 20 20 73 71  pExpr ){..    sq
480d7 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
480d8 74 28 70 43 74 78 2c 20 22 22 2c 20 30 2c 20 53  t(pCtx, "", 0, S
480d9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0d 0a  QLITE_STATIC);..
480da 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
480db 0d 0a 0d 0a 20 20 6d 65 6d 73 65 74 28 26 73 43  ....  memset(&sC
480dc 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43  tx, 0, sizeof(sC
480dd 74 78 29 29 3b 0d 0a 20 20 61 73 73 65 72 74 28  tx));..  assert(
480de 20 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72 65   pCsr->isRequire
480df 53 65 65 6b 3d 3d 30 20 29 3b 0d 0a 0d 0a 20 20  Seek==0 );....  
480e0 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
480e1 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
480e2 74 68 65 20 71 75 65 72 79 20 2a 2f 0d 0a 20 20  the query */..  
480e3 72 63 20 3d 20 66 74 73 33 45 78 70 72 4c 6f 61  rc = fts3ExprLoa
480e4 64 44 6f 63 6c 69 73 74 73 28 70 43 73 72 2c 20  dDoclists(pCsr, 
480e5 30 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0d 0a 20 20  0, &nToken);..  
480e6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
480e7 4b 20 29 20 67 6f 74 6f 20 6f 66 66 73 65 74 73  K ) goto offsets
480e8 5f 6f 75 74 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c  _out;....  /* Al
480e9 6c 6f 63 61 74 65 20 74 68 65 20 61 72 72 61 79  locate the array
480ea 20 6f 66 20 54 65 72 6d 4f 66 66 73 65 74 20 69   of TermOffset i
480eb 74 65 72 61 74 6f 72 73 2e 20 2a 2f 0d 0a 20 20  terators. */..  
480ec 73 43 74 78 2e 61 54 65 72 6d 20 3d 20 28 54 65  sCtx.aTerm = (Te
480ed 72 6d 4f 66 66 73 65 74 20 2a 29 73 71 6c 69 74  rmOffset *)sqlit
480ee 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
480ef 28 54 65 72 6d 4f 66 66 73 65 74 29 2a 6e 54 6f  (TermOffset)*nTo
480f0 6b 65 6e 29 3b 0d 0a 20 20 69 66 28 20 30 3d 3d  ken);..  if( 0==
480f1 73 43 74 78 2e 61 54 65 72 6d 20 29 7b 0d 0a 20  sCtx.aTerm ){.. 
480f2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
480f3 4f 4d 45 4d 3b 0d 0a 20 20 20 20 67 6f 74 6f 20  OMEM;..    goto 
480f4 6f 66 66 73 65 74 73 5f 6f 75 74 3b 0d 0a 20 20  offsets_out;..  
480f5 7d 0d 0a 20 20 73 43 74 78 2e 69 44 6f 63 69 64  }..  sCtx.iDocid
480f6 20 3d 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64   = pCsr->iPrevId
480f7 3b 0d 0a 20 20 73 43 74 78 2e 70 43 73 72 20 3d  ;..  sCtx.pCsr =
480f8 20 70 43 73 72 3b 0d 0a 0d 0a 20 20 2f 2a 20 4c   pCsr;....  /* L
480f9 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
480fa 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 2c 20 61  table columns, a
480fb 70 70 65 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  ppending offset 
480fc 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 0d  information to .
480fd 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2d 62 75 66  .  ** string-buf
480fe 66 65 72 20 72 65 73 20 66 6f 72 20 65 61 63 68  fer res for each
480ff 20 63 6f 6c 75 6d 6e 2e 0d 0a 20 20 2a 2f 0d 0a   column...  */..
48100 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
48101 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e  ol<pTab->nColumn
48102 3b 20 69 43 6f 6c 2b 2b 29 7b 0d 0a 20 20 20 20  ; iCol++){..    
48103 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
48104 72 5f 63 75 72 73 6f 72 20 2a 70 43 3b 20 2f 2a  r_cursor *pC; /*
48105 20 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f   Tokenizer curso
48106 72 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 69 53  r */..    int iS
48107 74 61 72 74 3b 0d 0a 20 20 20 20 69 6e 74 20 69  tart;..    int i
48108 45 6e 64 3b 0d 0a 20 20 20 20 69 6e 74 20 69 43  End;..    int iC
48109 75 72 72 65 6e 74 3b 0d 0a 20 20 20 20 63 6f 6e  urrent;..    con
4810a 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0d 0a  st char *zDoc;..
4810b 20 20 20 20 69 6e 74 20 6e 44 6f 63 3b 0d 0a 0d      int nDoc;...
4810c 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
4810d 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ze the contents 
4810e 6f 66 20 73 43 74 78 2e 61 54 65 72 6d 5b 5d 20  of sCtx.aTerm[] 
4810f 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 2e  for column iCol.
48110 20 54 68 65 72 65 20 69 73 20 0d 0a 20 20 20 20   There is ..    
48111 2a 2a 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74  ** no way that t
48112 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 63 61  his operation ca
48113 6e 20 66 61 69 6c 2c 20 73 6f 20 74 68 65 20 72  n fail, so the r
48114 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 0d  eturn code from.
48115 0a 20 20 20 20 2a 2a 20 66 74 73 33 45 78 70 72  .    ** fts3Expr
48116 49 74 65 72 61 74 65 28 29 20 63 61 6e 20 62 65  Iterate() can be
48117 20 64 69 73 63 61 72 64 65 64 2e 0d 0a 20 20 20   discarded...   
48118 20 2a 2f 0d 0a 20 20 20 20 73 43 74 78 2e 69 43   */..    sCtx.iC
48119 6f 6c 20 3d 20 69 43 6f 6c 3b 0d 0a 20 20 20 20  ol = iCol;..    
4811a 73 43 74 78 2e 69 54 65 72 6d 20 3d 20 30 3b 0d  sCtx.iTerm = 0;.
4811b 0a 20 20 20 20 28 76 6f 69 64 29 66 74 73 33 45  .    (void)fts3E
4811c 78 70 72 49 74 65 72 61 74 65 28 70 43 73 72 2d  xprIterate(pCsr-
4811d 3e 70 45 78 70 72 2c 20 66 74 73 33 45 78 70 72  >pExpr, fts3Expr
4811e 54 65 72 6d 4f 66 66 73 65 74 49 6e 69 74 2c 20  TermOffsetInit, 
4811f 28 76 6f 69 64 20 2a 29 26 73 43 74 78 29 3b 0d  (void *)&sCtx);.
48120 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 74 72 65 69  ...    /* Retrei
48121 76 65 20 74 68 65 20 74 65 78 74 20 73 74 6f 72  ve the text stor
48122 65 64 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f  ed in column iCo
48123 6c 2e 20 49 66 20 61 6e 20 53 51 4c 20 4e 55 4c  l. If an SQL NUL
48124 4c 20 69 73 20 73 74 6f 72 65 64 20 0d 0a 20 20  L is stored ..  
48125 20 20 2a 2a 20 69 6e 20 63 6f 6c 75 6d 6e 20 69    ** in column i
48126 43 6f 6c 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69  Col, jump immedi
48127 61 74 65 6c 79 20 74 6f 20 74 68 65 20 6e 65 78  ately to the nex
48128 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
48129 68 65 20 6c 6f 6f 70 2e 0d 0a 20 20 20 20 2a 2a  he loop...    **
4812a 20 49 66 20 61 6e 20 4f 4f 4d 20 6f 63 63 75 72   If an OOM occur
4812b 73 20 77 68 69 6c 65 20 72 65 74 72 69 65 76 69  s while retrievi
4812c 6e 67 20 74 68 65 20 64 61 74 61 20 28 74 68 69  ng the data (thi
4812d 73 20 63 61 6e 20 68 61 70 70 65 6e 20 69 66 20  s can happen if 
4812e 53 51 4c 69 74 65 0d 0a 20 20 20 20 2a 2a 20 6e  SQLite..    ** n
4812f 65 65 64 73 20 74 6f 20 74 72 61 6e 73 66 6f 72  eeds to transfor
48130 6d 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20  m the data from 
48131 75 74 66 2d 31 36 20 74 6f 20 75 74 66 2d 38 29  utf-16 to utf-8)
48132 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
48133 4e 4f 4d 45 4d 20 0d 0a 20 20 20 20 2a 2a 20 74  NOMEM ..    ** t
48134 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 0d 0a  o the caller. ..
48135 20 20 20 20 2a 2f 0d 0a 20 20 20 20 7a 44 6f 63      */..    zDoc
48136 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
48137 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
48138 74 65 78 74 28 70 43 73 72 2d 3e 70 53 74 6d 74  text(pCsr->pStmt
48139 2c 20 69 43 6f 6c 2b 31 29 3b 0d 0a 20 20 20 20  , iCol+1);..    
4813a 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63  nDoc = sqlite3_c
4813b 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 73 72  olumn_bytes(pCsr
4813c 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29  ->pStmt, iCol+1)
4813d 3b 0d 0a 20 20 20 20 69 66 28 20 7a 44 6f 63 3d  ;..    if( zDoc=
4813e 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  =0 ){..      if(
4813f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
48140 74 79 70 65 28 70 43 73 72 2d 3e 70 53 74 6d 74  type(pCsr->pStmt
48141 2c 20 69 43 6f 6c 2b 31 29 3d 3d 53 51 4c 49 54  , iCol+1)==SQLIT
48142 45 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 20  E_NULL ){..     
48143 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0d 0a 20 20     continue;..  
48144 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 72 63 20      }..      rc 
48145 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  = SQLITE_NOMEM;.
48146 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 66 66 73  .      goto offs
48147 65 74 73 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d  ets_out;..    }.
48148 0a 0d 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  ...    /* Initia
48149 6c 69 7a 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72  lize a tokenizer
4814a 20 69 74 65 72 61 74 6f 72 20 74 6f 20 69 74 65   iterator to ite
4814b 72 61 74 65 20 74 68 72 6f 75 67 68 20 63 6f 6c  rate through col
4814c 75 6d 6e 20 69 43 6f 6c 2e 20 2a 2f 0d 0a 20 20  umn iCol. */..  
4814d 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4f 70    rc = pMod->xOp
4814e 65 6e 28 70 54 61 62 2d 3e 70 54 6f 6b 65 6e 69  en(pTab->pTokeni
4814f 7a 65 72 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c  zer, zDoc, nDoc,
48150 20 26 70 43 29 3b 0d 0a 20 20 20 20 69 66 28 20   &pC);..    if( 
48151 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
48152 67 6f 74 6f 20 6f 66 66 73 65 74 73 5f 6f 75 74  goto offsets_out
48153 3b 0d 0a 20 20 20 20 70 43 2d 3e 70 54 6f 6b 65  ;..    pC->pToke
48154 6e 69 7a 65 72 20 3d 20 70 54 61 62 2d 3e 70 54  nizer = pTab->pT
48155 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 0d 0a 20 20 20  okenizer;....   
48156 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65 78   rc = pMod->xNex
48157 74 28 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20 26  t(pC, &ZDUMMY, &
48158 4e 44 55 4d 4d 59 2c 20 26 69 53 74 61 72 74 2c  NDUMMY, &iStart,
48159 20 26 69 45 6e 64 2c 20 26 69 43 75 72 72 65 6e   &iEnd, &iCurren
4815a 74 29 3b 0d 0a 20 20 20 20 77 68 69 6c 65 28 20  t);..    while( 
4815b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
4815c 0d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20  ..      int i;  
4815d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4815e 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
4815f 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 65 72 6d  oop through term
48160 73 20 2a 2f 0d 0a 20 20 20 20 20 20 69 6e 74 20  s */..      int 
48161 69 4d 69 6e 50 6f 73 20 3d 20 30 78 37 46 46 46  iMinPos = 0x7FFF
48162 46 46 46 46 3b 20 20 20 2f 2a 20 50 6f 73 69 74  FFFF;   /* Posit
48163 69 6f 6e 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65  ion of next toke
48164 6e 20 2a 2f 0d 0a 20 20 20 20 20 20 54 65 72 6d  n */..      Term
48165 4f 66 66 73 65 74 20 2a 70 54 65 72 6d 20 3d 20  Offset *pTerm = 
48166 30 3b 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 4f  0;      /* TermO
48167 66 66 73 65 74 20 61 73 73 6f 63 69 61 74 65 64  ffset associated
48168 20 77 69 74 68 20 6e 65 78 74 20 74 6f 6b 65 6e   with next token
48169 20 2a 2f 0d 0a 0d 0a 20 20 20 20 20 20 66 6f 72   */....      for
4816a 28 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20  (i=0; i<nToken; 
4816b 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 54  i++){..        T
4816c 65 72 6d 4f 66 66 73 65 74 20 2a 70 54 20 3d 20  ermOffset *pT = 
4816d 26 73 43 74 78 2e 61 54 65 72 6d 5b 69 5d 3b 0d  &sCtx.aTerm[i];.
4816e 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 2d  .        if( pT-
4816f 3e 70 4c 69 73 74 20 26 26 20 28 70 54 2d 3e 69  >pList && (pT->i
48170 50 6f 73 2d 70 54 2d 3e 69 4f 66 66 29 3c 69 4d  Pos-pT->iOff)<iM
48171 69 6e 50 6f 73 20 29 7b 0d 0a 20 20 20 20 20 20  inPos ){..      
48172 20 20 20 20 69 4d 69 6e 50 6f 73 20 3d 20 70 54      iMinPos = pT
48173 2d 3e 69 50 6f 73 2d 70 54 2d 3e 69 4f 66 66 3b  ->iPos-pT->iOff;
48174 0d 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72  ..          pTer
48175 6d 20 3d 20 70 54 3b 0d 0a 20 20 20 20 20 20 20  m = pT;..       
48176 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 0d 0a 20   }..      }.... 
48177 20 20 20 20 20 69 66 28 20 21 70 54 65 72 6d 20       if( !pTerm 
48178 29 7b 0d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41  ){..        /* A
48179 6c 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74  ll offsets for t
4817a 68 69 73 20 63 6f 6c 75 6d 6e 20 68 61 76 65 20  his column have 
4817b 62 65 65 6e 20 67 61 74 68 65 72 65 64 2e 20 2a  been gathered. *
4817c 2f 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  /..        rc = 
4817d 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0d 0a 20 20  SQLITE_DONE;..  
4817e 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
4817f 20 20 20 20 61 73 73 65 72 74 28 20 69 43 75 72      assert( iCur
48180 72 65 6e 74 3c 3d 69 4d 69 6e 50 6f 73 20 29 3b  rent<=iMinPos );
48181 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d  ..        if( 0=
48182 3d 28 30 78 46 45 26 2a 70 54 65 72 6d 2d 3e 70  =(0xFE&*pTerm->p
48183 4c 69 73 74 29 20 29 7b 0d 0a 20 20 20 20 20 20  List) ){..      
48184 20 20 20 20 70 54 65 72 6d 2d 3e 70 4c 69 73 74      pTerm->pList
48185 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 7d   = 0;..        }
48186 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 20  else{..         
48187 20 66 74 73 33 47 65 74 44 65 6c 74 61 50 6f 73   fts3GetDeltaPos
48188 69 74 69 6f 6e 28 26 70 54 65 72 6d 2d 3e 70 4c  ition(&pTerm->pL
48189 69 73 74 2c 20 26 70 54 65 72 6d 2d 3e 69 50 6f  ist, &pTerm->iPo
4818a 73 29 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a  s);..        }..
4818b 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
4818c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
4818d 69 43 75 72 72 65 6e 74 3c 69 4d 69 6e 50 6f 73  iCurrent<iMinPos
4818e 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   ){..          r
4818f 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65 78 74 28  c = pMod->xNext(
48190 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20 26 4e 44  pC, &ZDUMMY, &ND
48191 55 4d 4d 59 2c 20 26 69 53 74 61 72 74 2c 20 26  UMMY, &iStart, &
48192 69 45 6e 64 2c 20 26 69 43 75 72 72 65 6e 74 29  iEnd, &iCurrent)
48193 3b 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20 20  ;..        }..  
48194 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
48195 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
48196 20 20 20 20 20 20 63 68 61 72 20 61 42 75 66 66        char aBuff
48197 65 72 5b 36 34 5d 3b 0d 0a 20 20 20 20 20 20 20  er[64];..       
48198 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
48199 6e 74 66 28 73 69 7a 65 6f 66 28 61 42 75 66 66  ntf(sizeof(aBuff
4819a 65 72 29 2c 20 61 42 75 66 66 65 72 2c 20 0d 0a  er), aBuffer, ..
4819b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
4819c 64 20 25 64 20 25 64 20 25 64 20 22 2c 20 69 43  d %d %d %d ", iC
4819d 6f 6c 2c 20 70 54 65 72 6d 2d 73 43 74 78 2e 61  ol, pTerm-sCtx.a
4819e 54 65 72 6d 2c 20 69 53 74 61 72 74 2c 20 69 45  Term, iStart, iE
4819f 6e 64 2d 69 53 74 61 72 74 0d 0a 20 20 20 20 20  nd-iStart..     
481a0 20 20 20 20 20 29 3b 0d 0a 20 20 20 20 20 20 20       );..       
481a1 20 20 20 72 63 20 3d 20 66 74 73 33 53 74 72 69     rc = fts3Stri
481a2 6e 67 41 70 70 65 6e 64 28 26 72 65 73 2c 20 61  ngAppend(&res, a
481a3 42 75 66 66 65 72 2c 20 2d 31 29 3b 0d 0a 20 20  Buffer, -1);..  
481a4 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
481a5 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
481a6 26 26 20 70 54 61 62 2d 3e 7a 43 6f 6e 74 65 6e  && pTab->zConten
481a7 74 54 62 6c 3d 3d 30 20 29 7b 0d 0a 20 20 20 20  tTbl==0 ){..    
481a8 20 20 20 20 20 20 72 63 20 3d 20 46 54 53 5f 43        rc = FTS_C
481a9 4f 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20  ORRUPT_VTAB;..  
481aa 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 7d        }..      }
481ab 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69 66 28  ..    }..    if(
481ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
481ad 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   ){..      rc = 
481ae 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20  SQLITE_OK;..    
481af 7d 0d 0a 0d 0a 20 20 20 20 70 4d 6f 64 2d 3e 78  }....    pMod->x
481b0 43 6c 6f 73 65 28 70 43 29 3b 0d 0a 20 20 20 20  Close(pC);..    
481b1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
481b2 4b 20 29 20 67 6f 74 6f 20 6f 66 66 73 65 74 73  K ) goto offsets
481b3 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 6f  _out;..  }.... o
481b4 66 66 73 65 74 73 5f 6f 75 74 3a 0d 0a 20 20 73  ffsets_out:..  s
481b5 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 74 78  qlite3_free(sCtx
481b6 2e 61 54 65 72 6d 29 3b 0d 0a 20 20 61 73 73 65  .aTerm);..  asse
481b7 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
481b8 4f 4e 45 20 29 3b 0d 0a 20 20 73 71 6c 69 74 65  ONE );..  sqlite
481b9 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f  3Fts3SegmentsClo
481ba 73 65 28 70 54 61 62 29 3b 0d 0a 20 20 69 66 28  se(pTab);..  if(
481bb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
481bc 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  {..    sqlite3_r
481bd 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
481be 28 70 43 74 78 2c 20 20 72 63 29 3b 0d 0a 20 20  (pCtx,  rc);..  
481bf 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
481c0 65 73 2e 7a 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b  es.z);..  }else{
481c1 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
481c2 73 75 6c 74 5f 74 65 78 74 28 70 43 74 78 2c 20  sult_text(pCtx, 
481c3 72 65 73 2e 7a 2c 20 72 65 73 2e 6e 2d 31 2c 20  res.z, res.n-1, 
481c4 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0d 0a  sqlite3_free);..
481c5 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 3b 0d 0a    }..  return;..
481c6 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c  }..../*..** Impl
481c7 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 61  ementation of ma
481c8 74 63 68 69 6e 66 6f 28 29 20 66 75 6e 63 74 69  tchinfo() functi
481c9 6f 6e 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f  on...*/..SQLITE_
481ca 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
481cb 69 74 65 33 46 74 73 33 4d 61 74 63 68 69 6e 66  ite3Fts3Matchinf
481cc 6f 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  o(..  sqlite3_co
481cd 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
481ce 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f        /* Functio
481cf 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74 20 2a  n call context *
481d0 2f 0d 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  /..  Fts3Cursor 
481d1 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
481d2 20 20 20 20 20 2f 2a 20 46 54 53 33 20 74 61 62       /* FTS3 tab
481d3 6c 65 20 63 75 72 73 6f 72 20 2a 2f 0d 0a 20 20  le cursor */..  
481d4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
481d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481d6 2f 2a 20 53 65 63 6f 6e 64 20 61 72 67 20 74 6f  /* Second arg to
481d7 20 6d 61 74 63 68 69 6e 66 6f 28 29 20 66 75 6e   matchinfo() fun
481d8 63 74 69 6f 6e 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  ction */..){..  
481d9 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 20  Fts3Table *pTab 
481da 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70  = (Fts3Table *)p
481db 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  Csr->base.pVtab;
481dc 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69  ..  int rc;..  i
481dd 6e 74 20 69 3b 0d 0a 20 20 63 6f 6e 73 74 20 63  nt i;..  const c
481de 68 61 72 20 2a 7a 46 6f 72 6d 61 74 3b 0d 0a 0d  har *zFormat;...
481df 0a 20 20 69 66 28 20 7a 41 72 67 20 29 7b 0d 0a  .  if( zArg ){..
481e0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 41 72      for(i=0; zAr
481e1 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  g[i]; i++){..   
481e2 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
481e3 30 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 66 74  0;..      if( ft
481e4 73 33 4d 61 74 63 68 69 6e 66 6f 43 68 65 63 6b  s3MatchinfoCheck
481e5 28 70 54 61 62 2c 20 7a 41 72 67 5b 69 5d 2c 20  (pTab, zArg[i], 
481e6 26 7a 45 72 72 29 20 29 7b 0d 0a 20 20 20 20 20  &zErr) ){..     
481e7 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
481e8 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  t_error(pContext
481e9 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0d 0a 20 20  , zErr, -1);..  
481ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
481eb 65 65 28 7a 45 72 72 29 3b 0d 0a 20 20 20 20 20  ee(zErr);..     
481ec 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20     return;..    
481ed 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20    }..    }..    
481ee 7a 46 6f 72 6d 61 74 20 3d 20 7a 41 72 67 3b 0d  zFormat = zArg;.
481ef 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a  .  }else{..    z
481f0 46 6f 72 6d 61 74 20 3d 20 46 54 53 33 5f 4d 41  Format = FTS3_MA
481f1 54 43 48 49 4e 46 4f 5f 44 45 46 41 55 4c 54 3b  TCHINFO_DEFAULT;
481f2 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21  ..  }....  if( !
481f3 70 43 73 72 2d 3e 70 45 78 70 72 20 29 7b 0d 0a  pCsr->pExpr ){..
481f4 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
481f5 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65 78 74  lt_blob(pContext
481f6 2c 20 22 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f  , "", 0, SQLITE_
481f7 53 54 41 54 49 43 29 3b 0d 0a 20 20 20 20 72 65  STATIC);..    re
481f8 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  turn;..  }....  
481f9 2f 2a 20 52 65 74 72 69 65 76 65 20 6d 61 74 63  /* Retrieve matc
481fa 68 69 6e 66 6f 28 29 20 64 61 74 61 2e 20 2a 2f  hinfo() data. */
481fb 0d 0a 20 20 72 63 20 3d 20 66 74 73 33 47 65 74  ..  rc = fts3Get
481fc 4d 61 74 63 68 69 6e 66 6f 28 70 43 73 72 2c 20  Matchinfo(pCsr, 
481fd 7a 46 6f 72 6d 61 74 29 3b 0d 0a 20 20 73 71 6c  zFormat);..  sql
481fe 69 74 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73  ite3Fts3Segments
481ff 43 6c 6f 73 65 28 70 54 61 62 29 3b 0d 0a 0d 0a  Close(pTab);....
48200 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
48201 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69  _OK ){..    sqli
48202 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
48203 5f 63 6f 64 65 28 70 43 6f 6e 74 65 78 74 2c 20  _code(pContext, 
48204 72 63 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  rc);..  }else{..
48205 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 43 73 72      int n = pCsr
48206 2d 3e 6e 4d 61 74 63 68 69 6e 66 6f 20 2a 20 73  ->nMatchinfo * s
48207 69 7a 65 6f 66 28 75 33 32 29 3b 0d 0a 20 20 20  izeof(u32);..   
48208 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
48209 62 6c 6f 62 28 70 43 6f 6e 74 65 78 74 2c 20 70  blob(pContext, p
4820a 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 2c  Csr->aMatchinfo,
4820b 20 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53   n, SQLITE_TRANS
4820c 49 45 4e 54 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a  IENT);..  }..}..
4820d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a  ..#endif..../***
4820e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
4820f 6f 66 20 66 74 73 33 5f 73 6e 69 70 70 65 74 2e  of fts3_snippet.
48210 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
48211 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a  **********/../**
48213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
48214 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20  in file rtree.c 
48215 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48216 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48217 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a  ***********/../*
48218 0d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  ..** 2001 Septem
48219 62 65 72 20 31 35 0d 0a 2a 2a 0d 0a 2a 2a 20 54  ber 15..**..** T
4821a 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
4821b 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
4821c 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
4821d 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0d  e.  In place of.
4821e 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
4821f 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
48220 65 73 73 69 6e 67 3a 0d 0a 2a 2a 0d 0a 2a 2a 20  essing:..**..** 
48221 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
48222 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
48223 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
48224 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
48225 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
48226 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
48227 2e 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  ...**    May you
48228 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
48229 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
4822a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0d   than you give..
4822b 0a 2a 2a 0d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**..***********
4822c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4822d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4822e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4822f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0d 0a  **************..
48230 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
48231 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 69  tains code for i
48232 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
48233 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6e 64  f the r-tree and
48234 20 72 2a 2d 74 72 65 65 0d 0a 2a 2a 20 61 6c 67   r*-tree..** alg
48235 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67 65 64  orithms packaged
48236 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20 76 69   as an SQLite vi
48237 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
48238 6c 65 2e 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a  le...*/..../*..*
48239 2a 20 44 61 74 61 62 61 73 65 20 46 6f 72 6d 61  * Database Forma
4823a 74 20 6f 66 20 52 2d 54 72 65 65 20 54 61 62 6c  t of R-Tree Tabl
4823b 65 73 0d 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  es..** ---------
4823c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4823d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a 2a 20  -------..**..** 
4823e 54 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  The data structu
4823f 72 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  re for a single 
48240 76 69 72 74 75 61 6c 20 72 2d 74 72 65 65 20 74  virtual r-tree t
48241 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69  able is stored i
48242 6e 20 74 68 72 65 65 20 0d 0a 2a 2a 20 6e 61 74  n three ..** nat
48243 69 76 65 20 53 51 4c 69 74 65 20 74 61 62 6c 65  ive SQLite table
48244 73 20 64 65 63 6c 61 72 65 64 20 61 73 20 66 6f  s declared as fo
48245 6c 6c 6f 77 73 2e 20 49 6e 20 65 61 63 68 20 63  llows. In each c
48246 61 73 65 2c 20 74 68 65 20 27 25 27 20 63 68 61  ase, the '%' cha
48247 72 61 63 74 65 72 0d 0a 2a 2a 20 69 6e 20 74 68  racter..** in th
48248 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
48249 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 74 68  replaced with th
4824a 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20  e user-supplied 
4824b 6e 61 6d 65 20 6f 66 20 74 68 65 20 72 2d 74 72  name of the r-tr
4824c 65 65 0d 0a 2a 2a 20 74 61 62 6c 65 2e 0d 0a 2a  ee..** table...*
4824d 2a 0d 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54  *..**   CREATE T
4824e 41 42 4c 45 20 25 5f 6e 6f 64 65 28 6e 6f 64 65  ABLE %_node(node
4824f 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
48250 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c 4f  RY KEY, data BLO
48251 42 29 0d 0a 2a 2a 20 20 20 43 52 45 41 54 45 20  B)..**   CREATE 
48252 54 41 42 4c 45 20 25 5f 70 61 72 65 6e 74 28 6e  TABLE %_parent(n
48253 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52  odeno INTEGER PR
48254 49 4d 41 52 59 20 4b 45 59 2c 20 70 61 72 65 6e  IMARY KEY, paren
48255 74 6e 6f 64 65 20 49 4e 54 45 47 45 52 29 0d 0a  tnode INTEGER)..
48256 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
48257 45 20 25 5f 72 6f 77 69 64 28 72 6f 77 69 64 20  E %_rowid(rowid 
48258 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
48259 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45  KEY, nodeno INTE
4825a 47 45 52 29 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  GER)..**..** The
4825b 20 64 61 74 61 20 66 6f 72 20 65 61 63 68 20 6e   data for each n
4825c 6f 64 65 20 6f 66 20 74 68 65 20 72 2d 74 72 65  ode of the r-tre
4825d 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73  e structure is s
4825e 74 6f 72 65 64 20 69 6e 20 74 68 65 20 25 5f 6e  tored in the %_n
4825f 6f 64 65 0d 0a 2a 2a 20 74 61 62 6c 65 2e 20 46  ode..** table. F
48260 6f 72 20 65 61 63 68 20 6e 6f 64 65 20 74 68 61  or each node tha
48261 74 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f  t is not the roo
48262 74 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 72 2d  t node of the r-
48263 74 72 65 65 2c 20 74 68 65 72 65 20 69 73 0d 0a  tree, there is..
48264 2a 2a 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  ** an entry in t
48265 68 65 20 25 5f 70 61 72 65 6e 74 20 74 61 62 6c  he %_parent tabl
48266 65 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68  e associating th
48267 65 20 6e 6f 64 65 20 77 69 74 68 20 69 74 73 20  e node with its 
48268 70 61 72 65 6e 74 2e 0d 0a 2a 2a 20 41 6e 64 20  parent...** And 
48269 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20  for each row of 
4826a 64 61 74 61 20 69 6e 20 74 68 65 20 74 61 62 6c  data in the tabl
4826b 65 2c 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  e, there is an e
4826c 6e 74 72 79 20 69 6e 20 74 68 65 20 25 5f 72 6f  ntry in the %_ro
4826d 77 69 64 0d 0a 2a 2a 20 74 61 62 6c 65 20 74 68  wid..** table th
4826e 61 74 20 6d 61 70 73 20 66 72 6f 6d 20 74 68 65  at maps from the
4826f 20 65 6e 74 72 69 65 73 20 72 6f 77 69 64 20 74   entries rowid t
48270 6f 20 74 68 65 20 69 64 20 6f 66 20 74 68 65 20  o the id of the 
48271 6e 6f 64 65 20 74 68 61 74 20 69 74 0d 0a 2a 2a  node that it..**
48272 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 2e 0d 0a   is stored on...
48273 2a 2a 0d 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20  **..** The root 
48274 6e 6f 64 65 20 6f 66 20 61 6e 20 72 2d 74 72 65  node of an r-tre
48275 65 20 61 6c 77 61 79 73 20 65 78 69 73 74 73 2c  e always exists,
48276 20 65 76 65 6e 20 69 66 20 74 68 65 20 72 2d 74   even if the r-t
48277 72 65 65 20 74 61 62 6c 65 20 69 73 0d 0a 2a 2a  ree table is..**
48278 20 65 6d 70 74 79 2e 20 54 68 65 20 6e 6f 64 65   empty. The node
48279 6e 6f 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6e  no of the root n
4827a 6f 64 65 20 69 73 20 61 6c 77 61 79 73 20 31 2e  ode is always 1.
4827b 20 41 6c 6c 20 6f 74 68 65 72 20 6e 6f 64 65 73   All other nodes
4827c 20 69 6e 20 74 68 65 0d 0a 2a 2a 20 74 61 62 6c   in the..** tabl
4827d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  e must be the sa
4827e 6d 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 72  me size as the r
4827f 6f 6f 74 20 6e 6f 64 65 2e 20 54 68 65 20 63 6f  oot node. The co
48280 6e 74 65 6e 74 20 6f 66 20 65 61 63 68 20 6e 6f  ntent of each no
48281 64 65 0d 0a 2a 2a 20 69 73 20 66 6f 72 6d 61 74  de..** is format
48282 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0d  ted as follows:.
48283 0a 2a 2a 0d 0a 2a 2a 20 20 20 31 2e 20 49 66 20  .**..**   1. If 
48284 74 68 65 20 6e 6f 64 65 20 69 73 20 74 68 65 20  the node is the 
48285 72 6f 6f 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20  root node (node 
48286 31 29 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72  1), then the fir
48287 73 74 20 32 20 62 79 74 65 73 0d 0a 2a 2a 20 20  st 2 bytes..**  
48288 20 20 20 20 6f 66 20 74 68 65 20 6e 6f 64 65 20      of the node 
48289 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 72 65 65  contain the tree
4828a 20 64 65 70 74 68 20 61 73 20 61 20 62 69 67 2d   depth as a big-
4828b 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0d  endian integer..
4828c 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e 6f 6e  .**      For non
4828d 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74 68 65  -root nodes, the
4828e 20 66 69 72 73 74 20 32 20 62 79 74 65 73 20 61   first 2 bytes a
4828f 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64 2e 0d  re left unused..
48290 0a 2a 2a 0d 0a 2a 2a 20 20 20 32 2e 20 54 68 65  .**..**   2. The
48291 20 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f   next 2 bytes co
48292 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
48293 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72   of entries curr
48294 65 6e 74 6c 79 20 0d 0a 2a 2a 20 20 20 20 20 20  ently ..**      
48295 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f  stored in the no
48296 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 33 2e  de...**..**   3.
48297 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   The remainder o
48298 66 20 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61  f the node conta
48299 69 6e 73 20 74 68 65 20 6e 6f 64 65 20 65 6e 74  ins the node ent
4829a 72 69 65 73 2e 20 45 61 63 68 20 65 6e 74 72 79  ries. Each entry
4829b 0d 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73 69 73  ..**      consis
4829c 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 38  ts of a single 8
4829d 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 66 6f  -byte integer fo
4829e 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 65 76 65  llowed by an eve
4829f 6e 20 6e 75 6d 62 65 72 0d 0a 2a 2a 20 20 20 20  n number..**    
482a0 20 20 6f 66 20 34 2d 62 79 74 65 20 63 6f 6f 72    of 4-byte coor
482a1 64 69 6e 61 74 65 73 2e 20 46 6f 72 20 6c 65 61  dinates. For lea
482a2 66 20 6e 6f 64 65 73 20 74 68 65 20 69 6e 74 65  f nodes the inte
482a3 67 65 72 20 69 73 20 74 68 65 20 72 6f 77 69 64  ger is the rowid
482a4 0d 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 72  ..**      of a r
482a5 65 63 6f 72 64 2e 20 46 6f 72 20 69 6e 74 65 72  ecord. For inter
482a6 6e 61 6c 20 6e 6f 64 65 73 20 69 74 20 69 73 20  nal nodes it is 
482a7 74 68 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  the node number 
482a8 6f 66 20 61 0d 0a 2a 2a 20 20 20 20 20 20 63 68  of a..**      ch
482a9 69 6c 64 20 70 61 67 65 2e 0d 0a 2a 2f 0d 0a 0d  ild page...*/...
482aa 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
482ab 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
482ac 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
482ad 42 4c 45 5f 52 54 52 45 45 29 0d 0a 0d 0a 2f 2a  BLE_RTREE)..../*
482ae 0d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  ..** This file c
482af 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c 65  ontains an imple
482b0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 63  mentation of a c
482b1 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65  ouple of differe
482b2 6e 74 20 76 61 72 69 61 6e 74 73 0d 0a 2a 2a 20  nt variants..** 
482b3 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c  of the r-tree al
482b4 67 6f 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65  gorithm. See the
482b5 20 52 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72   README file for
482b6 20 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73   further details
482b7 2e 20 54 68 65 20 0d 0a 2a 2a 20 73 61 6d 65 20  . The ..** same 
482b8 64 61 74 61 2d 73 74 72 75 63 74 75 72 65 20 69  data-structure i
482b9 73 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20  s used for all, 
482ba 62 75 74 20 74 68 65 20 61 6c 67 6f 72 69 74 68  but the algorith
482bb 6d 73 20 66 6f 72 20 69 6e 73 65 72 74 20 61 6e  ms for insert an
482bc 64 0d 0a 2a 2a 20 64 65 6c 65 74 65 20 6f 70 65  d..** delete ope
482bd 72 61 74 69 6f 6e 73 20 76 61 72 79 2e 20 54 68  rations vary. Th
482be 65 20 76 61 72 69 61 6e 74 73 20 75 73 65 64 20  e variants used 
482bf 61 72 65 20 73 65 6c 65 63 74 65 64 20 61 74 20  are selected at 
482c0 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 0d 0a 2a  compile time ..*
482c1 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20 74 68  * by defining th
482c2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62  e following symb
482c3 6f 6c 73 3a 0d 0a 2a 2f 0d 0a 0d 0a 2f 2a 20 45  ols:..*/..../* E
482c4 69 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e  ither, both or n
482c5 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
482c6 77 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20  wing may be set 
482c7 74 6f 20 61 63 74 69 76 61 74 65 20 0d 0a 2a 2a  to activate ..**
482c8 20 72 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20   r*tree variant 
482c9 61 6c 67 6f 72 69 74 68 6d 73 2e 0d 0a 2a 2f 0d  algorithms...*/.
482ca 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
482cb 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
482cc 45 53 55 42 54 52 45 45 20 30 0d 0a 23 64 65 66  ESUBTREE 0..#def
482cd 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41  ine VARIANT_RSTA
482ce 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 20 20  RTREE_REINSERT  
482cf 20 20 20 20 31 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a      1..../* ..**
482d0 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20   Exactly one of 
482d1 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75  the following mu
482d2 73 74 20 62 65 20 73 65 74 20 74 6f 20 31 2e 0d  st be set to 1..
482d3 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20 56 41 52  .*/..#define VAR
482d4 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
482d5 44 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0d 0a  DRATIC_SPLIT 0..
482d6 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
482d7 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
482d8 50 4c 49 54 20 20 20 20 30 0d 0a 23 64 65 66 69  PLIT    0..#defi
482d9 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  ne VARIANT_RSTAR
482da 54 52 45 45 5f 53 50 4c 49 54 20 20 20 20 20 20  TREE_SPLIT      
482db 20 20 20 31 0d 0a 0d 0a 23 64 65 66 69 6e 65 20     1....#define 
482dc 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
482dd 53 50 4c 49 54 20 5c 0d 0a 20 20 20 20 20 20 20  SPLIT \..       
482de 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41   (VARIANT_GUTTMA
482df 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c 7c  N_LINEAR_SPLIT||
482e0 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
482e1 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 29  QUADRATIC_SPLIT)
482e2 0d 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  ....#if VARIANT_
482e3 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49  GUTTMAN_QUADRATI
482e4 43 5f 53 50 4c 49 54 0d 0a 20 20 23 64 65 66 69  C_SPLIT..  #defi
482e5 6e 65 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64  ne PickNext Quad
482e6 72 61 74 69 63 50 69 63 6b 4e 65 78 74 0d 0a 20  raticPickNext.. 
482e7 20 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65   #define PickSee
482e8 64 73 20 51 75 61 64 72 61 74 69 63 50 69 63 6b  ds QuadraticPick
482e9 53 65 65 64 73 0d 0a 20 20 23 64 65 66 69 6e 65  Seeds..  #define
482ea 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70 6c   AssignCells spl
482eb 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0d 0a 23  itNodeGuttman..#
482ec 65 6e 64 69 66 0d 0a 23 69 66 20 56 41 52 49 41  endif..#if VARIA
482ed 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
482ee 52 5f 53 50 4c 49 54 0d 0a 20 20 23 64 65 66 69  R_SPLIT..  #defi
482ef 6e 65 20 50 69 63 6b 4e 65 78 74 20 4c 69 6e 65  ne PickNext Line
482f0 61 72 50 69 63 6b 4e 65 78 74 0d 0a 20 20 23 64  arPickNext..  #d
482f1 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
482f2 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 0d  LinearPickSeeds.
482f3 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69 67  .  #define Assig
482f4 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65  nCells splitNode
482f5 47 75 74 74 6d 61 6e 0d 0a 23 65 6e 64 69 66 0d  Guttman..#endif.
482f6 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
482f7 41 52 54 52 45 45 5f 53 50 4c 49 54 0d 0a 20 20  ARTREE_SPLIT..  
482f8 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
482f9 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 53 74 61  lls splitNodeSta
482fa 72 74 72 65 65 0d 0a 23 65 6e 64 69 66 0d 0a 0d  rtree..#endif...
482fb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
482fc 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65  EBUG) && !define
482fd 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
482fe 0d 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55  ..# define NDEBU
482ff 47 20 31 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23  G 1..#endif....#
48300 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
48301 52 45 0d 0a 20 20 53 51 4c 49 54 45 5f 45 58 54  RE..  SQLITE_EXT
48302 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0d 0a 23 65  ENSION_INIT1..#e
48303 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f  lse..#endif..../
48304 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69  * #include <stri
48305 6e 67 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20 23 69 6e  ng.h> */../* #in
48306 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
48307 20 2a 2f 0d 0a 0d 0a 23 69 66 6e 64 65 66 20 53   */....#ifndef S
48308 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
48309 4f 4e 0d 0a 23 69 6e 63 6c 75 64 65 20 22 73 71  ON..#include "sq
4830a 6c 69 74 65 33 72 74 72 65 65 2e 68 22 0d 0a 74  lite3rtree.h"..t
4830b 79 70 65 64 65 66 20 73 71 6c 69 74 65 33 5f 69  ypedef sqlite3_i
4830c 6e 74 36 34 20 69 36 34 3b 0d 0a 74 79 70 65 64  nt64 i64;..typed
4830d 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ef unsigned char
4830e 20 75 38 3b 0d 0a 74 79 70 65 64 65 66 20 75 6e   u8;..typedef un
4830f 73 69 67 6e 65 64 20 69 6e 74 20 75 33 32 3b 0d  signed int u32;.
48310 0a 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 20 20 54  .#endif..../*  T
48311 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
48312 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 73 75  ro is used to su
48313 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 72 20  ppress compiler 
48314 77 61 72 6e 69 6e 67 73 2e 0d 0a 2a 2f 0d 0a 23  warnings...*/..#
48315 69 66 6e 64 65 66 20 55 4e 55 53 45 44 5f 50 41  ifndef UNUSED_PA
48316 52 41 4d 45 54 45 52 0d 0a 23 20 64 65 66 69 6e  RAMETER..# defin
48317 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  e UNUSED_PARAMET
48318 45 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0d  ER(x) (void)(x).
48319 0a 23 65 6e 64 69 66 0d 0a 0d 0a 74 79 70 65 64  .#endif....typed
4831a 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65 20  ef struct Rtree 
4831b 52 74 72 65 65 3b 0d 0a 74 79 70 65 64 65 66 20  Rtree;..typedef 
4831c 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73  struct RtreeCurs
4831d 6f 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b 0d  or RtreeCursor;.
4831e 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4831f 52 74 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e  RtreeNode RtreeN
48320 6f 64 65 3b 0d 0a 74 79 70 65 64 65 66 20 73 74  ode;..typedef st
48321 72 75 63 74 20 52 74 72 65 65 43 65 6c 6c 20 52  ruct RtreeCell R
48322 74 72 65 65 43 65 6c 6c 3b 0d 0a 74 79 70 65 64  treeCell;..typed
48323 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43  ef struct RtreeC
48324 6f 6e 73 74 72 61 69 6e 74 20 52 74 72 65 65 43  onstraint RtreeC
48325 6f 6e 73 74 72 61 69 6e 74 3b 0d 0a 74 79 70 65  onstraint;..type
48326 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
48327 4d 61 74 63 68 41 72 67 20 52 74 72 65 65 4d 61  MatchArg RtreeMa
48328 74 63 68 41 72 67 3b 0d 0a 74 79 70 65 64 65 66  tchArg;..typedef
48329 20 73 74 72 75 63 74 20 52 74 72 65 65 47 65 6f   struct RtreeGeo
4832a 6d 43 61 6c 6c 62 61 63 6b 20 52 74 72 65 65 47  mCallback RtreeG
4832b 65 6f 6d 43 61 6c 6c 62 61 63 6b 3b 0d 0a 74 79  eomCallback;..ty
4832c 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65  pedef union Rtre
4832d 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72  eCoord RtreeCoor
4832e 64 3b 0d 0a 0d 0a 2f 2a 20 54 68 65 20 72 74 72  d;..../* The rtr
4832f 65 65 20 6d 61 79 20 68 61 76 65 20 62 65 74 77  ee may have betw
48330 65 65 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f  een 1 and RTREE_
48331 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64  MAX_DIMENSIONS d
48332 69 6d 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0d 0a 23  imensions. */..#
48333 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58  define RTREE_MAX
48334 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 35 0d 0a 0d  _DIMENSIONS 5...
48335 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 68 61 73 68  ./* Size of hash
48336 20 74 61 62 6c 65 20 52 74 72 65 65 2e 61 48 61   table Rtree.aHa
48337 73 68 2e 20 54 68 69 73 20 68 61 73 68 20 74 61  sh. This hash ta
48338 62 6c 65 20 69 73 20 6e 6f 74 20 65 78 70 65 63  ble is not expec
48339 74 65 64 20 74 6f 0d 0a 2a 2a 20 65 76 65 72 20  ted to..** ever 
4833a 63 6f 6e 74 61 69 6e 20 76 65 72 79 20 6d 61 6e  contain very man
4833b 79 20 65 6e 74 72 69 65 73 2c 20 73 6f 20 61 20  y entries, so a 
4833c 66 69 78 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  fixed number of 
4833d 62 75 63 6b 65 74 73 20 69 73 20 0d 0a 2a 2a 20  buckets is ..** 
4833e 75 73 65 64 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69  used...*/..#defi
4833f 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32 38 0d  ne HASHSIZE 128.
48340 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 41 6e 20 72 74  .../* ..** An rt
48341 72 65 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  ree virtual-tabl
48342 65 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73  e object...*/..s
48343 74 72 75 63 74 20 52 74 72 65 65 20 7b 0d 0a 20  truct Rtree {.. 
48344 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62 61   sqlite3_vtab ba
48345 73 65 3b 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  se;..  sqlite3 *
48346 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
48347 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62     /* Host datab
48348 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
48349 2f 0d 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69  /..  int iNodeSi
4834a 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
4834b 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65   /* Size in byte
4834c 73 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69  s of each node i
4834d 6e 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65  n the node table
4834e 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 44 69 6d 3b   */..  int nDim;
4834f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48350 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
48351 64 69 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0d 0a 20  dimensions */.. 
48352 20 69 6e 74 20 6e 42 79 74 65 73 50 65 72 43 65   int nBytesPerCe
48353 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll;          /* 
48354 42 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 70  Bytes consumed p
48355 65 72 20 63 65 6c 6c 20 2a 2f 0d 0a 20 20 69 6e  er cell */..  in
48356 74 20 69 44 65 70 74 68 3b 20 20 20 20 20 20 20  t iDepth;       
48357 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
48358 72 65 6e 74 20 64 65 70 74 68 20 6f 66 20 74 68  rent depth of th
48359 65 20 72 2d 74 72 65 65 20 73 74 72 75 63 74 75  e r-tree structu
4835a 72 65 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a  re */..  char *z
4835b 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
4835c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
4835d 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
4835e 69 6e 67 20 72 2d 74 72 65 65 20 74 61 62 6c 65  ing r-tree table
4835f 20 2a 2f 0d 0a 20 20 63 68 61 72 20 2a 7a 4e 61   */..  char *zNa
48360 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
48361 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 72 2d     /* Name of r-
48362 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 20 0d 0a  tree table */ ..
48363 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 61 48 61    RtreeNode *aHa
48364 73 68 5b 48 41 53 48 53 49 5a 45 5d 3b 20 2f 2a  sh[HASHSIZE]; /*
48365 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 69   Hash table of i
48366 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 73 2e 20  n-memory nodes. 
48367 2a 2f 20 0d 0a 20 20 69 6e 74 20 6e 42 75 73 79  */ ..  int nBusy
48368 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
48369 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
4836a 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66  mber of users of
4836b 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
4836c 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 4c 69 73 74 20  */....  /* List 
4836d 6f 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64  of nodes removed
4836e 20 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65 6e   during a Conden
4836f 73 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f 6e  seTree operation
48370 2e 20 4c 69 73 74 20 69 73 0d 0a 20 20 2a 2a 20  . List is..  ** 
48371 6c 69 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20  linked together 
48372 76 69 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20  via the pointer 
48373 6e 6f 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f  normally used fo
48374 72 20 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0d  r hash chains -.
48375 0a 20 20 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e  .  ** RtreeNode.
48376 70 4e 65 78 74 2e 20 52 74 72 65 65 4e 6f 64 65  pNext. RtreeNode
48377 2e 69 4e 6f 64 65 20 73 74 6f 72 65 73 20 74 68  .iNode stores th
48378 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 73  e depth of the s
48379 75 62 2d 74 72 65 65 20 0d 0a 20 20 2a 2a 20 68  ub-tree ..  ** h
4837a 65 61 64 65 64 20 62 79 20 74 68 65 20 6e 6f 64  eaded by the nod
4837b 65 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61  e (leaf nodes ha
4837c 76 65 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f  ve RtreeNode.iNo
4837d 64 65 3d 3d 30 29 2e 0d 0a 20 20 2a 2f 0d 0a 20  de==0)...  */.. 
4837e 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65 6c   RtreeNode *pDel
4837f 65 74 65 64 3b 0d 0a 20 20 69 6e 74 20 69 52 65  eted;..  int iRe
48380 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20  insertHeight;   
48381 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f       /* Height o
48382 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e  f sub-trees Rein
48383 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f  sert() has run o
48384 6e 20 2a 2f 0d 0a 0d 0a 20 20 2f 2a 20 53 74 61  n */....  /* Sta
48385 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f  tements to read/
48386 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72  write/delete a r
48387 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e  ecord from xxx_n
48388 6f 64 65 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65  ode */..  sqlite
48389 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64  3_stmt *pReadNod
4838a 65 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  e;..  sqlite3_st
4838b 6d 74 20 2a 70 57 72 69 74 65 4e 6f 64 65 3b 0d  mt *pWriteNode;.
4838c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
4838d 2a 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0d 0a 0d  *pDeleteNode;...
4838e 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73  .  /* Statements
4838f 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64   to read/write/d
48390 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66  elete a record f
48391 72 6f 6d 20 78 78 78 5f 72 6f 77 69 64 20 2a 2f  rom xxx_rowid */
48392 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ..  sqlite3_stmt
48393 20 2a 70 52 65 61 64 52 6f 77 69 64 3b 0d 0a 20   *pReadRowid;.. 
48394 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
48395 57 72 69 74 65 52 6f 77 69 64 3b 0d 0a 20 20 73  WriteRowid;..  s
48396 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65  qlite3_stmt *pDe
48397 6c 65 74 65 52 6f 77 69 64 3b 0d 0a 0d 0a 20 20  leteRowid;....  
48398 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f  /* Statements to
48399 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65   read/write/dele
4839a 74 65 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d  te a record from
4839b 20 78 78 78 5f 70 61 72 65 6e 74 20 2a 2f 0d 0a   xxx_parent */..
4839c 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
4839d 70 52 65 61 64 50 61 72 65 6e 74 3b 0d 0a 20 20  pReadParent;..  
4839e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 57  sqlite3_stmt *pW
4839f 72 69 74 65 50 61 72 65 6e 74 3b 0d 0a 20 20 73  riteParent;..  s
483a0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65  qlite3_stmt *pDe
483a1 6c 65 74 65 50 61 72 65 6e 74 3b 0d 0a 0d 0a 20  leteParent;.... 
483a2 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b   int eCoordType;
483a3 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20 50 6f 73 73 69  ..};..../* Possi
483a4 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 65  ble values for e
483a5 43 6f 6f 72 64 54 79 70 65 3a 20 2a 2f 0d 0a 23  CoordType: */..#
483a6 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f  define RTREE_COO
483a7 52 44 5f 52 45 41 4c 33 32 20 30 0d 0a 23 64 65  RD_REAL32 0..#de
483a8 66 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44  fine RTREE_COORD
483a9 5f 49 4e 54 33 32 20 20 31 0d 0a 0d 0a 2f 2a 0d  _INT32  1..../*.
483aa 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20  .** The minimum 
483ab 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
483ac 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20 6e 6f  allowed for a no
483ad 64 65 20 69 73 20 61 20 74 68 69 72 64 20 6f 66  de is a third of
483ae 20 74 68 65 20 0d 0a 2a 2a 20 6d 61 78 69 6d 75   the ..** maximu
483af 6d 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20 6e  m. In Gutman's n
483b0 6f 74 61 74 69 6f 6e 3a 0d 0a 2a 2a 0d 0a 2a 2a  otation:..**..**
483b1 20 20 20 20 20 6d 20 3d 20 4d 2f 33 0d 0a 2a 2a       m = M/3..**
483b2 0d 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d 74 72  ..** If an R*-tr
483b3 65 65 20 22 52 65 69 6e 73 65 72 74 22 20 6f 70  ee "Reinsert" op
483b4 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69  eration is requi
483b5 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20 6e 75  red, the same nu
483b6 6d 62 65 72 20 6f 66 0d 0a 2a 2a 20 63 65 6c 6c  mber of..** cell
483b7 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 72  s are removed fr
483b8 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c 20  om the overfull 
483b9 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65 72  node and reinser
483ba 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65  ted into the tre
483bb 65 2e 0d 0a 2a 2f 0d 0a 23 64 65 66 69 6e 65 20  e...*/..#define 
483bc 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
483bd 29 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53  ) ((((p)->iNodeS
483be 69 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74  ize-4)/(p)->nByt
483bf 65 73 50 65 72 43 65 6c 6c 29 2f 33 29 0d 0a 23  esPerCell)/3)..#
483c0 64 65 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49  define RTREE_REI
483c1 4e 53 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d  NSERT(p) RTREE_M
483c2 49 4e 43 45 4c 4c 53 28 70 29 0d 0a 23 64 65 66  INCELLS(p)..#def
483c3 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 43 45 4c  ine RTREE_MAXCEL
483c4 4c 53 20 35 31 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  LS 51..../*..** 
483c5 54 68 65 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73  The smallest pos
483c6 73 69 62 6c 65 20 6e 6f 64 65 2d 73 69 7a 65 20  sible node-size 
483c7 69 73 20 28 35 31 32 2d 36 34 29 3d 3d 34 34 38  is (512-64)==448
483c8 20 62 79 74 65 73 2e 20 41 6e 64 20 74 68 65 20   bytes. And the 
483c9 6c 61 72 67 65 73 74 0d 0a 2a 2a 20 73 75 70 70  largest..** supp
483ca 6f 72 74 65 64 20 63 65 6c 6c 20 73 69 7a 65 20  orted cell size 
483cb 69 73 20 34 38 20 62 79 74 65 73 20 28 38 20 62  is 48 bytes (8 b
483cc 79 74 65 20 72 6f 77 69 64 20 2b 20 74 65 6e 20  yte rowid + ten 
483cd 34 20 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74  4 byte coordinat
483ce 65 73 29 2e 0d 0a 2a 2a 20 54 68 65 72 65 66 6f  es)...** Therefo
483cf 72 65 20 61 6c 6c 20 6e 6f 6e 2d 72 6f 6f 74 20  re all non-root 
483d0 6e 6f 64 65 73 20 6d 75 73 74 20 63 6f 6e 74 61  nodes must conta
483d1 69 6e 20 61 74 20 6c 65 61 73 74 20 33 20 65 6e  in at least 3 en
483d2 74 72 69 65 73 2e 20 53 69 6e 63 65 20 0d 0a 2a  tries. Since ..*
483d3 2a 20 32 5e 34 30 20 69 73 20 67 72 65 61 74 65  * 2^40 is greate
483d4 72 20 74 68 61 6e 20 32 5e 36 34 2c 20 61 6e 20  r than 2^64, an 
483d5 72 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  r-tree structure
483d6 20 61 6c 77 61 79 73 20 68 61 73 20 61 20 64 65   always has a de
483d7 70 74 68 20 6f 66 0d 0a 2a 2a 20 34 30 20 6f 72  pth of..** 40 or
483d8 20 6c 65 73 73 2e 0d 0a 2a 2f 0d 0a 23 64 65 66   less...*/..#def
483d9 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 45  ine RTREE_MAX_DE
483da 50 54 48 20 34 30 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  PTH 40..../* ..*
483db 2a 20 41 6e 20 72 74 72 65 65 20 63 75 72 73 6f  * An rtree curso
483dc 72 20 6f 62 6a 65 63 74 2e 0d 0a 2a 2f 0d 0a 73  r object...*/..s
483dd 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73 6f  truct RtreeCurso
483de 72 20 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76  r {..  sqlite3_v
483df 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  tab_cursor base;
483e0 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ..  RtreeNode *p
483e1 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Node;           
483e2 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75        /* Node cu
483e3 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c  rsor is currentl
483e4 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  y pointing at */
483e5 0d 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20  ..  int iCell;  
483e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483e7 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
483e8 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69  f current cell i
483e9 6e 20 70 4e 6f 64 65 20 2a 2f 0d 0a 20 20 69 6e  n pNode */..  in
483ea 74 20 69 53 74 72 61 74 65 67 79 3b 20 20 20 20  t iStrategy;    
483eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483ec 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64 78 4e 75  /* Copy of idxNu
483ed 6d 20 73 65 61 72 63 68 20 70 61 72 61 6d 65 74  m search paramet
483ee 65 72 20 2a 2f 0d 0a 20 20 69 6e 74 20 6e 43 6f  er */..  int nCo
483ef 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20  nstraint;       
483f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
483f1 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
483f2 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a  in aConstraint *
483f3 2f 0d 0a 20 20 52 74 72 65 65 43 6f 6e 73 74 72  /..  RtreeConstr
483f4 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e  aint *aConstrain
483f5 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68  t;     /* Search
483f6 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a 2f   constraints. */
483f7 0d 0a 7d 3b 0d 0a 0d 0a 75 6e 69 6f 6e 20 52 74  ..};....union Rt
483f8 72 65 65 43 6f 6f 72 64 20 7b 0d 0a 20 20 66 6c  reeCoord {..  fl
483f9 6f 61 74 20 66 3b 0d 0a 20 20 69 6e 74 20 69 3b  oat f;..  int i;
483fa 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ..};..../*..** T
483fb 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  he argument is a
483fc 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e 20 52 65  n RtreeCoord. Re
483fd 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 73  turn the value s
483fe 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68 65  tored within the
483ff 20 52 74 72 65 65 43 6f 6f 72 64 0d 0a 2a 2a 20   RtreeCoord..** 
48400 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61 20 64  formatted as a d
48401 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61 63 72  ouble. This macr
48402 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 6c  o assumes that l
48403 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 70 52  ocal variable pR
48404 74 72 65 65 20 70 6f 69 6e 74 73 0d 0a 2a 2a 20  tree points..** 
48405 74 6f 20 74 68 65 20 52 74 72 65 65 20 73 74 72  to the Rtree str
48406 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
48407 64 20 77 69 74 68 20 74 68 65 20 52 74 72 65 65  d with the Rtree
48408 43 6f 6f 72 64 2e 0d 0a 2a 2f 0d 0a 23 64 65 66  Coord...*/..#def
48409 69 6e 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64  ine DCOORD(coord
4840a 29 20 28 20 20 20 20 20 20 20 20 20 20 20 20 20  ) (             
4840b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d                \.
4840c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f  .  (pRtree->eCoo
4840d 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
4840e 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f 20 20 20  ORD_REAL32) ?   
4840f 20 20 20 5c 0d 0a 20 20 20 20 28 28 64 6f 75 62     \..    ((doub
48410 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20 20 20  le)coord.f) :   
48411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48412 20 20 20 20 20 20 20 20 5c 0d 0a 20 20 20 20 28          \..    (
48413 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69 29  (double)coord.i)
48414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48415 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0d 0a               \..
48416 29 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20 73 65  )..../*..** A se
48417 61 72 63 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e  arch constraint.
48418 0d 0a 2a 2f 0d 0a 73 74 72 75 63 74 20 52 74 72  ..*/..struct Rtr
48419 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 7b 0d 0a  eeConstraint {..
4841a 20 20 69 6e 74 20 69 43 6f 6f 72 64 3b 20 20 20    int iCoord;   
4841b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4841c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
4841d 6e 73 74 72 61 69 6e 65 64 20 63 6f 6f 72 64 69  nstrained coordi
4841e 6e 61 74 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 6f  nate */..  int o
4841f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
48420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
48421 6e 73 74 72 61 69 6e 69 6e 67 20 6f 70 65 72 61  nstraining opera
48422 74 69 6f 6e 20 2a 2f 0d 0a 20 20 64 6f 75 62 6c  tion */..  doubl
48423 65 20 72 56 61 6c 75 65 3b 20 20 20 20 20 20 20  e rValue;       
48424 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
48425 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20  nstraint value. 
48426 2a 2f 0d 0a 20 20 69 6e 74 20 28 2a 78 47 65 6f  */..  int (*xGeo
48427 6d 29 28 73 71 6c 69 74 65 33 5f 72 74 72 65 65  m)(sqlite3_rtree
48428 5f 67 65 6f 6d 65 74 72 79 20 2a 2c 20 69 6e 74  _geometry *, int
48429 2c 20 64 6f 75 62 6c 65 20 2a 2c 20 69 6e 74 20  , double *, int 
4842a 2a 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 72  *);..  sqlite3_r
4842b 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 20 2a 70  tree_geometry *p
4842c 47 65 6f 6d 3b 20 20 2f 2a 20 43 6f 6e 73 74 72  Geom;  /* Constr
4842d 61 69 6e 74 20 63 61 6c 6c 62 61 63 6b 20 61 72  aint callback ar
4842e 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 4d 41 54  gument for a MAT
4842f 43 48 20 2a 2f 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 20  CH */..};..../* 
48430 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
48431 66 6f 72 20 52 74 72 65 65 43 6f 6e 73 74 72 61  for RtreeConstra
48432 69 6e 74 2e 6f 70 20 2a 2f 0d 0a 23 64 65 66 69  int.op */..#defi
48433 6e 65 20 52 54 52 45 45 5f 45 51 20 20 20 20 30  ne RTREE_EQ    0
48434 78 34 31 0d 0a 23 64 65 66 69 6e 65 20 52 54 52  x41..#define RTR
48435 45 45 5f 4c 45 20 20 20 20 30 78 34 32 0d 0a 23  EE_LE    0x42..#
48436 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20  define RTREE_LT 
48437 20 20 20 30 78 34 33 0d 0a 23 64 65 66 69 6e 65     0x43..#define
48438 20 52 54 52 45 45 5f 47 45 20 20 20 20 30 78 34   RTREE_GE    0x4
48439 34 0d 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  4..#define RTREE
4843a 5f 47 54 20 20 20 20 30 78 34 35 0d 0a 23 64 65  _GT    0x45..#de
4843b 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 54 43 48  fine RTREE_MATCH
4843c 20 30 78 34 36 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a   0x46..../* ..**
4843d 20 41 6e 20 72 74 72 65 65 20 73 74 72 75 63 74   An rtree struct
4843e 75 72 65 20 6e 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73  ure node...*/..s
4843f 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20  truct RtreeNode 
48440 7b 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  {..  RtreeNode *
48441 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20  pParent;        
48442 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
48443 20 6e 6f 64 65 20 2a 2f 0d 0a 20 20 69 36 34 20   node */..  i64 
48444 69 4e 6f 64 65 3b 0d 0a 20 20 69 6e 74 20 6e 52  iNode;..  int nR
48445 65 66 3b 0d 0a 20 20 69 6e 74 20 69 73 44 69 72  ef;..  int isDir
48446 74 79 3b 0d 0a 20 20 75 38 20 2a 7a 44 61 74 61  ty;..  u8 *zData
48447 3b 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ;..  RtreeNode *
48448 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
48449 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6e         /* Next n
4844a 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73 68  ode in this hash
4844b 20 63 68 61 69 6e 20 2a 2f 0d 0a 7d 3b 0d 0a 23   chain */..};..#
4844c 64 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f  define NCELL(pNo
4844d 64 65 29 20 72 65 61 64 49 6e 74 31 36 28 26 28  de) readInt16(&(
4844e 70 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d  pNode)->zData[2]
4844f 29 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 53 74 72  )..../* ..** Str
48450 75 63 74 75 72 65 20 74 6f 20 73 74 6f 72 65 20  ucture to store 
48451 61 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20 72  a deserialized r
48452 74 72 65 65 20 72 65 63 6f 72 64 2e 0d 0a 2a 2f  tree record...*/
48453 0d 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 65  ..struct RtreeCe
48454 6c 6c 20 7b 0d 0a 20 20 69 36 34 20 69 52 6f 77  ll {..  i64 iRow
48455 69 64 3b 0d 0a 20 20 52 74 72 65 65 43 6f 6f 72  id;..  RtreeCoor
48456 64 20 61 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d  d aCoord[RTREE_M
48457 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 5d  AX_DIMENSIONS*2]
48458 3b 0d 0a 7d 3b 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a  ;..};....../*..*
48459 2a 20 56 61 6c 75 65 20 66 6f 72 20 74 68 65 20  * Value for the 
4845a 66 69 72 73 74 20 66 69 65 6c 64 20 6f 66 20 65  first field of e
4845b 76 65 72 79 20 52 74 72 65 65 4d 61 74 63 68 41  very RtreeMatchA
4845c 72 67 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 4d  rg object. The M
4845d 41 54 43 48 0d 0a 2a 2a 20 6f 70 65 72 61 74 6f  ATCH..** operato
4845e 72 20 74 65 73 74 73 20 74 68 61 74 20 74 68 65  r tests that the
4845f 20 66 69 72 73 74 20 66 69 65 6c 64 20 6f 66 20   first field of 
48460 61 20 62 6c 6f 62 20 6f 70 65 72 61 6e 64 20 6d  a blob operand m
48461 61 74 63 68 65 73 20 74 68 69 73 0d 0a 2a 2a 20  atches this..** 
48462 76 61 6c 75 65 20 74 6f 20 61 76 6f 69 64 20 6f  value to avoid o
48463 70 65 72 61 74 69 6e 67 20 6f 6e 20 69 6e 76 61  perating on inva
48464 6c 69 64 20 62 6c 6f 62 73 20 28 77 68 69 63 68  lid blobs (which
48465 20 63 6f 75 6c 64 20 63 61 75 73 65 20 61 20 73   could cause a s
48466 65 67 66 61 75 6c 74 29 2e 0d 0a 2a 2f 0d 0a 23  egfault)...*/..#
48467 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 45 4f  define RTREE_GEO
48468 4d 45 54 52 59 5f 4d 41 47 49 43 20 30 78 38 39  METRY_MAGIC 0x89
48469 31 32 34 35 41 42 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  1245AB..../*..**
4846a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
4846b 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6d  this structure m
4846c 75 73 74 20 62 65 20 73 75 70 70 6c 69 65 64 20  ust be supplied 
4846d 61 73 20 61 20 62 6c 6f 62 20 61 72 67 75 6d 65  as a blob argume
4846e 6e 74 20 74 6f 0d 0a 2a 2a 20 74 68 65 20 72 69  nt to..** the ri
4846f 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
48470 20 61 6e 20 53 51 4c 20 4d 41 54 43 48 20 6f 70   an SQL MATCH op
48471 65 72 61 74 6f 72 20 75 73 65 64 20 74 6f 20 63  erator used to c
48472 6f 6e 73 74 72 61 69 6e 20 61 6e 0d 0a 2a 2a 20  onstrain an..** 
48473 72 2d 74 72 65 65 20 71 75 65 72 79 2e 0d 0a 2a  r-tree query...*
48474 2f 0d 0a 73 74 72 75 63 74 20 52 74 72 65 65 4d  /..struct RtreeM
48475 61 74 63 68 41 72 67 20 7b 0d 0a 20 20 75 33 32  atchArg {..  u32
48476 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20   magic;         
48477 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
48478 41 6c 77 61 79 73 20 52 54 52 45 45 5f 47 45 4f  Always RTREE_GEO
48479 4d 45 54 52 59 5f 4d 41 47 49 43 20 2a 2f 0d 0a  METRY_MAGIC */..
4847a 20 20 69 6e 74 20 28 2a 78 47 65 6f 6d 29 28 73    int (*xGeom)(s
4847b 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f  qlite3_rtree_geo
4847c 6d 65 74 72 79 20 2a 2c 20 69 6e 74 2c 20 64 6f  metry *, int, do
4847d 75 62 6c 65 20 2a 2c 20 69 6e 74 20 2a 29 3b 0d  uble *, int *);.
4847e 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78  .  void *pContex
4847f 74 3b 0d 0a 20 20 69 6e 74 20 6e 50 61 72 61 6d  t;..  int nParam
48480 3b 0d 0a 20 20 64 6f 75 62 6c 65 20 61 50 61 72  ;..  double aPar
48481 61 6d 5b 31 5d 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a  am[1];..};..../*
48482 0d 0a 2a 2a 20 57 68 65 6e 20 61 20 67 65 6f 6d  ..** When a geom
48483 65 74 72 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  etry callback is
48484 20 63 72 65 61 74 65 64 20 28 73 65 65 20 73 71   created (see sq
48485 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d  lite3_rtree_geom
48486 65 74 72 79 5f 63 61 6c 6c 62 61 63 6b 29 2c 0d  etry_callback),.
48487 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 6e 73  .** a single ins
48488 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
48489 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
4848a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20 49   is allocated. I
4848b 74 20 69 73 20 75 73 65 64 0d 0a 2a 2a 20 61 73  t is used..** as
4848c 20 74 68 65 20 63 6f 6e 74 65 78 74 20 66 6f 72   the context for
4848d 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
4848e 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 62 79  on created by by
4848f 20 73 5f 72 5f 67 5f 63 28 29 2e 20 54 68 65 20   s_r_g_c(). The 
48490 6f 62 6a 65 63 74 0d 0a 2a 2a 20 69 73 20 65 76  object..** is ev
48491 65 6e 74 75 61 6c 6c 79 20 64 65 6c 65 74 65 64  entually deleted
48492 20 62 79 20 74 68 65 20 64 65 73 74 72 75 63 74   by the destruct
48493 6f 72 20 6d 65 63 68 61 6e 69 73 6d 20 70 72 6f  or mechanism pro
48494 76 69 64 65 64 20 62 79 0d 0a 2a 2a 20 73 71 6c  vided by..** sql
48495 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
48496 74 69 6f 6e 5f 76 32 28 29 20 28 77 68 69 63 68  tion_v2() (which
48497 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 5f   is called by s_
48498 72 5f 67 5f 63 28 29 20 74 6f 20 63 72 65 61 74  r_g_c() to creat
48499 65 0d 0a 2a 2a 20 74 68 65 20 67 65 6f 6d 65 74  e..** the geomet
4849a 72 79 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  ry callback func
4849b 74 69 6f 6e 29 2e 0d 0a 2a 2f 0d 0a 73 74 72 75  tion)...*/..stru
4849c 63 74 20 52 74 72 65 65 47 65 6f 6d 43 61 6c 6c  ct RtreeGeomCall
4849d 62 61 63 6b 20 7b 0d 0a 20 20 69 6e 74 20 28 2a  back {..  int (*
4849e 78 47 65 6f 6d 29 28 73 71 6c 69 74 65 33 5f 72  xGeom)(sqlite3_r
4849f 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 20 2a 2c  tree_geometry *,
484a0 20 69 6e 74 2c 20 64 6f 75 62 6c 65 20 2a 2c 20   int, double *, 
484a1 69 6e 74 20 2a 29 3b 0d 0a 20 20 76 6f 69 64 20  int *);..  void 
484a2 2a 70 43 6f 6e 74 65 78 74 3b 0d 0a 7d 3b 0d 0a  *pContext;..};..
484a3 0d 0a 23 69 66 6e 64 65 66 20 4d 41 58 0d 0a 23  ..#ifndef MAX..#
484a4 20 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79 29   define MAX(x,y)
484a5 20 28 28 78 29 20 3c 20 28 79 29 20 3f 20 28 79   ((x) < (y) ? (y
484a6 29 20 3a 20 28 78 29 29 0d 0a 23 65 6e 64 69 66  ) : (x))..#endif
484a7 0d 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0d 0a 23  ..#ifndef MIN..#
484a8 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29   define MIN(x,y)
484a9 20 28 28 78 29 20 3e 20 28 79 29 20 3f 20 28 79   ((x) > (y) ? (y
484aa 29 20 3a 20 28 78 29 29 0d 0a 23 65 6e 64 69 66  ) : (x))..#endif
484ab 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 75 6e 63 74  ..../*..** Funct
484ac 69 6f 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c  ions to deserial
484ad 69 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74  ize a 16 bit int
484ae 65 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61  eger, 32 bit rea
484af 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0d 0a 2a 2a  l number and..**
484b0 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e   64 bit integer.
484b1 20 54 68 65 20 64 65 73 65 72 69 61 6c 69 7a 65   The deserialize
484b2 64 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72  d value is retur
484b3 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ned...*/..static
484b4 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28 75   int readInt16(u
484b5 38 20 2a 70 29 7b 0d 0a 20 20 72 65 74 75 72 6e  8 *p){..  return
484b6 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
484b7 5d 3b 0d 0a 7d 0d 0a 73 74 61 74 69 63 20 76 6f  ];..}..static vo
484b8 69 64 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20  id readCoord(u8 
484b9 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a  *p, RtreeCoord *
484ba 70 43 6f 6f 72 64 29 7b 0d 0a 20 20 75 33 32 20  pCoord){..  u32 
484bb 69 20 3d 20 28 0d 0a 20 20 20 20 28 28 28 75 33  i = (..    (((u3
484bc 32 29 70 5b 30 5d 29 20 3c 3c 20 32 34 29 20 2b  2)p[0]) << 24) +
484bd 20 0d 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   ..    (((u32)p[
484be 31 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0d 0a 20  1]) << 16) + .. 
484bf 20 20 20 28 28 28 75 33 32 29 70 5b 32 5d 29 20     (((u32)p[2]) 
484c0 3c 3c 20 20 38 29 20 2b 20 0d 0a 20 20 20 20 28  <<  8) + ..    (
484c1 28 28 75 33 32 29 70 5b 33 5d 29 20 3c 3c 20 20  ((u32)p[3]) <<  
484c2 30 29 0d 0a 20 20 29 3b 0d 0a 20 20 2a 28 75 33  0)..  );..  *(u3
484c3 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69 3b 0d  2 *)pCoord = i;.
484c4 0a 7d 0d 0a 73 74 61 74 69 63 20 69 36 34 20 72  .}..static i64 r
484c5 65 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b  eadInt64(u8 *p){
484c6 0d 0a 20 20 72 65 74 75 72 6e 20 28 0d 0a 20 20  ..  return (..  
484c7 20 20 28 28 28 69 36 34 29 70 5b 30 5d 29 20 3c    (((i64)p[0]) <
484c8 3c 20 35 36 29 20 2b 20 0d 0a 20 20 20 20 28 28  < 56) + ..    ((
484c9 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20 34 38  (i64)p[1]) << 48
484ca 29 20 2b 20 0d 0a 20 20 20 20 28 28 28 69 36 34  ) + ..    (((i64
484cb 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b 20  )p[2]) << 40) + 
484cc 0d 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33  ..    (((i64)p[3
484cd 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0d 0a 20 20  ]) << 32) + ..  
484ce 20 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c    (((i64)p[4]) <
484cf 3c 20 32 34 29 20 2b 20 0d 0a 20 20 20 20 28 28  < 24) + ..    ((
484d0 28 69 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36  (i64)p[5]) << 16
484d1 29 20 2b 20 0d 0a 20 20 20 20 28 28 28 69 36 34  ) + ..    (((i64
484d2 29 70 5b 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20  )p[6]) <<  8) + 
484d3 0d 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 37  ..    (((i64)p[7
484d4 5d 29 20 3c 3c 20 20 30 29 0d 0a 20 20 29 3b 0d  ]) <<  0)..  );.
484d5 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46 75 6e  .}..../*..** Fun
484d6 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69 61 6c  ctions to serial
484d7 69 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74  ize a 16 bit int
484d8 65 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61  eger, 32 bit rea
484d9 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0d 0a 2a 2a  l number and..**
484da 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e   64 bit integer.
484db 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
484dc 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ned is the numbe
484dd 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
484de 65 6e 0d 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72  en..** to the ar
484df 67 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61  gument buffer (a
484e0 6c 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38  lways 2, 4 and 8
484e1 20 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0d   respectively)..
484e2 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
484e3 77 72 69 74 65 49 6e 74 31 36 28 75 38 20 2a 70  writeInt16(u8 *p
484e4 2c 20 69 6e 74 20 69 29 7b 0d 0a 20 20 70 5b 30  , int i){..  p[0
484e5 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78 46 46  ] = (i>> 8)&0xFF
484e6 3b 0d 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e  ;..  p[1] = (i>>
484e7 20 30 29 26 30 78 46 46 3b 0d 0a 20 20 72 65 74   0)&0xFF;..  ret
484e8 75 72 6e 20 32 3b 0d 0a 7d 0d 0a 73 74 61 74 69  urn 2;..}..stati
484e9 63 20 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64  c int writeCoord
484ea 28 75 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f  (u8 *p, RtreeCoo
484eb 72 64 20 2a 70 43 6f 6f 72 64 29 7b 0d 0a 20 20  rd *pCoord){..  
484ec 75 33 32 20 69 3b 0d 0a 20 20 61 73 73 65 72 74  u32 i;..  assert
484ed 28 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 6f  ( sizeof(RtreeCo
484ee 6f 72 64 29 3d 3d 34 20 29 3b 0d 0a 20 20 61 73  ord)==4 );..  as
484ef 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32  sert( sizeof(u32
484f0 29 3d 3d 34 20 29 3b 0d 0a 20 20 69 20 3d 20 2a  )==4 );..  i = *
484f1 28 75 33 32 20 2a 29 70 43 6f 6f 72 64 3b 0d 0a  (u32 *)pCoord;..
484f2 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 32 34 29    p[0] = (i>>24)
484f3 26 30 78 46 46 3b 0d 0a 20 20 70 5b 31 5d 20 3d  &0xFF;..  p[1] =
484f4 20 28 69 3e 3e 31 36 29 26 30 78 46 46 3b 0d 0a   (i>>16)&0xFF;..
484f5 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20 38 29    p[2] = (i>> 8)
484f6 26 30 78 46 46 3b 0d 0a 20 20 70 5b 33 5d 20 3d  &0xFF;..  p[3] =
484f7 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0d 0a   (i>> 0)&0xFF;..
484f8 20 20 72 65 74 75 72 6e 20 34 3b 0d 0a 7d 0d 0a    return 4;..}..
484f9 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65  static int write
484fa 49 6e 74 36 34 28 75 38 20 2a 70 2c 20 69 36 34  Int64(u8 *p, i64
484fb 20 69 29 7b 0d 0a 20 20 70 5b 30 5d 20 3d 20 28   i){..  p[0] = (
484fc 69 3e 3e 35 36 29 26 30 78 46 46 3b 0d 0a 20 20  i>>56)&0xFF;..  
484fd 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26 30  p[1] = (i>>48)&0
484fe 78 46 46 3b 0d 0a 20 20 70 5b 32 5d 20 3d 20 28  xFF;..  p[2] = (
484ff 69 3e 3e 34 30 29 26 30 78 46 46 3b 0d 0a 20 20  i>>40)&0xFF;..  
48500 70 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30  p[3] = (i>>32)&0
48501 78 46 46 3b 0d 0a 20 20 70 5b 34 5d 20 3d 20 28  xFF;..  p[4] = (
48502 69 3e 3e 32 34 29 26 30 78 46 46 3b 0d 0a 20 20  i>>24)&0xFF;..  
48503 70 5b 35 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30  p[5] = (i>>16)&0
48504 78 46 46 3b 0d 0a 20 20 70 5b 36 5d 20 3d 20 28  xFF;..  p[6] = (
48505 69 3e 3e 20 38 29 26 30 78 46 46 3b 0d 0a 20 20  i>> 8)&0xFF;..  
48506 70 5b 37 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[7] = (i>> 0)&0
48507 78 46 46 3b 0d 0a 20 20 72 65 74 75 72 6e 20 38  xFF;..  return 8
48508 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
48509 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ncrement the ref
4850a 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20  erence count of 
4850b 6e 6f 64 65 20 70 2e 0d 0a 2a 2f 0d 0a 73 74 61  node p...*/..sta
4850c 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66  tic void nodeRef
4850d 65 72 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65  erence(RtreeNode
4850e 20 2a 70 29 7b 0d 0a 20 20 69 66 28 20 70 20 29   *p){..  if( p )
4850f 7b 0d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  {..    p->nRef++
48510 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  ;..  }..}..../*.
48511 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
48512 6e 74 65 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20  ntent of node p 
48513 28 73 65 74 20 61 6c 6c 20 62 79 74 65 73 20 74  (set all bytes t
48514 6f 20 30 78 30 30 29 2e 0d 0a 2a 2f 0d 0a 73 74  o 0x00)...*/..st
48515 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 5a 65  atic void nodeZe
48516 72 6f 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ro(Rtree *pRtree
48517 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 29 7b  , RtreeNode *p){
48518 0d 0a 20 20 6d 65 6d 73 65 74 28 26 70 2d 3e 7a  ..  memset(&p->z
48519 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52 74 72  Data[2], 0, pRtr
4851a 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 32 29  ee->iNodeSize-2)
4851b 3b 0d 0a 20 20 70 2d 3e 69 73 44 69 72 74 79 20  ;..  p->isDirty 
4851c 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  = 1;..}..../*..*
4851d 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64 65 20 6e  * Given a node n
4851e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20 72 65 74  umber iNode, ret
4851f 75 72 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f  urn the correspo
48520 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20 75 73 65  nding key to use
48521 0d 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74 72 65  ..** in the Rtre
48522 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e 0d 0a  e.aHash table...
48523 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  */..static int n
48524 6f 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64  odeHash(i64 iNod
48525 65 29 7b 0d 0a 20 20 72 65 74 75 72 6e 20 28 0d  e){..  return (.
48526 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29  .    (iNode>>56)
48527 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e   ^ (iNode>>48) ^
48528 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28   (iNode>>40) ^ (
48529 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20 0d 0a 20  iNode>>32) ^ .. 
4852a 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29 20 5e     (iNode>>24) ^
4852b 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e 20 28   (iNode>>16) ^ (
4852c 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28 69 4e  iNode>> 8) ^ (iN
4852d 6f 64 65 3e 3e 20 30 29 0d 0a 20 20 29 20 25 20  ode>> 0)..  ) % 
4852e 48 41 53 48 53 49 5a 45 3b 0d 0a 7d 0d 0a 0d 0a  HASHSIZE;..}....
4852f 2f 2a 0d 0a 2a 2a 20 53 65 61 72 63 68 20 74 68  /*..** Search th
48530 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62 6c  e node hash tabl
48531 65 20 66 6f 72 20 6e 6f 64 65 20 69 4e 6f 64 65  e for node iNode
48532 2e 20 49 66 20 66 6f 75 6e 64 2c 20 72 65 74 75  . If found, retu
48533 72 6e 20 61 20 70 6f 69 6e 74 65 72 0d 0a 2a 2a  rn a pointer..**
48534 20 74 6f 20 69 74 2e 20 4f 74 68 65 72 77 69 73   to it. Otherwis
48535 65 2c 20 72 65 74 75 72 6e 20 30 2e 0d 0a 2a 2f  e, return 0...*/
48536 0d 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f  ..static RtreeNo
48537 64 65 20 2a 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  de *nodeHashLook
48538 75 70 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  up(Rtree *pRtree
48539 2c 20 69 36 34 20 69 4e 6f 64 65 29 7b 0d 0a 20  , i64 iNode){.. 
4853a 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 3b 0d 0a   RtreeNode *p;..
4853b 20 20 66 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e    for(p=pRtree->
4853c 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 69  aHash[nodeHash(i
4853d 4e 6f 64 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e  Node)]; p && p->
4853e 69 4e 6f 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d  iNode!=iNode; p=
4853f 70 2d 3e 70 4e 65 78 74 29 3b 0d 0a 20 20 72 65  p->pNext);..  re
48540 74 75 72 6e 20 70 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  turn p;..}..../*
48541 0d 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20 70 4e  ..** Add node pN
48542 6f 64 65 20 74 6f 20 74 68 65 20 6e 6f 64 65 20  ode to the node 
48543 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a 2a 2f 0d  hash table...*/.
48544 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
48545 65 48 61 73 68 49 6e 73 65 72 74 28 52 74 72 65  eHashInsert(Rtre
48546 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
48547 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0d 0a 20  Node *pNode){.. 
48548 20 69 6e 74 20 69 48 61 73 68 3b 0d 0a 20 20 61   int iHash;..  a
48549 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 70 4e  ssert( pNode->pN
4854a 65 78 74 3d 3d 30 20 29 3b 0d 0a 20 20 69 48 61  ext==0 );..  iHa
4854b 73 68 20 3d 20 6e 6f 64 65 48 61 73 68 28 70 4e  sh = nodeHash(pN
4854c 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a 20 20  ode->iNode);..  
4854d 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70  pNode->pNext = p
4854e 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61  Rtree->aHash[iHa
4854f 73 68 5d 3b 0d 0a 20 20 70 52 74 72 65 65 2d 3e  sh];..  pRtree->
48550 61 48 61 73 68 5b 69 48 61 73 68 5d 20 3d 20 70  aHash[iHash] = p
48551 4e 6f 64 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  Node;..}..../*..
48552 2a 2a 20 52 65 6d 6f 76 65 20 6e 6f 64 65 20 70  ** Remove node p
48553 4e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 6e 6f  Node from the no
48554 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0d 0a  de hash table...
48555 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
48556 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 52  nodeHashDelete(R
48557 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
48558 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b  reeNode *pNode){
48559 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a  ..  RtreeNode **
4855a 70 70 3b 0d 0a 20 20 69 66 28 20 70 4e 6f 64 65  pp;..  if( pNode
4855b 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0d 0a 20  ->iNode!=0 ){.. 
4855c 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65 2d     pp = &pRtree-
4855d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28  >aHash[nodeHash(
4855e 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0d  pNode->iNode)];.
4855f 0a 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70  .    for( ; (*pp
48560 29 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26  )!=pNode; pp = &
48561 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61  (*pp)->pNext){ a
48562 73 73 65 72 74 28 2a 70 70 29 3b 20 7d 0d 0a 20  ssert(*pp); }.. 
48563 20 20 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e     *pp = pNode->
48564 70 4e 65 78 74 3b 0d 0a 20 20 20 20 70 4e 6f 64  pNext;..    pNod
48565 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0d 0a 20  e->pNext = 0;.. 
48566 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
48567 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74  Allocate and ret
48568 75 72 6e 20 6e 65 77 20 72 2d 74 72 65 65 20 6e  urn new r-tree n
48569 6f 64 65 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20  ode. Initially, 
4856a 28 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65  (RtreeNode.iNode
4856b 3d 3d 30 29 2c 0d 0a 2a 2a 20 69 6e 64 69 63 61  ==0),..** indica
4856c 74 69 6e 67 20 74 68 61 74 20 6e 6f 64 65 20 68  ting that node h
4856d 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  as not yet been 
4856e 61 73 73 69 67 6e 65 64 20 61 20 6e 6f 64 65 20  assigned a node 
4856f 6e 75 6d 62 65 72 2e 20 49 74 20 69 73 0d 0a 2a  number. It is..*
48570 2a 20 61 73 73 69 67 6e 65 64 20 61 20 6e 6f 64  * assigned a nod
48571 65 20 6e 75 6d 62 65 72 20 77 68 65 6e 20 6e 6f  e number when no
48572 64 65 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  deWrite() is cal
48573 6c 65 64 20 74 6f 20 77 72 69 74 65 20 74 68 65  led to write the
48574 0d 0a 2a 2a 20 6e 6f 64 65 20 63 6f 6e 74 65 6e  ..** node conten
48575 74 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  ts out to the da
48576 74 61 62 61 73 65 2e 0d 0a 2a 2f 0d 0a 73 74 61  tabase...*/..sta
48577 74 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e  tic RtreeNode *n
48578 6f 64 65 4e 65 77 28 52 74 72 65 65 20 2a 70 52  odeNew(Rtree *pR
48579 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
4857a 2a 70 50 61 72 65 6e 74 29 7b 0d 0a 20 20 52 74  *pParent){..  Rt
4857b 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0d  reeNode *pNode;.
4857c 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72 65  .  pNode = (Rtre
4857d 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33 5f  eNode *)sqlite3_
4857e 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
4857f 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72 65  reeNode) + pRtre
48580 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0d 0a  e->iNodeSize);..
48581 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0d 0a    if( pNode ){..
48582 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65      memset(pNode
48583 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , 0, sizeof(Rtre
48584 65 4e 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d  eNode) + pRtree-
48585 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0d 0a 20 20  >iNodeSize);..  
48586 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d    pNode->zData =
48587 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d   (u8 *)&pNode[1]
48588 3b 0d 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52  ;..    pNode->nR
48589 65 66 20 3d 20 31 3b 0d 0a 20 20 20 20 70 4e 6f  ef = 1;..    pNo
4858a 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50  de->pParent = pP
4858b 61 72 65 6e 74 3b 0d 0a 20 20 20 20 70 4e 6f 64  arent;..    pNod
4858c 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0d  e->isDirty = 1;.
4858d 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
4858e 63 65 28 70 50 61 72 65 6e 74 29 3b 0d 0a 20 20  ce(pParent);..  
4858f 7d 0d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f 64  }..  return pNod
48590 65 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  e;..}..../*..** 
48591 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
48592 63 65 20 74 6f 20 61 6e 20 72 2d 74 72 65 65 20  ce to an r-tree 
48593 6e 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  node...*/..stati
48594 63 20 69 6e 74 0d 0a 6e 6f 64 65 41 63 71 75 69  c int..nodeAcqui
48595 72 65 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52  re(..  Rtree *pR
48596 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
48597 20 20 2f 2a 20 52 2d 74 72 65 65 20 73 74 72 75    /* R-tree stru
48598 63 74 75 72 65 20 2a 2f 0d 0a 20 20 69 36 34 20  cture */..  i64 
48599 69 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20  iNode,          
4859a 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6e         /* Node n
4859b 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a 2f  umber to load */
4859c 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ..  RtreeNode *p
4859d 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f  Parent,        /
4859e 2a 20 45 69 74 68 65 72 20 74 68 65 20 70 61 72  * Either the par
4859f 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c  ent node or NULL
485a0 20 2a 2f 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65   */..  RtreeNode
485a1 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20 20   **ppNode       
485a2 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69 72    /* OUT: Acquir
485a3 65 64 20 6e 6f 64 65 20 2a 2f 0d 0a 29 7b 0d 0a  ed node */..){..
485a4 20 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74    int rc;..  int
485a5 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b   rc2 = SQLITE_OK
485a6 3b 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ;..  RtreeNode *
485a7 70 4e 6f 64 65 3b 0d 0a 0d 0a 20 20 2f 2a 20 43  pNode;....  /* C
485a8 68 65 63 6b 20 69 66 20 74 68 65 20 72 65 71 75  heck if the requ
485a9 65 73 74 65 64 20 6e 6f 64 65 20 69 73 20 61 6c  ested node is al
485aa 72 65 61 64 79 20 69 6e 20 74 68 65 20 68 61 73  ready in the has
485ab 68 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 0d  h table. If so,.
485ac 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73 65 20 69  .  ** increase i
485ad 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  ts reference cou
485ae 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74  nt and return it
485af 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 28  ...  */..  if( (
485b0 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73 68  pNode = nodeHash
485b1 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69  Lookup(pRtree, i
485b2 4e 6f 64 65 29 29 20 29 7b 0d 0a 20 20 20 20 61  Node)) ){..    a
485b3 73 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20  ssert( !pParent 
485b4 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65  || !pNode->pPare
485b5 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61  nt || pNode->pPa
485b6 72 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b  rent==pParent );
485b7 0d 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e  ..    if( pParen
485b8 74 20 26 26 20 21 70 4e 6f 64 65 2d 3e 70 50 61  t && !pNode->pPa
485b9 72 65 6e 74 20 29 7b 0d 0a 20 20 20 20 20 20 6e  rent ){..      n
485ba 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
485bb 72 65 6e 74 29 3b 0d 0a 20 20 20 20 20 20 70 4e  rent);..      pN
485bc 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  ode->pParent = p
485bd 50 61 72 65 6e 74 3b 0d 0a 20 20 20 20 7d 0d 0a  Parent;..    }..
485be 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b      pNode->nRef+
485bf 2b 3b 0d 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20  +;..    *ppNode 
485c0 3d 20 70 4e 6f 64 65 3b 0d 0a 20 20 20 20 72 65  = pNode;..    re
485c1 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
485c2 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65  .  }....  sqlite
485c3 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
485c4 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c 20  ree->pReadNode, 
485c5 31 2c 20 69 4e 6f 64 65 29 3b 0d 0a 20 20 72 63  1, iNode);..  rc
485c6 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
485c7 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
485c8 65 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53  e);..  if( rc==S
485c9 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20 20  QLITE_ROW ){..  
485ca 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c 6f    const u8 *zBlo
485cb 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  b = sqlite3_colu
485cc 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d 3e  mn_blob(pRtree->
485cd 70 52 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0d 0a  pReadNode, 0);..
485ce 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
485cf 69 4e 6f 64 65 53 69 7a 65 3d 3d 73 71 6c 69 74  iNodeSize==sqlit
485d0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
485d1 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
485d2 65 2c 20 30 29 20 29 7b 0d 0a 20 20 20 20 20 20  e, 0) ){..      
485d3 70 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f  pNode = (RtreeNo
485d4 64 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  de *)sqlite3_mal
485d5 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
485d6 4e 6f 64 65 29 2b 70 52 74 72 65 65 2d 3e 69 4e  Node)+pRtree->iN
485d7 6f 64 65 53 69 7a 65 29 3b 0d 0a 20 20 20 20 20  odeSize);..     
485d8 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b 0d 0a   if( !pNode ){..
485d9 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 53 51          rc2 = SQ
485da 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
485db 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
485dc 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e     pNode->pParen
485dd 74 20 3d 20 70 50 61 72 65 6e 74 3b 0d 0a 20 20  t = pParent;..  
485de 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61        pNode->zDa
485df 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64  ta = (u8 *)&pNod
485e0 65 5b 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 70  e[1];..        p
485e1 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0d  Node->nRef = 1;.
485e2 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e  .        pNode->
485e3 69 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b 0d 0a  iNode = iNode;..
485e4 20 20 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69          pNode->i
485e5 73 44 69 72 74 79 20 3d 20 30 3b 0d 0a 20 20 20  sDirty = 0;..   
485e6 20 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78       pNode->pNex
485e7 74 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 20 20  t = 0;..        
485e8 6d 65 6d 63 70 79 28 70 4e 6f 64 65 2d 3e 7a 44  memcpy(pNode->zD
485e9 61 74 61 2c 20 7a 42 6c 6f 62 2c 20 70 52 74 72  ata, zBlob, pRtr
485ea 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0d  ee->iNodeSize);.
485eb 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65 66  .        nodeRef
485ec 65 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b  erence(pParent);
485ed 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
485ee 0d 0a 20 20 7d 0d 0a 20 20 72 63 20 3d 20 73 71  ..  }..  rc = sq
485ef 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
485f0 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0d  ee->pReadNode);.
485f1 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
485f2 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 3b  E_OK ) rc = rc2;
485f3 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ....  /* If the 
485f4 72 6f 6f 74 20 6e 6f 64 65 20 77 61 73 20 6a 75  root node was ju
485f5 73 74 20 6c 6f 61 64 65 64 2c 20 73 65 74 20 70  st loaded, set p
485f6 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20 74 6f  Rtree->iDepth to
485f7 20 74 68 65 20 68 65 69 67 68 74 0d 0a 20 20 2a   the height..  *
485f8 2a 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  * of the r-tree 
485f9 73 74 72 75 63 74 75 72 65 2e 20 41 20 68 65 69  structure. A hei
485fa 67 68 74 20 6f 66 20 7a 65 72 6f 20 6d 65 61 6e  ght of zero mean
485fb 73 20 61 6c 6c 20 64 61 74 61 20 69 73 20 73 74  s all data is st
485fc 6f 72 65 64 20 6f 6e 0d 0a 20 20 2a 2a 20 74 68  ored on..  ** th
485fd 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 41 20 68  e root node. A h
485fe 65 69 67 68 74 20 6f 66 20 6f 6e 65 20 6d 65 61  eight of one mea
485ff 6e 73 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20  ns the children 
48600 6f 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  of the root node
48601 0d 0a 20 20 2a 2a 20 61 72 65 20 74 68 65 20 6c  ..  ** are the l
48602 65 61 76 65 73 2c 20 61 6e 64 20 73 6f 20 6f 6e  eaves, and so on
48603 2e 20 49 66 20 74 68 65 20 64 65 70 74 68 20 61  . If the depth a
48604 73 20 73 70 65 63 69 66 69 65 64 20 6f 6e 20 74  s specified on t
48605 68 65 20 72 6f 6f 74 20 6e 6f 64 65 0d 0a 20 20  he root node..  
48606 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ** is greater th
48607 61 6e 20 52 54 52 45 45 5f 4d 41 58 5f 44 45 50  an RTREE_MAX_DEP
48608 54 48 2c 20 74 68 65 20 72 2d 74 72 65 65 20 73  TH, the r-tree s
48609 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 62 65  tructure must be
4860a 20 63 6f 72 72 75 70 74 2e 0d 0a 20 20 2a 2f 0d   corrupt...  */.
4860b 0a 20 20 69 66 28 20 70 4e 6f 64 65 20 26 26 20  .  if( pNode && 
4860c 69 4e 6f 64 65 3d 3d 31 20 29 7b 0d 0a 20 20 20  iNode==1 ){..   
4860d 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
4860e 3d 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64  = readInt16(pNod
4860f 65 2d 3e 7a 44 61 74 61 29 3b 0d 0a 20 20 20 20  e->zData);..    
48610 69 66 28 20 70 52 74 72 65 65 2d 3e 69 44 65 70  if( pRtree->iDep
48611 74 68 3e 52 54 52 45 45 5f 4d 41 58 5f 44 45 50  th>RTREE_MAX_DEP
48612 54 48 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  TH ){..      rc 
48613 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
48614 5f 56 54 41 42 3b 0d 0a 20 20 20 20 7d 0d 0a 20  _VTAB;..    }.. 
48615 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 6e 6f   }....  /* If no
48616 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
48617 72 65 64 20 73 6f 20 66 61 72 2c 20 63 68 65 63  red so far, chec
48618 6b 20 69 66 20 74 68 65 20 22 6e 75 6d 62 65 72  k if the "number
48619 20 6f 66 20 65 6e 74 72 69 65 73 22 0d 0a 20 20   of entries"..  
4861a 2a 2a 20 66 69 65 6c 64 20 6f 6e 20 74 68 65 20  ** field on the 
4861b 6e 6f 64 65 20 69 73 20 74 6f 6f 20 6c 61 72 67  node is too larg
4861c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68  e. If so, set th
4861d 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f  e return code to
4861e 20 0d 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43   ..  ** SQLITE_C
4861f 4f 52 52 55 50 54 5f 56 54 41 42 2e 0d 0a 20 20  ORRUPT_VTAB...  
48620 2a 2f 0d 0a 20 20 69 66 28 20 70 4e 6f 64 65 20  */..  if( pNode 
48621 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
48622 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 4e 43 45   ){..    if( NCE
48623 4c 4c 28 70 4e 6f 64 65 29 3e 28 28 70 52 74 72  LL(pNode)>((pRtr
48624 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29  ee->iNodeSize-4)
48625 2f 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50  /pRtree->nBytesP
48626 65 72 43 65 6c 6c 29 20 29 7b 0d 0a 20 20 20 20  erCell) ){..    
48627 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
48628 52 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 20  RRUPT_VTAB;..   
48629 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28   }..  }....  if(
4862a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
4862b 7b 0d 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  {..    if( pNode
4862c 21 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 6e 6f  !=0 ){..      no
4862d 64 65 48 61 73 68 49 6e 73 65 72 74 28 70 52 74  deHashInsert(pRt
4862e 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0d 0a 20 20  ree, pNode);..  
4862f 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
48630 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
48631 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 20 20 7d  UPT_VTAB;..    }
48632 0d 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20  ..    *ppNode = 
48633 70 4e 6f 64 65 3b 0d 0a 20 20 7d 65 6c 73 65 7b  pNode;..  }else{
48634 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
48635 65 65 28 70 4e 6f 64 65 29 3b 0d 0a 20 20 20 20  ee(pNode);..    
48636 2a 70 70 4e 6f 64 65 20 3d 20 30 3b 0d 0a 20 20  *ppNode = 0;..  
48637 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  }....  return rc
48638 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 4f  ;..}..../*..** O
48639 76 65 72 77 72 69 74 65 20 63 65 6c 6c 20 69 43  verwrite cell iC
4863a 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64  ell of node pNod
4863b 65 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65  e with the conte
4863c 6e 74 73 20 6f 66 20 70 43 65 6c 6c 2e 0d 0a 2a  nts of pCell...*
4863d 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  /..static void n
4863e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c  odeOverwriteCell
4863f 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  (..  Rtree *pRtr
48640 65 65 2c 20 0d 0a 20 20 52 74 72 65 65 4e 6f 64  ee, ..  RtreeNod
48641 65 20 2a 70 4e 6f 64 65 2c 20 20 0d 0a 20 20 52  e *pNode,  ..  R
48642 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c  treeCell *pCell,
48643 20 0d 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0d 0a   ..  int iCell..
48644 29 7b 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20  ){..  int ii;.. 
48645 20 75 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d   u8 *p = &pNode-
48646 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
48647 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
48648 2a 69 43 65 6c 6c 5d 3b 0d 0a 20 20 70 20 2b 3d  *iCell];..  p +=
48649 20 77 72 69 74 65 49 6e 74 36 34 28 70 2c 20 70   writeInt64(p, p
4864a 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0d 0a  Cell->iRowid);..
4864b 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
4864c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
4864d 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 70 20 2b   ii++){..    p +
4864e 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70 2c 20  = writeCoord(p, 
4864f 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69  &pCell->aCoord[i
48650 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 20 20 70 4e 6f  i]);..  }..  pNo
48651 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
48652 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
48653 6d 6f 76 65 20 63 65 6c 6c 20 74 68 65 20 63 65  move cell the ce
48654 6c 6c 20 77 69 74 68 20 69 6e 64 65 78 20 69 43  ll with index iC
48655 65 6c 6c 20 66 72 6f 6d 20 6e 6f 64 65 20 70 4e  ell from node pN
48656 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ode...*/..static
48657 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c 65 74 65   void nodeDelete
48658 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72  Cell(Rtree *pRtr
48659 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
4865a 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Node, int iCell)
4865b 7b 0d 0a 20 20 75 38 20 2a 70 44 73 74 20 3d 20  {..  u8 *pDst = 
4865c 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20  &pNode->zData[4 
4865d 2b 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  + pRtree->nBytes
4865e 50 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0d  PerCell*iCell];.
4865f 0a 20 20 75 38 20 2a 70 53 72 63 20 3d 20 26 70  .  u8 *pSrc = &p
48660 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79 74  Dst[pRtree->nByt
48661 65 73 50 65 72 43 65 6c 6c 5d 3b 0d 0a 20 20 69  esPerCell];..  i
48662 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c  nt nByte = (NCEL
48663 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c  L(pNode) - iCell
48664 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65 2d 3e   - 1) * pRtree->
48665 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0d 0a  nBytesPerCell;..
48666 20 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c 20    memmove(pDst, 
48667 70 53 72 63 2c 20 6e 42 79 74 65 29 3b 0d 0a 20  pSrc, nByte);.. 
48668 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f   writeInt16(&pNo
48669 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43  de->zData[2], NC
4866a 45 4c 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0d 0a  ELL(pNode)-1);..
4866b 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79    pNode->isDirty
4866c 20 3d 20 31 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a   = 1;..}..../*..
4866d 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  ** Insert the co
4866e 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
4866f 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70  Cell into node p
48670 4e 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73  Node. If the ins
48671 65 72 74 0d 0a 2a 2a 20 69 73 20 73 75 63 63 65  ert..** is succe
48672 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51  ssful, return SQ
48673 4c 49 54 45 5f 4f 4b 2e 0d 0a 2a 2a 0d 0a 2a 2a  LITE_OK...**..**
48674 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   If there is not
48675 20 65 6e 6f 75 67 68 20 66 72 65 65 20 73 70 61   enough free spa
48676 63 65 20 69 6e 20 70 4e 6f 64 65 2c 20 72 65 74  ce in pNode, ret
48677 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e  urn SQLITE_FULL.
48678 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
48679 0d 0a 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c  ..nodeInsertCell
4867a 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  (..  Rtree *pRtr
4867b 65 65 2c 20 0d 0a 20 20 52 74 72 65 65 4e 6f 64  ee, ..  RtreeNod
4867c 65 20 2a 70 4e 6f 64 65 2c 20 0d 0a 20 20 52 74  e *pNode, ..  Rt
4867d 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0d  reeCell *pCell .
4867e 0a 29 7b 0d 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  .){..  int nCell
4867f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
48680 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
48681 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
48682 69 6e 20 70 4e 6f 64 65 20 2a 2f 0d 0a 20 20 69  in pNode */..  i
48683 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20  nt nMaxCell;    
48684 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
48685 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  Maximum number o
48686 66 20 63 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64  f cells for pNod
48687 65 20 2a 2f 0d 0a 0d 0a 20 20 6e 4d 61 78 43 65  e */....  nMaxCe
48688 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e 69 4e  ll = (pRtree->iN
48689 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74 72 65  odeSize-4)/pRtre
4868a 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
4868b 3b 0d 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45  ;..  nCell = NCE
4868c 4c 4c 28 70 4e 6f 64 65 29 3b 0d 0a 0d 0a 20 20  LL(pNode);....  
4868d 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 3d 6e  assert( nCell<=n
4868e 4d 61 78 43 65 6c 6c 20 29 3b 0d 0a 20 20 69 66  MaxCell );..  if
4868f 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
48690 20 29 7b 0d 0a 20 20 20 20 6e 6f 64 65 4f 76 65   ){..    nodeOve
48691 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65  rwriteCell(pRtre
48692 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c  e, pNode, pCell,
48693 20 6e 43 65 6c 6c 29 3b 0d 0a 20 20 20 20 77 72   nCell);..    wr
48694 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65 2d  iteInt16(&pNode-
48695 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e 43 65 6c 6c  >zData[2], nCell
48696 2b 31 29 3b 0d 0a 20 20 20 20 70 4e 6f 64 65 2d  +1);..    pNode-
48697 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0d 0a 20  >isDirty = 1;.. 
48698 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 28   }....  return (
48699 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29  nCell==nMaxCell)
4869a 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
4869b 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69  f the node is di
4869c 72 74 79 2c 20 77 72 69 74 65 20 69 74 20 6f 75  rty, write it ou
4869d 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
4869e 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
4869f 6e 74 0d 0a 6e 6f 64 65 57 72 69 74 65 28 52 74  nt..nodeWrite(Rt
486a0 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
486a1 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0d  eeNode *pNode){.
486a2 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
486a3 54 45 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 4e  TE_OK;..  if( pN
486a4 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 29 7b 0d  ode->isDirty ){.
486a5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  .    sqlite3_stm
486a6 74 20 2a 70 20 3d 20 70 52 74 72 65 65 2d 3e 70  t *p = pRtree->p
486a7 57 72 69 74 65 4e 6f 64 65 3b 0d 0a 20 20 20 20  WriteNode;..    
486a8 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
486a9 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
486aa 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c  e3_bind_int64(p,
486ab 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65   1, pNode->iNode
486ac 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a  );..    }else{..
486ad 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
486ae 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b 0d 0a  nd_null(p, 1);..
486af 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
486b0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20  e3_bind_blob(p, 
486b1 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  2, pNode->zData,
486b2 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
486b3 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ze, SQLITE_STATI
486b4 43 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  C);..    sqlite3
486b5 5f 73 74 65 70 28 70 29 3b 0d 0a 20 20 20 20 70  _step(p);..    p
486b6 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
486b7 30 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  0;..    rc = sql
486b8 69 74 65 33 5f 72 65 73 65 74 28 70 29 3b 0d 0a  ite3_reset(p);..
486b9 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69      if( pNode->i
486ba 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 3d 3d 53  Node==0 && rc==S
486bb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
486bc 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20     pNode->iNode 
486bd 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  = sqlite3_last_i
486be 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74 72  nsert_rowid(pRtr
486bf 65 65 2d 3e 64 62 29 3b 0d 0a 20 20 20 20 20 20  ee->db);..      
486c0 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70  nodeHashInsert(p
486c1 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0d 0a  Rtree, pNode);..
486c2 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
486c3 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
486c4 2a 0d 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  *..** Release a 
486c5 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 6e  reference to a n
486c6 6f 64 65 2e 20 49 66 20 74 68 65 20 6e 6f 64 65  ode. If the node
486c7 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 74 68   is dirty and th
486c8 65 20 72 65 66 65 72 65 6e 63 65 0d 0a 2a 2a 20  e reference..** 
486c9 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 7a  count drops to z
486ca 65 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20 64 61  ero, the node da
486cb 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ta is written to
486cc 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0d 0a   the database...
486cd 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 0d 0a  */..static int..
486ce 6e 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72 65  nodeRelease(Rtre
486cf 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
486d0 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0d 0a 20  Node *pNode){.. 
486d1 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
486d2 5f 4f 4b 3b 0d 0a 20 20 69 66 28 20 70 4e 6f 64  _OK;..  if( pNod
486d3 65 20 29 7b 0d 0a 20 20 20 20 61 73 73 65 72 74  e ){..    assert
486d4 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30 20  ( pNode->nRef>0 
486d5 29 3b 0d 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e  );..    pNode->n
486d6 52 65 66 2d 2d 3b 0d 0a 20 20 20 20 69 66 28 20  Ref--;..    if( 
486d7 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20 29  pNode->nRef==0 )
486d8 7b 0d 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f  {..      if( pNo
486d9 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0d  de->iNode==1 ){.
486da 0a 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d  .        pRtree-
486db 3e 69 44 65 70 74 68 20 3d 20 2d 31 3b 0d 0a 20  >iDepth = -1;.. 
486dc 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
486dd 28 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  ( pNode->pParent
486de 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
486df 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
486e0 74 72 65 65 2c 20 70 4e 6f 64 65 2d 3e 70 50 61  tree, pNode->pPa
486e1 72 65 6e 74 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  rent);..      }.
486e2 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
486e3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
486e4 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 57 72       rc = nodeWr
486e5 69 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ite(pRtree, pNod
486e6 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20  e);..      }..  
486e7 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65      nodeHashDele
486e8 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
486e9 29 3b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
486ea 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0d 0a  3_free(pNode);..
486eb 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
486ec 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
486ed 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  *..** Return the
486ee 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
486ef 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64  value associated
486f0 20 77 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c   with cell iCell
486f1 20 6f 66 0d 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f   of..** node pNo
486f2 64 65 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20  de. If pNode is 
486f3 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 69  a leaf node, thi
486f4 73 20 69 73 20 61 20 72 6f 77 69 64 2e 20 49 66  s is a rowid. If
486f5 20 69 74 20 69 73 0d 0a 2a 2a 20 61 6e 20 69 6e   it is..** an in
486f6 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20 74 68 65  ternal node, the
486f7 6e 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  n the 64-bit int
486f8 65 67 65 72 20 69 73 20 61 20 63 68 69 6c 64 20  eger is a child 
486f9 70 61 67 65 20 6e 75 6d 62 65 72 2e 0d 0a 2a 2f  page number...*/
486fa 0d 0a 73 74 61 74 69 63 20 69 36 34 20 6e 6f 64  ..static i64 nod
486fb 65 47 65 74 52 6f 77 69 64 28 0d 0a 20 20 52 74  eGetRowid(..  Rt
486fc 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0d 0a 20  ree *pRtree, .. 
486fd 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
486fe 65 2c 20 0d 0a 20 20 69 6e 74 20 69 43 65 6c 6c  e, ..  int iCell
486ff 0d 0a 29 7b 0d 0a 20 20 61 73 73 65 72 74 28 20  ..){..  assert( 
48700 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64  iCell<NCELL(pNod
48701 65 29 20 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  e) );..  return 
48702 72 65 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65  readInt64(&pNode
48703 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72  ->zData[4 + pRtr
48704 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
48705 6c 2a 69 43 65 6c 6c 5d 29 3b 0d 0a 7d 0d 0a 0d  l*iCell]);..}...
48706 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 63  ./*..** Return c
48707 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72 64  oordinate iCoord
48708 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c 6c   from cell iCell
48709 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 0d   in node pNode..
4870a 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64  .*/..static void
4870b 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 0d 0a   nodeGetCoord(..
4870c 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
4870d 20 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a   ..  RtreeNode *
4870e 70 4e 6f 64 65 2c 20 0d 0a 20 20 69 6e 74 20 69  pNode, ..  int i
4870f 43 65 6c 6c 2c 0d 0a 20 20 69 6e 74 20 69 43 6f  Cell,..  int iCo
48710 6f 72 64 2c 0d 0a 20 20 52 74 72 65 65 43 6f 6f  ord,..  RtreeCoo
48711 72 64 20 2a 70 43 6f 6f 72 64 20 20 20 20 20 20  rd *pCoord      
48712 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
48713 20 77 72 69 74 65 20 72 65 73 75 6c 74 20 74 6f   write result to
48714 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 72 65 61 64 43   */..){..  readC
48715 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44 61  oord(&pNode->zDa
48716 74 61 5b 31 32 20 2b 20 70 52 74 72 65 65 2d 3e  ta[12 + pRtree->
48717 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43  nBytesPerCell*iC
48718 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d 2c  ell + 4*iCoord],
48719 20 70 43 6f 6f 72 64 29 3b 0d 0a 7d 0d 0a 0d 0a   pCoord);..}....
4871a 2f 2a 0d 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69  /*..** Deseriali
4871b 7a 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66  ze cell iCell of
4871c 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70   node pNode. Pop
4871d 75 6c 61 74 65 20 74 68 65 20 73 74 72 75 63 74  ulate the struct
4871e 75 72 65 20 70 6f 69 6e 74 65 64 0d 0a 2a 2a 20  ure pointed..** 
4871f 74 6f 20 62 79 20 70 43 65 6c 6c 20 77 69 74 68  to by pCell with
48720 20 74 68 65 20 72 65 73 75 6c 74 73 2e 0d 0a 2a   the results...*
48721 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  /..static void n
48722 6f 64 65 47 65 74 43 65 6c 6c 28 0d 0a 20 20 52  odeGetCell(..  R
48723 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0d 0a  tree *pRtree, ..
48724 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
48725 64 65 2c 20 0d 0a 20 20 69 6e 74 20 69 43 65 6c  de, ..  int iCel
48726 6c 2c 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  l,..  RtreeCell 
48727 2a 70 43 65 6c 6c 0d 0a 29 7b 0d 0a 20 20 69 6e  *pCell..){..  in
48728 74 20 69 69 3b 0d 0a 20 20 70 43 65 6c 6c 2d 3e  t ii;..  pCell->
48729 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
4872a 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e  Rowid(pRtree, pN
4872b 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0d 0a 20 20  ode, iCell);..  
4872c 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74  for(ii=0; ii<pRt
4872d 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69 69 2b  ree->nDim*2; ii+
4872e 2b 29 7b 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74  +){..    nodeGet
4872f 43 6f 6f 72 64 28 70 52 74 72 65 65 2c 20 70 4e  Coord(pRtree, pN
48730 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c 20  ode, iCell, ii, 
48731 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69  &pCell->aCoord[i
48732 69 5d 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a  i]);..  }..}....
48733 0d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
48734 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  laration for the
48735 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64   function that d
48736 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 0d  oes the work of.
48737 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20  .** the virtual 
48738 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72  table module xCr
48739 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f 6e 6e  eate() and xConn
4873a 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e 0d 0a  ect() methods...
4873b 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72  */..static int r
4873c 74 72 65 65 49 6e 69 74 28 0d 0a 20 20 73 71 6c  treeInit(..  sql
4873d 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20  ite3 *, void *, 
4873e 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
4873f 2a 63 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33  *const*, sqlite3
48740 5f 76 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a  _vtab **, char *
48741 2a 2c 20 69 6e 74 0d 0a 29 3b 0d 0a 0d 0a 2f 2a  *, int..);..../*
48742 20 0d 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74   ..** Rtree virt
48743 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
48744 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e   xCreate method.
48745 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
48746 20 72 74 72 65 65 43 72 65 61 74 65 28 0d 0a 20   rtreeCreate(.. 
48747 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0d 0a 20   sqlite3 *db,.. 
48748 20 76 6f 69 64 20 2a 70 41 75 78 2c 0d 0a 20 20   void *pAux,..  
48749 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
4874a 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
4874b 2c 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ,..  sqlite3_vta
4874c 62 20 2a 2a 70 70 56 74 61 62 2c 0d 0a 20 20 63  b **ppVtab,..  c
4874d 68 61 72 20 2a 2a 70 7a 45 72 72 0d 0a 29 7b 0d  har **pzErr..){.
4874e 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65 65 49  .  return rtreeI
4874f 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20 61 72  nit(db, pAux, ar
48750 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74 61 62  gc, argv, ppVtab
48751 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0d 0a 7d 0d  , pzErr, 1);..}.
48752 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 52 74 72 65 65  .../* ..** Rtree
48753 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
48754 6f 64 75 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d  odule xConnect m
48755 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  ethod...*/..stat
48756 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f 6e 6e  ic int rtreeConn
48757 65 63 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  ect(..  sqlite3 
48758 2a 64 62 2c 0d 0a 20 20 76 6f 69 64 20 2a 70 41  *db,..  void *pA
48759 75 78 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63 2c  ux,..  int argc,
4875a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
4875b 73 74 2a 61 72 67 76 2c 0d 0a 20 20 73 71 6c 69  st*argv,..  sqli
4875c 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61  te3_vtab **ppVta
4875d 62 2c 0d 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  b,..  char **pzE
4875e 72 72 0d 0a 29 7b 0d 0a 20 20 72 65 74 75 72 6e  rr..){..  return
4875f 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70   rtreeInit(db, p
48760 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  Aux, argc, argv,
48761 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20   ppVtab, pzErr, 
48762 30 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  0);..}..../*..**
48763 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Increment the r
48764 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 20  -tree reference 
48765 63 6f 75 6e 74 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  count...*/..stat
48766 69 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 66  ic void rtreeRef
48767 65 72 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52  erence(Rtree *pR
48768 74 72 65 65 29 7b 0d 0a 20 20 70 52 74 72 65 65  tree){..  pRtree
48769 2d 3e 6e 42 75 73 79 2b 2b 3b 0d 0a 7d 0d 0a 0d  ->nBusy++;..}...
4876a 0a 2f 2a 0d 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ./*..** Decremen
4876b 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
4876c 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 57 68  erence count. Wh
4876d 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
4876e 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 0d 0a   count reaches..
4876f 2a 2a 20 7a 65 72 6f 20 74 68 65 20 73 74 72 75  ** zero the stru
48770 63 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64  cture is deleted
48771 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
48772 69 64 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  id rtreeRelease(
48773 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0d  Rtree *pRtree){.
48774 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79  .  pRtree->nBusy
48775 2d 2d 3b 0d 0a 20 20 69 66 28 20 70 52 74 72 65  --;..  if( pRtre
48776 65 2d 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0d 0a  e->nBusy==0 ){..
48777 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
48778 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65  lize(pRtree->pRe
48779 61 64 4e 6f 64 65 29 3b 0d 0a 20 20 20 20 73 71  adNode);..    sq
4877a 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
4877b 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
4877c 65 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  e);..    sqlite3
4877d 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
4877e 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0d  ->pDeleteNode);.
4877f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
48780 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52  alize(pRtree->pR
48781 65 61 64 52 6f 77 69 64 29 3b 0d 0a 20 20 20 20  eadRowid);..    
48782 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
48783 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
48784 6f 77 69 64 29 3b 0d 0a 20 20 20 20 73 71 6c 69  owid);..    sqli
48785 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
48786 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
48787 64 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  d);..    sqlite3
48788 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
48789 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0d  ->pReadParent);.
4878a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
4878b 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 57  alize(pRtree->pW
4878c 72 69 74 65 50 61 72 65 6e 74 29 3b 0d 0a 20 20  riteParent);..  
4878d 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
4878e 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  ze(pRtree->pDele
4878f 74 65 50 61 72 65 6e 74 29 3b 0d 0a 20 20 20 20  teParent);..    
48790 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 52 74  sqlite3_free(pRt
48791 72 65 65 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d  ree);..  }..}...
48792 0a 2f 2a 20 0d 0a 2a 2a 20 52 74 72 65 65 20 76  ./* ..** Rtree v
48793 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
48794 75 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 20  ule xDisconnect 
48795 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74 61  method...*/..sta
48796 74 69 63 20 69 6e 74 20 72 74 72 65 65 44 69 73  tic int rtreeDis
48797 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33 5f  connect(sqlite3_
48798 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0d 0a 20  vtab *pVtab){.. 
48799 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 28 52   rtreeRelease((R
4879a 74 72 65 65 20 2a 29 70 56 74 61 62 29 3b 0d 0a  tree *)pVtab);..
4879b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
4879c 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a  OK;..}..../* ..*
4879d 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
4879e 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65  table module xDe
4879f 73 74 72 6f 79 20 6d 65 74 68 6f 64 2e 0d 0a 2a  stroy method...*
487a0 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  /..static int rt
487a1 72 65 65 44 65 73 74 72 6f 79 28 73 71 6c 69 74  reeDestroy(sqlit
487a2 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
487a3 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  ..  Rtree *pRtre
487a4 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74  e = (Rtree *)pVt
487a5 61 62 3b 0d 0a 20 20 69 6e 74 20 72 63 3b 0d 0a  ab;..  int rc;..
487a6 20 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20    char *zCreate 
487a7 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
487a8 66 28 0d 0a 20 20 20 20 22 44 52 4f 50 20 54 41  f(..    "DROP TA
487a9 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64  BLE '%q'.'%q_nod
487aa 65 27 3b 22 0d 0a 20 20 20 20 22 44 52 4f 50 20  e';"..    "DROP 
487ab 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 72  TABLE '%q'.'%q_r
487ac 6f 77 69 64 27 3b 22 0d 0a 20 20 20 20 22 44 52  owid';"..    "DR
487ad 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25  OP TABLE '%q'.'%
487ae 71 5f 70 61 72 65 6e 74 27 3b 22 2c 0d 0a 20 20  q_parent';",..  
487af 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70    pRtree->zDb, p
487b0 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0d 0a  Rtree->zName, ..
487b1 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c      pRtree->zDb,
487b2 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0d   pRtree->zName,.
487b3 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62  .    pRtree->zDb
487b4 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 0d  , pRtree->zName.
487b5 0a 20 20 29 3b 0d 0a 20 20 69 66 28 20 21 7a 43  .  );..  if( !zC
487b6 72 65 61 74 65 20 29 7b 0d 0a 20 20 20 20 72 63  reate ){..    rc
487b7 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
487b8 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
487b9 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
487ba 63 28 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43  c(pRtree->db, zC
487bb 72 65 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b  reate, 0, 0, 0);
487bc 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
487bd 65 65 28 7a 43 72 65 61 74 65 29 3b 0d 0a 20 20  ee(zCreate);..  
487be 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
487bf 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72  ITE_OK ){..    r
487c0 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72  treeRelease(pRtr
487c1 65 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72  ee);..  }....  r
487c2 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
487c3 2f 2a 20 0d 0a 2a 2a 20 52 74 72 65 65 20 76 69  /* ..** Rtree vi
487c4 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
487c5 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 2e  le xOpen method.
487c6 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
487c7 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69 74   rtreeOpen(sqlit
487c8 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
487c9 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
487ca 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b  sor **ppCursor){
487cb 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
487cc 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 52 74  ITE_NOMEM;..  Rt
487cd 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b  reeCursor *pCsr;
487ce 0d 0a 0d 0a 20 20 70 43 73 72 20 3d 20 28 52 74  ....  pCsr = (Rt
487cf 72 65 65 43 75 72 73 6f 72 20 2a 29 73 71 6c 69  reeCursor *)sqli
487d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
487d1 66 28 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b  f(RtreeCursor));
487d2 0d 0a 20 20 69 66 28 20 70 43 73 72 20 29 7b 0d  ..  if( pCsr ){.
487d3 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73 72  .    memset(pCsr
487d4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , 0, sizeof(Rtre
487d5 65 43 75 72 73 6f 72 29 29 3b 0d 0a 20 20 20 20  eCursor));..    
487d6 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
487d7 20 3d 20 70 56 54 61 62 3b 0d 0a 20 20 20 20 72   = pVTab;..    r
487d8 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
487d9 20 20 7d 0d 0a 20 20 2a 70 70 43 75 72 73 6f 72    }..  *ppCursor
487da 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62   = (sqlite3_vtab
487db 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0d  _cursor *)pCsr;.
487dc 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d  ...  return rc;.
487dd 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 46  .}....../*..** F
487de 72 65 65 20 74 68 65 20 52 74 72 65 65 43 75 72  ree the RtreeCur
487df 73 6f 72 2e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  sor.aConstraint[
487e0 5d 20 61 72 72 61 79 20 61 6e 64 20 69 74 73 20  ] array and its 
487e1 63 6f 6e 74 65 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73  contents...*/..s
487e2 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 43  tatic void freeC
487e3 75 72 73 6f 72 43 6f 6e 73 74 72 61 69 6e 74 73  ursorConstraints
487e4 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43  (RtreeCursor *pC
487e5 73 72 29 7b 0d 0a 20 20 69 66 28 20 70 43 73 72  sr){..  if( pCsr
487e6 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 29 7b  ->aConstraint ){
487e7 0d 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20  ..    int i;    
487e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487e9 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69      /* Used to i
487ea 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 63  terate through c
487eb 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61 79 20  onstraint array 
487ec 2a 2f 0d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  */..    for(i=0;
487ed 20 69 3c 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72   i<pCsr->nConstr
487ee 61 69 6e 74 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20  aint; i++){..   
487ef 20 20 20 73 71 6c 69 74 65 33 5f 72 74 72 65 65     sqlite3_rtree
487f0 5f 67 65 6f 6d 65 74 72 79 20 2a 70 47 65 6f 6d  _geometry *pGeom
487f1 20 3d 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72   = pCsr->aConstr
487f2 61 69 6e 74 5b 69 5d 2e 70 47 65 6f 6d 3b 0d 0a  aint[i].pGeom;..
487f3 20 20 20 20 20 20 69 66 28 20 70 47 65 6f 6d 20        if( pGeom 
487f4 29 7b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ){..        if( 
487f5 70 47 65 6f 6d 2d 3e 78 44 65 6c 55 73 65 72 20  pGeom->xDelUser 
487f6 29 20 70 47 65 6f 6d 2d 3e 78 44 65 6c 55 73 65  ) pGeom->xDelUse
487f7 72 28 70 47 65 6f 6d 2d 3e 70 55 73 65 72 29 3b  r(pGeom->pUser);
487f8 0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
487f9 33 5f 66 72 65 65 28 70 47 65 6f 6d 29 3b 0d 0a  3_free(pGeom);..
487fa 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
487fb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
487fc 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  (pCsr->aConstrai
487fd 6e 74 29 3b 0d 0a 20 20 20 20 70 43 73 72 2d 3e  nt);..    pCsr->
487fe 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b  aConstraint = 0;
487ff 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  ..  }..}..../* .
48800 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
48801 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
48802 43 6c 6f 73 65 20 6d 65 74 68 6f 64 2e 0d 0a 2a  Close method...*
48803 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  /..static int rt
48804 72 65 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  reeClose(sqlite3
48805 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75  _vtab_cursor *cu
48806 72 29 7b 0d 0a 20 20 52 74 72 65 65 20 2a 70 52  r){..  Rtree *pR
48807 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
48808 28 63 75 72 2d 3e 70 56 74 61 62 29 3b 0d 0a 20  (cur->pVtab);.. 
48809 20 69 6e 74 20 72 63 3b 0d 0a 20 20 52 74 72 65   int rc;..  Rtre
4880a 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  eCursor *pCsr = 
4880b 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63  (RtreeCursor *)c
4880c 75 72 3b 0d 0a 20 20 66 72 65 65 43 75 72 73 6f  ur;..  freeCurso
4880d 72 43 6f 6e 73 74 72 61 69 6e 74 73 28 70 43 73  rConstraints(pCs
4880e 72 29 3b 0d 0a 20 20 72 63 20 3d 20 6e 6f 64 65  r);..  rc = node
4880f 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
48810 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0d 0a 20  pCsr->pNode);.. 
48811 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
48812 73 72 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72  sr);..  return r
48813 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  c;..}..../*..** 
48814 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
48815 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f 66 20  ble module xEof 
48816 6d 65 74 68 6f 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20  method...**..** 
48817 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
48818 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  if the cursor do
48819 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
4881a 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
4881b 64 20 0d 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69  d ..** record (i
4881c 2e 65 20 69 66 20 74 68 65 20 73 63 61 6e 20 68  .e if the scan h
4881d 61 73 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72  as finished), or
4881e 20 7a 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e   zero otherwise.
4881f 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
48820 20 72 74 72 65 65 45 6f 66 28 73 71 6c 69 74 65   rtreeEof(sqlite
48821 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63  3_vtab_cursor *c
48822 75 72 29 7b 0d 0a 20 20 52 74 72 65 65 43 75 72  ur){..  RtreeCur
48823 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
48824 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b 0d  eeCursor *)cur;.
48825 0a 20 20 72 65 74 75 72 6e 20 28 70 43 73 72 2d  .  return (pCsr-
48826 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0d 0a 7d 0d 0a  >pNode==0);..}..
48827 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 72 2d 74  ../*..** The r-t
48828 72 65 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  ree constraint p
48829 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
4882a 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
4882b 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
4882c 0d 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20  ..** guaranteed 
4882d 74 6f 20 62 65 20 61 20 4d 41 54 43 48 20 63 6f  to be a MATCH co
4882e 6e 73 74 72 61 69 6e 74 2e 0d 0a 2a 2f 0d 0a 73  nstraint...*/..s
4882f 74 61 74 69 63 20 69 6e 74 20 74 65 73 74 52 74  tatic int testRt
48830 72 65 65 47 65 6f 6d 28 0d 0a 20 20 52 74 72 65  reeGeom(..  Rtre
48831 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20  e *pRtree,      
48832 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
48833 2d 54 72 65 65 20 6f 62 6a 65 63 74 20 2a 2f 0d  -Tree object */.
48834 0a 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  .  RtreeConstrai
48835 6e 74 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 2c  nt *pConstraint,
48836 20 20 20 2f 2a 20 4d 41 54 43 48 20 63 6f 6e 73     /* MATCH cons
48837 74 72 61 69 6e 74 20 74 6f 20 74 65 73 74 20 2a  traint to test *
48838 2f 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  /..  RtreeCell *
48839 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  pCell,          
4883a 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20       /* Cell to 
4883b 74 65 73 74 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a  test */..  int *
4883c 70 62 52 65 73 20 20 20 20 20 20 20 20 20 20 20  pbRes           
4883d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
4883e 54 3a 20 54 65 73 74 20 72 65 73 75 6c 74 20 2a  T: Test result *
4883f 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d  /..){..  int i;.
48840 0a 20 20 64 6f 75 62 6c 65 20 61 43 6f 6f 72 64  .  double aCoord
48841 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
48842 53 49 4f 4e 53 2a 32 5d 3b 0d 0a 20 20 69 6e 74  SIONS*2];..  int
48843 20 6e 43 6f 6f 72 64 20 3d 20 70 52 74 72 65 65   nCoord = pRtree
48844 2d 3e 6e 44 69 6d 2a 32 3b 0d 0a 0d 0a 20 20 61  ->nDim*2;....  a
48845 73 73 65 72 74 28 20 70 43 6f 6e 73 74 72 61 69  ssert( pConstrai
48846 6e 74 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4d 41  nt->op==RTREE_MA
48847 54 43 48 20 29 3b 0d 0a 20 20 61 73 73 65 72 74  TCH );..  assert
48848 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 70  ( pConstraint->p
48849 47 65 6f 6d 20 29 3b 0d 0a 0d 0a 20 20 66 6f 72  Geom );....  for
4884a 28 69 3d 30 3b 20 69 3c 6e 43 6f 6f 72 64 3b 20  (i=0; i<nCoord; 
4884b 69 2b 2b 29 7b 0d 0a 20 20 20 20 61 43 6f 6f 72  i++){..    aCoor
4884c 64 5b 69 5d 20 3d 20 44 43 4f 4f 52 44 28 70 43  d[i] = DCOORD(pC
4884d 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 5d 29 3b  ell->aCoord[i]);
4884e 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
4884f 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 78 47 65  pConstraint->xGe
48850 6f 6d 28 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  om(pConstraint->
48851 70 47 65 6f 6d 2c 20 6e 43 6f 6f 72 64 2c 20 61  pGeom, nCoord, a
48852 43 6f 6f 72 64 2c 20 70 62 52 65 73 29 3b 0d 0a  Coord, pbRes);..
48853 7d 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 43 75 72  }..../* ..** Cur
48854 73 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72  sor pCursor curr
48855 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ently points to 
48856 61 20 63 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d  a cell in a non-
48857 6c 65 61 66 20 70 61 67 65 2e 0d 0a 2a 2a 20 53  leaf page...** S
48858 65 74 20 2a 70 62 45 6f 66 20 74 6f 20 74 72 75  et *pbEof to tru
48859 65 20 69 66 20 74 68 65 20 73 75 62 2d 74 72 65  e if the sub-tre
4885a 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  e headed by the 
4885b 63 65 6c 6c 20 69 73 20 66 69 6c 74 65 72 65 64  cell is filtered
4885c 0d 0a 2a 2a 20 28 65 78 63 6c 75 64 65 64 29 20  ..** (excluded) 
4885d 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  by the constrain
4885e 74 73 20 69 6e 20 74 68 65 20 70 43 75 72 73 6f  ts in the pCurso
4885f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  r->aConstraint[]
48860 20 0d 0a 2a 2a 20 61 72 72 61 79 2c 20 6f 72 20   ..** array, or 
48861 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
48862 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ..**..** Return 
48863 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
48864 63 65 73 73 66 75 6c 20 6f 72 20 61 6e 20 53 51  cessful or an SQ
48865 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
48866 69 66 20 61 6e 20 65 72 72 6f 72 0d 0a 2a 2a 20  if an error..** 
48867 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 20  occurs within a 
48868 67 65 6f 6d 65 74 72 79 20 63 61 6c 6c 62 61 63  geometry callbac
48869 6b 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  k...*/..static i
4886a 6e 74 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c  nt testRtreeCell
4886b 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
4886c 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75  RtreeCursor *pCu
4886d 72 73 6f 72 2c 20 69 6e 74 20 2a 70 62 45 6f 66  rsor, int *pbEof
4886e 29 7b 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ){..  RtreeCell 
4886f 63 65 6c 6c 3b 0d 0a 20 20 69 6e 74 20 69 69 3b  cell;..  int ii;
48870 0d 0a 20 20 69 6e 74 20 62 52 65 73 20 3d 20 30  ..  int bRes = 0
48871 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
48872 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 6e 6f  LITE_OK;....  no
48873 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
48874 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65  , pCursor->pNode
48875 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c  , pCursor->iCell
48876 2c 20 26 63 65 6c 6c 29 3b 0d 0a 20 20 66 6f 72  , &cell);..  for
48877 28 69 69 3d 30 3b 20 62 52 65 73 3d 3d 30 20 26  (ii=0; bRes==0 &
48878 26 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43  & ii<pCursor->nC
48879 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
4887a 7b 0d 0a 20 20 20 20 52 74 72 65 65 43 6f 6e 73  {..    RtreeCons
4887b 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 75  traint *p = &pCu
4887c 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  rsor->aConstrain
4887d 74 5b 69 69 5d 3b 0d 0a 20 20 20 20 64 6f 75 62  t[ii];..    doub
4887e 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20 3d 20 44 43  le cell_min = DC
4887f 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64  OORD(cell.aCoord
48880 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29 2a  [(p->iCoord>>1)*
48881 32 5d 29 3b 0d 0a 20 20 20 20 64 6f 75 62 6c 65  2]);..    double
48882 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44 43 4f 4f   cell_max = DCOO
48883 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28  RD(cell.aCoord[(
48884 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 2b  p->iCoord>>1)*2+
48885 31 5d 29 3b 0d 0a 0d 0a 20 20 20 20 61 73 73 65  1]);....    asse
48886 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  rt(p->op==RTREE_
48887 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  LE || p->op==RTR
48888 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LT || p->op==
48889 52 54 52 45 45 5f 47 45 20 0d 0a 20 20 20 20 20  RTREE_GE ..     
4888a 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52     || p->op==RTR
4888b 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_GT || p->op==
4888c 52 54 52 45 45 5f 45 51 20 7c 7c 20 70 2d 3e 6f  RTREE_EQ || p->o
4888d 70 3d 3d 52 54 52 45 45 5f 4d 41 54 43 48 0d 0a  p==RTREE_MATCH..
4888e 20 20 20 20 29 3b 0d 0a 0d 0a 20 20 20 20 73 77      );....    sw
4888f 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0d 0a  itch( p->op ){..
48890 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45        case RTREE
48891 5f 4c 45 3a 20 63 61 73 65 20 52 54 52 45 45 5f  _LE: case RTREE_
48892 4c 54 3a 20 0d 0a 20 20 20 20 20 20 20 20 62 52  LT: ..        bR
48893 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3c 63  es = p->rValue<c
48894 65 6c 6c 5f 6d 69 6e 3b 20 0d 0a 20 20 20 20 20  ell_min; ..     
48895 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20     break;....   
48896 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47 45     case RTREE_GE
48897 3a 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a  : case RTREE_GT:
48898 20 0d 0a 20 20 20 20 20 20 20 20 62 52 65 73 20   ..        bRes 
48899 3d 20 70 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c  = p->rValue>cell
4889a 5f 6d 61 78 3b 20 0d 0a 20 20 20 20 20 20 20 20  _max; ..        
4889b 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20 20 20 20 20  break;....      
4889c 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a 0d 0a  case RTREE_EQ:..
4889d 20 20 20 20 20 20 20 20 62 52 65 73 20 3d 20 28          bRes = (
4889e 70 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d  p->rValue>cell_m
4889f 61 78 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c  ax || p->rValue<
488a0 63 65 6c 6c 5f 6d 69 6e 29 3b 0d 0a 20 20 20 20  cell_min);..    
488a1 20 20 20 20 62 72 65 61 6b 3b 0d 0a 0d 0a 20 20      break;....  
488a2 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0d 0a      default: {..
488a3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
488a4 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4d 41 54  p->op==RTREE_MAT
488a5 43 48 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 72  CH );..        r
488a6 63 20 3d 20 74 65 73 74 52 74 72 65 65 47 65 6f  c = testRtreeGeo
488a7 6d 28 70 52 74 72 65 65 2c 20 70 2c 20 26 63 65  m(pRtree, p, &ce
488a8 6c 6c 2c 20 26 62 52 65 73 29 3b 0d 0a 20 20 20  ll, &bRes);..   
488a9 20 20 20 20 20 62 52 65 73 20 3d 20 21 62 52 65       bRes = !bRe
488aa 73 3b 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61  s;..        brea
488ab 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  k;..      }..   
488ac 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 62   }..  }....  *pb
488ad 45 6f 66 20 3d 20 62 52 65 73 3b 0d 0a 20 20 72  Eof = bRes;..  r
488ae 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a  eturn rc;..}....
488af 2f 2a 20 0d 0a 2a 2a 20 54 65 73 74 20 69 66 20  /* ..** Test if 
488b0 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 63 75  the cell that cu
488b1 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72  rsor pCursor cur
488b2 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f  rently points to
488b3 0d 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 66 69  ..** would be fi
488b4 6c 74 65 72 65 64 20 28 65 78 63 6c 75 64 65 64  ltered (excluded
488b5 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61  ) by the constra
488b6 69 6e 74 73 20 69 6e 20 74 68 65 20 0d 0a 2a 2a  ints in the ..**
488b7 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74   pCursor->aConst
488b8 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 2e 20 49  raint[] array. I
488b9 66 20 73 6f 2c 20 73 65 74 20 2a 70 62 45 6f 66  f so, set *pbEof
488ba 20 74 6f 20 74 72 75 65 20 62 65 66 6f 72 65 0d   to true before.
488bb 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e 20 49  .** returning. I
488bc 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 6e 6f  f the cell is no
488bd 74 20 66 69 6c 74 65 72 65 64 20 28 65 78 63 6c  t filtered (excl
488be 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
488bf 73 74 72 61 69 6e 74 73 2c 0d 0a 2a 2a 20 73 65  straints,..** se
488c0 74 20 70 62 45 6f 66 20 74 6f 20 7a 65 72 6f 2e  t pbEof to zero.
488c1 0d 0a 2a 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20  ..**..** Return 
488c2 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
488c3 63 65 73 73 66 75 6c 20 6f 72 20 61 6e 20 53 51  cessful or an SQ
488c4 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
488c5 69 66 20 61 6e 20 65 72 72 6f 72 0d 0a 2a 2a 20  if an error..** 
488c6 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 20  occurs within a 
488c7 67 65 6f 6d 65 74 72 79 20 63 61 6c 6c 62 61 63  geometry callbac
488c8 6b 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20  k...**..** This 
488c9 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
488ca 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20 69   that the cell i
488cb 73 20 70 61 72 74 20 6f 66 20 61 20 6c 65 61 66  s part of a leaf
488cc 20 6e 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74   node...*/..stat
488cd 69 63 20 69 6e 74 20 74 65 73 74 52 74 72 65 65  ic int testRtree
488ce 45 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52 74  Entry(Rtree *pRt
488cf 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72  ree, RtreeCursor
488d0 20 2a 70 43 75 72 73 6f 72 2c 20 69 6e 74 20 2a   *pCursor, int *
488d1 70 62 45 6f 66 29 7b 0d 0a 20 20 52 74 72 65 65  pbEof){..  Rtree
488d2 43 65 6c 6c 20 63 65 6c 6c 3b 0d 0a 20 20 69 6e  Cell cell;..  in
488d3 74 20 69 69 3b 0d 0a 20 20 2a 70 62 45 6f 66 20  t ii;..  *pbEof 
488d4 3d 20 30 3b 0d 0a 0d 0a 20 20 6e 6f 64 65 47 65  = 0;....  nodeGe
488d5 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  tCell(pRtree, pC
488d6 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43  ursor->pNode, pC
488d7 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63  ursor->iCell, &c
488d8 65 6c 6c 29 3b 0d 0a 20 20 66 6f 72 28 69 69 3d  ell);..  for(ii=
488d9 30 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e 6e  0; ii<pCursor->n
488da 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b  Constraint; ii++
488db 29 7b 0d 0a 20 20 20 20 52 74 72 65 65 43 6f 6e  ){..    RtreeCon
488dc 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43  straint *p = &pC
488dd 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
488de 6e 74 5b 69 69 5d 3b 0d 0a 20 20 20 20 64 6f 75  nt[ii];..    dou
488df 62 6c 65 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f  ble coord = DCOO
488e0 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70  RD(cell.aCoord[p
488e1 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0d 0a 20 20 20  ->iCoord]);..   
488e2 20 69 6e 74 20 72 65 73 3b 0d 0a 20 20 20 20 61   int res;..    a
488e3 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52  ssert(p->op==RTR
488e4 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LE || p->op==
488e5 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f  RTREE_LT || p->o
488e6 70 3d 3d 52 54 52 45 45 5f 47 45 20 0d 0a 20 20  p==RTREE_GE ..  
488e7 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d        || p->op==
488e8 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f  RTREE_GT || p->o
488e9 70 3d 3d 52 54 52 45 45 5f 45 51 20 7c 7c 20 70  p==RTREE_EQ || p
488ea 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4d 41 54 43  ->op==RTREE_MATC
488eb 48 0d 0a 20 20 20 20 29 3b 0d 0a 20 20 20 20 73  H..    );..    s
488ec 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0d  witch( p->op ){.
488ed 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
488ee 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_LE: res = (coo
488ef 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20  rd<=p->rValue); 
488f0 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61  break;..      ca
488f1 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72 65 73  se RTREE_LT: res
488f2 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61   = (coord<p->rVa
488f3 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0d 0a 20  lue);  break;.. 
488f4 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
488f5 47 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  GE: res = (coord
488f6 3e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72  >=p->rValue); br
488f7 65 61 6b 3b 0d 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
488f8 20 52 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d   RTREE_GT: res =
488f9 20 28 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75   (coord>p->rValu
488fa 65 29 3b 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20  e);  break;..   
488fb 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 45 51     case RTREE_EQ
488fc 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d  : res = (coord==
488fd 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61  p->rValue); brea
488fe 6b 3b 0d 0a 20 20 20 20 20 20 64 65 66 61 75 6c  k;..      defaul
488ff 74 3a 20 7b 0d 0a 20 20 20 20 20 20 20 20 69 6e  t: {..        in
48900 74 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 61  t rc;..        a
48901 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 52 54  ssert( p->op==RT
48902 52 45 45 5f 4d 41 54 43 48 20 29 3b 0d 0a 20 20  REE_MATCH );..  
48903 20 20 20 20 20 20 72 63 20 3d 20 74 65 73 74 52        rc = testR
48904 74 72 65 65 47 65 6f 6d 28 70 52 74 72 65 65 2c  treeGeom(pRtree,
48905 20 70 2c 20 26 63 65 6c 6c 2c 20 26 72 65 73 29   p, &cell, &res)
48906 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
48907 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c!=SQLITE_OK ){.
48908 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
48909 6e 20 72 63 3b 0d 0a 20 20 20 20 20 20 20 20 7d  n rc;..        }
4890a 0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ..        break;
4890b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
4890c 0d 0a 0d 0a 20 20 20 20 69 66 28 20 21 72 65 73  ....    if( !res
4890d 20 29 7b 0d 0a 20 20 20 20 20 20 2a 70 62 45 6f   ){..      *pbEo
4890e 66 20 3d 20 31 3b 0d 0a 20 20 20 20 20 20 72 65  f = 1;..      re
4890f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
48910 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
48911 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
48912 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
48913 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  Cursor pCursor c
48914 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
48915 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20 68  at a node that h
48916 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65 20  eads a sub-tree 
48917 6f 66 0d 0a 2a 2a 20 68 65 69 67 68 74 20 69 48  of..** height iH
48918 65 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68  eight (if iHeigh
48919 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e  t==0, then the n
4891a 6f 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20  ode is a leaf). 
4891b 44 65 73 63 65 6e 64 0d 0a 2a 2a 20 74 6f 20 70  Descend..** to p
4891c 6f 69 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74  oint to the left
4891d 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68  -most cell of th
4891e 65 20 73 75 62 2d 74 72 65 65 20 74 68 61 74 20  e sub-tree that 
4891f 6d 61 74 63 68 65 73 20 74 68 65 20 0d 0a 2a 2a  matches the ..**
48920 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73   configured cons
48921 74 72 61 69 6e 74 73 2e 0d 0a 2a 2f 0d 0a 73 74  traints...*/..st
48922 61 74 69 63 20 69 6e 74 20 64 65 73 63 65 6e 64  atic int descend
48923 54 6f 43 65 6c 6c 28 0d 0a 20 20 52 74 72 65 65  ToCell(..  Rtree
48924 20 2a 70 52 74 72 65 65 2c 20 0d 0a 20 20 52 74   *pRtree, ..  Rt
48925 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73  reeCursor *pCurs
48926 6f 72 2c 20 0d 0a 20 20 69 6e 74 20 69 48 65 69  or, ..  int iHei
48927 67 68 74 2c 0d 0a 20 20 69 6e 74 20 2a 70 45 6f  ght,..  int *pEo
48928 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
48929 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f    /* OUT: Set to
4892a 20 74 72 75 65 20 69 66 20 63 61 6e 6e 6f 74 20   true if cannot 
4892b 64 65 73 63 65 6e 64 20 2a 2f 0d 0a 29 7b 0d 0a  descend */..){..
4892c 20 20 69 6e 74 20 69 73 45 6f 66 3b 0d 0a 20 20    int isEof;..  
4892d 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74 20 69  int rc;..  int i
4892e 69 3b 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  i;..  RtreeNode 
4892f 2a 70 43 68 69 6c 64 3b 0d 0a 20 20 73 71 6c 69  *pChild;..  sqli
48930 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64  te3_int64 iRowid
48931 3b 0d 0a 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65  ;....  RtreeNode
48932 20 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70   *pSavedNode = p
48933 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0d 0a  Cursor->pNode;..
48934 20 20 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c    int iSavedCell
48935 20 3d 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c   = pCursor->iCel
48936 6c 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 20  l;....  assert( 
48937 69 48 65 69 67 68 74 3e 3d 30 20 29 3b 0d 0a 0d  iHeight>=0 );...
48938 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d  .  if( iHeight==
48939 30 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 74  0 ){..    rc = t
4893a 65 73 74 52 74 72 65 65 45 6e 74 72 79 28 70 52  estRtreeEntry(pR
4893b 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c 20 26  tree, pCursor, &
4893c 69 73 45 6f 66 29 3b 0d 0a 20 20 7d 65 6c 73 65  isEof);..  }else
4893d 7b 0d 0a 20 20 20 20 72 63 20 3d 20 74 65 73 74  {..    rc = test
4893e 52 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65 65  RtreeCell(pRtree
4893f 2c 20 70 43 75 72 73 6f 72 2c 20 26 69 73 45 6f  , pCursor, &isEo
48940 66 29 3b 0d 0a 20 20 7d 0d 0a 20 20 69 66 28 20  f);..  }..  if( 
48941 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
48942 20 69 73 45 6f 66 20 7c 7c 20 69 48 65 69 67 68   isEof || iHeigh
48943 74 3d 3d 30 20 29 7b 0d 0a 20 20 20 20 67 6f 74  t==0 ){..    got
48944 6f 20 64 65 73 63 65 6e 64 5f 74 6f 5f 63 65 6c  o descend_to_cel
48945 6c 5f 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  l_out;..  }.... 
48946 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65   iRowid = nodeGe
48947 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
48948 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70  Cursor->pNode, p
48949 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29 3b 0d  Cursor->iCell);.
4894a 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75  .  rc = nodeAcqu
4894b 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f 77  ire(pRtree, iRow
4894c 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f  id, pCursor->pNo
4894d 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0d 0a 20  de, &pChild);.. 
4894e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
4894f 4f 4b 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20  OK ){..    goto 
48950 64 65 73 63 65 6e 64 5f 74 6f 5f 63 65 6c 6c 5f  descend_to_cell_
48951 6f 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 6e  out;..  }....  n
48952 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
48953 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64  e, pCursor->pNod
48954 65 29 3b 0d 0a 20 20 70 43 75 72 73 6f 72 2d 3e  e);..  pCursor->
48955 70 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64 3b 0d  pNode = pChild;.
48956 0a 20 20 69 73 45 6f 66 20 3d 20 31 3b 0d 0a 20  .  isEof = 1;.. 
48957 20 66 6f 72 28 69 69 3d 30 3b 20 69 73 45 6f 66   for(ii=0; isEof
48958 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28 70 43 68   && ii<NCELL(pCh
48959 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20  ild); ii++){..  
4895a 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c    pCursor->iCell
4895b 20 3d 20 69 69 3b 0d 0a 20 20 20 20 72 63 20 3d   = ii;..    rc =
4895c 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70   descendToCell(p
4895d 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c 20  Rtree, pCursor, 
4895e 69 48 65 69 67 68 74 2d 31 2c 20 26 69 73 45 6f  iHeight-1, &isEo
4895f 66 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 21  f);..    if( rc!
48960 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
48961 20 20 20 20 20 67 6f 74 6f 20 64 65 73 63 65 6e       goto descen
48962 64 5f 74 6f 5f 63 65 6c 6c 5f 6f 75 74 3b 0d 0a  d_to_cell_out;..
48963 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
48964 69 66 28 20 69 73 45 6f 66 20 29 7b 0d 0a 20 20  if( isEof ){..  
48965 20 20 61 73 73 65 72 74 28 20 70 43 75 72 73 6f    assert( pCurso
48966 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69 6c 64  r->pNode==pChild
48967 20 29 3b 0d 0a 20 20 20 20 6e 6f 64 65 52 65 66   );..    nodeRef
48968 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f 64  erence(pSavedNod
48969 65 29 3b 0d 0a 20 20 20 20 6e 6f 64 65 52 65 6c  e);..    nodeRel
4896a 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68  ease(pRtree, pCh
4896b 69 6c 64 29 3b 0d 0a 20 20 20 20 70 43 75 72 73  ild);..    pCurs
4896c 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76  or->pNode = pSav
4896d 65 64 4e 6f 64 65 3b 0d 0a 20 20 20 20 70 43 75  edNode;..    pCu
4896e 72 73 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53  rsor->iCell = iS
4896f 61 76 65 64 43 65 6c 6c 3b 0d 0a 20 20 7d 0d 0a  avedCell;..  }..
48970 0d 0a 64 65 73 63 65 6e 64 5f 74 6f 5f 63 65 6c  ..descend_to_cel
48971 6c 5f 6f 75 74 3a 0d 0a 20 20 2a 70 45 6f 66 20  l_out:..  *pEof 
48972 3d 20 69 73 45 6f 66 3b 0d 0a 20 20 72 65 74 75  = isEof;..  retu
48973 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
48974 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 63  .** One of the c
48975 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f  ells in node pNo
48976 64 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  de is guaranteed
48977 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d 62 69   to have a 64-bi
48978 74 20 0d 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76  t ..** integer v
48979 61 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69 52  alue equal to iR
4897a 6f 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68 65  owid. Return the
4897b 20 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20 63   index of this c
4897c 65 6c 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ell...*/..static
4897d 20 69 6e 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e   int nodeRowidIn
4897e 64 65 78 28 0d 0a 20 20 52 74 72 65 65 20 2a 70  dex(..  Rtree *p
4897f 52 74 72 65 65 2c 20 0d 0a 20 20 52 74 72 65 65  Rtree, ..  Rtree
48980 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0d 0a 20  Node *pNode, .. 
48981 20 69 36 34 20 69 52 6f 77 69 64 2c 0d 0a 20 20   i64 iRowid,..  
48982 69 6e 74 20 2a 70 69 49 6e 64 65 78 0d 0a 29 7b  int *piIndex..){
48983 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 69  ..  int ii;..  i
48984 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  nt nCell = NCELL
48985 28 70 4e 6f 64 65 29 3b 0d 0a 20 20 66 6f 72 28  (pNode);..  for(
48986 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20  ii=0; ii<nCell; 
48987 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 66 28 20  ii++){..    if( 
48988 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
48989 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 29 3d  ree, pNode, ii)=
4898a 3d 69 52 6f 77 69 64 20 29 7b 0d 0a 20 20 20 20  =iRowid ){..    
4898b 20 20 2a 70 69 49 6e 64 65 78 20 3d 20 69 69 3b    *piIndex = ii;
4898c 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
4898d 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d  QLITE_OK;..    }
4898e 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20  ..  }..  return 
4898f 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 56  SQLITE_CORRUPT_V
48990 54 41 42 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  TAB;..}..../*..*
48991 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
48992 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ex of the cell c
48993 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
48994 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  ter to node pNod
48995 65 0d 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61 72  e..** in its par
48996 65 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69 73  ent. If pNode is
48997 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c 20   the root node, 
48998 72 65 74 75 72 6e 20 2d 31 2e 0d 0a 2a 2f 0d 0a  return -1...*/..
48999 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 50  static int nodeP
4899a 61 72 65 6e 74 49 6e 64 65 78 28 52 74 72 65 65  arentIndex(Rtree
4899b 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
4899c 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20  ode *pNode, int 
4899d 2a 70 69 49 6e 64 65 78 29 7b 0d 0a 20 20 52 74  *piIndex){..  Rt
4899e 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
4899f 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
489a0 74 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 65 6e  t;..  if( pParen
489a1 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e  t ){..    return
489a2 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
489a3 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
489a4 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20 70   pNode->iNode, p
489a5 69 49 6e 64 65 78 29 3b 0d 0a 20 20 7d 0d 0a 20  iIndex);..  }.. 
489a6 20 2a 70 69 49 6e 64 65 78 20 3d 20 2d 31 3b 0d   *piIndex = -1;.
489a7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
489a8 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d 0a  _OK;..}..../* ..
489a9 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
489aa 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 4e   table module xN
489ab 65 78 74 20 6d 65 74 68 6f 64 2e 0d 0a 2a 2f 0d  ext method...*/.
489ac 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
489ad 65 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74  eNext(sqlite3_vt
489ae 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
489af 43 75 72 73 6f 72 29 7b 0d 0a 20 20 52 74 72 65  Cursor){..  Rtre
489b0 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
489b1 65 65 20 2a 29 28 70 56 74 61 62 43 75 72 73 6f  ee *)(pVtabCurso
489b2 72 2d 3e 70 56 74 61 62 29 3b 0d 0a 20 20 52 74  r->pVtab);..  Rt
489b3 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20  reeCursor *pCsr 
489b4 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a  = (RtreeCursor *
489b5 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0d 0a 20  )pVtabCursor;.. 
489b6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
489b7 5f 4f 4b 3b 0d 0a 0d 0a 20 20 2f 2a 20 52 74 72  _OK;....  /* Rtr
489b8 65 65 43 75 72 73 6f 72 2e 70 4e 6f 64 65 20 6d  eeCursor.pNode m
489b9 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e  ust not be NULL.
489ba 20 49 66 20 69 73 20 69 73 20 4e 55 4c 4c 2c 20   If is is NULL, 
489bb 74 68 65 6e 20 74 68 69 73 20 63 75 72 73 6f 72  then this cursor
489bc 20 69 73 0d 0a 20 20 2a 2a 20 61 6c 72 65 61 64   is..  ** alread
489bd 79 20 61 74 20 45 4f 46 2e 20 49 74 20 69 73 20  y at EOF. It is 
489be 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c 65  against the rule
489bf 73 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 4e  s to call the xN
489c0 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 0d  ext() method of.
489c1 0a 20 20 2a 2a 20 61 20 63 75 72 73 6f 72 20 74  .  ** a cursor t
489c2 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  hat has already 
489c3 72 65 61 63 68 65 64 20 45 4f 46 2e 0d 0a 20 20  reached EOF...  
489c4 2a 2f 0d 0a 20 20 61 73 73 65 72 74 28 20 70 43  */..  assert( pC
489c5 73 72 2d 3e 70 4e 6f 64 65 20 29 3b 0d 0a 0d 0a  sr->pNode );....
489c6 20 20 69 66 28 20 70 43 73 72 2d 3e 69 53 74 72    if( pCsr->iStr
489c7 61 74 65 67 79 3d 3d 31 20 29 7b 0d 0a 20 20 20  ategy==1 ){..   
489c8 20 2f 2a 20 54 68 69 73 20 22 73 63 61 6e 22 20   /* This "scan" 
489c9 69 73 20 61 20 64 69 72 65 63 74 20 6c 6f 6f 6b  is a direct look
489ca 75 70 20 62 79 20 72 6f 77 69 64 2e 20 54 68 65  up by rowid. The
489cb 72 65 20 69 73 20 6e 6f 20 6e 65 78 74 20 65 6e  re is no next en
489cc 74 72 79 2e 20 2a 2f 0d 0a 20 20 20 20 6e 6f 64  try. */..    nod
489cd 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
489ce 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0d 0a   pCsr->pNode);..
489cf 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
489d0 3d 20 30 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  = 0;..  }else{..
489d1 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 6f 20 74      /* Move to t
489d2 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 74 68  he next entry th
489d3 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 63  at matches the c
489d4 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72  onfigured constr
489d5 61 69 6e 74 73 2e 20 2a 2f 0d 0a 20 20 20 20 69  aints. */..    i
489d6 6e 74 20 69 48 65 69 67 68 74 20 3d 20 30 3b 0d  nt iHeight = 0;.
489d7 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 73 72  .    while( pCsr
489d8 2d 3e 70 4e 6f 64 65 20 29 7b 0d 0a 20 20 20 20  ->pNode ){..    
489d9 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
489da 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e 6f 64 65  de = pCsr->pNode
489db 3b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 65  ;..      int nCe
489dc 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
489dd 29 3b 0d 0a 20 20 20 20 20 20 66 6f 72 28 70 43  );..      for(pC
489de 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43 73  sr->iCell++; pCs
489df 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  r->iCell<nCell; 
489e0 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0d  pCsr->iCell++){.
489e1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 45  .        int isE
489e2 6f 66 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20  of;..        rc 
489e3 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28  = descendToCell(
489e4 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69 48  pRtree, pCsr, iH
489e5 65 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b 0d  eight, &isEof);.
489e6 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
489e7 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69  =SQLITE_OK || !i
489e8 73 45 6f 66 20 29 7b 0d 0a 20 20 20 20 20 20 20  sEof ){..       
489e9 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20     return rc;.. 
489ea 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
489eb 7d 0d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70  }..      pCsr->p
489ec 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  Node = pNode->pP
489ed 61 72 65 6e 74 3b 0d 0a 20 20 20 20 20 20 72 63  arent;..      rc
489ee 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
489ef 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
489f0 2c 20 26 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  , &pCsr->iCell);
489f1 0d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
489f2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
489f3 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
489f4 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
489f5 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70   nodeReference(p
489f6 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0d 0a 20 20  Csr->pNode);..  
489f7 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
489f8 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0d  pRtree, pNode);.
489f9 0a 20 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b  .      iHeight++
489fa 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
489fb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
489fc 0d 0a 0d 0a 2f 2a 20 0d 0a 2a 2a 20 52 74 72 65  ..../* ..** Rtre
489fd 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
489fe 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20 6d 65  module xRowid me
489ff 74 68 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  thod...*/..stati
48a00 63 20 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64  c int rtreeRowid
48a01 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
48a02 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
48a03 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  r, sqlite_int64 
48a04 2a 70 52 6f 77 69 64 29 7b 0d 0a 20 20 52 74 72  *pRowid){..  Rtr
48a05 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
48a06 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73 6f  ree *)pVtabCurso
48a07 72 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 52 74 72  r->pVtab;..  Rtr
48a08 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
48a09 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
48a0a 70 56 74 61 62 43 75 72 73 6f 72 3b 0d 0a 0d 0a  pVtabCursor;....
48a0b 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e 70    assert(pCsr->p
48a0c 4e 6f 64 65 29 3b 0d 0a 20 20 2a 70 52 6f 77 69  Node);..  *pRowi
48a0d 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
48a0e 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  (pRtree, pCsr->p
48a0f 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c  Node, pCsr->iCel
48a10 6c 29 3b 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20  l);....  return 
48a11 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d  SQLITE_OK;..}...
48a12 0a 2f 2a 20 0d 0a 2a 2a 20 52 74 72 65 65 20 76  ./* ..** Rtree v
48a13 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
48a14 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68  ule xColumn meth
48a15 6f 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  od...*/..static 
48a16 69 6e 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e 28  int rtreeColumn(
48a17 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
48a18 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c 69 74 65  sor *cur, sqlite
48a19 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
48a1a 69 6e 74 20 69 29 7b 0d 0a 20 20 52 74 72 65 65  int i){..  Rtree
48a1b 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
48a1c 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62 3b 0d  e *)cur->pVtab;.
48a1d 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
48a1e 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
48a1f 73 6f 72 20 2a 29 63 75 72 3b 0d 0a 0d 0a 20 20  sor *)cur;....  
48a20 69 66 28 20 69 3d 3d 30 20 29 7b 0d 0a 20 20 20  if( i==0 ){..   
48a21 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f   i64 iRowid = no
48a22 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
48a23 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20  e, pCsr->pNode, 
48a24 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0d 0a 20  pCsr->iCell);.. 
48a25 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
48a26 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69 52 6f  t_int64(ctx, iRo
48a27 77 69 64 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d  wid);..  }else{.
48a28 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
48a29 63 3b 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74 43  c;..    nodeGetC
48a2a 6f 6f 72 64 28 70 52 74 72 65 65 2c 20 70 43 73  oord(pRtree, pCs
48a2b 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e  r->pNode, pCsr->
48a2c 69 43 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b  iCell, i-1, &c);
48a2d 0d 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  ..    if( pRtree
48a2e 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
48a2f 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
48a30 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
48a31 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
48a32 28 63 74 78 2c 20 63 2e 66 29 3b 0d 0a 20 20 20  (ctx, c.f);..   
48a33 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 61   }else{..      a
48a34 73 73 65 72 74 28 20 70 52 74 72 65 65 2d 3e 65  ssert( pRtree->e
48a35 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
48a36 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 20 29 3b 0d  _COORD_INT32 );.
48a37 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
48a38 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 63  esult_int(ctx, c
48a39 2e 69 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  .i);..    }..  }
48a3a 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ....  return SQL
48a3b 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ITE_OK;..}..../*
48a3c 20 0d 0a 2a 2a 20 55 73 65 20 6e 6f 64 65 41 63   ..** Use nodeAc
48a3d 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74 61 69  quire() to obtai
48a3e 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20  n the leaf node 
48a3f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
48a40 65 63 6f 72 64 20 77 69 74 68 20 0d 0a 2a 2a 20  ecord with ..** 
48a41 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49 66  rowid iRowid. If
48a42 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65 74   successful, set
48a43 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69 6e   *ppLeaf to poin
48a44 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61 6e  t to the node an
48a45 64 0d 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  d..** return SQL
48a46 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65  ITE_OK. If there
48a47 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f   is no such reco
48a48 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  rd in the table,
48a49 20 73 65 74 0d 0a 2a 2a 20 2a 70 70 4c 65 61 66   set..** *ppLeaf
48a4a 20 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e   to 0 and return
48a4b 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61   SQLITE_OK. If a
48a4c 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
48a4d 73 65 74 20 2a 70 70 4c 65 61 66 0d 0a 2a 2a 20  set *ppLeaf..** 
48a4e 74 6f 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75  to zero and retu
48a4f 72 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  rn an SQLite err
48a50 6f 72 20 63 6f 64 65 2e 0d 0a 2a 2f 0d 0a 73 74  or code...*/..st
48a51 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 65 61  atic int findLea
48a52 66 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74  fNode(Rtree *pRt
48a53 72 65 65 2c 20 69 36 34 20 69 52 6f 77 69 64 2c  ree, i64 iRowid,
48a54 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c   RtreeNode **ppL
48a55 65 61 66 29 7b 0d 0a 20 20 69 6e 74 20 72 63 3b  eaf){..  int rc;
48a56 0d 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 30 3b  ..  *ppLeaf = 0;
48a57 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
48a58 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
48a59 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 69 52  ReadRowid, 1, iR
48a5a 6f 77 69 64 29 3b 0d 0a 20 20 69 66 28 20 73 71  owid);..  if( sq
48a5b 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
48a5c 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3d 3d  e->pReadRowid)==
48a5d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a 20  SQLITE_ROW ){.. 
48a5e 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73     i64 iNode = s
48a5f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
48a60 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
48a61 64 52 6f 77 69 64 2c 20 30 29 3b 0d 0a 20 20 20  dRowid, 0);..   
48a62 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72   rc = nodeAcquir
48a63 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c  e(pRtree, iNode,
48a64 20 30 2c 20 70 70 4c 65 61 66 29 3b 0d 0a 20 20   0, ppLeaf);..  
48a65 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
48a66 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
48a67 69 64 29 3b 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a  id);..  }else{..
48a68 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
48a69 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
48a6a 52 65 61 64 52 6f 77 69 64 29 3b 0d 0a 20 20 7d  ReadRowid);..  }
48a6b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
48a6c 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73  }..../*..** This
48a6d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
48a6e 6c 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65  led to configure
48a6f 20 74 68 65 20 52 74 72 65 65 43 6f 6e 73 74 72   the RtreeConstr
48a70 61 69 6e 74 20 6f 62 6a 65 63 74 20 70 61 73 73  aint object pass
48a71 65 64 0d 0a 2a 2a 20 61 73 20 74 68 65 20 73 65  ed..** as the se
48a72 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 6f  cond argument fo
48a73 72 20 61 20 4d 41 54 43 48 20 63 6f 6e 73 74 72  r a MATCH constr
48a74 61 69 6e 74 2e 20 54 68 65 20 76 61 6c 75 65 20  aint. The value 
48a75 70 61 73 73 65 64 20 61 73 20 74 68 65 0d 0a 2a  passed as the..*
48a76 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * first argument
48a77 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
48a78 6e 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 68  n is the right-h
48a79 61 6e 64 20 6f 70 65 72 61 6e 64 20 74 6f 20 74  and operand to t
48a7a 68 65 20 4d 41 54 43 48 0d 0a 2a 2a 20 6f 70 65  he MATCH..** ope
48a7b 72 61 74 6f 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  rator...*/..stat
48a7c 69 63 20 69 6e 74 20 64 65 73 65 72 69 61 6c 69  ic int deseriali
48a7d 7a 65 47 65 6f 6d 65 74 72 79 28 73 71 6c 69 74  zeGeometry(sqlit
48a7e 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
48a7f 2c 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e  , RtreeConstrain
48a80 74 20 2a 70 43 6f 6e 73 29 7b 0d 0a 20 20 52 74  t *pCons){..  Rt
48a81 72 65 65 4d 61 74 63 68 41 72 67 20 2a 70 3b 0d  reeMatchArg *p;.
48a82 0a 20 20 73 71 6c 69 74 65 33 5f 72 74 72 65 65  .  sqlite3_rtree
48a83 5f 67 65 6f 6d 65 74 72 79 20 2a 70 47 65 6f 6d  _geometry *pGeom
48a84 3b 0d 0a 20 20 69 6e 74 20 6e 42 6c 6f 62 3b 0d  ;..  int nBlob;.
48a85 0a 0d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ...  /* Check th
48a86 61 74 20 76 61 6c 75 65 20 69 73 20 61 63 74 75  at value is actu
48a87 61 6c 6c 79 20 61 20 62 6c 6f 62 2e 20 2a 2f 0d  ally a blob. */.
48a88 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
48a89 61 6c 75 65 5f 74 79 70 65 28 70 56 61 6c 75 65  alue_type(pValue
48a8a 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )!=SQLITE_BLOB )
48a8b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
48a8c 52 52 4f 52 3b 0d 0a 0d 0a 20 20 2f 2a 20 43 68  RROR;....  /* Ch
48a8d 65 63 6b 20 74 68 61 74 20 74 68 65 20 62 6c 6f  eck that the blo
48a8e 62 20 69 73 20 72 6f 75 67 68 6c 79 20 74 68 65  b is roughly the
48a8f 20 72 69 67 68 74 20 73 69 7a 65 2e 20 2a 2f 0d   right size. */.
48a90 0a 20 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74  .  nBlob = sqlit
48a91 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 70  e3_value_bytes(p
48a92 56 61 6c 75 65 29 3b 0d 0a 20 20 69 66 28 20 6e  Value);..  if( n
48a93 42 6c 6f 62 3c 28 69 6e 74 29 73 69 7a 65 6f 66  Blob<(int)sizeof
48a94 28 52 74 72 65 65 4d 61 74 63 68 41 72 67 29 20  (RtreeMatchArg) 
48a95 0d 0a 20 20 20 7c 7c 20 28 28 6e 42 6c 6f 62 2d  ..   || ((nBlob-
48a96 73 69 7a 65 6f 66 28 52 74 72 65 65 4d 61 74 63  sizeof(RtreeMatc
48a97 68 41 72 67 29 29 25 73 69 7a 65 6f 66 28 64 6f  hArg))%sizeof(do
48a98 75 62 6c 65 29 29 21 3d 30 0d 0a 20 20 29 7b 0d  uble))!=0..  ){.
48a99 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
48a9a 54 45 5f 45 52 52 4f 52 3b 0d 0a 20 20 7d 0d 0a  TE_ERROR;..  }..
48a9b 0d 0a 20 20 70 47 65 6f 6d 20 3d 20 28 73 71 6c  ..  pGeom = (sql
48a9c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65  ite3_rtree_geome
48a9d 74 72 79 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  try *)sqlite3_ma
48a9e 6c 6c 6f 63 28 0d 0a 20 20 20 20 20 20 73 69 7a  lloc(..      siz
48a9f 65 6f 66 28 73 71 6c 69 74 65 33 5f 72 74 72 65  eof(sqlite3_rtre
48aa0 65 5f 67 65 6f 6d 65 74 72 79 29 20 2b 20 6e 42  e_geometry) + nB
48aa1 6c 6f 62 0d 0a 20 20 29 3b 0d 0a 20 20 69 66 28  lob..  );..  if(
48aa2 20 21 70 47 65 6f 6d 20 29 20 72 65 74 75 72 6e   !pGeom ) return
48aa3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a   SQLITE_NOMEM;..
48aa4 20 20 6d 65 6d 73 65 74 28 70 47 65 6f 6d 2c 20    memset(pGeom, 
48aa5 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  0, sizeof(sqlite
48aa6 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
48aa7 29 29 3b 0d 0a 20 20 70 20 3d 20 28 52 74 72 65  ));..  p = (Rtre
48aa8 65 4d 61 74 63 68 41 72 67 20 2a 29 26 70 47 65  eMatchArg *)&pGe
48aa9 6f 6d 5b 31 5d 3b 0d 0a 0d 0a 20 20 6d 65 6d 63  om[1];....  memc
48aaa 70 79 28 70 2c 20 73 71 6c 69 74 65 33 5f 76 61  py(p, sqlite3_va
48aab 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c 75 65 29  lue_blob(pValue)
48aac 2c 20 6e 42 6c 6f 62 29 3b 0d 0a 20 20 69 66 28  , nBlob);..  if(
48aad 20 70 2d 3e 6d 61 67 69 63 21 3d 52 54 52 45 45   p->magic!=RTREE
48aae 5f 47 45 4f 4d 45 54 52 59 5f 4d 41 47 49 43 20  _GEOMETRY_MAGIC 
48aaf 0d 0a 20 20 20 7c 7c 20 6e 42 6c 6f 62 21 3d 28  ..   || nBlob!=(
48ab0 69 6e 74 29 28 73 69 7a 65 6f 66 28 52 74 72 65  int)(sizeof(Rtre
48ab1 65 4d 61 74 63 68 41 72 67 29 20 2b 20 28 70 2d  eMatchArg) + (p-
48ab2 3e 6e 50 61 72 61 6d 2d 31 29 2a 73 69 7a 65 6f  >nParam-1)*sizeo
48ab3 66 28 64 6f 75 62 6c 65 29 29 0d 0a 20 20 29 7b  f(double))..  ){
48ab4 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
48ab5 65 65 28 70 47 65 6f 6d 29 3b 0d 0a 20 20 20 20  ee(pGeom);..    
48ab6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
48ab7 52 4f 52 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 70  ROR;..  }....  p
48ab8 47 65 6f 6d 2d 3e 70 43 6f 6e 74 65 78 74 20 3d  Geom->pContext =
48ab9 20 70 2d 3e 70 43 6f 6e 74 65 78 74 3b 0d 0a 20   p->pContext;.. 
48aba 20 70 47 65 6f 6d 2d 3e 6e 50 61 72 61 6d 20 3d   pGeom->nParam =
48abb 20 70 2d 3e 6e 50 61 72 61 6d 3b 0d 0a 20 20 70   p->nParam;..  p
48abc 47 65 6f 6d 2d 3e 61 50 61 72 61 6d 20 3d 20 70  Geom->aParam = p
48abd 2d 3e 61 50 61 72 61 6d 3b 0d 0a 0d 0a 20 20 70  ->aParam;....  p
48abe 43 6f 6e 73 2d 3e 78 47 65 6f 6d 20 3d 20 70 2d  Cons->xGeom = p-
48abf 3e 78 47 65 6f 6d 3b 0d 0a 20 20 70 43 6f 6e 73  >xGeom;..  pCons
48ac0 2d 3e 70 47 65 6f 6d 20 3d 20 70 47 65 6f 6d 3b  ->pGeom = pGeom;
48ac1 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
48ac2 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 20 0d  E_OK;..}..../* .
48ac3 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
48ac4 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
48ac5 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 0d 0a  Filter method...
48ac6 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 72  */..static int r
48ac7 74 72 65 65 46 69 6c 74 65 72 28 0d 0a 20 20 73  treeFilter(..  s
48ac8 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
48ac9 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c  or *pVtabCursor,
48aca 20 0d 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c   ..  int idxNum,
48acb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78   const char *idx
48acc 53 74 72 2c 0d 0a 20 20 69 6e 74 20 61 72 67 63  Str,..  int argc
48acd 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
48ace 2a 2a 61 72 67 76 0d 0a 29 7b 0d 0a 20 20 52 74  **argv..){..  Rt
48acf 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
48ad0 74 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73  tree *)pVtabCurs
48ad1 6f 72 2d 3e 70 56 74 61 62 3b 0d 0a 20 20 52 74  or->pVtab;..  Rt
48ad2 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20  reeCursor *pCsr 
48ad3 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a  = (RtreeCursor *
48ad4 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0d 0a 0d  )pVtabCursor;...
48ad5 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
48ad6 6f 6f 74 20 3d 20 30 3b 0d 0a 20 20 69 6e 74 20  oot = 0;..  int 
48ad7 69 69 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  ii;..  int rc = 
48ad8 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20  SQLITE_OK;....  
48ad9 72 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70  rtreeReference(p
48ada 52 74 72 65 65 29 3b 0d 0a 0d 0a 20 20 66 72 65  Rtree);....  fre
48adb 65 43 75 72 73 6f 72 43 6f 6e 73 74 72 61 69 6e  eCursorConstrain
48adc 74 73 28 70 43 73 72 29 3b 0d 0a 20 20 70 43 73  ts(pCsr);..  pCs
48add 72 2d 3e 69 53 74 72 61 74 65 67 79 20 3d 20 69  r->iStrategy = i
48ade 64 78 4e 75 6d 3b 0d 0a 0d 0a 20 20 69 66 28 20  dxNum;....  if( 
48adf 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0d 0a 20 20  idxNum==1 ){..  
48ae0 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
48ae1 65 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  e - lookup by ro
48ae2 77 69 64 2e 20 2a 2f 0d 0a 20 20 20 20 52 74 72  wid. */..    Rtr
48ae3 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20  eeNode *pLeaf;  
48ae4 20 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e        /* Leaf on
48ae5 20 77 68 69 63 68 20 74 68 65 20 72 65 71 75 69   which the requi
48ae6 72 65 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73  red cell resides
48ae7 20 2a 2f 0d 0a 20 20 20 20 69 36 34 20 69 52 6f   */..    i64 iRo
48ae8 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  wid = sqlite3_va
48ae9 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
48aea 5d 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 66 69  ]);..    rc = fi
48aeb 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52 74 72 65  ndLeafNode(pRtre
48aec 65 2c 20 69 52 6f 77 69 64 2c 20 26 70 4c 65 61  e, iRowid, &pLea
48aed 66 29 3b 0d 0a 20 20 20 20 70 43 73 72 2d 3e 70  f);..    pCsr->p
48aee 4e 6f 64 65 20 3d 20 70 4c 65 61 66 3b 20 0d 0a  Node = pLeaf; ..
48aef 20 20 20 20 69 66 28 20 70 4c 65 61 66 20 29 7b      if( pLeaf ){
48af0 0d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
48af1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
48af2 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64  ..      rc = nod
48af3 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72  eRowidIndex(pRtr
48af4 65 65 2c 20 70 4c 65 61 66 2c 20 69 52 6f 77 69  ee, pLeaf, iRowi
48af5 64 2c 20 26 70 43 73 72 2d 3e 69 43 65 6c 6c 29  d, &pCsr->iCell)
48af6 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73  ;..    }..  }els
48af7 65 7b 0d 0a 20 20 20 20 2f 2a 20 4e 6f 72 6d 61  e{..    /* Norma
48af8 6c 20 63 61 73 65 20 2d 20 72 2d 74 72 65 65 20  l case - r-tree 
48af9 73 63 61 6e 2e 20 53 65 74 20 75 70 20 74 68 65  scan. Set up the
48afa 20 52 74 72 65 65 43 75 72 73 6f 72 2e 61 43 6f   RtreeCursor.aCo
48afb 6e 73 74 72 61 69 6e 74 20 61 72 72 61 79 20 0d  nstraint array .
48afc 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65  .    ** with the
48afd 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73   configured cons
48afe 74 72 61 69 6e 74 73 2e 20 0d 0a 20 20 20 20 2a  traints. ..    *
48aff 2f 0d 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  /..    if( argc>
48b00 30 20 29 7b 0d 0a 20 20 20 20 20 20 70 43 73 72  0 ){..      pCsr
48b01 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->aConstraint = 
48b02 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
48b03 69 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74  izeof(RtreeConst
48b04 72 61 69 6e 74 29 2a 61 72 67 63 29 3b 0d 0a 20  raint)*argc);.. 
48b05 20 20 20 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73       pCsr->nCons
48b06 74 72 61 69 6e 74 20 3d 20 61 72 67 63 3b 0d 0a  traint = argc;..
48b07 20 20 20 20 20 20 69 66 28 20 21 70 43 73 72 2d        if( !pCsr-
48b08 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0d  >aConstraint ){.
48b09 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
48b0a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20  LITE_NOMEM;..   
48b0b 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
48b0c 20 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2d 3e     memset(pCsr->
48b0d 61 43 6f 6e 73 74 72 61 69 6e 74 2c 20 30 2c 20  aConstraint, 0, 
48b0e 73 69 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73  sizeof(RtreeCons
48b0f 74 72 61 69 6e 74 29 2a 61 72 67 63 29 3b 0d 0a  traint)*argc);..
48b10 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
48b11 28 69 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72  (idxStr==0 && ar
48b12 67 63 3d 3d 30 29 0d 0a 20 20 20 20 20 20 20 20  gc==0)..        
48b13 20 20 20 20 20 20 20 20 7c 7c 20 28 69 64 78 53          || (idxS
48b14 74 72 20 26 26 20 28 69 6e 74 29 73 74 72 6c 65  tr && (int)strle
48b15 6e 28 69 64 78 53 74 72 29 3d 3d 61 72 67 63 2a  n(idxStr)==argc*
48b16 32 29 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 66  2) );..        f
48b17 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72 67 63  or(ii=0; ii<argc
48b18 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  ; ii++){..      
48b19 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61      RtreeConstra
48b1a 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d 3e  int *p = &pCsr->
48b1b 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b  aConstraint[ii];
48b1c 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f  ..          p->o
48b1d 70 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d  p = idxStr[ii*2]
48b1e 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ;..          p->
48b1f 69 43 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b  iCoord = idxStr[
48b20 69 69 2a 32 2b 31 5d 2d 27 61 27 3b 0d 0a 20 20  ii*2+1]-'a';..  
48b21 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f          if( p->o
48b22 70 3d 3d 52 54 52 45 45 5f 4d 41 54 43 48 20 29  p==RTREE_MATCH )
48b23 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  {..            /
48b24 2a 20 41 20 4d 41 54 43 48 20 6f 70 65 72 61 74  * A MATCH operat
48b25 6f 72 2e 20 54 68 65 20 72 69 67 68 74 2d 68 61  or. The right-ha
48b26 6e 64 2d 73 69 64 65 20 6d 75 73 74 20 62 65 20  nd-side must be 
48b27 61 20 62 6c 6f 62 20 74 68 61 74 0d 0a 20 20 20  a blob that..   
48b28 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6e 20           ** can 
48b29 62 65 20 63 61 73 74 20 69 6e 74 6f 20 61 6e 20  be cast into an 
48b2a 52 74 72 65 65 4d 61 74 63 68 41 72 67 20 6f 62  RtreeMatchArg ob
48b2b 6a 65 63 74 2e 20 4f 6e 65 20 63 72 65 61 74 65  ject. One create
48b2c 64 20 75 73 69 6e 67 0d 0a 20 20 20 20 20 20 20  d using..       
48b2d 20 20 20 20 20 2a 2a 20 61 6e 20 73 71 6c 69 74       ** an sqlit
48b2e 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72  e3_rtree_geometr
48b2f 79 5f 63 61 6c 6c 62 61 63 6b 28 29 20 53 51 4c  y_callback() SQL
48b30 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0d   user function..
48b31 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0d  .            */.
48b32 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
48b33 3d 20 64 65 73 65 72 69 61 6c 69 7a 65 47 65 6f  = deserializeGeo
48b34 6d 65 74 72 79 28 61 72 67 76 5b 69 69 5d 2c 20  metry(argv[ii], 
48b35 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20 20  p);..           
48b36 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
48b37 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  OK ){..         
48b38 20 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20       break;..   
48b39 20 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20           }..    
48b3a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
48b3b 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 56 61            p->rVa
48b3c 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
48b3d 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
48b3e 69 69 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 20  ii]);..         
48b3f 20 7d 0d 0a 20 20 20 20 20 20 20 20 7d 0d 0a 20   }..        }.. 
48b40 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20       }..    }.. 
48b41 20 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   ..    if( rc==S
48b42 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
48b43 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
48b44 20 30 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   0;..      rc = 
48b45 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
48b46 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74  ee, 1, 0, &pRoot
48b47 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
48b48 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
48b49 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 69   ){..      int i
48b4a 73 45 6f 66 20 3d 20 31 3b 0d 0a 20 20 20 20 20  sEof = 1;..     
48b4b 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45   int nCell = NCE
48b4c 4c 4c 28 70 52 6f 6f 74 29 3b 0d 0a 20 20 20 20  LL(pRoot);..    
48b4d 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20    pCsr->pNode = 
48b4e 70 52 6f 6f 74 3b 0d 0a 20 20 20 20 20 20 66 6f  pRoot;..      fo
48b4f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d 30 3b  r(pCsr->iCell=0;
48b50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
48b51 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43  & pCsr->iCell<nC
48b52 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c  ell; pCsr->iCell
48b53 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 61 73  ++){..        as
48b54 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f 64  sert( pCsr->pNod
48b55 65 3d 3d 70 52 6f 6f 74 20 29 3b 0d 0a 20 20 20  e==pRoot );..   
48b56 20 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e       rc = descen
48b57 64 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  dToCell(pRtree, 
48b58 70 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44  pCsr, pRtree->iD
48b59 65 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0d 0a  epth, &isEof);..
48b5a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 45          if( !isE
48b5b 6f 66 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20  of ){..         
48b5c 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48b5d 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20   }..      }..   
48b5e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
48b5f 45 5f 4f 4b 20 26 26 20 69 73 45 6f 66 20 29 7b  E_OK && isEof ){
48b60 0d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
48b61 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
48b62 52 6f 6f 74 20 29 3b 0d 0a 20 20 20 20 20 20 20  Root );..       
48b63 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
48b64 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0d 0a 20 20  ree, pRoot);..  
48b65 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64        pCsr->pNod
48b66 65 20 3d 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d  e = 0;..      }.
48b67 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
48b68 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
48b69 21 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20  !pCsr->pNode || 
48b6a 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c  pCsr->iCell<NCEL
48b6b 4c 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20 29  L(pCsr->pNode) )
48b6c 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d  ;..    }..  }...
48b6d 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
48b6e 70 52 74 72 65 65 29 3b 0d 0a 20 20 72 65 74 75  pRtree);..  retu
48b6f 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  rn rc;..}..../*.
48b70 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
48b71 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
48b72 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
48b73 2e 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  . There are thre
48b74 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 73 63 61 6e  e..** table scan
48b75 20 73 74 72 61 74 65 67 69 65 73 20 74 6f 20 63   strategies to c
48b76 68 6f 6f 73 65 20 66 72 6f 6d 20 28 69 6e 20 6f  hoose from (in o
48b77 72 64 65 72 20 66 72 6f 6d 20 6d 6f 73 74 20 74  rder from most t
48b78 6f 20 0d 0a 2a 2a 20 6c 65 61 73 74 20 64 65 73  o ..** least des
48b79 69 72 61 62 6c 65 29 3a 0d 0a 2a 2a 0d 0a 2a 2a  irable):..**..**
48b7a 20 20 20 69 64 78 4e 75 6d 20 20 20 20 20 69 64     idxNum     id
48b7b 78 53 74 72 20 20 20 20 20 20 20 20 53 74 72 61  xStr        Stra
48b7c 74 65 67 79 0d 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  tegy..**   -----
48b7d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48b7e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48b7f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 20  -----------..** 
48b80 20 20 20 20 31 20 20 20 20 20 20 20 20 55 6e 75      1        Unu
48b81 73 65 64 20 20 20 20 20 20 20 20 44 69 72 65 63  sed        Direc
48b82 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
48b83 64 2e 0d 0a 2a 2a 20 20 20 20 20 32 20 20 20 20  d...**     2    
48b84 20 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20 20      See below   
48b85 20 20 52 2d 74 72 65 65 20 71 75 65 72 79 20 6f    R-tree query o
48b86 72 20 66 75 6c 6c 2d 74 61 62 6c 65 20 73 63 61  r full-table sca
48b87 6e 2e 0d 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  n...**   -------
48b88 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48b89 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48b8a 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a 0d 0a 2a  ---------..**..*
48b8b 2a 20 49 66 20 73 74 72 61 74 65 67 79 20 31 20  * If strategy 1 
48b8c 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 69 64  is used, then id
48b8d 78 53 74 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e  xStr is not mean
48b8e 69 6e 67 66 75 6c 2e 20 49 66 20 73 74 72 61 74  ingful. If strat
48b8f 65 67 79 0d 0a 2a 2a 20 32 20 69 73 20 75 73 65  egy..** 2 is use
48b90 64 2c 20 69 64 78 53 74 72 20 69 73 20 66 6f 72  d, idxStr is for
48b91 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e 74 61 69  matted to contai
48b92 6e 20 32 20 62 79 74 65 73 20 66 6f 72 20 65 61  n 2 bytes for ea
48b93 63 68 20 0d 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  ch ..** constrai
48b94 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66 69 72  nt used. The fir
48b95 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f 66 20  st two bytes of 
48b96 69 64 78 53 74 72 20 63 6f 72 72 65 73 70 6f 6e  idxStr correspon
48b97 64 20 74 6f 20 0d 0a 2a 2a 20 74 68 65 20 63 6f  d to ..** the co
48b98 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c 69  nstraint in sqli
48b99 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 61  te3_index_info.a
48b9a 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
48b9b 5d 20 77 69 74 68 0d 0a 2a 2a 20 28 61 72 67 76  ] with..** (argv
48b9c 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0d 0a  Index==1) etc...
48b9d 2a 2a 0d 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  **..** The first
48b9e 20 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66   of each pair of
48b9f 20 62 79 74 65 73 20 69 6e 20 69 64 78 53 74 72   bytes in idxStr
48ba0 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
48ba1 63 6f 6e 73 74 72 61 69 6e 74 0d 0a 2a 2a 20 6f  constraint..** o
48ba2 70 65 72 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f  perator as follo
48ba3 77 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 4f 70  ws:..**..**   Op
48ba4 65 72 61 74 6f 72 20 20 20 20 42 79 74 65 20 56  erator    Byte V
48ba5 61 6c 75 65 0d 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  alue..**   -----
48ba6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48ba7 2d 0d 0a 2a 2a 20 20 20 20 20 20 3d 20 20 20 20  -..**      =    
48ba8 20 20 20 20 30 78 34 31 20 28 27 41 27 29 0d 0a      0x41 ('A')..
48ba9 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20 20 20 20  **     <=       
48baa 20 30 78 34 32 20 28 27 42 27 29 0d 0a 2a 2a 20   0x42 ('B')..** 
48bab 20 20 20 20 20 3c 20 20 20 20 20 20 20 20 30 78       <        0x
48bac 34 33 20 28 27 43 27 29 0d 0a 2a 2a 20 20 20 20  43 ('C')..**    
48bad 20 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34 20   >=        0x44 
48bae 28 27 44 27 29 0d 0a 2a 2a 20 20 20 20 20 20 3e  ('D')..**      >
48baf 20 20 20 20 20 20 20 20 30 78 34 35 20 28 27 45          0x45 ('E
48bb0 27 29 0d 0a 2a 2a 20 20 20 4d 41 54 43 48 20 20  ')..**   MATCH  
48bb1 20 20 20 20 20 30 78 34 36 20 28 27 46 27 29 0d       0x46 ('F').
48bb2 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
48bb3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 2a 2a  ------------..**
48bb4 0d 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
48bb5 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20  of each pair of 
48bb6 62 79 74 65 73 20 69 64 65 6e 74 69 66 69 65 73  bytes identifies
48bb7 20 74 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20   the coordinate 
48bb8 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 74 6f 20 77 68  column..** to wh
48bb9 69 63 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69  ich the constrai
48bba 6e 74 20 61 70 70 6c 69 65 73 2e 20 54 68 65 20  nt applies. The 
48bbb 6c 65 66 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e  leftmost coordin
48bbc 61 74 65 20 63 6f 6c 75 6d 6e 0d 0a 2a 2a 20 69  ate column..** i
48bbd 73 20 27 61 27 2c 20 74 68 65 20 73 65 63 6f 6e  s 'a', the secon
48bbe 64 20 66 72 6f 6d 20 74 68 65 20 6c 65 66 74 20  d from the left 
48bbf 27 62 27 20 65 74 63 2e 0d 0a 2a 2f 0d 0a 73 74  'b' etc...*/..st
48bc0 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42 65  atic int rtreeBe
48bc1 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f  stIndex(sqlite3_
48bc2 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69 74  vtab *tab, sqlit
48bc3 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
48bc4 49 64 78 49 6e 66 6f 29 7b 0d 0a 20 20 69 6e 74  IdxInfo){..  int
48bc5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
48bc6 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 0d 0a 20  ..  int ii;.... 
48bc7 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0d 0a   int iIdx = 0;..
48bc8 20 20 63 68 61 72 20 7a 49 64 78 53 74 72 5b 52    char zIdxStr[R
48bc9 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
48bca 4f 4e 53 2a 38 2b 31 5d 3b 0d 0a 20 20 6d 65 6d  ONS*8+1];..  mem
48bcb 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c 20  set(zIdxStr, 0, 
48bcc 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29 29  sizeof(zIdxStr))
48bcd 3b 0d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
48bce 4d 45 54 45 52 28 74 61 62 29 3b 0d 0a 0d 0a 20  METER(tab);.... 
48bcf 20 61 73 73 65 72 74 28 20 70 49 64 78 49 6e 66   assert( pIdxInf
48bd0 6f 2d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0d  o->idxStr==0 );.
48bd1 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
48bd2 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
48bd3 72 61 69 6e 74 20 26 26 20 69 49 64 78 3c 28 69  raint && iIdx<(i
48bd4 6e 74 29 28 73 69 7a 65 6f 66 28 7a 49 64 78 53  nt)(sizeof(zIdxS
48bd5 74 72 29 2d 31 29 3b 20 69 69 2b 2b 29 7b 0d 0a  tr)-1); ii++){..
48bd6 20 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74      struct sqlit
48bd7 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
48bd8 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78 49 6e  int *p = &pIdxIn
48bd9 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  fo->aConstraint[
48bda 69 69 5d 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20  ii];....    if( 
48bdb 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
48bdc 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d  iColumn==0 && p-
48bdd 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  >op==SQLITE_INDE
48bde 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
48bdf 29 7b 0d 0a 20 20 20 20 20 20 2f 2a 20 57 65 20  ){..      /* We 
48be0 68 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79  have an equality
48be1 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74   constraint on t
48be2 68 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74  he rowid. Use st
48be3 72 61 74 65 67 79 20 31 2e 20 2a 2f 0d 0a 20 20  rategy 1. */..  
48be4 20 20 20 20 69 6e 74 20 6a 6a 3b 0d 0a 20 20 20      int jj;..   
48be5 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
48be6 69 69 3b 20 6a 6a 2b 2b 29 7b 0d 0a 20 20 20 20  ii; jj++){..    
48be7 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43      pIdxInfo->aC
48be8 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 6a  onstraintUsage[j
48be9 6a 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 30  j].argvIndex = 0
48bea 3b 0d 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  ;..        pIdxI
48beb 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
48bec 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d  Usage[jj].omit =
48bed 20 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20   0;..      }..  
48bee 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64      pIdxInfo->id
48bef 78 4e 75 6d 20 3d 20 31 3b 0d 0a 20 20 20 20 20  xNum = 1;..     
48bf0 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
48bf1 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e  traintUsage[ii].
48bf2 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0d 0a  argvIndex = 1;..
48bf3 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
48bf4 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
48bf5 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0d 0a  [jj].omit = 1;..
48bf6 0d 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
48bf7 73 74 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65  strategy involve
48bf8 73 20 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f  s a two rowid lo
48bf9 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72  okups on an B-Tr
48bfa 65 65 20 73 74 72 75 63 74 75 72 65 73 0d 0a 20  ee structures.. 
48bfb 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e       ** and then
48bfc 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
48bfd 20 6f 66 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f   of an R-Tree no
48bfe 64 65 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20  de. This should 
48bff 62 65 20 0d 0a 20 20 20 20 20 20 2a 2a 20 63 6f  be ..      ** co
48c00 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f 73 74 20  nsidered almost 
48c01 61 73 20 71 75 69 63 6b 20 61 73 20 61 20 64 69  as quick as a di
48c02 72 65 63 74 20 72 6f 77 69 64 20 6c 6f 6f 6b 75  rect rowid looku
48c03 70 20 28 66 6f 72 20 77 68 69 63 68 20 0d 0a 20  p (for which .. 
48c04 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 20 75       ** sqlite u
48c05 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20  ses an internal 
48c06 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0d 0a 20  cost of 0.0)... 
48c07 20 20 20 20 20 2a 2f 20 0d 0a 20 20 20 20 20 20       */ ..      
48c08 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
48c09 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0d  tedCost = 10.0;.
48c0a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
48c0b 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 20 20 7d 0d  LITE_OK;..    }.
48c0c 0a 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73  ...    if( p->us
48c0d 61 62 6c 65 20 26 26 20 28 70 2d 3e 69 43 6f 6c  able && (p->iCol
48c0e 75 6d 6e 3e 30 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  umn>0 || p->op==
48c0f 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
48c10 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 29 20 29  STRAINT_MATCH) )
48c11 7b 0d 0a 20 20 20 20 20 20 75 38 20 6f 70 3b 0d  {..      u8 op;.
48c12 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70  .      switch( p
48c13 2d 3e 6f 70 20 29 7b 0d 0a 20 20 20 20 20 20 20  ->op ){..       
48c14 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
48c15 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51  EX_CONSTRAINT_EQ
48c16 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 45 51 3b  : op = RTREE_EQ;
48c17 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48c18 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
48c19 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54  EX_CONSTRAINT_GT
48c1a 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 54 3b  : op = RTREE_GT;
48c1b 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48c1c 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
48c1d 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45  EX_CONSTRAINT_LE
48c1e 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45 3b  : op = RTREE_LE;
48c1f 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48c20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
48c21 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
48c22 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b  : op = RTREE_LT;
48c23 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48c24 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
48c25 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45  EX_CONSTRAINT_GE
48c26 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b  : op = RTREE_GE;
48c27 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20 20 20 20   break;..       
48c28 20 64 65 66 61 75 6c 74 3a 0d 0a 20 20 20 20 20   default:..     
48c29 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
48c2a 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  op==SQLITE_INDEX
48c2b 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
48c2c 48 20 29 3b 0d 0a 20 20 20 20 20 20 20 20 20 20  H );..          
48c2d 6f 70 20 3d 20 52 54 52 45 45 5f 4d 41 54 43 48  op = RTREE_MATCH
48c2e 3b 20 0d 0a 20 20 20 20 20 20 20 20 20 20 62 72  ; ..          br
48c2f 65 61 6b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  eak;..      }.. 
48c30 20 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64       zIdxStr[iId
48c31 78 2b 2b 5d 20 3d 20 6f 70 3b 0d 0a 20 20 20 20  x++] = op;..    
48c32 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
48c33 5d 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  ] = p->iColumn -
48c34 20 31 20 2b 20 27 61 27 3b 0d 0a 20 20 20 20 20   1 + 'a';..     
48c35 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
48c36 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e  traintUsage[ii].
48c37 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49 64  argvIndex = (iId
48c38 78 2f 32 29 3b 0d 0a 20 20 20 20 20 20 70 49 64  x/2);..      pId
48c39 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
48c3a 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d 69 74  ntUsage[ii].omit
48c3b 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20   = 1;..    }..  
48c3c 7d 0d 0a 0d 0a 20 20 70 49 64 78 49 6e 66 6f 2d  }....  pIdxInfo-
48c3d 3e 69 64 78 4e 75 6d 20 3d 20 32 3b 0d 0a 20 20  >idxNum = 2;..  
48c3e 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
48c3f 46 72 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0d  FreeIdxStr = 1;.
48c40 0a 20 20 69 66 28 20 69 49 64 78 3e 30 20 26 26  .  if( iIdx>0 &&
48c41 20 30 3d 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69   0==(pIdxInfo->i
48c42 64 78 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f  dxStr = sqlite3_
48c43 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 49  mprintf("%s", zI
48c44 64 78 53 74 72 29 29 20 29 7b 0d 0a 20 20 20 20  dxStr)) ){..    
48c45 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
48c46 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 61 73 73  MEM;..  }..  ass
48c47 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b 0d  ert( iIdx>=0 );.
48c48 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
48c49 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30  imatedCost = (20
48c4a 30 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c  00000.0 / (doubl
48c4b 65 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0d 0a  e)(iIdx + 1));..
48c4c 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
48c4d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e  .../*..** Return
48c4e 20 74 68 65 20 4e 2d 64 69 6d 65 6e 73 69 6f 6e   the N-dimension
48c4f 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  al volumn of the
48c50 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 69 6e 20   cell stored in 
48c51 2a 70 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  *p...*/..static 
48c52 66 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61 28 52  float cellArea(R
48c53 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
48c54 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0d 0a 20 20  reeCell *p){..  
48c55 66 6c 6f 61 74 20 61 72 65 61 20 3d 20 31 2e 30  float area = 1.0
48c56 3b 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20  ;..  int ii;..  
48c57 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
48c58 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
48c59 69 2b 3d 32 29 7b 0d 0a 20 20 20 20 61 72 65 61  i+=2){..    area
48c5a 20 3d 20 28 66 6c 6f 61 74 29 28 61 72 65 61 20   = (float)(area 
48c5b 2a 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  * (DCOORD(p->aCo
48c5c 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f  ord[ii+1]) - DCO
48c5d 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69  ORD(p->aCoord[ii
48c5e 5d 29 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  ])));..  }..  re
48c5f 74 75 72 6e 20 61 72 65 61 3b 0d 0a 7d 0d 0a 0d  turn area;..}...
48c60 0a 2f 2a 0d 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./*..** Return t
48c61 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68  he margin length
48c62 20 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65 20   of cell p. The 
48c63 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69 73  margin length is
48c64 20 74 68 65 20 73 75 6d 0d 0a 2a 2a 20 6f 66 20   the sum..** of 
48c65 74 68 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65  the objects size
48c66 20 69 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69   in each dimensi
48c67 6f 6e 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  on...*/..static 
48c68 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72 67 69 6e  float cellMargin
48c69 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
48c6a 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0d 0a  RtreeCell *p){..
48c6b 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d    float margin =
48c6c 20 30 2e 30 3b 0d 0a 20 20 69 6e 74 20 69 69 3b   0.0;..  int ii;
48c6d 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
48c6e 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
48c6f 29 3b 20 69 69 2b 3d 32 29 7b 0d 0a 20 20 20 20  ); ii+=2){..    
48c70 6d 61 72 67 69 6e 20 2b 3d 20 28 66 6c 6f 61 74  margin += (float
48c71 29 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f  )(DCOORD(p->aCoo
48c72 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f  rd[ii+1]) - DCOO
48c73 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d  RD(p->aCoord[ii]
48c74 29 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75  ));..  }..  retu
48c75 72 6e 20 6d 61 72 67 69 6e 3b 0d 0a 7d 0d 0a 0d  rn margin;..}...
48c76 0a 2f 2a 0d 0a 2a 2a 20 53 74 6f 72 65 20 74 68  ./*..** Store th
48c77 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c 6c 73  e union of cells
48c78 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20 70 31   p1 and p2 in p1
48c79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f  ...*/..static vo
48c7a 69 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72  id cellUnion(Rtr
48c7b 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
48c7c 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
48c7d 43 65 6c 6c 20 2a 70 32 29 7b 0d 0a 20 20 69 6e  Cell *p2){..  in
48c7e 74 20 69 69 3b 0d 0a 20 20 69 66 28 20 70 52 74  t ii;..  if( pRt
48c7f 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
48c80 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
48c81 4c 33 32 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28  L32 ){..    for(
48c82 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65  ii=0; ii<(pRtree
48c83 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32  ->nDim*2); ii+=2
48c84 29 7b 0d 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  ){..      p1->aC
48c85 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e  oord[ii].f = MIN
48c86 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
48c87 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  f, p2->aCoord[ii
48c88 5d 2e 66 29 3b 0d 0a 20 20 20 20 20 20 70 31 2d  ].f);..      p1-
48c89 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20  >aCoord[ii+1].f 
48c8a 3d 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64  = MAX(p1->aCoord
48c8b 5b 69 69 2b 31 5d 2e 66 2c 20 70 32 2d 3e 61 43  [ii+1].f, p2->aC
48c8c 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 29 3b 0d 0a  oord[ii+1].f);..
48c8d 20 20 20 20 7d 0d 0a 20 20 7d 65 6c 73 65 7b 0d      }..  }else{.
48c8e 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  .    for(ii=0; i
48c8f 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
48c90 32 29 3b 20 69 69 2b 3d 32 29 7b 0d 0a 20 20 20  2); ii+=2){..   
48c91 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
48c92 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43  ].i = MIN(p1->aC
48c93 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e  oord[ii].i, p2->
48c94 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b 0d 0a  aCoord[ii].i);..
48c95 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64        p1->aCoord
48c96 5b 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28 70  [ii+1].i = MAX(p
48c97 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  1->aCoord[ii+1].
48c98 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  i, p2->aCoord[ii
48c99 2b 31 5d 2e 69 29 3b 0d 0a 20 20 20 20 7d 0d 0a  +1].i);..    }..
48c9a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
48c9b 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
48c9c 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64  the area covered
48c9d 20 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73   by p2 is a subs
48c9e 65 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63  et of the area c
48c9f 6f 76 65 72 65 64 0d 0a 2a 2a 20 62 79 20 70 31  overed..** by p1
48ca0 2e 20 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73  . False otherwis
48ca1 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
48ca2 6e 74 20 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28  nt cellContains(
48ca3 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
48ca4 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74  treeCell *p1, Rt
48ca5 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b 0d 0a 20  reeCell *p2){.. 
48ca6 20 69 6e 74 20 69 69 3b 0d 0a 20 20 69 6e 74 20   int ii;..  int 
48ca7 69 73 49 6e 74 20 3d 20 28 70 52 74 72 65 65 2d  isInt = (pRtree-
48ca8 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52  >eCoordType==RTR
48ca9 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 29 3b  EE_COORD_INT32);
48caa 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
48cab 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
48cac 29 3b 20 69 69 2b 3d 32 29 7b 0d 0a 20 20 20 20  ); ii+=2){..    
48cad 52 74 72 65 65 43 6f 6f 72 64 20 2a 61 31 20 3d  RtreeCoord *a1 =
48cae 20 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d   &p1->aCoord[ii]
48caf 3b 0d 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72  ;..    RtreeCoor
48cb0 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43 6f  d *a2 = &p2->aCo
48cb1 6f 72 64 5b 69 69 5d 3b 0d 0a 20 20 20 20 69 66  ord[ii];..    if
48cb2 28 20 28 21 69 73 49 6e 74 20 26 26 20 28 61 32  ( (!isInt && (a2
48cb3 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c  [0].f<a1[0].f ||
48cb4 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66   a2[1].f>a1[1].f
48cb5 29 29 20 0d 0a 20 20 20 20 20 7c 7c 20 28 20 69  )) ..     || ( i
48cb6 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e 69  sInt && (a2[0].i
48cb7 3c 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b 31  <a1[0].i || a2[1
48cb8 5d 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0d 0a  ].i>a1[1].i)) ..
48cb9 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 72 65      ){..      re
48cba 74 75 72 6e 20 30 3b 0d 0a 20 20 20 20 7d 0d 0a  turn 0;..    }..
48cbb 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 31 3b    }..  return 1;
48cbc 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
48cbd 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
48cbe 63 65 6c 6c 20 70 20 77 6f 75 6c 64 20 67 72 6f  cell p would gro
48cbf 77 20 62 79 20 69 66 20 69 74 20 77 65 72 65 20  w by if it were 
48cc0 75 6e 69 6f 6e 65 64 20 77 69 74 68 20 70 43 65  unioned with pCe
48cc1 6c 6c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ll...*/..static 
48cc2 66 6c 6f 61 74 20 63 65 6c 6c 47 72 6f 77 74 68  float cellGrowth
48cc3 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
48cc4 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 52 74  RtreeCell *p, Rt
48cc5 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 29 7b  reeCell *pCell){
48cc6 0d 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0d  ..  float area;.
48cc7 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c  .  RtreeCell cel
48cc8 6c 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 26 63 65  l;..  memcpy(&ce
48cc9 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52 74  ll, p, sizeof(Rt
48cca 72 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 61 72  reeCell));..  ar
48ccb 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
48ccc 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0d 0a 20  tree, &cell);.. 
48ccd 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
48cce 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29  e, &cell, pCell)
48ccf 3b 0d 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c  ;..  return (cel
48cd0 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63  lArea(pRtree, &c
48cd1 65 6c 6c 29 2d 61 72 65 61 29 3b 0d 0a 7d 0d 0a  ell)-area);..}..
48cd2 0d 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53  ..#if VARIANT_RS
48cd3 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55  TARTREE_CHOOSESU
48cd4 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41 4e 54  BTREE || VARIANT
48cd5 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54  _RSTARTREE_SPLIT
48cd6 0d 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  ..static float c
48cd7 65 6c 6c 4f 76 65 72 6c 61 70 28 0d 0a 20 20 52  ellOverlap(..  R
48cd8 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0d 0a  tree *pRtree, ..
48cd9 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20    RtreeCell *p, 
48cda 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ..  RtreeCell *a
48cdb 43 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20 6e 43  Cell, ..  int nC
48cdc 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20 69 45 78  ell, ..  int iEx
48cdd 63 6c 75 64 65 0d 0a 29 7b 0d 0a 20 20 69 6e 74  clude..){..  int
48cde 20 69 69 3b 0d 0a 20 20 66 6c 6f 61 74 20 6f 76   ii;..  float ov
48cdf 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0d 0a 20 20  erlap = 0.0;..  
48ce0 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65  for(ii=0; ii<nCe
48ce1 6c 6c 3b 20 69 69 2b 2b 29 7b 0d 0a 23 69 66 20  ll; ii++){..#if 
48ce2 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
48ce3 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0d  E_CHOOSESUBTREE.
48ce4 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69 45 78  .    if( ii!=iEx
48ce5 63 6c 75 64 65 20 29 0d 0a 23 65 6c 73 65 0d 0a  clude )..#else..
48ce6 20 20 20 20 61 73 73 65 72 74 28 20 69 45 78 63      assert( iExc
48ce7 6c 75 64 65 3d 3d 2d 31 20 29 3b 0d 0a 20 20 20  lude==-1 );..   
48ce8 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
48ce9 52 28 69 45 78 63 6c 75 64 65 29 3b 0d 0a 23 65  R(iExclude);..#e
48cea 6e 64 69 66 0d 0a 20 20 20 20 7b 0d 0a 20 20 20  ndif..    {..   
48ceb 20 20 20 69 6e 74 20 6a 6a 3b 0d 0a 20 20 20 20     int jj;..    
48cec 20 20 66 6c 6f 61 74 20 6f 20 3d 20 31 2e 30 3b    float o = 1.0;
48ced 0d 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30  ..      for(jj=0
48cee 3b 20 6a 6a 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; jj<(pRtree->nD
48cef 69 6d 2a 32 29 3b 20 6a 6a 2b 3d 32 29 7b 0d 0a  im*2); jj+=2){..
48cf0 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78          double x
48cf1 31 3b 0d 0a 20 20 20 20 20 20 20 20 64 6f 75 62  1;..        doub
48cf2 6c 65 20 78 32 3b 0d 0a 0d 0a 20 20 20 20 20 20  le x2;....      
48cf3 20 20 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f 52    x1 = MAX(DCOOR
48cf4 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d 29  D(p->aCoord[jj])
48cf5 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  , DCOORD(aCell[i
48cf6 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29 3b  i].aCoord[jj]));
48cf7 0d 0a 20 20 20 20 20 20 20 20 78 32 20 3d 20 4d  ..        x2 = M
48cf8 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  IN(DCOORD(p->aCo
48cf9 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f  ord[jj+1]), DCOO
48cfa 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
48cfb 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0d 0a 0d 0a  ord[jj+1]));....
48cfc 20 20 20 20 20 20 20 20 69 66 28 20 78 32 3c 78          if( x2<x
48cfd 31 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20  1 ){..          
48cfe 6f 20 3d 20 30 2e 30 3b 0d 0a 20 20 20 20 20 20  o = 0.0;..      
48cff 20 20 20 20 62 72 65 61 6b 3b 0d 0a 20 20 20 20      break;..    
48d00 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20      }else{..    
48d01 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28 66        o = o * (f
48d02 6c 6f 61 74 29 28 78 32 2d 78 31 29 3b 0d 0a 20  loat)(x2-x1);.. 
48d03 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
48d04 7d 0d 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70  }..      overlap
48d05 20 2b 3d 20 6f 3b 0d 0a 20 20 20 20 7d 0d 0a 20   += o;..    }.. 
48d06 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 6f 76 65   }..  return ove
48d07 72 6c 61 70 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  rlap;..}..#endif
48d08 0d 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  ....#if VARIANT_
48d09 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
48d0a 53 55 42 54 52 45 45 0d 0a 73 74 61 74 69 63 20  SUBTREE..static 
48d0b 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61  float cellOverla
48d0c 70 45 6e 6c 61 72 67 65 6d 65 6e 74 28 0d 0a 20  pEnlargement(.. 
48d0d 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
48d0e 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ..  RtreeCell *p
48d0f 2c 20 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  , ..  RtreeCell 
48d10 2a 70 49 6e 73 65 72 74 2c 20 0d 0a 20 20 52 74  *pInsert, ..  Rt
48d11 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20  reeCell *aCell, 
48d12 0d 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0d  ..  int nCell, .
48d13 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64 65 0d  .  int iExclude.
48d14 0a 29 7b 0d 0a 20 20 64 6f 75 62 6c 65 20 62 65  .){..  double be
48d15 66 6f 72 65 3b 0d 0a 20 20 64 6f 75 62 6c 65 20  fore;..  double 
48d16 61 66 74 65 72 3b 0d 0a 20 20 62 65 66 6f 72 65  after;..  before
48d17 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70   = cellOverlap(p
48d18 52 74 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c  Rtree, p, aCell,
48d19 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65   nCell, iExclude
48d1a 29 3b 0d 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28  );..  cellUnion(
48d1b 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e 73 65  pRtree, p, pInse
48d1c 72 74 29 3b 0d 0a 20 20 61 66 74 65 72 20 3d 20  rt);..  after = 
48d1d 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72  cellOverlap(pRtr
48d1e 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43  ee, p, aCell, nC
48d1f 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0d  ell, iExclude);.
48d20 0a 20 20 72 65 74 75 72 6e 20 28 66 6c 6f 61 74  .  return (float
48d21 29 28 61 66 74 65 72 2d 62 65 66 6f 72 65 29 3b  )(after-before);
48d22 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 0d  ..}..#endif.....
48d23 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./*..** This fun
48d24 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
48d25 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66 20   the ChooseLeaf 
48d26 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20 47  algorithm from G
48d27 75 74 6d 61 6e 5b 38 34 5d 2e 0d 0a 2a 2a 20 43  utman[84]...** C
48d28 68 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20  hooseSubTree in 
48d29 72 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f  r*tree terminolo
48d2a 67 79 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  gy...*/..static 
48d2b 69 6e 74 20 43 68 6f 6f 73 65 4c 65 61 66 28 0d  int ChooseLeaf(.
48d2c 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
48d2d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
48d2e 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
48d2f 2f 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  /..  RtreeCell *
48d30 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  pCell,          
48d31 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20 69 6e 73    /* Cell to ins
48d32 65 72 74 20 69 6e 74 6f 20 72 74 72 65 65 20 2a  ert into rtree *
48d33 2f 0d 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74  /..  int iHeight
48d34 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
48d35 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 73    /* Height of s
48d36 75 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20 61  ub-tree rooted a
48d37 74 20 70 43 65 6c 6c 20 2a 2f 0d 0a 20 20 52 74  t pCell */..  Rt
48d38 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66  reeNode **ppLeaf
48d39 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
48d3a 54 3a 20 53 65 6c 65 63 74 65 64 20 6c 65 61 66  T: Selected leaf
48d3b 20 70 61 67 65 20 2a 2f 0d 0a 29 7b 0d 0a 20 20   page */..){..  
48d3c 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74 20 69  int rc;..  int i
48d3d 69 3b 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  i;..  RtreeNode 
48d3e 2a 70 4e 6f 64 65 3b 0d 0a 20 20 72 63 20 3d 20  *pNode;..  rc = 
48d3f 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
48d40 65 65 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65  ee, 1, 0, &pNode
48d41 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69 3d 30  );....  for(ii=0
48d42 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
48d43 26 26 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 69  && ii<(pRtree->i
48d44 44 65 70 74 68 2d 69 48 65 69 67 68 74 29 3b 20  Depth-iHeight); 
48d45 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 6e 74 20  ii++){..    int 
48d46 69 43 65 6c 6c 3b 0d 0a 20 20 20 20 73 71 6c 69  iCell;..    sqli
48d47 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74 20  te3_int64 iBest 
48d48 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 66 6c 6f 61  = 0;....    floa
48d49 74 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20 30  t fMinGrowth = 0
48d4a 2e 30 3b 0d 0a 20 20 20 20 66 6c 6f 61 74 20 66  .0;..    float f
48d4b 4d 69 6e 41 72 65 61 20 3d 20 30 2e 30 3b 0d 0a  MinArea = 0.0;..
48d4c 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
48d4d 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54  RTREE_CHOOSESUBT
48d4e 52 45 45 0d 0a 20 20 20 20 66 6c 6f 61 74 20 66  REE..    float f
48d4f 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20 30 2e 30  MinOverlap = 0.0
48d50 3b 0d 0a 20 20 20 20 66 6c 6f 61 74 20 6f 76 65  ;..    float ove
48d51 72 6c 61 70 3b 0d 0a 23 65 6e 64 69 66 0d 0a 0d  rlap;..#endif...
48d52 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d  .    int nCell =
48d53 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0d 0a   NCELL(pNode);..
48d54 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65      RtreeCell ce
48d55 6c 6c 3b 0d 0a 20 20 20 20 52 74 72 65 65 4e 6f  ll;..    RtreeNo
48d56 64 65 20 2a 70 43 68 69 6c 64 3b 0d 0a 0d 0a 20  de *pChild;.... 
48d57 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43     RtreeCell *aC
48d58 65 6c 6c 20 3d 20 30 3b 0d 0a 0d 0a 23 69 66 20  ell = 0;....#if 
48d59 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
48d5a 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0d  E_CHOOSESUBTREE.
48d5b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70 52  .    if( ii==(pR
48d5c 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29 20  tree->iDepth-1) 
48d5d 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a  ){..      int jj
48d5e 3b 0d 0a 20 20 20 20 20 20 61 43 65 6c 6c 20 3d  ;..      aCell =
48d5f 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
48d60 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c  sizeof(RtreeCell
48d61 29 2a 6e 43 65 6c 6c 29 3b 0d 0a 20 20 20 20 20  )*nCell);..     
48d62 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0d 0a   if( !aCell ){..
48d63 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
48d64 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
48d65 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
48d66 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0d  pRtree, pNode);.
48d67 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d  .        pNode =
48d68 20 30 3b 0d 0a 20 20 20 20 20 20 20 20 63 6f 6e   0;..        con
48d69 74 69 6e 75 65 3b 0d 0a 20 20 20 20 20 20 7d 0d  tinue;..      }.
48d6a 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b  .      for(jj=0;
48d6b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
48d6c 7b 0d 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 47  {..        nodeG
48d6d 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
48d6e 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c  Node, jj, &aCell
48d6f 5b 6a 6a 5d 29 3b 0d 0a 20 20 20 20 20 20 7d 0d  [jj]);..      }.
48d70 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a  .    }..#endif..
48d71 0d 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20  ..    /* Select 
48d72 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 77  the child node w
48d73 68 69 63 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c  hich will be enl
48d74 61 72 67 65 64 20 74 68 65 20 6c 65 61 73 74 20  arged the least 
48d75 69 66 20 70 43 65 6c 6c 0d 0a 20 20 20 20 2a 2a  if pCell..    **
48d76 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74   is inserted int
48d77 6f 20 69 74 2e 20 52 65 73 6f 6c 76 65 20 74 69  o it. Resolve ti
48d78 65 73 20 62 79 20 63 68 6f 6f 73 69 6e 67 20 74  es by choosing t
48d79 68 65 20 65 6e 74 72 79 20 77 69 74 68 0d 0a 20  he entry with.. 
48d7a 20 20 20 2a 2a 20 74 68 65 20 73 6d 61 6c 6c 65     ** the smalle
48d7b 73 74 20 61 72 65 61 2e 0d 0a 20 20 20 20 2a 2f  st area...    */
48d7c 0d 0a 20 20 20 20 66 6f 72 28 69 43 65 6c 6c 3d  ..    for(iCell=
48d7d 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  0; iCell<nCell; 
48d7e 69 43 65 6c 6c 2b 2b 29 7b 0d 0a 20 20 20 20 20  iCell++){..     
48d7f 20 69 6e 74 20 62 42 65 73 74 20 3d 20 30 3b 0d   int bBest = 0;.
48d80 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 67 72 6f  .      float gro
48d81 77 74 68 3b 0d 0a 20 20 20 20 20 20 66 6c 6f 61  wth;..      floa
48d82 74 20 61 72 65 61 3b 0d 0a 20 20 20 20 20 20 6e  t area;..      n
48d83 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
48d84 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c  e, pNode, iCell,
48d85 20 26 63 65 6c 6c 29 3b 0d 0a 20 20 20 20 20 20   &cell);..      
48d86 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72 6f  growth = cellGro
48d87 77 74 68 28 70 52 74 72 65 65 2c 20 26 63 65 6c  wth(pRtree, &cel
48d88 6c 2c 20 70 43 65 6c 6c 29 3b 0d 0a 20 20 20 20  l, pCell);..    
48d89 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65    area = cellAre
48d8a 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29  a(pRtree, &cell)
48d8b 3b 0d 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54  ;....#if VARIANT
48d8c 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
48d8d 45 53 55 42 54 52 45 45 0d 0a 20 20 20 20 20 20  ESUBTREE..      
48d8e 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65 2d  if( ii==(pRtree-
48d8f 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0d 0a 20  >iDepth-1) ){.. 
48d90 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d         overlap =
48d91 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61   cellOverlapEnla
48d92 72 67 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26  rgement(pRtree,&
48d93 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c  cell,pCell,aCell
48d94 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0d 0a  ,nCell,iCell);..
48d95 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20        }else{..  
48d96 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d 20        overlap = 
48d97 30 2e 30 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  0.0;..      }.. 
48d98 20 20 20 20 20 69 66 28 20 28 69 43 65 6c 6c 3d       if( (iCell=
48d99 3d 30 29 20 0d 0a 20 20 20 20 20 20 20 7c 7c 20  =0) ..       || 
48d9a 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e 4f 76 65  (overlap<fMinOve
48d9b 72 6c 61 70 29 20 0d 0a 20 20 20 20 20 20 20 7c  rlap) ..       |
48d9c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e  | (overlap==fMin
48d9d 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f 77 74  Overlap && growt
48d9e 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0d 0a 20  h<fMinGrowth).. 
48d9f 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
48da0 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20 26  p==fMinOverlap &
48da1 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47 72  & growth==fMinGr
48da2 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d 69  owth && area<fMi
48da3 6e 41 72 65 61 29 0d 0a 20 20 20 20 20 20 29 7b  nArea)..      ){
48da4 0d 0a 20 20 20 20 20 20 20 20 62 42 65 73 74 20  ..        bBest 
48da5 3d 20 31 3b 0d 0a 20 20 20 20 20 20 20 20 66 4d  = 1;..        fM
48da6 69 6e 4f 76 65 72 6c 61 70 20 3d 20 6f 76 65 72  inOverlap = over
48da7 6c 61 70 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 23  lap;..      }..#
48da8 65 6c 73 65 0d 0a 20 20 20 20 20 20 69 66 28 20  else..      if( 
48da9 69 43 65 6c 6c 3d 3d 30 7c 7c 67 72 6f 77 74 68  iCell==0||growth
48daa 3c 66 4d 69 6e 47 72 6f 77 74 68 7c 7c 28 67 72  <fMinGrowth||(gr
48dab 6f 77 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68  owth==fMinGrowth
48dac 20 26 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65   && area<fMinAre
48dad 61 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 62  a) ){..        b
48dae 42 65 73 74 20 3d 20 31 3b 0d 0a 20 20 20 20 20  Best = 1;..     
48daf 20 7d 0d 0a 23 65 6e 64 69 66 0d 0a 20 20 20 20   }..#endif..    
48db0 20 20 69 66 28 20 62 42 65 73 74 20 29 7b 0d 0a    if( bBest ){..
48db1 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77          fMinGrow
48db2 74 68 20 3d 20 67 72 6f 77 74 68 3b 0d 0a 20 20  th = growth;..  
48db3 20 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d        fMinArea =
48db4 20 61 72 65 61 3b 0d 0a 20 20 20 20 20 20 20 20   area;..        
48db5 69 42 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f  iBest = cell.iRo
48db6 77 69 64 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  wid;..      }.. 
48db7 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 73 71 6c 69     }....    sqli
48db8 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
48db9 0d 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  ..    rc = nodeA
48dba 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
48dbb 42 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26 70 43  Best, pNode, &pC
48dbc 68 69 6c 64 29 3b 0d 0a 20 20 20 20 6e 6f 64 65  hild);..    node
48dbd 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
48dbe 70 4e 6f 64 65 29 3b 0d 0a 20 20 20 20 70 4e 6f  pNode);..    pNo
48dbf 64 65 20 3d 20 70 43 68 69 6c 64 3b 0d 0a 20 20  de = pChild;..  
48dc0 7d 0d 0a 0d 0a 20 20 2a 70 70 4c 65 61 66 20 3d  }....  *ppLeaf =
48dc1 20 70 4e 6f 64 65 3b 0d 0a 20 20 72 65 74 75 72   pNode;..  retur
48dc2 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
48dc3 2a 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74  ** A cell with t
48dc4 68 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20  he same content 
48dc5 61 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73  as pCell has jus
48dc6 74 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20  t been inserted 
48dc7 69 6e 74 6f 0d 0a 2a 2a 20 74 68 65 20 6e 6f 64  into..** the nod
48dc8 65 20 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75  e pNode. This fu
48dc9 6e 63 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74  nction updates t
48dca 68 65 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20  he bounding box 
48dcb 63 65 6c 6c 73 20 69 6e 0d 0a 2a 2a 20 61 6c 6c  cells in..** all
48dcc 20 61 6e 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e   ancestor elemen
48dcd 74 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ts...*/..static 
48dce 69 6e 74 20 41 64 6a 75 73 74 54 72 65 65 28 0d  int AdjustTree(.
48dcf 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
48dd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
48dd1 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74 61       /* Rtree ta
48dd2 62 6c 65 20 2a 2f 0d 0a 20 20 52 74 72 65 65 4e  ble */..  RtreeN
48dd3 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 20 20 20  ode *pNode,     
48dd4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
48dd5 64 6a 75 73 74 20 61 6e 63 65 73 74 72 79 20 6f  djust ancestry o
48dd6 66 20 74 68 69 73 20 6e 6f 64 65 2e 20 2a 2f 0d  f this node. */.
48dd7 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
48dd8 65 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ell             
48dd9 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 65 6c       /* This cel
48dda 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65 72  l was just inser
48ddb 74 65 64 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 52 74  ted */..){..  Rt
48ddc 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f  reeNode *p = pNo
48ddd 64 65 3b 0d 0a 20 20 77 68 69 6c 65 28 20 70 2d  de;..  while( p-
48dde 3e 70 50 61 72 65 6e 74 20 29 7b 0d 0a 20 20 20  >pParent ){..   
48ddf 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
48de0 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74  ent = p->pParent
48de1 3b 0d 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  ;..    RtreeCell
48de2 20 63 65 6c 6c 3b 0d 0a 20 20 20 20 69 6e 74 20   cell;..    int 
48de3 69 43 65 6c 6c 3b 0d 0a 0d 0a 20 20 20 20 69 66  iCell;....    if
48de4 28 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  ( nodeParentInde
48de5 78 28 70 52 74 72 65 65 2c 20 70 2c 20 26 69 43  x(pRtree, p, &iC
48de6 65 6c 6c 29 20 29 7b 0d 0a 20 20 20 20 20 20 72  ell) ){..      r
48de7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
48de8 52 55 50 54 5f 56 54 41 42 3b 0d 0a 20 20 20 20  RUPT_VTAB;..    
48de9 7d 0d 0a 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74  }....    nodeGet
48dea 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
48deb 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 26 63 65  rent, iCell, &ce
48dec 6c 6c 29 3b 0d 0a 20 20 20 20 69 66 28 20 21 63  ll);..    if( !c
48ded 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52 74 72  ellContains(pRtr
48dee 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c  ee, &cell, pCell
48def 29 20 29 7b 0d 0a 20 20 20 20 20 20 63 65 6c 6c  ) ){..      cell
48df0 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 63  Union(pRtree, &c
48df1 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0d 0a 20 20  ell, pCell);..  
48df2 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
48df3 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50  eCell(pRtree, pP
48df4 61 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43  arent, &cell, iC
48df5 65 6c 6c 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 0d  ell);..    }.. .
48df6 0a 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e 74  .    p = pParent
48df7 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
48df8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a   SQLITE_OK;..}..
48df9 0d 0a 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 6d  ../*..** Write m
48dfa 61 70 70 69 6e 67 20 28 69 52 6f 77 69 64 2d 3e  apping (iRowid->
48dfb 69 4e 6f 64 65 29 20 74 6f 20 74 68 65 20 3c 72  iNode) to the <r
48dfc 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c  tree>_rowid tabl
48dfd 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  e...*/..static i
48dfe 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52 74  nt rowidWrite(Rt
48dff 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c  ree *pRtree, sql
48e00 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69  ite3_int64 iRowi
48e01 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
48e02 20 69 4e 6f 64 65 29 7b 0d 0a 20 20 73 71 6c 69   iNode){..  sqli
48e03 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
48e04 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
48e05 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0d  id, 1, iRowid);.
48e06 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
48e07 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57  int64(pRtree->pW
48e08 72 69 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e  riteRowid, 2, iN
48e09 6f 64 65 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33  ode);..  sqlite3
48e0a 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57  _step(pRtree->pW
48e0b 72 69 74 65 52 6f 77 69 64 29 3b 0d 0a 20 20 72  riteRowid);..  r
48e0c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65  eturn sqlite3_re
48e0d 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69  set(pRtree->pWri
48e0e 74 65 52 6f 77 69 64 29 3b 0d 0a 7d 0d 0a 0d 0a  teRowid);..}....
48e0f 2f 2a 0d 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70  /*..** Write map
48e10 70 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69 50 61  ping (iNode->iPa
48e11 72 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65  r) to the <rtree
48e12 3e 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 0d  >_parent table..
48e13 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  .*/..static int 
48e14 70 61 72 65 6e 74 57 72 69 74 65 28 52 74 72 65  parentWrite(Rtre
48e15 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74  e *pRtree, sqlit
48e16 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20  e3_int64 iNode, 
48e17 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
48e18 61 72 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f  ar){..  sqlite3_
48e19 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
48e1a 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 2c  e->pWriteParent,
48e1b 20 31 2c 20 69 4e 6f 64 65 29 3b 0d 0a 20 20 73   1, iNode);..  s
48e1c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
48e1d 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  4(pRtree->pWrite
48e1e 50 61 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29  Parent, 2, iPar)
48e1f 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  ;..  sqlite3_ste
48e20 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  p(pRtree->pWrite
48e21 50 61 72 65 6e 74 29 3b 0d 0a 20 20 72 65 74 75  Parent);..  retu
48e22 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
48e23 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
48e24 61 72 65 6e 74 29 3b 0d 0a 7d 0d 0a 0d 0a 73 74  arent);..}....st
48e25 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
48e26 73 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a  sertCell(Rtree *
48e27 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52  , RtreeNode *, R
48e28 74 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29  treeCell *, int)
48e29 3b 0d 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54  ;....#if VARIANT
48e2a 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f  _GUTTMAN_LINEAR_
48e2b 53 50 4c 49 54 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d  SPLIT../*..** Im
48e2c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
48e2d 74 68 65 20 6c 69 6e 65 61 72 20 76 61 72 69 61  the linear varia
48e2e 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 4e 65  nt of the PickNe
48e2f 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72  xt() function fr
48e30 6f 6d 0d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38  om..** Guttman[8
48e31 34 5d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  4]...*/..static 
48e32 52 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e 65 61  RtreeCell *Linea
48e33 72 50 69 63 6b 4e 65 78 74 28 0d 0a 20 20 52 74  rPickNext(..  Rt
48e34 72 65 65 20 2a 70 52 74 72 65 65 2c 0d 0a 20 20  ree *pRtree,..  
48e35 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
48e36 2c 20 0d 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  , ..  int nCell,
48e37 20 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a   ..  RtreeCell *
48e38 70 4c 65 66 74 42 6f 78 2c 20 0d 0a 20 20 52 74  pLeftBox, ..  Rt
48e39 72 65 65 43 65 6c 6c 20 2a 70 52 69 67 68 74 42  reeCell *pRightB
48e3a 6f 78 2c 0d 0a 20 20 69 6e 74 20 2a 61 69 55 73  ox,..  int *aiUs
48e3b 65 64 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 69  ed..){..  int ii
48e3c 3b 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61  ;..  for(ii=0; a
48e3d 69 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29  iUsed[ii]; ii++)
48e3e 3b 0d 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20  ;..  aiUsed[ii] 
48e3f 3d 20 31 3b 0d 0a 20 20 72 65 74 75 72 6e 20 26  = 1;..  return &
48e40 61 43 65 6c 6c 5b 69 69 5d 3b 0d 0a 7d 0d 0a 0d  aCell[ii];..}...
48e41 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./*..** Implemen
48e42 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
48e43 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66 20  near variant of 
48e44 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
48e45 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0d 0a 2a  function from..*
48e46 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0d 0a  * Guttman[84]...
48e47 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
48e48 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 28  LinearPickSeeds(
48e49 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  ..  Rtree *pRtre
48e4a 65 2c 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  e,..  RtreeCell 
48e4b 2a 61 43 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20  *aCell, ..  int 
48e4c 6e 43 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20 2a  nCell, ..  int *
48e4d 70 69 4c 65 66 74 53 65 65 64 2c 20 0d 0a 20 20  piLeftSeed, ..  
48e4e 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65 65 64  int *piRightSeed
48e4f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a  ..){..  int i;..
48e50 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20    int iLeftSeed 
48e51 3d 20 30 3b 0d 0a 20 20 69 6e 74 20 69 52 69 67  = 0;..  int iRig
48e52 68 74 53 65 65 64 20 3d 20 31 3b 0d 0a 20 20 66  htSeed = 1;..  f
48e53 6c 6f 61 74 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e  loat maxNormalIn
48e54 6e 65 72 57 69 64 74 68 20 3d 20 30 2e 30 3b 0d  nerWidth = 0.0;.
48e55 0a 0d 0a 20 20 2f 2a 20 50 69 63 6b 20 74 77 6f  ...  /* Pick two
48e56 20 22 73 65 65 64 22 20 63 65 6c 6c 73 20 66 72   "seed" cells fr
48e57 6f 6d 20 74 68 65 20 61 72 72 61 79 20 6f 66 20  om the array of 
48e58 63 65 6c 6c 73 2e 20 54 68 65 20 61 6c 67 6f 72  cells. The algor
48e59 69 74 68 6d 20 75 73 65 64 0d 0a 20 20 2a 2a 20  ithm used..  ** 
48e5a 68 65 72 65 20 69 73 20 74 68 65 20 4c 69 6e 65  here is the Line
48e5b 61 72 50 69 63 6b 53 65 65 64 73 20 61 6c 67 6f  arPickSeeds algo
48e5c 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
48e5d 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20 0d 0a 20  n[1984]. The .. 
48e5e 20 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74   ** indices of t
48e5f 68 65 20 74 77 6f 20 73 65 65 64 20 63 65 6c 6c  he two seed cell
48e60 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61  s in the array a
48e61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 63  re stored in loc
48e62 61 6c 0d 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  al..  ** variabl
48e63 65 73 20 69 4c 65 66 74 53 65 65 6b 20 61 6e 64  es iLeftSeek and
48e64 20 69 52 69 67 68 74 53 65 65 64 2e 0d 0a 20 20   iRightSeed...  
48e65 2a 2f 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  */..  for(i=0; i
48e66 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
48e67 2b 2b 29 7b 0d 0a 20 20 20 20 66 6c 6f 61 74 20  ++){..    float 
48e68 78 31 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  x1 = DCOORD(aCel
48e69 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d  l[0].aCoord[i*2]
48e6a 29 3b 0d 0a 20 20 20 20 66 6c 6f 61 74 20 78 32  );..    float x2
48e6b 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
48e6c 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d  0].aCoord[i*2+1]
48e6d 29 3b 0d 0a 20 20 20 20 66 6c 6f 61 74 20 78 33  );..    float x3
48e6e 20 3d 20 78 31 3b 0d 0a 20 20 20 20 66 6c 6f 61   = x1;..    floa
48e6f 74 20 78 34 20 3d 20 78 32 3b 0d 0a 20 20 20 20  t x4 = x2;..    
48e70 69 6e 74 20 6a 6a 3b 0d 0a 0d 0a 20 20 20 20 69  int jj;....    i
48e71 6e 74 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30  nt iCellLeft = 0
48e72 3b 0d 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ;..    int iCell
48e73 52 69 67 68 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20  Right = 0;....  
48e74 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e    for(jj=1; jj<n
48e75 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0d 0a 20 20  Cell; jj++){..  
48e76 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d      float left =
48e77 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a 6a   DCOORD(aCell[jj
48e78 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b 0d  ].aCoord[i*2]);.
48e79 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67  .      float rig
48e7a 68 74 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ht = DCOORD(aCel
48e7b 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32  l[jj].aCoord[i*2
48e7c 2b 31 5d 29 3b 0d 0a 0d 0a 20 20 20 20 20 20 69  +1]);....      i
48e7d 66 28 20 6c 65 66 74 3c 78 31 20 29 20 78 31 20  f( left<x1 ) x1 
48e7e 3d 20 6c 65 66 74 3b 0d 0a 20 20 20 20 20 20 69  = left;..      i
48e7f 66 28 20 72 69 67 68 74 3e 78 34 20 29 20 78 34  f( right>x4 ) x4
48e80 20 3d 20 72 69 67 68 74 3b 0d 0a 20 20 20 20 20   = right;..     
48e81 20 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b 0d   if( left>x3 ){.
48e82 0a 20 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65  .        x3 = le
48e83 66 74 3b 0d 0a 20 20 20 20 20 20 20 20 69 43 65  ft;..        iCe
48e84 6c 6c 52 69 67 68 74 20 3d 20 6a 6a 3b 0d 0a 20  llRight = jj;.. 
48e85 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 69 66       }..      if
48e86 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0d 0a 20  ( right<x2 ){.. 
48e87 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67 68         x2 = righ
48e88 74 3b 0d 0a 20 20 20 20 20 20 20 20 69 43 65 6c  t;..        iCel
48e89 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0d 0a 20 20 20  lLeft = jj;..   
48e8a 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20     }..    }.... 
48e8b 20 20 20 69 66 28 20 78 34 21 3d 78 31 20 29 7b     if( x4!=x1 ){
48e8c 0d 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6e 6f  ..      float no
48e8d 72 6d 61 6c 77 69 64 74 68 20 3d 20 28 78 33 20  rmalwidth = (x3 
48e8e 2d 20 78 32 29 20 2f 20 28 78 34 20 2d 20 78 31  - x2) / (x4 - x1
48e8f 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f  );..      if( no
48e90 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78 4e 6f 72  rmalwidth>maxNor
48e91 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 29 7b  malInnerWidth ){
48e92 0d 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 53  ..        iLeftS
48e93 65 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66 74 3b  eed = iCellLeft;
48e94 0d 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74  ..        iRight
48e95 53 65 65 64 20 3d 20 69 43 65 6c 6c 52 69 67 68  Seed = iCellRigh
48e96 74 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  t;..      }..   
48e97 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a 70 69   }..  }....  *pi
48e98 4c 65 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74  LeftSeed = iLeft
48e99 53 65 65 64 3b 0d 0a 20 20 2a 70 69 52 69 67 68  Seed;..  *piRigh
48e9a 74 53 65 65 64 20 3d 20 69 52 69 67 68 74 53 65  tSeed = iRightSe
48e9b 65 64 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 20 2f  ed;..}..#endif /
48e9c 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  * VARIANT_GUTTMA
48e9d 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a  N_LINEAR_SPLIT *
48e9e 2f 0d 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54  /....#if VARIANT
48e9f 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
48ea0 49 43 5f 53 50 4c 49 54 0d 0a 2f 2a 0d 0a 2a 2a  IC_SPLIT../*..**
48ea1 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
48ea2 6f 66 20 74 68 65 20 71 75 61 64 72 61 74 69 63  of the quadratic
48ea3 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20   variant of the 
48ea4 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63 74  PickNext() funct
48ea5 69 6f 6e 20 66 72 6f 6d 0d 0a 2a 2a 20 47 75 74  ion from..** Gut
48ea6 74 6d 61 6e 5b 38 34 5d 2e 0d 0a 2a 2f 0d 0a 73  tman[84]...*/..s
48ea7 74 61 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20  tatic RtreeCell 
48ea8 2a 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65  *QuadraticPickNe
48ea9 78 74 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52  xt(..  Rtree *pR
48eaa 74 72 65 65 2c 0d 0a 20 20 52 74 72 65 65 43 65  tree,..  RtreeCe
48eab 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0d 0a 20 20 69  ll *aCell, ..  i
48eac 6e 74 20 6e 43 65 6c 6c 2c 20 0d 0a 20 20 52 74  nt nCell, ..  Rt
48ead 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f  reeCell *pLeftBo
48eae 78 2c 20 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c  x, ..  RtreeCell
48eaf 20 2a 70 52 69 67 68 74 42 6f 78 2c 0d 0a 20 20   *pRightBox,..  
48eb0 69 6e 74 20 2a 61 69 55 73 65 64 0d 0a 29 7b 0d  int *aiUsed..){.
48eb1 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53 28  .  #define FABS(
48eb2 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e 30  a) ((a)<0.0?-1.0
48eb3 2a 28 61 29 3a 28 61 29 29 0d 0a 0d 0a 20 20 69  *(a):(a))....  i
48eb4 6e 74 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b  nt iSelect = -1;
48eb5 0d 0a 20 20 66 6c 6f 61 74 20 66 44 69 66 66 3b  ..  float fDiff;
48eb6 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 66  ..  int ii;..  f
48eb7 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
48eb8 6c 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 69  l; ii++){..    i
48eb9 66 28 20 61 69 55 73 65 64 5b 69 69 5d 3d 3d 30  f( aiUsed[ii]==0
48eba 20 29 7b 0d 0a 20 20 20 20 20 20 66 6c 6f 61 74   ){..      float
48ebb 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47 72 6f 77   left = cellGrow
48ebc 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  th(pRtree, pLeft
48ebd 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29  Box, &aCell[ii])
48ebe 3b 0d 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72  ;..      float r
48ebf 69 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f 77 74  ight = cellGrowt
48ec0 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 42  h(pRtree, pLeftB
48ec1 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b  ox, &aCell[ii]);
48ec2 0d 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 64 69  ..      float di
48ec3 66 66 20 3d 20 46 41 42 53 28 72 69 67 68 74 2d  ff = FABS(right-
48ec4 6c 65 66 74 29 3b 0d 0a 20 20 20 20 20 20 69 66  left);..      if
48ec5 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64  ( iSelect<0 || d
48ec6 69 66 66 3e 66 44 69 66 66 20 29 7b 0d 0a 20 20  iff>fDiff ){..  
48ec7 20 20 20 20 20 20 66 44 69 66 66 20 3d 20 64 69        fDiff = di
48ec8 66 66 3b 0d 0a 20 20 20 20 20 20 20 20 69 53 65  ff;..        iSe
48ec9 6c 65 63 74 20 3d 20 69 69 3b 0d 0a 20 20 20 20  lect = ii;..    
48eca 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d    }..    }..  }.
48ecb 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65 63  .  aiUsed[iSelec
48ecc 74 5d 20 3d 20 31 3b 0d 0a 20 20 72 65 74 75 72  t] = 1;..  retur
48ecd 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74  n &aCell[iSelect
48ece 5d 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  ];..}..../*..** 
48ecf 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
48ed0 66 20 74 68 65 20 71 75 61 64 72 61 74 69 63 20  f the quadratic 
48ed1 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50  variant of the P
48ed2 69 63 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74  ickSeeds() funct
48ed3 69 6f 6e 20 66 72 6f 6d 0d 0a 2a 2a 20 47 75 74  ion from..** Gut
48ed4 74 6d 61 6e 5b 38 34 5d 2e 0d 0a 2a 2f 0d 0a 73  tman[84]...*/..s
48ed5 74 61 74 69 63 20 76 6f 69 64 20 51 75 61 64 72  tatic void Quadr
48ed6 61 74 69 63 50 69 63 6b 53 65 65 64 73 28 0d 0a  aticPickSeeds(..
48ed7 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
48ed8 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ..  RtreeCell *a
48ed9 43 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20 6e 43  Cell, ..  int nC
48eda 65 6c 6c 2c 20 0d 0a 20 20 69 6e 74 20 2a 70 69  ell, ..  int *pi
48edb 4c 65 66 74 53 65 65 64 2c 20 0d 0a 20 20 69 6e  LeftSeed, ..  in
48edc 74 20 2a 70 69 52 69 67 68 74 53 65 65 64 0d 0a  t *piRightSeed..
48edd 29 7b 0d 0a 20 20 69 6e 74 20 69 69 3b 0d 0a 20  ){..  int ii;.. 
48ede 20 69 6e 74 20 6a 6a 3b 0d 0a 0d 0a 20 20 69 6e   int jj;....  in
48edf 74 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b  t iLeftSeed = 0;
48ee0 0d 0a 20 20 69 6e 74 20 69 52 69 67 68 74 53 65  ..  int iRightSe
48ee1 65 64 20 3d 20 31 3b 0d 0a 20 20 66 6c 6f 61 74  ed = 1;..  float
48ee2 20 66 57 61 73 74 65 20 3d 20 30 2e 30 3b 0d 0a   fWaste = 0.0;..
48ee3 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
48ee4 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0d 0a  <nCell; ii++){..
48ee5 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69 2b 31 3b      for(jj=ii+1;
48ee6 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
48ee7 7b 0d 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72  {..      float r
48ee8 69 67 68 74 20 3d 20 63 65 6c 6c 41 72 65 61 28  ight = cellArea(
48ee9 70 52 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 6a  pRtree, &aCell[j
48eea 6a 5d 29 3b 0d 0a 20 20 20 20 20 20 66 6c 6f 61  j]);..      floa
48eeb 74 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47  t growth = cellG
48eec 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26 61  rowth(pRtree, &a
48eed 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c 6c  Cell[ii], &aCell
48eee 5b 6a 6a 5d 29 3b 0d 0a 20 20 20 20 20 20 66 6c  [jj]);..      fl
48eef 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72 6f 77  oat waste = grow
48ef0 74 68 20 2d 20 72 69 67 68 74 3b 0d 0a 0d 0a 20  th - right;.... 
48ef1 20 20 20 20 20 69 66 28 20 77 61 73 74 65 3e 66       if( waste>f
48ef2 57 61 73 74 65 20 29 7b 0d 0a 20 20 20 20 20 20  Waste ){..      
48ef3 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69 69    iLeftSeed = ii
48ef4 3b 0d 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  ;..        iRigh
48ef5 74 53 65 65 64 20 3d 20 6a 6a 3b 0d 0a 20 20 20  tSeed = jj;..   
48ef6 20 20 20 20 20 66 57 61 73 74 65 20 3d 20 77 61       fWaste = wa
48ef7 73 74 65 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  ste;..      }.. 
48ef8 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2a     }..  }....  *
48ef9 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c 65  piLeftSeed = iLe
48efa 66 74 53 65 65 64 3b 0d 0a 20 20 2a 70 69 52 69  ftSeed;..  *piRi
48efb 67 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74  ghtSeed = iRight
48efc 53 65 65 64 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66  Seed;..}..#endif
48efd 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54   /* VARIANT_GUTT
48efe 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50  MAN_QUADRATIC_SP
48eff 4c 49 54 20 2a 2f 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a  LIT */..../*..**
48f00 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
48f01 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64 20 61   aDistance and a
48f02 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20  Spare all point 
48f03 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a  to arrays of siz
48f04 65 0d 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20  e..** nIdx. The 
48f05 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61  aIdx array conta
48f06 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20 69  ins the set of i
48f07 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74  ntegers from 0 t
48f08 6f 20 0d 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20  o ..** (nIdx-1) 
48f09 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72  in no particular
48f0a 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e   order. This fun
48f0b 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20  ction sorts the 
48f0c 76 61 6c 75 65 73 0d 0a 2a 2a 20 69 6e 20 61 49  values..** in aI
48f0d 64 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  dx according to 
48f0e 74 68 65 20 69 6e 64 65 78 65 64 20 76 61 6c 75  the indexed valu
48f0f 65 73 20 69 6e 20 61 44 69 73 74 61 6e 63 65 2e  es in aDistance.
48f10 20 46 6f 72 0d 0a 2a 2a 20 65 78 61 6d 70 6c 65   For..** example
48f11 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 69  , assuming the i
48f12 6e 70 75 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  nputs:..**..**  
48f13 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30   aIdx      = { 0
48f14 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20  ,   1,   2,   3 
48f15 7d 0d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63  }..**   aDistanc
48f16 65 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20  e = { 5.0, 2.0, 
48f17 37 2e 30 2c 20 36 2e 30 20 7d 0d 0a 2a 2a 0d 0a  7.0, 6.0 }..**..
48f18 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
48f19 20 73 65 74 73 20 74 68 65 20 61 49 64 78 20 61   sets the aIdx a
48f1a 72 72 61 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a  rray to contain:
48f1b 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 61 49 64 78 20  ..**..**   aIdx 
48f1c 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c       = { 0,   1,
48f1d 20 20 20 32 2c 20 20 20 33 20 7d 0d 0a 2a 2a 0d     2,   3 }..**.
48f1e 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20 61  .** The aSpare a
48f1f 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73 20  rray is used as 
48f20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e  temporary workin
48f21 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0d 0a  g space by the..
48f22 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72  ** sorting algor
48f23 69 74 68 6d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69  ithm...*/..stati
48f24 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44 69 73  c void SortByDis
48f25 74 61 6e 63 65 28 0d 0a 20 20 69 6e 74 20 2a 61  tance(..  int *a
48f26 49 64 78 2c 20 0d 0a 20 20 69 6e 74 20 6e 49 64  Idx, ..  int nId
48f27 78 2c 20 0d 0a 20 20 66 6c 6f 61 74 20 2a 61 44  x, ..  float *aD
48f28 69 73 74 61 6e 63 65 2c 20 0d 0a 20 20 69 6e 74  istance, ..  int
48f29 20 2a 61 53 70 61 72 65 0d 0a 29 7b 0d 0a 20 20   *aSpare..){..  
48f2a 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0d 0a 20  if( nIdx>1 ){.. 
48f2b 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
48f2c 3b 0d 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68  ;..    int iRigh
48f2d 74 20 3d 20 30 3b 0d 0a 0d 0a 20 20 20 20 69 6e  t = 0;....    in
48f2e 74 20 6e 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32  t nLeft = nIdx/2
48f2f 3b 0d 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68  ;..    int nRigh
48f30 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66 74 3b 0d  t = nIdx-nLeft;.
48f31 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66 74 20  .    int *aLeft 
48f32 3d 20 61 49 64 78 3b 0d 0a 20 20 20 20 69 6e 74  = aIdx;..    int
48f33 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64 78   *aRight = &aIdx
48f34 5b 6e 4c 65 66 74 5d 3b 0d 0a 0d 0a 20 20 20 20  [nLeft];....    
48f35 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61  SortByDistance(a
48f36 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44 69  Left, nLeft, aDi
48f37 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b  stance, aSpare);
48f38 0d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73 74  ..    SortByDist
48f39 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e 52 69  ance(aRight, nRi
48f3a 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ght, aDistance, 
48f3b 61 53 70 61 72 65 29 3b 0d 0a 0d 0a 20 20 20 20  aSpare);....    
48f3c 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61  memcpy(aSpare, a
48f3d 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74  Left, sizeof(int
48f3e 29 2a 6e 4c 65 66 74 29 3b 0d 0a 20 20 20 20 61  )*nLeft);..    a
48f3f 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0d 0a  Left = aSpare;..
48f40 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65  ..    while( iLe
48f41 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67  ft<nLeft || iRig
48f42 68 74 3c 6e 52 69 67 68 74 20 29 7b 0d 0a 20 20  ht<nRight ){..  
48f43 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d 6e      if( iLeft==n
48f44 4c 65 66 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  Left ){..       
48f45 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
48f46 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69  ht] = aRight[iRi
48f47 67 68 74 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69  ght];..        i
48f48 52 69 67 68 74 2b 2b 3b 0d 0a 20 20 20 20 20 20  Right++;..      
48f49 7d 65 6c 73 65 20 69 66 28 20 69 52 69 67 68 74  }else if( iRight
48f4a 3d 3d 6e 52 69 67 68 74 20 29 7b 0d 0a 20 20 20  ==nRight ){..   
48f4b 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
48f4c 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
48f4d 69 4c 65 66 74 5d 3b 0d 0a 20 20 20 20 20 20 20  iLeft];..       
48f4e 20 69 4c 65 66 74 2b 2b 3b 0d 0a 20 20 20 20 20   iLeft++;..     
48f4f 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 20   }else{..       
48f50 20 66 6c 6f 61 74 20 66 4c 65 66 74 20 3d 20 61   float fLeft = a
48f51 44 69 73 74 61 6e 63 65 5b 61 4c 65 66 74 5b 69  Distance[aLeft[i
48f52 4c 65 66 74 5d 5d 3b 0d 0a 20 20 20 20 20 20 20  Left]];..       
48f53 20 66 6c 6f 61 74 20 66 52 69 67 68 74 20 3d 20   float fRight = 
48f54 61 44 69 73 74 61 6e 63 65 5b 61 52 69 67 68 74  aDistance[aRight
48f55 5b 69 52 69 67 68 74 5d 5d 3b 0d 0a 20 20 20 20  [iRight]];..    
48f56 20 20 20 20 69 66 28 20 66 4c 65 66 74 3c 66 52      if( fLeft<fR
48f57 69 67 68 74 20 29 7b 0d 0a 20 20 20 20 20 20 20  ight ){..       
48f58 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52     aIdx[iLeft+iR
48f59 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c  ight] = aLeft[iL
48f5a 65 66 74 5d 3b 0d 0a 20 20 20 20 20 20 20 20 20  eft];..         
48f5b 20 69 4c 65 66 74 2b 2b 3b 0d 0a 20 20 20 20 20   iLeft++;..     
48f5c 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
48f5d 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
48f5e 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74  iRight] = aRight
48f5f 5b 69 52 69 67 68 74 5d 3b 0d 0a 20 20 20 20 20  [iRight];..     
48f60 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0d 0a       iRight++;..
48f61 20 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20          }..     
48f62 20 7d 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 23 69 66   }..    }....#if
48f63 20 30 0d 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b   0..    /* Check
48f64 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 20 77   that the sort w
48f65 6f 72 6b 65 64 20 2a 2f 0d 0a 20 20 20 20 7b 0d  orked */..    {.
48f66 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0d 0a  .      int jj;..
48f67 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20        for(jj=1; 
48f68 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0d  jj<nIdx; jj++){.
48f69 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 6c  .        float l
48f6a 65 66 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b  eft = aDistance[
48f6b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0d 0a 20 20  aIdx[jj-1]];..  
48f6c 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68        float righ
48f6d 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 49  t = aDistance[aI
48f6e 64 78 5b 6a 6a 5d 5d 3b 0d 0a 20 20 20 20 20 20  dx[jj]];..      
48f6f 20 20 61 73 73 65 72 74 28 20 6c 65 66 74 3c 3d    assert( left<=
48f70 72 69 67 68 74 20 29 3b 0d 0a 20 20 20 20 20 20  right );..      
48f71 7d 0d 0a 20 20 20 20 7d 0d 0a 23 65 6e 64 69 66  }..    }..#endif
48f72 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  ..  }..}..../*..
48f73 2a 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64  ** Arguments aId
48f74 78 2c 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70  x, aCell and aSp
48f75 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f  are all point to
48f76 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0d   arrays of size.
48f77 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49  .** nIdx. The aI
48f78 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  dx array contain
48f79 73 20 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74  s the set of int
48f7a 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20  egers from 0 to 
48f7b 0d 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e  ..** (nIdx-1) in
48f7c 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 6f   no particular o
48f7d 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  rder. This funct
48f7e 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20 76 61  ion sorts the va
48f7f 6c 75 65 73 0d 0a 2a 2a 20 69 6e 20 61 49 64 78  lues..** in aIdx
48f80 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 64 69   according to di
48f81 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 6f 66 20  mension iDim of 
48f82 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 61 43 65  the cells in aCe
48f83 6c 6c 2e 20 54 68 65 0d 0a 2a 2a 20 6d 69 6e 69  ll. The..** mini
48f84 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 64 69 6d  mum value of dim
48f85 65 6e 73 69 6f 6e 20 69 44 69 6d 20 69 73 20 63  ension iDim is c
48f86 6f 6e 73 69 64 65 72 65 64 20 66 69 72 73 74 2c  onsidered first,
48f87 20 74 68 65 0d 0a 2a 2a 20 6d 61 78 69 6d 75 6d   the..** maximum
48f88 20 75 73 65 64 20 74 6f 20 62 72 65 61 6b 20 74   used to break t
48f89 69 65 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65  ies...**..** The
48f8a 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69 73   aSpare array is
48f8b 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61   used as tempora
48f8c 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65  ry working space
48f8d 20 62 79 20 74 68 65 0d 0a 2a 2a 20 73 6f 72 74   by the..** sort
48f8e 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0d 0a  ing algorithm...
48f8f 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
48f90 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28  SortByDimension(
48f91 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  ..  Rtree *pRtre
48f92 65 2c 0d 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c  e,..  int *aIdx,
48f93 20 0d 0a 20 20 69 6e 74 20 6e 49 64 78 2c 20 0d   ..  int nIdx, .
48f94 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0d 0a 20  .  int iDim, .. 
48f95 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
48f96 6c 2c 20 0d 0a 20 20 69 6e 74 20 2a 61 53 70 61  l, ..  int *aSpa
48f97 72 65 0d 0a 29 7b 0d 0a 20 20 69 66 28 20 6e 49  re..){..  if( nI
48f98 64 78 3e 31 20 29 7b 0d 0a 0d 0a 20 20 20 20 69  dx>1 ){....    i
48f99 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b 0d 0a 20  nt iLeft = 0;.. 
48f9a 20 20 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20     int iRight = 
48f9b 30 3b 0d 0a 0d 0a 20 20 20 20 69 6e 74 20 6e 4c  0;....    int nL
48f9c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0d 0a 20  eft = nIdx/2;.. 
48f9d 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
48f9e 6e 49 64 78 2d 6e 4c 65 66 74 3b 0d 0a 20 20 20  nIdx-nLeft;..   
48f9f 20 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49   int *aLeft = aI
48fa0 64 78 3b 0d 0a 20 20 20 20 69 6e 74 20 2a 61 52  dx;..    int *aR
48fa1 69 67 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65  ight = &aIdx[nLe
48fa2 66 74 5d 3b 0d 0a 0d 0a 20 20 20 20 53 6f 72 74  ft];....    Sort
48fa3 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72  ByDimension(pRtr
48fa4 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74  ee, aLeft, nLeft
48fa5 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61  , iDim, aCell, a
48fa6 53 70 61 72 65 29 3b 0d 0a 20 20 20 20 53 6f 72  Spare);..    Sor
48fa7 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74  tByDimension(pRt
48fa8 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e 52 69  ree, aRight, nRi
48fa9 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c  ght, iDim, aCell
48faa 2c 20 61 53 70 61 72 65 29 3b 0d 0a 0d 0a 20 20  , aSpare);....  
48fab 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c    memcpy(aSpare,
48fac 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69   aLeft, sizeof(i
48fad 6e 74 29 2a 6e 4c 65 66 74 29 3b 0d 0a 20 20 20  nt)*nLeft);..   
48fae 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b   aLeft = aSpare;
48faf 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65  ..    while( iLe
48fb0 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67  ft<nLeft || iRig
48fb1 68 74 3c 6e 52 69 67 68 74 20 29 7b 0d 0a 20 20  ht<nRight ){..  
48fb2 20 20 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74      double xleft
48fb3 31 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c  1 = DCOORD(aCell
48fb4 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61  [aLeft[iLeft]].a
48fb5 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0d  Coord[iDim*2]);.
48fb6 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 6c  .      double xl
48fb7 65 66 74 32 20 3d 20 44 43 4f 4f 52 44 28 61 43  eft2 = DCOORD(aC
48fb8 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ell[aLeft[iLeft]
48fb9 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b  ].aCoord[iDim*2+
48fba 31 5d 29 3b 0d 0a 20 20 20 20 20 20 64 6f 75 62  1]);..      doub
48fbb 6c 65 20 78 72 69 67 68 74 31 20 3d 20 44 43 4f  le xright1 = DCO
48fbc 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74  ORD(aCell[aRight
48fbd 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64  [iRight]].aCoord
48fbe 5b 69 44 69 6d 2a 32 5d 29 3b 0d 0a 20 20 20 20  [iDim*2]);..    
48fbf 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 32    double xright2
48fc0 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
48fc1 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
48fc2 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
48fc3 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 28 69  );..      if( (i
48fc4 4c 65 66 74 21 3d 6e 4c 65 66 74 29 20 26 26 20  Left!=nLeft) && 
48fc5 28 28 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74  ((iRight==nRight
48fc6 29 0d 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c  )..       || (xl
48fc7 65 66 74 31 3c 78 72 69 67 68 74 31 29 0d 0a 20  eft1<xright1).. 
48fc8 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66 74 31        || (xleft1
48fc9 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78 6c 65  ==xright1 && xle
48fca 66 74 32 3c 78 72 69 67 68 74 32 29 0d 0a 20 20  ft2<xright2)..  
48fcb 20 20 20 20 29 29 7b 0d 0a 20 20 20 20 20 20 20      )){..       
48fcc 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
48fcd 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
48fce 74 5d 3b 0d 0a 20 20 20 20 20 20 20 20 69 4c 65  t];..        iLe
48fcf 66 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 65 6c  ft++;..      }el
48fd0 73 65 7b 0d 0a 20 20 20 20 20 20 20 20 61 49 64  se{..        aId
48fd1 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20  x[iLeft+iRight] 
48fd2 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d  = aRight[iRight]
48fd3 3b 0d 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  ;..        iRigh
48fd4 74 2b 2b 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  t++;..      }.. 
48fd5 20 20 20 7d 0d 0a 0d 0a 23 69 66 20 30 0d 0a 20     }....#if 0.. 
48fd6 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74     /* Check that
48fd7 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b 65 64   the sort worked
48fd8 20 2a 2f 0d 0a 20 20 20 20 7b 0d 0a 20 20 20 20   */..    {..    
48fd9 20 20 69 6e 74 20 6a 6a 3b 0d 0a 20 20 20 20 20    int jj;..     
48fda 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49   for(jj=1; jj<nI
48fdb 64 78 3b 20 6a 6a 2b 2b 29 7b 0d 0a 20 20 20 20  dx; jj++){..    
48fdc 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74 31      float xleft1
48fdd 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
48fde 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  -1]].aCoord[iDim
48fdf 2a 32 5d 3b 0d 0a 20 20 20 20 20 20 20 20 66 6c  *2];..        fl
48fe0 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61 43 65  oat xleft2 = aCe
48fe1 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61  ll[aIdx[jj-1]].a
48fe2 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b  Coord[iDim*2+1];
48fe3 0d 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  ..        float 
48fe4 78 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c 5b  xright1 = aCell[
48fe5 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64  aIdx[jj]].aCoord
48fe6 5b 69 44 69 6d 2a 32 5d 3b 0d 0a 20 20 20 20 20  [iDim*2];..     
48fe7 20 20 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32     float xright2
48fe8 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
48fe9 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
48fea 2b 31 5d 3b 0d 0a 20 20 20 20 20 20 20 20 61 73  +1];..        as
48feb 73 65 72 74 28 20 78 6c 65 66 74 31 3c 3d 78 72  sert( xleft1<=xr
48fec 69 67 68 74 31 20 26 26 20 28 78 6c 65 66 74 31  ight1 && (xleft1
48fed 3c 78 72 69 67 68 74 31 20 7c 7c 20 78 6c 65 66  <xright1 || xlef
48fee 74 32 3c 3d 78 72 69 67 68 74 32 29 20 29 3b 0d  t2<=xright2) );.
48fef 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
48ff0 0a 23 65 6e 64 69 66 0d 0a 20 20 7d 0d 0a 7d 0d  .#endif..  }..}.
48ff1 0a 0d 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ...#if VARIANT_R
48ff2 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 0d 0a  STARTREE_SPLIT..
48ff3 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  /*..** Implement
48ff4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 2a 2d  ation of the R*-
48ff5 74 72 65 65 20 76 61 72 69 61 6e 74 20 6f 66 20  tree variant of 
48ff6 53 70 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 42  SplitNode from B
48ff7 65 63 6b 6d 61 6e 5b 31 39 39 30 5d 2e 0d 0a 2a  eckman[1990]...*
48ff8 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 70  /..static int sp
48ff9 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 28  litNodeStartree(
48ffa 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  ..  Rtree *pRtre
48ffb 65 2c 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  e,..  RtreeCell 
48ffc 2a 61 43 65 6c 6c 2c 0d 0a 20 20 69 6e 74 20 6e  *aCell,..  int n
48ffd 43 65 6c 6c 2c 0d 0a 20 20 52 74 72 65 65 4e 6f  Cell,..  RtreeNo
48ffe 64 65 20 2a 70 4c 65 66 74 2c 0d 0a 20 20 52 74  de *pLeft,..  Rt
48fff 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c  reeNode *pRight,
49000 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ..  RtreeCell *p
49001 42 62 6f 78 4c 65 66 74 2c 0d 0a 20 20 52 74 72  BboxLeft,..  Rtr
49002 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67  eeCell *pBboxRig
49003 68 74 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 2a 2a  ht..){..  int **
49004 61 61 53 6f 72 74 65 64 3b 0d 0a 20 20 69 6e 74  aaSorted;..  int
49005 20 2a 61 53 70 61 72 65 3b 0d 0a 20 20 69 6e 74   *aSpare;..  int
49006 20 69 69 3b 0d 0a 0d 0a 20 20 69 6e 74 20 69 42   ii;....  int iB
49007 65 73 74 44 69 6d 20 3d 20 30 3b 0d 0a 20 20 69  estDim = 0;..  i
49008 6e 74 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20  nt iBestSplit = 
49009 30 3b 0d 0a 20 20 66 6c 6f 61 74 20 66 42 65 73  0;..  float fBes
4900a 74 4d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0d 0a  tMargin = 0.0;..
4900b 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  ..  int nByte = 
4900c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2b 31 29  (pRtree->nDim+1)
4900d 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a 29 2b 6e  *(sizeof(int*)+n
4900e 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29  Cell*sizeof(int)
4900f 29 3b 0d 0a 0d 0a 20 20 61 61 53 6f 72 74 65 64  );....  aaSorted
49010 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69 74   = (int **)sqlit
49011 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  e3_malloc(nByte)
49012 3b 0d 0a 20 20 69 66 28 20 21 61 61 53 6f 72 74  ;..  if( !aaSort
49013 65 64 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ed ){..    retur
49014 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
49015 0a 20 20 7d 0d 0a 0d 0a 20 20 61 53 70 61 72 65  .  }....  aSpare
49016 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61 61 53   = &((int *)&aaS
49017 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e 44  orted[pRtree->nD
49018 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e 6e 44 69  im])[pRtree->nDi
49019 6d 2a 6e 43 65 6c 6c 5d 3b 0d 0a 20 20 6d 65 6d  m*nCell];..  mem
4901a 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20 30 2c  set(aaSorted, 0,
4901b 20 6e 42 79 74 65 29 3b 0d 0a 20 20 66 6f 72 28   nByte);..  for(
4901c 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65 2d  ii=0; ii<pRtree-
4901d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0d 0a 20  >nDim; ii++){.. 
4901e 20 20 20 69 6e 74 20 6a 6a 3b 0d 0a 20 20 20 20     int jj;..    
4901f 61 61 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26  aaSorted[ii] = &
49020 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65  ((int *)&aaSorte
49021 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29  d[pRtree->nDim])
49022 5b 69 69 2a 6e 43 65 6c 6c 5d 3b 0d 0a 20 20 20  [ii*nCell];..   
49023 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43   for(jj=0; jj<nC
49024 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0d 0a 20 20 20  ell; jj++){..   
49025 20 20 20 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b     aaSorted[ii][
49026 6a 6a 5d 20 3d 20 6a 6a 3b 0d 0a 20 20 20 20 7d  jj] = jj;..    }
49027 0d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65  ..    SortByDime
49028 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 61  nsion(pRtree, aa
49029 53 6f 72 74 65 64 5b 69 69 5d 2c 20 6e 43 65 6c  Sorted[ii], nCel
4902a 6c 2c 20 69 69 2c 20 61 43 65 6c 6c 2c 20 61 53  l, ii, aCell, aS
4902b 70 61 72 65 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20  pare);..  }.... 
4902c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52   for(ii=0; ii<pR
4902d 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b  tree->nDim; ii++
4902e 29 7b 0d 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61  ){..    float ma
4902f 72 67 69 6e 20 3d 20 30 2e 30 3b 0d 0a 20 20 20  rgin = 0.0;..   
49030 20 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65 72   float fBestOver
49031 6c 61 70 20 3d 20 30 2e 30 3b 0d 0a 20 20 20 20  lap = 0.0;..    
49032 66 6c 6f 61 74 20 66 42 65 73 74 41 72 65 61 20  float fBestArea 
49033 3d 20 30 2e 30 3b 0d 0a 20 20 20 20 69 6e 74 20  = 0.0;..    int 
49034 69 42 65 73 74 4c 65 66 74 20 3d 20 30 3b 0d 0a  iBestLeft = 0;..
49035 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 3b 0d 0a      int nLeft;..
49036 0d 0a 20 20 20 20 66 6f 72 28 0d 0a 20 20 20 20  ..    for(..    
49037 20 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49    nLeft=RTREE_MI
49038 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20  NCELLS(pRtree); 
49039 0d 0a 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28  ..      nLeft<=(
4903a 6e 43 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43  nCell-RTREE_MINC
4903b 45 4c 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0d  ELLS(pRtree)); .
4903c 0a 20 20 20 20 20 20 6e 4c 65 66 74 2b 2b 0d 0a  .      nLeft++..
4903d 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20 52 74      ){..      Rt
4903e 72 65 65 43 65 6c 6c 20 6c 65 66 74 3b 0d 0a 20  reeCell left;.. 
4903f 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 72       RtreeCell r
49040 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 69 6e 74  ight;..      int
49041 20 6b 6b 3b 0d 0a 20 20 20 20 20 20 66 6c 6f 61   kk;..      floa
49042 74 20 6f 76 65 72 6c 61 70 3b 0d 0a 20 20 20 20  t overlap;..    
49043 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0d 0a 0d    float area;...
49044 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 6c  .      memcpy(&l
49045 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f  eft, &aCell[aaSo
49046 72 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69  rted[ii][0]], si
49047 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
49048 3b 0d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ;..      memcpy(
49049 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61  &right, &aCell[a
4904a 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c  aSorted[ii][nCel
4904b 6c 2d 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  l-1]], sizeof(Rt
4904c 72 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 20 20  reeCell));..    
4904d 20 20 66 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28    for(kk=1; kk<(
4904e 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b  nCell-1); kk++){
4904f 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b 6b  ..        if( kk
49050 3c 6e 4c 65 66 74 20 29 7b 0d 0a 20 20 20 20 20  <nLeft ){..     
49051 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70       cellUnion(p
49052 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 61  Rtree, &left, &a
49053 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
49054 5d 5b 6b 6b 5d 5d 29 3b 0d 0a 20 20 20 20 20 20  ][kk]]);..      
49055 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
49056 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
49057 74 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61  tree, &right, &a
49058 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
49059 5d 5b 6b 6b 5d 5d 29 3b 0d 0a 20 20 20 20 20 20  ][kk]]);..      
4905a 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
4905b 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65      margin += ce
4905c 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72 65 65 2c  llMargin(pRtree,
4905d 20 26 6c 65 66 74 29 3b 0d 0a 20 20 20 20 20 20   &left);..      
4905e 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61  margin += cellMa
4905f 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 72 69  rgin(pRtree, &ri
49060 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 6f 76 65  ght);..      ove
49061 72 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c  rlap = cellOverl
49062 61 70 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ap(pRtree, &left
49063 2c 20 26 72 69 67 68 74 2c 20 31 2c 20 2d 31 29  , &right, 1, -1)
49064 3b 0d 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20  ;..      area = 
49065 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
49066 20 26 6c 65 66 74 29 20 2b 20 63 65 6c 6c 41 72   &left) + cellAr
49067 65 61 28 70 52 74 72 65 65 2c 20 26 72 69 67 68  ea(pRtree, &righ
49068 74 29 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 28  t);..      if( (
49069 6e 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d 49 4e  nLeft==RTREE_MIN
4906a 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0d 0a  CELLS(pRtree))..
4906b 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
4906c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c 61 70 29  ap<fBestOverlap)
4906d 0d 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65  ..       || (ove
4906e 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c  rlap==fBestOverl
4906f 61 70 20 26 26 20 61 72 65 61 3c 66 42 65 73 74  ap && area<fBest
49070 41 72 65 61 29 0d 0a 20 20 20 20 20 20 29 7b 0d  Area)..      ){.
49071 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 4c 65  .        iBestLe
49072 66 74 20 3d 20 6e 4c 65 66 74 3b 0d 0a 20 20 20  ft = nLeft;..   
49073 20 20 20 20 20 66 42 65 73 74 4f 76 65 72 6c 61       fBestOverla
49074 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0d 0a 20 20  p = overlap;..  
49075 20 20 20 20 20 20 66 42 65 73 74 41 72 65 61 20        fBestArea 
49076 3d 20 61 72 65 61 3b 0d 0a 20 20 20 20 20 20 7d  = area;..      }
49077 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20 69  ..    }....    i
49078 66 28 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67  f( ii==0 || marg
49079 69 6e 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29  in<fBestMargin )
4907a 7b 0d 0a 20 20 20 20 20 20 69 42 65 73 74 44 69  {..      iBestDi
4907b 6d 20 3d 20 69 69 3b 0d 0a 20 20 20 20 20 20 66  m = ii;..      f
4907c 42 65 73 74 4d 61 72 67 69 6e 20 3d 20 6d 61 72  BestMargin = mar
4907d 67 69 6e 3b 0d 0a 20 20 20 20 20 20 69 42 65 73  gin;..      iBes
4907e 74 53 70 6c 69 74 20 3d 20 69 42 65 73 74 4c 65  tSplit = iBestLe
4907f 66 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  ft;..    }..  }.
49080 0a 0d 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f  ...  memcpy(pBbo
49081 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61  xLeft, &aCell[aa
49082 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d  Sorted[iBestDim]
49083 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72  [0]], sizeof(Rtr
49084 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 6d 65 6d  eeCell));..  mem
49085 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
49086 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
49087 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73 74 53  iBestDim][iBestS
49088 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52  plit]], sizeof(R
49089 74 72 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 66  treeCell));..  f
4908a 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
4908b 6c 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20 52  l; ii++){..    R
4908c 74 72 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65  treeNode *pTarge
4908d 74 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  t = (ii<iBestSpl
4908e 69 74 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74  it)?pLeft:pRight
4908f 3b 0d 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  ;..    RtreeCell
49090 20 2a 70 42 62 6f 78 20 3d 20 28 69 69 3c 69 42   *pBbox = (ii<iB
49091 65 73 74 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c  estSplit)?pBboxL
49092 65 66 74 3a 70 42 62 6f 78 52 69 67 68 74 3b 0d  eft:pBboxRight;.
49093 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  .    RtreeCell *
49094 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c 6c 5b 61  pCell = &aCell[a
49095 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d  aSorted[iBestDim
49096 5d 5b 69 69 5d 5d 3b 0d 0a 20 20 20 20 6e 6f 64  ][ii]];..    nod
49097 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
49098 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70 43 65  ee, pTarget, pCe
49099 6c 6c 29 3b 0d 0a 20 20 20 20 63 65 6c 6c 55 6e  ll);..    cellUn
4909a 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
4909b 78 2c 20 70 43 65 6c 6c 29 3b 0d 0a 20 20 7d 0d  x, pCell);..  }.
4909c 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ...  sqlite3_fre
4909d 65 28 61 61 53 6f 72 74 65 64 29 3b 0d 0a 20 20  e(aaSorted);..  
4909e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
4909f 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
490a0 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54  #if VARIANT_GUTT
490a1 4d 41 4e 5f 53 50 4c 49 54 0d 0a 2f 2a 0d 0a 2a  MAN_SPLIT../*..*
490a2 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
490a3 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20   of the regular 
490a4 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f 64 65  R-tree SplitNode
490a5 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39   from Guttman[19
490a6 38 34 5d 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  84]...*/..static
490a7 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75   int splitNodeGu
490a8 74 74 6d 61 6e 28 0d 0a 20 20 52 74 72 65 65 20  ttman(..  Rtree 
490a9 2a 70 52 74 72 65 65 2c 0d 0a 20 20 52 74 72 65  *pRtree,..  Rtre
490aa 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0d 0a 20  eCell *aCell,.. 
490ab 20 69 6e 74 20 6e 43 65 6c 6c 2c 0d 0a 20 20 52   int nCell,..  R
490ac 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c  treeNode *pLeft,
490ad 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ..  RtreeNode *p
490ae 52 69 67 68 74 2c 0d 0a 20 20 52 74 72 65 65 43  Right,..  RtreeC
490af 65 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0d  ell *pBboxLeft,.
490b0 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42  .  RtreeCell *pB
490b1 62 6f 78 52 69 67 68 74 0d 0a 29 7b 0d 0a 20 20  boxRight..){..  
490b2 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d 20  int iLeftSeed = 
490b3 30 3b 0d 0a 20 20 69 6e 74 20 69 52 69 67 68 74  0;..  int iRight
490b4 53 65 65 64 20 3d 20 31 3b 0d 0a 20 20 69 6e 74  Seed = 1;..  int
490b5 20 2a 61 69 55 73 65 64 3b 0d 0a 20 20 69 6e 74   *aiUsed;..  int
490b6 20 69 3b 0d 0a 0d 0a 20 20 61 69 55 73 65 64 20   i;....  aiUsed 
490b7 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
490b8 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65  (sizeof(int)*nCe
490b9 6c 6c 29 3b 0d 0a 20 20 69 66 28 20 21 61 69 55  ll);..  if( !aiU
490ba 73 65 64 20 29 7b 0d 0a 20 20 20 20 72 65 74 75  sed ){..    retu
490bb 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
490bc 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d 73 65 74 28  ..  }..  memset(
490bd 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
490be 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0d 0a  f(int)*nCell);..
490bf 0d 0a 20 20 50 69 63 6b 53 65 65 64 73 28 70 52  ..  PickSeeds(pR
490c0 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  tree, aCell, nCe
490c1 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65 64 2c 20  ll, &iLeftSeed, 
490c2 26 69 52 69 67 68 74 53 65 65 64 29 3b 0d 0a 0d  &iRightSeed);...
490c3 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c  .  memcpy(pBboxL
490c4 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66  eft, &aCell[iLef
490c5 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52  tSeed], sizeof(R
490c6 74 72 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 6d  treeCell));..  m
490c7 65 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74  emcpy(pBboxRight
490c8 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53  , &aCell[iRightS
490c9 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72  eed], sizeof(Rtr
490ca 65 65 43 65 6c 6c 29 29 3b 0d 0a 20 20 6e 6f 64  eeCell));..  nod
490cb 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
490cc 65 65 2c 20 70 4c 65 66 74 2c 20 26 61 43 65 6c  ee, pLeft, &aCel
490cd 6c 5b 69 4c 65 66 74 53 65 65 64 5d 29 3b 0d 0a  l[iLeftSeed]);..
490ce 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c    nodeInsertCell
490cf 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c  (pRtree, pRight,
490d0 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65   &aCell[iRightSe
490d1 65 64 5d 29 3b 0d 0a 20 20 61 69 55 73 65 64 5b  ed]);..  aiUsed[
490d2 69 4c 65 66 74 53 65 65 64 5d 20 3d 20 31 3b 0d  iLeftSeed] = 1;.
490d3 0a 20 20 61 69 55 73 65 64 5b 69 52 69 67 68 74  .  aiUsed[iRight
490d4 53 65 65 64 5d 20 3d 20 31 3b 0d 0a 0d 0a 20 20  Seed] = 1;....  
490d5 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20 69  for(i=nCell-2; i
490d6 3e 30 3b 20 69 2d 2d 29 7b 0d 0a 20 20 20 20 52  >0; i--){..    R
490d7 74 72 65 65 43 65 6c 6c 20 2a 70 4e 65 78 74 3b  treeCell *pNext;
490d8 0d 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 50 69  ..    pNext = Pi
490d9 63 6b 4e 65 78 74 28 70 52 74 72 65 65 2c 20 61  ckNext(pRtree, a
490da 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 42 62  Cell, nCell, pBb
490db 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78 52 69 67  oxLeft, pBboxRig
490dc 68 74 2c 20 61 69 55 73 65 64 29 3b 0d 0a 20 20  ht, aiUsed);..  
490dd 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 20    float diff =  
490de 0d 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77  ..      cellGrow
490df 74 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78  th(pRtree, pBbox
490e0 4c 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20 0d  Left, pNext) - .
490e1 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
490e2 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52  h(pRtree, pBboxR
490e3 69 67 68 74 2c 20 70 4e 65 78 74 29 0d 0a 20 20  ight, pNext)..  
490e4 20 20 3b 0d 0a 20 20 20 20 69 66 28 20 28 52 54    ;..    if( (RT
490e5 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
490e6 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67 68  ree)-NCELL(pRigh
490e7 74 29 3d 3d 69 29 0d 0a 20 20 20 20 20 7c 7c 20  t)==i)..     || 
490e8 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28 52 54  (diff>0.0 && (RT
490e9 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
490ea 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74  ree)-NCELL(pLeft
490eb 29 21 3d 69 29 29 0d 0a 20 20 20 20 29 7b 0d 0a  )!=i))..    ){..
490ec 20 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74        nodeInsert
490ed 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69  Cell(pRtree, pRi
490ee 67 68 74 2c 20 70 4e 65 78 74 29 3b 0d 0a 20 20  ght, pNext);..  
490ef 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
490f0 74 72 65 65 2c 20 70 42 62 6f 78 52 69 67 68 74  tree, pBboxRight
490f1 2c 20 70 4e 65 78 74 29 3b 0d 0a 20 20 20 20 7d  , pNext);..    }
490f2 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 6e 6f 64  else{..      nod
490f3 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
490f4 65 65 2c 20 70 4c 65 66 74 2c 20 70 4e 65 78 74  ee, pLeft, pNext
490f5 29 3b 0d 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  );..      cellUn
490f6 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
490f7 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0d 0a  xLeft, pNext);..
490f8 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20      }..  }....  
490f9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 69 55  sqlite3_free(aiU
490fa 73 65 64 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20  sed);..  return 
490fb 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 23  SQLITE_OK;..}..#
490fc 65 6e 64 69 66 0d 0a 0d 0a 73 74 61 74 69 63 20  endif....static 
490fd 69 6e 74 20 75 70 64 61 74 65 4d 61 70 70 69 6e  int updateMappin
490fe 67 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74  g(..  Rtree *pRt
490ff 72 65 65 2c 20 0d 0a 20 20 69 36 34 20 69 52 6f  ree, ..  i64 iRo
49100 77 69 64 2c 20 0d 0a 20 20 52 74 72 65 65 4e 6f  wid, ..  RtreeNo
49101 64 65 20 2a 70 4e 6f 64 65 2c 20 0d 0a 20 20 69  de *pNode, ..  i
49102 6e 74 20 69 48 65 69 67 68 74 0d 0a 29 7b 0d 0a  nt iHeight..){..
49103 20 20 69 6e 74 20 28 2a 78 53 65 74 4d 61 70 70    int (*xSetMapp
49104 69 6e 67 29 28 52 74 72 65 65 20 2a 2c 20 73 71  ing)(Rtree *, sq
49105 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 73 71 6c  lite3_int64, sql
49106 69 74 65 33 5f 69 6e 74 36 34 29 3b 0d 0a 20 20  ite3_int64);..  
49107 78 53 65 74 4d 61 70 70 69 6e 67 20 3d 20 28 28  xSetMapping = ((
49108 69 48 65 69 67 68 74 3d 3d 30 29 3f 72 6f 77 69  iHeight==0)?rowi
49109 64 57 72 69 74 65 3a 70 61 72 65 6e 74 57 72 69  dWrite:parentWri
4910a 74 65 29 3b 0d 0a 20 20 69 66 28 20 69 48 65 69  te);..  if( iHei
4910b 67 68 74 3e 30 20 29 7b 0d 0a 20 20 20 20 52 74  ght>0 ){..    Rt
4910c 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20  reeNode *pChild 
4910d 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70  = nodeHashLookup
4910e 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 29  (pRtree, iRowid)
4910f 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c  ;..    if( pChil
49110 64 20 29 7b 0d 0a 20 20 20 20 20 20 6e 6f 64 65  d ){..      node
49111 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
49112 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29  pChild->pParent)
49113 3b 0d 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  ;..      nodeRef
49114 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0d 0a  erence(pNode);..
49115 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50        pChild->pP
49116 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0d 0a  arent = pNode;..
49117 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 72 65      }..  }..  re
49118 74 75 72 6e 20 78 53 65 74 4d 61 70 70 69 6e 67  turn xSetMapping
49119 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
4911a 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d   pNode->iNode);.
4911b 0a 7d 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74  .}....static int
4911c 20 53 70 6c 69 74 4e 6f 64 65 28 0d 0a 20 20 52   SplitNode(..  R
4911d 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0d 0a 20  tree *pRtree,.. 
4911e 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
4911f 65 2c 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  e,..  RtreeCell 
49120 2a 70 43 65 6c 6c 2c 0d 0a 20 20 69 6e 74 20 69  *pCell,..  int i
49121 48 65 69 67 68 74 0d 0a 29 7b 0d 0a 20 20 69 6e  Height..){..  in
49122 74 20 69 3b 0d 0a 20 20 69 6e 74 20 6e 65 77 43  t i;..  int newC
49123 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 30 3b 0d  ellIsRight = 0;.
49124 0a 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ...  int rc = SQ
49125 4c 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74 20  LITE_OK;..  int 
49126 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
49127 6f 64 65 29 3b 0d 0a 20 20 52 74 72 65 65 43 65  ode);..  RtreeCe
49128 6c 6c 20 2a 61 43 65 6c 6c 3b 0d 0a 20 20 69 6e  ll *aCell;..  in
49129 74 20 2a 61 69 55 73 65 64 3b 0d 0a 0d 0a 20 20  t *aiUsed;....  
4912a 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
4912b 20 3d 20 30 3b 0d 0a 20 20 52 74 72 65 65 4e 6f   = 0;..  RtreeNo
4912c 64 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0d  de *pRight = 0;.
4912d 0a 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 6c  ...  RtreeCell l
4912e 65 66 74 62 62 6f 78 3b 0d 0a 20 20 52 74 72 65  eftbbox;..  Rtre
4912f 65 43 65 6c 6c 20 72 69 67 68 74 62 62 6f 78 3b  eCell rightbbox;
49130 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ....  /* Allocat
49131 65 20 61 6e 20 61 72 72 61 79 20 61 6e 64 20 70  e an array and p
49132 6f 70 75 6c 61 74 65 20 69 74 20 77 69 74 68 20  opulate it with 
49133 61 20 63 6f 70 79 20 6f 66 20 70 43 65 6c 6c 20  a copy of pCell 
49134 61 6e 64 20 0d 0a 20 20 2a 2a 20 61 6c 6c 20 63  and ..  ** all c
49135 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f 64 65 20 70  ells from node p
49136 4c 65 66 74 2e 20 54 68 65 6e 20 7a 65 72 6f 20  Left. Then zero 
49137 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6e 6f 64  the original nod
49138 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 61 43 65 6c  e...  */..  aCel
49139 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  l = sqlite3_mall
4913a 6f 63 28 28 73 69 7a 65 6f 66 28 52 74 72 65 65  oc((sizeof(Rtree
4913b 43 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69 6e 74  Cell)+sizeof(int
4913c 29 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0d 0a  ))*(nCell+1));..
4913d 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0d    if( !aCell ){.
4913e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
4913f 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 67 6f 74  _NOMEM;..    got
49140 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b  o splitnode_out;
49141 0d 0a 20 20 7d 0d 0a 20 20 61 69 55 73 65 64 20  ..  }..  aiUsed 
49142 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b  = (int *)&aCell[
49143 6e 43 65 6c 6c 2b 31 5d 3b 0d 0a 20 20 6d 65 6d  nCell+1];..  mem
49144 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73  set(aiUsed, 0, s
49145 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65 6c  izeof(int)*(nCel
49146 6c 2b 31 29 29 3b 0d 0a 20 20 66 6f 72 28 69 3d  l+1));..  for(i=
49147 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
49148 7b 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  {..    nodeGetCe
49149 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
4914a 2c 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29 3b  , i, &aCell[i]);
4914b 0d 0a 20 20 7d 0d 0a 20 20 6e 6f 64 65 5a 65 72  ..  }..  nodeZer
4914c 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  o(pRtree, pNode)
4914d 3b 0d 0a 20 20 6d 65 6d 63 70 79 28 26 61 43 65  ;..  memcpy(&aCe
4914e 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c  ll[nCell], pCell
4914f 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
49150 6c 6c 29 29 3b 0d 0a 20 20 6e 43 65 6c 6c 2b 2b  ll));..  nCell++
49151 3b 0d 0a 0d 0a 20 20 69 66 28 20 70 4e 6f 64 65  ;....  if( pNode
49152 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0d 0a 20  ->iNode==1 ){.. 
49153 20 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65     pRight = node
49154 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  New(pRtree, pNod
49155 65 29 3b 0d 0a 20 20 20 20 70 4c 65 66 74 20 3d  e);..    pLeft =
49156 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
49157 20 70 4e 6f 64 65 29 3b 0d 0a 20 20 20 20 70 52   pNode);..    pR
49158 74 72 65 65 2d 3e 69 44 65 70 74 68 2b 2b 3b 0d  tree->iDepth++;.
49159 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  .    pNode->isDi
4915a 72 74 79 20 3d 20 31 3b 0d 0a 20 20 20 20 77 72  rty = 1;..    wr
4915b 69 74 65 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e  iteInt16(pNode->
4915c 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69  zData, pRtree->i
4915d 44 65 70 74 68 29 3b 0d 0a 20 20 7d 65 6c 73 65  Depth);..  }else
4915e 7b 0d 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70  {..    pLeft = p
4915f 4e 6f 64 65 3b 0d 0a 20 20 20 20 70 52 69 67 68  Node;..    pRigh
49160 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
49161 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65  ee, pLeft->pPare
49162 6e 74 29 3b 0d 0a 20 20 20 20 6e 6f 64 65 52 65  nt);..    nodeRe
49163 66 65 72 65 6e 63 65 28 70 4c 65 66 74 29 3b 0d  ference(pLeft);.
49164 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 70  .  }....  if( !p
49165 4c 65 66 74 20 7c 7c 20 21 70 52 69 67 68 74 20  Left || !pRight 
49166 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ){..    rc = SQL
49167 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20  ITE_NOMEM;..    
49168 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
49169 75 74 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 6d 65  ut;..  }....  me
4916a 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61 74  mset(pLeft->zDat
4916b 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  a, 0, pRtree->iN
4916c 6f 64 65 53 69 7a 65 29 3b 0d 0a 20 20 6d 65 6d  odeSize);..  mem
4916d 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44 61 74  set(pRight->zDat
4916e 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  a, 0, pRtree->iN
4916f 6f 64 65 53 69 7a 65 29 3b 0d 0a 0d 0a 20 20 72  odeSize);....  r
49170 63 20 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28  c = AssignCells(
49171 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e  pRtree, aCell, n
49172 43 65 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69  Cell, pLeft, pRi
49173 67 68 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ght, &leftbbox, 
49174 26 72 69 67 68 74 62 62 6f 78 29 3b 0d 0a 20 20  &rightbbox);..  
49175 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
49176 4b 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 73  K ){..    goto s
49177 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0d 0a 20  plitnode_out;.. 
49178 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 45 6e 73 75 72   }....  /* Ensur
49179 65 20 62 6f 74 68 20 63 68 69 6c 64 20 6e 6f 64  e both child nod
4917a 65 73 20 68 61 76 65 20 6e 6f 64 65 20 6e 75 6d  es have node num
4917b 62 65 72 73 20 61 73 73 69 67 6e 65 64 20 74 6f  bers assigned to
4917c 20 74 68 65 6d 20 62 79 20 63 61 6c 6c 69 6e 67   them by calling
4917d 0d 0a 20 20 2a 2a 20 6e 6f 64 65 57 72 69 74 65  ..  ** nodeWrite
4917e 28 29 2e 20 4e 6f 64 65 20 70 52 69 67 68 74 20  (). Node pRight 
4917f 61 6c 77 61 79 73 20 6e 65 65 64 73 20 61 20 6e  always needs a n
49180 6f 64 65 20 6e 75 6d 62 65 72 2c 20 61 73 20 69  ode number, as i
49181 74 20 77 61 73 20 63 72 65 61 74 65 64 0d 0a 20  t was created.. 
49182 20 2a 2a 20 62 79 20 6e 6f 64 65 4e 65 77 28 29   ** by nodeNew()
49183 20 61 62 6f 76 65 2e 20 42 75 74 20 6e 6f 64 65   above. But node
49184 20 70 4c 65 66 74 20 73 6f 6d 65 74 69 6d 65 73   pLeft sometimes
49185 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 6e   already has a n
49186 6f 64 65 20 6e 75 6d 62 65 72 2e 0d 0a 20 20 2a  ode number...  *
49187 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  * In this case a
49188 76 6f 69 64 20 74 68 65 20 61 6c 6c 20 74 6f 20  void the all to 
49189 6e 6f 64 65 57 72 69 74 65 28 29 2e 0d 0a 20 20  nodeWrite()...  
4918a 2a 2f 0d 0a 20 20 69 66 28 20 53 51 4c 49 54 45  */..  if( SQLITE
4918b 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65 57  _OK!=(rc = nodeW
4918c 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 52 69  rite(pRtree, pRi
4918d 67 68 74 29 29 0d 0a 20 20 20 7c 7c 20 28 30 3d  ght))..   || (0=
4918e 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26 26  =pLeft->iNode &&
4918f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
49190 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72  = nodeWrite(pRtr
49191 65 65 2c 20 70 4c 65 66 74 29 29 29 0d 0a 20 20  ee, pLeft)))..  
49192 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c  ){..    goto spl
49193 69 74 6e 6f 64 65 5f 6f 75 74 3b 0d 0a 20 20 7d  itnode_out;..  }
49194 0d 0a 0d 0a 20 20 72 69 67 68 74 62 62 6f 78 2e  ....  rightbbox.
49195 69 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d  iRowid = pRight-
49196 3e 69 4e 6f 64 65 3b 0d 0a 20 20 6c 65 66 74 62  >iNode;..  leftb
49197 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65  box.iRowid = pLe
49198 66 74 2d 3e 69 4e 6f 64 65 3b 0d 0a 0d 0a 20 20  ft->iNode;....  
49199 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
4919a 3d 3d 31 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ==1 ){..    rc =
4919b 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c   rtreeInsertCell
4919c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e  (pRtree, pLeft->
4919d 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62  pParent, &leftbb
4919e 6f 78 2c 20 69 48 65 69 67 68 74 2b 31 29 3b 0d  ox, iHeight+1);.
4919f 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
491a0 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20  ITE_OK ){..     
491a1 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
491a2 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  out;..    }..  }
491a3 65 6c 73 65 7b 0d 0a 20 20 20 20 52 74 72 65 65  else{..    Rtree
491a4 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20  Node *pParent = 
491a5 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 3b 0d  pLeft->pParent;.
491a6 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0d  .    int iCell;.
491a7 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 50 61  .    rc = nodePa
491a8 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65  rentIndex(pRtree
491a9 2c 20 70 4c 65 66 74 2c 20 26 69 43 65 6c 6c 29  , pLeft, &iCell)
491aa 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
491ab 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
491ac 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
491ad 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
491ae 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c  rent, &leftbbox,
491af 20 69 43 65 6c 6c 29 3b 0d 0a 20 20 20 20 20 20   iCell);..      
491b0 72 63 20 3d 20 41 64 6a 75 73 74 54 72 65 65 28  rc = AdjustTree(
491b1 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
491b2 20 26 6c 65 66 74 62 62 6f 78 29 3b 0d 0a 20 20   &leftbbox);..  
491b3 20 20 7d 0d 0a 20 20 20 20 69 66 28 20 72 63 21    }..    if( rc!
491b4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
491b5 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
491b6 6f 64 65 5f 6f 75 74 3b 0d 0a 20 20 20 20 7d 0d  ode_out;..    }.
491b7 0a 20 20 7d 0d 0a 20 20 69 66 28 20 28 72 63 20  .  }..  if( (rc 
491b8 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
491b9 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  l(pRtree, pRight
491ba 2d 3e 70 50 61 72 65 6e 74 2c 20 26 72 69 67 68  ->pParent, &righ
491bb 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31  tbbox, iHeight+1
491bc 29 29 20 29 7b 0d 0a 20 20 20 20 67 6f 74 6f 20  )) ){..    goto 
491bd 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0d 0a  splitnode_out;..
491be 20 20 7d 0d 0a 0d 0a 20 20 66 6f 72 28 69 3d 30    }....  for(i=0
491bf 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69 67 68 74  ; i<NCELL(pRight
491c0 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 69 36  ); i++){..    i6
491c1 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47  4 iRowid = nodeG
491c2 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
491c3 70 52 69 67 68 74 2c 20 69 29 3b 0d 0a 20 20 20  pRight, i);..   
491c4 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70   rc = updateMapp
491c5 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77  ing(pRtree, iRow
491c6 69 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65 69  id, pRight, iHei
491c7 67 68 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 69  ght);..    if( i
491c8 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52  Rowid==pCell->iR
491c9 6f 77 69 64 20 29 7b 0d 0a 20 20 20 20 20 20 6e  owid ){..      n
491ca 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20  ewCellIsRight = 
491cb 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  1;..    }..    i
491cc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
491cd 20 29 7b 0d 0a 20 20 20 20 20 20 67 6f 74 6f 20   ){..      goto 
491ce 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0d 0a  splitnode_out;..
491cf 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66      }..  }..  if
491d0 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d  ( pNode->iNode==
491d1 31 20 29 7b 0d 0a 20 20 20 20 66 6f 72 28 69 3d  1 ){..    for(i=
491d2 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 4c 65 66 74  0; i<NCELL(pLeft
491d3 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20  ); i++){..      
491d4 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
491d5 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
491d6 2c 20 70 4c 65 66 74 2c 20 69 29 3b 0d 0a 20 20  , pLeft, i);..  
491d7 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d      rc = updateM
491d8 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69  apping(pRtree, i
491d9 52 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48  Rowid, pLeft, iH
491da 65 69 67 68 74 29 3b 0d 0a 20 20 20 20 20 20 69  eight);..      i
491db 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
491dc 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 67 6f 74   ){..        got
491dd 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b  o splitnode_out;
491de 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
491df 0d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 65  ..  }else if( ne
491e0 77 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30 20  wCellIsRight==0 
491e1 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 75 70 64  ){..    rc = upd
491e2 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
491e3 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  e, pCell->iRowid
491e4 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
491e5 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28  );..  }....  if(
491e6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
491e7 7b 0d 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  {..    rc = node
491e8 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
491e9 70 52 69 67 68 74 29 3b 0d 0a 20 20 20 20 70 52  pRight);..    pR
491ea 69 67 68 74 20 3d 20 30 3b 0d 0a 20 20 7d 0d 0a  ight = 0;..  }..
491eb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
491ec 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  _OK ){..    rc =
491ed 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
491ee 72 65 65 2c 20 70 4c 65 66 74 29 3b 0d 0a 20 20  ree, pLeft);..  
491ef 20 20 70 4c 65 66 74 20 3d 20 30 3b 0d 0a 20 20    pLeft = 0;..  
491f0 7d 0d 0a 0d 0a 73 70 6c 69 74 6e 6f 64 65 5f 6f  }....splitnode_o
491f1 75 74 3a 0d 0a 20 20 6e 6f 64 65 52 65 6c 65 61  ut:..  nodeRelea
491f2 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  se(pRtree, pRigh
491f3 74 29 3b 0d 0a 20 20 6e 6f 64 65 52 65 6c 65 61  t);..  nodeRelea
491f4 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  se(pRtree, pLeft
491f5 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
491f6 65 65 28 61 43 65 6c 6c 29 3b 0d 0a 20 20 72 65  ee(aCell);..  re
491f7 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f  turn rc;..}..../
491f8 2a 0d 0a 2a 2a 20 49 66 20 6e 6f 64 65 20 70 4c  *..** If node pL
491f9 65 61 66 20 69 73 20 6e 6f 74 20 74 68 65 20 72  eaf is not the r
491fa 6f 6f 74 20 6f 66 20 74 68 65 20 72 2d 74 72 65  oot of the r-tre
491fb 65 20 61 6e 64 20 69 74 73 20 70 50 61 72 65 6e  e and its pParen
491fc 74 20 70 6f 69 6e 74 65 72 20 69 73 20 0d 0a 2a  t pointer is ..*
491fd 2a 20 73 74 69 6c 6c 20 4e 55 4c 4c 2c 20 6c 6f  * still NULL, lo
491fe 61 64 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20  ad all ancestor 
491ff 6e 6f 64 65 73 20 6f 66 20 70 4c 65 61 66 20 69  nodes of pLeaf i
49200 6e 74 6f 20 6d 65 6d 6f 72 79 20 61 6e 64 20 70  nto memory and p
49201 6f 70 75 6c 61 74 65 0d 0a 2a 2a 20 74 68 65 20  opulate..** the 
49202 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 20 63  pLeaf->pParent c
49203 68 61 69 6e 20 61 6c 6c 20 74 68 65 20 77 61 79  hain all the way
49204 20 75 70 20 74 6f 20 74 68 65 20 72 6f 6f 74 20   up to the root 
49205 6e 6f 64 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  node...**..** Th
49206 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  is operation is 
49207 72 65 71 75 69 72 65 64 20 77 68 65 6e 20 61 20  required when a 
49208 72 6f 77 20 69 73 20 64 65 6c 65 74 65 64 20 28  row is deleted (
49209 6f 72 20 75 70 64 61 74 65 64 20 2d 20 61 6e 20  or updated - an 
4920a 75 70 64 61 74 65 0d 0a 2a 2a 20 69 73 20 69 6d  update..** is im
4920b 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 64  plemented as a d
4920c 65 6c 65 74 65 20 66 6f 6c 6c 6f 77 65 64 20 62  elete followed b
4920d 79 20 61 6e 20 69 6e 73 65 72 74 29 2e 20 53 51  y an insert). SQ
4920e 4c 69 74 65 20 70 72 6f 76 69 64 65 73 20 74 68  Lite provides th
4920f 65 0d 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74  e..** rowid of t
49210 68 65 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65  he row to delete
49211 2c 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75  , which can be u
49212 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20  sed to find the 
49213 6c 65 61 66 20 6f 6e 20 77 68 69 63 68 0d 0a 2a  leaf on which..*
49214 2a 20 74 68 65 20 65 6e 74 72 79 20 72 65 73 69  * the entry resi
49215 64 65 73 20 28 61 72 67 75 6d 65 6e 74 20 70 4c  des (argument pL
49216 65 61 66 29 2e 20 4f 6e 63 65 20 74 68 65 20 6c  eaf). Once the l
49217 65 61 66 20 69 73 20 6c 6f 63 61 74 65 64 2c 20  eaf is located, 
49218 74 68 69 73 20 0d 0a 2a 2a 20 66 75 6e 63 74 69  this ..** functi
49219 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
4921a 64 65 74 65 72 6d 69 6e 65 20 69 74 73 20 61 6e  determine its an
4921b 63 65 73 74 72 79 2e 0d 0a 2a 2f 0d 0a 73 74 61  cestry...*/..sta
4921c 74 69 63 20 69 6e 74 20 66 69 78 4c 65 61 66 50  tic int fixLeafP
4921d 61 72 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74  arent(Rtree *pRt
4921e 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
4921f 70 4c 65 61 66 29 7b 0d 0a 20 20 69 6e 74 20 72  pLeaf){..  int r
49220 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a  c = SQLITE_OK;..
49221 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
49222 69 6c 64 20 3d 20 70 4c 65 61 66 3b 0d 0a 20 20  ild = pLeaf;..  
49223 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
49224 45 5f 4f 4b 20 26 26 20 70 43 68 69 6c 64 2d 3e  E_OK && pChild->
49225 69 4e 6f 64 65 21 3d 31 20 26 26 20 70 43 68 69  iNode!=1 && pChi
49226 6c 64 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29  ld->pParent==0 )
49227 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d  {..    int rc2 =
49228 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
49229 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f       /* sqlite3_
4922a 72 65 73 65 74 28 29 20 72 65 74 75 72 6e 20 63  reset() return c
4922b 6f 64 65 20 2a 2f 0d 0a 20 20 20 20 73 71 6c 69  ode */..    sqli
4922c 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
4922d 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65  Rtree->pReadPare
4922e 6e 74 2c 20 31 2c 20 70 43 68 69 6c 64 2d 3e 69  nt, 1, pChild->i
4922f 4e 6f 64 65 29 3b 0d 0a 20 20 20 20 72 63 20 3d  Node);..    rc =
49230 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
49231 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
49232 74 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  t);..    if( rc=
49233 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0d 0a  =SQLITE_ROW ){..
49234 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20        RtreeNode 
49235 2a 70 54 65 73 74 3b 20 20 20 20 20 20 20 20 20  *pTest;         
49236 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 74 65 73    /* Used to tes
49237 74 20 66 6f 72 20 72 65 66 65 72 65 6e 63 65 20  t for reference 
49238 6c 6f 6f 70 73 20 2a 2f 0d 0a 20 20 20 20 20 20  loops */..      
49239 69 36 34 20 69 4e 6f 64 65 3b 20 20 20 20 20 20  i64 iNode;      
4923a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
4923b 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  ode number of pa
4923c 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0d 0a 0d 0a  rent node */....
4923d 20 20 20 20 20 20 2f 2a 20 42 65 66 6f 72 65 20        /* Before 
4923e 73 65 74 74 69 6e 67 20 70 43 68 69 6c 64 2d 3e  setting pChild->
4923f 70 50 61 72 65 6e 74 2c 20 74 65 73 74 20 74 68  pParent, test th
49240 61 74 20 77 65 20 61 72 65 20 6e 6f 74 20 63 72  at we are not cr
49241 65 61 74 69 6e 67 20 61 0d 0a 20 20 20 20 20 20  eating a..      
49242 2a 2a 20 6c 6f 6f 70 20 6f 66 20 72 65 66 65 72  ** loop of refer
49243 65 6e 63 65 73 20 28 61 73 20 77 65 20 77 6f 75  ences (as we wou
49244 6c 64 20 69 66 2c 20 73 61 79 2c 20 70 43 68 69  ld if, say, pChi
49245 6c 64 3d 3d 70 50 61 72 65 6e 74 29 2e 20 57 65  ld==pParent). We
49246 20 64 6f 6e 27 74 0d 0a 20 20 20 20 20 20 2a 2a   don't..      **
49247 20 77 61 6e 74 20 74 6f 20 64 6f 20 74 68 69 73   want to do this
49248 20 61 73 20 69 74 20 6c 65 61 64 73 20 74 6f 20   as it leads to 
49249 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68  a memory leak wh
4924a 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 64 65 6c  en trying to del
4924b 65 74 65 0d 0a 20 20 20 20 20 20 2a 2a 20 74 68  ete..      ** th
4924c 65 20 72 65 66 65 72 65 6e 63 65 64 20 63 6f 75  e referenced cou
4924d 6e 74 65 64 20 6e 6f 64 65 20 73 74 72 75 63 74  nted node struct
4924e 75 72 65 73 2e 0d 0a 20 20 20 20 20 20 2a 2f 0d  ures...      */.
4924f 0a 20 20 20 20 20 20 69 4e 6f 64 65 20 3d 20 73  .      iNode = s
49250 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
49251 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
49252 64 50 61 72 65 6e 74 2c 20 30 29 3b 0d 0a 20 20  dParent, 0);..  
49253 20 20 20 20 66 6f 72 28 70 54 65 73 74 3d 70 4c      for(pTest=pL
49254 65 61 66 3b 20 70 54 65 73 74 20 26 26 20 70 54  eaf; pTest && pT
49255 65 73 74 2d 3e 69 4e 6f 64 65 21 3d 69 4e 6f 64  est->iNode!=iNod
49256 65 3b 20 70 54 65 73 74 3d 70 54 65 73 74 2d 3e  e; pTest=pTest->
49257 70 50 61 72 65 6e 74 29 3b 0d 0a 20 20 20 20 20  pParent);..     
49258 20 69 66 28 20 21 70 54 65 73 74 20 29 7b 0d 0a   if( !pTest ){..
49259 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f          rc2 = no
4925a 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
4925b 2c 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70 43 68  , iNode, 0, &pCh
4925c 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0d 0a  ild->pParent);..
4925d 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
4925e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
4925f 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
49260 52 65 61 64 50 61 72 65 6e 74 29 3b 0d 0a 20 20  ReadParent);..  
49261 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
49262 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 3b 0d  _OK ) rc = rc2;.
49263 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
49264 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 68 69 6c  ITE_OK && !pChil
49265 64 2d 3e 70 50 61 72 65 6e 74 20 29 20 72 63 20  d->pParent ) rc 
49266 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
49267 5f 56 54 41 42 3b 0d 0a 20 20 20 20 70 43 68 69  _VTAB;..    pChi
49268 6c 64 20 3d 20 70 43 68 69 6c 64 2d 3e 70 50 61  ld = pChild->pPa
49269 72 65 6e 74 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65  rent;..  }..  re
4926a 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 73  turn rc;..}....s
4926b 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65  tatic int delete
4926c 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74  Cell(Rtree *, Rt
4926d 72 65 65 4e 6f 64 65 20 2a 2c 20 69 6e 74 2c 20  reeNode *, int, 
4926e 69 6e 74 29 3b 0d 0a 0d 0a 73 74 61 74 69 63 20  int);....static 
4926f 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64 65 28 52  int removeNode(R
49270 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
49271 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
49272 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0d 0a 20  int iHeight){.. 
49273 20 69 6e 74 20 72 63 3b 0d 0a 20 20 69 6e 74 20   int rc;..  int 
49274 72 63 32 3b 0d 0a 20 20 52 74 72 65 65 4e 6f 64  rc2;..  RtreeNod
49275 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 30 3b 0d  e *pParent = 0;.
49276 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0d 0a 0d  .  int iCell;...
49277 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65  .  assert( pNode
49278 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0d 0a 0d 0a  ->nRef==1 );....
49279 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
4927a 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72  entry in the par
4927b 65 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0d 0a 20 20  ent cell. */..  
4927c 72 63 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49  rc = nodeParentI
4927d 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  ndex(pRtree, pNo
4927e 64 65 2c 20 26 69 43 65 6c 6c 29 3b 0d 0a 20 20  de, &iCell);..  
4927f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
49280 4b 20 29 7b 0d 0a 20 20 20 20 70 50 61 72 65 6e  K ){..    pParen
49281 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  t = pNode->pPare
49282 6e 74 3b 0d 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  nt;..    pNode->
49283 70 50 61 72 65 6e 74 20 3d 20 30 3b 0d 0a 20 20  pParent = 0;..  
49284 20 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c    rc = deleteCel
49285 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  l(pRtree, pParen
49286 74 2c 20 69 43 65 6c 6c 2c 20 69 48 65 69 67 68  t, iCell, iHeigh
49287 74 2b 31 29 3b 0d 0a 20 20 7d 0d 0a 20 20 72 63  t+1);..  }..  rc
49288 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
49289 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29  pRtree, pParent)
4928a 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
4928b 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 72  ITE_OK ){..    r
4928c 63 20 3d 20 72 63 32 3b 0d 0a 20 20 7d 0d 0a 20  c = rc2;..  }.. 
4928d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
4928e 4f 4b 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  OK ){..    retur
4928f 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  n rc;..  }....  
49290 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78  /* Remove the xx
49291 78 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f  x_node entry. */
49292 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
49293 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
49294 44 65 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70  DeleteNode, 1, p
49295 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a 20  Node->iNode);.. 
49296 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
49297 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64  tree->pDeleteNod
49298 65 29 3b 0d 0a 20 20 69 66 28 20 53 51 4c 49 54  e);..  if( SQLIT
49299 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
4929a 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
4929b 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 29 20  ->pDeleteNode)) 
4929c 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ){..    return r
4929d 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20  c;..  }....  /* 
4929e 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 70  Remove the xxx_p
4929f 61 72 65 6e 74 20 65 6e 74 72 79 2e 20 2a 2f 0d  arent entry. */.
492a0 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
492a1 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
492a2 65 6c 65 74 65 50 61 72 65 6e 74 2c 20 31 2c 20  eleteParent, 1, 
492a3 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a  pNode->iNode);..
492a4 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
492a5 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
492a6 72 65 6e 74 29 3b 0d 0a 20 20 69 66 28 20 53 51  rent);..  if( SQ
492a7 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
492a8 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
492a9 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
492aa 6e 74 29 29 20 29 7b 0d 0a 20 20 20 20 72 65 74  nt)) ){..    ret
492ab 75 72 6e 20 72 63 3b 0d 0a 20 20 7d 0d 0a 20 20  urn rc;..  }..  
492ac 0d 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68  ..  /* Remove th
492ad 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20  e node from the 
492ae 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74  in-memory hash t
492af 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74  able and link it
492b0 20 69 6e 74 6f 0d 0a 20 20 2a 2a 20 74 68 65 20   into..  ** the 
492b1 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20 6c  Rtree.pDeleted l
492b2 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  ist. Its content
492b3 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73  s will be re-ins
492b4 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0d  erted later on..
492b5 0a 20 20 2a 2f 0d 0a 20 20 6e 6f 64 65 48 61 73  .  */..  nodeHas
492b6 68 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20  hDelete(pRtree, 
492b7 70 4e 6f 64 65 29 3b 0d 0a 20 20 70 4e 6f 64 65  pNode);..  pNode
492b8 2d 3e 69 4e 6f 64 65 20 3d 20 69 48 65 69 67 68  ->iNode = iHeigh
492b9 74 3b 0d 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65  t;..  pNode->pNe
492ba 78 74 20 3d 20 70 52 74 72 65 65 2d 3e 70 44 65  xt = pRtree->pDe
492bb 6c 65 74 65 64 3b 0d 0a 20 20 70 4e 6f 64 65 2d  leted;..  pNode-
492bc 3e 6e 52 65 66 2b 2b 3b 0d 0a 20 20 70 52 74 72  >nRef++;..  pRtr
492bd 65 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70  ee->pDeleted = p
492be 4e 6f 64 65 3b 0d 0a 0d 0a 20 20 72 65 74 75 72  Node;....  retur
492bf 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d 0d  n SQLITE_OK;..}.
492c0 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  ...static int fi
492c1 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 52 74 72  xBoundingBox(Rtr
492c2 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
492c3 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0d 0a  eNode *pNode){..
492c4 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
492c5 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  rent = pNode->pP
492c6 61 72 65 6e 74 3b 0d 0a 20 20 69 6e 74 20 72 63  arent;..  int rc
492c7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0d 0a   = SQLITE_OK; ..
492c8 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b    if( pParent ){
492c9 0d 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 0d 0a  ..    int ii; ..
492ca 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
492cb 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0d 0a 20  NCELL(pNode);.. 
492cc 20 20 20 52 74 72 65 65 43 65 6c 6c 20 62 6f 78     RtreeCell box
492cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
492ce 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
492cf 42 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66 6f 72  Bounding box for
492d0 20 70 4e 6f 64 65 20 2a 2f 0d 0a 20 20 20 20 6e   pNode */..    n
492d1 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
492d2 65 2c 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62 6f  e, pNode, 0, &bo
492d3 78 29 3b 0d 0a 20 20 20 20 66 6f 72 28 69 69 3d  x);..    for(ii=
492d4 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  1; ii<nCell; ii+
492d5 2b 29 7b 0d 0a 20 20 20 20 20 20 52 74 72 65 65  +){..      Rtree
492d6 43 65 6c 6c 20 63 65 6c 6c 3b 0d 0a 20 20 20 20  Cell cell;..    
492d7 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
492d8 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
492d9 20 26 63 65 6c 6c 29 3b 0d 0a 20 20 20 20 20 20   &cell);..      
492da 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
492db 2c 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b 0d  , &box, &cell);.
492dc 0a 20 20 20 20 7d 0d 0a 20 20 20 20 62 6f 78 2e  .    }..    box.
492dd 69 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e  iRowid = pNode->
492de 69 4e 6f 64 65 3b 0d 0a 20 20 20 20 72 63 20 3d  iNode;..    rc =
492df 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
492e0 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
492e1 26 69 69 29 3b 0d 0a 20 20 20 20 69 66 28 20 72  &ii);..    if( r
492e2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
492e3 0a 20 20 20 20 20 20 6e 6f 64 65 4f 76 65 72 77  .      nodeOverw
492e4 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  riteCell(pRtree,
492e5 20 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c 20   pParent, &box, 
492e6 69 69 29 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d  ii);..      rc =
492e7 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
492e8 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29  pRtree, pParent)
492e9 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20  ;..    }..  }.. 
492ea 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a   return rc;..}..
492eb 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65 6c 65 74 65 20  ../*..** Delete 
492ec 74 68 65 20 63 65 6c 6c 20 61 74 20 69 6e 64 65  the cell at inde
492ed 78 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  x iCell of node 
492ee 70 4e 6f 64 65 2e 20 41 66 74 65 72 20 72 65 6d  pNode. After rem
492ef 6f 76 69 6e 67 20 74 68 65 0d 0a 2a 2a 20 63 65  oving the..** ce
492f0 6c 6c 2c 20 61 64 6a 75 73 74 20 74 68 65 20 72  ll, adjust the r
492f1 2d 74 72 65 65 20 64 61 74 61 20 73 74 72 75 63  -tree data struc
492f2 74 75 72 65 20 69 66 20 72 65 71 75 69 72 65 64  ture if required
492f3 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
492f4 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
492f5 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
492f6 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e  eNode *pNode, in
492f7 74 20 69 43 65 6c 6c 2c 20 69 6e 74 20 69 48 65  t iCell, int iHe
492f8 69 67 68 74 29 7b 0d 0a 20 20 52 74 72 65 65 4e  ight){..  RtreeN
492f9 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 0d 0a 20  ode *pParent;.. 
492fa 20 69 6e 74 20 72 63 3b 0d 0a 0d 0a 20 20 69 66   int rc;....  if
492fb 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
492fc 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
492fd 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 29  (pRtree, pNode))
492fe 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20   ){..    return 
492ff 72 63 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  rc;..  }....  /*
49300 20 52 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c   Remove the cell
49301 20 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65 2e 20   from the node. 
49302 54 68 69 73 20 63 61 6c 6c 20 6a 75 73 74 20 6d  This call just m
49303 6f 76 65 73 20 62 79 74 65 73 20 61 72 6f 75 6e  oves bytes aroun
49304 64 0d 0a 20 20 2a 2a 20 74 68 65 20 69 6e 2d 6d  d..  ** the in-m
49305 65 6d 6f 72 79 20 6e 6f 64 65 20 69 6d 61 67 65  emory node image
49306 2c 20 73 6f 20 69 74 20 63 61 6e 6e 6f 74 20 66  , so it cannot f
49307 61 69 6c 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 6e 6f  ail...  */..  no
49308 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 70 52 74  deDeleteCell(pRt
49309 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
4930a 6c 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 49 66 20 74  l);....  /* If t
4930b 68 65 20 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74  he node is not t
4930c 68 65 20 74 72 65 65 20 72 6f 6f 74 20 61 6e 64  he tree root and
4930d 20 6e 6f 77 20 68 61 73 20 6c 65 73 73 20 74 68   now has less th
4930e 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0d 0a  an the minimum..
4930f 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63    ** number of c
49310 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20 69 74 20  ells, remove it 
49311 66 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 4f  from the tree. O
49312 74 68 65 72 77 69 73 65 2c 20 75 70 64 61 74 65  therwise, update
49313 20 74 68 65 0d 0a 20 20 2a 2a 20 63 65 6c 6c 20   the..  ** cell 
49314 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6e 6f  in the parent no
49315 64 65 20 73 6f 20 74 68 61 74 20 69 74 20 74 69  de so that it ti
49316 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74  ghtly contains t
49317 68 65 20 75 70 64 61 74 65 64 0d 0a 20 20 2a 2a  he updated..  **
49318 20 6e 6f 64 65 2e 0d 0a 20 20 2a 2f 0d 0a 20 20   node...  */..  
49319 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d  pParent = pNode-
4931a 3e 70 50 61 72 65 6e 74 3b 0d 0a 20 20 61 73 73  >pParent;..  ass
4931b 65 72 74 28 20 70 50 61 72 65 6e 74 20 7c 7c 20  ert( pParent || 
4931c 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
4931d 29 3b 0d 0a 20 20 69 66 28 20 70 50 61 72 65 6e  );..  if( pParen
4931e 74 20 29 7b 0d 0a 20 20 20 20 69 66 28 20 4e 43  t ){..    if( NC
4931f 45 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45 45  ELL(pNode)<RTREE
49320 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
49321 29 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d  ) ){..      rc =
49322 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
49323 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67  ee, pNode, iHeig
49324 68 74 29 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b  ht);..    }else{
49325 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 78  ..      rc = fix
49326 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72  BoundingBox(pRtr
49327 65 65 2c 20 70 4e 6f 64 65 29 3b 0d 0a 20 20 20  ee, pNode);..   
49328 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74   }..  }....  ret
49329 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 73 74  urn rc;..}....st
4932a 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73 65 72  atic int Reinser
4932b 74 28 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74  t(..  Rtree *pRt
4932c 72 65 65 2c 20 0d 0a 20 20 52 74 72 65 65 4e 6f  ree, ..  RtreeNo
4932d 64 65 20 2a 70 4e 6f 64 65 2c 20 0d 0a 20 20 52  de *pNode, ..  R
4932e 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c  treeCell *pCell,
4932f 20 0d 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74   ..  int iHeight
49330 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 2a 61 4f 72  ..){..  int *aOr
49331 64 65 72 3b 0d 0a 20 20 69 6e 74 20 2a 61 53 70  der;..  int *aSp
49332 61 72 65 3b 0d 0a 20 20 52 74 72 65 65 43 65 6c  are;..  RtreeCel
49333 6c 20 2a 61 43 65 6c 6c 3b 0d 0a 20 20 66 6c 6f  l *aCell;..  flo
49334 61 74 20 2a 61 44 69 73 74 61 6e 63 65 3b 0d 0a  at *aDistance;..
49335 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0d 0a 20 20    int nCell;..  
49336 66 6c 6f 61 74 20 61 43 65 6e 74 65 72 43 6f 6f  float aCenterCoo
49337 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  rd[RTREE_MAX_DIM
49338 45 4e 53 49 4f 4e 53 5d 3b 0d 0a 20 20 69 6e 74  ENSIONS];..  int
49339 20 69 44 69 6d 3b 0d 0a 20 20 69 6e 74 20 69 69   iDim;..  int ii
4933a 3b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
4933b 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20 6d 65  LITE_OK;....  me
4933c 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f 6f 72  mset(aCenterCoor
4933d 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 6c 6f  d, 0, sizeof(flo
4933e 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f 44 49  at)*RTREE_MAX_DI
4933f 4d 45 4e 53 49 4f 4e 53 29 3b 0d 0a 0d 0a 20 20  MENSIONS);....  
49340 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
49341 6f 64 65 29 2b 31 3b 0d 0a 0d 0a 20 20 2f 2a 20  ode)+1;....  /* 
49342 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 62 75 66  Allocate the buf
49343 66 65 72 73 20 75 73 65 64 20 62 79 20 74 68 69  fers used by thi
49344 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65  s operation. The
49345 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0d 0a   allocation is..
49346 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73 68 65    ** relinquishe
49347 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  d before this fu
49348 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0d  nction returns..
49349 0a 20 20 2a 2f 0d 0a 20 20 61 43 65 6c 6c 20 3d  .  */..  aCell =
4934a 20 28 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71   (RtreeCell *)sq
4934b 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65  lite3_malloc(nCe
4934c 6c 6c 20 2a 20 28 0d 0a 20 20 20 20 73 69 7a 65  ll * (..    size
4934d 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 20 2b 20  of(RtreeCell) + 
4934e 20 20 20 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c          /* aCell
4934f 20 61 72 72 61 79 20 2a 2f 0d 0a 20 20 20 20 73   array */..    s
49350 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20  izeof(int)      
49351 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 4f   +         /* aO
49352 72 64 65 72 20 61 72 72 61 79 20 2a 2f 0d 0a 20  rder array */.. 
49353 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 20     sizeof(int)  
49354 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20 2f       +         /
49355 2a 20 61 53 70 61 72 65 20 61 72 72 61 79 20 2a  * aSpare array *
49356 2f 0d 0a 20 20 20 20 73 69 7a 65 6f 66 28 66 6c  /..    sizeof(fl
49357 6f 61 74 29 20 20 20 20 20 20 20 20 20 20 20 20  oat)            
49358 20 20 20 2f 2a 20 61 44 69 73 74 61 6e 63 65 20     /* aDistance 
49359 61 72 72 61 79 20 2a 2f 0d 0a 20 20 29 29 3b 0d  array */..  ));.
4935a 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b  .  if( !aCell ){
4935b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ..    return SQL
4935c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d  ITE_NOMEM;..  }.
4935d 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d 20 28  .  aOrder    = (
4935e 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
4935f 6c 6c 5d 3b 0d 0a 20 20 61 53 70 61 72 65 20 20  ll];..  aSpare  
49360 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72 64    = (int *)&aOrd
49361 65 72 5b 6e 43 65 6c 6c 5d 3b 0d 0a 20 20 61 44  er[nCell];..  aD
49362 69 73 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74  istance = (float
49363 20 2a 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c   *)&aSpare[nCell
49364 5d 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69 3d 30  ];....  for(ii=0
49365 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
49366 29 7b 0d 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  ){..    if( ii==
49367 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0d 0a 20 20  (nCell-1) ){..  
49368 20 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c      memcpy(&aCel
49369 6c 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69  l[ii], pCell, si
4936a 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
4936b 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20  ;..    }else{.. 
4936c 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c       nodeGetCell
4936d 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
4936e 69 69 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b  ii, &aCell[ii]);
4936f 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 61 4f 72  ..    }..    aOr
49370 64 65 72 5b 69 69 5d 20 3d 20 69 69 3b 0d 0a 20  der[ii] = ii;.. 
49371 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
49372 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
49373 3b 20 69 44 69 6d 2b 2b 29 7b 0d 0a 20 20 20 20  ; iDim++){..    
49374 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69    aCenterCoord[i
49375 44 69 6d 5d 20 2b 3d 20 28 66 6c 6f 61 74 29 44  Dim] += (float)D
49376 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
49377 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
49378 0d 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72 43  ..      aCenterC
49379 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 28 66  oord[iDim] += (f
4937a 6c 6f 61 74 29 44 43 4f 4f 52 44 28 61 43 65 6c  loat)DCOORD(aCel
4937b 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  l[ii].aCoord[iDi
4937c 6d 2a 32 2b 31 5d 29 3b 0d 0a 20 20 20 20 7d 0d  m*2+1]);..    }.
4937d 0a 20 20 7d 0d 0a 20 20 66 6f 72 28 69 44 69 6d  .  }..  for(iDim
4937e 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d  =0; iDim<pRtree-
4937f 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0d  >nDim; iDim++){.
49380 0a 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72  .    aCenterCoor
49381 64 5b 69 44 69 6d 5d 20 3d 20 28 66 6c 6f 61 74  d[iDim] = (float
49382 29 28 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69  )(aCenterCoord[i
49383 44 69 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65  Dim]/((float)nCe
49384 6c 6c 2a 32 2e 30 29 29 3b 0d 0a 20 20 7d 0d 0a  ll*2.0));..  }..
49385 0d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
49386 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0d 0a  <nCell; ii++){..
49387 20 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69 69      aDistance[ii
49388 5d 20 3d 20 30 2e 30 3b 0d 0a 20 20 20 20 66 6f  ] = 0.0;..    fo
49389 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70  r(iDim=0; iDim<p
4938a 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69  Rtree->nDim; iDi
4938b 6d 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 66 6c 6f  m++){..      flo
4938c 61 74 20 63 6f 6f 72 64 20 3d 20 28 66 6c 6f 61  at coord = (floa
4938d 74 29 28 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  t)(DCOORD(aCell[
4938e 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  ii].aCoord[iDim*
4938f 32 2b 31 5d 29 20 2d 20 0d 0a 20 20 20 20 20 20  2+1]) - ..      
49390 20 20 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c      DCOORD(aCell
49391 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  [ii].aCoord[iDim
49392 2a 32 5d 29 29 3b 0d 0a 20 20 20 20 20 20 61 44  *2]));..      aD
49393 69 73 74 61 6e 63 65 5b 69 69 5d 20 2b 3d 20 28  istance[ii] += (
49394 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f  coord-aCenterCoo
49395 72 64 5b 69 44 69 6d 5d 29 2a 28 63 6f 6f 72 64  rd[iDim])*(coord
49396 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44  -aCenterCoord[iD
49397 69 6d 5d 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  im]);..    }..  
49398 7d 0d 0a 0d 0a 20 20 53 6f 72 74 42 79 44 69 73  }....  SortByDis
49399 74 61 6e 63 65 28 61 4f 72 64 65 72 2c 20 6e 43  tance(aOrder, nC
4939a 65 6c 6c 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ell, aDistance, 
4939b 61 53 70 61 72 65 29 3b 0d 0a 20 20 6e 6f 64 65  aSpare);..  node
4939c 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f  Zero(pRtree, pNo
4939d 64 65 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69  de);....  for(ii
4939e 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
4939f 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c 2d 28  K && ii<(nCell-(
493a0 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
493a1 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69 2b 2b  Rtree)+1)); ii++
493a2 29 7b 0d 0a 20 20 20 20 52 74 72 65 65 43 65 6c  ){..    RtreeCel
493a3 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f  l *p = &aCell[aO
493a4 72 64 65 72 5b 69 69 5d 5d 3b 0d 0a 20 20 20 20  rder[ii]];..    
493a5 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
493a6 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29  Rtree, pNode, p)
493a7 3b 0d 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52  ;..    if( p->iR
493a8 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f  owid==pCell->iRo
493a9 77 69 64 20 29 7b 0d 0a 20 20 20 20 20 20 69 66  wid ){..      if
493aa 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0d  ( iHeight==0 ){.
493ab 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f  .        rc = ro
493ac 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
493ad 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64   p->iRowid, pNod
493ae 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a 20 20 20 20  e->iNode);..    
493af 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
493b0 20 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69    rc = parentWri
493b1 74 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52  te(pRtree, p->iR
493b2 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  owid, pNode->iNo
493b3 64 65 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20  de);..      }.. 
493b4 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 69 66 28     }..  }..  if(
493b5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
493b6 7b 0d 0a 20 20 20 20 72 63 20 3d 20 66 69 78 42  {..    rc = fixB
493b7 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65  oundingBox(pRtre
493b8 65 2c 20 70 4e 6f 64 65 29 3b 0d 0a 20 20 7d 0d  e, pNode);..  }.
493b9 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c  .  for(; rc==SQL
493ba 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65  ITE_OK && ii<nCe
493bb 6c 6c 3b 20 69 69 2b 2b 29 7b 0d 0a 20 20 20 20  ll; ii++){..    
493bc 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74  /* Find a node t
493bd 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c  o store this cel
493be 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f  l in. pNode->iNo
493bf 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  de currently con
493c0 74 61 69 6e 73 0d 0a 20 20 20 20 2a 2a 20 74 68  tains..    ** th
493c1 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
493c2 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
493c3 62 79 20 74 68 65 20 63 65 6c 6c 2e 0d 0a 20 20  by the cell...  
493c4 20 20 2a 2f 0d 0a 20 20 20 20 52 74 72 65 65 4e    */..    RtreeN
493c5 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b 0d 0a 20  ode *pInsert;.. 
493c6 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 20     RtreeCell *p 
493c7 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b  = &aCell[aOrder[
493c8 69 69 5d 5d 3b 0d 0a 20 20 20 20 72 63 20 3d 20  ii]];..    rc = 
493c9 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65  ChooseLeaf(pRtre
493ca 65 2c 20 70 2c 20 69 48 65 69 67 68 74 2c 20 26  e, p, iHeight, &
493cb 70 49 6e 73 65 72 74 29 3b 0d 0a 20 20 20 20 69  pInsert);..    i
493cc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
493cd 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72   ){..      int r
493ce 63 32 3b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20  c2;..      rc = 
493cf 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28  rtreeInsertCell(
493d0 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c  pRtree, pInsert,
493d1 20 70 2c 20 69 48 65 69 67 68 74 29 3b 0d 0a 20   p, iHeight);.. 
493d2 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52       rc2 = nodeR
493d3 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
493d4 49 6e 73 65 72 74 29 3b 0d 0a 20 20 20 20 20 20  Insert);..      
493d5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
493d6 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63  K ){..        rc
493d7 20 3d 20 72 63 32 3b 0d 0a 20 20 20 20 20 20 7d   = rc2;..      }
493d8 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a  ..    }..  }....
493d9 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
493da 43 65 6c 6c 29 3b 0d 0a 20 20 72 65 74 75 72 6e  Cell);..  return
493db 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a   rc;..}..../*..*
493dc 2a 20 49 6e 73 65 72 74 20 63 65 6c 6c 20 70 43  * Insert cell pC
493dd 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e  ell into node pN
493de 6f 64 65 2e 20 4e 6f 64 65 20 70 4e 6f 64 65 20  ode. Node pNode 
493df 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61  is the head of a
493e0 20 0d 0a 2a 2a 20 73 75 62 74 72 65 65 20 69 48   ..** subtree iH
493e1 65 69 67 68 74 20 68 69 67 68 20 28 6c 65 61 66  eight high (leaf
493e2 20 6e 6f 64 65 73 20 68 61 76 65 20 69 48 65 69   nodes have iHei
493e3 67 68 74 3d 3d 30 29 2e 0d 0a 2a 2f 0d 0a 73 74  ght==0)...*/..st
493e4 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
493e5 73 65 72 74 43 65 6c 6c 28 0d 0a 20 20 52 74 72  sertCell(..  Rtr
493e6 65 65 20 2a 70 52 74 72 65 65 2c 0d 0a 20 20 52  ee *pRtree,..  R
493e7 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
493e8 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ..  RtreeCell *p
493e9 43 65 6c 6c 2c 0d 0a 20 20 69 6e 74 20 69 48 65  Cell,..  int iHe
493ea 69 67 68 74 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20  ight..){..  int 
493eb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  rc = SQLITE_OK;.
493ec 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30  .  if( iHeight>0
493ed 20 29 7b 0d 0a 20 20 20 20 52 74 72 65 65 4e 6f   ){..    RtreeNo
493ee 64 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64  de *pChild = nod
493ef 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72  eHashLookup(pRtr
493f0 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69  ee, pCell->iRowi
493f1 64 29 3b 0d 0a 20 20 20 20 69 66 28 20 70 43 68  d);..    if( pCh
493f2 69 6c 64 20 29 7b 0d 0a 20 20 20 20 20 20 6e 6f  ild ){..      no
493f3 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
493f4 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e  , pChild->pParen
493f5 74 29 3b 0d 0a 20 20 20 20 20 20 6e 6f 64 65 52  t);..      nodeR
493f6 65 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b  eference(pNode);
493f7 0d 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e  ..      pChild->
493f8 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b  pParent = pNode;
493f9 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20  ..    }..  }..  
493fa 69 66 28 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  if( nodeInsertCe
493fb 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
493fc 2c 20 70 43 65 6c 6c 29 20 29 7b 0d 0a 23 69 66  , pCell) ){..#if
493fd 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
493fe 45 45 5f 52 45 49 4e 53 45 52 54 0d 0a 20 20 20  EE_REINSERT..   
493ff 20 69 66 28 20 69 48 65 69 67 68 74 3c 3d 70 52   if( iHeight<=pR
49400 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48  tree->iReinsertH
49401 65 69 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e  eight || pNode->
49402 69 4e 6f 64 65 3d 3d 31 29 7b 0d 0a 20 20 20 20  iNode==1){..    
49403 20 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65    rc = SplitNode
49404 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
49405 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b  pCell, iHeight);
49406 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20  ..    }else{..  
49407 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
49408 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 69 48  nsertHeight = iH
49409 65 69 67 68 74 3b 0d 0a 20 20 20 20 20 20 72 63  eight;..      rc
4940a 20 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74 72   = Reinsert(pRtr
4940b 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c  ee, pNode, pCell
4940c 2c 20 69 48 65 69 67 68 74 29 3b 0d 0a 20 20 20  , iHeight);..   
4940d 20 7d 0d 0a 23 65 6c 73 65 0d 0a 20 20 20 20 72   }..#else..    r
4940e 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52  c = SplitNode(pR
4940f 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
49410 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0d 0a 23  ll, iHeight);..#
49411 65 6e 64 69 66 0d 0a 20 20 7d 65 6c 73 65 7b 0d  endif..  }else{.
49412 0a 20 20 20 20 72 63 20 3d 20 41 64 6a 75 73 74  .    rc = Adjust
49413 54 72 65 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Tree(pRtree, pNo
49414 64 65 2c 20 70 43 65 6c 6c 29 3b 0d 0a 20 20 20  de, pCell);..   
49415 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
49416 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69 66 28  OK ){..      if(
49417 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0d 0a   iHeight==0 ){..
49418 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77          rc = row
49419 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c 20  idWrite(pRtree, 
4941a 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70  pCell->iRowid, p
4941b 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a 20  Node->iNode);.. 
4941c 20 20 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20       }else{..   
4941d 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74       rc = parent
4941e 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 43  Write(pRtree, pC
4941f 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  ell->iRowid, pNo
49420 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d 0a 20 20 20  de->iNode);..   
49421 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d     }..    }..  }
49422 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a  ..  return rc;..
49423 7d 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  }....static int 
49424 72 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74  reinsertNodeCont
49425 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72 65  ent(Rtree *pRtre
49426 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
49427 6f 64 65 29 7b 0d 0a 20 20 69 6e 74 20 69 69 3b  ode){..  int ii;
49428 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
49429 49 54 45 5f 4f 4b 3b 0d 0a 20 20 69 6e 74 20 6e  ITE_OK;..  int n
4942a 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
4942b 64 65 29 3b 0d 0a 0d 0a 20 20 66 6f 72 28 69 69  de);....  for(ii
4942c 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
4942d 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  K && ii<nCell; i
4942e 69 2b 2b 29 7b 0d 0a 20 20 20 20 52 74 72 65 65  i++){..    Rtree
4942f 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b 0d 0a  Node *pInsert;..
49430 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65      RtreeCell ce
49431 6c 6c 3b 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74  ll;..    nodeGet
49432 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
49433 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0d  de, ii, &cell);.
49434 0a 0d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ...    /* Find a
49435 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
49436 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
49437 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
49438 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0d 0a 20 20  tly contains..  
49439 20 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20    ** the height 
4943a 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20  of the sub-tree 
4943b 68 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65  headed by the ce
4943c 6c 6c 2e 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20  ll...    */..   
4943d 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66   rc = ChooseLeaf
4943e 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
4943f 28 69 6e 74 29 70 4e 6f 64 65 2d 3e 69 4e 6f 64  (int)pNode->iNod
49440 65 2c 20 26 70 49 6e 73 65 72 74 29 3b 0d 0a 20  e, &pInsert);.. 
49441 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
49442 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69  E_OK ){..      i
49443 6e 74 20 72 63 32 3b 0d 0a 20 20 20 20 20 20 72  nt rc2;..      r
49444 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
49445 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73  ell(pRtree, pIns
49446 65 72 74 2c 20 26 63 65 6c 6c 2c 20 28 69 6e 74  ert, &cell, (int
49447 29 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0d  )pNode->iNode);.
49448 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64  .      rc2 = nod
49449 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
4944a 20 70 49 6e 73 65 72 74 29 3b 0d 0a 20 20 20 20   pInsert);..    
4944b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
4944c 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  _OK ){..        
4944d 72 63 20 3d 20 72 63 32 3b 0d 0a 20 20 20 20 20  rc = rc2;..     
4944e 20 7d 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a   }..    }..  }..
4944f 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d    return rc;..}.
49450 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 6c 65 63 74  .../*..** Select
49451 20 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75   a currently unu
49452 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61 20  sed rowid for a 
49453 6e 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f 72  new r-tree recor
49454 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  d...*/..static i
49455 6e 74 20 6e 65 77 52 6f 77 69 64 28 52 74 72 65  nt newRowid(Rtre
49456 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 2a  e *pRtree, i64 *
49457 70 69 52 6f 77 69 64 29 7b 0d 0a 20 20 69 6e 74  piRowid){..  int
49458 20 72 63 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f   rc;..  sqlite3_
49459 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65  bind_null(pRtree
4945a 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31  ->pWriteRowid, 1
4945b 29 3b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  );..  sqlite3_bi
4945c 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e  nd_null(pRtree->
4945d 70 57 72 69 74 65 52 6f 77 69 64 2c 20 32 29 3b  pWriteRowid, 2);
4945e 0d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ..  sqlite3_step
4945f 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
49460 6f 77 69 64 29 3b 0d 0a 20 20 72 63 20 3d 20 73  owid);..  rc = s
49461 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
49462 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
49463 29 3b 0d 0a 20 20 2a 70 69 52 6f 77 69 64 20 3d  );..  *piRowid =
49464 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
49465 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65  sert_rowid(pRtre
49466 65 2d 3e 64 62 29 3b 0d 0a 20 20 72 65 74 75 72  e->db);..  retur
49467 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  n rc;..}..../*..
49468 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65 6e  ** Remove the en
49469 74 72 79 20 77 69 74 68 20 72 6f 77 69 64 3d 69  try with rowid=i
4946a 44 65 6c 65 74 65 20 66 72 6f 6d 20 74 68 65 20  Delete from the 
4946b 72 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  r-tree structure
4946c 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  ...*/..static in
4946d 74 20 72 74 72 65 65 44 65 6c 65 74 65 52 6f 77  t rtreeDeleteRow
4946e 69 64 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  id(Rtree *pRtree
4946f 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
49470 69 44 65 6c 65 74 65 29 7b 0d 0a 20 20 69 6e 74  iDelete){..  int
49471 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
49472 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49473 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0d 0a  Return code */..
49474 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
49475 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  af;             
49476 20 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20 63    /* Leaf node c
49477 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72 64  ontaining record
49478 20 69 44 65 6c 65 74 65 20 2a 2f 0d 0a 20 20 69   iDelete */..  i
49479 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt iCell;       
4947a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4947b 2a 20 49 6e 64 65 78 20 6f 66 20 69 44 65 6c 65  * Index of iDele
4947c 74 65 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61 66  te cell in pLeaf
4947d 20 2a 2f 0d 0a 20 20 52 74 72 65 65 4e 6f 64 65   */..  RtreeNode
4947e 20 2a 70 52 6f 6f 74 3b 20 20 20 20 20 20 20 20   *pRoot;        
4947f 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6e         /* Root n
49480 6f 64 65 20 6f 66 20 72 74 72 65 65 20 73 74 72  ode of rtree str
49481 75 63 74 75 72 65 20 2a 2f 0d 0a 0d 0a 0d 0a 20  ucture */...... 
49482 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66   /* Obtain a ref
49483 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f  erence to the ro
49484 6f 74 20 6e 6f 64 65 20 74 6f 20 69 6e 69 74 69  ot node to initi
49485 61 6c 69 73 65 20 52 74 72 65 65 2e 69 44 65 70  alise Rtree.iDep
49486 74 68 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 6e 6f  th */..  rc = no
49487 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
49488 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b  , 1, 0, &pRoot);
49489 0d 0a 0d 0a 20 20 2f 2a 20 4f 62 74 61 69 6e 20  ....  /* Obtain 
4948a 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
4948b 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 74 68 61  he leaf node tha
4948c 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65  t contains the e
4948d 6e 74 72 79 20 0d 0a 20 20 2a 2a 20 61 62 6f 75  ntry ..  ** abou
4948e 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e  t to be deleted.
4948f 20 0d 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72   ..  */..  if( r
49490 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d  c==SQLITE_OK ){.
49491 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 65  .    rc = findLe
49492 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 69  afNode(pRtree, i
49493 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29 3b  Delete, &pLeaf);
49494 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 44 65  ..  }....  /* De
49495 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20 69 6e  lete the cell in
49496 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74   question from t
49497 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  he leaf node. */
49498 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
49499 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 6e  TE_OK ){..    in
4949a 74 20 72 63 32 3b 0d 0a 20 20 20 20 72 63 20 3d  t rc2;..    rc =
4949b 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
4949c 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69  pRtree, pLeaf, i
4949d 44 65 6c 65 74 65 2c 20 26 69 43 65 6c 6c 29 3b  Delete, &iCell);
4949e 0d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
4949f 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20  LITE_OK ){..    
494a0 20 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c    rc = deleteCel
494a1 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  l(pRtree, pLeaf,
494a2 20 69 43 65 6c 6c 2c 20 30 29 3b 0d 0a 20 20 20   iCell, 0);..   
494a3 20 7d 0d 0a 20 20 20 20 72 63 32 20 3d 20 6e 6f   }..    rc2 = no
494a4 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
494a5 2c 20 70 4c 65 61 66 29 3b 0d 0a 20 20 20 20 69  , pLeaf);..    i
494a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
494a7 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20 3d 20   ){..      rc = 
494a8 72 63 32 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d  rc2;..    }..  }
494a9 0d 0a 0d 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20  ....  /* Delete 
494aa 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
494ab 67 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 3c  g entry in the <
494ac 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62  rtree>_rowid tab
494ad 6c 65 2e 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63  le. */..  if( rc
494ae 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a  ==SQLITE_OK ){..
494af 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
494b0 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
494b1 44 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20  DeleteRowid, 1, 
494b2 69 44 65 6c 65 74 65 29 3b 0d 0a 20 20 20 20 73  iDelete);..    s
494b3 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
494b4 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
494b5 29 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  );..    rc = sql
494b6 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
494b7 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29  e->pDeleteRowid)
494b8 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 43  ;..  }....  /* C
494b9 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f 74  heck if the root
494ba 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73 20 65 78   node now has ex
494bb 61 63 74 6c 79 20 6f 6e 65 20 63 68 69 6c 64 2e  actly one child.
494bc 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65 0d 0a   If so, remove..
494bd 20 20 2a 2a 20 69 74 2c 20 73 63 68 65 64 75 6c    ** it, schedul
494be 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
494bf 66 20 74 68 65 20 63 68 69 6c 64 20 66 6f 72 20  f the child for 
494c0 72 65 69 6e 73 65 72 74 69 6f 6e 20 61 6e 64 20  reinsertion and 
494c1 0d 0a 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68  ..  ** reduce th
494c2 65 20 74 72 65 65 20 68 65 69 67 68 74 20 62 79  e tree height by
494c3 20 6f 6e 65 2e 0d 0a 20 20 2a 2a 0d 0a 20 20 2a   one...  **..  *
494c4 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61  * This is equiva
494c5 6c 65 6e 74 20 74 6f 20 63 6f 70 79 69 6e 67 20  lent to copying 
494c6 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
494c7 74 68 65 20 63 68 69 6c 64 20 69 6e 74 6f 0d 0a  the child into..
494c8 20 20 2a 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f    ** the root no
494c9 64 65 20 28 74 68 65 20 6f 70 65 72 61 74 69 6f  de (the operatio
494ca 6e 20 74 68 61 74 20 47 75 74 6d 61 6e 27 73 20  n that Gutman's 
494cb 70 61 70 65 72 20 73 61 79 73 20 74 6f 20 70 65  paper says to pe
494cc 72 66 6f 72 6d 20 0d 0a 20 20 2a 2a 20 69 6e 20  rform ..  ** in 
494cd 74 68 69 73 20 73 63 65 6e 61 72 69 6f 29 2e 0d  this scenario)..
494ce 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 72 63 3d  .  */..  if( rc=
494cf 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52  =SQLITE_OK && pR
494d0 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 26  tree->iDepth>0 &
494d1 26 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3d 3d  & NCELL(pRoot)==
494d2 31 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 72 63  1 ){..    int rc
494d3 32 3b 0d 0a 20 20 20 20 52 74 72 65 65 4e 6f 64  2;..    RtreeNod
494d4 65 20 2a 70 43 68 69 6c 64 3b 0d 0a 20 20 20 20  e *pChild;..    
494d5 69 36 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64  i64 iChild = nod
494d6 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
494d7 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0d 0a 20 20  , pRoot, 0);..  
494d8 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
494d9 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69 6c  re(pRtree, iChil
494da 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c  d, pRoot, &pChil
494db 64 29 3b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  d);..    if( rc=
494dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
494dd 20 20 20 20 20 72 63 20 3d 20 72 65 6d 6f 76 65       rc = remove
494de 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 43 68  Node(pRtree, pCh
494df 69 6c 64 2c 20 70 52 74 72 65 65 2d 3e 69 44 65  ild, pRtree->iDe
494e0 70 74 68 2d 31 29 3b 0d 0a 20 20 20 20 7d 0d 0a  pth-1);..    }..
494e1 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65      rc2 = nodeRe
494e2 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
494e3 68 69 6c 64 29 3b 0d 0a 20 20 20 20 69 66 28 20  hild);..    if( 
494e4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc==SQLITE_OK ) 
494e5 72 63 20 3d 20 72 63 32 3b 0d 0a 20 20 20 20 69  rc = rc2;..    i
494e6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
494e7 20 29 7b 0d 0a 20 20 20 20 20 20 70 52 74 72 65   ){..      pRtre
494e8 65 2d 3e 69 44 65 70 74 68 2d 2d 3b 0d 0a 20 20  e->iDepth--;..  
494e9 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 70      writeInt16(p
494ea 52 6f 6f 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74  Root->zData, pRt
494eb 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0d 0a 20  ree->iDepth);.. 
494ec 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69       pRoot->isDi
494ed 72 74 79 20 3d 20 31 3b 0d 0a 20 20 20 20 7d 0d  rty = 1;..    }.
494ee 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 52 65 2d  .  }....  /* Re-
494ef 69 6e 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65  insert the conte
494f0 6e 74 73 20 6f 66 20 61 6e 79 20 75 6e 64 65 72  nts of any under
494f1 66 75 6c 6c 20 6e 6f 64 65 73 20 72 65 6d 6f 76  full nodes remov
494f2 65 64 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65  ed from the tree
494f3 2e 20 2a 2f 0d 0a 20 20 66 6f 72 28 70 4c 65 61  . */..  for(pLea
494f4 66 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  f=pRtree->pDelet
494f5 65 64 3b 20 70 4c 65 61 66 3b 20 70 4c 65 61 66  ed; pLeaf; pLeaf
494f6 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  =pRtree->pDelete
494f7 64 29 7b 0d 0a 20 20 20 20 69 66 28 20 72 63 3d  d){..    if( rc=
494f8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20  =SQLITE_OK ){.. 
494f9 20 20 20 20 20 72 63 20 3d 20 72 65 69 6e 73 65       rc = reinse
494fa 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52  rtNodeContent(pR
494fb 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0d 0a 20  tree, pLeaf);.. 
494fc 20 20 20 7d 0d 0a 20 20 20 20 70 52 74 72 65 65     }..    pRtree
494fd 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c 65  ->pDeleted = pLe
494fe 61 66 2d 3e 70 4e 65 78 74 3b 0d 0a 20 20 20 20  af->pNext;..    
494ff 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 65  sqlite3_free(pLe
49500 61 66 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f  af);..  }....  /
49501 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 65  * Release the re
49502 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72  ference to the r
49503 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0d 0a 20 20  oot node. */..  
49504 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
49505 4b 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d 20 6e  K ){..    rc = n
49506 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
49507 65 2c 20 70 52 6f 6f 74 29 3b 0d 0a 20 20 7d 65  e, pRoot);..  }e
49508 6c 73 65 7b 0d 0a 20 20 20 20 6e 6f 64 65 52 65  lse{..    nodeRe
49509 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
4950a 6f 6f 74 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  oot);..  }....  
4950b 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
4950c 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 78 55 70 64  ./*..** The xUpd
4950d 61 74 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 72  ate method for r
4950e 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72 74  tree module virt
4950f 75 61 6c 20 74 61 62 6c 65 73 2e 0d 0a 2a 2f 0d  ual tables...*/.
49510 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
49511 65 55 70 64 61 74 65 28 0d 0a 20 20 73 71 6c 69  eUpdate(..  sqli
49512 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
49513 20 0d 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20   ..  int nData, 
49514 0d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ..  sqlite3_valu
49515 65 20 2a 2a 61 7a 44 61 74 61 2c 20 0d 0a 20 20  e **azData, ..  
49516 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52  sqlite_int64 *pR
49517 6f 77 69 64 0d 0a 29 7b 0d 0a 20 20 52 74 72 65  owid..){..  Rtre
49518 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
49519 65 65 20 2a 29 70 56 74 61 62 3b 0d 0a 20 20 69  ee *)pVtab;..  i
4951a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
4951b 4b 3b 0d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  K;..  RtreeCell 
4951c 63 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  cell;           
4951d 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c        /* New cel
4951e 6c 20 74 6f 20 69 6e 73 65 72 74 20 69 66 20 6e  l to insert if n
4951f 44 61 74 61 3e 31 20 2a 2f 0d 0a 20 20 69 6e 74  Data>1 */..  int
49520 20 62 48 61 76 65 52 6f 77 69 64 20 3d 20 30 3b   bHaveRowid = 0;
49521 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49522 53 65 74 20 74 6f 20 31 20 61 66 74 65 72 20 6e  Set to 1 after n
49523 65 77 20 72 6f 77 69 64 20 69 73 20 64 65 74 65  ew rowid is dete
49524 72 6d 69 6e 65 64 20 2a 2f 0d 0a 0d 0a 20 20 72  rmined */....  r
49525 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70 52  treeReference(pR
49526 74 72 65 65 29 3b 0d 0a 20 20 61 73 73 65 72 74  tree);..  assert
49527 28 6e 44 61 74 61 3e 3d 31 29 3b 0d 0a 0d 0a 20  (nData>=1);.... 
49528 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 68   /* Constraint h
49529 61 6e 64 6c 69 6e 67 2e 20 41 20 77 72 69 74 65  andling. A write
4952a 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 6e   operation on an
4952b 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 6d 61   r-tree table ma
4952c 79 20 72 65 74 75 72 6e 0d 0a 20 20 2a 2a 20 53  y return..  ** S
4952d 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
4952e 20 66 6f 72 20 74 77 6f 20 72 65 61 73 6f 6e 73   for two reasons
4952f 3a 0d 0a 20 20 2a 2a 0d 0a 20 20 2a 2a 20 20 20  :..  **..  **   
49530 31 2e 20 41 20 64 75 70 6c 69 63 61 74 65 20 72  1. A duplicate r
49531 6f 77 69 64 20 76 61 6c 75 65 2c 20 6f 72 0d 0a  owid value, or..
49532 20 20 2a 2a 20 20 20 32 2e 20 54 68 65 20 73 75    **   2. The su
49533 70 70 6c 69 65 64 20 64 61 74 61 20 76 69 6f 6c  pplied data viol
49534 61 74 65 73 20 74 68 65 20 22 78 32 3e 3d 78 31  ates the "x2>=x1
49535 22 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0d 0a 20  " constraint... 
49536 20 2a 2a 0d 0a 20 20 2a 2a 20 49 6e 20 74 68 65   **..  ** In the
49537 20 66 69 72 73 74 20 63 61 73 65 2c 20 69 66 20   first case, if 
49538 74 68 65 20 63 6f 6e 66 6c 69 63 74 2d 68 61 6e  the conflict-han
49539 64 6c 69 6e 67 20 6d 6f 64 65 20 69 73 20 52 45  dling mode is RE
4953a 50 4c 41 43 45 2c 20 74 68 65 6e 0d 0a 20 20 2a  PLACE, then..  *
4953b 2a 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e  * the conflictin
4953c 67 20 72 6f 77 20 63 61 6e 20 62 65 20 72 65 6d  g row can be rem
4953d 6f 76 65 64 20 62 65 66 6f 72 65 20 70 72 6f 63  oved before proc
4953e 65 65 64 69 6e 67 2e 20 49 6e 20 74 68 65 20 73  eeding. In the s
4953f 65 63 6f 6e 64 0d 0a 20 20 2a 2a 20 63 61 73 65  econd..  ** case
49540 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  , SQLITE_CONSTRA
49541 49 4e 54 20 6d 75 73 74 20 62 65 20 72 65 74 75  INT must be retu
49542 72 6e 65 64 20 72 65 67 61 72 64 6c 65 73 73 20  rned regardless 
49543 6f 66 20 74 68 65 0d 0a 20 20 2a 2a 20 63 6f 6e  of the..  ** con
49544 66 6c 69 63 74 2d 68 61 6e 64 6c 69 6e 67 20 6d  flict-handling m
49545 6f 64 65 20 73 70 65 63 69 66 69 65 64 20 62 79  ode specified by
49546 20 74 68 65 20 75 73 65 72 2e 0d 0a 20 20 2a 2f   the user...  */
49547 0d 0a 20 20 69 66 28 20 6e 44 61 74 61 3e 31 20  ..  if( nData>1 
49548 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 69 3b 0d  ){..    int ii;.
49549 0a 0d 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ...    /* Popula
4954a 74 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f  te the cell.aCoo
4954b 72 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20  rd[] array. The 
4954c 66 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  first coordinate
4954d 20 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a   is azData[3]. *
4954e 2f 0d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  /..    assert( n
4954f 44 61 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e  Data==(pRtree->n
49550 44 69 6d 2a 32 20 2b 20 33 29 20 29 3b 0d 0a 20  Dim*2 + 3) );.. 
49551 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65     if( pRtree->e
49552 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
49553 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b  _COORD_REAL32 ){
49554 0d 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30  ..      for(ii=0
49555 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
49556 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0d 0a  im*2); ii+=2){..
49557 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f          cell.aCo
49558 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c 6f  ord[ii].f = (flo
49559 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  at)sqlite3_value
4955a 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b 69  _double(azData[i
4955b 69 2b 33 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20  i+3]);..        
4955c 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
4955d 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c  ].f = (float)sql
4955e 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
4955f 65 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b  e(azData[ii+4]);
49560 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 65  ..        if( ce
49561 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e  ll.aCoord[ii].f>
49562 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
49563 5d 2e 66 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ].f ){..        
49564 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
49565 4e 53 54 52 41 49 4e 54 3b 0d 0a 20 20 20 20 20  NSTRAINT;..     
49566 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72       goto constr
49567 61 69 6e 74 3b 0d 0a 20 20 20 20 20 20 20 20 7d  aint;..        }
49568 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d  ..      }..    }
49569 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 66 6f 72  else{..      for
4956a 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
4956b 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
4956c 32 29 7b 0d 0a 20 20 20 20 20 20 20 20 63 65 6c  2){..        cel
4956d 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d  l.aCoord[ii].i =
4956e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
4956f 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29  nt(azData[ii+3])
49570 3b 0d 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e  ;..        cell.
49571 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d  aCoord[ii+1].i =
49572 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
49573 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29  nt(azData[ii+4])
49574 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ;..        if( c
49575 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
49576 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b  >cell.aCoord[ii+
49577 31 5d 2e 69 20 29 7b 0d 0a 20 20 20 20 20 20 20  1].i ){..       
49578 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
49579 4f 4e 53 54 52 41 49 4e 54 3b 0d 0a 20 20 20 20  ONSTRAINT;..    
4957a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74        goto const
4957b 72 61 69 6e 74 3b 0d 0a 20 20 20 20 20 20 20 20  raint;..        
4957c 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20  }..      }..    
4957d 7d 0d 0a 0d 0a 20 20 20 20 2f 2a 20 49 66 20 61  }....    /* If a
4957e 20 72 6f 77 69 64 20 76 61 6c 75 65 20 77 61 73   rowid value was
4957f 20 73 75 70 70 6c 69 65 64 2c 20 63 68 65 63 6b   supplied, check
49580 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64   if it is alread
49581 79 20 70 72 65 73 65 6e 74 20 69 6e 20 0d 0a 20  y present in .. 
49582 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e     ** the table.
49583 20 49 66 20 73 6f 2c 20 74 68 65 20 63 6f 6e 73   If so, the cons
49584 74 72 61 69 6e 74 20 68 61 73 20 66 61 69 6c 65  traint has faile
49585 64 2e 20 2a 2f 0d 0a 20 20 20 20 69 66 28 20 73  d. */..    if( s
49586 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
49587 65 28 61 7a 44 61 74 61 5b 32 5d 29 21 3d 53 51  e(azData[2])!=SQ
49588 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0d 0a 20 20  LITE_NULL ){..  
49589 20 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64 20      cell.iRowid 
4958a 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
4958b 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d 29  int64(azData[2])
4958c 3b 0d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ;..      if( sql
4958d 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
4958e 61 7a 44 61 74 61 5b 30 5d 29 3d 3d 53 51 4c 49  azData[0])==SQLI
4958f 54 45 5f 4e 55 4c 4c 0d 0a 20 20 20 20 20 20 20  TE_NULL..       
49590 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  || sqlite3_value
49591 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 30 5d  _int64(azData[0]
49592 29 21 3d 63 65 6c 6c 2e 69 52 6f 77 69 64 0d 0a  )!=cell.iRowid..
49593 20 20 20 20 20 20 29 7b 0d 0a 20 20 20 20 20 20        ){..      
49594 20 20 69 6e 74 20 73 74 65 70 72 63 3b 0d 0a 20    int steprc;.. 
49595 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62         sqlite3_b
49596 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
49597 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c  ->pReadRowid, 1,
49598 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0d 0a   cell.iRowid);..
49599 20 20 20 20 20 20 20 20 73 74 65 70 72 63 20 3d          steprc =
4959a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
4959b 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
4959c 29 3b 0d 0a 20 20 20 20 20 20 20 20 72 63 20 3d  );..        rc =
4959d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
4959e 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
4959f 64 29 3b 0d 0a 20 20 20 20 20 20 20 20 69 66 28  d);..        if(
495a0 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 74 65   SQLITE_ROW==ste
495a1 70 72 63 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  prc ){..        
495a2 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 74    if( sqlite3_vt
495a3 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 70  ab_on_conflict(p
495a4 52 74 72 65 65 2d 3e 64 62 29 3d 3d 53 51 4c 49  Rtree->db)==SQLI
495a5 54 45 5f 52 45 50 4c 41 43 45 20 29 7b 0d 0a 20  TE_REPLACE ){.. 
495a6 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
495a7 72 74 72 65 65 44 65 6c 65 74 65 52 6f 77 69 64  rtreeDeleteRowid
495a8 28 70 52 74 72 65 65 2c 20 63 65 6c 6c 2e 69 52  (pRtree, cell.iR
495a9 6f 77 69 64 29 3b 0d 0a 20 20 20 20 20 20 20 20  owid);..        
495aa 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
495ab 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
495ac 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0d 0a 20  E_CONSTRAINT;.. 
495ad 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
495ae 63 6f 6e 73 74 72 61 69 6e 74 3b 0d 0a 20 20 20  constraint;..   
495af 20 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20         }..      
495b0 20 20 7d 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20    }..      }..  
495b1 20 20 20 20 62 48 61 76 65 52 6f 77 69 64 20 3d      bHaveRowid =
495b2 20 31 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d   1;..    }..  }.
495b3 0a 0d 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61 74  ...  /* If azDat
495b4 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20 53  a[0] is not an S
495b5 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 69  QL NULL value, i
495b6 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  t is the rowid o
495b7 66 20 61 0d 0a 20 20 2a 2a 20 72 65 63 6f 72 64  f a..  ** record
495b8 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20   to delete from 
495b9 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
495ba 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
495bb 62 6c 6f 63 6b 20 64 6f 65 73 0d 0a 20 20 2a 2a  block does..  **
495bc 20 6a 75 73 74 20 74 68 61 74 2e 0d 0a 20 20 2a   just that...  *
495bd 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
495be 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61  _value_type(azDa
495bf 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e  ta[0])!=SQLITE_N
495c0 55 4c 4c 20 29 7b 0d 0a 20 20 20 20 72 63 20 3d  ULL ){..    rc =
495c1 20 72 74 72 65 65 44 65 6c 65 74 65 52 6f 77 69   rtreeDeleteRowi
495c2 64 28 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65  d(pRtree, sqlite
495c3 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a  3_value_int64(az
495c4 44 61 74 61 5b 30 5d 29 29 3b 0d 0a 20 20 7d 0d  Data[0]));..  }.
495c5 0a 0d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  ...  /* If the a
495c6 7a 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f  zData[] array co
495c7 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
495c8 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c   one element, el
495c9 65 6d 65 6e 74 73 0d 0a 20 20 2a 2a 20 28 61 7a  ements..  ** (az
495ca 44 61 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b  Data[2]..azData[
495cb 61 72 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e  argc-1]) contain
495cc 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f   a new record to
495cd 20 69 6e 73 65 72 74 20 69 6e 74 6f 0d 0a 20 20   insert into..  
495ce 2a 2a 20 74 68 65 20 72 2d 74 72 65 65 20 73 74  ** the r-tree st
495cf 72 75 63 74 75 72 65 2e 0d 0a 20 20 2a 2f 0d 0a  ructure...  */..
495d0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
495d1 5f 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20 29  _OK && nData>1 )
495d2 7b 0d 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  {..    /* Insert
495d3 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
495d4 69 6e 74 6f 20 74 68 65 20 72 2d 74 72 65 65 20  into the r-tree 
495d5 2a 2f 0d 0a 20 20 20 20 52 74 72 65 65 4e 6f 64  */..    RtreeNod
495d6 65 20 2a 70 4c 65 61 66 3b 0d 0a 0d 0a 20 20 20  e *pLeaf;....   
495d7 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
495d8 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
495d9 6e 65 77 20 72 6f 77 2e 20 2a 2f 0d 0a 20 20 20  new row. */..   
495da 20 69 66 28 20 62 48 61 76 65 52 6f 77 69 64 3d   if( bHaveRowid=
495db 3d 30 20 29 7b 0d 0a 20 20 20 20 20 20 72 63 20  =0 ){..      rc 
495dc 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74 72 65  = newRowid(pRtre
495dd 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69 64 29  e, &cell.iRowid)
495de 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 2a 70  ;..    }..    *p
495df 52 6f 77 69 64 20 3d 20 63 65 6c 6c 2e 69 52 6f  Rowid = cell.iRo
495e0 77 69 64 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20  wid;....    if( 
495e1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
495e2 0d 0a 20 20 20 20 20 20 72 63 20 3d 20 43 68 6f  ..      rc = Cho
495e3 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20  oseLeaf(pRtree, 
495e4 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66  &cell, 0, &pLeaf
495e5 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 69  );..    }..    i
495e6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
495e7 20 29 7b 0d 0a 20 20 20 20 20 20 69 6e 74 20 72   ){..      int r
495e8 63 32 3b 0d 0a 20 20 20 20 20 20 70 52 74 72 65  c2;..      pRtre
495e9 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67  e->iReinsertHeig
495ea 68 74 20 3d 20 2d 31 3b 0d 0a 20 20 20 20 20 20  ht = -1;..      
495eb 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74  rc = rtreeInsert
495ec 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65  Cell(pRtree, pLe
495ed 61 66 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0d 0a  af, &cell, 0);..
495ee 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65        rc2 = node
495ef 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
495f0 70 4c 65 61 66 29 3b 0d 0a 20 20 20 20 20 20 69  pLeaf);..      i
495f1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
495f2 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 72 63 20   ){..        rc 
495f3 3d 20 72 63 32 3b 0d 0a 20 20 20 20 20 20 7d 0d  = rc2;..      }.
495f4 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 63  .    }..  }....c
495f5 6f 6e 73 74 72 61 69 6e 74 3a 0d 0a 20 20 72 74  onstraint:..  rt
495f6 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
495f7 65 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 72 63  e);..  return rc
495f8 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54  ;..}..../*..** T
495f9 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
495fa 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75  d for rtree modu
495fb 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  le virtual table
495fc 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  s...*/..static i
495fd 6e 74 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73  nt rtreeRename(s
495fe 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
495ff 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
49600 7a 4e 65 77 4e 61 6d 65 29 7b 0d 0a 20 20 52 74  zNewName){..  Rt
49601 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
49602 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0d 0a 20  tree *)pVtab;.. 
49603 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
49604 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 63 68 61 72 20  _NOMEM;..  char 
49605 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
49606 6d 70 72 69 6e 74 66 28 0d 0a 20 20 20 20 22 41  mprintf(..    "A
49607 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
49608 71 5f 6e 6f 64 65 27 20 20 20 52 45 4e 41 4d 45  q_node'   RENAME
49609 20 54 4f 20 5c 22 25 77 5f 6e 6f 64 65 5c 22 3b   TO \"%w_node\";
4960a 22 0d 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  "..    "ALTER TA
4960b 42 4c 45 20 25 51 2e 27 25 71 5f 70 61 72 65 6e  BLE %Q.'%q_paren
4960c 74 27 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25  t' RENAME TO \"%
4960d 77 5f 70 61 72 65 6e 74 5c 22 3b 22 0d 0a 20 20  w_parent\";"..  
4960e 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
4960f 51 2e 27 25 71 5f 72 6f 77 69 64 27 20 20 52 45  Q.'%q_rowid'  RE
49610 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 72 6f 77  NAME TO \"%w_row
49611 69 64 5c 22 3b 22 0d 0a 20 20 20 20 2c 20 70 52  id\";"..    , pR
49612 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
49613 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61  e->zName, zNewNa
49614 6d 65 20 0d 0a 20 20 20 20 2c 20 70 52 74 72 65  me ..    , pRtre
49615 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
49616 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20  zName, zNewName 
49617 0d 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e  ..    , pRtree->
49618 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
49619 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 0d 0a 20 20  me, zNewName..  
4961a 29 3b 0d 0a 20 20 69 66 28 20 7a 53 71 6c 20 29  );..  if( zSql )
4961b 7b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  {..    rc = sqli
4961c 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
4961d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  >db, zSql, 0, 0,
4961e 20 30 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65   0);..    sqlite
4961f 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d 0a 20  3_free(zSql);.. 
49620 20 7d 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
49621 0d 0a 7d 0d 0a 0d 0a 73 74 61 74 69 63 20 73 71  ..}....static sq
49622 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 72 74 72  lite3_module rtr
49623 65 65 4d 6f 64 75 6c 65 20 3d 20 7b 0d 0a 20 20  eeModule = {..  
49624 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
49625 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
49626 56 65 72 73 69 6f 6e 20 2a 2f 0d 0a 20 20 72 74  Version */..  rt
49627 72 65 65 43 72 65 61 74 65 2c 20 20 20 20 20 20  reeCreate,      
49628 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72            /* xCr
49629 65 61 74 65 20 2d 20 63 72 65 61 74 65 20 61 20  eate - create a 
4962a 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 72 74 72 65  table */..  rtre
4962b 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20 20 20 20  eConnect,       
4962c 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6e 6e          /* xConn
4962d 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74 20 74 6f  ect - connect to
4962e 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62   an existing tab
4962f 6c 65 20 2a 2f 0d 0a 20 20 72 74 72 65 65 42 65  le */..  rtreeBe
49630 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20  stIndex,        
49631 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49 6e 64       /* xBestInd
49632 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65 20 73  ex - Determine s
49633 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20 2a  earch strategy *
49634 2f 0d 0a 20 20 72 74 72 65 65 44 69 73 63 6f 6e  /..  rtreeDiscon
49635 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  nect,           
49636 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20   /* xDisconnect 
49637 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 6f  - Disconnect fro
49638 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20  m a table */..  
49639 72 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20  rtreeDestroy,   
4963a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
4963b 44 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61  Destroy - Drop a
4963c 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 72 74 72   table */..  rtr
4963d 65 65 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  eeOpen,         
4963e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
4963f 6e 20 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f  n - open a curso
49640 72 20 2a 2f 0d 0a 20 20 72 74 72 65 65 43 6c 6f  r */..  rtreeClo
49641 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
49642 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20      /* xClose - 
49643 63 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 2a  close a cursor *
49644 2f 0d 0a 20 20 72 74 72 65 65 46 69 6c 74 65 72  /..  rtreeFilter
49645 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
49646 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20 63 6f   /* xFilter - co
49647 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63 6f 6e  nfigure scan con
49648 73 74 72 61 69 6e 74 73 20 2a 2f 0d 0a 20 20 72  straints */..  r
49649 74 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20 20  treeNext,       
4964a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e             /* xN
4964b 65 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61 20  ext - advance a 
4964c 63 75 72 73 6f 72 20 2a 2f 0d 0a 20 20 72 74 72  cursor */..  rtr
4964d 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20  eeEof,          
4964e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66           /* xEof
4964f 20 2a 2f 0d 0a 20 20 72 74 72 65 65 43 6f 6c 75   */..  rtreeColu
49650 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mn,             
49651 20 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20     /* xColumn - 
49652 72 65 61 64 20 64 61 74 61 20 2a 2f 0d 0a 20 20  read data */..  
49653 72 74 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20  rtreeRowid,     
49654 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
49655 52 6f 77 69 64 20 2d 20 72 65 61 64 20 64 61 74  Rowid - read dat
49656 61 20 2a 2f 0d 0a 20 20 72 74 72 65 65 55 70 64  a */..  rtreeUpd
49657 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
49658 20 20 20 20 2f 2a 20 78 55 70 64 61 74 65 20 2d      /* xUpdate -
49659 20 77 72 69 74 65 20 64 61 74 61 20 2a 2f 0d 0a   write data */..
4965a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
4965b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4965c 20 78 42 65 67 69 6e 20 2d 20 62 65 67 69 6e 20   xBegin - begin 
4965d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a  transaction */..
4965e 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
4965f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49660 20 78 53 79 6e 63 20 2d 20 73 79 6e 63 20 74 72   xSync - sync tr
49661 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a 20 20  ansaction */..  
49662 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
49663 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
49664 43 6f 6d 6d 69 74 20 2d 20 63 6f 6d 6d 69 74 20  Commit - commit 
49665 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0d 0a  transaction */..
49666 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
49667 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49668 20 78 52 6f 6c 6c 62 61 63 6b 20 2d 20 72 6f 6c   xRollback - rol
49669 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f  lback transactio
4966a 6e 20 2a 2f 0d 0a 20 20 30 2c 20 20 20 20 20 20  n */..  0,      
4966b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4966c 20 20 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63      /* xFindFunc
4966d 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69 6f 6e 20  tion - function 
4966e 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a 2f 0d 0a  overloading */..
4966f 20 20 72 74 72 65 65 52 65 6e 61 6d 65 2c 20 20    rtreeRename,  
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49671 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e 61 6d   xRename - renam
49672 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0d 0a  e the table */..
49673 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
49674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49675 20 78 53 61 76 65 70 6f 69 6e 74 20 2a 2f 0d 0a   xSavepoint */..
49676 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
49677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49678 20 78 52 65 6c 65 61 73 65 20 2a 2f 0d 0a 20 20   xRelease */..  
49679 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
4967a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
4967b 52 6f 6c 6c 62 61 63 6b 54 6f 20 2a 2f 0d 0a 7d  RollbackTo */..}
4967c 3b 0d 0a 0d 0a 73 74 61 74 69 63 20 69 6e 74 20  ;....static int 
4967d 72 74 72 65 65 53 71 6c 49 6e 69 74 28 0d 0a 20  rtreeSqlInit(.. 
4967e 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
4967f 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  ..  sqlite3 *db,
49680 20 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   ..  const char 
49681 2a 7a 44 62 2c 20 0d 0a 20 20 63 6f 6e 73 74 20  *zDb, ..  const 
49682 63 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20 0d  char *zPrefix, .
49683 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 0d  .  int isCreate.
49684 0a 29 7b 0d 0a 20 20 69 6e 74 20 72 63 20 3d 20  .){..  int rc = 
49685 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 0d 0a 20 20  SQLITE_OK;....  
49686 23 64 65 66 69 6e 65 20 4e 5f 53 54 41 54 45 4d  #define N_STATEM
49687 45 4e 54 20 39 0d 0a 20 20 73 74 61 74 69 63 20  ENT 9..  static 
49688 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 53 71  const char *azSq
49689 6c 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 20 3d  l[N_STATEMENT] =
4968a 20 7b 0d 0a 20 20 20 20 2f 2a 20 52 65 61 64 20   {..    /* Read 
4968b 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78  and write the xx
4968c 78 5f 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0d  x_node table */.
4968d 0a 20 20 20 20 22 53 45 4c 45 43 54 20 64 61 74  .    "SELECT dat
4968e 61 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  a FROM '%q'.'%q_
4968f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e 6f 64 65  node' WHERE node
49690 6e 6f 20 3d 20 3a 31 22 2c 0d 0a 20 20 20 20 22  no = :1",..    "
49691 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
49692 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  E INTO '%q'.'%q_
49693 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 3a 31 2c  node' VALUES(:1,
49694 20 3a 32 29 22 2c 0d 0a 20 20 20 20 22 44 45 4c   :2)",..    "DEL
49695 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  ETE FROM '%q'.'%
49696 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e 6f  q_node' WHERE no
49697 64 65 6e 6f 20 3d 20 3a 31 22 2c 0d 0a 0d 0a 20  deno = :1",.... 
49698 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77     /* Read and w
49699 72 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77  rite the xxx_row
4969a 69 64 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  id table */..   
4969b 20 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20   "SELECT nodeno 
4969c 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f  FROM '%q'.'%q_ro
4969d 77 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64  wid' WHERE rowid
4969e 20 3d 20 3a 31 22 2c 0d 0a 20 20 20 20 22 49 4e   = :1",..    "IN
4969f 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20  SERT OR REPLACE 
496a0 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f  INTO '%q'.'%q_ro
496a1 77 69 64 27 20 56 41 4c 55 45 53 28 3a 31 2c 20  wid' VALUES(:1, 
496a2 3a 32 29 22 2c 0d 0a 20 20 20 20 22 44 45 4c 45  :2)",..    "DELE
496a3 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  TE FROM '%q'.'%q
496a4 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f  _rowid' WHERE ro
496a5 77 69 64 20 3d 20 3a 31 22 2c 0d 0a 0d 0a 20 20  wid = :1",....  
496a6 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
496a7 69 74 65 20 74 68 65 20 78 78 78 5f 70 61 72 65  ite the xxx_pare
496a8 6e 74 20 74 61 62 6c 65 20 2a 2f 0d 0a 20 20 20  nt table */..   
496a9 20 22 53 45 4c 45 43 54 20 70 61 72 65 6e 74 6e   "SELECT parentn
496aa 6f 64 65 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  ode FROM '%q'.'%
496ab 71 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20  q_parent' WHERE 
496ac 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0d 0a 20  nodeno = :1",.. 
496ad 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45     "INSERT OR RE
496ae 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e  PLACE INTO '%q'.
496af 27 25 71 5f 70 61 72 65 6e 74 27 20 56 41 4c 55  '%q_parent' VALU
496b0 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0d 0a 20 20  ES(:1, :2)",..  
496b1 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27    "DELETE FROM '
496b2 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 20  %q'.'%q_parent' 
496b3 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a  WHERE nodeno = :
496b4 31 22 0d 0a 20 20 7d 3b 0d 0a 20 20 73 71 6c 69  1"..  };..  sqli
496b5 74 65 33 5f 73 74 6d 74 20 2a 2a 61 70 70 53 74  te3_stmt **appSt
496b6 6d 74 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 3b  mt[N_STATEMENT];
496b7 0d 0a 20 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20  ..  int i;....  
496b8 70 52 74 72 65 65 2d 3e 64 62 20 3d 20 64 62 3b  pRtree->db = db;
496b9 0d 0a 0d 0a 20 20 69 66 28 20 69 73 43 72 65 61  ....  if( isCrea
496ba 74 65 20 29 7b 0d 0a 20 20 20 20 63 68 61 72 20  te ){..    char 
496bb 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
496bc 65 33 5f 6d 70 72 69 6e 74 66 28 0d 0a 22 43 52  e3_mprintf(.."CR
496bd 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c  EATE TABLE \"%w\
496be 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f  ".\"%w_node\"(no
496bf 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49  deno INTEGER PRI
496c0 4d 41 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42  MARY KEY, data B
496c1 4c 4f 42 29 3b 22 0d 0a 22 43 52 45 41 54 45 20  LOB);".."CREATE 
496c2 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
496c3 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77 69 64 20  w_rowid\"(rowid 
496c4 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
496c5 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45  KEY, nodeno INTE
496c6 47 45 52 29 3b 22 0d 0a 22 43 52 45 41 54 45 20  GER);".."CREATE 
496c7 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
496c8 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e  w_parent\"(noden
496c9 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
496ca 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64  Y KEY, parentnod
496cb 65 20 49 4e 54 45 47 45 52 29 3b 22 0d 0a 22 49  e INTEGER);".."I
496cc 4e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27 2e  NSERT INTO '%q'.
496cd 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53  '%q_node' VALUES
496ce 28 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64 29  (1, zeroblob(%d)
496cf 29 22 2c 0d 0a 20 20 20 20 20 20 7a 44 62 2c 20  )",..      zDb, 
496d0 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50  zPrefix, zDb, zP
496d1 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
496d2 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
496d3 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  x, pRtree->iNode
496d4 53 69 7a 65 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  Size..    );..  
496d5 20 20 69 66 28 20 21 7a 43 72 65 61 74 65 20 29    if( !zCreate )
496d6 7b 0d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {..      return 
496d7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20  SQLITE_NOMEM;.. 
496d8 20 20 20 7d 0d 0a 20 20 20 20 72 63 20 3d 20 73     }..    rc = s
496d9 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
496da 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c 20 30  zCreate, 0, 0, 0
496db 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  );..    sqlite3_
496dc 66 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0d 0a  free(zCreate);..
496dd 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
496de 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20  TE_OK ){..      
496df 72 65 74 75 72 6e 20 72 63 3b 0d 0a 20 20 20 20  return rc;..    
496e0 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 61 70 70 53  }..  }....  appS
496e1 74 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65  tmt[0] = &pRtree
496e2 2d 3e 70 52 65 61 64 4e 6f 64 65 3b 0d 0a 20 20  ->pReadNode;..  
496e3 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52  appStmt[1] = &pR
496e4 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65  tree->pWriteNode
496e5 3b 0d 0a 20 20 61 70 70 53 74 6d 74 5b 32 5d 20  ;..  appStmt[2] 
496e6 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
496e7 74 65 4e 6f 64 65 3b 0d 0a 20 20 61 70 70 53 74  teNode;..  appSt
496e8 6d 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[3] = &pRtree-
496e9 3e 70 52 65 61 64 52 6f 77 69 64 3b 0d 0a 20 20  >pReadRowid;..  
496ea 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52  appStmt[4] = &pR
496eb 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
496ec 64 3b 0d 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d  d;..  appStmt[5]
496ed 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c   = &pRtree->pDel
496ee 65 74 65 52 6f 77 69 64 3b 0d 0a 20 20 61 70 70  eteRowid;..  app
496ef 53 74 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72 65  Stmt[6] = &pRtre
496f0 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 3b 0d  e->pReadParent;.
496f1 0a 20 20 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20  .  appStmt[7] = 
496f2 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  &pRtree->pWriteP
496f3 61 72 65 6e 74 3b 0d 0a 20 20 61 70 70 53 74 6d  arent;..  appStm
496f4 74 5b 38 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[8] = &pRtree->
496f5 70 44 65 6c 65 74 65 50 61 72 65 6e 74 3b 0d 0a  pDeleteParent;..
496f6 0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e  ..  for(i=0; i<N
496f7 5f 53 54 41 54 45 4d 45 4e 54 20 26 26 20 72 63  _STATEMENT && rc
496f8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b 2b  ==SQLITE_OK; i++
496f9 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  ){..    char *zS
496fa 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
496fb 69 6e 74 66 28 61 7a 53 71 6c 5b 69 5d 2c 20 7a  intf(azSql[i], z
496fc 44 62 2c 20 7a 50 72 65 66 69 78 29 3b 0d 0a 20  Db, zPrefix);.. 
496fd 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0d 0a     if( zSql ){..
496fe 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
496ff 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
49700 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70 70 53  , zSql, -1, appS
49701 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0d 0a 20 20  tmt[i], 0); ..  
49702 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20    }else{..      
49703 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
49704 4d 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73  M;..    }..    s
49705 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
49706 29 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74  );..  }....  ret
49707 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  urn rc;..}..../*
49708 0d 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
49709 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
4970a 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69   function contai
4970b 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  ns the text of a
4970c 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0d  n SQL statement.
4970d 0a 2a 2a 20 74 68 61 74 20 72 65 74 75 72 6e 73  .** that returns
4970e 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65   a single intege
4970f 72 20 76 61 6c 75 65 2e 20 54 68 65 20 73 74 61  r value. The sta
49710 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 69 6c  tement is compil
49711 65 64 20 61 6e 64 20 65 78 65 63 75 74 65 64 0d  ed and executed.
49712 0a 2a 2a 20 75 73 69 6e 67 20 64 61 74 61 62 61  .** using databa
49713 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  se connection db
49714 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
49715 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
49716 75 65 20 72 65 74 75 72 6e 65 64 0d 0a 2a 2a 20  ue returned..** 
49717 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  is written to *p
49718 69 56 61 6c 20 61 6e 64 20 53 51 4c 49 54 45 5f  iVal and SQLITE_
49719 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  OK returned. Oth
4971a 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69 74  erwise, an SQLit
4971b 65 20 65 72 72 6f 72 0d 0a 2a 2a 20 63 6f 64 65  e error..** code
4971c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
4971d 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 2a 70   the value of *p
4971e 69 56 61 6c 20 61 66 74 65 72 20 72 65 74 75 72  iVal after retur
4971f 6e 69 6e 67 20 69 73 20 6e 6f 74 20 64 65 66 69  ning is not defi
49720 6e 65 64 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  ned...*/..static
49721 20 69 6e 74 20 67 65 74 49 6e 74 46 72 6f 6d 53   int getIntFromS
49722 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  tmt(sqlite3 *db,
49723 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
49724 6c 2c 20 69 6e 74 20 2a 70 69 56 61 6c 29 7b 0d  l, int *piVal){.
49725 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
49726 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 69 66 28  TE_NOMEM;..  if(
49727 20 7a 53 71 6c 20 29 7b 0d 0a 20 20 20 20 73 71   zSql ){..    sq
49728 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
49729 74 20 3d 20 30 3b 0d 0a 20 20 20 20 72 63 20 3d  t = 0;..    rc =
4972a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
4972b 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  _v2(db, zSql, -1
4972c 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0d 0a 20  , &pStmt, 0);.. 
4972d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
4972e 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 69  E_OK ){..      i
4972f 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  f( SQLITE_ROW==s
49730 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
49731 74 29 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 2a  t) ){..        *
49732 70 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  piVal = sqlite3_
49733 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74  column_int(pStmt
49734 2c 20 30 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a  , 0);..      }..
49735 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
49736 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
49737 74 29 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d  t);..    }..  }.
49738 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d  .  return rc;..}
49739 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 69 73 20  ..../*..** This 
4973a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
4973b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  ed from within t
4973c 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f 72  he xConnect() or
4973d 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
4973e 64 20 74 6f 0d 0a 2a 2a 20 64 65 74 65 72 6d 69  d to..** determi
4973f 6e 65 20 74 68 65 20 6e 6f 64 65 2d 73 69 7a 65  ne the node-size
49740 20 75 73 65 64 20 62 79 20 74 68 65 20 72 74 72   used by the rtr
49741 65 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63  ee table being c
49742 72 65 61 74 65 64 20 6f 72 20 63 6f 6e 6e 65 63  reated or connec
49743 74 65 64 0d 0a 2a 2a 20 74 6f 2e 20 49 66 20 73  ted..** to. If s
49744 75 63 63 65 73 73 66 75 6c 2c 20 70 52 74 72 65  uccessful, pRtre
49745 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 69 73 20  e->iNodeSize is 
49746 70 6f 70 75 6c 61 74 65 64 20 61 6e 64 20 53 51  populated and SQ
49747 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
49748 2e 0d 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  ...** Otherwise,
49749 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
4974a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
4974b 64 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 49 66 20 74 68  d...**..** If th
4974c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62  is function is b
4974d 65 69 6e 67 20 63 61 6c 6c 65 64 20 61 73 20 70  eing called as p
4974e 61 72 74 20 6f 66 20 61 6e 20 78 43 6f 6e 6e 65  art of an xConne
4974f 63 74 28 29 2c 20 74 68 65 6e 20 74 68 65 20 72  ct(), then the r
49750 74 72 65 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 61  tree..** table a
49751 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 49  lready exists. I
49752 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
49753 6e 6f 64 65 2d 73 69 7a 65 20 69 73 20 64 65 74  node-size is det
49754 65 72 6d 69 6e 65 64 20 62 79 20 69 6e 73 70 65  ermined by inspe
49755 63 74 69 6e 67 0d 0a 2a 2a 20 74 68 65 20 72 6f  cting..** the ro
49756 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 74  ot node of the t
49757 72 65 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 4f 74 68  ree...**..** Oth
49758 65 72 77 69 73 65 2c 20 66 6f 72 20 61 6e 20 78  erwise, for an x
49759 43 72 65 61 74 65 28 29 2c 20 75 73 65 20 36 34  Create(), use 64
4975a 20 62 79 74 65 73 20 6c 65 73 73 20 74 68 61 6e   bytes less than
4975b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
4975c 67 65 2d 73 69 7a 65 2e 20 0d 0a 2a 2a 20 54 68  ge-size. ..** Th
4975d 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  is ensures that 
4975e 65 61 63 68 20 6e 6f 64 65 20 69 73 20 73 74 6f  each node is sto
4975f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  red on a single 
49760 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 49  database page. I
49761 66 20 74 68 65 20 0d 0a 2a 2a 20 64 61 74 61 62  f the ..** datab
49762 61 73 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73  ase page-size is
49763 20 73 6f 20 6c 61 72 67 65 20 74 68 61 74 20 6d   so large that m
49764 6f 72 65 20 74 68 61 6e 20 52 54 52 45 45 5f 4d  ore than RTREE_M
49765 41 58 43 45 4c 4c 53 20 65 6e 74 72 69 65 73 20  AXCELLS entries 
49766 0d 0a 2a 2a 20 77 6f 75 6c 64 20 66 69 74 20 69  ..** would fit i
49767 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f 64 65 2c  n a single node,
49768 20 75 73 65 20 61 20 73 6d 61 6c 6c 65 72 20 6e   use a smaller n
49769 6f 64 65 2d 73 69 7a 65 2e 0d 0a 2a 2f 0d 0a 73  ode-size...*/..s
4976a 74 61 74 69 63 20 69 6e 74 20 67 65 74 4e 6f 64  tatic int getNod
4976b 65 53 69 7a 65 28 0d 0a 20 20 73 71 6c 69 74 65  eSize(..  sqlite
4976c 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
4976d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
4976e 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0d  abase handle */.
4976f 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
49770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
49771 20 20 20 2f 2a 20 52 74 72 65 65 20 68 61 6e 64     /* Rtree hand
49772 6c 65 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 43  le */..  int isC
49773 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
49774 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
49775 20 66 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61   for xCreate, fa
49776 6c 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74  lse for xConnect
49777 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69 6e 74 20 72   */..){..  int r
49778 63 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  c;..  char *zSql
49779 3b 0d 0a 20 20 69 66 28 20 69 73 43 72 65 61 74  ;..  if( isCreat
4977a 65 20 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 50  e ){..    int iP
4977b 61 67 65 53 69 7a 65 20 3d 20 30 3b 0d 0a 20 20  ageSize = 0;..  
4977c 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
4977d 5f 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d 41  _mprintf("PRAGMA
4977e 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c 20   %Q.page_size", 
4977f 70 52 74 72 65 65 2d 3e 7a 44 62 29 3b 0d 0a 20  pRtree->zDb);.. 
49780 20 20 20 72 63 20 3d 20 67 65 74 49 6e 74 46 72     rc = getIntFr
49781 6f 6d 53 74 6d 74 28 64 62 2c 20 7a 53 71 6c 2c  omStmt(db, zSql,
49782 20 26 69 50 61 67 65 53 69 7a 65 29 3b 0d 0a 20   &iPageSize);.. 
49783 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
49784 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 20 20 70  E_OK ){..      p
49785 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65  Rtree->iNodeSize
49786 20 3d 20 69 50 61 67 65 53 69 7a 65 2d 36 34 3b   = iPageSize-64;
49787 0d 0a 20 20 20 20 20 20 69 66 28 20 28 34 2b 70  ..      if( (4+p
49788 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
49789 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45  Cell*RTREE_MAXCE
4978a 4c 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f  LLS)<pRtree->iNo
4978b 64 65 53 69 7a 65 20 29 7b 0d 0a 20 20 20 20 20  deSize ){..     
4978c 20 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65     pRtree->iNode
4978d 53 69 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d  Size = 4+pRtree-
4978e 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
4978f 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0d 0a  TREE_MAXCELLS;..
49790 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d 0a        }..    }..
49791 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 7a 53    }else{..    zS
49792 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
49793 69 6e 74 66 28 0d 0a 20 20 20 20 20 20 20 20 22  intf(..        "
49794 53 45 4c 45 43 54 20 6c 65 6e 67 74 68 28 64 61  SELECT length(da
49795 74 61 29 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  ta) FROM '%q'.'%
49796 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e 6f  q_node' WHERE no
49797 64 65 6e 6f 20 3d 20 31 22 2c 0d 0a 20 20 20 20  deno = 1",..    
49798 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c      pRtree->zDb,
49799 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 0d 0a   pRtree->zName..
4979a 20 20 20 20 29 3b 0d 0a 20 20 20 20 72 63 20 3d      );..    rc =
4979b 20 67 65 74 49 6e 74 46 72 6f 6d 53 74 6d 74 28   getIntFromStmt(
4979c 64 62 2c 20 7a 53 71 6c 2c 20 26 70 52 74 72 65  db, zSql, &pRtre
4979d 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0d 0a  e->iNodeSize);..
4979e 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33    }....  sqlite3
4979f 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d 0a 20 20  _free(zSql);..  
497a0 72 65 74 75 72 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d  return rc;..}...
497a1 0a 2f 2a 20 0d 0a 2a 2a 20 54 68 69 73 20 66 75  ./* ..** This fu
497a2 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69 6d  nction is the im
497a3 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
497a4 62 6f 74 68 20 74 68 65 20 78 43 6f 6e 6e 65 63  both the xConnec
497a5 74 20 61 6e 64 20 78 43 72 65 61 74 65 0d 0a 2a  t and xCreate..*
497a6 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65  * methods of the
497a7 20 72 2d 74 72 65 65 20 76 69 72 74 75 61 6c 20   r-tree virtual 
497a8 74 61 62 6c 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20  table...**..**  
497a9 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f   argv[0]   -> mo
497aa 64 75 6c 65 20 6e 61 6d 65 0d 0a 2a 2a 20 20 20  dule name..**   
497ab 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74  argv[1]   -> dat
497ac 61 62 61 73 65 20 6e 61 6d 65 0d 0a 2a 2a 20 20  abase name..**  
497ad 20 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61   argv[2]   -> ta
497ae 62 6c 65 20 6e 61 6d 65 0d 0a 2a 2a 20 20 20 61  ble name..**   a
497af 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75  rgv[...] -> colu
497b0 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0d 0a 2a 2f 0d  mn names.....*/.
497b1 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
497b2 65 49 6e 69 74 28 0d 0a 20 20 73 71 6c 69 74 65  eInit(..  sqlite
497b3 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
497b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
497b5 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
497b6 74 69 6f 6e 20 2a 2f 0d 0a 20 20 76 6f 69 64 20  tion */..  void 
497b7 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20  *pAux,          
497b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
497b9 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52  * One of the RTR
497ba 45 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74  EE_COORD_* const
497bb 61 6e 74 73 20 2a 2f 0d 0a 20 20 69 6e 74 20 61  ants */..  int a
497bc 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
497bd 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f  *const*argv,   /
497be 2a 20 50 61 72 61 6d 65 74 65 72 73 20 74 6f 20  * Parameters to 
497bf 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
497c0 74 65 6d 65 6e 74 20 2a 2f 0d 0a 20 20 73 71 6c  tement */..  sql
497c1 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
497c2 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
497c3 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 76 69 72   /* OUT: New vir
497c4 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0d 0a 20  tual table */.. 
497c5 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 2c 20 20   char **pzErr,  
497c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
497c7 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 72 72       /* OUT: Err
497c8 6f 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20 61  or message, if a
497c9 6e 79 20 2a 2f 0d 0a 20 20 69 6e 74 20 69 73 43  ny */..  int isC
497ca 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
497cb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
497cc 54 72 75 65 20 66 6f 72 20 78 43 72 65 61 74 65  True for xCreate
497cd 2c 20 66 61 6c 73 65 20 66 6f 72 20 78 43 6f 6e  , false for xCon
497ce 6e 65 63 74 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 69  nect */..){..  i
497cf 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
497d0 4b 3b 0d 0a 20 20 52 74 72 65 65 20 2a 70 52 74  K;..  Rtree *pRt
497d1 72 65 65 3b 0d 0a 20 20 69 6e 74 20 6e 44 62 3b  ree;..  int nDb;
497d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
497d3 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e   Length of strin
497d4 67 20 61 72 67 76 5b 31 5d 20 2a 2f 0d 0a 20 20  g argv[1] */..  
497d5 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20  int nName;      
497d6 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
497d7 6f 66 20 73 74 72 69 6e 67 20 61 72 67 76 5b 32  of string argv[2
497d8 5d 20 2a 2f 0d 0a 20 20 69 6e 74 20 65 43 6f 6f  ] */..  int eCoo
497d9 72 64 54 79 70 65 20 3d 20 28 70 41 75 78 20 3f  rdType = (pAux ?
497da 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54   RTREE_COORD_INT
497db 33 32 20 3a 20 52 54 52 45 45 5f 43 4f 4f 52 44  32 : RTREE_COORD
497dc 5f 52 45 41 4c 33 32 29 3b 0d 0a 0d 0a 20 20 63  _REAL32);....  c
497dd 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72 72 4d  onst char *aErrM
497de 73 67 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 30 2c  sg[] = {..    0,
497df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
497e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
497e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
497e2 20 20 20 20 2f 2a 20 30 20 2a 2f 0d 0a 20 20 20      /* 0 */..   
497e3 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
497e4 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
497e5 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
497e6 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0d 0a         /* 1 */..
497e7 20 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c      "Too few col
497e8 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65  umns for an rtre
497e9 65 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20  e table",       
497ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a            /* 2 *
497eb 2f 0d 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79  /..    "Too many
497ec 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20   columns for an 
497ed 72 74 72 65 65 20 74 61 62 6c 65 22 20 20 20 20  rtree table"    
497ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
497ef 33 20 2a 2f 0d 0a 20 20 7d 3b 0d 0a 0d 0a 20 20  3 */..  };....  
497f0 69 6e 74 20 69 45 72 72 20 3d 20 28 61 72 67 63  int iErr = (argc
497f1 3c 36 29 20 3f 20 32 20 3a 20 61 72 67 63 3e 28  <6) ? 2 : argc>(
497f2 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
497f3 49 4f 4e 53 2a 32 2b 34 29 20 3f 20 33 20 3a 20  IONS*2+4) ? 3 : 
497f4 61 72 67 63 25 32 3b 0d 0a 20 20 69 66 28 20 61  argc%2;..  if( a
497f5 45 72 72 4d 73 67 5b 69 45 72 72 5d 20 29 7b 0d  ErrMsg[iErr] ){.
497f6 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
497f7 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
497f8 73 22 2c 20 61 45 72 72 4d 73 67 5b 69 45 72 72  s", aErrMsg[iErr
497f9 5d 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 20  ]);..    return 
497fa 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d 0a 20  SQLITE_ERROR;.. 
497fb 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69 74 65 33 5f   }....  sqlite3_
497fc 76 74 61 62 5f 63 6f 6e 66 69 67 28 64 62 2c 20  vtab_config(db, 
497fd 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53  SQLITE_VTAB_CONS
497fe 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 2c 20  TRAINT_SUPPORT, 
497ff 31 29 3b 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f  1);....  /* Allo
49800 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 33  cate the sqlite3
49801 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20  _vtab structure 
49802 2a 2f 0d 0a 20 20 6e 44 62 20 3d 20 73 74 72 6c  */..  nDb = strl
49803 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0d 0a 20 20  en(argv[1]);..  
49804 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 61  nName = strlen(a
49805 72 67 76 5b 32 5d 29 3b 0d 0a 20 20 70 52 74 72  rgv[2]);..  pRtr
49806 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 73 71  ee = (Rtree *)sq
49807 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
49808 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62 2b 6e  eof(Rtree)+nDb+n
49809 4e 61 6d 65 2b 32 29 3b 0d 0a 20 20 69 66 28 20  Name+2);..  if( 
4980a 21 70 52 74 72 65 65 20 29 7b 0d 0a 20 20 20 20  !pRtree ){..    
4980b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
4980c 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20 6d 65 6d  MEM;..  }..  mem
4980d 73 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73  set(pRtree, 0, s
4980e 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62  izeof(Rtree)+nDb
4980f 2b 6e 4e 61 6d 65 2b 32 29 3b 0d 0a 20 20 70 52  +nName+2);..  pR
49810 74 72 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b  tree->nBusy = 1;
49811 0d 0a 20 20 70 52 74 72 65 65 2d 3e 62 61 73 65  ..  pRtree->base
49812 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72 74 72 65  .pModule = &rtre
49813 65 4d 6f 64 75 6c 65 3b 0d 0a 20 20 70 52 74 72  eModule;..  pRtr
49814 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61 72 20  ee->zDb = (char 
49815 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0d 0a 20  *)&pRtree[1];.. 
49816 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20 3d   pRtree->zName =
49817 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e 44   &pRtree->zDb[nD
49818 62 2b 31 5d 3b 0d 0a 20 20 70 52 74 72 65 65 2d  b+1];..  pRtree-
49819 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29  >nDim = (argc-4)
4981a 2f 32 3b 0d 0a 20 20 70 52 74 72 65 65 2d 3e 6e  /2;..  pRtree->n
4981b 42 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38  BytesPerCell = 8
4981c 20 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a   + pRtree->nDim*
4981d 34 2a 32 3b 0d 0a 20 20 70 52 74 72 65 65 2d 3e  4*2;..  pRtree->
4981e 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f  eCoordType = eCo
4981f 6f 72 64 54 79 70 65 3b 0d 0a 20 20 6d 65 6d 63  ordType;..  memc
49820 70 79 28 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20  py(pRtree->zDb, 
49821 61 72 67 76 5b 31 5d 2c 20 6e 44 62 29 3b 0d 0a  argv[1], nDb);..
49822 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65 2d    memcpy(pRtree-
49823 3e 7a 4e 61 6d 65 2c 20 61 72 67 76 5b 32 5d 2c  >zName, argv[2],
49824 20 6e 4e 61 6d 65 29 3b 0d 0a 0d 0a 20 20 2f 2a   nName);....  /*
49825 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
49826 6e 6f 64 65 20 73 69 7a 65 20 74 6f 20 75 73 65  node size to use
49827 2e 20 2a 2f 0d 0a 20 20 72 63 20 3d 20 67 65 74  . */..  rc = get
49828 4e 6f 64 65 53 69 7a 65 28 64 62 2c 20 70 52 74  NodeSize(db, pRt
49829 72 65 65 2c 20 69 73 43 72 65 61 74 65 29 3b 0d  ree, isCreate);.
4982a 0a 0d 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 43  ...  /* Create/C
4982b 6f 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75 6e  onnect to the un
4982c 64 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69 6f  derlying relatio
4982d 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 63 68  nal database sch
4982e 65 6d 61 2e 20 49 66 0d 0a 20 20 2a 2a 20 74 68  ema. If..  ** th
4982f 61 74 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  at is successful
49830 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64  , call sqlite3_d
49831 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f  eclare_vtab() to
49832 20 63 6f 6e 66 69 67 75 72 65 0d 0a 20 20 2a 2a   configure..  **
49833 20 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c   the r-tree tabl
49834 65 20 73 63 68 65 6d 61 2e 0d 0a 20 20 2a 2f 0d  e schema...  */.
49835 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
49836 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 69 66 28  E_OK ){..    if(
49837 20 28 72 63 20 3d 20 72 74 72 65 65 53 71 6c 49   (rc = rtreeSqlI
49838 6e 69 74 28 70 52 74 72 65 65 2c 20 64 62 2c 20  nit(pRtree, db, 
49839 61 72 67 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d  argv[1], argv[2]
4983a 2c 20 69 73 43 72 65 61 74 65 29 29 20 29 7b 0d  , isCreate)) ){.
4983b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  .      *pzErr = 
4983c 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
4983d 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  "%s", sqlite3_er
4983e 72 6d 73 67 28 64 62 29 29 3b 0d 0a 20 20 20 20  rmsg(db));..    
4983f 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 63 68  }else{..      ch
49840 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74  ar *zSql = sqlit
49841 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41  e3_mprintf("CREA
49842 54 45 20 54 41 42 4c 45 20 78 28 25 73 22 2c 20  TE TABLE x(%s", 
49843 61 72 67 76 5b 33 5d 29 3b 0d 0a 20 20 20 20 20  argv[3]);..     
49844 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0d 0a 20 20   char *zTmp;..  
49845 20 20 20 20 69 6e 74 20 69 69 3b 0d 0a 20 20 20      int ii;..   
49846 20 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53 71     for(ii=4; zSq
49847 6c 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69 69  l && ii<argc; ii
49848 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 20 20 7a 54  ++){..        zT
49849 6d 70 20 3d 20 7a 53 71 6c 3b 0d 0a 20 20 20 20  mp = zSql;..    
4984a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
4984b 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 20  e3_mprintf("%s, 
4984c 25 73 22 2c 20 7a 54 6d 70 2c 20 61 72 67 76 5b  %s", zTmp, argv[
4984d 69 69 5d 29 3b 0d 0a 20 20 20 20 20 20 20 20 73  ii]);..        s
4984e 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 6d 70  qlite3_free(zTmp
4984f 29 3b 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20  );..      }..   
49850 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0d 0a     if( zSql ){..
49851 20 20 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a          zTmp = z
49852 53 71 6c 3b 0d 0a 20 20 20 20 20 20 20 20 7a 53  Sql;..        zS
49853 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
49854 69 6e 74 66 28 22 25 73 29 3b 22 2c 20 7a 54 6d  intf("%s);", zTm
49855 70 29 3b 0d 0a 20 20 20 20 20 20 20 20 73 71 6c  p);..        sql
49856 69 74 65 33 5f 66 72 65 65 28 7a 54 6d 70 29 3b  ite3_free(zTmp);
49857 0d 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20  ..      }..     
49858 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0d 0a 20   if( !zSql ){.. 
49859 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
4985a 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 20 20 20  TE_NOMEM;..     
4985b 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
4985c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
4985d 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
4985e 28 64 62 2c 20 7a 53 71 6c 29 29 20 29 7b 0d 0a  (db, zSql)) ){..
4985f 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d          *pzErr =
49860 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
49861 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65  ("%s", sqlite3_e
49862 72 72 6d 73 67 28 64 62 29 29 3b 0d 0a 20 20 20  rrmsg(db));..   
49863 20 20 20 7d 0d 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
49864 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0d  te3_free(zSql);.
49865 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
49866 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
49867 4f 4b 20 29 7b 0d 0a 20 20 20 20 2a 70 70 56 74  OK ){..    *ppVt
49868 61 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  ab = (sqlite3_vt
49869 61 62 20 2a 29 70 52 74 72 65 65 3b 0d 0a 20 20  ab *)pRtree;..  
4986a 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 72 74 72 65  }else{..    rtre
4986b 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 29  eRelease(pRtree)
4986c 3b 0d 0a 20 20 7d 0d 0a 20 20 72 65 74 75 72 6e  ;..  }..  return
4986d 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 0d 0a 2f 2a 0d   rc;..}....../*.
4986e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
4986f 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20 66  on of a scalar f
49870 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 63  unction that dec
49871 6f 64 65 73 20 72 2d 74 72 65 65 20 6e 6f 64 65  odes r-tree node
49872 73 20 74 6f 0d 0a 2a 2a 20 68 75 6d 61 6e 20 72  s to..** human r
49873 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67 73 2e  eadable strings.
49874 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
49875 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20  d for debugging 
49876 61 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0d 0a 2a  and analysis...*
49877 2a 0d 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61 72  *..** The scalar
49878 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20   function takes 
49879 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2c 20 61  two arguments, a
4987a 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 63 6f   blob of data co
4987b 6e 74 61 69 6e 69 6e 67 0d 0a 2a 2a 20 61 6e 20  ntaining..** an 
4987c 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61 6e 64  r-tree node, and
4987d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
4987e 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20 72 2d  imensions the r-
4987f 74 72 65 65 20 69 6e 64 65 78 65 73 2e 0d 0a 2a  tree indexes...*
49880 2a 20 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d 65  * For a two-dime
49881 6e 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20 73  nsional r-tree s
49882 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65 64 20  tructure called 
49883 22 72 74 22 2c 20 74 6f 20 64 65 73 65 72 69 61  "rt", to deseria
49884 6c 69 7a 65 0d 0a 2a 2a 20 61 6c 6c 20 6e 6f 64  lize..** all nod
49885 65 73 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20  es, a statement 
49886 6c 69 6b 65 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  like:..**..**   
49887 53 45 4c 45 43 54 20 72 74 72 65 65 6e 6f 64 65  SELECT rtreenode
49888 28 32 2c 20 64 61 74 61 29 20 46 52 4f 4d 20 72  (2, data) FROM r
49889 74 5f 6e 6f 64 65 3b 0d 0a 2a 2a 0d 0a 2a 2a 20  t_node;..**..** 
4988a 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61 62  The human readab
4988b 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73 20  le string takes 
4988c 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54 63  the form of a Tc
4988d 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65 0d  l list with one.
4988e 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61  .** entry for ea
4988f 63 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72  ch cell in the r
49890 2d 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68  -tree node. Each
49891 20 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66   entry is itself
49892 20 61 0d 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e   a..** list, con
49893 74 61 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79  taining the 8-by
49894 74 65 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20  te rowid/pageno 
49895 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20  followed by the 
49896 0d 0a 2a 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73  ..** <num-dimens
49897 69 6f 6e 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74  ion>*2 coordinat
49898 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  es...*/..static 
49899 76 6f 69 64 20 72 74 72 65 65 6e 6f 64 65 28 73  void rtreenode(s
4989a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
4989b 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73  ctx, int nArg, s
4989c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
4989d 70 41 72 67 29 7b 0d 0a 20 20 63 68 61 72 20 2a  pArg){..  char *
4989e 7a 54 65 78 74 20 3d 20 30 3b 0d 0a 20 20 52 74  zText = 0;..  Rt
4989f 72 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0d 0a 20  reeNode node;.. 
498a0 20 52 74 72 65 65 20 74 72 65 65 3b 0d 0a 20 20   Rtree tree;..  
498a1 69 6e 74 20 69 69 3b 0d 0a 0d 0a 20 20 55 4e 55  int ii;....  UNU
498a2 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 41  SED_PARAMETER(nA
498a3 72 67 29 3b 0d 0a 20 20 6d 65 6d 73 65 74 28 26  rg);..  memset(&
498a4 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  node, 0, sizeof(
498a5 52 74 72 65 65 4e 6f 64 65 29 29 3b 0d 0a 20 20  RtreeNode));..  
498a6 6d 65 6d 73 65 74 28 26 74 72 65 65 2c 20 30 2c  memset(&tree, 0,
498a7 20 73 69 7a 65 6f 66 28 52 74 72 65 65 29 29 3b   sizeof(Rtree));
498a8 0d 0a 20 20 74 72 65 65 2e 6e 44 69 6d 20 3d 20  ..  tree.nDim = 
498a9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
498aa 74 28 61 70 41 72 67 5b 30 5d 29 3b 0d 0a 20 20  t(apArg[0]);..  
498ab 74 72 65 65 2e 6e 42 79 74 65 73 50 65 72 43 65  tree.nBytesPerCe
498ac 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20 74 72 65  ll = 8 + 8 * tre
498ad 65 2e 6e 44 69 6d 3b 0d 0a 20 20 6e 6f 64 65 2e  e.nDim;..  node.
498ae 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 73 71  zData = (u8 *)sq
498af 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
498b0 28 61 70 41 72 67 5b 31 5d 29 3b 0d 0a 0d 0a 20  (apArg[1]);.... 
498b1 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43   for(ii=0; ii<NC
498b2 45 4c 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b  ELL(&node); ii++
498b3 29 7b 0d 0a 20 20 20 20 63 68 61 72 20 7a 43 65  ){..    char zCe
498b4 6c 6c 5b 35 31 32 5d 3b 0d 0a 20 20 20 20 69 6e  ll[512];..    in
498b5 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0d 0a 20 20  t nCell = 0;..  
498b6 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
498b7 3b 0d 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0d 0a  ;..    int jj;..
498b8 0d 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  ..    nodeGetCel
498b9 6c 28 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20  l(&tree, &node, 
498ba 69 69 2c 20 26 63 65 6c 6c 29 3b 0d 0a 20 20 20  ii, &cell);..   
498bb 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
498bc 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65  f(512-nCell,&zCe
498bd 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 25 6c 6c 64 22  ll[nCell],"%lld"
498be 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0d  , cell.iRowid);.
498bf 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72  .    nCell = str
498c0 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0d 0a 20 20 20  len(zCell);..   
498c1 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 74 72   for(jj=0; jj<tr
498c2 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b 2b 29  ee.nDim*2; jj++)
498c3 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  {..      sqlite3
498c4 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43  _snprintf(512-nC
498c5 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c  ell,&zCell[nCell
498c6 5d 2c 22 20 25 66 22 2c 28 64 6f 75 62 6c 65 29  ]," %f",(double)
498c7 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 2e  cell.aCoord[jj].
498c8 66 29 3b 0d 0a 20 20 20 20 20 20 6e 43 65 6c 6c  f);..      nCell
498c9 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29   = strlen(zCell)
498ca 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20 20 20  ;..    }....    
498cb 69 66 28 20 7a 54 65 78 74 20 29 7b 0d 0a 20 20  if( zText ){..  
498cc 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 4e      char *zTextN
498cd 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ew = sqlite3_mpr
498ce 69 6e 74 66 28 22 25 73 20 7b 25 73 7d 22 2c 20  intf("%s {%s}", 
498cf 7a 54 65 78 74 2c 20 7a 43 65 6c 6c 29 3b 0d 0a  zText, zCell);..
498d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
498d1 65 65 28 7a 54 65 78 74 29 3b 0d 0a 20 20 20 20  ee(zText);..    
498d2 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78 74 4e    zText = zTextN
498d3 65 77 3b 0d 0a 20 20 20 20 7d 65 6c 73 65 7b 0d  ew;..    }else{.
498d4 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73  .      zText = s
498d5 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
498d6 7b 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0d 0a  {%s}", zCell);..
498d7 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 20 20 0d 0a      }..  }..  ..
498d8 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
498d9 5f 74 65 78 74 28 63 74 78 2c 20 7a 54 65 78 74  _text(ctx, zText
498da 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72  , -1, sqlite3_fr
498db 65 65 29 3b 0d 0a 7d 0d 0a 0d 0a 73 74 61 74 69  ee);..}....stati
498dc 63 20 76 6f 69 64 20 72 74 72 65 65 64 65 70 74  c void rtreedept
498dd 68 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  h(sqlite3_contex
498de 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67  t *ctx, int nArg
498df 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
498e0 2a 2a 61 70 41 72 67 29 7b 0d 0a 20 20 55 4e 55  **apArg){..  UNU
498e1 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 41  SED_PARAMETER(nA
498e2 72 67 29 3b 0d 0a 20 20 69 66 28 20 73 71 6c 69  rg);..  if( sqli
498e3 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
498e4 70 41 72 67 5b 30 5d 29 21 3d 53 51 4c 49 54 45  pArg[0])!=SQLITE
498e5 5f 42 4c 4f 42 20 0d 0a 20 20 20 7c 7c 20 73 71  _BLOB ..   || sq
498e6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
498e7 73 28 61 70 41 72 67 5b 30 5d 29 3c 32 0d 0a 20  s(apArg[0])<2.. 
498e8 20 29 7b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33   ){..    sqlite3
498e9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 74  _result_error(ct
498ea 78 2c 20 22 49 6e 76 61 6c 69 64 20 61 72 67 75  x, "Invalid argu
498eb 6d 65 6e 74 20 74 6f 20 72 74 72 65 65 64 65 70  ment to rtreedep
498ec 74 68 28 29 22 2c 20 2d 31 29 3b 20 0d 0a 20 20  th()", -1); ..  
498ed 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 75 38 20 2a  }else{..    u8 *
498ee 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a 29 73 71  zBlob = (u8 *)sq
498ef 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
498f0 28 61 70 41 72 67 5b 30 5d 29 3b 0d 0a 20 20 20  (apArg[0]);..   
498f1 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
498f2 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49 6e 74  int(ctx, readInt
498f3 31 36 28 7a 42 6c 6f 62 29 29 3b 0d 0a 20 20 7d  16(zBlob));..  }
498f4 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 52 65  ..}..../*..** Re
498f5 67 69 73 74 65 72 20 74 68 65 20 72 2d 74 72 65  gister the r-tre
498f6 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 64 61  e module with da
498f7 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
498f8 2e 20 54 68 69 73 20 63 72 65 61 74 65 73 20 74  . This creates t
498f9 68 65 0d 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  he..** virtual t
498fa 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74 72  able module "rtr
498fb 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62 75  ee" and the debu
498fc 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20 73  gging/analysis s
498fd 63 61 6c 61 72 20 0d 0a 2a 2a 20 66 75 6e 63 74  calar ..** funct
498fe 69 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e  ion "rtreenode".
498ff 0d 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/..SQLITE_PRI
49900 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49901 52 74 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65  RtreeInit(sqlite
49902 33 20 2a 64 62 29 7b 0d 0a 20 20 63 6f 6e 73 74  3 *db){..  const
49903 20 69 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49   int utf8 = SQLI
49904 54 45 5f 55 54 46 38 3b 0d 0a 20 20 69 6e 74 20  TE_UTF8;..  int 
49905 72 63 3b 0d 0a 0d 0a 20 20 72 63 20 3d 20 73 71  rc;....  rc = sq
49906 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
49907 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65 65  ction(db, "rtree
49908 6e 6f 64 65 22 2c 20 32 2c 20 75 74 66 38 2c 20  node", 2, utf8, 
49909 30 2c 20 72 74 72 65 65 6e 6f 64 65 2c 20 30 2c  0, rtreenode, 0,
4990a 20 30 29 3b 0d 0a 20 20 69 66 28 20 72 63 3d 3d   0);..  if( rc==
4990b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20  SQLITE_OK ){..  
4990c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
4990d 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
4990e 62 2c 20 22 72 74 72 65 65 64 65 70 74 68 22 2c  b, "rtreedepth",
4990f 20 31 2c 20 75 74 66 38 2c 20 30 2c 72 74 72 65   1, utf8, 0,rtre
49910 65 64 65 70 74 68 2c 20 30 2c 20 30 29 3b 0d 0a  edepth, 0, 0);..
49911 20 20 7d 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
49912 51 4c 49 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20  QLITE_OK ){..   
49913 20 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64   void *c = (void
49914 20 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52   *)RTREE_COORD_R
49915 45 41 4c 33 32 3b 0d 0a 20 20 20 20 72 63 20 3d  EAL32;..    rc =
49916 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
49917 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20 22 72  module_v2(db, "r
49918 74 72 65 65 22 2c 20 26 72 74 72 65 65 4d 6f 64  tree", &rtreeMod
49919 75 6c 65 2c 20 63 2c 20 30 29 3b 0d 0a 20 20 7d  ule, c, 0);..  }
4991a 0d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
4991b 54 45 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 76 6f  TE_OK ){..    vo
4991c 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29  id *c = (void *)
4991d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33  RTREE_COORD_INT3
4991e 32 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  2;..    rc = sql
4991f 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
49920 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65  le_v2(db, "rtree
49921 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f 64  _i32", &rtreeMod
49922 75 6c 65 2c 20 63 2c 20 30 29 3b 0d 0a 20 20 7d  ule, c, 0);..  }
49923 0d 0a 0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ....  return rc;
49924 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 41 20  ..}..../*..** A 
49925 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74  version of sqlit
49926 65 33 5f 66 72 65 65 28 29 20 74 68 61 74 20 63  e3_free() that c
49927 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 20  an be used as a 
49928 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20 69  callback. This i
49929 73 20 75 73 65 64 0d 0a 2a 2a 20 69 6e 20 74 77  s used..** in tw
4992a 6f 20 70 6c 61 63 65 73 20 2d 20 61 73 20 74 68  o places - as th
4992b 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
4992c 20 74 68 65 20 62 6c 6f 62 20 76 61 6c 75 65 20   the blob value 
4992d 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0d  returned by the.
4992e 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  .** invocation o
4992f 66 20 61 20 67 65 6f 6d 65 74 72 79 20 66 75 6e  f a geometry fun
49930 63 74 69 6f 6e 2c 20 61 6e 64 20 61 73 20 74 68  ction, and as th
49931 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
49932 20 74 68 65 20 67 65 6f 6d 65 74 72 79 0d 0a 2a   the geometry..*
49933 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6d  * functions them
49934 73 65 6c 76 65 73 2e 0d 0a 2a 2f 0d 0a 73 74 61  selves...*/..sta
49935 74 69 63 20 76 6f 69 64 20 64 6f 53 71 6c 69 74  tic void doSqlit
49936 65 33 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  e3Free(void *p){
49937 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
49938 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a  (p);..}..../*..*
49939 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73  * Each call to s
4993a 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f  qlite3_rtree_geo
4993b 6d 65 74 72 79 5f 63 61 6c 6c 62 61 63 6b 28 29  metry_callback()
4993c 20 63 72 65 61 74 65 73 20 61 6e 20 6f 72 64 69   creates an ordi
4993d 6e 61 72 79 20 53 51 4c 69 74 65 0d 0a 2a 2a 20  nary SQLite..** 
4993e 73 63 61 6c 61 72 20 75 73 65 72 20 66 75 6e 63  scalar user func
4993f 74 69 6f 6e 2e 20 54 68 69 73 20 43 20 66 75 6e  tion. This C fun
49940 63 74 69 6f 6e 20 69 73 20 74 68 65 20 63 61 6c  ction is the cal
49941 6c 62 61 63 6b 20 75 73 65 64 20 66 6f 72 20 61  lback used for a
49942 6c 6c 20 73 75 63 68 0d 0a 2a 2a 20 72 65 67 69  ll such..** regi
49943 73 74 65 72 65 64 20 53 51 4c 20 66 75 6e 63 74  stered SQL funct
49944 69 6f 6e 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68  ions...**..** Th
49945 65 20 73 63 61 6c 61 72 20 75 73 65 72 20 66 75  e scalar user fu
49946 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61  nctions return a
49947 20 62 6c 6f 62 20 74 68 61 74 20 69 73 20 69 6e   blob that is in
49948 74 65 72 70 72 65 74 65 64 20 62 79 20 72 2d 74  terpreted by r-t
49949 72 65 65 0d 0a 2a 2a 20 74 61 62 6c 65 20 4d 41  ree..** table MA
4994a 54 43 48 20 6f 70 65 72 61 74 6f 72 73 2e 0d 0a  TCH operators...
4994b 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
4994c 67 65 6f 6d 43 61 6c 6c 62 61 63 6b 28 73 71 6c  geomCallback(sql
4994d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74  ite3_context *ct
4994e 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c  x, int nArg, sql
4994f 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 41 72  ite3_value **aAr
49950 67 29 7b 0d 0a 20 20 52 74 72 65 65 47 65 6f 6d  g){..  RtreeGeom
49951 43 61 6c 6c 62 61 63 6b 20 2a 70 47 65 6f 6d 43  Callback *pGeomC
49952 74 78 20 3d 20 28 52 74 72 65 65 47 65 6f 6d 43  tx = (RtreeGeomC
49953 61 6c 6c 62 61 63 6b 20 2a 29 73 71 6c 69 74 65  allback *)sqlite
49954 33 5f 75 73 65 72 5f 64 61 74 61 28 63 74 78 29  3_user_data(ctx)
49955 3b 0d 0a 20 20 52 74 72 65 65 4d 61 74 63 68 41  ;..  RtreeMatchA
49956 72 67 20 2a 70 42 6c 6f 62 3b 0d 0a 20 20 69 6e  rg *pBlob;..  in
49957 74 20 6e 42 6c 6f 62 3b 0d 0a 0d 0a 20 20 6e 42  t nBlob;....  nB
49958 6c 6f 62 20 3d 20 73 69 7a 65 6f 66 28 52 74 72  lob = sizeof(Rtr
49959 65 65 4d 61 74 63 68 41 72 67 29 20 2b 20 28 6e  eeMatchArg) + (n
4995a 41 72 67 2d 31 29 2a 73 69 7a 65 6f 66 28 64 6f  Arg-1)*sizeof(do
4995b 75 62 6c 65 29 3b 0d 0a 20 20 70 42 6c 6f 62 20  uble);..  pBlob 
4995c 3d 20 28 52 74 72 65 65 4d 61 74 63 68 41 72 67  = (RtreeMatchArg
4995d 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
4995e 63 28 6e 42 6c 6f 62 29 3b 0d 0a 20 20 69 66 28  c(nBlob);..  if(
4995f 20 21 70 42 6c 6f 62 20 29 7b 0d 0a 20 20 20 20   !pBlob ){..    
49960 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
49961 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 74 78 29 3b  rror_nomem(ctx);
49962 0d 0a 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20  ..  }else{..    
49963 69 6e 74 20 69 3b 0d 0a 20 20 20 20 70 42 6c 6f  int i;..    pBlo
49964 62 2d 3e 6d 61 67 69 63 20 3d 20 52 54 52 45 45  b->magic = RTREE
49965 5f 47 45 4f 4d 45 54 52 59 5f 4d 41 47 49 43 3b  _GEOMETRY_MAGIC;
49966 0d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 78 47 65  ..    pBlob->xGe
49967 6f 6d 20 3d 20 70 47 65 6f 6d 43 74 78 2d 3e 78  om = pGeomCtx->x
49968 47 65 6f 6d 3b 0d 0a 20 20 20 20 70 42 6c 6f 62  Geom;..    pBlob
49969 2d 3e 70 43 6f 6e 74 65 78 74 20 3d 20 70 47 65  ->pContext = pGe
4996a 6f 6d 43 74 78 2d 3e 70 43 6f 6e 74 65 78 74 3b  omCtx->pContext;
4996b 0d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 50 61  ..    pBlob->nPa
4996c 72 61 6d 20 3d 20 6e 41 72 67 3b 0d 0a 20 20 20  ram = nArg;..   
4996d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 72 67   for(i=0; i<nArg
4996e 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 20 20 70  ; i++){..      p
4996f 42 6c 6f 62 2d 3e 61 50 61 72 61 6d 5b 69 5d 20  Blob->aParam[i] 
49970 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
49971 64 6f 75 62 6c 65 28 61 41 72 67 5b 69 5d 29 3b  double(aArg[i]);
49972 0d 0a 20 20 20 20 7d 0d 0a 20 20 20 20 73 71 6c  ..    }..    sql
49973 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
49974 28 63 74 78 2c 20 70 42 6c 6f 62 2c 20 6e 42 6c  (ctx, pBlob, nBl
49975 6f 62 2c 20 64 6f 53 71 6c 69 74 65 33 46 72 65  ob, doSqlite3Fre
49976 65 29 3b 0d 0a 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f  e);..  }..}..../
49977 2a 0d 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  *..** Register a
49978 20 6e 65 77 20 67 65 6f 6d 65 74 72 79 20 66 75   new geometry fu
49979 6e 63 74 69 6f 6e 20 66 6f 72 20 75 73 65 20 77  nction for use w
4997a 69 74 68 20 74 68 65 20 72 2d 74 72 65 65 20 4d  ith the r-tree M
4997b 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 0d 0a  ATCH operator...
4997c 2a 2f 0d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  */..SQLITE_API i
4997d 6e 74 20 73 71 6c 69 74 65 33 5f 72 74 72 65 65  nt sqlite3_rtree
4997e 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c 6c 62 61  _geometry_callba
4997f 63 6b 28 0d 0a 20 20 73 71 6c 69 74 65 33 20 2a  ck(..  sqlite3 *
49980 64 62 2c 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  db,..  const cha
49981 72 20 2a 7a 47 65 6f 6d 2c 0d 0a 20 20 69 6e 74  r *zGeom,..  int
49982 20 28 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74 65   (*xGeom)(sqlite
49983 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
49984 20 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 20   *, int, double 
49985 2a 2c 20 69 6e 74 20 2a 29 2c 0d 0a 20 20 76 6f  *, int *),..  vo
49986 69 64 20 2a 70 43 6f 6e 74 65 78 74 0d 0a 29 7b  id *pContext..){
49987 0d 0a 20 20 52 74 72 65 65 47 65 6f 6d 43 61 6c  ..  RtreeGeomCal
49988 6c 62 61 63 6b 20 2a 70 47 65 6f 6d 43 74 78 3b  lback *pGeomCtx;
49989 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
4998a 20 6f 62 6a 65 63 74 20 66 6f 72 20 6e 65 77 20   object for new 
4998b 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 2a 2f  user-function */
4998c 0d 0a 0d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ....  /* Allocat
4998d 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74  e and populate t
4998e 68 65 20 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63  he context objec
4998f 74 2e 20 2a 2f 0d 0a 20 20 70 47 65 6f 6d 43 74  t. */..  pGeomCt
49990 78 20 3d 20 28 52 74 72 65 65 47 65 6f 6d 43 61  x = (RtreeGeomCa
49991 6c 6c 62 61 63 6b 20 2a 29 73 71 6c 69 74 65 33  llback *)sqlite3
49992 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
49993 74 72 65 65 47 65 6f 6d 43 61 6c 6c 62 61 63 6b  treeGeomCallback
49994 29 29 3b 0d 0a 20 20 69 66 28 20 21 70 47 65 6f  ));..  if( !pGeo
49995 6d 43 74 78 20 29 20 72 65 74 75 72 6e 20 53 51  mCtx ) return SQ
49996 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 70  LITE_NOMEM;..  p
49997 47 65 6f 6d 43 74 78 2d 3e 78 47 65 6f 6d 20 3d  GeomCtx->xGeom =
49998 20 78 47 65 6f 6d 3b 0d 0a 20 20 70 47 65 6f 6d   xGeom;..  pGeom
49999 43 74 78 2d 3e 70 43 6f 6e 74 65 78 74 20 3d 20  Ctx->pContext = 
4999a 70 43 6f 6e 74 65 78 74 3b 0d 0a 0d 0a 20 20 2f  pContext;....  /
4999b 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77  * Create the new
4999c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 2e 20   user-function. 
4999d 52 65 67 69 73 74 65 72 20 61 20 64 65 73 74 72  Register a destr
4999e 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 74  uctor function t
4999f 6f 20 64 65 6c 65 74 65 0d 0a 20 20 2a 2a 20 74  o delete..  ** t
499a0 68 65 20 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63  he context objec
499a1 74 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f 20  t when it is no 
499a2 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e  longer required.
499a3 20 20 2a 2f 0d 0a 20 20 72 65 74 75 72 6e 20 73    */..  return s
499a4 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
499a5 6e 63 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a 47  nction_v2(db, zG
499a6 65 6f 6d 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  eom, -1, SQLITE_
499a7 41 4e 59 2c 20 0d 0a 20 20 20 20 20 20 28 76 6f  ANY, ..      (vo
499a8 69 64 20 2a 29 70 47 65 6f 6d 43 74 78 2c 20 67  id *)pGeomCtx, g
499a9 65 6f 6d 43 61 6c 6c 62 61 63 6b 2c 20 30 2c 20  eomCallback, 0, 
499aa 30 2c 20 64 6f 53 71 6c 69 74 65 33 46 72 65 65  0, doSqlite3Free
499ab 0d 0a 20 20 29 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66  ..  );..}....#if
499ac 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0d 0a 53   !SQLITE_CORE..S
499ad 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
499ae 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
499af 69 6e 69 74 28 0d 0a 20 20 73 71 6c 69 74 65 33  init(..  sqlite3
499b0 20 2a 64 62 2c 0d 0a 20 20 63 68 61 72 20 2a 2a   *db,..  char **
499b1 70 7a 45 72 72 4d 73 67 2c 0d 0a 20 20 63 6f 6e  pzErrMsg,..  con
499b2 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
499b3 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0d 0a 29  outines *pApi..)
499b4 7b 0d 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45  {..  SQLITE_EXTE
499b5 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69  NSION_INIT2(pApi
499b6 29 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  )..  return sqli
499b7 74 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29  te3RtreeInit(db)
499b8 3b 0d 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a  ;..}..#endif....
499b9 23 65 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a  #endif..../*****
499ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
499bb 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   rtree.c *******
499bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499be 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a  ********/../****
499bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
499c0 20 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a 2a 2a   file icu.c ****
499c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a  *********/../*..
499c4 2a 2a 20 32 30 30 37 20 4d 61 79 20 36 0d 0a 2a  ** 2007 May 6..*
499c5 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
499c6 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
499c7 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
499c8 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
499c9 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
499ca 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
499cb 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
499cc 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
499cd 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
499ce 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
499cf 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
499d0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
499d1 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
499d2 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
499d3 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
499d4 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
499d5 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
499d6 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
499d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
499db 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 24 49 64 3a 20 69  *****..** $Id: i
499dc 63 75 2e 63 2c 76 20 31 2e 37 20 32 30 30 37 2f  cu.c,v 1.7 2007/
499dd 31 32 2f 31 33 20 32 31 3a 35 34 3a 31 31 20 64  12/13 21:54:11 d
499de 72 68 20 45 78 70 20 24 0d 0a 2a 2a 0d 0a 2a 2a  rh Exp $..**..**
499df 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
499e0 6d 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 72 61  ments an integra
499e1 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 65  tion between the
499e2 20 49 43 55 20 6c 69 62 72 61 72 79 20 0d 0a 2a   ICU library ..*
499e3 2a 20 28 22 49 6e 74 65 72 6e 61 74 69 6f 6e 61  * ("Internationa
499e4 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f 72  l Components for
499e5 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e 20 6f 70   Unicode", an op
499e6 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62 72 61 72  en-source librar
499e7 79 20 0d 0a 2a 2a 20 66 6f 72 20 68 61 6e 64 6c  y ..** for handl
499e8 69 6e 67 20 75 6e 69 63 6f 64 65 20 64 61 74 61  ing unicode data
499e9 29 20 61 6e 64 20 53 51 4c 69 74 65 2e 20 54 68  ) and SQLite. Th
499ea 65 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 75 73  e integration us
499eb 65 73 20 0d 0a 2a 2a 20 49 43 55 20 74 6f 20 70  es ..** ICU to p
499ec 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f  rovide the follo
499ed 77 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a 0d  wing to SQLite:.
499ee 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 41 6e 20 69  .**..**   * An i
499ef 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
499f0 20 74 68 65 20 53 51 4c 20 72 65 67 65 78 70 28   the SQL regexp(
499f1 29 20 66 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20  ) function (and 
499f2 68 65 6e 63 65 20 52 45 47 45 58 50 0d 0a 2a 2a  hence REGEXP..**
499f3 20 20 20 20 20 6f 70 65 72 61 74 6f 72 29 20 75       operator) u
499f4 73 69 6e 67 20 74 68 65 20 49 43 55 20 75 72 65  sing the ICU ure
499f5 67 65 78 5f 58 58 28 29 20 41 50 49 73 2e 0d 0a  gex_XX() APIs...
499f6 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65  **..**   * Imple
499f7 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
499f8 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70 70  e SQL scalar upp
499f9 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29  er() and lower()
499fa 20 66 75 6e 63 74 69 6f 6e 73 0d 0a 2a 2a 20 20   functions..**  
499fb 20 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70     for case mapp
499fc 69 6e 67 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a  ing...**..**   *
499fd 20 49 6e 74 65 67 72 61 74 69 6f 6e 20 6f 66 20   Integration of 
499fe 49 43 55 20 61 6e 64 20 53 51 4c 69 74 65 20 63  ICU and SQLite c
499ff 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 65 6e 63 65  ollation seqence
49a00 73 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 2a 20 41  s...**..**   * A
49a01 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
49a02 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 70 65   of the LIKE ope
49a03 72 61 74 6f 72 20 74 68 61 74 20 75 73 65 73 20  rator that uses 
49a04 49 43 55 20 74 6f 20 0d 0a 2a 2a 20 20 20 20 20  ICU to ..**     
49a05 70 72 6f 76 69 64 65 20 63 61 73 65 2d 69 6e 64  provide case-ind
49a06 65 70 65 6e 64 65 6e 74 20 6d 61 74 63 68 69 6e  ependent matchin
49a07 67 2e 0d 0a 2a 2f 0d 0a 0d 0a 23 69 66 20 21 64  g...*/....#if !d
49a08 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
49a09 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
49a0a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
49a0b 29 0d 0a 0d 0a 2f 2a 20 49 6e 63 6c 75 64 65 20  )..../* Include 
49a0c 49 43 55 20 68 65 61 64 65 72 73 20 2a 2f 0d 0a  ICU headers */..
49a0d 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
49a0e 65 2f 75 74 79 70 65 73 2e 68 3e 0d 0a 23 69 6e  e/utypes.h>..#in
49a0f 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
49a10 72 65 67 65 78 2e 68 3e 0d 0a 23 69 6e 63 6c 75  regex.h>..#inclu
49a11 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74 72  de <unicode/ustr
49a12 69 6e 67 2e 68 3e 0d 0a 23 69 6e 63 6c 75 64 65  ing.h>..#include
49a13 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e 68   <unicode/ucol.h
49a14 3e 0d 0a 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  >..../* #include
49a15 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0d 0a   <assert.h> */..
49a16 0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
49a17 5f 43 4f 52 45 0d 0a 20 20 53 51 4c 49 54 45 5f  _CORE..  SQLITE_
49a18 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0d  EXTENSION_INIT1.
49a19 0a 23 65 6c 73 65 0d 0a 23 65 6e 64 69 66 0d 0a  .#else..#endif..
49a1a 0d 0a 2f 2a 0d 0a 2a 2a 20 4d 61 78 69 6d 75 6d  ../*..** Maximum
49a1b 20 6c 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65   length (in byte
49a1c 73 29 20 6f 66 20 74 68 65 20 70 61 74 74 65 72  s) of the patter
49a1d 6e 20 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47  n in a LIKE or G
49a1e 4c 4f 42 0d 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  LOB..** operator
49a1f 2e 0d 0a 2a 2f 0d 0a 23 69 66 6e 64 65 66 20 53  ...*/..#ifndef S
49a20 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
49a21 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0d 0a 23  ATTERN_LENGTH..#
49a22 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
49a23 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
49a24 4c 45 4e 47 54 48 20 35 30 30 30 30 0d 0a 23 65  LENGTH 50000..#e
49a25 6e 64 69 66 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 56  ndif..../*..** V
49a26 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
49a27 33 5f 66 72 65 65 28 29 20 74 68 61 74 20 69 73  3_free() that is
49a28 20 61 6c 77 61 79 73 20 61 20 66 75 6e 63 74 69   always a functi
49a29 6f 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61 63 72  on, never a macr
49a2a 6f 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  o...*/..static v
49a2b 6f 69 64 20 78 46 72 65 65 28 76 6f 69 64 20 2a  oid xFree(void *
49a2c 70 29 7b 0d 0a 20 20 73 71 6c 69 74 65 33 5f 66  p){..  sqlite3_f
49a2d 72 65 65 28 70 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a  ree(p);..}..../*
49a2e 0d 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f  ..** Compare two
49a2f 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66   UTF-8 strings f
49a30 6f 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72  or equality wher
49a31 65 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69  e the first stri
49a32 6e 67 20 69 73 0d 0a 2a 2a 20 61 20 22 4c 49 4b  ng is..** a "LIK
49a33 45 22 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 52  E" expression. R
49a34 65 74 75 72 6e 20 74 72 75 65 20 28 31 29 20 69  eturn true (1) i
49a35 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 73  f they are the s
49a36 61 6d 65 20 61 6e 64 20 0d 0a 2a 2a 20 66 61 6c  ame and ..** fal
49a37 73 65 20 28 30 29 20 69 66 20 74 68 65 79 20 61  se (0) if they a
49a38 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0d 0a 2a  re different...*
49a39 2f 0d 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  /..static int ic
49a3a 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 0d 0a 20  uLikeCompare(.. 
49a3b 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a   const uint8_t *
49a3c 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4c  zPattern,   /* L
49a3d 49 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f 0d 0a  IKE pattern */..
49a3e 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20    const uint8_t 
49a3f 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a 20  *zString,    /* 
49a40 54 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67  The UTF-8 string
49a41 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69   to compare agai
49a42 6e 73 74 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  nst */..  const 
49a43 55 43 68 61 72 33 32 20 75 45 73 63 20 20 20 20  UChar32 uEsc    
49a44 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
49a45 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0d  pe character */.
49a46 0a 29 7b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f  .){..  static co
49a47 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f 4e  nst int MATCH_ON
49a48 45 20 3d 20 28 55 43 68 61 72 33 32 29 27 5f 27  E = (UChar32)'_'
49a49 3b 0d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ;..  static cons
49a4a 74 20 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20  t int MATCH_ALL 
49a4b 3d 20 28 55 43 68 61 72 33 32 29 27 25 27 3b 0d  = (UChar32)'%';.
49a4c 0a 0d 0a 20 20 69 6e 74 20 69 50 61 74 74 65 72  ...  int iPatter
49a4d 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  n = 0;       /* 
49a4e 43 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64  Current byte ind
49a4f 65 78 20 69 6e 20 7a 50 61 74 74 65 72 6e 20 2a  ex in zPattern *
49a50 2f 0d 0a 20 20 69 6e 74 20 69 53 74 72 69 6e 67  /..  int iString
49a51 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
49a52 43 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64  Current byte ind
49a53 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20 2a 2f  ex in zString */
49a54 0d 0a 0d 0a 20 20 69 6e 74 20 70 72 65 76 45 73  ....  int prevEs
49a55 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  cape = 0;     /*
49a56 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65   True if the pre
49a57 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20  vious character 
49a58 77 61 73 20 75 45 73 63 20 2a 2f 0d 0a 0d 0a 20  was uEsc */.... 
49a59 20 77 68 69 6c 65 28 20 7a 50 61 74 74 65 72 6e   while( zPattern
49a5a 5b 69 50 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b  [iPattern]!=0 ){
49a5b 0d 0a 0d 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ....    /* Read 
49a5c 28 61 6e 64 20 63 6f 6e 73 75 6d 65 29 20 74 68  (and consume) th
49a5d 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 72  e next character
49a5e 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20   from the input 
49a5f 70 61 74 74 65 72 6e 2e 20 2a 2f 0d 0a 20 20 20  pattern. */..   
49a60 20 55 43 68 61 72 33 32 20 75 50 61 74 74 65 72   UChar32 uPatter
49a61 6e 3b 0d 0a 20 20 20 20 55 38 5f 4e 45 58 54 5f  n;..    U8_NEXT_
49a62 55 4e 53 41 46 45 28 7a 50 61 74 74 65 72 6e 2c  UNSAFE(zPattern,
49a63 20 69 50 61 74 74 65 72 6e 2c 20 75 50 61 74 74   iPattern, uPatt
49a64 65 72 6e 29 3b 0d 0a 20 20 20 20 61 73 73 65 72  ern);..    asser
49a65 74 28 75 50 61 74 74 65 72 6e 21 3d 30 29 3b 0d  t(uPattern!=0);.
49a66 0a 0d 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  ...    /* There 
49a67 61 72 65 20 6e 6f 77 20 34 20 70 6f 73 73 69 62  are now 4 possib
49a68 69 6c 69 74 69 65 73 3a 0d 0a 20 20 20 20 2a 2a  ilities:..    **
49a69 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 31 2e 20  ..    **     1. 
49a6a 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
49a6b 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d 61  nescaped match-a
49a6c 6c 6c 20 63 68 61 72 61 63 74 65 72 20 22 25 22  ll character "%"
49a6d 2c 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 32 2e  ,..    **     2.
49a6e 20 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20   uPattern is an 
49a6f 75 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d  unescaped match-
49a70 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 22 5f  one character "_
49a71 22 2c 0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 33  ",..    **     3
49a72 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e  . uPattern is an
49a73 20 75 6e 65 73 63 61 70 65 64 20 65 73 63 61 70   unescaped escap
49a74 65 20 63 68 61 72 61 63 74 65 72 2c 20 6f 72 0d  e character, or.
49a75 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e 20 75  .    **     4. u
49a76 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20 62 65  Pattern is to be
49a77 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e 20 6f   handled as an o
49a78 72 64 69 6e 61 72 79 20 63 68 61 72 61 63 74 65  rdinary characte
49a79 72 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69  r..    */..    i
49a7a 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
49a7b 26 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43  & uPattern==MATC
49a7c 48 5f 41 4c 4c 20 29 7b 0d 0a 20 20 20 20 20 20  H_ALL ){..      
49a7d 2f 2a 20 43 61 73 65 20 31 2e 20 2a 2f 0d 0a 20  /* Case 1. */.. 
49a7e 20 20 20 20 20 75 69 6e 74 38 5f 74 20 63 3b 0d       uint8_t c;.
49a7f 0a 0d 0a 20 20 20 20 20 20 2f 2a 20 53 6b 69 70  ...      /* Skip
49a80 20 61 6e 79 20 4d 41 54 43 48 5f 41 4c 4c 20 6f   any MATCH_ALL o
49a81 72 20 4d 41 54 43 48 5f 4f 4e 45 20 63 68 61 72  r MATCH_ONE char
49a82 61 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c  acters that foll
49a83 6f 77 20 61 0d 0a 20 20 20 20 20 20 2a 2a 20 4d  ow a..      ** M
49a84 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72 20 65 61  ATCH_ALL. For ea
49a85 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c 20 73 6b  ch MATCH_ONE, sk
49a86 69 70 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ip one character
49a87 20 69 6e 20 74 68 65 20 0d 0a 20 20 20 20 20 20   in the ..      
49a88 2a 2a 20 74 65 73 74 20 73 74 72 69 6e 67 2e 0d  ** test string..
49a89 0a 20 20 20 20 20 20 2a 2f 0d 0a 20 20 20 20 20  .      */..     
49a8a 20 77 68 69 6c 65 28 20 28 63 3d 7a 50 61 74 74   while( (c=zPatt
49a8b 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d  ern[iPattern]) =
49a8c 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63  = MATCH_ALL || c
49a8d 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b   == MATCH_ONE ){
49a8e 0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  ..        if( c=
49a8f 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0d 0a 20  =MATCH_ONE ){.. 
49a90 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 74           if( zSt
49a91 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30  ring[iString]==0
49a92 20 29 20 72 65 74 75 72 6e 20 30 3b 0d 0a 20 20   ) return 0;..  
49a93 20 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31          U8_FWD_1
49a94 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
49a95 20 69 53 74 72 69 6e 67 29 3b 0d 0a 20 20 20 20   iString);..    
49a96 20 20 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 69      }..        i
49a97 50 61 74 74 65 72 6e 2b 2b 3b 0d 0a 20 20 20 20  Pattern++;..    
49a98 20 20 7d 0d 0a 0d 0a 20 20 20 20 20 20 69 66 28    }....      if(
49a99 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65   zPattern[iPatte
49a9a 72 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rn]==0 ) return 
49a9b 31 3b 0d 0a 0d 0a 20 20 20 20 20 20 77 68 69 6c  1;....      whil
49a9c 65 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69  e( zString[iStri
49a9d 6e 67 5d 20 29 7b 0d 0a 20 20 20 20 20 20 20 20  ng] ){..        
49a9e 69 66 28 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61  if( icuLikeCompa
49a9f 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 69 50 61  re(&zPattern[iPa
49aa0 74 74 65 72 6e 5d 2c 20 26 7a 53 74 72 69 6e 67  ttern], &zString
49aa1 5b 69 53 74 72 69 6e 67 5d 2c 20 75 45 73 63 29  [iString], uEsc)
49aa2 20 29 7b 0d 0a 20 20 20 20 20 20 20 20 20 20 72   ){..          r
49aa3 65 74 75 72 6e 20 31 3b 0d 0a 20 20 20 20 20 20  eturn 1;..      
49aa4 20 20 7d 0d 0a 20 20 20 20 20 20 20 20 55 38 5f    }..        U8_
49aa5 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74  FWD_1_UNSAFE(zSt
49aa6 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0d  ring, iString);.
49aa7 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
49aa8 72 65 74 75 72 6e 20 30 3b 0d 0a 0d 0a 20 20 20  return 0;....   
49aa9 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65 76   }else if( !prev
49aaa 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74 65  Escape && uPatte
49aab 72 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b  rn==MATCH_ONE ){
49aac 0d 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  ..      /* Case 
49aad 32 2e 20 2a 2f 0d 0a 20 20 20 20 20 20 69 66 28  2. */..      if(
49aae 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
49aaf 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ]==0 ) return 0;
49ab0 0d 0a 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31  ..      U8_FWD_1
49ab1 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
49ab2 20 69 53 74 72 69 6e 67 29 3b 0d 0a 0d 0a 20 20   iString);....  
49ab3 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
49ab4 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
49ab5 65 72 6e 3d 3d 75 45 73 63 29 7b 0d 0a 20 20 20  ern==uEsc){..   
49ab6 20 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f     /* Case 3. */
49ab7 0d 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61  ..      prevEsca
49ab8 70 65 20 3d 20 31 3b 0d 0a 0d 0a 20 20 20 20 7d  pe = 1;....    }
49ab9 65 6c 73 65 7b 0d 0a 20 20 20 20 20 20 2f 2a 20  else{..      /* 
49aba 43 61 73 65 20 34 2e 20 2a 2f 0d 0a 20 20 20 20  Case 4. */..    
49abb 20 20 55 43 68 61 72 33 32 20 75 53 74 72 69 6e    UChar32 uStrin
49abc 67 3b 0d 0a 20 20 20 20 20 20 55 38 5f 4e 45 58  g;..      U8_NEX
49abd 54 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67  T_UNSAFE(zString
49abe 2c 20 69 53 74 72 69 6e 67 2c 20 75 53 74 72 69  , iString, uStri
49abf 6e 67 29 3b 0d 0a 20 20 20 20 20 20 75 53 74 72  ng);..      uStr
49ac0 69 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65  ing = u_foldCase
49ac1 28 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c 44  (uString, U_FOLD
49ac2 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0d  _CASE_DEFAULT);.
49ac3 0a 20 20 20 20 20 20 75 50 61 74 74 65 72 6e 20  .      uPattern 
49ac4 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75 50 61  = u_foldCase(uPa
49ac5 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41  ttern, U_FOLD_CA
49ac6 53 45 5f 44 45 46 41 55 4c 54 29 3b 0d 0a 20 20  SE_DEFAULT);..  
49ac7 20 20 20 20 69 66 28 20 75 53 74 72 69 6e 67 21      if( uString!
49ac8 3d 75 50 61 74 74 65 72 6e 20 29 7b 0d 0a 20 20  =uPattern ){..  
49ac9 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0d        return 0;.
49aca 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
49acb 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 0d  prevEscape = 0;.
49acc 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20  .    }..  }.... 
49acd 20 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b   return zString[
49ace 69 53 74 72 69 6e 67 5d 3d 3d 30 3b 0d 0a 7d 0d  iString]==0;..}.
49acf 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c 65 6d  .../*..** Implem
49ad0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
49ad1 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74  like() SQL funct
49ad2 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74  ion.  This funct
49ad3 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0d 0a  ion implements..
49ad4 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
49ad5 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20  LIKE operator.  
49ad6 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
49ad7 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
49ad8 6f 6e 20 69 73 20 74 68 65 0d 0a 2a 2a 20 70 61  on is the..** pa
49ad9 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65  ttern and the se
49ada 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
49adb 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f   the string.  So
49adc 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  , the SQL statem
49add 65 6e 74 73 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20  ents:..**..**   
49ade 20 20 20 20 41 20 4c 49 4b 45 20 42 0d 0a 2a 2a      A LIKE B..**
49adf 0d 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e  ..** is implemen
49ae0 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 20 41  ted as like(B, A
49ae1 29 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ). If there is a
49ae2 6e 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74  n escape charact
49ae3 65 72 20 45 2c 20 0d 0a 2a 2a 0d 0a 2a 2a 20 20  er E, ..**..**  
49ae4 20 20 20 20 20 41 20 4c 49 4b 45 20 42 20 45 53       A LIKE B ES
49ae5 43 41 50 45 20 45 0d 0a 2a 2a 0d 0a 2a 2a 20 69  CAPE E..**..** i
49ae6 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69 6b 65  s mapped to like
49ae7 28 42 2c 20 41 2c 20 45 29 2e 0d 0a 2a 2f 0d 0a  (B, A, E)...*/..
49ae8 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c  static void icuL
49ae9 69 6b 65 46 75 6e 63 28 0d 0a 20 20 73 71 6c 69  ikeFunc(..  sqli
49aea 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
49aeb 74 65 78 74 2c 20 0d 0a 20 20 69 6e 74 20 61 72  text, ..  int ar
49aec 67 63 2c 20 0d 0a 20 20 73 71 6c 69 74 65 33 5f  gc, ..  sqlite3_
49aed 76 61 6c 75 65 20 2a 2a 61 72 67 76 0d 0a 29 7b  value **argv..){
49aee 0d 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ..  const unsign
49aef 65 64 20 63 68 61 72 20 2a 7a 41 20 3d 20 73 71  ed char *zA = sq
49af0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
49af1 28 61 72 67 76 5b 30 5d 29 3b 0d 0a 20 20 63 6f  (argv[0]);..  co
49af2 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
49af3 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f  r *zB = sqlite3_
49af4 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
49af5 31 5d 29 3b 0d 0a 20 20 55 43 68 61 72 33 32 20  1]);..  UChar32 
49af6 75 45 73 63 20 3d 20 30 3b 0d 0a 0d 0a 20 20 2f  uEsc = 0;....  /
49af7 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67  * Limit the leng
49af8 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f  th of the LIKE o
49af9 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
49afa 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73  o avoid problems
49afb 0d 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72  ..  ** of deep r
49afc 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e  ecursion and N*N
49afd 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74   behavior in pat
49afe 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0d 0a  ternCompare()...
49aff 20 20 2a 2f 0d 0a 20 20 69 66 28 20 73 71 6c 69    */..  if( sqli
49b00 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
49b01 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f  argv[0])>SQLITE_
49b02 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
49b03 5f 4c 45 4e 47 54 48 20 29 7b 0d 0a 20 20 20 20  _LENGTH ){..    
49b04 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
49b05 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c  rror(context, "L
49b06 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
49b07 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22  ern too complex"
49b08 2c 20 2d 31 29 3b 0d 0a 20 20 20 20 72 65 74 75  , -1);..    retu
49b09 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 0d 0a 20 20  rn;..  }......  
49b0a 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0d 0a  if( argc==3 ){..
49b0b 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70      /* The escap
49b0c 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69  e character stri
49b0d 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 74 20  ng must consist 
49b0e 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d  of a single UTF-
49b0f 38 20 63 68 61 72 61 63 74 65 72 2e 0d 0a 20 20  8 character...  
49b10 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    ** Otherwise, 
49b11 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
49b12 0d 0a 20 20 20 20 2a 2f 0d 0a 20 20 20 20 69 6e  ..    */..    in
49b13 74 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f 76 61  t nE= sqlite3_va
49b14 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32  lue_bytes(argv[2
49b15 5d 29 3b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 75  ]);..    const u
49b16 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45  nsigned char *zE
49b17 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
49b18 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0d  _text(argv[2]);.
49b19 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0d  .    int i = 0;.
49b1a 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d 30 20 29  .    if( zE==0 )
49b1b 20 72 65 74 75 72 6e 3b 0d 0a 20 20 20 20 55 38   return;..    U8
49b1c 5f 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45 2c  _NEXT(zE, i, nE,
49b1d 20 75 45 73 63 29 3b 0d 0a 20 20 20 20 69 66 28   uEsc);..    if(
49b1e 20 69 21 3d 6e 45 29 7b 0d 0a 20 20 20 20 20 20   i!=nE){..      
49b1f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
49b20 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0d 0a  rror(context, ..
49b21 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50            "ESCAP
49b22 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73  E expression mus
49b23 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68  t be a single ch
49b24 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0d 0a  aracter", -1);..
49b25 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20        return;.. 
49b26 20 20 20 7d 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 69     }..  }....  i
49b27 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0d 0a  f( zA && zB ){..
49b28 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
49b29 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
49b2a 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 7a  icuLikeCompare(z
49b2b 41 2c 20 7a 42 2c 20 75 45 73 63 29 29 3b 0d 0a  A, zB, uEsc));..
49b2c 20 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a    }..}..../*..**
49b2d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
49b2e 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  s called when an
49b2f 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 63 61   ICU function ca
49b30 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
49b31 0d 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  ..** the impleme
49b32 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51  ntation of an SQ
49b33 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
49b34 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  n returns an err
49b35 6f 72 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 68 65 20  or...**..** The 
49b36 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
49b37 63 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20 61  context passed a
49b38 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
49b39 6d 65 6e 74 20 69 73 20 0d 0a 2a 2a 20 6c 6f 61  ment is ..** loa
49b3a 64 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ded with an erro
49b3b 72 20 6d 65 73 73 61 67 65 20 62 61 73 65 64 20  r message based 
49b3c 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  on the following
49b3d 20 74 77 6f 20 61 72 67 73 2e 0d 0a 2a 2f 0d 0a   two args...*/..
49b3e 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 46  static void icuF
49b3f 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0d 0a 20  unctionError(.. 
49b40 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
49b41 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f 2a   *pCtx,       /*
49b42 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20 66   SQLite scalar f
49b43 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
49b44 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
49b45 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
49b46 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43     /* Name of IC
49b47 55 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  U function that 
49b48 66 61 69 6c 65 64 20 2a 2f 0d 0a 20 20 55 45 72  failed */..  UEr
49b49 72 6f 72 43 6f 64 65 20 65 20 20 20 20 20 20 20  rorCode e       
49b4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
49b4b 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
49b4c 20 62 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e   by ICU function
49b4d 20 2a 2f 0d 0a 29 7b 0d 0a 20 20 63 68 61 72 20   */..){..  char 
49b4e 7a 42 75 66 5b 31 32 38 5d 3b 0d 0a 20 20 73 71  zBuf[128];..  sq
49b4f 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
49b50 32 38 2c 20 7a 42 75 66 2c 20 22 49 43 55 20 65  28, zBuf, "ICU e
49b51 72 72 6f 72 3a 20 25 73 28 29 3a 20 25 73 22 2c  rror: %s(): %s",
49b52 20 7a 4e 61 6d 65 2c 20 75 5f 65 72 72 6f 72 4e   zName, u_errorN
49b53 61 6d 65 28 65 29 29 3b 0d 0a 20 20 7a 42 75 66  ame(e));..  zBuf
49b54 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b 0d 0a 20  [127] = '\0';.. 
49b55 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
49b56 65 72 72 6f 72 28 70 43 74 78 2c 20 7a 42 75 66  error(pCtx, zBuf
49b57 2c 20 2d 31 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  , -1);..}..../*.
49b58 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20  .** Function to 
49b59 64 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64 20  delete compiled 
49b5a 72 65 67 65 78 70 20 6f 62 6a 65 63 74 73 2e 20  regexp objects. 
49b5b 52 65 67 69 73 74 65 72 65 64 20 61 73 0d 0a 2a  Registered as..*
49b5c 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66  * a destructor f
49b5d 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 71 6c  unction with sql
49b5e 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
49b5f 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ()...*/..static 
49b60 76 6f 69 64 20 69 63 75 52 65 67 65 78 70 44 65  void icuRegexpDe
49b61 6c 65 74 65 28 76 6f 69 64 20 2a 70 29 7b 0d 0a  lete(void *p){..
49b62 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65 73    URegularExpres
49b63 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20 28 55  sion *pExpr = (U
49b64 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f  RegularExpressio
49b65 6e 20 2a 29 70 3b 0d 0a 20 20 75 72 65 67 65 78  n *)p;..  uregex
49b66 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0d 0a  _close(pExpr);..
49b67 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49 6d 70 6c  }..../*..** Impl
49b68 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51  ementation of SQ
49b69 4c 69 74 65 20 52 45 47 45 58 50 20 6f 70 65 72  Lite REGEXP oper
49b6a 61 74 6f 72 2e 20 54 68 69 73 20 73 63 61 6c 61  ator. This scala
49b6b 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73  r function takes
49b6c 0d 0a 2a 2a 20 74 77 6f 20 61 72 67 75 6d 65 6e  ..** two argumen
49b6d 74 73 2e 20 54 68 65 20 66 69 72 73 74 20 69 73  ts. The first is
49b6e 20 61 20 72 65 67 75 6c 61 72 20 65 78 70 72 65   a regular expre
49b6f 73 73 69 6f 6e 20 70 61 74 74 65 72 6e 20 74 6f  ssion pattern to
49b70 20 63 6f 6d 70 69 6c 65 0d 0a 2a 2a 20 74 68 65   compile..** the
49b71 20 73 65 63 6f 6e 64 20 69 73 20 61 20 73 74 72   second is a str
49b72 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 61 67 61  ing to match aga
49b73 69 6e 73 74 20 74 68 61 74 20 70 61 74 74 65 72  inst that patter
49b74 6e 2e 20 49 66 20 65 69 74 68 65 72 20 0d 0a 2a  n. If either ..*
49b75 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  * argument is an
49b76 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   SQL NULL, then 
49b77 4e 55 4c 4c 20 49 73 20 72 65 74 75 72 6e 65 64  NULL Is returned
49b78 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
49b79 20 72 65 73 75 6c 74 0d 0a 2a 2a 20 69 73 20 31   result..** is 1
49b7a 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 6d   if the string m
49b7b 61 74 63 68 65 73 20 74 68 65 20 70 61 74 74 65  atches the patte
49b7c 72 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69  rn, or 0 otherwi
49b7d 73 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 53 51 4c 69  se...**..** SQLi
49b7e 74 65 20 6d 61 70 73 20 74 68 65 20 72 65 67 65  te maps the rege
49b7f 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  xp() function to
49b80 20 74 68 65 20 72 65 67 65 78 70 28 29 20 6f 70   the regexp() op
49b81 65 72 61 74 6f 72 20 73 75 63 68 0d 0a 2a 2a 20  erator such..** 
49b82 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  that the followi
49b83 6e 67 20 74 77 6f 20 61 72 65 20 65 71 75 69 76  ng two are equiv
49b84 61 6c 65 6e 74 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20  alent:..**..**  
49b85 20 20 20 7a 53 74 72 69 6e 67 20 52 45 47 45 58     zString REGEX
49b86 50 20 7a 50 61 74 74 65 72 6e 0d 0a 2a 2a 20 20  P zPattern..**  
49b87 20 20 20 72 65 67 65 78 70 28 7a 50 61 74 74 65     regexp(zPatte
49b88 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0d 0a 2a 2a  rn, zString)..**
49b89 0d 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66 6f  ..** Uses the fo
49b8a 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67 65  llowing ICU rege
49b8b 78 70 20 41 50 49 73 3a 0d 0a 2a 2a 0d 0a 2a 2a  xp APIs:..**..**
49b8c 20 20 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e       uregex_open
49b8d 28 29 0d 0a 2a 2a 20 20 20 20 20 75 72 65 67 65  ()..**     urege
49b8e 78 5f 6d 61 74 63 68 65 73 28 29 0d 0a 2a 2a 20  x_matches()..** 
49b8f 20 20 20 20 75 72 65 67 65 78 5f 63 6c 6f 73 65      uregex_close
49b90 28 29 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76  ()..*/..static v
49b91 6f 69 64 20 69 63 75 52 65 67 65 78 70 46 75 6e  oid icuRegexpFun
49b92 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
49b93 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20  t *p, int nArg, 
49b94 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
49b95 61 70 41 72 67 29 7b 0d 0a 20 20 55 45 72 72 6f  apArg){..  UErro
49b96 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55  rCode status = U
49b97 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0d 0a 20 20  _ZERO_ERROR;..  
49b98 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69  URegularExpressi
49b99 6f 6e 20 2a 70 45 78 70 72 3b 0d 0a 20 20 55 42  on *pExpr;..  UB
49b9a 6f 6f 6c 20 72 65 73 3b 0d 0a 20 20 63 6f 6e 73  ool res;..  cons
49b9b 74 20 55 43 68 61 72 20 2a 7a 53 74 72 69 6e 67  t UChar *zString
49b9c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
49b9d 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 31 5d  _text16(apArg[1]
49b9e 29 3b 0d 0a 0d 0a 20 20 28 76 6f 69 64 29 6e 41  );....  (void)nA
49b9f 72 67 3b 20 20 2f 2a 20 55 6e 75 73 65 64 20 70  rg;  /* Unused p
49ba0 61 72 61 6d 65 74 65 72 20 2a 2f 0d 0a 0d 0a 20  arameter */.... 
49ba1 20 2f 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20   /* If the left 
49ba2 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
49ba3 20 72 65 67 65 78 70 20 6f 70 65 72 61 74 6f 72   regexp operator
49ba4 20 69 73 20 4e 55 4c 4c 2c 20 0d 0a 20 20 2a 2a   is NULL, ..  **
49ba5 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
49ba6 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 0d   is also NULL. .
49ba7 0a 20 20 2a 2f 0d 0a 20 20 69 66 28 20 21 7a 53  .  */..  if( !zS
49ba8 74 72 69 6e 67 20 29 7b 0d 0a 20 20 20 20 72 65  tring ){..    re
49ba9 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20  turn;..  }....  
49baa 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f  pExpr = sqlite3_
49bab 67 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30  get_auxdata(p, 0
49bac 29 3b 0d 0a 20 20 69 66 28 20 21 70 45 78 70 72  );..  if( !pExpr
49bad 20 29 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 55   ){..    const U
49bae 43 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 20 3d  Char *zPattern =
49baf 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
49bb0 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b  ext16(apArg[0]);
49bb1 0d 0a 20 20 20 20 69 66 28 20 21 7a 50 61 74 74  ..    if( !zPatt
49bb2 65 72 6e 20 29 7b 0d 0a 20 20 20 20 20 20 72 65  ern ){..      re
49bb3 74 75 72 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20  turn;..    }..  
49bb4 20 20 70 45 78 70 72 20 3d 20 75 72 65 67 65 78    pExpr = uregex
49bb5 5f 6f 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20  _open(zPattern, 
49bb6 2d 31 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75  -1, 0, 0, &statu
49bb7 73 29 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 55  s);....    if( U
49bb8 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
49bb9 20 29 7b 0d 0a 20 20 20 20 20 20 73 71 6c 69 74   ){..      sqlit
49bba 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 70  e3_set_auxdata(p
49bbb 2c 20 30 2c 20 70 45 78 70 72 2c 20 69 63 75 52  , 0, pExpr, icuR
49bbc 65 67 65 78 70 44 65 6c 65 74 65 29 3b 0d 0a 20  egexpDelete);.. 
49bbd 20 20 20 7d 65 6c 73 65 7b 0d 0a 20 20 20 20 20     }else{..     
49bbe 20 61 73 73 65 72 74 28 21 70 45 78 70 72 29 3b   assert(!pExpr);
49bbf 0d 0a 20 20 20 20 20 20 69 63 75 46 75 6e 63 74  ..      icuFunct
49bc0 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
49bc1 67 65 78 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75  gex_open", statu
49bc2 73 29 3b 0d 0a 20 20 20 20 20 20 72 65 74 75 72  s);..      retur
49bc3 6e 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  n;..    }..  }..
49bc4 0d 0a 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65  ..  /* Configure
49bc5 20 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74   the text that t
49bc6 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  he regular expre
49bc7 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f  ssion operates o
49bc8 6e 2e 20 2a 2f 0d 0a 20 20 75 72 65 67 65 78 5f  n. */..  uregex_
49bc9 73 65 74 54 65 78 74 28 70 45 78 70 72 2c 20 7a  setText(pExpr, z
49bca 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73 74 61  String, -1, &sta
49bcb 74 75 73 29 3b 0d 0a 20 20 69 66 28 20 21 55 5f  tus);..  if( !U_
49bcc 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
49bcd 29 7b 0d 0a 20 20 20 20 69 63 75 46 75 6e 63 74  ){..    icuFunct
49bce 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
49bcf 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73 74  gex_setText", st
49bd0 61 74 75 73 29 3b 0d 0a 20 20 20 20 72 65 74 75  atus);..    retu
49bd1 72 6e 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a  rn;..  }....  /*
49bd2 20 41 74 74 65 6d 70 74 20 74 68 65 20 6d 61 74   Attempt the mat
49bd3 63 68 20 2a 2f 0d 0a 20 20 72 65 73 20 3d 20 75  ch */..  res = u
49bd4 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28 70 45  regex_matches(pE
49bd5 78 70 72 2c 20 30 2c 20 26 73 74 61 74 75 73 29  xpr, 0, &status)
49bd6 3b 0d 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43  ;..  if( !U_SUCC
49bd7 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0d 0a  ESS(status) ){..
49bd8 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
49bd9 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f  rror(p, "uregex_
49bda 6d 61 74 63 68 65 73 22 2c 20 73 74 61 74 75 73  matches", status
49bdb 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0d  );..    return;.
49bdc 0a 20 20 7d 0d 0a 0d 0a 20 20 2f 2a 20 53 65 74  .  }....  /* Set
49bdd 20 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74   the text that t
49bde 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  he regular expre
49bdf 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f  ssion operates o
49be0 6e 20 74 6f 20 61 20 4e 55 4c 4c 0d 0a 20 20 2a  n to a NULL..  *
49be1 2a 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20  * pointer. This 
49be2 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65  is not really ne
49be3 63 65 73 73 61 72 79 2c 20 62 75 74 20 69 74 20  cessary, but it 
49be4 69 73 20 74 69 64 69 65 72 20 74 68 61 6e 20 0d  is tidier than .
49be5 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68  .  ** leaving th
49be6 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
49be7 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66  sion object conf
49be8 69 67 75 72 65 64 20 77 69 74 68 20 61 6e 20 69  igured with an i
49be9 6e 76 61 6c 69 64 0d 0a 20 20 2a 2a 20 70 6f 69  nvalid..  ** poi
49bea 6e 74 65 72 20 61 66 74 65 72 20 74 68 69 73 20  nter after this 
49beb 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
49bec 2e 0d 0a 20 20 2a 2f 0d 0a 20 20 75 72 65 67 65  ...  */..  urege
49bed 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
49bee 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b   0, 0, &status);
49bef 0d 0a 0d 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  ....  /* Return 
49bf0 31 20 6f 72 20 30 2e 20 2a 2f 0d 0a 20 20 73 71  1 or 0. */..  sq
49bf1 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
49bf2 28 70 2c 20 72 65 73 20 3f 20 31 20 3a 20 30 29  (p, res ? 1 : 0)
49bf3 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 49  ;..}..../*..** I
49bf4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
49bf5 66 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  f scalar functio
49bf6 6e 73 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70  ns for case mapp
49bf7 69 6e 67 20 2d 20 75 70 70 65 72 28 29 20 61 6e  ing - upper() an
49bf8 64 20 0d 0a 2a 2a 20 6c 6f 77 65 72 28 29 2e 20  d ..** lower(). 
49bf9 46 75 6e 63 74 69 6f 6e 20 75 70 70 65 72 28 29  Function upper()
49bfa 20 63 6f 6e 76 65 72 74 73 20 69 74 73 20 69 6e   converts its in
49bfb 70 75 74 20 74 6f 20 75 70 70 65 72 2d 63 61 73  put to upper-cas
49bfc 65 20 28 41 42 43 29 2e 0d 0a 2a 2a 20 46 75 6e  e (ABC)...** Fun
49bfd 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29 20 63 6f  ction lower() co
49bfe 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77 65 72 2d  nverts to lower-
49bff 63 61 73 65 20 28 61 62 63 29 2e 0d 0a 2a 2a 0d  case (abc)...**.
49c00 0a 2a 2a 20 49 43 55 20 70 72 6f 76 69 64 65 73  .** ICU provides
49c01 20 74 77 6f 20 74 79 70 65 73 20 6f 66 20 63 61   two types of ca
49c02 73 65 20 6d 61 70 70 69 6e 67 2c 20 22 67 65 6e  se mapping, "gen
49c03 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70 70 69  eral" case mappi
49c04 6e 67 20 61 6e 64 0d 0a 2a 2a 20 22 6c 61 6e 67  ng and..** "lang
49c05 75 61 67 65 20 73 70 65 63 69 66 69 63 22 2e 20  uage specific". 
49c06 52 65 66 65 72 20 74 6f 20 49 43 55 20 64 6f 63  Refer to ICU doc
49c07 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74  umentation for t
49c08 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 0d 0a  he differences..
49c09 2a 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  ** between the t
49c0a 77 6f 2e 0d 0a 2a 2a 0d 0a 2a 2a 20 54 6f 20 75  wo...**..** To u
49c0b 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61 6c 22  tilise "general"
49c0c 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 74   case mapping, t
49c0d 68 65 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f  he upper() or lo
49c0e 77 65 72 28 29 20 73 63 61 6c 61 72 20 0d 0a 2a  wer() scalar ..*
49c0f 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
49c10 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65  invoked with one
49c11 20 61 72 67 75 6d 65 6e 74 3a 0d 0a 2a 2a 0d 0a   argument:..**..
49c12 2a 2a 20 20 20 20 20 75 70 70 65 72 28 27 41 42  **     upper('AB
49c13 43 27 29 20 2d 3e 20 27 61 62 63 27 0d 0a 2a 2a  C') -> 'abc'..**
49c14 20 20 20 20 20 6c 6f 77 65 72 28 27 61 62 63 27       lower('abc'
49c15 29 20 2d 3e 20 27 41 42 43 27 0d 0a 2a 2a 0d 0a  ) -> 'ABC'..**..
49c16 2a 2a 20 54 6f 20 61 63 63 65 73 73 20 49 43 55  ** To access ICU
49c17 20 22 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69   "language speci
49c18 66 69 63 22 20 63 61 73 65 20 6d 61 70 70 69 6e  fic" case mappin
49c19 67 2c 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f  g, upper() or lo
49c1a 77 65 72 28 29 0d 0a 2a 2a 20 73 68 6f 75 6c 64  wer()..** should
49c1b 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
49c1c 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20   two arguments. 
49c1d 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
49c1e 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 0d  ent is the name.
49c1f 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 6f 63 61 6c  .** of the local
49c20 65 20 74 6f 20 75 73 65 2e 20 50 61 73 73 69 6e  e to use. Passin
49c21 67 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  g an empty strin
49c22 67 20 28 22 22 29 20 6f 72 20 53 51 4c 20 4e 55  g ("") or SQL NU
49c23 4c 4c 20 76 61 6c 75 65 0d 0a 2a 2a 20 61 73 20  LL value..** as 
49c24 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
49c25 65 6e 74 20 69 73 20 74 68 65 20 73 61 6d 65 20  ent is the same 
49c26 61 73 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  as invoking the 
49c27 31 20 61 72 67 75 6d 65 6e 74 20 76 65 72 73 69  1 argument versi
49c28 6f 6e 0d 0a 2a 2a 20 6f 66 20 75 70 70 65 72 28  on..** of upper(
49c29 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e 0d 0a 2a  ) or lower()...*
49c2a 2a 0d 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28  *..**     lower(
49c2b 27 49 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d 3e  'I', 'en_us') ->
49c2c 20 27 69 27 0d 0a 2a 2a 20 20 20 20 20 6c 6f 77   'i'..**     low
49c2d 65 72 28 27 49 27 2c 20 27 74 72 5f 74 72 27 29  er('I', 'tr_tr')
49c2e 20 2d 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20   -> '..' (small 
49c2f 64 6f 74 6c 65 73 73 20 69 29 0d 0a 2a 2a 0d 0a  dotless i)..**..
49c30 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e 69 63  ** http://www.ic
49c31 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f 75 73  u-project.org/us
49c32 65 72 67 75 69 64 65 2f 70 6f 73 69 78 2e 68 74  erguide/posix.ht
49c33 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e 67 73  ml#case_mappings
49c34 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 76 6f 69  ..*/..static voi
49c35 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 28  d icuCaseFunc16(
49c36 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
49c37 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71  *p, int nArg, sq
49c38 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
49c39 41 72 67 29 7b 0d 0a 20 20 63 6f 6e 73 74 20 55  Arg){..  const U
49c3a 43 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0d 0a 20  Char *zInput;.. 
49c3b 20 55 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b   UChar *zOutput;
49c3c 0d 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0d  ..  int nInput;.
49c3d 0a 20 20 69 6e 74 20 6e 4f 75 74 70 75 74 3b 0d  .  int nOutput;.
49c3e 0a 0d 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  ...  UErrorCode 
49c3f 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
49c40 45 52 52 4f 52 3b 0d 0a 20 20 63 6f 6e 73 74 20  ERROR;..  const 
49c41 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 20 3d 20  char *zLocale = 
49c42 30 3b 0d 0a 0d 0a 20 20 61 73 73 65 72 74 28 6e  0;....  assert(n
49c43 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d 3d  Arg==1 || nArg==
49c44 32 29 3b 0d 0a 20 20 69 66 28 20 6e 41 72 67 3d  2);..  if( nArg=
49c45 3d 32 20 29 7b 0d 0a 20 20 20 20 7a 4c 6f 63 61  =2 ){..    zLoca
49c46 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
49c47 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
49c48 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29 3b  _text(apArg[1]);
49c49 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 7a 49 6e 70 75  ..  }....  zInpu
49c4a 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
49c4b 65 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30  e_text16(apArg[0
49c4c 5d 29 3b 0d 0a 20 20 69 66 28 20 21 7a 49 6e 70  ]);..  if( !zInp
49c4d 75 74 20 29 7b 0d 0a 20 20 20 20 72 65 74 75 72  ut ){..    retur
49c4e 6e 3b 0d 0a 20 20 7d 0d 0a 20 20 6e 49 6e 70 75  n;..  }..  nInpu
49c4f 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
49c50 65 5f 62 79 74 65 73 31 36 28 61 70 41 72 67 5b  e_bytes16(apArg[
49c51 30 5d 29 3b 0d 0a 0d 0a 20 20 6e 4f 75 74 70 75  0]);....  nOutpu
49c52 74 20 3d 20 6e 49 6e 70 75 74 20 2a 20 32 20 2b  t = nInput * 2 +
49c53 20 32 3b 0d 0a 20 20 7a 4f 75 74 70 75 74 20 3d   2;..  zOutput =
49c54 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
49c55 6e 4f 75 74 70 75 74 29 3b 0d 0a 20 20 69 66 28  nOutput);..  if(
49c56 20 21 7a 4f 75 74 70 75 74 20 29 7b 0d 0a 20 20   !zOutput ){..  
49c57 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a    return;..  }..
49c58 0d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
49c59 75 73 65 72 5f 64 61 74 61 28 70 29 20 29 7b 0d  user_data(p) ){.
49c5a 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 70 70 65  .    u_strToUppe
49c5b 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70  r(zOutput, nOutp
49c5c 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49  ut/2, zInput, nI
49c5d 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c  nput/2, zLocale,
49c5e 20 26 73 74 61 74 75 73 29 3b 0d 0a 20 20 7d 65   &status);..  }e
49c5f 6c 73 65 7b 0d 0a 20 20 20 20 75 5f 73 74 72 54  lse{..    u_strT
49c60 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75 74 2c 20  oLower(zOutput, 
49c61 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
49c62 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
49c63 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0d  cale, &status);.
49c64 0a 20 20 7d 0d 0a 0d 0a 20 20 69 66 28 20 21 55  .  }....  if( !U
49c65 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
49c66 20 29 7b 0d 0a 20 20 20 20 69 63 75 46 75 6e 63   ){..    icuFunc
49c67 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f  tionError(p, "u_
49c68 73 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73  strToLower()/u_s
49c69 74 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74  trToUpper", stat
49c6a 75 73 29 3b 0d 0a 20 20 20 20 72 65 74 75 72 6e  us);..    return
49c6b 3b 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 73 71 6c 69  ;..  }....  sqli
49c6c 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
49c6d 36 28 70 2c 20 7a 4f 75 74 70 75 74 2c 20 2d 31  6(p, zOutput, -1
49c6e 2c 20 78 46 72 65 65 29 3b 0d 0a 7d 0d 0a 0d 0a  , xFree);..}....
49c6f 2f 2a 0d 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e  /*..** Collation
49c70 20 73 65 71 75 65 6e 63 65 20 64 65 73 74 72 75   sequence destru
49c71 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 20 54  ctor function. T
49c72 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74  he pCtx argument
49c73 20 70 6f 69 6e 74 73 20 74 6f 0d 0a 2a 2a 20 61   points to..** a
49c74 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63   UCollator struc
49c75 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
49c76 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
49c77 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0d 0a 2a 2f  ucol_open()...*/
49c78 0d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  ..static void ic
49c79 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f  uCollationDel(vo
49c7a 69 64 20 2a 70 43 74 78 29 7b 0d 0a 20 20 55 43  id *pCtx){..  UC
49c7b 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43  ollator *p = (UC
49c7c 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0d  ollator *)pCtx;.
49c7d 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 29  .  ucol_close(p)
49c7e 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43  ;..}..../*..** C
49c7f 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
49c80 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  e comparison fun
49c81 63 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20  ction. The pCtx 
49c82 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20  argument points 
49c83 74 6f 0d 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74  to..** a UCollat
49c84 6f 72 20 73 74 72 75 63 74 75 72 65 20 70 72 65  or structure pre
49c85 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
49c86 64 20 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65  d using ucol_ope
49c87 6e 28 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63  n()...*/..static
49c88 20 69 6e 74 20 69 63 75 43 6f 6c 6c 61 74 69 6f   int icuCollatio
49c89 6e 43 6f 6c 6c 28 0d 0a 20 20 76 6f 69 64 20 2a  nColl(..  void *
49c8a 70 43 74 78 2c 0d 0a 20 20 69 6e 74 20 6e 4c 65  pCtx,..  int nLe
49c8b 66 74 2c 0d 0a 20 20 63 6f 6e 73 74 20 76 6f 69  ft,..  const voi
49c8c 64 20 2a 7a 4c 65 66 74 2c 0d 0a 20 20 69 6e 74  d *zLeft,..  int
49c8d 20 6e 52 69 67 68 74 2c 0d 0a 20 20 63 6f 6e 73   nRight,..  cons
49c8e 74 20 76 6f 69 64 20 2a 7a 52 69 67 68 74 0d 0a  t void *zRight..
49c8f 29 7b 0d 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e  ){..  UCollation
49c90 52 65 73 75 6c 74 20 72 65 73 3b 0d 0a 20 20 55  Result res;..  U
49c91 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55  Collator *p = (U
49c92 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b  Collator *)pCtx;
49c93 0d 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c 5f 73  ..  res = ucol_s
49c94 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68 61 72  trcoll(p, (UChar
49c95 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66 74 2f   *)zLeft, nLeft/
49c96 32 2c 20 28 55 43 68 61 72 20 2a 29 7a 52 69 67  2, (UChar *)zRig
49c97 68 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b 0d 0a  ht, nRight/2);..
49c98 20 20 73 77 69 74 63 68 28 20 72 65 73 20 29 7b    switch( res ){
49c99 0d 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  ..    case UCOL_
49c9a 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e 20  LESS:    return 
49c9b 2d 31 3b 0d 0a 20 20 20 20 63 61 73 65 20 55 43  -1;..    case UC
49c9c 4f 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75  OL_GREATER: retu
49c9d 72 6e 20 2b 31 3b 0d 0a 20 20 20 20 63 61 73 65  rn +1;..    case
49c9e 20 55 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72   UCOL_EQUAL:   r
49c9f 65 74 75 72 6e 20 30 3b 0d 0a 20 20 7d 0d 0a 20  eturn 0;..  }.. 
49ca0 20 61 73 73 65 72 74 28 21 22 55 6e 65 78 70 65   assert(!"Unexpe
49ca1 63 74 65 64 20 72 65 74 75 72 6e 20 76 61 6c 75  cted return valu
49ca2 65 20 66 72 6f 6d 20 75 63 6f 6c 5f 73 74 72 63  e from ucol_strc
49ca3 6f 6c 6c 28 29 22 29 3b 0d 0a 20 20 72 65 74 75  oll()");..  retu
49ca4 72 6e 20 30 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a  rn 0;..}..../*..
49ca5 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
49ca6 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72 20  n of the scalar 
49ca7 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f 6c 6f 61  function icu_loa
49ca8 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0d 0a  d_collation()...
49ca9 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 73 63 61 6c  **..** This scal
49caa 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  ar function is u
49cab 73 65 64 20 74 6f 20 61 64 64 20 49 43 55 20 63  sed to add ICU c
49cac 6f 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64 20 63  ollation based c
49cad 6f 6c 6c 61 74 69 6f 6e 20 0d 0a 2a 2a 20 74 79  ollation ..** ty
49cae 70 65 73 20 74 6f 20 61 6e 20 53 51 4c 69 74 65  pes to an SQLite
49caf 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
49cb0 74 69 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74 65  tion. It is inte
49cb1 6e 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65  nded to be calle
49cb2 64 0d 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73  d..** as follows
49cb3 3a 0d 0a 2a 2a 0d 0a 2a 2a 20 20 20 20 20 53 45  :..**..**     SE
49cb4 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63 6f  LECT icu_load_co
49cb5 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65 3e  llation(<locale>
49cb6 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d  , <collation-nam
49cb7 65 3e 29 3b 0d 0a 2a 2a 0d 0a 2a 2a 20 57 68 65  e>);..**..** Whe
49cb8 72 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61  re <locale> is a
49cb9 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
49cba 6e 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65  ng an ICU locale
49cbb 20 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65   identifier (i.e
49cbc 2e 0d 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22  ...** "en_AU", "
49cbd 74 72 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64  tr_TR" etc.) and
49cbe 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65   <collation-name
49cbf 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  > is the name of
49cc0 20 74 68 65 0d 0a 2a 2a 20 63 6f 6c 6c 61 74 69   the..** collati
49cc1 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20 63  on sequence to c
49cc2 72 65 61 74 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74  reate...*/..stat
49cc3 69 63 20 76 6f 69 64 20 69 63 75 4c 6f 61 64 43  ic void icuLoadC
49cc4 6f 6c 6c 61 74 69 6f 6e 28 0d 0a 20 20 73 71 6c  ollation(..  sql
49cc5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
49cc6 20 0d 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 0d   ..  int nArg, .
49cc7 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
49cc8 20 2a 2a 61 70 41 72 67 0d 0a 29 7b 0d 0a 20 20   **apArg..){..  
49cc9 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
49cca 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
49ccb 5f 75 73 65 72 5f 64 61 74 61 28 70 29 3b 0d 0a  _user_data(p);..
49ccc 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61    UErrorCode sta
49ccd 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52  tus = U_ZERO_ERR
49cce 4f 52 3b 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61  OR;..  const cha
49ccf 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20  r *zLocale;     
49cd0 20 2f 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74   /* Locale ident
49cd1 69 66 69 65 72 20 2d 20 28 65 67 2e 20 22 6a 70  ifier - (eg. "jp
49cd2 5f 4a 50 22 29 20 2a 2f 0d 0a 20 20 63 6f 6e 73  _JP") */..  cons
49cd3 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
49cd4 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c        /* SQL Col
49cd5 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
49cd6 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61 70 61 6e  name (eg. "japan
49cd7 65 73 65 22 29 20 2a 2f 0d 0a 20 20 55 43 6f 6c  ese") */..  UCol
49cd8 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61 74 6f  lator *pUCollato
49cd9 72 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c 69 62  r;    /* ICU lib
49cda 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f  rary collation o
49cdb 62 6a 65 63 74 20 2a 2f 0d 0a 20 20 69 6e 74 20  bject */..  int 
49cdc 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
49cdd 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
49cde 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74 65  code from sqlite
49cdf 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
49ce0 6f 6e 5f 78 28 29 20 2a 2f 0d 0a 0d 0a 20 20 61  on_x() */....  a
49ce1 73 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0d  ssert(nArg==2);.
49ce2 0a 20 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f  .  zLocale = (co
49ce3 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
49ce4 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70  e3_value_text(ap
49ce5 41 72 67 5b 30 5d 29 3b 0d 0a 20 20 7a 4e 61 6d  Arg[0]);..  zNam
49ce6 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
49ce7 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
49ce8 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0d  text(apArg[1]);.
49ce9 0a 0d 0a 20 20 69 66 28 20 21 7a 4c 6f 63 61 6c  ...  if( !zLocal
49cea 65 20 7c 7c 20 21 7a 4e 61 6d 65 20 29 7b 0d 0a  e || !zName ){..
49ceb 20 20 20 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d      return;..  }
49cec 0d 0a 0d 0a 20 20 70 55 43 6f 6c 6c 61 74 6f 72  ....  pUCollator
49ced 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c 6f   = ucol_open(zLo
49cee 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0d  cale, &status);.
49cef 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
49cf0 53 28 73 74 61 74 75 73 29 20 29 7b 0d 0a 20 20  S(status) ){..  
49cf1 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72    icuFunctionErr
49cf2 6f 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65 6e  or(p, "ucol_open
49cf3 22 2c 20 73 74 61 74 75 73 29 3b 0d 0a 20 20 20  ", status);..   
49cf4 20 72 65 74 75 72 6e 3b 0d 0a 20 20 7d 0d 0a 20   return;..  }.. 
49cf5 20 61 73 73 65 72 74 28 70 29 3b 0d 0a 0d 0a 20   assert(p);.... 
49cf6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
49cf7 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
49cf8 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  2(db, zName, SQL
49cf9 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
49cfa 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0d   *)pUCollator, .
49cfb 0a 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74  .      icuCollat
49cfc 69 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c  ionColl, icuColl
49cfd 61 74 69 6f 6e 44 65 6c 0d 0a 20 20 29 3b 0d 0a  ationDel..  );..
49cfe 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
49cff 5f 4f 4b 20 29 7b 0d 0a 20 20 20 20 75 63 6f 6c  _OK ){..    ucol
49d00 5f 63 6c 6f 73 65 28 70 55 43 6f 6c 6c 61 74 6f  _close(pUCollato
49d01 72 29 3b 0d 0a 20 20 20 20 73 71 6c 69 74 65 33  r);..    sqlite3
49d02 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 2c  _result_error(p,
49d03 20 22 45 72 72 6f 72 20 72 65 67 69 73 74 65 72   "Error register
49d04 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75  ing collation fu
49d05 6e 63 74 69 6f 6e 22 2c 20 2d 31 29 3b 0d 0a 20  nction", -1);.. 
49d06 20 7d 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20   }..}..../*..** 
49d07 52 65 67 69 73 74 65 72 20 74 68 65 20 49 43 55  Register the ICU
49d08 20 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e 63 74   extension funct
49d09 69 6f 6e 73 20 77 69 74 68 20 64 61 74 61 62 61  ions with databa
49d0a 73 65 20 64 62 2e 0d 0a 2a 2f 0d 0a 53 51 4c 49  se db...*/..SQLI
49d0b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
49d0c 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73 71  qlite3IcuInit(sq
49d0d 6c 69 74 65 33 20 2a 64 62 29 7b 0d 0a 20 20 73  lite3 *db){..  s
49d0e 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20  truct IcuScalar 
49d0f 7b 0d 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  {..    const cha
49d10 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
49d11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d12 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e 61 6d   /* Function nam
49d13 65 20 2a 2f 0d 0a 20 20 20 20 69 6e 74 20 6e 41  e */..    int nA
49d14 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
49d15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d16 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
49d17 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0d 0a 20   arguments */.. 
49d18 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20 20     int enc;     
49d19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49d1b 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e 63  Optimal text enc
49d1c 6f 64 69 6e 67 20 2a 2f 0d 0a 20 20 20 20 76 6f  oding */..    vo
49d1d 69 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20  id *pContext;   
49d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d1f 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
49d20 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63  e3_user_data() c
49d21 6f 6e 74 65 78 74 20 2a 2f 0d 0a 20 20 20 20 76  ontext */..    v
49d22 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
49d23 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
49d24 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
49d25 2a 29 3b 0d 0a 20 20 7d 20 73 63 61 6c 61 72 73  *);..  } scalars
49d26 5b 5d 20 3d 20 7b 0d 0a 20 20 20 20 7b 22 72 65  [] = {..    {"re
49d27 67 65 78 70 22 2c 20 32 2c 20 53 51 4c 49 54 45  gexp", 2, SQLITE
49d28 5f 41 4e 59 2c 20 20 20 20 20 20 20 20 20 20 30  _ANY,          0
49d29 2c 20 69 63 75 52 65 67 65 78 70 46 75 6e 63 7d  , icuRegexpFunc}
49d2a 2c 0d 0a 0d 0a 20 20 20 20 7b 22 6c 6f 77 65 72  ,....    {"lower
49d2b 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  ",  1, SQLITE_UT
49d2c 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69  F16,        0, i
49d2d 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0d 0a  cuCaseFunc16},..
49d2e 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32      {"lower",  2
49d2f 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20  , SQLITE_UTF16, 
49d30 20 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73         0, icuCas
49d31 65 46 75 6e 63 31 36 7d 2c 0d 0a 20 20 20 20 7b  eFunc16},..    {
49d32 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c  "upper",  1, SQL
49d33 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
49d34 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63  *)1, icuCaseFunc
49d35 31 36 7d 2c 0d 0a 20 20 20 20 7b 22 75 70 70 65  16},..    {"uppe
49d36 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
49d37 54 46 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20  TF16, (void*)1, 
49d38 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0d  icuCaseFunc16},.
49d39 0a 0d 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c  ...    {"lower",
49d3a 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    1, SQLITE_UTF8
49d3b 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  ,         0, icu
49d3c 43 61 73 65 46 75 6e 63 31 36 7d 2c 0d 0a 20 20  CaseFunc16},..  
49d3d 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20    {"lower",  2, 
49d3e 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
49d3f 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65 46       0, icuCaseF
49d40 75 6e 63 31 36 7d 2c 0d 0a 20 20 20 20 7b 22 75  unc16},..    {"u
49d41 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  pper",  1, SQLIT
49d42 45 5f 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29  E_UTF8,  (void*)
49d43 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
49d44 7d 2c 0d 0a 20 20 20 20 7b 22 75 70 70 65 72 22  },..    {"upper"
49d45 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  2, SQLITE_UTF
49d46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63  8,  (void*)1, ic
49d47 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0d 0a 0d  uCaseFunc16},...
49d48 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20  .    {"like",   
49d49 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
49d4a 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69          0, icuLi
49d4b 6b 65 46 75 6e 63 7d 2c 0d 0a 20 20 20 20 7b 22  keFunc},..    {"
49d4c 6c 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49  like",   3, SQLI
49d4d 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20  TE_UTF8,        
49d4e 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d   0, icuLikeFunc}
49d4f 2c 0d 0a 0d 0a 20 20 20 20 7b 22 69 63 75 5f 6c  ,....    {"icu_l
49d50 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20  oad_collation", 
49d51 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
49d52 20 28 76 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c   (void*)db, icuL
49d53 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0d 0a  oadCollation},..
49d54 20 20 7d 3b 0d 0a 0d 0a 20 20 69 6e 74 20 72 63    };....  int rc
49d55 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 20   = SQLITE_OK;.. 
49d56 20 69 6e 74 20 69 3b 0d 0a 0d 0a 20 20 66 6f 72   int i;....  for
49d57 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
49d58 5f 4f 4b 20 26 26 20 69 3c 28 69 6e 74 29 28 73  _OK && i<(int)(s
49d59 69 7a 65 6f 66 28 73 63 61 6c 61 72 73 29 2f 73  izeof(scalars)/s
49d5a 69 7a 65 6f 66 28 73 63 61 6c 61 72 73 5b 30 5d  izeof(scalars[0]
49d5b 29 29 3b 20 69 2b 2b 29 7b 0d 0a 20 20 20 20 73  )); i++){..    s
49d5c 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20  truct IcuScalar 
49d5d 2a 70 20 3d 20 26 73 63 61 6c 61 72 73 5b 69 5d  *p = &scalars[i]
49d5e 3b 0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
49d5f 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
49d60 69 6f 6e 28 0d 0a 20 20 20 20 20 20 20 20 64 62  ion(..        db
49d61 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 6e  , p->zName, p->n
49d62 41 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d 3e  Arg, p->enc, p->
49d63 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46 75  pContext, p->xFu
49d64 6e 63 2c 20 30 2c 20 30 0d 0a 20 20 20 20 29 3b  nc, 0, 0..    );
49d65 0d 0a 20 20 7d 0d 0a 0d 0a 20 20 72 65 74 75 72  ..  }....  retur
49d66 6e 20 72 63 3b 0d 0a 7d 0d 0a 0d 0a 23 69 66 20  n rc;..}....#if 
49d67 21 53 51 4c 49 54 45 5f 43 4f 52 45 0d 0a 53 51  !SQLITE_CORE..SQ
49d68 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
49d69 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
49d6a 6e 69 74 28 0d 0a 20 20 73 71 6c 69 74 65 33 20  nit(..  sqlite3 
49d6b 2a 64 62 2c 20 0d 0a 20 20 63 68 61 72 20 2a 2a  *db, ..  char **
49d6c 70 7a 45 72 72 4d 73 67 2c 0d 0a 20 20 63 6f 6e  pzErrMsg,..  con
49d6d 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
49d6e 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0d 0a 29  outines *pApi..)
49d6f 7b 0d 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45  {..  SQLITE_EXTE
49d70 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69  NSION_INIT2(pApi
49d71 29 0d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  )..  return sqli
49d72 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0d  te3IcuInit(db);.
49d73 0a 7d 0d 0a 23 65 6e 64 69 66 0d 0a 0d 0a 23 65  .}..#endif....#e
49d74 6e 64 69 66 0d 0a 0d 0a 2f 2a 2a 2a 2a 2a 2a 2a  ndif..../*******
49d75 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69  ******* End of i
49d76 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.c ***********
49d77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d79 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
49d7a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
49d7b 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20 2a  ile fts3_icu.c *
49d7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d7e 2a 2a 2a 2a 2a 2a 2a 2f 0d 0a 2f 2a 0d 0a 2a 2a  *******/../*..**
49d7f 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0d 0a 2a   2007 June 22..*
49d80 2a 0d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  *..** The author
49d81 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
49d82 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
49d83 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
49d84 61 63 65 20 6f 66 0d 0a 2a 2a 20 61 20 6c 65 67  ace of..** a leg
49d85 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
49d86 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0d 0a  is a blessing:..
49d87 2a 2a 0d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  **..**    May yo
49d88 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
49d89 74 20 65 76 69 6c 2e 0d 0a 2a 2a 20 20 20 20 4d  t evil...**    M
49d8a 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
49d8b 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
49d8c 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
49d8d 20 6f 74 68 65 72 73 2e 0d 0a 2a 2a 20 20 20 20   others...**    
49d8e 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
49d8f 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
49d90 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
49d91 20 67 69 76 65 2e 0d 0a 2a 2a 0d 0a 2a 2a 2a 2a   give...**..****
49d92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49d96 2a 2a 2a 2a 2a 0d 0a 2a 2a 20 54 68 69 73 20 66  *****..** This f
49d97 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
49d98 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66   tokenizer for f
49d99 74 73 33 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ts3 based on the
49d9a 20 49 43 55 20 6c 69 62 72 61 72 79 2e 0d 0a 2a   ICU library...*
49d9b 2f 0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
49d9c 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
49d9d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
49d9e 4e 41 42 4c 45 5f 46 54 53 33 29 0d 0a 23 69 66  NABLE_FTS3)..#if
49d9f 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
49da0 45 5f 49 43 55 0d 0a 0d 0a 2f 2a 20 23 69 6e 63  E_ICU..../* #inc
49da1 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20  lude <assert.h> 
49da2 2a 2f 0d 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  */../* #include 
49da3 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d 0a 0d  <string.h> */...
49da4 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
49da5 64 65 2f 75 62 72 6b 2e 68 3e 0d 0a 2f 2a 20 23  de/ubrk.h>../* #
49da6 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65  include <unicode
49da7 2f 75 63 6f 6c 2e 68 3e 20 2a 2f 0d 0a 2f 2a 20  /ucol.h> */../* 
49da8 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
49da9 65 2f 75 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0d  e/ustring.h> */.
49daa 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
49dab 64 65 2f 75 74 66 31 36 2e 68 3e 0d 0a 0d 0a 74  de/utf16.h>....t
49dac 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63  ypedef struct Ic
49dad 75 54 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f  uTokenizer IcuTo
49dae 6b 65 6e 69 7a 65 72 3b 0d 0a 74 79 70 65 64 65  kenizer;..typede
49daf 66 20 73 74 72 75 63 74 20 49 63 75 43 75 72 73  f struct IcuCurs
49db0 6f 72 20 49 63 75 43 75 72 73 6f 72 3b 0d 0a 0d  or IcuCursor;...
49db1 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65 6e  .struct IcuToken
49db2 69 7a 65 72 20 7b 0d 0a 20 20 73 71 6c 69 74 65  izer {..  sqlite
49db3 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65  3_tokenizer base
49db4 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61  ;..  char *zLoca
49db5 6c 65 3b 0d 0a 7d 3b 0d 0a 0d 0a 73 74 72 75 63  le;..};....struc
49db6 74 20 49 63 75 43 75 72 73 6f 72 20 7b 0d 0a 20  t IcuCursor {.. 
49db7 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
49db8 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0d  er_cursor base;.
49db9 0a 0d 0a 20 20 55 42 72 65 61 6b 49 74 65 72 61  ...  UBreakItera
49dba 74 6f 72 20 2a 70 49 74 65 72 3b 20 20 20 20 20  tor *pIter;     
49dbb 20 2f 2a 20 49 43 55 20 62 72 65 61 6b 2d 69 74   /* ICU break-it
49dbc 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  erator object */
49dbd 0d 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20  ..  int nChar;  
49dbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49dbf 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 43 68  /* Number of UCh
49dc0 61 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70  ar elements in p
49dc1 49 6e 70 75 74 20 2a 2f 0d 0a 20 20 55 43 68 61  Input */..  UCha
49dc2 72 20 2a 61 43 68 61 72 3b 20 20 20 20 20 20 20  r *aChar;       
49dc3 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
49dc4 6f 66 20 69 6e 70 75 74 20 75 73 69 6e 67 20 75  of input using u
49dc5 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 2a  tf-16 encoding *
49dc6 2f 0d 0a 20 20 69 6e 74 20 2a 61 4f 66 66 73 65  /..  int *aOffse
49dc7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
49dc8 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 65   /* Offsets of e
49dc9 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e  ach character in
49dca 20 75 74 66 2d 38 20 69 6e 70 75 74 20 2a 2f 0d   utf-8 input */.
49dcb 0a 0d 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72  ...  int nBuffer
49dcc 3b 0d 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 66  ;..  char *zBuff
49dcd 65 72 3b 0d 0a 0d 0a 20 20 69 6e 74 20 69 54 6f  er;....  int iTo
49dce 6b 65 6e 3b 0d 0a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a  ken;..};..../*..
49dcf 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
49dd0 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
49dd1 63 65 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20  ce...*/..static 
49dd2 69 6e 74 20 69 63 75 43 72 65 61 74 65 28 0d 0a  int icuCreate(..
49dd3 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20    int argc,     
49dd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49dd5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
49dd6 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
49dd7 72 67 76 5b 5d 20 2a 2f 0d 0a 20 20 63 6f 6e 73  rgv[] */..  cons
49dd8 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
49dd9 61 72 67 76 2c 20 20 20 20 20 20 20 20 20 20 20  argv,           
49dda 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 72   /* Tokenizer cr
49ddb 65 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  eation arguments
49ddc 20 2a 2f 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74   */..  sqlite3_t
49ddd 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b  okenizer **ppTok
49dde 65 6e 69 7a 65 72 20 20 20 20 20 20 2f 2a 20 4f  enizer      /* O
49ddf 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65  UT: Created toke
49de0 6e 69 7a 65 72 20 2a 2f 0d 0a 29 7b 0d 0a 20 20  nizer */..){..  
49de1 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b  IcuTokenizer *p;
49de2 0d 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0d 0a  ..  int n = 0;..
49de3 0d 0a 20 20 69 66 28 20 61 72 67 63 3e 30 20 29  ..  if( argc>0 )
49de4 7b 0d 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65  {..    n = strle
49de5 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0d 0a 20  n(argv[0])+1;.. 
49de6 20 7d 0d 0a 20 20 70 20 3d 20 28 49 63 75 54 6f   }..  p = (IcuTo
49de7 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74 65  kenizer *)sqlite
49de8 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
49de9 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29  IcuTokenizer)+n)
49dea 3b 0d 0a 20 20 69 66 28 20 21 70 20 29 7b 0d 0a  ;..  if( !p ){..
49deb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
49dec 45 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20  E_NOMEM;..  }.. 
49ded 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69   memset(p, 0, si
49dee 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69 7a 65  zeof(IcuTokenize
49def 72 29 29 3b 0d 0a 0d 0a 20 20 69 66 28 20 6e 20  r));....  if( n 
49df0 29 7b 0d 0a 20 20 20 20 70 2d 3e 7a 4c 6f 63 61  ){..    p->zLoca
49df1 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b  le = (char *)&p[
49df2 31 5d 3b 0d 0a 20 20 20 20 6d 65 6d 63 70 79 28  1];..    memcpy(
49df3 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72 67 76  p->zLocale, argv
49df4 5b 30 5d 2c 20 6e 29 3b 0d 0a 20 20 7d 0d 0a 0d  [0], n);..  }...
49df5 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  .  *ppTokenizer 
49df6 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  = (sqlite3_token
49df7 69 7a 65 72 20 2a 29 70 3b 0d 0a 0d 0a 20 20 72  izer *)p;....  r
49df8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
49df9 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 44 65  ..}..../*..** De
49dfa 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
49dfb 72 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69 6e  r..*/..static in
49dfc 74 20 69 63 75 44 65 73 74 72 6f 79 28 73 71 6c  t icuDestroy(sql
49dfd 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
49dfe 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0d 0a 20 20  pTokenizer){..  
49dff 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20  IcuTokenizer *p 
49e00 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  = (IcuTokenizer 
49e01 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0d 0a 20  *)pTokenizer;.. 
49e02 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
49e03 3b 0d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
49e04 54 45 5f 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d  TE_OK;..}..../*.
49e05 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62  .** Prepare to b
49e06 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  egin tokenizing 
49e07 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72  a particular str
49e08 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0d  ing.  The input.
49e09 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65  .** string to be
49e0a 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49   tokenized is pI
49e0b 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31  nput[0..nBytes-1
49e0c 5d 2e 20 20 41 20 63 75 72 73 6f 72 0d 0a 2a 2a  ].  A cursor..**
49e0d 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
49e0e 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
49e0f 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
49e10 65 74 75 72 6e 65 64 20 69 6e 20 0d 0a 2a 2a 20  eturned in ..** 
49e11 2a 70 70 43 75 72 73 6f 72 2e 0d 0a 2a 2f 0d 0a  *ppCursor...*/..
49e12 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 4f 70  static int icuOp
49e13 65 6e 28 0d 0a 20 20 73 71 6c 69 74 65 33 5f 74  en(..  sqlite3_t
49e14 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
49e15 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
49e16 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
49e17 2f 0d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  /..  const char 
49e18 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20  *zInput,        
49e19 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
49e1a 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0d 0a  nput string */..
49e1b 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20    int nInput,   
49e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49e1d 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
49e1e 74 68 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20  th of zInput in 
49e1f 62 79 74 65 73 20 2a 2f 0d 0a 20 20 73 71 6c 69  bytes */..  sqli
49e20 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
49e21 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20  rsor **ppCursor 
49e22 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e     /* OUT: Token
49e23 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a  ization cursor *
49e24 2f 0d 0a 29 7b 0d 0a 20 20 49 63 75 54 6f 6b 65  /..){..  IcuToke
49e25 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75 54  nizer *p = (IcuT
49e26 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65  okenizer *)pToke
49e27 6e 69 7a 65 72 3b 0d 0a 20 20 49 63 75 43 75 72  nizer;..  IcuCur
49e28 73 6f 72 20 2a 70 43 73 72 3b 0d 0a 0d 0a 20 20  sor *pCsr;....  
49e29 63 6f 6e 73 74 20 69 6e 74 33 32 5f 74 20 6f 70  const int32_t op
49e2a 74 20 3d 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f  t = U_FOLD_CASE_
49e2b 44 45 46 41 55 4c 54 3b 0d 0a 20 20 55 45 72 72  DEFAULT;..  UErr
49e2c 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
49e2d 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0d 0a 20  U_ZERO_ERROR;.. 
49e2e 20 69 6e 74 20 6e 43 68 61 72 3b 0d 0a 0d 0a 20   int nChar;.... 
49e2f 20 55 43 68 61 72 33 32 20 63 3b 0d 0a 20 20 69   UChar32 c;..  i
49e30 6e 74 20 69 49 6e 70 75 74 20 3d 20 30 3b 0d 0a  nt iInput = 0;..
49e31 20 20 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 0d    int iOut = 0;.
49e32 0a 0d 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  ...  *ppCursor =
49e33 20 30 3b 0d 0a 0d 0a 20 20 69 66 28 20 6e 49 6e   0;....  if( nIn
49e34 70 75 74 3c 30 20 29 7b 0d 0a 20 20 20 20 6e 49  put<0 ){..    nI
49e35 6e 70 75 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49  nput = strlen(zI
49e36 6e 70 75 74 29 3b 0d 0a 20 20 7d 0d 0a 20 20 6e  nput);..  }..  n
49e37 43 68 61 72 20 3d 20 6e 49 6e 70 75 74 2b 31 3b  Char = nInput+1;
49e38 0d 0a 20 20 70 43 73 72 20 3d 20 28 49 63 75 43  ..  pCsr = (IcuC
49e39 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
49e3a 6d 61 6c 6c 6f 63 28 0d 0a 20 20 20 20 20 20 73  malloc(..      s
49e3b 69 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29  izeof(IcuCursor)
49e3c 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
49e3d 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 20 2a    /* IcuCursor *
49e3e 2f 0d 0a 20 20 20 20 20 20 6e 43 68 61 72 20 2a  /..      nChar *
49e3f 20 73 69 7a 65 6f 66 28 55 43 68 61 72 29 20 2b   sizeof(UChar) +
49e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
49e41 63 75 43 75 72 73 6f 72 2e 61 43 68 61 72 5b 5d  cuCursor.aChar[]
49e42 20 2a 2f 0d 0a 20 20 20 20 20 20 28 6e 43 68 61   */..      (nCha
49e43 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66 28 69 6e  r+1) * sizeof(in
49e44 74 29 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t)            /*
49e45 20 49 63 75 43 75 72 73 6f 72 2e 61 4f 66 66 73   IcuCursor.aOffs
49e46 65 74 5b 5d 20 2a 2f 0d 0a 20 20 29 3b 0d 0a 20  et[] */..  );.. 
49e47 20 69 66 28 20 21 70 43 73 72 20 29 7b 0d 0a 20   if( !pCsr ){.. 
49e48 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
49e49 5f 4e 4f 4d 45 4d 3b 0d 0a 20 20 7d 0d 0a 20 20  _NOMEM;..  }..  
49e4a 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
49e4b 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72  sizeof(IcuCursor
49e4c 29 29 3b 0d 0a 20 20 70 43 73 72 2d 3e 61 43 68  ));..  pCsr->aCh
49e4d 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29 26 70  ar = (UChar *)&p
49e4e 43 73 72 5b 31 5d 3b 0d 0a 20 20 70 43 73 72 2d  Csr[1];..  pCsr-
49e4f 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 20  >aOffset = (int 
49e50 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b 6e  *)&pCsr->aChar[n
49e51 43 68 61 72 5d 3b 0d 0a 0d 0a 20 20 70 43 73 72  Char];....  pCsr
49e52 2d 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20  ->aOffset[iOut] 
49e53 3d 20 69 49 6e 70 75 74 3b 0d 0a 20 20 55 38 5f  = iInput;..  U8_
49e54 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e  NEXT(zInput, iIn
49e55 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b  put, nInput, c);
49e56 20 0d 0a 20 20 77 68 69 6c 65 28 20 63 3e 30 20   ..  while( c>0 
49e57 29 7b 0d 0a 20 20 20 20 69 6e 74 20 69 73 45 72  ){..    int isEr
49e58 72 6f 72 20 3d 20 30 3b 0d 0a 20 20 20 20 63 20  ror = 0;..    c 
49e59 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 63 2c 20  = u_foldCase(c, 
49e5a 6f 70 74 29 3b 0d 0a 20 20 20 20 55 31 36 5f 41  opt);..    U16_A
49e5b 50 50 45 4e 44 28 70 43 73 72 2d 3e 61 43 68 61  PPEND(pCsr->aCha
49e5c 72 2c 20 69 4f 75 74 2c 20 6e 43 68 61 72 2c 20  r, iOut, nChar, 
49e5d 63 2c 20 69 73 45 72 72 6f 72 29 3b 0d 0a 20 20  c, isError);..  
49e5e 20 20 69 66 28 20 69 73 45 72 72 6f 72 20 29 7b    if( isError ){
49e5f 0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
49e60 66 72 65 65 28 70 43 73 72 29 3b 0d 0a 20 20 20  free(pCsr);..   
49e61 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
49e62 5f 45 52 52 4f 52 3b 0d 0a 20 20 20 20 7d 0d 0a  _ERROR;..    }..
49e63 20 20 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65      pCsr->aOffse
49e64 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74  t[iOut] = iInput
49e65 3b 0d 0a 0d 0a 20 20 20 20 69 66 28 20 69 49 6e  ;....    if( iIn
49e66 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b 0d 0a 20  put<nInput ){.. 
49e67 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e       U8_NEXT(zIn
49e68 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e  put, iInput, nIn
49e69 70 75 74 2c 20 63 29 3b 0d 0a 20 20 20 20 7d 65  put, c);..    }e
49e6a 6c 73 65 7b 0d 0a 20 20 20 20 20 20 63 20 3d 20  lse{..      c = 
49e6b 30 3b 0d 0a 20 20 20 20 7d 0d 0a 20 20 7d 0d 0a  0;..    }..  }..
49e6c 0d 0a 20 20 70 43 73 72 2d 3e 70 49 74 65 72 20  ..  pCsr->pIter 
49e6d 3d 20 75 62 72 6b 5f 6f 70 65 6e 28 55 42 52 4b  = ubrk_open(UBRK
49e6e 5f 57 4f 52 44 2c 20 70 2d 3e 7a 4c 6f 63 61 6c  _WORD, p->zLocal
49e6f 65 2c 20 70 43 73 72 2d 3e 61 43 68 61 72 2c 20  e, pCsr->aChar, 
49e70 69 4f 75 74 2c 20 26 73 74 61 74 75 73 29 3b 0d  iOut, &status);.
49e71 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
49e72 53 28 73 74 61 74 75 73 29 20 29 7b 0d 0a 20 20  S(status) ){..  
49e73 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
49e74 43 73 72 29 3b 0d 0a 20 20 20 20 72 65 74 75 72  Csr);..    retur
49e75 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0d  n SQLITE_ERROR;.
49e76 0a 20 20 7d 0d 0a 20 20 70 43 73 72 2d 3e 6e 43  .  }..  pCsr->nC
49e77 68 61 72 20 3d 20 69 4f 75 74 3b 0d 0a 0d 0a 20  har = iOut;.... 
49e78 20 75 62 72 6b 5f 66 69 72 73 74 28 70 43 73 72   ubrk_first(pCsr
49e79 2d 3e 70 49 74 65 72 29 3b 0d 0a 20 20 2a 70 70  ->pIter);..  *pp
49e7a 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65  Cursor = (sqlite
49e7b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
49e7c 6f 72 20 2a 29 70 43 73 72 3b 0d 0a 20 20 72 65  or *)pCsr;..  re
49e7d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d  turn SQLITE_OK;.
49e7e 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 43 6c 6f  .}..../*..** Clo
49e7f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  se a tokenizatio
49e80 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75  n cursor previou
49e81 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20  sly opened by a 
49e82 63 61 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28  call to icuOpen(
49e83 29 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  )...*/..static i
49e84 6e 74 20 69 63 75 43 6c 6f 73 65 28 73 71 6c 69  nt icuClose(sqli
49e85 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
49e86 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0d  rsor *pCursor){.
49e87 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70 43  .  IcuCursor *pC
49e88 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20  sr = (IcuCursor 
49e89 2a 29 70 43 75 72 73 6f 72 3b 0d 0a 20 20 75 62  *)pCursor;..  ub
49e8a 72 6b 5f 63 6c 6f 73 65 28 70 43 73 72 2d 3e 70  rk_close(pCsr->p
49e8b 49 74 65 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65  Iter);..  sqlite
49e8c 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 7a 42 75  3_free(pCsr->zBu
49e8d 66 66 65 72 29 3b 0d 0a 20 20 73 71 6c 69 74 65  ffer);..  sqlite
49e8e 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0d 0a 20  3_free(pCsr);.. 
49e8f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
49e90 4b 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20  K;..}..../*..** 
49e91 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74  Extract the next
49e92 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f   token from a to
49e93 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
49e94 72 2e 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 69  r...*/..static i
49e95 6e 74 20 69 63 75 4e 65 78 74 28 0d 0a 20 20 73  nt icuNext(..  s
49e96 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
49e97 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
49e98 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
49e99 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
49e9a 70 65 6e 20 2a 2f 0d 0a 20 20 63 6f 6e 73 74 20  pen */..  const 
49e9b 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20  char **ppToken, 
49e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49e9d 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69   OUT: *ppToken i
49e9e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  s the token text
49e9f 20 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 6e 42 79   */..  int *pnBy
49ea0 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
49ea1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
49ea2 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
49ea3 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0d 0a  es in token */..
49ea4 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66    int *piStartOf
49ea5 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
49ea6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74        /* OUT: St
49ea7 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  arting offset of
49ea8 20 74 6f 6b 65 6e 20 2a 2f 0d 0a 20 20 69 6e 74   token */..  int
49ea9 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20   *piEndOffset,  
49eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49eab 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20   /* OUT: Ending 
49eac 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
49ead 2a 2f 0d 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73  */..  int *piPos
49eae 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ition           
49eaf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
49eb0 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67  : Position integ
49eb1 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0d 0a  er of token */..
49eb2 29 7b 0d 0a 20 20 49 63 75 43 75 72 73 6f 72 20  ){..  IcuCursor 
49eb3 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75 72 73  *pCsr = (IcuCurs
49eb4 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0d 0a 0d  or *)pCursor;...
49eb5 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 3d 20  .  int iStart = 
49eb6 30 3b 0d 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d  0;..  int iEnd =
49eb7 20 30 3b 0d 0a 20 20 69 6e 74 20 6e 42 79 74 65   0;..  int nByte
49eb8 20 3d 20 30 3b 0d 0a 0d 0a 20 20 77 68 69 6c 65   = 0;....  while
49eb9 28 20 69 53 74 61 72 74 3d 3d 69 45 6e 64 20 29  ( iStart==iEnd )
49eba 7b 0d 0a 20 20 20 20 55 43 68 61 72 33 32 20 63  {..    UChar32 c
49ebb 3b 0d 0a 0d 0a 20 20 20 20 69 53 74 61 72 74 20  ;....    iStart 
49ebc 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28 70  = ubrk_current(p
49ebd 43 73 72 2d 3e 70 49 74 65 72 29 3b 0d 0a 20 20  Csr->pIter);..  
49ebe 20 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65    iEnd = ubrk_ne
49ebf 78 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  xt(pCsr->pIter);
49ec0 0d 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d  ..    if( iEnd==
49ec1 55 42 52 4b 5f 44 4f 4e 45 20 29 7b 0d 0a 20 20  UBRK_DONE ){..  
49ec2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
49ec3 45 5f 44 4f 4e 45 3b 0d 0a 20 20 20 20 7d 0d 0a  E_DONE;..    }..
49ec4 0d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53 74  ..    while( iSt
49ec5 61 72 74 3c 69 45 6e 64 20 29 7b 0d 0a 20 20 20  art<iEnd ){..   
49ec6 20 20 20 69 6e 74 20 69 57 68 69 74 65 20 3d 20     int iWhite = 
49ec7 69 53 74 61 72 74 3b 0d 0a 20 20 20 20 20 20 55  iStart;..      U
49ec8 38 5f 4e 45 58 54 28 70 43 73 72 2d 3e 61 43 68  8_NEXT(pCsr->aCh
49ec9 61 72 2c 20 69 57 68 69 74 65 2c 20 70 43 73 72  ar, iWhite, pCsr
49eca 2d 3e 6e 43 68 61 72 2c 20 63 29 3b 0d 0a 20 20  ->nChar, c);..  
49ecb 20 20 20 20 69 66 28 20 75 5f 69 73 73 70 61 63      if( u_isspac
49ecc 65 28 63 29 20 29 7b 0d 0a 20 20 20 20 20 20 20  e(c) ){..       
49ecd 20 69 53 74 61 72 74 20 3d 20 69 57 68 69 74 65   iStart = iWhite
49ece 3b 0d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0d  ;..      }else{.
49ecf 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0d  .        break;.
49ed0 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 7d 0d  .      }..    }.
49ed1 0a 20 20 20 20 61 73 73 65 72 74 28 69 53 74 61  .    assert(iSta
49ed2 72 74 3c 3d 69 45 6e 64 29 3b 0d 0a 20 20 7d 0d  rt<=iEnd);..  }.
49ed3 0a 0d 0a 20 20 64 6f 20 7b 0d 0a 20 20 20 20 55  ...  do {..    U
49ed4 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
49ed5 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
49ed6 0d 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 20  ..    if( nByte 
49ed7 29 7b 0d 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ){..      char *
49ed8 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  zNew = sqlite3_r
49ed9 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a 42 75  ealloc(pCsr->zBu
49eda 66 66 65 72 2c 20 6e 42 79 74 65 29 3b 0d 0a 20  ffer, nByte);.. 
49edb 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29       if( !zNew )
49edc 7b 0d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  {..        retur
49edd 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0d  n SQLITE_NOMEM;.
49ede 0a 20 20 20 20 20 20 7d 0d 0a 20 20 20 20 20 20  .      }..      
49edf 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 20 3d 20  pCsr->zBuffer = 
49ee0 7a 4e 65 77 3b 0d 0a 20 20 20 20 20 20 70 43 73  zNew;..      pCs
49ee1 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e 42 79  r->nBuffer = nBy
49ee2 74 65 3b 0d 0a 20 20 20 20 7d 0d 0a 0d 0a 20 20  te;..    }....  
49ee3 20 20 75 5f 73 74 72 54 6f 55 54 46 38 28 0d 0a    u_strToUTF8(..
49ee4 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42          pCsr->zB
49ee5 75 66 66 65 72 2c 20 70 43 73 72 2d 3e 6e 42 75  uffer, pCsr->nBu
49ee6 66 66 65 72 2c 20 26 6e 42 79 74 65 2c 20 20 20  ffer, &nByte,   
49ee7 20 2f 2a 20 4f 75 74 70 75 74 20 76 61 72 73 20   /* Output vars 
49ee8 2a 2f 0d 0a 20 20 20 20 20 20 20 20 26 70 43 73  */..        &pCs
49ee9 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72 74 5d  r->aChar[iStart]
49eea 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c 20 20  , iEnd-iStart,  
49eeb 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 76 61       /* Input va
49eec 72 73 20 2a 2f 0d 0a 20 20 20 20 20 20 20 20 26  rs */..        &
49eed 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
49eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49eef 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
49ef0 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72  t success/failur
49ef1 65 20 2a 2f 0d 0a 20 20 20 20 29 3b 0d 0a 20 20  e */..    );..  
49ef2 7d 20 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70  } while( nByte>p
49ef3 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0d  Csr->nBuffer );.
49ef4 0a 0d 0a 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20  ...  *ppToken = 
49ef5 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 3b 0d 0a  pCsr->zBuffer;..
49ef6 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e 42 79    *pnBytes = nBy
49ef7 74 65 3b 0d 0a 20 20 2a 70 69 53 74 61 72 74 4f  te;..  *piStartO
49ef8 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f  ffset = pCsr->aO
49ef9 66 66 73 65 74 5b 69 53 74 61 72 74 5d 3b 0d 0a  ffset[iStart];..
49efa 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d    *piEndOffset =
49efb 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69   pCsr->aOffset[i
49efc 45 6e 64 5d 3b 0d 0a 20 20 2a 70 69 50 6f 73 69  End];..  *piPosi
49efd 74 69 6f 6e 20 3d 20 70 43 73 72 2d 3e 69 54 6f  tion = pCsr->iTo
49efe 6b 65 6e 2b 2b 3b 0d 0a 0d 0a 20 20 72 65 74 75  ken++;....  retu
49eff 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0d 0a 7d  rn SQLITE_OK;..}
49f00 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 54 68 65 20 73  ..../*..** The s
49f01 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74  et of routines t
49f02 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  hat implement th
49f03 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a  e simple tokeniz
49f04 65 72 0d 0a 2a 2f 0d 0a 73 74 61 74 69 63 20 63  er..*/..static c
49f05 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
49f06 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 63  enizer_module ic
49f07 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
49f08 20 3d 20 7b 0d 0a 20 20 30 2c 20 20 20 20 20 20   = {..  0,      
49f09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f0a 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
49f0b 20 2a 2f 0d 0a 20 20 69 63 75 43 72 65 61 74 65   */..  icuCreate
49f0c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
49f0d 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20      /* xCreate  
49f0e 2a 2f 0d 0a 20 20 69 63 75 44 65 73 74 72 6f 79  */..  icuDestroy
49f0f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
49f10 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a     /* xCreate  *
49f11 2f 0d 0a 20 20 69 63 75 4f 70 65 6e 2c 20 20 20  /..  icuOpen,   
49f12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f13 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20 2a 2f    /* xOpen    */
49f14 0d 0a 20 20 69 63 75 43 6c 6f 73 65 2c 20 20 20  ..  icuClose,   
49f15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f16 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 2a 2f 0d   /* xClose   */.
49f17 0a 20 20 69 63 75 4e 65 78 74 2c 20 20 20 20 20  .  icuNext,     
49f18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f19 2f 2a 20 78 4e 65 78 74 20 20 20 20 2a 2f 0d 0a  /* xNext    */..
49f1a 7d 3b 0d 0a 0d 0a 2f 2a 0d 0a 2a 2a 20 53 65 74  };..../*..** Set
49f1b 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f   *ppModule to po
49f1c 69 6e 74 20 61 74 20 74 68 65 20 69 6d 70 6c 65  int at the imple
49f1d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
49f1e 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72 2e 0d   ICU tokenizer..
49f1f 0a 2a 2f 0d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/..SQLITE_PRIV
49f20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
49f21 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72  Fts3IcuTokenizer
49f22 4d 6f 64 75 6c 65 28 0d 0a 20 20 73 71 6c 69 74  Module(..  sqlit
49f23 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
49f24 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64  ule const**ppMod
49f25 75 6c 65 0d 0a 29 7b 0d 0a 20 20 2a 70 70 4d 6f  ule..){..  *ppMo
49f26 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b 65 6e  dule = &icuToken
49f27 69 7a 65 72 4d 6f 64 75 6c 65 3b 0d 0a 7d 0d 0a  izerModule;..}..
49f28 0d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  ..#endif /* defi
49f29 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
49f2a 45 5f 49 43 55 29 20 2a 2f 0d 0a 23 65 6e 64 69  E_ICU) */..#endi
49f2b 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
49f2c 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
49f2d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
49f2e 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0d 0a 0d 0a  BLE_FTS3) */....
49f2f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
49f30 45 6e 64 20 6f 66 20 66 74 73 33 5f 69 63 75 2e  End of fts3_icu.
49f31 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
49f32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49f33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0d  **************/.
49f34 0a                                               .